The structured data objects of the language are the compound
terms. A compound term comprises a functor (called the
principal functor of the term) and a sequence of one or more
terms called arguments. A functor is characterized by
its name, which is an atom, and its arity or number of
arguments. For example the compound term whose
functor is named point
of arity 3, with
arguments X
, Y
and Z
, is written
point(X, Y, Z)
Note that an atom is considered to be a functor of arity 0.
Functors are generally analogous to common nouns in natural language. One may think of a functor as a record type and the arguments of a compound term as the fields of a record. Compound terms are usefully pictured as trees. For example, the term
s(np(john),vp(v(likes),np(mary)))
would be pictured as the compound term
s / \ np vp | / \ john v np | | likes mary
Sometimes it is convenient to write certain functors as operators--2-ary functors may be declared as infix operators and 1-ary functors as prefix or postfix operators. Thus it is possible to write e.g.:
X+Y (P;Q) X<Y +X P;
as optional alternatives to
+(X,Y) ;(P,Q) <(X,Y) +(X) ;(P)
The use of operators is described fully below (see Operators).
Lists form an important class of data structures in Prolog. They
are essentially the same as the lists of LISP: a list either
is the atom []
representing the empty list, or is a
compound term with functor .
and two arguments,
which are respectively the head and tail of the list.
Thus a list of the first three natural numbers is the
compound term
. / \ 1 . / \ 2 . / \ 3 []
which could be written, using the standard syntax, as
.(1,.(2,.(3,[])))
but which is normally written, in a special list notation, as
[1,2,3]
The special list notation in the case when the tail of a list is a variable is exemplified by
[X|L] [a,b|L]
representing
. . / \ / \ X L a . / \ b L
respectively.
Note that this notation does not add any new power to the language; it simply makes it more readable. e.g. the above examples could equally be written
.(X,L) .(a,.(b,L))
For convenience, a further notational variant is allowed for lists of integers that correspond to character codes or one-char atoms. Lists written in this notation are called strings. E.g.:
"SICStus"
which, by default, denotes exactly the same list as
[83,73,67,83,116,117,115]
The Prolog flag double_quotes
can be used to change the way
strings are interpreted. The default value of the flag is codes
,
which implies the above interpretation. If the flag is set to
chars
, a string is transformed to a char-list. E.g. with
this setting the above string represents the list:
['S','I','C','S',t,u,s]
Finally if double_quotes
has the value atom
, then the
string is made equivalent to the atom formed from its characters:
the above sample string is then the same as the atom
'SICStus'
.
Unless character escapes have been switched off, backslashes in the
sequence denote escape sequences (see Escape Sequences). As
for quoted atoms, if a double quote character is included in the
sequence it must be escaped, e.g. "can\"t"
.