You are on page 1of 5

Introducing Prolog II

1.3. VARIETIES OF PROLOG

An important goal of this book is to teach you how to write portable Prolog code.
Accordingly, we will stick to features of the language that are the same in practically all implementations.
The programs in this book were developed in Arity Prolog and ALS Prolog on IBM PCs and Quintus Prolog on Sun
workstations. Most of them have also been tested in SWI Prolog, LP A Prolog, Cogent (Amzi) Prolog, and Expert
Systems Limited's Public Domain Prolog-2.1
For many years, the de facto standard for Prolog was the language described by Clocksin and Me1lish in their
popular textbook, Programming in Prolog (1981, second edition 1984). This is essentially the language implemented
on the DEC-10 by D. H. D. Warren and his colleagues in the late 19705, and is often called "Edinburgh Prolog" or
"DEC-10 Prolog." Most commercial implementations of Prolog aim to be compatible with it
In 1995 the International Organization for Standardization (ISO) published an international standard for the
Prolog language (Scowen 1995). ISO Prolog is very similar to Edinburgh Prolog but extends it in some ways. Our aim
in this book is to be as compatible with the ISO standard as possible, but without using features of ISO Prolog that are
not yet widely implemented.
Finally, we must warn you that this book is not about Turbo Prolog (PDC Prolog), nor about Colmerauer's
Prolog II and Prolog ill. Turbo Prolog is Prolog with data type declarations added. As a result, programs run faster but
are largely unable to examine and modify themselves. Colmerauer's Prolog II and ill are CONSTRAINT LOGIC
PROGRAMMING languages, which means they let you put limits on the value of a variable before acb1ally giving it a
value; this makes many new techniques available. The concepts in this book are certainly relevant to Turbo (PDC)
Prolog and Prolog II and ill, but the details of the languages are different

Exercise 1.3.1
If you have not done so already~ familiarize yourself with the manuals for the version of Prolog that you will
be using.

Exercise 1.3.2
In the Prolog that you are using, does the query '?- he1p .' do anything useful? Try it and see.

1.4. A PRACTICAL KNOWLEDGE BASE

Figure 1.1 shows a Prolog knowledge base that describes the locations of certain North American cities. It
defines a single relation, called located-in, which relates a city to a larger geographical unit The knowledge base
consists of facts such as “Atlanta is located in Georgia,” “Houston is located in Texas,” and the like, plus rules such as
“X is located in the United States if X is located in Georgia.”

1
Users of FSL Public Domain Prolog-2 must select Edinburgh-compatible syntax by adding the line ' : -
state(token.class,_,_dec10).' at the beginning of every program. Note that FSL Prolog-2 has nothing to do with
CoJmerauer's Prolog II.
% File GEO.PL
% Sample geographical knowledge base

/* Clause 1 */ located-in(atlanta,georgia).
/* Clause 2 */ located-in(houston,texas).
/* Clause 3 */ located-in(austin, texas) .
/* Clause 4 */ located-in(toronto,ontario).
/* Clause 5 */ located-in(X,usa) :- located-in(X,georgia).
/* Clause 6 */ located-in(X,usa) :- located-in(X,texas).
/* Clause 7 */ located-in(X,canada) :- located-in(X,ontario).
/* Clause 8 */ located-in(X,north-america) :- located-in(X,usa).
/* Clause 9 */ located-in(X,north-america) :- located-in(X,canada).

Figure 1.1 A simple Prolog knowledge base.

Notice that names of individuals, as well as the predicate located-in, always begin with lowercase
letters. Names that begin with capital letters are variables and can be given any value needed to carry out
the computation. This knowledge base contains only one variable, called X. Any name can contain the
underscore character ( _ ).
Notice also that there are two ways to delimit comments. Anything bracketed by /* and */ is a
comment; so is anything between % and the end of the line, like this:
/* This is a comment */
% So is this
Comments are ignored by the computer; we use them to add explanatory information and (in this
case) to number the clauses so we can talk about them conveniently.
It is not clear whether to call this knowledge base a program; it contains nothing that will actually
cause computation to start. Instead, the user loads the knowledge base into the computer and then starts
computation by typing a QUERY, which is a question that you want the computer to answer. A query is
also called a GOAL. It looks like a Prolog clause except that it is preceded by '?-' -although in most cases
the Prolog implementation supplies the '?-' and you need only type the goal itself.
Unfortunately, we cannot tell you how to use Prolog on your computer because there is
considerable variation from one implementation to another. In general, though, the procedure is as follows.
First use a text editor to create a file of clauses such as GEO.PL in Figure 1.1 Then get into the Prolog
interpreter and type the special query:
?- consult('geo.pl').
(Remember the period at the end -- if you don't type it, Prolog will assume your query continues onto the
next line.) Pro1og replies
yes
to indicate that it succeeded in loading the knowledge base.
.
Two important notes: First, if you want to load the same program again after escaping to an editor,
use reconsult instead of consult. That way you won't get two copies of it in memory at the same time.
Second, if you're using a PC, note that backslashes (\) in the file name may have to be written twice (e.g.,
consult(‘c:\ \myprog.pl’) to load C:\MYPROG.PL). This is required in the IS0 standard but not in most of
the MS-DOS Prologs that we have worked with.
As soon as consult has done its work, you can type your queries. Eventually, you'll be through
using Prolog, and you can exit from the Prolog system by typing the special query
?- halt.

Most queries, however, retrieve information from the knowledge base. You can type
?- located-in(atlanta,georgia).

to ask whether Atlanta is in Georgia. Of course it is; this query matches Clause 1
exactly, so Prolog again replies "yes." Similarly, the query

?- located-in(atlanta,usa) .
can be answered (or, in Prolog jargon, SOLVED or SATISFIED) by calling Cause 5 and
then Clause 1, so it, too, gets a "yes." On the other hand, the query

? - located-in(atlanta, texas) .
gets a "no" because the knowledge base contains no information from which the
existence of an Atlanta in Texas can be deduced.
We say that a query SUCCEEDS if it gets a "yes" answer, or FAILS if it gets a "no" answer.
Besides answering yes or no to specific queries, Prolog can fill in the blanks in a query that
contains variables. For example, the query

?- located-in(X,texas).
means "Give me a value of X such that in(X, texas) succeeds."
Here we run into another unique feature of Prolog -a single query can have multiple solutions.
Both Houston and Austin are in Texas. What happens in this case is that Prolog finds one solution and then
asks you whether to look for another. This continues until all alternatives are found or you stop asking for
them. In some Prologs, the process looks like this:

?- located-in(X,texas).

X = houston
More (y/n)? y
X = austin
More (y/n)? y
no

The "no" at the end means there are no more solutions.


In Arity Prolog, the notation is more concise. After each solution, the computer displays an arrow
( -> ). You respond by typing a semicolon (meaning look for more alternatives) or by hitting Return
(meaning quit), like this:
?- located-in(X,texas).
X = houston -> ;
X = austin -> ;
no
In Quintus Prolog and many others, there isn't even an arrow; the computer just pauses and waits
for you to type a semicolon and then hit Return, or else hit Return by itself:
?- located-in(X,texas).
X = houston j
X = austin
no
Also, you'll find it hard to predict whether the computer pauses after the last solution; it depends
partly on the way the user interface is written, and partly on exactly what you have queried. From here on,
we will present interactions like these by printing only the solutions themselves and leaving out whatever
the user had to type to get the alternatives.
Sometimes your Prolog system may not let you ask for alternatives (by typing semicolons, or whatever)
even though alternative solutions do exist. There are two possible reasons. First, if your query has
performed any output of its own, the Prolog system will assume that you've already printed out whatever
you wanted to see, and thus that you're not going to want to search for alternatives interactively. So, for
example, the query
?- located-in(X,texas), write(X).
displays only one answer even though, logically, there are alternatives. Second, if your query contains no
variables, Prolog will only print 'yes" once no matter how many ways of satisfying the query there actually
are.
Regardless of how your Prolog system acts, here's a sure-fire way to get a list of an the cities in
Texas that the knowledge base knows about

?- located-in(X,texas), write(X), nl, fail.


The special predicate write causes each value of X to be written out; n1 starts a new line after each
value is written; and fail forces the computer to backtrack to find all solutions. We will explain how this
works in Chapter 2. For now, take it on faith.
We say that the predicate located-in is NONDETERMINISTIC because the same question can
yield more than one answer. The term "nondeterministic" does not mean that computers are unpredictable
or that they have free will, but only that they can produce more than one solution to a single problem.
Another important characteristic of Prolog is that any of the arguments of a predicate can be
queried. Prolog can either compute the state from the city or compute the city from the state. Thus, the
query

?- located_in(austin,X).
retrieves the names of regions that contain Austin, and
?- located-in(X.texas).
retrieves the names of cities that are in Texas. We will cal this feature REVERSIBILITY OR
INTERCHANGEABILITY OF UNKNOWNS. In many – but not all – situations, Prolog can fill in any argument of a
predicate by searching the knowledge base.

We can even query all the arguments of a predicate at once. The query

?- located-in(X.Y).

means "What is in what?" and each answer contains values for both X and Y (Atlanta is in Georgia, Houston is in
Texas, Austin is in Texas, Toronto is in Ontario, Atlanta is in the U.S.A., Austin is in the U.S.A., Toronto ins in
Canada and so forth). On the other hand,

?- located-in(X.X).
means "What is in itself?" and fails --both occurrences of X have to have the same value, and there is no value of X that
can successfu1ly occur in both positions at the same time. If we were to add New York to the knowledge base, this
query could succeed because the city has the same name as the state containing it.

Exercise 1.4.1
Load GEO.PL into your Prolog system and try it out. How does your Prolog system respond to each of the following
queries?
?- 1ocated-in(Austin,texas) .
?- 1ocated-in(austin.georgia).
?- 1ocated-in(What,texas).
?- 1ocated-in(at1anta.What).

Exercise 1.4.2
Add your hometown and state (or region) and country to GEO.PL and demonstrate that the modified version works
correctly.

Exercise 1.4.3
How does GEO.PL respond to the query '?- 1ocated_in(texas,usa).’ ? Why?

Exercise 1.4.4 (for PC users only)


Does your Prolog require backslashes in file names to be written double? That is, to load C:\MYDIR\MYPROG.PL,
do you have to type consult(‘c:\\mydir\\myprog.pl’)?
Try it and see.

From Prolog Programming In Depth by Michael A. Covington, Donld Nute and André Vellino.
© 1997 Prentice-Hall, Inc.
Used with written permission

You might also like