Difference between revisions of "Warning c666: Due to the range of the involved domain this test always succeeds"
(Initial) |
(No difference)
|
Revision as of 12:15, 18 November 2008
This warning (introduced in Visual Prolog 7.2) is rather important and should not be neglected.
The warning will be given for code like this:
domains positive = integer [0..]. predicates ppp : (integer X). qqq : (positive Y). clauses ppp(X) :- Y = X - 13, Y >= 0, % w666 !, qqq(Y). ppp(_X).
The test Y >= 0 is clearly intended to ensure that qqq is not called with a negative value.
But in reality the code works in a quite different way than the programmer intended.
The type analysis see that Y must have the type positive, and therefore Y must be in the range [0..]. To ensure this property of Y the compiler inserts a range check at the point where Y is calculated. So if X - 13 is negative an out-of-range exception is raised. Subsequently, the test Y >= 0 will always succeed. And this it what is meant by: Due to the range of the involved domain this test always succeeds.
I.e. Y has type positive and therefore the test always succeeds.
The warning very is important, because the programmer thought that the code would fail if Y was negative, but in reality an exception is raised.
A good way to eliminate the warning and get the intended behavior (i.e. fail to the next clause) is by using tryConvert, like this:
clauses ppp(X) :- Y = tryConvert(positive, X - 13), !, qqq(Y). ppp(_X).
This code tests whether the conversion to positive is possible, rather than testing with the boundary.