Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.

Similar presentations


Presentation on theme: "Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson."— Presentation transcript:

1 Prolog Overview Syntax Mechanism Summary

2 Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson (1965) created algorithms for resolution refutation.

3 Resolution P V Q ~P V A Q V A star(A75) V galaxy(andromeda) ~star(X) V bright(X) galaxy(andromeda) V bright(A75) Θ = {X/A75}

4 History First Prolog Program written in France early 1970s as part of a project in natural language understanding. Theoretical background defined by Kowalski, Hayes, and others. Major development of the language done from 1975-1979 at the dept. of artificial intelligence of the University of Edinburgh.

5 Applications Automatic code generation Program verification Design of high-level specification languages Example: Mathematica

6 Examples “Everyone likes Susie” likes(X,susie) “George likes Kate and George likes Susie” likes(george,kate), likes(george,susie) “George likes Kate or George likes Susie” likes(george,kate) ; likes(george,susie) “George likes Susie if George does not like Kate” likes(george,susie) :- not(likes(george,kate))

7 Prolog Overview Syntax Mechanism Summary

8 Syntax There are many dialects of Prolog. We will use C-Prolog (Clocksin and Mellish 1984) Prolog uses Predicate Calculus as its language. EnglishPredicate C.Prolog And^, OrV; Only if  :- Not~not

9 Prolog Overview Syntax Mechanism Summary

10 Prolog’s Mechanism A Prolog program is a set of specifications in predicate calculus. The specification is known as the database of the system. Prolog is an interactive language (the user enters queries in response to a prompt).

11 Example 1 likes(george,kate). likes(george,susie). likes(george,wine). likes(susie,wine). likes(kate,gin). likes(kate,susie). ?- likes(george,kate) yes ?- likes(george,beer) no

12 Example 2 likes(george,kate). likes(george,susie). likes(george,wine). ?- likes(george,X) X = kate ; X = susie ; X = wine ; no

13 Points to Consider Successive user prompts ; cause the interpreter to return all terms that can be substituted for X. They are returned in the order found. **ORDER IS IMPORTANT ** PROLOG adopts the closed-world assumption: All knowledge of the world is present in the database. If a term is not in the database, assume it is false.

14 Defining Rules To define a rule use logical implication :- Example: friends(X,Y) :- likes(X,Z), likes(Y,Z). Only one predicate is allowed on the left of the implication The predicate on the left cannot be negated This is Horn Clause Logic

15 Example Rule likes(george,kate). likes(george,susie). likes(george,wine). likes(susie,wine). likes(kate,gin). likes(kate,susie). friends(X,Y) :- likes(X,Z), likes(Y,Z). ?- friends(george,susie) yes

16 Quantifiers When a variable appears in the specification of a database, the variable is universally quantified. Example: likes(susie,Y) “Susie likes everyone” For the existentially quantifier one may do two things: a.Enter the value directly into the database likes(george,Z) becomes likes(george,wine) b. Query the interpreter ?- likes(george,Z) returns a value for Z if one exists.

17 Recursion in Prolog Recursion is the primary control structure in Prolog. Let’s look at an example with lists: Elements in lists are enclosed by brackets: [1,2,3,4] [[george,kate],[allen,amy],[don,pat]] []

18 lists The first element of a list can be separated from the tail using operator | Example: Match the list [tom,dick,harry,fred] to [X|Y] then X = tom and Y = [dick,harry,fred] [X,Y|Z] then X = tom, Y = dick, and Z = [harry,fred] [V,W,X,Y,Z|U] will not match [tom,X|[harry,fred]] gives X = dick

19 List membership We want to write a function member that works as follows: ?- member(a,[a,b,c,d,e]) yes ?- member(a,[1,2,3,4]) no ?- member(X,[a,b,c]) X = a ; X = b ; X = c; no

20 Function membership Define two predicates: member(X,[X|T]). member(X,[Y|T]) :- member(X,T). Trace the following call ?- member(c,[a,b,c]) yes

21 Function membership A more elegant definition uses anonymous variables: member(X,[X,_]). member(X,[_|T]) :- member(X,T). The symbol _ indicates that the contents of that variable is unimportant.

22 Function write list writelist([]). writelist([H|T]) :- write(H), nl, writelist(T). A reverse write of a list is as follows: reverse_writelist([]). reverse_writelist([H|T]) :- reverse_writelist(T), write(H), nl.

23 Other functions ?- assertz(likes(david,sarah)) Adds this predicate to the database. To remove a predicate p use ?- retract(p)

24 Other functions To download a database of sentences: ?- consult(myfile) yes Or ?- [myfile] To read and write from console: ?- read(X) ?- write(X)

25 Prolog Overview Syntax Mechanism Summary

26 There are many dialects of Prolog; we will use C-Prolog (Clocksin and Mellish 1984). Prolog uses Predicate Calculus as its language. A Prolog program is a set of specifications in predicate calculus. The specification is known as the database of the system. Prolog is an interactive language (the user enters queries in response to a prompt). Recursion is the primary control structure in Prolog. PROLOG adopts the closed-world assumption: All knowledge of the world is present in the database. If a term is not in the database assume it is false.


Download ppt "Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson."

Similar presentations


Ads by Google