Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement suggestion: bagof/3 has the possibility to "break off with failure early" in some cases #700

Closed
dtonhofer opened this issue Oct 26, 2020 · 4 comments

Comments

@dtonhofer
Copy link
Sponsor Contributor

dtonhofer commented Oct 26, 2020

This is most probably a side-effect from developing bagof/3 to be "steadfast": bagof/3 (and most likely setof/3) does not use information that could help it to decide early that it will eventually fail.

This makes sense:

?- bagof(X,between(1,inf,X),Bag).
ERROR: Stack limit (1.0Gb) exceeded

But in this case, bagof/3 could break off at the 3rd or 4th backtracking over between/3 and fail as at that point it can notice it's running out of space and success is forever out of reach:

?- bagof(X,between(1,inf,X),[A,B,C,D]).
ERROR: Stack limit (1.0Gb) exceeded

It could give up early for more limited cases, too:

?- bagof(X,(between(1,10,X),format("~q\n",[X])),[A,B,C,D]).
1
2
3
4
5
6
7
8
9
10
false.

The only thing remaining from the above calls-that-will-fail are the side-effects and CPU time & energy. I hope a programmer would not rely on obtaining and exploiting side-effects from a predicate that fails, so "failing early" would not lose friends.

Performing checks to see whether the receiving term is a closed list that is too small will soak up a few cycles too and make the code more complex...

@JanWielemaker
Copy link
Member

For bagof/3 the stuff is more complicated as filtering based on free variables splits the result set. For setof/3 we have to actively check for duplicates (which may be worthwhile, notably to save space). For findall/3 this is all a bit simpler (and bagof/3 with no free variables simply translates to findall/3 demanding the result set to be non-empty).

All in all I have my doubt this is worthwhile. I rarely see the output argument of these predicates instantiated. If it is, scanning the list may add arbitrary complexity to the goal (consider a long list and a goal that produced zero or just a few answers).

In the few cases this is really needed you could define your own findall/3 as

myfindall(Templ, Goal, List) :-
    is_list(List),
    !,
    length(List, Limit),
    findall(Templ, limit(Limit, Goal), List).
myfindall(Templ, Goal, List) :-
    findall(Templ, Goal, List).

@dtonhofer
Copy link
Sponsor Contributor Author

Yes. Checking for available list length would only make sense if the "collector" part could verify, just before the next call to the generating goal, whether there actually is still space in the list. Preliminary full scanning, on the other hand, would be bad.

@JanWielemaker
Copy link
Member

Preliminary full scanning, on the other hand, would be bad.

It is the only sort-of realistic option considering we are backtracking each time to get the next solution. Not really claiming no hack is possible, but it will get ugly and IMHO not worth the trouble.

@dtonhofer
Copy link
Sponsor Contributor Author

Probably not. Thank you. Let's close this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants