Prolog LanguageOperators

Predefined operators

Predefined operators according to ISO/IEC 13211-1 and 13211-2:

1200xfx:- -->
1200fx:- ?-Directive, query
700xfx= \\=Term unification
700xfx== \\== @< @=< @> @>=Term comparison
700xfxis =:= =\= < > =< >=Arithmetic evaluation and comparison
600xfy:Module qualification
500yfx+ - /\ \/
400yfx* / div mod // rem << >>
200xfx**Float power
200xfy^Variable quantification, integer power
200fy+ - \Arithmetic identity, negation ; bitwise complement

Many systems provide further operators as an implementation specific extension:

1150fxdynamic multifile discontiguous initializationStandard directives
1150fxmode public volatile block meta_predicate
900fyspy nospy

Operator declaration

In Prolog, custom operators can be defined using op/3:

op(+Precedence, +Type, :Operator)

  • Declares Operator to be an operator of a Type with a Precedence. Operator 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, where 0 removes the declaration.

  • Type is one of: xf, yf, xfx, xfy, yfx, fy or fx where f indicates the position of the functor and x and y indicate the positions of the arguments. y denotes a term with a precedence lower or equal to the precedence of the functor, whereas x denotes a strictly lower precedence.

    • Prefix: fx , fy
    • Infix: xfx (not associative), xfy (right associative), yfx (left associative)
    • Postfix: xf , yf

Example usage:

:- op(900, xf, is_true).

X_0 is_true :-

Example query:

?- dif(X, a) is_true.
dif(X, a).

Term ordering

Two terms may be compared via the standard ordering:

variables @< numbers @< atoms @< strings @< structures @< lists


  • Structures compare alphabetically by functor first, then by arity and lastly by the comparison of each argument.

  • Lists compare by length first, then by each element.

Order operatorSucceeds if
X @< YX is less than Y in the standard order
X @> YX is greater than Y in the standard order
X @=< YX is less than or equal to Y in the standard order
X @>= YX is greater than or equal to Y in the standard order

Example queries:

?- alpha @< beta.

?- alpha(1) @< beta.

?- alpha(X) @< alpha(1).

?- alpha(X) @=< alpha(Y).

?- alpha(X) @> alpha(Y).

?- compound(z) @< compound(inner(a)).

Term equality

Equality operatorSucceeds if
X = YX can be unified with Y
X \= YX cannot be unified with Y
X == YX and Y are identical (i.e. they unify with no variable bindings occurring)
X \== YX and Y are not identical
X =:= YX and Y are arithmetically equal
X =\= YX and Y are not arithmetically equal