1 / 56

Intro to Prolog

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to Prolog

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

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

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

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

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

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

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

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

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

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

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

  13. Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

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

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

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

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

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

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

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

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

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

  23. Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

  24. Another example ?- jealous(X,Y). loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

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

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

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

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

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

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

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

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

  33. 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, <, =<, >, >=, =:=

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

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

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

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

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

  39. Head and Tail example 1 • [mia, vincent, jules, yolanda]Head: Tail:

  40. Head and Tail example 1 • [mia, vincent, jules, yolanda]Head: miaTail:

  41. Head and Tail example 1 • [mia, vincent, jules, yolanda]Head: miaTail: [vincent, jules, yolanda]

  42. Head and Tail example 2 • [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: Tail:

  43. Head and Tail example 2 • [[ ], dead(z), [2, [b,c]], [ ], Z, [2, [b,c]]] Head: [ ]Tail:

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

  45. Head and Tail example 3 • [dead(z)] Head: Tail:

  46. Head and Tail example 3 • [dead(z)] Head: dead(z) Tail:

  47. Head and Tail example 3 • [dead(z)] Head: dead(z) Tail: [ ]

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

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

  50. The built-in operator | ?- [Head|Tail] = [mia, vincent, jules, yolanda]. Head = mia Tail = [vincent,jules,yolanda] yes ?-

More Related