[ Running/Obtaining Prolog | On-line Documentation | Books ]
[ Sample Prolog code | Typing in and running Prolog code | Debugging]
You can check out a longer list of Prolog books
here. And if you
want to read about the general idea of logic programming, take a look at
R. Kowalski's Logic for Problem Solving (North-Holland NY, 1979)
To add facts or rules to the Prolog database, you need to use the
consult/1 predicate (which is like the use function of
SML). To enter stuff from the keyboard, say consult(user). To load
code from a file, specify a filename as an argument (preferably within
apostrophes). You can specify multiple files as arguments to the
consult predicate, but you will have to separate the names with
commas. Prolog has a useful shorthand notation : [...] is the same
as consult(...). So, I could add some facts from the
keyboard like this (a period is necessary at the end of each line and I pressed
Ctrl-D when I was done) :
| ?- consult(user).
| male(john_doe).
| female(susie_q).
| awesome_band(black_sabbath).
| ^D (or type in end_of_file)
% user compiled in module user, 0.010 sec 596 bytes
yes
| ?- female(X),awesome_band(Y).
X = susie_q,
Y = black_sabbath ;
no
| ?-
Now if I created a file called test.pl that contained the following
facts :
member( simon, simon_and_garfunkel ).
member( garfunkel, simon_and_garfunkel ).
I can now load it into Prolog :
| ?- ['test.pl'].
% compiling file
/afs/cs.wisc.edu/p/course/cs538-larus/public/html/prolog/test.pl
% test.pl compiled in module user, 0.030 sec 392 bytes
yes
| ?- member(X,simon_and_garfunkel).
X = simon ;
X = garfunkel ;
no
| ?-
CAVEAT : If you made a change in test.pl and then want to load the
new clauses into the Prolog database in the same session, then use
reconsult instead of consult. Reconsult
replaces the clauses present in the database with the ones from the
file while consult simply adds them on at the end, so you'll end
up with multiple copies of your facts and rules.
The Prolog system basically offers you two debugging operations :
1. Choosing when Prolog reports the occurrence of an event :