Replica.lean

Lean 4 source, lean/DataMiningAsObservation/Replica.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
Replica.lean. Chapter 13 section 13.3, the replica.

A replica trails the primary by a lag, and a read of a key written within the lag returns a
stale value. The set of stale keys grows with the lag, so the stale fraction is monotone in
it. A naive certificate that clears every read has coverage one and a false-clear rate equal
to the stale fraction, which is the joint rate, and a witness-backed certificate that clears
only reads of keys outside the lag has a false-clear rate of zero at the price of coverage.
The measured rates on ZooKeeper, Postgres, and MongoDB are the ledger's numbers.
-/
import Mathlib
import DataMiningAsObservation.Certificate

namespace DMO.Replica

open Finset

variable {κ : Type*} [DecidableEq κ]

/-- The keys stale under lag `lag`, those whose last write is within the lag. -/
noncomputable def stale (K : Finset κ) (age : κ → ℝ) (lag : ℝ) : Finset κ := K.filter fun k => age k < lag

omit [DecidableEq κ] in
/-- **More lag, more stale keys.** -/
theorem stale_mono (K : Finset κ) (age : κ → ℝ) (lag lag' : ℝ) (h : lag ≤ lag') :
    stale K age lag ⊆ stale K age lag' :=
  Finset.monotone_filter_right K fun _ _ hk => lt_of_lt_of_le hk h

omit [DecidableEq κ] in
/-- **No lag, nothing stale.** -/
theorem stale_zero (K : Finset κ) (age : κ → ℝ) (hage : ∀ k ∈ K, 0 ≤ age k) : stale K age 0 = ∅ := by
  unfold stale
  rw [Finset.filter_eq_empty_iff]
  intro k hk
  exact not_lt.2 (hage k hk)

/-- **The naive certificate.** Clearing every read gives coverage one and a false-clear rate
equal to the stale fraction. -/
theorem naive_certificate (K : Finset κ) (hK : K.Nonempty) (age : κ → ℝ) (lag : ℝ) :
    DMO.Certificate.coverage K K = 1 ∧
      DMO.Certificate.falseClear K (stale K age lag) = (stale K age lag).card / K.card := by
  constructor
  · unfold DMO.Certificate.coverage
    exact div_self (by exact_mod_cast hK.card_pos.ne')
  · unfold DMO.Certificate.falseClear
    have h : K ∩ stale K age lag = stale K age lag := Finset.inter_eq_right.2 (Finset.filter_subset _ K)
    rw [h]

/-- **The witness-backed certificate.** Clearing only the keys outside the lag has a
false-clear rate of zero. -/
theorem witnessed_certificate (K : Finset κ) (age : κ → ℝ) (lag : ℝ) :
    DMO.Certificate.falseClear (K \ stale K age lag) (stale K age lag) = 0 := by
  unfold DMO.Certificate.falseClear
  rw [Finset.sdiff_inter_self, Finset.card_empty]
  simp

/-- Its coverage is the fresh fraction, one minus the stale fraction. -/
theorem witnessed_coverage (K : Finset κ) (hK : K.Nonempty) (age : κ → ℝ) (lag : ℝ) :
    DMO.Certificate.coverage K (K \ stale K age lag) = 1 - (stale K age lag).card / K.card := by
  unfold DMO.Certificate.coverage
  have h : stale K age lag ∩ K = stale K age lag := Finset.inter_eq_left.2 (Finset.filter_subset _ K)
  rw [Finset.card_sdiff, h]
  have hpos : (0 : ℝ) < K.card := by exact_mod_cast hK.card_pos
  have hle : (stale K age lag).card ≤ K.card := Finset.card_filter_le _ _
  rw [Nat.cast_sub hle]
  field_simp

end DMO.Replica