Use of redundant goals in queries

false :

(Upon the suggestion of @repeat) Consider a query of a pure program1 ?- G_0. What use if any would the query ?- G_0, G_0. have?

Footnotes
1 No tabling (to be safe), constraints are OK.

false :

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).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29401&siteId=1