Intro to prolog
This presentation is the property of its rightful owner.
Sponsored Links
1 / 56

Intro to Prolog PowerPoint PPT Presentation


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

Intro to Prolog. 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

Download Presentation

Intro to Prolog

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


Intro to prolog

Intro to Prolog


Conventional vs logic programming

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

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

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

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

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

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

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

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

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

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

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

Example

f(a).

f(b).

g(a).

g(b).

h(b).

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

?- k(Y).


Example search tree

Example: search tree

?- k(Y).

f(a).

f(b).

g(a).

g(b).

h(b).

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

?- k(Y).


Example search tree1

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 tree2

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 tree3

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 tree4

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 tree5

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 tree6

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 tree7

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 tree8

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

Another example

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).


Another example1

Another example

?- jealous(X,Y).

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).


Another example2

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 example3

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 example4

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 example5

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 example6

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 example7

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 example8

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 example9

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

  • 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

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

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

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

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

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

Head and Tail example 1

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


Head and tail example 11

Head and Tail example 1

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


Head and tail example 12

Head and Tail example 1

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


Head and tail example 2

Head and Tail example 2

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


Head and tail example 21

Head and Tail example 2

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


Head and tail example 22

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

Head and Tail example 3

  • [dead(z)] Head: Tail:


Head and tail example 31

Head and Tail example 3

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


Head and tail example 32

Head and Tail example 3

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


Head and tail of empty list

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

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 operator1

The built-in operator |

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

Head = mia

Tail = [vincent,jules,yolanda]

yes

?-


The built in operator2

The built-in operator |

?- [X|Y] = [ ].

no

?-


The built in operator3

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

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

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

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

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.


  • Login