prolog example programs

Document Sample
prolog example programs Powered By Docstoc
					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 gene.pro (or other prolog file) or write sample sentences into the IDE main window (top-middle part of the
screen)

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. http://www.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: Yasin Yilmaz, yyilmaz@su.sabanciuniv.edu. Modified by Berrin Yanikoglu. Date: Written in 2002, modified latest in 2007

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

Logic Programming
Let's look at the simple example in more detail. In classical logic we might say "All people are mortal," or, rephrased for Prolog, "For all X, X is mortal if X is a person." mortal(X) :- person(X). A fact, is asserted in a similar way: male(yasin). We could also ask "Who is mortal?" like this ?- mortal(X). and receive the response ?- mortal(X). X = aliye To print out all mortals, we can add a mortal_report function to our mortal.pro file: mortal_report:write('Known mortals are:'),nl, mortal(X), write(X),nl, fail. When we reconsult and write: ?- mortal_report. The output will be: Known mortals are: aliye yasin yusuf no.

The relations and other stuff we have written in our mortal.pro are all called predicates in prolog jargon. Prolog programs are composed of these predicates. An 'if x is then y' relation is a predicate, and even a print command is expressed in the same format as shown in the above example. Predicates are composed of clauses. A clause can be either a fact or a rule. An example fact will be: male(yasin). A rule example is: person(X) :- male(X). Facts are the simplest form of Prolog predicates, and are similar to records in a relational database. As we will see they can be queried like database records. For example the following 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 like defining two databases (male, female) with table of one field with some entries (1 in female, 2 in male). The 4th and 5th lines are for defining a third database that defines a relation on previous databases. It is very easy to implement such relation tables in prolog. Just think how many lines of code it will require for a C program to do the same job. To query your family logicbase base, enter: parent(X,yusuf). The output is: X = aliye Press ; X = yasin We got the parents of yusuf. The queries can be much more functional. For example they can be compined: Lets get the father of yusuf. He must be a parent of Yusuf, and must be male.... So query like this: ?- parent(X,yusuf), male(X). X = yasin In Prolog, “,” corresponds to “” and “;” corresponds to te “”.

Rules
We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no more than a stored query. Its syntax is head :- body. where head :body is a predicate definition is the neck symbol, sometimes read as "if" one or more goals (a query)

For example lets define a rule for a father in the .pro file : father(X,Y) :- parent(X,Y), male(X). So to query father of yusuf, reconsult your source code and type: ?- father(F,yusuf). Output is: F = yasin

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.g., we may want to add a new child to this 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. Like the other I/O predicates, it always fails on backtracking and does not undo its work. assertz(X) Same as asserta, only it adds the clause X as the last clause for its predicate. retract(X) Removes the clause X from the knowledgebase, again with a permanent effect that is not undone on backtracking. The following example adds a daughter to the family: ?- assertz(female(elif)). yes ?- assertz(parent(aliye,elif)). yes ?- assertz(parent(yasin,elif)). Yes Show us the children of yasin: ?- parent(yasin,X). X = yusuf [press a key] X = elif Most of the topics and examples are covered in the tutorial of the Amzi!. And the program setup file includes a big family relationship example prolog code that uses the syntax represented here. The example file is located in [Amzi! installation dir]/samples/prolog/gene.pro If you scroll down the source code, you will see a bunch of rules that defines the family relationships. The data entries are manipulated by functions such as add_person(Name,Gender,Mother,Father,Spouse)

Then you can query on the people in the family. An example usage:
?- consult('C:\\Documents
and Settings\\yyilmaz\\Belgelerim\\Sabanci\\Prolog\\gene.pro')

yes ?- add_person(yasin,male,mother1,father1,aliye). yes ?- add_person(aliye,female,mother2,father2,yasin). yes ?- add_person(yusuf,male,aliye,yasin,zeynep). yes

?- add_person(elif,female,aliye,yasin,ahmet). yes ?After we add 4 people in the family we can query on them: ?- mother(X,yusuf). X = aliye ?- brother(X,elif). X = yusuf ?- nephew(X,yusuf). No etc...

Reference
Amzi! Documentation: [your Amzi! Installation dir]\docs\aip\


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:13300
posted:2/19/2009
language:English
pages:7