Currently we only have assertion/1 from library(debug)
described as
assertion(:Goal)
which throws an exception with a (non-ISO standard) formal term assertion_error/2
if the Goal
fails.
In fact, the exception thrown in case of Goal
equal to false
is
error(assertion_error(fail,user:false),_4750)
This is similar to Java’s assert(expr)
(first added in Java 1.4 in 2002 only btw.)
I suppose assertion/1
can be compiled-out using expand_goal/2, if the Goal
is too expensive at runtime; I haven’t tried that yet.
It would be useful to have an assertion/2
described as:
assertion(:Goal,:Closure)
where Closure
is executed when Goal
fails with one additional uninstantiated argument, which shall be instantiated by the Closure, and which inserted into the second argument (the context term) of the error/2
term:
error(assertion_error(fail,user:false),T)
Then one could generate good error messages, similar to Java’s assert(expr,msg)
:
Example:
Call point:
assertion(X<Y,assertion_handler(X,Y)).
And handler:
assertion_handler(X,Y,T) :-
with_output_to(string(T),format("This is bad. ~q < ~q; this rocket's gonna crash!!",[X,Y])).