Prolog Nonmonotonic logic Monotonic logic n n Standard

  • Slides: 30
Download presentation
Prolog Nonmonotonic logic

Prolog Nonmonotonic logic

Monotonic logic n n Standard logic is monotonic: once you prove something is true,

Monotonic logic n n 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? 2

Nonmonotonic logic n n Prolog uses nonmonotonic logic Facts and rules can be changed

Nonmonotonic logic n n Prolog uses nonmonotonic logic Facts and rules can be changed at any time n n 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 3

Examples of assert and retract n assert(man(plato)). assert((loves(chuck, X) : - female(X), rich(X))). retract(man(plato)).

Examples of assert and retract n assert(man(plato)). assert((loves(chuck, X) : - female(X), rich(X))). retract(man(plato)). retract((loves(chuck, X) : - female(X), rich(X))). n Notice that we use double parentheses for rules n n n this is to avoid a minor syntax problem assert(foo : - bar, baz). How many arguments did we give to assert? 4

Limitations of backtracking n n In Prolog, backtracking over something generally undoes it Output

Limitations of backtracking n n 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 5

Modeling “real life” n n Real life isn’t monotonic; things change Prolog is superb

Modeling “real life” n n 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 6

Starting Prolog n n n [Macintosh: ~] dave% prolog % library(swi_hooks) compiled into pce_swi_hooks

Starting Prolog n n n [Macintosh: ~] dave% prolog % library(swi_hooks) compiled into pce_swi_hooks 0. 00 sec, 3, 928 bytes Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5. 10. 1) Copyright (c) 1990 -2010 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. ? - consult('C: \_Prolog\dragon. pl'). % C: _Prologdragon. pl compiled 0. 00 sec, 14, 560 bytes Yes 7

Instructions n n ? - start. Enter commands using standard Prolog syntax. Available commands

Instructions n n ? - 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. 8

Starting out n n start. You are in a meadow. To the north mouth

Starting out n n start. You are in a meadow. To the north mouth of a cave; to the south is a building. Your assignment, should accept it, is to recover the famed ruby and return it to this meadow. is the dark small you decide to Bar-Abzad true. 9

Going south n n ? - s. You are in a small building. The

Going south n n ? - 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. 10

Taking things, locked doors n n ? - take(flashlight). OK. true. ? - e.

Taking things, locked doors n n ? - take(flashlight). OK. true. ? - e. The door appears to be locked. You can't go that way. true. 11

Some time later. . . n n ? - use(key). The closet is no

Some time later. . . n n ? - use(key). The closet is no longer locked. true. n n n Later still. . . ? - look. You are in a big, dark cave. The air is fetid. There is a chest here. 12

Essential facts n Where I am at present: n n Where other things are

Essential facts n Where I am at present: n n Where other things are at: n n at(flashlight, building). What I am holding: n n i_am_at(meadow). holding(key). Which facts may be changed: n : - dynamic i_am_at/1, at/2, holding/1. 13

Input and output n Input is unpleasant; we avoid it by giving commands (as

Input and output n Input is unpleasant; we avoid it by giving commands (as questions) directly to Prolog n n 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. 14

The map N W E S cave_entrance cave meadow building closet 15

The map N W E S cave_entrance cave meadow building closet 15

Implementing the map n n n path(cave, w, cave_entrance). path(cave_entrance, e, cave). path(meadow, s,

Implementing the map n n n path(cave, w, cave_entrance). path(cave_entrance, e, cave). path(meadow, s, building). path(building, n, meadow). Could have done this instead: n path(cave, w, cave_entrance). path(X, e, Y) : - path(Y, w, X). 16

listing n listing(predicate) is a good way to examine the current state of the

listing n listing(predicate) is a good way to examine the current state of the program n n ? - listing(at). at(key, cave_entrance). at(flashlight, building). at(sword, closet). true. 17

North, south, east, west n n The commands n, s, e, w all call

North, south, east, west n n The commands n, s, e, w all call go. n : - go(n). s : - go(s). e : - go(e). w : - go(w). 18

Making predicates succeed n ? - go(s). n false. This works, but it isn’t

Making predicates succeed n ? - go(s). n false. This works, but it isn’t very user friendly. Remember: n n n A predicate can consist of more than one clause The clauses will be tried in order n So we can get the following behavior: ? - go(s). You can't go that way. n true. We could further improve this with an explanation, such as, “The door is locked. ”, or, “The guard demands to see your ID. ” n 19

go n n go(Direction) : i_am_at(Here), path(Here, Direction, There), retract(i_am_at(Here)), assert(i_am_at(There)), look. go(_) :

go n n 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. '). 20

take n take(X) : i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(holding(X)), write('OK. '), nl. 21

take n take(X) : i_am_at(Place), at(X, Place), retract(at(X, Place)), assert(holding(X)), write('OK. '), nl. 21

You can’t always take(A) : holding(A), write('You're already holding it!'), nl. take(A) : -

You can’t always 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. 22

Making things fail n n n A predicate will fail if it doesn’t succeed

Making things fail n n n A predicate will fail if it doesn’t succeed You can explicitly use fail works like this: call fail n fail This often isn’t strong enough; it doesn’t force the entire predicate to fail 23

cut n The “cut, ” written ! , is a commit point n n

cut n The “cut, ” written ! , is a commit point n n n 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 call ! exit 24

cut-fail n n n The cut-fail combination: !, fail means really fail It commits

cut-fail n n n 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 25

A locked door n n n path(building, e, closet) : locked(closet), write('The door appears

A locked door n n n 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 26

Dropping objects drop(A) : holding(A), i_am_at(B), retract(holding(A)), assert(at(A, B)), write('OK. '), nl. drop(A) :

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

What else is Prolog good for? n n Prolog is primarily an AI (Artificial

What else is Prolog good for? n n Prolog is primarily an AI (Artificial Intelligence) language It’s second only to LISP in popularity It’s more popular in Britain than in the U. S. Prolog is also a very enjoyable language in which to program (subjective opinion, obviously!) 28

Prolog vs. LISP n Unlike LISP, Prolog provides: n n built-in theorem proving built

Prolog vs. LISP n Unlike LISP, Prolog provides: n n built-in theorem proving built in Definite Clause Grammars, good for parsing natural language If you just want to use these tools, Prolog is arguably better If you want to build your own theorem prover or parser, LISP is clearly better 29

The End 30

The End 30