SWI Prolog Tutorial
By now it goes without saying, but I'll say it anyhow: This is a environment tutorial, not a language tutorial. For language documentation please see:
http://www.cs.washington.edu/education/courses/341/CurrentQtr/prolog/prolog.html
The tutorial is divided into three section listed below.
finding and running SWI prolog
SWI-Prolog is located on orcas and sanjuan in the following directory:
/cse/courses/misc_lang/axp/prolog/bin/pl
To avoid typing the directory every time, just add the following line to your .mycshrc file in your home directory:
set path = ($path /cse/courses/misc_lang/axp/prolog/bin)
and just type pl from anywhere to run SWI Prolog. If you wish to run SWI prolog from home the source code is available from the SWI-Prolog site at:
http://www.hio.hen.nl/faq/SWI-Prolog.htmlGetting Help
You should see the following text when you run SWI-Prolog.
orcas% pl Welcome to SWI-Prolog (Version 2.9.5) Copyright (c) 1993-1997 University of Amsterdam. All rights reserved. For help, use ?- help(Topic). or ?- apropos(Word). 1 ?-
Getting help is easy thanks to the built in features in SWI-Prolog. As suggested by the startup screen, SWI-Prolog has a built in manual, which can be searched using help(<topic you need help on>). Just like in unix, you can search for a keyword using apropos(<some word>) To view the top level help menu simply type:
1 ?- help.
Make sure to include the PERIOD ("."). The period tells prolog to interpret what you have written and not wait for any further input. Some more (relevant) examples of help:
2 ?- help(3). - shows the third section of the manual **note that typing "q" returns you to the command prompt**
3 ?- help(;). - the continue operator
4 ?- apropos(trace). - lists key words associated with the trace function
Sample Prolog Interaction
Welcome to the comically complex sample of prolog. The sample interaction is motivated by the movie The Stupids. I suggest that none of you waste your time watching this movie. This single highlight of this movie is the "I'm my own grandpa" song, whose lyrics can be viewed here.
Like Haskell you are unable to write rules directly at the command line. They must be loaded from a file using the consult command. Our sample file is called grandpa.pl, and should be saved locally. Once you have a copy of grandpa.pl start SGI Prolog and load the file by typing:
1 ?- consult('grandpa.pl').
You may also type ['grandpa.pl'] or just [grandpa] (because pl is the default file name.
NOTE: if you have a file named grandpa, and and file named grandpa.pl [grandpa] will load grandpa.pl. Look up help(consult) for further details.
Now all the definitions have been loaded you may further investigate the lineage of Stanley Stupid. In grandpa.pl are rules defining what the various relations are and facts about the Stupid Family. There is also another copy of the lyrics, with notes in the margins with the names we choose for characters. Type:
1 ?- married(father_stupid, X).
It will display the first value of X (a variable because it is capital) that satisfies the predicate married. To view more matching results use the ; key.
Experiment with the trace and spy functions, which allow you to follow prologs logic as it progresses through the rules. For complicated interactions you will see numbers appearing in the place of the variable names, these are values that the prolog uses when trying to find a match to the rules. Type:
1 ?- trace(grandparent). trace(step_parent).
Now you have turned on tracing for these two rules. To test if Stanley really is his own grandfather type:
2 ?- grandfather(stan_stupid, stan_stupid).
Now experiment with the different relations. Try:
1 ?- uncle(X, Y).
If there is circular logic then you can get the same answers over and over again. For now just use <control-c> to bring up the debugging menu and type a to abort. To exit the environment type:
1 ?- halt.
Have Fun. **Bonus points will be awarded to those who memorize the song.
** No bonus points will actually be awarded.