Freshness.lean

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

/-
Freshness.lean. Chapter 13 section 13.3 and chapter 2 section 2.7, freshness.

A stored value of age `a` is fresh for a reader whose coherence time is `T` when `a < T`.
Freshness is consumer-relative. Two readers of the same replica with different coherence
times disagree about every age between the two, and the staleness rate of a mixed
population of reads is a weighted mean of the two readers' rates, which lies strictly
between them when they differ and so describes neither reader.
-/
import Mathlib

namespace DMO.Freshness

/-- A value of age `a` is fresh for a reader with coherence time `T`. -/
def fresh (a T : ℝ) : Prop := a < T

/-- **Two readers disagree** about every age between their coherence times. -/
theorem disagree (a T₁ T₂ : ℝ) (h₁ : T₁ ≤ a) (h₂ : a < T₂) : ¬ fresh a T₁ ∧ fresh a T₂ :=
  ⟨not_lt.2 h₁, h₂⟩

/-- The staleness rate of a mixed population, a share `p` of reads by the first reader at
rate `r₁` and the rest by the second at rate `r₂`. -/
def mixedRate (p r₁ r₂ : ℝ) : ℝ := p * r₁ + (1 - p) * r₂

/-- **The aggregate rate describes neither reader.** When the two rates differ and both
readers are present, the mixed rate lies strictly between them. -/
theorem mixedRate_between (p r₁ r₂ : ℝ) (hp0 : 0 < p) (hp1 : p < 1) (h : r₁ < r₂) :
    r₁ < mixedRate p r₁ r₂ ∧ mixedRate p r₁ r₂ < r₂ := by
  unfold mixedRate
  constructor <;> nlinarith

/-- The mixed rate equals a reader's rate only when that reader is the whole population or
the two rates agree. -/
theorem mixedRate_eq_left_iff (p r₁ r₂ : ℝ) :
    mixedRate p r₁ r₂ = r₁ ↔ p = 1 ∨ r₁ = r₂ := by
  unfold mixedRate
  constructor
  · intro h
    have : (1 - p) * (r₂ - r₁) = 0 := by linarith
    rcases mul_eq_zero.1 this with h0 | h0
    · left; linarith
    · right; linarith
  · rintro (h | h)
    · rw [h]; ring
    · rw [h]; ring

/-- A value older than every reader's coherence time is stale for all of them, and a value
younger than every coherence time is fresh for all. -/
theorem stale_for_all (a : ℝ) (T : Finset ℝ) (h : ∀ t ∈ T, t ≤ a) : ∀ t ∈ T, ¬ fresh a t :=
  fun t ht => not_lt.2 (h t ht)

end DMO.Freshness