gdp-wired 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -1
- package/dist/core/bmc.d.ts +38 -0
- package/dist/core/bmc.js +137 -0
- package/dist/core/check.d.ts +1 -1
- package/dist/core/check.js +19 -6
- package/dist/core/cycles.d.ts +15 -0
- package/dist/core/cycles.js +446 -8
- package/dist/core/cyclic.d.ts +6 -1
- package/dist/core/cyclic.js +13 -1
- package/dist/core/data.d.ts +17 -1
- package/dist/core/data.js +637 -97
- package/dist/core/dsl.d.ts +10 -1
- package/dist/core/dsl.js +47 -12
- package/dist/core/infer.js +518 -5
- package/dist/core/nonterm.d.ts +34 -0
- package/dist/core/nonterm.js +610 -0
- package/dist/core/q.d.ts +17 -0
- package/dist/core/q.js +52 -0
- package/dist/core/rationals.d.ts +112 -0
- package/dist/core/rationals.js +281 -0
- package/dist/core/relation.d.ts +28 -0
- package/dist/core/relation.js +480 -0
- package/dist/core/solver.d.ts +5 -2
- package/dist/core/solver.js +65 -20
- package/dist/core/transfer.d.ts +3 -3
- package/dist/core/transfer.js +77 -42
- package/dist/kernel/cdcl.d.ts +43 -0
- package/dist/kernel/cdcl.js +564 -0
- package/dist/kernel/kernel.d.ts +91 -0
- package/dist/kernel/kernel.js +310 -0
- package/dist/kernel/prove.d.ts +20 -1
- package/dist/kernel/prove.js +119 -44
- package/dist/kernel/qprove.d.ts +18 -0
- package/dist/kernel/qprove.js +157 -0
- package/dist/kernel/reader.d.ts +79 -0
- package/dist/kernel/reader.js +539 -0
- package/dist/kernel/simplex.d.ts +36 -0
- package/dist/kernel/simplex.js +259 -0
- package/dist/lean-kernel/emit.d.ts +19 -1
- package/dist/lean-kernel/emit.js +71 -3
- package/dist/lean-kernel/library.js +2 -0
- package/dist/lean-kernel/qlean.d.ts +26 -0
- package/dist/lean-kernel/qlean.js +105 -0
- package/package.json +6 -2
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
const eps = 1e-9;
|
|
2
|
+
// A bound's tolerance: relative past 1 (values drift by a few ulps a pivot).
|
|
3
|
+
const tol = (b) => 1e-7 * Math.max(1, Math.abs(b));
|
|
4
|
+
const gcd = (a, b) => (b === 0 ? Math.abs(a) : gcd(b, a % b));
|
|
5
|
+
const formMemo = new WeakMap();
|
|
6
|
+
function formOf(lin) {
|
|
7
|
+
let out = formMemo.get(lin);
|
|
8
|
+
if (out !== undefined)
|
|
9
|
+
return out;
|
|
10
|
+
const keys = Object.keys(lin).filter(key => key !== '1' && lin[key] !== 0).sort();
|
|
11
|
+
if (!keys.length)
|
|
12
|
+
out = null;
|
|
13
|
+
else if (keys.length === 1)
|
|
14
|
+
out = { single: keys[0], c: lin[keys[0]] };
|
|
15
|
+
else {
|
|
16
|
+
let g = 0;
|
|
17
|
+
for (const key of keys)
|
|
18
|
+
g = gcd(g, lin[key]);
|
|
19
|
+
if (lin[keys[0]] < 0)
|
|
20
|
+
g = -g;
|
|
21
|
+
const terms = keys.map(key => [key, lin[key] / g]);
|
|
22
|
+
out = { slack: `σ(${terms.map(([key, c]) => `${key}:${c}`).join(',')})`, g, terms };
|
|
23
|
+
}
|
|
24
|
+
formMemo.set(lin, out);
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
export class Simplex {
|
|
28
|
+
integral;
|
|
29
|
+
constructor(integral = true) {
|
|
30
|
+
this.integral = integral;
|
|
31
|
+
}
|
|
32
|
+
index = new Map();
|
|
33
|
+
lo = [];
|
|
34
|
+
hi = [];
|
|
35
|
+
loWhy = [];
|
|
36
|
+
hiWhy = [];
|
|
37
|
+
val = [];
|
|
38
|
+
// A basic variable's row: basic = Σ coef · nonbasic. rowOf[x] its row, or -1 for a nonbasic one; colOf[x] the rows
|
|
39
|
+
// a nonbasic x appears in.
|
|
40
|
+
rowOf = [];
|
|
41
|
+
rows = [];
|
|
42
|
+
colOf = [];
|
|
43
|
+
pivots = 0;
|
|
44
|
+
varOf(name) {
|
|
45
|
+
let j = this.index.get(name);
|
|
46
|
+
if (j !== undefined)
|
|
47
|
+
return j;
|
|
48
|
+
j = this.val.length;
|
|
49
|
+
this.index.set(name, j);
|
|
50
|
+
this.lo.push(-Infinity);
|
|
51
|
+
this.hi.push(Infinity);
|
|
52
|
+
this.loWhy.push(-1);
|
|
53
|
+
this.hiWhy.push(-1);
|
|
54
|
+
this.val.push(0);
|
|
55
|
+
this.rowOf.push(-1);
|
|
56
|
+
this.colOf.push(new Set());
|
|
57
|
+
return j;
|
|
58
|
+
}
|
|
59
|
+
// A slack for a form: basic, its row the form with each basic variable in it replaced by that one's row.
|
|
60
|
+
slackOf(f) {
|
|
61
|
+
const had = this.index.get(f.slack);
|
|
62
|
+
if (had !== undefined)
|
|
63
|
+
return had;
|
|
64
|
+
const coef = new Map();
|
|
65
|
+
const addTo = (j, c) => { const x = (coef.get(j) ?? 0) + c; if (Math.abs(x) < eps)
|
|
66
|
+
coef.delete(j);
|
|
67
|
+
else
|
|
68
|
+
coef.set(j, x); };
|
|
69
|
+
for (const [name, c] of f.terms) {
|
|
70
|
+
const j = this.varOf(name), r = this.rowOf[j];
|
|
71
|
+
if (r < 0)
|
|
72
|
+
addTo(j, c);
|
|
73
|
+
else
|
|
74
|
+
for (const [k, a] of this.rows[r].coef)
|
|
75
|
+
addTo(k, c * a);
|
|
76
|
+
}
|
|
77
|
+
const s = this.varOf(f.slack), r = this.rows.length;
|
|
78
|
+
this.rows.push({ basic: s, coef });
|
|
79
|
+
this.rowOf[s] = r;
|
|
80
|
+
for (const j of coef.keys())
|
|
81
|
+
this.colOf[j].add(r);
|
|
82
|
+
let value = 0;
|
|
83
|
+
for (const [j, c] of coef)
|
|
84
|
+
value += c * this.val[j];
|
|
85
|
+
this.val[s] = value;
|
|
86
|
+
return s;
|
|
87
|
+
}
|
|
88
|
+
// A node's rows as the bounds (a natural's own row x >= 0 among them). A pair of bounds that cross is a conflict at
|
|
89
|
+
// once.
|
|
90
|
+
load(rows) {
|
|
91
|
+
for (let j = 0; j < this.val.length; j++) {
|
|
92
|
+
this.lo[j] = -Infinity;
|
|
93
|
+
this.hi[j] = Infinity;
|
|
94
|
+
this.loWhy[j] = -1;
|
|
95
|
+
this.hiWhy[j] = -1;
|
|
96
|
+
}
|
|
97
|
+
const tighten = (j, upper, b, why) => {
|
|
98
|
+
if (upper) {
|
|
99
|
+
if (b < this.hi[j]) {
|
|
100
|
+
this.hi[j] = b;
|
|
101
|
+
this.hiWhy[j] = why;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else if (b > this.lo[j]) {
|
|
105
|
+
this.lo[j] = b;
|
|
106
|
+
this.loWhy[j] = why;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
const integral = this.integral;
|
|
110
|
+
// c·x + k <= 0: x <= −k/c (c > 0) or x >= k/−c (c < 0); rounded to integers, or over the rationals moved in when
|
|
111
|
+
// strict.
|
|
112
|
+
const bound = (c, j, k, i, strict) => {
|
|
113
|
+
const b = c > 0 ? -k / c : k / -c;
|
|
114
|
+
if (integral) {
|
|
115
|
+
if (c > 0)
|
|
116
|
+
tighten(j, true, Math.floor(b + eps), i);
|
|
117
|
+
else
|
|
118
|
+
tighten(j, false, Math.ceil(b - eps), i);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const d = strict ? 1e-7 * Math.max(1, Math.abs(b)) : 0;
|
|
122
|
+
if (c > 0)
|
|
123
|
+
tighten(j, true, b - d, i);
|
|
124
|
+
else
|
|
125
|
+
tighten(j, false, b + d, i);
|
|
126
|
+
};
|
|
127
|
+
for (const [i, r] of rows.entries()) {
|
|
128
|
+
const f = formOf(r.lin), k = r.lin['1'] ?? 0;
|
|
129
|
+
if (f === null) {
|
|
130
|
+
if (k > 0 || (!integral && r.strict && k >= 0))
|
|
131
|
+
return { kind: 'conflict', rows: [i] };
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if ('single' in f) {
|
|
135
|
+
bound(f.c, this.varOf(f.single), k, i, !integral && r.strict);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
// g·form + k <= 0
|
|
139
|
+
bound(f.g, this.slackOf(f), k, i, !integral && r.strict);
|
|
140
|
+
}
|
|
141
|
+
for (let j = 0; j < this.val.length; j++)
|
|
142
|
+
if (this.lo[j] > this.hi[j] + eps)
|
|
143
|
+
return { kind: 'conflict', rows: [this.loWhy[j], this.hiWhy[j]].filter(x => x >= 0) };
|
|
144
|
+
// Nonbasic variables into their bounds, then each basic one computed afresh from its row (no drift from the shifts
|
|
145
|
+
// of earlier nodes).
|
|
146
|
+
for (let j = 0; j < this.val.length; j++) {
|
|
147
|
+
if (this.rowOf[j] >= 0)
|
|
148
|
+
continue;
|
|
149
|
+
const x = this.val[j];
|
|
150
|
+
this.val[j] = x < this.lo[j] ? this.lo[j] : x > this.hi[j] ? this.hi[j] : x;
|
|
151
|
+
}
|
|
152
|
+
for (const row of this.rows) {
|
|
153
|
+
let value = 0;
|
|
154
|
+
for (const [j, c] of row.coef)
|
|
155
|
+
value += c * this.val[j];
|
|
156
|
+
this.val[row.basic] = value;
|
|
157
|
+
}
|
|
158
|
+
return { kind: 'sat' };
|
|
159
|
+
}
|
|
160
|
+
shift(j, d) {
|
|
161
|
+
this.val[j] += d;
|
|
162
|
+
for (const r of this.colOf[j]) {
|
|
163
|
+
const row = this.rows[r];
|
|
164
|
+
this.val[row.basic] += row.coef.get(j) * d;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
check(maxPivots = 5_000) {
|
|
168
|
+
for (let n = 0; n < maxPivots; n++) {
|
|
169
|
+
// Bland: the violated basic variable of least index, then the nonbasic one of least index that can move.
|
|
170
|
+
let b = -1;
|
|
171
|
+
for (const row of this.rows) {
|
|
172
|
+
const j = row.basic, x = this.val[j];
|
|
173
|
+
if ((x < this.lo[j] - tol(this.lo[j]) || x > this.hi[j] + tol(this.hi[j])) && (b < 0 || j < b))
|
|
174
|
+
b = j;
|
|
175
|
+
}
|
|
176
|
+
if (b < 0)
|
|
177
|
+
return { kind: 'sat' };
|
|
178
|
+
const row = this.rows[this.rowOf[b]], below = this.val[b] < this.lo[b];
|
|
179
|
+
let e = -1;
|
|
180
|
+
for (const [j, a] of row.coef) {
|
|
181
|
+
if (e >= 0 && j > e)
|
|
182
|
+
continue;
|
|
183
|
+
const up = below === a > 0; // whether x_j must rise
|
|
184
|
+
if (up ? this.val[j] < this.hi[j] - eps : this.val[j] > this.lo[j] + eps)
|
|
185
|
+
e = j;
|
|
186
|
+
}
|
|
187
|
+
if (e < 0) {
|
|
188
|
+
const why = [below ? this.loWhy[b] : this.hiWhy[b]];
|
|
189
|
+
for (const [j, a] of row.coef)
|
|
190
|
+
why.push(below === a > 0 ? this.hiWhy[j] : this.loWhy[j]);
|
|
191
|
+
return { kind: 'conflict', rows: [...new Set(why)].filter(x => x >= 0) };
|
|
192
|
+
}
|
|
193
|
+
const target = below ? this.lo[b] : this.hi[b];
|
|
194
|
+
this.pivot(b, e, target);
|
|
195
|
+
this.pivots++;
|
|
196
|
+
}
|
|
197
|
+
return { kind: 'unknown' };
|
|
198
|
+
}
|
|
199
|
+
// x_b leaves the basis for x_e, x_b set to `target`.
|
|
200
|
+
pivot(b, e, target) {
|
|
201
|
+
const r = this.rowOf[b], row = this.rows[r], a = row.coef.get(e);
|
|
202
|
+
// x_e = (x_b − Σ_{j≠e} c_j x_j) / a
|
|
203
|
+
const theta = (target - this.val[b]) / a;
|
|
204
|
+
this.shift(e, theta); // moves x_b to target and every other basic variable with x_e
|
|
205
|
+
const ecoef = new Map();
|
|
206
|
+
for (const [j, c] of row.coef)
|
|
207
|
+
if (j !== e)
|
|
208
|
+
ecoef.set(j, -c / a);
|
|
209
|
+
ecoef.set(b, 1 / a);
|
|
210
|
+
for (const j of row.coef.keys())
|
|
211
|
+
this.colOf[j].delete(r);
|
|
212
|
+
row.basic = e;
|
|
213
|
+
row.coef = ecoef;
|
|
214
|
+
this.rowOf[e] = r;
|
|
215
|
+
this.rowOf[b] = -1;
|
|
216
|
+
for (const j of ecoef.keys())
|
|
217
|
+
this.colOf[j].add(r);
|
|
218
|
+
// Every other row reading x_e: substitute.
|
|
219
|
+
for (const q of [...this.colOf[e]]) {
|
|
220
|
+
if (q === r)
|
|
221
|
+
continue;
|
|
222
|
+
const other = this.rows[q], c = other.coef.get(e);
|
|
223
|
+
other.coef.delete(e);
|
|
224
|
+
this.colOf[e].delete(q);
|
|
225
|
+
for (const [j, d] of ecoef) {
|
|
226
|
+
const x = (other.coef.get(j) ?? 0) + c * d;
|
|
227
|
+
if (Math.abs(x) < eps) {
|
|
228
|
+
if (other.coef.delete(j))
|
|
229
|
+
this.colOf[j].delete(q);
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
if (!other.coef.has(j))
|
|
233
|
+
this.colOf[j].add(q);
|
|
234
|
+
other.coef.set(j, x);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
this.colOf[e].delete(r);
|
|
239
|
+
for (const j of ecoef.keys())
|
|
240
|
+
this.colOf[j].add(r);
|
|
241
|
+
}
|
|
242
|
+
// Whether a row (lin <= 0) holds at the current point, or undefined where it reads a variable the tableau hasn't met.
|
|
243
|
+
holds(lin) {
|
|
244
|
+
let x = lin['1'] ?? 0;
|
|
245
|
+
for (const key in lin) {
|
|
246
|
+
if (key === '1')
|
|
247
|
+
continue;
|
|
248
|
+
const j = this.index.get(key);
|
|
249
|
+
if (j === undefined)
|
|
250
|
+
return undefined;
|
|
251
|
+
x += lin[key] * this.val[j];
|
|
252
|
+
}
|
|
253
|
+
return x <= tol(x);
|
|
254
|
+
}
|
|
255
|
+
get size() { return this.rows.length; }
|
|
256
|
+
get nnz() { let n = 0; for (const r of this.rows)
|
|
257
|
+
n += r.coef.size; return n; }
|
|
258
|
+
get vars() { return this.val.length; }
|
|
259
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { C, E, Law, Machine, Recurrence } from '../core/machine.js';
|
|
2
2
|
import { type Lin, type Step } from '../core/solver.js';
|
|
3
|
-
import { type Fold, type Goal, type Instances, type Liveness, type Loop, type Path, type StepLaw, type Tree } from '../kernel/kernel.js';
|
|
3
|
+
import { type Fold, type Goal, type Instances, type Liveness, type Loop, type Path, type Recur, type StepLaw, type Tree } from '../kernel/kernel.js';
|
|
4
4
|
import { type Cyclic } from '../kernel/cyclic.js';
|
|
5
5
|
import { leanLibrary } from './library.js';
|
|
6
6
|
export { leanLibrary };
|
|
@@ -33,6 +33,15 @@ export declare function loopToLean(id: string, l: Loop, laws: Law[], rank: E | E
|
|
|
33
33
|
what: string;
|
|
34
34
|
loop: Loop;
|
|
35
35
|
}): string;
|
|
36
|
+
export declare function neverEndsToLean(id: string, l: Loop, r: Recur, tampered?: {
|
|
37
|
+
what: string;
|
|
38
|
+
recur: Recur;
|
|
39
|
+
}[]): string;
|
|
40
|
+
export declare function recurLean(id: string, l: Loop, fallback: Recur): {
|
|
41
|
+
vars: string[];
|
|
42
|
+
loop: string;
|
|
43
|
+
recur: (r: Recur) => string;
|
|
44
|
+
};
|
|
36
45
|
export declare function foldToLean(id: string, f: Fold, laws: Law[], extra?: string): string;
|
|
37
46
|
export declare function readable(id: string, laws: Law[], vars: string[], maps: string[], reach: {
|
|
38
47
|
binders: string;
|
|
@@ -178,6 +187,14 @@ export type LeanItem = {
|
|
|
178
187
|
loop: Loop;
|
|
179
188
|
rank: E | E[];
|
|
180
189
|
laws?: Law[];
|
|
190
|
+
} | {
|
|
191
|
+
id: string;
|
|
192
|
+
neverEnds: Loop;
|
|
193
|
+
recur: Recur;
|
|
194
|
+
tampered?: {
|
|
195
|
+
what: string;
|
|
196
|
+
recur: Recur;
|
|
197
|
+
}[];
|
|
181
198
|
} | {
|
|
182
199
|
id: string;
|
|
183
200
|
fold: Fold;
|
|
@@ -206,6 +223,7 @@ export declare function toLean(items: LeanItem[], opts?: {
|
|
|
206
223
|
native?: boolean;
|
|
207
224
|
cyclic?: boolean;
|
|
208
225
|
}): string;
|
|
226
|
+
export { rationalsToLean } from './qlean.js';
|
|
209
227
|
export declare function leanProject(file: string, opts?: {
|
|
210
228
|
name?: string;
|
|
211
229
|
}): Record<string, string>;
|
package/dist/lean-kernel/emit.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { atMost, n, not, pointWrite } from "../core/machine.js";
|
|
2
2
|
import { add, k, scale, sub } from "../core/solver.js";
|
|
3
|
-
import { boundOf, canon, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, historyMachine, foldObligations, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessRanks, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
|
|
3
|
+
import { boundOf, check, checkRecur, recurGoal, canon, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, historyMachine, foldObligations, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessRanks, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
|
|
4
4
|
import { grow, minimal, proveBuilt, proveLaws, proveStepLaws, withDeadline } from "../kernel/prove.js";
|
|
5
5
|
import { refinement, refinementSides } from "../core/compose.js";
|
|
6
6
|
import { leanSurface, leanTerms } from "./lean-terms.js";
|
|
@@ -226,7 +226,14 @@ export function encode(ex, tamper = false, dropStep = true, globalVars, globalMa
|
|
|
226
226
|
return `.combine ${s.a} ${s.b} ${int(s.ma)} ${int(s.mb)}`;
|
|
227
227
|
}
|
|
228
228
|
const from = replay[s.of];
|
|
229
|
-
|
|
229
|
+
// A cut's quotient is the row it divides (Cert.lean's cutOk: coefs = g · q over the integers). A tree read against a
|
|
230
|
+
// goal it doesn't refute can cut a row g doesn't divide: that is no derivation of this goal, and a quotient written
|
|
231
|
+
// as a fraction isn't Lean (Int has no fractional literal), so it is refused here, never printed.
|
|
232
|
+
const q = Object.fromEntries(Object.entries(from).filter(([key]) => key !== '1').map(([key, v]) => {
|
|
233
|
+
if (!Number.isInteger(v / s.g))
|
|
234
|
+
throw new Error(`${ex.name}: a cut by ${s.g} of a row it doesn't divide (${key}: ${v}); the tree doesn't refute this goal`);
|
|
235
|
+
return [key, v / s.g];
|
|
236
|
+
}));
|
|
230
237
|
const cc = Math.ceil((from['1'] ?? 0) / s.g);
|
|
231
238
|
replay.push({ ...q, 1: cc });
|
|
232
239
|
return `.cut ${s.of} ${int(s.g)} ${sparse(dense(q))} ${int(cc)}`;
|
|
@@ -336,6 +343,61 @@ export function loopToLean(id, l, laws, rank, tamper) {
|
|
|
336
343
|
loopObligations(l, laws, rank);
|
|
337
344
|
return smachineToLean(id, loopMachine(l), laws, { liveness: [loopLiveness(l, rank)], loop: { loop: l, ...(tamper ? { tamper } : {}) } });
|
|
338
345
|
}
|
|
346
|
+
// A loop with a run that never ends, through Kern.never_ends_of (NonTerm.lean): the loop, the recurrent set and its
|
|
347
|
+
// witness, the start and the stem as values, and the closure goal's table and tree; Lean evaluates the stem and builds
|
|
348
|
+
// the goal from the loop and the set (the certificate names no hypothesis, so the tree is read against Lean's goal).
|
|
349
|
+
// Each tampered certificate's check is computed to false.
|
|
350
|
+
export function neverEndsToLean(id, l, r, tampered = []) {
|
|
351
|
+
if (!checkRecur(l, r))
|
|
352
|
+
throw new Error(`${id}: the certificate does not check`);
|
|
353
|
+
const { vars, loop, recur } = recurLean(id, l, r);
|
|
354
|
+
return `-- ${l.name}: a run that never ends (variables ${vars.map((v2, j) => `${j} ${v2}`).join(', ')})
|
|
355
|
+
def ${id}_loop : Loop := ${loop}
|
|
356
|
+
def ${id}_recur : Recur := ${recur(r)}
|
|
357
|
+
/-- ${l.name} never ends: for some params meeting its precondition, a run of turns from the start goes on forever. -/
|
|
358
|
+
theorem ${id}_never_ends : NeverEnds ${id}_loop :=
|
|
359
|
+
never_ends_of ${id}_loop ${id}_recur (by ${checkBy})${tampered.map((t, j) => `
|
|
360
|
+
-- TAMPERED: ${t.what}. Lean computes the check to false.
|
|
361
|
+
def ${id}_tampered${j} : Recur := ${recur(t.recur)}
|
|
362
|
+
example : checkRecur ${id}_loop ${id}_tampered${j} = false := by ${checkBy}`).join('')}`;
|
|
363
|
+
}
|
|
364
|
+
// The loop and a certificate as NonTerm.lean's Loop and Recur (the domain every variable; a certificate whose tree
|
|
365
|
+
// doesn't refute its own goal carries `fallback`'s table and tree, which refutes `fallback`'s). lean-kernel/conformance.ts prints its cases with it.
|
|
366
|
+
export function recurLean(id, l, fallback) {
|
|
367
|
+
const vars = [...l.params, ...l.inputs, ...Object.keys(l.state)];
|
|
368
|
+
const { e, c } = leanTerms(vars, [], id);
|
|
369
|
+
const ix = (name) => { const j = vars.indexOf(name); if (j < 0)
|
|
370
|
+
throw new Error(`${id}: no variable for ${name}`); return j; };
|
|
371
|
+
const loop = `⟨${list(l.inputs.map(name => String(ix(name))))},
|
|
372
|
+
${list(Object.entries(l.state).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
|
|
373
|
+
${list(Object.entries(l.body).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
|
|
374
|
+
${c(l.while)},
|
|
375
|
+
${l.pre ? c(l.pre) : TRUE},
|
|
376
|
+
${l.assume ? c(l.assume) : TRUE}⟩`;
|
|
377
|
+
const vals = (o) => list(Object.entries(o).map(([name, v]) => `(${ix(name)}, ${v})`));
|
|
378
|
+
const recur = (rr) => {
|
|
379
|
+
const enc = (g) => encode({ name: `${id}: ${l.name} never ends`, goal: recurGoal(l, g), tree: g.tree }, false, true, vars, []);
|
|
380
|
+
// The tree is written against the goal it refutes: the certificate's own where kernel.ts's check accepts it there,
|
|
381
|
+
// else `fallback`'s (which must check), and Lean reads it against the goal it builds from `rr` either way. So every
|
|
382
|
+
// number written is one kernel.ts's check replayed: integers, every cut dividing its row.
|
|
383
|
+
const fits = (g) => { try {
|
|
384
|
+
return check(recurGoal(l, g), g.tree);
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
return false;
|
|
388
|
+
} };
|
|
389
|
+
// (neither refuting its goal, as a forged certificate's: a leaf with no step, the tree that refutes nothing)
|
|
390
|
+
const by = fits(rr) ? rr : fits(fallback) ? fallback : undefined;
|
|
391
|
+
const coded = by ? enc(by) : { tab: '[]', tree: '.leafAt [] [] [] []' };
|
|
392
|
+
return `⟨${list(vars.map((_, j) => String(j)))},
|
|
393
|
+
${vals(rr.start)},
|
|
394
|
+
${list(rr.stem.map(vals))},
|
|
395
|
+
${c(rr.set)},
|
|
396
|
+
${list(Object.entries(rr.wit).map(([name, t]) => `(${ix(name)}, ${e(t)})`))},
|
|
397
|
+
(${coded.tab}, [], ${coded.tree})⟩`;
|
|
398
|
+
};
|
|
399
|
+
return { vars, loop, recur };
|
|
400
|
+
}
|
|
339
401
|
// Folds through Kern.fold_sound: Lean builds the leaf and node goals; the trees are the kernel's.
|
|
340
402
|
export function foldToLean(id, f, laws, extra = '') {
|
|
341
403
|
const st = Object.keys(f.leaf);
|
|
@@ -1729,6 +1791,10 @@ function toLeanWith(items, opts) {
|
|
|
1729
1791
|
parts.push(`-- ${item.id}\n${cyc && 'text' in cyc ? cyc.text : loopToLean(id, item.loop, item.laws ?? [], item.rank)}`);
|
|
1730
1792
|
theorems.push(`${id}_halts`);
|
|
1731
1793
|
}
|
|
1794
|
+
else if ('neverEnds' in item) {
|
|
1795
|
+
parts.push(`-- ${item.id}\n${neverEndsToLean(id, item.neverEnds, item.recur, item.tampered ?? [])}`);
|
|
1796
|
+
theorems.push(`${id}_never_ends`);
|
|
1797
|
+
}
|
|
1732
1798
|
else if ('cyclic' in item) {
|
|
1733
1799
|
parts.push(`-- ${item.id}\n${cyclicToLean(id, item.cyclic, item.tampered ?? [], item.after, item.bridge, item.recur, item.fold)}`);
|
|
1734
1800
|
theorems.push(`${id}_sound`, ...(item.bridge?.laws ?? item.fold?.laws ?? []).map((_, j) => `${id}_law${j}_holds`), ...(item.recur ? [`${id}_values`] : []), ...(item.recur ? [...item.recur.lemmas.map((_, j) => `${id}_lemma${j}`), ...item.recur.between.map((_, j) => `${id}_between${j}`)] : []),
|
|
@@ -1755,7 +1821,7 @@ function toLeanWith(items, opts) {
|
|
|
1755
1821
|
}
|
|
1756
1822
|
// The file the items need: Live.lean for a liveness or a loop, Sets.lean (which imports Steps.lean) for any machine.
|
|
1757
1823
|
return `import History
|
|
1758
|
-
${cycTexts ? 'import BridgeRules\n' : ''}${items.some(item => 'cyclic' in item) ? (items.some(item => 'cyclic' in item && (item.bridge || item.recur || item.fold)) ? 'import Bridge\n' : 'import Cyclic\n') : ''}${items.some(item => 'cyclic' in item && item.data) ? 'import Data\n' : ''}import ${items.some(item => 'refinement' in item) ? 'Refine' : items.some(item => 'machine' in item && (item.ghosts?.length || item.laws.some(l => l.some?.length))) ? 'Ghost' : items.some(item => ('machine' in item && item.liveness?.length) || 'loop' in item) ? 'Live' : items.some(item => 'machine' in item) ? 'Sets' : 'Steps'}
|
|
1824
|
+
${items.some(item => 'neverEnds' in item) ? 'import NonTerm\n' : ''}${cycTexts ? 'import BridgeRules\n' : ''}${items.some(item => 'cyclic' in item) ? (items.some(item => 'cyclic' in item && (item.bridge || item.recur || item.fold)) ? 'import Bridge\n' : 'import Cyclic\n') : ''}${items.some(item => 'cyclic' in item && item.data) ? 'import Data\n' : ''}import ${items.some(item => 'refinement' in item) ? 'Refine' : items.some(item => 'machine' in item && (item.ghosts?.length || item.laws.some(l => l.some?.length))) ? 'Ghost' : items.some(item => ('machine' in item && item.liveness?.length) || 'loop' in item) ? 'Live' : items.some(item => 'machine' in item) ? 'Sets' : 'Steps'}
|
|
1759
1825
|
open Kern Cert
|
|
1760
1826
|
set_option maxRecDepth 100000
|
|
1761
1827
|
set_option maxHeartbeats 0
|
|
@@ -1768,6 +1834,8 @@ ${parts.join('\n\n')}
|
|
|
1768
1834
|
${theorems.map(t => `#print axioms ${t}`).join('\n')}
|
|
1769
1835
|
`;
|
|
1770
1836
|
}
|
|
1837
|
+
// Rules over the rationals (core/rationals.ts), each through Rationals.lean's Q.machine_sound: lean-kernel/qlean.ts.
|
|
1838
|
+
export { rationalsToLean } from "./qlean.js";
|
|
1771
1839
|
// A Lake project that builds the twin and the file: `lake build` in its folder checks every proof.
|
|
1772
1840
|
export function leanProject(file, opts = {}) {
|
|
1773
1841
|
const name = opts.name ?? 'Proofs';
|