projog

3.74. op(X,Y,Z)

Allows functors (names of predicates) to be defined as "operators". The use of operators allows syntax to be easier to write and read. Z is the atom that we want to be an operator, X is the precedence class (an integer), and Y the associativity specifier. e.g. op(1200,xfx,':-')

Examples

'~'(X,Y) :- X>Y-4, X<Y+4.

?- op(1000,xfx,'~').

yes

?- 4 ~ 7.

yes

?- 7 ~ 7.

yes

?- 10 ~ 7.

yes

?- 11 ~ 7.

no

?- 3 ~ 7.

no

Example of invalid arguments

?- op(X,xfx,'><').

Expected Numeric but got: VARIABLE with value: X

?- op(1000,Y,'><').

Expected an atom but got: VARIABLE with value: Y

?- op(1000,xfx,Z).

Expected an atom but got: VARIABLE with value: Z

?- op(1000,zfz,'><').

Cannot add operand with associativity of: zfz as the only values allowed are: [xfx, xfy, yfx, fx, fy, xf, yf]

Create some prefix and postfix operators for the later examples below.

?- op(550, fy, 'fyExample').

yes

?- op(650, fx, 'fxExample').

yes

?- op(600, yf, 'yfExample').

yes

?- op(500, xf, 'xfExample').

yes

Example of nested prefix operators.

?- X = fxExample fyExample fyExample a, write_canonical(X), nl.
fxExample(fyExample(fyExample(a)))
X = fxExample fyExample fyExample a

yes

Example of a postfix operator.

?- X = 123 yfExample, write_canonical(X), nl.
yfExample(123)
X = 123 yfExample

yes

Example of nested postfix operators.

?- X = a xfExample yfExample yfExample, write_canonical(X), nl.
yfExample(yfExample(xfExample(a)))
X = a xfExample yfExample yfExample

yes

Example of combining post and prefix operators where the postfix operator has the higher precedence.

?- X = fyExample a yfExample, write_canonical(X), nl.
yfExample(fyExample(a))
X = fyExample a yfExample

yes

Example of combining post and prefix operators where the prefix operator has the higher precedence.

?- op(699, fy, 'fyExampleB').

yes

?- X = fyExampleB a yfExample, write_canonical(X), nl.
fyExampleB(yfExample(a))
X = fyExampleB a yfExample

yes

Examples of how an "x" in an associativity (i.e. "fx" or "xf") means that the argument can contain operators of only a lower level of priority than the operator represented by "f".

?- X = a xfExample xfExample.

Operator priority clash. xfExample (500) conflicts with previous priority (500) Line: X = a xfExample xfExample.

?- X = fxExample fxExample a.

Operator priority clash. fxExample (650) conflicts with previous priority (650) Line: X = fxExample fxExample a.