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,1168 @@
|
|
|
1
|
+
import Sets
|
|
2
|
+
|
|
3
|
+
/-!
|
|
4
|
+
Liveness under fairness: "from leads to to", proven sound for machines with sets (`sleads_sound`, and under weak
|
|
5
|
+
fairness `sleads_wf_sound`); a machine with maps and flags is one with no sets. kernel.ts proves it by a rank (naturals over the state and params, compared in
|
|
6
|
+
order: the first that differs decides) and helpful requests, each with the region where it helps. While `from` holds and
|
|
7
|
+
`to` doesn't: every request allowed keeps `from` or reaches `to`, and reaches `to`, lowers the rank, or keeps the rank
|
|
8
|
+
with every region it was in kept (a step law); some region holds (a step law that reads no step); and each helpful
|
|
9
|
+
request, taken in its region, reaches `to` or lowers the rank (a step law whose `when` names the inputs the request
|
|
10
|
+
sets; the others, and the maps it carries, stay free: a history's queries among them). Lean builds all of them from the
|
|
11
|
+
liveness and checks them with the step-law rules (StepLaw.lean's, Sets.lean's). Then along every run that takes each
|
|
12
|
+
helpful request infinitely often, each state where `from` holds is followed by one where `to` holds: the rank can't drop
|
|
13
|
+
forever, once it stops dropping the region it is in stays, and that region's request comes and lowers it.
|
|
14
|
+
-/
|
|
15
|
+
|
|
16
|
+
namespace Kern
|
|
17
|
+
|
|
18
|
+
/-! Ranks compared in order -/
|
|
19
|
+
|
|
20
|
+
def lexB : List Nat → List Nat → Bool
|
|
21
|
+
| a' :: as', a :: as => decide (a' < a) || (decide (a' = a) && lexB as' as)
|
|
22
|
+
| _, _ => false
|
|
23
|
+
|
|
24
|
+
theorem lexB_irrefl : ∀ x : List Nat, lexB x x = false
|
|
25
|
+
| [] => rfl
|
|
26
|
+
| a :: as => by simp [lexB, lexB_irrefl as]
|
|
27
|
+
|
|
28
|
+
theorem nat_down {g : Nat → Nat} {n0 : Nat} (h : ∀ n, n0 ≤ n → g (n + 1) ≤ g n) : ∀ d, g (n0 + d) ≤ g n0
|
|
29
|
+
| 0 => Nat.le_refl _
|
|
30
|
+
| d + 1 => Nat.le_trans (h (n0 + d) (Nat.le_add_right _ _)) (nat_down h d)
|
|
31
|
+
|
|
32
|
+
-- A natural that never rises from n0 on stops changing.
|
|
33
|
+
theorem nat_stable (g : Nat → Nat) : ∀ v n0, g n0 ≤ v → (∀ n, n0 ≤ n → g (n + 1) ≤ g n) →
|
|
34
|
+
∃ T, n0 ≤ T ∧ ∀ m, T ≤ m → g m = g T := by
|
|
35
|
+
intro v
|
|
36
|
+
induction v with
|
|
37
|
+
| zero =>
|
|
38
|
+
intro n0 hv h
|
|
39
|
+
refine ⟨n0, Nat.le_refl _, fun m hm => ?_⟩
|
|
40
|
+
obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm
|
|
41
|
+
have := nat_down h d; omega
|
|
42
|
+
| succ v ih =>
|
|
43
|
+
intro n0 hv h
|
|
44
|
+
by_cases hall : ∀ m, n0 ≤ m → g m = g n0
|
|
45
|
+
· exact ⟨n0, Nat.le_refl _, hall⟩
|
|
46
|
+
· have hex : ∃ m, n0 ≤ m ∧ g m ≠ g n0 := Classical.byContradiction fun hno =>
|
|
47
|
+
hall fun m hm => Classical.byContradiction fun hne => hno ⟨m, hm, hne⟩
|
|
48
|
+
obtain ⟨m, hm, hne⟩ := hex
|
|
49
|
+
obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm
|
|
50
|
+
have := nat_down h d
|
|
51
|
+
obtain ⟨T, hT, hTm⟩ := ih (n0 + d) (by omega) (fun n hn => h n (by omega))
|
|
52
|
+
exact ⟨T, by omega, hTm⟩
|
|
53
|
+
|
|
54
|
+
-- A rank of k naturals that never rises (in order) from n0 on stops changing.
|
|
55
|
+
theorem lex_stable : ∀ (k : Nat) (r : Nat → List Nat) (n0 : Nat), (∀ n, (r n).length = k) →
|
|
56
|
+
(∀ n, n0 ≤ n → lexB (r (n + 1)) (r n) = true ∨ r (n + 1) = r n) → ∃ T, n0 ≤ T ∧ ∀ m, T ≤ m → r m = r T
|
|
57
|
+
| 0, r, n0, hk, _ => ⟨n0, Nat.le_refl _, fun m _ => by
|
|
58
|
+
rw [List.eq_nil_of_length_eq_zero (hk m), List.eq_nil_of_length_eq_zero (hk n0)]⟩
|
|
59
|
+
| k + 1, r, n0, hk, hmono => by
|
|
60
|
+
have hcons : ∀ n, r n = (r n).headD 0 :: (r n).tail := by
|
|
61
|
+
intro n
|
|
62
|
+
have := hk n
|
|
63
|
+
cases h : r n with
|
|
64
|
+
| nil => rw [h] at this; simp at this
|
|
65
|
+
| cons a as => rfl
|
|
66
|
+
have hstep : ∀ n, n0 ≤ n → (r (n + 1)).headD 0 < (r n).headD 0 ∨
|
|
67
|
+
((r (n + 1)).headD 0 = (r n).headD 0 ∧ (lexB (r (n + 1)).tail (r n).tail = true ∨ (r (n + 1)).tail = (r n).tail)) := by
|
|
68
|
+
intro n hn
|
|
69
|
+
have h := hmono n hn
|
|
70
|
+
rw [hcons (n + 1), hcons n] at h
|
|
71
|
+
simp only [lexB, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq, List.cons.injEq] at h
|
|
72
|
+
rcases h with (h | ⟨h1, h2⟩) | ⟨h1, h2⟩
|
|
73
|
+
· exact Or.inl h
|
|
74
|
+
· exact Or.inr ⟨h1, Or.inl h2⟩
|
|
75
|
+
· exact Or.inr ⟨h1, Or.inr h2⟩
|
|
76
|
+
obtain ⟨T1, hT1, hT1m⟩ := nat_stable (fun n => (r n).headD 0) _ n0 (Nat.le_refl _) (fun n hn => by
|
|
77
|
+
rcases hstep n hn with h | ⟨h, _⟩
|
|
78
|
+
· exact Nat.le_of_lt h
|
|
79
|
+
· exact Nat.le_of_eq h)
|
|
80
|
+
obtain ⟨T2, hT2, hT2m⟩ := lex_stable k (fun n => (r n).tail) T1 (fun n => by simp [hk n]) (fun n hn => by
|
|
81
|
+
rcases hstep n (by omega) with h | ⟨_, h⟩
|
|
82
|
+
· have e1 := hT1m (n + 1) (by omega); have e2 := hT1m n hn; omega
|
|
83
|
+
· exact h)
|
|
84
|
+
refine ⟨T2, by omega, fun m hm => ?_⟩
|
|
85
|
+
have e1 := hT1m m (by omega); have e2 := hT1m T2 hT2; have e3 := hT2m m hm
|
|
86
|
+
rw [hcons m, hcons T2, e3, e1, e2]
|
|
87
|
+
|
|
88
|
+
/-- Along a run: if `P ∧ ¬Q` is kept (or Q reached) with the rank never rising, and kept only with every region kept;
|
|
89
|
+
some region holds while `P ∧ ¬Q` does; and each region's helpful step reaches Q or lowers the rank there; then Q
|
|
90
|
+
follows every P, provided each helpful step is taken from any point on where it stays enabled (E), and it is enabled in
|
|
91
|
+
its region while `P ∧ ¬Q` holds. With E always true, that is: each helpful step taken infinitely often. -/
|
|
92
|
+
theorem leads_of_lex {α : Type} (P Q : Nat → Prop) (r : Nat → List Nat) (k : Nat) (hk : ∀ n, (r n).length = k)
|
|
93
|
+
(helps : List α) (W H E : α → Nat → Prop)
|
|
94
|
+
(hstay : ∀ n, P n → ¬ Q n → (P (n + 1) ∨ Q (n + 1)) ∧
|
|
95
|
+
(Q (n + 1) ∨ lexB (r (n + 1)) (r n) = true ∨ (r (n + 1) = r n ∧ ∀ h ∈ helps, W h n → W h (n + 1))))
|
|
96
|
+
(hcover : ∀ n, P n → ¬ Q n → ∃ h ∈ helps, W h n)
|
|
97
|
+
(hhelp : ∀ h ∈ helps, ∀ n, P n → ¬ Q n → W h n → H h n → Q (n + 1) ∨ lexB (r (n + 1)) (r n) = true)
|
|
98
|
+
(henabled : ∀ h ∈ helps, ∀ n, P n → ¬ Q n → W h n → E h n)
|
|
99
|
+
(fair : ∀ h ∈ helps, ∀ n, (∀ m, n ≤ m → E h m) → ∃ m, n ≤ m ∧ H h m) : ∀ n, P n → ∃ m, n ≤ m ∧ Q m := by
|
|
100
|
+
intro n0 hP
|
|
101
|
+
apply Classical.byContradiction
|
|
102
|
+
intro hno
|
|
103
|
+
have hnq : ∀ m, n0 ≤ m → ¬ Q m := fun m hm hq => hno ⟨m, hm, hq⟩
|
|
104
|
+
have hPd : ∀ d, P (n0 + d) := by
|
|
105
|
+
intro d
|
|
106
|
+
induction d with
|
|
107
|
+
| zero => exact hP
|
|
108
|
+
| succ d ih =>
|
|
109
|
+
rcases (hstay (n0 + d) ih (hnq _ (Nat.le_add_right _ _))).1 with h | h
|
|
110
|
+
· exact h
|
|
111
|
+
· exact absurd h (hnq _ (by omega))
|
|
112
|
+
have hPm : ∀ m, n0 ≤ m → P m := fun m hm => by obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm; exact hPd d
|
|
113
|
+
have hmono : ∀ n, n0 ≤ n → lexB (r (n + 1)) (r n) = true ∨ r (n + 1) = r n := by
|
|
114
|
+
intro n hn
|
|
115
|
+
rcases (hstay n (hPm n hn) (hnq n hn)).2 with h | h | ⟨h, _⟩
|
|
116
|
+
· exact absurd h (hnq _ (by omega))
|
|
117
|
+
· exact Or.inl h
|
|
118
|
+
· exact Or.inr h
|
|
119
|
+
-- The rank stops changing at T; the region T is in stays, and its helpful step would lower the rank.
|
|
120
|
+
obtain ⟨T, hT, hTm⟩ := lex_stable k r n0 hk hmono
|
|
121
|
+
have hlow : ∀ m, T ≤ m → lexB (r (m + 1)) (r m) ≠ true := fun m hm hl => by
|
|
122
|
+
rw [hTm (m + 1) (by omega), hTm m hm, lexB_irrefl] at hl; exact absurd hl (by simp)
|
|
123
|
+
obtain ⟨h, hh, hW⟩ := hcover T (hPm T hT) (hnq T hT)
|
|
124
|
+
have hWd : ∀ d, W h (T + d) := by
|
|
125
|
+
intro d
|
|
126
|
+
induction d with
|
|
127
|
+
| zero => exact hW
|
|
128
|
+
| succ d ih =>
|
|
129
|
+
rcases (hstay (T + d) (hPm _ (by omega)) (hnq _ (by omega))).2 with hq | hl | ⟨_, hkeep⟩
|
|
130
|
+
· exact absurd hq (hnq _ (by omega))
|
|
131
|
+
· exact absurd hl (hlow _ (by omega))
|
|
132
|
+
· exact hkeep h hh ih
|
|
133
|
+
obtain ⟨m, hm, hH⟩ := fair h hh T (fun m hm => by
|
|
134
|
+
obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm
|
|
135
|
+
exact henabled h hh (T + d) (hPm _ (by omega)) (hnq _ (by omega)) (hWd d))
|
|
136
|
+
obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm
|
|
137
|
+
rcases hhelp h hh (T + d) (hPm _ (by omega)) (hnq _ (by omega)) (hWd d) hH with hq | hl
|
|
138
|
+
· exact hnq _ (by omega) hq
|
|
139
|
+
· exact hlow _ (by omega) hl
|
|
140
|
+
|
|
141
|
+
/-! Strong fairness -/
|
|
142
|
+
|
|
143
|
+
-- Finitely many conditions, each holding only finitely often, all stop holding together.
|
|
144
|
+
theorem eventually_none {α : Type} (W : α → Nat → Prop) : ∀ l : List α, (∀ h ∈ l, ∃ B, ∀ m, B ≤ m → ¬ W h m) →
|
|
145
|
+
∃ B, ∀ h ∈ l, ∀ m, B ≤ m → ¬ W h m
|
|
146
|
+
| [], _ => ⟨0, fun _ hh => by simp at hh⟩
|
|
147
|
+
| a :: as, hall => by
|
|
148
|
+
obtain ⟨Ba, hBa⟩ := hall a List.mem_cons_self
|
|
149
|
+
obtain ⟨B, hB⟩ := eventually_none W as (fun h hh => hall h (List.mem_cons_of_mem _ hh))
|
|
150
|
+
refine ⟨max Ba B, fun h hh m hm => ?_⟩
|
|
151
|
+
rcases List.mem_cons.mp hh with rfl | hh
|
|
152
|
+
· exact hBa m (by omega)
|
|
153
|
+
· exact hB h hh m (by omega)
|
|
154
|
+
|
|
155
|
+
-- Among the members of a list where P holds, one where f is largest.
|
|
156
|
+
theorem exists_max {α : Type} (P : α → Prop) (f : α → Nat) : ∀ l : List α, (∃ a ∈ l, P a) → ∃ a ∈ l, P a ∧ ∀ b ∈ l, P b → f b ≤ f a
|
|
157
|
+
| [], ⟨_, ha, _⟩ => by simp at ha
|
|
158
|
+
| x :: xs, hex => by
|
|
159
|
+
by_cases hxs : ∃ a ∈ xs, P a
|
|
160
|
+
· obtain ⟨a, ha, hPa, hmax⟩ := exists_max P f xs hxs
|
|
161
|
+
by_cases hx : P x ∧ f a < f x
|
|
162
|
+
· refine ⟨x, List.mem_cons_self, hx.1, fun b hb hPb => ?_⟩
|
|
163
|
+
rcases List.mem_cons.mp hb with rfl | hb
|
|
164
|
+
· exact Nat.le_refl _
|
|
165
|
+
· have := hmax b hb hPb; omega
|
|
166
|
+
· refine ⟨a, List.mem_cons_of_mem _ ha, hPa, fun b hb hPb => ?_⟩
|
|
167
|
+
rcases List.mem_cons.mp hb with rfl | hb
|
|
168
|
+
· exact Classical.byContradiction fun hlt => hx ⟨hPb, by omega⟩
|
|
169
|
+
· exact hmax b hb hPb
|
|
170
|
+
· obtain ⟨a, ha, hPa⟩ := hex
|
|
171
|
+
rcases List.mem_cons.mp ha with rfl | ha
|
|
172
|
+
· refine ⟨a, List.mem_cons_self, hPa, fun b hb hPb => ?_⟩
|
|
173
|
+
rcases List.mem_cons.mp hb with rfl | hb
|
|
174
|
+
· exact Nat.le_refl _
|
|
175
|
+
· exact absurd ⟨b, hb, hPb⟩ hxs
|
|
176
|
+
· exact absurd ⟨a, ha, hPa⟩ hxs
|
|
177
|
+
|
|
178
|
+
/-- Along a run, under strong fairness: if `P ∧ ¬Q` is kept (or Q reached) with the rank never rising; some region holds
|
|
179
|
+
while `P ∧ ¬Q` does; and each region's helpful step, taken there, reaches Q, lowers the rank, or reaches the region of a
|
|
180
|
+
helpful step of a higher level; then Q follows every P, provided each helpful step taken infinitely often in its
|
|
181
|
+
region wherever it is enabled there infinitely often (E), and it is enabled in its region while `P ∧ ¬Q` holds. The
|
|
182
|
+
regions need not stay: once the rank stops, some region holds infinitely often, and of those the one of the highest
|
|
183
|
+
level is left for good by no step but its own, which comes. -/
|
|
184
|
+
theorem leads_of_lex_sf {α : Type} (P Q : Nat → Prop) (r : Nat → List Nat) (k : Nat) (hk : ∀ n, (r n).length = k)
|
|
185
|
+
(helps : List α) (L : α → Nat) (W H E : α → Nat → Prop)
|
|
186
|
+
(hstay : ∀ n, P n → ¬ Q n → (P (n + 1) ∨ Q (n + 1)) ∧ (Q (n + 1) ∨ lexB (r (n + 1)) (r n) = true ∨ r (n + 1) = r n))
|
|
187
|
+
(hcover : ∀ n, P n → ¬ Q n → ∃ h ∈ helps, W h n)
|
|
188
|
+
(hhelp : ∀ h ∈ helps, ∀ n, P n → ¬ Q n → W h n → H h n →
|
|
189
|
+
Q (n + 1) ∨ lexB (r (n + 1)) (r n) = true ∨ ∃ h' ∈ helps, L h < L h' ∧ W h' (n + 1))
|
|
190
|
+
(henabled : ∀ h ∈ helps, ∀ n, P n → ¬ Q n → W h n → E h n)
|
|
191
|
+
(fair : ∀ h ∈ helps, (∀ n, ∃ m, n ≤ m ∧ W h m ∧ E h m) → ∀ n, ∃ m, n ≤ m ∧ W h m ∧ H h m) :
|
|
192
|
+
∀ n, P n → ∃ m, n ≤ m ∧ Q m := by
|
|
193
|
+
intro n0 hP
|
|
194
|
+
apply Classical.byContradiction
|
|
195
|
+
intro hno
|
|
196
|
+
have hnq : ∀ m, n0 ≤ m → ¬ Q m := fun m hm hq => hno ⟨m, hm, hq⟩
|
|
197
|
+
have hPd : ∀ d, P (n0 + d) := by
|
|
198
|
+
intro d
|
|
199
|
+
induction d with
|
|
200
|
+
| zero => exact hP
|
|
201
|
+
| succ d ih =>
|
|
202
|
+
rcases (hstay (n0 + d) ih (hnq _ (Nat.le_add_right _ _))).1 with h | h
|
|
203
|
+
· exact h
|
|
204
|
+
· exact absurd h (hnq _ (by omega))
|
|
205
|
+
have hPm : ∀ m, n0 ≤ m → P m := fun m hm => by obtain ⟨d, rfl⟩ := Nat.exists_eq_add_of_le hm; exact hPd d
|
|
206
|
+
have hmono : ∀ n, n0 ≤ n → lexB (r (n + 1)) (r n) = true ∨ r (n + 1) = r n := by
|
|
207
|
+
intro n hn
|
|
208
|
+
rcases (hstay n (hPm n hn) (hnq n hn)).2 with h | h | h
|
|
209
|
+
· exact absurd h (hnq _ (by omega))
|
|
210
|
+
· exact Or.inl h
|
|
211
|
+
· exact Or.inr h
|
|
212
|
+
obtain ⟨T, hT, hTm⟩ := lex_stable k r n0 hk hmono
|
|
213
|
+
have hlow : ∀ m, T ≤ m → lexB (r (m + 1)) (r m) ≠ true := fun m hm hl => by
|
|
214
|
+
rw [hTm (m + 1) (by omega), hTm m hm, lexB_irrefl] at hl; exact absurd hl (by simp)
|
|
215
|
+
-- Some region holds infinitely often.
|
|
216
|
+
let io := fun h => ∀ n, ∃ m, n ≤ m ∧ W h m
|
|
217
|
+
have hsome : ∃ h ∈ helps, io h := by
|
|
218
|
+
apply Classical.byContradiction; intro hnone
|
|
219
|
+
have hfin : ∀ h ∈ helps, ∃ B, ∀ m, B ≤ m → ¬ W h m := fun h hh => Classical.byContradiction fun hb =>
|
|
220
|
+
hnone ⟨h, hh, fun n => Classical.byContradiction fun hn => hb ⟨n, fun m hm hw => hn ⟨m, hm, hw⟩⟩⟩
|
|
221
|
+
obtain ⟨B, hB⟩ := eventually_none W helps hfin
|
|
222
|
+
obtain ⟨h, hh, hw⟩ := hcover (max B T) (hPm _ (by omega)) (hnq _ (by omega))
|
|
223
|
+
exact hB h hh (max B T) (by omega) hw
|
|
224
|
+
-- Of those, one of the highest level; the regions of higher levels hold finitely often.
|
|
225
|
+
obtain ⟨h, hh, hio, hmax⟩ := exists_max io L helps hsome
|
|
226
|
+
have hfin : ∀ h' ∈ helps.filter (fun h' => decide (L h < L h')), ∃ B, ∀ m, B ≤ m → ¬ W h' m := by
|
|
227
|
+
intro h' hm'
|
|
228
|
+
obtain ⟨hh', hl⟩ := List.mem_filter.mp hm'
|
|
229
|
+
have hnio : ¬ io h' := fun hi => by have := hmax h' hh' hi; simp at hl; omega
|
|
230
|
+
exact Classical.byContradiction fun hb =>
|
|
231
|
+
hnio fun n => Classical.byContradiction fun hn => hb ⟨n, fun m hm hw => hn ⟨m, hm, hw⟩⟩
|
|
232
|
+
obtain ⟨B, hB⟩ := eventually_none W _ hfin
|
|
233
|
+
have hE : ∀ n, ∃ m, n ≤ m ∧ W h m ∧ E h m := fun n => by
|
|
234
|
+
obtain ⟨m, hm, hw⟩ := hio (max n T)
|
|
235
|
+
exact ⟨m, by omega, hw, henabled h hh m (hPm m (by omega)) (hnq m (by omega)) hw⟩
|
|
236
|
+
obtain ⟨m, hm, hW, hH⟩ := fair h hh hE (max B T)
|
|
237
|
+
rcases hhelp h hh m (hPm m (by omega)) (hnq m (by omega)) hW hH with hq | hl | ⟨h', hh', hlt, hw'⟩
|
|
238
|
+
· exact hnq _ (by omega) hq
|
|
239
|
+
· exact hlow m (by omega) hl
|
|
240
|
+
· exact hB h' (List.mem_filter.mpr ⟨hh', by simpa using hlt⟩) (m + 1) (by omega) hw'
|
|
241
|
+
|
|
242
|
+
/-! Primed names: a condition read after a step -/
|
|
243
|
+
|
|
244
|
+
-- A term with its state variables renamed to their primed names (`pr`: variable, name) and its maps to theirs (`pm`).
|
|
245
|
+
mutual
|
|
246
|
+
def primeE (pr pm : List (Nat × Nat)) : E → E
|
|
247
|
+
| .var i => match lookup (fun q => q.1 == i) pr with
|
|
248
|
+
| some q => .var q.2
|
|
249
|
+
| none => .var i
|
|
250
|
+
| .const n => .const n
|
|
251
|
+
| .add a b => .add (primeE pr pm a) (primeE pr pm b)
|
|
252
|
+
| .sub a b => .sub (primeE pr pm a) (primeE pr pm b)
|
|
253
|
+
| .mul k a => .mul k (primeE pr pm a)
|
|
254
|
+
| .min a b => .min (primeE pr pm a) (primeE pr pm b)
|
|
255
|
+
| .max a b => .max (primeE pr pm a) (primeE pr pm b)
|
|
256
|
+
| .prod a b => .prod (primeE pr pm a) (primeE pr pm b)
|
|
257
|
+
| .ite c t f => .ite (primeC pr pm c) (primeE pr pm t) (primeE pr pm f)
|
|
258
|
+
| .get m k => .get (match lookup (fun q => q.1 == m) pm with
|
|
259
|
+
| some q => q.2
|
|
260
|
+
| none => m) (primeE pr pm k)
|
|
261
|
+
| .tup a b => .tup (primeE pr pm a) (primeE pr pm b)
|
|
262
|
+
| .proj k a => .proj k (primeE pr pm a)
|
|
263
|
+
def primeC (pr pm : List (Nat × Nat)) : C → C
|
|
264
|
+
| .le a b => .le (primeE pr pm a) (primeE pr pm b)
|
|
265
|
+
| .lt a b => .lt (primeE pr pm a) (primeE pr pm b)
|
|
266
|
+
| .eq a b => .eq (primeE pr pm a) (primeE pr pm b)
|
|
267
|
+
| .and a b => .and (primeC pr pm a) (primeC pr pm b)
|
|
268
|
+
| .or a b => .or (primeC pr pm a) (primeC pr pm b)
|
|
269
|
+
| .not a => .not (primeC pr pm a)
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
-- Read where `then` reads (primed names after, the rest before), a renamed term is the term after, wherever a variable
|
|
273
|
+
-- or map it reads is renamed or the same before and after, and reads no primed name.
|
|
274
|
+
section prime
|
|
275
|
+
variable (pr pm : List (Nat × Nat)) (hpr : distinct (pr.map (·.2)) = true) (hpm : distinct (pm.map (·.2)) = true)
|
|
276
|
+
(ρ ρ' : Nat → Nat) (M M' : Nat → Nat → Nat)
|
|
277
|
+
include hpr hpm
|
|
278
|
+
mutual
|
|
279
|
+
theorem primeE_eval : (e : E) → (∀ i, occE i e = true → i ∉ pr.map (·.2) ∧ (i ∈ pr.map (·.1) ∨ ρ' i = ρ i)) →
|
|
280
|
+
(∀ mp, occME mp e = true → mp ∉ pm.map (·.2) ∧ (mp ∈ pm.map (·.1) ∨ M' mp = M mp)) →
|
|
281
|
+
evalE (primed pr ρ' ρ) (primedMaps pm M' M) (primeE pr pm e) = evalE ρ' M' e
|
|
282
|
+
| .var i, hv, _ => by
|
|
283
|
+
simp only [primeE]
|
|
284
|
+
cases hl : lookup (fun q => q.1 == i) pr with
|
|
285
|
+
| some q =>
|
|
286
|
+
have ⟨hq, hqi⟩ := lookup_some hl
|
|
287
|
+
simp only [beq_iff_eq] at hqi
|
|
288
|
+
simp only [evalE, primed, lookup_ren hpr q hq, hqi]
|
|
289
|
+
| none =>
|
|
290
|
+
have hi := hv i (by simp [occE])
|
|
291
|
+
have hnot : i ∉ pr.map (·.1) := fun hm => by
|
|
292
|
+
obtain ⟨q, hq, hqi⟩ := List.mem_map.mp hm
|
|
293
|
+
have := lookup_none_all hl q hq; simp [hqi] at this
|
|
294
|
+
have hn2 : lookup (fun q => q.2 == i) pr = none := lookup_none (fun q hq => by
|
|
295
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact hi.1 (by rw [← he]; exact List.mem_map_of_mem hq))
|
|
296
|
+
simp only [evalE, primed, hn2]
|
|
297
|
+
rcases hi.2 with h | h
|
|
298
|
+
· exact absurd h hnot
|
|
299
|
+
· exact h.symm
|
|
300
|
+
| .const n, _, _ => rfl
|
|
301
|
+
| .add a b, hv, hm => by
|
|
302
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
303
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
304
|
+
| .sub a b, hv, hm => by
|
|
305
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
306
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
307
|
+
| .mul k a, hv, hm => by
|
|
308
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
309
|
+
| .min a b, hv, hm => by
|
|
310
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
311
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
312
|
+
| .max a b, hv, hm => by
|
|
313
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
314
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
315
|
+
| .prod a b, hv, hm => by
|
|
316
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
317
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
318
|
+
| .ite c t f, hv, hm => by
|
|
319
|
+
simp only [primeE, evalE, primeC_eval c (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
320
|
+
primeE_eval t (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
321
|
+
primeE_eval f (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
322
|
+
| .get m k, hv, hm => by
|
|
323
|
+
have hk := primeE_eval k (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))
|
|
324
|
+
simp only [primeE, evalE, hk]
|
|
325
|
+
cases hl : lookup (fun q => q.1 == m) pm with
|
|
326
|
+
| some q =>
|
|
327
|
+
have ⟨hq, hqm⟩ := lookup_some hl
|
|
328
|
+
simp only [beq_iff_eq] at hqm
|
|
329
|
+
simp only [primedMaps, lookup_ren hpm q hq, hqm]
|
|
330
|
+
| none =>
|
|
331
|
+
have hmm := hm m (by simp [occME])
|
|
332
|
+
have hnot : m ∉ pm.map (·.1) := fun hmem => by
|
|
333
|
+
obtain ⟨q, hq, hqm⟩ := List.mem_map.mp hmem
|
|
334
|
+
have := lookup_none_all hl q hq; simp [hqm] at this
|
|
335
|
+
have hn2 : lookup (fun q => q.2 == m) pm = none := lookup_none (fun q hq => by
|
|
336
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact hmm.1 (by rw [← he]; exact List.mem_map_of_mem hq))
|
|
337
|
+
simp only [primedMaps, hn2]
|
|
338
|
+
rcases hmm.2 with h | h
|
|
339
|
+
· exact absurd h hnot
|
|
340
|
+
· rw [h]
|
|
341
|
+
| .tup a b, hv, hm => by
|
|
342
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h])),
|
|
343
|
+
primeE_eval b (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
344
|
+
| .proj k a, hv, hm => by
|
|
345
|
+
simp only [primeE, evalE, primeE_eval a (fun i hi => hv i (by simp [occE, hi])) (fun mp h => hm mp (by simp [occME, h]))]
|
|
346
|
+
theorem primeC_eval : (c : C) → (∀ i, occC i c = true → i ∉ pr.map (·.2) ∧ (i ∈ pr.map (·.1) ∨ ρ' i = ρ i)) →
|
|
347
|
+
(∀ mp, occMC mp c = true → mp ∉ pm.map (·.2) ∧ (mp ∈ pm.map (·.1) ∨ M' mp = M mp)) →
|
|
348
|
+
evalC (primed pr ρ' ρ) (primedMaps pm M' M) (primeC pr pm c) = evalC ρ' M' c
|
|
349
|
+
| .le a b, hv, hm => by
|
|
350
|
+
simp only [primeC, evalC, primeE_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h])),
|
|
351
|
+
primeE_eval b (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
352
|
+
| .lt a b, hv, hm => by
|
|
353
|
+
simp only [primeC, evalC, primeE_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h])),
|
|
354
|
+
primeE_eval b (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
355
|
+
| .eq a b, hv, hm => by
|
|
356
|
+
simp only [primeC, evalC, primeE_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h])),
|
|
357
|
+
primeE_eval b (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
358
|
+
| .and a b, hv, hm => by
|
|
359
|
+
simp only [primeC, evalC, primeC_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h])),
|
|
360
|
+
primeC_eval b (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
361
|
+
| .or a b, hv, hm => by
|
|
362
|
+
simp only [primeC, evalC, primeC_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h])),
|
|
363
|
+
primeC_eval b (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
364
|
+
| .not a, hv, hm => by
|
|
365
|
+
simp only [primeC, evalC, primeC_eval a (fun i hi => hv i (by simp [occC, hi])) (fun mp h => hm mp (by simp [occMC, h]))]
|
|
366
|
+
end
|
|
367
|
+
end prime
|
|
368
|
+
|
|
369
|
+
-- Read where `then` reads, a condition that reads no primed name is the condition before the step.
|
|
370
|
+
theorem unprimedC_eval {pr pm : List (Nat × Nat)} {c : C} (hv : ∀ i, occC i c = true → i ∉ pr.map (·.2)) (hm : ∀ mp, occMC mp c = true → mp ∉ pm.map (·.2))
|
|
371
|
+
(ρ ρ' : Nat → Nat) (M M' : Nat → Nat → Nat) : evalC (primed pr ρ' ρ) (primedMaps pm M' M) c = evalC ρ M c := by
|
|
372
|
+
rw [congrC _ ρ _ c (fun i hi => by
|
|
373
|
+
simp only [primed]
|
|
374
|
+
rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact hv i hi (by rw [← he]; exact List.mem_map_of_mem hq))])]
|
|
375
|
+
exact congrMC ρ _ M c (fun mp hmp => by
|
|
376
|
+
funext k; simp only [primedMaps]
|
|
377
|
+
rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact hm mp hmp (by rw [← he]; exact List.mem_map_of_mem hq))])
|
|
378
|
+
|
|
379
|
+
theorem unprimed_eval {pr pm : List (Nat × Nat)} {e : E} (hv : ∀ i, occE i e = true → i ∉ pr.map (·.2)) (hm : ∀ mp, occME mp e = true → mp ∉ pm.map (·.2))
|
|
380
|
+
(ρ ρ' : Nat → Nat) (M M' : Nat → Nat → Nat) : evalE (primed pr ρ' ρ) (primedMaps pm M' M) e = evalE ρ M e := by
|
|
381
|
+
rw [congrE _ ρ _ e (fun i hi => by
|
|
382
|
+
simp only [primed]
|
|
383
|
+
rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact hv i hi (by rw [← he]; exact List.mem_map_of_mem hq))])]
|
|
384
|
+
exact congrME ρ _ M e (fun mp hmp => by
|
|
385
|
+
funext k; simp only [primedMaps]
|
|
386
|
+
rw [lookup_none (fun q hq => by simp only [beq_eq_false_iff_ne]; intro he; exact hm mp hmp (by rw [← he]; exact List.mem_map_of_mem hq))])
|
|
387
|
+
|
|
388
|
+
-- A condition (a term) read across a step: primed, it is read after the step; plain, before it, whether in `then`'s
|
|
389
|
+
-- reading or in `when`'s; given that what it reads is renamed or unchanged by the step, and unchanged by the request.
|
|
390
|
+
theorem view_C {pr pm : List (Nat × Nat)} (hpr : distinct (pr.map (·.2)) = true) (hpm : distinct (pm.map (·.2)) = true)
|
|
391
|
+
{c : C} {ρ X ρ' : Nat → Nat} {M N M' : Nat → Nat → Nat}
|
|
392
|
+
(hv : ∀ i, occC i c = true → i ∉ pr.map (·.2) ∧ (i ∈ pr.map (·.1) ∨ ρ' i = X i))
|
|
393
|
+
(hm : ∀ mp, occMC mp c = true → mp ∉ pm.map (·.2) ∧ (mp ∈ pm.map (·.1) ∨ M' mp = N mp))
|
|
394
|
+
(hx : ∀ i, occC i c = true → X i = ρ i) (hn : ∀ mp, occMC mp c = true → N mp = M mp) :
|
|
395
|
+
evalC (primed pr ρ' X) (primedMaps pm M' N) (primeC pr pm c) = evalC ρ' M' c
|
|
396
|
+
∧ evalC (primed pr ρ' X) (primedMaps pm M' N) c = evalC ρ M c ∧ evalC X N c = evalC ρ M c := by
|
|
397
|
+
have h3 : evalC X N c = evalC ρ M c := by rw [congrMC X N M c hn]; exact congrC X ρ M c hx
|
|
398
|
+
refine ⟨primeC_eval pr pm hpr hpm X ρ' N M' c hv hm, ?_, h3⟩
|
|
399
|
+
rw [unprimedC_eval (fun i hi => (hv i hi).1) (fun mp h => (hm mp h).1), h3]
|
|
400
|
+
|
|
401
|
+
theorem view_E {pr pm : List (Nat × Nat)} (hpr : distinct (pr.map (·.2)) = true) (hpm : distinct (pm.map (·.2)) = true)
|
|
402
|
+
{e : E} {ρ X ρ' : Nat → Nat} {M N M' : Nat → Nat → Nat}
|
|
403
|
+
(hv : ∀ i, occC i (.eq e e) = true → i ∉ pr.map (·.2) ∧ (i ∈ pr.map (·.1) ∨ ρ' i = X i))
|
|
404
|
+
(hm : ∀ mp, occMC mp (.eq e e) = true → mp ∉ pm.map (·.2) ∧ (mp ∈ pm.map (·.1) ∨ M' mp = N mp))
|
|
405
|
+
(hx : ∀ i, occC i (.eq e e) = true → X i = ρ i) (hn : ∀ mp, occMC mp (.eq e e) = true → N mp = M mp) :
|
|
406
|
+
evalE (primed pr ρ' X) (primedMaps pm M' N) (primeE pr pm e) = evalE ρ' M' e
|
|
407
|
+
∧ evalE (primed pr ρ' X) (primedMaps pm M' N) e = evalE ρ M e := by
|
|
408
|
+
have h3 : evalE X N e = evalE ρ M e := by
|
|
409
|
+
rw [congrME X N M e (fun mp h => hn mp (by simp [occMC, h]))]; exact congrE X ρ M e (fun i h => hx i (by simp [occC, h]))
|
|
410
|
+
refine ⟨primeE_eval pr pm hpr hpm X ρ' N M' e (fun i h => hv i (by simp [occC, h])) (fun mp h => hm mp (by simp [occMC, h])), ?_⟩
|
|
411
|
+
rw [unprimed_eval (fun i h => (hv i (by simp [occC, h])).1) (fun mp h => (hm mp (by simp [occMC, h])).1), h3]
|
|
412
|
+
|
|
413
|
+
/-! The conditions Lean builds -/
|
|
414
|
+
|
|
415
|
+
def noneC : C := .lt (.const 0) (.const 0)
|
|
416
|
+
-- The rank after (as') below the rank before (as), in order; the two equal; each region, if it held, held after; some
|
|
417
|
+
-- region; the request's inputs as its terms.
|
|
418
|
+
def rankLtC : List E → List E → C
|
|
419
|
+
| a' :: as', a :: as => .or (.lt a' a) (.and (.eq a' a) (rankLtC as' as))
|
|
420
|
+
| _, _ => noneC
|
|
421
|
+
def eqsC : List E → List E → C
|
|
422
|
+
| a' :: as', a :: as => .and (.eq a' a) (eqsC as' as)
|
|
423
|
+
| _, _ => trueC
|
|
424
|
+
def keepC (pr pm : List (Nat × Nat)) : List C → C
|
|
425
|
+
| [] => trueC
|
|
426
|
+
| w :: ws => .and (.or (.not w) (primeC pr pm w)) (keepC pr pm ws)
|
|
427
|
+
def anyC : List C → C
|
|
428
|
+
| [] => noneC
|
|
429
|
+
| w :: ws => .or w (anyC ws)
|
|
430
|
+
def reqC : List (Nat × E) → C
|
|
431
|
+
| [] => trueC
|
|
432
|
+
| p :: ps => .and (.eq (.var p.1) p.2) (reqC ps)
|
|
433
|
+
|
|
434
|
+
theorem rankLtC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ as' as : List E,
|
|
435
|
+
evalC ρ M (rankLtC as' as) = lexB (as'.map (evalE ρ M)) (as.map (evalE ρ M))
|
|
436
|
+
| a' :: as', a :: as => by simp only [rankLtC, lexB, evalC, List.map_cons, rankLtC_eval ρ M as' as]
|
|
437
|
+
| [], _ => by simp [rankLtC, lexB, evalC, noneC]
|
|
438
|
+
| _ :: _, [] => by simp [rankLtC, lexB, evalC, noneC]
|
|
439
|
+
|
|
440
|
+
theorem eqsC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ as' as : List E, as'.length = as.length →
|
|
441
|
+
(evalC ρ M (eqsC as' as) = true ↔ as'.map (evalE ρ M) = as.map (evalE ρ M))
|
|
442
|
+
| a' :: as', a :: as, h => by
|
|
443
|
+
simp only [eqsC, evalC, Bool.and_eq_true, decide_eq_true_eq, List.map_cons, List.cons.injEq, eqsC_eval ρ M as' as (by simpa using h)]
|
|
444
|
+
| [], [], _ => by simp [eqsC, evalC, trueC]
|
|
445
|
+
| [], _ :: _, h => by simp at h
|
|
446
|
+
| _ :: _, [], h => by simp at h
|
|
447
|
+
|
|
448
|
+
theorem keepC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) (pr pm : List (Nat × Nat)) : ∀ ws : List C,
|
|
449
|
+
evalC ρ M (keepC pr pm ws) = true ↔ ∀ w ∈ ws, evalC ρ M w = true → evalC ρ M (primeC pr pm w) = true
|
|
450
|
+
| [] => by simp [keepC, evalC, trueC]
|
|
451
|
+
| w :: ws => by
|
|
452
|
+
rw [List.forall_mem_cons, ← keepC_eval ρ M pr pm ws]
|
|
453
|
+
simp only [keepC, evalC, Bool.and_eq_true, Bool.or_eq_true, Bool.not_eq_true']
|
|
454
|
+
cases evalC ρ M w <;> simp
|
|
455
|
+
|
|
456
|
+
theorem anyC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ ws : List C, evalC ρ M (anyC ws) = true ↔ ∃ w ∈ ws, evalC ρ M w = true
|
|
457
|
+
| [] => by simp [anyC, evalC, noneC]
|
|
458
|
+
| w :: ws => by simp only [anyC, evalC, Bool.or_eq_true, anyC_eval ρ M ws, List.mem_cons, exists_eq_or_imp]
|
|
459
|
+
|
|
460
|
+
theorem reqC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ ps : List (Nat × E), evalC ρ M (reqC ps) = true ↔ ∀ p ∈ ps, ρ p.1 = evalE ρ M p.2
|
|
461
|
+
| [] => by simp [reqC, evalC, trueC]
|
|
462
|
+
| p :: ps => by
|
|
463
|
+
rw [List.forall_mem_cons, ← reqC_eval ρ M ps]
|
|
464
|
+
simp only [reqC, evalC, evalE, Bool.and_eq_true]
|
|
465
|
+
exact ⟨fun h => ⟨of_decide_eq_true h.1, h.2⟩, fun h => ⟨decide_eq_true h.1, h.2⟩⟩
|
|
466
|
+
|
|
467
|
+
theorem zip_cover {α β : Type} : ∀ (as : List α) (bs : List β), as.length = bs.length → ∀ a ∈ as, ∃ b, (a, b) ∈ as.zip bs
|
|
468
|
+
| [], [], _, a, ha => by simp at ha
|
|
469
|
+
| a :: as, b :: bs, h, x, hx => by
|
|
470
|
+
rcases List.mem_cons.mp hx with rfl | hx
|
|
471
|
+
· exact ⟨b, by simp⟩
|
|
472
|
+
· obtain ⟨b', hb'⟩ := zip_cover as bs (by simpa using h) x hx
|
|
473
|
+
exact ⟨b', by simp [hb']⟩
|
|
474
|
+
| [], _ :: _, h, _, _ => by simp at h
|
|
475
|
+
| _ :: _, [], h, _, _ => by simp at h
|
|
476
|
+
|
|
477
|
+
/-! Liveness -/
|
|
478
|
+
|
|
479
|
+
structure Live where
|
|
480
|
+
from_ : C
|
|
481
|
+
to : C
|
|
482
|
+
rank : List E -- compared in order
|
|
483
|
+
helps : List (C × List (Nat × E)) -- (the region where it helps, the request: an input and its term over the state and params)
|
|
484
|
+
prime : List (Nat × Nat) -- a state variable, and its primed name
|
|
485
|
+
primeMaps : List (Nat × Nat) -- a map, and its primed name
|
|
486
|
+
|
|
487
|
+
def Live.pend (l : Live) : C := .and l.from_ (.not l.to)
|
|
488
|
+
def liveStayC (l : Live) : C :=
|
|
489
|
+
.and (.or (primeC l.prime l.primeMaps l.from_) (primeC l.prime l.primeMaps l.to))
|
|
490
|
+
(.or (primeC l.prime l.primeMaps l.to) (.or (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
491
|
+
(.and (eqsC (l.rank.map (primeE l.prime l.primeMaps)) l.rank) (keepC l.prime l.primeMaps (l.helps.map (·.1))))))
|
|
492
|
+
def liveCoverC (l : Live) : C := anyC (l.helps.map (·.1))
|
|
493
|
+
def liveHelpWhen (l : Live) (h : C × List (Nat × E)) : C := .and l.pend (.and h.1 (reqC h.2))
|
|
494
|
+
def liveHelpThen (l : Live) : C := .or (primeC l.prime l.primeMaps l.to) (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
495
|
+
-- Under strong fairness: a step keeps `from` or reaches `to`, and reaches `to`, lowers the rank or keeps it (the regions
|
|
496
|
+
-- need not stay); each helpful request, at its level, reaches `to`, lowers the rank, or reaches the region of a helpful
|
|
497
|
+
-- request of a higher level (`lv`: a level per helpful request, in order).
|
|
498
|
+
def liveStaySC (l : Live) : C :=
|
|
499
|
+
.and (.or (primeC l.prime l.primeMaps l.from_) (primeC l.prime l.primeMaps l.to))
|
|
500
|
+
(.or (primeC l.prime l.primeMaps l.to) (.or (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
501
|
+
(eqsC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)))
|
|
502
|
+
def liveHelpThenS (l : Live) (lv : List Nat) (li : Nat) : C :=
|
|
503
|
+
.or (primeC l.prime l.primeMaps l.to) (.or (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
504
|
+
(anyC (((l.helps.zip lv).filter (fun x => decide (li < x.2))).map (fun x => primeC l.prime l.primeMaps x.1.1))))
|
|
505
|
+
-- What reads the state alone: `from`, `to`, each part of the rank and each region.
|
|
506
|
+
def liveConds (l : Live) : List C := [l.from_, l.to] ++ l.rank.map (fun e => .eq e e) ++ l.helps.map (·.1)
|
|
507
|
+
|
|
508
|
+
theorem mem_conds_rank {l : Live} {e : E} (he : e ∈ l.rank) : C.eq e e ∈ liveConds l := by
|
|
509
|
+
simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inl (Or.inr ⟨e, he, rfl⟩)
|
|
510
|
+
|
|
511
|
+
/-- The argument, given how each step reads: `B`, `BM` the state and maps `when` reads (with the request), `A`, `AM`
|
|
512
|
+
those `then` reads (primed names after the step). -/
|
|
513
|
+
theorem leads_view (l : Live) (st : Nat → (Nat → Nat) × (Nat → Nat → Nat)) (B A : Nat → Nat → Nat) (BM AM : Nat → Nat → Nat → Nat)
|
|
514
|
+
(H En : C × List (Nat × E) → Nat → Prop)
|
|
515
|
+
(hC : ∀ n, ∀ c ∈ liveConds l, evalC (A n) (AM n) (primeC l.prime l.primeMaps c) = evalC (st (n + 1)).1 (st (n + 1)).2 c
|
|
516
|
+
∧ evalC (A n) (AM n) c = evalC (st n).1 (st n).2 c ∧ evalC (B n) (BM n) c = evalC (st n).1 (st n).2 c)
|
|
517
|
+
(hE : ∀ n, ∀ e ∈ l.rank, evalE (A n) (AM n) (primeE l.prime l.primeMaps e) = evalE (st (n + 1)).1 (st (n + 1)).2 e
|
|
518
|
+
∧ evalE (A n) (AM n) e = evalE (st n).1 (st n).2 e)
|
|
519
|
+
(hstay : ∀ n, evalC (B n) (BM n) l.pend = true → evalC (A n) (AM n) (liveStayC l) = true)
|
|
520
|
+
(hcover : ∀ n, evalC (B n) (BM n) l.pend = true → evalC (A n) (AM n) (liveCoverC l) = true)
|
|
521
|
+
(hhelp : ∀ h ∈ l.helps, ∀ n, evalC (B n) (BM n) (liveHelpWhen l h) = true → evalC (A n) (AM n) (liveHelpThen l) = true)
|
|
522
|
+
(hreq : ∀ h ∈ l.helps, ∀ n, H h n → evalC (B n) (BM n) (reqC h.2) = true)
|
|
523
|
+
(henabled : ∀ h ∈ l.helps, ∀ n, evalC (st n).1 (st n).2 l.from_ = true → evalC (st n).1 (st n).2 l.to = false →
|
|
524
|
+
evalC (st n).1 (st n).2 h.1 = true → En h n)
|
|
525
|
+
(fair : ∀ h ∈ l.helps, ∀ n, (∀ m, n ≤ m → En h m) → ∃ m, n ≤ m ∧ H h m) :
|
|
526
|
+
∀ n, evalC (st n).1 (st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (st m).1 (st m).2 l.to = true := by
|
|
527
|
+
have memF : l.from_ ∈ liveConds l := by simp [liveConds]
|
|
528
|
+
have memT : l.to ∈ liveConds l := by simp [liveConds]
|
|
529
|
+
have memH : ∀ h ∈ l.helps, h.1 ∈ liveConds l := fun h hh => by
|
|
530
|
+
simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inr ⟨h, hh, rfl⟩
|
|
531
|
+
have h1 : ∀ n, (l.rank.map (primeE l.prime l.primeMaps)).map (evalE (A n) (AM n)) = l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2) := by
|
|
532
|
+
intro n; rw [List.map_map]; exact List.map_congr_left (fun e he => (hE n e he).1)
|
|
533
|
+
have h2 : ∀ n, l.rank.map (evalE (A n) (AM n)) = l.rank.map (evalE (st n).1 (st n).2) := fun n =>
|
|
534
|
+
List.map_congr_left (fun e he => (hE n e he).2)
|
|
535
|
+
have hlex : ∀ n, evalC (A n) (AM n) (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
536
|
+
= lexB (l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2)) (l.rank.map (evalE (st n).1 (st n).2)) := by
|
|
537
|
+
intro n; rw [rankLtC_eval, h1, h2]
|
|
538
|
+
have heqs : ∀ n, evalC (A n) (AM n) (eqsC (l.rank.map (primeE l.prime l.primeMaps)) l.rank) = true
|
|
539
|
+
↔ l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2) = l.rank.map (evalE (st n).1 (st n).2) := by
|
|
540
|
+
intro n; rw [eqsC_eval _ _ _ _ (by simp), h1, h2]
|
|
541
|
+
have hpend : ∀ n, evalC (st n).1 (st n).2 l.from_ = true → ¬ evalC (st n).1 (st n).2 l.to = true → evalC (B n) (BM n) l.pend = true := by
|
|
542
|
+
intro n hP hnQ
|
|
543
|
+
simp only [Live.pend, evalC, Bool.and_eq_true, Bool.not_eq_true']
|
|
544
|
+
rw [(hC n _ memF).2.2, (hC n _ memT).2.2]; exact ⟨hP, by simpa using hnQ⟩
|
|
545
|
+
refine leads_of_lex (fun n => evalC (st n).1 (st n).2 l.from_ = true) (fun n => evalC (st n).1 (st n).2 l.to = true)
|
|
546
|
+
(fun n => l.rank.map (evalE (st n).1 (st n).2)) l.rank.length (fun n => by simp) l.helps
|
|
547
|
+
(fun h n => evalC (st n).1 (st n).2 h.1 = true) H En ?_ ?_ ?_ (fun h hh n hP hnQ hW => henabled h hh n hP (by simpa using hnQ) hW) fair
|
|
548
|
+
· intro n hP hnQ
|
|
549
|
+
have h := hstay n (hpend n hP hnQ)
|
|
550
|
+
simp only [liveStayC, evalC, Bool.and_eq_true, Bool.or_eq_true] at h
|
|
551
|
+
rw [(hC n _ memF).1, (hC n _ memT).1, hlex n, heqs n, keepC_eval] at h
|
|
552
|
+
refine ⟨h.1, ?_⟩
|
|
553
|
+
rcases h.2 with h2 | h2 | ⟨h2, h3⟩
|
|
554
|
+
· exact Or.inl h2
|
|
555
|
+
· exact Or.inr (Or.inl h2)
|
|
556
|
+
· refine Or.inr (Or.inr ⟨h2, fun hp hhp hW => ?_⟩)
|
|
557
|
+
have := h3 hp.1 (List.mem_map_of_mem hhp) (by rw [(hC n _ (memH hp hhp)).2.1]; exact hW)
|
|
558
|
+
rwa [(hC n _ (memH hp hhp)).1] at this
|
|
559
|
+
· intro n hP hnQ
|
|
560
|
+
have h := hcover n (hpend n hP hnQ)
|
|
561
|
+
rw [liveCoverC, anyC_eval] at h
|
|
562
|
+
obtain ⟨w, hw, hW⟩ := h
|
|
563
|
+
obtain ⟨hp, hhp, rfl⟩ := List.mem_map.mp hw
|
|
564
|
+
exact ⟨hp, hhp, by rw [← (hC n _ (memH hp hhp)).2.1]; exact hW⟩
|
|
565
|
+
· intro hp hhp n hP hnQ hW hH
|
|
566
|
+
have hB : evalC (B n) (BM n) (liveHelpWhen l hp) = true := by
|
|
567
|
+
have := hpend n hP hnQ
|
|
568
|
+
simp only [liveHelpWhen, evalC, Bool.and_eq_true]
|
|
569
|
+
rw [(hC n _ (memH hp hhp)).2.2]
|
|
570
|
+
exact ⟨this, hW, hreq hp hhp n hH⟩
|
|
571
|
+
have h := hhelp hp hhp n hB
|
|
572
|
+
simp only [liveHelpThen, evalC, Bool.or_eq_true] at h
|
|
573
|
+
rwa [(hC n _ memT).1, hlex n] at h
|
|
574
|
+
|
|
575
|
+
/-- `leads_view` under strong fairness: the regions need not stay, and a helpful request may hand over to one of a
|
|
576
|
+
higher level. -/
|
|
577
|
+
theorem leads_view_sf (l : Live) (lv : List Nat) (hlv : l.helps.length = lv.length) (st : Nat → (Nat → Nat) × (Nat → Nat → Nat))
|
|
578
|
+
(B A : Nat → Nat → Nat) (BM AM : Nat → Nat → Nat → Nat) (H En : C × List (Nat × E) → Nat → Prop)
|
|
579
|
+
(hC : ∀ n, ∀ c ∈ liveConds l, evalC (A n) (AM n) (primeC l.prime l.primeMaps c) = evalC (st (n + 1)).1 (st (n + 1)).2 c
|
|
580
|
+
∧ evalC (A n) (AM n) c = evalC (st n).1 (st n).2 c ∧ evalC (B n) (BM n) c = evalC (st n).1 (st n).2 c)
|
|
581
|
+
(hE : ∀ n, ∀ e ∈ l.rank, evalE (A n) (AM n) (primeE l.prime l.primeMaps e) = evalE (st (n + 1)).1 (st (n + 1)).2 e
|
|
582
|
+
∧ evalE (A n) (AM n) e = evalE (st n).1 (st n).2 e)
|
|
583
|
+
(hstay : ∀ n, evalC (B n) (BM n) l.pend = true → evalC (A n) (AM n) (liveStaySC l) = true)
|
|
584
|
+
(hcover : ∀ n, evalC (B n) (BM n) l.pend = true → evalC (A n) (AM n) (liveCoverC l) = true)
|
|
585
|
+
(hhelp : ∀ x ∈ l.helps.zip lv, ∀ n, evalC (B n) (BM n) (liveHelpWhen l x.1) = true → evalC (A n) (AM n) (liveHelpThenS l lv x.2) = true)
|
|
586
|
+
(hreq : ∀ h ∈ l.helps, ∀ n, H h n → evalC (B n) (BM n) (reqC h.2) = true)
|
|
587
|
+
(henabled : ∀ h ∈ l.helps, ∀ n, evalC (st n).1 (st n).2 l.from_ = true → evalC (st n).1 (st n).2 l.to = false →
|
|
588
|
+
evalC (st n).1 (st n).2 h.1 = true → En h n)
|
|
589
|
+
(fair : ∀ h ∈ l.helps, (∀ n, ∃ m, n ≤ m ∧ evalC (st m).1 (st m).2 h.1 = true ∧ En h m) →
|
|
590
|
+
∀ n, ∃ m, n ≤ m ∧ evalC (st m).1 (st m).2 h.1 = true ∧ H h m) :
|
|
591
|
+
∀ n, evalC (st n).1 (st n).2 l.from_ = true → ∃ m, n ≤ m ∧ evalC (st m).1 (st m).2 l.to = true := by
|
|
592
|
+
have memF : l.from_ ∈ liveConds l := by simp [liveConds]
|
|
593
|
+
have memT : l.to ∈ liveConds l := by simp [liveConds]
|
|
594
|
+
have memH : ∀ h ∈ l.helps, h.1 ∈ liveConds l := fun h hh => by
|
|
595
|
+
simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inr ⟨h, hh, rfl⟩
|
|
596
|
+
have memZ : ∀ x ∈ l.helps.zip lv, x.1 ∈ l.helps := fun x hx => (List.of_mem_zip hx).1
|
|
597
|
+
have h1 : ∀ n, (l.rank.map (primeE l.prime l.primeMaps)).map (evalE (A n) (AM n)) = l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2) := by
|
|
598
|
+
intro n; rw [List.map_map]; exact List.map_congr_left (fun e he => (hE n e he).1)
|
|
599
|
+
have h2 : ∀ n, l.rank.map (evalE (A n) (AM n)) = l.rank.map (evalE (st n).1 (st n).2) := fun n =>
|
|
600
|
+
List.map_congr_left (fun e he => (hE n e he).2)
|
|
601
|
+
have hlex : ∀ n, evalC (A n) (AM n) (rankLtC (l.rank.map (primeE l.prime l.primeMaps)) l.rank)
|
|
602
|
+
= lexB (l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2)) (l.rank.map (evalE (st n).1 (st n).2)) := by
|
|
603
|
+
intro n; rw [rankLtC_eval, h1, h2]
|
|
604
|
+
have heqs : ∀ n, evalC (A n) (AM n) (eqsC (l.rank.map (primeE l.prime l.primeMaps)) l.rank) = true
|
|
605
|
+
↔ l.rank.map (evalE (st (n + 1)).1 (st (n + 1)).2) = l.rank.map (evalE (st n).1 (st n).2) := by
|
|
606
|
+
intro n; rw [eqsC_eval _ _ _ _ (by simp), h1, h2]
|
|
607
|
+
have hpend : ∀ n, evalC (st n).1 (st n).2 l.from_ = true → ¬ evalC (st n).1 (st n).2 l.to = true → evalC (B n) (BM n) l.pend = true := by
|
|
608
|
+
intro n hP hnQ
|
|
609
|
+
simp only [Live.pend, evalC, Bool.and_eq_true, Bool.not_eq_true']
|
|
610
|
+
rw [(hC n _ memF).2.2, (hC n _ memT).2.2]; exact ⟨hP, by simpa using hnQ⟩
|
|
611
|
+
refine leads_of_lex_sf (fun n => evalC (st n).1 (st n).2 l.from_ = true) (fun n => evalC (st n).1 (st n).2 l.to = true)
|
|
612
|
+
(fun n => l.rank.map (evalE (st n).1 (st n).2)) l.rank.length (fun n => by simp) (l.helps.zip lv) (fun x => x.2)
|
|
613
|
+
(fun x n => evalC (st n).1 (st n).2 x.1.1 = true) (fun x n => H x.1 n) (fun x n => En x.1 n) ?_ ?_ ?_
|
|
614
|
+
(fun x hx n hP hnQ hW => henabled x.1 (memZ x hx) n hP (by simpa using hnQ) hW) (fun x hx => fair x.1 (memZ x hx))
|
|
615
|
+
· intro n hP hnQ
|
|
616
|
+
have h := hstay n (hpend n hP hnQ)
|
|
617
|
+
simp only [liveStaySC, evalC, Bool.and_eq_true, Bool.or_eq_true] at h
|
|
618
|
+
rw [(hC n _ memF).1, (hC n _ memT).1, hlex n, heqs n] at h
|
|
619
|
+
exact ⟨h.1, h.2⟩
|
|
620
|
+
· intro n hP hnQ
|
|
621
|
+
have h := hcover n (hpend n hP hnQ)
|
|
622
|
+
rw [liveCoverC, anyC_eval] at h
|
|
623
|
+
obtain ⟨w, hw, hW⟩ := h
|
|
624
|
+
obtain ⟨hp, hhp, rfl⟩ := List.mem_map.mp hw
|
|
625
|
+
obtain ⟨b, hb⟩ := zip_cover l.helps lv hlv hp hhp
|
|
626
|
+
exact ⟨(hp, b), hb, by rw [← (hC n _ (memH hp hhp)).2.1]; exact hW⟩
|
|
627
|
+
· intro x hx n hP hnQ hW hH
|
|
628
|
+
have hB : evalC (B n) (BM n) (liveHelpWhen l x.1) = true := by
|
|
629
|
+
have := hpend n hP hnQ
|
|
630
|
+
simp only [liveHelpWhen, evalC, Bool.and_eq_true]
|
|
631
|
+
rw [(hC n _ (memH x.1 (memZ x hx))).2.2]
|
|
632
|
+
exact ⟨this, hW, hreq x.1 (memZ x hx) n hH⟩
|
|
633
|
+
have h := hhelp x hx n hB
|
|
634
|
+
simp only [liveHelpThenS, evalC, Bool.or_eq_true] at h
|
|
635
|
+
rw [(hC n _ memT).1, hlex n, anyC_eval] at h
|
|
636
|
+
rcases h with h | h | ⟨c, hc, hcv⟩
|
|
637
|
+
· exact Or.inl h
|
|
638
|
+
· exact Or.inr (Or.inl h)
|
|
639
|
+
· obtain ⟨y, hy, rfl⟩ := List.mem_map.mp hc
|
|
640
|
+
obtain ⟨hyz, hyl⟩ := List.mem_filter.mp hy
|
|
641
|
+
refine Or.inr (Or.inr ⟨y, hyz, by simpa using hyl, ?_⟩)
|
|
642
|
+
rw [← (hC n _ (memH y.1 (memZ y hyz))).1]; exact hcv
|
|
643
|
+
|
|
644
|
+
theorem assoc_distinct : ∀ {σ : List (Nat × E)}, distinct (σ.map (·.1)) = true → ∀ {p}, p ∈ σ → assoc σ p.1 = some p.2
|
|
645
|
+
| [], _, _, hp => by simp at hp
|
|
646
|
+
| a :: as, hd, p, hp => by
|
|
647
|
+
simp only [List.map_cons, distinct, Bool.and_eq_true, Bool.not_eq_true'] at hd
|
|
648
|
+
simp only [assoc, lookup]
|
|
649
|
+
rcases List.mem_cons.mp hp with rfl | hp
|
|
650
|
+
· simp
|
|
651
|
+
· have hne : (a.1 == p.1) = false := by
|
|
652
|
+
simp only [beq_eq_false_iff_ne]; intro he
|
|
653
|
+
have h1 : (as.map (·.1)).contains a.1 = true := List.contains_iff_mem.mpr (by rw [he]; exact List.mem_map_of_mem hp)
|
|
654
|
+
rw [hd.1] at h1; cases h1
|
|
655
|
+
simp only [hne, Bool.false_eq_true, if_false]
|
|
656
|
+
have := assoc_distinct hd.2 hp
|
|
657
|
+
simpa [assoc] using this
|
|
658
|
+
|
|
659
|
+
/-! Machines with sets -/
|
|
660
|
+
|
|
661
|
+
-- As for maps and flags, with the request's extended variables (inputs, quantified flags, witnesses) and the maps it
|
|
662
|
+
-- carries read by no part of the liveness, and maps written at every key unprimed counted as written.
|
|
663
|
+
def sliveVars (s : SMachine) (l : Live) : List Nat :=
|
|
664
|
+
l.prime.map (·.2) ++ s.extVars ++ (flagVars s.f.flags ++ s.f.m.step.map (·.1)).filter (fun i => !(l.prime.map (·.1)).contains i)
|
|
665
|
+
def sliveMaps (s : SMachine) (l : Live) : List Nat :=
|
|
666
|
+
l.primeMaps.map (·.2) ++ s.carried ++ (s.f.m.writes.map (·.1) ++ s.every.map (·.1)).filter (fun mp => !(l.primeMaps.map (·.1)).contains mp)
|
|
667
|
+
def sliveOk (s : SMachine) (l : Live) : Bool :=
|
|
668
|
+
distinct (l.prime.map (·.2)) && distinct (l.primeMaps.map (·.2))
|
|
669
|
+
&& (liveConds l).all (fun c => avoids (sliveVars s l) c && avoidsMaps (sliveMaps s l) c)
|
|
670
|
+
&& l.helps.all (fun h => h.2.all (fun p => s.f.m.inputs.contains p.1 && avoids s.extVars (.eq p.2 p.2) && avoidsMaps s.carried (.eq p.2 p.2)))
|
|
671
|
+
|
|
672
|
+
-- A step law over sets's proof: (law instances, quantified flag instances, fact instances, several-key instances, tree).
|
|
673
|
+
-- A proof's instances (laws, quantified flags, facts, laws about several keys, facts about several keys) and its certificate.
|
|
674
|
+
abbrev SProof := List (Nat × E) × List (Nat × List E) × List (Nat × E) × List (Nat × E × List E) × List (Nat × E × List E) × Cert
|
|
675
|
+
def sliveStep (l : Live) (w t : C) (p : SProof) : SKStep :=
|
|
676
|
+
⟨⟨[w], t, [[]], l.prime, l.primeMaps, p.1, (p.2.2.2.2.2.1, p.2.2.2.2.2.2.2)⟩, [[]], p.2.1, p.2.2.1, p.2.2.2.1, p.2.2.2.2.1, p.2.2.2.2.2.2.1, []⟩
|
|
677
|
+
def sliveHelpsOk (s : SMachine) (ps : List SEntry) (l : Live) (hp : List SProof) : Bool :=
|
|
678
|
+
l.helps.length == hp.length && (l.helps.zip hp).all (fun x => checkSKStep s ps (sliveStep l (liveHelpWhen l x.1) (liveHelpThen l) x.2))
|
|
679
|
+
|
|
680
|
+
-- A run of a machine with sets: each step a request (inputs, the maps it carries) the machine allows.
|
|
681
|
+
structure SRun (s : SMachine) where
|
|
682
|
+
st : Nat → (Nat → Nat) × (Nat → Nat → Nat)
|
|
683
|
+
req : Nat → SRequest
|
|
684
|
+
start : SReach s (st 0).1 (st 0).2
|
|
685
|
+
allowed : ∀ n, evalC (sView s (st n).1 (st n).2 (req n).1 (req n).2).1 (sView s (st n).1 (st n).2 (req n).1 (req n).2).2 s.f.m.assume = true
|
|
686
|
+
next : ∀ n, st (n + 1) = sNext s (st n).1 (st n).2 (req n).1 (req n).2
|
|
687
|
+
|
|
688
|
+
theorem srun_reach {s : SMachine} (run : SRun s) : ∀ n, SReach s (run.st n).1 (run.st n).2
|
|
689
|
+
| 0 => run.start
|
|
690
|
+
| n + 1 => by rw [run.next n]; exact SReach.step _ _ _ _ (srun_reach run n) (run.allowed n)
|
|
691
|
+
|
|
692
|
+
-- A step law over sets of one step: every request allowed that `when` admits takes a step where `then` holds.
|
|
693
|
+
theorem skstep_one {s : SMachine} {ps : List SEntry} {l : Live} {w t : C} {p : SProof} (hl : checkSMachine s ps = true)
|
|
694
|
+
(hc : checkSKStep s ps (sliveStep l w t p) = true) : ∀ ρ M, SReach s ρ M → ∀ ι κ,
|
|
695
|
+
evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true →
|
|
696
|
+
evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) w = true →
|
|
697
|
+
evalC (primed l.prime (sNext s ρ M ι κ).1 (withExt s ρ (sReq s ρ M ι κ)))
|
|
698
|
+
(primedMaps l.primeMaps (sNext s ρ M ι κ).2 (withCarried s M κ)) t = true := by
|
|
699
|
+
intro ρ M hr ι κ hA hW
|
|
700
|
+
have h := skstep_sound s ps _ hl hc ρ M hr (ι, κ) [] rfl ⟨hA, hW, trivial⟩
|
|
701
|
+
simpa [sliveStep, sEnd, sExts, renEnv, renMaps, lookup] using h
|
|
702
|
+
|
|
703
|
+
-- A step law over sets of one step, read as one step: every request allowed that `when` admits takes a step where `then`
|
|
704
|
+
-- holds, its fresh names read as the step reads them.
|
|
705
|
+
theorem skstep_holds_one {s : SMachine} {sl : SKStep} {w : C} (h : SKStepHolds s sl) (hw : sl.k.whens = [w]) (hren : sl.k.ren = [[]])
|
|
706
|
+
(hmren : sl.mren = [[]]) : ∀ ρ M, SReach s ρ M → ∀ ι κ,
|
|
707
|
+
evalC (sView s ρ M ι κ).1 (sView s ρ M ι κ).2 s.f.m.assume = true →
|
|
708
|
+
evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) w = true →
|
|
709
|
+
evalC (primed sl.k.prime (sNext s ρ M ι κ).1 (withExt s ρ (sReq s ρ M ι κ)))
|
|
710
|
+
(primedMaps sl.k.primeMaps (sNext s ρ M ι κ).2 (withCarried s M κ)) sl.k.then_ = true := by
|
|
711
|
+
intro ρ M hr ι κ hA hW
|
|
712
|
+
have := h ρ M hr (ι, κ) [] (by simp [hw]) (by rw [hw]; exact ⟨hA, hW, trivial⟩)
|
|
713
|
+
rw [hren, hmren] at this
|
|
714
|
+
simpa [sEnd, sExts, renEnv, renMaps, lookup] using this
|
|
715
|
+
|
|
716
|
+
theorem decideQ_other {s : SMachine} {i : Nat} (h : i ∉ s.qVars) (X : Nat → Nat) (N : Nat → Nat → Nat) : decideQ s X N i = X i := by
|
|
717
|
+
simp only [decideQ]
|
|
718
|
+
rw [lookup_none (fun q hq => by
|
|
719
|
+
simp only [beq_eq_false_iff_ne]; intro he; exact h (by rw [← he]; exact List.mem_map_of_mem hq))]
|
|
720
|
+
|
|
721
|
+
theorem slive_reads {s : SMachine} {l : Live} (hok : sliveOk s l = true) {c : C} (hc : c ∈ liveConds l)
|
|
722
|
+
(ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
|
|
723
|
+
(∀ i, occC i c = true → i ∉ l.prime.map (·.2) ∧ (i ∈ l.prime.map (·.1) ∨ (sNext s ρ M ι κ).1 i = withExt s ρ (sReq s ρ M ι κ) i))
|
|
724
|
+
∧ (∀ mp, occMC mp c = true → mp ∉ l.primeMaps.map (·.2) ∧ (mp ∈ l.primeMaps.map (·.1) ∨ (sNext s ρ M ι κ).2 mp = withCarried s M κ mp))
|
|
725
|
+
∧ (∀ i, occC i c = true → withExt s ρ (sReq s ρ M ι κ) i = ρ i)
|
|
726
|
+
∧ (∀ mp, occMC mp c = true → withCarried s M κ mp = M mp) := by
|
|
727
|
+
simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok
|
|
728
|
+
have hv := hok.1.2 c hc
|
|
729
|
+
have noExt : ∀ i, occC i c = true → i ∉ s.extVars := fun i hi hm => avoids_occ hv.1 hi (List.mem_append_left _ (List.mem_append_right _ hm))
|
|
730
|
+
have hext : ∀ i, occC i c = true → withExt s ρ (sReq s ρ M ι κ) i = ρ i := fun i hi => by
|
|
731
|
+
simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using noExt i hi)]
|
|
732
|
+
have noCar : ∀ mp, occMC mp c = true → mp ∉ s.carried := fun mp hmp hm => avoidsMaps_occ hv.2 hmp (List.mem_append_left _ (List.mem_append_right _ hm))
|
|
733
|
+
have hcar : ∀ mp, occMC mp c = true → withCarried s M κ mp = M mp := fun mp hmp => by
|
|
734
|
+
simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using noCar mp hmp)]
|
|
735
|
+
refine ⟨fun i hi => ⟨fun h => avoids_occ hv.1 hi (List.mem_append_left _ (List.mem_append_left _ h)), ?_⟩,
|
|
736
|
+
fun mp hmp => ⟨fun h => avoidsMaps_occ hv.2 hmp (List.mem_append_left _ (List.mem_append_left _ h)), ?_⟩, hext, hcar⟩
|
|
737
|
+
· by_cases hp : i ∈ l.prime.map (·.1)
|
|
738
|
+
· exact Or.inl hp
|
|
739
|
+
· right
|
|
740
|
+
have hn : i ∉ flagVars s.f.flags ++ s.f.m.step.map (·.1) := fun hm =>
|
|
741
|
+
avoids_occ hv.1 hi (List.mem_append_right _ (List.mem_filter.mpr ⟨hm, by simpa [List.contains_iff_mem] using hp⟩))
|
|
742
|
+
have hx := noExt i hi
|
|
743
|
+
simp only [SMachine.extVars, SMachine.qVars, List.mem_append, not_or] at hx
|
|
744
|
+
rw [hext i hi]
|
|
745
|
+
simp only [sNext, sView]
|
|
746
|
+
rw [applyS_other (fun h => hn (List.mem_append_right _ h)), decide_other (fun h => hn (List.mem_append_left _ h)),
|
|
747
|
+
decideQ_other (by simpa [SMachine.qVars] using hx.1.2), withInputs_other hx.1.1]
|
|
748
|
+
· by_cases hp : mp ∈ l.primeMaps.map (·.1)
|
|
749
|
+
· exact Or.inl hp
|
|
750
|
+
· right
|
|
751
|
+
have hw : mp ∉ s.f.m.writes.map (·.1) ++ s.every.map (·.1) := fun hm =>
|
|
752
|
+
avoidsMaps_occ hv.2 hmp (List.mem_append_right _ (List.mem_filter.mpr ⟨hm, by simpa [List.contains_iff_mem] using hp⟩))
|
|
753
|
+
funext k
|
|
754
|
+
simp only [sNext, everyMaps]
|
|
755
|
+
rw [lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro he; exact hw (List.mem_append_right _ (by rw [← he]; exact List.mem_map_of_mem hw')))]
|
|
756
|
+
simp only [writeMaps]
|
|
757
|
+
rw [lookup_none (fun w hw' => by simp only [beq_eq_false_iff_ne]; intro he; exact hw (List.mem_append_left _ (by rw [← he]; exact List.mem_map_of_mem hw')))]
|
|
758
|
+
rfl
|
|
759
|
+
|
|
760
|
+
/-- Liveness under fairness: if the laws' proof checks, the liveness reads what it may (`sliveOk`), and the step laws Lean
|
|
761
|
+
builds from it check, then along every run that takes each helpful request (its named inputs as its terms; the rest,
|
|
762
|
+
and the maps it carries, free) infinitely often, each state where `from` holds is followed by one where `to` holds. -/
|
|
763
|
+
theorem sleads_sound (s : SMachine) (ps : List SEntry) (l : Live) (sp cp : SProof) (hp : List SProof)
|
|
764
|
+
(hl : checkSMachine s ps = true) (hok : sliveOk s l = true)
|
|
765
|
+
(hs : checkSKStep s ps (sliveStep l l.pend (liveStayC l) sp) = true) (hc : checkSKStep s ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
766
|
+
(hh : sliveHelpsOk s ps l hp = true) (run : SRun s)
|
|
767
|
+
(fair : ∀ h ∈ l.helps, ∀ n, ∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
768
|
+
∀ 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 := by
|
|
769
|
+
have hok0 := hok
|
|
770
|
+
simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok0
|
|
771
|
+
obtain ⟨⟨⟨hd1, hd2⟩, _⟩, hreqs⟩ := hok0
|
|
772
|
+
simp only [sliveHelpsOk, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hh
|
|
773
|
+
obtain ⟨hlen, hall⟩ := hh
|
|
774
|
+
refine leads_view l run.st (fun n => withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2))
|
|
775
|
+
(fun n => primed l.prime (run.st (n + 1)).1 (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)))
|
|
776
|
+
(fun n => withCarried s (run.st n).2 (run.req n).2) (fun n => primedMaps l.primeMaps (run.st (n + 1)).2 (withCarried s (run.st n).2 (run.req n).2))
|
|
777
|
+
(fun h n => ∀ p ∈ h.2, (run.req n).1 p.1 = evalE (run.st n).1 (run.st n).2 p.2) (fun _ _ => True) ?_ ?_ ?_ ?_ ?_ ?_
|
|
778
|
+
(fun _ _ _ _ _ _ => trivial) (fun h hh n _ => fair h hh n)
|
|
779
|
+
· intro n c hc
|
|
780
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok hc (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
781
|
+
rw [run.next n]
|
|
782
|
+
exact view_C hd1 hd2 hv hm hx hn
|
|
783
|
+
· intro n e he
|
|
784
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok (mem_conds_rank he) (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
785
|
+
rw [run.next n]
|
|
786
|
+
exact view_E hd1 hd2 hv hm hx hn
|
|
787
|
+
· intro n hB
|
|
788
|
+
have := skstep_one hl hs _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
789
|
+
rw [run.next n]; exact this
|
|
790
|
+
· intro n hB
|
|
791
|
+
have := skstep_one hl hc _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
792
|
+
rw [run.next n]; exact this
|
|
793
|
+
· intro h hmem n hB
|
|
794
|
+
obtain ⟨pp, hpp⟩ := zip_cover l.helps hp hlen h hmem
|
|
795
|
+
have := skstep_one hl (hall _ hpp) _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
796
|
+
rw [run.next n]; exact this
|
|
797
|
+
· intro h hmem n hH
|
|
798
|
+
show evalC (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)) (withCarried s (run.st n).2 (run.req n).2) (reqC h.2) = true
|
|
799
|
+
rw [reqC_eval]
|
|
800
|
+
intro p hp'
|
|
801
|
+
have hq := hreqs h hmem p hp'
|
|
802
|
+
obtain ⟨⟨hin, hav⟩, havm⟩ := hq
|
|
803
|
+
have hext : s.extVars.contains p.1 = true := by
|
|
804
|
+
simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append]; exact Or.inl (Or.inl (List.contains_iff_mem.mp hin))
|
|
805
|
+
have hl1 : withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2) p.1 = (run.req n).1 p.1 := by
|
|
806
|
+
simp only [withExt, hext, if_true, sReq, hin]
|
|
807
|
+
have hr1 : evalE (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)) (withCarried s (run.st n).2 (run.req n).2) p.2
|
|
808
|
+
= evalE (run.st n).1 (run.st n).2 p.2 := by
|
|
809
|
+
rw [congrME _ _ (run.st n).2 p.2 (fun mp hmp => by
|
|
810
|
+
simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using avoidsMaps_occ havm (by simp [occMC, hmp]))])]
|
|
811
|
+
exact congrE _ _ _ p.2 (fun i hi => by
|
|
812
|
+
simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using avoids_occ hav (by simp [occC, hi]))])
|
|
813
|
+
rw [hl1, hr1]; exact hH p hp'
|
|
814
|
+
|
|
815
|
+
/-! Weak fairness over sets -/
|
|
816
|
+
|
|
817
|
+
-- A request whose inputs named in a list are the list's terms meets the list's condition, in the goal's view.
|
|
818
|
+
theorem sreq_list {s : SMachine} {ps' : List (Nat × E)}
|
|
819
|
+
(hc : ∀ p ∈ ps', (s.f.m.inputs.contains p.1 = true ∧ avoids s.extVars (.eq p.2 p.2) = true) ∧ avoidsMaps s.carried (.eq p.2 p.2) = true)
|
|
820
|
+
{ρ : Nat → Nat} {M : Nat → Nat → Nat} {ι : Nat → Nat} {κ : Nat → Nat → Nat} (hH : ∀ p ∈ ps', ι p.1 = evalE ρ M p.2) :
|
|
821
|
+
evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) (reqC ps') = true := by
|
|
822
|
+
rw [reqC_eval]
|
|
823
|
+
intro p hp'
|
|
824
|
+
obtain ⟨⟨hin, hav⟩, havm⟩ := hc p hp'
|
|
825
|
+
have hext : s.extVars.contains p.1 = true := by
|
|
826
|
+
simp only [SMachine.extVars, List.contains_iff_mem, List.mem_append]; exact Or.inl (Or.inl (List.contains_iff_mem.mp hin))
|
|
827
|
+
have hl1 : withExt s ρ (sReq s ρ M ι κ) p.1 = ι p.1 := by simp only [withExt, hext, if_true, sReq, hin]
|
|
828
|
+
have hr1 : evalE (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) p.2 = evalE ρ M p.2 := by
|
|
829
|
+
rw [congrME _ _ M p.2 (fun mp hmp => by
|
|
830
|
+
simp only [withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using avoidsMaps_occ havm (by simp [occMC, hmp]))])]
|
|
831
|
+
exact congrE _ _ _ p.2 (fun i hi => by
|
|
832
|
+
simp only [withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using avoids_occ hav (by simp [occC, hi]))])
|
|
833
|
+
rw [hl1, hr1]; exact hH p hp'
|
|
834
|
+
|
|
835
|
+
theorem sreq_holds {s : SMachine} {l : Live} (hok : sliveOk s l = true) {h : C × List (Nat × E)} (hmem : h ∈ l.helps)
|
|
836
|
+
{ρ : Nat → Nat} {M : Nat → Nat → Nat} {ι : Nat → Nat} {κ : Nat → Nat → Nat} (hH : ∀ p ∈ h.2, ι p.1 = evalE ρ M p.2) :
|
|
837
|
+
evalC (withExt s ρ (sReq s ρ M ι κ)) (withCarried s M κ) (reqC h.2) = true := by
|
|
838
|
+
simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok
|
|
839
|
+
exact sreq_list (fun p hp' => hok.2 h hmem p hp') hH
|
|
840
|
+
|
|
841
|
+
-- A helpful request allowed in its region, carrying empty sets: the precondition, the facts, the laws, `when`, each
|
|
842
|
+
-- quantified flag closed, and the carried maps empty at the keys the proof names refute the assumption's failing. The
|
|
843
|
+
-- facts read no request's variable or map.
|
|
844
|
+
def zeroHyps (s : SMachine) (zkeys : List E) : List C := s.carried.flatMap fun mp => zkeys.map fun t => .eq (.get mp t) (.const 0)
|
|
845
|
+
-- Allowed by a condition `a` (the machine's assumption, or one that implies it where the machine is: Sums.lean's rule's
|
|
846
|
+
-- own assumption, whose machine assumes facts true there besides).
|
|
847
|
+
def sAllowGoalA (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E) (a : C) : List C :=
|
|
848
|
+
[s.f.m.pre] ++ factHyps s p.2.2.1 p.2.2.2.2.1 ++ (scalarHyps (sLaws ps p.2.2.2.1) ++ instHyps (sLaws ps p.2.2.2.1) (sInsts ps p.1 p.2.2.2.1))
|
|
849
|
+
++ [w] ++ closingHyps s p.2.1 ++ zeroHyps s zkeys ++ [.not (inlineC s.f.flags a)]
|
|
850
|
+
def sAllowGoal (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E) : List C := sAllowGoalA s ps w p zkeys s.f.m.assume
|
|
851
|
+
def checkSAllowA (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E) (a : C) : Bool :=
|
|
852
|
+
s.facts.all (fun l => avoids s.extVars l.holds && avoidsMaps s.carried l.holds) && avoids s.wVars a
|
|
853
|
+
&& checkIn (sAllowGoalA s ps w p zkeys a) p.2.2.2.2.2
|
|
854
|
+
def checkSAllow (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E) : Bool := checkSAllowA s ps w p zkeys s.f.m.assume
|
|
855
|
+
|
|
856
|
+
-- The request carrying empty sets.
|
|
857
|
+
def noSets : Nat → Nat → Nat := fun _ _ => 0
|
|
858
|
+
|
|
859
|
+
theorem sallowA_sound (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E) (a : C)
|
|
860
|
+
(hl : checkSMachine s ps = true) (hc : checkSAllowA s ps w p zkeys a = true) :
|
|
861
|
+
∀ ρ M, SReach s ρ M → ∀ ι, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) w = true →
|
|
862
|
+
evalC (sView s ρ M ι noSets).1 (sView s ρ M ι noSets).2 a = true := by
|
|
863
|
+
intro ρ M hr ι hw
|
|
864
|
+
simp only [checkSAllowA, Bool.and_eq_true, List.all_eq_true] at hc
|
|
865
|
+
obtain ⟨⟨hfa, hpa⟩, hgoal⟩ := hc
|
|
866
|
+
have hs := sOk_of hl
|
|
867
|
+
obtain ⟨hpreS, hrp⟩ := spre_parts hl
|
|
868
|
+
let W := withExt s ρ (sReq s ρ M ι noSets)
|
|
869
|
+
let M0 := withCarried s M noSets
|
|
870
|
+
have hWo : ∀ v, v ∉ s.extVars → W v = ρ v := fun v hv => by
|
|
871
|
+
simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hv)]
|
|
872
|
+
have hMo : ∀ mp, mp ∉ s.carried → M0 mp = M mp := fun mp hmp => by
|
|
873
|
+
simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)]
|
|
874
|
+
-- Every entry, and each pair's, holds in the goal's view of the state.
|
|
875
|
+
have hall : ∀ e ∈ ps, e.Holds W M0 := fun e he =>
|
|
876
|
+
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
|
|
877
|
+
(fun i hi => by simp only [W, withExt]; rw [if_neg (by simpa [List.contains_iff_mem] using hi)])
|
|
878
|
+
(fun mp hmp => by simp only [M0, withCarried]; rw [if_neg (by simpa [List.contains_iff_mem] using hmp)])
|
|
879
|
+
(smachine_sound s ps hl ρ M hr e he)
|
|
880
|
+
have hlY : ∀ l ∈ sLaws ps p.2.2.2.1, l.Holds W M0 := fun l hl' => by
|
|
881
|
+
rcases List.mem_append.mp hl' with hl' | hl'
|
|
882
|
+
· obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl'; exact SEntry.holds_law (hall e' he')
|
|
883
|
+
· exact pairLaws_hold ps p.2.2.2.1 (skeys_apart hl) hall l hl'
|
|
884
|
+
have hfacts : ∀ l ∈ s.facts, l.Holds W M0 := fun l hl' => by
|
|
885
|
+
have h := hfa l hl'
|
|
886
|
+
exact holds_transfer l h.1 h.2 (fun i hi => hWo i hi) (fun mp hmp => hMo mp hmp) (facts_sreach s (sfacts_ok hl) hr l hl')
|
|
887
|
+
apply Decidable.byContradiction; intro hn
|
|
888
|
+
apply sound_in _ _ hgoal W M0
|
|
889
|
+
intro c hc
|
|
890
|
+
simp only [sAllowGoalA, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hc
|
|
891
|
+
rcases hc with (((((rfl | hc) | hc) | rfl) | hc) | hc) | rfl
|
|
892
|
+
· rw [noReadsC W M0 M s.f.m.pre hrp, congrC W ρ M s.f.m.pre (fun j hj => hWo j (fun h' =>
|
|
893
|
+
avoids_occ hpreS hj (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ h')))))]
|
|
894
|
+
exact pre_sreach s hpreS hrp hr
|
|
895
|
+
· -- The facts about several keys, as the reachable state has them: they read no request and no carried map.
|
|
896
|
+
have hpf : s.PFacts W M0 := fun f hf' =>
|
|
897
|
+
holdsOver_transfer f.1 (pfactsOk_parts (sfacts_ok hl) f hf').1 (pfactsOk_parts (sfacts_ok hl) f hf').2.1 f.2
|
|
898
|
+
(fun i hi => hWo i (fun h' => hi (List.mem_append_left _ (List.mem_append_left _ (List.mem_append_left _ h')))))
|
|
899
|
+
(fun mp hmp => hMo mp (fun h' => hmp (List.mem_append_left _ (List.mem_append_left _ h'))))
|
|
900
|
+
(pfacts_sreach s (sfacts_ok hl) hr f hf')
|
|
901
|
+
exact facthyps_true s p.2.2.1 p.2.2.2.2.1 W M0 (fun f hf' => (pfactsOk_parts (sfacts_ok hl) f hf').2.2) hfacts hpf c hc
|
|
902
|
+
· exact laws_hyps_true _ _ W M0 hlY c (List.mem_append.mpr hc)
|
|
903
|
+
· exact hw
|
|
904
|
+
· exact closing_true s ρ M ι noSets hs p.2.1 c hc
|
|
905
|
+
· simp only [zeroHyps, List.mem_flatMap, List.mem_map] at hc
|
|
906
|
+
obtain ⟨mp, hmp, t, _, rfl⟩ := hc
|
|
907
|
+
simp only [evalC, evalE, M0, withCarried, List.contains_iff_mem.mpr hmp, if_true, noSets, decide_eq_true_eq]
|
|
908
|
+
· -- The assumption, inlined: the view decides the plain flags as the step does, away from the witnesses.
|
|
909
|
+
simp only [evalC, Bool.not_eq_true']
|
|
910
|
+
rw [inline_evalC]
|
|
911
|
+
have hagD := decide_agree_on (sOk_parts s hs).2.2.2.2.2.2 (withCarried s M noSets) (view_agree s ρ M ι noSets hs)
|
|
912
|
+
rw [congrC _ (decideFlags s.f.flags (decideQ s (withInputs s.f.m.inputs ρ ι) (withCarried s M noSets)) (withCarried s M noSets)) _ a
|
|
913
|
+
(fun i hi => (hagD i (fun h => avoids_occ hpa hi h)).symm)]
|
|
914
|
+
have hf : evalC (sView s ρ M ι noSets).1 (sView s ρ M ι noSets).2 a = false := by simpa using hn
|
|
915
|
+
exact hf
|
|
916
|
+
|
|
917
|
+
theorem sallow_sound (s : SMachine) (ps : List SEntry) (w : C) (p : SProof) (zkeys : List E)
|
|
918
|
+
(hl : checkSMachine s ps = true) (hc : checkSAllow s ps w p zkeys = true) :
|
|
919
|
+
∀ ρ M, SReach s ρ M → ∀ ι, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) w = true →
|
|
920
|
+
evalC (sView s ρ M ι noSets).1 (sView s ρ M ι noSets).2 s.f.m.assume = true :=
|
|
921
|
+
sallowA_sound s ps w p zkeys s.f.m.assume hl hc
|
|
922
|
+
|
|
923
|
+
def sliveAllowsOkA (s : SMachine) (ps : List SEntry) (l : Live) (ap : List (List (Nat × E) × SProof × List E)) (a : C) : Bool :=
|
|
924
|
+
l.helps.length == ap.length
|
|
925
|
+
&& (l.helps.zip ap).all (fun x => distinct ((x.1.2 ++ x.2.1).map (·.1))
|
|
926
|
+
&& x.2.1.all (fun p => s.f.m.inputs.contains p.1 && avoids s.extVars (.eq p.2 p.2) && avoidsMaps s.carried (.eq p.2 p.2))
|
|
927
|
+
&& checkSAllowA s ps (.and (liveHelpWhen l x.1) (reqC x.2.1)) x.2.2.1 x.2.2.2 a)
|
|
928
|
+
def sliveAllowsOk (s : SMachine) (ps : List SEntry) (l : Live) (ap : List (List (Nat × E) × SProof × List E)) : Bool :=
|
|
929
|
+
sliveAllowsOkA s ps l ap s.f.m.assume
|
|
930
|
+
|
|
931
|
+
/-- Liveness under weak fairness of a condition `a` (the machine's assumption, `sleads_wf_sound`), for machines with sets: as `sleads_sound`, with each helpful request also proven
|
|
932
|
+
allowed in its region carrying empty sets (`sliveAllowsOk`); then along every run that takes each helpful request
|
|
933
|
+
whenever, from some point on, it stays allowed (some request with its named inputs as its terms, carrying some sets, is a
|
|
934
|
+
step the machine allows), each state where `from` holds is followed by one where `to` holds. -/
|
|
935
|
+
theorem sleads_wf_soundA (s : SMachine) (ps : List SEntry) (l : Live) (sp cp : SProof) (hp : List SProof) (ap : List (List (Nat × E) × SProof × List E))
|
|
936
|
+
(a : C) (hl : checkSMachine s ps = true) (hok : sliveOk s l = true)
|
|
937
|
+
(hs : checkSKStep s ps (sliveStep l l.pend (liveStayC l) sp) = true) (hc : checkSKStep s ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
938
|
+
(hh : sliveHelpsOk s ps l hp = true) (ha : sliveAllowsOkA s ps l ap a = true) (run : SRun s)
|
|
939
|
+
(fair : ∀ h ∈ l.helps, ∀ n, (∀ m, n ≤ m → ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
940
|
+
∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 a = true) →
|
|
941
|
+
∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
942
|
+
∀ 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 := by
|
|
943
|
+
have hok0 := hok
|
|
944
|
+
simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok0
|
|
945
|
+
obtain ⟨⟨⟨hd1, hd2⟩, _⟩, hreqs⟩ := hok0
|
|
946
|
+
simp only [sliveHelpsOk, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hh
|
|
947
|
+
obtain ⟨hlen, hall⟩ := hh
|
|
948
|
+
simp only [sliveAllowsOkA, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at ha
|
|
949
|
+
obtain ⟨halen, haall⟩ := ha
|
|
950
|
+
refine leads_view l run.st (fun n => withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2))
|
|
951
|
+
(fun n => primed l.prime (run.st (n + 1)).1 (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)))
|
|
952
|
+
(fun n => withCarried s (run.st n).2 (run.req n).2) (fun n => primedMaps l.primeMaps (run.st (n + 1)).2 (withCarried s (run.st n).2 (run.req n).2))
|
|
953
|
+
(fun h n => ∀ p ∈ h.2, (run.req n).1 p.1 = evalE (run.st n).1 (run.st n).2 p.2)
|
|
954
|
+
(fun h m => ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
955
|
+
∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 a = true) ?_ ?_ ?_ ?_ ?_ ?_ ?_ fair
|
|
956
|
+
· intro n c hc
|
|
957
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok hc (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
958
|
+
rw [run.next n]
|
|
959
|
+
exact view_C hd1 hd2 hv hm hx hn
|
|
960
|
+
· intro n e he
|
|
961
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok (mem_conds_rank he) (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
962
|
+
rw [run.next n]
|
|
963
|
+
exact view_E hd1 hd2 hv hm hx hn
|
|
964
|
+
· intro n hB
|
|
965
|
+
have := skstep_one hl hs _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
966
|
+
rw [run.next n]; exact this
|
|
967
|
+
· intro n hB
|
|
968
|
+
have := skstep_one hl hc _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
969
|
+
rw [run.next n]; exact this
|
|
970
|
+
· intro h hmem n hB
|
|
971
|
+
obtain ⟨pp, hpp⟩ := zip_cover l.helps hp hlen h hmem
|
|
972
|
+
have := skstep_one hl (hall _ hpp) _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
973
|
+
rw [run.next n]; exact this
|
|
974
|
+
· intro h hmem n hH
|
|
975
|
+
show evalC (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)) (withCarried s (run.st n).2 (run.req n).2) (reqC h.2) = true
|
|
976
|
+
exact sreq_holds hok hmem (fun p hp' => hH p hp')
|
|
977
|
+
· -- Enabled in its region: the request with its named inputs as its terms, the rest as the state has them, carrying
|
|
978
|
+
-- empty sets.
|
|
979
|
+
intro h hmem n hP hnQ hW
|
|
980
|
+
obtain ⟨x, hx⟩ := zip_cover l.helps ap halen h hmem
|
|
981
|
+
obtain ⟨⟨hdist, hwit⟩, hpc⟩ := haall _ hx
|
|
982
|
+
let ρ := (run.st n).1
|
|
983
|
+
let M := (run.st n).2
|
|
984
|
+
let ι := applyS (h.2 ++ x.1) ρ M
|
|
985
|
+
have hιp : ∀ p ∈ h.2, ι p.1 = evalE ρ M p.2 := fun p hp' => by
|
|
986
|
+
simp only [ι, applyS, assoc_distinct hdist (List.mem_append_left _ hp')]
|
|
987
|
+
have hιw : ∀ p ∈ x.1, ι p.1 = evalE ρ M p.2 := fun p hp' => by
|
|
988
|
+
simp only [ι, applyS, assoc_distinct hdist (List.mem_append_right _ hp')]
|
|
989
|
+
have hX : ∀ c ∈ liveConds l, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) c = evalC ρ M c := fun c hc' => by
|
|
990
|
+
obtain ⟨_, _, hx', hn'⟩ := slive_reads hok hc' ρ M ι noSets
|
|
991
|
+
rw [congrMC _ _ M c hn']; exact congrC _ _ M c hx'
|
|
992
|
+
have memF : l.from_ ∈ liveConds l := by simp [liveConds]
|
|
993
|
+
have memT : l.to ∈ liveConds l := by simp [liveConds]
|
|
994
|
+
have memH : h.1 ∈ liveConds l := by simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inr ⟨h, hmem, rfl⟩
|
|
995
|
+
refine ⟨ι, noSets, hιp, sallowA_sound s ps _ x.2.1 x.2.2 a hl hpc ρ M (srun_reach run n) ι ?_⟩
|
|
996
|
+
simp only [liveHelpWhen, Live.pend, evalC, Bool.and_eq_true, Bool.not_eq_true']
|
|
997
|
+
rw [hX _ memF, hX _ memT, hX _ memH]
|
|
998
|
+
exact ⟨⟨⟨hP, hnQ⟩, hW, sreq_holds hok hmem hιp⟩, sreq_list (fun p hp' => hwit p hp') hιw⟩
|
|
999
|
+
|
|
1000
|
+
/-- Liveness under weak fairness, for machines with sets: `sleads_wf_soundA` at the machine's own assumption. -/
|
|
1001
|
+
theorem sleads_wf_sound (s : SMachine) (ps : List SEntry) (l : Live) (sp cp : SProof) (hp : List SProof) (ap : List (List (Nat × E) × SProof × List E))
|
|
1002
|
+
(hl : checkSMachine s ps = true) (hok : sliveOk s l = true)
|
|
1003
|
+
(hs : checkSKStep s ps (sliveStep l l.pend (liveStayC l) sp) = true) (hc : checkSKStep s ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
1004
|
+
(hh : sliveHelpsOk s ps l hp = true) (ha : sliveAllowsOk s ps l ap = true) (run : SRun s)
|
|
1005
|
+
(fair : ∀ h ∈ l.helps, ∀ n, (∀ m, n ≤ m → ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
1006
|
+
∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 s.f.m.assume = true) →
|
|
1007
|
+
∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
1008
|
+
∀ 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 :=
|
|
1009
|
+
sleads_wf_soundA s ps l sp cp hp ap s.f.m.assume hl hok hs hc hh ha run fair
|
|
1010
|
+
|
|
1011
|
+
/-! Strong fairness over sets -/
|
|
1012
|
+
|
|
1013
|
+
def sliveHelpsOkS (s : SMachine) (ps : List SEntry) (l : Live) (lv : List Nat) (hp : List SProof) : Bool :=
|
|
1014
|
+
l.helps.length == lv.length && (l.helps.zip lv).length == hp.length
|
|
1015
|
+
&& ((l.helps.zip lv).zip hp).all (fun x => checkSKStep s ps (sliveStep l (liveHelpWhen l x.1.1) (liveHelpThenS l lv x.1.2) x.2))
|
|
1016
|
+
|
|
1017
|
+
/-- Liveness under strong fairness of a condition `a` (the machine's assumption, or one that implies it where the machine
|
|
1018
|
+
is), for machines with sets: the step laws Lean builds under strong fairness (`liveStaySC`: the regions need not stay;
|
|
1019
|
+
`liveHelpThenS`: a helpful request may reach the region of one of a higher level instead), each helpful request proven
|
|
1020
|
+
allowed by `a` in its region; then along every run that takes each helpful request in its region infinitely often
|
|
1021
|
+
wherever, infinitely often, it is allowed there (some request with its named inputs as its terms, carrying some sets,
|
|
1022
|
+
meets `a`), each state where `from` holds is followed by one where `to` holds. -/
|
|
1023
|
+
theorem sleads_sf_soundA (s : SMachine) (ps : List SEntry) (l : Live) (lv : List Nat) (sp cp : SProof) (hp : List SProof)
|
|
1024
|
+
(ap : List (List (Nat × E) × SProof × List E)) (a : C) (hl : checkSMachine s ps = true) (hok : sliveOk s l = true)
|
|
1025
|
+
(hs : checkSKStep s ps (sliveStep l l.pend (liveStaySC l) sp) = true) (hc : checkSKStep s ps (sliveStep l l.pend (liveCoverC l) cp) = true)
|
|
1026
|
+
(hh : sliveHelpsOkS s ps l lv hp = true) (ha : sliveAllowsOkA s ps l ap a = true) (run : SRun s)
|
|
1027
|
+
(fair : ∀ h ∈ l.helps, (∀ n, ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 h.1 = true ∧ ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
1028
|
+
∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 a = true) →
|
|
1029
|
+
∀ n, ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 h.1 = true ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) :
|
|
1030
|
+
∀ 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 := by
|
|
1031
|
+
have hok0 := hok
|
|
1032
|
+
simp only [sliveOk, Bool.and_eq_true, List.all_eq_true] at hok0
|
|
1033
|
+
obtain ⟨⟨⟨hd1, hd2⟩, _⟩, hreqs⟩ := hok0
|
|
1034
|
+
simp only [sliveHelpsOkS, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at hh
|
|
1035
|
+
obtain ⟨⟨hlv, hlen⟩, hall⟩ := hh
|
|
1036
|
+
simp only [sliveAllowsOkA, Bool.and_eq_true, beq_iff_eq, List.all_eq_true] at ha
|
|
1037
|
+
obtain ⟨halen, haall⟩ := ha
|
|
1038
|
+
refine leads_view_sf l lv hlv run.st (fun n => withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2))
|
|
1039
|
+
(fun n => primed l.prime (run.st (n + 1)).1 (withExt s (run.st n).1 (sReq s (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2)))
|
|
1040
|
+
(fun n => withCarried s (run.st n).2 (run.req n).2) (fun n => primedMaps l.primeMaps (run.st (n + 1)).2 (withCarried s (run.st n).2 (run.req n).2))
|
|
1041
|
+
(fun h n => ∀ p ∈ h.2, (run.req n).1 p.1 = evalE (run.st n).1 (run.st n).2 p.2)
|
|
1042
|
+
(fun h m => ∃ ι κ, (∀ p ∈ h.2, ι p.1 = evalE (run.st m).1 (run.st m).2 p.2)
|
|
1043
|
+
∧ evalC (sView s (run.st m).1 (run.st m).2 ι κ).1 (sView s (run.st m).1 (run.st m).2 ι κ).2 a = true) ?_ ?_ ?_ ?_ ?_ ?_ ?_ fair
|
|
1044
|
+
· intro n c hc
|
|
1045
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok hc (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
1046
|
+
rw [run.next n]
|
|
1047
|
+
exact view_C hd1 hd2 hv hm hx hn
|
|
1048
|
+
· intro n e he
|
|
1049
|
+
obtain ⟨hv, hm, hx, hn⟩ := slive_reads hok (mem_conds_rank he) (run.st n).1 (run.st n).2 (run.req n).1 (run.req n).2
|
|
1050
|
+
rw [run.next n]
|
|
1051
|
+
exact view_E hd1 hd2 hv hm hx hn
|
|
1052
|
+
· intro n hB
|
|
1053
|
+
have := skstep_one hl hs _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
1054
|
+
rw [run.next n]; exact this
|
|
1055
|
+
· intro n hB
|
|
1056
|
+
have := skstep_one hl hc _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
1057
|
+
rw [run.next n]; exact this
|
|
1058
|
+
· intro x hx n hB
|
|
1059
|
+
obtain ⟨pp, hpp⟩ := zip_cover (l.helps.zip lv) hp hlen x hx
|
|
1060
|
+
have := skstep_one hl (hall _ hpp) _ _ (srun_reach run n) _ _ (run.allowed n) hB
|
|
1061
|
+
rw [run.next n]; exact this
|
|
1062
|
+
· intro h hmem n hH
|
|
1063
|
+
exact sreq_holds hok hmem (fun p hp' => hH p hp')
|
|
1064
|
+
· intro h hmem n hP hnQ hW
|
|
1065
|
+
obtain ⟨x, hx⟩ := zip_cover l.helps ap halen h hmem
|
|
1066
|
+
obtain ⟨⟨hdist, hwit⟩, hpc⟩ := haall _ hx
|
|
1067
|
+
let ρ := (run.st n).1
|
|
1068
|
+
let M := (run.st n).2
|
|
1069
|
+
let ι := applyS (h.2 ++ x.1) ρ M
|
|
1070
|
+
have hιp : ∀ p ∈ h.2, ι p.1 = evalE ρ M p.2 := fun p hp' => by
|
|
1071
|
+
simp only [ι, applyS, assoc_distinct hdist (List.mem_append_left _ hp')]
|
|
1072
|
+
have hιw : ∀ p ∈ x.1, ι p.1 = evalE ρ M p.2 := fun p hp' => by
|
|
1073
|
+
simp only [ι, applyS, assoc_distinct hdist (List.mem_append_right _ hp')]
|
|
1074
|
+
have hX : ∀ c ∈ liveConds l, evalC (withExt s ρ (sReq s ρ M ι noSets)) (withCarried s M noSets) c = evalC ρ M c := fun c hc' => by
|
|
1075
|
+
obtain ⟨_, _, hx', hn'⟩ := slive_reads hok hc' ρ M ι noSets
|
|
1076
|
+
rw [congrMC _ _ M c hn']; exact congrC _ _ M c hx'
|
|
1077
|
+
have memF : l.from_ ∈ liveConds l := by simp [liveConds]
|
|
1078
|
+
have memT : l.to ∈ liveConds l := by simp [liveConds]
|
|
1079
|
+
have memH : h.1 ∈ liveConds l := by simp only [liveConds, List.mem_append, List.mem_map]; exact Or.inr ⟨h, hmem, rfl⟩
|
|
1080
|
+
refine ⟨ι, noSets, hιp, sallowA_sound s ps _ x.2.1 x.2.2 a hl hpc ρ M (srun_reach run n) ι ?_⟩
|
|
1081
|
+
simp only [liveHelpWhen, Live.pend, evalC, Bool.and_eq_true, Bool.not_eq_true']
|
|
1082
|
+
rw [hX _ memF, hX _ memT, hX _ memH]
|
|
1083
|
+
exact ⟨⟨⟨hP, hnQ⟩, hW, sreq_holds hok hmem hιp⟩, sreq_list (fun p hp' => hwit p hp') hιw⟩
|
|
1084
|
+
|
|
1085
|
+
/-! Loops, as machines -/
|
|
1086
|
+
|
|
1087
|
+
-- A loop as a machine with sets (none of them): the loop's state, inputs, precondition and assumption, its body taken
|
|
1088
|
+
-- where the guard holds and the state kept where it doesn't. That it halts is a liveness claim: every request helps,
|
|
1089
|
+
-- `to` is the guard failing, and the loop's ranks drop.
|
|
1090
|
+
def Loop.machine (l : Loop) : SMachine :=
|
|
1091
|
+
⟨⟨⟨l.inputs, l.init, [], l.body.map (fun p => (p.1, E.ite l.guard p.2 (.var p.1))), [], l.pre, l.assume⟩, []⟩, [], [], [], [], [], []⟩
|
|
1092
|
+
|
|
1093
|
+
theorem assoc_guarded (g : C) (i : Nat) : ∀ body : List (Nat × E),
|
|
1094
|
+
assoc (body.map fun p => (p.1, E.ite g p.2 (.var p.1))) i = (assoc body i).map fun e => E.ite g e (.var i)
|
|
1095
|
+
| [] => rfl
|
|
1096
|
+
| p :: ps => by
|
|
1097
|
+
simp only [List.map_cons, assoc, lookup] at ⊢
|
|
1098
|
+
by_cases h : (p.1 == i) = true
|
|
1099
|
+
· have hp : p.1 = i := beq_iff_eq.mp h
|
|
1100
|
+
simp only [h, ite_true, Option.map_some]; rw [hp]
|
|
1101
|
+
· simp only [h, Bool.false_eq_true, ite_false]
|
|
1102
|
+
have := assoc_guarded g i ps
|
|
1103
|
+
simp only [assoc] at this; exact this
|
|
1104
|
+
|
|
1105
|
+
-- The loop's machine reads a request as the loop does: its inputs set, nothing decided, no map carried.
|
|
1106
|
+
theorem sView_loop (l : Loop) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat) :
|
|
1107
|
+
sView l.machine ρ M ι κ = (withInputs l.inputs ρ ι, M) := by
|
|
1108
|
+
have hc : withCarried l.machine M κ = M := by funext mp; simp [withCarried, Loop.machine]
|
|
1109
|
+
have hq : decideQ l.machine (withInputs l.inputs ρ ι) M = withInputs l.inputs ρ ι := by funext v; simp [decideQ, Loop.machine, lookup]
|
|
1110
|
+
simp only [sView, hc]
|
|
1111
|
+
show (decideFlags [] (decideQ l.machine (withInputs l.inputs ρ ι) M) M, M) = _
|
|
1112
|
+
rw [hq]; simp [decideFlags, flagSub, applyS_nil']
|
|
1113
|
+
|
|
1114
|
+
-- Where the guard holds, a step of the loop's machine is a turn of the loop, and leaves the maps.
|
|
1115
|
+
theorem sNext_loop (l : Loop) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) (κ : Nat → Nat → Nat)
|
|
1116
|
+
(hg : evalC (withInputs l.inputs ρ ι) M l.guard = true) :
|
|
1117
|
+
sNext l.machine ρ M ι κ = (applyS l.body (withInputs l.inputs ρ ι) M, M) := by
|
|
1118
|
+
simp only [sNext, sView_loop]
|
|
1119
|
+
refine Prod.ext ?_ ?_
|
|
1120
|
+
· funext i
|
|
1121
|
+
show applyS (l.body.map fun p => (p.1, E.ite l.guard p.2 (.var p.1))) (withInputs l.inputs ρ ι) M i = _
|
|
1122
|
+
simp only [applyS, assoc_guarded]
|
|
1123
|
+
cases assoc l.body i with
|
|
1124
|
+
| none => rfl
|
|
1125
|
+
| some e => simp [evalE, hg]
|
|
1126
|
+
· funext mp key; simp [everyMaps, Loop.machine, lookup, writeMaps]
|
|
1127
|
+
|
|
1128
|
+
/-- A loop halts: if the loop's machine reaches the guard failing from anywhere, along every run whose helpful requests
|
|
1129
|
+
(here naming no input, so every run's) recur, then for params meeting the precondition no run of turns is infinite. -/
|
|
1130
|
+
theorem loop_halts_of (l : Loop) (lv : Live) (hfrom : lv.from_ = trueC) (hto : lv.to = .not l.guard) (hhelps : ∀ h ∈ lv.helps, h.2 = [])
|
|
1131
|
+
(hguard : avoids l.inputs l.guard = true)
|
|
1132
|
+
(hlive : ∀ run : SRun l.machine, (∀ h ∈ lv.helps, ∀ n, ∃ m, n ≤ m ∧ ∀ p ∈ h.2, (run.req m).1 p.1 = evalE (run.st m).1 (run.st m).2 p.2) →
|
|
1133
|
+
∀ n, evalC (run.st n).1 (run.st n).2 lv.from_ = true → ∃ m, n ≤ m ∧ evalC (run.st m).1 (run.st m).2 lv.to = true)
|
|
1134
|
+
(maps : Nat → Nat → Nat) (ρ0 : Nat → Nat) (h0 : evalC ρ0 maps l.pre = true) :
|
|
1135
|
+
¬ ∃ f : Nat → Nat → Nat, f 0 = applyS l.init ρ0 maps ∧ ∀ n, Turn l maps (f n) (f (n + 1)) := by
|
|
1136
|
+
rintro ⟨f, hf0, hf⟩
|
|
1137
|
+
let ι := fun n => Classical.choose (hf n)
|
|
1138
|
+
have hg : ∀ n, evalC (withInputs l.inputs (f n) (ι n)) maps l.guard = true := fun n => (Classical.choose_spec (hf n)).1
|
|
1139
|
+
have ha : ∀ n, evalC (withInputs l.inputs (f n) (ι n)) maps l.assume = true := fun n => (Classical.choose_spec (hf n)).2.1
|
|
1140
|
+
have hnext : ∀ n, f (n + 1) = applyS l.body (withInputs l.inputs (f n) (ι n)) maps := fun n => (Classical.choose_spec (hf n)).2.2
|
|
1141
|
+
let run : SRun l.machine := {
|
|
1142
|
+
st := fun n => (f n, maps)
|
|
1143
|
+
req := fun n => (ι n, fun _ _ => 0)
|
|
1144
|
+
start := by
|
|
1145
|
+
show SReach l.machine (f 0) maps
|
|
1146
|
+
rw [hf0]
|
|
1147
|
+
exact SReach.start (s := l.machine) ρ0 maps (fun p hp => by simp [Loop.machine] at hp) h0 (fun l' hl' => by simp [Loop.machine] at hl') (fun p hp => by simp [Loop.machine] at hp)
|
|
1148
|
+
allowed := fun n => by
|
|
1149
|
+
show evalC (sView l.machine (f n) maps (ι n) _).1 (sView l.machine (f n) maps (ι n) _).2 l.assume = true
|
|
1150
|
+
rw [sView_loop]; exact ha n
|
|
1151
|
+
next := fun n => by
|
|
1152
|
+
show (f (n + 1), maps) = sNext l.machine (f n) maps (ι n) _
|
|
1153
|
+
rw [sNext_loop l _ _ _ _ (hg n), hnext n] }
|
|
1154
|
+
obtain ⟨m, _, hm⟩ := hlive run (fun h hh n => ⟨n, Nat.le_refl n, by simp [hhelps h hh]⟩) 0 (by rw [hfrom]; simp [trueC, evalC, evalE])
|
|
1155
|
+
rw [hto] at hm
|
|
1156
|
+
have hgm := hg m
|
|
1157
|
+
rw [congrC (withInputs l.inputs (f m) (ι m)) (f m) maps l.guard (fun i hi => withInputs_other (avoids_occ hguard hi) _ _)] at hgm
|
|
1158
|
+
simp only [evalC, Bool.not_eq_true'] at hm
|
|
1159
|
+
show False
|
|
1160
|
+
have : evalC (run.st m).1 (run.st m).2 l.guard = true := hgm
|
|
1161
|
+
rw [this] at hm; exact Bool.noConfusion hm
|
|
1162
|
+
|
|
1163
|
+
end Kern
|
|
1164
|
+
|
|
1165
|
+
#print axioms Kern.sleads_sound
|
|
1166
|
+
#print axioms Kern.sleads_wf_sound
|
|
1167
|
+
#print axioms Kern.sleads_sf_soundA
|
|
1168
|
+
#print axioms Kern.loop_halts_of
|