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,426 @@
1
+ import Ghost
2
+
3
+ /-!
4
+ GhostMap: ghost maps, proven. Ghost.lean's `sghost_reach` lets a machine gain ghost values (scalars the step keeps);
5
+ this file lets it gain ghost maps too: maps the original never reads, each starting at a constant and written by the
6
+ step at one key (`gwrites`) or at every key (`gevery`). A ghost map is a Skolem function the step keeps: a law of the
7
+ ghosted machine about every key k, reading the ghost map W at k, says that every k has a witness, W[k].
8
+
9
+ Lean checks that a ghosted machine is the original's parts plus each ghost's start and step (`SGhostedM.full`), with
10
+ new names read by no part of the original (`smghostOk`), and proves, for machines with maps, flags and sets:
11
+ - `smghost_reach`: every reachable state of the original is, outside the ghost variables and the ghost maps, a
12
+ reachable state of the ghosted machine (the generalization of `sghost_reach` from ghost values to ghost maps);
13
+ - `smghost_laws`: a law of the ghosted machine that reads no ghost holds of the original;
14
+ - `some_of_ghostmap`: the laws of the ghosted machine give, at every reachable state of the original, ghost values and
15
+ ghost maps (Skolem functions of the keys) that agree with the state elsewhere and where every law holds;
16
+ - `forall_some_of_ghostmap`: the ∀∃ reading. A law about every key kv that reads the ghost map W only at one key term t
17
+ gives: for every value of kv, some natural x satisfies the law with each read of W replaced by x (`substRC [] (rdW W
18
+ x)`), a statement about the original alone when that law reads no other ghost.
19
+ -/
20
+
21
+ namespace Kern
22
+ open Classical
23
+
24
+ structure SGhostedM where
25
+ orig : SMachine
26
+ ghosts : List (Nat × E × E) -- ghost values: (variable, start over the params, step), as SGhosted's
27
+ gmaps : List (Nat × Nat) -- ghost maps: (map, its value at every key at the start)
28
+ gwrites : List (Nat × E × E) -- (ghost map, key, value): a write at one key
29
+ gevery : List (Nat × Nat × E) -- (ghost map, bound key, value): a write at every key
30
+
31
+ def SGhostedM.vars (g : SGhostedM) : List Nat := g.ghosts.map (·.1)
32
+ def SGhostedM.maps (g : SGhostedM) : List Nat := g.gmaps.map (·.1)
33
+
34
+ -- The ghosted machine: the original, each ghost's start and step added after the original's.
35
+ def SGhostedM.full (g : SGhostedM) : SMachine :=
36
+ ⟨⟨⟨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 ++ g.gmaps,
37
+ g.orig.f.m.step ++ g.ghosts.map (fun x => (x.1, x.2.2)), g.orig.f.m.writes ++ g.gwrites, g.orig.f.m.pre, g.orig.f.m.assume⟩, g.orig.f.flags⟩,
38
+ g.orig.carried, g.orig.every ++ g.gevery, g.orig.qflags, g.orig.wits, g.orig.facts, g.orig.pfacts⟩
39
+
40
+ -- The original's maps, in any role: started, written at a key, written at every key, carried.
41
+ def origMaps (s : SMachine) : List Nat := s.f.m.maps0.map (·.1) ++ s.f.m.writes.map (·.1) ++ s.every.map (·.1) ++ s.carried
42
+
43
+ -- Reads none of the variables V and none of the maps MS.
44
+ def apartC (V MS : List Nat) (c : C) : Bool := avoids V c && avoidsMaps MS c
45
+ def apartE (V MS : List Nat) (e : E) : Bool := avoidsE V e && avoidsMapsE MS e
46
+
47
+ -- The ghosts are new names (variables and maps), the ghost writes write ghost maps, and no part of the original reads a
48
+ -- ghost.
49
+ def smghostOk (g : SGhostedM) : Bool :=
50
+ distinct g.vars && distinct g.maps
51
+ && 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)
52
+ && g.maps.all (fun mp => !(origMaps g.orig).contains mp)
53
+ && (g.gwrites.map (·.1) ++ g.gevery.map (·.1)).all (fun mp => g.maps.contains mp)
54
+ && apartC g.vars g.maps g.orig.f.m.pre && apartC g.vars g.maps g.orig.f.m.assume
55
+ && g.orig.f.flags.all (fun p => apartC g.vars g.maps p.2) && g.orig.qflags.all (fun q => apartC g.vars g.maps q.2.2)
56
+ && g.orig.f.m.init.all (fun p => apartE g.vars g.maps p.2) && g.orig.f.m.step.all (fun p => apartE g.vars g.maps p.2)
57
+ && g.orig.f.m.writes.all (fun w => apartE g.vars g.maps w.2.1 && apartE g.vars g.maps w.2.2)
58
+ && g.orig.every.all (fun w => apartE g.vars g.maps w.2.2)
59
+ && g.orig.facts.all (fun l => apartC g.vars g.maps l.holds) && g.orig.pfacts.all (fun f => apartC g.vars g.maps f.1.holds)
60
+
61
+ /-! Reading alike outside the ghosts -/
62
+
63
+ theorem apartC_parts {V MS : List Nat} {c : C} (h : apartC V MS c = true) : avoids V c = true ∧ avoidsMaps MS c = true := by
64
+ simpa [apartC] using h
65
+ theorem apartE_parts {V MS : List Nat} {e : E} (h : apartE V MS e = true) : avoidsE V e = true ∧ avoidsMapsE MS e = true := by
66
+ simpa [apartE] using h
67
+
68
+ theorem evalC_apart {V MS : List Nat} {c : C} (h : apartC V MS c = true) {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat}
69
+ (hag : Agree V ρ ρ') (hm : ∀ mp, mp ∉ MS → M mp = M' mp) : evalC ρ M c = evalC ρ' M' c := by
70
+ obtain ⟨hv, hmm⟩ := apartC_parts h
71
+ rw [congrC ρ ρ' M c (fun i hi => hag i (avoids_occ hv hi)), congrMC ρ' M M' c (fun mp hmp => hm mp (avoidsMaps_occ hmm hmp))]
72
+
73
+ theorem evalE_apart {V MS : List Nat} {e : E} (h : apartE V MS e = true) {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat}
74
+ (hag : Agree V ρ ρ') (hm : ∀ mp, mp ∉ MS → M mp = M' mp) : evalE ρ M e = evalE ρ' M' e := by
75
+ obtain ⟨hv, hmm⟩ := apartE_parts h
76
+ rw [congrE ρ ρ' M e (fun i hi => hag i (avoidsE_occ hv hi)), congrME ρ' M M' e (fun mp hmp => hm mp (avoidsMapsE_occ hmm hmp))]
77
+
78
+ -- Flags decided over states and maps that agree outside the ghosts, by conditions that read no ghost, agree there.
79
+ theorem decide_apart {fl : List (Nat × C)} {V MS : List Nat} (hav : ∀ p ∈ fl, apartC V MS p.2 = true) {X X' : Nat → Nat}
80
+ {N N' : Nat → Nat → Nat} (h : Agree V X X') (hN : ∀ mp, mp ∉ MS → N mp = N' mp) : Agree V (decideFlags fl X N) (decideFlags fl X' N') := by
81
+ intro i hi
82
+ simp only [decideFlags, applyS, assoc_flagSub]
83
+ cases hc : flagOf fl i with
84
+ | none => exact h i hi
85
+ | some c =>
86
+ obtain ⟨p, hp, rfl⟩ := flagOf_some hc
87
+ simp only [Option.map_some, evalE]
88
+ rw [evalC_apart (hav p hp) h hN]
89
+
90
+ theorem lookup_append_none {α : Type} (p : α → Bool) (l1 l2 : List α) (h : lookup p l2 = none) : lookup p (l1 ++ l2) = lookup p l1 := by
91
+ rw [lookup_append]; cases lookup p l1 <;> simp [h]
92
+
93
+ -- The ghost maps at their starts, every other map as it is.
94
+ def startWith (gm : List (Nat × Nat)) (M : Nat → Nat → Nat) : Nat → Nat → Nat := fun mp =>
95
+ match lookup (fun p => p.1 == mp) gm with
96
+ | some p => fun _ => p.2
97
+ | none => M mp
98
+
99
+ -- The facts about several keys, read where maps agree on what they read.
100
+ theorem holdsOver_maps {l : MLaw} {MS : List Nat} (hm : avoidsMaps MS l.holds = true) {M M' : Nat → Nat → Nat}
101
+ (hmag : ∀ mp, mp ∉ MS → M mp = M' mp) : ∀ (gs : List Nat) (ρ : Nat → Nat), holdsOver l M' gs ρ → holdsOver l M gs ρ
102
+ | [], ρ, h => holds_transfer (V := []) l (by simp [avoids]) hm (fun _ _ => rfl) hmag h
103
+ | _ :: gs, ρ, h => fun w => holdsOver_maps hm hmag gs _ (h w)
104
+
105
+ section smghost
106
+ variable (g : SGhostedM) (hg : smghostOk g = true)
107
+ include hg
108
+
109
+ theorem smghostOk_parts :
110
+ distinct g.vars = true ∧ distinct g.maps = true
111
+ ∧ (∀ mp ∈ g.maps, mp ∉ origMaps g.orig)
112
+ ∧ (∀ mp ∈ g.gwrites.map (·.1) ++ g.gevery.map (·.1), mp ∈ g.maps)
113
+ ∧ apartC g.vars g.maps g.orig.f.m.pre = true ∧ apartC g.vars g.maps g.orig.f.m.assume = true
114
+ ∧ (∀ p ∈ g.orig.f.flags, apartC g.vars g.maps p.2 = true) ∧ (∀ q ∈ g.orig.qflags, apartC g.vars g.maps q.2.2 = true)
115
+ ∧ (∀ p ∈ g.orig.f.m.init, apartE g.vars g.maps p.2 = true) ∧ (∀ p ∈ g.orig.f.m.step, apartE g.vars g.maps p.2 = true)
116
+ ∧ (∀ w ∈ g.orig.f.m.writes, apartE g.vars g.maps w.2.1 = true ∧ apartE g.vars g.maps w.2.2 = true)
117
+ ∧ (∀ w ∈ g.orig.every, apartE g.vars g.maps w.2.2 = true)
118
+ ∧ (∀ l ∈ g.orig.facts, apartC g.vars g.maps l.holds = true) ∧ (∀ f ∈ g.orig.pfacts, apartC g.vars g.maps f.1.holds = true) := by
119
+ simp only [smghostOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at hg
120
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hdv, hdm⟩, _⟩, hom⟩, hw⟩, hp⟩, ha⟩, hf⟩, hq⟩, hi⟩, hs⟩, hwr⟩, he⟩, hfa⟩, hpf⟩ := hg
121
+ exact ⟨hdv, hdm, fun mp hm => by simpa using hom mp hm, fun mp hm => by simpa using hw mp hm, hp, ha, hf, hq, hi, hs, hwr, he, hfa, hpf⟩
122
+
123
+ /-- One step from states and maps that agree outside the ghosts, with the same request: allowed alike, and to states
124
+ and maps that agree outside the ghosts. -/
125
+ theorem smghost_step {ρg ρ : Nat → Nat} {Mg M : Nat → Nat → Nat} (hag : Agree g.vars ρg ρ) (hmag : ∀ mp, mp ∉ g.maps → Mg mp = M mp)
126
+ (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
127
+ evalC (sView g.full ρg Mg ι κ).1 (sView g.full ρg Mg ι κ).2 g.full.f.m.assume = evalC (sView g.orig ρ M ι κ).1 (sView g.orig ρ M ι κ).2 g.orig.f.m.assume
128
+ ∧ Agree g.vars (sNext g.full ρg Mg ι κ).1 (sNext g.orig ρ M ι κ).1
129
+ ∧ ∀ mp, mp ∉ g.maps → (sNext g.full ρg Mg ι κ).2 mp = (sNext g.orig ρ M ι κ).2 mp := by
130
+ obtain ⟨_, _, _, hw, _, ha, hf, hq, _, hs, hwr, he, _, _⟩ := smghostOk_parts g hg
131
+ have hX : Agree g.vars (withInputs g.orig.f.m.inputs ρg ι) (withInputs g.orig.f.m.inputs ρ ι) := withInputs_agree hag ι
132
+ have hN : ∀ mp, mp ∉ g.maps → withCarried g.orig Mg κ mp = withCarried g.orig M κ mp := fun mp hmp => by
133
+ simp only [withCarried]; split
134
+ · rfl
135
+ · exact hmag mp hmp
136
+ have hQ : Agree g.vars (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig Mg κ))
137
+ (decideQ g.orig (withInputs g.orig.f.m.inputs ρ ι) (withCarried g.orig M κ)) := by
138
+ intro v hv
139
+ rw [decideQ_of, decideQ_of]
140
+ cases hl : lookup (fun q => q.1 == v) g.orig.qflags with
141
+ | some q =>
142
+ obtain ⟨hqv, hqm⟩ := apartC_parts (hq q (lookup_some hl).1)
143
+ exact ite_all_congr (fun i hi _ => hX i (avoids_occ hqv hi)) (fun mp hm => hN mp (avoidsMaps_occ hqm hm))
144
+ | none => exact hX v hv
145
+ have hD := decide_apart hf hQ hN
146
+ -- The ghosted machine's lists are the original's with the ghosts' after them.
147
+ have hstep : g.full.f.m.step = g.orig.f.m.step ++ g.ghosts.map (fun x => (x.1, x.2.2)) := rfl
148
+ have hevery : g.full.every = g.orig.every ++ g.gevery := rfl
149
+ have hwrites : g.full.f.m.writes = g.orig.f.m.writes ++ g.gwrites := rfl
150
+ refine ⟨?_, fun i hi => ?_, fun mp hmp => ?_⟩
151
+ · show evalC (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ) g.orig.f.m.assume = _
152
+ exact evalC_apart ha hD hN
153
+ · show applyS g.full.f.m.step (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ) i = _
154
+ rw [hstep, applyS_append_other (by simpa [SGhostedM.vars] using hi)]
155
+ simp only [sNext, sView, applyS]
156
+ cases hst : assoc g.orig.f.m.step i with
157
+ | none => exact hD i hi
158
+ | some e =>
159
+ have hm : (i, e) ∈ g.orig.f.m.step := by
160
+ simp only [assoc, Option.map_eq_some_iff] at hst
161
+ obtain ⟨q, hq', rfl⟩ := hst
162
+ obtain ⟨hqm, hqv⟩ := lookup_some hq'
163
+ simp only [beq_iff_eq] at hqv
164
+ rw [← hqv]; exact hqm
165
+ exact evalE_apart (hs (i, e) hm) hD hN
166
+ · funext key
167
+ show everyMaps g.full (decideFlags g.orig.f.flags (decideQ g.orig (withInputs g.orig.f.m.inputs ρg ι) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ)) (withCarried g.orig Mg κ) mp key
168
+ = 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
169
+ have hge : lookup (fun w => w.1 == mp) g.gevery = none :=
170
+ lookup_none (fun w hw' => by
171
+ simp only [beq_eq_false_iff_ne]; intro he'
172
+ exact hmp (by rw [← he']; exact hw w.1 (List.mem_append_right _ (List.mem_map_of_mem hw'))))
173
+ have hgw : lookup (fun w => w.1 == mp) g.gwrites = none :=
174
+ lookup_none (fun w hw' => by
175
+ simp only [beq_eq_false_iff_ne]; intro he'
176
+ exact hmp (by rw [← he']; exact hw w.1 (List.mem_append_left _ (List.mem_map_of_mem hw'))))
177
+ simp only [everyMaps, hevery, lookup_append_none _ g.orig.every _ hge]
178
+ cases hl : lookup (fun w => w.1 == mp) g.orig.every with
179
+ | some w => exact evalE_apart (he w (lookup_some hl).1) (upd_agree hD w.2.1 key) hN
180
+ | none =>
181
+ simp only [writeMaps, hwrites, lookup_append_none _ g.orig.f.m.writes _ hgw]
182
+ cases hl2 : lookup (fun w => w.1 == mp) g.orig.f.m.writes with
183
+ | none => exact congrFun (hN mp hmp) key
184
+ | some w =>
185
+ obtain ⟨h1, h2⟩ := hwr w (lookup_some hl2).1
186
+ dsimp only
187
+ rw [evalE_apart h1 hD hN, evalE_apart h2 hD hN, hN mp hmp]
188
+
189
+ /-- Every reachable state of the original is, outside the ghost variables and the ghost maps, a reachable state of the
190
+ ghosted machine. -/
191
+ theorem smghost_reach : ∀ {ρ M}, SReach g.orig ρ M → ∃ ρg Mg, SReach g.full ρg Mg ∧ Agree g.vars ρg ρ ∧ ∀ mp, mp ∉ g.maps → Mg mp = M mp := by
192
+ obtain ⟨_, hdm, hom, _, hp, _, _, _, hi, _, _, _, hfa, hpf⟩ := smghostOk_parts g hg
193
+ intro ρ M hr
194
+ induction hr with
195
+ | start ρ0 M0 hM h0 hfa0 hpf0 =>
196
+ have hout : ∀ mp, mp ∉ g.maps → startWith g.gmaps M0 mp = M0 mp := fun mp hmp => by
197
+ have hl : lookup (fun p => p.1 == mp) g.gmaps = none := lookup_none (fun p hp' => by
198
+ simp only [beq_eq_false_iff_ne]; intro he'; exact hmp (by rw [← he']; exact List.mem_map_of_mem hp'))
199
+ simp only [startWith, hl]
200
+ have hstart : StartMaps g.full.f.m (startWith g.gmaps M0) := by
201
+ intro p hp' key
202
+ rcases List.mem_append.mp hp' with hp' | hp'
203
+ · have hpo : p.1 ∉ g.maps := fun hm => hom p.1 hm (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.mem_map_of_mem hp'))))
204
+ rw [hout p.1 hpo]; exact hM p hp' key
205
+ · have hl : lookup (fun q => q.1 == p.1) g.gmaps = some p := lookup_unique (by simpa [SGhostedM.maps] using hdm) hp'
206
+ simp only [startWith, hl]
207
+ obtain ⟨_, hpm⟩ := apartC_parts hp
208
+ refine ⟨applyS g.full.f.m.init ρ0 (startWith g.gmaps M0), startWith g.gmaps M0, SReach.start ρ0 _ hstart ?_ ?_ ?_, fun i hi' => ?_, hout⟩
209
+ · show evalC ρ0 (startWith g.gmaps M0) g.orig.f.m.pre = true
210
+ rw [congrMC ρ0 _ M0 _ (fun mp hm => hout mp (avoidsMaps_occ hpm hm))]; exact h0
211
+ · intro l hl
212
+ exact holds_transfer (V := []) l (by simp [avoids]) (apartC_parts (hfa l hl)).2 (fun _ _ => rfl) hout (hfa0 l hl)
213
+ · intro f hf
214
+ exact holdsOver_maps (apartC_parts (hpf f hf)).2 hout f.2 ρ0 (hpf0 f hf)
215
+ · show applyS (g.orig.f.m.init ++ g.ghosts.map (fun x => (x.1, x.2.1))) ρ0 (startWith g.gmaps M0) i = applyS g.orig.f.m.init ρ0 M0 i
216
+ rw [applyS_append_other (by simpa [SGhostedM.vars] using hi')]
217
+ simp only [applyS]
218
+ cases hst : assoc g.orig.f.m.init i with
219
+ | none => rfl
220
+ | some e =>
221
+ have hm : (i, e) ∈ g.orig.f.m.init := by
222
+ simp only [assoc, Option.map_eq_some_iff] at hst
223
+ obtain ⟨q, hq', rfl⟩ := hst
224
+ obtain ⟨hqm, hqv⟩ := lookup_some hq'
225
+ simp only [beq_iff_eq] at hqv
226
+ rw [← hqv]; exact hqm
227
+ exact evalE_apart (hi (i, e) hm) (fun _ _ => rfl) hout
228
+ | step ρ M ι κ _ ha ih =>
229
+ obtain ⟨ρg, Mg, hrg, hag, hmag⟩ := ih
230
+ obtain ⟨hA, hS, hW⟩ := smghost_step g hg hag hmag ι κ
231
+ exact ⟨_, _, SReach.step ρg Mg ι κ hrg (by rw [hA]; exact ha), hS, hW⟩
232
+
233
+ /-- A law of the ghosted machine that reads no ghost holds in every reachable state of the original. -/
234
+ theorem smghost_laws (ps : List SEntry) (hl : checkSMachine g.full ps = true) :
235
+ ∀ ρ M, SReach g.orig ρ M → ∀ e ∈ ps, avoids g.vars e.law.holds = true → avoidsMaps g.maps e.law.holds = true → e.Holds ρ M := by
236
+ intro ρ M hr e he hav ham
237
+ obtain ⟨ρg, Mg, hrg, hag, hmag⟩ := smghost_reach g hg hr
238
+ exact sentry_transfer (V := g.vars) (MS := g.maps) hav ham (fun i hi => (hag i hi).symm) (fun mp hmp => (hmag mp hmp).symm)
239
+ (smachine_sound g.full ps hl ρg Mg hrg e he)
240
+
241
+ /-- The laws of the ghosted machine, at every reachable state of the original: some ghost values and some ghost maps
242
+ (Skolem functions of the keys), the state and maps as they are elsewhere, where every law holds. -/
243
+ theorem some_of_ghostmap (ps : List SEntry) (hl : checkSMachine g.full ps = true) :
244
+ ∀ ρ M, SReach g.orig ρ M → ∃ ρg Mg, Agree g.vars ρg ρ ∧ (∀ mp, mp ∉ g.maps → Mg mp = M mp) ∧ ∀ e ∈ ps, e.Holds ρg Mg := by
245
+ intro ρ M hr
246
+ obtain ⟨ρg, Mg, hrg, hag, hmag⟩ := smghost_reach g hg hr
247
+ exact ⟨ρg, Mg, hag, hmag, smachine_sound g.full ps hl ρg Mg hrg⟩
248
+ end smghost
249
+
250
+ /-! The ∀∃ reading: a ghost map read at one key, as a natural -/
251
+
252
+ -- Every read of the map W is at the key term t.
253
+ mutual
254
+ def atKeyE (W : Nat) (t : E) : E → Bool
255
+ | .var _ => true
256
+ | .const _ => true
257
+ | .add a b => atKeyE W t a && atKeyE W t b
258
+ | .sub a b => atKeyE W t a && atKeyE W t b
259
+ | .mul _ a => atKeyE W t a
260
+ | .min a b => atKeyE W t a && atKeyE W t b
261
+ | .max a b => atKeyE W t a && atKeyE W t b
262
+ | .prod a b => atKeyE W t a && atKeyE W t b
263
+ | .ite c a b => atKeyC W t c && atKeyE W t a && atKeyE W t b
264
+ | .get m key => if m == W then beqE key t else atKeyE W t key
265
+ | .tup a b => atKeyE W t a && atKeyE W t b
266
+ | .proj _ a => atKeyE W t a
267
+ def atKeyC (W : Nat) (t : E) : C → Bool
268
+ | .le a b => atKeyE W t a && atKeyE W t b
269
+ | .lt a b => atKeyE W t a && atKeyE W t b
270
+ | .eq a b => atKeyE W t a && atKeyE W t b
271
+ | .and a b => atKeyC W t a && atKeyC W t b
272
+ | .or a b => atKeyC W t a && atKeyC W t b
273
+ | .not a => atKeyC W t a
274
+ end
275
+
276
+ -- A read of W as the variable x; any other read as it is.
277
+ def rdW (W x : Nat) : Nat → E → E := fun m k => if m == W then .var x else .get m k
278
+
279
+ -- The maps with W the constant v.
280
+ def withConst (W v : Nat) (M : Nat → Nat → Nat) : Nat → Nat → Nat := fun m => if m == W then fun _ => v else M m
281
+
282
+ section atkey
283
+ variable (W : Nat) (t : E) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
284
+ mutual
285
+ theorem atKey_evalE : (e : E) → atKeyE W t e = true → evalE ρ M e = evalE ρ (withConst W (M W (evalE ρ M t)) M) e
286
+ | .var _, _ => rfl
287
+ | .const _, _ => rfl
288
+ | .add a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
289
+ | .sub a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
290
+ | .mul _ a, h => by simp only [atKeyE] at h; simp only [evalE, atKey_evalE a h]
291
+ | .min a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
292
+ | .max a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
293
+ | .prod a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
294
+ | .ite c a b, h => by
295
+ simp only [atKeyE, Bool.and_eq_true] at h
296
+ simp only [evalE, atKey_evalC c h.1.1, atKey_evalE a h.1.2, atKey_evalE b h.2]
297
+ | .get m key, h => by
298
+ simp only [atKeyE] at h
299
+ by_cases hm : m = W
300
+ · subst hm
301
+ simp only [beq_self_eq_true, ite_true] at h
302
+ rw [beqE_eq h]
303
+ simp [evalE, withConst]
304
+ · have hb : (m == W) = false := by simp [hm]
305
+ simp only [hb, Bool.false_eq_true, ite_false] at h
306
+ simp only [evalE, withConst, hb, Bool.false_eq_true, ite_false, ← atKey_evalE key h]
307
+ | .tup a b, h => by simp only [atKeyE, Bool.and_eq_true] at h; simp only [evalE, atKey_evalE a h.1, atKey_evalE b h.2]
308
+ | .proj _ a, h => by simp only [atKeyE] at h; simp only [evalE, atKey_evalE a h]
309
+ theorem atKey_evalC : (c : C) → atKeyC W t c = true → evalC ρ M c = evalC ρ (withConst W (M W (evalE ρ M t)) M) c
310
+ | .le a b, h => by simp only [atKeyC, Bool.and_eq_true] at h; simp only [evalC, atKey_evalE a h.1, atKey_evalE b h.2]
311
+ | .lt a b, h => by simp only [atKeyC, Bool.and_eq_true] at h; simp only [evalC, atKey_evalE a h.1, atKey_evalE b h.2]
312
+ | .eq a b, h => by simp only [atKeyC, Bool.and_eq_true] at h; simp only [evalC, atKey_evalE a h.1, atKey_evalE b h.2]
313
+ | .and a b, h => by simp only [atKeyC, Bool.and_eq_true] at h; simp only [evalC, atKey_evalC a h.1, atKey_evalC b h.2]
314
+ | .or a b, h => by simp only [atKeyC, Bool.and_eq_true] at h; simp only [evalC, atKey_evalC a h.1, atKey_evalC b h.2]
315
+ | .not a, h => by simp only [atKeyC] at h; simp only [evalC, atKey_evalC a h]
316
+ end
317
+ end atkey
318
+
319
+ -- A condition that reads W only at t, with each read of W replaced by a fresh x, read where x is W's value at t.
320
+ theorem rdW_evalC (W x : Nat) (t : E) (c : C) (hat : atKeyC W t c = true) (hx : occC x c = false) (ρ : Nat → Nat) (M : Nat → Nat → Nat) :
321
+ evalC (upd ρ x (M W (evalE ρ M t))) M (substRC [] (rdW W x) c) = evalC ρ M c := by
322
+ have hrd : ∀ m k, evalE (upd ρ x (M W (evalE ρ M t))) M (rdW W x m k)
323
+ = withConst W (M W (evalE ρ M t)) M m (evalE (upd ρ x (M W (evalE ρ M t))) M k) := by
324
+ intro m k
325
+ by_cases hm : m = W
326
+ · subst hm; simp [rdW, withConst, evalE, upd]
327
+ · have hb : (m == W) = false := by simp [hm]
328
+ simp [rdW, withConst, evalE, hb]
329
+ rw [substR_evalC [] (rdW W x) _ M _ hrd c, applyS_nil, atKey_evalC W t ρ M c hat]
330
+ exact congrC _ _ _ c (fun i hi => by
331
+ simp only [upd]; split
332
+ · rename_i hix; subst hix; simp [hx] at hi
333
+ · rfl)
334
+
335
+ /-- The ∀∃ reading of a ghost map: a law of the ghosted machine about every value of kv (no further keys) that reads
336
+ the ghost map W only at the key term t gives, at every reachable state of the original, for every value of kv some
337
+ natural x where the law holds with each read of W replaced by x; when that condition reads no ghost, it is a statement
338
+ about the original alone. -/
339
+ theorem forall_some_of_ghostmap (g : SGhostedM) (hg : smghostOk g = true) (ps : List SEntry) (hl : checkSMachine g.full ps = true)
340
+ (e : SEntry) (he : e ∈ ps) (kv : Nat) (hk : e.law.key = some kv) (hk2 : e.keys2 = []) (W x : Nat) (t : E)
341
+ (hat : atKeyC W t e.law.holds = true) (hx : occC x e.law.holds = false)
342
+ (hav : avoids g.vars (substRC [] (rdW W x) e.law.holds) = true) (ham : avoidsMaps g.maps (substRC [] (rdW W x) e.law.holds) = true) :
343
+ ∀ ρ M, SReach g.orig ρ M → ∀ v, ∃ val, evalC (upd (upd ρ kv v) x val) M (substRC [] (rdW W x) e.law.holds) = true := by
344
+ intro ρ M hr v
345
+ obtain ⟨ρg, Mg, hrg, hag, hmag⟩ := smghost_reach g hg hr
346
+ have hall := smachine_sound g.full ps hl ρg Mg hrg e he
347
+ simp only [SEntry.Holds, hk2, holdsOver, MLaw.Holds, hk] at hall
348
+ refine ⟨Mg W (evalE (upd ρg kv v) Mg t), ?_⟩
349
+ have h1 := rdW_evalC W x t e.law.holds hat hx (upd ρg kv v) Mg
350
+ rw [hall v] at h1
351
+ have hagx : Agree g.vars (upd (upd ρ kv v) x (Mg W (evalE (upd ρg kv v) Mg t))) (upd (upd ρg kv v) x (Mg W (evalE (upd ρg kv v) Mg t))) :=
352
+ upd_agree (upd_agree (fun i hi => (hag i hi).symm) kv v) x _
353
+ rw [congrC _ _ M _ (fun i hi => hagx i (avoids_occ hav hi)),
354
+ congrMC _ M Mg _ (fun mp hmp => (hmag mp (avoidsMaps_occ ham hmp)).symm)]
355
+ exact h1
356
+
357
+ /-! The ∃ reading of witness terms over the ghosts: any number of bound naturals, any number of keys -/
358
+
359
+ -- An ∃ law at a state, read as MLaw.Holds and holdsOver read a law: each further key at every value, then its key at
360
+ -- every value, then some natural for each of xs (written over the state) where c holds.
361
+ def ExHolds (c : C) (key : Option Nat) (xs : List Nat) (M : Nat → Nat → Nat) : List Nat → (Nat → Nat) → Prop
362
+ | [], ρ => match key with
363
+ | none => ∃ f : Nat → Nat, evalC (updAll ρ (xs.map fun i => (i, f i))) M c = true
364
+ | some kv => ∀ v, ∃ f : Nat → Nat, evalC (updAll (upd ρ kv v) (xs.map fun i => (i, f i))) M c = true
365
+ | g :: gs, ρ => ∀ w, ExHolds c key xs M gs (upd ρ g w)
366
+
367
+ theorem updAll_agree {V : List Nat} {X X' : Nat → Nat} (h : Agree V X X') : ∀ (l : List (Nat × Nat)), Agree V (updAll X l) (updAll X' l)
368
+ | [] => h
369
+ | p :: ps => updAll_agree (upd_agree h p.1 p.2) ps
370
+
371
+ -- Witness terms σ in place, read over a ghosted state: the terms' values there, written over a state that agrees with
372
+ -- it outside the ghosts, satisfy c where c reads no ghost.
373
+ theorem some_of_subst_apart {V MS : List Nat} (σ : List (Nat × E)) (c : C) (hav : avoids V c = true) (ham : avoidsMaps MS c = true)
374
+ {ρg ρ : Nat → Nat} {Mg M : Nat → Nat → Nat} (hag : Agree V ρg ρ) (hmag : ∀ mp, mp ∉ MS → Mg mp = M mp)
375
+ (h : evalC ρg Mg (substC σ c) = true) :
376
+ ∃ f : Nat → Nat, evalC (updAll ρ ((σ.map (·.1)).map fun i => (i, f i))) M c = true := by
377
+ refine ⟨fun i => applyS σ ρg Mg i, ?_⟩
378
+ have h1 := some_of_subst σ c ρg Mg h
379
+ have hx := updAll_agree hag ((σ.map (·.1)).map fun i => (i, applyS σ ρg Mg i))
380
+ rw [congrC _ _ M _ (fun i hi => (hx i (avoids_occ hav hi)).symm),
381
+ congrMC _ M Mg _ (fun mp hmp => (hmag mp (avoidsMaps_occ ham hmp)).symm)]
382
+ exact h1
383
+
384
+ /-- The ∀∃ reading of witness terms over ghosts: a law of the ghosted machine that is c with each bound natural x
385
+ replaced by a term σ x (`substC σ c`), where c reads no ghost (variable or map), gives at every reachable state of the
386
+ original, for every value of its keys, some natural for each bound name where c holds: a statement about the original
387
+ alone. A term may read anything of the ghosted machine (ghost maps at any key, the state, the law's keys), so one
388
+ ghost map per bound name (forall_some_of_ghostmap's), several of them, a disjunction of ∃s (their bound names together)
389
+ and a witness built of ghost reads are all instances. -/
390
+ theorem forall_some_of_ghostsubst (g : SGhostedM) (hg : smghostOk g = true) (ps : List SEntry) (hl : checkSMachine g.full ps = true)
391
+ (e : SEntry) (he : e ∈ ps) (σ : List (Nat × E)) (c : C) (hsub : e.law.holds = substC σ c)
392
+ (hav : avoids g.vars c = true) (ham : avoidsMaps g.maps c = true) :
393
+ ∀ ρ M, SReach g.orig ρ M → ExHolds c e.law.key (σ.map (·.1)) M e.keys2 ρ := by
394
+ intro ρ M hr
395
+ obtain ⟨ρg, Mg, hrg, hag, hmag⟩ := smghost_reach g hg hr
396
+ have hall := smachine_sound g.full ps hl ρg Mg hrg e he
397
+ simp only [SEntry.Holds] at hall
398
+ -- (along the further keys, each at the same value on both sides)
399
+ have go : ∀ (gs : List Nat) (ρg ρ : Nat → Nat), Agree g.vars ρg ρ → holdsOver e.law Mg gs ρg → ExHolds c e.law.key (σ.map (·.1)) M gs ρ := by
400
+ intro gs
401
+ induction gs with
402
+ | nil =>
403
+ intro ρg ρ hag h
404
+ simp only [holdsOver, MLaw.Holds] at h
405
+ cases hk : e.law.key with
406
+ | none =>
407
+ simp only [hk, hsub] at h
408
+ simp only [ExHolds]
409
+ exact some_of_subst_apart σ c hav ham hag hmag h
410
+ | some kv =>
411
+ simp only [hk, hsub] at h
412
+ simp only [ExHolds]
413
+ intro v
414
+ exact some_of_subst_apart σ c hav ham (upd_agree hag kv v) hmag (h v)
415
+ | cons k gs ih =>
416
+ intro ρg ρ hag h w
417
+ exact ih (upd ρg k w) (upd ρ k w) (upd_agree hag k w) (h w)
418
+ exact go e.keys2 ρg ρ hag hall
419
+
420
+ end Kern
421
+
422
+ #print axioms Kern.forall_some_of_ghostsubst
423
+ #print axioms Kern.smghost_reach
424
+ #print axioms Kern.smghost_laws
425
+ #print axioms Kern.some_of_ghostmap
426
+ #print axioms Kern.forall_some_of_ghostmap