tslaws 1.1.0 → 1.2.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 +177 -187
- package/dist/api.d.ts +86 -0
- package/dist/api.js +78 -0
- package/dist/badge.d.ts +58 -11
- package/dist/badge.js +194 -27
- package/dist/browser.d.ts +5 -2
- package/dist/browser.js +403 -173
- package/dist/browserApi.d.ts +305 -0
- package/dist/browserApi.js +456 -0
- package/dist/browserServer.d.ts +13 -0
- package/dist/browserServer.js +406 -0
- package/dist/certificates.js +50 -28
- package/dist/chc.js +30 -7
- package/dist/check.js +55 -30
- package/dist/cli.js +12 -2
- package/dist/compile.js +41 -4
- package/dist/coverage.d.ts +17 -0
- package/dist/coverage.js +269 -18
- package/dist/derived.d.ts +2 -0
- package/dist/derived.js +9 -4
- package/dist/generators.d.ts +5 -1
- package/dist/generators.js +50 -4
- package/dist/lawHost.d.ts +2 -0
- package/dist/lawHost.js +31 -18
- package/dist/nativeZ3.d.ts +11 -0
- package/dist/nativeZ3.js +162 -0
- package/dist/plan.d.ts +7 -0
- package/dist/plan.js +11 -0
- package/dist/proofCache.d.ts +24 -0
- package/dist/proofCache.js +135 -0
- package/dist/property.d.ts +2 -2
- package/dist/property.js +33 -8
- package/dist/propertyWorker.js +13 -5
- package/dist/sandbox.d.ts +1 -1
- package/dist/sandbox.js +48 -8
- package/dist/seal.js +47 -8
- package/dist/smt.d.ts +7 -1
- package/dist/smt.js +202 -26
- package/dist/symbolic.d.ts +38 -3
- package/dist/symbolic.js +504 -47
- package/dist/types.d.ts +4 -0
- package/package.json +6 -6
package/dist/symbolic.js
CHANGED
|
@@ -8,33 +8,130 @@
|
|
|
8
8
|
// A proof with no `bounded` entry covers every input; with entries it covers every input within the bounds.
|
|
9
9
|
// Numbers are IEEE doubles (SMT-LIB floating point), except integer values, which are tracked exactly with an
|
|
10
10
|
// obligation that they stay within the safe integers, so the two agree with JavaScript.
|
|
11
|
-
import { readFileSync } from 'node:fs';
|
|
12
|
-
import { dirname, resolve } from 'node:path';
|
|
11
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
12
|
+
import { dirname, join, resolve } from 'node:path';
|
|
13
13
|
import ts from 'typescript';
|
|
14
14
|
import { plural } from "./plural.js";
|
|
15
15
|
export class Unsupported extends Error {
|
|
16
16
|
}
|
|
17
17
|
export const maxSafe = 9007199254740991n;
|
|
18
18
|
export const elementSort = (e) => (e === 'bool' ? 'Bool' : 'Int');
|
|
19
|
+
const interopHelpers = new Set(['__importStar', '__importDefault', '__toESM']);
|
|
20
|
+
function isFile(path) {
|
|
21
|
+
try {
|
|
22
|
+
return statSync(path).isFile();
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** Resolves a relative CommonJS specifier the way Node does: `base.js`, else `base/index.js`. */
|
|
29
|
+
export function resolveRelativeJs(fromFile, specifier) {
|
|
30
|
+
const base = resolve(dirname(fromFile), specifier);
|
|
31
|
+
if (/\.[cm]?js$/.test(base))
|
|
32
|
+
return base;
|
|
33
|
+
for (const ext of ['.js', '.cjs', '.mjs']) {
|
|
34
|
+
const file = `${base}${ext}`;
|
|
35
|
+
if (isFile(file))
|
|
36
|
+
return file;
|
|
37
|
+
}
|
|
38
|
+
for (const ext of ['.js', '.cjs', '.mjs']) {
|
|
39
|
+
const index = join(base, `index${ext}`);
|
|
40
|
+
if (isFile(index))
|
|
41
|
+
return index;
|
|
42
|
+
}
|
|
43
|
+
if (isFile(base))
|
|
44
|
+
return base;
|
|
45
|
+
return `${base}.js`;
|
|
46
|
+
}
|
|
47
|
+
/** Stat-validated cache of parsed compiled `.js` source files, shared across `Executor` attempts and `smt.ts`. */
|
|
48
|
+
const parsedJsCache = new Map();
|
|
49
|
+
/** Reads and parses a compiled `.js` file, reusing the AST while its size and modification time are unchanged. */
|
|
50
|
+
export function readParsedJs(file) {
|
|
51
|
+
let st;
|
|
52
|
+
try {
|
|
53
|
+
st = statSync(file);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
throw new Unsupported(`cannot read module ${file}`);
|
|
57
|
+
}
|
|
58
|
+
const cached = parsedJsCache.get(file);
|
|
59
|
+
if (cached !== undefined && cached.size === st.size && cached.mtimeMs === st.mtimeMs)
|
|
60
|
+
return cached.source;
|
|
61
|
+
let text;
|
|
62
|
+
try {
|
|
63
|
+
text = readFileSync(file, 'utf8');
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new Unsupported(`cannot read module ${file}`);
|
|
67
|
+
}
|
|
68
|
+
const source = ts.createSourceFile(file, text, ts.ScriptTarget.ES2022, true, ts.ScriptKind.JS);
|
|
69
|
+
if (parsedJsCache.size > 1000)
|
|
70
|
+
parsedJsCache.clear();
|
|
71
|
+
parsedJsCache.set(file, { size: st.size, mtimeMs: st.mtimeMs, source });
|
|
72
|
+
return source;
|
|
73
|
+
}
|
|
74
|
+
const fileIndexCache = new Map();
|
|
75
|
+
function cachedFileIndex(file, build) {
|
|
76
|
+
let st;
|
|
77
|
+
try {
|
|
78
|
+
st = statSync(file);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
throw new Unsupported(`cannot read module ${file}`);
|
|
82
|
+
}
|
|
83
|
+
const cached = fileIndexCache.get(file);
|
|
84
|
+
if (cached !== undefined && cached.size === st.size && cached.mtimeMs === st.mtimeMs)
|
|
85
|
+
return cached.index;
|
|
86
|
+
const source = readParsedJs(file);
|
|
87
|
+
const index = build(source);
|
|
88
|
+
if (fileIndexCache.size > 1000)
|
|
89
|
+
fileIndexCache.clear();
|
|
90
|
+
fileIndexCache.set(file, { size: st.size, mtimeMs: st.mtimeMs, index });
|
|
91
|
+
return index;
|
|
92
|
+
}
|
|
93
|
+
/** Extracts the relative path from `require('./x')` or an interop wrapper like `__importStar(require('./x'))`. */
|
|
94
|
+
function relativeRequire(expression) {
|
|
95
|
+
let call = expression;
|
|
96
|
+
while (ts.isCallExpression(call) && call.arguments.length >= 1) {
|
|
97
|
+
const callee = call.expression;
|
|
98
|
+
const name = ts.isIdentifier(callee) ? callee.text : ts.isPropertyAccessExpression(callee) ? callee.name.text : '';
|
|
99
|
+
if (!interopHelpers.has(name))
|
|
100
|
+
break;
|
|
101
|
+
call = call.arguments[0];
|
|
102
|
+
}
|
|
103
|
+
if (ts.isCallExpression(call) &&
|
|
104
|
+
ts.isIdentifier(call.expression) &&
|
|
105
|
+
call.expression.text === 'require' &&
|
|
106
|
+
call.arguments.length === 1 &&
|
|
107
|
+
ts.isStringLiteral(call.arguments[0]) &&
|
|
108
|
+
call.arguments[0].text.startsWith('.'))
|
|
109
|
+
return call.arguments[0].text;
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
19
112
|
class Frame {
|
|
20
113
|
env = new Map();
|
|
21
114
|
done = 'false';
|
|
22
115
|
brk = 'false';
|
|
23
116
|
cnt = 'false';
|
|
24
117
|
ret;
|
|
118
|
+
fnKey;
|
|
119
|
+
decreases;
|
|
25
120
|
file;
|
|
26
121
|
depth;
|
|
27
|
-
constructor(file, depth) {
|
|
122
|
+
constructor(file, depth, fnKey) {
|
|
28
123
|
this.file = file;
|
|
29
124
|
this.depth = depth;
|
|
125
|
+
this.fnKey = fnKey;
|
|
30
126
|
}
|
|
31
127
|
fork() {
|
|
32
|
-
const f = new Frame(this.file, this.depth);
|
|
128
|
+
const f = new Frame(this.file, this.depth, this.fnKey);
|
|
33
129
|
f.env = new Map(this.env);
|
|
34
130
|
f.done = this.done;
|
|
35
131
|
f.brk = this.brk;
|
|
36
132
|
f.cnt = this.cnt;
|
|
37
133
|
f.ret = this.ret;
|
|
134
|
+
f.decreases = this.decreases;
|
|
38
135
|
return f;
|
|
39
136
|
}
|
|
40
137
|
}
|
|
@@ -91,6 +188,14 @@ export class Executor {
|
|
|
91
188
|
havocked = 0;
|
|
92
189
|
assumptions = [];
|
|
93
190
|
obligations = [];
|
|
191
|
+
/** Intermediate proof steps from `lemma(...)` / `calc(...)` / `induct(...)`: each is proved under prior assumptions. */
|
|
192
|
+
subgoals = [];
|
|
193
|
+
/** Assumptions introduced by `lemma(...)` / `calc(...)` (omitted when sending the whole query to an independent certifier). */
|
|
194
|
+
lemmaAssumptions = new Set();
|
|
195
|
+
/** True when `induct()` discharged the claim via base + inductive step subgoals. */
|
|
196
|
+
usedInduction = false;
|
|
197
|
+
/** Active call frames for checking recursive `decreases(...)` termination measures. */
|
|
198
|
+
callFrames = [];
|
|
94
199
|
bounded = [];
|
|
95
200
|
/** Advice for the reader when a proof fails on an obligation (a divisor that may be 0). */
|
|
96
201
|
hints = [];
|
|
@@ -120,40 +225,42 @@ export class Executor {
|
|
|
120
225
|
const known = this.files.get(file);
|
|
121
226
|
if (known !== undefined)
|
|
122
227
|
return known;
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
for (const
|
|
132
|
-
|
|
133
|
-
|
|
228
|
+
const index = cachedFileIndex(file, (source) => {
|
|
229
|
+
const built = {
|
|
230
|
+
functions: new Map(),
|
|
231
|
+
imports: new Map(),
|
|
232
|
+
constants: new Map(),
|
|
233
|
+
reexports: new Map(),
|
|
234
|
+
stars: [],
|
|
235
|
+
};
|
|
236
|
+
for (const statement of source.statements) {
|
|
237
|
+
if (ts.isFunctionDeclaration(statement) && statement.name !== undefined && statement.body !== undefined)
|
|
238
|
+
built.functions.set(statement.name.text, statement);
|
|
239
|
+
if (!ts.isVariableStatement(statement))
|
|
134
240
|
continue;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
init
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
241
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
242
|
+
const init = declaration.initializer;
|
|
243
|
+
if (!ts.isIdentifier(declaration.name) || init === undefined)
|
|
244
|
+
continue;
|
|
245
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init))
|
|
246
|
+
built.functions.set(declaration.name.text, init);
|
|
247
|
+
else {
|
|
248
|
+
const specifier = relativeRequire(init);
|
|
249
|
+
if (specifier !== undefined)
|
|
250
|
+
built.imports.set(declaration.name.text, resolveRelativeJs(file, specifier));
|
|
251
|
+
else if ((declaration.parent.flags & ts.NodeFlags.Const) !== 0)
|
|
252
|
+
built.constants.set(declaration.name.text, init);
|
|
146
253
|
}
|
|
147
254
|
}
|
|
148
|
-
else if ((declaration.parent.flags & ts.NodeFlags.Const) !== 0)
|
|
149
|
-
index.constants.set(declaration.name.text, init);
|
|
150
255
|
}
|
|
151
|
-
|
|
152
|
-
|
|
256
|
+
this.indexExports(source, file, built);
|
|
257
|
+
return built;
|
|
258
|
+
});
|
|
259
|
+
this.files.set(file, index);
|
|
153
260
|
return index;
|
|
154
261
|
}
|
|
155
|
-
/** `exports.NAME = value` assigned exactly once, at the top level, is a constant;
|
|
156
|
-
indexExports(source, index) {
|
|
262
|
+
/** `exports.NAME = value` assigned exactly once, at the top level, is a constant; also records CommonJS re-exports. */
|
|
263
|
+
indexExports(source, file, index) {
|
|
157
264
|
const endsVoid = (e) => {
|
|
158
265
|
while (ts.isBinaryExpression(e) && e.operatorToken.kind === ts.SyntaxKind.EqualsToken)
|
|
159
266
|
e = e.right;
|
|
@@ -179,12 +286,113 @@ export class Executor {
|
|
|
179
286
|
};
|
|
180
287
|
visit(source);
|
|
181
288
|
for (const statement of source.statements) {
|
|
182
|
-
if (!ts.isExpressionStatement(statement)
|
|
289
|
+
if (!ts.isExpressionStatement(statement))
|
|
290
|
+
continue;
|
|
291
|
+
if (ts.isCallExpression(statement.expression)) {
|
|
292
|
+
const call = statement.expression;
|
|
293
|
+
const callee = call.expression;
|
|
294
|
+
const calleeName = ts.isIdentifier(callee)
|
|
295
|
+
? callee.text
|
|
296
|
+
: ts.isPropertyAccessExpression(callee)
|
|
297
|
+
? callee.name.text
|
|
298
|
+
: '';
|
|
299
|
+
// `__exportStar(require("./sub"), exports)` (tsc) or `__reExport(target, require("./sub"), module.exports)` (esbuild)
|
|
300
|
+
if (calleeName === '__exportStar' && call.arguments.length >= 1) {
|
|
301
|
+
const specifier = relativeRequire(call.arguments[0]);
|
|
302
|
+
if (specifier !== undefined)
|
|
303
|
+
index.stars.push(resolveRelativeJs(file, specifier));
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (calleeName === '__reExport' && call.arguments.length >= 2) {
|
|
307
|
+
const specifier = relativeRequire(call.arguments[1]);
|
|
308
|
+
if (specifier !== undefined)
|
|
309
|
+
index.stars.push(resolveRelativeJs(file, specifier));
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
// esbuild: `__export(stdin_exports, { name: () => import_mod.orig, local: () => localFn })`
|
|
313
|
+
if (calleeName === '__export' &&
|
|
314
|
+
call.arguments.length === 2 &&
|
|
315
|
+
ts.isObjectLiteralExpression(call.arguments[1])) {
|
|
316
|
+
for (const prop of call.arguments[1].properties) {
|
|
317
|
+
if (!ts.isPropertyAssignment(prop))
|
|
318
|
+
continue;
|
|
319
|
+
const exportedName = ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name) ? prop.name.text : undefined;
|
|
320
|
+
if (exportedName === undefined)
|
|
321
|
+
continue;
|
|
322
|
+
const getter = prop.initializer;
|
|
323
|
+
if (!ts.isArrowFunction(getter) && !ts.isFunctionExpression(getter))
|
|
324
|
+
continue;
|
|
325
|
+
const returned = ts.isBlock(getter.body)
|
|
326
|
+
? getter.body.statements.length === 1 && ts.isReturnStatement(getter.body.statements[0])
|
|
327
|
+
? getter.body.statements[0].expression
|
|
328
|
+
: undefined
|
|
329
|
+
: getter.body;
|
|
330
|
+
if (returned === undefined)
|
|
331
|
+
continue;
|
|
332
|
+
if (ts.isPropertyAccessExpression(returned) && ts.isIdentifier(returned.expression)) {
|
|
333
|
+
const target = index.imports.get(returned.expression.text);
|
|
334
|
+
if (target !== undefined)
|
|
335
|
+
index.reexports.set(exportedName, { file: target, name: returned.name.text });
|
|
336
|
+
}
|
|
337
|
+
else if (ts.isIdentifier(returned) &&
|
|
338
|
+
index.functions.has(returned.text) &&
|
|
339
|
+
!index.functions.has(exportedName)) {
|
|
340
|
+
index.functions.set(exportedName, index.functions.get(returned.text));
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
// tsc: `Object.defineProperty(exports, "name", { enumerable: true, get: function () { return mod_1.orig; } })`
|
|
346
|
+
if (ts.isPropertyAccessExpression(callee) &&
|
|
347
|
+
ts.isIdentifier(callee.expression) &&
|
|
348
|
+
callee.expression.text === 'Object' &&
|
|
349
|
+
callee.name.text === 'defineProperty' &&
|
|
350
|
+
call.arguments.length === 3 &&
|
|
351
|
+
ts.isIdentifier(call.arguments[0]) &&
|
|
352
|
+
call.arguments[0].text === 'exports' &&
|
|
353
|
+
ts.isStringLiteral(call.arguments[1]) &&
|
|
354
|
+
ts.isObjectLiteralExpression(call.arguments[2])) {
|
|
355
|
+
const exportedName = call.arguments[1].text;
|
|
356
|
+
const getter = call.arguments[2].properties.find((p) => ts.isPropertyAssignment(p) &&
|
|
357
|
+
ts.isIdentifier(p.name) &&
|
|
358
|
+
p.name.text === 'get' &&
|
|
359
|
+
(ts.isFunctionExpression(p.initializer) || ts.isArrowFunction(p.initializer)));
|
|
360
|
+
if (getter !== undefined) {
|
|
361
|
+
const fn = getter.initializer;
|
|
362
|
+
const returned = ts.isBlock(fn.body)
|
|
363
|
+
? fn.body.statements.length === 1 && ts.isReturnStatement(fn.body.statements[0])
|
|
364
|
+
? fn.body.statements[0].expression
|
|
365
|
+
: undefined
|
|
366
|
+
: fn.body;
|
|
367
|
+
if (returned !== undefined &&
|
|
368
|
+
ts.isPropertyAccessExpression(returned) &&
|
|
369
|
+
ts.isIdentifier(returned.expression)) {
|
|
370
|
+
const target = index.imports.get(returned.expression.text);
|
|
371
|
+
if (target !== undefined)
|
|
372
|
+
index.reexports.set(exportedName, { file: target, name: returned.name.text });
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (!ts.isBinaryExpression(statement.expression))
|
|
183
379
|
continue;
|
|
184
380
|
const e = statement.expression;
|
|
185
381
|
const name = exported(e.left);
|
|
186
382
|
if (e.operatorToken.kind !== ts.SyntaxKind.EqualsToken || name === undefined || endsVoid(e.right))
|
|
187
383
|
continue;
|
|
384
|
+
// `exports.default = fn` or `exports.alias = localFn`
|
|
385
|
+
if (ts.isIdentifier(e.right) && index.functions.has(e.right.text) && !index.functions.has(name)) {
|
|
386
|
+
index.functions.set(name, index.functions.get(e.right.text));
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (ts.isPropertyAccessExpression(e.right) && ts.isIdentifier(e.right.expression)) {
|
|
390
|
+
const target = index.imports.get(e.right.expression.text);
|
|
391
|
+
if (target !== undefined) {
|
|
392
|
+
index.reexports.set(name, { file: target, name: e.right.name.text });
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
188
396
|
if (writes.get(name) === 1 && !index.constants.has(name))
|
|
189
397
|
index.constants.set(name, e.right);
|
|
190
398
|
}
|
|
@@ -192,14 +400,27 @@ export class Executor {
|
|
|
192
400
|
constantValues = new Map();
|
|
193
401
|
resolving = new Set();
|
|
194
402
|
/** The value of a module-level constant of `file` (a literal or arithmetic over other such constants). */
|
|
195
|
-
constantValue(file, name) {
|
|
196
|
-
const init = this.index(file).constants.get(name);
|
|
197
|
-
if (init === undefined)
|
|
198
|
-
return undefined;
|
|
403
|
+
constantValue(file, name, seen = new Set()) {
|
|
199
404
|
const key = `${file}\0${name}`;
|
|
200
405
|
const known = this.constantValues.get(key);
|
|
201
406
|
if (known !== undefined)
|
|
202
407
|
return known;
|
|
408
|
+
if (seen.has(key))
|
|
409
|
+
return undefined;
|
|
410
|
+
seen.add(key);
|
|
411
|
+
const idx = this.index(file);
|
|
412
|
+
const init = idx.constants.get(name);
|
|
413
|
+
if (init === undefined) {
|
|
414
|
+
const reexport = idx.reexports.get(name);
|
|
415
|
+
if (reexport !== undefined)
|
|
416
|
+
return this.constantValue(reexport.file, reexport.name, seen);
|
|
417
|
+
for (const star of idx.stars) {
|
|
418
|
+
const found = this.constantValue(star, name, seen);
|
|
419
|
+
if (found !== undefined)
|
|
420
|
+
return found;
|
|
421
|
+
}
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
203
424
|
if (this.resolving.has(key))
|
|
204
425
|
throw new Unsupported(`constant ${name} refers to itself`);
|
|
205
426
|
this.simpleConstant(init, name);
|
|
@@ -533,7 +754,7 @@ export class Executor {
|
|
|
533
754
|
loop(_init, condition, incrementor, body, f, pc) {
|
|
534
755
|
const invariants = this.invariantsOf(body);
|
|
535
756
|
if (invariants !== undefined) {
|
|
536
|
-
this.invariantLoop(condition, incrementor, invariants.rest, invariants.exprs, f, pc);
|
|
757
|
+
this.invariantLoop(condition, incrementor, invariants.rest, invariants.exprs, invariants.decreases, f, pc);
|
|
537
758
|
return;
|
|
538
759
|
}
|
|
539
760
|
if (this.infer) {
|
|
@@ -559,21 +780,28 @@ export class Executor {
|
|
|
559
780
|
f.brk = 'false';
|
|
560
781
|
}
|
|
561
782
|
// ---- loops with invariants: a proof for every number of iterations -------------------------------------------
|
|
562
|
-
/** The `invariant(...)` calls leading a loop body, and the statements after them. */
|
|
783
|
+
/** The `invariant(...)` and `decreases(...)` calls leading a loop body, and the statements after them. */
|
|
563
784
|
invariantsOf(body) {
|
|
564
785
|
const statements = ts.isBlock(body) ? body.statements : [body];
|
|
565
786
|
const exprs = [];
|
|
787
|
+
const decreases = [];
|
|
566
788
|
let i = 0;
|
|
567
789
|
for (; i < statements.length; i += 1) {
|
|
568
790
|
const s = statements[i];
|
|
569
791
|
if (!ts.isExpressionStatement(s) || !ts.isCallExpression(s.expression))
|
|
570
792
|
break;
|
|
571
793
|
const callee = this.calleeOf(s.expression);
|
|
572
|
-
if (callee?.name
|
|
794
|
+
if (callee?.name === 'invariant' && s.expression.arguments.length === 1) {
|
|
795
|
+
exprs.push(s.expression.arguments[0]);
|
|
796
|
+
}
|
|
797
|
+
else if (callee?.name === 'decreases' && s.expression.arguments.length === 1) {
|
|
798
|
+
decreases.push(s.expression.arguments[0]);
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
573
801
|
break;
|
|
574
|
-
|
|
802
|
+
}
|
|
575
803
|
}
|
|
576
|
-
return exprs.length === 0 ? undefined : { exprs, rest: statements.slice(i) };
|
|
804
|
+
return exprs.length === 0 ? undefined : { exprs, decreases, rest: statements.slice(i) };
|
|
577
805
|
}
|
|
578
806
|
calleeOf(node) {
|
|
579
807
|
let callee = node.expression;
|
|
@@ -689,7 +917,7 @@ export class Executor {
|
|
|
689
917
|
* Hoare-style: the invariant must hold on entry and after any iteration that starts from a state satisfying it; the
|
|
690
918
|
* code after the loop sees an arbitrary such state where the condition is false. No unrolling, so no bound.
|
|
691
919
|
*/
|
|
692
|
-
invariantLoop(condition, incrementor, rest, exprs, f, pc) {
|
|
920
|
+
invariantLoop(condition, incrementor, rest, exprs, decreasesExprs, f, pc) {
|
|
693
921
|
// The condition is read from the arbitrary state of the loop, so a write in it would go unmodelled.
|
|
694
922
|
if (condition !== undefined && this.modifiedNames([condition]).size > 0)
|
|
695
923
|
throw new Unsupported('a loop whose condition changes a variable');
|
|
@@ -714,6 +942,15 @@ export class Executor {
|
|
|
714
942
|
}
|
|
715
943
|
const [holds, c] = this.evaluatedWhere(exprs, condition, run, this.guard(f, pc));
|
|
716
944
|
const inside = and(pc, holds, c);
|
|
945
|
+
// Termination measures (`decreases(...)`): must be >= 0 at loop entry of every iteration and strictly decrease.
|
|
946
|
+
const measuresBefore = [];
|
|
947
|
+
for (const d of decreasesExprs) {
|
|
948
|
+
const v = this.ev(d, run, inside);
|
|
949
|
+
if (v.k !== 'int' && v.k !== 'big')
|
|
950
|
+
throw new Unsupported('decreases() measure must be an integer');
|
|
951
|
+
this.oblige(run, inside, `(<= 0 ${v.t})`);
|
|
952
|
+
measuresBefore.push(v);
|
|
953
|
+
}
|
|
717
954
|
this.execList(rest, run, inside);
|
|
718
955
|
run.cnt = 'false';
|
|
719
956
|
if (incrementor !== undefined)
|
|
@@ -725,6 +962,13 @@ export class Executor {
|
|
|
725
962
|
throw new Unsupported(`${name} changes kind inside a loop with an invariant`);
|
|
726
963
|
}
|
|
727
964
|
this.oblige(run, inside, this.invariantTerm(exprs, run, this.guard(run, inside)));
|
|
965
|
+
for (let idx = 0; idx < decreasesExprs.length; idx += 1) {
|
|
966
|
+
const after = this.ev(decreasesExprs[idx], run, this.guard(run, inside));
|
|
967
|
+
const before = measuresBefore[idx];
|
|
968
|
+
if (after.k !== before.k)
|
|
969
|
+
throw new Unsupported('decreases() measure changed kind in loop');
|
|
970
|
+
this.oblige(run, inside, `(< ${after.t} ${before.t})`);
|
|
971
|
+
}
|
|
728
972
|
// After the loop: any state satisfying the invariant in which the condition is false, or the state of an iteration
|
|
729
973
|
// that started from such a state and left through a return or break. Which of the two is not known: a fresh choice.
|
|
730
974
|
const left = or(and(run.done, not(f.done)), run.brk);
|
|
@@ -1800,6 +2044,26 @@ export class Executor {
|
|
|
1800
2044
|
if (owner === 'tslaws_1' || /tslaws/.test(owner)) {
|
|
1801
2045
|
if (name === 'assume')
|
|
1802
2046
|
return this.assumeCall(node, f, pc);
|
|
2047
|
+
if (name === 'lemma')
|
|
2048
|
+
return this.lemmaCall(node, f, pc);
|
|
2049
|
+
if (name === 'requires')
|
|
2050
|
+
return this.requiresCall(node, f, pc);
|
|
2051
|
+
if (name === 'ensures')
|
|
2052
|
+
return this.ensuresCall(node, f, pc);
|
|
2053
|
+
if (name === 'decreases')
|
|
2054
|
+
return this.decreasesCall(node, f, pc);
|
|
2055
|
+
if (name === 'implies')
|
|
2056
|
+
return this.impliesCall(node, f, pc);
|
|
2057
|
+
if (name === 'iff')
|
|
2058
|
+
return this.iffCall(node, f, pc);
|
|
2059
|
+
if (name === 'distinct')
|
|
2060
|
+
return this.distinctCall(node, f, pc);
|
|
2061
|
+
if (name === 'unreachable')
|
|
2062
|
+
return this.unreachableCall(f, pc);
|
|
2063
|
+
if (name === 'calc')
|
|
2064
|
+
return this.calcCall(node, f, pc);
|
|
2065
|
+
if (name === 'induct')
|
|
2066
|
+
return this.inductCall(node, f, pc);
|
|
1803
2067
|
}
|
|
1804
2068
|
}
|
|
1805
2069
|
return this.method(callee.expression, name, node.arguments, f, pc, statement);
|
|
@@ -1807,6 +2071,26 @@ export class Executor {
|
|
|
1807
2071
|
if (ts.isIdentifier(callee)) {
|
|
1808
2072
|
if (callee.text === 'assume')
|
|
1809
2073
|
return this.assumeCall(node, f, pc);
|
|
2074
|
+
if (callee.text === 'lemma')
|
|
2075
|
+
return this.lemmaCall(node, f, pc);
|
|
2076
|
+
if (callee.text === 'requires')
|
|
2077
|
+
return this.requiresCall(node, f, pc);
|
|
2078
|
+
if (callee.text === 'ensures')
|
|
2079
|
+
return this.ensuresCall(node, f, pc);
|
|
2080
|
+
if (callee.text === 'decreases')
|
|
2081
|
+
return this.decreasesCall(node, f, pc);
|
|
2082
|
+
if (callee.text === 'implies')
|
|
2083
|
+
return this.impliesCall(node, f, pc);
|
|
2084
|
+
if (callee.text === 'iff')
|
|
2085
|
+
return this.iffCall(node, f, pc);
|
|
2086
|
+
if (callee.text === 'distinct')
|
|
2087
|
+
return this.distinctCall(node, f, pc);
|
|
2088
|
+
if (callee.text === 'unreachable')
|
|
2089
|
+
return this.unreachableCall(f, pc);
|
|
2090
|
+
if (callee.text === 'calc')
|
|
2091
|
+
return this.calcCall(node, f, pc);
|
|
2092
|
+
if (callee.text === 'induct')
|
|
2093
|
+
return this.inductCall(node, f, pc);
|
|
1810
2094
|
if (callee.text === 'Number' && node.arguments.length === 1) {
|
|
1811
2095
|
const v = this.ev(node.arguments[0], f, pc);
|
|
1812
2096
|
if (v.k === 'int' || v.k === 'fp')
|
|
@@ -1841,18 +2125,191 @@ export class Executor {
|
|
|
1841
2125
|
this.assume(f, pc, this.asBool(this.ev(node.arguments[0], f, pc)));
|
|
1842
2126
|
return { k: 'undef' };
|
|
1843
2127
|
}
|
|
2128
|
+
lemmaCall(node, f, pc) {
|
|
2129
|
+
if (node.arguments.length !== 1)
|
|
2130
|
+
throw new Unsupported('lemma takes one condition');
|
|
2131
|
+
const c = this.asBool(this.ev(node.arguments[0], f, pc));
|
|
2132
|
+
const guard = this.guard(f, pc);
|
|
2133
|
+
if (c !== 'true' && guard !== 'false') {
|
|
2134
|
+
const fact = guard === 'true' ? c : `(=> ${guard} ${c})`;
|
|
2135
|
+
// Record as an independent subgoal to be proved BEFORE being assumed for subsequent statements.
|
|
2136
|
+
this.subgoals.push({ assumptions: [...this.assumptions], goal: fact });
|
|
2137
|
+
this.lemmaAssumptions.add(fact);
|
|
2138
|
+
this.assumptions.push(fact);
|
|
2139
|
+
}
|
|
2140
|
+
return { k: 'undef' };
|
|
2141
|
+
}
|
|
2142
|
+
requiresCall(node, f, pc) {
|
|
2143
|
+
if (node.arguments.length !== 1)
|
|
2144
|
+
throw new Unsupported('requires takes one condition');
|
|
2145
|
+
const c = this.asBool(this.ev(node.arguments[0], f, pc));
|
|
2146
|
+
if (f.depth === 0)
|
|
2147
|
+
this.assume(f, pc, c);
|
|
2148
|
+
else
|
|
2149
|
+
this.oblige(f, pc, c);
|
|
2150
|
+
return { k: 'undef' };
|
|
2151
|
+
}
|
|
2152
|
+
impliesCall(node, f, pc) {
|
|
2153
|
+
if (node.arguments.length !== 2)
|
|
2154
|
+
throw new Unsupported('implies takes (premise, conclusion)');
|
|
2155
|
+
const p = this.asBool(this.ev(node.arguments[0], f, pc));
|
|
2156
|
+
if (p === 'false')
|
|
2157
|
+
return T;
|
|
2158
|
+
const q = this.asBool(this.ev(node.arguments[1], f, and(pc, p)));
|
|
2159
|
+
return bool(this.implies(p, q));
|
|
2160
|
+
}
|
|
2161
|
+
iffCall(node, f, pc) {
|
|
2162
|
+
if (node.arguments.length !== 2)
|
|
2163
|
+
throw new Unsupported('iff takes two boolean conditions');
|
|
2164
|
+
const a = this.asBool(this.ev(node.arguments[0], f, pc));
|
|
2165
|
+
const b = this.asBool(this.ev(node.arguments[1], f, pc));
|
|
2166
|
+
if (a === b)
|
|
2167
|
+
return T;
|
|
2168
|
+
return bool(`(= ${a} ${b})`);
|
|
2169
|
+
}
|
|
2170
|
+
distinctCall(node, f, pc) {
|
|
2171
|
+
const vals = node.arguments.map((a) => this.ev(a, f, pc));
|
|
2172
|
+
if (vals.length <= 1)
|
|
2173
|
+
return T;
|
|
2174
|
+
const checks = [];
|
|
2175
|
+
for (let i = 0; i < vals.length; i += 1) {
|
|
2176
|
+
for (let j = i + 1; j < vals.length; j += 1) {
|
|
2177
|
+
checks.push(not(this.strictEquals(vals[i], vals[j]).t));
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
return bool(and(...checks));
|
|
2181
|
+
}
|
|
2182
|
+
unreachableCall(f, pc) {
|
|
2183
|
+
this.oblige(f, pc, 'false');
|
|
2184
|
+
f.done = or(f.done, pc);
|
|
2185
|
+
return { k: 'undef' };
|
|
2186
|
+
}
|
|
2187
|
+
calcCall(node, f, pc) {
|
|
2188
|
+
if (node.arguments.length < 2)
|
|
2189
|
+
return T;
|
|
2190
|
+
const vals = node.arguments.map((a) => this.ev(a, f, pc));
|
|
2191
|
+
const guard = this.guard(f, pc);
|
|
2192
|
+
for (let i = 0; i + 1 < vals.length; i += 1) {
|
|
2193
|
+
const eq = this.strictEquals(vals[i], vals[i + 1]).t;
|
|
2194
|
+
if (eq !== 'true' && guard !== 'false') {
|
|
2195
|
+
const fact = guard === 'true' ? eq : `(=> ${guard} ${eq})`;
|
|
2196
|
+
this.subgoals.push({ assumptions: [...this.assumptions], goal: fact });
|
|
2197
|
+
this.lemmaAssumptions.add(fact);
|
|
2198
|
+
this.assumptions.push(fact);
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
return this.strictEquals(vals[0], vals[vals.length - 1]);
|
|
2202
|
+
}
|
|
2203
|
+
inductCall(node, f, pc) {
|
|
2204
|
+
if (node.arguments.length !== 2)
|
|
2205
|
+
throw new Unsupported('induct takes (n, (k) => predicate)');
|
|
2206
|
+
const nVal = this.ev(node.arguments[0], f, pc);
|
|
2207
|
+
if (nVal.k !== 'int')
|
|
2208
|
+
throw new Unsupported('induct() requires an integer argument');
|
|
2209
|
+
const fn = this.callback(node.arguments[1]);
|
|
2210
|
+
if (fn.parameters.length !== 1)
|
|
2211
|
+
throw new Unsupported('induct() predicate takes exactly one parameter (k)');
|
|
2212
|
+
const guard = this.guard(f, pc);
|
|
2213
|
+
this.oblige(f, pc, this.le(intLit(0n), nVal).t);
|
|
2214
|
+
// Base case: P(0) (capture obligations inside P(0) into the base subgoal)
|
|
2215
|
+
const baseOblStart = this.obligations.length;
|
|
2216
|
+
const base = this.asBool(this.applyCallback(fn, [intLit(0n)], f, pc));
|
|
2217
|
+
const baseObls = this.obligations.splice(baseOblStart);
|
|
2218
|
+
const baseGoal = and(base, ...baseObls);
|
|
2219
|
+
if (baseGoal !== 'true' && guard !== 'false') {
|
|
2220
|
+
this.subgoals.push({
|
|
2221
|
+
assumptions: [...this.assumptions],
|
|
2222
|
+
goal: guard === 'true' ? baseGoal : `(=> ${guard} ${baseGoal})`,
|
|
2223
|
+
});
|
|
2224
|
+
}
|
|
2225
|
+
// Inductive step: for all k with 0 <= k < n, P(k) ==> P(k + 1)
|
|
2226
|
+
const kName = `__ind_${++this.havocked}`;
|
|
2227
|
+
this.freshDecls.push(`(declare-const ${kName} Int)`);
|
|
2228
|
+
const kVal = { k: 'int', t: kName };
|
|
2229
|
+
const kNext = { k: 'int', t: `(+ ${kName} 1)` };
|
|
2230
|
+
const ihOblStart = this.obligations.length;
|
|
2231
|
+
const ih = this.asBool(this.applyCallback(fn, [kVal], f, pc));
|
|
2232
|
+
this.obligations.splice(ihOblStart);
|
|
2233
|
+
const stepOblStart = this.obligations.length;
|
|
2234
|
+
const step = this.asBool(this.applyCallback(fn, [kNext], f, pc));
|
|
2235
|
+
const stepObls = this.obligations.splice(stepOblStart);
|
|
2236
|
+
const stepGoal = and(step, ...stepObls);
|
|
2237
|
+
if (stepGoal !== 'true' && guard !== 'false') {
|
|
2238
|
+
this.subgoals.push({
|
|
2239
|
+
assumptions: [...this.assumptions, `(<= 0 ${kName})`, `(< ${kName} ${nVal.t})`, ih],
|
|
2240
|
+
goal: guard === 'true' ? stepGoal : `(=> ${guard} ${stepGoal})`,
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
this.usedInduction = true;
|
|
2244
|
+
return T;
|
|
2245
|
+
}
|
|
2246
|
+
ensuresCall(node, f, pc) {
|
|
2247
|
+
if (node.arguments.length !== 1)
|
|
2248
|
+
throw new Unsupported('ensures takes one condition');
|
|
2249
|
+
this.oblige(f, pc, this.asBool(this.ev(node.arguments[0], f, pc)));
|
|
2250
|
+
return { k: 'undef' };
|
|
2251
|
+
}
|
|
2252
|
+
decreasesCall(node, f, pc) {
|
|
2253
|
+
if (node.arguments.length !== 1)
|
|
2254
|
+
throw new Unsupported('decreases takes one measure');
|
|
2255
|
+
const v = this.ev(node.arguments[0], f, pc);
|
|
2256
|
+
if (v.k !== 'int' && v.k !== 'big')
|
|
2257
|
+
throw new Unsupported('decreases() measure must be an integer');
|
|
2258
|
+
this.oblige(f, pc, `(<= 0 ${v.t})`);
|
|
2259
|
+
// If this is a recursive call of the same function, verify strict decrease against the nearest caller frame.
|
|
2260
|
+
if (f.fnKey !== undefined) {
|
|
2261
|
+
for (let i = this.callFrames.length - 2; i >= 0; i -= 1) {
|
|
2262
|
+
const caller = this.callFrames[i];
|
|
2263
|
+
if (caller.fnKey === f.fnKey && caller.decreases !== undefined) {
|
|
2264
|
+
if (caller.decreases.k !== v.k)
|
|
2265
|
+
throw new Unsupported('decreases() measure changed kind across recursive call');
|
|
2266
|
+
this.oblige(f, pc, `(< ${v.t} ${caller.decreases.t})`);
|
|
2267
|
+
break;
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
f.decreases = v;
|
|
2272
|
+
return { k: 'undef' };
|
|
2273
|
+
}
|
|
2274
|
+
resolveFunction(file, name, seen = new Set()) {
|
|
2275
|
+
const key = `${file}\0${name}`;
|
|
2276
|
+
if (seen.has(key))
|
|
2277
|
+
return undefined;
|
|
2278
|
+
seen.add(key);
|
|
2279
|
+
const idx = this.index(file);
|
|
2280
|
+
const direct = idx.functions.get(name);
|
|
2281
|
+
if (direct !== undefined)
|
|
2282
|
+
return { file, fn: direct };
|
|
2283
|
+
const reexport = idx.reexports.get(name);
|
|
2284
|
+
if (reexport !== undefined)
|
|
2285
|
+
return this.resolveFunction(reexport.file, reexport.name, seen);
|
|
2286
|
+
for (const star of idx.stars) {
|
|
2287
|
+
const found = this.resolveFunction(star, name, seen);
|
|
2288
|
+
if (found !== undefined)
|
|
2289
|
+
return found;
|
|
2290
|
+
}
|
|
2291
|
+
return undefined;
|
|
2292
|
+
}
|
|
1844
2293
|
callProject(file, name, args, f, pc) {
|
|
1845
|
-
const
|
|
1846
|
-
if (
|
|
2294
|
+
const resolved = this.resolveFunction(file, name);
|
|
2295
|
+
if (resolved === undefined)
|
|
1847
2296
|
throw new Unsupported(`cannot find the source of ${name}`);
|
|
2297
|
+
const { file: targetFile, fn } = resolved;
|
|
1848
2298
|
const values = args.map((a) => this.escape(this.ev(a, f, pc)));
|
|
1849
2299
|
if (f.depth + 1 > this.bound + 2) {
|
|
1850
2300
|
this.cut(f, pc, 'true', `recursion is cut at depth ${this.bound}`);
|
|
1851
2301
|
return { k: 'undef' };
|
|
1852
2302
|
}
|
|
1853
|
-
const
|
|
2303
|
+
const fnKey = `${targetFile}\0${name}`;
|
|
2304
|
+
const inner = new Frame(targetFile, f.depth + 1, fnKey);
|
|
1854
2305
|
this.bindParams(inner, fn, values);
|
|
1855
|
-
this.
|
|
2306
|
+
this.callFrames.push(inner);
|
|
2307
|
+
try {
|
|
2308
|
+
this.body(fn, inner, this.guard(f, pc));
|
|
2309
|
+
}
|
|
2310
|
+
finally {
|
|
2311
|
+
this.callFrames.pop();
|
|
2312
|
+
}
|
|
1856
2313
|
if (inner.ret === undefined)
|
|
1857
2314
|
return { k: 'undef' };
|
|
1858
2315
|
return inner.ret;
|