Logic Using Prolog
Logic Using Prolog
Language constructs
Facts, rules, queries through examples
Horn clauses
Goal-oriented semantics
Procedural semantics
1
Prolog, CS314 Fall 2007 BGRyder/Borgida
inYear(jane,3).
%% jane.year = 3;
2
3
Prolog, CS314 Fall 2007 BGRyder/Borgida
Logic Programming
Computation engine: theorem-proving
Uses unification, resolution, backward chaining,
backtracking
4
Prolog, CS314 Fall 2007 BGRyder/Borgida
Prolog
As database management
Start with program as a database of facts
Simple queries with constants and variables
(binding), conjunctions and disjunctions
Add to program rules to derive additional facts
Two interpretations
Declarative: based on logic
Procedural: searching for answers to queries
Search trees and rule firings can be traced
5
Prolog, CS314 Fall 2007 BGRyder/Borgida
Facts
likes(eve, pie).
likes(al, eve).
likes(eve, tom).
likes(eve, eve).
predicates
food(pie).
food(apple).
person(tom).
constants
6
Prolog, CS314 Fall 2007 BGRyder/Borgida
food(pie).
food(apple).
person(tom).
variable
?-likes(al,Who).
Who=eve
?-likes(eve,W).
W=pie ;
answer with
W=tom ;
variable binding
W=eve ;
no
force search for
more answers 7
Harder Queries
likes(eve, pie).
likes(al, eve).
likes(eve, tom).
likes(eve, eve).
food(pie).
food(apple).
person(tom).
?-likes(A,B).
A=eve,B=pie ; A=al,B=eve ;
?-likes(D,D).
and
D=eve ; no
?-likes(eve,W), person(W).
W=tom
?-likes(al,V), likes(eve,V).
V=eve ; no
Harder Queries
likes(eve, pie).
likes(al, eve).
likes(eve, tom).
likes(eve, eve).
food(pie).
food(apple).
person(tom).
same binding
?-likes(eve,W),likes(W,V).
W=eve,V=pie ; W=eve,V=tom ; W=eve,V=eve
?-likes(eve,W),person(W),food(V).
W=tom,V=pie ; W=tom,V=apple or
?-likes(eve,V),(person(V);food(V)).
V=pie ; V=tom ; no
?-likes(eve,W),\+likes(al,W).
W=pie ; W=tom ; no
Prolog, CS314 Fall 2007 BGRyder/Borgida
not
Rules
likes(eve, pie).
likes(al, eve).
likes(eve, tom).
likes(eve, eve).
food(pie).
food(apple).
person(tom).
Rules
likes(eve, pie).
food(pie).
likes(al, eve).
food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
rule1:-likes(eve,V),person(V).
rule2(V):-likes(eve,V),person(V).
?-rule2(H).
H=tom ; no
?-rule2(pie).
no
Note rule1 and rule2 are just like any other predicate!
11
Prolog, CS314 Fall 2007 BGRyder/Borgida
?- sister_of(alice,Y).
Y = edward
?- sister_of(alice, victoria).
no
14
Prolog, CS314 Fall 2007 BGRyder/Borgida
Horn Clauses
(logical foundations)
likes(calvin,hobbes)
tiger(hobbes), child(calvin).
15
Prolog, CS314 Fall 2007 BGRyder/Borgida
Horn Clauses
In Prolog, a Horn clause c h1 ^ ^hn
is written c :- h1 , ... , hn.
Horn Clause is a Clause
Consequent is a Goal or a Head
Antecedents are Subgoals or Tail
Horn Clause with No Tail is a Fact
male(edward). dependent on no other conditions
16
Prolog, CS314 Fall 2007 BGRyder/Borgida
Horn Clauses
Variables may appear in the antecedents and
consequent of a Horn clause:
c(X1,. . . ,Xn) :- h(X1 ,.. . ,Xn,Y1 ,.. . ,Yk).
For all values of X1 ,.. . ,Xn , the formula
c(X1,. . . ,Xn) is true if there exist values
of Y1,. . . ,Yk such that the formula
h(X1 ,. . . ,Xn,Y1 ,.. . ,Yk) is true
Call Yi an auxiliary variable. Its value will be
bound to make consequent true, but not reported
by Prolog, because it doesnt appear in the
consequent.
17
Prolog, CS314 Fall 2007 BGRyder/Borgida
Declarative Semantics
Prolog program consists of facts and rules
Rules like
sister_of(X,Y):female(X),parents(X,M,F),parents(Y,M,F).
18
Prolog, CS314 Fall 2007 BGRyder/Borgida
Declarative Semantics
A query is a conjunction of atoms, to
be proven
If query has no variables and is provable,
answer is yes
If query has variables, proof process
causes some variables to be bound to
values (called a substitution); these are
reported
19
Prolog, CS314 Fall 2007 BGRyder/Borgida
Example
?-sister_of(X,Y):
female(X),parents(X,M,F),parents(Y,M,F).
?-sister_of(alice,Y). (1)male(albert).
Y = edward
(2)female(alice).
(3)male(edward).
?-sister_of(X,Y).
(4)female(victoria).
X = alice
(5)parents(edward,victoria,albert).
(6)parents(alice,victoria,albert).
Y = edward ;
X = alice
Y = alice ;
Example shows
no
-subgoal order of evaluation
-backtracking
-computation in rule order
20
10
Procedural Semantics
?-sister_of(X,Y):
female(X),parents(X,M,F),parents(Y,M,F).
21
Prolog, CS314 Fall 2007 BGRyder/Borgida
22
Prolog, CS314 Fall 2007 BGRyder/Borgida
11
Example
sis(X,Y):-female(X),parents(X,M,F),
parents(Y,M,F),\+(X==Y).
?-sis(X,Y). last subgoal disallows X,Y to have same
value
X=alice
Y=edward ;
no
Negation as Failure
\+(P) succeeds when P fails
12
Negation by Failure
Not equivalent to logical not in Prolog
Prolog can only assert that something is
true
Prolog cannot assert that something is
false, but only that it cannot be proven
with the given rules
25
Prolog, CS314 Fall 2007 BGRyder/Borgida
Transitive Relations
Family tree
lee joe
ann mike
mary al
parents(jane,bob,sally).
parents(john,bob,sally).
parents(sally,al,mary).
parents(bob,mike,ann).
parents(mary,joe,lee).
sally bob
ancestor(X,Y) :- parents(X,Y,_).
jane john
ancestor(X,Y) :- parents(X,_,Y).
ancestor(X,Y) :- parents(X,W,_),ancestor(W,Y).
ancestor(X,Y) :- parents(X,_,W), ancestor(W,Y).
?- ancestor(jane,X).
X = bob ;
X = sally ;
X = mike ;
X = ann ;
Prolog, CS314 Fall 2007 BGRyder/Borgida
X= al ;
X = mary ;
X = joe ;
X = lee ;
No
26
13
Prolog: Deterministic
Expand first rule first
Explore first(leftmost) subgoal first
Results may depend on rule and subgoal ordering
27
Prolog, CS314 Fall 2007 BGRyder/Borgida
28
14