gdp-wired 0.50.6 → 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 (91) 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 +11 -2
  9. package/dist/core/dsl.js +194 -29
  10. package/dist/core/fixed-division.d.ts +9 -0
  11. package/dist/core/fixed-division.js +87 -0
  12. package/dist/core/fixed-integers.d.ts +33 -0
  13. package/dist/core/fixed-integers.js +279 -0
  14. package/dist/core/live-support.d.ts +5 -0
  15. package/dist/core/live-support.js +148 -0
  16. package/dist/core/live.js +194 -37
  17. package/dist/core/lockstep.d.ts +5 -0
  18. package/dist/core/lockstep.js +23 -0
  19. package/dist/core/machine.d.ts +1 -0
  20. package/dist/core/machine.js +33 -3
  21. package/dist/core/ordered-pair-seeds.d.ts +2 -0
  22. package/dist/core/ordered-pair-seeds.js +109 -0
  23. package/dist/core/proof-output.d.ts +38 -1
  24. package/dist/core/proof-output.js +38 -0
  25. package/dist/core/search-termination.d.ts +4 -0
  26. package/dist/core/search-termination.js +1 -1
  27. package/dist/core/source-check.d.ts +4 -1
  28. package/dist/core/source-check.js +5 -2
  29. package/dist/core/sparse-kind-proof.d.ts +7 -0
  30. package/dist/core/sparse-kind-proof.js +49 -0
  31. package/dist/core/step-seeds.d.ts +46 -0
  32. package/dist/core/step-seeds.js +316 -0
  33. package/dist/kernel/integer-primitives.d.ts +38 -0
  34. package/dist/kernel/integer-primitives.js +179 -0
  35. package/dist/kernel/kernel.d.ts +1 -0
  36. package/dist/kernel/kernel.js +44 -5
  37. package/dist/kernel/prove.js +13 -0
  38. package/dist/kernel/word-division-cuts.d.ts +6 -0
  39. package/dist/kernel/word-division-cuts.js +69 -0
  40. package/dist/kernel/word-division.d.ts +12 -0
  41. package/dist/kernel/word-division.js +167 -0
  42. package/dist/lean-kernel/Cert.lean +222 -0
  43. package/dist/lean-kernel/Count.lean +1568 -0
  44. package/dist/lean-kernel/Ghost.lean +373 -0
  45. package/dist/lean-kernel/GhostMap.lean +426 -0
  46. package/dist/lean-kernel/Kernel.lean +2881 -0
  47. package/dist/lean-kernel/Live.lean +1168 -0
  48. package/dist/lean-kernel/Progress.lean +543 -0
  49. package/dist/lean-kernel/Sets.lean +1975 -0
  50. package/dist/lean-kernel/StepLaw.lean +128 -0
  51. package/dist/lean-kernel/Steps.lean +477 -0
  52. package/dist/lean-kernel/Sums.lean +916 -0
  53. package/dist/lean-kernel/emit.d.ts +8 -1
  54. package/dist/lean-kernel/emit.js +59 -15
  55. package/dist/lean-kernel/integer-primitives.d.ts +17 -0
  56. package/dist/lean-kernel/integer-primitives.js +81 -0
  57. package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
  58. package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
  59. package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
  60. package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
  61. package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
  62. package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
  63. package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
  64. package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
  65. package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
  66. package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
  67. package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
  68. package/dist/lean-kernel/lean-aggregate/index.js +52 -0
  69. package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
  70. package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
  71. package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
  72. package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
  73. package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
  74. package/dist/lean-kernel/lean-aggregate/source.js +141 -0
  75. package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
  76. package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
  77. package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
  78. package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
  79. package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
  80. package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
  81. package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
  82. package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
  83. package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
  84. package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
  85. package/dist/lean-kernel/library.js +13 -0
  86. package/dist/lean-kernel/word-division-export.d.ts +26 -0
  87. package/dist/lean-kernel/word-division-export.js +172 -0
  88. package/dist/lean-kernel/word-division-lower.d.ts +10 -0
  89. package/dist/lean-kernel/word-division-lower.js +103 -0
  90. package/docs/lean-aggregate.md +29 -0
  91. package/package.json +6 -1
@@ -0,0 +1,2881 @@
1
+ import Cert
2
+ /-!
3
+ Kernel: the Lean twin of gdp-wired's kernel/kernel.ts tree check, with its soundness proof. Lean 4.34.0, core only.
4
+
5
+ A goal is a variable table and hypotheses over naturals (+, ∸, × by a constant, min, max, if, map reads; ≤, <, =,
6
+ and, or, not). A proof is a tree of splits on linear atoms, with a leaf certificate: the rows it claims, a Cert
7
+ derivation over them, and the pairs of cells it claims equal. At a leaf the twin computes the rows the path, the
8
+ hypotheses' facts, the naturals and the verified cell equalities give, checks every claimed row is one of them, and
9
+ replays the derivation with `Cert.check`. `sound`: if `checkGoal` accepts, no naturals and no maps satisfy every
10
+ hypothesis.
11
+
12
+ The table names what each column of a row is: a variable of the environment, or a map's cell at a key (a row over
13
+ earlier columns). Rows compare by coefficients (`same`), so the exporter's ordering doesn't matter.
14
+ -/
15
+
16
+ namespace Kern
17
+ open Cert
18
+ set_option linter.unusedSimpArgs false
19
+ set_option linter.unusedSectionVars false
20
+
21
+ /-! The pairing a tuple key is read through (Data.lean's datatype codes use it too): `cun`, the naturals onto the pairs
22
+ in order of their sum, and `cpair` its inverse (`cun_cpair`, `cpair_cun`); `tupk`, naturals packed by `cpair`, right-nested. -/
23
+
24
+ /-- The naturals onto the pairs, in order of their sum: 0 is (0, 0), and each next natural the pair after. -/
25
+ def cun : Nat → Nat × Nat
26
+ | 0 => (0, 0)
27
+ | n + 1 => match cun n with
28
+ | (0, b) => (b + 1, 0)
29
+ | (a + 1, b) => (a, b + 1)
30
+
31
+ /-- 0 + 1 + … + s. -/
32
+ def tri : Nat → Nat
33
+ | 0 => 0
34
+ | s + 1 => tri s + s + 1
35
+
36
+ /-- `cun`'s inverse. -/
37
+ def cpair (a b : Nat) : Nat := tri (a + b) + b
38
+
39
+ theorem le_tri : ∀ s, s ≤ tri s
40
+ | 0 => Nat.le_refl 0
41
+ | s + 1 => by have := le_tri s; simp only [tri]; omega
42
+
43
+ theorem cpair_succ_left (b : Nat) : cpair (b + 1) 0 = cpair 0 b + 1 := by
44
+ simp only [cpair, Nat.add_zero, Nat.zero_add, tri]
45
+
46
+ theorem cpair_succ_right (a b : Nat) : cpair a (b + 1) = cpair (a + 1) b + 1 := by
47
+ simp only [cpair]
48
+ have e : a + (b + 1) = a + 1 + b := by omega
49
+ rw [e]; omega
50
+
51
+ theorem cpair_cun : ∀ n, cpair (cun n).1 (cun n).2 = n
52
+ | 0 => rfl
53
+ | n + 1 => by
54
+ have ih := cpair_cun n
55
+ rcases h : cun n with ⟨a, b⟩
56
+ rw [h] at ih
57
+ cases a with
58
+ | zero => simp only [cun, h]; rw [cpair_succ_left]; simp only at ih; omega
59
+ | succ a => simp only [cun, h]; rw [cpair_succ_right]; simp only at ih; omega
60
+
61
+ theorem cun_tri : ∀ s b, b ≤ s → cun (tri s + b) = (s - b, b)
62
+ | 0, 0, _ => rfl
63
+ | 0, _ + 1, h => absurd h (by omega)
64
+ | s + 1, 0, _ => by
65
+ have ih := cun_tri s s (Nat.le_refl s)
66
+ have e : tri (s + 1) + 0 = (tri s + s) + 1 := by simp only [tri]; try omega
67
+ rw [e]
68
+ simp only [cun, ih, Nat.sub_self, Nat.sub_zero]
69
+ | s + 1, b + 1, h => by
70
+ have ih := cun_tri (s + 1) b (by omega)
71
+ have e : tri (s + 1) + (b + 1) = (tri (s + 1) + b) + 1 := by omega
72
+ rw [e]
73
+ simp only [cun, ih]
74
+ have e2 : s + 1 - b = (s - b) + 1 := by omega
75
+ rw [e2]
76
+ simp only
77
+ have e3 : s + 1 - (b + 1) = s - b := by omega
78
+ rw [e3]
79
+
80
+ theorem cun_cpair (a b : Nat) : cun (cpair a b) = (a, b) := by
81
+ unfold cpair
82
+ rw [cun_tri (a + b) b (by omega)]
83
+ simp
84
+
85
+ @[simp] theorem cun_cpair_fst (a b : Nat) : (cun (cpair a b)).1 = a := by rw [cun_cpair]
86
+ @[simp] theorem cun_cpair_snd (a b : Nat) : (cun (cpair a b)).2 = b := by rw [cun_cpair]
87
+ @[simp] theorem cpair_cun_eq (n : Nat) : cpair (cun n).1 (cun n).2 = n := cpair_cun n
88
+
89
+ /-- Naturals packed by `cpair`, right-nested; one is itself. -/
90
+ def tupk : List Nat → Nat
91
+ | [] => 0
92
+ | [x] => x
93
+ | x :: xs => cpair x (tupk xs)
94
+
95
+ @[simp] theorem tupk_one (x : Nat) : tupk [x] = x := rfl
96
+ @[simp] theorem tupk_cons (x y : Nat) (ys : List Nat) : tupk (x :: y :: ys) = cpair x (tupk (y :: ys)) := rfl
97
+
98
+ -- A pair's part: the first (k = 0) or the second (any other k).
99
+ def pickP (k v : Nat) : Nat := if k = 0 then (cun v).1 else (cun v).2
100
+
101
+ -- Equal naturals are equal pairs.
102
+ theorem eq_iff_cun (n1 n2 : Nat) : n1 = n2 ↔ (cun n1).1 = (cun n2).1 ∧ (cun n1).2 = (cun n2).2 := by
103
+ constructor
104
+ · intro h; subst h; exact ⟨rfl, rfl⟩
105
+ · intro h; rw [← cpair_cun n1, ← cpair_cun n2, h.1, h.2]
106
+
107
+ mutual
108
+ inductive E where
109
+ | var (i : Nat)
110
+ | const (n : Nat)
111
+ | add (a b : E)
112
+ | sub (a b : E)
113
+ | mul (k : Nat) (a : E)
114
+ | min (a b : E)
115
+ | max (a b : E)
116
+ | prod (a b : E)
117
+ | ite (c : C) (t f : E)
118
+ | get (m : Nat) (key : E)
119
+ | tup (a b : E) -- the pair (Cantor's `cpair`) of two terms; an n-tuple nests to the right
120
+ | proj (k : Nat) (a : E) -- a pair's first part (k = 0) or second (any other k)
121
+ inductive C where
122
+ | le (a b : E)
123
+ | lt (a b : E)
124
+ | eq (a b : E)
125
+ | and (a b : C)
126
+ | or (a b : C)
127
+ | not (a : C)
128
+ end
129
+
130
+ deriving instance DecidableEq for E, C
131
+
132
+ /-! Boolean equality of terms and conditions, by structure: what a check compares terms with (the derived DecidableEq
133
+ builds a proof at each comparison, which the kernel then reduces). -/
134
+
135
+ mutual
136
+ def beqE : E → E → Bool
137
+ | .var x0, .var y0 => x0 == y0
138
+ | .const x0, .const y0 => x0 == y0
139
+ | .add x0 x1, .add y0 y1 => beqE x0 y0 && beqE x1 y1
140
+ | .sub x0 x1, .sub y0 y1 => beqE x0 y0 && beqE x1 y1
141
+ | .mul x0 x1, .mul y0 y1 => x0 == y0 && beqE x1 y1
142
+ | .min x0 x1, .min y0 y1 => beqE x0 y0 && beqE x1 y1
143
+ | .max x0 x1, .max y0 y1 => beqE x0 y0 && beqE x1 y1
144
+ | .prod x0 x1, .prod y0 y1 => beqE x0 y0 && beqE x1 y1
145
+ | .ite x0 x1 x2, .ite y0 y1 y2 => beqC x0 y0 && beqE x1 y1 && beqE x2 y2
146
+ | .get x0 x1, .get y0 y1 => x0 == y0 && beqE x1 y1
147
+ | .tup x0 x1, .tup y0 y1 => beqE x0 y0 && beqE x1 y1
148
+ | .proj x0 x1, .proj y0 y1 => x0 == y0 && beqE x1 y1
149
+ | _, _ => false
150
+ def beqC : C → C → Bool
151
+ | .le x0 x1, .le y0 y1 => beqE x0 y0 && beqE x1 y1
152
+ | .lt x0 x1, .lt y0 y1 => beqE x0 y0 && beqE x1 y1
153
+ | .eq x0 x1, .eq y0 y1 => beqE x0 y0 && beqE x1 y1
154
+ | .and x0 x1, .and y0 y1 => beqC x0 y0 && beqC x1 y1
155
+ | .or x0 x1, .or y0 y1 => beqC x0 y0 && beqC x1 y1
156
+ | .not x0, .not y0 => beqC x0 y0
157
+ | _, _ => false
158
+ end
159
+
160
+ mutual
161
+ theorem beqE_eq : ∀ {a b : E}, beqE a b = true → a = b
162
+ | .var x0, .var y0, h => by
163
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
164
+ rw [h]
165
+ | .const x0, .const y0, h => by
166
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
167
+ rw [h]
168
+ | .add x0 x1, .add y0 y1, h => by
169
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
170
+ rw [beqE_eq h.1, beqE_eq h.2]
171
+ | .sub x0 x1, .sub y0 y1, h => by
172
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
173
+ rw [beqE_eq h.1, beqE_eq h.2]
174
+ | .mul x0 x1, .mul y0 y1, h => by
175
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
176
+ rw [h.1, beqE_eq h.2]
177
+ | .min x0 x1, .min y0 y1, h => by
178
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
179
+ rw [beqE_eq h.1, beqE_eq h.2]
180
+ | .max x0 x1, .max y0 y1, h => by
181
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
182
+ rw [beqE_eq h.1, beqE_eq h.2]
183
+ | .prod x0 x1, .prod y0 y1, h => by
184
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
185
+ rw [beqE_eq h.1, beqE_eq h.2]
186
+ | .ite x0 x1 x2, .ite y0 y1 y2, h => by
187
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
188
+ rw [beqC_eq h.1.1, beqE_eq h.1.2, beqE_eq h.2]
189
+ | .get x0 x1, .get y0 y1, h => by
190
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
191
+ rw [h.1, beqE_eq h.2]
192
+ | .tup x0 x1, .tup y0 y1, h => by
193
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
194
+ rw [beqE_eq h.1, beqE_eq h.2]
195
+ | .proj x0 x1, .proj y0 y1, h => by
196
+ simp only [beqE, Bool.and_eq_true, beq_iff_eq] at h
197
+ rw [h.1, beqE_eq h.2]
198
+ | .var _, .const _, h => by simp [beqE] at h
199
+ | .var _, .add _ _, h => by simp [beqE] at h
200
+ | .var _, .sub _ _, h => by simp [beqE] at h
201
+ | .var _, .mul _ _, h => by simp [beqE] at h
202
+ | .var _, .min _ _, h => by simp [beqE] at h
203
+ | .var _, .max _ _, h => by simp [beqE] at h
204
+ | .var _, .prod _ _, h => by simp [beqE] at h
205
+ | .var _, .ite _ _ _, h => by simp [beqE] at h
206
+ | .var _, .get _ _, h => by simp [beqE] at h
207
+ | .var _, .tup _ _, h => by simp [beqE] at h
208
+ | .var _, .proj _ _, h => by simp [beqE] at h
209
+ | .const _, .var _, h => by simp [beqE] at h
210
+ | .const _, .add _ _, h => by simp [beqE] at h
211
+ | .const _, .sub _ _, h => by simp [beqE] at h
212
+ | .const _, .mul _ _, h => by simp [beqE] at h
213
+ | .const _, .min _ _, h => by simp [beqE] at h
214
+ | .const _, .max _ _, h => by simp [beqE] at h
215
+ | .const _, .prod _ _, h => by simp [beqE] at h
216
+ | .const _, .ite _ _ _, h => by simp [beqE] at h
217
+ | .const _, .get _ _, h => by simp [beqE] at h
218
+ | .const _, .tup _ _, h => by simp [beqE] at h
219
+ | .const _, .proj _ _, h => by simp [beqE] at h
220
+ | .add _ _, .var _, h => by simp [beqE] at h
221
+ | .add _ _, .const _, h => by simp [beqE] at h
222
+ | .add _ _, .sub _ _, h => by simp [beqE] at h
223
+ | .add _ _, .mul _ _, h => by simp [beqE] at h
224
+ | .add _ _, .min _ _, h => by simp [beqE] at h
225
+ | .add _ _, .max _ _, h => by simp [beqE] at h
226
+ | .add _ _, .prod _ _, h => by simp [beqE] at h
227
+ | .add _ _, .ite _ _ _, h => by simp [beqE] at h
228
+ | .add _ _, .get _ _, h => by simp [beqE] at h
229
+ | .add _ _, .tup _ _, h => by simp [beqE] at h
230
+ | .add _ _, .proj _ _, h => by simp [beqE] at h
231
+ | .sub _ _, .var _, h => by simp [beqE] at h
232
+ | .sub _ _, .const _, h => by simp [beqE] at h
233
+ | .sub _ _, .add _ _, h => by simp [beqE] at h
234
+ | .sub _ _, .mul _ _, h => by simp [beqE] at h
235
+ | .sub _ _, .min _ _, h => by simp [beqE] at h
236
+ | .sub _ _, .max _ _, h => by simp [beqE] at h
237
+ | .sub _ _, .prod _ _, h => by simp [beqE] at h
238
+ | .sub _ _, .ite _ _ _, h => by simp [beqE] at h
239
+ | .sub _ _, .get _ _, h => by simp [beqE] at h
240
+ | .sub _ _, .tup _ _, h => by simp [beqE] at h
241
+ | .sub _ _, .proj _ _, h => by simp [beqE] at h
242
+ | .mul _ _, .var _, h => by simp [beqE] at h
243
+ | .mul _ _, .const _, h => by simp [beqE] at h
244
+ | .mul _ _, .add _ _, h => by simp [beqE] at h
245
+ | .mul _ _, .sub _ _, h => by simp [beqE] at h
246
+ | .mul _ _, .min _ _, h => by simp [beqE] at h
247
+ | .mul _ _, .max _ _, h => by simp [beqE] at h
248
+ | .mul _ _, .prod _ _, h => by simp [beqE] at h
249
+ | .mul _ _, .ite _ _ _, h => by simp [beqE] at h
250
+ | .mul _ _, .get _ _, h => by simp [beqE] at h
251
+ | .mul _ _, .tup _ _, h => by simp [beqE] at h
252
+ | .mul _ _, .proj _ _, h => by simp [beqE] at h
253
+ | .min _ _, .var _, h => by simp [beqE] at h
254
+ | .min _ _, .const _, h => by simp [beqE] at h
255
+ | .min _ _, .add _ _, h => by simp [beqE] at h
256
+ | .min _ _, .sub _ _, h => by simp [beqE] at h
257
+ | .min _ _, .mul _ _, h => by simp [beqE] at h
258
+ | .min _ _, .max _ _, h => by simp [beqE] at h
259
+ | .min _ _, .prod _ _, h => by simp [beqE] at h
260
+ | .min _ _, .ite _ _ _, h => by simp [beqE] at h
261
+ | .min _ _, .get _ _, h => by simp [beqE] at h
262
+ | .min _ _, .tup _ _, h => by simp [beqE] at h
263
+ | .min _ _, .proj _ _, h => by simp [beqE] at h
264
+ | .max _ _, .var _, h => by simp [beqE] at h
265
+ | .max _ _, .const _, h => by simp [beqE] at h
266
+ | .max _ _, .add _ _, h => by simp [beqE] at h
267
+ | .max _ _, .sub _ _, h => by simp [beqE] at h
268
+ | .max _ _, .mul _ _, h => by simp [beqE] at h
269
+ | .max _ _, .min _ _, h => by simp [beqE] at h
270
+ | .max _ _, .prod _ _, h => by simp [beqE] at h
271
+ | .max _ _, .ite _ _ _, h => by simp [beqE] at h
272
+ | .max _ _, .get _ _, h => by simp [beqE] at h
273
+ | .max _ _, .tup _ _, h => by simp [beqE] at h
274
+ | .max _ _, .proj _ _, h => by simp [beqE] at h
275
+ | .prod _ _, .var _, h => by simp [beqE] at h
276
+ | .prod _ _, .const _, h => by simp [beqE] at h
277
+ | .prod _ _, .add _ _, h => by simp [beqE] at h
278
+ | .prod _ _, .sub _ _, h => by simp [beqE] at h
279
+ | .prod _ _, .mul _ _, h => by simp [beqE] at h
280
+ | .prod _ _, .min _ _, h => by simp [beqE] at h
281
+ | .prod _ _, .max _ _, h => by simp [beqE] at h
282
+ | .prod _ _, .ite _ _ _, h => by simp [beqE] at h
283
+ | .prod _ _, .get _ _, h => by simp [beqE] at h
284
+ | .prod _ _, .tup _ _, h => by simp [beqE] at h
285
+ | .prod _ _, .proj _ _, h => by simp [beqE] at h
286
+ | .ite _ _ _, .var _, h => by simp [beqE] at h
287
+ | .ite _ _ _, .const _, h => by simp [beqE] at h
288
+ | .ite _ _ _, .add _ _, h => by simp [beqE] at h
289
+ | .ite _ _ _, .sub _ _, h => by simp [beqE] at h
290
+ | .ite _ _ _, .mul _ _, h => by simp [beqE] at h
291
+ | .ite _ _ _, .min _ _, h => by simp [beqE] at h
292
+ | .ite _ _ _, .max _ _, h => by simp [beqE] at h
293
+ | .ite _ _ _, .prod _ _, h => by simp [beqE] at h
294
+ | .ite _ _ _, .get _ _, h => by simp [beqE] at h
295
+ | .ite _ _ _, .tup _ _, h => by simp [beqE] at h
296
+ | .ite _ _ _, .proj _ _, h => by simp [beqE] at h
297
+ | .get _ _, .var _, h => by simp [beqE] at h
298
+ | .get _ _, .const _, h => by simp [beqE] at h
299
+ | .get _ _, .add _ _, h => by simp [beqE] at h
300
+ | .get _ _, .sub _ _, h => by simp [beqE] at h
301
+ | .get _ _, .mul _ _, h => by simp [beqE] at h
302
+ | .get _ _, .min _ _, h => by simp [beqE] at h
303
+ | .get _ _, .max _ _, h => by simp [beqE] at h
304
+ | .get _ _, .prod _ _, h => by simp [beqE] at h
305
+ | .get _ _, .ite _ _ _, h => by simp [beqE] at h
306
+ | .get _ _, .tup _ _, h => by simp [beqE] at h
307
+ | .get _ _, .proj _ _, h => by simp [beqE] at h
308
+ | .tup _ _, .var _, h => by simp [beqE] at h
309
+ | .tup _ _, .const _, h => by simp [beqE] at h
310
+ | .tup _ _, .add _ _, h => by simp [beqE] at h
311
+ | .tup _ _, .sub _ _, h => by simp [beqE] at h
312
+ | .tup _ _, .mul _ _, h => by simp [beqE] at h
313
+ | .tup _ _, .min _ _, h => by simp [beqE] at h
314
+ | .tup _ _, .max _ _, h => by simp [beqE] at h
315
+ | .tup _ _, .prod _ _, h => by simp [beqE] at h
316
+ | .tup _ _, .ite _ _ _, h => by simp [beqE] at h
317
+ | .tup _ _, .get _ _, h => by simp [beqE] at h
318
+ | .tup _ _, .proj _ _, h => by simp [beqE] at h
319
+ | .proj _ _, .var _, h => by simp [beqE] at h
320
+ | .proj _ _, .const _, h => by simp [beqE] at h
321
+ | .proj _ _, .add _ _, h => by simp [beqE] at h
322
+ | .proj _ _, .sub _ _, h => by simp [beqE] at h
323
+ | .proj _ _, .mul _ _, h => by simp [beqE] at h
324
+ | .proj _ _, .min _ _, h => by simp [beqE] at h
325
+ | .proj _ _, .max _ _, h => by simp [beqE] at h
326
+ | .proj _ _, .prod _ _, h => by simp [beqE] at h
327
+ | .proj _ _, .ite _ _ _, h => by simp [beqE] at h
328
+ | .proj _ _, .get _ _, h => by simp [beqE] at h
329
+ | .proj _ _, .tup _ _, h => by simp [beqE] at h
330
+ theorem beqC_eq : ∀ {a b : C}, beqC a b = true → a = b
331
+ | .le x0 x1, .le y0 y1, h => by
332
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
333
+ rw [beqE_eq h.1, beqE_eq h.2]
334
+ | .lt x0 x1, .lt y0 y1, h => by
335
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
336
+ rw [beqE_eq h.1, beqE_eq h.2]
337
+ | .eq x0 x1, .eq y0 y1, h => by
338
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
339
+ rw [beqE_eq h.1, beqE_eq h.2]
340
+ | .and x0 x1, .and y0 y1, h => by
341
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
342
+ rw [beqC_eq h.1, beqC_eq h.2]
343
+ | .or x0 x1, .or y0 y1, h => by
344
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
345
+ rw [beqC_eq h.1, beqC_eq h.2]
346
+ | .not x0, .not y0, h => by
347
+ simp only [beqC, Bool.and_eq_true, beq_iff_eq] at h
348
+ rw [beqC_eq h]
349
+ | .le _ _, .lt _ _, h => by simp [beqC] at h
350
+ | .le _ _, .eq _ _, h => by simp [beqC] at h
351
+ | .le _ _, .and _ _, h => by simp [beqC] at h
352
+ | .le _ _, .or _ _, h => by simp [beqC] at h
353
+ | .le _ _, .not _, h => by simp [beqC] at h
354
+ | .lt _ _, .le _ _, h => by simp [beqC] at h
355
+ | .lt _ _, .eq _ _, h => by simp [beqC] at h
356
+ | .lt _ _, .and _ _, h => by simp [beqC] at h
357
+ | .lt _ _, .or _ _, h => by simp [beqC] at h
358
+ | .lt _ _, .not _, h => by simp [beqC] at h
359
+ | .eq _ _, .le _ _, h => by simp [beqC] at h
360
+ | .eq _ _, .lt _ _, h => by simp [beqC] at h
361
+ | .eq _ _, .and _ _, h => by simp [beqC] at h
362
+ | .eq _ _, .or _ _, h => by simp [beqC] at h
363
+ | .eq _ _, .not _, h => by simp [beqC] at h
364
+ | .and _ _, .le _ _, h => by simp [beqC] at h
365
+ | .and _ _, .lt _ _, h => by simp [beqC] at h
366
+ | .and _ _, .eq _ _, h => by simp [beqC] at h
367
+ | .and _ _, .or _ _, h => by simp [beqC] at h
368
+ | .and _ _, .not _, h => by simp [beqC] at h
369
+ | .or _ _, .le _ _, h => by simp [beqC] at h
370
+ | .or _ _, .lt _ _, h => by simp [beqC] at h
371
+ | .or _ _, .eq _ _, h => by simp [beqC] at h
372
+ | .or _ _, .and _ _, h => by simp [beqC] at h
373
+ | .or _ _, .not _, h => by simp [beqC] at h
374
+ | .not _, .le _ _, h => by simp [beqC] at h
375
+ | .not _, .lt _ _, h => by simp [beqC] at h
376
+ | .not _, .eq _ _, h => by simp [beqC] at h
377
+ | .not _, .and _ _, h => by simp [beqC] at h
378
+ | .not _, .or _ _, h => by simp [beqC] at h
379
+ end
380
+
381
+ mutual
382
+ def evalE (env : Nat → Nat) (maps : Nat → Nat → Nat) : E → Nat
383
+ | .var i => env i
384
+ | .const n => n
385
+ | .add a b => evalE env maps a + evalE env maps b
386
+ | .sub a b => evalE env maps a - evalE env maps b
387
+ | .mul k a => k * evalE env maps a
388
+ | .min a b => min (evalE env maps a) (evalE env maps b)
389
+ | .max a b => max (evalE env maps a) (evalE env maps b)
390
+ | .prod a b => evalE env maps a * evalE env maps b
391
+ | .ite c t f => if evalC env maps c then evalE env maps t else evalE env maps f
392
+ | .get m key => maps m (evalE env maps key)
393
+ | .tup a b => cpair (evalE env maps a) (evalE env maps b)
394
+ | .proj k a => pickP k (evalE env maps a)
395
+ def evalC (env : Nat → Nat) (maps : Nat → Nat → Nat) : C → Bool
396
+ | .le a b => decide (evalE env maps a ≤ evalE env maps b)
397
+ | .lt a b => decide (evalE env maps a < evalE env maps b)
398
+ | .eq a b => decide (evalE env maps a = evalE env maps b)
399
+ | .and a b => evalC env maps a && evalC env maps b
400
+ | .or a b => evalC env maps a || evalC env maps b
401
+ | .not a => !evalC env maps a
402
+ end
403
+
404
+ -- A condition's meaning as a proposition: what a law says in plain Lean (`evalC_iff`). Exported laws are stated with it,
405
+ -- so a statement like `ρ x ≤ ρ cap` is the law itself, up to unfolding.
406
+ def holdsC (env : Nat → Nat) (maps : Nat → Nat → Nat) : C → Prop
407
+ | .le a b => evalE env maps a ≤ evalE env maps b
408
+ | .lt a b => evalE env maps a < evalE env maps b
409
+ | .eq a b => evalE env maps a = evalE env maps b
410
+ | .and a b => holdsC env maps a ∧ holdsC env maps b
411
+ | .or a b => holdsC env maps a ∨ holdsC env maps b
412
+ | .not a => ¬ holdsC env maps a
413
+
414
+ theorem evalC_iff (env : Nat → Nat) (maps : Nat → Nat → Nat) : ∀ c, evalC env maps c = true ↔ holdsC env maps c
415
+ | .le a b => by simp [evalC, holdsC]
416
+ | .lt a b => by simp [evalC, holdsC]
417
+ | .eq a b => by simp [evalC, holdsC]
418
+ | .and a b => by simp [evalC, holdsC, evalC_iff env maps a, evalC_iff env maps b]
419
+ | .or a b => by simp [evalC, holdsC, evalC_iff env maps a, evalC_iff env maps b]
420
+ | .not a => by
421
+ have h := evalC_iff env maps a
422
+ simp only [evalC, holdsC]
423
+ cases hb : evalC env maps a <;> simp_all
424
+
425
+ /-! Linear forms are Cert rows: `eval r x = dot r.coefs x + r.const`. -/
426
+
427
+ def lconst (n : Int) : Row := ⟨[], n⟩
428
+ def ladd (a b : Row) : Row := ⟨addL a.coefs b.coefs, a.const + b.const⟩
429
+ def lscale (m : Int) (a : Row) : Row := ⟨scaleL m a.coefs, m * a.const⟩
430
+ def lsub (a b : Row) : Row := ladd a (lscale (-1) b)
431
+ def lunit (j : Nat) : Row := ⟨[(j, 1)], 0⟩
432
+ -- A row as the exporters write it: its nonzero coefficients (column, coefficient), in column order, and its constant.
433
+ def sp (cs : List (Nat × Int)) (c : Int) : Row := ⟨cs, c⟩
434
+ def same (a b : Row) : Bool := allZero (lsub a b).coefs && (lsub a b).const == 0
435
+ -- 1 − a: over the integers, a ≤ 0 fails exactly when 1 − a ≤ 0 holds.
436
+ def flip (a : Row) : Row := lsub (lconst 1) a
437
+
438
+ theorem eval_lconst (n : Int) (x : List Int) : eval (lconst n) x = n := by
439
+ cases x <;> simp [eval, lconst, dot]
440
+ theorem eval_ladd (a b : Row) (x : List Int) : eval (ladd a b) x = eval a x + eval b x := by
441
+ simp only [eval, ladd, dot_addL]; omega
442
+ theorem eval_lscale (m : Int) (a : Row) (x : List Int) : eval (lscale m a) x = m * eval a x := by
443
+ simp only [eval, lscale, dot_scaleL, Int.mul_add]
444
+ theorem eval_lsub (a b : Row) (x : List Int) : eval (lsub a b) x = eval a x - eval b x := by
445
+ simp only [lsub, eval_ladd, eval_lscale]; omega
446
+ theorem eval_flip (a : Row) (x : List Int) : eval (flip a) x = 1 - eval a x := by
447
+ simp only [flip, eval_lsub, eval_lconst]
448
+ theorem eval_lunit (j : Nat) (x : List Int) : eval (lunit j) x = x.getD j 0 := by
449
+ simp [eval, lunit, dot]
450
+ theorem same_eval {a b : Row} (h : same a b = true) (x : List Int) : eval a x = eval b x := by
451
+ simp only [same, Bool.and_eq_true, beq_iff_eq] at h
452
+ have h1 : eval (lsub a b) x = 0 := by simp only [eval, dot_allZero _ x h.1, h.2]; rfl
453
+ rw [eval_lsub] at h1; omega
454
+
455
+ /-! The path: atoms decided so far. `(l, true)` says l ≤ 0, `(l, false)` says l ≥ 1. -/
456
+
457
+ abbrev Path := List (Row × Bool)
458
+ def PathHolds (path : Path) (x : List Int) : Prop := ∀ a ∈ path, decide (eval a.1 x ≤ 0) = a.2
459
+
460
+ def lookup {α : Type} (p : α → Bool) : List α → Option α
461
+ | [] => none
462
+ | a :: as => if p a then some a else lookup p as
463
+ theorem lookup_some {α : Type} {p : α → Bool} : ∀ {l : List α} {a : α}, lookup p l = some a → a ∈ l ∧ p a = true
464
+ | [], _, h => by simp [lookup] at h
465
+ | b :: bs, a, h => by
466
+ simp only [lookup] at h
467
+ split at h
468
+ · simp only [Option.some.injEq] at h; subst h; exact ⟨List.mem_cons_self, by assumption⟩
469
+ · have := lookup_some h; exact ⟨List.mem_cons_of_mem _ this.1, this.2⟩
470
+
471
+ def dec (path : Path) (l : Row) : Option Bool :=
472
+ if allZero l.coefs then some (decide (l.const ≤ 0))
473
+ else match lookup (fun a => same a.1 l) path with
474
+ | some a => some a.2
475
+ | none => match lookup (fun a => same a.1 (flip l)) path with
476
+ | some a => some (!a.2)
477
+ | none => none
478
+
479
+ theorem dec_sound {path : Path} {x : List Int} (hP : PathHolds path x) {l : Row} {b : Bool}
480
+ (h : dec path l = some b) : decide (eval l x ≤ 0) = b := by
481
+ simp only [dec] at h
482
+ split at h
483
+ · rename_i hz
484
+ simp only [Option.some.injEq] at h
485
+ subst h
486
+ simp [eval, dot_allZero _ x hz]
487
+ · split at h
488
+ · rename_i a ha
489
+ simp only [Option.some.injEq] at h; subst h
490
+ have ⟨hm, hs⟩ := lookup_some ha
491
+ rw [← hP a hm, same_eval hs]
492
+ · split at h
493
+ · rename_i a ha
494
+ simp only [Option.some.injEq] at h; subst h
495
+ have ⟨hm, hs⟩ := lookup_some ha
496
+ have := hP a hm
497
+ rw [same_eval hs, eval_flip] at this
498
+ rw [← this]
499
+ by_cases hl : eval l x ≤ 0 <;> simp [hl] <;> omega
500
+ · simp at h
501
+
502
+ /-! The table: column j is a variable of the environment, or map m's cell at a key (a row over earlier columns). -/
503
+
504
+ inductive VDef where
505
+ | var (i : Nat)
506
+ | cell (m : Nat) (key : Row)
507
+ | cellT (m : Nat) (keys : List Row) -- map m's cell at the tuple of the keys (`tupk` of their values)
508
+ | mono (fs : List Nat) -- the product of earlier columns (a monomial)
509
+ | term (e : E) -- a choice the path leaves open (∸, min, max, if), standing for its value
510
+
511
+ def prodV (x : List Int) : List Nat → Int
512
+ | [] => 1
513
+ | j :: js => x.getD j 0 * prodV x js
514
+
515
+ def valD (env : Nat → Nat) (maps : Nat → Nat → Nat) : VDef → List Int → Int
516
+ | .var i, _ => (env i : Int)
517
+ | .cell m k, x => (maps m (eval k x).toNat : Int)
518
+ | .cellT m ks, x => (maps m (tupk (ks.map fun k => (eval k x).toNat)) : Int)
519
+ | .mono fs, x => ((prodV x fs).toNat : Int)
520
+ | .term e, _ => (evalE env maps e : Int)
521
+
522
+ -- x is a valuation of the table: each column holds what its definition says, and every value is a natural.
523
+ def Model (tab : List VDef) (env : Nat → Nat) (maps : Nat → Nat → Nat) (x : List Int) : Prop :=
524
+ (∀ j d, tab[j]? = some d → x.getD j 0 = valD env maps d x) ∧ ∀ j, 0 ≤ x.getD j 0
525
+
526
+ def findIdx {α : Type} (p : α → Bool) : List α → Option Nat
527
+ | [] => none
528
+ | a :: as => if p a then some 0 else (findIdx p as).map (· + 1)
529
+ theorem findIdx_some {α : Type} {p : α → Bool} : ∀ {l : List α} {j : Nat}, findIdx p l = some j → ∃ a, l[j]? = some a ∧ p a = true
530
+ | [], _, h => by simp [findIdx] at h
531
+ | b :: bs, j, h => by
532
+ simp only [findIdx] at h
533
+ split at h
534
+ · simp only [Option.some.injEq] at h; subst h; exact ⟨b, rfl, by assumption⟩
535
+ · cases hj : findIdx p bs with
536
+ | none => simp [hj] at h
537
+ | some i =>
538
+ simp only [hj, Option.map_some, Option.some.injEq] at h; subst h
539
+ obtain ⟨a, ha, hp⟩ := findIdx_some (l := bs) hj
540
+ exact ⟨a, by simpa using ha, hp⟩
541
+
542
+ def isVar (i : Nat) : VDef → Bool
543
+ | .var i' => i' == i
544
+ | _ => false
545
+ def isCell (m : Nat) (k : Row) : VDef → Bool
546
+ | .cell m' k' => m' == m && same k' k
547
+ | _ => false
548
+ -- Rows equal pairwise (`same`), the lists of one length.
549
+ def sameL : List Row → List Row → Bool
550
+ | [], [] => true
551
+ | a :: as, b :: bs => same a b && sameL as bs
552
+ | _, _ => false
553
+ def isCellT (m : Nat) (ks : List Row) : VDef → Bool
554
+ | .cellT m' ks' => m' == m && sameL ks' ks
555
+ | _ => false
556
+ def isTerm (e : E) : VDef → Bool
557
+ | .term e' => beqE e' e
558
+ | _ => false
559
+ -- An open choice's column: the table's column for the whole term, if it has one.
560
+ def termCol (tab : List VDef) (e : E) : Option Row := (findIdx (isTerm e) tab).map lunit
561
+ def isMono (fs : List Nat) : VDef → Bool
562
+ | .mono fs' => fs' == fs
563
+ | _ => false
564
+
565
+ /-! Products: monomials are columns, each the product of the columns its factors name (sorted, so one monomial is one
566
+ column). Multiplying two rows looks up the column of each product of two columns. -/
567
+
568
+ def ins (a : Nat) : List Nat → List Nat
569
+ | [] => [a]
570
+ | b :: bs => if a ≤ b then a :: b :: bs else b :: ins a bs
571
+ def srt : List Nat → List Nat
572
+ | [] => []
573
+ | a :: as => ins a (srt as)
574
+
575
+ theorem prodV_ins (x : List Int) (a : Nat) : ∀ l, prodV x (ins a l) = x.getD a 0 * prodV x l
576
+ | [] => by simp [ins, prodV]
577
+ | b :: bs => by
578
+ simp only [ins]; split
579
+ · rfl
580
+ · simp only [prodV, prodV_ins x a bs]; rw [Int.mul_left_comm]
581
+ theorem prodV_srt (x : List Int) : ∀ l, prodV x (srt l) = prodV x l
582
+ | [] => rfl
583
+ | a :: as => by simp only [srt, prodV_ins, prodV_srt x as, prodV]
584
+ theorem prodV_append (x : List Int) : ∀ l1 l2, prodV x (l1 ++ l2) = prodV x l1 * prodV x l2
585
+ | [], l2 => by simp [prodV]
586
+ | a :: as, l2 => by simp only [List.cons_append, prodV, prodV_append x as l2, Int.mul_assoc]
587
+ theorem prodV_nonneg {x : List Int} (h : ∀ j, 0 ≤ x.getD j 0) : ∀ l, 0 ≤ prodV x l
588
+ | [] => by simp [prodV]
589
+ | a :: as => Int.mul_nonneg (h a) (prodV_nonneg h as)
590
+
591
+ -- A column's factors: a monomial's own, or the column itself (a variable, a map's cell, an open choice).
592
+ def fac (tab : List VDef) (i : Nat) : Option (List Nat) :=
593
+ match tab[i]? with
594
+ | some (.mono fs) => some fs
595
+ | some _ => some [i]
596
+ | none => none
597
+ def pcol (tab : List VDef) (i j : Nat) : Option Nat :=
598
+ match fac tab i, fac tab j with
599
+ | some a, some b => findIdx (isMono (srt (a ++ b))) tab
600
+ | _, _ => none
601
+
602
+ -- x_i · Σ b x_j, over b's coefficients (column j, coefficient b)
603
+ def mulRow (tab : List VDef) (i : Nat) : List (Nat × Int) → Option Row
604
+ | [] => some (lconst 0)
605
+ | q :: bs => match mulRow tab i bs with
606
+ | none => none
607
+ | some r => if q.2 == 0 then some r else match pcol tab i q.1 with
608
+ | some k => some (ladd (lscale q.2 (lunit k)) r)
609
+ | none => none
610
+ -- (Σ a x_i) · Σ b x_j
611
+ def mulRows (tab : List VDef) (bs : List (Nat × Int)) : List (Nat × Int) → Option Row
612
+ | [] => some (lconst 0)
613
+ | p :: as => match mulRows tab bs as with
614
+ | none => none
615
+ | some r => if p.2 == 0 then some r else match mulRow tab p.1 bs with
616
+ | some q => some (ladd (lscale p.2 q) r)
617
+ | none => none
618
+ def rowMul (tab : List VDef) (a b : Row) : Option Row :=
619
+ match mulRows tab b.coefs a.coefs with
620
+ | some p => some (ladd p (ladd (lscale a.const ⟨b.coefs, 0⟩) (ladd (lscale b.const ⟨a.coefs, 0⟩) (lconst (a.const * b.const)))))
621
+ | none => none
622
+
623
+ /-! Tuples. `shapeT` reads a tuple's term as its tree of pairs, each leaf a part that isn't a tuple, with its linear form
624
+ if it has one. Two tuples are equal exactly when their parts are, pair by pair (`cpair` is a bijection), so the truth and
625
+ the facts of their equality are those of the conjunction of their parts' equalities. A map's cell at a right-nested
626
+ tuple whose parts have linear forms is a column of its own (`cellT`), keyed by the parts' forms (`parts`): a tuple's,
627
+ or a projection's (the second part of a tuple of three or more is itself a tuple, whose parts are the rest). -/
628
+
629
+ inductive Sh where
630
+ | leaf (r : Option Row)
631
+ | node (a b : Sh)
632
+
633
+ def isTup : E → Bool
634
+ | .tup _ _ => true
635
+ | _ => false
636
+
637
+ -- Kleene's and: the truth of `.and`.
638
+ def kand : Option Bool → Option Bool → Option Bool
639
+ | some false, _ => some false
640
+ | _, some false => some false
641
+ | some true, some true => some true
642
+ | _, _ => none
643
+
644
+ -- The truth of an equality of two linear forms under the path.
645
+ def eqL (path : Path) (la lb : Row) : Option Bool :=
646
+ match dec path (lsub la lb), dec path (lsub lb la) with
647
+ | some false, _ => some false
648
+ | _, some false => some false
649
+ | some true, some true => some true
650
+ | _, _ => none
651
+
652
+ -- Two trees of linear forms equal: leaves by their forms, pairs part by part.
653
+ def eqSh (path : Path) : Sh → Sh → Option Bool
654
+ | .leaf (some la), .leaf (some lb) => eqL path la lb
655
+ | .node s1 s2, .node t1 t2 => kand (eqSh path s1 t1) (eqSh path s2 t2)
656
+ | _, _ => none
657
+
658
+ mutual
659
+ def linear (tab : List VDef) (path : Path) : E → Option Row
660
+ | .var i => (findIdx (isVar i) tab).map lunit
661
+ | .const n => some (lconst n)
662
+ | .add a b => match linear tab path a, linear tab path b with
663
+ | some la, some lb => some (ladd la lb)
664
+ | _, _ => none
665
+ | .mul k a => match linear tab path a with
666
+ | some la => some (lscale k la)
667
+ | none => none
668
+ | .sub a b => match linear tab path a, linear tab path b with
669
+ | some la, some lb => match dec path (lsub lb la) with
670
+ | some true => some (lsub la lb)
671
+ | some false => some (lconst 0)
672
+ | none => termCol tab (.sub a b)
673
+ | _, _ => none
674
+ | .min a b => match linear tab path a, linear tab path b with
675
+ | some la, some lb => match dec path (lsub la lb) with
676
+ | some true => some la
677
+ | some false => some lb
678
+ | none => termCol tab (.min a b)
679
+ | _, _ => none
680
+ | .max a b => match linear tab path a, linear tab path b with
681
+ | some la, some lb => match dec path (lsub la lb) with
682
+ | some true => some lb
683
+ | some false => some la
684
+ | none => termCol tab (.max a b)
685
+ | _, _ => none
686
+ | .prod a b => match linear tab path a, linear tab path b with
687
+ | some la, some lb => rowMul tab la lb
688
+ | _, _ => none
689
+ | .ite c t f => match truth tab path c with
690
+ | some true => linear tab path t
691
+ | some false => linear tab path f
692
+ | none => termCol tab (.ite c t f)
693
+ | .get m key => match parts tab path key with
694
+ | some rs => (findIdx (isCellT m rs) tab).map lunit
695
+ | none => match linear tab path key with
696
+ | some lk => (findIdx (isCell m lk) tab).map lunit
697
+ | none => none
698
+ | .tup _ _ => none
699
+ | .proj k a => if k = 0 then
700
+ (match parts tab path a with
701
+ | some (r :: _) => some r
702
+ | _ => none)
703
+ else
704
+ (match parts tab path a with
705
+ | some [_, r] => some r
706
+ | _ => none)
707
+ def truth (tab : List VDef) (path : Path) : C → Option Bool
708
+ | .le a b => match linear tab path a, linear tab path b with
709
+ | some la, some lb => dec path (lsub la lb)
710
+ | _, _ => none
711
+ | .lt a b => match linear tab path a, linear tab path b with
712
+ | some la, some lb => dec path (ladd (lsub la lb) (lconst 1))
713
+ | _, _ => none
714
+ | .eq a b => if isTup a && isTup b then eqSh path (shapeT tab path a) (shapeT tab path b)
715
+ else match linear tab path a, linear tab path b with
716
+ | some la, some lb => eqL path la lb
717
+ | _, _ => none
718
+ | .and a b => match truth tab path a, truth tab path b with
719
+ | some false, _ => some false
720
+ | _, some false => some false
721
+ | some true, some true => some true
722
+ | _, _ => none
723
+ | .or a b => match truth tab path a, truth tab path b with
724
+ | some true, _ => some true
725
+ | _, some true => some true
726
+ | some false, some false => some false
727
+ | _, _ => none
728
+ | .not a => match truth tab path a with
729
+ | some b => some (!b)
730
+ | none => none
731
+ -- A tuple's tree of linear forms; anything else has none.
732
+ def shapeT (tab : List VDef) (path : Path) : E → Sh
733
+ | .tup a b => .node (if isTup a then shapeT tab path a else .leaf (linear tab path a))
734
+ (if isTup b then shapeT tab path b else .leaf (linear tab path b))
735
+ | _ => .leaf none
736
+ -- A tuple's parts' linear forms (two or more): a tup's, its first part's form then its second's parts (its form, if
737
+ -- it is no tuple); a second part's (proj k with k ≠ 0), the rest of its tuple's parts when they are two or more.
738
+ def parts (tab : List VDef) (path : Path) : E → Option (List Row)
739
+ | .tup a b => match linear tab path a, (if isTup b then parts tab path b else (linear tab path b).map (fun r => [r])) with
740
+ | some r, some rs => some (r :: rs)
741
+ | _, _ => none
742
+ | .proj k a => if k = 0 then none else
743
+ (match parts tab path a with
744
+ | some (_ :: rest) => if 2 ≤ rest.length then some rest else none
745
+ | _ => none)
746
+ | _ => none
747
+ end
748
+
749
+ -- Arity used by the TypeScript scope/runtime boundary: read the same parts as a cell, including a projected tail.
750
+ -- If no linear parts exist, retain the syntactic arity (scope can still reject the term for another reason).
751
+ def tupleArity : E → Nat
752
+ | .tup _ b => 1 + tupleArity b
753
+ | _ => 1
754
+
755
+ def keyArity (tab : List VDef) (path : Path) (e : E) : Nat :=
756
+ match parts tab path e with
757
+ | some ps => ps.length
758
+ | none => tupleArity e
759
+
760
+ theorem keyArity_of_parts {tab : List VDef} {path : Path} {e : E} {ps : List Row}
761
+ (h : parts tab path e = some ps) : keyArity tab path e = ps.length := by
762
+ simp only [keyArity, h]
763
+
764
+ example : keyArity [] [] (.proj 1 (.tup (.const 0) (.tup (.const 1) (.const 2)))) = 2 := by decide +kernel
765
+ example : keyArity [] [] (.proj 1 (.tup (.const 0) (.const 1))) = 1 := by decide +kernel
766
+
767
+ -- What a tree of linear forms says of a natural: a leaf's form is its value, a pair's parts are its parts'.
768
+ def DenSh (x : List Int) : Sh → Nat → Prop
769
+ | .leaf none, _ => True
770
+ | .leaf (some r), v => eval r x = (v : Int)
771
+ | .node s t, v => DenSh x s (cun v).1 ∧ DenSh x t (cun v).2
772
+
773
+ theorem decide_cast_eq (a b : Nat) : decide ((a : Int) = (b : Int)) = decide (a = b) :=
774
+ decide_eq_decide.mpr (by constructor <;> intro h <;> omega)
775
+
776
+ theorem decide_eq_cun (n1 n2 : Nat) :
777
+ decide (n1 = n2) = (decide ((cun n1).1 = (cun n2).1) && decide ((cun n1).2 = (cun n2).2)) := by
778
+ rw [← Bool.decide_and]; exact decide_eq_decide.mpr (eq_iff_cun n1 n2)
779
+
780
+ theorem kand_sound {P Q r : Bool} {a b : Option Bool} (ha : ∀ v, a = some v → P = v) (hb : ∀ v, b = some v → Q = v)
781
+ (h : kand a b = some r) : (P && Q) = r := by
782
+ rcases a with _ | _ | _ <;> rcases b with _ | _ | _ <;> simp only [kand, Option.some.injEq, reduceCtorEq] at h <;>
783
+ subst h <;> simp_all
784
+
785
+ theorem eqL_sound {path : Path} {x : List Int} (hP : PathHolds path x) {la lb : Row} {b : Bool}
786
+ (h : eqL path la lb = some b) : decide (eval la x = eval lb x) = b := by
787
+ simp only [eqL] at h
788
+ revert h
789
+ rcases h1 : dec path (lsub la lb) with _ | _ | _ <;> rcases h2 : dec path (lsub lb la) with _ | _ | _ <;>
790
+ intro h <;> simp only [Option.some.injEq, reduceCtorEq] at h <;> subst h <;>
791
+ first
792
+ | (have t1 := dec_sound hP h1; have t2 := dec_sound hP h2; rw [eval_lsub] at t1 t2
793
+ simp only [decide_eq_true_eq, decide_eq_false_iff_not] at t1 t2 ⊢; omega)
794
+ | (have t1 := dec_sound hP h1; rw [eval_lsub] at t1
795
+ simp only [decide_eq_true_eq, decide_eq_false_iff_not] at t1 ⊢; omega)
796
+ | (have t2 := dec_sound hP h2; rw [eval_lsub] at t2
797
+ simp only [decide_eq_true_eq, decide_eq_false_iff_not] at t2 ⊢; omega)
798
+
799
+ theorem eqSh_sound {path : Path} {x : List Int} (hP : PathHolds path x) :
800
+ ∀ (s t : Sh) (n1 n2 : Nat) (b : Bool), DenSh x s n1 → DenSh x t n2 → eqSh path s t = some b → decide (n1 = n2) = b
801
+ | .leaf (some la), .leaf (some lb), n1, n2, b, h1, h2, h => by
802
+ simp only [eqSh] at h
803
+ have := eqL_sound hP h
804
+ simp only [DenSh] at h1 h2
805
+ rw [h1, h2, decide_cast_eq] at this
806
+ exact this
807
+ | .node s1 s2, .node t1 t2, n1, n2, b, h1, h2, h => by
808
+ simp only [DenSh] at h1 h2
809
+ simp only [eqSh] at h
810
+ rw [decide_eq_cun]
811
+ exact kand_sound (fun v hv => eqSh_sound hP s1 t1 _ _ v h1.1 h2.1 hv) (fun v hv => eqSh_sound hP s2 t2 _ _ v h1.2 h2.2 hv) h
812
+ | .leaf none, .leaf _, _, _, _, _, _, h => by simp [eqSh] at h
813
+ | .leaf none, .node _ _, _, _, _, _, _, h => by simp [eqSh] at h
814
+ | .leaf (some _), .leaf none, _, _, _, _, _, h => by simp [eqSh] at h
815
+ | .leaf (some _), .node _ _, _, _, _, _, _, h => by simp [eqSh] at h
816
+ | .node _ _, .leaf _, _, _, _, _, _, h => by simp [eqSh] at h
817
+
818
+ -- Rows and the naturals they evaluate to, one by one.
819
+ def RowsVal (x : List Int) : List Row → List Nat → Prop
820
+ | [], [] => True
821
+ | r :: rs, v :: vs => eval r x = (v : Int) ∧ RowsVal x rs vs
822
+ | _, _ => False
823
+
824
+ theorem rowsVal_len {x : List Int} : ∀ {rs : List Row} {vs : List Nat}, RowsVal x rs vs → rs.length = vs.length
825
+ | [], [], _ => rfl
826
+ | _ :: _, _ :: _, h => by simp only [List.length_cons, rowsVal_len h.2]
827
+ | [], _ :: _, h => h.elim
828
+ | _ :: _, [], h => h.elim
829
+
830
+ theorem rowsVal_map {x : List Int} : ∀ {rs : List Row} {vs : List Nat}, RowsVal x rs vs →
831
+ rs.map (fun r => (eval r x).toNat) = vs
832
+ | [], [], _ => rfl
833
+ | _ :: _, _ :: _, h => by simp only [List.map_cons, h.1, rowsVal_map h.2, Int.toNat_natCast]
834
+ | [], _ :: _, h => h.elim
835
+ | _ :: _, [], h => h.elim
836
+
837
+ theorem tupk_cons_ne {v : Nat} {vs : List Nat} (h : vs ≠ []) : tupk (v :: vs) = cpair v (tupk vs) := by
838
+ cases vs with
839
+ | nil => exact absurd rfl h
840
+ | cons _ _ => rfl
841
+
842
+ theorem sameL_map {x : List Int} : ∀ {as bs : List Row}, sameL as bs = true →
843
+ as.map (fun r => (eval r x).toNat) = bs.map (fun r => (eval r x).toNat)
844
+ | [], [], _ => rfl
845
+ | a :: as, b :: bs, h => by
846
+ simp only [sameL, Bool.and_eq_true] at h
847
+ simp only [List.map_cons, same_eval h.1 x, sameL_map h.2]
848
+ | [], _ :: _, h => by simp [sameL] at h
849
+ | _ :: _, [], h => by simp [sameL] at h
850
+
851
+ section
852
+ variable {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
853
+
854
+ theorem var_col (hM : Model tab env maps x) {i j : Nat} (h : findIdx (isVar i) tab = some j) : x.getD j 0 = env i := by
855
+ obtain ⟨d, hd, hv⟩ := findIdx_some h
856
+ rw [hM.1 j d hd]
857
+ cases d with
858
+ | var i' => simp only [isVar, beq_iff_eq] at hv; subst hv; rfl
859
+ | _ => simp [isVar] at hv
860
+
861
+ theorem term_col (hM : Model tab env maps x) {e : E} {j : Nat} (h : findIdx (isTerm e) tab = some j) :
862
+ x.getD j 0 = (evalE env maps e : Int) := by
863
+ obtain ⟨d, hd, hv⟩ := findIdx_some h
864
+ rw [hM.1 j d hd]
865
+ cases d with
866
+ | term e' => simp only [isTerm] at hv; have he := beqE_eq hv; subst he; rfl
867
+ | _ => simp [isTerm] at hv
868
+
869
+ theorem termCol_sound (hM : Model tab env maps x) {e : E} {l : Row} (h : termCol tab e = some l) :
870
+ eval l x = (evalE env maps e : Int) := by
871
+ simp only [termCol] at h
872
+ cases hj : findIdx (isTerm e) tab with
873
+ | none => simp [hj] at h
874
+ | some j => simp only [hj, Option.map_some, Option.some.injEq] at h; subst h; rw [eval_lunit, term_col hM hj]
875
+
876
+ theorem cell_col (hM : Model tab env maps x) {m j : Nat} {k : Row} (h : findIdx (isCell m k) tab = some j) :
877
+ x.getD j 0 = (maps m (eval k x).toNat : Int) := by
878
+ obtain ⟨d, hd, hv⟩ := findIdx_some h
879
+ rw [hM.1 j d hd]
880
+ cases d with
881
+ | cell m' k' =>
882
+ simp only [isCell, Bool.and_eq_true, beq_iff_eq] at hv
883
+ obtain ⟨rfl, hs⟩ := hv
884
+ simp only [valD, same_eval hs x]
885
+ | _ => simp [isCell] at hv
886
+
887
+ theorem cellT_col (hM : Model tab env maps x) {m j : Nat} {ks : List Row} (h : findIdx (isCellT m ks) tab = some j) :
888
+ x.getD j 0 = (maps m (tupk (ks.map fun r => (eval r x).toNat)) : Int) := by
889
+ obtain ⟨d, hd, hv⟩ := findIdx_some h
890
+ rw [hM.1 j d hd]
891
+ cases d with
892
+ | cellT m' ks' =>
893
+ simp only [isCellT, Bool.and_eq_true, beq_iff_eq] at hv
894
+ obtain ⟨rfl, hs⟩ := hv
895
+ simp only [valD, sameL_map hs]
896
+ | _ => simp [isCellT] at hv
897
+
898
+ theorem fac_val (hM : Model tab env maps x) {i : Nat} {fs : List Nat} (h : fac tab i = some fs) : x.getD i 0 = prodV x fs := by
899
+ simp only [fac] at h
900
+ split at h
901
+ · rename_i fs' hd
902
+ simp only [Option.some.injEq] at h; subst h
903
+ rw [hM.1 i _ hd]; simp only [valD]; exact Int.toNat_of_nonneg (prodV_nonneg hM.2 _)
904
+ · simp only [Option.some.injEq] at h; subst h; simp [prodV]
905
+ · simp at h
906
+
907
+ theorem pcol_val (hM : Model tab env maps x) {i j k : Nat} (h : pcol tab i j = some k) :
908
+ x.getD k 0 = x.getD i 0 * x.getD j 0 := by
909
+ simp only [pcol] at h
910
+ split at h
911
+ · rename_i a b ha hb
912
+ obtain ⟨d, hd, hv⟩ := findIdx_some h
913
+ cases d with
914
+ | mono fs =>
915
+ simp only [isMono, beq_iff_eq] at hv; subst hv
916
+ rw [hM.1 k _ hd]; simp only [valD]
917
+ rw [Int.toNat_of_nonneg (prodV_nonneg hM.2 _), prodV_srt, prodV_append, ← fac_val hM ha, ← fac_val hM hb]
918
+ | _ => simp [isMono] at hv
919
+ · simp at h
920
+
921
+ theorem mulRow_sound (hM : Model tab env maps x) (i : Nat) :
922
+ ∀ bs r, mulRow tab i bs = some r → eval r x = x.getD i 0 * dot bs x
923
+ | [], r, h => by simp only [mulRow, Option.some.injEq] at h; subst h; simp [eval_lconst, dot]
924
+ | q :: bs, r, h => by
925
+ simp only [mulRow] at h
926
+ split at h
927
+ · simp at h
928
+ · rename_i r' hr'
929
+ have ih := mulRow_sound hM i bs r' hr'
930
+ split at h
931
+ · rename_i hb
932
+ simp only [Option.some.injEq] at h; subst h
933
+ simp only [beq_iff_eq] at hb; simp [dot, hb, ih]
934
+ · split at h
935
+ · rename_i k hk
936
+ simp only [Option.some.injEq] at h; subst h
937
+ rw [eval_ladd, eval_lscale, eval_lunit, pcol_val hM hk, ih]
938
+ simp only [dot]; rw [Int.mul_add, Int.mul_left_comm (x.getD i 0) q.2]
939
+ · simp at h
940
+
941
+ theorem mulRows_sound (hM : Model tab env maps x) (bs : List (Nat × Int)) :
942
+ ∀ as r, mulRows tab bs as = some r → eval r x = dot as x * dot bs x
943
+ | [], r, h => by simp only [mulRows, Option.some.injEq] at h; subst h; simp [eval_lconst, dot]
944
+ | p :: as, r, h => by
945
+ simp only [mulRows] at h
946
+ split at h
947
+ · simp at h
948
+ · rename_i r' hr'
949
+ have ih := mulRows_sound hM bs as r' hr'
950
+ split at h
951
+ · rename_i ha
952
+ simp only [Option.some.injEq] at h; subst h
953
+ simp only [beq_iff_eq] at ha; simp [dot, ha, ih]
954
+ · split at h
955
+ · rename_i q hq
956
+ simp only [Option.some.injEq] at h; subst h
957
+ rw [eval_ladd, eval_lscale, mulRow_sound hM p.1 bs q hq, ih]
958
+ simp only [dot]; rw [Int.add_mul, Int.mul_assoc]
959
+ · simp at h
960
+
961
+ theorem rowMul_sound (hM : Model tab env maps x) {a b r : Row} (h : rowMul tab a b = some r) : eval r x = eval a x * eval b x := by
962
+ simp only [rowMul] at h
963
+ split at h
964
+ · rename_i p hp
965
+ simp only [Option.some.injEq] at h; subst h
966
+ have e := mulRows_sound hM b.coefs a.coefs p hp
967
+ simp only [eval_ladd, eval_lscale, eval_lconst, e]
968
+ simp only [eval, Int.add_zero]
969
+ rw [Int.add_mul, Int.mul_add, Int.mul_add, Int.mul_comm b.const]; omega
970
+ · simp at h
971
+
972
+ mutual
973
+ theorem linear_sound (hM : Model tab env maps x) (hP : PathHolds path x) :
974
+ (e : E) → (l : Row) → linear tab path e = some l → eval l x = (evalE env maps e : Int)
975
+ | .var i, l, h => by
976
+ simp only [linear] at h
977
+ cases hj : findIdx (isVar i) tab with
978
+ | none => simp [hj] at h
979
+ | some j =>
980
+ simp only [hj, Option.map_some, Option.some.injEq] at h; subst h
981
+ rw [eval_lunit, var_col hM hj, evalE]
982
+ | .const n, l, h => by
983
+ simp only [linear, Option.some.injEq] at h; subst h
984
+ simp [eval_lconst, evalE]
985
+ | .add a b, l, h => by
986
+ simp only [linear] at h
987
+ split at h
988
+ · rename_i la lb ha hb
989
+ simp only [Option.some.injEq] at h; subst h
990
+ rw [eval_ladd, linear_sound hM hP a la ha, linear_sound hM hP b lb hb, evalE]; omega
991
+ · simp at h
992
+ | .mul k a, l, h => by
993
+ simp only [linear] at h
994
+ split at h
995
+ · rename_i la ha
996
+ simp only [Option.some.injEq] at h; subst h
997
+ rw [eval_lscale, linear_sound hM hP a la ha, evalE]; simp
998
+ · simp at h
999
+ | .sub a b, l, h => by
1000
+ simp only [linear] at h
1001
+ split at h
1002
+ · rename_i la lb ha hb
1003
+ have ea := linear_sound hM hP a la ha
1004
+ have eb := linear_sound hM hP b lb hb
1005
+ split at h
1006
+ · rename_i hd
1007
+ simp only [Option.some.injEq] at h; subst h
1008
+ have := dec_sound hP hd
1009
+ simp only [decide_eq_true_eq, eval_lsub] at this
1010
+ rw [eval_lsub, evalE]; omega
1011
+ · rename_i hd
1012
+ simp only [Option.some.injEq] at h; subst h
1013
+ have := dec_sound hP hd
1014
+ simp only [decide_eq_false_iff_not, eval_lsub] at this
1015
+ rw [eval_lconst, evalE]; omega
1016
+ · exact termCol_sound hM h
1017
+ · simp at h
1018
+ | .min a b, l, h => by
1019
+ simp only [linear] at h
1020
+ split at h
1021
+ · rename_i la lb ha hb
1022
+ have ea := linear_sound hM hP a la ha
1023
+ have eb := linear_sound hM hP b lb hb
1024
+ split at h
1025
+ · rename_i hd
1026
+ simp only [Option.some.injEq] at h; subst h
1027
+ have := dec_sound hP hd
1028
+ simp only [decide_eq_true_eq, eval_lsub] at this
1029
+ rw [evalE, Nat.min_def]; split <;> omega
1030
+ · rename_i hd
1031
+ simp only [Option.some.injEq] at h; subst h
1032
+ have := dec_sound hP hd
1033
+ simp only [decide_eq_false_iff_not, eval_lsub] at this
1034
+ rw [evalE, Nat.min_def]; split <;> omega
1035
+ · exact termCol_sound hM h
1036
+ · simp at h
1037
+ | .max a b, l, h => by
1038
+ simp only [linear] at h
1039
+ split at h
1040
+ · rename_i la lb ha hb
1041
+ have ea := linear_sound hM hP a la ha
1042
+ have eb := linear_sound hM hP b lb hb
1043
+ split at h
1044
+ · rename_i hd
1045
+ simp only [Option.some.injEq] at h; subst h
1046
+ have := dec_sound hP hd
1047
+ simp only [decide_eq_true_eq, eval_lsub] at this
1048
+ rw [evalE, Nat.max_def]; split <;> omega
1049
+ · rename_i hd
1050
+ simp only [Option.some.injEq] at h; subst h
1051
+ have := dec_sound hP hd
1052
+ simp only [decide_eq_false_iff_not, eval_lsub] at this
1053
+ rw [evalE, Nat.max_def]; split <;> omega
1054
+ · exact termCol_sound hM h
1055
+ · simp at h
1056
+ | .prod a b, l, h => by
1057
+ simp only [linear] at h
1058
+ split at h
1059
+ · rename_i la lb ha hb
1060
+ rw [rowMul_sound hM h, linear_sound hM hP a la ha, linear_sound hM hP b lb hb, evalE]; simp
1061
+ · simp at h
1062
+ | .ite c t f, l, h => by
1063
+ simp only [linear] at h
1064
+ split at h
1065
+ · rename_i hc
1066
+ have := truth_sound hM hP c true hc
1067
+ rw [linear_sound hM hP t l h, evalE, this]; rfl
1068
+ · rename_i hc
1069
+ have := truth_sound hM hP c false hc
1070
+ rw [linear_sound hM hP f l h, evalE, this]; rfl
1071
+ · exact termCol_sound hM h
1072
+ | .get m key, l, h => by
1073
+ simp only [linear] at h
1074
+ split at h
1075
+ · rename_i rs hrs
1076
+ cases hj : findIdx (isCellT m rs) tab with
1077
+ | none => simp [hj] at h
1078
+ | some j =>
1079
+ simp only [hj, Option.map_some, Option.some.injEq] at h; subst h
1080
+ obtain ⟨vs, hv, ht, _⟩ := parts_sound hM hP key rs hrs
1081
+ rw [eval_lunit, cellT_col hM hj, rowsVal_map hv, ht, evalE]
1082
+ · split at h
1083
+ · rename_i lk hk
1084
+ cases hj : findIdx (isCell m lk) tab with
1085
+ | none => simp [hj] at h
1086
+ | some j =>
1087
+ simp only [hj, Option.map_some, Option.some.injEq] at h; subst h
1088
+ rw [eval_lunit, cell_col hM hj, linear_sound hM hP key lk hk, evalE]; simp
1089
+ · simp at h
1090
+ | .tup _ _, l, h => by simp [linear] at h
1091
+ | .proj k a, l, h => by
1092
+ simp only [linear] at h
1093
+ split at h
1094
+ · rename_i hk
1095
+ split at h
1096
+ · rename_i r rs' hp
1097
+ simp only [Option.some.injEq] at h; subst h
1098
+ obtain ⟨vs, hv, ht, hl⟩ := parts_sound hM hP a _ hp
1099
+ match vs, hv with
1100
+ | v :: vs', hv =>
1101
+ have hne : vs' ≠ [] := by
1102
+ intro e; subst e; have := rowsVal_len hv.2; simp only [List.length_nil, List.length_cons] at hl this; omega
1103
+ rw [hv.1, evalE, ← ht, tupk_cons_ne hne]; simp [pickP, hk]
1104
+ · simp at h
1105
+ · rename_i hk
1106
+ split at h
1107
+ · rename_i r0 r hp
1108
+ simp only [Option.some.injEq] at h; subst h
1109
+ obtain ⟨vs, hv, ht, _⟩ := parts_sound hM hP a _ hp
1110
+ match vs, hv with
1111
+ | [v0, v], hv => rw [hv.2.1, evalE, ← ht, tupk_cons, tupk_one]; simp [pickP, hk]
1112
+ · simp at h
1113
+ theorem truth_sound (hM : Model tab env maps x) (hP : PathHolds path x) :
1114
+ (c : C) → (b : Bool) → truth tab path c = some b → evalC env maps c = b
1115
+ | .le a b', r, h => by
1116
+ simp only [truth] at h
1117
+ split at h
1118
+ · rename_i la lb ha hb
1119
+ have ea := linear_sound hM hP a la ha
1120
+ have eb := linear_sound hM hP b' lb hb
1121
+ have := dec_sound hP h
1122
+ rw [eval_lsub] at this
1123
+ simp only [evalC]; rw [← this]; simp only [decide_eq_decide]; omega
1124
+ · simp at h
1125
+ | .lt a b', r, h => by
1126
+ simp only [truth] at h
1127
+ split at h
1128
+ · rename_i la lb ha hb
1129
+ have ea := linear_sound hM hP a la ha
1130
+ have eb := linear_sound hM hP b' lb hb
1131
+ have := dec_sound hP h
1132
+ rw [eval_ladd, eval_lsub, eval_lconst] at this
1133
+ simp only [evalC]; rw [← this]; simp only [decide_eq_decide]; omega
1134
+ · simp at h
1135
+ | .eq a b', r, h => by
1136
+ simp only [truth] at h
1137
+ split at h
1138
+ · simp only [evalC]
1139
+ exact eqSh_sound hP _ _ _ _ r (shapeT_sound hM hP a) (shapeT_sound hM hP b') h
1140
+ · split at h
1141
+ · rename_i la lb ha hb
1142
+ have := eqL_sound hP h
1143
+ rw [linear_sound hM hP a la ha, linear_sound hM hP b' lb hb, decide_cast_eq] at this
1144
+ simp only [evalC]; exact this
1145
+ · simp at h
1146
+ | .and a b', r, h => by
1147
+ simp only [truth] at h
1148
+ simp only [evalC]
1149
+ split at h
1150
+ · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP a false h1]
1151
+ · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP b' false h2]
1152
+ · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP a true h1, truth_sound hM hP b' true h2]
1153
+ · simp at h
1154
+ | .or a b', r, h => by
1155
+ simp only [truth] at h
1156
+ simp only [evalC]
1157
+ split at h
1158
+ · rename_i h1; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP a true h1]
1159
+ · rename_i h2 _; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP b' true h2]
1160
+ · rename_i h1 h2; simp only [Option.some.injEq] at h; subst h; simp [truth_sound hM hP a false h1, truth_sound hM hP b' false h2]
1161
+ · simp at h
1162
+ | .not a, r, h => by
1163
+ simp only [truth] at h
1164
+ split at h
1165
+ · rename_i r' h1; simp only [Option.some.injEq] at h; subst h; simp [evalC, truth_sound hM hP a r' h1]
1166
+ · simp at h
1167
+ theorem shapeT_sound (hM : Model tab env maps x) (hP : PathHolds path x) :
1168
+ (e : E) → DenSh x (shapeT tab path e) (evalE env maps e)
1169
+ | .tup a b => by
1170
+ have ha : DenSh x (if isTup a then shapeT tab path a else .leaf (linear tab path a)) (evalE env maps a) := by
1171
+ split
1172
+ · exact shapeT_sound hM hP a
1173
+ · cases hl : linear tab path a with
1174
+ | none => exact trivial
1175
+ | some r => exact linear_sound hM hP a r hl
1176
+ have hb : DenSh x (if isTup b then shapeT tab path b else .leaf (linear tab path b)) (evalE env maps b) := by
1177
+ split
1178
+ · exact shapeT_sound hM hP b
1179
+ · cases hl : linear tab path b with
1180
+ | none => exact trivial
1181
+ | some r => exact linear_sound hM hP b r hl
1182
+ simp only [shapeT, evalE, DenSh, cun_cpair_fst, cun_cpair_snd]
1183
+ exact ⟨ha, hb⟩
1184
+ | .var _ => trivial
1185
+ | .const _ => trivial
1186
+ | .add _ _ => trivial
1187
+ | .sub _ _ => trivial
1188
+ | .mul _ _ => trivial
1189
+ | .min _ _ => trivial
1190
+ | .max _ _ => trivial
1191
+ | .prod _ _ => trivial
1192
+ | .ite _ _ _ => trivial
1193
+ | .get _ _ => trivial
1194
+ | .proj _ _ => trivial
1195
+ theorem parts_sound (hM : Model tab env maps x) (hP : PathHolds path x) :
1196
+ (e : E) → (rs : List Row) → parts tab path e = some rs →
1197
+ ∃ vs, RowsVal x rs vs ∧ tupk vs = evalE env maps e ∧ 2 ≤ rs.length
1198
+ | .tup a b, rs, h => by
1199
+ simp only [parts] at h
1200
+ split at h
1201
+ · rename_i r rs' ha hb
1202
+ simp only [Option.some.injEq] at h; subst h
1203
+ have ea := linear_sound hM hP a r ha
1204
+ split at hb
1205
+ · obtain ⟨vs, hv, ht, hl⟩ := parts_sound hM hP b rs' hb
1206
+ have hne : vs ≠ [] := by intro e; subst e; have := rowsVal_len hv; simp only [List.length_nil, List.length_cons] at hl this; omega
1207
+ refine ⟨evalE env maps a :: vs, ⟨ea, hv⟩, ?_, by simp; omega⟩
1208
+ rw [tupk_cons_ne hne, ht, evalE]
1209
+ · cases hlb : linear tab path b with
1210
+ | none => simp [hlb] at hb
1211
+ | some rb =>
1212
+ simp only [hlb, Option.map_some, Option.some.injEq] at hb; subst hb
1213
+ refine ⟨[evalE env maps a, evalE env maps b], ⟨ea, linear_sound hM hP b rb hlb, trivial⟩, ?_, by simp⟩
1214
+ simp [evalE]
1215
+ · simp at h
1216
+ | .proj k a, rs, h => by
1217
+ simp only [parts] at h
1218
+ split at h
1219
+ · simp at h
1220
+ · rename_i hk
1221
+ split at h
1222
+ · rename_i r0 rest hp
1223
+ split at h
1224
+ · rename_i hl
1225
+ simp only [Option.some.injEq] at h; subst h
1226
+ obtain ⟨vs, hv, ht, _⟩ := parts_sound hM hP a _ hp
1227
+ match vs, hv with
1228
+ | v0 :: vs', hv =>
1229
+ have hne : vs' ≠ [] := by intro e; subst e; have := rowsVal_len hv.2; simp only [List.length_nil, List.length_cons] at hl this; omega
1230
+ refine ⟨vs', hv.2, ?_, hl⟩
1231
+ rw [evalE, ← ht, tupk_cons_ne hne]; simp [pickP, hk]
1232
+ · simp at h
1233
+ · simp at h
1234
+ | .var _, _, h => by simp [parts] at h
1235
+ | .const _, _, h => by simp [parts] at h
1236
+ | .add _ _, _, h => by simp [parts] at h
1237
+ | .sub _ _, _, h => by simp [parts] at h
1238
+ | .mul _ _, _, h => by simp [parts] at h
1239
+ | .min _ _, _, h => by simp [parts] at h
1240
+ | .max _ _, _, h => by simp [parts] at h
1241
+ | .prod _ _, _, h => by simp [parts] at h
1242
+ | .ite _ _ _, _, h => by simp [parts] at h
1243
+ | .get _ _, _, h => by simp [parts] at h
1244
+ end
1245
+ end
1246
+
1247
+ /-! Facts: the rows a hypothesis gives under the path, when it has the truth value `pos`. -/
1248
+
1249
+ -- An equality of two linear forms: both differences ≤ 0 when it holds; when it fails, whichever strict side the path
1250
+ -- leaves open.
1251
+ def eqF (path : Path) (pos : Bool) (la lb : Row) : List Row :=
1252
+ if pos then [lsub la lb, lsub lb la]
1253
+ else
1254
+ let u1 := ladd (lsub la lb) (lconst 1)
1255
+ let u2 := ladd (lsub lb la) (lconst 1)
1256
+ let o1 := dec path u1 != some false
1257
+ let o2 := dec path u2 != some false
1258
+ if o1 && o2 then [] else if o1 then [u1] else if o2 then [u2] else [lconst 1]
1259
+
1260
+ -- An equality of two tuples: the facts of the conjunction of their parts' equalities (as `.and`'s).
1261
+ def factsSh (path : Path) (pos : Bool) : Sh → Sh → List Row
1262
+ | .leaf (some la), .leaf (some lb) => eqF path pos la lb
1263
+ | .node s1 s2, .node t1 t2 =>
1264
+ if pos then factsSh path pos s1 t1 ++ factsSh path pos s2 t2
1265
+ else
1266
+ let oa := eqSh path s1 t1 != some (!pos)
1267
+ let ob := eqSh path s2 t2 != some (!pos)
1268
+ if oa && ob then [] else if oa then factsSh path pos s1 t1 else if ob then factsSh path pos s2 t2 else [lconst 1]
1269
+ | _, _ => []
1270
+
1271
+ theorem eqF_sound {path : Path} {x : List Int} (hP : PathHolds path x) {la lb : Row} {pos : Bool}
1272
+ (h : decide (eval la x = eval lb x) = pos) : ∀ r ∈ eqF path pos la lb, eval r x ≤ 0 := by
1273
+ intro r hr
1274
+ simp only [eqF] at hr
1275
+ cases pos with
1276
+ | true =>
1277
+ simp only [decide_eq_true_eq] at h
1278
+ simp only [ite_true, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1279
+ rcases hr with hr | hr <;> subst hr <;> rw [eval_lsub] <;> omega
1280
+ | false =>
1281
+ simp only [decide_eq_false_iff_not] at h
1282
+ simp only [Bool.false_eq_true, ite_false] at hr
1283
+ have d1 := fun (hv : dec path (ladd (lsub la lb) (lconst 1)) = some false) => dec_sound hP hv
1284
+ have d2 := fun (hv : dec path (ladd (lsub lb la) (lconst 1)) = some false) => dec_sound hP hv
1285
+ simp only [eval_ladd, eval_lsub, eval_lconst, decide_eq_false_iff_not] at d1 d2
1286
+ split at hr
1287
+ · simp at hr
1288
+ · split at hr
1289
+ · have := d2 (by simp_all)
1290
+ simp only [List.mem_singleton] at hr; subst hr
1291
+ rw [eval_ladd, eval_lsub, eval_lconst]; omega
1292
+ · split at hr
1293
+ · have := d1 (by simp_all)
1294
+ simp only [List.mem_singleton] at hr; subst hr
1295
+ rw [eval_ladd, eval_lsub, eval_lconst]; omega
1296
+ · have := d1 (by simp_all); have := d2 (by simp_all); omega
1297
+
1298
+ theorem factsSh_sound {path : Path} {x : List Int} (hP : PathHolds path x) :
1299
+ ∀ (s t : Sh) (n1 n2 : Nat) (pos : Bool), DenSh x s n1 → DenSh x t n2 → decide (n1 = n2) = pos →
1300
+ ∀ r ∈ factsSh path pos s t, eval r x ≤ 0
1301
+ | .leaf (some la), .leaf (some lb), n1, n2, pos, h1, h2, h, r, hr => by
1302
+ simp only [factsSh] at hr
1303
+ simp only [DenSh] at h1 h2
1304
+ exact eqF_sound hP (by rw [h1, h2, decide_cast_eq]; exact h) r hr
1305
+ | .node s1 s2, .node t1 t2, n1, n2, pos, h1, h2, h, r, hr => by
1306
+ simp only [DenSh] at h1 h2
1307
+ simp only [factsSh] at hr
1308
+ rw [decide_eq_cun] at h
1309
+ have ta := fun v (hv : eqSh path s1 t1 = some v) => eqSh_sound hP s1 t1 _ _ v h1.1 h2.1 hv
1310
+ have tb := fun v (hv : eqSh path s2 t2 = some v) => eqSh_sound hP s2 t2 _ _ v h1.2 h2.2 hv
1311
+ cases pos with
1312
+ | true =>
1313
+ simp only [Bool.and_eq_true] at h
1314
+ simp only [ite_true, List.mem_append] at hr
1315
+ rcases hr with hr | hr
1316
+ · exact factsSh_sound hP s1 t1 _ _ true h1.1 h2.1 h.1 r hr
1317
+ · exact factsSh_sound hP s2 t2 _ _ true h1.2 h2.2 h.2 r hr
1318
+ | false =>
1319
+ simp only [Bool.false_eq_true, ite_false, Bool.not_false] at hr
1320
+ simp only [Bool.and_eq_false_iff] at h
1321
+ split at hr
1322
+ · simp at hr
1323
+ · split at hr
1324
+ · have hb : eqSh path s2 t2 = some true := by simp_all
1325
+ have := tb true hb
1326
+ exact factsSh_sound hP s1 t1 _ _ false h1.1 h2.1 (by simp_all) r hr
1327
+ · split at hr
1328
+ · have ha : eqSh path s1 t1 = some true := by simp_all
1329
+ have := ta true ha
1330
+ exact factsSh_sound hP s2 t2 _ _ false h1.2 h2.2 (by simp_all) r hr
1331
+ · have ha : eqSh path s1 t1 = some true := by simp_all
1332
+ have hb : eqSh path s2 t2 = some true := by simp_all
1333
+ have := ta true ha; have := tb true hb
1334
+ simp_all
1335
+ | .leaf none, .leaf _, _, _, _, _, _, _, r, hr => by simp [factsSh] at hr
1336
+ | .leaf none, .node _ _, _, _, _, _, _, _, r, hr => by simp [factsSh] at hr
1337
+ | .leaf (some _), .leaf none, _, _, _, _, _, _, r, hr => by simp [factsSh] at hr
1338
+ | .leaf (some _), .node _ _, _, _, _, _, _, _, r, hr => by simp [factsSh] at hr
1339
+ | .node _ _, .leaf _, _, _, _, _, _, _, r, hr => by simp [factsSh] at hr
1340
+
1341
+ def facts (tab : List VDef) (path : Path) : Bool → C → List Row
1342
+ | pos, .not a => facts tab path (!pos) a
1343
+ | pos, .and a b =>
1344
+ if pos then facts tab path pos a ++ facts tab path pos b
1345
+ else
1346
+ let oa := truth tab path a != some (!pos)
1347
+ let ob := truth tab path b != some (!pos)
1348
+ if oa && ob then [] else if oa then facts tab path pos a else if ob then facts tab path pos b else [lconst 1]
1349
+ | pos, .or a b =>
1350
+ if !pos then facts tab path pos a ++ facts tab path pos b
1351
+ else
1352
+ let oa := truth tab path a != some (!pos)
1353
+ let ob := truth tab path b != some (!pos)
1354
+ if oa && ob then [] else if oa then facts tab path pos a else if ob then facts tab path pos b else [lconst 1]
1355
+ | pos, .le a b => match linear tab path a, linear tab path b with
1356
+ | some la, some lb =>
1357
+ if pos then [lsub la lb]
1358
+ else let u := ladd (lsub lb la) (lconst 1); if dec path u == some false then [lconst 1] else [u]
1359
+ | _, _ => []
1360
+ | pos, .lt a b => match linear tab path a, linear tab path b with
1361
+ | some la, some lb =>
1362
+ if pos then [ladd (lsub la lb) (lconst 1)]
1363
+ else let u := lsub lb la; if dec path u == some false then [lconst 1] else [u]
1364
+ | _, _ => []
1365
+ | pos, .eq a b => if isTup a && isTup b then factsSh path pos (shapeT tab path a) (shapeT tab path b)
1366
+ else match linear tab path a, linear tab path b with
1367
+ | some la, some lb => eqF path pos la lb
1368
+ | _, _ => []
1369
+
1370
+ section
1371
+ variable {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {path : Path}
1372
+
1373
+ theorem facts_sound (hM : Model tab env maps x) (hP : PathHolds path x) :
1374
+ (c : C) → (pos : Bool) → evalC env maps c = pos → ∀ r ∈ facts tab path pos c, eval r x ≤ 0
1375
+ | .not a, pos, h, r, hr => by
1376
+ simp only [facts] at hr
1377
+ exact facts_sound hM hP a (!pos) (by simp [evalC] at h; simp [← h]) r hr
1378
+ | .and a b, pos, h, r, hr => by
1379
+ simp only [facts] at hr
1380
+ cases pos with
1381
+ | true =>
1382
+ simp only [evalC, Bool.and_eq_true] at h
1383
+ simp only [ite_true, List.mem_append] at hr
1384
+ rcases hr with hr | hr
1385
+ · exact facts_sound hM hP a true h.1 r hr
1386
+ · exact facts_sound hM hP b true h.2 r hr
1387
+ | false =>
1388
+ simp only [Bool.false_eq_true, ite_false, Bool.not_false] at hr
1389
+ have ta := fun v (hv : truth tab path a = some v) => truth_sound hM hP a v hv
1390
+ have tb := fun v (hv : truth tab path b = some v) => truth_sound hM hP b v hv
1391
+ simp only [evalC, Bool.and_eq_false_iff] at h
1392
+ split at hr
1393
+ · simp at hr
1394
+ · split at hr
1395
+ · rename_i _ hoa
1396
+ have hb : truth tab path b = some true := by simp_all
1397
+ have := tb true hb
1398
+ exact facts_sound hM hP a false (by simp_all) r hr
1399
+ · split at hr
1400
+ · rename_i _ _ hob
1401
+ have ha : truth tab path a = some true := by simp_all
1402
+ have := ta true ha
1403
+ exact facts_sound hM hP b false (by simp_all) r hr
1404
+ · have ha : truth tab path a = some true := by simp_all
1405
+ have hb : truth tab path b = some true := by simp_all
1406
+ have := ta true ha; have := tb true hb
1407
+ simp_all
1408
+ | .or a b, pos, h, r, hr => by
1409
+ simp only [facts] at hr
1410
+ cases pos with
1411
+ | false =>
1412
+ simp only [evalC, Bool.or_eq_false_iff] at h
1413
+ simp only [Bool.not_false, ite_true, List.mem_append] at hr
1414
+ rcases hr with hr | hr
1415
+ · exact facts_sound hM hP a false h.1 r hr
1416
+ · exact facts_sound hM hP b false h.2 r hr
1417
+ | true =>
1418
+ simp only [Bool.not_true, Bool.false_eq_true, ite_false] at hr
1419
+ have ta := fun v (hv : truth tab path a = some v) => truth_sound hM hP a v hv
1420
+ have tb := fun v (hv : truth tab path b = some v) => truth_sound hM hP b v hv
1421
+ simp only [evalC, Bool.or_eq_true] at h
1422
+ split at hr
1423
+ · simp at hr
1424
+ · split at hr
1425
+ · have hb : truth tab path b = some false := by simp_all
1426
+ have := tb false hb
1427
+ exact facts_sound hM hP a true (by simp_all) r hr
1428
+ · split at hr
1429
+ · have ha : truth tab path a = some false := by simp_all
1430
+ have := ta false ha
1431
+ exact facts_sound hM hP b true (by simp_all) r hr
1432
+ · have ha : truth tab path a = some false := by simp_all
1433
+ have hb : truth tab path b = some false := by simp_all
1434
+ have := ta false ha; have := tb false hb
1435
+ simp_all
1436
+ | .le a b, pos, h, r, hr => by
1437
+ simp only [facts] at hr
1438
+ split at hr
1439
+ · rename_i la lb ha hb
1440
+ have ea := linear_sound hM hP a la ha
1441
+ have eb := linear_sound hM hP b lb hb
1442
+ cases pos with
1443
+ | true =>
1444
+ simp only [evalC, decide_eq_true_eq] at h
1445
+ simp only [ite_true, List.mem_singleton] at hr; subst hr
1446
+ rw [eval_lsub]; omega
1447
+ | false =>
1448
+ simp only [evalC, decide_eq_false_iff_not] at h
1449
+ simp only [Bool.false_eq_true, ite_false] at hr
1450
+ split at hr
1451
+ · rename_i hd
1452
+ have := dec_sound hP (by simpa using hd)
1453
+ simp only [eval_ladd, eval_lsub, eval_lconst, decide_eq_false_iff_not] at this; omega
1454
+ · simp only [List.mem_singleton] at hr; subst hr
1455
+ rw [eval_ladd, eval_lsub, eval_lconst]; omega
1456
+ · simp at hr
1457
+ | .lt a b, pos, h, r, hr => by
1458
+ simp only [facts] at hr
1459
+ split at hr
1460
+ · rename_i la lb ha hb
1461
+ have ea := linear_sound hM hP a la ha
1462
+ have eb := linear_sound hM hP b lb hb
1463
+ cases pos with
1464
+ | true =>
1465
+ simp only [evalC, decide_eq_true_eq] at h
1466
+ simp only [ite_true, List.mem_singleton] at hr; subst hr
1467
+ rw [eval_ladd, eval_lsub, eval_lconst]; omega
1468
+ | false =>
1469
+ simp only [evalC, decide_eq_false_iff_not] at h
1470
+ simp only [Bool.false_eq_true, ite_false] at hr
1471
+ split at hr
1472
+ · rename_i hd
1473
+ have := dec_sound hP (by simpa using hd)
1474
+ simp only [eval_lsub, decide_eq_false_iff_not] at this; omega
1475
+ · simp only [List.mem_singleton] at hr; subst hr
1476
+ rw [eval_lsub]; omega
1477
+ · simp at hr
1478
+ | .eq a b, pos, h, r, hr => by
1479
+ simp only [facts] at hr
1480
+ split at hr
1481
+ · exact factsSh_sound hP _ _ _ _ pos (shapeT_sound hM hP a) (shapeT_sound hM hP b) (by simpa [evalC] using h) r hr
1482
+ · split at hr
1483
+ · rename_i la lb ha hb
1484
+ refine eqF_sound hP ?_ r hr
1485
+ rw [linear_sound hM hP a la ha, linear_sound hM hP b lb hb, decide_cast_eq]
1486
+ simpa only [evalC] using h
1487
+ · simp at hr
1488
+ end
1489
+
1490
+ /-! Leaves and trees. -/
1491
+
1492
+ def atomRow (a : Row × Bool) : Row := if a.2 then a.1 else flip a.1
1493
+ def natRows : Nat → List Row
1494
+ | 0 => []
1495
+ | n + 1 => lscale (-1) (lunit n) :: natRows n
1496
+ -- Two lists of keys of one length that the path proves equal, key by key.
1497
+ def keysEq (path : Path) : List Row → List Row → Bool
1498
+ | [], [] => true
1499
+ | k :: ks, k' :: ks' => dec path (lsub k k') == some true && dec path (lsub k' k) == some true && keysEq path ks ks'
1500
+ | _, _ => false
1501
+ -- Two cells of one map whose keys the path proves equal are equal (the pair is the leaf's claim; it is checked here).
1502
+ def cellEq (tab : List VDef) (path : Path) (p : Nat × Nat) : List Row :=
1503
+ match tab[p.1]?, tab[p.2]? with
1504
+ | some (.cell m k), some (.cell m' k') =>
1505
+ if m == m' && dec path (lsub k k') == some true && dec path (lsub k' k) == some true
1506
+ then [lsub (lunit p.1) (lunit p.2), lsub (lunit p.2) (lunit p.1)] else []
1507
+ | some (.cellT m ks), some (.cellT m' ks') =>
1508
+ if m == m' && keysEq path ks ks' then [lsub (lunit p.1) (lunit p.2), lsub (lunit p.2) (lunit p.1)] else []
1509
+ | _, _ => []
1510
+ -- A term column's rows: its bounds, whichever way its choice goes (min ≤ each side, max ≥ each side, a ∸ b ≥ a − b and
1511
+ -- ≤ a, an if ≤ the sum of its branches when both are constants), and its equality to the term's form under the path
1512
+ -- (the branch taken, once the path decides it).
1513
+ def constIn (r : Row) : Bool := r.coefs.all (fun p => p.2 == 0)
1514
+ def termBounds (j : Nat) (la lb : Row) : E → List Row
1515
+ | .min _ _ => [lsub (lunit j) la, lsub (lunit j) lb]
1516
+ | .max _ _ => [lsub la (lunit j), lsub lb (lunit j)]
1517
+ | .sub _ _ => [lsub (lsub la lb) (lunit j), lsub (lunit j) la]
1518
+ | .ite _ _ _ => [lsub (lunit j) (ladd la lb)]
1519
+ | _ => []
1520
+ def termRowsAt (tab : List VDef) (path : Path) (j : Nat) : List Row :=
1521
+ match tab[j]? with
1522
+ | some (.term e) =>
1523
+ (match e with
1524
+ | .min a b | .max a b | .sub a b => match linear tab path a, linear tab path b with
1525
+ | some la, some lb => termBounds j la lb e
1526
+ | _, _ => []
1527
+ | .ite _ a b => match linear tab path a, linear tab path b with
1528
+ | some la, some lb => if constIn la && constIn lb then termBounds j la lb e else []
1529
+ | _, _ => []
1530
+ | _ => [])
1531
+ ++ (match linear tab path e with
1532
+ | some r => [lsub (lunit j) r, lsub r (lunit j)]
1533
+ | none => [])
1534
+ | _ => []
1535
+ def termRows (tab : List VDef) (path : Path) : List Row := (List.range tab.length).flatMap (termRowsAt tab path)
1536
+
1537
+ def leafRows (tab : List VDef) (path : Path) (hyps : List C) (eqs : List (Nat × Nat)) : List Row :=
1538
+ path.map atomRow ++ hyps.flatMap (facts tab path true) ++ natRows tab.length ++ eqs.flatMap (cellEq tab path) ++ termRows tab path
1539
+
1540
+ -- Where a leaf's row is looked for first: the path's i-th atom (the latest decision first), a hypothesis's facts, column i's naturality, the i-th
1541
+ -- pair of equal cells, or term column j's rows. A hint only saves work: a row not found there is looked for among all
1542
+ -- of the leaf's rows, as one without a hint is.
1543
+ inductive Src where
1544
+ | atom (i : Nat)
1545
+ | fact (h : Nat)
1546
+ | nat (i : Nat)
1547
+ | eq (i : Nat)
1548
+ | term (j : Nat)
1549
+ | any
1550
+
1551
+ def srcRows (tab : List VDef) (path : Path) (hyps : List C) (eqs : List (Nat × Nat)) : Src → List Row
1552
+ | .atom i => match path[i]? with | some a => [atomRow a] | none => []
1553
+ | .fact h => match hyps[h]? with | some c => facts tab path true c | none => []
1554
+ | .nat i => if i < tab.length then [lscale (-1) (lunit i)] else []
1555
+ | .eq i => match eqs[i]? with | some p => cellEq tab path p | none => []
1556
+ | .term j => if j < tab.length then termRowsAt tab path j else []
1557
+ | .any => []
1558
+
1559
+ -- A leaf may add products of two of its rows (p ≤ 0 and q ≤ 0 give −p·q ≤ 0); a pair may name an earlier product.
1560
+ -- `leafAt` is a leaf whose rows come with hints; `leafSq` one that also adds the square of each of its forms t
1561
+ -- (−t·t ≤ 0, whatever t's sign) after its rows, before the products, which may name them.
1562
+ inductive Tree where
1563
+ | split (l : Row) (yes no : Tree)
1564
+ | leaf (rows : List Row) (steps : List Step) (eqs : List (Nat × Nat)) (prods : List (Nat × Nat))
1565
+ | leafAt (rows : List (Row × Src)) (steps : List Step) (eqs : List (Nat × Nat)) (prods : List (Nat × Nat))
1566
+ | leafSq (rows : List (Row × Src)) (sqs : List Row) (steps : List Step) (eqs : List (Nat × Nat)) (prods : List (Nat × Nat))
1567
+
1568
+ def squares (tab : List VDef) : List Row → Option (List Row)
1569
+ | [] => some []
1570
+ | t :: ts => match rowMul tab t t, squares tab ts with
1571
+ | some r, some rs => some (lscale (-1) r :: rs)
1572
+ | _, _ => none
1573
+
1574
+ def extend (tab : List VDef) : List Row → List (Nat × Nat) → Option (List Row)
1575
+ | acc, [] => some acc
1576
+ | acc, (i, j) :: ps => match acc[i]?, acc[j]? with
1577
+ | some p, some q => match rowMul tab p q with
1578
+ | some r => extend tab (acc ++ [lscale (-1) r]) ps
1579
+ | none => none
1580
+ | _, _ => none
1581
+
1582
+ def leafOk (tab : List VDef) (path : Path) (hyps : List C) (rows : List Row) (steps : List Step) (eqs : List (Nat × Nat))
1583
+ (prods : List (Nat × Nat)) : Bool :=
1584
+ let comp := leafRows tab path hyps eqs
1585
+ rows.all (fun r => comp.any (same r)) && match extend tab rows prods with
1586
+ | some rows' => Cert.check rows' steps
1587
+ | none => false
1588
+
1589
+ -- Each row where its hint says, or else among the leaf's rows.
1590
+ def leafAtOk (tab : List VDef) (path : Path) (hyps : List C) (rows : List (Row × Src)) (steps : List Step) (eqs : List (Nat × Nat))
1591
+ (prods : List (Nat × Nat)) : Bool :=
1592
+ rows.all (fun r => (srcRows tab path hyps eqs r.2).any (same r.1) || (leafRows tab path hyps eqs).any (same r.1))
1593
+ && match extend tab (rows.map (·.1)) prods with
1594
+ | some rows' => Cert.check rows' steps
1595
+ | none => false
1596
+
1597
+ -- Each row where its hint says, or else among the leaf's rows; then the squares, then the products.
1598
+ def leafSqOk (tab : List VDef) (path : Path) (hyps : List C) (rows : List (Row × Src)) (sqs : List Row) (steps : List Step)
1599
+ (eqs : List (Nat × Nat)) (prods : List (Nat × Nat)) : Bool :=
1600
+ rows.all (fun r => (srcRows tab path hyps eqs r.2).any (same r.1) || (leafRows tab path hyps eqs).any (same r.1))
1601
+ && match squares tab sqs with
1602
+ | some sr => match extend tab (rows.map (·.1) ++ sr) prods with
1603
+ | some rows' => Cert.check rows' steps
1604
+ | none => false
1605
+ | none => false
1606
+
1607
+ def checkT (tab : List VDef) (hyps : List C) : Path → Tree → Bool
1608
+ | path, .split l y n => checkT tab hyps ((l, true) :: path) y && checkT tab hyps ((l, false) :: path) n
1609
+ | path, .leaf rows steps eqs prods => leafOk tab path hyps rows steps eqs prods
1610
+ | path, .leafAt rows steps eqs prods => leafAtOk tab path hyps rows steps eqs prods
1611
+ | path, .leafSq rows sqs steps eqs prods => leafSqOk tab path hyps rows sqs steps eqs prods
1612
+
1613
+ structure Goal where
1614
+ tab : List VDef
1615
+ hyps : List C
1616
+
1617
+ -- Each cell's key reads only earlier columns, so the table has a valuation.
1618
+ def wfTab : Nat → List VDef → Bool
1619
+ | _, [] => true
1620
+ | n, .var _ :: ds => wfTab (n + 1) ds
1621
+ | n, .cell _ k :: ds => k.coefs.all (·.1 < n) && wfTab (n + 1) ds
1622
+ | n, .cellT _ ks :: ds => ks.all (fun k => k.coefs.all (·.1 < n)) && wfTab (n + 1) ds
1623
+ | n, .mono fs :: ds => fs.all (· < n) && wfTab (n + 1) ds
1624
+ | n, .term _ :: ds => wfTab (n + 1) ds
1625
+
1626
+ def checkGoal (g : Goal) (t : Tree) : Bool := wfTab 0 g.tab && checkT g.tab g.hyps [] t
1627
+
1628
+ section
1629
+ variable {tab : List VDef} {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int}
1630
+
1631
+ theorem natRows_sound (hM : Model tab env maps x) : ∀ n, ∀ r ∈ natRows n, eval r x ≤ 0
1632
+ | 0, r, hr => by simp [natRows] at hr
1633
+ | n + 1, r, hr => by
1634
+ simp only [natRows, List.mem_cons] at hr
1635
+ rcases hr with hr | hr
1636
+ · subst hr; rw [eval_lscale, eval_lunit]; have := hM.2 n; omega
1637
+ · exact natRows_sound hM n r hr
1638
+
1639
+ theorem termRows_sound {path : Path} (hM : Model tab env maps x) (hP : PathHolds path x) :
1640
+ ∀ r ∈ termRows tab path, eval r x ≤ 0 := by
1641
+ intro r hr
1642
+ simp only [termRows, List.mem_flatMap] at hr
1643
+ obtain ⟨j, _, hr⟩ := hr
1644
+ simp only [termRowsAt] at hr
1645
+ split at hr
1646
+ · rename_i e hj
1647
+ have hx : x.getD j 0 = (evalE env maps e : Int) := by rw [hM.1 j _ hj]; rfl
1648
+ simp only [List.mem_append] at hr
1649
+ rcases hr with hr | hr
1650
+ · cases e with
1651
+ | min a b =>
1652
+ simp only at hr
1653
+ split at hr
1654
+ · rename_i la lb ha hb
1655
+ have ea := linear_sound hM hP a la ha
1656
+ have eb := linear_sound hM hP b lb hb
1657
+ simp only [termBounds, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1658
+ simp only [evalE, Nat.min_def] at hx
1659
+ split at hx <;> rcases hr with rfl | rfl <;> rw [eval_lsub, eval_lunit] <;> omega
1660
+ · simp at hr
1661
+ | max a b =>
1662
+ simp only at hr
1663
+ split at hr
1664
+ · rename_i la lb ha hb
1665
+ have ea := linear_sound hM hP a la ha
1666
+ have eb := linear_sound hM hP b lb hb
1667
+ simp only [termBounds, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1668
+ simp only [evalE, Nat.max_def] at hx
1669
+ split at hx <;> rcases hr with rfl | rfl <;> rw [eval_lsub, eval_lunit] <;> omega
1670
+ · simp at hr
1671
+ | sub a b =>
1672
+ simp only at hr
1673
+ split at hr
1674
+ · rename_i la lb ha hb
1675
+ have ea := linear_sound hM hP a la ha
1676
+ have eb := linear_sound hM hP b lb hb
1677
+ simp only [termBounds, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1678
+ simp only [evalE] at hx
1679
+ rcases hr with rfl | rfl
1680
+ · rw [eval_lsub, eval_lsub, eval_lunit]; omega
1681
+ · rw [eval_lsub, eval_lunit]; omega
1682
+ · simp at hr
1683
+ | ite c a b =>
1684
+ simp only at hr
1685
+ split at hr
1686
+ · rename_i la lb ha hb
1687
+ split at hr
1688
+ · have ea := linear_sound hM hP a la ha
1689
+ have eb := linear_sound hM hP b lb hb
1690
+ simp only [termBounds, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1691
+ simp only [evalE] at hx
1692
+ subst hr
1693
+ rw [eval_lsub, eval_ladd, eval_lunit]
1694
+ split at hx <;> omega
1695
+ · simp at hr
1696
+ · simp at hr
1697
+ | _ => simp at hr
1698
+ · split at hr
1699
+ · rename_i r' hr'
1700
+ have := linear_sound hM hP e r' hr'
1701
+ simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1702
+ rcases hr with rfl | rfl <;> rw [eval_lsub, eval_lunit] <;> omega
1703
+ · simp at hr
1704
+ · simp at hr
1705
+
1706
+ theorem keysEq_map {path : Path} (hP : PathHolds path x) : ∀ {ks ks' : List Row}, keysEq path ks ks' = true →
1707
+ ks.map (fun r => (eval r x).toNat) = ks'.map (fun r => (eval r x).toNat)
1708
+ | [], [], _ => rfl
1709
+ | k :: ks, k' :: ks', h => by
1710
+ simp only [keysEq, Bool.and_eq_true, beq_iff_eq] at h
1711
+ obtain ⟨⟨h1, h2⟩, h3⟩ := h
1712
+ have t1 := dec_sound hP h1; have t2 := dec_sound hP h2
1713
+ simp only [eval_lsub, decide_eq_true_eq] at t1 t2
1714
+ have hkk : eval k x = eval k' x := by omega
1715
+ simp only [List.map_cons, hkk, keysEq_map hP h3]
1716
+ | [], _ :: _, h => by simp [keysEq] at h
1717
+ | _ :: _, [], h => by simp [keysEq] at h
1718
+
1719
+ theorem cellEq_sound {path : Path} (hM : Model tab env maps x) (hP : PathHolds path x) (p : Nat × Nat) :
1720
+ ∀ r ∈ cellEq tab path p, eval r x ≤ 0 := by
1721
+ intro r hr
1722
+ simp only [cellEq] at hr
1723
+ split at hr
1724
+ · rename_i m k m' k' h1 h2
1725
+ split at hr
1726
+ · rename_i hc
1727
+ simp only [Bool.and_eq_true, beq_iff_eq] at hc
1728
+ obtain ⟨⟨rfl, hk1⟩, hk2⟩ := hc
1729
+ have t1 := dec_sound hP hk1; have t2 := dec_sound hP hk2
1730
+ simp only [eval_lsub, decide_eq_true_eq] at t1 t2
1731
+ have e1 := hM.1 p.1 _ h1; have e2 := hM.1 p.2 _ h2
1732
+ simp only [valD] at e1 e2
1733
+ have hkk : eval k x = eval k' x := by omega
1734
+ simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1735
+ rcases hr with rfl | rfl <;> rw [eval_lsub, eval_lunit, eval_lunit, e1, e2, hkk] <;> omega
1736
+ · simp at hr
1737
+ · rename_i m ks m' ks' h1 h2
1738
+ split at hr
1739
+ · rename_i hc
1740
+ simp only [Bool.and_eq_true, beq_iff_eq] at hc
1741
+ obtain ⟨rfl, hk⟩ := hc
1742
+ have e1 := hM.1 p.1 _ h1; have e2 := hM.1 p.2 _ h2
1743
+ simp only [valD] at e1 e2
1744
+ rw [keysEq_map hP hk] at e1
1745
+ simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hr
1746
+ rcases hr with rfl | rfl <;> rw [eval_lsub, eval_lunit, eval_lunit, e1, e2] <;> omega
1747
+ · simp at hr
1748
+ · simp at hr
1749
+
1750
+ theorem leafRows_sound {path : Path} (hM : Model tab env maps x) (hP : PathHolds path x) {hyps : List C}
1751
+ (hH : ∀ c ∈ hyps, evalC env maps c = true) (eqs : List (Nat × Nat)) : ∀ r ∈ leafRows tab path hyps eqs, eval r x ≤ 0 := by
1752
+ intro r hr
1753
+ simp only [leafRows, List.mem_append, List.mem_map, List.mem_flatMap] at hr
1754
+ rcases hr with (((⟨a, ha, rfl⟩ | ⟨c, hc, hr⟩) | hr) | ⟨p, _, hr⟩) | hr
1755
+ · have := hP a ha
1756
+ simp only [atomRow]
1757
+ cases h2 : a.2 with
1758
+ | true => rw [h2] at this; simpa using this
1759
+ | false => rw [h2] at this; simp only [decide_eq_false_iff_not] at this; simp only [ite_false, Bool.false_eq_true, eval_flip]; omega
1760
+ · exact facts_sound hM hP c true (hH c hc) r hr
1761
+ · exact natRows_sound hM _ r hr
1762
+ · exact cellEq_sound hM hP p r hr
1763
+ · exact termRows_sound hM hP r hr
1764
+
1765
+ -- A hint names some of the leaf's rows.
1766
+ theorem natRows_mem : ∀ {n i : Nat}, i < n → lscale (-1) (lunit i) ∈ natRows n
1767
+ | 0, _, h => absurd h (Nat.not_lt_zero _)
1768
+ | n + 1, i, h => by
1769
+ simp only [natRows, List.mem_cons]
1770
+ rcases Nat.lt_succ_iff_lt_or_eq.mp h with h | rfl
1771
+ · exact Or.inr (natRows_mem h)
1772
+ · exact Or.inl rfl
1773
+
1774
+ theorem srcRows_sub {path : Path} {hyps : List C} {eqs : List (Nat × Nat)} {r : Row} :
1775
+ ∀ s, r ∈ srcRows tab path hyps eqs s → r ∈ leafRows tab path hyps eqs
1776
+ | .atom i, hr => by
1777
+ simp only [srcRows] at hr
1778
+ split at hr
1779
+ · rename_i a ha
1780
+ simp only [List.mem_singleton] at hr; subst hr
1781
+ simp only [leafRows, List.mem_append, List.mem_map]
1782
+ exact Or.inl (Or.inl (Or.inl (Or.inl ⟨a, List.mem_of_getElem? ha, rfl⟩)))
1783
+ · simp at hr
1784
+ | .fact h, hr => by
1785
+ simp only [srcRows] at hr
1786
+ split at hr
1787
+ · rename_i c hc
1788
+ simp only [leafRows, List.mem_append, List.mem_flatMap]
1789
+ exact Or.inl (Or.inl (Or.inl (Or.inr ⟨c, List.mem_of_getElem? hc, hr⟩)))
1790
+ · simp at hr
1791
+ | .nat i, hr => by
1792
+ simp only [srcRows] at hr
1793
+ split at hr
1794
+ · rename_i hi
1795
+ simp only [List.mem_singleton] at hr; subst hr
1796
+ simp only [leafRows, List.mem_append]
1797
+ exact Or.inl (Or.inl (Or.inr (natRows_mem hi)))
1798
+ · simp at hr
1799
+ | .eq i, hr => by
1800
+ simp only [srcRows] at hr
1801
+ split at hr
1802
+ · rename_i p hp
1803
+ simp only [leafRows, List.mem_append, List.mem_flatMap]
1804
+ exact Or.inl (Or.inr ⟨p, List.mem_of_getElem? hp, hr⟩)
1805
+ · simp at hr
1806
+ | .term j, hr => by
1807
+ simp only [srcRows] at hr
1808
+ split at hr
1809
+ · rename_i hj
1810
+ simp only [leafRows, termRows, List.mem_append, List.mem_flatMap, List.mem_range]
1811
+ exact Or.inr ⟨j, hj, hr⟩
1812
+ · simp at hr
1813
+ | .any, hr => by simp [srcRows] at hr
1814
+
1815
+ theorem extend_sound (hM : Model tab env maps x) :
1816
+ ∀ ps acc rows', extend tab acc ps = some rows' → (∀ r ∈ acc, eval r x ≤ 0) → ∀ r ∈ rows', eval r x ≤ 0
1817
+ | [], acc, rows', h, ha => by simp only [extend, Option.some.injEq] at h; subst h; exact ha
1818
+ | (i, j) :: ps, acc, rows', h, ha => by
1819
+ simp only [extend] at h
1820
+ split at h
1821
+ · rename_i p q hp hq
1822
+ split at h
1823
+ · rename_i r hr
1824
+ refine extend_sound hM ps _ rows' h (fun r' hr' => ?_)
1825
+ rcases List.mem_append.mp hr' with hr' | hr'
1826
+ · exact ha r' hr'
1827
+ · simp only [List.mem_singleton] at hr'; subst hr'
1828
+ have e1 := ha p (List.mem_of_getElem? hp)
1829
+ have e2 := ha q (List.mem_of_getElem? hq)
1830
+ rw [eval_lscale, rowMul_sound hM hr]
1831
+ have : 0 ≤ (-eval p x) * (-eval q x) := Int.mul_nonneg (by omega) (by omega)
1832
+ rw [Int.neg_mul_neg] at this; omega
1833
+ · simp at h
1834
+ · simp at h
1835
+
1836
+ theorem squares_sound (hM : Model tab env maps x) : ∀ ts rs, squares tab ts = some rs → ∀ r ∈ rs, eval r x ≤ 0
1837
+ | [], rs, h => by simp only [squares, Option.some.injEq] at h; subst h; simp
1838
+ | t :: ts, rs, h => by
1839
+ simp only [squares] at h
1840
+ split at h
1841
+ · rename_i r rs' hr hrs
1842
+ simp only [Option.some.injEq] at h; subst h
1843
+ intro r' hr'
1844
+ rcases List.mem_cons.mp hr' with hr' | hr'
1845
+ · subst hr'
1846
+ rw [eval_lscale, rowMul_sound hM hr]
1847
+ have : 0 ≤ eval t x * eval t x := by
1848
+ rcases Int.le_total 0 (eval t x) with h0 | h0
1849
+ · exact Int.mul_nonneg h0 h0
1850
+ · have := Int.mul_nonneg (show 0 ≤ -eval t x by omega) (show 0 ≤ -eval t x by omega)
1851
+ rwa [Int.neg_mul_neg] at this
1852
+ omega
1853
+ · exact squares_sound hM ts rs' hrs r' hr'
1854
+ · simp at h
1855
+
1856
+ theorem checkT_sound (hM : Model tab env maps x) {hyps : List C} (hH : ∀ c ∈ hyps, evalC env maps c = true) :
1857
+ ∀ (t : Tree) (path : Path), PathHolds path x → checkT tab hyps path t = true → False
1858
+ | .split l y n, path, hP, h => by
1859
+ simp only [checkT, Bool.and_eq_true] at h
1860
+ by_cases hl : eval l x ≤ 0
1861
+ · refine checkT_sound hM hH y _ ?_ h.1
1862
+ intro a ha
1863
+ rcases List.mem_cons.mp ha with ha | ha
1864
+ · subst ha; simpa using hl
1865
+ · exact hP a ha
1866
+ · refine checkT_sound hM hH n _ ?_ h.2
1867
+ intro a ha
1868
+ rcases List.mem_cons.mp ha with ha | ha
1869
+ · subst ha; simpa using hl
1870
+ · exact hP a ha
1871
+ | .leaf rows steps eqs prods, path, hP, h => by
1872
+ simp only [checkT, leafOk, Bool.and_eq_true, List.all_eq_true, List.any_eq_true] at h
1873
+ obtain ⟨h1, h2⟩ := h
1874
+ split at h2
1875
+ · rename_i rows' he
1876
+ refine Cert.sound rows' steps h2 x (extend_sound hM prods rows rows' he ?_)
1877
+ intro r hr
1878
+ obtain ⟨c, hc, hs⟩ := h1 r hr
1879
+ rw [same_eval hs]
1880
+ exact leafRows_sound hM hP hH eqs c hc
1881
+ · simp at h2
1882
+ | .leafAt rows steps eqs prods, path, hP, h => by
1883
+ simp only [checkT, leafAtOk, Bool.and_eq_true, List.all_eq_true, Bool.or_eq_true, List.any_eq_true] at h
1884
+ obtain ⟨h1, h2⟩ := h
1885
+ split at h2
1886
+ · rename_i rows' he
1887
+ refine Cert.sound rows' steps h2 x (extend_sound hM prods _ rows' he ?_)
1888
+ intro r hr
1889
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hr
1890
+ rcases h1 p hp with ⟨c, hc, hs⟩ | ⟨c, hc, hs⟩
1891
+ · rw [same_eval hs]; exact leafRows_sound hM hP hH eqs c (srcRows_sub p.2 hc)
1892
+ · rw [same_eval hs]; exact leafRows_sound hM hP hH eqs c hc
1893
+ · simp at h2
1894
+ | .leafSq rows sqs steps eqs prods, path, hP, h => by
1895
+ simp only [checkT, leafSqOk, Bool.and_eq_true, List.all_eq_true, Bool.or_eq_true, List.any_eq_true] at h
1896
+ obtain ⟨h1, h2⟩ := h
1897
+ split at h2
1898
+ · rename_i sr hsr
1899
+ split at h2
1900
+ · rename_i rows' he
1901
+ refine Cert.sound rows' steps h2 x (extend_sound hM prods _ rows' he ?_)
1902
+ intro r hr
1903
+ rcases List.mem_append.mp hr with hr | hr
1904
+ · obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hr
1905
+ rcases h1 p hp with ⟨c, hc, hs⟩ | ⟨c, hc, hs⟩
1906
+ · rw [same_eval hs]; exact leafRows_sound hM hP hH eqs c (srcRows_sub p.2 hc)
1907
+ · rw [same_eval hs]; exact leafRows_sound hM hP hH eqs c hc
1908
+ · exact squares_sound hM sqs sr hsr r hr
1909
+ · simp at h2
1910
+ · simp at h2
1911
+ end
1912
+
1913
+ /-! The table's valuation. -/
1914
+
1915
+ def build (env : Nat → Nat) (maps : Nat → Nat → Nat) : List VDef → List Int → List Int
1916
+ | [], acc => acc
1917
+ | d :: ds, acc => build env maps ds (acc ++ [valD env maps d acc])
1918
+
1919
+ theorem dot_append : ∀ (a : List (Nat × Int)) (l1 l2 : List Int), (∀ p ∈ a, p.1 < l1.length) → dot a (l1 ++ l2) = dot a l1
1920
+ | [], _, _, _ => by simp [dot]
1921
+ | p :: a, l1, l2, h => by
1922
+ simp only [dot]
1923
+ rw [dot_append a l1 l2 (fun q hq => h q (List.mem_cons_of_mem _ hq))]
1924
+ have hp := h p List.mem_cons_self
1925
+ rw [List.getD_eq_getElem?_getD, List.getD_eq_getElem?_getD, List.getElem?_append_left hp]
1926
+
1927
+ -- What a column may refer to: only earlier columns.
1928
+ def Refs (n : Nat) : VDef → Prop
1929
+ | .var _ => True
1930
+ | .cell _ k => ∀ p ∈ k.coefs, p.1 < n
1931
+ | .cellT _ ks => ∀ k ∈ ks, ∀ p ∈ k.coefs, p.1 < n
1932
+ | .mono fs => ∀ f ∈ fs, f < n
1933
+ | .term _ => True
1934
+
1935
+ theorem Refs.mono_le {n m : Nat} (h : n ≤ m) : ∀ {d : VDef}, Refs n d → Refs m d
1936
+ | .var _, _ => trivial
1937
+ | .cell _ _, hd => fun p hp => by have := hd p hp; omega
1938
+ | .cellT _ _, hd => fun k hk p hp => by have := hd k hk p hp; omega
1939
+ | .mono _, hd => fun f hf => by have := hd f hf; omega
1940
+ | .term _, _ => trivial
1941
+
1942
+ theorem prodV_append_left (acc more : List Int) : ∀ fs : List Nat, (∀ f ∈ fs, f < acc.length) → prodV (acc ++ more) fs = prodV acc fs
1943
+ | [], _ => rfl
1944
+ | f :: fs, h => by
1945
+ simp only [prodV]
1946
+ rw [prodV_append_left acc more fs (fun g hg => h g (List.mem_cons_of_mem _ hg))]
1947
+ have hf := h f List.mem_cons_self
1948
+ rw [List.getD_eq_getElem?_getD, List.getD_eq_getElem?_getD, List.getElem?_append_left hf]
1949
+
1950
+ theorem map_eval_append (acc more : List Int) : ∀ ks : List Row, (∀ k ∈ ks, ∀ p ∈ k.coefs, p.1 < acc.length) →
1951
+ ks.map (fun r => (eval r (acc ++ more)).toNat) = ks.map (fun r => (eval r acc).toNat)
1952
+ | [], _ => rfl
1953
+ | k :: ks, h => by
1954
+ simp only [List.map_cons]
1955
+ rw [map_eval_append acc more ks (fun k' hk' => h k' (List.mem_cons_of_mem _ hk'))]
1956
+ simp only [eval, dot_append k.coefs acc more (h k List.mem_cons_self)]
1957
+
1958
+ theorem valD_append (env : Nat → Nat) (maps : Nat → Nat → Nat) (d : VDef) (acc more : List Int)
1959
+ (h : Refs acc.length d) : valD env maps d (acc ++ more) = valD env maps d acc := by
1960
+ cases d with
1961
+ | var i => rfl
1962
+ | cell m k => simp only [Refs] at h; simp only [valD, eval, dot_append k.coefs acc more h]
1963
+ | cellT m ks => simp only [Refs] at h; simp only [valD, map_eval_append acc more ks h]
1964
+ | mono fs => simp only [valD, prodV_append_left acc more fs h]
1965
+ | term e => rfl
1966
+
1967
+ def Valid (env : Nat → Nat) (maps : Nat → Nat → Nat) (acc : List Int) (pre : List VDef) : Prop :=
1968
+ acc.length = pre.length ∧ (∀ (j : Nat) (d : VDef), pre[j]? = some d → Refs j d) ∧
1969
+ ∀ (j : Nat) (d : VDef), pre[j]? = some d → acc[j]? = some (valD env maps d acc)
1970
+
1971
+ theorem valid_snoc {env : Nat → Nat} {maps : Nat → Nat → Nat} {acc : List Int} {pre : List VDef} (d : VDef)
1972
+ (hv : Valid env maps acc pre) (hd : Refs pre.length d) :
1973
+ Valid env maps (acc ++ [valD env maps d acc]) (pre ++ [d]) := by
1974
+ obtain ⟨hl, hw, hx⟩ := hv
1975
+ refine ⟨by simp [hl], ?_, ?_⟩
1976
+ · intro j d' hj
1977
+ by_cases hlt : j < pre.length
1978
+ · rw [List.getElem?_append_left hlt] at hj; exact hw j d' hj
1979
+ · rw [List.getElem?_append_right (by omega)] at hj
1980
+ have hj0 : j - pre.length = 0 := by
1981
+ cases h : j - pre.length with
1982
+ | zero => rfl
1983
+ | succ n => rw [h] at hj; simp at hj
1984
+ rw [hj0] at hj; simp only [List.getElem?_cons_zero, Option.some.injEq] at hj; subst hj
1985
+ exact Refs.mono_le (by omega) hd
1986
+ · intro j d' hj
1987
+ by_cases hlt : j < pre.length
1988
+ · rw [List.getElem?_append_left hlt] at hj
1989
+ rw [List.getElem?_append_left (by omega), hx j d' hj]
1990
+ rw [valD_append env maps d' acc _ (Refs.mono_le (by omega) (hw j d' hj))]
1991
+ · rw [List.getElem?_append_right (by omega)] at hj
1992
+ have hj0 : j - pre.length = 0 := by
1993
+ cases h : j - pre.length with
1994
+ | zero => rfl
1995
+ | succ n => rw [h] at hj; simp at hj
1996
+ rw [hj0] at hj; simp only [List.getElem?_cons_zero, Option.some.injEq] at hj; subst hj
1997
+ rw [List.getElem?_append_right (by omega), show j - acc.length = 0 by omega]
1998
+ simp only [List.getElem?_cons_zero, Option.some.injEq]
1999
+ rw [valD_append env maps d acc _ (by rw [hl]; exact hd)]
2000
+
2001
+ theorem build_valid (env : Nat → Nat) (maps : Nat → Nat → Nat) : ∀ (ds : List VDef) (acc : List Int) (pre : List VDef),
2002
+ Valid env maps acc pre → wfTab pre.length ds = true → Valid env maps (build env maps ds acc) (pre ++ ds)
2003
+ | [], acc, pre, hv, _ => by simpa [build] using hv
2004
+ | d :: ds, acc, pre, hv, hw => by
2005
+ have hd : Refs pre.length d ∧ wfTab (pre.length + 1) ds = true := by
2006
+ cases d with
2007
+ | var i => exact ⟨trivial, by simpa [wfTab] using hw⟩
2008
+ | cell m k =>
2009
+ simp only [wfTab, Bool.and_eq_true, List.all_eq_true, decide_eq_true_eq] at hw
2010
+ exact ⟨hw.1, hw.2⟩
2011
+ | cellT m ks =>
2012
+ simp only [wfTab, Bool.and_eq_true, List.all_eq_true, decide_eq_true_eq] at hw
2013
+ exact ⟨hw.1, hw.2⟩
2014
+ | mono fs =>
2015
+ simp only [wfTab, Bool.and_eq_true, List.all_eq_true, decide_eq_true_eq] at hw
2016
+ exact ⟨hw.1, hw.2⟩
2017
+ | term e => exact ⟨trivial, by simpa [wfTab] using hw⟩
2018
+ have := build_valid env maps ds _ (pre ++ [d]) (valid_snoc d hv hd.1) (by simpa using hd.2)
2019
+ simpa [build] using this
2020
+
2021
+ theorem model_of_valid {env : Nat → Nat} {maps : Nat → Nat → Nat} {x : List Int} {tab : List VDef}
2022
+ (hv : Valid env maps x tab) : Model tab env maps x := by
2023
+ obtain ⟨hl, _, hx⟩ := hv
2024
+ refine ⟨fun j d hj => by rw [List.getD_eq_getElem?_getD, hx j d hj]; rfl, fun j => ?_⟩
2025
+ rw [List.getD_eq_getElem?_getD]
2026
+ cases h : x[j]? with
2027
+ | none => simp
2028
+ | some v =>
2029
+ have hj : j < tab.length := by
2030
+ have := (List.getElem?_eq_some_iff.mp h).1; omega
2031
+ obtain ⟨d, hd⟩ : ∃ d, tab[j]? = some d := ⟨tab[j], List.getElem?_eq_getElem hj⟩
2032
+ rw [hx j d hd] at h
2033
+ simp only [Option.some.injEq] at h; subst h
2034
+ cases d <;> simp [valD]
2035
+ exact Int.le_max_right _ _
2036
+
2037
+ /-- The kernel's tree check is sound: an accepted goal's hypotheses hold for no naturals and no maps. -/
2038
+ theorem sound (g : Goal) (t : Tree) (h : checkGoal g t = true) (env : Nat → Nat) (maps : Nat → Nat → Nat) :
2039
+ ¬ ∀ c ∈ g.hyps, evalC env maps c = true := by
2040
+ intro hH
2041
+ simp only [checkGoal, Bool.and_eq_true] at h
2042
+ have hv := build_valid env maps g.tab [] [] ⟨rfl, by simp, by simp⟩ (by simpa using h.1)
2043
+ simp only [List.nil_append] at hv
2044
+ exact checkT_sound (model_of_valid hv) hH t [] (by simp [PathHolds]) h.2
2045
+
2046
+ /-! Step machines with scalar state: the induction rule over kernel goals. The goals are built here, in Lean, from
2047
+ the machine, so the TypeScript `obligations` is not trusted for these: a law holds at the start (`startGoal` is
2048
+ refuted) and one step keeps every law (`stepGoal` is refuted, assuming them all and the request's assumption), so every
2049
+ law holds in every reachable state. Laws may not read the request's inputs. -/
2050
+
2051
+ def assoc (σ : List (Nat × E)) (i : Nat) : Option E := (lookup (fun p => p.1 == i) σ).map (·.2)
2052
+
2053
+ mutual
2054
+ def substE (σ : List (Nat × E)) : E → E
2055
+ | .var i => match assoc σ i with
2056
+ | some e => e
2057
+ | none => .var i
2058
+ | .const n => .const n
2059
+ | .add a b => .add (substE σ a) (substE σ b)
2060
+ | .sub a b => .sub (substE σ a) (substE σ b)
2061
+ | .mul k a => .mul k (substE σ a)
2062
+ | .min a b => .min (substE σ a) (substE σ b)
2063
+ | .max a b => .max (substE σ a) (substE σ b)
2064
+ | .prod a b => .prod (substE σ a) (substE σ b)
2065
+ | .ite c t f => .ite (substC σ c) (substE σ t) (substE σ f)
2066
+ | .get m key => .get m (substE σ key)
2067
+ | .tup a b => .tup (substE σ a) (substE σ b)
2068
+ | .proj k a => .proj k (substE σ a)
2069
+ def substC (σ : List (Nat × E)) : C → C
2070
+ | .le a b => .le (substE σ a) (substE σ b)
2071
+ | .lt a b => .lt (substE σ a) (substE σ b)
2072
+ | .eq a b => .eq (substE σ a) (substE σ b)
2073
+ | .and a b => .and (substC σ a) (substC σ b)
2074
+ | .or a b => .or (substC σ a) (substC σ b)
2075
+ | .not a => .not (substC σ a)
2076
+ end
2077
+
2078
+ -- The environment after σ: each assigned variable takes its term's value, the rest keep theirs.
2079
+ def applyS (σ : List (Nat × E)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) : Nat → Nat :=
2080
+ fun i => match assoc σ i with
2081
+ | some e => evalE ρ maps e
2082
+ | none => ρ i
2083
+
2084
+ mutual
2085
+ theorem subst_evalE (σ : List (Nat × E)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
2086
+ (e : E) → evalE ρ maps (substE σ e) = evalE (applyS σ ρ maps) maps e
2087
+ | .var i => by
2088
+ simp only [substE]
2089
+ cases h : assoc σ i <;> simp [evalE, applyS, h]
2090
+ | .const n => by simp [substE, evalE]
2091
+ | .add a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2092
+ | .sub a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2093
+ | .mul k a => by simp [substE, evalE, subst_evalE σ ρ maps a]
2094
+ | .min a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2095
+ | .max a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2096
+ | .prod a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2097
+ | .ite c t f => by simp [substE, evalE, subst_evalC σ ρ maps c, subst_evalE σ ρ maps t, subst_evalE σ ρ maps f]
2098
+ | .get m key => by simp [substE, evalE, subst_evalE σ ρ maps key]
2099
+ | .tup a b => by simp [substE, evalE, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2100
+ | .proj k a => by simp [substE, evalE, subst_evalE σ ρ maps a]
2101
+ theorem subst_evalC (σ : List (Nat × E)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
2102
+ (c : C) → evalC ρ maps (substC σ c) = evalC (applyS σ ρ maps) maps c
2103
+ | .le a b => by simp [substC, evalC, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2104
+ | .lt a b => by simp [substC, evalC, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2105
+ | .eq a b => by simp [substC, evalC, subst_evalE σ ρ maps a, subst_evalE σ ρ maps b]
2106
+ | .and a b => by simp [substC, evalC, subst_evalC σ ρ maps a, subst_evalC σ ρ maps b]
2107
+ | .or a b => by simp [substC, evalC, subst_evalC σ ρ maps a, subst_evalC σ ρ maps b]
2108
+ | .not a => by simp [substC, evalC, subst_evalC σ ρ maps a]
2109
+ end
2110
+
2111
+ mutual
2112
+ def occE (i : Nat) : E → Bool
2113
+ | .var j => j == i
2114
+ | .const _ => false
2115
+ | .add a b => occE i a || occE i b
2116
+ | .sub a b => occE i a || occE i b
2117
+ | .mul _ a => occE i a
2118
+ | .min a b => occE i a || occE i b
2119
+ | .max a b => occE i a || occE i b
2120
+ | .prod a b => occE i a || occE i b
2121
+ | .ite c t f => occC i c || occE i t || occE i f
2122
+ | .get _ key => occE i key
2123
+ | .tup a b => occE i a || occE i b
2124
+ | .proj _ a => occE i a
2125
+ def occC (i : Nat) : C → Bool
2126
+ | .le a b => occE i a || occE i b
2127
+ | .lt a b => occE i a || occE i b
2128
+ | .eq a b => occE i a || occE i b
2129
+ | .and a b => occC i a || occC i b
2130
+ | .or a b => occC i a || occC i b
2131
+ | .not a => occC i a
2132
+ end
2133
+
2134
+ mutual
2135
+ theorem congrE (ρ ρ' : Nat → Nat) (maps : Nat → Nat → Nat) :
2136
+ (e : E) → (∀ i, occE i e = true → ρ i = ρ' i) → evalE ρ maps e = evalE ρ' maps e
2137
+ | .var j, h => by simp only [evalE]; exact h j (by simp [occE])
2138
+ | .const n, _ => rfl
2139
+ | .add a b, h => by
2140
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2141
+ | .sub a b, h => by
2142
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2143
+ | .mul k a, h => by simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi]))]
2144
+ | .min a b, h => by
2145
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2146
+ | .max a b, h => by
2147
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2148
+ | .prod a b, h => by
2149
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2150
+ | .ite c t f, h => by
2151
+ simp only [evalE, congrC ρ ρ' maps c (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps t (fun i hi => h i (by simp [occE, hi])),
2152
+ congrE ρ ρ' maps f (fun i hi => h i (by simp [occE, hi]))]
2153
+ | .get m key, h => by simp only [evalE, congrE ρ ρ' maps key (fun i hi => h i (by simp [occE, hi]))]
2154
+ | .tup a b, h => by
2155
+ simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occE, hi]))]
2156
+ | .proj k a, h => by simp only [evalE, congrE ρ ρ' maps a (fun i hi => h i (by simp [occE, hi]))]
2157
+ theorem congrC (ρ ρ' : Nat → Nat) (maps : Nat → Nat → Nat) :
2158
+ (c : C) → (∀ i, occC i c = true → ρ i = ρ' i) → evalC ρ maps c = evalC ρ' maps c
2159
+ | .le a b, h => by
2160
+ simp only [evalC, congrE ρ ρ' maps a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occC, hi]))]
2161
+ | .lt a b, h => by
2162
+ simp only [evalC, congrE ρ ρ' maps a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occC, hi]))]
2163
+ | .eq a b, h => by
2164
+ simp only [evalC, congrE ρ ρ' maps a (fun i hi => h i (by simp [occC, hi])), congrE ρ ρ' maps b (fun i hi => h i (by simp [occC, hi]))]
2165
+ | .and a b, h => by
2166
+ simp only [evalC, congrC ρ ρ' maps a (fun i hi => h i (by simp [occC, hi])), congrC ρ ρ' maps b (fun i hi => h i (by simp [occC, hi]))]
2167
+ | .or a b, h => by
2168
+ simp only [evalC, congrC ρ ρ' maps a (fun i hi => h i (by simp [occC, hi])), congrC ρ ρ' maps b (fun i hi => h i (by simp [occC, hi]))]
2169
+ | .not a, h => by simp only [evalC, congrC ρ ρ' maps a (fun i hi => h i (by simp [occC, hi]))]
2170
+ end
2171
+
2172
+ structure Machine where
2173
+ inputs : List Nat
2174
+ init : List (Nat × E) -- each state variable's start, over the params
2175
+ step : List (Nat × E) -- each state variable after a request, over params, inputs and state
2176
+ pre : C -- about the params alone: a precondition, so it holds at the start and at every step
2177
+ assume : C -- about each request (and the params); holds at a step, not at the start
2178
+
2179
+ -- A request: its inputs replace those variables; nothing else changes until the step.
2180
+ def withInputs (inputs : List Nat) (ρ ι : Nat → Nat) : Nat → Nat := fun i => if inputs.contains i then ι i else ρ i
2181
+
2182
+ inductive Reach (m : Machine) (maps : Nat → Nat → Nat) : (Nat → Nat) → Prop
2183
+ | start (ρ : Nat → Nat) : evalC ρ maps m.pre = true → Reach m maps (applyS m.init ρ maps)
2184
+ | step (ρ ι : Nat → Nat) : Reach m maps ρ → evalC (withInputs m.inputs ρ ι) maps m.assume = true →
2185
+ Reach m maps (applyS m.step (withInputs m.inputs ρ ι) maps)
2186
+
2187
+ def startGoal (m : Machine) (law : C) : List C := [m.pre, .not (substC m.init law)]
2188
+ def stepGoal (m : Machine) (laws : List C) (law : C) : List C := [m.pre, m.assume] ++ laws ++ [.not (substC m.step law)]
2189
+
2190
+ -- Per law: the law, then (table, tree) for its start goal and for its step goal.
2191
+ abbrev MachineProof := List (C × (List VDef × Tree) × (List VDef × Tree))
2192
+
2193
+ -- None of `vs` occurs in c.
2194
+ def avoids (vs : List Nat) (c : C) : Bool := vs.all fun i => !occC i c
2195
+
2196
+ -- The precondition reads only params: no input, no variable the start or a step assigns.
2197
+ def preOk (m : Machine) : Bool := avoids (m.inputs ++ m.init.map (·.1) ++ m.step.map (·.1)) m.pre
2198
+
2199
+ def checkMachine (m : Machine) (ps : MachineProof) : Bool :=
2200
+ preOk m && ps.all fun p => avoids m.inputs p.1
2201
+ && checkGoal ⟨p.2.1.1, startGoal m p.1⟩ p.2.1.2
2202
+ && checkGoal ⟨p.2.2.1, stepGoal m (ps.map (·.1)) p.1⟩ p.2.2.2
2203
+
2204
+ theorem avoids_occ {vs : List Nat} {c : C} (h : avoids vs c = true) {i : Nat} (hi : occC i c = true) : i ∉ vs := by
2205
+ intro hm
2206
+ simp only [avoids, List.all_eq_true] at h
2207
+ have := h i hm; simp [hi] at this
2208
+
2209
+ theorem assoc_mem {σ : List (Nat × E)} {i : Nat} {e : E} (h : assoc σ i = some e) : i ∈ σ.map (·.1) := by
2210
+ simp only [assoc, Option.map_eq_some_iff] at h
2211
+ obtain ⟨a, ha, _⟩ := h
2212
+ have ⟨hm, hp⟩ := lookup_some ha
2213
+ simp only [beq_iff_eq] at hp
2214
+ exact List.mem_map.mpr ⟨a, hm, hp⟩
2215
+
2216
+ -- σ leaves every variable it doesn't assign alone.
2217
+ theorem applyS_other {σ : List (Nat × E)} {i : Nat} (h : i ∉ σ.map (·.1)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
2218
+ applyS σ ρ maps i = ρ i := by
2219
+ simp only [applyS]
2220
+ cases ha : assoc σ i with
2221
+ | none => rfl
2222
+ | some e => exact absurd (assoc_mem ha) h
2223
+
2224
+ theorem withInputs_other {inputs : List Nat} {i : Nat} (h : i ∉ inputs) (ρ ι : Nat → Nat) : withInputs inputs ρ ι i = ρ i := by
2225
+ simp only [withInputs]; split
2226
+ · rename_i hc; exact absurd (List.contains_iff_mem.mp hc) h
2227
+ · rfl
2228
+
2229
+ -- Conditions that avoid some variables read the same before and after anything that only changes those.
2230
+ theorem evalC_avoids {vs : List Nat} {c : C} (h : avoids vs c = true) {ρ ρ' : Nat → Nat} (maps : Nat → Nat → Nat)
2231
+ (hρ : ∀ i, i ∉ vs → ρ i = ρ' i) : evalC ρ maps c = evalC ρ' maps c :=
2232
+ congrC ρ ρ' maps c (fun i hi => hρ i (avoids_occ h hi))
2233
+
2234
+ theorem pre_reach {m : Machine} (hp : preOk m = true) {maps : Nat → Nat → Nat} :
2235
+ ∀ {ρ}, Reach m maps ρ → evalC ρ maps m.pre = true := by
2236
+ intro ρ hr
2237
+ induction hr with
2238
+ | start ρ h0 =>
2239
+ rw [evalC_avoids hp maps (ρ' := ρ) (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ maps)]; exact h0
2240
+ | step ρ ι _ _ ih =>
2241
+ rw [evalC_avoids hp maps (ρ' := ρ) (fun i hi => by
2242
+ rw [applyS_other (fun h => hi (by simp [h])), withInputs_other (fun h => hi (by simp [h]))])]; exact ih
2243
+
2244
+ /-- The induction rule: for params that meet the precondition, every law of an accepted machine proof holds in every
2245
+ reachable state, for any maps. -/
2246
+ theorem machine_sound (m : Machine) (ps : MachineProof) (h : checkMachine m ps = true) (maps : Nat → Nat → Nat) :
2247
+ ∀ ρ, Reach m maps ρ → ∀ l ∈ ps.map (·.1), evalC ρ maps l = true := by
2248
+ simp only [checkMachine, List.all_eq_true, Bool.and_eq_true] at h
2249
+ obtain ⟨hpre, h⟩ := h
2250
+ intro ρ hr
2251
+ induction hr with
2252
+ | start ρ h0 =>
2253
+ intro l hl
2254
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl
2255
+ have hs := sound _ _ (h p hp).1.2 ρ maps
2256
+ apply Decidable.byContradiction
2257
+ intro hf
2258
+ apply hs
2259
+ intro c hc
2260
+ simp only [startGoal, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
2261
+ rcases hc with rfl | rfl
2262
+ · exact h0
2263
+ · simp only [evalC, subst_evalC]; simpa using hf
2264
+ | step ρ ι hr ha ih =>
2265
+ intro l hl
2266
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl
2267
+ have hs := sound _ _ (h p hp).2 (withInputs m.inputs ρ ι) maps
2268
+ apply Decidable.byContradiction
2269
+ intro hf
2270
+ apply hs
2271
+ intro c hc
2272
+ simp only [stepGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
2273
+ rcases hc with ((rfl | rfl) | hc) | rfl
2274
+ · rw [evalC_avoids hpre maps (ρ' := ρ) (fun i hi => withInputs_other (fun h => hi (by simp [h])) ρ ι)]
2275
+ exact pre_reach hpre hr
2276
+ · exact ha
2277
+ · obtain ⟨q, hq, rfl⟩ := List.mem_map.mp hc
2278
+ rw [evalC_avoids (h q hq).1.1 maps (ρ' := ρ) (fun i hi => withInputs_other hi ρ ι)]
2279
+ exact ih q.1 (List.mem_map.mpr ⟨q, hq, rfl⟩)
2280
+ · simp only [evalC, subst_evalC]; simpa using hf
2281
+
2282
+ /-! Loops: termination. A loop runs `body` while `guard` holds, each turn reading fresh inputs that meet `assume`. That it
2283
+ halts is a liveness of its machine (Live.lean's Loop.machine and loop_halts_of): from anywhere, the guard fails later. -/
2284
+
2285
+ structure Loop where
2286
+ inputs : List Nat
2287
+ init : List (Nat × E)
2288
+ body : List (Nat × E)
2289
+ guard : C -- while
2290
+ pre : C -- about the params alone
2291
+ assume : C -- about each turn's inputs
2292
+
2293
+ -- One turn: some inputs meeting the assumption, the guard holds, the body runs.
2294
+ def Turn (l : Loop) (maps : Nat → Nat → Nat) (ρ ρ' : Nat → Nat) : Prop :=
2295
+ ∃ ι, evalC (withInputs l.inputs ρ ι) maps l.guard = true ∧ evalC (withInputs l.inputs ρ ι) maps l.assume = true ∧
2296
+ ρ' = applyS l.body (withInputs l.inputs ρ ι) maps
2297
+
2298
+ /-! Folds over binary trees: structural induction. Each leaf and node carries its own inputs; a leaf's state comes from
2299
+ its inputs, a node's from its children's (read through copies of the state variables, one per side) and its inputs.
2300
+ The goals are built here: each law holds at every leaf, and at a node whenever every law holds at both children. -/
2301
+
2302
+ structure Fold where
2303
+ inputs : List Nat
2304
+ state : List (Nat × Nat × Nat) -- a state variable, its copy for the left child, its copy for the right child
2305
+ leaf : List (Nat × E)
2306
+ node : List (Nat × E)
2307
+ pre : C
2308
+ assume : C
2309
+
2310
+ inductive BTree where
2311
+ | leaf (ι : Nat → Nat)
2312
+ | node (ι : Nat → Nat) (l r : BTree)
2313
+
2314
+ -- A node's environment: each copy reads the child's state variable.
2315
+ def withChildren (f : Fold) (env a b : Nat → Nat) : Nat → Nat := fun i =>
2316
+ match lookup (fun e => e.2.1 == i) f.state with
2317
+ | some e => a e.1
2318
+ | none => match lookup (fun e => e.2.2 == i) f.state with
2319
+ | some e => b e.1
2320
+ | none => env i
2321
+
2322
+ def foldT (f : Fold) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) : BTree → Nat → Nat
2323
+ | .leaf ι => applyS f.leaf (withInputs f.inputs ρ ι) maps
2324
+ | .node ι l r => applyS f.node (withChildren f (withInputs f.inputs ρ ι) (foldT f ρ maps l) (foldT f ρ maps r)) maps
2325
+
2326
+ -- Every leaf's and node's inputs meet the assumption.
2327
+ def Allowed (f : Fold) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) : BTree → Prop
2328
+ | .leaf ι => evalC (withInputs f.inputs ρ ι) maps f.assume = true
2329
+ | .node ι l r => evalC (withChildren f (withInputs f.inputs ρ ι) (foldT f ρ maps l) (foldT f ρ maps r)) maps f.assume = true
2330
+ ∧ Allowed f ρ maps l ∧ Allowed f ρ maps r
2331
+
2332
+ def leftSub (f : Fold) : List (Nat × E) := f.state.map fun e => (e.1, .var e.2.1)
2333
+ def rightSub (f : Fold) : List (Nat × E) := f.state.map fun e => (e.1, .var e.2.2)
2334
+
2335
+ def leafGoal (f : Fold) (law : C) : List C := [f.pre, f.assume, .not (substC f.leaf law)]
2336
+ def nodeGoal (f : Fold) (laws : List C) (law : C) : List C :=
2337
+ [f.pre, f.assume] ++ laws.flatMap (fun h => [substC (leftSub f) h, substC (rightSub f) h]) ++ [.not (substC f.node law)]
2338
+
2339
+ -- Variables a fold changes or reads per tree: inputs, the copies, and whatever a leaf or node assigns.
2340
+ def touched (f : Fold) : List Nat :=
2341
+ f.inputs ++ f.state.map (·.2.1) ++ f.state.map (·.2.2) ++ f.leaf.map (·.1) ++ f.node.map (·.1)
2342
+ -- Copies name one state variable each, and no right copy is a left copy.
2343
+ def copiesOk (f : Fold) : Bool :=
2344
+ f.state.all fun e => f.state.all fun e' => (e.2.1 != e'.2.1 || e.1 == e'.1) && (e.2.2 != e'.2.2 || e.1 == e'.1) && e.2.2 != e'.2.1
2345
+
2346
+ -- Per law: the law, then (table, tree) for its leaf goal and its node goal.
2347
+ abbrev FoldProof := MachineProof
2348
+
2349
+ def checkFold (f : Fold) (ps : FoldProof) : Bool :=
2350
+ copiesOk f && avoids (touched f) f.pre
2351
+ && ps.all fun p => avoids ((touched f).filter fun i => !(f.state.map (·.1)).contains i) p.1
2352
+ && checkGoal ⟨p.2.1.1, leafGoal f p.1⟩ p.2.1.2
2353
+ && checkGoal ⟨p.2.2.1, nodeGoal f (ps.map (·.1)) p.1⟩ p.2.2.2
2354
+
2355
+ theorem lookup_none {α : Type} {p : α → Bool} {l : List α} (h : ∀ a ∈ l, p a = false) : lookup p l = none := by
2356
+ cases hl : lookup p l with
2357
+ | none => rfl
2358
+ | some a => have := lookup_some hl; simp [h a this.1] at this
2359
+
2360
+ theorem lookup_none_all {α : Type} {p : α → Bool} : ∀ {l : List α}, lookup p l = none → ∀ a ∈ l, p a = false
2361
+ | [], _, a, ha => by simp at ha
2362
+ | b :: bs, h, a, ha => by
2363
+ simp only [lookup] at h
2364
+ split at h
2365
+ · simp at h
2366
+ · rcases List.mem_cons.mp ha with rfl | ha
2367
+ · simpa using (by assumption : ¬ p a = true)
2368
+ · exact lookup_none_all h a ha
2369
+
2370
+ theorem withChildren_plain (f : Fold) (env a b : Nat → Nat) {i : Nat} (hl : i ∉ f.state.map (·.2.1))
2371
+ (hr : i ∉ f.state.map (·.2.2)) : withChildren f env a b i = env i := by
2372
+ simp only [withChildren]
2373
+ rw [lookup_none (fun e he => by simp only [beq_eq_false_iff_ne]; intro h; exact hl (List.mem_map.mpr ⟨e, he, h⟩)),
2374
+ lookup_none (fun e he => by simp only [beq_eq_false_iff_ne]; intro h; exact hr (List.mem_map.mpr ⟨e, he, h⟩))]
2375
+
2376
+ theorem withChildren_left {f : Fold} (hc : copiesOk f = true) (env a b : Nat → Nat) {e : Nat × Nat × Nat} (he : e ∈ f.state) :
2377
+ withChildren f env a b e.2.1 = a e.1 := by
2378
+ simp only [copiesOk, List.all_eq_true, Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq, beq_iff_eq] at hc
2379
+ simp only [withChildren]
2380
+ cases h : lookup (fun e' => e'.2.1 == e.2.1) f.state with
2381
+ | none => have := lookup_none_all h e he; simp at this
2382
+ | some e' =>
2383
+ have ⟨hm, hp⟩ := lookup_some h
2384
+ simp only [beq_iff_eq] at hp
2385
+ have := (hc e' hm e he).1.1
2386
+ simp only [hp, not_true_eq_false, false_or] at this
2387
+ simp only [this]
2388
+
2389
+ theorem withChildren_right {f : Fold} (hc : copiesOk f = true) (env a b : Nat → Nat) {e : Nat × Nat × Nat} (he : e ∈ f.state) :
2390
+ withChildren f env a b e.2.2 = b e.1 := by
2391
+ simp only [copiesOk, List.all_eq_true, Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq, beq_iff_eq] at hc
2392
+ simp only [withChildren]
2393
+ rw [lookup_none (fun e' he' => by simp only [beq_eq_false_iff_ne]; intro h; exact (hc e he e' he').2 h.symm)]
2394
+ cases h : lookup (fun e' => e'.2.2 == e.2.2) f.state with
2395
+ | none => have := lookup_none_all h e he; simp at this
2396
+ | some e' =>
2397
+ have ⟨hm, hp⟩ := lookup_some h
2398
+ simp only [beq_iff_eq] at hp
2399
+ have := (hc e' hm e he).1.2
2400
+ simp only [hp, not_true_eq_false, false_or] at this
2401
+ simp only [this]
2402
+
2403
+ theorem foldT_other (f : Fold) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) {i : Nat} (hi : i ∉ touched f) :
2404
+ ∀ t, foldT f ρ maps t i = ρ i
2405
+ | .leaf ι => by
2406
+ simp only [foldT]
2407
+ rw [applyS_other (fun h => hi (by simp [touched, h])), withInputs_other (fun h => hi (by simp [touched, h]))]
2408
+ | .node ι l r => by
2409
+ simp only [foldT]
2410
+ rw [applyS_other (fun h => hi (by simp [touched, h])), withChildren_plain _ _ _ _ (fun h => hi (by simp [touched, h]))
2411
+ (fun h => hi (by simp [touched, h])), withInputs_other (fun h => hi (by simp [touched, h]))]
2412
+
2413
+ theorem assoc_map {s : List (Nat × Nat × Nat)} {g : Nat × Nat × Nat → Nat} {i : Nat} {x : E}
2414
+ (h : assoc (s.map fun e => (e.1, E.var (g e))) i = some x) : ∃ e ∈ s, e.1 = i ∧ x = .var (g e) := by
2415
+ simp only [assoc, Option.map_eq_some_iff] at h
2416
+ obtain ⟨a, ha, rfl⟩ := h
2417
+ have ⟨hm, hp⟩ := lookup_some ha
2418
+ obtain ⟨e, he, rfl⟩ := List.mem_map.mp hm
2419
+ simp only [beq_iff_eq] at hp
2420
+ exact ⟨e, he, hp, rfl⟩
2421
+
2422
+ theorem assoc_map_none {s : List (Nat × Nat × Nat)} {g : Nat × Nat × Nat → Nat} {i : Nat} (hi : i ∉ s.map (·.1)) :
2423
+ assoc (s.map fun e => (e.1, E.var (g e))) i = none := by
2424
+ cases h : assoc (s.map fun e => (e.1, E.var (g e))) i with
2425
+ | none => rfl
2426
+ | some x => obtain ⟨e, he, rfl, -⟩ := assoc_map h; exact absurd (List.mem_map.mpr ⟨e, he, rfl⟩) hi
2427
+
2428
+ theorem assoc_map_some {s : List (Nat × Nat × Nat)} {g : Nat × Nat × Nat → Nat} {i : Nat} (hi : i ∈ s.map (·.1))
2429
+ (h : assoc (s.map fun e => (e.1, E.var (g e))) i = none) : False := by
2430
+ simp only [assoc, Option.map_eq_none_iff] at h
2431
+ obtain ⟨e, he, rfl⟩ := List.mem_map.mp hi
2432
+ have := lookup_none_all h (e.1, E.var (g e)) (List.mem_map.mpr ⟨e, he, rfl⟩)
2433
+ simp at this
2434
+
2435
+ -- With no assumption (the trivially true one), every tree is allowed.
2436
+ theorem allowed_trivial (f : Fold) (h : f.assume = .le (.const 0) (.const 0)) (ρ : Nat → Nat) (maps : Nat → Nat → Nat) :
2437
+ ∀ t, Allowed f ρ maps t
2438
+ | .leaf ι => by simp [Allowed, h, evalC, evalE]
2439
+ | .node ι l r => ⟨by simp [h, evalC, evalE], allowed_trivial f h ρ maps l, allowed_trivial f h ρ maps r⟩
2440
+
2441
+ /-- Structural induction: for params that meet the precondition, every law of an accepted fold proof holds of every
2442
+ tree whose inputs meet the assumption, for any maps. -/
2443
+ theorem fold_sound (f : Fold) (ps : FoldProof) (h : checkFold f ps = true) (maps : Nat → Nat → Nat) (ρ : Nat → Nat)
2444
+ (h0 : evalC ρ maps f.pre = true) : ∀ t, Allowed f ρ maps t → ∀ l ∈ ps.map (·.1), evalC (foldT f ρ maps t) maps l = true := by
2445
+ simp only [checkFold, List.all_eq_true, Bool.and_eq_true] at h
2446
+ obtain ⟨⟨hc, hpre⟩, h⟩ := h
2447
+ have preAt : ∀ env : Nat → Nat, (∀ i, i ∉ touched f → env i = ρ i) → evalC env maps f.pre = true := fun env he => by
2448
+ rw [evalC_avoids hpre maps (ρ' := ρ) he]; exact h0
2449
+ -- What a law reads: the state variables, or variables no tree changes.
2450
+ have reads : ∀ q ∈ ps, ∀ i, occC i q.1 = true → i ∈ f.state.map (·.1) ∨ i ∉ touched f := by
2451
+ intro q hq i hi
2452
+ by_cases hs : i ∈ f.state.map (·.1)
2453
+ · exact Or.inl hs
2454
+ · refine Or.inr fun ht => avoids_occ (h q hq).1.1 hi ?_
2455
+ simp only [List.mem_filter, ht, true_and, Bool.not_eq_true', List.contains_iff_mem]
2456
+ simpa using hs
2457
+ intro t
2458
+ induction t with
2459
+ | leaf ι =>
2460
+ intro ha l hl
2461
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl
2462
+ apply Decidable.byContradiction; intro hn
2463
+ apply sound _ _ (h p hp).1.2 (withInputs f.inputs ρ ι) maps
2464
+ intro c hc
2465
+ simp only [leafGoal, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
2466
+ rcases hc with rfl | rfl | rfl
2467
+ · exact preAt _ (fun i hi => withInputs_other (fun h => hi (by simp [touched, h])) ρ ι)
2468
+ · exact ha
2469
+ · simp only [evalC, subst_evalC]; simpa [foldT] using hn
2470
+ | node ι lt rt ihl ihr =>
2471
+ intro ha l hl
2472
+ obtain ⟨hA, hal, har⟩ := ha
2473
+ obtain ⟨p, hp, rfl⟩ := List.mem_map.mp hl
2474
+ let Env := withChildren f (withInputs f.inputs ρ ι) (foldT f ρ maps lt) (foldT f ρ maps rt)
2475
+ -- A law at a child, read through that side's copies.
2476
+ have side : ∀ (g : Nat × Nat × Nat → Nat) (child : BTree),
2477
+ (∀ e ∈ f.state, Env (g e) = foldT f ρ maps child e.1) → ∀ q ∈ ps,
2478
+ evalC Env maps (substC (f.state.map fun e => (e.1, E.var (g e))) q.1) = evalC (foldT f ρ maps child) maps q.1 := by
2479
+ intro g child hg q hq
2480
+ rw [subst_evalC]
2481
+ refine congrC _ _ maps q.1 (fun i hi => ?_)
2482
+ cases hx : assoc (f.state.map fun e => (e.1, E.var (g e))) i with
2483
+ | some x =>
2484
+ obtain ⟨e, he, rfl, rfl⟩ := assoc_map hx
2485
+ simp only [applyS, hx, evalE]; exact hg e he
2486
+ | none =>
2487
+ have hs : i ∉ f.state.map (·.1) := fun hm => assoc_map_some (g := g) hm hx
2488
+ have ht : i ∉ touched f := (reads q hq i hi).resolve_left hs
2489
+ simp only [applyS, hx, Env]
2490
+ rw [withChildren_plain _ _ _ _ (fun h => ht (by simp [touched, h])) (fun h => ht (by simp [touched, h])),
2491
+ withInputs_other (fun h => ht (by simp [touched, h])), foldT_other f ρ maps ht]
2492
+ apply Decidable.byContradiction; intro hn
2493
+ apply sound _ _ (h p hp).2 Env maps
2494
+ intro c hc'
2495
+ simp only [nodeGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false,
2496
+ List.mem_flatMap] at hc'
2497
+ rcases hc' with ((rfl | rfl) | ⟨q, hq, hcq⟩) | rfl
2498
+ · exact preAt Env (fun i hi => by
2499
+ simp only [Env]
2500
+ rw [withChildren_plain _ _ _ _ (fun h => hi (by simp [touched, h])) (fun h => hi (by simp [touched, h])),
2501
+ withInputs_other (fun h => hi (by simp [touched, h]))])
2502
+ · exact hA
2503
+ · obtain ⟨q', hq', rfl⟩ := List.mem_map.mp hq
2504
+ rcases hcq with rfl | rfl
2505
+ · simp only [leftSub]
2506
+ rw [side (·.2.1) lt (fun e he => withChildren_left hc _ _ _ he) q' hq']
2507
+ exact ihl hal _ (List.mem_map.mpr ⟨q', hq', rfl⟩)
2508
+ · simp only [rightSub]
2509
+ rw [side (·.2.2) rt (fun e he => withChildren_right hc _ _ _ he) q' hq']
2510
+ exact ihr har _ (List.mem_map.mpr ⟨q', hq', rfl⟩)
2511
+ · simp only [evalC, subst_evalC]; simpa [foldT, Env] using hn
2512
+
2513
+ /-! Step machines with maps. A step may write each map at one key (read-over-write: a read after the step is the
2514
+ written value at the written key, the old cell elsewhere). A law may hold at every key: it is proven at a fresh skolem
2515
+ variable and assumed, before the step, at whatever keys the proof names (instantiating ∀ is sound at any term). The
2516
+ goals are built here, as for scalar machines. -/
2517
+
2518
+ mutual
2519
+ def readsE : E → Bool
2520
+ | .var _ => false
2521
+ | .const _ => false
2522
+ | .add a b => readsE a || readsE b
2523
+ | .sub a b => readsE a || readsE b
2524
+ | .mul _ a => readsE a
2525
+ | .min a b => readsE a || readsE b
2526
+ | .max a b => readsE a || readsE b
2527
+ | .prod a b => readsE a || readsE b
2528
+ | .ite c t f => readsC c || readsE t || readsE f
2529
+ | .get _ _ => true
2530
+ | .tup a b => readsE a || readsE b
2531
+ | .proj _ a => readsE a
2532
+ def readsC : C → Bool
2533
+ | .le a b => readsE a || readsE b
2534
+ | .lt a b => readsE a || readsE b
2535
+ | .eq a b => readsE a || readsE b
2536
+ | .and a b => readsC a || readsC b
2537
+ | .or a b => readsC a || readsC b
2538
+ | .not a => readsC a
2539
+ end
2540
+
2541
+ mutual
2542
+ theorem noReadsE (ρ : Nat → Nat) (M M' : Nat → Nat → Nat) : (e : E) → readsE e = false → evalE ρ M e = evalE ρ M' e
2543
+ | .var _, _ => rfl
2544
+ | .const _, _ => rfl
2545
+ | .add a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2546
+ | .sub a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2547
+ | .mul _ a, h => by simp only [readsE] at h; simp only [evalE, noReadsE ρ M M' a h]
2548
+ | .min a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2549
+ | .max a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2550
+ | .prod a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2551
+ | .ite c t f, h => by
2552
+ simp only [readsE, Bool.or_eq_false_iff] at h
2553
+ simp only [evalE, noReadsC ρ M M' c h.1.1, noReadsE ρ M M' t h.1.2, noReadsE ρ M M' f h.2]
2554
+ | .get _ _, h => by simp [readsE] at h
2555
+ | .tup a b, h => by simp only [readsE, Bool.or_eq_false_iff] at h; simp only [evalE, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2556
+ | .proj _ a, h => by simp only [readsE] at h; simp only [evalE, noReadsE ρ M M' a h]
2557
+ theorem noReadsC (ρ : Nat → Nat) (M M' : Nat → Nat → Nat) : (c : C) → readsC c = false → evalC ρ M c = evalC ρ M' c
2558
+ | .le a b, h => by simp only [readsC, Bool.or_eq_false_iff] at h; simp only [evalC, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2559
+ | .lt a b, h => by simp only [readsC, Bool.or_eq_false_iff] at h; simp only [evalC, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2560
+ | .eq a b, h => by simp only [readsC, Bool.or_eq_false_iff] at h; simp only [evalC, noReadsE ρ M M' a h.1, noReadsE ρ M M' b h.2]
2561
+ | .and a b, h => by simp only [readsC, Bool.or_eq_false_iff] at h; simp only [evalC, noReadsC ρ M M' a h.1, noReadsC ρ M M' b h.2]
2562
+ | .or a b, h => by simp only [readsC, Bool.or_eq_false_iff] at h; simp only [evalC, noReadsC ρ M M' a h.1, noReadsC ρ M M' b h.2]
2563
+ | .not a, h => by simp only [readsC] at h; simp only [evalC, noReadsC ρ M M' a h]
2564
+ end
2565
+
2566
+ -- Substitution of variables by σ and of each read by `rd` (applied to the substituted key).
2567
+ mutual
2568
+ def substRE (σ : List (Nat × E)) (rd : Nat → E → E) : E → E
2569
+ | .var i => match assoc σ i with
2570
+ | some e => e
2571
+ | none => .var i
2572
+ | .const n => .const n
2573
+ | .add a b => .add (substRE σ rd a) (substRE σ rd b)
2574
+ | .sub a b => .sub (substRE σ rd a) (substRE σ rd b)
2575
+ | .mul k a => .mul k (substRE σ rd a)
2576
+ | .min a b => .min (substRE σ rd a) (substRE σ rd b)
2577
+ | .max a b => .max (substRE σ rd a) (substRE σ rd b)
2578
+ | .prod a b => .prod (substRE σ rd a) (substRE σ rd b)
2579
+ | .ite c t f => .ite (substRC σ rd c) (substRE σ rd t) (substRE σ rd f)
2580
+ | .get m key => rd m (substRE σ rd key)
2581
+ | .tup a b => .tup (substRE σ rd a) (substRE σ rd b)
2582
+ | .proj k a => .proj k (substRE σ rd a)
2583
+ def substRC (σ : List (Nat × E)) (rd : Nat → E → E) : C → C
2584
+ | .le a b => .le (substRE σ rd a) (substRE σ rd b)
2585
+ | .lt a b => .lt (substRE σ rd a) (substRE σ rd b)
2586
+ | .eq a b => .eq (substRE σ rd a) (substRE σ rd b)
2587
+ | .and a b => .and (substRC σ rd a) (substRC σ rd b)
2588
+ | .or a b => .or (substRC σ rd a) (substRC σ rd b)
2589
+ | .not a => .not (substRC σ rd a)
2590
+ end
2591
+
2592
+ section
2593
+ variable (σ : List (Nat × E)) (rd : Nat → E → E) (W : Nat → Nat) (M M' : Nat → Nat → Nat)
2594
+ (hrd : ∀ m k, evalE W M (rd m k) = M' m (evalE W M k))
2595
+ include hrd
2596
+ mutual
2597
+ theorem substR_evalE : (e : E) → evalE W M (substRE σ rd e) = evalE (applyS σ W M) M' e
2598
+ | .var i => by
2599
+ simp only [substRE]
2600
+ cases h : assoc σ i <;> simp [evalE, applyS, h]
2601
+ | .const n => by simp [substRE, evalE]
2602
+ | .add a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2603
+ | .sub a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2604
+ | .mul k a => by simp [substRE, evalE, substR_evalE a]
2605
+ | .min a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2606
+ | .max a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2607
+ | .prod a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2608
+ | .ite c t f => by simp [substRE, evalE, substR_evalC c, substR_evalE t, substR_evalE f]
2609
+ | .get m key => by simp only [substRE, evalE, hrd, substR_evalE key]
2610
+ | .tup a b => by simp [substRE, evalE, substR_evalE a, substR_evalE b]
2611
+ | .proj k a => by simp [substRE, evalE, substR_evalE a]
2612
+ theorem substR_evalC : (c : C) → evalC W M (substRC σ rd c) = evalC (applyS σ W M) M' c
2613
+ | .le a b => by simp [substRC, evalC, substR_evalE a, substR_evalE b]
2614
+ | .lt a b => by simp [substRC, evalC, substR_evalE a, substR_evalE b]
2615
+ | .eq a b => by simp [substRC, evalC, substR_evalE a, substR_evalE b]
2616
+ | .and a b => by simp [substRC, evalC, substR_evalC a, substR_evalC b]
2617
+ | .or a b => by simp [substRC, evalC, substR_evalC a, substR_evalC b]
2618
+ | .not a => by simp [substRC, evalC, substR_evalC a]
2619
+ end
2620
+ end
2621
+
2622
+ structure MMachine where
2623
+ inputs : List Nat
2624
+ init : List (Nat × E)
2625
+ maps0 : List (Nat × Nat) -- each map's value at every key at the start
2626
+ step : List (Nat × E)
2627
+ writes : List (Nat × E × E) -- a map, the key it's written at and the value, over the state before the step
2628
+ pre : C -- about the params alone, reading no map
2629
+ assume : C -- about each request
2630
+
2631
+ def writeMaps (ws : List (Nat × E × E)) (W : Nat → Nat) (M : Nat → Nat → Nat) : Nat → Nat → Nat := fun m key =>
2632
+ match lookup (fun w => w.1 == m) ws with
2633
+ | some w => if key = evalE W M w.2.1 then evalE W M w.2.2 else M m key
2634
+ | none => M m key
2635
+
2636
+ def StartMaps (m : MMachine) (M : Nat → Nat → Nat) : Prop := ∀ p ∈ m.maps0, ∀ key, M p.1 key = p.2
2637
+
2638
+ inductive MReach (m : MMachine) : (Nat → Nat) → (Nat → Nat → Nat) → Prop
2639
+ | start (ρ : Nat → Nat) (M : Nat → Nat → Nat) : StartMaps m M → evalC ρ M m.pre = true → MReach m (applyS m.init ρ M) M
2640
+ | step (ρ : Nat → Nat) (M : Nat → Nat → Nat) (ι : Nat → Nat) : MReach m ρ M →
2641
+ evalC (withInputs m.inputs ρ ι) M m.assume = true →
2642
+ MReach m (applyS m.step (withInputs m.inputs ρ ι) M) (writeMaps m.writes (withInputs m.inputs ρ ι) M)
2643
+
2644
+ def upd (ρ : Nat → Nat) (i v : Nat) : Nat → Nat := fun j => if j = i then v else ρ j
2645
+
2646
+ -- A law, and for a law about every key, the variable it holds at.
2647
+ structure MLaw where
2648
+ holds : C
2649
+ key : Option Nat
2650
+
2651
+ def MLaw.Holds (l : MLaw) (ρ : Nat → Nat) (M : Nat → Nat → Nat) : Prop :=
2652
+ match l.key with
2653
+ | none => evalC ρ M l.holds = true
2654
+ | some kv => ∀ v, evalC (upd ρ kv v) M l.holds = true
2655
+
2656
+ def rdStart (m : MMachine) : Nat → E → E := fun mp k =>
2657
+ match lookup (fun p => p.1 == mp) m.maps0 with
2658
+ | some p => .const p.2
2659
+ | none => .get mp k
2660
+ def rdStep (m : MMachine) : Nat → E → E := fun mp k =>
2661
+ match lookup (fun w => w.1 == mp) m.writes with
2662
+ | some w => .ite (.eq k w.2.1) w.2.2 (.get mp k)
2663
+ | none => .get mp k
2664
+
2665
+ -- What a goal proves: the law, at the skolem variable if it holds at every key.
2666
+ def goalOf (l : MLaw) (sk : Nat) : C :=
2667
+ match l.key with
2668
+ | none => l.holds
2669
+ | some kv => substC [(kv, .var sk)] l.holds
2670
+
2671
+ structure MEntry where
2672
+ law : MLaw
2673
+ sk : Nat -- the skolem variable
2674
+ insts : List (Nat × E) -- (law index, key): where the step goal assumes the laws about every key
2675
+ start : List VDef × Tree
2676
+ step : List VDef × Tree
2677
+
2678
+ def scalarHyps (laws : List MLaw) : List C := laws.filterMap fun l =>
2679
+ match l.key with
2680
+ | none => some l.holds
2681
+ | some _ => none
2682
+ def instHyps (laws : List MLaw) (insts : List (Nat × E)) : List C := insts.filterMap fun p =>
2683
+ match laws[p.1]? with
2684
+ | some l => match l.key with
2685
+ | some kv => some (substC [(kv, p.2)] l.holds)
2686
+ | none => none
2687
+ | none => none
2688
+
2689
+ def mStartGoal (m : MMachine) (e : MEntry) : List C := [m.pre, .not (substRC m.init (rdStart m) (goalOf e.law e.sk))]
2690
+ def mStepGoal (m : MMachine) (laws : List MLaw) (e : MEntry) : List C :=
2691
+ [m.pre, m.assume] ++ scalarHyps laws ++ instHyps laws e.insts ++ [.not (substRC m.step (rdStep m) (goalOf e.law e.sk))]
2692
+
2693
+ -- The skolem is fresh: no input or state variable, and nothing the machine or a law reads.
2694
+ def skOk (m : MMachine) (laws : List MLaw) (sk : Nat) : Bool :=
2695
+ !(m.inputs ++ m.init.map (·.1) ++ m.step.map (·.1)).contains sk && !occC sk m.pre && !occC sk m.assume
2696
+ && laws.all (fun l => !occC sk l.holds) && m.init.all (fun p => !occE sk p.2) && m.step.all (fun p => !occE sk p.2)
2697
+ && m.writes.all (fun w => !occE sk w.2.1 && !occE sk w.2.2)
2698
+
2699
+ def checkMMachine (m : MMachine) (ps : List MEntry) : Bool :=
2700
+ avoids (m.inputs ++ m.init.map (·.1) ++ m.step.map (·.1)) m.pre && !readsC m.pre
2701
+ && ps.all fun e => avoids m.inputs e.law.holds && skOk m (ps.map (·.law)) e.sk
2702
+ && checkGoal ⟨e.start.1, mStartGoal m e⟩ e.start.2
2703
+ && checkGoal ⟨e.step.1, mStepGoal m (ps.map (·.law)) e⟩ e.step.2
2704
+
2705
+ theorem occ_false {i : Nat} {c : C} (h : (!occC i c) = true) : occC i c = false := by simpa using h
2706
+
2707
+ -- A law reads the same at the skolem as it holds at every key.
2708
+ theorem goalOf_holds (l : MLaw) {sk : Nat} (hs : occC sk l.holds = false) (ρ : Nat → Nat) (M : Nat → Nat → Nat) :
2709
+ (∀ v, evalC (upd ρ sk v) M (goalOf l sk) = true) → l.Holds ρ M := by
2710
+ intro h
2711
+ unfold MLaw.Holds
2712
+ unfold goalOf at h
2713
+ split
2714
+ · rename_i hk
2715
+ simp only [hk] at h
2716
+ have := h 0
2717
+ rwa [congrC _ ρ M l.holds (fun i hi => by simp only [upd]; split <;> simp_all)] at this
2718
+ · rename_i kv hk
2719
+ simp only [hk] at h
2720
+ intro v
2721
+ have := h v
2722
+ rw [subst_evalC] at this
2723
+ rwa [congrC _ (upd ρ kv v) M l.holds (fun i hi => by
2724
+ simp only [applyS, assoc, lookup, upd]
2725
+ by_cases hik : i = kv
2726
+ · subst hik; simp [evalE, upd]
2727
+ · have hb : (kv == i) = false := by simp; omega
2728
+ simp only [hb, Bool.false_eq_true, ite_false, Option.map_none, hik]
2729
+ split <;> simp_all)] at this
2730
+
2731
+ theorem pre_mreach {m : MMachine} (hp : avoids (m.inputs ++ m.init.map (·.1) ++ m.step.map (·.1)) m.pre = true)
2732
+ (hr : readsC m.pre = false) : ∀ {ρ M}, MReach m ρ M → evalC ρ M m.pre = true := by
2733
+ intro ρ M h
2734
+ induction h with
2735
+ | start ρ M _ h0 =>
2736
+ rw [evalC_avoids hp M (ρ' := ρ) (fun i hi => applyS_other (fun h => hi (by simp [h])) ρ M)]; exact h0
2737
+ | step ρ M ι _ _ ih =>
2738
+ rw [noReadsC _ _ M m.pre hr]
2739
+ rw [evalC_avoids hp M (ρ' := ρ) (fun i hi => by
2740
+ rw [applyS_other (fun h => hi (by simp [h])), withInputs_other (fun h => hi (by simp [h]))])]; exact ih
2741
+
2742
+ -- With a fresh skolem, σ commutes with setting it.
2743
+ theorem applyS_upd {σ : List (Nat × E)} {sk : Nat} (hk : sk ∉ σ.map (·.1)) (he : σ.all (fun p => !occE sk p.2) = true)
2744
+ (W : Nat → Nat) (M : Nat → Nat → Nat) (v : Nat) : applyS σ (upd W sk v) M = upd (applyS σ W M) sk v := by
2745
+ funext i
2746
+ simp only [upd]
2747
+ by_cases hi : i = sk
2748
+ · subst hi; rw [applyS_other hk]; simp [upd]
2749
+ · simp only [hi, ite_false, applyS]
2750
+ cases h : assoc σ i with
2751
+ | none => simp [upd, hi]
2752
+ | some e =>
2753
+ simp only [assoc, Option.map_eq_some_iff] at h
2754
+ obtain ⟨p, hp, rfl⟩ := h
2755
+ have hm := (lookup_some hp).1
2756
+ simp only [List.all_eq_true] at he
2757
+ have := he p hm
2758
+ exact congrE _ _ M p.2 (fun j hj => by simp only [upd]; split <;> simp_all)
2759
+
2760
+ /-- The induction rule with maps: for params that meet the precondition and maps that start as declared, every law of
2761
+ an accepted proof holds in every reachable state (a law about every key, at every key). -/
2762
+ theorem mmachine_sound (m : MMachine) (ps : List MEntry) (h : checkMMachine m ps = true) :
2763
+ ∀ ρ M, MReach m ρ M → ∀ e ∈ ps, e.law.Holds ρ M := by
2764
+ simp only [checkMMachine, List.all_eq_true, Bool.and_eq_true] at h
2765
+ obtain ⟨⟨hpre, hrp⟩, h⟩ := h
2766
+ simp only [Bool.not_eq_true'] at hrp
2767
+ have hsk : ∀ e ∈ ps, _ := fun e he => (h e he).1.1.2
2768
+ intro ρ M hr
2769
+ induction hr with
2770
+ | start ρ M hM h0 =>
2771
+ intro e he
2772
+ obtain ⟨⟨⟨_, hs⟩, hst⟩, _⟩ := h e he
2773
+ simp only [skOk, Bool.and_eq_true, Bool.not_eq_true', List.all_eq_true] at hs
2774
+ obtain ⟨⟨⟨⟨⟨⟨hfresh, hspre⟩, _⟩, hslaw⟩, hsinit⟩, _⟩, _⟩ := hs
2775
+ refine goalOf_holds e.law (by simpa using hslaw e.law (List.mem_map.mpr ⟨e, he, rfl⟩)) _ M (fun v => ?_)
2776
+ apply Decidable.byContradiction; intro hn
2777
+ apply sound _ _ hst (upd ρ e.sk v) M
2778
+ intro c hc
2779
+ simp only [mStartGoal, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
2780
+ rcases hc with rfl | rfl
2781
+ · rw [congrC _ ρ M m.pre (fun i hi => by simp only [upd]; split <;> simp_all)]; exact h0
2782
+ · have hrd : ∀ mp k, evalE (upd ρ e.sk v) M (rdStart m mp k) = M mp (evalE (upd ρ e.sk v) M k) := by
2783
+ intro mp k
2784
+ simp only [rdStart]
2785
+ split
2786
+ · rename_i p hp
2787
+ have ⟨hm, hq⟩ := lookup_some hp
2788
+ simp only [beq_iff_eq] at hq
2789
+ simp only [evalE]; rw [← hq, hM p hm]
2790
+ · rfl
2791
+ simp only [evalC, substR_evalC _ _ _ M M hrd]
2792
+ rw [applyS_upd (fun h => by simp [h] at hfresh) (by simpa [List.all_eq_true] using hsinit)]
2793
+ simpa using hn
2794
+ | step ρ M ι hr ha ih =>
2795
+ intro e he
2796
+ obtain ⟨⟨⟨hin, hs⟩, _⟩, hst⟩ := h e he
2797
+ simp only [skOk, Bool.and_eq_true, Bool.not_eq_true', List.all_eq_true] at hs
2798
+ obtain ⟨⟨⟨⟨⟨⟨hfresh, hspre⟩, hsas⟩, hslaw⟩, _⟩, hsstep⟩, hsw⟩ := hs
2799
+ have hfresh' : e.sk ∉ m.inputs ∧ e.sk ∉ m.step.map (·.1) := by
2800
+ constructor <;> intro hm <;> simp [hm] at hfresh
2801
+ refine goalOf_holds e.law (by simpa using hslaw e.law (List.mem_map.mpr ⟨e, he, rfl⟩)) _ _ (fun v => ?_)
2802
+ let W0 := withInputs m.inputs ρ ι
2803
+ apply Decidable.byContradiction; intro hn
2804
+ apply sound _ _ hst (upd W0 e.sk v) M
2805
+ -- Reading a law about the state before the step, at the goal's environment.
2806
+ have before : ∀ (l : MLaw), l ∈ ps.map (·.law) → ∀ (g : Nat → Nat), (∀ i, i ∉ m.inputs → i ≠ e.sk → g i = ρ i) →
2807
+ avoids m.inputs l.holds = true → occC e.sk l.holds = false → ∀ c, c = l.holds → evalC g M c = evalC ρ M l.holds := by
2808
+ intro l _ g hg hl hsl c hc; subst hc
2809
+ exact congrC _ _ M l.holds (fun i hi => hg i (avoids_occ hl hi) (by intro h'; subst h'; simp [hsl] at hi))
2810
+ intro c hc
2811
+ simp only [mStepGoal, List.mem_append, List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at hc
2812
+ rcases hc with (((rfl | rfl) | hc) | hc) | rfl
2813
+ · rw [congrC _ ρ M m.pre (fun i hi => by
2814
+ simp only [upd, W0]
2815
+ split
2816
+ · rename_i h'; subst h'; simp [hspre] at hi
2817
+ · exact withInputs_other (fun hm => avoids_occ hpre hi (by simp [hm])) ρ ι)]
2818
+ exact pre_mreach hpre hrp hr
2819
+ · rw [congrC _ W0 M m.assume (fun i hi => by simp only [upd]; split <;> simp_all)]; exact ha
2820
+ · simp only [scalarHyps, List.mem_filterMap] at hc
2821
+ obtain ⟨l, hl, hk⟩ := hc
2822
+ split at hk
2823
+ · rename_i hkey
2824
+ simp only [Option.some.injEq] at hk; subst hk
2825
+ obtain ⟨e', he', rfl⟩ := List.mem_map.mp hl
2826
+ have := ih e' he'
2827
+ simp only [MLaw.Holds, hkey] at this
2828
+ rw [before e'.law hl _ (fun i hi hs' => by simp only [upd, hs', ite_false, W0]; exact withInputs_other hi ρ ι)
2829
+ (h e' he').1.1.1 (by simpa using hslaw e'.law hl) _ rfl]
2830
+ exact this
2831
+ · simp at hk
2832
+ · simp only [instHyps, List.mem_filterMap] at hc
2833
+ obtain ⟨p, _, hk⟩ := hc
2834
+ split at hk
2835
+ · rename_i l hl
2836
+ split at hk
2837
+ · rename_i kv hkey
2838
+ simp only [Option.some.injEq] at hk; subst hk
2839
+ have hlm : l ∈ ps.map (·.law) := List.mem_of_getElem? hl
2840
+ obtain ⟨e', he', rfl⟩ := List.mem_map.mp hlm
2841
+ have := ih e' he'
2842
+ simp only [MLaw.Holds, hkey] at this
2843
+ rw [subst_evalC]
2844
+ have hsl : occC e.sk e'.law.holds = false := by simpa using hslaw e'.law hlm
2845
+ rw [congrC _ (upd ρ kv (evalE (upd W0 e.sk v) M p.2)) M e'.law.holds (fun i hi => by
2846
+ simp only [applyS, assoc, lookup, upd]
2847
+ by_cases hik : i = kv
2848
+ · subst hik; simp
2849
+ · have hb : (kv == i) = false := by simp; omega
2850
+ have hs' : i ≠ e.sk := by intro h'; subst h'; simp [hsl] at hi
2851
+ simp only [hb, Bool.false_eq_true, ite_false, Option.map_none, hik, hs', W0]
2852
+ exact withInputs_other (avoids_occ (h e' he').1.1.1 hi) ρ ι)]
2853
+ exact this _
2854
+ · simp at hk
2855
+ · simp at hk
2856
+ · have hrd : ∀ mp k, evalE (upd W0 e.sk v) M (rdStep m mp k) = writeMaps m.writes W0 M mp (evalE (upd W0 e.sk v) M k) := by
2857
+ intro mp k
2858
+ simp only [rdStep, writeMaps]
2859
+ split
2860
+ · rename_i w hw
2861
+ have hm := (lookup_some hw).1
2862
+ have h1 := hsw w hm
2863
+ try simp only [Bool.and_eq_true, Bool.not_eq_true'] at h1
2864
+ have e1 : evalE (upd W0 e.sk v) M w.2.1 = evalE W0 M w.2.1 :=
2865
+ congrE _ _ M _ (fun j hj => by simp only [upd]; split <;> simp_all)
2866
+ have e2 : evalE (upd W0 e.sk v) M w.2.2 = evalE W0 M w.2.2 :=
2867
+ congrE _ _ M _ (fun j hj => by simp only [upd]; split <;> simp_all)
2868
+ simp only [evalE, evalC, e1, e2]
2869
+ by_cases hk : evalE (upd W0 e.sk v) M k = evalE W0 M w.2.1 <;> simp [hk]
2870
+ · rfl
2871
+ simp only [evalC, substR_evalC _ _ _ M _ hrd]
2872
+ rw [applyS_upd hfresh'.2 (by simpa [List.all_eq_true] using hsstep)]
2873
+ simpa using hn
2874
+
2875
+ end Kern
2876
+
2877
+ #print axioms Kern.keyArity_of_parts
2878
+ #print axioms Kern.sound
2879
+ #print axioms Kern.machine_sound
2880
+ #print axioms Kern.fold_sound
2881
+ #print axioms Kern.mmachine_sound