Getting Starting with Prolog using Amzi!

o Download, install, start, select Exit/Run Free version (top right) o Select “New-Project-Prolog Project” to start a Project o Choose “File-New-File” and copy and paste your (or other prolog file) or write sample sentences into the IDE main window (top-middle part of the

o Choose project name and select or “Rus an Interpreted Project” (which automatically calls the listener and consults the .pro files in your project) or select a single file and choose “Run as Single Prolog File” o Type in your queries or add new facts (e.g. “person(yasin).”).

o Type “quit.” in the Listener window (lower half of your screen) or hit the red button on upper-right of Listener window

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. Some of the following sections replicate content of the lecture (prolog.ppt), but other parts get into details and the Amzi environment. Authors: Yasin Yilmaz, Modified by Berrin Yanikoglu. Date: Written in 2002, modified latest in 2007

pro. ?. the program output will be: ?. yes ?. And select the file from the file-save as menu to a file as mortal. Yes .mortal(araba).female(X). person(X) :. male(yusuf).consult(mortal). Start the Listener using either the Listener/Start menu item. or female. This enters the rules and facts in in your program into the knowledge base. Now you can enter more facts or make inferences using the Prolog Listener. male(yasin). or the ALIS toolbar button. You should see the typical listener prompt. No Open the Amzi! and from the file menu select new file option.Getting Started with Prolog First we will desing a program that will test if a given subject is mortal or not based on a number of relations. person(X) :. Yes Now we can test the program inside the Listener with prolog queries: ?. [CtrlI]. it is a person. No ?. You can also consult a Prolog source file directly from the listener prompt like this. Our rules are like this: If a subject is male.person(X).mortal(araba). and if it is a person it is mortal. Select Listener/Consult from the main menu.mortal(yasin).mortal(yasin). yes ?. female(aliye). Given the correct rules (we will come into that).male(X). ?Entering the source code in the Listener is called' from the file menu. Enter the following code to the opened child window: mortal(X) :. and select 'mortal.mortal(aliye).

X = aliye 1. "For all X. mortal(X).mortal(X). we can add a mortal_report function to our mortal.mortal_report. fail.If you change your source code you have to reconsult it inside the file: mortal_report:write('Known mortals are:'). is asserted in a similar way: male(yasin). With reconsult command or by using the Listener-reconsult menu option. An example fact will be: male(yasin). We could also ask "Who is mortal?" like this ?." mortal(X) In classical logic we might say "All people are mortal. The output will be: Known mortals are: aliye yasin yusuf no. To print out all mortals.mortal(X). write(X). When we reconsult and write: ?. rephrased for Prolog." or. . and even a print command is expressed in the same format as shown in the above example. Logic Programming Let's look at the simple example in more detail.male(X).person(X). Predicates are composed of clauses. X is mortal if X is a person. A clause can be either a fact or a rule. A rule example is: person(X) are all called predicates in prolog An 'if x is then y' relation is a predicate. Prolog programs are composed of these predicates. A fact. and receive the response ?. The relations and other stuff we have written in our mortal.

yusuf). and are similar to records in a relational database. where head :body is a predicate definition is the neck symbol.. parent(aliye. Just think how many lines of code it will require for a C program to do the same job. female) with table of one field with some entries (1 in female. Output is: F = yasin .” corresponds to te “∨”.yusuf).Y) :.. male(X).father(F. He must be a parent of Yusuf. sometimes read as "if" one or more goals (a query) For example lets define a rule for a father in the . To query your family logicbase base. male(X). For example they can be compined: Lets get the father of yusuf. X = yasin In Prolog.. So query like this: ?.parent(X. and must be male. enter: parent(X.yusuf).parent(X. male(yasin). The 4th and 5th lines are for defining a third database that defines a relation on previous databases. Its syntax is head :. So to query father of yusuf. For example the following facts define a simple family of yusuf.Y). aliye. and yasin. A rule is no more than a stored query. which may be facts or rules. female(aliye). male(yusuf). The queries can be much more functional. It is very easy to implement such relation tables in prolog.yusuf).” corresponds to “∧” and “. The first three fact is like defining two databases (male. parent(yasin. The output is: X = aliye Press . As we will see they can be queried like database records. 2 in male). Rules We said earlier a predicate is defined by clauses. “.pro file : father(X.body. X = yasin We got the parents of yusuf.yusuf). reconsult your source code and type: ?.Facts are the simplest form of Prolog predicates.

aliye).mother2.parent(yasin. The example file is located in [Amzi! installation dir]/samples/prolog/gene.assertz(parent(yasin.. The following example adds a daughter to the family: ?. it always fails on backtracking and does not undo its work. yes ?.elif))..yasin). again with a permanent effect that is not undone on backtracking.female. Like the other I/O predicates. And the program setup file includes a big family relationship example prolog code that uses the syntax represented If you scroll down the source code. only it adds the clause X as the last clause for its predicate.Managing Data We have created our knowledge base in the program source code.. But it is possible to insert new predicates to the program while it is running. E.Mother.add_person(yasin.g.assertz(female(elif)). yes ?.Spouse) Then you can query on the people in the family. Managing the logicbase on runtime is done through the following commands: asserta(X) Adds the clause X as the first clause for its predicate.elif)).consult('C:\\Documents and Settings\\yyilmaz\\Belgelerim\\Sabanci\\Prolog\\gene. yes .Gender.male. assertz(X) Same as') yes ?. Yes Show us the children of yasin: ?.Father. The data entries are manipulated by functions such as add_person(Name. yes ?. retract(X) Removes the clause X from the knowledgebase.assertz(parent(aliye. you will see a bunch of rules that defines the family relationships.add_person(aliye. 2. X = yusuf [press a key] X = elif Most of the topics and examples are covered in the tutorial of the Amzi!.X). An example usage: ?.father1.mother1.father2. we may want to add a new child to this family. X = yusuf ?.mother(X. Reference Amzi! Documentation: [your Amzi! Installation dir]\docs\aip\ . X = aliye ?..elif).yusuf).ahmet). yes ?After we add 4 people in the family we can query on them: ?.add_person(yusuf. yes ?.male.nephew(X.yasin.zeynep). No etc.?.

Sign up to vote on this title
UsefulNot useful