Information Technology Reference
In-Depth Information
Between each argument, there must be a comma.
Immediately after the last argument, there must be a right parenthesis.
When a predicate has no arguments
(
k
=
)
, the parentheses can be left out.
0
Conditional sentences
The
conditional sentences
of Prolog have this form:
head
:-
body
1
,
...
,
body
n
where the head and each element of the body is a Prolog atom. In this case, the
notation is quite different from the sentences in chapter 2. For one thing, the
head
,
which represents the
then
part, appears first, and the
body
, which represents the
if
part, appears afterward. So
father(Y,X) :- child(X,Y), male(Y)
is equivalent to
If X is a child of Y and Y is male then Y is a father of X
. Note that the
words
if
,
then
, and
and
are left out, and special punctuation is required:
Immediately after the head, there must be a colon and then a hyphen.
Between each element of the body, there must be a comma.
When the body is empty
, the
:-
should be omitted. This means that as far
as Prolog is concerned, an atomic sentence is really just a special case of a conditional
sentence where the body happens to be empty. (The body can also include other
things discussed later.)
In sum, a Prolog
program
is a sequence of clauses, where a
clause
is an atomic
or conditional sentence terminated by a period. To make programs easier to read
(without changing what the program does), spaces, newlines, and comments can
be inserted at the end of a program or just before a constant or variable. A Prolog
comment
starts with a
%
character (the percent sign) and continues to the end of the
line. (In this topic, comments are shown in gray italics, but they do not have to be.
Only the percent sign is required.)
Here is a very short but complete Prolog program:
zzz.
This is a Prolog program for the following reasons. A program can be a single clause
terminated by a period; a clause can be an atomic sentence; an atomic sentence can
be a predicate with no arguments or parentheses; a predicate is written as a constant;
and finally,
zzz
is a constant. Here is a somewhat longer program:
(
n
=
)
0