- 135 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Intro to Prolog' - jasmine-long

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

Conventional vs Logic Programming

- Prolog = “Programming with Logic”
- Procedural programming – do this, do this, do this, …, execute
- Declarative programming – this is true, this is true,…, is this true?

Basic idea of Prolog

- Describe the situation of interest
- Ask a question
- Prolog logically deduces new facts about the situation we described
- Prolog gives us its deductions back as answers

Variables, Predicates

- Any capitalized letter or word in Prolog is a variable.
- Every letter or word not capitalized is a literal – either a constant or a predicate.
- A predicate is a fact about the world.
- Some predicates are rules for deducing more facts.
- Predicates represent relationships between things.

The world of Ann & Sue

- Doll is a toy.
- Snoopy is a toy.
- Ann plays with Snoopy.
- It’s Tuesday.
- Ann likes every toy she plays with.
- Sue likes everything Ann likes.

Translate the world to Prolog.Facts:

- Doll is a toy.
isToy(doll).

- Snoopy is a toy.
isToy(snoopy).

- Ann plays with Snoopy.
plays(ann,snoopy).

- It’s Tuesday.
tuesday.

Translate the world to Prolog:Rules

- Ann likes every toy she plays with.
likes(ann,X) :- isToy(X), plays(ann,X).

Read as: “Ann likes X if X is a toy and Ann plays with X.”

- In logic, this means: To prove that ann likes X, one must first prove that X is a toy and that Ann plays with X.
- Sue likes everything Ann likes – translate!

Multiple ways to prove

- There can be more than one way to prove something:
- Ann likes every toy she plays with, and she also likes every toy Susan plays with.
- Two ways to prove Ann likes something:
- likes(ann,X):- isToy(X), plays(ann,X).
- likes(ann,X):- isToy(X), plays(susan,X).

- Semicolon can also be used to express “or”.

Using Prolog

- Prolog is interpreted.
- Begin by building database.
assert(isToy(snoopy)).

assert(isToy(doll)).

- Ask – Is snoopy a toy?
?- isToy(snoopy).

Yes

- Ask – What are the toys?
?- isToy(X).

X = snoopy;X = doll;

No

User types “;” to mean – find another answer.

Consult

- When you write a program, you will not want to keep “asserting” everything from scratch.
- You can write facts to a prolog file and then add all your facts at once.
- Traditionally – ?- consult(‘filename’).
- Consult may be a menu option.

Unifying Variables

- Prolog uses pattern matching to find constants that can be substituted in for variables
- Two terms are unifiable if there is a variable substitution which makes the 2 terms have the same value.
- isToy(X) unifies with isToy(snoopy) under the assignment X = snoopy.

Proof Search

- Now that we know about unification, we are in a position to learn how Prolog searches a knowledge base to see if a query is satisfied.
- In other words: we are ready to learn about proof search

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

?- k(Y).

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

?- g(a), h(a).

?- k(Y).

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

?- g(a), h(a).

?- k(Y).

?- h(a).

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

?- g(a), h(a).

?- k(Y).

?- h(a).

†

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

X=b

?- g(a), h(a).

?- g(b), h(b).

?- k(Y).

?- h(a).

†

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

X=b

?- g(a), h(a).

?- g(b), h(b).

?- k(Y).

?- h(a).

?- h(b).

†

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

X=b

?- g(a), h(a).

?- g(b), h(b).

?- k(Y).

Y=b

?- h(a).

?- h(b).

†

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

k(X):- f(X), g(X), h(X).

Y=X

?- f(X), g(X), h(X).

X=a

X=b

?- g(a), h(a).

?- g(b), h(b).

?- k(Y).

Y=b;

no

?-

?- h(a).

?- h(b).

†

Another example

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

?- jealous(X,Y).

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=vincent

C=mia

?- loves(B,mia).

?- jealous(X,Y).

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=vincent

C=mia

?- loves(B,mia).

?- jealous(X,Y).

X=vincent

Y=vincent

B=vincent

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=vincent

C=mia

?- loves(B,mia).

?- jealous(X,Y).

X=vincent

Y=vincent;

X=vincent

Y=marsellus

B=vincent

B=marsellus

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=marsellus

C=mia

A=vincent

C=mia

?- loves(B,mia).

?- loves(B,mia).

?- jealous(X,Y).

X=vincent

Y=vincent;

X=vincent

Y=marsellus;

B=vincent

B=marsellus

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=marsellus

C=mia

A=vincent

C=mia

?- loves(B,mia).

?- loves(B,mia).

….

X=vincent

Y=marsellus;

X=marsellus

Y=vincent

B=vincent

B=vincent

B=marsellus

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=marsellus

C=mia

A=vincent

C=mia

?- loves(B,mia).

?- loves(B,mia).

….

X=marsellus

Y=vincent;

X=marsellus

Y=marsellus

B=vincent

B=vincent

B=marsellus

B=marsellus

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

X=A

Y=B

?- loves(A,C), loves(B,C).

A=marsellus

C=mia

A=vincent

C=mia

?- loves(B,mia).

?- loves(B,mia).

….

X=marsellus

Y=vincent;

X=marsellus

Y=marsellus;

no

B=vincent

B=vincent

B=marsellus

B=marsellus

Arithmetic

- Arithmetic in Prolog:
- X is 4 + 2.
- add(X,Y,Z) :- Z is X+Y.
- “=“ means unifiable.
- add(X,Y,Z) :- Z = X+Y.
- This sets Z to +(X,Y).
- +, -, *, /, // (integer division), mod, <, =<, >, >=, =:=

Recursion

- Recursive predicate: Predicate name appears on both sides of rule.
- Examples:
- ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).
- Must FIRST have base case:
- ancestor(X,Y) :- parent(X,Y).

Recursion Practice: Java and Prolog

- factorial(+X,-Y)
- True when X! = Y

- sequence(+N,-X)
- True when X is the Nth number of the sequence: 2,6,14,30

The pluses and minuses are for comments only – they aren’t prolog syntax.

Lists

- A list is a finite sequence of elements
- Examples of lists in Prolog:
[mia, vincent, jules, yolanda]

[mia, robber(honeybunny), X, 2, mia]

[ ]

[mia, [vincent, jules], [butch, friend(butch)]]

[[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

Important things about lists

- List elements are enclosed in square brackets
- The length of a list is the number of elements it has
- All sorts of Prolog terms can be elements of a list
- There is a special list: the empty list [ ]

Head and Tail

- A non-empty list can be thought of as consisting of two parts
- The head
- The tail

- The head is the first item in the list
- The tail is everything else
- The tail is the list that remains when we take the first element away
- The tail of a list is always a list

Head and Tail example 1

- [mia, vincent, jules, yolanda]Head: Tail:

Head and Tail example 1

- [mia, vincent, jules, yolanda]Head: miaTail:

Head and Tail example 1

- [mia, vincent, jules, yolanda]Head: miaTail: [vincent, jules, yolanda]

Head and Tail example 2

- [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:

Head and Tail example 2

- [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ]Tail:

Head and Tail example 2

- [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ]Tail: [dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]]

Head and Tail example 3

- [dead(z)] Head: Tail:

Head and Tail example 3

- [dead(z)] Head: dead(z) Tail:

Head and Tail example 3

- [dead(z)] Head: dead(z) Tail: [ ]

Head and tail of empty list

- The empty list has neither a head nor a tail
- For Prolog, [ ] is a special simple list without any internal structure
- The empty list plays an important role in recursive predicates for list processing in Prolog

The built-in operator |

- Prolog has a special built-in operator | which can be used to decompose a list into its head and tail
- The | operator is a key tool for writing Prolog list manipulation predicates

The built-in operator |

?- [Head|Tail] = [mia, vincent, jules, yolanda].

Head = mia

Tail = [vincent,jules,yolanda]

yes

?-

The built-in operator |

?- [X,Y|Tail] = [[ ], dead(z), [2, [b,c]], [], Z, [2, [b,c]]] .

X = [ ]

Y = dead(z)

Z = _4543

Tail = [[2, [b,c]], [ ], Z, [2, [b,c]]]

yes

?-

The anonymous variable

- Is used when you need to use a variable, but you are not interested in what Prolog instantiates it to
- Each occurrence of the anonymous variable is independent, i.e. can be bound to something different

Anonymous variables

- There is a simpler way of obtaining only the information we want:

?- [ _,X2, _,X4|_ ] = [mia, vincent, marsellus, jody, yolanda].

X2 = vincent

X4 = jody

yes

?-

The underscore is the anonymous variable

Recursing down lists

- The member/2 predicate works by recursively working its way down a list
- doing something to the head, and then
- recursively doing the same thing to the tail

- This technique is very common in Prolog and therefore very important that you master it

Practice (Java and Prolog)

- countElement(+List, +Element, -Num)
- True when there are Num instances of Element in the List.

- reverse(+List1, -List2)
- True when List2 is the reverse of List1

- positives(+List, -Num)
- True when there are Num positive numbers in List

- doubleEach(+List1, -List2)
- True when each number in List2 is double the element in List 1.

Download Presentation

Connecting to Server..