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.
- package/CLAUDE.md +106 -0
- package/README.md +117 -2
- package/package.json +24 -4
- package/scripts/pack.ts +67 -0
- package/scripts/setup-lean.ts +88 -0
- package/src/assistant.ts +131 -0
- package/src/check.ts +173 -0
- package/src/cli.ts +70 -0
- package/src/expressions.ts +65 -0
- package/src/index.ts +4 -0
- package/src/input-scope.ts +71 -0
- package/src/lean/command.ts +40 -0
- package/src/lean/index.ts +437 -0
- package/src/lean/iteration.lean +145 -0
- package/src/lean/lean-assets.d.ts +4 -0
- package/src/lean/number.lean +427 -0
- package/src/lean/number.ts +25 -0
- package/src/lean/program.ts +110 -0
- package/src/lean/replay-predicate.ts +95 -0
- package/src/lean/replay.ts +136 -0
- package/src/lean/scalar.lean +183 -0
- package/src/lean/scalar.ts +196 -0
- package/src/lean/subject.ts +443 -0
- package/src/lean/syntax.ts +287 -0
- package/src/module.ts +110 -0
- package/src/project-runner.ts +26 -0
- package/src/prover-process.ts +16 -0
- package/src/reporter.ts +173 -0
- package/src/run.ts +60 -0
- package/src/source.ts +54 -0
- package/src/suite-authoring.ts +135 -0
- package/src/suite-check.ts +157 -0
- package/src/suite.ts +515 -0
- package/src/terminal-progress.ts +145 -0
- package/src/terminal.ts +41 -0
- package/test/authoring.test.ts +151 -0
- package/test/lean.test.ts +394 -0
- package/test/module.test.ts +55 -0
- package/test/number.test.ts +318 -0
- package/test/package.test.ts +170 -0
- package/test/suite.test.ts +257 -0
- package/tsconfig.json +7 -0
- package/tsconfig.pack.json +12 -0
package/src/run.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { failure, Unsupported } from "./assistant.ts";
|
|
2
|
+
import { checkFiles as checkInProcess, emit as emitInProcess, type CheckOptions, type CheckProgress, type CheckReport, type EmitOptions } from "./check.ts";
|
|
3
|
+
|
|
4
|
+
type CheckRequest = { kind: "check"; files: readonly string[]; progress: boolean };
|
|
5
|
+
type EmitRequest = { kind: "emit"; file: string; property?: string };
|
|
6
|
+
export type ProjectRequest = CheckRequest | EmitRequest;
|
|
7
|
+
export type ProjectReply =
|
|
8
|
+
| { kind: "progress"; event: CheckProgress }
|
|
9
|
+
| { kind: "result"; value: CheckReport[] | string }
|
|
10
|
+
| { kind: "error"; error: Error; unsupported: boolean; code?: string };
|
|
11
|
+
|
|
12
|
+
function runProject(request: CheckRequest, onProgress?: CheckOptions["onProgress"]): Promise<CheckReport[]>;
|
|
13
|
+
function runProject(request: EmitRequest): Promise<string>;
|
|
14
|
+
async function runProject(request: ProjectRequest, onProgress?: CheckOptions["onProgress"]): Promise<CheckReport[] | string> {
|
|
15
|
+
let reply: Exclude<ProjectReply, { kind: "progress" }> | undefined;
|
|
16
|
+
let callbackFailure: { cause: unknown } | undefined;
|
|
17
|
+
// Each batch owns its Bun module cache. Project output goes to stderr, while IPC carries proof data.
|
|
18
|
+
const child = Bun.spawn([process.execPath, Bun.resolveSync("./project-runner.js", import.meta.dir)], {
|
|
19
|
+
stdin: "inherit", stdout: 2, stderr: "inherit",
|
|
20
|
+
ipc(message: ProjectReply, worker) {
|
|
21
|
+
if (message.kind === "progress") {
|
|
22
|
+
try {
|
|
23
|
+
onProgress?.(message.event);
|
|
24
|
+
if (message.event.kind === "validating") worker.send("continue");
|
|
25
|
+
} catch (cause) {
|
|
26
|
+
callbackFailure = { cause };
|
|
27
|
+
worker.kill();
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
reply = message;
|
|
31
|
+
worker.disconnect();
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
child.send(request);
|
|
36
|
+
const code = await child.exited;
|
|
37
|
+
if (callbackFailure) throw callbackFailure.cause;
|
|
38
|
+
if (reply?.kind === "result") return reply.value;
|
|
39
|
+
const error = reply?.error ?? new Error(`Project process exited with ${child.signalCode ?? code} before returning its result.`);
|
|
40
|
+
if (reply?.unsupported) Object.setPrototypeOf(error, Unsupported.prototype);
|
|
41
|
+
if (reply?.code) Object.assign(error, { code: reply.code });
|
|
42
|
+
if (request.kind === "emit") throw error;
|
|
43
|
+
return request.files.map(file => ({ file, results: [failure(error)] }));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Check a batch against shared snapshots, loading project config once per run. */
|
|
47
|
+
export function checkFiles(files: readonly string[], options: CheckOptions = {}): Promise<CheckReport[]> {
|
|
48
|
+
if (options.adapter || !files.length) return checkInProcess(files, options);
|
|
49
|
+
return runProject({ kind: "check", files, progress: !!options.onProgress }, options.onProgress);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function check(file: string, options?: CheckOptions): Promise<CheckReport> {
|
|
53
|
+
return (await checkFiles([file], options))[0]!;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Emit the same verification unit as pud --emit, without running a prover. */
|
|
57
|
+
export function emit(file: string, selector?: string | EmitOptions): Promise<string> {
|
|
58
|
+
const options = typeof selector === "string" ? { property: selector } : selector ?? {};
|
|
59
|
+
return options.adapter ? emitInProcess(file, options) : runProject({ kind: "emit", file, property: options.property });
|
|
60
|
+
}
|
package/src/source.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { parse } from "@babel/parser";
|
|
2
|
+
import type { Node, StringLiteral } from "@babel/types";
|
|
3
|
+
import { readdirSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import { basename, dirname, extname, resolve } from "node:path";
|
|
5
|
+
import { Unsupported } from "./assistant.ts";
|
|
6
|
+
|
|
7
|
+
export type { BlockStatement, Node, Statement, TryStatement } from "@babel/types";
|
|
8
|
+
|
|
9
|
+
export function invalid(file: string, node: Node | undefined, message: string): never {
|
|
10
|
+
const at = node?.loc?.start;
|
|
11
|
+
throw new Unsupported(`${file}${at ? `:${at.line}:${at.column + 1}` : ""}: ${message}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function parseSourceModule(file: string, source: string) {
|
|
15
|
+
try {
|
|
16
|
+
const { program } = parse(source, { sourceType: "module", plugins: ["typescript"] });
|
|
17
|
+
return program;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw new Unsupported(`${file}: ${error instanceof Error ? error.message : error}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolveTypeScriptImport(file: string, specifier: StringLiteral, base: string): string {
|
|
24
|
+
const path = specifier.value;
|
|
25
|
+
if (!/^\.\.?\//.test(path)) invalid(file, specifier, "Import the implementation using a relative path.");
|
|
26
|
+
const direct = resolve(base, path);
|
|
27
|
+
let target = direct;
|
|
28
|
+
let canonical: string;
|
|
29
|
+
try {
|
|
30
|
+
if (!path.endsWith(".ts")) {
|
|
31
|
+
// A sole matching .ts file is unambiguous. Keep the logical path to see symlink retargeting.
|
|
32
|
+
const name = basename(direct);
|
|
33
|
+
const candidates = !extname(name) && !path.endsWith("/") ? readdirSync(dirname(direct)).filter(entry =>
|
|
34
|
+
entry === name || (entry.startsWith(name + ".") && !entry.slice(name.length + 1).includes("."))) : [];
|
|
35
|
+
if (candidates.length === 1 && candidates[0] === name + ".ts") target = direct + ".ts";
|
|
36
|
+
else {
|
|
37
|
+
// Resolve competing files or directories in a fresh Bun process.
|
|
38
|
+
const run = Bun.spawnSync([process.execPath, "--preserve-symlinks", "-e",
|
|
39
|
+
"process.stdout.write(Bun.resolveSync(process.argv[1], process.argv[2]))", path, base], { stderr: "ignore" });
|
|
40
|
+
if (run.exitCode !== 0) throw new Error("Unresolved import");
|
|
41
|
+
target = run.stdout.toString();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
canonical = realpathSync(target);
|
|
45
|
+
if (!statSync(canonical).isFile()) throw new Error("Expected a file");
|
|
46
|
+
} catch {
|
|
47
|
+
return invalid(file, specifier, `Cannot resolve '${path}' from this proof file.`);
|
|
48
|
+
}
|
|
49
|
+
if (!canonical.endsWith(".ts") || canonical.endsWith(".d.ts")) {
|
|
50
|
+
invalid(file, specifier, `'${path}' resolves to ${canonical}. Pud needs a .ts implementation file.`);
|
|
51
|
+
}
|
|
52
|
+
return target;
|
|
53
|
+
}
|
|
54
|
+
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { ProofAssistant, ProofErrorKind } from "./assistant.ts";
|
|
2
|
+
|
|
3
|
+
type Value = bigint | number | boolean | string | readonly unknown[];
|
|
4
|
+
type Subject = (...args: never[]) => Value;
|
|
5
|
+
declare const valueType: unique symbol;
|
|
6
|
+
declare const assistantType: unique symbol;
|
|
7
|
+
declare const nativeType: unique symbol;
|
|
8
|
+
declare const propositionType: unique symbol;
|
|
9
|
+
|
|
10
|
+
export interface Proposition {
|
|
11
|
+
readonly [propositionType]: true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type Predicate = boolean | Proposition;
|
|
15
|
+
|
|
16
|
+
export interface Schema<T, Id extends string = never> {
|
|
17
|
+
readonly [valueType]: T;
|
|
18
|
+
readonly [assistantType]: Id;
|
|
19
|
+
refine(predicate: (value: T) => Predicate): this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface NativeTerm<Id extends string> {
|
|
23
|
+
readonly [nativeType]: { kind: "term"; id: (id: Id) => Id };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface NativeModule<Id extends string> {
|
|
27
|
+
readonly [nativeType]: { kind: "module"; id: (id: Id) => Id };
|
|
28
|
+
readonly path: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface NativeSyntax<Id extends string> {
|
|
32
|
+
(parts: TemplateStringsArray, ...args: Value[]): NativeTerm<Id>;
|
|
33
|
+
ref(name: string, ...args: Value[]): NativeTerm<Id>;
|
|
34
|
+
module(path: string): NativeModule<Id>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface NumericSchema<T extends bigint | number, Id extends string> extends Schema<T, Id> {
|
|
38
|
+
eq(value: T): this;
|
|
39
|
+
gt(value: T): this;
|
|
40
|
+
gte(value: T): this;
|
|
41
|
+
lt(value: T): this;
|
|
42
|
+
lte(value: T): this;
|
|
43
|
+
satisfies(term: NativeTerm<Id>): this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface NumberSchema<Id extends string> extends NumericSchema<number, Id> {
|
|
47
|
+
/** ECMAScript SameValue, including NaN and the sign of zero. */
|
|
48
|
+
sameValue(value: number): this;
|
|
49
|
+
/** Nearest whole number, with explicit half ties. Preserve NaN, infinities and the sign of a zero result. */
|
|
50
|
+
nearestIntegerTo(value: number, options: { ties: "up" | "down" }): this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface BooleanSchema<Id extends string> extends Schema<boolean, Id> {
|
|
54
|
+
eq(value: boolean): BooleanSchema<Id>;
|
|
55
|
+
satisfies(term: NativeTerm<Id>): BooleanSchema<Id>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface ArraySchema<T, Id extends string> extends Schema<readonly T[], Id> {
|
|
59
|
+
satisfies(term: NativeTerm<Id>): ArraySchema<T, Id>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface SchemaBuilders<Id extends string> {
|
|
63
|
+
bigint(): NumericSchema<bigint, Id>;
|
|
64
|
+
number(): NumberSchema<Id>;
|
|
65
|
+
boolean(): BooleanSchema<Id>;
|
|
66
|
+
int(): NumericSchema<bigint, Id>;
|
|
67
|
+
bool(): BooleanSchema<Id>;
|
|
68
|
+
opaque<T>(): Schema<T, Id>;
|
|
69
|
+
array<T>(items: Schema<T, Id>): ArraySchema<T, Id>;
|
|
70
|
+
textOrArray<T>(items: Schema<T, Id>): Schema<string | readonly T[], Id>;
|
|
71
|
+
forall<T extends bigint | number | boolean>(domain: Schema<T, Id> | readonly T[], predicate: (value: T) => Predicate): Proposition;
|
|
72
|
+
exists<T extends bigint | number | boolean>(domain: Schema<T, Id> | readonly T[], predicate: (value: T) => Predicate): Proposition;
|
|
73
|
+
and(left: Predicate, right: Predicate): Proposition;
|
|
74
|
+
or(left: Predicate, right: Predicate): Proposition;
|
|
75
|
+
not(operand: Predicate): Proposition;
|
|
76
|
+
implies(assumption: Predicate, conclusion: Predicate): Proposition;
|
|
77
|
+
iff(left: Predicate, right: Predicate): Proposition;
|
|
78
|
+
length(items: readonly unknown[]): bigint;
|
|
79
|
+
sum(items: readonly bigint[]): bigint;
|
|
80
|
+
modulo(value: bigint, divisor: bigint): bigint;
|
|
81
|
+
quotient(value: bigint, divisor: bigint): bigint;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
interface SuiteAuthoring<Id extends string> {
|
|
85
|
+
pud: SchemaBuilders<Id>;
|
|
86
|
+
it: {
|
|
87
|
+
<D extends Record<string, Schema<Value, Id>>>(
|
|
88
|
+
name: string,
|
|
89
|
+
inputs: D,
|
|
90
|
+
property: (args: { [K in keyof D]: D[K][typeof valueType] }) => void,
|
|
91
|
+
): { by(proof: NativeTerm<Id>): void };
|
|
92
|
+
todo(name: string): void;
|
|
93
|
+
};
|
|
94
|
+
given(condition: Predicate): void;
|
|
95
|
+
expect: {
|
|
96
|
+
<T extends Value>(value: T): {
|
|
97
|
+
to(schema: Schema<NoInfer<T>, Id>): void;
|
|
98
|
+
/** Prove that the schema accepts precisely the observed scalar result for each admitted input. */
|
|
99
|
+
toExactly(schema: [T] extends [number | bigint | boolean] ? Schema<NoInfer<T>, Id> : never): void;
|
|
100
|
+
toEqual(reference: NoInfer<T> & (bigint | number | boolean)): void;
|
|
101
|
+
};
|
|
102
|
+
(call: () => Value): { toThrow(error: ProofErrorKind): void };
|
|
103
|
+
};
|
|
104
|
+
use(module: NativeModule<Id>): void;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface PudConfig<Id extends string = string> extends SchemaBuilders<Id> {
|
|
108
|
+
adapter: ProofAssistant<Id>;
|
|
109
|
+
describe(subject: Subject, suite: (helpers: SuiteAuthoring<Id>) => void): void;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function sourceOnly(): never {
|
|
113
|
+
throw new Error("Pud reads proof declarations from *.proof.ts source. Run pud check.");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const schemaBuilders = {
|
|
117
|
+
bigint: sourceOnly, number: sourceOnly, boolean: sourceOnly, int: sourceOnly, bool: sourceOnly,
|
|
118
|
+
opaque: sourceOnly, array: sourceOnly, textOrArray: sourceOnly,
|
|
119
|
+
forall: sourceOnly, exists: sourceOnly, and: sourceOnly, or: sourceOnly, not: sourceOnly, implies: sourceOnly, iff: sourceOnly,
|
|
120
|
+
length: sourceOnly, sum: sourceOnly, modulo: sourceOnly, quotient: sourceOnly,
|
|
121
|
+
};
|
|
122
|
+
export const pud: SchemaBuilders<never> = schemaBuilders;
|
|
123
|
+
export const it: SuiteAuthoring<never>["it"] = Object.assign(sourceOnly.bind(undefined), { todo: sourceOnly });
|
|
124
|
+
export const given: SuiteAuthoring<never>["given"] = sourceOnly;
|
|
125
|
+
export const expect: SuiteAuthoring<never>["expect"] = sourceOnly;
|
|
126
|
+
export const describe: (subject: Subject, suite: () => void) => void = sourceOnly;
|
|
127
|
+
|
|
128
|
+
/** A static tag for an adapter's native proof text. */
|
|
129
|
+
export function native<const Id extends string>(_id: Id): NativeSyntax<Id> {
|
|
130
|
+
return Object.assign(sourceOnly.bind(undefined), { ref: sourceOnly, module: sourceOnly });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function defineConfig<const Id extends string>(config: { adapter: ProofAssistant<Id> }): PudConfig<Id> {
|
|
134
|
+
return { ...schemaBuilders, ...config, describe: sourceOnly };
|
|
135
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { failure, Unsupported, type NativeProofTerm, type ProofAssistant, type ProofClaim, type ProofConstraint, type ProofModule, type ProofPredicate } from "./assistant.ts";
|
|
5
|
+
import type { CheckOptions, CheckResult, EmitOptions, ItemProgress } from "./check.ts";
|
|
6
|
+
import { concreteScope } from "./input-scope.ts";
|
|
7
|
+
import { bindClaim, type Suite } from "./suite.ts";
|
|
8
|
+
|
|
9
|
+
type Read = (file: string) => { canonical: string; source: string };
|
|
10
|
+
|
|
11
|
+
export function findConfig(file: string): string | undefined {
|
|
12
|
+
for (let dir = dirname(file); ; dir = dirname(dir)) {
|
|
13
|
+
const candidate = join(dir, "pud.config.ts");
|
|
14
|
+
if (statSync(candidate, { throwIfNoEntry: false })?.isFile()) return candidate;
|
|
15
|
+
if (dirname(dir) === dir) return;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async function importConfig({ canonical, source }: ReturnType<Read>) {
|
|
20
|
+
if (readFileSync(canonical, "utf8") !== source) throw new Error(`${canonical} changed during verification. Rerun pud check.`);
|
|
21
|
+
return (await import(pathToFileURL(canonical).href)).default?.adapter as ProofAssistant | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function loadAdapter(config: string | undefined, override: ProofAssistant | undefined, read: Read): Promise<ProofAssistant> {
|
|
25
|
+
const adapter = override ?? (config ? await importConfig(read(config)) : undefined);
|
|
26
|
+
if (!adapter || typeof adapter.id !== "string" || !/^[a-z][a-z0-9_-]*$/.test(adapter.id) || typeof adapter.prepare !== "function") {
|
|
27
|
+
throw new Error("Select a proof assistant with the adapter field in pud.config.ts or pass an adapter option.");
|
|
28
|
+
}
|
|
29
|
+
return adapter;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function prepare(suite: Suite, adapter: ProofAssistant, read: Read) {
|
|
33
|
+
const { canonical, source } = read(suite.subject.file);
|
|
34
|
+
if (!canonical.endsWith(".ts") || canonical.endsWith(".d.ts")) {
|
|
35
|
+
throw new Unsupported(`${suite.subject.file} must resolve to a TypeScript implementation file.`);
|
|
36
|
+
}
|
|
37
|
+
const subject = await adapter.prepare({ file: canonical, source, name: suite.subject.name,
|
|
38
|
+
readDependency: dependency => read(resolve(dirname(canonical), dependency)).source,
|
|
39
|
+
readModule(path, resolveAgain) {
|
|
40
|
+
if (resolveAgain) suite.imports.push({ path, resolveAgain });
|
|
41
|
+
return read(path);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
if (!subject || !Array.isArray(subject.parameters) || !subject.result || typeof subject.emit !== "function" || typeof subject.verify !== "function") {
|
|
45
|
+
throw new Error(`The ${adapter.id} adapter must provide a subject signature, emit(), and verify().`);
|
|
46
|
+
}
|
|
47
|
+
return subject;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function modulesFor(suite: Suite, assistant: string, read: Read): ProofModule[] {
|
|
51
|
+
const files = new Set(suite.modules.filter(module => module.assistant === assistant).map(module => module.file));
|
|
52
|
+
return [...files].map(file => ({ file, source: read(file).source }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function* predicateTerms(predicate: ProofPredicate): IterableIterator<NativeProofTerm> {
|
|
56
|
+
switch (predicate.kind) {
|
|
57
|
+
case "boolean": return;
|
|
58
|
+
case "not": yield* predicateTerms(predicate.operand); return;
|
|
59
|
+
case "binary":
|
|
60
|
+
yield* predicateTerms(predicate.left);
|
|
61
|
+
yield* predicateTerms(predicate.right);
|
|
62
|
+
return;
|
|
63
|
+
case "quantifier":
|
|
64
|
+
if (predicate.domain.kind === "schema") yield* constraintTerms(predicate.domain.constraints);
|
|
65
|
+
yield* predicateTerms(predicate.predicate);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function* constraintTerms(constraints: readonly ProofConstraint[]): IterableIterator<NativeProofTerm> {
|
|
70
|
+
for (const constraint of constraints) {
|
|
71
|
+
if (constraint.kind === "native") yield constraint.term;
|
|
72
|
+
if (constraint.kind === "refinement" || constraint.kind === "proposition") yield* predicateTerms(constraint.predicate);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function checkAssistant(claim: ProofClaim, assistant: string) {
|
|
77
|
+
const terms = [claim.proof, ...constraintTerms(claim.premises), ...constraintTerms(claim.assertions)];
|
|
78
|
+
const incompatible = terms.find(term => term && term.assistant !== assistant);
|
|
79
|
+
if (incompatible) throw new Unsupported(`This property uses ${incompatible.assistant} proof code. The selected adapter is ${assistant}.`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function checkSuite(suite: Suite, config: string | undefined, options: CheckOptions, read: Read, report?: ItemProgress): Promise<CheckResult[]> {
|
|
83
|
+
const results: CheckResult[] = [];
|
|
84
|
+
const finished = new Set<number>();
|
|
85
|
+
const active = suite.properties.flatMap((property, index) => property.kind === "claim" ? [{ property, index }] : []);
|
|
86
|
+
let assistant: string | undefined;
|
|
87
|
+
|
|
88
|
+
function finish(index: number, status: CheckResult["status"]) {
|
|
89
|
+
if (!report || finished.has(index)) return;
|
|
90
|
+
finished.add(index);
|
|
91
|
+
report({ kind: "finished", indices: [index], statuses: [status] });
|
|
92
|
+
}
|
|
93
|
+
function record(index: number, result: CheckResult) {
|
|
94
|
+
results[index] = result;
|
|
95
|
+
finish(index, result.status);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (active.length) {
|
|
99
|
+
try {
|
|
100
|
+
const adapter = await loadAdapter(config, options.adapter, read);
|
|
101
|
+
assistant = adapter.id;
|
|
102
|
+
const subject = await prepare(suite, adapter, read);
|
|
103
|
+
const accepted: { index: number; claim: ProofClaim }[] = [];
|
|
104
|
+
for (const { property, index } of active) {
|
|
105
|
+
try {
|
|
106
|
+
const claim = bindClaim(suite, property, subject);
|
|
107
|
+
checkAssistant(claim, assistant);
|
|
108
|
+
accepted.push({ index, claim });
|
|
109
|
+
} catch (error) {
|
|
110
|
+
record(index, failure(error));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (accepted.length) {
|
|
114
|
+
report?.({ kind: "running", indices: accepted.map(item => item.index) });
|
|
115
|
+
const outcomes = await subject.verify(accepted.map(item => item.claim), modulesFor(suite, assistant, read),
|
|
116
|
+
report ? (offset, state, status) => {
|
|
117
|
+
const item = accepted[offset];
|
|
118
|
+
if (!item || finished.has(item.index)) return;
|
|
119
|
+
if (state === "running") report({ kind: "running", indices: [item.index] });
|
|
120
|
+
else if (status) finish(item.index, status);
|
|
121
|
+
} : undefined);
|
|
122
|
+
if (!Array.isArray(outcomes) || outcomes.length !== accepted.length || accepted.some((_, index) =>
|
|
123
|
+
!["proved", "disproved", "unproved", "unsupported", "error"].includes(outcomes[index]?.status ?? ""))) {
|
|
124
|
+
throw new Error("The proof assistant must return one verdict per submitted claim.");
|
|
125
|
+
}
|
|
126
|
+
for (const [offset, { index, claim }] of accepted.entries()) {
|
|
127
|
+
const outcome = outcomes[offset]!;
|
|
128
|
+
const scope = concreteScope(subject.parameters, claim);
|
|
129
|
+
record(index, { ...outcome, ...(scope && { concreteScope: scope }),
|
|
130
|
+
...(claim.exactResult && { exactResult: true }),
|
|
131
|
+
...(outcome.status === "disproved" && { detail: outcome.counterexample.summary }) });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
for (const { index } of active) if (!results[index]) record(index, failure(error));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return suite.properties.map((property, index) => ({
|
|
139
|
+
...(property.kind === "todo" ? { status: "todo" as const } : results[index]!),
|
|
140
|
+
name: property.name, line: property.line, subject: suite.subject.name, ...(assistant && { assistant }),
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export async function emitSuite(suite: Suite, config: string | undefined, options: EmitOptions, read: Read): Promise<string> {
|
|
145
|
+
const active = suite.properties.filter(property => property.kind === "claim");
|
|
146
|
+
const matches = options.property
|
|
147
|
+
? active.filter(property => [suite.binding, property.name, `${suite.binding}.${property.name}`].includes(options.property!))
|
|
148
|
+
: active;
|
|
149
|
+
if (matches.length !== 1) {
|
|
150
|
+
throw new Error(`Select one active property. Available: ${active.map(property => property.name).join(", ") || "none"}.`);
|
|
151
|
+
}
|
|
152
|
+
const adapter = await loadAdapter(config, options.adapter, read);
|
|
153
|
+
const subject = await prepare(suite, adapter, read);
|
|
154
|
+
const claim = bindClaim(suite, matches[0]!, subject);
|
|
155
|
+
checkAssistant(claim, adapter.id);
|
|
156
|
+
return subject.emit(claim, modulesFor(suite, adapter.id, read));
|
|
157
|
+
}
|