开发者

behaviour of unification in prolog in 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 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜