(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.
Previous post on the subject.
Redundant data is a bad idea because when you modify data (update/insert/delete), then you need to do it in more than one place. This opens up the possibility that the data becomes inconsistent across the database. The reason redundancy is sometimes necessary is for performance reasons.
Denormalization is the process of adding precomputed redundant data to an otherwise normalized relational database to improve read performance of the database. Normalizing a database involves removing redundancy so only a single copy exists of each piece of information.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With