Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Getting Starting with Prolog using Amzi!

o Install AMZI! Using the exe file (v. 7. ! un"er le#tures$ start$ sele#t %xit&'un (ree version (to) right! o Sele#t *+ew,Pro-e#t,Prolog Pro-e#t. to start a Pro-e#t o /hoose *(ile,+ew,(ile. an" #o)0 an" )aste 0our gene.)ro (or other )rolog file! or write sam)le senten#es into the I1% main win"ow (top-middle part of the
screen)

o /hoose )ro-e#t name an" sele#t or *'un an Inter)rete" Pro-e#t. (whi#h automati#all0 #alls the listener an" #onsults the .)ro files in 0our )ro-e#t! or sele#t a single file an" #hoose *'un as Single Prolog (ile. o 20)e in 0our 3ueries or a"" new fa#ts (e.g. *)erson(0asin!..!.

o 20)e *3uit.. in the 4istener win"ow (lower half of your screen) or hit the re" 5utton on u))er,right of 4istener win"ow

About this document


The rest of this document has been summarized from the Tutorial, The Adventure in Prolog from the Amzi! Documentation. Please refer to this document if you need more detail about Prolog. http !!""".amzi.com Some of the following sections replicate content of the lecture (prolog.ppt), but other parts get into details and the Amzi environment. Authors #asin #ilmaz, yyilmaz$su.sabanciuniv.edu. %odified by &errin #ani'oglu. Date (ritten in )**), modified latest in )**+

Getting Started with Prolog


,irst "e "ill desing a program that "ill test if a given sub-ect is mortal or not based on a number of relations. .ur rules are li'e this /f a sub-ect is male, or female, it is a person, and if it is a person it is mortal. 0iven the correct rules 1"e "ill come into that2, the program output "ill be ?- mortal(yasin). yes ?- mortal(aliye). yes ?- mortal(araba). No .pen the Amzi! and from the file menu select ne" file option. 3nter the follo"ing code to the opened child "indo" mortal(X) :- person(X). person(X) :- female(X). person(X) :- male(X). male(yasin). female(aliye). male(yusuf). And select the file from the file4save as menu to a file as mortal.pro. 5o" you can enter more facts or ma'e inferences using the Prolog Listener. 6tart the 7istener using either the 7istener!6tart menu item, 89trl/:, or the A7/6 toolbar button. #ou should see the typical listener prompt. ?3ntering the source code in the 7istener is called consulting. 6elect 7istener!9onsult from the main menu, and select ;mortal.pro; from the file menu. This enters the rules and facts in in your program into the 'no"ledge base. #ou can also consult a Prolog source file directly from the listener prompt li'e this. ?- consult(mortal). Yes 5o" "e can test the program inside the 7istener "ith prolog <ueries ?- mortal(araba). No ?- mortal(yasin). Yes

/f you change your source code you have to reconsult it inside the listener. (ith reconsult command or by using the 7istener4reconsult menu option.

Logic Programming
7et;s loo' at the simple e=ample in more detail. /n classical logic "e might say >All people are mortal,> or, rephrased for Prolog, >,or all ?, ? is mortal if ? is a person.> mortal(X) :- person(X). A fact, is asserted in a similar "ay male(yasin). (e could also as' >(ho is mortal@> li'e this ?- mortal(X). and receive the response ?- mortal(X). X = aliye To print out all mortals, "e can add a mortalAreport function to our mortal.pro file mortal_report:write('Known mortals are:') nl mortal(X) write(X) nl fail. (hen "e reconsult and "rite ?- mortal_report. The output "ill be Known mortals are: aliye yasin yusuf no.

The relations and other stuff "e have "ritten in our mortal.pro are all called predicates in prolog -argon. Prolog programs are composed of these predicates. An ;if = is then y; relation is a predicate, and even a print command is e=pressed in the same format as sho"n in the above e=ample. Predicates are composed of clauses. A clause can be either a fact or a rule. An e=ample fact "ill be male(yasin). A rule e=ample is person(X) :- male(X).

,acts are the simplest form of Prolog predicates, and are similar to records in a relational database. As "e "ill see they can be <ueried li'e database records. ,or e=ample the follo"ing facts define a simple family of yusuf, aliye, and yasin. male(yasin). female(aliye). male(yusuf). parent(aliye yusuf). parent(yasin yusuf). The first three fact is li'e defining t"o databases 1male, female2 "ith table of one field "ith some entries 1B in female, ) in male2. The Cth and Dth lines are for defining a third database that defines a relation on previous databases. /t is very easy to implement such relation tables in prolog. Eust thin' ho" many lines of code it "ill re<uire for a 9 program to do the same -ob. To <uery your family logicbase base, enter parent(X yusuf). The output is X = aliye Press ; X = yasin (e got the parents of yusuf. The <ueries can be much more functional. ,or e=ample they can be compined 7ets get the father of yusuf. Fe must be a parent of #usuf, and must be male.... 6o <uery li'e this ?- parent(X yusuf) male(X). X = yasin n Prolog, !," corresponds to ! " and !;" corresponds to te ! ".

#ules
(e said earlier a predicate is defined by clauses, "hich may be facts or rules. A rule is no more than a stored <uery. /ts synta= is !ea" :- bo"y. "here head 4 body is a predicate definition is the nec$ symbol, sometimes read as >if> one or more goals 1a <uery2 ,or e=ample lets define a rule for a father in the .pro file fat!er(X Y) :- parent(X Y) male(X). 6o to <uery father of yusuf, reconsult your source code and type ?- fat!er(# yusuf). .utput is # = yasin

Managing Data
(e have created our 'no"ledge base in the program source code. &ut it is possible to insert ne" predicates to the program "hile it is running. 3.g., "e may "ant to add a ne" child to this family... %anaging the logicbase on runtime is done through the follo"ing commands asserta1?2 Adds the clause ? as the first clause for its predicate. 7i'e the other /!. predicates, it al"ays fails on bac'trac'ing and does not undo its "or'. assertz1?2 6ame as asserta, only it adds the clause ? as the last clause for its predicate. retract1?2 Gemoves the clause ? from the 'no"ledgebase, again "ith a permanent effect that is not undone on bac'trac'ing. The follo"ing e=ample adds a daughter to the family ?- assert$(female(elif)). yes ?- assert$(parent(aliye elif)). yes ?- assert$(parent(yasin elif)). Yes 6ho" us the children of yasin ?- parent(yasin X). X = yusuf %press a &ey' X = elif %ost of the topics and e=amples are covered in the tutorial of the Amzi!. And the program setup file includes a big family relationship e=ample prolog code that uses the synta= represented here. The e=ample file is located in 8Amzi! installation dir:!samples!prolog!gene.pro /f you scroll do"n the source code, you "ill see a bunch of rules that defines the family relationships. The data entries are manipulated by functions such as addAperson15ame,0ender,%other,,ather,6pouse2

Then you can <uery on the people in the family. An e=ample usage
?- consult('(:))*ocuments
an" +ettin,s))yyilma$))-el,elerim))+abanci)).rolo,)),ene.pro')

yes ?- a""_person(yasin male mot!er/ fat!er/ aliye). yes ?- a""_person(aliye female mot!er0 fat!er0 yasin). yes

?- a""_person(yusuf male aliye yasin $eynep). yes ?- a""_person(elif female aliye yasin a!met). yes ?After "e add C people in the family "e can <uery on them ?- mot!er(X yusuf). X = aliye ?- brot!er(X elif). X = yusuf ?- nep!ew(X yusuf). No etc...

Reference
Amzi! Documentation 8your Amzi! /nstallation dir:HdocsHaipH

You might also like