CS 6
This presentation is the property of its rightful owner.
Sponsored Links
1 / 39

OfficeHours(Who, When, Where) Zach: Fri. 2-4 pm, LAC; Colleen: Mon 2:30-4:30 1241 Olin PowerPoint PPT Presentation


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

CS 6. true!. Hw #8: Prolog. Due by 11:59 pm: Monday, 10/1. Problem #1: The Simpsons Problem #2: javabat Problem #3: unicalc Problem #4: lists part 1. Matt Groening 's family?. OfficeHours(Who, When, Where) Zach: Fri. 2-4 pm, LAC; Colleen: Mon 2:30-4:30 1241 Olin.

Download Presentation

OfficeHours(Who, When, Where) Zach: Fri. 2-4 pm, LAC; Colleen: Mon 2:30-4:30 1241 Olin

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


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

CS 6

true!

Hw #8: Prolog

Due by 11:59 pm: Monday, 10/1

Problem #1: The Simpsons

Problem #2: javabat

Problem #3: unicalc

Problem #4: lists part 1

Matt Groening's family?

OfficeHours(Who, When, Where)

Zach: Fri. 2-4 pm, LAC; Colleen: Mon 2:30-4:30 1241 Olin


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

The CS 60 language tree:

Programming

What mood am I in today?

Imperative

Programming

Declarative

Programming

Logic Programming

Functional Programming

Object-Oriented

Programming

Low-level Programming

Racket

Java

Prolog

JFlap

simply describe the problem - and I'll solve it!

algorithms via functions

data structures

machine models

more abstraction away from the machine

closer to the machine


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

Key to thinking in Prolog

You are not permitted to solve the problem!

So don't try.

Resistance is futile.

but have a nice day :-)

Note Prolog's Gallic sense of friendly inevitability.

What's more, this is almost legal Prolog syntax!


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

Prolog: true!

prolog is very definitive!

Data ~ similar to Racket's

X = [3.14,42,spam,a,'hi'].

floating point

strings

symbols

integers

But -- Prolog has only 1 algorithm!

Provide data and relationships to describe the world.

Then ask questions! function evaluation vs. inference…

The idea:

Racket

Prolog

There is not now, and never will be, a programming language in which it is the least bit difficult to write bad programs.

Caution!

- Lawrence Flon


Prolog demo a

Prolog Demo A

  • Open Prolog

  • Edit new file

  • Type some facts:

    • awesome(spam).

    • awesome(42).

    • awesome(swimming).

  • Save the file

No space

.


Prolog demo a1

Prolog Demo A

Things typed into Prolog interpreter

  • [lecture6a].

  • awesome(42).

  • awesome(41).

  • awesome(swimming).

  • awesome(hoch).

Loads the file

Queries


Search to find facts a k a definitions that match

Search to find facts (a.k.a. definitions) that match

6

1

5

4

3

2


Prolog demo a2

Prolog Demo A

Things starting with Uppercase letters are variables

Things typed into Prolog interpreter

  • awesome(What).

  • awesome(W).

  • awesome(w).

; gets you the rest of the things that match

Lowercase w isn’t a variable. This is false.


Search to find facts a k a definitions that match1

Search to find facts (a.k.a. definitions) that match

6

1

5

4

3

2

What = swimming

What = 42

What = spam


Prolog demo a check in

Prolog Demo A: Check-in

  • Write a definition to say that you are awesome.

  • Write a query to check if skittlesare awesome.


Prolog demo b

Prolog Demo B

Facts added to lecture6b.py

  • likes(dodds, 42).

  • likes(dodds, spam).

  • likes(colleen, swimming).

Reads: “Dodds likes 42”

Facts can connect multiple things


Prolog demo b1

Prolog Demo B

Queries typed into Prolog

  • likes(dodds, 42).

  • likes(dodds, W).

  • likes(Who, What).

There were two answers

Use helpful names!

Prolog tries to find bindings for all variables

2 bindings for each answer.


Search to find facts a k a definitions that match2

Search to find facts (a.k.a. definitions) that match

6

1

5

4

3

2


Search to find facts a k a definitions that match3

Search to find facts (a.k.a. definitions) that match

6

1

5

4

3

2

W = 42

W = spam


Search to find facts a k a definitions that match4

Search to find facts (a.k.a. definitions) that match

6

1

5

4

3

2

Who = dodds What = spam

Who = dodds What = 42

Who = colleen What = swimming


Prolog demo b check in

Prolog Demo B: Check-in

  • Write a definition to say that you like spam.

  • Write a query to see who likes spam.


Prolog demo c

Prolog Demo C

“if”

  • swam(dodds).

  • swam(lewis).

  • happy(lewis) :- swam(lewis).

facts

A rule

The head

The body


Prolog demo c1

Prolog Demo C

Queries typed into Prolog

  • happy(lewis).

  • happy(dodds).


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

6

1

2

5

4

3

swam(lewis). is like a new query

(aka subgoal)


Prolog demo c check in

Prolog Demo C: Check-in

  • Write a definition to say that you swam.

  • Write a rule to say that if you swam you are happy.

  • Update the diagram on the next page to show how the query happy(lewis) would be executed.


Update me

UPDATE ME


Prolog demo d

Prolog Demo D

Definitions/Rules

  • swam(dodds).

  • swam(lewis).

  • happy(Person) :- swam(Person).


Prolog demo d1

Prolog Demo D

  • happy(lewis).

  • happy(dodds).

  • happy(Who).


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

6

1

Person = lewis

2

5

4

3


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

6

1

Who = Person

2

5

4

3

Person = dodds

Person = lewis


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

Prolog Execution

Depth-first search through possible bindings

Given a goal, Prolog tries the each rule of that name...

If a rule has subgoals/body (a right-hand side),

Subgoals are checked in order, binding variables as needed

Those bindings persist until backtracking backs over them

Watch out for unbound variables… !

If a goal or subgoal fails, Prolog backtracks and tries again with the next available option (another binding or rule).

Be careful with negation!

trace.


Prolog demo d check in

Prolog Demo D: Check-in

  • Fill in the blank so that the query below returns false.

    happy(_________).

  • Write a sentence (or more) that explains why this returns false.


Prolog demo e

Prolog Demo E

Both paths were true.

  • swam(dodds).

  • swam(lewis).

  • ate(lewis, skittles).

  • happy(Person) :- swam(Person).

  • happy(Person) :- ate(Person, skittles).

Two paths to happiness


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

10

7

6

1

Person = lewis

Person = lewis

2

5

4

9

3

8


Prolog demo e1

Prolog Demo E

; means OR

We could re-write these two rules as 1.


Prolog demo e check in

Prolog Demo E: Check-in

  • Write a new rule that a person is happy if they ate spam or skittles.

  • Come up with a way that you will remember that ; means “or.” Describe your method below:


Prolog demo f

Prolog Demo F

Comma means AND

Rules/Definitions:

  • swam(dodds).

  • swam(lewis).

  • ate(lewis, skittles).

  • happy(Person) :- swam(Person), ate(Person, skittles).


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

8

1

Person = lewis

7

2

3

4

6

5


Prolog demo e check in1

Prolog Demo E: Check-in

  • Write a new rule that a person is happy if they ate spam and skittles.

  • Come up with a way that you will remember that , means “and.” Describe your method below:


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

File simpsons.pl

/*

* the age predicate

*/

age(helga, 97).

age(olf, 99).

age(uggette, 93).

age(ug, 92).

age(matilda, 65).

age(homericus, 76).

age(skugerina, 101).

age(skug, 78).

age(esmerelda, 55).

age(gemini, 54).

age(klotho, 20).

age(atropos, 19).

age(lachesis, 18).

age(marge, 35).

age(homer, 38).

age(lisa, 8).

age(maggie, 1).

age(bart, 10).

age(gomer, 41).

age(john, 62).

age(jackie, 59).

age(patty, 38).

age(selma, 38).

age(glum, 27).

age(cher, 44).

age(millhouse, 8).

age(terpsichore, 8).

/*

* the parent predicate

*/

parent(olf, skug).

parent(helga, skug).

parent(skug, esmerelda).

parent(skugerina, esmerelda).

parent(esmerelda, klotho).

parent(gemini, klotho).

parent(esmerelda, atropos).

parent(gemini, atropos).

parent(esmerelda, lachesis).

parent(gemini, lachesis).

parent(olf, homericus).

parent(helga, homericus).

parent(ug, matilda).

parent(uggette, matilda).

parent(homericus, homer).

parent(matilda, homer).

parent(homericus, gomer).

parent(matilda, gomer).

parent(homer, bart).

parent(marge, bart).

parent(homer, lisa).

parent(marge, lisa).

parent(homer, maggie).

parent(marge, maggie).

parent(john, marge).

parent(jackie, marge).

parent(john, selma).

parent(jackie, selma).

parent(john, patty).

parent(jackie, patty).

parent(john, glum).

parent(jackie, glum).

parent(glum, millhouse).

parent(cher, millhouse).

parent(glum, terpsichore).

parent(cher, terpsichore).

/*

* the male predicate

*/

male(olf).

male(skug).

male(homericus).

male(ug).

male(homer).

male(gomer).

male(gemini).

male(john).

male(glum).

male(bart).

male(millhouse).

/*

* the female predicate

*/

female(helga).

female(esmerelda).

female(skugerina).

female(uggette).

female(matilda).

female(marge).

female(jackie).

female(selma).

female(patty).

female(cher).

female(lisa).

female(maggie).

female(klotho).

female(atropos).

female(lachesis).

female(terpsichore).


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

age

A more intuitive picture ...

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

Oh, brother!


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

Homework Prep

Write these four predicates in prolog:

True if MomOrDad is the parent of Kid

child(MomOrDad, Kid) :-

True if Mom is the parent of Kid and Mom is female

mother(Mom,Kid) :-

True if X is the parent of Y or if X is an ancestor of Y’s parent

anc(X,Y) :-

anc(X,Y) :-

true if N is the factorial of X (for X >= 0)

fac(X,N) :-

fac( , ) :-

Extra!

you need a base case, too – what should it be?


Officehours who when where zach fri 2 4 pm lac colleen mon 2 30 4 30 1241 olin

/*

* Here are three rules about families

*/

child(X, Y) :- parent(Y, X).

mother(X, Y) :- female(X), parent(X, Y).

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

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


  • Login