Documentation

CslibFrontier.DataStructure.ApproximateMembership.QuotientFilter.Operations

Pure Cluster-Level Insert and Delete Operations for the Quotient Filter #

This file defines the computable cluster-level state updates insertCluster and deleteCluster on FilterState b f, together with FilterInv.insertState and FilterInv.deleteState, and proves that they preserve the canonical quotient-filter invariant FilterInv.

Main definitions #

Main results #

Section 1: Primitive slot updates and shift loops on FilterState #

Update isOcc at bucket q.

Update isRunStart at slot q.

Update remAt at slot q.

@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.FilterState.remAt_setRem {b f : ℕ} (st : FilterState b f) (q p : Fin (2 ^ b)) (v : Fin (2 ^ f)) :
(st.setRem q v).remAt p = if p = q then v else st.remAt p

Tail-recursive right-shift of (isRunStart, remAt) on [s .. s + k) into [s + 1 .. s + k + 1) relative to o.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.shiftRight_outside {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (s k : ℕ) (p : Fin (2 ^ b)) (hp : ∀ d < k, p ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o (s + d + 1)) :
(shiftRight st o s k).isRunStart p = st.isRunStart p ∧ (shiftRight st o s k).remAt p = st.remAt p

Tail-recursive left-shift of (isRunStart, remAt) on [s + 1 .. s + k + 1) into [s .. s + k) relative to o.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.shiftLeft_outside {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (s k : ℕ) (p : Fin (2 ^ b)) (hp : ∀ d < k, p ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o (s + d)) :
(shiftLeft st o s k).isRunStart p = st.isRunStart p ∧ (shiftLeft st o s k).remAt p = st.remAt p

Section 2: Range filter helper #

Section 3: Computable run boundaries and target slot offsets #

Computable starting slot offset S_a of bucket a's run in cluster [o .. o + ℓ).

def Cslib.DataStructure.ApproximateMembership.QuotientFilter.calcTargetSlot {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (a ℓ : ℕ) (r : Fin (2 ^ f)) :

Computable target slot offset s for inserting or deleting remainder r in bucket a of cluster [o .. o + ℓ).

Prefix scan over m < i for the stopping offset of the left-shift during delete in cluster [o .. o + ℓ): the first m < i with a < m and occBefore st o m < runsBefore st o (m + 1), or ℓ if none exists in [0, i).

Computable stopping offset e ∈ (a, ℓ] for the left-shift during delete in cluster [o .. o + ℓ): the first m > a (m < ℓ) where occBefore st o m < runsBefore st o (m + 1), or ℓ if none exists.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.calcRunStart_eq {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) {a : ℕ} (ha : a ≤ ℓ) :
calcRunStart st o a ℓ = runOffset S quot rem o a
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.remAt_probeSlot_eq_getElem {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) {a : ℕ} (ha : a < ℓ) {d : ℕ} (hd : d < (bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o a)).length) :
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.calcTargetSlot_eq {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) {a : ℕ} (ha : a < ℓ) (r : Fin (2 ^ f)) :
calcTargetSlot st o a ℓ r = runOffset S quot rem o a + List.countP (fun (x : Fin (2 ^ f)) => decide (x < r)) (bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o a))
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.calcDeleteStop_spec {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) {a : ℕ} (ha : a < ℓ) :
have e := calcDeleteStop st o a ℓ; a < e ∧ e ≤ ℓ ∧ runOffset S quot rem o e = e ∧ ∀ (m : ℕ), a ≤ m → m + 1 < e → m + 2 ≤ runOffset S quot rem o (m + 1)

Section 4: Slot-stream list lemmas for shiftRight and shiftLeft #

Pair (isRunStart, remAt) stored at offset i from o.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.map_slotPair_shiftRight {b f : ℕ} (st₁ : FilterState b f) (o : Fin (2 ^ b)) {s ℓ : ℕ} (hs : s ≤ ℓ) (hℓ : ℓ < 2 ^ b) (v : Bool) (r : Fin (2 ^ f)) :
have st₂ := shiftRight st₁ o s (ℓ - s); have st₃ := (st₂.setRunStart (Dictionary.WordRAM.LinearProbing.probeSlot b o s) v).setRem (Dictionary.WordRAM.LinearProbing.probeSlot b o s) r; have L₁ := List.map (slotPair st₁ o) (List.range ℓ); List.map (slotPair st₃ o) (List.range (ℓ + 1)) = List.take s L₁ ++ (v, r) :: List.drop s L₁
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.map_slotPair_shiftLeft {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) {s e : ℕ} (hs : s < e) (he : e ≤ 2 ^ b) :
have st₁ := shiftLeft st o s (e - 1 - s); have M := List.map (slotPair st o) (List.range e); List.map (slotPair st₁ o) (List.range (e - 1)) = List.take s M ++ List.drop (s + 1) M
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.taggedRun_insert_split {f : ℕ} (xs : List (Fin (2 ^ f))) (r : Fin (2 ^ f)) {k : ℕ} (hk : k ≤ xs.length) :
have M₁ := if k = 0 then List.map (fun (x : Fin (2 ^ f)) => (false, x)) xs else taggedRun xs; List.take k M₁ ++ (decide (k = 0), r) :: List.drop k M₁ = taggedRun (List.take k xs ++ r :: List.drop k xs)

Section 5: IntervalValid combinators #

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.runOffset_congr {U b f : ℕ} {S S' : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {o : Fin (2 ^ b)} {m : ℕ} (h : ∀ j < m, bucketRun S' quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o j) = bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o j)) :
runOffset S' quot rem o m = runOffset S quot rem o m
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.congr {U b f : ℕ} {S S' : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st st' : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hbucket : ∀ j < ℓ, bucketRun S' quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o j) = bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o j)) (hfull : ∀ i < ℓ, st'.isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o i) = st.isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o i)) (hpair : ∀ i < ℓ, slotPair st' o i = slotPair st o i) :
IntervalValid S' quot rem st' o ℓ
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.runOffset_add {U b f : ℕ} (S : Finset (Fin U)) (quot : Fin U → Fin (2 ^ b)) (rem : Fin U → Fin (2 ^ f)) (o : Fin (2 ^ b)) (m₁ m₂ : ℕ) :
runOffset S quot rem o (m₁ + m₂) = runOffset S quot rem o m₁ + runOffset S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o m₁) m₂
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.append {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ₁ ℓ₂ : ℕ} (h₁ : IntervalValid S quot rem st o ℓ₁) (h₂ : IntervalValid S quot rem st (Dictionary.WordRAM.LinearProbing.probeSlot b o ℓ₁) ℓ₂) :
IntervalValid S quot rem st o (ℓ₁ + ℓ₂)
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.drop_of_runOffset_eq {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {e ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (he : e ≤ ℓ) (hrun_e : runOffset S quot rem o e = e) :

Section 6: Cluster-level insertCluster and deleteCluster #

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.calcTargetSlot_eq_of_empty {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) {a : ℕ} (ha : a ≤ ℓ) (ha_eq : a = ℓ → bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o a) = []) (r : Fin (2 ^ f)) :
calcTargetSlot st o a ℓ r = runOffset S quot rem o a + List.countP (fun (x : Fin (2 ^ f)) => decide (x < r)) (bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o a))

Pure cluster-level insertion of remainder r into bucket a ≤ ℓ of cluster [o .. o + ℓ).

Pure cluster-level deletion of remainder r from bucket a < ℓ of cluster [o .. o + ℓ).

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.isOcc_deleteCluster_of_ne {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (a ℓ : ℕ) (r : Fin (2 ^ f)) {p : Fin (2 ^ b)} (hp : p ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o a) :
(deleteCluster st o a ℓ r).isOcc p = st.isOcc p
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.deleteCluster_at_end {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (a ℓ : ℕ) (r : Fin (2 ^ f)) :
have p_end := Dictionary.WordRAM.LinearProbing.probeSlot b o (calcDeleteStop st o a ℓ - 1); (deleteCluster st o a ℓ r).isRunStart p_end = false ∧ (deleteCluster st o a ℓ r).remAt p_end = ⟨0, ⋯⟩
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.flatMap_taggedRun_three_split {U b f : ℕ} (S : Finset (Fin U)) (quot : Fin U → Fin (2 ^ b)) (rem : Fin U → Fin (2 ^ f)) (o : Fin (2 ^ b)) {a n : ℕ} (ha : a < n) :
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.take_append_drop_middle {α : Type u_1} (pref mid suff : List α) (x : α) {k : ℕ} (hk : k ≤ mid.length) :
List.take (pref.length + k) (pref ++ mid ++ suff) ++ x :: List.drop (pref.length + k) (pref ++ mid ++ suff) = pref ++ (List.take k mid ++ x :: List.drop k mid) ++ suff
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.take_append_drop_succ_middle {α : Type u_1} (pref mid suff : List α) {k : ℕ} (hk : k < mid.length) :
List.take (pref.length + k) (pref ++ mid ++ suff) ++ List.drop (pref.length + k + 1) (pref ++ mid ++ suff) = pref ++ (List.take k mid ++ List.drop (k + 1) mid) ++ suff
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.set_middle_zero {α : Type u_1} (pref : List α) (x₀ : α) (xs suff : List α) (y : α) :
(pref ++ x₀ :: xs ++ suff).set pref.length y = pref ++ y :: xs ++ suff
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.insertCluster_intervalValid {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) (hℓ : ℓ < 2 ^ b) (hempty_ℓ : bucketRun S quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o ℓ) = []) {u : Fin U} (hu : u ∉ S) {a : ℕ} (ha : a ≤ ℓ) (hq : quot u = Dictionary.WordRAM.LinearProbing.probeSlot b o a) :
IntervalValid (insert u S) quot rem (insertCluster st o a ℓ (rem u)) o (ℓ + 1)
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.take_of_runOffset_eq {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {e ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (he : e ≤ ℓ) (hrun_e : runOffset S quot rem o e = e) :
IntervalValid S quot rem st o e
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.IntervalValid.deleteCluster_intervalValid {U b f : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} {o : Fin (2 ^ b)} {ℓ : ℕ} (hinv : IntervalValid S quot rem st o ℓ) (hocc : ∀ (q : Fin (2 ^ b)), st.isOcc q = !(bucketRun S quot rem q).isEmpty) (hℓ : ℓ ≤ 2 ^ b) {u : Fin U} (hu : u ∈ S) {a : ℕ} (ha : a < ℓ) (hq : quot u = Dictionary.WordRAM.LinearProbing.probeSlot b o a) :
have e := calcDeleteStop st o a ℓ; have st' := deleteCluster st o a ℓ (rem u); IntervalValid (S.erase u) quot rem st' o (e - 1) ∧ st'.isOcc (Dictionary.WordRAM.LinearProbing.probeSlot b o a) = !(bucketRun (S.erase u) quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o a)).isEmpty ∧ bucketRun (S.erase u) quot rem (Dictionary.WordRAM.LinearProbing.probeSlot b o (e - 1)) = []

Section 7: Global FilterInv preservation under insert and delete #

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.maximal_full_eq_or_disjoint {b : ℕ} (isFull : Fin (2 ^ b) → Bool) {o₁ o₂ : Fin (2 ^ b)} {ℓ₁ ℓ₂ : ℕ} (hback₁ : isFull (Dictionary.WordRAM.LinearProbing.backSlot b o₁ 1) = false) (hend₁ : isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o₁ ℓ₁) = false) (hfull₁ : ∀ i < ℓ₁, isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o₁ i) = true) (hback₂ : isFull (Dictionary.WordRAM.LinearProbing.backSlot b o₂ 1) = false) (hend₂ : isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o₂ ℓ₂) = false) (hfull₂ : ∀ j < ℓ₂, isFull (Dictionary.WordRAM.LinearProbing.probeSlot b o₂ j) = true) :
o₁ = o₂ ∧ ℓ₁ = ℓ₂ ∨ ∀ i ≤ ℓ₁, ∀ j < ℓ₂, Dictionary.WordRAM.LinearProbing.probeSlot b o₁ i ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o₂ j
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.insertCluster_outside {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) (a ℓ : ℕ) (r : Fin (2 ^ f)) (p : Fin (2 ^ b)) (hp : ∀ i ≤ ℓ, p ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o i) :
(insertCluster st o a ℓ r).isRunStart p = st.isRunStart p ∧ (insertCluster st o a ℓ r).remAt p = st.remAt p
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.deleteCluster_outside {b f : ℕ} (st : FilterState b f) (o : Fin (2 ^ b)) {a ℓ : ℕ} (ha : a < calcDeleteStop st o a ℓ) (r : Fin (2 ^ f)) (p : Fin (2 ^ b)) (hp : ∀ i < calcDeleteStop st o a ℓ, p ≠ Dictionary.WordRAM.LinearProbing.probeSlot b o i) :
(deleteCluster st o a ℓ r).isRunStart p = st.isRunStart p ∧ (deleteCluster st o a ℓ r).remAt p = st.remAt p
noncomputable def Cslib.DataStructure.ApproximateMembership.QuotientFilter.FilterInv.insertState {U b f n : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} (hinv : FilterInv n S quot rem st) (hS_lt : S.card < 2 ^ b) (u : Fin U) :

Pure quotient-filter state update for insert u.

noncomputable def Cslib.DataStructure.ApproximateMembership.QuotientFilter.FilterInv.deleteState {U b f n : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} (hinv : FilterInv n S quot rem st) (hS_lt : S.card < 2 ^ b) (u : Fin U) :

Pure quotient-filter state update for delete u.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.FilterInv.insertState_filterInv {U b f n : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} (hinv : FilterInv n S quot rem st) {u : Fin U} (hu : u ∉ S) (hcap : (insert u S).card ≤ n) (hS'_lt : (insert u S).card < 2 ^ b) :
FilterInv n (insert u S) quot rem (hinv.insertState ⋯ u)

Global FilterInv preservation under insert: inserting u ∉ S (when #(insert u S) ≤ n and #(insert u S) < 2 ^ b) preserves FilterInv.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.FilterInv.deleteState_filterInv {U b f n : ℕ} {S : Finset (Fin U)} {quot : Fin U → Fin (2 ^ b)} {rem : Fin U → Fin (2 ^ f)} {st : FilterState b f} (hinv : FilterInv n S quot rem st) {u : Fin U} (hu : u ∈ S) (hS_lt : S.card < 2 ^ b) :
FilterInv n (S.erase u) quot rem (hinv.deleteState hS_lt u)

Global FilterInv preservation under delete: deleting u ∈ S (when #S < 2 ^ b) preserves FilterInv.