문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top