Terms
KEY The data objects of the language are called terms. A term is either a constant, a variable or a compound term. The constants include integers such as 0 1 999 512 Integers prefixed with a minus sign are currently read as full terms (not negative integers). I.e. -77 -909 -1 are read as the terms: -(77) -(909) -(1) However these are valid integer expressions and will work as expected with the arithmetic predicates, such as is or < etc. Constants also include atoms such as a void = := 'Algol-68' [] The symbol for an atom can be any sequence of characters, written in single quotes if there is possibility of confusion with other symbols (such as variables, integers). As in conventional programming languages, constants are definite elementary objects, and correspond to proper nouns in natural language. Variables are distinguished by an initial capital letter or by the initial character "_", e.g. X Value A A1 _3 _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. This is analogous to the use of a pronoun in natural language. Note that a variable is not simply a writeable storage location as in most programming languages; rather it is a local name for some data object, cf. the variable of pure LISP and identity declarations in Algol68. 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 characterised 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 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, 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 in Section 1.14.2 below. 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 structure . / \ 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 list syntax is only syntactic sugar for terms of the form '.'(_,_) and does not provide any additional facilities that were not available in Prolog. 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. E.g. "Prolog" which represents exactly the same list as [80,114,111,108,111,103]