2 Monotonic logicStandard logic is monotonic: once you prove something is true, it is true foreverLogic isn’t a good fit to realityIf the wallet is in the purse, and the purse in is the car, we can conclude that the wallet is in the carBut what if we take the purse out of the car?
3 Nonmonotonic logic Prolog uses nonmonotonic logic Facts and rules can be changed at any timesuch facts and rules are said to be dynamicassert(...) adds a fact or ruleretract(...) removes a fact or ruleassert and retract are said to be extralogical predicates
4 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 rulesthis is to avoid a minor syntax problemassert(foo :- bar, baz).How many arguments did we give to assert?
5 Marking clauses as “Dynamic” Standard Prolog allows you to assert and retract clauses without any restrictions.SWI-Prolog and some others require you to mark variable clauses as “dynamic.”:- dynamic i_am_at/1, at/2, alive/0.The “:-” at the beginning says “do it now.”
6 Solving problems with dynamic If Prolog already knows a clause, and it’s static, it’s too late to mark it dynamicProlog must see :- dynamic functor/arity before it sees any clauses of functor/arity.This includes clauses loaded in from an earlier consultYou can restart SWI-Prolog, or……you can use abolish(functor, arity)
7 Arithmetic The equals sign, =, means “unify.” 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.
8 Limitations of backtracking In Prolog, backtracking over something generally undoes itOutput can’t be undone by backtrackingNeither can assert and retract be undone by backtrackingPerform any necessary testing before you use write, nl, assert, or retract
9 Modeling “real life” Real life isn’t monotonic; things change Prolog is superb for modeling changeGames are often a model of real (or fantasy!) lifeProlog is just about ideal for adventure games
10 Starting Prolog[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 ) Copyright (c) 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 for details.?- consult('C:\\_Prolog\\dragon.pl').% C:\_Prolog\dragon.pl compiled 0.00 sec, 14,560 bytes Yes
11 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.
12 Starting outstart.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.
13 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.
14 Taking things, locked doors ?- take(flashlight).OK. true.?- e.The door appears to be locked. You can't go that way. true.
15 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.
16 Essential facts Where I am at present: Where other things are at: 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.
17 Input and outputInput is unpleasant; we avoid it by giving commands (as questions) directly to Prologtake(flashlight).write(...) outputs its one argumentnl ends the line (writes a newline)describe(closet) :- write('You are in an old storage closet.'), nl.
18 The mapcave_entrancecavemeadowbuildingclosetN W E S
19 Implementing the map Could have done this instead: 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).
20 listinglisting(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.
21 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).
22 Making predicates succeed ?- go(s). false.This works, but it isn’t very user friendly.Remember:A predicate can consist of more than one clauseThe clauses will be tried in orderSo we can get the following behavior:?- go(s). You can't go that way. true.We could further improve this with an explanation, such as, “The door is locked.”, or, “The guard demands to see your ID.”
23 gogo(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.').
25 You can’t always take take(A) :- holding(A), write('You\'re already holding it!'), nl.take(A) :- (actually take something, as before).write('I don\'t see it here.'), nl.
26 Making things fail fail A predicate will fail if it doesn’t succeedYou can explicitly use failfail works like this:failcallThis often isn’t strong enough; it doesn’t force the entire predicate to fail
27 cut ! The “cut,” written ! , is a commit point It commits to the clause in which it occurs, andeverything before it in that clauseUsing cut says: Don’t try any other clauses, and don’t backtrack past the cut!callexit
28 cut-fail The cut-fail combination: !, fail means really fail It commits to this clause, then failsThis means no other clauses of this predicate will be tried, so the predicate as a whole fails
29 A locked doorpath(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 usedIf the closet door is locked, the cut prevents the second clause from ever being reached
31 What else is Prolog good for? Prolog is primarily an AI (Artificial Intelligence) languageIt’s second only to LISP in popularityIt’s more popular in Britain than in the U.S.Prolog is also a very enjoyable language in which to program (subjective opinion, obviously!)
32 Prolog vs. LISP Unlike LISP, Prolog provides: built-in theorem provingbuilt in Definite Clause Grammars, good for parsing natural languageIf you just want to use these tools, Prolog is arguably betterIf you want to build your own theorem prover or parser, LISP is clearly better
Your consent to our cookies if you continue to use this website.