Information Technology Reference
In-Depth Information
Figure 3.10.
A review of Prolog programs and queries
Here are the pieces that make up Prolog programs and queries:
A
constant
is either a string of characters enclosed within single quotes or a
lowercase letter optionally followed by letters, digits, and underscores.
A
variable
is an uppercase letter or an underscore optionally followed by letters,
digits, and underscores.
A
number
is a sequence of one or more digits optionally preceded by a minus
sign and optionally containing a decimal point.
A
term
is a constant, variable, or number.
A
predicate
is written as a constant.
An
atom
is a predicate optionally followed by terms (called the
arguments
of the
predicate) enclosed within parentheses and separated by commas.
An
equality
is two terms separated by the
=
symbol.
A
literal
is an atom or an equality optionally preceded by the
\+
symbol.
A
query
is
a
sequence
of
one
or
more
literals
separated
by
commas
and
terminated with a period.
A
clause
is an atom (called the
head
of the clause) followed by a period or by the
:-
symbol and then a query (called the
body
of the clause).
A
program
is a sequence of one or more clauses.
Figure 3.11.
The likes example
likes.pl
% This is a program about who likes what kinds of food.
likes(john,pizza).
% John likes pizza.
likes(john,sushi).
% John likes sushi.
likes(mary,sushi).
% Mary likes sushi.
likes(paul,X) :- likes(john,X).
% Paul likes what John likes.
likes(X,icecream).
% Everybody likes ice cream.
3.3.1 Unification
Clauses in a program are selected during back-chaining through a matching process
called
unification
. Two atoms whose variables are distinct are said to
unify
if there is a