next up previous contents index
Next: Character Conversion Up: Built-in predicates Previous: Representing text in strings   Contents   Index

Operators

Operators are defined to improve the readibility of source-code. For example, without operators, to write 2*3+4*5 one would have to write +(*(2,3),*(4,5)). In Prolog, a number of operators have been predefined. All operators, except for the comma (,) can be redefined by the user.

Some care has to be taken before defining new operators. Defining too many operators might make your source `natural' looking, but at the same time lead to hard to understand the limits of your syntax. To ease the pain, as of SWI-Prolog 3.3.0, operators are local to the module in which they are defined. The module-table of the module user acts as default table for all modules. This global table can be modified explictly from inside a module:


\begin{code}
:- module(prove,
[ prove/1
]).
\par :- op(900, xfx, user:(=>)).
\end{code}

Unlike what many users think, operators and quoted atoms have no relation: defining a atom as an operator does not influence parsing characters into atoms and quoting an atom does not stop it from acting as an operator. To stop an atom acting as an operator, enclose it in braces like this: (myop).

op3+Precedence, +Type, :Name Declare Name to be an operator of type Type with precedence Precedence. Name can also be a list of names, in which case all elements of the list are declared to be identical operators. Precedence is an integer between 0 and 1200. Precedence 0 removes the declaration. Type is one of: xf, yf, xfx, xfy, yfx, yfy, fy or fx. The `f' indicates the position of the functor, while x and y indicate the position of the arguments. `y' should be interpreted as ``on this position a term with precedence lower or equal to the precedence of the functor should occur''. For `x' the precedence of the argument must be strictly lower. The precedence of a term is 0, unless its principal functor is an operator, in which case the precedence is the precedence of this operator. A term enclosed in brackets (...) has precedence 0.

The predefined operators are shown in operators. Note that all operators can be redefined by the user.


Table: System operators
1200 xfx
1200 fx
1150 fx
1100 xfy
1050 xfy
1000 xfy
954 xfy
900 fy
900 fx
700 xfx
600 xfy
500 yfx
500 fx
400 yfx
200 xfx
200 xfy


current_op3?Precedence, ?Type, ?:Name Succeeds when Name is currently defined as an operator of type Type with precedence Precedence. See also op3.


next up previous contents index
Next: Character Conversion Up: Built-in predicates Previous: Representing text in strings   Contents   Index
Dr. Richard Botting 2001-12-12