eutaxy 0.4.0 → 0.5.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/README.md +2 -2
- package/dist/aws.d.mts +7 -5
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +41 -135
- package/dist/aws.mjs.map +1 -1
- package/dist/bin.mjs +2 -2
- package/dist/checked-CGpA2-F0.d.mts +285 -0
- package/dist/checked-CGpA2-F0.d.mts.map +1 -0
- package/dist/cli-IJrKkeYS.mjs +2 -0
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +2 -2
- package/dist/cloudflare.d.mts +232 -51
- package/dist/cloudflare.d.mts.map +1 -1
- package/dist/cloudflare.mjs +3 -1083
- package/dist/delivery-invariants-BraYqcYN.d.mts +5 -0
- package/dist/delivery-invariants-BraYqcYN.d.mts.map +1 -0
- package/dist/delivery-invariants-Cuxdna28.mjs +39 -0
- package/dist/delivery-invariants-Cuxdna28.mjs.map +1 -0
- package/dist/index.d.mts +66 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +81 -5
- package/dist/index.mjs.map +1 -0
- package/dist/{platform-TwATAkL3.d.mts → platform-DfMwknw1.d.mts} +179 -42
- package/dist/platform-DfMwknw1.d.mts.map +1 -0
- package/dist/src-Bqc9Eyb1.mjs +2655 -0
- package/dist/src-Bqc9Eyb1.mjs.map +1 -0
- package/dist/src-DKb-fkdb.mjs +3399 -0
- package/dist/src-DKb-fkdb.mjs.map +1 -0
- package/dist/src-Dxl9ZlKn.mjs +1752 -0
- package/dist/src-Dxl9ZlKn.mjs.map +1 -0
- package/package.json +1 -1
- package/dist/cli-Dr-Ziogu.mjs +0 -2
- package/dist/cloudflare.mjs.map +0 -1
- package/dist/explore-YVHTRjjS.d.mts +0 -63
- package/dist/explore-YVHTRjjS.d.mts.map +0 -1
- package/dist/platform-TwATAkL3.d.mts.map +0 -1
- package/dist/src-BBXd0WEp.mjs +0 -117
- package/dist/src-BBXd0WEp.mjs.map +0 -1
- package/dist/src-BGdIBGkN.mjs +0 -311
- package/dist/src-BGdIBGkN.mjs.map +0 -1
- package/dist/src-Brt0dfNL.mjs +0 -1286
- package/dist/src-Brt0dfNL.mjs.map +0 -1
- package/dist/src-s6uYyC0B.mjs +0 -89
- package/dist/src-s6uYyC0B.mjs.map +0 -1
- package/dist/verdict-evEM5Gxx.mjs +0 -12
- package/dist/verdict-evEM5Gxx.mjs.map +0 -1
|
@@ -0,0 +1,1752 @@
|
|
|
1
|
+
import { S as canonicalJson } from "./src-Bqc9Eyb1.mjs";
|
|
2
|
+
// packages/spec/src/ir.ts
|
|
3
|
+
const domains = [
|
|
4
|
+
"int",
|
|
5
|
+
"nat",
|
|
6
|
+
"number",
|
|
7
|
+
"boolean",
|
|
8
|
+
"string",
|
|
9
|
+
"bigint"
|
|
10
|
+
];
|
|
11
|
+
// packages/spec/src/tokens.ts
|
|
12
|
+
const glyphs = {
|
|
13
|
+
"∧": "and",
|
|
14
|
+
"∨": "or",
|
|
15
|
+
"¬": "not",
|
|
16
|
+
"→": "implies",
|
|
17
|
+
"↔": "iff",
|
|
18
|
+
"≡": "equiv",
|
|
19
|
+
"≢": "nequiv",
|
|
20
|
+
"≠": "neq",
|
|
21
|
+
"∀": "forall",
|
|
22
|
+
"∃": "exists",
|
|
23
|
+
"∈": "member",
|
|
24
|
+
"∞": "infinity"
|
|
25
|
+
};
|
|
26
|
+
/** ASCII spellings of Lemma connectives, longest first. */
|
|
27
|
+
const asciiOps = [
|
|
28
|
+
["<->", "iff"],
|
|
29
|
+
["==>", "implies"],
|
|
30
|
+
["->", "implies"],
|
|
31
|
+
["\\/", "or"]
|
|
32
|
+
];
|
|
33
|
+
const punctuators = [
|
|
34
|
+
">>>=",
|
|
35
|
+
"...",
|
|
36
|
+
"===",
|
|
37
|
+
"!==",
|
|
38
|
+
"**=",
|
|
39
|
+
"<<=",
|
|
40
|
+
">>=",
|
|
41
|
+
">>>",
|
|
42
|
+
"&&=",
|
|
43
|
+
"||=",
|
|
44
|
+
"??=",
|
|
45
|
+
"=>",
|
|
46
|
+
"==",
|
|
47
|
+
"!=",
|
|
48
|
+
"<=",
|
|
49
|
+
">=",
|
|
50
|
+
"&&",
|
|
51
|
+
"||",
|
|
52
|
+
"??",
|
|
53
|
+
"?.",
|
|
54
|
+
"++",
|
|
55
|
+
"--",
|
|
56
|
+
"+=",
|
|
57
|
+
"-=",
|
|
58
|
+
"*=",
|
|
59
|
+
"/=",
|
|
60
|
+
"%=",
|
|
61
|
+
"&=",
|
|
62
|
+
"|=",
|
|
63
|
+
"^=",
|
|
64
|
+
"**",
|
|
65
|
+
"<<",
|
|
66
|
+
">>"
|
|
67
|
+
];
|
|
68
|
+
/** Keywords after which a `/` starts a regular expression rather than a division. */
|
|
69
|
+
const regexAfter = /* @__PURE__ */ new Set([
|
|
70
|
+
"in",
|
|
71
|
+
"of",
|
|
72
|
+
"typeof",
|
|
73
|
+
"instanceof",
|
|
74
|
+
"return",
|
|
75
|
+
"new",
|
|
76
|
+
"delete",
|
|
77
|
+
"void",
|
|
78
|
+
"case",
|
|
79
|
+
"do",
|
|
80
|
+
"else",
|
|
81
|
+
"yield",
|
|
82
|
+
"await"
|
|
83
|
+
]);
|
|
84
|
+
const identStart = /[\p{ID_Start}$_]/u;
|
|
85
|
+
const identPart = /[\p{ID_Continue}$]/u;
|
|
86
|
+
const numberAt = /^(?:0[xXoObB][0-9a-fA-F_]+n?|(?:\d[\d_]*(?:\.[\d_]*)?|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?n?)/;
|
|
87
|
+
var TokenizeError = class extends Error {};
|
|
88
|
+
/** Keywords whose parenthesized condition is followed by a statement, not an operator. */
|
|
89
|
+
const controlKeywords = /* @__PURE__ */ new Set([
|
|
90
|
+
"if",
|
|
91
|
+
"while",
|
|
92
|
+
"for",
|
|
93
|
+
"with"
|
|
94
|
+
]);
|
|
95
|
+
/**
|
|
96
|
+
* Whether the code tokens up to `k` end an operand, so a `/` after them
|
|
97
|
+
* divides. `conditions` holds the `)` that close a control statement's
|
|
98
|
+
* condition (`if (x) /re/.test(s)`), after which a statement starts.
|
|
99
|
+
*/
|
|
100
|
+
function endsOperand$1(tokens, k, conditions, blocks) {
|
|
101
|
+
while (k >= 0 && tokens[k]?.kind === "comment") k--;
|
|
102
|
+
const t = tokens[k];
|
|
103
|
+
if (!t) return false;
|
|
104
|
+
switch (t.kind) {
|
|
105
|
+
case "ident": {
|
|
106
|
+
const before = tokens.slice(0, k).findLast((u) => u.kind !== "comment");
|
|
107
|
+
if (before?.kind === "punct" && (before.text === "." || before.text === "?.")) return true;
|
|
108
|
+
return !regexAfter.has(t.text);
|
|
109
|
+
}
|
|
110
|
+
case "punct":
|
|
111
|
+
if (t.text === "!" || t.text === "++" || t.text === "--") return endsOperand$1(tokens, k - 1, conditions, blocks);
|
|
112
|
+
if (t.text === ")") return !conditions.has(k);
|
|
113
|
+
if (t.text === "}") return !blocks.has(k);
|
|
114
|
+
return t.text === "]";
|
|
115
|
+
case "op": return false;
|
|
116
|
+
default: return true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** The end of a quoted string starting at `i`. */
|
|
120
|
+
function endOfString(text, i) {
|
|
121
|
+
const quote = text[i];
|
|
122
|
+
for (let j = i + 1; j < text.length; j++) {
|
|
123
|
+
const c = text[j];
|
|
124
|
+
if (c === "\\") j++;
|
|
125
|
+
else if (c === quote) return j + 1;
|
|
126
|
+
else if (c === "\n") break;
|
|
127
|
+
}
|
|
128
|
+
throw new TokenizeError(`unterminated string at offset ${i}`);
|
|
129
|
+
}
|
|
130
|
+
/** The end of the template literal starting at `i`, substitutions included. */
|
|
131
|
+
function endOfTemplate(text, i) {
|
|
132
|
+
for (let j = i + 1; j < text.length; j++) {
|
|
133
|
+
const c = text[j];
|
|
134
|
+
if (c === "\\") j++;
|
|
135
|
+
else if (c === "`") return j + 1;
|
|
136
|
+
else if (c === "$" && text[j + 1] === "{") j = endOfSubstitution(text, j + 2) - 1;
|
|
137
|
+
}
|
|
138
|
+
throw new TokenizeError(`unterminated template literal at offset ${i}`);
|
|
139
|
+
}
|
|
140
|
+
/** The offset just past the `}` that closes a substitution whose body starts at `i`, scanned as code. */
|
|
141
|
+
function endOfSubstitution(text, i) {
|
|
142
|
+
return scan(text, i, { untilBrace: true }).end;
|
|
143
|
+
}
|
|
144
|
+
/** The source of each `${…}` substitution in a template literal's text, outermost only. */
|
|
145
|
+
function templateSubstitutions(template) {
|
|
146
|
+
const out = [];
|
|
147
|
+
for (let j = 1; j < template.length; j++) {
|
|
148
|
+
const c = template[j];
|
|
149
|
+
if (c === "\\") j++;
|
|
150
|
+
else if (c === "$" && template[j + 1] === "{") {
|
|
151
|
+
const end = endOfSubstitution(template, j + 2);
|
|
152
|
+
out.push(template.slice(j + 2, end - 1));
|
|
153
|
+
j = end - 1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
/** The end of the regex literal starting at `i`, flags included. */
|
|
159
|
+
function endOfRegex(text, i) {
|
|
160
|
+
let inClass = false;
|
|
161
|
+
for (let j = i + 1; j < text.length; j++) {
|
|
162
|
+
const c = text[j];
|
|
163
|
+
if (c === "\\") j++;
|
|
164
|
+
else if (c === "\n") break;
|
|
165
|
+
else if (inClass) {
|
|
166
|
+
if (c === "]") inClass = false;
|
|
167
|
+
} else if (c === "[") inClass = true;
|
|
168
|
+
else if (c === "/") {
|
|
169
|
+
let k = j + 1;
|
|
170
|
+
while (k < text.length && identPart.test(text[k])) k++;
|
|
171
|
+
return k;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
throw new TokenizeError(`unterminated regular expression at offset ${i}`);
|
|
175
|
+
}
|
|
176
|
+
/** Tokens of `text`; comments are dropped unless `comments` is set (then they are "comment" tokens). */
|
|
177
|
+
function tokenize(text, options = {}) {
|
|
178
|
+
return scan(text, 0, options).tokens;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Scan from `start`. With `untilBrace`, stop after the `}` that closes an
|
|
182
|
+
* enclosing substitution (one not opened in the scanned code) and return
|
|
183
|
+
* the offset after it.
|
|
184
|
+
*/
|
|
185
|
+
function scan(text, start, options) {
|
|
186
|
+
const tokens = [];
|
|
187
|
+
const push = (kind, start, end, op) => {
|
|
188
|
+
tokens.push({
|
|
189
|
+
kind,
|
|
190
|
+
text: text.slice(start, end),
|
|
191
|
+
start,
|
|
192
|
+
end,
|
|
193
|
+
...op ? { op } : {}
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
let braces = 0;
|
|
197
|
+
const parens = [];
|
|
198
|
+
const conditions = /* @__PURE__ */ new Set();
|
|
199
|
+
const curlies = [];
|
|
200
|
+
const blocks = /* @__PURE__ */ new Set();
|
|
201
|
+
let i = start;
|
|
202
|
+
while (i < text.length) {
|
|
203
|
+
const c = text[i];
|
|
204
|
+
if (/\s/u.test(c)) {
|
|
205
|
+
i++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (c === "/" && text[i + 1] === "/") {
|
|
209
|
+
const from = i;
|
|
210
|
+
while (i < text.length && text[i] !== "\n") i++;
|
|
211
|
+
push("comment", from, i);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (c === "/" && text[i + 1] === "*") {
|
|
215
|
+
const close = text.indexOf("*/", i + 2);
|
|
216
|
+
if (close < 0) throw new TokenizeError(`unterminated comment at offset ${i}`);
|
|
217
|
+
push("comment", i, close + 2);
|
|
218
|
+
i = close + 2;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const glyph = glyphs[c];
|
|
222
|
+
if (glyph) {
|
|
223
|
+
push("op", i, i + 1, glyph);
|
|
224
|
+
i++;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const ascii = asciiOps.find(([s]) => text.startsWith(s, i));
|
|
228
|
+
if (ascii) {
|
|
229
|
+
push("op", i, i + ascii[0].length, ascii[1]);
|
|
230
|
+
i += ascii[0].length;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (c === "/") {
|
|
234
|
+
if (!endsOperand$1(tokens, tokens.length - 1, conditions, blocks)) {
|
|
235
|
+
const end = endOfRegex(text, i);
|
|
236
|
+
push("regex", i, end);
|
|
237
|
+
i = end;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (text[i + 1] === "\\") {
|
|
241
|
+
push("op", i, i + 2, "and");
|
|
242
|
+
i += 2;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (c === "\"" || c === "'") {
|
|
247
|
+
const end = endOfString(text, i);
|
|
248
|
+
push("string", i, end);
|
|
249
|
+
i = end;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (c === "`") {
|
|
253
|
+
const end = endOfTemplate(text, i);
|
|
254
|
+
push("template", i, end);
|
|
255
|
+
i = end;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
const num = numberAt.exec(text.slice(i));
|
|
259
|
+
if (num && (/\d/.test(c) || c === "." && /\d/.test(text[i + 1] ?? ""))) {
|
|
260
|
+
push("number", i, i + num[0].length);
|
|
261
|
+
i += num[0].length;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (identStart.test(c)) {
|
|
265
|
+
let j = i + 1;
|
|
266
|
+
while (j < text.length && identPart.test(text[j])) j++;
|
|
267
|
+
push("ident", i, j);
|
|
268
|
+
i = j;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const p = punctuators.find((s) => text.startsWith(s, i)) ?? c;
|
|
272
|
+
if (p === "(") {
|
|
273
|
+
const before = tokens.findLast((t) => t.kind !== "comment");
|
|
274
|
+
parens.push(before?.kind === "ident" && controlKeywords.has(before.text));
|
|
275
|
+
} else if (p === ")" && parens.pop()) conditions.add(tokens.length);
|
|
276
|
+
if (p === "{") {
|
|
277
|
+
const before = tokens.findLast((t) => t.kind !== "comment");
|
|
278
|
+
curlies.push(before === void 0 || before.kind === "punct" && [
|
|
279
|
+
")",
|
|
280
|
+
"{",
|
|
281
|
+
"}",
|
|
282
|
+
";",
|
|
283
|
+
"=>"
|
|
284
|
+
].includes(before.text) || before.kind === "ident" && [
|
|
285
|
+
"else",
|
|
286
|
+
"do",
|
|
287
|
+
"try",
|
|
288
|
+
"finally"
|
|
289
|
+
].includes(before.text));
|
|
290
|
+
} else if (p === "}" && curlies.pop()) blocks.add(tokens.length);
|
|
291
|
+
if (p === "{") braces++;
|
|
292
|
+
if (p === "}" && braces-- === 0 && options.untilBrace) return {
|
|
293
|
+
tokens: keep(tokens, options),
|
|
294
|
+
end: i + 1
|
|
295
|
+
};
|
|
296
|
+
push("punct", i, i + p.length);
|
|
297
|
+
i += p.length;
|
|
298
|
+
}
|
|
299
|
+
if (options.untilBrace) throw new TokenizeError(`unterminated template substitution at offset ${start}`);
|
|
300
|
+
return {
|
|
301
|
+
tokens: keep(tokens, options),
|
|
302
|
+
end: i
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
function keep(tokens, options) {
|
|
306
|
+
return options.comments ? tokens : tokens.filter((t) => t.kind !== "comment");
|
|
307
|
+
}
|
|
308
|
+
// packages/spec/src/parse.ts
|
|
309
|
+
/**
|
|
310
|
+
* The Lemma `@ensures` property parser: grammar.ebnf of lemma-lang, with
|
|
311
|
+
* the side conditions it states on atoms. A property is a quantifier
|
|
312
|
+
* prefix and a braced formula body; the body's structure (connectives,
|
|
313
|
+
* equations, grouping) is Lemma, and its leaves are TypeScript expressions
|
|
314
|
+
* kept verbatim. Validation that needs the enclosing module (free
|
|
315
|
+
* identifiers exported) is left to whoever evaluates the leaves.
|
|
316
|
+
*/
|
|
317
|
+
/** A property that is not Lemma. `stage` says which part rejected it. */
|
|
318
|
+
var LemmaSyntaxError = class extends Error {
|
|
319
|
+
stage;
|
|
320
|
+
constructor(stage, message) {
|
|
321
|
+
super(message);
|
|
322
|
+
this.stage = stage;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const numeric = /* @__PURE__ */ new Set([
|
|
326
|
+
"int",
|
|
327
|
+
"nat",
|
|
328
|
+
"number",
|
|
329
|
+
"bigint"
|
|
330
|
+
]);
|
|
331
|
+
const literalSyntax = {
|
|
332
|
+
int: /^[+-]?\d[\d_]*$/,
|
|
333
|
+
nat: /^[+-]?\d[\d_]*$/,
|
|
334
|
+
bigint: /^[+-]?\d[\d_]*n?$/,
|
|
335
|
+
number: /^[+-]?(?:\d[\d_]*(?:\.[\d_]*)?|\.\d[\d_]*)(?:[eE][+-]?\d+)?$/
|
|
336
|
+
};
|
|
337
|
+
const assignments = /* @__PURE__ */ new Set([
|
|
338
|
+
"=",
|
|
339
|
+
"+=",
|
|
340
|
+
"-=",
|
|
341
|
+
"*=",
|
|
342
|
+
"/=",
|
|
343
|
+
"%=",
|
|
344
|
+
"**=",
|
|
345
|
+
"<<=",
|
|
346
|
+
">>=",
|
|
347
|
+
">>>=",
|
|
348
|
+
"&=",
|
|
349
|
+
"|=",
|
|
350
|
+
"^=",
|
|
351
|
+
"&&=",
|
|
352
|
+
"||=",
|
|
353
|
+
"??="
|
|
354
|
+
]);
|
|
355
|
+
/** Parse the text after `@ensures{name}`. Throws LemmaSyntaxError. */
|
|
356
|
+
function parseProperty(text) {
|
|
357
|
+
let tokens;
|
|
358
|
+
try {
|
|
359
|
+
tokens = tokenize(text);
|
|
360
|
+
} catch (err) {
|
|
361
|
+
if (err instanceof TokenizeError) throw new LemmaSyntaxError("formula", err.message);
|
|
362
|
+
throw err;
|
|
363
|
+
}
|
|
364
|
+
const prefix = new Prefix(tokens);
|
|
365
|
+
return {
|
|
366
|
+
binders: prefix.parse(),
|
|
367
|
+
...new Body(text, tokens, prefix.at).parse()
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
const isOp = (t, op) => t?.kind === "op" && t.op === op;
|
|
371
|
+
const isPunct$1 = (t, p) => t?.kind === "punct" && t.text === p;
|
|
372
|
+
var Prefix = class {
|
|
373
|
+
tokens;
|
|
374
|
+
at = 0;
|
|
375
|
+
constructor(tokens) {
|
|
376
|
+
this.tokens = tokens;
|
|
377
|
+
}
|
|
378
|
+
fail(message) {
|
|
379
|
+
throw new LemmaSyntaxError("prefix", message);
|
|
380
|
+
}
|
|
381
|
+
peek() {
|
|
382
|
+
return this.tokens[this.at];
|
|
383
|
+
}
|
|
384
|
+
next(what) {
|
|
385
|
+
const t = this.tokens[this.at++];
|
|
386
|
+
if (!t) this.fail(`expected ${what}, found the end of the annotation`);
|
|
387
|
+
return t;
|
|
388
|
+
}
|
|
389
|
+
parse() {
|
|
390
|
+
const first = this.next("forall");
|
|
391
|
+
if (isOp(first, "exists") || first.kind === "ident" && first.text === "exists") this.fail("existential quantifiers are not Lemma: sampling cannot confirm that something exists, so state the property with forall");
|
|
392
|
+
if (!(isOp(first, "forall") || first.kind === "ident" && first.text === "forall")) this.fail(`a property starts with forall, not ${JSON.stringify(first.text)}`);
|
|
393
|
+
const binders = [];
|
|
394
|
+
while (isPunct$1(this.peek(), "(")) binders.push(...this.group());
|
|
395
|
+
if (binders.length === 0) this.fail("forall needs at least one binder group, as in (x: int)");
|
|
396
|
+
const seen = /* @__PURE__ */ new Set();
|
|
397
|
+
for (const b of binders) {
|
|
398
|
+
if (seen.has(b.name)) this.fail(`${b.name} is bound twice`);
|
|
399
|
+
seen.add(b.name);
|
|
400
|
+
}
|
|
401
|
+
if (!isPunct$1(this.peek(), "{")) this.fail("expected { to open the property body after the binders");
|
|
402
|
+
return binders;
|
|
403
|
+
}
|
|
404
|
+
group() {
|
|
405
|
+
this.next("(");
|
|
406
|
+
const names = [];
|
|
407
|
+
while (this.peek()?.kind === "ident") names.push(this.next("a name").text);
|
|
408
|
+
const colon = this.next(":");
|
|
409
|
+
if (!isPunct$1(colon, ":")) this.fail(names.length === 0 ? "a binder group needs at least one name before the colon, as in (x: int)" : `expected : before the domain, found ${JSON.stringify(colon.text)}`);
|
|
410
|
+
if (names.length === 0) this.fail("a binder group needs at least one name before the colon, as in (x: int)");
|
|
411
|
+
for (const n of names) if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(n)) this.fail(`${JSON.stringify(n)} is not a binder name`);
|
|
412
|
+
const d = this.next("a domain");
|
|
413
|
+
if (d.kind !== "ident" || !domains.includes(d.text)) this.fail(`${JSON.stringify(d.text)} is not a Lemma domain (one of ${domains.join(", ")})`);
|
|
414
|
+
const domain = d.text;
|
|
415
|
+
let guard;
|
|
416
|
+
const member = this.peek();
|
|
417
|
+
if (isOp(member, "member") || member?.kind === "ident" && member.text === "in") {
|
|
418
|
+
this.at++;
|
|
419
|
+
guard = this.guard(domain);
|
|
420
|
+
}
|
|
421
|
+
const close = this.next(")");
|
|
422
|
+
if (!isPunct$1(close, ")")) this.fail(`expected ) to close the binder group, found ${JSON.stringify(close.text)}`);
|
|
423
|
+
return names.map((name) => ({
|
|
424
|
+
name,
|
|
425
|
+
domain,
|
|
426
|
+
...guard ? { guard } : {}
|
|
427
|
+
}));
|
|
428
|
+
}
|
|
429
|
+
guard(domain) {
|
|
430
|
+
const t = this.peek();
|
|
431
|
+
if (t?.kind === "regex") {
|
|
432
|
+
this.at++;
|
|
433
|
+
if (domain !== "string") this.fail(`a regex guard applies to string, not ${domain}`);
|
|
434
|
+
const slash = t.text.lastIndexOf("/");
|
|
435
|
+
const pattern = t.text.slice(1, slash);
|
|
436
|
+
const flags = t.text.slice(slash + 1);
|
|
437
|
+
if (!/^[su]*$/.test(flags) || new Set(flags).size !== flags.length) this.fail(`a regex guard takes only the s and u flags, not ${JSON.stringify(flags)}`);
|
|
438
|
+
try {
|
|
439
|
+
new RegExp(`^(?:${pattern})$`, flags);
|
|
440
|
+
} catch (err) {
|
|
441
|
+
this.fail(`the regex guard does not compile: ${String(err)}`);
|
|
442
|
+
}
|
|
443
|
+
return {
|
|
444
|
+
kind: "regex",
|
|
445
|
+
pattern,
|
|
446
|
+
flags
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
if (!numeric.has(domain)) this.fail(`an interval guard applies to numeric domains only, not ${domain}`);
|
|
450
|
+
const open = this.next("[ or (");
|
|
451
|
+
if (!(isPunct$1(open, "[") || isPunct$1(open, "("))) this.fail(`expected an interval or a regex after ∈, found ${JSON.stringify(open.text)}`);
|
|
452
|
+
const lower = this.endpoint(domain, isPunct$1(open, "["));
|
|
453
|
+
const comma = this.next(",");
|
|
454
|
+
if (!isPunct$1(comma, ",")) this.fail(`expected , between the interval's endpoints, found ${JSON.stringify(comma.text)}`);
|
|
455
|
+
const upperValue = this.endpointValue(domain);
|
|
456
|
+
const close = this.next("] or )");
|
|
457
|
+
if (isPunct$1(close, ",")) this.fail("an interval has exactly two endpoints");
|
|
458
|
+
if (!(isPunct$1(close, "]") || isPunct$1(close, ")"))) this.fail(`expected ] or ) to close the interval, found ${JSON.stringify(close.text)}`);
|
|
459
|
+
const upper = {
|
|
460
|
+
value: upperValue,
|
|
461
|
+
inclusive: isPunct$1(close, "]")
|
|
462
|
+
};
|
|
463
|
+
this.checkInterval(domain, lower, upper);
|
|
464
|
+
return {
|
|
465
|
+
kind: "interval",
|
|
466
|
+
lower,
|
|
467
|
+
upper
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
endpoint(domain, inclusive) {
|
|
471
|
+
return {
|
|
472
|
+
value: this.endpointValue(domain),
|
|
473
|
+
inclusive
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
endpointValue(domain) {
|
|
477
|
+
let sign = "";
|
|
478
|
+
const s = this.peek();
|
|
479
|
+
if (isPunct$1(s, "-") || isPunct$1(s, "+")) {
|
|
480
|
+
this.at++;
|
|
481
|
+
sign = s?.text === "-" ? "-" : "";
|
|
482
|
+
}
|
|
483
|
+
const t = this.next("an endpoint");
|
|
484
|
+
if (isOp(t, "infinity") || t.kind === "ident" && t.text === "Infinity") return `${sign}∞`;
|
|
485
|
+
if (t.kind !== "number") this.fail(`${JSON.stringify(t.text)} is not an interval endpoint`);
|
|
486
|
+
const value = `${sign}${t.text}`;
|
|
487
|
+
const syntax = literalSyntax[domain];
|
|
488
|
+
if (syntax && !syntax.test(value)) this.fail(`${value} is not a ${domain} literal`);
|
|
489
|
+
return value;
|
|
490
|
+
}
|
|
491
|
+
checkInterval(domain, lower, upper) {
|
|
492
|
+
const integral = domain !== "number";
|
|
493
|
+
for (const e of [lower, upper]) if (e.value.endsWith("∞") && e.inclusive && integral) this.fail(`an ∞ endpoint must be exclusive for ${domain}`);
|
|
494
|
+
if (lower.value === "∞") this.fail("the lower endpoint cannot be +∞");
|
|
495
|
+
if (upper.value === "-∞") this.fail("the upper endpoint cannot be -∞");
|
|
496
|
+
const exact = (v) => integral && !v.endsWith("∞") ? BigInt(v.replace(/_/g, "").replace(/n$/, "")) : endpointNumber(v);
|
|
497
|
+
const lo = exact(lower.value);
|
|
498
|
+
const hi = exact(upper.value);
|
|
499
|
+
if (lo > hi || lo === hi && !(lower.inclusive && upper.inclusive)) this.fail(`the interval from ${lower.value} to ${upper.value} is empty`);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
/** An endpoint's value for ordering: exact for integer literals, as a double otherwise. */
|
|
503
|
+
function endpointNumber(value) {
|
|
504
|
+
if (value === "∞") return Number.POSITIVE_INFINITY;
|
|
505
|
+
if (value === "-∞") return Number.NEGATIVE_INFINITY;
|
|
506
|
+
return Number(value.replace(/_/g, "").replace(/n$/, ""));
|
|
507
|
+
}
|
|
508
|
+
const connectives = /* @__PURE__ */ new Set([
|
|
509
|
+
"and",
|
|
510
|
+
"or",
|
|
511
|
+
"not",
|
|
512
|
+
"implies",
|
|
513
|
+
"iff"
|
|
514
|
+
]);
|
|
515
|
+
var Body = class {
|
|
516
|
+
text;
|
|
517
|
+
tokens;
|
|
518
|
+
open;
|
|
519
|
+
/** Per token of the body: how many brackets enclose it inside the body braces. */
|
|
520
|
+
depth = /* @__PURE__ */ new Map();
|
|
521
|
+
/** Bracket matches, both ways. */
|
|
522
|
+
match = /* @__PURE__ */ new Map();
|
|
523
|
+
/** Per token: the innermost enclosing opening bracket, or -1. */
|
|
524
|
+
enclosing = /* @__PURE__ */ new Map();
|
|
525
|
+
constructor(text, tokens, open) {
|
|
526
|
+
this.text = text;
|
|
527
|
+
this.tokens = tokens;
|
|
528
|
+
this.open = open;
|
|
529
|
+
}
|
|
530
|
+
fail(message) {
|
|
531
|
+
throw new LemmaSyntaxError("formula", message);
|
|
532
|
+
}
|
|
533
|
+
parse() {
|
|
534
|
+
const pairs = {
|
|
535
|
+
")": "(",
|
|
536
|
+
"]": "[",
|
|
537
|
+
"}": "{"
|
|
538
|
+
};
|
|
539
|
+
const stack = [];
|
|
540
|
+
let close = -1;
|
|
541
|
+
for (let i = this.open; i < this.tokens.length; i++) {
|
|
542
|
+
const t = this.tokens[i];
|
|
543
|
+
this.depth.set(i, stack.length - 1);
|
|
544
|
+
this.enclosing.set(i, stack[stack.length - 1] ?? -1);
|
|
545
|
+
if (t.kind !== "punct") continue;
|
|
546
|
+
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
547
|
+
else if (pairs[t.text]) {
|
|
548
|
+
const o = stack.pop();
|
|
549
|
+
if (o === void 0 || this.tokens[o]?.text !== pairs[t.text]) this.fail(`unbalanced ${t.text} in the property body`);
|
|
550
|
+
this.match.set(o, i);
|
|
551
|
+
this.match.set(i, o);
|
|
552
|
+
if (stack.length === 0) {
|
|
553
|
+
close = i;
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
if (close < 0) this.fail("the property body is not closed with }");
|
|
559
|
+
if (close !== this.tokens.length - 1) this.fail("the } closing the property body must be the annotation's last token");
|
|
560
|
+
const s = this.open + 1;
|
|
561
|
+
const e = close;
|
|
562
|
+
if (s >= e) this.fail("the property body is empty");
|
|
563
|
+
const grouped = isPunct$1(this.tokens[s], "(") && this.match.get(s) === e - 1;
|
|
564
|
+
const f = this.formula(s, e, 0);
|
|
565
|
+
if (!grouped && f.kind === "implies") return {
|
|
566
|
+
requires: [...f.antecedents],
|
|
567
|
+
claim: f.consequent
|
|
568
|
+
};
|
|
569
|
+
return {
|
|
570
|
+
requires: [],
|
|
571
|
+
claim: f
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
at(i, base) {
|
|
575
|
+
return this.depth.get(i) === base;
|
|
576
|
+
}
|
|
577
|
+
/** Base-level positions of tokens satisfying `pred` in [s, e). */
|
|
578
|
+
split(s, e, base, pred) {
|
|
579
|
+
const out = [];
|
|
580
|
+
for (let i = s; i < e; i++) if (this.at(i, base) && pred(this.tokens[i], i)) out.push(i);
|
|
581
|
+
return out;
|
|
582
|
+
}
|
|
583
|
+
segments(s, e, cuts) {
|
|
584
|
+
const out = [];
|
|
585
|
+
let from = s;
|
|
586
|
+
for (const c of cuts) {
|
|
587
|
+
out.push([from, c]);
|
|
588
|
+
from = c + 1;
|
|
589
|
+
}
|
|
590
|
+
out.push([from, e]);
|
|
591
|
+
return out;
|
|
592
|
+
}
|
|
593
|
+
formula(s, e, base) {
|
|
594
|
+
if (s >= e) this.fail("a connective is missing an operand");
|
|
595
|
+
const iff = this.split(s, e, base, (t, i) => {
|
|
596
|
+
if (t.op === "iff") return true;
|
|
597
|
+
const prev = this.tokens[i - 1];
|
|
598
|
+
return t.kind === "ident" && t.text === "iff" && !isPunct$1(prev, ".") && !isPunct$1(prev, "?.");
|
|
599
|
+
});
|
|
600
|
+
if (iff.length > 1) this.fail("↔ is non-associative: parenthesize a ↔ b ↔ c");
|
|
601
|
+
if (iff.length === 1) {
|
|
602
|
+
const k = iff[0];
|
|
603
|
+
return {
|
|
604
|
+
kind: "iff",
|
|
605
|
+
left: this.formula(s, k, base),
|
|
606
|
+
right: this.formula(k + 1, e, base)
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
const implies = this.split(s, e, base, (t) => t.op === "implies");
|
|
610
|
+
if (implies.length > 0) {
|
|
611
|
+
const parts = this.segments(s, e, implies).map(([a, b]) => this.formula(a, b, base));
|
|
612
|
+
return {
|
|
613
|
+
kind: "implies",
|
|
614
|
+
antecedents: parts.slice(0, -1),
|
|
615
|
+
consequent: parts[parts.length - 1]
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
for (const kind of ["or", "and"]) {
|
|
619
|
+
const cuts = this.split(s, e, base, (t) => t.op === kind);
|
|
620
|
+
if (cuts.length > 0) return {
|
|
621
|
+
kind,
|
|
622
|
+
operands: this.segments(s, e, cuts).map(([a, b]) => this.formula(a, b, base))
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
if (isOp(this.tokens[s], "not")) return {
|
|
626
|
+
kind: "not",
|
|
627
|
+
operand: this.formula(s + 1, e, base)
|
|
628
|
+
};
|
|
629
|
+
if (isPunct$1(this.tokens[s], "(") && this.match.get(s) === e - 1) return this.formula(s + 1, e - 1, base + 1);
|
|
630
|
+
return this.atom(s, e, base);
|
|
631
|
+
}
|
|
632
|
+
slice(s, e) {
|
|
633
|
+
return this.text.slice(this.tokens[s].start, this.tokens[e - 1].end).trim();
|
|
634
|
+
}
|
|
635
|
+
atom(s, e, base) {
|
|
636
|
+
const equations = [];
|
|
637
|
+
for (let i = s; i < e; i++) {
|
|
638
|
+
const t = this.tokens[i];
|
|
639
|
+
const prev = i > s ? this.tokens[i - 1] : void 0;
|
|
640
|
+
this.checkToken(t, prev);
|
|
641
|
+
if (t.kind === "op") {
|
|
642
|
+
if (t.op === "equiv" || t.op === "nequiv") {
|
|
643
|
+
if (!this.at(i, base)) this.fail(`${t.text} only joins an atom's two sides; in nested positions call Object.is`);
|
|
644
|
+
equations.push(i);
|
|
645
|
+
} else if (connectives.has(t.op)) this.fail(`${t.text} inside a JavaScript expression: use && || ! there, or parenthesize the whole formula`);
|
|
646
|
+
else this.fail(`${t.text} belongs in a binder group, not the property body`);
|
|
647
|
+
}
|
|
648
|
+
if (t.kind !== "punct") continue;
|
|
649
|
+
if (assignments.has(t.text) && !this.inParameterList(i)) this.fail(`assignments are not allowed in a property (${t.text})`);
|
|
650
|
+
if (!this.at(i, base)) continue;
|
|
651
|
+
if (t.text === "&&" || t.text === "||") this.fail(`${t.text} at an atom's top level: use ${t.text === "&&" ? "∧" : "∨"}`);
|
|
652
|
+
if (t.text === "!" && !endsOperand(prev)) this.fail("prefix ! at an atom's top level: use ¬");
|
|
653
|
+
}
|
|
654
|
+
if (equations.length > 1) this.fail("≡ is non-associative: write a ≡ b ∧ b ≡ c");
|
|
655
|
+
if (equations.length === 0) return {
|
|
656
|
+
kind: "atom",
|
|
657
|
+
text: this.slice(s, e)
|
|
658
|
+
};
|
|
659
|
+
const k = equations[0];
|
|
660
|
+
for (let i = s; i < e; i++) {
|
|
661
|
+
const t = this.tokens[i];
|
|
662
|
+
if (this.at(i, base) && t.kind === "punct" && [
|
|
663
|
+
"??",
|
|
664
|
+
"?",
|
|
665
|
+
"===",
|
|
666
|
+
"!==",
|
|
667
|
+
",",
|
|
668
|
+
"=>"
|
|
669
|
+
].includes(t.text)) this.fail(`${t.text} beside an equation is ambiguous: parenthesize the side it belongs to`);
|
|
670
|
+
}
|
|
671
|
+
if (k === s || k === e - 1) this.fail(`${this.tokens[k]?.text} needs an expression on both sides`);
|
|
672
|
+
return {
|
|
673
|
+
kind: "equation",
|
|
674
|
+
left: this.slice(s, k),
|
|
675
|
+
right: this.slice(k + 1, e),
|
|
676
|
+
negated: this.tokens[k]?.op === "nequiv"
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* What no position in a property allows, for one token of an atom or of
|
|
681
|
+
* an expression nested in one: ≠, quantifiers, loose equality; a
|
|
682
|
+
* template's substitutions are nested expressions under the same rules.
|
|
683
|
+
*/
|
|
684
|
+
checkToken(t, prev) {
|
|
685
|
+
if (t.kind === "op") {
|
|
686
|
+
if (t.op === "neq") this.fail("≠ is not Lemma: write ≢ (not the same value) or !==");
|
|
687
|
+
if (t.op === "forall" || t.op === "exists") this.fail("quantifiers nest only in the prefix");
|
|
688
|
+
}
|
|
689
|
+
if (t.kind === "ident" && (t.text === "forall" || t.text === "exists") && !isPunct$1(prev, ".") && !isPunct$1(prev, "?.")) this.fail("quantifiers nest only in the prefix");
|
|
690
|
+
if (t.kind === "template") for (const sub of templateSubstitutions(t.text)) this.nested(sub);
|
|
691
|
+
if (t.kind === "punct" && (t.text === "==" || t.text === "!=")) this.fail(`loose ${t.text} is not allowed: use ≡ / ≢ or === / !==`);
|
|
692
|
+
}
|
|
693
|
+
/** Check an expression nested in an atom (a template substitution): what no position allows. */
|
|
694
|
+
nested(text) {
|
|
695
|
+
let tokens;
|
|
696
|
+
try {
|
|
697
|
+
tokens = tokenize(text);
|
|
698
|
+
} catch (err) {
|
|
699
|
+
if (err instanceof TokenizeError) this.fail(err.message);
|
|
700
|
+
throw err;
|
|
701
|
+
}
|
|
702
|
+
const stack = [];
|
|
703
|
+
const enclosing = [];
|
|
704
|
+
const match = /* @__PURE__ */ new Map();
|
|
705
|
+
tokens.forEach((t, i) => {
|
|
706
|
+
enclosing.push(stack[stack.length - 1] ?? -1);
|
|
707
|
+
if (t.kind !== "punct") return;
|
|
708
|
+
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
709
|
+
else if (t.text === ")" || t.text === "]" || t.text === "}") {
|
|
710
|
+
const o = stack.pop();
|
|
711
|
+
if (o !== void 0) match.set(o, i);
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
tokens.forEach((t, i) => {
|
|
715
|
+
this.checkToken(t, tokens[i - 1]);
|
|
716
|
+
if (t.kind === "op") {
|
|
717
|
+
if (t.op === "equiv" || t.op === "nequiv") this.fail(`${t.text} only joins an atom's two sides; in nested positions call Object.is`);
|
|
718
|
+
this.fail(`${t.text} inside a JavaScript expression: use && || ! there, or parenthesize the whole formula`);
|
|
719
|
+
}
|
|
720
|
+
if (t.kind !== "punct") return;
|
|
721
|
+
if (assignments.has(t.text)) {
|
|
722
|
+
const open = enclosing[i] ?? -1;
|
|
723
|
+
const close = open >= 0 ? match.get(open) : void 0;
|
|
724
|
+
if (!(t.text === "=" && isPunct$1(tokens[open], "(") && close !== void 0 && isPunct$1(tokens[close + 1], "=>"))) this.fail(`assignments are not allowed in a property (${t.text})`);
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
/** A default initializer: `=` directly inside the parentheses of an arrow function's parameters. */
|
|
729
|
+
inParameterList(i) {
|
|
730
|
+
if (this.tokens[i]?.text !== "=") return false;
|
|
731
|
+
const open = this.enclosing.get(i) ?? -1;
|
|
732
|
+
if (open < 0 || !isPunct$1(this.tokens[open], "(")) return false;
|
|
733
|
+
const close = this.match.get(open);
|
|
734
|
+
return close !== void 0 && isPunct$1(this.tokens[close + 1], "=>");
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
/** Whether a token ends an operand, so a `!` after it is postfix (non-null assertion). */
|
|
738
|
+
function endsOperand(t) {
|
|
739
|
+
if (!t) return false;
|
|
740
|
+
if (t.kind === "punct") return t.text === ")" || t.text === "]" || t.text === "}";
|
|
741
|
+
return t.kind !== "op";
|
|
742
|
+
}
|
|
743
|
+
// packages/spec/src/extract.ts
|
|
744
|
+
const tagLine = /^@(\w+)(?:\{([^}]*)\})?/;
|
|
745
|
+
/** The JSDoc's tags, each with its text up to the next tag. */
|
|
746
|
+
function tags(comment, lineOf) {
|
|
747
|
+
const body = comment.text.slice(3, -2);
|
|
748
|
+
const first = lineOf(comment.start);
|
|
749
|
+
const out = [];
|
|
750
|
+
body.split("\n").forEach((raw, i) => {
|
|
751
|
+
const line = raw.replace(/^\s*\*?\s?/, "");
|
|
752
|
+
const m = tagLine.exec(line.trimStart());
|
|
753
|
+
if (m) out.push({
|
|
754
|
+
name: m[1],
|
|
755
|
+
label: m[2],
|
|
756
|
+
lines: [line.trimStart().slice(m[0].length)],
|
|
757
|
+
line: first + i
|
|
758
|
+
});
|
|
759
|
+
else out[out.length - 1]?.lines.push(line);
|
|
760
|
+
});
|
|
761
|
+
return out.map((t) => ({
|
|
762
|
+
name: t.name,
|
|
763
|
+
label: t.label,
|
|
764
|
+
text: t.lines.join("\n").trim(),
|
|
765
|
+
line: t.line
|
|
766
|
+
}));
|
|
767
|
+
}
|
|
768
|
+
const isWord = (t, w) => t?.kind === "ident" && t.text === w;
|
|
769
|
+
const isPunct = (t, p) => t?.kind === "punct" && t.text === p;
|
|
770
|
+
/** `export [async] function [*] name(` at `i`: the name and the index of `(`. */
|
|
771
|
+
function functionAt(tokens, i) {
|
|
772
|
+
if (!isWord(tokens[i], "export")) return null;
|
|
773
|
+
let j = i + 1;
|
|
774
|
+
if (isWord(tokens[j], "async")) j++;
|
|
775
|
+
if (!isWord(tokens[j], "function")) return null;
|
|
776
|
+
j++;
|
|
777
|
+
if (isPunct(tokens[j], "*")) j++;
|
|
778
|
+
const name = tokens[j];
|
|
779
|
+
if (name?.kind !== "ident") return null;
|
|
780
|
+
j++;
|
|
781
|
+
if (isPunct(tokens[j], "<")) j = skipAngles(tokens, j);
|
|
782
|
+
return isPunct(tokens[j], "(") ? {
|
|
783
|
+
name: name.text,
|
|
784
|
+
open: j
|
|
785
|
+
} : null;
|
|
786
|
+
}
|
|
787
|
+
/** The index after the `>` closing the type parameters opened at `i`. */
|
|
788
|
+
function skipAngles(tokens, i) {
|
|
789
|
+
let depth = 0;
|
|
790
|
+
for (let j = i; j < tokens.length; j++) {
|
|
791
|
+
const t = tokens[j];
|
|
792
|
+
if (t.kind !== "punct") continue;
|
|
793
|
+
if (t.text === "<") depth++;
|
|
794
|
+
else if (t.text === ">" || t.text === ">>" || t.text === ">>>") {
|
|
795
|
+
depth -= t.text.length;
|
|
796
|
+
if (depth <= 0) return j + 1;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return tokens.length;
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Parameter names of the list opened at `open`; null for destructured and
|
|
803
|
+
* rest parameters. `ambiguous` when a default value has a top-level `<` or
|
|
804
|
+
* `>`: a comparison or a generic call's type arguments (`pair<A, B>()`),
|
|
805
|
+
* which a token scan cannot tell apart, and guessing wrong would bind a
|
|
806
|
+
* binder to another parameter's argument.
|
|
807
|
+
*/
|
|
808
|
+
function params(tokens, open) {
|
|
809
|
+
const firsts = [];
|
|
810
|
+
let depth = 0;
|
|
811
|
+
let start = true;
|
|
812
|
+
let inType = false;
|
|
813
|
+
let inDefault = false;
|
|
814
|
+
let ambiguous = false;
|
|
815
|
+
for (let j = open + 1; j < tokens.length; j++) {
|
|
816
|
+
const t = tokens[j];
|
|
817
|
+
if (depth === 0 && isPunct(t, ")")) break;
|
|
818
|
+
if (start) firsts.push(t);
|
|
819
|
+
start = false;
|
|
820
|
+
if (t.kind !== "punct") continue;
|
|
821
|
+
if (inDefault && depth === 0 && /^(<|>)/.test(t.text) && t.text !== ">=" && t.text !== "<=") ambiguous = true;
|
|
822
|
+
if ([
|
|
823
|
+
"(",
|
|
824
|
+
"[",
|
|
825
|
+
"{"
|
|
826
|
+
].includes(t.text) || inType && t.text === "<") depth++;
|
|
827
|
+
else if ([
|
|
828
|
+
")",
|
|
829
|
+
"]",
|
|
830
|
+
"}"
|
|
831
|
+
].includes(t.text)) depth--;
|
|
832
|
+
else if (inType && /^>+$/.test(t.text)) depth -= t.text.length;
|
|
833
|
+
else if (depth === 0 && t.text === ":") inType = true;
|
|
834
|
+
else if (depth === 0 && t.text === "=") {
|
|
835
|
+
inType = false;
|
|
836
|
+
inDefault = true;
|
|
837
|
+
} else if (t.text === "," && depth === 0) {
|
|
838
|
+
start = true;
|
|
839
|
+
inType = false;
|
|
840
|
+
inDefault = false;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
return {
|
|
844
|
+
names: firsts.filter((t) => !isWord(t, "this")).map((t) => t.kind === "ident" ? t.text : null),
|
|
845
|
+
ambiguous
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
/** Names the module exports as values (not types). */
|
|
849
|
+
function exportedNames(tokens) {
|
|
850
|
+
const names = /* @__PURE__ */ new Set();
|
|
851
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
852
|
+
if (!isWord(tokens[i], "export")) continue;
|
|
853
|
+
let j = i + 1;
|
|
854
|
+
if (isWord(tokens[j], "declare")) j++;
|
|
855
|
+
if (isWord(tokens[j], "default")) continue;
|
|
856
|
+
if (isWord(tokens[j], "async")) j++;
|
|
857
|
+
if (isWord(tokens[j], "abstract")) j++;
|
|
858
|
+
const kw = tokens[j];
|
|
859
|
+
if ([
|
|
860
|
+
"function",
|
|
861
|
+
"class",
|
|
862
|
+
"enum"
|
|
863
|
+
].some((w) => isWord(kw, w))) {
|
|
864
|
+
let k = j + 1;
|
|
865
|
+
if (isPunct(tokens[k], "*")) k++;
|
|
866
|
+
if (tokens[k]?.kind === "ident") names.add(tokens[k].text);
|
|
867
|
+
} else if ([
|
|
868
|
+
"const",
|
|
869
|
+
"let",
|
|
870
|
+
"var"
|
|
871
|
+
].some((w) => isWord(kw, w))) {
|
|
872
|
+
let depth = 0;
|
|
873
|
+
let expectName = true;
|
|
874
|
+
for (let k = j + 1; k < tokens.length; k++) {
|
|
875
|
+
const t = tokens[k];
|
|
876
|
+
if (expectName && depth === 0 && t.kind === "ident") names.add(t.text);
|
|
877
|
+
expectName = false;
|
|
878
|
+
if (t.kind !== "punct") continue;
|
|
879
|
+
if ([
|
|
880
|
+
"(",
|
|
881
|
+
"[",
|
|
882
|
+
"{"
|
|
883
|
+
].includes(t.text)) depth++;
|
|
884
|
+
else if ([
|
|
885
|
+
")",
|
|
886
|
+
"]",
|
|
887
|
+
"}"
|
|
888
|
+
].includes(t.text)) depth--;
|
|
889
|
+
else if (t.text === "," && depth === 0) expectName = true;
|
|
890
|
+
else if (t.text === ";" && depth === 0) break;
|
|
891
|
+
if (depth < 0) break;
|
|
892
|
+
}
|
|
893
|
+
} else if (isPunct(kw, "{")) {
|
|
894
|
+
const close = tokens.findIndex((t, k) => k > j && isPunct(t, "}"));
|
|
895
|
+
if (close < 0 || isWord(tokens[close + 1], "from")) continue;
|
|
896
|
+
for (let k = j + 1; k < close; k++) {
|
|
897
|
+
const t = tokens[k];
|
|
898
|
+
if (t.kind !== "ident" || isWord(tokens[k - 1], "type")) continue;
|
|
899
|
+
if (isWord(tokens[k + 1], "as")) continue;
|
|
900
|
+
if (isWord(t, "as") || isWord(t, "type")) continue;
|
|
901
|
+
names.add(t.text);
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
return [...names];
|
|
906
|
+
}
|
|
907
|
+
function extractSpecs(source) {
|
|
908
|
+
const starts = [0];
|
|
909
|
+
for (let i = 0; i < source.length; i++) if (source[i] === "\n") starts.push(i + 1);
|
|
910
|
+
const lineOf = (offset) => {
|
|
911
|
+
let lo = 0;
|
|
912
|
+
let hi = starts.length - 1;
|
|
913
|
+
while (lo < hi) {
|
|
914
|
+
const mid = lo + hi + 1 >> 1;
|
|
915
|
+
if (starts[mid] <= offset) lo = mid;
|
|
916
|
+
else hi = mid - 1;
|
|
917
|
+
}
|
|
918
|
+
return lo + 1;
|
|
919
|
+
};
|
|
920
|
+
let all;
|
|
921
|
+
try {
|
|
922
|
+
all = tokenize(source, { comments: true });
|
|
923
|
+
} catch (err) {
|
|
924
|
+
if (err instanceof TokenizeError) return {
|
|
925
|
+
functions: [],
|
|
926
|
+
exports: [],
|
|
927
|
+
errors: [{
|
|
928
|
+
line: 1,
|
|
929
|
+
message: err.message
|
|
930
|
+
}]
|
|
931
|
+
};
|
|
932
|
+
throw err;
|
|
933
|
+
}
|
|
934
|
+
const code = all.filter((t) => t.kind !== "comment");
|
|
935
|
+
const indexOf = new Map(code.map((t, i) => [t, i]));
|
|
936
|
+
const errors = [];
|
|
937
|
+
const functions = [];
|
|
938
|
+
all.forEach((comment, i) => {
|
|
939
|
+
if (comment.kind !== "comment" || !comment.text.startsWith("/**")) return;
|
|
940
|
+
const found = tags(comment, lineOf).filter((t) => [
|
|
941
|
+
"ensures",
|
|
942
|
+
"total",
|
|
943
|
+
"throws"
|
|
944
|
+
].includes(t.name));
|
|
945
|
+
if (found.length === 0) return;
|
|
946
|
+
const next = all.slice(i + 1).find((t) => t.kind !== "comment");
|
|
947
|
+
const decl = next ? functionAt(code, indexOf.get(next) ?? -1) : null;
|
|
948
|
+
if (!decl) {
|
|
949
|
+
errors.push({
|
|
950
|
+
line: found[0]?.line ?? lineOf(comment.start),
|
|
951
|
+
message: "Lemma annotations attach to an exported function declaration"
|
|
952
|
+
});
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
const ensures = [];
|
|
956
|
+
for (const t of found) {
|
|
957
|
+
if (t.name !== "ensures") continue;
|
|
958
|
+
const name = t.label?.trim();
|
|
959
|
+
if (!name) {
|
|
960
|
+
errors.push({
|
|
961
|
+
line: t.line,
|
|
962
|
+
message: "@ensures needs a name, as in @ensures{non-negative}"
|
|
963
|
+
});
|
|
964
|
+
continue;
|
|
965
|
+
}
|
|
966
|
+
if (ensures.some((e) => e.name === name)) {
|
|
967
|
+
errors.push({
|
|
968
|
+
line: t.line,
|
|
969
|
+
message: `${decl.name} has two properties named ${name}`
|
|
970
|
+
});
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
try {
|
|
974
|
+
ensures.push({
|
|
975
|
+
name,
|
|
976
|
+
property: parseProperty(t.text),
|
|
977
|
+
text: t.text
|
|
978
|
+
});
|
|
979
|
+
} catch (err) {
|
|
980
|
+
if (!(err instanceof LemmaSyntaxError)) throw err;
|
|
981
|
+
errors.push({
|
|
982
|
+
line: t.line,
|
|
983
|
+
message: `@ensures{${name}} on ${decl.name}: ${err.message}`
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
if (functions.some((f) => f.name === decl.name)) {
|
|
988
|
+
errors.push({
|
|
989
|
+
line: lineOf(code[decl.open].start),
|
|
990
|
+
message: `${decl.name} is annotated on more than one declaration (overloads): put its annotations on one`
|
|
991
|
+
});
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
const signature = params(code, decl.open);
|
|
995
|
+
if (signature.ambiguous) errors.push({
|
|
996
|
+
line: lineOf(code[decl.open].start),
|
|
997
|
+
message: `cannot tell the parameters of ${decl.name} apart: a default value has a top-level < or >; parenthesize it`
|
|
998
|
+
});
|
|
999
|
+
functions.push({
|
|
1000
|
+
name: decl.name,
|
|
1001
|
+
params: signature.names,
|
|
1002
|
+
ensures,
|
|
1003
|
+
total: found.some((t) => t.name === "total"),
|
|
1004
|
+
throws: found.some((t) => t.name === "throws"),
|
|
1005
|
+
line: lineOf(code[decl.open].start)
|
|
1006
|
+
});
|
|
1007
|
+
});
|
|
1008
|
+
return {
|
|
1009
|
+
functions,
|
|
1010
|
+
exports: exportedNames(code),
|
|
1011
|
+
errors
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
// packages/spec/src/predicate.ts
|
|
1015
|
+
const tsType = {
|
|
1016
|
+
int: "number",
|
|
1017
|
+
nat: "number",
|
|
1018
|
+
number: "number",
|
|
1019
|
+
boolean: "boolean",
|
|
1020
|
+
string: "string",
|
|
1021
|
+
bigint: "bigint"
|
|
1022
|
+
};
|
|
1023
|
+
/** Words that cannot name a binding. */
|
|
1024
|
+
const reserved = new Set("await break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield arguments eval".split(" "));
|
|
1025
|
+
function islands(f) {
|
|
1026
|
+
switch (f.kind) {
|
|
1027
|
+
case "atom": return [f.text];
|
|
1028
|
+
case "equation": return [f.left, f.right];
|
|
1029
|
+
case "not": return islands(f.operand);
|
|
1030
|
+
case "and":
|
|
1031
|
+
case "or": return f.operands.flatMap(islands);
|
|
1032
|
+
case "implies": return [...f.antecedents, f.consequent].flatMap(islands);
|
|
1033
|
+
case "iff": return [...islands(f.left), ...islands(f.right)];
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Indices of the tokens that are TypeScript type positions or binding
|
|
1038
|
+
* names, not references, and the scopes arrow functions bind names in.
|
|
1039
|
+
*/
|
|
1040
|
+
function notReferences(tokens) {
|
|
1041
|
+
const skip = /* @__PURE__ */ new Set();
|
|
1042
|
+
const scopes = [];
|
|
1043
|
+
const match = /* @__PURE__ */ new Map();
|
|
1044
|
+
const stack = [];
|
|
1045
|
+
/** Per token, the innermost open bracket around it, or -1. */
|
|
1046
|
+
const enclosing = [];
|
|
1047
|
+
tokens.forEach((t, i) => {
|
|
1048
|
+
enclosing.push(stack[stack.length - 1] ?? -1);
|
|
1049
|
+
if (t.kind !== "punct") return;
|
|
1050
|
+
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
1051
|
+
else if (t.text === ")" || t.text === "]" || t.text === "}") {
|
|
1052
|
+
const o = stack.pop();
|
|
1053
|
+
if (o !== void 0) match.set(o, i);
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
const is = (k, text) => tokens[k]?.kind === "punct" && tokens[k]?.text === text;
|
|
1057
|
+
/** The last token of an arrow body starting at `k`: a block, or an expression up to a top-level `,` or close. */
|
|
1058
|
+
const bodyEnd = (k) => {
|
|
1059
|
+
if (is(k, "{")) return match.get(k) ?? tokens.length - 1;
|
|
1060
|
+
let depth = 0;
|
|
1061
|
+
for (let m = k; m < tokens.length; m++) {
|
|
1062
|
+
const u = tokens[m];
|
|
1063
|
+
if (u.kind !== "punct") continue;
|
|
1064
|
+
if (u.text === "(" || u.text === "[" || u.text === "{") depth++;
|
|
1065
|
+
else if (u.text === ")" || u.text === "]" || u.text === "}") {
|
|
1066
|
+
if (depth === 0) return m - 1;
|
|
1067
|
+
depth--;
|
|
1068
|
+
} else if (u.text === "," && depth === 0) return m - 1;
|
|
1069
|
+
}
|
|
1070
|
+
return tokens.length - 1;
|
|
1071
|
+
};
|
|
1072
|
+
/**
|
|
1073
|
+
* The index just after a type starting at `k`: names, dots, brackets,
|
|
1074
|
+
* unions and intersections, and type arguments, but a `<` only when its
|
|
1075
|
+
* `>` closes before anything that cannot be in a type (else it compares).
|
|
1076
|
+
*/
|
|
1077
|
+
const typeEnd = (k) => {
|
|
1078
|
+
const typeish = (u, angles) => u !== void 0 && (u.kind === "ident" || u.kind === "string" || u.kind === "number" || u.kind === "punct" && [
|
|
1079
|
+
".",
|
|
1080
|
+
"[",
|
|
1081
|
+
"]",
|
|
1082
|
+
"|",
|
|
1083
|
+
"&"
|
|
1084
|
+
].includes(u.text) || angles > 0 && u.kind === "punct" && [
|
|
1085
|
+
",",
|
|
1086
|
+
"(",
|
|
1087
|
+
")",
|
|
1088
|
+
"=>",
|
|
1089
|
+
":",
|
|
1090
|
+
"{",
|
|
1091
|
+
"}",
|
|
1092
|
+
";"
|
|
1093
|
+
].includes(u.text));
|
|
1094
|
+
let m = k;
|
|
1095
|
+
while (m < tokens.length) {
|
|
1096
|
+
const u = tokens[m];
|
|
1097
|
+
if (u.kind === "punct" && u.text === "<") {
|
|
1098
|
+
let angles = 0;
|
|
1099
|
+
let n = m;
|
|
1100
|
+
for (; n < tokens.length; n++) {
|
|
1101
|
+
const w = tokens[n];
|
|
1102
|
+
if (w.kind === "punct" && w.text === "<") angles++;
|
|
1103
|
+
else if (w.kind === "punct" && /^>+$/.test(w.text)) {
|
|
1104
|
+
angles -= w.text.length;
|
|
1105
|
+
if (angles <= 0) break;
|
|
1106
|
+
} else if (!typeish(w, angles)) break;
|
|
1107
|
+
}
|
|
1108
|
+
if (angles > 0 || n >= tokens.length || !/^>+$/.test(tokens[n].text)) return m;
|
|
1109
|
+
m = n + 1;
|
|
1110
|
+
continue;
|
|
1111
|
+
}
|
|
1112
|
+
if (!typeish(u, 0)) return m;
|
|
1113
|
+
m++;
|
|
1114
|
+
}
|
|
1115
|
+
return m;
|
|
1116
|
+
};
|
|
1117
|
+
/** After the `)` of a parameter list at `close`: an optional `: ReturnType`, then the index of what follows. */
|
|
1118
|
+
const afterReturnType = (close) => {
|
|
1119
|
+
if (!is(close + 1, ":")) return close + 1;
|
|
1120
|
+
const end = typeEnd(close + 2);
|
|
1121
|
+
for (let m = close + 2; m < end; m++) skip.add(m);
|
|
1122
|
+
return end;
|
|
1123
|
+
};
|
|
1124
|
+
/**
|
|
1125
|
+
* The names a parameter list from `open` to `close` binds, skipping them and
|
|
1126
|
+
* their type annotations. A `:` directly in the list starts a type; inside a
|
|
1127
|
+
* destructuring pattern it renames (`{ a: b }` binds b).
|
|
1128
|
+
*/
|
|
1129
|
+
const parameters = (open, close) => {
|
|
1130
|
+
const names = /* @__PURE__ */ new Set();
|
|
1131
|
+
const nesting = [];
|
|
1132
|
+
let inType = false;
|
|
1133
|
+
let inDefault = false;
|
|
1134
|
+
let defaultDepth = 0;
|
|
1135
|
+
for (let k = open + 1; k < close; k++) {
|
|
1136
|
+
const u = tokens[k];
|
|
1137
|
+
if (inDefault) {
|
|
1138
|
+
if (u.kind !== "punct") continue;
|
|
1139
|
+
if (u.text === "(" || u.text === "[" || u.text === "{") {
|
|
1140
|
+
defaultDepth++;
|
|
1141
|
+
continue;
|
|
1142
|
+
}
|
|
1143
|
+
if ((u.text === ")" || u.text === "]" || u.text === "}") && defaultDepth > 0) {
|
|
1144
|
+
defaultDepth--;
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
if (defaultDepth > 0 || !(u.text === "," || u.text === ")" || u.text === "]" || u.text === "}")) continue;
|
|
1148
|
+
inDefault = false;
|
|
1149
|
+
}
|
|
1150
|
+
if (u.kind === "punct") {
|
|
1151
|
+
if (!inType && u.text === "=") {
|
|
1152
|
+
inDefault = true;
|
|
1153
|
+
defaultDepth = 0;
|
|
1154
|
+
continue;
|
|
1155
|
+
}
|
|
1156
|
+
if (inType) {
|
|
1157
|
+
if (u.text === "(" || u.text === "[" || u.text === "{" || u.text === "<") nesting.push(u.text);
|
|
1158
|
+
else if (nesting.length > 0 && [
|
|
1159
|
+
")",
|
|
1160
|
+
"]",
|
|
1161
|
+
"}"
|
|
1162
|
+
].includes(u.text)) nesting.pop();
|
|
1163
|
+
else if (nesting.length > 0 && /^>+$/.test(u.text)) nesting.splice(-u.text.length);
|
|
1164
|
+
else if (nesting.length === 0 && u.text === ",") inType = false;
|
|
1165
|
+
else if (nesting.length === 0 && u.text === "=") {
|
|
1166
|
+
inType = false;
|
|
1167
|
+
inDefault = true;
|
|
1168
|
+
defaultDepth = 0;
|
|
1169
|
+
}
|
|
1170
|
+
continue;
|
|
1171
|
+
}
|
|
1172
|
+
if (u.text === "{" || u.text === "[") nesting.push(u.text);
|
|
1173
|
+
else if (u.text === "}" || u.text === "]") nesting.pop();
|
|
1174
|
+
else if (u.text === ":" && nesting.length === 0) inType = true;
|
|
1175
|
+
continue;
|
|
1176
|
+
}
|
|
1177
|
+
if (inType) {
|
|
1178
|
+
skip.add(k);
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
if (u.kind !== "ident") continue;
|
|
1182
|
+
const prev = tokens[k - 1];
|
|
1183
|
+
const next = tokens[k + 1];
|
|
1184
|
+
if (nesting.length > 0 && next?.kind === "punct" && next.text === ":") {
|
|
1185
|
+
skip.add(k);
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (prev?.kind === "punct" && [
|
|
1189
|
+
"(",
|
|
1190
|
+
",",
|
|
1191
|
+
"...",
|
|
1192
|
+
"{",
|
|
1193
|
+
"[",
|
|
1194
|
+
":"
|
|
1195
|
+
].includes(prev.text)) {
|
|
1196
|
+
names.add(u.text);
|
|
1197
|
+
skip.add(k);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
return names;
|
|
1201
|
+
};
|
|
1202
|
+
tokens.forEach((t, i) => {
|
|
1203
|
+
if (t.kind === "ident" && is(i + 1, "=>")) {
|
|
1204
|
+
skip.add(i);
|
|
1205
|
+
scopes.push({
|
|
1206
|
+
names: /* @__PURE__ */ new Set([t.text]),
|
|
1207
|
+
from: i,
|
|
1208
|
+
to: bodyEnd(i + 2)
|
|
1209
|
+
});
|
|
1210
|
+
}
|
|
1211
|
+
const close = is(i, "(") ? match.get(i) : void 0;
|
|
1212
|
+
const arrow = close === void 0 ? -1 : afterReturnType(close);
|
|
1213
|
+
if (close !== void 0 && is(arrow, "=>")) scopes.push({
|
|
1214
|
+
names: parameters(i, close),
|
|
1215
|
+
from: i,
|
|
1216
|
+
to: bodyEnd(arrow + 1)
|
|
1217
|
+
});
|
|
1218
|
+
if (t.kind === "ident" && is(i + 1, "(") && (is(i - 1, "{") || is(i - 1, ","))) {
|
|
1219
|
+
const end = match.get(i + 1);
|
|
1220
|
+
const body = end === void 0 ? -1 : afterReturnType(end);
|
|
1221
|
+
if (end !== void 0 && is(body, "{")) {
|
|
1222
|
+
skip.add(i);
|
|
1223
|
+
scopes.push({
|
|
1224
|
+
names: parameters(i + 1, end),
|
|
1225
|
+
from: i + 1,
|
|
1226
|
+
to: match.get(body) ?? tokens.length - 1
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
if (t.kind === "ident" && t.text === "for" && is(i + 1, "(")) {
|
|
1231
|
+
const end = match.get(i + 1);
|
|
1232
|
+
const decl = tokens[i + 2];
|
|
1233
|
+
if (end !== void 0 && decl?.kind === "ident" && [
|
|
1234
|
+
"const",
|
|
1235
|
+
"let",
|
|
1236
|
+
"var"
|
|
1237
|
+
].includes(decl.text)) {
|
|
1238
|
+
const names = /* @__PURE__ */ new Set();
|
|
1239
|
+
for (let k = i + 3; k < end; k++) {
|
|
1240
|
+
const u = tokens[k];
|
|
1241
|
+
if (u.kind === "ident" && (u.text === "of" || u.text === "in")) {
|
|
1242
|
+
skip.add(k);
|
|
1243
|
+
break;
|
|
1244
|
+
}
|
|
1245
|
+
if (u.kind === "ident" && !is(k + 1, ":")) {
|
|
1246
|
+
names.add(u.text);
|
|
1247
|
+
skip.add(k);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
const body = end + 1;
|
|
1251
|
+
const to = is(body, "{") ? match.get(body) ?? tokens.length - 1 : bodyEnd(body);
|
|
1252
|
+
scopes.push({
|
|
1253
|
+
names,
|
|
1254
|
+
from: i,
|
|
1255
|
+
to
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
if (t.kind === "ident" && t.text === "catch" && is(i + 1, "(")) {
|
|
1260
|
+
const end = match.get(i + 1);
|
|
1261
|
+
if (end !== void 0 && is(end + 1, "{")) scopes.push({
|
|
1262
|
+
names: parameters(i + 1, end),
|
|
1263
|
+
from: i + 1,
|
|
1264
|
+
to: match.get(end + 1) ?? tokens.length - 1
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
if (t.kind === "ident" && t.text === "function") {
|
|
1268
|
+
const named = tokens[i + 1]?.kind === "ident";
|
|
1269
|
+
const statement = i === 0 || [
|
|
1270
|
+
"{",
|
|
1271
|
+
"}",
|
|
1272
|
+
";"
|
|
1273
|
+
].some((p) => is(i - 1, p));
|
|
1274
|
+
if (named && statement) {
|
|
1275
|
+
const open = enclosing[i] ?? -1;
|
|
1276
|
+
skip.add(i + 1);
|
|
1277
|
+
scopes.push({
|
|
1278
|
+
names: /* @__PURE__ */ new Set([tokens[i + 1].text]),
|
|
1279
|
+
from: open,
|
|
1280
|
+
to: open >= 0 ? match.get(open) ?? tokens.length - 1 : tokens.length - 1
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
const open = named ? i + 2 : i + 1;
|
|
1284
|
+
const end = is(open, "(") ? match.get(open) : void 0;
|
|
1285
|
+
const body = end === void 0 ? -1 : afterReturnType(end);
|
|
1286
|
+
if (end !== void 0 && is(body, "{")) {
|
|
1287
|
+
const names = parameters(open, end);
|
|
1288
|
+
if (named) {
|
|
1289
|
+
names.add(tokens[i + 1].text);
|
|
1290
|
+
skip.add(i + 1);
|
|
1291
|
+
}
|
|
1292
|
+
scopes.push({
|
|
1293
|
+
names,
|
|
1294
|
+
from: i,
|
|
1295
|
+
to: match.get(body) ?? tokens.length - 1
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
if (t.kind === "ident" && is(i + 1, ":") && (is(i - 1, "{") || is(i - 1, ","))) skip.add(i);
|
|
1300
|
+
if (t.kind === "ident" && (t.text === "as" || t.text === "satisfies")) {
|
|
1301
|
+
skip.add(i);
|
|
1302
|
+
for (let k = i + 1; k < typeEnd(i + 1); k++) skip.add(k);
|
|
1303
|
+
}
|
|
1304
|
+
if (t.kind === "ident" && is(i + 1, "<")) {
|
|
1305
|
+
const end = typeEnd(i + 1);
|
|
1306
|
+
if (end > i + 1 && is(end, "(")) for (let m = i + 2; m < end; m++) skip.add(m);
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
return {
|
|
1310
|
+
skip,
|
|
1311
|
+
scopes
|
|
1312
|
+
};
|
|
1313
|
+
}
|
|
1314
|
+
/**
|
|
1315
|
+
* Identifiers the islands refer to from outside: not after a `.`, not a
|
|
1316
|
+
* binder, not an object key, an arrow function's parameter or a type. What
|
|
1317
|
+
* is left may still over-approximate (a `function` expression's parameter),
|
|
1318
|
+
* which only costs a binding the inner name shadows.
|
|
1319
|
+
*/
|
|
1320
|
+
function freeNames(p) {
|
|
1321
|
+
const bound = new Set(p.binders.map((b) => b.name));
|
|
1322
|
+
const names = /* @__PURE__ */ new Set();
|
|
1323
|
+
const scan = (text, outer) => {
|
|
1324
|
+
const tokens = tokenize(text);
|
|
1325
|
+
const { skip, scopes } = notReferences(tokens);
|
|
1326
|
+
const local = (n, i) => scopes.some((sc) => sc.names.has(n) && sc.from <= i && i <= sc.to);
|
|
1327
|
+
tokens.forEach((t, i) => {
|
|
1328
|
+
if (t.kind === "template") {
|
|
1329
|
+
const inScope = new Set(outer);
|
|
1330
|
+
for (const sc of scopes) if (sc.from <= i && i <= sc.to) for (const n of sc.names) inScope.add(n);
|
|
1331
|
+
for (const sub of templateSubstitutions(t.text)) scan(sub, inScope);
|
|
1332
|
+
}
|
|
1333
|
+
const prev = tokens[i - 1];
|
|
1334
|
+
if (t.kind !== "ident" || skip.has(i) || bound.has(t.text) || reserved.has(t.text) || outer.has(t.text) || local(t.text, i)) return;
|
|
1335
|
+
if (prev?.kind === "punct" && (prev.text === "." || prev.text === "?.")) return;
|
|
1336
|
+
names.add(t.text);
|
|
1337
|
+
});
|
|
1338
|
+
};
|
|
1339
|
+
for (const text of [...p.requires, p.claim].flatMap(islands)) scan(text, /* @__PURE__ */ new Set());
|
|
1340
|
+
return [...names];
|
|
1341
|
+
}
|
|
1342
|
+
function expression(f, h) {
|
|
1343
|
+
switch (f.kind) {
|
|
1344
|
+
case "atom": return `${h.bool}((${f.text}))`;
|
|
1345
|
+
case "equation": return `${f.negated ? "!" : ""}${h.same}((${f.left}), (${f.right}))`;
|
|
1346
|
+
case "not": return `!${expression(f.operand, h)}`;
|
|
1347
|
+
case "and":
|
|
1348
|
+
case "or": return `(${f.operands.map((o) => expression(o, h)).join(f.kind === "and" ? " && " : " || ")})`;
|
|
1349
|
+
case "implies": return `(!(${f.antecedents.map((a) => expression(a, h)).join(" && ")}) || ${expression(f.consequent, h)})`;
|
|
1350
|
+
case "iff": return `(${expression(f.left, h)} === ${expression(f.right, h)})`;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* The predicate's source: an expression evaluating to the arrow function.
|
|
1355
|
+
* It takes the helpers as parameters under names fresh for this property,
|
|
1356
|
+
* so no binder or identifier in it can shadow them, and binds each free
|
|
1357
|
+
* name from the module's namespace when it has it, else from the globals,
|
|
1358
|
+
* which is the scope Lemma gives a property (re-exports included).
|
|
1359
|
+
*/
|
|
1360
|
+
function predicateSource(p, helpers) {
|
|
1361
|
+
const free = freeNames(p).filter((n) => ![
|
|
1362
|
+
"globalThis",
|
|
1363
|
+
"undefined",
|
|
1364
|
+
"NaN",
|
|
1365
|
+
"Infinity"
|
|
1366
|
+
].includes(n));
|
|
1367
|
+
const taken = /* @__PURE__ */ new Set([
|
|
1368
|
+
...p.binders.map((b) => b.name),
|
|
1369
|
+
...free,
|
|
1370
|
+
"globalThis"
|
|
1371
|
+
]);
|
|
1372
|
+
const fresh = (base) => {
|
|
1373
|
+
let name = base;
|
|
1374
|
+
while (taken.has(name)) name = `${name}$`;
|
|
1375
|
+
taken.add(name);
|
|
1376
|
+
return name;
|
|
1377
|
+
};
|
|
1378
|
+
const local = {
|
|
1379
|
+
bool: fresh("__bool"),
|
|
1380
|
+
same: fresh("__same"),
|
|
1381
|
+
scope: fresh("__scope"),
|
|
1382
|
+
unresolved: fresh("__unresolved"),
|
|
1383
|
+
missing: fresh("__missing")
|
|
1384
|
+
};
|
|
1385
|
+
const params = p.binders.map((b) => `${b.name}: ${tsType[b.domain]}`).join(", ");
|
|
1386
|
+
return [
|
|
1387
|
+
`((${local.bool}: (v: unknown) => boolean, ${local.same}: (a: unknown, b: unknown) => boolean, ${local.scope}: any, ${local.unresolved}: (names: string[]) => never) => {`,
|
|
1388
|
+
` const ${local.missing}: string[] = [];`,
|
|
1389
|
+
...free.map((n) => {
|
|
1390
|
+
const k = JSON.stringify(n);
|
|
1391
|
+
return ` const ${n} = ${k} in ${local.scope} ? ${local.scope}[${k}] : ${k} in globalThis ? (globalThis as any)[${k}] : (${local.missing}.push(${k}), undefined);`;
|
|
1392
|
+
}),
|
|
1393
|
+
` return (${params}): "held" | "discarded" | "refuted" => {`,
|
|
1394
|
+
` if (${local.missing}.length > 0) ${local.unresolved}(${local.missing});`,
|
|
1395
|
+
...p.requires.map((r) => ` if (!${expression(r, local)}) return "discarded";`),
|
|
1396
|
+
` return ${expression(p.claim, local)} ? "held" : "refuted";`,
|
|
1397
|
+
" };",
|
|
1398
|
+
`})(${helpers.bool}, ${helpers.same}, ${helpers.scope}, ${helpers.unresolved})`
|
|
1399
|
+
].join("\n");
|
|
1400
|
+
}
|
|
1401
|
+
// packages/prover/src/checked.ts
|
|
1402
|
+
/** Thrown by `lemmaBool` when an atom is not a genuine boolean: the property is malformed, not false. */
|
|
1403
|
+
var AtomNotBoolean = class extends TypeError {};
|
|
1404
|
+
/** Thrown by `lemmaUnresolved`: the property names something neither its module nor the globals define. */
|
|
1405
|
+
var UnresolvedName = class extends ReferenceError {};
|
|
1406
|
+
/** What generated predicates call when free names did not resolve. */
|
|
1407
|
+
function lemmaUnresolved(names) {
|
|
1408
|
+
throw new UnresolvedName(`the property refers to ${names.join(", ")}, which neither its module exports nor the globals define`);
|
|
1409
|
+
}
|
|
1410
|
+
/** The atom check generated predicates call: its argument when it is a boolean. */
|
|
1411
|
+
function lemmaBool(value) {
|
|
1412
|
+
if (typeof value !== "boolean") throw new AtomNotBoolean(`a Lemma atom evaluated to ${render(value)}, not a boolean`);
|
|
1413
|
+
return value;
|
|
1414
|
+
}
|
|
1415
|
+
/** A value as a JavaScript literal, exact for the binder domains. */
|
|
1416
|
+
function render(value) {
|
|
1417
|
+
switch (typeof value) {
|
|
1418
|
+
case "bigint": return `${value}n`;
|
|
1419
|
+
case "number": return Object.is(value, -0) ? "-0" : String(value);
|
|
1420
|
+
case "string": return JSON.stringify(value);
|
|
1421
|
+
case "undefined": return "undefined";
|
|
1422
|
+
case "boolean": return String(value);
|
|
1423
|
+
case "function": return "[function]";
|
|
1424
|
+
}
|
|
1425
|
+
try {
|
|
1426
|
+
const text = JSON.stringify(value, (_, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
1427
|
+
return text === void 0 ? String(value) : text.length > 200 ? `${text.slice(0, 200)}…` : text;
|
|
1428
|
+
} catch {
|
|
1429
|
+
return String(value);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
function bound(value, integral) {
|
|
1433
|
+
if (integral && !value.endsWith("∞")) return BigInt(value.replace(/_/g, "").replace(/n$/, ""));
|
|
1434
|
+
return endpointNumber(value);
|
|
1435
|
+
}
|
|
1436
|
+
/** Whether a value lies in a binder's domain and guard. */
|
|
1437
|
+
function inDomain(b, v) {
|
|
1438
|
+
switch (b.domain) {
|
|
1439
|
+
case "int":
|
|
1440
|
+
if (!(typeof v === "number" && Number.isInteger(v))) return false;
|
|
1441
|
+
break;
|
|
1442
|
+
case "nat":
|
|
1443
|
+
if (!(typeof v === "number" && Number.isInteger(v) && v >= 0)) return false;
|
|
1444
|
+
break;
|
|
1445
|
+
case "number":
|
|
1446
|
+
if (typeof v !== "number") return false;
|
|
1447
|
+
break;
|
|
1448
|
+
case "bigint":
|
|
1449
|
+
if (typeof v !== "bigint") return false;
|
|
1450
|
+
break;
|
|
1451
|
+
case "string":
|
|
1452
|
+
if (typeof v !== "string") return false;
|
|
1453
|
+
break;
|
|
1454
|
+
case "boolean": return typeof v === "boolean";
|
|
1455
|
+
}
|
|
1456
|
+
const g = b.guard;
|
|
1457
|
+
if (!g) return true;
|
|
1458
|
+
if (g.kind === "regex") return new RegExp(`^(?:${g.pattern})$`, g.flags).test(v);
|
|
1459
|
+
const integral = b.domain !== "number";
|
|
1460
|
+
const x = integral ? BigInt(v) : v;
|
|
1461
|
+
const lo = bound(g.lower.value, integral);
|
|
1462
|
+
const hi = bound(g.upper.value, integral);
|
|
1463
|
+
const aboveLower = g.lower.inclusive ? x >= lo : x > lo;
|
|
1464
|
+
const belowUpper = g.upper.inclusive ? x <= hi : x < hi;
|
|
1465
|
+
return aboveLower && belowUpper;
|
|
1466
|
+
}
|
|
1467
|
+
function checkedRung(input) {
|
|
1468
|
+
const { module, fn, property } = input;
|
|
1469
|
+
const base = {
|
|
1470
|
+
module,
|
|
1471
|
+
function: fn.name,
|
|
1472
|
+
property: property.name,
|
|
1473
|
+
text: property.text
|
|
1474
|
+
};
|
|
1475
|
+
const unknown = (cause, reason) => ({
|
|
1476
|
+
...base,
|
|
1477
|
+
kind: "UNKNOWN",
|
|
1478
|
+
cause,
|
|
1479
|
+
reason,
|
|
1480
|
+
assumptions: []
|
|
1481
|
+
});
|
|
1482
|
+
if (input.unobservable) return unknown("unavailable", `calls to ${fn.name} could not be observed: ${input.unobservable}`);
|
|
1483
|
+
const predicate = input.predicate;
|
|
1484
|
+
if (!predicate) return unknown("unavailable", "the property's predicate was not compiled");
|
|
1485
|
+
const binders = property.property.binders;
|
|
1486
|
+
const positions = [];
|
|
1487
|
+
for (const b of binders) {
|
|
1488
|
+
const at = fn.params.indexOf(b.name);
|
|
1489
|
+
if (at < 0) return unknown("unsupported", `binder ${b.name} is not a parameter of ${fn.name}, so the inputs the simulator passed do not determine it`);
|
|
1490
|
+
positions.push(at);
|
|
1491
|
+
}
|
|
1492
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1493
|
+
let held = 0;
|
|
1494
|
+
let discarded = 0;
|
|
1495
|
+
for (const call of input.calls) {
|
|
1496
|
+
const values = positions.map((p) => call.args[p]);
|
|
1497
|
+
const key = values.map(render).join(", ");
|
|
1498
|
+
if (seen.has(key)) continue;
|
|
1499
|
+
seen.add(key);
|
|
1500
|
+
if (!binders.every((b, i) => inDomain(b, values[i]))) {
|
|
1501
|
+
discarded++;
|
|
1502
|
+
continue;
|
|
1503
|
+
}
|
|
1504
|
+
const counterexample = (error) => ({
|
|
1505
|
+
binders: binders.map((b, i) => ({
|
|
1506
|
+
name: b.name,
|
|
1507
|
+
value: render(values[i])
|
|
1508
|
+
})),
|
|
1509
|
+
call: `${fn.name}(${call.args.map(render).join(", ")})`,
|
|
1510
|
+
...error === void 0 ? {} : { error }
|
|
1511
|
+
});
|
|
1512
|
+
let outcome;
|
|
1513
|
+
try {
|
|
1514
|
+
outcome = predicate(...values);
|
|
1515
|
+
} catch (err) {
|
|
1516
|
+
if (err instanceof AtomNotBoolean || err instanceof ReferenceError) return unknown("unsupported", `the property could not be evaluated: ${String(err)}`);
|
|
1517
|
+
return {
|
|
1518
|
+
...base,
|
|
1519
|
+
kind: "REFUTED",
|
|
1520
|
+
rung: "CHECKED",
|
|
1521
|
+
counterexample: counterexample(String(err)),
|
|
1522
|
+
assumptions: []
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
if (outcome === "refuted") return {
|
|
1526
|
+
...base,
|
|
1527
|
+
kind: "REFUTED",
|
|
1528
|
+
rung: "CHECKED",
|
|
1529
|
+
counterexample: counterexample(),
|
|
1530
|
+
assumptions: []
|
|
1531
|
+
};
|
|
1532
|
+
if (outcome === "discarded") discarded++;
|
|
1533
|
+
else held++;
|
|
1534
|
+
}
|
|
1535
|
+
if (held === 0) return unknown("gave_up", input.calls.length === 0 ? `the simulator never called ${fn.name}` : `none of the ${seen.size} distinct input(s) the simulator passed is inside the claim (domains, guards, preconditions)`);
|
|
1536
|
+
return {
|
|
1537
|
+
...base,
|
|
1538
|
+
kind: "CHECKED",
|
|
1539
|
+
inputs: held,
|
|
1540
|
+
discarded,
|
|
1541
|
+
assumptions: [`held on the ${held} distinct input(s) the simulator passed to ${fn.name}; inputs it never passed are not covered`]
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
/** Verdicts for the markers the CHECKED rung cannot decide. */
|
|
1545
|
+
function markerVerdicts(module, fn) {
|
|
1546
|
+
const out = [];
|
|
1547
|
+
const base = {
|
|
1548
|
+
module,
|
|
1549
|
+
function: fn.name,
|
|
1550
|
+
assumptions: []
|
|
1551
|
+
};
|
|
1552
|
+
if (fn.total) out.push({
|
|
1553
|
+
...base,
|
|
1554
|
+
property: "@total",
|
|
1555
|
+
text: "@total",
|
|
1556
|
+
kind: "UNKNOWN",
|
|
1557
|
+
cause: "unsupported",
|
|
1558
|
+
reason: "termination is not observable on the inputs the simulator passed; @total needs a prover"
|
|
1559
|
+
});
|
|
1560
|
+
if (fn.throws) out.push({
|
|
1561
|
+
...base,
|
|
1562
|
+
property: "@throws",
|
|
1563
|
+
text: "@throws",
|
|
1564
|
+
kind: "UNKNOWN",
|
|
1565
|
+
cause: "unsupported",
|
|
1566
|
+
reason: "Lemma has not fixed what @throws claims yet"
|
|
1567
|
+
});
|
|
1568
|
+
return out;
|
|
1569
|
+
}
|
|
1570
|
+
/** Every spec verdict for one module, from what was observed of its calls. */
|
|
1571
|
+
function checkSpecModule(module, specs, predicates, observed) {
|
|
1572
|
+
return specs.functions.flatMap((fn) => {
|
|
1573
|
+
const calls = observed.calls.filter((c) => c.function === fn.name);
|
|
1574
|
+
const unobservable = Object.hasOwn(observed.unobservable, fn.name) ? observed.unobservable[fn.name] : void 0;
|
|
1575
|
+
const own = Object.hasOwn(predicates, fn.name) ? predicates[fn.name] : void 0;
|
|
1576
|
+
return [...fn.ensures.map((property) => checkedRung({
|
|
1577
|
+
module,
|
|
1578
|
+
fn,
|
|
1579
|
+
property,
|
|
1580
|
+
calls,
|
|
1581
|
+
predicate: own && Object.hasOwn(own, property.name) ? own[property.name] : void 0,
|
|
1582
|
+
...unobservable === void 0 ? {} : { unobservable }
|
|
1583
|
+
})), ...markerVerdicts(module, fn)];
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1586
|
+
// packages/core/src/verdict.ts
|
|
1587
|
+
/** One line per verdict, for error messages; full rendering lives in `report`. */
|
|
1588
|
+
function describeVerdict(v) {
|
|
1589
|
+
switch (v.kind) {
|
|
1590
|
+
case "VIOLATED": return `VIOLATED ${v.scenario}: ${v.violation.message} (seed ${v.schedule.seed}, ${v.reached.runs} run(s))`;
|
|
1591
|
+
case "BOUNDED": return `BOUNDED ${v.scenario}: no violation in ${v.reached.runs} run(s), ${v.reached.effectPoints} effect point(s), ${v.reached.faultsInjected} fault(s)`;
|
|
1592
|
+
case "UNSUPPORTED": return `UNSUPPORTED ${v.scenario}: ${v.cause}: ${v.reason}`;
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
// packages/report/src/regression.ts
|
|
1596
|
+
/**
|
|
1597
|
+
* The Vitest regression emitter. Given a violation, produce a test file that
|
|
1598
|
+
* replays its shrunk schedule on the restricted scenario. By default the
|
|
1599
|
+
* test expects the invariants to hold, so it fails while the bug exists and
|
|
1600
|
+
* passes once it is fixed. With `expect: "violates"` it asserts the recorded
|
|
1601
|
+
* violation instead, which is how Eutaxy's own corpus exercises the emitter.
|
|
1602
|
+
*
|
|
1603
|
+
* The emitter knows nothing about any platform: the caller supplies the
|
|
1604
|
+
* import lines and the expression that builds the platform.
|
|
1605
|
+
*/
|
|
1606
|
+
function emitRegressionTest(verdict, options) {
|
|
1607
|
+
const engineModule = options.engineModule ?? "eutaxy";
|
|
1608
|
+
const mode = options.expect ?? "holds";
|
|
1609
|
+
const schedule = canonicalJson(verdict.schedule);
|
|
1610
|
+
const kept = canonicalJson([...verdict.keptEvents]);
|
|
1611
|
+
const keys = canonicalJson([...verdict.violation.keys]);
|
|
1612
|
+
const title = mode === "holds" ? `${verdict.scenario}: ${verdict.violation.invariant} holds on the recorded schedule` : `${verdict.scenario}: ${verdict.violation.invariant} is still violated on the recorded schedule`;
|
|
1613
|
+
const assertion = mode === "holds" ? ` expect(violations.map((v) => \`\${v.invariant}: \${v.message}\`)).toEqual([]);` : ` expect(violations.map((v) => [v.invariant, v.keys])).toContainEqual([${JSON.stringify(verdict.violation.invariant)}, ${keys}]);`;
|
|
1614
|
+
const observe = options.observe ?? [];
|
|
1615
|
+
const replayLines = observe.length === 0 ? [" const log = await replay(spec, platform, schedule);"] : [
|
|
1616
|
+
` const observers = [${observe.map((o) => `observeCore(${o.namespace}, ${JSON.stringify(o.functions)})`).join(", ")}];`,
|
|
1617
|
+
" const log = await replay(spec, platform, schedule).finally(() => {",
|
|
1618
|
+
" for (const o of observers) o.restore();",
|
|
1619
|
+
" });"
|
|
1620
|
+
];
|
|
1621
|
+
return [
|
|
1622
|
+
"// Generated by Eutaxy. Replays a recorded violation; regenerate rather than edit.",
|
|
1623
|
+
`// Scenario ${verdict.scenario}, seed ${String(verdict.seed)}: ${verdict.violation.message}`,
|
|
1624
|
+
`import { checkInvariants, ${observe.length > 0 ? "observeCore, " : ""}replay, restrictSpec } from ${JSON.stringify(engineModule)};`,
|
|
1625
|
+
...options.imports,
|
|
1626
|
+
"import { expect, test } from \"vitest\";",
|
|
1627
|
+
"",
|
|
1628
|
+
`const schedule = ${schedule} as const;`,
|
|
1629
|
+
`const keptEvents = ${kept} as const;`,
|
|
1630
|
+
"",
|
|
1631
|
+
`test(${JSON.stringify(title)}, async () => {`,
|
|
1632
|
+
` const platform = ${options.platform};`,
|
|
1633
|
+
` const spec = restrictSpec(platform, ${options.scenario}, keptEvents);`,
|
|
1634
|
+
...replayLines,
|
|
1635
|
+
" const violations = checkInvariants(log, spec.invariants);",
|
|
1636
|
+
assertion,
|
|
1637
|
+
"});",
|
|
1638
|
+
""
|
|
1639
|
+
].join("\n");
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1642
|
+
* The JSON report: one object, canonical, with a schema version at the top.
|
|
1643
|
+
* Spec verdicts, when there are any, sit beside the scenario's, never in it.
|
|
1644
|
+
*/
|
|
1645
|
+
function renderJson(verdict, specs = []) {
|
|
1646
|
+
return canonicalJson({
|
|
1647
|
+
schemaVersion: 1,
|
|
1648
|
+
generator: "eutaxy",
|
|
1649
|
+
verdict,
|
|
1650
|
+
...specs.length > 0 ? { specs } : {}
|
|
1651
|
+
});
|
|
1652
|
+
}
|
|
1653
|
+
/** Spec verdicts as text: one line each, a counterexample under a refutation. */
|
|
1654
|
+
function renderSpecs(specs) {
|
|
1655
|
+
if (specs.length === 0) return "";
|
|
1656
|
+
const out = ["Specs (CHECKED means held on the inputs the simulator passed; it is not a proof)"];
|
|
1657
|
+
for (const v of specs) {
|
|
1658
|
+
const what = `${v.function} ${v.property}`;
|
|
1659
|
+
switch (v.kind) {
|
|
1660
|
+
case "CHECKED":
|
|
1661
|
+
out.push(` CHECKED ${what}: held on ${v.inputs} input(s)${v.discarded > 0 ? `, ${v.discarded} outside the claim` : ""}`);
|
|
1662
|
+
break;
|
|
1663
|
+
case "REFUTED": {
|
|
1664
|
+
const c = v.counterexample;
|
|
1665
|
+
out.push(` REFUTED ${what}: false at ${c.call}${c.error ? `, which threw ${c.error}` : ""}`);
|
|
1666
|
+
out.push(` ${c.binders.map((b) => `${b.name} = ${b.value}`).join(", ")}`);
|
|
1667
|
+
out.push(` ${v.text.replace(/\s+/g, " ")}`);
|
|
1668
|
+
break;
|
|
1669
|
+
}
|
|
1670
|
+
case "UNKNOWN": out.push(` UNKNOWN ${what}: ${v.cause}: ${v.reason}`);
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
return out.join("\n");
|
|
1674
|
+
}
|
|
1675
|
+
function clip(s, max) {
|
|
1676
|
+
return s.length <= max ? s : `${s.slice(0, max - 1)}…`;
|
|
1677
|
+
}
|
|
1678
|
+
function pad(s, width) {
|
|
1679
|
+
return s.length >= width ? s : s + " ".repeat(width - s.length);
|
|
1680
|
+
}
|
|
1681
|
+
/** The trace as a fixed-width table; evidence rows are marked with `*`. */
|
|
1682
|
+
function renderTrace(log, evidence = []) {
|
|
1683
|
+
const rows = log.entries.map((e) => [
|
|
1684
|
+
evidence.includes(e.seq) ? "*" : " ",
|
|
1685
|
+
String(e.seq),
|
|
1686
|
+
e.invocation,
|
|
1687
|
+
e.kind,
|
|
1688
|
+
e.target,
|
|
1689
|
+
e.op,
|
|
1690
|
+
clip(canonicalJson(e.payload), 72)
|
|
1691
|
+
]);
|
|
1692
|
+
const header = [
|
|
1693
|
+
" ",
|
|
1694
|
+
"seq",
|
|
1695
|
+
"invocation",
|
|
1696
|
+
"kind",
|
|
1697
|
+
"target",
|
|
1698
|
+
"op",
|
|
1699
|
+
"payload"
|
|
1700
|
+
];
|
|
1701
|
+
const widths = header.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
1702
|
+
const line = (cells) => cells.map((c, i) => i === cells.length - 1 ? c : pad(c, widths[i])).join(" ").trimEnd();
|
|
1703
|
+
return [line(header), ...rows.map(line)].join("\n");
|
|
1704
|
+
}
|
|
1705
|
+
/** A readable report: verdict, what it rests on, and for a violation the schedule and trace. */
|
|
1706
|
+
function renderText(verdict) {
|
|
1707
|
+
const out = [];
|
|
1708
|
+
const r = verdict.reached;
|
|
1709
|
+
out.push(`${verdict.kind} ${verdict.scenario} (seed ${String(verdict.seed)})`);
|
|
1710
|
+
switch (verdict.kind) {
|
|
1711
|
+
case "VIOLATED":
|
|
1712
|
+
out.push("");
|
|
1713
|
+
out.push(`Invariant ${verdict.violation.invariant} is violated: ${verdict.violation.message}`);
|
|
1714
|
+
out.push(`Keys: ${verdict.violation.keys.join(", ")}`);
|
|
1715
|
+
out.push("");
|
|
1716
|
+
out.push(`Schedule (run seed ${verdict.schedule.seed}), events kept: ${verdict.keptEvents.join(", ")}`);
|
|
1717
|
+
if (verdict.schedule.faults.length === 0) out.push(" no injected faults");
|
|
1718
|
+
for (const f of verdict.schedule.faults) out.push(` ${f.fault} at ${f.invocation} effect #${f.ordinal} (${f.target} ${f.op})`);
|
|
1719
|
+
out.push(`Shrunk: ${verdict.shrunk.eventsRemoved} event(s) and ${verdict.shrunk.faultsRemoved} fault(s) removed in ${verdict.shrunk.runs} run(s)`);
|
|
1720
|
+
out.push("");
|
|
1721
|
+
out.push("Trace:");
|
|
1722
|
+
out.push(renderTrace(verdict.trace, verdict.violation.evidence));
|
|
1723
|
+
break;
|
|
1724
|
+
case "BOUNDED": {
|
|
1725
|
+
const b = verdict.bounds;
|
|
1726
|
+
out.push("");
|
|
1727
|
+
out.push("No violation found within the stated bounds. This is not a proof.");
|
|
1728
|
+
out.push(`Bounds: faults per run ≤ ${b.maxFaultsPerRun}, redeliveries ≤ ${b.maxRedeliveries}, concurrent invocations ≤ ${b.maxConcurrentInvocations}, budget ${b.budgetMs} ms`);
|
|
1729
|
+
break;
|
|
1730
|
+
}
|
|
1731
|
+
case "UNSUPPORTED":
|
|
1732
|
+
out.push("");
|
|
1733
|
+
out.push(`Eutaxy could not decide this scenario (${verdict.cause}): ${verdict.reason}`);
|
|
1734
|
+
}
|
|
1735
|
+
out.push("");
|
|
1736
|
+
out.push(`Explored: ${r.runs} run(s), ${r.effectPoints} effect point(s), ${r.faultsInjected} fault(s) injected, ${r.elapsedMs} ms`);
|
|
1737
|
+
const e = r.exhaustive;
|
|
1738
|
+
if (e) {
|
|
1739
|
+
const stopped = {
|
|
1740
|
+
VIOLATED: "stopped at the first violation",
|
|
1741
|
+
BOUNDED: "stopped at the budget before every schedule ran",
|
|
1742
|
+
UNSUPPORTED: "stopped where the scenario could not be decided"
|
|
1743
|
+
}[verdict.kind];
|
|
1744
|
+
out.push(`Exhaustive: ${e.complete ? "every schedule within the bounds ran" : stopped} (${e.schedules} schedules${e.reduction ? ", partial-order reduction" : ""})`);
|
|
1745
|
+
}
|
|
1746
|
+
out.push("Assumptions:");
|
|
1747
|
+
for (const a of verdict.assumptions) out.push(` - ${a}`);
|
|
1748
|
+
return out.join("\n");
|
|
1749
|
+
}
|
|
1750
|
+
export { emitRegressionTest as a, checkedRung as c, predicateSource as d, extractSpecs as f, renderTrace as i, lemmaBool as l, parseProperty as m, renderSpecs as n, describeVerdict as o, LemmaSyntaxError as p, renderText as r, checkSpecModule as s, renderJson as t, lemmaUnresolved as u };
|
|
1751
|
+
|
|
1752
|
+
//# sourceMappingURL=src-Dxl9ZlKn.mjs.map
|