Loud12
-
Upload
arun-manick -
Category
Documents
-
view
217 -
download
0
Transcript of Loud12
-
7/29/2019 Loud12
1/92
1
COMP313A ProgrammingLanguages
Logic Programming (2)
-
7/29/2019 Loud12
2/92
2
Lecture Outline
Horn Clauses
Resolution Some Prolog
-
7/29/2019 Loud12
3/92
3
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).
-
7/29/2019 Loud12
4/92
4
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 YX is the sister of Y if
Z is the parent of both X and Y and
X is a female
-
7/29/2019 Loud12
5/92
5
Horn Clauses
A clause is either a single predicate called a fact
or
a rule of the formconditionconclusion
Conclusion is a single predicate
Condition is a conjunction of predicates
a1 a2 a3 an
parent(X,Z) parent(Z,Y) grandparent(X,Y)
-
7/29/2019 Loud12
6/92
6
Horn Clauses
Horn clauses dont have any quantifiers
Assumes that the variables in the head areuniversally quantified and the variables in the
body are existentially quantified (if they dont
appear in the head)
-
7/29/2019 Loud12
7/92
7
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)
-
7/29/2019 Loud12
8/92
8
Horn clauses continued
A fact
mammal(human) A query or goal
mammal(human)
-
7/29/2019 Loud12
9/92
9
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)
-
7/29/2019 Loud12
10/92
10
legs(x,4) mammal(x) legs(x,2).
legs(x,2) mammal(x) legs(x,4).
mammal(human).
mammal(horse).
legs(horse, 2).
-
7/29/2019 Loud12
11/92
11
Resolution
Inference rule for Horn clauses
Combine left and right hand sides of two horn
clauses Cancel those statements which match on
both sides
-
7/29/2019 Loud12
12/92
12
Resolution example
Example 1Fact: mammal(human).
Query: mammal(human).
Resolution:
mammal(human).
mammal(human) mammal(human).
-
7/29/2019 Loud12
13/92
13
Resolution example
Example 1Facts: 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).
-
7/29/2019 Loud12
14/92
14
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
Prove that John likes peanuts using backwardchaining
-
7/29/2019 Loud12
15/92
15
"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 ors. Drop the qualifiers to get the horn
clauses
-
7/29/2019 Loud12
16/92
16
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
-
7/29/2019 Loud12
17/92
17
Horn clause example
The Euclidian algorithm to compute the gcdof two positive integers, u and v:
The algorithm
The gcd ofu and 0is u
The gcd of u and v, ifvis not 0, is the same asthe gcd ofvand the remainder of dividing vinto u.
-
7/29/2019 Loud12
18/92
18
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 ingcd(15,10,X)
What value of X makes gcd(15,10,X) True
-
7/29/2019 Loud12
19/92
19
COMP313A ProgrammingLanguages
Logic Programming (3)
-
7/29/2019 Loud12
20/92
20
Lecture Outline
Some Prolog Unification
-
7/29/2019 Loud12
21/92
21
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).
-
7/29/2019 Loud12
22/92
22
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).
-
7/29/2019 Loud12
23/92
-
7/29/2019 Loud12
24/92
24
Data Objects
Atoms versus numbers
versus Variables Atoms
strings of letters, digits, and the underscore
characterbeginning 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
-
7/29/2019 Loud12
25/92
25
Data Objects
Atoms 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, _)
-
7/29/2019 Loud12
26/92
26
Data Objects
Structured objects objects that have several components
location(X, Y, Orientation).location(156, 786, 25).
location(156, 786, Orientation).
location is the functor
-
7/29/2019 Loud12
27/92
27
Structures
date(X,Y,Z).
date(20, june, 2005).
date(Day, june, 2005).
date(Day, Month, Year) :- Day > 0, Day
-
7/29/2019 Loud12
28/92
28
data objects
simple objects structures
constants variables
atoms numbers
These are all terms
-
7/29/2019 Loud12
29/92
29
Operations on lists
Membership
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]).
?
-
7/29/2019 Loud12
30/92
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.
member(X, [X|Tail]).
member(X, [Head | Tail]) :- member(X,Tail).
Note two separate clauses
-
7/29/2019 Loud12
31/92
31
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
-
7/29/2019 Loud12
32/92
32
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]
-
7/29/2019 Loud12
33/92
33
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.
-
7/29/2019 Loud12
34/92
34
Concatentation
conc([], L, L).
conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).
-
7/29/2019 Loud12
35/92
35
Unification
Matching clauses with variables
Have to find the appropriate substitutions forvariables so that the clauses match
Process is called unification
Process by which variables are instantiated
-
7/29/2019 Loud12
36/92
36
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)
-
7/29/2019 Loud12
37/92
37
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)
-
7/29/2019 Loud12
38/92
38
Unification in Prolog
A variable that is uninstantiated unifies with anythingand 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).
-
7/29/2019 Loud12
39/92
39
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))
-
7/29/2019 Loud12
40/92
40
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))
-
7/29/2019 Loud12
41/92
-
7/29/2019 Loud12
42/92
42
Lecture Outline
Some Prolog lists
Unification
-
7/29/2019 Loud12
43/92
43
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).
-
7/29/2019 Loud12
44/92
44
Adding and deleting
How do you add an item to a list?
Deleting an item from a list del(X, L, Result)
1. If X is the head of L result is the tail of L
2. 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 willapply.
-
7/29/2019 Loud12
45/92
45
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.
-
7/29/2019 Loud12
46/92
46
Delete
What if we want to delete every instance from
the list
-
7/29/2019 Loud12
47/92
47
del (X, [], []).
del (X, [X|Tail], Tail1) :- del (X, Tail, Tail1).
del (X, [Y|Tail], [Y|Tail1]) :- del (X, Tail, Tail1).
-
7/29/2019 Loud12
48/92
48
Relations/Terms/Queries
An 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.
-
7/29/2019 Loud12
49/92
49
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 []
-
7/29/2019 Loud12
50/92
50
[] is even
A list is even if its tail is odd
A list is odd if its tail is even
-
7/29/2019 Loud12
51/92
51
evenlength([]).
evenlength([First | Rest]) :- oddlength(Rest).
oddlength([First | Rest]) :- evenlength(Rest).
-
7/29/2019 Loud12
52/92
52
Unification
Matching clauses with variables
Have to find the appropriate substitutions forvariables so that the clauses match
Process is called unification
Process by which variables are instantiated
-
7/29/2019 Loud12
53/92
53
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)
-
7/29/2019 Loud12
54/92
54
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)
-
7/29/2019 Loud12
55/92
55
Unification in Prolog
A variable that is uninstantiated unifies with anythingand 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).
-
7/29/2019 Loud12
56/92
56
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))
-
7/29/2019 Loud12
57/92
57
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))
-
7/29/2019 Loud12
58/92
58
COMP313A ProgrammingLanguages
Logic Programming (5)
-
7/29/2019 Loud12
59/92
59
Lecture Outline
Cut operator
Negation as Failure
Control Information
-
7/29/2019 Loud12
60/92
60
Backtracking
PROGRAMbig(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 7dark(Z) :- brown(Z). %clause 8
?dark(X), big(X).
-
7/29/2019 Loud12
61/92
61
Controlling Backtracking
1 2 3
4 5 6
7 8 9
Knights Tour Problem
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).
-
7/29/2019 Loud12
62/92
62
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
-
7/29/2019 Loud12
63/92
63
Controlling Backtracking
path2(X,Y) :- move(X,Z), !, move(Z,Y).
? path2(1, W).
? W = 1
? W = 3
! is the cut operator
-
7/29/2019 Loud12
64/92
64
Controlling Backtracking
path2(X,Y) :- move(X,Z), move(Z,Y),!.
? path2(1, W).
-
7/29/2019 Loud12
65/92
65
Controlling Recursive Calls
predicate path determines if there is a path between two nodes.
path1(Z,Z,L).
path1(X,Z,L) :- move(X,Y), not (member(Y,L)), path(Y, Z, [Y|L]).
path2((Z,Z,L).
path2(X,Z,L) :- move(X,Y), not (member(Y,L)), path(Y, Z, [Y|L]), ! .
path1(1,3, []).
path2(1,3, []).
-
7/29/2019 Loud12
66/92
66
Negation as Failure
Closed world assumption
The goal not (X) succeeds whenever the goal X fails!
?mother(amy, bob).
?not(parent(amy, bob)).
Why did it fail?
-
7/29/2019 Loud12
67/92
67
Negation as Failure
Nonmonotonic reasoning more information
sometimes means fewer things can be
proved
human(bob).
?human(X). X = bob
?not human(X). no
?not (not human(X)). X = X
why?
-
7/29/2019 Loud12
68/92
68
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).
-
7/29/2019 Loud12
69/92
69
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).parent(bob, ann).
parent(bob, pat).
parent(pat, jim).
? pred(tom, pat).
-
7/29/2019 Loud12
70/92
70
COMP313A ProgrammingLanguages
Logic Programming (6)
-
7/29/2019 Loud12
71/92
71
Some More Prolog
structures
-
7/29/2019 Loud12
72/92
Tk li P l
-
7/29/2019 Loud12
73/92
73
Tkeclipse Prolog
stdin, stdout
standard input and output streams
?- write("fred").
Yes (0.00s cpu)
open a stream for input or output open(SourceSink, Mode, Stream)
? open('//C/Documents and Settings/mjeff/MyDocuments/prolog/fred.txt', read, Fred),read_string(Fred, "\r\n ", 10, X).
Fred = 31
X = "freddy"
Yes (0.00s cpu)
-
7/29/2019 Loud12
74/92
-
7/29/2019 Loud12
75/92
75
More bits and pieces
Comparison operators
X > Y
X < Y
X >= Y
X =< Y
X = Y 1 + A = B + 2 1 + A = 2 + B
X \= Y X =:= Y 1 + 2 =:= 2 + 1 1 + A =:= B + 2
X =\= Y
Structures
-
7/29/2019 Loud12
76/92
76
a family database
family(person(tom, fox, date(7,may, 1950), employed),person(ann, armstrong, date(9, may, 1951), employed),
[person(pat, fox, date(5, may, 1973, unemployed),
person(jim, fox, date(5, may, 1973), unemployed)]).
family(_,_,_). % any familyfamily(person(_, fox, _, _), _, _). % the fox
family
family(_, person(_,armstrong, _, _), _) % or the armstrong family
family(_, _, [_, _, _]). % any three childfamily
family(_,person(Name, Surname, _, _), [_, _, _, | _]).
% all married women
who have at
least three children
husband(X) :- family(X, , ).
-
7/29/2019 Loud12
77/92
77
husband(X) : family(X, _, _).
wife(X) :- f amily(_,X,_).
?- husband(X).
X = person(tom, fox, date(7, may, 1950), employed)
Yes (0.00s cpu)
?- wife(X).X = person(ann, armstrong, date(9, may, 1951), employed)
Yes (0.00s cpu)
Problem this may be too simplistic
-
7/29/2019 Loud12
78/92
78
husband2(X,Y) :- family(person(X,Y,_,_), _, _).
married(X,Y) :- family(person(X, _, _, _), person(Y,_, _, _),_).
married(X,Y) :- married(Y,X).
child(X) :- family(_, _, Children), member(X, Children).
?? child(X)
??
-
7/29/2019 Loud12
79/92
-
7/29/2019 Loud12
80/92
80
Selectors
husband(family(Husband, _, _), Husband).wife(family(_, Wife, _), Wife).
children(family(_, _, Childlist), Childlist).
Define relation which allow us to access particularcomponents 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
-
7/29/2019 Loud12
81/92
81
Selectors
firstchild(Family, First) :- children(Family, [First | _]).
secondchild(Family, Second) :- children(Family, [_, Second | _]).
nthchild(N, Family, Child) :- children(Family, ChildList),
nth_member(ChildList, N, Child).
firstname(person(Name, _, _,_), Name).
surname(person(_, Surname, _, _), Surname).
-
7/29/2019 Loud12
82/92
82
nth_member([X|_], 1, X).
nth_member([_| L], N, Child) :- N1 is N-1, nth_member(L, N1, Child).
-
7/29/2019 Loud12
83/92
-
7/29/2019 Loud12
84/92
84
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.
-
7/29/2019 Loud12
85/92
85
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?
-
7/29/2019 Loud12
86/92
86
L = [movie(X1, wizardofoz),
movie(X2, drwhoandtheDaleks),
movie(X3, starwars),
movie(X4, shortcircuit)],
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
-
7/29/2019 Loud12
87/92
87
L = [movie(X1, wizardofoz),
movie(X2, drwhoandtheDaleks),
movie(X3, starwars),
movie(X4, shortcircuit)],
-
7/29/2019 Loud12
88/92
88
L = [movie(X1, wizardofoz),
movie(X2, drwhoandtheDaleks),
movie(X3, starwars),
movie(X4, shortcircuit)],
Robotslist = [tinman, dalek, r2d2, johnnyfive],
We will draw the values for X1..X2, from Robotslist
We do this using the member predicate
-
7/29/2019 Loud12
89/92
-
7/29/2019 Loud12
90/92
90
L = [movie(X1, wizardofoz),
movie(X2, drwhoandtheDaleks),
movie(X3, starwars),
movie(X4, shortcircuit)],
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,
dalek,
There are just two more things left to do
L = [movie(X1, wizardofoz),solution(L):-
-
7/29/2019 Loud12
91/92
91
movie(X2, drwhoandtheDaleks),
movie(X3, starwars),
movie(X4, shortcircuit)],
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 \= X1print_movies(L).
-
7/29/2019 Loud12
92/92