Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.

Similar presentations


Presentation on theme: "1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel."— Presentation transcript:

1 1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel

2 2 Satisfying Goals

3 3 Variables (same as last time) zThey can stand for objects. zA variable is y“Instantiated” if it currently stands for an object. y“Not instantiated” if it doesn’t. zStart with an upper-case letter.

4 4 Questions with Variables (same as last time) zAlso known as “Goals”. zProlog looks for a match. zlikes(john,X). yLogically means: “Is there anything John likes?” yReally means: “Is there an X such that this is true?”

5 5 What It Means to Prolog z“Now if an uninstantiated variable appears in an argument, Prolog will allow that argument to match any other argument in the same position in the fact.” – C&M p. 9

6 6 Example zFile ‘likes.pl’: likes(john,mary). likes(john,gold). likes(mary,john). likes( john, football). likes(john,mary). zQuestions & Answers: ?- likes(john,X). X = mary [I hit RETURN here] Yes ?-

7 7 How It Works zProlog searches top-to-bottom through the database. zWhen it finds a match (“satisfies the goal”), X is instantiated to mary. zProlog marks the place in the database. zProlog prints all objects that variables now stand for. zYou hit RETURN—you’re done!

8 8 Continuing Instead ?- likes(john,X). [I type this] X = mary [I type ; ] X = gold [I type ; ] X = football [I type ; ] X = mary [I type ; ] No [No more matches] ?-

9 9 How This Case Works zProlog searches top-to-bottom through the database. zWhen it finds a match, X is instantiated to mary. zProlog marks the place in the database. zProlog prints all objects that variables now stand for. zYou hit semicolon—Prolog attempts to re-satisfy the goal, starting where it left off (not from the beginning).

10 10 What’s Happening ?- likes(john,X). [At this point, X is not instantiated] X = mary ; [X is instantiated to mary] X = gold ; [X is instantiated to gold] X = football ; [etc.] X = mary ; [etc.] No [No more matches] ?-

11 11 One More Time: Satisfy vs. Re-Satisfy zWhen Prolog tries to satisfy a goal, it starts from the top of the database, looking for a match. zWhen Prolog tries to re-satisfy a goal, it starts looking in the database from where it left off (i.e. just after the last match of that goal).

12 12 Conjunctions zProlog uses a comma to indicate AND. zTo get OR, for now just use separate goals or rules. yCould also use semicolon.

13 13 Conjunctions: AND zDo John and Mary like each other? ?- likes(john,mary), likes(mary,john). Yes ?- likes(john,gold), likes(gold,john). No zProlog tries to satisfy each goal. yEach of these examples has two goals.

14 14 Satisfying Multiple Goals I zWhat if the multiple goals involve variables? y?- likes( mary, X), likes(john, X). yFor the goal to be satisfied, the variable X must be instantiated to the same value in each goal. yIn other words, is there one object X that is liked by both Mary and John?

15 15 Satisfying Multiple Goals II zProlog tries to satisfy the first goal. zIf first goal is satisfied, Prolog marks the place and tries to satisfy the second goal. zIf second goal becomes satisfied, Prolog also marks that place. yEach goal has its own place marker. zWhat if the second goal is not satisfied? yNot satisfied = no match OR user says to try again. yMust “backtrack”.

16 16 Backtracking

17 17 Backtracking zWhen the second goal fails, uninstantiate X, then try to re-satisfy the first goal. yStart where you left off on the first goal. zIf the first goal gets re-satisfied, try to satisfy the second goal. yNot re-satisfy. yStart matching second goal from the beginning. zOrder is very important. yProlog always tries to satisfy goals left-to-right.

18 18 Backtracking Exercise

19 19 Rules

20 20 Rules zRules are general statements about objects and their relationships. zExample: yJohn likes everyone ySame as: John likes every object, if the object is a person. zAnother example: yJohn likes any woman who likes wine. ySame as: John likes X if X is a woman and X likes wine.

21 21 Rule Syntax zHead :- Body. z“:-” (colon, hyphen) pronounced “if”. zHead is the thing that is true if the Body is true.

22 22 Rule Example zSame example: yJohn likes any woman who likes wine. ySame as: John likes X if X is a woman and X likes wine. ylikes(john,X) :- likes(X,wine),female(X). yDon’t forget the dot!

23 23 Scope zlikes(john,X) :- likes(X,wine),female(X). zWhen X becomes instantiated, all X’s are instantiated within the scope of X. yIn this case, within the same rule.

24 24 Another Example male(albert). male(edward). female(alice). female(victoria). parents(edward, albert, victoria). parents(alice, albert, victoria). zNote: parents(child, father, mother). z Rule to test if X is the sister of Y: sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). z How to write are_sisters() ?

25 25 Clauses z2 ways to give Prolog information about predicates: yFacts yRules zThese are called clauses.

26 26 Prolog Syntax: Comments and Integers zComments like C or PL/I: y/* your comment here! */ zIntegers ySome versions of Prolog do not allow negative numbers. xSWI-Prolog does. xIt even allows non-integers.

27 27 Prolog Syntax: Atoms zAtoms (objects and predicates) must be lower case. yCan include digits and underscores (but cannot start with them). yObjects can be enclosed in single quotes.

28 28 Prolog Syntax: Variables zVariables must start with upper case or underscore (“_”). zSingle underscore is the anonymous variable. yDo not have to match each other, even in the same clause.

29 29 Structures a.k.a. Compound Terms zMade up of “functor” and “components”. zEach component acts as an object. zExamples: yowns(john,book(wuthering_heights,bronte)). yowns(john,book(wuthering_heights, author(emily,bronte))).

30 30 File ‘books.pl’ owns(john, book(wuthering_heights, author(emily, bronte))). owns(john, book(jane_eyre, author(charlotte, bronte))). owns(mary, book(wuthering_heights, author(emily, bronte))). owns(john, book(the_professor, author(charlotte, bronte))). owns(john, book(the_invisible_man, author(h_g, wells))). owns(mary, book(invisible_man, author(ralph, ellison))).

31 31 Structures and Goals zWhat do these test for? yowns(john,X). yowns(john,book(X, author(Y,bronte))). yowns(X,Y),owns(Z,Y).

32 32 Equality zEquals: “=“ zNot Equals: “\=“ zCan we eliminate duplicate owners from the last slide?

33 33 Next Time zMore Prolog yStructures yOperators yComplicated rules


Download ppt "1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel."

Similar presentations


Ads by Google