@milkenjoyer/toolchain 0.12.0 → 0.13.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/README.md +95 -6
- package/dist/arithmetic.d.mts +45 -0
- package/dist/arithmetic.mjs +25 -0
- package/dist/formal.mjs +393 -184
- package/dist/knip.mjs +2 -1
- package/package.json +7 -2
- package/src/formal/arithmetic.ts +190 -0
- package/src/formal/checks.ts +19 -1
- package/src/formal/proof-run.ts +77 -0
- package/src/formal/proofs.ts +18 -9
- package/src/formal/prover.ts +291 -0
- package/src/formal/specs.ts +27 -1
- package/src/formal/tla.ts +36 -17
- package/src/formal/z3.ts +321 -0
- package/src/formal.ts +1 -1
package/src/formal/specs.ts
CHANGED
|
@@ -71,8 +71,34 @@ const Entry = Schema.Union([
|
|
|
71
71
|
Schema.Struct({ ...Proof, mode: Schema.Literal("manual") }),
|
|
72
72
|
Schema.Struct({ ...Common, mode: Schema.Literal("library") }),
|
|
73
73
|
]);
|
|
74
|
+
const ArithmeticCommon = {
|
|
75
|
+
covers: Common.covers,
|
|
76
|
+
timeoutSeconds: Runtime.timeoutSeconds,
|
|
77
|
+
breaks: Schema.NonEmptyArray(
|
|
78
|
+
Schema.Struct({
|
|
79
|
+
file: RepoPath,
|
|
80
|
+
find: Schema.NonEmptyString,
|
|
81
|
+
replace: Schema.String,
|
|
82
|
+
})
|
|
83
|
+
),
|
|
84
|
+
};
|
|
85
|
+
const ArithmeticProof = Schema.Union([
|
|
86
|
+
Schema.Struct({ ...ArithmeticCommon, kind: Schema.Literal("every") }),
|
|
87
|
+
Schema.Struct({
|
|
88
|
+
...ArithmeticCommon,
|
|
89
|
+
kind: Schema.Literal("z3"),
|
|
90
|
+
samples: Schema.Int.check(
|
|
91
|
+
Schema.isBetween({ minimum: 10000, maximum: 1000000 })
|
|
92
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed(10000))),
|
|
93
|
+
}),
|
|
94
|
+
]);
|
|
74
95
|
const SpecsFile = Schema.Struct({
|
|
75
96
|
critical: Schema.NonEmptyArray(Schema.NonEmptyString),
|
|
97
|
+
arithmetic: Schema.optionalKey(Schema.Array(Schema.NonEmptyString)),
|
|
98
|
+
proofs: Schema.Record(
|
|
99
|
+
RepoPath.check(Schema.isPattern(/\.proof\.ts$/u)),
|
|
100
|
+
ArithmeticProof
|
|
101
|
+
).pipe(Schema.withDecodingDefaultKey(Effect.succeed({}))),
|
|
76
102
|
specs: Schema.Record(RepoPath.check(Schema.isPattern(/\.tla$/u)), Entry),
|
|
77
103
|
kani: Schema.Record(
|
|
78
104
|
RepoPath.check(Schema.isPattern(/\.rs$/u)),
|
|
@@ -123,7 +149,7 @@ export const load = Effect.fn("formal.load")(function* (root: string) {
|
|
|
123
149
|
files.split("\0").filter(Boolean),
|
|
124
150
|
(file) => fs.exists(path.join(root, file))
|
|
125
151
|
);
|
|
126
|
-
let file: SpecsFile = { critical: [], specs: {}, kani: {} };
|
|
152
|
+
let file: SpecsFile = { critical: [], specs: {}, kani: {}, proofs: {} };
|
|
127
153
|
let decodeErrors: ReadonlyArray<string> = [];
|
|
128
154
|
if (yield* fs.exists(path.join(root, "specs.json"))) {
|
|
129
155
|
const raw = yield* fs.readFileString(path.join(root, "specs.json"));
|
package/src/formal/tla.ts
CHANGED
|
@@ -11,6 +11,7 @@ import * as Layer from "effect/Layer";
|
|
|
11
11
|
import * as Option from "effect/Option";
|
|
12
12
|
import { Path } from "effect/Path";
|
|
13
13
|
import { ChildProcess, ChildProcessSpawner } from "effect/process";
|
|
14
|
+
import * as Result from "effect/Result";
|
|
14
15
|
import * as Stream from "effect/Stream";
|
|
15
16
|
|
|
16
17
|
import { fromXml } from "./semantics.ts";
|
|
@@ -52,12 +53,18 @@ type Formatted =
|
|
|
52
53
|
const pins = [
|
|
53
54
|
{
|
|
54
55
|
file: "tla2tools-1.8.0.jar",
|
|
55
|
-
|
|
56
|
+
urls: [
|
|
57
|
+
"https://github.com/tlaplus/tlaplus/releases/download/v1.8.0/tla2tools.jar",
|
|
58
|
+
"https://api.github.com/repos/tlaplus/tlaplus/releases/assets/718777374",
|
|
59
|
+
],
|
|
56
60
|
sha256: "e1f11b75c4b5bc4478ba0ac5cb6ae28e40a01b0016bc33e0aa97da22dfdbcd7f",
|
|
57
61
|
},
|
|
58
62
|
{
|
|
59
63
|
file: "CommunityModules-deps-202611060952.jar",
|
|
60
|
-
|
|
64
|
+
urls: [
|
|
65
|
+
"https://github.com/tlaplus/CommunityModules/releases/download/202611060952/CommunityModules-deps-202611060952.jar",
|
|
66
|
+
"https://api.github.com/repos/tlaplus/CommunityModules/releases/assets/716678698",
|
|
67
|
+
],
|
|
61
68
|
sha256: "a3b27c40f1fb5085f30c6ead37e918457f8cfc62c1e50a1dc668ef592ad2b5fb",
|
|
62
69
|
},
|
|
63
70
|
];
|
|
@@ -119,25 +126,37 @@ const make = Effect.gen(function* () {
|
|
|
119
126
|
prefix: `.${pin.file}.`,
|
|
120
127
|
suffix: ".part",
|
|
121
128
|
});
|
|
122
|
-
const bytes = yield*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
const { url, bytes } = yield* Effect.gen(function* () {
|
|
130
|
+
const failures: string[] = [];
|
|
131
|
+
for (const url of pin.urls) {
|
|
132
|
+
const result = yield* HttpClient.get(url, {
|
|
133
|
+
headers: { Accept: "application/octet-stream" },
|
|
134
|
+
}).pipe(
|
|
135
|
+
Effect.flatMap((response) =>
|
|
136
|
+
response.status === 200
|
|
137
|
+
? response.arrayBuffer.pipe(Effect.mapError(error))
|
|
138
|
+
: Effect.fail(
|
|
139
|
+
new FormalError({
|
|
140
|
+
message: `HTTP ${response.status}`,
|
|
141
|
+
})
|
|
142
|
+
)
|
|
143
|
+
),
|
|
144
|
+
Effect.map((buffer) => new Uint8Array(buffer)),
|
|
145
|
+
Effect.timeout("120 seconds"),
|
|
146
|
+
Effect.provide(FetchHttpClient.layer),
|
|
147
|
+
Effect.result
|
|
148
|
+
);
|
|
149
|
+
if (Result.isSuccess(result))
|
|
150
|
+
return { url, bytes: result.success };
|
|
151
|
+
failures.push(`${url}: ${error(result.failure).message}`);
|
|
152
|
+
}
|
|
153
|
+
return yield* new FormalError({ message: failures.join("; ") });
|
|
154
|
+
});
|
|
136
155
|
yield* fs.writeFile(temp, bytes);
|
|
137
156
|
const actual = digest(bytes);
|
|
138
157
|
if (actual !== pin.sha256)
|
|
139
158
|
return yield* new FormalError({
|
|
140
|
-
message: `${
|
|
159
|
+
message: `${url} has sha256 ${actual}, expected ${pin.sha256}`,
|
|
141
160
|
});
|
|
142
161
|
return yield* fs.rename(temp, file);
|
|
143
162
|
})
|
package/src/formal/z3.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
import * as Schema from "effect/Schema";
|
|
4
|
+
import { init, killThreads } from "z3-solver";
|
|
5
|
+
import type { Arith, BitVec, Bool, Context, Expr, Solver } from "z3-solver";
|
|
6
|
+
|
|
7
|
+
import { invalid, message } from "./proof-run.ts";
|
|
8
|
+
import type { Domain, Outcome, Request } from "./proof-run.ts";
|
|
9
|
+
|
|
10
|
+
type Term = Arith | BitVec | Bool;
|
|
11
|
+
type Body = {
|
|
12
|
+
readonly assume?: (...args: unknown[]) => unknown;
|
|
13
|
+
readonly model: (...args: unknown[]) => unknown;
|
|
14
|
+
readonly code: (...args: unknown[]) => unknown;
|
|
15
|
+
readonly holds: (...args: unknown[]) => unknown;
|
|
16
|
+
};
|
|
17
|
+
type Tuple = Record<string, number>;
|
|
18
|
+
const random = (path: string) => {
|
|
19
|
+
let seed = 0xcbf29ce484222325n;
|
|
20
|
+
const mask = (1n << 64n) - 1n;
|
|
21
|
+
for (const byte of new TextEncoder().encode(path))
|
|
22
|
+
seed = ((seed ^ BigInt(byte)) * 0x100000001b3n) & mask;
|
|
23
|
+
return (min: number, max: number) => {
|
|
24
|
+
const span = BigInt(max) - BigInt(min) + 1n;
|
|
25
|
+
const limit = (1n << 64n) - ((1n << 64n) % span);
|
|
26
|
+
for (;;) {
|
|
27
|
+
seed = (seed + 0x9e3779b97f4a7c15n) & mask;
|
|
28
|
+
let z = seed;
|
|
29
|
+
z = ((z ^ (z >> 30n)) * 0xbf58476d1ce4e5b9n) & mask;
|
|
30
|
+
z = ((z ^ (z >> 27n)) * 0x94d049bb133111ebn) & mask;
|
|
31
|
+
z ^= z >> 31n;
|
|
32
|
+
if (z < limit) return Number(BigInt(min) + (z % span));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
const corners = (domains: ReadonlyArray<Domain>) => {
|
|
37
|
+
let tuples: Tuple[] = [{}];
|
|
38
|
+
for (const d of domains) {
|
|
39
|
+
const values = [...new Set([d.min, d.min + 1, d.max - 1, d.max])].filter(
|
|
40
|
+
(n) => n >= d.min && n <= d.max
|
|
41
|
+
);
|
|
42
|
+
tuples = tuples
|
|
43
|
+
.flatMap((at) => values.map((n) => ({ ...at, [d.name]: n })))
|
|
44
|
+
.slice(0, 1000);
|
|
45
|
+
}
|
|
46
|
+
return tuples;
|
|
47
|
+
};
|
|
48
|
+
const bits = async (
|
|
49
|
+
Z3: Context,
|
|
50
|
+
claims: ReadonlyArray<Bool>
|
|
51
|
+
): Promise<string | undefined> => {
|
|
52
|
+
if (claims.length === 0) return "";
|
|
53
|
+
const parts = claims.map((c) =>
|
|
54
|
+
Z3.If(c, Z3.BitVec.val(1, 1), Z3.BitVec.val(0, 1))
|
|
55
|
+
);
|
|
56
|
+
const first = parts[0];
|
|
57
|
+
if (!first) return "";
|
|
58
|
+
let layer: BitVec[] = parts;
|
|
59
|
+
while (layer.length > 1000) {
|
|
60
|
+
const next: BitVec[] = [];
|
|
61
|
+
for (let i = 0; i < layer.length; i += 1000) {
|
|
62
|
+
const group = layer.slice(i, i + 1000);
|
|
63
|
+
const head = group[0];
|
|
64
|
+
if (head) next.push(group.length === 1 ? head : Z3.Concat(...group));
|
|
65
|
+
}
|
|
66
|
+
layer = next;
|
|
67
|
+
}
|
|
68
|
+
const term = layer.length === 1 ? (layer[0] ?? first) : Z3.Concat(...layer);
|
|
69
|
+
const result = await Z3.simplify(term);
|
|
70
|
+
return Z3.isBitVecVal(result)
|
|
71
|
+
? result.value().toString(2).padStart(parts.length, "0")
|
|
72
|
+
: undefined;
|
|
73
|
+
};
|
|
74
|
+
export const prove = async (
|
|
75
|
+
request: Request,
|
|
76
|
+
body: Body,
|
|
77
|
+
domains: ReadonlyArray<Domain>,
|
|
78
|
+
started: number
|
|
79
|
+
): Promise<Outcome> => {
|
|
80
|
+
const api = await init();
|
|
81
|
+
const makeContext = api.Context;
|
|
82
|
+
const em: unknown = api.em;
|
|
83
|
+
try {
|
|
84
|
+
const Z3 = makeContext("main");
|
|
85
|
+
const symbols: Record<string, Arith | BitVec> = {};
|
|
86
|
+
const domain: Bool[] = [];
|
|
87
|
+
for (const d of domains) {
|
|
88
|
+
if (d.kind === "uint") symbols[d.name] = Z3.BitVec.const(d.name, d.bits);
|
|
89
|
+
else {
|
|
90
|
+
const term = Z3.Int.const(d.name);
|
|
91
|
+
symbols[d.name] = term;
|
|
92
|
+
domain.push(term.ge(d.min), term.le(d.max));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const isTerm = (value: unknown): value is Term =>
|
|
96
|
+
Z3.isArith(value) || Z3.isBitVec(value) || Z3.isBool(value);
|
|
97
|
+
const output = body.model(symbols, Z3);
|
|
98
|
+
const claim = body.holds(symbols, output, Z3);
|
|
99
|
+
const assumption = body.assume
|
|
100
|
+
? body.assume(symbols, Z3)
|
|
101
|
+
: Z3.Bool.val(true);
|
|
102
|
+
if (!Z3.isBool(claim) || !Z3.isBool(assumption))
|
|
103
|
+
return invalid("holds and assume must return Bool terms");
|
|
104
|
+
const scalar = Z3.isExpr(output);
|
|
105
|
+
const record = scalar
|
|
106
|
+
? { value: output }
|
|
107
|
+
: Schema.decodeUnknownSync(Schema.Record(Schema.String, Schema.Unknown))(
|
|
108
|
+
output
|
|
109
|
+
);
|
|
110
|
+
const sorts = new Set<string>();
|
|
111
|
+
const seen = new Set<number>();
|
|
112
|
+
const walk = (expr: Expr): void => {
|
|
113
|
+
if (seen.has(expr.id())) return;
|
|
114
|
+
seen.add(expr.id());
|
|
115
|
+
sorts.add(String(expr.sort.name()));
|
|
116
|
+
if (Z3.isApp(expr) || Z3.isQuantifier(expr))
|
|
117
|
+
for (const child of expr.children()) walk(child);
|
|
118
|
+
};
|
|
119
|
+
for (const root of [assumption, claim, ...Object.values(record)])
|
|
120
|
+
if (Z3.isExpr(root)) walk(root);
|
|
121
|
+
const main = new Z3.Solver();
|
|
122
|
+
main.add(...domain, assumption, Z3.Not(claim));
|
|
123
|
+
if (["Real", "FloatingPoint", "RoundingMode"].some((s) => sorts.has(s)))
|
|
124
|
+
return invalid(
|
|
125
|
+
"the claim uses Real or Float arithmetic; money is whole numbers, so state it over Int or BitVec"
|
|
126
|
+
);
|
|
127
|
+
const terms: [string, Term][] = [];
|
|
128
|
+
for (const [key, term] of Object.entries(record)) {
|
|
129
|
+
if (!isTerm(term))
|
|
130
|
+
return invalid(
|
|
131
|
+
"model must return an Int, BitVec, Bool or a record of those terms"
|
|
132
|
+
);
|
|
133
|
+
terms.push([key, term]);
|
|
134
|
+
}
|
|
135
|
+
if (terms.length === 0)
|
|
136
|
+
return invalid("model must return at least one term");
|
|
137
|
+
const substitutions = (at: Tuple): [Expr, Expr][] =>
|
|
138
|
+
domains.map((d) => {
|
|
139
|
+
const symbol = symbols[d.name];
|
|
140
|
+
if (!symbol) throw new Error(`missing symbol ${d.name}`);
|
|
141
|
+
return [
|
|
142
|
+
symbol,
|
|
143
|
+
d.kind === "uint"
|
|
144
|
+
? Z3.BitVec.val(at[d.name] ?? 0, d.bits)
|
|
145
|
+
: Z3.Int.val(at[d.name] ?? 0),
|
|
146
|
+
];
|
|
147
|
+
});
|
|
148
|
+
const valueOf = async (at: Tuple) => {
|
|
149
|
+
const values: Record<string, string | number | boolean> = {};
|
|
150
|
+
for (const [key, term] of terms) {
|
|
151
|
+
const value = await Z3.simplify(
|
|
152
|
+
Z3.substitute(term, ...substitutions(at))
|
|
153
|
+
);
|
|
154
|
+
values[key] =
|
|
155
|
+
Z3.isIntVal(value) || Z3.isBitVecVal(value)
|
|
156
|
+
? Number(value.value())
|
|
157
|
+
: Z3.isTrue(value)
|
|
158
|
+
? true
|
|
159
|
+
: Z3.isFalse(value)
|
|
160
|
+
? false
|
|
161
|
+
: value.sexpr();
|
|
162
|
+
}
|
|
163
|
+
return scalar ? values.value : values;
|
|
164
|
+
};
|
|
165
|
+
const draw = random(relative(request.root, request.proof));
|
|
166
|
+
const corner = corners(domains);
|
|
167
|
+
const accepted: Tuple[] = [];
|
|
168
|
+
let drawn = 0;
|
|
169
|
+
while (accepted.length < request.samples && drawn < 100 * request.samples) {
|
|
170
|
+
const batch: Tuple[] = [];
|
|
171
|
+
const count = Math.min(
|
|
172
|
+
request.samples - accepted.length,
|
|
173
|
+
100 * request.samples - drawn
|
|
174
|
+
);
|
|
175
|
+
for (let i = 0; i < count; i++) {
|
|
176
|
+
batch.push(
|
|
177
|
+
corner[drawn] ??
|
|
178
|
+
Object.fromEntries(domains.map((d) => [d.name, draw(d.min, d.max)]))
|
|
179
|
+
);
|
|
180
|
+
drawn++;
|
|
181
|
+
}
|
|
182
|
+
const selected = body.assume
|
|
183
|
+
? await bits(
|
|
184
|
+
Z3,
|
|
185
|
+
batch.map((at) => {
|
|
186
|
+
const term = Z3.substitute(assumption, ...substitutions(at));
|
|
187
|
+
if (!Z3.isBool(term))
|
|
188
|
+
throw new Error("assume did not produce a Bool");
|
|
189
|
+
return term;
|
|
190
|
+
})
|
|
191
|
+
)
|
|
192
|
+
: "1".repeat(batch.length);
|
|
193
|
+
if (selected === undefined)
|
|
194
|
+
return invalid("assume has no value on the sampled inputs");
|
|
195
|
+
batch.forEach((at, i) => {
|
|
196
|
+
if (selected[i] === "1") accepted.push(at);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
if (accepted.length < request.samples)
|
|
200
|
+
return invalid(
|
|
201
|
+
`assume accepted ${accepted.length} of ${drawn} drawn inputs; need ${request.samples}`
|
|
202
|
+
);
|
|
203
|
+
const equalities: Bool[] = [];
|
|
204
|
+
const codes: unknown[] = [];
|
|
205
|
+
for (const at of accepted) {
|
|
206
|
+
let code: unknown;
|
|
207
|
+
try {
|
|
208
|
+
code = body.code(at);
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { _tag: "Throws", at, message: message(error) };
|
|
211
|
+
}
|
|
212
|
+
codes.push(code);
|
|
213
|
+
const fields = Schema.decodeUnknownOption(
|
|
214
|
+
Schema.Record(Schema.String, Schema.Unknown)
|
|
215
|
+
)(code);
|
|
216
|
+
const values = scalar
|
|
217
|
+
? { value: code }
|
|
218
|
+
: fields._tag === "Some"
|
|
219
|
+
? fields.value
|
|
220
|
+
: undefined;
|
|
221
|
+
const eq: Bool[] = [];
|
|
222
|
+
if (values === undefined || Object.keys(values).length !== terms.length)
|
|
223
|
+
return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
224
|
+
for (const [key, term] of terms) {
|
|
225
|
+
const value = values[key];
|
|
226
|
+
const substituted = Z3.substitute(term, ...substitutions(at));
|
|
227
|
+
if (Z3.isBool(substituted) && typeof value === "boolean")
|
|
228
|
+
eq.push(substituted.eq(value));
|
|
229
|
+
else if (
|
|
230
|
+
(Z3.isArith(substituted) || Z3.isBitVec(substituted)) &&
|
|
231
|
+
typeof value === "number" &&
|
|
232
|
+
Number.isSafeInteger(value) &&
|
|
233
|
+
(!Z3.isBitVec(substituted) ||
|
|
234
|
+
(value >= 0 && BigInt(value) < 1n << BigInt(substituted.size())))
|
|
235
|
+
)
|
|
236
|
+
eq.push(
|
|
237
|
+
substituted.eq(
|
|
238
|
+
Z3.isBitVec(substituted)
|
|
239
|
+
? Z3.BitVec.val(value, substituted.size())
|
|
240
|
+
: Z3.Int.val(value)
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
else return { _tag: "Disagrees", at, code, model: await valueOf(at) };
|
|
244
|
+
}
|
|
245
|
+
equalities.push(Z3.And(...eq));
|
|
246
|
+
}
|
|
247
|
+
const same = await bits(Z3, equalities);
|
|
248
|
+
if (same === undefined) {
|
|
249
|
+
for (const [i, eq] of equalities.entries()) {
|
|
250
|
+
const value = await Z3.simplify(eq);
|
|
251
|
+
if (!Z3.isTrue(value) && !Z3.isFalse(value))
|
|
252
|
+
return invalid(
|
|
253
|
+
`the model has no value at ${JSON.stringify(accepted[i])}`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
return invalid("the model has no value on the sampled inputs");
|
|
257
|
+
}
|
|
258
|
+
const mismatch = same.indexOf("0");
|
|
259
|
+
const at = accepted[mismatch];
|
|
260
|
+
if (at)
|
|
261
|
+
return {
|
|
262
|
+
_tag: "Disagrees",
|
|
263
|
+
at,
|
|
264
|
+
code: codes[mismatch],
|
|
265
|
+
model: await valueOf(at),
|
|
266
|
+
};
|
|
267
|
+
const fresh: Record<string, Term> = {};
|
|
268
|
+
for (const [key, term] of terms)
|
|
269
|
+
fresh[key] = Z3.isBool(term)
|
|
270
|
+
? Z3.Bool.fresh()
|
|
271
|
+
: Z3.isBitVec(term)
|
|
272
|
+
? Z3.FreshConst(term.sort)
|
|
273
|
+
: Z3.Int.fresh();
|
|
274
|
+
const freshClaim = body.holds(symbols, scalar ? fresh.value : fresh, Z3);
|
|
275
|
+
if (!Z3.isBool(freshClaim)) return invalid("holds must return a Bool term");
|
|
276
|
+
const vacuity = new Z3.Solver();
|
|
277
|
+
vacuity.add(...domain, assumption, Z3.Not(freshClaim));
|
|
278
|
+
const check = async (solver: Solver) => {
|
|
279
|
+
const left =
|
|
280
|
+
request.timeoutSeconds * 1000 - (performance.now() - started);
|
|
281
|
+
if (left <= 0) return "unknown";
|
|
282
|
+
solver.set("timeout", Math.max(1, Math.floor(left)));
|
|
283
|
+
return await solver.check();
|
|
284
|
+
};
|
|
285
|
+
const unknown = (solver: Solver): Outcome => ({
|
|
286
|
+
_tag: "Unknown",
|
|
287
|
+
reason:
|
|
288
|
+
performance.now() - started >= request.timeoutSeconds * 1000
|
|
289
|
+
? "timeout"
|
|
290
|
+
: solver.reasonUnknown(),
|
|
291
|
+
seconds: (performance.now() - started) / 1000,
|
|
292
|
+
});
|
|
293
|
+
const meaningful = await check(vacuity);
|
|
294
|
+
if (meaningful === "unsat")
|
|
295
|
+
return invalid(
|
|
296
|
+
"holds is true for any output, so it says nothing about the code"
|
|
297
|
+
);
|
|
298
|
+
if (meaningful === "unknown") return unknown(vacuity);
|
|
299
|
+
const result = await check(main);
|
|
300
|
+
if (result === "unknown") return unknown(main);
|
|
301
|
+
if (result === "sat") {
|
|
302
|
+
const at: Tuple = {};
|
|
303
|
+
const model = main.model();
|
|
304
|
+
for (const [name, term] of Object.entries(symbols)) {
|
|
305
|
+
const value = model.eval(term, true);
|
|
306
|
+
if (Z3.isIntVal(value) || Z3.isBitVecVal(value))
|
|
307
|
+
at[name] = Number(value.value());
|
|
308
|
+
}
|
|
309
|
+
return { _tag: "Counterexample", at };
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
_tag: "Proved",
|
|
313
|
+
detail: `z3 unsat, code agrees on ${accepted.length} samples`,
|
|
314
|
+
seconds: (performance.now() - started) / 1000,
|
|
315
|
+
};
|
|
316
|
+
} catch (error) {
|
|
317
|
+
return invalid(message(error));
|
|
318
|
+
} finally {
|
|
319
|
+
await killThreads(em);
|
|
320
|
+
}
|
|
321
|
+
};
|
package/src/formal.ts
CHANGED
|
@@ -38,7 +38,7 @@ export const formal = Effect.fn("formal")(function* (root: string) {
|
|
|
38
38
|
);
|
|
39
39
|
} else
|
|
40
40
|
yield* Console.log(
|
|
41
|
-
`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, no findings`
|
|
41
|
+
`formal: ${repo.specs.length} specs, ${repo.specs.reduce((n, s) => n + s.configs.length, 0)} configs, ${Object.keys(repo.file.proofs).length} proofs, no findings`
|
|
42
42
|
);
|
|
43
43
|
return findings.length ? 1 : 0;
|
|
44
44
|
});
|