- 107 Views
- Uploaded on
- Presentation posted in: General

CS 60

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

CS 60

2012-09-26

Wednesday – Week 3

- parent(sleeping, swimming).
- parent(swimming, skittles).
- parent(skittles, spam).
- parent(42, spam).
- child(Kid, P) :- parent(P, Kid).

42

10

1

Kid = spam

What = P

9

2

7

8

6

5

3

4

P = sleeping

Kid = swimming

P = swimming

Kid = skittles

P = skittles

Kid = spam

P = 42

Kid = spam

42

- anc(A, B) :- parent(A, B).
- anc(X, Y) :- parent(X, Z), anc(Z, Y).

42

A

B

This matched with the base case!

42

X

Z

A

Y

B

42

42

We made a recursive call here. And then matched with the base case.

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2

X = Y

- X = Y succeeds if X can be unified with Y.
X \= Y

- X \= Y succeeds if X cannot be unified with Y.
X == Y

- X == Y succeeds if X and Y are identical, i.e., they unify with no variable bindings occurring.
X \== Y

- X \== Y succeeds if X and Y are not identical.
http://www.amzi.com/manuals/amzi/pro/ref_manipulating_terms.htm#EqualityofTerms

\= succeeds if they CANNOT be unified

= succeeds if they CAN be unified

== succeeds if they are ALREADY the same

\== succeeds if they are NOT ALREADY the same

- parent(homer, bart).
- parent(homer, lisa).
- parent(marge, bart).
- parent(marge, lisa).
- parent(homer, maggie).
- parent(marge, maggie).
- age(lisa, 8).
- age(maggie, 1).
- age(bart, 10).
- age(marge, 35).
- age(homer, 38).
- person(marge).
- person(homer).
- person(lisa).
- person(bart).
- person(maggie).

This says that bart is his own sibling!

Works for specific cases.

If X and Y are unbound, X \= Y returns false

REMEMBER:

Put negation as late as possible so that variables already have known values!

Why are there all of these duplicates?

setof allows you to see only non-duplicates

[X, Y] gives the format we want our answer in (as a list)

\+ means not

Singleton variables: [Y].

_ is a variable that doesn’t need to match anything else.

Doesn’t work with variables!

- eightYearsOld(X) :-
- olderThan8(X) :-
- not8(X) :-
Hints: You can use > < and these can build upon each other

Lists in Prolog

Demo Length

Expressing if in prolog

L unifies with []

N unifies with 0

"The length of L is N"

if

and

length(L,N) :- L = [], N = 0.

Even simpler version of this:

Prolog allows pattern-matching within the left-hand side of its rules!

length([],0).

Nonempty matching: The cons bar!

length([],0).

This matches only the empty list.

This matches any NON-empty list – and names the first F and the rest R!

length([F|R],N) :-

This is read "F cons R" . It only binds to nonempty lists - and you can use F and R!

Expressing if in prolog

"F cons R"

L unifies with [F|R]

F = first of L

"The length of L is N"

R = rest of L

if

L is nonempty!

length(L,N) :- L = [F|R],

and

length(R,M),

and

N is M+1.

Even simpler version of this:

length([F|R],N) :- length(R,M),

N is M+1.

pattern matching is cool!

Inference on structure, instead of a database.

More prolog lists, trees, graphs

Test

Generate

member(E, [1,2,3,4,5]).

E = 1 ;

E = 2 ;

E = 3 ;

E = 4 ;

E = 5 ;

false

member(3, [1,2,3,4,5]).

true

member(6, [1,2,3,4,5]).

false

both variable?

don't cares

Prolog let's you say "I don't care!"

We never used R … it's a singleton.

mem( E, [E|R] ).

mem( E, [F|R] ) :- mem(E, R).

Prolog will warn you about singletons!

and we never used F in this rule!

The underscore _ is just a place holder:

mem( E, [E|_] ).

mem( E, [_|R] ) :- mem(E, R).

Let me underscore that _ has no value!

Prolog patterns

(1) Base case (usually no constraint clauses at all!)

mem(E, [E|_]).

mem(E, [_|R]) :- mem(E, R).

(2) Recursion first! (when possible)

an element

a list

(3) Arithmetic next (RHS must be bound)

len([], 0).

len([_|R], N) :- len(R, M), N is M+1.

list

its length

(4) Negation last (all vars must be bound)

true or false ?

Name: ________________

For each line, determine if Prolog will find it true or false. What bindings will Prolog create?

[F|R] = [42]. <--

[F|R] = []. <--

[Root,L,R] = [42,[],[]] <--

[Root,L,R] = [42,[]] <--

X = 5+2. <--

X \= Y. <--

X=3, Y=2, X \= Y. <--

Binding

=

tries to assign any two structures to one another…

1+2 == 1+2. <--

1+2 == 2+1. <--

[1,X] == [1,X]. <--

[1,2] == [1,X]. <--

X \== Y. <--

X=3, Y=3, X \== Y. <--

Structure

==

compares any two structures to one another…

Math

X is 5+2. <--

X is Y+3. <--

Y = 6, X is Y*7. <--

1+2 is 2+1. <--

X = 3, Y < X <--

is

computes the right-hand side and binds to the left-hand side

Not all equals are created equal!

For each line, determine if Prolog will find it true or false. What bindings will Prolog create?

[F|R] = [42]. <-- true, binds F to 42 and R to []

[F|R] = []. <-- false, [] does not have a first!

[Root,L,R] = [42,[],[]] <-- true, binds Root=42, L=R=[]

[Root,L,R] = [42,[]] <-- false, different # of elems.

X = 5+2. <-- true, but X is 5+2, not 7

X \= Y. <-- false, X CAN unify with Y.

X=3, Y=2, X \= Y. <-- true, they can not be unified

Binding

=

tries to assign any two structures to one another…

1+2 == 1+2. <-- true, the same structure

1+2 == 2+1. <-- false, NOT the same structure!

[1,X] == [1,X]. <-- true

[1,2] == [1,X]. <-- false, no bindings are made

X \== Y. <-- true, they have different names!

X=3, Y=3, X \== Y. <-- false, it uses the values!

Structure

==

compares any two structures to one another…

Math

X is 5+2. <-- true, now X is 7

X is Y+3. <-- error: Y is unbound

Y = 6, X is Y*7. <-- true, X is bound to 42

1+2 is 2+1. <-- false: only evals the RHS

X = 3, Y < X <-- error: Y is unbound

is

computes the right-hand side and binds to the left-hand side

age

Homework Reference

age

male

female

93

92

97

99

uggette

ug

helga

olf

65

76

101

78

59

62

matilda

skug

skugerina

homericus

john

jackie

27

44

35

55

54

38

38

38

41

cher

glum

marge

patty

selma

gemini

esmerelda

homer

gomer

19

1

18

10

8

20

8

8

lachesis

maggie

atropos

lisa

terpsichore

klotho

bart

millhouse

- What is at least one thing Prof. Lewis does during lecture that helps you learn?
- What is at least one thing Prof. Lewis could do (or could stop doing) during lecture to help you learn?
- What is one thing you’re having trouble understanding in CS60?
- Other comments?