prolog1 33 2up.ps
Post on 07-Apr-2018
218 Views
Preview:
TRANSCRIPT
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 1/17
Logic Programming and Prolog
LOGIC PROGRAMMING
AND PROLOG
Logic programming languages are not proce-
dural or functional.
• Specify relations between objects
- larger(3,2)
- father(tom,jane)
Reading:
• Sebesta, chapter 16
• Separate logic from control:
- Programmer declares what facts and relations
are true.
- System determines how to use facts to solve
problems.
- System instantiates variables in order to make
relations true!
References:
• Clocksin and Mellish, Ch. 1-4, 6, 8
• Online Resources (tutorials, SWI page, etc.) • Computation engine: theorem-proving and recur-
sion (Unification, Resolution, Backward Chaining,
Backtracking)
- Higher-level than imperative languagesome material ©Diane Horton 2000, and
©Suzanne Stevenson 200l.
Modified by Sheila McIlraith 2004.
1 2
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 3/17
Prolog vs Scheme Logic Programming
In Scheme, we program with functions (" pro-
cedures" ) .
• A function's arguments are different from
the function's value.
• Give a single Scheme function, we can only
ask one kind of question:
• A program consists of facts and rules.
• Running a program means asking queries.
• The language tries to find one way
(or more) to prove that the query is true.
Here are the argument values; tell me
what is the function's value.
• This may have the side effect of freezing
variable values.
• The language determines how to do all of
th is, not the prog ram.
In Prolog, we program with relations.
• There is no bias; all arguments are the
same.
• Given a single Prolog predicate, we can ask
many kinds of question:
• How does the language do it? Using unifi-
cation, resolution, and backtracking.
Here are some of the argument val-
ues; tell me what the others have to
be in order to make a true statement.
5 6
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 5/17
?- notrace.
Ye s
cdf% pI
W elc ome to S WI- Pro log (M ult i- thre aded , Ve rsi on 5.2 .11 )
?- parent(Person, edward). For help, use ?- h elp (To pic) . or ?- apropos(Word).
Person
Person
No
albert;
victoria;
?- [family].
[ fa mi ly l oa de d]
Ye s
?- p ar en t( Pe rs on , e dw ar d) .
?- halt.
cdf% Person
Person
No
albert;
victoria;
edit family.P and remove parent(albert,edward). - --
?- ['family'].
% family compiled 0.00 sec, 5,200 bytes
Ye s
?- p ar en t( Pe rs on , e dw ar d) .
Person = victoria;
No
?- halt.
cdf%
9
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 6/17
Some Prolog Syntax
Lexica I Rules:
• Variables are capitalized.
• Constants begin with a lower case letter.
• Predicate names begin with a lower case
letter.
Prolog Queries
A query is a proposed fact that is to be proven.
Simplified Grammar:• Ifthe query has no variables, returns yes/no.
<clause> ::= <pred>
<pred> :- <pred> { <pred>}. • If the query has variables, returns appro-
priate values of variables (called a substi-
tution).pred> ::= <pname>'(' <term> { , <term> } C ) '
<term> ::= <int> I <atom> I <var>
Note: No blank between predicate name and
opening bracket.
10 11
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 7/17
Horn Clauses
(Rules)
Horn Clause Terminology
• Horn Clause = Clause
A Horn Clause is: C f-hl 1\ h2 1\ h3 1\ ... 1\ hn • Consequent = Goal = Head
• Antecedents = Subgoals = Tail
• Antecedents: conjunction of zero or more
conditions which are atomic formulae in
predicate logic
• Horn Clause with No Tail = Fact
• Horn Clause with Tail = Rule
• Consequent: an atomic formula in predi-
cate logic
In Prolog, a Horn clause
is written
Meaning of a Horn clause:
Syntax elements:r , ,
• "The consequent is true if the antecedents
are all true"
• c is true if hl. ba , h3..... and hn are all
true
12 13
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 8/17
Prolog Horn Clause Examples Meaning of Prolog Rules
Without VariablesA Horn clause with no tail:
A prolog rule must have this form:
male(albert) .
I.e., a fact: albert is a male dependent on no
other conditions
which means in logic:
A Horn clause with a tail:
father( albert,edward):-
male(albert), parent(albert,edward). Restrictions
I.e., a rule: albert is the father of edward if al-
bert is male and albert is a parent of edward's.
• There can be zero or more antecedents,
but they are conjoined; we cannot disjoin
them .
• There cannot be more than 1 consequent.
14 15
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 9/17
Bending the Restrictions Why Can't We
Disjoin Consequents?
Getting disjoined antecedents
Why did the designers of Prolog disallow this?
Solution:
Getting more than 1 consequent, conjoined
Solution:
Getter more than 1 consequent, disjoined
Solution:
16 17
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 10/17
Logic Review Horn Clauses with Variables
Variables may appear in the antecedents and
consequent of a Horn clause:
"For all values of Xl, ... ,Xn, the formula
C(Xl, ... ,Xn) is true if the formula h(Xl, ... ,Xn)
is true"
"For all values of Xl, ... ,Xn, the formula
C(Xl, ... ,Xn) is true if there exist values of
Yl,· .. ,Yk such that the formula h(Xl,· .. ,Xn,Yl,· .
is true"
18 19
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 11/17
Meaning of Prolog Rules
With Variables
Sample run
cdf% pI
Welcome to SWI- Prolog (Multi-threaded, Version 5.2.11) ...
Example: ?- ['family'].
isaMother(X) ._ female (X) , parent (X, Y).W a rn i ng : ( . /f a mi l y. p l: 5 0) :
S i ng l et o n v a ri a bl e s: [y]
% family compiled 0.00 sec, 5,528 bytes
Logic:
parent(X, Y) 1\ female(X) :J isaM other(X).
But this is meaningless without quantifiers for
the variables.
?- isaMother(X).
X = victoria;
X = victoria;
No
The rule
A Prolog rule of this form (n ~ O,m::; n,k ~ 0) :
means:
VX1,···Xn
[::::lYl,·.. Yk [a(Xl,··· Xm, Yl,· .. Yk) :J C(Xl,· .. Xn)]
20 21
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 12/17
Rule Ordering and Unification How Prolog Handles a Query
1. rule ordering used in search
2. unification requires two instances of the
same variable in the same rule to get the
same value
Example 1
Database:
3. unification does not require differently named
variables to get different values: hence,
sibling (edward ,edward).
4. all rules searched if requested by'·'
1 ) m al e(t om ).
2 ) m al e( pe te r) .
3) m al e(d ou g).
4 ) f em al e( su sa n) .
5 ) m al e( da vi d) .
6) parent (doug, susan).
7) parent (tom, william).
8) parent (doug, david).
9) parent (doug, tom).
10) grandfather(GP, GC) :- male (GP),
parent (GP, X) ,
p ar en t( X, G C) .
Query:
I?- grandfather(X,Y).
22 23
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 13/17
Trace it by handTrace it in Prolog
[trace]Call:
Call:
Exit:
Call:
Exit:
Call:
Fail:
Redo:
Exit:
Call:
Fail:
Redo:
Exit:
Call:
Exit:
Call:
Fail:
Redo:
Exit:
Call:
Fail:
Redo:
Exit:
Call:
Exit:Exit:
X doug
Y = william
Ye s
24
?- grandfather(X,Y).( 7) g ra nd fa th er (_ G2 83 , _ G2 84 ) ? creep
( 8 ) m a le ( _G 28 3 ) ? creep
( 8 ) m a le ( to m) ? creep
( 8) p ar en t ( to m, _ L2 05 ) ? creep
( 8) p ar en t ( to m, w il li am ) ? creep
( 8) p ar en t ( wi ll ia m, _ G2 84 ) ? creep
( 8) p ar en t ( wi ll ia m, _ G2 84 ) ? creep
( 8 ) m a le ( _G 28 3 ) ? creep
( 8 ) m a le ( pe te r ) ? creep
( 8) p ar en t ( pe te r, _ L2 05 ) ? creep
( 8) p ar en t ( pe te r, _ L2 05 ) ? creep
( 8 ) m a le ( _G 28 3 ) ? creep
( 8 ) m a le ( do ug ) ? creep
( 8) p ar en t ( do ug , _ L2 05 ) ? creep
(8 ) p aren t (do ug, su san ) ? creep
( 8) p ar en t ( su sa n, _ G2 84 ) ? creep
( 8) p ar en t ( su sa n, _ G2 84 ) ? creep
( 8) p ar en t ( do ug , _ L2 05 ) ? creep
( 8) p ar en t ( do ug , d av id ) ? creep
( 8) p ar en t ( da vi d, _ G2 84 ) ? creep
( 8) p ar en t ( da vi d, _ G2 84 ) ? creep
( 8) p ar en t ( do ug , _ L2 05 ) ? creep
(8 ) p aren t (do ug, t om) ? creep
( 8) p ar en t ( to m, _ G2 84 ) ? creep
( 8) p ar en t ( to m, w il li am ) ? creep( 7) g ra nd fa th er ( do ug , w il li am ) ? creep
25
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 14/17
Prolog Search Trees Example 2
• Each node is an ordered list of goals. Database:
• Each edge is labelled with the variable bind-
ings that occurred due to applying a rule.
(The binding are in effect throughout the
subtree.)
1 ) m al e( al be rt ).
2 ) f em al e( al ic e) .
3 ) m al e( ed wa rd ).
4 ) f em al e( vi ct or ia ).
5 ) p ar e nt ( al be r t, e dw a rd ) .
6 ) p ar e nt ( vi ct o ri a ,e d wa r d) .
7 ) p ar e nt ( al be r t, a li c e) .
8 ) p ar e nt ( vi ct o ri a ,a l ic e ).
9) sibl ing( X, y) :- p ar en t( P, X ), p ar en t( P, y).
• Each leaf represents either success or fail-
ure.
Query:
?- sibling(alice,Asib).
Asib = edward ;
Asib = alice ;
Asib = edward;
Asib = alice ;
No
?- s i bl i ng ( As ib , a li c e) .
Asib = edward ;
Asib = edward ;
Asib = alice
Asib = alice
No
26 27
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 15/17
Trace it by hand Trace it in Prolog
[trace]
Call:
Call:
Exit:
Call:
Exit:
Exit:
?- sibling(alice,Asib).
( 7) s ib li ng (a li ce , _ G2 84 ) ? creep
( 8) p ar en t ( _L 20 5, a li ce ) ? creep
( 8) p ar en t ( al be rt , a li ce ) ? creep
( 8) p ar en t ( al be rt , _ G2 84 ) ? creep
( 8) p ar en t ( al be rt , e dw ar d) ? creep
( 7) s ib li ng (a li ce , e dw ar d) ? creep
Asib = edward ;
Redo: (8 ) p ar en t ( al be rt , _G284) ? creep
Exit: (8 ) p ar en t ( al be rt , a li ce ) ? creep
Exit: (7) s ib li ng (a li ce , a li ce ) ? creep
Asib = alice
Redo : (8 ) p aren t (_L 205 , a lic e) ? creep
Exi t: (8 ) p aren t (vi cto ria , ali ce) ? creep
Ca ll: (8 ) p aren t (vi cto ria , _G2 84) ? creep
Exi t: (8 ) p aren t (vi cto ria , edw ard) ? creep
Exi t: (7 ) si bli ng( ali ce, ed war d) ? creep
Asib = edward ;
Redo : (8 ) p aren t (vi cto ria , _G2 84) ? creep
Exi t: (8 ) p aren t (vi cto ria , ali ce) ? creep
Exi t: (7 ) si bli ng( ali ce, al ice ) ? creep
Asib = alice
No
28 29
8/6/2019 Prolog1 33 2up.ps
http://slidepdf.com/reader/full/prolog1-33-2upps 17/17
Logic Programming vs. Prolog Logic Programming vs. Prolog
cousin(X,Y) :- parent(W,X), sister(W,Z),
parent(Z,Y).
I ?- cousin(X,jane). % a query
Logic Programming: Nondeterministic
• Arbitrarily choose rule to expand first
• Arbitrarily choose subgoal to to explore
first
• Results don't depend on rule and sub-
goal ordering
cousin(X,Y) :- parent(W,X), brother(W,Z),
parent(Z,Y).
Rule and Goal Ordering:
• There are two rules for cousin
• Which rule do we try first?
• Each rule for cousin has several subgoals
• Which subgoal do we try first?
Prolog: Deterministic
• Expand first rule first
• Explore first subgoal first
• Results may depend on rule and subgoal
ordering
32 33
top related