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:
/home/username/prolog/
.pl
in this directory; for example socrates.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
man(plato). man(socrates). mortal(X):- man(X).
/home/username/prolog/
).prolog
, or swipl
. 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.[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]
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 ?-
;
) 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!