Question

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 was expecting

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

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

Is there some "precedence" problem? I'm using the last swi-prolog release.

Was it helpful?

Solution

Yes, there is a precedence issue that you need to take into account.

Prolog attaches a numeric precedence value to each operator defined, so that its parse can automatically treat, e.g., 3+4*5 the same as if parentheses had been used to state 3+(4*5).

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top