scryer-prolog icon indicating copy to clipboard operation
scryer-prolog copied to clipboard

Avoid type errors in library(clpz)

Open bakaq opened this issue 2 years ago • 2 comments

I wish there was a way to force library(clpz) to fail instead of throwing type errors in queries like this:

?- A in 0..10, A = [].
   error(type_error(integer,[]),unknown([])-1).

This would be useful in constrained.pl (see https://github.com/bakaq/constrained.pl/issues/5#issuecomment-1740115280).

I think a nice way to selectively do this for my use case is to have some hook in library(clpz) to chose between error and failure. For example, it could check if the variable has an attribute clpz_fail(true), so the behavior would be as follows:

?- A in 0..10, A = [].
   error(type_error(integer,[]),unknown([])-1).
?- A in 0..10, clpz:put_atts(A, clpz_fail(true)), A = [].
   false.

(And now that I think about it, maybe a similar idea would be useful in #2041, so that you could mark individual variables as monotonic, not an entire module)

bakaq avatar Oct 01 '23 20:10 bakaq

IF/Prolog had exception_handler/3, but so far I have not seen this in any other system. Also, it is not that evident that systematic replacement of some kind of errors is such a good idea. What about the programming errors that are now replaced by silent failure.

What you suggest would imply that constraints are processed in a very specific order such that your constraint would be faster and lead to failure prior to other constraints. Sounds "reasonable", but I do not see a Prolog system with a good approach in this area.

UWN avatar Oct 06 '23 08:10 UWN

... but, imposing a global restriction would imply that one knows all other uses.

UWN avatar Oct 06 '23 09:10 UWN