Professional Documents
Culture Documents
Prolog: MSC Computing Fariba Sadri
Prolog: MSC Computing Fariba Sadri
Prolog: MSC Computing Fariba Sadri
Prolog
Prolog is a high level declarative programming language based on a subset of predicate logic. It is a logic programming language.
Prolog 1
MSc Computing Fariba Sadri
With thanks to Keith Clark for the use of some of his lecture material
Particularly favoured for applications in AI expert system and computational linguistics. It was developed in the early 1970s through the theoretical studies of Professor Robert Kowalski at Imperial College and Edinburgh Alain Colmerauer in Marseille, France, and the first compiler was written by David H.D. Warren in Edinburgh, Scotland.
Introduction to Prolog
Introduction to Prolog
Example In logic
We will be using Sicstus Prolog and Windows. Program files are saved as plain text.
S (pass_exams(S) pass_cwks(S) pass_projs(S) pass_msc(S)) pass_exams(john) pass_cwks(john) pass_projs(john) L |- pass_msc(john). But it is not the case that L |- pass_msc(mary).
Introduction to Prolog 3 Introduction to Prolog 4
11/5/2009
Example in Prolog
S (pass_msc(S) pass_exams(S) pass_cwks(S) pass_projs(S) pass_exams(john) pass_cwks(john) pass_projs(john)
pass_msc(S) :- pass_exams(S), pass_cwks(S), pass_projs(S). pass_exams(john). pass_cwks(john). pass_projs(john). Query to P Answer Query to P Answer Query to P Answer
Introduction to Prolog 5
: : : : : :
Prolog syntax
Prolog program is a sequence of clauses. A clause has the form: H :- C1,...,Ck. conditional clause or H. unconditional clause A terminating .<space>, .<newline> or .<tab> is essential after each clause.
H :- C1,...,Ck.
Must be NO space between p and the ( p is the predicate or relation name of the atomic formula. t1,...tn are terms. Clause is about the predicate of H. Each Ci is sometimes referred to as a call or condition. Later we will see that we can have more complex conditions.
7 Introduction to Prolog 8
Introduction to Prolog
11/5/2009
Logical reading
A conditional clause H :- C1,...,Ck. is read as: X1 . . . Xm(C1 . . . Ck H) where the Xi are all the variables that occur in the clause, or equivalently: X1 . . . Xi (Xi+1,.. ,Xm(C1 . . Ck) H) where Xi+1,..,Xk are variables that only appear in the conditions of the clause. An unconditional clause H is read as: X1 . . . Xm(H) where the Xi are all the variables that occur in H.
Introduction to Prolog 9
Prolog terms
constants - usually alphanumeric sequence of one or more symbols beginning with a lower case letter e.g. bill, maryJones, elephant67 numbers - usual syntax e.g. 3, -6, 34.89 variable names - alphanumeric sequence of one or more symbols beginning with an upper case letter or _ e.g. X, Apple, _456, _ compound terms - a function name (same syntax as constant) applied to n terms of the form f(t1,..,tn), e.g. fun23(3,apple,98) Predicate names have same syntax as constants. There are three other kinds of terms - quoted constants, strings and lists (we will come to these later).
Introduction to Prolog 10
Prolog queries
A query is a conjunction of conditions, i.e. ?- C1, . . . , Cn .<newline> Each Ci is a condition/call (as in a clause). ?- is a prompt displayed by Prolog. Terminating .<newline> is needed.
Introduction to Prolog
12
11/5/2009
Exercise
c -----------------------------------------p(X) {X=john} p(john) q(X,Y) {X=1, Y=2} q(X,Y) {X=1, Y=f(Z)} q(X, f(X)) {X=g(5)}
C
Introduction to Prolog 14
Example query
?- drinks(X) i.e. "Is there someone, X, who drinks?" or "Who drinks? It is a request for an answer ={X=name} such that drinks(X) i.e. drinks(name) follows from the program clauses or for confirmation that there is no such (no such name).
Introduction to Prolog 15
11/5/2009
Anonymous Variables
Variables that appear only once in a rule, can be anonymous, i.e. do not have to be named. You can use _ (underscore) to denote such variables. needs(_,cameras). happy(fs) :- likes(_, logic). But be careful! Two or more "_" in the same rule represent different variables. really_happy(fs) :- likes(_, logic), likes(_, prolog). is understood as really_happy(fs) :- likes(X, logic), likes(Y, prolog).
Introduction to Prolog
17
Write Prolog Queries for the following: 1. 2. 3. 4. 5. Does Britain sell oil to the USA? Who sells grain to who? Who sells oil to Britain? Who sells what to Hungary? Who sells something to Hungary?
Introduction to Prolog
20
11/5/2009
Scope of identifiers
The scope of a variable is just the clause or query in which it occurs. The scope of any other name (constant, function name, predicate name) is the whole program and any query.
Introduction to Prolog
22
Exercise
1. Define colleague/2, such that colleague(W1,W2) holds if W1,W2 are different workers that work in the same department. 2. Add a new clause for managerOf(W,M) to express that M is the manager of W if M is the manager of the department in which W works.
23 Introduction to Prolog 24
Introduction to Prolog
11/5/2009
Recursion
superiorOf(E,S) :superiorOf(E,S):managerOf(E,S). managerOf(E,M), superiorOf(M,S).
superiorOf/2 is a recursive predicate. The first rule for superiorOf/2 is a base case. The second rule for superiorOf/2 is a recursive rule. With earlier facts and rules we get: ?-superiorOf(bill,paul). Yes What are the answers to ?-superiorOf(X,Y). ?
Introduction to Prolog
henry sally
Introduction to Prolog
26
Introduction to Prolog
27
Introduction to Prolog
28
11/5/2009
More on syntax
Constants, function symbols and predicate symbols can also be any sequence of characters in single quotes, e.g. fs@doc.ic.ac.uk Sam bill green *****
Introduction to Prolog 29
inelligible_to_vote(X) :-under_age(X) ; mentally_ill(X). The Prolog rule p:-c1;c2. has the same meaning as the two rules p:-c1. p:-c2. Exercise: Prove in logic that pc1 c2 (p c1) (p c2).
Introduction to Prolog 30
11/5/2009
q(1). r(1,1). r(2,2). r(3,3). p(X,Y) :- q(X), r(X,Y). ?- p(W,Z). p(X,Y) :- q(X), r(X,Y). q(W), r(W,Z). {X=W, Y=Z} r(1,Z) {W=1} Succeeds {W=1, Z=1}.
Introduction to Prolog 35
q(1). r(1,1). r(2,2). r(3,3). p(X,Y) :- r(X,Y), q(X). ?- p(W,Z). r(W,Z), q(W) q(1) q(2) q(3) .. succeeds fails fails {W=1,Z=1}
Introduction to Prolog 36
11/5/2009
Unification
There is an algorithm for unifying two atomic formulas and producing the unifying substitution. h(X,a,Y) and h(b,Z,W) unify with: ={X=b, Z=a, Y=W} (or W=Y) both become: h(b,a,W)
p(X,Y,h(Y)) p(Z,a,Z) unify with: ={X=h(a), Y=a, Z=h(a)} both become: p(h(a),a,h(a)) Find out more about unification by entering queries using the =/2 unification primitive. Try the query: ?- p(X,Y,h(Y)) = p(Z,a,Z).
Introduction to Prolog
10
11/5/2009
Successful Evaluation
Repeat reduction step until {X1=t1, X2=t2, .. ,Xk=tk} is generated This is an answer to the query. To find more answers, backtrack to most recent preceding query for which there is an unused applicable clause. Continue by reducing that query using its next untried applicable clause.
Introduction to Prolog 41
Example evaluation
Program: sells(usa, grain, mexico). sells(S, P, R) :- produces(S, P), needs(R, P). produces(japan, cameras). . needs(_,cameras). Query : 1:{E=E,I=I}-sells(E,cameras,I) 1st (and only) applicable clause is: sells(S,P,R):- produces(S,P),needs(R,P) 1={S=E, P=cameras, R=I} sells(E,cameras,I)1 sells(S,P,R)1 are both: sells(E,cameras,I).
Introduction to Prolog
42
Introduction to Prolog
44
11
11/5/2009
Introduction to Prolog
46
1: {X=X}-ancestor(bill,X) using first ancestor/2 clause, ={C=bill,A=X} 2: {X=X}-parent(bill,X) backtrack query 1 using 1st parent/2 fact (only applicable clause) = {X=mary} 3: {X=mary} 1st solution backtrack query 1 retry 1, using second (and last) ancestor/2 clause, = {C=bill,A=X} 4: {X=X}-parent(bill,P),ancestor(P,X) no backtrack query using 1st parent/2 fact (only applicable clause) = {P=mary} 5: {X=X}-ancestor(mary,X) no backtrack query using 1st ancestor/2 clause, = {C=mary,A=X} 6: {X=X}-parent(mary,X) backtrack query 5 using 2nd parent/2 fact (only applicable clause) = {X=jane} 7: {X=jane} 2nd solution backtrack query 5 ..
Introduction to Prolog
47
12