gdp-wired 0.43.0 → 0.45.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.
Files changed (79) hide show
  1. package/README.md +8 -3
  2. package/dist/core/c/reader.js +22 -7
  3. package/dist/core/check.js +3 -2
  4. package/dist/core/cli.d.ts +7 -0
  5. package/dist/core/cli.js +184 -12
  6. package/dist/core/counts.d.ts +1 -1
  7. package/dist/core/counts.js +30 -12
  8. package/dist/core/data.d.ts +6 -1
  9. package/dist/core/data.js +894 -152
  10. package/dist/core/dsl.d.ts +2 -0
  11. package/dist/core/dsl.js +88 -10
  12. package/dist/core/effect-prims.d.ts +60 -0
  13. package/dist/core/effect-prims.js +625 -0
  14. package/dist/core/effect.d.ts +17 -1
  15. package/dist/core/effect.js +108 -4
  16. package/dist/core/ex/harness.exs +2 -0
  17. package/dist/core/ex/reader.exs +17 -1
  18. package/dist/core/exists.d.ts +15 -1
  19. package/dist/core/exists.js +263 -41
  20. package/dist/core/extract-c.d.ts +2 -0
  21. package/dist/core/extract-c.js +22 -13
  22. package/dist/core/extract-check.d.ts +7 -0
  23. package/dist/core/extract-check.js +57 -4
  24. package/dist/core/extract-ex.js +5 -5
  25. package/dist/core/extract-go-check.js +22 -17
  26. package/dist/core/extract-go.d.ts +3 -0
  27. package/dist/core/extract-go.js +24 -9
  28. package/dist/core/extract-jvm.js +68 -16
  29. package/dist/core/extract-print.js +3 -0
  30. package/dist/core/extract-py.js +5 -5
  31. package/dist/core/extract-rb.js +5 -5
  32. package/dist/core/extract-read.d.ts +8 -0
  33. package/dist/core/extract-read.js +55 -0
  34. package/dist/core/extract-rs.js +28 -25
  35. package/dist/core/extract-shapes.js +20 -0
  36. package/dist/core/extract-sol.js +23 -12
  37. package/dist/core/extract-step.d.ts +59 -3
  38. package/dist/core/extract-step.js +334 -64
  39. package/dist/core/extract-swift.js +22 -15
  40. package/dist/core/extract.d.ts +1 -0
  41. package/dist/core/extract.js +27 -2
  42. package/dist/core/fresh.d.ts +2 -2
  43. package/dist/core/fresh.js +7 -3
  44. package/dist/core/go/reader.go +7 -1
  45. package/dist/core/infer.js +113 -40
  46. package/dist/core/jvm/Harness.java +8 -1
  47. package/dist/core/jvm/JavaReader.java +10 -2
  48. package/dist/core/jvm/Jvm.java +18 -2
  49. package/dist/core/jvm/KtReader.java +60 -5
  50. package/dist/core/machine.d.ts +2 -0
  51. package/dist/core/machine.js +32 -6
  52. package/dist/core/py/harness.py +128 -24
  53. package/dist/core/py/reader.py +50 -5
  54. package/dist/core/rb/reader.rb +52 -5
  55. package/dist/core/rs/src/main.rs +10 -0
  56. package/dist/core/sol/Replay.sol +52 -0
  57. package/dist/core/sol/harness.py +511 -0
  58. package/dist/core/solver.js +5 -4
  59. package/dist/core/system.d.ts +62 -1
  60. package/dist/core/system.js +274 -6
  61. package/dist/core/transfer.js +17 -0
  62. package/dist/core/watch.js +1 -1
  63. package/dist/interchange/check.d.ts +18 -1
  64. package/dist/interchange/check.js +19 -12
  65. package/dist/interchange/cli.d.ts +2 -0
  66. package/dist/interchange/cli.js +45 -0
  67. package/dist/interchange/format.d.ts +5 -1
  68. package/dist/interchange/format.js +25 -9
  69. package/dist/kernel/cdcl.js +65 -14
  70. package/dist/kernel/epr.js +35 -5
  71. package/dist/kernel/kernel.d.ts +2 -0
  72. package/dist/kernel/kernel.js +67 -40
  73. package/dist/kernel/prove.d.ts +22 -0
  74. package/dist/kernel/prove.js +156 -7
  75. package/dist/kernel/reader.js +3 -2
  76. package/dist/lean-kernel/emit.d.ts +9 -3
  77. package/dist/lean-kernel/emit.js +170 -29
  78. package/dist/lean-kernel/library.js +1 -1
  79. package/package.json +12 -3
@@ -15,7 +15,7 @@ export const leanLibrary = {
15
15
  "Refine.lean": "import Ghost\n\n/-!\nWhat a refinement means, proven: compose.ts's `refinement(impl, spec, {relation, witness, stutter})` builds one machine,\nthe implementation beside the spec, the spec sent at each of the implementation's requests the request the witness\nnames, and left as it was where `stutter` holds. Lean builds that machine from the two (`SRefined.full`), the spec\ngiven in the composite's names, and checks that neither reads the other's parts (`srOk`). The machines have maps,\nflags and sets (a machine with maps and flags is one with no sets): requests carry maps, the spec's among the\nimplementation's, shared by name. With the relation proven of the composite (smachine_sound) and its step law that the\nspec stutters or allows every request it is sent (skstep_sound):\n- `srefine_lift`: every reachable state of the implementation is, outside the spec's part, a reachable state of the\n composite;\n- `srefine_project`: the spec's part of every reachable state of the composite is a reachable state of the spec;\n- `srefines`: so every reachable state of the implementation is related, by the relation, to a reachable state of the\n spec.\n-/\n\nnamespace Kern\n\nstructure Refined where\n impl : FMMachine -- the implementation, in the composite's names\n spec : FMMachine -- the spec, in the composite's names (its inputs fresh)\n wit : List (Nat × E) -- each spec input and its term over the implementation's request and both states\n stutter : C -- where the implementation's step is one the spec doesn't see\n\n-- The composite: the implementation, then the spec, its inputs read as the witness, each part kept where it stutters.\ndef Refined.full (r : Refined) : FMMachine :=\n ⟨{ inputs := r.impl.m.inputs\n init := r.impl.m.init ++ r.spec.m.init\n maps0 := r.impl.m.maps0 ++ r.spec.m.maps0\n step := r.impl.m.step ++ r.spec.m.step.map (fun p => (p.1, .ite r.stutter (.var p.1) (substE r.wit p.2)))\n writes := r.impl.m.writes ++ r.spec.m.writes.map (fun w =>\n (w.1, substE r.wit w.2.1, .ite r.stutter (.get w.1 (substE r.wit w.2.1)) (substE r.wit w.2.2)))\n pre := r.impl.m.pre\n assume := r.impl.m.assume },\n r.impl.flags ++ r.spec.flags.map (fun p => (p.1, substC r.wit p.2))⟩\n\ndef readsNone (V MS : List Nat) (c : C) : Bool := avoids V c && avoidsMaps MS c\ndef readsNoneE (V MS : List Nat) (e : E) : Bool := avoidsE V e && avoidsMapsE MS e\n\n/-! Lists and flags -/\n\ntheorem applyS_append_right {σ τ : List (Nat × E)} {i : Nat} (h : i ∉ σ.map (·.1)) (ρ : Nat → Nat) (M : Nat → Nat → Nat) :\n applyS (σ ++ τ) ρ M i = applyS τ ρ M i := by\n have hσ : lookup (fun p => p.1 == i) σ = none :=\n lookup_none (fun p hp => by simp only [beq_eq_false_iff_ne]; intro he; exact h (by rw [← he]; exact List.mem_map_of_mem hp))\n simp only [applyS, assoc, lookup_append, hσ]\n\ntheorem flagOf_append_left {fl fl' : List (Nat × C)} {i : Nat} (h : i ∉ flagVars fl') : flagOf (fl ++ fl') i = flagOf fl i := by\n have h' : lookup (fun p => p.1 == i) fl' = none :=\n lookup_none (fun p hp => by simp only [beq_eq_false_iff_ne]; intro he; exact h (by rw [← he]; exact List.mem_map_of_mem hp))\n simp only [flagOf, lookup_append]\n cases hl : lookup (fun p => p.1 == i) fl <;> simp [h']\n\ntheorem flagOf_append_right {fl fl' : List (Nat × C)} {i : Nat} (h : i ∉ flagVars fl) : flagOf (fl ++ fl') i = flagOf fl' i := by\n have h' : lookup (fun p => p.1 == i) fl = none :=\n lookup_none (fun p hp => by simp only [beq_eq_false_iff_ne]; intro he; exact h (by rw [← he]; exact List.mem_map_of_mem hp))\n simp only [flagOf, lookup_append, h']\n\ntheorem flagOf_map {fl : List (Nat × C)} {g : C → C} (i : Nat) : flagOf (fl.map (fun p => (p.1, g p.2))) i = (flagOf fl i).map g := by\n simp only [flagOf]\n induction fl with\n | nil => rfl\n | cons a as ih =>\n simp only [List.map_cons, lookup]\n split <;> simp_all\n\ntheorem decideFlags_of (fl : List (Nat × C)) (X : Nat → Nat) (M : Nat → Nat → Nat) (i : Nat) :\n decideFlags fl X M i = match flagOf fl i with\n | some c => if evalC X M c = true then 1 else 0\n | none => X i := by\n simp only [decideFlags, applyS, assoc_flagSub]\n cases flagOf fl i <;> simp [evalE]\n\ntheorem rn_v {V MS : List Nat} {c : C} (h : readsNone V MS c = true) : avoids V c = true := by simp only [readsNone, Bool.and_eq_true] at h; exact h.1\ntheorem rn_m {V MS : List Nat} {c : C} (h : readsNone V MS c = true) : avoidsMaps MS c = true := by simp only [readsNone, Bool.and_eq_true] at h; exact h.2\ntheorem rnE_v {V MS : List Nat} {e : E} (h : readsNoneE V MS e = true) : avoidsE V e = true := by simp only [readsNoneE, Bool.and_eq_true] at h; exact h.1\ntheorem rnE_m {V MS : List Nat} {e : E} (h : readsNoneE V MS e = true) : avoidsMapsE MS e = true := by simp only [readsNoneE, Bool.and_eq_true] at h; exact h.2\n\ntheorem assoc_map_fst (l : List (Nat × E)) (g : Nat × E → E) (v : Nat) :\n assoc (l.map (fun q => (q.1, g q))) v = (lookup (fun q => q.1 == v) l).map g := by\n simp only [assoc, lookup_map_pred, Option.map_map]; rfl\n\ntheorem lookup_writes_map (l : List (Nat × E × E)) (g : Nat × E × E → E × E) (mp : Nat) :\n lookup (fun w : Nat × E × E => w.1 == mp) (l.map (fun w => (w.1, g w))) = (lookup (fun w : Nat × E × E => w.1 == mp) l).map (fun w => (w.1, g w)) := by\n rw [lookup_map_pred]\n\n-- Read where `then` reads, a condition that reads no primed name is the condition before the step.\ntheorem unprimed_evalC {pr pm : List (Nat × Nat)} {c : C} (hv : avoids (pr.map (·.2)) c = true) (hm : avoidsMaps (pm.map (·.2)) c = true)\n (ρ ρ' : Nat → Nat) (M M' : Nat → Nat → Nat) : evalC (primed pr ρ' ρ) (primedMaps pm M' M) c = evalC ρ M c := by\n rw [congrC _ ρ _ c (fun i hi => by\n simp only [primed]\n rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact avoids_occ hv hi (by rw [← he]; exact List.mem_map_of_mem hq))])]\n exact congrMC ρ _ M c (fun mp hmp => by\n funext k; simp only [primedMaps]\n rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact avoidsMaps_occ hm hmp (by rw [← he]; exact List.mem_map_of_mem hq))])\n\n\n/-! Refinements -/\n\nopen Classical\n\nstructure SRefined where\n impl : SMachine\n spec : SMachine\n wit : List (Nat × E)\n stutter : C\n\n\ndef SRefined.base (r : SRefined) : Refined := ⟨r.impl.f, r.spec.f, r.wit, r.stutter⟩\ndef SRefined.full (r : SRefined) : SMachine :=\n ⟨r.base.full, r.impl.carried,\n r.impl.every ++ r.spec.every.map (fun w => (w.1, w.2.1, E.ite r.stutter (.get w.1 (.var w.2.1)) (substE r.wit w.2.2))),\n r.impl.qflags ++ r.spec.qflags.map (fun q => (q.1, q.2.1, substC r.wit q.2.2)),\n r.impl.wits ++ r.spec.wits, [], []⟩\ndef SRefined.allowed (r : SRefined) : C := inlineC r.full.f.flags (.or r.stutter (substC r.wit r.spec.f.m.assume))\n\ntheorem sfull_inputs (r : SRefined) : r.full.f.m.inputs = r.impl.f.m.inputs := rfl\ntheorem sfull_carried (r : SRefined) : r.full.carried = r.impl.carried := rfl\ntheorem sfull_flags (r : SRefined) : r.full.f.flags = r.impl.f.flags ++ r.spec.f.flags.map (fun p => (p.1, substC r.wit p.2)) := rfl\ntheorem sfull_qflags (r : SRefined) : r.full.qflags = r.impl.qflags ++ r.spec.qflags.map (fun q => (q.1, q.2.1, substC r.wit q.2.2)) := rfl\ntheorem sfull_every (r : SRefined) : r.full.every = r.impl.every ++ r.spec.every.map (fun w => (w.1, w.2.1, E.ite r.stutter (.get w.1 (.var w.2.1)) (substE r.wit w.2.2))) := rfl\ntheorem sfull_step (r : SRefined) : r.full.f.m.step = r.impl.f.m.step ++ r.spec.f.m.step.map (fun p => (p.1, .ite r.stutter (.var p.1) (substE r.wit p.2))) := rfl\ntheorem sfull_writes (r : SRefined) : r.full.f.m.writes = r.impl.f.m.writes ++ r.spec.f.m.writes.map (fun w =>\n (w.1, substE r.wit w.2.1, .ite r.stutter (.get w.1 (substE r.wit w.2.1)) (substE r.wit w.2.2))) := rfl\ntheorem sfull_init (r : SRefined) : r.full.f.m.init = r.impl.f.m.init ++ r.spec.f.m.init := rfl\ntheorem sfull_maps0 (r : SRefined) : r.full.f.m.maps0 = r.impl.f.m.maps0 ++ r.spec.f.m.maps0 := rfl\ntheorem sfull_assume (r : SRefined) : r.full.f.m.assume = r.impl.f.m.assume := rfl\ntheorem sfull_pre (r : SRefined) : r.full.f.m.pre = r.impl.f.m.pre := rfl\n\n-- The implementation's names and maps (its own, and those only it carries); the spec's; where the spec's state and the\n-- composite's may differ; what the witness and `stutter` may not read.\ndef SRefined.IV (r : SRefined) : List Nat := r.impl.f.m.inputs ++ r.impl.f.m.init.map (·.1) ++ r.impl.f.m.step.map (·.1) ++ flagVars r.impl.f.flags ++ r.impl.qVars ++ bkeys r.impl\ndef SRefined.IO (r : SRefined) : List Nat := r.impl.f.m.maps0.map (·.1) ++ r.impl.f.m.writes.map (·.1) ++ r.impl.every.map (·.1)\ndef SRefined.IM (r : SRefined) : List Nat := r.IO ++ r.impl.carried.filter (fun mp => !r.spec.carried.contains mp)\ndef SRefined.SV (r : SRefined) : List Nat := r.spec.f.m.init.map (·.1) ++ r.spec.f.m.step.map (·.1) ++ flagVars r.spec.f.flags ++ r.spec.qVars ++ bkeys r.spec\ndef SRefined.SM (r : SRefined) : List Nat := r.spec.f.m.maps0.map (·.1) ++ r.spec.f.m.writes.map (·.1) ++ r.spec.every.map (·.1)\ndef SRefined.X (r : SRefined) : List Nat := r.IV ++ flagVars r.spec.f.flags ++ r.spec.qVars ++ r.spec.f.m.inputs\ndef SRefined.W (r : SRefined) : List Nat := flagVars r.impl.f.flags ++ flagVars r.spec.f.flags ++ r.spec.f.m.inputs ++ r.impl.qVars ++ r.spec.qVars ++ bkeys r.spec\n\ndef srImplOk (r : SRefined) : Bool :=\n readsNone r.SV r.SM r.impl.f.m.pre && readsNone r.SV r.SM r.impl.f.m.assume\n && r.impl.f.flags.all (fun p => readsNone r.SV r.SM p.2) && r.impl.f.m.step.all (fun p => readsNoneE r.SV r.SM p.2)\n && r.impl.f.m.init.all (fun p => readsNoneE r.SV r.SM p.2)\n && r.impl.f.m.writes.all (fun w => readsNoneE r.SV r.SM w.2.1 && readsNoneE r.SV r.SM w.2.2)\n && r.impl.every.all (fun w => readsNoneE r.SV r.SM w.2.2) && r.impl.qflags.all (fun q => readsNone r.SV r.SM q.2.2)\ndef srSpecOk (r : SRefined) : Bool :=\n readsNone r.IV r.IM r.spec.f.m.assume && r.spec.f.flags.all (fun p => readsNone (r.IV ++ flagVars r.spec.f.flags) r.IM p.2)\n && r.spec.f.m.step.all (fun p => readsNoneE r.IV r.IM p.2) && r.spec.f.m.init.all (fun p => readsNoneE (r.IV ++ r.spec.f.m.inputs) r.IM p.2)\n && r.spec.f.m.writes.all (fun w => readsNoneE r.IV r.IM w.2.1 && readsNoneE r.IV r.IM w.2.2)\n && r.spec.every.all (fun w => readsNoneE r.IV r.IM w.2.2)\n && r.spec.qflags.all (fun q => readsNone (r.IV ++ flagVars r.spec.f.flags ++ r.spec.qVars) r.IM q.2.2)\n && readsNone (r.X ++ r.SV) r.IM r.spec.f.m.pre && !readsC r.spec.f.m.pre\ndef srNamesOk (r : SRefined) : Bool :=\n r.SV.all (fun v => !r.IV.contains v) && r.spec.f.m.inputs.all (fun v => !(r.IV ++ r.SV).contains v)\n && r.SM.all (fun mp => !r.IM.contains mp) && r.spec.carried.all (fun mp => r.impl.carried.contains mp)\n && r.impl.carried.all (fun mp => !(r.IO ++ r.SM).contains mp)\n && r.wit.all (fun p => r.spec.f.m.inputs.contains p.1 && avoidsE r.W p.2) && avoids r.W r.stutter\n && r.impl.facts.isEmpty && r.spec.facts.isEmpty\ndef srOk (r : SRefined) : Bool := srImplOk r && srSpecOk r && (srNamesOk r && r.spec.pfacts.isEmpty)\n\ntheorem srImpl_parts {r : SRefined} (h : srImplOk r = true) :\n readsNone r.SV r.SM r.impl.f.m.pre = true ∧ readsNone r.SV r.SM r.impl.f.m.assume = true\n ∧ (∀ p ∈ r.impl.f.flags, readsNone r.SV r.SM p.2 = true) ∧ (∀ p ∈ r.impl.f.m.step, readsNoneE r.SV r.SM p.2 = true)\n ∧ (∀ p ∈ r.impl.f.m.init, readsNoneE r.SV r.SM p.2 = true)\n ∧ (∀ w ∈ r.impl.f.m.writes, readsNoneE r.SV r.SM w.2.1 = true ∧ readsNoneE r.SV r.SM w.2.2 = true)\n ∧ (∀ w ∈ r.impl.every, readsNoneE r.SV r.SM w.2.2 = true) ∧ (∀ q ∈ r.impl.qflags, readsNone r.SV r.SM q.2.2 = true) := by\n simp only [srImplOk, Bool.and_eq_true, List.all_eq_true] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩, h7⟩, h8⟩ := h\n exact ⟨h1, h2, h3, h4, h5, h6, h7, h8⟩\n\ntheorem srSpec_parts {r : SRefined} (h : srSpecOk r = true) :\n readsNone r.IV r.IM r.spec.f.m.assume = true ∧ (∀ p ∈ r.spec.f.flags, readsNone (r.IV ++ flagVars r.spec.f.flags) r.IM p.2 = true)\n ∧ (∀ p ∈ r.spec.f.m.step, readsNoneE r.IV r.IM p.2 = true) ∧ (∀ p ∈ r.spec.f.m.init, readsNoneE (r.IV ++ r.spec.f.m.inputs) r.IM p.2 = true)\n ∧ (∀ w ∈ r.spec.f.m.writes, readsNoneE r.IV r.IM w.2.1 = true ∧ readsNoneE r.IV r.IM w.2.2 = true)\n ∧ (∀ w ∈ r.spec.every, readsNoneE r.IV r.IM w.2.2 = true)\n ∧ (∀ q ∈ r.spec.qflags, readsNone (r.IV ++ flagVars r.spec.f.flags ++ r.spec.qVars) r.IM q.2.2 = true)\n ∧ readsNone (r.X ++ r.SV) r.IM r.spec.f.m.pre = true ∧ readsC r.spec.f.m.pre = false := by\n simp only [srSpecOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩, h7⟩, h8⟩, h9⟩ := h\n exact ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9⟩\n\ntheorem srNames_parts {r : SRefined} (h : srNamesOk r = true) :\n (∀ v ∈ r.SV, v ∉ r.IV) ∧ (∀ v ∈ r.spec.f.m.inputs, v ∉ r.IV ++ r.SV) ∧ (∀ mp ∈ r.SM, mp ∉ r.IM)\n ∧ (∀ mp ∈ r.spec.carried, mp ∈ r.impl.carried) ∧ (∀ mp ∈ r.impl.carried, mp ∉ r.IO ++ r.SM)\n ∧ (∀ p ∈ r.wit, p.1 ∈ r.spec.f.m.inputs ∧ avoidsE r.W p.2 = true) ∧ avoids r.W r.stutter = true\n ∧ r.impl.facts = [] ∧ r.spec.facts = [] := by\n simp only [srNamesOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true', List.isEmpty_iff] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩, h7⟩, h8⟩, h9⟩ := h\n refine ⟨fun v hv hm => ?_, fun v hv hm => ?_, fun mp hm hm' => ?_, fun mp hm => List.contains_iff_mem.mp (h4 mp hm), fun mp hm hm' => ?_,\n fun p hp => ⟨List.contains_iff_mem.mp (h6 p hp).1, (h6 p hp).2⟩, h7, h8, h9⟩\n · have := h1 v hv; rw [List.contains_iff_mem.mpr hm] at this; cases this\n · have := h2 v hv; rw [List.contains_iff_mem.mpr hm] at this; cases this\n · have := h3 mp hm; rw [List.contains_iff_mem.mpr hm'] at this; cases this\n · have := h5 mp hm; rw [List.contains_iff_mem.mpr hm'] at this; cases this\n\n/-! The implementation's runs, inside the composite's -/\n\nsection slift\nvariable (r : SRefined) (hok : srOk r = true)\ninclude hok\n\ntheorem sr_impl : srImplOk r = true := by simp only [srOk, Bool.and_eq_true] at hok; exact hok.1.1\ntheorem sr_spec : srSpecOk r = true := by simp only [srOk, Bool.and_eq_true] at hok; exact hok.1.2\ntheorem sr_names : srNamesOk r = true := by simp only [srOk, Bool.and_eq_true] at hok; exact hok.2.1\ntheorem sr_pfacts : r.spec.pfacts = [] := by simp only [srOk, Bool.and_eq_true, List.isEmpty_iff] at hok; exact hok.2.2\n\n-- One step of the composite from a state that agrees with the implementation's outside the spec's part, with the same\n-- request: allowed alike, to states that still agree.\ntheorem srefine_step {ρ ρc : Nat → Nat} {M Mc : Nat → Nat → Nat} (hag : Agree r.SV ρc ρ) (hmag : ∀ mp, mp ∉ r.SM → Mc mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n evalC (sView r.full ρc Mc ι κ).1 (sView r.full ρc Mc ι κ).2 r.full.f.m.assume = evalC (sView r.impl ρ M ι κ).1 (sView r.impl ρ M ι κ).2 r.impl.f.m.assume\n ∧ Agree r.SV (sNext r.full ρc Mc ι κ).1 (sNext r.impl ρ M ι κ).1\n ∧ ∀ mp, mp ∉ r.SM → (sNext r.full ρc Mc ι κ).2 mp = (sNext r.impl ρ M ι κ).2 mp := by\n obtain ⟨_, ha, hf, hs, _, hw, he, hq⟩ := srImpl_parts (sr_impl r hok)\n have hN : ∀ mp, mp ∉ r.SM → withCarried r.full Mc κ mp = withCarried r.impl M κ mp := fun mp hmp => by\n show (if r.impl.carried.contains mp = true then κ mp else Mc mp) = (if r.impl.carried.contains mp = true then κ mp else M mp)\n by_cases hc : r.impl.carried.contains mp = true\n · rw [if_pos hc, if_pos hc]\n · rw [if_neg hc, if_neg hc]; exact hmag mp hmp\n have hX : Agree r.SV (withInputs r.full.f.m.inputs ρc ι) (withInputs r.impl.f.m.inputs ρ ι) := fun i hi => by\n rw [sfull_inputs]; simp only [withInputs]; split\n · rfl\n · exact hag i hi\n -- The quantified flags: the composite's decide the implementation's alike, outside the spec's names.\n have hQ : Agree r.SV (decideQ r.full (withInputs r.full.f.m.inputs ρc ι) (withCarried r.full Mc κ))\n (decideQ r.impl (withInputs r.impl.f.m.inputs ρ ι) (withCarried r.impl M κ)) := by\n intro v hv\n rw [decideQ_of, decideQ_of, sfull_qflags, lookup_append]\n cases hl : lookup (fun q => q.1 == v) r.impl.qflags with\n | some q =>\n have hrd := hq q (lookup_some hl).1\n exact ite_all_congr (fun i hi _ => hX i (avoids_occ (rn_v hrd) hi)) (fun mp hmp => hN mp (avoidsMaps_occ (rn_m hrd) hmp))\n | none =>\n have hn : lookup (fun q => q.1 == v) (r.spec.qflags.map (fun q => (q.1, q.2.1, substC r.wit q.2.2))) = none :=\n lookup_none (fun q hq' => by\n simp only [beq_eq_false_iff_ne]; intro hqv\n obtain ⟨q0, hq0, rfl⟩ := List.mem_map.mp hq'\n exact hv (by simp only [SRefined.SV, SMachine.qVars, List.mem_append]; left; right; rw [← hqv]; exact List.mem_map.mpr ⟨q0, hq0, rfl⟩))\n simp only [hn]\n exact hX v hv\n -- The plain flags, likewise.\n have hD : Agree r.SV (decideFlags r.full.f.flags (decideQ r.full (withInputs r.full.f.m.inputs ρc ι) (withCarried r.full Mc κ)) (withCarried r.full Mc κ))\n (decideFlags r.impl.f.flags (decideQ r.impl (withInputs r.impl.f.m.inputs ρ ι) (withCarried r.impl M κ)) (withCarried r.impl M κ)) := by\n intro i hi\n have hif : i ∉ flagVars (r.spec.f.flags.map (fun p => (p.1, substC r.wit p.2))) := fun h' => hi (by\n simp only [flagVars, List.map_map] at h'; simp only [SRefined.SV, List.mem_append]; left; left; right\n simpa [flagVars, Function.comp_def] using h')\n rw [sfull_flags, decideFlags_of, decideFlags_of, flagOf_append_left hif]\n cases hfo : flagOf r.impl.f.flags i with\n | none => exact hQ i hi\n | some c =>\n obtain ⟨p, hp, rfl⟩ := flagOf_some hfo\n simp only\n rw [congrMC _ _ _ _ (fun mp hmp => hN mp (avoidsMaps_occ (rn_m (hf p hp)) hmp)),\n congrC _ _ _ _ (fun j hj => hQ j (avoids_occ (rn_v (hf p hp)) hj))]\n refine ⟨?_, fun i hi => ?_, fun mp hmp => ?_⟩\n · simp only [sView]\n rw [sfull_assume, congrMC _ _ _ _ (fun mp hmp => hN mp (avoidsMaps_occ (rn_m ha) hmp)), congrC _ _ _ _ (fun j hj => hD j (avoids_occ (rn_v ha) hj))]\n · simp only [sNext, sView]\n rw [sfull_step]\n have hit : i ∉ (r.spec.f.m.step.map (fun p => (p.1, E.ite r.stutter (.var p.1) (substE r.wit p.2)))).map (·.1) := fun h' => hi (by\n simp only [List.map_map] at h'; simp only [SRefined.SV, List.mem_append]; left; left; left; right; simpa [Function.comp_def] using h')\n rw [applyS_append_other hit]\n simp only [applyS]\n cases hst : assoc r.impl.f.m.step i with\n | none => exact hD i hi\n | some e =>\n have hm : (i, e) ∈ r.impl.f.m.step := by\n simp only [assoc, Option.map_eq_some_iff] at hst\n obtain ⟨q, hq', rfl⟩ := hst\n obtain ⟨hqm, hqv⟩ := lookup_some hq'\n simp only [beq_iff_eq] at hqv; rw [← hqv]; exact hqm\n dsimp only\n rw [congrME _ _ _ e (fun mp hmp => hN mp (avoidsMapsE_occ (rnE_m (hs (i, e) hm)) hmp)),\n congrE _ _ _ e (fun j hj => hD j (avoidsE_occ (rnE_v (hs (i, e) hm)) hj))]\n · funext key\n simp only [sNext, sView, everyMaps]\n rw [sfull_every, lookup_append]\n cases hl : lookup (fun w => w.1 == mp) r.impl.every with\n | some w =>\n have hrd := he w (lookup_some hl).1\n simp only\n rw [congrME _ _ _ w.2.2 (fun mp' h => hN mp' (avoidsMapsE_occ (rnE_m hrd) h)),\n congrE _ _ _ w.2.2 (fun j hj => upd_agree hD w.2.1 key j (avoidsE_occ (rnE_v hrd) hj))]\n | none =>\n have hn : lookup (fun w => w.1 == mp) (r.spec.every.map (fun w => (w.1, w.2.1, E.ite r.stutter (.get w.1 (.var w.2.1)) (substE r.wit w.2.2)))) = none :=\n lookup_none (fun w hw' => by\n simp only [beq_eq_false_iff_ne]; intro he'\n obtain ⟨w0, hw0, rfl⟩ := List.mem_map.mp hw'\n exact hmp (by simp only [SRefined.SM, List.mem_append]; right; rw [← he']; exact List.mem_map.mpr ⟨w0, hw0, rfl⟩))\n simp only [hn]\n have hwm : lookup (fun w : Nat × E × E => w.1 == mp) (r.spec.f.m.writes.map (fun w =>\n (w.1, substE r.wit w.2.1, E.ite r.stutter (.get w.1 (substE r.wit w.2.1)) (substE r.wit w.2.2)))) = none :=\n lookup_none (fun w hw' => by\n simp only [beq_eq_false_iff_ne]; intro he'\n obtain ⟨w0, hw0, rfl⟩ := List.mem_map.mp hw'\n exact hmp (by simp only [SRefined.SM, List.mem_append]; left; right; rw [← he']; exact List.mem_map.mpr ⟨w0, hw0, rfl⟩))\n simp only [writeMaps]\n rw [sfull_writes, lookup_append]\n cases hl2 : lookup (fun w => w.1 == mp) r.impl.f.m.writes with\n | none => simp only [hwm]; exact congrFun (hN mp hmp) key\n | some w =>\n obtain ⟨h1, h2⟩ := hw w (lookup_some hl2).1\n simp only\n rw [congrME _ _ _ w.2.1 (fun mp' h => hN mp' (avoidsMapsE_occ (rnE_m h1) h)), congrE _ _ _ w.2.1 (fun j hj => hD j (avoidsE_occ (rnE_v h1) hj)),\n congrME _ _ _ w.2.2 (fun mp' h => hN mp' (avoidsMapsE_occ (rnE_m h2) h)), congrE _ _ _ w.2.2 (fun j hj => hD j (avoidsE_occ (rnE_v h2) hj)), hN mp hmp]\n\n/-- Every reachable state of the implementation is, outside the spec's part, a reachable state of the composite. -/\ntheorem srefine_lift (hmo : distinct (r.spec.f.m.maps0.map (·.1)) = true) :\n ∀ {ρ M}, SReach r.impl ρ M → ∃ ρc Mc, SReach r.full ρc Mc ∧ Agree r.SV ρc ρ ∧ ∀ mp, mp ∉ r.SM → Mc mp = M mp := by\n obtain ⟨hp, _, _, _, hi, _⟩ := srImpl_parts (sr_impl r hok)\n obtain ⟨_, _, hsm, _⟩ := srNames_parts (sr_names r hok)\n intro ρ M hr\n induction hr with\n | start ρ M hM h0 _ _ =>\n let Mc : Nat → Nat → Nat := fun mp => match lookup (fun p => p.1 == mp) r.spec.f.m.maps0 with\n | some p => fun _ => p.2\n | none => M mp\n have hMc : ∀ mp, mp ∉ r.SM → Mc mp = M mp := fun mp hmp => by\n simp only [Mc]\n rw [lookup_none (fun p hp' => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact hmp (by simp only [SRefined.SM, List.mem_append]; left; left; rw [← he]; exact List.mem_map_of_mem hp'))]\n have hpre : evalC ρ Mc r.full.f.m.pre = true := by\n rw [sfull_pre, congrMC _ Mc M _ (fun mp hmp => hMc mp (avoidsMaps_occ (rn_m hp) hmp))]; exact h0\n have hstart : StartMaps r.full.f.m Mc := by\n intro p hp' key\n rw [sfull_maps0] at hp'\n rcases List.mem_append.mp hp' with hp' | hp'\n · have hn : p.1 ∉ r.SM := fun h' => hsm p.1 h' (by\n simp only [SRefined.IM, SRefined.IO, List.mem_append]; left; left; left; exact List.mem_map_of_mem hp')\n rw [hMc p.1 hn]; exact hM p hp' key\n · have hl : lookup (fun q => q.1 == p.1) r.spec.f.m.maps0 = some p := lookup_unique1 hmo hp'\n simp only [Mc, hl]\n have hfacts : ∀ l ∈ r.full.facts, l.Holds ρ Mc := fun l hl => by simp [SRefined.full] at hl\n refine ⟨_, Mc, SReach.start ρ Mc hstart hpre hfacts (fun p hp => by simp [SRefined.full] at hp), fun i hi' => ?_, hMc⟩\n show applyS (r.impl.f.m.init ++ r.spec.f.m.init) ρ Mc i = applyS r.impl.f.m.init ρ M i\n rw [applyS_append_other (by\n intro h'; apply hi'; simp only [SRefined.SV, List.mem_append]; left; left; left; left; exact h')]\n simp only [applyS]\n cases hst : assoc r.impl.f.m.init i with\n | none => rfl\n | some e =>\n have hm : (i, e) ∈ r.impl.f.m.init := by\n simp only [assoc, Option.map_eq_some_iff] at hst\n obtain ⟨q, hq, rfl⟩ := hst\n obtain ⟨hqm, hqv⟩ := lookup_some hq\n simp only [beq_iff_eq] at hqv; rw [← hqv]; exact hqm\n exact congrME _ Mc M e (fun mp hmp => hMc mp (avoidsMapsE_occ (rnE_m (hi (i, e) hm)) hmp))\n | step ρ M ι κ _ ha ih =>\n obtain ⟨ρc, Mc, hrc, hag, hmag⟩ := ih\n obtain ⟨hA, hS, hW⟩ := srefine_step r hok hag hmag ι κ\n exact ⟨_, _, SReach.step ρc Mc ι κ hrc (by rw [hA]; exact ha), hS, hW⟩\nend slift\n\n/-! The spec's part of the composite's runs -/\n\ntheorem lookup_qmap (l : List (Nat × List Nat × C)) (g : Nat × List Nat × C → C) (v : Nat) :\n lookup (fun q : Nat × List Nat × C => q.1 == v) (l.map (fun q => (q.1, q.2.1, g q))) = (lookup (fun q : Nat × List Nat × C => q.1 == v) l).map (fun q => (q.1, q.2.1, g q)) := by\n rw [lookup_map_pred]\ntheorem lookup_emap (l : List (Nat × Nat × E)) (g : Nat × Nat × E → E) (v : Nat) :\n lookup (fun w : Nat × Nat × E => w.1 == v) (l.map (fun w => (w.1, w.2.1, g w))) = (lookup (fun w : Nat × Nat × E => w.1 == v) l).map (fun w => (w.1, w.2.1, g w)) := by\n rw [lookup_map_pred]\n\ntheorem sView_snd (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n (sView s ρ M ι κ).2 = withCarried s M κ := rfl\n\nsection sproject\nvariable (r : SRefined) (hok : srOk r = true)\ninclude hok\n\n-- The maps the spec reads: alike in its view and the composite's.\ntheorem sr_maps {M Ms : Nat → Nat → Nat} (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp) (κ : Nat → Nat → Nat) :\n ∀ mp, mp ∉ r.IM → withCarried r.spec Ms κ mp = withCarried r.full M κ mp := by\n obtain ⟨_, _, _, hcar, _⟩ := srNames_parts (sr_names r hok)\n intro mp hmp\n show (if r.spec.carried.contains mp = true then κ mp else Ms mp) = (if r.impl.carried.contains mp = true then κ mp else M mp)\n by_cases hs : r.spec.carried.contains mp = true\n · rw [if_pos hs, if_pos (List.contains_iff_mem.mpr (hcar mp (List.contains_iff_mem.mp hs)))]\n · have hi : ¬ r.impl.carried.contains mp = true := fun hc => hmp (by\n simp only [SRefined.IM, List.mem_append, List.mem_filter]; right; exact ⟨List.contains_iff_mem.mp hc, by simpa using hs⟩)\n rw [if_neg hs, if_neg hi]\n exact hmag mp hmp (fun h' => hs (List.contains_iff_mem.mpr h'))\n\ntheorem sr_W {v : Nat} (hv : v ∈ flagVars r.full.f.flags ∨ v ∈ r.full.qVars) : v ∈ r.W := by\n simp only [sfull_flags, flagVars, List.map_append, List.map_map, SMachine.qVars, sfull_qflags, List.mem_append] at hv\n simp only [SRefined.W, SMachine.qVars, flagVars, List.mem_append]\n rcases hv with (h | h) | (h | h)\n · left; left; left; left; left; exact h\n · left; left; left; left; right; simpa [Function.comp_def] using h\n · left; left; right; exact h\n · left; right; simpa [Function.comp_def] using h\n\n-- A term the witness has, read in the composite's view, before its flags or with a key set: alike.\ntheorem sr_witE {ρ : Nat → Nat} {M : Nat → Nat → Nat} (ι : Nat → Nat) (κ : Nat → Nat → Nat) {e : E} (he : avoidsE r.W e = true) :\n evalE (decideFlags r.full.f.flags (decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ)) (withCarried r.full M κ)) (withCarried r.full M κ) e\n = evalE (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ) e := by\n apply congrE; intro j hj\n have hjW := avoidsE_occ he hj\n rw [decide_other (fun h' => hjW (sr_W r hok (Or.inl h'))), decideQ_other (fun h' => hjW (sr_W r hok (Or.inr h')))]\n\ntheorem sr_wit_upd {X : Nat → Nat} {N : Nat → Nat → Nat} {e : E} (he : avoidsE r.W e = true) {k : Nat} (hk : k ∈ bkeys r.spec) (key : Nat) :\n evalE (upd X k key) N e = evalE X N e := by\n apply congrE; intro j hj\n simp only [upd]; split\n · rename_i h; subst h; exact absurd (by simp only [SRefined.W, List.mem_append]; right; exact hk) (avoidsE_occ he hj)\n · rfl\n\ntheorem sr_wit_atKeys {X : Nat → Nat} {N : Nat → Nat → Nat} {e : E} (he : avoidsE r.W e = true) {ks : List Nat} (hk : ∀ k ∈ ks, k ∈ bkeys r.spec) (v : Nat → Nat) :\n evalE (atKeys X ks v) N e = evalE X N e := by\n apply congrE; intro j hj\n by_cases hjk : j ∈ ks\n · exact absurd (by simp only [SRefined.W, List.mem_append]; right; exact hk j hjk) (avoidsE_occ he hj)\n · exact atKeys_out hjk\n\n-- The witness's domain: spec inputs.\ntheorem sr_wdom {i : Nat} (hi : i ∉ r.spec.f.m.inputs) : i ∉ r.wit.map (·.1) := fun h' => by\n obtain ⟨_, _, _, _, _, hwit, _⟩ := srNames_parts (sr_names r hok)\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h'; exact hi (hwit p hp).1\n\ntheorem sr_wit_some {i : Nat} {e : E} (h : assoc r.wit i = some e) : (i, e) ∈ r.wit := by\n simp only [assoc, Option.map_eq_some_iff] at h\n obtain ⟨q, hq, rfl⟩ := h\n obtain ⟨hqm, hqv⟩ := lookup_some hq\n simp only [beq_iff_eq] at hqv; rw [← hqv]; exact hqm\n\n-- The spec's quantified flags, decided over its view (its inputs as the witness reads them), agree with the composite's\n-- with the witness put in, outside the implementation's names and the spec's plain flags.\ntheorem sr_q {ρ σ : Nat → Nat} {M Ms : Nat → Nat → Nat} (hag : Agree r.X σ ρ) (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n Agree (r.IV ++ flagVars r.spec.f.flags)\n (decideQ r.spec (withInputs r.spec.f.m.inputs σ (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ))) (withCarried r.spec Ms κ))\n (applyS r.wit (decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ)) (withCarried r.full M κ)) := by\n obtain ⟨_, _, _, _, _, _, hsq, _, _⟩ := srSpec_parts (sr_spec r hok)\n obtain ⟨hsv, hsi, _, _, _, hwit, _, _, _⟩ := srNames_parts (sr_names r hok)\n have hN := sr_maps r hok hmag κ\n intro v hv\n have hvI : v ∉ r.IV := fun h' => hv (List.mem_append_left _ h')\n have hvF : v ∉ flagVars r.spec.f.flags := fun h' => hv (List.mem_append_right _ h')\n -- Names of the spec's part are none of the spec's inputs.\n have hSI : ∀ i, i ∈ r.SV → i ∉ r.spec.f.m.inputs := fun i hi h' => hsi i h' (List.mem_append_right _ hi)\n have hIVq : ∀ i, i ∉ r.IV → i ∉ r.impl.qVars := fun i hi h' => hi (by simp only [SRefined.IV, List.mem_append]; left; right; exact h')\n have hIVi : ∀ i, i ∉ r.IV → i ∉ r.impl.f.m.inputs := fun i hi h' => hi (by simp only [SRefined.IV, List.mem_append]; left; left; left; left; left; exact h')\n have hIVf : ∀ i, i ∉ r.IV → i ∉ flagVars r.impl.f.flags := fun i hi h' => hi (by simp only [SRefined.IV, List.mem_append]; left; left; right; exact h')\n have hfull_q : ∀ i, i ∉ r.impl.qVars → i ∉ r.spec.qVars → i ∉ r.full.qVars := fun i h1 h2 h' => by\n simp only [SMachine.qVars, sfull_qflags, List.map_append, List.map_map, List.mem_append] at h'\n rcases h' with h' | h'\n · exact h1 h'\n · exact h2 (by simpa [SMachine.qVars, Function.comp_def] using h')\n have hfull_f : ∀ i, i ∉ flagVars r.impl.f.flags → i ∉ flagVars r.spec.f.flags → i ∉ flagVars r.full.f.flags := fun i h1 h2 h' => by\n simp only [sfull_flags, flagVars, List.map_append, List.map_map, List.mem_append] at h'\n rcases h' with h' | h'\n · exact h1 h'\n · exact h2 (by simpa [flagVars, Function.comp_def] using h')\n -- The spec inputs as the witness reads them: in the composite's view, or before its flags; the same.\n have hιs : ∀ i, i ∈ r.spec.f.m.inputs → applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ) i\n = applyS r.wit (decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ)) (withCarried r.full M κ) i := fun i hi => by\n have hiI : i ∉ r.IV := fun h' => hsi i hi (List.mem_append_left _ h')\n have hiS : i ∉ r.SV := fun h' => hsi i hi (List.mem_append_right _ h')\n have hiF : i ∉ flagVars r.full.f.flags := hfull_f i (hIVf i hiI) (fun h' => hiS (by simp only [SRefined.SV, List.mem_append]; left; left; right; exact h'))\n simp only [applyS, sView]\n cases ha : assoc r.wit i with\n | none => exact decide_other hiF _ _\n | some e =>\n have hm := sr_wit_some r hok ha\n dsimp only\n rw [sr_witE r hok ι κ (hwit _ hm).2]\n exact (congrE _ _ _ e (fun j hj => decideQ_other (fun h' => avoidsE_occ (hwit _ hm).2 hj (sr_W r hok (Or.inr h'))) _ _)).symm\n by_cases hvq : v ∈ r.spec.qVars\n · -- A spec's quantified flag: its body over the spec's view; in the composite, with the witness put in.\n have hvi : v ∉ r.spec.f.m.inputs := hSI v (by simp only [SRefined.SV, List.mem_append]; left; right; exact hvq)\n rw [decideQ_of, applyS_other (sr_wdom r hok hvi), decideQ_of, sfull_qflags, lookup_append]\n have hn : lookup (fun q => q.1 == v) r.impl.qflags = none := lookup_none (fun q hq' => by\n simp only [beq_eq_false_iff_ne]; intro he; exact hIVq v hvI (by rw [← he]; exact List.mem_map_of_mem hq'))\n simp only [hn, lookup_qmap]\n cases hl : lookup (fun q => q.1 == v) r.spec.qflags with\n | none => exact absurd hvq (fun h' => by obtain ⟨q, hq, hqv⟩ := List.mem_map.mp h'; have := lookup_none_all hl q hq; simp [hqv] at this)\n | some q =>\n have hqm := (lookup_some hl).1\n have hrd := hsq q hqm\n have hk : ∀ k ∈ q.2.1, k ∈ bkeys r.spec := fun k hk' => by simp only [bkeys, List.mem_append]; right; exact List.mem_flatMap.mpr ⟨q, hqm, hk'⟩\n simp only [Option.map_some]\n have hc : (∀ k, holdsC (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ) (substC r.wit q.2.2))\n ↔ (∀ k, holdsC (applyS r.wit (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ)) (withCarried r.full M κ) q.2.2) := by\n apply forall_congr'; intro k; rw [← evalC_iff, ← evalC_iff, subst_evalC]\n rw [show (if ∀ k, holdsC (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ) (substC r.wit q.2.2) then 1 else 0)\n = (if ∀ k, holdsC (applyS r.wit (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ)) (withCarried r.full M κ) q.2.2 then 1 else 0) by\n simp only [hc]]\n have hkdom : ∀ k ∈ q.2.1, k ∉ r.wit.map (·.1) := fun k hk' => sr_wdom r hok (hSI _ (by simp only [SRefined.SV, List.mem_append]; right; exact hk k hk'))\n -- Both sides as one condition over every value of the keys: compare the states they read the body at.\n have hall : ∀ k, evalC (atKeys (withInputs r.spec.f.m.inputs σ (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ))) q.2.1 k) (withCarried r.spec Ms κ) q.2.2\n = evalC (applyS r.wit (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ)) (withCarried r.full M κ) q.2.2 := by\n intro k\n rw [congrMC _ _ (withCarried r.full M κ) _ (fun mp hmp => hN mp (avoidsMaps_occ (rn_m hrd) hmp))]\n apply congrC; intro i hi\n have hiR := avoids_occ (rn_v hrd) hi\n simp only [List.mem_append, not_or] at hiR\n obtain ⟨⟨hiI, hiF⟩, hiQ⟩ := hiR\n by_cases hik : i ∈ q.2.1\n · rw [applyS_other (hkdom i hik), atKeys_in hik, atKeys_in hik]\n · rw [atKeys_out hik]\n by_cases hii : i ∈ r.spec.f.m.inputs\n · simp only [withInputs, List.contains_iff_mem.mpr hii, if_true]\n rw [hιs i hii]\n simp only [applyS]\n cases ha : assoc r.wit i with\n | none => rw [atKeys_out hik]; exact decideQ_other (fun h' => hfull_q i (hIVq i hiI) hiQ h') _ _\n | some e =>\n have hm := sr_wit_some r hok ha\n dsimp only\n rw [sr_wit_atKeys r hok (hwit _ hm).2 hk]\n exact congrE _ _ _ e (fun j hj => decideQ_other (fun h' => avoidsE_occ (hwit _ hm).2 hj (sr_W r hok (Or.inr h'))) _ _)\n · rw [withInputs_other hii, applyS_other (sr_wdom r hok hii), atKeys_out hik]\n rw [sfull_inputs, withInputs_other (hIVi i hiI)]\n exact hag i (by simp only [SRefined.X, List.mem_append, not_or]; exact ⟨⟨⟨hiI, hiF⟩, hiQ⟩, hii⟩)\n simp only [show (∀ k, holdsC (atKeys (withInputs r.spec.f.m.inputs σ (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ))) q.2.1 k) (withCarried r.spec Ms κ) q.2.2)\n ↔ (∀ k, holdsC (applyS r.wit (atKeys (withInputs r.full.f.m.inputs ρ ι) q.2.1 k) (withCarried r.full M κ)) (withCarried r.full M κ) q.2.2) from\n forall_congr' (fun k => by rw [← evalC_iff, ← evalC_iff, hall k])]\n · rw [decideQ_other hvq]\n by_cases hvi : v ∈ r.spec.f.m.inputs\n · simp only [withInputs, List.contains_iff_mem.mpr hvi, if_true]\n exact hιs v hvi\n · rw [withInputs_other hvi, applyS_other (sr_wdom r hok hvi), decideQ_other (hfull_q v (hIVq v hvI) hvq), sfull_inputs, withInputs_other (hIVi v hvI)]\n exact hag v (by simp only [SRefined.X, List.mem_append, not_or]; exact ⟨⟨⟨hvI, hvF⟩, hvq⟩, hvi⟩)\n\n-- The spec's view of a step agrees with the composite's with the witness put in, outside the implementation's names.\ntheorem sr_view {ρ σ : Nat → Nat} {M Ms : Nat → Nat → Nat} (hag : Agree r.X σ ρ) (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n Agree r.IV (sView r.spec σ Ms (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ)) κ).1\n (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ)) := by\n obtain ⟨_, hsf, _, _, _, _, _, _, _⟩ := srSpec_parts (sr_spec r hok)\n obtain ⟨hsv, hsi, _, _, _, hwit, _, _, _⟩ := srNames_parts (sr_names r hok)\n have hN := sr_maps r hok hmag κ\n have hQ := sr_q r hok hag hmag ι κ\n simp only [sView] at hQ\n have hIVf : ∀ i, i ∉ r.IV → i ∉ flagVars r.impl.f.flags := fun i hi h' => hi (by simp only [SRefined.IV, List.mem_append]; left; left; right; exact h')\n intro v hv\n simp only [sView]\n by_cases hvf : v ∈ flagVars r.spec.f.flags\n · have hvi : v ∉ r.spec.f.m.inputs := fun h' => hsi v h' (List.mem_append_right _ (by simp only [SRefined.SV, List.mem_append]; left; left; right; exact hvf))\n cases hfo : flagOf r.spec.f.flags v with\n | none => exact absurd hvf (flagOf_none hfo)\n | some c =>\n obtain ⟨p, hp, rfl⟩ := flagOf_some hfo\n have hfull : flagOf r.full.f.flags v = some (substC r.wit p.2) := by\n rw [sfull_flags, flagOf_append_right (hIVf v hv), flagOf_map, hfo]; rfl\n rw [decideFlags_of, hfo, applyS_other (sr_wdom r hok hvi), decideFlags_of, hfull]\n simp only\n rw [subst_evalC, congrMC _ _ (withCarried r.full M κ) _ (fun mp hmp => hN mp (avoidsMaps_occ (rn_m (hsf p hp)) hmp))]\n rw [congrC _ (applyS r.wit (decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ)) (withCarried r.full M κ)) _ p.2\n (fun j hj => hQ j (avoids_occ (rn_v (hsf p hp)) hj))]\n · rw [decide_other hvf]\n have hX : v ∉ r.IV ++ flagVars r.spec.f.flags := by simp only [List.mem_append, not_or]; exact ⟨hv, hvf⟩\n rw [hQ v hX]\n simp only [applyS]\n cases ha : assoc r.wit v with\n | none =>\n dsimp only\n rw [decide_other (fun h' => by\n simp only [sfull_flags, flagVars, List.map_append, List.map_map, List.mem_append] at h'\n rcases h' with h' | h'\n · exact hIVf v hv h'\n · exact hvf (by simpa [flagVars, Function.comp_def] using h'))]\n | some e =>\n have hm := sr_wit_some r hok ha\n dsimp only\n exact congrE _ _ _ e (fun j hj => (decide_other (fun h' => avoidsE_occ (hwit _ hm).2 hj (sr_W r hok (Or.inl h'))) _ _).symm)\n\n-- A term of the spec, read in the spec's view, is the term with the witness put in, read in the composite's view; with a\n-- bound key of the spec set, alike.\ntheorem sr_term {ρ σ : Nat → Nat} {M Ms : Nat → Nat → Nat} (hag : Agree r.X σ ρ) (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) {e : E} (he : readsNoneE r.IV r.IM e = true) :\n evalE (sView r.spec σ Ms (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ)) κ).1 (withCarried r.spec Ms κ) e\n = evalE (sView r.full ρ M ι κ).1 (withCarried r.full M κ) (substE r.wit e) := by\n rw [subst_evalE, congrME _ _ (withCarried r.full M κ) e (fun mp hmp => sr_maps r hok hmag κ mp (avoidsMapsE_occ (rnE_m he) hmp))]\n exact congrE _ _ _ e (fun j hj => sr_view r hok hag hmag ι κ j (avoidsE_occ (rnE_v he) hj))\n\ntheorem sr_cond {ρ σ : Nat → Nat} {M Ms : Nat → Nat → Nat} (hag : Agree r.X σ ρ) (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) {c : C} (hc : readsNone r.IV r.IM c = true) :\n evalC (sView r.spec σ Ms (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ)) κ).1 (withCarried r.spec Ms κ) c\n = evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) (substC r.wit c) := by\n rw [subst_evalC, congrMC _ _ (withCarried r.full M κ) c (fun mp hmp => sr_maps r hok hmag κ mp (avoidsMaps_occ (rn_m hc) hmp))]\n exact congrC _ _ _ c (fun j hj => sr_view r hok hag hmag ι κ j (avoids_occ (rn_v hc) hj))\n\ntheorem sr_term_upd {ρ σ : Nat → Nat} {M Ms : Nat → Nat → Nat} (hag : Agree r.X σ ρ) (hmag : ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) {e : E} (he : readsNoneE r.IV r.IM e = true) {k : Nat} (hk : k ∈ bkeys r.spec) (key : Nat) :\n evalE (upd (sView r.spec σ Ms (applyS r.wit (sView r.full ρ M ι κ).1 (withCarried r.full M κ)) κ).1 k key) (withCarried r.spec Ms κ) e\n = evalE (upd (sView r.full ρ M ι κ).1 k key) (withCarried r.full M κ) (substE r.wit e) := by\n obtain ⟨_, hsi, _, _, _, hwit, _, _, _⟩ := srNames_parts (sr_names r hok)\n have hkdom : k ∉ r.wit.map (·.1) := sr_wdom r hok (fun h' => hsi k h' (List.mem_append_right _ (by simp only [SRefined.SV, List.mem_append]; right; exact hk)))\n rw [subst_evalE, congrME _ _ (withCarried r.full M κ) e (fun mp hmp => sr_maps r hok hmag κ mp (avoidsMapsE_occ (rnE_m he) hmp))]\n apply congrE; intro j hj\n by_cases hjk : j = k\n · subst hjk; rw [applyS_other hkdom]; simp [upd]\n · have hv := sr_view r hok hag hmag ι κ j (avoidsE_occ (rnE_v he) hj)\n simp only [upd, hjk, if_false]\n rw [hv]\n simp only [applyS]\n cases ha : assoc r.wit j with\n | none => simp [upd, hjk]\n | some e' =>\n have hm := sr_wit_some r hok ha\n dsimp only\n exact (sr_wit_upd r hok (hwit _ hm).2 hk key).symm\n\n-- A variable none of the names the composite's view sets, nor the spec's: each view reads it as its state does.\ntheorem sr_plain_full {ρ : Nat → Nat} {M : Nat → Nat → Nat} (ι : Nat → Nat) (κ : Nat → Nat → Nat) {v : Nat} (hv : v ∉ r.X) :\n (sView r.full ρ M ι κ).1 v = ρ v := by\n simp only [SRefined.X, SRefined.IV, List.mem_append, not_or] at hv\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨hi, _⟩, _⟩, hf⟩, hq⟩, _⟩, hsf⟩, hsq⟩, _⟩ := hv\n simp only [sView]\n rw [decide_other (fun h' => by\n simp only [sfull_flags, flagVars, List.map_append, List.map_map, List.mem_append] at h'\n rcases h' with h' | h'\n · exact hf h'\n · exact hsf (by simpa [flagVars, Function.comp_def] using h'))]\n rw [decideQ_other (fun h' => by\n simp only [SMachine.qVars, sfull_qflags, List.map_append, List.map_map, List.mem_append] at h'\n rcases h' with h' | h'\n · exact hq h'\n · exact hsq (by simpa [SMachine.qVars, Function.comp_def] using h')), sfull_inputs]\n exact withInputs_other hi ρ ι\n\ntheorem sr_plain_spec {σ : Nat → Nat} {Ms : Nat → Nat → Nat} (ι : Nat → Nat) (κ : Nat → Nat → Nat) {v : Nat} (hv : v ∉ r.X) :\n (sView r.spec σ Ms ι κ).1 v = σ v := by\n simp only [SRefined.X, List.mem_append, not_or] at hv\n obtain ⟨⟨⟨_, hsf⟩, hsq⟩, hsi⟩ := hv\n simp only [sView]\n rw [decide_other hsf, decideQ_other hsq]\n exact withInputs_other hsi σ ι\n\n/-- The spec's part of every reachable state of the composite is a reachable state of the spec, given that the spec's\nprecondition holds at every reachable state of the composite (one of its laws) and that, at every step the composite\ntakes, the spec stutters or allows the request it is sent (its step law). -/\ntheorem srefine_project (hpre : ∀ ρ M, SReach r.full ρ M → evalC ρ M r.spec.f.m.pre = true)\n (hallow : ∀ ρ M, SReach r.full ρ M → ∀ ι κ, evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 r.full.f.m.assume = true →\n evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 (.or r.stutter (substC r.wit r.spec.f.m.assume)) = true) :\n ∀ {ρ M}, SReach r.full ρ M → ∃ σ Ms, SReach r.spec σ Ms ∧ Agree r.X σ ρ ∧ ∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = M mp := by\n obtain ⟨hsa, _, hss, _, hsw, hse, _, hsp, _⟩ := srSpec_parts (sr_spec r hok)\n obtain ⟨_, _, _, _, hcarO, _, hst, _, hsfacts⟩ := srNames_parts (sr_names r hok)\n have hIVst : ∀ v, v ∉ r.IV → v ∉ r.impl.f.m.step.map (·.1) := fun v hv h' => hv (by simp only [SRefined.IV, List.mem_append]; left; left; left; right; exact h')\n -- A map of the spec's part, unwritten by the implementation, read where the request carries none: the state's.\n have hNc : ∀ (M : Nat → Nat → Nat) (κ : Nat → Nat → Nat) mp, mp ∉ r.IM → mp ∉ r.spec.carried → withCarried r.full M κ mp = M mp := fun M κ mp h1 h2 => by\n show (if r.impl.carried.contains mp = true then κ mp else M mp) = M mp\n rw [if_neg (fun hc => h1 (by\n simp only [SRefined.IM, List.mem_append, List.mem_filter]; right\n exact ⟨List.contains_iff_mem.mp hc, by simpa [List.contains_iff_mem] using h2⟩))]\n have hIMe : ∀ mp, mp ∉ r.IM → lookup (fun w => w.1 == mp) r.impl.every = none := fun mp h => lookup_none (fun w hw => by\n simp only [beq_eq_false_iff_ne]; intro he; exact h (by simp only [SRefined.IM, SRefined.IO, List.mem_append]; left; right; rw [← he]; exact List.mem_map_of_mem hw))\n have hIMw : ∀ mp, mp ∉ r.IM → lookup (fun w : Nat × E × E => w.1 == mp) r.impl.f.m.writes = none := fun mp h => lookup_none (fun w hw => by\n simp only [beq_eq_false_iff_ne]; intro he; exact h (by simp only [SRefined.IM, SRefined.IO, List.mem_append]; left; left; right; rw [← he]; exact List.mem_map_of_mem hw))\n intro ρ M hr\n induction hr with\n | start ρ M hM h0 _ _ =>\n have hp0 : evalC ρ M r.spec.f.m.pre = true := by\n have h := hpre _ M (SReach.start ρ M hM h0 (fun l hl => by simp [SRefined.full] at hl) (fun p hp => by simp [SRefined.full] at hp))\n have hag0 : ∀ i, occC i r.spec.f.m.pre = true → applyS r.full.f.m.init ρ M i = ρ i := fun i hi => applyS_other (fun h' => by\n apply avoids_occ (rn_v hsp) hi\n rw [sfull_init] at h'\n simp only [List.map_append, List.mem_append] at h'\n rcases h' with h' | h' <;> simp [SRefined.X, SRefined.IV, SRefined.SV, h']) ρ M\n rwa [congrC _ ρ M r.spec.f.m.pre hag0] at h\n have hMs : StartMaps r.spec.f.m M := fun p hp' key => hM p (by rw [sfull_maps0]; exact List.mem_append_right _ hp') key\n refine ⟨_, M, SReach.start ρ M hMs hp0 (fun l hl => by simp [hsfacts] at hl) (fun p hp => by simp [sr_pfacts r hok] at hp), fun i hi => ?_, fun _ _ _ => rfl⟩\n show applyS r.spec.f.m.init ρ M i = applyS (r.impl.f.m.init ++ r.spec.f.m.init) ρ M i\n rw [applyS_append_right (fun h' => hi (by simp [SRefined.X, SRefined.IV, h']))]\n | step ρ M ι κ hr ha ih =>\n obtain ⟨σ, Ms, hrs, hag, hmag⟩ := ih\n have hA := hallow ρ M hr ι κ ha\n simp only [evalC, Bool.or_eq_true, sView_snd] at hA\n -- The composite's step at a variable of the spec's part: kept where it stutters, the spec's term otherwise.\n have hstepS : ∀ v, v ∉ r.IV → (sNext r.full ρ M ι κ).1 v\n = applyS (r.spec.f.m.step.map (fun p => (p.1, E.ite r.stutter (.var p.1) (substE r.wit p.2)))) (sView r.full ρ M ι κ).1 (withCarried r.full M κ) v :=\n fun v hv => by simp only [sNext]; rw [sfull_step]; exact applyS_append_right (hIVst v hv) _ _\n have hstutK : ∀ k ∈ bkeys r.spec, ∀ key, evalC (upd (sView r.full ρ M ι κ).1 k key) (withCarried r.full M κ) r.stutter\n = evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) r.stutter := fun k hk key => by\n apply congrC; intro j hj; simp only [upd]; split\n · rename_i h; subst h; exact absurd (by simp only [SRefined.W, List.mem_append]; right; exact hk) (avoids_occ hst hj)\n · rfl\n by_cases hstut : evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) r.stutter = true\n · -- The spec doesn't step: its state is the same, and so is the composite's spec part.\n refine ⟨σ, Ms, hrs, fun v hv => ?_, fun mp hmp hmc => ?_⟩\n · rw [hstepS v (fun h' => hv (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ h'))))]\n simp only [applyS]\n cases hl : assoc (r.spec.f.m.step.map (fun p => (p.1, E.ite r.stutter (.var p.1) (substE r.wit p.2)))) v with\n | none => dsimp only; rw [sr_plain_full r hok ι κ hv]; exact hag v hv\n | some e =>\n obtain ⟨q, hq, hqv, rfl⟩ : ∃ q ∈ r.spec.f.m.step, q.1 = v ∧ e = E.ite r.stutter (.var q.1) (substE r.wit q.2) := by\n simp only [assoc, Option.map_eq_some_iff] at hl\n obtain ⟨x, hx, rfl⟩ := hl\n obtain ⟨hxm, hxv⟩ := lookup_some hx\n obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hxm\n simp only [beq_iff_eq] at hxv\n exact ⟨q, hq, hxv, rfl⟩\n dsimp only\n simp only [evalE, hstut, if_true]\n rw [hqv, sr_plain_full r hok ι κ hv]; exact hag v hv\n · funext key\n rw [hmag mp hmp hmc]\n simp only [sNext, everyMaps, sView_snd]\n rw [sfull_every, lookup_append, hIMe mp hmp]\n simp only [lookup_emap]\n cases hw : lookup (fun w => w.1 == mp) r.spec.every with\n | some w =>\n have hwm := (lookup_some hw).2\n simp only [beq_iff_eq] at hwm\n have hk : w.2.1 ∈ bkeys r.spec := by simp only [bkeys, List.mem_append]; left; exact List.mem_map_of_mem (lookup_some hw).1\n simp only [Option.map_some, evalE]\n rw [hstutK _ hk key]\n simp only [hstut, if_true, upd, hwm, hNc M κ mp hmp hmc]\n | none =>\n simp only [Option.map_none, writeMaps]\n rw [sfull_writes, lookup_append, hIMw mp hmp]\n try dsimp only\n rw [lookup_writes_map]\n cases hw0 : lookup (fun w : Nat × E × E => w.1 == mp) r.spec.f.m.writes with\n | none => simp only [Option.map_none]; exact (congrFun (hNc M κ mp hmp hmc) key).symm\n | some w0 =>\n have hw0m := (lookup_some hw0).2\n simp only [beq_iff_eq] at hw0m\n simp only [Option.map_some, evalE, hstut, if_true, hw0m]\n by_cases hk : key = evalE (sView r.full ρ M ι κ).1 (withCarried r.full M κ) (substE r.wit w0.2.1)\n · rw [if_pos hk, ← hk]; exact (congrFun (hNc M κ mp hmp hmc) _).symm\n · rw [if_neg hk]; exact (congrFun (hNc M κ mp hmp hmc) key).symm\n · -- The spec steps, sent the witness's request, which it allows.\n have hX := hA.resolve_left hstut\n rw [← sr_cond r hok hag hmag ι κ (c := r.spec.f.m.assume) hsa] at hX\n refine ⟨_, _, SReach.step σ Ms _ κ hrs hX, fun v hv => ?_, fun mp hmp hmc => ?_⟩\n · rw [hstepS v (fun h' => hv (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ h'))))]\n simp only [sNext, applyS, sView_snd]\n rw [assoc_map_fst]\n cases hl : lookup (fun q => q.1 == v) r.spec.f.m.step with\n | none =>\n have hn : assoc r.spec.f.m.step v = none := by simp only [assoc, hl, Option.map_none]\n rw [hn]; dsimp only\n rw [sr_plain_full r hok ι κ hv, sr_plain_spec r hok _ κ hv]; exact hag v hv\n | some q =>\n have hq := lookup_some hl\n simp only [beq_iff_eq] at hq\n have hn : assoc r.spec.f.m.step v = some q.2 := by simp only [assoc, hl, Option.map_some]\n rw [hn]; dsimp only\n simp only [Option.map_some, evalE, show evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) r.stutter = false by simpa using hstut, Bool.false_eq_true, if_false]\n have hm : (v, q.2) ∈ r.spec.f.m.step := by rw [← hq.2]; exact hq.1\n exact sr_term r hok hag hmag ι κ (hss (v, q.2) hm)\n · funext key\n simp only [sNext, everyMaps, sView_snd]\n rw [sfull_every, lookup_append, hIMe mp hmp]\n simp only [lookup_emap]\n cases hw : lookup (fun w => w.1 == mp) r.spec.every with\n | some w =>\n have hk : w.2.1 ∈ bkeys r.spec := by simp only [bkeys, List.mem_append]; left; exact List.mem_map_of_mem (lookup_some hw).1\n simp only [Option.map_some, evalE]\n rw [hstutK _ hk key]\n simp only [show evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) r.stutter = false by simpa using hstut, Bool.false_eq_true, if_false]\n exact sr_term_upd r hok hag hmag ι κ (hse w (lookup_some hw).1) hk key\n | none =>\n simp only [Option.map_none, writeMaps]\n rw [sfull_writes, lookup_append, hIMw mp hmp]\n try dsimp only\n rw [lookup_writes_map]\n cases hw0 : lookup (fun w : Nat × E × E => w.1 == mp) r.spec.f.m.writes with\n | none => simp only [Option.map_none]; exact congrFun (sr_maps r hok hmag κ mp hmp) key\n | some w0 =>\n obtain ⟨h1, h2⟩ := hsw w0 (lookup_some hw0).1\n simp only [Option.map_some, evalE, show evalC (sView r.full ρ M ι κ).1 (withCarried r.full M κ) r.stutter = false by simpa using hstut, Bool.false_eq_true, if_false]\n rw [sr_term r hok hag hmag ι κ h1, sr_term r hok hag hmag ι κ h2, sr_maps r hok hmag κ mp hmp]\nend sproject\n\n-- The composite's step law: on every request it takes, the spec stutters or allows the request it is sent.\ndef srStep (r : SRefined) (prime primeMaps : List (Nat × Nat)) (p : SProof) : SKStep :=\n ⟨⟨[trueC], r.allowed, [[]], prime, primeMaps, p.1, (p.2.2.2.2.2.1, p.2.2.2.2.2.2.2)⟩, [[]], p.2.1, p.2.2.1, p.2.2.2.1, p.2.2.2.2.1, p.2.2.2.2.2.2.1, []⟩\n\n/-- What a refinement over sets means: with the relation (and the spec's precondition, the j-th law) proven of the\ncomposite, and its step law that the spec stutters or allows every request it is sent, every reachable state of the\nimplementation is, outside the spec's part, a reachable state of the composite, whose spec part is a reachable state of\nthe spec, the two related by every law. -/\ntheorem srefines (r : SRefined) (hok : srOk r = true) (hmo : distinct (r.spec.f.m.maps0.map (·.1)) = true) (ps : List SEntry)\n (hl : checkSMachine r.full ps = true)\n (j : Nat) (hj : (ps[j]?).map (·.law.holds) = some r.spec.f.m.pre ∧ (ps[j]?).map (·.law.key) = some none)\n (prime primeMaps : List (Nat × Nat)) (p : SProof) (hs : checkSKStep r.full ps (srStep r prime primeMaps p) = true)\n (hfresh : avoids (prime.map (·.2)) r.allowed = true ∧ avoidsMaps (primeMaps.map (·.2)) r.allowed = true)\n (hw : avoids r.full.wVars r.allowed = true) (hiq : r.full.f.m.inputs.all (fun i => !r.full.qVars.contains i) = true) :\n ∀ ρ M, SReach r.impl ρ M → ∃ ρc Mc σ Ms, SReach r.full ρc Mc ∧ Agree r.SV ρc ρ ∧ (∀ mp, mp ∉ r.SM → Mc mp = M mp)\n ∧ SReach r.spec σ Ms ∧ Agree r.X σ ρc ∧ (∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = Mc mp) ∧ ∀ e ∈ ps, e.Holds ρc Mc := by\n have hlaws := smachine_sound r.full ps hl\n have hpre : ∀ ρ M, SReach r.full ρ M → evalC ρ M r.spec.f.m.pre = true := by\n intro ρ M hr\n cases he : ps[j]? with\n | none => simp [he] at hj\n | some e =>\n simp only [he, Option.map_some, Option.some.injEq] at hj\n have h := SEntry.holds_law (hlaws ρ M hr e (List.mem_of_getElem? he))\n unfold MLaw.Holds at h\n rw [hj.2] at h\n simpa [hj.1] using h\n have hiq' : ∀ i, i ∈ r.full.f.m.inputs → i ∉ r.full.qVars := fun i hi hq => by\n simp only [List.all_eq_true, Bool.not_eq_true'] at hiq\n have := hiq i hi; rw [List.contains_iff_mem.mpr hq] at this; cases this\n have hallow : ∀ ρ M, SReach r.full ρ M → ∀ ι κ, evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 r.full.f.m.assume = true →\n evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 (.or r.stutter (substC r.wit r.spec.f.m.assume)) = true := by\n intro ρ M hr ι κ hA\n have h := skstep_sound r.full ps _ hl hs ρ M hr (ι, κ) [] rfl ⟨hA, by simp [trueC, evalC, evalE], trivial⟩\n have h' : evalC (primed prime (sNext r.full ρ M ι κ).1 (withExt r.full ρ (sReq r.full ρ M ι κ)))\n (primedMaps primeMaps (sNext r.full ρ M ι κ).2 (withCarried r.full M κ)) r.allowed = true := by\n simpa [srStep, sEnd, sExts, renEnv, renMaps, lookup] using h\n rw [unprimed_evalC hfresh.1 hfresh.2] at h'\n -- The request's extended variables, read as the step reads them: the inputs, and each quantified flag decided.\n have hE : ∀ v, v ∉ r.full.wVars → withExt r.full ρ (sReq r.full ρ M ι κ) v\n = decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ) v := by\n intro v hv\n simp only [withExt, sReq]\n by_cases hi : v ∈ r.full.f.m.inputs\n · have hext : r.full.extVars.contains v = true := List.contains_iff_mem.mpr (by simp [SMachine.extVars, hi])\n rw [decideQ_other (hiq' v hi)]\n simp only [hext, if_true, List.contains_iff_mem.mpr hi, withInputs]\n · have hin : r.full.f.m.inputs.contains v = false := by simpa [List.contains_iff_mem] using hi\n by_cases hq : v ∈ r.full.qVars\n · have hext : r.full.extVars.contains v = true := List.contains_iff_mem.mpr (by simp [SMachine.extVars, hq])\n obtain ⟨q, hq0, rfl⟩ := List.mem_map.mp hq\n cases hl : lookup (fun q' => q'.1 == q.1) r.full.qflags with\n | none => have := lookup_none_all hl q hq0; simp at this\n | some q' => simp only [hext, if_true, hin, Bool.false_eq_true, if_false, hl]\n · have hext : r.full.extVars.contains v = false := by\n simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append, not_or]\n simpa [List.contains_iff_mem] using (⟨hi, hq, hv⟩ : v ∉ r.full.f.m.inputs ∧ v ∉ r.full.qVars ∧ v ∉ r.full.wVars)\n rw [decideQ_other hq, withInputs_other hi]\n simp only [hext, Bool.false_eq_true, if_false]\n rw [congrC _ _ _ r.allowed (fun i hi => hE i (avoids_occ hw hi))] at h'\n simp only [SRefined.allowed] at h'\n rw [inline_evalC] at h'\n exact h'\n intro ρ M hr\n obtain ⟨ρc, Mc, hrc, hag, hmag⟩ := srefine_lift r hok hmo hr\n obtain ⟨σ, Ms, hrs, hags, hmags⟩ := srefine_project r hok hpre hallow hrc\n exact ⟨ρc, Mc, σ, Ms, hrc, hag, hmag, hrs, hags, hmags, hlaws ρc Mc hrc⟩\n\nend Kern\n\n#print axioms Kern.srefine_lift\n#print axioms Kern.srefine_project\n#print axioms Kern.srefines\n",
16
16
  "Bridge.lean": "import Sets\nimport Cyclic\n\n/-!\nBridge: a cyclic proof of one of today's rules is a proof about the rule. Lean 4.34.0, core only, on top of\n`Sets.lean` and `Cyclic.lean`.\n\n`cyclic_sound` says what a cyclic proof's nodes say about any maps that meet its facts. core/cyclic.ts's instances say a\nrule in those terms, and this file says them back: what an instance's facts and nodes are is a function of the rule's\nown syntax here, a check (`cycMOk`) compares the exported proof against it, and a theorem carries the node to the rule's\nown meaning.\n\nMachines (`machineNodes`). A run of an `SMachine` (Sets.lean's `SReach`, with no flag that holds at every key) is put\nas maps over time: each state variable and input x as a map x@ whose cell u is x's value at time u (an input, the\nrequest taken at u), each map M (and each map a request carries) as a map M@ whose cell u + k·Ω is M's cell k at time u,\nΩ above the run's length T, so that u < Ω and the pair decodes (u = (u + k·Ω) % Ω, k = (u + k·Ω) / Ω) at every key k.\nThe facts are the start and each step (flags inlined), each map's start and step (a write at one key read at the key\nthe fact names), the functions' facts, and conditions that hold of every natural (`natC`). `cyc_machine_sound`: if the\nexported facts are among those, the node's hypotheses among the precondition, t ≤ T and T < Ω, and its conclusion the\nlaw at time t, then the law holds in every reachable state, at every value of its keys.\n-/\n\nnamespace Kern\nset_option linter.unusedSimpArgs false\nset_option linter.unusedSectionVars false\n\n/-! Lists and terms -/\n\ntheorem not_mem_of_not_contains {l : List Nat} {a : Nat} (h : (!l.contains a) = true) : a ∉ l := fun hm => by\n rw [List.contains_iff_mem.mpr hm] at h; exact absurd h (by decide)\n\n-- A condition true of every natural: 0 ≤ e, and conjunctions of those.\ndef natC : C → Bool\n | .le a _ => beqE a (.const 0)\n | .and a b => natC a && natC b\n | _ => false\n\ntheorem natC_true {ρ : Nat → Nat} {M : Nat → Nat → Nat} : ∀ c, natC c = true → evalC ρ M c = true\n | .le a b, h => by simp only [natC] at h; rw [beqE_eq h]; simp [evalC, evalE]\n | .and a b, h => by\n simp only [natC, Bool.and_eq_true] at h; simp only [evalC, natC_true a h.1, natC_true b h.2, Bool.and_self]\n | .lt _ _, h => by simp [natC] at h\n | .eq _ _, h => by simp [natC] at h\n | .or _ _, h => by simp [natC] at h\n | .not _, h => by simp [natC] at h\n\n-- A list of conditions as their conjunction, as TypeScript's `and` and the exporter write it.\ndef andL : List C → C\n | [] => trueC\n | [a] => a\n | a :: b :: rest => .and a (andL (b :: rest))\n\ntheorem andL_true {ρ : Nat → Nat} {M : Nat → Nat → Nat} : ∀ l : List C, (∀ c ∈ l, evalC ρ M c = true) → evalC ρ M (andL l) = true\n | [], _ => by simp [andL, trueC, evalC, evalE]\n | [a], h => by simpa [andL] using h a (by simp)\n | a :: b :: rest, h => by\n simp only [andL, evalC, Bool.and_eq_true]\n exact ⟨h a (by simp), andL_true (b :: rest) (fun c hc => h c (List.mem_cons_of_mem _ hc))⟩\n\ntheorem evalE_ite_eq (ρ : Nat → Nat) (M : Nat → Nat → Nat) (a b t f : E) :\n evalE ρ M (.ite (.eq a b) t f) = if evalE ρ M a = evalE ρ M b then evalE ρ M t else evalE ρ M f := by\n show (if evalC ρ M (.eq a b) = true then evalE ρ M t else evalE ρ M f) = _\n by_cases h : evalE ρ M a = evalE ρ M b\n · rw [if_pos ((evalC_iff ρ M (.eq a b)).mpr h), if_pos h]\n · rw [if_neg (fun hc => h ((evalC_iff ρ M (.eq a b)).mp hc)), if_neg h]\n\ntheorem impl_true {ρ : Nat → Nat} {M : Nat → Nat → Nat} {a b : C} (h : evalC ρ M a = true → evalC ρ M b = true) :\n evalC ρ M (.or (.not a) b) = true := by\n cases ha : evalC ρ M a with\n | false => simp [evalC, ha]\n | true => simp [evalC, ha, h ha]\n\ntheorem congr2E {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat} (e : E) (hv : ∀ i, occE i e = true → ρ i = ρ' i)\n (hm : ∀ m, occME m e = true → M m = M' m) : evalE ρ M e = evalE ρ' M' e := by\n rw [congrE ρ ρ' M e hv, congrME ρ' M M' e hm]\ntheorem congr2C {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat} (c : C) (hv : ∀ i, occC i c = true → ρ i = ρ' i)\n (hm : ∀ m, occMC m c = true → M m = M' m) : evalC ρ M c = evalC ρ' M' c := by\n rw [congrC ρ ρ' M c hv, congrMC ρ' M M' c hm]\n\n-- Every read at the term `fr` read at `to` instead (core/cyclic.ts's readsAt): the same wherever the two are equal.\nmutual\ndef readsAtE (fr to : E) : E → E\n | .var i => .var i\n | .const n => .const n\n | .add a b => .add (readsAtE fr to a) (readsAtE fr to b)\n | .sub a b => .sub (readsAtE fr to a) (readsAtE fr to b)\n | .mul k a => .mul k (readsAtE fr to a)\n | .min a b => .min (readsAtE fr to a) (readsAtE fr to b)\n | .max a b => .max (readsAtE fr to a) (readsAtE fr to b)\n | .prod a b => .prod (readsAtE fr to a) (readsAtE fr to b)\n | .ite c t f => .ite (readsAtC fr to c) (readsAtE fr to t) (readsAtE fr to f)\n | .get m key => if beqE key fr then .get m to else .get m (readsAtE fr to key)\ndef readsAtC (fr to : E) : C → C\n | .le a b => .le (readsAtE fr to a) (readsAtE fr to b)\n | .lt a b => .lt (readsAtE fr to a) (readsAtE fr to b)\n | .eq a b => .eq (readsAtE fr to a) (readsAtE fr to b)\n | .and a b => .and (readsAtC fr to a) (readsAtC fr to b)\n | .or a b => .or (readsAtC fr to a) (readsAtC fr to b)\n | .not a => .not (readsAtC fr to a)\nend\n\nsection\nvariable {ρ : Nat → Nat} {M : Nat → Nat → Nat} {fr to : E} (h : evalE ρ M to = evalE ρ M fr)\ninclude h\nmutual\ntheorem readsAt_evalE : (e : E) → evalE ρ M (readsAtE fr to e) = evalE ρ M e\n | .var _ => rfl\n | .const _ => rfl\n | .add a b => by simp only [readsAtE, evalE, readsAt_evalE a, readsAt_evalE b]\n | .sub a b => by simp only [readsAtE, evalE, readsAt_evalE a, readsAt_evalE b]\n | .mul k a => by simp only [readsAtE, evalE, readsAt_evalE a]\n | .min a b => by simp only [readsAtE, evalE, readsAt_evalE a, readsAt_evalE b]\n | .max a b => by simp only [readsAtE, evalE, readsAt_evalE a, readsAt_evalE b]\n | .prod a b => by simp only [readsAtE, evalE, readsAt_evalE a, readsAt_evalE b]\n | .ite c t f => by simp only [readsAtE, evalE, readsAt_evalC c, readsAt_evalE t, readsAt_evalE f]\n | .get m key => by\n simp only [readsAtE]\n split\n · rename_i hb; simp only [evalE, h, beqE_eq hb]\n · simp only [evalE, readsAt_evalE key]\ntheorem readsAt_evalC : (c : C) → evalC ρ M (readsAtC fr to c) = evalC ρ M c\n | .le a b => by simp only [readsAtC, evalC, readsAt_evalE a, readsAt_evalE b]\n | .lt a b => by simp only [readsAtC, evalC, readsAt_evalE a, readsAt_evalE b]\n | .eq a b => by simp only [readsAtC, evalC, readsAt_evalE a, readsAt_evalE b]\n | .and a b => by simp only [readsAtC, evalC, readsAt_evalC a, readsAt_evalC b]\n | .or a b => by simp only [readsAtC, evalC, readsAt_evalC a, readsAt_evalC b]\n | .not a => by simp only [readsAtC, evalC, readsAt_evalC a]\nend\nend\n\ntheorem applyS_getD (σ : List (Nat × E)) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (i : Nat) :\n applyS σ ρ M i = evalE ρ M ((assoc σ i).getD (.var i)) := by\n simp only [applyS]; cases assoc σ i <;> rfl\n\ntheorem assoc_some_mem {σ : List (Nat × E)} {i : Nat} {e : E} (h : assoc σ i = some e) : (i, e) ∈ σ := by\n simp only [assoc, Option.map_eq_some_iff] at h\n obtain ⟨p, hp, rfl⟩ := h\n obtain ⟨hm, hq⟩ := lookup_some hp\n have : p.1 = i := by simpa using hq\n rw [← this]; exact hm\n\ntheorem pair_mod {u T : Nat} (h : u ≤ T) (kv : Nat) : (u + kv * (T + 1)) % (T + 1) = u := by\n rw [Nat.add_mul_mod_self_right]; exact Nat.mod_eq_of_lt (by omega)\ntheorem pair_div {u T : Nat} (h : u ≤ T) (kv : Nat) : (u + kv * (T + 1)) / (T + 1) = kv := by\n rw [Nat.add_mul_div_right _ _ (by omega), Nat.div_eq_of_lt (by omega), Nat.zero_add]\n\n/-! The encoding (core/cyclic.ts's machineNodes) as a function of the machine -/\n\n-- The variables a machine's facts and nodes add (a fact's time and key, the run's length, Ω, a node's time), and the\n-- maps over time: each state variable and input's, each map's.\nstructure CycEnc where\n u : Nat\n k : Nat\n T : Nat\n om : Nat\n t : Nat\n vtr : List (Nat × Nat)\n mtr : List (Nat × Nat)\n\ndef CycEnc.special (ce : CycEnc) : List Nat := [ce.u, ce.k, ce.T, ce.om, ce.t]\ndef CycEnc.tm (ce : CycEnc) : List Nat := ce.vtr.map (·.2) ++ ce.mtr.map (·.2)\n\n-- A term at time w: each state variable and input read from its map at w, each map's cell k from its map at w + k·Ω.\ndef cycσ (ce : CycEnc) (w : E) : List (Nat × E) := ce.vtr.map fun p => (p.1, .get p.2 w)\ndef cycRd (ce : CycEnc) (w : E) : Nat → E → E := fun m key =>\n match lookup (fun p => p.1 == m) ce.mtr with\n | some p => .get p.2 (.add w (.prod key (.var ce.om)))\n | none => .get m key\ndef trE (ce : CycEnc) (w : E) : E → E := substRE (cycσ ce w) (cycRd ce w)\ndef trC (ce : CycEnc) (w : E) : C → C := substRC (cycσ ce w) (cycRd ce w)\n\ndef cycStepped (ce : CycEnc) : C := .and (.le (.const 1) (.var ce.u)) (.le (.var ce.u) (.var ce.T))\ndef cycPrev (ce : CycEnc) : E := .sub (.var ce.u) (.const 1)\ndef cycCell (ce : CycEnc) (m : Nat) (w : E) : E := cycRd ce w m (.var ce.k)\ndef cycStepTerm (s : SMachine) (x : Nat) : E := (assoc s.f.m.step x).getD (.var x)\n\ndef cycStart (s : SMachine) (ce : CycEnc) : Fact :=\n ⟨[ce.u], .or (.not (.le (.var ce.u) (.const 0))) (andL (trueC :: s.f.m.init.map fun p => .eq (trE ce (.var ce.u) (.var p.1)) p.2))⟩\ndef cycStep (s : SMachine) (ce : CycEnc) (withAssume : Bool) : Fact :=\n ⟨[ce.u], .or (.not (cycStepped ce)) (andL (trueC :: ((if withAssume then [trC ce (cycPrev ce) (inlineC s.f.flags s.f.m.assume)] else [])\n ++ s.f.m.init.map fun p => .eq (trE ce (.var ce.u) (.var p.1)) (trE ce (cycPrev ce) (inlineE s.f.flags (cycStepTerm s p.1))))))⟩\n-- A map's cell k after a step: a write at every key, its value at k; a write at one key, where k is the key, its value\n-- read at k; else the cell before.\ndef cycNext (s : SMachine) (ce : CycEnc) (m : Nat) : E :=\n match lookup (fun w => w.1 == m) s.every with\n | some w => trE ce (cycPrev ce) (inlineE s.f.flags (substE [(w.2.1, .var ce.k)] w.2.2))\n | none => match lookup (fun w => w.1 == m) s.f.m.writes with\n | some w => .ite (.eq (.var ce.k) (trE ce (cycPrev ce) (inlineE s.f.flags w.2.1)))\n (trE ce (cycPrev ce) (readsAtE (inlineE s.f.flags w.2.1) (.var ce.k) (inlineE s.f.flags w.2.2))) (cycCell ce m (cycPrev ce))\n | none => cycCell ce m (cycPrev ce)\ndef cycMapFacts (s : SMachine) (ce : CycEnc) : List Fact := s.f.m.maps0.flatMap fun p =>\n [⟨[ce.u, ce.k], .or (.not (.le (.var ce.u) (.const 0))) (.eq (cycCell ce p.1 (.var ce.u)) (.const p.2))⟩,\n ⟨[ce.u, ce.k], .or (.not (cycStepped ce)) (.eq (cycCell ce p.1 (.var ce.u)) (cycNext s ce p.1))⟩]\n-- The functions' facts, each with its further keys.\ndef cycGiven (s : SMachine) : List (MLaw × List Nat) := s.facts.map (fun l => (l, [])) ++ s.pfacts\ndef cycFnFacts (s : SMachine) : List Fact := (cycGiven s).map fun p => ⟨p.1.key.toList ++ p.2, p.1.holds⟩\n-- The step with the assumption and without it (core/cyclic.ts leaves it out when the machine has none).\ndef cycCanon (s : SMachine) (ce : CycEnc) : List Fact :=\n cycStart s ce :: cycStep s ce true :: cycStep s ce false :: (cycMapFacts s ce ++ cycFnFacts s)\n\n/-! The check -/\n\ndef cycZ (ce : CycEnc) (keys : List Nat) : List Nat := ce.special ++ keys\ndef cycP (s : SMachine) (ce : CycEnc) (keys : List Nat) : List Nat := cycZ ce keys ++ ce.vtr.map (·.1) ++ flagVars s.f.flags\n\n-- The encoding fits the machine: every variable a step changes has its map over time, every map a step or a request\n-- changes has its own, those maps are fresh, and the added variables are apart from the machine's.\ndef cycShapeOk (s : SMachine) (ce : CycEnc) (keys : List Nat) : Bool :=\n s.qflags.isEmpty && flagsOk s.f.flags && distinct ce.special && distinct ce.tm\n && (cycZ ce keys).all (fun z => !(ce.vtr.map (·.1)).contains z && !(flagVars s.f.flags).contains z)\n && keys.all (fun z => !ce.special.contains z)\n && ce.tm.all (fun m => !(ce.mtr.map (·.1)).contains m)\n && s.f.m.init.all (fun p => (ce.vtr.map (·.1)).contains p.1 && !s.f.m.inputs.contains p.1)\n && s.f.m.step.all (fun p => (ce.vtr.map (·.1)).contains p.1)\n && s.f.m.inputs.all (fun x => (ce.vtr.map (·.1)).contains x)\n && distinct (s.f.m.init.map (·.1))\n && s.f.m.maps0.all (fun p => (ce.mtr.map (·.1)).contains p.1 && !s.carried.contains p.1)\n && s.carried.all (fun m => (ce.mtr.map (·.1)).contains m)\n && s.f.m.writes.all (fun w => (ce.mtr.map (·.1)).contains w.1)\n && s.every.all (fun w => (ce.mtr.map (·.1)).contains w.1)\n\n-- The machine's terms read none of the added variables nor the maps over time; what is read at the start (the start\n-- terms, the precondition, the functions' facts) reads no variable a step changes either.\ndef cycTermsOk (s : SMachine) (ce : CycEnc) (keys : List Nat) : Bool :=\n s.f.flags.all (fun p => avoids (cycZ ce keys) p.2 && avoidsMaps ce.tm p.2)\n && s.f.m.step.all (fun p => avoidsE (cycZ ce keys) p.2 && avoidsMapsE ce.tm p.2)\n && s.f.m.writes.all (fun w => avoidsE (cycZ ce keys) w.2.1 && avoidsMapsE ce.tm w.2.1 && avoidsE (cycZ ce keys) w.2.2 && avoidsMapsE ce.tm w.2.2)\n && s.every.all (fun w => (cycZ ce keys).all (fun z => z == w.2.1 || !occE z w.2.2) && avoidsMapsE ce.tm w.2.2)\n && avoids (cycZ ce keys) s.f.m.assume && avoidsMaps ce.tm s.f.m.assume\n && s.f.m.init.all (fun p => avoidsE (cycP s ce keys) p.2 && avoidsMapsE ce.tm p.2)\n && avoids (cycP s ce keys) s.f.m.pre && avoidsMaps ce.tm s.f.m.pre\n && (cycGiven s).all (fun g => (cycP s ce keys).all (fun z => g.1.key == some z || g.2.contains z || !occC z g.1.holds)\n && avoidsMaps ce.tm g.1.holds)\n\n-- The law reads no added variable, no input, no map over time and no map a request carries.\ndef cycLawOk (s : SMachine) (ce : CycEnc) (law : C) : Bool :=\n avoids (ce.special ++ s.f.m.inputs) law && avoidsMaps (ce.tm ++ s.carried) law\n\ndef factBeq (f g : Fact) : Bool := f.keys == g.keys && beqC f.holds g.holds\n\n-- Each exported fact is one of the encoding's, or true of every natural.\ndef cycFactsIn (s : SMachine) (ce : CycEnc) (facts : List Fact) : Bool :=\n facts.all fun f => natC f.holds || (cycCanon s ce).any (factBeq f)\n\n-- The node: its conclusion the law at time t, its hypotheses among the precondition, t ≤ T and T < Ω.\ndef cycNodeOk (s : SMachine) (ce : CycEnc) (law : C) (n : CNode) : Bool :=\n beqC n.concl (trC ce (.var ce.t) law)\n && n.hyps.all (fun h => beqC h s.f.m.pre || beqC h (.le (.var ce.t) (.var ce.T)) || beqC h (.lt (.var ce.T) (.var ce.om)))\n\ndef cycMOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (law : C) (keys : List Nat) (n : CNode) : Bool :=\n cycShapeOk s ce keys && cycTermsOk s ce keys && cycLawOk s ce law && cycFactsIn s ce facts && cycNodeOk s ce law n\n\n/-! The checks, as propositions -/\n\ntheorem and_parts {a b : Bool} (h : (a && b) = true) : a = true ∧ b = true := by\n cases a <;> cases b <;> simp_all\n\ntheorem mem_toList {o : Option Nat} {j : Nat} (h : j ∈ o.toList) : o = some j := by\n cases o <;> simp_all [Option.toList]\n\ntheorem isEmpty_nil {α : Type} {l : List α} (h : l.isEmpty = true) : l = [] := by\n cases l\n · rfl\n · simp [List.isEmpty] at h\n\nstructure CycShape (s : SMachine) (ce : CycEnc) (keys : List Nat) : Prop where\n q : s.qflags = []\n fl : flagsOk s.f.flags = true\n dsp : distinct ce.special = true\n dtm : distinct ce.tm = true\n zv : ∀ z ∈ cycZ ce keys, z ∉ ce.vtr.map (·.1)\n zf : ∀ z ∈ cycZ ce keys, z ∉ flagVars s.f.flags\n ks : ∀ z ∈ keys, z ∉ ce.special\n tmd : ∀ m ∈ ce.tm, m ∉ ce.mtr.map (·.1)\n initv : ∀ p ∈ s.f.m.init, p.1 ∈ ce.vtr.map (·.1)\n initin : ∀ p ∈ s.f.m.init, p.1 ∉ s.f.m.inputs\n stepv : ∀ p ∈ s.f.m.step, p.1 ∈ ce.vtr.map (·.1)\n inv : ∀ x ∈ s.f.m.inputs, x ∈ ce.vtr.map (·.1)\n dinit : distinct (s.f.m.init.map (·.1)) = true\n m0 : ∀ p ∈ s.f.m.maps0, p.1 ∈ ce.mtr.map (·.1)\n m0c : ∀ p ∈ s.f.m.maps0, p.1 ∉ s.carried\n carm : ∀ m ∈ s.carried, m ∈ ce.mtr.map (·.1)\n wrm : ∀ w ∈ s.f.m.writes, w.1 ∈ ce.mtr.map (·.1)\n evm : ∀ w ∈ s.every, w.1 ∈ ce.mtr.map (·.1)\n\ntheorem cycShape_of {s : SMachine} {ce : CycEnc} {keys : List Nat} (h : cycShapeOk s ce keys = true) : CycShape s ce keys := by\n simp only [cycShapeOk, Bool.and_eq_true] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hq, hfl⟩, hdsp⟩, hdtm⟩, hz⟩, hks⟩, htm⟩, hinit⟩, hstep⟩, hin⟩, hdi⟩, hm0⟩, hcar⟩, hwr⟩, hev⟩ := h\n have hz' := List.all_eq_true.mp hz\n have hinit' := List.all_eq_true.mp hinit\n have hm0' := List.all_eq_true.mp hm0\n refine ⟨isEmpty_nil hq, hfl, hdsp, hdtm, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, hdi, ?_, ?_, ?_, ?_, ?_⟩\n · intro z hzm; exact not_mem_of_not_contains (and_parts (hz' z hzm)).1\n · intro z hzm; exact not_mem_of_not_contains (and_parts (hz' z hzm)).2\n · intro z hzm; exact not_mem_of_not_contains (List.all_eq_true.mp hks z hzm)\n · intro m hm; exact not_mem_of_not_contains (List.all_eq_true.mp htm m hm)\n · intro p hp; exact List.contains_iff_mem.mp (and_parts (hinit' p hp)).1\n · intro p hp; exact not_mem_of_not_contains (and_parts (hinit' p hp)).2\n · intro p hp; exact List.contains_iff_mem.mp (List.all_eq_true.mp hstep p hp)\n · intro x hx; exact List.contains_iff_mem.mp (List.all_eq_true.mp hin x hx)\n · intro p hp; exact List.contains_iff_mem.mp (and_parts (hm0' p hp)).1\n · intro p hp; exact not_mem_of_not_contains (and_parts (hm0' p hp)).2\n · intro m hm; exact List.contains_iff_mem.mp (List.all_eq_true.mp hcar m hm)\n · intro w hw; exact List.contains_iff_mem.mp (List.all_eq_true.mp hwr w hw)\n · intro w hw; exact List.contains_iff_mem.mp (List.all_eq_true.mp hev w hw)\n\nstructure CycTerms (s : SMachine) (ce : CycEnc) (keys : List Nat) : Prop where\n flz : ∀ p ∈ s.f.flags, avoids (cycZ ce keys) p.2 = true\n flm : ∀ p ∈ s.f.flags, avoidsMaps ce.tm p.2 = true\n stz : ∀ p ∈ s.f.m.step, avoidsE (cycZ ce keys) p.2 = true\n stm : ∀ p ∈ s.f.m.step, avoidsMapsE ce.tm p.2 = true\n wkz : ∀ w ∈ s.f.m.writes, avoidsE (cycZ ce keys) w.2.1 = true\n wkm : ∀ w ∈ s.f.m.writes, avoidsMapsE ce.tm w.2.1 = true\n wvz : ∀ w ∈ s.f.m.writes, avoidsE (cycZ ce keys) w.2.2 = true\n wvm : ∀ w ∈ s.f.m.writes, avoidsMapsE ce.tm w.2.2 = true\n evz : ∀ w ∈ s.every, ∀ z ∈ cycZ ce keys, z ≠ w.2.1 → occE z w.2.2 = false\n evm : ∀ w ∈ s.every, avoidsMapsE ce.tm w.2.2 = true\n asz : avoids (cycZ ce keys) s.f.m.assume = true\n asm : avoidsMaps ce.tm s.f.m.assume = true\n inz : ∀ p ∈ s.f.m.init, avoidsE (cycP s ce keys) p.2 = true\n inm : ∀ p ∈ s.f.m.init, avoidsMapsE ce.tm p.2 = true\n prz : avoids (cycP s ce keys) s.f.m.pre = true\n prm : avoidsMaps ce.tm s.f.m.pre = true\n gz : ∀ g ∈ cycGiven s, ∀ z ∈ cycP s ce keys, g.1.key ≠ some z → z ∉ g.2 → occC z g.1.holds = false\n gm : ∀ g ∈ cycGiven s, avoidsMaps ce.tm g.1.holds = true\n\ntheorem cycTerms_of {s : SMachine} {ce : CycEnc} {keys : List Nat} (h : cycTermsOk s ce keys = true) : CycTerms s ce keys := by\n simp only [cycTermsOk, Bool.and_eq_true] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨hfl, hst⟩, hw⟩, hev⟩, has⟩, ham⟩, hin⟩, hpz⟩, hpm⟩, hg⟩ := h\n have hfl' := List.all_eq_true.mp hfl\n have hst' := List.all_eq_true.mp hst\n have hw' := List.all_eq_true.mp hw\n have hev' := List.all_eq_true.mp hev\n have hin' := List.all_eq_true.mp hin\n have hg' := List.all_eq_true.mp hg\n refine ⟨fun p hp => (and_parts (hfl' p hp)).1, fun p hp => (and_parts (hfl' p hp)).2,\n fun p hp => (and_parts (hst' p hp)).1, fun p hp => (and_parts (hst' p hp)).2,\n fun w hw => (and_parts (and_parts (and_parts (hw' w hw)).1).1).1, fun w hw => (and_parts (and_parts (and_parts (hw' w hw)).1).1).2,\n fun w hw => (and_parts (and_parts (hw' w hw)).1).2, fun w hw => (and_parts (hw' w hw)).2,\n fun w hw z hz hne => ?_, fun w hw => (and_parts (hev' w hw)).2,\n has, ham, fun p hp => (and_parts (hin' p hp)).1, fun p hp => (and_parts (hin' p hp)).2, hpz, hpm,\n fun g hg z hz hk hgs => ?_, fun g hg => (and_parts (hg' g hg)).2⟩\n · have := List.all_eq_true.mp (and_parts (hev' w hw)).1 z hz\n cases hb : (z == w.2.1)\n · rw [hb] at this; simpa using this\n · exact absurd (beq_iff_eq.mp hb) hne\n · have := List.all_eq_true.mp (and_parts (hg' g hg)).1 z hz\n have h1 : (g.1.key == some z) = false := by\n cases hb : (g.1.key == some z)\n · rfl\n · exact absurd (beq_iff_eq.mp hb) hk\n have h2 : g.2.contains z = false := by\n cases hb : g.2.contains z\n · rfl\n · exact absurd (List.contains_iff_mem.mp hb) hgs\n rw [h1, h2] at this; simpa using this\n\n/-! A run, step by step -/\n\nstructure CRun where\n ρ0 : Nat → Nat -- before the start\n st : Nat → Nat → Nat -- the state at each time\n mp : Nat → Nat → Nat → Nat -- the maps at each time\n ι : Nat → Nat → Nat -- the request taken at each time\n κ : Nat → Nat → Nat → Nat -- the maps it carries\n\ndef CRun.N (s : SMachine) (r : CRun) (u : Nat) : Nat → Nat → Nat := withCarried s (r.mp u) (r.κ u)\ndef CRun.X (s : SMachine) (r : CRun) (u : Nat) : Nat → Nat := withInputs s.f.m.inputs (r.st u) (r.ι u)\ndef CRun.V (s : SMachine) (r : CRun) (u : Nat) : Nat → Nat := decideFlags s.f.flags (r.X s u) (r.N s u)\n\n-- A run of T steps from a start SReach allows.\nstructure CRun.Ok (s : SMachine) (r : CRun) (T : Nat) : Prop where\n maps0 : StartMaps s.f.m (r.mp 0)\n pre : evalC r.ρ0 (r.mp 0) s.f.m.pre = true\n facts : ∀ l ∈ s.facts, l.Holds r.ρ0 (r.mp 0)\n pfacts : s.PFacts r.ρ0 (r.mp 0)\n init : r.st 0 = applyS s.f.m.init r.ρ0 (r.mp 0)\n step : ∀ u, u < T → evalC (r.V s u) (r.N s u) s.f.m.assume = true ∧ r.st (u + 1) = applyS s.f.m.step (r.V s u) (r.N s u)\n ∧ r.mp (u + 1) = everyMaps s (r.V s u) (r.N s u)\n\n-- The run with one more step.\ndef CRun.snoc (r : CRun) (T : Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : CRun :=\n ⟨r.ρ0, fun u => if u ≤ T then r.st u else ρ, fun u => if u ≤ T then r.mp u else M,\n fun u => if u < T then r.ι u else ι, fun u => if u < T then r.κ u else κ⟩\n\ntheorem decideQ_nil {s : SMachine} (hq : s.qflags = []) (X : Nat → Nat) (N : Nat → Nat → Nat) : decideQ s X N = X := by\n funext v; simp only [decideQ, hq, lookup]\n\ntheorem snoc_early {s : SMachine} {r : CRun} {T u : Nat} (hu : u < T) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat)\n (κ : Nat → Nat → Nat) : (r.snoc T ρ M ι κ).N s u = r.N s u ∧ (r.snoc T ρ M ι κ).V s u = r.V s u\n ∧ (r.snoc T ρ M ι κ).st (u + 1) = r.st (u + 1) ∧ (r.snoc T ρ M ι κ).mp (u + 1) = r.mp (u + 1) := by\n have h1 : u ≤ T := Nat.le_of_lt hu\n have h2 : u + 1 ≤ T := hu\n refine ⟨?_, ?_, ?_, ?_⟩ <;> simp only [CRun.V, CRun.X, CRun.N, CRun.snoc, if_pos h1, if_pos hu, if_pos h2]\n\ntheorem snoc_zero {r : CRun} {T : Nat} (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n (r.snoc T ρ M ι κ).ρ0 = r.ρ0 ∧ (r.snoc T ρ M ι κ).st 0 = r.st 0 ∧ (r.snoc T ρ M ι κ).mp 0 = r.mp 0 := by\n refine ⟨rfl, ?_, ?_⟩ <;> simp only [CRun.snoc, if_pos (Nat.zero_le T)]\n\ntheorem snoc_last {r : CRun} {T : Nat} (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n (r.snoc T ρ M ι κ).st (T + 1) = ρ ∧ (r.snoc T ρ M ι κ).mp (T + 1) = M ∧ (r.snoc T ρ M ι κ).st T = r.st T\n ∧ (r.snoc T ρ M ι κ).mp T = r.mp T ∧ (r.snoc T ρ M ι κ).ι T = ι ∧ (r.snoc T ρ M ι κ).κ T = κ := by\n have h1 : ¬ T + 1 ≤ T := by omega\n have h2 : ¬ T < T := Nat.lt_irrefl T\n refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ <;> simp only [CRun.snoc, if_neg h1, if_neg h2, if_pos (Nat.le_refl T)]\n\n-- A reachable state is the end of a run (no flag holds at every key).\ntheorem reach_run {s : SMachine} (hq : s.qflags = []) :\n ∀ {ρ M}, SReach s ρ M → ∃ (r : CRun) (T : Nat), r.Ok s T ∧ r.st T = ρ ∧ r.mp T = M := by\n intro ρ M h\n induction h with\n | start ρ M hM hp hf hpf =>\n exact ⟨⟨ρ, fun _ => applyS s.f.m.init ρ M, fun _ => M, fun _ => ρ, fun _ => M⟩, 0,\n ⟨hM, hp, hf, hpf, rfl, fun u hu => absurd hu (Nat.not_lt_zero u)⟩, rfl, rfl⟩\n | step ρ M ι κ _ ha ih =>\n obtain ⟨r, T, hr, hst, hmp⟩ := ih\n have hv : sView s ρ M ι κ = (decideFlags s.f.flags (withInputs s.f.m.inputs ρ ι) (withCarried s M κ), withCarried s M κ) := by\n simp only [sView, decideQ_nil hq]\n obtain ⟨l1, l2, l3, l4, l5, l6⟩ := snoc_last (r := r) (T := T) (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n obtain ⟨z1, z2, z3⟩ := snoc_zero (r := r) (T := T) (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n have hN : (r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ).N s T = (sView s ρ M ι κ).2 := by\n rw [hv, CRun.N, l4, l6, hmp]\n have hV : (r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ).V s T = (sView s ρ M ι κ).1 := by\n rw [CRun.V, hN, CRun.X, l3, l5, hst, hv]\n refine ⟨r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ, T + 1, ⟨?_, ?_, ?_, ?_, ?_, ?_⟩, l1, l2⟩\n · rw [z3]; exact hr.maps0\n · rw [z1, z3]; exact hr.pre\n · rw [z1, z3]; exact hr.facts\n · rw [z1, z3]; exact hr.pfacts\n · rw [z1, z2, z3]; exact hr.init\n · intro u hu\n rcases Nat.lt_or_ge u T with hlt | hge\n · obtain ⟨e1, e2, e3, e4⟩ := snoc_early (s := s) (r := r) hlt (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n rw [e1, e2, e3, e4]; exact hr.step u hlt\n · have hu' : u = T := by omega\n rw [hu', hV, hN, l1, l2]\n exact ⟨ha, rfl, rfl⟩\n\nsection run\nvariable {s : SMachine} {r : CRun} {T : Nat} (hr : r.Ok s T)\ninclude hr\n\n-- What no step changes keeps its value from before the start.\ntheorem run_frame : ∀ u, u ≤ T → ∀ i, i ∉ s.f.m.init.map (·.1) → i ∉ s.f.m.step.map (·.1) → i ∉ s.f.m.inputs →\n i ∉ flagVars s.f.flags → r.st u i = r.ρ0 i := by\n intro u\n induction u with\n | zero => intro _ i hi _ _ _; rw [hr.init]; exact applyS_other hi _ _\n | succ u ih =>\n intro hu i hi hs hin hf\n rw [(hr.step u (by omega)).2.1, applyS_other hs, CRun.V, decide_other hf, CRun.X, withInputs_other hin]\n exact ih (by omega) i hi hs hin hf\n\n-- A map no step writes and no request carries keeps its cells.\ntheorem run_maps {D : List Nat} (hc : ∀ m ∈ s.carried, m ∈ D) (hw : ∀ w ∈ s.f.m.writes, w.1 ∈ D) (he : ∀ w ∈ s.every, w.1 ∈ D) :\n ∀ u, u ≤ T → ∀ m, m ∉ D → r.mp u m = r.mp 0 m := by\n intro u\n induction u with\n | zero => intros; rfl\n | succ u ih =>\n intro hu m hm\n rw [(hr.step u (by omega)).2.2]\n funext key\n have hne : lookup (fun w => w.1 == m) s.every = none :=\n lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro h; exact hm (h ▸ he w hw'))\n have hnw : lookup (fun w => w.1 == m) s.f.m.writes = none :=\n lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro h; exact hm (h ▸ hw w hw'))\n have hnc : ¬ s.carried.contains m = true := fun h => hm (hc m (List.contains_iff_mem.mp h))\n simp only [everyMaps, hne, writeMaps, hnw, CRun.N, withCarried]\n rw [if_neg hnc]\n exact congrFun (ih (by omega) m hm) key\nend run\n\ntheorem N_not_carried {s : SMachine} {r : CRun} {u m : Nat} (hm : m ∉ s.carried) : r.N s u m = r.mp u m := by\n simp only [CRun.N, withCarried]\n rw [if_neg (fun h => hm (List.contains_iff_mem.mp h))]\n\n/-! The run as maps over time -/\n\ndef cycMaps (s : SMachine) (ce : CycEnc) (r : CRun) (om : Nat) : Nat → Nat → Nat := fun i =>\n match lookup (fun p => p.2 == i) ce.vtr with\n | some p => fun u => r.X s u p.1\n | none => match lookup (fun p => p.2 == i) ce.mtr with\n | some p => fun y => r.N s (y % om) p.1 (y / om)\n | none => r.mp 0 i\n\n-- The maps a step at time u reads, as the encoding gives them: the maps over time as they are, the rest as the run's.\ndef cycView (s : SMachine) (ce : CycEnc) (r : CRun) (om u : Nat) : Nat → Nat → Nat := fun m =>\n if ce.tm.contains m then cycMaps s ce r om m else r.N s u m\n\n-- The node's environment: t and T the run's length, Ω one above it.\ndef cycEnv (ce : CycEnc) (T : Nat) (ρ' : Nat → Nat) : Nat → Nat := fun i =>\n if i = ce.t then T else if i = ce.T then T else if i = ce.om then T + 1 else ρ' i\n\n-- An environment a fact is read at: T and Ω as the node's, and from before the start wherever no step reaches.\ndef EnvOk (s : SMachine) (ce : CycEnc) (keys : List Nat) (r : CRun) (T : Nat) (Q : Nat → Nat) : Prop :=\n Q ce.T = T ∧ Q ce.om = T + 1 ∧ ∀ i, i ∉ cycP s ce keys → Q i = r.ρ0 i\n\nsection enc\nvariable {s : SMachine} {ce : CycEnc} {keys : List Nat} {r : CRun} {T : Nat}\n\ntheorem tm_parts (hd : distinct ce.tm = true) :\n distinct (ce.vtr.map (·.2)) = true ∧ distinct (ce.mtr.map (·.2)) = true ∧ ∀ a ∈ ce.vtr.map (·.2), a ∉ ce.mtr.map (·.2) :=\n distinct_append hd\n\ntheorem cm_v (hd : distinct ce.tm = true) {om : Nat} {p : Nat × Nat} (hp : p ∈ ce.vtr) :\n cycMaps s ce r om p.2 = fun u => r.X s u p.1 := by\n simp only [cycMaps, lookup_ren (tm_parts hd).1 p hp]\n\ntheorem cm_m (hd : distinct ce.tm = true) {om : Nat} {p : Nat × Nat} (hp : p ∈ ce.mtr) :\n cycMaps s ce r om p.2 = fun y => r.N s (y % om) p.1 (y / om) := by\n obtain ⟨_, h2, h3⟩ := tm_parts hd\n have hn : lookup (fun q => q.2 == p.2) ce.vtr = none := lookup_none (fun q hq => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact h3 q.2 (List.mem_map.mpr ⟨q, hq, rfl⟩) (List.mem_map.mpr ⟨p, hp, he.symm⟩))\n simp only [cycMaps, hn, lookup_ren h2 p hp]\n\ntheorem cm_o {om i : Nat} (hi : i ∉ ce.tm) : cycMaps s ce r om i = r.mp 0 i := by\n have h1 : lookup (fun q => q.2 == i) ce.vtr = none := lookup_none (fun q hq => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact hi (List.mem_append_left _ (List.mem_map.mpr ⟨q, hq, he⟩)))\n have h2 : lookup (fun q => q.2 == i) ce.mtr = none := lookup_none (fun q hq => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact hi (List.mem_append_right _ (List.mem_map.mpr ⟨q, hq, he⟩)))\n simp only [cycMaps, h1, h2]\n\ntheorem cv_N {om u m : Nat} (hm : m ∉ ce.tm) : cycView s ce r om u m = r.N s u m := by\n simp only [cycView]\n rw [if_neg (fun h => hm (List.contains_iff_mem.mp h))]\n\ntheorem notP {i : Nat} (h : i ∉ cycP s ce keys) :\n i ∉ ce.special ∧ i ∉ keys ∧ i ∉ ce.vtr.map (·.1) ∧ i ∉ flagVars s.f.flags := by\n simp only [cycP, cycZ, List.mem_append, not_or] at h\n exact ⟨h.1.1.1, h.1.1.2, h.1.2, h.2⟩\n\ntheorem special_parts (h : distinct ce.special = true) :\n ce.u ≠ ce.T ∧ ce.u ≠ ce.om ∧ ce.k ≠ ce.T ∧ ce.k ≠ ce.om ∧ ce.T ≠ ce.om ∧ ce.t ≠ ce.T ∧ ce.t ≠ ce.om := by\n have h0 : distinct [ce.u, ce.k, ce.T, ce.om, ce.t] = true := h\n obtain ⟨n1, d1⟩ := distinct_cons h0\n obtain ⟨n2, d2⟩ := distinct_cons d1\n obtain ⟨n3, d3⟩ := distinct_cons d2\n obtain ⟨n4, _⟩ := distinct_cons d3\n refine ⟨fun e => n1 (by rw [e]; simp), fun e => n1 (by rw [e]; simp), fun e => n2 (by rw [e]; simp),\n fun e => n2 (by rw [e]; simp), fun e => n3 (by rw [e]; simp), fun e => n3 (by rw [← e]; simp), fun e => n4 (by rw [← e]; simp)⟩\n\ntheorem in_special {i : Nat} (h : i = ce.u ∨ i = ce.k ∨ i = ce.T ∨ i = ce.om ∨ i = ce.t) : i ∈ ce.special := by\n simp only [CycEnc.special, List.mem_cons, List.not_mem_nil, or_false]; exact h\n\n-- Flags decided at two frames that agree off Z (and off the flags), over maps that agree off TM.\ntheorem decide_view {fl : List (Nat × C)} (hfl : flagsOk fl = true) {Z TM : List Nat} (hz : ∀ p ∈ fl, avoids Z p.2 = true)\n (hm : ∀ p ∈ fl, avoidsMaps TM p.2 = true) {Y X : Nat → Nat} {V N : Nat → Nat → Nat}\n (hY : ∀ j, j ∉ Z → j ∉ flagVars fl → Y j = X j) (hV : ∀ m, m ∉ TM → V m = N m) {i : Nat} (hi : i ∉ Z) :\n decideFlags fl Y V i = decideFlags fl X N i := by\n simp only [decideFlags, applyS, assoc_flagSub]\n cases hc : flagOf fl i with\n | none => exact hY i hi (flagOf_none hc)\n | some c =>\n obtain ⟨p, hp, rfl⟩ := flagOf_some hc\n simp only [Option.map_some, evalE]\n rw [congr2C (ρ' := X) (M' := N) p.2 (fun j hj => hY j (avoids_occ (hz p hp) hj) (avoids_occ (List.all_eq_true.mp hfl p hp) hj))\n (fun m hm' => hV m (avoidsMaps_occ (hm p hp) hm'))]\n\nvariable (hS : CycShape s ce keys) (hT : CycTerms s ce keys) (hr : r.Ok s T)\ninclude hS hT hr\n\ntheorem enc_maps {u : Nat} (hu : u ≤ T) {m : Nat} (hm : m ∉ ce.mtr.map (·.1)) : r.N s u m = r.mp 0 m := by\n rw [N_not_carried (fun h => hm (hS.carm m h))]\n exact run_maps hr hS.carm hS.wrm hS.evm u hu m hm\n\ntheorem enc_frame {u : Nat} (hu : u ≤ T) {i : Nat} (hv : i ∉ ce.vtr.map (·.1)) (hf : i ∉ flagVars s.f.flags) :\n r.X s u i = r.ρ0 i := by\n have hin : i ∉ s.f.m.inputs := fun h => hv (hS.inv i h)\n rw [CRun.X, withInputs_other hin]\n exact run_frame hr u hu i (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.stepv p hp)) hin hf\n\nvariable {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) {w : E} {u : Nat}\n (hw : evalE Q (cycMaps s ce r (T + 1)) w = u) (hu : u ≤ T)\ninclude hQ hw hu\n\n-- A read of a map at time w: the maps over time read back the run's.\ntheorem enc_rd : ∀ m key, evalE Q (cycMaps s ce r (T + 1)) (cycRd ce w m key)\n = cycView s ce r (T + 1) u m (evalE Q (cycMaps s ce r (T + 1)) key) := by\n intro m key\n simp only [cycRd]\n split\n · rename_i p hp\n obtain ⟨hpm, hpe⟩ := lookup_some hp\n have hpm' : p.1 = m := by simpa using hpe\n have hmd : m ∈ ce.mtr.map (·.1) := List.mem_map.mpr ⟨p, hpm, hpm'⟩\n have hmt : m ∉ ce.tm := fun h => hS.tmd m h hmd\n rw [cv_N hmt]\n simp only [evalE, cm_m hS.dtm hpm, hw, hQ.2.1, pair_mod hu, pair_div hu, hpm']\n · rename_i hn\n have hmd : m ∉ ce.mtr.map (·.1) := fun h => by\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h\n have := lookup_none_all hn p hp; simp at this\n simp only [evalE, cycView]\n by_cases ht : ce.tm.contains m = true\n · rw [if_pos ht]\n · rw [if_neg ht, cm_o (fun h => ht (List.contains_iff_mem.mpr h)), enc_maps hS hT hr hu hmd]\n\ntheorem enc_trE (e : E) : evalE Q (cycMaps s ce r (T + 1)) (trE ce w e)\n = evalE (applyS (cycσ ce w) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u) e :=\n substR_evalE (cycσ ce w) (cycRd ce w) Q (cycMaps s ce r (T + 1)) (cycView s ce r (T + 1) u) (enc_rd hS hT hr hQ hw hu) e\ntheorem enc_trC (c : C) : evalC Q (cycMaps s ce r (T + 1)) (trC ce w c)\n = evalC (applyS (cycσ ce w) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u) c :=\n substR_evalC (cycσ ce w) (cycRd ce w) Q (cycMaps s ce r (T + 1)) (cycView s ce r (T + 1) u) (enc_rd hS hT hr hQ hw hu) c\n\ntheorem enc_σ_in {i : Nat} (hi : i ∈ ce.vtr.map (·.1)) : applyS (cycσ ce w) Q (cycMaps s ce r (T + 1)) i = r.X s u i := by\n cases h : lookup (fun p => p.1 == i) ce.vtr with\n | none =>\n exfalso; obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hi\n have := lookup_none_all h p hp; simp at this\n | some q =>\n obtain ⟨hq, hqi⟩ := lookup_some h\n have hqi' : q.1 = i := by simpa using hqi\n have ha : assoc (cycσ ce w) i = some (.get q.2 w) := by\n simp only [assoc, cycσ, lookup_map_pred, h, Option.map_some]\n simp only [applyS, ha, evalE, cm_v hS.dtm hq, hw, hqi']\n\ntheorem enc_σ_out {i : Nat} (hi : i ∉ ce.vtr.map (·.1)) : applyS (cycσ ce w) Q (cycMaps s ce r (T + 1)) i = Q i := by\n apply applyS_other\n intro h; apply hi\n obtain ⟨a, ha, rfl⟩ := List.mem_map.mp h\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp ha\n exact List.mem_map.mpr ⟨p, hp, rfl⟩\n\n-- The frame a traced term reads: the run's state and request at time u, off the added variables and the flags.\ntheorem enc_Y {i : Nat} (hz : i ∉ cycZ ce keys) (hf : i ∉ flagVars s.f.flags) :\n applyS (cycσ ce w) Q (cycMaps s ce r (T + 1)) i = r.X s u i := by\n by_cases hv : i ∈ ce.vtr.map (·.1)\n · exact enc_σ_in hS hT hr hQ hw hu hv\n · rw [enc_σ_out hS hT hr hQ hw hu hv, hQ.2.2 i (by\n simp only [cycP, List.mem_append, not_or]; exact ⟨⟨hz, hv⟩, hf⟩), enc_frame hS hT hr hu hv hf]\n\ntheorem enc_D {i : Nat} (hz : i ∉ cycZ ce keys) :\n decideFlags s.f.flags (applyS (cycσ ce w) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u) i = r.V s u i :=\n decide_view hS.fl hT.flz hT.flm (fun _ hj hf => enc_Y hS hT hr hQ hw hu hj hf) (fun m hm => cv_N hm) hz\n\n-- A machine term, its flags inlined, read at the frame of time w: the term at the run's step from u.\ntheorem enc_inl (e : E) (hz : avoidsE (cycZ ce keys) e = true) (hm : avoidsMapsE ce.tm e = true) :\n evalE (applyS (cycσ ce w) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u) (inlineE s.f.flags e) = evalE (r.V s u) (r.N s u) e := by\n rw [inline_evalE]\n exact congr2E e (fun i hi => enc_D hS hT hr hQ hw hu (avoidsE_occ hz hi)) (fun m hm' => cv_N (avoidsMapsE_occ hm hm'))\n\ntheorem enc_now (e : E) (hz : avoidsE (cycZ ce keys) e = true) (hm : avoidsMapsE ce.tm e = true) :\n evalE Q (cycMaps s ce r (T + 1)) (trE ce w (inlineE s.f.flags e)) = evalE (r.V s u) (r.N s u) e := by\n rw [enc_trE hS hT hr hQ hw hu, enc_inl hS hT hr hQ hw hu e hz hm]\n\ntheorem enc_nowC (c : C) (hz : avoids (cycZ ce keys) c = true) (hm : avoidsMaps ce.tm c = true) :\n evalC Q (cycMaps s ce r (T + 1)) (trC ce w (inlineC s.f.flags c)) = evalC (r.V s u) (r.N s u) c := by\n rw [enc_trC hS hT hr hQ hw hu, inline_evalC]\n exact congr2C c (fun i hi => enc_D hS hT hr hQ hw hu (avoids_occ hz hi)) (fun m hm' => cv_N (avoidsMaps_occ hm hm'))\nend enc\n\n/-! The facts hold of the run -/\n\ntheorem holds_any (l : MLaw) (M : Nat → Nat → Nat) : ∀ (gs : List Nat) (ρ : Nat → Nat), holdsOver l M gs ρ →\n ∀ X : Nat → Nat, (∀ j, occC j l.holds = true → j ∉ gs → l.key ≠ some j → X j = ρ j) → evalC X M l.holds = true\n | [], ρ, h, X, hX => by\n simp only [holdsOver, MLaw.Holds] at h\n split at h\n · rename_i hk\n rw [congrC X ρ M l.holds (fun j hj => hX j hj (by simp) (by rw [hk]; simp))]; exact h\n · rename_i kv hk\n rw [congrC X (upd ρ kv (X kv)) M l.holds (fun j hj => by\n simp only [upd]\n split\n · rename_i e; rw [e]\n · rename_i e; exact hX j hj (by simp) (by rw [hk]; intro h'; exact e (Option.some.inj h').symm))]\n exact h (X kv)\n | g :: gs, ρ, h, X, hX => by\n simp only [holdsOver] at h\n exact holds_any l M gs (upd ρ g (X g)) (h (X g)) X (fun j hj hgs hk => by\n simp only [upd]\n split\n · rename_i e; rw [e]\n · rename_i e\n exact hX j hj (fun hm => by rcases List.mem_cons.mp hm with h' | h'; exact e h'; exact hgs h') hk)\n\ntheorem cycNext_every {s : SMachine} {ce : CycEnc} {m : Nat} {w : Nat × Nat × E} (h : lookup (fun w => w.1 == m) s.every = some w) :\n cycNext s ce m = trE ce (cycPrev ce) (inlineE s.f.flags (substE [(w.2.1, .var ce.k)] w.2.2)) := by\n simp only [cycNext, h]\ntheorem cycNext_write {s : SMachine} {ce : CycEnc} {m : Nat} {w : Nat × E × E} (he : lookup (fun w => w.1 == m) s.every = none)\n (h : lookup (fun w => w.1 == m) s.f.m.writes = some w) :\n cycNext s ce m = .ite (.eq (.var ce.k) (trE ce (cycPrev ce) (inlineE s.f.flags w.2.1)))\n (trE ce (cycPrev ce) (readsAtE (inlineE s.f.flags w.2.1) (.var ce.k) (inlineE s.f.flags w.2.2))) (cycCell ce m (cycPrev ce)) := by\n simp only [cycNext, he, h]\ntheorem cycNext_none {s : SMachine} {ce : CycEnc} {m : Nat} (he : lookup (fun w => w.1 == m) s.every = none)\n (h : lookup (fun w => w.1 == m) s.f.m.writes = none) : cycNext s ce m = cycCell ce m (cycPrev ce) := by\n simp only [cycNext, he, h]\ntheorem everyMaps_every {s : SMachine} {D : Nat → Nat} {N : Nat → Nat → Nat} {m key : Nat} {w : Nat × Nat × E}\n (h : lookup (fun w => w.1 == m) s.every = some w) : everyMaps s D N m key = evalE (upd D w.2.1 key) N w.2.2 := by\n simp only [everyMaps, h]\ntheorem everyMaps_write {s : SMachine} {D : Nat → Nat} {N : Nat → Nat → Nat} {m key : Nat} {w : Nat × E × E}\n (he : lookup (fun w => w.1 == m) s.every = none) (h : lookup (fun w => w.1 == m) s.f.m.writes = some w) :\n everyMaps s D N m key = if key = evalE D N w.2.1 then evalE D N w.2.2 else N m key := by\n simp only [everyMaps, he, writeMaps, h]\ntheorem everyMaps_none {s : SMachine} {D : Nat → Nat} {N : Nat → Nat → Nat} {m key : Nat}\n (he : lookup (fun w => w.1 == m) s.every = none) (h : lookup (fun w => w.1 == m) s.f.m.writes = none) :\n everyMaps s D N m key = N m key := by\n simp only [everyMaps, he, writeMaps, h]\n\nsection facts\nvariable {s : SMachine} {ce : CycEnc} {keys : List Nat} {r : CRun} {T : Nat}\n (hS : CycShape s ce keys) (hT : CycTerms s ce keys) (hr : r.Ok s T)\ninclude hS hT hr\n\ntheorem stepTerm_avoids {x : Nat} (hx : x ∈ ce.vtr.map (·.1)) :\n avoidsE (cycZ ce keys) (cycStepTerm s x) = true ∧ avoidsMapsE ce.tm (cycStepTerm s x) = true := by\n simp only [cycStepTerm]\n cases h : assoc s.f.m.step x with\n | some e => exact ⟨hT.stz _ (assoc_some_mem h), hT.stm _ (assoc_some_mem h)⟩\n | none =>\n refine ⟨?_, ?_⟩\n · simp only [Option.getD, avoidsE, List.all_eq_true, occE, Bool.not_eq_true', beq_eq_false_iff_ne]\n intro z hz e; subst e; exact hS.zv _ hz hx\n · simp [Option.getD, avoidsMapsE, occME]\n\ntheorem given_holds {g : MLaw × List Nat} (hg : g ∈ cycGiven s) : holdsOver g.1 (r.mp 0) g.2 r.ρ0 := by\n simp only [cycGiven, List.mem_append, List.mem_map] at hg\n rcases hg with ⟨l, hl, rfl⟩ | hg\n · exact hr.facts l hl\n · exact hr.pfacts g hg\n\ntheorem start_holds {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) :\n evalC Q (cycMaps s ce r (T + 1)) (cycStart s ce).holds = true := by\n apply impl_true\n intro h0\n rw [evalC_iff] at h0; simp only [holdsC, evalE] at h0\n have hw : evalE Q (cycMaps s ce r (T + 1)) (.var ce.u) = 0 := by simp only [evalE]; omega\n apply andL_true\n intro c hc\n rcases List.mem_cons.mp hc with rfl | hc\n · simp [trueC, evalC, evalE]\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hc\n have ha : assoc s.f.m.init p.1 = some p.2 := by simp only [assoc, lookup_unique hS.dinit hp, Option.map_some]\n rw [evalC_iff]; simp only [holdsC]\n rw [enc_trE hS hT hr hQ hw (Nat.zero_le T)]\n show applyS (cycσ ce (.var ce.u)) Q (cycMaps s ce r (T + 1)) p.1 = _\n rw [enc_σ_in hS hT hr hQ hw (Nat.zero_le T) (hS.initv p hp), CRun.X, withInputs_other (hS.initin p hp), hr.init]\n simp only [applyS, ha]\n exact congr2E p.2 (fun j hj => (hQ.2.2 j (avoidsE_occ (hT.inz p hp) hj)).symm)\n (fun m hm => (cm_o (avoidsMapsE_occ (hT.inm p hp) hm)).symm)\n\ntheorem step_holds (b : Bool) {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) :\n evalC Q (cycMaps s ce r (T + 1)) (cycStep s ce b).holds = true := by\n apply impl_true\n intro h1\n rw [evalC_iff] at h1; simp only [cycStepped, holdsC, evalE] at h1\n obtain ⟨h1a, h1b⟩ := h1\n have hQT := hQ.1\n obtain ⟨u', hu'⟩ : ∃ u', Q ce.u = u' + 1 := ⟨Q ce.u - 1, by omega⟩\n have hlt : u' < T := by omega\n have hw : evalE Q (cycMaps s ce r (T + 1)) (cycPrev ce) = u' := by simp only [cycPrev, evalE]; omega\n have hw0 : evalE Q (cycMaps s ce r (T + 1)) (.var ce.u) = u' + 1 := hu'\n apply andL_true\n intro c hc\n rcases List.mem_cons.mp hc with rfl | hc\n · simp [trueC, evalC, evalE]\n rcases List.mem_append.mp hc with hc | hc\n · cases b\n · simp at hc\n · simp at hc\n subst hc\n rw [enc_nowC hS hT hr hQ hw (Nat.le_of_lt hlt) _ hT.asz hT.asm]\n exact (hr.step u' hlt).1\n · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hc\n have hx := hS.initv p hp\n rw [evalC_iff]; simp only [holdsC]\n rw [enc_trE hS hT hr hQ hw0 hlt, enc_now hS hT hr hQ hw (Nat.le_of_lt hlt) _ (stepTerm_avoids hS hT hr hx).1 (stepTerm_avoids hS hT hr hx).2]\n show applyS (cycσ ce (.var ce.u)) Q (cycMaps s ce r (T + 1)) p.1 = _\n rw [enc_σ_in hS hT hr hQ hw0 hlt hx, CRun.X, withInputs_other (hS.initin p hp), (hr.step u' hlt).2.1, applyS_getD]\n rfl\n\ntheorem mapStart_holds {p : Nat × Nat} (hp : p ∈ s.f.m.maps0) {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) :\n evalC Q (cycMaps s ce r (T + 1)) (.or (.not (.le (.var ce.u) (.const 0))) (.eq (cycCell ce p.1 (.var ce.u)) (.const p.2))) = true := by\n apply impl_true\n intro h0\n rw [evalC_iff] at h0; simp only [holdsC, evalE] at h0\n have hw : evalE Q (cycMaps s ce r (T + 1)) (.var ce.u) = 0 := by simp only [evalE]; omega\n rw [evalC_iff]; simp only [holdsC, cycCell]\n rw [enc_rd hS hT hr hQ hw (Nat.zero_le T), cv_N (fun h => hS.tmd _ h (hS.m0 p hp)), N_not_carried (hS.m0c p hp)]\n exact hr.maps0 p hp _\n\ntheorem mapStep_holds {p : Nat × Nat} (hp : p ∈ s.f.m.maps0) {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) :\n evalC Q (cycMaps s ce r (T + 1)) (.or (.not (cycStepped ce)) (.eq (cycCell ce p.1 (.var ce.u)) (cycNext s ce p.1))) = true := by\n apply impl_true\n intro h1\n rw [evalC_iff] at h1; simp only [cycStepped, holdsC, evalE] at h1\n obtain ⟨h1a, h1b⟩ := h1\n have hQT := hQ.1\n obtain ⟨u', hu'⟩ : ∃ u', Q ce.u = u' + 1 := ⟨Q ce.u - 1, by omega⟩\n have hlt : u' < T := by omega\n have hw : evalE Q (cycMaps s ce r (T + 1)) (cycPrev ce) = u' := by simp only [cycPrev, evalE]; omega\n have hw0 : evalE Q (cycMaps s ce r (T + 1)) (.var ce.u) = u' + 1 := hu'\n have hk : evalE Q (cycMaps s ce r (T + 1)) (.var ce.k) = Q ce.k := rfl\n have hmt : p.1 ∉ ce.tm := fun h => hS.tmd _ h (hS.m0 p hp)\n have hsp := special_parts hS.dsp\n have hkZ : ce.k ∈ cycZ ce keys := List.mem_append_left _ (in_special (Or.inr (Or.inl rfl)))\n -- the frame of time u' at k: the fact's own key\n have hYk : applyS (cycσ ce (cycPrev ce)) Q (cycMaps s ce r (T + 1)) ce.k = Q ce.k :=\n enc_σ_out hS hT hr hQ hw (Nat.le_of_lt hlt) (hS.zv _ hkZ)\n rw [evalC_iff]; simp only [holdsC, cycCell]\n rw [enc_rd hS hT hr hQ hw0 hlt, cv_N hmt, N_not_carried (hS.m0c p hp), (hr.step u' hlt).2.2, hk]\n by_cases hev : ∃ w, lookup (fun w => w.1 == p.1) s.every = some w\n · obtain ⟨w, he⟩ := hev\n obtain ⟨hwm, _⟩ := lookup_some he\n rw [cycNext_every he, everyMaps_every he, enc_trE hS hT hr hQ hw (Nat.le_of_lt hlt), inline_evalE, subst_evalE, applyS_single]\n have hD : evalE (decideFlags s.f.flags (applyS (cycσ ce (cycPrev ce)) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u'))\n (cycView s ce r (T + 1) u') (.var ce.k) = Q ce.k := by\n show decideFlags s.f.flags (applyS (cycσ ce (cycPrev ce)) Q (cycMaps s ce r (T + 1))) (cycView s ce r (T + 1) u') ce.k = Q ce.k\n rw [decide_other (hS.zf _ hkZ), hYk]\n rw [hD]\n refine congr2E w.2.2 (fun j hj => ?_) (fun m hm => (cv_N (avoidsMapsE_occ (hT.evm w hwm) hm)).symm)\n simp only [upd]\n split\n · rfl\n · rename_i hne\n have hjz : j ∉ cycZ ce keys := fun hz => by\n have := hT.evz w hwm j hz hne; rw [this] at hj; exact absurd hj (by decide)\n exact (enc_D hS hT hr hQ hw (Nat.le_of_lt hlt) hjz).symm\n · have he : lookup (fun w => w.1 == p.1) s.every = none := by\n cases h : lookup (fun w => w.1 == p.1) s.every\n · rfl\n · exact absurd ⟨_, h⟩ hev\n by_cases hwr' : ∃ w, lookup (fun w => w.1 == p.1) s.f.m.writes = some w\n · obtain ⟨w, hwr⟩ := hwr'\n obtain ⟨hwm, _⟩ := lookup_some hwr\n rw [cycNext_write he hwr, everyMaps_write he hwr]\n rw [evalE_ite_eq, hk, enc_now hS hT hr hQ hw (Nat.le_of_lt hlt) _ (hT.wkz w hwm) (hT.wkm w hwm)]\n by_cases hkk : Q ce.k = evalE (r.V s u') (r.N s u') w.2.1\n · rw [if_pos hkk, if_pos hkk, enc_trE hS hT hr hQ hw (Nat.le_of_lt hlt), readsAt_evalE]\n · exact (enc_inl hS hT hr hQ hw (Nat.le_of_lt hlt) _ (hT.wvz w hwm) (hT.wvm w hwm)).symm\n · show applyS (cycσ ce (cycPrev ce)) Q (cycMaps s ce r (T + 1)) ce.k = _\n rw [hYk, enc_inl hS hT hr hQ hw (Nat.le_of_lt hlt) _ (hT.wkz w hwm) (hT.wkm w hwm)]\n exact hkk\n · rw [if_neg hkk, if_neg hkk, cycCell, enc_rd hS hT hr hQ hw (Nat.le_of_lt hlt), cv_N hmt, hk]\n · have hwr : lookup (fun w => w.1 == p.1) s.f.m.writes = none := by\n cases h : lookup (fun w => w.1 == p.1) s.f.m.writes\n · rfl\n · exact absurd ⟨_, h⟩ hwr'\n rw [cycNext_none he hwr, everyMaps_none he hwr, cycCell, enc_rd hS hT hr hQ hw (Nat.le_of_lt hlt), cv_N hmt, hk]\n\ntheorem fn_holds {g : MLaw × List Nat} (hg : g ∈ cycGiven s) (X : Nat → Nat)\n (hX : ∀ j, j ∉ cycP s ce keys → j ∉ g.1.key.toList ++ g.2 → X j = r.ρ0 j) :\n evalC X (cycMaps s ce r (T + 1)) g.1.holds = true := by\n rw [congrMC X (cycMaps s ce r (T + 1)) (r.mp 0) _ (fun m hm => cm_o (avoidsMaps_occ (hT.gm g hg) hm))]\n apply holds_any g.1 (r.mp 0) g.2 r.ρ0 (given_holds hS hT hr hg) X\n intro j hj hgs hk\n apply hX j\n · intro hP; have := hT.gz g hg j hP hk hgs; rw [this] at hj; exact absurd hj (by decide)\n · intro hm\n rcases List.mem_append.mp hm with h | h\n · exact hk (mem_toList h)\n · exact hgs h\n\ntheorem canon_holds {g : Fact} (hg : g ∈ cycCanon s ce) {Q : Nat → Nat} (hQ : EnvOk s ce keys r T Q) (vals : Nat → Nat) :\n evalC (override g.keys vals Q) (cycMaps s ce r (T + 1)) g.holds = true := by\n have hsp := special_parts hS.dsp\n have hov : ∀ ks : List Nat, (∀ x ∈ ks, x = ce.u ∨ x = ce.k) → EnvOk s ce keys r T (override ks vals Q) := by\n intro ks hks\n refine ⟨?_, ?_, ?_⟩\n · simp only [override]\n rw [if_neg (fun h => by\n rcases hks _ (List.contains_iff_mem.mp h) with e | e\n · exact hsp.1 e.symm\n · exact hsp.2.2.1 e.symm)]\n exact hQ.1\n · simp only [override]\n rw [if_neg (fun h => by\n rcases hks _ (List.contains_iff_mem.mp h) with e | e\n · exact hsp.2.1 e.symm\n · exact hsp.2.2.2.1 e.symm)]\n exact hQ.2.1\n · intro i hi\n simp only [override]\n rw [if_neg (fun h => by\n rcases hks _ (List.contains_iff_mem.mp h) with e | e\n · exact (notP hi).1 (in_special (Or.inl e))\n · exact (notP hi).1 (in_special (Or.inr (Or.inl e))))]\n exact hQ.2.2 i hi\n simp only [cycCanon, List.mem_cons, List.mem_append] at hg\n rcases hg with rfl | rfl | rfl | hg | hg\n · exact start_holds hS hT hr (hov [ce.u] (by simp))\n · exact step_holds hS hT hr true (hov [ce.u] (by simp))\n · exact step_holds hS hT hr false (hov [ce.u] (by simp))\n · simp only [cycMapFacts, List.mem_flatMap, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hg\n obtain ⟨p, hp, rfl | rfl⟩ := hg\n · exact mapStart_holds hS hT hr hp (hov [ce.u, ce.k] (by simp))\n · exact mapStep_holds hS hT hr hp (hov [ce.u, ce.k] (by simp))\n · simp only [cycFnFacts, List.mem_map] at hg\n obtain ⟨q, hq, rfl⟩ := hg\n exact fn_holds hS hT hr hq _ (fun j hjP hjk => by\n simp only [override]\n rw [if_neg (fun h => hjk (List.contains_iff_mem.mp h))]\n exact hQ.2.2 j hjP)\nend facts\n\n/-! The bridge for machines -/\n\n/-- A cyclic proof of a machine law, checked against the machine's encoding (`cycMOk`): the law holds in every state\nthe machine reaches, at every value of its keys. -/\ntheorem cyc_machine_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (law : C) (keys : List Nat) (n : CNode) (hn : n ∈ nodes) (hok : cycMOk s ce facts law keys n = true) :\n ∀ ρ M, SReach s ρ M → ∀ ρ' : Nat → Nat, (∀ i, i ∉ keys → ρ' i = ρ i) → evalC ρ' M law = true := by\n simp only [cycMOk, Bool.and_eq_true] at hok\n obtain ⟨⟨⟨⟨hsh, hte⟩, hlaw⟩, hfa⟩, hnd⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n simp only [cycNodeOk, Bool.and_eq_true] at hnd\n simp only [cycLawOk, Bool.and_eq_true] at hlaw\n intro ρ M hreach ρ' hρ'\n obtain ⟨r, T, hr, hst, hmp⟩ := reach_run hS.q hreach\n have hsp := special_parts hS.dsp\n have hnot : ∀ i, i ∉ ce.special → cycEnv ce T ρ' i = ρ' i := fun i hi => by\n simp only [cycEnv]\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]\n have hEnv : EnvOk s ce keys r T (cycEnv ce T ρ') := by\n refine ⟨by simp [cycEnv], by simp [cycEnv, Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1], ?_⟩\n intro i hi\n obtain ⟨h1, h2, h3, h4⟩ := notP hi\n rw [hnot i h1, hρ' i h2, ← hst]\n exact run_frame hr T (Nat.le_refl T) i (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact h3 (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact h3 (hS.stepv p hp)) (fun h => h3 (hS.inv i h)) h4\n have hF : FactsHold facts (cycEnv ce T ρ') (cycMaps s ce r (T + 1)) := by\n intro f hf vals\n have hfi := List.all_eq_true.mp hfa f hf\n simp only [Bool.or_eq_true] at hfi\n rcases hfi with hnc | hcan\n · exact natC_true _ hnc\n · obtain ⟨g, hg, hb⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb\n rw [hb.1, beqC_eq hb.2]\n exact canon_holds hS hT hr hg hEnv vals\n have htT : cycEnv ce T ρ' ce.t = T := by simp [cycEnv]\n have hH : ∀ c ∈ n.hyps, evalC (cycEnv ce T ρ') (cycMaps s ce r (T + 1)) c = true := by\n intro c hc\n have h := List.all_eq_true.mp hnd.2 c hc\n simp only [Bool.or_eq_true] at h\n rcases h with (h | h) | h\n · rw [beqC_eq h, congr2C (ρ' := r.ρ0) (M' := r.mp 0) _ (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj))\n (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, htT, hEnv.1] <;> omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hEnv.1, hEnv.2.1] <;> omega\n have hc0 := cyclic_sound facts nodes hc (cycMaps s ce r (T + 1)) (cycEnv ce T ρ') hF n hn hH\n have htT' : evalE (cycEnv ce T ρ') (cycMaps s ce r (T + 1)) (.var ce.t) = T := htT\n rw [beqC_eq hnd.1, enc_trC hS hT hr hEnv htT' (Nat.le_refl T)] at hc0\n rw [congr2C (ρ' := applyS (cycσ ce (.var ce.t)) (cycEnv ce T ρ') (cycMaps s ce r (T + 1))) (M' := cycView s ce r (T + 1) T) law\n (fun j hj => ?_) (fun m hm => ?_)]\n · exact hc0\n · have hj' := avoids_occ hlaw.1 hj\n have hjs : j ∉ ce.special := fun h => hj' (List.mem_append_left _ h)\n have hji : j ∉ s.f.m.inputs := fun h => hj' (List.mem_append_right _ h)\n by_cases hv : j ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr hEnv htT' (Nat.le_refl T) hv, CRun.X, withInputs_other hji, hst]\n exact hρ' j (fun hk => hS.zv j (List.mem_append_right _ hk) hv)\n · rw [enc_σ_out hS hT hr hEnv htT' (Nat.le_refl T) hv, hnot j hjs]\n · have hm' := avoidsMaps_occ hlaw.2 hm\n rw [cv_N (fun h => hm' (List.mem_append_left _ h)), N_not_carried (fun h => hm' (List.mem_append_right _ h)), hmp]\n\n-- The same as a law of the machine about every value of its keys (Sets.lean's `holdsOver`).\ntheorem cyc_machine_holds {M : Nat → Nat → Nat} {ρ : Nat → Nat} {law : C} {key : Option Nat} {gs : List Nat}\n (h : ∀ ρ' : Nat → Nat, (∀ i, i ∉ key.toList ++ gs → ρ' i = ρ i) → evalC ρ' M law = true) : holdsOver ⟨law, key⟩ M gs ρ := by\n induction gs generalizing ρ with\n | nil =>\n cases key with\n | none => simp only [holdsOver, MLaw.Holds]; exact h ρ (fun _ _ => rfl)\n | some kv =>\n simp only [holdsOver, MLaw.Holds]\n intro v\n exact h _ (fun i hi => by\n simp only [upd]\n rw [if_neg (fun e => hi (by simp [Option.toList, e]))])\n | cons g gs ih =>\n simp only [holdsOver]\n intro v\n apply ih\n intro ρ' hρ'\n apply h\n intro i hi\n rw [hρ' i (fun hm => hi (by\n rcases List.mem_append.mp hm with h1 | h1\n · exact List.mem_append_left _ h1\n · exact List.mem_append_right _ (List.mem_cons_of_mem _ h1)))]\n simp only [upd]\n rw [if_neg (fun e => hi (List.mem_append_right _ (by rw [e]; exact List.mem_cons_self)))]\n\n/-! Recurrences (`recurNodes`) -/\n\n-- A recurrence's values: f(0) is `zero`, f(n + 1) is `next` with x read as n and prev as f(n), the rest of the\n-- environment (the further arguments) as given.\ndef recF (zero next : E) (x prev : Nat) (env : Nat → Nat) (maps : Nat → Nat → Nat) : Nat → Nat\n | 0 => evalE env maps zero\n | n + 1 => evalE (upd (upd env x n) prev (recF zero next x prev env maps n)) maps next\n\n-- The maps with one of them replaced.\ndef withMap (maps : Nat → Nat → Nat) (f : Nat) (g : Nat → Nat) : Nat → Nat → Nat := fun m => if m = f then g else maps m\n\n-- The function's map, the recurrence's x and prev, the step fact's key.\nstructure RecEnc where\n f : Nat\n x : Nat\n prev : Nat\n k : Nat\n\n-- The recurrence's two equations as recurNodes states them: f(0) = zero, and at every k, f(k + 1) = next(k, f(k)).\ndef recFacts (re : RecEnc) (zero next : E) : List Fact :=\n [⟨[], .eq (.get re.f (.const 0)) zero⟩,\n ⟨[re.k], .eq (.get re.f (.add (.var re.k) (.const 1))) (substE [(re.x, .var re.k), (re.prev, .get re.f (.var re.k))] next)⟩]\n\n-- The exported facts are the equations, facts given about other functions (`given`), or true of every natural; the\n-- recurrence reads its own values only through prev, and nothing given reads them.\ndef recOk (re : RecEnc) (zero next : E) (given facts : List Fact) : Bool :=\n !occME re.f zero && !occME re.f next && !occE re.k next && re.x != re.prev && re.k != re.x && re.k != re.prev\n && given.all (fun g => !occMC re.f g.holds)\n && facts.all (fun g => natC g.holds || (recFacts re zero next ++ given).any (factBeq g))\n\ntheorem override_nil (vals env : Nat → Nat) : override [] vals env = env := by\n funext i; simp [override]\n\ntheorem rec_facts {re : RecEnc} {zero next : E} {given facts : List Fact} (hok : recOk re zero next given facts = true)\n (maps : Nat → Nat → Nat) (env : Nat → Nat) (hg : FactsHold given env maps) :\n FactsHold facts env (withMap maps re.f (recF zero next re.x re.prev env maps)) := by\n simp only [recOk, Bool.and_eq_true, Bool.not_eq_true', bne_iff_ne, ne_eq] at hok\n obtain ⟨⟨⟨⟨⟨⟨⟨hz, hn⟩, hk⟩, hxp⟩, hkx⟩, hkp⟩, hgv⟩, hfa⟩ := hok\n have hM : ∀ e : E, occME re.f e = false → ∀ ρ, evalE ρ (withMap maps re.f (recF zero next re.x re.prev env maps)) e = evalE ρ maps e :=\n fun e he ρ => congrME ρ _ maps e (fun m hm => by\n simp only [withMap]; rw [if_neg (fun h => by rw [h, he] at hm; exact absurd hm (by decide))])\n intro f hf vals\n have hfi := List.all_eq_true.mp hfa f hf\n simp only [Bool.or_eq_true] at hfi\n rcases hfi with hnc | hcan\n · exact natC_true _ hnc\n obtain ⟨g, hg', hb⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb\n rw [hb.1, beqC_eq hb.2]\n simp only [recFacts, List.cons_append, List.nil_append, List.mem_cons] at hg'\n rcases hg' with rfl | rfl | hg'\n · show evalC (override [] vals env) _ (.eq (.get re.f (.const 0)) zero) = true\n rw [override_nil, evalC_iff]\n show withMap maps re.f (recF zero next re.x re.prev env maps) re.f 0 = evalE env _ zero\n rw [hM zero hz env]\n simp only [withMap, if_pos (rfl : re.f = re.f), ite_true, recF]\n · show evalC (override [re.k] vals env) _ (.eq (.get re.f (.add (.var re.k) (.const 1)))\n (substE [(re.x, .var re.k), (re.prev, .get re.f (.var re.k))] next)) = true\n rw [evalC_iff]\n show withMap maps re.f (recF zero next re.x re.prev env maps) re.f (override [re.k] vals env re.k + 1) = _\n rw [subst_evalE, hM next hn]\n simp only [withMap, if_pos (rfl : re.f = re.f), ite_true, recF]\n refine congrE _ _ maps next (fun j hj => ?_)\n have hjk : j ≠ re.k := fun h => by rw [h, hk] at hj; exact absurd hj (by decide)\n simp only [upd, applyS, assoc, lookup]\n by_cases hjp : j = re.prev\n · subst hjp\n simp [hxp, Ne.symm hxp, evalE, withMap]\n · by_cases hjx : j = re.x\n · subst hjx\n simp [evalE, hxp]\n · have h1 : (re.x == j) = false := by simpa using Ne.symm hjx\n have h2 : (re.prev == j) = false := by simpa using Ne.symm hjp\n simp only [hjp, hjx, if_false, h1, h2, Bool.false_eq_true, Option.map_none, override]\n rw [if_neg (fun h => hjk (by simpa using h))]\n · rw [congrMC _ _ maps g.holds (fun m hm => by\n simp only [withMap]\n rw [if_neg (fun h => by\n have := List.all_eq_true.mp hgv g hg'; rw [h] at hm; rw [hm] at this; exact absurd this (by decide))])]\n exact hg g hg' vals\n\n/-- A cyclic proof of a recurrence's lemmas and laws between two points (`recurNodes`), checked against its equations\n(`recOk`): every node holds of the recurrence's values (`recF`), at every environment where the facts given about the\nfunctions it reads hold. -/\ntheorem cyc_recur_sound (re : RecEnc) (zero next : E) (given facts : List Fact) (nodes : List CNode)\n (hc : checkCyclic facts nodes = true) (hok : recOk re zero next given facts = true) (maps : Nat → Nat → Nat) (env : Nat → Nat)\n (hg : FactsHold given env maps) : ∀ n ∈ nodes, (∀ c ∈ n.hyps, evalC env (withMap maps re.f (recF zero next re.x re.prev env maps)) c = true) →\n evalC env (withMap maps re.f (recF zero next re.x re.prev env maps)) n.concl = true :=\n cyclic_sound facts nodes hc _ env (rec_facts hok maps env hg)\n\ntheorem assoc_map_kv {α : Type} (l : List α) (k : α → Nat) (g : α → E) (y : Nat) :\n assoc (l.map fun a => (k a, g a)) y = (lookup (fun a => k a == y) l).map g := by\n simp only [assoc, lookup_map_pred, Option.map_map]; rfl\n\ntheorem holds_of_eq {ρ : Nat → Nat} {M : Nat → Nat → Nat} {a b : E} (h : evalE ρ M a = evalE ρ M b) : evalC ρ M (.eq a b) = true :=\n (evalC_iff ρ M (.eq a b)).mpr h\ntheorem holds_of_lt {ρ : Nat → Nat} {M : Nat → Nat → Nat} {a b : E} (h : evalE ρ M a < evalE ρ M b) : evalC ρ M (.lt a b) = true :=\n (evalC_iff ρ M (.lt a b)).mpr h\n\ntheorem holds_of_le {ρ : Nat → Nat} {M : Nat → Nat → Nat} {a b : E} (h : evalE ρ M a ≤ evalE ρ M b) : evalC ρ M (.le a b) = true :=\n (evalC_iff ρ M (.le a b)).mpr h\ntheorem and_true2 {ρ : Nat → Nat} {M : Nat → Nat → Nat} {a b : C} (ha : evalC ρ M a = true) (hb : evalC ρ M b = true) :\n evalC ρ M (.and a b) = true := by simp [evalC, ha, hb]\n\n/-! Folds over trees (`foldNodes`) -/\n\ndef BTree.size : BTree → Nat\n | .leaf _ => 1\n | .node _ l r => l.size + r.size + 1\n-- The inputs a leaf or node carries.\ndef BTree.req : BTree → Nat → Nat\n | .leaf ι => ι\n | .node ι _ _ => ι\n-- The subtrees in post-order: each node after its children, so its right child just before it and its left child just\n-- before its right subtree (foldNodes' table, children numbered below their parent).\ndef BTree.flat : BTree → List BTree\n | .leaf ι => [.leaf ι]\n | .node ι l r => l.flat ++ r.flat ++ [.node ι l r]\n\ntheorem flat_length : ∀ t : BTree, t.flat.length = t.size\n | .leaf _ => rfl\n | .node _ l r => by simp [BTree.flat, BTree.size, flat_length l, flat_length r]; omega\n\ntheorem size_pos : ∀ t : BTree, 1 ≤ t.size\n | .leaf _ => by simp [BTree.size]\n | .node _ l r => by simp [BTree.size]\n\ntheorem flat_last : ∀ t : BTree, t.flat[t.size - 1]? = some t\n | .leaf _ => rfl\n | .node ι l r => by\n simp only [BTree.flat, BTree.size]\n rw [List.getElem?_append_right (by simp [flat_length])]\n simp [flat_length]\n\ntheorem flat_node : ∀ (t : BTree) (i : Nat) (ι : Nat → Nat) (l r : BTree), t.flat[i]? = some (.node ι l r) →\n l.size + r.size ≤ i ∧ t.flat[i - 1]? = some r ∧ t.flat[i - 1 - r.size]? = some l\n | .leaf _, i, ι, l, r, h => by\n cases i with\n | zero => simp [BTree.flat] at h\n | succ i => simp [BTree.flat] at h\n | .node ι' l' r', i, ι, l, r, h => by\n have hl := flat_length l'\n have hr := flat_length r'\n have pl := size_pos l'\n have pr := size_pos r'\n have pl2 := size_pos l\n simp only [BTree.flat] at h ⊢\n rcases Nat.lt_or_ge i l'.size with h1 | h1\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_left (by omega)] at h\n obtain ⟨b, e1, e2⟩ := flat_node l' i ι l r h\n refine ⟨b, ?_, ?_⟩\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_left (by omega), e1]\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_left (by omega), e2]\n · rcases Nat.lt_or_ge i (l'.size + r'.size) with h2 | h2\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_right (by omega), hl] at h\n obtain ⟨b, e1, e2⟩ := flat_node r' (i - l'.size) ι l r h\n refine ⟨by omega, ?_, ?_⟩\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_right (by omega), hl,\n show i - 1 - l'.size = i - l'.size - 1 by omega]\n exact e1\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_right (by omega), hl,\n show i - 1 - r.size - l'.size = i - l'.size - 1 - r.size by omega]\n exact e2\n · rw [List.getElem?_append_right (by simp; omega)] at h\n simp only [List.length_append, hl, hr] at h\n rcases Nat.lt_or_ge i (l'.size + r'.size + 1) with h3 | h3\n · have hi : i - (l'.size + r'.size) = 0 := by omega\n rw [hi] at h\n simp only [List.getElem?_cons_zero, Option.some.injEq, BTree.node.injEq] at h\n obtain ⟨_, hll, hrr⟩ := h\n subst hll\n subst hrr\n refine ⟨by omega, ?_, ?_⟩\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_right (by omega), hl,\n show i - 1 - l'.size = r'.size - 1 by omega, ← hr]\n rw [hr]; exact flat_last r'\n · rw [List.getElem?_append_left (by simp; omega), List.getElem?_append_left (by omega),\n show i - 1 - r'.size = l'.size - 1 by omega]\n exact flat_last l'\n · rw [List.getElem?_eq_none (by simp; omega)] at h\n exact absurd h (by simp)\n\ntheorem flat_allowed {f : Fold} {ρ : Nat → Nat} {maps : Nat → Nat → Nat} : ∀ t : BTree, Allowed f ρ maps t →\n ∀ s ∈ t.flat, Allowed f ρ maps s\n | .leaf ι, h, s, hs => by simp [BTree.flat] at hs; rw [hs]; exact h\n | .node ι l r, h, s, hs => by\n simp only [BTree.flat, List.mem_append, List.mem_singleton] at hs\n rcases hs with (hs | hs) | hs\n · exact flat_allowed l h.2.1 s hs\n · exact flat_allowed r h.2.2 s hs\n · rw [hs]; exact h\n\n-- The table's variables (a node's number n, the tree's size N) and maps (each node's kind and children, and each\n-- state variable and input's value at each node).\nstructure FoldEnc where\n n : Nat\n N : Nat\n kind : Nat\n left : Nat\n right : Nat\n vtr : List (Nat × Nat)\n\ndef foldσ (fe : FoldEnc) (w : E) : List (Nat × E) := fe.vtr.map fun p => (p.1, .get p.2 w)\n-- A node's frame: its inputs, and each copy read at the child's number.\ndef foldNodeσ (f : Fold) (fe : FoldEnc) : List (Nat × E) :=\n f.inputs.map (fun i => (i, substE (foldσ fe (.var fe.n)) (.var i)))\n ++ f.state.map (fun e => (e.2.1, substE (foldσ fe (.get fe.left (.var fe.n))) (.var e.1)))\n ++ f.state.map (fun e => (e.2.2, substE (foldσ fe (.get fe.right (.var fe.n))) (.var e.1)))\n\n-- foldNodes' table fact: below the size, a leaf's values from its inputs, a node's from its children's, numbered below\n-- it (with the assumption at each, or without when the fold has none).\ndef foldTable (f : Fold) (fe : FoldEnc) (withAssume : Bool) : Fact :=\n ⟨[fe.n], .or (.not (.lt (.var fe.n) (.var fe.N))) (.and (.le (.get fe.kind (.var fe.n)) (.const 1)) (.and\n (.or (.not (.eq (.get fe.kind (.var fe.n)) (.const 0))) (andL (trueC :: ((if withAssume then [substC (foldσ fe (.var fe.n)) f.assume] else [])\n ++ f.leaf.map fun p => .eq (substE (foldσ fe (.var fe.n)) (.var p.1)) (substE (foldσ fe (.var fe.n)) p.2)))))\n (.or (.not (.eq (.get fe.kind (.var fe.n)) (.const 1))) (andL (.lt (.get fe.left (.var fe.n)) (.var fe.n) :: .lt (.get fe.right (.var fe.n)) (.var fe.n)\n :: ((if withAssume then [substC (foldσ fe (.var fe.n)) f.assume] else [])\n ++ f.leaf.map fun p => .eq (substE (foldσ fe (.var fe.n)) (.var p.1)) (substE (foldNodeσ f fe) ((assoc f.node p.1).getD (.var p.1)))))))))⟩\n\ndef foldCopies (f : Fold) : List Nat := f.state.map (·.2.1) ++ f.state.map (·.2.2)\ndef foldTM (fe : FoldEnc) : List Nat := [fe.kind, fe.left, fe.right] ++ fe.vtr.map (·.2)\ndef foldNot (f : Fold) (fe : FoldEnc) : List Nat := f.state.map (·.1) ++ foldCopies f ++ [fe.n, fe.N]\n\n-- The table fits the fold: names apart, every input and state variable with its map, leaf and node set the state\n-- variables, leaves and the assumption read no state variable or copy, nodes no state variable, the precondition\n-- params alone, the law no input or copy; none reads the added variables or maps.\ndef foldShapeOk (f : Fold) (fe : FoldEnc) : Bool :=\n copiesOk f && distinct (f.inputs ++ f.state.map (·.1) ++ foldCopies f ++ [fe.n, fe.N]) && distinct (f.leaf.map (·.1))\n && f.leaf.all (fun p => (f.state.map (·.1)).contains p.1 && (f.node.map (·.1)).contains p.1)\n && f.node.all (fun p => (f.state.map (·.1)).contains p.1)\n && distinct (foldTM fe) && (f.inputs ++ f.state.map (·.1)).all (fun x => (fe.vtr.map (·.1)).contains x)\n && !(fe.vtr.map (·.1)).contains fe.n && !(fe.vtr.map (·.1)).contains fe.N\ndef foldTermsOk (f : Fold) (fe : FoldEnc) (law : C) : Bool :=\n f.leaf.all (fun p => avoidsE (foldNot f fe) p.2 && avoidsMapsE (foldTM fe) p.2)\n && f.node.all (fun p => avoidsE (f.state.map (·.1) ++ [fe.n, fe.N]) p.2 && avoidsMapsE (foldTM fe) p.2)\n && avoids (foldNot f fe) f.assume && avoidsMaps (foldTM fe) f.assume\n && avoids (f.inputs ++ foldNot f fe) f.pre && avoidsMaps (foldTM fe) f.pre\n && avoids (f.inputs ++ foldCopies f ++ [fe.n, fe.N]) law && avoidsMaps (foldTM fe) law\ndef foldOk (f : Fold) (fe : FoldEnc) (facts : List Fact) (law : C) (nd : CNode) : Bool :=\n foldShapeOk f fe && foldTermsOk f fe law\n && facts.all (fun g => natC g.holds || [foldTable f fe true, foldTable f fe false].any (factBeq g))\n && beqC nd.concl (substC (foldσ fe (.var fe.n)) law)\n && nd.hyps.all (fun h => beqC h f.pre || beqC h (.lt (.var fe.n) (.var fe.N)))\n\n-- The tree as foldNodes' table.\ndef foldMaps (f : Fold) (fe : FoldEnc) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) (t : BTree) : Nat → Nat → Nat := fun i =>\n if i = fe.kind then fun j => match t.flat[j]? with\n | some (.node _ _ _) => 1\n | _ => 0\n else if i = fe.left then fun j => match t.flat[j]? with\n | some (.node _ _ r) => j - 1 - r.size\n | _ => 0\n else if i = fe.right then fun j => j - 1\n else match lookup (fun p => p.2 == i) fe.vtr with\n | some p => fun j => match t.flat[j]? with\n | some s => foldT f ρ maps s p.1\n | none => 0\n | none => maps i\n\nstructure FoldShape (f : Fold) (fe : FoldEnc) : Prop where\n copies : copiesOk f = true\n dvars : distinct (f.inputs ++ f.state.map (·.1) ++ foldCopies f ++ [fe.n, fe.N]) = true\n dleaf : distinct (f.leaf.map (·.1)) = true\n leafst : ∀ p ∈ f.leaf, p.1 ∈ f.state.map (·.1)\n leafnode : ∀ p ∈ f.leaf, p.1 ∈ f.node.map (·.1)\n nodest : ∀ p ∈ f.node, p.1 ∈ f.state.map (·.1)\n dtm : distinct (foldTM fe) = true\n vin : ∀ x ∈ f.inputs, x ∈ fe.vtr.map (·.1)\n vst : ∀ x ∈ f.state.map (·.1), x ∈ fe.vtr.map (·.1)\n vn : fe.n ∉ fe.vtr.map (·.1)\n vN : fe.N ∉ fe.vtr.map (·.1)\n\ntheorem foldShape_of {f : Fold} {fe : FoldEnc} (h : foldShapeOk f fe = true) : FoldShape f fe := by\n simp only [foldShapeOk, Bool.and_eq_true] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨hc, hd⟩, hdl⟩, hl⟩, hn⟩, hdt⟩, hv⟩, hvn⟩, hvN⟩ := h\n refine ⟨hc, hd, hdl, fun p hp => List.contains_iff_mem.mp (and_parts (List.all_eq_true.mp hl p hp)).1,\n fun p hp => List.contains_iff_mem.mp (and_parts (List.all_eq_true.mp hl p hp)).2,\n fun p hp => List.contains_iff_mem.mp (List.all_eq_true.mp hn p hp), hdt,\n fun x hx => List.contains_iff_mem.mp (List.all_eq_true.mp hv x (List.mem_append_left _ hx)),\n fun x hx => List.contains_iff_mem.mp (List.all_eq_true.mp hv x (List.mem_append_right _ hx)),\n not_mem_of_not_contains hvn, not_mem_of_not_contains hvN⟩\n\nsection fold\nvariable {f : Fold} {fe : FoldEnc} {ρ : Nat → Nat} {maps : Nat → Nat → Nat} {t : BTree} (hS : FoldShape f fe)\ninclude hS\n\ntheorem fs_parts : fe.kind ≠ fe.left ∧ fe.kind ≠ fe.right ∧ fe.left ≠ fe.right ∧ (∀ a ∈ fe.vtr.map (·.2), a ≠ fe.kind ∧ a ≠ fe.left ∧ a ≠ fe.right)\n ∧ distinct (fe.vtr.map (·.2)) = true := by\n have h := hS.dtm\n simp only [foldTM, List.cons_append, List.nil_append] at h\n obtain ⟨n1, d1⟩ := distinct_cons h\n obtain ⟨n2, d2⟩ := distinct_cons d1\n obtain ⟨n3, d3⟩ := distinct_cons d2\n refine ⟨fun e => n1 (by rw [e]; simp), fun e => n1 (by rw [e]; simp), fun e => n2 (by rw [e]; simp),\n fun a ha => ⟨fun e => n1 (by rw [← e]; simp [ha]), fun e => n2 (by rw [← e]; simp [ha]), fun e => n3 (by rw [← e]; exact ha)⟩, d3⟩\n\ntheorem fm_kind (j : Nat) : foldMaps f fe ρ maps t fe.kind j = match t.flat[j]? with\n | some (.node _ _ _) => 1\n | _ => 0 := by\n simp only [foldMaps, if_pos rfl, ite_true]\ntheorem fm_left (j : Nat) : foldMaps f fe ρ maps t fe.left j = match t.flat[j]? with\n | some (.node _ _ r) => j - 1 - r.size\n | _ => 0 := by\n have := fs_parts hS\n simp only [foldMaps, if_neg (Ne.symm this.1), if_pos rfl, ite_true]\ntheorem fm_right (j : Nat) : foldMaps f fe ρ maps t fe.right j = j - 1 := by\n have := fs_parts hS\n simp only [foldMaps, if_neg (Ne.symm this.2.1), if_neg (Ne.symm this.2.2.1), if_pos rfl, ite_true]\ntheorem fm_v {p : Nat × Nat} (hp : p ∈ fe.vtr) (j : Nat) : foldMaps f fe ρ maps t p.2 j = match t.flat[j]? with\n | some s => foldT f ρ maps s p.1\n | none => 0 := by\n obtain ⟨_, _, _, ha, hd⟩ := fs_parts hS\n have h3 := ha p.2 (List.mem_map.mpr ⟨p, hp, rfl⟩)\n simp only [foldMaps, if_neg h3.1, if_neg h3.2.1, if_neg h3.2.2, lookup_ren hd p hp]\ntheorem fm_o {i : Nat} (hi : i ∉ foldTM fe) : foldMaps f fe ρ maps t i = maps i := by\n have h1 : i ≠ fe.kind := fun e => hi (by simp [foldTM, e])\n have h2 : i ≠ fe.left := fun e => hi (by simp [foldTM, e])\n have h3 : i ≠ fe.right := fun e => hi (by simp [foldTM, e])\n have hn : lookup (fun p => p.2 == i) fe.vtr = none := lookup_none (fun q hq => by\n simp only [beq_eq_false_iff_ne]; intro he\n exact hi (List.mem_append_right _ (List.mem_map.mpr ⟨q, hq, he⟩)))\n simp only [foldMaps, if_neg h1, if_neg h2, if_neg h3, hn]\n\n-- What no leaf or node sets, and no copy: the inputs a node carries, else as before the tree.\ntheorem foldT_frame {y : Nat} (hy : y ∉ f.state.map (·.1)) (hc : y ∉ foldCopies f) (s : BTree) :\n foldT f ρ maps s y = withInputs f.inputs ρ s.req y := by\n cases s with\n | leaf ι =>\n simp only [foldT, BTree.req]\n exact applyS_other (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hy (hS.leafst p hp)) _ _\n | node ι l r =>\n simp only [foldT, BTree.req]\n rw [applyS_other (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hy (hS.nodest p hp))]\n exact withChildren_plain f _ _ _ (fun h => hc (List.mem_append_left _ h)) (fun h => hc (List.mem_append_right _ h))\n\ntheorem fold_σ_in {Q : Nat → Nat} {w : E} {j : Nat} {s : BTree} (hw : evalE Q (foldMaps f fe ρ maps t) w = j)\n (hs : t.flat[j]? = some s) {x : Nat} (hx : x ∈ fe.vtr.map (·.1)) : applyS (foldσ fe w) Q (foldMaps f fe ρ maps t) x = foldT f ρ maps s x := by\n cases h : lookup (fun p => p.1 == x) fe.vtr with\n | none =>\n exfalso; obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hx\n have := lookup_none_all h p hp; simp at this\n | some q =>\n obtain ⟨hq, hqx⟩ := lookup_some h\n have hqx' : q.1 = x := by simpa using hqx\n have ha : assoc (foldσ fe w) x = some (.get q.2 w) := by\n simp only [assoc, foldσ, lookup_map_pred, h, Option.map_some]\n simp only [applyS, ha, evalE, fm_v hS hq, hw, hs, hqx']\n\ntheorem fold_σ_out {Q : Nat → Nat} {w : E} {x : Nat} (hx : x ∉ fe.vtr.map (·.1)) :\n applyS (foldσ fe w) Q (foldMaps f fe ρ maps t) x = Q x := by\n apply applyS_other\n intro h; apply hx\n obtain ⟨a, ha, rfl⟩ := List.mem_map.mp h\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp ha\n exact List.mem_map.mpr ⟨p, hp, rfl⟩\n\n-- A term that reads no state variable, copy or added variable, at a node's frame: at its inputs.\ntheorem fold_at {Q : Nat → Nat} (hQ : ∀ i, i ≠ fe.n → i ≠ fe.N → Q i = ρ i) {w : E} {j : Nat} {s : BTree}\n (hw : evalE Q (foldMaps f fe ρ maps t) w = j) (hs : t.flat[j]? = some s) {y : Nat} (hy : y ∉ foldNot f fe) :\n applyS (foldσ fe w) Q (foldMaps f fe ρ maps t) y = withInputs f.inputs ρ s.req y := by\n have hy1 : y ∉ f.state.map (·.1) := fun h => hy (by simp only [foldNot, List.mem_append]; exact Or.inl (Or.inl h))\n have hy2 : y ∉ foldCopies f := fun h => hy (by simp only [foldNot, List.mem_append]; exact Or.inl (Or.inr h))\n have hy3 : y ≠ fe.n := fun e => hy (by simp [foldNot, e])\n have hy4 : y ≠ fe.N := fun e => hy (by simp [foldNot, e])\n by_cases hv : y ∈ fe.vtr.map (·.1)\n · rw [fold_σ_in hS hw hs hv, foldT_frame hS hy1 hy2]\n · rw [fold_σ_out hS hv, hQ y hy3 hy4, withInputs_other (fun h => hv (hS.vin y h))]\n\ntheorem dvars_parts : (∀ x ∈ f.inputs, x ∉ foldCopies f) ∧ fe.n ≠ fe.N ∧ (∀ x ∈ f.inputs ++ f.state.map (·.1) ++ foldCopies f, x ≠ fe.n ∧ x ≠ fe.N) := by\n obtain ⟨d1, d2, d3⟩ := distinct_append hS.dvars\n obtain ⟨d4, _, d6⟩ := distinct_append d1\n obtain ⟨_, _, d9⟩ := distinct_append d4\n obtain ⟨n1, _⟩ := distinct_cons d2\n refine ⟨fun x hx hc => d6 x (List.mem_append_left _ hx) hc, fun e => n1 (by rw [e]; simp), fun x hx => ⟨fun e => d3 x hx (by rw [e]; simp), fun e => d3 x hx (by rw [e]; simp)⟩⟩\n\n-- A node's frame, as foldNodeσ reads it from the table: as withChildren reads it from the children.\ntheorem fold_node {Q : Nat → Nat} (hQ : ∀ i, i ≠ fe.n → i ≠ fe.N → Q i = ρ i) {j : Nat} {ι : Nat → Nat} {l r : BTree}\n (hs : t.flat[j]? = some (.node ι l r)) (hj : Q fe.n = j) {y : Nat} (hy1 : y ∉ f.state.map (·.1)) (hy2 : y ≠ fe.n) (hy3 : y ≠ fe.N) :\n applyS (foldNodeσ f fe) Q (foldMaps f fe ρ maps t) y = withChildren f (withInputs f.inputs ρ ι) (foldT f ρ maps l) (foldT f ρ maps r) y := by\n obtain ⟨_, hr1, hl1⟩ := flat_node t j ι l r hs\n have hwn : evalE Q (foldMaps f fe ρ maps t) (.var fe.n) = j := hj\n have hwl : evalE Q (foldMaps f fe ρ maps t) (.get fe.left (.var fe.n)) = j - 1 - r.size := by\n simp only [evalE, hj, fm_left hS, hs]\n have hwr : evalE Q (foldMaps f fe ρ maps t) (.get fe.right (.var fe.n)) = j - 1 := by\n simp only [evalE, hj, fm_right hS]\n have hcp := (dvars_parts hS).1\n simp only [applyS, foldNodeσ, assoc_append_or, assoc_map_kv, withChildren]\n by_cases hin : y ∈ f.inputs\n · have hl0 : lookup (fun e => e.2.1 == y) f.state = none := lookup_none (fun e he => by\n simp only [beq_eq_false_iff_ne]; intro h; exact hcp y hin (List.mem_append_left _ (List.mem_map.mpr ⟨e, he, h⟩)))\n have hr0 : lookup (fun e => e.2.2 == y) f.state = none := lookup_none (fun e he => by\n simp only [beq_eq_false_iff_ne]; intro h; exact hcp y hin (List.mem_append_right _ (List.mem_map.mpr ⟨e, he, h⟩)))\n have hi0 : (lookup (fun i => i == y) f.inputs).map (fun i => substE (foldσ fe (.var fe.n)) (.var i)) = some (substE (foldσ fe (.var fe.n)) (.var y)) := by\n cases h : lookup (fun i => i == y) f.inputs with\n | none => have := lookup_none_all h y hin; simp at this\n | some i => obtain ⟨_, hi⟩ := lookup_some h; simp only [beq_iff_eq] at hi; simp [hi]\n simp only [hl0, hr0, hi0, Option.some_or]\n rw [subst_evalE]\n show applyS (foldσ fe (.var fe.n)) Q (foldMaps f fe ρ maps t) y = _\n rw [fold_σ_in hS hwn hs (hS.vin y hin), foldT_frame hS hy1 (hcp y hin)]\n rfl\n · have hi0 : lookup (fun i => i == y) f.inputs = none := lookup_none (fun i hi => by\n simp only [beq_eq_false_iff_ne]; intro h; exact hin (h ▸ hi))\n simp only [hi0, Option.map_none, Option.none_or]\n cases hl : lookup (fun e => e.2.1 == y) f.state with\n | some e =>\n obtain ⟨he, _⟩ := lookup_some hl\n simp only [Option.map_some, Option.some_or]\n rw [subst_evalE]\n show applyS (foldσ fe (.get fe.left (.var fe.n))) Q (foldMaps f fe ρ maps t) e.1 = _\n exact fold_σ_in hS hwl hl1 (hS.vst e.1 (List.mem_map.mpr ⟨e, he, rfl⟩))\n | none =>\n simp only [Option.map_none, Option.none_or]\n cases hr : lookup (fun e => e.2.2 == y) f.state with\n | some e =>\n obtain ⟨he, _⟩ := lookup_some hr\n simp only [Option.map_some]\n rw [subst_evalE]\n show applyS (foldσ fe (.get fe.right (.var fe.n))) Q (foldMaps f fe ρ maps t) e.1 = _\n exact fold_σ_in hS hwr hr1 (hS.vst e.1 (List.mem_map.mpr ⟨e, he, rfl⟩))\n | none =>\n simp only [Option.map_none]\n rw [hQ y hy2 hy3, withInputs_other hin]\nend fold\n\nstructure FoldTerms (f : Fold) (fe : FoldEnc) (law : C) : Prop where\n leafz : ∀ p ∈ f.leaf, avoidsE (foldNot f fe) p.2 = true\n leafm : ∀ p ∈ f.leaf, avoidsMapsE (foldTM fe) p.2 = true\n nodez : ∀ p ∈ f.node, avoidsE (f.state.map (·.1) ++ [fe.n, fe.N]) p.2 = true\n nodem : ∀ p ∈ f.node, avoidsMapsE (foldTM fe) p.2 = true\n asz : avoids (foldNot f fe) f.assume = true\n asm : avoidsMaps (foldTM fe) f.assume = true\n prz : avoids (f.inputs ++ foldNot f fe) f.pre = true\n prm : avoidsMaps (foldTM fe) f.pre = true\n lawz : avoids (f.inputs ++ foldCopies f ++ [fe.n, fe.N]) law = true\n lawm : avoidsMaps (foldTM fe) law = true\n\ntheorem foldTerms_of {f : Fold} {fe : FoldEnc} {law : C} (h : foldTermsOk f fe law = true) : FoldTerms f fe law := by\n simp only [foldTermsOk, Bool.and_eq_true] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨hl, hn⟩, ha⟩, ham⟩, hp⟩, hpm⟩, hz⟩, hzm⟩ := h\n exact ⟨fun p hp => (and_parts (List.all_eq_true.mp hl p hp)).1, fun p hp => (and_parts (List.all_eq_true.mp hl p hp)).2,\n fun p hp => (and_parts (List.all_eq_true.mp hn p hp)).1, fun p hp => (and_parts (List.all_eq_true.mp hn p hp)).2, ha, ham, hp, hpm, hz, hzm⟩\n\n-- The table's fact holds of the tree's table, wherever T and Ω... the size N is the tree's.\ntheorem table_holds {f : Fold} {fe : FoldEnc} {law : C} {ρ : Nat → Nat} {maps : Nat → Nat → Nat} {t : BTree} (hS : FoldShape f fe)\n (hT : FoldTerms f fe law) (hA : Allowed f ρ maps t) (b : Bool) {Q : Nat → Nat} (hQ : ∀ i, i ≠ fe.n → i ≠ fe.N → Q i = ρ i)\n (hN : Q fe.N = t.size) : evalC Q (foldMaps f fe ρ maps t) (foldTable f fe b).holds = true := by\n apply impl_true\n intro h0\n rw [evalC_iff] at h0; simp only [holdsC, evalE] at h0\n rw [hN, ← flat_length] at h0\n have hs : t.flat[Q fe.n]? = some t.flat[Q fe.n] := List.getElem?_eq_getElem h0\n have hal := flat_allowed t hA _ (List.getElem_mem h0)\n have hw : evalE Q (foldMaps f fe ρ maps t) (.var fe.n) = Q fe.n := rfl\n have hkind : evalE Q (foldMaps f fe ρ maps t) (.get fe.kind (.var fe.n)) = match t.flat[Q fe.n]? with\n | some (.node _ _ _) => 1\n | _ => 0 := by simp only [evalE, fm_kind hS]\n -- the assumption, at the node's own inputs\n have hasm : ∀ (s : BTree), t.flat[Q fe.n]? = some s → evalC (withInputs f.inputs ρ s.req) maps f.assume = true →\n evalC Q (foldMaps f fe ρ maps t) (substC (foldσ fe (.var fe.n)) f.assume) = true := by\n intro s hs' hx\n rw [subst_evalC, congr2C (ρ' := withInputs f.inputs ρ s.req) (M' := maps) f.assume\n (fun y hy => fold_at hS hQ hw hs' (avoids_occ hT.asz hy)) (fun m hm => fm_o hS (avoidsMaps_occ hT.asm hm))]\n exact hx\n generalize t.flat[Q fe.n] = s at hs hal\n cases s with\n | leaf ι =>\n have hk : evalE Q (foldMaps f fe ρ maps t) (.get fe.kind (.var fe.n)) = 0 := by rw [hkind, hs]\n refine and_true2 (holds_of_le (by rw [hk]; simp [evalE])) (and_true2 (impl_true fun _ => ?_) (impl_true fun h1 => ?_))\n · apply andL_true\n intro c hc\n rcases List.mem_cons.mp hc with rfl | hc\n · simp [trueC, evalC, evalE]\n rcases List.mem_append.mp hc with hc | hc\n · cases b\n · simp at hc\n · simp at hc; subst hc; exact hasm _ hs hal\n · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hc\n apply holds_of_eq\n rw [subst_evalE, subst_evalE]\n show applyS (foldσ fe (.var fe.n)) Q (foldMaps f fe ρ maps t) p.1 = _\n rw [fold_σ_in hS hw hs (hS.vst p.1 (hS.leafst p hp)), congr2E (ρ' := withInputs f.inputs ρ ι) (M' := maps) p.2\n (fun y hy => fold_at hS hQ hw hs (avoidsE_occ (hT.leafz p hp) hy)) (fun m hm => fm_o hS (avoidsMapsE_occ (hT.leafm p hp) hm))]\n simp only [foldT, applyS, assoc, lookup_unique hS.dleaf hp, Option.map_some]\n · rw [evalC_iff] at h1; simp only [holdsC, hk] at h1; simp [evalE] at h1\n | node ι l r =>\n obtain ⟨hb, _, _⟩ := flat_node t (Q fe.n) ι l r hs\n have pl := size_pos l\n have hk : evalE Q (foldMaps f fe ρ maps t) (.get fe.kind (.var fe.n)) = 1 := by rw [hkind, hs]\n refine and_true2 (holds_of_le (by rw [hk]; simp [evalE])) (and_true2 (impl_true fun h1 => ?_) (impl_true fun _ => ?_))\n · rw [evalC_iff] at h1; simp only [holdsC, hk] at h1; simp [evalE] at h1\n · apply andL_true\n intro c hc\n rcases List.mem_cons.mp hc with rfl | hc\n · apply holds_of_lt\n have hl' : evalE Q (foldMaps f fe ρ maps t) (.get fe.left (.var fe.n)) = Q fe.n - 1 - r.size := by\n simp only [evalE, fm_left hS, hs]\n rw [hl']; show Q fe.n - 1 - r.size < Q fe.n; omega\n rcases List.mem_cons.mp hc with rfl | hc\n · apply holds_of_lt\n have hr' : evalE Q (foldMaps f fe ρ maps t) (.get fe.right (.var fe.n)) = Q fe.n - 1 := by\n simp only [evalE, fm_right hS]\n rw [hr']; show Q fe.n - 1 < Q fe.n; omega\n rcases List.mem_append.mp hc with hc | hc\n · cases b\n · simp at hc\n · simp at hc; subst hc\n apply hasm _ hs\n show evalC (withInputs f.inputs ρ ι) maps f.assume = true\n rw [← congrC (withChildren f (withInputs f.inputs ρ ι) (foldT f ρ maps l) (foldT f ρ maps r)) _ maps f.assume\n (fun y hy => withChildren_plain f _ _ _\n (fun h => avoids_occ hT.asz hy (by simp only [foldNot, foldCopies, List.mem_append]; exact Or.inl (Or.inr (Or.inl h))))\n (fun h => avoids_occ hT.asz hy (by simp only [foldNot, foldCopies, List.mem_append]; exact Or.inl (Or.inr (Or.inr h)))))]\n exact hal.1\n · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hc\n apply holds_of_eq\n rw [subst_evalE, subst_evalE]\n show applyS (foldσ fe (.var fe.n)) Q (foldMaps f fe ρ maps t) p.1 = _\n rw [fold_σ_in hS hw hs (hS.vst p.1 (hS.leafst p hp))]\n simp only [foldT]\n rw [applyS_getD]\n obtain ⟨e, he⟩ : ∃ e, assoc f.node p.1 = some e := by\n cases h : assoc f.node p.1 with\n | some e => exact ⟨e, rfl⟩\n | none =>\n exfalso; obtain ⟨q, hq, hq1⟩ := List.mem_map.mp (hS.leafnode p hp)\n simp only [assoc, Option.map_eq_none_iff] at h\n have := lookup_none_all h q hq; simp [hq1] at this\n have hem := assoc_some_mem he\n rw [he]\n simp only [Option.getD]\n exact congr2E e (fun y hy => (fold_node hS hQ hs rfl (fun h => avoidsE_occ (hT.nodez _ hem) hy (List.mem_append_left _ h))\n (fun h => avoidsE_occ (hT.nodez _ hem) hy (by rw [h]; simp)) (fun h => avoidsE_occ (hT.nodez _ hem) hy (by rw [h]; simp))).symm)\n (fun m hm => (fm_o hS (avoidsMapsE_occ (hT.nodem _ hem) hm)).symm)\n\ndef foldEnv (fe : FoldEnc) (t : BTree) (ρ : Nat → Nat) : Nat → Nat := fun i =>\n if i = fe.n then t.size - 1 else if i = fe.N then t.size else ρ i\n\n/-- A cyclic proof of a fold's law over trees as tables (`foldNodes`), checked against the fold's table (`foldOk`): for\nparams that meet the precondition, the law holds of every tree whose inputs meet the assumption, as `fold_sound` says of\nthe structural induction. -/\ntheorem cyc_fold_sound (f : Fold) (fe : FoldEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (law : C) (nd : CNode) (hn : nd ∈ nodes) (hok : foldOk f fe facts law nd = true) (maps : Nat → Nat → Nat) (ρ : Nat → Nat)\n (h0 : evalC ρ maps f.pre = true) : ∀ t, Allowed f ρ maps t → evalC (foldT f ρ maps t) maps law = true := by\n intro t hA\n simp only [foldOk, Bool.and_eq_true] at hok\n obtain ⟨⟨⟨⟨hsh, hte⟩, hfa⟩, hcon⟩, hhy⟩ := hok\n have hS := foldShape_of hsh\n have hT := foldTerms_of hte\n obtain ⟨_, hnN, hx⟩ := dvars_parts hS\n have hE : ∀ i, i ≠ fe.n → i ≠ fe.N → foldEnv fe t ρ i = ρ i := fun i h1 h2 => by simp [foldEnv, h1, h2]\n have hEn : foldEnv fe t ρ fe.n = t.size - 1 := by simp [foldEnv]\n have hEN : foldEnv fe t ρ fe.N = t.size := by simp [foldEnv, Ne.symm hnN]\n have pt := size_pos t\n have hF : FactsHold facts (foldEnv fe t ρ) (foldMaps f fe ρ maps t) := by\n intro g hg vals\n have hgi := List.all_eq_true.mp hfa g hg\n simp only [Bool.or_eq_true] at hgi\n rcases hgi with hnc | hcan\n · exact natC_true _ hnc\n obtain ⟨g', hg', hb⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb\n rw [hb.1, beqC_eq hb.2]\n have hk : g'.keys = [fe.n] := by\n simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hg'\n rcases hg' with rfl | rfl <;> rfl\n have hQ : ∀ i, i ≠ fe.n → i ≠ fe.N → override g'.keys vals (foldEnv fe t ρ) i = ρ i := fun i h1 h2 => by\n rw [hk]; simp only [override]\n rw [if_neg (fun h => h1 (by simpa using h))]\n exact hE i h1 h2\n have hQN : override g'.keys vals (foldEnv fe t ρ) fe.N = t.size := by\n rw [hk]; simp only [override]\n rw [if_neg (fun h => hnN (by have h' : fe.N = fe.n := by simpa using h\n exact h'.symm))]\n exact hEN\n simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hg'\n rcases hg' with rfl | rfl\n · exact table_holds hS hT hA true hQ hQN\n · exact table_holds hS hT hA false hQ hQN\n have hH : ∀ c ∈ nd.hyps, evalC (foldEnv fe t ρ) (foldMaps f fe ρ maps t) c = true := by\n intro c hc\n have h := List.all_eq_true.mp hhy c hc\n simp only [Bool.or_eq_true] at h\n rcases h with h | h\n · rw [beqC_eq h, congr2C (ρ' := ρ) (M' := maps) _ (fun y hy => hE y\n (fun e => avoids_occ hT.prz hy (by rw [e]; simp [foldNot])) (fun e => avoids_occ hT.prz hy (by rw [e]; simp [foldNot])))\n (fun m hm => fm_o hS (avoidsMaps_occ hT.prm hm))]\n exact h0\n · rw [beqC_eq h]; apply holds_of_lt; simp only [evalE, hEn, hEN]; omega\n have hc0 := cyclic_sound facts nodes hc (foldMaps f fe ρ maps t) (foldEnv fe t ρ) hF nd hn hH\n have hw : evalE (foldEnv fe t ρ) (foldMaps f fe ρ maps t) (.var fe.n) = t.size - 1 := hEn\n rw [beqC_eq hcon, subst_evalC] at hc0\n refine (congr2C (ρ' := applyS (foldσ fe (.var fe.n)) (foldEnv fe t ρ) (foldMaps f fe ρ maps t)) (M' := foldMaps f fe ρ maps t) law\n (fun y hy => ?_) (fun m hm => (fm_o hS (avoidsMaps_occ hT.lawm hm)).symm)).trans hc0\n have hy' := avoids_occ hT.lawz hy\n have hyi : y ∉ f.inputs := fun h => hy' (List.mem_append_left _ (List.mem_append_left _ h))\n have hyc : y ∉ foldCopies f := fun h => hy' (List.mem_append_left _ (List.mem_append_right _ h))\n have hyn : y ≠ fe.n := fun e => hy' (by rw [e]; simp)\n have hyN : y ≠ fe.N := fun e => hy' (by rw [e]; simp)\n by_cases hv : y ∈ fe.vtr.map (·.1)\n · rw [fold_σ_in hS hw (flat_last t) hv]\n · rw [fold_σ_out hS hv, hE y hyn hyN, foldT_frame hS (fun h => hv (hS.vst y h)) hyc, withInputs_other hyi]\n\nend Kern\n",
17
17
  "BridgeRules.lean": "import Bridge\nimport Refine\nimport History\n\n/-!\nBridgeRules: the rest of today's rules carried by cyclic proofs. Lean 4.34.0, core only, on top of `Bridge.lean`.\n\nBridge.lean carries a cyclic proof's node to a machine's law about every reachable state. The rules beside the laws\nclaim more: a step law (and a k-step law) is about every k allowed requests in a row from every reachable state. Here\na node over the same encoding reads the run's last k steps up to time t, each time one step back from the next (t − 1,\nt − 1 − 1, …, as a step's fact reads the time before it): the first state at t − k, the j-th request at t − (k − j), the\nstate after them at t. Its hypotheses are among the precondition, k ≤ t, t ≤ T, T < Ω and each step's `when` at its time,\nits conclusion `then` with the plain names at t − k, each later request's inputs and the maps it carries at its time, and\nthe primed names at t (`cycKOk`). `cyc_kstep_sound`: such a node of a checked cyclic proof makes the k-step law hold, as\nSets.lean's `SKStepHolds` states it (the claim `skstep_sound` proves of the usual certificate). The run is the one a\nreachable state ends (`reach_run`), extended by the k requests (`run_ext`).\n\nFrom those, what the other rules claim, each from its claims' meaning in place of their certificates: liveness under\nfairness (`sleads_of_holds`) and weak fairness (`sleads_wf_of_holds`, a helpful request allowed being a node at the\nrun's last request, `cyc_allow_sound`), a history's laws through its plain machine (`hreach_plain`), a ghosted machine's\nlaws of the original (`cyc_ghost_holds`), a refinement (`srefines_of_holds`).\n\nFlags that hold at every key (a machine with `qflags`): Bridge.lean's encoding has none. Here the machine is read as its\nsk machine (`SMachine.sk`: the flags and their witnesses inputs a request names), whose runs, every request decided as a\ngoal reads it (`sReq`, `CRun.Decided`), are the machine's off the witnesses (`sk_step`, `sk_run`); the encoding adds the\nflags' closing conditions at each time (`cycQFacts`: 0 or 1, 0 or the body at every value of its keys, 1 or the body\nfailing at the witness), which hold on such runs (`qfacts_hold`, through Sets.lean's `closing_true`). `cyc_qmachine_sound`,\n`cyc_qkstep_sound` and `cyc_qallow_sound` are the three bridges for them.\n-/\n\nnamespace Kern\nset_option linter.unusedSimpArgs false\nset_option linter.unusedSectionVars false\n\n/-! A run extended by requests -/\n\ntheorem run_snoc_ok {s : SMachine} (hq : s.qflags = []) {r : CRun} {T : Nat} (hr : r.Ok s T) {ρ : Nat → Nat}\n {M : Nat → Nat → Nat} (hst : r.st T = ρ) (hmp : r.mp T = M) (ι : Nat → Nat) (κ : Nat → Nat → Nat)\n (ha : evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true) :\n (r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ).Ok s (T + 1) := by\n have hv : sView s ρ M ι κ = (decideFlags s.f.flags (withInputs s.f.m.inputs ρ ι) (withCarried s M κ), withCarried s M κ) := by\n simp only [sView, decideQ_nil hq]\n obtain ⟨l1, l2, l3, l4, l5, l6⟩ := snoc_last (r := r) (T := T) (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n obtain ⟨z1, z2, z3⟩ := snoc_zero (r := r) (T := T) (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n have hN : (r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ).N s T = (sView s ρ M ι κ).2 := by\n rw [hv, CRun.N, l4, l6, hmp]\n have hV : (r.snoc T (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ).V s T = (sView s ρ M ι κ).1 := by\n rw [CRun.V, hN, CRun.X, l3, l5, hst, hv]\n refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩\n · rw [z3]; exact hr.maps0\n · rw [z1, z3]; exact hr.pre\n · rw [z1, z3]; exact hr.facts\n · rw [z1, z3]; exact hr.pfacts\n · rw [z1, z2, z3]; exact hr.init\n · intro u hu\n rcases Nat.lt_or_ge u T with hlt | hge\n · obtain ⟨e1, e2, e3, e4⟩ := snoc_early (s := s) (r := r) hlt (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 ι κ\n rw [e1, e2, e3, e4]; exact hr.step u hlt\n · have hu' : u = T := by omega\n rw [hu', hV, hN, l1, l2]\n exact ⟨ha, rfl, rfl⟩\n\n-- A run that ends in a state, extended by requests the machine allows one after another.\ntheorem run_ext {s : SMachine} (hq : s.qflags = []) : ∀ (qs : List SRequest) (ws : List C) (r : CRun) (T : Nat)\n (ρ : Nat → Nat) (M : Nat → Nat → Nat), r.Ok s T → r.st T = ρ → r.mp T = M → qs.length ≤ ws.length → SAllowed s ws ρ M qs →\n ∃ r' : CRun, r'.Ok s (T + qs.length) ∧ r'.ρ0 = r.ρ0 ∧ (∀ u, u ≤ T → r'.st u = r.st u ∧ r'.mp u = r.mp u)\n ∧ (∀ u, u < T → r'.ι u = r.ι u ∧ r'.κ u = r.κ u)\n ∧ (∀ j, j ≤ qs.length → r'.st (T + j) = (sEnd s ρ M (qs.take j)).1 ∧ r'.mp (T + j) = (sEnd s ρ M (qs.take j)).2)\n ∧ (∀ j (h : j < qs.length), r'.ι (T + j) = qs[j].1 ∧ r'.κ (T + j) = qs[j].2)\n | [], _, r, T, ρ, M, hr, hst, hmp, _, _ => by\n refine ⟨r, hr, rfl, fun _ _ => ⟨rfl, rfl⟩, fun _ _ => ⟨rfl, rfl⟩, fun j hj => ?_, fun j hj => absurd hj (Nat.not_lt_zero j)⟩\n have : j = 0 := by simpa using hj\n subst this\n simp [sEnd, hst, hmp]\n | q :: qs, [], _, _, _, _, _, _, _, hlen, _ => by simp at hlen\n | q :: qs, w :: ws, r, T, ρ, M, hr, hst, hmp, hlen, hK => by\n obtain ⟨ha, _, hrest⟩ := hK\n have hr1 := run_snoc_ok hq hr hst hmp q.1 q.2 ha\n obtain ⟨l1, l2, l3, l4, l5, l6⟩ := snoc_last (r := r) (T := T) (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 q.1 q.2\n obtain ⟨r', hr', h0, hle, hlt, hj, hι⟩ := run_ext hq qs ws _ (T + 1) _ _ hr1 l1 l2 (by simp at hlen; omega) hrest\n refine ⟨r', by simpa [Nat.add_assoc, Nat.add_comm 1] using hr', h0, fun u hu => ?_, fun u hu => ?_, fun j hjl => ?_, fun j hjl => ?_⟩\n · obtain ⟨a, b⟩ := hle u (by omega)\n rw [a, b]; simp [CRun.snoc, hu]\n · obtain ⟨a, b⟩ := hlt u (by omega)\n rw [a, b]; simp [CRun.snoc, hu]\n · cases j with\n | zero =>\n obtain ⟨a, b⟩ := hle T (by omega)\n rw [Nat.add_zero, a, b, l3, l4, hst, hmp]; simp [sEnd]\n | succ j =>\n obtain ⟨a, b⟩ := hj j (by simp at hjl; omega)\n rw [show T + (j + 1) = T + 1 + j by omega, a, b]\n simp [sEnd]\n · cases j with\n | zero =>\n obtain ⟨a, b⟩ := hlt T (by omega)\n rw [Nat.add_zero, a, b, l5, l6]; simp\n | succ j =>\n obtain ⟨a, b⟩ := hι j (by simp at hjl; omega)\n rw [show T + (j + 1) = T + 1 + j by omega, a, b]\n simp\n\n-- Each `when` along the requests: the j-th met at the state the first j steps reach.\ntheorem sallowed_at {s : SMachine} : ∀ (qs : List SRequest) (ws : List C) (ρ : Nat → Nat) (M : Nat → Nat → Nat),\n SAllowed s ws ρ M qs → ∀ j (hq : j < qs.length) (hw : j < ws.length),\n evalC (withExt s (sEnd s ρ M (qs.take j)).1 (sReq s (sEnd s ρ M (qs.take j)).1 (sEnd s ρ M (qs.take j)).2 qs[j].1 qs[j].2))\n (withCarried s (sEnd s ρ M (qs.take j)).2 qs[j].2) ws[j] = true\n | [], _, _, _, _, j, hq, _ => absurd hq (Nat.not_lt_zero j)\n | _ :: _, [], _, _, _, j, _, hw => absurd hw (Nat.not_lt_zero j)\n | q :: qs, w :: ws, ρ, M, hK, j, hq, hw => by\n obtain ⟨_, hW, hrest⟩ := hK\n cases j with\n | zero => simpa [sEnd] using hW\n | succ j =>\n have := sallowed_at qs ws _ _ hrest j (by simp at hq; omega) (by simp at hw; omega)\n simpa [sEnd] using this\n\n/-! A machine with no flag at every key and no witness reads its request plainly -/\n\ntheorem sReq_plain {s : SMachine} (hq : s.qflags = []) (hw : s.wits = []) (ρ : Nat → Nat) (M : Nat → Nat → Nat)\n (ι : Nat → Nat) (κ : Nat → Nat → Nat) (v : Nat) : sReq s ρ M ι κ v = if s.f.m.inputs.contains v then ι v else 0 := by\n simp only [sReq, hq, hw, lookup]\n\ntheorem withExt_plain {s : SMachine} (hq : s.qflags = []) (hw : s.wits = []) (ρ e : Nat → Nat) (v : Nat) :\n withExt s ρ e v = if s.f.m.inputs.contains v then e v else ρ v := by\n simp only [withExt, SMachine.extVars, SMachine.qVars, SMachine.wVars, hq, hw, List.map_nil, List.flatMap_nil, List.append_nil]\n\ntheorem sExts_plain {s : SMachine} (hq : s.qflags = []) (hw : s.wits = []) : ∀ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (qs : List SRequest),\n sExts s ρ M qs = qs.map (fun q v => if s.f.m.inputs.contains v then q.1 v else 0)\n | _, _, [] => rfl\n | ρ, M, q :: qs => by\n simp only [sExts, List.map_cons, sExts_plain hq hw _ _ qs]\n congr 1; funext v; exact sReq_plain hq hw ρ M q.1 q.2 v\n\n/-! The encoding of a k-step law -/\n\n-- Time t − n, one step back at a time (t − 1 − 1 …), as a step's fact reads the time before it: the j-th state of k at\n-- t − (k − j), the last at t.\ndef tBack (ce : CycEnc) : Nat → E\n | 0 => .var ce.t\n | n + 1 => .sub (tBack ce n) (.const 1)\n\ntheorem tBack_eval (ce : CycEnc) (Q : Nat → Nat) (M : Nat → Nat → Nat) : ∀ n, evalE Q M (tBack ce n) = Q ce.t - n\n | 0 => by simp [tBack, evalE]\n | n + 1 => by simp [tBack, evalE, tBack_eval ce Q M n]; omega\n\n-- The same times read t − n at once (as core/translate.ts's step laws over several steps read them).\ndef tSub (ce : CycEnc) : Nat → E\n | 0 => .var ce.t\n | n + 1 => .sub (.var ce.t) (.const (n + 1))\n\ntheorem tSub_eval (ce : CycEnc) (Q : Nat → Nat) (M : Nat → Nat → Nat) : ∀ n, evalE Q M (tSub ce n) = Q ce.t - n\n | 0 => by simp [tSub, evalE]\n | n + 1 => by simp [tSub, evalE]\n\n-- Either: one step back at a time (b), or at once.\ndef tAtS (tb : Bool) (ce : CycEnc) (n : Nat) : E := if tb then tBack ce n else tSub ce n\n\ntheorem tAtS_eval (tb : Bool) (ce : CycEnc) (Q : Nat → Nat) (M : Nat → Nat → Nat) (n : Nat) : evalE Q M (tAtS tb ce n) = Q ce.t - n := by\n cases tb\n · simp only [tAtS, Bool.false_eq_true, ↓reduceIte]; exact tSub_eval ce Q M n\n · simp only [tAtS, ↓reduceIte]; exact tBack_eval ce Q M n\n\n-- The map over time of a traced variable, and of a map.\ndef cycVM (ce : CycEnc) (x : Nat) : Nat := ((lookup (fun p => p.1 == x) ce.vtr).map (·.2)).getD 0\ndef cycMM (ce : CycEnc) (m : Nat) : Nat := ((lookup (fun p => p.1 == m) ce.mtr).map (·.2)).getD 0\n\n-- Each later request's inputs at their fresh names, read at its time (of k steps, the j-th at t − (k − j)).\ndef kRenσ (tb : Bool) (ce : CycEnc) (k : Nat) : Nat → List (List (Nat × Nat)) → List (Nat × E)\n | _, [] => []\n | j, r :: rs => r.map (fun p => (p.2, .get (cycVM ce p.1) (tAtS tb ce (k - j)))) ++ kRenσ tb ce k (j + 1) rs\n\n-- The step and the carried map a fresh map name stands for.\ndef kMren : Nat → List (List (Nat × Nat)) → Nat → Option (Nat × Nat)\n | _, [], _ => none\n | j, r :: rs, m => match lookup (fun p => p.2 == m) r with\n | some p => some (j, p.1)\n | none => kMren (j + 1) rs m\n\n-- `then` over the run of k steps ending at t: primed names at t, each later request's inputs at its time, the rest at\n-- t − k.\ndef kσ (tb : Bool) (ce : CycEnc) (sl : SKStep) : List (Nat × E) :=\n sl.k.prime.map (fun q => (q.2, .get (cycVM ce q.1) (tAtS tb ce 0))) ++ kRenσ tb ce sl.k.whens.length 0 sl.k.ren\n ++ cycσ ce (tAtS tb ce sl.k.whens.length)\ndef kRd (tb : Bool) (ce : CycEnc) (sl : SKStep) : Nat → E → E := fun m key =>\n match lookup (fun q => q.2 == m) sl.k.primeMaps with\n | some q => .get (cycMM ce q.1) (.add (tAtS tb ce 0) (.prod key (.var ce.om)))\n | none => match kMren 0 sl.mren m with\n | some p => .get (cycMM ce p.2) (.add (tAtS tb ce (sl.k.whens.length - p.1)) (.prod key (.var ce.om)))\n | none => cycRd ce (tAtS tb ce sl.k.whens.length) m key\n\n-- Each step's `when` at its time.\ndef kWhens (tb : Bool) (ce : CycEnc) (k : Nat) : Nat → List C → List C\n | _, [] => []\n | j, w :: ws => trC ce (tAtS tb ce (k - j)) w :: kWhens tb ce k (j + 1) ws\n\ntheorem kWhens_mem (tb : Bool) (ce : CycEnc) (k : Nat) : ∀ (j : Nat) (ws : List C) {c : C}, c ∈ kWhens tb ce k j ws → ∃ i, ∃ h : i < ws.length, c = trC ce (tAtS tb ce (k - (j + i))) ws[i]\n | _, [], _, h => by simp [kWhens] at h\n | j, w :: ws, c, h => by\n simp only [kWhens, List.mem_cons] at h\n rcases h with rfl | h\n · exact ⟨0, by simp, by simp⟩\n · obtain ⟨i, hi, rfl⟩ := kWhens_mem tb ce k (j + 1) ws h\n exact ⟨i + 1, by simp; omega, by simp [Nat.add_assoc, Nat.add_comm 1 i]⟩\n\n/-- A k-step law's node, checked against the machine's encoding: the encoding fits the machine as for its laws, the\nlaw's fresh names stand for traced inputs and state variables and the maps for maps over time, `then` and each `when`\nread no added variable, flag or map over time, the conclusion is `then` over the run (`kσ`, `kRd`) and the hypotheses\nare among the precondition, k ≤ t, t ≤ T, T < Ω and each step's `when` at its time. -/\ndef cycKOkAt (tb : Bool) (s : SMachine) (ce : CycEnc) (facts : List Fact) (sl : SKStep) (n : CNode) : Bool :=\n cycShapeOk s ce [] && cycTermsOk s ce [] && s.wits.isEmpty && cycFactsIn s ce facts\n && sl.k.ren.length == sl.k.whens.length && sl.mren.length == sl.k.whens.length\n && sl.k.ren.all (fun r => r.all fun p => s.f.m.inputs.contains p.1)\n && sl.k.prime.all (fun q => (ce.vtr.map (·.1)).contains q.1 && !s.f.m.inputs.contains q.1)\n && sl.mren.all (fun r => r.all fun p => s.carried.contains p.1 && !ce.tm.contains p.2)\n && sl.k.primeMaps.all (fun q => (ce.mtr.map (·.1)).contains q.1 && !s.carried.contains q.1 && !ce.tm.contains q.2)\n && avoids (ce.special ++ flagVars s.f.flags) sl.k.then_ && avoidsMaps ce.tm sl.k.then_\n && sl.k.whens.all (fun w => avoids (ce.special ++ flagVars s.f.flags) w && avoidsMaps ce.tm w)\n && beqC n.concl (substRC (kσ tb ce sl) (kRd tb ce sl) sl.k.then_)\n && n.hyps.all (fun h => beqC h s.f.m.pre || beqC h (.le (.const sl.k.whens.length) (.var ce.t)) || beqC h (.le (.var ce.t) (.var ce.T))\n || beqC h (.lt (.var ce.T) (.var ce.om)) || (kWhens tb ce sl.k.whens.length 0 sl.k.whens).any (beqC h))\n\n/-! The encoding read back -/\n\ntheorem applyS_app (A B : List (Nat × E)) (Q : Nat → Nat) (M : Nat → Nat → Nat) (v : Nat) :\n applyS (A ++ B) Q M v = match assoc A v with | some e => evalE Q M e | none => applyS B Q M v := by\n simp only [applyS, assoc_append_or]; cases assoc A v <;> simp [Option.or]\n\ntheorem assoc_ren (r : List (Nat × Nat)) (g : Nat × Nat → E) (v : Nat) :\n assoc (r.map (fun p => (p.2, g p))) v = (lookup (fun p => p.2 == v) r).map g := by\n simp only [assoc, lookup_map_pred, Option.map_map]; rfl\n\ntheorem not_tgt_cons {r : List (Nat × Nat)} {rs : List (List (Nat × Nat))} {v : Nat} (hv : v ∈ tgts (r :: rs))\n (hn : lookup (fun p => p.2 == v) r = none) : v ∈ tgts rs := by\n simp only [tgts, List.mem_append] at hv\n rcases hv with h | h\n · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h\n have := lookup_none_all hn p hp; simp at this\n · exact h\n\n-- Each later request's inputs at their fresh names: the run's request at its time, as renEnv reads it.\ntheorem kRen_eval (tb : Bool) (ce : CycEnc) (k : Nat) (Q : Nat → Nat) (cm : Nat → Nat → Nat) (b : Nat → Nat) (rest : List (Nat × E)) :\n ∀ (rs : List (List (Nat × Nat))) (j : Nat) (ιs : List (Nat → Nat)), rs.length ≤ ιs.length →\n (∀ i (hi : i < rs.length) (hι : i < ιs.length), ∀ p ∈ rs[i], evalE Q cm (.get (cycVM ce p.1) (tAtS tb ce (k - (j + i)))) = ιs[i] p.1) →\n ∀ v, v ∈ tgts rs → applyS (kRenσ tb ce k j rs ++ rest) Q cm v = renEnv rs ιs b v\n | [], _, _, _, _, v, hv => by simp [tgts] at hv\n | _ :: _, _, [], hl, _, _, _ => by simp at hl\n | r :: rs, j, ι :: ιs, hl, hX, v, hv => by\n simp only [kRenσ, List.append_assoc]\n rw [applyS_app, assoc_ren]\n simp only [renEnv]\n cases hr : lookup (fun p => p.2 == v) r with\n | some p =>\n have h0 := hX 0 (by simp) (by simp) p (lookup_some hr).1\n simp only [Nat.add_zero, List.getElem_cons_zero] at h0\n simpa using h0\n | none =>\n simp only [Option.map_none]\n exact kRen_eval tb ce k Q cm b rest rs (j + 1) ιs (by simp at hl; omega)\n (fun i hi hι p hp => by\n have := hX (i + 1) (by simp; omega) (by simp; omega) p (by simpa using hp)\n simpa [Nat.add_assoc, Nat.add_comm 1 i] using this) v (not_tgt_cons hv hr)\n\ntheorem kRen_out (tb : Bool) (ce : CycEnc) (k : Nat) (Q : Nat → Nat) (cm : Nat → Nat → Nat) (rest : List (Nat × E)) :\n ∀ (rs : List (List (Nat × Nat))) (j : Nat) (v : Nat), v ∉ tgts rs → applyS (kRenσ tb ce k j rs ++ rest) Q cm v = applyS rest Q cm v\n | [], _, _, _ => by simp [kRenσ]\n | r :: rs, j, v, hv => by\n simp only [kRenσ, List.append_assoc]\n rw [applyS_app, assoc_ren]\n have hn : lookup (fun p => p.2 == v) r = none := lookup_none (fun p hp => by\n simp only [beq_eq_false_iff_ne]; intro he; exact hv (by simp only [tgts, List.mem_append]; exact Or.inl (List.mem_map.mpr ⟨p, hp, he⟩)))\n simp only [hn, Option.map_none]\n exact kRen_out tb ce k Q cm rest rs (j + 1) v (fun h => hv (by simp only [tgts, List.mem_append]; exact Or.inr h))\n\n-- The maps a later request carries at their fresh names: the run's at its time, as renMaps reads them.\ntheorem kMren_eval : ∀ (rs : List (List (Nat × Nat))) (j : Nat) (κs : List (Nat → Nat → Nat)) (b : Nat → Nat → Nat) (m : Nat),\n rs.length ≤ κs.length →\n (kMren j rs m = none → renMaps rs κs b m = b m)\n ∧ (∀ i c, kMren j rs m = some (i, c) → ∃ (i' : Nat) (hi : i' < rs.length) (hκ : i' < κs.length), i = j + i'\n ∧ (∃ p ∈ rs[i'], p.1 = c ∧ p.2 = m) ∧ renMaps rs κs b m = κs[i'] c)\n | [], _, κs, b, m, _ => ⟨fun _ => by cases κs <;> rfl, fun _ _ h => by simp [kMren] at h⟩\n | _ :: _, _, [], _, _, hl => by simp at hl\n | r :: rs, j, κ :: κs, b, m, hl => by\n have ih := kMren_eval rs (j + 1) κs b m (by simp at hl; omega)\n cases hr : lookup (fun p => p.2 == m) r with\n | some p =>\n refine ⟨fun h => by simp [kMren, hr] at h, fun i c h => ?_⟩\n simp only [kMren, hr, Option.some.injEq, Prod.mk.injEq] at h\n obtain ⟨rfl, rfl⟩ := h\n exact ⟨0, by simp, by simp, by simp, ⟨p, by simpa using (lookup_some hr).1, rfl, by simpa using (lookup_some hr).2⟩, by simp [renMaps, hr]⟩\n | none =>\n refine ⟨fun h => ?_, fun i c h => ?_⟩\n · simp only [kMren, hr] at h; simp only [renMaps, hr]; exact ih.1 h\n · simp only [kMren, hr] at h\n obtain ⟨i', hi, hκ, rfl, hp, he⟩ := ih.2 i c h\n exact ⟨i' + 1, by simp; omega, by simp; omega, by omega, by simpa using hp, by simp [renMaps, hr, he]⟩\n\ntheorem cycVM_spec {ce : CycEnc} {x : Nat} (h : x ∈ ce.vtr.map (·.1)) : ∃ p ∈ ce.vtr, p.1 = x ∧ cycVM ce x = p.2 := by\n cases hl : lookup (fun p => p.1 == x) ce.vtr with\n | none => obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; have := lookup_none_all hl p hp; simp at this\n | some p => exact ⟨p, (lookup_some hl).1, by simpa using (lookup_some hl).2, by simp [cycVM, hl]⟩\ntheorem cycMM_spec {ce : CycEnc} {m : Nat} (h : m ∈ ce.mtr.map (·.1)) : ∃ p ∈ ce.mtr, p.1 = m ∧ cycMM ce m = p.2 := by\n cases hl : lookup (fun p => p.1 == m) ce.mtr with\n | none => obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; have := lookup_none_all hl p hp; simp at this\n | some p => exact ⟨p, (lookup_some hl).1, by simpa using (lookup_some hl).2, by simp [cycMM, hl]⟩\n\n-- A traced variable's map over time, read at a time within the run: the run's state with its request there.\ntheorem read_vm {s : SMachine} {ce : CycEnc} {r : CRun} {om : Nat} (hd : distinct ce.tm = true) {x : Nat} (hx : x ∈ ce.vtr.map (·.1)) (u : Nat) :\n cycMaps s ce r om (cycVM ce x) u = r.X s u x := by\n obtain ⟨p, hp, rfl, he⟩ := cycVM_spec hx\n rw [he, cm_v hd hp]\n-- A map's map over time, read at a time within the run and a key.\ntheorem read_mm {s : SMachine} {ce : CycEnc} {r : CRun} {T : Nat} (hd : distinct ce.tm = true) {m : Nat} (hm : m ∈ ce.mtr.map (·.1))\n {u : Nat} (hu : u ≤ T) (kv : Nat) : cycMaps s ce r (T + 1) (cycMM ce m) (u + kv * (T + 1)) = r.N s u m kv := by\n obtain ⟨p, hp, rfl, he⟩ := cycMM_spec hm\n rw [he, cm_m hd hp]; simp only [pair_mod hu, pair_div hu]\n\n/-! The bridge for step laws and k-step laws -/\n\n-- The bridge for one time form (`tAtS b`).\ntheorem cyc_kstep_sound_at (tb : Bool) (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (sl : SKStep) (n : CNode) (hn : n ∈ nodes) (hok : cycKOkAt tb s ce facts sl n = true) : SKStepHolds s sl := by\n simp only [cycKOkAt, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hok\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hsh, hte⟩, hwit⟩, hfa⟩, hrl⟩, hml⟩, hrin⟩, hpr⟩, hmr⟩, hpm⟩, hthv⟩, hthm⟩, hwh⟩, hcon⟩, hhy⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n have hW : s.wits = [] := isEmpty_nil hwit\n have hsp := special_parts hS.dsp\n intro ρ M hr q₁ qs hlen hK\n obtain ⟨r, T0, hr0, hst, hmp⟩ := reach_run hS.q hr\n obtain ⟨r', hr', _, _, _, hj, hι⟩ := run_ext hS.q (q₁ :: qs) sl.k.whens r T0 ρ M hr0 hst hmp (by simp; omega) hK\n have hk : sl.k.whens.length = (q₁ :: qs).length := by simp; omega\n -- The node's environment: t and T the run's end (the last state's time), Ω one above it, the rest from before the\n -- start.\n obtain ⟨Q, hQt, hQT, hQo, hQr⟩ : ∃ Q : Nat → Nat, Q ce.t = T0 + (q₁ :: qs).length ∧ Q ce.T = T0 + (q₁ :: qs).length ∧ Q ce.om = T0 + (q₁ :: qs).length + 1\n ∧ ∀ i, i ∉ ce.special → Q i = r'.ρ0 i :=\n ⟨fun i => if i = ce.t then T0 + (q₁ :: qs).length else if i = ce.T then T0 + (q₁ :: qs).length else if i = ce.om then T0 + (q₁ :: qs).length + 1 else r'.ρ0 i,\n by simp, by simp [Ne.symm hsp.2.2.2.2.2.1], by simp [Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1],\n fun i hi => by\n dsimp only\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]⟩\n have hEnv : EnvOk s ce [] r' (T0 + (q₁ :: qs).length) Q := ⟨hQT, hQo, fun i hi => hQr i (notP hi).1⟩\n -- The state at each time, and each request at its time.\n have hst0 : r'.st T0 = ρ ∧ r'.mp T0 = M := by simpa [sEnd] using hj 0 (by simp)\n have hι0 : r'.ι T0 = q₁.1 ∧ r'.κ T0 = q₁.2 := by\n have h := hι 0 (by simp); simp only [Nat.add_zero, List.getElem_cons_zero] at h; exact h\n have hend : r'.st (T0 + (q₁ :: qs).length) = (sEnd s ρ M (q₁ :: qs)).1 ∧ r'.mp (T0 + (q₁ :: qs).length) = (sEnd s ρ M (q₁ :: qs)).2 := by\n simpa using hj (q₁ :: qs).length (Nat.le_refl _)\n have hframe : ∀ u, u ≤ T0 + (q₁ :: qs).length → ∀ i, i ∉ ce.vtr.map (·.1) → i ∉ flagVars s.f.flags → r'.st u i = r'.ρ0 i := fun u hu i hv hf =>\n run_frame hr' u hu i (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.stepv p hp)) (fun h => hv (hS.inv i h)) hf\n have hw0 : evalE Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) (tAtS tb ce sl.k.whens.length) = T0 := by rw [tAtS_eval tb, hQt, hk]; omega\n -- The facts hold of the run.\n have hF : FactsHold facts Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) := by\n intro f hf vals\n have hfi := List.all_eq_true.mp hfa f hf\n simp only [Bool.or_eq_true] at hfi\n rcases hfi with hnc | hcan\n · exact natC_true _ hnc\n · obtain ⟨g, hg, hb⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb\n rw [hb.1, beqC_eq hb.2]\n exact canon_holds hS hT hr' hg hEnv vals\n -- The node's hypotheses hold there.\n have hH : ∀ c ∈ n.hyps, evalC Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) c = true := by\n intro c hc\n have h := hhy c hc\n simp only [Bool.or_eq_true] at h\n rcases h with (((h | h) | h) | h) | h\n · rw [beqC_eq h, congr2C (ρ' := r'.ρ0) (M' := r'.mp 0) _ (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj))\n (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr'.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hk]; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hQT]; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQT, hQo]; omega\n · obtain ⟨c', hc', hb⟩ := List.any_eq_true.mp h\n rw [beqC_eq hb]\n obtain ⟨i, hi, rfl⟩ := kWhens_mem tb ce sl.k.whens.length 0 sl.k.whens hc'\n have hiq : i < (q₁ :: qs).length := by omega\n have hu : T0 + i ≤ T0 + (q₁ :: qs).length := by omega\n have hwi : evalE Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) (tAtS tb ce (sl.k.whens.length - (0 + i))) = T0 + i := by rw [tAtS_eval tb, hQt, hk]; omega\n have hall := sallowed_at (q₁ :: qs) sl.k.whens ρ M hK i hiq hi\n obtain ⟨hsti, hmpi⟩ := hj i (by omega)\n obtain ⟨hιi, hκi⟩ := hι i hiq\n have hav := hwh _ (List.getElem_mem hi)\n rw [enc_trC hS hT hr' hEnv hwi hu]\n rw [congr2C (ρ' := withExt s (sEnd s ρ M ((q₁ :: qs).take i)).1 (sReq s (sEnd s ρ M ((q₁ :: qs).take i)).1 (sEnd s ρ M ((q₁ :: qs).take i)).2 (q₁ :: qs)[i].1 (q₁ :: qs)[i].2))\n (M' := withCarried s (sEnd s ρ M ((q₁ :: qs).take i)).2 (q₁ :: qs)[i].2) _ ?_ ?_]\n · exact hall\n · intro x hx\n have hxz := avoids_occ hav.1 hx\n rw [withExt_plain hS.q hW, sReq_plain hS.q hW]\n by_cases hv : x ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr' hEnv hwi hu hv, CRun.X, hsti, hιi]; simp only [withInputs]; split <;> rfl\n · have hxi : x ∉ s.f.m.inputs := fun h => hv (hS.inv x h)\n rw [enc_σ_out hS hT hr' hEnv hwi hu hv, if_neg (fun h => hxi (List.contains_iff_mem.mp h)), ← hsti,\n hframe _ hu x hv (fun h => hxz (List.mem_append_right _ h))]\n exact hQr x (fun h => hxz (List.mem_append_left _ h))\n · intro m hm\n rw [cv_N (avoidsMaps_occ hav.2 hm), CRun.N, hmpi, hκi]\n have hc0 := cyclic_sound facts nodes hc (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) Q hF n hn hH\n rw [beqC_eq hcon] at hc0\n -- `then` over the run: the maps it reads, then its names.\n have hTle : T0 ≤ T0 + (q₁ :: qs).length := Nat.le_add_right _ _\n let Mt : Nat → Nat → Nat := primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 (renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2))\n have hrd : ∀ m key, evalE Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) (kRd tb ce sl m key)\n = (fun m => if ce.tm.contains m then cycMaps s ce r' (T0 + (q₁ :: qs).length + 1) m else Mt m) m\n (evalE Q (cycMaps s ce r' (T0 + (q₁ :: qs).length + 1)) key) := by\n intro m key\n simp only [kRd]\n cases hq : lookup (fun q => q.2 == m) sl.k.primeMaps with\n | some q =>\n obtain ⟨hqm, hqe⟩ := lookup_some hq\n have hqe' : q.2 = m := by simpa using hqe\n obtain ⟨⟨hq1, hq2⟩, hq3⟩ := hpm q hqm\n have hnt : ¬ ce.tm.contains m = true := by rw [← hqe']; simpa using hq3\n simp only [evalE, tAtS_eval tb, hQt, hQo, Nat.sub_zero]\n rw [read_mm hS.dtm (List.contains_iff_mem.mp hq1) (Nat.le_refl _), N_not_carried (by simpa using hq2), hend.2, if_neg hnt]\n simp only [Mt, primedMaps, hq]\n | none =>\n have hkm := kMren_eval sl.mren 0 ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2) m (by simp; omega)\n cases hkr : kMren 0 sl.mren m with\n | some p =>\n obtain ⟨i', hi', hκ', hi0, ⟨p', hp', hp1, hp2⟩, hren⟩ := hkm.2 p.1 p.2 hkr\n obtain ⟨hcar, hnt0⟩ := hmr _ (List.getElem_mem hi') p' hp'\n have hnt : ¬ ce.tm.contains m = true := by rw [← hp2]; simpa using hnt0\n have hiq : i' < (q₁ :: qs).length := by simpa using hκ'\n obtain ⟨hιi, hκi⟩ := hι i' hiq\n simp only [evalE, tAtS_eval tb, hQt, hQo, hi0, Nat.zero_add]\n rw [show T0 + (q₁ :: qs).length - (sl.k.whens.length - i') = T0 + i' by rw [hk]; omega]\n rw [read_mm hS.dtm (hS.carm _ (by rw [← hp1]; exact List.contains_iff_mem.mp hcar)) (by omega), CRun.N, hκi, if_neg hnt]\n simp only [Mt, primedMaps, hq, hren, withCarried, ← hp1, hcar, if_true, List.getElem_map]\n | none =>\n rw [enc_rd hS hT hr' hEnv hw0 hTle]\n simp only [cycView]\n by_cases ht : ce.tm.contains m = true\n · rw [if_pos ht, if_pos ht]\n · rw [if_neg ht, if_neg ht, CRun.N, hst0.2, hι0.2]\n simp only [Mt, primedMaps, hq, hkm.1 hkr]\n rw [substR_evalC (kσ tb ce sl) (kRd tb ce sl) Q _ _ hrd] at hc0\n rw [congr2C (ρ' := primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 (renEnv sl.k.ren (sExts s ρ M (q₁ :: qs)) (withExt s ρ (sReq s ρ M q₁.1 q₁.2))))\n (M' := Mt) _ ?_ ?_] at hc0\n · exact hc0\n · intro x hx\n have hxz := avoids_occ hthv hx\n simp only [kσ, List.append_assoc]\n rw [applyS_app, assoc_ren]\n simp only [primed]\n cases hq : lookup (fun q => q.2 == x) sl.k.prime with\n | some q =>\n obtain ⟨hqm, _⟩ := lookup_some hq\n obtain ⟨hq1, hq2⟩ := hpr q hqm\n simp only [Option.map_some, evalE, tAtS_eval tb, hQt, Nat.sub_zero]\n rw [read_vm hS.dtm (List.contains_iff_mem.mp hq1), CRun.X, withInputs_other (by simpa using hq2), hend.1]\n | none =>\n simp only [Option.map_none]\n by_cases hxr : x ∈ tgts sl.k.ren\n · rw [kRen_eval tb ce sl.k.whens.length Q _ _ _ sl.k.ren 0 (sExts s ρ M (q₁ :: qs)) (by rw [sExts_plain hS.q hW]; simp; omega) ?_ x hxr]\n intro i hi hιl p hp\n have hpin := hrin _ (List.getElem_mem hi) p hp\n have hiq : i < (q₁ :: qs).length := by rw [sExts_plain hS.q hW] at hιl; simpa using hιl\n obtain ⟨hιi, _⟩ := hι i hiq\n simp only [evalE, tAtS_eval tb, hQt, Nat.zero_add]\n rw [show T0 + (q₁ :: qs).length - (sl.k.whens.length - i) = T0 + i by rw [hk]; omega]\n rw [read_vm hS.dtm (hS.inv _ (List.contains_iff_mem.mp hpin)), CRun.X]\n simp only [withInputs, hpin, if_true, hιi]\n have hx : ∀ (l : List (Nat → Nat)), l = (q₁ :: qs).map (fun q v => if s.f.m.inputs.contains v then q.1 v else 0) →\n ∀ h : i < l.length, (q₁ :: qs)[i].1 p.1 = l[i] p.1 := by\n intro l hl h; subst hl; simp only [List.getElem_map, hpin, if_true]\n exact hx _ (sExts_plain hS.q hW _ _ _) hιl\n · rw [kRen_out tb ce sl.k.whens.length Q _ _ sl.k.ren 0 x hxr, renEnv_other _ _ _ x hxr, withExt_plain hS.q hW, sReq_plain hS.q hW]\n by_cases hv : x ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr' hEnv hw0 hTle hv, CRun.X, hst0.1, hι0.1]; simp only [withInputs]; split <;> rfl\n · have hxi : x ∉ s.f.m.inputs := fun h => hv (hS.inv x h)\n rw [enc_σ_out hS hT hr' hEnv hw0 hTle hv, if_neg (fun h => hxi (List.contains_iff_mem.mp h)), ← hst0.1,\n hframe _ hTle x hv (fun h => hxz (List.mem_append_right _ h))]\n exact hQr x (fun h => hxz (List.mem_append_left _ h))\n · intro m hm\n simp only [if_neg (fun h => avoidsMaps_occ hthm hm (List.contains_iff_mem.mp h))]\n\n-- A step law's node with its times either way (`cycKOkAt`).\ndef cycKOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (sl : SKStep) (n : CNode) : Bool :=\n cycKOkAt true s ce facts sl n || cycKOkAt false s ce facts sl n\n\n/-- A cyclic proof of a k-step law (a step law, k = 1), checked against the machine's encoding (`cycKOk`): from every\nreachable state, every k requests in a row the machine allows, each meeting its `when`, end where `then` holds. -/\ntheorem cyc_kstep_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (sl : SKStep) (n : CNode) (hn : n ∈ nodes) (hok : cycKOk s ce facts sl n = true) : SKStepHolds s sl := by\n simp only [cycKOk, Bool.or_eq_true] at hok\n rcases hok with h | h\n · exact cyc_kstep_sound_at true s ce facts nodes hc sl n hn h\n · exact cyc_kstep_sound_at false s ce facts nodes hc sl n hn h\n\n/-! Liveness from step laws -/\n\n-- A step law Lean builds from a liveness (Live.lean's `sliveStep`), with no certificate: one `when`, its `then`, the\n-- liveness's primed names.\ndef liveK (l : Live) (w t : C) : SKStep := ⟨⟨[w], t, [[]], l.prime, l.primeMaps, [], ([], .leaf [] [] [] [])⟩, [[]], [], [], [], [], [], []⟩\n\n/-- Liveness under fairness from its step laws: if the liveness reads what it may (`sliveOk`) and the step laws Lean\nbuilds from it hold (`SKStepHolds`, a cyclic proof's through `cyc_kstep_sound`), then along every run that takes each\nhelpful request infinitely often, each state where `from` holds is followed by one where `to` holds. `sleads_sound`\nwith the step laws' meaning in place of their certificates. -/\ntheorem sleads_of_holds (s : SMachine) (l : Live) (hok : sliveOk s l = true)\n (hs : SKStepHolds s (liveK l l.pend (liveStayC l))) (hc : SKStepHolds s (liveK l l.pend (liveCoverC l)))\n (hh : ∀ h ∈ l.helps, SKStepHolds s (liveK l (liveHelpWhen l h) (liveHelpThen l))) (run : SRun s)\n (fair : ∀ h ∈ l.helps, ∀ n, ∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :\n ∀ n, evalC (run.st n).1 (run.st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 l.to = true := by\n have hok0 := hok\n simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok0\n obtain ⟨⟨⟨hd1, hd2⟩, _⟩, hreqs⟩ := hok0\n refine leads_view l run.st (fun n => withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2))\n (fun n => primed l.prime (run.st (n + 1)).1 (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)))\n (fun n => withCarried s (run.st n).2 (run.req n).2) (fun n => primedMaps l.primeMaps (run.st (n + 1)).2 (withCarried s (run.st n).2 (run.req n).2))\n (fun h n => ∀ p ∈ h.2, (run.req n).1 p.1 = evalE (run.st n).1 (run.st n).2 p.2) (fun _ _ => True) ?_ ?_ ?_ ?_ ?_ ?_\n (fun _ _ _ _ _ _ => trivial) (fun h hh n _ => fair h hh n)\n · intro n c hc\n obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok hc (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2\n rw [run.next n]\n exact view_C hd1 hd2 hv hm hx hn\n · intro n e he\n obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok (mem_conds_rank he) (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2\n rw [run.next n]\n exact view_E hd1 hd2 hv hm hx hn\n · intro n hB\n have := skstep_holds_one hs rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro n hB\n have := skstep_holds_one hc rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro h hmem n hB\n have := skstep_holds_one (hh h hmem) rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro h hmem n hH\n show evalC (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)) (withCarried s (run.st n).2 (run.req n).2) (reqC h.2) = true\n exact sreq_list (fun p hp => hreqs h hmem p hp) (fun p hp' => hH p hp')\n\n/-! Histories through their plain machine -/\n\n/-- A machine with a history reaches only states its plain machine reaches, once the record goals and the history's\nside conditions check (`checkHMachine` with no laws: `histOk`, the record goals, the plain machine's own conditions). -/\ntheorem hreach_plain (h : HMachine) (rs : List (List VDef × Tree)) (hc : checkHMachine h [] rs = true) :\n ∀ ρ M hist, HReach h ρ M hist → SReach h.plain ρ M := by\n simp only [checkHMachine, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hc\n obtain ⟨⟨⟨hok, hlen⟩, hgoals⟩, hsm⟩ := hc\n have hrs : ∀ r ∈ h.rels, ∃ tt : List VDef × Tree, checkGoal ⟨tt.1, recordGoal h r⟩ tt.2 = true := by\n intro r hr\n obtain ⟨tt, htt⟩ := zip_mem_left h.rels rs hlen.symm r hr\n exact ⟨tt, hgoals (r, tt) htt⟩\n intro ρ M hist hr\n exact hreach_sreach [] (histOk_sound hok) hrs hsm hr\n\n/-! Ghosts -/\n\n/-- A law of a ghosted machine (a cyclic proof's, as `cyc_machine_sound` states it) that reads no ghost holds in every\nreachable state of the original, at every value of its keys. -/\ntheorem cyc_ghost_holds (g : SGhosted) (hg : sghostOk g = true) (law : C) (keys : List Nat) (hav : avoids g.vars law = true)\n (hl : ∀ ρ M, SReach g.full ρ M → ∀ ρ' : Nat → Nat, (∀ i, i ∉ keys → ρ' i = ρ i) → evalC ρ' M law = true) :\n ∀ ρ M, SReach g.orig ρ M → ∀ ρ' : Nat → Nat, (∀ i, i ∉ keys → ρ' i = ρ i) → evalC ρ' M law = true := by\n intro ρ M hr ρ' hρ'\n obtain ⟨ρg, hrg, hag⟩ := sghost_reach g hg hr\n have h := hl ρg M hrg (fun i => if keys.contains i then ρ' i else ρg i) (fun i hi => by\n rw [if_neg (fun h => hi (List.contains_iff_mem.mp h))])\n rw [congrC _ ρ' M law (fun i hi => by\n by_cases hk : keys.contains i = true\n · rw [if_pos hk]\n · rw [if_neg hk, hag i (avoids_occ hav hi), hρ' i (fun h => hk (List.contains_iff_mem.mpr h))])] at h\n exact h\n\n/-! A helpful request allowed, and liveness under weak fairness -/\n\n/-- A request allowed: from every reachable state, every request that meets `w`, carrying empty sets, is one the\nmachine allows (its assumption holds, the flags decided): what `sallow_sound` proves of the usual certificate. -/\ndef AllowHolds (s : SMachine) (w : C) : Prop :=\n ∀ ρ M, SReach s ρ M → ∀ ι, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) w = true →\n evalC (sView s ρ M ι noSets).1 (sView s ρ M ι noSets).2 s.f.m.assume = true\n\n-- A carried map's cell at time t, 0: the request at t carries empty sets.\ndef cycZeroOk (s : SMachine) (ce : CycEnc) : C → Bool\n | .eq (.get m (.add (.var a) (.prod _ (.var b)))) (.const 0) => a == ce.t && b == ce.om && s.carried.any (fun c => cycMM ce c == m)\n | _ => false\n\n/-- A request allowed, as a node at time t over the machine's encoding: the request taken at t (the run's last, which no\nstep reads) is the one the claim is about. The node's conclusion is the assumption at t, flags inlined; its hypotheses\namong the precondition, t ≤ T, T < Ω, `w` at t, and a carried map's cell at t being 0. -/\ndef cycAOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (w : C) (n : CNode) : Bool :=\n cycShapeOk s ce [] && cycTermsOk s ce [] && s.wits.isEmpty && cycFactsIn s ce facts\n && avoids (ce.special ++ flagVars s.f.flags) w && avoidsMaps ce.tm w\n && beqC n.concl (trC ce (.var ce.t) (inlineC s.f.flags s.f.m.assume))\n && n.hyps.all (fun h => beqC h s.f.m.pre || beqC h (.le (.var ce.t) (.var ce.T)) || beqC h (.lt (.var ce.T) (.var ce.om))\n || beqC h (trC ce (.var ce.t) w) || cycZeroOk s ce h)\n\ntheorem cycZeroOk_spec {s : SMachine} {ce : CycEnc} {h : C} (hz : cycZeroOk s ce h = true) :\n ∃ c key, c ∈ s.carried ∧ h = .eq (.get (cycMM ce c) (.add (.var ce.t) (.prod key (.var ce.om)))) (.const 0) := by\n match h, hz with\n | .eq (.get m (.add (.var a) (.prod key (.var b)))) (.const 0), hz =>\n simp only [cycZeroOk, Bool.and_eq_true, beq_iff_eq, List.any_eq_true] at hz\n obtain ⟨⟨rfl, rfl⟩, c, hc, hm⟩ := hz\n exact ⟨c, key, hc, by rw [hm]⟩\n\n/-- A cyclic proof of a request's being allowed, checked against the machine's encoding (`cycAOk`). -/\ntheorem cyc_allow_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (w : C) (n : CNode) (hn : n ∈ nodes) (hok : cycAOk s ce facts w n = true) : AllowHolds s w := by\n simp only [cycAOk, Bool.and_eq_true, List.all_eq_true] at hok\n obtain ⟨⟨⟨⟨⟨⟨⟨hsh, hte⟩, hwit⟩, hfa⟩, hwv⟩, hwm⟩, hcon⟩, hhy⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n have hW : s.wits = [] := isEmpty_nil hwit\n have hsp := special_parts hS.dsp\n intro ρ M hr ι hw\n obtain ⟨r, T, hr0, hst, hmp⟩ := reach_run hS.q hr\n -- The run with the request at T (the last, which no step takes) the claim's, carrying empty sets.\n let r2 : CRun := ⟨r.ρ0, r.st, r.mp, fun u => if u = T then ι else r.ι u, fun u => if u = T then noSets else r.κ u⟩\n have hNX : ∀ u, u < T → r2.N s u = r.N s u ∧ r2.V s u = r.V s u := fun u hu => by\n have hne : u ≠ T := Nat.ne_of_lt hu\n refine ⟨?_, ?_⟩ <;> simp only [CRun.V, CRun.X, CRun.N, r2, if_neg hne]\n have hr2 : r2.Ok s T := ⟨hr0.maps0, hr0.pre, hr0.facts, hr0.pfacts, hr0.init, fun u hu => by\n rw [(hNX u hu).1, (hNX u hu).2]; exact hr0.step u hu⟩\n have hX : r2.X s T = withInputs s.f.m.inputs ρ ι := by simp only [CRun.X, r2, if_pos rfl, hst, if_true]\n have hN : r2.N s T = withCarried s M noSets := by simp only [CRun.N, r2, if_pos rfl, hmp, if_true]\n obtain ⟨Q, hQt, hQT, hQo, hQr⟩ : ∃ Q : Nat → Nat, Q ce.t = T ∧ Q ce.T = T ∧ Q ce.om = T + 1 ∧ ∀ i, i ∉ ce.special → Q i = r.ρ0 i :=\n ⟨fun i => if i = ce.t then T else if i = ce.T then T else if i = ce.om then T + 1 else r.ρ0 i,\n by simp, by simp [Ne.symm hsp.2.2.2.2.2.1], by simp [Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1],\n fun i hi => by\n dsimp only\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]⟩\n have hEnv : EnvOk s ce [] r2 T Q := ⟨hQT, hQo, fun i hi => hQr i (notP hi).1⟩\n have hwt : evalE Q (cycMaps s ce r2 (T + 1)) (.var ce.t) = T := by simp [evalE, hQt]\n have hF : FactsHold facts Q (cycMaps s ce r2 (T + 1)) := by\n intro f hf vals\n have hfi := List.all_eq_true.mp hfa f hf\n simp only [Bool.or_eq_true] at hfi\n rcases hfi with hnc | hcan\n · exact natC_true _ hnc\n · obtain ⟨g, hg, hb⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb\n rw [hb.1, beqC_eq hb.2]\n exact canon_holds hS hT hr2 hg hEnv vals\n have hH : ∀ c ∈ n.hyps, evalC Q (cycMaps s ce r2 (T + 1)) c = true := by\n intro c hc\n have h := hhy c hc\n simp only [Bool.or_eq_true] at h\n rcases h with ((((h | h) | h) | h) | h)\n · rw [beqC_eq h, congr2C (ρ' := r2.ρ0) (M' := r2.mp 0) _ (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj))\n (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr2.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hQT]; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQT, hQo]; omega\n · rw [beqC_eq h, enc_trC hS hT hr2 hEnv hwt (Nat.le_refl T)]\n rw [congr2C (ρ' := withExt s ρ (sReq s ρ M ι noSets)) (M' := withCarried s M noSets) _ ?_ ?_]\n · exact hw\n · intro x hx\n have hxz := avoids_occ hwv hx\n rw [withExt_plain hS.q hW, sReq_plain hS.q hW]\n by_cases hv : x ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr2 hEnv hwt (Nat.le_refl T) hv, hX]; simp only [withInputs]; split <;> rfl\n · have hxi : x ∉ s.f.m.inputs := fun h => hv (hS.inv x h)\n rw [enc_σ_out hS hT hr2 hEnv hwt (Nat.le_refl T) hv, if_neg (fun h => hxi (List.contains_iff_mem.mp h)), ← hst,\n run_frame hr0 T (Nat.le_refl T) x (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.stepv p hp)) hxi (fun h => hxz (List.mem_append_right _ h))]\n exact hQr x (fun h => hxz (List.mem_append_left _ h))\n · intro m hm\n rw [cv_N (avoidsMaps_occ hwm hm), hN]\n · obtain ⟨cm, key, hcm, rfl⟩ := cycZeroOk_spec h\n rw [evalC_iff]\n simp only [holdsC, evalE, hQt, hQo]\n rw [read_mm hS.dtm (hS.carm cm hcm) (Nat.le_refl T), hN]\n simp only [withCarried, List.contains_iff_mem.mpr hcm, if_true, noSets]\n have hc0 := cyclic_sound facts nodes hc (cycMaps s ce r2 (T + 1)) Q hF n hn hH\n rw [beqC_eq hcon, enc_nowC hS hT hr2 hEnv hwt (Nat.le_refl T) s.f.m.assume hT.asz hT.asm] at hc0\n have hv : sView s ρ M ι noSets = (r2.V s T, r2.N s T) := by\n simp only [sView, decideQ_nil hS.q, CRun.V, hX, hN]\n rw [hv]; exact hc0\n\n/-- Liveness under weak fairness from its step laws and its helpful requests allowed: `sleads_wf_sound` with the step\nlaws' meaning (`SKStepHolds`) and each helpful request's being allowed in its region, with its named inputs and a\nwitness for the rest, carrying empty sets (`AllowHolds`, a cyclic proof's through `cyc_allow_sound`), in place of their\ncertificates. -/\ntheorem sleads_wf_of_holds (s : SMachine) (l : Live) (hok : sliveOk s l = true)\n (hs : SKStepHolds s (liveK l l.pend (liveStayC l))) (hc : SKStepHolds s (liveK l l.pend (liveCoverC l)))\n (hh : ∀ h ∈ l.helps, SKStepHolds s (liveK l (liveHelpWhen l h) (liveHelpThen l)))\n (ha : ∀ h ∈ l.helps, ∃ wit : List (Nat × E), distinct ((h.2 ++ wit).map (·.1)) = true\n ∧ (∀ p ∈ wit, (s.f.m.inputs.contains p.1 = true ∧ avoids s.extVars (.eq p.2 p.2) = true) ∧ avoidsMaps s.carried (.eq p.2 p.2) = true)\n ∧ AllowHolds s (.and (liveHelpWhen l h) (reqC wit))) (run : SRun s)\n (fair : ∀ h ∈ l.helps, ∀ n, (∀ m, n ≤ m → ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)\n ∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 s.f.m.assume = true) →\n ∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :\n ∀ n, evalC (run.st n).1 (run.st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 l.to = true := by\n have hok0 := hok\n simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok0\n obtain ⟨⟨⟨hd1, hd2⟩, _⟩, hreqs⟩ := hok0\n refine leads_view l run.st (fun n => withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2))\n (fun n => primed l.prime (run.st (n + 1)).1 (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)))\n (fun n => withCarried s (run.st n).2 (run.req n).2) (fun n => primedMaps l.primeMaps (run.st (n + 1)).2 (withCarried s (run.st n).2 (run.req n).2))\n (fun h n => ∀ p ∈ h.2, (run.req n).1 p.1 = evalE (run.st n).1 (run.st n).2 p.2)\n (fun h m => ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)\n ∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 s.f.m.assume = true) ?_ ?_ ?_ ?_ ?_ ?_ ?_ fair\n · intro n c hc\n obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok hc (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2\n rw [run.next n]\n exact view_C hd1 hd2 hv hm hx hn\n · intro n e he\n obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok (mem_conds_rank he) (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2\n rw [run.next n]\n exact view_E hd1 hd2 hv hm hx hn\n · intro n hB\n have := skstep_holds_one hs rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro n hB\n have := skstep_holds_one hc rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro h hmem n hB\n have := skstep_holds_one (hh h hmem) rfl rfl rfl _ _ (srun_reach run n) _ _ (run.allowed n) hB\n rw [run.next n]; exact this\n · intro h hmem n hH\n show evalC (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)) (withCarried s (run.st n).2 (run.req n).2) (reqC h.2) = true\n exact sreq_holds hok hmem (fun p hp' => hH p hp')\n · -- Enabled in its region: the request with its named inputs as its terms, the rest as the state has them, carrying\n -- empty sets.\n intro h hmem n hP hnQ hW\n obtain ⟨wit, hdist, hwit, hA⟩ := ha h hmem\n let ρ := (run.st n).1\n let M := (run.st n).2\n let ι := applyS (h.2 ++ wit) ρ M\n have hιp : ∀ p ∈ h.2, ι p.1 = evalE ρ M p.2 := fun p hp' => by\n simp only [ι, applyS, assoc_distinct hdist (List.mem_append_left _ hp')]\n have hιw : ∀ p ∈ wit, ι p.1 = evalE ρ M p.2 := fun p hp' => by\n simp only [ι, applyS, assoc_distinct hdist (List.mem_append_right _ hp')]\n have hX : ∀ c ∈ liveConds l, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) c = evalC ρ M c := fun c hc' => by\n obtain ⟨_, _, hx', hn'⟩ := slive_reads hok hc' ρ M ι noSets\n rw [congrMC _ _ M c hn']; exact congrC _ _ M c hx'\n have memF : l.from_ ∈ liveConds l := by simp [liveConds]\n have memT : l.to ∈ liveConds l := by simp [liveConds]\n have memH : h.1 ∈ liveConds l := by simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inr ⟨h, hmem, rfl⟩\n refine ⟨ι, noSets, hιp, hA ρ M (srun_reach run n) ι ?_⟩\n simp only [liveHelpWhen, Live.pend, evalC, Bool.and_eq_true, Bool.not_eq_true']\n rw [hX _ memF, hX _ memT, hX _ memH]\n exact ⟨⟨⟨hP, hnQ⟩, hW, sreq_holds hok hmem hιp⟩, sreq_list (fun p hp' => hwit p hp') hιw⟩\n\n/-! Refinements from their laws and step law -/\n\n-- A step law's certificate where none is read (SKStepHolds reads no certificate).\ndef noSProof : SProof := ([], [], [], [], [], ([], [], .leaf [] [] [] []))\n\n/-- What a refinement over sets means, from the meaning of its claims (`srefines` with them in place of their\ncertificates): with a property `P` of every reachable state of the composite (its laws, a cyclic proof's), the spec's\nprecondition holding there, and the composite's step law that the spec stutters or allows every request it is sent\n(`SKStepHolds`, a cyclic proof's through `cyc_kstep_sound`), every reachable state of the implementation is, outside the\nspec's part, a reachable state of the composite where `P` holds, whose spec part is a reachable state of the spec. -/\ntheorem srefines_of_holds (r : SRefined) (hok : srOk r = true) (hmo : distinct (r.spec.f.m.maps0.map (·.1)) = true)\n (P : (Nat → Nat) → (Nat → Nat → Nat) → Prop) (hlaws : ∀ ρ M, SReach r.full ρ M → P ρ M)\n (hpre : ∀ ρ M, SReach r.full ρ M → evalC ρ M r.spec.f.m.pre = true)\n (prime primeMaps : List (Nat × Nat)) (p : SProof) (hs : SKStepHolds r.full (srStep r prime primeMaps p))\n (hfresh : avoids (prime.map (·.2)) r.allowed = true ∧ avoidsMaps (primeMaps.map (·.2)) r.allowed = true)\n (hw : avoids r.full.wVars r.allowed = true) (hiq : r.full.f.m.inputs.all (fun i => !r.full.qVars.contains i) = true) :\n ∀ ρ M, SReach r.impl ρ M → ∃ ρc Mc σ Ms, SReach r.full ρc Mc ∧ Agree r.SV ρc ρ ∧ (∀ mp, mp ∉ r.SM → Mc mp = M mp)\n ∧ SReach r.spec σ Ms ∧ Agree r.X σ ρc ∧ (∀ mp, mp ∉ r.IM → mp ∉ r.spec.carried → Ms mp = Mc mp) ∧ P ρc Mc := by\n have hiq' : ∀ i, i ∈ r.full.f.m.inputs → i ∉ r.full.qVars := fun i hi hq => by\n simp only [List.all_eq_true, Bool.not_eq_true'] at hiq\n have := hiq i hi; rw [List.contains_iff_mem.mpr hq] at this; cases this\n have hallow : ∀ ρ M, SReach r.full ρ M → ∀ ι κ, evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 r.full.f.m.assume = true →\n evalC (sView r.full ρ M ι κ).1 (sView r.full ρ M ι κ).2 (.or r.stutter (substC r.wit r.spec.f.m.assume)) = true := by\n intro ρ M hr ι κ hA\n have h := hs ρ M hr (ι, κ) [] rfl ⟨hA, by simp [trueC, evalC, evalE], trivial⟩\n have h' : evalC (primed prime (sNext r.full ρ M ι κ).1 (withExt r.full ρ (sReq r.full ρ M ι κ)))\n (primedMaps primeMaps (sNext r.full ρ M ι κ).2 (withCarried r.full M κ)) r.allowed = true := by\n simpa [srStep, sEnd, sExts, renEnv, renMaps, lookup] using h\n rw [unprimed_evalC hfresh.1 hfresh.2] at h'\n -- The request's extended variables, read as the step reads them: the inputs, and each quantified flag decided.\n have hE : ∀ v, v ∉ r.full.wVars → withExt r.full ρ (sReq r.full ρ M ι κ) v\n = decideQ r.full (withInputs r.full.f.m.inputs ρ ι) (withCarried r.full M κ) v := by\n intro v hv\n simp only [withExt, sReq]\n by_cases hi : v ∈ r.full.f.m.inputs\n · have hext : r.full.extVars.contains v = true := List.contains_iff_mem.mpr (by simp [SMachine.extVars, hi])\n rw [decideQ_other (hiq' v hi)]\n simp only [hext, if_true, List.contains_iff_mem.mpr hi, withInputs]\n · have hin : r.full.f.m.inputs.contains v = false := by simpa [List.contains_iff_mem] using hi\n by_cases hq : v ∈ r.full.qVars\n · have hext : r.full.extVars.contains v = true := List.contains_iff_mem.mpr (by simp [SMachine.extVars, hq])\n obtain ⟨q, hq0, rfl⟩ := List.mem_map.mp hq\n cases hl : lookup (fun q' => q'.1 == q.1) r.full.qflags with\n | none => have := lookup_none_all hl q hq0; simp at this\n | some q' => simp only [hext, if_true, hin, Bool.false_eq_true, if_false, hl]\n · have hext : r.full.extVars.contains v = false := by\n simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append, not_or]\n simpa [List.contains_iff_mem] using (⟨hi, hq, hv⟩ : v ∉ r.full.f.m.inputs ∧ v ∉ r.full.qVars ∧ v ∉ r.full.wVars)\n rw [decideQ_other hq, withInputs_other hi]\n simp only [hext, Bool.false_eq_true, if_false]\n rw [congrC _ _ _ r.allowed (fun i hi => hE i (avoids_occ hw hi))] at h'\n simp only [SRefined.allowed] at h'\n rw [inline_evalC] at h'\n exact h'\n intro ρ M hr\n obtain ⟨ρc, Mc, hrc, hag, hmag⟩ := srefine_lift r hok hmo hr\n obtain ⟨σ, Ms, hrs, hags, hmags⟩ := srefine_project r hok hpre hallow hrc\n exact ⟨ρc, Mc, σ, Ms, hrc, hag, hmag, hrs, hags, hmags, hlaws ρc Mc hrc⟩\n\n/-! Flags that hold at every key -/\n\n-- The machine with its quantified flags and their witnesses as inputs a request names: no flag holds at every key in\n-- it. A request of the machine is one of its, the flags decided and the witnesses chosen as a goal reads them (`sReq`).\ndef SMachine.sk (s : SMachine) : SMachine :=\n ⟨⟨⟨s.extVars, s.f.m.init, s.f.m.maps0, s.f.m.step, s.f.m.writes, s.f.m.pre, s.f.m.assume⟩, s.f.flags⟩, s.carried, s.every, [], [], s.facts, s.pfacts⟩\n\nsection sk\nvariable {s : SMachine}\n\ntheorem sk_q : s.sk.qflags = [] := rfl\ntheorem sk_inputs : s.sk.f.m.inputs = s.extVars := rfl\n\n-- sReq reads the request's inputs alone (and the state where the flags' bodies read it, off the witnesses).\ntheorem sReq_inputs {ρ : Nat → Nat} {M : Nat → Nat → Nat} {ι ι' : Nat → Nat} {κ : Nat → Nat → Nat}\n (h : ∀ v ∈ s.f.m.inputs, ι' v = ι v) : sReq s ρ M ι' κ = sReq s ρ M ι κ := by\n have hw : withInputs s.f.m.inputs ρ ι' = withInputs s.f.m.inputs ρ ι := by\n funext v; simp only [withInputs]; split\n · rename_i hv; exact h v (List.contains_iff_mem.mp hv)\n · rfl\n funext v; simp only [sReq, hw]; split\n · rename_i hv; exact h v (List.contains_iff_mem.mp hv)\n · rfl\n\ntheorem sReq_input {ρ : Nat → Nat} {M : Nat → Nat → Nat} {ι : Nat → Nat} {κ : Nat → Nat → Nat} {v : Nat} (hv : v ∈ s.f.m.inputs) :\n sReq s ρ M ι κ v = ι v := by simp only [sReq]; rw [if_pos (List.contains_iff_mem.mpr hv)]\n\ntheorem sReq_idem (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n sReq s ρ M (sReq s ρ M ι κ) κ = sReq s ρ M ι κ := sReq_inputs (fun _ hv => sReq_input hv)\n\n-- The flags' bodies read no witness, so states that agree off the witnesses decide them alike.\ntheorem decideQ_agree (hs : sOk s = true) {X X' : Nat → Nat} (hag : Agree s.wVars X' X) (N : Nat → Nat → Nat) :\n Agree s.wVars (decideQ s X' N) (decideQ s X N) := by\n obtain ⟨_, _, _, _, _, hbody, _⟩ := sOk_parts s hs\n intro v hv\n simp only [decideQ]\n cases hl : lookup (fun q => q.1 == v) s.qflags with\n | none => exact hag v hv\n | some q =>\n have hq := (lookup_some hl).1\n have hb : ∀ k, holdsC (atKeys X' q.2.1 k) N q.2.2 = holdsC (atKeys X q.2.1 k) N q.2.2 := fun k => propext (by\n rw [← evalC_iff, ← evalC_iff, congrC _ (atKeys X q.2.1 k) N q.2.2 (fun j hj => by\n simp only [atKeys]; split\n · rfl\n · exact hag j (fun h => avoids_occ (hbody q hq).1 hj (List.mem_append_left _ (List.mem_append_right _ h))))])\n have hiff : (∀ k, holdsC (atKeys X' q.2.1 k) N q.2.2) ↔ (∀ k, holdsC (atKeys X q.2.1 k) N q.2.2) :=\n forall_congr' (fun k => by rw [hb k])\n dsimp only\n by_cases h1 : ∀ k, holdsC (atKeys X q.2.1 k) N q.2.2\n · rw [if_pos (hiff.mpr h1), if_pos h1]\n · rw [if_neg (fun h => h1 (hiff.mp h)), if_neg h1]\n\ntheorem choose_congr {α : Type} {p q : α → Prop} (h : p = q) (hp : ∃ x, p x) (hq : ∃ x, q x) : Classical.choose hp = Classical.choose hq := by\n subst h; rfl\n\ntheorem witnessKeys_agree (hs : sOk s = true) {X X' : Nat → Nat} (hag : Agree s.wVars X' X) (N : Nat → Nat → Nat) {q : Nat × List Nat × C}\n (hq : q ∈ s.qflags) : witnessKeys X' N q = witnessKeys X N q := by\n obtain ⟨_, _, _, _, _, hbody, _⟩ := sOk_parts s hs\n have hb : ∀ k, holdsC (atKeys X' q.2.1 k) N q.2.2 = holdsC (atKeys X q.2.1 k) N q.2.2 := fun k => propext (by\n rw [← evalC_iff, ← evalC_iff, congrC _ (atKeys X q.2.1 k) N q.2.2 (fun j hj => by\n simp only [atKeys]; split\n · rfl\n · exact hag j (fun h => avoids_occ (hbody q hq).1 hj (List.mem_append_left _ (List.mem_append_right _ h))))])\n have hP : (fun k => holdsC (atKeys X' q.2.1 k) N q.2.2) = (fun k => holdsC (atKeys X q.2.1 k) N q.2.2) := funext hb\n have hp2 : (fun k => ¬holdsC (atKeys X' q.2.1 k) N q.2.2) = (fun k => ¬holdsC (atKeys X q.2.1 k) N q.2.2) := funext fun k => by rw [hb k]\n unfold witnessKeys\n split\n · rename_i h1\n have h2 : ∃ k, ¬holdsC (atKeys X q.2.1 k) N q.2.2 := by obtain ⟨k, hk⟩ := h1; exact ⟨k, by rwa [← hb k]⟩\n rw [dif_pos h2]\n exact choose_congr hp2 h1 h2\n · rename_i h1\n have h2 : ¬∃ k, ¬holdsC (atKeys X q.2.1 k) N q.2.2 := fun ⟨k, hk⟩ => h1 ⟨k, by rwa [hb k]⟩\n rw [dif_neg h2]\n\ntheorem sReq_agree (hs : sOk s = true) {ρ ρ' : Nat → Nat} (hag : Agree s.wVars ρ' ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat)\n (κ : Nat → Nat → Nat) : sReq s ρ' M ι κ = sReq s ρ M ι κ := by\n have hX : Agree s.wVars (withInputs s.f.m.inputs ρ' ι) (withInputs s.f.m.inputs ρ ι) := fun v hv => by\n simp only [withInputs]; split\n · rfl\n · exact hag v hv\n funext v\n simp only [sReq]\n split\n · rfl\n · split\n · rename_i q hq\n exact decideQ_agree hs hX _ v (fun h => (ext_apart s hs).2 v (by\n have := (lookup_some hq); simp only [beq_iff_eq] at this; rw [← this.2]; exact List.mem_map_of_mem this.1) h)\n · split\n · split\n · rename_i q hq\n split\n · rw [witnessKeys_agree hs hX _ (lookup_some hq).1]\n · rfl\n · rfl\n · rfl\n\n-- A machine's request read by its sk machine: the view is the goal's view, which agrees with the machine's off the\n-- witnesses.\ntheorem sk_inputsView (ρ ρ' : Nat → Nat) (hag : Agree s.wVars ρ' ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n withInputs s.sk.f.m.inputs ρ' (sReq s ρ M ι κ) = withExt s ρ (sReq s ρ M ι κ) := by\n funext v\n simp only [withInputs, withExt, sk_inputs]\n by_cases hv : s.extVars.contains v = true\n · simp only [hv, ↓reduceIte]\n · simp only [hv, Bool.false_eq_true, ↓reduceIte]\n exact hag v (fun h => hv (by simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append]; exact Or.inr h))\n\ntheorem sk_view (hs : sOk s = true) {ρ ρ' : Nat → Nat} (hag : Agree s.wVars ρ' ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n Agree s.wVars (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 (sView s ρ M ι κ).1 ∧ (sView s.sk ρ' M (sReq s ρ M ι κ) κ).2 = (sView s ρ M ι κ).2 := by\n obtain ⟨hfl, _, _, _, _, _, hflw⟩ := sOk_parts s hs\n refine ⟨fun i hi => ?_, rfl⟩\n simp only [sView]\n rw [decideQ_nil sk_q, sk_inputsView ρ ρ' hag]\n exact decide_view (Z := s.wVars) (TM := []) hfl hflw (fun _ _ => rfl) (fun j hj _ => (view_agree s ρ M ι κ hs j hj).symm)\n (fun _ _ => rfl) hi\n\n-- Inlining the flags keeps every variable a term reads but the flags'.\nmutual\ntheorem occE_inline (fl : List (Nat × C)) {i : Nat} (hi : flagOf fl i = none) : ∀ e : E, occE i e = true → occE i (inlineE fl e) = true\n | .var j, h => by\n have hj : j = i := by simpa [occE] using h\n subst hj; simp [inlineE, hi, occE]\n | .const _, h => by simp [occE] at h\n | .add a b, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .sub a b, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .mul _ a, h => by simp only [occE] at h; simp only [inlineE, occE]; exact occE_inline fl hi a h\n | .min a b, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .max a b, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .prod a b, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .ite c t f, h => by\n simp only [occE, Bool.or_eq_true] at h; simp only [inlineE, occE, Bool.or_eq_true]\n rcases h with (h | h) | h\n · exact Or.inl (Or.inl (occC_inline fl hi c h))\n · exact Or.inl (Or.inr (occE_inline fl hi t h))\n · exact Or.inr (occE_inline fl hi f h)\n | .get _ key, h => by simp only [occE] at h; simp only [inlineE, occE]; exact occE_inline fl hi key h\ntheorem occC_inline (fl : List (Nat × C)) {i : Nat} (hi : flagOf fl i = none) : ∀ c : C, occC i c = true → occC i (inlineC fl c) = true\n | .le a b, h => by\n simp only [occC, Bool.or_eq_true] at h; simp only [inlineC, occC, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .lt a b, h => by\n simp only [occC, Bool.or_eq_true] at h; simp only [inlineC, occC, Bool.or_eq_true]\n exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n | .eq a b, h => by\n simp only [occC, Bool.or_eq_true] at h\n have hd : occC i (.eq (inlineE fl a) (inlineE fl b)) = true := by\n simp only [occC, Bool.or_eq_true]; exact h.imp (occE_inline fl hi a) (occE_inline fl hi b)\n simp only [inlineC, eqOrFlag]\n split\n · split\n · rename_i c hc\n exfalso\n rcases h with h | h\n · simp only [occE, beq_iff_eq] at h; subst h; rw [hi] at hc; cases hc\n · simp [occE] at h\n · exact hd\n · exact hd\n | .and a b, h => by\n simp only [occC, Bool.or_eq_true] at h; simp only [inlineC, occC, Bool.or_eq_true]\n exact h.imp (occC_inline fl hi a) (occC_inline fl hi b)\n | .or a b, h => by\n simp only [occC, Bool.or_eq_true] at h; simp only [inlineC, occC, Bool.or_eq_true]\n exact h.imp (occC_inline fl hi a) (occC_inline fl hi b)\n | .not a, h => by simp only [occC] at h; simp only [inlineC, occC]; exact occC_inline fl hi a h\nend\n\ntheorem avoidsE_of_inline {fl : List (Nat × C)} {Z : List Nat} (hz : ∀ i ∈ Z, i ∉ flagVars fl) {e : E}\n (h : avoidsE Z (inlineE fl e) = true) : avoidsE Z e = true := by\n simp only [avoidsE, List.all_eq_true, Bool.not_eq_true'] at h ⊢\n intro i hi\n cases ho : occE i e\n · rfl\n · have hn : flagOf fl i = none := by\n simp only [flagOf]\n rw [lookup_none (fun p hp => by\n simp only [beq_eq_false_iff_ne]; intro he; exact hz _ hi (by rw [← he]; exact List.mem_map_of_mem hp))]\n rfl\n have := h i hi; rw [occE_inline fl hn e ho] at this; cases this\n\ntheorem sOk_terms (hs : sOk s = true) : (∀ p ∈ s.f.m.step, avoidsE s.wVars (inlineE s.f.flags p.2) = true)\n ∧ (∀ w ∈ s.f.m.writes, avoidsE s.wVars (inlineE s.f.flags w.2.1) = true ∧ avoidsE s.wVars (inlineE s.f.flags w.2.2) = true)\n ∧ (∀ w ∈ s.every, avoidsE s.wVars (inlineE s.f.flags w.2.2) = true) ∧ avoids s.wVars s.f.m.assume = true\n ∧ avoids (s.wVars ++ s.qVars) s.f.m.pre = true := by\n simp only [sOk, Bool.and_eq_true, List.all_eq_true] at hs\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨_, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩, hpre⟩, hst⟩, hwr⟩, hev⟩, has⟩ := hs\n exact ⟨hst, fun w hw => hwr w hw, fun w hw => (hev w hw).1.1.1, has, hpre⟩\n\n-- The witnesses are no flags.\ntheorem wVars_flags (hs : sOk s = true) : ∀ i ∈ s.wVars, i ∉ flagVars s.f.flags := by\n obtain ⟨_, _, _, _, hd, _, _⟩ := sOk_parts s hs\n intro i hi hf\n exact (distinct_append hd).2.2 i (by simp only [SMachine.extVars, List.mem_append]; exact Or.inr hi) hf\n\ntheorem sk_view_eq (ρ ρ' : Nat → Nat) (hag : Agree s.wVars ρ' ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n sView s.sk ρ' M (sReq s ρ M ι κ) κ = (decideFlags s.f.flags (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ), withCarried s M κ) := by\n simp only [sView]; rw [decideQ_nil sk_q, sk_inputsView ρ ρ' hag]; rfl\n\n-- A step of the machine is a step of its sk machine, its request decided: allowed alike, the state after it agreeing\n-- off the witnesses, the maps the same.\ntheorem sk_step (hs : sOk s = true) {ρ ρ' : Nat → Nat} (hag : Agree s.wVars ρ' ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :\n evalC (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 (sView s.sk ρ' M (sReq s ρ M ι κ) κ).2 s.f.m.assume\n = evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume\n ∧ Agree s.wVars (sNext s.sk ρ' M (sReq s ρ M ι κ) κ).1 (sNext s ρ M ι κ).1\n ∧ (sNext s.sk ρ' M (sReq s ρ M ι κ) κ).2 = (sNext s ρ M ι κ).2 := by\n obtain ⟨hV, hN⟩ := sk_view hs hag M ι κ\n obtain ⟨hst, hwr, hev, has, _⟩ := sOk_terms hs\n have hz := wVars_flags hs\n have hraw : ∀ e : E, avoidsE s.wVars (inlineE s.f.flags e) = true → ∀ j, occE j e = true → j ∉ s.wVars :=\n fun e he j hj => avoidsE_occ (avoidsE_of_inline hz he) hj\n have hEq : ∀ e : E, avoidsE s.wVars (inlineE s.f.flags e) = true → ∀ (A B : Nat → Nat), Agree s.wVars A B → ∀ N : Nat → Nat → Nat,\n evalE A N e = evalE B N e := fun e he A B hab N => congrE A B N e (fun j hj => hab j (hraw e he j hj))\n refine ⟨?_, fun i hi => ?_, ?_⟩\n · rw [hN, congrC _ (sView s ρ M ι κ).1 _ s.f.m.assume (fun j hj => hV j (avoids_occ has hj))]\n · simp only [sNext, applyS]\n rw [show s.sk.f.m.step = s.f.m.step from rfl]\n cases ha : assoc s.f.m.step i with\n | none => show (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 i = (sView s ρ M ι κ).1 i; exact hV i hi\n | some e =>\n show evalE (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 (sView s.sk ρ' M (sReq s ρ M ι κ) κ).2 e = evalE (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 e\n obtain ⟨p, hp, rfl⟩ : ∃ p ∈ s.f.m.step, p.2 = e := by\n simp only [assoc, Option.map_eq_some_iff] at ha; obtain ⟨p, hl, rfl⟩ := ha; exact ⟨p, (lookup_some hl).1, rfl⟩\n rw [hN]; exact hEq p.2 (hst p hp) _ _ hV _\n · simp only [sNext]\n funext mp key\n show everyMaps s (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 (sView s.sk ρ' M (sReq s ρ M ι κ) κ).2 mp key = everyMaps s (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 mp key\n rw [hN]\n simp only [everyMaps]\n cases hl : lookup (fun w => w.1 == mp) s.every with\n | some w =>\n have hu : Agree s.wVars (upd (sView s.sk ρ' M (sReq s ρ M ι κ) κ).1 w.2.1 key) (upd (sView s ρ M ι κ).1 w.2.1 key) := by\n intro j hj; simp only [upd]; split\n · rfl\n · exact hV j hj\n exact hEq w.2.2 (hev w (lookup_some hl).1) _ _ hu _\n | none =>\n simp only [writeMaps]\n cases hl2 : lookup (fun w => w.1 == mp) s.f.m.writes with\n | none => rfl\n | some w =>\n obtain ⟨h1, h2⟩ := hwr w (lookup_some hl2).1\n simp only [hEq w.2.1 h1 _ _ hV, hEq w.2.2 h2 _ _ hV]\n\n-- A run's request at time u decided: its flags and witnesses as a goal reads them from its inputs.\ndef CRun.Decided (s : SMachine) (r : CRun) (u : Nat) : Prop := r.ι u = sReq s (r.st u) (r.mp u) (r.ι u) (r.κ u)\n\n-- The run with the request at T (the last, which no step takes) replaced.\ndef CRun.last (r : CRun) (T : Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : CRun :=\n ⟨r.ρ0, r.st, r.mp, fun u => if u = T then ι else r.ι u, fun u => if u = T then κ else r.κ u⟩\n\ntheorem last_ok {s' : SMachine} {r : CRun} {T : Nat} (hr : r.Ok s' T) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (r.last T ι κ).Ok s' T := by\n have hNX : ∀ u, u < T → (r.last T ι κ).N s' u = r.N s' u ∧ (r.last T ι κ).V s' u = r.V s' u := fun u hu => by\n have hne : u ≠ T := Nat.ne_of_lt hu\n refine ⟨?_, ?_⟩ <;> simp only [CRun.V, CRun.X, CRun.N, CRun.last, if_neg hne]\n exact ⟨hr.maps0, hr.pre, hr.facts, hr.pfacts, hr.init, fun u hu => by rw [(hNX u hu).1, (hNX u hu).2]; exact hr.step u hu⟩\n\n-- The request at T decided from no inputs: a decided last request.\ntheorem last_decided {r : CRun} {T : Nat} : (r.last T (sReq s (r.st T) (r.mp T) (fun _ => 0) noSets) noSets).Decided s T := by\n simp only [CRun.Decided, CRun.last, if_pos rfl]; exact (sReq_idem _ _ _ _).symm\n\ntheorem last_other {r : CRun} {T u : Nat} (hu : u ≠ T) (ι : Nat → Nat) (κ : Nat → Nat → Nat) (h : r.Decided s u) : (r.last T ι κ).Decided s u := by\n simp only [CRun.Decided, CRun.last, if_neg hu]; exact h\n\n-- A reachable state of the machine ends a run of its sk machine, every request of it decided, off the witnesses.\ntheorem sk_run (hs : sOk s = true) : ∀ {ρ M}, SReach s ρ M → ∃ (r : CRun) (T : Nat), r.Ok s.sk T ∧ (∀ u, u ≤ T → r.Decided s u)\n ∧ Agree s.wVars (r.st T) ρ ∧ r.mp T = M := by\n intro ρ M h\n induction h with\n | start ρ M hM hp hf hpf =>\n exact ⟨⟨ρ, fun _ => applyS s.f.m.init ρ M, fun _ => M, fun _ => sReq s (applyS s.f.m.init ρ M) M (fun _ => 0) noSets, fun _ => noSets⟩, 0,\n ⟨hM, hp, hf, hpf, rfl, fun u hu => absurd hu (Nat.not_lt_zero u)⟩, fun _ _ => (sReq_idem _ _ _ _).symm, fun _ _ => rfl, rfl⟩\n | step ρ M ι κ _ ha ih =>\n obtain ⟨r, T, hr, hd, hag, hmp⟩ := ih\n obtain ⟨hA, hS, hMn⟩ := sk_step hs hag M ι κ\n have hr1 := run_snoc_ok sk_q hr rfl hmp (sReq s ρ M ι κ) κ (by show evalC _ _ s.f.m.assume = true; rw [hA]; exact ha)\n let r1 := r.snoc T (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).1 (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).2 (sReq s ρ M ι κ) κ\n obtain ⟨l1, l2, l3, l4, l5, l6⟩ := snoc_last (r := r) (T := T) (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).1 (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).2 (sReq s ρ M ι κ) κ\n refine ⟨r1.last (T + 1) (sReq s (r1.st (T + 1)) (r1.mp (T + 1)) (fun _ => 0) noSets) noSets, T + 1, last_ok hr1 _ _, fun u hu => ?_, ?_, ?_⟩\n · rcases Nat.lt_or_ge u T with hlt | hge\n · refine last_other (Nat.ne_of_lt (by omega)) _ _ ?_\n obtain ⟨e1, _, e3, e4⟩ := snoc_early (s := s.sk) (r := r) hlt (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).1 (sNext s.sk (r.st T) M (sReq s ρ M ι κ) κ).2 (sReq s ρ M ι κ) κ\n have h := hd u (Nat.le_of_lt hlt)\n simp only [CRun.Decided, r1, CRun.snoc, if_pos hlt, if_pos (Nat.le_of_lt hlt)] at h ⊢\n exact h\n · rcases Nat.lt_or_ge u (T + 1) with hlt | hge'\n · have hu : u = T := by omega\n subst hu\n refine last_other (by omega) _ _ ?_\n simp only [CRun.Decided, r1, l3, l4, l5, l6, hmp]\n rw [sReq_agree hs hag, sReq_idem]\n · have hu : u = T + 1 := by omega\n subst hu; exact last_decided\n · show Agree s.wVars (r1.st (T + 1)) (sNext s ρ M ι κ).1\n rw [l1]; exact hS\n · show r1.mp (T + 1) = (sNext s ρ M ι κ).2\n rw [l2]; exact hMn\n\n-- A guard: where u ≤ T.\ndef cycUpTo (ce : CycEnc) (c : C) : C := .or (.not (.le (.var ce.u) (.var ce.T))) (trC ce (.var ce.u) c)\n\n-- The facts the encoding adds for each flag that holds at every key, at each time u ≤ T (Sets.lean's closingHyps read\n-- at u): the flag is 0 or 1; it is 0 or its body holds at every value of its keys; it is 1 or its body fails at the\n-- witness.\ndef cycQFacts (s : SMachine) (ce : CycEnc) : List Fact :=\n s.qflags.flatMap fun q =>\n [⟨[ce.u], cycUpTo ce (.le (.var q.1) (.const 1))⟩, ⟨ce.u :: q.2.1, cycUpTo ce (.or (.eq (.var q.1) (.const 0)) q.2.2)⟩]\n ++ (match lookup (fun w => w.1 == q.1) s.wits with\n | some w => [⟨[ce.u], cycUpTo ce (.or (.eq (.var q.1) (.const 1)) (.not (substC (q.2.1.zip (w.2.map .var)) q.2.2)))⟩]\n | none => [])\n\ntheorem assoc_zip_none : ∀ (ks : List Nat) (ts : List E), ks.length ≤ ts.length → ∀ {j : Nat}, assoc (ks.zip ts) j = none → j ∉ ks\n | [], _, _, _, _ => by simp\n | _ :: _, [], h, _, _ => by simp at h\n | k :: ks, t :: ts, h, j, ha => by\n simp only [List.zip_cons_cons, assoc, lookup] at ha\n by_cases hk : k = j\n · subst hk; simp at ha\n · simp only [hk, beq_iff_eq, if_false] at ha\n intro hm\n rcases List.mem_cons.mp hm with hm | hm\n · exact hk hm.symm\n · exact assoc_zip_none ks ts (by simpa using h) (by simpa [assoc] using ha) hm\n\ntheorem assoc_zip_self (g : Nat → E) : ∀ (ks : List Nat) {j : Nat}, j ∈ ks → assoc (ks.zip (ks.map g)) j = some (g j)\n | [], _, h => by simp at h\n | k :: ks, j, h => by\n simp only [List.map_cons, List.zip_cons_cons, assoc, lookup]\n by_cases hk : k = j\n · subst hk; simp\n · have hj : j ∈ ks := by rcases List.mem_cons.mp h with h | h; exact absurd h.symm hk; exact h\n have := assoc_zip_self g ks hj\n simp only [assoc] at this\n simp [hk, this]\n\n-- On a decided run, the encoding at a time up to T reads the goal's view of that step (Sets.lean's withExt, sReq).\ntheorem sk_X {r : CRun} {u : Nat} (hd : r.Decided s u) :\n r.X s.sk u = withExt s (r.st u) (sReq s (r.st u) (r.mp u) (r.ι u) (r.κ u)) := by\n funext v\n simp only [CRun.X, withInputs, withExt, sk_inputs]\n by_cases hv : s.extVars.contains v = true\n · simp only [hv, ↓reduceIte]; exact congrFun hd v\n · simp only [hv, Bool.false_eq_true, ↓reduceIte]\n\n-- A quantified flag's body reads no added variable, no flag, no map over time, and of the keys none but its own.\ndef cycBodyOk (s : SMachine) (ce : CycEnc) (keys : List Nat) : Bool :=\n s.qflags.all fun q => avoids ce.special q.2.2 && avoidsMaps ce.tm q.2.2 && keys.all (fun k => q.2.1.contains k || !occC k q.2.2)\n && q.2.1.all (fun k => keys.contains k)\n\nsection qfacts\nvariable {ce : CycEnc} {keys : List Nat} (hs : sOk s = true) (hS : CycShape s.sk ce keys) (hT : CycTerms s.sk ce keys)\n {r : CRun} {T : Nat} (hr : r.Ok s.sk T) (hd : ∀ u, u ≤ T → r.Decided s u) (hb : cycBodyOk s ce keys = true)\n {Q : Nat → Nat} (hQ : EnvOk s.sk ce keys r T Q)\ninclude hs hS hT hr hd hb hQ\n\n-- A fact over u and some keys, guarded by u ≤ T: true when its condition is, read at the run's step at u.\ntheorem upto_true {ks : List Nat} (hks : ∀ x ∈ ks, x = ce.u ∨ x ∈ keys) (hu : ce.u ∈ ks) (vals : Nat → Nat) (c : C)\n (h : vals ce.u ≤ T → evalC (applyS (cycσ ce (.var ce.u)) (override ks vals Q) (cycMaps s.sk ce r (T + 1))) (cycView s.sk ce r (T + 1) (vals ce.u)) c = true) :\n evalC (override ks vals Q) (cycMaps s.sk ce r (T + 1)) (cycUpTo ce c) = true := by\n have hsp := special_parts hS.dsp\n have hnk : ∀ i, (i = ce.T ∨ i = ce.om) → i ∉ ks := fun i hi hm => by\n rcases hks i hm with h1 | h1\n · rcases hi with rfl | rfl\n · exact hsp.1 h1.symm\n · exact hsp.2.1 h1.symm\n · exact hS.ks i h1 (in_special (by rcases hi with rfl | rfl; exact Or.inr (Or.inr (Or.inl rfl)); exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))))\n have hQ1 : EnvOk s.sk ce keys r T (override ks vals Q) := by\n refine ⟨?_, ?_, fun i hi => ?_⟩\n · simp only [override]; rw [if_neg (fun h => hnk ce.T (Or.inl rfl) (List.contains_iff_mem.mp h))]; exact hQ.1\n · simp only [override]; rw [if_neg (fun h => hnk ce.om (Or.inr rfl) (List.contains_iff_mem.mp h))]; exact hQ.2.1\n · simp only [override]\n rw [if_neg (fun h => by\n rcases hks i (List.contains_iff_mem.mp h) with h1 | h1\n · exact (notP hi).1 (in_special (Or.inl h1))\n · exact (notP hi).2.1 h1)]\n exact hQ.2.2 i hi\n have hw : evalE (override ks vals Q) (cycMaps s.sk ce r (T + 1)) (.var ce.u) = vals ce.u := by\n simp only [evalE, override, List.contains_iff_mem.mpr hu, ite_true]\n simp only [cycUpTo, evalC, Bool.or_eq_true, Bool.not_eq_true']\n by_cases hle : vals ce.u ≤ T\n · right; rw [enc_trC hS hT hr hQ1 hw hle]; exact h hle\n · left; simp only [evalE] at hw; simp only [decide_eq_false_iff_not, evalE, hw, hQ1.1]; exact hle\n\ntheorem qbody_parts {q : Nat × List Nat × C} (hq : q ∈ s.qflags) :\n avoids ce.special q.2.2 = true ∧ avoidsMaps ce.tm q.2.2 = true ∧ (∀ k ∈ keys, q.2.1.contains k = true ∨ occC k q.2.2 = false)\n ∧ (∀ k ∈ q.2.1, k ∈ keys) := by\n have h := List.all_eq_true.mp hb q hq\n simp only [Bool.and_eq_true, List.all_eq_true, Bool.or_eq_true, Bool.not_eq_true'] at h\n exact ⟨h.1.1.1, h.1.1.2, h.1.2, fun k hk => List.contains_iff_mem.mp (h.2 k hk)⟩\n\n-- At a time u0 ≤ T, a variable no added name or flag is reads the goal's view of the run's step there.\ntheorem upto_view {ks : List Nat} (hks : ∀ x ∈ ks, x = ce.u ∨ x ∈ keys) (vals : Nat → Nat) (hu0 : vals ce.u ≤ T) (hu : ce.u ∈ ks)\n {j : Nat} (hz : j ∉ cycZ ce keys) (hf : j ∉ flagVars s.f.flags) :\n applyS (cycσ ce (.var ce.u)) (override ks vals Q) (cycMaps s.sk ce r (T + 1)) j\n = withExt s (r.st (vals ce.u)) (sReq s (r.st (vals ce.u)) (r.mp (vals ce.u)) (r.ι (vals ce.u)) (r.κ (vals ce.u))) j := by\n have hsp := special_parts hS.dsp\n have hQ1 : EnvOk s.sk ce keys r T (override ks vals Q) := by\n have hnk : ∀ i, (i = ce.T ∨ i = ce.om) → i ∉ ks := fun i hi hm => by\n rcases hks i hm with h1 | h1\n · rcases hi with rfl | rfl\n · exact hsp.1 h1.symm\n · exact hsp.2.1 h1.symm\n · exact hS.ks i h1 (in_special (by rcases hi with rfl | rfl; exact Or.inr (Or.inr (Or.inl rfl)); exact Or.inr (Or.inr (Or.inr (Or.inl rfl)))))\n refine ⟨?_, ?_, fun i hi => ?_⟩\n · simp only [override]; rw [if_neg (fun h => hnk ce.T (Or.inl rfl) (List.contains_iff_mem.mp h))]; exact hQ.1\n · simp only [override]; rw [if_neg (fun h => hnk ce.om (Or.inr rfl) (List.contains_iff_mem.mp h))]; exact hQ.2.1\n · simp only [override]\n rw [if_neg (fun h => by\n rcases hks i (List.contains_iff_mem.mp h) with h1 | h1\n · exact (notP hi).1 (in_special (Or.inl h1))\n · exact (notP hi).2.1 h1)]\n exact hQ.2.2 i hi\n have hw : evalE (override ks vals Q) (cycMaps s.sk ce r (T + 1)) (.var ce.u) = vals ce.u := by\n simp only [evalE, override, List.contains_iff_mem.mpr hu, ite_true]\n rw [enc_Y hS hT hr hQ1 hw hu0 hz hf, sk_X (hd _ hu0)]\n\n-- A variable of the goal's view: a quantified flag or a witness is traced, no added name, no flag.\ntheorem ext_free {v : Nat} (hv : v ∈ s.extVars) : v ∉ cycZ ce keys ∧ v ∉ flagVars s.f.flags := by\n obtain ⟨_, _, _, _, hd', _, _⟩ := sOk_parts s hs\n exact ⟨fun h => hS.zv v h (hS.inv v (by rw [sk_inputs]; exact hv)), (distinct_append hd').2.2 v hv⟩\n\ntheorem qflag_ext {q : Nat × List Nat × C} (hq : q ∈ s.qflags) : q.1 ∈ s.extVars := by\n simp only [SMachine.extVars, SMachine.qVars, List.mem_append, List.mem_map]; exact Or.inl (Or.inr ⟨q, hq, rfl⟩)\n\n-- A body variable, not one of the body's keys: no added name, no flag.\ntheorem body_free {q : Nat × List Nat × C} (hq : q ∈ s.qflags) {j : Nat} (hj : occC j q.2.2 = true) (hk : j ∉ q.2.1) :\n j ∉ cycZ ce keys ∧ j ∉ flagVars s.f.flags := by\n obtain ⟨hsb, _, hkb, _⟩ := qbody_parts hs hS hT hr hd hb hQ hq\n obtain ⟨_, _, _, _, _, hbody, _⟩ := sOk_parts s hs\n refine ⟨fun h => ?_, fun h => avoids_occ (hbody q hq).1 hj (List.mem_append_right _ h)⟩\n simp only [cycZ, List.mem_append] at h\n rcases h with h | h\n · exact avoids_occ hsb hj h\n · rcases hkb j h with h1 | h1\n · exact hk (List.contains_iff_mem.mp h1)\n · rw [hj] at h1; cases h1\n\ntheorem qfacts_hold {f : Fact} (hf : f ∈ cycQFacts s ce) (vals : Nat → Nat) :\n evalC (override f.keys vals Q) (cycMaps s.sk ce r (T + 1)) f.holds = true := by\n simp only [cycQFacts, List.mem_flatMap, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hf\n obtain ⟨q, hq, hf⟩ := hf\n obtain ⟨_, hmb, _, hbk⟩ := qbody_parts hs hS hT hr hd hb hQ hq\n have hqv := ext_free hs hS hT hr hd hb hQ (qflag_ext hs hS hT hr hd hb hQ hq)\n -- The maps a body reads, at u0: the step's.\n have hN : ∀ u0 m, m ∉ ce.tm → cycView s.sk ce r (T + 1) u0 m = withCarried s (r.mp u0) (r.κ u0) m := fun u0 m hm => by\n rw [cv_N hm]; rfl\n rcases hf with (rfl | rfl) | hf\n · -- 0 or 1\n refine upto_true hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) (by simp) vals _ (fun hu0 => ?_)\n have hc := closing_true s (r.st (vals ce.u)) (r.mp (vals ce.u)) (r.ι (vals ce.u)) (r.κ (vals ce.u)) hs [] (.le (.var q.1) (.const 1)) (by\n simp only [closingHyps, List.mem_flatMap]; exact ⟨q, hq, by simp⟩)\n rw [evalC_iff] at hc ⊢\n simp only [holdsC, evalE] at hc ⊢\n rw [upto_view hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) vals hu0 (by simp) hqv.1 hqv.2]; exact hc\n · -- 0, or the body at every value of the keys\n have hks : ∀ x ∈ ce.u :: q.2.1, x = ce.u ∨ x ∈ keys := fun x hx => by\n rcases List.mem_cons.mp hx with h | h; exact Or.inl h; exact Or.inr (hbk x h)\n refine upto_true hs hS hT hr hd hb hQ hks List.mem_cons_self vals _ (fun hu0 => ?_)\n let u0 := vals ce.u\n let consts := q.2.1.map (fun k => E.const (vals k))\n have hc := closing_true s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0) hs [(q.1, consts)] (.or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip consts) q.2.2)) (by\n simp only [closingHyps, List.mem_flatMap]; exact ⟨q, hq, by simp⟩)\n rw [evalC_iff] at hc ⊢\n simp only [holdsC, evalE] at hc ⊢\n rw [upto_view hs hS hT hr hd hb hQ hks vals hu0 List.mem_cons_self hqv.1 hqv.2]\n rcases hc with hc | hc\n · exact Or.inl hc\n · right\n rw [← evalC_iff] at hc ⊢\n rw [subst_evalC] at hc\n rw [congr2C (ρ' := applyS (q.2.1.zip consts) (withExt s (r.st u0) (sReq s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0))) (withCarried s (r.mp u0) (r.κ u0)))\n (M' := withCarried s (r.mp u0) (r.κ u0)) q.2.2 (fun j hj => ?_) (fun m hm => hN u0 m (avoidsMaps_occ hmb hm))]\n · exact hc\n · by_cases hjk : j ∈ q.2.1\n · have hr1 : applyS (q.2.1.zip consts) (withExt s (r.st u0) (sReq s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0))) (withCarried s (r.mp u0) (r.κ u0)) j = vals j := by\n simp only [applyS, consts, assoc_zip_self (fun k => E.const (vals k)) q.2.1 hjk, evalE]\n have hz : (cycσ ce (.var ce.u)).map (·.1) = ce.vtr.map (·.1) := by simp [cycσ]\n rw [hr1, applyS_other (by rw [hz]; exact fun h => hS.zv j (by simp only [cycZ, List.mem_append]; exact Or.inr (hbk j hjk)) h)]\n simp only [override, List.contains_iff_mem.mpr (List.mem_cons_of_mem _ hjk), ite_true]\n · obtain ⟨hz, hfl⟩ := body_free hs hS hT hr hd hb hQ hq hj hjk\n rw [upto_view hs hS hT hr hd hb hQ hks vals hu0 List.mem_cons_self hz hfl, applyS_other (fun h => hjk (by\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact (List.of_mem_zip hp).1))]\n · -- 1, or the body failing at the witness\n cases hl : lookup (fun w => w.1 == q.1) s.wits with\n | none => rw [hl] at hf; simp at hf\n | some w =>\n rw [hl] at hf; simp only [List.mem_singleton] at hf; subst hf\n refine upto_true hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) (by simp) vals _ (fun hu0 => ?_)\n let u0 := vals ce.u\n have hc := closing_true s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0) hs [] (.or (.eq (.var q.1) (.const 1)) (.not (substC (q.2.1.zip (w.2.map .var)) q.2.2))) (by\n simp only [closingHyps, List.mem_flatMap]; exact ⟨q, hq, by simp [hl]⟩)\n have hwv : ∀ v ∈ w.2, v ∈ s.extVars := fun v hv => by\n simp only [SMachine.extVars, SMachine.wVars, List.mem_append, List.mem_flatMap]\n exact Or.inr ⟨w, (lookup_some hl).1, hv⟩\n have hσ : ∀ j, applyS (q.2.1.zip (w.2.map .var)) (applyS (cycσ ce (.var ce.u)) (override [ce.u] vals Q) (cycMaps s.sk ce r (T + 1))) (cycView s.sk ce r (T + 1) u0) j\n = applyS (q.2.1.zip (w.2.map .var)) (withExt s (r.st u0) (sReq s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0))) (withCarried s (r.mp u0) (r.κ u0)) j ∨ occC j q.2.2 = false := by\n intro j\n by_cases hjo : occC j q.2.2 = true\n · left\n simp only [applyS]\n cases ha : assoc (q.2.1.zip (w.2.map .var)) j with\n | some e =>\n obtain ⟨v, hv, rfl⟩ : ∃ v ∈ w.2, e = .var v := by\n simp only [assoc, Option.map_eq_some_iff] at ha\n obtain ⟨p, hp, rfl⟩ := ha\n have := (lookup_some hp).1\n obtain ⟨_, hpw⟩ := List.of_mem_zip this\n obtain ⟨v, hv, he⟩ := List.mem_map.mp hpw\n exact ⟨v, hv, he.symm⟩\n simp only [evalE]\n have hf := ext_free hs hS hT hr hd hb hQ (hwv v hv)\n exact upto_view hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) vals hu0 (by simp) hf.1 hf.2\n | none =>\n have hjk : j ∉ q.2.1 := by\n obtain ⟨_, _, hwd, hcov, _, _, _⟩ := sOk_parts s hs\n obtain ⟨⟨w', hw', hw1, hlen⟩, _⟩ := hcov q hq\n simp only [distinctPairs, Bool.and_eq_true] at hwd\n have hww : w = w' := by\n have h1 := lookup_unique hwd.1 hw'\n rw [hw1, hl] at h1; exact Option.some.inj h1\n subst hww\n exact assoc_zip_none q.2.1 (w.2.map .var) (by simp [hlen]) ha\n obtain ⟨hz, hfl⟩ := body_free hs hS hT hr hd hb hQ hq hjo hjk\n exact upto_view hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) vals hu0 (by simp) hz hfl\n · right; simpa using hjo\n rw [evalC_iff] at hc ⊢\n simp only [holdsC, evalE] at hc ⊢\n rw [upto_view hs hS hT hr hd hb hQ (fun x hx => by simp at hx; exact Or.inl hx) vals hu0 (by simp) hqv.1 hqv.2]\n rcases hc with hc | hc\n · exact Or.inl hc\n · right\n intro hn; apply hc\n rw [← evalC_iff] at hn ⊢\n rw [subst_evalC]\n rw [subst_evalC] at hn\n rw [← congr2C (ρ' := applyS (q.2.1.zip (w.2.map .var)) (withExt s (r.st u0) (sReq s (r.st u0) (r.mp u0) (r.ι u0) (r.κ u0))) (withCarried s (r.mp u0) (r.κ u0)))\n (M' := withCarried s (r.mp u0) (r.κ u0)) q.2.2 (fun j hj => by rcases hσ j with h | h; exact h; rw [hj] at h; cases h)\n (fun m hm => hN u0 m (avoidsMaps_occ hmb hm))]\n exact hn\n\nend qfacts\n\nend sk\n\nsection skseq\nvariable {s : SMachine}\n\n-- Requests in a row, read by the sk machine: each decided at the machine's state it is taken in.\nnoncomputable def skReqs (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → List SRequest\n | _, _, [] => []\n | ρ, M, q :: qs => (sReq s ρ M q.1 q.2, q.2) :: skReqs s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs\n\ntheorem skReqs_length : ∀ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (qs : List SRequest), (skReqs s ρ M qs).length = qs.length\n | _, _, [] => rfl\n | ρ, M, q :: qs => by simp [skReqs, skReqs_length _ _ qs]\n\ntheorem skReqs_take : ∀ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (qs : List SRequest) (j : Nat), (skReqs s ρ M qs).take j = skReqs s ρ M (qs.take j)\n | _, _, [], _ => by simp [skReqs]\n | _, _, _ :: _, 0 => by simp [skReqs]\n | ρ, M, q :: qs, j + 1 => by simp [skReqs, skReqs_take _ _ qs j]\n\n-- Along requests the machine allows, its sk machine allows theirs, each state agreeing off the witnesses, the maps the\n-- same; each `when` read alike.\ntheorem sk_allowed (hs : sOk s = true) : ∀ (ws : List C) (qs : List SRequest) (ρ ρ' : Nat → Nat) (M : Nat → Nat → Nat),\n Agree s.wVars ρ' ρ → SAllowed s ws ρ M qs → SAllowed s.sk ws ρ' M (skReqs s ρ M qs)\n | [], _, _, _, _, _, _ => by cases (skReqs _ _ _ _) <;> trivial\n | _ :: _, [], _, _, _, _, _ => trivial\n | w :: ws, q :: qs, ρ, ρ', M, hag, hK => by\n obtain ⟨hA, hW, hrest⟩ := hK\n obtain ⟨hA', hS', hM'⟩ := sk_step hs hag M q.1 q.2\n refine ⟨by show evalC _ _ s.f.m.assume = true; rw [hA']; exact hA, ?_, ?_⟩\n · show evalC (withExt s.sk ρ' (sReq s.sk ρ' M (sReq s ρ M q.1 q.2) q.2)) (withCarried s.sk M q.2) w = true\n have he : withExt s.sk ρ' (sReq s.sk ρ' M (sReq s ρ M q.1 q.2) q.2) = withInputs s.sk.f.m.inputs ρ' (sReq s ρ M q.1 q.2) := by\n funext v\n rw [withExt_plain sk_q rfl, sReq_plain sk_q rfl]\n simp only [withInputs]; split <;> rfl\n rw [he, sk_inputsView ρ ρ' hag]; exact hW\n · show SAllowed s.sk ws (sNext s.sk ρ' M (sReq s ρ M q.1 q.2) q.2).1 (sNext s.sk ρ' M (sReq s ρ M q.1 q.2) q.2).2\n (skReqs s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs)\n rw [hM']\n exact sk_allowed hs ws qs _ _ _ hS' hrest\n\ntheorem sk_end (hs : sOk s = true) : ∀ (qs : List SRequest) (ρ ρ' : Nat → Nat) (M : Nat → Nat → Nat), Agree s.wVars ρ' ρ →\n Agree s.wVars (sEnd s.sk ρ' M (skReqs s ρ M qs)).1 (sEnd s ρ M qs).1 ∧ (sEnd s.sk ρ' M (skReqs s ρ M qs)).2 = (sEnd s ρ M qs).2\n | [], _, _, _, hag => ⟨hag, rfl⟩\n | q :: qs, ρ, ρ', M, hag => by\n obtain ⟨_, hS', hM'⟩ := sk_step hs hag M q.1 q.2\n simp only [skReqs, sEnd]\n rw [hM']\n exact sk_end hs qs _ _ _ hS'\n\n-- The j-th request of a row, read by the sk machine.\ntheorem skReqs_get : ∀ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (qs : List SRequest) (j : Nat) (h : j < qs.length),\n (skReqs s ρ M qs)[j]'(by rw [skReqs_length]; exact h)\n = (sReq s (sEnd s ρ M (qs.take j)).1 (sEnd s ρ M (qs.take j)).2 qs[j].1 qs[j].2, qs[j].2)\n | _, _, [], _, h => absurd h (Nat.not_lt_zero _)\n | ρ, M, q :: qs, 0, _ => by simp [skReqs, sEnd]\n | ρ, M, q :: qs, j + 1, h => by\n simp only [skReqs, List.getElem_cons_succ, List.take_succ_cons, sEnd, List.length_cons] at h ⊢\n exact skReqs_get _ _ qs j (by omega)\n\n-- Each step's extended request, as SKStepHolds reads it.\ntheorem sExts_get : ∀ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (qs : List SRequest) (j : Nat) (h : j < qs.length),\n (sExts s ρ M qs)[j]'(by\n have : ∀ ρ M (qs : List SRequest), (sExts s ρ M qs).length = qs.length := by\n intro ρ M qs; induction qs generalizing ρ M with\n | nil => rfl\n | cons q qs ih => simp [sExts, ih]\n rw [this]; exact h)\n = sReq s (sEnd s ρ M (qs.take j)).1 (sEnd s ρ M (qs.take j)).2 qs[j].1 qs[j].2\n | _, _, [], _, h => absurd h (Nat.not_lt_zero _)\n | ρ, M, q :: qs, 0, _ => by simp [sExts, sEnd]\n | ρ, M, q :: qs, j + 1, h => by\n simp only [sExts, List.getElem_cons_succ, List.take_succ_cons, sEnd, List.length_cons] at h ⊢\n exact sExts_get _ _ qs j (by omega)\n\nend skseq\n\n-- The keys the quantified flags bind.\ndef SMachine.bk (s : SMachine) : List Nat := s.qflags.flatMap (·.2.1)\n\n-- Each exported fact is one of the sk machine's encoding, one a flag that holds at every key adds, or true of every natural.\ndef cycQFactsIn (s : SMachine) (ce : CycEnc) (facts : List Fact) : Bool :=\n facts.all fun f => natC f.holds || (cycCanon s.sk ce ++ cycQFacts s ce).any (factBeq f)\n\ntheorem qfacts_all {s : SMachine} {ce : CycEnc} {keys : List Nat} (hs : sOk s = true) (hS : CycShape s.sk ce keys) (hT : CycTerms s.sk ce keys)\n {r : CRun} {T : Nat} (hr : r.Ok s.sk T) (hd : ∀ u, u ≤ T → r.Decided s u) (hb : cycBodyOk s ce keys = true)\n {Q : Nat → Nat} (hQ : EnvOk s.sk ce keys r T Q) {facts : List Fact} (hfa : cycQFactsIn s ce facts = true) :\n FactsHold facts Q (cycMaps s.sk ce r (T + 1)) := by\n intro f hf vals\n have hfi := List.all_eq_true.mp hfa f hf\n simp only [Bool.or_eq_true] at hfi\n rcases hfi with hnc | hcan\n · exact natC_true _ hnc\n · obtain ⟨g, hg, hb'⟩ := List.any_eq_true.mp hcan\n simp only [factBeq, Bool.and_eq_true, beq_iff_eq] at hb'\n rw [hb'.1, beqC_eq hb'.2]\n rcases List.mem_append.mp hg with hg | hg\n · exact canon_holds hS hT hr hg hQ vals\n · exact qfacts_hold hs hS hT hr hd hb hQ hg vals\n\n/-- A cyclic proof of a law of a machine with flags that hold at every key, checked against its sk machine's encoding\nwith those flags' facts (`cycQMOk`): the law holds in every state the machine reaches, at every value of its keys. -/\ndef cycQMOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (law : C) (keys : List Nat) (n : CNode) : Bool :=\n sOk s && cycShapeOk s.sk ce (keys ++ s.bk) && cycTermsOk s.sk ce (keys ++ s.bk) && cycBodyOk s ce (keys ++ s.bk)\n && cycLawOk s.sk ce law && cycQFactsIn s ce facts && cycNodeOk s.sk ce law n\n\ntheorem cyc_qmachine_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (law : C) (keys : List Nat) (n : CNode) (hn : n ∈ nodes) (hok : cycQMOk s ce facts law keys n = true) :\n ∀ ρ M, SReach s ρ M → ∀ ρ' : Nat → Nat, (∀ i, i ∉ keys → ρ' i = ρ i) → evalC ρ' M law = true := by\n simp only [cycQMOk, Bool.and_eq_true] at hok\n obtain ⟨⟨⟨⟨⟨⟨hs, hsh⟩, hte⟩, hbo⟩, hlaw⟩, hfa⟩, hnd⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n simp only [cycNodeOk, Bool.and_eq_true] at hnd\n simp only [cycLawOk, Bool.and_eq_true] at hlaw\n intro ρ M hreach ρ' hρ'\n obtain ⟨r, T, hr, hd, hag, hmp⟩ := sk_run hs hreach\n have hsp := special_parts hS.dsp\n have hnot : ∀ i, i ∉ ce.special → cycEnv ce T ρ' i = ρ' i := fun i hi => by\n simp only [cycEnv]\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]\n have hEnv : EnvOk s.sk ce (keys ++ s.bk) r T (cycEnv ce T ρ') := by\n refine ⟨by simp [cycEnv], by simp [cycEnv, Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1], ?_⟩\n intro i hi\n obtain ⟨h1, h2, h3, h4⟩ := notP hi\n have hwi : i ∉ s.wVars := fun h => h3 (hS.inv i (by rw [sk_inputs]; simp only [SMachine.extVars, List.mem_append]; exact Or.inr h))\n rw [hnot i h1, hρ' i (fun h => h2 (List.mem_append_left _ h)), ← hag i hwi]\n exact run_frame hr T (Nat.le_refl T) i (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact h3 (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact h3 (hS.stepv p hp)) (fun h => h3 (hS.inv i h)) h4\n have hF : FactsHold facts (cycEnv ce T ρ') (cycMaps s.sk ce r (T + 1)) := qfacts_all hs hS hT hr hd hbo hEnv hfa\n have htT : cycEnv ce T ρ' ce.t = T := by simp [cycEnv]\n have hH : ∀ c ∈ n.hyps, evalC (cycEnv ce T ρ') (cycMaps s.sk ce r (T + 1)) c = true := by\n intro c hc\n have h := List.all_eq_true.mp hnd.2 c hc\n simp only [Bool.or_eq_true] at h\n rcases h with (h | h) | h\n · rw [beqC_eq h, congr2C (ρ' := r.ρ0) (M' := r.mp 0) _ (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj))\n (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, htT, hEnv.1] <;> omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hEnv.1, hEnv.2.1] <;> omega\n have hc0 := cyclic_sound facts nodes hc (cycMaps s.sk ce r (T + 1)) (cycEnv ce T ρ') hF n hn hH\n have htT' : evalE (cycEnv ce T ρ') (cycMaps s.sk ce r (T + 1)) (.var ce.t) = T := htT\n rw [beqC_eq hnd.1, enc_trC hS hT hr hEnv htT' (Nat.le_refl T)] at hc0\n rw [congr2C (ρ' := applyS (cycσ ce (.var ce.t)) (cycEnv ce T ρ') (cycMaps s.sk ce r (T + 1))) (M' := cycView s.sk ce r (T + 1) T) law\n (fun j hj => ?_) (fun m hm => ?_)]\n · exact hc0\n · have hj' := avoids_occ hlaw.1 hj\n have hjs : j ∉ ce.special := fun h => hj' (List.mem_append_left _ h)\n have hji : j ∉ s.sk.f.m.inputs := fun h => hj' (List.mem_append_right _ h)\n by_cases hv : j ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr hEnv htT' (Nat.le_refl T) hv, CRun.X, withInputs_other hji]\n have hwj : j ∉ s.wVars := fun h => hji (by rw [sk_inputs]; simp only [SMachine.extVars, List.mem_append]; exact Or.inr h)\n rw [hag j hwj]\n exact hρ' j (fun hk => hS.zv j (List.mem_append_right _ (List.mem_append_left _ hk)) hv)\n · rw [enc_σ_out hS hT hr hEnv htT' (Nat.le_refl T) hv, hnot j hjs]\n · have hm' := avoidsMaps_occ hlaw.2 hm\n rw [cv_N (fun h => hm' (List.mem_append_left _ h)), N_not_carried (fun h => hm' (List.mem_append_right _ h)), hmp]\n\n\n/-- A k-step law's node for a machine with flags that hold at every key, checked against its sk machine's encoding with\nthose flags' facts (`cycKOk` over the sk machine, the flags' keys apart). -/\ndef cycQKOkAt (tb : Bool) (s : SMachine) (ce : CycEnc) (facts : List Fact) (sl : SKStep) (n : CNode) : Bool :=\n sOk s && cycShapeOk s.sk ce s.bk && cycTermsOk s.sk ce s.bk && cycBodyOk s ce s.bk && cycQFactsIn s ce facts\n && sl.k.ren.length == sl.k.whens.length && sl.mren.length == sl.k.whens.length\n && sl.k.ren.all (fun r => r.all fun p => s.sk.f.m.inputs.contains p.1)\n && sl.k.prime.all (fun q => (ce.vtr.map (·.1)).contains q.1 && !s.sk.f.m.inputs.contains q.1)\n && sl.mren.all (fun r => r.all fun p => s.carried.contains p.1 && !ce.tm.contains p.2)\n && sl.k.primeMaps.all (fun q => (ce.mtr.map (·.1)).contains q.1 && !s.carried.contains q.1 && !ce.tm.contains q.2)\n && avoids (ce.special ++ s.bk ++ flagVars s.f.flags) sl.k.then_ && avoidsMaps ce.tm sl.k.then_\n && sl.k.whens.all (fun w => avoids (ce.special ++ s.bk ++ flagVars s.f.flags) w && avoidsMaps ce.tm w)\n && beqC n.concl (substRC (kσ tb ce sl) (kRd tb ce sl) sl.k.then_)\n && n.hyps.all (fun h => beqC h s.f.m.pre || beqC h (.le (.const sl.k.whens.length) (.var ce.t)) || beqC h (.le (.var ce.t) (.var ce.T))\n || beqC h (.lt (.var ce.T) (.var ce.om)) || (kWhens tb ce sl.k.whens.length 0 sl.k.whens).any (beqC h))\n\ntheorem cyc_qkstep_sound_at (tb : Bool) (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (sl : SKStep) (n : CNode) (hn : n ∈ nodes) (hok : cycQKOkAt tb s ce facts sl n = true) : SKStepHolds s sl := by\n simp only [cycQKOkAt, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hok\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hs, hsh⟩, hte⟩, hbo⟩, hfa⟩, hrl⟩, hml⟩, hrin⟩, hpr⟩, hmr⟩, hpm⟩, hthv⟩, hthm⟩, hwh⟩, hcon⟩, hhy⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n have hsp := special_parts hS.dsp\n have hcz : ∀ x, x ∉ ce.special ++ s.bk ++ flagVars s.f.flags → x ∉ cycZ ce s.bk ∧ x ∉ flagVars s.f.flags := fun x hx =>\n ⟨fun h => hx (List.mem_append_left _ h), fun h => hx (List.mem_append_right _ h)⟩\n intro ρ M hr q₁ qs hlen hK\n obtain ⟨r, T0, hr0, hdec, hag, hmp⟩ := sk_run hs hr\n have hK' := sk_allowed hs sl.k.whens (q₁ :: qs) ρ (r.st T0) M hag hK\n have hql : (skReqs s ρ M (q₁ :: qs)).length = (q₁ :: qs).length := skReqs_length _ _ _\n obtain ⟨r1, hr1, _, hle, hlt, hj, hι⟩ := run_ext sk_q (skReqs s ρ M (q₁ :: qs)) sl.k.whens r T0 (r.st T0) M hr0 rfl hmp (by rw [hql]; simp; omega) hK'\n rw [hql] at hr1 hj\n have hk : sl.k.whens.length = (q₁ :: qs).length := by simp; omega\n -- The state after the first j requests, and the j-th request, as the sk machine's run has them.\n have hagj : ∀ j, j ≤ (q₁ :: qs).length → Agree s.wVars (r1.st (T0 + j)) (sEnd s ρ M ((q₁ :: qs).take j)).1\n ∧ r1.mp (T0 + j) = (sEnd s ρ M ((q₁ :: qs).take j)).2 := fun j hj' => by\n obtain ⟨a, b⟩ := hj j hj'\n rw [a, b, skReqs_take]; exact sk_end hs _ _ _ M hag\n have hreqj : ∀ j (h : j < (q₁ :: qs).length), r1.ι (T0 + j) = sReq s (sEnd s ρ M ((q₁ :: qs).take j)).1 (sEnd s ρ M ((q₁ :: qs).take j)).2 (q₁ :: qs)[j].1 (q₁ :: qs)[j].2\n ∧ r1.κ (T0 + j) = (q₁ :: qs)[j].2 := fun j h => by\n obtain ⟨a, b⟩ := hι j (by rw [hql]; exact h)\n have hg := skReqs_get (s := s) ρ M (q₁ :: qs) j h\n rw [a, b]; simp only [hg, and_self]\n -- The last request (no step takes it) decided too: every request of the run is.\n let T' := T0 + (q₁ :: qs).length\n let r' := r1.last T' (sReq s (r1.st T') (r1.mp T') (fun _ => 0) noSets) noSets\n have hr' : r'.Ok s.sk T' := last_ok hr1 _ _\n have hd' : ∀ u, u ≤ T' → r'.Decided s u := by\n intro u hu\n by_cases huT : u = T'\n · subst huT; exact last_decided\n · refine last_other huT _ _ ?_\n rcases Nat.lt_or_ge u T0 with hlt0 | hge0\n · have h := hdec u (Nat.le_of_lt hlt0)\n obtain ⟨a, b⟩ := hle u (Nat.le_of_lt hlt0)\n obtain ⟨c', d⟩ := hlt u hlt0\n simp only [CRun.Decided, a, b, c', d] at h ⊢; exact h\n · obtain ⟨j, rfl⟩ : ∃ j, u = T0 + j := ⟨u - T0, by omega⟩\n have hjl : j < (q₁ :: qs).length := by simp only [T'] at hu huT; omega\n obtain ⟨ha, hm⟩ := hagj j (Nat.le_of_lt hjl)\n obtain ⟨hι', hκ'⟩ := hreqj j hjl\n simp only [CRun.Decided, hι', hκ', hm]\n rw [sReq_agree hs ha, sReq_idem]\n -- The node's environment: t and T the run's end, Ω one above it, the rest from before the start.\n obtain ⟨Q, hQt, hQT, hQo, hQr⟩ : ∃ Q : Nat → Nat, Q ce.t = T' ∧ Q ce.T = T' ∧ Q ce.om = T' + 1 ∧ ∀ i, i ∉ ce.special → Q i = r'.ρ0 i :=\n ⟨fun i => if i = ce.t then T' else if i = ce.T then T' else if i = ce.om then T' + 1 else r'.ρ0 i,\n by simp, by simp [Ne.symm hsp.2.2.2.2.2.1], by simp [Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1],\n fun i hi => by\n dsimp only\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]⟩\n have hEnv : EnvOk s.sk ce s.bk r' T' Q := ⟨hQT, hQo, fun i hi => hQr i (notP hi).1⟩\n have hF : FactsHold facts Q (cycMaps s.sk ce r' (T' + 1)) := qfacts_all hs hS hT hr' hd' hbo hEnv hfa\n have hframe : ∀ u, u ≤ T' → ∀ i, i ∉ ce.vtr.map (·.1) → i ∉ flagVars s.f.flags → r'.st u i = r'.ρ0 i := fun u hu i hv hf =>\n run_frame hr' u hu i (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.initv p hp))\n (fun h => by obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h; exact hv (hS.stepv p hp)) (fun h => hv (hS.inv i h)) hf\n have hwV : ∀ x, x ∉ ce.vtr.map (·.1) → x ∉ s.extVars := fun x hv h => hv (hS.inv x (by rw [sk_inputs]; exact h))\n have hwW : ∀ x, x ∉ ce.vtr.map (·.1) → x ∉ s.wVars := fun x hv h => hwV x hv (by simp only [SMachine.extVars, List.mem_append]; exact Or.inr h)\n -- What the run reads at T0 + j: the goal's view of the j-th step of the machine.\n have hXj : ∀ j (h : j < (q₁ :: qs).length), r'.X s.sk (T0 + j) = withExt s (sEnd s ρ M ((q₁ :: qs).take j)).1\n (sReq s (sEnd s ρ M ((q₁ :: qs).take j)).1 (sEnd s ρ M ((q₁ :: qs).take j)).2 (q₁ :: qs)[j].1 (q₁ :: qs)[j].2) := fun j h => by\n have hne : T0 + j ≠ T' := by simp only [T']; omega\n show withInputs s.sk.f.m.inputs (r1.st (T0 + j)) (if T0 + j = T' then _ else r1.ι (T0 + j)) = _\n rw [if_neg hne, (hreqj j h).1]\n exact sk_inputsView _ _ (hagj j (Nat.le_of_lt h)).1 _ _ _\n have hNj : ∀ j (h : j < (q₁ :: qs).length), r'.N s.sk (T0 + j) = withCarried s (sEnd s ρ M ((q₁ :: qs).take j)).2 (q₁ :: qs)[j].2 := fun j h => by\n have hne : T0 + j ≠ T' := by simp only [T']; omega\n show withCarried s.sk (r1.mp (T0 + j)) (if T0 + j = T' then _ else r1.κ (T0 + j)) = _\n rw [if_neg hne, (hreqj j h).2, (hagj j (Nat.le_of_lt h)).2]; rfl\n have hq0 : 0 < (q₁ :: qs).length := by simp\n have hw0 : evalE Q (cycMaps s.sk ce r' (T' + 1)) (tAtS tb ce sl.k.whens.length) = T0 := by rw [tAtS_eval tb, hQt, hk]; simp only [T']; omega\n -- The node's hypotheses hold there.\n have hH : ∀ c ∈ n.hyps, evalC Q (cycMaps s.sk ce r' (T' + 1)) c = true := by\n intro c hc\n have h := hhy c hc\n simp only [Bool.or_eq_true] at h\n rcases h with (((h | h) | h) | h) | h\n · rw [beqC_eq h, congr2C (ρ := Q) (M := cycMaps s.sk ce r' (T' + 1)) (ρ' := r'.ρ0) (M' := r'.mp 0) s.f.m.pre\n (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj)) (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr'.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hk]; simp only [T']; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hQT]; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQT, hQo]; omega\n · obtain ⟨c', hc', hb⟩ := List.any_eq_true.mp h\n rw [beqC_eq hb]\n obtain ⟨i, hi, rfl⟩ := kWhens_mem tb ce sl.k.whens.length 0 sl.k.whens hc'\n have hiq : i < (q₁ :: qs).length := by omega\n have hu : T0 + i ≤ T' := by simp only [T']; omega\n have hwi : evalE Q (cycMaps s.sk ce r' (T' + 1)) (tAtS tb ce (sl.k.whens.length - (0 + i))) = T0 + i := by rw [tAtS_eval tb, hQt, hk]; simp only [T']; omega\n have hall := sallowed_at (q₁ :: qs) sl.k.whens ρ M hK i hiq hi\n have hav := hwh _ (List.getElem_mem hi)\n rw [enc_trC hS hT hr' hEnv hwi hu]\n rw [congr2C (ρ' := withExt s (sEnd s ρ M ((q₁ :: qs).take i)).1 (sReq s (sEnd s ρ M ((q₁ :: qs).take i)).1 (sEnd s ρ M ((q₁ :: qs).take i)).2 (q₁ :: qs)[i].1 (q₁ :: qs)[i].2))\n (M' := withCarried s (sEnd s ρ M ((q₁ :: qs).take i)).2 (q₁ :: qs)[i].2) _ ?_ ?_]\n · exact hall\n · intro x hx\n obtain ⟨hz, hfl⟩ := hcz x (avoids_occ hav.1 hx)\n rw [enc_Y hS hT hr' hEnv hwi hu hz hfl, hXj i hiq]\n · intro m hm\n rw [cv_N (avoidsMaps_occ hav.2 hm), hNj i hiq]\n have hc0 := cyclic_sound facts nodes hc (cycMaps s.sk ce r' (T' + 1)) Q hF n hn hH\n rw [beqC_eq hcon] at hc0\n -- `then` over the run: the maps it reads, then its names.\n have hTle : T0 ≤ T' := by simp only [T']; omega\n obtain ⟨hagE, hmE⟩ : Agree s.wVars (r'.st T') (sEnd s ρ M (q₁ :: qs)).1 ∧ r'.mp T' = (sEnd s ρ M (q₁ :: qs)).2 := by\n have h := hagj (q₁ :: qs).length (Nat.le_refl _); simp only [List.take_length] at h; exact h\n let Mt : Nat → Nat → Nat := primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 (renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2))\n have hrd : ∀ m key, evalE Q (cycMaps s.sk ce r' (T' + 1)) (kRd tb ce sl m key)\n = (fun m => if ce.tm.contains m then cycMaps s.sk ce r' (T' + 1) m else Mt m) m (evalE Q (cycMaps s.sk ce r' (T' + 1)) key) := by\n intro m key\n simp only [kRd]\n cases hq : lookup (fun q => q.2 == m) sl.k.primeMaps with\n | some q =>\n obtain ⟨hqm, hqe⟩ := lookup_some hq\n have hqe' : q.2 = m := by simpa using hqe\n obtain ⟨⟨hq1, hq2⟩, hq3⟩ := hpm q hqm\n have hnt : ¬ ce.tm.contains m = true := by rw [← hqe']; simpa using hq3\n simp only [evalE, tAtS_eval tb, hQt, hQo, Nat.sub_zero]\n rw [read_mm hS.dtm (List.contains_iff_mem.mp hq1) (Nat.le_refl _), N_not_carried (show q.1 ∉ s.sk.carried from fun h => by simp only [Bool.not_eq_true'] at hq2; rw [List.contains_iff_mem.mpr (show q.1 ∈ s.carried from h)] at hq2; cases hq2), hmE, if_neg hnt]\n simp only [Mt, primedMaps, hq]\n | none =>\n have hkm := kMren_eval sl.mren 0 ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2) m (by simp; omega)\n cases hkr : kMren 0 sl.mren m with\n | some p =>\n obtain ⟨i', hi', hκ', hi0, ⟨p', hp', hp1, hp2⟩, hren⟩ := hkm.2 p.1 p.2 hkr\n obtain ⟨hcar, hnt0⟩ := hmr _ (List.getElem_mem hi') p' hp'\n have hnt : ¬ ce.tm.contains m = true := by rw [← hp2]; simpa using hnt0\n have hiq : i' < (q₁ :: qs).length := by simpa using hκ'\n simp only [evalE, tAtS_eval tb, hQt, hQo, hi0, Nat.zero_add]\n rw [show T' - (sl.k.whens.length - i') = T0 + i' by rw [hk]; simp only [T']; omega]\n rw [read_mm hS.dtm (hS.carm _ (by rw [← hp1]; exact List.contains_iff_mem.mp hcar)) (by simp only [T']; omega), hNj i' hiq, if_neg hnt]\n simp only [Mt, primedMaps, hq, hren, withCarried, ← hp1, hcar, if_true, List.getElem_map]\n | none =>\n rw [enc_rd hS hT hr' hEnv hw0 hTle]\n simp only [cycView]\n by_cases ht : ce.tm.contains m = true\n · rw [if_pos ht, if_pos ht]\n · rw [if_neg ht, if_neg ht]\n have h0 := hNj 0 hq0\n simp only [Nat.add_zero, List.take_zero, sEnd, List.getElem_cons_zero] at h0\n rw [h0]\n simp only [Mt, primedMaps, hq, hkm.1 hkr]\n rw [substR_evalC (kσ tb ce sl) (kRd tb ce sl) Q _ _ hrd] at hc0\n rw [congr2C (ρ' := primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 (renEnv sl.k.ren (sExts s ρ M (q₁ :: qs)) (withExt s ρ (sReq s ρ M q₁.1 q₁.2))))\n (M' := Mt) _ ?_ ?_] at hc0\n · exact hc0\n · intro x hx\n obtain ⟨hz, hfl⟩ := hcz x (avoids_occ hthv hx)\n simp only [kσ, List.append_assoc]\n rw [applyS_app, assoc_ren]\n simp only [primed]\n cases hq : lookup (fun q => q.2 == x) sl.k.prime with\n | some q =>\n obtain ⟨hqm, _⟩ := lookup_some hq\n obtain ⟨hq1, hq2⟩ := hpr q hqm\n have hqw : q.1 ∉ s.wVars := fun h => by\n simp only [Bool.not_eq_true'] at hq2\n rw [List.contains_iff_mem.mpr (by rw [sk_inputs]; simp only [SMachine.extVars, List.mem_append]; exact Or.inr h)] at hq2; cases hq2\n simp only [Option.map_some, evalE, tAtS_eval tb, hQt, Nat.sub_zero]\n rw [read_vm hS.dtm (List.contains_iff_mem.mp hq1), CRun.X, withInputs_other (by simpa using hq2), hagE q.1 hqw]\n | none =>\n simp only [Option.map_none]\n by_cases hxr : x ∈ tgts sl.k.ren\n · have hel : ∀ ρ M (qs : List SRequest), (sExts s ρ M qs).length = qs.length := by\n intro ρ M qs; induction qs generalizing ρ M with\n | nil => rfl\n | cons q qs ih => simp [sExts, ih]\n rw [kRen_eval tb ce sl.k.whens.length Q _ _ _ sl.k.ren 0 (sExts s ρ M (q₁ :: qs)) (by rw [hel]; omega) ?_ x hxr]\n intro i hi hιl p hp\n have hpin := hrin _ (List.getElem_mem hi) p hp\n have hiq : i < (q₁ :: qs).length := by rw [hel] at hιl; exact hιl\n simp only [evalE, tAtS_eval tb, hQt, Nat.zero_add]\n rw [show T' - (sl.k.whens.length - i) = T0 + i by rw [hk]; simp only [T']; omega]\n rw [read_vm hS.dtm (hS.inv _ (List.contains_iff_mem.mp hpin)), hXj i hiq, sExts_get ρ M (q₁ :: qs) i hiq]\n simp only [withExt, show s.extVars.contains p.1 = true from by rw [← sk_inputs]; exact hpin, if_true]\n · rw [kRen_out tb ce sl.k.whens.length Q _ _ sl.k.ren 0 x hxr, renEnv_other _ _ _ x hxr]\n have hX0 := hXj 0 hq0\n simp only [Nat.add_zero, List.take_zero, sEnd, List.getElem_cons_zero] at hX0\n by_cases hv : x ∈ ce.vtr.map (·.1)\n · rw [enc_σ_in hS hT hr' hEnv hw0 hTle hv, hX0]\n · rw [enc_σ_out hS hT hr' hEnv hw0 hTle hv, hQr x (fun h => hz (List.mem_append_left _ h))]\n simp only [withExt, show s.extVars.contains x = false from by simpa [List.contains_iff_mem] using hwV x hv, Bool.false_eq_true, if_false]\n have h0 := hagj 0 (Nat.zero_le _)\n simp only [Nat.add_zero, List.take_zero, sEnd] at h0\n rw [← h0.1 x (hwW x hv)]\n exact (hframe T0 hTle x hv hfl).symm\n · intro m hm\n simp only [if_neg (fun h => avoidsMaps_occ hthm hm (List.contains_iff_mem.mp h))]\n\n\ndef cycQKOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (sl : SKStep) (n : CNode) : Bool :=\n cycQKOkAt true s ce facts sl n || cycQKOkAt false s ce facts sl n\n\ntheorem cyc_qkstep_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (sl : SKStep) (n : CNode) (hn : n ∈ nodes) (hok : cycQKOk s ce facts sl n = true) : SKStepHolds s sl := by\n simp only [cycQKOk, Bool.or_eq_true] at hok\n rcases hok with h | h\n · exact cyc_qkstep_sound_at true s ce facts nodes hc sl n hn h\n · exact cyc_qkstep_sound_at false s ce facts nodes hc sl n hn h\n\n/-- A request allowed, as a node for a machine with flags that hold at every key (`cycAOk` over its sk machine, those\nflags' facts, the flags' keys apart). -/\ndef cycQAOk (s : SMachine) (ce : CycEnc) (facts : List Fact) (w : C) (n : CNode) : Bool :=\n sOk s && cycShapeOk s.sk ce s.bk && cycTermsOk s.sk ce s.bk && cycBodyOk s ce s.bk && cycQFactsIn s ce facts\n && avoids (ce.special ++ s.bk ++ flagVars s.f.flags) w && avoidsMaps ce.tm w\n && beqC n.concl (trC ce (.var ce.t) (inlineC s.f.flags s.f.m.assume))\n && n.hyps.all (fun h => beqC h s.f.m.pre || beqC h (.le (.var ce.t) (.var ce.T)) || beqC h (.lt (.var ce.T) (.var ce.om))\n || beqC h (trC ce (.var ce.t) w) || cycZeroOk s ce h)\n\ntheorem cyc_qallow_sound (s : SMachine) (ce : CycEnc) (facts : List Fact) (nodes : List CNode) (hc : checkCyclic facts nodes = true)\n (w : C) (n : CNode) (hn : n ∈ nodes) (hok : cycQAOk s ce facts w n = true) : AllowHolds s w := by\n simp only [cycQAOk, Bool.and_eq_true, List.all_eq_true] at hok\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨hs, hsh⟩, hte⟩, hbo⟩, hfa⟩, hwv⟩, hwm⟩, hcon⟩, hhy⟩ := hok\n have hS := cycShape_of hsh\n have hT := cycTerms_of hte\n have hsp := special_parts hS.dsp\n intro ρ M hr ι hw\n obtain ⟨r, T, hr0, hd, hag, hmp⟩ := sk_run hs hr\n -- The run with the request at T (the last, which no step takes) the claim's, decided, carrying empty sets.\n let r2 := r.last T (sReq s ρ M ι noSets) noSets\n have hr2 : r2.Ok s.sk T := last_ok hr0 _ _\n have hd2 : ∀ u, u ≤ T → r2.Decided s u := by\n intro u hu\n by_cases huT : u = T\n · subst huT\n simp only [CRun.Decided, r2, CRun.last, ↓reduceIte, hmp]\n rw [sReq_agree hs hag, sReq_idem]\n · exact last_other huT _ _ (hd u hu)\n have hX : r2.X s.sk T = withExt s ρ (sReq s ρ M ι noSets) := by\n show withInputs s.sk.f.m.inputs (r.st T) (if T = T then sReq s ρ M ι noSets else r.ι T) = _\n rw [if_pos rfl]; exact sk_inputsView _ _ hag _ _ _\n have hN : r2.N s.sk T = withCarried s M noSets := by\n show withCarried s.sk (r.mp T) (if T = T then noSets else r.κ T) = _\n rw [if_pos rfl, hmp]; rfl\n obtain ⟨Q, hQt, hQT, hQo, hQr⟩ : ∃ Q : Nat → Nat, Q ce.t = T ∧ Q ce.T = T ∧ Q ce.om = T + 1 ∧ ∀ i, i ∉ ce.special → Q i = r.ρ0 i :=\n ⟨fun i => if i = ce.t then T else if i = ce.T then T else if i = ce.om then T + 1 else r.ρ0 i,\n by simp, by simp [Ne.symm hsp.2.2.2.2.2.1], by simp [Ne.symm hsp.2.2.2.2.2.2, Ne.symm hsp.2.2.2.2.1],\n fun i hi => by\n dsimp only\n rw [if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inr e)))))), if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inl e))))),\n if_neg (fun e => hi (in_special (Or.inr (Or.inr (Or.inr (Or.inl e))))))]⟩\n have hEnv : EnvOk s.sk ce s.bk r2 T Q := ⟨hQT, hQo, fun i hi => hQr i (notP hi).1⟩\n have hwt : evalE Q (cycMaps s.sk ce r2 (T + 1)) (.var ce.t) = T := by simp [evalE, hQt]\n have hF : FactsHold facts Q (cycMaps s.sk ce r2 (T + 1)) := qfacts_all hs hS hT hr2 hd2 hbo hEnv hfa\n have hH : ∀ c ∈ n.hyps, evalC Q (cycMaps s.sk ce r2 (T + 1)) c = true := by\n intro c hc\n have h := hhy c hc\n simp only [Bool.or_eq_true] at h\n rcases h with ((((h | h) | h) | h) | h)\n · rw [beqC_eq h, congr2C (ρ := Q) (M := cycMaps s.sk ce r2 (T + 1)) (ρ' := r2.ρ0) (M' := r2.mp 0) s.f.m.pre\n (fun j hj => hEnv.2.2 j (avoids_occ hT.prz hj)) (fun m hm => cm_o (avoidsMaps_occ hT.prm hm))]\n exact hr2.pre\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQt, hQT]; omega\n · rw [beqC_eq h, evalC_iff]; simp only [holdsC, evalE, hQT, hQo]; omega\n · rw [beqC_eq h, enc_trC hS hT hr2 hEnv hwt (Nat.le_refl T)]\n rw [congr2C (ρ' := withExt s ρ (sReq s ρ M ι noSets)) (M' := withCarried s M noSets) _ ?_ ?_]\n · exact hw\n · intro x hx\n have hxz := avoids_occ hwv hx\n rw [enc_Y hS hT hr2 hEnv hwt (Nat.le_refl T) (fun h => hxz (List.mem_append_left _ h)) (fun h => hxz (List.mem_append_right _ h)), hX]\n · intro m hm\n rw [cv_N (avoidsMaps_occ hwm hm), hN]\n · obtain ⟨cm, key, hcm, rfl⟩ := cycZeroOk_spec h\n rw [evalC_iff]\n simp only [holdsC, evalE, hQt, hQo]\n rw [read_mm hS.dtm (hS.carm cm hcm) (Nat.le_refl T), hN]\n simp only [withCarried, List.contains_iff_mem.mpr hcm, if_true, noSets]\n have hc0 := cyclic_sound facts nodes hc (cycMaps s.sk ce r2 (T + 1)) Q hF n hn hH\n rw [beqC_eq hcon] at hc0\n change evalC Q _ (trC ce (.var ce.t) (inlineC s.sk.f.flags s.f.m.assume)) = true at hc0\n rw [enc_nowC hS hT hr2 hEnv hwt (Nat.le_refl T) s.f.m.assume hT.asz hT.asm] at hc0\n obtain ⟨hV, hNv⟩ := sk_view hs hag M ι noSets\n have hV2 : r2.V s.sk T = (sView s.sk (r.st T) M (sReq s ρ M ι noSets) noSets).1 := by\n rw [sk_view_eq _ _ hag]; simp only [CRun.V, hX, hN]; rfl\n have hN2 : r2.N s.sk T = (sView s ρ M ι noSets).2 := by rw [hN]; rfl\n obtain ⟨_, _, _, has, _⟩ := sOk_terms hs\n rw [hV2, hN2, congrC _ (sView s ρ M ι noSets).1 _ s.f.m.assume (fun j hj => hV j (avoids_occ has hj))] at hc0\n exact hc0\n\nend Kern\n",
18
- "Data.lean": "import Cyclic\n\n/-!\nData: recursive datatypes as values, for cyclic proofs. Lean 4.34.0, core only, on top of `Cyclic.lean`.\n\ncore/data.ts proves laws about datatypes (lists, trees, any declared with constructors whose fields are naturals or\nvalues of the datatype) by cyclic proofs over an encoding: every natural is a value of each datatype, a value is read\nthrough maps (its constructor's tag, its fields, its size), a function of several arguments is a map read at their\npairing `pk a b = (a + b)² + b` (left-nested). Nothing here is trusted beyond `Cyclic.lean`: an export defines the\ndatatype as a Lean `inductive` and the functions by pattern matching, gives each map of the encoding its meaning (the\nmodel, through a bijection between the naturals and the datatype's values), proves every fact of the cyclic proof true\nof that model, and concludes the law about the Lean definitions from `cyclic_sound`. What this file gives the exports:\n\n- `pk`, the pairing, injective (`pk_inj`), and a left inverse `unpk` (`unpk_pk`, chosen classically, so no division or\n square root is computed);\n- `cun`, the naturals onto the pairs, in order of their sum (`cun 0 = (0, 0)`, then each pair after the last), with\n `cpair` its inverse (`cun_cpair`, `cpair_cun`): a bijection, the one a datatype's codes are built from, each part at\n most the whole (`cun_fst_le`, `cun_snd_le`) so a value's fields have smaller codes;\n- `tup q k i`, the i-th of k naturals packed in q by `tupk` (`tup_tupk`), at most q (`tup_le`);\n- `wf_fix_unique`: a definition by well-founded recursion has exactly one solution. core/data.ts takes a function whose\n recursion isn't structural once the kernel has proven that each of its calls lowers a measure (a lexicographic tuple\n of naturals, the order of the cyclic ranks, `lexLt`) under the path to the call; its body, read as a functional F of\n the function, then reads it only below the argument's measure, and such an F has one fixpoint. So the function's\n equation, a fact of its proofs, has a model (what they conclude isn't vacuous), and only one: the function as written.\n An export defines such a function with `termination_by` the same measure, `Lx` (a tuple, ordered by `lexLt`) where\n it has several components, and Lean checks the decrease again;\n- `wf_fix_unique_ring`: the same for a ring of functions calling each other (mutual recursion), each with its own\n measure (all of one length): one functional over the ring's tuple of functions, reading each only below the measure\n of the argument it is called at. An export defines the ring in one `mutual` block, each function with `termination_by`\n its measure.\n-/\n\nnamespace Kern\n\n/-! ### The pairing the encoding reads several arguments at -/\n\n/-- `(a + b)² + b`: core/dsl.ts's argsKey of two arguments. -/\ndef pk (a b : Nat) : Nat := (a + b) * (a + b) + b\n\ntheorem sq_succ (s : Nat) : (s + 1) * (s + 1) = s * s + 2 * s + 1 := by\n rw [Nat.add_mul, Nat.mul_add, Nat.mul_one, Nat.one_mul]; omega\n\ntheorem pk_lt {a b c d : Nat} (h : a + b < c + d) : pk a b < pk c d := by\n unfold pk\n have h1 : (a + b + 1) * (a + b + 1) ≤ (c + d) * (c + d) := Nat.mul_self_le_mul_self h\n have h2 := sq_succ (a + b)\n omega\n\ntheorem pk_inj {a b c d : Nat} (h : pk a b = pk c d) : a = c ∧ b = d := by\n have hs : a + b = c + d := by\n rcases Nat.lt_trichotomy (a + b) (c + d) with hl | he | hg\n · exact absurd h (Nat.ne_of_lt (pk_lt hl))\n · exact he\n · exact absurd h (Nat.ne_of_gt (pk_lt hg))\n unfold pk at h\n rw [hs] at h\n omega\n\nopen Classical in\n/-- A left inverse of `pk`: the pair a natural is the pairing of, where it is one. -/\nnoncomputable def unpk (n : Nat) : Nat × Nat :=\n if h : ∃ p : Nat × Nat, pk p.1 p.2 = n then Classical.choose h else (0, 0)\n\ntheorem unpk_pk (a b : Nat) : unpk (pk a b) = (a, b) := by\n have h : ∃ p : Nat × Nat, pk p.1 p.2 = pk a b := ⟨(a, b), rfl⟩\n unfold unpk\n split\n · rename_i h'\n have ⟨h1, h2⟩ := pk_inj (Classical.choose_spec h')\n exact Prod.ext h1 h2\n · exact absurd h ‹_›\n\n@[simp] theorem unpk_fst (a b : Nat) : (unpk ((a + b) * (a + b) + b)).1 = a := by\n have := unpk_pk a b; unfold pk at this; rw [this]\n@[simp] theorem unpk_snd (a b : Nat) : (unpk ((a + b) * (a + b) + b)).2 = b := by\n have := unpk_pk a b; unfold pk at this; rw [this]\n\n-- The pairing with a 0 argument, as simp leaves it: (a + 0)² + 0 is a·a, (0 + b)² + b is b·b + b, and (0 + 0)² + 0 is 0.\n@[simp] theorem unpk_sq_fst (a : Nat) : (unpk (a * a)).1 = a := by\n have := unpk_pk a 0; simp only [pk, Nat.add_zero] at this; rw [this]\n@[simp] theorem unpk_sq_snd (a : Nat) : (unpk (a * a)).2 = 0 := by\n have := unpk_pk a 0; simp only [pk, Nat.add_zero] at this; rw [this]\n@[simp] theorem unpk_sqb_fst (b : Nat) : (unpk (b * b + b)).1 = 0 := by\n have := unpk_pk 0 b; simp only [pk, Nat.zero_add] at this; rw [this]\n@[simp] theorem unpk_sqb_snd (b : Nat) : (unpk (b * b + b)).2 = b := by\n have := unpk_pk 0 b; simp only [pk, Nat.zero_add] at this; rw [this]\n@[simp] theorem unpk_zero : unpk 0 = (0, 0) := unpk_pk 0 0\n\n/-! ### A bijection between the naturals and the pairs -/\n\n/-- The naturals onto the pairs, in order of their sum: 0 is (0, 0), and each next natural the pair after. -/\ndef cun : Nat → Nat × Nat\n | 0 => (0, 0)\n | n + 1 => match cun n with\n | (0, b) => (b + 1, 0)\n | (a + 1, b) => (a, b + 1)\n\n/-- 0 + 1 + … + s. -/\ndef tri : Nat → Nat\n | 0 => 0\n | s + 1 => tri s + s + 1\n\n/-- `cun`'s inverse. -/\ndef cpair (a b : Nat) : Nat := tri (a + b) + b\n\ntheorem le_tri : ∀ s, s ≤ tri s\n | 0 => Nat.le_refl 0\n | s + 1 => by have := le_tri s; simp only [tri]; omega\n\ntheorem cpair_succ_left (b : Nat) : cpair (b + 1) 0 = cpair 0 b + 1 := by\n simp only [cpair, Nat.add_zero, Nat.zero_add, tri]\n\ntheorem cpair_succ_right (a b : Nat) : cpair a (b + 1) = cpair (a + 1) b + 1 := by\n simp only [cpair]\n have e : a + (b + 1) = a + 1 + b := by omega\n rw [e]; omega\n\ntheorem cpair_cun : ∀ n, cpair (cun n).1 (cun n).2 = n\n | 0 => rfl\n | n + 1 => by\n have ih := cpair_cun n\n rcases h : cun n with ⟨a, b⟩\n rw [h] at ih\n cases a with\n | zero => simp only [cun, h]; rw [cpair_succ_left]; simp only at ih; omega\n | succ a => simp only [cun, h]; rw [cpair_succ_right]; simp only at ih; omega\n\ntheorem cun_tri : ∀ s b, b ≤ s → cun (tri s + b) = (s - b, b)\n | 0, 0, _ => rfl\n | 0, _ + 1, h => absurd h (by omega)\n | s + 1, 0, _ => by\n have ih := cun_tri s s (Nat.le_refl s)\n have e : tri (s + 1) + 0 = (tri s + s) + 1 := by simp only [tri]; try omega\n rw [e]\n simp only [cun, ih, Nat.sub_self, Nat.sub_zero]\n | s + 1, b + 1, h => by\n have ih := cun_tri (s + 1) b (by omega)\n have e : tri (s + 1) + (b + 1) = (tri (s + 1) + b) + 1 := by omega\n rw [e]\n simp only [cun, ih]\n have e2 : s + 1 - b = (s - b) + 1 := by omega\n rw [e2]\n simp only\n have e3 : s + 1 - (b + 1) = s - b := by omega\n rw [e3]\n\ntheorem cun_cpair (a b : Nat) : cun (cpair a b) = (a, b) := by\n unfold cpair\n rw [cun_tri (a + b) b (by omega)]\n simp\n\n@[simp] theorem cun_cpair_fst (a b : Nat) : (cun (cpair a b)).1 = a := by rw [cun_cpair]\n@[simp] theorem cun_cpair_snd (a b : Nat) : (cun (cpair a b)).2 = b := by rw [cun_cpair]\n@[simp] theorem cpair_cun_eq (n : Nat) : cpair (cun n).1 (cun n).2 = n := cpair_cun n\n\ntheorem cpair_ge (a b : Nat) : a + b ≤ cpair a b := by\n unfold cpair; have := le_tri (a + b); omega\n\ntheorem cun_fst_le (n : Nat) : (cun n).1 ≤ n := by\n have h := cpair_ge (cun n).1 (cun n).2; rw [cpair_cun] at h; omega\ntheorem cun_snd_le (n : Nat) : (cun n).2 ≤ n := by\n have h := cpair_ge (cun n).1 (cun n).2; rw [cpair_cun] at h; omega\n\n/-! ### Tuples of naturals -/\n\n/-- The i-th of k naturals packed in q (`tupk`): the first is `(cun q).1`, the rest packed in `(cun q).2`, the last q\nitself. -/\ndef tup (q : Nat) : Nat → Nat → Nat\n | 0, _ => q\n | 1, _ => q\n | _ + 2, 0 => (cun q).1\n | k + 2, i + 1 => tup (cun q).2 (k + 1) i\n\n/-- Naturals packed by `cpair`, right-nested; one is itself. -/\ndef tupk : List Nat → Nat\n | [] => 0\n | [x] => x\n | x :: xs => cpair x (tupk xs)\n\ntheorem tup_le : ∀ (k i q : Nat), tup q k i ≤ q\n | 0, _, _ => Nat.le_refl _\n | 1, _, _ => Nat.le_refl _\n | _ + 2, 0, q => cun_fst_le q\n | k + 2, i + 1, q => Nat.le_trans (tup_le (k + 1) i (cun q).2) (cun_snd_le q)\n\n@[simp] theorem tup_two_zero (k a q : Nat) : tup (cpair a q) (k + 2) 0 = a := by simp [tup]\n@[simp] theorem tup_two_succ (k i a q : Nat) : tup (cpair a q) (k + 2) (i + 1) = tup q (k + 1) i := by simp [tup]\n@[simp] theorem tup_one (q i : Nat) : tup q 1 i = q := rfl\n@[simp] theorem tupk_one (x : Nat) : tupk [x] = x := rfl\n@[simp] theorem tupk_cons (x y : Nat) (ys : List Nat) : tupk (x :: y :: ys) = cpair x (tupk (y :: ys)) := rfl\n\n/-! ### Definitions by well-founded recursion -/\n\n/-- A functional that reads its function only at arguments of lower measure (lexicographic, as the cyclic ranks are\nordered) has exactly one fixpoint: a function satisfying `f x = F f x` at every argument, and every other one equal to\nit. `F g x = F h x` wherever g and h agree below x's measure: a body whose calls (under the path the argument takes)\nare each at a lower measure. -/\ntheorem wf_fix_unique {α β : Type} [Inhabited β] (μ : α → List Nat) (F : (α → β) → α → β)\n (hF : ∀ (g h : α → β) (x : α), (∀ y, lexLt (μ y) (μ x) → g y = h y) → F g x = F h x) :\n ∃ f : α → β, (∀ x, f x = F f x) ∧ ∀ f' : α → β, (∀ x, f' x = F f' x) → f' = f := by\n classical\n have wf : WellFounded (InvImage lexLt μ) := InvImage.wf μ lexLt_wf\n let G : (x : α) → ((y : α) → InvImage lexLt μ y x → β) → β :=\n fun x rec => F (fun y => if h : lexLt (μ y) (μ x) then rec y h else default) x\n have hfix : ∀ x, wf.fix G x = F (wf.fix G) x := by\n intro x\n rw [wf.fix_eq G x]\n apply hF\n intro y hy\n simp [hy]\n refine ⟨wf.fix G, hfix, fun f' hf' => funext fun x => ?_⟩\n exact wf.induction (C := fun x => f' x = wf.fix G x) x (fun x ih => by\n rw [hf' x, hfix x]\n exact hF f' (wf.fix G) x (fun y hy => ih y hy))\n\n/-- A ring of functions calling each other (one per index i, each `α i → β i`), read as one functional F over the ring's\ntuple, that reads each function of the ring only at arguments of lower measure than its own argument's (the measures\nof the ring's functions, one per function, all of one length, compared by `lexLt`), has exactly one fixpoint: a tuple\nsatisfying `f i x = F f i x` for every function and argument, and every other tuple equal to it. core/data.ts takes a\nring that isn't structural recursion once the kernel has proven that each call within the ring, under its path, lowers\nthe callee's measure at the call's arguments below the caller's at its parameters: that is `hF`, each function's body\nreading the ring's functions only at those calls. `wf_fix_unique` is the ring of one. -/\ntheorem wf_fix_unique_ring {ι : Type} {α β : ι → Type} [∀ i, Inhabited (β i)] (μ : (i : ι) → α i → List Nat)\n (F : ((i : ι) → α i → β i) → (i : ι) → α i → β i)\n (hF : ∀ (g h : (i : ι) → α i → β i) (i : ι) (x : α i),\n (∀ (j : ι) (y : α j), lexLt (μ j y) (μ i x) → g j y = h j y) → F g i x = F h i x) :\n ∃ f : (i : ι) → α i → β i, (∀ i x, f i x = F f i x) ∧\n ∀ f' : (i : ι) → α i → β i, (∀ i x, f' i x = F f' i x) → f' = f := by\n classical\n -- the ring's arguments as one type (a function's index and its argument), ordered by the measure there\n let m : (Σ i, α i) → List Nat := fun p => μ p.1 p.2\n have wf : WellFounded (InvImage lexLt m) := InvImage.wf m lexLt_wf\n let G : (p : Σ i, α i) → ((q : Σ i, α i) → InvImage lexLt m q p → β q.1) → β p.1 :=\n fun p rec => F (fun j y => if h : lexLt (μ j y) (μ p.1 p.2) then rec ⟨j, y⟩ h else default) p.1 p.2\n let f : (i : ι) → α i → β i := fun i x => wf.fix G ⟨i, x⟩\n have hfix : ∀ i x, f i x = F f i x := by\n intro i x\n show wf.fix G ⟨i, x⟩ = F f i x\n rw [wf.fix_eq G ⟨i, x⟩]\n apply hF\n intro j y hy\n simp [hy, f]\n refine ⟨f, hfix, fun f' hf' => ?_⟩\n have key : ∀ p : Σ i, α i, f' p.1 p.2 = f p.1 p.2 := fun p =>\n wf.induction (C := fun p => f' p.1 p.2 = f p.1 p.2) p (fun p ih => by\n rw [hf' p.1 p.2, hfix p.1 p.2]\n exact hF f' f p.1 p.2 (fun j y hy => ih ⟨j, y⟩ hy))\n funext i x\n exact key ⟨i, x⟩\n\n/-- A measure of several components for `termination_by`: the tuple, ordered by `lexLt` (an export's measures all have\none length). -/\nstructure Lx where\n l : List Nat\n\ninstance : WellFoundedRelation Lx := ⟨fun a b => lexLt a.l b.l, InvImage.wf Lx.l lexLt_wf⟩\n\n/-! ### Facts at their keys -/\n\n/-- A condition's value as a natural is at most 1 (a function to `Bool` as the encoding reads it). -/\n@[simp] theorem ite_one_zero_le (p : Prop) [Decidable p] : (if p then 1 else 0) ≤ 1 := by\n split <;> omega\n\n/-- A fact read at its own keys reads the values given there. -/\ntheorem override_at (keys : List Nat) (i : Nat) (h : keys.contains i = true) (vals env : Nat → Nat) :\n override keys vals env i = vals i := by\n unfold override; rw [if_pos h]\n\nend Kern\n",
18
+ "Data.lean": "import Cyclic\n\n/-!\nData: recursive datatypes as values, for cyclic proofs. Lean 4.34.0, core only, on top of `Cyclic.lean`.\n\ncore/data.ts proves laws about datatypes (lists, trees, any declared with constructors whose fields are naturals or\nvalues of the datatype) by cyclic proofs over an encoding: every natural is a value of each datatype, a value is read\nthrough maps (its constructor's tag, its fields, its size), a function of several arguments is a map read at their\npairing `pk a b = (a + b)² + b` (left-nested). Nothing here is trusted beyond `Cyclic.lean`: an export defines the\ndatatype as a Lean `inductive` and the functions by pattern matching, gives each map of the encoding its meaning (the\nmodel, through a bijection between the naturals and the datatype's values), proves every fact of the cyclic proof true\nof that model, and concludes the law about the Lean definitions from `cyclic_sound`. What this file gives the exports:\n\n- `pk`, the pairing, injective (`pk_inj`), and a left inverse `unpk` (`unpk_pk`, chosen classically, so no division or\n square root is computed);\n- `cun`, the naturals onto the pairs, in order of their sum (`cun 0 = (0, 0)`, then each pair after the last), with\n `cpair` its inverse (`cun_cpair`, `cpair_cun`): a bijection, the one a datatype's codes are built from, each part at\n most the whole (`cun_fst_le`, `cun_snd_le`) so a value's fields have smaller codes;\n- `tup q k i`, the i-th of k naturals packed in q by `tupk` (`tup_tupk`), at most q (`tup_le`);\n- `wf_fix_unique`: a definition by well-founded recursion has exactly one solution. core/data.ts takes a function whose\n recursion isn't structural once the kernel has proven that each of its calls lowers a measure (a lexicographic tuple\n of naturals, the order of the cyclic ranks, `lexLt`) under the path to the call; its body, read as a functional F of\n the function, then reads it only below the argument's measure, and such an F has one fixpoint. So the function's\n equation, a fact of its proofs, has a model (what they conclude isn't vacuous), and only one: the function as written.\n An export defines such a function with `termination_by` the same measure, `Lx` (a tuple, ordered by `lexLt`) where\n it has several components, and Lean checks the decrease again;\n- `wf_fix_unique_ring`: the same for a ring of functions calling each other (mutual recursion), each with its own\n measure (all of one length): one functional over the ring's tuple of functions, reading each only below the measure\n of the argument it is called at. An export defines the ring in one `mutual` block, each function with `termination_by`\n its measure;\n- `wf_fix_inv_unique_ring` (`wf_fix_inv_unique` its ring of one): nested recursion, a call of the ring in another's\n argument, so a body reads the ring at its own values. An invariant of each function (core/data.ts: how its value's\n size relates to an argument's) is proven with the decrease: each call lowers the measure and each body keeps the\n invariant, wherever the calls the body reads below the measure keep it. The fixpoint is unique and keeps it.\n research/termination/Nested.lean defines two such functions through it (Lean's `termination_by` can't read a call's\n value: core/data.ts's Lean export doesn't write these yet).\n-/\n\nnamespace Kern\n\n/-! ### The pairing the encoding reads several arguments at -/\n\n/-- `(a + b)² + b`: core/dsl.ts's argsKey of two arguments. -/\ndef pk (a b : Nat) : Nat := (a + b) * (a + b) + b\n\ntheorem sq_succ (s : Nat) : (s + 1) * (s + 1) = s * s + 2 * s + 1 := by\n rw [Nat.add_mul, Nat.mul_add, Nat.mul_one, Nat.one_mul]; omega\n\ntheorem pk_lt {a b c d : Nat} (h : a + b < c + d) : pk a b < pk c d := by\n unfold pk\n have h1 : (a + b + 1) * (a + b + 1) ≤ (c + d) * (c + d) := Nat.mul_self_le_mul_self h\n have h2 := sq_succ (a + b)\n omega\n\ntheorem pk_inj {a b c d : Nat} (h : pk a b = pk c d) : a = c ∧ b = d := by\n have hs : a + b = c + d := by\n rcases Nat.lt_trichotomy (a + b) (c + d) with hl | he | hg\n · exact absurd h (Nat.ne_of_lt (pk_lt hl))\n · exact he\n · exact absurd h (Nat.ne_of_gt (pk_lt hg))\n unfold pk at h\n rw [hs] at h\n omega\n\nopen Classical in\n/-- A left inverse of `pk`: the pair a natural is the pairing of, where it is one. -/\nnoncomputable def unpk (n : Nat) : Nat × Nat :=\n if h : ∃ p : Nat × Nat, pk p.1 p.2 = n then Classical.choose h else (0, 0)\n\ntheorem unpk_pk (a b : Nat) : unpk (pk a b) = (a, b) := by\n have h : ∃ p : Nat × Nat, pk p.1 p.2 = pk a b := ⟨(a, b), rfl⟩\n unfold unpk\n split\n · rename_i h'\n have ⟨h1, h2⟩ := pk_inj (Classical.choose_spec h')\n exact Prod.ext h1 h2\n · exact absurd h ‹_›\n\n@[simp] theorem unpk_fst (a b : Nat) : (unpk ((a + b) * (a + b) + b)).1 = a := by\n have := unpk_pk a b; unfold pk at this; rw [this]\n@[simp] theorem unpk_snd (a b : Nat) : (unpk ((a + b) * (a + b) + b)).2 = b := by\n have := unpk_pk a b; unfold pk at this; rw [this]\n\n-- The pairing with a 0 argument, as simp leaves it: (a + 0)² + 0 is a·a, (0 + b)² + b is b·b + b, and (0 + 0)² + 0 is 0.\n@[simp] theorem unpk_sq_fst (a : Nat) : (unpk (a * a)).1 = a := by\n have := unpk_pk a 0; simp only [pk, Nat.add_zero] at this; rw [this]\n@[simp] theorem unpk_sq_snd (a : Nat) : (unpk (a * a)).2 = 0 := by\n have := unpk_pk a 0; simp only [pk, Nat.add_zero] at this; rw [this]\n@[simp] theorem unpk_sqb_fst (b : Nat) : (unpk (b * b + b)).1 = 0 := by\n have := unpk_pk 0 b; simp only [pk, Nat.zero_add] at this; rw [this]\n@[simp] theorem unpk_sqb_snd (b : Nat) : (unpk (b * b + b)).2 = b := by\n have := unpk_pk 0 b; simp only [pk, Nat.zero_add] at this; rw [this]\n@[simp] theorem unpk_zero : unpk 0 = (0, 0) := unpk_pk 0 0\n\n/-! ### A bijection between the naturals and the pairs -/\n\n/-- The naturals onto the pairs, in order of their sum: 0 is (0, 0), and each next natural the pair after. -/\ndef cun : Nat → Nat × Nat\n | 0 => (0, 0)\n | n + 1 => match cun n with\n | (0, b) => (b + 1, 0)\n | (a + 1, b) => (a, b + 1)\n\n/-- 0 + 1 + … + s. -/\ndef tri : Nat → Nat\n | 0 => 0\n | s + 1 => tri s + s + 1\n\n/-- `cun`'s inverse. -/\ndef cpair (a b : Nat) : Nat := tri (a + b) + b\n\ntheorem le_tri : ∀ s, s ≤ tri s\n | 0 => Nat.le_refl 0\n | s + 1 => by have := le_tri s; simp only [tri]; omega\n\ntheorem cpair_succ_left (b : Nat) : cpair (b + 1) 0 = cpair 0 b + 1 := by\n simp only [cpair, Nat.add_zero, Nat.zero_add, tri]\n\ntheorem cpair_succ_right (a b : Nat) : cpair a (b + 1) = cpair (a + 1) b + 1 := by\n simp only [cpair]\n have e : a + (b + 1) = a + 1 + b := by omega\n rw [e]; omega\n\ntheorem cpair_cun : ∀ n, cpair (cun n).1 (cun n).2 = n\n | 0 => rfl\n | n + 1 => by\n have ih := cpair_cun n\n rcases h : cun n with ⟨a, b⟩\n rw [h] at ih\n cases a with\n | zero => simp only [cun, h]; rw [cpair_succ_left]; simp only at ih; omega\n | succ a => simp only [cun, h]; rw [cpair_succ_right]; simp only at ih; omega\n\ntheorem cun_tri : ∀ s b, b ≤ s → cun (tri s + b) = (s - b, b)\n | 0, 0, _ => rfl\n | 0, _ + 1, h => absurd h (by omega)\n | s + 1, 0, _ => by\n have ih := cun_tri s s (Nat.le_refl s)\n have e : tri (s + 1) + 0 = (tri s + s) + 1 := by simp only [tri]; try omega\n rw [e]\n simp only [cun, ih, Nat.sub_self, Nat.sub_zero]\n | s + 1, b + 1, h => by\n have ih := cun_tri (s + 1) b (by omega)\n have e : tri (s + 1) + (b + 1) = (tri (s + 1) + b) + 1 := by omega\n rw [e]\n simp only [cun, ih]\n have e2 : s + 1 - b = (s - b) + 1 := by omega\n rw [e2]\n simp only\n have e3 : s + 1 - (b + 1) = s - b := by omega\n rw [e3]\n\ntheorem cun_cpair (a b : Nat) : cun (cpair a b) = (a, b) := by\n unfold cpair\n rw [cun_tri (a + b) b (by omega)]\n simp\n\n@[simp] theorem cun_cpair_fst (a b : Nat) : (cun (cpair a b)).1 = a := by rw [cun_cpair]\n@[simp] theorem cun_cpair_snd (a b : Nat) : (cun (cpair a b)).2 = b := by rw [cun_cpair]\n@[simp] theorem cpair_cun_eq (n : Nat) : cpair (cun n).1 (cun n).2 = n := cpair_cun n\n\ntheorem cpair_ge (a b : Nat) : a + b ≤ cpair a b := by\n unfold cpair; have := le_tri (a + b); omega\n\ntheorem cun_fst_le (n : Nat) : (cun n).1 ≤ n := by\n have h := cpair_ge (cun n).1 (cun n).2; rw [cpair_cun] at h; omega\ntheorem cun_snd_le (n : Nat) : (cun n).2 ≤ n := by\n have h := cpair_ge (cun n).1 (cun n).2; rw [cpair_cun] at h; omega\n\n/-! ### Tuples of naturals -/\n\n/-- The i-th of k naturals packed in q (`tupk`): the first is `(cun q).1`, the rest packed in `(cun q).2`, the last q\nitself. -/\ndef tup (q : Nat) : Nat → Nat → Nat\n | 0, _ => q\n | 1, _ => q\n | _ + 2, 0 => (cun q).1\n | k + 2, i + 1 => tup (cun q).2 (k + 1) i\n\n/-- Naturals packed by `cpair`, right-nested; one is itself. -/\ndef tupk : List Nat → Nat\n | [] => 0\n | [x] => x\n | x :: xs => cpair x (tupk xs)\n\ntheorem tup_le : ∀ (k i q : Nat), tup q k i ≤ q\n | 0, _, _ => Nat.le_refl _\n | 1, _, _ => Nat.le_refl _\n | _ + 2, 0, q => cun_fst_le q\n | k + 2, i + 1, q => Nat.le_trans (tup_le (k + 1) i (cun q).2) (cun_snd_le q)\n\n@[simp] theorem tup_two_zero (k a q : Nat) : tup (cpair a q) (k + 2) 0 = a := by simp [tup]\n@[simp] theorem tup_two_succ (k i a q : Nat) : tup (cpair a q) (k + 2) (i + 1) = tup q (k + 1) i := by simp [tup]\n@[simp] theorem tup_one (q i : Nat) : tup q 1 i = q := rfl\n@[simp] theorem tupk_one (x : Nat) : tupk [x] = x := rfl\n@[simp] theorem tupk_cons (x y : Nat) (ys : List Nat) : tupk (x :: y :: ys) = cpair x (tupk (y :: ys)) := rfl\n\n/-! ### Definitions by well-founded recursion -/\n\n/-- A functional that reads its function only at arguments of lower measure (lexicographic, as the cyclic ranks are\nordered) has exactly one fixpoint: a function satisfying `f x = F f x` at every argument, and every other one equal to\nit. `F g x = F h x` wherever g and h agree below x's measure: a body whose calls (under the path the argument takes)\nare each at a lower measure. -/\ntheorem wf_fix_unique {α β : Type} [Inhabited β] (μ : α → List Nat) (F : (α → β) → α → β)\n (hF : ∀ (g h : α → β) (x : α), (∀ y, lexLt (μ y) (μ x) → g y = h y) → F g x = F h x) :\n ∃ f : α → β, (∀ x, f x = F f x) ∧ ∀ f' : α → β, (∀ x, f' x = F f' x) → f' = f := by\n classical\n have wf : WellFounded (InvImage lexLt μ) := InvImage.wf μ lexLt_wf\n let G : (x : α) → ((y : α) → InvImage lexLt μ y x → β) → β :=\n fun x rec => F (fun y => if h : lexLt (μ y) (μ x) then rec y h else default) x\n have hfix : ∀ x, wf.fix G x = F (wf.fix G) x := by\n intro x\n rw [wf.fix_eq G x]\n apply hF\n intro y hy\n simp [hy]\n refine ⟨wf.fix G, hfix, fun f' hf' => funext fun x => ?_⟩\n exact wf.induction (C := fun x => f' x = wf.fix G x) x (fun x ih => by\n rw [hf' x, hfix x]\n exact hF f' (wf.fix G) x (fun y hy => ih y hy))\n\n/-- A ring of functions calling each other (one per index i, each `α i → β i`), read as one functional F over the ring's\ntuple, that reads each function of the ring only at arguments of lower measure than its own argument's (the measures\nof the ring's functions, one per function, all of one length, compared by `lexLt`), has exactly one fixpoint: a tuple\nsatisfying `f i x = F f i x` for every function and argument, and every other tuple equal to it. core/data.ts takes a\nring that isn't structural recursion once the kernel has proven that each call within the ring, under its path, lowers\nthe callee's measure at the call's arguments below the caller's at its parameters: that is `hF`, each function's body\nreading the ring's functions only at those calls. `wf_fix_unique` is the ring of one. -/\ntheorem wf_fix_unique_ring {ι : Type} {α β : ι → Type} [∀ i, Inhabited (β i)] (μ : (i : ι) → α i → List Nat)\n (F : ((i : ι) → α i → β i) → (i : ι) → α i → β i)\n (hF : ∀ (g h : (i : ι) → α i → β i) (i : ι) (x : α i),\n (∀ (j : ι) (y : α j), lexLt (μ j y) (μ i x) → g j y = h j y) → F g i x = F h i x) :\n ∃ f : (i : ι) → α i → β i, (∀ i x, f i x = F f i x) ∧\n ∀ f' : (i : ι) → α i → β i, (∀ i x, f' i x = F f' i x) → f' = f := by\n classical\n -- the ring's arguments as one type (a function's index and its argument), ordered by the measure there\n let m : (Σ i, α i) → List Nat := fun p => μ p.1 p.2\n have wf : WellFounded (InvImage lexLt m) := InvImage.wf m lexLt_wf\n let G : (p : Σ i, α i) → ((q : Σ i, α i) → InvImage lexLt m q p → β q.1) → β p.1 :=\n fun p rec => F (fun j y => if h : lexLt (μ j y) (μ p.1 p.2) then rec ⟨j, y⟩ h else default) p.1 p.2\n let f : (i : ι) → α i → β i := fun i x => wf.fix G ⟨i, x⟩\n have hfix : ∀ i x, f i x = F f i x := by\n intro i x\n show wf.fix G ⟨i, x⟩ = F f i x\n rw [wf.fix_eq G ⟨i, x⟩]\n apply hF\n intro j y hy\n simp [hy, f]\n refine ⟨f, hfix, fun f' hf' => ?_⟩\n have key : ∀ p : Σ i, α i, f' p.1 p.2 = f p.1 p.2 := fun p =>\n wf.induction (C := fun p => f' p.1 p.2 = f p.1 p.2) p (fun p ih => by\n rw [hf' p.1 p.2, hfix p.1 p.2]\n exact hF f' f p.1 p.2 (fun j y hy => ih ⟨j, y⟩ hy))\n funext i x\n exact key ⟨i, x⟩\n\n/-- Nested recursion: a ring whose calls may sit in the arguments (or conditions) of others, so a body reads the ring at\nvalues of the ring itself. A functional F such that, wherever g and h agree below x's measure and g keeps the invariant\nI there (`I j y (g j y)`), F g and F h agree at x and F g keeps I at x, has exactly one fixpoint, and it keeps I\neverywhere: the decrease and the invariant proven together, by well-founded induction on the measure. core/data.ts\ntakes such a ring once the kernel has proven, for each call within it (under its path, each call of the ring in its\nargument or condition read as a value r with `lexLt (μ j a) (μ i x) → I j a r`), that it lowers the callee's measure\nbelow the caller's, and that each body, its calls read the same way, keeps its function's invariant: that is `hF`.\n`wf_fix_unique_ring` is the case I = True. -/\ntheorem wf_fix_inv_unique_ring {ι : Type} {α β : ι → Type} [∀ i, Inhabited (β i)] (μ : (i : ι) → α i → List Nat)\n (I : (i : ι) → α i → β i → Prop) (F : ((i : ι) → α i → β i) → (i : ι) → α i → β i)\n (hF : ∀ (g h : (i : ι) → α i → β i) (i : ι) (x : α i),\n (∀ (j : ι) (y : α j), lexLt (μ j y) (μ i x) → g j y = h j y ∧ I j y (g j y)) →\n F g i x = F h i x ∧ I i x (F g i x)) :\n ∃ f : (i : ι) → α i → β i, (∀ i x, f i x = F f i x ∧ I i x (f i x)) ∧\n ∀ f' : (i : ι) → α i → β i, (∀ i x, f' i x = F f' i x) → f' = f := by\n classical\n let m : (Σ i, α i) → List Nat := fun p => μ p.1 p.2\n have wf : WellFounded (InvImage lexLt m) := InvImage.wf m lexLt_wf\n let G : (p : Σ i, α i) → ((q : Σ i, α i) → InvImage lexLt m q p → β q.1) → β p.1 :=\n fun p rec => F (fun j y => if h : lexLt (μ j y) (μ p.1 p.2) then rec ⟨j, y⟩ h else default) p.1 p.2\n let f : (i : ι) → α i → β i := fun i x => wf.fix G ⟨i, x⟩\n -- the equation and the invariant at once, by induction on the measure\n have hfix : ∀ p : Σ i, α i, f p.1 p.2 = F f p.1 p.2 ∧ I p.1 p.2 (f p.1 p.2) := fun p =>\n wf.induction (C := fun p => f p.1 p.2 = F f p.1 p.2 ∧ I p.1 p.2 (f p.1 p.2)) p (fun p ih => by\n have e : f p.1 p.2 = F (fun j y => if lexLt (μ j y) (μ p.1 p.2) then f j y else default) p.1 p.2 := by\n show wf.fix G p = _\n rw [wf.fix_eq G p]\n rfl\n have k := hF (fun j y => if lexLt (μ j y) (μ p.1 p.2) then f j y else default) f p.1 p.2\n (fun j y hy => by rw [if_pos hy]; exact ⟨rfl, (ih ⟨j, y⟩ hy).2⟩)\n rw [e]\n exact ⟨k.1, k.2⟩)\n refine ⟨f, fun i x => hfix ⟨i, x⟩, fun f' hf' => ?_⟩\n have key : ∀ p : Σ i, α i, f' p.1 p.2 = f p.1 p.2 := fun p =>\n wf.induction (C := fun p => f' p.1 p.2 = f p.1 p.2) p (fun p ih => by\n rw [hf' p.1 p.2, (hfix p).1]\n exact (hF f f' p.1 p.2 (fun j y hy => ⟨(ih ⟨j, y⟩ hy).symm, (hfix ⟨j, y⟩).2⟩)).1.symm)\n funext i x\n exact key ⟨i, x⟩\n\n/-- `wf_fix_inv_unique_ring`'s ring of one: a nested recursive function (McCarthy's `m(m(x + 11))`, a sort that sorts\nthe result of sorting) with its invariant. -/\ntheorem wf_fix_inv_unique {α β : Type} [Inhabited β] (μ : α → List Nat) (I : α → β → Prop) (F : (α → β) → α → β)\n (hF : ∀ (g h : α → β) (x : α), (∀ y, lexLt (μ y) (μ x) → g y = h y ∧ I y (g y)) → F g x = F h x ∧ I x (F g x)) :\n ∃ f : α → β, (∀ x, f x = F f x ∧ I x (f x)) ∧ ∀ f' : α → β, (∀ x, f' x = F f' x) → f' = f := by\n obtain ⟨f, hf, hu⟩ := wf_fix_inv_unique_ring (ι := Unit) (α := fun _ => α) (β := fun _ => β) (fun _ => μ) (fun _ => I)\n (fun g _ x => F (g ()) x) (fun g h _ x hy => hF (g ()) (h ()) x (fun y hy' => hy () y hy'))\n refine ⟨f (), fun x => hf () x, fun f' hf' => ?_⟩\n have := hu (fun _ => f') (fun _ x => hf' x)\n exact congrFun this ()\n\n/-- A measure of several components for `termination_by`: the tuple, ordered by `lexLt` (an export's measures all have\none length). -/\nstructure Lx where\n l : List Nat\n\ninstance : WellFoundedRelation Lx := ⟨fun a b => lexLt a.l b.l, InvImage.wf Lx.l lexLt_wf⟩\n\n/-! ### Facts at their keys -/\n\n/-- A condition's value as a natural is at most 1 (a function to `Bool` as the encoding reads it). -/\n@[simp] theorem ite_one_zero_le (p : Prop) [Decidable p] : (if p then 1 else 0) ≤ 1 := by\n split <;> omega\n\n/-- A fact read at its own keys reads the values given there. -/\ntheorem override_at (keys : List Nat) (i : Nat) (h : keys.contains i = true) (vals env : Nat → Nat) :\n override keys vals env i = vals i := by\n unfold override; rw [if_pos h]\n\nend Kern\n",
19
19
  "Rationals.lean": "import Kernel\n/-!\nRationals: linear arithmetic over ℚ, the twin of kernel/kernel.ts's rational section and core/check.ts's derivations\nwithout cuts, with its soundness proof. Lean 4.34.0, core only (core's `Rat`).\n\nThe rows are Cert's (integer coefficients and constant), read at a rational point, each with a strictness: `(r, false)`\nsays r ≤ 0, `(r, true)` says r < 0. A derivation is Cert's steps: an input row, or a positive combination of two\nearlier rows (strict when either is). A cut is refused: it is the one step whose soundness needs an integer point\n(`Cert.cut_sound`), so the naturals' and integers' checks are this one with the cut added. `checkQ` accepts when the\nlast row has only zero coefficients and a positive constant, or a constant ≥ 0 and is strict: Farkas' lemma with\nstrict rows (Motzkin's), the refutation complete for linear arithmetic over ℚ. `checkQ_sound`: no rational point\nsatisfies every input.\n\nTerms (`QE`) are rationals: variables, rational constants, +, − (not truncated), × by a rational constant, min, max,\nif; conditions (`QC`) ≤, <, =, and, or, not. A term's linear form is a Cert row over a positive denominator (`LF`:\nthe term's value is `evalQ row / den`), so every row a leaf reads has integer coefficients and none is rounded. A\nproof is a tree of splits on rows (yes: r ≤ 0, no: −r < 0) with a leaf certificate: the rows it claims (each one of\nthe leaf's rows, by coefficients) and a derivation over them. `sound`: if `checkGoal` accepts, no rational\nenvironment satisfies every hypothesis. `machine_sound`: the induction rule for a step machine whose state, params and\ninputs are rationals.\n-/\n\nnamespace Q\nopen Cert Kern\nset_option linter.unusedSimpArgs false\nset_option linter.unusedSectionVars false\n\n/-! Rows at a rational point. -/\n\ndef dotQ : List (Nat × Int) → (Nat → Rat) → Rat\n | [], _ => 0\n | p :: rest, x => (p.2 : Rat) * x p.1 + dotQ rest x\n\ndef evalQ (r : Row) (x : Nat → Rat) : Rat := dotQ r.coefs x + r.const\n\ntheorem dotQ_app (x : Nat → Rat) : ∀ (a b : List (Nat × Int)), dotQ (a ++ b) x = dotQ a x + dotQ b x\n | [], b => by simp [dotQ, Rat.zero_add]\n | p :: a, b => by simp only [List.cons_append, dotQ, dotQ_app x a b]; grind\n\ntheorem dotQ_addF (x : Nat → Rat) : ∀ (n : Nat) (a b : List (Nat × Int)), dotQ (addF n a b) x = dotQ a x + dotQ b x\n | 0, a, b => by simp only [addF, dotQ_app]\n | _ + 1, [], b => by simp [addF, dotQ, Rat.zero_add]\n | _ + 1, _ :: _, [] => by simp [addF, dotQ, Rat.add_zero]\n | n + 1, p :: as, q :: bs => by\n simp only [addF]\n split\n · simp only [dotQ, dotQ_addF x n as (q :: bs)]; grind\n · split\n · simp only [dotQ, dotQ_addF x n (p :: as) bs]; grind\n · have hpq : p.1 = q.1 := by omega\n split\n · rename_i hz\n simp only [dotQ, dotQ_addF x n as bs, hpq]\n have hz' : (p.2 : Rat) + (q.2 : Rat) = 0 := by rw [← Rat.intCast_add, hz]; rfl\n have : (p.2 : Rat) * x q.1 + (q.2 : Rat) * x q.1 = 0 := by rw [← Rat.add_mul, hz', Rat.zero_mul]\n grind\n · simp only [dotQ, dotQ_addF x n as bs, hpq, Rat.intCast_add, Rat.add_mul]; grind\n\ntheorem dotQ_addL (a b : List (Nat × Int)) (x : Nat → Rat) : dotQ (addL a b) x = dotQ a x + dotQ b x := dotQ_addF x _ a b\n\ntheorem dotQ_map (m : Int) (x : Nat → Rat) : ∀ (a : List (Nat × Int)), dotQ (a.map (fun p => (p.1, m * p.2))) x = m * dotQ a x\n | [] => by simp [dotQ]\n | p :: a => by simp only [List.map_cons, dotQ, dotQ_map m x a, Rat.intCast_mul, Rat.mul_add, Rat.mul_assoc]\n\ntheorem dotQ_scaleL (m : Int) (a : List (Nat × Int)) (x : Nat → Rat) : dotQ (scaleL m a) x = m * dotQ a x := by\n unfold scaleL\n split\n · rename_i h; subst h; simp [dotQ]\n · exact dotQ_map m x a\n\ntheorem dotQ_allZero : ∀ (a : List (Nat × Int)) (x : Nat → Rat), allZero a = true → dotQ a x = 0\n | [], _, _ => rfl\n | p :: a, x, h => by\n simp only [allZero, Bool.and_eq_true, beq_iff_eq] at h\n simp [dotQ, h.1, dotQ_allZero a x h.2, Rat.zero_add]\n\ntheorem evalQ_ladd (a b : Row) (x : Nat → Rat) : evalQ (ladd a b) x = evalQ a x + evalQ b x := by\n simp only [evalQ, ladd, dotQ_addL, Rat.intCast_add]; grind\ntheorem evalQ_lscale (m : Int) (a : Row) (x : Nat → Rat) : evalQ (lscale m a) x = m * evalQ a x := by\n simp only [evalQ, lscale, dotQ_scaleL, Rat.intCast_mul, Rat.mul_add]\ntheorem evalQ_lsub (a b : Row) (x : Nat → Rat) : evalQ (lsub a b) x = evalQ a x - evalQ b x := by\n simp only [lsub, evalQ_ladd, evalQ_lscale]; grind\ntheorem evalQ_lunit (j : Nat) (x : Nat → Rat) : evalQ (lunit j) x = x j := by\n simp [evalQ, lunit, dotQ, Rat.add_zero]\ntheorem evalQ_lconst (n : Int) (x : Nat → Rat) : evalQ (lconst n) x = n := by\n simp [evalQ, lconst, dotQ, Rat.zero_add]\ntheorem same_evalQ {a b : Row} (h : same a b = true) (x : Nat → Rat) : evalQ a x = evalQ b x := by\n simp only [same, Bool.and_eq_true, beq_iff_eq] at h\n have h1 : evalQ (lsub a b) x = 0 := by simp only [evalQ, dotQ_allZero _ x h.1, h.2]; grind\n rw [evalQ_lsub] at h1; grind\n\n/-! Derivations without cuts, over strict and non-strict rows. -/\n\n-- `(r, s)` holds at x: r < 0 if s, else r ≤ 0.\ndef Holds (x : Nat → Rat) (p : Row × Bool) : Prop := if p.2 then evalQ p.1 x < 0 else evalQ p.1 x ≤ 0\n\ntheorem Holds.le {x : Nat → Rat} {p : Row × Bool} (h : Holds x p) : evalQ p.1 x ≤ 0 := by\n unfold Holds at h; split at h\n · exact Rat.le_of_lt h\n · exact h\n\ndef replayQ (inputs : List (Row × Bool)) : List Step → Nat → List (Row × Bool) → Option (List (Row × Bool))\n | [], _, rows => some rows\n | .input i :: ss, n, rows =>\n match inputs[i]? with\n | some r => replayQ inputs ss (n + 1) (r :: rows)\n | none => none\n | .combine a b ma mb :: ss, n, rows =>\n match rows[n - 1 - a]?, rows[n - 1 - b]? with\n | some A, some B => if a < n ∧ b < n ∧ 0 < ma ∧ 0 < mb then replayQ inputs ss (n + 1) ((combineRow ma mb A.1 B.1, A.2 || B.2) :: rows) else none\n | _, _ => none\n | .cut _ _ _ _ :: _, _, _ => none\n\ndef checkQ (inputs : List (Row × Bool)) (steps : List Step) : Bool :=\n match replayQ inputs steps 0 [] with\n | some rows =>\n match rows.head? with\n | some r => allZero r.1.coefs && (decide (0 < r.1.const) || (r.2 && decide (0 ≤ r.1.const)))\n | none => false\n | none => false\n\ntheorem combine_holds {x : Nat → Rat} {ma mb : Int} (hma : 0 < ma) (hmb : 0 < mb) {A B : Row × Bool}\n (hA : Holds x A) (hB : Holds x B) : Holds x (combineRow ma mb A.1 B.1, A.2 || B.2) := by\n have eq : evalQ (combineRow ma mb A.1 B.1) x = (ma : Rat) * evalQ A.1 x + (mb : Rat) * evalQ B.1 x := by\n simp only [evalQ, combineRow, dotQ_addL, dotQ_scaleL, Rat.intCast_add, Rat.intCast_mul]; grind\n have pa : (0 : Rat) < ma := Rat.intCast_pos.mpr hma\n have pb : (0 : Rat) < mb := Rat.intCast_pos.mpr hmb\n have la : (ma : Rat) * evalQ A.1 x ≤ 0 := by\n have := Rat.mul_le_mul_of_nonneg_left hA.le (Rat.le_of_lt pa); simpa using this\n have lb : (mb : Rat) * evalQ B.1 x ≤ 0 := by\n have := Rat.mul_le_mul_of_nonneg_left hB.le (Rat.le_of_lt pb); simpa using this\n unfold Holds\n simp only [eq]\n split\n · rename_i hs\n simp only [Bool.or_eq_true] at hs\n rcases hs with hs | hs\n · have hA' : evalQ A.1 x < 0 := by unfold Holds at hA; simp [hs] at hA; exact hA\n have : (ma : Rat) * evalQ A.1 x < 0 := by\n have := Rat.mul_lt_mul_of_pos_left hA' pa; simpa using this\n grind\n · have hB' : evalQ B.1 x < 0 := by unfold Holds at hB; simp [hs] at hB; exact hB\n have : (mb : Rat) * evalQ B.1 x < 0 := by\n have := Rat.mul_lt_mul_of_pos_left hB' pb; simpa using this\n grind\n · grind\n\n-- Every row a replay produces holds wherever the inputs and the rows so far hold.\ntheorem replayQ_holds (inputs : List (Row × Bool)) (x : Nat → Rat) (hx : ∀ r ∈ inputs, Holds x r) :\n ∀ (ss : List Step) (n : Nat) (rows out : List (Row × Bool)), (∀ r ∈ rows, Holds x r) →\n replayQ inputs ss n rows = some out → ∀ r ∈ out, Holds x r := by\n intro ss\n induction ss with\n | nil =>\n intro n rows out hrows h\n simp only [replayQ, Option.some.injEq] at h\n exact h ▸ hrows\n | cons s ss ih =>\n intro n rows out hrows h\n cases s with\n | input i =>\n simp only [replayQ] at h\n split at h\n · rename_i r hr\n refine ih _ _ out ?_ h\n intro r' hr'\n rcases List.mem_cons.mp hr' with hr' | hr'\n · exact hr' ▸ hx r (List.mem_of_getElem? hr)\n · exact hrows r' hr'\n · exact absurd h (by simp)\n | combine a b ma mb =>\n simp only [replayQ] at h\n split at h\n · rename_i A B hA hB\n split at h\n · rename_i hm\n refine ih _ _ out ?_ h\n intro r' hr'\n rcases List.mem_cons.mp hr' with hr' | hr'\n · subst hr'\n exact combine_holds hm.2.2.1 hm.2.2.2 (hrows A (List.mem_of_getElem? hA)) (hrows B (List.mem_of_getElem? hB))\n · exact hrows r' hr'\n · exact absurd h (by simp)\n · exact absurd h (by simp)\n | cut o g q c => simp [replayQ] at h\n\n/-- A certificate `checkQ` accepts refutes its inputs: no rational point satisfies them all. -/\ntheorem checkQ_sound (inputs : List (Row × Bool)) (steps : List Step) (h : checkQ inputs steps = true) (x : Nat → Rat)\n (hx : ∀ r ∈ inputs, Holds x r) : False := by\n simp only [checkQ] at h\n split at h\n · rename_i rows hrows\n split at h\n · rename_i r hr\n simp only [Bool.and_eq_true, Bool.or_eq_true, decide_eq_true_eq] at h\n have hall := replayQ_holds inputs x hx steps 0 [] rows (by simp) hrows\n have hm : r ∈ rows := by\n cases rows with\n | nil => simp at hr\n | cons a as => simp only [List.head?, Option.some.injEq] at hr; subst hr; exact List.mem_cons_self\n have hh := hall r hm\n have hv : evalQ r.1 x = r.1.const := by simp only [evalQ, dotQ_allZero r.1.coefs x h.1]; grind\n unfold Holds at hh\n rcases h.2 with hc | ⟨hs, hc⟩\n · have hc' : (0 : Rat) < r.1.const := Rat.intCast_pos.mpr hc\n split at hh <;> grind\n · have hc' : (0 : Rat) ≤ r.1.const := Rat.intCast_nonneg.mpr hc\n simp only [hs, ite_true] at hh\n grind\n · exact absurd h (by simp)\n · exact absurd h (by simp)\n\n/-! Terms and conditions over the rationals. -/\n\nmutual\ninductive QE where\n | var (i : Nat)\n | const (q : Rat)\n | add (a b : QE)\n | sub (a b : QE)\n | mul (k : Rat) (a : QE)\n | min (a b : QE)\n | max (a b : QE)\n | ite (c : QC) (t f : QE)\ninductive QC where\n | le (a b : QE)\n | lt (a b : QE)\n | eq (a b : QE)\n | and (a b : QC)\n | or (a b : QC)\n | not (a : QC)\nend\n\nmutual\ndef evalE (ρ : Nat → Rat) : QE → Rat\n | .var i => ρ i\n | .const q => q\n | .add a b => evalE ρ a + evalE ρ b\n | .sub a b => evalE ρ a - evalE ρ b\n | .mul k a => k * evalE ρ a\n | .min a b => if evalE ρ a ≤ evalE ρ b then evalE ρ a else evalE ρ b\n | .max a b => if evalE ρ a ≤ evalE ρ b then evalE ρ b else evalE ρ a\n | .ite c t f => if evalC ρ c then evalE ρ t else evalE ρ f\ndef evalC (ρ : Nat → Rat) : QC → Bool\n | .le a b => decide (evalE ρ a ≤ evalE ρ b)\n | .lt a b => decide (evalE ρ a < evalE ρ b)\n | .eq a b => decide (evalE ρ a = evalE ρ b)\n | .and a b => evalC ρ a && evalC ρ b\n | .or a b => evalC ρ a || evalC ρ b\n | .not a => !evalC ρ a\nend\n\n/-! Linear forms: a row over a positive denominator, the term's value being `evalQ row / den`. -/\n\nstructure LF where\n row : Row\n den : Nat\n\n-- The row of a − b (its value a positive multiple of a's value minus b's).\ndef dif (a b : LF) : Row := lsub (lscale b.den a.row) (lscale a.den b.row)\n\n-- The path: rows decided so far. `(l, true)` says l ≤ 0, `(l, false)` says l > 0.\ndef PathHolds (path : Path) (x : Nat → Rat) : Prop := ∀ a ∈ path, decide (evalQ a.1 x ≤ 0) = a.2\n\n-- Whether the path decides l ≤ 0: a constant row decides itself, else the path's own decision on the same row.\ndef dec (path : Path) (l : Row) : Option Bool :=\n if allZero l.coefs then some (decide (l.const ≤ 0)) else (lookup (fun a => same a.1 l) path).map (·.2)\n\nmutual\ndef linear (path : Path) : QE → Option LF\n | .var i => some ⟨lunit i, 1⟩\n | .const q => some ⟨lconst q.num, q.den⟩\n | .add a b => match linear path a, linear path b with\n | some la, some lb => some ⟨ladd (lscale lb.den la.row) (lscale la.den lb.row), la.den * lb.den⟩\n | _, _ => none\n | .sub a b => match linear path a, linear path b with\n | some la, some lb => some ⟨dif la lb, la.den * lb.den⟩\n | _, _ => none\n | .mul k a => match linear path a with\n | some la => some ⟨lscale k.num la.row, k.den * la.den⟩\n | none => none\n | .min a b => match linear path a, linear path b with\n | some la, some lb => match dec path (dif la lb) with\n | some true => some la\n | some false => some lb\n | none => none\n | _, _ => none\n | .max a b => match linear path a, linear path b with\n | some la, some lb => match dec path (dif la lb) with\n | some true => some lb\n | some false => some la\n | none => none\n | _, _ => none\n | .ite c t f => match truth path c with\n | some true => linear path t\n | some false => linear path f\n | none => none\ndef truth (path : Path) : QC → Option Bool\n | .le a b => match linear path a, linear path b with\n | some la, some lb => dec path (dif la lb)\n | _, _ => none\n | .lt a b => match linear path a, linear path b with\n | some la, some lb => (dec path (dif lb la)).map (!·)\n | _, _ => none\n | .eq a b => match linear path a, linear path b with\n | some la, some lb => match dec path (dif la lb), dec path (dif lb la) with\n | some false, _ => some false\n | _, some false => some false\n | some true, some true => some true\n | _, _ => none\n | _, _ => none\n | .and a b => match truth path a, truth path b with\n | some false, _ => some false\n | _, some false => some false\n | some true, some true => some true\n | _, _ => none\n | .or a b => match truth path a, truth path b with\n | some true, _ => some true\n | _, some true => some true\n | some false, some false => some false\n | _, _ => none\n | .not a => (truth path a).map (!·)\nend\n\n/-! Facts: the rows a hypothesis gives under the path when it has the truth value `pos` (strict ones flagged). -/\n\ndef facts (path : Path) : Bool → QC → List (Row × Bool)\n | pos, .not a => facts path (!pos) a\n | pos, .and a b =>\n if pos then facts path pos a ++ facts path pos b\n else if truth path a = some true then facts path false b\n else if truth path b = some true then facts path false a\n else []\n | pos, .or a b =>\n if !pos then facts path false a ++ facts path false b\n else if truth path a = some false then facts path true b\n else if truth path b = some false then facts path true a\n else []\n | pos, .le a b => match linear path a, linear path b with\n | some la, some lb => if pos then [(dif la lb, false)] else [(dif lb la, true)]\n | _, _ => []\n | pos, .lt a b => match linear path a, linear path b with\n | some la, some lb => if pos then [(dif la lb, true)] else [(dif lb la, false)]\n | _, _ => []\n | pos, .eq a b => match linear path a, linear path b with\n | some la, some lb =>\n if pos then [(dif la lb, false), (dif lb la, false)]\n else if dec path (dif la lb) = some true then [(dif la lb, true)]\n else if dec path (dif lb la) = some true then [(dif lb la, true)]\n else []\n | _, _ => []\n\n/-! Leaves and trees. -/\n\n-- A decided atom as a row: l ≤ 0, or −l < 0.\ndef atomRow (a : Row × Bool) : Row × Bool := if a.2 then (a.1, false) else (lscale (-1) a.1, true)\ndef leafRows (path : Path) (hyps : List QC) : List (Row × Bool) := path.map atomRow ++ hyps.flatMap (facts path true)\n\n-- A claimed row is a leaf row with the same coefficients, as strict as claimed (a strict row may be claimed as ≤).\ndef claimOk (rows : List (Row × Bool)) (r : Row × Bool) : Bool := rows.any fun q => same q.1 r.1 && (q.2 || !r.2)\n\ninductive QTree where\n | split (l : Row) (yes no : QTree)\n | leaf (rows : List (Row × Bool)) (steps : List Step)\n\n-- A split on a row the path already decides is refused (its two sides would read one decision two ways).\ndef checkT (hyps : List QC) : Path → QTree → Bool\n | path, .leaf rows steps => rows.all (claimOk (leafRows path hyps)) && checkQ rows steps\n | path, .split l yes no => (dec path l).isNone && checkT hyps ((l, true) :: path) yes && checkT hyps ((l, false) :: path) no\n\ndef checkGoal (hyps : List QC) (t : QTree) : Bool := checkT hyps [] t\n\n/-! Soundness. -/\n\ntheorem num_eq (q : Rat) : (q.num : Rat) = (q.den : Rat) * q := by\n have h1 := Rat.num_divInt_den q\n rw [Rat.divInt_eq_div] at h1\n have hd : (q.den : Rat) ≠ 0 := by simp [q.den_nz]\n have e : ((q.den : Int) : Rat) = (q.den : Rat) := rfl\n rw [e] at h1\n calc (q.num : Rat) = (q.num : Rat) / q.den * q.den := (Rat.div_mul_cancel hd).symm\n _ = q * q.den := by rw [h1]\n _ = q.den * q := Rat.mul_comm _ _\n\ntheorem mul_nonpos_iff {k t : Rat} (hk : 0 < k) : k * t ≤ 0 ↔ t ≤ 0 := by\n constructor\n · intro h; exact Rat.le_of_mul_le_mul_left (by simpa using h) hk\n · intro h; have := Rat.mul_le_mul_of_nonneg_left h (Rat.le_of_lt hk); simpa using this\ntheorem mul_neg_iff {k t : Rat} (hk : 0 < k) : k * t < 0 ↔ t < 0 := by\n have := Rat.mul_lt_mul_left (a := t) (b := 0) hk; simpa using this\n\n-- l represents the value v at x.\ndef Rep (x : Nat → Rat) (l : LF) (v : Rat) : Prop := 0 < l.den ∧ evalQ l.row x = (l.den : Rat) * v\n\ntheorem dif_eval {x : Nat → Rat} {la lb : LF} {va vb : Rat} (ha : Rep x la va) (hb : Rep x lb vb) :\n evalQ (dif la lb) x = ((la.den : Rat) * lb.den) * (va - vb) := by\n simp only [dif, evalQ_lsub, evalQ_lscale, ha.2, hb.2, Rat.intCast_natCast]; grind\n\ntheorem den_pos {la lb : LF} {va vb : Rat} {x : Nat → Rat} (ha : Rep x la va) (hb : Rep x lb vb) :\n (0 : Rat) < (la.den : Rat) * lb.den :=\n Rat.mul_pos (Rat.natCast_pos.mpr ha.1) (Rat.natCast_pos.mpr hb.1)\n\ntheorem dif_le {x : Nat → Rat} {la lb : LF} {va vb : Rat} (ha : Rep x la va) (hb : Rep x lb vb) :\n evalQ (dif la lb) x ≤ 0 ↔ va ≤ vb := by\n rw [dif_eval ha hb, mul_nonpos_iff (den_pos ha hb)]; grind\ntheorem dif_lt {x : Nat → Rat} {la lb : LF} {va vb : Rat} (ha : Rep x la va) (hb : Rep x lb vb) :\n evalQ (dif la lb) x < 0 ↔ va < vb := by\n rw [dif_eval ha hb, mul_neg_iff (den_pos ha hb)]; grind\n\ntheorem dec_sound {path : Path} {x : Nat → Rat} (hP : PathHolds path x) {l : Row} {b : Bool}\n (h : dec path l = some b) : decide (evalQ l x ≤ 0) = b := by\n simp only [dec] at h\n split at h\n · rename_i hz\n simp only [Option.some.injEq] at h\n subst h\n have hv : evalQ l x = l.const := by simp only [evalQ, dotQ_allZero _ x hz]; grind\n rw [hv]; simp only [decide_eq_decide]\n exact ⟨fun h => Rat.intCast_nonpos.mp h, fun h => Rat.intCast_nonpos.mpr h⟩\n · simp only [Option.map_eq_some_iff] at h\n obtain ⟨a, ha, rfl⟩ := h\n have ⟨hm, hs⟩ := lookup_some ha\n rw [← hP a hm, same_evalQ hs]\n\ntheorem dec_le {path : Path} {x : Nat → Rat} (hP : PathHolds path x) {la lb : LF} {va vb : Rat}\n (ha : Rep x la va) (hb : Rep x lb vb) {b : Bool} (h : dec path (dif la lb) = some b) : decide (va ≤ vb) = b := by\n rw [← dec_sound hP h]; simp only [decide_eq_decide]; exact (dif_le ha hb).symm\n\nsection\nvariable {x : Nat → Rat} {path : Path}\n\nmutual\ntheorem linear_sound (hP : PathHolds path x) : (e : QE) → (l : LF) → linear path e = some l → Rep x l (evalE x e)\n | .var i, l, h => by\n simp only [linear, Option.some.injEq] at h; subst h\n refine ⟨Nat.one_pos, ?_⟩\n simp only [evalQ_lunit, evalE]; grind\n | .const q, l, h => by\n simp only [linear, Option.some.injEq] at h; subst h\n exact ⟨q.den_pos, by simp only [evalQ_lconst, evalE]; exact num_eq q⟩\n | .add a b, l, h => by\n simp only [linear] at h\n split at h\n · rename_i la lb ha hb\n simp only [Option.some.injEq] at h; subst h\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n refine ⟨Nat.mul_pos ea.1 eb.1, ?_⟩\n simp only [evalQ_ladd, evalQ_lscale, ea.2, eb.2, evalE, Rat.intCast_natCast, Rat.natCast_mul]; grind\n · simp at h\n | .sub a b, l, h => by\n simp only [linear] at h\n split at h\n · rename_i la lb ha hb\n simp only [Option.some.injEq] at h; subst h\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n refine ⟨Nat.mul_pos ea.1 eb.1, ?_⟩\n simp only [dif_eval ea eb, evalE, Rat.natCast_mul]\n · simp at h\n | .mul k a, l, h => by\n simp only [linear] at h\n split at h\n · rename_i la ha\n simp only [Option.some.injEq] at h; subst h\n have ea := linear_sound hP a la ha\n refine ⟨Nat.mul_pos k.den_pos ea.1, ?_⟩\n simp only [evalQ_lscale, ea.2, evalE, Rat.natCast_mul, num_eq k]; grind\n · simp at h\n | .min a b, l, h => by\n simp only [linear] at h\n split at h\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n split at h\n · rename_i hd\n simp only [Option.some.injEq] at h; subst h\n have := dec_le hP ea eb hd\n simp only [decide_eq_true_eq] at this\n simp only [evalE, this, ite_true]; exact ea\n · rename_i hd\n simp only [Option.some.injEq] at h; subst h\n have := dec_le hP ea eb hd\n simp only [decide_eq_false_iff_not] at this\n simp only [evalE, this, ite_false]; exact eb\n · simp at h\n · simp at h\n | .max a b, l, h => by\n simp only [linear] at h\n split at h\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n split at h\n · rename_i hd\n simp only [Option.some.injEq] at h; subst h\n have := dec_le hP ea eb hd\n simp only [decide_eq_true_eq] at this\n simp only [evalE, this, ite_true]; exact eb\n · rename_i hd\n simp only [Option.some.injEq] at h; subst h\n have := dec_le hP ea eb hd\n simp only [decide_eq_false_iff_not] at this\n simp only [evalE, this, ite_false]; exact ea\n · simp at h\n · simp at h\n | .ite c t f, l, h => by\n simp only [linear] at h\n split at h\n · rename_i hc\n simp only [evalE, truth_sound hP c true hc, ite_true]; exact linear_sound hP t l h\n · rename_i hc\n simp only [evalE, truth_sound hP c false hc]; exact linear_sound hP f l h\n · simp at h\ntheorem truth_sound (hP : PathHolds path x) : (c : QC) → (b : Bool) → truth path c = some b → evalC x c = b\n | .le a b', r, h => by\n simp only [truth] at h\n split at h\n · rename_i la lb ha hb\n simp only [evalC]; exact dec_le hP (linear_sound hP a la ha) (linear_sound hP b' lb hb) h\n · simp at h\n | .lt a b', r, h => by\n simp only [truth] at h\n split at h\n · rename_i la lb ha hb\n simp only [Option.map_eq_some_iff] at h\n obtain ⟨r', hr, rfl⟩ := h\n have := dec_le hP (linear_sound hP b' lb hb) (linear_sound hP a la ha) hr\n simp only [evalC]; rw [← this]\n cases h' : decide (evalE x b' ≤ evalE x a) <;> simp at h' <;> simp [h'] <;> grind\n · simp at h\n | .eq a b', r, h => by\n simp only [truth] at h\n split at h\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b' lb hb\n simp only [evalC]\n revert h\n rcases h1 : dec path (dif la lb) with _ | _ | _ <;> rcases h2 : dec path (dif lb la) with _ | _ | _ <;>\n intro h <;> simp only [Option.some.injEq, reduceCtorEq] at h <;> subst h <;>\n first\n | (have t1 := dec_le hP ea eb h1; have t2 := dec_le hP eb ea h2\n simp only [decide_eq_true_eq, decide_eq_false_iff_not, decide_eq_decide] at t1 t2 ⊢; grind)\n | (have t1 := dec_le hP ea eb h1\n simp only [decide_eq_true_eq, decide_eq_false_iff_not, decide_eq_decide] at t1 ⊢; grind)\n | (have t2 := dec_le hP eb ea h2\n simp only [decide_eq_true_eq, decide_eq_false_iff_not, decide_eq_decide] at t2 ⊢; grind)\n · simp at h\n | .and a b', r, h => by\n simp only [truth] at h\n simp only [evalC]\n split at h\n · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP a false h1]\n · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP b' false h2]\n · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP a true h1, truth_sound hP b' true h2]\n · simp at h\n | .or a b', r, h => by\n simp only [truth] at h\n simp only [evalC]\n split at h\n · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP a true h1]\n · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP b' true h2]\n · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hP a false h1, truth_sound hP b' false h2]\n · simp at h\n | .not a, r, h => by\n simp only [truth, Option.map_eq_some_iff] at h\n obtain ⟨r', h1, rfl⟩ := h\n simp [evalC, truth_sound hP a r' h1]\nend\n\ntheorem facts_sound (hP : PathHolds path x) : (pos : Bool) → (c : QC) → evalC x c = pos → ∀ r ∈ facts path pos c, Holds x r\n | pos, .not a, h, r, hr => by\n simp only [facts] at hr\n exact facts_sound hP (!pos) a (by simp only [evalC] at h; cases pos <;> simp_all) r hr\n | pos, .and a b, h, r, hr => by\n simp only [facts] at hr\n simp only [evalC] at h\n split at hr\n · rename_i hp; subst hp\n simp only [Bool.and_eq_true] at h\n rcases List.mem_append.mp hr with hr | hr\n · exact facts_sound hP true a h.1 r hr\n · exact facts_sound hP true b h.2 r hr\n · rename_i hp\n simp only [Bool.not_eq_true] at hp; subst hp\n split at hr\n · rename_i ht; have := truth_sound hP a true ht\n exact facts_sound hP false b (by simp_all) r hr\n · split at hr\n · rename_i _ ht; have := truth_sound hP b true ht\n exact facts_sound hP false a (by simp_all) r hr\n · simp at hr\n | pos, .or a b, h, r, hr => by\n simp only [facts] at hr\n simp only [evalC] at h\n split at hr\n · rename_i hp\n simp only [Bool.not_eq_true'] at hp; subst hp\n simp only [Bool.or_eq_false_iff] at h\n rcases List.mem_append.mp hr with hr | hr\n · exact facts_sound hP false a h.1 r hr\n · exact facts_sound hP false b h.2 r hr\n · rename_i hp\n simp only [Bool.not_eq_true', Bool.not_eq_false] at hp; subst hp\n split at hr\n · rename_i ht; have := truth_sound hP a false ht\n exact facts_sound hP true b (by simp_all) r hr\n · split at hr\n · rename_i _ ht; have := truth_sound hP b false ht\n exact facts_sound hP true a (by simp_all) r hr\n · simp at hr\n | pos, .le a b, h, r, hr => by\n simp only [facts] at hr\n split at hr\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n simp only [evalC] at h\n cases pos\n · simp only [Bool.false_eq_true, ite_false, List.mem_singleton] at hr; subst hr\n simp only [decide_eq_false_iff_not] at h\n unfold Holds; simp only [ite_true]; exact (dif_lt eb ea).mpr (by grind)\n · simp only [ite_true, List.mem_singleton] at hr; subst hr\n simp only [decide_eq_true_eq] at h\n unfold Holds; simp only [Bool.false_eq_true, ite_false]; exact (dif_le ea eb).mpr h\n · simp at hr\n | pos, .lt a b, h, r, hr => by\n simp only [facts] at hr\n split at hr\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n simp only [evalC] at h\n cases pos\n · simp only [Bool.false_eq_true, ite_false, List.mem_singleton] at hr; subst hr\n simp only [decide_eq_false_iff_not] at h\n unfold Holds; simp only [Bool.false_eq_true, ite_false]; exact (dif_le eb ea).mpr (by grind)\n · simp only [ite_true, List.mem_singleton] at hr; subst hr\n simp only [decide_eq_true_eq] at h\n unfold Holds; simp only [ite_true]; exact (dif_lt ea eb).mpr h\n · simp at hr\n | pos, .eq a b, h, r, hr => by\n simp only [facts] at hr\n split at hr\n · rename_i la lb ha hb\n have ea := linear_sound hP a la ha\n have eb := linear_sound hP b lb hb\n simp only [evalC] at h\n cases pos\n · simp only [decide_eq_false_iff_not] at h\n simp only [Bool.false_eq_true, ite_false] at hr\n split at hr\n · rename_i hd\n simp only [List.mem_singleton] at hr; subst hr\n have := dec_le hP ea eb hd; simp only [decide_eq_true_eq] at this\n unfold Holds; simp only [ite_true]; exact (dif_lt ea eb).mpr (by grind)\n · split at hr\n · rename_i _ hd\n simp only [List.mem_singleton] at hr; subst hr\n have := dec_le hP eb ea hd; simp only [decide_eq_true_eq] at this\n unfold Holds; simp only [ite_true]; exact (dif_lt eb ea).mpr (by grind)\n · simp at hr\n · simp only [decide_eq_true_eq] at h\n simp only [ite_true, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr\n unfold Holds\n rcases hr with rfl | rfl\n · simp only [Bool.false_eq_true, ite_false]; exact (dif_le ea eb).mpr (by grind)\n · simp only [Bool.false_eq_true, ite_false]; exact (dif_le eb ea).mpr (by grind)\n · simp at hr\n\ntheorem atomRow_holds (hP : PathHolds path x) : ∀ a ∈ path, Holds x (atomRow a) := by\n intro a ha\n have := hP a ha\n unfold atomRow Holds\n cases hb : a.2\n · simp only [hb, decide_eq_false_iff_not] at this\n simp only [Bool.false_eq_true, ite_false, ite_true, evalQ_lscale]; grind\n · simp only [hb, decide_eq_true_eq] at this\n simp only [ite_true, Bool.false_eq_true, ite_false]; exact this\n\ntheorem leafRows_sound (hP : PathHolds path x) {hyps : List QC} (hH : ∀ c ∈ hyps, evalC x c = true) :\n ∀ r ∈ leafRows path hyps, Holds x r := by\n intro r hr\n simp only [leafRows, List.mem_append, List.mem_map, List.mem_flatMap] at hr\n rcases hr with ⟨a, ha, rfl⟩ | ⟨c, hc, hr⟩\n · exact atomRow_holds hP a ha\n · exact facts_sound hP true c (hH c hc) r hr\n\ntheorem claim_holds {rows : List (Row × Bool)} (hrows : ∀ q ∈ rows, Holds x q) {r : Row × Bool}\n (h : claimOk rows r = true) : Holds x r := by\n simp only [claimOk, List.any_eq_true, Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true'] at h\n obtain ⟨q, hq, hs, hst⟩ := h\n have hq' := hrows q hq\n have e := same_evalQ hs x\n unfold Holds at hq' ⊢\n rw [← e]\n rcases hst with h1 | h1\n · simp only [h1, ite_true] at hq'\n split\n · exact hq'\n · exact Rat.le_of_lt hq'\n · simp only [h1, Bool.false_eq_true, ite_false]\n split at hq'\n · exact Rat.le_of_lt hq'\n · exact hq'\nend\n\ntheorem checkT_sound {x : Nat → Rat} {hyps : List QC} (hH : ∀ c ∈ hyps, evalC x c = true) :\n ∀ (t : QTree) (path : Path), PathHolds path x → checkT hyps path t = true → False\n | .leaf rows steps, path, hP, h => by\n simp only [checkT, Bool.and_eq_true, List.all_eq_true] at h\n exact checkQ_sound rows steps h.2 x (fun r hr => claim_holds (leafRows_sound hP hH) (h.1 r hr))\n | .split l yes no, path, hP, h => by\n simp only [checkT, Bool.and_eq_true] at h\n by_cases hl : evalQ l x ≤ 0\n · refine checkT_sound hH yes ((l, true) :: path) ?_ h.1.2\n intro a ha\n rcases List.mem_cons.mp ha with rfl | ha\n · simp [hl]\n · exact hP a ha\n · refine checkT_sound hH no ((l, false) :: path) ?_ h.2\n intro a ha\n rcases List.mem_cons.mp ha with rfl | ha\n · simp [hl]\n · exact hP a ha\n\n/-- The tree check is sound: an accepted goal's hypotheses hold together at no rational point. -/\ntheorem sound (hyps : List QC) (t : QTree) (h : checkGoal hyps t = true) (x : Nat → Rat) :\n ¬ (∀ c ∈ hyps, evalC x c = true) :=\n fun hH => checkT_sound hH t [] (by simp [PathHolds]) h\n\n/-! Step machines over the rationals: the induction rule over these goals, built here in Lean from the machine (as\nKernel.lean's `checkMachine`), so the TypeScript obligations are not trusted. A law holds at the start (`startGoal` is\nrefuted) and one step keeps every law (`stepGoal` is refuted, assuming them all and the request's assumption), so every\nlaw holds in every reachable state. Laws may not read the request's inputs. -/\n\ndef assoc (σ : List (Nat × QE)) (i : Nat) : Option QE := (lookup (fun p => p.1 == i) σ).map (·.2)\n\nmutual\ndef substE (σ : List (Nat × QE)) : QE → QE\n | .var i => match assoc σ i with\n | some e => e\n | none => .var i\n | .const q => .const q\n | .add a b => .add (substE σ a) (substE σ b)\n | .sub a b => .sub (substE σ a) (substE σ b)\n | .mul k a => .mul k (substE σ a)\n | .min a b => .min (substE σ a) (substE σ b)\n | .max a b => .max (substE σ a) (substE σ b)\n | .ite c t f => .ite (substC σ c) (substE σ t) (substE σ f)\ndef substC (σ : List (Nat × QE)) : QC → QC\n | .le a b => .le (substE σ a) (substE σ b)\n | .lt a b => .lt (substE σ a) (substE σ b)\n | .eq a b => .eq (substE σ a) (substE σ b)\n | .and a b => .and (substC σ a) (substC σ b)\n | .or a b => .or (substC σ a) (substC σ b)\n | .not a => .not (substC σ a)\nend\n\n-- The environment after σ: each assigned variable takes its term's value, the rest keep theirs.\ndef applyS (σ : List (Nat × QE)) (ρ : Nat → Rat) : Nat → Rat :=\n fun i => match assoc σ i with\n | some e => evalE ρ e\n | none => ρ i\n\nmutual\ntheorem subst_evalE (σ : List (Nat × QE)) (ρ : Nat → Rat) : (e : QE) → evalE ρ (substE σ e) = evalE (applyS σ ρ) e\n | .var i => by\n simp only [substE]\n cases h : assoc σ i <;> simp [evalE, applyS, h]\n | .const q => by simp [substE, evalE]\n | .add a b => by simp [substE, evalE, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .sub a b => by simp [substE, evalE, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .mul k a => by simp [substE, evalE, subst_evalE σ ρ a]\n | .min a b => by simp [substE, evalE, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .max a b => by simp [substE, evalE, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .ite c t f => by simp [substE, evalE, subst_evalC σ ρ c, subst_evalE σ ρ t, subst_evalE σ ρ f]\ntheorem subst_evalC (σ : List (Nat × QE)) (ρ : Nat → Rat) : (c : QC) → evalC ρ (substC σ c) = evalC (applyS σ ρ) c\n | .le a b => by simp [substC, evalC, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .lt a b => by simp [substC, evalC, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .eq a b => by simp [substC, evalC, subst_evalE σ ρ a, subst_evalE σ ρ b]\n | .and a b => by simp [substC, evalC, subst_evalC σ ρ a, subst_evalC σ ρ b]\n | .or a b => by simp [substC, evalC, subst_evalC σ ρ a, subst_evalC σ ρ b]\n | .not a => by simp [substC, evalC, subst_evalC σ ρ a]\nend\n\nmutual\ndef occE (i : Nat) : QE → Bool\n | .var j => j == i\n | .const _ => false\n | .add a b => occE i a || occE i b\n | .sub a b => occE i a || occE i b\n | .mul _ a => occE i a\n | .min a b => occE i a || occE i b\n | .max a b => occE i a || occE i b\n | .ite c t f => occC i c || occE i t || occE i f\ndef occC (i : Nat) : QC → Bool\n | .le a b => occE i a || occE i b\n | .lt a b => occE i a || occE i b\n | .eq a b => occE i a || occE i b\n | .and a b => occC i a || occC i b\n | .or a b => occC i a || occC i b\n | .not a => occC i a\nend\n\nmutual\ntheorem congrE (ρ ρ' : Nat → Rat) : (e : QE) → (∀ i, occE i e = true → ρ i = ρ' i) → evalE ρ e = evalE ρ' e\n | .var j, h => by simp only [evalE]; exact h j (by simp [occE])\n | .const _, _ => rfl\n | .add a b, h => by\n simp only [evalE, congrE ρ ρ' a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occE, hi]))]\n | .sub a b, h => by\n simp only [evalE, congrE ρ ρ' a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occE, hi]))]\n | .mul k a, h => by simp only [evalE, congrE ρ ρ' a (fun i hi => h i (by simp [occE, hi]))]\n | .min a b, h => by\n simp only [evalE, congrE ρ ρ' a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occE, hi]))]\n | .max a b, h => by\n simp only [evalE, congrE ρ ρ' a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occE, hi]))]\n | .ite c t f, h => by\n simp only [evalE, congrC ρ ρ' c (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' t (fun i hi => h i (by simp [occE, hi])),\n congrE ρ ρ' f (fun i hi => h i (by simp [occE, hi]))]\ntheorem congrC (ρ ρ' : Nat → Rat) : (c : QC) → (∀ i, occC i c = true → ρ i = ρ' i) → evalC ρ c = evalC ρ' c\n | .le a b, h => by\n simp only [evalC, congrE ρ ρ' a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occC, hi]))]\n | .lt a b, h => by\n simp only [evalC, congrE ρ ρ' a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occC, hi]))]\n | .eq a b, h => by\n simp only [evalC, congrE ρ ρ' a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' b (fun i hi => h i (by simp [occC, hi]))]\n | .and a b, h => by\n simp only [evalC, congrC ρ ρ' a (fun i hi => h i (by simp [occC, hi])), congrC ρ ρ' b (fun i hi => h i (by simp [occC, hi]))]\n | .or a b, h => by\n simp only [evalC, congrC ρ ρ' a (fun i hi => h i (by simp [occC, hi])), congrC ρ ρ' b (fun i hi => h i (by simp [occC, hi]))]\n | .not a, h => by simp only [evalC, congrC ρ ρ' a (fun i hi => h i (by simp [occC, hi]))]\nend\n\nstructure Machine where\n inputs : List Nat\n init : List (Nat × QE) -- each state variable's start, over the params\n step : List (Nat × QE) -- each state variable after a request, over params, inputs and state\n pre : QC -- about the params alone: a precondition, so it holds at the start and at every step\n assume : QC -- about each request (and the params); holds at a step, not at the start\n\n-- A request: its inputs replace those variables; nothing else changes until the step.\ndef withInputs (inputs : List Nat) (ρ ι : Nat → Rat) : Nat → Rat := fun i => if inputs.contains i then ι i else ρ i\n\ninductive Reach (m : Machine) : (Nat → Rat) → Prop\n | start (ρ : Nat → Rat) : evalC ρ m.pre = true → Reach m (applyS m.init ρ)\n | step (ρ ι : Nat → Rat) : Reach m ρ → evalC (withInputs m.inputs ρ ι) m.assume = true →\n Reach m (applyS m.step (withInputs m.inputs ρ ι))\n\ndef startGoal (m : Machine) (law : QC) : List QC := [m.pre, .not (substC m.init law)]\ndef stepGoal (m : Machine) (laws : List QC) (law : QC) : List QC := [m.pre, m.assume] ++ laws ++ [.not (substC m.step law)]\n\n-- Per law: the law, then the trees of its start goal and of its step goal.\nabbrev MachineProof := List (QC × QTree × QTree)\n\ndef avoids (vs : List Nat) (c : QC) : Bool := vs.all fun i => !occC i c\n-- The precondition reads only params: no input, no variable the start or a step assigns.\ndef preOk (m : Machine) : Bool := avoids (m.inputs ++ m.init.map (·.1) ++ m.step.map (·.1)) m.pre\n\ndef checkMachine (m : Machine) (ps : MachineProof) : Bool :=\n preOk m && ps.all fun p => avoids m.inputs p.1\n && checkGoal (startGoal m p.1) p.2.1\n && checkGoal (stepGoal m (ps.map (·.1)) p.1) p.2.2\n\ntheorem avoids_occ {vs : List Nat} {c : QC} (h : avoids vs c = true) {i : Nat} (hi : occC i c = true) : i ∉ vs := by\n intro hm\n simp only [avoids, List.all_eq_true] at h\n have := h i hm; simp [hi] at this\n\ntheorem assoc_mem {σ : List (Nat × QE)} {i : Nat} {e : QE} (h : assoc σ i = some e) : i ∈ σ.map (·.1) := by\n simp only [assoc, Option.map_eq_some_iff] at h\n obtain ⟨a, ha, _⟩ := h\n have ⟨hm, hp⟩ := lookup_some ha\n simp only [beq_iff_eq] at hp\n exact List.mem_map.mpr ⟨a, hm, hp⟩\n\ntheorem applyS_other {σ : List (Nat × QE)} {i : Nat} (h : i ∉ σ.map (·.1)) (ρ : Nat → Rat) : applyS σ ρ i = ρ i := by\n simp only [applyS]\n cases ha : assoc σ i with\n | none => rfl\n | some e => exact absurd (assoc_mem ha) h\n\ntheorem withInputs_other {inputs : List Nat} {i : Nat} (h : i ∉ inputs) (ρ ι : Nat → Rat) : withInputs inputs ρ ι i = ρ i := by\n simp only [withInputs]; split\n · rename_i hc; exact absurd (List.contains_iff_mem.mp hc) h\n · rfl\n\ntheorem evalC_avoids {vs : List Nat} {c : QC} (h : avoids vs c = true) {ρ ρ' : Nat → Rat}\n (hρ : ∀ i, i ∉ vs → ρ i = ρ' i) : evalC ρ c = evalC ρ' c :=\n congrC ρ ρ' c (fun i hi => hρ i (avoids_occ h hi))\n\ntheorem pre_reach {m : Machine} (hp : preOk m = true) : ∀ {ρ}, Reach m ρ → evalC ρ m.pre = true := by\n intro ρ hr\n induction hr with\n | start ρ h0 =>\n rw [evalC_avoids hp (ρ' := ρ) (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ)]; exact h0\n | step ρ ι _ _ ih =>\n rw [evalC_avoids hp (ρ' := ρ) (fun i hi => by\n rw [applyS_other (fun h => hi (by simp [h])), withInputs_other (fun h => hi (by simp [h]))])]; exact ih\n\n/-- The induction rule over the rationals: for params that meet the precondition, every law of an accepted machine\nproof holds in every reachable state. -/\ntheorem machine_sound (m : Machine) (ps : MachineProof) (h : checkMachine m ps = true) :\n ∀ ρ, Reach m ρ → ∀ l ∈ ps.map (·.1), evalC ρ l = true := by\n simp only [checkMachine, 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 := sound _ _ (h p hp).1.2 ρ\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [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 | step ρ ι hr ha ih =>\n intro l hl\n obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl\n have hs := sound _ _ (h p hp).2 (withInputs m.inputs ρ ι)\n apply Decidable.byContradiction\n intro hf\n apply hs\n intro c hc\n simp only [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 (ρ' := ρ) (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 (ρ' := ρ) (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\n#print axioms checkQ_sound\n#print axioms sound\n#print axioms machine_sound\n\nend Q\n",
20
20
  "KInd.lean": "import Live\n\n/-!\nKInd: k-induction for machines with sets, proven sound. Lean 4.34.0, core only, on top of `Sets.lean` and `Live.lean`.\n\nA law L (about the state, params and maps, no key) is k-inductive when\n- the base: L holds at the start (`kStartGoal`), and after every run of j steps from the start, 1 ≤ j < k; and\n- the step: after any k steps in a row from a reachable state, each starting where L holds, L holds.\n\nEvery goal but the start's is Sets.lean's k-step goal (`SKStep`, checked by `checkSKStep`, its claim `skstep_sound`):\nLean builds each `when` and the `then` itself from L (`KInd.with`), and only the trees, the instances and the names a\ngoal reads later requests by come from the certificate. The base of depth j reads the first state as the start\n(`startC`: each state variable its start term; a reachable state where they hold is the start's as far as the scalars\ngo, so the claim, made of every reachable state, covers the start) with L there too, and L before each later step; the\nstep reads L before each of its k steps. `then` is L with its state variables and maps renamed to their primed names\n(Live.lean's `primeC`). So `kind_sound`: if the laws' proof checks and the certificate does, L holds in every reachable\nstate, by strong induction on the length of the run that reaches it: a run shorter than k is a base, a longer one is the\nstep from the state k requests back, where L held at each state after by the induction.\n-/\n\nnamespace Kern\nset_option linter.unusedSimpArgs false\nset_option linter.unusedSectionVars false\n\n/-! The certificate -/\n\n-- A conjunction: the condition itself for one, true for none (as kernel.ts's `and` prints).\ndef andC : List C → C\n | [] => trueC\n | [c] => c\n | c :: cs => .and c (andC cs)\n\n-- The start, as a condition: each state variable its start term.\ndef startC (s : SMachine) : C := andC (s.f.m.init.map fun p => .eq (.var p.1) p.2)\n\nstructure KInd where\n law : C\n k : Nat\n prime : List (Nat × Nat) -- a state variable, and the name `then` reads its value after the last step by\n primeMaps : List (Nat × Nat) -- a map, and the name `then` reads it after the last step by\n finsts : List (Nat × E) -- the start goal's fact instances\n pfinsts : List (Nat × E × List E) -- and its instances of the facts about several keys\n start : Cert -- L at the start\n base : List SKStep -- depth j = 1, …, k − 1: its instances, names and tree (Lean sets whens and then)\n step : SKStep -- depth k: k states where L held, then L\n\ndef KInd.then_ (kc : KInd) : C := primeC kc.prime kc.primeMaps kc.law\ndef KInd.with (kc : KInd) (ws : List C) (sl : SKStep) : SKStep :=\n { sl with k := { sl.k with whens := ws, then_ := kc.then_, prime := kc.prime, primeMaps := kc.primeMaps } }\n\n-- The base of depth j: the start with L, then L before each later step.\ndef baseWhens (s : SMachine) (L : C) (j : Nat) : List C := .and (startC s) L :: List.replicate (j - 1) L\ndef kStartGoal (s : SMachine) (kc : KInd) : List C :=\n [s.f.m.pre] ++ factHyps s kc.finsts kc.pfinsts ++ [.not (substRC s.f.m.init (rdStart s.f.m) kc.law)]\n\n-- What L may not read: a primed name, a request's variables, a flag, and a state variable a step writes that has no\n-- primed name; a primed map, a carried map, and a map a step writes that has none.\ndef kindVars (s : SMachine) (kc : KInd) : List Nat :=\n kc.prime.map (·.2) ++ s.extVars ++ (flagVars s.f.flags ++ s.f.m.step.map (·.1)).filter (fun i => !(kc.prime.map (·.1)).contains i)\ndef kindMaps (s : SMachine) (kc : KInd) : List Nat :=\n kc.primeMaps.map (·.2) ++ s.carried ++ (s.f.m.writes.map (·.1) ++ s.every.map (·.1)).filter (fun mp => !(kc.primeMaps.map (·.1)).contains mp)\n-- Nor a name a k-step goal reads a later request by.\ndef kAvoids (kc : KInd) (sl : SKStep) : Bool := avoids (tgts sl.k.ren) kc.law && avoidsMaps (tgts sl.mren) kc.law\n\n-- The start terms read no request variable, no state variable the start sets and no carried map; each variable once.\ndef startOk (s : SMachine) : Bool :=\n distinct (s.f.m.init.map (·.1))\n && s.f.m.init.all (fun p => !s.extVars.contains p.1 && avoidsE (s.extVars ++ s.f.m.init.map (·.1)) p.2 && avoidsMapsE s.carried p.2)\n\ndef checkBase (s : SMachine) (ps : List SEntry) (kc : KInd) : Nat → List SKStep → Bool\n | _, [] => true\n | j, sl :: rest => kAvoids kc sl && checkSKStep s ps (kc.with (baseWhens s kc.law j) sl) && checkBase s ps kc (j + 1) rest\n\ndef checkKInd (s : SMachine) (ps : List SEntry) (kc : KInd) : Bool :=\n 1 ≤ kc.k && kc.base.length + 1 == kc.k\n && distinct (kc.prime.map (·.2)) && distinct (kc.primeMaps.map (·.2))\n && avoids (kindVars s kc) kc.law && avoidsMaps (kindMaps s kc) kc.law\n && startOk s\n && checkIn (kStartGoal s kc) kc.start\n && checkBase s ps kc 1 kc.base\n && kAvoids kc kc.step && checkSKStep s ps (kc.with (List.replicate kc.k kc.law) kc.step)\n\n/-! Runs as lists of requests -/\n\n-- Each request allowed in turn.\ndef SPath (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → Prop\n | _, _, [] => True\n | ρ, M, q :: qs => evalC (sView s ρ M q.1 q.2).1 (sView s ρ M q.1 q.2).2 s.f.m.assume = true\n ∧ SPath s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs\n\ntheorem sEnd_append (s : SMachine) : ∀ (a b : List SRequest) (ρ : Nat → Nat) (M : Nat → Nat → Nat),\n sEnd s ρ M (a ++ b) = sEnd s (sEnd s ρ M a).1 (sEnd s ρ M a).2 b\n | [], _, _, _ => rfl\n | q :: a, b, ρ, M => by simp only [List.cons_append, sEnd]; exact sEnd_append s a b _ _\n\ntheorem spath_append (s : SMachine) : ∀ (a b : List SRequest) (ρ : Nat → Nat) (M : Nat → Nat → Nat),\n SPath s ρ M (a ++ b) ↔ SPath s ρ M a ∧ SPath s (sEnd s ρ M a).1 (sEnd s ρ M a).2 b\n | [], _, _, _ => by simp [SPath, sEnd]\n | q :: a, b, ρ, M => by\n simp only [List.cons_append, SPath, sEnd]\n rw [spath_append s a b _ _]; exact and_assoc.symm\n\ntheorem spath_take {s : SMachine} {ρ : Nat → Nat} {M : Nat → Nat → Nat} {qs : List SRequest} (h : SPath s ρ M qs) (j : Nat) :\n SPath s ρ M (qs.take j) := by\n rw [← List.take_append_drop j qs, spath_append] at h; exact h.1\n\ntheorem sreach_path (s : SMachine) : ∀ (qs : List SRequest) {ρ : Nat → Nat} {M : Nat → Nat → Nat},\n SReach s ρ M → SPath s ρ M qs → SReach s (sEnd s ρ M qs).1 (sEnd s ρ M qs).2\n | [], _, _, hr, _ => hr\n | q :: qs, _, _, hr, hp => sreach_path s qs (SReach.step _ _ _ _ hr hp.1) hp.2\n\n-- A reachable state is the end of a run from a start.\ntheorem sreach_from (s : SMachine) {ρ : Nat → Nat} {M : Nat → Nat → Nat} (h : SReach s ρ M) :\n ∃ ρ0 M0, StartMaps s.f.m M0 ∧ evalC ρ0 M0 s.f.m.pre = true ∧ (∀ l ∈ s.facts, l.Holds ρ0 M0) ∧ s.PFacts ρ0 M0\n ∧ ∃ qs, SPath s (applyS s.f.m.init ρ0 M0) M0 qs ∧ sEnd s (applyS s.f.m.init ρ0 M0) M0 qs = (ρ, M) := by\n induction h with\n | start ρ M hM h0 hf hpf => exact ⟨ρ, M, hM, h0, hf, hpf, [], trivial, rfl⟩\n | step ρ M ι κ _ ha ih =>\n obtain ⟨ρ0, M0, hM, h0, hf, hpf, qs, hp, he⟩ := ih\n refine ⟨ρ0, M0, hM, h0, hf, hpf, qs ++ [(ι, κ)], ?_, ?_⟩\n · rw [spath_append, he]; exact ⟨hp, ha, trivial⟩\n · rw [sEnd_append, he]; rfl\n\n/-! What a run leaves alone -/\n\ntheorem sNext_other (s : SMachine) {i : Nat} (hx : i ∉ s.extVars) (hf : i ∉ flagVars s.f.flags) (hs : i ∉ s.f.m.step.map (·.1))\n (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (sNext s ρ M ι κ).1 i = ρ i := by\n simp only [SMachine.extVars, SMachine.qVars, List.mem_append, not_or] at hx\n simp only [sNext, sView]\n rw [applyS_other hs, decide_other hf, decideQ_other (by simpa [SMachine.qVars] using hx.1.2), withInputs_other hx.1.1]\n\ntheorem sEnd_other (s : SMachine) {i : Nat} (hx : i ∉ s.extVars) (hf : i ∉ flagVars s.f.flags) (hs : i ∉ s.f.m.step.map (·.1)) :\n ∀ (qs : List SRequest) (ρ : Nat → Nat) (M : Nat → Nat → Nat), (sEnd s ρ M qs).1 i = ρ i\n | [], _, _ => rfl\n | q :: qs, ρ, M => by simp only [sEnd]; rw [sEnd_other s hx hf hs qs _ _, sNext_other s hx hf hs]\n\ntheorem sNext_maps_other (s : SMachine) {mp : Nat} (hc : mp ∉ s.carried) (hw : mp ∉ s.f.m.writes.map (·.1)) (he : mp ∉ s.every.map (·.1))\n (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (sNext s ρ M ι κ).2 mp = M mp := by\n funext k\n simp only [sNext, everyMaps]\n rw [lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro h; exact he (by rw [← h]; exact List.mem_map_of_mem hw'))]\n simp only [writeMaps]\n rw [lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro h; exact hw (by rw [← h]; exact List.mem_map_of_mem hw'))]\n simp only [sView, withCarried]\n rw [if_neg (by simpa [List.contains_iff_mem] using hc)]\n\ntheorem sEnd_maps_other (s : SMachine) {mp : Nat} (hc : mp ∉ s.carried) (hw : mp ∉ s.f.m.writes.map (·.1)) (he : mp ∉ s.every.map (·.1)) :\n ∀ (qs : List SRequest) (ρ : Nat → Nat) (M : Nat → Nat → Nat), (sEnd s ρ M qs).2 mp = M mp\n | [], _, _ => rfl\n | q :: qs, ρ, M => by simp only [sEnd]; rw [sEnd_maps_other s hc hw he qs _ _, sNext_maps_other s hc hw he]\n\n-- A condition that reads no request variable and no carried map reads the same in a step's view.\ntheorem view_eval {s : SMachine} {c : C} (hx : avoids s.extVars c = true) (hm : avoidsMaps s.carried c = true)\n (ρ X : Nat → Nat) (M κ : Nat → Nat → Nat) : evalC (withExt s ρ X) (withCarried s M κ) c = evalC ρ M c := by\n rw [congrMC _ _ M c (fun mp hmp => by\n simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using avoidsMaps_occ hm hmp)])]\n exact congrC _ _ _ c (fun i hi => by\n simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using avoids_occ hx hi)])\n\ntheorem andC_true {ρ : Nat → Nat} {M : Nat → Nat → Nat} : ∀ {cs : List C}, (∀ c ∈ cs, evalC ρ M c = true) → evalC ρ M (andC cs) = true\n | [], _ => by simp [andC, trueC, evalC, evalE]\n | [c], h => by simpa [andC] using h c List.mem_cons_self\n | c :: d :: cs, h => by\n simp only [andC, evalC, Bool.and_eq_true]\n exact ⟨h c List.mem_cons_self, andC_true (fun x hx => h x (List.mem_cons_of_mem _ hx))⟩\n\n/-! The parts of the check -/\n\nsection parts\nvariable {s : SMachine} {ps : List SEntry} {kc : KInd}\n\ntheorem kind_parts (h : checkKInd s ps kc = true) :\n 1 ≤ kc.k ∧ kc.base.length + 1 = kc.k ∧ distinct (kc.prime.map (·.2)) = true ∧ distinct (kc.primeMaps.map (·.2)) = true\n ∧ avoids (kindVars s kc) kc.law = true ∧ avoidsMaps (kindMaps s kc) kc.law = true ∧ startOk s = true\n ∧ checkIn (kStartGoal s kc) kc.start = true ∧ checkBase s ps kc 1 kc.base = true\n ∧ kAvoids kc kc.step = true ∧ checkSKStep s ps (kc.with (List.replicate kc.k kc.law) kc.step) = true := by\n simp only [checkKInd, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h\n obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩, h7⟩, h8⟩, h9⟩, h10⟩, h11⟩ := h\n exact ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11⟩\n\ntheorem checkBase_get : ∀ (l : List SKStep) (j : Nat), checkBase s ps kc j l = true → ∀ i (hi : i < l.length),\n kAvoids kc l[i] = true ∧ checkSKStep s ps (kc.with (baseWhens s kc.law (j + i)) l[i]) = true\n | [], _, _, i, hi => absurd hi (Nat.not_lt_zero _)\n | sl :: rest, j, h, i, hi => by\n simp only [checkBase, Bool.and_eq_true] at h\n cases i with\n | zero => exact ⟨h.1.1, by simpa using h.1.2⟩\n | succ i =>\n have := checkBase_get rest (j + 1) h.2 i (by simpa using hi)\n simpa [Nat.add_assoc, Nat.add_comm 1 i] using this\n\n-- L reads none of the request variables and carried maps; outside its primed names, what it reads no step moves.\ntheorem law_reads (h5 : avoids (kindVars s kc) kc.law = true) (h6 : avoidsMaps (kindMaps s kc) kc.law = true) :\n avoids s.extVars kc.law = true ∧ avoidsMaps s.carried kc.law = true\n ∧ (∀ i, occC i kc.law = true → i ∉ kc.prime.map (·.2) ∧ i ∉ s.extVars\n ∧ (i ∈ kc.prime.map (·.1) ∨ (i ∉ flagVars s.f.flags ∧ i ∉ s.f.m.step.map (·.1))))\n ∧ (∀ mp, occMC mp kc.law = true → mp ∉ kc.primeMaps.map (·.2) ∧ mp ∉ s.carried\n ∧ (mp ∈ kc.primeMaps.map (·.1) ∨ (mp ∉ s.f.m.writes.map (·.1) ∧ mp ∉ s.every.map (·.1)))) := by\n have hv : ∀ i, occC i kc.law = true → i ∉ kindVars s kc := fun i hi => avoids_occ h5 hi\n have hm : ∀ mp, occMC mp kc.law = true → mp ∉ kindMaps s kc := fun mp hmp => avoidsMaps_occ h6 hmp\n refine ⟨?_, ?_, fun i hi => ?_, fun mp hmp => ?_⟩\n · simp only [avoids, List.all_eq_true, Bool.not_eq_true']\n intro i hx\n cases hc : occC i kc.law\n · rfl\n · exact absurd (List.mem_append_left _ (List.mem_append_right _ hx)) (hv i hc)\n · simp only [avoidsMaps, List.all_eq_true, Bool.not_eq_true']\n intro mp hx\n cases hc : occMC mp kc.law\n · rfl\n · exact absurd (List.mem_append_left _ (List.mem_append_right _ hx)) (hm mp hc)\n · have h := hv i hi\n simp only [kindVars, List.mem_append, List.mem_filter, not_or, not_and, Bool.not_eq_true', List.contains_iff_mem] at h\n refine ⟨h.1.1, h.1.2, ?_⟩\n by_cases hp : i ∈ kc.prime.map (·.1)\n · exact Or.inl hp\n · right\n have hpf : (kc.prime.map (·.1)).contains i = false := by simpa [List.contains_iff_mem] using hp\n exact ⟨fun hf => by have := h.2 (Or.inl hf); simp_all, fun hs => by have := h.2 (Or.inr hs); simp_all⟩\n · have h := hm mp hmp\n simp only [kindMaps, List.mem_append, List.mem_filter, not_or, not_and, Bool.not_eq_true', List.contains_iff_mem] at h\n refine ⟨h.1.1, h.1.2, ?_⟩\n by_cases hp : mp ∈ kc.primeMaps.map (·.1)\n · exact Or.inl hp\n · right\n have hpf : (kc.primeMaps.map (·.1)).contains mp = false := by simpa [List.contains_iff_mem] using hp\n exact ⟨fun hf => by have := h.2 (Or.inl hf); simp_all, fun hs => by have := h.2 (Or.inr hs); simp_all⟩\n\nend parts\n\n/-! Each part's meaning -/\n\n-- The start goal: L at the start.\ntheorem kind_start (s : SMachine) (ps : List SEntry) (kc : KInd) (hl : checkSMachine s ps = true)\n (hst : checkIn (kStartGoal s kc) kc.start = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat)\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 evalC (applyS s.f.m.init ρ M) M kc.law = true := by\n have hfok := sfacts_ok hl\n apply Decidable.byContradiction; intro hn\n apply sound_in _ _ hst ρ M\n intro c hc\n simp only [kStartGoal, 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 · exact h0\n · exact facthyps_true s kc.finsts kc.pfinsts ρ M (fun f hf => (pfactsOk_parts hfok f hf).2.2) hfa hpf c hc\n · have hrd : ∀ mp k, evalE ρ M (rdStart s.f.m mp k) = M mp (evalE ρ 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 simpa using hn\n\n-- The start meets `startC`, read in a step's view.\ntheorem start_meets (s : SMachine) (hs : startOk s = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (X : Nat → Nat) (κ : Nat → Nat → Nat) :\n evalC (withExt s (applyS s.f.m.init ρ M) X) (withCarried s M κ) (startC s) = true := by\n simp only [startOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at hs\n obtain ⟨hd, hp⟩ := hs\n apply andC_true\n intro c hc\n obtain ⟨p, hpm, rfl⟩ := List.mem_map.mp hc\n obtain ⟨⟨hx, hav⟩, havm⟩ := hp p hpm\n have hx' : p.1 ∉ s.extVars := by simpa [List.contains_iff_mem] using hx\n simp only [evalC]\n apply decide_eq_true\n simp only [evalE]\n have hl : withExt s (applyS s.f.m.init ρ M) X p.1 = evalE ρ M p.2 := by\n simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hx')]\n simp only [applyS, assoc, lookup_unique hd hpm, Option.map_some]\n rw [hl]\n symm\n rw [congrME _ _ M p.2 (fun mp hmp => by\n simp only [withCarried]\n rw [if_neg (by\n simp only [avoidsMapsE, List.all_eq_true, Bool.not_eq_true'] at havm\n intro hc'; have := havm mp (List.contains_iff_mem.mp hc'); simp [hmp] at this)])]\n exact congrE _ _ M p.2 (fun i hi => by\n simp only [avoidsE, List.all_eq_true, Bool.not_eq_true'] at hav\n have hi1 : i ∉ s.extVars := fun h' => by have := hav i (List.mem_append_left _ h'); simp [hi] at this\n have hi2 : i ∉ s.f.m.init.map (·.1) := fun h' => by have := hav i (List.mem_append_right _ h'); simp [hi] at this\n simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi1)]\n exact applyS_other hi2 ρ M)\n\n-- Requests allowed in turn, L before each: the k-step goal's `when`s for L, k times.\ntheorem sallowed_rep (s : SMachine) (L : C) (hx : avoids s.extVars L = true) (hm : avoidsMaps s.carried L = true) :\n ∀ (qs : List SRequest) (ρ : Nat → Nat) (M : Nat → Nat → Nat), SPath s ρ M qs →\n (∀ j < qs.length, evalC (sEnd s ρ M (qs.take j)).1 (sEnd s ρ M (qs.take j)).2 L = true) →\n SAllowed s (List.replicate qs.length L) ρ M qs\n | [], _, _, _, _ => by simp [SAllowed]\n | q :: qs, ρ, M, hp, hL => by\n simp only [List.length_cons, List.replicate_succ, SAllowed]\n refine ⟨hp.1, ?_, sallowed_rep s L hx hm qs _ _ hp.2 (fun j hj => ?_)⟩\n · rw [view_eval hx hm]; exact hL 0 (by simp)\n · have := hL (j + 1) (by simp; omega)\n simpa [List.take_succ_cons, sEnd] using this\n\n-- A k-step goal Lean built from L: L after its steps.\ntheorem kind_then (s : SMachine) (kc : KInd) (ws : List C) (sl : SKStep) (h : SKStepHolds s (kc.with ws sl))\n (h3 : distinct (kc.prime.map (·.2)) = true) (h4 : distinct (kc.primeMaps.map (·.2)) = true)\n (h5 : avoids (kindVars s kc) kc.law = true) (h6 : avoidsMaps (kindMaps s kc) kc.law = true) (hav : kAvoids kc sl = true)\n {ρ : Nat → Nat} {M : Nat → Nat → Nat} (hr : SReach s ρ M) (q₁ : SRequest) (qs : List SRequest)\n (hlen : qs.length + 1 = ws.length) (hA : SAllowed s ws ρ M (q₁ :: qs)) :\n evalC (sEnd s ρ M (q₁ :: qs)).1 (sEnd s ρ M (q₁ :: qs)).2 kc.law = true := by\n have hh := h ρ M hr q₁ qs hlen hA\n simp only [KInd.with, KInd.then_] at hh\n obtain ⟨_, _, hv, hmv⟩ := law_reads h5 h6\n simp only [kAvoids, Bool.and_eq_true] at hav\n rw [primeC_eval kc.prime kc.primeMaps h3 h4 _ _ _ _ kc.law (fun i hi => ?_) (fun mp hmp => ?_)] at hh\n · exact hh\n · obtain ⟨hpt, hx, hor⟩ := hv i hi\n refine ⟨hpt, hor.imp id (fun hn => ?_)⟩\n rw [sEnd_other s hx hn.1 hn.2, renEnv_other _ _ _ i (avoids_occ hav.1 hi)]\n simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hx)]\n · obtain ⟨hpt, hc, hor⟩ := hmv mp hmp\n refine ⟨hpt, hor.imp id (fun hn => ?_)⟩\n rw [sEnd_maps_other s hc hn.1 hn.2, renMaps_other _ _ _ mp (avoidsMaps_occ hav.2 hmp)]\n simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hc)]\n\n/-! k-induction -/\n\n/-- k-induction: if the laws' proof checks and a k-induction certificate for L does (its start goal, the k-step goals\nof its base from the start for each depth 1 ≤ j < k, and its step over k steps, every `when` and `then` built by Lean\nfrom L), then L holds in every reachable state. -/\ntheorem kind_sound (s : SMachine) (ps : List SEntry) (kc : KInd) (hl : checkSMachine s ps = true) (hk : checkKInd s ps kc = true) :\n ∀ ρ M, SReach s ρ M → evalC ρ M kc.law = true := by\n obtain ⟨h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11⟩ := kind_parts hk\n obtain ⟨hx, hm, _, _⟩ := law_reads h5 h6\n intro ρ M hr\n obtain ⟨ρ0, M0, hM0, hp0, hf0, hpf0, qs, hpath, hend⟩ := sreach_from s hr\n have hrs : SReach s (applyS s.f.m.init ρ0 M0) M0 := SReach.start ρ0 M0 hM0 hp0 hf0 hpf0\n suffices hall : ∀ n, ∀ qs : List SRequest, qs.length = n → SPath s (applyS s.f.m.init ρ0 M0) M0 qs →\n evalC (sEnd s (applyS s.f.m.init ρ0 M0) M0 qs).1 (sEnd s (applyS s.f.m.init ρ0 M0) M0 qs).2 kc.law = true by\n have := hall qs.length qs rfl hpath\n rw [hend] at this; exact this\n intro n\n induction n using Nat.strongRecOn with\n | ind n ih =>\n intro qs hn hp\n -- L at every shorter run's end, and so at each state along this one.\n have ihp : ∀ j < n, evalC (sEnd s (applyS s.f.m.init ρ0 M0) M0 (qs.take j)).1 (sEnd s (applyS s.f.m.init ρ0 M0) M0 (qs.take j)).2 kc.law = true :=\n fun j hj => ih j hj _ (by rw [List.length_take]; omega) (spath_take hp j)\n by_cases h0 : n = 0\n · -- The start.\n subst h0\n rw [List.length_eq_zero_iff.mp hn]\n exact kind_start s ps kc hl h8 ρ0 M0 hM0 hp0 hf0 hpf0\n by_cases hlt : n < kc.k\n · -- A base: the run from the start, of depth n.\n obtain ⟨q₁, rest, rfl⟩ : ∃ q₁ rest, qs = q₁ :: rest := by\n cases qs with\n | nil => simp at hn; omega\n | cons q rest => exact ⟨q, rest, rfl⟩\n have hi : n - 1 < kc.base.length := by omega\n obtain ⟨hav, hc⟩ := checkBase_get kc.base 1 h9 (n - 1) hi\n rw [show 1 + (n - 1) = n by omega] at hc\n have hs := skstep_sound s ps _ hl hc\n simp only [List.length_cons] at hn\n refine kind_then s kc _ _ hs h3 h4 h5 h6 hav hrs q₁ rest (by simp [baseWhens]; omega) ?_\n simp only [baseWhens, SAllowed]\n refine ⟨hp.1, ?_, ?_⟩\n · simp only [evalC, Bool.and_eq_true]\n refine ⟨start_meets s h7 ρ0 M0 _ _, ?_⟩\n rw [view_eval hx hm]; exact ihp 0 (by omega)\n · rw [show n - 1 = rest.length by omega]\n exact sallowed_rep s kc.law hx hm rest _ _ hp.2 (fun j hj => by\n have := ihp (j + 1) (by omega)\n simpa [List.take_succ_cons, sEnd] using this)\n · -- The step: the state k requests back, and L at each state after it.\n have hkn : kc.k ≤ n := by omega\n let pre := qs.take (n - kc.k)\n let post := qs.drop (n - kc.k)\n have hsplit : pre ++ post = qs := List.take_append_drop _ _\n have hpost : post.length = kc.k := by simp only [post, List.length_drop]; omega\n have hp' := hp\n rw [← hsplit, spath_append] at hp'\n have hr' := sreach_path s pre hrs hp'.1\n obtain ⟨q₁, rest, hqr⟩ : ∃ q₁ rest, post = q₁ :: rest := by\n cases h : post with\n | nil => rw [h] at hpost; simp at hpost; omega\n | cons q rest => exact ⟨q, rest, rfl⟩\n have hs := skstep_sound s ps _ hl h11\n have hA : SAllowed s (List.replicate kc.k kc.law) (sEnd s (applyS s.f.m.init ρ0 M0) M0 pre).1 (sEnd s (applyS s.f.m.init ρ0 M0) M0 pre).2 post := by\n rw [← hpost]\n refine sallowed_rep s kc.law hx hm post _ _ hp'.2 (fun j hj => ?_)\n rw [← sEnd_append]\n have hpre : pre.length = n - kc.k := by simp only [pre, List.length_take]; omega\n have := ih (n - kc.k + j) (by omega) (pre ++ post.take j) (by simp [hpre, List.length_take]; omega)\n ((spath_append s _ _ _ _).mpr ⟨hp'.1, spath_take hp'.2 j⟩)\n exact this\n rw [hqr] at hA\n have := kind_then s kc _ _ hs h3 h4 h5 h6 h10 hr' q₁ rest (by rw [List.length_replicate, ← hpost, hqr]; simp) hA\n rw [← hqr, ← sEnd_append, hsplit] at this\n exact this\n\nend Kern\n\n#print axioms Kern.kind_sound\n",
21
21
  };