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 / seed / node / proofs / Result.lean
| 1 | /- |
| 2 | seed/node/proofs/Result.lean -- DRA-292: the RESULT record rule of seed/node/journal.om (jn_rapply) |
| 3 | and the lost-action rule of seed/node/auth.om (au_lost), as a model checked by Lean 4 core (no |
| 4 | Mathlib). The model, not the .om code, is what is proved; the correspondence is by reading: |
| 5 | |
| 6 | jn_rapply: refuse (25) when the objective index is 64 or more, the result is 0, or the objective |
| 7 | already has a result; otherwise record (result, turns) for it. |
| 8 | cw_run at restart: an objective with a recorded result is reported and never resumed. |
| 9 | au_lost: a lost action of class 2 (inference, no external effect) is recorded not performed |
| 10 | (DONE how 6); any other class stays UNKNOWN. |
| 11 | -/ |
| 12 | namespace OmegaNode.Result |
| 13 | |
| 14 | /-- Recorded results per objective: `some (result, turns)` once a RESULT was applied. -/ |
| 15 | abbrev Table := Nat → Option (Nat × Nat) |
| 16 | |
| 17 | /-- A RESULT record: objective, result, turns. -/ |
| 18 | structure Rec where |
| 19 | obj : Nat |
| 20 | res : Nat |
| 21 | turns : Nat |
| 22 | |
| 23 | /-- jn_rapply: `none` is the refusal (25), which leaves the table unchanged. -/ |
| 24 | def apply (m : Table) (r : Rec) : Option Table := |
| 25 | if r.obj ≥ 64 ∨ r.res = 0 ∨ (m r.obj).isSome then none |
| 26 | else some (fun j => if j = r.obj then some (r.res, r.turns) else m j) |
| 27 | |
| 28 | /-- Replay of a journal's RESULT records in order; a refused record is not in the file |
| 29 | (jn_append applies before it writes), so replay skips nothing it accepted. -/ |
| 30 | def replay : Table → List Rec → Table |
| 31 | | m, [] => m |
| 32 | | m, r :: rs => match apply m r with |
| 33 | | some m' => replay m' rs |
| 34 | | none => replay m rs |
| 35 | |
| 36 | /-- The restart decision of cw_run: resume only an objective without a result. -/ |
| 37 | def resumes (m : Table) (i : Nat) : Bool := (m i).isNone |
| 38 | |
| 39 | theorem apply_sets {m m' : Table} {r : Rec} (h : apply m r = some m') : |
| 40 | m' r.obj = some (r.res, r.turns) := by |
| 41 | unfold apply at h |
| 42 | split at h |
| 43 | · cases h |
| 44 | · cases h; simp |
| 45 | |
| 46 | theorem apply_keeps {m m' : Table} {r : Rec} {j : Nat} {v : Nat × Nat} |
| 47 | (h : apply m r = some m') (hj : m j = some v) : m' j = some v := by |
| 48 | unfold apply at h |
| 49 | split at h |
| 50 | · cases h |
| 51 | · rename_i hc |
| 52 | cases h |
| 53 | by_cases e : j = r.obj |
| 54 | · subst e; simp [hj] at hc |
| 55 | · simp [e, hj] |
| 56 | |
| 57 | /-- A second RESULT for an objective is refused. -/ |
| 58 | theorem second_refused {m m' : Table} {r r' : Rec} (h : apply m r = some m') |
| 59 | (same : r'.obj = r.obj) : apply m' r' = none := by |
| 60 | have hs := apply_sets h |
| 61 | unfold apply |
| 62 | simp [same, hs] |
| 63 | |
| 64 | /-- Finality: once an objective has a result, every later replay keeps exactly that result, so |
| 65 | a restart never resumes it. -/ |
| 66 | theorem replay_keeps (rs : List Rec) : ∀ (m : Table) (j : Nat) (v : Nat × Nat), |
| 67 | m j = some v → replay m rs j = some v := by |
| 68 | induction rs with |
| 69 | | nil => intro m j v h; simpa [replay] using h |
| 70 | | cons r rs ih => |
| 71 | intro m j v h |
| 72 | unfold replay |
| 73 | cases ha : apply m r with |
| 74 | | none => exact ih m j v h |
| 75 | | some m' => exact ih m' j v (apply_keeps ha h) |
| 76 | |
| 77 | theorem never_resumed_after_result (rs : List Rec) (m : Table) (j : Nat) (v : Nat × Nat) |
| 78 | (h : m j = some v) : resumes (replay m rs) j = false := by |
| 79 | simp [resumes, replay_keeps rs m j v h] |
| 80 | |
| 81 | /-- au_lost: the DONE `how` of a lost action; `none` is UNKNOWN. -/ |
| 82 | def lostHow (cls : Nat) : Option Nat := if cls = 2 then some 6 else none |
| 83 | |
| 84 | /-- Only an inference (class 2) is ever recorded not performed when its worker is lost; an |
| 85 | external (1) or idempotent (3) effect stays UNKNOWN. -/ |
| 86 | theorem lost_not_performed_only_inference (cls : Nat) : lostHow cls = some 6 ↔ cls = 2 := by |
| 87 | unfold lostHow; split <;> simp_all |
| 88 | |
| 89 | end OmegaNode.Result |