omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / docs / turing / proofs / TcrBound.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: Apache-2.0 |
| 3 | Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | |
| 5 | docs/turing/proofs/TcrBound.lean: the length bound of the TCR1 range coder (L111). |
| 6 | |
| 7 | Model (transcribed from calibration/docs/CODER_SPEC.md section 5 and the arithmetic of |
| 8 | src/turing/tc_range.om `encs`, which is byte-identical to src/turing/tc_range.c on every gate |
| 9 | unit). The theorems prove this model. They do not prove `encs`, and they do not mention `low`, |
| 10 | the output cache, or the bytes the encoder emits: |
| 11 | r = R >> 16; R' = (last ? R - r*cum : r*q); while R' < 2^24: R' <<= 8, shifts += 1 |
| 12 | with R in [2^24, 2^32) between symbols, R0 = 2^32 - 1, q >= 1, cum + q <= 65536 and |
| 13 | cum + q = 65536 for the last symbol of the row (rows sum to 65536). |
| 14 | |
| 15 | Main result `encode_bound`: for every valid symbol list of length n with realised entries q_t, |
| 16 | R0 * (prod q_t) * 16711681^n * 256^S < 2^32 * (2^40)^n |
| 17 | where S is the total number of renormalisation shifts and 16711681 = 2^24 - 65535. |
| 18 | Taking log2 (docs/turing/TURING_YIELD_RANGE_CODER_OM.md): |
| 19 | 8 S < sum_t (16 - log2 q_t) + n * (24 - log2 16711681) + (32 - log2 R0) |
| 20 | = ideal_exact_bits + n * eps + 3.36e-10, eps = -log2(1 - 65535/2^24). |
| 21 | Lower side `encode_lower`: 2^24 * (2^16)^n <= R0 * (prod wq_t) * 256^S, wq_t = q_t (+ 256 for |
| 22 | the last symbol of the row), so 8 S > ideal_exact_bits - G - 8, G = sum over last symbols of |
| 23 | log2(1 + 256/q_t). |
| 24 | Natural numbers only; no Mathlib, no extra axioms. |
| 25 | -/ |
| 26 | namespace TcrBound |
| 27 | |
| 28 | def TOP : Nat := 16777216 -- 2^24 |
| 29 | def MQ : Nat := 65536 -- 2^16, the row total |
| 30 | def C : Nat := 16711681 -- 2^24 - 65535 |
| 31 | |
| 32 | /-- One coding step before renormalisation (CODER_SPEC s.5). -/ |
| 33 | def step (R q cum : Nat) (last : Bool) : Nat := |
| 34 | if last then R - (R / MQ) * cum else (R / MQ) * q |
| 35 | |
| 36 | /-- Renormalisation: at most two byte shifts are needed once R' >= 256 (lemma `step_ge`). -/ |
| 37 | def renorm (R : Nat) : Nat × Nat := |
| 38 | if R < TOP then (if R * 256 < TOP then (R * 65536, 2) else (R * 256, 1)) else (R, 0) |
| 39 | |
| 40 | structure Sym where |
| 41 | q : Nat |
| 42 | cum : Nat |
| 43 | last : Bool |
| 44 | |
| 45 | def Sym.valid (s : Sym) : Prop := |
| 46 | 1 ≤ s.q ∧ s.cum + s.q ≤ MQ ∧ (s.last = true → s.cum + s.q = MQ) |
| 47 | |
| 48 | /-- Encoder range and shift count after a list of symbols. -/ |
| 49 | def run : Nat → List Sym → Nat × Nat |
| 50 | | R, [] => (R, 0) |
| 51 | | R, s :: rest => |
| 52 | let p := renorm (step R s.q s.cum s.last) |
| 53 | let t := run p.1 rest |
| 54 | (t.1, p.2 + t.2) |
| 55 | |
| 56 | def prodq : List Sym → Nat |
| 57 | | [] => 1 |
| 58 | | s :: rest => s.q * prodq rest |
| 59 | |
| 60 | theorem div_facts (R : Nat) : MQ * (R / MQ) ≤ R ∧ R < MQ * (R / MQ) + MQ := by |
| 61 | have h1 := Nat.div_add_mod R MQ |
| 62 | have h2 : R % MQ < MQ := Nat.mod_lt R (by decide) |
| 63 | constructor <;> omega |
| 64 | |
| 65 | /-- The per-step inequality: the coded interval keeps at least (1 - 65535/2^24) of p * R. -/ |
| 66 | theorem step_lb (R : Nat) (s : Sym) (hR : TOP ≤ R) (hv : s.valid) : |
| 67 | R * s.q * C ≤ step R s.q s.cum s.last * (MQ * TOP) := by |
| 68 | obtain ⟨hq, hc, hl⟩ := hv |
| 69 | obtain ⟨d1, d2⟩ := div_facts R |
| 70 | unfold step |
| 71 | cases hlast : s.last |
| 72 | · -- r * q: 2^40 * r >= 16711681 * R, times q |
| 73 | simp only [Bool.false_eq_true, ↓reduceIte] |
| 74 | have key : R * C ≤ (R / MQ) * (MQ * TOP) := by |
| 75 | unfold C MQ TOP at *; omega |
| 76 | calc R * s.q * C = (R * C) * s.q := by rw [Nat.mul_right_comm] |
| 77 | _ ≤ ((R / MQ) * (MQ * TOP)) * s.q := Nat.mul_le_mul_right _ key |
| 78 | _ = (R / MQ) * s.q * (MQ * TOP) := by rw [Nat.mul_right_comm] |
| 79 | · -- R - r * cum, with cum + q = 2^16 |
| 80 | simp only [↓reduceIte] |
| 81 | have hsum := hl hlast |
| 82 | have a1 : MQ * ((R / MQ) * s.cum) ≤ R * s.cum := by |
| 83 | rw [← Nat.mul_assoc]; exact Nat.mul_le_mul_right _ d1 |
| 84 | have a2 : R * s.cum + R * s.q = R * MQ := by rw [← Nat.mul_add, hsum] |
| 85 | have a3 : (R / MQ) * s.cum ≤ R := by |
| 86 | have : (R / MQ) * s.cum ≤ (R / MQ) * MQ := Nat.mul_le_mul_left _ (by omega) |
| 87 | rw [Nat.mul_comm (R / MQ) MQ] at this; omega |
| 88 | have a4 : R * s.q ≤ (R - (R / MQ) * s.cum) * MQ := by |
| 89 | rw [Nat.sub_mul]; rw [Nat.mul_comm ((R / MQ) * s.cum) MQ]; omega |
| 90 | have a5 : R * s.q * C ≤ R * s.q * TOP := Nat.mul_le_mul_left _ (by unfold C TOP; omega) |
| 91 | calc R * s.q * C ≤ R * s.q * TOP := a5 |
| 92 | _ ≤ ((R - (R / MQ) * s.cum) * MQ) * TOP := Nat.mul_le_mul_right _ a4 |
| 93 | _ = (R - (R / MQ) * s.cum) * (MQ * TOP) := by rw [Nat.mul_assoc] |
| 94 | |
| 95 | /-- R' >= 256 and R' <= R: renormalisation needs at most two shifts and never overflows. -/ |
| 96 | theorem step_ge (R : Nat) (s : Sym) (hR : TOP ≤ R) (hv : s.valid) : |
| 97 | 256 ≤ step R s.q s.cum s.last ∧ step R s.q s.cum s.last ≤ R := by |
| 98 | obtain ⟨hq, hc, hl⟩ := hv |
| 99 | obtain ⟨d1, d2⟩ := div_facts R |
| 100 | have hr : 256 ≤ R / MQ := by unfold TOP MQ at *; omega |
| 101 | unfold step |
| 102 | cases hlast : s.last |
| 103 | · simp only [Bool.false_eq_true, ↓reduceIte] |
| 104 | have u1 : (R / MQ) * 1 ≤ (R / MQ) * s.q := Nat.mul_le_mul_left _ hq |
| 105 | have u2 : (R / MQ) * s.q ≤ (R / MQ) * MQ := Nat.mul_le_mul_left _ (by omega) |
| 106 | rw [Nat.mul_comm (R / MQ) MQ] at u2 |
| 107 | omega |
| 108 | · simp only [↓reduceIte] |
| 109 | have u2 : (R / MQ) * s.cum ≤ (R / MQ) * (MQ - 1) := Nat.mul_le_mul_left _ (by omega) |
| 110 | have u3 : (R / MQ) * (MQ - 1) + (R / MQ) = MQ * (R / MQ) := by |
| 111 | unfold MQ; rw [Nat.mul_comm 65536]; omega |
| 112 | omega |
| 113 | |
| 114 | def W : Nat := 4294967296 -- 2^32 |
| 115 | |
| 116 | theorem renorm_spec (x : Nat) (h1 : 256 ≤ x) (h2 : x < W) : |
| 117 | (renorm x).1 = x * 256 ^ (renorm x).2 ∧ TOP ≤ (renorm x).1 ∧ (renorm x).1 < W := by |
| 118 | unfold renorm |
| 119 | by_cases a : x < TOP |
| 120 | · by_cases b : x * 256 < TOP |
| 121 | · simp only [a, b, ↓reduceIte] |
| 122 | have hb : x < 65536 := by unfold TOP at b; omega |
| 123 | have c1 : x * 65536 < 65536 * 65536 := Nat.mul_lt_mul_of_pos_right hb (by decide) |
| 124 | have c2 : 256 * 65536 ≤ x * 65536 := Nat.mul_le_mul_right _ h1 |
| 125 | refine ⟨?_, ?_, ?_⟩ |
| 126 | · trivial |
| 127 | · show 16777216 ≤ x * 65536 |
| 128 | exact c2 |
| 129 | · show x * 65536 < 4294967296 |
| 130 | exact c1 |
| 131 | · simp only [a, b, ↓reduceIte] |
| 132 | have c1 : x * 256 < 65536 * 65536 := by unfold TOP at a; omega |
| 133 | refine ⟨?_, ?_, ?_⟩ |
| 134 | · trivial |
| 135 | · show 16777216 ≤ x * 256 |
| 136 | unfold TOP at b; omega |
| 137 | · show x * 256 < 4294967296 |
| 138 | exact c1 |
| 139 | · simp only [a, ↓reduceIte] |
| 140 | refine ⟨?_, ?_, h2⟩ |
| 141 | · show x = x * 256 ^ 0 |
| 142 | rw [Nat.pow_zero, Nat.mul_one] |
| 143 | · unfold TOP at *; omega |
| 144 | |
| 145 | theorem run_cons (R : Nat) (s : Sym) (rest : List Sym) : |
| 146 | run R (s :: rest) = |
| 147 | ((run (renorm (step R s.q s.cum s.last)).1 rest).1, |
| 148 | (renorm (step R s.q s.cum s.last)).2 + (run (renorm (step R s.q s.cum s.last)).1 rest).2) := rfl |
| 149 | |
| 150 | /-- Telescoped bound from any start range in [2^24, 2^32). -/ |
| 151 | theorem run_bound (l : List Sym) (hv : ∀ s ∈ l, s.valid) : |
| 152 | ∀ R, TOP ≤ R → R < W → |
| 153 | R * prodq l * C ^ l.length * 256 ^ (run R l).2 ≤ (run R l).1 * (MQ * TOP) ^ l.length ∧ |
| 154 | TOP ≤ (run R l).1 ∧ (run R l).1 < W := by |
| 155 | induction l with |
| 156 | | nil => intro R h1 h2; simp [run, prodq]; exact ⟨h1, h2⟩ |
| 157 | | cons s rest ih => |
| 158 | intro R h1 h2 |
| 159 | have hs : s.valid := hv s (by simp) |
| 160 | have hrest : ∀ x ∈ rest, x.valid := fun x hx => hv x (by simp [hx]) |
| 161 | obtain ⟨g1, g2⟩ := step_ge R s h1 hs |
| 162 | have lb := step_lb R s h1 hs |
| 163 | rw [run_cons] |
| 164 | simp only [prodq, List.length_cons] |
| 165 | generalize step R s.q s.cum s.last = x at g1 g2 lb ⊢ |
| 166 | obtain ⟨e1, e2, e3⟩ := renorm_spec x g1 (by omega) |
| 167 | generalize renorm x = p at e1 e2 e3 ⊢ |
| 168 | obtain ⟨R2, k⟩ := p |
| 169 | simp only at e1 e2 e3 ⊢ |
| 170 | obtain ⟨i1, i2, i3⟩ := ih hrest R2 e2 e3 |
| 171 | refine ⟨?_, i2, i3⟩ |
| 172 | have lb2 : R * s.q * C * 256 ^ k ≤ R2 * (MQ * TOP) := by |
| 173 | rw [e1] |
| 174 | calc R * s.q * C * 256 ^ k ≤ x * (MQ * TOP) * 256 ^ k := Nat.mul_le_mul_right _ lb |
| 175 | _ = x * 256 ^ k * (MQ * TOP) := by rw [Nat.mul_right_comm] |
| 176 | calc R * (s.q * prodq rest) * C ^ (rest.length + 1) * 256 ^ (k + (run R2 rest).2) |
| 177 | = (R * s.q * C * 256 ^ k) * (prodq rest * C ^ rest.length * 256 ^ (run R2 rest).2) := by |
| 178 | rw [Nat.pow_succ, Nat.pow_add]; ac_rfl |
| 179 | _ ≤ (R2 * (MQ * TOP)) * (prodq rest * C ^ rest.length * 256 ^ (run R2 rest).2) := |
| 180 | Nat.mul_le_mul_right _ lb2 |
| 181 | _ = (R2 * prodq rest * C ^ rest.length * 256 ^ (run R2 rest).2) * (MQ * TOP) := by ac_rfl |
| 182 | _ ≤ ((run R2 rest).1 * (MQ * TOP) ^ rest.length) * (MQ * TOP) := Nat.mul_le_mul_right _ i1 |
| 183 | _ = (run R2 rest).1 * (MQ * TOP) ^ (rest.length + 1) := by rw [Nat.pow_succ]; ac_rfl |
| 184 | |
| 185 | /-- The coder's start range 2^32 - 1: the result used for the length bound |
| 186 | (W = 2^32, MQ * TOP = 2^40). -/ |
| 187 | theorem encode_bound (l : List Sym) (hv : ∀ s ∈ l, s.valid) : |
| 188 | (W - 1) * prodq l * C ^ l.length * 256 ^ (run (W - 1) l).2 < W * (MQ * TOP) ^ l.length := by |
| 189 | obtain ⟨b1, _, b3⟩ := run_bound l hv (W - 1) (by unfold TOP W; omega) (by unfold W; omega) |
| 190 | have hpos : 0 < (MQ * TOP) ^ l.length := Nat.pow_pos (by unfold MQ TOP; omega) |
| 191 | calc _ ≤ (run (W - 1) l).1 * (MQ * TOP) ^ l.length := b1 |
| 192 | _ < W * (MQ * TOP) ^ l.length := Nat.mul_lt_mul_of_pos_right b3 hpos |
| 193 | |
| 194 | theorem constants : W = 2 ^ 32 ∧ MQ * TOP = 2 ^ 40 ∧ C = 2 ^ 24 - 65535 ∧ TOP = 2 ^ 24 ∧ MQ = 2 ^ 16 := by |
| 195 | unfold W MQ TOP C; decide |
| 196 | |
| 197 | /-! Lower side. The last symbol of a row takes the slack R - r * cum, so it can get more than its |
| 198 | share: at most R * (q + 256) / 2^16 (`step_ub`). Every other symbol gets at most R * q / 2^16. -/ |
| 199 | |
| 200 | /-- Width credited to a symbol by the upper estimate: q, plus 256 for the last symbol of a row. -/ |
| 201 | def wq (s : Sym) : Nat := s.q + (if s.last then 256 else 0) |
| 202 | |
| 203 | def prodw : List Sym → Nat |
| 204 | | [] => 1 |
| 205 | | s :: rest => wq s * prodw rest |
| 206 | |
| 207 | theorem step_ub (R : Nat) (s : Sym) (hR : TOP ≤ R) (hv : s.valid) : |
| 208 | step R s.q s.cum s.last * MQ ≤ R * wq s := by |
| 209 | obtain ⟨hq, hc, hl⟩ := hv |
| 210 | obtain ⟨d1, d2⟩ := div_facts R |
| 211 | unfold step wq |
| 212 | cases hlast : s.last |
| 213 | · simp only [Bool.false_eq_true, ↓reduceIte, Nat.add_zero] |
| 214 | calc (R / MQ) * s.q * MQ = (MQ * (R / MQ)) * s.q := by ac_rfl |
| 215 | _ ≤ R * s.q := Nat.mul_le_mul_right _ d1 |
| 216 | · simp only [↓reduceIte] |
| 217 | have hsum := hl hlast |
| 218 | -- rem = R - MQ * r < MQ, cum < MQ, so cum * rem < 2^32 <= 256 * R |
| 219 | have hcum : s.cum ≤ MQ - 1 := by omega |
| 220 | have hrem : R - MQ * (R / MQ) ≤ MQ - 1 := by omega |
| 221 | have p1 : s.cum * (R - MQ * (R / MQ)) ≤ (MQ - 1) * (MQ - 1) := Nat.mul_le_mul hcum hrem |
| 222 | have p2 : (MQ - 1) * (MQ - 1) ≤ 256 * R := by unfold MQ TOP at *; omega |
| 223 | have e1 : s.cum * (R - MQ * (R / MQ)) = s.cum * R - MQ * ((R / MQ) * s.cum) := by |
| 224 | rw [Nat.mul_sub, Nat.mul_comm s.cum R] |
| 225 | congr 1; ac_rfl |
| 226 | have a1 : MQ * ((R / MQ) * s.cum) ≤ R * s.cum := by |
| 227 | rw [← Nat.mul_assoc]; exact Nat.mul_le_mul_right _ d1 |
| 228 | have a2 : R * s.cum + R * s.q = R * MQ := by rw [← Nat.mul_add, hsum] |
| 229 | have a3 : (R / MQ) * s.cum ≤ R := by |
| 230 | have : (R / MQ) * s.cum ≤ (R / MQ) * MQ := Nat.mul_le_mul_left _ (by omega) |
| 231 | rw [Nat.mul_comm (R / MQ) MQ] at this; omega |
| 232 | rw [Nat.sub_mul, Nat.mul_comm ((R / MQ) * s.cum) MQ, Nat.mul_add] |
| 233 | rw [Nat.mul_comm s.cum R] at e1 |
| 234 | omega |
| 235 | |
| 236 | /-- Telescoped lower side from any start range in [2^24, 2^32). -/ |
| 237 | theorem run_lower (l : List Sym) (hv : ∀ s ∈ l, s.valid) : |
| 238 | ∀ R, TOP ≤ R → R < W → |
| 239 | (run R l).1 * MQ ^ l.length ≤ R * prodw l * 256 ^ (run R l).2 := by |
| 240 | induction l with |
| 241 | | nil => intro R _ _; simp [run, prodw] |
| 242 | | cons s rest ih => |
| 243 | intro R h1 h2 |
| 244 | have hs : s.valid := hv s (by simp) |
| 245 | have hrest : ∀ x ∈ rest, x.valid := fun x hx => hv x (by simp [hx]) |
| 246 | obtain ⟨g1, g2⟩ := step_ge R s h1 hs |
| 247 | have ub := step_ub R s h1 hs |
| 248 | rw [run_cons] |
| 249 | simp only [prodw, List.length_cons] |
| 250 | generalize step R s.q s.cum s.last = x at g1 g2 ub ⊢ |
| 251 | obtain ⟨e1, e2, e3⟩ := renorm_spec x g1 (by omega) |
| 252 | generalize renorm x = p at e1 e2 e3 ⊢ |
| 253 | obtain ⟨R2, k⟩ := p |
| 254 | simp only at e1 e2 e3 ⊢ |
| 255 | have i1 := ih hrest R2 e2 e3 |
| 256 | have ub2 : R2 * MQ ≤ R * wq s * 256 ^ k := by |
| 257 | rw [e1] |
| 258 | calc x * 256 ^ k * MQ = x * MQ * 256 ^ k := by rw [Nat.mul_right_comm] |
| 259 | _ ≤ R * wq s * 256 ^ k := Nat.mul_le_mul_right _ ub |
| 260 | calc (run R2 rest).1 * MQ ^ (rest.length + 1) |
| 261 | = ((run R2 rest).1 * MQ ^ rest.length) * MQ := by rw [Nat.pow_succ]; ac_rfl |
| 262 | _ ≤ (R2 * prodw rest * 256 ^ (run R2 rest).2) * MQ := Nat.mul_le_mul_right _ i1 |
| 263 | _ = (R2 * MQ) * (prodw rest * 256 ^ (run R2 rest).2) := by ac_rfl |
| 264 | _ ≤ (R * wq s * 256 ^ k) * (prodw rest * 256 ^ (run R2 rest).2) := Nat.mul_le_mul_right _ ub2 |
| 265 | _ = R * (wq s * prodw rest) * 256 ^ (k + (run R2 rest).2) := by rw [Nat.pow_add]; ac_rfl |
| 266 | |
| 267 | /-- Lower side for the coder's start range: 2^24 * 2^(16 n) <= (2^32 - 1) * prod wq_t * 256^S. -/ |
| 268 | theorem encode_lower (l : List Sym) (hv : ∀ s ∈ l, s.valid) : |
| 269 | TOP * MQ ^ l.length ≤ (W - 1) * prodw l * 256 ^ (run (W - 1) l).2 := by |
| 270 | have b1 := run_lower l hv (W - 1) (by unfold TOP W; omega) (by unfold W; omega) |
| 271 | obtain ⟨_, b2, _⟩ := run_bound l hv (W - 1) (by unfold TOP W; omega) (by unfold W; omega) |
| 272 | calc TOP * MQ ^ l.length ≤ (run (W - 1) l).1 * MQ ^ l.length := Nat.mul_le_mul_right _ b2 |
| 273 | _ ≤ (W - 1) * prodw l * 256 ^ (run (W - 1) l).2 := b1 |
| 274 | |
| 275 | /-! Negative control: a coder that drops the last-symbol slack rule and uses r = R >> 17 |
| 276 | violates the per-step inequality on a concrete valid symbol, so `step_lb` is not vacuous. -/ |
| 277 | def badStep (R q : Nat) : Nat := (R / 131072) * q |
| 278 | |
| 279 | theorem bad_step_violates : |
| 280 | ¬ (16777216 * 1 * C ≤ badStep 16777216 1 * (MQ * TOP)) := by |
| 281 | unfold badStep C MQ TOP; decide |
| 282 | |
| 283 | /-- Second control: the loss factor C < 2^24 is needed. With no loss (C replaced by 2^24) the |
| 284 | per-step inequality fails at R = 2^24 + 65535, q = 1, cum = 0, where r = R >> 16 = 256 throws |
| 285 | away 65535 of the range: the per-symbol eps of the bound is a real cost, not slack. -/ |
| 286 | theorem tight_without_loss : |
| 287 | ¬ ((16777216 + 65535) * 1 * 16777216 ≤ step (16777216 + 65535) 1 0 false * (MQ * TOP)) := by |
| 288 | unfold step MQ TOP; decide |
| 289 | |
| 290 | |
| 291 | /-- Lower-side control: the +256 credit for the last symbol is needed. Without it the step |
| 292 | estimate fails at R = 2^24 + 65535, q = 1, cum = 65535: the slack gives that symbol about 256 |
| 293 | times its share, i.e. it codes about 8 bits below its ideal. -/ |
| 294 | theorem lower_needs_slack : |
| 295 | ¬ (step (16777216 + 65535) 1 65535 true * MQ ≤ (16777216 + 65535) * 1) := by |
| 296 | unfold step MQ; decide |
| 297 | |
| 298 | end TcrBound |
| 299 | |
| 300 | #print axioms TcrBound.step_lb |
| 301 | #print axioms TcrBound.run_bound |
| 302 | #print axioms TcrBound.encode_bound |
| 303 | #print axioms TcrBound.step_ub |
| 304 | #print axioms TcrBound.run_lower |
| 305 | #print axioms TcrBound.encode_lower |
| 306 | #print axioms TcrBound.constants |
| 307 | #print axioms TcrBound.tight_without_loss |
| 308 | #print axioms TcrBound.bad_step_violates |
| 309 | #print axioms TcrBound.lower_needs_slack |