920 likes | 1.37k Views
COMP313A Programming Languages. Logic Programming (2). Lecture Outline. Horn Clauses Resolution Some Prolog. A little bit of Prolog. Objects and relations between objects Facts and rules parent(pam, bob). parent(tom,bob). parent(tom, liz). parent(bob, ann).
E N D
COMP313A Programming Languages Logic Programming (2)
Lecture Outline • Horn Clauses • Resolution • Some Prolog
A little bit of Prolog • Objects and relations between objects • Facts and rules parent(pam, bob). parent(tom,bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? parent(bob, pat). ? parent(bob, liz). ? parent(bob, ben). ? parent(bob, X). ? parent(X, Y).
Prolog grandparent (X,Y) :- parent(X, Z), parent(Z, Y). For all X and Y X is the grandparent of Y if X is a parent of Z and Z is a parent of Y sister (X,Y) :- parent(Z, X), parent(Z, Y), female(X) For all X and Y X is the sister of Y if Z is the parent of both X and Y and X is a female
Horn Clauses • A clause is either a single predicate called a fact or • a rule of the form conditionÞ conclusion • Conclusion is a single predicate • Condition is a conjunction of predicates a1Ùa2Ù a3 … Ù an parent(X,Z) Ù parent(Z,Y)Þ grandparent(X,Y)
Horn Clauses • Horn clauses don’t have any quantifiers • Assumes that the variables in the head are universally quantified and the variables in the body are existentially quantified (if they don’t appear in the head)
Horn Clauses grandparent(X,Y)Ü parent(X,Z) Ù parent(Z,Y) grandparent(X,Y):- parent(X,Z),parent(Z,Y) Equivalent to "x : "y : $z parent(X,Z) Ù parent(Z,Y) Þ grandparent(X,Y)
Horn clauses continued • A fact mammal(human) Ü • A query or goalÜ mammal(human)
Horn clause example " x mammal(x) Þlegs(x,2) Úlegs(x,4) mammal(x) ÙØ legs(x,2) Þ legs(x,4) mammal(x) Ù Ølegs(x, 4) Þ legs(x, 2) Prolog legs(x,4) :- mammal(x), not legs(x,2) legs(x,2) :- mammal(x), not legs(x,4)
legs(x,4) Ü mammal(x) ÙØ legs(x,2). legs(x,2) Ü mammal(x) ÙØ legs(x,4). mammal(human). mammal(horse). Ø legs(horse, 2).
Resolution • Inference rule for Horn clauses • Combine left and right hand sides of two horn clauses • Cancel those statements which match on both sides
Resolution example Example 1 Fact: mammal(human). Query: Ü mammal(human). Resolution: Ü mammal(human). mammal(human) Ü mammal(human). Ü
Resolution example Example 1 Facts: see slide 10 Query: Ü legs(horse,4). Resolution: Ü legs(horse,4). legs(horse,4) Ü legs(horse,4), mammal(horse), Ø legs(horse, 2). Ü mammal(horse), Ø legs(horse, 2). …..
Turn the following sentences into formulae in first order predicate logic • John likes all kinds of food • Apples are food • Chicken is food • Anything anyone eats and isn’t killed by is food • Bill eats peanuts and is still alive • Sue eats everything Bill eats • Prove that John likes peanuts using backward chaining
"x : food(x) Þ likes(John,x) • food(Apples) • food(Chicken) • "x :"x :eats(x,y) ÙØ killed_by(x,y) Þfood(x) • eats(Bill, Peanuts) • alive (Bill) • x : eats(Bill, x) Þ eats(Sue,x) We have no “or”s. Drop the qualifiers to get the horn clauses
1. likes(John,x) Ü food(x). 2. food(Apples). 3. food(Chicken). 4. food(x) Ü eats(x,y) ÙØ killed_by(x,y). 5. eats(Bill, Peanuts). 6. alive (Bill). 7. eats(Sue,x) Ü eats(Bill, x). 8. alive(x,y) Ü Ø killed_by(x,y). 9. Ø killed_by(x,y) Ü alive(x,y). Proves that John likes Peanuts using resolution
Horn clause example • The Euclidian algorithm to compute the gcd of two positive integers, u and v: The algorithm The gcd of u and 0 is u The gcd of u and v, if v is not 0, is the same as the gcd of v and the remainder of dividing v into u.
Horn clause example greatest common divisor "u gcd(u, 0, u) " u, " v, " w, Ø zero(v) Ùgcd(v, u mod v, w) Þgcd(u,v,w) gcd(u,0,u) gcd(u,v,w) ÜØzero(v) Ùgcd(v, u mod v, w) What is the greatest common denominator of 15, and 10. i.e. find and instantiation for X in gcd(15,10,X) What value of X makes gcd(15,10,X) True
COMP313A Programming Languages Logic Programming (3)
Lecture Outline • Some Prolog • Unification
Some more prolog • Recursive rules example predecessor relation predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), parent(Y,Z) And so on… predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z).
Some more prolog parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? predecessor(pam,bob). ? predecessor(pam, ann). ? predecessor(pam, liz). ? predecessor(pam, X).
A prolog program comprises clauses - facts, rules and queries PROGRAM big(bear). %clause 1 big(elephant). %clause 2 small(cat). %clause 3 brown(bear). %clause 4 black(cat). %clause 5 grey(elephant). %clause 6 dark(Z) :- black(Z). %clause 7 dark(Z) :- brown(Z). %clause 8 ?dark(X), big(X).
Data ObjectsAtoms versus numbers versus Variables • Atoms • strings of letters, digits, and the underscore character beginning with a lower case letter • some strings of special characters • can enclose strings of characters in single quotes e.g. ‘Fred’ • Numbers • integers and floats
Data ObjectsAtoms versus Variables • Variables are strings of characters, digits and underscores that begin with an uppercase letter or an underscore • Can use the anonymous underscore hasachild(X) :- parent(X,Y) hasachild(X) :- parent(X,_) ? parent(X, _)
Data ObjectsStructured objects • objects that have several components location(X, Y, Orientation). location(156, 786, 25). location(156, 786, Orientation). location is the functor
Structures date(X,Y,Z). date(20, june, 2005). date(Day, june, 2005). date(Day, Month, Year) :- Day > 0, Day <= 31,…….
data objects structures simple objects constants variables atoms numbers These are all terms
Operations on listsMembership • The member relation member(X,L) ? member(d, [a, b, h, d]). ? ? member(d, [a,b,[d h], e]). ? ?member([d, h], [a, [d,h], e f]). ?
Membership • X is a member of L if • X is the head of L, or • X is a member of the tail of L. member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). Note two separate clauses
Membership • X is a member of L if • X is the head of L, or • X is a member of the tail of L, or • X is a member of a sublist of L member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). plus one more clause……
Concatenation • The concatenation relation – conc(L1, L2, L3) ? conc([a,b], [c,d], [a,b,c,d]) yes ? conc([a,b], [c,d], [a, b, [c,d]]) no ?conc([a,b], [c,d], X). X = [a,b,c,d]
Concatentation • conc(L1, L2, Result) • If L1 is the empty list • then L2 and Result must be equal • If L1 is nonempty • then have [X|L1tail] • recursively X becomes the head of Result and we use conc to find the tail of result [X|TailResult] • Eventually will have exhausted L1 and TailResult will be L2.
Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).
Unification • Matching clauses with variables • Have to find the appropriate substitutions for variables so that the clauses match • Process is called unification • Process by which variables are instantiated
GCD example gcd(u,0,u) gcd(u,v,w) Ü not zero(v), gcd(v, u mod v, w) Using resolution the goal Ü gcd(15, 10, x)
Unification in Prolog • A constant unifies only with itself ? me = me. Yes ?me = you. No Gcd(5, 0, 5) Ü Gcd(5, 0, 5) Gcd(5, 10, w) Ü Gcd(5, 0, w)
Unification in Prolog… • A variable that is uninstantiated unifies with anything and becomes instantiated with that thing ? me = X. X = me ? f(a,X) = f(Y, b). X = b Y = a ? f(X) = f(Y) gcd(u,v,w) Ü not zero(v), gcd(v, u mod v, w), gcd(15, 10, x). gcd(15, 10, x) Ü not zero(10), gcd(10, 15 mod 10, x), gcd(15, 10, x).
Unification in Prolog • A structured term (predicate or function applied to arguments requires • Same predicate/function name • Same number of arguments • Arguments can be unified recursively ? f(X) = g(X) ? f(X) = f(a,b) ? f(a, g(X)) = f(Y, b) ? f(a, g(X)) = f(Y, g(b))
Unification examples • Unify the following : p(X,Y) and p(a, Z) p(X,X) and p(a,b) ancestor(X,Y) and ancestor(bill, W) p(X,a,Y) and p(Z,Z,b) p(Marcus, g(X,Y)) and f(x, g(Caesar, Marcus)) g(X, X) and g(f(X), f(X))
COMP313A Programming Languages Logic Programming (4)
Lecture Outline • Some Prolog • lists • Unification
Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3). Can use concat to decompose lists How? Can use concat to define the member predicate - member2(X, L) :- conc(L1, [X|L2], L).
Adding and deleting • How do you add an item to a list? • Deleting an item from a list – del(X, L, Result) • If X is the head of L – result is the tail of L • If X is contained in the tail of L then recursively split L into its head and tail until X is at the head. Then 1 will apply.
Deleting… del(X, [X|Tail], Tail]). del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1) Deletes one occurrence from the list What happens when: del(a, [a, b, a, a], X). What if we changed it to del(X, [X|Tail], Tail]). del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1), X=\=Y.
Delete • What if we want to delete every instance from the list
del (X, [], []). del (X, [X|Tail], Tail1) :- del (X, Tail, Tail1). del (X, [Y|Tail], [Y|Tail1]) :- del (X, Tail, Tail1).
Relations/Terms/QueriesAn important note • You can define different relations with the same name but with different numbers of arguments • e.g. member/1, member/2, member/3 • If you leave off an argument prolog thinks it is a different relation • If it is undefined for that number of arguments you will get an error message • And if there is such a relation predefined…….
Define two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively. For example ? evenlength([a,b,c,d]) ? yes ?oddlength([c, d, e]) ?yes The trick is to define them as mutually recursive clauses. Start with []
[] is even A list is even if its tail is odd A list is odd if its tail is even