Use of redundant goals in queries

Consider a query of a pure program1 ?- G_0. What use if any would the query ?- G_0, G_0. have?

I see no usefulness of the second goal, especially when tail recursion optimization (last call optimization) is ON.

I could realize an GC issue (stack/heap overflow) when the query is resources-greedy and above options are OFF (e.g. when debugging).

I think the second call is redundant (for pure program) and should be eliminated by the compiler.


The query ?- G_0, G_0. helps to identify redundant answers of ?- G_0.

To do so it suffices to compare the number of answers of ?- G_0. with the number of answers of ?- G_0, G_0.. No need to store those answers (which is a frequent source of errors anyway). Just two integers suffice! If they are equal, then there is no redundancy. But if ?- G_0, G_0. has more answers, then there is some redundancy. Here is an example:

p(f(_,a)).
p(f(b,_)).

?- p(X).
   X = f(_A, a)
;  X = f(b, _A).  % two answers

?- p(X), p(X).
   X = f(_A, a) 
;  X = f(b, a)
;  X = f(b, a)
;  X = f(b, _A).   % four answers
                   % thus p(X) contains redundancies

... and now let's fix this:

p(f(B,a)) :-
   dif(B, b).
p(f(b,_)).

?- p(X).
   X = f(_A, a), dif(_A, b)
;  X = f(b, _A).

?- p(X), p(X).
   X = f(_A, a), dif(_A, b), dif(_A, b).
;  X = f(b, _A).    % again two answers, thus no redundancy

No need to manually inspect the constraints involved.

This can be further extended when we are explicitly searching for redundant answers only using call_nth/2.

?- G_0, call_nth(G_0, 2).