Prolog Lecture 1: David Eyers Michaelmas 2008
Prolog Lecture 1: David Eyers Michaelmas 2008
Course aims
Introduce a declarative style of programming
Explain fundamental elements of Prolog: terms, clauses, lists, arithmetic, cuts, backtracking, negation
Demonstrate Prolog problem-solving techniques By the end of the course you should be able to:
Write and understand Prolog programs Use difference structures Understand basic constraint programming principles
2
Assessment
One exam question in Paper 3 Assessed Exercise (a tick)
you must get a tick for either Prolog or C & C++ tick exercises and submission done in Lent term more information to follow closer to the time
Supervision work
Some example questions are provided at the end of the lecture handout
Note: Prolog examples are often easy to follow ... Make sure you can write your own programs too!
I will give some pointers and outline solutions during the lectures
Recommended text
PROLOG Programming for Artificial Intelligence, Ivan Bratko, Addison Wesley (3rd edition, 2000) Provides an alternative angle on the basics Examines problem solving with Prolog in detail Not all of the textbook is directly relevant
Lecture 1
Logic programming and declarative programs Introduction to Prolog Basic operation of the Prolog interpreter Prolog syntax: Terms Unification of terms Solving a logic puzzle
6
Imperative programming
Formulate a how to compute it recipe, e.g.:
to compute the sum of the list, iterate through the list adding each value to an accumulator variable
int sum(int[] list ) { int result = 0; for(int i=0; i<list.length; ++i) { result += list[i]; } return result; }
7
Functional programming
Again formulate a how to compute it recipe
(* 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 tail. *) fun sum([]) = 0 | sum(h::t) = h + sum(t);
8
Logic programming
% the sum of the empty list is zero sum([],0). % 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.
Not how to compute the result Instead this is true about the result
9
Facts + Rules
Questions
Answers
10
11
Reboot a PWF terminal into Linux Log into linux.pwf.cam.ac.uk Log into the SRCF Linux users: use your package manager
14
Press enter to accept a query answer and return to the top level shell Type a semi-colon (;) to request the next answer Type w to display fully a long result that Prolog has abbreviated
18
Terms are the building blocks with which Prolog represents data
Constants Numbers: 1 -2 3.14 Atoms: tigger '100 Acre Wood' Variables X A_variable _ Compound terms likes(pooh_bear,honey) plus(4,mult(3,plus(1,9)))
19
e.g. X-Y, 2+3, etc. Any infix expression has an equivalent prefix form Ensure that you are comfortable with this equivalence
20
You can ask Prolog to display any term using prefix notation alone
Infix notation is just for human convenience
Does not affect Prolog's internal data structures Requires operator precedence to be defined
The query write_canonical(Term) will display Term without using any infix operators
Potentially useful for your Prolog experimentation We will gloss over how this actually works for now
21
1 3 5 7 9
a a tree(l,r) tree(A,r) A
a A A tree(l,C) a(A)
2 4 6 8 10
a a tree(l,r) tree(A,r) a
Note: _ is a special variable that unifies with anything. Each _ in an expression unifies independently
22
Zebra Puzzle
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. There are five houses. The Englishman lives in the red house. The Spaniard owns the dog. Coffee is drunk in the green house. The Ukrainian drinks tea. The green house is immediately to the right of the ivory house. The Old Gold smoker owns snails. Kools are smoked in the yellow house. Milk is drunk in the middle house. The Norwegian lives in the first house. The man who smokes Chesterfields lives in the house next to the man with the fox. Kools are smoked in the house next to the house where the horse is kept. The Lucky Strike smoker drinks orange juice. The Japanese smokes Parliaments. The Norwegian lives next to the blue house.
24
Question
What sort of a term is: house(Nationality,Pet,Smokes,Drinks,Colour) a) number b) atom c) compound d) variable
25
Question
What sort of a term is: Nationality a) number b) atom c) compound d) variable
26
Question
What sort of a term is: (H1,H2,H3,H4,H5) a) number b) atom c) compound d) variable
27
The Englishman lives in the red house. That is: house(british,_,_,_,red) Simplify: let's say it unifies with the first house. The houses 5-tuple would then unify with term:
(house(british,_,_,_,red),_,_,_,_)
The comma requires both parts of the query to hold Prolog will progressively bind variables via unification
However, we're usually specifically interested in what the variables actually get bound to!
i.e. there exists a house that has a certain property We discussed firstHouse(A,(A,_,_,_,_)). The generalisation to at least one house is:
6.
middleHouse(A,(_,_,A,_,_)).
10.
firstHouse(A,(A,_,_,_,_)).
33
(that's a colon followed directly by a hyphen) Prolog executes those queries when the file is loaded We'll have more to say on this later...
Zebra Puzzle
> prolog ?- [zebra].
We use print(WaterDrinker), print(ZebraOwner) in our query to produce this output
norwegian japanese
?- halt.
39
End
Next lecture: