P. 1
Prolog 01

Prolog 01

|Views: 174|Likes:
Published by Rahul Jain

More info:

Published by: Rahul Jain on May 02, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

08/04/2011

pdf

text

original

Genealogy

Simula
Lisp functional programming objects

Prolog
pattern matching

C syntax C++

Smalltalk

ML

Haskell

Java
Actors

Erlan g

Scala
1

Prolog
Programming in Logic

4-Aug-11

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/

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?

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 Prolog uses backward reasoning -- from (potential) conclusions to facts 

Instead, you ask Prolog specific questions 

Is Socrates mortal? Prolog man(socrates).man(X). All men are mortal. mortal(X) :. ?.Syllogisms in Prolog Syllogism Socrates is a man.mortal(socrates). .

Facts. rules.man(X). man(socrates). mortal(X) :. Queries have the same form as facts . Rule: All men are mortal. Query: Is Socrates mortal? mortal(socrates). and queries        Fact: Socrates is a man.

with a .Running Prolog I    Create your "database" (program) in any editor Save it as text only.pl extension Here's the complete program: man(socrates). mortal(X) :.man(X). .

ask your question at the prompt:  ?.pl file.prompt:  ?. or Double-clicking on the Prolog application and consulting your file at the ?. Yes  Prolog responds:  .  Then.Running Prolog II  Prolog is completely interactive.pl'). Begin by   Double-clicking on your .mortal(socrates).consult('C:\\My Programs\\adv.

No   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).mortal(plato). X = socrates .Prolog is a theorem prover  Prolog's "Yes" means "I can prove it" -Prolog's "No" means "I can't prove it"  ?.

south. Omit the parentheses if there are no arguments Example structures:    sunshine man(socrates) path(garden. sundial) .Syntax I: Structures    A structure consists of a name and zero or more arguments.

loves(mary. Example base clauses:    debug_on. mary). bill).Syntax II: Base Clauses    A base clause is just a structure. terminated with a period. . A base clause represents a simple fact. loves(john.

Syntax III: Nonbase Clauses   A nonbase clause is a structure.man(X). Example nonbase clauses:    :- mortal(X) :. wealthy(X). a turnstile (meaning ³if´).  The comma between structures means ³and´ . and a list of structures. wise(X). happy(X) :.woman(X).healthy(X). mortal(X) :.

Syntax IV: Predicates   A predicate is a collection of clauses with the same functor (name) and arity (number of arguments). bill).female(X). loves(mary. . rich(X). loves(john. mary). X) :. loves(chuck.

Clauses within a predicate are used in the order in which they occur. .Syntax V: Programs    A program is a collection of predicates. Predicates can be in any order.

Syntax VI: Variables and atoms    Variables begin with a capital letter: X. _result Atoms do not begin with a capital letter: x. or beginning with a capital letter. socrates Atoms containing special characters.pl' . Socrates. must be enclosed in single quotes:  'C:\\My Documents\\examples.

pl'  Backslashes in file names must also be doubled:  . a single quote must be doubled or backslashed:  'Can''t.Syntax VII: Strings are atoms  In a quoted atom. or won\'t?' 'C:\\My Documents\\examples.

not a string Don¶t forget the period! (But you can put it on the next line.) . Double quotes indicate a list of ASCII character values.Common problems     Capitalization is meaningful! No space is allowed between a functor and its argument list: man(socrates). not man (socrates).

---------. X). female(jane).  female(X) = female(mary). X) :.Backtracking      loves(chuck.Suppose we ask: loves(chuck.  rich(jane) fails.  rich(mary) succeeds. X = jane. female(mary). rich(X). X = mary.  female(X) = female(jane). rich(mary). .female(X).

fail ports connect to redo ports . exit. redo. fail Exit ports connect to call ports.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.

rich(X).female(X).Calls as nested beads loves(chuck. X) :. loves(chuck. X) call fail female(X) rich(X) exit redo .

female(mary). ?.female(X). X = jane .Additional answers      female(jane). female(susan). X = mary Yes female(X) female(jane) female(mary) female(susan) .

rich(X). first find a female X. Declarative readings are almost always preferred. . X) :. Approximate procedural reading: To find an X that Chuck loves.female(X).Readings     loves(chuck. then check that X is rich. Declarative reading: Chuck loves X if X is female and rich.

Monotonic logic     Standard logic is monotonic: once you prove something is true. we can conclude that the wallet is in the car But what if we take the purse out of the car? . 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.

.) adds a fact or rule retract(...) removes a fact or rule assert and retract are said to be extralogical predicates .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(..

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

nl.Limitations of backtracking     In Prolog. assert. or retract . 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.

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 .

Please visit http://www. and you are welcome to redistribute it under certain conditions.1) Copyright (c) 1990-2010 University of Amsterdam.pl'). % C:\_Prolog\dragon.928 bytes Welcome to SWI-Prolog (Multi-threaded. 14. ?.org for details. 64 bits. VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY.00 sec. Version 5.10.Starting Prolog  [Macintosh:~] dave% prolog % library(swi_hooks) compiled into pce_swi_hooks 0. 3.560 bytes Yes   .pl compiled 0. This is free software.swi-prolog.consult('C:\\_Prolog\\dragon.00 sec.

w. -.start.to use an object.to attack an enemy. -.to start the game. attack. -. s. -. instructions. . look. take(Object). -. halt.to look around you again.to put down an object. Available commands are: start.to pick up an object. e. n. use(Object).to end the game and quit. -. -. -. -.to go in that direction.Instructions   ?.to see this message again. drop(Object). Enter commands using standard Prolog syntax.

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

s. The room is devoid of furniture. You are in a small building. There is a flashlight here. and the only feature seems to be a small door to the east.Going south   ?. Yes . The exit is to the north.

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

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

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

').    write(... we avoid it by giving commands (as questions) directly to Prolog  take(flashlight). nl. .Input and output  Input is unpleasant.) outputs its one argument nl ends the line (writes a newline) describe(closet) :write('You are in an old storage closet.

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

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

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

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

retract(i_am_at(Here)).go   go(Direction) :i_am_at(Here). go(_) :write('You can''t go that way. assert(i_am_at(There)). . There). path(Here.'). Direction. look.

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

').You can't always take take(A) :holding(A). as before). take(A) :. take(A) :write('I don\'t see it here. nl.(actually take something. write('You\'re already holding it!'). . nl.

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

" written ! .cut  The "cut. 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 . is a commit point   It commits to the clause in which it occurs.

then fails This means no other clauses of this predicate will be tried. fail means really fail It commits to this clause. so the predicate as a whole fails .cut-fail    The cut-fail combination: !.

closet) :locked(closet). If the closet door isn't locked. fail. path(building. nl.'). the cut prevents the second clause from ever being reached    ." and the second clause is used If the closet door is locked. the first clause fails "normally.A locked door path(building. e. write('The door appears to be locked. !. closet). e.

Dropping objects drop(A) :holding(A). drop(A) :write('You aren\'t holding it!'). i_am_at(B). B)).'). retract(holding(A)). write('OK. assert(at(A. . nl. nl.

Prolog is also a very enjoyable language in which to program (subjective opinion. obviously!) .What else is Prolog good for?     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.

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

The End .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->