behaviour of unification in prolog in presence of arithmetic operators
-
26-10-2019 - |
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.
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.
OTHER TIPS
Yes, it's something about it
Each operator have Precedence. the precedence of + is 500, and the precedence of * 400
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow