gdp-wired 0.51.0 → 0.53.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.
- package/README.md +2 -2
- package/dist/core/accel-refute.d.ts +53 -0
- package/dist/core/accel-refute.js +997 -0
- package/dist/core/aggregate-kinds.d.ts +10 -0
- package/dist/core/aggregate-kinds.js +44 -0
- package/dist/core/aggregate-search.d.ts +9 -0
- package/dist/core/aggregate-search.js +74 -0
- package/dist/core/aggregate-weighted/candidates.d.ts +18 -0
- package/dist/core/aggregate-weighted/candidates.js +128 -0
- package/dist/core/aggregate-weighted/search.d.ts +17 -0
- package/dist/core/aggregate-weighted/search.js +68 -0
- package/dist/core/cli.d.ts +2 -0
- package/dist/core/cli.js +8 -1
- package/dist/core/cycles.d.ts +2 -0
- package/dist/core/cycles.js +12 -9
- package/dist/core/dsl.d.ts +7 -1
- package/dist/core/dsl.js +158 -26
- package/dist/core/filtered-sum-candidates.d.ts +17 -0
- package/dist/core/filtered-sum-candidates.js +77 -0
- package/dist/core/live-support.d.ts +5 -0
- package/dist/core/live-support.js +148 -0
- package/dist/core/live.js +194 -37
- package/dist/core/lockstep.d.ts +5 -0
- package/dist/core/lockstep.js +23 -0
- package/dist/core/ordered-pair-seeds.d.ts +2 -0
- package/dist/core/ordered-pair-seeds.js +109 -0
- package/dist/core/proof-output.d.ts +49 -2
- package/dist/core/proof-output.js +44 -0
- package/dist/core/search-termination.d.ts +4 -0
- package/dist/core/search-termination.js +1 -1
- package/dist/core/source-check.d.ts +4 -1
- package/dist/core/source-check.js +5 -2
- package/dist/core/sparse-kind-proof.d.ts +7 -0
- package/dist/core/sparse-kind-proof.js +49 -0
- package/dist/core/step-seeds.d.ts +46 -0
- package/dist/core/step-seeds.js +316 -0
- package/dist/kernel/finite-sum.d.ts +31 -0
- package/dist/kernel/finite-sum.js +74 -0
- package/dist/kernel/ghost-write.d.ts +117 -0
- package/dist/kernel/ghost-write.js +89 -0
- package/dist/lean-kernel/Cert.lean +222 -0
- package/dist/lean-kernel/Count.lean +1568 -0
- package/dist/lean-kernel/Ghost.lean +373 -0
- package/dist/lean-kernel/GhostMap.lean +426 -0
- package/dist/lean-kernel/Kernel.lean +2881 -0
- package/dist/lean-kernel/Live.lean +1168 -0
- package/dist/lean-kernel/Progress.lean +543 -0
- package/dist/lean-kernel/Sets.lean +1975 -0
- package/dist/lean-kernel/StepLaw.lean +128 -0
- package/dist/lean-kernel/Steps.lean +477 -0
- package/dist/lean-kernel/Sums.lean +916 -0
- package/dist/lean-kernel/emit.d.ts +15 -0
- package/dist/lean-kernel/emit.js +47 -6
- package/dist/lean-kernel/finite-sum-export.d.ts +3 -0
- package/dist/lean-kernel/finite-sum-export.js +163 -0
- package/dist/lean-kernel/ghost-write-export.d.ts +3 -0
- package/dist/lean-kernel/ghost-write-export.js +81 -0
- package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
- package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
- package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
- package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
- package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
- package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
- package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
- package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
- package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
- package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
- package/dist/lean-kernel/lean-aggregate/index.js +52 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
- package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
- package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/source.js +141 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
- package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
- package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
- package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
- package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
- package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
- package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
- package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
- package/dist/lean-kernel/library.js +3 -0
- package/dist/lean-kernel/lift-simulation/shape.js +5 -5
- package/docs/lean-aggregate.md +29 -0
- package/package.json +6 -1
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
import Ghost
|
|
2
|
+
/-!
|
|
3
|
+
Sums over a map's keys: kernel.ts's `sumMachine`, a machine whose state variables may each be the sum, over every key,
|
|
4
|
+
of a term read at that key (`M.sum()`: the cell itself; a count of the keys where a condition holds; a field's total
|
|
5
|
+
where another field says so). The sum is exact: a map starts at the same value everywhere, so the term must be 0 at the
|
|
6
|
+
start at every key, and each step changes it at finitely many keys (the cover), so it is 0 past some key in every
|
|
7
|
+
reachable state and its sum is the sum below there (`IsSum`).
|
|
8
|
+
|
|
9
|
+
A machine with sums (`SumMachine`) is a machine with sets (`SMachine`) plus its sums (`SumDecl`): the variable, the key
|
|
10
|
+
the term reads its maps at, the term, the cover keys (terms over the step's view: every key a step may change the term
|
|
11
|
+
at), a quantified flag and its witness. Lean builds the machine kernel.ts proves things about (`SumMachine.plain`):
|
|
12
|
+
- each sum a state variable from 0, after each step `(S + new) ∸ old`, where old and new are the term's values at the
|
|
13
|
+
cover keys, each key counted once (`dTermE`), before and after the step;
|
|
14
|
+
- the assumption conjoined with two facts at every step: old ≤ S (`sumFact`), and a quantified flag that holds at every
|
|
15
|
+
key k: the term at k and at the cover keys, each key once, is at most S (`sumBody`), so a goal reads `M[t] <= S`, and
|
|
16
|
+
`M[t] + M[c] <= S` where t is not c, at the keys its proof names.
|
|
17
|
+
It checks kernel.ts's side conditions (`sumOk`: the term reads its maps only at its key and no variable, is 0 at the
|
|
18
|
+
start, reads no carried map; each map it reads written at one key has that key among the cover keys) and, for each map
|
|
19
|
+
the term reads that a step writes at every key, the cover goal (`sumCoverGoal`): at a key none of the cover keys is, the
|
|
20
|
+
write keeps the cell. The cover goal assumes there the cells' laws (`cellLaws`): each law proven of the plain machine
|
|
21
|
+
about one key that reads only the summed maps' cells at it, such as a cell's kind (a yes/no at most 1).
|
|
22
|
+
|
|
23
|
+
`sum_cells`: in every state the plain machine reaches, each sum variable is the sum of its term over the keys (IsSum)
|
|
24
|
+
and every law proven of it holds, by one induction over the runs (a step keeps the sums exact where the laws hold
|
|
25
|
+
before it); `sum_holds` is its first half, and `sum_allowed`: there the plain machine allows exactly the requests the
|
|
26
|
+
rule's own assumption allows, the facts being true. So every claim proven of the plain machine (laws, step laws, k-induction, liveness) is a claim about the rule with
|
|
27
|
+
its sums exact; under weak fairness the helpful requests are proven allowed by the rule's own assumption
|
|
28
|
+
(`sumleads_wf_sound`, through Live.lean's `sleads_wf_soundA`).
|
|
29
|
+
-/
|
|
30
|
+
namespace Kern
|
|
31
|
+
open Classical
|
|
32
|
+
|
|
33
|
+
/-! Finite sums of naturals over the keys -/
|
|
34
|
+
|
|
35
|
+
def sumTo (f : Nat → Nat) : Nat → Nat
|
|
36
|
+
| 0 => 0
|
|
37
|
+
| n + 1 => sumTo f n + f n
|
|
38
|
+
|
|
39
|
+
-- f is 0 from some key N on, and n is its sum below N.
|
|
40
|
+
def IsSum (f : Nat → Nat) (n : Nat) : Prop := ∃ N, (∀ k, N ≤ k → f k = 0) ∧ n = sumTo f N
|
|
41
|
+
|
|
42
|
+
-- The sum, where there is one.
|
|
43
|
+
noncomputable def sumOf (f : Nat → Nat) : Nat := if h : ∃ n, IsSum f n then Classical.choose h else 0
|
|
44
|
+
|
|
45
|
+
theorem sumTo_tail {f : Nat → Nat} {N : Nat} (h : ∀ k, N ≤ k → f k = 0) : ∀ d, sumTo f (N + d) = sumTo f N
|
|
46
|
+
| 0 => rfl
|
|
47
|
+
| d + 1 => by
|
|
48
|
+
show sumTo f (N + d) + f (N + d) = sumTo f N
|
|
49
|
+
rw [sumTo_tail h d, h (N + d) (Nat.le_add_right _ _)]; rfl
|
|
50
|
+
|
|
51
|
+
theorem sumTo_past {f : Nat → Nat} {N N' : Nat} (h : ∀ k, N ≤ k → f k = 0) (hle : N ≤ N') : sumTo f N' = sumTo f N := by
|
|
52
|
+
obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hle; exact sumTo_tail h d
|
|
53
|
+
|
|
54
|
+
theorem isSum_unique {f : Nat → Nat} {a b : Nat} (ha : IsSum f a) (hb : IsSum f b) : a = b := by
|
|
55
|
+
obtain ⟨N, hN, rfl⟩ := ha
|
|
56
|
+
obtain ⟨N', hN', rfl⟩ := hb
|
|
57
|
+
rw [← sumTo_past hN (Nat.le_max_left N N'), ← sumTo_past hN' (Nat.le_max_right N N')]
|
|
58
|
+
|
|
59
|
+
theorem sumOf_eq {f : Nat → Nat} {n : Nat} (h : IsSum f n) : sumOf f = n := by
|
|
60
|
+
have hex : ∃ n, IsSum f n := ⟨n, h⟩
|
|
61
|
+
simp only [sumOf, dif_pos hex]
|
|
62
|
+
exact isSum_unique (Classical.choose_spec hex) h
|
|
63
|
+
|
|
64
|
+
theorem sumTo_const0 : ∀ N, sumTo (fun _ => 0) N = 0
|
|
65
|
+
| 0 => rfl
|
|
66
|
+
| N + 1 => by show sumTo (fun _ => 0) N + 0 = 0; rw [sumTo_const0 N]
|
|
67
|
+
|
|
68
|
+
theorem isSum_zero {f : Nat → Nat} (h : ∀ k, f k = 0) : IsSum f 0 :=
|
|
69
|
+
⟨0, fun k _ => h k, rfl⟩
|
|
70
|
+
|
|
71
|
+
-- f with key c set to 0.
|
|
72
|
+
def zap (f : Nat → Nat) (c : Nat) : Nat → Nat := fun k => if k = c then 0 else f k
|
|
73
|
+
|
|
74
|
+
theorem sumTo_zap (f : Nat → Nat) (c : Nat) : ∀ N, c < N → sumTo f N = f c + sumTo (zap f c) N
|
|
75
|
+
| 0, h => absurd h (Nat.not_lt_zero _)
|
|
76
|
+
| N + 1, h => by
|
|
77
|
+
show sumTo f N + f N = f c + (sumTo (zap f c) N + zap f c N)
|
|
78
|
+
by_cases hc : c = N
|
|
79
|
+
· subst hc
|
|
80
|
+
have hz : ∀ M, M ≤ c → sumTo (zap f c) M = sumTo f M := by
|
|
81
|
+
intro M
|
|
82
|
+
induction M with
|
|
83
|
+
| zero => intro _; rfl
|
|
84
|
+
| succ M ih =>
|
|
85
|
+
intro hM
|
|
86
|
+
show sumTo (zap f c) M + zap f c M = sumTo f M + f M
|
|
87
|
+
rw [ih (by omega)]; simp only [zap, if_neg (show M ≠ c by omega)]
|
|
88
|
+
rw [hz c (Nat.le_refl _)]; simp only [zap, ite_true]; omega
|
|
89
|
+
· have hne : N ≠ c := fun h' => hc h'.symm
|
|
90
|
+
rw [sumTo_zap f c N (by omega)]; simp only [zap, hne, ite_false]; omega
|
|
91
|
+
|
|
92
|
+
theorem zap_tail {f : Nat → Nat} {N : Nat} (h : ∀ k, N ≤ k → f k = 0) (c : Nat) : ∀ k, N ≤ k → zap f c k = 0 := by
|
|
93
|
+
intro k hk; simp only [zap]; split
|
|
94
|
+
· rfl
|
|
95
|
+
· exact h k hk
|
|
96
|
+
|
|
97
|
+
-- A sum less one cell, and back.
|
|
98
|
+
theorem isSum_zap {f : Nat → Nat} {s : Nat} (h : IsSum f s) (c : Nat) : f c ≤ s ∧ IsSum (zap f c) (s - f c) := by
|
|
99
|
+
obtain ⟨N, hN, rfl⟩ := h
|
|
100
|
+
have hN' : ∀ k, max N (c + 1) ≤ k → f k = 0 := fun k hk => hN k (by omega)
|
|
101
|
+
have e1 := sumTo_past hN (Nat.le_max_left N (c + 1))
|
|
102
|
+
have e2 := sumTo_zap f c (max N (c + 1)) (by omega)
|
|
103
|
+
refine ⟨by omega, max N (c + 1), zap_tail hN' c, by omega⟩
|
|
104
|
+
|
|
105
|
+
theorem isSum_unzap {f : Nat → Nat} {r : Nat} (c : Nat) (h : IsSum (zap f c) r) (hf : ∃ N, ∀ k, N ≤ k → f k = 0) : IsSum f (f c + r) := by
|
|
106
|
+
obtain ⟨N, hN, rfl⟩ := h
|
|
107
|
+
obtain ⟨N0, hN0⟩ := hf
|
|
108
|
+
have hM : ∀ k, max (max N N0) (c + 1) ≤ k → f k = 0 := fun k hk => hN0 k (by omega)
|
|
109
|
+
have e1 := sumTo_past hN (show N ≤ max (max N N0) (c + 1) by omega)
|
|
110
|
+
have e2 := sumTo_zap f c (max (max N N0) (c + 1)) (by omega)
|
|
111
|
+
exact ⟨max (max N N0) (c + 1), hM, by omega⟩
|
|
112
|
+
|
|
113
|
+
-- The sum of f at a list of keys, each key once (later copies read 0).
|
|
114
|
+
def dsum (f : Nat → Nat) : List Nat → Nat
|
|
115
|
+
| [] => 0
|
|
116
|
+
| c :: cs => f c + dsum (zap f c) cs
|
|
117
|
+
|
|
118
|
+
-- f with every listed key set to 0.
|
|
119
|
+
def zapAll (f : Nat → Nat) : List Nat → Nat → Nat
|
|
120
|
+
| [] => f
|
|
121
|
+
| c :: cs => zapAll (zap f c) cs
|
|
122
|
+
|
|
123
|
+
theorem zapAll_eq (f : Nat → Nat) : ∀ (cs : List Nat) (k : Nat), zapAll f cs k = if k ∈ cs then 0 else f k
|
|
124
|
+
| [], k => by simp [zapAll]
|
|
125
|
+
| c :: cs, k => by
|
|
126
|
+
simp only [zapAll, zapAll_eq (zap f c) cs k, zap, List.mem_cons]
|
|
127
|
+
by_cases h1 : k ∈ cs
|
|
128
|
+
· simp [h1]
|
|
129
|
+
· by_cases h2 : k = c <;> simp [h1, h2]
|
|
130
|
+
|
|
131
|
+
-- A sum is the listed keys' (each once) plus the rest's.
|
|
132
|
+
theorem isSum_split : ∀ (cs : List Nat) {f : Nat → Nat} {s : Nat}, IsSum f s → ∃ r, IsSum (zapAll f cs) r ∧ s = dsum f cs + r
|
|
133
|
+
| [], _, s, h => ⟨s, h, by simp [dsum]⟩
|
|
134
|
+
| c :: cs, f, s, h => by
|
|
135
|
+
obtain ⟨hc, hz⟩ := isSum_zap h c
|
|
136
|
+
obtain ⟨r, hr, hs⟩ := isSum_split cs hz
|
|
137
|
+
exact ⟨r, hr, by simp only [dsum]; omega⟩
|
|
138
|
+
|
|
139
|
+
theorem isSum_tail {f : Nat → Nat} {s : Nat} (h : IsSum f s) : ∃ N, ∀ k, N ≤ k → f k = 0 := by
|
|
140
|
+
obtain ⟨N, hN, _⟩ := h; exact ⟨N, hN⟩
|
|
141
|
+
|
|
142
|
+
theorem isSum_join : ∀ (cs : List Nat) {f : Nat → Nat} {r : Nat}, IsSum (zapAll f cs) r → (∃ N, ∀ k, N ≤ k → f k = 0) →
|
|
143
|
+
IsSum f (dsum f cs + r)
|
|
144
|
+
| [], _, r, h, _ => by simpa [dsum, zapAll] using h
|
|
145
|
+
| c :: cs, f, r, h, hf => by
|
|
146
|
+
have hzf : ∃ N, ∀ k, N ≤ k → zap f c k = 0 := by obtain ⟨N, hN⟩ := hf; exact ⟨N, zap_tail hN c⟩
|
|
147
|
+
have h1 := isSum_join cs (f := zap f c) h hzf
|
|
148
|
+
have h2 := isSum_unzap c h1 hf
|
|
149
|
+
simp only [dsum]; rwa [Nat.add_assoc]
|
|
150
|
+
|
|
151
|
+
/-- The listed keys, each once, sum to at most the whole. -/
|
|
152
|
+
theorem dsum_le {f : Nat → Nat} {s : Nat} (h : IsSum f s) (cs : List Nat) : dsum f cs ≤ s := by
|
|
153
|
+
obtain ⟨r, _, hs⟩ := isSum_split cs h; omega
|
|
154
|
+
|
|
155
|
+
/-- A function that agrees with f but at the listed keys sums to f's sum less the keys' old values plus their new. -/
|
|
156
|
+
theorem isSum_change {f f' : Nat → Nat} {s : Nat} (cs : List Nat) (h : IsSum f s) (hag : ∀ k, k ∉ cs → f' k = f k) :
|
|
157
|
+
IsSum f' (s + dsum f' cs - dsum f cs) := by
|
|
158
|
+
obtain ⟨r, hr, hs⟩ := isSum_split cs h
|
|
159
|
+
have hz : zapAll f' cs = zapAll f cs := by
|
|
160
|
+
funext k; rw [zapAll_eq, zapAll_eq]; split
|
|
161
|
+
· rfl
|
|
162
|
+
· rename_i hk; exact hag k hk
|
|
163
|
+
obtain ⟨N, hN⟩ := isSum_tail h
|
|
164
|
+
have hf' : ∃ N, ∀ k, N ≤ k → f' k = 0 := by
|
|
165
|
+
refine ⟨max N (cs.foldr max 0 + 1), fun k hk => ?_⟩
|
|
166
|
+
have hk' : k ∉ cs := by
|
|
167
|
+
intro hm
|
|
168
|
+
have : ∀ l : List Nat, ∀ x ∈ l, x ≤ l.foldr max 0 := by
|
|
169
|
+
intro l; induction l with
|
|
170
|
+
| nil => intro x hx; simp at hx
|
|
171
|
+
| cons a as ih => intro x hx; rcases List.mem_cons.mp hx with rfl | hx
|
|
172
|
+
· simp only [List.foldr_cons]; omega
|
|
173
|
+
· have := ih x hx; simp only [List.foldr_cons]; omega
|
|
174
|
+
have := this cs k hm; omega
|
|
175
|
+
rw [hag k hk']; exact hN k (by omega)
|
|
176
|
+
have := isSum_join cs (f := f') (by rw [hz]; exact hr) hf'
|
|
177
|
+
have e : s + dsum f' cs - dsum f cs = dsum f' cs + r := by omega
|
|
178
|
+
rw [e]; exact this
|
|
179
|
+
|
|
180
|
+
/-! The term read at a key -/
|
|
181
|
+
|
|
182
|
+
-- A term that reads its maps at the key variable k alone, and no variable but there.
|
|
183
|
+
mutual
|
|
184
|
+
def gOkE (k : Nat) : E → Bool
|
|
185
|
+
| .var _ => false
|
|
186
|
+
| .const _ => true
|
|
187
|
+
| .add a b => gOkE k a && gOkE k b
|
|
188
|
+
| .sub a b => gOkE k a && gOkE k b
|
|
189
|
+
| .mul _ a => gOkE k a
|
|
190
|
+
| .min a b => gOkE k a && gOkE k b
|
|
191
|
+
| .max a b => gOkE k a && gOkE k b
|
|
192
|
+
| .prod a b => gOkE k a && gOkE k b
|
|
193
|
+
| .ite c t f => gOkC k c && gOkE k t && gOkE k f
|
|
194
|
+
| .get _ key => match key with
|
|
195
|
+
| .var i => i == k
|
|
196
|
+
| _ => false
|
|
197
|
+
| .tup a b => gOkE k a && gOkE k b
|
|
198
|
+
| .proj _ a => gOkE k a
|
|
199
|
+
def gOkC (k : Nat) : C → Bool
|
|
200
|
+
| .le a b => gOkE k a && gOkE k b
|
|
201
|
+
| .lt a b => gOkE k a && gOkE k b
|
|
202
|
+
| .eq a b => gOkE k a && gOkE k b
|
|
203
|
+
| .and a b => gOkC k a && gOkC k b
|
|
204
|
+
| .or a b => gOkC k a && gOkC k b
|
|
205
|
+
| .not a => gOkC k a
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
-- The term's value at a key: each map read there.
|
|
209
|
+
def gAt (g : E) (M : Nat → Nat → Nat) (key : Nat) : Nat := evalE (fun _ => 0) (fun mp _ => M mp key) g
|
|
210
|
+
|
|
211
|
+
mutual
|
|
212
|
+
theorem g_sliceE (k : Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : (e : E) → gOkE k e = true →
|
|
213
|
+
evalE ρ M e = evalE (fun _ => 0) (fun mp _ => M mp (ρ k)) e
|
|
214
|
+
| .var _, h => by simp [gOkE] at h
|
|
215
|
+
| .const _, _ => rfl
|
|
216
|
+
| .add a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
217
|
+
| .sub a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
218
|
+
| .mul _ a, h => by simp only [gOkE] at h; simp only [evalE, g_sliceE k ρ M a h]
|
|
219
|
+
| .min a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
220
|
+
| .max a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
221
|
+
| .prod a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
222
|
+
| .ite c t f, h => by
|
|
223
|
+
simp only [gOkE, Bool.and_eq_true] at h
|
|
224
|
+
simp only [evalE, g_sliceC k ρ M c h.1.1, g_sliceE k ρ M t h.1.2, g_sliceE k ρ M f h.2]
|
|
225
|
+
| .get m key, h => by
|
|
226
|
+
cases key <;> simp_all [gOkE, evalE]
|
|
227
|
+
| .tup a b, h => by simp only [gOkE, Bool.and_eq_true] at h; simp only [evalE, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
228
|
+
| .proj _ a, h => by simp only [gOkE] at h; simp only [evalE, g_sliceE k ρ M a h]
|
|
229
|
+
theorem g_sliceC (k : Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : (c : C) → gOkC k c = true →
|
|
230
|
+
evalC ρ M c = evalC (fun _ => 0) (fun mp _ => M mp (ρ k)) c
|
|
231
|
+
| .le a b, h => by simp only [gOkC, Bool.and_eq_true] at h; simp only [evalC, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
232
|
+
| .lt a b, h => by simp only [gOkC, Bool.and_eq_true] at h; simp only [evalC, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
233
|
+
| .eq a b, h => by simp only [gOkC, Bool.and_eq_true] at h; simp only [evalC, g_sliceE k ρ M a h.1, g_sliceE k ρ M b h.2]
|
|
234
|
+
| .and a b, h => by simp only [gOkC, Bool.and_eq_true] at h; simp only [evalC, g_sliceC k ρ M a h.1, g_sliceC k ρ M b h.2]
|
|
235
|
+
| .or a b, h => by simp only [gOkC, Bool.and_eq_true] at h; simp only [evalC, g_sliceC k ρ M a h.1, g_sliceC k ρ M b h.2]
|
|
236
|
+
| .not a, h => by simp only [gOkC] at h; simp only [evalC, g_sliceC k ρ M a h]
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
theorem gAt_eval {k : Nat} {g : E} (hg : gOkE k g = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : evalE ρ M g = gAt g M (ρ k) :=
|
|
240
|
+
g_sliceE k ρ M g hg
|
|
241
|
+
|
|
242
|
+
theorem gAt_congr {g : E} {M M' : Nat → Nat → Nat} {key : Nat} (h : ∀ mp, occME mp g = true → M mp key = M' mp key) :
|
|
243
|
+
gAt g M key = gAt g M' key :=
|
|
244
|
+
congrME _ _ _ g (fun mp hmp => funext fun _ => h mp hmp)
|
|
245
|
+
|
|
246
|
+
/-! Each key once, as a term -/
|
|
247
|
+
|
|
248
|
+
-- The sum of G at each listed key, a key equal to an earlier one read as 0.
|
|
249
|
+
def dTermE (G : E → E) : List E → E
|
|
250
|
+
| [] => .const 0
|
|
251
|
+
| c :: cs => .add (G c) (dTermE (fun t => .ite (.eq t c) (.const 0) (G t)) cs)
|
|
252
|
+
|
|
253
|
+
theorem dTermE_eval (W : Nat → Nat) (N : Nat → Nat → Nat) : ∀ (cs : List E) (G : E → E) (f : Nat → Nat),
|
|
254
|
+
(∀ t, evalE W N (G t) = f (evalE W N t)) → evalE W N (dTermE G cs) = dsum f (cs.map (evalE W N))
|
|
255
|
+
| [], _, _, _ => rfl
|
|
256
|
+
| c :: cs, G, f, hG => by
|
|
257
|
+
simp only [dTermE, evalE, List.map_cons, dsum, hG c]
|
|
258
|
+
congr 1
|
|
259
|
+
apply dTermE_eval W N cs _ (zap f (evalE W N c))
|
|
260
|
+
intro t
|
|
261
|
+
simp only [evalE, evalC, zap, hG t]
|
|
262
|
+
by_cases h : evalE W N t = evalE W N c <;> simp [h]
|
|
263
|
+
|
|
264
|
+
/-! Machines with sums -/
|
|
265
|
+
|
|
266
|
+
structure SumDecl where
|
|
267
|
+
v : Nat -- the sum's state variable
|
|
268
|
+
k : Nat -- the key the term reads its maps at
|
|
269
|
+
g : E -- the term at each key
|
|
270
|
+
cs : List E -- the cover keys: every key a step may change the term at, over the step's view
|
|
271
|
+
q : Nat -- the quantified flag: at every key, the term there and at the cover keys is at most the sum
|
|
272
|
+
w : Nat -- its witness
|
|
273
|
+
x : Nat -- the key a cover goal reads
|
|
274
|
+
|
|
275
|
+
structure SumMachine where
|
|
276
|
+
s : SMachine -- the rule: its sums' variables are state it reads and no step sets
|
|
277
|
+
sums : List SumDecl
|
|
278
|
+
|
|
279
|
+
-- The term at the key t, before the step.
|
|
280
|
+
def gHere (d : SumDecl) (t : E) : E := substE [(d.k, t)] d.g
|
|
281
|
+
|
|
282
|
+
-- A map read after a step, over the step's view: written at every key, its value at the key; written at one key, the
|
|
283
|
+
-- value there and the old cell elsewhere.
|
|
284
|
+
def postRd (s : SMachine) : Nat → E → E := fun mp key =>
|
|
285
|
+
match lookup (fun w => w.1 == mp) s.every with
|
|
286
|
+
| some w => substE [(w.2.1, key)] w.2.2
|
|
287
|
+
| none => match lookup (fun w => w.1 == mp) s.f.m.writes with
|
|
288
|
+
| some w => .ite (.eq key w.2.1) w.2.2 (.get mp key)
|
|
289
|
+
| none => .get mp key
|
|
290
|
+
|
|
291
|
+
-- The term at the key t, after the step.
|
|
292
|
+
def gAfter (s : SMachine) (d : SumDecl) (t : E) : E := substRE [(d.k, t)] (postRd s) d.g
|
|
293
|
+
|
|
294
|
+
-- The sum after a step: the new values at the cover keys in, the old ones out.
|
|
295
|
+
def sumNext (s : SMachine) (d : SumDecl) : E := .sub (.add (.var d.v) (dTermE (gAfter s d) d.cs)) (dTermE (gHere d) d.cs)
|
|
296
|
+
-- The old values at the cover keys are at most the sum.
|
|
297
|
+
def sumFact (d : SumDecl) : C := .le (dTermE (gHere d) d.cs) (.var d.v)
|
|
298
|
+
-- At every key k: the term at k and at the cover keys, each key once, is at most the sum (a quantified flag's body, its
|
|
299
|
+
-- flags inlined: a quantified flag is decided before the plain flags).
|
|
300
|
+
def sumBody (s : SMachine) (d : SumDecl) : C := .le (dTermE (gHere d) (.var d.k :: d.cs.map (inlineE s.f.flags))) (.var d.v)
|
|
301
|
+
def sumsC : List SumDecl → C
|
|
302
|
+
| [] => trueC
|
|
303
|
+
| d :: ds => .and (.and (sumFact d) (.eq (.var d.q) (.const 1))) (sumsC ds)
|
|
304
|
+
|
|
305
|
+
/-- The machine kernel.ts proves things about: each sum a state variable from 0, moved by `sumNext`; the facts conjoined
|
|
306
|
+
to the assumption; each sum's quantified flag. -/
|
|
307
|
+
def SumMachine.plain (h : SumMachine) : SMachine :=
|
|
308
|
+
{ h.s with
|
|
309
|
+
f := ⟨{ h.s.f.m with
|
|
310
|
+
init := h.s.f.m.init ++ h.sums.map (fun d => (d.v, .const 0)),
|
|
311
|
+
step := h.s.f.m.step ++ h.sums.map (fun d => (d.v, sumNext h.s d)),
|
|
312
|
+
assume := .and h.s.f.m.assume (sumsC h.sums) }, h.s.f.flags⟩,
|
|
313
|
+
qflags := h.s.qflags ++ h.sums.map (fun d => (d.q, [d.k], sumBody h.s d)),
|
|
314
|
+
wits := h.s.wits ++ h.sums.map (fun d => (d.q, [d.w])) }
|
|
315
|
+
|
|
316
|
+
-- The writes at every key to a map the term reads.
|
|
317
|
+
def coverWs (s : SMachine) (d : SumDecl) : List (Nat × Nat × E) := s.every.filter (fun w => occME w.1 d.g)
|
|
318
|
+
|
|
319
|
+
-- The maps a term reads.
|
|
320
|
+
mutual
|
|
321
|
+
def mapsE : E → List Nat
|
|
322
|
+
| .var _ => []
|
|
323
|
+
| .const _ => []
|
|
324
|
+
| .add a b => mapsE a ++ mapsE b
|
|
325
|
+
| .sub a b => mapsE a ++ mapsE b
|
|
326
|
+
| .mul _ a => mapsE a
|
|
327
|
+
| .min a b => mapsE a ++ mapsE b
|
|
328
|
+
| .max a b => mapsE a ++ mapsE b
|
|
329
|
+
| .prod a b => mapsE a ++ mapsE b
|
|
330
|
+
| .ite c t f => mapsC c ++ mapsE t ++ mapsE f
|
|
331
|
+
| .get m key => m :: mapsE key
|
|
332
|
+
| .tup a b => mapsE a ++ mapsE b
|
|
333
|
+
| .proj _ a => mapsE a
|
|
334
|
+
def mapsC : C → List Nat
|
|
335
|
+
| .le a b => mapsE a ++ mapsE b
|
|
336
|
+
| .lt a b => mapsE a ++ mapsE b
|
|
337
|
+
| .eq a b => mapsE a ++ mapsE b
|
|
338
|
+
| .and a b => mapsC a ++ mapsC b
|
|
339
|
+
| .or a b => mapsC a ++ mapsC b
|
|
340
|
+
| .not a => mapsC a
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
mutual
|
|
344
|
+
theorem mapsE_occ (mp : Nat) : (e : E) → occME mp e = true → mp ∈ mapsE e
|
|
345
|
+
| .var _, h => by simp [occME] at h
|
|
346
|
+
| .const _, h => by simp [occME] at h
|
|
347
|
+
| .add a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
348
|
+
| .sub a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
349
|
+
| .mul _ a, h => by simp only [occME] at h; simp only [mapsE]; exact mapsE_occ mp a h
|
|
350
|
+
| .min a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
351
|
+
| .max a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
352
|
+
| .prod a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
353
|
+
| .ite c t f, h => by
|
|
354
|
+
simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]
|
|
355
|
+
rcases h with (h | h) | h
|
|
356
|
+
· exact Or.inl (Or.inl (mapsC_occ mp c h))
|
|
357
|
+
· exact Or.inl (Or.inr (mapsE_occ mp t h))
|
|
358
|
+
· exact Or.inr (mapsE_occ mp f h)
|
|
359
|
+
| .get m key, h => by
|
|
360
|
+
simp only [occME, Bool.or_eq_true, beq_iff_eq] at h; simp only [mapsE, List.mem_cons]
|
|
361
|
+
rcases h with h | h
|
|
362
|
+
· exact Or.inl h.symm
|
|
363
|
+
· exact Or.inr (mapsE_occ mp key h)
|
|
364
|
+
| .tup a b, h => by simp only [occME, Bool.or_eq_true] at h; simp only [mapsE, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
365
|
+
| .proj _ a, h => by simp only [occME] at h; simp only [mapsE]; exact mapsE_occ mp a h
|
|
366
|
+
theorem mapsC_occ (mp : Nat) : (c : C) → occMC mp c = true → mp ∈ mapsC c
|
|
367
|
+
| .le a b, h => by simp only [occMC, Bool.or_eq_true] at h; simp only [mapsC, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
368
|
+
| .lt a b, h => by simp only [occMC, Bool.or_eq_true] at h; simp only [mapsC, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
369
|
+
| .eq a b, h => by simp only [occMC, Bool.or_eq_true] at h; simp only [mapsC, List.mem_append]; exact h.imp (mapsE_occ mp a) (mapsE_occ mp b)
|
|
370
|
+
| .and a b, h => by simp only [occMC, Bool.or_eq_true] at h; simp only [mapsC, List.mem_append]; exact h.imp (mapsC_occ mp a) (mapsC_occ mp b)
|
|
371
|
+
| .or a b, h => by simp only [occMC, Bool.or_eq_true] at h; simp only [mapsC, List.mem_append]; exact h.imp (mapsC_occ mp a) (mapsC_occ mp b)
|
|
372
|
+
| .not a, h => by simp only [occMC] at h; simp only [mapsC]; exact mapsC_occ mp a h
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
-- The cells' laws at the cover goal's key: each law proven about one key (no further key) that reads nothing but the
|
|
376
|
+
-- cells of maps the term reads, at its key (gOkC: no other variable, no other key), read at x. A cell's kind is one: a
|
|
377
|
+
-- yes/no is at most 1, a choice below its count.
|
|
378
|
+
def cellLaws (ps : List SEntry) (d : SumDecl) : List C := ps.filterMap fun e =>
|
|
379
|
+
match e.law.key with
|
|
380
|
+
| some kv => if e.keys2.isEmpty && gOkC kv e.law.holds && (mapsC e.law.holds).all (fun mp => (mapsE d.g).contains mp)
|
|
381
|
+
then some (substC [(kv, .var d.x)] e.law.holds) else none
|
|
382
|
+
| none => none
|
|
383
|
+
|
|
384
|
+
-- At a key x none of the cover keys is, a step the rule allows keeps the map's cell, the cells' laws holding at x.
|
|
385
|
+
def sumCoverGoal (s : SMachine) (ps : List SEntry) (d : SumDecl) (w : Nat × Nat × E) : List C :=
|
|
386
|
+
[inlineC s.f.flags s.f.m.assume] ++ cellLaws ps d ++ d.cs.map (fun c => .not (.eq (.var d.x) (inlineE s.f.flags c)))
|
|
387
|
+
++ [.not (.eq (substE [(w.2.1, .var d.x)] (inlineE s.f.flags w.2.2)) (.get w.1 (.var d.x)))]
|
|
388
|
+
|
|
389
|
+
def startVal (s : SMachine) (mp : Nat) : Nat := ((lookup (fun p => p.1 == mp) s.f.m.maps0).map (·.2)).getD 0
|
|
390
|
+
|
|
391
|
+
-- kernel.ts's conditions on each sum.
|
|
392
|
+
def sumOk1 (h : SumMachine) (d : SumDecl) : Bool :=
|
|
393
|
+
let s := h.s
|
|
394
|
+
gOkE d.k d.g
|
|
395
|
+
-- 0 at the start at every key: each map it reads starts at a constant, and the term there is 0
|
|
396
|
+
&& (mapsE d.g).all (fun mp => (s.f.m.maps0.map (·.1)).contains mp)
|
|
397
|
+
&& evalE (fun _ => 0) (fun mp _ => startVal s mp) d.g == 0
|
|
398
|
+
&& avoidsMapsE s.carried d.g
|
|
399
|
+
-- a map it reads written at one key: the key is a cover key
|
|
400
|
+
&& s.f.m.writes.all (fun w => !occME w.1 d.g || d.cs.any (fun c => beqE c w.2.1))
|
|
401
|
+
-- written at every key: the key bound there is read by no flag, nor is the cover goal's key by anything it reads
|
|
402
|
+
&& (coverWs s d).all (fun w => s.f.flags.all (fun p => !occC w.2.1 p.2) && !(flagVars s.f.flags).contains w.2.1
|
|
403
|
+
&& avoidsE [d.x] (inlineE s.f.flags w.2.2) && d.x != w.2.1)
|
|
404
|
+
&& d.cs.all (fun c => avoidsE [d.k] c && avoidsE [d.x] (inlineE s.f.flags c))
|
|
405
|
+
&& avoids [d.x] (inlineC s.f.flags s.f.m.assume)
|
|
406
|
+
-- the sum's variable: no input, flag, quantified flag or witness, set by no start or step of the rule, not the key
|
|
407
|
+
&& !(s.f.m.inputs ++ flagVars s.f.flags ++ h.plain.qVars ++ h.plain.wVars ++ s.f.m.init.map (·.1) ++ s.f.m.step.map (·.1)).contains d.v
|
|
408
|
+
&& d.v != d.k
|
|
409
|
+
|
|
410
|
+
def sumOk (h : SumMachine) : Bool := distinct (h.sums.map (·.v)) && h.sums.all (sumOk1 h)
|
|
411
|
+
|
|
412
|
+
-- Per sum, per write at every key to a map it reads: (table, tree) for its cover goal, over the laws `ps` proven of the
|
|
413
|
+
-- plain machine.
|
|
414
|
+
def checkSums (h : SumMachine) (ps : List SEntry) (covers : List (List (List VDef × Tree))) : Bool :=
|
|
415
|
+
sumOk h && covers.length == h.sums.length
|
|
416
|
+
&& (h.sums.zip covers).all (fun p => (coverWs h.s p.1).length == p.2.length
|
|
417
|
+
&& ((coverWs h.s p.1).zip p.2).all (fun x => checkGoal ⟨x.2.1, sumCoverGoal h.s ps p.1 x.1⟩ x.2.2))
|
|
418
|
+
|
|
419
|
+
/-! What the checks give -/
|
|
420
|
+
|
|
421
|
+
theorem sumOk1_parts {h : SumMachine} {d : SumDecl} (hd : sumOk1 h d = true) :
|
|
422
|
+
gOkE d.k d.g = true
|
|
423
|
+
∧ (∀ mp ∈ mapsE d.g, mp ∈ h.s.f.m.maps0.map (·.1))
|
|
424
|
+
∧ evalE (fun _ => 0) (fun mp _ => startVal h.s mp) d.g = 0
|
|
425
|
+
∧ avoidsMapsE h.s.carried d.g = true
|
|
426
|
+
∧ (∀ w ∈ h.s.f.m.writes, occME w.1 d.g = true → ∃ c ∈ d.cs, beqE c w.2.1 = true)
|
|
427
|
+
∧ (∀ w ∈ coverWs h.s d, (∀ p ∈ h.s.f.flags, occC w.2.1 p.2 = false) ∧ w.2.1 ∉ flagVars h.s.f.flags
|
|
428
|
+
∧ avoidsE [d.x] (inlineE h.s.f.flags w.2.2) = true ∧ d.x ≠ w.2.1)
|
|
429
|
+
∧ (∀ c ∈ d.cs, avoidsE [d.k] c = true ∧ avoidsE [d.x] (inlineE h.s.f.flags c) = true)
|
|
430
|
+
∧ avoids [d.x] (inlineC h.s.f.flags h.s.f.m.assume) = true
|
|
431
|
+
∧ d.v ∉ h.s.f.m.inputs ++ flagVars h.s.f.flags ++ h.plain.qVars ++ h.plain.wVars ++ h.s.f.m.init.map (·.1) ++ h.s.f.m.step.map (·.1)
|
|
432
|
+
∧ d.v ≠ d.k := by
|
|
433
|
+
simp only [sumOk1, Bool.and_eq_true, List.all_eq_true, List.any_eq_true, Bool.not_eq_true', beq_iff_eq, bne_iff_ne, ne_eq,
|
|
434
|
+
Bool.or_eq_true, List.contains_iff_mem] at hd
|
|
435
|
+
obtain ⟨⟨⟨⟨⟨⟨⟨⟨⟨hg, hm⟩, h0⟩, hc⟩, hw⟩, hev⟩, hcs⟩, ha⟩, hv⟩, hvk⟩ := hd
|
|
436
|
+
refine ⟨hg, hm, h0, hc, fun w hw' ho => ?_, fun w hw' => ?_, hcs, ha, ?_, hvk⟩
|
|
437
|
+
· rcases hw w hw' with h1 | h1
|
|
438
|
+
· simp [h1] at ho
|
|
439
|
+
· exact h1
|
|
440
|
+
· obtain ⟨⟨⟨h1, h2⟩, h3⟩, h4⟩ := hev w hw'
|
|
441
|
+
exact ⟨fun p hp => by simpa using h1 p hp, by simpa using h2, h3, h4⟩
|
|
442
|
+
· intro hm'; simp_all
|
|
443
|
+
|
|
444
|
+
theorem sumOk_parts {h : SumMachine} (hok : sumOk h = true) : distinct (h.sums.map (·.v)) = true ∧ ∀ d ∈ h.sums, sumOk1 h d = true := by
|
|
445
|
+
simp only [sumOk, Bool.and_eq_true, List.all_eq_true] at hok; exact hok
|
|
446
|
+
|
|
447
|
+
theorem checkSums_parts {h : SumMachine} {ps : List SEntry} {covers : List (List (List VDef × Tree))} (hc : checkSums h ps covers = true) :
|
|
448
|
+
sumOk h = true ∧ ∀ d ∈ h.sums, ∀ w ∈ coverWs h.s d, ∃ t : List VDef × Tree, checkGoal ⟨t.1, sumCoverGoal h.s ps d w⟩ t.2 = true := by
|
|
449
|
+
simp only [checkSums, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hc
|
|
450
|
+
obtain ⟨⟨hok, hlen⟩, hall⟩ := hc
|
|
451
|
+
refine ⟨hok, fun d hd w hw => ?_⟩
|
|
452
|
+
obtain ⟨cl, hcl⟩ := zip_cover h.sums covers hlen.symm d hd
|
|
453
|
+
obtain ⟨hl2, hall2⟩ := hall _ hcl
|
|
454
|
+
obtain ⟨t, ht⟩ := zip_cover (coverWs h.s d) cl hl2 w hw
|
|
455
|
+
exact ⟨t, hall2 _ ht⟩
|
|
456
|
+
|
|
457
|
+
/-! Lookups -/
|
|
458
|
+
|
|
459
|
+
theorem assoc_after {σ τ : List (Nat × E)} {i : Nat} (h : i ∉ σ.map (·.1)) : assoc (σ ++ τ) i = assoc τ i := by
|
|
460
|
+
have hσ : lookup (fun p => p.1 == i) σ = none :=
|
|
461
|
+
lookup_none (fun p hp => by simp only [beq_eq_false_iff_ne]; intro he; exact h (by rw [← he]; exact List.mem_map_of_mem hp))
|
|
462
|
+
simp only [assoc, lookup_append, hσ]
|
|
463
|
+
|
|
464
|
+
theorem assoc_sums (F : SumDecl → E) {ds : List SumDecl} (hd : distinct (ds.map (·.v)) = true) {d : SumDecl} (hm : d ∈ ds) :
|
|
465
|
+
assoc (ds.map (fun d => (d.v, F d))) d.v = some (F d) := by
|
|
466
|
+
have hd' : distinct ((ds.map (fun d => (d.v, F d))).map (·.1)) = true := by rw [List.map_map]; exact hd
|
|
467
|
+
have := lookup_unique_fst hd' (List.mem_map_of_mem (f := fun d => (d.v, F d)) hm)
|
|
468
|
+
simp only [assoc, this, Option.map_some]
|
|
469
|
+
|
|
470
|
+
theorem upd_at (ρ : Nat → Nat) (i v : Nat) : upd ρ i v i = v := by simp [upd]
|
|
471
|
+
|
|
472
|
+
/-! The terms' values -/
|
|
473
|
+
|
|
474
|
+
section terms
|
|
475
|
+
variable (h : SumMachine) (d : SumDecl)
|
|
476
|
+
|
|
477
|
+
theorem gHere_eval (hg : gOkE d.k d.g = true) (W : Nat → Nat) (N : Nat → Nat → Nat) (t : E) :
|
|
478
|
+
evalE W N (gHere d t) = gAt d.g N (evalE W N t) := by
|
|
479
|
+
rw [gHere, subst_evalE, applyS_single, gAt_eval hg, upd_at]
|
|
480
|
+
|
|
481
|
+
theorem postRd_eval (D : Nat → Nat) (N : Nat → Nat → Nat) (mp : Nat) (key : E) :
|
|
482
|
+
evalE D N (postRd h.s mp key) = everyMaps h.s D N mp (evalE D N key) := by
|
|
483
|
+
simp only [postRd, everyMaps]
|
|
484
|
+
cases hl : lookup (fun w => w.1 == mp) h.s.every with
|
|
485
|
+
| some w => simp only [subst_evalE, applyS_single]
|
|
486
|
+
| none =>
|
|
487
|
+
simp only [writeMaps]
|
|
488
|
+
cases hl2 : lookup (fun w => w.1 == mp) h.s.f.m.writes with
|
|
489
|
+
| some w => simp only [evalE, evalC]; by_cases hk : evalE D N key = evalE D N w.2.1 <;> simp [hk]
|
|
490
|
+
| none => rfl
|
|
491
|
+
|
|
492
|
+
theorem gAfter_eval (hg : gOkE d.k d.g = true) (W : Nat → Nat) (N : Nat → Nat → Nat) (t : E) :
|
|
493
|
+
evalE W N (gAfter h.s d t) = gAt d.g (everyMaps h.s W N) (evalE W N t) := by
|
|
494
|
+
rw [gAfter, substR_evalE_on [(d.k, t)] (postRd h.s) W N (everyMaps h.s W N) [] (fun m key _ => postRd_eval h W N m key) d.g (by simp [avoidsMapsE]),
|
|
495
|
+
applyS_single, gAt_eval hg, upd_at]
|
|
496
|
+
|
|
497
|
+
-- The term reads no carried map: read through the maps a request carries, it is the term over the state's maps.
|
|
498
|
+
theorem gAt_carried (hc : avoidsMapsE h.s.carried d.g = true) (M κ : Nat → Nat → Nat) :
|
|
499
|
+
gAt d.g (withCarried h.s M κ) = gAt d.g M := by
|
|
500
|
+
funext key
|
|
501
|
+
apply gAt_congr; intro mp hmp
|
|
502
|
+
simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using avoidsMapsE_occ hc hmp)]
|
|
503
|
+
|
|
504
|
+
end terms
|
|
505
|
+
|
|
506
|
+
/-! The cells' laws at the cover goal's key -/
|
|
507
|
+
|
|
508
|
+
/-- Where every law of `ps` holds of the state (ρ, M), each cell law holds at the cover goal's key in any view that
|
|
509
|
+
reads the maps a request doesn't carry as M does. -/
|
|
510
|
+
theorem cellLaw_holds {ps : List SEntry} {d : SumDecl} {c : C} (hc : c ∈ cellLaws ps d) {carried : List Nat}
|
|
511
|
+
(hcar : ∀ e ∈ ps, avoidsMaps carried e.law.holds = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat} (hlaws : ∀ e ∈ ps, e.Holds ρ M)
|
|
512
|
+
(W : Nat → Nat) (N : Nat → Nat → Nat) (hN : ∀ mp, mp ∉ carried → N mp (W d.x) = M mp (W d.x)) : evalC W N c = true := by
|
|
513
|
+
obtain ⟨e, he, hsome⟩ := List.mem_filterMap.mp hc
|
|
514
|
+
cases hk : e.law.key with
|
|
515
|
+
| none => simp [hk] at hsome
|
|
516
|
+
| some kv =>
|
|
517
|
+
simp only [hk] at hsome
|
|
518
|
+
split at hsome
|
|
519
|
+
· rename_i hif
|
|
520
|
+
simp only [Option.some.injEq] at hsome
|
|
521
|
+
subst hsome
|
|
522
|
+
simp only [Bool.and_eq_true, List.isEmpty_iff] at hif
|
|
523
|
+
obtain ⟨⟨hk2, hg⟩, _⟩ := hif
|
|
524
|
+
have hl := hlaws e he
|
|
525
|
+
simp only [SEntry.Holds, hk2, holdsOver, MLaw.Holds, hk] at hl
|
|
526
|
+
have h1 := hl (W d.x)
|
|
527
|
+
rw [g_sliceC kv _ M _ hg] at h1
|
|
528
|
+
rw [subst_evalC, applyS_single, g_sliceC kv _ N _ hg]
|
|
529
|
+
simp only [evalE, upd_at] at h1 ⊢
|
|
530
|
+
rw [congrMC _ _ _ _ (fun mp hmp => funext fun _ => hN mp (avoidsMaps_occ (hcar e he) hmp))]
|
|
531
|
+
exact h1
|
|
532
|
+
· simp at hsome
|
|
533
|
+
|
|
534
|
+
/-! The sums are exact -/
|
|
535
|
+
|
|
536
|
+
section exact
|
|
537
|
+
variable (h : SumMachine)
|
|
538
|
+
|
|
539
|
+
-- The view reads a sum's variable as the state does.
|
|
540
|
+
theorem view_v {d : SumDecl} (hv : d.v ∉ h.s.f.m.inputs ++ flagVars h.s.f.flags ++ h.plain.qVars ++ h.plain.wVars ++ h.s.f.m.init.map (·.1) ++ h.s.f.m.step.map (·.1))
|
|
541
|
+
(ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) : (sView h.plain ρ M ι κ).1 d.v = ρ d.v := by
|
|
542
|
+
simp only [List.mem_append, not_or] at hv
|
|
543
|
+
show decideFlags h.s.f.flags (decideQ h.plain (withInputs h.s.f.m.inputs ρ ι) (withCarried h.s M κ)) (withCarried h.s M κ) d.v = ρ d.v
|
|
544
|
+
rw [decide_other hv.1.1.1.1.2, decideQ_other hv.1.1.1.2, withInputs_other hv.1.1.1.1.1]
|
|
545
|
+
|
|
546
|
+
/-- One step the rule's assumption allows keeps every sum exact, from a state where the laws hold. -/
|
|
547
|
+
theorem sum_step (hL : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}
|
|
548
|
+
(hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) (hlaws : ∀ e ∈ ps, e.Holds ρ M) (ι : Nat → Nat) (κ : Nat → Nat → Nat)
|
|
549
|
+
(ha : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume = true) :
|
|
550
|
+
∀ d ∈ h.sums, IsSum (gAt d.g (sNext h.plain ρ M ι κ).2) ((sNext h.plain ρ M ι κ).1 d.v) := by
|
|
551
|
+
obtain ⟨hok, hcov⟩ := checkSums_parts hc
|
|
552
|
+
obtain ⟨hdist, hall⟩ := sumOk_parts hok
|
|
553
|
+
intro d hd
|
|
554
|
+
obtain ⟨hg, _, _, hcar, hwr, hev, hcs, hax, hv, _⟩ := sumOk1_parts (hall d hd)
|
|
555
|
+
let X := decideQ h.plain (withInputs h.s.f.m.inputs ρ ι) (withCarried h.s M κ)
|
|
556
|
+
let N := withCarried h.s M κ
|
|
557
|
+
let V := decideFlags h.s.f.flags X N
|
|
558
|
+
have hV : (sView h.plain ρ M ι κ).1 = V := rfl
|
|
559
|
+
have hN : (sView h.plain ρ M ι κ).2 = N := rfl
|
|
560
|
+
let M' := everyMaps h.s V N
|
|
561
|
+
have hM' : (sNext h.plain ρ M ι κ).2 = M' := rfl
|
|
562
|
+
let cs' := d.cs.map (evalE V N)
|
|
563
|
+
-- The term moves only at the cover keys.
|
|
564
|
+
have hag : ∀ key, key ∉ cs' → gAt d.g M' key = gAt d.g M key := by
|
|
565
|
+
intro key hkey
|
|
566
|
+
apply gAt_congr; intro mp hmp
|
|
567
|
+
have hNM : N mp key = M mp key := by
|
|
568
|
+
simp only [N, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using avoidsMapsE_occ hcar hmp)]
|
|
569
|
+
simp only [M', everyMaps]
|
|
570
|
+
cases hl : lookup (fun w => w.1 == mp) h.s.every with
|
|
571
|
+
| some w =>
|
|
572
|
+
obtain ⟨hwm, hw1⟩ := lookup_some hl
|
|
573
|
+
simp only [beq_iff_eq] at hw1
|
|
574
|
+
have hwc : w ∈ coverWs h.s d := List.mem_filter.mpr ⟨hwm, by rw [hw1]; exact hmp⟩
|
|
575
|
+
obtain ⟨t, ht⟩ := hcov d hd w hwc
|
|
576
|
+
obtain ⟨hfl, hbf, hxv, hxb⟩ := hev w hwc
|
|
577
|
+
show evalE (upd V w.2.1 key) N w.2.2 = M mp key
|
|
578
|
+
apply Decidable.byContradiction; intro hne
|
|
579
|
+
apply sound ⟨t.1, sumCoverGoal h.s ps d w⟩ t.2 ht (upd X d.x key) N
|
|
580
|
+
intro c hcm
|
|
581
|
+
simp only [sumCoverGoal, List.mem_append, List.mem_singleton, List.mem_map] at hcm
|
|
582
|
+
rcases hcm with ((rfl | hcl) | ⟨c0, hc0, rfl⟩) | rfl
|
|
583
|
+
· rw [congrC _ X N _ (fun i hi => by simp only [upd]; rw [if_neg (fun he => avoids_occ hax hi (by simp [he]))]), inline_evalC]
|
|
584
|
+
exact ha
|
|
585
|
+
· refine cellLaw_holds hcl (carried := h.s.carried) (fun e he => (slaws_avoid hL e.law (List.mem_map_of_mem he)).2) hlaws _ N (fun mp hmp => ?_)
|
|
586
|
+
simp only [upd_at, N, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]
|
|
587
|
+
· have hc0' := (hcs c0 hc0).2
|
|
588
|
+
simp only [evalC, evalE, upd_at, Bool.not_eq_true', decide_eq_false_iff_not]
|
|
589
|
+
rw [congrE _ X N _ (fun i hi => by simp only [upd]; rw [if_neg (fun he => avoidsE_occ hc0' hi (by simp [he]))]), inline_evalE]
|
|
590
|
+
intro he; exact hkey (List.mem_map.mpr ⟨c0, hc0, he.symm⟩)
|
|
591
|
+
· simp only [evalC, evalE, Bool.not_eq_true', decide_eq_false_iff_not, upd_at]
|
|
592
|
+
rw [subst_evalE, applyS_single]
|
|
593
|
+
simp only [evalE, upd_at]
|
|
594
|
+
rw [congrE _ (upd X w.2.1 key) N _ (fun i hi => by
|
|
595
|
+
simp only [upd]; by_cases hib : i = w.2.1
|
|
596
|
+
· simp [hib]
|
|
597
|
+
· rw [if_neg hib, if_neg hib, if_neg (fun he => avoidsE_occ hxv hi (by simp [he]))]), inline_evalE,
|
|
598
|
+
decide_upd hbf (fun p hp => hfl p hp)]
|
|
599
|
+
intro he; rw [he] at hne; exact hne (by rw [hw1]; exact hNM)
|
|
600
|
+
| none =>
|
|
601
|
+
simp only [writeMaps]
|
|
602
|
+
cases hl2 : lookup (fun w => w.1 == mp) h.s.f.m.writes with
|
|
603
|
+
| some w =>
|
|
604
|
+
obtain ⟨hwm, hw1⟩ := lookup_some hl2
|
|
605
|
+
simp only [beq_iff_eq] at hw1
|
|
606
|
+
obtain ⟨c, hcm, hce⟩ := hwr w hwm (by rw [hw1]; exact hmp)
|
|
607
|
+
have hin : evalE V N w.2.1 ∈ cs' := List.mem_map.mpr ⟨c, hcm, by rw [beqE_eq hce]⟩
|
|
608
|
+
have hne : key ≠ evalE V N w.2.1 := fun he => hkey (by rw [he]; exact hin)
|
|
609
|
+
simp only [if_neg hne]; exact hNM
|
|
610
|
+
| none => exact hNM
|
|
611
|
+
have hold : evalE V N (dTermE (gHere d) d.cs) = dsum (gAt d.g M) cs' := by
|
|
612
|
+
rw [dTermE_eval V N d.cs (gHere d) (gAt d.g N) (fun t => gHere_eval d hg V N t), gAt_carried h d hcar]
|
|
613
|
+
have hnew : evalE V N (dTermE (gAfter h.s d) d.cs) = dsum (gAt d.g M') cs' :=
|
|
614
|
+
dTermE_eval V N d.cs (gAfter h.s d) (gAt d.g M') (fun t => gAfter_eval h d hg V N t)
|
|
615
|
+
have hval : (sNext h.plain ρ M ι κ).1 d.v = ρ d.v + dsum (gAt d.g M') cs' - dsum (gAt d.g M) cs' := by
|
|
616
|
+
show applyS (h.s.f.m.step ++ h.sums.map (fun d => (d.v, sumNext h.s d))) V N d.v = _
|
|
617
|
+
simp only [applyS]
|
|
618
|
+
rw [assoc_after (fun hm => hv (by simp [hm])), assoc_sums (sumNext h.s) hdist hd]
|
|
619
|
+
simp only [sumNext, evalE, hold, hnew]
|
|
620
|
+
rw [← hV, view_v h hv ρ M ι κ]
|
|
621
|
+
rw [hval, hM']
|
|
622
|
+
exact isSum_change cs' (hsum d hd) hag
|
|
623
|
+
|
|
624
|
+
-- At the start every term is 0 at every key, and every sum 0.
|
|
625
|
+
theorem sum_start (hc : checkSums h ps covers = true) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (hM : StartMaps h.plain.f.m M) :
|
|
626
|
+
∀ d ∈ h.sums, IsSum (gAt d.g M) (applyS h.plain.f.m.init ρ M d.v) := by
|
|
627
|
+
obtain ⟨hok, _⟩ := checkSums_parts hc
|
|
628
|
+
obtain ⟨hdist, hall⟩ := sumOk_parts hok
|
|
629
|
+
intro d hd
|
|
630
|
+
obtain ⟨_, hm, h0, _, _, _, _, _, hv, _⟩ := sumOk1_parts (hall d hd)
|
|
631
|
+
have hval : applyS h.plain.f.m.init ρ M d.v = 0 := by
|
|
632
|
+
show applyS (h.s.f.m.init ++ h.sums.map (fun d => (d.v, E.const 0))) ρ M d.v = 0
|
|
633
|
+
simp only [applyS]
|
|
634
|
+
rw [assoc_after (fun hm' => hv (by simp [hm'])), assoc_sums (fun _ => E.const 0) hdist hd]
|
|
635
|
+
rfl
|
|
636
|
+
rw [hval]
|
|
637
|
+
refine isSum_zero (fun key => ?_)
|
|
638
|
+
rw [gAt]
|
|
639
|
+
refine Eq.trans (congrME _ _ _ d.g (fun mp hmp => ?_)) h0
|
|
640
|
+
funext _
|
|
641
|
+
obtain ⟨p, hp, hpe⟩ := List.mem_map.mp (hm mp (mapsE_occ mp d.g hmp))
|
|
642
|
+
cases hl : lookup (fun p => p.1 == mp) h.s.f.m.maps0 with
|
|
643
|
+
| none => exact absurd (lookup_none_all hl p hp) (by simp [hpe])
|
|
644
|
+
| some p0 =>
|
|
645
|
+
obtain ⟨hp0, hp0e⟩ := lookup_some hl
|
|
646
|
+
simp only [beq_iff_eq] at hp0e
|
|
647
|
+
simp only [startVal, hl, Option.map_some, Option.getD_some]
|
|
648
|
+
rw [← hp0e]; exact hM p0 hp0 key
|
|
649
|
+
|
|
650
|
+
-- Each sum's quantified flag, in the plain machine.
|
|
651
|
+
theorem q_mem {d : SumDecl} (hd : d ∈ h.sums) : (d.q, [d.k], sumBody h.s d) ∈ h.plain.qflags :=
|
|
652
|
+
List.mem_append_right _ (List.mem_map.mpr ⟨d, hd, rfl⟩)
|
|
653
|
+
|
|
654
|
+
/-- Where every sum is exact, the facts the plain machine assumes hold of every request, in its view. -/
|
|
655
|
+
theorem sum_facts (hc : checkSums h ps covers = true) (hs : sOk h.plain = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}
|
|
656
|
+
(hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
|
|
657
|
+
evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 (sumsC h.sums) = true := by
|
|
658
|
+
obtain ⟨hok, _⟩ := checkSums_parts hc
|
|
659
|
+
obtain ⟨_, hall⟩ := sumOk_parts hok
|
|
660
|
+
obtain ⟨_, hqd, _, _, hdx, _, _⟩ := sOk_parts h.plain hs
|
|
661
|
+
let N := withCarried h.s M κ
|
|
662
|
+
let Wi := withInputs h.s.f.m.inputs ρ ι
|
|
663
|
+
have one : ∀ d ∈ h.sums, evalC (sView h.plain ρ M ι κ).1 N (.and (sumFact d) (.eq (.var d.q) (.const 1))) = true := by
|
|
664
|
+
intro d hd
|
|
665
|
+
obtain ⟨hg, _, _, hcar, _, _, _, _, hv, hvk⟩ := sumOk1_parts (hall d hd)
|
|
666
|
+
have hvi : d.v ∉ h.s.f.m.inputs := fun hm => hv (by simp [hm])
|
|
667
|
+
simp only [evalC, Bool.and_eq_true, decide_eq_true_eq]
|
|
668
|
+
refine ⟨?_, ?_⟩
|
|
669
|
+
· show decide (evalE _ N (dTermE (gHere d) d.cs) ≤ (sView h.plain ρ M ι κ).1 d.v) = true
|
|
670
|
+
apply decide_eq_true
|
|
671
|
+
rw [dTermE_eval _ N d.cs (gHere d) (gAt d.g N) (fun t => gHere_eval d hg _ N t), gAt_carried h d hcar, view_v h hv ρ M ι κ]
|
|
672
|
+
exact dsum_le (hsum d hd) _
|
|
673
|
+
· have hqv : d.q ∈ h.plain.qVars := List.mem_map_of_mem (q_mem h hd)
|
|
674
|
+
have hqf : d.q ∉ flagVars h.s.f.flags := fun hm =>
|
|
675
|
+
(distinct_append hdx).2.2 d.q (by simp only [SMachine.extVars, List.mem_append]; exact Or.inl (Or.inr hqv)) hm
|
|
676
|
+
show decideFlags h.s.f.flags (decideQ h.plain Wi N) N d.q = 1
|
|
677
|
+
rw [decide_other hqf, decideQ_of, lookup_unique hqd (q_mem h hd)]
|
|
678
|
+
simp only
|
|
679
|
+
rw [if_pos]
|
|
680
|
+
intro key
|
|
681
|
+
rw [← evalC_iff]
|
|
682
|
+
show decide (evalE _ N (dTermE (gHere d) (.var d.k :: d.cs.map (inlineE h.s.f.flags))) ≤ atKeys Wi [d.k] key d.v) = true
|
|
683
|
+
apply decide_eq_true
|
|
684
|
+
rw [dTermE_eval _ N _ (gHere d) (gAt d.g N) (fun t => gHere_eval d hg _ N t), gAt_carried h d hcar,
|
|
685
|
+
atKeys_out (show d.v ∉ [d.k] by simpa using hvk)]
|
|
686
|
+
show _ ≤ withInputs h.s.f.m.inputs ρ ι d.v
|
|
687
|
+
rw [withInputs_other hvi]
|
|
688
|
+
exact dsum_le (hsum d hd) _
|
|
689
|
+
have all : ∀ ds : List SumDecl, (∀ d ∈ ds, d ∈ h.sums) → evalC (sView h.plain ρ M ι κ).1 N (sumsC ds) = true := by
|
|
690
|
+
intro ds
|
|
691
|
+
induction ds with
|
|
692
|
+
| nil => intro _; rfl
|
|
693
|
+
| cons d ds ih =>
|
|
694
|
+
intro hm
|
|
695
|
+
have h1 := one d (hm d List.mem_cons_self)
|
|
696
|
+
have h2 := ih (fun d' hd' => hm d' (List.mem_cons_of_mem _ hd'))
|
|
697
|
+
show (evalC _ N (.and (sumFact d) (.eq (.var d.q) (.const 1))) && evalC _ N (sumsC ds)) = true
|
|
698
|
+
rw [h1, h2]; rfl
|
|
699
|
+
exact all h.sums (fun _ hd => hd)
|
|
700
|
+
|
|
701
|
+
/-- The plain machine allows exactly the requests the rule's own assumption allows, wherever the sums are exact. -/
|
|
702
|
+
theorem sum_assume (hc : checkSums h ps covers = true) (hs : sOk h.plain = true) {ρ : Nat → Nat} {M : Nat → Nat → Nat}
|
|
703
|
+
(hsum : ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
|
|
704
|
+
evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.plain.f.m.assume
|
|
705
|
+
= evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume := by
|
|
706
|
+
show (evalC _ _ h.s.f.m.assume && evalC _ _ (sumsC h.sums)) = _
|
|
707
|
+
rw [sum_facts h hc hs hsum ι κ, Bool.and_true]
|
|
708
|
+
|
|
709
|
+
/-- **A sum's cells keep their laws**: in every state the plain machine reaches, each sum's variable is the sum, over
|
|
710
|
+
every key, of its term there, and every law proven of the plain machine holds. Together, by induction over the runs:
|
|
711
|
+
a step keeps each sum exact where its cover goals hold, and they assume, at their key, the laws about one key that read
|
|
712
|
+
only the summed maps' cells there (`cellLaws`: a cell's kind, a yes/no at most 1, a choice below its count), which
|
|
713
|
+
hold at the state before the step. -/
|
|
714
|
+
theorem sum_cells (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
715
|
+
∀ ρ M, SReach h.plain ρ M → (∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) ∧ ∀ e ∈ ps, e.Holds ρ M := by
|
|
716
|
+
intro ρ M hr
|
|
717
|
+
induction hr with
|
|
718
|
+
| start ρ M hM h0 hfa hpf => exact ⟨sum_start h hc ρ M hM, smachine_sound h.plain ps hl _ _ (SReach.start ρ M hM h0 hfa hpf)⟩
|
|
719
|
+
| step ρ M ι κ hr' ha ih =>
|
|
720
|
+
have ha0 : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume = true := by
|
|
721
|
+
have : evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 (.and h.s.f.m.assume (sumsC h.sums)) = true := ha
|
|
722
|
+
simp only [evalC, Bool.and_eq_true] at this; exact this.1
|
|
723
|
+
exact ⟨sum_step h hl hc ih.1 ih.2 ι κ ha0, smachine_sound h.plain ps hl _ _ (SReach.step ρ M ι κ hr' ha)⟩
|
|
724
|
+
|
|
725
|
+
/-- **The sums are exact**: in every state the plain machine reaches, each sum's variable is the sum, over every key,
|
|
726
|
+
of its term there. -/
|
|
727
|
+
theorem sum_holds (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
728
|
+
∀ ρ M, SReach h.plain ρ M → ∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v) :=
|
|
729
|
+
fun ρ M hr => (sum_cells h hl hc ρ M hr).1
|
|
730
|
+
|
|
731
|
+
/-- **The plain machine is the rule**: in every state it reaches, it allows a request exactly when the rule's own
|
|
732
|
+
assumption does. -/
|
|
733
|
+
theorem sum_allowed (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
734
|
+
∀ ρ M, SReach h.plain ρ M → ∀ ι κ, evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.plain.f.m.assume
|
|
735
|
+
= evalC (sView h.plain ρ M ι κ).1 (sView h.plain ρ M ι κ).2 h.s.f.m.assume :=
|
|
736
|
+
fun ρ M hr ι κ => sum_assume h hc (sOk_of hl) (sum_holds h hl hc ρ M hr) ι κ
|
|
737
|
+
|
|
738
|
+
end exact
|
|
739
|
+
|
|
740
|
+
/-! A rule that reads none of its sums -/
|
|
741
|
+
|
|
742
|
+
-- The sums' variables and quantified flags, as ghosts of the rule (Ghost.lean's check: new names nothing of it reads).
|
|
743
|
+
def SumMachine.ghosts (h : SumMachine) : SGhosted := ⟨h.s, h.sums.flatMap (fun d => [(d.v, .const 0, .var d.v), (d.q, .const 0, .var d.q)])⟩
|
|
744
|
+
|
|
745
|
+
theorem ghosts_vars (h : SumMachine) {d : SumDecl} (hd : d ∈ h.sums) : d.v ∈ h.ghosts.vars ∧ d.q ∈ h.ghosts.vars := by
|
|
746
|
+
simp only [SGhosted.vars, SumMachine.ghosts, List.mem_map, List.mem_flatMap, List.mem_cons, List.not_mem_nil, or_false]
|
|
747
|
+
exact ⟨⟨_, ⟨d, hd, Or.inl rfl⟩, rfl⟩, ⟨_, ⟨d, hd, Or.inr rfl⟩, rfl⟩⟩
|
|
748
|
+
|
|
749
|
+
section project
|
|
750
|
+
variable (h : SumMachine) (hg : sghostOk h.ghosts = true)
|
|
751
|
+
include hg
|
|
752
|
+
|
|
753
|
+
-- One step from states that agree outside the sums' names: the plain machine reads the rule's assumption as the rule
|
|
754
|
+
-- does, and moves to a state that agrees outside them, with the same maps.
|
|
755
|
+
theorem sum_ghost_step {ρ ρg : Nat → Nat} (hag : Agree h.ghosts.vars ρg ρ) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
|
|
756
|
+
evalC (sView h.plain ρg M ι κ).1 (sView h.plain ρg M ι κ).2 h.s.f.m.assume = evalC (sView h.s ρ M ι κ).1 (sView h.s ρ M ι κ).2 h.s.f.m.assume
|
|
757
|
+
∧ Agree h.ghosts.vars (sNext h.plain ρg M ι κ).1 (sNext h.s ρ M ι κ).1
|
|
758
|
+
∧ (sNext h.plain ρg M ι κ).2 = (sNext h.s ρ M ι κ).2 := by
|
|
759
|
+
obtain ⟨_, hn, _, ha, hf, hs, _, hw, he, hq, _⟩ := sghostOk_parts h.ghosts hg
|
|
760
|
+
have hX : Agree h.ghosts.vars (withInputs h.s.f.m.inputs ρg ι) (withInputs h.s.f.m.inputs ρ ι) := fun i hi => by
|
|
761
|
+
simp only [withInputs]; split
|
|
762
|
+
· rfl
|
|
763
|
+
· exact hag i hi
|
|
764
|
+
have hQ : Agree h.ghosts.vars (decideQ h.plain (withInputs h.s.f.m.inputs ρg ι) (withCarried h.s M κ))
|
|
765
|
+
(decideQ h.s (withInputs h.s.f.m.inputs ρ ι) (withCarried h.s M κ)) := by
|
|
766
|
+
intro v hv
|
|
767
|
+
rw [decideQ_of, decideQ_of]
|
|
768
|
+
show (match lookup (fun q => q.1 == v) (h.s.qflags ++ h.sums.map (fun d => (d.q, [d.k], sumBody h.s d))) with
|
|
769
|
+
| some q => if ∀ k, holdsC (atKeys (withInputs h.s.f.m.inputs ρg ι) q.2.1 k) (withCarried h.s M κ) q.2.2 then 1 else 0
|
|
770
|
+
| none => withInputs h.s.f.m.inputs ρg ι v) = _
|
|
771
|
+
rw [lookup_append]
|
|
772
|
+
cases hl : lookup (fun q => q.1 == v) h.s.qflags with
|
|
773
|
+
| some q => exact ite_all_congr (fun i hi _ => hX i (avoids_occ (hq q (lookup_some hl).1) hi)) (fun _ _ => rfl)
|
|
774
|
+
| none =>
|
|
775
|
+
have hl2 : lookup (fun q => q.1 == v) (h.sums.map (fun d => (d.q, [d.k], sumBody h.s d))) = none := lookup_none (fun x hx => by
|
|
776
|
+
obtain ⟨d, hd, rfl⟩ := List.mem_map.mp hx
|
|
777
|
+
simp only [beq_eq_false_iff_ne]; intro he'; exact hv (by rw [← he']; exact (ghosts_vars h hd).2))
|
|
778
|
+
simp only [hl2]; exact hX v hv
|
|
779
|
+
have hD : Agree h.ghosts.vars (decideFlags h.s.f.flags (decideQ h.plain (withInputs h.s.f.m.inputs ρg ι) (withCarried h.s M κ)) (withCarried h.s M κ))
|
|
780
|
+
(decideFlags h.s.f.flags (decideQ h.s (withInputs h.s.f.m.inputs ρ ι) (withCarried h.s M κ)) (withCarried h.s M κ)) :=
|
|
781
|
+
decide_agree_on hf (withCarried h.s M κ) hQ
|
|
782
|
+
refine ⟨?_, fun i hi => ?_, ?_⟩
|
|
783
|
+
· show evalC (decideFlags h.s.f.flags (decideQ h.plain (withInputs h.s.f.m.inputs ρg ι) (withCarried h.s M κ)) (withCarried h.s M κ)) _ h.s.f.m.assume = _
|
|
784
|
+
exact congrC _ _ _ _ (fun i hi => hD i (avoids_occ ha hi))
|
|
785
|
+
· have hvi : i ∉ h.sums.map (fun d => d.v) := fun hm => by
|
|
786
|
+
obtain ⟨d, hd, rfl⟩ := List.mem_map.mp hm; exact hi (ghosts_vars h hd).1
|
|
787
|
+
show applyS (h.s.f.m.step ++ h.sums.map (fun d => (d.v, sumNext h.s d))) (decideFlags h.s.f.flags (decideQ h.plain (withInputs h.s.f.m.inputs ρg ι) (withCarried h.s M κ)) (withCarried h.s M κ)) (withCarried h.s M κ) i = _
|
|
788
|
+
rw [applyS_append_other (by simpa using hvi)]
|
|
789
|
+
simp only [sNext, sView, applyS]
|
|
790
|
+
cases hst : assoc h.s.f.m.step i with
|
|
791
|
+
| none => exact hD i hi
|
|
792
|
+
| some e =>
|
|
793
|
+
have hm : (i, e) ∈ h.s.f.m.step := by
|
|
794
|
+
simp only [assoc, Option.map_eq_some_iff] at hst
|
|
795
|
+
obtain ⟨q, hq', rfl⟩ := hst
|
|
796
|
+
obtain ⟨hqm, hqv⟩ := lookup_some hq'
|
|
797
|
+
simp only [beq_iff_eq] at hqv
|
|
798
|
+
rw [← hqv]; exact hqm
|
|
799
|
+
exact congrE _ _ _ e (fun j hj => hD j (avoidsE_occ (hs (i, e) hm) hj))
|
|
800
|
+
· funext mp key
|
|
801
|
+
show everyMaps h.s (decideFlags h.s.f.flags (decideQ h.plain (withInputs h.s.f.m.inputs ρg ι) (withCarried h.s M κ)) (withCarried h.s M κ)) (withCarried h.s M κ) mp key
|
|
802
|
+
= everyMaps h.s (decideFlags h.s.f.flags (decideQ h.s (withInputs h.s.f.m.inputs ρ ι) (withCarried h.s M κ)) (withCarried h.s M κ)) (withCarried h.s M κ) mp key
|
|
803
|
+
simp only [everyMaps]
|
|
804
|
+
cases hl : lookup (fun w => w.1 == mp) h.s.every with
|
|
805
|
+
| some w => exact congrE _ _ _ w.2.2 (fun j hj => upd_agree hD w.2.1 key j (avoidsE_occ (he w (lookup_some hl).1) hj))
|
|
806
|
+
| none =>
|
|
807
|
+
simp only [writeMaps]
|
|
808
|
+
cases hl2 : lookup (fun w => w.1 == mp) h.s.f.m.writes with
|
|
809
|
+
| none => rfl
|
|
810
|
+
| some w =>
|
|
811
|
+
obtain ⟨h1, h2⟩ := hw w (lookup_some hl2).1
|
|
812
|
+
simp only [congrE _ _ _ w.2.1 (fun j hj => hD j (avoidsE_occ h1 hj)), congrE _ _ _ w.2.2 (fun j hj => hD j (avoidsE_occ h2 hj))]
|
|
813
|
+
|
|
814
|
+
/-- **A rule that reads none of its sums**: every state the rule reaches is, outside the sums' names, one the plain
|
|
815
|
+
machine reaches, so a law proven of the plain machine that reads none of them is a law of the rule. -/
|
|
816
|
+
theorem sum_project (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
817
|
+
∀ {ρ M}, SReach h.s ρ M → ∃ ρg, SReach h.plain ρg M ∧ Agree h.ghosts.vars ρg ρ := by
|
|
818
|
+
intro ρ M hr
|
|
819
|
+
induction hr with
|
|
820
|
+
| start ρ M hM h0 hfa hpf =>
|
|
821
|
+
refine ⟨applyS h.plain.f.m.init ρ M, SReach.start ρ M hM h0 hfa hpf, fun i hi => ?_⟩
|
|
822
|
+
have hvi : i ∉ h.sums.map (fun d => d.v) := fun hm => by
|
|
823
|
+
obtain ⟨d, hd, rfl⟩ := List.mem_map.mp hm; exact hi (ghosts_vars h hd).1
|
|
824
|
+
show applyS (h.s.f.m.init ++ h.sums.map (fun d => (d.v, E.const 0))) ρ M i = applyS h.s.f.m.init ρ M i
|
|
825
|
+
exact applyS_append_other (by simpa using hvi) ρ M
|
|
826
|
+
| step ρ M ι κ _ ha ih =>
|
|
827
|
+
obtain ⟨ρg, hrg, hag⟩ := ih
|
|
828
|
+
obtain ⟨hA, hS, hW⟩ := sum_ghost_step h hg hag M ι κ
|
|
829
|
+
refine ⟨_, ?_, hS⟩
|
|
830
|
+
rw [← hW]
|
|
831
|
+
refine SReach.step ρg M ι κ hrg ?_
|
|
832
|
+
rw [sum_allowed h hl hc ρg M hrg ι κ, hA]; exact ha
|
|
833
|
+
|
|
834
|
+
/-- The laws of the plain machine that read no sum's name hold in every state the rule reaches. -/
|
|
835
|
+
theorem sum_project_laws (ps : List SEntry) (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
836
|
+
∀ ρ M, SReach h.s ρ M → ∀ e ∈ ps, avoids h.ghosts.vars e.law.holds = true → e.Holds ρ M := by
|
|
837
|
+
intro ρ M hr e he hav
|
|
838
|
+
obtain ⟨ρg, hrg, hag⟩ := sum_project h hg hl hc hr
|
|
839
|
+
exact sentry_transfer (V := h.ghosts.vars) (MS := []) hav (by simp [avoidsMaps]) (fun i hi => (hag i hi).symm) (fun _ _ => rfl)
|
|
840
|
+
(smachine_sound h.plain ps hl ρg M hrg e he)
|
|
841
|
+
|
|
842
|
+
end project
|
|
843
|
+
|
|
844
|
+
/-! Claims about the rule with its sums -/
|
|
845
|
+
|
|
846
|
+
/-- The laws proven of the plain machine hold in every state it reaches, where every sum is exact. -/
|
|
847
|
+
theorem sum_laws (h : SumMachine) (ps : List SEntry) (covers : List (List (List VDef × Tree)))
|
|
848
|
+
(hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true) :
|
|
849
|
+
∀ ρ M, SReach h.plain ρ M → (∀ d ∈ h.sums, IsSum (gAt d.g M) (ρ d.v)) ∧ ∀ e ∈ ps, e.Holds ρ M :=
|
|
850
|
+
sum_cells h hl hc
|
|
851
|
+
|
|
852
|
+
-- A run of the rule with its sums: each request one the rule's own assumption allows.
|
|
853
|
+
structure SumRun (h : SumMachine) where
|
|
854
|
+
st : Nat → (Nat → Nat) × (Nat → Nat → Nat)
|
|
855
|
+
req : Nat → SRequest
|
|
856
|
+
start : SReach h.plain (st 0).1 (st 0).2
|
|
857
|
+
allowed : ∀ n, evalC (sView h.plain (st n).1 (st n).2 (req n).1 (req n).2).1 (sView h.plain (st n).1 (st n).2 (req n).1 (req n).2).2 h.s.f.m.assume = true
|
|
858
|
+
next : ∀ n, st (n + 1) = sNext h.plain (st n).1 (st n).2 (req n).1 (req n).2
|
|
859
|
+
|
|
860
|
+
-- It is a run of the plain machine: there the facts hold, so the plain machine allows each of its requests.
|
|
861
|
+
def SumRun.plain {h : SumMachine} {ps : List SEntry} {covers : List (List (List VDef × Tree))} (hl : checkSMachine h.plain ps = true) (hc : checkSums h ps covers = true)
|
|
862
|
+
(run : SumRun h) : SRun h.plain :=
|
|
863
|
+
have reach : ∀ n, SReach h.plain (run.st n).1 (run.st n).2 := fun n => by
|
|
864
|
+
induction n with
|
|
865
|
+
| zero => exact run.start
|
|
866
|
+
| succ n ih =>
|
|
867
|
+
rw [run.next n]
|
|
868
|
+
exact SReach.step _ _ _ _ ih (by rw [sum_allowed h hl hc _ _ ih]; exact run.allowed n)
|
|
869
|
+
⟨run.st, run.req, run.start, fun n => by rw [sum_allowed h hl hc _ _ (reach n)]; exact run.allowed n, run.next⟩
|
|
870
|
+
|
|
871
|
+
/-- Liveness of the rule with its sums, along every run that takes each helpful request infinitely often. -/
|
|
872
|
+
theorem sumleads_sound (h : SumMachine) (covers : List (List (List VDef × Tree))) (ps : List SEntry) (l : Live) (sp cp : SProof) (hp : List SProof)
|
|
873
|
+
(hc : checkSums h ps covers = true) (hl : checkSMachine h.plain ps = true) (hok : sliveOk h.plain l = true)
|
|
874
|
+
(hs : checkSKStep h.plain ps (sliveStep l l.pend (liveStayC l) sp) = true) (hcv : checkSKStep h.plain ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
875
|
+
(hh : sliveHelpsOk h.plain ps l hp = true) (run : SumRun h)
|
|
876
|
+
(fair : ∀ x ∈ l.helps, ∀ n, ∃ m, n ≤ m ∧ ∀ p ∈ x.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
877
|
+
∀ n, evalC (run.st n).1 (run.st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 l.to = true :=
|
|
878
|
+
sleads_sound h.plain ps l sp cp hp hl hok hs hcv hh (run.plain hl hc) fair
|
|
879
|
+
|
|
880
|
+
/-- Liveness of the rule with its sums under weak fairness of its own assumption: each helpful request is proven allowed
|
|
881
|
+
by the rule's assumption in its region (the facts the plain machine assumes besides hold there). -/
|
|
882
|
+
theorem sumleads_wf_sound (h : SumMachine) (covers : List (List (List VDef × Tree))) (ps : List SEntry) (l : Live) (sp cp : SProof)
|
|
883
|
+
(hp : List SProof) (ap : List (List (Nat × E) × SProof × List E))
|
|
884
|
+
(hc : checkSums h ps covers = true) (hl : checkSMachine h.plain ps = true) (hok : sliveOk h.plain l = true)
|
|
885
|
+
(hs : checkSKStep h.plain ps (sliveStep l l.pend (liveStayC l) sp) = true) (hcv : checkSKStep h.plain ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
886
|
+
(hh : sliveHelpsOk h.plain ps l hp = true) (ha : sliveAllowsOkA h.plain ps l ap h.s.f.m.assume = true) (run : SumRun h)
|
|
887
|
+
(fair : ∀ x ∈ l.helps, ∀ n, (∀ m, n ≤ m → ∃ ι κ, (∀ p ∈ x.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
888
|
+
∧ evalC (sView h.plain (run.st m).1 (run.st m).2 ι κ).1 (sView h.plain (run.st m).1 (run.st m).2 ι κ).2 h.s.f.m.assume = true) →
|
|
889
|
+
∃ m, n ≤ m ∧ ∀ p ∈ x.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
890
|
+
∀ n, evalC (run.st n).1 (run.st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 l.to = true :=
|
|
891
|
+
sleads_wf_soundA h.plain ps l sp cp hp ap h.s.f.m.assume hl hok hs hcv hh ha (run.plain hl hc) fair
|
|
892
|
+
|
|
893
|
+
/-- The same under strong fairness (Live.lean's `sleads_sf_soundA`). -/
|
|
894
|
+
theorem sumleads_sf_sound (h : SumMachine) (covers : List (List (List VDef × Tree))) (ps : List SEntry) (l : Live) (lv : List Nat) (sp cp : SProof)
|
|
895
|
+
(hp : List SProof) (ap : List (List (Nat × E) × SProof × List E))
|
|
896
|
+
(hc : checkSums h ps covers = true) (hl : checkSMachine h.plain ps = true) (hok : sliveOk h.plain l = true)
|
|
897
|
+
(hs : checkSKStep h.plain ps (sliveStep l l.pend (liveStaySC l) sp) = true) (hcv : checkSKStep h.plain ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
898
|
+
(hh : sliveHelpsOkS h.plain ps l lv hp = true) (ha : sliveAllowsOkA h.plain ps l ap h.s.f.m.assume = true) (run : SumRun h)
|
|
899
|
+
(fair : ∀ x ∈ l.helps, (∀ n, ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 x.1 = true ∧ ∃ ι κ, (∀ p ∈ x.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
900
|
+
∧ evalC (sView h.plain (run.st m).1 (run.st m).2 ι κ).1 (sView h.plain (run.st m).1 (run.st m).2 ι κ).2 h.s.f.m.assume = true) →
|
|
901
|
+
∀ n, ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 x.1 = true ∧ ∀ p ∈ x.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
902
|
+
∀ n, evalC (run.st n).1 (run.st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 l.to = true :=
|
|
903
|
+
sleads_sf_soundA h.plain ps l lv sp cp hp ap h.s.f.m.assume hl hok hs hcv hh ha (run.plain hl hc) fair
|
|
904
|
+
|
|
905
|
+
end Kern
|
|
906
|
+
|
|
907
|
+
#print axioms Kern.isSum_change
|
|
908
|
+
#print axioms Kern.dsum_le
|
|
909
|
+
#print axioms Kern.sum_cells
|
|
910
|
+
#print axioms Kern.sum_holds
|
|
911
|
+
#print axioms Kern.sum_allowed
|
|
912
|
+
#print axioms Kern.sum_laws
|
|
913
|
+
#print axioms Kern.sumleads_sound
|
|
914
|
+
#print axioms Kern.sumleads_wf_sound
|
|
915
|
+
#print axioms Kern.sumleads_sf_sound
|
|
916
|
+
#print axioms Kern.sum_project_laws
|