Course Aims ● Introduce a declarative style of programming ● Fundamental elements of Prolog: terms, Prolog clauses, lists, arithmetic, cut, negation ● Write programs in Prolog and understand their Andrew Rice execution Michealmas 2007 ● Learn to use difference structures ● Understand the basic principles of constraint 2 programming Assessment Supervision Work ● 1 Exam question in Paper 4 ● Separate set of questions at the end of the lecture handout ● Tickable Assessed Exercise ● I will give some pointers and outline solutions – you must get a tick for either Prolog or C & C++ during the lectures (next term) – more information to follow 3 4
Recommended Text Lecture 1 ● Logic programming and declarative programs ● Introduction to Prolog ● How to use it “PROLOG Programming for Artificial Intelligence”, Ivan Bratko ● Prolog syntax: Terms ● Unification ● Solving a logic puzzle 5 6 Imperative Programming Functional Programming /* to compute the sum of the list, go through the * list adding each value to the accumulator */ (* The sum of the empty list is zero and the sum of the list with head h and tail t is h plus the sum of the int sum(int[] list ) { tail *) int result = 0; for(int i=0; i<list.length; ++i) { fun sum([]) = 0 result += list[i]; | sum(h::t) = h + sum(t); } return result; } 7 8
Logic Programming Prolog Programs Answer Questions % the sum of the empty list is zero sum([],0). Facts + Rules % the sum of the list with head H and tail T is N % if the sum of the list T is M and N is M + H sum([H|T],N) :- sum(T,M), N is M+H. This is a declarative reading of a program Questions Answers - it describes the result not the procedure 9 10 Prolog came from the field of Modern Prolog Interpreters Use the Natural Language Processing Warren Abstract Machine PROgramming en LOGique Colmerauer, A., Kanoui, H., Roussel, P . and David H. D. Warren. “An abstract Prolog Pasero, R. “Un systeme de communication instruction set.” Technical Note 309, SRI homme-machine en français”, Groupe de International, Menlo Park, CA, October 1983. Recherche en Intelligence Artificielle, Université d'Aix-Marseille. 1973. 11 12
Prolog's database can answer You are expected to use SWI-Prolog simple questions > prolog ......................or maybe pl on your system ?- [user]. .............get ready to enter a new program ● Open-source Prolog environment. |: milestone(rousell,1972). |: milestone(warren,1983). ● Development began in 1987 |: milestone(swiprolog,1987). |: milestone(yourcourse,2007). ........................type [CTRL]-D when done |: % user://1 compiled 0.01 sec, 764 bytes ● Available for Linux, MacOS X and Windows. Yes ?- milestone(warren,1983). .........................................ask it a question Yes ....................................the answer is “yes” ● http://www.swi-prolog.org/ ?- milestone(swiprolog,X). ....................................let it find an answer X=1987 ....................................the answer is 1987 Yes We will use SWI-Prolog throughout this course ?- milestone(yourcourse,2000). .........................................ask it a question No ......................................the answer is “no” Get a copy! 13 14 ?- halt. ......................................exit the interpreter We will usually write programs to a Our program is composed of facts source file on disk and queries > cat milestone.pl > cat milestone.pl .................enter your program in a text file milestone(rousell,1972). .......................it must have a .pl extension milestone(rousell,1972). milestone(warren,1983). milestone(warren,1983). These are facts milestone(swiprolog,1987). milestone(swiprolog,1987). milestone(yourcourse,2007). (a particular type of clause ) milestone(yourcourse,2007). > prolog ?- [milestone]. ...........instruct prolog to load the program > prolog ?- milestone(warren,1983). ?- [milestone]. Yes These are queries ?- milestone(X,Y). ..............................................find answers ?- milestone(warren,1983). X = rousell Yes Y = 1972 ; ............you type a semi-colon (;) for more ?- milestone(X,Y). X = rousell X= warren Y = 1983 you press enter when you've had enough Y = 1972 ; Yes 15 16 ?- halt. X= warren Y = 1983
Terms can be Using the prolog shell Constants,Compounds or Variables ● The Prolog shell accepts only queries at the Constants top-level. Compound ● Press enter to accept the answer and return to likes(pooh_bear,honey) Numbers: 1 -2 plus(4,mult(3,plus(1,9))) the top level 3.14 Atoms: tigger ● Type a semi-colon (;) to request the next '100 Acre Wood' answer Variables ● Type w to fully display a long result which X Sticks 17 18 Prolog has abbreviated Unification is Prolog's fundamental Zebra Puzzle 1. There are five houses. operation 2. The Englishman lives in the red house. 3. The Spaniard owns the dog. 4. Coffee is drunk in the green house. unify(A,A). 5. The Ukrainian drinks tea. 6. The green house is immediately to the right of the ivory house. 7. The Old Gold smoker owns snails. Do these unify: 8. Kools are smoked in the yellow house. 9. Milk is drunk in the middle house. a a a b 10. The Norwegian lives in the first house. 11. The man who smokes Chesterfields lives in the house next to the man with the fox. a A a B 12. Kools are smoked in the house next to the house where the horse is kept. tree(l,r) A tree(l,r) tree(B,C) 13. The Lucky Strike smoker drinks orange juice. 14. The Japanese smokes Parliaments. tree(A,r) tree(l,C) tree(A,r) tree(A,B) 15. The Norwegian lives next to the blue house. Who drinks water? Who owns the zebra? A a(A) a a(A) 19 20
Model the situation Question What sort of a term is: Represent each house with the clause: house(Nationality,Pet,Smokes,Drinks,Colour) house(Nationality,Pet,Smokes,Drinks,Colour) a) number Represent the row of houses as follows: b) atom c) compound (H1,H2,H3,H4,H5) d) variable 21 22 Question Question What sort of a term is: What sort of a term is: Nationality (H1,H2,H3,H4,H5) a) number a) number b) atom b) atom c) compound c) compound d) variable d) variable 23 24
Define relevant facts More Facts exists(A,(A,_,_,_,_)). 6. The green house is immediately to the right of the ivory house. exists(A,(_,A,_,_,_)). exists(A,(_,_,A,_,_)). rightOf(A,B,(B,A,_,_,_)). exists(A,(_,_,_,A,_)). exists(A,(_,_,_,_,A)). rightOf(A,B,(_,B,A,_,_)). Read this as “exists(A,(A,_,_,_,_)) is true if rightOf(A,B,(_,_,B,A,_)). the details of house A unifies with the rightOf(A,B,(_,_,_,B,A)). 25 26 state of the first house” More Facts More facts 11. The man who smokes Chesterfields lives in the house next to the man with the fox. nextTo(A,B,(A,B,_,_,_)). 9. Milk is drunk in the middle house. nextTo(A,B,(_,A,B,_,_)). middleHouse(A,(_,_,A,_,_)). nextTo(A,B,(_,_,A,B,_)). nextTo(A,B,(_,_,_,A,B)). nextTo(A,B,(B,A,_,_,_)). 10. The Norwegian lives in the first house. nextTo(A,B,(_,B,A,_,_)). firstHouse(A,(A,_,_,_,_)). nextTo(A,B,(_,_,B,A,_)). 27 28 nextTo(A,B,(_,_,_,B,A)).
Recommend
More recommend