behaviour of unification in prolog in presence of arithmetic operators
-
26-10-2019 - |
문제
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.
해결책
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.
다른 팁
Yes, it's something about it
Each operator have Precedence. the precedence of + is 500, and the precedence of * 400
제휴하지 않습니다 StackOverflow