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

Intro to Prolog

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

Intro to Prolog

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

- 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

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

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

- Doll is a toy.
isToy(doll).

- Snoopy is a toy.
isToy(snoopy).

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

- It’s Tuesday.
tuesday.

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

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

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

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

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

- 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

f(a).

f(b).

g(a).

g(b).

h(b).

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

?- k(Y).

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

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

?- k(Y).

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

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

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

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

†

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

†

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

†

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

†

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

†

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).

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

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

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

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

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

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

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

?- 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 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, <, =<, >, >=, =:=

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

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

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

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

- 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

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

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

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

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

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

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

- [dead(z)] Head: Tail:

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

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

- 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

- 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

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

Head = mia

Tail = [vincent,jules,yolanda]

yes

?-

?- [X|Y] = [ ].

no

?-

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

?-

- 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

- 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

- 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

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