Programming Paradigms

Back to CoSc 450.

Setting up for Prolog

Installing GNU Prolog

We will be using GNU Prolog available at http://www.gprolog.org/. There are binary distribution packages for Windows and Mac. Linux users can compile gprolog from the source.

The Mac distribution installs GNU Prolog in /opt/local/bin, so you will have to put that location in your path using the following steps.

1. cd to your home directory.
2. vi .profile
3. Add this line
PATH=/opt/local/bin:$PATH
4. If it does not already exist, add this line
export PATH

Using gprolog

I recommend that you startup a text editor for your Prolog program and at the same time maintain a command line window with Prolog running. You can write your Prolog program and save it in your text editor, and then switch to the command line window to test it.

I highly recommend that you use this opportunity to learn how to use vi, the standard Unix text editor, for writing your Prolog programs. There are many beginning tutorials online. Here is a summary of the vi commands.

Example gprolog session

When the gprolog engine gives one solution and asks for further input with ?, you have three options:

; to compute the next solution
a to compute all remaining solutions
<ret> to stop the execution

Here is a prolog session from the first chapter.
Stan-Warfords-iMac:prolog warford$ gprolog
GNU Prolog 1.4.0
By Daniel Diaz
Copyright (C) 1999-2011 Daniel Diaz
| ?- consult( 'fig1_8.pl').
compiling /Users/warford/Documents/Classes/cs450/prolog/fig1_8.pl for byte code...
/Users/warford/Documents/Classes/cs450/prolog/fig1_8.pl compiled, 46 lines read - 3223 bytes written, 18 ms

(1 ms) yes
| ?- parent( bob, pat).

yes
| ?- parent( liz, pat).

no
| ?- parent( X, liz).

X = tom ? a

no
| ?- parent( X, Y).

X = pam
Y = bob ? ;

X = tom
Y = bob ? ;

X = tom
Y = liz ? ;

X = bob
Y = ann ? a

X = bob
Y = pat

X = pat
Y = jim

yes
| ?- halt.
You exit gprolog with the
halt.
command. The period is mandatory.

Code for the programs in the book are on the course web page. There is one requirement for gprolog programs not followed by our book. All clauses must be contiguous on the head of the clause. Thus, the program fragment from the book
female( pam).            % Pam is female
male( tom).              % Tom is male
male( bob).
female( liz).
female( ann).
female( pat).
male( jim).
must be written
female( pam).            % Pam is female
female( liz).
female( ann).
female( pat).
male( tom).              % Tom is male
male( bob).
male( jim).
Also, the different predicate is not supplied in our version of prolog. Define different with two rules as follows (see page 125):
different( X, X) :- !,fail.
different( _, _).
The cut operator ! prevents the second rule from executing after the first rule fails. You should include these two rules in all files that use the predicate different.