next up previous contents index
Next: Using Tabling in XSB: Up: Syntax Previous: From HiLog to Prolog   Contents   Index


Operators

From a theoretical point of view, operators in Prolog are simply a notational convenience and add absolutely nothing to the power of the language. For example, in most Prologs '+' is an infix operator, so

                                 2 + 1
is an alternative way of writing the term +(2, 1). That is, 2 + 1 represents the data structure:

                                   +
                                  / \
                                 2   1
and not the number 3. (The addition would only be performed if the structure were passed as an argument to an appropriate procedure, such as is/2).

However, from a practical or a programmer's point of view, the existence of operators is highly desirable, and clearly handy.

Prolog syntax allows operators of three kinds: infix, prefix, and postfix. An infix operator appears between its two arguments, while a prefix operator precedes its single argument and a postfix operator follows its single argument.

Each operator has a precedence, which is an integer from 1 to 1200. The precedence is used to disambiguate expressions in which the structure of the term denoted is not made explicit through the use of parentheses. The general rule is that the operator with the highest precedence is the principal functor. Thus if '+' has a higher precedence than '/', then the following


                           a+b/c     a+(b/c)
are equivalent, and both denote the same term +(a,/(b,c)). Note that in this case, the infix form of the term /(+(a,b),c) must be written with explicit use of parentheses, as in:

                               (a+b)/c

If there are two operators in the expression having the same highest precedence, the ambiguity must be resolved from the types (and the implied associativity) of the operators. The possible types for an infix operator are


                          yfx     xfx     xfy
Operators of type 'xfx' are not associative. Thus, it is required that both of the arguments of the operator must be subexpressions of lower precedence than the operator itself; that is, the principal functor of each subexpression must be of lower precedence, unless the subexpression is written in parentheses (which automatically gives it zero precedence).

Operators of type 'xfy' are right-associative: only the first (left-hand) subexpression must be of lower precedence; the right-hand subexpression can be of the same precedence as the main operator. Left-associative operators (type 'yfx') are the other way around.

An atom named Name can be declared as an operator of type Type and precedence Precedence by the command;


                    :- op(Precedence, Type, Name).
The same command can be used to redefine one of the predefined XSB operators (see appendix B.3). However, it is not allowed to alter the definition of the comma ( ',') operator. An operator declaration can be cancelled by redeclaring the Name with the same Type, but Precedence 0.

As a notational convenience, the argument Name can also be a list of names of operators of the same type and precedence.

It is possible to have more than one operator of the same name, so long as they are of different kinds: infix, prefix, or postfix. An operator of any kind may be redefined by a new declaration of the same kind. Declarations for all these built-in operators can be found in appendix B.3. For example, the built-in operators '+' and '-' are as if they had been declared by the command:


                       :- op(500, yfx, [+,-]).
so that:

                                  1-2+3
is valid syntax, and denotes the compound term:

                                 (1-2)+3
or pictorially:

                                    +
                                   / \
                                  -   3
                                 / \
                                1   2

In XSB, the list functor '.'/2 is one of the standard operators, that can be thought as declared by the command:


                          :- op(661, xfy, .).
So, in XSB,

                                  1.2.[]
represents the structure

                                    .
                                   / \
                                  1   .
                                     / \
                                    2   []
Contrasting this picture with the picture above for 1-2+3 shows the difference between 'yfx' operators where the tree grows to the left, and 'xfy' operators where it grows to the right. The tree cannot grow at all for 'xfx' type operators. It is simply illegal to combine 'xfx' operators having equal precedences in this way.

If these precedence and associativity rules seem rather complex, remember that you can always use parentheses when in any doubt.

In XSB, at the time when this is written, the possible types for prefix operators are:


                      fx       fy       hx       hy
and the possible types for postfix operators are:

                               xf       yf

We end our discussion about operators by just mentioning that prefix operators of type hx and hy are proper HiLog operators. The discussion of proper HiLog operators and their properties is deferred for the manual of a future version.


next up previous contents index
Next: Using Tabling in XSB: Up: Syntax Previous: From HiLog to Prolog   Contents   Index
Baoqiu Cui
2000-04-23