arithmetic in Prolog

筅森魡賤 提交于 2019-12-10 21:05:48

问题


If I have

A in 0..4, A * A #=A.

Is it possible that

A in 0..1.

?

I would say no because 0*0 = 0, and 1*1 = 1, so these two can not be A?

Thank you in advance.


回答1:


What you are asking here is about the consistency of CLPFD-systems. Generally speaking, systems try to maintain consistency "as good as they can" with different kinds of tradeoff to consistency vs. speed. But most of the time they deliver only safe approximations. In this case, however, everything seems perfect (here using SICStus):

| ?- A in 0..4, A * A #=A.
clpfd:(A*A#=A),
A in 0..1 ? ;
no

So, we get as an answer: Yes, there are solutions, provided clpfd(:A*A#=A), A in 0..1 is true. A priori we cannot say whether or not that is the case, but we might try it out:

| ?- A in 0..4, A * A #=A, A = 1.
A = 1 ? ;
no
| ?- A in 0..4, A * A #=A, A = 0.
A = 0 ? ;
no

So: Both 0 and 1 are a solution, and thus A in 0..1 is a perfect answer!

BTW, you get (essentially) the same answer in SICStus, SWI, B, and GNU.



来源:https://stackoverflow.com/questions/23714580/arithmetic-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!