A first look at Prolog

Prolog is a logic language, not an algorithmic language, and one therefore has to learn to think about programs in a somewhat different way. The terminology is also somewhat different.

The following is a simple Prolog program:

	mortal(X) :- man(X).

The first line can be read, ``Socrates is a man.'' It is a base clause, which represents a simple fact.

The second line can be read, ``X is mortal if X is a man;'' in other words, ``All men are mortal.'' This is a clause, or rule, for determining when its input X is ``mortal.'' (The symbol ``:-'', sometimes called a turnstile, is pronounced ``if''.) We can test the program by asking the question:

	| ?- mortal(socrates).
that is, ``Is Socrates mortal?'' (The ``| ?-'' is the computer's prompt for a question.) Prolog will respond ``yes''. Another question we may ask is:
	| ?- mortal(X).
That is, ``Who (X) is mortal?'' Prolog will respond ``X = socrates''.

            Table of contents
Next page

Copyright © 1995 by David Matuszek
All rights reserved.
Last updated July 15, 1995