Operators

2 + 1is an alternative way of writing the term

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

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

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 xfyOperators of type

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 (

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+3is valid syntax, and denotes the compound term:

(1-2)+3or 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

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 hyand 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.