Transcript ppt

Announcements

HW1 is graded



I’ll release your grades in HW Server after class
Papers available for pickup in front of my office,
Lally 314
Quiz 1&2 graded too


Grades will be available soon
We’ll go over quiz problems before tests
Spring 16 CSCI 4430, A Milanova
1
Announcements

HW3 (Prolog) is due February 29th





I’ll have HW3 set in the HW Server today
Get started with SWI Prolog
Try the simple examples from class
Read Chapter 11 in Scott’s book
Ask questions!
Spring 16 CSCI 4430, A Milanova
2
Last Class

Logic Programming
Logic Programming Concepts

Prolog



Language constructs: facts, rules and queries
Prolog concepts: search tree, rule ordering,
unification, backtracking, backward chaining
Spring 16 CSCI 4430, A Milanova
3
Today’s Lecture Outline

Prolog



Lists
Arithmetic
Imperative Control Flow
Spring 16 CSCI 4430, A Milanova
4
Logic Programming and Prolog
Keep reading: Scott, Chapter 11.2.1-6
5
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
[ ]
6
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
7
Improper and Proper Lists
[1 | 2]
1
versus
2
Spring 16 CSCI 4430, A Milanova
[1, 2]
1
2
[ ]
8
Question. Can we unify these lists?
[abc, Y]
abc
Y
=?
[ ]
[abc | Y]
abc
Y
What happens here? Can we unify these lists?
Spring 16 CSCI 4430, A Milanova
9
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
10
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
11
Example
?-
member(a,[b, c, X]).
?- member(a,Y).
1. member(A, [A | B]).
2. member(A, [B | C]) :- member (A, C).
Lazy evaluation of unbounded list
structure. a as first element, then a as
second element, third element, etc.
Spring 16 CSCI 4430, A Milanova
12
Prolog Search Tree (simplified)
member(X,[a,b,c])
A=X=a,B=[b,c]
_A=_X,B=a,C=[b,c]
X=a
success
A’=X=b,B’=[c]
X=b
success
member(X,[b,c])
_A’=_X,B’=b,C’=[c]
member(X,[c])
A”=X=c,B”=[ ]
X=c
success
A”=X
B”=c, C”=[ ]
member(X,[ ])
fail
1. member(A, [A | B] ).
2. member(A, [B | C]) :- member (A, C).
fail
13
Question
1. member(A, [A | B]).
2. member(A, [B | C]) :- member(A, C).
Give all answers to the following query:
?- member(a,[b, a, X]).
Spring 16 CSCI 4430, A Milanova
14
Question
1. member(A, [A | B]).
2. member(A, [B | C]) :- member(A, C).
Give all answers to the following query:
?- member(a, [b | a]).
Spring 16 CSCI 4430, A Milanova
15
Another Search Tree
member(a, [b,c,X])
A=a, B = b, C = [c,X]
fail, a can’t
unify with b
member(a,[c, X])
A’=a, B’=c, C’=[X]
fail, a can’t
unify with c
member(a,[X]).
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).
fail, can’t unify
[ ] with a list
Spring 16 CSCI 4430, A Milanova
fail, ditto
16
“Procedural” Interpretation
member(A, [A|B]).
member(A, [B|C]) :- member(A,C).
member is a recursive “procedure”
member(A, [A|B]). is the base case.
“Procedure” exits with true if the element we are
looking for, A, is the first element in the list. It exits
with false if we have reached the end of the list
member(A, [B|C]) :- member(A,C). is the
recursive case. If element A is not the first element
in the list, call member recursively with arguments A
and tail C
Spring 16 CSCI 4430, A Milanova
17
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 ]
?- append([a,b,c],[d,e],Y).
Y = [ a,b,c,d,e ]
Spring 16 CSCI 4430, A Milanova
18
More Append
append([ ], A, A).
append([A|B], C, [A|D]) :- append(B,C,D).

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
19
More Append
? - append(X,Y,[a,b]).
Spring 16 CSCI 4430, A Milanova
20
More Append

Generating an unbounded number of lists
?- append(X,[b],Y).

Be careful when using append with 2
unbounded arguments!!!
Spring 16 CSCI 4430, A Milanova
21
Question

What does this “procedure” do:
p([],[]).
p([A|B],[[A]|Rest]) :- p(B,Rest).

Puts brackets around each element in the list
?- p([a,b,c],Y).
Y = [ [a],[b],[c] ]

It can also “flatten” a list:
?- p(X,[[a],[b],[c]]).
X = [ a,b,c ]
Spring 16 CSCI 4430, A Milanova
22
Common Structure

“Processing” a list:
proc([],[]).
proc([H|T],[H1|T1]) :- f(H,H1),proc(T,T1).


Base case: we have reached the end of list.
In our case, the result for [ ] is [ ].
Recursive case: result is [H1|T1]. H1 was
obtained by calling f(H,H1) --- processes
element H into result H1. T1 is the result of
recursive call of proc on T.
Spring 16 CSCI 4430, A Milanova
23
Lecture Outline

Prolog



Lists
Arithmetic
Imperative Control Flow
Spring 16 CSCI 4430, A Milanova
24
Arithmetic


Prolog has all arithmetic operators
Built-in predicate is
is(X, 1+3) or more commonly we write
 X is 1+3
is forces evaluation of 1+3:
?- X is 1+3
X = 4


= is unification not assignment!
?- X = 4-1
X = 4-1 % unifies X with 4-1!!!
Spring 16 CSCI 4430, A Milanova
25
Arithmetic: Common Pitfalls

is is not invertible! That is, arguments on
the right cannot be unbound!
3 is 3 – X.
ERROR: is/2: Arguments are not
sufficiently instantiated


This doesn’t work either:
?- X is 4, X = X+1.
false.
Why? What is going on here?
Spring 16 CSCI 4430, A Milanova
26
Exercise

Write sum, which takes a list of integers and
computes the sum of the integers. E.g.,
sum([1,2,3],R).
?- R = 6.

How about if the integers are arbitrarily
nested? E.g.,
sum([[1],[[[2]],3]],R).
?- R = 6.
Spring 16 CSCI 4430, A Milanova
27
Exercise

Write plus10, which takes a list of integers
and computes another list, where all integers
are shifted +10. E.g.,
plus10([1,2,3],R).
?- R = [11,12,13].

Write len, which takes a list and computes
the length of the list. E.g.,
len([1,[2],3],R).
?- R = 3.
Spring 16 CSCI 4430, A Milanova
28
Exercise

Write atoms, which takes a list and
computes the number of atoms in the list.
E.g.,
atoms([a,[b,[[c]]]],R).
?- R = 3.

Hint: built-in predicate atom(X) yields true if X is an
atom (i.e., symbolic constant such as x, abc,
tom).
Spring 16 CSCI 4430, A Milanova
29
Lecture Outline

Prolog



Lists
Arithmetic
Imperative Control Flow
Spring 16 CSCI 4430, A Milanova
30
Imperative Control Flow

Programmer has explicit control on
backtracking process
cut (!)

As a goal it succeeds, but with a side effect:

Commits interpreter to all bindings made since
unifying parent goal with left-hand side of current
rule
Spring 16 CSCI 4430, A Milanova
31
Cut (!) Example
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), !, cold(X).
?- snowy(C).
Spring 16 CSCI 4430, A Milanova
32
Cut (!) Example
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), !, cold(X).
snowy(C)
_C = _X
snowy(X)
AND
rainy(X)
X = seattle
OR
rainy(seattle)
Spring 16 CSCI 4430, A Milanova
!
rainy(rochester)
cold(seattle)
fails; no
backtracking to
rainy(X).
GOAL FAILS.
cold(X)
cold(rochester)
33
Cut (!) Example 2
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), !, cold(X).
snowy(troy).
?- snowy(C).
Spring 16 CSCI 4430, A Milanova
34
Cut (!) Example 2
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), !, cold(X).
snowy(troy).
snowy(C)
OR
2 committed OR
bindings:
_C = _X
and X = seattle
GOAL FAILS.
_C = _X
snowy(X)
snowy(troy)
AND
rainy(X)
X = seattle
OR
rainy(seattle)
!
rainy(rochester)
cold(X)
cold(rochester)
How about query ?- snowy(troy)?
Spring 16 CSCI 4430, A Milanova
35
Cut (!) Example 3
rainy(seattle) :- !.
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), cold(X).
snowy(troy).
?- snowy(C).
Spring 16 CSCI 4430, A Milanova
36
Cut (!) Example 3
rainy(seattle) :- !.
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), cold(X).
snowy(troy).
C = troy
SUCCEEDS
snowy(C)
OR
_C = _X
snowy(X)
Only rainy(X) is
committed to
bindings (X =
seattle).
C = troy
snowy(troy)
AND
rainy(X)
X = seattle
OR
rainy(seattle)
!
cold(X)
rainy(rochester)
cold(rochester)
How about goal ? - snowy(rochester)?
37
Cut (!) Example 4
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- !, rainy(X), cold(X).
?- snowy(C).
Spring 16 CSCI 4430, A Milanova
38
Cut (!) Example 4
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- !, rainy(X), cold(X).
snowy(C)
_C = _X
success
snowy(X)
!
rainy(X)
X = seattle
OR
rainy(seattle)
Spring 16 CSCI 4430, A Milanova
AND
cold(seattle)
fails;
backtrack.
cold(X)
X =
rochester
rainy(rochester)
cold(rochester)
39
Cut (!) Example 5
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), cold(X), !.
?- snowy(C).
Spring 16 CSCI 4430, A Milanova
40
Cut (!) Example 5
rainy(seattle).
rainy(rochester).
cold(rochester).
snowy(X) :- rainy(X), cold(X), !.
snowy(C)
_C = _X
success
snowy(X)
AND
X = seattle
rainy(X)
OR
rainy(seattle)
Spring 16 CSCI 4430, A Milanova
!
cold(X)
X = rochester
rainy(rochester)
cold(rochester)
41
Negation by Failure
takes(jane, his).
takes(jane, cs).
takes(ajit, art).
takes(ajit, cs).
classmates(X,Y):-takes(X,Z),takes(Y,Z).
?- classmates(jane,C).
classmates(X,Y):- takes(X,Z),
takes(Y,Z),
\+(X=Y).
Spring 16 CSCI 4430, A Milanova
42
Negation by Failure: not(X), \+(X)

not(X) succeeds when X fails
Called negation by failure, defined:
not(X):- X,!,fail.
not(_).
 classmates(X,Y):- takes(X,Z),
takes(Y,Z),
\+(X=Y).


Not the same as logical negation ¬X!
Spring 16 CSCI 4430, A Milanova
43
Example

p(X) :- q(X), not(r(X)).
r(X) :- w(X), not(s(X)).
q(a). q(b). q(c).
s(a). s(c).
w(a). w(b).

Evaluate:



?- p(a).
?- p(b).
?- p(c).
Spring 16 CSCI 4430, A Milanova
44
A Harder Exercise

1.
2.
3.


Remember the grammar…
S  aSbS
S  bSaS
Sε
Write a parser in Prolog which given a string,
computes all leftmost derivations:
?- parse([a,b,a,b],R).
R = [1, 3, 1, 3, 3] ; // seq. of
productions
R = [1, 2, 3, 3, 3] ; // different seq
false. // no more derivations
Hint: use append to break list into constituent
45