AndreaNobili
AndreaNobili

Reputation: 42967

Operator priority evaluation order in Prolog

If I define this operator:

op(700, yfx, sum).

700: rappresent the priority respect the other operators.

yfx: rappresent the precedence of arguments respect the operator itself. This configuration say that the operator is infix and that the argument y have precedence <= to the operatore priority and the argument x have precedence < of the operator priority.

The highest precedence is the principal functor of the therms, that means that isthe last operation to be executed..

So, it means that if I have the following evalutation:

9 sum 5 sum 7

so it means that I have a three in which first I evvaluate the value of 5 sum 7 and then I evaluate: 9 sum (5 sum 7)

Is it right my reasoning about the operator priority?

Upvotes: 0

Views: 492

Answers (1)

CapelliC
CapelliC

Reputation: 60024

I think the wording is different than that you're using:

700: Precedence. Lower binds more strictly.

yfx: Associativity to left.

?- write_canonical(1 sum 2 sum 3). yields sum(sum(1,2),3).

This operator associate to left, like the arithmetic binary operators:

?- setof(X-O,current_op(X,yfx,O),L),pairs_keys_values(L,_,Os).
L = [250- (?), 400- (*), 400- (/), 400- (//), 400- (<<), 400- (>>), 400- (div), 400- (mod), ... - ...|...],
Os = [?, *, /, //, <<, >>, div, mod, rdiv|...].

A practical way to inspect operator' relations is via unification.

?- (1 sum 2 sum 3) = (1 sum X).
false.

?- (1 sum 2 sum 3) = (X sum 3).
X = (1 sum 2).

Note the parenthesis are required (sum has higher precedence than unification (=)/2).

For predefined, systemic, operators see the doc page.

Upvotes: 2

Related Questions