gdp-wired 0.50.6 → 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 (91) 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 +11 -2
  9. package/dist/core/dsl.js +194 -29
  10. package/dist/core/fixed-division.d.ts +9 -0
  11. package/dist/core/fixed-division.js +87 -0
  12. package/dist/core/fixed-integers.d.ts +33 -0
  13. package/dist/core/fixed-integers.js +279 -0
  14. package/dist/core/live-support.d.ts +5 -0
  15. package/dist/core/live-support.js +148 -0
  16. package/dist/core/live.js +194 -37
  17. package/dist/core/lockstep.d.ts +5 -0
  18. package/dist/core/lockstep.js +23 -0
  19. package/dist/core/machine.d.ts +1 -0
  20. package/dist/core/machine.js +33 -3
  21. package/dist/core/ordered-pair-seeds.d.ts +2 -0
  22. package/dist/core/ordered-pair-seeds.js +109 -0
  23. package/dist/core/proof-output.d.ts +38 -1
  24. package/dist/core/proof-output.js +38 -0
  25. package/dist/core/search-termination.d.ts +4 -0
  26. package/dist/core/search-termination.js +1 -1
  27. package/dist/core/source-check.d.ts +4 -1
  28. package/dist/core/source-check.js +5 -2
  29. package/dist/core/sparse-kind-proof.d.ts +7 -0
  30. package/dist/core/sparse-kind-proof.js +49 -0
  31. package/dist/core/step-seeds.d.ts +46 -0
  32. package/dist/core/step-seeds.js +316 -0
  33. package/dist/kernel/integer-primitives.d.ts +38 -0
  34. package/dist/kernel/integer-primitives.js +179 -0
  35. package/dist/kernel/kernel.d.ts +1 -0
  36. package/dist/kernel/kernel.js +44 -5
  37. package/dist/kernel/prove.js +13 -0
  38. package/dist/kernel/word-division-cuts.d.ts +6 -0
  39. package/dist/kernel/word-division-cuts.js +69 -0
  40. package/dist/kernel/word-division.d.ts +12 -0
  41. package/dist/kernel/word-division.js +167 -0
  42. package/dist/lean-kernel/Cert.lean +222 -0
  43. package/dist/lean-kernel/Count.lean +1568 -0
  44. package/dist/lean-kernel/Ghost.lean +373 -0
  45. package/dist/lean-kernel/GhostMap.lean +426 -0
  46. package/dist/lean-kernel/Kernel.lean +2881 -0
  47. package/dist/lean-kernel/Live.lean +1168 -0
  48. package/dist/lean-kernel/Progress.lean +543 -0
  49. package/dist/lean-kernel/Sets.lean +1975 -0
  50. package/dist/lean-kernel/StepLaw.lean +128 -0
  51. package/dist/lean-kernel/Steps.lean +477 -0
  52. package/dist/lean-kernel/Sums.lean +916 -0
  53. package/dist/lean-kernel/emit.d.ts +8 -1
  54. package/dist/lean-kernel/emit.js +59 -15
  55. package/dist/lean-kernel/integer-primitives.d.ts +17 -0
  56. package/dist/lean-kernel/integer-primitives.js +81 -0
  57. package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
  58. package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
  59. package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
  60. package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
  61. package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
  62. package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
  63. package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
  64. package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
  65. package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
  66. package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
  67. package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
  68. package/dist/lean-kernel/lean-aggregate/index.js +52 -0
  69. package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
  70. package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
  71. package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
  72. package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
  73. package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
  74. package/dist/lean-kernel/lean-aggregate/source.js +141 -0
  75. package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
  76. package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
  77. package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
  78. package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
  79. package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
  80. package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
  81. package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
  82. package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
  83. package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
  84. package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
  85. package/dist/lean-kernel/library.js +13 -0
  86. package/dist/lean-kernel/word-division-export.d.ts +26 -0
  87. package/dist/lean-kernel/word-division-export.js +172 -0
  88. package/dist/lean-kernel/word-division-lower.d.ts +10 -0
  89. package/dist/lean-kernel/word-division-lower.js +103 -0
  90. package/docs/lean-aggregate.md +29 -0
  91. package/package.json +6 -1
@@ -0,0 +1,373 @@
1
+ import Live
2
+
3
+ /-!
4
+ What a ghost means, proven: compose.ts's `ghost(m, vars)` adds state that watches a machine without changing it. Lean
5
+ checks that a ghosted machine is the original's parts plus each ghost's start and step, read by no part of the original
6
+ (`sghostOk`), and proves, for machines with maps, flags and sets:
7
+ - `sghost_reach`: every reachable state of the original is, outside the ghosts, a reachable state of the ghosted
8
+ machine, so a law of the ghosted machine that reads no ghost is a law of the original (`sghost_laws`);
9
+ - `sghost_run`: every run of the original is, outside the ghosts, a run of the ghosted machine taking the same requests;
10
+ - `sever_meaning`: along a run, a ghost that starts at 0 and is set to 1 by a step where `c` holds (compose.ts's `ever`)
11
+ is 1 exactly when some step before took a request where `c` held.
12
+ -/
13
+
14
+ namespace Kern
15
+ open Classical
16
+
17
+ theorem lookup_unique_fst {p : Nat × E} {l : List (Nat × E)} (hd : distinct (l.map (·.1)) = true) (hp : p ∈ l) :
18
+ lookup (fun q => q.1 == p.1) l = some p := by
19
+ induction l with
20
+ | nil => simp at hp
21
+ | cons a as ih =>
22
+ simp only [List.map_cons] at hd
23
+ obtain ⟨hna, hd'⟩ := distinct_cons hd
24
+ simp only [lookup]
25
+ rcases List.mem_cons.mp hp with rfl | hp'
26
+ · simp
27
+ · have hne : (a.1 == p.1) = false := by
28
+ simp only [beq_eq_false_iff_ne]; intro he; exact hna (by rw [he]; exact List.mem_map_of_mem hp')
29
+ simp only [hne, Bool.false_eq_true, ite_false]; exact ih hd' hp'
30
+
31
+ theorem assoc_append_none {σ τ : List (Nat × E)} {i : Nat} (h : i ∉ τ.map (·.1)) : assoc (σ ++ τ) i = assoc σ i := by
32
+ have hτ : lookup (fun p => p.1 == i) τ = none :=
33
+ 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))
34
+ simp only [assoc, lookup_append]
35
+ cases hσ : lookup (fun p => p.1 == i) σ <;> simp [hτ]
36
+
37
+ theorem applyS_append_other {σ τ : List (Nat × E)} {i : Nat} (h : i ∉ τ.map (·.1)) (ρ : Nat → Nat) (M : Nat → Nat → Nat) :
38
+ applyS (σ ++ τ) ρ M i = applyS σ ρ M i := by
39
+ simp only [applyS, assoc_append_none h]
40
+
41
+ -- The keys a machine's every-key writes and quantified flags bind.
42
+ def bkeys (s : SMachine) : List Nat := s.every.map (·.2.1) ++ s.qflags.flatMap (·.2.1)
43
+
44
+ -- A flag that holds at every value of its keys, decided over states that agree where its body reads (its keys aside), is
45
+ -- decided alike.
46
+ theorem ite_all_congr {X X' : Nat → Nat} {N N' : Nat → Nat → Nat} {ks : List Nat} {c : C}
47
+ (hv : ∀ i, occC i c = true → i ∉ ks → X i = X' i) (hm : ∀ mp, occMC mp c = true → N mp = N' mp) :
48
+ (if ∀ k, holdsC (atKeys X ks k) N c then 1 else 0) = (if ∀ k, holdsC (atKeys X' ks k) N' c then 1 else 0) := by
49
+ have h : (∀ k, holdsC (atKeys X ks k) N c) ↔ (∀ k, holdsC (atKeys X' ks k) N' c) := by
50
+ apply forall_congr'; intro k
51
+ rw [← evalC_iff, ← evalC_iff, congrMC _ N N' c hm]
52
+ rw [congrC (atKeys X ks k) (atKeys X' ks k) N' c (fun i hi => by
53
+ by_cases hik : i ∈ ks
54
+ · rw [atKeys_in hik, atKeys_in hik]
55
+ · rw [atKeys_out hik, atKeys_out hik]; exact hv i hi hik)]
56
+ simp only [h]
57
+
58
+ theorem decideQ_of (s : SMachine) (X : Nat → Nat) (N : Nat → Nat → Nat) (v : Nat) :
59
+ decideQ s X N v = match lookup (fun q => q.1 == v) s.qflags with
60
+ | some q => if ∀ k, holdsC (atKeys X q.2.1 k) N q.2.2 then 1 else 0
61
+ | none => X v := rfl
62
+
63
+ theorem upd_agree {V : List Nat} {X X' : Nat → Nat} (h : Agree V X X') (k v : Nat) : Agree V (upd X k v) (upd X' k v) := fun i hi => by
64
+ simp only [upd]; split
65
+ · rfl
66
+ · exact h i hi
67
+
68
+ /-! Ghosts -/
69
+
70
+ structure SGhosted where
71
+ orig : SMachine
72
+ ghosts : List (Nat × E × E) -- (variable, start over the params, step over the state before, the request and flags)
73
+
74
+ def SGhosted.vars (g : SGhosted) : List Nat := g.ghosts.map (·.1)
75
+
76
+ -- The ghosted machine: the original, each ghost's start and step added.
77
+ def SGhosted.full (g : SGhosted) : SMachine :=
78
+ ⟨⟨⟨g.orig.f.m.inputs, g.orig.f.m.init ++ g.ghosts.map (fun x => (x.1, x.2.1)), g.orig.f.m.maps0,
79
+ g.orig.f.m.step ++ g.ghosts.map (fun x => (x.1, x.2.2)), g.orig.f.m.writes, g.orig.f.m.pre, g.orig.f.m.assume⟩, g.orig.f.flags⟩,
80
+ g.orig.carried, g.orig.every, g.orig.qflags, g.orig.wits, g.orig.facts, g.orig.pfacts⟩
81
+
82
+ -- The ghosts are new names, and nothing of the original reads them.
83
+ def sghostOk (g : SGhosted) : Bool :=
84
+ distinct g.vars
85
+ && g.vars.all (fun v => !(g.orig.extVars ++ g.orig.f.m.init.map (·.1) ++ g.orig.f.m.step.map (·.1) ++ flagVars g.orig.f.flags ++ bkeys g.orig).contains v)
86
+ && avoids g.vars g.orig.f.m.pre && avoids g.vars g.orig.f.m.assume && g.orig.f.flags.all (fun p => avoids g.vars p.2)
87
+ && g.orig.f.m.step.all (fun p => avoidsE g.vars p.2) && g.orig.f.m.init.all (fun p => avoidsE g.vars p.2)
88
+ && g.orig.f.m.writes.all (fun w => avoidsE g.vars w.2.1 && avoidsE g.vars w.2.2)
89
+ && g.orig.every.all (fun w => avoidsE g.vars w.2.2) && g.orig.qflags.all (fun q => avoids g.vars q.2.2)
90
+ && g.orig.facts.all (fun l => avoids g.vars l.holds)
91
+
92
+ -- The ghosted machine reads requests and decides flags as the original does.
93
+ theorem sview_full (g : SGhosted) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
94
+ sView g.full ρ M ι κ = sView g.orig ρ M ι κ := rfl
95
+
96
+ -- A run of the original lifted to the ghosted machine from a ghosted state s0 that agrees with its first: the ghosts
97
+ -- move with each request the run takes.
98
+ noncomputable def sghostFrom (g : SGhosted) (run : SRun g.orig) (s0 : (Nat → Nat) × (Nat → Nat → Nat)) : Nat → (Nat → Nat) × (Nat → Nat → Nat)
99
+ | 0 => s0
100
+ | n + 1 => sNext g.full (sghostFrom g run s0 n).1 (sghostFrom g run s0 n).2 (run.req n).1 (run.req n).2
101
+
102
+ section sghost
103
+ variable (g : SGhosted) (hg : sghostOk g = true)
104
+ include hg
105
+
106
+ theorem sghostOk_parts :
107
+ distinct g.vars = true
108
+ ∧ (∀ v ∈ g.vars, v ∉ g.orig.extVars ++ g.orig.f.m.init.map (·.1) ++ g.orig.f.m.step.map (·.1) ++ flagVars g.orig.f.flags ++ bkeys g.orig)
109
+ ∧ avoids g.vars g.orig.f.m.pre = true ∧ avoids g.vars g.orig.f.m.assume = true ∧ (∀ p ∈ g.orig.f.flags, avoids g.vars p.2 = true)
110
+ ∧ (∀ p ∈ g.orig.f.m.step, avoidsE g.vars p.2 = true) ∧ (∀ p ∈ g.orig.f.m.init, avoidsE g.vars p.2 = true)
111
+ ∧ (∀ w ∈ g.orig.f.m.writes, avoidsE g.vars w.2.1 = true ∧ avoidsE g.vars w.2.2 = true)
112
+ ∧ (∀ w ∈ g.orig.every, avoidsE g.vars w.2.2 = true) ∧ (∀ q ∈ g.orig.qflags, avoids g.vars q.2.2 = true)
113
+ ∧ (∀ l ∈ g.orig.facts, avoids g.vars l.holds = true) := by
114
+ simp only [sghostOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at hg
115
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hd, hn⟩, hp⟩, ha⟩, hf⟩, hs⟩, hi⟩, hw⟩, he⟩, hq⟩, hfa⟩ := hg
116
+ exact ⟨hd, fun v hv h => (by have := hn v hv; rw [List.contains_iff_mem.mpr h] at this; cases this), hp, ha, hf, hs, hi, hw, he, hq, hfa⟩
117
+
118
+ -- One step from states that agree outside the ghosts, with the same request: allowed alike, and to states that agree
119
+ -- outside the ghosts, with the same maps.
120
+ theorem sghost_step {ρ ρg : Nat → Nat} (hag : Agree g.vars ρg ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
121
+ evalC (sView g.full ρg M ι κ).1 (sView g.full ρg M ι κ).2 g.full.f.m.assume = evalC (sView g.orig ρ M ι κ).1 (sView g.orig ρ M ι κ).2 g.orig.f.m.assume
122
+ ∧ Agree g.vars (sNext g.full ρg M ι κ).1 (sNext g.orig ρ M ι κ).1
123
+ ∧ (sNext g.full ρg M ι κ).2 = (sNext g.orig ρ M ι κ).2 := by
124
+ obtain ⟨_, _, _, ha, hf, hs, _, hw, he, hq, _⟩ := sghostOk_parts g hg
125
+ have hX : Agree g.vars (withInputs g.orig.f.m.inputs ρg ι) (withInputs g.orig.f.m.inputs ρ ι) := fun i hi => by
126
+ simp only [withInputs]; split
127
+ · rfl
128
+ · exact hag i hi
129
+ have hQ : Agree g.vars (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig M κ))
130
+ (decideQ g.orig (withInputs g.orig.f.m.inputs ρ ι) (withCarried g.orig M κ)) := by
131
+ intro v hv
132
+ rw [decideQ_of, decideQ_of]
133
+ cases hl : lookup (fun q => q.1 == v) g.orig.qflags with
134
+ | some q => exact ite_all_congr (fun i hi _ => hX i (avoids_occ (hq q (lookup_some hl).1) hi)) (fun _ _ => rfl)
135
+ | none => exact hX v hv
136
+ have hD := decide_agree_on hf (withCarried g.orig M κ) hQ
137
+ refine ⟨?_, fun i hi => ?_, ?_⟩
138
+ · show evalC (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig M κ)) (withCarried g.orig M κ)) _ g.orig.f.m.assume = _
139
+ exact congrC _ _ _ _ (fun i hi => hD i (avoids_occ ha hi))
140
+ · show applyS (g.orig.f.m.step ++ g.ghosts.map (fun x => (x.1, x.2.2))) (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig M κ)) (withCarried g.orig M κ)) (withCarried g.orig M κ) i = _
141
+ rw [applyS_append_other (by simpa [SGhosted.vars] using hi)]
142
+ simp only [sNext, sView, applyS]
143
+ cases hst : assoc g.orig.f.m.step i with
144
+ | none => exact hD i hi
145
+ | some e =>
146
+ have hm : (i, e) ∈ g.orig.f.m.step := by
147
+ simp only [assoc, Option.map_eq_some_iff] at hst
148
+ obtain ⟨q, hq', rfl⟩ := hst
149
+ obtain ⟨hqm, hqv⟩ := lookup_some hq'
150
+ simp only [beq_iff_eq] at hqv
151
+ rw [← hqv]; exact hqm
152
+ exact congrE _ _ _ e (fun j hj => hD j (avoidsE_occ (hs (i, e) hm) hj))
153
+ · funext mp key
154
+ show everyMaps g.orig (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig M κ)) (withCarried g.orig M κ)) (withCarried g.orig M κ) mp key
155
+ = everyMaps g.orig (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρ ι) (withCarried g.orig M κ)) (withCarried g.orig M κ)) (withCarried g.orig M κ) mp key
156
+ simp only [everyMaps]
157
+ cases hl : lookup (fun w => w.1 == mp) g.orig.every with
158
+ | some w => exact congrE _ _ _ w.2.2 (fun j hj => upd_agree hD w.2.1 key j (avoidsE_occ (he w (lookup_some hl).1) hj))
159
+ | none =>
160
+ simp only [writeMaps]
161
+ cases hl2 : lookup (fun w => w.1 == mp) g.orig.f.m.writes with
162
+ | none => rfl
163
+ | some w =>
164
+ obtain ⟨h1, h2⟩ := hw w (lookup_some hl2).1
165
+ simp only [congrE _ _ _ w.2.1 (fun j hj => hD j (avoidsE_occ h1 hj)), congrE _ _ _ w.2.2 (fun j hj => hD j (avoidsE_occ h2 hj))]
166
+
167
+ /-- Every reachable state of the original is, outside the ghosts, a reachable state of the ghosted machine. -/
168
+ theorem sghost_reach : ∀ {ρ M}, SReach g.orig ρ M → ∃ ρg, SReach g.full ρg M ∧ Agree g.vars ρg ρ := by
169
+ intro ρ M hr
170
+ induction hr with
171
+ | start ρ M hM h0 hfa hpf =>
172
+ refine ⟨applyS g.full.f.m.init ρ M, SReach.start ρ M hM h0 hfa hpf, fun i hi => ?_⟩
173
+ show applyS (g.orig.f.m.init ++ g.ghosts.map (fun x => (x.1, x.2.1))) ρ M i = applyS g.orig.f.m.init ρ M i
174
+ exact applyS_append_other (by simpa [SGhosted.vars] using hi) ρ M
175
+ | step ρ M ι κ _ ha ih =>
176
+ obtain ⟨ρg, hrg, hag⟩ := ih
177
+ obtain ⟨hA, hS, hW⟩ := sghost_step g hg hag M ι κ
178
+ refine ⟨_, ?_, hS⟩
179
+ rw [← hW]
180
+ exact SReach.step ρg M ι κ hrg (by rw [hA]; exact ha)
181
+
182
+ /-- A law of the ghosted machine that reads no ghost holds in every reachable state of the original. -/
183
+ theorem sghost_laws (ps : List SEntry) (hl : checkSMachine g.full ps = true) :
184
+ ∀ ρ M, SReach g.orig ρ M → ∀ e ∈ ps, avoids g.vars e.law.holds = true → e.Holds ρ M := by
185
+ intro ρ M hr e he hav
186
+ obtain ⟨ρg, hrg, hag⟩ := sghost_reach g hg hr
187
+ exact sentry_transfer (V := g.vars) (MS := []) hav (by simp [avoidsMaps]) (fun i hi => (hag i hi).symm) (fun _ _ => rfl)
188
+ (smachine_sound g.full ps hl ρg M hrg e he)
189
+
190
+ theorem sghostFrom_agree (run : SRun g.orig) (s0 : (Nat → Nat) × (Nat → Nat → Nat)) (hM0 : s0.2 = (run.st 0).2) (hA0 : Agree g.vars s0.1 (run.st 0).1) :
191
+ ∀ n, (sghostFrom g run s0 n).2 = (run.st n).2 ∧ Agree g.vars (sghostFrom g run s0 n).1 (run.st n).1
192
+ | 0 => ⟨hM0, hA0⟩
193
+ | n + 1 => by
194
+ obtain ⟨hM, hag⟩ := sghostFrom_agree run s0 hM0 hA0 n
195
+ obtain ⟨_, hS, hW⟩ := sghost_step g hg hag (run.st n).2 (run.req n).1 (run.req n).2
196
+ rw [run.next n]
197
+ simp only [sghostFrom, hM]
198
+ exact ⟨hW, hS⟩
199
+
200
+ omit hg in
201
+ noncomputable def sghostRun (hg : sghostOk g = true) (run : SRun g.orig) (s0 : (Nat → Nat) × (Nat → Nat → Nat)) (hr : SReach g.full s0.1 s0.2)
202
+ (hM0 : s0.2 = (run.st 0).2) (hA0 : Agree g.vars s0.1 (run.st 0).1) : SRun g.full :=
203
+ ⟨sghostFrom g run s0, run.req, hr,
204
+ fun n => by
205
+ obtain ⟨hM, hag⟩ := sghostFrom_agree g hg run s0 hM0 hA0 n
206
+ obtain ⟨hA, _, _⟩ := sghost_step g hg hag (run.st n).2 (run.req n).1 (run.req n).2
207
+ rw [hM, hA]; exact run.allowed n,
208
+ fun n => rfl⟩
209
+
210
+ /-- Every run of the original is, outside the ghosts, a run of the ghosted machine taking the same requests. -/
211
+ theorem sghost_run (run : SRun g.orig) : ∃ run' : SRun g.full, run'.req = run.req ∧ ∀ n, (run'.st n).2 = (run.st n).2 ∧ Agree g.vars (run'.st n).1 (run.st n).1 :=
212
+ ⟨sghostRun g hg run (Classical.choose (sghost_reach g hg run.start), (run.st 0).2) (Classical.choose_spec (sghost_reach g hg run.start)).1 rfl
213
+ (Classical.choose_spec (sghost_reach g hg run.start)).2, rfl,
214
+ sghostFrom_agree g hg run (Classical.choose (sghost_reach g hg run.start), (run.st 0).2) rfl (Classical.choose_spec (sghost_reach g hg run.start)).2⟩
215
+ /-- A run of the original from its start lifts to one of the ghosted machine from its start: each ghost at its start term. -/
216
+ theorem sghost_run_start (run : SRun g.orig) {ρ0 : Nat → Nat} {M0 : Nat → Nat → Nat} (hM : StartMaps g.orig.f.m M0)
217
+ (hp : evalC ρ0 M0 g.orig.f.m.pre = true) (hfa : ∀ l ∈ g.orig.facts, l.Holds ρ0 M0) (hpf : g.orig.PFacts ρ0 M0) (hst : run.st 0 = (applyS g.orig.f.m.init ρ0 M0, M0)) :
218
+ ∃ run' : SRun g.full, run'.req = run.req ∧ run'.st 0 = (applyS g.full.f.m.init ρ0 M0, M0)
219
+ ∧ ∀ n, (run'.st n).2 = (run.st n).2 ∧ Agree g.vars (run'.st n).1 (run.st n).1 := by
220
+ have hA0 : Agree g.vars (applyS g.full.f.m.init ρ0 M0) (run.st 0).1 := fun i hi => by
221
+ rw [hst]
222
+ show applyS (g.orig.f.m.init ++ g.ghosts.map (fun x => (x.1, x.2.1))) ρ0 M0 i = applyS g.orig.f.m.init ρ0 M0 i
223
+ exact applyS_append_other (by simpa [SGhosted.vars] using hi) ρ0 M0
224
+ have hM0 : M0 = (run.st 0).2 := by rw [hst]
225
+ exact ⟨sghostRun g hg run (applyS g.full.f.m.init ρ0 M0, M0) (SReach.start ρ0 M0 hM hp hfa hpf) hM0 hA0, rfl, rfl,
226
+ sghostFrom_agree g hg run (applyS g.full.f.m.init ρ0 M0, M0) hM0 hA0⟩
227
+
228
+ -- A condition that reads no ghost, read where a step reads (its request, its flags decided), reads alike in a run and
229
+ -- its lift.
230
+ theorem sghost_view (run : SRun g.orig) (run' : SRun g.full) (hreq : run'.req = run.req)
231
+ (hag : ∀ n, (run'.st n).2 = (run.st n).2 ∧ Agree g.vars (run'.st n).1 (run.st n).1) {c : C} (hc : avoids g.vars c = true) (n : Nat) :
232
+ evalC (sView g.orig (run'.st n).1 (run'.st n).2 (run'.req n).1 (run'.req n).2).1 (sView g.orig (run'.st n).1 (run'.st n).2 (run'.req n).1 (run'.req n).2).2 c
233
+ = evalC (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).1 (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).2 c := by
234
+ obtain ⟨_, _, _, _, hf, _, _, _, _, hq, _⟩ := sghostOk_parts g hg
235
+ obtain ⟨hM, hA⟩ := hag n
236
+ rw [hM, hreq]
237
+ have hX : Agree g.vars (withInputs g.orig.f.m.inputs (run'.st n).1 (run.req n).1) (withInputs g.orig.f.m.inputs (run.st n).1 (run.req n).1) := fun i hi => by
238
+ simp only [withInputs]; split
239
+ · rfl
240
+ · exact hA i hi
241
+ have hQ : Agree g.vars (decideQ g.orig (withInputs g.orig.f.m.inputs (run'.st n).1 (run.req n).1) (withCarried g.orig (run.st n).2 (run.req n).2))
242
+ (decideQ g.orig (withInputs g.orig.f.m.inputs (run.st n).1 (run.req n).1) (withCarried g.orig (run.st n).2 (run.req n).2)) := by
243
+ intro v hv
244
+ rw [decideQ_of, decideQ_of]
245
+ cases hl : lookup (fun q => q.1 == v) g.orig.qflags with
246
+ | some q => exact ite_all_congr (fun i hi _ => hX i (avoids_occ (hq q (lookup_some hl).1) hi)) (fun _ _ => rfl)
247
+ | none => exact hX v hv
248
+ exact congrC _ _ _ c (fun i hi => decide_agree_on hf _ hQ i (avoids_occ hc hi))
249
+ end sghost
250
+
251
+ /-- What `ever` means: along a run of the ghosted machine where the ghost x starts at 0, with x's step `if c then 1
252
+ else x`, x is 1 at step n exactly when some step before n took a request where c held (read where that step reads: its
253
+ request, its flags decided). -/
254
+ theorem sever_meaning (g : SGhosted) (hg : sghostOk g = true) {x : Nat} {s0 : E} {c : C} (hx : (x, s0, .ite c (.const 1) (.var x)) ∈ g.ghosts)
255
+ (run : SRun g.full) (h0 : (run.st 0).1 x = 0) :
256
+ ∀ n, (run.st n).1 x = 1 ↔ ∃ i, i < n ∧ evalC (sView g.orig (run.st i).1 (run.st i).2 (run.req i).1 (run.req i).2).1
257
+ (sView g.orig (run.st i).1 (run.st i).2 (run.req i).1 (run.req i).2).2 c = true := by
258
+ obtain ⟨hd, hn, _⟩ := sghostOk_parts g hg
259
+ have hxv : x ∈ g.vars := List.mem_map_of_mem hx
260
+ have hxo := hn x hxv
261
+ have hassoc : assoc g.full.f.m.step x = some (.ite c (.const 1) (.var x)) := by
262
+ show assoc (g.orig.f.m.step ++ g.ghosts.map (fun y => (y.1, y.2.2))) x = _
263
+ simp only [assoc, lookup_append]
264
+ rw [lookup_none (fun p hp => by simp only [beq_eq_false_iff_ne]; intro he; exact hxo (by
265
+ rw [← he]; exact List.mem_append_left _ (List.mem_append_left _ (List.mem_append_right _ (List.mem_map_of_mem hp)))))]
266
+ have hl : lookup (fun p => p.1 == x) (g.ghosts.map (fun y => (y.1, y.2.2))) = some (x, .ite c (.const 1) (.var x)) := by
267
+ have hd' : distinct ((g.ghosts.map (fun y => (y.1, y.2.2))).map (·.1)) = true := by rw [List.map_map]; exact hd
268
+ exact lookup_unique_fst hd' (List.mem_map.mpr ⟨_, hx, rfl⟩)
269
+ simp [hl]
270
+ have hxe : x ∉ g.orig.extVars := fun h => hxo (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ h))))
271
+ have hxf : x ∉ flagVars g.orig.f.flags := fun h => hxo (List.mem_append_left _ (List.mem_append_right _ h))
272
+ simp only [SMachine.extVars, SMachine.qVars, List.mem_append, not_or] at hxe
273
+ intro n
274
+ induction n with
275
+ | zero => simp [h0]
276
+ | succ n ih =>
277
+ have hst : (run.st (n + 1)).1 x = if evalC (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).1
278
+ (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).2 c = true then 1 else (run.st n).1 x := by
279
+ rw [run.next n]
280
+ simp only [sNext, applyS, hassoc, evalE]
281
+ rw [sview_full]
282
+ congr 1
283
+ simp only [sView]
284
+ rw [decide_other hxf, decideQ_other (by simpa [SMachine.qVars] using hxe.1.2), withInputs_other hxe.1.1]
285
+ rw [hst]
286
+ constructor
287
+ · intro h1
288
+ by_cases hc : evalC (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).1 (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).2 c = true
289
+ · exact ⟨n, Nat.lt_succ_self n, hc⟩
290
+ · rw [if_neg hc] at h1
291
+ obtain ⟨i, hi, hci⟩ := ih.mp h1
292
+ exact ⟨i, Nat.lt_succ_of_lt hi, hci⟩
293
+ · rintro ⟨i, hi, hci⟩
294
+ by_cases hc : evalC (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).1 (sView g.orig (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2).2 c = true
295
+ · rw [if_pos hc]
296
+ · rw [if_neg hc]
297
+ rcases Nat.lt_succ_iff_lt_or_eq.mp hi with hi | rfl
298
+ · exact ih.mpr ⟨i, hi, hci⟩
299
+ · exact absurd hci hc
300
+
301
+ /-! There exists: a law ∃ x. P x (core/exists.ts), proven through its witness. The kernel proves a plain law: P with a
302
+ term of the state for each bound value (`substC σ P`, a witness term), or P over ghosts the machine gains (a ghost
303
+ witness, carried along the run); either way the values the law binds are some assignment of its variables, and
304
+ `updAll` writes that assignment over the state. `some_updAll` is the step from what the kernel proved to ∃: the
305
+ witness's values, written over the state, satisfy P (the state elsewhere as it was). -/
306
+
307
+ -- The state with each pair's variable set to its value, in order.
308
+ def updAll (ρ : Nat → Nat) : List (Nat × Nat) → Nat → Nat
309
+ | [] => ρ
310
+ | p :: ps => updAll (upd ρ p.1 p.2) ps
311
+
312
+ theorem updAll_val (vs : List Nat) (ρ ρ' : Nat → Nat) (i : Nat) :
313
+ updAll ρ (vs.map fun j => (j, ρ' j)) i = if i ∈ vs then ρ' i else ρ i := by
314
+ induction vs generalizing ρ with
315
+ | nil => simp [updAll]
316
+ | cons v vs ih =>
317
+ simp only [List.map_cons, updAll]
318
+ rw [ih]
319
+ by_cases hi : i ∈ vs
320
+ · simp [hi]
321
+ · by_cases hv : i = v
322
+ · subst hv; simp [hi, upd]
323
+ · simp [hi, hv, upd]
324
+
325
+ -- A state that agrees with ρ outside V, read where c reads of V only variables in vs: ρ with vs set to its values
326
+ -- reads alike.
327
+ theorem evalC_updAll (vs V : List Nat) {ρ ρ' : Nat → Nat} (hag : ∀ i, i ∉ V → ρ' i = ρ i) (c : C)
328
+ (hc : ∀ i, occC i c = true → i ∈ V → i ∈ vs) (M : Nat → Nat → Nat) :
329
+ evalC (updAll ρ (vs.map fun i => (i, ρ' i))) M c = evalC ρ' M c := by
330
+ apply congrC
331
+ intro i hi
332
+ rw [updAll_val]
333
+ by_cases hv : i ∈ vs
334
+ · simp [hv]
335
+ · simp only [hv, if_false]
336
+ exact (hag i (fun hV => hv (hc i hi hV))).symm
337
+
338
+ /-- A law the kernel proved with witness terms σ in place (`substC σ c`) gives the existential: the terms' values,
339
+ written over the state, satisfy c. -/
340
+ theorem some_of_subst (σ : List (Nat × E)) (c : C) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
341
+ (h : evalC ρ M (substC σ c) = true) :
342
+ evalC (updAll ρ ((σ.map (·.1)).map fun i => (i, applyS σ ρ M i))) M c = true := by
343
+ rw [evalC_updAll (σ.map (·.1)) (σ.map (·.1)) (fun i hi => applyS_other hi ρ M) c (fun _ _ h => h) M, ← subst_evalC]
344
+ exact h
345
+
346
+ /-- A law of the ghosted machine over its ghosts vs gives, at every reachable state of the original, the existential:
347
+ the ghosts' values there, written over the state, satisfy it. -/
348
+ theorem some_of_ghost (g : SGhosted) (hg : sghostOk g = true) (vs : List Nat) (c : C)
349
+ (hc : avoids (g.vars.filter fun i => !vs.contains i) c = true)
350
+ (hl : ∀ ρ M, SReach g.full ρ M → evalC ρ M c = true) :
351
+ ∀ ρ M, SReach g.orig ρ M → ∃ ρg : Nat → Nat, evalC (updAll ρ (vs.map fun i => (i, ρg i))) M c = true := by
352
+ intro ρ M hr
353
+ obtain ⟨ρg, hrg, hag⟩ := sghost_reach g hg hr
354
+ have hc' : ∀ i, occC i c = true → i ∈ g.vars → i ∈ vs := by
355
+ intro i hi hV
356
+ by_cases hv : i ∈ vs
357
+ · exact hv
358
+ · have hp : (!vs.contains i) = true := by
359
+ cases h : vs.contains i with
360
+ | false => rfl
361
+ | true => exact absurd (List.contains_iff_mem.mp h) hv
362
+ exact absurd (List.mem_filter.mpr ⟨hV, hp⟩) (avoids_occ hc hi)
363
+ exact ⟨ρg, by rw [evalC_updAll vs g.vars hag c hc' M]; exact hl ρg M hrg⟩
364
+
365
+ end Kern
366
+
367
+ #print axioms Kern.some_of_subst
368
+ #print axioms Kern.some_of_ghost
369
+ #print axioms Kern.sghost_reach
370
+ #print axioms Kern.sghost_laws
371
+ #print axioms Kern.sghost_run
372
+ #print axioms Kern.sever_meaning
373
+ #print axioms Kern.sghost_run_start