@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/ir.js
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// The intermediate representation: first-order expressions over three scalar
|
|
2
|
+
// types. Everything structured (records, arrays, closures) is resolved by the
|
|
3
|
+
// translator before it reaches this level.
|
|
4
|
+
export const TRUE = { t: "lit", ty: "bool", value: true };
|
|
5
|
+
export const FALSE = { t: "lit", ty: "bool", value: false };
|
|
6
|
+
export function lit(value) {
|
|
7
|
+
if (value === true)
|
|
8
|
+
return TRUE;
|
|
9
|
+
if (value === false)
|
|
10
|
+
return FALSE;
|
|
11
|
+
return { t: "lit", ty: typeof value === "bigint" ? "int" : "str", value };
|
|
12
|
+
}
|
|
13
|
+
export function variable(name, ty) {
|
|
14
|
+
return { t: "var", ty, name };
|
|
15
|
+
}
|
|
16
|
+
function isLit(e) {
|
|
17
|
+
return e.t === "lit";
|
|
18
|
+
}
|
|
19
|
+
export function same(a, b) {
|
|
20
|
+
if (a === b)
|
|
21
|
+
return true;
|
|
22
|
+
if (a.t !== b.t || a.ty !== b.ty)
|
|
23
|
+
return false;
|
|
24
|
+
if (a.t === "var")
|
|
25
|
+
return a.name === b.name;
|
|
26
|
+
if (a.t === "lit")
|
|
27
|
+
return a.value === b.value;
|
|
28
|
+
const other = b;
|
|
29
|
+
return a.op === other.op && a.args.length === other.args.length && a.args.every((x, i) => same(x, other.args[i]));
|
|
30
|
+
}
|
|
31
|
+
function op(o, ty, ...args) {
|
|
32
|
+
return { t: "op", ty, op: o, args };
|
|
33
|
+
}
|
|
34
|
+
export function not(a) {
|
|
35
|
+
if (isLit(a))
|
|
36
|
+
return lit(!a.value);
|
|
37
|
+
if (a.t === "op" && a.op === "not")
|
|
38
|
+
return a.args[0];
|
|
39
|
+
return op("not", "bool", a);
|
|
40
|
+
}
|
|
41
|
+
export function and(a, b) {
|
|
42
|
+
if (isLit(a))
|
|
43
|
+
return a.value ? b : FALSE;
|
|
44
|
+
if (isLit(b))
|
|
45
|
+
return b.value ? a : FALSE;
|
|
46
|
+
if (same(a, b))
|
|
47
|
+
return a;
|
|
48
|
+
return op("and", "bool", a, b);
|
|
49
|
+
}
|
|
50
|
+
export function or(a, b) {
|
|
51
|
+
if (isLit(a))
|
|
52
|
+
return a.value ? TRUE : b;
|
|
53
|
+
if (isLit(b))
|
|
54
|
+
return b.value ? TRUE : a;
|
|
55
|
+
if (same(a, b))
|
|
56
|
+
return a;
|
|
57
|
+
return op("or", "bool", a, b);
|
|
58
|
+
}
|
|
59
|
+
export function all(parts) {
|
|
60
|
+
return parts.reduce(and, TRUE);
|
|
61
|
+
}
|
|
62
|
+
export function eq(a, b) {
|
|
63
|
+
if (isLit(a) && isLit(b))
|
|
64
|
+
return lit(a.value === b.value);
|
|
65
|
+
if (same(a, b))
|
|
66
|
+
return TRUE;
|
|
67
|
+
if (a.ty === "bool") {
|
|
68
|
+
if (isLit(b))
|
|
69
|
+
return b.value ? a : not(a);
|
|
70
|
+
if (isLit(a))
|
|
71
|
+
return a.value ? b : not(b);
|
|
72
|
+
}
|
|
73
|
+
return op("eq", "bool", a, b);
|
|
74
|
+
}
|
|
75
|
+
export function lt(a, b) {
|
|
76
|
+
if (isLit(a) && isLit(b))
|
|
77
|
+
return lit(a.value < b.value);
|
|
78
|
+
if (same(a, b))
|
|
79
|
+
return FALSE;
|
|
80
|
+
return op("lt", "bool", a, b);
|
|
81
|
+
}
|
|
82
|
+
export function le(a, b) {
|
|
83
|
+
if (isLit(a) && isLit(b))
|
|
84
|
+
return lit(a.value <= b.value);
|
|
85
|
+
if (same(a, b))
|
|
86
|
+
return TRUE;
|
|
87
|
+
return op("le", "bool", a, b);
|
|
88
|
+
}
|
|
89
|
+
const isInt = (e, n) => isLit(e) && e.value === n;
|
|
90
|
+
export function add(a, b) {
|
|
91
|
+
if (isLit(a) && isLit(b))
|
|
92
|
+
return lit(a.value + b.value);
|
|
93
|
+
if (isInt(a, 0n))
|
|
94
|
+
return b;
|
|
95
|
+
if (isInt(b, 0n))
|
|
96
|
+
return a;
|
|
97
|
+
return op("add", "int", a, b);
|
|
98
|
+
}
|
|
99
|
+
export function sub(a, b) {
|
|
100
|
+
if (isLit(a) && isLit(b))
|
|
101
|
+
return lit(a.value - b.value);
|
|
102
|
+
if (isInt(b, 0n))
|
|
103
|
+
return a;
|
|
104
|
+
if (same(a, b))
|
|
105
|
+
return lit(0n);
|
|
106
|
+
return op("sub", "int", a, b);
|
|
107
|
+
}
|
|
108
|
+
export function mul(a, b) {
|
|
109
|
+
if (isLit(a) && isLit(b))
|
|
110
|
+
return lit(a.value * b.value);
|
|
111
|
+
if (isInt(a, 1n))
|
|
112
|
+
return b;
|
|
113
|
+
if (isInt(b, 1n))
|
|
114
|
+
return a;
|
|
115
|
+
if (isInt(a, 0n) || isInt(b, 0n))
|
|
116
|
+
return lit(0n);
|
|
117
|
+
return op("mul", "int", a, b);
|
|
118
|
+
}
|
|
119
|
+
export function neg(a) {
|
|
120
|
+
if (isLit(a))
|
|
121
|
+
return lit(-a.value);
|
|
122
|
+
return op("neg", "int", a);
|
|
123
|
+
}
|
|
124
|
+
/** BigInt division, which truncates toward zero. The divisor must not be zero. */
|
|
125
|
+
export function div(a, b) {
|
|
126
|
+
if (isLit(a) && isLit(b) && b.value !== 0n)
|
|
127
|
+
return lit(a.value / b.value);
|
|
128
|
+
if (isInt(b, 1n))
|
|
129
|
+
return a;
|
|
130
|
+
// Zero divided by anything is zero; a zero divisor is ruled out separately.
|
|
131
|
+
if (isInt(a, 0n))
|
|
132
|
+
return a;
|
|
133
|
+
return op("div", "int", a, b);
|
|
134
|
+
}
|
|
135
|
+
const floorDivide = (a, b) => (a % b !== 0n && (a < 0n) !== (b < 0n) ? a / b - 1n : a / b);
|
|
136
|
+
/** Division rounded toward minus infinity, as `Math.floor(a / b)` computes it for whole numbers. The divisor must not be zero. */
|
|
137
|
+
export function fdiv(a, b) {
|
|
138
|
+
if (isLit(a) && isLit(b) && b.value !== 0n)
|
|
139
|
+
return lit(floorDivide(a.value, b.value));
|
|
140
|
+
if (isInt(b, 1n))
|
|
141
|
+
return a;
|
|
142
|
+
if (isInt(a, 0n))
|
|
143
|
+
return a;
|
|
144
|
+
return op("fdiv", "int", a, b);
|
|
145
|
+
}
|
|
146
|
+
/** BigInt remainder, which takes the sign of the dividend. */
|
|
147
|
+
export function mod(a, b) {
|
|
148
|
+
if (isLit(a) && isLit(b) && b.value !== 0n)
|
|
149
|
+
return lit(a.value % b.value);
|
|
150
|
+
if (isInt(a, 0n))
|
|
151
|
+
return a;
|
|
152
|
+
return op("mod", "int", a, b);
|
|
153
|
+
}
|
|
154
|
+
/** String concatenation, kept flat and left-nested with adjacent literals merged so equal strings look equal. */
|
|
155
|
+
export function concat(a, b) {
|
|
156
|
+
const parts = [];
|
|
157
|
+
const collect = (e) => {
|
|
158
|
+
if (e.t === "op" && e.op === "concat")
|
|
159
|
+
e.args.forEach(collect);
|
|
160
|
+
else if (isLit(e)) {
|
|
161
|
+
const last = parts.at(-1);
|
|
162
|
+
if (e.value === "")
|
|
163
|
+
return;
|
|
164
|
+
if (last && isLit(last))
|
|
165
|
+
parts[parts.length - 1] = lit(last.value + e.value);
|
|
166
|
+
else
|
|
167
|
+
parts.push(e);
|
|
168
|
+
}
|
|
169
|
+
else
|
|
170
|
+
parts.push(e);
|
|
171
|
+
};
|
|
172
|
+
collect(a);
|
|
173
|
+
collect(b);
|
|
174
|
+
if (!parts.length)
|
|
175
|
+
return lit("");
|
|
176
|
+
return parts.reduce((left, right) => op("concat", "str", left, right));
|
|
177
|
+
}
|
|
178
|
+
/** The decimal text of a bigint, as `String(n)` gives it. */
|
|
179
|
+
export function toStr(a) {
|
|
180
|
+
if (isLit(a))
|
|
181
|
+
return lit(String(a.value));
|
|
182
|
+
return op("str", "str", a);
|
|
183
|
+
}
|
|
184
|
+
export function ite(c, a, b) {
|
|
185
|
+
if (isLit(c))
|
|
186
|
+
return c.value ? a : b;
|
|
187
|
+
if (same(a, b))
|
|
188
|
+
return a;
|
|
189
|
+
if (a.ty === "bool") {
|
|
190
|
+
if (isLit(a) && isLit(b))
|
|
191
|
+
return a.value ? c : not(c);
|
|
192
|
+
if (isLit(a))
|
|
193
|
+
return a.value ? or(c, b) : and(not(c), b);
|
|
194
|
+
if (isLit(b))
|
|
195
|
+
return b.value ? or(not(c), a) : and(c, a);
|
|
196
|
+
}
|
|
197
|
+
return op("ite", a.ty, c, a, b);
|
|
198
|
+
}
|
|
199
|
+
function rebuild(o, args) {
|
|
200
|
+
const [a, b, c] = args;
|
|
201
|
+
switch (o) {
|
|
202
|
+
case "not": return not(a);
|
|
203
|
+
case "and": return and(a, b);
|
|
204
|
+
case "or": return or(a, b);
|
|
205
|
+
case "eq": return eq(a, b);
|
|
206
|
+
case "lt": return lt(a, b);
|
|
207
|
+
case "le": return le(a, b);
|
|
208
|
+
case "add": return add(a, b);
|
|
209
|
+
case "sub": return sub(a, b);
|
|
210
|
+
case "mul": return mul(a, b);
|
|
211
|
+
case "neg": return neg(a);
|
|
212
|
+
case "div": return div(a, b);
|
|
213
|
+
case "mod": return mod(a, b);
|
|
214
|
+
case "fdiv": return fdiv(a, b);
|
|
215
|
+
case "concat": return concat(a, b);
|
|
216
|
+
case "str": return toStr(a);
|
|
217
|
+
case "ite": return ite(a, b, c);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/** Replace variables by expressions, folding constants on the way back up. */
|
|
221
|
+
export function substitute(e, values, memo = new Map()) {
|
|
222
|
+
if (e.t === "lit")
|
|
223
|
+
return e;
|
|
224
|
+
if (e.t === "var")
|
|
225
|
+
return values.get(e.name) ?? e;
|
|
226
|
+
const seen = memo.get(e);
|
|
227
|
+
if (seen)
|
|
228
|
+
return seen;
|
|
229
|
+
let changed = false;
|
|
230
|
+
const args = e.args.map(arg => {
|
|
231
|
+
const next = substitute(arg, values, memo);
|
|
232
|
+
if (next !== arg)
|
|
233
|
+
changed = true;
|
|
234
|
+
return next;
|
|
235
|
+
});
|
|
236
|
+
const result = changed ? rebuild(e.op, args) : e;
|
|
237
|
+
memo.set(e, result);
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
240
|
+
export function freeVariables(e, into = new Set(), seen = new Set()) {
|
|
241
|
+
if (e.t === "var")
|
|
242
|
+
into.add(e.name);
|
|
243
|
+
else if (e.t === "op" && !seen.has(e)) {
|
|
244
|
+
seen.add(e);
|
|
245
|
+
for (const arg of e.args)
|
|
246
|
+
freeVariables(arg, into, seen);
|
|
247
|
+
}
|
|
248
|
+
return into;
|
|
249
|
+
}
|
|
250
|
+
/** Size of the expression when printed as a tree. */
|
|
251
|
+
export function treeSize(e, memo = new Map()) {
|
|
252
|
+
if (e.t !== "op")
|
|
253
|
+
return 1;
|
|
254
|
+
const seen = memo.get(e);
|
|
255
|
+
if (seen !== undefined)
|
|
256
|
+
return seen;
|
|
257
|
+
let size = 1;
|
|
258
|
+
for (const arg of e.args)
|
|
259
|
+
size += treeSize(arg, memo);
|
|
260
|
+
memo.set(e, size);
|
|
261
|
+
return size;
|
|
262
|
+
}
|
|
263
|
+
export function stringLiterals(e, into = new Set(), seen = new Set()) {
|
|
264
|
+
if (e.t === "lit" && typeof e.value === "string")
|
|
265
|
+
into.add(e.value);
|
|
266
|
+
else if (e.t === "op" && !seen.has(e)) {
|
|
267
|
+
seen.add(e);
|
|
268
|
+
for (const arg of e.args)
|
|
269
|
+
stringLiterals(arg, into, seen);
|
|
270
|
+
}
|
|
271
|
+
return into;
|
|
272
|
+
}
|
|
273
|
+
export function integerLiterals(e, into = new Set(), seen = new Set()) {
|
|
274
|
+
if (e.t === "lit" && typeof e.value === "bigint")
|
|
275
|
+
into.add(e.value);
|
|
276
|
+
else if (e.t === "op" && !seen.has(e)) {
|
|
277
|
+
seen.add(e);
|
|
278
|
+
for (const arg of e.args)
|
|
279
|
+
integerLiterals(arg, into, seen);
|
|
280
|
+
}
|
|
281
|
+
return into;
|
|
282
|
+
}
|
|
283
|
+
export class EvaluationError extends Error {
|
|
284
|
+
}
|
|
285
|
+
/** Evaluate with JavaScript semantics. Only used for diagnostics, never to accept a law. */
|
|
286
|
+
export function evaluate(e, env) {
|
|
287
|
+
if (e.t === "lit")
|
|
288
|
+
return e.value;
|
|
289
|
+
if (e.t === "var") {
|
|
290
|
+
const value = env.get(e.name);
|
|
291
|
+
if (value === undefined)
|
|
292
|
+
throw new EvaluationError(`unbound variable ${e.name}`);
|
|
293
|
+
return value;
|
|
294
|
+
}
|
|
295
|
+
const arg = (i) => evaluate(e.args[i], env);
|
|
296
|
+
switch (e.op) {
|
|
297
|
+
case "not": return !arg(0);
|
|
298
|
+
case "and": return arg(0) && arg(1);
|
|
299
|
+
case "or": return arg(0) || arg(1);
|
|
300
|
+
case "eq": return arg(0) === arg(1);
|
|
301
|
+
case "lt": return arg(0) < arg(1);
|
|
302
|
+
case "le": return arg(0) <= arg(1);
|
|
303
|
+
case "add": return arg(0) + arg(1);
|
|
304
|
+
case "sub": return arg(0) - arg(1);
|
|
305
|
+
case "mul": return arg(0) * arg(1);
|
|
306
|
+
case "neg": return -arg(0);
|
|
307
|
+
case "div":
|
|
308
|
+
case "mod":
|
|
309
|
+
case "fdiv": {
|
|
310
|
+
const divisor = arg(1);
|
|
311
|
+
if (divisor === 0n)
|
|
312
|
+
throw new EvaluationError("division by zero");
|
|
313
|
+
if (e.op === "fdiv")
|
|
314
|
+
return floorDivide(arg(0), divisor);
|
|
315
|
+
return e.op === "div" ? arg(0) / divisor : arg(0) % divisor;
|
|
316
|
+
}
|
|
317
|
+
case "concat": return arg(0) + arg(1);
|
|
318
|
+
case "str": return String(arg(0));
|
|
319
|
+
case "ite": return arg(0) ? arg(1) : arg(2);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/** A value as it is written in TypeScript. `num` marks an integer that stands for a `number`. */
|
|
323
|
+
export function showScalar(value, num = false) {
|
|
324
|
+
return typeof value === "bigint" ? (num ? String(value) : `${value}n`) : JSON.stringify(value);
|
|
325
|
+
}
|
|
326
|
+
/** The largest whole number a JavaScript `number` holds exactly. */
|
|
327
|
+
export const MAX_SAFE = 2n ** 53n - 1n;
|
|
328
|
+
/** Whether an integer is one a `number` holds exactly. */
|
|
329
|
+
export function isSafe(e) {
|
|
330
|
+
return and(le(lit(-MAX_SAFE), e), le(e, lit(MAX_SAFE)));
|
|
331
|
+
}
|
package/dist/lean.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Binder, Expr } from "./ir.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The modules the generated theorems need. Importing only these starts Lean
|
|
4
|
+
* several times faster than `import Init`, and they are what a trimmed Lean
|
|
5
|
+
* runtime ships.
|
|
6
|
+
*/
|
|
7
|
+
export declare const FAST_IMPORTS: string[];
|
|
8
|
+
/**
|
|
9
|
+
* A slightly larger set for a second attempt. Measured against the whole of
|
|
10
|
+
* `Init` on thousands of generated theorems, the fallback tactic proves
|
|
11
|
+
* exactly as much under these imports.
|
|
12
|
+
*/
|
|
13
|
+
export declare const FALLBACK_IMPORTS: string[];
|
|
14
|
+
export declare const FAST_HEADER: string;
|
|
15
|
+
export declare const FALLBACK_HEADER: string;
|
|
16
|
+
/** The whole core library, for proofs an author wrote. A trimmed runtime does not have it. */
|
|
17
|
+
export declare const FULL_HEADER = "import Init";
|
|
18
|
+
export declare const DEFAULT_TACTIC = "grind";
|
|
19
|
+
/**
|
|
20
|
+
* Why an author's proof script cannot be used, if it cannot. The script is
|
|
21
|
+
* placed inside parentheses in the theorem, and that only confines it if every
|
|
22
|
+
* parenthesis in its text is one Lean sees. So the things that can hide a
|
|
23
|
+
* parenthesis from Lean (comments, string and character literals, quoted
|
|
24
|
+
* names) are not allowed, and the parentheses must balance.
|
|
25
|
+
*/
|
|
26
|
+
export declare function scriptProblem(script: string): string | undefined;
|
|
27
|
+
/** How a statement is proved: by evaluating every case, by the default automation, or by the author's script. */
|
|
28
|
+
export declare function proofKind(statement: string): "finite" | "automatic" | "authored";
|
|
29
|
+
/** The same statement with the stronger, slower automation. Only meaningful for an automatic statement. */
|
|
30
|
+
export declare function withFallback(statement: string): string;
|
|
31
|
+
export declare class TooLarge extends Error {
|
|
32
|
+
}
|
|
33
|
+
export interface Check {
|
|
34
|
+
/** Everything after the theorem name. Also the cache key, together with the Lean version. */
|
|
35
|
+
statement: string;
|
|
36
|
+
/** Number of concrete cases the theorem covers when it is decided by evaluation. */
|
|
37
|
+
cases: number;
|
|
38
|
+
}
|
|
39
|
+
export declare function checksFor(binders: readonly Binder[], guard: Expr, claim: Expr, tactic: string | undefined): Check[];
|
|
40
|
+
export interface LeanOutcome {
|
|
41
|
+
ok: boolean;
|
|
42
|
+
/** Lean's message when the theorem was rejected. */
|
|
43
|
+
message: string;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Check each statement, in parallel across processes when there are many.
|
|
47
|
+
*
|
|
48
|
+
* Statements Pudding generated share files. A statement that carries an
|
|
49
|
+
* author's proof script gets a file of its own (`alone`), with any error in
|
|
50
|
+
* that file counting against it: Lean recovers from a syntax error by reading
|
|
51
|
+
* on from the next command, so text in a script could otherwise define things
|
|
52
|
+
* that change what the theorems after it mean.
|
|
53
|
+
*/
|
|
54
|
+
export declare function prove(lean: string, header: string, statements: string[], processes?: number, alone?: boolean): Promise<LeanOutcome[]>;
|
|
55
|
+
/** The Lean source for a set of statements, for inspection. */
|
|
56
|
+
export declare function render(header: string, statements: string[]): string;
|