Transcript ppt

Announcements

We are almost done grading HW1

HW2 due today

Download SWI Prolog!
HW3 (Prolog) will be posted today, due on
February 29th

Spring 16 CSCI 4430, A Milanova
1
Last Class

Bottom-up (LR) parsing




Characteristic Finite State Machine (CFSM)
SLR(1) parsing table
Conflicts in SLR(1)
LR parsing variants
Spring 16 CSCI 4430, A Milanova
2
Today’s Lecture Outline

Logic Programming
Logic Programming Concepts

Prolog




Language constructs: facts, queries, rules
Prolog concepts: search tree, unification,
backtracking, backward chaining
Lists
Spring 16 CSCI 4430, A Milanova
3
Logic Programming and Prolog
Read: Scott, Chapter 11.1, 11.2.1-4
4
Logic Programming

Download and install SWI Prolog on laptop!

J.R.Fisher’s Prolog Tutorial:
http://www.cpp.edu/~jrfisher/www/prolog_tutorial/contents.html
Spring 16 CSCI 4430, A Milanova
5
Logic Programming

Logic programming

Logic programming is declarative programming
Logic program states what (logic), not how (control)

Programmer declares axioms



Programmer states a theorem, or a goal (the what)


In Prolog, facts and rules
In Prolog, a query
Language implementation determines how to use the
axioms to prove the goal
Spring 16 CSCI 4430, A Milanova
6
Logic Programming

Logic programming style is characterized by

Database of facts and rules that represent
logical relations. Computation is modeled as
search (queries) over this database

Manipulation of lists and use of recursion, in
which it is very similar to the functional
programming style
Spring 16 CSCI 4430, A Milanova
7
Logic Programming Concepts

A Horn Clause is: H  B1,B2,…,Bn



Meaning of a Horn clause:



Antecedents (B’s): conjunction of zero or more terms in
predicate calculus; this is the body of the horn clause
Consequent (H): a term in predicate calculus
H is true if B1, B2 , . . . , and Bn are all true
When the antecedents Bi are all true, we deduce that
consequent H is true as well
For example:
rainy(seattle).
cold(rochester).
snowy(rochester)  rainy(rochester),cold(rochester).
8
Logic Programming Concepts
Resolution Principle
If two Horn clauses
A  B1,B2,B3 ,…, Bm
C  D1,D2,D3 ,…, Dn
are such that A matches D1,
then we can replace D1 with B1,B2,B3 ,…, Bm
C  B1,B2,B3 ,…, Bm,D2,D3 ,…, Dn
For example:
Spring 16 CSCI 4430, A Milanova
C  A,B
DC
A
D  A,B,C
9
Horn Clauses in Prolog

In Prolog, a Horn clause is written
h :- b1,...,bn.


Horn Clause is called clause
Consequent is called goal or head
Antecedents are called subgoals or tail

Horn Clause with no tail is a fact



E.g., rainy(seattle). Depends on no other conditions
Horn Clause with a tail is a rule
snowy(X) :- rainy(X),cold(X).
Spring 16 CSCI 4430, A Milanova
10
Horn Clauses in Prolog

Clause is composed of terms

Constants


Number, e.g., 123, etc.
Atoms e.g., seattle, rochester, rainy, foo
In Prolog, atoms begin with a lower-case letter!

Variables

X, Foo, My_var
In Prolog, variables must begin with upper-case letter!

Structures consists of an atom, called a functor
and a list of arguments

rainy(seattle), snowy(X)
11
Horn Clauses in Prolog

Variables may appear in the tail and head of a
rule:


c(X) :- h(X,Y).
For all values of X, c(X) is true if there exist a
value of Y such that h(X,Y) is true
Call Y an auxiliary variable. Its value will be bound
to make consequent true, but not reported by
Prolog, because it does not appear in the head
Spring 16 CSCI 4430, A Milanova
12
Lecture Outline

Logic Programming
Logic Programming Concepts

Prolog




Language constructs: facts, queries, rules
Prolog concepts: search tree, unification,
backtracking, backward chaining
Lists
Spring 16 CSCI 4430, A Milanova
13
Prolog


Program has a database of clauses i.e., facts and
rules; the rules help derive more facts
We add simple queries with constants, variables,
conjunctions or disjunctions
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X),cold(X).
? - rainy(C).
? – snowy(C).
Spring 16 CSCI 4430, A Milanova
14
Facts
likes(eve, pie).
food(pie).
likes(al, eve).food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
functors
constants
The combination of the functor and its arity (i.e.,
its number of arguments) is called a predicate.
Spring 16 CSCI 4430, A Milanova
15
Queries
likes(eve, pie).
food(pie).
likes(al, eve).food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
variable
query
?-likes(al,eve).
?-likes(al,Who).
true.
Who=eve.
answer
?-likes(al,pie).
false.
?-likes(eve,al).
false.
Spring 16 CSCI 4430, A Milanova
?-likes(eve,W).answer with
variable binding
W=pie ;
W=tom ;
W=eve .
force search for
more answers
16
Question
likes(eve, pie).
food(pie).
likes(al, eve).food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
?-likes(eve,W).
W = pie ;
W = tom ;
W = eve .
Prolog gives us the answer precisely in this order:
first W=pie then W=tom and finally W=eve.
Can you guess why?
17
Harder Queries
likes(eve, pie).
likes(al, eve).
likes(eve, tom).
likes(eve, eve).
and
food(pie).
food(apple).
person(tom).
?-likes(al,V) , likes(eve,V).
V=eve.
?-likes(eve,W), person(W).
W=tom
?-likes(A,B).
A=eve,B=pie ; A=al,B=eve ; A=eve,B=tom ;
A=eve,B=eve.
?-likes(D,D).
D=eve.
18
Harder Queries
likes(eve, pie).
food(pie).
likes(al, eve).food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
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
Spring 16 CSCI 4430, A Milanova
19
Rules
likes(eve, pie).
food(pie).
likes(al, eve).food(apple).
likes(eve, tom).
person(tom).
likes(eve, eve).
Add a rule to the database:
rule1:-likes(eve,V),person(V).
?-rule1.
Spring 16 CSCI 4430, A Milanova
20
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
?-rule2(pie).
false.
Spring 16 CSCI 4430, A Milanova
21
Lecture Outline

Logic Programming
Logic Programming Concepts

Prolog




Language constructs: facts, queries, rules
Prolog concepts: search tree, unification,
backtracking, backward chaining
Lists
Spring 16 CSCI 4430, A Milanova
22
Logical Semantics

Prolog program consists of facts and rules
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
Rules like snowy(X):- rainy(X),cold(X).
correspond to logical formulas
X[snowy(X) 
rainly(X) ^ cold(X)]
/* For every X, X is snowy, if X is rainy and X is cold */
Spring 16 CSCI 4430, A Milanova
23
Logical Semantics
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
A query such as ?- rainy(C).
triggers resolution. Logical semantics does
not impose restriction in the order of application
of resolution rules

C = seattle
C = rochester
Spring 16 CSCI 4430, A Milanova
C = rochester
C = seattle
24
Procedural Semantics
?- snowy(C)
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
snowy(X) :- rainy(X),cold(X).
First, find the first clause in the database whose head matches
the query, in our case this is clause
snowy(X) :- rainy(X), cold(X)
Then, find a binding for X to make rainy(X) true; then, check
if cold(X) is true with that binding



If yes, report binding as successful
Otherwise, backtrack to the binding of X, unbind and consider
the next binding
Prolog’s computation is well-defined procedurally by search
tree, rule ordering, unification and backtracking
Spring 16 CSCI 4430, A Milanova
25
Question
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
snowy(troy).
What does this query yield?
?- snowy(C).
Answer:
C = rochester ;
C = troy.
Spring 16 CSCI 4430, A Milanova
26
Procedural Semantics
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X),cold(X).
snowy(C)
_C = _X
success
snowy(X)
AND
rainy(X)
X = seattle
OR
rainy(seattle)
cold(X)
X = rochester
rainy(rochester)
Spring 16 CSCI 4430, A Milanova
cold(seattle)
fails; backtrack.
cold(rochester)
27
Prolog Concepts: Search Tree
OR levels:
parent: goal (e.g., rainy(X))
children: heads-of-clauses (rainy(…))
ORDER: from left to right
AND levels:
parent: goal (e.g., snowy(X))
children: subgoals (rainy(X), cold(X))
ORDER: from left to right
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
?- snowy(C).
snowy(C)
snowy(X)
AND
rainy(X)
OR
rainy(seattle)
rainy(rochester)
cold(X)
cold(rochester)
28
Prolog Concepts: Unification

At OR levels Prolog performs unification


Unifies parent (goal), with child (head-of-clause)
E.g.,

snowy(C) =



rainy(seattle)
success, X = seattle
parents(alice,M,F) = parents(edward,victoria,albert)


success, _C = _X
rainy(X) =

snowy(X)
fail
parents(alice,M,F) = parents(alice,victoria,albert)

success, M = victoria, F = albert
In Prolog, = denotes unification, not assignment!29
Prolog Concepts: Unification

A constant unifies only with itself


Two structures unify if and only if (i) they have the
same functor, (ii) they have the same number of
arguments, and (iii) their arguments unify recursively


E.g., alice=alice, but alice=edward fails
E.g., rainy(X) = rainy(seattle)
A variable unifies with anything. If the other thing has
a value, then variable is bound to that value. If the
other thing is an unbound variable, then the two
variables are associated and if either one gets a
value, both do.
Spring 16 CSCI 4430, A Milanova
30
Prolog Concepts: Backtracking
If at some point, a goal fails, Prolog backtracks
to the last goal (i.e., last unification point)
where there is an untried binding, undoes
current binding and tries new binding (an
alternative OR branch), etc.
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X):-rainy(X),cold(X).
?- snowy(C).
snowy(C)
_C = _X
snowy(X)
AND
rainy(X)
X = seattle
OR
rainy(seattle)
rainy(rochester)
Spring 16 CSCI 4430, A Milanova
cold(seattle)
fails; backtrack.
cold(X)
cold(rochester)
31
Prolog Concepts: Backward Chaining

Backward chaining: starts
from goal, towards facts
? – snowy(rochester).

snowy(rochester):rainy(rochester),
cold(rochester)
rainy(rochester)
---------------------snowy(rochester):cold(rochester)
cold(rocheter)
---------------------snowy(rochester).
rainy(rochester)
snowy(rochester):rainy(rochester),
cold(rochester)
----------------------------cold(rochester)
snowy(rochester):cold(rochester)
----------------------------snowy(rochester).
Spring 16 CSCI 4430, A Milanova
Forward chaining: starts from
facts towards goal
? – snowy(rochester).
32
Exercise
takes(jane, his).
takes(jane, cs).
takes(ajit, art).
takes(ajit, cs).
classmates(X,Y):-takes(X,Z),takes(Y,Z).
?- classmates(jane,C).
Draw search tree for query.
What are the bindings for C?
Spring 16 CSCI 4430, A Milanova
33
Running Prolog Programs
Enter the database of facts into a file. E.g.,
we can enter the classmates database into
file classmates.pl
 At the interpreter prompt, load the file then
run queries. E.g.,
?- [classmates].
… true.

?- takes(ajit,C).
C = art ; C = cs.
34
Lecture Outline

Logic Programming
Logic programming concepts

Prolog




Language constructs: facts, queries, rules
Prolog concepts: search tree, unification,
backtracking, backward chaining
Lists
Spring 16 CSCI 4430, A Milanova
35
Lists
list
head
tail
[a,b,c]
a
[b,c]
[X,[cat],Y]
[a,[b,c],d]
X
a
[[cat],Y]
[[b,c],d]
[X | Y]
X
Y
a
b
c
[ ]
a
b
c
[ ]
d
Spring 16 CSCI 4430, A Milanova
[ ]
36
Lists: Unification

[ H1 | T1 ] = [ H2 | T2 ]



E.g., [ a | [b, c] ] = [ X | Y ]



Head H1 unifies with H2, possibly recursively
Tail T1 unifies with T2, possibly recursively
X = a
Y = [b, c]
NOTE: In Prolog, = denotes unification, not
assignment!
Spring 16 CSCI 4430, A Milanova
37
Question

[X,Y,Z] = [john, likes, fish]


[cat] = [X | Y]


X = john, Y = likes, Z = fish
X = cat, Y = [ ]
[[the, Y]|Z] = [[X, hare]|[is,here]]

X = the, Y = hare, Z = [is, here]
Spring 16 CSCI 4430, A Milanova
38
Lists: Unification


Sequence of comma separated terms, or
[ first term | rest_of_list ]
[ [the | Y] |
]
the
Z]
Y
= [ [X, hare] | [is, here]
Z
X
hare
Spring 16 CSCI 4430, A Milanova
is
here
[]
[]
39
Lists Unification
look at the trees to see how this works!
[ a, b, c ] = [ X | Y ]
X = a, Y = [b,c].
[a | Z ] =? [ X | Y ]
X = a, Y = Z.
Spring 16 CSCI 4430, A Milanova
40
Improper and Proper Lists
[1 | 2]
1
versus
2
Spring 16 CSCI 4430, A Milanova
[1, 2]
1
2
[ ]
41
Question. Can we unify these lists?
[abc, Y]
abc
Y
=?
[ ]
[ abc | Y ]
abc
Y
What happens here?
Answer: No. There is no value binding
for Y, that makes these two trees
isomorphic
42
Member_of “Procedure”
?- member(a,[a,b]).
true.
?- member(a,[b,c]).
false.
?- member(X,[a,b,c]).
X = a ;
X = b ;
1. member(A, [A | B] ).
X = c ;
2. member(A, [B | C]) :- member (A, C).
false
Spring 16 CSCI 4430, A Milanova
43
Example
?-
member(a,[b, c, X]).
1. member(A, [A | B]).
2. member(A, [B | C]) :- member (A, C).
Spring 16 CSCI 4430, A Milanova
44
Member_of “Procedure”
member(A, [A|B]).
member(A, [B|C]) :- member (A,C).
logical semantics: For every value assignment
of A, B and C, we have
member(A,[B|C]) if member(A,C);
procedural semantics: Head of clause is
procedure entry. Procedure body consists of
calls within this procedure.
Spring 16 CSCI 4430, A Milanova
45
Prolog Search Tree (OR levels only)
member(X,[a,b,c])
A=X=a,B=[b,c]
A=X,B=a,C=[b,c]
X=a
member(X,[b,c])
success
A’=X,B’=b,C’=[c]
A’=X=b,B’=[c]
member(X,[c])
X=b
A”=X
A”=X=c,B”=[ ]
success
B”=c, C”=[ ]
X=c
member(X,[ ])
success
fail
1. member(A, [A | B] ).
2. member(A, [B | C]) :- member (A, C).
fail
46
Another Search Tree
member(a, [b,c,X])
A=a, B = b, C = [c,X]
fail, a can’tmember(a,[c, X])
unify with b
A’=a, B’=c, C’=[X]
fail, a can’t member(a,[X]).
unify with c
A’’=a,X=B”,C”= [ ]
A’’=X=a, B”= [ ]
member(a,[ ])
success
1. member(A, [A | B] ).
2. member(A, [B | C]) :- member (A, C).
Spring 16 CSCI 4430, A Milanova
fail, can’t
unify [ ]
with a list
fail,
ditto
47
Append “Procedure”
append([ ], A, A).
append([A|B], C, [A|D]) :- append(B,C,D).

Build a list
?- append([a],[b],Y).
Y = [ a,b ]

Break a list into constituent parts
?- append(X,[b],[a,b]).
X = [ a ]
?- append([a],Y,[a,b]).
Y = [ b ]
Spring 16 CSCI 4430, A Milanova
48
More Append
? - append(X,Y,[a,b]).
X = [ ]
Y = [a,b] ;
X = [a]
Y = [b]
;
X = [a,b]
Y = [ ]
;
false.
Spring 16 CSCI 4430, A Milanova
49
More Append

Generating an unbounded number of lists
?- append(X,[b],Y).
X = [ ]
Y = [b] ;
X = [ _169]
Y = [ _169, b]
;
X = [ _169, _170 ]
Y = [ _169, _170, b]
etc.
Spring 16 CSCI 4430, A Milanova
;
50
Group Exercise

Create a file lists.pl, add member and
append to it
1. member(A, [A | B]).
2. member(A, [B | C]) :- member (A, C).
1. append([ ], A, A).
2. append([A|B], C, [A|D]) :- append(B,C,D).


Write a few queries with member and append
Now add another “procedure” brackets
which encloses each element in brackets
e.g., brackets([a,[b],c],R).
returns R = [[a],[[b]],[c]]
Spring 16 CSCI 4430, A Milanova
51