@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/runner.js
ADDED
|
@@ -0,0 +1,672 @@
|
|
|
1
|
+
// Orchestrates a run: read the laws with the TypeScript compiler, prove the
|
|
2
|
+
// resulting obligations in Lean, explain what failed, and compare the proved
|
|
3
|
+
// model with the real code.
|
|
4
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
5
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
6
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
7
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
return path;
|
|
11
|
+
};
|
|
12
|
+
import { execFileSync } from "node:child_process";
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { createRequire } from "node:module";
|
|
15
|
+
import { existsSync, globSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
18
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
19
|
+
import { threadId, Worker } from "node:worker_threads";
|
|
20
|
+
import { SyntaxKind as K } from "typescript/unstable/ast";
|
|
21
|
+
import { API, DiagnosticCategory, SymbolFlags } from "typescript/unstable/sync";
|
|
22
|
+
import { cacheDirectory, toolchainName } from "./install.js";
|
|
23
|
+
import * as ir from "./ir.js";
|
|
24
|
+
import { findInput, showInputs, showValue } from "./search.js";
|
|
25
|
+
import { checksFor, FALLBACK_HEADER, FAST_HEADER, FULL_HEADER, proofKind, prove, render, scriptProblem, TooLarge, withFallback } from "./lean.js";
|
|
26
|
+
import { Translator } from "./translate.js";
|
|
27
|
+
export const MINIMUM_LEAN = [4, 36, 0];
|
|
28
|
+
const CACHE_VERSION = 2;
|
|
29
|
+
const RUNTIME_BUDGET = 4_000;
|
|
30
|
+
const RUNTIME_TIMEOUT_MS = 30_000;
|
|
31
|
+
/** A path as shown to the user: relative inside the project, absolute outside it. */
|
|
32
|
+
export function displayPath(cwd, file) {
|
|
33
|
+
const path = relative(cwd, file);
|
|
34
|
+
return path === "" || path.startsWith("..") ? file : path;
|
|
35
|
+
}
|
|
36
|
+
const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
|
|
37
|
+
/** The name law files import from, read from this package so a rename needs no code change. */
|
|
38
|
+
export const packageName = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8")).name;
|
|
39
|
+
const hash = (text) => createHash("sha256").update(text).digest("hex").slice(0, 32);
|
|
40
|
+
async function loadConfig(cwd) {
|
|
41
|
+
for (const name of ["pudding.config.ts", "pudding.config.mts", "pudding.config.js", "pudding.config.mjs"]) {
|
|
42
|
+
const file = join(cwd, name);
|
|
43
|
+
if (!existsSync(file))
|
|
44
|
+
continue;
|
|
45
|
+
// The query makes a changed file load again when one process runs several times, as watch mode does.
|
|
46
|
+
const module = (await import(__rewriteRelativeImportExtension(`${pathToFileURL(file).href}?changed=${statSync(file).mtimeMs}`)));
|
|
47
|
+
return { config: module.default ?? {}, file };
|
|
48
|
+
}
|
|
49
|
+
return { config: {}, file: undefined };
|
|
50
|
+
}
|
|
51
|
+
/** Raised when no Lean binary can be found, so the CLI can offer to install one. */
|
|
52
|
+
export class LeanMissing extends Error {
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Find the Lean binary. In order: the configuration, `PUDDING_LEAN`, the
|
|
56
|
+
* runtime package for this platform, a `.toolchain` directory in the project
|
|
57
|
+
* or in this package, the machine-wide cache `install-lean` fills, elan, and
|
|
58
|
+
* finally `lean` on the PATH.
|
|
59
|
+
*/
|
|
60
|
+
export function locateLean(config, cwd) {
|
|
61
|
+
const explicit = config.lean ?? process.env["PUDDING_LEAN"];
|
|
62
|
+
if (explicit)
|
|
63
|
+
return isAbsolute(explicit) ? explicit : resolve(cwd, explicit);
|
|
64
|
+
const suffix = process.platform === "win32" ? ".exe" : "";
|
|
65
|
+
const name = toolchainName();
|
|
66
|
+
// What `install-lean` left behind: in the project, next to Pudding itself, or in the shared cache.
|
|
67
|
+
const installed = [
|
|
68
|
+
...[cwd, packageRoot].flatMap(root => globSync(".toolchain/lean-*/bin/lean" + suffix, { cwd: root }).sort().slice(-1).map(found => join(root, found))),
|
|
69
|
+
...(name ? [join(cacheDirectory(), name, "bin", "lean" + suffix)] : []),
|
|
70
|
+
].filter(candidate => existsSync(candidate));
|
|
71
|
+
// A whole Lean is only there because someone asked for one, so that a proof
|
|
72
|
+
// of their own can use more of the library. It goes before the trimmed
|
|
73
|
+
// runtime package for that reason.
|
|
74
|
+
const whole = installed.find(candidate => existsSync(join(dirname(dirname(candidate)), "lib", "lean", "Lean.olean")));
|
|
75
|
+
if (whole)
|
|
76
|
+
return whole;
|
|
77
|
+
try {
|
|
78
|
+
const manifest = createRequire(import.meta.url).resolve(`${packageName}-lean-${process.platform}-${process.arch}/package.json`);
|
|
79
|
+
const bundled = join(dirname(manifest), "bin", "lean" + suffix);
|
|
80
|
+
if (existsSync(bundled))
|
|
81
|
+
return bundled;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// The runtime package is optional.
|
|
85
|
+
}
|
|
86
|
+
const elan = join(homedir(), ".elan", "bin", "lean" + suffix);
|
|
87
|
+
return installed[0] ?? (existsSync(elan) ? elan : "lean");
|
|
88
|
+
}
|
|
89
|
+
function leanVersion(lean, cache) {
|
|
90
|
+
let key = lean;
|
|
91
|
+
try {
|
|
92
|
+
const stat = statSync(lean);
|
|
93
|
+
key = `${lean}:${stat.mtimeMs}:${stat.size}`;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Resolved through PATH; ask the binary every time.
|
|
97
|
+
}
|
|
98
|
+
let version = cache.lean[key];
|
|
99
|
+
if (!version || key === lean) {
|
|
100
|
+
try {
|
|
101
|
+
version = execFileSync(lean, ["--version"], { encoding: "utf8" }).trim();
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
throw new LeanMissing(lean === "lean"
|
|
105
|
+
? `Lean is not installed. Run \`npx pudding install-lean\`, or set PUDDING_LEAN to a Lean ${MINIMUM_LEAN.join(".")} binary.`
|
|
106
|
+
: `Lean could not be run at \`${lean}\`. Run \`npx pudding install-lean\`, or set PUDDING_LEAN to a Lean ${MINIMUM_LEAN.join(".")} binary.`);
|
|
107
|
+
}
|
|
108
|
+
cache.lean = { [key]: version };
|
|
109
|
+
}
|
|
110
|
+
const match = /version (\d+)\.(\d+)\.(\d+)/.exec(version);
|
|
111
|
+
const found = match ? match.slice(1, 4).map(Number) : [0, 0, 0];
|
|
112
|
+
const tooOld = MINIMUM_LEAN.findIndex((part, i) => found[i] !== part);
|
|
113
|
+
if (tooOld >= 0 && found[tooOld] < MINIMUM_LEAN[tooOld]) {
|
|
114
|
+
throw new Error(`Pudding needs Lean ${MINIMUM_LEAN.join(".")} or newer, but \`${lean}\` is ${found.join(".")}.`);
|
|
115
|
+
}
|
|
116
|
+
return version;
|
|
117
|
+
}
|
|
118
|
+
/** The most proofs remembered. Only runs over part of the laws can reach it, since a full run forgets what it did not use. */
|
|
119
|
+
const CACHE_LIMIT = 50_000;
|
|
120
|
+
function cachePath(cwd) {
|
|
121
|
+
return existsSync(join(cwd, "node_modules")) ? join(cwd, "node_modules", ".cache", "pudding", "cache.json") : join(cwd, ".pudding", "cache.json");
|
|
122
|
+
}
|
|
123
|
+
/** Write a file so that another run reading it at the same moment never sees half of it. */
|
|
124
|
+
function replaceFile(file, text) {
|
|
125
|
+
const partial = `${file}.${process.pid}.${threadId}.tmp`;
|
|
126
|
+
writeFileSync(partial, text);
|
|
127
|
+
renameSync(partial, file);
|
|
128
|
+
}
|
|
129
|
+
function loadCache(file) {
|
|
130
|
+
try {
|
|
131
|
+
const cache = JSON.parse(readFileSync(file, "utf8"));
|
|
132
|
+
if (cache.version === CACHE_VERSION)
|
|
133
|
+
return cache;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// Missing or unreadable: start fresh.
|
|
137
|
+
}
|
|
138
|
+
return { version: CACHE_VERSION, lean: {}, proved: [] };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Write the TypeScript project Pudding reads the laws through: the law files
|
|
142
|
+
* as roots, on top of the project's own compiler options. This is why law
|
|
143
|
+
* files need no entry in anyone's `include`, and why a project without a
|
|
144
|
+
* tsconfig still works.
|
|
145
|
+
*/
|
|
146
|
+
function writeProject(directory, base, lawFiles, partial) {
|
|
147
|
+
const defaults = join(directory, "tsconfig.defaults.json");
|
|
148
|
+
// A run over some of the law files gets a project of its own, because
|
|
149
|
+
// several of them can be going at once: Vitest proves each file in its own worker.
|
|
150
|
+
const subset = createHash("sha256").update(lawFiles.join("\0")).digest("hex").slice(0, 12);
|
|
151
|
+
const project = join(directory, partial ? `tsconfig.${subset}.json` : "tsconfig.json");
|
|
152
|
+
const write = (file, value) => {
|
|
153
|
+
const text = JSON.stringify(value, null, 2) + "\n";
|
|
154
|
+
if (!existsSync(file) || readFileSync(file, "utf8") !== text)
|
|
155
|
+
replaceFile(file, text);
|
|
156
|
+
};
|
|
157
|
+
mkdirSync(directory, { recursive: true });
|
|
158
|
+
write(defaults, {
|
|
159
|
+
compilerOptions: { target: "ESNext", module: "Preserve", moduleResolution: "Bundler", strict: true, skipLibCheck: true },
|
|
160
|
+
});
|
|
161
|
+
write(project, {
|
|
162
|
+
extends: base ? [defaults, base] : [defaults],
|
|
163
|
+
compilerOptions: { noEmit: true, allowImportingTsExtensions: true, composite: false, incremental: false, declaration: false },
|
|
164
|
+
files: lawFiles,
|
|
165
|
+
include: [],
|
|
166
|
+
exclude: [],
|
|
167
|
+
});
|
|
168
|
+
return project;
|
|
169
|
+
}
|
|
170
|
+
// ----------------------------------------------------------------- runtime
|
|
171
|
+
/**
|
|
172
|
+
* What Node needs to execute the law files the way TypeScript reads them:
|
|
173
|
+
* where each import resolves, and which imported or exported names are types
|
|
174
|
+
* only. Node erases type syntax but neither resolves `./cart` to `./cart.ts`
|
|
175
|
+
* nor drops an imported interface, and bundler-style code relies on both.
|
|
176
|
+
*/
|
|
177
|
+
function runtimeMap(program, checker, ambiguous) {
|
|
178
|
+
const map = { resolutions: new Map(), elided: new Map() };
|
|
179
|
+
for (const name of program.getSourceFileNames()) {
|
|
180
|
+
if (!isAbsolute(name) || name.includes("/node_modules/"))
|
|
181
|
+
continue;
|
|
182
|
+
const source = program.getSourceFile(name);
|
|
183
|
+
if (!source || source.isDeclarationFile)
|
|
184
|
+
continue;
|
|
185
|
+
const text = source.text;
|
|
186
|
+
const resolutions = new Map();
|
|
187
|
+
const ranges = [];
|
|
188
|
+
/** The range of a binding, widened over one neighbouring comma. */
|
|
189
|
+
const withComma = (node) => {
|
|
190
|
+
let start = node.getStart(source);
|
|
191
|
+
let end = node.end;
|
|
192
|
+
const after = /^\s*,/.exec(text.slice(end));
|
|
193
|
+
if (after)
|
|
194
|
+
end += after[0].length;
|
|
195
|
+
else {
|
|
196
|
+
const before = /,\s*$/.exec(text.slice(0, start));
|
|
197
|
+
if (before)
|
|
198
|
+
start -= before[0].length;
|
|
199
|
+
}
|
|
200
|
+
return [start, end];
|
|
201
|
+
};
|
|
202
|
+
for (const statement of source.statements) {
|
|
203
|
+
const isImport = statement.kind === K.ImportDeclaration;
|
|
204
|
+
if (!isImport && statement.kind !== K.ExportDeclaration)
|
|
205
|
+
continue;
|
|
206
|
+
const specifier = statement.moduleSpecifier;
|
|
207
|
+
if (specifier?.kind === K.StringLiteral) {
|
|
208
|
+
const symbol = checker.getSymbolAtLocation(specifier);
|
|
209
|
+
const target = (symbol?.valueDeclaration ?? symbol?.declarations[0])?.resolve()?.getSourceFile();
|
|
210
|
+
if (target && !target.isDeclarationFile && !program.isSourceFileFromExternalLibrary(target)) {
|
|
211
|
+
// `./x.js` normally means the TypeScript file `./x.ts`. If a real
|
|
212
|
+
// `./x.js` also exists, Node runs that one, and it may not be what
|
|
213
|
+
// TypeScript read. Leave the import to Node and say so.
|
|
214
|
+
const literal = /^\.{1,2}\//.test(specifier.text) ? resolve(dirname(name), specifier.text) : undefined;
|
|
215
|
+
if (literal && literal !== target.fileName && existsSync(literal) && statSync(literal).isFile()) {
|
|
216
|
+
ambiguous(`${name} imports \`${specifier.text}\`, and both that file and ${target.fileName} exist: TypeScript reads one and Node runs the other, so a proof about the first says nothing certain about the second. Remove one of them or import the TypeScript file by its own name`);
|
|
217
|
+
}
|
|
218
|
+
else
|
|
219
|
+
resolutions.set(specifier.text, target.fileName);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (isImport ? statement.importClause?.phaseModifier === K.TypeKeyword : statement.isTypeOnly)
|
|
223
|
+
continue;
|
|
224
|
+
const bindings = isImport
|
|
225
|
+
? statement.importClause?.namedBindings?.kind === K.NamedImports ? [...statement.importClause.namedBindings.elements] : []
|
|
226
|
+
: statement.exportClause?.kind === K.NamedExports ? [...statement.exportClause.elements] : [];
|
|
227
|
+
const typeOnly = bindings.filter(binding => {
|
|
228
|
+
if (binding.isTypeOnly)
|
|
229
|
+
return false;
|
|
230
|
+
let symbol = checker.getSymbolAtLocation(binding.name);
|
|
231
|
+
if (symbol && symbol.flags & SymbolFlags.Alias)
|
|
232
|
+
symbol = checker.getAliasedSymbol(symbol);
|
|
233
|
+
return !!symbol && !checker.isUnknownSymbol(symbol) && (symbol.flags & SymbolFlags.Value) === 0;
|
|
234
|
+
});
|
|
235
|
+
// TypeScript drops an import whose names are all types; so must we, or Node would load a module for nothing.
|
|
236
|
+
const whole = isImport && typeOnly.length > 0 && typeOnly.length === bindings.length && !statement.importClause.name;
|
|
237
|
+
if (whole)
|
|
238
|
+
ranges.push([statement.getStart(source), statement.end]);
|
|
239
|
+
else
|
|
240
|
+
for (const binding of typeOnly)
|
|
241
|
+
ranges.push(withComma(binding));
|
|
242
|
+
}
|
|
243
|
+
if (resolutions.size)
|
|
244
|
+
map.resolutions.set(name, resolutions);
|
|
245
|
+
if (ranges.length)
|
|
246
|
+
map.elided.set(name, ranges);
|
|
247
|
+
}
|
|
248
|
+
return map;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Execute the laws in a worker thread: fresh module state on every run, no
|
|
252
|
+
* way for the project's code to disturb this process, and a time limit.
|
|
253
|
+
*/
|
|
254
|
+
function executeLaws(job) {
|
|
255
|
+
return new Promise(resolve => {
|
|
256
|
+
const outcomes = new Map();
|
|
257
|
+
const entry = new URL(import.meta.url.replace(/runner\.(ts|js)$/, "crosscheck.$1"));
|
|
258
|
+
const worker = new Worker(entry, { workerData: job, stdout: true, stderr: true });
|
|
259
|
+
let settled = false;
|
|
260
|
+
const finish = (timedOut) => {
|
|
261
|
+
if (settled)
|
|
262
|
+
return;
|
|
263
|
+
settled = true;
|
|
264
|
+
clearTimeout(timer);
|
|
265
|
+
void worker.terminate();
|
|
266
|
+
resolve({ outcomes, timedOut });
|
|
267
|
+
};
|
|
268
|
+
const timer = setTimeout(() => finish(true), RUNTIME_TIMEOUT_MS);
|
|
269
|
+
worker.on("message", (message) => {
|
|
270
|
+
if (message === "done")
|
|
271
|
+
finish(false);
|
|
272
|
+
else
|
|
273
|
+
outcomes.set(message.file, message);
|
|
274
|
+
});
|
|
275
|
+
worker.on("error", () => finish(false));
|
|
276
|
+
worker.on("exit", () => finish(false));
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function emptyReport(lean) {
|
|
280
|
+
return {
|
|
281
|
+
ok: false, laws: [], errors: [], warnings: [], counts: { proved: 0, checked: 0, sampled: 0, failed: 0, skipped: 0, todo: 0 },
|
|
282
|
+
obligations: 0, cachedObligations: 0, cases: 0, durationMs: 0, lean,
|
|
283
|
+
retried: 0, timings: { typescript: 0, prove: 0, retry: 0, crossCheck: 0 },
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
export async function run(options = {}) {
|
|
287
|
+
const started = performance.now();
|
|
288
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
289
|
+
const loaded = await loadConfig(cwd);
|
|
290
|
+
const given = Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined));
|
|
291
|
+
const config = { ...loaded.config, ...given };
|
|
292
|
+
const filters = config.files ?? [];
|
|
293
|
+
const lawFiles = config.lawFiles?.map(file => resolve(cwd, file)) ?? globSync(config.include ?? ["**/*.law.ts"], {
|
|
294
|
+
cwd, exclude: ["**/node_modules/**", "**/.toolchain/**", ...(config.exclude ?? [])],
|
|
295
|
+
}).sort().map(file => resolve(cwd, file)).filter(file => !filters.length || filters.some(filter => displayPath(cwd, file).includes(filter)));
|
|
296
|
+
const base = resolve(cwd, config.tsconfig ?? "tsconfig.json");
|
|
297
|
+
const cacheFile = cachePath(cwd);
|
|
298
|
+
const cache = config.cache === false ? loadCache("") : loadCache(cacheFile);
|
|
299
|
+
const lean = locateLean(config, cwd);
|
|
300
|
+
const version = leanVersion(lean, cache);
|
|
301
|
+
const report = emptyReport(version);
|
|
302
|
+
if (config.reporter)
|
|
303
|
+
report.reporter = config.reporter;
|
|
304
|
+
const finish = () => {
|
|
305
|
+
report.durationMs = Math.round(performance.now() - started);
|
|
306
|
+
return report;
|
|
307
|
+
};
|
|
308
|
+
if (!lawFiles.length) {
|
|
309
|
+
report.errors.push(filters.length ? `no law file matches ${filters.map(f => `\`${f}\``).join(" or ")}` : "no law files were found");
|
|
310
|
+
return finish();
|
|
311
|
+
}
|
|
312
|
+
if (config.tsconfig && !existsSync(base)) {
|
|
313
|
+
report.errors.push(`the tsconfig \`${config.tsconfig}\` named in the configuration was not found`);
|
|
314
|
+
return finish();
|
|
315
|
+
}
|
|
316
|
+
let selected = () => true;
|
|
317
|
+
if (config.name) {
|
|
318
|
+
try {
|
|
319
|
+
const pattern = new RegExp(config.name);
|
|
320
|
+
selected = name => pattern.test(name);
|
|
321
|
+
}
|
|
322
|
+
catch {
|
|
323
|
+
report.errors.push(`\`${config.name}\` is not a valid regular expression`);
|
|
324
|
+
return finish();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const partial = config.lawFiles !== undefined || filters.length > 0 || config.name !== undefined;
|
|
328
|
+
const tsconfig = writeProject(dirname(cacheFile), existsSync(base) ? base : undefined, lawFiles, partial);
|
|
329
|
+
// Read and translate the laws.
|
|
330
|
+
const reading = performance.now();
|
|
331
|
+
const api = new API({ cwd });
|
|
332
|
+
let laws = [];
|
|
333
|
+
let executable;
|
|
334
|
+
try {
|
|
335
|
+
const snapshot = api.updateSnapshot({ openProjects: [tsconfig] });
|
|
336
|
+
const project = snapshot.getProject(tsconfig) ?? snapshot.getProjects()[0];
|
|
337
|
+
if (!project) {
|
|
338
|
+
report.errors.push("TypeScript could not open the project Pudding generated for the law files");
|
|
339
|
+
return finish();
|
|
340
|
+
}
|
|
341
|
+
const { program, checker } = project;
|
|
342
|
+
const diagnose = (file) => {
|
|
343
|
+
const source = program.getSourceFile(file);
|
|
344
|
+
for (const diagnostic of [...program.getSyntacticDiagnostics(file), ...program.getSemanticDiagnostics(file)]) {
|
|
345
|
+
if (diagnostic.category !== DiagnosticCategory.Error)
|
|
346
|
+
continue;
|
|
347
|
+
const at = source?.getLineAndCharacterOfPosition(diagnostic.pos);
|
|
348
|
+
report.errors.push(`${displayPath(cwd, file)}:${at ? `${at.line + 1}:${at.character + 1}` : "?"} TS${diagnostic.code}: ${diagnostic.text}`);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const readable = lawFiles.filter(file => {
|
|
352
|
+
if (program.getSourceFile(file))
|
|
353
|
+
return true;
|
|
354
|
+
report.errors.push(`${displayPath(cwd, file)} could not be read by TypeScript`);
|
|
355
|
+
return false;
|
|
356
|
+
});
|
|
357
|
+
readable.forEach(diagnose);
|
|
358
|
+
if (report.errors.length)
|
|
359
|
+
return finish();
|
|
360
|
+
const translator = new Translator(program, checker, packageName);
|
|
361
|
+
for (const file of readable)
|
|
362
|
+
laws.push(...translator.translateFile(program.getSourceFile(file), selected));
|
|
363
|
+
for (const file of translator.reached)
|
|
364
|
+
if (!readable.includes(file))
|
|
365
|
+
diagnose(file);
|
|
366
|
+
if (report.errors.length)
|
|
367
|
+
return finish();
|
|
368
|
+
const map = runtimeMap(program, checker, message => report.errors.push(message.replaceAll(cwd + "/", "")));
|
|
369
|
+
if (report.errors.length)
|
|
370
|
+
return finish();
|
|
371
|
+
if (config.crossCheck !== false)
|
|
372
|
+
executable = map;
|
|
373
|
+
}
|
|
374
|
+
finally {
|
|
375
|
+
api.close();
|
|
376
|
+
report.timings.typescript = Math.round(performance.now() - reading);
|
|
377
|
+
}
|
|
378
|
+
// Turn obligations into Lean theorems.
|
|
379
|
+
const lawReports = new Map();
|
|
380
|
+
const pending = [];
|
|
381
|
+
for (const law of laws) {
|
|
382
|
+
const entry = {
|
|
383
|
+
name: law.name, file: law.file, line: law.line, column: law.column, status: "proved", findings: [], notes: [], obligations: 0, cases: 0,
|
|
384
|
+
};
|
|
385
|
+
lawReports.set(law, entry);
|
|
386
|
+
if (law.problem || law.mode !== "run")
|
|
387
|
+
continue;
|
|
388
|
+
const unusable = law.proof === undefined ? undefined : scriptProblem(law.proof);
|
|
389
|
+
if (unusable) {
|
|
390
|
+
law.problem = { kind: "invalid", message: unusable, file: law.file, line: law.line, column: law.column };
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
try {
|
|
394
|
+
const batch = [];
|
|
395
|
+
for (const instance of law.instances) {
|
|
396
|
+
for (const obligation of instance.obligations) {
|
|
397
|
+
batch.push({ law, instance, obligation, checks: checksFor(instance.binders, obligation.guard, obligation.claim, law.proof) });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
pending.push(...batch);
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
if (!(error instanceof TooLarge))
|
|
404
|
+
throw error;
|
|
405
|
+
law.problem = { kind: "unsupported", message: error.message, file: law.file, line: law.line, column: law.column };
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
// Start executing the laws against the real code now; it runs alongside
|
|
409
|
+
// the proving and is compared with the model once both are done.
|
|
410
|
+
const executing = performance.now();
|
|
411
|
+
const candidates = laws.filter(law => law.index >= 0);
|
|
412
|
+
const job = executable && {
|
|
413
|
+
map: executable,
|
|
414
|
+
budget: RUNTIME_BUDGET,
|
|
415
|
+
files: lawFiles.map(file => ({
|
|
416
|
+
file,
|
|
417
|
+
registered: candidates.filter(law => law.file === file).length,
|
|
418
|
+
laws: candidates.filter(law => law.file === file && law.mode === "run" && law.problem?.kind !== "invalid").map(law => ({
|
|
419
|
+
index: law.index,
|
|
420
|
+
unproved: law.problem !== undefined,
|
|
421
|
+
mentioned: law.mentioned,
|
|
422
|
+
instances: law.instances.map(instance => ({
|
|
423
|
+
binders: instance.binders, arg: instance.arg, obligations: instance.obligations.map(o => ({ guard: o.guard, claim: o.claim, inexact: o.kind === "overflow" || o.kind === "nan" })),
|
|
424
|
+
})),
|
|
425
|
+
})),
|
|
426
|
+
})),
|
|
427
|
+
};
|
|
428
|
+
const execution = job && executeLaws(job).then(result => {
|
|
429
|
+
report.timings.crossCheck = Math.round(performance.now() - executing);
|
|
430
|
+
return result;
|
|
431
|
+
});
|
|
432
|
+
// Prove what the cache does not already hold.
|
|
433
|
+
const proved = new Set(cache.proved);
|
|
434
|
+
const keyOf = (statement) => hash(version + "\0" + statement);
|
|
435
|
+
const statements = [...new Set(pending.flatMap(p => p.checks.map(c => c.statement)))];
|
|
436
|
+
const outcomes = new Map();
|
|
437
|
+
const fresh = [];
|
|
438
|
+
for (const statement of statements) {
|
|
439
|
+
if (proved.has(keyOf(statement)))
|
|
440
|
+
outcomes.set(statement, { ok: true, message: "" });
|
|
441
|
+
else
|
|
442
|
+
fresh.push(statement);
|
|
443
|
+
}
|
|
444
|
+
report.obligations = statements.length;
|
|
445
|
+
report.cachedObligations = statements.length - fresh.length;
|
|
446
|
+
if (config.emit) {
|
|
447
|
+
mkdirSync(dirname(resolve(cwd, config.emit)), { recursive: true });
|
|
448
|
+
writeFileSync(resolve(cwd, config.emit), render(FAST_HEADER, statements));
|
|
449
|
+
}
|
|
450
|
+
// Everything starts under the fast imports. A proof the author supplied may
|
|
451
|
+
// name lemmas from anywhere in Lean's core library, so one that fails is
|
|
452
|
+
// tried again with all of it, where the installed Lean has all of it.
|
|
453
|
+
const proving = performance.now();
|
|
454
|
+
const generated = fresh.filter(statement => proofKind(statement) !== "authored");
|
|
455
|
+
const scripted = fresh.filter(statement => proofKind(statement) === "authored");
|
|
456
|
+
// A theorem with an author's script is checked in a file of its own.
|
|
457
|
+
const [first, own] = await Promise.all([prove(lean, FAST_HEADER, generated), prove(lean, FAST_HEADER, scripted, undefined, true)]);
|
|
458
|
+
generated.forEach((statement, i) => outcomes.set(statement, first[i]));
|
|
459
|
+
scripted.forEach((statement, i) => outcomes.set(statement, own[i]));
|
|
460
|
+
const authored = scripted.filter((_, i) => !own[i].ok);
|
|
461
|
+
if (authored.length) {
|
|
462
|
+
try {
|
|
463
|
+
const again = await prove(lean, FULL_HEADER, authored, undefined, true);
|
|
464
|
+
authored.forEach((statement, i) => outcomes.set(statement, again[i]));
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
for (const statement of authored) {
|
|
468
|
+
const outcome = outcomes.get(statement);
|
|
469
|
+
outcome.message += "\n(This Lean installation holds only the part of the library Pudding itself uses. A proof that needs more of it needs a full Lean: run `pudding install-lean --full`, or set PUDDING_LEAN.)";
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
report.timings.prove = Math.round(performance.now() - proving);
|
|
474
|
+
// An obligation with a counterexample is false, so trying harder to prove
|
|
475
|
+
// it would only waste time. The search is cheap and its result is reused
|
|
476
|
+
// when the failure is explained.
|
|
477
|
+
const retrying = performance.now();
|
|
478
|
+
const counterexamples = new Map();
|
|
479
|
+
const unexplained = new Set();
|
|
480
|
+
for (const { instance, obligation, checks } of pending) {
|
|
481
|
+
const rejected = checks.filter(c => !outcomes.get(c.statement).ok);
|
|
482
|
+
if (!rejected.length)
|
|
483
|
+
continue;
|
|
484
|
+
const { found } = findInput(instance.binders, ir.and(obligation.guard, ir.not(obligation.claim)));
|
|
485
|
+
counterexamples.set(obligation, found);
|
|
486
|
+
if (!found)
|
|
487
|
+
for (const check of rejected)
|
|
488
|
+
unexplained.add(check.statement);
|
|
489
|
+
}
|
|
490
|
+
// What the default automation could not close gets a slower second attempt
|
|
491
|
+
// before the rejection is reported. Lean's first message is kept, because it
|
|
492
|
+
// describes the goal best.
|
|
493
|
+
const retry = generated.filter((statement, i) => !first[i].ok && proofKind(statement) === "automatic" && unexplained.has(statement));
|
|
494
|
+
const second = await prove(lean, FALLBACK_HEADER, retry.map(withFallback));
|
|
495
|
+
retry.forEach((statement, i) => {
|
|
496
|
+
if (second[i].ok)
|
|
497
|
+
outcomes.set(statement, second[i]);
|
|
498
|
+
});
|
|
499
|
+
report.timings.retry = Math.round(performance.now() - retrying);
|
|
500
|
+
report.retried = retry.length;
|
|
501
|
+
const used = new Set();
|
|
502
|
+
for (const statement of statements)
|
|
503
|
+
if (outcomes.get(statement).ok)
|
|
504
|
+
used.add(keyOf(statement));
|
|
505
|
+
// Explain each obligation that Lean rejected.
|
|
506
|
+
for (const { law, instance, obligation, checks } of pending) {
|
|
507
|
+
const entry = lawReports.get(law);
|
|
508
|
+
entry.obligations += checks.length;
|
|
509
|
+
entry.cases += checks.reduce((n, c) => n + c.cases, 0);
|
|
510
|
+
const rejected = checks.map(c => outcomes.get(c.statement)).find(o => !o.ok);
|
|
511
|
+
if (!rejected)
|
|
512
|
+
continue;
|
|
513
|
+
const at = { file: obligation.file, line: obligation.line, column: obligation.column, text: obligation.text };
|
|
514
|
+
const found = counterexamples.get(obligation);
|
|
515
|
+
if (found) {
|
|
516
|
+
const inputs = showInputs(instance.binders, found);
|
|
517
|
+
if (obligation.kind === "expect") {
|
|
518
|
+
const sides = obligation.sides;
|
|
519
|
+
entry.findings.push({
|
|
520
|
+
kind: "counterexample", message: "the expectation does not hold", ...at, inputs,
|
|
521
|
+
...(sides ? { detail: `actual: ${showValue(sides.actual, found, sides.num)}\nexpected: ${sides.negated ? "anything but " : ""}${showValue(sides.expected, found, sides.num)}` } : {}),
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
entry.findings.push(obligation.kind === "overflow" || obligation.kind === "nan" ? {
|
|
526
|
+
kind: "inexact", ...at, inputs,
|
|
527
|
+
message: obligation.kind === "nan" ? "this divides a number by zero, which does not give a whole number"
|
|
528
|
+
: "this result is too large for a number to hold exactly (beyond 2^53 - 1)",
|
|
529
|
+
} : {
|
|
530
|
+
kind: "throws", ...at, inputs,
|
|
531
|
+
message: obligation.kind === "nothrow" ? "this divides by zero"
|
|
532
|
+
: obligation.kind === "falloff" ? "this function reaches its end without returning a value"
|
|
533
|
+
: obligation.kind === "missing" ? "this reads a property the object does not have" : "this throw is reached",
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
const message = obligation.kind === "expect" ? "Lean could not prove this, and no counterexample was found"
|
|
539
|
+
: obligation.kind === "nothrow" || obligation.kind === "nan" ? "Lean could not prove that the divisor is never zero"
|
|
540
|
+
: obligation.kind === "overflow" ? "Lean could not prove that this result stays small enough for a number to hold exactly"
|
|
541
|
+
: obligation.kind === "falloff" ? "Lean could not prove that this function always returns a value"
|
|
542
|
+
: obligation.kind === "missing" ? "Lean could not prove that this key is always a property of the object"
|
|
543
|
+
: "Lean could not prove that this throw is never reached";
|
|
544
|
+
entry.findings.push({ kind: "unproved", message, ...at, detail: rejected.message.split("\n").slice(0, 8).join("\n") });
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
// An expectation whose condition can never be true proves nothing.
|
|
548
|
+
const vacuityChecks = [];
|
|
549
|
+
for (const law of laws) {
|
|
550
|
+
if (law.problem || law.mode !== "run")
|
|
551
|
+
continue;
|
|
552
|
+
const entry = lawReports.get(law);
|
|
553
|
+
for (const [key, site] of law.expectSites) {
|
|
554
|
+
const reaching = law.instances.flatMap(instance => instance.obligations
|
|
555
|
+
.filter(o => o.kind === "expect" && `${o.file}:${o.line}:${o.column}` === key).map(obligation => ({ instance, obligation })));
|
|
556
|
+
const vacuous = () => entry.findings.push({
|
|
557
|
+
kind: "vacuous", message: "this expectation is never reached, so it proves nothing", ...site,
|
|
558
|
+
});
|
|
559
|
+
if (!reaching.length) {
|
|
560
|
+
vacuous();
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
const searches = reaching.map(({ instance, obligation }) => findInput(instance.binders, obligation.guard));
|
|
564
|
+
if (searches.some(s => s.found))
|
|
565
|
+
continue;
|
|
566
|
+
if (searches.every(s => s.exhaustive))
|
|
567
|
+
vacuous();
|
|
568
|
+
else {
|
|
569
|
+
const checks = reaching.flatMap(({ instance, obligation }) => checksFor(instance.binders, obligation.guard, ir.FALSE, undefined));
|
|
570
|
+
vacuityChecks.push({ entry, site, law, checks });
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (vacuityChecks.length) {
|
|
575
|
+
const all = [...new Set(vacuityChecks.flatMap(v => v.checks.map(c => c.statement)))];
|
|
576
|
+
const results = await prove(lean, FAST_HEADER, all);
|
|
577
|
+
const neverTrue = new Set(all.filter((_, i) => results[i].ok));
|
|
578
|
+
for (const { entry, site, law, checks } of vacuityChecks) {
|
|
579
|
+
if (checks.every(c => neverTrue.has(c.statement))) {
|
|
580
|
+
entry.findings.push({ kind: "vacuous", message: "the condition guarding this expectation is never true, so it proves nothing", ...site });
|
|
581
|
+
}
|
|
582
|
+
else
|
|
583
|
+
entry.notes.push(`line ${site.line}: no input satisfying the condition was found by sampling`);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
// Compare the model that was proved with what the real code did.
|
|
587
|
+
const executedOnly = new Map();
|
|
588
|
+
if (job && execution) {
|
|
589
|
+
const { outcomes, timedOut } = await execution;
|
|
590
|
+
for (const { file } of job.files) {
|
|
591
|
+
const outcome = outcomes.get(file);
|
|
592
|
+
for (const note of outcome?.rerouted ?? []) {
|
|
593
|
+
report.warnings.push(`the laws ran against the files TypeScript read, which are not the ones Node would load: ${note.replaceAll(cwd + "/", "")}`);
|
|
594
|
+
}
|
|
595
|
+
const reason = outcome?.error ?? (outcome ? undefined : timedOut ? `executing the laws took longer than ${RUNTIME_TIMEOUT_MS / 1000} s` : "the worker that executes laws stopped unexpectedly");
|
|
596
|
+
if (outcome?.throws) {
|
|
597
|
+
// The file itself fails when it is loaded, so nothing in it holds at run time, whatever was proved about the model.
|
|
598
|
+
for (const law of candidates.filter(candidate => candidate.file === file && candidate.mode === "run")) {
|
|
599
|
+
lawReports.get(law).findings.push({
|
|
600
|
+
kind: "throws", message: `the law file throws when it is loaded (${reason})`, file: law.file, line: law.line, column: law.column,
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
else if (outcome?.miscounted) {
|
|
605
|
+
// Laws that exist at run time but were never read would otherwise go unchecked without a word.
|
|
606
|
+
report.errors.push(`${displayPath(cwd, file)} registers ${outcome.miscounted.ran} ${outcome.miscounted.ran === 1 ? "law" : "laws"} when it runs, but Pudding read ${outcome.miscounted.read} from it; write each law as a plain law() or describe() call`);
|
|
607
|
+
}
|
|
608
|
+
else if (reason)
|
|
609
|
+
report.warnings.push(`${displayPath(cwd, file)} was not compared with the running code: ${reason}`);
|
|
610
|
+
for (const result of outcome?.laws ?? []) {
|
|
611
|
+
const law = candidates.find(candidate => candidate.file === file && candidate.index === result.index);
|
|
612
|
+
const entry = lawReports.get(law);
|
|
613
|
+
if (result.failure) {
|
|
614
|
+
const { kind, inputs, actual, expected } = result.failure;
|
|
615
|
+
entry.findings.push({
|
|
616
|
+
kind, inputs, file: law.file, line: law.line, column: law.column,
|
|
617
|
+
message: kind === "counterexample" ? "the law fails when executed"
|
|
618
|
+
: `the running code ${actual ? "satisfies" : "violates"} the law for this input, but the model Pudding proved things about ${expected ? "satisfies" : "violates"} it; the proof cannot be trusted`,
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
if (law.problem && result.executed)
|
|
622
|
+
executedOnly.set(law, result.exhaustive);
|
|
623
|
+
// Lean accepted every theorem of this law, yet the model those theorems describe fails here.
|
|
624
|
+
if (!law.problem && !entry.findings.length && result.modelFails) {
|
|
625
|
+
entry.findings.push({
|
|
626
|
+
kind: "mismatch", inputs: result.modelFails, file: law.file, line: law.line, column: law.column,
|
|
627
|
+
message: "Lean accepted the proof, but evaluating the same model on this input says the law fails; the proof cannot be trusted",
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
// Settle each law's status.
|
|
634
|
+
for (const law of laws) {
|
|
635
|
+
const entry = lawReports.get(law);
|
|
636
|
+
if (law.mode !== "run")
|
|
637
|
+
entry.status = law.mode === "todo" ? "todo" : "skipped";
|
|
638
|
+
if (law.problem) {
|
|
639
|
+
const { kind, message, file, line, column } = law.problem;
|
|
640
|
+
if (kind === "invalid")
|
|
641
|
+
entry.findings.push({ kind, message, file, line, column });
|
|
642
|
+
else if (!executedOnly.has(law)) {
|
|
643
|
+
entry.findings.push({ kind, message: `${message}; the law could not be executed instead, so nothing was checked`, file, line, column });
|
|
644
|
+
}
|
|
645
|
+
else {
|
|
646
|
+
entry.status = executedOnly.get(law) ? "checked" : "sampled";
|
|
647
|
+
entry.notes.unshift(`not proved: ${message} (${displayPath(cwd, file)}:${line}:${column})`);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (entry.findings.length)
|
|
651
|
+
entry.status = "failed";
|
|
652
|
+
if (config.strict && (entry.status === "checked" || entry.status === "sampled")) {
|
|
653
|
+
entry.status = "failed";
|
|
654
|
+
entry.findings.push({ kind: "unsupported", message: "strict mode requires a proof, and this law was only executed", file: law.file, line: law.line, column: law.column });
|
|
655
|
+
}
|
|
656
|
+
report.counts[entry.status]++;
|
|
657
|
+
report.cases += entry.cases;
|
|
658
|
+
report.laws.push(entry);
|
|
659
|
+
}
|
|
660
|
+
report.ok = report.counts.failed === 0 && report.errors.length === 0;
|
|
661
|
+
finish();
|
|
662
|
+
if (config.cache !== false) {
|
|
663
|
+
// A full run keeps only what it used. A run over part of the laws must not
|
|
664
|
+
// forget the rest, so it adds to what is there now, which another run may
|
|
665
|
+
// have written since this one started.
|
|
666
|
+
const kept = partial ? loadCache(cacheFile).proved.filter(hash => !used.has(hash)) : [];
|
|
667
|
+
const saved = { version: CACHE_VERSION, lean: cache.lean, proved: [...kept, ...used].slice(-CACHE_LIMIT) };
|
|
668
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
669
|
+
replaceFile(cacheFile, JSON.stringify(saved));
|
|
670
|
+
}
|
|
671
|
+
return report;
|
|
672
|
+
}
|