Académique Documents
Professionnel Documents
Culture Documents
LOGIC PROGRAMMING
AND PROLOG
Reading:
Sebesta,
hapter 16
Referen
es:
Jumping Right In
parent(albert,edward).
parent(vi
toria,edward).
parent(albert,ali
e).
parent(vi
toria,ali
e).
?- male(vi
toria).
No
?- sibling(albert, vi
toria).
No
?- female(Person).
Person = ali
e;
Person = vi
toria;
No
?- sibling(edward, Sib).
Sib = edward;
Sib = ali
e;
Sib = edward;
Sib = ali
e;
No
?- parent(Person, edward).
Person = albert;
Person = vi
toria;
No
?- parent(Person, edward), female(Person).
Person = vi
toria;
No
3
Prolog vs S heme
Logi Programming
How does the language do it? Using uni ation, resolution, and ba ktra king.
Here are some of the argument values; tell me what the others have to
be in order to make a true statement.
5
?- tra
e.
Yes
[tra
e
df% ls
family.pl
Person = vi
toria
Yes
[debug
?-
?- parent(Person, edward).
Person = albert;
<----------- ";" to get more
Person = vi
toria;
No
?- parent(Person, edward).
Person = albert
Yes
?- notra
e.
Yes
df% pl
Wel
ome to SWI-Prolog (Multi-threaded, Version 5.2.11) ...
?- parent(Person, edward).
Person = albert;
Person = vi
toria;
No
?- [family.
[family loaded
Yes
?- halt.
df%
?- parent(Person, edward).
Person = albert;
Person = vi
toria;
No
--- edit family.P and remove parent(albert,edward). --?- ['family'.
% family
ompiled 0.00 se
, 5,200 bytes
Yes
?- parent(Person, edward).
Person = vi
toria;
No
?- halt.
df%
9
Prolog Queries
A query is a proposed fa
t that is to be proven.
<term>
11
Horn Clauses
(Rules)
A Horn Clause is:
1 ^ h2 ^ h3 ^ : : : ^ hn
h1 ^ : : : ^ hn
is written
:- h1, , hn.
Syntax elements:
`:-'
`,'
`.'
is true if 1,
true
2,
3,
. . . , and
hn
are all
12
13
n:
1 ^ a2 ^ a3 ^ ^ an ! :
Restri tions
I.e., a rule: albert is the father of edward if albert is male and albert is a parent of edward's.
15
Why Can't We
Disjoin Consequents?
Example:
1 _ a2 _ a3_ ! :
Solution:
Getting more than 1
onsequent,
onjoined
Example:
1 ^ a2 ^ a3 ! 1 ^ 2:
Solution:
Getter more than 1
onsequent, disjoined
Example:
1 ^ a2 ^ a3 ! 1 _ 2:
Solution:
16
17
Logi Review
and
(X1,. . . ,X ) :- h(X1,. . . ,X ).
n
18
19
Sample run
df% pl
Wel
ome to SWI-Prolog (Multi-threaded, Version 5.2.11)...
Example:
?- ['family'.
Logi :
parent X; Y
Warning: (./family.pl:50):
Singleton variables: [Y
% family
ompiled 0.00 se
, 5,528 bytes
?- isaMother(X).
X = vi
toria;
X = vi
toria;
No
The rule
; m n; k o
8X1 ; Xn
21
Database:
1)
2)
3)
4)
5)
male(tom).
male(peter).
male(doug).
female(susan).
male(david).
6)
7)
8)
9)
parent(doug, susan).
parent(tom, william).
parent(doug, david).
parent(doug, tom).
10)
Query:
| ?- grandfather(X,Y).
22
23
Tra e it in Prolog
Tra e it by hand
[tra
e ?- grandfather(X,Y).
Call: (7) grandfather(_G283, _G284) ?
reep
Call: (8) male(_G283) ?
reep
Exit: (8) male(tom) ?
reep
Call: (8) parent(tom, _L205) ?
reep
Exit: (8) parent(tom, william) ?
reep
Call: (8) parent(william, _G284) ?
reep
Fail: (8) parent(william, _G284) ?
reep
Redo: (8) male(_G283) ?
reep
Exit: (8) male(peter) ?
reep
Call: (8) parent(peter, _L205) ?
reep
Fail: (8) parent(peter, _L205) ?
reep
Redo: (8) male(_G283) ?
reep
Exit: (8) male(doug) ?
reep
Call: (8) parent(doug, _L205) ?
reep
Exit: (8) parent(doug, susan) ?
reep
Call: (8) parent(susan, _G284) ?
reep
Fail: (8) parent(susan, _G284) ?
reep
Redo: (8) parent(doug, _L205) ?
reep
Exit: (8) parent(doug, david) ?
reep
Call: (8) parent(david, _G284) ?
reep
Fail: (8) parent(david, _G284) ?
reep
Redo: (8) parent(doug, _L205) ?
reep
Exit: (8) parent(doug, tom) ?
reep
Call: (8) parent(tom, _G284) ?
reep
Exit: (8) parent(tom, william) ?
reep
Exit: (7) grandfather(doug, william) ?
reep
X = doug
Y = william
Yes
24
25
Example 2
Database:
1)
2)
3)
4)
5)
6)
7)
8)
9)
male(albert).
female(ali
e).
male(edward).
female(vi
toria).
parent(albert,edward).
parent(vi
toria,edward).
parent(albert,ali
e).
parent(vi
toria,ali
e).
sibling(X, Y) :- parent(P, X), parent(P, Y).
Query:
?- sibling(ali
e,Asib).
Asib = edward ;
Asib = ali
e ;
Asib = edward;
Asib = ali
e ;
No
?- sibling(Asib, ali
e).
Asib = edward ;
Asib = edward ;
Asib = ali
e ;
Asib = ali
e ;
No
26
27
Tra e it by hand
Tra e it in Prolog
[tra
e
Call:
Call:
Exit:
Call:
Exit:
Exit:
?- sibling(ali
e,Asib).
(7) sibling(ali
e, _G284) ?
reep
(8) parent(_L205, ali
e) ?
reep
(8) parent(albert, ali
e) ?
reep
(8) parent(albert, _G284) ?
reep
(8) parent(albert, edward) ?
reep
(7) sibling(ali
e, edward) ?
reep
Asib = edward ;
Redo: (8) parent(albert, _G284) ?
reep
Exit: (8) parent(albert, ali
e) ?
reep
Exit: (7) sibling(ali
e, ali
e) ?
reep
Asib = ali
e
Redo: (8)
Exit: (8)
Call: (8)
Exit: (8)
Exit: (7)
;
parent(_L205, ali
e) ?
reep
parent(vi
toria, ali
e) ?
reep
parent(vi
toria, _G284) ?
reep
parent(vi
toria, edward) ?
reep
sibling(ali
e, edward) ?
reep
Asib = edward ;
Redo: (8) parent(vi
toria, _G284) ?
reep
Exit: (8) parent(vi
toria, ali
e) ?
reep
Exit: (7) sibling(ali
e, ali
e) ?
reep
Asib = ali
e ;
No
28
29
Chaining
31
j ?-
ousin(X,jane). % a query
Rule and Goal Ordering:
There are two rules for
ousin
Whi
h rule do we try rst?
Ea
h rule for
ousin has several subgoals
Whi
h subgoal do we try rst?
32
Nondeterministi
rst
Results don't depend on rule and subgoal ordering
Prolog:
Deterministi
33