gdp-wired 0.50.6 → 0.51.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,4 +44,17 @@ export const leanLibrary = {
44
44
  "InvariantSimulation.lean": "import FiniteSimulation\n\n/-! Invariant-restricted, one-step simulation. The invariant comes from the\nchecked target family, not from an extra premise on source executions. -/\nnamespace Bapa.InvariantSimulation\nopen Kern Bapa.FiniteSimulation\n\n/-- Local correspondence is required only where a checked target invariant\nholds. In contrast to `SourceInclusion`, this makes no all-state claim. -/\nstructure RestrictedInclusion (s t : SMachine) (Inv : KState → Prop) : Prop where\n initial : ∀ x, Initial s x → Initial t x\n admission : ∀ ρ M ι κ, Inv (ρ,M) →\n evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true →\n evalC (sView t ρ M ι κ).1 (sView t ρ M ι κ).2 t.f.m.assume = true\n next : ∀ ρ M ι κ, Inv (ρ,M) →\n evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true →\n sNext s ρ M ι κ = sNext t ρ M ι κ\n\n/-- Boundary states coincide and satisfy the target's checked invariant. -/\ndef Relation (Inv : KState → Prop) (x y : KState) : Prop := x = y ∧ Inv y\n\n/-- The family invariant is a theorem about target reachability. It is never\ninserted into the source assumption or the source theorem's hypotheses. -/\ntheorem RestrictedInclusion.simulation {s t : SMachine} {Inv : KState → Prop}\n (hinc : RestrictedInclusion s t Inv)\n (checked : ∀ ρ M, SReach t ρ M → Inv (ρ,M)) :\n SSimulation s t (Relation Inv) where\n initial := by\n intro x hx\n have ht : Reachable (Initial t) (STransition t) x := .initial (hinc.initial x hx)\n exact ⟨x, ht, rfl, checked x.1 x.2 (sreach_out ht)⟩\n matching := by\n intro x x' y _ hy hrel hedge\n rcases hrel with ⟨heq, hinv⟩\n subst y\n rcases hedge with ⟨ι, κ, ha, rfl⟩\n have hta := hinc.admission x.1 x.2 ι κ hinv ha\n have hn := hinc.next x.1 x.2 ι κ hinv ha\n have edge : STransition t x (sNext t x.1 x.2 ι κ) := ⟨ι, κ, hta, rfl⟩\n have reach := Reachable.next hy edge\n refine ⟨sNext t x.1 x.2 ι κ, .next (.refl x) edge, hn, ?_⟩\n exact checked _ _ (sreach_out reach)\n\ntheorem RestrictedInclusion.sreach {s t : SMachine} {Inv : KState → Prop}\n (hinc : RestrictedInclusion s t Inv)\n (checked : ∀ ρ M, SReach t ρ M → Inv (ρ,M)) {ρ M}\n (hr : SReach s ρ M) : SReach t ρ M := by\n obtain ⟨y, hy, hrel⟩ := sprojected_reach (hinc.simulation checked) hr\n rcases hrel with ⟨rfl, _⟩\n exact hy\n\n/-- Transfer the exact source conclusion; only the target proof supplies the\ninvariant. No bounded-key premise or strengthened source assumption occurs. -/\ntheorem RestrictedInclusion.law {s t : SMachine} {Inv : KState → Prop}\n (hinc : RestrictedInclusion s t Inv)\n (checked : ∀ ρ M, SReach t ρ M → Inv (ρ,M))\n (P : (Nat → Nat) → (Nat → Nat → Nat) → Prop)\n (target : ∀ ρ M, SReach t ρ M → P ρ M) :\n ∀ ρ M, SReach s ρ M → P ρ M :=\n fun ρ M hr => target ρ M (hinc.sreach checked hr)\n\n/-- A Boolean map bound is sufficient for threshold and Boolean-normalized\nreads to coincide. This elementary implication is false without the bound. -/\ntheorem threshold_eq_one {n : Nat} (h : n ≤ 1) : (1 ≤ n ↔ n = 1) := by omega\n\ntheorem boolean_stutter {n : Nat} (h : n ≤ 1) : (if n = 1 then 1 else 0) = n := by\n split <;> omega\n\n#print axioms RestrictedInclusion.simulation\n#print axioms RestrictedInclusion.sreach\n#print axioms RestrictedInclusion.law\n#print axioms threshold_eq_one\n#print axioms boolean_stutter\nend Bapa.InvariantSimulation\n",
45
45
  "SemanticSums.lean": "import Sums\n\n/-! Exact global sums from semantic finite-change covers. A cover is proved\nfrom update syntax and the already checked family; it is not an assumption\nadded to a source transition. This interface avoids requiring historical cover\ncertificate trees that an otherwise complete family capsule did not retain. -/\nnamespace Bapa.SemanticSums\nopen Kern\n\n/-- Only addresses explicitly present in a finite syntactic cover may change\nan integrand. Quantification is over every natural address, not a finite test. -/\ndef Preserves (h : SumMachine) (ps : List SEntry) : Prop :=\n ∀ ρ M, (∀ e ∈ ps, e.Holds ρ M) → ∀ ι κ,\n evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume = true →\n ∀ d ∈ h.sums, ∀ key,\n key ∉ d.cs.map (evalE (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2) →\n gAt d.g (sNext h.plain ρ M ι κ).2 key = gAt d.g M key\n\nvariable (h : SumMachine)\ntheorem sum_step (hok : sumOk h = true) (cover : Preserves h ps) {ρ M}\n (hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v))\n (hlaws : ∀ e ∈ ps, e.Holds ρ M) (ι : Nat → Nat) (κ : Nat → Nat → Nat)\n (ha : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume = true) :\n ∀ d ∈ h.sums, IsSum (gAt d.g (sNext h.plain ρ M ι κ).2) ((sNext h.plain ρ M ι κ).1 d.v) := by\n obtain ⟨hdist, hall⟩ := sumOk_parts hok\n intro d hd\n obtain ⟨hg, _, _, hcar, _, _, _, _, hv, _⟩ := sumOk1_parts (hall d hd)\n let V := (sView h.plain ρ M ι κ).1\n let N := withCarried h.s M κ\n let M' := (sNext h.plain ρ M ι κ).2\n let cs' := d.cs.map (evalE V N)\n have hold : evalE V N (dTermE (gHere d) d.cs) = dsum (gAt d.g M) cs' := by\n rw [dTermE_eval V N d.cs (gHere d) (gAt d.g N) (fun t => gHere_eval d hg V N t), gAt_carried h d hcar]\n have hnew : evalE V N (dTermE (gAfter h.s d) d.cs) = dsum (gAt d.g M') cs' :=\n dTermE_eval V N d.cs (gAfter h.s d) (gAt d.g M') (fun t => gAfter_eval h d hg V N t)\n have hval : (sNext h.plain ρ M ι κ).1 d.v = ρ d.v + dsum (gAt d.g M') cs' - dsum (gAt d.g M) cs' := by\n show applyS (h.s.f.m.step ++ h.sums.map (fun d => (d.v, sumNext h.s d))) V N d.v = _\n simp only [applyS]\n rw [assoc_after (fun hm => hv (by simp [hm])), assoc_sums (sumNext h.s) hdist hd]\n simp only [sumNext, evalE, hold, hnew]\n have hview : V d.v = ρ d.v := view_v h hv ρ M ι κ\n rw [hview]\n rw [hval]\n exact isSum_change cs' (hsum d hd) (cover ρ M hlaws ι κ ha d hd)\n\ntheorem sum_start (hok : sumOk h = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (hM : StartMaps h.plain.f.m M) :\n ∀ d ∈ h.sums, IsSum (gAt d.g M) (applyS h.plain.f.m.init ρ M d.v) := by\n obtain ⟨hdist, hall⟩ := sumOk_parts hok\n intro d hd\n obtain ⟨_, hm, h0, _, _, _, _, _, hv, _⟩ := sumOk1_parts (hall d hd)\n have hval : applyS h.plain.f.m.init ρ M d.v = 0 := by\n show applyS (h.s.f.m.init ++ h.sums.map (fun d => (d.v, E.const 0))) ρ M d.v = 0\n simp only [applyS]\n rw [assoc_after (fun hm' => hv (by simp [hm'])), assoc_sums (fun _ => E.const 0) hdist hd]\n rfl\n rw [hval]\n refine isSum_zero (fun key => ?_)\n rw [gAt]\n refine Eq.trans (congrME _ _ _ d.g (fun mp hmp => ?_)) h0\n funext _\n obtain ⟨p, hp, hpe⟩ := List.mem_map.mp (hm mp (mapsE_occ mp d.g hmp))\n cases hl : lookup (fun p => p.1 == mp) h.s.f.m.maps0 with\n | none => exact absurd (lookup_none_all hl p hp) (by simp [hpe])\n | some p0 =>\n obtain ⟨hp0, hp0e⟩ := lookup_some hl\n simp only [beq_iff_eq] at hp0e\n simp only [startVal, hl, Option.map_some, Option.getD_some]\n rw [← hp0e]; exact hM p0 hp0 key\n\n\ntheorem sum_facts (hok : sumOk h = true) (hs : sOk h.plain = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}\n (hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 (sumsC h.sums) = true := by\n obtain ⟨_, hall⟩ := sumOk_parts hok\n obtain ⟨_, hqd, _, _, hdx, _, _⟩ := sOk_parts h.plain hs\n let N := withCarried h.s M κ\n let Wi := withInputs h.s.f.m.inputs ρ ι\n have one : ∀ d ∈ h.sums, evalC (sView h.plain ρ M ι κ).1 N (.and (sumFact d) (.eq (.var d.q) (.const 1))) = true := by\n intro d hd\n obtain ⟨hg, _, _, hcar, _, _, _, _, hv, hvk⟩ := sumOk1_parts (hall d hd)\n have hvi : d.v ∉ h.s.f.m.inputs := fun hm => hv (by simp [hm])\n simp only [evalC, Bool.and_eq_true, decide_eq_true_eq]\n refine ⟨?_, ?_⟩\n · show decide (evalE _ N (dTermE (gHere d) d.cs) ≤ (sView h.plain ρ M ι κ).1 d.v) = true\n apply decide_eq_true\n rw [dTermE_eval _ N d.cs (gHere d) (gAt d.g N) (fun t => gHere_eval d hg _ N t), gAt_carried h d hcar, view_v h hv ρ M ι κ]\n exact dsum_le (hsum d hd) _\n · have hqv : d.q ∈ h.plain.qVars := List.mem_map_of_mem (q_mem h hd)\n have hqf : d.q ∉ flagVars h.s.f.flags := fun hm =>\n (distinct_append hdx).2.2 d.q (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inr hqv)) hm\n show decideFlags h.s.f.flags (decideQ h.plain Wi N) N d.q = 1\n rw [decide_other hqf, decideQ_of, lookup_unique hqd (q_mem h hd)]\n simp only\n rw [if_pos]\n intro key\n rw [← evalC_iff]\n show decide (evalE _ N (dTermE (gHere d) (.var d.k :: d.cs.map (inlineE h.s.f.flags))) ≤ atKeys Wi [d.k] key d.v) = true\n apply decide_eq_true\n rw [dTermE_eval _ N _ (gHere d) (gAt d.g N) (fun t => gHere_eval d hg _ N t), gAt_carried h d hcar,\n atKeys_out (show d.v ∉ [d.k] by simpa using hvk)]\n show _ ≤ withInputs h.s.f.m.inputs ρ ι d.v\n rw [withInputs_other hvi]\n exact dsum_le (hsum d hd) _\n have all : ∀ ds : List SumDecl, (∀ d ∈ ds, d ∈ h.sums) → evalC (sView h.plain ρ M ι κ).1 N (sumsC ds) = true := by\n intro ds\n induction ds with\n | nil => intro _; rfl\n | cons d ds ih =>\n intro hm\n have h1 := one d (hm d List.mem_cons_self)\n have h2 := ih (fun d' hd' => hm d' (List.mem_cons_of_mem _ hd'))\n show (evalC _ N (.and (sumFact d) (.eq (.var d.q) (.const 1))) && evalC _ N (sumsC ds)) = true\n rw [h1, h2]; rfl\n exact all h.sums (fun _ hd => hd)\n\n/-- The plain machine allows exactly the requests the rule's own assumption allows, wherever the sums are exact. -/\ntheorem sum_assume (hok : sumOk h = true) (hs : sOk h.plain = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}\n (hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.plain.f.m.assume\n = evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume := by\n show (evalC _ _ h.s.f.m.assume && evalC _ _ (sumsC h.sums)) = _\n rw [sum_facts h hok hs hsum ι κ, Bool.and_true]\n\n\ntheorem sum_cells (hl : checkSMachine h.plain ps = true) (hok : sumOk h = true)\n (cover : Preserves h ps) :\n ∀ ρ M, SReach h.plain ρ M →\n (∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) ∧ ∀ e ∈ ps, e.Holds ρ M := by\n intro ρ M hr\n induction hr with\n | start ρ M hm hp hf hpf =>\n exact ⟨sum_start h hok ρ M hm, smachine_sound h.plain ps hl _ _ (.start ρ M hm hp hf hpf)⟩\n | step ρ M ι κ hr ha ih =>\n have ha0 : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume = true := by\n have hboth : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2\n (.and h.s.f.m.assume (sumsC h.sums)) = true := ha\n simp only [evalC, Bool.and_eq_true] at hboth\n exact hboth.1\n exact ⟨sum_step h hok cover ih.1 ih.2 ι κ ha0,\n smachine_sound h.plain ps hl _ _ (.step ρ M ι κ hr ha)⟩\n\ntheorem sum_holds (hl : checkSMachine h.plain ps = true) (hok : sumOk h = true)\n (cover : Preserves h ps) :\n ∀ ρ M, SReach h.plain ρ M → ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v) :=\n fun ρ M hr => (sum_cells h hl hok cover ρ M hr).1\n\ntheorem sum_allowed (hl : checkSMachine h.plain ps = true) (hok : sumOk h = true)\n (cover : Preserves h ps) :\n ∀ ρ M, SReach h.plain ρ M → ∀ ι κ,\n evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.plain.f.m.assume =\n evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume :=\n fun ρ M hr ι κ => sum_assume h hok (sOk_of hl) (sum_holds h hl hok cover ρ M hr) ι κ\n\nsection project\nvariable (hg : sghostOk h.ghosts = true)\ninclude hg\ntheorem sum_project (hl : checkSMachine h.plain ps = true) (hok : sumOk h = true)\n (cover : Preserves h ps) :\n ∀ {ρ M}, SReach h.s ρ M → ∃ ρg, SReach h.plain ρg M ∧ Agree h.ghosts.vars ρg ρ := by\n intro ρ M hr\n induction hr with\n | start ρ M hM h0 hfa hpf =>\n refine ⟨applyS h.plain.f.m.init ρ M, SReach.start ρ M hM h0 hfa hpf, fun i hi => ?_⟩\n have hvi : i ∉ h.sums.map (fun d => d.v) := fun hm => by\n obtain ⟨d, hd, rfl⟩ := List.mem_map.mp hm; exact hi (ghosts_vars h hd).1\n show applyS (h.s.f.m.init ++ h.sums.map (fun d => (d.v, E.const 0))) ρ M i = applyS h.s.f.m.init ρ M i\n exact applyS_append_other (by simpa using hvi) ρ M\n | step ρ M ι κ _ ha ih =>\n obtain ⟨ρg, hrg, hag⟩ := ih\n obtain ⟨hA, hS, hW⟩ := sum_ghost_step h hg hag M ι κ\n refine ⟨_, ?_, hS⟩\n rw [← hW]\n refine SReach.step ρg M ι κ hrg ?_\n rw [sum_allowed h hl hok cover ρg M hrg ι κ, hA]; exact ha\n\n/-- The laws of the plain machine that read no sum's name hold in every state the rule reaches. -/\ntheorem sum_project_laws (ps : List SEntry) (hl : checkSMachine h.plain ps = true) (hok : sumOk h = true)\n (cover : Preserves h ps) :\n ∀ ρ M, SReach h.s ρ M → ∀ e ∈ ps, avoids h.ghosts.vars e.law.holds = true → e.Holds ρ M := by\n intro ρ M hr e he hav\n obtain ⟨ρg, hrg, hag⟩ := sum_project h hg hl hok cover hr\n exact sentry_transfer (V := h.ghosts.vars) (MS := []) hav (by simp [avoidsMaps]) (fun i hi => (hag i hi).symm) (fun _ _ => rfl)\n (smachine_sound h.plain ps hl ρg M hrg e he)\n\n\nend project\n#print axioms sum_cells\n#print axioms sum_project_laws\nend Bapa.SemanticSums\n",
46
46
  "RadixSumTransport.lean": "import RadixCertificateTransport\nimport Sums\n\n/-! Shape-parameterized radix candidate generation with opaque SUM addresses.\nNo source names, fixed basis, fixed radix, law indices or SUM identifiers occur.\nThis module constructs data only. Complete family and source checking are\nseparate mandatory obligations; a failed conversion never returns an old family.\n-/\nnamespace Bapa.RadixSumTransport\nopen Kern\nopen RadixKeySyntax\n\nstructure Plan where\n radix : Nat\n layouts : List (Nat × Nat)\n freshBase : Nat\n /-- Quantified SUM flags whose single argument denotes an opaque address. -/\n sumFlags : List (Nat × Nat)\n\nabbrev Error := RadixCertificateTransport.Error\n\ndef layout (p : Plan) (mp : Nat) : Option Nat :=\n RadixBinderExpansion.mapArity p.layouts mp\n\ndef config (p : Plan) (bs : List RawAddress := []) : RadixTupleSyntax.Config :=\n ⟨p.radix,p.layouts,bs,none⟩\n\n/-- Unbound scalar addresses are already raw SUM addresses, not components. -/\ndef address (p : Plan) (arity : Nat) (bs : List RawAddress) (e : E) : Except RadixKeySyntax.Error E :=\n match e with\n | .var v => if bs.any (fun b => b.variableId == v) then rekey p.radix arity bs e else .ok e\n | _ => rekey p.radix arity bs e\n\nmutual\n def expr (p : Plan) (bs : List RawAddress) : E → Except RadixKeySyntax.Error E\n | .var v => .ok (.var v)\n | .const n => .ok (.const n)\n | .get m k => do\n let k' ← match layout p m with\n | some arity => address p arity bs k\n | none => expr p bs k\n return .get m k'\n | .add a b => do return .add (← expr p bs a) (← expr p bs b)\n | .sub a b => do return .sub (← expr p bs a) (← expr p bs b)\n | .mul n a => do return .mul n (← expr p bs a)\n | .min a b => do return .min (← expr p bs a) (← expr p bs b)\n | .max a b => do return .max (← expr p bs a) (← expr p bs b)\n | .prod a b => do return .prod (← expr p bs a) (← expr p bs b)\n | .ite c a b => do return .ite (← cond p bs c) (← expr p bs a) (← expr p bs b)\n | .tup a b => do return .tup (← expr p bs a) (← expr p bs b)\n | .proj n a => do return .proj n (← expr p bs a)\n def cond (p : Plan) (bs : List RawAddress) : C → Except RadixKeySyntax.Error C\n | .lt a b => do return .lt (← expr p bs a) (← expr p bs b)\n | .le a b => do return .le (← expr p bs a) (← expr p bs b)\n | .eq a b => do\n let ca := RadixTupleSyntax.rawArity (config p bs) a\n let cb := RadixTupleSyntax.rawArity (config p bs) b\n match ca, cb with\n | none, none => return .eq (← expr p bs a) (← expr p bs b)\n | some n, none | none, some n => return .eq (← address p n bs a) (← address p n bs b)\n | some n, some m =>\n if n != m then throw (.unsupportedKey m)\n return .eq (← address p n bs a) (← address p n bs b)\n | .and a b => do return .and (← cond p bs a) (← cond p bs b)\n | .or a b => do return .or (← cond p bs a) (← cond p bs b)\n | .not a => do return .not (← cond p bs a)\nend\n\ndef isOpaque (bs : List RawAddress) (tab : List VDef) (r : Cert.Row) : Bool :=\n match r.coefs with\n | [(j,1)] => r.const == 0 && (match tab[j]? with\n | some (.var v) => !(bs.any (fun b => b.variableId == v))\n | _ => false)\n | _ => false\n\ndef table (p : Plan) (bs : List RawAddress) (old : List VDef) : Except RadixCertificateTable.Error (List VDef) := do\n let fresh := (RadixCertificateTable.freshVariables old bs).map VDef.var\n let shift := fresh.length\n let moved ← old.mapM fun d => do\n match d with\n | .var v => pure (.var v)\n | .mono js => pure (.mono (js.map (·+shift)))\n | .term e => match expr p bs (CertificateBoundErasure.expr p.radix e) with\n | .ok e' => pure (.term e')\n | .error error => throw (.key error)\n | .cell m k => match layout p m with\n | none => pure (.cell m (RadixCertificateTable.row shift k))\n | some n => if isOpaque bs old k then pure (.cell m (RadixCertificateTable.row shift k)) else\n return .cellT m (← RadixCertificateTable.components p.radix n bs old fresh k)\n | .cellT m ks => pure (.cellT m (ks.map (RadixCertificateTable.row shift)))\n return fresh ++ moved\n\ndef certificate (p : Plan) (bs : List RawAddress) (goal : List C) (k : Kern.Cert) : Except Error Kern.Cert := do\n let tab ← RadixCertificateTransport.liftTable (table p bs k.1)\n let shift := tab.length-k.1.length\n let plans := RadixEqualityTree.plans shift k.1 tab\n return CertificateBooleanCleanup.certificate goal\n (tab,[],RadixEqualityTree.tree shift (some p.radix) plans [] k.2.2)\n\ndef instanceParts (p : Plan) (n : Nat) (bs : List RawAddress) (e : E) : Except RadixKeySyntax.Error (List E) :=\n match e with\n | .var v => if bs.any (fun b => b.variableId == v) then keyParts p.radix bs n e\n else .ok (RadixTupleSyntax.projectionParts n e)\n | _ => keyParts p.radix bs n e\n\ndef entry (p : Plan) (profiles : List (Nat × Nat)) (bs : List RawAddress) (e : SEntry) : Except RadixKeySyntax.Error SEntry := do\n let mut one := []\n let mut many := []\n for (i,k) in e.insts do\n match RadixBinderExpansion.mapArity profiles i with\n | some n => match ← instanceParts p n bs k with\n | a :: rest => many := many ++ [(i,a,rest)]\n | [] => throw (.unsupportedKey n)\n | none => one := one ++ [(i,← expr p bs k)]\n for (i,k,ks) in e.pinsts do\n match RadixBinderExpansion.mapArity profiles i with\n | some n => match ← instanceParts p n bs k with\n | a :: rest => many := many ++ [(i,a,rest ++ (← ks.mapM (expr p bs)))]\n | [] => throw (.unsupportedKey n)\n | none => many := many ++ [(i,← expr p bs k,← ks.mapM (expr p bs))]\n return {e with\n law := {e.law with holds := ← cond p bs (CertificateBoundErasure.condition p.radix e.law.holds)}\n insts := one, pinsts := many\n qinsts := ← e.qinsts.mapM (fun (i,ks) => do\n match RadixBinderExpansion.mapArity p.sumFlags i with\n | none => return (i,← ks.mapM (expr p bs))\n | some n => match ks with\n | [k] => return (i,[← address p n bs k])\n | _ => throw (.unsupportedKey n))\n finsts := ← e.finsts.mapM (fun (i,k) => do return (i,← expr p bs k))\n pfinsts := ← e.pfinsts.mapM (fun (i,k,ks) => do return (i,← expr p bs k,← ks.mapM (expr p bs)))}\n\n/-- Covers use the integrand's map layout; whole-address integrands remain\nunchanged. A mixed-arity integrand is unsupported, never guessed. -/\ndef sumArity (p : Plan) (g : E) : Except Error (Option Nat) := do\n let arities := (mapsE g).filterMap (layout p)\n match arities with\n | [] => return none\n | n :: ns => if ns.all (· == n) then return some n else throw (.syntax (.unsupportedKey n))\n\ndef target (p : Plan) (old : SumMachine) : Except Error SumMachine := do\n let base ← RadixCertificateTransport.liftSyntax (RadixTupleSyntax.machine (config p) old.s)\n let sums ← old.sums.mapM fun d => do\n let n ← sumArity p d.g\n let cs ← d.cs.mapM fun k => RadixCertificateTransport.liftSyntax\n (match n with | some n => rekey p.radix n [] k | none => expr p [] k)\n return {d with cs := cs}\n return ⟨base,sums⟩\n\n/-- Fresh indices use an arity-dependent prefix sum, not a fixed stride. -/\ndef prepare (p : Plan) (ps : List SEntry) : Except Error (List RadixCertificateTransport.Prepared) := do\n let mut fresh := p.freshBase\n let mut result := []\n for (e,i) in ps.zipIdx do\n let next ← RadixCertificateTransport.prepare p.layouts fresh 0 e\n let width := match next.profile with | some (_,n) => n-1 | none => 0\n result := result ++ [{next with profile := next.profile.map (fun (_,n) => (i,n))}]\n fresh := fresh + width\n return result\n\ndef translate (p : Plan) (old : SumMachine) (ps : List SEntry) : Except Error (SumMachine × List SEntry) := do\n let t ← target p old\n let prepared ← prepare p ps\n let bs := prepared.flatMap (·.bindings)\n let profiles := prepared.filterMap (·.profile)\n let es ← prepared.mapM (fun x => RadixCertificateTransport.liftSyntax (entry p profiles bs x.entry))\n let proof ← es.mapM fun e => do\n let start ← certificate p bs (sStartGoal t.plain e) e.start\n let step ← certificate p bs (sStepGoal t.plain es e) e.step\n return {e with start := start, step := step}\n return (t,proof)\n\n/-- Candidate success is not a validity-preservation axiom. -/\ntheorem checked (t : SumMachine) (ps : List SEntry) (h : checkSMachine t.plain ps = true) :\n ∀ ρ M, SReach t.plain ρ M → ∀ e ∈ ps, e.Holds ρ M := smachine_sound _ _ h\n#print axioms checked\nend Bapa.RadixSumTransport\n",
47
+ "IntegerPrimitives.lean": "import Init.Data.BitVec.Lemmas\nimport Init.Data.Int.DivMod\nimport Init.Data.SInt\nimport Init.Data.UInt\n\n/-! Exact fixed-width integer semantics. Inputs are mathematical integers; except for\n`cast`, an operand outside the target interval is rejected rather than normalized.\nDivision by zero and signed MIN / -1 trapping are checked before Lean's totalized\ninteger division can be evaluated. All wrapping goes through the standard BitVec. -/\nnamespace IntegerPrimitives\n\n-- Except does not ship a DecidableEq instance in this pinned Init surface.\n-- Deriving constructs an ordinary kernel-checkable decision procedure.\nderiving instance DecidableEq for Except\n\ninductive Operation where\n | add | sub | mul | neg | cast | div | rem\n deriving DecidableEq, Repr\n\ninductive Overflow where\n | WRAP | SATURATE | TRAP\n deriving DecidableEq, Repr\n\ninductive Division where\n | FLOOR | TRUNCATING | EUCLIDEAN\n deriving DecidableEq, Repr, Inhabited\n\ninductive Failure where\n | invalidSpec | invalidInput | divisionByZero | overflow\n deriving DecidableEq, Repr\n\nstructure PrimitiveSpec where\n width : Nat\n signed : Bool\n operation : Operation\n overflow : Overflow\n division : Option Division := none\n deriving DecidableEq, Repr\n\ndef validWidth (w : Nat) : Bool := w == 8 || w == 16 || w == 32 || w == 64\n\ndef lower (s : PrimitiveSpec) : Int :=\n if s.signed then -(Int.ofNat (2 ^ (s.width - 1))) else 0\n\ndef upper (s : PrimitiveSpec) : Int :=\n if s.signed then Int.ofNat (2 ^ (s.width - 1)) - 1\n else Int.ofNat (2 ^ s.width) - 1\n\ndef inRange (s : PrimitiveSpec) (x : Int) : Bool :=\n decide (lower s ≤ x) && decide (x ≤ upper s)\n\ndef decode (signed : Bool) (v : BitVec w) : Int :=\n if signed then v.toInt else Int.ofNat v.toNat\n\ndef wrap (s : PrimitiveSpec) (x : Int) : Int :=\n decode s.signed (BitVec.ofInt s.width x)\n\n/-- These bridges are to the actual standard BitVec operations, not a parallel\nmodel of modular arithmetic. They hold for arbitrary width and arbitrary Ints. -/\ntheorem wrap_add (w : Nat) (signed : Bool) (a b : Int) :\n decode signed (BitVec.ofInt w (a + b)) =\n decode signed (BitVec.ofInt w a + BitVec.ofInt w b) := by\n rw [BitVec.ofInt_add]\n\ntheorem wrap_mul (w : Nat) (signed : Bool) (a b : Int) :\n decode signed (BitVec.ofInt w (a * b)) =\n decode signed (BitVec.ofInt w a * BitVec.ofInt w b) := by\n rw [BitVec.ofInt_mul]\n\ntheorem wrap_neg (w : Nat) (signed : Bool) (a : Int) :\n decode signed (BitVec.ofInt w (-a)) =\n decode signed (-BitVec.ofInt w a) := by\n rw [BitVec.ofInt_neg]\n\ntheorem wrap_sub (w : Nat) (signed : Bool) (a b : Int) :\n decode signed (BitVec.ofInt w (a - b)) =\n decode signed (BitVec.ofInt w a - BitVec.ofInt w b) := by\n simp only [Int.sub_eq_add_neg, BitVec.sub_eq_add_neg, BitVec.ofInt_add, BitVec.ofInt_neg]\n\n-- Native IntN/UIntN representations expose these same standard bitvectors.\nexample (z : Int) : (Int8.ofInt z).toBitVec = BitVec.ofInt 8 z := rfl\nexample (z : Int) : (Int16.ofInt z).toBitVec = BitVec.ofInt 16 z := rfl\nexample (z : Int) : (Int32.ofInt z).toBitVec = BitVec.ofInt 32 z := rfl\nexample (z : Int) : (Int64.ofInt z).toBitVec = BitVec.ofInt 64 z := rfl\nexample (z : Int) : (⟨BitVec.ofInt 8 z⟩ : UInt8).toBitVec = BitVec.ofInt 8 z := rfl\nexample (z : Int) : (⟨BitVec.ofInt 16 z⟩ : UInt16).toBitVec = BitVec.ofInt 16 z := rfl\nexample (z : Int) : (⟨BitVec.ofInt 32 z⟩ : UInt32).toBitVec = BitVec.ofInt 32 z := rfl\nexample (z : Int) : (⟨BitVec.ofInt 64 z⟩ : UInt64).toBitVec = BitVec.ofInt 64 z := rfl\n\n/-- Division modes use the exact three standard Int division/remainder pairs.\nThe caller must reject zero first: every Lean Int division is total at zero. -/\ndef quotient (mode : Division) (a b : Int) : Int :=\n match mode with\n | .FLOOR => Int.fdiv a b\n | .TRUNCATING => Int.tdiv a b\n | .EUCLIDEAN => Int.ediv a b\n\ndef remainder (mode : Division) (a b : Int) : Int :=\n match mode with\n | .FLOOR => Int.fmod a b\n | .TRUNCATING => Int.tmod a b\n | .EUCLIDEAN => Int.emod a b\n\n/-- `division` is mandatory precisely for div/rem. The input restriction on\nnon-casts prevents an implicit normalization before the operation. -/\ndef validSpec (s : PrimitiveSpec) : Bool :=\n validWidth s.width &&\n (if s.operation == .div || s.operation == .rem\n then s.division.isSome else s.division.isNone)\n\ndef needsRight (op : Operation) : Bool :=\n op == .add || op == .sub || op == .mul || op == .div || op == .rem\n\ndef finish (s : PrimitiveSpec) (x : Int) : Except Failure Int :=\n if inRange s x then .ok x\n else match s.overflow with\n | .WRAP => .ok (wrap s x)\n | .SATURATE => .ok (if x < lower s then lower s else upper s)\n | .TRAP => .error .overflow\n\n/-- Pair-level MIN / -1 overflow is an error for BOTH div and rem in TRAP mode.\nWRAP/SATURATE remainder is zero, while quotient goes through `finish`. -/\ndef evaluate (s : PrimitiveSpec) (a : Int) (b : Option Int := none) : Except Failure Int := Id.run do\n if !validSpec s || b.isSome != needsRight s.operation then\n return .error .invalidSpec\n if (s.operation != .cast && !inRange s a) ||\n (needsRight s.operation && !inRange s b.get!) then\n return .error .invalidInput\n if s.operation == .div || s.operation == .rem then\n let y := b.get!\n if y == 0 then return .error .divisionByZero\n if s.signed && a == lower s && y == -1 then\n if s.overflow == .TRAP then return .error .overflow\n if s.operation == .rem then return .ok 0\n let mode := s.division.get!\n return finish s (if s.operation == .div then quotient mode a y else remainder mode a y)\n let raw := match s.operation with\n | .add => a + b.get!\n | .sub => a - b.get!\n | .mul => a * b.get!\n | .neg => -a\n | .cast => a\n | .div | .rem => 0 -- unreachable after the division branch\n return finish s raw\n\nend IntegerPrimitives\n",
48
+ "FixedLimbBridge.lean": "import IntegerPrimitives\n\n/-! The exact little-endian addition loop from core/fixed-integers.ts: add each\nlimb and incoming carry, emit remainder, propagate quotient. For 16/32/64\nbits there are respectively one/two/four radix-65536 limbs. This proof is\nabout that loop, not merely about BitVec addition of already assembled words. -/\nnamespace FixedLimbBridge\n\ndef radix : Nat := 65536\n\n/-- Little-endian polynomial, including the empty word. -/\ndef assemble : List Nat → Nat\n | [] => 0\n | x :: xs => x + radix * assemble xs\n\n/-- The implementation's addRaw, restricted to equally long limb arrays. -/\ndef addCarry : List Nat → List Nat → Nat → List Nat × Nat\n | [], [], c => ([], c)\n | x :: xs, y :: ys, c =>\n let s := x + y + c\n let rest := addCarry xs ys (s / radix)\n (s % radix :: rest.1, rest.2)\n | _, _, c => ([], c)\n\n/-- Each loop iteration reconstructs the exact sum from remainder and carry. -/\ntheorem digit_carry (s : Nat) : s % radix + radix * (s / radix) = s := by\n exact Nat.mod_add_div s radix\n\n/-- Polynomial reconstruction for *the actual recursive carry algorithm*.\nNo boundedness assumption is needed for this exact identity. -/\ntheorem addCarry_reconstruct (a b : List Nat) (c : Nat)\n (h : a.length = b.length) :\n assemble (addCarry a b c).1 + radix ^ a.length * (addCarry a b c).2 =\n assemble a + assemble b + c := by\n induction a generalizing b c with\n | nil =>\n cases b with\n | nil => simp [addCarry, assemble]\n | cons _ _ => simp at h\n | cons x xs ih =>\n cases b with\n | nil => simp at h\n | cons y ys =>\n have ht : xs.length = ys.length := Nat.succ.inj h\n let s := x + y + c\n let rest := addCarry xs ys (s / radix)\n have hr : assemble rest.1 + radix ^ xs.length * rest.2 =\n assemble xs + assemble ys + s / radix := ih ys (s / radix) ht\n change s % radix + radix * assemble rest.1 +\n radix ^ (xs.length + 1) * rest.2 =\n (x + radix * assemble xs) + (y + radix * assemble ys) + c\n rw [Nat.pow_succ]\n calc\n s % radix + radix * assemble rest.1 +\n (radix ^ xs.length * radix) * rest.2 =\n s % radix + radix * (assemble rest.1 + radix ^ xs.length * rest.2) := by\n simp only [Nat.mul_add, ← Nat.mul_assoc]\n ac_rfl\n _ = s % radix + radix * (assemble xs + assemble ys + s / radix) := by rw [hr]\n _ = (x + radix * assemble xs) + (y + radix * assemble ys) + c := by\n have hd := digit_carry s\n dsimp [s, radix] at hd ⊢\n omega\n\n/-- The loop emits exactly one bounded digit per input digit. -/\ntheorem addCarry_digits (a b : List Nat) (c : Nat) (h : a.length = b.length) :\n (addCarry a b c).1.length = a.length ∧\n ∀ d ∈ (addCarry a b c).1, d < radix := by\n induction a generalizing b c with\n | nil =>\n cases b with\n | nil => simp [addCarry]\n | cons _ _ => simp at h\n | cons x xs ih =>\n cases b with\n | nil => simp at h\n | cons y ys =>\n have ht : xs.length = ys.length := Nat.succ.inj h\n have hr := ih ys ((x + y + c) / radix) ht\n simp only [addCarry, List.length_cons, List.mem_cons]\n constructor\n · exact congrArg Nat.succ hr.1\n · intro d hd\n rcases hd with rfl | hd\n · exact Nat.mod_lt _ (by decide : 0 < radix)\n · exact hr.2 d hd\n\n/-- A bounded limb array represents a number strictly below its radix power. -/\ntheorem assemble_bound (xs : List Nat) (h : ∀ d ∈ xs, d < radix) :\n assemble xs < radix ^ xs.length := by\n induction xs with\n | nil => simp [assemble]\n | cons x rest ih =>\n have hx : x < radix := h x (by simp)\n have ht : ∀ d ∈ rest, d < radix := by\n intro d hd\n exact h d (by simp [hd])\n have hb := ih ht\n simp only [assemble, List.length_cons, Nat.pow_succ]\n dsimp [radix] at hx hb ⊢\n omega\n\n/-- Full wrap equivalence of the emitted limb polynomial, not just one digit. -/\ntheorem addCarry_wrap (a b : List Nat) (h : a.length = b.length) :\n assemble (addCarry a b 0).1 % radix ^ a.length =\n (assemble a + assemble b) % radix ^ a.length := by\n have hd := addCarry_digits a b 0 h\n have hb := assemble_bound (addCarry a b 0).1 hd.2\n rw [hd.1] at hb\n have he := addCarry_reconstruct a b 0 h\n simp only [Nat.add_zero] at he\n have hm := congrArg (fun z : Nat => z % radix ^ a.length) he\n simpa [Nat.add_mod, Nat.mul_mod, Nat.mod_eq_of_lt hb] using hm\n\n/-- Four 16-bit limbs implement 64-bit wrapping addition, with arbitrary\nsymbolic bounded input limbs and an actual low/carry output. -/\ntheorem four_limb_add_wrap (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) :\n assemble (addCarry [a₀, a₁, a₂, a₃] [b₀, b₁, b₂, b₃] 0).1 % 2 ^ 64 =\n (assemble [a₀, a₁, a₂, a₃] + assemble [b₀, b₁, b₂, b₃]) % 2 ^ 64 := by\n simpa [radix, show (65536 : Nat) ^ 4 = 2 ^ 64 by decide] using\n addCarry_wrap [a₀, a₁, a₂, a₃] [b₀, b₁, b₂, b₃] (by rfl)\n\n/-- The same loop used for the 16-bit and 32-bit specializations. -/\ntheorem one_limb_add_wrap (a₀ b₀ : Nat) :\n assemble (addCarry [a₀] [b₀] 0).1 % 2 ^ 16 =\n (assemble [a₀] + assemble [b₀]) % 2 ^ 16 := by\n simpa [radix, show (65536 : Nat) ^ 1 = 2 ^ 16 by decide] using\n addCarry_wrap [a₀] [b₀] (by rfl)\n\ntheorem two_limb_add_wrap (a₀ a₁ b₀ b₁ : Nat) :\n assemble (addCarry [a₀, a₁] [b₀, b₁] 0).1 % 2 ^ 32 =\n (assemble [a₀, a₁] + assemble [b₀, b₁]) % 2 ^ 32 := by\n simpa [radix, show (65536 : Nat) ^ 2 = 2 ^ 32 by decide] using\n addCarry_wrap [a₀, a₁] [b₀, b₁] (by rfl)\n\n#print axioms FixedLimbBridge.digit_carry\n#print axioms FixedLimbBridge.addCarry_reconstruct\n#print axioms FixedLimbBridge.addCarry_digits\n#print axioms FixedLimbBridge.assemble_bound\n#print axioms FixedLimbBridge.addCarry_wrap\n#print axioms FixedLimbBridge.one_limb_add_wrap\n#print axioms FixedLimbBridge.two_limb_add_wrap\n#print axioms FixedLimbBridge.four_limb_add_wrap\nend FixedLimbBridge\n",
49
+ "FixedLimbSub.lean": "import FixedLimbBridge\n\n/-! WRAP subtraction by the actual two addRaw calls: invert every bounded\nlittle-endian limb, add, then add the one-word. This deliberately says nothing\nabout SATURATE, TRAP, or signed decoding. -/\nnamespace FixedLimbSub\n\nopen FixedLimbBridge\n\nprivate def base : Nat := 65536\n\n/-- The implementation's limbwise complement, valid only for bounded limbs. -/\ndef invert (xs : List Nat) : List Nat := xs.map (fun d => base - 1 - d)\n\n/-- The representation of one with precisely as many limbs as a nonempty word. -/\ndef oneLike : List Nat → List Nat\n | [] => []\n | _ :: xs => 1 :: xs.map (fun _ => 0)\n\nprivate theorem zero_tail (xs : List Nat) :\n assemble (xs.map (fun _ => 0)) = 0 := by\n induction xs with\n | nil => simp [assemble]\n | cons _ xs ih => simp [assemble, ih, FixedLimbBridge.assemble]\n\nprivate theorem oneLike_length (xs : List Nat) : (oneLike xs).length = xs.length := by\n cases xs <;> simp [oneLike]\n\nprivate theorem oneLike_assemble {x : Nat} {xs : List Nat} :\n assemble (oneLike (x :: xs)) = 1 := by\n simp [oneLike, assemble, zero_tail]\n\n/-- Complement is the all-ones word, not a truncated natural subtraction.\nThe proof works at arbitrary limb count; the fixed sizes below instantiate it. -/\ntheorem complement (xs : List Nat) (h : ∀ d ∈ xs, d < base) :\n assemble xs + assemble (invert xs) + 1 = base ^ xs.length := by\n induction xs with\n | nil => simp [assemble, invert, base]\n | cons x xs ih =>\n have hx : x < base := h x (by simp)\n have ht : ∀ d ∈ xs, d < base := by\n intro d hd\n exact h d (by simp [hd])\n have hr := ih ht\n simp only [List.length_cons, Nat.pow_succ, invert, List.map_cons, assemble]\n simp only [invert] at hr\n dsimp [base, FixedLimbBridge.radix] at hx hr ⊢\n omega\n\n/-- The exact result of addRaw(a,invert(b)), followed by\naddRaw(low,one): subtraction in the residue ring, not Nat subtraction.\nThe precondition b<M makes (a+M)-b an ordinary Nat representative. -/\ntheorem subtract_wrap (a b : List Nat)\n (hlen : a.length = b.length) (hne : a ≠ [])\n (hb : ∀ d ∈ b, d < base) :\n let low := (addCarry a (invert b) 0).1\n assemble (addCarry low (oneLike a) 0).1 % base ^ a.length =\n (assemble a + base ^ a.length - assemble b) % base ^ a.length := by\n dsimp\n have hi : (invert b).length = b.length := by simp [invert]\n have hfirst : a.length = (invert b).length := hlen.trans hi.symm\n have hlo := (addCarry_digits a (invert b) 0 hfirst).1\n have hone : assemble (oneLike a) = 1 := by\n cases a with\n | nil => exact False.elim (hne rfl)\n | cons x xs => exact oneLike_assemble\n have hc := complement b hb\n have bound := assemble_bound b (by\n intro d hd\n exact hb d hd)\n have hmod := addCarry_wrap (addCarry a (invert b) 0).1 (oneLike a)\n (hlo.trans (oneLike_length a).symm)\n have hfirstmod := addCarry_wrap a (invert b) hfirst\n rw [hlo] at hmod\n rw [hone] at hmod\n rw [← hlen] at hc\n change assemble b < 65536 ^ b.length at bound\n rw [← hlen] at bound\n dsimp [base] at hc ⊢\n have he : assemble a + assemble (invert b) + 1 =\n assemble a + (65536 ^ a.length - assemble b) := by omega\n have heq : (assemble (addCarry a (invert b) 0).1 + 1) % 65536 ^ a.length =\n (assemble a + assemble (invert b) + 1) % 65536 ^ a.length := by\n change assemble (addCarry a (invert b) 0).1 % 65536 ^ a.length =\n (assemble a + assemble (invert b)) % 65536 ^ a.length at hfirstmod\n simpa only [Nat.add_mod, Nat.mod_mod] using\n congrArg (fun z : Nat => (z + 1) % 65536 ^ a.length) hfirstmod\n calc\n assemble (addCarry (addCarry a (invert b) 0).1 (oneLike a) 0).1 %\n 65536 ^ a.length =\n (assemble (addCarry a (invert b) 0).1 + 1) % 65536 ^ a.length := by\n simpa [FixedLimbBridge.radix] using hmod\n _ = (assemble a + assemble (invert b) + 1) % 65536 ^ a.length := heq\n _ = (assemble a + (65536 ^ a.length - assemble b)) % 65536 ^ a.length := by rw [he]\n _ = (assemble a + 65536 ^ a.length - assemble b) % 65536 ^ a.length := by\n congr 1\n omega\n\n/-- Explicit 8-bit static specialization (one radix-256 digit). -/\ntheorem byte_complement (b : Nat) (hb : b < 256) : b + (255 - b) + 1 = 256 := by\n omega\n\n/-- Explicit 16-, 32-, and 64-bit limb counts; the generic result above\ncovers every positive count with the same implementation loop. -/\ntheorem sub16 (a₀ b₀ : Nat) (hb₀ : b₀ < 65536) :\n let a := [a₀]; let b := [b₀]\n assemble (addCarry (addCarry a (invert b) 0).1 (oneLike a) 0).1 % 2 ^ 16 =\n (assemble a + 2 ^ 16 - assemble b) % 2 ^ 16 := by\n simpa [base, show (65536 : Nat) ^ 1 = 2 ^ 16 by decide] using\n subtract_wrap [a₀] [b₀] (by simp) (by simp) (by simpa [base] using hb₀)\n\ntheorem sub32 (a₀ a₁ b₀ b₁ : Nat) (hb₀ : b₀ < 65536) (hb₁ : b₁ < 65536) :\n let a := [a₀, a₁]; let b := [b₀, b₁]\n assemble (addCarry (addCarry a (invert b) 0).1 (oneLike a) 0).1 % 2 ^ 32 =\n (assemble a + 2 ^ 32 - assemble b) % 2 ^ 32 := by\n simpa [base, show (65536 : Nat) ^ 2 = 2 ^ 32 by decide] using\n subtract_wrap [a₀, a₁] [b₀, b₁] (by simp) (by simp)\n (by simpa [base] using (show b₀ < 65536 ∧ b₁ < 65536 from ⟨hb₀, hb₁⟩))\n\ntheorem sub64 (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb₀ : b₀ < 65536) (hb₁ : b₁ < 65536)\n (hb₂ : b₂ < 65536) (hb₃ : b₃ < 65536) :\n let a := [a₀, a₁, a₂, a₃]; let b := [b₀, b₁, b₂, b₃]\n assemble (addCarry (addCarry a (invert b) 0).1 (oneLike a) 0).1 % 2 ^ 64 =\n (assemble a + 2 ^ 64 - assemble b) % 2 ^ 64 := by\n simpa [base, show (65536 : Nat) ^ 4 = 2 ^ 64 by decide] using\n subtract_wrap [a₀, a₁, a₂, a₃] [b₀, b₁, b₂, b₃] (by simp) (by simp)\n (by simpa [base] using\n (show b₀ < 65536 ∧ b₁ < 65536 ∧ b₂ < 65536 ∧ b₃ < 65536 from\n ⟨hb₀, hb₁, hb₂, hb₃⟩))\n\nend FixedLimbSub\n",
50
+ "FixedLimbMul.lean": "import Std\n\n/-! The low-diagonal/carry part of `FixedWord.mul` in `core/fixed-integers.ts`.\nThe inputs here are unsigned limb *magnitudes*. Signed magnitude conversion,\nnegation of the low word, overflow detection, saturation and trapping are not\ncovered by these unsigned low-WRAP identities. -/\nnamespace FixedLimbMul\n\nprivate def base : Nat := 65536\n\ndef word1 (a₀ : Nat) : Nat := a₀\ndef word2 (a₀ a₁ : Nat) : Nat := a₀ + base * a₁\ndef word4 (a₀ a₁ a₂ a₃ : Nat) : Nat :=\n a₀ + base * (a₁ + base * (a₂ + base * a₃))\n\n/-- The first four columns of the actual multiplication loop; the quotient of\none column is the initial carry of the next. -/\ndef columns4 (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) : Nat :=\n let c₀ := a₀ * b₀\n let c₁ := c₀ / base + a₀ * b₁ + a₁ * b₀\n let c₂ := c₁ / base + a₀ * b₂ + a₁ * b₁ + a₂ * b₀\n let c₃ := c₂ / base + a₀ * b₃ + a₁ * b₂ + a₂ * b₁ + a₃ * b₀\n c₀ % base + base * (c₁ % base + base * (c₂ % base + base * (c₃ % base)))\n\nprivate def low4 (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) : Nat :=\n a₀*b₀ + base*(a₀*b₁+a₁*b₀) +\n base^2*(a₀*b₂+a₁*b₁+a₂*b₀) +\n base^3*(a₀*b₃+a₁*b₂+a₂*b₁+a₃*b₀)\n\nprivate def high4 (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) : Nat :=\n a₁*b₃+a₂*b₂+a₃*b₁ + base*(a₂*b₃+a₃*b₂) + base^2*(a₃*b₃)\n\n/-- Polynomial multiplication, retaining the high diagonal terms without\nsubtraction or a nonlinear arithmetic decision procedure. -/\nprivate theorem product4 (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) :\n word4 a₀ a₁ a₂ a₃ * word4 b₀ b₁ b₂ b₃ =\n low4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ +\n base^4 * high4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ := by\n simp [word4, low4, high4, base, Nat.pow_succ, Nat.mul_add, Nat.add_mul,\n Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm] <;> omega\n\nprivate theorem carry4 (t₀ t₁ t₂ t₃ : Nat) :\n let c₀ := t₀\n let c₁ := c₀ / base + t₁\n let c₂ := c₁ / base + t₂\n let c₃ := c₂ / base + t₃\n c₀ % base + base * (c₁ % base + base * (c₂ % base + base * (c₃ % base))) +\n base^4 * (c₃ / base) =\n t₀ + base*t₁ + base^2*t₂ + base^3*t₃ := by\n dsimp [base]\n have h₀ := Nat.mod_add_div t₀ 65536\n have h₁ := Nat.mod_add_div (t₀ / 65536 + t₁) 65536\n have h₂ := Nat.mod_add_div ((t₀ / 65536 + t₁) / 65536 + t₂) 65536\n have h₃ := Nat.mod_add_div\n (((t₀ / 65536 + t₁) / 65536 + t₂) / 65536 + t₃) 65536\n omega\n\nprivate theorem columns4_reconstruct (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) :\n columns4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ +\n base^4 * (((((a₀*b₀ / base + a₀*b₁ + a₁*b₀) / base +\n a₀*b₂+a₁*b₁+a₂*b₀) / base +\n a₀*b₃+a₁*b₂+a₂*b₁+a₃*b₀) / base)) =\n low4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ := by\n simpa [columns4, low4, Nat.add_assoc, Nat.mul_add, Nat.mul_assoc] using\n carry4 (a₀*b₀) (a₀*b₁+a₁*b₀) (a₀*b₂+a₁*b₁+a₂*b₀)\n (a₀*b₃+a₁*b₂+a₂*b₁+a₃*b₀)\n\n/-- Four emitted low digits reconstruct the product modulo 2^64. No digit\nbounds are needed: each emitted digit is a remainder by the radix. -/\ntheorem four_limb_mul_wrap (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) :\n columns4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ % 2^64 =\n (word4 a₀ a₁ a₂ a₃ * word4 b₀ b₁ b₂ b₃) % 2^64 := by\n have hc := columns4_reconstruct a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃\n have hp := product4 a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃\n have hm : base^4 = 2^64 := by decide\n rw [← hm]\n have h := congrArg (fun x : Nat => x % base^4) hc\n have hpmod := congrArg (fun x : Nat => x % base^4) hp\n dsimp [base] at h hpmod ⊢\n omega\n\n/-- One radix-65536 column is the actual 16-bit specialization. -/\ntheorem one_limb_mul_wrap (a₀ b₀ : Nat) :\n (a₀*b₀ % base) % 2^16 = (word1 a₀ * word1 b₀) % 2^16 := by\n simp [word1, base, show (65536 : Nat) = 2^16 by decide, Nat.mod_mod]\n\n/-- Two low columns are the actual 32-bit specialization. -/\ntheorem two_limb_mul_wrap (a₀ a₁ b₀ b₁ : Nat) :\n let c₀ := a₀*b₀\n let c₁ := c₀ / base + a₀*b₁ + a₁*b₀\n (c₀ % base + base*(c₁ % base)) % 2^32 =\n (word2 a₀ a₁ * word2 b₀ b₁) % 2^32 := by\n dsimp only\n have h₀ := Nat.mod_add_div (a₀*b₀) base\n have h₁ := Nat.mod_add_div (a₀*b₀ / base + a₀*b₁ + a₁*b₀) base\n have hp : word2 a₀ a₁ * word2 b₀ b₁ =\n a₀*b₀ + base*(a₀*b₁+a₁*b₀) + base^2*(a₁*b₁) := by\n simp [word2, base, Nat.pow_succ, Nat.mul_add, Nat.add_mul,\n Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm] <;> omega\n have hm : base^2 = 2^32 := by decide\n rw [← hm, hp]\n have he : a₀*b₀ % base + base*((a₀*b₀ / base + a₀*b₁+a₁*b₀) % base) +\n base^2*((a₀*b₀ / base + a₀*b₁+a₁*b₀) / base) =\n a₀*b₀ + base*(a₀*b₁+a₁*b₀) := by\n dsimp [base] at *\n omega\n dsimp [base] at he ⊢\n omega\n\n/-- Width eight uses the single radix-256 column, not radix 65536. -/\ntheorem eight_bit_mul_wrap (a₀ b₀ : Nat) :\n (a₀*b₀ % 256) % 2^8 = (a₀*b₀) % 2^8 := by\n simp [show (256 : Nat) = 2^8 by decide, Nat.mod_mod]\n\n#print axioms FixedLimbMul.four_limb_mul_wrap\n#print axioms FixedLimbMul.two_limb_mul_wrap\n#print axioms FixedLimbMul.one_limb_mul_wrap\n#print axioms FixedLimbMul.eight_bit_mul_wrap\nend FixedLimbMul\n",
51
+ "FixedBitVecBridge.lean": "import FixedLimbBridge\n\n/-! The actual low limbs emitted by `addCarry` represent standard Lean bitvector\naddition. The proof is parametric in the number of radix-65536 limbs; signed\ninterpretation is only a view of the same standard bitvector. -/\nnamespace FixedBitVecBridge\n\nprivate theorem radix_pow (n : Nat) : 65536 ^ n = 2 ^ (16 * n) := by\n rw [show (65536 : Nat) = 2 ^ 16 by decide, Nat.pow_mul]\n\n/-- The low output of the real little-endian carry loop equals standard\n`BitVec` addition at exactly sixteen bits per input limb. The equality\nof lengths is necessary because the implementation truncates on a mismatch. -/\ntheorem addCarry_bitvec (a b : List Nat) (h : a.length = b.length) :\n BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble (FixedLimbBridge.addCarry a b 0).1) =\n BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble a) +\n BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble b) := by\n apply BitVec.eq_of_toNat_eq\n simp only [BitVec.toNat_ofNat, BitVec.toNat_add]\n rw [← radix_pow a.length]\n simpa [FixedLimbBridge.radix, Nat.add_mod, Nat.mod_mod] using FixedLimbBridge.addCarry_wrap a b h\n\n/-- Signed and unsigned decoding both interpret the very same standard result;\nno second signed arithmetic semantics is introduced. -/\ntheorem addCarry_decode (signed : Bool) (a b : List Nat)\n (h : a.length = b.length) :\n IntegerPrimitives.decode signed\n (BitVec.ofNat (16 * a.length)\n (FixedLimbBridge.assemble (FixedLimbBridge.addCarry a b 0).1)) =\n IntegerPrimitives.decode signed\n (BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble a) +\n BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble b)) := by\n rw [addCarry_bitvec a b h]\n\n/-- The signed view is exactly standard `BitVec.toInt`. -/\ntheorem addCarry_toInt (a b : List Nat) (h : a.length = b.length) :\n (BitVec.ofNat (16 * a.length)\n (FixedLimbBridge.assemble (FixedLimbBridge.addCarry a b 0).1)).toInt =\n (BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble a) +\n BitVec.ofNat (16 * a.length) (FixedLimbBridge.assemble b)).toInt := by\n rw [addCarry_bitvec a b h]\n\ntheorem one_limb_add_bitvec (a₀ b₀ : Nat) :\n BitVec.ofNat 16 (FixedLimbBridge.assemble\n (FixedLimbBridge.addCarry [a₀] [b₀] 0).1) =\n BitVec.ofNat 16 (FixedLimbBridge.assemble [a₀]) +\n BitVec.ofNat 16 (FixedLimbBridge.assemble [b₀]) := by\n simpa using addCarry_bitvec [a₀] [b₀] (by rfl)\n\ntheorem two_limb_add_bitvec (a₀ a₁ b₀ b₁ : Nat) :\n BitVec.ofNat 32 (FixedLimbBridge.assemble\n (FixedLimbBridge.addCarry [a₀, a₁] [b₀, b₁] 0).1) =\n BitVec.ofNat 32 (FixedLimbBridge.assemble [a₀, a₁]) +\n BitVec.ofNat 32 (FixedLimbBridge.assemble [b₀, b₁]) := by\n simpa using addCarry_bitvec [a₀, a₁] [b₀, b₁] (by rfl)\n\ntheorem four_limb_add_bitvec (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat) :\n BitVec.ofNat 64 (FixedLimbBridge.assemble\n (FixedLimbBridge.addCarry [a₀, a₁, a₂, a₃] [b₀, b₁, b₂, b₃] 0).1) =\n BitVec.ofNat 64 (FixedLimbBridge.assemble [a₀, a₁, a₂, a₃]) +\n BitVec.ofNat 64 (FixedLimbBridge.assemble [b₀, b₁, b₂, b₃]) := by\n simpa using addCarry_bitvec [a₀, a₁, a₂, a₃] [b₀, b₁, b₂, b₃] (by rfl)\n\nend FixedBitVecBridge\n",
52
+ "WordDivision.lean": "import Std\n\n/-! Exact unsigned natural-number semantics of kernel/word-division.ts. Each input\nlimb is reduced modulo the radix before assembly; division by zero is explicitly\nmapped to zero, rather than relying on Lean's total division convention. -/\nnamespace WordDivision\n\ndef radix (width : Nat) : Nat := if width = 8 then 256 else 65536\ndef limbs (width : Nat) : Nat := if width = 8 then 1 else width / 16\n\ndef normalize (base : Nat) (xs : List Nat) : List Nat := xs.map (· % base)\n\ndef assemble (base : Nat) : List Nat → Nat\n | [] => 0\n | x :: xs => x + base * assemble base xs\n\ndef digit (base x i : Nat) : Nat := x / base ^ i % base\n\ndef wordA (width : Nat) (values : List Nat) : Nat :=\n assemble (radix width) (normalize (radix width) (values.take (limbs width)))\ndef wordB (width : Nat) (values : List Nat) : Nat :=\n assemble (radix width) (normalize (radix width)\n ((values.drop (limbs width)).take (limbs width)))\ndef quotient (width : Nat) (values : List Nat) : Nat :=\n if wordB width values = 0 then 0 else wordA width values / wordB width values\ndef remainder (width : Nat) (values : List Nat) : Nat :=\n if wordB width values = 0 then 0 else wordA width values % wordB width values\n\ndef qDigit (width : Nat) (values : List Nat) (i : Nat) : Nat :=\n digit (radix width) (quotient width values) i\ndef rDigit (width : Nat) (values : List Nat) (i : Nat) : Nat :=\n digit (radix width) (remainder width values) i\n\ntheorem radix_pos (w : Nat) : 0 < radix w := by\n unfold radix\n split <;> omega\n\ntheorem radix_gt_one (w : Nat) : 1 < radix w := by\n unfold radix\n split <;> omega\n\ntheorem digit_lt (w x i : Nat) : digit (radix w) x i < radix w := by\n exact Nat.mod_lt _ (radix_pos w)\n\ntheorem qDigit_lt (w : Nat) (v : List Nat) (i : Nat) :\n qDigit w v i < radix w := digit_lt w _ _\ntheorem rDigit_lt (w : Nat) (v : List Nat) (i : Nat) :\n rDigit w v i < radix w := digit_lt w _ _\n\n/-- The actual successive low-to-high remainder extraction. -/\ndef digits (base x : Nat) : Nat → List Nat\n | 0 => []\n | n + 1 => x % base :: digits base (x / base) n\n\ntheorem digits_length (base x n : Nat) : (digits base x n).length = n := by\n induction n generalizing x with\n | zero => rfl\n | succ n ih => simp [digits, ih]\n\ntheorem digits_reconstruct (base x n : Nat) :\n assemble base (digits base x n) + base ^ n * (x / base ^ n) = x := by\n induction n generalizing x with\n | zero => simp [digits, assemble]\n | succ n ih =>\n have h := ih (x / base)\n have hd := Nat.mod_add_div x base\n simp only [digits, assemble, Nat.pow_succ]\n -- The recursive quotient divides first by the radix, then by its power.\n simp only [Nat.mul_comm (base ^ n) base]\n rw [← Nat.div_div_eq_div_mul x base (base ^ n)]\n calc\n x % base + base * assemble base (digits base (x / base) n) +\n (base * base ^ n) * ((x / base) / base ^ n) =\n x % base + base * (assemble base (digits base (x / base) n) +\n base ^ n * ((x / base) / base ^ n)) := by\n simp [Nat.mul_add, Nat.mul_assoc, Nat.add_assoc]\n _ = x % base + base * (x / base) := by rw [h]\n _ = x := hd\n\ntheorem zero_divisor (w : Nat) (v : List Nat) (h : wordB w v = 0) :\n quotient w v = 0 ∧ remainder w v = 0 := by\n simp [quotient, remainder, h]\n\ntheorem zero_divisor_digits (w : Nat) (v : List Nat)\n (h : wordB w v = 0) (i : Nat) :\n qDigit w v i = 0 ∧ rDigit w v i = 0 := by\n have ⟨hq, hr⟩ := zero_divisor w v h\n simp [qDigit, rDigit, digit, hq, hr]\n\ntheorem division_identity (w : Nat) (v : List Nat)\n (h : wordB w v ≠ 0) :\n wordB w v * quotient w v + remainder w v = wordA w v := by\n simp only [quotient, remainder, if_neg h]\n exact Nat.div_add_mod (wordA w v) (wordB w v)\n\ntheorem remainder_lt (w : Nat) (v : List Nat)\n (h : wordB w v ≠ 0) : remainder w v < wordB w v := by\n simp only [remainder, if_neg h]\n exact Nat.mod_lt _ (Nat.pos_of_ne_zero h)\n\ntheorem quotient_le (w : Nat) (v : List Nat)\n (h : wordB w v ≠ 0) : quotient w v ≤ wordA w v := by\n simp only [quotient, if_neg h]\n exact Nat.div_le_self _ _\n\ntheorem divisor_one (w : Nat) (v : List Nat)\n (h : wordB w v = 1) :\n quotient w v = wordA w v ∧ remainder w v = 0 := by\n simp [quotient, remainder, h, Nat.mod_one]\n\ntheorem divisor_one_digits (w : Nat) (v : List Nat)\n (h : wordB w v = 1) (i : Nat) :\n qDigit w v i = digit (radix w) (wordA w v) i ∧ rDigit w v i = 0 := by\n have ⟨hq, hr⟩ := divisor_one w v h\n simp [qDigit, rDigit, digit, hq, hr]\n\n\n/-- The normalized input operand's digit (zero for an absent slot). -/\ndef inputDigit (width : Nat) (values : List Nat) (i : Nat) : Nat :=\n (values[i]?.getD 0) % radix width\n\ndef bDigit (width : Nat) (values : List Nat) (i : Nat) : Nat :=\n inputDigit width values (limbs width + i)\n\n/-- The convolution summands of column col in q*b, exactly as in the TS loop. -/\ndef convolution (width : Nat) (values : List Nat) (col : Nat) : Nat :=\n ((List.range (limbs width)).map fun j =>\n if j ≤ col ∧ col - j < limbs width then\n qDigit width values j * bDigit width values (col - j) else 0).sum\n\n/-- Iterated quotient-carry of the low-to-high convolution. -/\ndef carry (width : Nat) (values : List Nat) : Nat → Nat\n | 0 => 0\n | col + 1 =>\n (carry width values col +\n (if col < limbs width then rDigit width values col else 0) +\n convolution width values col) / radix width\n\n/-- The compiler-facing total helper: 'q' and 'r' have slots 0..n-1;\n'c' has slots 1..2n. This agrees with wordDivisionValue on valid arguments. -/\ndef value (width part index : Nat) (values : List Nat) : Nat :=\n if part = 0 then qDigit width values index\n else if part = 1 then rDigit width values index\n else if part = 2 then carry width values index\n else 0\n\ntheorem value_q (w i : Nat) (v : List Nat) : value w 0 i v = qDigit w v i := by\n simp [value]\ntheorem value_r (w i : Nat) (v : List Nat) : value w 1 i v = rDigit w v i := by\n simp [value]\ntheorem value_c (w i : Nat) (v : List Nat) : value w 2 i v = carry w v i := by\n simp [value]\n\n/-- Unconditional digit bounds, including zero-divisor and out-of-range cases. -/\ntheorem value_q_lt (w i : Nat) (v : List Nat) : value w 0 i v < radix w := by\n simpa [value_q] using qDigit_lt w v i\ntheorem value_r_lt (w i : Nat) (v : List Nat) : value w 1 i v < radix w := by\n simpa [value_r] using rDigit_lt w v i\n\n\n/-- A little-endian array of bounded digits fits in its radix power. -/\ntheorem assemble_bound (base : Nat) (xs : List Nat) (hp : 0 < base)\n (h : ∀ x ∈ xs, x < base) : assemble base xs < base ^ xs.length := by\n induction xs with\n | nil => simp [assemble]\n | cons x xs ih =>\n have hx : x < base := h x (by simp)\n have ht : ∀ y ∈ xs, y < base := by\n intro y hy\n exact h y (by simp [hy])\n have hs := ih ht\n have hm := Nat.mul_le_mul_left base (Nat.succ_le_of_lt hs)\n simp only [Nat.mul_succ] at hm\n simp only [assemble, List.length_cons, Nat.pow_succ]\n rw [Nat.mul_comm (base ^ xs.length) base]\n omega\n\ntheorem normalize_mem_lt (base : Nat) (xs : List Nat) (hp : 0 < base) :\n ∀ x ∈ normalize base xs, x < base := by\n intro x hx\n obtain ⟨y, _, rfl⟩ := List.mem_map.mp hx\n exact Nat.mod_lt _ hp\n\ntheorem wordA_bound (w : Nat) (v : List Nat) :\n wordA w v < radix w ^ (v.take (limbs w)).length := by\n unfold wordA\n simpa [normalize] using assemble_bound (radix w)\n (normalize (radix w) (v.take (limbs w))) (radix_pos w)\n (normalize_mem_lt (radix w) _ (radix_pos w))\n\ntheorem wordB_bound (w : Nat) (v : List Nat) :\n wordB w v < radix w ^ ((v.drop (limbs w)).take (limbs w)).length := by\n unfold wordB\n simpa [normalize] using assemble_bound (radix w)\n (normalize (radix w) ((v.drop (limbs w)).take (limbs w))) (radix_pos w)\n (normalize_mem_lt (radix w) _ (radix_pos w))\n\n/-- Each carry is precisely the quotient produced at its column. -/\ntheorem carry_step (w : Nat) (v : List Nat) (i : Nat) :\n carry w v i + (if i < limbs w then rDigit w v i else 0) +\n convolution w v i =\n radix w * carry w v (i + 1) +\n (carry w v i + (if i < limbs w then rDigit w v i else 0) +\n convolution w v i) % radix w := by\n simpa [carry, Nat.add_comm, Nat.add_left_comm, Nat.add_assoc] using (Nat.mod_add_div\n (carry w v i + (if i < limbs w then rDigit w v i else 0) +\n convolution w v i) (radix w)).symm\n\n/-- The emitted carry starts at zero and is nonnegative by its Nat codomain. -/\ntheorem carry_zero (w : Nat) (v : List Nat) : carry w v 0 = 0 := rfl\n\n\n/-- The first column already exhausts a one-limb quotient/product. This holds\nfor all natural limb inputs, not just enumerated samples. -/\ntheorem single_limb_carry_zero (w a b : Nat) (hn : limbs w = 1)\n (hb : b % radix w ≠ 0) : carry w [a, b] 1 = 0 := by\n have ha : wordA w [a, b] = a % radix w := by\n simp [wordA, normalize, assemble, hn]\n have hbd : wordB w [a, b] = b % radix w := by\n simp [wordB, normalize, assemble, hn]\n have hne : wordB w [a, b] ≠ 0 := by simpa [hbd] using hb\n have hid := division_identity w [a, b] hne\n have hq := quotient_le w [a, b] hne\n have hr := remainder_lt w [a, b] hne\n have hqa : quotient w [a, b] < radix w := by\n have h := Nat.mod_lt a (radix_pos w)\n omega\n have hra : remainder w [a, b] < radix w := by\n have h := Nat.mod_lt b (radix_pos w)\n omega\n have hq0 : qDigit w [a, b] 0 = quotient w [a, b] := by\n simp [qDigit, digit, Nat.mod_eq_of_lt hqa]\n have hr0 : rDigit w [a, b] 0 = remainder w [a, b] := by\n simp [rDigit, digit, Nat.mod_eq_of_lt hra]\n have hc : convolution w [a, b] 0 = quotient w [a, b] * (b % radix w) := by\n simp [convolution, hn, hq0, bDigit, inputDigit]\n have hsmall : remainder w [a, b] +\n quotient w [a, b] * (b % radix w) < radix w := by\n have h := Nat.mod_lt a (radix_pos w)\n have hid' : quotient w [a, b] * (b % radix w) + remainder w [a, b] =\n a % radix w := by simpa [ha, hbd, Nat.mul_comm] using hid\n omega\n simp [carry, hn, hr0, hc, Nat.div_eq_of_lt hsmall]\n\ntheorem eight_bit_carry_zero (a b : Nat) (hb : b % 256 ≠ 0) :\n carry 8 [a, b] 1 = 0 := by\n exact single_limb_carry_zero 8 a b (by decide) (by simpa [radix] using hb)\n\ntheorem sixteen_bit_carry_zero (a b : Nat) (hb : b % 65536 ≠ 0) :\n carry 16 [a, b] 1 = 0 := by\n exact single_limb_carry_zero 16 a b (by decide) (by simpa [radix] using hb)\n\n\n/-- Two-column carry elimination is purely algebraic and independent of\nparticular limb values: the full product fits in two columns. -/\ntheorem two_column_carry_core (base q₀ q₁ r₀ r₁ b₀ b₁ a₀ a₁ : Nat)\n (hp : 0 < base) (ha₀ : a₀ < base) (ha₁ : a₁ < base)\n (he : (q₀ + base*q₁) * (b₀ + base*b₁) + (r₀ + base*r₁) =\n a₀ + base*a₁) :\n ((r₀ + q₀*b₀) / base + r₁ + q₀*b₁ + q₁*b₀) / base = 0 := by\n have hd := Nat.mod_add_div (r₀ + q₀*b₀) base\n have hring : (q₀ + base*q₁) * (b₀ + base*b₁) + (r₀ + base*r₁) =\n (r₀ + q₀*b₀) + base*(r₁ + q₀*b₁ + q₁*b₀) + base^2*(q₁*b₁) := by\n simp [Nat.pow_succ, Nat.mul_add, Nat.add_mul, Nat.mul_assoc,\n Nat.mul_left_comm, Nat.mul_comm, Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]\n have hpoly : (r₀ + q₀*b₀) % base +\n base * ((r₀ + q₀*b₀) / base + r₁ + q₀*b₁ + q₁*b₀) +\n base ^ 2 * (q₁*b₁) = a₀ + base*a₁ := by\n have hexpand := hring.symm.trans he\n simp only [Nat.mul_add] at hexpand ⊢\n omega\n have hsmall : a₀ + base*a₁ < base ^ 2 := by\n have hm := Nat.mul_le_mul_left base (Nat.succ_le_of_lt ha₁)\n simp only [Nat.mul_succ] at hm\n have hh : base ^ 2 = base * base := by simp [Nat.pow_succ, Nat.mul_assoc]\n omega\n have hmult : base * ((r₀ + q₀*b₀) / base + r₁ + q₀*b₁ + q₁*b₀) <\n base * base := by\n have hh : base ^ 2 = base * base := by simp [Nat.pow_succ, Nat.mul_assoc]\n omega\n have hlt : (r₀ + q₀*b₀) / base + r₁ + q₀*b₁ + q₁*b₀ < base := by\n exact Nat.lt_of_mul_lt_mul_left hmult\n exact Nat.div_eq_of_lt hlt\n\n\n/-- Reconstruct a number below two radix places from its low digits. -/\ntheorem two_digit_expand (base x : Nat) (h : x < base ^ 2) :\n x = x % base + base * (x / base % base) := by\n have hd := digits_reconstruct base x 2\n have hz : x / base ^ 2 = 0 := Nat.div_eq_of_lt h\n simpa [digits, assemble, hz] using hd.symm\n\n/-- The concrete 32-bit helper's carry after the two low columns is zero. -/\ntheorem thirty_two_bit_carry_zero (a₀ a₁ b₀ b₁ : Nat)\n (hb : wordB 32 [a₀, a₁, b₀, b₁] ≠ 0) :\n carry 32 [a₀, a₁, b₀, b₁] 2 = 0 := by\n let v := [a₀, a₁, b₀, b₁]\n have ha : wordA 32 v = a₀ % 65536 + 65536 * (a₁ % 65536) := by\n simp [v, wordA, normalize, assemble, radix, limbs]\n have hbd : wordB 32 v = b₀ % 65536 + 65536 * (b₁ % 65536) := by\n simp [v, wordB, normalize, assemble, radix, limbs]\n have habound : wordA 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordA_bound 32 v\n have hbbound : wordB 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordB_bound 32 v\n have hq : quotient 32 v < 65536 ^ 2 :=\n Nat.lt_of_le_of_lt (quotient_le 32 v hb) habound\n have hr : remainder 32 v < 65536 ^ 2 :=\n Nat.lt_trans (remainder_lt 32 v hb) hbbound\n have hqe := two_digit_expand 65536 (quotient 32 v) hq\n have hre := two_digit_expand 65536 (remainder 32 v) hr\n have he : (qDigit 32 v 0 + 65536*qDigit 32 v 1) *\n (b₀ % 65536 + 65536*(b₁ % 65536)) +\n (rDigit 32 v 0 + 65536*rDigit 32 v 1) =\n a₀ % 65536 + 65536*(a₁ % 65536) := by\n have hqeq : qDigit 32 v 0 + 65536*qDigit 32 v 1 = quotient 32 v := by\n simpa [qDigit, digit, radix] using hqe.symm\n have hreq : rDigit 32 v 0 + 65536*rDigit 32 v 1 = remainder 32 v := by\n simpa [rDigit, digit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 32 v hb\n have hc : carry 32 v 2 =\n ((rDigit 32 v 0 + qDigit 32 v 0 * (b₀ % 65536)) / 65536 +\n rDigit 32 v 1 + qDigit 32 v 0 * (b₁ % 65536) +\n qDigit 32 v 1 * (b₀ % 65536)) / 65536 := by\n have hrange : List.range 2 = [0, 1] := by decide\n simp [carry, convolution, hrange, bDigit, inputDigit, v, limbs, radix,\n Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]\n rw [hc]\n exact two_column_carry_core 65536 (qDigit 32 v 0) (qDigit 32 v 1)\n (rDigit 32 v 0) (rDigit 32 v 1) (b₀ % 65536) (b₁ % 65536)\n (a₀ % 65536) (a₁ % 65536) (by decide)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n\n\n/-- The four-column carry equation over the actual 16-bit radix. -/\ntheorem four_column_carry_reconstruct (t₀ t₁ t₂ t₃ : Nat) :\n let c₁ := t₀ / 65536\n let c₂ := (c₁ + t₁) / 65536\n let c₃ := (c₂ + t₂) / 65536\n let c₄ := (c₃ + t₃) / 65536\n t₀ % 65536 + 65536 * ((c₁ + t₁) % 65536) +\n 65536 ^ 2 * ((c₂ + t₂) % 65536) +\n 65536 ^ 3 * ((c₃ + t₃) % 65536) +\n 65536 ^ 4 * c₄ =\n t₀ + 65536*t₁ + 65536^2*t₂ + 65536^3*t₃ := by\n dsimp\n have h₀ := Nat.mod_add_div t₀ 65536\n have h₁ := Nat.mod_add_div (t₀ / 65536 + t₁) 65536\n have h₂ := Nat.mod_add_div ((t₀ / 65536 + t₁) / 65536 + t₂) 65536\n have h₃ := Nat.mod_add_div\n (((t₀ / 65536 + t₁) / 65536 + t₂) / 65536 + t₃) 65536\n omega\n\n/-- Exact four-limb polynomial expansion: every high diagonal is retained. -/\ntheorem four_column_product (q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃\n b₀ b₁ b₂ b₃ : Nat) :\n assemble 65536 [q₀,q₁,q₂,q₃] * assemble 65536 [b₀,b₁,b₂,b₃] +\n assemble 65536 [r₀,r₁,r₂,r₃] =\n (r₀ + q₀*b₀) +\n 65536*(r₁ + q₀*b₁+q₁*b₀) +\n 65536^2*(r₂ + q₀*b₂+q₁*b₁+q₂*b₀) +\n 65536^3*(r₃ + q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀) +\n 65536^4*(q₁*b₃+q₂*b₂+q₃*b₁ +\n 65536*(q₂*b₃+q₃*b₂) + 65536^2*(q₃*b₃)) := by\n simp [assemble, Nat.pow_succ, Nat.mul_add, Nat.add_mul,\n Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm,\n Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]\n\n/-- No carry escapes the fourth low column when the whole unsigned product\nand remainder equal a four-limb word. Universal in all twelve digits. -/\ntheorem four_column_carry_core (q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃\n b₀ b₁ b₂ b₃ a₀ a₁ a₂ a₃ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536)\n (ha₂ : a₂ < 65536) (ha₃ : a₃ < 65536)\n (he : assemble 65536 [q₀,q₁,q₂,q₃] *\n assemble 65536 [b₀,b₁,b₂,b₃] + assemble 65536 [r₀,r₁,r₂,r₃] =\n assemble 65536 [a₀,a₁,a₂,a₃]) :\n let t₀ := r₀ + q₀*b₀\n let t₁ := r₁ + q₀*b₁+q₁*b₀\n let t₂ := r₂ + q₀*b₂+q₁*b₁+q₂*b₀\n let t₃ := r₃ + q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀\n ((((t₀ / 65536 + t₁) / 65536 + t₂) / 65536 + t₃) / 65536) = 0 := by\n dsimp only\n have hc := four_column_carry_reconstruct (r₀ + q₀*b₀)\n (r₁ + q₀*b₁+q₁*b₀) (r₂ + q₀*b₂+q₁*b₁+q₂*b₀)\n (r₃ + q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀)\n dsimp only at hc\n have hp := four_column_product q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃ b₀ b₁ b₂ b₃\n have hb : assemble 65536 [a₀,a₁,a₂,a₃] < 65536 ^ 4 := by\n apply assemble_bound 65536 [a₀,a₁,a₂,a₃] (by decide)\n intro d hd\n simp only [List.mem_cons, List.mem_nil_iff, or_false] at hd\n rcases hd with rfl | rfl | rfl | rfl\n all_goals assumption\n rw [he] at hp\n omega\n\n\n/-- Four successive quotients/remainders recover the bounded word. -/\ntheorem four_digit_expand (base x : Nat) (h : x < base ^ 4) :\n x = assemble base [digit base x 0, digit base x 1,\n digit base x 2, digit base x 3] := by\n have hd := digits_reconstruct base x 4\n have hz : x / base ^ 4 = 0 := Nat.div_eq_of_lt h\n rw [hz] at hd\n simp only [Nat.mul_zero, Nat.add_zero] at hd\n simpa [digits, assemble, digit, Nat.div_div_eq_div_mul,\n Nat.pow_succ, Nat.mul_assoc] using hd.symm\n\nset_option maxHeartbeats 2000000\n/-- Full 64-bit division helper: the carry after four low columns vanishes for\nall eight input limbs, after normalization, whenever the divisor is nonzero. -/\ntheorem sixty_four_bit_carry_zero (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n carry 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] 4 = 0 := by\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have ha : wordA 64 v = assemble 65536\n [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n simp [v, wordA, normalize, radix, limbs]\n have hbd : wordB 64 v = assemble 65536\n [b₀%65536,b₁%65536,b₂%65536,b₃%65536] := by\n simp [v, wordB, normalize, radix, limbs]\n have habound : wordA 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordA_bound 64 v\n have hbbound : wordB 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordB_bound 64 v\n have hq : quotient 64 v < 65536 ^ 4 :=\n Nat.lt_of_le_of_lt (quotient_le 64 v hb) habound\n have hr : remainder 64 v < 65536 ^ 4 :=\n Nat.lt_trans (remainder_lt 64 v hb) hbbound\n have hqe := four_digit_expand 65536 (quotient 64 v) hq\n have hre := four_digit_expand 65536 (remainder 64 v) hr\n have he : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] *\n assemble 65536 [b₀%65536,b₁%65536,b₂%65536,b₃%65536] +\n assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] =\n assemble 65536 [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n have hqeq : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] = quotient 64 v := by\n simpa [qDigit, radix] using hqe.symm\n have hreq : assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] = remainder 64 v := by\n simpa [rDigit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 64 v hb\n have hc : carry 64 v 4 =\n let t₀ := rDigit 64 v 0 + qDigit 64 v 0*(b₀%65536)\n let t₁ := rDigit 64 v 1 + qDigit 64 v 0*(b₁%65536) + qDigit 64 v 1*(b₀%65536)\n let t₂ := rDigit 64 v 2 + qDigit 64 v 0*(b₂%65536) + qDigit 64 v 1*(b₁%65536) + qDigit 64 v 2*(b₀%65536)\n let t₃ := rDigit 64 v 3 + qDigit 64 v 0*(b₃%65536) + qDigit 64 v 1*(b₂%65536) + qDigit 64 v 2*(b₁%65536) + qDigit 64 v 3*(b₀%65536)\n ((((t₀ / 65536 + t₁) / 65536 + t₂) / 65536 + t₃) / 65536) := by\n have hrange : List.range 4 = [0,1,2,3] := by decide\n simp [carry, convolution, hrange, bDigit, inputDigit, v, limbs, radix]\n omega\n rw [hc]\n exact four_column_carry_core (qDigit 64 v 0) (qDigit 64 v 1)\n (qDigit 64 v 2) (qDigit 64 v 3) (rDigit 64 v 0) (rDigit 64 v 1)\n (rDigit 64 v 2) (rDigit 64 v 3) (b₀%65536) (b₁%65536)\n (b₂%65536) (b₃%65536) (a₀%65536) (a₁%65536)\n (a₂%65536) (a₃%65536)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide))\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n\n\n/-- Division by zero is zero throughout every carry column as well as both\nquotient/remainder digit arrays. -/\ntheorem zero_divisor_carry (w : Nat) (v : List Nat)\n (h : wordB w v = 0) (i : Nat) : carry w v i = 0 := by\n induction i with\n | zero => rfl\n | succ i ih =>\n have hr := (zero_divisor_digits w v h i).2\n have hc : convolution w v i = 0 := by\n simp only [convolution, qDigit, digit, quotient, h]\n have hz : ∀ xs : List Nat, (xs.map fun _ => (0 : Nat)).sum = 0 := by\n intro xs\n induction xs with\n | nil => rfl\n | cons x xs ih => simp [ih]\n simpa using hz (List.range (limbs w))\n simp [carry, ih, hr, hc]\n\n/-- Zero divisor also gives zero for all three observable parts, even outside\nnominal index ranges; it does not depend on caller-supplied digit bounds. -/\ntheorem zero_divisor_value (w i : Nat) (v : List Nat)\n (h : wordB w v = 0) :\n value w 0 i v = 0 ∧ value w 1 i v = 0 ∧ value w 2 i v = 0 := by\n simp [value_q, value_r, value_c, (zero_divisor_digits w v h i).1,\n (zero_divisor_digits w v h i).2, zero_divisor_carry w v h i]\n\n\nset_option maxHeartbeats 2000000\n/-- All four low-column equations follow from the exact full product, not from\nan assumed carry invariant. The emitted digits are unique bounded radix digits. -/\nprivate theorem four_column_eq_core (q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃\n b₀ b₁ b₂ b₃ a₀ a₁ a₂ a₃ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536)\n (ha₂ : a₂ < 65536) (ha₃ : a₃ < 65536)\n (he : assemble 65536 [q₀,q₁,q₂,q₃] *\n assemble 65536 [b₀,b₁,b₂,b₃] + assemble 65536 [r₀,r₁,r₂,r₃] =\n assemble 65536 [a₀,a₁,a₂,a₃]) :\n let t₀ := r₀ + q₀*b₀\n let t₁ := r₁ + q₀*b₁+q₁*b₀\n let t₂ := r₂ + q₀*b₂+q₁*b₁+q₂*b₀\n let t₃ := r₃ + q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀\n let c₁ := t₀ / 65536\n let c₂ := (c₁+t₁) / 65536\n let c₃ := (c₂+t₂) / 65536\n let c₄ := (c₃+t₃) / 65536\n t₀ = a₀ + 65536*c₁ ∧\n c₁+t₁ = a₁ + 65536*c₂ ∧\n c₂+t₂ = a₂ + 65536*c₃ ∧\n c₃+t₃ = a₃ + 65536*c₄ := by\n dsimp only\n have hc := four_column_carry_reconstruct (r₀ + q₀*b₀)\n (r₁ + q₀*b₁+q₁*b₀) (r₂ + q₀*b₂+q₁*b₁+q₂*b₀)\n (r₃ + q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀)\n dsimp only at hc\n have hp := four_column_product q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃ b₀ b₁ b₂ b₃\n rw [he] at hp\n have h₀ := Nat.mod_add_div (r₀ + q₀*b₀) 65536\n have h₁ := Nat.mod_add_div ((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀) 65536\n have h₂ := Nat.mod_add_div\n (((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀)/65536 +\n r₂+q₀*b₂+q₁*b₁+q₂*b₀) 65536\n have h₃ := Nat.mod_add_div\n (((((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀)/65536 +\n r₂+q₀*b₂+q₁*b₁+q₂*b₀)/65536) +\n r₃+q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀) 65536\n have hb₀ := Nat.mod_lt (r₀ + q₀*b₀) (by decide : 0 < 65536)\n have hb₁ := Nat.mod_lt ((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀)\n (by decide : 0 < 65536)\n have hb₂ := Nat.mod_lt\n (((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀)/65536 +\n r₂+q₀*b₂+q₁*b₁+q₂*b₀) (by decide : 0 < 65536)\n have hb₃ := Nat.mod_lt\n (((((r₀+q₀*b₀)/65536 + r₁+q₀*b₁+q₁*b₀)/65536 +\n r₂+q₀*b₂+q₁*b₁+q₂*b₀)/65536) +\n r₃+q₀*b₃+q₁*b₂+q₂*b₁+q₃*b₀) (by decide : 0 < 65536)\n simp only [assemble] at hp\n omega\n\n\nset_option maxHeartbeats 2000000\n/-- The four guarded low-column identities of the concrete 64-bit helper. -/\ntheorem sixty_four_bit_columns (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n (∀ i < 4, carry 64 v i + rDigit 64 v i + convolution 64 v i =\n inputDigit 64 v i + 65536 * carry 64 v (i+1)) := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have ha : wordA 64 v = assemble 65536\n [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n simp [v, wordA, normalize, radix, limbs]\n have hbd : wordB 64 v = assemble 65536\n [b₀%65536,b₁%65536,b₂%65536,b₃%65536] := by\n simp [v, wordB, normalize, radix, limbs]\n have habound : wordA 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordA_bound 64 v\n have hbbound : wordB 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordB_bound 64 v\n have hq : quotient 64 v < 65536 ^ 4 :=\n Nat.lt_of_le_of_lt (quotient_le 64 v hb) habound\n have hr : remainder 64 v < 65536 ^ 4 :=\n Nat.lt_trans (remainder_lt 64 v hb) hbbound\n have hqe := four_digit_expand 65536 (quotient 64 v) hq\n have hre := four_digit_expand 65536 (remainder 64 v) hr\n have he : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] *\n assemble 65536 [b₀%65536,b₁%65536,b₂%65536,b₃%65536] +\n assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] =\n assemble 65536 [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n have hqeq : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] = quotient 64 v := by\n simpa [qDigit, radix] using hqe.symm\n have hreq : assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] = remainder 64 v := by\n simpa [rDigit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 64 v hb\n have hcore := four_column_eq_core (qDigit 64 v 0) (qDigit 64 v 1)\n (qDigit 64 v 2) (qDigit 64 v 3) (rDigit 64 v 0) (rDigit 64 v 1)\n (rDigit 64 v 2) (rDigit 64 v 3) (b₀%65536) (b₁%65536)\n (b₂%65536) (b₃%65536) (a₀%65536) (a₁%65536)\n (a₂%65536) (a₃%65536)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide))\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n intro i hi\n have hi_cases : i = 0 ∨ i = 1 ∨ i = 2 ∨ i = 3 := by omega\n rcases hi_cases with rfl | rfl | rfl | rfl\n all_goals simp [carry, convolution, bDigit, inputDigit, v, limbs, radix,\n show List.range 4 = [0,1,2,3] by decide] at hcore ⊢\n all_goals omega\n\n\n/-- All strictly high diagonals vanish when the full product plus remainder\nfits in four radix digits. They are nonnegative, hence cannot cancel. -/\ntheorem four_column_high_zero_core (q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃\n b₀ b₁ b₂ b₃ a₀ a₁ a₂ a₃ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536)\n (ha₂ : a₂ < 65536) (ha₃ : a₃ < 65536)\n (he : assemble 65536 [q₀,q₁,q₂,q₃] *\n assemble 65536 [b₀,b₁,b₂,b₃] + assemble 65536 [r₀,r₁,r₂,r₃] =\n assemble 65536 [a₀,a₁,a₂,a₃]) :\n q₁*b₃+q₂*b₂+q₃*b₁ = 0 ∧ q₂*b₃+q₃*b₂ = 0 ∧ q₃*b₃ = 0 := by\n have hp := four_column_product q₀ q₁ q₂ q₃ r₀ r₁ r₂ r₃ b₀ b₁ b₂ b₃\n rw [he] at hp\n have hb : assemble 65536 [a₀,a₁,a₂,a₃] < 65536 ^ 4 := by\n apply assemble_bound 65536 [a₀,a₁,a₂,a₃] (by decide)\n intro d hd\n simp only [List.mem_cons, List.mem_nil_iff, or_false] at hd\n rcases hd with rfl | rfl | rfl | rfl\n all_goals assumption\n omega\n\n\nset_option maxHeartbeats 2000000\n/-- Every convolution column beyond the four-digit word is identically zero. -/\ntheorem sixty_four_bit_high_convolution_zero (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n convolution 64 v 4 = 0 ∧ convolution 64 v 5 = 0 ∧\n convolution 64 v 6 = 0 ∧ convolution 64 v 7 = 0 := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have ha : wordA 64 v = assemble 65536\n [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n simp [v, wordA, normalize, radix, limbs]\n have hbd : wordB 64 v = assemble 65536\n [b₀%65536,b₁%65536,b₂%65536,b₃%65536] := by\n simp [v, wordB, normalize, radix, limbs]\n have habound : wordA 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordA_bound 64 v\n have hbbound : wordB 64 v < 65536 ^ 4 := by\n simpa [v, radix, limbs] using wordB_bound 64 v\n have hq : quotient 64 v < 65536 ^ 4 :=\n Nat.lt_of_le_of_lt (quotient_le 64 v hb) habound\n have hr : remainder 64 v < 65536 ^ 4 :=\n Nat.lt_trans (remainder_lt 64 v hb) hbbound\n have hqe := four_digit_expand 65536 (quotient 64 v) hq\n have hre := four_digit_expand 65536 (remainder 64 v) hr\n have he : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] *\n assemble 65536 [b₀%65536,b₁%65536,b₂%65536,b₃%65536] +\n assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] =\n assemble 65536 [a₀%65536,a₁%65536,a₂%65536,a₃%65536] := by\n have hqeq : assemble 65536 [qDigit 64 v 0,qDigit 64 v 1,\n qDigit 64 v 2,qDigit 64 v 3] = quotient 64 v := by\n simpa [qDigit, radix] using hqe.symm\n have hreq : assemble 65536 [rDigit 64 v 0,rDigit 64 v 1,\n rDigit 64 v 2,rDigit 64 v 3] = remainder 64 v := by\n simpa [rDigit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 64 v hb\n have hh := four_column_high_zero_core (qDigit 64 v 0) (qDigit 64 v 1)\n (qDigit 64 v 2) (qDigit 64 v 3) (rDigit 64 v 0) (rDigit 64 v 1)\n (rDigit 64 v 2) (rDigit 64 v 3) (b₀%65536) (b₁%65536)\n (b₂%65536) (b₃%65536) (a₀%65536) (a₁%65536)\n (a₂%65536) (a₃%65536)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide))\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n have hrange : List.range 4 = [0,1,2,3] := by decide\n simp [convolution, hrange, bDigit, inputDigit, v, limbs, radix] at hh ⊢\n omega\n\n\n/-- The actual high carry slots 4..8 all vanish; not merely the last slot. -/\ntheorem sixty_four_bit_high_carries_zero (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n carry 64 v 4 = 0 ∧ carry 64 v 5 = 0 ∧ carry 64 v 6 = 0 ∧\n carry 64 v 7 = 0 ∧ carry 64 v 8 = 0 := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have hc₄ : carry 64 v 4 = 0 := sixty_four_bit_carry_zero a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb\n obtain ⟨h₄, h₅, h₆, h₇⟩ :=\n sixty_four_bit_high_convolution_zero a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb\n change convolution 64 v 4 = 0 at h₄\n change convolution 64 v 5 = 0 at h₅\n change convolution 64 v 6 = 0 at h₆\n change convolution 64 v 7 = 0 at h₇\n have hc₅ : carry 64 v 5 = 0 := by\n change (carry 64 v 4 + (if 4 < limbs 64 then rDigit 64 v 4 else 0) +\n convolution 64 v 4) / radix 64 = 0\n simp [limbs, radix, hc₄, h₄]\n have hc₆ : carry 64 v 6 = 0 := by\n change (carry 64 v 5 + (if 5 < limbs 64 then rDigit 64 v 5 else 0) +\n convolution 64 v 5) / radix 64 = 0\n simp [limbs, radix, hc₅, h₅]\n have hc₇ : carry 64 v 7 = 0 := by\n change (carry 64 v 6 + (if 6 < limbs 64 then rDigit 64 v 6 else 0) +\n convolution 64 v 6) / radix 64 = 0\n simp [limbs, radix, hc₆, h₆]\n have hc₈ : carry 64 v 8 = 0 := by\n change (carry 64 v 7 + (if 7 < limbs 64 then rDigit 64 v 7 else 0) +\n convolution 64 v 7) / radix 64 = 0\n simp [limbs, radix, hc₇, h₇]\n exact ⟨hc₄, hc₅, hc₆, hc₇, hc₈⟩\n\n/-- Every guarded high-column equation in the 64-bit machine is sound. -/\ntheorem sixty_four_bit_high_columns (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n ∀ i, 4 ≤ i → i < 8 →\n carry 64 v i + convolution 64 v i = 65536 * carry 64 v (i+1) := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n obtain ⟨hc₄,hc₅,hc₆,hc₇,hc₈⟩ :=\n sixty_four_bit_high_carries_zero a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb\n obtain ⟨h₄,h₅,h₆,h₇⟩ :=\n sixty_four_bit_high_convolution_zero a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb\n intro i hlow hhigh\n have hi : i = 4 ∨ i = 5 ∨ i = 6 ∨ i = 7 := by omega\n rcases hi with rfl | rfl | rfl | rfl\n all_goals simp [hc₄,hc₅,hc₆,hc₇,hc₈,h₄,h₅,h₆,h₇]\n\n\n/-- Each convolution coefficient is bounded independently of the divisor:\nnormalization applies even to zero-divisor and out-of-domain input limbs. -/\ntheorem convolution_bound (w : Nat) (v : List Nat) (col : Nat) :\n convolution w v col ≤ limbs w * (radix w * (radix w - 1)) := by\n let base := radix w\n let cap := base * (base - 1)\n have hterm (j : Nat) :\n (if j ≤ col ∧ col - j < limbs w then\n qDigit w v j * bDigit w v (col-j) else 0) ≤ cap := by\n split\n · have hq : qDigit w v j ≤ base - 1 := by\n have h := qDigit_lt w v j\n dsimp [base]\n omega\n have hb : bDigit w v (col-j) ≤ base := by\n have h := Nat.mod_lt (v[limbs w + (col-j)]?.getD 0) (radix_pos w)\n dsimp [bDigit, inputDigit, base]\n omega\n have hm := Nat.mul_le_mul hq hb\n dsimp [cap]\n simpa [Nat.mul_comm] using hm\n · exact Nat.zero_le _\n have hsum (xs : List Nat) :\n (xs.map fun j => if j ≤ col ∧ col-j < limbs w then\n qDigit w v j * bDigit w v (col-j) else 0).sum ≤ xs.length * cap := by\n induction xs with\n | nil => simp\n | cons j xs ih =>\n simp only [List.map_cons, List.sum_cons, List.length_cons, Nat.succ_mul]\n have hadd := Nat.add_le_add (hterm j) ih\n omega\n simpa [convolution, cap, base] using hsum (List.range (limbs w))\n\n/-- The TS rule's unconditional c_i ≤ n*base bound holds for every index,\nincluding zero divisors, arbitrary length lists and every natural width. -/\ntheorem carry_bound (w : Nat) (v : List Nat) (i : Nat) :\n carry w v i ≤ limbs w * radix w := by\n let base := radix w\n let n := limbs w\n have hb : 0 < base := radix_pos w\n induction i with\n | zero => simp [carry]\n | succ i ih =>\n change carry w v i ≤ n * base at ih\n let r := if i < n then rDigit w v i else 0\n have hr : r < base := by\n by_cases h : i < n\n · simp only [r, if_pos h]\n exact rDigit_lt w v i\n · simp only [r, if_neg h]\n exact hb\n have hconv : convolution w v i ≤ n * (base * (base - 1)) :=\n convolution_bound w v i\n have hpre : base - 1 + 1 = base := Nat.sub_add_cancel (Nat.one_le_iff_ne_zero.mpr (Nat.ne_of_gt hb))\n have halg : n * base + n * (base * (base - 1)) + base =\n base * (n * base + 1) := by\n calc\n n * base + n * (base * (base - 1)) + base =\n (n * base) * (1 + (base - 1)) + base := by\n simp [Nat.mul_add, Nat.mul_assoc, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm]\n _ = base * (n * base + 1) := by\n rw [Nat.add_comm 1 (base-1), hpre]\n simp [Nat.mul_add, Nat.mul_comm, Nat.mul_assoc]\n have htotal : carry w v i + r + convolution w v i <\n base * (n * base + 1) := by omega\n have hq : (carry w v i + r + convolution w v i) / base <\n n * base + 1 := (Nat.div_lt_iff_lt_mul hb).2 (by simpa [Nat.mul_comm] using htotal)\n change (carry w v i + r + convolution w v i) / base ≤ n * base\n omega\n\n\n/-- The concrete one-limb machine's two convolution columns, universally in\nboth operands; this covers the 8- and 16-bit specializations. -/\ntheorem single_limb_columns (w a b : Nat) (hn : limbs w = 1)\n (hb : b % radix w ≠ 0) :\n let v := [a,b]\n (carry w v 0 + rDigit w v 0 + convolution w v 0 =\n inputDigit w v 0 + radix w * carry w v 1) ∧\n (carry w v 1 + convolution w v 1 = radix w * carry w v 2) := by\n dsimp only\n let v := [a,b]\n have ha : wordA w v = a % radix w := by\n simp [v, wordA, normalize, assemble, hn]\n have hbd : wordB w v = b % radix w := by\n simp [v, wordB, normalize, assemble, hn]\n have hne : wordB w v ≠ 0 := by simpa [hbd, v] using hb\n have hid := division_identity w v hne\n have hq : quotient w v < radix w := by\n have hle := quotient_le w v hne\n have hm := Nat.mod_lt a (radix_pos w)\n omega\n have hr : remainder w v < radix w := by\n have hle := remainder_lt w v hne\n have hm := Nat.mod_lt b (radix_pos w)\n omega\n have hq₀ : qDigit w v 0 = quotient w v := by\n simp [qDigit, digit, Nat.mod_eq_of_lt hq]\n have hr₀ : rDigit w v 0 = remainder w v := by\n simp [rDigit, digit, Nat.mod_eq_of_lt hr]\n have hc₁ : carry w v 1 = 0 := by\n simpa [v] using single_limb_carry_zero w a b hn hb\n have hconv₀ : convolution w v 0 = quotient w v * (b % radix w) := by\n simp [convolution, hn, hq₀, bDigit, inputDigit, v]\n have hconv₁ : convolution w v 1 = 0 := by\n simp [convolution, hn]\n have hc₂ : carry w v 2 = 0 := by\n change (carry w v 1 + (if 1 < limbs w then rDigit w v 1 else 0) +\n convolution w v 1) / radix w = 0\n simp [hn, hc₁, hconv₁]\n dsimp only [v] at *\n constructor\n · have hi : quotient w [a,b] * (b % radix w) + remainder w [a,b] = a % radix w := by\n simpa [ha, hbd, Nat.mul_comm] using hid\n simp [carry_zero, hr₀, hconv₀, hc₁, inputDigit]\n omega\n · simpa [hc₁, hc₂, hconv₁]\n\n\n/-- Two bounded output digits uniquely determine the two low-column equations. -/\ntheorem two_column_eq_core (q₀ q₁ r₀ r₁ b₀ b₁ a₀ a₁ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536)\n (he : (q₀ + 65536*q₁) * (b₀ + 65536*b₁) +\n (r₀ + 65536*r₁) = a₀ + 65536*a₁) :\n let t₀ := r₀ + q₀*b₀\n let t₁ := r₁ + q₀*b₁ + q₁*b₀\n let c₁ := t₀ / 65536\n let c₂ := (c₁ + t₁) / 65536\n t₀ = a₀ + 65536*c₁ ∧ c₁+t₁ = a₁ + 65536*c₂ := by\n dsimp only\n have hring : (q₀ + 65536*q₁) * (b₀ + 65536*b₁) +\n (r₀ + 65536*r₁) =\n (r₀ + q₀*b₀) + 65536*(r₁+q₀*b₁+q₁*b₀) +\n 65536^2*(q₁*b₁) := by\n simp [Nat.pow_succ, Nat.mul_add, Nat.add_mul,\n Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm,\n Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]\n have hprod := hring.symm.trans he\n have hd := Nat.mod_add_div (r₀ + q₀*b₀) 65536\n have hd₁ := Nat.mod_add_div\n ((r₀ + q₀*b₀) / 65536 + r₁+q₀*b₁+q₁*b₀) 65536\n have hb₀ := Nat.mod_lt (r₀ + q₀*b₀) (by decide : 0 < 65536)\n have hb₁ := Nat.mod_lt\n ((r₀ + q₀*b₀) / 65536 + r₁+q₀*b₁+q₁*b₀) (by decide : 0 < 65536)\n have hcarry := two_column_carry_core 65536 q₀ q₁ r₀ r₁ b₀ b₁ a₀ a₁\n (by decide) ha₀ ha₁ he\n omega\n\n\nset_option maxHeartbeats 2000000\n/-- Both guarded low columns of the actual 32-bit helper. -/\ntheorem thirty_two_bit_low_columns (a₀ a₁ b₀ b₁ : Nat)\n (hb : wordB 32 [a₀,a₁,b₀,b₁] ≠ 0) :\n let v := [a₀,a₁,b₀,b₁]\n ∀ i < 2, carry 32 v i + rDigit 32 v i + convolution 32 v i =\n inputDigit 32 v i + 65536*carry 32 v (i+1) := by\n dsimp only\n let v := [a₀, a₁, b₀, b₁]\n have ha : wordA 32 v = a₀ % 65536 + 65536 * (a₁ % 65536) := by\n simp [v, wordA, normalize, assemble, radix, limbs]\n have hbd : wordB 32 v = b₀ % 65536 + 65536 * (b₁ % 65536) := by\n simp [v, wordB, normalize, assemble, radix, limbs]\n have habound : wordA 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordA_bound 32 v\n have hbbound : wordB 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordB_bound 32 v\n have hq : quotient 32 v < 65536 ^ 2 :=\n Nat.lt_of_le_of_lt (quotient_le 32 v hb) habound\n have hr : remainder 32 v < 65536 ^ 2 :=\n Nat.lt_trans (remainder_lt 32 v hb) hbbound\n have hqe := two_digit_expand 65536 (quotient 32 v) hq\n have hre := two_digit_expand 65536 (remainder 32 v) hr\n have he : (qDigit 32 v 0 + 65536*qDigit 32 v 1) *\n (b₀ % 65536 + 65536*(b₁ % 65536)) +\n (rDigit 32 v 0 + 65536*rDigit 32 v 1) =\n a₀ % 65536 + 65536*(a₁ % 65536) := by\n have hqeq : qDigit 32 v 0 + 65536*qDigit 32 v 1 = quotient 32 v := by\n simpa [qDigit, digit, radix] using hqe.symm\n have hreq : rDigit 32 v 0 + 65536*rDigit 32 v 1 = remainder 32 v := by\n simpa [rDigit, digit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 32 v hb\n have hcore := two_column_eq_core (qDigit 32 v 0) (qDigit 32 v 1)\n (rDigit 32 v 0) (rDigit 32 v 1) (b₀ % 65536) (b₁ % 65536)\n (a₀ % 65536) (a₁ % 65536)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n intro i hi\n have hs : i = 0 ∨ i = 1 := by omega\n rcases hs with rfl | rfl\n all_goals simp [carry, convolution, bDigit, inputDigit, v, limbs, radix,\n show List.range 2 = [0,1] by decide] at hcore ⊢\n all_goals omega\n\n\n/-- The unique high diagonal of a two-limb product cannot fit in its word. -/\ntheorem two_column_high_zero_core (q₀ q₁ r₀ r₁ b₀ b₁ a₀ a₁ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536)\n (he : (q₀ + 65536*q₁) * (b₀ + 65536*b₁) +\n (r₀ + 65536*r₁) = a₀ + 65536*a₁) : q₁*b₁ = 0 := by\n have hp : (q₀ + 65536*q₁) * (b₀ + 65536*b₁) +\n (r₀ + 65536*r₁) =\n (r₀ + q₀*b₀) + 65536*(r₁+q₀*b₁+q₁*b₀) +\n 65536^2*(q₁*b₁) := by\n simp [Nat.pow_succ, Nat.mul_add, Nat.add_mul,\n Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm,\n Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]\n have hs : a₀ + 65536*a₁ < 65536^2 := by omega\n have ht := hp.symm.trans he\n omega\n\n\n/-- Both guarded high columns and last carry of the actual 32-bit helper. -/\ntheorem thirty_two_bit_high_columns (a₀ a₁ b₀ b₁ : Nat)\n (hb : wordB 32 [a₀,a₁,b₀,b₁] ≠ 0) :\n let v := [a₀,a₁,b₀,b₁]\n carry 32 v 2 + convolution 32 v 2 = 65536*carry 32 v 3 ∧\n carry 32 v 3 + convolution 32 v 3 = 65536*carry 32 v 4 ∧\n carry 32 v 4 = 0 := by\n dsimp only\n let v := [a₀, a₁, b₀, b₁]\n have ha : wordA 32 v = a₀ % 65536 + 65536 * (a₁ % 65536) := by\n simp [v, wordA, normalize, assemble, radix, limbs]\n have hbd : wordB 32 v = b₀ % 65536 + 65536 * (b₁ % 65536) := by\n simp [v, wordB, normalize, assemble, radix, limbs]\n have habound : wordA 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordA_bound 32 v\n have hbbound : wordB 32 v < 65536 ^ 2 := by\n simpa [v, radix, limbs] using wordB_bound 32 v\n have hq : quotient 32 v < 65536 ^ 2 :=\n Nat.lt_of_le_of_lt (quotient_le 32 v hb) habound\n have hr : remainder 32 v < 65536 ^ 2 :=\n Nat.lt_trans (remainder_lt 32 v hb) hbbound\n have hqe := two_digit_expand 65536 (quotient 32 v) hq\n have hre := two_digit_expand 65536 (remainder 32 v) hr\n have he : (qDigit 32 v 0 + 65536*qDigit 32 v 1) *\n (b₀ % 65536 + 65536*(b₁ % 65536)) +\n (rDigit 32 v 0 + 65536*rDigit 32 v 1) =\n a₀ % 65536 + 65536*(a₁ % 65536) := by\n have hqeq : qDigit 32 v 0 + 65536*qDigit 32 v 1 = quotient 32 v := by\n simpa [qDigit, digit, radix] using hqe.symm\n have hreq : rDigit 32 v 0 + 65536*rDigit 32 v 1 = remainder 32 v := by\n simpa [rDigit, digit, radix] using hre.symm\n rw [hqeq, hreq, ← hbd, ← ha]\n simpa [Nat.mul_comm] using division_identity 32 v hb\n have hhigh := two_column_high_zero_core (qDigit 32 v 0) (qDigit 32 v 1)\n (rDigit 32 v 0) (rDigit 32 v 1) (b₀%65536) (b₁%65536)\n (a₀%65536) (a₁%65536)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide)) he\n have h₂ : convolution 32 v 2 = 0 := by\n have hrange : List.range 2 = [0,1] := by decide\n simpa [convolution, hrange, bDigit, inputDigit, v, limbs, radix] using hhigh\n have h₃ : convolution 32 v 3 = 0 := by\n simp [convolution, show List.range 2 = [0,1] by decide, limbs]\n have hc₂ : carry 32 v 2 = 0 := thirty_two_bit_carry_zero a₀ a₁ b₀ b₁ hb\n have hc₃ : carry 32 v 3 = 0 := by\n change (carry 32 v 2 + (if 2 < limbs 32 then rDigit 32 v 2 else 0) +\n convolution 32 v 2) / radix 32 = 0\n simp [limbs, radix, hc₂, h₂]\n have hc₄ : carry 32 v 4 = 0 := by\n change (carry 32 v 3 + (if 3 < limbs 32 then rDigit 32 v 3 else 0) +\n convolution 32 v 3) / radix 32 = 0\n simp [limbs, radix, hc₃, h₃]\n dsimp only [v] at *\n simpa [hc₂,hc₃,hc₄,h₂,h₃]\n\n#print axioms WordDivision.thirty_two_bit_high_columns\n#print axioms WordDivision.two_column_high_zero_core\n#print axioms WordDivision.thirty_two_bit_low_columns\n#print axioms WordDivision.two_column_eq_core\n#print axioms WordDivision.single_limb_columns\n#print axioms WordDivision.carry_bound\n#print axioms WordDivision.sixty_four_bit_high_columns\n#print axioms WordDivision.sixty_four_bit_high_carries_zero\n#print axioms WordDivision.sixty_four_bit_high_convolution_zero\n#print axioms WordDivision.four_column_high_zero_core\n#print axioms WordDivision.sixty_four_bit_columns\n#print axioms WordDivision.zero_divisor_carry\n#print axioms WordDivision.single_limb_carry_zero\n#print axioms WordDivision.thirty_two_bit_carry_zero\n#print axioms WordDivision.sixty_four_bit_carry_zero\n#print axioms WordDivision.four_column_carry_core\n#print axioms WordDivision.division_identity\n#print axioms WordDivision.remainder_lt\n#print axioms WordDivision.quotient_le\nend WordDivision\n",
53
+ "WordDivisionBridge.lean": "import Kernel\nimport WordDivision\n\n/-! Sound checker extension for *proved* facts about one fixed concrete map interpretation.\nThe ordinary Kern.checkMachine is unchanged. Extra premises are checked as proof\nhypotheses, and are discharged separately for every environment; no declaration\nof a reserved map may substitute for the semantic proof. -/\nnamespace WordDivisionBridge\nopen Kern\n\nabbrev FactProof := List (C × (List VDef × Tree) × (List VDef × Tree))\n\ndef startGoal (m : Machine) (law : C) (extra : List C) : List C :=\n Kern.startGoal m law ++ extra\n\ndef stepGoal (m : Machine) (ps : FactProof) (law : C) (extra : List C) : List C :=\n Kern.stepGoal m (ps.map (·.1)) law ++ extra\n\ndef checkWithFacts (m : Machine) (ps : FactProof) (extra : List C) : Bool :=\n preOk m && ps.all fun p => avoids m.inputs p.1\n && checkGoal ⟨p.2.1.1, startGoal m p.1 extra⟩ p.2.1.2\n && checkGoal ⟨p.2.2.1, stepGoal m ps p.1 extra⟩ p.2.2.2\n\n/-- Every accepted tree yields an actual invariant when its additional facts\nare *proved for the concrete interpretation at every environment*. -/\ntheorem checkWithFacts_sound (m : Machine) (ps : FactProof) (extra : List C)\n (h : checkWithFacts m ps extra = true) (maps : Nat → Nat → Nat)\n (facts : ∀ env, ∀ c ∈ extra, evalC env maps c = true) :\n ∀ ρ, Reach m maps ρ → ∀ l ∈ ps.map (·.1), evalC ρ maps l = true := by\n simp only [checkWithFacts, List.all_eq_true, Bool.and_eq_true] at h\n obtain ⟨hpre, h⟩ := h\n intro ρ hr\n induction hr with\n | start ρ h0 =>\n intro l hl\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl\n have hs := Kern.sound _ _ (h p hp).1.2 ρ maps\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [startGoal] at hc\n rcases List.mem_append.mp hc with hc | hc\n · simp only [Kern.startGoal, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc\n rcases hc with rfl | rfl\n · exact h0\n · simp only [evalC, subst_evalC]; simpa using hf\n · exact facts ρ c hc\n | step ρ ι hr ha ih =>\n intro l hl\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl\n have hs := Kern.sound _ _ (h p hp).2 (withInputs m.inputs ρ ι) maps\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [stepGoal] at hc\n rcases List.mem_append.mp hc with hc | hc\n · simp only [Kern.stepGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc\n rcases hc with ((rfl | rfl) | hc) | rfl\n · rw [evalC_avoids hpre maps (ρ' := ρ) (fun i hi => withInputs_other (fun h => hi (by simp [h])) ρ ι)]\n exact pre_reach hpre hr\n · exact ha\n · obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hc\n rw [evalC_avoids (h q hq).1.1 maps (ρ' := ρ) (fun i hi => withInputs_other hi ρ ι)]\n exact ih q.1 (List.mem_map.mpr ⟨q, hq, rfl⟩)\n · simp only [evalC, subst_evalC]; simpa using hf\n · exact facts (withInputs m.inputs ρ ι) c hc\n\n/-! Per-obligation facts match the original generated certificate's order: they\nare inserted immediately before the negated conclusion, not appended after it.\nThis is the shape required by leaf row indices in real proof trees. -/\nstructure Entry where\n law : C\n startFacts : List C\n stepFacts : List C\n start : List VDef × Tree\n step : List VDef × Tree\n\ndef startGoalAt (m : Machine) (p : Entry) : List C :=\n [m.pre] ++ p.startFacts ++ [.not (substC m.init p.law)]\n\ndef stepGoalAt (m : Machine) (ps : List Entry) (p : Entry) : List C :=\n [m.pre, m.assume] ++ ps.map (·.law) ++ p.stepFacts ++ [.not (substC m.step p.law)]\n\ndef checkEntries (m : Machine) (ps : List Entry) : Bool :=\n preOk m && ps.all fun p => avoids m.inputs p.law\n && checkGoal ⟨p.start.1, startGoalAt m p⟩ p.start.2\n && checkGoal ⟨p.step.1, stepGoalAt m ps p⟩ p.step.2\n\n/-- The exact generated-facts checker shape: each obligation may use only its\nown facts, and every such fact is proved for every environment under the fixed\nconcrete map interpretation. -/\ntheorem checkEntries_sound (m : Machine) (ps : List Entry)\n (h : checkEntries m ps = true) (maps : Nat → Nat → Nat)\n (facts : ∀ env p, p ∈ ps →\n (∀ c ∈ p.startFacts, evalC env maps c = true) ∧\n (∀ c ∈ p.stepFacts, evalC env maps c = true)) :\n ∀ ρ, Reach m maps ρ → ∀ l ∈ ps.map (·.law), evalC ρ maps l = true := by\n simp only [checkEntries, List.all_eq_true, Bool.and_eq_true] at h\n obtain ⟨hpre, h⟩ := h\n intro ρ hr\n induction hr with\n | start ρ h0 =>\n intro l hl\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl\n have hs := Kern.sound _ _ (h p hp).1.2 ρ maps\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [startGoalAt, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc\n rcases hc with (rfl | hc) | rfl\n · exact h0\n · exact (facts ρ p hp).1 c hc\n · simp only [evalC, subst_evalC]; simpa using hf\n | step ρ ι hr ha ih =>\n intro l hl\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl\n have hs := Kern.sound _ _ (h p hp).2 (withInputs m.inputs ρ ι) maps\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [stepGoalAt, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc\n rcases hc with (((rfl | rfl) | hc) | hc) | rfl\n · rw [evalC_avoids hpre maps (ρ' := ρ) (fun i hi => withInputs_other (fun h => hi (by simp [h])) ρ ι)]\n exact pre_reach hpre hr\n · exact ha\n · obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hc\n rw [evalC_avoids (h q hq).1.1 maps (ρ' := ρ) (fun i hi => withInputs_other hi ρ ι)]\n exact ih q.law (List.mem_map.mpr ⟨q, hq, rfl⟩)\n · exact (facts (withInputs m.inputs ρ ι) p hp).2 c hc\n · simp only [evalC, subst_evalC]; simpa using hf\n\n#print axioms WordDivisionBridge.checkEntries_sound\n\n/-- The unconditional quotient-digit bound is universal in the machine inputs,\nnot merely a closed test vector. -/\ntheorem q8_bound (a b : Nat) : WordDivision.value 8 0 0 [a,b] < 256 := by\n simpa [WordDivision.radix] using WordDivision.value_q_lt 8 0 [a,b]\n\n/-- Concrete map semantics for a one-limb quotient. The right tuple key is\nunpaired by the same Kern.pickP operations as the exporter. -/\ndef map8q (base : Nat → Nat → Nat) (mp key : Nat) : Nat :=\n if mp = 0 then WordDivision.value 8 0 0 [Kern.pickP 0 key, Kern.pickP 1 key]\n else base mp key\n\ntheorem map8q_range (base : Nat → Nat → Nat) (env : Nat → Nat) (key : E) :\n evalC env (map8q base) (.lt (.get 0 key) (.const 256)) = true := by\n have h := WordDivision.value_q_lt 8 0\n [Kern.pickP 0 (evalE env (map8q base) key),\n Kern.pickP 1 (evalE env (map8q base) key)]\n simpa [evalC, evalE, map8q, WordDivision.radix] using h\n\n#print axioms WordDivisionBridge.map8q_range\n\n/-- All digit and carry range facts emitted by the TypeScript generator have\nsemantic proofs for arbitrary input limbs, including unbounded operands. -/\ntheorem quotient_digit_bound (w i : Nat) (v : List Nat) :\n WordDivision.value w 0 i v < WordDivision.radix w :=\n WordDivision.value_q_lt w i v\n\ntheorem remainder_digit_bound (w i : Nat) (v : List Nat) :\n WordDivision.value w 1 i v < WordDivision.radix w :=\n WordDivision.value_r_lt w i v\n\ntheorem carry_digit_bound (w i : Nat) (v : List Nat) :\n WordDivision.value w 2 i v ≤ WordDivision.limbs w * WordDivision.radix w := by\n rw [WordDivision.value_c]\n exact WordDivision.carry_bound w v i\n\ntheorem single_limb_remainder_lex (w a b : Nat) (hn : WordDivision.limbs w = 1)\n (hb : WordDivision.wordB w [a,b] ≠ 0) :\n WordDivision.rDigit w [a,b] 0 < b % WordDivision.radix w := by\n have hr := WordDivision.remainder_lt w [a,b] hb\n have hbe : WordDivision.wordB w [a,b] = b % WordDivision.radix w := by\n simp [WordDivision.wordB, WordDivision.normalize, WordDivision.assemble, hn]\n have hlt : WordDivision.remainder w [a,b] < WordDivision.radix w := by\n rw [hbe] at hr\n exact Nat.lt_trans hr (Nat.mod_lt b (WordDivision.radix_pos w))\n have hd : WordDivision.rDigit w [a,b] 0 = WordDivision.remainder w [a,b] := by\n simp [WordDivision.rDigit, WordDivision.digit, Nat.mod_eq_of_lt hlt]\n simpa [hbe, hd] using hr\n\ntheorem single_limb_quotient_lex (w a b : Nat) (hn : WordDivision.limbs w = 1)\n (hb : WordDivision.wordB w [a,b] ≠ 0) :\n WordDivision.qDigit w [a,b] 0 ≤ a % WordDivision.radix w := by\n have hq := WordDivision.quotient_le w [a,b] hb\n have hae : WordDivision.wordA w [a,b] = a % WordDivision.radix w := by\n simp [WordDivision.wordA, WordDivision.normalize, WordDivision.assemble, hn]\n have hlt : WordDivision.quotient w [a,b] < WordDivision.radix w := by\n rw [hae] at hq\n exact Nat.lt_of_le_of_lt hq (Nat.mod_lt a (WordDivision.radix_pos w))\n have hd : WordDivision.qDigit w [a,b] 0 = WordDivision.quotient w [a,b] := by\n simp [WordDivision.qDigit, WordDivision.digit, Nat.mod_eq_of_lt hlt]\n simpa [hae, hd] using hq\n\n#print axioms WordDivisionBridge.single_limb_remainder_lex\n#print axioms WordDivisionBridge.single_limb_quotient_lex\n\n/-- Two-limb radix comparison; for 32-bit normalized word arrays. -/\ntheorem two_digit_lt_lex (a₀ a₁ b₀ b₁ : Nat)\n (ha₀ : a₀ < 65536) (hb₀ : b₀ < 65536)\n (h : WordDivision.assemble 65536 [a₀,a₁] <\n WordDivision.assemble 65536 [b₀,b₁]) :\n a₁ < b₁ ∨ (a₁ = b₁ ∧ a₀ < b₀) := by\n simp only [WordDivision.assemble, Nat.mul_zero, Nat.add_zero] at h\n omega\n\ntheorem two_digit_le_lex (a₀ a₁ b₀ b₁ : Nat)\n (ha₀ : a₀ < 65536) (hb₀ : b₀ < 65536)\n (h : WordDivision.assemble 65536 [a₀,a₁] ≤\n WordDivision.assemble 65536 [b₀,b₁]) :\n a₁ < b₁ ∨ (a₁ = b₁ ∧ a₀ < b₀) ∨ (a₁ = b₁ ∧ a₀ = b₀) := by\n simp only [WordDivision.assemble, Nat.mul_zero, Nat.add_zero] at h\n omega\n\ntheorem remainder32_digit_lex (a₀ a₁ b₀ b₁ : Nat)\n (hb : WordDivision.wordB 32 [a₀,a₁,b₀,b₁] ≠ 0) :\n let v := [a₀,a₁,b₀,b₁]\n WordDivision.rDigit 32 v 1 < b₁ % 65536 ∨\n (WordDivision.rDigit 32 v 1 = b₁ % 65536 ∧\n WordDivision.rDigit 32 v 0 < b₀ % 65536) := by\n dsimp only\n let v := [a₀,a₁,b₀,b₁]\n have hr := WordDivision.remainder_lt 32 v hb\n have hbound : WordDivision.wordB 32 v < 65536 ^ 2 := by\n simpa [v, WordDivision.radix, WordDivision.limbs] using WordDivision.wordB_bound 32 v\n have he := WordDivision.two_digit_expand 65536 (WordDivision.remainder 32 v) (Nat.lt_trans hr hbound)\n have hre : WordDivision.assemble 65536\n [WordDivision.rDigit 32 v 0, WordDivision.rDigit 32 v 1] =\n WordDivision.remainder 32 v := by\n simpa [WordDivision.rDigit, WordDivision.digit, WordDivision.radix,\n WordDivision.assemble] using he.symm\n have hbe : WordDivision.wordB 32 v = WordDivision.assemble 65536\n [b₀ % 65536, b₁ % 65536] := by\n simp [v, WordDivision.wordB, WordDivision.normalize, WordDivision.radix,\n WordDivision.limbs]\n rw [← hre, hbe] at hr\n exact two_digit_lt_lex _ _ _ _\n (by simpa [WordDivision.radix] using WordDivision.rDigit_lt 32 v 0)\n (Nat.mod_lt _ (by decide)) hr\n\ntheorem quotient32_digit_lex (a₀ a₁ b₀ b₁ : Nat)\n (hb : WordDivision.wordB 32 [a₀,a₁,b₀,b₁] ≠ 0) :\n let v := [a₀,a₁,b₀,b₁]\n WordDivision.qDigit 32 v 1 < a₁ % 65536 ∨\n (WordDivision.qDigit 32 v 1 = a₁ % 65536 ∧\n WordDivision.qDigit 32 v 0 < a₀ % 65536) ∨\n (WordDivision.qDigit 32 v 1 = a₁ % 65536 ∧\n WordDivision.qDigit 32 v 0 = a₀ % 65536) := by\n dsimp only\n let v := [a₀,a₁,b₀,b₁]\n have hq := WordDivision.quotient_le 32 v hb\n have hbound : WordDivision.wordA 32 v < 65536 ^ 2 := by\n simpa [v, WordDivision.radix, WordDivision.limbs] using WordDivision.wordA_bound 32 v\n have he := WordDivision.two_digit_expand 65536 (WordDivision.quotient 32 v) (Nat.lt_of_le_of_lt hq hbound)\n have hqe : WordDivision.assemble 65536\n [WordDivision.qDigit 32 v 0, WordDivision.qDigit 32 v 1] =\n WordDivision.quotient 32 v := by\n simpa [WordDivision.qDigit, WordDivision.digit, WordDivision.radix,\n WordDivision.assemble] using he.symm\n have hae : WordDivision.wordA 32 v = WordDivision.assemble 65536\n [a₀ % 65536, a₁ % 65536] := by\n simp [v, WordDivision.wordA, WordDivision.normalize, WordDivision.radix,\n WordDivision.limbs]\n rw [← hqe, hae] at hq\n exact two_digit_le_lex _ _ _ _\n (by simpa [WordDivision.radix] using WordDivision.qDigit_lt 32 v 0)\n (Nat.mod_lt _ (by decide)) hq\n\n#print axioms WordDivisionBridge.remainder32_digit_lex\n#print axioms WordDivisionBridge.quotient32_digit_lex\n#print axioms WordDivisionBridge.two_digit_lt_lex\n#print axioms WordDivisionBridge.two_digit_le_lex\n\n/-- Arithmetic comparison of four bounded radix-65536 digit arrays. The input\nand output use the same most-significant-first alternatives as digitLex in the\nTypeScript intrinsic fact generator. -/\ntheorem four_digit_lt_lex (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536) (ha₂ : a₂ < 65536)\n (hb₀ : b₀ < 65536) (hb₁ : b₁ < 65536) (hb₂ : b₂ < 65536)\n (h : WordDivision.assemble 65536 [a₀,a₁,a₂,a₃] <\n WordDivision.assemble 65536 [b₀,b₁,b₂,b₃]) :\n a₃ < b₃ ∨ (a₃ = b₃ ∧ a₂ < b₂) ∨\n (a₃ = b₃ ∧ a₂ = b₂ ∧ a₁ < b₁) ∨\n (a₃ = b₃ ∧ a₂ = b₂ ∧ a₁ = b₁ ∧ a₀ < b₀) := by\n simp only [WordDivision.assemble, Nat.mul_zero, Nat.add_zero] at h\n omega\n\ntheorem four_digit_le_lex (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (ha₀ : a₀ < 65536) (ha₁ : a₁ < 65536) (ha₂ : a₂ < 65536)\n (hb₀ : b₀ < 65536) (hb₁ : b₁ < 65536) (hb₂ : b₂ < 65536)\n (h : WordDivision.assemble 65536 [a₀,a₁,a₂,a₃] ≤\n WordDivision.assemble 65536 [b₀,b₁,b₂,b₃]) :\n a₃ < b₃ ∨ (a₃ = b₃ ∧ a₂ < b₂) ∨\n (a₃ = b₃ ∧ a₂ = b₂ ∧ a₁ < b₁) ∨\n (a₃ = b₃ ∧ a₂ = b₂ ∧ a₁ = b₁ ∧ a₀ < b₀) ∨\n (a₃ = b₃ ∧ a₂ = b₂ ∧ a₁ = b₁ ∧ a₀ = b₀) := by\n simp only [WordDivision.assemble, Nat.mul_zero, Nat.add_zero] at h\n omega\n\ntheorem remainder64_digit_lex (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : WordDivision.wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n WordDivision.rDigit 64 v 3 < b₃ % 65536 ∨\n (WordDivision.rDigit 64 v 3 = b₃ % 65536 ∧ WordDivision.rDigit 64 v 2 < b₂ % 65536) ∨\n (WordDivision.rDigit 64 v 3 = b₃ % 65536 ∧ WordDivision.rDigit 64 v 2 = b₂ % 65536 ∧ WordDivision.rDigit 64 v 1 < b₁ % 65536) ∨\n (WordDivision.rDigit 64 v 3 = b₃ % 65536 ∧ WordDivision.rDigit 64 v 2 = b₂ % 65536 ∧ WordDivision.rDigit 64 v 1 = b₁ % 65536 ∧ WordDivision.rDigit 64 v 0 < b₀ % 65536) := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have hr := WordDivision.remainder_lt 64 v hb\n have hbound : WordDivision.wordB 64 v < 65536 ^ 4 := by\n simpa [v, WordDivision.radix, WordDivision.limbs] using WordDivision.wordB_bound 64 v\n have he := WordDivision.four_digit_expand 65536 (WordDivision.remainder 64 v) (Nat.lt_trans hr hbound)\n have hre : WordDivision.assemble 65536\n [WordDivision.rDigit 64 v 0, WordDivision.rDigit 64 v 1,\n WordDivision.rDigit 64 v 2, WordDivision.rDigit 64 v 3] =\n WordDivision.remainder 64 v := by\n simpa [WordDivision.rDigit, WordDivision.radix] using he.symm\n have hbe : WordDivision.wordB 64 v = WordDivision.assemble 65536\n [b₀ % 65536, b₁ % 65536, b₂ % 65536, b₃ % 65536] := by\n simp [v, WordDivision.wordB, WordDivision.normalize, WordDivision.radix,\n WordDivision.limbs]\n rw [← hre, hbe] at hr\n exact four_digit_lt_lex _ _ _ _ _ _ _ _\n (by simpa [WordDivision.radix] using WordDivision.rDigit_lt 64 v 0)\n (by simpa [WordDivision.radix] using WordDivision.rDigit_lt 64 v 1)\n (by simpa [WordDivision.radix] using WordDivision.rDigit_lt 64 v 2)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide))\n (Nat.mod_lt _ (by decide)) hr\n\ntheorem quotient64_digit_lex (a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ : Nat)\n (hb : WordDivision.wordB 64 [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃] ≠ 0) :\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n WordDivision.qDigit 64 v 3 < a₃ % 65536 ∨\n (WordDivision.qDigit 64 v 3 = a₃ % 65536 ∧ WordDivision.qDigit 64 v 2 < a₂ % 65536) ∨\n (WordDivision.qDigit 64 v 3 = a₃ % 65536 ∧ WordDivision.qDigit 64 v 2 = a₂ % 65536 ∧ WordDivision.qDigit 64 v 1 < a₁ % 65536) ∨\n (WordDivision.qDigit 64 v 3 = a₃ % 65536 ∧ WordDivision.qDigit 64 v 2 = a₂ % 65536 ∧ WordDivision.qDigit 64 v 1 = a₁ % 65536 ∧ WordDivision.qDigit 64 v 0 < a₀ % 65536) ∨\n (WordDivision.qDigit 64 v 3 = a₃ % 65536 ∧ WordDivision.qDigit 64 v 2 = a₂ % 65536 ∧ WordDivision.qDigit 64 v 1 = a₁ % 65536 ∧ WordDivision.qDigit 64 v 0 = a₀ % 65536) := by\n dsimp only\n let v := [a₀,a₁,a₂,a₃,b₀,b₁,b₂,b₃]\n have hq := WordDivision.quotient_le 64 v hb\n have hbound : WordDivision.wordA 64 v < 65536 ^ 4 := by\n simpa [v, WordDivision.radix, WordDivision.limbs] using WordDivision.wordA_bound 64 v\n have he := WordDivision.four_digit_expand 65536 (WordDivision.quotient 64 v) (Nat.lt_of_le_of_lt hq hbound)\n have hqe : WordDivision.assemble 65536\n [WordDivision.qDigit 64 v 0, WordDivision.qDigit 64 v 1,\n WordDivision.qDigit 64 v 2, WordDivision.qDigit 64 v 3] =\n WordDivision.quotient 64 v := by\n simpa [WordDivision.qDigit, WordDivision.radix] using he.symm\n have hae : WordDivision.wordA 64 v = WordDivision.assemble 65536\n [a₀ % 65536, a₁ % 65536, a₂ % 65536, a₃ % 65536] := by\n simp [v, WordDivision.wordA, WordDivision.normalize, WordDivision.radix,\n WordDivision.limbs]\n rw [← hqe, hae] at hq\n exact four_digit_le_lex _ _ _ _ _ _ _ _\n (by simpa [WordDivision.radix] using WordDivision.qDigit_lt 64 v 0)\n (by simpa [WordDivision.radix] using WordDivision.qDigit_lt 64 v 1)\n (by simpa [WordDivision.radix] using WordDivision.qDigit_lt 64 v 2)\n (Nat.mod_lt _ (by decide)) (Nat.mod_lt _ (by decide))\n (Nat.mod_lt _ (by decide)) hq\n\n#print axioms WordDivisionBridge.remainder64_digit_lex\n#print axioms WordDivisionBridge.quotient64_digit_lex\n#print axioms WordDivisionBridge.four_digit_lt_lex\n#print axioms WordDivisionBridge.four_digit_le_lex\n#print axioms WordDivisionBridge.checkWithFacts_sound\n#print axioms WordDivisionBridge.quotient_digit_bound\n#print axioms WordDivisionBridge.remainder_digit_bound\n#print axioms WordDivisionBridge.carry_digit_bound\nend WordDivisionBridge\n",
54
+ "WordReach.lean": "import Sets\nimport WordDivision\n\n/-! Runs with concrete immutable reserved word maps. These runs do not broaden\n`Kern.SReach`: a start uses a concrete overlay, and every transition is exactly\nthe source machine's transition, including arbitrary carried maps. -/\nnamespace Kern.WordReach\n\n/-- Pointwise, at every key of every reserved map, not just at a proof's reads. -/\ndef WordInterpret (reserved : List Nat) (value M : Nat → Nat → Nat) : Prop :=\n ∀ mp ∈ reserved, ∀ key, M mp key = value mp key\n\n/-- Right-biased concrete interpretation; all ordinary map IDs retain `base`. -/\ndef overlay (reserved : List Nat) (value base : Nat → Nat → Nat) : Nat → Nat → Nat :=\n fun mp key => if reserved.contains mp then value mp key else base mp key\n\ntheorem overlay_interpret (reserved : List Nat) (value base : Nat → Nat → Nat) :\n WordInterpret reserved value (overlay reserved value base) := by\n intro mp hm key\n simp [overlay, hm]\n\ntheorem overlay_other (reserved : List Nat) (value base : Nat → Nat → Nat)\n (mp : Nat) (hm : mp ∉ reserved) (key : Nat) :\n overlay reserved value base mp key = base mp key := by\n simp [overlay, hm]\n\n/-- The reserved namespace is untouched by initialization and by all three\nkinds of map transition: request-carried, point writes, and every-key writes. -/\nstructure WordSMachine where\n s : SMachine\n reserved : List Nat\n value : Nat → Nat → Nat\n startDisjoint : ∀ p ∈ s.f.m.maps0, p.1 ∉ reserved\n carriedDisjoint : ∀ mp ∈ s.carried, mp ∉ reserved\n everyDisjoint : ∀ w ∈ s.every, w.1 ∉ reserved\n writesDisjoint : ∀ w ∈ s.f.m.writes, w.1 ∉ reserved\n\ntheorem overlay_start (w : WordSMachine) (base : Nat → Nat → Nat)\n (h : StartMaps w.s.f.m base) : StartMaps w.s.f.m (overlay w.reserved w.value base) := by\n intro p hp key\n rw [overlay_other _ _ _ _ (w.startDisjoint p hp)]\n exact h p hp key\n\n/-- Exact decoding for a reserved getter at an arbitrary expression key. -/\ntheorem read_interpret {reserved : List Nat} {value M : Nat → Nat → Nat}\n (h : WordInterpret reserved value M) (X : Nat → Nat) (mp : Nat)\n (hm : mp ∈ reserved) (key : E) :\n evalE X M (.get mp key) = value mp (evalE X M key) := by\n exact h mp hm (evalE X M key)\n\ntheorem carried_interpret (w : WordSMachine) {M κ : Nat → Nat → Nat}\n (h : WordInterpret w.reserved w.value M) :\n WordInterpret w.reserved w.value (withCarried w.s M κ) := by\n intro mp hm key\n have hc : mp ∉ w.s.carried := by\n intro hmp\n exact w.carriedDisjoint mp hmp hm\n simp [withCarried, hc, h mp hm key]\n\ntheorem every_interpret (w : WordSMachine) {D : Nat → Nat}\n {N : Nat → Nat → Nat} (h : WordInterpret w.reserved w.value N) :\n WordInterpret w.reserved w.value (everyMaps w.s D N) := by\n intro mp hm key\n have he : lookup (fun entry => entry.1 == mp) w.s.every = none := by\n apply lookup_none\n intro entry he\n simp only [beq_eq_false_iff_ne]\n intro eq\n exact w.everyDisjoint entry he (eq ▸ hm)\n have hw : lookup (fun entry => entry.1 == mp) w.s.f.m.writes = none := by\n apply lookup_none\n intro entry he\n simp only [beq_eq_false_iff_ne]\n intro eq\n exact w.writesDisjoint entry he (eq ▸ hm)\n simpa [everyMaps, writeMaps, he, hw] using h mp hm key\n\n/-- A reserved map cannot be silently substituted by a carried frame name. -/\ntheorem frameRd_reserved (w : WordSMachine) (mr : List (Nat × Nat))\n (hframe : ∀ p ∈ mr, p.1 ∈ w.s.carried)\n (rd : Nat → E → E) (mp : Nat) (hm : mp ∈ w.reserved) (key : E) :\n frameRd mr rd mp key = rd mp key := by\n have hn : lookup (fun p => p.1 == mp) mr = none := by\n apply lookup_none\n intro p hp\n simp only [beq_eq_false_iff_ne]\n intro he\n exact w.carriedDisjoint p.1 (hframe p hp) (he ▸ hm)\n simp [frameRd, hn]\n\n/-- A reserved map is unaffected by either form of source-step map rewrite. -/\ntheorem stepRdS_reserved (w : WordSMachine) (τ : List (Nat × E))\n (rd : Nat → E → E) (mp : Nat) (hm : mp ∈ w.reserved) (key : E) :\n stepRdS w.s τ rd mp key = rd mp key := by\n have he : lookup (fun entry => entry.1 == mp) w.s.every = none := by\n apply lookup_none\n intro entry hp\n simp only [beq_eq_false_iff_ne]\n intro eq\n exact w.everyDisjoint entry hp (eq ▸ hm)\n have hw : lookup (fun entry => entry.1 == mp) w.s.f.m.writes = none := by\n apply lookup_none\n intro entry hp\n simp only [beq_eq_false_iff_ne]\n intro eq\n exact w.writesDisjoint entry hp (eq ▸ hm)\n simp [stepRdS, stepRd, he, hw]\n\ntheorem view_interpret (w : WordSMachine) {ρ ι : Nat → Nat}\n {M κ : Nat → Nat → Nat} (h : WordInterpret w.reserved w.value M) :\n WordInterpret w.reserved w.value (sView w.s ρ M ι κ).2 := by\n exact carried_interpret w h\n\ntheorem next_interpret (w : WordSMachine) {ρ ι : Nat → Nat}\n {M κ : Nat → Nat → Nat} (h : WordInterpret w.reserved w.value M) :\n WordInterpret w.reserved w.value (sNext w.s ρ M ι κ).2 := by\n exact every_interpret w (view_interpret w h)\n\n/-- Starts from the actual concrete overlay and follows *precisely* `SReach`.\nNo arbitrary initial word maps, no stipulated helper law, no altered admission. -/\ninductive WordSReach (w : WordSMachine) : (Nat → Nat) → (Nat → Nat → Nat) → Prop\n | start (ρ : Nat → Nat) (base : Nat → Nat → Nat) :\n StartMaps w.s.f.m base →\n evalC ρ (overlay w.reserved w.value base) w.s.f.m.pre = true →\n (∀ l ∈ w.s.facts, l.Holds ρ (overlay w.reserved w.value base)) →\n w.s.PFacts ρ (overlay w.reserved w.value base) →\n WordSReach w (applyS w.s.f.m.init ρ (overlay w.reserved w.value base))\n (overlay w.reserved w.value base)\n | step (ρ : Nat → Nat) (M : Nat → Nat → Nat)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n WordSReach w ρ M →\n evalC (sView w.s ρ M ι κ).1 (sView w.s ρ M ι κ).2 w.s.f.m.assume = true →\n WordSReach w (sNext w.s ρ M ι κ).1 (sNext w.s ρ M ι κ).2\n\n/-- Forget concrete-word provenance, preserving the unmodified source reach. -/\ntheorem word_sreach_sreach (w : WordSMachine) :\n ∀ {ρ M}, WordSReach w ρ M → SReach w.s ρ M := by\n intro ρ M hr\n induction hr with\n | start ρ base hm hp hf hpf =>\n exact SReach.start ρ (overlay w.reserved w.value base)\n (overlay_start w base hm) hp hf hpf\n | step ρ M ι κ _ ha ih => exact SReach.step ρ M ι κ ih ha\n\n/-- Every reachable state retains the concrete right-key interpretation at\n*every* reserved map and key, including across arbitrary request maps. -/\ntheorem word_sreach_interpret (w : WordSMachine) :\n ∀ {ρ M}, WordSReach w ρ M → WordInterpret w.reserved w.value M := by\n intro ρ M hr\n induction hr with\n | start ρ base _ _ _ _ => exact overlay_interpret _ _ _\n | step ρ M ι κ _ _ ih => exact next_interpret w ih\n\n/-- Map semantics at an admitted frame, before the step reads its terms. -/\ntheorem word_sreach_view_interpret (w : WordSMachine) {ρ M ι κ}\n (hr : WordSReach w ρ M) :\n WordInterpret w.reserved w.value (sView w.s ρ M ι κ).2 :=\n view_interpret w (word_sreach_interpret w hr)\n\nend Kern.WordReach\n",
55
+ "WordLex.lean": "import WordDivision\n\n/-! Numeric order implies most-significant-differing-digit order for bounded\nlittle-endian words, independently of the number of digits. -/\nnamespace WordLex\n\n/-- A strict comparison at one digit, with equality at all higher digits. -/\ndef lt (xs ys : List Nat) : Prop :=\n ∃ i, i < xs.length ∧\n (∀ j, i < j → j < xs.length → xs[j]?.getD 0 = ys[j]?.getD 0) ∧\n xs[i]?.getD 0 < ys[i]?.getD 0\n\n/-- Non-strict little-endian lexicographic order, including word equality. -/\ndef le (xs ys : List Nat) : Prop := lt xs ys ∨ xs = ys\n\n/-- Bounded equal-length words are ordered by their highest differing digit. -/\ntheorem assemble_le_to_lt_or_eq (base : Nat) (xs ys : List Nat)\n (hb : 1 < base) (hlen : xs.length = ys.length)\n (hxs : ∀ x ∈ xs, x < base) (hys : ∀ y ∈ ys, y < base)\n (h : WordDivision.assemble base xs ≤ WordDivision.assemble base ys) :\n lt xs ys ∨ xs = ys := by\n induction xs generalizing ys with\n | nil =>\n cases ys with\n | nil => exact Or.inr rfl\n | cons y ys => simp at hlen\n | cons x xs ih =>\n cases ys with\n | nil => simp at hlen\n | cons y ys =>\n have hlen' : xs.length = ys.length := by simpa using hlen\n have hx : x < base := hxs x (by simp)\n have hy : y < base := hys y (by simp)\n have hxs' : ∀ z ∈ xs, z < base := by\n intro z hz\n exact hxs z (by simp [hz])\n have hys' : ∀ z ∈ ys, z < base := by\n intro z hz\n exact hys z (by simp [hz])\n have ht : WordDivision.assemble base xs ≤ WordDivision.assemble base ys := by\n by_cases hn : WordDivision.assemble base xs ≤ WordDivision.assemble base ys\n · exact hn\n have hm := Nat.mul_le_mul_left base\n (show WordDivision.assemble base ys + 1 ≤ WordDivision.assemble base xs by omega)\n simp only [Nat.mul_add, Nat.mul_one] at hm\n simp only [WordDivision.assemble] at h\n omega\n rcases ih ys hlen' hxs' hys' ht with hlt | heq\n · left\n rcases hlt with ⟨i, hi, hh, hl⟩\n refine ⟨i + 1, by simp; omega, ?_, ?_⟩\n · intro j hij hj\n cases j with\n | zero => omega\n | succ j =>\n simpa using hh j (by omega) (by simpa using hj)\n · simpa using hl\n · have hxy : x ≤ y := by\n simp only [WordDivision.assemble, heq] at h\n omega\n rcases Nat.lt_or_eq_of_le hxy with hlt | he\n · left\n refine ⟨0, by simp, ?_, ?_⟩\n · intro j hj _\n cases j with\n | zero => omega\n | succ j => simp [heq]\n · simpa using hlt\n · right\n simp [he, heq]\n\n/-- Strict numeric order gives a strict lexicographic witness. -/\ntheorem assemble_lt_to_lt (base : Nat) (xs ys : List Nat)\n (hb : 1 < base) (hlen : xs.length = ys.length)\n (hxs : ∀ x ∈ xs, x < base) (hys : ∀ y ∈ ys, y < base)\n (h : WordDivision.assemble base xs < WordDivision.assemble base ys) :\n lt xs ys := by\n rcases assemble_le_to_lt_or_eq base xs ys hb hlen hxs hys (Nat.le_of_lt h) with hl | he\n · exact hl\n · subst ys\n exact False.elim (Nat.lt_irrefl _ h)\n\n/-- The successive-remainder representation agrees with indexed digits. -/\ntheorem range_digits (base x n : Nat) :\n (List.range n).map (WordDivision.digit base x) = WordDivision.digits base x n := by\n induction n generalizing x with\n | zero => rfl\n | succ n ih =>\n have hs (i : Nat) :\n WordDivision.digit base x (i + 1) = WordDivision.digit base (x / base) i := by\n unfold WordDivision.digit\n rw [Nat.pow_succ, Nat.mul_comm (base ^ i) base, ← Nat.div_div_eq_div_mul]\n simp only [List.range_succ_eq_map, List.map_cons, List.map_map,\n Function.comp_def, WordDivision.digits]\n simp only [hs, ih]\n simp [WordDivision.digit]\n\n/-- Assemble all indexed digits of a number that fits in the word. -/\ntheorem assemble_range_digits (base x n : Nat) (h : x < base ^ n) :\n WordDivision.assemble base ((List.range n).map (WordDivision.digit base x)) = x := by\n rw [range_digits]\n have he := WordDivision.digits_reconstruct base x n\n simpa [Nat.div_eq_of_lt h] using he\n\n/-- Indexed, zero-defaulted input extraction agrees with a bounded list prefix. -/\ntheorem range_input_take (base n : Nat) (v : List Nat) (hn : n ≤ v.length) :\n (List.range n).map (fun i => (v[i]?.getD 0) % base) =\n (v.take n).map (fun x => x % base) := by\n induction n generalizing v with\n | zero => rfl\n | succ n ih =>\n cases v with\n | nil => simp at hn\n | cons x xs =>\n have ht : n ≤ xs.length := by simpa using hn\n simpa [List.range_succ_eq_map, List.map_map, Function.comp_def] using\n congrArg (List.cons (x % base)) (ih xs ht)\n\n/-- The normalized input digit list assembles to operand A. -/\ntheorem assemble_inputDigits (w : Nat) (v : List Nat)\n (hn : WordDivision.limbs w ≤ v.length) :\n WordDivision.assemble (WordDivision.radix w)\n ((List.range (WordDivision.limbs w)).map (WordDivision.inputDigit w v)) =\n WordDivision.wordA w v := by\n exact congrArg (WordDivision.assemble (WordDivision.radix w))\n (range_input_take (WordDivision.radix w) (WordDivision.limbs w) v hn)\n\n/-- The normalized divisor digit list assembles to operand B. -/\ntheorem assemble_bDigits (w : Nat) (v : List Nat)\n (hn : 2 * WordDivision.limbs w ≤ v.length) :\n WordDivision.assemble (WordDivision.radix w)\n ((List.range (WordDivision.limbs w)).map (WordDivision.bDigit w v)) =\n WordDivision.wordB w v := by\n have ht : WordDivision.limbs w ≤ (v.drop (WordDivision.limbs w)).length := by\n simp only [List.length_drop]\n omega\n unfold WordDivision.bDigit WordDivision.inputDigit WordDivision.wordB WordDivision.normalize\n simpa only [List.getElem?_drop] using\n congrArg (WordDivision.assemble (WordDivision.radix w))\n (range_input_take (WordDivision.radix w) (WordDivision.limbs w)\n (v.drop (WordDivision.limbs w)) ht)\n\n/-- A positive digit makes the assembled word positive. -/\ntheorem assemble_pos_of_mem (base : Nat) (xs : List Nat) (hb : 0 < base)\n (z : Nat) (hz : z ∈ xs) (hpos : 0 < z) : 0 < WordDivision.assemble base xs := by\n induction xs with\n | nil => simp at hz\n | cons x xs ih =>\n rcases List.mem_cons.mp hz with he | ht\n · subst x\n simp only [WordDivision.assemble]\n omega\n · have hp := Nat.mul_pos hb (ih ht)\n simp only [WordDivision.assemble]\n omega\n\n/-- A positive normalized divisor digit rules out the zero-divisor branch. -/\ntheorem wordB_nonzero (w : Nat) (v : List Nat)\n (hw : w ∈ [8,16,32,64]) (hlen : v.length = 2 * WordDivision.limbs w)\n (hpos : ∃ i, i < WordDivision.limbs w ∧ 0 < WordDivision.bDigit w v i) :\n WordDivision.wordB w v ≠ 0 := by\n obtain ⟨i, hi, hp⟩ := hpos\n have hm : WordDivision.bDigit w v i ∈\n (List.range (WordDivision.limbs w)).map (WordDivision.bDigit w v) :=\n List.mem_map.mpr ⟨i, List.mem_range.mpr hi, rfl⟩\n have ha := assemble_pos_of_mem (WordDivision.radix w)\n ((List.range (WordDivision.limbs w)).map (WordDivision.bDigit w v))\n (WordDivision.radix_pos w) (WordDivision.bDigit w v i) hm hp\n rw [assemble_bDigits w v (by omega)] at ha\n exact Nat.ne_of_gt ha\n\n/-- Remainder and quotient comparisons in the exact digit lists used by WordFacts. -/\ntheorem word_lex (w : Nat) (v : List Nat)\n (hw : w ∈ [8,16,32,64]) (hlen : v.length = 2 * WordDivision.limbs w)\n (hb : WordDivision.wordB w v ≠ 0) :\n lt ((List.range (WordDivision.limbs w)).map (WordDivision.rDigit w v))\n ((List.range (WordDivision.limbs w)).map (WordDivision.bDigit w v)) ∧\n (lt ((List.range (WordDivision.limbs w)).map (WordDivision.qDigit w v))\n ((List.range (WordDivision.limbs w)).map (WordDivision.inputDigit w v)) ∨\n (List.range (WordDivision.limbs w)).map (WordDivision.qDigit w v) =\n (List.range (WordDivision.limbs w)).map (WordDivision.inputDigit w v)) := by\n have hn : WordDivision.limbs w ≤ v.length := by omega\n have hbn : WordDivision.limbs w ≤ (v.drop (WordDivision.limbs w)).length := by\n simp only [List.length_drop]\n omega\n have ha : WordDivision.wordA w v < WordDivision.radix w ^ WordDivision.limbs w := by\n simpa [List.length_take, Nat.min_eq_left hn] using WordDivision.wordA_bound w v\n have hbd : WordDivision.wordB w v < WordDivision.radix w ^ WordDivision.limbs w := by\n have hbound := WordDivision.wordB_bound w v\n rw [List.length_take, Nat.min_eq_left hbn] at hbound\n exact hbound\n have hq : WordDivision.quotient w v < WordDivision.radix w ^ WordDivision.limbs w :=\n Nat.lt_of_le_of_lt (WordDivision.quotient_le w v hb) ha\n have hr : WordDivision.remainder w v < WordDivision.radix w ^ WordDivision.limbs w :=\n Nat.lt_trans (WordDivision.remainder_lt w v hb) hbd\n have hqe : WordDivision.assemble (WordDivision.radix w)\n ((List.range (WordDivision.limbs w)).map (WordDivision.qDigit w v)) =\n WordDivision.quotient w v :=\n assemble_range_digits (WordDivision.radix w) (WordDivision.quotient w v)\n (WordDivision.limbs w) hq\n have hre : WordDivision.assemble (WordDivision.radix w)\n ((List.range (WordDivision.limbs w)).map (WordDivision.rDigit w v)) =\n WordDivision.remainder w v :=\n assemble_range_digits (WordDivision.radix w) (WordDivision.remainder w v)\n (WordDivision.limbs w) hr\n constructor\n · apply assemble_lt_to_lt (WordDivision.radix w)\n · exact WordDivision.radix_gt_one w\n · simp\n · intro d hd\n obtain ⟨i, _, rfl⟩ := List.mem_map.mp hd\n exact WordDivision.rDigit_lt w v i\n · intro d hd\n obtain ⟨i, _, rfl⟩ := List.mem_map.mp hd\n exact Nat.mod_lt _ (WordDivision.radix_pos w)\n · rw [hre, assemble_bDigits w v (by omega)]\n exact WordDivision.remainder_lt w v hb\n · apply assemble_le_to_lt_or_eq (WordDivision.radix w)\n · exact WordDivision.radix_gt_one w\n · simp\n · intro d hd\n obtain ⟨i, _, rfl⟩ := List.mem_map.mp hd\n exact WordDivision.qDigit_lt w v i\n · intro d hd\n obtain ⟨i, _, rfl⟩ := List.mem_map.mp hd\n exact Nat.mod_lt _ (WordDivision.radix_pos w)\n · rw [hqe, assemble_inputDigits w v hn]\n exact WordDivision.quotient_le w v hb\n\nend WordLex\n\n#print axioms WordLex.assemble_lt_to_lt\n#print axioms WordLex.assemble_le_to_lt_or_eq\n#print axioms WordLex.word_lex\n#print axioms WordLex.wordB_nonzero\n",
56
+ "WordColumns.lean": "import WordDivision\n\n/-! Numeric wrapper for the columns emitted by `wordDivisionFacts` in\n`kernel/word-division.ts`. Input digits are zero-padded above the dividend's\n`n` limbs: `WordDivision.inputDigit` itself reads the full input list, including\nits divisor half, so it must only be used below `n` in a column equation.\nAll input limbs may be arbitrary natural numbers; WordDivision normalizes them.\n-/\nnamespace WordColumns\nopen WordDivision\n\nprivate theorem list_two (v : List Nat) (h : v.length = 2) :\n ∃ a b, v = [a, b] := by\n rcases v with _ | ⟨a, _ | ⟨b, v⟩⟩\n · simp at h\n · simp at h\n have hv : v = [] := by simpa using h\n subst v\n exact ⟨a, b, rfl⟩\n\nprivate theorem list_four (v : List Nat) (h : v.length = 4) :\n ∃ a₀ a₁ b₀ b₁, v = [a₀, a₁, b₀, b₁] := by\n rcases v with _ | ⟨a₀, _ | ⟨a₁, _ | ⟨b₀, _ | ⟨b₁, v⟩⟩⟩⟩\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n have hv : v = [] := by simpa using h\n subst v\n exact ⟨a₀, a₁, b₀, b₁, rfl⟩\n\nprivate theorem list_eight (v : List Nat) (h : v.length = 8) :\n ∃ a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃, v = [a₀, a₁, a₂, a₃, b₀, b₁, b₂, b₃] := by\n rcases v with _ | ⟨a₀, _ | ⟨a₁, _ | ⟨a₂, _ | ⟨a₃,\n _ | ⟨b₀, _ | ⟨b₁, _ | ⟨b₂, _ | ⟨b₃, v⟩⟩⟩⟩⟩⟩⟩⟩\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n · simp at h\n have hv : v = [] := by simpa using h\n subst v\n exact ⟨a₀, a₁, a₂, a₃, b₀, b₁, b₂, b₃, rfl⟩\n\nprivate theorem one_limb (w : Nat) (v : List Nat) (hn : limbs w = 1)\n (hlen : v.length = 2) (hb : wordB w v ≠ 0) :\n (∀ i < 2 * limbs w,\n carry w v i + (if i < limbs w then rDigit w v i else 0) +\n convolution w v i =\n (if i < limbs w then inputDigit w v i else 0) +\n radix w * carry w v (i + 1)) ∧\n carry w v (limbs w) = 0 := by\n obtain ⟨a, b, rfl⟩ := list_two v hlen\n have hb' : b % radix w ≠ 0 := by\n simpa [wordB, normalize, assemble, hn] using hb\n obtain ⟨h₀, h₁⟩ := single_limb_columns w a b hn hb'\n constructor\n · intro i hi\n have his : i = 0 ∨ i = 1 := by rw [hn] at hi; omega\n rcases his with rfl | rfl\n · simpa [hn] using h₀\n · simpa [hn] using h₁\n · simpa [hn] using single_limb_carry_zero w a b hn hb'\n\n/-- All generated low and zero-padded high columns, and the carry at the word\nboundary, for every supported width and arbitrary (not necessarily bounded)\nnatural input limbs. This is the numeric equation used by WordFacts.column. -/\ntheorem columns_and_carry (w : Nat) (v : List Nat)\n (hw : w ∈ [8, 16, 32, 64])\n (hlen : v.length = 2 * WordDivision.limbs w)\n (hb : WordDivision.wordB w v ≠ 0) :\n (∀ i < 2 * WordDivision.limbs w,\n WordDivision.carry w v i +\n (if i < WordDivision.limbs w then WordDivision.rDigit w v i else 0) +\n WordDivision.convolution w v i =\n (if i < WordDivision.limbs w then WordDivision.inputDigit w v i else 0) +\n WordDivision.radix w * WordDivision.carry w v (i + 1)) ∧\n WordDivision.carry w v (WordDivision.limbs w) = 0 := by\n simp only [List.mem_cons, List.not_mem_nil, or_false] at hw\n rcases hw with rfl | rfl | rfl | rfl\n · exact one_limb 8 v (by decide) (by simpa [limbs] using hlen) hb\n · exact one_limb 16 v (by decide) (by simpa [limbs] using hlen) hb\n · obtain ⟨a₀, a₁, b₀, b₁, rfl⟩ :=\n list_four v (by simpa [limbs] using hlen)\n constructor\n · intro i hi\n by_cases hlow : i < 2\n · simpa [limbs, radix, hlow] using\n thirty_two_bit_low_columns a₀ a₁ b₀ b₁ hb i hlow\n · have his : i = 2 ∨ i = 3 := by\n have hi' : i < 4 := by simpa [limbs] using hi\n omega\n have hc := thirty_two_bit_high_columns a₀ a₁ b₀ b₁ hb\n rcases his with rfl | rfl\n · simpa [limbs, radix] using hc.1\n · simpa [limbs, radix] using hc.2.1\n · simpa [limbs] using thirty_two_bit_carry_zero a₀ a₁ b₀ b₁ hb\n · obtain ⟨a₀, a₁, a₂, a₃, b₀, b₁, b₂, b₃, rfl⟩ :=\n list_eight v (by simpa [limbs] using hlen)\n constructor\n · intro i hi\n by_cases hlow : i < 4\n · simpa [limbs, radix, hlow] using\n sixty_four_bit_columns a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb i hlow\n · have hi' : i < 8 := by simpa [limbs] using hi\n simpa [limbs, radix, hlow] using\n sixty_four_bit_high_columns a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb i (by omega) hi'\n · simpa [limbs] using\n sixty_four_bit_carry_zero a₀ a₁ a₂ a₃ b₀ b₁ b₂ b₃ hb\n\n/-- A bounded negative control: the high input digit is a divisor digit, not\npadding. Using it unconditionally would falsely require `0 = 1`. -/\ntheorem unpadded_high_column_counterexample :\n wordB 8 [1, 1] ≠ 0 ∧\n carry 8 [1, 1] 1 + convolution 8 [1, 1] 1 ≠\n inputDigit 8 [1, 1] 1 + radix 8 * carry 8 [1, 1] 2 := by\n decide\n\n#print axioms WordColumns.columns_and_carry\n#print axioms WordColumns.unpadded_high_column_counterexample\nend WordColumns\n",
57
+ "WordOne.lean": "import WordLex\n\n/-! Divisor-one digits for the normalized little-endian source operand. No\nschema-generated fact is a premise of this numeric theorem. -/\nnamespace WordOne\n\n/-- Extraction is inverse to assembly for a bounded little-endian word. -/\ntheorem digit_assemble (base : Nat) (xs : List Nat) (hb : 0 < base)\n (hxs : ∀ x ∈ xs, x < base) (i : Nat) (hi : i < xs.length) :\n WordDivision.digit base (WordDivision.assemble base xs) i = xs[i]?.getD 0 := by\n induction xs generalizing i with\n | nil => simp at hi\n | cons x xs ih =>\n have hx : x < base := hxs x (by simp)\n have ht : ∀ y ∈ xs, y < base := by\n intro y hy\n exact hxs y (by simp [hy])\n cases i with\n | zero =>\n simp [WordDivision.digit, WordDivision.assemble, Nat.mod_eq_of_lt hx]\n | succ i =>\n have hs (z : Nat) : WordDivision.digit base z (i+1) =\n WordDivision.digit base (z/base) i := by\n unfold WordDivision.digit\n rw [Nat.pow_succ, Nat.mul_comm (base^i) base, ← Nat.div_div_eq_div_mul]\n rw [hs]\n have hdiv : WordDivision.assemble base (x::xs) / base =\n WordDivision.assemble base xs := by\n simp [WordDivision.assemble, Nat.add_mul_div_left, Nat.div_eq_of_lt hx, hb]\n rw [hdiv]\n simpa using ih ht i (by simpa using hi)\n\n/-- The source operand's reconstructed digit is its original normalized limb. -/\ntheorem input_digit (w : Nat) (v : List Nat)\n (hlen : WordDivision.limbs w ≤ v.length) (i : Nat) (hi : i < WordDivision.limbs w) :\n WordDivision.digit (WordDivision.radix w) (WordDivision.wordA w v) i =\n WordDivision.inputDigit w v i := by\n let ds := (List.range (WordDivision.limbs w)).map (WordDivision.inputDigit w v)\n have hds : ∀ x ∈ ds, x < WordDivision.radix w := by\n intro x hx\n obtain ⟨j, _, rfl⟩ := List.mem_map.mp hx\n exact Nat.mod_lt _ (WordDivision.radix_pos w)\n have h := digit_assemble (WordDivision.radix w) ds (WordDivision.radix_pos w) hds i (by simpa [ds] using hi)\n rw [WordLex.assemble_inputDigits w v hlen] at h\n simpa [ds, hi] using h\n\n/-- With divisor one, every quotient limb is exactly its input limb and every\nremainder limb is zero, at all four supported widths and all natural inputs. -/\ntheorem divisor_one_digits (w : Nat) (v : List Nat)\n (_hw : w ∈ [8,16,32,64]) (hlen : v.length = 2*WordDivision.limbs w)\n (hb : WordDivision.wordB w v = 1) :\n ∀ i < WordDivision.limbs w,\n WordDivision.qDigit w v i = WordDivision.inputDigit w v i ∧\n WordDivision.rDigit w v i = 0 := by\n intro i hi\n obtain ⟨hq, hr⟩ := WordDivision.divisor_one_digits w v hb i\n exact ⟨hq.trans (input_digit w v (by omega) i hi), hr⟩\n\n/-- The generator's divisor-one digit shape really denotes numeric one. -/\ntheorem b_digits_one (w : Nat) (v : List Nat)\n (hw : w ∈ [8,16,32,64]) (hlen : v.length = 2*WordDivision.limbs w)\n (h0 : WordDivision.bDigit w v 0 = 1)\n (hrest : ∀ i, 1 ≤ i → i < WordDivision.limbs w → WordDivision.bDigit w v i = 0) :\n WordDivision.wordB w v = 1 := by\n rw [← WordLex.assemble_bDigits w v (by omega)]\n have hn : WordDivision.limbs w = 1 ∨ WordDivision.limbs w = 2 ∨ WordDivision.limbs w = 4 := by\n simp only [List.mem_cons, List.not_mem_nil, or_false] at hw\n rcases hw with rfl | rfl | rfl | rfl <;> simp [WordDivision.limbs]\n rcases hn with hn | hn | hn\n · simp [hn, List.range_succ, WordDivision.assemble, h0]\n · have h1 := hrest 1 (by decide) (by omega)\n simp [hn, List.range_succ, WordDivision.assemble, h0, h1]\n · have h1 := hrest 1 (by decide) (by omega)\n have h2 := hrest 2 (by decide) (by omega)\n have h3 := hrest 3 (by decide) (by omega)\n simp [hn, List.range_succ, WordDivision.assemble, h0, h1, h2, h3]\n\nend WordOne\n#print axioms WordOne.divisor_one_digits\n#print axioms WordOne.b_digits_one\n",
58
+ "WordFacts.lean": "import WordDivisionBridge\nimport WordColumns\nimport WordLex\nimport WordOne\n\n/-! The full syntactic word-division fact schema. A map read premise is a\nproperty of the concrete word interpretation, never an assumption about a\nfact produced by this schema. -/\nnamespace WordFacts\nopen Kern\n\nstructure Spec where\n width : Nat\n args : List E\n qMaps : List Nat\n rMaps : List Nat\n cMaps : List Nat\n\n/-- Right-associated tuple key, matching kernel/word-division.ts. -/\ndef tupleKey : List E → E\n | [] => .const 0\n | [x] => x\n | x :: xs => .tup x (tupleKey xs)\n\nnamespace Spec\ndef key (s : Spec) : E := tupleKey s.args\ndef n (s : Spec) : Nat := WordDivision.limbs s.width\ndef base (s : Spec) : Nat := WordDivision.radix s.width\n\ndef a (s : Spec) (i : Nat) : E := s.args[i]?.getD (.const 0)\ndef b (s : Spec) (i : Nat) : E := s.args[s.n+i]?.getD (.const 0)\ndef q (s : Spec) (i : Nat) : E := .get (s.qMaps[i]?.getD 0) s.key\ndef r (s : Spec) (i : Nat) : E := .get (s.rMaps[i]?.getD 0) s.key\ndef c (s : Spec) (i : Nat) : E :=\n if i = 0 then .const 0 else .get (s.cMaps[i-1]?.getD 0) s.key\nend Spec\n\n/-- Width, tuple arity, and all slots exactly match the TypeScript name parser. -/\ndef Spec.Valid (s : Spec) : Prop :=\n s.width ∈ [8, 16, 32, 64] ∧ s.args.length = 2*s.n ∧\n s.qMaps.length = s.n ∧ s.rMaps.length = s.n ∧ s.cMaps.length = 2*s.n\n\n/-- The exporter retains a connective even for a singleton, using a neutral\nbinary child, exactly leanConnective in lean-kernel/lean-terms.ts. -/\ndef both : List C → C\n | [] => .le (.const 0) (.const 0)\n | [x] => .and x (.le (.const 0) (.const 0))\n | [x, y] => .and x y\n | x :: xs => .and x (both xs)\ndef either : List C → C\n | [] => .lt (.const 0) (.const 0)\n | [x] => .or x (.lt (.const 0) (.const 0))\n | [x, y] => .or x y\n | x :: xs => .or x (either xs)\n\ndef sumE (xs : List E) : E := xs.foldl E.add (.const 0)\ndef domain (s : Spec) : C :=\n both ((List.range (2*s.n)).map (fun i => C.lt (s.args[i]?.getD (.const 0)) (.const s.base)) ++\n [either ((List.range s.n).map (fun i => C.lt (.const 0) (s.b i)))])\ndef guarded (s : Spec) (fact : C) : C :=\n either [.not (domain s), fact]\n\ndef lex (xs ys : List E) (strict : Bool) : C :=\n let m := xs.length\n either (((List.range m).reverse.map fun i =>\n both (((List.range (m-i-1)).map (fun j => .eq (xs[i+1+j]?.getD (.const 0))\n (ys[i+1+j]?.getD (.const 0)))) ++\n [.lt (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0))])) ++\n if strict then [] else [both ((List.range m).map fun i =>\n .eq (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0)))])\n\ndef column (s : Spec) (i : Nat) : C :=\n let products := ((List.range s.n).filter fun j => j ≤ i && i-j < s.n).map\n (fun j => E.prod (s.q j) (s.b (i-j)))\n .eq (sumE ([s.c i] ++ (if i < s.n then [s.r i] else []) ++ products))\n (sumE [if i < s.n then s.a i else .const 0, .mul s.base (s.c (i+1))])\n\n/-- Unconditional range clauses of the generator, including zero divisor. -/\ndef rangeFacts (s : Spec) : List C :=\n ((List.range s.n).map (fun i => C.lt (s.q i) (.const s.base))) ++\n ((List.range s.n).map (fun i => C.lt (s.r i) (.const s.base))) ++\n ((List.range (2*s.n)).map (fun j => C.le (s.c (j+1)) (.const (s.n*s.base))))\n\ndef bOne (s : Spec) : C :=\n both ([.eq (s.b 0) (.const 1)] ++\n ((List.range (s.n-1)).map fun j => .eq (s.b (j+1)) (.const 0)))\ndef oneResult (s : Spec) : C :=\n both (((List.range s.n).map fun i => .eq (s.q i) (s.a i)) ++\n ((List.range s.n).map fun i => .eq (s.r i) (.const 0)))\ndef oneClause (s : Spec) : C := either [.not (bOne s), oneResult s]\n\n/-- Exactly the list emitted by wordDivisionFacts for one encountered key. -/\ndef facts (s : Spec) : List C :=\n rangeFacts s ++\n ((List.range (2*s.n)).map (fun i => guarded s (column s i))) ++\n [guarded s (.eq (s.c s.n) (.const 0)),\n guarded s (lex ((List.range s.n).map s.r) ((List.range s.n).map s.b) true),\n guarded s (lex ((List.range s.n).map s.q) ((List.range s.n).map s.a) false),\n guarded s (oneClause s)]\n\n/-- This condition concerns only concrete getters, not any generated clause. -/\ndef ReadSemantics (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat) : Prop :=\n let v := s.args.map (evalE X M)\n (∀ i < s.n, evalE X M (s.q i) = WordDivision.value s.width 0 i v) ∧\n (∀ i < s.n, evalE X M (s.r i) = WordDivision.value s.width 1 i v) ∧\n (∀ i, 1 ≤ i → i ≤ 2*s.n →\n evalE X M (s.c i) = WordDivision.value s.width 2 i v)\n\n/-- A concrete map interpretation decodes all operands from its queried key,\nnot from an ambient environment. The actual map-ID dispatch is supplied by\nWordReach and the emitter; this is the generic semantic equation they need. -/\ndef keyValues : Nat → Nat → List Nat\n | 0, _ => []\n | 1, k => [k]\n | i+2, k => Kern.pickP 0 k :: keyValues (i+1) (Kern.pickP 1 k)\n\ntheorem keyValues_tuple (xs : List E) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (h : xs ≠ []) :\n keyValues xs.length (evalE X M (tupleKey xs)) = xs.map (evalE X M) := by\n induction xs with\n | nil => contradiction\n | cons x xs ih =>\n cases xs with\n | nil => simp [keyValues, tupleKey]\n | cons y ys =>\n have ht : (y :: ys) ≠ [] := by simp\n have hh := ih ht\n simp only [tupleKey, evalE, keyValues, List.length_cons, List.map_cons]\n rw [Kern.pickP, Kern.pickP]\n simp only [if_pos rfl, if_neg (by decide : (1 : Nat) ≠ 0)]\n simp only [Kern.cun_cpair]\n exact congrArg (List.cons (evalE X M x)) hh\n\n\n\n\nprivate theorem both_sound (xs : List C) (X : Nat → Nat) (M : Nat → Nat → Nat) :\n evalC X M (both xs) = true ↔ ∀ c ∈ xs, evalC X M c = true := by\n induction xs with\n | nil => simp [both, evalC]\n | cons x xs ih =>\n cases xs with\n | nil => simp [both, evalC]\n | cons y ys =>\n cases ys with\n | nil => simp [both, evalC]\n | cons z zs =>\n simpa only [both, evalC, Bool.and_eq_true, List.mem_cons,\n forall_eq_or_imp] using\n (show (evalC X M x = true ∧ evalC X M (both (y :: z :: zs)) = true) ↔\n ∀ c ∈ x :: y :: z :: zs, evalC X M c = true from by\n simpa only [List.mem_cons, forall_eq_or_imp, ih] )\n\nprivate theorem either_sound (xs : List C) (X : Nat → Nat) (M : Nat → Nat → Nat) :\n evalC X M (either xs) = true ↔ ∃ c ∈ xs, evalC X M c = true := by\n induction xs with\n | nil => simp [either, evalC]\n | cons x xs ih =>\n cases xs with\n | nil => simp [either, evalC]\n | cons y ys =>\n cases ys with\n | nil => simp [either, evalC]\n | cons z zs =>\n simp only [either, evalC, Bool.or_eq_true, List.mem_cons]\n simpa [ih, or_assoc]\n\n/-- Guard discharge requires proof only on the bounded nonzero-divisor branch. -/\ntheorem guarded_sound (s : Spec) (fact : C) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (h : evalC X M (domain s) = true → evalC X M fact = true) :\n evalC X M (guarded s fact) = true := by\n by_cases hd : evalC X M (domain s) = true\n · simp [guarded, either, evalC, hd, h hd]\n · have hf : evalC X M (domain s) = false := by cases hh : evalC X M (domain s) <;> simp_all\n simp [guarded, either, evalC, hf]\n\n\n/-- Exactly the generated guard implies every source limb is bounded and at\nleast one divisor limb is positive. -/\ntheorem domain_sound (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hd : evalC X M (domain s) = true) :\n (∀ i < 2*s.n, evalE X M (s.args[i]?.getD (.const 0)) < s.base) ∧\n (∃ i, i < s.n ∧ 0 < evalE X M (s.b i)) := by\n have hall := (both_sound _ X M).mp hd\n constructor\n · intro i hi\n have hh := hall _ (List.mem_append.mpr (Or.inl\n (List.mem_map.mpr ⟨i, List.mem_range.mpr hi, rfl⟩)))\n change decide (evalE X M (s.args[i]?.getD (.const 0)) < s.base) = true at hh\n exact of_decide_eq_true hh\n · have hh := hall (either ((List.range s.n).map (fun i => C.lt (.const 0) (s.b i))))\n (List.mem_append.mpr (Or.inr (by simp)))\n obtain ⟨f, hf, hv⟩ := (either_sound _ X M).mp hh\n obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n refine ⟨i, List.mem_range.mp hi, ?_⟩\n change decide (0 < evalE X M (s.b i)) = true at hv\n exact of_decide_eq_true hv\n\n\n/-- Evaluation commutes with optional list indexing, including malformed or\nout-of-range descriptors; validity later ensures the actual slots exist. -/\nprivate theorem eval_arg (xs : List E) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (i : Nat) : evalE X M (xs[i]?.getD (.const 0)) =\n (xs.map (evalE X M))[i]?.getD 0 := by\n induction xs generalizing i with\n | nil => simp [evalE]\n | cons x xs ih =>\n cases i with\n | zero => simp\n | succ i => simpa using ih i\n\n/-- The generated guard means raw operand digits equal their normalized\nconcrete-word digits. No equality is assumed for unbounded operands. -/\ntheorem operand_digit (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hd : evalC X M (domain s) = true) (i : Nat) (hi : i < 2*s.n) :\n evalE X M (s.args[i]?.getD (.const 0)) =\n WordDivision.inputDigit s.width (s.args.map (evalE X M)) i := by\n have hb := (domain_sound s X M hd).1 i hi\n simp only [WordDivision.inputDigit, ← eval_arg]\n exact (Nat.mod_eq_of_lt hb).symm\n\n\nprivate theorem sumE_eval (xs : List E) (X : Nat → Nat) (M : Nat → Nat → Nat) :\n evalE X M (sumE xs) = (xs.map (evalE X M)).sum := by\n have hf (ys : List E) (acc : E) :\n evalE X M (ys.foldl E.add acc) =\n evalE X M acc + (ys.map (evalE X M)).sum := by\n induction ys generalizing acc with\n | nil => simp\n | cons y ys ih =>\n simp only [List.foldl_cons, List.map_cons, List.sum_cons, ih, evalE]\n omega\n simpa [sumE, evalE] using hf xs (.const 0)\n\nprivate theorem filter_sum (xs : List Nat) (p : Nat → Bool) (f : Nat → Nat) :\n ((xs.filter p).map f).sum =\n (xs.map fun j => if p j then f j else 0).sum := by\n induction xs with\n | nil => rfl\n | cons x xs ih =>\n cases hp : p x <;> simp [hp, ih]\n\n\n/-- A filtered TypeScript convolution sum is the concrete word convolution,\nwithout specializing or enumerating expression trees. -/\nprivate theorem products_eval (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true)\n (i : Nat) :\n ((((List.range s.n).filter fun j => j ≤ i && i-j < s.n).map\n (fun j => evalE X M (.prod (s.q j) (s.b (i-j))))).sum) =\n WordDivision.convolution s.width (s.args.map (evalE X M)) i := by\n let p : Nat → Bool := fun j => j ≤ i && i-j < s.n\n let v := s.args.map (evalE X M)\n rw [filter_sum (List.range s.n) p\n (fun j => evalE X M (.prod (s.q j) (s.b (i-j))))]\n unfold WordDivision.convolution\n apply congrArg List.sum\n apply List.map_congr_left\n intro j hj\n have hjlt : j < s.n := List.mem_range.mp hj\n by_cases hp : j ≤ i ∧ i-j < s.n\n · have hpb : p j = true := by simp [p, hp.1, hp.2]\n simp only [hpb, ↓reduceIte, evalE]\n rw [hr.1 j hjlt, show evalE X M (s.b (i-j)) =\n WordDivision.bDigit s.width v (i-j) from by\n simpa only [Spec.b, Spec.n, WordDivision.bDigit, v] using\n operand_digit s X M hd (s.n+(i-j)) (by omega)]\n change j ≤ i ∧ i-j < WordDivision.limbs s.width at hp\n simp only [if_pos hp, WordDivision.value_q, v]\n · have hpb : p j = false := by\n simp only [p, Bool.and_eq_true, decide_eq_true_eq]\n simp at hp ⊢\n omega\n change ¬(j ≤ i ∧ i-j < WordDivision.limbs s.width) at hp\n simp [hpb, hp]\n\n/-- Each guarded syntactic column has exactly the arithmetic shape proved by\nWordColumns, for arbitrary source terms and environment. -/\ntheorem column_sound (s : Spec) (hv : s.Valid) (X : Nat → Nat)\n (M : Nat → Nat → Nat) (hr : ReadSemantics s X M)\n (hd : evalC X M (domain s) = true)\n (hb : WordDivision.wordB s.width (s.args.map (evalE X M)) ≠ 0)\n (i : Nat) (hi : i < 2*s.n) : evalC X M (column s i) = true := by\n let v := s.args.map (evalE X M)\n have hc (j : Nat) (hj : j ≤ 2*s.n) :\n evalE X M (s.c j) = WordDivision.carry s.width v j := by\n by_cases hz : j = 0\n · subst j\n change evalE X M (.const 0) = 0\n rfl\n · simpa only [WordDivision.value_c] using hr.2.2 j (by omega) hj\n have hq (j : Nat) (hj : j < s.n) :\n evalE X M (s.r j) = WordDivision.rDigit s.width v j := by\n simpa only [WordDivision.value_r] using hr.2.1 j hj\n have hleft : evalE X M (sumE ([s.c i] ++\n (if i < s.n then [s.r i] else []) ++\n ((List.range s.n).filter fun j => j ≤ i && i-j < s.n).map\n (fun j => E.prod (s.q j) (s.b (i-j))))) =\n WordDivision.carry s.width v i +\n (if i < s.n then WordDivision.rDigit s.width v i else 0) +\n WordDivision.convolution s.width v i := by\n rw [sumE_eval]\n simp only [List.map_append, List.sum_append]\n simp only [List.map_map, Function.comp_def]\n rw [products_eval s X M hr hd i]\n by_cases h : i < s.n\n · simp [h, hc i (by omega), hq i h, v]\n · simp [h, hc i (by omega), v]\n have hright : evalE X M (sumE\n [if i < s.n then s.a i else .const 0, .mul s.base (s.c (i+1))]) =\n (if i < s.n then WordDivision.inputDigit s.width v i else 0) +\n WordDivision.radix s.width * WordDivision.carry s.width v (i+1) := by\n rw [sumE_eval]\n simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil, Nat.add_zero]\n by_cases h : i < s.n\n · simp only [if_pos h, evalE, Spec.base]\n rw [hc (i+1) (by omega)]\n rw [show evalE X M (s.a i) = WordDivision.inputDigit s.width v i from by\n simpa only [Spec.a, v] using operand_digit s X M hd i (by omega)]\n · simp only [if_neg h, evalE, Spec.base]\n rw [hc (i+1) (by omega)]\n change decide (_ = _) = true\n apply decide_eq_true\n rw [hleft, hright]\n exact (WordColumns.columns_and_carry s.width v hv.1\n (by simpa only [Spec.n, v, List.length_map] using hv.2.1) hb).1 i\n (by simpa only [Spec.n] using hi)\n\n\n/-- A highest-differing-digit witness satisfies the exact nested AST\nlexicographic disjunction; works for every digit count, not examples. -/\ntheorem lex_lt_sound (xs ys : List E) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (h : WordLex.lt (xs.map (evalE X M)) (ys.map (evalE X M))) :\n evalC X M (lex xs ys true) = true := by\n obtain ⟨i, hi, heq, hlt⟩ := h\n simp only [List.length_map] at hi\n apply (either_sound _ X M).2\n let atom := both (((List.range (xs.length-i-1)).map fun j =>\n C.eq (xs[i+1+j]?.getD (.const 0)) (ys[i+1+j]?.getD (.const 0))) ++\n [C.lt (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0))])\n refine ⟨atom, ?_, ?_⟩\n · simp only [lex, ↓reduceIte, List.append_nil]\n apply List.mem_map.mpr\n exact ⟨i, by simp [hi], rfl⟩\n · apply (both_sound _ X M).2\n intro f hf\n rcases List.mem_append.mp hf with hf | hf\n · obtain ⟨j, hj, rfl⟩ := List.mem_map.mp hf\n have hjlt : i+1+j < xs.length := by\n have hh := List.mem_range.mp hj\n omega\n change decide (evalE X M (xs[i+1+j]?.getD (.const 0)) =\n evalE X M (ys[i+1+j]?.getD (.const 0))) = true\n apply decide_eq_true\n rw [eval_arg, eval_arg]\n exact heq (i+1+j) (by omega) (by simpa only [List.length_map] using hjlt)\n · have hf' : f = C.lt (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0)) := by\n simpa using hf\n subst f\n change decide (evalE X M (xs[i]?.getD (.const 0)) <\n evalE X M (ys[i]?.getD (.const 0))) = true\n apply decide_eq_true\n rw [eval_arg, eval_arg]\n exact hlt\n\n/-- Non-strict quotient comparison permits the equality alternative, exactly\nas TS digitLex(..., false), with no finite-width case split. -/\ntheorem lex_le_sound (xs ys : List E) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (h : WordLex.lt (xs.map (evalE X M)) (ys.map (evalE X M)) ∨\n xs.map (evalE X M) = ys.map (evalE X M)) :\n evalC X M (lex xs ys false) = true := by\n rcases h with h | h\n · obtain ⟨i, hi, heq, hlt⟩ := h\n simp only [List.length_map] at hi\n apply (either_sound _ X M).2\n let atom := both (((List.range (xs.length-i-1)).map fun j =>\n C.eq (xs[i+1+j]?.getD (.const 0)) (ys[i+1+j]?.getD (.const 0))) ++\n [C.lt (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0))])\n refine ⟨atom, ?_, ?_⟩\n · apply List.mem_append.mpr\n exact Or.inl (List.mem_map.mpr ⟨i, by simp [hi], rfl⟩)\n · apply (both_sound _ X M).2\n intro f hf\n rcases List.mem_append.mp hf with hf | hf\n · obtain ⟨j, hj, rfl⟩ := List.mem_map.mp hf\n have hjlt : i+1+j < xs.length := by\n have hh := List.mem_range.mp hj\n omega\n change decide (evalE X M (xs[i+1+j]?.getD (.const 0)) =\n evalE X M (ys[i+1+j]?.getD (.const 0))) = true\n apply decide_eq_true\n rw [eval_arg, eval_arg]\n exact heq (i+1+j) (by omega) (by simpa only [List.length_map] using hjlt)\n · have hf' : f = C.lt (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0)) := by\n simpa using hf\n subst f\n change decide (evalE X M (xs[i]?.getD (.const 0)) <\n evalE X M (ys[i]?.getD (.const 0))) = true\n apply decide_eq_true\n rw [eval_arg, eval_arg]\n exact hlt\n · apply (either_sound _ X M).2\n refine ⟨both ((List.range xs.length).map fun i =>\n C.eq (xs[i]?.getD (.const 0)) (ys[i]?.getD (.const 0))), ?_, ?_⟩\n · apply List.mem_append.mpr\n exact Or.inr (by simp)\n · apply (both_sound _ X M).2\n intro f hf\n obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n change decide (evalE X M (xs[i]?.getD (.const 0)) =\n evalE X M (ys[i]?.getD (.const 0))) = true\n apply decide_eq_true\n rw [eval_arg, eval_arg, h]\n\n\n/-- The TS guard's positive divisor limb witnesses the concrete nonzero word;\nall operands may be arbitrary syntax and the underlying Nat inputs unbounded. -/\ntheorem nonzero_of_domain (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hd : evalC X M (domain s) = true) :\n WordDivision.wordB s.width (s.args.map (evalE X M)) ≠ 0 := by\n obtain ⟨i, hi, hp⟩ := (domain_sound s X M hd).2\n apply WordLex.wordB_nonzero s.width (s.args.map (evalE X M)) hv.1\n (by simpa only [Spec.n, List.length_map] using hv.2.1)\n refine ⟨i, by simpa only [Spec.n] using hi, ?_⟩\n have he : evalE X M (s.b i) = WordDivision.bDigit s.width\n (s.args.map (evalE X M)) i := by\n simpa only [Spec.b, Spec.n, WordDivision.bDigit] using\n operand_digit s X M hd (s.n+i) (by omega)\n rw [← he]\n exact hp\n\n/-- Every numeric digit list in WordLex is definitionally the evaluation of\nits corresponding generated q/r/a/b expression list under the guard. -/\ntheorem digit_lists (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true) :\n (((List.range s.n).map s.q).map (evalE X M) =\n (List.range s.n).map (WordDivision.qDigit s.width (s.args.map (evalE X M)))) ∧\n (((List.range s.n).map s.r).map (evalE X M) =\n (List.range s.n).map (WordDivision.rDigit s.width (s.args.map (evalE X M)))) ∧\n (((List.range s.n).map s.a).map (evalE X M) =\n (List.range s.n).map (WordDivision.inputDigit s.width (s.args.map (evalE X M)))) ∧\n (((List.range s.n).map s.b).map (evalE X M) =\n (List.range s.n).map (WordDivision.bDigit s.width (s.args.map (evalE X M)))) := by\n have hq : ∀ i < s.n, evalE X M (s.q i) =\n WordDivision.qDigit s.width (s.args.map (evalE X M)) i := by\n intro i hi\n simpa only [WordDivision.value_q] using hr.1 i hi\n have hrem : ∀ i < s.n, evalE X M (s.r i) =\n WordDivision.rDigit s.width (s.args.map (evalE X M)) i := by\n intro i hi\n simpa only [WordDivision.value_r] using hr.2.1 i hi\n have ha : ∀ i < s.n, evalE X M (s.a i) =\n WordDivision.inputDigit s.width (s.args.map (evalE X M)) i := by\n intro i hi\n simpa only [Spec.a] using operand_digit s X M hd i (by omega)\n have hb : ∀ i < s.n, evalE X M (s.b i) =\n WordDivision.bDigit s.width (s.args.map (evalE X M)) i := by\n intro i hi\n simpa only [Spec.b, Spec.n, WordDivision.bDigit] using\n operand_digit s X M hd (s.n+i) (by omega)\n repeat' constructor\n all_goals\n simp only [List.map_map, Function.comp_def]\n apply List.map_congr_left\n intro i hi\n first\n | exact hq i (List.mem_range.mp hi)\n | exact hrem i (List.mem_range.mp hi)\n | exact ha i (List.mem_range.mp hi)\n | exact hb i (List.mem_range.mp hi)\n\n\n/-- The guarded carry at the word boundary is zero for all supported widths. -/\ntheorem carry_zero_sound (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true) :\n evalC X M (.eq (s.c s.n) (.const 0)) = true := by\n have hn : 0 < s.n := by\n have hw : s.width = 8 ∨ s.width = 16 ∨ s.width = 32 ∨ s.width = 64 := by\n simpa using hv.1\n rcases hw with h | h | h | h <;> simp [h, Spec.n, WordDivision.limbs]\n have hc := hr.2.2 s.n (by omega) (by omega)\n have hb := nonzero_of_domain s hv X M hd\n have hz := (WordColumns.columns_and_carry s.width\n (s.args.map (evalE X M)) hv.1\n (by simpa only [Spec.n, List.length_map] using hv.2.1) hb).2\n change decide (evalE X M (s.c s.n) = 0) = true\n apply decide_eq_true\n rw [hc, WordDivision.value_c]\n simpa only [Spec.n] using hz\n\n/-- The full strict/non-strict lex clauses are not merely arithmetic claims:\nthese are their evaluated generated disjunctions for arbitrary AST operands. -/\ntheorem lex_facts_sound (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true) :\n evalC X M (lex ((List.range s.n).map s.r)\n ((List.range s.n).map s.b) true) = true ∧\n evalC X M (lex ((List.range s.n).map s.q)\n ((List.range s.n).map s.a) false) = true := by\n have hn := WordLex.word_lex s.width (s.args.map (evalE X M)) hv.1\n (by simpa only [Spec.n, List.length_map] using hv.2.1)\n (nonzero_of_domain s hv X M hd)\n obtain ⟨hq, hr', ha, hb⟩ := digit_lists s X M hr hd\n constructor\n · apply lex_lt_sound\n rw [hr', hb]\n exact hn.1\n · apply lex_le_sound\n rw [hq, ha]\n exact hn.2\n\n\n/-- Divisor-one shape of the generated b digit terms entails every generated\nq=a and r=0 equality; no generic fact is taken as a premise. -/\ntheorem one_result_sound (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true)\n (hon : evalC X M (bOne s) = true) :\n evalC X M (oneResult s) = true := by\n let v := s.args.map (evalE X M)\n have hall := (both_sound _ X M).mp hon\n have he0 : evalE X M (s.b 0) = 1 := by\n have he := hall (C.eq (s.b 0) (.const 1))\n (List.mem_append.mpr (Or.inl (by simp)))\n change decide (evalE X M (s.b 0) = 1) = true at he\n exact of_decide_eq_true he\n have heRest : ∀ i, 1 ≤ i → i < s.n → evalE X M (s.b i) = 0 := by\n intro i hlo hhi\n have hj : i-1 < s.n-1 := by omega\n have heq : i-1+1=i := by omega\n have hm : C.eq (s.b i) (.const 0) ∈\n ((List.range (s.n-1)).map fun j => C.eq (s.b (j+1)) (.const 0)) := by\n apply List.mem_map.mpr\n exact ⟨i-1, List.mem_range.mpr hj, by rw [heq]⟩\n have he := hall (C.eq (s.b i) (.const 0))\n (List.mem_append.mpr (Or.inr hm))\n change decide (evalE X M (s.b i) = 0) = true at he\n exact of_decide_eq_true he\n have hb : WordDivision.wordB s.width v = 1 := by\n apply WordOne.b_digits_one s.width v hv.1\n (by simpa only [Spec.n, v, List.length_map] using hv.2.1)\n · have he : evalE X M (s.b 0) = WordDivision.bDigit s.width v 0 := by\n simpa only [Spec.b, Spec.n, WordDivision.bDigit, v] using\n operand_digit s X M hd (s.n+0) (by\n have hn : 0 < s.n := by\n have hw : s.width=8 ∨ s.width=16 ∨ s.width=32 ∨ s.width=64 := by\n simpa using hv.1\n rcases hw with h | h | h | h <;> simp [h, Spec.n, WordDivision.limbs]\n omega)\n rw [← he]\n exact he0\n · intro i hlo hhi\n have hidx : i < s.n := by simpa only [Spec.n] using hhi\n have he : evalE X M (s.b i) = WordDivision.bDigit s.width v i := by\n simpa only [Spec.b, Spec.n, WordDivision.bDigit, v] using\n operand_digit s X M hd (s.n+i) (by omega)\n rw [← he]\n exact heRest i hlo (by simpa only [Spec.n] using hhi)\n have hdigits := WordOne.divisor_one_digits s.width v hv.1\n (by simpa only [Spec.n, v, List.length_map] using hv.2.1) hb\n apply (both_sound _ X M).2\n intro f hf\n simp only [oneResult, List.mem_append] at hf\n rcases hf with hf | hf\n · obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n have hidx : i < s.n := List.mem_range.mp hi\n change decide (evalE X M (s.q i) = evalE X M (s.a i)) = true\n apply decide_eq_true\n rw [hr.1 i hidx, WordDivision.value_q]\n rw [show evalE X M (s.a i) = WordDivision.inputDigit s.width v i from by\n simpa only [Spec.a, v] using operand_digit s X M hd i (by omega)]\n exact (hdigits i (by simpa only [Spec.n] using hidx)).1\n · obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n have hidx : i < s.n := List.mem_range.mp hi\n change decide (evalE X M (s.r i) = 0) = true\n apply decide_eq_true\n rw [hr.2.1 i hidx, WordDivision.value_r]\n exact (hdigits i (by simpa only [Spec.n] using hidx)).2\n\n/-- The generated b=1 implication has its own syntactic disjunction, whose\nantecedent is discharged by the previous theorem under the domain guard. -/\ntheorem one_clause_sound (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (hd : evalC X M (domain s) = true) :\n evalC X M (oneClause s) = true := by\n by_cases h : evalC X M (bOne s) = true\n · simp [oneClause, either, evalC, h, one_result_sound s hv X M hr hd h]\n · have hf : evalC X M (bOne s) = false := by\n cases hh : evalC X M (bOne s) <;> simp_all\n simp [oneClause, either, evalC, hf]\n\n/-- All unconditional q/r/c inequalities are valid for arbitrary input terms\nand all four widths, without a nonzero-divisor hypothesis. -/\ntheorem rangeFacts_sound (s : Spec) (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) :\n ∀ f ∈ rangeFacts s, evalC X M f = true := by\n intro f hf\n simp only [rangeFacts, List.mem_append] at hf\n rcases hf with (hf | hf) | hf\n · obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n have hidx : i < s.n := List.mem_range.mp hi\n simp only [evalC, evalE, Spec.base, hr.1 i hidx]\n exact decide_eq_true (WordDivision.value_q_lt s.width i (s.args.map (evalE X M)))\n · obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hf\n have hidx : i < s.n := List.mem_range.mp hi\n simp only [evalC, evalE, Spec.base, hr.2.1 i hidx]\n exact decide_eq_true (WordDivision.value_r_lt s.width i (s.args.map (evalE X M)))\n · obtain ⟨j, hj, rfl⟩ := List.mem_map.mp hf\n have hidx : j + 1 ≤ 2*s.n := by\n have hh := List.mem_range.mp hj\n omega\n simp only [evalC, evalE, Spec.base, Spec.n,\n hr.2.2 (j+1) (by omega) hidx, WordDivision.value_c]\n exact decide_eq_true (WordDivision.carry_bound s.width (s.args.map (evalE X M)) (j+1))\n\ndef Spec.reserved (s : Spec) : List Nat := s.qMaps ++ s.rMaps ++ s.cMaps\n\nprivate theorem getD_mem (xs : List Nat) (i : Nat) (h : i < xs.length) :\n xs[i]?.getD 0 ∈ xs := by\n induction xs generalizing i with\n | nil => simp at h\n | cons x xs ih =>\n cases i with\n | zero => simp\n | succ i =>\n simp only [List.length_cons] at h\n simp only [List.getElem?_cons_succ, List.mem_cons]\n exact Or.inr (ih i (by omega))\n\n/-- A concrete, environment-independent value function can be supplied by the\nemitter (a finite dispatch on the native map indices and decoded key). Its\nassociation with this descriptor is checked pointwise, never postulated as a\nfact's truth. -/\ndef ValueMatches (s : Spec) (value : Nat → Nat → Nat) : Prop :=\n (∀ i < s.n, ∀ k, value (s.qMaps[i]?.getD 0) k =\n WordDivision.value s.width 0 i (keyValues (2*s.n) k)) ∧\n (∀ i < s.n, ∀ k, value (s.rMaps[i]?.getD 0) k =\n WordDivision.value s.width 1 i (keyValues (2*s.n) k)) ∧\n (∀ i, 1 ≤ i → i ≤ 2*s.n → ∀ k,\n value (s.cMaps[i-1]?.getD 0) k =\n WordDivision.value s.width 2 i (keyValues (2*s.n) k))\n\n/-- The only bridge from a reserved-map world to the getter premise. It uses\nright-associated tuple decoding and never assumes any generated clause. -/\ntheorem interpret_sound (s : Spec) (hv : s.Valid)\n (value M : Nat → Nat → Nat) (X : Nat → Nat)\n (hvalue : ValueMatches s value)\n (hi : ∀ mp ∈ s.reserved, ∀ k, M mp k = value mp k) :\n ReadSemantics s X M := by\n have hk : keyValues (2*s.n) (evalE X M s.key) = s.args.map (evalE X M) := by\n rw [← hv.2.1]\n exact keyValues_tuple s.args X M (by\n intro hz\n have hlen : s.args.length = 0 := by simp [hz]\n have hn : 0 < s.n := by\n have hw : s.width = 8 ∨ s.width = 16 ∨ s.width = 32 ∨ s.width = 64 := by\n simpa using hv.1\n rcases hw with h | h | h | h <;> simp [h, Spec.n, WordDivision.limbs]\n have hlength := hv.2.1\n omega)\n constructor\n · intro i h\n have hqLen := hv.2.2.1\n change M (s.qMaps[i]?.getD 0) (evalE X M s.key) = _\n rw [hi _ (List.mem_append.mpr (Or.inl\n (List.mem_append.mpr (Or.inl (getD_mem s.qMaps i (by omega)))))) _]\n rw [(hvalue.1 i h) _, hk]\n constructor\n · intro i h\n have hrLen := hv.2.2.2.1\n change M (s.rMaps[i]?.getD 0) (evalE X M s.key) = _\n rw [hi _ (List.mem_append.mpr (Or.inl\n (List.mem_append.mpr (Or.inr (getD_mem s.rMaps i (by omega)))))) _]\n rw [(hvalue.2.1 i h) _, hk]\n · intro i hlo hhi\n have hcLen := hv.2.2.2.2\n have hpos : i ≠ 0 := by omega\n simp only [Spec.c, if_neg hpos, evalE]\n rw [hi _ (List.mem_append.mpr (Or.inr\n (getD_mem s.cMaps (i-1) (by omega)))) _]\n rw [(hvalue.2.2 i hlo hhi) _, hk]\n\n\n/-- Every clause of the exact TypeScript wordDivisionFacts schema evaluates to\ntrue at every environment under concrete word getter semantics. The only\npremise about maps is equality of getter reads with WordDivision.value; there\nare no assumptions about any generated clauses, source law, or examples. -/\ntheorem facts_sound (s : Spec) (hv : s.Valid)\n (X : Nat → Nat) (M : Nat → Nat → Nat)\n (hr : ReadSemantics s X M) (f : C) (hf : f ∈ facts s) :\n evalC X M f = true := by\n simp only [facts] at hf\n rcases List.mem_append.mp hf with hPrefix | hTail\n rcases List.mem_append.mp hPrefix with hRange | hColumn\n · exact rangeFacts_sound s X M hr f hRange\n · obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hColumn\n apply guarded_sound\n intro hd\n exact column_sound s hv X M hr hd\n (nonzero_of_domain s hv X M hd) i (List.mem_range.mp hi)\n simp only [List.mem_cons, List.not_mem_nil, or_false] at hTail\n rcases hTail with rfl | rfl | rfl | rfl\n · apply guarded_sound\n intro hd\n exact carry_zero_sound s hv X M hr hd\n · apply guarded_sound\n intro hd\n exact (lex_facts_sound s hv X M hr hd).1\n · apply guarded_sound\n intro hd\n exact (lex_facts_sound s hv X M hr hd).2\n · apply guarded_sound\n intro hd\n exact one_clause_sound s hv X M hr hd\n\n#print axioms WordFacts.facts_sound\n\nend WordFacts\n",
59
+ "WordSMachine.lean": "import WordReach\n\n/-! Generic trusted certificate extension: the original machine guards and goal\nhypothesis membership remain mandatory. Extras are semantics, not assumptions. -/\nnamespace Kern.WordSMachine\nopen Kern\n\ndef checkInWithFacts (base extra : List C) (k : Cert) : Bool :=\n checkIn (base ++ extra) k\n\ndef checkSMachineWithFacts (s : SMachine) (ps : List SEntry) (extra : List C) : Bool :=\n sOk s && avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) s.f.m.pre && !readsC s.f.m.pre\n && sTermsOk s (flagVars s.f.flags) && factsOk s\n && ps.all fun e =>\n avoids (s.extVars ++ flagVars s.f.flags) e.law.holds && avoidsMaps s.carried e.law.holds\n && !(s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains e.sk\n && ps.all (fun e' => !occC e.sk e'.law.holds) && !occC e.sk s.f.m.pre && s.f.m.init.all (fun p => !occE e.sk p.2)\n && skFresh s e.sk && key2Ok s ps e\n && checkIn (sStartGoal s e ++ extra) e.start\n && checkIn (sStepGoal s ps e ++ extra) e.step\n\ntheorem sound_in_with (base extra : List C) (k : Cert)\n (h : checkInWithFacts base extra k = true) (X : Nat → Nat) (N : Nat → Nat → Nat)\n (hextra : ∀ c ∈ extra, evalC X N c = true) :\n ¬ ∀ c ∈ base, evalC X N c = true := by\n intro hbase\n exact sound_in (base ++ extra) k h X N (fun c hc => by\n rcases List.mem_append.mp hc with hc | hc\n · exact hbase c hc\n · exact hextra c hc)\n\n\nvariable (extra : List C)\nvariable {P : (Nat → Nat → Nat) → Prop}\n\ntheorem word_sOk_of {s : SMachine} {ps : List SEntry} (hl : checkSMachineWithFacts s ps extra = true) : sOk s = true := by\n simp only [checkSMachineWithFacts, Bool.and_eq_true] at hl; exact hl.1.1.1.1.1\n\ntheorem word_spre_parts {s : SMachine} {ps : List SEntry} (hl : checkSMachineWithFacts s ps extra = true) :\n avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) s.f.m.pre = true ∧ readsC s.f.m.pre = false := by\n simp only [checkSMachineWithFacts, Bool.and_eq_true] at hl; exact ⟨hl.1.1.1.1.2, by simpa using hl.1.1.1.2⟩\n\ntheorem word_sfacts_ok {s : SMachine} {ps : List SEntry} (hl : checkSMachineWithFacts s ps extra = true) : factsOk s = true := by\n simp only [checkSMachineWithFacts, Bool.and_eq_true] at hl; exact hl.1.2\n\ntheorem word_slaws_avoid {s : SMachine} {ps : List SEntry} (hl : checkSMachineWithFacts s ps extra = true) :\n ∀ l ∈ ps.map (·.law), avoids s.extVars l.holds = true ∧ avoidsMaps s.carried l.holds = true := by\n simp only [checkSMachineWithFacts, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at hl\n intro l hl'\n obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl'\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩ := hl.2 e' he'\n refine ⟨?_, h2⟩\n simp only [avoids, List.all_append, Bool.and_eq_true] at h1 ⊢; exact h1.1\n\ntheorem word_skey2_parts {s : SMachine} {ps : List SEntry} (h : checkSMachineWithFacts s ps extra = true) {e : SEntry} (he : e ∈ ps) {g : Nat} (hg : g ∈ e.keys2) :\n skFresh s g = true ∧ (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains g = false\n ∧ occC g s.f.m.pre = false ∧ (∀ p ∈ s.f.m.init, occE g p.2 = false) ∧ (∀ e' ∈ ps, occC g e'.law.holds = false ∨ g ∈ e'.keys2)\n ∧ e.law.key ≠ some g := by\n simp only [checkSMachineWithFacts, List.all_eq_true, Bool.and_eq_true] at h\n have hk := (h.2 e he).1.1.2\n simp only [key2Ok, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true', bne_iff_ne, ne_eq, Bool.or_eq_true, List.contains_iff_mem] at hk\n obtain ⟨⟨⟨⟨⟨hkey, hf⟩, hc⟩, hp⟩, hi⟩, hall⟩ := hk g hg\n exact ⟨hf, hc, hp, hi, fun e' he' => (by have := (hall e' he').1; simpa using this), hkey⟩\n\ntheorem word_skeys_apart {s : SMachine} {ps : List SEntry} (h : checkSMachineWithFacts s ps extra = true) : ∀ e ∈ ps, ∀ k, e.law.key = some k → k ∉ e.keys2 :=\n fun e he k hk hg => (word_skey2_parts extra h he hg).2.2.2.2.2 hk\n\n/-- The start: at params that meet the precondition and the facts, with maps as declared, every law holds. -/\ntheorem word_sstart_case (s : SMachine) (ps : List SEntry) (extra : List C) (h : checkSMachineWithFacts s ps extra = true)\n (hExtra : ∀ X N, P N → ∀ c ∈ extra, evalC X N c = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (hWorld : P M)\n (hM : StartMaps s.f.m M) (h0 : evalC ρ M s.f.m.pre = true) (hfa : ∀ l ∈ s.facts, l.Holds ρ M) (hpf : s.PFacts ρ M) :\n ∀ e ∈ ps, e.law.Holds (applyS s.f.m.init ρ M) M := by\n simp only [checkSMachineWithFacts, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at h\n obtain ⟨⟨⟨⟨⟨_, _⟩, _⟩, _⟩, hfok⟩, h⟩ := h\n intro e he\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨_, _⟩, hfresh⟩, hslaw⟩, hspre⟩, hsinit⟩, hskf⟩, _⟩, hst⟩, _⟩ := h e he\n refine goalOf_holds e.law (hslaw e he) _ M (fun v => ?_)\n apply Decidable.byContradiction; intro hn\n apply sound_in_with (sStartGoal s e) extra e.start hst (upd ρ e.sk v) M (hExtra _ _ hWorld)\n intro c hc\n simp only [sStartGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc\n rcases hc with (rfl | hc) | rfl\n · rw [congrC _ ρ M s.f.m.pre (fun i hi => by\n simp only [upd]; split\n · rename_i hik; subst hik; simp [hspre] at hi\n · rfl)]\n exact h0\n · -- The facts hold at the start, and read not the skolem.\n exact facthyps_true s e.finsts e.pfinsts _ M (fun f hf => (pfactsOk_parts hfok f hf).2.2)\n (facts_frame s hfok [e.sk] [] (fun l hl => by simp [avoids, (skFresh_parts hskf).2.2.2.2.2.2 l hl])\n (fun _ _ => by simp [avoidsMaps]) (fun i _ hik => by simp only [upd]; rw [if_neg (by simpa using hik)]) (fun _ _ _ => rfl) hfa)\n (pfacts_frame s hfok [e.sk] [] (fun f hf => by simp [avoids, skFresh_pfacts hskf f hf])\n (fun _ _ => by simp [avoidsMaps]) (fun i _ hik => by simp only [upd]; rw [if_neg (by simpa using hik)]) (fun _ _ _ => rfl) hpf) c hc\n · have hrd : ∀ mp k, evalE (upd ρ e.sk v) M (rdStart s.f.m mp k) = M mp (evalE (upd ρ e.sk v) M k) := by\n intro mp k\n simp only [rdStart]\n split\n · rename_i p hp\n have ⟨hm, hq⟩ := lookup_some hp\n simp only [beq_iff_eq] at hq\n simp only [evalE]; rw [← hq, hM p hm]\n · rfl\n simp only [evalC, substR_evalC _ _ _ M M hrd]\n rw [applyS_upd (fun h => by simp [h] at hfresh) (by simpa [List.all_eq_true] using hsinit)]\n simpa using hn\n\n\ntheorem word_sstep_case (s : SMachine) (ps : List SEntry) (extra : List C) (h : checkSMachineWithFacts s ps extra = true)\n (hExtra : ∀ X N, P N → ∀ c ∈ extra, evalC X N c = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) (hWorld : P (withCarried s M κ)) (hp0 : evalC ρ M s.f.m.pre = true) (hfa : ∀ l ∈ s.facts, l.Holds ρ M) (hpf : s.PFacts ρ M)\n (ih : ∀ e ∈ ps, e.Holds ρ M) (ha : evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true) :\n ∀ e ∈ ps, e.law.Holds (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 := by\n have hlav := word_slaws_avoid extra h\n have hkk := word_skeys_apart extra h\n simp only [checkSMachineWithFacts, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at h\n obtain ⟨⟨⟨⟨⟨hs, hpre⟩, hrp⟩, hterms⟩, hfok⟩, h⟩ := h\n obtain ⟨hfav, hstp, hwr, hev⟩ := sTermsOk_parts hterms\n intro e he\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨hlx, hlm⟩, hfresh⟩, hslaw⟩, hspre⟩, _⟩, hskf⟩, _⟩, _⟩, hst⟩ := h e he\n obtain ⟨hf1, hf2, hf3, hf4, hf5, hf6, hf7⟩ := skFresh_parts hskf\n have hnot : e.sk ∉ s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1) := fun hm => by\n rw [List.contains_iff_mem.mpr hm] at hfresh; cases hfresh\n have hskE : e.sk ∉ s.extVars := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ hm)))\n have hskF : e.sk ∉ flagVars s.f.flags := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_right _ hm)))\n -- The law at the next state with the carried maps in place reads as at the next state: it reads none of them.\n refine holds_transfer (V := []) (MS := s.carried) (ρ' := (sNext s ρ M ι κ).1) (M' := withCarried s (sNext s ρ M ι κ).2 κ) e.law (by simp [avoids]) hlm (fun i _ => rfl)\n (fun mp hmp => by simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]) ?_\n refine goalOf_holds e.law (hslaw e he) _ _ (fun v => ?_)\n apply Decidable.byContradiction; intro hn\n -- The frame runs from the state with the skolem set to v: the machine reads it nowhere, so the step is the same.\n obtain ⟨hNext, hView, _⟩ := fresh_parts hskf (by simpa [List.contains_iff_mem] using hnot) ρ M ι κ v\n let W := withExt s (upd ρ e.sk v) (sReq s (upd ρ e.sk v) M ι κ)\n let M0 := withCarried s M κ\n have hH : ∀ Z, SHead s Z [] W M0 [] getRd (upd ρ e.sk v) M [] [] ι κ := fun Z => by\n refine ⟨fun i _ => ?_, fun mp k _ => ?_⟩\n · show applyS (frameSub [] []) W M0 i = W i\n simp only [frameSub, List.map_nil, List.append_nil, applyS_nil']\n · simp [frameRd, getRd, lookup, evalE, M0]\n have hD : SDenotes s (flagVars s.f.flags) [] W M0 [] getRd (upd ρ e.sk v) M := by\n refine ⟨fun i hie _ => ?_, fun mp k hmp _ => ?_⟩\n · rw [applyS_nil']; simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]\n · simp only [getRd, evalE, M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]\n have hD' := sstep_denotes s (flagVars s.f.flags) [] W M0 hs (fun v hv => hv) hfav [] [] ι κ hD (hH _) (fun p hp => by simp at hp)\n hstp (fun w hw => (hwr w hw).1) hev (fun _ _ => by simp [avoidsMapsE]) (fun _ _ => by simp [avoidsMapsE]) (fun _ _ => by simp [avoidsMapsE])\n apply sound_in_with (sStepGoal s ps e) extra e.step hst W M0 (hExtra _ _ (by simpa [M0] using hWorld))\n intro c hc\n simp only [sStepGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false, List.mem_map] at hc\n rcases hc with ((rfl | hc) | ⟨c', hc', rfl⟩) | rfl\n · -- The precondition: it reads no map, no request and not the skolem.\n rw [noReadsC W M0 M s.f.m.pre hrp, congrC W ρ M s.f.m.pre (fun i hi => ?_)]\n · exact hp0\n · have hi1 := avoids_occ hpre hi\n have hik : i ≠ e.sk := fun h' => by rw [h'] at hi; simp [hspre] at hi\n simp only [W, withExt]\n rw [if_neg (fun h' => hi1 (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.contains_iff_mem.mp h')))))]\n simp only [upd, if_neg hik]\n · -- The facts: they read no request, not the skolem, and no map a request carries.\n exact facthyps_true s e.finsts e.pfinsts W M0 (fun f hf => (pfactsOk_parts hfok f hf).2.2)\n (facts_frame s hfok [e.sk] [] (fun l hl => by simp [avoids, hf7 l hl])\n (fun _ _ => by simp [avoidsMaps])\n (fun i hie hik => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]; simp only [upd]; rw [if_neg (by simpa using hik)])\n (fun mp hmc _ => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]) hfa)\n (pfacts_frame s hfok [e.sk] [] (fun f hf => by simp [avoids, skFresh_pfacts hskf f hf])\n (fun _ _ => by simp [avoidsMaps])\n (fun i hie hik => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]; simp only [upd]; rw [if_neg (by simpa using hik)])\n (fun mp hmc _ => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]) hpf) c hc\n · -- The entries hold at the state with the skolem set (their laws don't read it), so in the goal's view of it, and so\n -- does each pair's law; the assumption reads the skolem nowhere either.\n have hA : evalC (sView s (upd ρ e.sk v) M ι κ).1 (sView s (upd ρ e.sk v) M ι κ).2 s.f.m.assume = true := by\n rw [hView]; dsimp only\n rw [congrC _ (sView s ρ M ι κ).1 _ _ (fun i hi => by\n have hik : i ≠ e.sk := fun h' => by rw [h', hf3] at hi; cases hi\n simp only [upd, if_neg hik])]\n exact ha\n have hallY : ∀ e' ∈ ps, e'.Holds W M0 := fun e' he' =>\n sentry_transfer (hlav e'.law (List.mem_map_of_mem he')).1 (hlav e'.law (List.mem_map_of_mem he')).2\n (fun i hi => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi)])\n (fun mp hmp => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)])\n (sentry_upd (Or.inl (hslaw e' he')) v (ih e' he'))\n exact sframe_hyps s [] [] W M0 hs (sLaws ps e.pinsts) (sInsts ps e.insts e.pinsts) e.qinsts\n (fun l hl => by\n rcases List.mem_append.mp hl with hl | hl\n · obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl; exact SEntry.holds_law (hallY e' he')\n · exact pairLaws_hold ps e.pinsts hkk hallY l hl)\n (hH []) (fun _ _ => by simp [avoids]) (fun _ _ => by simp [avoids]) (fun _ _ => by simp [avoidsMaps]) hA (by simp [trueC, evalC, evalE]) c' hc'\n · -- The goal, read across the step: the maps a request carries read as carried, the rest as the step leaves them.\n have hrd' : ∀ mp k, evalE W M0 (stepRdS s (frameSub [] []) (frameRd [] getRd) mp k) = withCarried s (sNext s ρ M ι κ).2 κ mp (evalE W M0 k) := by\n intro mp k\n by_cases hmp : mp ∈ s.carried\n · have hne : lookup (fun w => w.1 == mp) s.every = none := lookup_none (fun w hw => by\n simp only [beq_eq_false_iff_ne]; intro h'; exact (hev w hw).2.2.2.2.1 (by rw [h']; exact hmp))\n have hnw : lookup (fun w : Nat × E × E => w.1 == mp) s.f.m.writes = none := lookup_none (fun w hw => by\n simp only [beq_eq_false_iff_ne]; intro h'; exact (hwr w hw).2 (by rw [h']; exact hmp))\n simp only [stepRdS, hne, stepRd, hnw]\n simp [frameRd, getRd, lookup, evalE, M0, withCarried, hmp]\n · rw [hD'.2 mp k hmp (by simp), hNext]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]\n simp only [evalC]\n rw [substR_evalC _ _ W M0 _ hrd', goalOf_agree e.law e.sk _ (X' := upd (sNext s ρ M ι κ).1 e.sk v)\n (by rw [hD'.1 e.sk hskE hskF, hNext]) (fun i hi => ?_)]\n · simpa using hn\n · have hi1 := avoids_occ hlx hi\n rw [hD'.1 i (fun h' => hi1 (List.mem_append_left _ h')) (fun h' => hi1 (List.mem_append_right _ h')), hNext]\n\n/-- The induction rule with sets: for params that meet the precondition, maps that start as declared and functions that\nmeet the facts, every entry of an accepted proof holds in every reachable state (a law about several keys at every value\nof its further keys), whatever maps each request carries. -/\n\ntheorem machine_sound_under_world (s : SMachine) (ps : List SEntry) (extra : List C) (h : checkSMachineWithFacts s ps extra = true)\n (hExtra : ∀ X N, P N → ∀ c ∈ extra, evalC X N c = true)\n (hWorld : ∀ ρ M, SReach s ρ M → P M) (hCarried : ∀ M κ, P M → P (withCarried s M κ)) :\n ∀ ρ M, SReach s ρ M → ∀ e ∈ ps, e.Holds ρ M := by\n have hpre := word_spre_parts extra h\n have hfok := word_sfacts_ok extra h\n -- Facts at a state hold with a further key set: they read no further key.\n have hfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → (∀ l ∈ s.facts, l.Holds ρ M) → ∀ w, ∀ l ∈ s.facts, l.Holds (upd ρ g w) M := by\n intro ρ M g hf hfa w\n exact facts_frame s hfok [g] [] (fun l hl => by simp [avoids, (skFresh_parts hf).2.2.2.2.2.2 l hl]) (fun _ _ => by simp [avoidsMaps])\n (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hfa\n have hpfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → s.PFacts ρ M → ∀ w, s.PFacts (upd ρ g w) M := by\n intro ρ M g hf hpf w\n exact pfacts_frame s hfok [g] [] (fun f hfm => by simp [avoids, skFresh_pfacts hf f hfm]) (fun _ _ => by simp [avoidsMaps])\n (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hpf\n have hpreu : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, occC g s.f.m.pre = false → evalC ρ M s.f.m.pre = true → ∀ w, evalC (upd ρ g w) M s.f.m.pre = true := by\n intro ρ M g hpg h0 w\n rw [congrC _ ρ M s.f.m.pre (fun i hi => by have hig' : i ≠ g := (fun h' => by rw [h', hpg] at hi; cases hi); simp only [upd, if_neg hig'])]; exact h0\n intro ρ M hr\n induction hr with\n | start ρ M hM h0 hfa hpf =>\n intro e he\n -- Each further key set in turn, from a start state that meets the precondition and the facts.\n suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →\n s.PFacts ρ' M → holdsOver e.law M gs (applyS s.f.m.init ρ' M) from hs e.keys2 (fun g hg => hg) ρ h0 hfa hpf\n intro gs\n induction gs with\n | nil => intro _ ρ' hp hf hq; exact word_sstart_case s ps extra h hExtra ρ' M (hWorld _ _ (SReach.start _ _ hM hp hf hq)) hM hp hf hq e he\n | cons g gs ih =>\n intro hsub ρ' hp hf hq w\n obtain ⟨hfr, hc, hpg, hig, _, _⟩ := word_skey2_parts extra h he (hsub g List.mem_cons_self)\n have hI := (fresh_parts hfr hc ρ' M (fun _ => 0) (fun _ _ => 0) 0).2.2\n rw [← applyS_upd hI (by simpa [List.all_eq_true] using hig)]\n exact ih (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)\n | step ρ M ι κ hr ha ih =>\n intro e he\n suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →\n s.PFacts ρ' M → (∀ e' ∈ ps, e'.Holds ρ' M) → evalC (sView s ρ' M ι κ).1 (sView s ρ' M ι κ).2 s.f.m.assume = true →\n holdsOver e.law (sNext s ρ' M ι κ).2 gs (sNext s ρ' M ι κ).1 from\n hs e.keys2 (fun g hg => hg) ρ (pre_sreach s hpre.1 hpre.2 hr) (facts_sreach s hfok hr) (pfacts_sreach s hfok hr) ih ha\n intro gs\n induction gs with\n | nil => intro _ ρ' hp hf hq hi ha'; exact word_sstep_case s ps extra h hExtra ρ' M ι κ (hCarried M κ (hWorld _ _ hr)) hp hf hq hi ha' e he\n | cons g gs ihg =>\n intro hsub ρ' hp hf hq hi ha' w\n obtain ⟨hfr, hc, hpg, _, hall, _⟩ := word_skey2_parts extra h he (hsub g List.mem_cons_self)\n obtain ⟨hNext, hView, _⟩ := fresh_parts hfr hc ρ' M ι κ w\n have := ihg (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)\n (fun e' he' => sentry_upd (hall e' he') w (hi e' he'))\n (by\n rw [hView]; dsimp only\n rw [congrC _ (sView s ρ' M ι κ).1 _ _ (fun i hi => by\n have hig' : i ≠ g := fun h' => by rw [h', (skFresh_parts hfr).2.2.1] at hi; cases hi\n simp only [upd, if_neg hig'])]\n exact ha')\n rw [hNext] at this; exact this\ntheorem concrete_smachine_sound (w : WordReach.WordSMachine) (ps : List SEntry) (extra : List C) (h : checkSMachineWithFacts w.s ps extra = true)\n (hExtra : ∀ X N, WordReach.WordInterpret w.reserved w.value N → ∀ c ∈ extra, evalC X N c = true) :\n ∀ ρ M, WordReach.WordSReach w ρ M → ∀ e ∈ ps, e.Holds ρ M := by\n let s := w.s\n have hpre := word_spre_parts extra h\n have hfok := word_sfacts_ok extra h\n -- Facts at a state hold with a further key set: they read no further key.\n have hfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → (∀ l ∈ s.facts, l.Holds ρ M) → ∀ w, ∀ l ∈ s.facts, l.Holds (upd ρ g w) M := by\n intro ρ M g hf hfa w\n exact facts_frame s hfok [g] [] (fun l hl => by simp [avoids, (skFresh_parts hf).2.2.2.2.2.2 l hl]) (fun _ _ => by simp [avoidsMaps])\n (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hfa\n have hpfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → s.PFacts ρ M → ∀ w, s.PFacts (upd ρ g w) M := by\n intro ρ M g hf hpf w\n exact pfacts_frame s hfok [g] [] (fun f hfm => by simp [avoids, skFresh_pfacts hf f hfm]) (fun _ _ => by simp [avoidsMaps])\n (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hpf\n have hpreu : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, occC g s.f.m.pre = false → evalC ρ M s.f.m.pre = true → ∀ w, evalC (upd ρ g w) M s.f.m.pre = true := by\n intro ρ M g hpg h0 w\n rw [congrC _ ρ M s.f.m.pre (fun i hi => by have hig' : i ≠ g := (fun h' => by rw [h', hpg] at hi; cases hi); simp only [upd, if_neg hig'])]; exact h0\n intro ρ M hr\n induction hr with\n | start ρ base hM h0 hfa hpf =>\n let M := WordReach.overlay w.reserved w.value base\n intro e he\n -- Each further key set in turn, from a start state that meets the precondition and the facts.\n suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →\n s.PFacts ρ' M → holdsOver e.law M gs (applyS s.f.m.init ρ' M) from hs e.keys2 (fun g hg => hg) ρ h0 hfa hpf\n intro gs\n induction gs with\n | nil => intro _ ρ' hp hf hq; exact word_sstart_case s ps extra h hExtra ρ' M (WordReach.overlay_interpret _ _ _) (WordReach.overlay_start w base hM) hp hf hq e he\n | cons g gs ih =>\n intro hsub ρ' hp hf hq w\n obtain ⟨hfr, hc, hpg, hig, _, _⟩ := word_skey2_parts extra h he (hsub g List.mem_cons_self)\n have hI := (fresh_parts hfr hc ρ' M (fun _ => 0) (fun _ _ => 0) 0).2.2\n rw [← applyS_upd hI (by simpa [List.all_eq_true] using hig)]\n exact ih (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)\n | step ρ M ι κ hr ha ih =>\n intro e he\n suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →\n s.PFacts ρ' M → (∀ e' ∈ ps, e'.Holds ρ' M) → evalC (sView s ρ' M ι κ).1 (sView s ρ' M ι κ).2 s.f.m.assume = true →\n holdsOver e.law (sNext s ρ' M ι κ).2 gs (sNext s ρ' M ι κ).1 from\n hs e.keys2 (fun g hg => hg) ρ (pre_sreach s hpre.1 hpre.2 (WordReach.word_sreach_sreach w hr)) (facts_sreach s hfok (WordReach.word_sreach_sreach w hr)) (pfacts_sreach s hfok (WordReach.word_sreach_sreach w hr)) ih ha\n intro gs\n induction gs with\n | nil => intro _ ρ' hp hf hq hi ha'; exact word_sstep_case s ps extra h hExtra ρ' M ι κ (WordReach.carried_interpret w (WordReach.word_sreach_interpret w hr)) hp hf hq hi ha' e he\n | cons g gs ihg =>\n intro hsub ρ' hp hf hq hi ha' w\n obtain ⟨hfr, hc, hpg, _, hall, _⟩ := word_skey2_parts extra h he (hsub g List.mem_cons_self)\n obtain ⟨hNext, hView, _⟩ := fresh_parts hfr hc ρ' M ι κ w\n have := ihg (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)\n (fun e' he' => sentry_upd (hall e' he') w (hi e' he'))\n (by\n rw [hView]; dsimp only\n rw [congrC _ (sView s ρ' M ι κ).1 _ _ (fun i hi => by\n have hig' : i ≠ g := fun h' => by rw [h', (skFresh_parts hfr).2.2.1] at hi; cases hi\n simpa only [s, upd, if_neg hig'])]\n exact ha')\n rw [hNext] at this; exact this\n\n\n/-- Every original k-step guard, plus disjointness of reserved IDs from frame map targets. -/\ndef checkSKStepWithFacts (w : WordReach.WordSMachine) (ps : List SEntry) (sl : SKStep) (extra : List C) : Bool :=\n let s := w.s\n sl.k.whens.length == sl.k.ren.length && sl.mren.length == sl.k.ren.length && sl.k.ren.head? == some [] && sl.mren.head? == some []\n && distinct (tgts sl.k.ren) && distinct (tgts sl.mren)\n && sl.k.ren.all (fun r => r.all fun p => s.extVars.contains p.1)\n && sl.k.ren.tail.all (fun r => s.extVars.all fun i => r.any (·.1 == i))\n && sl.mren.all (fun r => r.all fun p => s.carried.contains p.1)\n && sl.mren.tail.all (fun r => s.carried.all fun mp => r.any (·.1 == mp))\n && sl.k.prime.all (fun q => !s.extVars.contains q.1 && !(szone s sl).contains q.1)\n && sl.k.primeMaps.all (fun q => !s.carried.contains q.1 && !(tgts sl.mren).contains q.1)\n && avoids (szone s sl) s.f.m.pre\n && sl.k.whens.all (fun w => (sFrameHyps s (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) [] w).all (fun c => avoids (szone s sl) c)\n && (sFrameHyps s (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) sl.qinsts w).all (fun c => avoidsMaps (tgts sl.mren) c))\n && s.qflags.all (fun q => avoids (szone s sl) q.2.2)\n && sTermsOk s (szone s sl) && sMapsOk s (tgts sl.mren)\n && checkInWithFacts (sKGoal s ps sl) extra ⟨sl.k.proof.1, sl.used, sl.k.proof.2⟩\n && distinct (tgts sl.k.ren ++ tgts sl.names)\n && sl.names.all (fun n => n.all fun p => !s.extVars.contains p.1 && !(szone s sl).contains p.1)\n && avoids (tgts sl.names) sl.k.then_\n && (tgts sl.mren).all (fun mp => !w.reserved.contains mp)\n\n/-- The same k-step conclusion, restricted to concrete-word reachable states. -/\ndef WordSKStepHolds (w : WordReach.WordSMachine) (sl : SKStep) : Prop :=\n ∀ ρ M, WordReach.WordSReach w ρ M → ∀ q₁ qs, qs.length + 1 = sl.k.whens.length → SAllowed w.s sl.k.whens ρ M (q₁ :: qs) →\n evalC (primed sl.k.prime (sEnd w.s ρ M (q₁ :: qs)).1 (renEnv sl.k.ren (sExts w.s ρ M (q₁ :: qs)) (withExt w.s ρ (sReq w.s ρ M q₁.1 q₁.2))))\n (primedMaps sl.k.primeMaps (sEnd w.s ρ M (q₁ :: qs)).2 (renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried w.s M q₁.2))) sl.k.then_ = true\n\ntheorem word_skrun_sound (machine : WordReach.WordSMachine) (ps : List SEntry) (extra : List C) (hl : checkSMachineWithFacts machine.s ps extra = true) (Z MZ : List Nat) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E))\n (hlY : ∀ ρ M ι κ, WordReach.WordSReach machine ρ M → ∀ l ∈ laws, l.Holds (withExt machine.s ρ (sReq machine.s ρ M ι κ)) (withCarried machine.s M κ))\n (W : Nat → Nat) (M0 : Nat → Nat → Nat)\n (hfz : ∀ v ∈ flagVars machine.s.f.flags, v ∈ Z) (hterms : sTermsOk machine.s Z = true) (hmaps : sMapsOk machine.s MZ = true)\n (hqb : ∀ q ∈ machine.s.qflags, avoids Z q.2.2 = true) :\n ∀ (ws : List C) (rs mrs ns : List (List (Nat × Nat))) (qs : List SRequest) (σ : List (Nat × E)) (rd : Nat → E → E)\n (ρ : Nat → Nat) (M : Nat → Nat → Nat),\n ws.length = rs.length → ws.length = mrs.length → ws.length = qs.length →\n (∀ whenC ∈ ws, (∀ c ∈ sFrameHyps machine.s laws insts [] whenC, avoids Z c = true) ∧ ∀ c ∈ sFrameHyps machine.s laws insts qinsts whenC, avoidsMaps MZ c = true) →\n (∀ r ∈ rs, ∀ p ∈ r, p.1 ∈ machine.s.extVars) →\n (∀ r ∈ rs.tail, ∀ i ∈ machine.s.extVars, ∃ p ∈ r, p.1 = i) →\n (∀ r ∈ mrs, ∀ p ∈ r, p.1 ∈ machine.s.carried) →\n (∀ r ∈ mrs.tail, ∀ mp ∈ machine.s.carried, ∃ p ∈ r, p.1 = mp) →\n RenOK W rs.tail (sExts machine.s ρ M qs).tail → MRenOK M0 mrs.tail (qs.map (·.2)).tail →\n (∀ n ∈ ns, ∀ p ∈ n, p.1 ∉ machine.s.extVars ∧ p.1 ∉ Z) → RenOK W ns (sStates machine.s ρ M qs) →\n WordReach.WordSReach machine ρ M → SDenotes machine.s Z MZ W M0 σ rd ρ M → SHeadL machine.s Z MZ W M0 σ rd ρ M rs mrs qs → SAllowed machine.s ws ρ M qs →\n (∀ c ∈ (sRun machine.s laws insts qinsts ws rs mrs ns σ rd).1, evalC W M0 c = true)\n ∧ SDenotes machine.s Z MZ W M0 (sRun machine.s laws insts qinsts ws rs mrs ns σ rd).2.1 (sRun machine.s laws insts qinsts ws rs mrs ns σ rd).2.2\n (sEnd machine.s ρ M qs).1 (sEnd machine.s ρ M qs).2\n | [], rs, mrs, ns, qs, σ, rd, ρ, M, hl1, hl2, hl3, _, _, _, _, _, _, _, _, _, _, hD, _, _ => by\n cases rs with\n | cons _ _ => simp at hl1\n | nil =>\n cases mrs with\n | cons _ _ => simp at hl2\n | nil =>\n cases qs with\n | cons _ _ => simp at hl3\n | nil => exact ⟨fun c hc => by simp [sRun] at hc, hD⟩\n | whenC :: ws, rs, mrs, ns, qs, σ, rd, ρ, M, hl1, hl2, hl3, hav, hkeys, hcov, hmkeys, hmcov, hren, hmren, hnsrc, hnok, hr, hD, hH, hK => by\n rcases rs with _ | ⟨r, rs⟩\n · simp at hl1\n rcases mrs with _ | ⟨mr, mrs⟩\n · simp at hl2\n rcases qs with _ | ⟨q, qs⟩\n · simp at hl3\n simp only [List.length_cons, Nat.add_right_cancel_iff] at hl1 hl2 hl3\n simp only [List.tail_cons] at hcov hmcov\n have hren1 : RenOK W rs (sExts machine.s (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 qs) := hren\n have hmren1 : MRenOK M0 mrs (qs.map (·.2)) := hmren\n obtain ⟨hA, hW, hK'⟩ := hK\n have hH1 : SHead machine.s Z MZ W M0 σ rd ρ M r mr q.1 q.2 := hH\n obtain ⟨hfav, hstp, hwr, hev⟩ := sTermsOk_parts hterms\n obtain ⟨hstm, hwrm, hevm⟩ := sMapsOk_parts hmaps\n have hD' := sstep_denotes machine.s Z MZ W M0 (word_sOk_of extra hl) hfz hfav r mr q.1 q.2 hD hH1 (hkeys r List.mem_cons_self) hstp\n (fun machine hw => (hwr machine hw).1) hev hstm hwrm hevm\n have hr' : WordReach.WordSReach machine (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 := WordReach.WordSReach.step ρ M q.1 q.2 hr hA\n -- Each fresh variable names the next state value of its source variable.\n have hnames : ∀ p ∈ ns.headD [], W p.2 = (sNext machine.s ρ M q.1 q.2).1 p.1 := by\n cases ns with\n | nil => intro p hp; simp at hp\n | cons n ns' => exact hnok.1\n have hnsrc1 : ∀ p ∈ ns.headD [], p.1 ∉ machine.s.extVars ∧ p.1 ∉ Z := by\n cases ns with\n | nil => intro p hp; simp at hp\n | cons n ns' => exact hnsrc n List.mem_cons_self\n have hD'' : SDenotes machine.s Z MZ W M0 (frameSub (ns.headD []) (stepSub machine.s.f (frameSub r σ) (frameRd mr rd))) (stepRdS machine.s (frameSub r σ) (frameRd mr rd))\n (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 := by\n refine ⟨fun v hve hvz => ?_, hD'.2⟩\n by_cases hin : ∃ p ∈ ns.headD [], p.1 = v\n · exact applyS_frame_in hin hnames\n · rw [applyS_frame_out (fun p hp he => hin ⟨p, hp, he⟩)]; exact hD'.1 v hve hvz\n -- The next frame agrees with the successor state and request.\n have hH' : SHeadL machine.s Z MZ W M0 (frameSub (ns.headD []) (stepSub machine.s.f (frameSub r σ) (frameRd mr rd))) (stepRdS machine.s (frameSub r σ) (frameRd mr rd))\n (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 rs mrs qs := by\n rcases rs with _ | ⟨r', rs'⟩\n · exact trivial\n rcases mrs with _ | ⟨mr', mrs'⟩\n · exact trivial\n rcases qs with _ | ⟨q', qs'⟩\n · exact trivial\n have hr1 : ∀ p ∈ r', W p.2 = sReq machine.s (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 q'.1 q'.2 p.1 := hren1.1\n have hm1 : ∀ p ∈ mr', M0 p.2 = q'.2 p.1 := hmren1.1\n refine ⟨fun v hvz => ?_, fun mp k hmz => ?_⟩\n · by_cases hve : v ∈ machine.s.extVars\n · rw [applyS_frame_in (hcov r' List.mem_cons_self v hve) hr1]\n simp only [withExt]; rw [if_pos (List.contains_iff_mem.mpr hve)]\n · rw [applyS_frame_out (fun p hp he => hve (by rw [← he]; exact hkeys r' (List.mem_cons_of_mem _ List.mem_cons_self) p hp)),\n hD''.1 v hve hvz]\n simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hve)]\n · by_cases hmc : mp ∈ machine.s.carried\n · cases hlk : lookup (fun p : Nat × Nat => p.1 == mp) mr' with\n | none =>\n obtain ⟨p, hp, hp1⟩ := hmcov mr' List.mem_cons_self mp hmc\n have := lookup_none_all hlk p hp\n simp [hp1] at this\n | some p0 =>\n have hp0 := lookup_some hlk\n simp only [beq_iff_eq] at hp0\n simp only [frameRd, hlk, evalE]\n rw [hm1 p0 hp0.1, hp0.2]; simp only [withCarried]; rw [if_pos (List.contains_iff_mem.mpr hmc)]\n · have hlk : lookup (fun p : Nat × Nat => p.1 == mp) mr' = none := lookup_none (fun p hp => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact hmc (by rw [← he]; exact hmkeys mr' (List.mem_cons_of_mem _ List.mem_cons_self) p hp))\n simp only [frameRd, hlk]\n rw [hD'.2 mp k hmc hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]\n have hren' : RenOK W rs.tail (sExts machine.s (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 qs).tail := by\n rcases rs with _ | ⟨r', rs'⟩\n · exact trivial\n rcases qs with _ | ⟨q', qs'⟩\n · cases rs' <;> exact trivial\n exact hren1.2\n have hmren' : MRenOK M0 mrs.tail (qs.map (·.2)).tail := by\n rcases mrs with _ | ⟨mr', mrs'⟩\n · exact trivial\n rcases qs with _ | ⟨q', qs'⟩\n · cases mrs' <;> exact trivial\n exact hmren1.2\n have hnsrc' : ∀ n ∈ ns.tail, ∀ p ∈ n, p.1 ∉ machine.s.extVars ∧ p.1 ∉ Z := fun n hn => hnsrc n (List.mem_of_mem_tail hn)\n have hnok' : RenOK W ns.tail (sStates machine.s (sNext machine.s ρ M q.1 q.2).1 (sNext machine.s ρ M q.1 q.2).2 qs) := by\n cases ns with\n | nil => exact trivial\n | cons n ns' => exact hnok.2\n obtain ⟨ih1, ih2⟩ := word_skrun_sound machine ps extra hl Z MZ laws insts qinsts hlY W M0 hfz hterms hmaps hqb ws rs mrs ns.tail qs _ _ _ _ hl1 hl2 hl3\n (fun machine' hw' => hav machine' (List.mem_cons_of_mem _ hw'))\n (fun r' hr'' => hkeys r' (List.mem_cons_of_mem _ hr''))\n (fun r' hr'' => hcov r' (List.mem_of_mem_tail hr''))\n (fun r' hr'' => hmkeys r' (List.mem_cons_of_mem _ hr''))\n (fun r' hr'' => hmcov r' (List.mem_of_mem_tail hr''))\n hren' hmren' hnsrc' hnok' hr' hD'' hH' hK'\n refine ⟨fun c hc => ?_, ih2⟩\n simp only [sRun, List.mem_append, List.mem_map] at hc\n rcases hc with (⟨c', hc', rfl⟩ | hc) | hc\n · exact sframe_hyps machine.s Z MZ W M0 (word_sOk_of extra hl) laws insts qinsts (hlY ρ M q.1 q.2 hr)\n hH1 (hav whenC List.mem_cons_self).1 hqb (hav whenC List.mem_cons_self).2 hA hW c' hc'\n · -- Each fresh name equals the successor value denoted by its term.\n simp only [nameHyps, List.mem_map] at hc\n obtain ⟨p, hp, rfl⟩ := hc\n simp only [evalC]\n apply decide_eq_true\n show W p.2 = evalE W M0 ((assoc (stepSub machine.s.f (frameSub r σ) (frameRd mr rd)) p.1).getD (.var p.1))\n rw [hnames p hp, evalE_assoc_getD]\n exact (hD'.1 p.1 (hnsrc1 p hp).1 (hnsrc1 p hp).2).symm\n · exact ih1 c hc\n\ntheorem word_skstep_sound (w : WordReach.WordSMachine) (ps : List SEntry) (sl : SKStep) (extra : List C)\n (hl : checkSMachineWithFacts w.s ps extra = true) (hExtra : ∀ X N, WordReach.WordInterpret w.reserved w.value N → ∀ c ∈ extra, evalC X N c = true)\n (hc : checkSKStepWithFacts w ps sl extra = true) : WordSKStepHolds w sl := by\n let s := w.s\n intro ρ M hr q₁ qs hlen hK\n simp only [checkSKStepWithFacts, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hc\n obtain ⟨hc, hnotarget⟩ := hc\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hlw, hlm⟩, hhead⟩, hmhead⟩, hdist⟩, hmdist⟩, hkeys⟩, hcov⟩, hmkeys⟩, hmcov⟩, hpr⟩, hpm⟩, hpre⟩, hws⟩, hqb⟩, hterms⟩, hmaps⟩, hgoal⟩, hnd⟩, hnsrc⟩, hnthen⟩ := hc\n obtain ⟨hpreS, hrp⟩ := word_spre_parts extra hl\n have hflags : ∀ v ∈ flagVars s.f.flags, v ∈ szone s sl := fun v hv => List.mem_append_left _ (List.mem_append_right _ hv)\n obtain ⟨rs, hrs⟩ : ∃ rs, sl.k.ren = [] :: rs := by\n cases h : sl.k.ren with\n | nil => rw [h] at hhead; simp at hhead\n | cons r rs => rw [h] at hhead; simp only [List.head?_cons, Option.some.injEq] at hhead; exact ⟨rs, by rw [hhead]⟩\n obtain ⟨mrs, hmrs⟩ : ∃ mrs, sl.mren = [] :: mrs := by\n cases h : sl.mren with\n | nil => rw [h] at hmhead; simp at hmhead\n | cons r rs => rw [h] at hmhead; simp only [List.head?_cons, Option.some.injEq] at hmhead; exact ⟨rs, by rw [hmhead]⟩\n -- The goal's environment: each later request at its fresh variables, and each step's state at its names.\n let W0 := renEnv sl.k.ren (sExts s ρ M (q₁ :: qs)) (withExt s ρ (sReq s ρ M q₁.1 q₁.2))\n let W := renEnv sl.names (sStates s ρ M (q₁ :: qs)) W0\n obtain ⟨_, hndn, hndis⟩ := distinct_append hnd\n have hWn : ∀ v, v ∉ tgts sl.names → W v = W0 v := fun v hv => renEnv_other _ _ _ v hv\n let M0 := renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2)\n have hWo : ∀ v, v ∉ szone s sl → W v = withExt s ρ (sReq s ρ M q₁.1 q₁.2) v := fun v hv => by\n rw [hWn v (fun h => hv (List.mem_append_right _ h))]\n exact renEnv_other _ _ _ v (fun h => hv (List.mem_append_left _ (List.mem_append_left _ h)))\n have hMo : ∀ mp, mp ∉ tgts sl.mren → M0 mp = withCarried s M q₁.2 mp := fun mp hmp => renMaps_other _ _ _ mp hmp\n have hD0 : SDenotes s (szone s sl) (tgts sl.mren) W M0 [] getRd ρ M := by\n refine ⟨fun v hve hvz => ?_, fun mp k hmc hmz => ?_⟩\n · rw [applyS_nil', hWo v hvz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hve)]\n · simp only [getRd, evalE]; rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]\n have hH0 : SHeadL s (szone s sl) (tgts sl.mren) W M0 [] getRd ρ M sl.k.ren sl.mren (q₁ :: qs) := by\n rw [hrs, hmrs]\n show SHead s (szone s sl) (tgts ([] :: mrs)) W M0 [] getRd ρ M [] [] q₁.1 q₁.2\n refine ⟨fun v hvz => ?_, fun mp k hmz => ?_⟩\n · simp only [frameSub, List.map_nil, List.append_nil, applyS_nil']; exact hWo v hvz\n · simp only [frameRd, lookup, getRd, evalE]; rw [hMo mp (by rw [hmrs]; exact hmz)]\n have hren0 : RenOK W sl.k.ren.tail (sExts s ρ M (q₁ :: qs)).tail := by\n have h : RenOK W sl.k.ren (sExts s ρ M (q₁ :: qs)) :=\n renOK_congr _ _ (fun v hv => hWn v (hndis v hv)) (renEnv_ok sl.k.ren _ _ hdist)\n rw [hrs] at h ⊢; exact h.2\n have hmren0 : MRenOK M0 sl.mren.tail ((q₁ :: qs).map (·.2)).tail := by\n have h : MRenOK M0 sl.mren ((q₁ :: qs).map (·.2)) := renMaps_ok sl.mren _ _ hmdist\n rw [hmrs] at h ⊢; exact h.2\n -- Every entry holds at each reachable state, so each law (and each pair's) holds in the goal's view of it.\n have hlY : ∀ ρ M ι κ, WordReach.WordSReach w ρ M → ∀ l ∈ sLaws ps sl.pinsts, l.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) := by\n intro ρ M ι κ hr l hl'\n have hall : ∀ e ∈ ps, e.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) := fun e he =>\n sentry_transfer (word_slaws_avoid extra hl e.law (List.mem_map_of_mem he)).1 (word_slaws_avoid extra hl e.law (List.mem_map_of_mem he)).2\n (fun i hi => by simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi)])\n (fun mp hmp => by simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)])\n (concrete_smachine_sound w ps extra hl hExtra ρ M hr e he)\n rcases List.mem_append.mp hl' with hl' | hl'\n · obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl'; exact SEntry.holds_law (hall e' he')\n · exact pairLaws_hold ps sl.pinsts (word_skeys_apart extra hl) hall l hl'\n obtain ⟨hh, hfin⟩ := word_skrun_sound w ps extra hl (szone s sl) (tgts sl.mren) (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) sl.qinsts hlY W M0 hflags hterms hmaps hqb\n sl.k.whens sl.k.ren sl.mren sl.names (q₁ :: qs) [] getRd ρ M hlw (by omega) (by simp only [List.length_cons]; omega)\n hws (fun r hr p hp => List.contains_iff_mem.mp (hkeys r hr p hp))\n (fun r hr i hi => by have := hcov r hr i hi; simpa only [List.any_eq_true, beq_iff_eq] using this)\n (fun r hr p hp => List.contains_iff_mem.mp (hmkeys r hr p hp))\n (fun r hr i hi => by have := hmcov r hr i hi; simpa only [List.any_eq_true, beq_iff_eq] using this)\n hren0 hmren0\n (fun n hn p hp => by\n have h := hnsrc n hn p hp\n simp only [Bool.and_eq_true, Bool.not_eq_true', List.contains_iff_mem] at h\n exact ⟨by simpa using h.1, by simpa using h.2⟩)\n (renEnv_ok sl.names _ _ hndn) hr hD0 hH0 hK\n have hfin' : SDenotes s (szone s sl) (tgts sl.mren) W M0 (sKOut s ps sl).2.1 (sKOut s ps sl).2.2\n (sEnd s ρ M (q₁ :: qs)).1 (sEnd s ρ M (q₁ :: qs)).2 := hfin\n apply Decidable.byContradiction; intro hn\n -- `then` reads none of the names, so the goal's environment reads it as the claim's does.\n have hn' : ¬ evalC (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W) (primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 M0) sl.k.then_ = true := by\n rw [congrC (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W) (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W0) _ sl.k.then_ (fun i hi => by\n simp only [primed]; split\n · rfl\n · exact hWn i (avoids_occ hnthen hi))]\n exact hn\n have hworld : WordReach.WordInterpret w.reserved w.value M0 := by\n intro mp hm key\n have htarget : mp ∉ tgts sl.mren := by\n intro ht\n have hn := hnotarget mp ht\n have hyes : w.reserved.contains mp = true := List.contains_iff_mem.mpr hm\n rw [hyes] at hn\n cases hn\n rw [hMo mp htarget]\n exact WordReach.carried_interpret w (WordReach.word_sreach_interpret w hr) mp hm key\n apply sound_in_with (sKGoal s ps sl) extra ⟨sl.k.proof.1, sl.used, sl.k.proof.2⟩ hgoal W M0 (hExtra W M0 hworld)\n intro c hc\n simp only [sKGoal, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc\n rcases hc with ((rfl | hc) | hc) | rfl\n · -- The precondition: at the first state (it reads no map, no request and no fresh variable).\n rw [noReadsC W M0 M s.f.m.pre hrp, congrC W ρ M s.f.m.pre (fun j hj => ?_)]\n · exact pre_sreach s hpreS hrp (WordReach.word_sreach_sreach w hr)\n · rw [hWo j (avoids_occ hpre hj)]; simp only [withExt]\n rw [if_neg (fun h' => avoids_occ hpreS hj (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.contains_iff_mem.mp h')))))]\n · -- The facts, at the first frame: it agrees with the first state away from the zone and the request.\n exact facthyps_true s sl.finsts sl.pfinsts W M0 (fun f hf => (pfactsOk_parts (word_sfacts_ok extra hl) f hf).2.2)\n (facts_frame s (word_sfacts_ok extra hl) (szone s sl) (tgts sl.mren) (sTermsOk_facts hterms) (sMapsOk_facts hmaps)\n (fun i hie hiz => by rw [hWo i hiz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)])\n (fun mp hmc hmz => by rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)])\n (facts_sreach s (word_sfacts_ok extra hl) (WordReach.word_sreach_sreach w hr)))\n (pfacts_frame s (word_sfacts_ok extra hl) (szone s sl) (tgts sl.mren) (sTermsOk_pfacts hterms) (sMapsOk_pfacts hmaps)\n (fun i hie hiz => by rw [hWo i hiz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)])\n (fun mp hmc hmz => by rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)])\n (pfacts_sreach s (word_sfacts_ok extra hl) (WordReach.word_sreach_sreach w hr))) c hc\n · exact hh c hc\n · -- `then`, read across the run, is what SKStepHolds reads.\n have hrdE : ∀ mp k, evalE W M0 (kEndRd sl.k (sKOut s ps sl).2.2 mp k)\n = primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 M0 mp (evalE W M0 k) := by\n intro mp k\n simp only [kEndRd, primedMaps]\n cases hq : lookup (fun q => q.2 == mp) sl.k.primeMaps with\n | none => simp [evalE]\n | some q =>\n have hq1 := hpm q (lookup_some hq).1\n simp only [Bool.not_eq_true'] at hq1\n exact hfin'.2 q.1 k (by simpa using hq1.1) (by simpa using hq1.2)\n have henv : applyS (kEndSub sl.k (sKOut s ps sl).2.1 (sKOut s ps sl).2.2) W M0\n = primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W := by\n funext i\n rw [kEndSub, applyS_primes]\n simp only [primed]\n cases hq : lookup (fun q => q.2 == i) sl.k.prime with\n | none => rfl\n | some q =>\n have hq1 := hpr q (lookup_some hq).1\n simp only [Bool.not_eq_true'] at hq1\n have hqi : q.1 ∉ s.extVars := by simpa using hq1.1\n have hqz : q.1 ∉ szone s sl := by simpa using hq1.2\n show evalE W M0 (substRE (sKOut s ps sl).2.1 (sKOut s ps sl).2.2 (.var q.1)) = (sEnd s ρ M (q₁ :: qs)).1 q.1\n rw [substR_var]; exact hfin'.1 q.1 hqi hqz\n simp only [evalC, substR_evalC _ _ W M0 _ hrdE, henv]\n simpa using hn'\n\nend Kern.WordSMachine\n\n#print axioms Kern.WordSMachine.concrete_smachine_sound\n#print axioms Kern.WordSMachine.word_skstep_sound\n",
47
60
  };
@@ -0,0 +1,26 @@
1
+ import { type WordDivisionSpec } from '../kernel/word-division.js';
2
+ import type { E } from '../core/machine.js';
3
+ export type WordRead = {
4
+ spec: WordDivisionSpec;
5
+ args: E[];
6
+ key: E;
7
+ map: string;
8
+ };
9
+ /** Walk arbitrary machine/law syntax; invalid reserved names or tuple arity fail closed. */
10
+ export declare function wordReads(source: unknown): WordRead[];
11
+ /** Native helper IDs needed even when a word-valued update is irrelevant to all
12
+ * exported laws and therefore absent from the minimized proof obligations. */
13
+ export declare function wordSchemaMapNames(source: unknown): string[];
14
+ /** Build a closed Lean computation from actual lowered limb values. The expression
15
+ * is independently evaluated by Lean's Nat division, not by a TS answer. */
16
+ export declare function wordDigitLean(read: WordRead, operands: readonly number[]): string;
17
+ /** Right-associated Kern.cpair key decoded in the same order as wordDivisionArgs. */
18
+ export declare function wordKeyPartsLean(key: string, count: number): string[];
19
+ /** A concrete, transparent interpretation of every reserved map index, including
20
+ * helper digit/carry maps introduced only by generated certificate hypotheses.
21
+ * Ordinary map indices continue to read base. This overlay alone is NOT a proof
22
+ * of a machine certificate: all used helper facts need a Lean semantic discharge. */
23
+ export declare function wordMapOverlayLean(reads: readonly WordRead[], maps: readonly string[], name: string): string;
24
+ /** Exact descriptor data for the generic Lean schema. No caller-supplied fact
25
+ * proposition is trusted: Lean constructs the complete clause list itself. */
26
+ export declare function wordMachineBridgeLean(id: string, source: unknown, vars: readonly string[], maps: readonly string[]): string;