Documentation

Provenance.QueryAdequacy

Data-part adequacy of the annotated semantics #

This file relates the annotated semantics Query.evaluateAnnotated to the plain semantics Query.evaluate, on the data parts: what happens when all annotations are forgotten.

For the positive (Diff-free) fragment the two semantics agree exactly, for any m-semiring K (Query.evaluateAnnotated_toPlain_of_noDiff). This is the counterpart, in the copies-as-elements encoding of annotated relations used here, of the -adequacy theorem of Benzaken, Cohen-Boulakia, Contejean, Keller & Zucchini, A Coq formalization of data provenance: in their function-with-support encoding of K-relations, tuple multiplicities must be carried by the annotations, so adequacy is a statement about K = ℕ; here multiplicities are carried by the multiset structure itself and the data-part statement is annotation-generic.

In the presence of difference the exact correspondence breaks down – necessarily so: over , monus-based difference and the all-or-nothing plain difference genuinely disagree once duplicate elimination has accumulated annotations (Nat.counterexample_diff_adequacy, at the end of this file). What survives is:

The inclusion cannot be strengthened to an equality on the tuples with non-zero annotation: nesting differences makes the support of the annotated result both under- and over-approximate the plain result.

Main definitions #

Main results #

References #

def Query.stripDiff {T : Type} {n : } :
Query T nQuery T n

Replace every difference node Diff q₁ q₂ of a query with its left argument q₁, recursively. Since the annotated semantics of Diff keeps every tuple slot of its left argument (only annotations change), the data part of the annotated evaluation of q is the plain evaluation of q.stripDiff (see Query.evaluateAnnotated_toPlain).

Equations
Instances For
    def Query.noDiff {T : Type} {n : } (q : Query T n) :

    The Diff-free (positive) fragment of the relational algebra, mirroring Query.noAgg.

    Equations
    Instances For
      theorem Query.stripDiff_of_noDiff {T : Type} {n : } (q : Query T n) (hd : q.noDiff) :

      On Diff-free queries, Query.stripDiff is the identity.

      def AnnotatedRelation.toPlain {T K : Type} {n : } [Zero K] (r : AnnotatedRelation T K n) :

      Forget the annotations of an annotated relation, keeping one tuple slot per annotated tuple (including slots whose annotation is 0). The return type is a bare Multiset (Tuple T n) so that Multiset lemmas apply without an extra unfolding step.

      Equations
      Instances For

        Forget the annotations of an annotated database, relation by relation.

        Equations
        Instances For

          Helper lemmas: Multiset.map Prod.fst commutes with the operators #

          All helpers are stated on bare Multiset (α × β) carriers, so that applying them by exact (full-transparency unification) sees through the AnnotatedRelation / ×ₗ type synonyms in the goals.

          Data-part equality. For any non-aggregation query q over any m-semiring K, forgetting the annotations of the annotated evaluation of q yields the plain evaluation of the stripped query q.stripDiff on the plain database: annotated Diff never removes tuple slots, so all the differences disappear from the data part.

          theorem Query.evaluate_le_stripDiff {T : Type} [ValueType T] {n : } (q : Query T n) :
          q.noAgg∀ (d : Database T), q.evaluate d q.stripDiff.evaluate d

          Plain evaluation is monotone (in the sub-multiset order) under stripping differences: the plain Diff is a filter of its left argument, and every other operator is monotone.

          Data-part inclusion. The plain evaluation of q on the underlying plain database is a sub-multiset of the data part of the annotated evaluation: annotated evaluation never loses tuple slots, it only zeroes annotations. The inclusion is strict in general (Diff slots whose annotation was zeroed remain on the right), and it cannot be strengthened to an equality on non-zero-annotated slots (see Nat.counterexample_diff_adequacy).

          Data-part adequacy for the positive fragment. On Diff-free (and aggregation-free) queries, the annotated semantics computes exactly the plain semantics on the data parts, for any m-semiring K. In the copies-as-elements encoding this is the analogue of the K = ℕ adequacy theorem of Benzaken, Cohen-Boulakia, Contejean, Keller & Zucchini (there, multiplicities live in the annotations, so the statement is -specific; here they live in the multiset).

          -adequacy genuinely fails beyond the positive fragment #

          The following counterexample shows that Query.evaluateAnnotated_toPlain_of_noDiff cannot be extended to queries with difference, not even on the tuples with non-zero annotation: over K = ℕ with all base annotations 1, the query (δ R₁) ∖ R₂ on R₁ = {t, t}, R₂ = {t} keeps t with annotation 2 ∸ 1 = 1 in the annotated semantics, while the plain semantics removes it. The Dedup is essential: it accumulates the annotation 2, which the monus difference then decrements instead of zeroing. This is why the -adequacy theorem of Benzaken, Cohen-Boulakia, Contejean, Keller & Zucchini is necessarily restricted to the positive fragment.

          @[implicit_reducible]

          Natural numbers as a value type (used for kernel-computable counterexamples: unlike String, the order on reduces by decide).

          Equations
          theorem Nat.counterexample_diff_adequacy :
          have t := ![0]; have r₁ := [(t, 1), (t, 1)]; have r₂ := [(t, 1)]; have d := [("R1", 1, r₁), ("R2", 1, r₂)]; have q := ε (Query.Rel 1 "R1") - Query.Rel 1 "R2"; ∃ (hq : q.noAgg), Multiset.map Prod.fst (Multiset.filter (fun (p : AnnotatedTuple 1) => p.2 0) (q.evaluateAnnotated hq d)) q.evaluate d.toPlain

          Over , the non-zero-annotated support of the annotated evaluation differs from the plain evaluation on a query combining Dedup and Diff: -adequacy stops at the positive fragment.