All posts by ligeza
Constraint Programming
BPMN
Prolog
To start using SWI-Prolog under Linux you need to have it installed. Fortunately, SWI-Prolog is a standard package in every reasonable Linux distribution.
In Ubuntu, you can use the Synaptic Package Manager to check (if unsure) or install (if not performed yet) the SWI-Prolog.
In order to start using Prolog follow the simplest recipe:
- Make a directory to store your Prolog programs, for example
/home/username/prolog/
- Create an empty text file with a name of your choice and extension
.pl
in this directory; for examplesocrates.pl
. You can use any ASCII text editor of your preference (e.g. vi, vim, gedit, emacs).
The above steps may look as follows:
ali@ali:~/ALI/PROLOG$ mkdir prolog
ali@ali:~/ALI/PROLOG$ cd prolog
ali@ali:~/ALI/PROLOG/prolog$ gedit socrates.pl
- Edit your first program with use of the editor. The program may be as follows:
man(plato).
man(socrates).
mortal(X):- man(X).
- Open a shell window and go to the directory where you store your Prolog programs (e.g.
/home/username/prolog/
). - Start the Prolog interpreter/compiler. In most cases you just write
prolog
, orswipl
. The prompt changes to?-
. Disappointed? No graphical environment? Do not worry. Intelligent people quickly get used to this modest environment and appreciate the ideas behind Prolog. - Now you have to load your program. This can be done by typing
[programname].
command in the shell window. Rememebr that the name is not followed by.pl
and after the closing bracket there must be a full stop. For example type[socrates]
- In case there are no errors you can start using the program by asking questions. You just type a predicate name (one appearing in the program) with the appropriate number of arguments in parentheses; the arguments must be separated by commas. Remember to always add full stop.
The above steps may look like:
ali@ali:~/ALI/PROLOG/prolog$ prolog
Welcome to SWI-Prolog (Multi-threaded, Version 5.6.14)
Copyright (c) 1990-2006 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
?- [socrates].
% socrates compiled 0.00 sec, 864 bytes
Yes
?-
- If you expect more than one answer, use the colon (
;
) to produce them.
This may look like:
?- man(socrates).
Yes
?- man(X).
X = plato ;
X = socrates ;
No
?- mortal(socrates).
Yes
?- mortal(X).
X = plato ;
X = socrates ;
No
?-
Names like socrates
, i.e. ones starting with a lower case letter are constants (proper names); variable names starts with an upper case letter. Both of them are parameters of predicates (relations) such as man
or mortal
. Every fact (a simple statement) such as man(socrates).
is followed by a full stop. Every clause such as mortal(X):- man(X).
represents and inference rule and is also followed by a full stop.
You can modify your program with the editor. After saving it always re-load it.
*To stop the Prolog interpreter/compiler type in the halt.
command.
To obtain help you may type in one of the following commands:
?- help.
Explain the basics of the help system.
?- help(topic).
Show help-page on the specified topic.
?- apropos(string).
Show all topics holding `string' in their summary documentation.
Have a fun by modifying the program and asking more and more complex questions!