aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / bootstrap / formal-oracles / lean / AienSeq / Theorems.lean
| 1 | import AienSeq.Model |
| 2 | namespace AienSeq |
| 3 | |
| 4 | /-! Rules enforced by the code (AIEN.INV.SEQUENCE.STALE_ID.V1). -/ |
| 5 | |
| 6 | /-! ### Generation arithmetic -/ |
| 7 | |
| 8 | theorem nextGen_range {g : Nat} (_h1 : 1 ≤ g) (_h2 : g < two32) : 1 ≤ nextGen g ∧ nextGen g < two32 := by |
| 9 | unfold nextGen two32 at *; omega |
| 10 | |
| 11 | /-- one free always changes the generation (for every valid generation) -/ |
| 12 | theorem nextGen_ne {g : Nat} (_h1 : 1 ≤ g) (_h2 : g < two32) : nextGen g ≠ g := by |
| 13 | unfold nextGen two32 at *; omega |
| 14 | |
| 15 | /-- generation 0 is never produced -/ |
| 16 | theorem nextGen_pos (g : Nat) : 1 ≤ nextGen g := by |
| 17 | unfold nextGen; omega |
| 18 | |
| 19 | /-- iterate `nextGen` k times -/ |
| 20 | def iterGen : Nat → Nat → Nat |
| 21 | | 0, g => g |
| 22 | | k + 1, g => iterGen k (nextGen g) |
| 23 | |
| 24 | /-- Closed form: generations cycle through 1 .. 2^32 - 1, so the period is genMax = 2^32 - 1. -/ |
| 25 | theorem iterGen_closed (k : Nat) : ∀ g, 1 ≤ g → g < two32 → iterGen k g = (g - 1 + k) % genMax + 1 := by |
| 26 | induction k with |
| 27 | | zero => intro g h1 h2; unfold iterGen genMax two32 at *; omega |
| 28 | | succ k ih => |
| 29 | intro g h1 h2 |
| 30 | have hr := nextGen_range h1 h2 |
| 31 | show iterGen k (nextGen g) = _ |
| 32 | rw [ih _ hr.1 hr.2] |
| 33 | unfold nextGen genMax two32 at * |
| 34 | omega |
| 35 | |
| 36 | /-- Slot reuse changes identity: while fewer than 2^32 - 1 frees have hit one slot, its generation |
| 37 | differs from the one an old id carries. -/ |
| 38 | theorem no_aba_before_wrap {g k : Nat} (h1 : 1 ≤ g) (h2 : g < two32) (hk0 : 0 < k) (hk : k < genMax) : |
| 39 | iterGen k g ≠ g := by |
| 40 | rw [iterGen_closed k g h1 h2]; unfold genMax two32 at *; omega |
| 41 | |
| 42 | /-- THE GENERATION-WRAP LIMITATION, stated as a theorem: after exactly 2^32 - 1 frees of the same |
| 43 | slot the generation is back where it started, so a stale id from before the cycle resolves again. -/ |
| 44 | theorem aba_after_exactly_one_cycle {g : Nat} (h1 : 1 ≤ g) (h2 : g < two32) : |
| 45 | iterGen genMax g = g := by |
| 46 | rw [iterGen_closed genMax g h1 h2]; unfold genMax two32 at *; omega |
| 47 | |
| 48 | /-- ...and the wrap skips generation 0 (u32::MAX goes to 1, not 0). -/ |
| 49 | theorem wrap_skips_zero : nextGen genMax = 1 := by decide |
| 50 | |
| 51 | /-! ### Scheduler variant: retire at u32::MAX -/ |
| 52 | |
| 53 | /-- a non-retiring step strictly increases the generation, so ids are never reissued -/ |
| 54 | theorem retire_strictly_increases {g g' : Nat} (h : retireNext g = some g') : g < g' := by |
| 55 | unfold retireNext at h; split at h <;> simp_all <;> omega |
| 56 | |
| 57 | /-- at u32::MAX the slot is retired instead of wrapping, so no id of that slot is ever reissued -/ |
| 58 | theorem retire_at_max : retireNext genMax = none := by decide |
| 59 | |
| 60 | /-! ### Counting -/ |
| 61 | |
| 62 | theorem occCount_le (occ : Nat → Bool) : ∀ n, occCount occ n ≤ n := by |
| 63 | intro n; induction n with |
| 64 | | zero => simp [occCount] |
| 65 | | succ n ih => simp only [occCount]; split <;> omega |
| 66 | |
| 67 | theorem occCount_all (occ : Nat → Bool) : ∀ n, (∀ i, i < n → occ i = true) → occCount occ n = n := by |
| 68 | intro n; induction n with |
| 69 | | zero => intro _; rfl |
| 70 | | succ n ih => |
| 71 | intro h |
| 72 | simp only [occCount, h n (by omega), ih (fun i hi => h i (by omega))]; rfl |
| 73 | |
| 74 | theorem occCount_congr {f g : Nat → Bool} : ∀ n, (∀ j, j < n → f j = g j) → occCount f n = occCount g n := by |
| 75 | intro n; induction n with |
| 76 | | zero => intro _; rfl |
| 77 | | succ n ih => |
| 78 | intro h |
| 79 | simp only [occCount, h n (by omega), ih (fun j hj => h j (by omega))] |
| 80 | |
| 81 | theorem occCount_set_true (occ : Nat → Bool) (i : Nat) : |
| 82 | ∀ n, i < n → occ i = false → occCount (upd occ i true) n = occCount occ n + 1 := by |
| 83 | intro n; induction n with |
| 84 | | zero => intro h; omega |
| 85 | | succ n ih => |
| 86 | intro hi ho |
| 87 | by_cases hn : i = n |
| 88 | · subst hn |
| 89 | have hc : occCount (upd occ i true) i = occCount occ i := |
| 90 | occCount_congr i (fun j hj => by simp [upd]; intro h; omega) |
| 91 | simp [occCount, hc, upd, ho] |
| 92 | · have := ih (by omega) ho |
| 93 | simp [occCount, this, upd, Ne.symm hn]; split <;> omega |
| 94 | |
| 95 | theorem occCount_set_false (occ : Nat → Bool) (i : Nat) : |
| 96 | ∀ n, i < n → occ i = true → occCount occ n = occCount (upd occ i false) n + 1 := by |
| 97 | intro n; induction n with |
| 98 | | zero => intro h; omega |
| 99 | | succ n ih => |
| 100 | intro hi ho |
| 101 | by_cases hn : i = n |
| 102 | · subst hn |
| 103 | have hc : occCount (upd occ i false) i = occCount occ i := |
| 104 | occCount_congr i (fun j hj => by simp [upd]; intro h; omega) |
| 105 | simp [occCount, hc, upd, ho] |
| 106 | · have := ih (by omega) ho |
| 107 | simp [occCount, this, upd, Ne.symm hn]; split <;> omega |
| 108 | |
| 109 | /-! ### Well-formedness -/ |
| 110 | |
| 111 | theorem occCount_none (n : Nat) : occCount (fun _ => false) n = 0 := by |
| 112 | induction n with |
| 113 | | zero => rfl |
| 114 | | succ n ih => simp [occCount, ih] |
| 115 | |
| 116 | theorem init_wf (cap : Nat) : (Arena.init cap).WF where |
| 117 | freeRange := by intro i hi; simpa [Arena.init] using hi |
| 118 | freeNodup := by simpa [Arena.init] using List.nodup_range |
| 119 | freeIff := by intro i hi; simp [Arena.init]; exact hi |
| 120 | genRange := by intro i; simp [Arena.init, two32] |
| 121 | activeEq := by simp [Arena.init, occCount_none] |
| 122 | |
| 123 | /-- allocation preserves well-formedness -/ |
| 124 | theorem alloc_wf {a a' : Arena} {id : Id} (hw : a.WF) (h : a.alloc = some (a', id)) : a'.WF := by |
| 125 | unfold Arena.alloc at h |
| 126 | cases hf : a.free with |
| 127 | | nil => simp [hf] at h |
| 128 | | cons s rest => |
| 129 | simp only [hf, Option.some.injEq, Prod.mk.injEq] at h |
| 130 | obtain ⟨rfl, rfl⟩ := h |
| 131 | have hs : s < a.cap := hw.freeRange s (by simp [hf]) |
| 132 | have hso : a.occ s = false := (hw.freeIff s hs).1 (by simp [hf]) |
| 133 | have hnd := hw.freeNodup; rw [hf] at hnd |
| 134 | rw [List.nodup_cons] at hnd |
| 135 | refine ⟨?_, hnd.2, ?_, hw.genRange, ?_⟩ |
| 136 | · intro i hi; exact hw.freeRange i (by simp [hf, hi]) |
| 137 | · intro i hi |
| 138 | by_cases hi' : i = s |
| 139 | · subst hi'; simp [upd, hnd.1] |
| 140 | · have := hw.freeIff i hi |
| 141 | simp only [hf, List.mem_cons, upd, hi', ite_false] at this ⊢ |
| 142 | simp [hi'] at this ⊢; exact this |
| 143 | · simp only |
| 144 | rw [occCount_set_true a.occ s a.cap hs hso, hw.activeEq] |
| 145 | |
| 146 | /-- release preserves well-formedness -/ |
| 147 | theorem release_wf {a a' : Arena} {id : Id} {b : Bool} (hw : a.WF) (h : a.release id = (a', b)) : a'.WF := by |
| 148 | unfold Arena.release at h |
| 149 | by_cases hl : a.live id = true |
| 150 | · simp only [hl, ite_true, Prod.mk.injEq] at h |
| 151 | obtain ⟨rfl, _⟩ := h |
| 152 | simp only [Arena.live, Bool.and_eq_true, decide_eq_true_eq] at hl |
| 153 | obtain ⟨⟨hs, hg⟩, ho⟩ := hl |
| 154 | have hnf : id.slot ∉ a.free := fun hm => by have := (hw.freeIff _ hs).1 hm; simp_all |
| 155 | refine ⟨?_, ?_, ?_, ?_, ?_⟩ |
| 156 | · intro i hi |
| 157 | simp only [List.mem_cons] at hi |
| 158 | rcases hi with rfl | hi |
| 159 | · exact hs |
| 160 | · exact hw.freeRange i hi |
| 161 | · exact List.nodup_cons.2 ⟨hnf, hw.freeNodup⟩ |
| 162 | · intro i hi |
| 163 | by_cases hi' : i = id.slot |
| 164 | · subst hi'; simp [upd] |
| 165 | · have := hw.freeIff i hi |
| 166 | simp [upd, hi', this] |
| 167 | · intro i |
| 168 | by_cases hi' : i = id.slot |
| 169 | · subst hi'; simp only [upd, ite_true] |
| 170 | exact nextGen_range (hw.genRange _).1 (hw.genRange _).2 |
| 171 | · simp only [upd, hi', ite_false]; exact hw.genRange i |
| 172 | · have := occCount_set_false a.occ id.slot a.cap hs ho |
| 173 | simp only |
| 174 | rw [hw.activeEq]; omega |
| 175 | · simp only [hl] at h |
| 176 | simp at h; obtain ⟨rfl, _⟩ := h; exact hw |
| 177 | |
| 178 | theorem fork_wf {a a' : Arena} {p id : Id} (hw : a.WF) (h : a.fork p = some (a', id)) : a'.WF := by |
| 179 | unfold Arena.fork at h |
| 180 | split at h |
| 181 | · exact alloc_wf hw h |
| 182 | · simp at h |
| 183 | |
| 184 | /-! ### The invariant theorems -/ |
| 185 | |
| 186 | /-- a live id has exactly the slot's current generation (generation mismatch never resolves) -/ |
| 187 | theorem live_gen_match {a : Arena} {id : Id} (h : a.live id = true) : |
| 188 | id.slot < a.cap ∧ a.gen id.slot = id.gen ∧ a.occ id.slot = true := by |
| 189 | simpa [Arena.live, Bool.and_eq_true, decide_eq_true_eq, and_assoc] using h |
| 190 | |
| 191 | theorem generation_mismatch_not_live {a : Arena} {id : Id} (h : a.gen id.slot ≠ id.gen) : |
| 192 | a.live id = false := by |
| 193 | unfold Arena.live; simp [h] |
| 194 | |
| 195 | /-- free invalidates the old id: it no longer resolves, and the slot carries a different generation -/ |
| 196 | theorem free_invalidates {a a' : Arena} {id : Id} (hw : a.WF) (h : a.release id = (a', true)) : |
| 197 | a'.live id = false ∧ a'.gen id.slot ≠ id.gen := by |
| 198 | unfold Arena.release at h |
| 199 | by_cases hl : a.live id = true |
| 200 | · simp only [hl, ite_true, Prod.mk.injEq] at h |
| 201 | obtain ⟨rfl, _⟩ := h |
| 202 | obtain ⟨hs, hg, ho⟩ := live_gen_match hl |
| 203 | have hne : nextGen (a.gen id.slot) ≠ id.gen := by |
| 204 | rw [← hg]; exact nextGen_ne (hw.genRange _).1 (hw.genRange _).2 |
| 205 | refine ⟨?_, ?_⟩ |
| 206 | · apply generation_mismatch_not_live; simpa [upd] using hne |
| 207 | · simpa [upd] using hne |
| 208 | · simp only [hl] at h; simp at h |
| 209 | |
| 210 | /-- free of a non-live id (stale, wrong generation, out of range, already freed) changes nothing -/ |
| 211 | theorem free_stale_noop {a : Arena} {id : Id} (h : a.live id = false) : |
| 212 | a.release id = (a, false) := by |
| 213 | unfold Arena.release; simp [h] |
| 214 | |
| 215 | /-- successful free is only possible for a live id -/ |
| 216 | theorem free_true_iff_live {a a' : Arena} {id : Id} (h : a.release id = (a', true)) : a.live id = true := by |
| 217 | unfold Arena.release at h |
| 218 | by_cases hl : a.live id = true |
| 219 | · exact hl |
| 220 | · simp only [hl] at h; simp at h |
| 221 | |
| 222 | /-- allocation hands out a live id at the slot's current generation -/ |
| 223 | theorem alloc_live {a a' : Arena} {id : Id} (hw : a.WF) (h : a.alloc = some (a', id)) : a'.live id = true := by |
| 224 | unfold Arena.alloc at h |
| 225 | cases hf : a.free with |
| 226 | | nil => simp [hf] at h |
| 227 | | cons s rest => |
| 228 | simp only [hf, Option.some.injEq, Prod.mk.injEq] at h |
| 229 | obtain ⟨rfl, rfl⟩ := h |
| 230 | have hs : s < a.cap := hw.freeRange s (by simp [hf]) |
| 231 | simp [Arena.live, upd, hs] |
| 232 | |
| 233 | /-- active count never exceeds capacity -/ |
| 234 | theorem active_le_cap {a : Arena} (hw : a.WF) : a.active ≤ a.cap := by |
| 235 | rw [hw.activeEq]; exact occCount_le _ _ |
| 236 | |
| 237 | /-- capacity cannot be exceeded: allocation is refused exactly when active = cap -/ |
| 238 | theorem alloc_none_iff_full {a : Arena} (hw : a.WF) : a.alloc = none ↔ a.active = a.cap := by |
| 239 | unfold Arena.alloc |
| 240 | cases hf : a.free with |
| 241 | | nil => |
| 242 | simp only [true_iff] |
| 243 | rw [hw.activeEq] |
| 244 | apply occCount_all |
| 245 | intro i hi |
| 246 | have := hw.freeIff i hi |
| 247 | simp only [hf, List.not_mem_nil, false_iff] at this |
| 248 | simpa using this |
| 249 | | cons s rest => |
| 250 | simp only [reduceCtorEq, false_iff] |
| 251 | have hs : s < a.cap := hw.freeRange s (by simp [hf]) |
| 252 | have hso : a.occ s = false := (hw.freeIff s hs).1 (by simp [hf]) |
| 253 | intro hfull |
| 254 | rw [hw.activeEq] at hfull |
| 255 | have := occCount_set_true a.occ s a.cap hs hso |
| 256 | have h2 := occCount_le (upd a.occ s true) a.cap |
| 257 | omega |
| 258 | |
| 259 | /-- successful allocation raises the active count by exactly one and stays within capacity -/ |
| 260 | theorem alloc_active {a a' : Arena} {id : Id} (hw : a.WF) (h : a.alloc = some (a', id)) : |
| 261 | a'.active = a.active + 1 ∧ a'.active ≤ a'.cap ∧ a'.cap = a.cap := by |
| 262 | have hw' := alloc_wf hw h |
| 263 | unfold Arena.alloc at h |
| 264 | cases hf : a.free with |
| 265 | | nil => simp [hf] at h |
| 266 | | cons s rest => |
| 267 | simp only [hf, Option.some.injEq, Prod.mk.injEq] at h |
| 268 | obtain ⟨rfl, rfl⟩ := h |
| 269 | exact ⟨rfl, active_le_cap hw', rfl⟩ |
| 270 | |
| 271 | /-- successful free lowers the active count by exactly one -/ |
| 272 | theorem release_active {a a' : Arena} {id : Id} (hw : a.WF) (h : a.release id = (a', true)) : |
| 273 | a'.active + 1 = a.active := by |
| 274 | have hl := free_true_iff_live h |
| 275 | obtain ⟨hs, _, ho⟩ := live_gen_match hl |
| 276 | unfold Arena.release at h |
| 277 | simp only [hl, ite_true, Prod.mk.injEq] at h |
| 278 | obtain ⟨rfl, _⟩ := h |
| 279 | have := occCount_set_false a.occ id.slot a.cap hs ho |
| 280 | simp only |
| 281 | rw [hw.activeEq]; omega |
| 282 | |
| 283 | /-- fork from a stale or unknown parent is refused -/ |
| 284 | theorem fork_stale_refused {a : Arena} {p : Id} (h : a.live p = false) : a.fork p = none := by |
| 285 | unfold Arena.fork; simp [h] |
| 286 | |
| 287 | /-- fork success implies the parent id was live -/ |
| 288 | theorem fork_parent_live {a a' : Arena} {p id : Id} (h : a.fork p = some (a', id)) : a.live p = true := by |
| 289 | unfold Arena.fork at h |
| 290 | by_cases hl : a.live p = true |
| 291 | · exact hl |
| 292 | · simp only [hl] at h; simp at h |
| 293 | |
| 294 | /-- allocation reads the slot's current generation and leaves every generation unchanged -/ |
| 295 | theorem alloc_gen {a a' : Arena} {id : Id} (h : a.alloc = some (a', id)) : |
| 296 | id.gen = a.gen id.slot ∧ a'.gen = a.gen := by |
| 297 | unfold Arena.alloc at h |
| 298 | cases hf : a.free with |
| 299 | | nil => simp [hf] at h |
| 300 | | cons s rest => |
| 301 | simp only [hf, Option.some.injEq, Prod.mk.injEq] at h |
| 302 | obtain ⟨rfl, rfl⟩ := h |
| 303 | exact ⟨rfl, rfl⟩ |
| 304 | |
| 305 | /-- slot reuse changes identity: free an id, allocate again, and if the same slot comes back the id |
| 306 | differs. One-step case; it needs no wrap assumption. -/ |
| 307 | theorem reuse_changes_identity {a a1 a2 : Arena} {id id2 : Id} (hw : a.WF) |
| 308 | (hf : a.release id = (a1, true)) (ha : a1.alloc = some (a2, id2)) (_hslot : id2.slot = id.slot) : |
| 309 | id2 ≠ id := by |
| 310 | intro heq |
| 311 | have hi := (free_invalidates hw hf).2 |
| 312 | have hg := (alloc_gen ha).1 |
| 313 | apply hi |
| 314 | rw [← heq]; exact hg.symm |
| 315 | |
| 316 | end AienSeq |