Setting up Prolog environment on Linux and Mac

Forrest Sheng Bao http://forrest.bao.googlepages.com

To implement an intelligent agent, there are two approaches, the algorithmic language and the declarative language. Prolog is a declarative language to build expert system, intelligent knowledge library and nature language reasoning.

Prolog is a language, not a software. You need to compile it before querying your system. Since Prolog is an industrial standard language (ISO/IEC 13211-1, ISO Prolog Standard), just like C and C++, anyone can write a Prolog compiler. SWI Prolog and GNU Prolog are two open source Prolog compiler. SWI Prolog is available for both Linux and Mac.

In Prolog, there is a term called "consulting", which means you load the program consist of clauses to the Prolog compiler. By consulting, you can build many facts of the environment. You can either use the Prolog compiler to generate an executable and then query on it, or you can just consult and query with the compiler prompt, as work with an interpreter. On Ubuntu Linux, install SWI Prolog and gprolog is quite easy, just type "sudo apt-get install swiprolog gprolog". On Mac OS X 10.4, go to the website of SWI Prolog to download it and the Safari browser will start the installation program automatically after finishing downloading. On Mac, system is installed in /opt/local/bin/swipl. Please add it into your UNIX PATH environmental variable if you wanna start it on the shell. In this article, I will use these family relations as an example:
Forrest is the brother of Christina.
Christina is the sister of Forrest.
Forrest is a boy and a child in his family.
Christina is a girl and a child in her family.
In Prolog, we can describe these relations as:
%Forrest is the brother of Christina.
brother(forrest,christina).
%Christina is the sister of Forrest.
sister(christina,forrest).
%Forrest is a boy and Christina is a girl.
gender(forrest,male).
gender(christina,female).
%Forrest and Christina are the children of the family.
family(forrest,child).
family(christina,child).
Each Prolog clause must end with a period. Words start with capitalized letter are variable. That is why here people's names are started with low-case letter. Save above program as helloworld.pl . Don't use capitalized letter as the first letter on file name either.

Then we can use SWI Prolog and GNU Prolog to compile our program.

Working with SWI Prolog


Start SWI Prolog by typing swipl in a shell and then you will see this:
forrest@flavia:~$ swipl
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).
You will see a Prolog prompt "?-". To consult, type "consult(helloworld)".
?- consult(helloworld).
% helloworld compiled 0.00 sec, 1,448 bytes
Yes
Now give the system some questions to see whether it has learned environment we told it.
?- brother(forrest,christina)
Yes
?- brother(forres,christina).
No
?- gender(forrest,female).
No
?- gender(forrest,male).
Yes
?- gender(christina,male).
No
Try some wh-questions. Who is the brother of Christina?
?- brother(X,christina).
X = forrest
Yes
What is the gender of Christina?
?- gender(christina,Y).
Y = female
Yes
Ok, a more complicated one, who are the children of the family?
?- family(X,child).
X = forrest ;
X = christina
Yes
The second answer "christina" will not be displayed until you type a semicolon after "forrest".

Working with GNU Prolog

You can also do this so in GNU Prolog. The only difference is on the consulting procedure. To start GNU Prolog, type gprolog in the Linux shell.
forrest@flavia:~$ gprolog
GNU Prolog 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
To consult, embrace the file name by a pair of square brackets.
| ?- [helloworld].
compiling /home/forrest/helloworld.pl for byte code...
/home/forrest/helloworld.pl compiled, 10 lines read - 961 bytes written, 20 ms
The query output of GNU Prolog is a little different from SWI Prolog:
| ?- family(X,child).
X = forrest ? ;
X = christina
yes
How to quit?

I still remembered the first time I didn't know how to poweroff a Linux machine. Maybe you will find that "exit" or "quit" command doesn't work on Prolog. Yeah, you should use "halt.". Don't forget the period after "halt".

Compiling into an executable
You can also compile your Prolog program into an executable.

On SWI Prolog, do like this:
forrest@flavia:~$ swipl -o helloworld -c helloworld.pl
% helloworld.pl compiled 0.00 sec, 2,776 bytes
Now you can execute the helloworld file:
forrest@flavia:~$ ./helloworld
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).
On GNU Prolog, do like this(the -v option is optional):

forrest@flavia:~$ gplc -o helloworld2 helloworld.pl -v
Prolog compiler (GNU Prolog) 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
GNU Prolog comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Prolog
under the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING.
Path used: /usr/lib/gprolog-iso

*** Compiling
--- file: helloworld.pl
pl2wam -o /tmp/gplcqs1Asb.wam helloworld.pl
wam2ma -o /tmp/gplcGKS1Kc.ma /tmp/gplcqs1Asb.wam
delete /tmp/gplcqs1Asb.wam
ma2asm -o /tmp/gplcW2Js3d.s /tmp/gplcGKS1Kc.ma
delete /tmp/gplcGKS1Kc.ma
as -o /tmp/gplc88leFa.o /tmp/gplcW2Js3d.s
delete /tmp/gplcW2Js3d.s

*** Linking
gcc-4.1 -o helloworld2 /usr/lib/gprolog-iso/obj_begin.o /tmp/gplc88leFa.o /usr/lib/gprolog-iso/all_pl_bips.o /usr/lib/gprolog-iso/all_fd_bips.o /usr/lib/gprolog-iso/top_level.o /usr/lib/gprolog-iso/debugger.o /usr/lib/gprolog-iso/libbips_fd.a /usr/lib/gprolog-iso/libengine_fd.a /usr/lib/gprolog-iso/libbips_pl.a /usr/lib/gprolog-iso/obj_end.o /usr/lib/gprolog-iso/libengine_pl.a /usr/lib/gprolog-iso/liblinedit.a -lm

delete /tmp/gplc88leFa.o

Start it:

forrest@flavia:~$ ./helloworld2
GNU Prolog 1.2.18
By Daniel Diaz
Copyright (C) 1999-2004 Daniel Diaz
| ?-
Trivial:
SWI Prolog can even correct typo for you. For example,
?- siser(forrest,christina).
Correct to: sister(forrest, christina)?
Please answer 'y' or 'n'? yes
No


Discussion

I am thinking about how to define such a rule in Prolog: "If X is a child of the family and X is female, then X must be the sister of Y, who is male and a child of the family."

It seems I need to spend a bit time on syntax and semantics of Prolog then.

References:
[1] GNU-Porlog Manual, http://www.gprolog.org/manual/gprolog.html
[2] A Quick Introduction to Using GNU Prolog, http://www.csc.uvic.ca/~csc330/Resources/Prolog-Intro.pdf
[3] Prolog Tutorial, http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html
[4] Getting Started Quickly Manual of SWI-Prolog, http://gollem.science.uva.nl/SWI-Prolog/Manual/quickstart.html

Figures:
GNU-Prolog on Ubuntu Linux 7.04


SWI-Prolog on Mac OS X:

No comments: