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,1975 @@
1
+ import Steps
2
+
3
+ /-!
4
+ Sets: kernel.ts's machines with sets, proven sound. A set is a map with cells 0 or 1; three things go beyond FMMachine:
5
+ a request carries whole maps (`carried`: any maps at each request), a step writes a map at every key at once (`every`:
6
+ the value at each key, read with the key bound), and a flag may hold at every key (`qflags`: ∀ key. body, its body
7
+ reading no flag), or at every tuple of keys (∀ k₁ … kₙ. body). Lean decides a quantified flag classically, which only the meaning needs; the checker never does.
8
+
9
+ Functions from outside the rule are maps no step writes (and no request carries); `facts` are laws about the params and
10
+ those maps alone, given rather than proven: a run starts where they hold, so they hold at every state, and every goal may
11
+ assume them at the keys its proof names (`finsts`). A fact about several keys (`pfacts`: a function of several
12
+ arguments, a law between two points) is given at every value of its further keys, as a law about several keys holds,
13
+ and a goal assumes it at the tuples its proof names (`pfinsts`).
14
+
15
+ kernel.ts closes each quantified flag ?f in a goal: ?f ≤ 1; ?f = 0 ∨ body(t) at keys t the proof names (`qinsts`, a
16
+ tuple of terms over several keys); and ?f = 1 ∨ ¬body(w) at fresh witnesses w, one per key (`wits`). A real step
17
+ satisfies these with w failing keys when ?f = 0, so a goal that refutes them refutes the machine. Lean builds the same
18
+ closed goals.
19
+ -/
20
+
21
+ namespace Kern
22
+ open Classical
23
+
24
+ /-! Certificates that name their hypotheses -/
25
+
26
+
27
+ -- A proof tree with the table it reads, and the hypotheses it refutes: none named, the goal's own, in the goal's order;
28
+ -- named, any of the goal's, in any order (each must be one of the goal's), so an exporter may lay its goal out its own
29
+ -- way as long as each hypothesis it uses is one Lean builds.
30
+ abbrev Cert := List VDef × List C × Tree
31
+ -- The goal's hypotheses after the first one equal to c, if there is one.
32
+ def afterFirst (c : C) : List C → Option (List C)
33
+ | [] => none
34
+ | h :: t => if beqC c h then some t else afterFirst c t
35
+ -- Each named hypothesis is one of the goal's: looked for after the last one found (exporters name them mostly in the
36
+ -- goal's order), and from the start when it is not there.
37
+ def namedIn (hyps : List C) : List C → List C → Bool
38
+ | [], _ => true
39
+ | c :: cs, rest => match afterFirst c rest with
40
+ | some rest' => namedIn hyps cs rest'
41
+ | none => match afterFirst c hyps with
42
+ | some rest' => namedIn hyps cs rest'
43
+ | none => false
44
+ def checkIn (hyps : List C) (k : Cert) : Bool :=
45
+ if k.2.1.isEmpty then checkGoal ⟨k.1, hyps⟩ k.2.2
46
+ else namedIn hyps k.2.1 hyps && checkGoal ⟨k.1, k.2.1⟩ k.2.2
47
+
48
+ theorem afterFirst_some : ∀ {c : C} {l t : List C}, afterFirst c l = some t → c ∈ l ∧ ∀ x ∈ t, x ∈ l
49
+ | _, [], _, h => by simp [afterFirst] at h
50
+ | c, h' :: l, t, h => by
51
+ simp only [afterFirst] at h
52
+ split at h
53
+ · rename_i hb
54
+ simp only [Option.some.injEq] at h; subst h
55
+ exact ⟨by rw [beqC_eq hb]; exact List.mem_cons_self, fun x hx => List.mem_cons_of_mem _ hx⟩
56
+ · obtain ⟨h1, h2⟩ := afterFirst_some h
57
+ exact ⟨List.mem_cons_of_mem _ h1, fun x hx => List.mem_cons_of_mem _ (h2 x hx)⟩
58
+
59
+ theorem namedIn_sound (hyps : List C) : ∀ (cs rest : List C), (∀ x ∈ rest, x ∈ hyps) → namedIn hyps cs rest = true → ∀ c ∈ cs, c ∈ hyps
60
+ | [], _, _, _ => by simp
61
+ | c :: cs, rest, hr, h => by
62
+ simp only [namedIn] at h
63
+ split at h
64
+ · rename_i rest' hs
65
+ obtain ⟨h1, h2⟩ := afterFirst_some hs
66
+ intro x hx
67
+ rcases List.mem_cons.mp hx with rfl | hx
68
+ · exact hr _ h1
69
+ · exact namedIn_sound hyps cs rest' (fun y hy => hr y (h2 y hy)) h x hx
70
+ · split at h
71
+ · rename_i rest' hs
72
+ obtain ⟨h1, h2⟩ := afterFirst_some hs
73
+ intro x hx
74
+ rcases List.mem_cons.mp hx with rfl | hx
75
+ · exact h1
76
+ · exact namedIn_sound hyps cs rest' h2 h x hx
77
+ · simp at h
78
+
79
+ theorem sound_in (hyps : List C) (k : Cert) (h : checkIn hyps k = true) (env : Nat → Nat) (maps : Nat → Nat → Nat) :
80
+ ¬ ∀ c ∈ hyps, evalC env maps c = true := by
81
+ intro hH
82
+ unfold checkIn at h
83
+ split at h
84
+ · exact sound ⟨k.1, hyps⟩ k.2.2 h env maps hH
85
+ · simp only [Bool.and_eq_true] at h
86
+ exact sound ⟨k.1, k.2.1⟩ k.2.2 h.2 env maps (fun c hc => hH c (namedIn_sound hyps k.2.1 hyps (fun _ hx => hx) h.1 c hc))
87
+
88
+ structure SMachine where
89
+ f : FMMachine
90
+ carried : List Nat -- maps a request carries
91
+ every : List (Nat × Nat × E) -- (map, bound key, value): the map at every key
92
+ qflags : List (Nat × List Nat × C) -- (flag variable, bound keys, body): 1 when the body holds at every value of the keys, else 0
93
+ wits : List (Nat × List Nat) -- (flag variable, its witness variables: one per key)
94
+ facts : List MLaw -- given about the params and the functions (maps no step writes)
95
+ pfacts : List (MLaw × List Nat) -- facts about several keys: the fact about its first key, and its further keys
96
+
97
+ def SMachine.qVars (s : SMachine) : List Nat := s.qflags.map (·.1)
98
+ def SMachine.wVars (s : SMachine) : List Nat := s.wits.flatMap (·.2)
99
+
100
+ /-! The meaning -/
101
+
102
+ -- The maps a step reads: its own, with the maps the request carries.
103
+ def withCarried (s : SMachine) (M κ : Nat → Nat → Nat) : Nat → Nat → Nat := fun mp => if s.carried.contains mp then κ mp else M mp
104
+
105
+ -- A state with the bound keys read from v.
106
+ def atKeys (X : Nat → Nat) (ks : List Nat) (v : Nat → Nat) : Nat → Nat := fun i => if ks.contains i then v i else X i
107
+
108
+ theorem atKeys_in {X : Nat → Nat} {ks : List Nat} {v : Nat → Nat} {i : Nat} (h : i ∈ ks) : atKeys X ks v i = v i := by
109
+ simp [atKeys, List.contains_iff_mem, h]
110
+ theorem atKeys_out {X : Nat → Nat} {ks : List Nat} {v : Nat → Nat} {i : Nat} (h : i ∉ ks) : atKeys X ks v i = X i := by
111
+ simp [atKeys, List.contains_iff_mem, h]
112
+
113
+ -- Each quantified flag decided: 1 where its body holds at every value of its keys, else 0.
114
+ noncomputable def decideQ (s : SMachine) (X : Nat → Nat) (N : Nat → Nat → Nat) : Nat → Nat := fun v =>
115
+ match lookup (fun q => q.1 == v) s.qflags with
116
+ | some q => if ∀ k, holdsC (atKeys X q.2.1 k) N q.2.2 then 1 else 0
117
+ | none => X v
118
+
119
+ -- A step's state before its terms: inputs set, quantified flags decided, then the plain flags; and the maps it reads.
120
+ noncomputable def sView (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (Nat → Nat) × (Nat → Nat → Nat) :=
121
+ (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ), withCarried s M κ)
122
+
123
+ -- The maps after a step: a map written at every key, its value there; any other as kernel.ts's writes leave it.
124
+ def everyMaps (s : SMachine) (D : Nat → Nat) (N : Nat → Nat → Nat) : Nat → Nat → Nat := fun mp key =>
125
+ match lookup (fun w => w.1 == mp) s.every with
126
+ | some w => evalE (upd D w.2.1 key) N w.2.2
127
+ | none => writeMaps s.f.m.writes D N mp key
128
+
129
+ noncomputable def sNext (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (Nat → Nat) × (Nat → Nat → Nat) :=
130
+ (applyS s.f.m.step (sView s ρ M ι κ).1 (sView s ρ M ι κ).2, everyMaps s (sView s ρ M ι κ).1 (sView s ρ M ι κ).2)
131
+
132
+ -- A law at every value of each of some keys.
133
+ def holdsOver (l : MLaw) (M : Nat → Nat → Nat) : List Nat → (Nat → Nat) → Prop
134
+ | [], ρ => l.Holds ρ M
135
+ | g :: gs, ρ => ∀ w, holdsOver l M gs (upd ρ g w)
136
+
137
+ -- The facts about several keys, each at every value of its further keys.
138
+ def SMachine.PFacts (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop := ∀ p ∈ s.pfacts, holdsOver p.1 M p.2 ρ
139
+
140
+ inductive SReach (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → Prop
141
+ | start (ρ : Nat → Nat) (M : Nat → Nat → Nat) : StartMaps s.f.m M → evalC ρ M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ M) →
142
+ s.PFacts ρ M → SReach s (applyS s.f.m.init ρ M) M
143
+ | step (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : SReach s ρ M →
144
+ evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true → SReach s (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2
145
+
146
+ /-! The goals -/
147
+
148
+ -- Each quantified flag closed, at the tuples the proof names.
149
+ def closingHyps (s : SMachine) (qinsts : List (Nat × List E)) : List C :=
150
+ s.qflags.flatMap fun q =>
151
+ [.le (.var q.1) (.const 1)]
152
+ ++ (qinsts.filter (fun p => p.1 == q.1)).map (fun p => .or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip p.2) q.2.2))
153
+ ++ (match lookup (fun w => w.1 == q.1) s.wits with
154
+ | some w => [.or (.eq (.var q.1) (.const 1)) (.not (substC (q.2.1.zip (w.2.map .var)) q.2.2))]
155
+ | none => [])
156
+
157
+ def trueC : C := .le (.const 0) (.const 0)
158
+ def pairInsts : Nat → List (Nat × E × List E) → List (Nat × E)
159
+ | _, [] => []
160
+ | n, p :: ps => (n, p.2.1) :: pairInsts (n + 1) ps
161
+
162
+ -- A fact about several keys at a tuple, as a fact about its first key: each further key read as the tuple's term.
163
+ def pfactLaw (s : SMachine) (p : Nat × E × List E) : MLaw :=
164
+ match s.pfacts[p.1]? with
165
+ | some f => ⟨substC (f.2.zip p.2.2) f.1.holds, f.1.key⟩
166
+ | none => ⟨trueC, none⟩
167
+
168
+ -- The facts, at the keys the proof names, and the facts about several keys at the tuples it names.
169
+ def factHyps (s : SMachine) (finsts : List (Nat × E)) (pfinsts : List (Nat × E × List E)) : List C :=
170
+ scalarHyps s.facts ++ instHyps s.facts finsts ++ instHyps (pfinsts.map (pfactLaw s)) (pairInsts 0 pfinsts)
171
+
172
+ -- A map written at every key reads, after the step, its value at the key read; any other map as stepRd reads it.
173
+ def stepRdS (s : SMachine) (τ : List (Nat × E)) (rd : Nat → E → E) : Nat → E → E := fun mp k =>
174
+ match lookup (fun w => w.1 == mp) s.every with
175
+ | some w => substRE ((w.2.1, k) :: τ) rd (inlineE s.f.flags w.2.2)
176
+ | none => stepRd s.f τ rd mp k
177
+
178
+ -- A later step reads the maps its request carries by fresh names.
179
+ def frameRd (mr : List (Nat × Nat)) (rd : Nat → E → E) : Nat → E → E := fun mp k =>
180
+ match lookup (fun p => p.1 == mp) mr with
181
+ | some p => .get p.2 k
182
+ | none => rd mp k
183
+
184
+ def sFrameHyps (s : SMachine) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E)) (w : C) : List C :=
185
+ scalarHyps laws ++ instHyps laws insts ++ [inlineC s.f.flags s.f.m.assume, w] ++ closingHyps s qinsts
186
+
187
+ -- The state after a step, named (`names`, per step but the last: each state variable and a fresh variable): the fresh
188
+ -- variable equals the variable's term there, and later frames read it (frameSub), so a run's frames stay small.
189
+ def nameHyps (n : List (Nat × Nat)) (σ : List (Nat × E)) : List C := n.map (fun p => .eq (.var p.2) ((assoc σ p.1).getD (.var p.1)))
190
+
191
+ def sRun (s : SMachine) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E)) :
192
+ List C → List (List (Nat × Nat)) → List (List (Nat × Nat)) → List (List (Nat × Nat)) → List (Nat × E) → (Nat → E → E) → List C × List (Nat × E) × (Nat → E → E)
193
+ | w :: ws, r :: rs, mr :: mrs, ns, σ, rd =>
194
+ ((sFrameHyps s laws insts qinsts w).map (substRC (frameSub r σ) (frameRd mr rd))
195
+ ++ nameHyps (ns.headD []) (stepSub s.f (frameSub r σ) (frameRd mr rd))
196
+ ++ (sRun s laws insts qinsts ws rs mrs ns.tail (frameSub (ns.headD []) (stepSub s.f (frameSub r σ) (frameRd mr rd))) (stepRdS s (frameSub r σ) (frameRd mr rd))).1,
197
+ (sRun s laws insts qinsts ws rs mrs ns.tail (frameSub (ns.headD []) (stepSub s.f (frameSub r σ) (frameRd mr rd))) (stepRdS s (frameSub r σ) (frameRd mr rd))).2)
198
+ | _, _, _, _, σ, rd => ([], σ, rd)
199
+
200
+ /-! What a step's goal reads it by -/
201
+
202
+ -- Values of the keys where a quantified flag's body fails, when there are some.
203
+ noncomputable def witnessKeys (X : Nat → Nat) (N : Nat → Nat → Nat) (q : Nat × List Nat × C) : Nat → Nat :=
204
+ if h : ∃ k, ¬ holdsC (atKeys X q.2.1 k) N q.2.2 then Classical.choose h else fun _ => 0
205
+
206
+ def SMachine.extVars (s : SMachine) : List Nat := s.f.m.inputs ++ s.qVars ++ s.wVars
207
+
208
+ -- A step's request as its goal reads it: the inputs, each quantified flag's value, each witness.
209
+ noncomputable def sReq (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : Nat → Nat := fun v =>
210
+ if s.f.m.inputs.contains v then ι v
211
+ else match lookup (fun q => q.1 == v) s.qflags with
212
+ | some _ => decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ) v
213
+ | none => match lookup (fun w => w.2.contains v) s.wits with
214
+ | some w => match lookup (fun q => q.1 == w.1) s.qflags with
215
+ | some q => match lookup (fun p => p.2 == v) (q.2.1.zip w.2) with
216
+ | some p => witnessKeys (withInputs s.f.m.inputs ρ ι) (withCarried s M κ) q p.1
217
+ | none => 0
218
+ | none => 0
219
+ | none => 0
220
+
221
+ def withExt (s : SMachine) (ρ e : Nat → Nat) : Nat → Nat := fun v => if s.extVars.contains v then e v else ρ v
222
+
223
+ -- The maps later requests carry, at their fresh names.
224
+ def renMaps : List (List (Nat × Nat)) → List (Nat → Nat → Nat) → (Nat → Nat → Nat) → Nat → Nat → Nat
225
+ | r :: rs, κ :: κs, b => fun mp => match lookup (fun p => p.2 == mp) r with
226
+ | some p => κ p.1
227
+ | none => renMaps rs κs b mp
228
+ | _, _, b => b
229
+
230
+ /-! The checks -/
231
+
232
+ def distinctPairs (ws : List (Nat × List Nat)) : Bool := distinct (ws.map (·.1)) && distinct (ws.flatMap (·.2))
233
+
234
+ -- The machine's own side conditions: inputs, flags, quantified flags and witnesses are distinct variables; a body reads no flag, quantified flag or witness, and its keys are none of them
235
+ -- and distinct; every quantified flag has a witness per key; the plain flags and the step read no witness.
236
+ def sOk (s : SMachine) : Bool :=
237
+ flagsOk s.f.flags && distinct s.qVars && distinctPairs s.wits
238
+ && s.qflags.all (fun q => s.wits.any (fun w => w.1 == q.1 && w.2.length == q.2.1.length) && distinct q.2.1)
239
+ && distinct (s.extVars ++ flagVars s.f.flags)
240
+ && s.qflags.all (fun q => avoids (s.qVars ++ s.wVars ++ flagVars s.f.flags) q.2.2 && q.2.1.all (fun k => !(s.extVars ++ flagVars s.f.flags).contains k))
241
+ && s.f.flags.all (fun p => avoids s.wVars p.2)
242
+ && avoids (s.wVars ++ s.qVars) s.f.m.pre
243
+ && s.f.m.step.all (fun p => avoidsE s.wVars (inlineE s.f.flags p.2))
244
+ && s.f.m.writes.all (fun w => avoidsE s.wVars (inlineE s.f.flags w.2.1) && avoidsE s.wVars (inlineE s.f.flags w.2.2))
245
+ && s.every.all (fun w => avoidsE s.wVars (inlineE s.f.flags w.2.2) && !(s.extVars ++ flagVars s.f.flags).contains w.2.1
246
+ && !(s.f.m.writes.map (·.1)).contains w.1 && !s.carried.contains w.1)
247
+ && avoids s.wVars s.f.m.assume
248
+
249
+ /-! One step's view -/
250
+
251
+ theorem lookup_unique {β : Type} {p : Nat × β} {l : List (Nat × β)} (hd : distinct (l.map (·.1)) = true) (hp : p ∈ l) :
252
+ lookup (fun q => q.1 == p.1) l = some p := by
253
+ induction l with
254
+ | nil => simp at hp
255
+ | cons a as ih =>
256
+ simp only [List.map_cons] at hd
257
+ obtain ⟨hna, hd'⟩ := distinct_cons hd
258
+ simp only [lookup]
259
+ rcases List.mem_cons.mp hp with rfl | hp'
260
+ · simp
261
+ · have hne : (a.1 == p.1) = false := by
262
+ simp only [beq_eq_false_iff_ne]; intro he; exact hna (by rw [he]; exact List.mem_map_of_mem hp')
263
+ simp only [hne, Bool.false_eq_true, ite_false]; exact ih hd' hp'
264
+
265
+ -- Witnesses: distinct across the flags, each found in its flag's entry, beside its key.
266
+ theorem lookup_wit : ∀ {ws : List (Nat × List Nat)}, distinct (ws.flatMap (·.2)) = true → ∀ {w : Nat × List Nat} {v : Nat}, w ∈ ws → v ∈ w.2 →
267
+ lookup (fun w' => w'.2.contains v) ws = some w
268
+ | [], _, _, _, h, _ => by simp at h
269
+ | a :: as, hd, w, v, h, hv => by
270
+ simp only [List.flatMap_cons] at hd
271
+ obtain ⟨_, hd2, hap⟩ := distinct_append hd
272
+ simp only [lookup]
273
+ rcases List.mem_cons.mp h with rfl | h
274
+ · simp [List.contains_iff_mem, hv]
275
+ · have hna : a.2.contains v = false := by
276
+ have : v ∉ a.2 := fun hva => hap v hva (List.mem_flatMap.mpr ⟨w, h, hv⟩)
277
+ simpa [List.contains_iff_mem] using this
278
+ simp only [hna, Bool.false_eq_true, ite_false]
279
+ exact lookup_wit hd2 h hv
280
+
281
+ theorem distinct_of_flatMap : ∀ {ws : List (Nat × List Nat)}, distinct (ws.flatMap (·.2)) = true → ∀ {w : Nat × List Nat}, w ∈ ws → distinct w.2 = true
282
+ | [], _, _, h => by simp at h
283
+ | a :: as, hd, w, h => by
284
+ simp only [List.flatMap_cons] at hd
285
+ obtain ⟨hd1, hd2, _⟩ := distinct_append hd
286
+ rcases List.mem_cons.mp h with rfl | h
287
+ · exact hd1
288
+ · exact distinct_of_flatMap hd2 h
289
+
290
+ theorem lookup_zip_wit : ∀ {ks ws : List Nat}, distinct ws = true → ∀ {k w : Nat}, (k, w) ∈ ks.zip ws → lookup (fun p => p.2 == w) (ks.zip ws) = some (k, w)
291
+ | [], _, _, _, _, h => by simp at h
292
+ | _ :: _, [], _, _, _, h => by simp at h
293
+ | a :: as, b :: bs, hd, k, w, h => by
294
+ obtain ⟨hb, hd'⟩ := distinct_cons hd
295
+ simp only [List.zip_cons_cons, List.mem_cons, Prod.mk.injEq] at h
296
+ simp only [List.zip_cons_cons, lookup]
297
+ rcases h with ⟨rfl, rfl⟩ | h
298
+ · simp
299
+ · have hw : w ≠ b := fun he => hb (he ▸ (List.of_mem_zip h).2)
300
+ have hbw : (b == w) = false := by simp only [beq_eq_false_iff_ne]; exact fun he => hw he.symm
301
+ simp only [hbw, Bool.false_eq_true, ite_false]
302
+ exact lookup_zip_wit hd' h
303
+
304
+ -- Distinct keys zipped with variables: each key reads the variable beside it.
305
+ theorem assoc_zip_var : ∀ {ks ws : List Nat}, distinct ks = true → ∀ {k w : Nat}, (k, w) ∈ ks.zip ws → assoc (ks.zip (ws.map E.var)) k = some (.var w)
306
+ | [], _, _, _, _, h => by simp at h
307
+ | _ :: _, [], _, _, _, h => by simp at h
308
+ | a :: as, b :: bs, hd, k, w, h => by
309
+ obtain ⟨ha, hd'⟩ := distinct_cons hd
310
+ simp only [List.zip_cons_cons, List.mem_cons, Prod.mk.injEq] at h
311
+ simp only [List.map_cons, List.zip_cons_cons, assoc, lookup]
312
+ rcases h with ⟨rfl, rfl⟩ | h
313
+ · simp
314
+ · have hk : k ≠ a := fun he => ha (he ▸ (List.of_mem_zip h).1)
315
+ have hak : (a == k) = false := by simp only [beq_eq_false_iff_ne]; exact fun he => hk he.symm
316
+ simp only [hak, Bool.false_eq_true, ite_false]
317
+ exact assoc_zip_var hd' h
318
+
319
+ theorem zip_has : ∀ {ks ws : List Nat}, ks.length = ws.length → ∀ {k : Nat}, k ∈ ks → ∃ w, (k, w) ∈ ks.zip ws
320
+ | [], _, _, _, h => by simp at h
321
+ | _ :: _, [], hl, _, _ => by simp at hl
322
+ | a :: as, b :: bs, hl, k, h => by
323
+ simp only [List.length_cons, Nat.add_right_cancel_iff] at hl
324
+ rcases List.mem_cons.mp h with rfl | h
325
+ · exact ⟨b, by simp⟩
326
+ · obtain ⟨w, hw⟩ := zip_has hl h
327
+ exact ⟨w, by simp [List.zip_cons_cons, hw]⟩
328
+
329
+ -- Terms put in for the keys: the state read at the keys' values.
330
+ theorem applyS_zip_atKeys (ks : List Nat) (ts : List E) (X : Nat → Nat) (N : Nat → Nat → Nat) :
331
+ applyS (ks.zip ts) X N = atKeys X ks (applyS (ks.zip ts) X N) := by
332
+ funext i
333
+ simp only [atKeys]
334
+ split
335
+ · rfl
336
+ · rename_i h
337
+ exact applyS_other (fun hm => h (by obtain ⟨⟨a, b⟩, hp, rfl⟩ := List.mem_map.mp hm; exact List.contains_iff_mem.mpr (List.of_mem_zip hp).1)) X N
338
+
339
+ theorem lookup_unique1 {p : Nat × Nat} {l : List (Nat × Nat)} (hd : distinct (l.map (·.1)) = true) (hp : p ∈ l) :
340
+ lookup (fun q => q.1 == p.1) l = some p := by
341
+ induction l with
342
+ | nil => simp at hp
343
+ | cons a as ih =>
344
+ simp only [List.map_cons] at hd
345
+ obtain ⟨hna, hd'⟩ := distinct_cons hd
346
+ simp only [lookup]
347
+ rcases List.mem_cons.mp hp with rfl | hp'
348
+ · simp
349
+ · have hne : (a.1 == p.1) = false := by
350
+ simp only [beq_eq_false_iff_ne]; intro he; exact hna (by rw [he]; exact List.mem_map_of_mem hp')
351
+ simp only [hne, Bool.false_eq_true, ite_false]; exact ih hd' hp'
352
+
353
+ theorem applyS_single (k : Nat) (t : E) (W : Nat → Nat) (M : Nat → Nat → Nat) : applyS [(k, t)] W M = upd W k (evalE W M t) := by
354
+ funext i; simp only [applyS, assoc, lookup, upd]
355
+ by_cases h : i = k
356
+ · subst h; simp
357
+ · have hb : (k == i) = false := by simp; omega
358
+ simp [hb, h]
359
+
360
+ -- Agreeing states decide the plain flags alike, wherever the flag conditions don't read where they differ.
361
+ theorem decide_agree_on {fl : List (Nat × C)} {V : List Nat} (hav : ∀ p ∈ fl, avoids V p.2 = true) {X X' : Nat → Nat}
362
+ (N : Nat → Nat → Nat) (h : Agree V X X') : Agree V (decideFlags fl X N) (decideFlags fl X' N) := by
363
+ intro i hi
364
+ simp only [decideFlags, applyS, assoc_flagSub]
365
+ cases hc : flagOf fl i with
366
+ | none => exact h i hi
367
+ | some c =>
368
+ obtain ⟨p, hp, rfl⟩ := flagOf_some hc
369
+ simp only [Option.map_some, evalE]
370
+ rw [congrC X X' N p.2 (fun j hj => h j (avoids_occ (hav p hp) hj))]
371
+
372
+ section view
373
+ variable (s : SMachine) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat)
374
+
375
+ theorem sOk_parts (hs : sOk s = true) :
376
+ flagsOk s.f.flags = true ∧ distinct s.qVars = true ∧ distinctPairs s.wits = true
377
+ ∧ (∀ q ∈ s.qflags, (∃ w ∈ s.wits, w.1 = q.1 ∧ w.2.length = q.2.1.length) ∧ distinct q.2.1 = true)
378
+ ∧ distinct (s.extVars ++ flagVars s.f.flags) = true
379
+ ∧ (∀ q ∈ s.qflags, avoids (s.qVars ++ s.wVars ++ flagVars s.f.flags) q.2.2 = true ∧ ∀ k ∈ q.2.1, k ∉ s.extVars ++ flagVars s.f.flags)
380
+ ∧ (∀ p ∈ s.f.flags, avoids s.wVars p.2 = true) := by
381
+ simp only [sOk, Bool.and_eq_true, List.all_eq_true, List.any_eq_true, beq_iff_eq, Bool.not_eq_true'] at hs
382
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hf, hq⟩, hw⟩, hcov⟩, hd⟩, hb⟩, hfl⟩, _⟩, _⟩, _⟩, _⟩, _⟩ := hs
383
+ refine ⟨hf, hq, hw, fun q hq' => ⟨(hcov q hq').1, (hcov q hq').2⟩, hd, fun q hq' => ⟨(hb q hq').1, fun k hk hm => ?_⟩, hfl⟩
384
+ have h2 := (hb q hq').2 k hk
385
+ exact absurd hm (by simpa using h2)
386
+
387
+ -- The extended variables are apart: an input is no quantified flag or witness, and so on.
388
+ theorem ext_apart (hs : sOk s = true) :
389
+ (∀ v ∈ s.f.m.inputs, v ∉ s.qVars ∧ v ∉ s.wVars) ∧ (∀ v ∈ s.qVars, v ∉ s.wVars) := by
390
+ obtain ⟨_, _, _, _, hd, _, _⟩ := sOk_parts s hs
391
+ obtain ⟨hd1, _, _⟩ := distinct_append hd
392
+ simp only [SMachine.extVars, List.append_assoc] at hd1
393
+ obtain ⟨_, hd2, hi⟩ := distinct_append hd1
394
+ obtain ⟨_, _, hqw⟩ := distinct_append hd2
395
+ exact ⟨fun v hv => ⟨fun h => hi v hv (List.mem_append_left _ h), fun h => hi v hv (List.mem_append_right _ h)⟩, hqw⟩
396
+
397
+ -- (F1) The goal's view and the decided quantified flags agree outside the witnesses.
398
+ theorem view_agree (hs : sOk s = true) :
399
+ Agree s.wVars (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withExt s ρ (sReq s ρ M ι κ)) := by
400
+ obtain ⟨hin, hqw⟩ := ext_apart s hs
401
+ intro v hvw
402
+ simp only [withExt, sReq]
403
+ by_cases hvi : v ∈ s.f.m.inputs
404
+ · have hvq : v ∉ s.qVars := (hin v hvi).1
405
+ have hl : lookup (fun q => q.1 == v) s.qflags = none := lookup_none (fun q hq => by
406
+ simp only [beq_eq_false_iff_ne]; intro he; exact hvq (by rw [← he]; exact List.mem_map_of_mem hq))
407
+ simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append, hvi, true_or, ite_true, decideQ, hl, withInputs]
408
+ · by_cases hvq : v ∈ s.qVars
409
+ · simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append, hvi, hvq, or_true, true_or, ite_true, false_or, ite_false]
410
+ obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hvq
411
+ have hl : lookup (fun q' => q'.1 == q.1) s.qflags = some q := lookup_unique (sOk_parts s hs).2.1 hq
412
+ simp only [hl]
413
+ · have hl : lookup (fun q => q.1 == v) s.qflags = none := lookup_none (fun q hq => by
414
+ simp only [beq_eq_false_iff_ne]; intro he; exact hvq (by rw [← he]; exact List.mem_map_of_mem hq))
415
+ simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append, hvi, hvq, hvw, or_false, ite_false, decideQ, hl, withInputs, List.contains_iff_mem]
416
+
417
+ -- (F2) Every closing hypothesis holds of the real step, read in the goal's view.
418
+ theorem closing_true (hs : sOk s = true) (qinsts : List (Nat × List E)) :
419
+ ∀ c ∈ closingHyps s qinsts, evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) c = true := by
420
+ obtain ⟨_, hqd, hwd, hcov, _, hbody, _⟩ := sOk_parts s hs
421
+ obtain ⟨hin, hqw⟩ := ext_apart s hs
422
+ have hag := view_agree s ρ M ι κ hs
423
+ intro c hc
424
+ simp only [closingHyps, List.mem_flatMap, List.mem_append, List.mem_singleton, List.mem_map, List.mem_filter] at hc
425
+ obtain ⟨q, hq, hc⟩ := hc
426
+ have hqv : q.1 ∈ s.qVars := List.mem_map_of_mem hq
427
+ -- The flag's value: 1 exactly when the body holds at every value of the keys (read in either view: they agree on the body).
428
+ have hY : withExt s ρ (sReq s ρ M ι κ) q.1 = if ∀ k, holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2 then 1 else 0 := by
429
+ rw [← hag q.1 (hqw _ hqv)]; simp only [decideQ, lookup_unique hqd hq]
430
+ have hbodyY : ∀ k, holdsC (atKeys (withExt s ρ (sReq s ρ M ι κ)) q.2.1 k) (withCarried s M κ) q.2.2
431
+ ↔ holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2 := by
432
+ intro k
433
+ rw [← evalC_iff, ← evalC_iff, congrC _ (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2 (fun j hj => by
434
+ simp only [atKeys]; split
435
+ · rfl
436
+ · have hjq : j ∉ s.qVars := fun h => avoids_occ (hbody q hq).1 hj (List.mem_append_left _ (List.mem_append_left _ h))
437
+ have hjw : j ∉ s.wVars := fun h => avoids_occ (hbody q hq).1 hj (List.mem_append_left _ (List.mem_append_right _ h))
438
+ rw [← hag j hjw]; simp only [decideQ]
439
+ rw [lookup_none (fun q' hq' => by simp only [beq_eq_false_iff_ne]; intro he; exact hjq (by rw [← he]; exact List.mem_map_of_mem hq'))])]
440
+ rcases hc with ((rfl | ⟨p, ⟨_, hpq⟩, rfl⟩) | hc)
441
+ · simp only [evalC, evalE, hY]; split <;> simp
442
+ · simp only [beq_iff_eq] at hpq
443
+ simp only [evalC, evalE, Bool.or_eq_true, decide_eq_true_eq]
444
+ by_cases h1 : ∀ k, holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2
445
+ · right; rw [subst_evalC, applyS_zip_atKeys, evalC_iff]; exact (hbodyY _).mpr (h1 _)
446
+ · left; simp [hY, h1]
447
+ · obtain ⟨⟨w, hw, hwq, hlen⟩, hkd⟩ := hcov q hq
448
+ have hlw : lookup (fun w' => w'.1 == q.1) s.wits = some w := by
449
+ rw [← hwq]; exact lookup_unique (by simp only [distinctPairs, Bool.and_eq_true] at hwd; exact hwd.1) hw
450
+ simp only [hlw, List.mem_singleton] at hc; subst hc
451
+ simp only [evalC, evalE, Bool.or_eq_true, decide_eq_true_eq, Bool.not_eq_true']
452
+ by_cases h1 : ∀ k, holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2
453
+ · left; simp [hY, h1]
454
+ · right
455
+ have hwd' : distinct (s.wits.flatMap (·.2)) = true := by simp only [distinctPairs, Bool.and_eq_true] at hwd; exact hwd.2
456
+ -- Each witness reads the failing value of the key beside it.
457
+ have hYw : ∀ k x, (k, x) ∈ q.2.1.zip w.2 → withExt s ρ (sReq s ρ M ι κ) x = witnessKeys (withInputs s.f.m.inputs ρ ι) (withCarried s M κ) q k := by
458
+ intro k x hkx
459
+ have hxw : x ∈ s.wVars := List.mem_flatMap.mpr ⟨w, hw, (List.of_mem_zip hkx).2⟩
460
+ have hxi : x ∉ s.f.m.inputs := fun h => (hin _ h).2 hxw
461
+ have hxq : x ∉ s.qVars := fun h => hqw _ h hxw
462
+ have hext : s.extVars.contains x = true := by
463
+ simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append]; exact Or.inr hxw
464
+ have hni : s.f.m.inputs.contains x = false := by simpa [List.contains_iff_mem] using hxi
465
+ simp only [withExt, hext, ite_true, sReq, hni, Bool.false_eq_true, ite_false]
466
+ rw [lookup_none (fun q' hq' => by
467
+ simp only [beq_eq_false_iff_ne]; intro he; exact hxq (by rw [← he]; exact List.mem_map_of_mem hq')),
468
+ lookup_wit hwd' hw (List.of_mem_zip hkx).2]
469
+ simp only [hwq, lookup_unique hqd hq]
470
+ rw [lookup_zip_wit (distinct_of_flatMap hwd' hw) hkx]
471
+ have hex : ∃ k, ¬ holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 k) (withCarried s M κ) q.2.2 := by simpa using h1
472
+ have hfail : ¬ holdsC (atKeys (withInputs s.f.m.inputs ρ ι) q.2.1 (witnessKeys (withInputs s.f.m.inputs ρ ι) (withCarried s M κ) q)) (withCarried s M κ) q.2.2 := by
473
+ simp only [witnessKeys, dif_pos hex]; exact Classical.choose_spec hex
474
+ rw [subst_evalC]
475
+ cases hb : evalC (applyS (q.2.1.zip (w.2.map E.var)) (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ)) (withCarried s M κ) q.2.2
476
+ · rfl
477
+ · refine absurd ((hbodyY _).mp ((evalC_iff _ _ _).mp ?_)) hfail
478
+ rw [← hb]
479
+ apply congrC; intro j _
480
+ simp only [atKeys]; split
481
+ · rename_i hj
482
+ obtain ⟨x, hx⟩ := zip_has hlen.symm (List.contains_iff_mem.mp hj)
483
+ simp only [applyS, assoc_zip_var hkd hx, evalE]
484
+ exact (hYw j x hx).symm
485
+ · rename_i hj
486
+ exact (applyS_other (fun hm => hj (by obtain ⟨⟨a, b⟩, hp, rfl⟩ := List.mem_map.mp hm; exact List.contains_iff_mem.mpr (List.of_mem_zip hp).1)) _ _).symm
487
+ end view
488
+
489
+ /-! Which maps a term reads -/
490
+
491
+ mutual
492
+ def occME (mp : Nat) : E → Bool
493
+ | .var _ => false
494
+ | .const _ => false
495
+ | .add a b => occME mp a || occME mp b
496
+ | .sub a b => occME mp a || occME mp b
497
+ | .mul _ a => occME mp a
498
+ | .min a b => occME mp a || occME mp b
499
+ | .max a b => occME mp a || occME mp b
500
+ | .prod a b => occME mp a || occME mp b
501
+ | .ite c t f => occMC mp c || occME mp t || occME mp f
502
+ | .get m key => m == mp || occME mp key
503
+ | .tup a b => occME mp a || occME mp b
504
+ | .proj _ a => occME mp a
505
+ def occMC (mp : Nat) : C → Bool
506
+ | .le a b => occME mp a || occME mp b
507
+ | .lt a b => occME mp a || occME mp b
508
+ | .eq a b => occME mp a || occME mp b
509
+ | .and a b => occMC mp a || occMC mp b
510
+ | .or a b => occMC mp a || occMC mp b
511
+ | .not a => occMC mp a
512
+ end
513
+
514
+ def avoidsMaps (ms : List Nat) (c : C) : Bool := ms.all fun mp => !occMC mp c
515
+ def avoidsMapsE (ms : List Nat) (e : E) : Bool := ms.all fun mp => !occME mp e
516
+
517
+ -- Maps that agree on every map a term reads give it the same value.
518
+ mutual
519
+ theorem congrME (ρ : Nat → Nat) (M M' : Nat → Nat → Nat) :
520
+ (e : E) → (∀ mp, occME mp e = true → M mp = M' mp) → evalE ρ M e = evalE ρ M' e
521
+ | .var _, _ => rfl
522
+ | .const _, _ => rfl
523
+ | .add a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
524
+ | .sub a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
525
+ | .mul k a, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm]))]
526
+ | .min a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
527
+ | .max a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
528
+ | .prod a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
529
+ | .ite c t f, h => by
530
+ simp only [evalE, congrMC ρ M M' c (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' t (fun mp hm => h mp (by simp [occME, hm])),
531
+ congrME ρ M M' f (fun mp hm => h mp (by simp [occME, hm]))]
532
+ | .get m key, h => by
533
+ simp only [evalE, congrME ρ M M' key (fun mp hm => h mp (by simp [occME, hm])), h m (by simp [occME])]
534
+ | .tup a b, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occME, hm]))]
535
+ | .proj k a, h => by simp only [evalE, congrME ρ M M' a (fun mp hm => h mp (by simp [occME, hm]))]
536
+ theorem congrMC (ρ : Nat → Nat) (M M' : Nat → Nat → Nat) :
537
+ (c : C) → (∀ mp, occMC mp c = true → M mp = M' mp) → evalC ρ M c = evalC ρ M' c
538
+ | .le a b, h => by simp only [evalC, congrME ρ M M' a (fun mp hm => h mp (by simp [occMC, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occMC, hm]))]
539
+ | .lt a b, h => by simp only [evalC, congrME ρ M M' a (fun mp hm => h mp (by simp [occMC, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occMC, hm]))]
540
+ | .eq a b, h => by simp only [evalC, congrME ρ M M' a (fun mp hm => h mp (by simp [occMC, hm])), congrME ρ M M' b (fun mp hm => h mp (by simp [occMC, hm]))]
541
+ | .and a b, h => by simp only [evalC, congrMC ρ M M' a (fun mp hm => h mp (by simp [occMC, hm])), congrMC ρ M M' b (fun mp hm => h mp (by simp [occMC, hm]))]
542
+ | .or a b, h => by simp only [evalC, congrMC ρ M M' a (fun mp hm => h mp (by simp [occMC, hm])), congrMC ρ M M' b (fun mp hm => h mp (by simp [occMC, hm]))]
543
+ | .not a, h => by simp only [evalC, congrMC ρ M M' a (fun mp hm => h mp (by simp [occMC, hm]))]
544
+ end
545
+
546
+ theorem avoidsMapsE_occ {ms : List Nat} {e : E} (h : avoidsMapsE ms e = true) {mp : Nat} (hm : occME mp e = true) : mp ∉ ms := by
547
+ intro hmem; simp only [avoidsMapsE, List.all_eq_true] at h; have := h mp hmem; simp [hm] at this
548
+
549
+ -- A term avoiding some maps: so does each of its parts.
550
+ theorem amE {ms : List Nat} {e a : E} (h : avoidsMapsE ms e = true) (hs : ∀ mp, occME mp a = true → occME mp e = true) : avoidsMapsE ms a = true := by
551
+ simp only [avoidsMapsE, List.all_eq_true, Bool.not_eq_true'] at h ⊢
552
+ intro mp hm; specialize h mp hm; cases hq : occME mp a
553
+ · rfl
554
+ · simp [hs mp hq] at h
555
+ theorem amEC {ms : List Nat} {e : E} {c : C} (h : avoidsMapsE ms e = true) (hs : ∀ mp, occMC mp c = true → occME mp e = true) : avoidsMaps ms c = true := by
556
+ simp only [avoidsMapsE, avoidsMaps, List.all_eq_true, Bool.not_eq_true'] at h ⊢
557
+ intro mp hm; specialize h mp hm; cases hq : occMC mp c
558
+ · rfl
559
+ · simp [hs mp hq] at h
560
+ theorem amCE {ms : List Nat} {c : C} {a : E} (h : avoidsMaps ms c = true) (hs : ∀ mp, occME mp a = true → occMC mp c = true) : avoidsMapsE ms a = true := by
561
+ simp only [avoidsMapsE, avoidsMaps, List.all_eq_true, Bool.not_eq_true'] at h ⊢
562
+ intro mp hm; specialize h mp hm; cases hq : occME mp a
563
+ · rfl
564
+ · simp [hs mp hq] at h
565
+ theorem amC {ms : List Nat} {c d : C} (h : avoidsMaps ms c = true) (hs : ∀ mp, occMC mp d = true → occMC mp c = true) : avoidsMaps ms d = true := by
566
+ simp only [avoidsMaps, List.all_eq_true, Bool.not_eq_true'] at h ⊢
567
+ intro mp hm; specialize h mp hm; cases hq : occMC mp d
568
+ · rfl
569
+ · simp [hs mp hq] at h
570
+
571
+ -- substR_evalE for a term avoiding the maps rd doesn't read as M' does.
572
+ section
573
+ variable (σ : List (Nat × E)) (rd : Nat → E → E) (W : Nat → Nat) (M M' : Nat → Nat → Nat) (X : List Nat)
574
+ (hrd : ∀ m k, m ∉ X → evalE W M (rd m k) = M' m (evalE W M k))
575
+ include hrd
576
+ mutual
577
+ theorem substR_evalE_on : (e : E) → avoidsMapsE X e = true → evalE W M (substRE σ rd e) = evalE (applyS σ W M) M' e
578
+ | .var i, _ => by
579
+ simp only [substRE]
580
+ cases h : assoc σ i <;> simp [evalE, applyS, h]
581
+ | .const n, _ => by simp [substRE, evalE]
582
+ | .add a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
583
+ | .sub a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
584
+ | .mul k a, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm]))]
585
+ | .min a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
586
+ | .max a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
587
+ | .prod a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
588
+ | .ite c t f, h => by
589
+ simp [substRE, evalE, substR_evalC_on c (amEC h (fun mp hm => by simp [occME, hm])), substR_evalE_on t (amE h (fun mp hm => by simp [occME, hm])),
590
+ substR_evalE_on f (amE h (fun mp hm => by simp [occME, hm]))]
591
+ | .get m key, h => by
592
+ have hmz : m ∉ X := avoidsMapsE_occ h (by simp [occME])
593
+ simp only [substRE, evalE]
594
+ rw [hrd m _ hmz, substR_evalE_on key (amE h (fun mp hm => by simp [occME, hm]))]
595
+ | .tup a b, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm])), substR_evalE_on b (amE h (fun mp hm => by simp [occME, hm]))]
596
+ | .proj k a, h => by simp [substRE, evalE, substR_evalE_on a (amE h (fun mp hm => by simp [occME, hm]))]
597
+ theorem substR_evalC_on : (c : C) → avoidsMaps X c = true → evalC W M (substRC σ rd c) = evalC (applyS σ W M) M' c
598
+ | .le a b, h => by simp [substRC, evalC, substR_evalE_on a (amCE h (fun mp hm => by simp [occMC, hm])), substR_evalE_on b (amCE h (fun mp hm => by simp [occMC, hm]))]
599
+ | .lt a b, h => by simp [substRC, evalC, substR_evalE_on a (amCE h (fun mp hm => by simp [occMC, hm])), substR_evalE_on b (amCE h (fun mp hm => by simp [occMC, hm]))]
600
+ | .eq a b, h => by simp [substRC, evalC, substR_evalE_on a (amCE h (fun mp hm => by simp [occMC, hm])), substR_evalE_on b (amCE h (fun mp hm => by simp [occMC, hm]))]
601
+ | .and a b, h => by simp [substRC, evalC, substR_evalC_on a (amC h (fun mp hm => by simp [occMC, hm])), substR_evalC_on b (amC h (fun mp hm => by simp [occMC, hm]))]
602
+ | .or a b, h => by simp [substRC, evalC, substR_evalC_on a (amC h (fun mp hm => by simp [occMC, hm])), substR_evalC_on b (amC h (fun mp hm => by simp [occMC, hm]))]
603
+ | .not a, h => by simp [substRC, evalC, substR_evalC_on a (amC h (fun mp hm => by simp [occMC, hm]))]
604
+ end
605
+ end
606
+
607
+ theorem substR_var (σ : List (Nat × E)) (rd : Nat → E → E) (W : Nat → Nat) (M : Nat → Nat → Nat) (i : Nat) :
608
+ evalE W M (substRE σ rd (.var i)) = applyS σ W M i := by
609
+ simp only [substRE]; cases h : assoc σ i <;> simp [evalE, applyS, h]
610
+
611
+ theorem avoidsMaps_occ {ms : List Nat} {c : C} (h : avoidsMaps ms c = true) {mp : Nat} (hm : occMC mp c = true) : mp ∉ ms := by
612
+ intro hin; simp only [avoidsMaps, List.all_eq_true] at h; have := h mp hin; simp [hm] at this
613
+
614
+ theorem applyS_cons (k : Nat) (t : E) (τ : List (Nat × E)) (W : Nat → Nat) (M : Nat → Nat → Nat) :
615
+ applyS ((k, t) :: τ) W M = upd (applyS τ W M) k (evalE W M t) := by
616
+ funext i; simp only [applyS, assoc, lookup, upd]
617
+ by_cases h : i = k
618
+ · subst h; simp
619
+ · have hb : (k == i) = false := by simp; omega
620
+ simp [hb, h]
621
+
622
+ -- Setting a variable no flag condition reads commutes with deciding the flags.
623
+ theorem decide_upd {fl : List (Nat × C)} {k : Nat} (hk : k ∉ flagVars fl) (hav : ∀ p ∈ fl, occC k p.2 = false)
624
+ (X : Nat → Nat) (N : Nat → Nat → Nat) (v : Nat) : decideFlags fl (upd X k v) N = upd (decideFlags fl X N) k v := by
625
+ funext i
626
+ simp only [decideFlags, applyS, assoc_flagSub, upd]
627
+ by_cases hi : i = k
628
+ · subst hi
629
+ have hn : flagOf fl i = none := by
630
+ simp only [flagOf, Option.map_eq_none_iff]
631
+ exact lookup_none (fun p hp => by
632
+ simp only [beq_eq_false_iff_ne]; intro he
633
+ exact hk (by simp only [flagVars]; rw [← he]; exact List.mem_map_of_mem hp))
634
+ simp [hn]
635
+ · simp only [hi, ite_false]
636
+ cases hc : flagOf fl i with
637
+ | none => simp
638
+ | some c =>
639
+ obtain ⟨p, hp, rfl⟩ := flagOf_some hc
640
+ simp only [Option.map_some, evalE]
641
+ rw [congrC (upd X k v) X N p.2 (fun j hj => by
642
+ simp only [upd]
643
+ split
644
+ · rename_i hjk; subst hjk; have h0 := hav p hp; rw [h0] at hj; exact absurd hj (by decide)
645
+ · rfl)]
646
+
647
+ /-! Laws read where a frame is read -/
648
+
649
+ -- The scalar laws and every-key laws at their instances hold wherever the laws do.
650
+ theorem laws_hyps_true (laws : List MLaw) (insts : List (Nat × E)) (Y : Nat → Nat) (N : Nat → Nat → Nat)
651
+ (hl : ∀ l ∈ laws, l.Holds Y N) : ∀ c ∈ scalarHyps laws ++ instHyps laws insts, evalC Y N c = true := by
652
+ intro c hc
653
+ rcases List.mem_append.mp hc with hc | hc
654
+ · simp only [scalarHyps, List.mem_filterMap] at hc
655
+ obtain ⟨l, hlm, hk⟩ := hc
656
+ split at hk
657
+ · rename_i hkey
658
+ simp only [Option.some.injEq] at hk; subst hk
659
+ have := hl l hlm
660
+ simpa [MLaw.Holds, hkey] using this
661
+ · simp at hk
662
+ · simp only [instHyps, List.mem_filterMap] at hc
663
+ obtain ⟨p, _, hk⟩ := hc
664
+ split at hk
665
+ · rename_i l hlg
666
+ split at hk
667
+ · rename_i kv hkey
668
+ simp only [Option.some.injEq] at hk; subst hk
669
+ have := hl l (List.mem_of_getElem? hlg)
670
+ simp only [MLaw.Holds, hkey] at this
671
+ rw [subst_evalC, applyS_single]
672
+ exact this _
673
+ · simp at hk
674
+ · simp at hk
675
+
676
+ -- A law reading neither the variables nor the maps two states differ at holds in both alike.
677
+ theorem holds_transfer {V MS : List Nat} (l : MLaw) (hv : avoids V l.holds = true) (hm : avoidsMaps MS l.holds = true)
678
+ {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat} (hag : Agree V ρ ρ') (hmag : ∀ mp, mp ∉ MS → M mp = M' mp) :
679
+ l.Holds ρ' M' → l.Holds ρ M := by
680
+ have cong : ∀ X X' : Nat → Nat, Agree V X X' → evalC X' M' l.holds = true → evalC X M l.holds = true := by
681
+ intro X X' ha h
682
+ rw [congrMC X M M' l.holds (fun mp hmp => hmag mp (avoidsMaps_occ hm hmp)), congrC X X' M' l.holds (fun i hi => ha i (avoids_occ hv hi))]
683
+ exact h
684
+ unfold MLaw.Holds
685
+ split
686
+ · exact cong ρ ρ' hag
687
+ · rename_i kv _
688
+ intro h v
689
+ have ha : Agree V (upd ρ kv v) (upd ρ' kv v) := by
690
+ intro i hi; simp only [upd]; split
691
+ · rfl
692
+ · exact hag i hi
693
+ exact cong _ _ ha (h v)
694
+
695
+ /-! Frames of a run of a machine with sets -/
696
+
697
+ -- The frame σ, rd denotes the state ρ, M: every variable outside the extended request's and the zone Z, and every map
698
+ -- read but those the requests carry and the zone MZ (the names later requests' maps are read by).
699
+ def SDenotes (s : SMachine) (Z MZ : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (σ : List (Nat × E)) (rd : Nat → E → E)
700
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop :=
701
+ (∀ v, v ∉ s.extVars → v ∉ Z → applyS σ W M0 v = ρ v) ∧ (∀ mp k, mp ∉ s.carried → mp ∉ MZ → evalE W M0 (rd mp k) = M mp (evalE W M0 k))
702
+
703
+ -- The frame a step reads (its request set) agrees with the state and request outside the zone, and reads the maps the
704
+ -- step reads.
705
+ def SHead (s : SMachine) (Z MZ : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (σ : List (Nat × E)) (rd : Nat → E → E)
706
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (r mr : List (Nat × Nat)) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : Prop :=
707
+ Agree Z (applyS (frameSub r σ) W M0) (withExt s ρ (sReq s ρ M ι κ))
708
+ ∧ ∀ mp k, mp ∉ MZ → evalE W M0 (frameRd mr rd mp k) = withCarried s M κ mp (evalE W M0 k)
709
+
710
+ section frame
711
+ variable (s : SMachine) (Z MZ : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat)
712
+
713
+ -- A term of the step read at the frame is its value at the real step (flags decided), away from the zone and witnesses.
714
+ theorem sframe_term (hs : sOk s = true) (hfz : ∀ v ∈ flagVars s.f.flags, v ∈ Z) (hfav : ∀ p ∈ s.f.flags, avoids Z p.2 = true)
715
+ {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat} {r mr : List (Nat × Nat)} {ι : Nat → Nat} {κ : Nat → Nat → Nat}
716
+ (hH : SHead s Z MZ W M0 σ rd ρ M r mr ι κ) (e : E) (he : avoidsE (Z ++ s.wVars) (inlineE s.f.flags e) = true)
717
+ (hem : avoidsMapsE MZ (inlineE s.f.flags e) = true) :
718
+ evalE W M0 (substRE (frameSub r σ) (frameRd mr rd) (inlineE s.f.flags e)) = evalE (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 e := by
719
+ obtain ⟨hX, hrd⟩ := hH
720
+ have hwfl := (sOk_parts s hs).2.2.2.2.2.2
721
+ rw [substR_evalE_on (frameSub r σ) (frameRd mr rd) W M0 (withCarried s M κ) MZ hrd _ hem,
722
+ congrE _ (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) _ (fun i hi => hX i (fun h => avoidsE_occ he hi (List.mem_append_left _ h))),
723
+ inline_evalE]
724
+ show evalE (decideFlags s.f.flags (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ)) (withCarried s M κ) e
725
+ = evalE (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ)) (withCarried s M κ) e
726
+ rw [← inline_evalE, ← inline_evalE]
727
+ exact congrE _ _ _ _ (fun i hi => (view_agree s ρ M ι κ hs i (fun h => avoidsE_occ he hi (List.mem_append_right _ h))).symm)
728
+ end frame
729
+
730
+ -- A substitution's terms replaced by their values: the same environment at its keys.
731
+ theorem assoc_zip_map (f : E → E) (i : Nat) : ∀ (ks : List Nat) (ts : List E), assoc (ks.zip (ts.map f)) i = (assoc (ks.zip ts) i).map f
732
+ | [], _ => by simp [assoc, lookup]
733
+ | _ :: _, [] => by simp [assoc, lookup]
734
+ | k :: ks, t :: ts => by
735
+ have ih := assoc_zip_map f i ks ts
736
+ by_cases h : k = i
737
+ · simp [assoc, lookup, h]
738
+ · simp only [assoc, List.zip_cons_cons, List.map_cons, lookup, beq_iff_eq, h, if_false] at ih ⊢; exact ih
739
+
740
+ theorem applyS_zip_const (ks : List Nat) (ts : List E) (V X : Nat → Nat) (N : Nat → Nat → Nat) (i : Nat) (h : V i = X i) :
741
+ applyS (ks.zip ts) V N i = applyS (ks.zip (ts.map (fun t => E.const (evalE V N t)))) X N i := by
742
+ simp only [applyS, assoc_zip_map]
743
+ cases assoc (ks.zip ts) i <;> simp [evalE, h]
744
+
745
+ -- A frame's hypothesis is one of the frame without the flags' instances, or an instance of a flag.
746
+ theorem sframe_mem (s : SMachine) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E)) (w c : C)
747
+ (hc : c ∈ sFrameHyps s laws insts qinsts w) :
748
+ c ∈ sFrameHyps s laws insts [] w ∨ ∃ q ∈ s.qflags, ∃ p ∈ qinsts, c = .or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip p.2) q.2.2) := by
749
+ rcases List.mem_append.mp hc with h | h
750
+ · exact Or.inl (List.mem_append_left _ h)
751
+ · obtain ⟨q, hq, h⟩ := List.mem_flatMap.mp h
752
+ rcases List.mem_append.mp h with h | h
753
+ · rcases List.mem_append.mp h with h | h
754
+ · exact Or.inl (List.mem_append_right _ (List.mem_flatMap.mpr ⟨q, hq, List.mem_append_left _ (List.mem_append_left _ h)⟩))
755
+ · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp h
756
+ exact Or.inr ⟨q, hq, p, (List.mem_filter.mp hp).1, rfl⟩
757
+ · exact Or.inl (List.mem_append_right _ (List.mem_flatMap.mpr ⟨q, hq, List.mem_append_right _ h⟩))
758
+
759
+ section frame2
760
+ variable (s : SMachine) (Z MZ : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat)
761
+
762
+ -- Every hypothesis of a frame holds where the frame is read, given the laws in the goal's view of its state.
763
+ theorem sframe_hyps (hs : sOk s = true) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E))
764
+ {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat} {r mr : List (Nat × Nat)} {ι : Nat → Nat} {κ : Nat → Nat → Nat} {w : C}
765
+ (hlY : ∀ l ∈ laws, l.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ))
766
+ (hH : SHead s Z MZ W M0 σ rd ρ M r mr ι κ)
767
+ (hav : ∀ c ∈ sFrameHyps s laws insts [] w, avoids Z c = true)
768
+ (hqb : ∀ q ∈ s.qflags, avoids Z q.2.2 = true)
769
+ (havm : ∀ c ∈ sFrameHyps s laws insts qinsts w, avoidsMaps MZ c = true)
770
+ (hA : evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true)
771
+ (hW : evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) w = true) :
772
+ ∀ c ∈ sFrameHyps s laws insts qinsts w, evalC W M0 (substRC (frameSub r σ) (frameRd mr rd) c) = true := by
773
+ obtain ⟨hX, hrd⟩ := hH
774
+ intro c hc
775
+ rw [substR_evalC_on (frameSub r σ) (frameRd mr rd) W M0 (withCarried s M κ) MZ hrd c (havm c hc)]
776
+ rcases sframe_mem s laws insts qinsts w c hc with hc | ⟨q, hq, p, _, rfl⟩
777
+ · rw [congrC _ (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) c (fun i hi => hX i (avoids_occ (hav c hc) hi))]
778
+ simp only [sFrameHyps, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
779
+ rcases hc with ((hc | rfl | rfl) | hc)
780
+ · exact laws_hyps_true laws insts _ _ hlY c (List.mem_append.mpr hc)
781
+ · -- The assumption, inlined: the view decides the plain flags as the step does, away from the witnesses.
782
+ rw [inline_evalC]
783
+ have hagD := decide_agree_on (sOk_parts s hs).2.2.2.2.2.2 (withCarried s M κ) (view_agree s ρ M ι κ hs)
784
+ rw [congrC _ (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ)) _ s.f.m.assume
785
+ (fun i hi => (hagD i (fun h => by
786
+ have hpa : avoids s.wVars s.f.m.assume = true := by simp only [sOk, Bool.and_eq_true] at hs; exact hs.2
787
+ exact avoids_occ hpa hi h)).symm)]
788
+ exact hA
789
+ · exact hW
790
+ · exact closing_true s ρ M ι κ hs [] c hc
791
+ · -- A flag's instance: its keys' terms may read the zone (a later step's names), the flag and its body don't. Read
792
+ -- in the frame it is the instance at the keys' values read in the step's view, which holds there.
793
+ have hq1 : q.1 ∉ Z := by
794
+ have hle : C.le (.var q.1) (.const 1) ∈ sFrameHyps s laws insts [] w :=
795
+ List.mem_append_right _ (List.mem_flatMap.mpr ⟨q, hq, List.mem_append_left _ (List.mem_append_left _ (List.mem_singleton.mpr rfl))⟩)
796
+ exact avoids_occ (hav _ hle) (by simp [occC, occE])
797
+ let ks := p.2.map (fun t => E.const (evalE (applyS (frameSub r σ) W M0) (withCarried s M κ) t))
798
+ have hc' := closing_true s ρ M ι κ hs [(q.1, ks)] (.or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip ks) q.2.2))
799
+ (List.mem_flatMap.mpr ⟨q, hq, List.mem_append_left _ (List.mem_append_right _
800
+ (List.mem_map.mpr ⟨(q.1, ks), List.mem_filter.mpr ⟨List.mem_singleton.mpr rfl, by simp⟩, rfl⟩))⟩)
801
+ have heq : evalC (applyS (frameSub r σ) W M0) (withCarried s M κ) (.or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip p.2) q.2.2))
802
+ = evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) (.or (.eq (.var q.1) (.const 0)) (substC (q.2.1.zip ks) q.2.2)) := by
803
+ simp only [evalC, evalE, subst_evalC, hX q.1 hq1]
804
+ rw [congrC _ _ _ q.2.2 (fun i hi => applyS_zip_const q.2.1 p.2 _ _ _ i (hX i (avoids_occ (hqb q hq) hi)))]
805
+ rfl
806
+ rw [heq]; exact hc'
807
+
808
+ -- One step keeps the frame denoting the state: each variable its step term, each map its writes.
809
+ theorem sstep_denotes (hs : sOk s = true) (hfz : ∀ v ∈ flagVars s.f.flags, v ∈ Z) (hfav : ∀ p ∈ s.f.flags, avoids Z p.2 = true)
810
+ {σ : List (Nat × E)} {rd : Nat → E → E} {ρ : Nat → Nat} {M : Nat → Nat → Nat} (r mr : List (Nat × Nat)) (ι : Nat → Nat) (κ : Nat → Nat → Nat)
811
+ (hD : SDenotes s Z MZ W M0 σ rd ρ M) (hH : SHead s Z MZ W M0 σ rd ρ M r mr ι κ)
812
+ (hrk : ∀ p ∈ r, p.1 ∈ s.extVars)
813
+ (hst : ∀ p ∈ s.f.m.step, avoidsE (Z ++ s.wVars) (inlineE s.f.flags p.2) = true)
814
+ (hwr : ∀ w ∈ s.f.m.writes, avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.1) = true ∧ avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) = true)
815
+ (hev : ∀ w ∈ s.every, avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) = true ∧ w.2.1 ∉ Z ∧ w.2.1 ∉ flagVars s.f.flags
816
+ ∧ (∀ p ∈ s.f.flags, occC w.2.1 p.2 = false) ∧ w.1 ∉ s.carried ∧ w.1 ∉ s.f.m.writes.map (·.1))
817
+ (hstm : ∀ p ∈ s.f.m.step, avoidsMapsE MZ (inlineE s.f.flags p.2) = true)
818
+ (hwrm : ∀ w ∈ s.f.m.writes, avoidsMapsE MZ (inlineE s.f.flags w.2.1) = true ∧ avoidsMapsE MZ (inlineE s.f.flags w.2.2) = true)
819
+ (hevm : ∀ w ∈ s.every, avoidsMapsE MZ (inlineE s.f.flags w.2.2) = true) :
820
+ SDenotes s Z MZ W M0 (stepSub s.f (frameSub r σ) (frameRd mr rd)) (stepRdS s (frameSub r σ) (frameRd mr rd)) (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 := by
821
+ obtain ⟨hv, hrdM⟩ := hD
822
+ have hH' := hH
823
+ obtain ⟨hX, hrd⟩ := hH
824
+ refine ⟨fun v hve hvz => ?_, fun mp k hmp hmz => ?_⟩
825
+ · simp only [sNext, applyS, stepSub, assoc_append_or, assoc_stepSub]
826
+ cases h : assoc s.f.m.step v with
827
+ | some e =>
828
+ have hm : (v, e) ∈ s.f.m.step := by
829
+ simp only [assoc, Option.map_eq_some_iff] at h
830
+ obtain ⟨q, hq, rfl⟩ := h
831
+ obtain ⟨hqm, hqv⟩ := lookup_some hq
832
+ simp only [beq_iff_eq] at hqv
833
+ rw [← hqv]; exact hqm
834
+ simp only [Option.map_some, Option.some_or]
835
+ exact sframe_term s Z MZ W M0 hs hfz hfav hH' e (hst (v, e) hm) (hstm (v, e) hm)
836
+ | none =>
837
+ simp only [Option.map_none, Option.none_or]
838
+ have hvf : v ∉ flagVars s.f.flags := fun h' => hvz (hfz v h')
839
+ have hvq : v ∉ s.qVars := fun h' => hve (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inr h'))
840
+ have hvi : v ∉ s.f.m.inputs := fun h' => hve (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inl h'))
841
+ show applyS (frameSub r σ) W M0 v = decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ) v
842
+ rw [decide_other hvf, hX v hvz]
843
+ simp only [withExt, decideQ, sReq]
844
+ rw [if_neg (by simpa [List.contains_iff_mem] using hve)]
845
+ rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact hvq (by rw [← he]; exact List.mem_map_of_mem hq))]
846
+ exact (withInputs_other hvi ρ ι).symm
847
+ · simp only [stepRdS, sNext, everyMaps]
848
+ cases he : lookup (fun w => w.1 == mp) s.every with
849
+ | some w =>
850
+ obtain ⟨hval, _, hkf, hkfl, _, _⟩ := hev w (lookup_some he).1
851
+ simp only
852
+ rw [substR_evalE_on ((w.2.1, k) :: frameSub r σ) (frameRd mr rd) W M0 (withCarried s M κ) MZ hrd _ (hevm w (lookup_some he).1), applyS_cons]
853
+ show evalE (upd (applyS (frameSub r σ) W M0) w.2.1 (evalE W M0 k)) (withCarried s M κ) (inlineE s.f.flags w.2.2)
854
+ = evalE (upd (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ)) w.2.1 (evalE W M0 k)) (withCarried s M κ) w.2.2
855
+ rw [← decide_upd hkf (fun p hp => hkfl p hp), ← inline_evalE]
856
+ -- The frame and the step read the inlined value alike: they agree away from the zone and the witnesses.
857
+ exact congrE _ _ _ _ (fun i hi => by
858
+ simp only [upd]; split
859
+ · rfl
860
+ · have hiz : i ∉ Z := fun h => avoidsE_occ hval hi (List.mem_append_left _ h)
861
+ have hiw : i ∉ s.wVars := fun h => avoidsE_occ hval hi (List.mem_append_right _ h)
862
+ rw [hX i hiz]; exact (view_agree s ρ M ι κ hs i hiw).symm)
863
+ | none =>
864
+ simp only [stepRd, writeMaps]
865
+ cases hw : lookup (fun w : Nat × E × E => w.1 == mp) s.f.m.writes with
866
+ | none => exact hrd mp k hmz
867
+ | some w =>
868
+ have hm := (lookup_some hw).1
869
+ simp only [evalE, evalC, sframe_term s Z MZ W M0 hs hfz hfav hH' w.2.1 (hwr w hm).1 (hwrm w hm).1,
870
+ sframe_term s Z MZ W M0 hs hfz hfav hH' w.2.2 (hwr w hm).2 (hwrm w hm).2, fun k => hrd mp k hmz]
871
+ simp only [sView]
872
+ by_cases hk : evalE W M0 k = evalE (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M κ)) (withCarried s M κ)) (withCarried s M κ) w.2.1 <;> simp [hk]
873
+ end frame2
874
+
875
+ /-! Laws: the induction rule for machines with sets -/
876
+
877
+ structure SEntry where
878
+ law : MLaw
879
+ sk : Nat -- the skolem variable
880
+ keys2 : List Nat -- a law about every two keys or more: its further keys, variables nothing but laws about them read
881
+ insts : List (Nat × E) -- (law index, key): where the step goal assumes the laws about every key
882
+ qinsts : List (Nat × List E) -- (quantified flag, keys): where the step goal closes it
883
+ finsts : List (Nat × E) -- (fact index, key): where both goals assume the facts
884
+ pinsts : List (Nat × E × List E) -- (law index, key, further keys): where the step goal assumes the laws about several keys
885
+ pfinsts : List (Nat × E × List E) -- (fact index, key, further keys): where both goals assume the facts about several keys
886
+ start : Cert
887
+ step : Cert
888
+
889
+ -- What an entry claims: its law, at every value of each further key.
890
+ def SEntry.Holds (e : SEntry) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop := holdsOver e.law M e.keys2 ρ
891
+
892
+ -- A law about several keys at a tuple, as a law about every first key: each further key read as the tuple's term.
893
+ def pairLaws (ps : List SEntry) (pinsts : List (Nat × E × List E)) : List MLaw := pinsts.map fun p =>
894
+ match ps[p.1]? with
895
+ | some e => ⟨substC (e.keys2.zip p.2.2) e.law.holds, e.law.key⟩
896
+ | none => ⟨trueC, none⟩
897
+ -- The laws a frame assumes (the entries' laws, then each pair's) and where (each pair's at its first term).
898
+ def sLaws (ps : List SEntry) (pinsts : List (Nat × E × List E)) : List MLaw := ps.map (·.law) ++ pairLaws ps pinsts
899
+ def sInsts (ps : List SEntry) (insts : List (Nat × E)) (pinsts : List (Nat × E × List E)) : List (Nat × E) := insts ++ pairInsts ps.length pinsts
900
+ def sStartGoal (s : SMachine) (e : SEntry) : List C := [s.f.m.pre] ++ factHyps s e.finsts e.pfinsts ++ [.not (substRC s.f.m.init (rdStart s.f.m) (goalOf e.law e.sk))]
901
+ def sStepGoal (s : SMachine) (ps : List SEntry) (e : SEntry) : List C :=
902
+ [s.f.m.pre] ++ factHyps s e.finsts e.pfinsts ++ (sFrameHyps s (sLaws ps e.pinsts) (sInsts ps e.insts e.pinsts) e.qinsts trueC).map (substRC (frameSub [] []) (frameRd [] getRd))
903
+ ++ [.not (substRC (stepSub s.f (frameSub [] []) (frameRd [] getRd)) (stepRdS s (frameSub [] []) (frameRd [] getRd)) (goalOf e.law e.sk))]
904
+
905
+ -- The machine's terms and the zone a frame keeps apart (the flags, and here the skolem).
906
+ def sTermsOk (s : SMachine) (Z : List Nat) : Bool :=
907
+ s.f.flags.all (fun p => avoids Z p.2)
908
+ && s.f.m.step.all (fun p => avoidsE (Z ++ s.wVars) (inlineE s.f.flags p.2))
909
+ && s.f.m.writes.all (fun w => avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.1) && avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) && !s.carried.contains w.1)
910
+ && s.every.all (fun w => avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) && !Z.contains w.2.1 && !(flagVars s.f.flags).contains w.2.1
911
+ && s.f.flags.all (fun p => !occC w.2.1 p.2) && !s.carried.contains w.1 && !(s.f.m.writes.map (·.1)).contains w.1)
912
+ && s.facts.all (fun l => avoids Z l.holds) && s.pfacts.all (fun f => avoids Z f.1.holds)
913
+
914
+ -- The facts read the params and the functions alone: no request, flag or state variable, and no map a step writes or
915
+ -- a request carries.
916
+ def factsOk1 (s : SMachine) : Bool :=
917
+ s.facts.all fun l => avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) l.holds
918
+ && avoidsMaps (s.carried ++ s.every.map (·.1) ++ s.f.m.writes.map (·.1)) l.holds
919
+ -- The facts about several keys alike, each about a first key apart from its further keys.
920
+ def pfactsOk (s : SMachine) : Bool :=
921
+ s.pfacts.all fun f => avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) f.1.holds
922
+ && avoidsMaps (s.carried ++ s.every.map (·.1) ++ s.f.m.writes.map (·.1)) f.1.holds && f.2.all (fun g => f.1.key != some g)
923
+ def factsOk (s : SMachine) : Bool := factsOk1 s && pfactsOk s
924
+
925
+ -- The skolem is fresh for the machine: no flag, body, assumption, term or write reads it, nor binds it.
926
+ def skFresh (s : SMachine) (sk : Nat) : Bool :=
927
+ s.f.flags.all (fun p => !occC sk p.2) && s.qflags.all (fun q => !occC sk q.2.2) && !occC sk s.f.m.assume
928
+ && s.f.m.step.all (fun p => !occE sk p.2) && s.f.m.writes.all (fun w => !occE sk w.2.1 && !occE sk w.2.2)
929
+ && s.every.all (fun w => !occE sk w.2.2 && w.2.1 != sk) && s.facts.all (fun l => !occC sk l.holds)
930
+ && s.pfacts.all (fun f => !occC sk f.1.holds)
931
+
932
+ -- Further keys: each fresh as a skolem is, not the law's own key, read by no law but those about it, and no entry's skolem.
933
+ def key2Ok (s : SMachine) (ps : List SEntry) (e : SEntry) : Bool :=
934
+ e.keys2.all fun g => e.law.key != some g && skFresh s g && !(s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains g
935
+ && !occC g s.f.m.pre && s.f.m.init.all (fun p => !occE g p.2)
936
+ && ps.all (fun e' => (!occC g e'.law.holds || e'.keys2.contains g) && e'.sk != g)
937
+
938
+ def checkSMachine (s : SMachine) (ps : List SEntry) : Bool :=
939
+ sOk s && avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) s.f.m.pre && !readsC s.f.m.pre
940
+ && sTermsOk s (flagVars s.f.flags) && factsOk s
941
+ && ps.all fun e =>
942
+ avoids (s.extVars ++ flagVars s.f.flags) e.law.holds && avoidsMaps s.carried e.law.holds
943
+ && !(s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains e.sk
944
+ && ps.all (fun e' => !occC e.sk e'.law.holds) && !occC e.sk s.f.m.pre && s.f.m.init.all (fun p => !occE e.sk p.2)
945
+ && skFresh s e.sk && key2Ok s ps e
946
+ && checkIn (sStartGoal s e) e.start
947
+ && checkIn (sStepGoal s ps e) e.step
948
+
949
+ theorem skFresh_parts {s : SMachine} {sk : Nat} (h : skFresh s sk = true) :
950
+ (∀ p ∈ s.f.flags, occC sk p.2 = false) ∧ (∀ q ∈ s.qflags, occC sk q.2.2 = false) ∧ occC sk s.f.m.assume = false
951
+ ∧ (∀ p ∈ s.f.m.step, occE sk p.2 = false) ∧ (∀ w ∈ s.f.m.writes, occE sk w.2.1 = false ∧ occE sk w.2.2 = false)
952
+ ∧ (∀ w ∈ s.every, occE sk w.2.2 = false ∧ w.2.1 ≠ sk) ∧ (∀ l ∈ s.facts, occC sk l.holds = false) := by
953
+ simp only [skFresh, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true', bne_iff_ne, ne_eq] at h
954
+ obtain ⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩, h7⟩, _⟩ := h
955
+ exact ⟨h1, h2, h3, h4, h5, h6, h7⟩
956
+
957
+ theorem skFresh_pfacts {s : SMachine} {sk : Nat} (h : skFresh s sk = true) : ∀ f ∈ s.pfacts, occC sk f.1.holds = false := by
958
+ simp only [skFresh, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at h; exact h.2
959
+
960
+ /-! A variable no step reads or writes: setting it commutes with a step -/
961
+
962
+ theorem withInputs_upd {ins : List Nat} {x : Nat} (hx : x ∉ ins) (ρ ι : Nat → Nat) (v : Nat) :
963
+ withInputs ins (upd ρ x v) ι = upd (withInputs ins ρ ι) x v := by
964
+ funext i
965
+ simp only [withInputs, upd]
966
+ by_cases hi : i = x
967
+ · subst hi; simp [List.contains_iff_mem, hx]
968
+ · simp [hi]
969
+
970
+ theorem decideQ_upd {s : SMachine} {x : Nat} (hq : x ∉ s.qVars) (hb : ∀ q ∈ s.qflags, occC x q.2.2 = false)
971
+ (X : Nat → Nat) (N : Nat → Nat → Nat) (v : Nat) : decideQ s (upd X x v) N = upd (decideQ s X N) x v := by
972
+ funext u
973
+ by_cases hux : u = x
974
+ · subst hux
975
+ have hl : lookup (fun q => q.1 == u) s.qflags = none := lookup_none (fun q hq' => by
976
+ simp only [beq_eq_false_iff_ne]; intro he; exact hq (by rw [← he]; exact List.mem_map_of_mem hq'))
977
+ simp only [decideQ, hl, upd, ite_true]
978
+ · simp only [upd, hux, ite_false]
979
+ simp only [decideQ]
980
+ cases hl : lookup (fun q => q.1 == u) s.qflags with
981
+ | none => simp [upd, hux]
982
+ | some q =>
983
+ have hbq := hb q (lookup_some hl).1
984
+ have hiff : ∀ k, holdsC (atKeys (upd X x v) q.2.1 k) N q.2.2 ↔ holdsC (atKeys X q.2.1 k) N q.2.2 := fun k => by
985
+ rw [← evalC_iff, ← evalC_iff, congrC _ (atKeys X q.2.1 k) N q.2.2 (fun i hi => by
986
+ have hix : i ≠ x := fun h => by rw [h, hbq] at hi; cases hi
987
+ simp only [atKeys]; split
988
+ · rfl
989
+ · simp [upd, hix])]
990
+ simp only [hiff]
991
+
992
+ theorem sView_upd {s : SMachine} {x : Nat} (hxi : x ∉ s.f.m.inputs) (hxq : x ∉ s.qVars) (hxf : x ∉ flagVars s.f.flags)
993
+ (hb : ∀ q ∈ s.qflags, occC x q.2.2 = false) (hfl : ∀ p ∈ s.f.flags, occC x p.2 = false)
994
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) (v : Nat) :
995
+ sView s (upd ρ x v) M ι κ = (upd (sView s ρ M ι κ).1 x v, (sView s ρ M ι κ).2) := by
996
+ simp only [sView, withInputs_upd hxi, decideQ_upd hxq hb, decide_upd hxf hfl]
997
+
998
+ theorem sNext_upd {s : SMachine} {x : Nat} (hxi : x ∉ s.f.m.inputs) (hxq : x ∉ s.qVars) (hxf : x ∉ flagVars s.f.flags)
999
+ (hb : ∀ q ∈ s.qflags, occC x q.2.2 = false) (hfl : ∀ p ∈ s.f.flags, occC x p.2 = false)
1000
+ (hxs : x ∉ s.f.m.step.map (·.1)) (hst : ∀ p ∈ s.f.m.step, occE x p.2 = false)
1001
+ (hwr : ∀ w ∈ s.f.m.writes, occE x w.2.1 = false ∧ occE x w.2.2 = false) (hev : ∀ w ∈ s.every, occE x w.2.2 = false ∧ w.2.1 ≠ x)
1002
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) (v : Nat) :
1003
+ sNext s (upd ρ x v) M ι κ = (upd (sNext s ρ M ι κ).1 x v, (sNext s ρ M ι κ).2) := by
1004
+ simp only [sNext, sView_upd hxi hxq hxf hb hfl]
1005
+ rw [applyS_upd hxs (by simpa [List.all_eq_true] using hst)]
1006
+ congr 1
1007
+ funext mp key
1008
+ simp only [everyMaps]
1009
+ cases he : lookup (fun w => w.1 == mp) s.every with
1010
+ | some w =>
1011
+ obtain ⟨hv, hk⟩ := hev w (lookup_some he).1
1012
+ exact congrE _ _ _ _ (fun i hi => by
1013
+ have hix : i ≠ x := fun h => by rw [h, hv] at hi; cases hi
1014
+ simp only [upd]
1015
+ by_cases h1 : i = w.2.1
1016
+ · simp [h1]
1017
+ · simp [h1, hix])
1018
+ | none =>
1019
+ simp only [writeMaps]
1020
+ cases hw : lookup (fun w : Nat × E × E => w.1 == mp) s.f.m.writes with
1021
+ | none => rfl
1022
+ | some w =>
1023
+ obtain ⟨h1, h2⟩ := hwr w (lookup_some hw).1
1024
+ have e1 : evalE (upd (sView s ρ M ι κ).1 x v) (sView s ρ M ι κ).2 w.2.1 = evalE (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 w.2.1 :=
1025
+ congrE _ _ _ _ (fun i hi => by have hix : i ≠ x := (fun h => by rw [h, h1] at hi; cases hi); simp [upd, hix])
1026
+ have e2 : evalE (upd (sView s ρ M ι κ).1 x v) (sView s ρ M ι κ).2 w.2.2 = evalE (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 w.2.2 :=
1027
+ congrE _ _ _ _ (fun i hi => by have hix : i ≠ x := (fun h => by rw [h, h2] at hi; cases hi); simp [upd, hix])
1028
+ simp only [e1, e2]
1029
+
1030
+ theorem sTermsOk_parts {s : SMachine} {Z : List Nat} (h : sTermsOk s Z = true) :
1031
+ (∀ p ∈ s.f.flags, avoids Z p.2 = true)
1032
+ ∧ (∀ p ∈ s.f.m.step, avoidsE (Z ++ s.wVars) (inlineE s.f.flags p.2) = true)
1033
+ ∧ (∀ w ∈ s.f.m.writes, (avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.1) = true ∧ avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) = true) ∧ w.1 ∉ s.carried)
1034
+ ∧ (∀ w ∈ s.every, avoidsE (Z ++ s.wVars) (inlineE s.f.flags w.2.2) = true ∧ w.2.1 ∉ Z ∧ w.2.1 ∉ flagVars s.f.flags
1035
+ ∧ (∀ p ∈ s.f.flags, occC w.2.1 p.2 = false) ∧ w.1 ∉ s.carried ∧ w.1 ∉ s.f.m.writes.map (·.1)) := by
1036
+ simp only [sTermsOk, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true'] at h
1037
+ obtain ⟨⟨⟨⟨⟨hf, hst⟩, hwr⟩, hev⟩, _⟩, _⟩ := h
1038
+ refine ⟨hf, hst, fun w hw => ⟨⟨(hwr w hw).1.1, (hwr w hw).1.2⟩, by simpa using (hwr w hw).2⟩, fun w hw => ?_⟩
1039
+ obtain ⟨⟨⟨⟨⟨h1, h2⟩, h3⟩, h4⟩, h5⟩, h6⟩ := hev w hw
1040
+ exact ⟨h1, by simpa using h2, by simpa using h3, fun p hp => by simpa using h4 p hp, by simpa using h5, by simpa using h6⟩
1041
+
1042
+ -- The precondition holds in every reachable state: it reads the params alone.
1043
+ theorem pre_sreach (s : SMachine) (hp : avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) s.f.m.pre = true)
1044
+ (hr : readsC s.f.m.pre = false) : ∀ {ρ M}, SReach s ρ M → evalC ρ M s.f.m.pre = true := by
1045
+ intro ρ M h
1046
+ induction h with
1047
+ | start ρ M _ h0 _ _ =>
1048
+ rw [evalC_avoids hp M (ρ' := ρ) (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ M)]; exact h0
1049
+ | step ρ M ι κ _ _ ih =>
1050
+ rw [noReadsC _ _ M s.f.m.pre hr]
1051
+ rw [evalC_avoids hp M (ρ' := ρ) (fun i hi => by
1052
+ simp only [sNext, sView]
1053
+ rw [applyS_other (fun h => hi (by simp [h])), decide_other (fun h => hi (by simp [h]))]
1054
+ simp only [decideQ]
1055
+ rw [lookup_none (fun q hq => by
1056
+ simp only [beq_eq_false_iff_ne]; intro he
1057
+ exact hi (by simp only [SMachine.extVars, SMachine.qVars, List.mem_append, List.mem_map]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inr ⟨q, hq, he⟩))))))]
1058
+ exact withInputs_other (fun h => hi (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inl h)))))) ρ ι)]
1059
+ exact ih
1060
+
1061
+ -- The facts hold in every reachable state: a run starts where they hold, and no step moves what they read.
1062
+ theorem facts_sreach (s : SMachine) (hf : factsOk s = true) : ∀ {ρ M}, SReach s ρ M → ∀ l ∈ s.facts, l.Holds ρ M := by
1063
+ simp only [factsOk, Bool.and_eq_true] at hf
1064
+ have hf := hf.1
1065
+ simp only [factsOk1, List.all_eq_true, Bool.and_eq_true] at hf
1066
+ intro ρ M h
1067
+ induction h with
1068
+ | start ρ M _ _ hfa _ =>
1069
+ intro l hl
1070
+ exact holds_transfer l (hf l hl).1 (MS := []) (by simp [avoidsMaps])
1071
+ (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ M) (fun _ _ => rfl) (hfa l hl)
1072
+ | step ρ M ι κ _ _ ih =>
1073
+ intro l hl
1074
+ refine holds_transfer l (hf l hl).1 (hf l hl).2 (fun i hi => ?_) (fun mp hmp => ?_) (ih l hl)
1075
+ · simp only [sNext, sView]
1076
+ rw [applyS_other (fun h => hi (by simp [h])), decide_other (fun h => hi (by simp [h]))]
1077
+ simp only [decideQ]
1078
+ rw [lookup_none (fun q hq => by
1079
+ simp only [beq_eq_false_iff_ne]; intro he
1080
+ exact hi (by simp only [SMachine.extVars, SMachine.qVars, List.mem_append, List.mem_map]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inr ⟨q, hq, he⟩))))))]
1081
+ exact withInputs_other (fun h => hi (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inl h)))))) ρ ι
1082
+ · simp only [List.mem_append, not_or] at hmp
1083
+ obtain ⟨⟨hmc, hme⟩, hmw⟩ := hmp
1084
+ funext key
1085
+ simp only [sNext, sView, everyMaps]
1086
+ rw [lookup_none (fun w hw => by simp only [beq_eq_false_iff_ne]; intro he; exact hme (by rw [← he]; exact List.mem_map_of_mem hw))]
1087
+ simp only [writeMaps]
1088
+ rw [lookup_none (fun w hw => by simp only [beq_eq_false_iff_ne]; intro he; exact hmw (by rw [← he]; exact List.mem_map_of_mem hw))]
1089
+ simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]
1090
+
1091
+ theorem sTermsOk_facts {s : SMachine} {Z : List Nat} (h : sTermsOk s Z = true) : ∀ l ∈ s.facts, avoids Z l.holds = true := by
1092
+ simp only [sTermsOk, Bool.and_eq_true, List.all_eq_true] at h; exact h.1.2
1093
+ theorem sTermsOk_pfacts {s : SMachine} {Z : List Nat} (h : sTermsOk s Z = true) : ∀ f ∈ s.pfacts, avoids Z f.1.holds = true := by
1094
+ simp only [sTermsOk, Bool.and_eq_true, List.all_eq_true] at h; exact h.2
1095
+
1096
+ -- The facts hold wherever the variables and maps they may read are the state's: away from the request, the flags, the
1097
+ -- state and the zone Vx, and from the maps a step writes, a request carries and the zone Mx.
1098
+ theorem facts_frame (s : SMachine) (hfok : factsOk s = true) (Vx Mx : List Nat)
1099
+ (hvx : ∀ l ∈ s.facts, avoids Vx l.holds = true) (hmx : ∀ l ∈ s.facts, avoidsMaps Mx l.holds = true)
1100
+ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {X : Nat → Nat} {N : Nat → Nat → Nat}
1101
+ (hX : ∀ i, i ∉ s.extVars → i ∉ Vx → X i = ρ i) (hN : ∀ mp, mp ∉ s.carried → mp ∉ Mx → N mp = M mp)
1102
+ (hl : ∀ l ∈ s.facts, l.Holds ρ M) : ∀ l ∈ s.facts, l.Holds X N := by
1103
+ simp only [factsOk, Bool.and_eq_true] at hfok
1104
+ have hfok := hfok.1
1105
+ simp only [factsOk1, List.all_eq_true, Bool.and_eq_true] at hfok
1106
+ intro l hl'
1107
+ refine holds_transfer (V := (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) ++ Vx)
1108
+ (MS := (s.carried ++ s.every.map (·.1) ++ s.f.m.writes.map (·.1)) ++ Mx) l
1109
+ (by simp only [avoids, List.all_append, Bool.and_eq_true] at hfok hvx ⊢; exact ⟨(hfok l hl').1, hvx l hl'⟩)
1110
+ (by simp only [avoidsMaps, List.all_append, Bool.and_eq_true] at hfok hmx ⊢; exact ⟨(hfok l hl').2, hmx l hl'⟩)
1111
+ (fun i hi => hX i (fun h => hi (by simp [h])) (fun h => hi (by simp [h])))
1112
+ (fun mp hmp => hN mp (fun h => hmp (by simp [h])) (fun h => hmp (by simp [h]))) (hl l hl')
1113
+
1114
+ -- Two environments that agree on what a law reads and at the skolem read its goal alike.
1115
+ theorem goalOf_agree (l : MLaw) (sk : Nat) (N : Nat → Nat → Nat) {X X' : Nat → Nat} (hsk : X sk = X' sk)
1116
+ (h : ∀ i, occC i l.holds = true → X i = X' i) : evalC X N (goalOf l sk) = evalC X' N (goalOf l sk) := by
1117
+ unfold goalOf
1118
+ split
1119
+ · exact congrC _ _ N _ h
1120
+ · rename_i kv _
1121
+ rw [subst_evalC, subst_evalC, applyS_single, applyS_single]
1122
+ exact congrC _ _ N _ (fun i hi => by
1123
+ simp only [upd, evalE]; split
1124
+ · exact hsk
1125
+ · exact h i hi)
1126
+
1127
+ theorem sOk_of {s : SMachine} {ps : List SEntry} (hl : checkSMachine s ps = true) : sOk s = true := by
1128
+ simp only [checkSMachine, Bool.and_eq_true] at hl; exact hl.1.1.1.1.1
1129
+
1130
+ theorem spre_parts {s : SMachine} {ps : List SEntry} (hl : checkSMachine s ps = true) :
1131
+ avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) s.f.m.pre = true ∧ readsC s.f.m.pre = false := by
1132
+ simp only [checkSMachine, Bool.and_eq_true] at hl; exact ⟨hl.1.1.1.1.2, by simpa using hl.1.1.1.2⟩
1133
+
1134
+ theorem sfacts_ok {s : SMachine} {ps : List SEntry} (hl : checkSMachine s ps = true) : factsOk s = true := by
1135
+ simp only [checkSMachine, Bool.and_eq_true] at hl; exact hl.1.2
1136
+
1137
+ theorem slaws_avoid {s : SMachine} {ps : List SEntry} (hl : checkSMachine s ps = true) :
1138
+ ∀ l ∈ ps.map (·.law), avoids s.extVars l.holds = true ∧ avoidsMaps s.carried l.holds = true := by
1139
+ simp only [checkSMachine, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at hl
1140
+ intro l hl'
1141
+ obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl'
1142
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨h1, h2⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩, _⟩ := hl.2 e' he'
1143
+ refine ⟨?_, h2⟩
1144
+ simp only [avoids, List.all_append, Bool.and_eq_true] at h1 ⊢; exact h1.1
1145
+
1146
+ /-! Entries, read at states -/
1147
+
1148
+ theorem upd_self (ρ : Nat → Nat) (x : Nat) : upd ρ x (ρ x) = ρ := funext fun i => by simp only [upd]; split <;> simp_all
1149
+ theorem upd_upd_same (ρ : Nat → Nat) (x u w : Nat) : upd (upd ρ x u) x w = upd ρ x w := funext fun i => by simp only [upd]; split <;> rfl
1150
+ theorem upd_comm (ρ : Nat → Nat) {x y : Nat} (h : x ≠ y) (u w : Nat) : upd (upd ρ x u) y w = upd (upd ρ y w) x u := funext fun i => by
1151
+ simp only [upd]; by_cases hy : i = y <;> by_cases hx : i = x <;> simp_all
1152
+
1153
+ -- An entry holds exactly where its law holds at every state that agrees with the state but at the further keys.
1154
+ theorem holdsOver_any {l : MLaw} {M : Nat → Nat → Nat} : ∀ (gs : List Nat) {Y : Nat → Nat}, holdsOver l M gs Y → ∀ Z, Agree gs Z Y → l.Holds Z M
1155
+ | [], Y, h, Z, hag => by
1156
+ have hZ : Z = Y := funext fun i => hag i (by simp)
1157
+ subst hZ; exact h
1158
+ | g :: gs, Y, h, Z, hag => holdsOver_any gs (h (Z g)) Z (fun i hi => by
1159
+ simp only [upd]; split
1160
+ · rename_i hig; subst hig; rfl
1161
+ · rename_i hig; exact hag i (by simp only [List.mem_cons, not_or]; exact ⟨hig, hi⟩))
1162
+
1163
+ theorem holdsOver_of_any {l : MLaw} {M : Nat → Nat → Nat} : ∀ (gs : List Nat) {Y : Nat → Nat}, (∀ Z, Agree gs Z Y → l.Holds Z M) → holdsOver l M gs Y
1164
+ | [], Y, h => h Y (fun _ _ => rfl)
1165
+ | g :: gs, Y, h => by
1166
+ intro w
1167
+ exact holdsOver_of_any gs (fun Z hag => h Z (fun i hi => by
1168
+ simp only [List.mem_cons, not_or] at hi
1169
+ rw [hag i hi.2]; simp only [upd, if_neg hi.1]))
1170
+
1171
+ theorem SEntry.holds_law {e : SEntry} {ρ : Nat → Nat} {M : Nat → Nat → Nat} (h : e.Holds ρ M) : e.law.Holds ρ M :=
1172
+ holdsOver_any e.keys2 h ρ (fun _ _ => rfl)
1173
+
1174
+ -- An entry read where its law reads alike holds alike (at every value of its further keys).
1175
+ theorem sentry_transfer {V MS : List Nat} {e : SEntry} (hv : avoids V e.law.holds = true) (hm : avoidsMaps MS e.law.holds = true)
1176
+ {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat} (hag : Agree V ρ ρ') (hmag : ∀ mp, mp ∉ MS → M mp = M' mp) : e.Holds ρ' M' → e.Holds ρ M := by
1177
+ intro h
1178
+ apply holdsOver_of_any
1179
+ intro Z hZ
1180
+ have h' := holdsOver_any e.keys2 h (fun i => if i ∈ e.keys2 then Z i else ρ' i) (fun i hi => by simp [hi])
1181
+ exact holds_transfer e.law hv hm (fun i hiV => by
1182
+ by_cases hk : i ∈ e.keys2
1183
+ · simp [hk]
1184
+ · simp only [hk, if_false]; rw [hZ i hk]; exact hag i hiV) hmag h'
1185
+
1186
+ -- Setting a variable the law reads nowhere, or one of its further keys, keeps the entry.
1187
+ theorem sentry_upd {e : SEntry} {x : Nat} (hx : occC x e.law.holds = false ∨ x ∈ e.keys2) {ρ : Nat → Nat} {M : Nat → Nat → Nat} (u : Nat) :
1188
+ e.Holds ρ M → e.Holds (upd ρ x u) M := by
1189
+ intro h
1190
+ apply holdsOver_of_any
1191
+ intro Z hZ
1192
+ rcases hx with hx | hx
1193
+ · have h' := holdsOver_any e.keys2 h (upd Z x (ρ x)) (fun i hi => by
1194
+ simp only [upd]; split
1195
+ · rename_i hix; subst hix; rfl
1196
+ · rename_i hix; rw [hZ i hi]; simp only [upd, if_neg hix])
1197
+ exact holds_transfer (V := [x]) (MS := []) e.law (by simp [avoids, hx]) (by simp [avoidsMaps])
1198
+ (fun i hi => by simp only [upd]; rw [if_neg (by simpa using hi)]) (fun _ _ => rfl) h'
1199
+ · exact holdsOver_any e.keys2 h Z (fun i hi => by
1200
+ rw [hZ i hi]; simp only [upd]; rw [if_neg (fun h' => hi (by rw [h']; exact hx))])
1201
+
1202
+ -- Each tuple's law holds wherever every entry does (the law's own key none of its further keys).
1203
+ theorem pairLaws_hold (ps : List SEntry) (pinsts : List (Nat × E × List E)) (hkk : ∀ e ∈ ps, ∀ k, e.law.key = some k → k ∉ e.keys2)
1204
+ {Y : Nat → Nat} {N : Nat → Nat → Nat} (hall : ∀ e ∈ ps, e.Holds Y N) : ∀ l ∈ pairLaws ps pinsts, l.Holds Y N := by
1205
+ have htrue : (⟨trueC, none⟩ : MLaw).Holds Y N := by simp [MLaw.Holds, trueC, evalC, evalE]
1206
+ intro l hl
1207
+ simp only [pairLaws, List.mem_map] at hl
1208
+ obtain ⟨p, _, rfl⟩ := hl
1209
+ split
1210
+ · rename_i e he
1211
+ have hem : e ∈ ps := List.mem_of_getElem? he
1212
+ have hany := holdsOver_any e.keys2 (hall e hem)
1213
+ have hdom : ∀ i, i ∉ e.keys2 → i ∉ (e.keys2.zip p.2.2).map (·.1) := fun i hi hm => by
1214
+ obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hm; exact hi (List.of_mem_zip hq).1
1215
+ cases hk : e.law.key with
1216
+ | none =>
1217
+ have h' := hany (applyS (e.keys2.zip p.2.2) Y N) (fun i hi => applyS_other (hdom i hi) Y N)
1218
+ simp only [MLaw.Holds, hk] at h' ⊢
1219
+ rw [subst_evalC]; exact h'
1220
+ | some k =>
1221
+ have hkg := hkk e hem k hk
1222
+ simp only [MLaw.Holds, hk]
1223
+ intro v
1224
+ rw [subst_evalC]
1225
+ have h' := hany (fun i => if i ∈ e.keys2 then applyS (e.keys2.zip p.2.2) (upd Y k v) N i else Y i) (fun i hi => by simp [hi])
1226
+ simp only [MLaw.Holds, hk] at h'
1227
+ have hZ : upd (fun i => if i ∈ e.keys2 then applyS (e.keys2.zip p.2.2) (upd Y k v) N i else Y i) k v = applyS (e.keys2.zip p.2.2) (upd Y k v) N := by
1228
+ funext i
1229
+ by_cases hik : i = k
1230
+ · subst hik; simp only [upd, if_true]; rw [applyS_other (hdom i hkg)]; simp [upd]
1231
+ · simp only [upd, if_neg hik]
1232
+ by_cases hi : i ∈ e.keys2
1233
+ · simp [hi]
1234
+ · simp only [hi, if_false]; rw [applyS_other (hdom i hi)]; simp [upd, hik]
1235
+ rw [← hZ]; exact h' v
1236
+ · exact htrue
1237
+
1238
+ /-! Facts about several keys -/
1239
+
1240
+ -- A law at every value of some keys, read where it reads alike, holds alike.
1241
+ theorem holdsOver_transfer {V MS : List Nat} (l : MLaw) (hv : avoids V l.holds = true) (hm : avoidsMaps MS l.holds = true) :
1242
+ ∀ (gs : List Nat) {ρ ρ' : Nat → Nat} {M M' : Nat → Nat → Nat}, Agree V ρ ρ' → (∀ mp, mp ∉ MS → M mp = M' mp) →
1243
+ holdsOver l M' gs ρ' → holdsOver l M gs ρ
1244
+ | [], _, _, _, _, hag, hmag, h => holds_transfer l hv hm hag hmag h
1245
+ | g :: gs, ρ, ρ', _, _, hag, hmag, h => fun w =>
1246
+ holdsOver_transfer l hv hm gs (ρ := upd ρ g w) (ρ' := upd ρ' g w) (fun i hi => by
1247
+ simp only [upd]; split
1248
+ · rfl
1249
+ · exact hag i hi) hmag (h w)
1250
+
1251
+ -- A law at every value of its further keys holds with each set to a term (the law about its first key, at the tuple).
1252
+ theorem holdsOver_subst (l : MLaw) (gs : List Nat) (ts : List E) (hkk : ∀ k, l.key = some k → k ∉ gs)
1253
+ {Y : Nat → Nat} {N : Nat → Nat → Nat} (h : holdsOver l N gs Y) : (⟨substC (gs.zip ts) l.holds, l.key⟩ : MLaw).Holds Y N := by
1254
+ have hany := holdsOver_any gs h
1255
+ have hdom : ∀ i, i ∉ gs → i ∉ (gs.zip ts).map (·.1) := fun i hi hm => by
1256
+ obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hm; exact hi (List.of_mem_zip hq).1
1257
+ cases hk : l.key with
1258
+ | none =>
1259
+ have h' := hany (applyS (gs.zip ts) Y N) (fun i hi => applyS_other (hdom i hi) Y N)
1260
+ simp only [MLaw.Holds, hk] at h' ⊢
1261
+ rw [subst_evalC]; exact h'
1262
+ | some k =>
1263
+ have hkg := hkk k hk
1264
+ simp only [MLaw.Holds, hk]
1265
+ intro v
1266
+ rw [subst_evalC]
1267
+ have h' := hany (fun i => if i ∈ gs then applyS (gs.zip ts) (upd Y k v) N i else Y i) (fun i hi => by simp [hi])
1268
+ simp only [MLaw.Holds, hk] at h'
1269
+ have hZ : upd (fun i => if i ∈ gs then applyS (gs.zip ts) (upd Y k v) N i else Y i) k v = applyS (gs.zip ts) (upd Y k v) N := by
1270
+ funext i
1271
+ by_cases hik : i = k
1272
+ · subst hik; simp only [upd, if_true]; rw [applyS_other (hdom i hkg)]; simp [upd]
1273
+ · simp only [upd, if_neg hik]
1274
+ by_cases hi : i ∈ gs
1275
+ · simp [hi]
1276
+ · simp only [hi, if_false]; rw [applyS_other (hdom i hi)]; simp [upd, hik]
1277
+ rw [← hZ]; exact h' v
1278
+
1279
+ theorem pfactsOk_parts {s : SMachine} (h : factsOk s = true) : ∀ f ∈ s.pfacts,
1280
+ avoids (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) f.1.holds = true
1281
+ ∧ avoidsMaps (s.carried ++ s.every.map (·.1) ++ s.f.m.writes.map (·.1)) f.1.holds = true ∧ ∀ k, f.1.key = some k → k ∉ f.2 := by
1282
+ simp only [factsOk, pfactsOk, Bool.and_eq_true, List.all_eq_true, bne_iff_ne, ne_eq] at h
1283
+ intro f hf
1284
+ obtain ⟨⟨h1, h2⟩, h3⟩ := h.2 f hf
1285
+ exact ⟨h1, h2, fun k hk hm => h3 k hm (by rw [hk])⟩
1286
+
1287
+ -- The facts about several keys at the tuples a goal names hold wherever the facts do.
1288
+ theorem pfactLaws_hold (s : SMachine) (pfinsts : List (Nat × E × List E)) (hkk : ∀ f ∈ s.pfacts, ∀ k, f.1.key = some k → k ∉ f.2)
1289
+ {Y : Nat → Nat} {N : Nat → Nat → Nat} (hp : s.PFacts Y N) : ∀ l ∈ pfinsts.map (pfactLaw s), l.Holds Y N := by
1290
+ intro l hl
1291
+ obtain ⟨p, _, rfl⟩ := List.mem_map.mp hl
1292
+ unfold pfactLaw
1293
+ split
1294
+ · rename_i f hf
1295
+ have hfm : f ∈ s.pfacts := List.mem_of_getElem? hf
1296
+ exact holdsOver_subst f.1 f.2 p.2.2 (hkk f hfm) (hp f hfm)
1297
+ · simp [MLaw.Holds, trueC, evalC, evalE]
1298
+
1299
+ -- A goal's facts all hold where the facts and the facts about several keys do.
1300
+ theorem facthyps_true (s : SMachine) (finsts : List (Nat × E)) (pfinsts : List (Nat × E × List E)) (Y : Nat → Nat) (N : Nat → Nat → Nat)
1301
+ (hkk : ∀ f ∈ s.pfacts, ∀ k, f.1.key = some k → k ∉ f.2) (hf : ∀ l ∈ s.facts, l.Holds Y N) (hp : s.PFacts Y N) :
1302
+ ∀ c ∈ factHyps s finsts pfinsts, evalC Y N c = true := by
1303
+ intro c hc
1304
+ simp only [factHyps] at hc
1305
+ rcases List.mem_append.mp hc with hc | hc
1306
+ · exact laws_hyps_true s.facts finsts Y N hf c hc
1307
+ · exact laws_hyps_true (pfinsts.map (pfactLaw s)) (pairInsts 0 pfinsts) Y N (pfactLaws_hold s pfinsts hkk hp) c (List.mem_append_right _ hc)
1308
+
1309
+ -- The facts about several keys hold in every reachable state, and wherever what they read is the state's.
1310
+ theorem pfacts_sreach (s : SMachine) (hf : factsOk s = true) : ∀ {ρ M}, SReach s ρ M → s.PFacts ρ M := by
1311
+ have hf := pfactsOk_parts hf
1312
+ intro ρ M h
1313
+ induction h with
1314
+ | start ρ M _ _ _ hpf =>
1315
+ intro f hfm
1316
+ exact holdsOver_transfer f.1 (hf f hfm).1 (MS := []) (by simp [avoidsMaps]) f.2
1317
+ (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ M) (fun _ _ => rfl) (hpf f hfm)
1318
+ | step ρ M ι κ _ _ ih =>
1319
+ intro f hfm
1320
+ refine holdsOver_transfer f.1 (hf f hfm).1 (hf f hfm).2.1 f.2 (fun i hi => ?_) (fun mp hmp => ?_) (ih f hfm)
1321
+ · simp only [sNext, sView]
1322
+ rw [applyS_other (fun h => hi (by simp [h])), decide_other (fun h => hi (by simp [h]))]
1323
+ simp only [decideQ]
1324
+ rw [lookup_none (fun q hq => by
1325
+ simp only [beq_eq_false_iff_ne]; intro he
1326
+ exact hi (by simp only [SMachine.extVars, SMachine.qVars, List.mem_append, List.mem_map]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inr ⟨q, hq, he⟩))))))]
1327
+ exact withInputs_other (fun h => hi (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inl (Or.inl (Or.inl (Or.inl h)))))) ρ ι
1328
+ · simp only [List.mem_append, not_or] at hmp
1329
+ obtain ⟨⟨hmc, hme⟩, hmw⟩ := hmp
1330
+ funext key
1331
+ simp only [sNext, sView, everyMaps]
1332
+ rw [lookup_none (fun w hw => by simp only [beq_eq_false_iff_ne]; intro he; exact hme (by rw [← he]; exact List.mem_map_of_mem hw))]
1333
+ simp only [writeMaps]
1334
+ rw [lookup_none (fun w hw => by simp only [beq_eq_false_iff_ne]; intro he; exact hmw (by rw [← he]; exact List.mem_map_of_mem hw))]
1335
+ simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]
1336
+
1337
+ theorem pfacts_frame (s : SMachine) (hfok : factsOk s = true) (Vx Mx : List Nat)
1338
+ (hvx : ∀ f ∈ s.pfacts, avoids Vx f.1.holds = true) (hmx : ∀ f ∈ s.pfacts, avoidsMaps Mx f.1.holds = true)
1339
+ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {X : Nat → Nat} {N : Nat → Nat → Nat}
1340
+ (hX : ∀ i, i ∉ s.extVars → i ∉ Vx → X i = ρ i) (hN : ∀ mp, mp ∉ s.carried → mp ∉ Mx → N mp = M mp)
1341
+ (hl : s.PFacts ρ M) : s.PFacts X N := by
1342
+ have hp := pfactsOk_parts hfok
1343
+ intro f hf'
1344
+ refine holdsOver_transfer (V := (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)) ++ Vx)
1345
+ (MS := (s.carried ++ s.every.map (·.1) ++ s.f.m.writes.map (·.1)) ++ Mx) f.1
1346
+ (by have h1 := (hp f hf').1; simp only [avoids, List.all_append, Bool.and_eq_true] at hvx h1 ⊢; exact ⟨h1, hvx f hf'⟩)
1347
+ (by have h2 := (hp f hf').2.1; simp only [avoidsMaps, List.all_append, Bool.and_eq_true] at hmx h2 ⊢; exact ⟨h2, hmx f hf'⟩) f.2
1348
+ (fun i hi => hX i (fun h => hi (by simp [h])) (fun h => hi (by simp [h])))
1349
+ (fun mp hmp => hN mp (fun h => hmp (by simp [h])) (fun h => hmp (by simp [h]))) (hl f hf')
1350
+
1351
+ -- A variable fresh as a skolem: setting it commutes with a step's view and with the step.
1352
+ theorem fresh_parts {s : SMachine} {x : Nat} (hf : skFresh s x = true)
1353
+ (hc : (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains x = false) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
1354
+ (ι : Nat → Nat) (κ : Nat → Nat → Nat) (v : Nat) :
1355
+ sNext s (upd ρ x v) M ι κ = (upd (sNext s ρ M ι κ).1 x v, (sNext s ρ M ι κ).2)
1356
+ ∧ sView s (upd ρ x v) M ι κ = (upd (sView s ρ M ι κ).1 x v, (sView s ρ M ι κ).2)
1357
+ ∧ x ∉ s.f.m.init.map (·.1) := by
1358
+ obtain ⟨hf1, hf2, _, hf4, hf5, hf6, _⟩ := skFresh_parts hf
1359
+ have hnot : x ∉ s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1) := fun hm => by
1360
+ rw [List.contains_iff_mem.mpr hm] at hc; cases hc
1361
+ have hE : x ∉ s.extVars := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ hm)))
1362
+ have hF : x ∉ flagVars s.f.flags := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_right _ hm)))
1363
+ have hS : x ∉ s.f.m.step.map (·.1) := fun hm => hnot (List.mem_append_right _ hm)
1364
+ have hI : x ∉ s.f.m.init.map (·.1) := fun hm => hnot (List.mem_append_left _ (List.mem_append_right _ hm))
1365
+ have hi : x ∉ s.f.m.inputs := fun hm => hE (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inl hm))
1366
+ have hq : x ∉ s.qVars := fun hm => hE (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inr hm))
1367
+ exact ⟨sNext_upd hi hq hF hf2 hf1 hS hf4 hf5 hf6 ρ M ι κ v, sView_upd hi hq hF hf2 hf1 ρ M ι κ v, hI⟩
1368
+
1369
+ theorem skey2_parts {s : SMachine} {ps : List SEntry} (h : checkSMachine s ps = true) {e : SEntry} (he : e ∈ ps) {g : Nat} (hg : g ∈ e.keys2) :
1370
+ skFresh s g = true ∧ (s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains g = false
1371
+ ∧ occC g s.f.m.pre = false ∧ (∀ p ∈ s.f.m.init, occE g p.2 = false) ∧ (∀ e' ∈ ps, occC g e'.law.holds = false ∨ g ∈ e'.keys2)
1372
+ ∧ e.law.key ≠ some g := by
1373
+ simp only [checkSMachine, List.all_eq_true, Bool.and_eq_true] at h
1374
+ have hk := (h.2 e he).1.1.2
1375
+ simp only [key2Ok, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true', bne_iff_ne, ne_eq, Bool.or_eq_true, List.contains_iff_mem] at hk
1376
+ obtain ⟨⟨⟨⟨⟨hkey, hf⟩, hc⟩, hp⟩, hi⟩, hall⟩ := hk g hg
1377
+ exact ⟨hf, hc, hp, hi, fun e' he' => (by have := (hall e' he').1; simpa using this), hkey⟩
1378
+
1379
+ theorem skeys_apart {s : SMachine} {ps : List SEntry} (h : checkSMachine s ps = true) : ∀ e ∈ ps, ∀ k, e.law.key = some k → k ∉ e.keys2 :=
1380
+ fun e he k hk hg => (skey2_parts h he hg).2.2.2.2.2 hk
1381
+
1382
+ /-- The start: at params that meet the precondition and the facts, with maps as declared, every law holds. -/
1383
+ theorem sstart_case (s : SMachine) (ps : List SEntry) (h : checkSMachine s ps = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
1384
+ (hM : StartMaps s.f.m M) (h0 : evalC ρ M s.f.m.pre = true) (hfa : ∀ l ∈ s.facts, l.Holds ρ M) (hpf : s.PFacts ρ M) :
1385
+ ∀ e ∈ ps, e.law.Holds (applyS s.f.m.init ρ M) M := by
1386
+ simp only [checkSMachine, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at h
1387
+ obtain ⟨⟨⟨⟨⟨_, _⟩, _⟩, _⟩, hfok⟩, h⟩ := h
1388
+ intro e he
1389
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨_, _⟩, hfresh⟩, hslaw⟩, hspre⟩, hsinit⟩, hskf⟩, _⟩, hst⟩, _⟩ := h e he
1390
+ refine goalOf_holds e.law (hslaw e he) _ M (fun v => ?_)
1391
+ apply Decidable.byContradiction; intro hn
1392
+ apply sound_in _ _ hst (upd ρ e.sk v) M
1393
+ intro c hc
1394
+ simp only [sStartGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
1395
+ rcases hc with (rfl | hc) | rfl
1396
+ · rw [congrC _ ρ M s.f.m.pre (fun i hi => by
1397
+ simp only [upd]; split
1398
+ · rename_i hik; subst hik; simp [hspre] at hi
1399
+ · rfl)]
1400
+ exact h0
1401
+ · -- The facts hold at the start, and read not the skolem.
1402
+ exact facthyps_true s e.finsts e.pfinsts _ M (fun f hf => (pfactsOk_parts hfok f hf).2.2)
1403
+ (facts_frame s hfok [e.sk] [] (fun l hl => by simp [avoids, (skFresh_parts hskf).2.2.2.2.2.2 l hl])
1404
+ (fun _ _ => by simp [avoidsMaps]) (fun i _ hik => by simp only [upd]; rw [if_neg (by simpa using hik)]) (fun _ _ _ => rfl) hfa)
1405
+ (pfacts_frame s hfok [e.sk] [] (fun f hf => by simp [avoids, skFresh_pfacts hskf f hf])
1406
+ (fun _ _ => by simp [avoidsMaps]) (fun i _ hik => by simp only [upd]; rw [if_neg (by simpa using hik)]) (fun _ _ _ => rfl) hpf) c hc
1407
+ · have hrd : ∀ mp k, evalE (upd ρ e.sk v) M (rdStart s.f.m mp k) = M mp (evalE (upd ρ e.sk v) M k) := by
1408
+ intro mp k
1409
+ simp only [rdStart]
1410
+ split
1411
+ · rename_i p hp
1412
+ have ⟨hm, hq⟩ := lookup_some hp
1413
+ simp only [beq_iff_eq] at hq
1414
+ simp only [evalE]; rw [← hq, hM p hm]
1415
+ · rfl
1416
+ simp only [evalC, substR_evalC _ _ _ M M hrd]
1417
+ rw [applyS_upd (fun h => by simp [h] at hfresh) (by simpa [List.all_eq_true] using hsinit)]
1418
+ simpa using hn
1419
+
1420
+ /-- One step: from a state where the precondition, the facts and every entry hold, a request allowed keeps each law. -/
1421
+ theorem sstep_case (s : SMachine) (ps : List SEntry) (h : checkSMachine s ps = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat)
1422
+ (ι : Nat → Nat) (κ : Nat → Nat → Nat) (hp0 : evalC ρ M s.f.m.pre = true) (hfa : ∀ l ∈ s.facts, l.Holds ρ M) (hpf : s.PFacts ρ M)
1423
+ (ih : ∀ e ∈ ps, e.Holds ρ M) (ha : evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true) :
1424
+ ∀ e ∈ ps, e.law.Holds (sNext s ρ M ι κ).1 (sNext s ρ M ι κ).2 := by
1425
+ have hlav := slaws_avoid h
1426
+ have hkk := skeys_apart h
1427
+ simp only [checkSMachine, List.all_eq_true, Bool.and_eq_true, Bool.not_eq_true'] at h
1428
+ obtain ⟨⟨⟨⟨⟨hs, hpre⟩, hrp⟩, hterms⟩, hfok⟩, h⟩ := h
1429
+ obtain ⟨hfav, hstp, hwr, hev⟩ := sTermsOk_parts hterms
1430
+ intro e he
1431
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨hlx, hlm⟩, hfresh⟩, hslaw⟩, hspre⟩, _⟩, hskf⟩, _⟩, _⟩, hst⟩ := h e he
1432
+ obtain ⟨hf1, hf2, hf3, hf4, hf5, hf6, hf7⟩ := skFresh_parts hskf
1433
+ have hnot : e.sk ∉ s.extVars ++ flagVars s.f.flags ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1) := fun hm => by
1434
+ rw [List.contains_iff_mem.mpr hm] at hfresh; cases hfresh
1435
+ have hskE : e.sk ∉ s.extVars := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ hm)))
1436
+ have hskF : e.sk ∉ flagVars s.f.flags := fun hm => hnot (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_right _ hm)))
1437
+ -- The law at the next state with the carried maps in place reads as at the next state: it reads none of them.
1438
+ refine holds_transfer (V := []) (MS := s.carried) (ρ' := (sNext s ρ M ι κ).1) (M' := withCarried s (sNext s ρ M ι κ).2 κ) e.law (by simp [avoids]) hlm (fun i _ => rfl)
1439
+ (fun mp hmp => by simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]) ?_
1440
+ refine goalOf_holds e.law (hslaw e he) _ _ (fun v => ?_)
1441
+ apply Decidable.byContradiction; intro hn
1442
+ -- The frame runs from the state with the skolem set to v: the machine reads it nowhere, so the step is the same.
1443
+ obtain ⟨hNext, hView, _⟩ := fresh_parts hskf (by simpa [List.contains_iff_mem] using hnot) ρ M ι κ v
1444
+ let W := withExt s (upd ρ e.sk v) (sReq s (upd ρ e.sk v) M ι κ)
1445
+ let M0 := withCarried s M κ
1446
+ have hH : ∀ Z, SHead s Z [] W M0 [] getRd (upd ρ e.sk v) M [] [] ι κ := fun Z => by
1447
+ refine ⟨fun i _ => ?_, fun mp k _ => ?_⟩
1448
+ · show applyS (frameSub [] []) W M0 i = W i
1449
+ simp only [frameSub, List.map_nil, List.append_nil, applyS_nil']
1450
+ · simp [frameRd, getRd, lookup, evalE, M0]
1451
+ have hD : SDenotes s (flagVars s.f.flags) [] W M0 [] getRd (upd ρ e.sk v) M := by
1452
+ refine ⟨fun i hie _ => ?_, fun mp k hmp _ => ?_⟩
1453
+ · rw [applyS_nil']; simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]
1454
+ · simp only [getRd, evalE, M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]
1455
+ have hD' := sstep_denotes s (flagVars s.f.flags) [] W M0 hs (fun v hv => hv) hfav [] [] ι κ hD (hH _) (fun p hp => by simp at hp)
1456
+ hstp (fun w hw => (hwr w hw).1) hev (fun _ _ => by simp [avoidsMapsE]) (fun _ _ => by simp [avoidsMapsE]) (fun _ _ => by simp [avoidsMapsE])
1457
+ apply sound_in _ _ hst W M0
1458
+ intro c hc
1459
+ simp only [sStepGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false, List.mem_map] at hc
1460
+ rcases hc with ((rfl | hc) | ⟨c', hc', rfl⟩) | rfl
1461
+ · -- The precondition: it reads no map, no request and not the skolem.
1462
+ rw [noReadsC W M0 M s.f.m.pre hrp, congrC W ρ M s.f.m.pre (fun i hi => ?_)]
1463
+ · exact hp0
1464
+ · have hi1 := avoids_occ hpre hi
1465
+ have hik : i ≠ e.sk := fun h' => by rw [h'] at hi; simp [hspre] at hi
1466
+ simp only [W, withExt]
1467
+ rw [if_neg (fun h' => hi1 (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.contains_iff_mem.mp h')))))]
1468
+ simp only [upd, if_neg hik]
1469
+ · -- The facts: they read no request, not the skolem, and no map a request carries.
1470
+ exact facthyps_true s e.finsts e.pfinsts W M0 (fun f hf => (pfactsOk_parts hfok f hf).2.2)
1471
+ (facts_frame s hfok [e.sk] [] (fun l hl => by simp [avoids, hf7 l hl])
1472
+ (fun _ _ => by simp [avoidsMaps])
1473
+ (fun i hie hik => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]; simp only [upd]; rw [if_neg (by simpa using hik)])
1474
+ (fun mp hmc _ => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]) hfa)
1475
+ (pfacts_frame s hfok [e.sk] [] (fun f hf => by simp [avoids, skFresh_pfacts hskf f hf])
1476
+ (fun _ _ => by simp [avoidsMaps])
1477
+ (fun i hie hik => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)]; simp only [upd]; rw [if_neg (by simpa using hik)])
1478
+ (fun mp hmc _ => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]) hpf) c hc
1479
+ · -- The entries hold at the state with the skolem set (their laws don't read it), so in the goal's view of it, and so
1480
+ -- does each pair's law; the assumption reads the skolem nowhere either.
1481
+ have hA : evalC (sView s (upd ρ e.sk v) M ι κ).1 (sView s (upd ρ e.sk v) M ι κ).2 s.f.m.assume = true := by
1482
+ rw [hView]; dsimp only
1483
+ rw [congrC _ (sView s ρ M ι κ).1 _ _ (fun i hi => by
1484
+ have hik : i ≠ e.sk := fun h' => by rw [h', hf3] at hi; cases hi
1485
+ simp only [upd, if_neg hik])]
1486
+ exact ha
1487
+ have hallY : ∀ e' ∈ ps, e'.Holds W M0 := fun e' he' =>
1488
+ sentry_transfer (hlav e'.law (List.mem_map_of_mem he')).1 (hlav e'.law (List.mem_map_of_mem he')).2
1489
+ (fun i hi => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi)])
1490
+ (fun mp hmp => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)])
1491
+ (sentry_upd (Or.inl (hslaw e' he')) v (ih e' he'))
1492
+ exact sframe_hyps s [] [] W M0 hs (sLaws ps e.pinsts) (sInsts ps e.insts e.pinsts) e.qinsts
1493
+ (fun l hl => by
1494
+ rcases List.mem_append.mp hl with hl | hl
1495
+ · obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl; exact SEntry.holds_law (hallY e' he')
1496
+ · exact pairLaws_hold ps e.pinsts hkk hallY l hl)
1497
+ (hH []) (fun _ _ => by simp [avoids]) (fun _ _ => by simp [avoids]) (fun _ _ => by simp [avoidsMaps]) hA (by simp [trueC, evalC, evalE]) c' hc'
1498
+ · -- The goal, read across the step: the maps a request carries read as carried, the rest as the step leaves them.
1499
+ have hrd' : ∀ mp k, evalE W M0 (stepRdS s (frameSub [] []) (frameRd [] getRd) mp k) = withCarried s (sNext s ρ M ι κ).2 κ mp (evalE W M0 k) := by
1500
+ intro mp k
1501
+ by_cases hmp : mp ∈ s.carried
1502
+ · have hne : lookup (fun w => w.1 == mp) s.every = none := lookup_none (fun w hw => by
1503
+ simp only [beq_eq_false_iff_ne]; intro h'; exact (hev w hw).2.2.2.2.1 (by rw [h']; exact hmp))
1504
+ have hnw : lookup (fun w : Nat × E × E => w.1 == mp) s.f.m.writes = none := lookup_none (fun w hw => by
1505
+ simp only [beq_eq_false_iff_ne]; intro h'; exact (hwr w hw).2 (by rw [h']; exact hmp))
1506
+ simp only [stepRdS, hne, stepRd, hnw]
1507
+ simp [frameRd, getRd, lookup, evalE, M0, withCarried, hmp]
1508
+ · rw [hD'.2 mp k hmp (by simp), hNext]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]
1509
+ simp only [evalC]
1510
+ rw [substR_evalC _ _ W M0 _ hrd', goalOf_agree e.law e.sk _ (X' := upd (sNext s ρ M ι κ).1 e.sk v)
1511
+ (by rw [hD'.1 e.sk hskE hskF, hNext]) (fun i hi => ?_)]
1512
+ · simpa using hn
1513
+ · have hi1 := avoids_occ hlx hi
1514
+ rw [hD'.1 i (fun h' => hi1 (List.mem_append_left _ h')) (fun h' => hi1 (List.mem_append_right _ h')), hNext]
1515
+
1516
+ /-- The induction rule with sets: for params that meet the precondition, maps that start as declared and functions that
1517
+ meet the facts, every entry of an accepted proof holds in every reachable state (a law about several keys at every value
1518
+ of its further keys), whatever maps each request carries. -/
1519
+ theorem smachine_sound (s : SMachine) (ps : List SEntry) (h : checkSMachine s ps = true) :
1520
+ ∀ ρ M, SReach s ρ M → ∀ e ∈ ps, e.Holds ρ M := by
1521
+ have hpre := spre_parts h
1522
+ have hfok := sfacts_ok h
1523
+ -- Facts at a state hold with a further key set: they read no further key.
1524
+ have hfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → (∀ l ∈ s.facts, l.Holds ρ M) → ∀ w, ∀ l ∈ s.facts, l.Holds (upd ρ g w) M := by
1525
+ intro ρ M g hf hfa w
1526
+ exact facts_frame s hfok [g] [] (fun l hl => by simp [avoids, (skFresh_parts hf).2.2.2.2.2.2 l hl]) (fun _ _ => by simp [avoidsMaps])
1527
+ (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hfa
1528
+ have hpfacts : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, skFresh s g = true → s.PFacts ρ M → ∀ w, s.PFacts (upd ρ g w) M := by
1529
+ intro ρ M g hf hpf w
1530
+ exact pfacts_frame s hfok [g] [] (fun f hfm => by simp [avoids, skFresh_pfacts hf f hfm]) (fun _ _ => by simp [avoidsMaps])
1531
+ (fun i _ hig' => by simp only [upd]; rw [if_neg (by simpa using hig')]) (fun _ _ _ => rfl) hpf
1532
+ have hpreu : ∀ {ρ : Nat → Nat} {M : Nat → Nat → Nat} {g : Nat}, occC g s.f.m.pre = false → evalC ρ M s.f.m.pre = true → ∀ w, evalC (upd ρ g w) M s.f.m.pre = true := by
1533
+ intro ρ M g hpg h0 w
1534
+ rw [congrC _ ρ M s.f.m.pre (fun i hi => by have hig' : i ≠ g := (fun h' => by rw [h', hpg] at hi; cases hi); simp only [upd, if_neg hig'])]; exact h0
1535
+ intro ρ M hr
1536
+ induction hr with
1537
+ | start ρ M hM h0 hfa hpf =>
1538
+ intro e he
1539
+ -- Each further key set in turn, from a start state that meets the precondition and the facts.
1540
+ suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →
1541
+ s.PFacts ρ' M → holdsOver e.law M gs (applyS s.f.m.init ρ' M) from hs e.keys2 (fun g hg => hg) ρ h0 hfa hpf
1542
+ intro gs
1543
+ induction gs with
1544
+ | nil => intro _ ρ' hp hf hq; exact sstart_case s ps h ρ' M hM hp hf hq e he
1545
+ | cons g gs ih =>
1546
+ intro hsub ρ' hp hf hq w
1547
+ obtain ⟨hfr, hc, hpg, hig, _, _⟩ := skey2_parts h he (hsub g List.mem_cons_self)
1548
+ have hI := (fresh_parts hfr hc ρ' M (fun _ => 0) (fun _ _ => 0) 0).2.2
1549
+ rw [← applyS_upd hI (by simpa [List.all_eq_true] using hig)]
1550
+ exact ih (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)
1551
+ | step ρ M ι κ hr ha ih =>
1552
+ intro e he
1553
+ suffices hs : ∀ gs : List Nat, (∀ g ∈ gs, g ∈ e.keys2) → ∀ ρ', evalC ρ' M s.f.m.pre = true → (∀ l ∈ s.facts, l.Holds ρ' M) →
1554
+ s.PFacts ρ' M → (∀ e' ∈ ps, e'.Holds ρ' M) → evalC (sView s ρ' M ι κ).1 (sView s ρ' M ι κ).2 s.f.m.assume = true →
1555
+ holdsOver e.law (sNext s ρ' M ι κ).2 gs (sNext s ρ' M ι κ).1 from
1556
+ hs e.keys2 (fun g hg => hg) ρ (pre_sreach s hpre.1 hpre.2 hr) (facts_sreach s hfok hr) (pfacts_sreach s hfok hr) ih ha
1557
+ intro gs
1558
+ induction gs with
1559
+ | nil => intro _ ρ' hp hf hq hi ha'; exact sstep_case s ps h ρ' M ι κ hp hf hq hi ha' e he
1560
+ | cons g gs ihg =>
1561
+ intro hsub ρ' hp hf hq hi ha' w
1562
+ obtain ⟨hfr, hc, hpg, _, hall, _⟩ := skey2_parts h he (hsub g List.mem_cons_self)
1563
+ obtain ⟨hNext, hView, _⟩ := fresh_parts hfr hc ρ' M ι κ w
1564
+ have := ihg (fun g' hg' => hsub g' (List.mem_cons_of_mem _ hg')) (upd ρ' g w) (hpreu hpg hp w) (hfacts hfr hf w) (hpfacts hfr hq w)
1565
+ (fun e' he' => sentry_upd (hall e' he') w (hi e' he'))
1566
+ (by
1567
+ rw [hView]; dsimp only
1568
+ rw [congrC _ (sView s ρ' M ι κ).1 _ _ (fun i hi => by
1569
+ have hig' : i ≠ g := fun h' => by rw [h', (skFresh_parts hfr).2.2.1] at hi; cases hi
1570
+ simp only [upd, if_neg hig'])]
1571
+ exact ha')
1572
+ rw [hNext] at this; exact this
1573
+
1574
+ /-! k-step laws over sets -/
1575
+
1576
+ structure SKStep where
1577
+ k : KStep -- whens (flags inlined), then, ren (each later step: every extended request variable), primes, insts, proof
1578
+ mren : List (List (Nat × Nat)) -- per step: (carried map, the name the goal reads it by there); [] for the first step
1579
+ qinsts : List (Nat × List E) -- (quantified flag, keys): where each frame closes it
1580
+ finsts : List (Nat × E) -- (fact index, key): where the goal assumes the facts, read at the first frame
1581
+ pinsts : List (Nat × E × List E) -- (law index, key, further keys): where each frame assumes the laws about several keys
1582
+ pfinsts : List (Nat × E × List E) -- (fact index, key, further keys): where the goal assumes the facts about several keys
1583
+ used : List C -- the hypotheses the tree refutes ([] for the goal's own, in its order)
1584
+ names : List (List (Nat × Nat)) -- per step but the last: (state variable, the fresh variable naming it after that step)
1585
+
1586
+ def sKOut (s : SMachine) (ps : List SEntry) (sl : SKStep) := sRun s (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) sl.qinsts sl.k.whens sl.k.ren sl.mren sl.names [] getRd
1587
+ def sKGoal (s : SMachine) (ps : List SEntry) (sl : SKStep) : List C :=
1588
+ [s.f.m.pre] ++ factHyps s sl.finsts sl.pfinsts ++ (sKOut s ps sl).1
1589
+ ++ [.not (substRC (kEndSub sl.k (sKOut s ps sl).2.1 (sKOut s ps sl).2.2) (kEndRd sl.k (sKOut s ps sl).2.2) sl.k.then_)]
1590
+
1591
+ -- The step's terms, flags inlined, read none of the names later requests' maps are read by.
1592
+ def sMapsOk (s : SMachine) (MZ : List Nat) : Bool :=
1593
+ s.f.m.step.all (fun p => avoidsMapsE MZ (inlineE s.f.flags p.2))
1594
+ && s.f.m.writes.all (fun w => avoidsMapsE MZ (inlineE s.f.flags w.2.1) && avoidsMapsE MZ (inlineE s.f.flags w.2.2))
1595
+ && s.every.all (fun w => avoidsMapsE MZ (inlineE s.f.flags w.2.2))
1596
+ && s.facts.all (fun l => avoidsMaps MZ l.holds) && s.pfacts.all (fun f => avoidsMaps MZ f.1.holds)
1597
+
1598
+ def szone (s : SMachine) (sl : SKStep) : List Nat := tgts sl.k.ren ++ flagVars s.f.flags ++ tgts sl.names
1599
+
1600
+ def checkSKStep (s : SMachine) (ps : List SEntry) (sl : SKStep) : Bool :=
1601
+ sl.k.whens.length == sl.k.ren.length && sl.mren.length == sl.k.ren.length && sl.k.ren.head? == some [] && sl.mren.head? == some []
1602
+ && distinct (tgts sl.k.ren) && distinct (tgts sl.mren)
1603
+ && sl.k.ren.all (fun r => r.all fun p => s.extVars.contains p.1)
1604
+ && sl.k.ren.tail.all (fun r => s.extVars.all fun i => r.any (·.1 == i))
1605
+ && sl.mren.all (fun r => r.all fun p => s.carried.contains p.1)
1606
+ && sl.mren.tail.all (fun r => s.carried.all fun mp => r.any (·.1 == mp))
1607
+ && sl.k.prime.all (fun q => !s.extVars.contains q.1 && !(szone s sl).contains q.1)
1608
+ && sl.k.primeMaps.all (fun q => !s.carried.contains q.1 && !(tgts sl.mren).contains q.1)
1609
+ && avoids (szone s sl) s.f.m.pre
1610
+ && sl.k.whens.all (fun w => (sFrameHyps s (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) [] w).all (fun c => avoids (szone s sl) c)
1611
+ && (sFrameHyps s (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) sl.qinsts w).all (fun c => avoidsMaps (tgts sl.mren) c))
1612
+ && s.qflags.all (fun q => avoids (szone s sl) q.2.2)
1613
+ && sTermsOk s (szone s sl) && sMapsOk s (tgts sl.mren)
1614
+ && checkIn (sKGoal s ps sl) ⟨sl.k.proof.1, sl.used, sl.k.proof.2⟩
1615
+ && distinct (tgts sl.k.ren ++ tgts sl.names)
1616
+ && sl.names.all (fun n => n.all fun p => !s.extVars.contains p.1 && !(szone s sl).contains p.1)
1617
+ && avoids (tgts sl.names) sl.k.then_
1618
+
1619
+ /-! What a k-step law over sets claims -/
1620
+
1621
+ abbrev SRequest := (Nat → Nat) × (Nat → Nat → Nat) -- a request's inputs, and the maps it carries
1622
+
1623
+ noncomputable def sEnd (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → (Nat → Nat) × (Nat → Nat → Nat)
1624
+ | ρ, M, [] => (ρ, M)
1625
+ | ρ, M, q :: qs => sEnd s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs
1626
+
1627
+ -- The state after each step.
1628
+ noncomputable def sStates (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → List (Nat → Nat)
1629
+ | _, _, [] => []
1630
+ | ρ, M, q :: qs => (sNext s ρ M q.1 q.2).1 :: sStates s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs
1631
+
1632
+ theorem evalE_assoc_getD (σ : List (Nat × E)) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (v : Nat) :
1633
+ evalE W M0 ((assoc σ v).getD (.var v)) = applyS σ W M0 v := by
1634
+ simp only [applyS]; cases assoc σ v <;> simp [evalE]
1635
+
1636
+ -- Each step's extended request (inputs, quantified flags, witnesses), as its goal reads it.
1637
+ noncomputable def sExts (s : SMachine) : (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → List (Nat → Nat)
1638
+ | _, _, [] => []
1639
+ | ρ, M, q :: qs => sReq s ρ M q.1 q.2 :: sExts s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs
1640
+
1641
+ -- Each request allowed (the assumption, flags decided) and meeting its step's `when`.
1642
+ def SAllowed (s : SMachine) : List C → (Nat → Nat) → (Nat → Nat → Nat) → List SRequest → Prop
1643
+ | w :: ws, ρ, M, q :: qs =>
1644
+ evalC (sView s ρ M q.1 q.2).1 (sView s ρ M q.1 q.2).2 s.f.m.assume = true
1645
+ ∧ evalC (withExt s ρ (sReq s ρ M q.1 q.2)) (withCarried s M q.2) w = true
1646
+ ∧ SAllowed s ws (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs
1647
+ | _, _, _, _ => True
1648
+
1649
+ /-- A k-step law over sets: from every reachable state, every k requests in a row the machine allows end in a state
1650
+ where `then` holds: primed names read the last state and maps, plain names the first state with the first request,
1651
+ each later request read by its fresh variables and the maps it carries by their fresh names. -/
1652
+ def SKStepHolds (s : SMachine) (sl : SKStep) : Prop :=
1653
+ ∀ ρ M, SReach s ρ M → ∀ q₁ qs, qs.length + 1 = sl.k.whens.length → SAllowed s sl.k.whens ρ M (q₁ :: qs) →
1654
+ evalC (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 (renEnv sl.k.ren (sExts s ρ M (q₁ :: qs)) (withExt s ρ (sReq s ρ M q₁.1 q₁.2))))
1655
+ (primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 (renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2))) sl.k.then_ = true
1656
+
1657
+ /-! The proof -/
1658
+
1659
+ theorem sMapsOk_parts {s : SMachine} {MZ : List Nat} (h : sMapsOk s MZ = true) :
1660
+ (∀ p ∈ s.f.m.step, avoidsMapsE MZ (inlineE s.f.flags p.2) = true)
1661
+ ∧ (∀ w ∈ s.f.m.writes, avoidsMapsE MZ (inlineE s.f.flags w.2.1) = true ∧ avoidsMapsE MZ (inlineE s.f.flags w.2.2) = true)
1662
+ ∧ (∀ w ∈ s.every, avoidsMapsE MZ (inlineE s.f.flags w.2.2) = true) := by
1663
+ simp only [sMapsOk, Bool.and_eq_true, List.all_eq_true] at h
1664
+ exact ⟨h.1.1.1.1, h.1.1.1.2, h.1.1.2⟩
1665
+
1666
+ theorem sMapsOk_facts {s : SMachine} {MZ : List Nat} (h : sMapsOk s MZ = true) : ∀ l ∈ s.facts, avoidsMaps MZ l.holds = true := by
1667
+ simp only [sMapsOk, Bool.and_eq_true, List.all_eq_true] at h; exact h.1.2
1668
+ theorem sMapsOk_pfacts {s : SMachine} {MZ : List Nat} (h : sMapsOk s MZ = true) : ∀ f ∈ s.pfacts, avoidsMaps MZ f.1.holds = true := by
1669
+ simp only [sMapsOk, Bool.and_eq_true, List.all_eq_true] at h; exact h.2
1670
+
1671
+ theorem renMaps_other : ∀ (rs : List (List (Nat × Nat))) (κs : List (Nat → Nat → Nat)) (b : Nat → Nat → Nat) (mp : Nat),
1672
+ mp ∉ tgts rs → renMaps rs κs b mp = b mp
1673
+ | [], κs, b, mp, _ => by cases κs <;> rfl
1674
+ | r :: rs, [], b, mp, _ => rfl
1675
+ | r :: rs, κ :: κs, b, mp, hv => by
1676
+ simp only [tgts, List.mem_append, not_or] at hv
1677
+ have hn : lookup (fun p : Nat × Nat => p.2 == mp) r = none := lookup_none (fun p hp => by
1678
+ simp only [beq_eq_false_iff_ne]; intro he; exact hv.1 (by rw [← he]; exact List.mem_map_of_mem hp))
1679
+ simp only [renMaps, hn]
1680
+ exact renMaps_other rs κs b mp hv.2
1681
+
1682
+ -- Each step's fresh map names hold the maps its request carries.
1683
+ def MRenOK (M0 : Nat → Nat → Nat) : List (List (Nat × Nat)) → List (Nat → Nat → Nat) → Prop
1684
+ | r :: rs, κ :: κs => (∀ p ∈ r, M0 p.2 = κ p.1) ∧ MRenOK M0 rs κs
1685
+ | _, _ => True
1686
+
1687
+ theorem mrenOK_congr {M0 M0' : Nat → Nat → Nat} : ∀ (rs : List (List (Nat × Nat))) (κs : List (Nat → Nat → Nat)),
1688
+ (∀ v ∈ tgts rs, M0 v = M0' v) → MRenOK M0' rs κs → MRenOK M0 rs κs
1689
+ | [], _, _, _ => trivial
1690
+ | _ :: _, [], _, _ => trivial
1691
+ | r :: rs, κ :: κs, hag, ⟨hh, ht⟩ => by
1692
+ refine ⟨fun p hp => ?_, mrenOK_congr rs κs (fun v hv => hag v (by simp [tgts, hv])) ht⟩
1693
+ rw [hag p.2 (by simp only [tgts, List.mem_append]; exact Or.inl (List.mem_map_of_mem hp))]; exact hh p hp
1694
+
1695
+ theorem renMaps_ok : ∀ (rs : List (List (Nat × Nat))) (κs : List (Nat → Nat → Nat)) (b : Nat → Nat → Nat),
1696
+ distinct (tgts rs) = true → MRenOK (renMaps rs κs b) rs κs
1697
+ | [], _, _, _ => trivial
1698
+ | _ :: _, [], _, _ => trivial
1699
+ | r :: rs, κ :: κs, b, hd => by
1700
+ simp only [tgts] at hd
1701
+ obtain ⟨hr, hrs, hdis⟩ := distinct_append hd
1702
+ refine ⟨fun p hp => ?_, mrenOK_congr rs κs (fun v hv => ?_) (renMaps_ok rs κs b hrs)⟩
1703
+ · simp only [renMaps, lookup_ren hr p hp]
1704
+ · have hn : lookup (fun p : Nat × Nat => p.2 == v) r = none := lookup_none (fun p hp => by
1705
+ simp only [beq_eq_false_iff_ne]; intro he; exact hdis v (by rw [← he]; exact List.mem_map_of_mem hp) hv)
1706
+ simp only [renMaps, hn]
1707
+
1708
+ def SHeadL (s : SMachine) (Z MZ : List Nat) (W : Nat → Nat) (M0 : Nat → Nat → Nat) (σ : List (Nat × E)) (rd : Nat → E → E)
1709
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) : List (List (Nat × Nat)) → List (List (Nat × Nat)) → List SRequest → Prop
1710
+ | r :: _, mr :: _, q :: _ => SHead s Z MZ W M0 σ rd ρ M r mr q.1 q.2
1711
+ | _, _, _ => True
1712
+
1713
+ theorem skrun_sound (s : SMachine) (ps : List SEntry) (hl : checkSMachine s ps = true) (Z MZ : List Nat) (laws : List MLaw) (insts : List (Nat × E)) (qinsts : List (Nat × List E))
1714
+ (hlY : ∀ ρ M ι κ, SReach s ρ M → ∀ l ∈ laws, l.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ))
1715
+ (W : Nat → Nat) (M0 : Nat → Nat → Nat)
1716
+ (hfz : ∀ v ∈ flagVars s.f.flags, v ∈ Z) (hterms : sTermsOk s Z = true) (hmaps : sMapsOk s MZ = true)
1717
+ (hqb : ∀ q ∈ s.qflags, avoids Z q.2.2 = true) :
1718
+ ∀ (ws : List C) (rs mrs ns : List (List (Nat × Nat))) (qs : List SRequest) (σ : List (Nat × E)) (rd : Nat → E → E)
1719
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat),
1720
+ ws.length = rs.length → ws.length = mrs.length → ws.length = qs.length →
1721
+ (∀ w ∈ ws, (∀ c ∈ sFrameHyps s laws insts [] w, avoids Z c = true) ∧ ∀ c ∈ sFrameHyps s laws insts qinsts w, avoidsMaps MZ c = true) →
1722
+ (∀ r ∈ rs, ∀ p ∈ r, p.1 ∈ s.extVars) →
1723
+ (∀ r ∈ rs.tail, ∀ i ∈ s.extVars, ∃ p ∈ r, p.1 = i) →
1724
+ (∀ r ∈ mrs, ∀ p ∈ r, p.1 ∈ s.carried) →
1725
+ (∀ r ∈ mrs.tail, ∀ mp ∈ s.carried, ∃ p ∈ r, p.1 = mp) →
1726
+ RenOK W rs.tail (sExts s ρ M qs).tail → MRenOK M0 mrs.tail (qs.map (·.2)).tail →
1727
+ (∀ n ∈ ns, ∀ p ∈ n, p.1 ∉ s.extVars ∧ p.1 ∉ Z) → RenOK W ns (sStates s ρ M qs) →
1728
+ SReach s ρ M → SDenotes s Z MZ W M0 σ rd ρ M → SHeadL s Z MZ W M0 σ rd ρ M rs mrs qs → SAllowed s ws ρ M qs →
1729
+ (∀ c ∈ (sRun s laws insts qinsts ws rs mrs ns σ rd).1, evalC W M0 c = true)
1730
+ ∧ SDenotes s Z MZ W M0 (sRun s laws insts qinsts ws rs mrs ns σ rd).2.1 (sRun s laws insts qinsts ws rs mrs ns σ rd).2.2
1731
+ (sEnd s ρ M qs).1 (sEnd s ρ M qs).2
1732
+ | [], rs, mrs, ns, qs, σ, rd, ρ, M, hl1, hl2, hl3, _, _, _, _, _, _, _, _, _, _, hD, _, _ => by
1733
+ cases rs with
1734
+ | cons _ _ => simp at hl1
1735
+ | nil =>
1736
+ cases mrs with
1737
+ | cons _ _ => simp at hl2
1738
+ | nil =>
1739
+ cases qs with
1740
+ | cons _ _ => simp at hl3
1741
+ | nil => exact ⟨fun c hc => by simp [sRun] at hc, hD⟩
1742
+ | w :: ws, rs, mrs, ns, qs, σ, rd, ρ, M, hl1, hl2, hl3, hav, hkeys, hcov, hmkeys, hmcov, hren, hmren, hnsrc, hnok, hr, hD, hH, hK => by
1743
+ rcases rs with _ | ⟨r, rs⟩
1744
+ · simp at hl1
1745
+ rcases mrs with _ | ⟨mr, mrs⟩
1746
+ · simp at hl2
1747
+ rcases qs with _ | ⟨q, qs⟩
1748
+ · simp at hl3
1749
+ simp only [List.length_cons, Nat.add_right_cancel_iff] at hl1 hl2 hl3
1750
+ simp only [List.tail_cons] at hcov hmcov
1751
+ have hren1 : RenOK W rs (sExts s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs) := hren
1752
+ have hmren1 : MRenOK M0 mrs (qs.map (·.2)) := hmren
1753
+ obtain ⟨hA, hW, hK'⟩ := hK
1754
+ have hH1 : SHead s Z MZ W M0 σ rd ρ M r mr q.1 q.2 := hH
1755
+ obtain ⟨hfav, hstp, hwr, hev⟩ := sTermsOk_parts hterms
1756
+ obtain ⟨hstm, hwrm, hevm⟩ := sMapsOk_parts hmaps
1757
+ have hD' := sstep_denotes s Z MZ W M0 (sOk_of hl) hfz hfav r mr q.1 q.2 hD hH1 (hkeys r List.mem_cons_self) hstp
1758
+ (fun w hw => (hwr w hw).1) hev hstm hwrm hevm
1759
+ have hr' : SReach s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 := SReach.step ρ M q.1 q.2 hr hA
1760
+ -- The state after the step, named: each fresh variable holds the next state's value of the variable it names.
1761
+ have hnames : ∀ p ∈ ns.headD [], W p.2 = (sNext s ρ M q.1 q.2).1 p.1 := by
1762
+ cases ns with
1763
+ | nil => intro p hp; simp at hp
1764
+ | cons n ns' => exact hnok.1
1765
+ have hnsrc1 : ∀ p ∈ ns.headD [], p.1 ∉ s.extVars ∧ p.1 ∉ Z := by
1766
+ cases ns with
1767
+ | nil => intro p hp; simp at hp
1768
+ | cons n ns' => exact hnsrc n List.mem_cons_self
1769
+ have hD'' : SDenotes s Z MZ W M0 (frameSub (ns.headD []) (stepSub s.f (frameSub r σ) (frameRd mr rd))) (stepRdS s (frameSub r σ) (frameRd mr rd))
1770
+ (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 := by
1771
+ refine ⟨fun v hve hvz => ?_, hD'.2⟩
1772
+ by_cases hin : ∃ p ∈ ns.headD [], p.1 = v
1773
+ · exact applyS_frame_in hin hnames
1774
+ · rw [applyS_frame_out (fun p hp he => hin ⟨p, hp, he⟩)]; exact hD'.1 v hve hvz
1775
+ -- The next step's frame, its request set and its maps read, agrees with the next state and request.
1776
+ have hH' : SHeadL s Z MZ W M0 (frameSub (ns.headD []) (stepSub s.f (frameSub r σ) (frameRd mr rd))) (stepRdS s (frameSub r σ) (frameRd mr rd))
1777
+ (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 rs mrs qs := by
1778
+ rcases rs with _ | ⟨r', rs'⟩
1779
+ · exact trivial
1780
+ rcases mrs with _ | ⟨mr', mrs'⟩
1781
+ · exact trivial
1782
+ rcases qs with _ | ⟨q', qs'⟩
1783
+ · exact trivial
1784
+ have hr1 : ∀ p ∈ r', W p.2 = sReq s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 q'.1 q'.2 p.1 := hren1.1
1785
+ have hm1 : ∀ p ∈ mr', M0 p.2 = q'.2 p.1 := hmren1.1
1786
+ refine ⟨fun v hvz => ?_, fun mp k hmz => ?_⟩
1787
+ · by_cases hve : v ∈ s.extVars
1788
+ · rw [applyS_frame_in (hcov r' List.mem_cons_self v hve) hr1]
1789
+ simp only [withExt]; rw [if_pos (List.contains_iff_mem.mpr hve)]
1790
+ · rw [applyS_frame_out (fun p hp he => hve (by rw [← he]; exact hkeys r' (List.mem_cons_of_mem _ List.mem_cons_self) p hp)),
1791
+ hD''.1 v hve hvz]
1792
+ simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hve)]
1793
+ · by_cases hmc : mp ∈ s.carried
1794
+ · cases hlk : lookup (fun p : Nat × Nat => p.1 == mp) mr' with
1795
+ | none =>
1796
+ obtain ⟨p, hp, hp1⟩ := hmcov mr' List.mem_cons_self mp hmc
1797
+ have := lookup_none_all hlk p hp
1798
+ simp [hp1] at this
1799
+ | some p0 =>
1800
+ have hp0 := lookup_some hlk
1801
+ simp only [beq_iff_eq] at hp0
1802
+ simp only [frameRd, hlk, evalE]
1803
+ rw [hm1 p0 hp0.1, hp0.2]; simp only [withCarried]; rw [if_pos (List.contains_iff_mem.mpr hmc)]
1804
+ · have hlk : lookup (fun p : Nat × Nat => p.1 == mp) mr' = none := lookup_none (fun p hp => by
1805
+ simp only [beq_eq_false_iff_ne]; intro he
1806
+ exact hmc (by rw [← he]; exact hmkeys mr' (List.mem_cons_of_mem _ List.mem_cons_self) p hp))
1807
+ simp only [frameRd, hlk]
1808
+ rw [hD'.2 mp k hmc hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]
1809
+ have hren' : RenOK W rs.tail (sExts s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs).tail := by
1810
+ rcases rs with _ | ⟨r', rs'⟩
1811
+ · exact trivial
1812
+ rcases qs with _ | ⟨q', qs'⟩
1813
+ · cases rs' <;> exact trivial
1814
+ exact hren1.2
1815
+ have hmren' : MRenOK M0 mrs.tail (qs.map (·.2)).tail := by
1816
+ rcases mrs with _ | ⟨mr', mrs'⟩
1817
+ · exact trivial
1818
+ rcases qs with _ | ⟨q', qs'⟩
1819
+ · cases mrs' <;> exact trivial
1820
+ exact hmren1.2
1821
+ have hnsrc' : ∀ n ∈ ns.tail, ∀ p ∈ n, p.1 ∉ s.extVars ∧ p.1 ∉ Z := fun n hn => hnsrc n (List.mem_of_mem_tail hn)
1822
+ have hnok' : RenOK W ns.tail (sStates s (sNext s ρ M q.1 q.2).1 (sNext s ρ M q.1 q.2).2 qs) := by
1823
+ cases ns with
1824
+ | nil => exact trivial
1825
+ | cons n ns' => exact hnok.2
1826
+ obtain ⟨ih1, ih2⟩ := skrun_sound s ps hl Z MZ laws insts qinsts hlY W M0 hfz hterms hmaps hqb ws rs mrs ns.tail qs _ _ _ _ hl1 hl2 hl3
1827
+ (fun w' hw' => hav w' (List.mem_cons_of_mem _ hw'))
1828
+ (fun r' hr'' => hkeys r' (List.mem_cons_of_mem _ hr''))
1829
+ (fun r' hr'' => hcov r' (List.mem_of_mem_tail hr''))
1830
+ (fun r' hr'' => hmkeys r' (List.mem_cons_of_mem _ hr''))
1831
+ (fun r' hr'' => hmcov r' (List.mem_of_mem_tail hr''))
1832
+ hren' hmren' hnsrc' hnok' hr' hD'' hH' hK'
1833
+ refine ⟨fun c hc => ?_, ih2⟩
1834
+ simp only [sRun, List.mem_append, List.mem_map] at hc
1835
+ rcases hc with (⟨c', hc', rfl⟩ | hc) | hc
1836
+ · exact sframe_hyps s Z MZ W M0 (sOk_of hl) laws insts qinsts (hlY ρ M q.1 q.2 hr)
1837
+ hH1 (hav w List.mem_cons_self).1 hqb (hav w List.mem_cons_self).2 hA hW c' hc'
1838
+ · -- Each name's equation: the fresh variable holds the next state's value, which the term denotes.
1839
+ simp only [nameHyps, List.mem_map] at hc
1840
+ obtain ⟨p, hp, rfl⟩ := hc
1841
+ simp only [evalC]
1842
+ apply decide_eq_true
1843
+ show W p.2 = evalE W M0 ((assoc (stepSub s.f (frameSub r σ) (frameRd mr rd)) p.1).getD (.var p.1))
1844
+ rw [hnames p hp, evalE_assoc_getD]
1845
+ exact (hD'.1 p.1 (hnsrc1 p hp).1 (hnsrc1 p hp).2).symm
1846
+ · exact ih1 c hc
1847
+
1848
+ /-- The k-step rule over sets: an accepted k-step goal makes its law hold. -/
1849
+ theorem skstep_sound (s : SMachine) (ps : List SEntry) (sl : SKStep)
1850
+ (hl : checkSMachine s ps = true) (hc : checkSKStep s ps sl = true) : SKStepHolds s sl := by
1851
+ intro ρ M hr q₁ qs hlen hK
1852
+ simp only [checkSKStep, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hc
1853
+ obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨⟨hlw, hlm⟩, hhead⟩, hmhead⟩, hdist⟩, hmdist⟩, hkeys⟩, hcov⟩, hmkeys⟩, hmcov⟩, hpr⟩, hpm⟩, hpre⟩, hws⟩, hqb⟩, hterms⟩, hmaps⟩, hgoal⟩, hnd⟩, hnsrc⟩, hnthen⟩ := hc
1854
+ obtain ⟨hpreS, hrp⟩ := spre_parts hl
1855
+ have hflags : ∀ v ∈ flagVars s.f.flags, v ∈ szone s sl := fun v hv => List.mem_append_left _ (List.mem_append_right _ hv)
1856
+ obtain ⟨rs, hrs⟩ : ∃ rs, sl.k.ren = [] :: rs := by
1857
+ cases h : sl.k.ren with
1858
+ | nil => rw [h] at hhead; simp at hhead
1859
+ | cons r rs => rw [h] at hhead; simp only [List.head?_cons, Option.some.injEq] at hhead; exact ⟨rs, by rw [hhead]⟩
1860
+ obtain ⟨mrs, hmrs⟩ : ∃ mrs, sl.mren = [] :: mrs := by
1861
+ cases h : sl.mren with
1862
+ | nil => rw [h] at hmhead; simp at hmhead
1863
+ | cons r rs => rw [h] at hmhead; simp only [List.head?_cons, Option.some.injEq] at hmhead; exact ⟨rs, by rw [hmhead]⟩
1864
+ -- The goal's environment: each later request at its fresh variables, and each step's state at its names.
1865
+ let W0 := renEnv sl.k.ren (sExts s ρ M (q₁ :: qs)) (withExt s ρ (sReq s ρ M q₁.1 q₁.2))
1866
+ let W := renEnv sl.names (sStates s ρ M (q₁ :: qs)) W0
1867
+ obtain ⟨_, hndn, hndis⟩ := distinct_append hnd
1868
+ have hWn : ∀ v, v ∉ tgts sl.names → W v = W0 v := fun v hv => renEnv_other _ _ _ v hv
1869
+ let M0 := renMaps sl.mren ((q₁ :: qs).map (·.2)) (withCarried s M q₁.2)
1870
+ have hWo : ∀ v, v ∉ szone s sl → W v = withExt s ρ (sReq s ρ M q₁.1 q₁.2) v := fun v hv => by
1871
+ rw [hWn v (fun h => hv (List.mem_append_right _ h))]
1872
+ exact renEnv_other _ _ _ v (fun h => hv (List.mem_append_left _ (List.mem_append_left _ h)))
1873
+ have hMo : ∀ mp, mp ∉ tgts sl.mren → M0 mp = withCarried s M q₁.2 mp := fun mp hmp => renMaps_other _ _ _ mp hmp
1874
+ have hD0 : SDenotes s (szone s sl) (tgts sl.mren) W M0 [] getRd ρ M := by
1875
+ refine ⟨fun v hve hvz => ?_, fun mp k hmc hmz => ?_⟩
1876
+ · rw [applyS_nil', hWo v hvz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hve)]
1877
+ · simp only [getRd, evalE]; rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)]
1878
+ have hH0 : SHeadL s (szone s sl) (tgts sl.mren) W M0 [] getRd ρ M sl.k.ren sl.mren (q₁ :: qs) := by
1879
+ rw [hrs, hmrs]
1880
+ show SHead s (szone s sl) (tgts ([] :: mrs)) W M0 [] getRd ρ M [] [] q₁.1 q₁.2
1881
+ refine ⟨fun v hvz => ?_, fun mp k hmz => ?_⟩
1882
+ · simp only [frameSub, List.map_nil, List.append_nil, applyS_nil']; exact hWo v hvz
1883
+ · simp only [frameRd, lookup, getRd, evalE]; rw [hMo mp (by rw [hmrs]; exact hmz)]
1884
+ have hren0 : RenOK W sl.k.ren.tail (sExts s ρ M (q₁ :: qs)).tail := by
1885
+ have h : RenOK W sl.k.ren (sExts s ρ M (q₁ :: qs)) :=
1886
+ renOK_congr _ _ (fun v hv => hWn v (hndis v hv)) (renEnv_ok sl.k.ren _ _ hdist)
1887
+ rw [hrs] at h ⊢; exact h.2
1888
+ have hmren0 : MRenOK M0 sl.mren.tail ((q₁ :: qs).map (·.2)).tail := by
1889
+ have h : MRenOK M0 sl.mren ((q₁ :: qs).map (·.2)) := renMaps_ok sl.mren _ _ hmdist
1890
+ rw [hmrs] at h ⊢; exact h.2
1891
+ -- Every entry holds at each reachable state, so each law (and each pair's) holds in the goal's view of it.
1892
+ have hlY : ∀ ρ M ι κ, SReach s ρ M → ∀ l ∈ sLaws ps sl.pinsts, l.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) := by
1893
+ intro ρ M ι κ hr l hl'
1894
+ have hall : ∀ e ∈ ps, e.Holds (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) := fun e he =>
1895
+ sentry_transfer (slaws_avoid hl e.law (List.mem_map_of_mem he)).1 (slaws_avoid hl e.law (List.mem_map_of_mem he)).2
1896
+ (fun i hi => by simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi)])
1897
+ (fun mp hmp => by simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)])
1898
+ (smachine_sound s ps hl ρ M hr e he)
1899
+ rcases List.mem_append.mp hl' with hl' | hl'
1900
+ · obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl'; exact SEntry.holds_law (hall e' he')
1901
+ · exact pairLaws_hold ps sl.pinsts (skeys_apart hl) hall l hl'
1902
+ obtain ⟨hh, hfin⟩ := skrun_sound s ps hl (szone s sl) (tgts sl.mren) (sLaws ps sl.pinsts) (sInsts ps sl.k.insts sl.pinsts) sl.qinsts hlY W M0 hflags hterms hmaps hqb
1903
+ sl.k.whens sl.k.ren sl.mren sl.names (q₁ :: qs) [] getRd ρ M hlw (by omega) (by simp only [List.length_cons]; omega)
1904
+ hws (fun r hr p hp => List.contains_iff_mem.mp (hkeys r hr p hp))
1905
+ (fun r hr i hi => by have := hcov r hr i hi; simpa only [List.any_eq_true, beq_iff_eq] using this)
1906
+ (fun r hr p hp => List.contains_iff_mem.mp (hmkeys r hr p hp))
1907
+ (fun r hr i hi => by have := hmcov r hr i hi; simpa only [List.any_eq_true, beq_iff_eq] using this)
1908
+ hren0 hmren0
1909
+ (fun n hn p hp => by
1910
+ have h := hnsrc n hn p hp
1911
+ simp only [Bool.and_eq_true, Bool.not_eq_true', List.contains_iff_mem] at h
1912
+ exact ⟨by simpa using h.1, by simpa using h.2⟩)
1913
+ (renEnv_ok sl.names _ _ hndn) hr hD0 hH0 hK
1914
+ have hfin' : SDenotes s (szone s sl) (tgts sl.mren) W M0 (sKOut s ps sl).2.1 (sKOut s ps sl).2.2
1915
+ (sEnd s ρ M (q₁ :: qs)).1 (sEnd s ρ M (q₁ :: qs)).2 := hfin
1916
+ apply Decidable.byContradiction; intro hn
1917
+ -- `then` reads none of the names, so the goal's environment reads it as the claim's does.
1918
+ have hn' : ¬ evalC (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W) (primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 M0) sl.k.then_ = true := by
1919
+ rw [congrC (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W) (primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W0) _ sl.k.then_ (fun i hi => by
1920
+ simp only [primed]; split
1921
+ · rfl
1922
+ · exact hWn i (avoids_occ hnthen hi))]
1923
+ exact hn
1924
+ apply sound_in _ _ hgoal W M0
1925
+ intro c hc
1926
+ simp only [sKGoal, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
1927
+ rcases hc with ((rfl | hc) | hc) | rfl
1928
+ · -- The precondition: at the first state (it reads no map, no request and no fresh variable).
1929
+ rw [noReadsC W M0 M s.f.m.pre hrp, congrC W ρ M s.f.m.pre (fun j hj => ?_)]
1930
+ · exact pre_sreach s hpreS hrp hr
1931
+ · rw [hWo j (avoids_occ hpre hj)]; simp only [withExt]
1932
+ rw [if_neg (fun h' => avoids_occ hpreS hj (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ (List.contains_iff_mem.mp h')))))]
1933
+ · -- The facts, at the first frame: it agrees with the first state away from the zone and the request.
1934
+ exact facthyps_true s sl.finsts sl.pfinsts W M0 (fun f hf => (pfactsOk_parts (sfacts_ok hl) f hf).2.2)
1935
+ (facts_frame s (sfacts_ok hl) (szone s sl) (tgts sl.mren) (sTermsOk_facts hterms) (sMapsOk_facts hmaps)
1936
+ (fun i hie hiz => by rw [hWo i hiz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)])
1937
+ (fun mp hmc hmz => by rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)])
1938
+ (facts_sreach s (sfacts_ok hl) hr))
1939
+ (pfacts_frame s (sfacts_ok hl) (szone s sl) (tgts sl.mren) (sTermsOk_pfacts hterms) (sMapsOk_pfacts hmaps)
1940
+ (fun i hie hiz => by rw [hWo i hiz]; simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hie)])
1941
+ (fun mp hmc hmz => by rw [hMo mp hmz]; simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmc)])
1942
+ (pfacts_sreach s (sfacts_ok hl) hr)) c hc
1943
+ · exact hh c hc
1944
+ · -- `then`, read across the run, is what SKStepHolds reads.
1945
+ have hrdE : ∀ mp k, evalE W M0 (kEndRd sl.k (sKOut s ps sl).2.2 mp k)
1946
+ = primedMaps sl.k.primeMaps (sEnd s ρ M (q₁ :: qs)).2 M0 mp (evalE W M0 k) := by
1947
+ intro mp k
1948
+ simp only [kEndRd, primedMaps]
1949
+ cases hq : lookup (fun q => q.2 == mp) sl.k.primeMaps with
1950
+ | none => simp [evalE]
1951
+ | some q =>
1952
+ have hq1 := hpm q (lookup_some hq).1
1953
+ simp only [Bool.not_eq_true'] at hq1
1954
+ exact hfin'.2 q.1 k (by simpa using hq1.1) (by simpa using hq1.2)
1955
+ have henv : applyS (kEndSub sl.k (sKOut s ps sl).2.1 (sKOut s ps sl).2.2) W M0
1956
+ = primed sl.k.prime (sEnd s ρ M (q₁ :: qs)).1 W := by
1957
+ funext i
1958
+ rw [kEndSub, applyS_primes]
1959
+ simp only [primed]
1960
+ cases hq : lookup (fun q => q.2 == i) sl.k.prime with
1961
+ | none => rfl
1962
+ | some q =>
1963
+ have hq1 := hpr q (lookup_some hq).1
1964
+ simp only [Bool.not_eq_true'] at hq1
1965
+ have hqi : q.1 ∉ s.extVars := by simpa using hq1.1
1966
+ have hqz : q.1 ∉ szone s sl := by simpa using hq1.2
1967
+ show evalE W M0 (substRE (sKOut s ps sl).2.1 (sKOut s ps sl).2.2 (.var q.1)) = (sEnd s ρ M (q₁ :: qs)).1 q.1
1968
+ rw [substR_var]; exact hfin'.1 q.1 hqi hqz
1969
+ simp only [evalC, substR_evalC _ _ W M0 _ hrdE, henv]
1970
+ simpa using hn'
1971
+
1972
+ end Kern
1973
+
1974
+ #print axioms Kern.smachine_sound
1975
+ #print axioms Kern.skstep_sound