pud.dev 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,257 @@
1
+ import { expect, test } from "bun:test";
2
+ import { writeFileSync } from "node:fs";
3
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { check, checkFiles, emit, Unsupported, type CheckProgress, type ClaimProgress, type ProofAssistant, type ProofClaim, type ProofModule } from "../src/index.ts";
7
+ import { leanAdapter } from "../src/lean/index.ts";
8
+
9
+ const increment = "export function increment(value: bigint): bigint { return value + 1n; }";
10
+ const portable = `import { describe, expect, given, it, pud } from "@pud.dev/lib";
11
+ import { increment } from "./increment.ts";
12
+ describe(increment, () => {
13
+ it("increments the input", { value: pud.int().gte(0n) }, ({ value: input }) => {
14
+ given(input !== 7n);
15
+ expect(increment(input)).to(pud.bigint().eq(input + 1n));
16
+ });
17
+ it.todo("a future property");
18
+ });`;
19
+
20
+ function adapter(verify: (claims: readonly ProofClaim[], modules: readonly ProofModule[], progress?: ClaimProgress) => void | Promise<void>): ProofAssistant<"test"> {
21
+ return {
22
+ id: "test",
23
+ prepare(subject) {
24
+ expect(subject.name).toBe("increment");
25
+ expect(subject.source).toBe(increment);
26
+ return {
27
+ parameters: [{ name: "value", type: { kind: "integer" } }],
28
+ result: { kind: "integer" },
29
+ emit: claim => JSON.stringify(claim.assertions.map(assertion => assertion.kind)),
30
+ async verify(claims, modules, progress) {
31
+ await verify(claims, modules, progress);
32
+ return claims.map(() => ({ status: "unproved", detail: "Test adapter received the claim." }));
33
+ },
34
+ };
35
+ },
36
+ };
37
+ }
38
+
39
+ async function fixture(run: (directory: string) => Promise<void>) {
40
+ const dir = await mkdtemp(join(tmpdir(), "pud-suite-"));
41
+ try {
42
+ await writeFile(join(dir, "increment.ts"), increment);
43
+ await run(dir);
44
+ } finally {
45
+ await rm(dir, { recursive: true, force: true });
46
+ }
47
+ }
48
+
49
+ test("an explicit adapter receives the portable claim and the frozen TypeScript implementation", async () => {
50
+ await fixture(async dir => {
51
+ const file = join(dir, "increment.proof.ts");
52
+ await writeFile(file, portable);
53
+ const selected = adapter((claims, modules) => {
54
+ expect(modules).toEqual([]);
55
+ expect(claims).toHaveLength(1);
56
+ expect(claims[0]?.kind).toBe("return");
57
+ expect(claims[0]?.premises.map(p => p.kind)).toEqual(["comparison", "condition"]);
58
+ expect(claims[0]?.assertions[0]).toMatchObject({ kind: "comparison", operator: "eq", value: { source: "input + 1n" } });
59
+ const clause = claims[0]?.assertions[0];
60
+ if (clause?.kind !== "comparison") throw new Error("Expected a comparison.");
61
+ expect([...clause.value.bindings]).toEqual([["input", "value"]]);
62
+ });
63
+ expect((await check(file, { adapter: selected })).results).toMatchObject([
64
+ { status: "unproved", assistant: "test", name: "increments the input" },
65
+ { status: "todo", name: "a future property" },
66
+ ]);
67
+ expect(await emit(file, { adapter: selected, property: "increments the input" })).toBe('["comparison"]');
68
+ });
69
+ });
70
+
71
+ test("the nearest project config selects the adapter and edited config is reloaded", async () => {
72
+ await fixture(async dir => {
73
+ const projects = join(dir, "project");
74
+ await mkdir(projects);
75
+ await writeFile(join(projects, "increment.ts"), increment);
76
+ const file = join(projects, "increment.proof.ts");
77
+ await writeFile(file, portable);
78
+ function config(id: string) {
79
+ return `export default { adapter: {
80
+ id: "${id}",
81
+ prepare() {
82
+ return {
83
+ parameters: [{ name: "value", type: { kind: "integer" } }],
84
+ result: { kind: "integer" },
85
+ emit() { return "${id}"; },
86
+ async verify(claims) { return claims.map(() => ({ status: "unproved", detail: "Fixture" })); },
87
+ };
88
+ },
89
+ } };`;
90
+ }
91
+ await writeFile(join(dir, "pud.config.ts"), config("outer"));
92
+ const local = join(projects, "pud.config.ts");
93
+ await writeFile(local, config("inner"));
94
+ expect(await emit(file)).toBe("inner");
95
+ await writeFile(local, config("changed"));
96
+ expect((await check(file)).results[0]).toMatchObject({ assistant: "changed", status: "unproved" });
97
+ });
98
+ });
99
+
100
+ test("a batch shares a config instance and concurrent runs reload its transitive imports", async () => {
101
+ await fixture(async dir => {
102
+ const first = join(dir, "first.proof.ts"), second = join(dir, "second.proof.ts");
103
+ const settings = join(dir, "settings.ts");
104
+ await writeFile(first, portable);
105
+ await writeFile(second, portable);
106
+ await writeFile(settings, 'export const label = "old";');
107
+ await writeFile(join(dir, "options.ts"), 'export { label } from "./settings.ts";');
108
+ await writeFile(join(dir, "pud.config.ts"), `import { label } from "./options.ts";
109
+ let checks = 0;
110
+ export default { adapter: { id: "local", prepare() {
111
+ return { parameters: [{ name: "value", type: { kind: "integer" } }], result: { kind: "integer" },
112
+ emit() { return label; },
113
+ async verify() { return [{ status: "unproved", detail: label + ++checks }]; },
114
+ };
115
+ } } };`);
116
+ expect(await emit(first)).toBe("old");
117
+ await writeFile(settings, 'export const label = "new";');
118
+ const [batch, single, unit] = await Promise.all([checkFiles([first, second]), check(first), emit(first)]);
119
+ expect(batch.map(report => report.results[0]?.detail)).toEqual(["new1", "new2"]);
120
+ expect(single.results[0]?.detail).toBe("new1");
121
+ expect(unit).toBe("new");
122
+
123
+ const report = await check(first, { onProgress(event) {
124
+ if (event.kind === "validating") writeFileSync(join(dir, "increment.ts"), increment.replace("+ 1n", "- 1n"));
125
+ } });
126
+ expect(report.results[0]).toMatchObject({ status: "error", detail: expect.stringContaining("changed during verification") });
127
+ });
128
+ });
129
+
130
+ test("each active property finishes once across binding errors, adapter updates and returned verdicts", async () => {
131
+ await fixture(async dir => {
132
+ const file = join(dir, "increment.proof.ts");
133
+ await writeFile(file, portable.replace('it.todo("a future property");', `
134
+ it.todo("a future property");
135
+ it("invalid", {}, ({}) => {});
136
+ it("positive step", { value: pud.bigint() }, ({ value }) => {
137
+ expect(increment(value)).to(pud.bigint().gt(value));
138
+ });`));
139
+ const selected = adapter((_claims, _modules, progress) => {
140
+ progress?.(0, "finished", "unproved");
141
+ progress?.(0, "finished", "unproved");
142
+ progress?.(0, "running");
143
+ progress?.(1, "finished");
144
+ });
145
+ const events: CheckProgress[] = [];
146
+ const { results } = await check(file, { adapter: selected, onProgress: event => events.push(event) });
147
+ expect(results.map(result => result.status)).toEqual(["unproved", "todo", "unsupported", "unproved"]);
148
+ expect(events.filter(event => event.kind === "finished").map(event => [event.indices, event.statuses]))
149
+ .toEqual([[[2], ["unsupported"]], [[0], ["unproved"]], [[3], ["unproved"]]]);
150
+ expect(events.filter(event => event.kind === "running").map(event => event.indices)).toEqual([[0, 3]]);
151
+ expect(events.at(-1)).toEqual({ kind: "validating" });
152
+ });
153
+ });
154
+
155
+ test("configured runs preserve emission errors and report a project that exits early", async () => {
156
+ await fixture(async dir => {
157
+ const file = join(dir, "increment.proof.ts"), config = join(dir, "pud.config.ts");
158
+ await expect(emit(file)).rejects.toMatchObject({ code: "ENOENT" });
159
+ await writeFile(file, "export default {};");
160
+ await expect(emit(file)).rejects.toBeInstanceOf(Unsupported);
161
+ await writeFile(file, portable);
162
+ await writeFile(config, 'throw new TypeError("Invalid project settings");');
163
+ await expect(emit(file)).rejects.toBeInstanceOf(TypeError);
164
+ await writeFile(config, "process.exit(7);");
165
+ expect((await check(file)).results).toMatchObject([{ status: "error", detail: expect.stringContaining("exited with 7") }]);
166
+ });
167
+ });
168
+
169
+ test("the Lean adapter accepts an untagged equality and rejects a contradictory claim", async () => {
170
+ await fixture(async dir => {
171
+ const subject = await leanAdapter().prepare({ file: join(dir, "increment.ts"), source: increment, name: "increment" });
172
+ const reference = { kind: "call", callee: "subject", args: [{ kind: "parameter", name: "value" }] } as const;
173
+ const legacy = JSON.parse(JSON.stringify({ premises: [], assertions: [], reference }));
174
+ expect(await subject.emit(legacy, [])).toBe(await subject.emit({ kind: "equal", premises: [], assertions: [], reference }, []));
175
+ const mixed = { ...legacy, kind: "throw", expectedError: "RangeError" };
176
+ expect(await subject.verify([mixed], [])).toMatchObject([{ status: "unsupported" }]);
177
+ });
178
+ });
179
+
180
+ test("quantified refinements keep the domain outside the callback's binding", async () => {
181
+ await fixture(async dir => {
182
+ const file = join(dir, "increment.proof.ts");
183
+ await writeFile(file, portable
184
+ .replace("given(input !== 7n)", "given(pud.forall(pud.boolean(), flag => pud.implies(flag, input !== 7n)))")
185
+ .replace("pud.bigint().eq(input + 1n)", `pud.bigint().refine(result =>
186
+ pud.forall(pud.bigint().gte(result), result => result > input),
187
+ )`));
188
+ const selected = adapter(claims => {
189
+ expect(claims[0]?.premises[1]).toMatchObject({
190
+ kind: "proposition",
191
+ predicate: { kind: "quantifier", quantifier: "forall", domain: { kind: "schema", type: "boolean" } },
192
+ });
193
+ expect(claims[0]?.assertions[0]).toMatchObject({
194
+ kind: "refinement", variable: "result", target: { kind: "result" },
195
+ predicate: { kind: "quantifier", quantifier: "forall", variable: "result",
196
+ domain: { kind: "schema", type: "integer", constraints: [
197
+ { kind: "comparison", target: { kind: "quantified" }, operator: "gte", value: { source: "result" } },
198
+ ] },
199
+ },
200
+ });
201
+ });
202
+ expect((await check(file, { adapter: selected })).results[0]?.status).toBe("unproved");
203
+ const lean = await emit(file, { adapter: leanAdapter() });
204
+ expect(lean).toContain("(∀ (q0 : Bool), ((q0 = true) → (p0 ≠ (7 : Int)))) →");
205
+ expect(lean).toContain("(∀ (q1 : Int), ((q1 ≥ result)) → (q1 > p0))");
206
+ });
207
+ });
208
+
209
+ test("a different adapter receives portable properties and excludes native Lean terms", async () => {
210
+ await fixture(async dir => {
211
+ await writeFile(join(dir, "pud.config.ts"), "throw new Error('The explicit adapter should bypass config evaluation.');");
212
+ const file = join(dir, "increment.proof.ts");
213
+ await writeFile(file, `import { lean } from "@pud.dev/lib/lean";
214
+ import pud from "./pud.config.ts";
215
+ import { increment } from "./increment.ts";
216
+ pud.describe(increment, ({ it, expect, use }) => {
217
+ use(lean.module("./lean-only.lean"));
218
+ it("portable", { value: pud.bigint() }, ({ value }) => {
219
+ expect(increment(value)).to(pud.bigint().gt(value));
220
+ });
221
+ it("native predicate", { value: pud.bigint() }, ({ value }) => {
222
+ expect(increment(value)).to(pud.bigint().satisfies(lean\`fun result => result > \${value}\`));
223
+ });
224
+ it("native proof", { value: pud.bigint() }, ({ value }) => {
225
+ expect(increment(value)).to(pud.bigint().gt(value));
226
+ }).by(lean.ref("Increment.correct"));
227
+ it("native quantified domain", { value: pud.bigint() }, ({ value }) => {
228
+ expect(increment(value)).to(pud.bigint().refine(result =>
229
+ pud.forall(pud.bigint().satisfies(lean.ref("Increment.admits")), other => other > result),
230
+ ));
231
+ });
232
+ });`);
233
+ const selected = adapter((claims, modules) => {
234
+ expect(modules).toEqual([]);
235
+ expect(claims).toHaveLength(1);
236
+ expect(claims[0]?.assertions[0]).toMatchObject({ kind: "comparison", operator: "gt" });
237
+ });
238
+ const results = (await check(file, { adapter: selected })).results;
239
+ expect(results.map(result => result.status)).toEqual(["unproved", "unsupported", "unsupported", "unsupported"]);
240
+ expect(results[1]?.detail).toBe("This property uses lean proof code. The selected adapter is test.");
241
+ expect(results[3]?.detail).toBe(results[1]?.detail);
242
+ await expect(emit(file, { adapter: selected, property: "native proof" })).rejects.toThrow("uses lean proof code");
243
+ });
244
+ });
245
+
246
+ test("a change to the implementation invalidates the adapter verdict", async () => {
247
+ await fixture(async dir => {
248
+ const file = join(dir, "increment.proof.ts");
249
+ await writeFile(file, portable);
250
+ const selected = adapter(async () => {
251
+ await writeFile(join(dir, "increment.ts"), increment.replace("+ 1n", "- 1n"));
252
+ });
253
+ const { results } = await check(file, { adapter: selected });
254
+ expect(results.map(result => result.status)).toEqual(["error", "todo"]);
255
+ expect(results[0]?.detail).toContain("increment.ts changed during verification");
256
+ });
257
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "include": [
4
+ "**/*.ts",
5
+ ".examples/**/*.ts"
6
+ ]
7
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "noEmit": false,
6
+ "noEmitOnError": true,
7
+ "declaration": true,
8
+ "emitDeclarationOnly": true,
9
+ "outDir": "./dist/types"
10
+ },
11
+ "include": ["src/index.ts", "src/lean/index.ts"]
12
+ }