Hints on Prolog
SWI Prolog is the default prolog on the IT and the Grad suns.
- To start Prolog type
module load prolog; prolog
'
at the unix prompt.
You need to type in the command 'module load prolog' only the
first time you use prolog in each login. If you prefer, you
can make this step automatic by adding the module 'prolog' to the
list of modules that are added to you path when you login. This
is done by modifying the module command line in your .cshrc file.
- To get out of Prolog type
followed by
the letter e
or
use the Prolog predicate halt
.
interrupts the execution of the program.
Type h
to see what commands are available.
- When Prolog starts it is expecting a goal (the prompt is ?-) not
facts or clauses. You should write facts and clauses into a
file and then consult the file by typing
[filename]
to
the prolog prompt. Conventionally prolog files use the suffix
.pl
, which is assumed by default when the file is consulted.
- A
%
indicates that the rest of
the line (up to the end of line) is a comment. Alternatively,
everything bewteen /*
and */
is considered a comment.
- Upper case names are used for variables, lower case for constants.
- Do not forget to end each clause or goal by a period.
- The typing format is free, you can write clauses in as many lines as
you want, using a
.
to terminate the clause.
- Prolog will give you the first soilution. You can obtain additional
solutions by typing
;
after each solution is returned.
A simple prolog example is in the file parent.
In this example the file name is parent
and not
parent.pl
to avoid problems with Netscape. The command to
consult the file would have been the same even if the file had the
.pl
extension.
Here is a sample run.
dante> pl
Welcome to SWI-Prolog (Version 4.0.0)
Copyright (c) 1990-2000 University of Amsterdam.
Copy policy: GPL-2 (see www.gnu.org)
For help, use ?- help(Topic). or ?- apropos(Word).
1 ?- [parent].
% parent compiled 0.01 sec, 1,792 bytes
Yes
2 ?- grandparent(X,Y).
X = john
Y = bill ;
X = john
Y = dan ;
No
3 ?- grandparent(john,X).
X = bill ;
X = dan ;
No
4 ?- parent(X,Y).
X = sonja
Y = mary ;
X = sonja
Y = jane ;
X = john
Y = jim ;
X = john
Y = bob ;
X = bob
Y = bill ;
X = bob
Y = dan ;
No
5 ?- listing.
mother(sonja, mary).
mother(sonja, jane).
father(john, jim).
father(john, bob).
father(bob, bill).
father(bob, dan).
parent(A, B) :-
mother(A, B).
parent(A, B) :-
father(A, B).
grandparent(A, B) :-
parent(A, C),
parent(C, B).
Yes
6 ?- halt.
dante>
- Prolog assumes that all predicates with the same head
appear together in the program, and if they are not together
there could be a typing error. Prolog also assumes that if you
use a variable in the head of a clause and not in the body of
the same clause it must be a typing error. Prolog issues warn-
ings that can be safely ignored.
- Comparison of numbers can be performed using
>=, =<, >, <
as
relational operators in infix form. These operators require
both their arguments to be bound to a numerical value. For
instance if you write something like:
commencement(Date):- Date >= 1982.
the variable Date needs to be bound when the goal
commencement(Date) is executed. Otherwise the comparison
Date>=1982 generates an error message because >= expects to
compare two numbers.
- To assign a value to a variable you need to use the operator
is
, which is used in infix notation, as in the example
shown here:
age(X,Age,now):- born(X,Date), Age is now-Date.
The operator 'is' assigns the value of the expression now-Date
to the variable Age
, so when the goal succeeds
Age
is bound.
- The negation is called
\+
and it is used as a
prefix operator without parentheses, as shown here:
married(X):- \+single(X).
Since negation can be expensive (because of the close world
assumption) is better to avoid using it, mainly when the
negated predicate has unbound arguments.
- To enter new facts during the execution you can use
the special directive
[user]
as shown here:
| ?- [user].
| age(john,70).
| single(john).
.... etc ... The system types the | to remind you that you
are entering information, not trying to prove a goal. When you
are done you type control-D.
- To debug programs you can use
trace
or set
spy points. The built-in predicate trace/0
switches the
debugger on, untrace/0
switches it off.
The built-in predicate spy/1
allows you to specify which predicates you want to spy when
they are used. nospy/1
removes the spy points. The built-in
predicate debugging/0
will show current spypoints and other
degugging options.
?- spy(parent).
?- spy(parent/2).
?- spy([parent/2,born_in_UK]).
-
The reference manual for is available on-line. The
relevant commands are:
help
, which explain the help-system briefly,
help(Topic)
which shows the help-page on the topic, and
apropos(String)
, which show all topics holding String in
their summary documentation.