pud.dev 0.0.1 → 0.0.2
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 +53 -27
- package/THIRD-PARTY-NOTICES +112 -0
- package/chunk-224av1ab.js +10 -0
- package/chunk-fstxscck.js +3 -0
- package/chunk-h90k6v7w.js +3 -0
- package/chunk-k7a83qqm.js +3 -0
- package/cli.js +37 -0
- package/index.js +2 -0
- package/lean/index.js +880 -0
- package/package.json +28 -19
- package/project-runner.js +2 -0
- package/types/assistant.d.ts +200 -0
- package/types/check.d.ts +55 -0
- package/types/expressions.d.ts +47 -0
- package/types/input-scope.d.ts +4 -0
- package/types/lean/command.d.ts +14 -0
- package/types/lean/index.d.ts +5 -0
- package/types/lean/number.d.ts +4 -0
- package/types/lean/program.d.ts +26 -0
- package/types/lean/replay-predicate.d.ts +5 -0
- package/types/lean/replay.d.ts +6 -0
- package/types/lean/scalar.d.ts +23 -0
- package/types/lean/subject.d.ts +25 -0
- package/types/lean/syntax.d.ts +42 -0
- package/types/module.d.ts +9 -0
- package/types/prover-process.d.ts +7 -0
- package/types/run.d.ts +30 -0
- package/types/source.d.ts +5 -0
- package/types/suite-authoring.d.ts +117 -0
- package/types/suite-check.d.ts +10 -0
- package/types/suite.d.ts +44 -0
- package/CLAUDE.md +0 -106
- package/scripts/pack.ts +0 -67
- package/scripts/setup-lean.ts +0 -88
- package/src/assistant.ts +0 -131
- package/src/check.ts +0 -173
- package/src/cli.ts +0 -70
- package/src/expressions.ts +0 -65
- package/src/input-scope.ts +0 -71
- package/src/lean/command.ts +0 -40
- package/src/lean/index.ts +0 -437
- package/src/lean/iteration.lean +0 -145
- package/src/lean/lean-assets.d.ts +0 -4
- package/src/lean/number.lean +0 -427
- package/src/lean/number.ts +0 -25
- package/src/lean/program.ts +0 -110
- package/src/lean/replay-predicate.ts +0 -95
- package/src/lean/replay.ts +0 -136
- package/src/lean/scalar.lean +0 -183
- package/src/lean/scalar.ts +0 -196
- package/src/lean/subject.ts +0 -443
- package/src/lean/syntax.ts +0 -287
- package/src/module.ts +0 -110
- package/src/project-runner.ts +0 -26
- package/src/prover-process.ts +0 -16
- package/src/reporter.ts +0 -173
- package/src/run.ts +0 -60
- package/src/source.ts +0 -54
- package/src/suite-authoring.ts +0 -135
- package/src/suite-check.ts +0 -157
- package/src/suite.ts +0 -515
- package/src/terminal-progress.ts +0 -145
- package/src/terminal.ts +0 -41
- package/test/authoring.test.ts +0 -151
- package/test/lean.test.ts +0 -394
- package/test/module.test.ts +0 -55
- package/test/number.test.ts +0 -318
- package/test/package.test.ts +0 -170
- package/test/suite.test.ts +0 -257
- package/tsconfig.json +0 -7
- package/tsconfig.pack.json +0 -12
- /package/{src/index.ts → types/index.d.ts} +0 -0
package/test/number.test.ts
DELETED
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
import { afterEach, expect, test } from "bun:test";
|
|
2
|
-
import { cp, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { pathToFileURL } from "node:url";
|
|
6
|
-
import { check } from "../src/index.ts";
|
|
7
|
-
import { leanCommand } from "../src/lean/command.ts";
|
|
8
|
-
import { leanAdapter } from "../src/lean/index.ts";
|
|
9
|
-
import { numberSemantics } from "../src/lean/number.ts";
|
|
10
|
-
import { millisecondsToSeconds, secondsToMilliseconds } from "../.examples/motion/time-conversion.ts";
|
|
11
|
-
import { toCents } from "../.examples/cents.ts";
|
|
12
|
-
import { lowerProgram } from "../src/lean/program.ts";
|
|
13
|
-
|
|
14
|
-
const examples = join(import.meta.dir, "../.examples");
|
|
15
|
-
const toolchain = (await readFile(join(examples, "lean-toolchain"), "utf8")).trim();
|
|
16
|
-
const command = leanCommand({ file: join(examples, "cents.ts"), name: "toCents", source: "" }, { toolchain });
|
|
17
|
-
const leanTest = test.skipIf(!Bun.which(command.binary));
|
|
18
|
-
const adapter = leanAdapter({ toolchain, replay: true });
|
|
19
|
-
const directories: string[] = [];
|
|
20
|
-
async function fixture() {
|
|
21
|
-
const path = await mkdtemp(join(tmpdir(), "pud-number-"));
|
|
22
|
-
directories.push(path);
|
|
23
|
-
return path;
|
|
24
|
-
}
|
|
25
|
-
afterEach(async () => {
|
|
26
|
-
for (const path of directories.splice(0)) await rm(path, { recursive: true, force: true });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("native rounding regressions retain their exact binary64 results", () => {
|
|
30
|
-
expect(secondsToMilliseconds(0.0015)).toBe(1.5);
|
|
31
|
-
expect(secondsToMilliseconds(1.005)).toBe(1004.9999999999999);
|
|
32
|
-
expect(secondsToMilliseconds(millisecondsToSeconds(1001))).toBe(1000.9999999999999);
|
|
33
|
-
expect(Object.is(millisecondsToSeconds(-Number.MIN_VALUE), -0)).toBe(true);
|
|
34
|
-
expect([1.005, -1.125, 0.29].map(toCents)).toEqual([100, -112, 29]);
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
leanTest("Motion's quantified laws detect premature rounding and a lost direction, with native witnesses", async () => {
|
|
38
|
-
const dir = await fixture();
|
|
39
|
-
await cp(join(examples, "motion"), dir, { recursive: true });
|
|
40
|
-
const seconds = join(dir, "seconds.proof.ts"), millis = join(dir, "milliseconds.proof.ts");
|
|
41
|
-
const source = join(dir, "time-conversion.ts"), original = await readFile(source, "utf8");
|
|
42
|
-
expect(new Bun.CryptoHasher("sha256").update(original).digest("hex"))
|
|
43
|
-
.toBe("13e16d991b9c609279d87c52295daf0fe797f85c517f5a59c11e0d7e9947d3c7");
|
|
44
|
-
const results = async (file: string) => (await check(file, { adapter })).results;
|
|
45
|
-
expect((await results(seconds)).map(r => r.status)).toEqual(["proved", "proved", "proved"]);
|
|
46
|
-
expect((await results(millis)).map(r => r.status)).toEqual(["proved", "proved", "proved"]);
|
|
47
|
-
|
|
48
|
-
await writeFile(source, original.replace("seconds * 1000", "Math.round(seconds * 1000)"));
|
|
49
|
-
const [sign, scale, reverse] = await results(seconds);
|
|
50
|
-
expect(sign?.status).toBe("proved");
|
|
51
|
-
expect(scale?.status).toBe("disproved");
|
|
52
|
-
const duration = Number(scale!.counterexample!.input.seconds);
|
|
53
|
-
expect(Math.round(duration * 1000)).not.toBe(duration * 1000);
|
|
54
|
-
expect(reverse?.status).toBe("disproved");
|
|
55
|
-
const reversed = Number(reverse!.counterexample!.input.seconds);
|
|
56
|
-
expect(Math.round(-reversed * 1000) === -Math.round(reversed * 1000)).toBe(false);
|
|
57
|
-
|
|
58
|
-
await writeFile(source, original.replace("milliseconds / 1000", "milliseconds / -1000"));
|
|
59
|
-
const [, direction] = await results(millis);
|
|
60
|
-
expect(direction?.status).toBe("disproved");
|
|
61
|
-
const negative = Number(direction!.counterexample!.input.milliseconds);
|
|
62
|
-
expect(negative).toBeLessThan(0);
|
|
63
|
-
expect(negative / -1000 < 0 || 1 / (negative / -1000) < 0).toBe(false);
|
|
64
|
-
|
|
65
|
-
await writeFile(source, original.replace("milliseconds / 1000", "milliseconds / 100"));
|
|
66
|
-
const [wrongUnits, signKept, reversalKept] = await results(millis);
|
|
67
|
-
expect([wrongUnits?.status, signKept?.status, reversalKept?.status]).toEqual(["disproved", "proved", "proved"]);
|
|
68
|
-
const value = Number(wrongUnits!.counterexample!.input.milliseconds);
|
|
69
|
-
expect(value / 100).not.toBe(value / 1000);
|
|
70
|
-
}, 70_000);
|
|
71
|
-
|
|
72
|
-
leanTest("the exact cents law rejects wrong fractional results, tie policies and signs of zero", async () => {
|
|
73
|
-
const dir = await fixture(), proof = join(dir, "cents.proof.ts"), source = join(dir, "cents.ts");
|
|
74
|
-
await cp(join(examples, "cents.proof.ts"), proof);
|
|
75
|
-
const original = await readFile(join(examples, "cents.ts"), "utf8");
|
|
76
|
-
const result = async () => (await check(proof, { adapter })).results[0]!;
|
|
77
|
-
await writeFile(source, original);
|
|
78
|
-
expect(await result()).toMatchObject({ status: "proved", exactResult: true });
|
|
79
|
-
|
|
80
|
-
const law = await readFile(proof, "utf8");
|
|
81
|
-
await writeFile(proof, law.replace('.nearestIntegerTo(amount * 100, { ties: "up" })', ".refine(cents => Number.isInteger(cents))"));
|
|
82
|
-
expect(await result()).toMatchObject({
|
|
83
|
-
status: "disproved", counterexample: { input: { amount: "0" }, result: "0", alternative: "1" },
|
|
84
|
-
});
|
|
85
|
-
await writeFile(proof, law);
|
|
86
|
-
|
|
87
|
-
// Each mutant agrees with both former laws (whole scaled cents and the zero interval).
|
|
88
|
-
for (const [amount, wrong] of [[0.29, 28.9], [0.125, 12], [-0.125, -13]] as const) {
|
|
89
|
-
const body = `amount === ${amount} ? ${wrong} : Math.round(amount * 100)`;
|
|
90
|
-
await writeFile(source, original.replace("Math.round(amount * 100)", body));
|
|
91
|
-
expect(await result()).toMatchObject({
|
|
92
|
-
status: "disproved", counterexample: { input: { amount: String(amount) }, result: String(wrong) },
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
await writeFile(source, original.replace("Math.round(amount * 100)", "Math.round(amount * 100) + 0"));
|
|
96
|
-
expect(await result()).toMatchObject({ status: "disproved", counterexample: { input: { amount: "-0" }, result: "0" } });
|
|
97
|
-
}, 45_000);
|
|
98
|
-
|
|
99
|
-
leanTest("an exact scalar schema proves uniqueness and reports a second accepted answer", async () => {
|
|
100
|
-
const dir = await fixture(), proof = join(dir, "id.proof.ts");
|
|
101
|
-
await writeFile(join(dir, "id.ts"), "export const id = (x: bigint) => x; export const zero = (x: bigint) => x === 0n;");
|
|
102
|
-
await writeFile(proof, `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
103
|
-
import { id, zero } from "./id.ts";
|
|
104
|
-
describe(id, () => {
|
|
105
|
-
it("keeps the lower bound", { x: pud.bigint() }, ({ x }) => {
|
|
106
|
-
expect(id(x)).to(pud.bigint().gte(x));
|
|
107
|
-
});
|
|
108
|
-
it("determines the answer from the lower bound", { x: pud.bigint() }, ({ x }) => {
|
|
109
|
-
expect(id(x)).toExactly(pud.bigint().gte(x));
|
|
110
|
-
});
|
|
111
|
-
it("pins the result between matching bounds", { x: pud.bigint() }, ({ x }) => {
|
|
112
|
-
expect(id(x)).toExactly(pud.bigint().gte(x).lte(x));
|
|
113
|
-
});
|
|
114
|
-
it("identifies zero", { x: pud.bigint() }, ({ x }) => {
|
|
115
|
-
expect(zero(id(x))).toExactly(pud.boolean().eq(x === 0n));
|
|
116
|
-
});
|
|
117
|
-
it("leaves either boolean possible", { x: pud.bigint() }, ({ x }) => {
|
|
118
|
-
expect(zero(id(x))).toExactly(pud.boolean());
|
|
119
|
-
});
|
|
120
|
-
});`);
|
|
121
|
-
const [invariant, ambiguous, unique, boolean, either] = (await check(proof, { adapter })).results;
|
|
122
|
-
expect(invariant?.status).toBe("proved");
|
|
123
|
-
expect(ambiguous).toMatchObject({ status: "disproved", exactResult: true,
|
|
124
|
-
counterexample: { input: { x: "0n" }, result: "0n", alternative: "1n" } });
|
|
125
|
-
expect(unique).toMatchObject({ status: "proved", exactResult: true });
|
|
126
|
-
expect(boolean).toMatchObject({ status: "proved", exactResult: true });
|
|
127
|
-
expect(either).toMatchObject({ status: "disproved", counterexample: { result: "true", alternative: "false" } });
|
|
128
|
-
}, 15_000);
|
|
129
|
-
|
|
130
|
-
leanTest("exact number rules distinguish signed zeros and make the half-tie policy explicit", async () => {
|
|
131
|
-
const dir = await fixture(), proof = join(dir, "round.proof.ts");
|
|
132
|
-
await writeFile(join(dir, "round.ts"), "export const rounded = (x: number) => -Math.round(-x);");
|
|
133
|
-
await writeFile(proof, `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
134
|
-
import { rounded } from "./round.ts";
|
|
135
|
-
describe(rounded, () => {
|
|
136
|
-
it("takes the lower integer at a tie", { x: pud.number() }, ({ x }) => {
|
|
137
|
-
expect(rounded(x)).toExactly(pud.number().nearestIntegerTo(x, { ties: "down" }));
|
|
138
|
-
});
|
|
139
|
-
it("takes the upper integer at a tie", { x: pud.number() }, ({ x }) => {
|
|
140
|
-
expect(rounded(x)).toExactly(pud.number().nearestIntegerTo(x, { ties: "up" }));
|
|
141
|
-
});
|
|
142
|
-
it("leaves the sign unspecified", { x: pud.number().gte(0).lt(0.5) }, ({ x }) => {
|
|
143
|
-
expect(rounded(x)).toExactly(pud.number().eq(0));
|
|
144
|
-
});
|
|
145
|
-
});`);
|
|
146
|
-
const [down, up, zeros] = (await check(proof, { adapter })).results;
|
|
147
|
-
expect(down?.status).toBe("proved");
|
|
148
|
-
expect(up).toMatchObject({ status: "disproved", counterexample: { input: { x: "0.5" }, result: "0" } });
|
|
149
|
-
expect(zeros).toMatchObject({ status: "disproved", counterexample: { input: { x: "0" }, result: "0", alternative: "-0" } });
|
|
150
|
-
}, 15_000);
|
|
151
|
-
|
|
152
|
-
leanTest("an exact result requires the function to return on its declared domain", async () => {
|
|
153
|
-
const dir = await fixture(), proof = join(dir, "partial.proof.ts");
|
|
154
|
-
await writeFile(join(dir, "partial.ts"), `export function partial(x: bigint): bigint {
|
|
155
|
-
if (x < 0n) throw new RangeError();
|
|
156
|
-
return x;
|
|
157
|
-
}`);
|
|
158
|
-
await writeFile(proof, `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
159
|
-
import { partial } from "./partial.ts";
|
|
160
|
-
describe(partial, () => {
|
|
161
|
-
it("returns the nonnegative input", { x: pud.bigint().gte(0n) }, ({ x }) => {
|
|
162
|
-
expect(partial(x)).toExactly(pud.bigint().eq(x));
|
|
163
|
-
});
|
|
164
|
-
it("always returns the input", { x: pud.bigint() }, ({ x }) => {
|
|
165
|
-
expect(partial(x)).toExactly(pud.bigint().eq(x));
|
|
166
|
-
});
|
|
167
|
-
});`);
|
|
168
|
-
const [guarded, unguarded] = (await check(proof, { adapter: leanAdapter({ toolchain }) })).results;
|
|
169
|
-
expect(guarded).toMatchObject({ status: "proved" });
|
|
170
|
-
expect(unguarded).toMatchObject({ status: "unproved", exactResult: true, diagnostic: expect.any(String) });
|
|
171
|
-
}, 15_000);
|
|
172
|
-
|
|
173
|
-
// Compare each emitted operator against the same original expression evaluated in Bun.
|
|
174
|
-
const operators = [
|
|
175
|
-
["add", "number", "a + b"], ["sub", "number", "a - b"], ["mul", "number", "a * b"], ["div", "number", "a / b"],
|
|
176
|
-
["neg", "number", "-a"], ["round", "number", "Math.round(a)"], ["floor", "number", "Math.floor(a)"],
|
|
177
|
-
["ceil", "number", "Math.ceil(a)"], ["trunc", "number", "Math.trunc(a)"], ["choose", "number", "a ? a : b"],
|
|
178
|
-
["eq", "boolean", "a === b"], ["ne", "boolean", "a !== b"], ["lt", "boolean", "a < b"],
|
|
179
|
-
["le", "boolean", "a <= b"], ["gt", "boolean", "a > b"], ["ge", "boolean", "a >= b"], ["falsy", "boolean", "!a"],
|
|
180
|
-
["integer", "boolean", "Number.isInteger(a)"], ["nan", "boolean", "Number.isNaN(a)"],
|
|
181
|
-
] as const;
|
|
182
|
-
|
|
183
|
-
leanTest("emitted binary64 operations agree with Bun on boundary pairs and seeded raw bit patterns", async () => {
|
|
184
|
-
const dir = await fixture(), file = join(dir, "operators.ts");
|
|
185
|
-
const source = operators.map(([name, type, expression]) =>
|
|
186
|
-
`export const ${name} = (a: number, b: number): ${type} => ${expression};`).join("\n");
|
|
187
|
-
await writeFile(file, source);
|
|
188
|
-
const native = await import(pathToFileURL(file).href);
|
|
189
|
-
const program = lowerProgram({ file, source, name: "add" });
|
|
190
|
-
const subjects = operators.map(([name]) => program.resolve(file, name));
|
|
191
|
-
const observations = subjects.map(subject =>
|
|
192
|
-
`${subject.result === "number" ? "JSNumber.toBits" : "Bool.toNat"} (${subject.code} a b)`);
|
|
193
|
-
observations.push("JSNumber.toBits (JSNumber.nearestInteger a true)", "JSNumber.toBits (JSNumber.nearestInteger a false)");
|
|
194
|
-
const runner = join(dir, "Numbers.lean");
|
|
195
|
-
await writeFile(runner, `import Std
|
|
196
|
-
${numberSemantics}
|
|
197
|
-
open Pud
|
|
198
|
-
set_option linter.unusedVariables false
|
|
199
|
-
${subjects.map(subject => subject.definition).join("\n")}
|
|
200
|
-
def main (args : List String) : IO Unit := do
|
|
201
|
-
let input ← IO.FS.readFile args.head!
|
|
202
|
-
for line in input.splitOn "\\n" do
|
|
203
|
-
let words := (line.splitOn " ").map String.toNat!
|
|
204
|
-
let a := JSNumber.ofBits words[0]!
|
|
205
|
-
let b := JSNumber.ofBits words[1]!
|
|
206
|
-
let actual : List Nat := [${observations.join(", ")}]
|
|
207
|
-
unless actual == words.drop 2 do
|
|
208
|
-
throw (IO.userError s!"inputs {words.take 2}, expected {words.drop 2}, obtained {actual}")
|
|
209
|
-
`);
|
|
210
|
-
const view = new DataView(new ArrayBuffer(8));
|
|
211
|
-
function bits(x: number) {
|
|
212
|
-
view.setFloat64(0, x);
|
|
213
|
-
return view.getBigUint64(0);
|
|
214
|
-
}
|
|
215
|
-
function number(word: bigint) {
|
|
216
|
-
view.setBigUint64(0, word);
|
|
217
|
-
return view.getFloat64(0);
|
|
218
|
-
}
|
|
219
|
-
const boundary = [0, -0, 0.1, 0.2, 0.5, -0.5, 0.5 - 2 ** -54, -(0.5 + 2 ** -53), 1.005, -1.125, 0.0015,
|
|
220
|
-
1, 1 + Number.EPSILON, 1.5, 2 ** -53, 3 * 2 ** -53, 100, 1000, 2 ** 52, 2 ** 53, 2 ** 970,
|
|
221
|
-
Number.MIN_VALUE, -Number.MIN_VALUE, 2 ** -1022, 2 ** -1022 - Number.MIN_VALUE,
|
|
222
|
-
Number.MAX_VALUE, -Number.MAX_VALUE, Infinity, -Infinity].map(bits);
|
|
223
|
-
boundary.push(0x7ff0000000000001n, 0xfff8000000000001n);
|
|
224
|
-
let seed = 0xa4093822299f31d0n;
|
|
225
|
-
function next() {
|
|
226
|
-
seed ^= BigInt.asUintN(64, seed << 13n);
|
|
227
|
-
seed ^= seed >> 7n;
|
|
228
|
-
seed ^= BigInt.asUintN(64, seed << 17n);
|
|
229
|
-
return seed;
|
|
230
|
-
}
|
|
231
|
-
const pairs = boundary.flatMap(a => boundary.map(b => [a, b] as const));
|
|
232
|
-
for (let i = 0; i < 2048; i++) pairs.push([next(), next()]);
|
|
233
|
-
// Exercise both sides of every change in integer spacing and of the half tie in each binade.
|
|
234
|
-
for (let exponent = -1; exponent <= 52; exponent++) {
|
|
235
|
-
for (const center of [2 ** exponent, 2 ** exponent + 0.5]) {
|
|
236
|
-
for (const offset of [-1n, 0n, 1n]) {
|
|
237
|
-
const value = number(bits(center) + offset);
|
|
238
|
-
for (const signed of [value, -value]) pairs.push([bits(signed), bits(1)]);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
const rows = pairs.map(([a, b]) => {
|
|
243
|
-
const results = operators.map(([name]) => {
|
|
244
|
-
const value = native[name](number(a), number(b));
|
|
245
|
-
if (typeof value === "boolean") return Number(value);
|
|
246
|
-
return Number.isNaN(value) ? 0x7ff8000000000000n : bits(value);
|
|
247
|
-
});
|
|
248
|
-
const x = number(a);
|
|
249
|
-
const policies = [Math.round(x), -Math.round(-x)].map(n => Number.isNaN(n) ? 0x7ff8000000000000n : bits(n));
|
|
250
|
-
return [a, b, ...results, ...policies].join(" ");
|
|
251
|
-
});
|
|
252
|
-
const vectors = join(dir, "vectors");
|
|
253
|
-
await writeFile(vectors, rows.join("\n"));
|
|
254
|
-
const run = Bun.spawnSync([command.binary, "--run", runner, vectors], { env: command.env, timeout: 15_000 });
|
|
255
|
-
expect({ status: run.exitCode, output: run.stdout.toString() + run.stderr.toString() }).toEqual({ status: 0, output: "" });
|
|
256
|
-
}, 20_000);
|
|
257
|
-
|
|
258
|
-
leanTest("numeric proof matchers use JavaScript equality and truthiness", async () => {
|
|
259
|
-
const dir = await fixture(), proof = join(dir, "value.proof.ts");
|
|
260
|
-
await writeFile(join(dir, "value.ts"), "export const identity = (x: number) => x; export const nan = () => 0 / 0;");
|
|
261
|
-
const imports = `import { describe, expect, it, pud } from "@pud.dev/lib";
|
|
262
|
-
import { identity, nan } from "./value.ts";`;
|
|
263
|
-
await writeFile(proof, imports + `
|
|
264
|
-
describe(identity, () => {
|
|
265
|
-
it("compares the result to itself", { x: pud.number() }, ({ x }) => {
|
|
266
|
-
expect(identity(x)).toEqual(identity(x));
|
|
267
|
-
});
|
|
268
|
-
});`);
|
|
269
|
-
expect((await check(proof, { adapter })).results[0]).toMatchObject({
|
|
270
|
-
status: "disproved", counterexample: { input: { x: "NaN" }, summary: expect.stringContaining("strict equality") },
|
|
271
|
-
});
|
|
272
|
-
await writeFile(proof, imports + `
|
|
273
|
-
describe(nan, () => {
|
|
274
|
-
it("treats NaN as falsy and unordered", {}, ({}) => {
|
|
275
|
-
expect(nan()).to(pud.number().refine(x => !x && x !== x && !(x >= 0) && !(x < 0)));
|
|
276
|
-
});
|
|
277
|
-
});`);
|
|
278
|
-
expect((await check(proof, { adapter })).results[0]).toMatchObject({ status: "proved", concreteScope: "no-inputs" });
|
|
279
|
-
await writeFile(proof, imports + `
|
|
280
|
-
describe(identity, () => {
|
|
281
|
-
it("uses the global Number with its real binding", { x: pud.number() }, ({ x }) => {
|
|
282
|
-
const Number = identity(x);
|
|
283
|
-
expect(Number).to(pud.number().refine(value => Number.isInteger(value)));
|
|
284
|
-
});
|
|
285
|
-
});`);
|
|
286
|
-
expect((await check(proof, { adapter })).results[0]).toMatchObject({ status: "unsupported", detail: expect.stringContaining("unshadowed Number") });
|
|
287
|
-
}, 10_000);
|
|
288
|
-
|
|
289
|
-
leanTest("replay respects scalar guards and leaves an unsupported quantified premise open", async () => {
|
|
290
|
-
const dir = await fixture(), proof = join(dir, "id.proof.ts");
|
|
291
|
-
await cp(join(examples, "pud.config.ts"), join(dir, "pud.config.ts"));
|
|
292
|
-
await writeFile(join(dir, "id.ts"), "export const id = (x: number) => x;");
|
|
293
|
-
await writeFile(proof, [
|
|
294
|
-
'import pud from "./pud.config.ts";',
|
|
295
|
-
'import { lean } from "@pud.dev/lib/lean";',
|
|
296
|
-
'import { id } from "./id.ts";',
|
|
297
|
-
'pud.describe(id, ({ it, expect, given }) => {',
|
|
298
|
-
' it("keeps a nonnegative integer", { x: pud.number() }, ({ x: amount }) => {',
|
|
299
|
-
' given(Number.isInteger(amount) && amount >= 0);',
|
|
300
|
-
' expect(id(amount)).to(pud.number().gte(0));',
|
|
301
|
-
' }).by(lean`by fail "exercise native replay"`);',
|
|
302
|
-
' it("has an unexecutable premise", { x: pud.number() }, ({ x }) => {',
|
|
303
|
-
' given(pud.forall(pud.number(), n => n === x));',
|
|
304
|
-
' expect(id(x)).to(pud.number().eq(0));',
|
|
305
|
-
' }).by(lean`by fail "exercise native replay"`);',
|
|
306
|
-
'});',
|
|
307
|
-
].join("\n"));
|
|
308
|
-
expect((await check(proof, { adapter })).results.map(r => r.status)).toEqual(["unproved", "unproved"]);
|
|
309
|
-
}, 10_000);
|
|
310
|
-
|
|
311
|
-
test("mixed numeric operations, unmodeled operators and a shadowed Math.round stay outside the model", () => {
|
|
312
|
-
const input = (source: string) => lowerProgram({ file: "input.ts", source, name: "f" });
|
|
313
|
-
expect(() => input("export const f = (n: number) => n + 1n;")).toThrow("Expected JSNumber");
|
|
314
|
-
expect(() => input("export const f = (n: number) => n % 3;")).toThrow("Unsupported number operator '%'");
|
|
315
|
-
expect(() => input("export const f = (Math: number) => Math.round(1);")).toThrow("unshadowed Math");
|
|
316
|
-
expect(() => input("export const f = (Number: number) => Number.isInteger(Number);")).toThrow("unshadowed Number");
|
|
317
|
-
expect(() => input("const Math = 0; export const f = (n: number) => Math.round(n);")).toThrow("unshadowed Math");
|
|
318
|
-
});
|
package/test/package.test.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "bun:test";
|
|
2
|
-
import { cp, mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join, resolve } from "node:path";
|
|
5
|
-
import { pack } from "../scripts/pack.ts";
|
|
6
|
-
import { leanCommand } from "../src/lean/command.ts";
|
|
7
|
-
|
|
8
|
-
test("a Bun project can install the archive offline and use the command, API, and driver types", async () => {
|
|
9
|
-
const dir = await mkdtemp(join(tmpdir(), "pud-consumer-"));
|
|
10
|
-
const cwd = join(dir, "app");
|
|
11
|
-
const source = join(import.meta.dir, "../.examples/clamp.ts");
|
|
12
|
-
const lean = leanCommand({ file: source, name: "clamp", source: "" }, {});
|
|
13
|
-
const env = { ...lean.env, PUD_LEAN: lean.binary, BUN_INSTALL_CACHE_DIR: join(dir, "empty-cache"), NO_COLOR: "1" };
|
|
14
|
-
function run(args: string[], code = 0) {
|
|
15
|
-
const child = Bun.spawnSync([process.execPath, ...args], { cwd, env, stdout: "pipe", stderr: "pipe" });
|
|
16
|
-
if (child.exitCode !== code) throw new Error(`${args.join(" ")} (exit ${child.exitCode})\n${child.stdout}\n${child.stderr}`);
|
|
17
|
-
return { stdout: child.stdout.toString(), stderr: child.stderr.toString() };
|
|
18
|
-
}
|
|
19
|
-
const config = (options = "") => [
|
|
20
|
-
'import "./diagnostics.cjs";',
|
|
21
|
-
'import { defineConfig } from "@pud.dev/lib";',
|
|
22
|
-
'import { leanAdapter } from "@pud.dev/lib/lean";',
|
|
23
|
-
`export default defineConfig({ adapter: leanAdapter(${options}) });`,
|
|
24
|
-
].join("\n");
|
|
25
|
-
try {
|
|
26
|
-
const archive = await pack(dir);
|
|
27
|
-
expect(archive).toEndWith("pud.dev-lib-0.0.1.tgz");
|
|
28
|
-
await mkdir(cwd);
|
|
29
|
-
await writeFile(join(cwd, "package.json"), JSON.stringify({ name: "consumer", private: true, type: "module" }));
|
|
30
|
-
run(["add", "--dev", "--offline", archive]);
|
|
31
|
-
await cp(join(import.meta.dir, "../.examples/motion"), join(cwd, "motion"), { recursive: true });
|
|
32
|
-
await writeFile(join(cwd, "quote.ts"), "export function quote(cents: bigint): bigint { return cents - 1n; }");
|
|
33
|
-
const configFile = join(cwd, "pud.config.ts");
|
|
34
|
-
await writeFile(join(cwd, "diagnostics.cjs"), 'console.log("Loading project config"); process.stdout.write("Using Lean\\n");');
|
|
35
|
-
await writeFile(configFile, config('{ executable: "./missing-lean" }'));
|
|
36
|
-
await writeFile(join(cwd, "quote.proof.ts"), [
|
|
37
|
-
'import { describe, expect, it, pud } from "@pud.dev/lib";',
|
|
38
|
-
'import { quote } from "./quote";',
|
|
39
|
-
'describe(quote, () => {',
|
|
40
|
-
' it("subtracts one cent", { cents: pud.bigint() }, ({ cents }) => {',
|
|
41
|
-
' expect(quote(cents)).to(pud.bigint().eq(cents - 1n));',
|
|
42
|
-
' });',
|
|
43
|
-
'});',
|
|
44
|
-
].join("\n"));
|
|
45
|
-
await writeFile(join(cwd, "native.proof.ts"), [
|
|
46
|
-
'import pud from "./pud.config";',
|
|
47
|
-
'import { lean } from "@pud.dev/lib/lean";',
|
|
48
|
-
'import { quote } from "./quote";',
|
|
49
|
-
'pud.describe(quote, ({ it, expect }) => {',
|
|
50
|
-
' it("subtracts one cent", { cents: pud.bigint() }, ({ cents }) => {',
|
|
51
|
-
` expect(quote(cents)).to(pud.bigint().satisfies(lean\`fun (result : Int) => result = \${cents} - 1\`));`,
|
|
52
|
-
' }).by(lean`by intro cents; rfl`);',
|
|
53
|
-
'});',
|
|
54
|
-
].join("\n"));
|
|
55
|
-
await writeFile(join(cwd, "types.ts"), [
|
|
56
|
-
'import { check, checkFiles, defineConfig, emit, expect, native, pud, type CheckReport, type ProofCall, type ProofClaim } from "@pud.dev/lib";',
|
|
57
|
-
'import { lean } from "@pud.dev/lib/lean";',
|
|
58
|
-
'import { quote } from "./quote";',
|
|
59
|
-
'const report: CheckReport = await check("quote.proof.ts");',
|
|
60
|
-
'const reports: CheckReport[] = await checkFiles(["quote.proof.ts"]);',
|
|
61
|
-
'const unit: string = await emit("quote.proof.ts", "subtracts one cent");',
|
|
62
|
-
'declare const reference: ProofCall;',
|
|
63
|
-
'const equality: ProofClaim = { kind: "equal", premises: [], assertions: [], reference };',
|
|
64
|
-
'// @ts-expect-error a call equality and an expected exception are separate claims',
|
|
65
|
-
'const contradictory: ProofClaim = { ...equality, expectedError: "RangeError" };',
|
|
66
|
-
'pud.number().gte(0).eq(1.5);',
|
|
67
|
-
'declare const amount: number, values: readonly bigint[];',
|
|
68
|
-
'expect(amount).toExactly(pud.number().nearestIntegerTo(amount, { ties: "up" }));',
|
|
69
|
-
'expect(amount).toExactly(pud.number().sameValue(amount));',
|
|
70
|
-
'// @ts-expect-error select a supported tie policy',
|
|
71
|
-
'pud.number().nearestIntegerTo(amount, { ties: "away" });',
|
|
72
|
-
'// @ts-expect-error exact result schemas currently support scalar returns',
|
|
73
|
-
'expect(values).toExactly(pud.array(pud.bigint()));',
|
|
74
|
-
'// @ts-expect-error a number schema cannot compare against a bigint',
|
|
75
|
-
'pud.number().eq(1n);',
|
|
76
|
-
'// @ts-expect-error portable helpers require a configured native scope',
|
|
77
|
-
'pud.bigint().satisfies(lean`fun _ => True`);',
|
|
78
|
-
'const other = defineConfig({ adapter: { id: "other", prepare() { throw new Error("type fixture"); } } });',
|
|
79
|
-
'const term = native("other");',
|
|
80
|
-
'other.describe(quote, ({ it, expect, use }) => {',
|
|
81
|
-
' // @ts-expect-error a Lean module cannot be used with the other adapter',
|
|
82
|
-
' use(lean.module("./helper.lean"));',
|
|
83
|
-
' use(term.module("./helper.other"));',
|
|
84
|
-
' const claim = it("subtracts one", { cents: other.bigint() }, ({ cents }) => {',
|
|
85
|
-
' // @ts-expect-error the inferred argument is bigint',
|
|
86
|
-
' const wrong: string = cents;',
|
|
87
|
-
' // @ts-expect-error a Lean term cannot be used with the other adapter',
|
|
88
|
-
' expect(quote(cents)).to(other.bigint().satisfies(lean`fun _ => True`));',
|
|
89
|
-
' });',
|
|
90
|
-
' // @ts-expect-error a Lean theorem cannot certify a property checked by the other adapter',
|
|
91
|
-
' claim.by(lean.ref("Quote.correct"));',
|
|
92
|
-
' claim.by(term.ref("Quote.correct"));',
|
|
93
|
-
'});',
|
|
94
|
-
].join("\n"));
|
|
95
|
-
await writeFile(join(cwd, "tsconfig.json"), JSON.stringify({
|
|
96
|
-
compilerOptions: { module: "preserve", target: "ESNext", strict: true, types: [], noEmit: true, allowImportingTsExtensions: true },
|
|
97
|
-
}));
|
|
98
|
-
const tsc = resolve(import.meta.dir, "../../../node_modules/typescript/bin/tsc");
|
|
99
|
-
run([tsc, "-p", "."]);
|
|
100
|
-
const { stdout: unit, stderr: emitLog } = run(["run", "pud", "--emit", "native.proof.ts"]);
|
|
101
|
-
expect(emitLog).toContain("Loading project config\nUsing Lean");
|
|
102
|
-
expect(unit).toStartWith("import Std\n");
|
|
103
|
-
expect(unit).toContain("def impl (p0 : Int) : Int");
|
|
104
|
-
expect(unit).toContain("result = (p0) - 1");
|
|
105
|
-
expect(unit).toContain("theorem checked : claim");
|
|
106
|
-
expect(run(["-e", 'import { emit } from "@pud.dev/lib"; process.stdout.write(await emit("native.proof.ts"))']).stdout).toBe(unit);
|
|
107
|
-
|
|
108
|
-
const missing = run(["run", "pud", "check", "quote.proof.ts", "--json"], 2);
|
|
109
|
-
expect(missing.stderr).toContain("Loading project config\nUsing Lean");
|
|
110
|
-
expect(JSON.parse(missing.stdout).files[0].results[0]).toMatchObject({ status: "error", assistant: "lean" });
|
|
111
|
-
if (Bun.which(lean.binary)) {
|
|
112
|
-
await writeFile(configFile, config());
|
|
113
|
-
const passed = run(["x", "--no-install", "pud", "check", "quote.proof.ts", "--json"]);
|
|
114
|
-
expect(JSON.parse(passed.stdout).files[0].results[0]).toMatchObject({ status: "proved", assistant: "lean" });
|
|
115
|
-
const motion = run(["run", "pud", "check", "motion"]);
|
|
116
|
-
expect(motion.stdout).toContain("6 passed");
|
|
117
|
-
expect(motion.stdout.match(/\[exact result\]/g)).toHaveLength(2);
|
|
118
|
-
|
|
119
|
-
await writeFile(join(cwd, "quote.ts"), "export function quote(cents: bigint): bigint { return cents - 2n; }");
|
|
120
|
-
const rejected = run(["run", "pud", "check", "quote.proof.ts", "--json"], 1);
|
|
121
|
-
expect(JSON.parse(rejected.stdout).files[0].results[0]).toMatchObject({ status: "unproved", assistant: "lean" });
|
|
122
|
-
await writeFile(join(cwd, "quote.ts"), "export function quote(cents: bigint): bigint { return cents - 1n; }");
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const alternate = join(cwd, "alternate");
|
|
126
|
-
await mkdir(alternate);
|
|
127
|
-
await writeFile(join(alternate, "identity.proof.ts"), [
|
|
128
|
-
'import { describe, expect, it, pud } from "@pud.dev/lib";',
|
|
129
|
-
'import { quote } from "../quote";',
|
|
130
|
-
'describe(quote, () => {',
|
|
131
|
-
' it("preserves the amount", { cents: pud.bigint() }, ({ cents }) => {',
|
|
132
|
-
' expect(quote(cents)).to(pud.bigint().eq(cents));',
|
|
133
|
-
' });',
|
|
134
|
-
'});',
|
|
135
|
-
].join("\n"));
|
|
136
|
-
await writeFile(join(alternate, "pud.config.ts"), [
|
|
137
|
-
'import { defineConfig, type ProofAssistant } from "@pud.dev/lib";',
|
|
138
|
-
'import { quote } from "../quote";',
|
|
139
|
-
'const adapter: ProofAssistant<"replay-test"> = {',
|
|
140
|
-
' id: "replay-test",',
|
|
141
|
-
' prepare() {',
|
|
142
|
-
' return {',
|
|
143
|
-
' parameters: [{ name: "cents", type: { kind: "integer" } }], result: { kind: "integer" },',
|
|
144
|
-
' emit() { return ""; },',
|
|
145
|
-
' async verify(claims) {',
|
|
146
|
-
' const result = quote(0n);',
|
|
147
|
-
' const received = result.toString() + "n";',
|
|
148
|
-
' return claims.map(() => result === 0n ? { status: "unproved", detail: "No witness" } : {',
|
|
149
|
-
' status: "disproved", counterexample: { summary: "The amount changed.", input: { cents: "0n" },',
|
|
150
|
-
' result: received, difference: { subject: "Quote", expected: "0n", received } },',
|
|
151
|
-
' });',
|
|
152
|
-
' },',
|
|
153
|
-
' };',
|
|
154
|
-
' },',
|
|
155
|
-
'};',
|
|
156
|
-
'export default defineConfig({ adapter });',
|
|
157
|
-
].join("\n"));
|
|
158
|
-
const replay = run(["run", "pud", "check", "alternate"], 1);
|
|
159
|
-
expect(replay.stdout).toBe("");
|
|
160
|
-
expect(replay.stderr).toContain("FAIL preserves the amount");
|
|
161
|
-
expect(replay.stderr).toContain("Expected 0n");
|
|
162
|
-
expect(replay.stderr).toContain("Received -1n");
|
|
163
|
-
|
|
164
|
-
const notices = await readFile(join(cwd, "node_modules/@pud.dev/lib/THIRD-PARTY-NOTICES"), "utf8");
|
|
165
|
-
expect(notices).toContain("@babel/parser@");
|
|
166
|
-
expect(notices).toContain("chalk@");
|
|
167
|
-
} finally {
|
|
168
|
-
await rm(dir, { recursive: true, force: true });
|
|
169
|
-
}
|
|
170
|
-
}, 40_000);
|