Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek,

Similar presentations


Presentation on theme: "1 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek,"— Presentation transcript:

1 1 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt CS 8520: Artificial Intelligence Prolog 1 Paula Matuszek Spring, 2013

2 2 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free Downloadable from: http://www.swi- prolog.org/http://www.swi- prolog.org

3 3 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syllogisms “Prolog” is all about programming in logic. Aristotle described syllogisms 2300 years ago Sample syllogism: –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal. This is logic. Can Prolog do it?

4 4 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Forward and backward reasoning A syllogism gives two premises, then asks, "What can we conclude?" –This is forward reasoning -- from premises to conclusions –it's inefficient when you have lots of premises Instead, you ask Prolog specific questions –Prolog uses backward reasoning -- from (potential) conclusions to facts

5 5 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syllogisms in Prolog Syllogism Socrates is a man. All men are mortal. Is Socrates mortal? man(socrates). mortal(X) :- man(X). ?- mortal(socrates). Prolog

6 6 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Facts, rules, and queries Fact: Socrates is a man. man(socrates). Rule: All men are mortal. mortal(X) :- man(X). Query: Is Socrates mortal? mortal(socrates). Queries have the same form as facts

7 7 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Running Prolog I Create your "database" (program) in any editor Save it as text only, with a.pl extension Here's the complete program: man(socrates). mortal(X) :- man(X).

8 8 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Running Prolog II Prolog is completely interactive. Begin by –Double-clicking on your.pl file, or –Double-clicking on the Prolog application and consulting your file at the ?- prompt: ?- consult('C:\\My Programs\\adv.pl'). –On a mac, opening a terminal window and typing swipl Then, ask your question at the prompt: –?- mortal(socrates). Prolog responds: –Yes

9 9 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Prolog is a theorem prover Prolog's "Yes" or “true” means "I can prove it" -- Prolog's "No" or “false” means "I can't prove it" –?- mortal(plato). False. This is the closed world assumption: the Prolog program knows everything it needs to know Prolog supplies values for variables when it can –?- mortal(X). X = socrates

10 10 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax I: Structures A structure consists of a name and zero or more arguments. Omit the parentheses if there are no arguments Example structures: –sunshine –man(socrates) –path(garden, south, sundial)

11 11 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax II: Base Clauses A base clause is just a structure. A base clause represents a simple fact. Example base clauses: –debug_on. –loves(john, mary). –loves(mary, bill).

12 12 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax III: Nonbase Clauses A nonbase clause is a structure, a turnstile (meaning IF), and a list of structures. Example nonbase clauses: –mortal(X) :- man(X). –mortal(X) :- woman(X) –happy(X) :- healthy(X), wealthy(X), wise(X). The comma between structures means AND

13 13 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax IV: Predicates A predicate is a collection of clauses with the same functor (name) and arity (number of arguments). loves(john, mary). loves(mary, bill). loves(chuck, X) :- female(X), rich(X).

14 14 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax V: Programs A program is a collection of predicates. Predicates can be in any order. Clauses within a predicate are used in the order in which they occur.

15 15 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax VI: Variables and atoms Variables begin with a capital letter: X, Socrates, _result Atoms do not begin with a capital letter: x, socrates Atoms containing special characters, or beginning with a capital letter, must be enclosed in single quotes: –'C:\\My Documents\\examples.pl' Variables consisting of, or beginning with, _ are called anonymous variables. They are used when we don’t care what their value is.

16 16 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Syntax VII: Strings are atoms In a quoted atom, a single quote must be doubled or backslashed: –'Can''t, or won\'t?' Backslashes in file names must also be doubled: –'C:\\My Documents\\examples.pl'

17 17 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Common problems Capitalization is extremely important! No space is allowed between a functor and its argument list: man(socrates), not man (socrates). Double quotes indicate a list of ASCII character values, not a string Don’t forget the period! (But you can put it on the next line.)

18 18 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Backtracking loves(chuck, X) :- female(X), rich(X). female(jane). female(mary). rich(mary). Suppose we ask: loves(chuck, X). –female(X) = female(jane), X = jane. –rich(jane) fails. –female(X) = female(mary), X = mary. –rich(mary) succeeds.

19 19 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Backtracking and Beads Each Prolog call is like a “bead” in a string of beads: call fail exit redo Each structure has four ports: call, exit, redo, fail Exit ports connect to call ports; fail ports connect to redo ports

20 20 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Calls as nested beads loves(chuck, X) :- female(X), rich(X). loves(chuck, X) female(X)rich(X) call fail exit redo

21 21 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Additional answers female(jane). female(mary). female(susan). ?- female(X). X = jane ; X = mary Yes female(jane) female(mary) female(susan) female(X)

22 22 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Readings loves(chuck, X) :- female(X), rich(X). Declarative reading: Chuck loves X if X is female and rich. Approximate procedural reading: To find an X that Chuck loves, first find a female X, then check that X is rich. Declarative readings are almost always preferred.

23 23 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Monotonic logic Standard logic is monotonic: once you prove something is true, it is true forever Logic isn't a good fit to reality If the wallet is in the purse, and the purse in is the car, we can conclude that the wallet is in the car But what if we take the purse out of the car?

24 24 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Nonmonotonic logic Prolog uses nonmonotonic logic Facts and rules can be changed at any time –such facts and rules are said to be dynamic assert(...) adds a fact or rule retract(...) removes a fact or rule assert and retract are said to be extralogical predicates

25 25 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Examples of assert and retract assert(man(plato)). assert((loves(chuck,X) :- female(X), rich(X))). retract(man(plato)). retract((loves(chuck,X) :- female(X), rich(X))). Notice that we use double parentheses for rules –this is to avoid a minor syntax problem –assert(foo :- bar, baz). –How many arguments did we give to assert?

26 26 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Limitations of backtracking In Prolog, backtracking over something generally undoes it Output can't be undone by backtracking Neither can assert and retract be undone by backtracking Perform any necessary testing before you use write, nl, assert, or retract

27 27 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Modeling "real life" Real life isn't monotonic; things change Prolog is superb for modeling change Games are often a model of real (or fantasy!) life Prolog is just about ideal for adventure games

28 28 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Trying an Adventure Game There is a file on the class webpage called dragon.pl. Download it someplace you can find it. Start Prolog. Depending on your system, you may be able to click on dragon.pl, or there may be an icon, or you may have to get to a prompt and run it directly. For the mac, open a terminal window –paladin.home% swipl You may also need to set a path variable or make a link to where you have the executable.

29 29 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Starting Prolog Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.2.6) Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details.http://www.swi-prolog.org For help, use ?- help(Topic). or ?- apropos(Word). ?- ?- consult('C:\\_Prolog\\dragon.pl'). % C:\_Prolog\dragon.pl compiled 0.00 sec, 54 clauses true. ?-

30 30 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Instructions ?- start. Enter commands using standard Prolog syntax. Available commands are: start. -- to start the game. n. s. e. w. -- to go in that direction. take(Object). -- to pick up an object. drop(Object). -- to put down an object. use(Object). -- to use an object. attack. -- to attack an enemy. look. -- to look around you again. instructions. -- to see this message again. halt. -- to end the game and quit. true. ?-

31 31 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Starting out You are in a meadow. To the north is the dark mouth of a cave; to the south is a small building. Your assignment, should you decide to accept it, is to recover the famed Bar-Abzad ruby and return it to this meadow. true.

32 32 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Going south ?- s. You are in a small building. The exit is to the north. The room is devoid of furniture, and the only feature seems to be a small door to the east. There is a flashlight here. true.

33 33 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Taking things, locked doors ?- take(flashlight). OK. true. ?- e. The door appears to be locked. You can't go that way. true.

34 34 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Some time later... ?- use(key). The closet is no longer locked. true. Later still... ?- look. You are in a big, dark cave. The air is fetid. There is a chest here.

35 35 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Input and output Input is unpleasant; we avoid it by giving commands (as questions) directly to Prolog –take(flashlight). write(...) outputs its one argument nl ends the line (writes a newline) describe(closet) :- write('You are in an old storage closet.'), nl.

36 36 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Implementing an Adventure Game If you examine the code for dragon.pl you can see how it is implemented. The following describe some of the decisions for implementation. Note that these are mostly descriptions of what is true or what something means. There is no control structure comparable to a language like Java or C.

37 37 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Essential facts Where I am at present: –i_am_at(meadow). Where other things are at: –at(flashlight, building). What I am holding: –holding(key). Which facts may be changed: –:- dynamic i_am_at/1, at/2, holding/1. –Not all implementations of Prolog require dynamic, but swipl does.

38 38 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt The map cave_entrance cave meadow buildingcloset N W E S

39 39 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Implementing the map path(cave, w, cave_entrance). path(cave_entrance, e, cave). path(meadow, s, building). path(building, n, meadow). Could have done this instead: –path(cave, w, cave_entrance). path(X, e, Y) :- path(Y, w, X).

40 40 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt listing listing(predicate) is a good way to examine the current state of the program ?- listing(at). –at(key, cave_entrance). at(flashlight, building). at(sword, closet). true

41 41 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt North, south, east, west The commands n, s, e, w all call go. n :- go(n). s :- go(s). e :- go(e). w :- go(w).

42 42 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt go go(Direction) :- i_am_at(Here), path(Here, Direction, There), retract(i_am_at(Here)), assert(i_am_at(There)), look. go(_) :- write('You can''t go that way.').

43 43 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt take take(X) :- i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(holding(X)), write('OK.'), nl.

44 44 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt You can't always take take(A) :- holding(A), write('You\'re already holding it!'), nl. take(A) :- (actually take something, as before). take(A) :- write('I don\'t see it here.'), nl.

45 45 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Making things fail A predicate will fail if it doesn't succeed You can explicitly use fail fail works like this: This often isn't strong enough; it doesn't force the entire predicate to fail fail call fail

46 46 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt cut The "cut," written !, is a commit point –It commits to the clause in which it occurs, and –everything before it in that clause Using cut says: Don't try any other clauses, and don't backtrack past the cut ! callexit

47 47 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt cut-fail The cut-fail combination: !, fail means really fail It commits to this clause, then fails This means no other clauses of this predicate will be tried, so the predicate as a whole fails

48 48 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt A locked door path(building, e, closet) :- locked(closet), write('The door appears to be locked.'), nl, !, fail. path(building, e, closet). If the closet door isn't locked, the first clause fails "normally," and the second clause is used If the closet door is locked, the cut prevents the second clause from ever being reached

49 49 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Dropping objects drop(A) :- holding(A), i_am_at(B), retract(holding(A)), assert(at(A, B)), write('OK.'), nl. drop(A) :- write('You aren\'t holding it!'), nl.

50 50 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like equality in algebra Unification is mostly like pattern matching Example: –loves(john, X) can unify with loves(john, mary) –and in the process, X gets unified with mary

51 51 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Unification I Any value can be unified with itself. –weather(sunny) = weather(sunny) A variable can be unified with another variable. –X = Y A variable can be unified with (“instantiated to”) any Prolog value. –Topic = weather(sunny)

52 52 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Unification II Two different structures can be unified if their constituents can be unified. –female(X) = female(jane) –mother(mary, X) = mother(Y, father(Z)) A variable can be unified with a structure containing that same variable. This is usually a Bad Idea. –X = f(X)

53 53 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Unification Once a variable has been unified with a value, it continues to have that value for the rest of the clause, and can be used that way If we have –female(X) = female(jane) then –write(X) will write jane.

54 54 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Scope of Names The scope of a variable is the single clause in which it appears. The scope of the “anonymous” (“don't care”) variable, _, is itself. –loves(_, _) = loves(john, mary) A variable that only occurs once in a clause is a useless singleton; you should replace it with the anonymous variable

55 55 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Writing Prolog Programs Suppose the database contains loves(chuck, X) :- female(X), rich(X). female(jane). and we ask who Chuck loves, ?- loves(chuck, Woman). female(X) finds a value for X, say, jane rich(X) then tests whether Jane is rich

56 56 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Clauses as Cases A predicate consists of multiple clauses, each of which represents a “case” grandson(X,Y) :- son(X,Z), son(Z,Y). grandson(X,Y) :- son(X,Z), daughter(Z,Y). abs(X, Y) :- X < 0, Y is -X. abs(X, X) :- X >= 0.

57 57 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Ordering Clauses are always tried in order buy(X) :- good(X). buy(X) :- cheap(X). cheap(‘Java 2 Complete’). good(‘Thinking in Java’). What will buy(X) choose first?

58 58 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Ordering II Try to handle more specific cases (those having more variables instantiated) first. dislikes(john, bill). dislikes(john, X) :- rich(X). dislikes(X, Y) :- loves(X, Z), loves(Z, Y).

59 59 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Ordering III Some "actions" cannot be undone by backtracking over them: –write, nl, assert, retract, consult Do tests before you do undoable actions: –take(Thing) :- holding(Thing), write('You\'re already holding it!'), nl.

60 60 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Basic and derived clauses You can often choose which facts you want to be "basic" and which derived son(isaac, steven). child(X, Y) :- son(X, Y). male(isaac). child(isaac, steven). son(X, Y) :- male(X), child(X, Y).

61 61 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Arithmetic The equals sign, =, means “unify.” 2+2 does not unify with 4. To force arithmetic to be performed, use “is”: X is 2 + 2, X = 4. Comparisons =:= =/= > >= < <= also force their operands to be evaluated. + - * / mod, when evaluated, have their usual meanings.

62 62 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt What else is Prolog good for? Prolog is primarily an AI (Artificial Intelligence) language It's more popular in Europe, especially Britain, than in the U.S. Prolog is also a very enjoyable language in which to program (subjective opinion, obviously!) More next week!

63 63 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek, http://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppthttp://www.cis.upenn.edu/~matuszek/cis554-2012/Lectures/prolog-01.ppt Some Resources These slides and dragon.pl were borrowed from David Matuszek. He has additional Prolog information at http://www.cis.upenn.edu/~matuszek/cis554- 2012/index.html http://www.cis.upenn.edu/~matuszek/cis554- 2012/index.html There is a good basic interactive Prolog tutorial at http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prol ogpages/. If you are having problems I’d suggest working through that. http://www.doc.gold.ac.uk/~mas02gw/prolog_tutorial/prol ogpages/ The SWI Prolog site (http://www.swi-prolog.org) has a manual, a FAQ, a mailing list, and other resources.http://www.swi-prolog.org Or try asking a question on our Piazza page! https://piazza.com/villanova/spring2013/8520/home https://piazza.com/villanova/spring2013/8520/home


Download ppt "1 CSC 8520 Spring 2013. Paula Matuszek Slides taken from David Matuszek,"

Similar presentations


Ads by Google