Replies: 1 comment 1 reply
-
This sounds interesting! I remember many cases, especially when writing libraries, where I wanted to use I now tried to find a concrete example where this could be useful, and I think one suitable case appears in Line 73 in 6b8e620 With the mechanism you propose, we could simply write all this as Another example is from scryer-prolog/src/lib/crypto.pl Line 102 in 6b8e620 This could simply be written as must_be_bytes(Bytes, Context) :- must_be(list, Bytes), maplist(must_be(integer), Bytes), ( member(B, Bytes), \+ between(0, 255, B) -> type_error(byte, B, Context) ; true ). we would have: must_be_bytes(Bytes) :- must_be(list, Bytes), maplist(must_be(byte), Bytes). and |
Beta Was this translation helpful? Give feedback.
-
The control construct
catch/3
introduces often a lot of inefficiencies such that it is rarely used. Maybe this situation can be improved. Here are some sources of inefficiencies forcatch(mypred(A), error(type_error(_,_),_), false)
:mypred(A)
is copied onto the heap and thus ifA
was a local variable, that information is lost, executions is less efficient, in particular withset_prolog_flag(occurs_check, error)
All of this is not necessary for each and every use, but that's the way current implementations including Scryer do it.
As this is so expensive, some "less expensive" interfaces have been devised. Think of
type_error(Type, Culprit, mypred/1)
instead oftype_error(Type, Culprit)
. This goes at the cost of clarity of code. One has to hand around a context that is not really interesting. Lots of code becomes unnecessarily dependent on the current representation of that argument.Maybe it is possible to identify some use cases that do not need the full treatment of
catch/3
and that can be implemented much more directly. I am thinking oferror/2
. Typically to indicate the "official" predicate responsible for the error.The functionality of both could be defined as
But the point would be to implement this much more efficiently, by
throw/1
throw/1
-prior to copying the term, forfail_catch/2
, that copy would never be neededrewrite_catch/2
Beta Was this translation helpful? Give feedback.
All reactions