Getting Starting With Prolog Using Amzi!: Select "New-Project-Prolog Project" To Start A Project
Getting Starting With Prolog Using Amzi!: 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 Type “quit.” in the Listener window (lower half of your screen) or hit the red button
on upper-right of Listener window
Some of the following sections replicate content of the lecture (prolog.ppt), but other parts get
into details and the Amzi environment.
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).
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.
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
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 is a predicate definition
:- is the neck symbol, sometimes read as "if"
body
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).
?- assertz(female(elif)).
yes
?- assertz(parent(aliye,elif)).
yes
?- assertz(parent(yasin,elif)).
Yes
?- 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.
Then you can query on the people in the family. An example usage:
etc...
Reference
Amzi! Documentation: [your Amzi! Installation dir]\docs\aip\