Documentation

Complexity.Algorithms.Lean.FusionTree.Correction

Arbitrary-query sketch correction #

Correct the rank of an arbitrary query using the actual significant-bit sketch and the two stored keys neighboring its inclusive sketch rank. Exact hits return that rank directly; other queries use a second sketch rank at a prefix boundary. No word-RAM running-time bound is asserted.

Main definitions #

Main results #

References #

See [FW93] and [MIT 6.851, Spring 2021, Lecture 12, handwritten page 4] (https://courses.csail.mit.edu/6.851/spring21/lectures/L12.pdf).

theorem Cslib.Algorithms.Lean.FusionTree.highestDiff_mem_relevantBits_of_lt {n w : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) {i j : Fin n} (hij : i < j) :
BitString.highestDiff w (keys i) (keys j) ∈ relevantBits w keys

Every pair of distinct stored keys has its highest differing bit among the adjacent-pair bits, even when the keys are not adjacent.

theorem Cslib.Algorithms.Lean.FusionTree.sketch_lt_query_of_prefix {n w cut q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) {i j : Fin n} (hij : i < j) (hprefix : keys j >>> cut = q >>> cut) (hne : keys i >>> cut ≠ q >>> cut) :

A query sharing the larger stored key's prefix lies strictly above every sketch of a smaller stored key outside that prefix block.

theorem Cslib.Algorithms.Lean.FusionTree.query_lt_sketch_of_prefix {n w cut q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) {i j : Fin n} (hij : i < j) (hprefix : keys i >>> cut = q >>> cut) (hne : keys j >>> cut ≠ q >>> cut) :

A query sharing the smaller stored key's prefix lies strictly below every sketch of a larger stored key outside that prefix block.

def Cslib.Algorithms.Lean.FusionTree.upperRank {n : ℕ} (values : Fin n → ℕ) (q : ℕ) :

The inclusive rank is the number of stored values at most the query.

Instances For
    def Cslib.Algorithms.Lean.FusionTree.lowerRank {n : ℕ} (values : Fin n → ℕ) (q : ℕ) :

    The strict rank is the number of stored values strictly below the query.

    Instances For
      theorem Cslib.Algorithms.Lean.FusionTree.upperRank_le {n : ℕ} (values : Fin n → ℕ) (q : ℕ) :
      upperRank values q ≤ n

      An inclusive rank is a cut between zero and the number of stored values.

      theorem Cslib.Algorithms.Lean.FusionTree.lt_upperRank_iff {n q : ℕ} {values : Fin n → ℕ} (hsorted : StrictMono values) (i : Fin n) :
      ↑i < upperRank values q ↔ values i ≤ q

      In a sorted sequence, inclusive rank exactly characterizes the indices below its cut.

      def Cslib.Algorithms.Lean.FusionTree.IsSketchNeighbor {n : ℕ} (w : ℕ) (keys : Fin n → ℕ) (q : ℕ) (i : Fin n) :

      The immediate sketch neighbors of the inclusive query cut. At the endpoints there is only one neighbor; in an empty sequence there are none.

      Instances For
        theorem Cslib.Algorithms.Lean.FusionTree.exists_sketchNeighbor_prefix {n w q cut : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hprefix : ∃ (i : Fin n), keys i >>> cut = q >>> cut) :
        ∃ (j : Fin n), IsSketchNeighbor w keys q j ∧ keys j >>> cut = q >>> cut

        Every nonempty full-prefix block contains an immediate sketch neighbor of the query. The query is arbitrary: it need not be a stored key or have a faithful sketch rank.

        theorem Cslib.Algorithms.Lean.FusionTree.exists_sketchNeighbor_min_highestDiff {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hn : 0 < n) (hmiss : ∀ (i : Fin n), keys i ≠ q) :
        ∃ (j : Fin n), IsSketchNeighbor w keys q j ∧ ∀ (i : Fin n), BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q

        For a query unequal to every stored key, an immediate sketch neighbor attains the longest full common prefix, expressed as the minimum highest differing position.

        theorem Cslib.Algorithms.Lean.FusionTree.min_highestDiff_of_sketchNeighbor {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hmiss : ∀ (i : Fin n), keys i ≠ q) (j : Fin n) (hbest : ∀ (i : Fin n), IsSketchNeighbor w keys q i → BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q) (i : Fin n) :

        Minimizing over the at most two sketch neighbors therefore minimizes over every key.

        theorem Cslib.Algorithms.Lean.FusionTree.testBit_ne_of_min_highestDiff {n w q : ℕ} {keys : Fin n → ℕ} (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hmiss : ∀ (i : Fin n), keys i ≠ q) (j : Fin n) (hmin : ∀ (i : Fin n), BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q) (i : Fin n) (hprefix : keys i >>> (BitString.highestDiff w (keys j) q + 1) = q >>> (BitString.highestDiff w (keys j) q + 1)) :
        (keys i).testBit (BitString.highestDiff w (keys j) q) ≠ q.testBit (BitString.highestDiff w (keys j) q)

        Every key sharing the query's prefix above the globally minimum differing bit differs from the query at that bit. Thus none follows the query's branch there.

        theorem Cslib.Algorithms.Lean.FusionTree.le_iff_sketch_le_lowerBoundary {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hmiss : ∀ (i : Fin n), keys i ≠ q) (j : Fin n) (hmin : ∀ (i : Fin n), BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q) (hbit : q.testBit (BitString.highestDiff w (keys j) q) = true) (i : Fin n) :

        If the query takes the one branch at its first divergence from a longest-prefix key, the true inclusive comparison equals the inclusive sketch comparison at the lower boundary.

        theorem Cslib.Algorithms.Lean.FusionTree.le_iff_sketch_lt_upperBoundary {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hmiss : ∀ (i : Fin n), keys i ≠ q) (j : Fin n) (hmin : ∀ (i : Fin n), BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q) (hbit : q.testBit (BitString.highestDiff w (keys j) q) = false) (i : Fin n) :

        If the query takes the zero branch at its first divergence from a longest-prefix key, the true inclusive comparison equals the strict sketch comparison at the upper boundary.

        def Cslib.Algorithms.Lean.FusionTree.boundaryRank {n : ℕ} (w : ℕ) (keys : Fin n → ℕ) (q : ℕ) (j : Fin n) :

        The second sketch query, with the comparison convention determined by the divergence bit.

        Instances For
          theorem Cslib.Algorithms.Lean.FusionTree.boundaryRank_eq_upperRank {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) (hmiss : ∀ (i : Fin n), keys i ≠ q) (j : Fin n) (hbest : ∀ (i : Fin n), IsSketchNeighbor w keys q i → BitString.highestDiff w (keys j) q ≤ BitString.highestDiff w (keys i) q) :
          boundaryRank w keys q j = upperRank keys q

          The corrected sketch rank equals the true inclusive rank when j minimizes divergence among the immediate sketch neighbors. This hypothesis is a comparison of at most two keys.

          theorem Cslib.Algorithms.Lean.FusionTree.upperRank_apply {n : ℕ} {values : Fin n → ℕ} (hsorted : StrictMono values) (i : Fin n) :
          upperRank values (values i) = ↑i + 1

          The inclusive rank of a stored key is its index plus one.

          theorem Cslib.Algorithms.Lean.FusionTree.upperRank_sketch_apply {n w : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (i : Fin n) :
          upperRank (fun (j : Fin n) => BitString.sketch (relevantBits w keys) (keys j)) (BitString.sketch (relevantBits w keys) (keys i)) = upperRank keys (keys i)

          An exact hit has its true inclusive rank already, before correction.

          def Cslib.Algorithms.Lean.FusionTree.sketchRank {n : ℕ} (w : ℕ) (keys : Fin n → ℕ) (q : ℕ) :

          Initial inclusive rank of the uncorrected query sketch.

          Instances For

            The predecessor of the inclusive sketch cut, when present.

            Instances For
              def Cslib.Algorithms.Lean.FusionTree.sketchSuccessor {n : ℕ} (w : ℕ) (keys : Fin n → ℕ) (q : ℕ) :

              The successor of the inclusive sketch cut, when present.

              Instances For
                theorem Cslib.Algorithms.Lean.FusionTree.sketchPredecessor_eq_some_iff {n w q : ℕ} {keys : Fin n → ℕ} (i : Fin n) :
                sketchPredecessor w keys q = some i ↔ ↑i + 1 = sketchRank w keys q

                The predecessor is precisely index t-1, using the inclusive convention.

                theorem Cslib.Algorithms.Lean.FusionTree.sketchSuccessor_eq_some_iff {n w q : ℕ} {keys : Fin n → ℕ} (i : Fin n) :
                sketchSuccessor w keys q = some i ↔ ↑i = sketchRank w keys q

                The successor is precisely index t, using the inclusive convention.

                theorem Cslib.Algorithms.Lean.FusionTree.isSketchNeighbor_iff {n w q : ℕ} {keys : Fin n → ℕ} (i : Fin n) :

                The two optional candidates are exactly the immediate sketch neighbors.

                def Cslib.Algorithms.Lean.FusionTree.chooseByScore {α : Type u_1} (score : α → ℕ) :
                Option α → Option α → Option α

                Compare at most two optional candidates by a natural-number score.

                Instances For
                  theorem Cslib.Algorithms.Lean.FusionTree.chooseByScore_spec {α : Type u_1} (score : α → ℕ) {a b : Option α} {j : α} (h : chooseByScore score a b = some j) :
                  (a = some j ∨ b = some j) ∧ ∀ (i : α), a = some i ∨ b = some i → score j ≤ score i

                  A chosen candidate belongs to the input pair and has minimum score among that pair.

                  theorem Cslib.Algorithms.Lean.FusionTree.chooseByScore_eq_none_iff {α : Type u_1} (score : α → ℕ) (a b : Option α) :

                  No candidate is selected exactly when both inputs are absent.

                  Exact hits score zero; other keys are scored by their highest differing bit plus one.

                  Instances For

                    Select a longest-prefix candidate from the two sketch neighbors, preferring exact hits.

                    Instances For
                      theorem Cslib.Algorithms.Lean.FusionTree.bestSketchNeighbor_spec {n w q : ℕ} {keys : Fin n → ℕ} {j : Fin n} (h : bestSketchNeighbor w keys q = some j) :
                      IsSketchNeighbor w keys q j ∧ ∀ (i : Fin n), IsSketchNeighbor w keys q i → divergenceScore w (keys j) q ≤ divergenceScore w (keys i) q

                      The selected neighbor minimizes the divergence score among the two candidates.

                      A nonempty node always has at least one of the two sketch neighbors.

                      theorem Cslib.Algorithms.Lean.FusionTree.isSketchNeighbor_of_eq {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (i : Fin n) (hi : keys i = q) :
                      IsSketchNeighbor w keys q i

                      An exact stored hit is the predecessor of the inclusive sketch cut.

                      theorem Cslib.Algorithms.Lean.FusionTree.miss_of_bestSketchNeighbor {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) {j : Fin n} (hj : bestSketchNeighbor w keys q = some j) (hmiss : keys j ≠ q) (i : Fin n) :
                      keys i ≠ q

                      If the selected candidate is not an exact hit, then no stored key is an exact hit. Only the two neighboring candidates are inspected by the selection function.

                      theorem Cslib.Algorithms.Lean.FusionTree.bestSketchNeighbor_highestDiff_le {n w q : ℕ} {keys : Fin n → ℕ} (hmiss : ∀ (i : Fin n), keys i ≠ q) {j : Fin n} (hj : bestSketchNeighbor w keys q = some j) (i : Fin n) (hi : IsSketchNeighbor w keys q i) :

                      Away from exact hits, the two-candidate selector minimizes the highest differing bit.

                      def Cslib.Algorithms.Lean.FusionTree.correctedRank {n : ℕ} (w : ℕ) (keys : Fin n → ℕ) (q : ℕ) :

                      Arbitrary-query correction using two sketch neighbors. Exact hits return the initial inclusive sketch rank directly. Otherwise a second sketch rank uses the appropriate boundary. After taking sketch ranks, candidate selection and correction use at most two full-key values. The sketches and rank primitives here are mathematical specifications, without a RAM-time bound.

                      Instances For
                        theorem Cslib.Algorithms.Lean.FusionTree.correctedRank_eq_upperRank {n w q : ℕ} {keys : Fin n → ℕ} (hsorted : StrictMono keys) (hbound : ∀ (i : Fin n), keys i < 2 ^ w) (hq : q < 2 ^ w) :
                        correctedRank w keys q = upperRank keys q

                        For every bounded query, correction of the actual significant-bit sketch gives the true inclusive rank among the stored keys. This includes empty nodes, boundary cuts, and exact hits.