gdp-wired 0.51.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/dist/core/accel-refute.d.ts +53 -0
  3. package/dist/core/accel-refute.js +997 -0
  4. package/dist/core/cli.d.ts +2 -0
  5. package/dist/core/cli.js +8 -1
  6. package/dist/core/cycles.d.ts +2 -0
  7. package/dist/core/cycles.js +12 -9
  8. package/dist/core/dsl.d.ts +7 -1
  9. package/dist/core/dsl.js +147 -26
  10. package/dist/core/live-support.d.ts +5 -0
  11. package/dist/core/live-support.js +148 -0
  12. package/dist/core/live.js +194 -37
  13. package/dist/core/lockstep.d.ts +5 -0
  14. package/dist/core/lockstep.js +23 -0
  15. package/dist/core/ordered-pair-seeds.d.ts +2 -0
  16. package/dist/core/ordered-pair-seeds.js +109 -0
  17. package/dist/core/proof-output.d.ts +38 -1
  18. package/dist/core/proof-output.js +38 -0
  19. package/dist/core/search-termination.d.ts +4 -0
  20. package/dist/core/search-termination.js +1 -1
  21. package/dist/core/source-check.d.ts +4 -1
  22. package/dist/core/source-check.js +5 -2
  23. package/dist/core/sparse-kind-proof.d.ts +7 -0
  24. package/dist/core/sparse-kind-proof.js +49 -0
  25. package/dist/core/step-seeds.d.ts +46 -0
  26. package/dist/core/step-seeds.js +316 -0
  27. package/dist/lean-kernel/Cert.lean +222 -0
  28. package/dist/lean-kernel/Count.lean +1568 -0
  29. package/dist/lean-kernel/Ghost.lean +373 -0
  30. package/dist/lean-kernel/GhostMap.lean +426 -0
  31. package/dist/lean-kernel/Kernel.lean +2881 -0
  32. package/dist/lean-kernel/Live.lean +1168 -0
  33. package/dist/lean-kernel/Progress.lean +543 -0
  34. package/dist/lean-kernel/Sets.lean +1975 -0
  35. package/dist/lean-kernel/StepLaw.lean +128 -0
  36. package/dist/lean-kernel/Steps.lean +477 -0
  37. package/dist/lean-kernel/Sums.lean +916 -0
  38. package/dist/lean-kernel/emit.d.ts +4 -0
  39. package/dist/lean-kernel/emit.js +23 -4
  40. package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
  41. package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
  42. package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
  43. package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
  44. package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
  45. package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
  46. package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
  47. package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
  48. package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
  49. package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
  50. package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
  51. package/dist/lean-kernel/lean-aggregate/index.js +52 -0
  52. package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
  53. package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
  54. package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
  55. package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
  56. package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
  57. package/dist/lean-kernel/lean-aggregate/source.js +141 -0
  58. package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
  59. package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
  60. package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
  61. package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
  62. package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
  63. package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
  64. package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
  65. package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
  66. package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
  67. package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
  68. package/docs/lean-aggregate.md +29 -0
  69. package/package.json +6 -1
@@ -0,0 +1,1568 @@
1
+ import Kernel
2
+
3
+ /-!
4
+ Count: quorum facts as a certificate kind, proven sound. Lean 4.34.0, core only; on top of `Kernel.lean` alone.
5
+
6
+ **The lemma.** Finite sums over the keys below a bound (`sumBelow f n` = f 0 + … + f (n − 1)) are monotone and linear,
7
+ so a pointwise inequality sums: if g sums below f over the keys below n, some key below n has g below f there
8
+ (`exists_lt_of_sumBelow_lt`). Its corollaries are the quorum facts, for sets of keys below n (`card`):
9
+ - `pigeonhole`: A, B ⊆ S and |A| + |B| > |S| give a key in A ∩ B;
10
+ - `majorities_meet`: two majorities of S (2|A| > |S|, 2|B| > |S|) meet;
11
+ - `quorums_meet_outside`: A, B ⊆ S and |A| + |B| > |S| + |F| give a key in A ∩ B outside F (thresholds: two sets of
12
+ f + 1 out of 2f + 1, or two sets of n − t when n > 3t, meet in a process outside the t faulty ones).
13
+
14
+ **The certificate kind** (kernel.ts's `checkCounted`, its twin). A counted goal is a goal (hypotheses over naturals and
15
+ maps, as `Kernel.lean`'s) and counts: each a variable `v` that names Σ_{k < below} body(k), the body read with its key
16
+ variable `key` at k (a 0/1 body is a set's size). A certificate is a list of instances and a tree. An instance names a
17
+ fresh witness variable `wit`, a bound `below` every count it reads shares, integer coefficients on counts (`terms`) and
18
+ on the constant 1 (`unit`). With P the positive parts and N the negative parts (posP z = max z 0, negP z = max (−z) 0):
19
+
20
+ countHyp = ( P·counts + P(unit)·below ≤ N·counts + N(unit)·below )
21
+ ∨ ( wit < below ∧ N·bodies(wit) + N(unit) < P·bodies(wit) + P(unit) )
22
+
23
+ which is the lemma read at one key: either the combination's sum is not positive, or `wit` is a key below the bound
24
+ where its pointwise value is. Lean builds the instances' hypotheses itself (`countHyps`), checks each witness is fresh
25
+ (`countedOk`) and checks the tree against the goal's hypotheses followed by them (`checkGoal`). `counted_sound`: an
26
+ accepted counted goal's hypotheses hold at no naturals and maps where every count is its sum.
27
+
28
+ Why this shape is the smallest that covers the class: the whole of a Venn-region (BAPA) certificate is a linear
29
+ combination of set sizes whose pointwise value is never positive, so one instance (its Farkas coefficients on the
30
+ counts) plus a refutation at the witness key replaces the region variables: the case split on which sets hold the
31
+ witness is the tree's, over the cells it reads, as the kernel already splits. Majorities (+1, +1 on two counts, −1 on
32
+ the set's), f + 1 of 2f + 1 (the same) and more than 2/3 with faulty processes (+1, +1, −1 on S, −1 on F) are each one
33
+ instance; a count of a compound body (A ∩ B, say) gives the numeric forms (|A ∩ B| ≥ |A| + |B| − |S|).
34
+ -/
35
+
36
+ namespace Kern
37
+
38
+ /-! Finite sums below a bound -/
39
+
40
+ def sumBelow (f : Nat → Nat) : Nat → Nat
41
+ | 0 => 0
42
+ | n + 1 => sumBelow f n + f n
43
+
44
+ theorem sumBelow_mono {f g : Nat → Nat} (n : Nat) (h : ∀ k, k < n → f k ≤ g k) : sumBelow f n ≤ sumBelow g n := by
45
+ induction n with
46
+ | zero => simp [sumBelow]
47
+ | succ n ih =>
48
+ simp only [sumBelow]
49
+ have h1 := ih (fun k hk => h k (by omega))
50
+ have h2 := h n (by omega)
51
+ omega
52
+
53
+ theorem sumBelow_add (f g : Nat → Nat) (n : Nat) : sumBelow (fun k => f k + g k) n = sumBelow f n + sumBelow g n := by
54
+ induction n with
55
+ | zero => simp [sumBelow]
56
+ | succ n ih => simp only [sumBelow, ih]; omega
57
+
58
+ theorem sumBelow_mul (c : Nat) (f : Nat → Nat) (n : Nat) : sumBelow (fun k => c * f k) n = c * sumBelow f n := by
59
+ induction n with
60
+ | zero => simp [sumBelow]
61
+ | succ n ih => simp only [sumBelow, ih, Nat.mul_add]
62
+
63
+ theorem sumBelow_const (d n : Nat) : sumBelow (fun _ => d) n = d * n := by
64
+ induction n with
65
+ | zero => simp [sumBelow]
66
+ | succ n ih => simp only [sumBelow, ih, Nat.mul_add, Nat.mul_one]
67
+
68
+ theorem sumBelow_congr {f g : Nat → Nat} (n : Nat) (h : ∀ k, k < n → f k = g k) : sumBelow f n = sumBelow g n := by
69
+ induction n with
70
+ | zero => rfl
71
+ | succ n ih => simp only [sumBelow, ih (fun k hk => h k (by omega)), h n (by omega)]
72
+
73
+ theorem sumBelow_zero_fn (n : Nat) : sumBelow (fun _ => 0) n = 0 := by
74
+ induction n with
75
+ | zero => rfl
76
+ | succ n ih => simp [sumBelow, ih]
77
+
78
+ /-- A pointwise inequality, summed: if g sums below f over the keys below n, some key below n has g below f. -/
79
+ theorem exists_lt_of_sumBelow_lt {f g : Nat → Nat} {n : Nat} (h : sumBelow g n < sumBelow f n) : ∃ k, k < n ∧ g k < f k := by
80
+ apply Classical.byContradiction
81
+ intro hno
82
+ have hle : ∀ k, k < n → f k ≤ g k := fun k hk => Nat.le_of_not_lt (fun hlt => hno ⟨k, hk, hlt⟩)
83
+ have := sumBelow_mono n hle
84
+ omega
85
+
86
+ /-! Sets of keys below a bound, and the quorum facts -/
87
+
88
+ -- The number of keys below n where p holds.
89
+ def card (p : Nat → Bool) (n : Nat) : Nat := sumBelow (fun k => if p k then 1 else 0) n
90
+
91
+ /-- The pigeonhole: two subsets of S whose sizes add up to more than S's meet. -/
92
+ theorem pigeonhole {a b s : Nat → Bool} {n : Nat} (ha : ∀ k, k < n → a k = true → s k = true)
93
+ (hb : ∀ k, k < n → b k = true → s k = true) (h : card s n < card a n + card b n) :
94
+ ∃ k, k < n ∧ a k = true ∧ b k = true := by
95
+ unfold card at h
96
+ rw [← sumBelow_add] at h
97
+ obtain ⟨k, hk, hlt⟩ := exists_lt_of_sumBelow_lt h
98
+ have h1 := ha k hk
99
+ have h2 := hb k hk
100
+ refine ⟨k, hk, ?_⟩
101
+ cases hak : a k <;> cases hbk : b k <;> cases hsk : s k <;> simp_all
102
+
103
+ /-- Two majorities of S meet. -/
104
+ theorem majorities_meet {a b s : Nat → Bool} {n : Nat} (ha : ∀ k, k < n → a k = true → s k = true)
105
+ (hb : ∀ k, k < n → b k = true → s k = true) (h1 : card s n < 2 * card a n) (h2 : card s n < 2 * card b n) :
106
+ ∃ k, k < n ∧ a k = true ∧ b k = true :=
107
+ pigeonhole ha hb (by omega)
108
+
109
+ /-- Thresholds: two subsets of S whose sizes add up to more than |S| + |F| meet outside F (f + 1 of 2f + 1 with F
110
+ empty; n − t of n with n > 3t and at most t faulty). -/
111
+ theorem quorums_meet_outside {a b s f : Nat → Bool} {n : Nat} (ha : ∀ k, k < n → a k = true → s k = true)
112
+ (hb : ∀ k, k < n → b k = true → s k = true) (h : card s n + card f n < card a n + card b n) :
113
+ ∃ k, k < n ∧ a k = true ∧ b k = true ∧ f k = false := by
114
+ unfold card at h
115
+ rw [← sumBelow_add, ← sumBelow_add] at h
116
+ obtain ⟨k, hk, hlt⟩ := exists_lt_of_sumBelow_lt h
117
+ have h1 := ha k hk
118
+ have h2 := hb k hk
119
+ refine ⟨k, hk, ?_⟩
120
+ cases hak : a k <;> cases hbk : b k <;> cases hsk : s k <;> cases hfk : f k <;> simp_all
121
+
122
+ /-! The certificate kind: counts in a goal -/
123
+
124
+ -- A count: the variable v names the sum of `body` over the keys below `below`, `body` read with `key` at each key.
125
+ structure Count where
126
+ v : Nat
127
+ key : Nat
128
+ below : E
129
+ body : E
130
+
131
+ -- What a count means at an environment.
132
+ def Count.Holds (c : Count) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop :=
133
+ ρ c.v = sumBelow (fun k => evalE (upd ρ c.key k) M c.body) (evalE ρ M c.below)
134
+
135
+ -- An instance of the lemma: a fresh witness, the bound its counts share, a coefficient per count (named by its
136
+ -- variable) and one on the constant 1.
137
+ structure CInst where
138
+ wit : Nat
139
+ below : E
140
+ terms : List (Int × Nat)
141
+ unit : Int
142
+
143
+ -- A counted goal: its hypotheses, and the counts they read.
144
+ structure CGoal where
145
+ hyps : List C
146
+ counts : List Count
147
+
148
+ def findCount (cs : List Count) (v : Nat) : Option Count := lookup (fun c => c.v == v) cs
149
+
150
+ -- Each term's count (the first with its variable), when every term names one whose bound is the instance's.
151
+ def resolve (cs : List Count) (B : E) : List (Int × Nat) → Option (List (Int × Count))
152
+ | [] => some []
153
+ | t :: ts => match findCount cs t.2 with
154
+ | none => none
155
+ | some c => if beqE c.below B then (resolve cs B ts).map (fun rs => (t.1, c) :: rs) else none
156
+
157
+ -- A sum of terms, right-nested from 0.
158
+ def sumE : List E → E
159
+ | [] => .const 0
160
+ | e :: es => .add e (sumE es)
161
+
162
+ def posP (z : Int) : Nat := z.toNat
163
+ def negP (z : Int) : Nat := (-z).toNat
164
+
165
+ -- One side of an instance: each count read by `rd` times its coefficient's part, then the unit's part times `one`.
166
+ def side (part : Int → Nat) (rd : Count → E) (one : E) (u : Int) (rs : List (Int × Count)) : E :=
167
+ .add (sumE (rs.map fun r => .mul (part r.1) (rd r.2))) (.mul (part u) one)
168
+
169
+ -- A count read as its variable, and its body read at the witness.
170
+ def atCount (c : Count) : E := .var c.v
171
+ def atWit (w : Nat) (c : Count) : E := substE [(c.key, .var w)] c.body
172
+
173
+ -- The instance's hypothesis: the combination's sum is not positive, or the witness is a key below the bound where the
174
+ -- combination's value is.
175
+ def countHyp (cs : List Count) (i : CInst) : Option C :=
176
+ (resolve cs i.below i.terms).map fun rs =>
177
+ .or (.le (side posP atCount i.below i.unit rs) (side negP atCount i.below i.unit rs))
178
+ (.and (.lt (.var i.wit) i.below)
179
+ (.lt (side negP (atWit i.wit) (.const 1) i.unit rs) (side posP (atWit i.wit) (.const 1) i.unit rs)))
180
+
181
+ def countHyps (cs : List Count) : List CInst → Option (List C)
182
+ | [] => some []
183
+ | i :: is => match countHyp cs i with
184
+ | none => none
185
+ | some h => (countHyps cs is).map (h :: ·)
186
+
187
+ def cDistinct : List Nat → Bool
188
+ | [] => true
189
+ | a :: as => !as.contains a && cDistinct as
190
+
191
+ -- A witness is fresh: no hypothesis reads it, no count names it or reads it (in its bound or its body), and no
192
+ -- instance's bound reads it.
193
+ def witFresh (hyps : List C) (cs : List Count) (is : List CInst) (w : Nat) : Bool :=
194
+ hyps.all (fun h => !occC w h) && cs.all (fun c => c.v != w && !occE w c.below && !occE w c.body) && is.all (fun j => !occE w j.below)
195
+
196
+ def countedOk (hyps : List C) (cs : List Count) (is : List CInst) : Bool :=
197
+ cDistinct (is.map (·.wit)) && is.all (fun i => witFresh hyps cs is i.wit)
198
+
199
+ /-! Conditional lemma cuts. A lemma shares the containing goal's table and may select only its hypotheses.
200
+ Its proof is an old-language tree, checked without any lemma assumptions. This rules out circular citations. -/
201
+
202
+ section LemmaCuts
203
+ set_option autoImplicit false
204
+
205
+ structure CutLemma where
206
+ when : List Cert.Row
207
+ cut : Cert.Row
208
+ tree : Tree
209
+ slice : Option (List Nat) := none
210
+
211
+ def lemmaSlice (hyps : List C) : List Nat → Option (List C)
212
+ | [] => some []
213
+ | i :: is => match hyps[i]?, lemmaSlice hyps is with
214
+ | some h, some hs => some (h :: hs)
215
+ | _, _ => none
216
+
217
+ def lemmaHyps (hyps : List C) (l : CutLemma) : Option (List C) :=
218
+ match l.slice with
219
+ | none => some hyps
220
+ | some is => lemmaSlice hyps is
221
+
222
+ def checkLemma (tab : List VDef) (hyps : List C) (l : CutLemma) : Bool :=
223
+ match lemmaHyps hyps l with
224
+ | some hs => checkT tab hs ((l.cut, false) :: l.when.map (fun w => (w, true))) l.tree
225
+ | none => false
226
+
227
+ -- Eligibility reads only the original split path, never the cuts being made available.
228
+ def lemmaActive (path : Path) (l : CutLemma) : Bool :=
229
+ l.when.all (fun w => dec path w == some true)
230
+
231
+ def lemmaPath (lemmas : List CutLemma) (path : Path) : Path :=
232
+ (lemmas.filter (lemmaActive path)).map (fun l => (l.cut, true)) ++ path
233
+
234
+ def checkTLemmas (tab : List VDef) (hyps : List C) (lemmas : List CutLemma) : Path → Tree → Bool
235
+ | path, .split l y n => checkTLemmas tab hyps lemmas ((l, true) :: path) y && checkTLemmas tab hyps lemmas ((l, false) :: path) n
236
+ | path, t => checkT tab hyps (lemmaPath lemmas path) t
237
+
238
+ def checkGoalLemmas (g : Goal) (t : Tree) (lemmas : List CutLemma) : Bool :=
239
+ wfTab 0 g.tab && lemmas.all (checkLemma g.tab g.hyps) && checkTLemmas g.tab g.hyps lemmas [] t
240
+
241
+ @[simp] theorem lemmaPath_nil (path : Path) : lemmaPath [] path = path := rfl
242
+
243
+ @[simp] theorem checkTLemmas_nil (tab : List VDef) (hyps : List C) (t : Tree) (path : Path) :
244
+ checkTLemmas tab hyps [] path t = checkT tab hyps path t := by
245
+ induction t generalizing path with
246
+ | split l y n ihy ihn => simp only [checkTLemmas, checkT, ihy, ihn]
247
+ | leaf rows steps eqs prods => rfl
248
+ | leafAt rows steps eqs prods => rfl
249
+ | leafSq rows sqs steps eqs prods => rfl
250
+
251
+ @[simp] theorem checkGoalLemmas_nil (g : Goal) (t : Tree) : checkGoalLemmas g t [] = checkGoal g t := by
252
+ simp [checkGoalLemmas, checkGoal]
253
+
254
+ theorem lemmaSlice_sub {hyps : List C} : ∀ {is : List Nat} {hs : List C},
255
+ lemmaSlice hyps is = some hs → ∀ h ∈ hs, h ∈ hyps
256
+ | [], hs, he => by simp only [lemmaSlice, Option.some.injEq] at he; subst he; simp
257
+ | i :: is, hs, he => by
258
+ cases hi : hyps[i]? with
259
+ | none => simp [lemmaSlice, hi] at he
260
+ | some c =>
261
+ cases ht : lemmaSlice hyps is with
262
+ | none => simp [lemmaSlice, hi, ht] at he
263
+ | some cs =>
264
+ simp only [lemmaSlice, hi, ht, Option.some.injEq] at he
265
+ subst he
266
+ intro h hh
267
+ rcases List.mem_cons.mp hh with rfl | hh
268
+ · exact List.mem_of_getElem? hi
269
+ · exact lemmaSlice_sub ht h hh
270
+
271
+ theorem lemmaHyps_sub {hyps hs : List C} {l : CutLemma} (he : lemmaHyps hyps l = some hs) :
272
+ ∀ h ∈ hs, h ∈ hyps := by
273
+ cases sl : l.slice with
274
+ | none => simp only [lemmaHyps, sl, Option.some.injEq] at he; subst he; exact fun _ hh => hh
275
+ | some is => exact lemmaSlice_sub (by simpa only [lemmaHyps, sl] using he)
276
+
277
+ def CutHolds (l : CutLemma) (x : List Int) : Prop :=
278
+ (∀ w ∈ l.when, Cert.eval w x ≤ 0) → Cert.eval l.cut x ≤ 0
279
+
280
+ /-- A checked lemma proves its cut under exactly its declared linear conditions. -/
281
+ theorem lemma_sound {tab : List VDef} {hyps : List C} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
282
+ (hM : Model tab env maps x) (hH : ∀ c ∈ hyps, evalC env maps c = true)
283
+ {l : CutLemma} (hck : checkLemma tab hyps l = true) : CutHolds l x := by
284
+ intro hw
285
+ apply Int.not_lt.mp
286
+ intro hc
287
+ cases hs : lemmaHyps hyps l with
288
+ | none => simp [checkLemma, hs] at hck
289
+ | some cs =>
290
+ simp only [checkLemma, hs] at hck
291
+ have hp : PathHolds ((l.cut, false) :: l.when.map (fun w => (w, true))) x := by
292
+ intro a ha
293
+ rcases List.mem_cons.mp ha with rfl | ha
294
+ · have hn : ¬ Cert.eval l.cut x ≤ 0 := by omega
295
+ simpa using hn
296
+ · obtain ⟨w, hwm, rfl⟩ := List.mem_map.mp ha
297
+ simpa using hw w hwm
298
+ exact checkT_sound hM (fun c hc => hH c (lemmaHyps_sub hs c hc)) l.tree _ hp hck
299
+
300
+ theorem lemmaPath_holds {lemmas : List CutLemma} {path : Path} {x : List Int}
301
+ (hP : PathHolds path x) (hL : ∀ l ∈ lemmas, CutHolds l x) : PathHolds (lemmaPath lemmas path) x := by
302
+ intro a ha
303
+ rcases List.mem_append.mp ha with ha | ha
304
+ · obtain ⟨l, hl, rfl⟩ := List.mem_map.mp ha
305
+ obtain ⟨hlm, hactive⟩ := List.mem_filter.mp hl
306
+ have hc : Cert.eval l.cut x ≤ 0 := hL l hlm (fun w hw => by
307
+ have hd : dec path w = some true := by
308
+ simpa only [lemmaActive, List.all_eq_true, beq_iff_eq] using (List.all_eq_true.mp hactive w hw)
309
+ have := dec_sound hP hd
310
+ simpa using this)
311
+ simpa using hc
312
+ · exact hP a ha
313
+
314
+ theorem checkTLemmas_sound {tab : List VDef} {hyps : List C} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
315
+ (hM : Model tab env maps x) (hH : ∀ c ∈ hyps, evalC env maps c = true)
316
+ {lemmas : List CutLemma} (hL : ∀ l ∈ lemmas, CutHolds l x) :
317
+ ∀ (t : Tree) (path : Path), PathHolds path x → checkTLemmas tab hyps lemmas path t = true → False
318
+ | .split l y n, path, hP, h => by
319
+ simp only [checkTLemmas, Bool.and_eq_true] at h
320
+ by_cases hl : Cert.eval l x ≤ 0
321
+ · refine checkTLemmas_sound hM hH hL y _ ?_ h.1
322
+ intro a ha
323
+ rcases List.mem_cons.mp ha with rfl | ha
324
+ · simpa using hl
325
+ · exact hP a ha
326
+ · refine checkTLemmas_sound hM hH hL n _ ?_ h.2
327
+ intro a ha
328
+ rcases List.mem_cons.mp ha with rfl | ha
329
+ · simpa using hl
330
+ · exact hP a ha
331
+ | .leaf rows steps eqs prods, path, hP, h =>
332
+ checkT_sound hM hH _ _ (lemmaPath_holds hP hL) h
333
+ | .leafAt rows steps eqs prods, path, hP, h =>
334
+ checkT_sound hM hH _ _ (lemmaPath_holds hP hL) h
335
+ | .leafSq rows sqs steps eqs prods, path, hP, h =>
336
+ checkT_sound hM hH _ _ (lemmaPath_holds hP hL) h
337
+
338
+ /-- The extended goal checker has the original unsatisfiability conclusion. -/
339
+ theorem goalLemmas_sound (g : Goal) (t : Tree) (lemmas : List CutLemma)
340
+ (h : checkGoalLemmas g t lemmas = true) (env : Nat → Nat) (maps : Nat → Nat → Nat) :
341
+ ¬ ∀ c ∈ g.hyps, evalC env maps c = true := by
342
+ intro hH
343
+ simp only [checkGoalLemmas, Bool.and_eq_true, List.all_eq_true] at h
344
+ obtain ⟨⟨hw, hl⟩, ht⟩ := h
345
+ have hv := build_valid env maps g.tab [] [] ⟨rfl, by simp, by simp⟩ (by simpa using hw)
346
+ simp only [List.nil_append] at hv
347
+ have hM := model_of_valid hv
348
+ exact checkTLemmas_sound hM hH (fun l hm => lemma_sound hM hH (hl l hm)) t [] (by simp [PathHolds]) ht
349
+
350
+ end LemmaCuts
351
+
352
+ /-! Conditional count frames (MLDR-DESIGN.md Section 15.7). A frame cut names two counts of the goal by their
353
+ variables, a post count and its pre count, and linear conditions `when` over the goal's table. It is accepted when,
354
+ with every `if` whose condition reads no count key and is decided under `when` resolved (`simpE`), the two counts have
355
+ the same key, bound and body. No tree is checked: the two sums are equal because their bounds and summands are
356
+ (`sumBelow_congr`). Where a leaf's original split path decides every condition, the two rows `post − pre ≤ 0` and
357
+ `pre − post ≤ 0` are available to it, as a lemma cut's row is. Nothing else changes: with no frames the checkers
358
+ below are the lemma-cut checkers (`checkGoalFrames_nil`). -/
359
+
360
+ section FrameCuts
361
+ set_option autoImplicit false
362
+
363
+ structure FrameCut where
364
+ when : List Cert.Row
365
+ post : Nat
366
+ pre : Nat
367
+
368
+ /-! Section 17 (MLDR-DESIGN.md): the simplifier decides conditions that read the key from their key-free parts, prunes
369
+ decided conjuncts and disjuncts, and folds `(if C then 1 else 0) = 1` to `C`, so a step's write tree substituted into a
370
+ count's body collapses to the pre body where the step does not write the map. -/
371
+
372
+ -- A term that only rises with any variable: variables, constants, sums, scalings, products, minima, maxima.
373
+ def monoE : E → Bool
374
+ | .var _ => true
375
+ | .const _ => true
376
+ | .add a b => monoE a && monoE b
377
+ | .mul _ a => monoE a
378
+ | .min a b => monoE a && monoE b
379
+ | .max a b => monoE a && monoE b
380
+ | .prod a b => monoE a && monoE b
381
+ | _ => false
382
+
383
+ -- The variable `k` replaced by 0.
384
+ def zeroK (k : Nat) : E → E
385
+ | .var i => if i = k then .const 0 else .var i
386
+ | .const n => .const n
387
+ | .add a b => .add (zeroK k a) (zeroK k b)
388
+ | .sub a b => .sub (zeroK k a) (zeroK k b)
389
+ | .mul m a => .mul m (zeroK k a)
390
+ | .min a b => .min (zeroK k a) (zeroK k b)
391
+ | .max a b => .max (zeroK k a) (zeroK k b)
392
+ | .prod a b => .prod (zeroK k a) (zeroK k b)
393
+ | .ite c t f => .ite c (zeroK k t) (zeroK k f)
394
+ | .get m key => .get m (zeroK k key)
395
+ | .tup a b => .tup (zeroK k a) (zeroK k b)
396
+ | .proj j a => .proj j (zeroK k a)
397
+
398
+ theorem occE_zeroK (k : Nat) : (e : E) → monoE e = true → occE k (zeroK k e) = false
399
+ | .var i, _ => by simp only [zeroK]; split <;> simp [occE, *]
400
+ | .const _, _ => by simp [zeroK, occE]
401
+ | .add a b, h => by
402
+ simp only [monoE, Bool.and_eq_true] at h
403
+ simp [zeroK, occE, occE_zeroK k a h.1, occE_zeroK k b h.2]
404
+ | .mul _ a, h => by simp only [monoE] at h; simp [zeroK, occE, occE_zeroK k a h]
405
+ | .min a b, h => by
406
+ simp only [monoE, Bool.and_eq_true] at h
407
+ simp [zeroK, occE, occE_zeroK k a h.1, occE_zeroK k b h.2]
408
+ | .max a b, h => by
409
+ simp only [monoE, Bool.and_eq_true] at h
410
+ simp [zeroK, occE, occE_zeroK k a h.1, occE_zeroK k b h.2]
411
+ | .prod a b, h => by
412
+ simp only [monoE, Bool.and_eq_true] at h
413
+ simp [zeroK, occE, occE_zeroK k a h.1, occE_zeroK k b h.2]
414
+ | .sub _ _, h => by simp [monoE] at h
415
+ | .ite _ _ _, h => by simp [monoE] at h
416
+ | .get _ _, h => by simp [monoE] at h
417
+ | .tup _ _, h => by simp [monoE] at h
418
+ | .proj _ _, h => by simp [monoE] at h
419
+
420
+ theorem zeroK_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) (k : Nat) :
421
+ (e : E) → monoE e = true → evalE ρ M (zeroK k e) = evalE (upd ρ k 0) M e
422
+ | .var i, _ => by simp only [zeroK]; split <;> simp_all [evalE, upd]
423
+ | .const _, _ => rfl
424
+ | .add a b, h => by
425
+ simp only [monoE, Bool.and_eq_true] at h
426
+ simp only [zeroK, evalE, zeroK_eval ρ M k a h.1, zeroK_eval ρ M k b h.2]
427
+ | .mul m a, h => by simp only [monoE] at h; simp only [zeroK, evalE, zeroK_eval ρ M k a h]
428
+ | .min a b, h => by
429
+ simp only [monoE, Bool.and_eq_true] at h
430
+ simp only [zeroK, evalE, zeroK_eval ρ M k a h.1, zeroK_eval ρ M k b h.2]
431
+ | .max a b, h => by
432
+ simp only [monoE, Bool.and_eq_true] at h
433
+ simp only [zeroK, evalE, zeroK_eval ρ M k a h.1, zeroK_eval ρ M k b h.2]
434
+ | .prod a b, h => by
435
+ simp only [monoE, Bool.and_eq_true] at h
436
+ simp only [zeroK, evalE, zeroK_eval ρ M k a h.1, zeroK_eval ρ M k b h.2]
437
+ | .sub _ _, h => by simp [monoE] at h
438
+ | .ite _ _ _, h => by simp [monoE] at h
439
+ | .get _ _, h => by simp [monoE] at h
440
+ | .tup _ _, h => by simp [monoE] at h
441
+ | .proj _ _, h => by simp [monoE] at h
442
+
443
+ theorem monoE_ge (ρ : Nat → Nat) (M : Nat → Nat → Nat) (k : Nat) :
444
+ (e : E) → monoE e = true → evalE (upd ρ k 0) M e ≤ evalE ρ M e
445
+ | .var i, _ => by simp only [evalE, upd]; split <;> simp
446
+ | .const _, _ => Nat.le_refl _
447
+ | .add a b, h => by
448
+ simp only [monoE, Bool.and_eq_true] at h
449
+ simp only [evalE]; exact Nat.add_le_add (monoE_ge ρ M k a h.1) (monoE_ge ρ M k b h.2)
450
+ | .mul m a, h => by
451
+ simp only [monoE] at h
452
+ simp only [evalE]; exact Nat.mul_le_mul (Nat.le_refl m) (monoE_ge ρ M k a h)
453
+ | .min a b, h => by
454
+ simp only [monoE, Bool.and_eq_true] at h
455
+ simp only [evalE]
456
+ have ha := monoE_ge ρ M k a h.1; have hb := monoE_ge ρ M k b h.2; omega
457
+ | .max a b, h => by
458
+ simp only [monoE, Bool.and_eq_true] at h
459
+ simp only [evalE]
460
+ have ha := monoE_ge ρ M k a h.1; have hb := monoE_ge ρ M k b h.2; omega
461
+ | .prod a b, h => by
462
+ simp only [monoE, Bool.and_eq_true] at h
463
+ simp only [evalE]; exact Nat.mul_le_mul (monoE_ge ρ M k a h.1) (monoE_ge ρ M k b h.2)
464
+ | .sub _ _, h => by simp [monoE] at h
465
+ | .ite _ _ _, h => by simp [monoE] at h
466
+ | .get _ _, h => by simp [monoE] at h
467
+ | .tup _ _, h => by simp [monoE] at h
468
+ | .proj _ _, h => by simp [monoE] at h
469
+
470
+ -- An atom reading the key, decided for every value of the key from its key-free instance at 0: with the key only on
471
+ -- the left and the left rising with it, false at 0 is false everywhere; with the key only on the right, true at 0 is
472
+ -- true everywhere. Anything else is undecided.
473
+ def atomK (tab : List VDef) (path : Path) (k : Nat) : C → Option Bool
474
+ | .le a b =>
475
+ if occE k a && !occE k b && monoE a then
476
+ (match truth tab path (.le (zeroK k a) b) with | some false => some false | _ => none)
477
+ else if !occE k a && occE k b && monoE b then
478
+ (match truth tab path (.le a (zeroK k b)) with | some true => some true | _ => none)
479
+ else none
480
+ | .lt a b =>
481
+ if occE k a && !occE k b && monoE a then
482
+ (match truth tab path (.lt (zeroK k a) b) with | some false => some false | _ => none)
483
+ else if !occE k a && occE k b && monoE b then
484
+ (match truth tab path (.lt a (zeroK k b)) with | some true => some true | _ => none)
485
+ else none
486
+ | _ => none
487
+
488
+ -- A condition decided for every value of the key: atoms reading no key by `truth`, atoms reading it by `atomK`,
489
+ -- `and`, `or` and `not` three-valued.
490
+ def truthK (tab : List VDef) (path : Path) (k : Nat) : C → Option Bool
491
+ | .le a b => if occC k (.le a b) then atomK tab path k (.le a b) else truth tab path (.le a b)
492
+ | .lt a b => if occC k (.lt a b) then atomK tab path k (.lt a b) else truth tab path (.lt a b)
493
+ | .eq a b => if occC k (.eq a b) then
494
+ (match atomK tab path k (.le a b), atomK tab path k (.le b a) with
495
+ | some false, _ => some false
496
+ | _, some false => some false
497
+ | _, _ => none)
498
+ else truth tab path (.eq a b)
499
+ | .and a b => match truthK tab path k a, truthK tab path k b with
500
+ | some false, _ => some false
501
+ | _, some false => some false
502
+ | some true, some true => some true
503
+ | _, _ => none
504
+ | .or a b => match truthK tab path k a, truthK tab path k b with
505
+ | some true, _ => some true
506
+ | _, some true => some true
507
+ | some false, some false => some false
508
+ | _, _ => none
509
+ | .not a => match truthK tab path k a with
510
+ | some b => some (!b)
511
+ | none => none
512
+
513
+ theorem atomK_sound {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
514
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i) :
515
+ (c : C) → (b : Bool) → atomK tab path k c = some b → evalC env' maps c = b
516
+ | .le a b', r, h => by
517
+ simp only [atomK] at h
518
+ split at h
519
+ · rename_i hc
520
+ simp only [Bool.and_eq_true, Bool.not_eq_true'] at hc
521
+ obtain ⟨⟨hoa, hob⟩, hma⟩ := hc
522
+ split at h
523
+ · rename_i hT
524
+ simp only [Option.some.injEq] at h; subst h
525
+ have ht := truth_sound hM hP _ false hT
526
+ have hcg : evalC env maps (.le (zeroK k a) b') = evalC env' maps (.le (zeroK k a) b') := by
527
+ apply congrC
528
+ intro i hi
529
+ by_cases hik : i = k
530
+ · subst hik; simp [occC, occE_zeroK i a hma, hob] at hi
531
+ · exact (hk i hik).symm
532
+ rw [hcg] at ht
533
+ simp only [evalC, decide_eq_false_iff_not, Nat.not_le] at ht ⊢
534
+ have h0 := zeroK_eval env' maps k a hma
535
+ have hge := monoE_ge env' maps k a hma
536
+ omega
537
+ · simp at h
538
+ · split at h
539
+ · rename_i hc
540
+ simp only [Bool.and_eq_true, Bool.not_eq_true'] at hc
541
+ obtain ⟨⟨hoa, hob⟩, hmb⟩ := hc
542
+ split at h
543
+ · rename_i hT
544
+ simp only [Option.some.injEq] at h; subst h
545
+ have ht := truth_sound hM hP _ true hT
546
+ have hcg : evalC env maps (.le a (zeroK k b')) = evalC env' maps (.le a (zeroK k b')) := by
547
+ apply congrC
548
+ intro i hi
549
+ by_cases hik : i = k
550
+ · subst hik; simp [occC, occE_zeroK i b' hmb, hoa] at hi
551
+ · exact (hk i hik).symm
552
+ rw [hcg] at ht
553
+ simp only [evalC, decide_eq_true_eq] at ht ⊢
554
+ have h0 := zeroK_eval env' maps k b' hmb
555
+ have hge := monoE_ge env' maps k b' hmb
556
+ omega
557
+ · simp at h
558
+ · simp at h
559
+ | .lt a b', r, h => by
560
+ simp only [atomK] at h
561
+ split at h
562
+ · rename_i hc
563
+ simp only [Bool.and_eq_true, Bool.not_eq_true'] at hc
564
+ obtain ⟨⟨hoa, hob⟩, hma⟩ := hc
565
+ split at h
566
+ · rename_i hT
567
+ simp only [Option.some.injEq] at h; subst h
568
+ have ht := truth_sound hM hP _ false hT
569
+ have hcg : evalC env maps (.lt (zeroK k a) b') = evalC env' maps (.lt (zeroK k a) b') := by
570
+ apply congrC
571
+ intro i hi
572
+ by_cases hik : i = k
573
+ · subst hik; simp [occC, occE_zeroK i a hma, hob] at hi
574
+ · exact (hk i hik).symm
575
+ rw [hcg] at ht
576
+ simp only [evalC, decide_eq_false_iff_not, Nat.not_lt] at ht ⊢
577
+ have h0 := zeroK_eval env' maps k a hma
578
+ have hge := monoE_ge env' maps k a hma
579
+ omega
580
+ · simp at h
581
+ · split at h
582
+ · rename_i hc
583
+ simp only [Bool.and_eq_true, Bool.not_eq_true'] at hc
584
+ obtain ⟨⟨hoa, hob⟩, hmb⟩ := hc
585
+ split at h
586
+ · rename_i hT
587
+ simp only [Option.some.injEq] at h; subst h
588
+ have ht := truth_sound hM hP _ true hT
589
+ have hcg : evalC env maps (.lt a (zeroK k b')) = evalC env' maps (.lt a (zeroK k b')) := by
590
+ apply congrC
591
+ intro i hi
592
+ by_cases hik : i = k
593
+ · subst hik; simp [occC, occE_zeroK i b' hmb, hoa] at hi
594
+ · exact (hk i hik).symm
595
+ rw [hcg] at ht
596
+ simp only [evalC, decide_eq_true_eq] at ht ⊢
597
+ have h0 := zeroK_eval env' maps k b' hmb
598
+ have hge := monoE_ge env' maps k b' hmb
599
+ omega
600
+ · simp at h
601
+ · simp at h
602
+ | .eq _ _, _, h => by simp [atomK] at h
603
+ | .and _ _, _, h => by simp [atomK] at h
604
+ | .or _ _, _, h => by simp [atomK] at h
605
+ | .not _, _, h => by simp [atomK] at h
606
+
607
+ theorem truthK_sound {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
608
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i) :
609
+ (c : C) → (b : Bool) → truthK tab path k c = some b → evalC env' maps c = b
610
+ | .le a b', r, h => by
611
+ simp only [truthK] at h
612
+ split at h
613
+ · exact atomK_sound hM hP k env' hk _ r h
614
+ · rename_i hocc
615
+ rw [← truth_sound hM hP _ r h]
616
+ exact congrC env' env maps _ (fun i hi => hk i (fun he => by subst he; exact hocc hi))
617
+ | .lt a b', r, h => by
618
+ simp only [truthK] at h
619
+ split at h
620
+ · exact atomK_sound hM hP k env' hk _ r h
621
+ · rename_i hocc
622
+ rw [← truth_sound hM hP _ r h]
623
+ exact congrC env' env maps _ (fun i hi => hk i (fun he => by subst he; exact hocc hi))
624
+ | .eq a b', r, h => by
625
+ simp only [truthK] at h
626
+ split at h
627
+ · split at h
628
+ · rename_i hA
629
+ simp only [Option.some.injEq] at h; subst h
630
+ have := atomK_sound hM hP k env' hk _ false hA
631
+ simp only [evalC, decide_eq_false_iff_not, Nat.not_le] at this
632
+ simp only [evalC, decide_eq_false_iff_not]; omega
633
+ · rename_i hB _
634
+ simp only [Option.some.injEq] at h; subst h
635
+ have := atomK_sound hM hP k env' hk _ false hB
636
+ simp only [evalC, decide_eq_false_iff_not, Nat.not_le] at this
637
+ simp only [evalC, decide_eq_false_iff_not]; omega
638
+ · simp at h
639
+ · rename_i hocc
640
+ rw [← truth_sound hM hP _ r h]
641
+ exact congrC env' env maps _ (fun i hi => hk i (fun he => by subst he; exact hocc hi))
642
+ | .and a b', r, h => by
643
+ simp only [truthK] at h
644
+ simp only [evalC]
645
+ split at h
646
+ · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk a false h1]
647
+ · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk b' false h2]
648
+ · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk a true h1, truthK_sound hM hP k env' hk b' true h2]
649
+ · simp at h
650
+ | .or a b', r, h => by
651
+ simp only [truthK] at h
652
+ simp only [evalC]
653
+ split at h
654
+ · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk a true h1]
655
+ · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk b' true h2]
656
+ · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truthK_sound hM hP k env' hk a false h1, truthK_sound hM hP k env' hk b' false h2]
657
+ · simp at h
658
+ | .not a, r, h => by
659
+ simp only [truthK] at h
660
+ split at h
661
+ · rename_i r' h1; simp only [Option.some.injEq] at h; subst h; simp [evalC, truthK_sound hM hP k env' hk a r' h1]
662
+ · simp at h
663
+
664
+ -- The decided conditions, as terms: 0 ≤ 0 and 0 < 0.
665
+ def cTrue : C := .le (.const 0) (.const 0)
666
+ def cFalse : C := .lt (.const 0) (.const 0)
667
+ def decideC (tab : List VDef) (path : Path) (k : Nat) (c : C) : C :=
668
+ match truthK tab path k c with
669
+ | some true => cTrue
670
+ | some false => cFalse
671
+ | none => c
672
+ def andC : C → C → C
673
+ | .lt (.const 0) (.const 0), _ => cFalse
674
+ | _, .lt (.const 0) (.const 0) => cFalse
675
+ | .le (.const 0) (.const 0), y => y
676
+ | x, .le (.const 0) (.const 0) => x
677
+ | x, y => .and x y
678
+ def orC : C → C → C
679
+ | .le (.const 0) (.const 0), _ => cTrue
680
+ | _, .le (.const 0) (.const 0) => cTrue
681
+ | .lt (.const 0) (.const 0), y => y
682
+ | x, .lt (.const 0) (.const 0) => x
683
+ | x, y => .or x y
684
+ def notC : C → C
685
+ | .le (.const 0) (.const 0) => cFalse
686
+ | .lt (.const 0) (.const 0) => cTrue
687
+ | .not y => y
688
+ | y => .not y
689
+ -- `(if C then 1 else 0) = 1` is C, `= 0` is ¬C; any other equation is decided as it stands.
690
+ def foldEq (tab : List VDef) (path : Path) (k : Nat) : E → E → C
691
+ | .ite c (.const 1) (.const 0), .const 1 => c
692
+ | .ite c (.const 1) (.const 0), .const 0 => notC c
693
+ | .const 1, .ite c (.const 1) (.const 0) => c
694
+ | .const 0, .ite c (.const 1) (.const 0) => notC c
695
+ | a, b => decideC tab path k (.eq a b)
696
+
697
+ theorem decideC_eval {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
698
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i)
699
+ (c : C) : evalC env' maps (decideC tab path k c) = evalC env' maps c := by
700
+ unfold decideC
701
+ split
702
+ · rename_i hT; rw [truthK_sound hM hP k env' hk c true hT]; simp [cTrue, evalC]
703
+ · rename_i hT; rw [truthK_sound hM hP k env' hk c false hT]; simp [cFalse, evalC]
704
+ · rfl
705
+ theorem andC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) (a b : C) : evalC ρ M (andC a b) = (evalC ρ M a && evalC ρ M b) := by
706
+ unfold andC; split <;> simp_all [evalC, cTrue, cFalse]
707
+ theorem orC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) (a b : C) : evalC ρ M (orC a b) = (evalC ρ M a || evalC ρ M b) := by
708
+ unfold orC; split <;> simp_all [evalC, cTrue, cFalse]
709
+ theorem notC_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) (a : C) : evalC ρ M (notC a) = !evalC ρ M a := by
710
+ unfold notC; split <;> simp_all [evalC, cTrue, cFalse]
711
+ theorem foldEq_eval {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
712
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i)
713
+ (a b : E) : evalC env' maps (foldEq tab path k a b) = evalC env' maps (.eq a b) := by
714
+ unfold foldEq
715
+ split
716
+ · simp only [evalC, evalE]; split <;> simp_all
717
+ · rw [notC_eval]; simp only [evalC, evalE]; split <;> simp_all
718
+ · simp only [evalC, evalE]; split <;> simp_all
719
+ · rw [notC_eval]; simp only [evalC, evalE]; split <;> simp_all
720
+ · exact decideC_eval hM hP k env' hk _
721
+
722
+ mutual
723
+ -- Each `if` whose condition is decided for every value of the key `k` under the path, resolved; the terms inside
724
+ -- conditions simplified and decided parts pruned; nothing else changes.
725
+ def simpE (tab : List VDef) (path : Path) (k : Nat) : E → E
726
+ | .var i => .var i
727
+ | .const n => .const n
728
+ | .add a b => .add (simpE tab path k a) (simpE tab path k b)
729
+ | .sub a b => .sub (simpE tab path k a) (simpE tab path k b)
730
+ | .mul m a => .mul m (simpE tab path k a)
731
+ | .min a b => .min (simpE tab path k a) (simpE tab path k b)
732
+ | .max a b => .max (simpE tab path k a) (simpE tab path k b)
733
+ | .prod a b => .prod (simpE tab path k a) (simpE tab path k b)
734
+ | .ite c t f =>
735
+ match simpC tab path k c with
736
+ | .le (.const 0) (.const 0) => simpE tab path k t
737
+ | .lt (.const 0) (.const 0) => simpE tab path k f
738
+ | c' => .ite c' (simpE tab path k t) (simpE tab path k f)
739
+ | .get m key => .get m (simpE tab path k key)
740
+ | .tup a b => .tup (simpE tab path k a) (simpE tab path k b)
741
+ | .proj j a => .proj j (simpE tab path k a)
742
+ def simpC (tab : List VDef) (path : Path) (k : Nat) : C → C
743
+ | .le a b => decideC tab path k (.le (simpE tab path k a) (simpE tab path k b))
744
+ | .lt a b => decideC tab path k (.lt (simpE tab path k a) (simpE tab path k b))
745
+ | .eq a b => foldEq tab path k (simpE tab path k a) (simpE tab path k b)
746
+ | .and a b => andC (simpC tab path k a) (simpC tab path k b)
747
+ | .or a b => orC (simpC tab path k a) (simpC tab path k b)
748
+ | .not a => notC (simpC tab path k a)
749
+ end
750
+
751
+ def whenPath (f : FrameCut) : Path := f.when.map (fun w => (w, true))
752
+
753
+ -- The two counts share their key, and under `when` their bounds and bodies are the same terms.
754
+ def frameOk (tab : List VDef) (cs : List Count) (f : FrameCut) : Bool :=
755
+ match findCount cs f.post, findCount cs f.pre with
756
+ | some c₁, some c₀ => c₁.key == c₀.key
757
+ && beqE (simpE tab (whenPath f) c₁.key c₁.below) (simpE tab (whenPath f) c₁.key c₀.below)
758
+ && beqE (simpE tab (whenPath f) c₁.key c₁.body) (simpE tab (whenPath f) c₁.key c₀.body)
759
+ | _, _ => false
760
+
761
+ -- The rows a frame gives a leaf: post − pre ≤ 0 and pre − post ≤ 0, over the table's columns of the two variables.
762
+ def frameRows (tab : List VDef) (f : FrameCut) : List Cert.Row :=
763
+ match findIdx (isVar f.post) tab, findIdx (isVar f.pre) tab with
764
+ | some j₁, some j₀ => [lsub (lunit j₁) (lunit j₀), lsub (lunit j₀) (lunit j₁)]
765
+ | _, _ => []
766
+
767
+ -- Eligibility reads only the original split path.
768
+ def frameActive (path : Path) (f : FrameCut) : Bool := f.when.all (fun w => dec path w == some true)
769
+
770
+ /-! Section 17.4 (MLDR-DESIGN.md): the per-key cut. A claim `post ≤ pre` between two counts of the same key and bound,
771
+ proved for every value of the key by the kernel's own goal checker on an auxiliary goal — hypotheses: the cut's conditions
772
+ (reading no key), the key below the bound, and the pre body below the post body at the key — and lifted to the sums by
773
+ `sumBelow_mono`. Nothing new is trusted: the auxiliary goal is checked by `checkGoal`. -/
774
+
775
+ structure MonoCut where
776
+ when : List C
777
+ post : Nat
778
+ pre : Nat
779
+ tab : List VDef
780
+ tree : Tree
781
+
782
+ def monoHyps (m : MonoCut) (c₁ c₀ : Count) : List C := m.when ++ [.lt (.var c₁.key) c₁.below, .lt c₀.body c₁.body]
783
+
784
+ def monoOk (cs : List Count) (m : MonoCut) : Bool :=
785
+ match findCount cs m.post, findCount cs m.pre with
786
+ | some c₁, some c₀ => c₁.key == c₀.key && beqE c₁.below c₀.below && !occE c₁.key c₁.below
787
+ && m.when.all (fun c => !occC c₁.key c) && checkGoal ⟨m.tab, monoHyps m c₁ c₀⟩ m.tree
788
+ | _, _ => false
789
+
790
+ -- The row a cut gives a leaf: post − pre ≤ 0, over the table's columns of the two variables.
791
+ def monoRows (tab : List VDef) (m : MonoCut) : List Cert.Row :=
792
+ match findIdx (isVar m.post) tab, findIdx (isVar m.pre) tab with
793
+ | some j₁, some j₀ => [lsub (lunit j₁) (lunit j₀)]
794
+ | _, _ => []
795
+
796
+ -- Eligibility: every condition decided true on the original split path.
797
+ def monoActive (tab : List VDef) (path : Path) (m : MonoCut) : Bool := m.when.all (fun c => truth tab path c == some true)
798
+
799
+ def MonoHolds (tab : List VDef) (env : Nat → Nat) (maps : Nat → Nat → Nat) (m : MonoCut) (x : List Int) : Prop :=
800
+ (∀ c ∈ m.when, evalC env maps c = true) → ∀ r ∈ monoRows tab m, Cert.eval r x ≤ 0
801
+
802
+ /-- An accepted cut's row holds under its conditions, wherever both counts are their sums. -/
803
+ theorem mono_sound {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
804
+ (hM : Model tab env maps x) {cs : List Count} (hc : ∀ c ∈ cs, c.Holds env maps)
805
+ {m : MonoCut} (hok : monoOk cs m = true) : MonoHolds tab env maps m x := by
806
+ intro hw r hr
807
+ simp only [monoOk] at hok
808
+ split at hok
809
+ · rename_i c₁ c₀ h₁ h₀
810
+ simp only [Bool.and_eq_true, beq_iff_eq, Bool.not_eq_true', List.all_eq_true] at hok
811
+ obtain ⟨⟨⟨⟨hkey, hbelow⟩, hob⟩, hwhen⟩, hchk⟩ := hok
812
+ have hb := beqE_eq hbelow
813
+ have hm₁ := lookup_some h₁
814
+ have hm₀ := lookup_some h₀
815
+ have hv₁ : c₁.v = m.post := by simpa using hm₁.2
816
+ have hv₀ : c₀.v = m.pre := by simpa using hm₀.2
817
+ have H₁ : env c₁.v = sumBelow (fun kk => evalE (upd env c₁.key kk) maps c₁.body) (evalE env maps c₁.below) := hc c₁ hm₁.1
818
+ have H₀ : env c₀.v = sumBelow (fun kk => evalE (upd env c₀.key kk) maps c₀.body) (evalE env maps c₀.below) := hc c₀ hm₀.1
819
+ have hpt : ∀ kk, kk < evalE env maps c₁.below →
820
+ evalE (upd env c₁.key kk) maps c₁.body ≤ evalE (upd env c₁.key kk) maps c₀.body := by
821
+ intro kk hkk
822
+ apply Classical.byContradiction
823
+ intro hnot
824
+ have hlt := Nat.lt_of_not_le hnot
825
+ apply sound ⟨m.tab, monoHyps m c₁ c₀⟩ m.tree hchk (upd env c₁.key kk) maps
826
+ intro c hcm
827
+ simp only [monoHyps, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hcm
828
+ rcases hcm with hcm | rfl | rfl
829
+ · have hk : ∀ i, occC i c = true → upd env c₁.key kk i = env i := fun i hi => by
830
+ have hne : i ≠ c₁.key := fun he => by subst he; simp [hwhen c hcm] at hi
831
+ simp [upd, hne]
832
+ rw [congrC (upd env c₁.key kk) env maps c hk]
833
+ exact hw c hcm
834
+ · have hk : ∀ i, occE i c₁.below = true → upd env c₁.key kk i = env i := fun i hi => by
835
+ have hne : i ≠ c₁.key := fun he => by subst he; simp [hob] at hi
836
+ simp [upd, hne]
837
+ simp only [evalC, evalE, decide_eq_true_eq]
838
+ rw [congrE (upd env c₁.key kk) env maps c₁.below hk]
839
+ simpa [upd] using hkk
840
+ · simp only [evalC, decide_eq_true_eq]; exact hlt
841
+ have hle : env c₁.v ≤ env c₀.v := by
842
+ rw [H₁, H₀, ← hb, ← hkey]
843
+ exact sumBelow_mono _ hpt
844
+ simp only [monoRows] at hr
845
+ split at hr
846
+ · rename_i j₁ j₀ hj₁ hj₀
847
+ have e₁ := var_col hM hj₁
848
+ have e₀ := var_col hM hj₀
849
+ simp only [List.mem_singleton] at hr
850
+ subst hr
851
+ rw [eval_lsub, eval_lunit, eval_lunit, e₁, e₀]
852
+ rw [hv₁, hv₀] at hle
853
+ omega
854
+ · simp at hr
855
+ · simp at hok
856
+
857
+ /-! Section 17.5 (MLDR-DESIGN.md): the linear per-key cut. A claim Σ pos m·x_v ≤ Σ neg m·x_v over counts of one key and
858
+ bound, proved for every value of the key by `checkGoal` on the auxiliary goal — the cut's conditions (reading no key), the key
859
+ below the bound, and Σ neg m·body_v(k) < Σ pos m·body_v(k) — and lifted to the sums by the linearity of `sumBelow`. The per-key
860
+ cut (MonoCut) is its one-term-a-side case. -/
861
+
862
+ structure LinCut where
863
+ when : List C
864
+ pos : List (Nat × Nat) -- (coefficient, count variable)
865
+ neg : List (Nat × Nat)
866
+ tab : List VDef
867
+ tree : Tree
868
+
869
+ -- The named counts with their coefficients, in order, or none.
870
+ def linCounts (cs : List Count) : List (Nat × Nat) → Option (List (Nat × Count))
871
+ | [] => some []
872
+ | (m, v) :: rest => match findCount cs v, linCounts cs rest with
873
+ | some c, some tl => some ((m, c) :: tl)
874
+ | _, _ => none
875
+
876
+ -- Σ m·body, as a term; the counts share their key, so every body reads it as written.
877
+ def linTerm : List (Nat × Count) → E
878
+ | [] => .const 0
879
+ | (m, c) :: rest => .add (.mul m c.body) (linTerm rest)
880
+
881
+ -- Σ m·x_v as a row over the table's columns of the variables, or none.
882
+ def linRow (tab : List VDef) : List (Nat × Nat) → Option Cert.Row
883
+ | [] => some (lconst 0)
884
+ | (m, v) :: rest => match findIdx (isVar v) tab, linRow tab rest with
885
+ | some j, some r => some (ladd (lscale (m : Int) (lunit j)) r)
886
+ | _, _ => none
887
+
888
+ def sameShape (c₀ : Count) (xs : List (Nat × Count)) : Bool := xs.all (fun p => p.2.key == c₀.key && beqE p.2.below c₀.below)
889
+
890
+ def linHyps (l : LinCut) (c₀ : Count) (ps ns : List (Nat × Count)) : List C :=
891
+ l.when ++ [.lt (.var c₀.key) c₀.below, .lt (linTerm ns) (linTerm ps)]
892
+
893
+ def linOk (cs : List Count) (l : LinCut) : Bool :=
894
+ match linCounts cs l.pos, linCounts cs l.neg with
895
+ | some ps, some ns => match ps ++ ns with
896
+ | [] => false
897
+ | (_, c₀) :: _ => sameShape c₀ ps && sameShape c₀ ns && !occE c₀.key c₀.below && l.when.all (fun c => !occC c₀.key c)
898
+ && checkGoal ⟨l.tab, linHyps l c₀ ps ns⟩ l.tree
899
+ | _, _ => false
900
+
901
+ def linRows (tab : List VDef) (l : LinCut) : List Cert.Row :=
902
+ match linRow tab l.pos, linRow tab l.neg with
903
+ | some rp, some rn => [lsub rp rn]
904
+ | _, _ => []
905
+
906
+ def linActive (tab : List VDef) (path : Path) (l : LinCut) : Bool := l.when.all (fun c => truth tab path c == some true)
907
+
908
+ def LinHolds (tab : List VDef) (env : Nat → Nat) (maps : Nat → Nat → Nat) (l : LinCut) (x : List Int) : Prop :=
909
+ (∀ c ∈ l.when, evalC env maps c = true) → ∀ r ∈ linRows tab l, Cert.eval r x ≤ 0
910
+
911
+ -- Σ m·(sum of the body below the bound), in Nat, and Σ m·(the variable), in Int.
912
+ def linSumN (env : Nat → Nat) (maps : Nat → Nat → Nat) (key n : Nat) : List (Nat × Count) → Nat
913
+ | [] => 0
914
+ | (m, c) :: rest => m * sumBelow (fun kk => evalE (upd env key kk) maps c.body) n + linSumN env maps key n rest
915
+ def linSumZ (env : Nat → Nat) : List (Nat × Nat) → Int
916
+ | [] => 0
917
+ | (m, v) :: rest => (m : Int) * ((env v : Nat) : Int) + linSumZ env rest
918
+
919
+ theorem linTerm_eval (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ xs : List (Nat × Count),
920
+ evalE ρ M (linTerm xs) = (xs.map (fun p => p.1 * evalE ρ M p.2.body)).sum
921
+ | [] => by simp [linTerm, evalE]
922
+ | (m, c) :: rest => by simp [linTerm, evalE, linTerm_eval ρ M rest]
923
+
924
+ theorem sumBelow_linTerm (env : Nat → Nat) (maps : Nat → Nat → Nat) (key n : Nat) : ∀ xs : List (Nat × Count),
925
+ sumBelow (fun kk => evalE (upd env key kk) maps (linTerm xs)) n = linSumN env maps key n xs
926
+ | [] => by simp [linTerm, evalE, linSumN, sumBelow_zero_fn]
927
+ | (m, c) :: rest => by
928
+ have h : (fun kk => evalE (upd env key kk) maps (linTerm ((m, c) :: rest)))
929
+ = fun kk => m * evalE (upd env key kk) maps c.body + evalE (upd env key kk) maps (linTerm rest) := by
930
+ funext kk; simp [linTerm, evalE]
931
+ rw [h, sumBelow_add, sumBelow_mul, sumBelow_linTerm env maps key n rest]; rfl
932
+
933
+ -- The row's value is Σ m·x[j] = Σ m·env v.
934
+ theorem linRow_eval {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} (hM : Model tab env maps x) :
935
+ ∀ (xs : List (Nat × Nat)) (r : Cert.Row), linRow tab xs = some r → Cert.eval r x = linSumZ env xs
936
+ | [], r, h => by simp only [linRow, Option.some.injEq] at h; subst h; simp [linSumZ, eval_lconst]
937
+ | (m, v) :: rest, r, h => by
938
+ simp only [linRow] at h
939
+ split at h
940
+ · rename_i j r' hj hr
941
+ simp only [Option.some.injEq] at h; subst h
942
+ rw [eval_ladd, eval_lscale, eval_lunit, var_col hM hj, linRow_eval hM rest r' hr]; rfl
943
+ · simp at h
944
+
945
+ -- With every named count its sum (of the shared key and bound), Σ m·env v is Σ m·(its body summed below the bound).
946
+ theorem linSum_holds {env : Nat → Nat} {maps : Nat → Nat → Nat} {cs : List Count} (hc : ∀ c ∈ cs, c.Holds env maps) (c₀ : Count) :
947
+ ∀ (vs : List (Nat × Nat)) (ps : List (Nat × Count)), linCounts cs vs = some ps → sameShape c₀ ps = true →
948
+ linSumZ env vs = ((linSumN env maps c₀.key (evalE env maps c₀.below) ps : Nat) : Int)
949
+ | [], ps, h, _ => by simp only [linCounts, Option.some.injEq] at h; subst h; simp [linSumZ, linSumN]
950
+ | (m, v) :: rest, ps, h, hs => by
951
+ simp only [linCounts] at h
952
+ split at h
953
+ · rename_i c tl hfind htl
954
+ simp only [Option.some.injEq] at h; subst h
955
+ simp only [sameShape, List.all_cons, Bool.and_eq_true, beq_iff_eq] at hs
956
+ obtain ⟨⟨hkey, hbelow⟩, hrest⟩ := hs
957
+ have hb := beqE_eq hbelow
958
+ have hm := lookup_some hfind
959
+ have hv : c.v = v := by simpa using hm.2
960
+ have H : env c.v = sumBelow (fun kk => evalE (upd env c.key kk) maps c.body) (evalE env maps c.below) := hc c hm.1
961
+ simp only [linSumZ, linSumN]
962
+ rw [← hv, H, hkey, hb, linSum_holds hc c₀ rest tl htl (by simpa [sameShape] using hrest)]
963
+ simp [Int.natCast_add, Int.natCast_mul]
964
+ · simp at h
965
+
966
+ /-- An accepted linear cut's row holds under its conditions, wherever the named counts are their sums. -/
967
+ theorem lin_sound {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
968
+ (hM : Model tab env maps x) {cs : List Count} (hc : ∀ c ∈ cs, c.Holds env maps)
969
+ {l : LinCut} (hok : linOk cs l = true) : LinHolds tab env maps l x := by
970
+ intro hw r hr
971
+ simp only [linOk] at hok
972
+ split at hok
973
+ · rename_i ps ns hps hns
974
+ split at hok
975
+ · simp at hok
976
+ · rename_i c₀ tl hcons
977
+ simp only [Bool.and_eq_true, Bool.not_eq_true', List.all_eq_true] at hok
978
+ obtain ⟨⟨⟨⟨hsp, hsn⟩, hob⟩, hwhen⟩, hchk⟩ := hok
979
+ -- pointwise: Σ pos ≤ Σ neg at every key below the bound
980
+ have hpt : ∀ kk, kk < evalE env maps c₀.below →
981
+ evalE (upd env c₀.key kk) maps (linTerm ps) ≤ evalE (upd env c₀.key kk) maps (linTerm ns) := by
982
+ intro kk hkk
983
+ apply Classical.byContradiction
984
+ intro hnot
985
+ have hlt := Nat.lt_of_not_le hnot
986
+ apply sound ⟨l.tab, linHyps l c₀ ps ns⟩ l.tree hchk (upd env c₀.key kk) maps
987
+ intro c hcm
988
+ simp only [linHyps, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hcm
989
+ rcases hcm with hcm | rfl | rfl
990
+ · have hk : ∀ i, occC i c = true → upd env c₀.key kk i = env i := fun i hi => by
991
+ have hne : i ≠ c₀.key := fun he => by subst he; simp [hwhen c hcm] at hi
992
+ simp [upd, hne]
993
+ rw [congrC (upd env c₀.key kk) env maps c hk]
994
+ exact hw c hcm
995
+ · have hk : ∀ i, occE i c₀.below = true → upd env c₀.key kk i = env i := fun i hi => by
996
+ have hne : i ≠ c₀.key := fun he => by subst he; simp [hob] at hi
997
+ simp [upd, hne]
998
+ simp only [evalC, evalE, decide_eq_true_eq]
999
+ rw [congrE (upd env c₀.key kk) env maps c₀.below hk]
1000
+ simpa [upd] using hkk
1001
+ · simp only [evalC, decide_eq_true_eq]; exact hlt
1002
+ have hsum := sumBelow_mono (evalE env maps c₀.below) hpt
1003
+ rw [sumBelow_linTerm, sumBelow_linTerm] at hsum
1004
+ -- the row
1005
+ simp only [linRows] at hr
1006
+ split at hr
1007
+ · rename_i rp rn hrp hrn
1008
+ simp only [List.mem_singleton] at hr
1009
+ subst hr
1010
+ rw [eval_lsub, linRow_eval hM l.pos rp hrp, linRow_eval hM l.neg rn hrn,
1011
+ linSum_holds hc c₀ l.pos ps hps hsp, linSum_holds hc c₀ l.neg ns hns hsn]
1012
+ have hz : ((linSumN env maps c₀.key (evalE env maps c₀.below) ps : Nat) : Int) ≤ ((linSumN env maps c₀.key (evalE env maps c₀.below) ns : Nat) : Int) := Int.ofNat_le.mpr hsum
1013
+ omega
1014
+ · simp at hr
1015
+ · simp at hok
1016
+
1017
+ -- The active frames' rows in front of the lemma path.
1018
+ def framePath (tab : List VDef) (frames : List FrameCut) (monos : List MonoCut) (lemmas : List CutLemma) (path : Path) (lins : List LinCut := []) : Path :=
1019
+ (lins.filter (linActive tab path)).flatMap (fun l => (linRows tab l).map (fun r => (r, true)))
1020
+ ++ ((monos.filter (monoActive tab path)).flatMap (fun m => (monoRows tab m).map (fun r => (r, true)))
1021
+ ++ ((frames.filter (frameActive path)).flatMap (fun f => (frameRows tab f).map (fun r => (r, true))) ++ lemmaPath lemmas path))
1022
+
1023
+ def checkTFrames (tab : List VDef) (hyps : List C) (lemmas : List CutLemma) (frames : List FrameCut) (monos : List MonoCut) (lins : List LinCut) : Path → Tree → Bool
1024
+ | path, .split l y n => checkTFrames tab hyps lemmas frames monos lins ((l, true) :: path) y && checkTFrames tab hyps lemmas frames monos lins ((l, false) :: path) n
1025
+ | path, t => checkT tab hyps (framePath tab frames monos lemmas path lins) t
1026
+
1027
+ def checkGoalFrames (g : Goal) (cs : List Count) (t : Tree) (lemmas : List CutLemma) (frames : List FrameCut) (monos : List MonoCut := []) (lins : List LinCut := []) : Bool :=
1028
+ wfTab 0 g.tab && lemmas.all (checkLemma g.tab g.hyps) && frames.all (frameOk g.tab cs) && monos.all (monoOk cs) && lins.all (linOk cs) && checkTFrames g.tab g.hyps lemmas frames monos lins [] t
1029
+
1030
+ @[simp] theorem framePath_nil (tab : List VDef) (lemmas : List CutLemma) (path : Path) : framePath tab [] [] lemmas path [] = lemmaPath lemmas path := rfl
1031
+
1032
+ @[simp] theorem checkTFrames_nil (tab : List VDef) (hyps : List C) (lemmas : List CutLemma) (t : Tree) (path : Path) :
1033
+ checkTFrames tab hyps lemmas [] [] [] path t = checkTLemmas tab hyps lemmas path t := by
1034
+ induction t generalizing path with
1035
+ | split l y n ihy ihn => simp only [checkTFrames, checkTLemmas, ihy, ihn]
1036
+ | leaf rows steps eqs prods => rfl
1037
+ | leafAt rows steps eqs prods => rfl
1038
+ | leafSq rows sqs steps eqs prods => rfl
1039
+
1040
+ @[simp] theorem checkGoalFrames_nil (g : Goal) (cs : List Count) (t : Tree) (lemmas : List CutLemma) :
1041
+ checkGoalFrames g cs t lemmas [] = checkGoalLemmas g t lemmas := by
1042
+ simp [checkGoalFrames, checkGoalLemmas]
1043
+
1044
+ mutual
1045
+ /-- Resolving decided conditions keeps a term's value, at any environment that differs from the model's only at the
1046
+ key. -/
1047
+ theorem simpE_eval {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
1048
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i) :
1049
+ (e : E) → evalE env' maps (simpE tab path k e) = evalE env' maps e
1050
+ | .var i => rfl
1051
+ | .const n => rfl
1052
+ | .add a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1053
+ | .sub a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1054
+ | .mul m a => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a]
1055
+ | .min a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1056
+ | .max a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1057
+ | .prod a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1058
+ | .get m key => by simp only [simpE, evalE, simpE_eval hM hP k env' hk key]
1059
+ | .tup a b => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a, simpE_eval hM hP k env' hk b]
1060
+ | .proj j a => by simp only [simpE, evalE, simpE_eval hM hP k env' hk a]
1061
+ | .ite c t f => by
1062
+ have ht := simpE_eval hM hP k env' hk t
1063
+ have hf := simpE_eval hM hP k env' hk f
1064
+ have hc := simpC_eval hM hP k env' hk c
1065
+ simp only [simpE]
1066
+ split
1067
+ · rename_i heq
1068
+ rw [heq] at hc
1069
+ have hc' : evalC env' maps c = true := by simpa [evalC] using hc.symm
1070
+ simp only [evalE, hc', ite_true, ht]
1071
+ · rename_i heq
1072
+ rw [heq] at hc
1073
+ have hc' : evalC env' maps c = false := by simpa [evalC] using hc.symm
1074
+ simp only [evalE, hc', Bool.false_eq_true, ite_false, hf]
1075
+ · simp only [evalE, hc, ht, hf]
1076
+ theorem simpC_eval {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
1077
+ (hM : Model tab env maps x) (hP : PathHolds path x) (k : Nat) (env' : Nat → Nat) (hk : ∀ i, i ≠ k → env' i = env i) :
1078
+ (c : C) → evalC env' maps (simpC tab path k c) = evalC env' maps c
1079
+ | .le a b => by
1080
+ have ha := simpE_eval hM hP k env' hk a
1081
+ have hb := simpE_eval hM hP k env' hk b
1082
+ simp only [simpC]; rw [decideC_eval hM hP k env' hk]; simp only [evalC, ha, hb]
1083
+ | .lt a b => by
1084
+ have ha := simpE_eval hM hP k env' hk a
1085
+ have hb := simpE_eval hM hP k env' hk b
1086
+ simp only [simpC]; rw [decideC_eval hM hP k env' hk]; simp only [evalC, ha, hb]
1087
+ | .eq a b => by
1088
+ have ha := simpE_eval hM hP k env' hk a
1089
+ have hb := simpE_eval hM hP k env' hk b
1090
+ simp only [simpC]; rw [foldEq_eval hM hP k env' hk]; simp only [evalC, ha, hb]
1091
+ | .and a b => by simp only [simpC, andC_eval, evalC, simpC_eval hM hP k env' hk a, simpC_eval hM hP k env' hk b]
1092
+ | .or a b => by simp only [simpC, orC_eval, evalC, simpC_eval hM hP k env' hk a, simpC_eval hM hP k env' hk b]
1093
+ | .not a => by simp only [simpC, notC_eval, evalC, simpC_eval hM hP k env' hk a]
1094
+ end
1095
+
1096
+ def FrameHolds (tab : List VDef) (f : FrameCut) (x : List Int) : Prop :=
1097
+ (∀ w ∈ f.when, Cert.eval w x ≤ 0) → ∀ r ∈ frameRows tab f, Cert.eval r x ≤ 0
1098
+
1099
+ /-- An accepted frame's rows hold under its conditions, wherever both counts are their sums. -/
1100
+ theorem frame_sound {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
1101
+ (hM : Model tab env maps x) {cs : List Count} (hc : ∀ c ∈ cs, c.Holds env maps)
1102
+ {f : FrameCut} (hok : frameOk tab cs f = true) : FrameHolds tab f x := by
1103
+ intro hw
1104
+ have hP : PathHolds (whenPath f) x := by
1105
+ intro a ha
1106
+ obtain ⟨w, hwm, rfl⟩ := List.mem_map.mp ha
1107
+ simpa using hw w hwm
1108
+ simp only [frameOk] at hok
1109
+ split at hok
1110
+ · rename_i c₁ c₀ h₁ h₀
1111
+ simp only [Bool.and_eq_true, beq_iff_eq] at hok
1112
+ obtain ⟨⟨hkey, hbelow⟩, hbody⟩ := hok
1113
+ have hb := beqE_eq hbelow
1114
+ have hd := beqE_eq hbody
1115
+ have hm₁ := lookup_some h₁
1116
+ have hm₀ := lookup_some h₀
1117
+ have hv₁ : c₁.v = f.post := by simpa using hm₁.2
1118
+ have hv₀ : c₀.v = f.pre := by simpa using hm₀.2
1119
+ have H₁ : env c₁.v = sumBelow (fun kk => evalE (upd env c₁.key kk) maps c₁.body) (evalE env maps c₁.below) := hc c₁ hm₁.1
1120
+ have H₀ : env c₀.v = sumBelow (fun kk => evalE (upd env c₀.key kk) maps c₀.body) (evalE env maps c₀.below) := hc c₀ hm₀.1
1121
+ have heq : env f.post = env f.pre := by
1122
+ rw [← hv₁, ← hv₀, H₁, H₀]
1123
+ have eb : evalE env maps c₁.below = evalE env maps c₀.below := by
1124
+ rw [← simpE_eval hM hP c₁.key env (fun _ _ => rfl) c₁.below, hb, simpE_eval hM hP c₁.key env (fun _ _ => rfl) c₀.below]
1125
+ rw [eb]
1126
+ apply sumBelow_congr
1127
+ intro kk _
1128
+ have hk' : ∀ i, i ≠ c₁.key → upd env c₁.key kk i = env i := fun i hi => by simp [upd, hi]
1129
+ rw [← simpE_eval hM hP c₁.key (upd env c₁.key kk) hk' c₁.body, hd, simpE_eval hM hP c₁.key (upd env c₁.key kk) hk' c₀.body, hkey]
1130
+ intro r hr
1131
+ simp only [frameRows] at hr
1132
+ split at hr
1133
+ · rename_i j₁ j₀ hj₁ hj₀
1134
+ have e₁ := var_col hM hj₁
1135
+ have e₀ := var_col hM hj₀
1136
+ simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1137
+ rcases hr with rfl | rfl
1138
+ · rw [eval_lsub, eval_lunit, eval_lunit, e₁, e₀, heq]; omega
1139
+ · rw [eval_lsub, eval_lunit, eval_lunit, e₁, e₀, heq]; omega
1140
+ · simp at hr
1141
+ · simp at hok
1142
+
1143
+ theorem framePath_holds {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {frames : List FrameCut} {monos : List MonoCut}
1144
+ {lins : List LinCut} {lemmas : List CutLemma} {path : Path} {x : List Int}
1145
+ (hM : Model tab env maps x) (hP : PathHolds path x) (hL : ∀ l ∈ lemmas, CutHolds l x) (hF : ∀ f ∈ frames, FrameHolds tab f x)
1146
+ (hMo : ∀ m ∈ monos, MonoHolds tab env maps m x) (hLi : ∀ l ∈ lins, LinHolds tab env maps l x) :
1147
+ PathHolds (framePath tab frames monos lemmas path lins) x := by
1148
+ intro a ha
1149
+ rcases List.mem_append.mp ha with ha | ha
1150
+ · obtain ⟨l, hl, ha⟩ := List.mem_flatMap.mp ha
1151
+ obtain ⟨hlm, hactive⟩ := List.mem_filter.mp hl
1152
+ obtain ⟨r, hr, rfl⟩ := List.mem_map.mp ha
1153
+ have hwhen : ∀ c ∈ l.when, evalC env maps c = true := fun c hc => by
1154
+ have ht : truth tab path c = some true := by
1155
+ simpa only [linActive, List.all_eq_true, beq_iff_eq] using (List.all_eq_true.mp hactive c hc)
1156
+ exact truth_sound hM hP c true ht
1157
+ simpa using hLi l hlm hwhen r hr
1158
+ rcases List.mem_append.mp ha with ha | ha
1159
+ · obtain ⟨m, hm, ha⟩ := List.mem_flatMap.mp ha
1160
+ obtain ⟨hmm, hactive⟩ := List.mem_filter.mp hm
1161
+ obtain ⟨r, hr, rfl⟩ := List.mem_map.mp ha
1162
+ have hwhen : ∀ c ∈ m.when, evalC env maps c = true := fun c hc => by
1163
+ have ht : truth tab path c = some true := by
1164
+ simpa only [monoActive, List.all_eq_true, beq_iff_eq] using (List.all_eq_true.mp hactive c hc)
1165
+ exact truth_sound hM hP c true ht
1166
+ simpa using hMo m hmm hwhen r hr
1167
+ rcases List.mem_append.mp ha with ha | ha
1168
+ · obtain ⟨f, hf, ha⟩ := List.mem_flatMap.mp ha
1169
+ obtain ⟨hfm, hactive⟩ := List.mem_filter.mp hf
1170
+ obtain ⟨r, hr, rfl⟩ := List.mem_map.mp ha
1171
+ have hwhen : ∀ w ∈ f.when, Cert.eval w x ≤ 0 := fun w hw => by
1172
+ have hd : dec path w = some true := by
1173
+ simpa only [frameActive, List.all_eq_true, beq_iff_eq] using (List.all_eq_true.mp hactive w hw)
1174
+ have := dec_sound hP hd
1175
+ simpa using this
1176
+ simpa using hF f hfm hwhen r hr
1177
+ · exact lemmaPath_holds hP hL a ha
1178
+
1179
+ theorem checkTFrames_sound {tab : List VDef} {hyps : List C} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
1180
+ (hM : Model tab env maps x) (hH : ∀ c ∈ hyps, evalC env maps c = true)
1181
+ {lemmas : List CutLemma} (hL : ∀ l ∈ lemmas, CutHolds l x) {frames : List FrameCut} (hF : ∀ f ∈ frames, FrameHolds tab f x)
1182
+ {monos : List MonoCut} (hMo : ∀ m ∈ monos, MonoHolds tab env maps m x) {lins : List LinCut} (hLi : ∀ l ∈ lins, LinHolds tab env maps l x) :
1183
+ ∀ (t : Tree) (path : Path), PathHolds path x → checkTFrames tab hyps lemmas frames monos lins path t = true → False
1184
+ | .split l y n, path, hP, h => by
1185
+ simp only [checkTFrames, Bool.and_eq_true] at h
1186
+ by_cases hl : Cert.eval l x ≤ 0
1187
+ · refine checkTFrames_sound hM hH hL hF hMo hLi y _ ?_ h.1
1188
+ intro a ha
1189
+ rcases List.mem_cons.mp ha with rfl | ha
1190
+ · simpa using hl
1191
+ · exact hP a ha
1192
+ · refine checkTFrames_sound hM hH hL hF hMo hLi n _ ?_ h.2
1193
+ intro a ha
1194
+ rcases List.mem_cons.mp ha with rfl | ha
1195
+ · simpa using hl
1196
+ · exact hP a ha
1197
+ | .leaf rows steps eqs prods, path, hP, h =>
1198
+ checkT_sound hM hH _ _ (framePath_holds hM hP hL hF hMo hLi) h
1199
+ | .leafAt rows steps eqs prods, path, hP, h =>
1200
+ checkT_sound hM hH _ _ (framePath_holds hM hP hL hF hMo hLi) h
1201
+ | .leafSq rows sqs steps eqs prods, path, hP, h =>
1202
+ checkT_sound hM hH _ _ (framePath_holds hM hP hL hF hMo hLi) h
1203
+
1204
+ /-- The goal checker with frames has the original unsatisfiability conclusion, wherever the named counts are their
1205
+ sums. -/
1206
+ theorem goalFrames_sound (g : Goal) (cs : List Count) (t : Tree) (lemmas : List CutLemma) (frames : List FrameCut) (monos : List MonoCut) (lins : List LinCut)
1207
+ (h : checkGoalFrames g cs t lemmas frames monos lins = true) (env : Nat → Nat) (maps : Nat → Nat → Nat)
1208
+ (hc : ∀ c ∈ cs, c.Holds env maps) : ¬ ∀ c ∈ g.hyps, evalC env maps c = true := by
1209
+ intro hH
1210
+ simp only [checkGoalFrames, Bool.and_eq_true, List.all_eq_true] at h
1211
+ obtain ⟨⟨⟨⟨⟨hw, hl⟩, hf⟩, hmo⟩, hli⟩, ht⟩ := h
1212
+ have hv := build_valid env maps g.tab [] [] ⟨rfl, by simp, by simp⟩ (by simpa using hw)
1213
+ simp only [List.nil_append] at hv
1214
+ have hM := model_of_valid hv
1215
+ exact checkTFrames_sound hM hH (fun l hm => lemma_sound hM hH (hl l hm)) (fun f hm => frame_sound hM hc (hf f hm)) (fun m hm => mono_sound hM hc (hmo m hm)) (fun l hm => lin_sound hM hc (hli l hm)) t [] (by simp [PathHolds]) ht
1216
+
1217
+ end FrameCuts
1218
+
1219
+ -- The check: fresh witnesses, every instance built, then the same goal extended by checked conditional cuts.
1220
+ def checkCounted (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) (lemmas : List CutLemma := []) : Bool :=
1221
+ countedOk g.hyps g.counts is && match countHyps g.counts is with
1222
+ | some hs => checkGoalLemmas ⟨tab, g.hyps ++ hs⟩ t lemmas
1223
+ | none => false
1224
+
1225
+ /-- With no lemma entries the old counted check is unchanged. -/
1226
+ theorem checkCounted_nil (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) :
1227
+ checkCounted g is tab t [] = (countedOk g.hyps g.counts is && match countHyps g.counts is with
1228
+ | some hs => checkGoal ⟨tab, g.hyps ++ hs⟩ t
1229
+ | none => false) := by
1230
+ simp only [checkCounted, checkGoalLemmas_nil]
1231
+
1232
+ /-! Soundness -/
1233
+
1234
+ theorem cDistinct_cons {a : Nat} {as : List Nat} (h : cDistinct (a :: as) = true) : a ∉ as ∧ cDistinct as = true := by
1235
+ simp only [cDistinct, Bool.and_eq_true] at h
1236
+ refine ⟨fun hm => ?_, h.2⟩
1237
+ have h1 := h.1
1238
+ simp at h1
1239
+ exact h1 hm
1240
+
1241
+ theorem applyS_one (k : Nat) (t : E) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : applyS [(k, t)] ρ M = upd ρ k (evalE ρ M t) := by
1242
+ funext i
1243
+ simp only [applyS, assoc, lookup, upd]
1244
+ by_cases h : i = k
1245
+ · subst h; simp
1246
+ · have hb : (k == i) = false := by simp; omega
1247
+ simp [hb, h]
1248
+
1249
+ theorem resolve_mem {cs : List Count} {B : E} : ∀ {ts : List (Int × Nat)} {rs : List (Int × Count)},
1250
+ resolve cs B ts = some rs → ∀ r ∈ rs, r.2 ∈ cs ∧ r.2.below = B
1251
+ | [], rs, h => by simp only [resolve, Option.some.injEq] at h; subst h; simp
1252
+ | t :: ts, rs, h => by
1253
+ cases hc : findCount cs t.2 with
1254
+ | none => simp [resolve, hc] at h
1255
+ | some c =>
1256
+ by_cases hb : beqE c.below B = true
1257
+ · cases hr : resolve cs B ts with
1258
+ | none => simp [resolve, hc, hb, hr] at h
1259
+ | some rs' =>
1260
+ simp [resolve, hc, hb, hr] at h
1261
+ subst h
1262
+ intro r hr'
1263
+ rcases List.mem_cons.mp hr' with rfl | hr'
1264
+ · exact ⟨(lookup_some hc).1, beqE_eq hb⟩
1265
+ · exact resolve_mem hr r hr'
1266
+ · simp [resolve, hc, hb] at h
1267
+
1268
+ theorem countHyps_mem {cs : List Count} : ∀ {is : List CInst} {hs : List C}, countHyps cs is = some hs → ∀ h ∈ hs, ∃ i ∈ is, countHyp cs i = some h
1269
+ | [], hs, hh => by simp only [countHyps, Option.some.injEq] at hh; subst hh; simp
1270
+ | i :: is, hs, hh => by
1271
+ cases e1 : countHyp cs i with
1272
+ | none => simp [countHyps, e1] at hh
1273
+ | some h1 =>
1274
+ cases e2 : countHyps cs is with
1275
+ | none => simp [countHyps, e1, e2] at hh
1276
+ | some hs' =>
1277
+ simp [countHyps, e1, e2] at hh
1278
+ subst hh
1279
+ intro h hm
1280
+ rcases List.mem_cons.mp hm with rfl | hm
1281
+ · exact ⟨i, List.mem_cons_self, e1⟩
1282
+ · obtain ⟨j, hj, hj'⟩ := countHyps_mem e2 h hm
1283
+ exact ⟨j, List.mem_cons_of_mem _ hj, hj'⟩
1284
+
1285
+ -- The two sides, evaluated: the counts' combination, and the bodies' at a key.
1286
+ def sumSide (part : Int → Nat) (ρ : Nat → Nat) : List (Int × Count) → Nat
1287
+ | [] => 0
1288
+ | r :: rs => part r.1 * ρ r.2.v + sumSide part ρ rs
1289
+ def ptSide (part : Int → Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : List (Int × Count) → Nat → Nat
1290
+ | [], _ => 0
1291
+ | r :: rs, k => part r.1 * evalE (upd ρ r.2.key k) M r.2.body + ptSide part ρ M rs k
1292
+
1293
+ theorem eval_sumE_count (part : Int → Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : ∀ rs : List (Int × Count),
1294
+ evalE ρ M (sumE (rs.map fun r => .mul (part r.1) (atCount r.2))) = sumSide part ρ rs
1295
+ | [] => by simp [sumE, evalE, sumSide]
1296
+ | r :: rs => by
1297
+ simp only [List.map_cons, sumE, evalE, eval_sumE_count part ρ M rs]
1298
+ simp [atCount, evalE, sumSide]
1299
+
1300
+ theorem eval_sumE_wit (part : Int → Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (w : Nat) : ∀ rs : List (Int × Count),
1301
+ evalE ρ M (sumE (rs.map fun r => .mul (part r.1) (atWit w r.2))) = ptSide part ρ M rs (ρ w)
1302
+ | [] => by simp [sumE, evalE, ptSide]
1303
+ | r :: rs => by
1304
+ simp only [List.map_cons, sumE, evalE, eval_sumE_wit part ρ M w rs]
1305
+ simp only [ptSide, atWit, subst_evalE, applyS_one, evalE]
1306
+
1307
+ -- With every count its sum below one bound, the counts' combination sums the bodies' over the keys below it.
1308
+ theorem sumSide_eq (part : Int → Nat) (ρ : Nat → Nat) (M : Nat → Nat → Nat) (n : Nat) : ∀ rs : List (Int × Count),
1309
+ (∀ r ∈ rs, ρ r.2.v = sumBelow (fun k => evalE (upd ρ r.2.key k) M r.2.body) n) →
1310
+ sumSide part ρ rs = sumBelow (ptSide part ρ M rs) n
1311
+ | [], _ => by
1312
+ have : ptSide part ρ M [] = fun _ => 0 := funext fun k => by simp [ptSide]
1313
+ simp [sumSide, this, sumBelow_const]
1314
+ | r :: rs, h => by
1315
+ simp only [sumSide]
1316
+ have hp : ptSide part ρ M (r :: rs) = fun k => part r.1 * evalE (upd ρ r.2.key k) M r.2.body + ptSide part ρ M rs k := rfl
1317
+ rw [hp, sumBelow_add, sumBelow_mul, ← h r List.mem_cons_self, sumSide_eq part ρ M n rs (fun r' hr' => h r' (List.mem_cons_of_mem _ hr'))]
1318
+
1319
+ -- The witness values a state gives the instances' witnesses.
1320
+ def setWits (ρ : Nat → Nat) (ws : List (Nat × Nat)) : Nat → Nat := fun j =>
1321
+ match lookup (fun p => p.1 == j) ws with
1322
+ | some p => p.2
1323
+ | none => ρ j
1324
+
1325
+ theorem setWits_other {ρ : Nat → Nat} {ws : List (Nat × Nat)} {j : Nat} (h : j ∉ ws.map (·.1)) : setWits ρ ws j = ρ j := by
1326
+ have hl : lookup (fun p => p.1 == j) ws = none :=
1327
+ 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))
1328
+ simp [setWits, hl]
1329
+
1330
+ theorem setWits_at {ρ : Nat → Nat} : ∀ {ws : List (Nat × Nat)}, cDistinct (ws.map (·.1)) = true → ∀ {p : Nat × Nat}, p ∈ ws → setWits ρ ws p.1 = p.2
1331
+ | [], _, _, hp => by simp at hp
1332
+ | a :: as, hd, p, hp => by
1333
+ simp only [List.map_cons] at hd
1334
+ obtain ⟨hna, hd'⟩ := cDistinct_cons hd
1335
+ rcases List.mem_cons.mp hp with rfl | hp'
1336
+ · simp [setWits, lookup]
1337
+ · have hne : (a.1 == p.1) = false := by
1338
+ simp only [beq_eq_false_iff_ne]; intro he; exact hna (by rw [he]; exact List.mem_map_of_mem hp')
1339
+ have ih := setWits_at (ρ := ρ) hd' hp'
1340
+ simp only [setWits, lookup, hne, Bool.false_eq_true, ite_false] at ih ⊢
1341
+ exact ih
1342
+
1343
+ theorem witFresh_parts {hyps : List C} {cs : List Count} {is : List CInst} {w : Nat} (h : witFresh hyps cs is w = true) :
1344
+ (∀ h' ∈ hyps, occC w h' = false) ∧ (∀ c ∈ cs, c.v ≠ w ∧ occE w c.below = false ∧ occE w c.body = false) ∧ (∀ j ∈ is, occE w j.below = false) := by
1345
+ simp only [witFresh, Bool.and_eq_true, List.all_eq_true, Bool.not_eq_true', bne_iff_ne, ne_eq] at h
1346
+ exact ⟨h.1.1, fun c hc => ⟨(h.1.2 c hc).1.1, (h.1.2 c hc).1.2, (h.1.2 c hc).2⟩, h.2⟩
1347
+
1348
+ /-- The kind is sound: an accepted counted goal's hypotheses hold at no naturals and maps where every count is its
1349
+ sum. -/
1350
+ theorem counted_sound (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) {lemmas : List CutLemma}
1351
+ (hck : checkCounted g is tab t lemmas = true)
1352
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (hc : ∀ c ∈ g.counts, c.Holds ρ M) : ¬ ∀ h ∈ g.hyps, evalC ρ M h = true := by
1353
+ intro hH
1354
+ simp only [checkCounted, Bool.and_eq_true] at hck
1355
+ obtain ⟨hok, hgoal⟩ := hck
1356
+ simp only [countedOk, Bool.and_eq_true, List.all_eq_true] at hok
1357
+ obtain ⟨hdist, hfresh⟩ := hok
1358
+ cases hhs : countHyps g.counts is with
1359
+ | none => simp [hhs] at hgoal
1360
+ | some hs =>
1361
+ simp only [hhs] at hgoal
1362
+ -- Each instance's witness value, chosen at ρ: a key below the bound where the combination is positive, when its sum is.
1363
+ have hx : ∀ i : CInst, ∃ x : Nat, ∀ rs, resolve g.counts i.below i.terms = some rs →
1364
+ sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below) ≤ sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below)
1365
+ ∨ (x < evalE ρ M i.below ∧ ptSide negP ρ M rs x + negP i.unit < ptSide posP ρ M rs x + posP i.unit) := by
1366
+ intro i
1367
+ cases hr : resolve g.counts i.below i.terms with
1368
+ | none => exact ⟨0, fun rs h => by simp at h⟩
1369
+ | some rs =>
1370
+ by_cases hlt : sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below) < sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below)
1371
+ · obtain ⟨k, hk, hk'⟩ := exists_lt_of_sumBelow_lt hlt
1372
+ exact ⟨k, fun rs' h => by simp only [Option.some.injEq] at h; subst h; exact Or.inr ⟨hk, hk'⟩⟩
1373
+ · exact ⟨0, fun rs' h => by simp only [Option.some.injEq] at h; subst h; exact Or.inl (Nat.le_of_not_lt hlt)⟩
1374
+ obtain ⟨xs, hxs⟩ : ∃ xs : CInst → Nat, ∀ i rs, resolve g.counts i.below i.terms = some rs →
1375
+ sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below) ≤ sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below)
1376
+ ∨ (xs i < evalE ρ M i.below ∧ ptSide negP ρ M rs (xs i) + negP i.unit < ptSide posP ρ M rs (xs i) + posP i.unit) :=
1377
+ ⟨fun i => Classical.choose (hx i), fun i => Classical.choose_spec (hx i)⟩
1378
+ -- ρs: ρ with each witness set to its value.
1379
+ obtain ⟨ρs, hother, hat⟩ : ∃ ρs : Nat → Nat, (∀ j, (∀ i ∈ is, i.wit ≠ j) → ρs j = ρ j) ∧ (∀ i ∈ is, ρs i.wit = xs i) := by
1380
+ have hws : (is.map fun i => (i.wit, xs i)).map (·.1) = is.map (·.wit) := by induction is <;> simp_all
1381
+ refine ⟨setWits ρ (is.map fun i => (i.wit, xs i)), fun j hj => setWits_other ?_, fun i hi => ?_⟩
1382
+ · rw [hws]; intro hm; obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hm; exact hj i hi rfl
1383
+ · exact setWits_at (ρ := ρ) (ws := is.map fun i => (i.wit, xs i)) (by rw [hws]; exact hdist) (p := (i.wit, xs i)) (List.mem_map.mpr ⟨i, hi, rfl⟩)
1384
+ -- What the witnesses' freshness gives: counts and bounds read alike at ρs and ρ.
1385
+ have hvar : ∀ c ∈ g.counts, ρs c.v = ρ c.v := fun c hc =>
1386
+ hother c.v (fun i hi he => ((witFresh_parts (hfresh i hi)).2.1 c hc).1 he.symm)
1387
+ have hbound : ∀ e : E, (∀ i ∈ is, occE i.wit e = false) → evalE ρs M e = evalE ρ M e := fun e he =>
1388
+ congrE ρs ρ M e (fun j hj => hother j (fun i hi hij => by have := he i hi; rw [hij] at this; simp [this] at hj))
1389
+ have hbody : ∀ c ∈ g.counts, ∀ k, evalE (upd ρs c.key k) M c.body = evalE (upd ρ c.key k) M c.body := fun c hc k =>
1390
+ congrE _ _ M c.body (fun j hj => by
1391
+ simp only [upd]; split
1392
+ · rfl
1393
+ · exact hother j (fun i hi hij => by
1394
+ have := ((witFresh_parts (hfresh i hi)).2.1 c hc).2.2; rw [hij] at this; simp [this] at hj))
1395
+ have hpt : ∀ (part : Int → Nat) (rs : List (Int × Count)), (∀ r ∈ rs, r.2 ∈ g.counts) → ∀ k, ptSide part ρs M rs k = ptSide part ρ M rs k := by
1396
+ intro part rs hrs k
1397
+ induction rs with
1398
+ | nil => rfl
1399
+ | cons r rs ih =>
1400
+ simp only [ptSide, hbody r.2 (hrs r List.mem_cons_self) k, ih (fun r' hr' => hrs r' (List.mem_cons_of_mem _ hr'))]
1401
+ -- Every instance's hypothesis holds at ρs.
1402
+ have hinst : ∀ i ∈ is, ∀ h, countHyp g.counts i = some h → evalC ρs M h = true := by
1403
+ intro i hi h hh
1404
+ simp only [countHyp, Option.map_eq_some_iff] at hh
1405
+ obtain ⟨rs, hr, rfl⟩ := hh
1406
+ have hrm := resolve_mem hr
1407
+ have hB : evalE ρs M i.below = evalE ρ M i.below := hbound i.below (fun j hj => (witFresh_parts (hfresh j hj)).2.2 i hi)
1408
+ have hcnt : ∀ r ∈ rs, ρs r.2.v = sumBelow (fun k => evalE (upd ρs r.2.key k) M r.2.body) (evalE ρ M i.below) := by
1409
+ intro r hr'
1410
+ obtain ⟨hcm, hcb⟩ := hrm r hr'
1411
+ have h1 : ρ r.2.v = sumBelow (fun k => evalE (upd ρ r.2.key k) M r.2.body) (evalE ρ M r.2.below) := hc r.2 hcm
1412
+ rw [hvar r.2 hcm, h1, hcb]
1413
+ exact sumBelow_congr _ (fun k _ => (hbody r.2 hcm k).symm)
1414
+ have hS : ∀ part, evalE ρs M (side part atCount i.below i.unit rs)
1415
+ = sumBelow (fun k => ptSide part ρ M rs k + part i.unit) (evalE ρ M i.below) := by
1416
+ intro part
1417
+ have e1 : evalE ρs M (side part atCount i.below i.unit rs) = sumSide part ρs rs + part i.unit * evalE ρ M i.below := by
1418
+ simp only [side, evalE, eval_sumE_count, hB]
1419
+ have e2 : sumBelow (fun k => ptSide part ρ M rs k + part i.unit) (evalE ρ M i.below)
1420
+ = sumBelow (ptSide part ρ M rs) (evalE ρ M i.below) + part i.unit * evalE ρ M i.below := by
1421
+ rw [sumBelow_add (ptSide part ρ M rs) (fun _ => part i.unit), sumBelow_const]
1422
+ have e3 : sumSide part ρs rs = sumBelow (ptSide part ρ M rs) (evalE ρ M i.below) := by
1423
+ rw [sumSide_eq part ρs M _ rs hcnt]
1424
+ exact sumBelow_congr _ (fun k _ => hpt part rs (fun r hr' => (hrm r hr').1) k)
1425
+ rw [e1, e2, e3]
1426
+ have hW : ∀ part, evalE ρs M (side part (atWit i.wit) (.const 1) i.unit rs) = ptSide part ρ M rs (xs i) + part i.unit := by
1427
+ intro part
1428
+ simp only [side, evalE, eval_sumE_wit, hat i hi, hpt part rs (fun r hr' => (hrm r hr').1), Nat.mul_one]
1429
+ have hv := hxs i rs hr
1430
+ simp only [evalC, hS, hW, evalE, hat i hi, hB, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq]
1431
+ exact hv
1432
+ -- The goal's hypotheses hold at ρs too: no witness is read there.
1433
+ have hhyp : ∀ h ∈ g.hyps, evalC ρs M h = true := fun h hm => by
1434
+ rw [congrC ρs ρ M h (fun j hj => hother j (fun i hi hij => by
1435
+ have := (witFresh_parts (hfresh i hi)).1 h hm; rw [hij] at this; simp [this] at hj))]
1436
+ exact hH h hm
1437
+ exact goalLemmas_sound ⟨tab, g.hyps ++ hs⟩ t lemmas hgoal ρs M (fun h hm => by
1438
+ rcases List.mem_append.mp hm with hm | hm
1439
+ · exact hhyp h hm
1440
+ · obtain ⟨i, hi, hh⟩ := countHyps_mem hhs h hm
1441
+ exact hinst i hi h hh)
1442
+
1443
+
1444
+ -- The counted check with frames: fresh witnesses, every instance built, the goal extended by checked lemma cuts and
1445
+ -- frames over its own counts.
1446
+ def checkCountedF (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) (frames : List FrameCut) (lemmas : List CutLemma := []) (monos : List MonoCut := []) (lins : List LinCut := []) : Bool :=
1447
+ countedOk g.hyps g.counts is && match countHyps g.counts is with
1448
+ | some hs => checkGoalFrames ⟨tab, g.hyps ++ hs⟩ g.counts t lemmas frames monos lins
1449
+ | none => false
1450
+
1451
+ /-- With no frames the counted check with lemma cuts is unchanged. -/
1452
+ theorem checkCountedF_nil (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) (lemmas : List CutLemma) :
1453
+ checkCountedF g is tab t [] lemmas = checkCounted g is tab t lemmas := by
1454
+ cases h : countHyps g.counts is <;> simp [checkCountedF, checkCounted, h]
1455
+
1456
+ /-- The kind with frames is sound: an accepted counted goal's hypotheses hold at no naturals and maps where every
1457
+ count is its sum. -/
1458
+ theorem countedF_sound (g : CGoal) (is : List CInst) (tab : List VDef) (t : Tree) (frames : List FrameCut) {lemmas : List CutLemma} {monos : List MonoCut} {lins : List LinCut}
1459
+ (hck : checkCountedF g is tab t frames lemmas monos lins = true)
1460
+ (ρ : Nat → Nat) (M : Nat → Nat → Nat) (hc : ∀ c ∈ g.counts, c.Holds ρ M) : ¬ ∀ h ∈ g.hyps, evalC ρ M h = true := by
1461
+ intro hH
1462
+ simp only [checkCountedF, Bool.and_eq_true] at hck
1463
+ obtain ⟨hok, hgoal⟩ := hck
1464
+ simp only [countedOk, Bool.and_eq_true, List.all_eq_true] at hok
1465
+ obtain ⟨hdist, hfresh⟩ := hok
1466
+ cases hhs : countHyps g.counts is with
1467
+ | none => simp [hhs] at hgoal
1468
+ | some hs =>
1469
+ simp only [hhs] at hgoal
1470
+ -- Each instance's witness value, chosen at ρ: a key below the bound where the combination is positive, when its sum is.
1471
+ have hx : ∀ i : CInst, ∃ x : Nat, ∀ rs, resolve g.counts i.below i.terms = some rs →
1472
+ sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below) ≤ sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below)
1473
+ ∨ (x < evalE ρ M i.below ∧ ptSide negP ρ M rs x + negP i.unit < ptSide posP ρ M rs x + posP i.unit) := by
1474
+ intro i
1475
+ cases hr : resolve g.counts i.below i.terms with
1476
+ | none => exact ⟨0, fun rs h => by simp at h⟩
1477
+ | some rs =>
1478
+ by_cases hlt : sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below) < sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below)
1479
+ · obtain ⟨k, hk, hk'⟩ := exists_lt_of_sumBelow_lt hlt
1480
+ exact ⟨k, fun rs' h => by simp only [Option.some.injEq] at h; subst h; exact Or.inr ⟨hk, hk'⟩⟩
1481
+ · exact ⟨0, fun rs' h => by simp only [Option.some.injEq] at h; subst h; exact Or.inl (Nat.le_of_not_lt hlt)⟩
1482
+ obtain ⟨xs, hxs⟩ : ∃ xs : CInst → Nat, ∀ i rs, resolve g.counts i.below i.terms = some rs →
1483
+ sumBelow (fun k => ptSide posP ρ M rs k + posP i.unit) (evalE ρ M i.below) ≤ sumBelow (fun k => ptSide negP ρ M rs k + negP i.unit) (evalE ρ M i.below)
1484
+ ∨ (xs i < evalE ρ M i.below ∧ ptSide negP ρ M rs (xs i) + negP i.unit < ptSide posP ρ M rs (xs i) + posP i.unit) :=
1485
+ ⟨fun i => Classical.choose (hx i), fun i => Classical.choose_spec (hx i)⟩
1486
+ -- ρs: ρ with each witness set to its value.
1487
+ obtain ⟨ρs, hother, hat⟩ : ∃ ρs : Nat → Nat, (∀ j, (∀ i ∈ is, i.wit ≠ j) → ρs j = ρ j) ∧ (∀ i ∈ is, ρs i.wit = xs i) := by
1488
+ have hws : (is.map fun i => (i.wit, xs i)).map (·.1) = is.map (·.wit) := by induction is <;> simp_all
1489
+ refine ⟨setWits ρ (is.map fun i => (i.wit, xs i)), fun j hj => setWits_other ?_, fun i hi => ?_⟩
1490
+ · rw [hws]; intro hm; obtain ⟨i, hi, rfl⟩ := List.mem_map.mp hm; exact hj i hi rfl
1491
+ · exact setWits_at (ρ := ρ) (ws := is.map fun i => (i.wit, xs i)) (by rw [hws]; exact hdist) (p := (i.wit, xs i)) (List.mem_map.mpr ⟨i, hi, rfl⟩)
1492
+ -- What the witnesses' freshness gives: counts and bounds read alike at ρs and ρ.
1493
+ have hvar : ∀ c ∈ g.counts, ρs c.v = ρ c.v := fun c hc =>
1494
+ hother c.v (fun i hi he => ((witFresh_parts (hfresh i hi)).2.1 c hc).1 he.symm)
1495
+ have hbound : ∀ e : E, (∀ i ∈ is, occE i.wit e = false) → evalE ρs M e = evalE ρ M e := fun e he =>
1496
+ congrE ρs ρ M e (fun j hj => hother j (fun i hi hij => by have := he i hi; rw [hij] at this; simp [this] at hj))
1497
+ have hbody : ∀ c ∈ g.counts, ∀ k, evalE (upd ρs c.key k) M c.body = evalE (upd ρ c.key k) M c.body := fun c hc k =>
1498
+ congrE _ _ M c.body (fun j hj => by
1499
+ simp only [upd]; split
1500
+ · rfl
1501
+ · exact hother j (fun i hi hij => by
1502
+ have := ((witFresh_parts (hfresh i hi)).2.1 c hc).2.2; rw [hij] at this; simp [this] at hj))
1503
+ have hpt : ∀ (part : Int → Nat) (rs : List (Int × Count)), (∀ r ∈ rs, r.2 ∈ g.counts) → ∀ k, ptSide part ρs M rs k = ptSide part ρ M rs k := by
1504
+ intro part rs hrs k
1505
+ induction rs with
1506
+ | nil => rfl
1507
+ | cons r rs ih =>
1508
+ simp only [ptSide, hbody r.2 (hrs r List.mem_cons_self) k, ih (fun r' hr' => hrs r' (List.mem_cons_of_mem _ hr'))]
1509
+ -- Every instance's hypothesis holds at ρs.
1510
+ have hinst : ∀ i ∈ is, ∀ h, countHyp g.counts i = some h → evalC ρs M h = true := by
1511
+ intro i hi h hh
1512
+ simp only [countHyp, Option.map_eq_some_iff] at hh
1513
+ obtain ⟨rs, hr, rfl⟩ := hh
1514
+ have hrm := resolve_mem hr
1515
+ have hB : evalE ρs M i.below = evalE ρ M i.below := hbound i.below (fun j hj => (witFresh_parts (hfresh j hj)).2.2 i hi)
1516
+ have hcnt : ∀ r ∈ rs, ρs r.2.v = sumBelow (fun k => evalE (upd ρs r.2.key k) M r.2.body) (evalE ρ M i.below) := by
1517
+ intro r hr'
1518
+ obtain ⟨hcm, hcb⟩ := hrm r hr'
1519
+ have h1 : ρ r.2.v = sumBelow (fun k => evalE (upd ρ r.2.key k) M r.2.body) (evalE ρ M r.2.below) := hc r.2 hcm
1520
+ rw [hvar r.2 hcm, h1, hcb]
1521
+ exact sumBelow_congr _ (fun k _ => (hbody r.2 hcm k).symm)
1522
+ have hS : ∀ part, evalE ρs M (side part atCount i.below i.unit rs)
1523
+ = sumBelow (fun k => ptSide part ρ M rs k + part i.unit) (evalE ρ M i.below) := by
1524
+ intro part
1525
+ have e1 : evalE ρs M (side part atCount i.below i.unit rs) = sumSide part ρs rs + part i.unit * evalE ρ M i.below := by
1526
+ simp only [side, evalE, eval_sumE_count, hB]
1527
+ have e2 : sumBelow (fun k => ptSide part ρ M rs k + part i.unit) (evalE ρ M i.below)
1528
+ = sumBelow (ptSide part ρ M rs) (evalE ρ M i.below) + part i.unit * evalE ρ M i.below := by
1529
+ rw [sumBelow_add (ptSide part ρ M rs) (fun _ => part i.unit), sumBelow_const]
1530
+ have e3 : sumSide part ρs rs = sumBelow (ptSide part ρ M rs) (evalE ρ M i.below) := by
1531
+ rw [sumSide_eq part ρs M _ rs hcnt]
1532
+ exact sumBelow_congr _ (fun k _ => hpt part rs (fun r hr' => (hrm r hr').1) k)
1533
+ rw [e1, e2, e3]
1534
+ have hW : ∀ part, evalE ρs M (side part (atWit i.wit) (.const 1) i.unit rs) = ptSide part ρ M rs (xs i) + part i.unit := by
1535
+ intro part
1536
+ simp only [side, evalE, eval_sumE_wit, hat i hi, hpt part rs (fun r hr' => (hrm r hr').1), Nat.mul_one]
1537
+ have hv := hxs i rs hr
1538
+ simp only [evalC, hS, hW, evalE, hat i hi, hB, Bool.or_eq_true, Bool.and_eq_true, decide_eq_true_eq]
1539
+ exact hv
1540
+ -- The goal's hypotheses hold at ρs too: no witness is read there.
1541
+ have hhyp : ∀ h ∈ g.hyps, evalC ρs M h = true := fun h hm => by
1542
+ rw [congrC ρs ρ M h (fun j hj => hother j (fun i hi hij => by
1543
+ have := (witFresh_parts (hfresh i hi)).1 h hm; rw [hij] at this; simp [this] at hj))]
1544
+ exact hH h hm
1545
+ have hholds' : ∀ c ∈ g.counts, c.Holds ρs M := by
1546
+ intro c hcm
1547
+ have h1 : ρ c.v = sumBelow (fun k => evalE (upd ρ c.key k) M c.body) (evalE ρ M c.below) := hc c hcm
1548
+ simp only [Count.Holds]
1549
+ rw [hvar c hcm, h1, hbound c.below (fun i hi => ((witFresh_parts (hfresh i hi)).2.1 c hcm).2.1)]
1550
+ exact sumBelow_congr _ (fun k _ => (hbody c hcm k).symm)
1551
+ exact goalFrames_sound ⟨tab, g.hyps ++ hs⟩ g.counts t lemmas frames monos lins hgoal ρs M hholds' (fun h hm => by
1552
+ rcases List.mem_append.mp hm with hm | hm
1553
+ · exact hhyp h hm
1554
+ · obtain ⟨i, hi, hh⟩ := countHyps_mem hhs h hm
1555
+ exact hinst i hi h hh)
1556
+
1557
+ end Kern
1558
+
1559
+ #print axioms Kern.exists_lt_of_sumBelow_lt
1560
+ #print axioms Kern.pigeonhole
1561
+ #print axioms Kern.majorities_meet
1562
+ #print axioms Kern.quorums_meet_outside
1563
+ #print axioms Kern.lemma_sound
1564
+ #print axioms Kern.goalLemmas_sound
1565
+ #print axioms Kern.counted_sound
1566
+ #print axioms Kern.frame_sound
1567
+ #print axioms Kern.goalFrames_sound
1568
+ #print axioms Kern.countedF_sound