gdp-wired 0.51.0 → 0.53.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) 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/aggregate-kinds.d.ts +10 -0
  5. package/dist/core/aggregate-kinds.js +44 -0
  6. package/dist/core/aggregate-search.d.ts +9 -0
  7. package/dist/core/aggregate-search.js +74 -0
  8. package/dist/core/aggregate-weighted/candidates.d.ts +18 -0
  9. package/dist/core/aggregate-weighted/candidates.js +128 -0
  10. package/dist/core/aggregate-weighted/search.d.ts +17 -0
  11. package/dist/core/aggregate-weighted/search.js +68 -0
  12. package/dist/core/cli.d.ts +2 -0
  13. package/dist/core/cli.js +8 -1
  14. package/dist/core/cycles.d.ts +2 -0
  15. package/dist/core/cycles.js +12 -9
  16. package/dist/core/dsl.d.ts +7 -1
  17. package/dist/core/dsl.js +158 -26
  18. package/dist/core/filtered-sum-candidates.d.ts +17 -0
  19. package/dist/core/filtered-sum-candidates.js +77 -0
  20. package/dist/core/live-support.d.ts +5 -0
  21. package/dist/core/live-support.js +148 -0
  22. package/dist/core/live.js +194 -37
  23. package/dist/core/lockstep.d.ts +5 -0
  24. package/dist/core/lockstep.js +23 -0
  25. package/dist/core/ordered-pair-seeds.d.ts +2 -0
  26. package/dist/core/ordered-pair-seeds.js +109 -0
  27. package/dist/core/proof-output.d.ts +49 -2
  28. package/dist/core/proof-output.js +44 -0
  29. package/dist/core/search-termination.d.ts +4 -0
  30. package/dist/core/search-termination.js +1 -1
  31. package/dist/core/source-check.d.ts +4 -1
  32. package/dist/core/source-check.js +5 -2
  33. package/dist/core/sparse-kind-proof.d.ts +7 -0
  34. package/dist/core/sparse-kind-proof.js +49 -0
  35. package/dist/core/step-seeds.d.ts +46 -0
  36. package/dist/core/step-seeds.js +316 -0
  37. package/dist/kernel/finite-sum.d.ts +31 -0
  38. package/dist/kernel/finite-sum.js +74 -0
  39. package/dist/kernel/ghost-write.d.ts +117 -0
  40. package/dist/kernel/ghost-write.js +89 -0
  41. package/dist/lean-kernel/Cert.lean +222 -0
  42. package/dist/lean-kernel/Count.lean +1568 -0
  43. package/dist/lean-kernel/Ghost.lean +373 -0
  44. package/dist/lean-kernel/GhostMap.lean +426 -0
  45. package/dist/lean-kernel/Kernel.lean +2881 -0
  46. package/dist/lean-kernel/Live.lean +1168 -0
  47. package/dist/lean-kernel/Progress.lean +543 -0
  48. package/dist/lean-kernel/Sets.lean +1975 -0
  49. package/dist/lean-kernel/StepLaw.lean +128 -0
  50. package/dist/lean-kernel/Steps.lean +477 -0
  51. package/dist/lean-kernel/Sums.lean +916 -0
  52. package/dist/lean-kernel/emit.d.ts +15 -0
  53. package/dist/lean-kernel/emit.js +47 -6
  54. package/dist/lean-kernel/finite-sum-export.d.ts +3 -0
  55. package/dist/lean-kernel/finite-sum-export.js +163 -0
  56. package/dist/lean-kernel/ghost-write-export.d.ts +3 -0
  57. package/dist/lean-kernel/ghost-write-export.js +81 -0
  58. package/dist/lean-kernel/lean-aggregate/FiniteFold.lean +44 -0
  59. package/dist/lean-kernel/lean-aggregate/GroupedTransport.lean +308 -0
  60. package/dist/lean-kernel/lean-aggregate/aggregate.d.ts +13 -0
  61. package/dist/lean-kernel/lean-aggregate/aggregate.js +68 -0
  62. package/dist/lean-kernel/lean-aggregate/assets.d.ts +5 -0
  63. package/dist/lean-kernel/lean-aggregate/assets.js +7 -0
  64. package/dist/lean-kernel/lean-aggregate/audit.d.ts +3 -0
  65. package/dist/lean-kernel/lean-aggregate/audit.js +13 -0
  66. package/dist/lean-kernel/lean-aggregate/exact.d.ts +15 -0
  67. package/dist/lean-kernel/lean-aggregate/exact.js +51 -0
  68. package/dist/lean-kernel/lean-aggregate/index.d.ts +19 -0
  69. package/dist/lean-kernel/lean-aggregate/index.js +52 -0
  70. package/dist/lean-kernel/lean-aggregate/point-fold.d.ts +27 -0
  71. package/dist/lean-kernel/lean-aggregate/point-fold.js +79 -0
  72. package/dist/lean-kernel/lean-aggregate/replay-fold.d.ts +2 -0
  73. package/dist/lean-kernel/lean-aggregate/replay-fold.js +163 -0
  74. package/dist/lean-kernel/lean-aggregate/source.d.ts +2 -0
  75. package/dist/lean-kernel/lean-aggregate/source.js +141 -0
  76. package/dist/lean-kernel/lean-aggregate/weighted-flags.d.ts +2 -0
  77. package/dist/lean-kernel/lean-aggregate/weighted-flags.js +26 -0
  78. package/dist/lean-kernel/lean-aggregate/weighted-replay.d.ts +7 -0
  79. package/dist/lean-kernel/lean-aggregate/weighted-replay.js +53 -0
  80. package/dist/lean-kernel/lean-aggregate/weighted-seeded.d.ts +30 -0
  81. package/dist/lean-kernel/lean-aggregate/weighted-seeded.js +64 -0
  82. package/dist/lean-kernel/lean-aggregate/weighted.d.ts +18 -0
  83. package/dist/lean-kernel/lean-aggregate/weighted.js +115 -0
  84. package/dist/lean-kernel/lean-aggregate/worker.d.ts +10 -0
  85. package/dist/lean-kernel/lean-aggregate/worker.js +46 -0
  86. package/dist/lean-kernel/library.js +3 -0
  87. package/dist/lean-kernel/lift-simulation/shape.js +5 -5
  88. package/docs/lean-aggregate.md +29 -0
  89. package/package.json +6 -1
@@ -0,0 +1,117 @@
1
+ import { type C, type E, type Law, type Machine } from '../core/machine.js';
2
+ import { type Instances, type Tree } from './kernel.js';
3
+ export type GhostWriteRecipe = {
4
+ ghostMap: string;
5
+ point: E;
6
+ gates: [column: string, value: number][];
7
+ group: string;
8
+ weight: string;
9
+ observer: string;
10
+ };
11
+ export type GhostWriteCertificate = {
12
+ kind: 'ghost-write';
13
+ recipe: GhostWriteRecipe;
14
+ projection: {
15
+ omitMap: string;
16
+ };
17
+ target: Machine;
18
+ laws: Law[];
19
+ pointLaw: string;
20
+ proof: {
21
+ instances: Record<string, Instances>;
22
+ trees: Record<string, Tree>;
23
+ };
24
+ };
25
+ export declare function ghostWriteTheory(source: Machine, law: Law, p: GhostWriteCertificate): {
26
+ orig: {
27
+ params: string[];
28
+ step: {
29
+ flags: Record<string, C>;
30
+ state?: Record<string, E>;
31
+ maps?: Record<string, import("../core/machine.ts").MapUpdate>;
32
+ };
33
+ primitiveFailure?: C;
34
+ name: string;
35
+ inputs: string[];
36
+ state: Record<string, E>;
37
+ maps?: Record<string, number>;
38
+ inputMaps?: string[];
39
+ pre?: C;
40
+ assume?: C;
41
+ history?: import("../core/machine.ts").History;
42
+ sums?: Record<string, import("../core/machine.ts").Sum>;
43
+ counts?: Record<string, import("../core/machine.ts").Counted>;
44
+ fns?: Record<string, import("../core/machine.ts").Fn>;
45
+ facts?: Law[];
46
+ leasts?: Record<string, import("../core/machine.ts").Least>;
47
+ relations?: Record<string, string[]>;
48
+ };
49
+ lift: {
50
+ orig: {
51
+ params: string[];
52
+ step: {
53
+ flags: Record<string, C>;
54
+ state?: Record<string, E>;
55
+ maps?: Record<string, import("../core/machine.ts").MapUpdate>;
56
+ };
57
+ primitiveFailure?: C;
58
+ name: string;
59
+ inputs: string[];
60
+ state: Record<string, E>;
61
+ maps?: Record<string, number>;
62
+ inputMaps?: string[];
63
+ pre?: C;
64
+ assume?: C;
65
+ history?: import("../core/machine.ts").History;
66
+ sums?: Record<string, import("../core/machine.ts").Sum>;
67
+ counts?: Record<string, import("../core/machine.ts").Counted>;
68
+ fns?: Record<string, import("../core/machine.ts").Fn>;
69
+ facts?: Law[];
70
+ leasts?: Record<string, import("../core/machine.ts").Least>;
71
+ relations?: Record<string, string[]>;
72
+ };
73
+ vars: {};
74
+ maps: {
75
+ [x: string]: {
76
+ start: number;
77
+ write: {
78
+ key: E;
79
+ value: E;
80
+ };
81
+ };
82
+ };
83
+ };
84
+ target: {
85
+ state: {
86
+ [x: string]: E;
87
+ };
88
+ sums: {
89
+ [x: string]: {
90
+ term: E;
91
+ key: string;
92
+ };
93
+ };
94
+ primitiveFailure?: C;
95
+ name: string;
96
+ params: string[];
97
+ inputs: string[];
98
+ maps?: Record<string, number>;
99
+ inputMaps?: string[];
100
+ step: {
101
+ flags?: Record<string, C>;
102
+ state?: Record<string, E>;
103
+ maps?: Record<string, import("../core/machine.ts").MapUpdate>;
104
+ };
105
+ pre?: C;
106
+ assume?: C;
107
+ history?: import("../core/machine.ts").History;
108
+ counts?: Record<string, import("../core/machine.ts").Counted>;
109
+ fns?: Record<string, import("../core/machine.ts").Fn>;
110
+ facts?: Law[];
111
+ leasts?: Record<string, import("../core/machine.ts").Least>;
112
+ relations?: Record<string, string[]>;
113
+ };
114
+ laws: Law[];
115
+ publicEntry: number;
116
+ } | undefined;
117
+ export declare function checkGhostWrite(source: Machine, law: Law, p: GhostWriteCertificate): boolean;
@@ -0,0 +1,89 @@
1
+ // Lean twin: GhostWriteTransport.checkedTransport, using the complete original
2
+ // post-row functional. Closed flags and a fresh observer are just views.
3
+ import { get, n, same, walkOnce, x } from "../core/machine.js";
4
+ import { checkLaws, ghostedMachine, ghostedOk, mapArities, obligations, own, sideGoals, substC } from "./kernel.js";
5
+ const text = (x) => JSON.stringify(x);
6
+ const eq = (a, b) => text(a) === text(b);
7
+ const safeNat = (v) => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;
8
+ const noSub = { vars: {}, maps: {}, flags: {} };
9
+ export function ghostWriteTheory(source, law, p) {
10
+ const r = p.recipe, ghost = r.ghostMap;
11
+ if (p.kind !== 'ghost-write' || Object.keys(p.projection).length !== 1 || p.projection.omitMap !== ghost ||
12
+ !law.forall || law.keys?.length || law.some?.length || law.ex || law.finite !== undefined ||
13
+ source.history || source.primitiveFailure || Object.keys(source.counts ?? {}).length || Object.keys(source.sums ?? {}).length ||
14
+ Object.keys(source.fns ?? {}).length || source.facts?.length || source.inputMaps?.length || Object.keys(source.leasts ?? {}).length)
15
+ return;
16
+ const sums = Object.entries(p.target.sums ?? {});
17
+ if (sums.length !== 1)
18
+ return;
19
+ const [sum, sd] = sums[0], used = text([source, law]);
20
+ const fresh = [r.observer, ghost, sum, sd.key];
21
+ if (new Set(fresh).size !== fresh.length || fresh.some(key => !key || used.includes(text(key))) ||
22
+ !eq(sd, { term: get(ghost, x(sd.key)), key: sd.key }))
23
+ return;
24
+ let unsupported = false;
25
+ walkOnce([source, law.holds, r.point], o => {
26
+ const t = o;
27
+ if (t.op === 'all' || t.op === 'get' && t.map?.startsWith('worddiv$') || t.op === 'const' && !safeNat(t.value) || t.op === 'mul' && !safeNat(t.k))
28
+ unsupported = true;
29
+ });
30
+ if (unsupported)
31
+ return;
32
+ obligations(source, [law]);
33
+ if (!Object.values(source.maps ?? {}).every(safeNat) || Object.values(mapArities(source, [law])).some(n => n !== 1))
34
+ return;
35
+ const written = (name) => { const w = own(source.step.maps ?? {}, name); return w && !('each' in w) ? w : undefined; };
36
+ if (!r.gates.length || !r.gates.every(([map, val]) => safeNat(val) && !!written(map)) || !written(r.group) || !written(r.weight) ||
37
+ !r.gates.some(([map]) => eq(written(map).key, r.point)))
38
+ return;
39
+ const post = (map, key) => {
40
+ const w = written(map);
41
+ return eq(key, w.key) ? w.value :
42
+ { op: 'if', c: same(key, w.key), then: w.value, else: get(map, key) };
43
+ };
44
+ const contribution = (read) => ({ op: 'if',
45
+ c: r.gates.reduceRight((tail, [map, val]) => ({ op: 'and', xs: [same(read(map), n(val)), tail] }), same(read(r.group), x(r.observer))),
46
+ then: read(r.weight), else: n(0) });
47
+ // Match the existing Lean closed-flag view of the same machine. Its dummy
48
+ // binders occur nowhere in the actual body or in the source machine.
49
+ let stem = 'internal$';
50
+ while (text([source, law, fresh]).includes(stem))
51
+ stem += 'a';
52
+ const closed = Object.create(null), inlined = Object.create(null);
53
+ for (const [i, [name, condition]] of Object.entries(source.step.flags ?? {}).entries()) {
54
+ const c = substC(condition, { ...noSub, flags: inlined });
55
+ inlined[name] = c;
56
+ closed[name] = { op: 'all', key: `${stem}${i}`, c };
57
+ }
58
+ const orig = { ...source, params: [...source.params, r.observer], step: { ...source.step, flags: closed } };
59
+ const lift = { orig, vars: {}, maps: { [ghost]: { start: 0, write: { key: r.point, value: contribution(map => post(map, r.point)) } } } };
60
+ if (!ghostedOk(lift))
61
+ return;
62
+ const full = ghostedMachine(lift);
63
+ const target = { ...full, state: { ...full.state, [sum]: n(0) }, sums: { [sum]: { term: get(ghost, x(sd.key)), key: sd.key } } };
64
+ if (!eq(target, p.target))
65
+ return;
66
+ const publicEntry = p.laws.findIndex(l => l.name === law.name), pointEntry = p.laws.findIndex(l => l.name === p.pointLaw);
67
+ if (publicEntry < 0 || pointEntry < 0 || publicEntry === pointEntry)
68
+ return;
69
+ const publicLaw = { name: law.name, holds: substC(law.holds, { ...noSub, vars: { [law.forall]: x(r.observer) } }) };
70
+ const binder = p.laws[pointEntry].forall;
71
+ if (!binder)
72
+ return;
73
+ const rowLaw = { name: p.pointLaw, forall: binder, holds: same(get(ghost, x(binder)), contribution(map => get(map, x(binder)))) };
74
+ if (!eq(publicLaw, p.laws[publicEntry]) || !eq(rowLaw, p.laws[pointEntry]))
75
+ return;
76
+ const laws = p.laws.map((entry, i) => i === publicEntry ? publicLaw : i === pointEntry ? rowLaw : entry);
77
+ if (laws.some(l => l.keys?.length || l.some?.length || l.ex || l.finite !== undefined))
78
+ return;
79
+ return { orig, lift, target, laws, publicEntry };
80
+ }
81
+ export function checkGhostWrite(source, law, p) {
82
+ try {
83
+ const t = ghostWriteTheory(source, law, p);
84
+ return !!t && sideGoals(t.target, t.laws).length === 0 && checkLaws(t.target, t.laws, p.proof);
85
+ }
86
+ catch {
87
+ return false;
88
+ }
89
+ }
@@ -0,0 +1,222 @@
1
+ /-
2
+ Cert: the trusted core of gdp-wired, with its proofs. Lean 4.34.0, core only.
3
+
4
+ A row `Σ cᵢ·xᵢ + c ≤ 0` over the integers. A certificate is a list of steps that replays into rows: an input row, a
5
+ positive combination of two earlier rows, or a cut (an earlier row whose coefficients are all g times q, replaced by q
6
+ with a constant c such that g·c ≤ constant + g − 1). `check` accepts when the last row has only zero coefficients and a
7
+ positive constant. `sound`: if `check` accepts, no integer point satisfies every input row.
8
+
9
+ A row holds its nonzero coefficients alone (column, coefficient), in column order, so a step costs a row's nonzeros
10
+ rather than the table's width. Any list reads as a sum (`dot`); sorted lists stay sorted and cancel to [] under `addL`.
11
+
12
+ `run_preserves` is the induction rule a step machine's laws rest on: a property true at the start and preserved by
13
+ every step holds after every sequence of steps.
14
+ -/
15
+
16
+ namespace Cert
17
+
18
+ structure Row where
19
+ coefs : List (Nat × Int)
20
+ const : Int
21
+ deriving Repr
22
+
23
+ def dot : List (Nat × Int) → List Int → Int
24
+ | [], _ => 0
25
+ | p :: rest, x => p.2 * x.getD p.1 0 + dot rest x
26
+
27
+ def eval (r : Row) (x : List Int) : Int := dot r.coefs x + r.const
28
+
29
+ -- Two sorted lists merged, coefficients of one column added (a column that cancels dropped). The fuel only bounds the
30
+ -- recursion: out of it, the lists are put side by side, which reads the same.
31
+ def addF : Nat → List (Nat × Int) → List (Nat × Int) → List (Nat × Int)
32
+ | 0, a, b => a ++ b
33
+ | _ + 1, [], b => b
34
+ | _ + 1, a, [] => a
35
+ | n + 1, p :: as, q :: bs =>
36
+ if p.1 < q.1 then p :: addF n as (q :: bs)
37
+ else if q.1 < p.1 then q :: addF n (p :: as) bs
38
+ else if p.2 + q.2 = 0 then addF n as bs else (p.1, p.2 + q.2) :: addF n as bs
39
+
40
+ def addL (a b : List (Nat × Int)) : List (Nat × Int) := addF (a.length + b.length) a b
41
+
42
+ def scaleL (m : Int) (a : List (Nat × Int)) : List (Nat × Int) := if m = 0 then [] else a.map (fun p => (p.1, m * p.2))
43
+
44
+ def allZero : List (Nat × Int) → Bool
45
+ | [] => true
46
+ | p :: a => p.2 == 0 && allZero a
47
+
48
+ theorem dot_app (x : List Int) : ∀ (a b : List (Nat × Int)), dot (a ++ b) x = dot a x + dot b x
49
+ | [], b => by simp [dot]
50
+ | p :: a, b => by simp only [List.cons_append, dot, dot_app x a b]; omega
51
+
52
+ theorem dot_addF (x : List Int) : ∀ (n : Nat) (a b : List (Nat × Int)), dot (addF n a b) x = dot a x + dot b x
53
+ | 0, a, b => by simp only [addF, dot_app]
54
+ | _ + 1, [], b => by simp [addF, dot]
55
+ | _ + 1, _ :: _, [] => by simp [addF, dot]
56
+ | n + 1, p :: as, q :: bs => by
57
+ simp only [addF]
58
+ split
59
+ · simp only [dot, dot_addF x n as (q :: bs)]; omega
60
+ · split
61
+ · simp only [dot, dot_addF x n (p :: as) bs]; omega
62
+ · have hpq : p.1 = q.1 := by omega
63
+ split
64
+ · rename_i hz
65
+ simp only [dot, dot_addF x n as bs, hpq]
66
+ have : p.2 * x.getD q.1 0 + q.2 * x.getD q.1 0 = 0 := by rw [← Int.add_mul, hz, Int.zero_mul]
67
+ omega
68
+ · simp only [dot, dot_addF x n as bs, hpq, Int.add_mul]; omega
69
+
70
+ theorem dot_addL (a b : List (Nat × Int)) (x : List Int) : dot (addL a b) x = dot a x + dot b x := dot_addF x _ a b
71
+
72
+ theorem dot_map (m : Int) (x : List Int) : ∀ (a : List (Nat × Int)), dot (a.map (fun p => (p.1, m * p.2))) x = m * dot a x
73
+ | [] => by simp [dot]
74
+ | p :: a => by simp only [List.map_cons, dot, dot_map m x a, Int.mul_add, Int.mul_assoc]
75
+
76
+ theorem dot_scaleL (m : Int) (a : List (Nat × Int)) (x : List Int) : dot (scaleL m a) x = m * dot a x := by
77
+ unfold scaleL
78
+ split
79
+ · rename_i h; subst h; simp [dot]
80
+ · exact dot_map m x a
81
+
82
+ theorem dot_allZero : ∀ (a : List (Nat × Int)) (x : List Int), allZero a = true → dot a x = 0
83
+ | [], _, _ => rfl
84
+ | p :: a, x, h => by
85
+ simp only [allZero, Bool.and_eq_true, beq_iff_eq] at h
86
+ simp [dot, h.1, dot_allZero a x h.2]
87
+
88
+ inductive Step where
89
+ | input (i : Nat)
90
+ | combine (a b : Nat) (ma mb : Int)
91
+ | cut (of : Nat) (g : Int) (q : List (Nat × Int)) (c : Int)
92
+ deriving Repr
93
+
94
+ def combineRow (ma mb : Int) (A B : Row) : Row :=
95
+ ⟨addL (scaleL ma A.coefs) (scaleL mb B.coefs), ma * A.const + mb * B.const⟩
96
+
97
+ def cutOk (A : Row) (g : Int) (q : List (Nat × Int)) (c : Int) : Bool :=
98
+ decide (1 ≤ g) && decide (A.coefs = scaleL g q) && decide (g * c ≤ A.const + g - 1)
99
+
100
+ -- The rows so far kept newest first, with their number (step i's row is at n − 1 − i): a row is put on the front and
101
+ -- read where it is, never past a chain of appends.
102
+ def replay (inputs : List Row) : List Step → Nat → List Row → Option (List Row)
103
+ | [], _, rows => some rows
104
+ | .input i :: ss, n, rows =>
105
+ match inputs[i]? with
106
+ | some r => replay inputs ss (n + 1) (r :: rows)
107
+ | none => none
108
+ | .combine a b ma mb :: ss, n, rows =>
109
+ match rows[n - 1 - a]?, rows[n - 1 - b]? with
110
+ | some A, some B => if a < n ∧ b < n ∧ 0 < ma ∧ 0 < mb then replay inputs ss (n + 1) (combineRow ma mb A B :: rows) else none
111
+ | _, _ => none
112
+ | .cut o g q c :: ss, n, rows =>
113
+ match rows[n - 1 - o]? with
114
+ | some A => if o < n ∧ cutOk A g q c then replay inputs ss (n + 1) (⟨q, c⟩ :: rows) else none
115
+ | none => none
116
+
117
+ def check (inputs : List Row) (steps : List Step) : Bool :=
118
+ match replay inputs steps 0 [] with
119
+ | some rows =>
120
+ match rows.head? with
121
+ | some r => allZero r.coefs && decide (0 < r.const)
122
+ | none => false
123
+ | none => false
124
+
125
+ theorem cut_sound (g d c c' : Int) (hg : 1 ≤ g) (h : g * d + c ≤ 0) (hc : g * c' ≤ c + g - 1) : d + c' ≤ 0 := by
126
+ have hd : g * (d + c') = g * d + g * c' := Int.mul_add g d c'
127
+ refine Int.not_lt.mp (fun h1 => ?_)
128
+ have h2 : g * 1 ≤ g * (d + c') := Int.mul_le_mul_of_nonneg_left (by omega) (by omega)
129
+ omega
130
+
131
+ -- Every row a replay produces holds wherever the inputs and the rows so far hold.
132
+ theorem replay_holds (inputs : List Row) (x : List Int) (hx : ∀ r ∈ inputs, eval r x ≤ 0) :
133
+ ∀ (ss : List Step) (n : Nat) (rows out : List Row), (∀ r ∈ rows, eval r x ≤ 0) →
134
+ replay inputs ss n rows = some out → ∀ r ∈ out, eval r x ≤ 0 := by
135
+ intro ss
136
+ induction ss with
137
+ | nil =>
138
+ intro n rows out hrows h
139
+ simp only [replay, Option.some.injEq] at h
140
+ exact h ▸ hrows
141
+ | cons s ss ih =>
142
+ intro n rows out hrows h
143
+ cases s with
144
+ | input i =>
145
+ simp only [replay] at h
146
+ split at h
147
+ · rename_i r hr
148
+ refine ih _ _ out ?_ h
149
+ intro r' hr'
150
+ rcases List.mem_cons.mp hr' with hr' | hr'
151
+ · exact hr' ▸ hx r (List.mem_of_getElem? hr)
152
+ · exact hrows r' hr'
153
+ · exact absurd h (by simp)
154
+ | combine a b ma mb =>
155
+ simp only [replay] at h
156
+ split at h
157
+ · rename_i A B hA hB
158
+ split at h
159
+ · rename_i hm
160
+ refine ih _ _ out ?_ h
161
+ intro r' hr'
162
+ rcases List.mem_cons.mp hr' with hr' | hr'
163
+ · subst hr'
164
+ have eA := hrows A (List.mem_of_getElem? hA)
165
+ have eB := hrows B (List.mem_of_getElem? hB)
166
+ simp only [eval, combineRow, dot_addL, dot_scaleL] at eA eB ⊢
167
+ have h1 : ma * (dot A.coefs x + A.const) ≤ ma * 0 := Int.mul_le_mul_of_nonneg_left eA (by omega)
168
+ have h2 : mb * (dot B.coefs x + B.const) ≤ mb * 0 := Int.mul_le_mul_of_nonneg_left eB (by omega)
169
+ simp only [Int.mul_add, Int.mul_zero] at h1 h2
170
+ omega
171
+ · exact hrows r' hr'
172
+ · exact absurd h (by simp)
173
+ · exact absurd h (by simp)
174
+ | cut o g q c =>
175
+ simp only [replay] at h
176
+ split at h
177
+ · rename_i A hA
178
+ split at h
179
+ · rename_i hok
180
+ refine ih _ _ out ?_ h
181
+ intro r' hr'
182
+ rcases List.mem_cons.mp hr' with hr' | hr'
183
+ · subst hr'
184
+ simp only [cutOk, Bool.and_eq_true, decide_eq_true_eq] at hok
185
+ obtain ⟨_, ⟨hg, hq⟩, hc⟩ := hok
186
+ have eA := hrows A (List.mem_of_getElem? hA)
187
+ have hdot : dot A.coefs x = g * dot q x := by rw [hq, dot_scaleL]
188
+ simp only [eval, hdot] at eA
189
+ exact cut_sound g (dot q x) A.const c hg eA hc
190
+ · exact hrows r' hr'
191
+ · exact absurd h (by simp)
192
+ · exact absurd h (by simp)
193
+
194
+ /-- A certificate the checker accepts refutes its inputs: no integer point satisfies them all. -/
195
+ theorem sound (inputs : List Row) (steps : List Step) (h : check inputs steps = true) (x : List Int)
196
+ (hx : ∀ r ∈ inputs, eval r x ≤ 0) : False := by
197
+ simp only [check] at h
198
+ split at h
199
+ · rename_i rows hrows
200
+ split at h
201
+ · rename_i r hr
202
+ simp only [Bool.and_eq_true, decide_eq_true_eq] at h
203
+ have hall := replay_holds inputs x hx steps 0 [] rows (by simp) hrows
204
+ have hm : r ∈ rows := by
205
+ cases rows with
206
+ | nil => simp at hr
207
+ | cons a as => simp only [List.head?, Option.some.injEq] at hr; subst hr; exact List.mem_cons_self
208
+ have := hall r hm
209
+ simp only [eval, dot_allZero r.coefs x h.1] at this
210
+ omega
211
+ · exact absurd h (by simp)
212
+ · exact absurd h (by simp)
213
+
214
+ /-- The induction rule: what holds at the start and survives every step holds after any sequence of steps. -/
215
+ theorem run_preserves {S I : Type} (step : S → I → S) (P : S → Prop) (start : S)
216
+ (h0 : P start) (hs : ∀ s i, P s → P (step s i)) : ∀ (is : List I), P (is.foldl step start) := by
217
+ intro is
218
+ induction is generalizing start with
219
+ | nil => exact h0
220
+ | cons i is ih => exact ih (step start i) (hs start i h0)
221
+
222
+ end Cert