Cs 60
This presentation is the property of its rightful owner.
Sponsored Links
1 / 39

CS 60 PowerPoint PPT Presentation


  • 98 Views
  • Uploaded on
  • Presentation posted in: General

CS 60. 2012-09-26 Wednesday – Week 3. Prof Lewis’ world + child. 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

Download Presentation

CS 60

An Image/Link below is provided (as is) to download presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 60

CS 60

2012-09-26

Wednesday – Week 3


Prof lewis world child

Prof Lewis’ world + child

  • parent(sleeping, swimming).

  • parent(swimming, skittles).

  • parent(skittles, spam).

  • parent(42, spam).

  • child(Kid, P) :- parent(P, Kid).

42


Cs 60

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


Ancestor anc

Ancestor (anc)

42


Ancestor anc1

Ancestor (anc)

  • anc(A, B) :- parent(A, B).

  • anc(X, Y) :- parent(X, Z), anc(Z, Y).

42


Cs 60

A

B

This matched with the base case!

42


Cs 60

X

Z

A

Y

B

42

42

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


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Cs 60

R1

R2

F1

F1

F2

F2

R1

R2

R1

R2

F1

F1

F1

F1

F2

F2

F2

F2


Negation summary

Negation (Summary)

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


Negation demo

Negation Demo

\= 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


Simpsonsmall pl

simpsonSmall.pl

  • 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).


Sib demo big idea negation

sib Demo (Big idea: Negation)

This says that bart is his own sibling!


Sib demo big idea negation1

sib Demo (Big idea: Negation)

Works for specific cases.

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


Sib demo big idea negation2

sib Demo (Big idea: Negation)

REMEMBER:

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

Why are there all of these duplicates?


Setof format query var

setof(format,query,Var).

setof allows you to see only non-duplicates

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


Notaparent demo intro

notAparent Demo (Intro: \+ )

\+ means not

Singleton variables: [Y].

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

Doesn’t work with variables!


Write

Write:

  • eightYearsOld(X) :-

  • olderThan8(X) :-

  • not8(X) :-

    Hints: You can use > < and these can build upon each other


Lists in prolog

Lists in Prolog

Demo Length


Cs 60

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).


Cs 60

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!


Cs 60

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!


Cs 60

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?


Cs 60

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!


Cs 60

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)


Cs 60

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


Cs 60

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


Cs 60

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


Solutions

Solutions


Cs 60 survey 2012 09 26 turn this in but don t write your name on it

CS 60 Survey 2012-09-26 (Turn this in, but don’t write your name on it)

  • 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?


  • Login