Terms

The syntax of SB-Prolog is by and large compatible with that of C-Prolog. The data objects of the language are called terms. A term is either a constant, a variable or a compound term. Constants can be integers or atoms. The symbol for an atom must begin with a lower case letter or the dollar sign $, and consist of any number of letters, digits, underscores and dollar signs; if it contains any character other than these, it must be enclosed within single quotes.3As in other programming languages, constants are definite elementary objects.

Variables are distinguished by an initial capital letter or by the initial character ``_'' for example

X Value A A1 _3 _RESULT _result
If a variable is only referred to once, it does not need to be named and may be written as an anonymous variable, indicated by the underline character _.

A variable should be thought of as standing for some definite but unidentified object. A variable is not simply a writable storage location as in most programming languages; rather it is a local name for some data object, cf. the variable of pure LISP and constant declarations in Pascal.

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)
An atom is considered to be a functor of arity 0.

A functor or predicate symbol is uniquely identified by its name and arity (in other words, it is possible for different symbols having different arities to share the same name). A functor or predicate symbol p with arity n is usually written p/n.

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 structure
      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

X+Y (P;Q) X<Y +X P;
as optional alternatives to
+(X,Y) ;(P,Q) <(X,Y) +(X) ;(P)
Operators are described fully in the next section.

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 `.'/2 and two arguments which are respectively the head and tail of the list. Thus a list of the first three natural numbers is the structure (shown in Figure [*]) which could be written, using the standard syntax, as .(1,.(2,.(3,[ ]))),

Figure: Structure for the Function .(1,.(2,.(3,[ ])))
  .
 / \
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 the structures shown in Figure [*]
Figure: Structures for the Functions [X|L] and [a,b|L
  .              .
 / \            / \
X   L          a   .
                  / \
                 b   L
respectively.

Note that this list syntax is only syntactic sugar for terms of the form `.'(_, _) and does not provide any new facilities that were not available otherwise.

For convenience, a further notational variant is allowed for lists of integers which correspond to ASCII character codes. Lists written in this notation are called strings. For example, "Prolog" represents exactly the same list as [80,114,111,108,111,103].