Josh
Josh

Reputation: 121

Prolog - Operator in predicate

Is it possible to include operators (like +, >, =, !=, >= etc.) in argument for a predicate (examples below are just a demonstraition and do not have much use)?

test(A > B) :- A > B.
test(A >= B) :- A < B.

Seems to work, but:

test(A != B) := A > B.
test(A <> B) := A < B.

Does not - why is that? Why sometimes the operators can be included and sometimes not? How can I make test(A != B) := A > B. work?

I am working under sicstus.

Upvotes: 0

Views: 2077

Answers (1)

Per Mildner
Per Mildner

Reputation: 10487

The symbols !=, <> and := are not operators in Prolog. You need to make them operators, with the op/3 directive. The operator declaration must be seen by Prolog before it parses your code.

The character sequence != is not a token in Prolog, so you need to surround it with single quotes.

:- op(700, xfx, '!=').
:- op(700, xfx, <>).
:- op(1100, xfx, :=).

test(A '!=' B) := A > B.
test(A <> B) := A < B.

The above defines a predicate with two clauses. The clauses has no bodies, the name of the predicate is := and its arity 2. It is exactly the same as:

:=(test('!='(A,B)), >(A,B)).
:=(test(<>(A,B)), <(A,B)).

Upvotes: 3

Related Questions