gdp-wired 0.51.0 → 0.52.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 (69) hide show
  1. package/README.md +2 -2
  2. package/dist/core/accel-refute.d.ts +53 -0
  3. package/dist/core/accel-refute.js +997 -0
  4. package/dist/core/cli.d.ts +2 -0
  5. package/dist/core/cli.js +8 -1
  6. package/dist/core/cycles.d.ts +2 -0
  7. package/dist/core/cycles.js +12 -9
  8. package/dist/core/dsl.d.ts +7 -1
  9. package/dist/core/dsl.js +147 -26
  10. package/dist/core/live-support.d.ts +5 -0
  11. package/dist/core/live-support.js +148 -0
  12. package/dist/core/live.js +194 -37
  13. package/dist/core/lockstep.d.ts +5 -0
  14. package/dist/core/lockstep.js +23 -0
  15. package/dist/core/ordered-pair-seeds.d.ts +2 -0
  16. package/dist/core/ordered-pair-seeds.js +109 -0
  17. package/dist/core/proof-output.d.ts +38 -1
  18. package/dist/core/proof-output.js +38 -0
  19. package/dist/core/search-termination.d.ts +4 -0
  20. package/dist/core/search-termination.js +1 -1
  21. package/dist/core/source-check.d.ts +4 -1
  22. package/dist/core/source-check.js +5 -2
  23. package/dist/core/sparse-kind-proof.d.ts +7 -0
  24. package/dist/core/sparse-kind-proof.js +49 -0
  25. package/dist/core/step-seeds.d.ts +46 -0
  26. package/dist/core/step-seeds.js +316 -0
  27. package/dist/lean-kernel/Cert.lean +222 -0
  28. package/dist/lean-kernel/Count.lean +1568 -0
  29. package/dist/lean-kernel/Ghost.lean +373 -0
  30. package/dist/lean-kernel/GhostMap.lean +426 -0
  31. package/dist/lean-kernel/Kernel.lean +2881 -0
  32. package/dist/lean-kernel/Live.lean +1168 -0
  33. package/dist/lean-kernel/Progress.lean +543 -0
  34. package/dist/lean-kernel/Sets.lean +1975 -0
  35. package/dist/lean-kernel/StepLaw.lean +128 -0
  36. package/dist/lean-kernel/Steps.lean +477 -0
  37. package/dist/lean-kernel/Sums.lean +916 -0
  38. package/dist/lean-kernel/emit.d.ts +4 -0
  39. package/dist/lean-kernel/emit.js +23 -4
  40. package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
  41. package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
  42. package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
  43. package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
  44. package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
  45. package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
  46. package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
  47. package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
  48. package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
  49. package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
  50. package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
  51. package/dist/lean-kernel/lean-aggregate/index.js +52 -0
  52. package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
  53. package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
  54. package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
  55. package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
  56. package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
  57. package/dist/lean-kernel/lean-aggregate/source.js +141 -0
  58. package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
  59. package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
  60. package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
  61. package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
  62. package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
  63. package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
  64. package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
  65. package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
  66. package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
  67. package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
  68. package/docs/lean-aggregate.md +29 -0
  69. package/package.json +6 -1
@@ -0,0 +1,543 @@
1
+ import Kernel
2
+ /-!
3
+ Progress: flags and "some request makes this happen", the two parts of kernel.ts that Kernel.lean left to TypeScript.
4
+
5
+ Flags. A step may name conditions (kernel.ts's `step.flags`), decided once per step from the request and the state
6
+ before it. Step terms read flag f as `.eq (.var f) (.const 1)`; the flag's variable holds 1 when its condition holds
7
+ and 0 when it doesn't (`decideFlags`). The exporter passes flags through untouched: Lean inlines them itself (`inlineC`
8
+ turns each `.eq (.var f) (.const 1)` into the condition, `inlineE` any other read of f into `ite cond 1 0`, exactly what
9
+ kernel.ts's goal builder does) and proves inlining keeps the meaning (`inline_evalE`, `inline_evalC`). A machine with
10
+ flags reaches, outside its flag variables, exactly the states its inlined machine reaches (`freach_plain`,
11
+ `fmreach_plain`), so the induction rules of Kernel.lean carry over (`fmachine_sound`, `fmmachine_sound`).
12
+
13
+ Progress. "In every reachable state where `when` holds, some request makes `then` true of its step": the request is
14
+ named (a term per input over the state and params), and Lean builds kernel.ts's two goals itself: the request is allowed
15
+ (the machine's assumption holds of it), and the step it causes makes `then` hold. `then` reads the state before the step
16
+ by plain names and after it by primed names (`prime`, `primeMaps`). `progress_sound`: for every reachable state where
17
+ `when` holds, the named request is a step the machine allows, and `then` holds of it.
18
+ -/
19
+ namespace Kern
20
+ open Cert
21
+
22
+ /-! Flags -/
23
+
24
+ def flagOf (fl : List (Nat × C)) (i : Nat) : Option C := (lookup (fun p => p.1 == i) fl).map (·.2)
25
+ def flagSub (fl : List (Nat × C)) : List (Nat × E) := fl.map fun p => (p.1, .ite p.2 (.const 1) (.const 0))
26
+ def flagVars (fl : List (Nat × C)) : List Nat := fl.map (·.1)
27
+ -- The state with every flag decided: flag f holds 1 when its condition holds before the step, else 0.
28
+ def decideFlags (fl : List (Nat × C)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) : Nat → Nat := applyS (flagSub fl) ρ maps
29
+
30
+ -- A read of flag f as a condition becomes the condition itself.
31
+ def eqOrFlag (fl : List (Nat × C)) (a b a' b' : E) : C :=
32
+ match a, b with
33
+ | .var i, .const 1 => match flagOf fl i with
34
+ | some c => c
35
+ | none => .eq a' b'
36
+ | _, _ => .eq a' b'
37
+
38
+ mutual
39
+ def inlineE (fl : List (Nat × C)) : E → E
40
+ | .var i => match flagOf fl i with
41
+ | some c => .ite c (.const 1) (.const 0)
42
+ | none => .var i
43
+ | .const n => .const n
44
+ | .add a b => .add (inlineE fl a) (inlineE fl b)
45
+ | .sub a b => .sub (inlineE fl a) (inlineE fl b)
46
+ | .mul k a => .mul k (inlineE fl a)
47
+ | .min a b => .min (inlineE fl a) (inlineE fl b)
48
+ | .max a b => .max (inlineE fl a) (inlineE fl b)
49
+ | .prod a b => .prod (inlineE fl a) (inlineE fl b)
50
+ | .ite c t f => .ite (inlineC fl c) (inlineE fl t) (inlineE fl f)
51
+ | .get m key => .get m (inlineE fl key)
52
+ | .tup a b => .tup (inlineE fl a) (inlineE fl b)
53
+ | .proj k a => .proj k (inlineE fl a)
54
+ def inlineC (fl : List (Nat × C)) : C → C
55
+ | .le a b => .le (inlineE fl a) (inlineE fl b)
56
+ | .lt a b => .lt (inlineE fl a) (inlineE fl b)
57
+ | .eq a b => eqOrFlag fl a b (inlineE fl a) (inlineE fl b)
58
+ | .and a b => .and (inlineC fl a) (inlineC fl b)
59
+ | .or a b => .or (inlineC fl a) (inlineC fl b)
60
+ | .not a => .not (inlineC fl a)
61
+ end
62
+
63
+ theorem lookup_map_same {α : Type} (p : α → Bool) (g : α → α) (hg : ∀ a, p (g a) = p a) :
64
+ ∀ l : List α, lookup p (l.map g) = (lookup p l).map g
65
+ | [] => rfl
66
+ | a :: as => by
67
+ simp only [List.map_cons, lookup, hg]
68
+ split <;> simp [lookup_map_same p g hg as]
69
+
70
+ theorem lookup_map_pred {α β : Type} (p : β → Bool) (g : α → β) :
71
+ ∀ l : List α, lookup p (l.map g) = (lookup (fun a => p (g a)) l).map g
72
+ | [] => rfl
73
+ | a :: as => by
74
+ simp only [List.map_cons, lookup]
75
+ split <;> simp [lookup_map_pred p g as]
76
+
77
+ theorem lookup_map_fst {β γ : Type} (i : Nat) (g : β → γ) :
78
+ ∀ l : List (Nat × β), lookup (fun q => q.1 == i) (l.map fun q => (q.1, g q.2)) = (lookup (fun q => q.1 == i) l).map fun q => (q.1, g q.2)
79
+ | [] => rfl
80
+ | a :: as => by
81
+ simp only [List.map_cons, lookup]
82
+ split <;> simp [lookup_map_fst i g as]
83
+
84
+ theorem assoc_flagSub (fl : List (Nat × C)) (i : Nat) :
85
+ assoc (flagSub fl) i = (flagOf fl i).map (fun c => .ite c (.const 1) (.const 0)) := by
86
+ simp only [assoc, flagSub, flagOf, lookup_map_pred, Option.map_map]; rfl
87
+
88
+ theorem assoc_map_snd (σ : List (Nat × E)) (g : E → E) (i : Nat) :
89
+ assoc (σ.map fun p => (p.1, g p.2)) i = (assoc σ i).map g := by
90
+ simp only [assoc, lookup_map_pred, Option.map_map]; rfl
91
+
92
+ mutual
93
+ theorem inline_evalE (fl : List (Nat × C)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
94
+ (e : E) → evalE ρ maps (inlineE fl e) = evalE (decideFlags fl ρ maps) maps e
95
+ | .var i => by
96
+ cases h : flagOf fl i <;> simp [inlineE, h, evalE, decideFlags, applyS, assoc_flagSub]
97
+ | .const n => by simp [inlineE, evalE]
98
+ | .add a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
99
+ | .sub a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
100
+ | .mul k a => by simp [inlineE, evalE, inline_evalE fl ρ maps a]
101
+ | .min a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
102
+ | .max a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
103
+ | .prod a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
104
+ | .ite c t f => by simp [inlineE, evalE, inline_evalC fl ρ maps c, inline_evalE fl ρ maps t, inline_evalE fl ρ maps f]
105
+ | .get m key => by simp [inlineE, evalE, inline_evalE fl ρ maps key]
106
+ | .tup a b => by simp [inlineE, evalE, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
107
+ | .proj k a => by simp [inlineE, evalE, inline_evalE fl ρ maps a]
108
+ theorem inline_evalC (fl : List (Nat × C)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
109
+ (c : C) → evalC ρ maps (inlineC fl c) = evalC (decideFlags fl ρ maps) maps c
110
+ | .le a b => by simp [inlineC, evalC, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
111
+ | .lt a b => by simp [inlineC, evalC, inline_evalE fl ρ maps a, inline_evalE fl ρ maps b]
112
+ | .eq a b => by
113
+ have ha := inline_evalE fl ρ maps a
114
+ have hb := inline_evalE fl ρ maps b
115
+ simp only [inlineC, eqOrFlag]
116
+ split
117
+ · rename_i i
118
+ cases h : flagOf fl i with
119
+ | none => simp only [evalC, ha, hb]
120
+ | some c =>
121
+ simp only [evalC, evalE, decideFlags, applyS, assoc_flagSub, h, Option.map_some]
122
+ cases evalC ρ maps c <;> simp
123
+ · simp only [evalC, ha, hb]
124
+ | .and a b => by simp [inlineC, evalC, inline_evalC fl ρ maps a, inline_evalC fl ρ maps b]
125
+ | .or a b => by simp [inlineC, evalC, inline_evalC fl ρ maps a, inline_evalC fl ρ maps b]
126
+ | .not a => by simp [inlineC, evalC, inline_evalC fl ρ maps a]
127
+ end
128
+
129
+ -- Flag conditions read the request and the state, never a flag.
130
+ def flagsOk (fl : List (Nat × C)) : Bool := fl.all fun p => avoids (flagVars fl) p.2
131
+
132
+ -- A term that reads none of these variables.
133
+ def avoidsE (vs : List Nat) (e : E) : Bool := vs.all fun i => !occE i e
134
+
135
+ theorem avoidsE_occ {vs : List Nat} {e : E} (h : avoidsE vs e = true) {i : Nat} (hi : occE i e = true) : i ∉ vs := by
136
+ intro hm
137
+ simp only [avoidsE, List.all_eq_true] at h
138
+ have := h i hm; simp [hi] at this
139
+
140
+ -- Two states that agree outside some variables.
141
+ def Agree (vs : List Nat) (ρ ρ' : Nat → Nat) : Prop := ∀ i, i ∉ vs → ρ i = ρ' i
142
+
143
+ theorem flagOf_none {fl : List (Nat × C)} {i : Nat} (h : flagOf fl i = none) : i ∉ flagVars fl := by
144
+ intro hm
145
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hm
146
+ simp only [flagOf, Option.map_eq_none_iff] at h
147
+ have := lookup_none_all h p hp
148
+ simp at this
149
+
150
+ theorem flagOf_some {fl : List (Nat × C)} {i : Nat} {c : C} (h : flagOf fl i = some c) : ∃ p ∈ fl, p.2 = c := by
151
+ simp only [flagOf, Option.map_eq_some_iff] at h
152
+ obtain ⟨p, hp, rfl⟩ := h
153
+ exact ⟨p, (lookup_some hp).1, rfl⟩
154
+
155
+ theorem decide_other {fl : List (Nat × C)} {i : Nat} (hi : i ∉ flagVars fl) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
156
+ decideFlags fl ρ maps i = ρ i :=
157
+ applyS_other (by simpa [flagSub, flagVars] using hi) ρ maps
158
+
159
+ -- States that agree outside the flags decide the flags alike, so they agree everywhere once the flags are decided.
160
+ theorem decide_agree {fl : List (Nat × C)} (hf : flagsOk fl = true) {X X' : Nat → Nat} (maps : Nat → Nat → Nat)
161
+ (h : Agree (flagVars fl) X X') : decideFlags fl X maps = decideFlags fl X' maps := by
162
+ funext i
163
+ simp only [decideFlags, applyS, assoc_flagSub]
164
+ cases hc : flagOf fl i with
165
+ | none => exact h i (flagOf_none hc)
166
+ | some c =>
167
+ obtain ⟨p, hp, rfl⟩ := flagOf_some hc
168
+ simp only [flagsOk, List.all_eq_true] at hf
169
+ have hav := hf p hp
170
+ simp only [Option.map_some, evalE]
171
+ rw [congrC X X' maps p.2 (fun j hj => h j (avoids_occ hav hj))]
172
+
173
+ theorem withInputs_agree {vs ins : List Nat} {ρ ρ' : Nat → Nat} (h : Agree vs ρ ρ') (ι : Nat → Nat) :
174
+ Agree vs (withInputs ins ρ ι) (withInputs ins ρ' ι) := by
175
+ intro i hi; simp only [withInputs]; split
176
+ · rfl
177
+ · exact h i hi
178
+
179
+ /-! Scalar machines with flags -/
180
+
181
+ structure FMachine where
182
+ m : Machine -- step terms and the assumption may read flags
183
+ flags : List (Nat × C) -- each flag's variable and its condition
184
+
185
+ def FMachine.plain (f : FMachine) : Machine :=
186
+ { f.m with step := f.m.step.map (fun p => (p.1, inlineE f.flags p.2)), assume := inlineC f.flags f.m.assume }
187
+
188
+ inductive FReach (f : FMachine) (maps : Nat → Nat → Nat) : (Nat → Nat) → Prop
189
+ | start (ρ : Nat → Nat) : evalC ρ maps f.m.pre = true → FReach f maps (applyS f.m.init ρ maps)
190
+ | step (ρ ι : Nat → Nat) : FReach f maps ρ →
191
+ evalC (decideFlags f.flags (withInputs f.m.inputs ρ ι) maps) maps f.m.assume = true →
192
+ FReach f maps (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ ι) maps) maps)
193
+
194
+ def checkFMachine (f : FMachine) (ps : MachineProof) : Bool :=
195
+ flagsOk f.flags && ps.all (fun p => avoids (flagVars f.flags) p.1) && checkMachine f.plain ps
196
+
197
+ theorem freach_plain (f : FMachine) (hf : flagsOk f.flags = true) (maps : Nat → Nat → Nat) :
198
+ ∀ {ρ}, FReach f maps ρ → ∃ ρ₀, Reach f.plain maps ρ₀ ∧ Agree (flagVars f.flags) ρ ρ₀ := by
199
+ intro ρ hr
200
+ induction hr with
201
+ | start ρ h0 => exact ⟨_, Reach.start (m := f.plain) ρ h0, fun _ _ => rfl⟩
202
+ | step ρ ι _ ha ih =>
203
+ obtain ⟨ρ₀, hr₀, hag⟩ := ih
204
+ have hdec := decide_agree hf maps (withInputs_agree (ins := f.m.inputs) hag ι)
205
+ refine ⟨_, Reach.step (m := f.plain) ρ₀ ι hr₀ ?_, ?_⟩
206
+ · show evalC (withInputs f.m.inputs ρ₀ ι) maps (inlineC f.flags f.m.assume) = true
207
+ rw [inline_evalC, ← hdec]; exact ha
208
+ · intro i hi
209
+ show _ = applyS (f.m.step.map (fun p => (p.1, inlineE f.flags p.2))) (withInputs f.m.inputs ρ₀ ι) maps i
210
+ simp only [applyS, assoc_map_snd]
211
+ cases h : assoc f.m.step i with
212
+ | none =>
213
+ simp only [Option.map_none]
214
+ rw [decide_other hi]; exact withInputs_agree (ins := f.m.inputs) hag ι i hi
215
+ | some e => simp only [Option.map_some, inline_evalE, hdec]
216
+
217
+ /-- Flags inlined by Lean: every law of an accepted proof of the inlined machine, none reading a flag, holds in every
218
+ state the machine with flags reaches. -/
219
+ theorem fmachine_sound (f : FMachine) (ps : MachineProof) (h : checkFMachine f ps = true) (maps : Nat → Nat → Nat) :
220
+ ∀ ρ, FReach f maps ρ → ∀ l ∈ ps.map (·.1), evalC ρ maps l = true := by
221
+ simp only [checkFMachine, Bool.and_eq_true, List.all_eq_true] at h
222
+ obtain ⟨⟨hf, hl⟩, hc⟩ := h
223
+ intro ρ hr l hlm
224
+ obtain ⟨ρ₀, hr₀, hag⟩ := freach_plain f hf maps hr
225
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hlm
226
+ rw [congrC ρ ρ₀ maps p.1 (fun j hj => hag j (avoids_occ (hl p hp) hj))]
227
+ exact machine_sound f.plain ps hc maps ρ₀ hr₀ p.1 hlm
228
+
229
+ /-! Machines with maps and flags -/
230
+
231
+ structure FMMachine where
232
+ m : MMachine
233
+ flags : List (Nat × C)
234
+
235
+ def inlineW (fl : List (Nat × C)) (ws : List (Nat × E × E)) : List (Nat × E × E) :=
236
+ ws.map fun w => (w.1, inlineE fl w.2.1, inlineE fl w.2.2)
237
+
238
+ def FMMachine.plain (f : FMMachine) : MMachine :=
239
+ { f.m with
240
+ step := f.m.step.map (fun p => (p.1, inlineE f.flags p.2))
241
+ writes := inlineW f.flags f.m.writes
242
+ assume := inlineC f.flags f.m.assume }
243
+
244
+ inductive FMReach (f : FMMachine) : (Nat → Nat) → (Nat → Nat → Nat) → Prop
245
+ | start (ρ : Nat → Nat) (M : Nat → Nat → Nat) : StartMaps f.m M → evalC ρ M f.m.pre = true → FMReach f (applyS f.m.init ρ M) M
246
+ | step (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) : FMReach f ρ M →
247
+ evalC (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M f.m.assume = true →
248
+ FMReach f (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M)
249
+ (writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M)
250
+
251
+ def checkFMMachine (f : FMMachine) (ps : List MEntry) : Bool :=
252
+ flagsOk f.flags && avoids (flagVars f.flags) f.m.pre && ps.all (fun e => avoids (flagVars f.flags) e.law.holds)
253
+ && checkMMachine f.plain ps
254
+
255
+ theorem writeMaps_inline (fl : List (Nat × C)) (ws : List (Nat × E × E)) (X : Nat → Nat) (M : Nat → Nat → Nat) :
256
+ writeMaps (inlineW fl ws) X M = writeMaps ws (decideFlags fl X M) M := by
257
+ funext mp key
258
+ simp only [writeMaps, inlineW, lookup_map_pred]
259
+ cases lookup (fun w : Nat × E × E => w.1 == mp) ws with
260
+ | none => rfl
261
+ | some w => simp only [Option.map_some, inline_evalE]
262
+
263
+ theorem fmreach_plain (f : FMMachine) (hf : flagsOk f.flags = true) :
264
+ ∀ {ρ M}, FMReach f ρ M → ∃ ρ₀, MReach f.plain ρ₀ M ∧ Agree (flagVars f.flags) ρ ρ₀ := by
265
+ intro ρ M hr
266
+ induction hr with
267
+ | start ρ M hM h0 => exact ⟨_, MReach.start (m := f.plain) ρ M hM h0, fun _ _ => rfl⟩
268
+ | step ρ M ι _ ha ih =>
269
+ obtain ⟨ρ₀, hr₀, hag⟩ := ih
270
+ have hdec := decide_agree hf M (withInputs_agree (ins := f.m.inputs) hag ι)
271
+ have hstep := MReach.step (m := f.plain) ρ₀ M ι hr₀ (by
272
+ show evalC (withInputs f.m.inputs ρ₀ ι) M (inlineC f.flags f.m.assume) = true
273
+ rw [inline_evalC, ← hdec]; exact ha)
274
+ have hw : writeMaps f.plain.writes (withInputs f.plain.inputs ρ₀ ι) M
275
+ = writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ ι) M) M := by
276
+ show writeMaps (inlineW f.flags f.m.writes) (withInputs f.m.inputs ρ₀ ι) M = _
277
+ rw [writeMaps_inline, ← hdec]
278
+ rw [hw] at hstep
279
+ refine ⟨_, hstep, ?_⟩
280
+ intro i hi
281
+ show _ = applyS (f.m.step.map (fun p => (p.1, inlineE f.flags p.2))) (withInputs f.m.inputs ρ₀ ι) M i
282
+ simp only [applyS, assoc_map_snd]
283
+ cases h : assoc f.m.step i with
284
+ | none =>
285
+ simp only [Option.map_none]
286
+ rw [decide_other hi]; exact withInputs_agree (ins := f.m.inputs) hag ι i hi
287
+ | some e => simp only [Option.map_some, inline_evalE, hdec]
288
+
289
+ -- A law that reads no flag holds alike in states that agree outside the flags.
290
+ theorem holds_agree {vs : List Nat} (l : MLaw) (hav : avoids vs l.holds = true) {ρ ρ₀ : Nat → Nat} (hag : Agree vs ρ ρ₀)
291
+ (M : Nat → Nat → Nat) : l.Holds ρ₀ M → l.Holds ρ M := by
292
+ unfold MLaw.Holds
293
+ split
294
+ · intro h; rwa [congrC ρ ρ₀ M l.holds (fun j hj => hag j (avoids_occ hav hj))]
295
+ · rename_i kv _
296
+ intro h v
297
+ rw [congrC (upd ρ kv v) (upd ρ₀ kv v) M l.holds (fun j hj => by
298
+ simp only [upd]; split
299
+ · rfl
300
+ · exact hag j (avoids_occ hav hj))]
301
+ exact h v
302
+
303
+ /-- Flags inlined by Lean, with maps: every law of an accepted proof of the inlined machine holds in every state the
304
+ machine with flags reaches. -/
305
+ theorem fmmachine_sound (f : FMMachine) (ps : List MEntry) (h : checkFMMachine f ps = true) :
306
+ ∀ ρ M, FMReach f ρ M → ∀ e ∈ ps, e.law.Holds ρ M := by
307
+ simp only [checkFMMachine, Bool.and_eq_true, List.all_eq_true] at h
308
+ obtain ⟨⟨⟨hf, _⟩, hl⟩, hc⟩ := h
309
+ intro ρ M hr e he
310
+ obtain ⟨ρ₀, hr₀, hag⟩ := fmreach_plain f hf hr
311
+ exact holds_agree e.law (hl e he) hag M (mmachine_sound f.plain ps hc ρ₀ M hr₀ e he)
312
+
313
+ /-! Progress -/
314
+
315
+ structure Progress where
316
+ when : C
317
+ then_ : C
318
+ req : List (Nat × E) -- the request: an input and its term over the state and params
319
+ prime : List (Nat × Nat) -- a state variable, and the name `then` reads its value after the step by
320
+ primeMaps : List (Nat × Nat) -- a map, and the name `then` reads it after the step by
321
+ insts : List (Nat × E) -- (law index, key): where the goals assume the laws about every key
322
+ allowed : List VDef × Tree
323
+ causes : List VDef × Tree
324
+
325
+ -- A term of the step, at the named request: flags inlined, then the request's terms for the inputs.
326
+ def nowE (f : FMMachine) (pp : Progress) (e : E) : E := substE pp.req (inlineE f.flags e)
327
+ -- What a state variable is after the step: its step term, or itself.
328
+ def stepTerm (f : FMMachine) (v : Nat) : E :=
329
+ match assoc f.m.step v with
330
+ | some e => e
331
+ | none => .var v
332
+ def primeSub (f : FMMachine) (pp : Progress) : List (Nat × E) := pp.prime.map fun q => (q.2, nowE f pp (stepTerm f q.1))
333
+ -- A read of a primed map is the map after the step's write (read-over-write); any other read is the map before.
334
+ def rdAfter (f : FMMachine) (pp : Progress) : Nat → E → E := fun mp k =>
335
+ match lookup (fun q => q.2 == mp) pp.primeMaps with
336
+ | some q => match lookup (fun w => w.1 == q.1) f.m.writes with
337
+ | some w => .ite (.eq k (nowE f pp w.2.1)) (nowE f pp w.2.2) (.get q.1 k)
338
+ | none => .get q.1 k
339
+ | none => .get mp k
340
+
341
+ def progressBefore (f : FMMachine) (laws : List MLaw) (pp : Progress) : List C :=
342
+ [f.m.pre] ++ scalarHyps laws ++ instHyps laws pp.insts ++ [pp.when]
343
+ def allowedGoal (f : FMMachine) (laws : List MLaw) (pp : Progress) : List C :=
344
+ progressBefore f laws pp ++ [.not (substC pp.req (inlineC f.flags f.m.assume))]
345
+ def causesGoal (f : FMMachine) (laws : List MLaw) (pp : Progress) : List C :=
346
+ progressBefore f laws pp ++ [.not (substRC (pp.req ++ primeSub f pp) (rdAfter f pp) pp.then_)]
347
+
348
+ -- The request names only inputs, no primed name is an input, and `then` reads no flag.
349
+ def checkProgress (f : FMMachine) (ps : List MEntry) (pp : Progress) : Bool :=
350
+ pp.req.all (fun p => f.m.inputs.contains p.1) && pp.prime.all (fun q => !f.m.inputs.contains q.2)
351
+ && avoids (flagVars f.flags) pp.then_
352
+ && checkGoal ⟨pp.allowed.1, allowedGoal f (ps.map (·.law)) pp⟩ pp.allowed.2
353
+ && checkGoal ⟨pp.causes.1, causesGoal f (ps.map (·.law)) pp⟩ pp.causes.2
354
+
355
+ -- What `then` reads: primed names after the step, the rest before it (with the request's inputs).
356
+ def primed (prime : List (Nat × Nat)) (after before : Nat → Nat) : Nat → Nat := fun i =>
357
+ match lookup (fun q => q.2 == i) prime with
358
+ | some q => after q.1
359
+ | none => before i
360
+ def primedMaps (pm : List (Nat × Nat)) (after before : Nat → Nat → Nat) : Nat → Nat → Nat := fun mp =>
361
+ match lookup (fun q => q.2 == mp) pm with
362
+ | some q => after q.1
363
+ | none => before mp
364
+
365
+ theorem lookup_append {α : Type} (p : α → Bool) : ∀ (l1 l2 : List α),
366
+ lookup p (l1 ++ l2) = match lookup p l1 with | some a => some a | none => lookup p l2
367
+ | [], _ => rfl
368
+ | a :: as, l2 => by simp only [List.cons_append, lookup]; split <;> simp [lookup_append p as l2]
369
+
370
+ theorem assoc_none_of {σ : List (Nat × E)} {i : Nat} (h : i ∉ σ.map (·.1)) : assoc σ i = none := by
371
+ cases ha : assoc σ i with
372
+ | none => rfl
373
+ | some e => exact absurd (assoc_mem ha) h
374
+
375
+ -- Every hypothesis before the request holds at a reachable state where `when` holds.
376
+ theorem before_true (f : FMMachine) (ps : List MEntry) (pp : Progress) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
377
+ (hpre : evalC ρ M f.m.pre = true) (hlaws : ∀ e ∈ ps, e.law.Holds ρ M) (hw : evalC ρ M pp.when = true) :
378
+ ∀ c ∈ progressBefore f (ps.map (·.law)) pp, evalC ρ M c = true := by
379
+ intro c hc
380
+ simp only [progressBefore, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
381
+ rcases hc with ((rfl | hc) | hc) | rfl
382
+ · exact hpre
383
+ · simp only [scalarHyps, List.mem_filterMap] at hc
384
+ obtain ⟨l, hl, hk⟩ := hc
385
+ split at hk
386
+ · rename_i hkey
387
+ simp only [Option.some.injEq] at hk; subst hk
388
+ obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl
389
+ have := hlaws e' he'
390
+ simpa [MLaw.Holds, hkey] using this
391
+ · simp at hk
392
+ · simp only [instHyps, List.mem_filterMap] at hc
393
+ obtain ⟨p, _, hk⟩ := hc
394
+ split at hk
395
+ · rename_i l hl
396
+ split at hk
397
+ · rename_i kv hkey
398
+ simp only [Option.some.injEq] at hk; subst hk
399
+ have hlm : l ∈ ps.map (·.law) := List.mem_of_getElem? hl
400
+ obtain ⟨e', he', rfl⟩ := List.mem_map.mp hlm
401
+ have := hlaws e' he'
402
+ simp only [MLaw.Holds, hkey] at this
403
+ rw [subst_evalC]
404
+ rw [congrC _ (upd ρ kv (evalE ρ M p.2)) M e'.law.holds (fun i _ => by
405
+ simp only [applyS, assoc, lookup, upd]
406
+ by_cases hik : i = kv
407
+ · subst hik; simp
408
+ · have hb : (kv == i) = false := by simp; omega
409
+ simp only [hb, Bool.false_eq_true, ite_false, Option.map_none, hik])]
410
+ exact this _
411
+ · simp at hk
412
+ · simp at hk
413
+ · exact hw
414
+
415
+ /-- Progress: in every reachable state where `when` holds, the named request (its inputs ι, the rest of the state as it
416
+ is) is a step the machine allows, and `then` holds of that step: primed names read the state and maps after it, plain
417
+ names the state before it with the request's inputs. -/
418
+ def ProgressHolds (f : FMMachine) (pp : Progress) : Prop :=
419
+ ∀ ρ M, FMReach f ρ M → evalC ρ M pp.when = true →
420
+ evalC (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M f.m.assume = true ∧
421
+ evalC (primed pp.prime (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M)
422
+ (withInputs f.m.inputs ρ (applyS pp.req ρ M)))
423
+ (primedMaps pp.primeMaps (writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M) M)
424
+ pp.then_ = true
425
+
426
+ theorem progress_sound (f : FMMachine) (ps : List MEntry) (pp : Progress)
427
+ (hl : checkFMMachine f ps = true) (hp : checkProgress f ps pp = true) : ProgressHolds f pp := by
428
+ unfold ProgressHolds
429
+ revert hl hp
430
+ intro hl hp
431
+ show ∀ ρ M, FMReach f ρ M → evalC ρ M pp.when = true →
432
+ evalC (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M f.m.assume = true ∧
433
+ evalC (primed pp.prime (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M)
434
+ (withInputs f.m.inputs ρ (applyS pp.req ρ M)))
435
+ (primedMaps pp.primeMaps (writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M) M)
436
+ pp.then_ = true
437
+ have hl0 := hl
438
+ simp only [checkFMMachine, Bool.and_eq_true, List.all_eq_true] at hl
439
+ obtain ⟨⟨⟨hf, hpref⟩, hlf⟩, hc⟩ := hl
440
+ simp only [checkProgress, Bool.and_eq_true, List.all_eq_true] at hp
441
+ obtain ⟨⟨⟨⟨hreq, hprime⟩, _⟩, hal⟩, hca⟩ := hp
442
+ intro ρ M hr hw
443
+ obtain ⟨ρ₀, hr₀, hag⟩ := fmreach_plain f hf hr
444
+ -- The laws and the precondition hold here.
445
+ have hlaws : ∀ e ∈ ps, e.law.Holds ρ M := fmmachine_sound f ps hl0 ρ M hr
446
+ have hcm := hc
447
+ simp only [checkMMachine, Bool.and_eq_true] at hcm
448
+ obtain ⟨⟨hpa, hpr⟩, _⟩ := hcm
449
+ simp only [Bool.not_eq_true'] at hpr
450
+ have hpre : evalC ρ M f.m.pre = true := by
451
+ rw [congrC ρ ρ₀ M f.m.pre (fun j hj => hag j (avoids_occ hpref hj))]
452
+ exact pre_mreach (m := f.plain) hpa hpr hr₀
453
+ have hb := before_true f ps pp ρ M hpre hlaws hw
454
+ -- The request's state: its inputs, the rest as it is.
455
+ have hX : applyS pp.req ρ M = withInputs f.m.inputs ρ (applyS pp.req ρ M) := by
456
+ funext i
457
+ simp only [withInputs]
458
+ split
459
+ · rfl
460
+ · rename_i hi
461
+ exact applyS_other (fun hm => by
462
+ obtain ⟨p, hp', rfl⟩ := List.mem_map.mp hm
463
+ exact hi (by simpa using hreq p hp')) ρ M
464
+ refine ⟨?_, ?_⟩
465
+ · apply Decidable.byContradiction; intro hn
466
+ apply sound _ _ hal ρ M
467
+ intro c hc'
468
+ simp only [allowedGoal, List.mem_append, List.mem_singleton] at hc'
469
+ rcases hc' with hc' | rfl
470
+ · exact hb c hc'
471
+ · simp only [evalC, subst_evalC, inline_evalC]
472
+ rw [← hX] at hn; simpa using hn
473
+ · apply Decidable.byContradiction; intro hn
474
+ apply sound _ _ hca ρ M
475
+ intro c hc'
476
+ simp only [causesGoal, List.mem_append, List.mem_singleton] at hc'
477
+ rcases hc' with hc' | rfl
478
+ · exact hb c hc'
479
+ · let e := decideFlags f.flags (applyS pp.req ρ M) M
480
+ have hnow : ∀ t, evalE ρ M (nowE f pp t) = evalE e M t := by
481
+ intro t; simp only [nowE, subst_evalE, inline_evalE, e]
482
+ have hrd : ∀ mp k, evalE ρ M (rdAfter f pp mp k)
483
+ = primedMaps pp.primeMaps (writeMaps f.m.writes e M) M mp (evalE ρ M k) := by
484
+ intro mp k
485
+ simp only [rdAfter, primedMaps]
486
+ cases hq : lookup (fun q => q.2 == mp) pp.primeMaps with
487
+ | none => simp [evalE]
488
+ | some q =>
489
+ simp only [writeMaps]
490
+ cases hw : lookup (fun w => w.1 == q.1) f.m.writes with
491
+ | none => simp [evalE]
492
+ | some w =>
493
+ simp only [evalE, evalC, hnow]
494
+ by_cases hk : evalE ρ M k = evalE e M w.2.1 <;> simp [hk]
495
+ have henv : applyS (pp.req ++ primeSub f pp) ρ M = primed pp.prime (applyS f.m.step e M) (applyS pp.req ρ M) := by
496
+ funext i
497
+ have hps : lookup (fun p : Nat × E => p.1 == i) (primeSub f pp)
498
+ = (lookup (fun q => q.2 == i) pp.prime).map (fun q => (q.2, nowE f pp (stepTerm f q.1))) := by
499
+ simp only [primeSub, lookup_map_pred]
500
+ simp only [primed, applyS, assoc, lookup_append, hps]
501
+ cases hq : lookup (fun q => q.2 == i) pp.prime with
502
+ | none => cases lookup (fun p => p.1 == i) pp.req <;> rfl
503
+ | some q =>
504
+ have ⟨hqm, hqi⟩ := lookup_some hq
505
+ simp only [beq_iff_eq] at hqi
506
+ have h1 : lookup (fun p => p.1 == i) pp.req = none := by
507
+ cases h1 : lookup (fun p => p.1 == i) pp.req with
508
+ | none => rfl
509
+ | some p =>
510
+ exfalso
511
+ have ⟨hpm, hpi⟩ := lookup_some h1
512
+ simp only [beq_iff_eq] at hpi
513
+ have h3 := hprime q hqm
514
+ have h4 := hreq p hpm
515
+ rw [hpi, ← hqi] at h4
516
+ simp at h3 h4; exact h3 h4
517
+ simp only [h1, Option.map_some]
518
+ rw [hnow]
519
+ simp only [stepTerm]
520
+ cases hs : assoc f.m.step q.1 with
521
+ | none =>
522
+ rw [show Option.map (fun x => x.2) (lookup (fun p => p.1 == q.1) f.m.step) = none from hs]
523
+ simp [evalE, e]
524
+ | some t =>
525
+ rw [show Option.map (fun x => x.2) (lookup (fun p => p.1 == q.1) f.m.step) = some t from hs]
526
+ simp only [evalC, substR_evalC _ _ _ M _ hrd, henv]
527
+ rw [← hX] at hn; simpa [e] using hn
528
+
529
+ -- The named request is a step the machine takes: the state after it is reachable too.
530
+ theorem progress_reach (f : FMMachine) (ps : List MEntry) (pp : Progress)
531
+ (hl : checkFMMachine f ps = true) (hp : checkProgress f ps pp = true) {ρ M} (hr : FMReach f ρ M)
532
+ (hw : evalC ρ M pp.when = true) :
533
+ FMReach f (applyS f.m.step (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M)
534
+ (writeMaps f.m.writes (decideFlags f.flags (withInputs f.m.inputs ρ (applyS pp.req ρ M)) M) M) :=
535
+ FMReach.step ρ M _ hr (progress_sound f ps pp hl hp ρ M hr hw).1
536
+
537
+ end Kern
538
+
539
+ #print axioms Kern.inline_evalC
540
+ #print axioms Kern.fmachine_sound
541
+ #print axioms Kern.fmmachine_sound
542
+ #print axioms Kern.progress_sound
543
+ #print axioms Kern.progress_reach