Unification behavior in the prologue in the presence of arithmetic operators

12 ?- 3+4*5 = X+Y.
X = 3,
Y = 4*5.

13 ?- 3+4*5 = X*Y.
false.

16 ?- 3*4+5 = X*Y.
false.

I expected

13 ?- 3+4*5 = X*Y.
X = 3+4, Y = 5.

16 ?- 3*4+5 = X*Y.
X = 3, Y = 4+5.

Is there a "priority" problem? I am using the latest release of swi-prolog.

+3
source share
2 answers

Yes, there is a priority issue that you must consider.

Prolog binds a numerical priority value to each specific operator, so that its parsing can automatically process, for example, 3 + 4 * 5, just as if the brackets were used to denote 3+ (4 * 5).

So your first example worked as expected, but not the second or third. There was simply no way to unify terms, so Prolog returned false.

+3
source

All Articles