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

library(error), missing error, module qualification?

Open UWN opened this issue 3 years ago • 5 comments

:- meta_predicate(resource_error(+,:)).
resource_error(Resource, Context) :-
   throw(error(resource_error(Resource), Context)).

UWN avatar Feb 12 '22 17:02 UWN

Is this predicate useful on the Prolog-level? This seems to be an error that would most likely be thrown by the engine itself instead of by Prolog programs (within Prolog code). For example, there seems no way to tell, within Prolog, whether there is sufficient memory to allocate a term. Is there currently a situation where a Prolog program could throw this error by itself and would already benefit from this predicate? Maybe if it itself catches such an error that is generated by the engine, and then wants to throw a resource error itself, is it intended for such situations?

triska avatar Feb 12 '22 17:02 triska

See the definition of length/2. Like length(L,L). Here, it is evident that this computation will take infinitely long. And thus it is legitimate to shorten the time by throwing immediately a resource error.

UWN avatar Feb 12 '22 17:02 UWN

And what about the module qualification? How else is it possible to tell which definition is meant?

UWN avatar Feb 13 '22 16:02 UWN

Like length(L,L). Here, it is evident that this computation will take infinitely long.

I don't understand why this is the case. I think the obvious thing to do here would be to make the predicate fail, because L needs to be both a list and an integer, which is impossible. This doesn't seem to clash with the declarative description of length/2 given here.

bakaq avatar Dec 13 '23 03:12 bakaq

This doesn't seem to clash with the declarative description of length/2

Right. But it has undesirable properties when porting a program from one implementation to another. The use of Brent or (previously) Floyd is relatively recent, like 2007. Also, making such an ad hoc improvement, when related predicates don't, seems to be quite odd. Think of ?- append(L,L,L).

And, lest I forget, length(L,L) can only produce that resource error immediately, if L is unattributed.

UWN avatar Dec 13 '23 15:12 UWN