Compound Terms

The structured data objects of Prolog are compound terms. A compound term comprises a name (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 principal functor is point of arity 3, and which has arguments X, Y, and Z, is written

     point(X, Y, Z)

When we need to refer explicitly to a functor we will normally denote it by the form Name/Arity. Thus, the functor point of arity 3 is denoted


Note that a functor of arity 0 is represented as an atom.

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 (compound) term

     s(np(john), vp(v(likes), np(mary)))

would be pictured as the following tree:

                     /   \
                  np       vp
                  |       /  \
                john     v     np
                         |     |
                       likes  mary

The principal functor of this term is s/2. Its arguments are also compound terms. In illustration, the principal functor of the first argument is np/1.

Sometimes it is convenient to write certain functors as operators; binary functors (that is, functors of two arguments) may be declared as infix operators, and unary functors (that is, functors of one argument) may be declared as either prefix or postfix operators. Thus it is possible to write

     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 in ref-syn-ops.