@bytewaveco/pudding 0.0.0-stage → 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +363 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +143 -0
- package/dist/crosscheck.d.ts +63 -0
- package/dist/crosscheck.js +164 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +72 -0
- package/dist/install.d.ts +25 -0
- package/dist/install.js +192 -0
- package/dist/ir.d.ts +93 -0
- package/dist/ir.js +331 -0
- package/dist/lean.d.ts +56 -0
- package/dist/lean.js +438 -0
- package/dist/report.d.ts +7 -0
- package/dist/report.js +74 -0
- package/dist/runner.d.ts +79 -0
- package/dist/runner.js +672 -0
- package/dist/search.d.ts +24 -0
- package/dist/search.js +137 -0
- package/dist/translate.d.ts +247 -0
- package/dist/translate.js +1938 -0
- package/dist/vitest.d.ts +8 -0
- package/dist/vitest.js +96 -0
- package/package.json +77 -4
package/dist/lean.js
ADDED
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// Print obligations as Lean theorems and run Lean over them.
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { availableParallelism, tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import * as ir from "./ir.js";
|
|
7
|
+
/**
|
|
8
|
+
* The modules the generated theorems need. Importing only these starts Lean
|
|
9
|
+
* several times faster than `import Init`, and they are what a trimmed Lean
|
|
10
|
+
* runtime ships.
|
|
11
|
+
*/
|
|
12
|
+
export const FAST_IMPORTS = [
|
|
13
|
+
"Init.Core", "Init.Omega", "Init.Grind", "Init.Data.List.Basic", "Init.Data.String.Basic", "Init.Data.Int.DivMod", "Init.Data.Int.OfNat",
|
|
14
|
+
];
|
|
15
|
+
/**
|
|
16
|
+
* A slightly larger set for a second attempt. Measured against the whole of
|
|
17
|
+
* `Init` on thousands of generated theorems, the fallback tactic proves
|
|
18
|
+
* exactly as much under these imports.
|
|
19
|
+
*/
|
|
20
|
+
export const FALLBACK_IMPORTS = [...FAST_IMPORTS, "Init.Data.String.Lemmas.Basic", "Init.Data.String.Length"];
|
|
21
|
+
const header = (imports) => ["prelude", ...imports.map(name => `import ${name}`)].join("\n");
|
|
22
|
+
export const FAST_HEADER = header(FAST_IMPORTS);
|
|
23
|
+
export const FALLBACK_HEADER = header(FALLBACK_IMPORTS);
|
|
24
|
+
/** The whole core library, for proofs an author wrote. A trimmed runtime does not have it. */
|
|
25
|
+
export const FULL_HEADER = "import Init";
|
|
26
|
+
export const DEFAULT_TACTIC = "grind";
|
|
27
|
+
const FINITE_TACTIC = "decide +kernel";
|
|
28
|
+
/** Tried, under the fallback imports, on statements the default tactic could not close. */
|
|
29
|
+
const FALLBACK_TACTIC = "first | grind | (intros; simp_all; done) | (intros; simp_all <;> omega)";
|
|
30
|
+
const MAX_TERM_SIZE = 200_000;
|
|
31
|
+
/**
|
|
32
|
+
* The most text one expression may come to. A value fed back into itself in a
|
|
33
|
+
* loop multiplies in size with every round, and Lean's time grows with it:
|
|
34
|
+
* about five seconds at this size, and minutes a few rounds later.
|
|
35
|
+
*/
|
|
36
|
+
const MAX_TERM_TEXT = 120_000;
|
|
37
|
+
/** How long one Lean process may run: this much, plus a share for each theorem in its file. */
|
|
38
|
+
const FILE_TIMEOUT_MS = 30_000;
|
|
39
|
+
const THEOREM_TIMEOUT_MS = 10_000;
|
|
40
|
+
const MAX_SPECIALIZATIONS = 512;
|
|
41
|
+
/**
|
|
42
|
+
* Why an author's proof script cannot be used, if it cannot. The script is
|
|
43
|
+
* placed inside parentheses in the theorem, and that only confines it if every
|
|
44
|
+
* parenthesis in its text is one Lean sees. So the things that can hide a
|
|
45
|
+
* parenthesis from Lean (comments, string and character literals, quoted
|
|
46
|
+
* names) are not allowed, and the parentheses must balance.
|
|
47
|
+
*/
|
|
48
|
+
export function scriptProblem(script) {
|
|
49
|
+
if (/--|\/-|"|«/.test(script))
|
|
50
|
+
return "a proof script may not contain comments, string literals or «quoted names»";
|
|
51
|
+
if (/'[()\[\]{}⟨⟩\\]/.test(script))
|
|
52
|
+
return "a proof script may not contain character literals";
|
|
53
|
+
let depth = 0;
|
|
54
|
+
for (const char of script) {
|
|
55
|
+
if (char === "(")
|
|
56
|
+
depth++;
|
|
57
|
+
else if (char === ")" && --depth < 0)
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
return depth === 0 ? undefined : "the parentheses in the proof script do not balance";
|
|
61
|
+
}
|
|
62
|
+
/** How a statement is proved: by evaluating every case, by the default automation, or by the author's script. */
|
|
63
|
+
export function proofKind(statement) {
|
|
64
|
+
if (statement.endsWith(` := by ${FINITE_TACTIC}`))
|
|
65
|
+
return "finite";
|
|
66
|
+
return statement.endsWith(` := by ${DEFAULT_TACTIC}`) || statement.endsWith(` := by ${FALLBACK_TACTIC}`) ? "automatic" : "authored";
|
|
67
|
+
}
|
|
68
|
+
/** The same statement with the stronger, slower automation. Only meaningful for an automatic statement. */
|
|
69
|
+
export function withFallback(statement) {
|
|
70
|
+
return statement.slice(0, -DEFAULT_TACTIC.length) + FALLBACK_TACTIC;
|
|
71
|
+
}
|
|
72
|
+
export class TooLarge extends Error {
|
|
73
|
+
}
|
|
74
|
+
function leanString(text) {
|
|
75
|
+
let out = '"';
|
|
76
|
+
for (const char of text) {
|
|
77
|
+
const code = char.codePointAt(0);
|
|
78
|
+
if (char === '"' || char === "\\")
|
|
79
|
+
out += "\\" + char;
|
|
80
|
+
else if (code >= 0x20 && code < 0x7f)
|
|
81
|
+
out += char;
|
|
82
|
+
else if (code < 0x10000)
|
|
83
|
+
out += "\\u" + code.toString(16).padStart(4, "0");
|
|
84
|
+
else
|
|
85
|
+
out += char;
|
|
86
|
+
}
|
|
87
|
+
return out + '"';
|
|
88
|
+
}
|
|
89
|
+
function leanScalar(value) {
|
|
90
|
+
if (typeof value === "boolean")
|
|
91
|
+
return String(value);
|
|
92
|
+
if (typeof value === "bigint")
|
|
93
|
+
return `(${value} : Int)`;
|
|
94
|
+
return leanString(value);
|
|
95
|
+
}
|
|
96
|
+
function print(e, memo) {
|
|
97
|
+
if (e.t === "var")
|
|
98
|
+
return e.name;
|
|
99
|
+
if (e.t === "lit")
|
|
100
|
+
return leanScalar(e.value);
|
|
101
|
+
const known = memo.get(e);
|
|
102
|
+
if (known !== undefined)
|
|
103
|
+
return known;
|
|
104
|
+
const [a, b, c] = e.args.map(arg => print(arg, memo));
|
|
105
|
+
let text;
|
|
106
|
+
switch (e.op) {
|
|
107
|
+
case "not":
|
|
108
|
+
text = `(!${a})`;
|
|
109
|
+
break;
|
|
110
|
+
case "and":
|
|
111
|
+
text = `(${a} && ${b})`;
|
|
112
|
+
break;
|
|
113
|
+
case "or":
|
|
114
|
+
text = `(${a} || ${b})`;
|
|
115
|
+
break;
|
|
116
|
+
case "eq":
|
|
117
|
+
text = `(${a} == ${b})`;
|
|
118
|
+
break;
|
|
119
|
+
case "lt":
|
|
120
|
+
text = `(decide (${a} < ${b}))`;
|
|
121
|
+
break;
|
|
122
|
+
case "le":
|
|
123
|
+
text = `(decide (${a} ≤ ${b}))`;
|
|
124
|
+
break;
|
|
125
|
+
case "add":
|
|
126
|
+
text = `(${a} + ${b})`;
|
|
127
|
+
break;
|
|
128
|
+
case "sub":
|
|
129
|
+
text = `(${a} - ${b})`;
|
|
130
|
+
break;
|
|
131
|
+
case "mul":
|
|
132
|
+
text = `(${a} * ${b})`;
|
|
133
|
+
break;
|
|
134
|
+
case "neg":
|
|
135
|
+
text = `(-${a})`;
|
|
136
|
+
break;
|
|
137
|
+
case "concat":
|
|
138
|
+
text = `(${a} ++ ${b})`;
|
|
139
|
+
break;
|
|
140
|
+
case "str":
|
|
141
|
+
text = `(toString ${a})`;
|
|
142
|
+
break;
|
|
143
|
+
case "ite":
|
|
144
|
+
text = `(if ${a} then ${b} else ${c})`;
|
|
145
|
+
break;
|
|
146
|
+
case "fdiv": {
|
|
147
|
+
// Lean's `/` on integers rounds toward minus infinity when the divisor
|
|
148
|
+
// is positive, so a literal divisor needs no case split.
|
|
149
|
+
const divisor = e.args[1];
|
|
150
|
+
if (divisor.t === "lit" && divisor.value > 0n)
|
|
151
|
+
text = `(${a} / ${divisor.value})`;
|
|
152
|
+
else if (divisor.t === "lit" && divisor.value < 0n)
|
|
153
|
+
text = `((-${a}) / ${-divisor.value})`;
|
|
154
|
+
else
|
|
155
|
+
text = `(Int.fdiv ${a} ${b})`;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
case "div":
|
|
159
|
+
case "mod": {
|
|
160
|
+
// BigInt division truncates toward zero and the remainder takes the sign
|
|
161
|
+
// of the dividend. With a literal divisor both are written with Lean's
|
|
162
|
+
// `/` and `%` on non-negative operands, which the arithmetic tactics
|
|
163
|
+
// understand. A zero divisor is excluded by a separate obligation.
|
|
164
|
+
const divisor = e.args[1];
|
|
165
|
+
const symbol = e.op === "div" ? "/" : "%";
|
|
166
|
+
if (divisor.t === "lit" && divisor.value !== 0n) {
|
|
167
|
+
const magnitude = divisor.value < 0n ? -divisor.value : divisor.value;
|
|
168
|
+
const flip = e.op === "div" && divisor.value < 0n;
|
|
169
|
+
const body = `(if (0 : Int) ≤ ${a} then ${a} ${symbol} ${magnitude} else -((-${a}) ${symbol} ${magnitude}))`;
|
|
170
|
+
text = flip ? `(-${body})` : body;
|
|
171
|
+
}
|
|
172
|
+
else
|
|
173
|
+
text = `(Int.${e.op === "div" ? "tdiv" : "tmod"} ${a} ${b})`;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
memo.set(e, text);
|
|
178
|
+
return text;
|
|
179
|
+
}
|
|
180
|
+
function printExpr(e) {
|
|
181
|
+
if (ir.treeSize(e) > MAX_TERM_SIZE)
|
|
182
|
+
throw new TooLarge("the translated expression is too large to check");
|
|
183
|
+
const text = print(e, new Map());
|
|
184
|
+
if (text.length > MAX_TERM_TEXT)
|
|
185
|
+
throw new TooLarge("the translated expression is too large to check");
|
|
186
|
+
return text;
|
|
187
|
+
}
|
|
188
|
+
function leanType(ty) {
|
|
189
|
+
return { bool: "Bool", int: "Int", str: "String" }[ty];
|
|
190
|
+
}
|
|
191
|
+
/** The proposition `guard → claim` as a single boolean. */
|
|
192
|
+
function implication(guard, claim) {
|
|
193
|
+
return ir.or(ir.not(guard), claim);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Theorems that together state `∀ binders, guard → claim`.
|
|
197
|
+
*
|
|
198
|
+
* When every input is finite there is one theorem, decided by evaluating all
|
|
199
|
+
* cases in Lean's kernel. Otherwise the finite inputs are specialized away and
|
|
200
|
+
* each remaining theorem quantifies over the unbounded ones.
|
|
201
|
+
*/
|
|
202
|
+
/** The bounds of a finite integer input whose values are consecutive, when it has more than a couple. */
|
|
203
|
+
function consecutive(binder) {
|
|
204
|
+
const values = binder.domain;
|
|
205
|
+
if (binder.ty !== "int" || !values || values.length < 3)
|
|
206
|
+
return undefined;
|
|
207
|
+
const sorted = values.toSorted((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
208
|
+
return sorted.every((value, i) => i === 0 || value === sorted[i - 1] + 1n) ? [sorted[0], sorted.at(-1)] : undefined;
|
|
209
|
+
}
|
|
210
|
+
/** Whether a variable is divided by, or multiplied with something that is not a constant. */
|
|
211
|
+
function usedNonlinearly(e, name, seen = new Set()) {
|
|
212
|
+
if (e.t !== "op" || seen.has(e))
|
|
213
|
+
return false;
|
|
214
|
+
seen.add(e);
|
|
215
|
+
const mentions = (part) => ir.freeVariables(part).has(name);
|
|
216
|
+
const [a, b] = e.args;
|
|
217
|
+
if ((e.op === "div" || e.op === "mod" || e.op === "fdiv") && mentions(b))
|
|
218
|
+
return true;
|
|
219
|
+
if (e.op === "mul" && a.t !== "lit" && b.t !== "lit" && (mentions(a) || mentions(b)))
|
|
220
|
+
return true;
|
|
221
|
+
return e.args.some(arg => usedNonlinearly(arg, name, seen));
|
|
222
|
+
}
|
|
223
|
+
export function checksFor(binders, guard, claim, tactic) {
|
|
224
|
+
const body = implication(guard, claim);
|
|
225
|
+
const free = ir.freeVariables(body);
|
|
226
|
+
const used = binders.filter(b => free.has(b.name));
|
|
227
|
+
if (used.every(b => b.domain)) {
|
|
228
|
+
const cases = used.reduce((n, b) => n * b.domain.length, 1);
|
|
229
|
+
let text = printExpr(body);
|
|
230
|
+
for (const binder of [...used].reverse()) {
|
|
231
|
+
text = `([${binder.domain.map(leanScalar).join(", ")}].all fun ${binder.name} => ${text})`;
|
|
232
|
+
}
|
|
233
|
+
return [{ statement: ` : ${text} = true := by ${FINITE_TACTIC}`, cases }];
|
|
234
|
+
}
|
|
235
|
+
// An integer input over a run of consecutive values does not have to be
|
|
236
|
+
// split into cases: its bounds can be stated instead, which the arithmetic
|
|
237
|
+
// tactics use directly. Splitting is kept where the input is multiplied or
|
|
238
|
+
// divided by, because fixing its value is what makes that arithmetic linear.
|
|
239
|
+
const bounded = new Map();
|
|
240
|
+
for (const binder of used) {
|
|
241
|
+
const range = consecutive(binder);
|
|
242
|
+
if (range && !usedNonlinearly(body, binder.name))
|
|
243
|
+
bounded.set(binder.name, range);
|
|
244
|
+
}
|
|
245
|
+
const finite = used.filter(b => b.domain && !bounded.has(b.name));
|
|
246
|
+
const symbolic = used.filter(b => !b.domain || bounded.has(b.name));
|
|
247
|
+
for (const [name, [low, high]] of bounded) {
|
|
248
|
+
const input = ir.variable(name, "int");
|
|
249
|
+
guard = ir.and(ir.and(ir.le(ir.lit(low), input), ir.le(input, ir.lit(high))), guard);
|
|
250
|
+
}
|
|
251
|
+
const combinations = finite.reduce((n, b) => n * b.domain.length, 1);
|
|
252
|
+
if (combinations > MAX_SPECIALIZATIONS) {
|
|
253
|
+
throw new TooLarge(`the finite inputs have ${combinations} combinations alongside unbounded ones; the limit is ${MAX_SPECIALIZATIONS}`);
|
|
254
|
+
}
|
|
255
|
+
// An author's script goes inside parentheses, so that it has to be a complete
|
|
256
|
+
// tactic sequence and nothing after it can be read as part of it.
|
|
257
|
+
const script = tactic === undefined ? DEFAULT_TACTIC : `(\n ${tactic.trim().split("\n").join("\n ")}\n)`;
|
|
258
|
+
const checks = [];
|
|
259
|
+
const visit = (index, values) => {
|
|
260
|
+
if (index < finite.length) {
|
|
261
|
+
for (const value of finite[index].domain)
|
|
262
|
+
visit(index + 1, new Map(values).set(finite[index].name, ir.lit(value)));
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const g = ir.substitute(guard, values);
|
|
266
|
+
const c = ir.substitute(claim, values);
|
|
267
|
+
if (implication(g, c) === ir.TRUE)
|
|
268
|
+
return;
|
|
269
|
+
const remaining = ir.freeVariables(implication(g, c));
|
|
270
|
+
const parameters = symbolic.filter(b => remaining.has(b.name)).map(b => ` (${b.name} : ${leanType(b.ty)})`).join("");
|
|
271
|
+
const proposition = g === ir.TRUE ? `${printExpr(c)} = true` : `${printExpr(g)} = true → ${printExpr(c)} = true`;
|
|
272
|
+
checks.push({ statement: `${parameters} : ${proposition} := by ${script}`, cases: 0 });
|
|
273
|
+
};
|
|
274
|
+
visit(0, new Map());
|
|
275
|
+
return checks;
|
|
276
|
+
}
|
|
277
|
+
/** Lean was stopped because it ran past its time limit. */
|
|
278
|
+
class LeanTimeout extends Error {
|
|
279
|
+
}
|
|
280
|
+
function runFile(lean, source, directory, name, timeout) {
|
|
281
|
+
return new Promise((resolve, reject) => {
|
|
282
|
+
const file = join(directory, name);
|
|
283
|
+
writeFile(file, source).then(() => {
|
|
284
|
+
// Lean stops reporting after 100 errors unless told otherwise, and a theorem it never reported on must not pass for proved.
|
|
285
|
+
execFile(lean, ["-DmaxErrors=0", "--json", file], { maxBuffer: 1 << 28, timeout, killSignal: "SIGKILL" }, (error, stdout, stderr) => {
|
|
286
|
+
if (error?.killed)
|
|
287
|
+
return reject(new LeanTimeout());
|
|
288
|
+
if (error && typeof error.code === "string") {
|
|
289
|
+
return reject(new Error(`could not run Lean at ${lean}: ${error.message}`));
|
|
290
|
+
}
|
|
291
|
+
const messages = [];
|
|
292
|
+
for (const line of stdout.split("\n")) {
|
|
293
|
+
if (!line.startsWith("{"))
|
|
294
|
+
continue;
|
|
295
|
+
try {
|
|
296
|
+
messages.push(JSON.parse(line));
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
// Not a diagnostic line.
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (error && !messages.some(m => m.severity === "error")) {
|
|
303
|
+
return reject(new Error(`Lean exited abnormally: ${(stderr || stdout).trim().slice(0, 2000)}`));
|
|
304
|
+
}
|
|
305
|
+
resolve(messages);
|
|
306
|
+
});
|
|
307
|
+
}, reject);
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
/** The axioms of Lean's own logic. A proof that rests on anything else, `sorry` above all, is not a proof. */
|
|
311
|
+
const ACCEPTED_AXIOMS = new Set(["propext", "Classical.choice", "Quot.sound"]);
|
|
312
|
+
async function runChunk(lean, header, statements, directory, name, strict, timeout) {
|
|
313
|
+
// Each theorem is followed by a question about it: which axioms does it
|
|
314
|
+
// rest on? A theorem counts as proved only when Lean reports no error for
|
|
315
|
+
// it *and* answers that question acceptably. Silence is never taken for
|
|
316
|
+
// success, and neither is an unfinished proof, which Lean only warns about.
|
|
317
|
+
const lines = header.split("\n");
|
|
318
|
+
const firstLine = [];
|
|
319
|
+
const askedAt = [];
|
|
320
|
+
for (const [i, statement] of statements.entries()) {
|
|
321
|
+
firstLine.push(lines.length + 1);
|
|
322
|
+
lines.push(...`theorem c${i}${statement}`.split("\n"));
|
|
323
|
+
askedAt.push(lines.length + 1);
|
|
324
|
+
lines.push(`#print axioms c${i}`);
|
|
325
|
+
}
|
|
326
|
+
// The file ends with a command that always answers, to show that Lean reached the end.
|
|
327
|
+
const sentinel = lines.length + 1;
|
|
328
|
+
lines.push("#check Nat");
|
|
329
|
+
const messages = await runFile(lean, lines.join("\n") + "\n", directory, name, timeout);
|
|
330
|
+
if (!messages.some(message => message.severity === "information" && message.pos?.line === sentinel)) {
|
|
331
|
+
throw new Error("Lean stopped before it had checked every theorem, so no result can be trusted");
|
|
332
|
+
}
|
|
333
|
+
const outcomes = statements.map(() => ({ ok: false, message: "" }));
|
|
334
|
+
const failed = new Set();
|
|
335
|
+
const reject = (index, text) => {
|
|
336
|
+
const outcome = outcomes[index];
|
|
337
|
+
outcome.message = outcome.message ? outcome.message + "\n" + text : text;
|
|
338
|
+
failed.add(index);
|
|
339
|
+
};
|
|
340
|
+
for (const message of messages) {
|
|
341
|
+
const line = message.pos?.line ?? 0;
|
|
342
|
+
let index = -1;
|
|
343
|
+
for (let i = 0; i < firstLine.length && firstLine[i] <= line; i++)
|
|
344
|
+
index = i;
|
|
345
|
+
if (message.severity === "error") {
|
|
346
|
+
// An error after the last theorem means something in the file disturbed what follows it. Trust none of it.
|
|
347
|
+
if (line >= sentinel)
|
|
348
|
+
return statements.map(() => ({ ok: false, message: `Lean reported an error after the theorems: ${message.data}` }));
|
|
349
|
+
if (index < 0)
|
|
350
|
+
throw new Error(`Lean rejected the generated preamble: ${message.data}`);
|
|
351
|
+
// An error on the question itself only repeats that the theorem above it failed.
|
|
352
|
+
if (line !== askedAt[index] || !failed.has(index))
|
|
353
|
+
reject(index, message.data);
|
|
354
|
+
}
|
|
355
|
+
else if (index >= 0 && line === askedAt[index] && message.severity === "information") {
|
|
356
|
+
const listed = /depends on axioms: \[([^\]]*)\]/.exec(message.data);
|
|
357
|
+
const foreign = listed ? listed[1].split(",").map(axiom => axiom.trim()).filter(axiom => axiom && !ACCEPTED_AXIOMS.has(axiom)) : [];
|
|
358
|
+
if (foreign.includes("sorryAx")) {
|
|
359
|
+
if (!failed.has(index))
|
|
360
|
+
reject(index, "the proof is unfinished: it uses `sorry`");
|
|
361
|
+
}
|
|
362
|
+
else if (foreign.length)
|
|
363
|
+
reject(index, `the proof rests on ${foreign.map(axiom => `\`${axiom}\``).join(", ")}, which Pudding does not accept`);
|
|
364
|
+
else if (listed || /does not depend on any axioms/.test(message.data))
|
|
365
|
+
outcomes[index].ok = true;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
for (const index of failed)
|
|
369
|
+
outcomes[index].ok = false;
|
|
370
|
+
// A file holding an author's script must be clean throughout: an error anywhere means Lean read something as more than a proof.
|
|
371
|
+
if (strict && messages.some(message => message.severity === "error"))
|
|
372
|
+
for (const outcome of outcomes)
|
|
373
|
+
outcome.ok = false;
|
|
374
|
+
for (const outcome of outcomes)
|
|
375
|
+
if (!outcome.ok && !outcome.message)
|
|
376
|
+
outcome.message = "Lean did not confirm this theorem";
|
|
377
|
+
return outcomes;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Check each statement, in parallel across processes when there are many.
|
|
381
|
+
*
|
|
382
|
+
* Statements Pudding generated share files. A statement that carries an
|
|
383
|
+
* author's proof script gets a file of its own (`alone`), with any error in
|
|
384
|
+
* that file counting against it: Lean recovers from a syntax error by reading
|
|
385
|
+
* on from the next command, so text in a script could otherwise define things
|
|
386
|
+
* that change what the theorems after it mean.
|
|
387
|
+
*/
|
|
388
|
+
export async function prove(lean, header, statements, processes = availableParallelism(), alone = false) {
|
|
389
|
+
if (!statements.length)
|
|
390
|
+
return [];
|
|
391
|
+
const directory = await mkdtemp(join(tmpdir(), "pudding-"));
|
|
392
|
+
try {
|
|
393
|
+
const chunks = [];
|
|
394
|
+
if (alone)
|
|
395
|
+
for (const statement of statements)
|
|
396
|
+
chunks.push([statement]);
|
|
397
|
+
else {
|
|
398
|
+
// Starting Lean costs little next to proving, so spread the work widely.
|
|
399
|
+
const workers = Math.max(1, Math.min(processes, Math.ceil(statements.length / 8)));
|
|
400
|
+
const size = Math.ceil(statements.length / workers);
|
|
401
|
+
for (let i = 0; i < statements.length; i += size)
|
|
402
|
+
chunks.push(statements.slice(i, i + size));
|
|
403
|
+
}
|
|
404
|
+
// Lean bounds its tactics by work done, not by the clock, and reading a
|
|
405
|
+
// huge theorem is not bounded at all. A file that runs past its time is
|
|
406
|
+
// tried again one theorem at a time, so that only the slow one goes unproved.
|
|
407
|
+
const attempt = async (chunk, file) => {
|
|
408
|
+
const timeout = FILE_TIMEOUT_MS + THEOREM_TIMEOUT_MS * chunk.length;
|
|
409
|
+
try {
|
|
410
|
+
return await runChunk(lean, header, chunk, directory, file, alone, timeout);
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
if (!(error instanceof LeanTimeout))
|
|
414
|
+
throw error;
|
|
415
|
+
if (chunk.length === 1)
|
|
416
|
+
return [{ ok: false, message: `Lean did not finish within ${timeout / 1000} seconds` }];
|
|
417
|
+
const outcomes = [];
|
|
418
|
+
for (const [n, statement] of chunk.entries())
|
|
419
|
+
outcomes.push(...await attempt([statement], `${file.replace(/\.lean$/, "")}_${n}.lean`));
|
|
420
|
+
return outcomes;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
const results = [];
|
|
424
|
+
// At most one process per core at a time.
|
|
425
|
+
for (let i = 0; i < chunks.length; i += Math.max(1, processes)) {
|
|
426
|
+
const wave = chunks.slice(i, i + Math.max(1, processes));
|
|
427
|
+
results.push(...await Promise.all(wave.map((chunk, n) => attempt(chunk, `Proof${i + n}.lean`))));
|
|
428
|
+
}
|
|
429
|
+
return results.flat();
|
|
430
|
+
}
|
|
431
|
+
finally {
|
|
432
|
+
await rm(directory, { recursive: true, force: true });
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** The Lean source for a set of statements, for inspection. */
|
|
436
|
+
export function render(header, statements) {
|
|
437
|
+
return header + "\n\n" + statements.map((statement, i) => `theorem c${i}${statement}`).join("\n\n") + "\n";
|
|
438
|
+
}
|
package/dist/report.d.ts
ADDED
package/dist/report.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Render a report as text for the terminal or for GitHub Actions.
|
|
2
|
+
import { displayPath } from "./runner.js";
|
|
3
|
+
const MARK = { proved: "✓", checked: "~", sampled: "~", failed: "✗", skipped: "↓", todo: "□" };
|
|
4
|
+
const COLOR = { proved: 32, checked: 33, sampled: 33, failed: 31, skipped: 2, todo: 2 };
|
|
5
|
+
const LABEL = {
|
|
6
|
+
proved: "proved", checked: "checked only", sampled: "sampled only", failed: "failed", skipped: "skipped", todo: "todo",
|
|
7
|
+
};
|
|
8
|
+
const ORDER = ["proved", "checked", "sampled", "failed", "skipped", "todo"];
|
|
9
|
+
export function format(report, options) {
|
|
10
|
+
if (options.reporter === "json")
|
|
11
|
+
return JSON.stringify(report, null, 2) + "\n";
|
|
12
|
+
const paint = (code, text) => (options.color ? `\x1b[${code}m${text}\x1b[0m` : text);
|
|
13
|
+
const dim = (text) => paint(2, text);
|
|
14
|
+
const path = (file) => displayPath(options.cwd, file);
|
|
15
|
+
const verbose = options.reporter === "verbose";
|
|
16
|
+
const lines = [];
|
|
17
|
+
const finding = (f) => {
|
|
18
|
+
lines.push(` ${paint(31, f.message)}`);
|
|
19
|
+
lines.push(dim(` ${path(f.file)}:${f.line}:${f.column}${f.text ? ` ${f.text}` : ""}`));
|
|
20
|
+
if (f.inputs && Object.keys(f.inputs).length) {
|
|
21
|
+
lines.push(` with ${Object.entries(f.inputs).map(([name, value]) => `${name} = ${value}`).join(", ")}`);
|
|
22
|
+
}
|
|
23
|
+
if (f.detail)
|
|
24
|
+
for (const line of f.detail.split("\n"))
|
|
25
|
+
lines.push(dim(` ${line}`));
|
|
26
|
+
};
|
|
27
|
+
const law = (entry) => {
|
|
28
|
+
const suffix = entry.status === "checked" ? dim(" executed for every input, not proved")
|
|
29
|
+
: entry.status === "sampled" ? dim(" executed on sampled inputs, not proved") : "";
|
|
30
|
+
lines.push(` ${paint(COLOR[entry.status], MARK[entry.status])} ${entry.status === "skipped" || entry.status === "todo" ? dim(entry.name) : entry.name}${suffix}`);
|
|
31
|
+
entry.findings.forEach(finding);
|
|
32
|
+
for (const note of entry.notes)
|
|
33
|
+
lines.push(dim(` note: ${note}`));
|
|
34
|
+
};
|
|
35
|
+
const files = [...new Set(report.laws.map(entry => entry.file))];
|
|
36
|
+
for (const file of files) {
|
|
37
|
+
const laws = report.laws.filter(entry => entry.file === file);
|
|
38
|
+
const failed = laws.some(entry => entry.status === "failed");
|
|
39
|
+
const ran = laws.filter(entry => entry.status !== "skipped" && entry.status !== "todo");
|
|
40
|
+
const quiet = (entry) => !verbose && (entry.status === "skipped" || entry.status === "todo" || (entry.status === "proved" && !entry.notes.length));
|
|
41
|
+
const mark = failed ? paint(31, "✗") : ran.length ? paint(32, "✓") : dim("↓");
|
|
42
|
+
lines.push(` ${mark} ${path(file)} ${dim(`(${laws.length} ${laws.length === 1 ? "law" : "laws"})`)}`);
|
|
43
|
+
for (const entry of laws)
|
|
44
|
+
if (!quiet(entry))
|
|
45
|
+
law(entry);
|
|
46
|
+
}
|
|
47
|
+
if (files.length)
|
|
48
|
+
lines.push("");
|
|
49
|
+
for (const error of report.errors)
|
|
50
|
+
lines.push(` ${paint(31, "error")} ${error}`);
|
|
51
|
+
for (const warning of report.warnings)
|
|
52
|
+
lines.push(` ${paint(33, "warning")} ${warning}`);
|
|
53
|
+
if (report.errors.length || report.warnings.length)
|
|
54
|
+
lines.push("");
|
|
55
|
+
const counts = ORDER.filter(status => report.counts[status]).map(status => paint(COLOR[status], `${report.counts[status]} ${LABEL[status]}`));
|
|
56
|
+
const version = /version ([\d.]+[^\s,]*)/.exec(report.lean)?.[1] ?? "unknown";
|
|
57
|
+
lines.push(` Laws ${counts.join(dim(", ")) || "none"}`);
|
|
58
|
+
lines.push(` Proofs ${report.obligations} ${report.obligations === 1 ? "theorem" : "theorems"}${report.cachedObligations ? ` (${report.cachedObligations} cached)` : ""}, ${report.cases.toLocaleString("en-US")} finite ${report.cases === 1 ? "case" : "cases"}, Lean ${version}`);
|
|
59
|
+
lines.push(` Time ${report.durationMs} ms`);
|
|
60
|
+
if (options.reporter === "github") {
|
|
61
|
+
// Workflow commands turn into annotations on the lines concerned.
|
|
62
|
+
const escape = (text) => text.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
63
|
+
const property = (text) => escape(text).replace(/:/g, "%3A").replace(/,/g, "%2C");
|
|
64
|
+
for (const entry of report.laws) {
|
|
65
|
+
for (const f of entry.findings) {
|
|
66
|
+
const inputs = f.inputs && Object.keys(f.inputs).length ? ` with ${Object.entries(f.inputs).map(([name, value]) => `${name} = ${value}`).join(", ")}` : "";
|
|
67
|
+
lines.push(`::error file=${property(path(f.file))},line=${f.line},col=${f.column},title=${property(entry.name)}::${escape(f.message + inputs)}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
for (const error of report.errors)
|
|
71
|
+
lines.push(`::error::${escape(error)}`);
|
|
72
|
+
}
|
|
73
|
+
return lines.join("\n") + "\n";
|
|
74
|
+
}
|
package/dist/runner.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Config } from "./index.ts";
|
|
2
|
+
export declare const MINIMUM_LEAN: readonly [4, 36, 0];
|
|
3
|
+
export interface RunOptions extends Config {
|
|
4
|
+
/** Project directory. Default: the current directory. */
|
|
5
|
+
cwd?: string;
|
|
6
|
+
/** Only check law files whose path contains one of these. */
|
|
7
|
+
files?: string[];
|
|
8
|
+
/** Check exactly these files, whatever the configuration includes. */
|
|
9
|
+
lawFiles?: string[];
|
|
10
|
+
/** Only check laws whose full name matches this regular expression. */
|
|
11
|
+
name?: string;
|
|
12
|
+
/** Write the generated Lean source to this file. */
|
|
13
|
+
emit?: string;
|
|
14
|
+
}
|
|
15
|
+
export type Status = "proved" | "checked" | "sampled" | "failed" | "skipped" | "todo";
|
|
16
|
+
export interface Finding {
|
|
17
|
+
kind: "counterexample" | "unproved" | "vacuous" | "throws" | "inexact" | "mismatch" | "unsupported" | "invalid";
|
|
18
|
+
message: string;
|
|
19
|
+
file: string;
|
|
20
|
+
line: number;
|
|
21
|
+
column: number;
|
|
22
|
+
/** Source text of the statement concerned. */
|
|
23
|
+
text?: string;
|
|
24
|
+
/** The inputs that show the problem, by the name they have in the law. */
|
|
25
|
+
inputs?: Record<string, string>;
|
|
26
|
+
/** Extra lines, such as the two sides of a failed equality or Lean's output. */
|
|
27
|
+
detail?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface LawReport {
|
|
30
|
+
name: string;
|
|
31
|
+
file: string;
|
|
32
|
+
line: number;
|
|
33
|
+
column: number;
|
|
34
|
+
status: Status;
|
|
35
|
+
findings: Finding[];
|
|
36
|
+
notes: string[];
|
|
37
|
+
obligations: number;
|
|
38
|
+
cases: number;
|
|
39
|
+
}
|
|
40
|
+
export interface Report {
|
|
41
|
+
ok: boolean;
|
|
42
|
+
laws: LawReport[];
|
|
43
|
+
/** Problems that prevented checking, such as TypeScript errors. */
|
|
44
|
+
errors: string[];
|
|
45
|
+
/** Things that weakened the run without failing it, such as a law file that could not be executed. */
|
|
46
|
+
warnings: string[];
|
|
47
|
+
counts: Record<Status, number>;
|
|
48
|
+
obligations: number;
|
|
49
|
+
cachedObligations: number;
|
|
50
|
+
cases: number;
|
|
51
|
+
durationMs: number;
|
|
52
|
+
/** Theorems the default automation could not close under the fast header. */
|
|
53
|
+
retried: number;
|
|
54
|
+
/** Milliseconds spent in each phase. */
|
|
55
|
+
timings: {
|
|
56
|
+
typescript: number;
|
|
57
|
+
prove: number;
|
|
58
|
+
retry: number;
|
|
59
|
+
crossCheck: number;
|
|
60
|
+
};
|
|
61
|
+
/** The reporter the project's configuration asks for, if any. */
|
|
62
|
+
reporter?: Config["reporter"];
|
|
63
|
+
lean: string;
|
|
64
|
+
}
|
|
65
|
+
/** A path as shown to the user: relative inside the project, absolute outside it. */
|
|
66
|
+
export declare function displayPath(cwd: string, file: string): string;
|
|
67
|
+
/** The name law files import from, read from this package so a rename needs no code change. */
|
|
68
|
+
export declare const packageName: string;
|
|
69
|
+
/** Raised when no Lean binary can be found, so the CLI can offer to install one. */
|
|
70
|
+
export declare class LeanMissing extends Error {
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find the Lean binary. In order: the configuration, `PUDDING_LEAN`, the
|
|
74
|
+
* runtime package for this platform, a `.toolchain` directory in the project
|
|
75
|
+
* or in this package, the machine-wide cache `install-lean` fills, elan, and
|
|
76
|
+
* finally `lean` on the PATH.
|
|
77
|
+
*/
|
|
78
|
+
export declare function locateLean(config: Config, cwd: string): string;
|
|
79
|
+
export declare function run(options?: RunOptions): Promise<Report>;
|