CompilerSaysNo
CompilerSaysNo

Reputation: 415

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 can not be A?

Thank you in advance.

Upvotes: 3

Views: 163

Answers (1)

false
false

Reputation: 10142

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.

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.
?- A in 0..4, A * A #=A, A = 0.
   A = 0.

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.

Upvotes: 3

Related Questions