COMP313A Programming Languages
COMP313A Programming Languages
Lecture Outline
Horn Clauses Resolution Some Prolog
Prolog
grandparent (X,Y) :- parent(X, Z), parent(Z, Y).
Horn Clauses
A clause is either a single predicate called a fact or a rule of the form
condition conclusion
Horn Clauses
Horn clauses dont have any quantifiers
Assumes that the variables in the head are universally quantified and the variables in the body are existentially quantified (if they dont 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)
10
Resolution
Inference rule for Horn clauses Combine left and right hand sides of two horn clauses Cancel those statements which match on both sides
11
Resolution example
Example 1 Fact: Query:
mammal(human). mammal(human).
Resolution:
mammal(human).
mammal(human) mammal(human).
12
Resolution example
Example 1 Facts: Query: Resolution: see slide 10 legs(horse,4).
legs(horse,4).
legs(horse,4) legs(horse,4),
13
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 isnt killed by is food Bill eats peanuts and is still alive Sue eats everything Bill eats
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 ors. Drop the qualifiers to get the horn clauses
15
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).
18
19
Lecture Outline
20
Numbers
integers and floats
24
Structures
date(X,Y,Z). date(20, june, 2005). date(Day, june, 2005).
date(Day, Month, Year) :- Day > 0, Day <= 31,.
27
data objects
simple objects
structures
constants
variables
atoms
numbers
28
29
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]).
30
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
31
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
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.
33
Concatentation
34
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
35
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)
36
Unification in Prolog
A constant unifies only with itself
? me = me. Yes ?me = you. No Gcd(5, 0, 5) Gcd(5, 10, w) Gcd(5, 0, 5) Gcd(5, 0, w)
37
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).
38
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))
39
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))
40
41
Lecture Outline
Some Prolog
lists
Unification
42
Concatentation
conc([], L, L).
43
44
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]).
Delete
What if we want to delete every instance from the list
46
del (X, [], []). del (X, [X|Tail], Tail1) :- del (X, Tail, Tail1). del (X, [Y|Tail], [Y|Tail1]) :- del (X, Tail, Tail1).
47
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 []
49
[] is even A list is even if its tail is odd A list is odd if its tail is even
50
evenlength([]).
evenlength([First | Rest]) :- oddlength(Rest). oddlength([First | Rest]) :- evenlength(Rest).
51
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
52
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)
53
Unification in Prolog
A constant unifies only with itself
? me = me. Yes ?me = you. No Gcd(5, 0, 5) Gcd(5, 10, w) Gcd(5, 0, 5) Gcd(5, 0, w)
54
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).
55
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))
56
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))
57
58
Lecture Outline
59
Backtracking
PROGRAM big(bear). big(elephant). small(cat). brown(bear). black(cat). grey(elephant). dark(Z) :- black(Z). dark(Z) :- brown(Z). ?dark(X), big(X).
60
Controlling Backtracking
move(1,8). move(1,6). move(2,9). move(2,7). move(3,8). move(3,4). move(4,3). move(4,9). move(6,1). move(6,7). move(7,2). move(7,6). move(8,1). move(8,3). move(9,2). move(9,4).
1 4 7
2 5 8
3 6 9
61
Controlling Backtracking
Find all the two-move paths
path2(X,Y) :- move(X,Z), move(Z,Y).
path2(1,W).
?W=1 ?W=3 ?W=1 ?W=7
62
Controlling Backtracking
path2(X,Y) :- move(X,Z), !, move(Z,Y).
? path2(1, W). ?W=1 ?W=3
! is the cut operator
63
Controlling Backtracking
path2(X,Y) :- move(X,Z), move(Z,Y),!.
? path2(1, W).
64
65
Negation as Failure
Closed world assumption
66
Negation as Failure
Nonmonotonic reasoning more information sometimes means fewer things can be proved
human(bob). ?human(X). ?not human(X). ?not (not human(X)). X = bob no X= X why?
67
Control Information
Prolog uses a depth first search strateggy Therefore order is important
pred1(X,Z) :- parent(X,Z). pred1(X,Z) :- parent(X,Y), pred1(Y,Z). pred2(X,Z) :- pred2(Y,Z), parent(X,Y). pred2(X,Z) :- parent(X,Z).
68
parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? pred(tom, pat).
69
70
structures
71
I/O in Prolog
Files associated with input and output streams I/O from terminal user In some Prologs only two files/streams are active at any one time
current input stream current output stream
see switches input streams tell switches output streams In prolog I/O is a side effect of the predicate
72
Tkeclipse Prolog
stdin, stdout
standard input and output streams
?- write("fred"). Yes (0.00s cpu)
1+A=B+2
1+A=2+B
1 + 2 =:= 2 + 1 1 + A =:= B + 2
75
% any three child family family(_,person(Name, Surname, _, _), [_, _, _, | _]). % all married women who have at least three children
76
husband2(X,Y) :- family(person(X,Y,_,_), _, _). married(X,Y) :- family(person(X, _, _, _), person(Y,_, _, _),_). married(X,Y) :- married(Y,X).
78
but
?? child(pat) child(X) :- family(_, _, Children), mem_children (X, Children). mem_children (X, [person(X, _, _, _) | Tail]). mem_children (X, [person(Y, _, _, _) | Tail]) :- mem_children1(X, Tail).
79
Selectors
Define relation which allow us to access particular components of a structure without knowing the details the structure This is data abstraction These selectors are useful when we want to create instances of families, for example
Selectors
firstchild(Family, First) :- children(Family, [First | _]). secondchild(Family, Second) :- children(Family, [_, Second | _]).
82
Using them..
Tom Fox and Jim Fox belong to the same family and Jim is the second child of Tom
firstname(Person1, tom), surname(Person1, fox), % person1 is Tom Fox firstname(Person2, jim), surname(Person2, fox), %person2 is Jim Fox husband(Family, Person1), secondchild(Family, Person2). Person1 = person(tom, fox, _, _) Person2 = person(jim, fox, _, _) Family = family(person(tom, fox, _, _), _, [_, person(jim, fox, _,_) | _])
83
Logic Puzzles
Use the following clues to write a prolog program to determine the movies the robots tinman, r2d2, johnny five, and a dalek starred in.
Neither Tinman nor Johnny five were one of the daleks in Dr Who and the Daleks The movie Short Circuit did not star Tinman. R2d2 wowed the audiences in Star Wars. A dalek did not star in the Wizard of Oz.
84
Structure is important
Solution(L) :We want a binding for L which will contain the result we are after What is the result we want?
85
Now we have to supply a mechanism for instantiating X1..X4 We need a way of selecting a value and then checking it against some constraints
86
87
88
89
X1
\=
dalek,
solution(L):-
Robotslist = [tinman, dalek, r2d2, johnnyfive], member(X1, Robotslist), X1 \= dalek, member(X2, Robotslist), X2 \= tinman, X2 \= johnnyfive, member(X3, Robotslist), X3 = r2d2, member(X4, Robotslist), X4 \= tinman, X2 \= X1, X2 \= X3, X2 \= X4, X3 \= X1, X3 \= X4, X4 \= X1 print_movies(L).
91
92