Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of redundant goals in queries

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

like image 566
false Avatar asked Feb 18 '20 19:02

false


People also ask

Why is redundant data bad in SQL?

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.

What is the process of introducing redundancy to improve query performance?

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.


1 Answers

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).
like image 123
false Avatar answered Sep 25 '22 22:09

false