Arithmetic in Prolog

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 cannot be A?

Thanks in advance.

+3
source share
1 answer

Here you ask about the consistency of CLPFD systems. Generally speaking, systems try to maintain consistency "as best as possible" with different types of trade-offs to consistency and speed. But most of the time they deliver only safe approximations. In this case, however, everything seems perfect (SICStus is used here):

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

So, we get the answer: Yes, there are solutions if clpfd(:A*A#=A), A in 0..1 true. A priori, we cannot say whether this is true, but we can try:

 | ?- 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 the solution, and therefore A in 0..1 is the perfect answer!

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

+3
source

Source: https://habr.com/ru/post/1207993/


All Articles