eutaxy 0.6.0 → 0.7.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 +41 -14
- package/dist/aws.d.mts +2 -2
- package/dist/bin.mjs +2 -2
- package/dist/cli-CL28yx3H.mjs +2 -0
- package/dist/cli.mjs +2 -2
- package/dist/cloudflare.d.mts +9 -9
- package/dist/cloudflare.d.mts.map +1 -1
- package/dist/cloudflare.mjs +1 -1
- package/dist/combinators-B7tV5fM_.d.mts +36 -0
- package/dist/combinators-B7tV5fM_.d.mts.map +1 -0
- package/dist/combinators-N4LmG4p4.mjs +26 -0
- package/dist/combinators-N4LmG4p4.mjs.map +1 -0
- package/dist/{delivery-invariants-BraYqcYN.d.mts → delivery-invariants-DfPkJL0Q.d.mts} +2 -2
- package/dist/{delivery-invariants-BraYqcYN.d.mts.map → delivery-invariants-DfPkJL0Q.d.mts.map} +1 -1
- package/dist/explore-DhfREiX9.d.mts +226 -0
- package/dist/explore-DhfREiX9.d.mts.map +1 -0
- package/dist/index.d.mts +139 -20
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -61
- package/dist/index.mjs.map +1 -1
- package/dist/{platform-DfMwknw1.d.mts → platform-Bpnav0kn.d.mts} +6 -4
- package/dist/{platform-DfMwknw1.d.mts.map → platform-Bpnav0kn.d.mts.map} +1 -1
- package/dist/spec.d.mts +2 -0
- package/dist/spec.mjs +1 -0
- package/dist/src-BO7I3f-1.mjs.map +1 -1
- package/dist/{src-C43iUZgr.mjs → src-CMoBPQuE.mjs} +4564 -1372
- package/dist/src-CMoBPQuE.mjs.map +1 -0
- package/dist/src-DzpZxvQi.mjs +1422 -0
- package/dist/src-DzpZxvQi.mjs.map +1 -0
- package/package.json +12 -7
- package/dist/cli-CsiVEsS-.mjs +0 -2
- package/dist/explore-CDpJA9zX.d.mts +0 -342
- package/dist/explore-CDpJA9zX.d.mts.map +0 -1
- package/dist/src-C43iUZgr.mjs.map +0 -1
- package/dist/src-Cs4Mg55e.mjs +0 -2672
- package/dist/src-Cs4Mg55e.mjs.map +0 -1
package/dist/src-Cs4Mg55e.mjs
DELETED
|
@@ -1,2672 +0,0 @@
|
|
|
1
|
-
import { C as nextU32, E as canonicalJson, S as nextInt, w as seedPrng, x as nextFloat } from "./src-BO7I3f-1.mjs";
|
|
2
|
-
// packages/core/src/verdict.ts
|
|
3
|
-
/** One line per verdict, for error messages; full rendering lives in `report`. */
|
|
4
|
-
function describeVerdict(v) {
|
|
5
|
-
switch (v.kind) {
|
|
6
|
-
case "VIOLATED": return `VIOLATED ${v.scenario}: ${v.violation.message} (seed ${v.schedule.seed}, ${v.reached.runs} run(s))`;
|
|
7
|
-
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)`;
|
|
8
|
-
case "UNSUPPORTED": return `UNSUPPORTED ${v.scenario}: ${v.cause}: ${v.reason}`;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
// packages/spec/src/ir.ts
|
|
12
|
-
const domains = [
|
|
13
|
-
"int",
|
|
14
|
-
"nat",
|
|
15
|
-
"number",
|
|
16
|
-
"boolean",
|
|
17
|
-
"string",
|
|
18
|
-
"bigint"
|
|
19
|
-
];
|
|
20
|
-
// packages/spec/src/tokens.ts
|
|
21
|
-
const glyphs = {
|
|
22
|
-
"∧": "and",
|
|
23
|
-
"∨": "or",
|
|
24
|
-
"¬": "not",
|
|
25
|
-
"→": "implies",
|
|
26
|
-
"↔": "iff",
|
|
27
|
-
"≡": "equiv",
|
|
28
|
-
"≢": "nequiv",
|
|
29
|
-
"≠": "neq",
|
|
30
|
-
"∀": "forall",
|
|
31
|
-
"∃": "exists",
|
|
32
|
-
"∈": "member",
|
|
33
|
-
"∞": "infinity"
|
|
34
|
-
};
|
|
35
|
-
/** ASCII spellings of Lemma connectives, longest first. */
|
|
36
|
-
const asciiOps = [
|
|
37
|
-
["<->", "iff"],
|
|
38
|
-
["==>", "implies"],
|
|
39
|
-
["->", "implies"],
|
|
40
|
-
["\\/", "or"]
|
|
41
|
-
];
|
|
42
|
-
const punctuators = [
|
|
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
|
-
"/=",
|
|
69
|
-
"%=",
|
|
70
|
-
"&=",
|
|
71
|
-
"|=",
|
|
72
|
-
"^=",
|
|
73
|
-
"**",
|
|
74
|
-
"<<",
|
|
75
|
-
">>"
|
|
76
|
-
];
|
|
77
|
-
/** Keywords after which a `/` starts a regular expression rather than a division. */
|
|
78
|
-
const regexAfter = /* @__PURE__ */ new Set([
|
|
79
|
-
"in",
|
|
80
|
-
"of",
|
|
81
|
-
"typeof",
|
|
82
|
-
"instanceof",
|
|
83
|
-
"return",
|
|
84
|
-
"new",
|
|
85
|
-
"delete",
|
|
86
|
-
"void",
|
|
87
|
-
"case",
|
|
88
|
-
"do",
|
|
89
|
-
"else",
|
|
90
|
-
"yield",
|
|
91
|
-
"await"
|
|
92
|
-
]);
|
|
93
|
-
const identStart = /[\p{ID_Start}$_]/u;
|
|
94
|
-
const identPart = /[\p{ID_Continue}$]/u;
|
|
95
|
-
const numberAt = /^(?:0[xXoObB][0-9a-fA-F_]+n?|(?:\d[\d_]*(?:\.[\d_]*)?|\.\d[\d_]*)(?:[eE][+-]?\d[\d_]*)?n?)/;
|
|
96
|
-
var TokenizeError = class extends Error {};
|
|
97
|
-
/** Keywords whose parenthesized condition is followed by a statement, not an operator. */
|
|
98
|
-
const controlKeywords = /* @__PURE__ */ new Set([
|
|
99
|
-
"if",
|
|
100
|
-
"while",
|
|
101
|
-
"for",
|
|
102
|
-
"with"
|
|
103
|
-
]);
|
|
104
|
-
/**
|
|
105
|
-
* Whether the code tokens up to `k` end an operand, so a `/` after them
|
|
106
|
-
* divides. `conditions` holds the `)` that close a control statement's
|
|
107
|
-
* condition (`if (x) /re/.test(s)`), after which a statement starts.
|
|
108
|
-
*/
|
|
109
|
-
function endsOperand$1(tokens, k, conditions, blocks) {
|
|
110
|
-
while (k >= 0 && tokens[k]?.kind === "comment") k--;
|
|
111
|
-
const t = tokens[k];
|
|
112
|
-
if (!t) return false;
|
|
113
|
-
switch (t.kind) {
|
|
114
|
-
case "ident": {
|
|
115
|
-
const before = tokens.slice(0, k).findLast((u) => u.kind !== "comment");
|
|
116
|
-
if (before?.kind === "punct" && (before.text === "." || before.text === "?.")) return true;
|
|
117
|
-
return !regexAfter.has(t.text);
|
|
118
|
-
}
|
|
119
|
-
case "punct":
|
|
120
|
-
if (t.text === "!" || t.text === "++" || t.text === "--") return endsOperand$1(tokens, k - 1, conditions, blocks);
|
|
121
|
-
if (t.text === ")") return !conditions.has(k);
|
|
122
|
-
if (t.text === "}") return !blocks.has(k);
|
|
123
|
-
return t.text === "]";
|
|
124
|
-
case "op": return false;
|
|
125
|
-
default: return true;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/** The end of a quoted string starting at `i`. */
|
|
129
|
-
function endOfString(text, i) {
|
|
130
|
-
const quote = text[i];
|
|
131
|
-
for (let j = i + 1; j < text.length; j++) {
|
|
132
|
-
const c = text[j];
|
|
133
|
-
if (c === "\\") j++;
|
|
134
|
-
else if (c === quote) return j + 1;
|
|
135
|
-
else if (c === "\n") break;
|
|
136
|
-
}
|
|
137
|
-
throw new TokenizeError(`unterminated string at offset ${i}`);
|
|
138
|
-
}
|
|
139
|
-
/** The end of the template literal starting at `i`, substitutions included. */
|
|
140
|
-
function endOfTemplate(text, i) {
|
|
141
|
-
for (let j = i + 1; j < text.length; j++) {
|
|
142
|
-
const c = text[j];
|
|
143
|
-
if (c === "\\") j++;
|
|
144
|
-
else if (c === "`") return j + 1;
|
|
145
|
-
else if (c === "$" && text[j + 1] === "{") j = endOfSubstitution(text, j + 2) - 1;
|
|
146
|
-
}
|
|
147
|
-
throw new TokenizeError(`unterminated template literal at offset ${i}`);
|
|
148
|
-
}
|
|
149
|
-
/** The offset just past the `}` that closes a substitution whose body starts at `i`, scanned as code. */
|
|
150
|
-
function endOfSubstitution(text, i) {
|
|
151
|
-
return scan(text, i, { untilBrace: true }).end;
|
|
152
|
-
}
|
|
153
|
-
/** The source of each `${…}` substitution in a template literal's text, outermost only. */
|
|
154
|
-
function templateSubstitutions(template) {
|
|
155
|
-
const out = [];
|
|
156
|
-
for (let j = 1; j < template.length; j++) {
|
|
157
|
-
const c = template[j];
|
|
158
|
-
if (c === "\\") j++;
|
|
159
|
-
else if (c === "$" && template[j + 1] === "{") {
|
|
160
|
-
const end = endOfSubstitution(template, j + 2);
|
|
161
|
-
out.push(template.slice(j + 2, end - 1));
|
|
162
|
-
j = end - 1;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return out;
|
|
166
|
-
}
|
|
167
|
-
/** The end of the regex literal starting at `i`, flags included. */
|
|
168
|
-
function endOfRegex(text, i) {
|
|
169
|
-
let inClass = false;
|
|
170
|
-
for (let j = i + 1; j < text.length; j++) {
|
|
171
|
-
const c = text[j];
|
|
172
|
-
if (c === "\\") j++;
|
|
173
|
-
else if (c === "\n") break;
|
|
174
|
-
else if (inClass) {
|
|
175
|
-
if (c === "]") inClass = false;
|
|
176
|
-
} else if (c === "[") inClass = true;
|
|
177
|
-
else if (c === "/") {
|
|
178
|
-
let k = j + 1;
|
|
179
|
-
while (k < text.length && identPart.test(text[k])) k++;
|
|
180
|
-
return k;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
throw new TokenizeError(`unterminated regular expression at offset ${i}`);
|
|
184
|
-
}
|
|
185
|
-
/** Tokens of `text`; comments are dropped unless `comments` is set (then they are "comment" tokens). */
|
|
186
|
-
function tokenize(text, options = {}) {
|
|
187
|
-
return scan(text, 0, options).tokens;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Scan from `start`. With `untilBrace`, stop after the `}` that closes an
|
|
191
|
-
* enclosing substitution (one not opened in the scanned code) and return
|
|
192
|
-
* the offset after it.
|
|
193
|
-
*/
|
|
194
|
-
function scan(text, start, options) {
|
|
195
|
-
const tokens = [];
|
|
196
|
-
const push = (kind, start, end, op) => {
|
|
197
|
-
tokens.push({
|
|
198
|
-
kind,
|
|
199
|
-
text: text.slice(start, end),
|
|
200
|
-
start,
|
|
201
|
-
end,
|
|
202
|
-
...op ? { op } : {}
|
|
203
|
-
});
|
|
204
|
-
};
|
|
205
|
-
let braces = 0;
|
|
206
|
-
const parens = [];
|
|
207
|
-
const conditions = /* @__PURE__ */ new Set();
|
|
208
|
-
const curlies = [];
|
|
209
|
-
const blocks = /* @__PURE__ */ new Set();
|
|
210
|
-
let i = start;
|
|
211
|
-
while (i < text.length) {
|
|
212
|
-
const c = text[i];
|
|
213
|
-
if (/\s/u.test(c)) {
|
|
214
|
-
i++;
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
if (c === "/" && text[i + 1] === "/") {
|
|
218
|
-
const from = i;
|
|
219
|
-
while (i < text.length && !/[\n\r\u2028\u2029]/u.test(text[i])) i++;
|
|
220
|
-
push("comment", from, i);
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
if (c === "/" && text[i + 1] === "*") {
|
|
224
|
-
const close = text.indexOf("*/", i + 2);
|
|
225
|
-
if (close < 0) throw new TokenizeError(`unterminated comment at offset ${i}`);
|
|
226
|
-
push("comment", i, close + 2);
|
|
227
|
-
i = close + 2;
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
const glyph = glyphs[c];
|
|
231
|
-
if (glyph) {
|
|
232
|
-
push("op", i, i + 1, glyph);
|
|
233
|
-
i++;
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
const ascii = asciiOps.find(([s]) => text.startsWith(s, i));
|
|
237
|
-
if (ascii) {
|
|
238
|
-
push("op", i, i + ascii[0].length, ascii[1]);
|
|
239
|
-
i += ascii[0].length;
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
if (c === "/") {
|
|
243
|
-
if (!endsOperand$1(tokens, tokens.length - 1, conditions, blocks)) {
|
|
244
|
-
const end = endOfRegex(text, i);
|
|
245
|
-
push("regex", i, end);
|
|
246
|
-
i = end;
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
if (text[i + 1] === "\\") {
|
|
250
|
-
push("op", i, i + 2, "and");
|
|
251
|
-
i += 2;
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (c === "\"" || c === "'") {
|
|
256
|
-
const end = endOfString(text, i);
|
|
257
|
-
push("string", i, end);
|
|
258
|
-
i = end;
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
if (c === "`") {
|
|
262
|
-
const end = endOfTemplate(text, i);
|
|
263
|
-
push("template", i, end);
|
|
264
|
-
i = end;
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
const num = numberAt.exec(text.slice(i));
|
|
268
|
-
if (num && (/\d/.test(c) || c === "." && /\d/.test(text[i + 1] ?? ""))) {
|
|
269
|
-
push("number", i, i + num[0].length);
|
|
270
|
-
i += num[0].length;
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
if (identStart.test(c)) {
|
|
274
|
-
let j = i + 1;
|
|
275
|
-
while (j < text.length && identPart.test(text[j])) j++;
|
|
276
|
-
push("ident", i, j);
|
|
277
|
-
i = j;
|
|
278
|
-
continue;
|
|
279
|
-
}
|
|
280
|
-
const p = punctuators.find((s) => text.startsWith(s, i)) ?? c;
|
|
281
|
-
if (p === "(") {
|
|
282
|
-
const before = tokens.findLast((t) => t.kind !== "comment");
|
|
283
|
-
parens.push(before?.kind === "ident" && controlKeywords.has(before.text));
|
|
284
|
-
} else if (p === ")" && parens.pop()) conditions.add(tokens.length);
|
|
285
|
-
if (p === "{") {
|
|
286
|
-
const before = tokens.findLast((t) => t.kind !== "comment");
|
|
287
|
-
curlies.push(before === void 0 || before.kind === "punct" && [
|
|
288
|
-
")",
|
|
289
|
-
"{",
|
|
290
|
-
"}",
|
|
291
|
-
";",
|
|
292
|
-
"=>"
|
|
293
|
-
].includes(before.text) || before.kind === "ident" && [
|
|
294
|
-
"else",
|
|
295
|
-
"do",
|
|
296
|
-
"try",
|
|
297
|
-
"finally"
|
|
298
|
-
].includes(before.text));
|
|
299
|
-
} else if (p === "}" && curlies.pop()) blocks.add(tokens.length);
|
|
300
|
-
if (p === "{") braces++;
|
|
301
|
-
if (p === "}" && braces-- === 0 && options.untilBrace) return {
|
|
302
|
-
tokens: keep(tokens, options),
|
|
303
|
-
end: i + 1
|
|
304
|
-
};
|
|
305
|
-
push("punct", i, i + p.length);
|
|
306
|
-
i += p.length;
|
|
307
|
-
}
|
|
308
|
-
if (options.untilBrace) throw new TokenizeError(`unterminated template substitution at offset ${start}`);
|
|
309
|
-
return {
|
|
310
|
-
tokens: keep(tokens, options),
|
|
311
|
-
end: i
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
function keep(tokens, options) {
|
|
315
|
-
return options.comments ? tokens : tokens.filter((t) => t.kind !== "comment");
|
|
316
|
-
}
|
|
317
|
-
// packages/spec/src/parse.ts
|
|
318
|
-
/**
|
|
319
|
-
* The Lemma `@ensures` property parser: grammar.ebnf of lemma-lang, with
|
|
320
|
-
* the side conditions it states on atoms. A property is a quantifier
|
|
321
|
-
* prefix and a braced formula body; the body's structure (connectives,
|
|
322
|
-
* equations, grouping) is Lemma, and its leaves are TypeScript expressions
|
|
323
|
-
* kept verbatim. Validation that needs the enclosing module (free
|
|
324
|
-
* identifiers exported) is left to whoever evaluates the leaves.
|
|
325
|
-
*/
|
|
326
|
-
/** A property that is not Lemma. `stage` says which part rejected it. */
|
|
327
|
-
var LemmaSyntaxError = class extends Error {
|
|
328
|
-
stage;
|
|
329
|
-
constructor(stage, message) {
|
|
330
|
-
super(message);
|
|
331
|
-
this.stage = stage;
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
const numeric = /* @__PURE__ */ new Set([
|
|
335
|
-
"int",
|
|
336
|
-
"nat",
|
|
337
|
-
"number",
|
|
338
|
-
"bigint"
|
|
339
|
-
]);
|
|
340
|
-
const literalSyntax = {
|
|
341
|
-
int: /^[+-]?\d[\d_]*$/,
|
|
342
|
-
nat: /^[+-]?\d[\d_]*$/,
|
|
343
|
-
bigint: /^[+-]?\d[\d_]*n?$/,
|
|
344
|
-
number: /^[+-]?(?:\d[\d_]*(?:\.[\d_]*)?|\.\d[\d_]*)(?:[eE][+-]?\d+)?$/
|
|
345
|
-
};
|
|
346
|
-
const assignments = /* @__PURE__ */ new Set([
|
|
347
|
-
"=",
|
|
348
|
-
"+=",
|
|
349
|
-
"-=",
|
|
350
|
-
"*=",
|
|
351
|
-
"/=",
|
|
352
|
-
"%=",
|
|
353
|
-
"**=",
|
|
354
|
-
"<<=",
|
|
355
|
-
">>=",
|
|
356
|
-
">>>=",
|
|
357
|
-
"&=",
|
|
358
|
-
"|=",
|
|
359
|
-
"^=",
|
|
360
|
-
"&&=",
|
|
361
|
-
"||=",
|
|
362
|
-
"??="
|
|
363
|
-
]);
|
|
364
|
-
/** Parse the text after `@ensures{name}`. Throws LemmaSyntaxError. */
|
|
365
|
-
function parseProperty(text) {
|
|
366
|
-
let tokens;
|
|
367
|
-
try {
|
|
368
|
-
tokens = tokenize(text);
|
|
369
|
-
} catch (err) {
|
|
370
|
-
if (err instanceof TokenizeError) throw new LemmaSyntaxError("formula", err.message);
|
|
371
|
-
throw err;
|
|
372
|
-
}
|
|
373
|
-
const prefix = new Prefix(tokens);
|
|
374
|
-
return {
|
|
375
|
-
binders: prefix.parse(),
|
|
376
|
-
...new Body(text, tokens, prefix.at).parse()
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
const isOp = (t, op) => t?.kind === "op" && t.op === op;
|
|
380
|
-
const isPunct$1 = (t, p) => t?.kind === "punct" && t.text === p;
|
|
381
|
-
var Prefix = class {
|
|
382
|
-
tokens;
|
|
383
|
-
at = 0;
|
|
384
|
-
constructor(tokens) {
|
|
385
|
-
this.tokens = tokens;
|
|
386
|
-
}
|
|
387
|
-
fail(message) {
|
|
388
|
-
throw new LemmaSyntaxError("prefix", message);
|
|
389
|
-
}
|
|
390
|
-
peek() {
|
|
391
|
-
return this.tokens[this.at];
|
|
392
|
-
}
|
|
393
|
-
next(what) {
|
|
394
|
-
const t = this.tokens[this.at++];
|
|
395
|
-
if (!t) this.fail(`expected ${what}, found the end of the annotation`);
|
|
396
|
-
return t;
|
|
397
|
-
}
|
|
398
|
-
parse() {
|
|
399
|
-
const first = this.next("forall");
|
|
400
|
-
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");
|
|
401
|
-
if (!(isOp(first, "forall") || first.kind === "ident" && first.text === "forall")) this.fail(`a property starts with forall, not ${JSON.stringify(first.text)}`);
|
|
402
|
-
const binders = [];
|
|
403
|
-
while (isPunct$1(this.peek(), "(")) binders.push(...this.group());
|
|
404
|
-
if (binders.length === 0) this.fail("forall needs at least one binder group, as in (x: int)");
|
|
405
|
-
const seen = /* @__PURE__ */ new Set();
|
|
406
|
-
for (const b of binders) {
|
|
407
|
-
if (seen.has(b.name)) this.fail(`${b.name} is bound twice`);
|
|
408
|
-
seen.add(b.name);
|
|
409
|
-
}
|
|
410
|
-
if (!isPunct$1(this.peek(), "{")) this.fail("expected { to open the property body after the binders");
|
|
411
|
-
return binders;
|
|
412
|
-
}
|
|
413
|
-
group() {
|
|
414
|
-
this.next("(");
|
|
415
|
-
const names = [];
|
|
416
|
-
while (this.peek()?.kind === "ident") names.push(this.next("a name").text);
|
|
417
|
-
const colon = this.next(":");
|
|
418
|
-
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)}`);
|
|
419
|
-
if (names.length === 0) this.fail("a binder group needs at least one name before the colon, as in (x: int)");
|
|
420
|
-
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`);
|
|
421
|
-
const d = this.next("a domain");
|
|
422
|
-
if (d.kind !== "ident" || !domains.includes(d.text)) this.fail(`${JSON.stringify(d.text)} is not a Lemma domain (one of ${domains.join(", ")})`);
|
|
423
|
-
const domain = d.text;
|
|
424
|
-
let guard;
|
|
425
|
-
const member = this.peek();
|
|
426
|
-
if (isOp(member, "member") || member?.kind === "ident" && member.text === "in") {
|
|
427
|
-
this.at++;
|
|
428
|
-
guard = this.guard(domain);
|
|
429
|
-
}
|
|
430
|
-
const close = this.next(")");
|
|
431
|
-
if (!isPunct$1(close, ")")) this.fail(`expected ) to close the binder group, found ${JSON.stringify(close.text)}`);
|
|
432
|
-
return names.map((name) => ({
|
|
433
|
-
name,
|
|
434
|
-
domain,
|
|
435
|
-
...guard ? { guard } : {}
|
|
436
|
-
}));
|
|
437
|
-
}
|
|
438
|
-
guard(domain) {
|
|
439
|
-
const t = this.peek();
|
|
440
|
-
if (t?.kind === "regex") {
|
|
441
|
-
this.at++;
|
|
442
|
-
if (domain !== "string") this.fail(`a regex guard applies to string, not ${domain}`);
|
|
443
|
-
const slash = t.text.lastIndexOf("/");
|
|
444
|
-
const pattern = t.text.slice(1, slash);
|
|
445
|
-
const flags = t.text.slice(slash + 1);
|
|
446
|
-
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)}`);
|
|
447
|
-
try {
|
|
448
|
-
new RegExp(`^(?:${pattern})$`, flags);
|
|
449
|
-
} catch (err) {
|
|
450
|
-
this.fail(`the regex guard does not compile: ${String(err)}`);
|
|
451
|
-
}
|
|
452
|
-
return {
|
|
453
|
-
kind: "regex",
|
|
454
|
-
pattern,
|
|
455
|
-
flags
|
|
456
|
-
};
|
|
457
|
-
}
|
|
458
|
-
if (!numeric.has(domain)) this.fail(`an interval guard applies to numeric domains only, not ${domain}`);
|
|
459
|
-
const open = this.next("[ or (");
|
|
460
|
-
if (!(isPunct$1(open, "[") || isPunct$1(open, "("))) this.fail(`expected an interval or a regex after ∈, found ${JSON.stringify(open.text)}`);
|
|
461
|
-
const lower = this.endpoint(domain, isPunct$1(open, "["));
|
|
462
|
-
const comma = this.next(",");
|
|
463
|
-
if (!isPunct$1(comma, ",")) this.fail(`expected , between the interval's endpoints, found ${JSON.stringify(comma.text)}`);
|
|
464
|
-
const upperValue = this.endpointValue(domain);
|
|
465
|
-
const close = this.next("] or )");
|
|
466
|
-
if (isPunct$1(close, ",")) this.fail("an interval has exactly two endpoints");
|
|
467
|
-
if (!(isPunct$1(close, "]") || isPunct$1(close, ")"))) this.fail(`expected ] or ) to close the interval, found ${JSON.stringify(close.text)}`);
|
|
468
|
-
const upper = {
|
|
469
|
-
value: upperValue,
|
|
470
|
-
inclusive: isPunct$1(close, "]")
|
|
471
|
-
};
|
|
472
|
-
this.checkInterval(domain, lower, upper);
|
|
473
|
-
return {
|
|
474
|
-
kind: "interval",
|
|
475
|
-
lower,
|
|
476
|
-
upper
|
|
477
|
-
};
|
|
478
|
-
}
|
|
479
|
-
endpoint(domain, inclusive) {
|
|
480
|
-
return {
|
|
481
|
-
value: this.endpointValue(domain),
|
|
482
|
-
inclusive
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
endpointValue(domain) {
|
|
486
|
-
let sign = "";
|
|
487
|
-
const s = this.peek();
|
|
488
|
-
if (isPunct$1(s, "-") || isPunct$1(s, "+")) {
|
|
489
|
-
this.at++;
|
|
490
|
-
sign = s?.text === "-" ? "-" : "";
|
|
491
|
-
}
|
|
492
|
-
const t = this.next("an endpoint");
|
|
493
|
-
if (isOp(t, "infinity") || t.kind === "ident" && t.text === "Infinity") return `${sign}∞`;
|
|
494
|
-
if (t.kind !== "number") this.fail(`${JSON.stringify(t.text)} is not an interval endpoint`);
|
|
495
|
-
const value = `${sign}${t.text}`;
|
|
496
|
-
const syntax = literalSyntax[domain];
|
|
497
|
-
if (syntax && !syntax.test(value)) this.fail(`${value} is not a ${domain} literal`);
|
|
498
|
-
return value;
|
|
499
|
-
}
|
|
500
|
-
checkInterval(domain, lower, upper) {
|
|
501
|
-
const integral = domain !== "number";
|
|
502
|
-
for (const e of [lower, upper]) if (e.value.endsWith("∞") && e.inclusive && integral) this.fail(`an ∞ endpoint must be exclusive for ${domain}`);
|
|
503
|
-
if (lower.value === "∞") this.fail("the lower endpoint cannot be +∞");
|
|
504
|
-
if (upper.value === "-∞") this.fail("the upper endpoint cannot be -∞");
|
|
505
|
-
const exact = (v) => integral && !v.endsWith("∞") ? BigInt(v.replace(/_/g, "").replace(/n$/, "")) : endpointNumber(v);
|
|
506
|
-
const lo = exact(lower.value);
|
|
507
|
-
const hi = exact(upper.value);
|
|
508
|
-
if (lo > hi || lo === hi && !(lower.inclusive && upper.inclusive)) this.fail(`the interval from ${lower.value} to ${upper.value} is empty`);
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
/** An endpoint's value for ordering: exact for integer literals, as a double otherwise. */
|
|
512
|
-
function endpointNumber(value) {
|
|
513
|
-
if (value === "∞") return Number.POSITIVE_INFINITY;
|
|
514
|
-
if (value === "-∞") return Number.NEGATIVE_INFINITY;
|
|
515
|
-
return Number(value.replace(/_/g, "").replace(/n$/, ""));
|
|
516
|
-
}
|
|
517
|
-
const connectives = /* @__PURE__ */ new Set([
|
|
518
|
-
"and",
|
|
519
|
-
"or",
|
|
520
|
-
"not",
|
|
521
|
-
"implies",
|
|
522
|
-
"iff"
|
|
523
|
-
]);
|
|
524
|
-
var Body = class {
|
|
525
|
-
text;
|
|
526
|
-
tokens;
|
|
527
|
-
open;
|
|
528
|
-
/** Per token of the body: how many brackets enclose it inside the body braces. */
|
|
529
|
-
depth = /* @__PURE__ */ new Map();
|
|
530
|
-
/** Bracket matches, both ways. */
|
|
531
|
-
match = /* @__PURE__ */ new Map();
|
|
532
|
-
/** Per token: the innermost enclosing opening bracket, or -1. */
|
|
533
|
-
enclosing = /* @__PURE__ */ new Map();
|
|
534
|
-
constructor(text, tokens, open) {
|
|
535
|
-
this.text = text;
|
|
536
|
-
this.tokens = tokens;
|
|
537
|
-
this.open = open;
|
|
538
|
-
}
|
|
539
|
-
fail(message) {
|
|
540
|
-
throw new LemmaSyntaxError("formula", message);
|
|
541
|
-
}
|
|
542
|
-
parse() {
|
|
543
|
-
const pairs = {
|
|
544
|
-
")": "(",
|
|
545
|
-
"]": "[",
|
|
546
|
-
"}": "{"
|
|
547
|
-
};
|
|
548
|
-
const stack = [];
|
|
549
|
-
let close = -1;
|
|
550
|
-
for (let i = this.open; i < this.tokens.length; i++) {
|
|
551
|
-
const t = this.tokens[i];
|
|
552
|
-
this.depth.set(i, stack.length - 1);
|
|
553
|
-
this.enclosing.set(i, stack[stack.length - 1] ?? -1);
|
|
554
|
-
if (t.kind !== "punct") continue;
|
|
555
|
-
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
556
|
-
else if (pairs[t.text]) {
|
|
557
|
-
const o = stack.pop();
|
|
558
|
-
if (o === void 0 || this.tokens[o]?.text !== pairs[t.text]) this.fail(`unbalanced ${t.text} in the property body`);
|
|
559
|
-
this.match.set(o, i);
|
|
560
|
-
this.match.set(i, o);
|
|
561
|
-
if (stack.length === 0) {
|
|
562
|
-
close = i;
|
|
563
|
-
break;
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
if (close < 0) this.fail("the property body is not closed with }");
|
|
568
|
-
if (close !== this.tokens.length - 1) this.fail("the } closing the property body must be the annotation's last token");
|
|
569
|
-
const s = this.open + 1;
|
|
570
|
-
const e = close;
|
|
571
|
-
if (s >= e) this.fail("the property body is empty");
|
|
572
|
-
const grouped = isPunct$1(this.tokens[s], "(") && this.match.get(s) === e - 1;
|
|
573
|
-
const f = this.formula(s, e, 0);
|
|
574
|
-
if (!grouped && f.kind === "implies") return {
|
|
575
|
-
requires: [...f.antecedents],
|
|
576
|
-
claim: f.consequent
|
|
577
|
-
};
|
|
578
|
-
return {
|
|
579
|
-
requires: [],
|
|
580
|
-
claim: f
|
|
581
|
-
};
|
|
582
|
-
}
|
|
583
|
-
at(i, base) {
|
|
584
|
-
return this.depth.get(i) === base;
|
|
585
|
-
}
|
|
586
|
-
/** Base-level positions of tokens satisfying `pred` in [s, e). */
|
|
587
|
-
split(s, e, base, pred) {
|
|
588
|
-
const out = [];
|
|
589
|
-
for (let i = s; i < e; i++) if (this.at(i, base) && pred(this.tokens[i], i)) out.push(i);
|
|
590
|
-
return out;
|
|
591
|
-
}
|
|
592
|
-
segments(s, e, cuts) {
|
|
593
|
-
const out = [];
|
|
594
|
-
let from = s;
|
|
595
|
-
for (const c of cuts) {
|
|
596
|
-
out.push([from, c]);
|
|
597
|
-
from = c + 1;
|
|
598
|
-
}
|
|
599
|
-
out.push([from, e]);
|
|
600
|
-
return out;
|
|
601
|
-
}
|
|
602
|
-
formula(s, e, base) {
|
|
603
|
-
if (s >= e) this.fail("a connective is missing an operand");
|
|
604
|
-
const iff = this.split(s, e, base, (t, i) => {
|
|
605
|
-
if (t.op === "iff") return true;
|
|
606
|
-
const prev = this.tokens[i - 1];
|
|
607
|
-
return t.kind === "ident" && t.text === "iff" && !isPunct$1(prev, ".") && !isPunct$1(prev, "?.");
|
|
608
|
-
});
|
|
609
|
-
if (iff.length > 1) this.fail("↔ is non-associative: parenthesize a ↔ b ↔ c");
|
|
610
|
-
if (iff.length === 1) {
|
|
611
|
-
const k = iff[0];
|
|
612
|
-
return {
|
|
613
|
-
kind: "iff",
|
|
614
|
-
left: this.formula(s, k, base),
|
|
615
|
-
right: this.formula(k + 1, e, base)
|
|
616
|
-
};
|
|
617
|
-
}
|
|
618
|
-
const implies = this.split(s, e, base, (t) => t.op === "implies");
|
|
619
|
-
if (implies.length > 0) {
|
|
620
|
-
const parts = this.segments(s, e, implies).map(([a, b]) => this.formula(a, b, base));
|
|
621
|
-
return {
|
|
622
|
-
kind: "implies",
|
|
623
|
-
antecedents: parts.slice(0, -1),
|
|
624
|
-
consequent: parts[parts.length - 1]
|
|
625
|
-
};
|
|
626
|
-
}
|
|
627
|
-
for (const kind of ["or", "and"]) {
|
|
628
|
-
const cuts = this.split(s, e, base, (t) => t.op === kind);
|
|
629
|
-
if (cuts.length > 0) return {
|
|
630
|
-
kind,
|
|
631
|
-
operands: this.segments(s, e, cuts).map(([a, b]) => this.formula(a, b, base))
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
if (isOp(this.tokens[s], "not")) return {
|
|
635
|
-
kind: "not",
|
|
636
|
-
operand: this.formula(s + 1, e, base)
|
|
637
|
-
};
|
|
638
|
-
if (isPunct$1(this.tokens[s], "(") && this.match.get(s) === e - 1) return this.formula(s + 1, e - 1, base + 1);
|
|
639
|
-
return this.atom(s, e, base);
|
|
640
|
-
}
|
|
641
|
-
slice(s, e) {
|
|
642
|
-
return this.text.slice(this.tokens[s].start, this.tokens[e - 1].end).trim();
|
|
643
|
-
}
|
|
644
|
-
atom(s, e, base) {
|
|
645
|
-
const equations = [];
|
|
646
|
-
for (let i = s; i < e; i++) {
|
|
647
|
-
const t = this.tokens[i];
|
|
648
|
-
const prev = i > s ? this.tokens[i - 1] : void 0;
|
|
649
|
-
this.checkToken(t, prev);
|
|
650
|
-
if (t.kind === "op") {
|
|
651
|
-
if (t.op === "equiv" || t.op === "nequiv") {
|
|
652
|
-
if (!this.at(i, base)) this.fail(`${t.text} only joins an atom's two sides; in nested positions call Object.is`);
|
|
653
|
-
equations.push(i);
|
|
654
|
-
} else if (connectives.has(t.op)) this.fail(`${t.text} inside a JavaScript expression: use && || ! there, or parenthesize the whole formula`);
|
|
655
|
-
else this.fail(`${t.text} belongs in a binder group, not the property body`);
|
|
656
|
-
}
|
|
657
|
-
if (t.kind !== "punct") continue;
|
|
658
|
-
if (assignments.has(t.text) && !this.inParameterList(i)) this.fail(`assignments are not allowed in a property (${t.text})`);
|
|
659
|
-
if (!this.at(i, base)) continue;
|
|
660
|
-
if (t.text === "&&" || t.text === "||") this.fail(`${t.text} at an atom's top level: use ${t.text === "&&" ? "∧" : "∨"}`);
|
|
661
|
-
if (t.text === "!" && !endsOperand(prev)) this.fail("prefix ! at an atom's top level: use ¬");
|
|
662
|
-
}
|
|
663
|
-
if (equations.length > 1) this.fail("≡ is non-associative: write a ≡ b ∧ b ≡ c");
|
|
664
|
-
if (equations.length === 0) return {
|
|
665
|
-
kind: "atom",
|
|
666
|
-
text: this.slice(s, e)
|
|
667
|
-
};
|
|
668
|
-
const k = equations[0];
|
|
669
|
-
for (let i = s; i < e; i++) {
|
|
670
|
-
const t = this.tokens[i];
|
|
671
|
-
if (this.at(i, base) && t.kind === "punct" && [
|
|
672
|
-
"??",
|
|
673
|
-
"?",
|
|
674
|
-
"===",
|
|
675
|
-
"!==",
|
|
676
|
-
",",
|
|
677
|
-
"=>"
|
|
678
|
-
].includes(t.text)) this.fail(`${t.text} beside an equation is ambiguous: parenthesize the side it belongs to`);
|
|
679
|
-
}
|
|
680
|
-
if (k === s || k === e - 1) this.fail(`${this.tokens[k]?.text} needs an expression on both sides`);
|
|
681
|
-
return {
|
|
682
|
-
kind: "equation",
|
|
683
|
-
left: this.slice(s, k),
|
|
684
|
-
right: this.slice(k + 1, e),
|
|
685
|
-
negated: this.tokens[k]?.op === "nequiv"
|
|
686
|
-
};
|
|
687
|
-
}
|
|
688
|
-
/**
|
|
689
|
-
* What no position in a property allows, for one token of an atom or of
|
|
690
|
-
* an expression nested in one: ≠, quantifiers, loose equality; a
|
|
691
|
-
* template's substitutions are nested expressions under the same rules.
|
|
692
|
-
*/
|
|
693
|
-
checkToken(t, prev) {
|
|
694
|
-
if (t.kind === "op") {
|
|
695
|
-
if (t.op === "neq") this.fail("≠ is not Lemma: write ≢ (not the same value) or !==");
|
|
696
|
-
if (t.op === "forall" || t.op === "exists") this.fail("quantifiers nest only in the prefix");
|
|
697
|
-
}
|
|
698
|
-
if (t.kind === "ident" && (t.text === "forall" || t.text === "exists") && !isPunct$1(prev, ".") && !isPunct$1(prev, "?.")) this.fail("quantifiers nest only in the prefix");
|
|
699
|
-
if (t.kind === "template") for (const sub of templateSubstitutions(t.text)) this.nested(sub);
|
|
700
|
-
if (t.kind === "punct" && (t.text === "==" || t.text === "!=")) this.fail(`loose ${t.text} is not allowed: use ≡ / ≢ or === / !==`);
|
|
701
|
-
}
|
|
702
|
-
/** Check an expression nested in an atom (a template substitution): what no position allows. */
|
|
703
|
-
nested(text) {
|
|
704
|
-
let tokens;
|
|
705
|
-
try {
|
|
706
|
-
tokens = tokenize(text);
|
|
707
|
-
} catch (err) {
|
|
708
|
-
if (err instanceof TokenizeError) this.fail(err.message);
|
|
709
|
-
throw err;
|
|
710
|
-
}
|
|
711
|
-
const stack = [];
|
|
712
|
-
const enclosing = [];
|
|
713
|
-
const match = /* @__PURE__ */ new Map();
|
|
714
|
-
tokens.forEach((t, i) => {
|
|
715
|
-
enclosing.push(stack[stack.length - 1] ?? -1);
|
|
716
|
-
if (t.kind !== "punct") return;
|
|
717
|
-
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
718
|
-
else if (t.text === ")" || t.text === "]" || t.text === "}") {
|
|
719
|
-
const o = stack.pop();
|
|
720
|
-
if (o !== void 0) match.set(o, i);
|
|
721
|
-
}
|
|
722
|
-
});
|
|
723
|
-
tokens.forEach((t, i) => {
|
|
724
|
-
this.checkToken(t, tokens[i - 1]);
|
|
725
|
-
if (t.kind === "op") {
|
|
726
|
-
if (t.op === "equiv" || t.op === "nequiv") this.fail(`${t.text} only joins an atom's two sides; in nested positions call Object.is`);
|
|
727
|
-
this.fail(`${t.text} inside a JavaScript expression: use && || ! there, or parenthesize the whole formula`);
|
|
728
|
-
}
|
|
729
|
-
if (t.kind !== "punct") return;
|
|
730
|
-
if (assignments.has(t.text)) {
|
|
731
|
-
const open = enclosing[i] ?? -1;
|
|
732
|
-
const close = open >= 0 ? match.get(open) : void 0;
|
|
733
|
-
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})`);
|
|
734
|
-
}
|
|
735
|
-
});
|
|
736
|
-
}
|
|
737
|
-
/** A default initializer: `=` directly inside the parentheses of an arrow function's parameters. */
|
|
738
|
-
inParameterList(i) {
|
|
739
|
-
if (this.tokens[i]?.text !== "=") return false;
|
|
740
|
-
const open = this.enclosing.get(i) ?? -1;
|
|
741
|
-
if (open < 0 || !isPunct$1(this.tokens[open], "(")) return false;
|
|
742
|
-
const close = this.match.get(open);
|
|
743
|
-
return close !== void 0 && isPunct$1(this.tokens[close + 1], "=>");
|
|
744
|
-
}
|
|
745
|
-
};
|
|
746
|
-
/** Whether a token ends an operand, so a `!` after it is postfix (non-null assertion). */
|
|
747
|
-
function endsOperand(t) {
|
|
748
|
-
if (!t) return false;
|
|
749
|
-
if (t.kind === "punct") return t.text === ")" || t.text === "]" || t.text === "}";
|
|
750
|
-
return t.kind !== "op";
|
|
751
|
-
}
|
|
752
|
-
// packages/spec/src/extract.ts
|
|
753
|
-
const tagLine = /^@(\w+)(?:\{([^}]*)\})?/;
|
|
754
|
-
/** The JSDoc's tags, each with its text up to the next tag. */
|
|
755
|
-
function tags(comment, lineOf) {
|
|
756
|
-
const body = comment.text.slice(3, -2);
|
|
757
|
-
const first = lineOf(comment.start);
|
|
758
|
-
const out = [];
|
|
759
|
-
body.split("\n").forEach((raw, i) => {
|
|
760
|
-
const line = raw.replace(/^\s*\*?\s?/, "");
|
|
761
|
-
const m = tagLine.exec(line.trimStart());
|
|
762
|
-
if (m) out.push({
|
|
763
|
-
name: m[1],
|
|
764
|
-
label: m[2],
|
|
765
|
-
lines: [line.trimStart().slice(m[0].length)],
|
|
766
|
-
line: first + i
|
|
767
|
-
});
|
|
768
|
-
else out[out.length - 1]?.lines.push(line);
|
|
769
|
-
});
|
|
770
|
-
return out.map((t) => ({
|
|
771
|
-
name: t.name,
|
|
772
|
-
label: t.label,
|
|
773
|
-
text: t.lines.join("\n").trim(),
|
|
774
|
-
line: t.line
|
|
775
|
-
}));
|
|
776
|
-
}
|
|
777
|
-
const isWord = (t, w) => t?.kind === "ident" && t.text === w;
|
|
778
|
-
const isPunct = (t, p) => t?.kind === "punct" && t.text === p;
|
|
779
|
-
/** `export [async] function [*] name(` at `i`: the name and the index of `(`. */
|
|
780
|
-
function functionAt(tokens, i) {
|
|
781
|
-
if (!isWord(tokens[i], "export")) return null;
|
|
782
|
-
let j = i + 1;
|
|
783
|
-
if (isWord(tokens[j], "async")) j++;
|
|
784
|
-
if (!isWord(tokens[j], "function")) return null;
|
|
785
|
-
j++;
|
|
786
|
-
if (isPunct(tokens[j], "*")) j++;
|
|
787
|
-
const name = tokens[j];
|
|
788
|
-
if (name?.kind !== "ident") return null;
|
|
789
|
-
j++;
|
|
790
|
-
if (isPunct(tokens[j], "<")) j = skipAngles(tokens, j);
|
|
791
|
-
return isPunct(tokens[j], "(") ? {
|
|
792
|
-
name: name.text,
|
|
793
|
-
open: j
|
|
794
|
-
} : null;
|
|
795
|
-
}
|
|
796
|
-
/** The index after the `>` closing the type parameters opened at `i`. */
|
|
797
|
-
function skipAngles(tokens, i) {
|
|
798
|
-
let depth = 0;
|
|
799
|
-
for (let j = i; j < tokens.length; j++) {
|
|
800
|
-
const t = tokens[j];
|
|
801
|
-
if (t.kind !== "punct") continue;
|
|
802
|
-
if (t.text === "<") depth++;
|
|
803
|
-
else if (t.text === ">" || t.text === ">>" || t.text === ">>>") {
|
|
804
|
-
depth -= t.text.length;
|
|
805
|
-
if (depth <= 0) return j + 1;
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
return tokens.length;
|
|
809
|
-
}
|
|
810
|
-
/**
|
|
811
|
-
* Parameter names of the list opened at `open`; null for destructured and
|
|
812
|
-
* rest parameters. `ambiguous` when a default value has a top-level `<` or
|
|
813
|
-
* `>`: a comparison or a generic call's type arguments (`pair<A, B>()`),
|
|
814
|
-
* which a token scan cannot tell apart, and guessing wrong would bind a
|
|
815
|
-
* binder to another parameter's argument.
|
|
816
|
-
*/
|
|
817
|
-
function params(tokens, open) {
|
|
818
|
-
const firsts = [];
|
|
819
|
-
let depth = 0;
|
|
820
|
-
let start = true;
|
|
821
|
-
let inType = false;
|
|
822
|
-
let inDefault = false;
|
|
823
|
-
let ambiguous = false;
|
|
824
|
-
for (let j = open + 1; j < tokens.length; j++) {
|
|
825
|
-
const t = tokens[j];
|
|
826
|
-
if (depth === 0 && isPunct(t, ")")) break;
|
|
827
|
-
if (start) firsts.push(t);
|
|
828
|
-
start = false;
|
|
829
|
-
if (t.kind !== "punct") continue;
|
|
830
|
-
if (inDefault && depth === 0 && /^(<|>)/.test(t.text) && t.text !== ">=" && t.text !== "<=") ambiguous = true;
|
|
831
|
-
if ([
|
|
832
|
-
"(",
|
|
833
|
-
"[",
|
|
834
|
-
"{"
|
|
835
|
-
].includes(t.text) || inType && t.text === "<") depth++;
|
|
836
|
-
else if ([
|
|
837
|
-
")",
|
|
838
|
-
"]",
|
|
839
|
-
"}"
|
|
840
|
-
].includes(t.text)) depth--;
|
|
841
|
-
else if (inType && /^>+$/.test(t.text)) depth -= t.text.length;
|
|
842
|
-
else if (depth === 0 && t.text === ":") inType = true;
|
|
843
|
-
else if (depth === 0 && t.text === "=") {
|
|
844
|
-
inType = false;
|
|
845
|
-
inDefault = true;
|
|
846
|
-
} else if (t.text === "," && depth === 0) {
|
|
847
|
-
start = true;
|
|
848
|
-
inType = false;
|
|
849
|
-
inDefault = false;
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
return {
|
|
853
|
-
names: firsts.filter((t) => !isWord(t, "this")).map((t) => t.kind === "ident" ? t.text : null),
|
|
854
|
-
ambiguous
|
|
855
|
-
};
|
|
856
|
-
}
|
|
857
|
-
/** Names the module exports as values (not types). */
|
|
858
|
-
function exportedNames(tokens) {
|
|
859
|
-
const names = /* @__PURE__ */ new Set();
|
|
860
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
861
|
-
if (!isWord(tokens[i], "export")) continue;
|
|
862
|
-
let j = i + 1;
|
|
863
|
-
if (isWord(tokens[j], "declare")) j++;
|
|
864
|
-
if (isWord(tokens[j], "default")) continue;
|
|
865
|
-
if (isWord(tokens[j], "async")) j++;
|
|
866
|
-
if (isWord(tokens[j], "abstract")) j++;
|
|
867
|
-
const kw = tokens[j];
|
|
868
|
-
if ([
|
|
869
|
-
"function",
|
|
870
|
-
"class",
|
|
871
|
-
"enum"
|
|
872
|
-
].some((w) => isWord(kw, w))) {
|
|
873
|
-
let k = j + 1;
|
|
874
|
-
if (isPunct(tokens[k], "*")) k++;
|
|
875
|
-
if (tokens[k]?.kind === "ident") names.add(tokens[k].text);
|
|
876
|
-
} else if ([
|
|
877
|
-
"const",
|
|
878
|
-
"let",
|
|
879
|
-
"var"
|
|
880
|
-
].some((w) => isWord(kw, w))) {
|
|
881
|
-
let depth = 0;
|
|
882
|
-
let expectName = true;
|
|
883
|
-
for (let k = j + 1; k < tokens.length; k++) {
|
|
884
|
-
const t = tokens[k];
|
|
885
|
-
if (expectName && depth === 0 && t.kind === "ident") names.add(t.text);
|
|
886
|
-
expectName = false;
|
|
887
|
-
if (t.kind !== "punct") continue;
|
|
888
|
-
if ([
|
|
889
|
-
"(",
|
|
890
|
-
"[",
|
|
891
|
-
"{"
|
|
892
|
-
].includes(t.text)) depth++;
|
|
893
|
-
else if ([
|
|
894
|
-
")",
|
|
895
|
-
"]",
|
|
896
|
-
"}"
|
|
897
|
-
].includes(t.text)) depth--;
|
|
898
|
-
else if (t.text === "," && depth === 0) expectName = true;
|
|
899
|
-
else if (t.text === ";" && depth === 0) break;
|
|
900
|
-
if (depth < 0) break;
|
|
901
|
-
}
|
|
902
|
-
} else if (isPunct(kw, "{")) {
|
|
903
|
-
const close = tokens.findIndex((t, k) => k > j && isPunct(t, "}"));
|
|
904
|
-
if (close < 0 || isWord(tokens[close + 1], "from")) continue;
|
|
905
|
-
for (let k = j + 1; k < close; k++) {
|
|
906
|
-
const t = tokens[k];
|
|
907
|
-
if (t.kind !== "ident" || isWord(tokens[k - 1], "type")) continue;
|
|
908
|
-
if (isWord(tokens[k + 1], "as")) continue;
|
|
909
|
-
if (isWord(t, "as") || isWord(t, "type")) continue;
|
|
910
|
-
names.add(t.text);
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
return [...names];
|
|
915
|
-
}
|
|
916
|
-
function extractSpecs(source) {
|
|
917
|
-
const starts = [0];
|
|
918
|
-
for (let i = 0; i < source.length; i++) if (source[i] === "\n") starts.push(i + 1);
|
|
919
|
-
const lineOf = (offset) => {
|
|
920
|
-
let lo = 0;
|
|
921
|
-
let hi = starts.length - 1;
|
|
922
|
-
while (lo < hi) {
|
|
923
|
-
const mid = lo + hi + 1 >> 1;
|
|
924
|
-
if (starts[mid] <= offset) lo = mid;
|
|
925
|
-
else hi = mid - 1;
|
|
926
|
-
}
|
|
927
|
-
return lo + 1;
|
|
928
|
-
};
|
|
929
|
-
let all;
|
|
930
|
-
try {
|
|
931
|
-
all = tokenize(source, { comments: true });
|
|
932
|
-
} catch (err) {
|
|
933
|
-
if (err instanceof TokenizeError) return {
|
|
934
|
-
functions: [],
|
|
935
|
-
exports: [],
|
|
936
|
-
errors: [{
|
|
937
|
-
line: 1,
|
|
938
|
-
message: err.message
|
|
939
|
-
}]
|
|
940
|
-
};
|
|
941
|
-
throw err;
|
|
942
|
-
}
|
|
943
|
-
const code = all.filter((t) => t.kind !== "comment");
|
|
944
|
-
const indexOf = new Map(code.map((t, i) => [t, i]));
|
|
945
|
-
const errors = [];
|
|
946
|
-
const functions = [];
|
|
947
|
-
all.forEach((comment, i) => {
|
|
948
|
-
if (comment.kind !== "comment" || !comment.text.startsWith("/**")) return;
|
|
949
|
-
const found = tags(comment, lineOf).filter((t) => [
|
|
950
|
-
"ensures",
|
|
951
|
-
"total",
|
|
952
|
-
"throws"
|
|
953
|
-
].includes(t.name));
|
|
954
|
-
if (found.length === 0) return;
|
|
955
|
-
const next = all.slice(i + 1).find((t) => t.kind !== "comment");
|
|
956
|
-
const decl = next ? functionAt(code, indexOf.get(next) ?? -1) : null;
|
|
957
|
-
if (!decl) {
|
|
958
|
-
errors.push({
|
|
959
|
-
line: found[0]?.line ?? lineOf(comment.start),
|
|
960
|
-
message: "Lemma annotations attach to an exported function declaration"
|
|
961
|
-
});
|
|
962
|
-
return;
|
|
963
|
-
}
|
|
964
|
-
const ensures = [];
|
|
965
|
-
for (const t of found) {
|
|
966
|
-
if (t.name !== "ensures") continue;
|
|
967
|
-
const name = t.label?.trim();
|
|
968
|
-
if (!name) {
|
|
969
|
-
errors.push({
|
|
970
|
-
line: t.line,
|
|
971
|
-
message: "@ensures needs a name, as in @ensures{non-negative}"
|
|
972
|
-
});
|
|
973
|
-
continue;
|
|
974
|
-
}
|
|
975
|
-
if (ensures.some((e) => e.name === name)) {
|
|
976
|
-
errors.push({
|
|
977
|
-
line: t.line,
|
|
978
|
-
message: `${decl.name} has two properties named ${name}`
|
|
979
|
-
});
|
|
980
|
-
continue;
|
|
981
|
-
}
|
|
982
|
-
try {
|
|
983
|
-
ensures.push({
|
|
984
|
-
name,
|
|
985
|
-
property: parseProperty(t.text),
|
|
986
|
-
text: t.text
|
|
987
|
-
});
|
|
988
|
-
} catch (err) {
|
|
989
|
-
if (!(err instanceof LemmaSyntaxError)) throw err;
|
|
990
|
-
errors.push({
|
|
991
|
-
line: t.line,
|
|
992
|
-
message: `@ensures{${name}} on ${decl.name}: ${err.message}`
|
|
993
|
-
});
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
if (functions.some((f) => f.name === decl.name)) {
|
|
997
|
-
errors.push({
|
|
998
|
-
line: lineOf(code[decl.open].start),
|
|
999
|
-
message: `${decl.name} is annotated on more than one declaration (overloads): put its annotations on one`
|
|
1000
|
-
});
|
|
1001
|
-
return;
|
|
1002
|
-
}
|
|
1003
|
-
const signature = params(code, decl.open);
|
|
1004
|
-
if (signature.ambiguous) errors.push({
|
|
1005
|
-
line: lineOf(code[decl.open].start),
|
|
1006
|
-
message: `cannot tell the parameters of ${decl.name} apart: a default value has a top-level < or >; parenthesize it`
|
|
1007
|
-
});
|
|
1008
|
-
functions.push({
|
|
1009
|
-
name: decl.name,
|
|
1010
|
-
params: signature.names,
|
|
1011
|
-
ensures,
|
|
1012
|
-
total: found.some((t) => t.name === "total"),
|
|
1013
|
-
throws: found.some((t) => t.name === "throws"),
|
|
1014
|
-
line: lineOf(code[decl.open].start),
|
|
1015
|
-
offset: code[decl.open].start
|
|
1016
|
-
});
|
|
1017
|
-
});
|
|
1018
|
-
return {
|
|
1019
|
-
functions,
|
|
1020
|
-
exports: exportedNames(code),
|
|
1021
|
-
errors
|
|
1022
|
-
};
|
|
1023
|
-
}
|
|
1024
|
-
// packages/spec/src/predicate.ts
|
|
1025
|
-
const tsType = {
|
|
1026
|
-
int: "number",
|
|
1027
|
-
nat: "number",
|
|
1028
|
-
number: "number",
|
|
1029
|
-
boolean: "boolean",
|
|
1030
|
-
string: "string",
|
|
1031
|
-
bigint: "bigint"
|
|
1032
|
-
};
|
|
1033
|
-
/** Words that cannot name a binding. */
|
|
1034
|
-
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(" "));
|
|
1035
|
-
function islands(f) {
|
|
1036
|
-
switch (f.kind) {
|
|
1037
|
-
case "atom": return [f.text];
|
|
1038
|
-
case "equation": return [f.left, f.right];
|
|
1039
|
-
case "not": return islands(f.operand);
|
|
1040
|
-
case "and":
|
|
1041
|
-
case "or": return f.operands.flatMap(islands);
|
|
1042
|
-
case "implies": return [...f.antecedents, f.consequent].flatMap(islands);
|
|
1043
|
-
case "iff": return [...islands(f.left), ...islands(f.right)];
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
/**
|
|
1047
|
-
* Indices of the tokens that are TypeScript type positions or binding
|
|
1048
|
-
* names, not references, and the scopes arrow functions bind names in.
|
|
1049
|
-
*/
|
|
1050
|
-
function notReferences(tokens) {
|
|
1051
|
-
const skip = /* @__PURE__ */ new Set();
|
|
1052
|
-
const scopes = [];
|
|
1053
|
-
const match = /* @__PURE__ */ new Map();
|
|
1054
|
-
const stack = [];
|
|
1055
|
-
/** Per token, the innermost open bracket around it, or -1. */
|
|
1056
|
-
const enclosing = [];
|
|
1057
|
-
tokens.forEach((t, i) => {
|
|
1058
|
-
enclosing.push(stack[stack.length - 1] ?? -1);
|
|
1059
|
-
if (t.kind !== "punct") return;
|
|
1060
|
-
if (t.text === "(" || t.text === "[" || t.text === "{") stack.push(i);
|
|
1061
|
-
else if (t.text === ")" || t.text === "]" || t.text === "}") {
|
|
1062
|
-
const o = stack.pop();
|
|
1063
|
-
if (o !== void 0) match.set(o, i);
|
|
1064
|
-
}
|
|
1065
|
-
});
|
|
1066
|
-
const is = (k, text) => tokens[k]?.kind === "punct" && tokens[k]?.text === text;
|
|
1067
|
-
/** The last token of an arrow body starting at `k`: a block, or an expression up to a top-level `,` or close. */
|
|
1068
|
-
const bodyEnd = (k) => {
|
|
1069
|
-
if (is(k, "{")) return match.get(k) ?? tokens.length - 1;
|
|
1070
|
-
let depth = 0;
|
|
1071
|
-
for (let m = k; m < tokens.length; m++) {
|
|
1072
|
-
const u = tokens[m];
|
|
1073
|
-
if (u.kind !== "punct") continue;
|
|
1074
|
-
if (u.text === "(" || u.text === "[" || u.text === "{") depth++;
|
|
1075
|
-
else if (u.text === ")" || u.text === "]" || u.text === "}") {
|
|
1076
|
-
if (depth === 0) return m - 1;
|
|
1077
|
-
depth--;
|
|
1078
|
-
} else if (u.text === "," && depth === 0) return m - 1;
|
|
1079
|
-
}
|
|
1080
|
-
return tokens.length - 1;
|
|
1081
|
-
};
|
|
1082
|
-
/**
|
|
1083
|
-
* The index just after a type starting at `k`: names, dots, brackets,
|
|
1084
|
-
* unions and intersections, and type arguments, but a `<` only when its
|
|
1085
|
-
* `>` closes before anything that cannot be in a type (else it compares).
|
|
1086
|
-
*/
|
|
1087
|
-
const typeEnd = (k) => {
|
|
1088
|
-
const typeish = (u, angles) => u !== void 0 && (u.kind === "ident" || u.kind === "string" || u.kind === "number" || u.kind === "punct" && [
|
|
1089
|
-
".",
|
|
1090
|
-
"[",
|
|
1091
|
-
"]",
|
|
1092
|
-
"|",
|
|
1093
|
-
"&"
|
|
1094
|
-
].includes(u.text) || angles > 0 && u.kind === "punct" && [
|
|
1095
|
-
",",
|
|
1096
|
-
"(",
|
|
1097
|
-
")",
|
|
1098
|
-
"=>",
|
|
1099
|
-
":",
|
|
1100
|
-
"{",
|
|
1101
|
-
"}",
|
|
1102
|
-
";"
|
|
1103
|
-
].includes(u.text));
|
|
1104
|
-
let m = k;
|
|
1105
|
-
while (m < tokens.length) {
|
|
1106
|
-
const u = tokens[m];
|
|
1107
|
-
if (u.kind === "punct" && u.text === "<") {
|
|
1108
|
-
let angles = 0;
|
|
1109
|
-
let n = m;
|
|
1110
|
-
for (; n < tokens.length; n++) {
|
|
1111
|
-
const w = tokens[n];
|
|
1112
|
-
if (w.kind === "punct" && w.text === "<") angles++;
|
|
1113
|
-
else if (w.kind === "punct" && /^>+$/.test(w.text)) {
|
|
1114
|
-
angles -= w.text.length;
|
|
1115
|
-
if (angles <= 0) break;
|
|
1116
|
-
} else if (!typeish(w, angles)) break;
|
|
1117
|
-
}
|
|
1118
|
-
if (angles > 0 || n >= tokens.length || !/^>+$/.test(tokens[n].text)) return m;
|
|
1119
|
-
m = n + 1;
|
|
1120
|
-
continue;
|
|
1121
|
-
}
|
|
1122
|
-
if (!typeish(u, 0)) return m;
|
|
1123
|
-
m++;
|
|
1124
|
-
}
|
|
1125
|
-
return m;
|
|
1126
|
-
};
|
|
1127
|
-
/** After the `)` of a parameter list at `close`: an optional `: ReturnType`, then the index of what follows. */
|
|
1128
|
-
const afterReturnType = (close) => {
|
|
1129
|
-
if (!is(close + 1, ":")) return close + 1;
|
|
1130
|
-
const end = typeEnd(close + 2);
|
|
1131
|
-
for (let m = close + 2; m < end; m++) skip.add(m);
|
|
1132
|
-
return end;
|
|
1133
|
-
};
|
|
1134
|
-
/**
|
|
1135
|
-
* The names a parameter list from `open` to `close` binds, skipping them and
|
|
1136
|
-
* their type annotations. A `:` directly in the list starts a type; inside a
|
|
1137
|
-
* destructuring pattern it renames (`{ a: b }` binds b).
|
|
1138
|
-
*/
|
|
1139
|
-
const parameters = (open, close) => {
|
|
1140
|
-
const names = /* @__PURE__ */ new Set();
|
|
1141
|
-
const nesting = [];
|
|
1142
|
-
let inType = false;
|
|
1143
|
-
let inDefault = false;
|
|
1144
|
-
let defaultDepth = 0;
|
|
1145
|
-
for (let k = open + 1; k < close; k++) {
|
|
1146
|
-
const u = tokens[k];
|
|
1147
|
-
if (inDefault) {
|
|
1148
|
-
if (u.kind !== "punct") continue;
|
|
1149
|
-
if (u.text === "(" || u.text === "[" || u.text === "{") {
|
|
1150
|
-
defaultDepth++;
|
|
1151
|
-
continue;
|
|
1152
|
-
}
|
|
1153
|
-
if ((u.text === ")" || u.text === "]" || u.text === "}") && defaultDepth > 0) {
|
|
1154
|
-
defaultDepth--;
|
|
1155
|
-
continue;
|
|
1156
|
-
}
|
|
1157
|
-
if (defaultDepth > 0 || !(u.text === "," || u.text === ")" || u.text === "]" || u.text === "}")) continue;
|
|
1158
|
-
inDefault = false;
|
|
1159
|
-
}
|
|
1160
|
-
if (u.kind === "punct") {
|
|
1161
|
-
if (!inType && u.text === "=") {
|
|
1162
|
-
inDefault = true;
|
|
1163
|
-
defaultDepth = 0;
|
|
1164
|
-
continue;
|
|
1165
|
-
}
|
|
1166
|
-
if (inType) {
|
|
1167
|
-
if (u.text === "(" || u.text === "[" || u.text === "{" || u.text === "<") nesting.push(u.text);
|
|
1168
|
-
else if (nesting.length > 0 && [
|
|
1169
|
-
")",
|
|
1170
|
-
"]",
|
|
1171
|
-
"}"
|
|
1172
|
-
].includes(u.text)) nesting.pop();
|
|
1173
|
-
else if (nesting.length > 0 && /^>+$/.test(u.text)) nesting.splice(-u.text.length);
|
|
1174
|
-
else if (nesting.length === 0 && u.text === ",") inType = false;
|
|
1175
|
-
else if (nesting.length === 0 && u.text === "=") {
|
|
1176
|
-
inType = false;
|
|
1177
|
-
inDefault = true;
|
|
1178
|
-
defaultDepth = 0;
|
|
1179
|
-
}
|
|
1180
|
-
continue;
|
|
1181
|
-
}
|
|
1182
|
-
if (u.text === "{" || u.text === "[") nesting.push(u.text);
|
|
1183
|
-
else if (u.text === "}" || u.text === "]") nesting.pop();
|
|
1184
|
-
else if (u.text === ":" && nesting.length === 0) inType = true;
|
|
1185
|
-
continue;
|
|
1186
|
-
}
|
|
1187
|
-
if (inType) {
|
|
1188
|
-
skip.add(k);
|
|
1189
|
-
continue;
|
|
1190
|
-
}
|
|
1191
|
-
if (u.kind !== "ident") continue;
|
|
1192
|
-
const prev = tokens[k - 1];
|
|
1193
|
-
const next = tokens[k + 1];
|
|
1194
|
-
if (nesting.length > 0 && next?.kind === "punct" && next.text === ":") {
|
|
1195
|
-
skip.add(k);
|
|
1196
|
-
continue;
|
|
1197
|
-
}
|
|
1198
|
-
if (prev?.kind === "punct" && [
|
|
1199
|
-
"(",
|
|
1200
|
-
",",
|
|
1201
|
-
"...",
|
|
1202
|
-
"{",
|
|
1203
|
-
"[",
|
|
1204
|
-
":"
|
|
1205
|
-
].includes(prev.text)) {
|
|
1206
|
-
names.add(u.text);
|
|
1207
|
-
skip.add(k);
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
return names;
|
|
1211
|
-
};
|
|
1212
|
-
tokens.forEach((t, i) => {
|
|
1213
|
-
if (t.kind === "ident" && is(i + 1, "=>")) {
|
|
1214
|
-
skip.add(i);
|
|
1215
|
-
scopes.push({
|
|
1216
|
-
names: /* @__PURE__ */ new Set([t.text]),
|
|
1217
|
-
from: i,
|
|
1218
|
-
to: bodyEnd(i + 2)
|
|
1219
|
-
});
|
|
1220
|
-
}
|
|
1221
|
-
const close = is(i, "(") ? match.get(i) : void 0;
|
|
1222
|
-
const arrow = close === void 0 ? -1 : afterReturnType(close);
|
|
1223
|
-
if (close !== void 0 && is(arrow, "=>")) scopes.push({
|
|
1224
|
-
names: parameters(i, close),
|
|
1225
|
-
from: i,
|
|
1226
|
-
to: bodyEnd(arrow + 1)
|
|
1227
|
-
});
|
|
1228
|
-
if (t.kind === "ident" && is(i + 1, "(") && (is(i - 1, "{") || is(i - 1, ","))) {
|
|
1229
|
-
const end = match.get(i + 1);
|
|
1230
|
-
const body = end === void 0 ? -1 : afterReturnType(end);
|
|
1231
|
-
if (end !== void 0 && is(body, "{")) {
|
|
1232
|
-
skip.add(i);
|
|
1233
|
-
scopes.push({
|
|
1234
|
-
names: parameters(i + 1, end),
|
|
1235
|
-
from: i + 1,
|
|
1236
|
-
to: match.get(body) ?? tokens.length - 1
|
|
1237
|
-
});
|
|
1238
|
-
}
|
|
1239
|
-
}
|
|
1240
|
-
if (t.kind === "ident" && t.text === "for" && is(i + 1, "(")) {
|
|
1241
|
-
const end = match.get(i + 1);
|
|
1242
|
-
const decl = tokens[i + 2];
|
|
1243
|
-
if (end !== void 0 && decl?.kind === "ident" && [
|
|
1244
|
-
"const",
|
|
1245
|
-
"let",
|
|
1246
|
-
"var"
|
|
1247
|
-
].includes(decl.text)) {
|
|
1248
|
-
const names = /* @__PURE__ */ new Set();
|
|
1249
|
-
for (let k = i + 3; k < end; k++) {
|
|
1250
|
-
const u = tokens[k];
|
|
1251
|
-
if (u.kind === "ident" && (u.text === "of" || u.text === "in")) {
|
|
1252
|
-
skip.add(k);
|
|
1253
|
-
break;
|
|
1254
|
-
}
|
|
1255
|
-
if (u.kind === "ident" && !is(k + 1, ":")) {
|
|
1256
|
-
names.add(u.text);
|
|
1257
|
-
skip.add(k);
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
const body = end + 1;
|
|
1261
|
-
const to = is(body, "{") ? match.get(body) ?? tokens.length - 1 : bodyEnd(body);
|
|
1262
|
-
scopes.push({
|
|
1263
|
-
names,
|
|
1264
|
-
from: i,
|
|
1265
|
-
to
|
|
1266
|
-
});
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
if (t.kind === "ident" && t.text === "catch" && is(i + 1, "(")) {
|
|
1270
|
-
const end = match.get(i + 1);
|
|
1271
|
-
if (end !== void 0 && is(end + 1, "{")) scopes.push({
|
|
1272
|
-
names: parameters(i + 1, end),
|
|
1273
|
-
from: i + 1,
|
|
1274
|
-
to: match.get(end + 1) ?? tokens.length - 1
|
|
1275
|
-
});
|
|
1276
|
-
}
|
|
1277
|
-
if (t.kind === "ident" && t.text === "function") {
|
|
1278
|
-
const named = tokens[i + 1]?.kind === "ident";
|
|
1279
|
-
const statement = i === 0 || [
|
|
1280
|
-
"{",
|
|
1281
|
-
"}",
|
|
1282
|
-
";"
|
|
1283
|
-
].some((p) => is(i - 1, p));
|
|
1284
|
-
if (named && statement) {
|
|
1285
|
-
const open = enclosing[i] ?? -1;
|
|
1286
|
-
skip.add(i + 1);
|
|
1287
|
-
scopes.push({
|
|
1288
|
-
names: /* @__PURE__ */ new Set([tokens[i + 1].text]),
|
|
1289
|
-
from: open,
|
|
1290
|
-
to: open >= 0 ? match.get(open) ?? tokens.length - 1 : tokens.length - 1
|
|
1291
|
-
});
|
|
1292
|
-
}
|
|
1293
|
-
const open = named ? i + 2 : i + 1;
|
|
1294
|
-
const end = is(open, "(") ? match.get(open) : void 0;
|
|
1295
|
-
const body = end === void 0 ? -1 : afterReturnType(end);
|
|
1296
|
-
if (end !== void 0 && is(body, "{")) {
|
|
1297
|
-
const names = parameters(open, end);
|
|
1298
|
-
if (named) {
|
|
1299
|
-
names.add(tokens[i + 1].text);
|
|
1300
|
-
skip.add(i + 1);
|
|
1301
|
-
}
|
|
1302
|
-
scopes.push({
|
|
1303
|
-
names,
|
|
1304
|
-
from: i,
|
|
1305
|
-
to: match.get(body) ?? tokens.length - 1
|
|
1306
|
-
});
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
if (t.kind === "ident" && is(i + 1, ":") && (is(i - 1, "{") || is(i - 1, ","))) skip.add(i);
|
|
1310
|
-
if (t.kind === "ident" && (t.text === "as" || t.text === "satisfies")) {
|
|
1311
|
-
skip.add(i);
|
|
1312
|
-
for (let k = i + 1; k < typeEnd(i + 1); k++) skip.add(k);
|
|
1313
|
-
}
|
|
1314
|
-
if (t.kind === "ident" && is(i + 1, "<")) {
|
|
1315
|
-
const end = typeEnd(i + 1);
|
|
1316
|
-
if (end > i + 1 && is(end, "(")) for (let m = i + 2; m < end; m++) skip.add(m);
|
|
1317
|
-
}
|
|
1318
|
-
});
|
|
1319
|
-
return {
|
|
1320
|
-
skip,
|
|
1321
|
-
scopes
|
|
1322
|
-
};
|
|
1323
|
-
}
|
|
1324
|
-
/**
|
|
1325
|
-
* Identifiers the islands refer to from outside: not after a `.`, not a
|
|
1326
|
-
* binder, not an object key, an arrow function's parameter or a type. What
|
|
1327
|
-
* is left may still over-approximate (a `function` expression's parameter),
|
|
1328
|
-
* which only costs a binding the inner name shadows.
|
|
1329
|
-
*/
|
|
1330
|
-
function freeNames(p) {
|
|
1331
|
-
const bound = new Set(p.binders.map((b) => b.name));
|
|
1332
|
-
const names = /* @__PURE__ */ new Set();
|
|
1333
|
-
const scan = (text, outer) => {
|
|
1334
|
-
const tokens = tokenize(text);
|
|
1335
|
-
const { skip, scopes } = notReferences(tokens);
|
|
1336
|
-
const local = (n, i) => scopes.some((sc) => sc.names.has(n) && sc.from <= i && i <= sc.to);
|
|
1337
|
-
tokens.forEach((t, i) => {
|
|
1338
|
-
if (t.kind === "template") {
|
|
1339
|
-
const inScope = new Set(outer);
|
|
1340
|
-
for (const sc of scopes) if (sc.from <= i && i <= sc.to) for (const n of sc.names) inScope.add(n);
|
|
1341
|
-
for (const sub of templateSubstitutions(t.text)) scan(sub, inScope);
|
|
1342
|
-
}
|
|
1343
|
-
const prev = tokens[i - 1];
|
|
1344
|
-
if (t.kind !== "ident" || skip.has(i) || bound.has(t.text) || reserved.has(t.text) || outer.has(t.text) || local(t.text, i)) return;
|
|
1345
|
-
if (prev?.kind === "punct" && (prev.text === "." || prev.text === "?.")) return;
|
|
1346
|
-
names.add(t.text);
|
|
1347
|
-
});
|
|
1348
|
-
};
|
|
1349
|
-
for (const text of [...p.requires, p.claim].flatMap(islands)) scan(text, /* @__PURE__ */ new Set());
|
|
1350
|
-
return [...names];
|
|
1351
|
-
}
|
|
1352
|
-
function expression(f, h) {
|
|
1353
|
-
switch (f.kind) {
|
|
1354
|
-
case "atom": return `${h.bool}((${f.text}))`;
|
|
1355
|
-
case "equation": return `${f.negated ? "!" : ""}${h.same}((${f.left}), (${f.right}))`;
|
|
1356
|
-
case "not": return `!${expression(f.operand, h)}`;
|
|
1357
|
-
case "and":
|
|
1358
|
-
case "or": return `(${f.operands.map((o) => expression(o, h)).join(f.kind === "and" ? " && " : " || ")})`;
|
|
1359
|
-
case "implies": return `(!(${f.antecedents.map((a) => expression(a, h)).join(" && ")}) || ${expression(f.consequent, h)})`;
|
|
1360
|
-
case "iff": return `(${expression(f.left, h)} === ${expression(f.right, h)})`;
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
/**
|
|
1364
|
-
* The predicate's source: an expression evaluating to the arrow function.
|
|
1365
|
-
* It takes the helpers as parameters under names fresh for this property,
|
|
1366
|
-
* so no binder or identifier in it can shadow them, and binds each free
|
|
1367
|
-
* name from the module's namespace when it has it, else from the globals,
|
|
1368
|
-
* which is the scope Lemma gives a property (re-exports included).
|
|
1369
|
-
*/
|
|
1370
|
-
function predicateSource(p, helpers) {
|
|
1371
|
-
const free = freeNames(p).filter((n) => ![
|
|
1372
|
-
"globalThis",
|
|
1373
|
-
"undefined",
|
|
1374
|
-
"NaN",
|
|
1375
|
-
"Infinity"
|
|
1376
|
-
].includes(n));
|
|
1377
|
-
const taken = /* @__PURE__ */ new Set([
|
|
1378
|
-
...p.binders.map((b) => b.name),
|
|
1379
|
-
...free,
|
|
1380
|
-
"globalThis"
|
|
1381
|
-
]);
|
|
1382
|
-
const fresh = (base) => {
|
|
1383
|
-
let name = base;
|
|
1384
|
-
while (taken.has(name)) name = `${name}$`;
|
|
1385
|
-
taken.add(name);
|
|
1386
|
-
return name;
|
|
1387
|
-
};
|
|
1388
|
-
const local = {
|
|
1389
|
-
bool: fresh("__bool"),
|
|
1390
|
-
same: fresh("__same"),
|
|
1391
|
-
scope: fresh("__scope"),
|
|
1392
|
-
unresolved: fresh("__unresolved"),
|
|
1393
|
-
missing: fresh("__missing")
|
|
1394
|
-
};
|
|
1395
|
-
const params = p.binders.map((b) => `${b.name}: ${tsType[b.domain]}`).join(", ");
|
|
1396
|
-
return [
|
|
1397
|
-
`((${local.bool}: (v: unknown) => boolean, ${local.same}: (a: unknown, b: unknown) => boolean, ${local.scope}: any, ${local.unresolved}: (names: string[]) => never) => {`,
|
|
1398
|
-
` const ${local.missing}: string[] = [];`,
|
|
1399
|
-
...free.map((n) => {
|
|
1400
|
-
const k = JSON.stringify(n);
|
|
1401
|
-
return ` const ${n} = ${k} in ${local.scope} ? ${local.scope}[${k}] : ${k} in globalThis ? (globalThis as any)[${k}] : (${local.missing}.push(${k}), undefined);`;
|
|
1402
|
-
}),
|
|
1403
|
-
` return (${params}): "held" | "discarded" | "refuted" => {`,
|
|
1404
|
-
` if (${local.missing}.length > 0) ${local.unresolved}(${local.missing});`,
|
|
1405
|
-
...p.requires.map((r) => ` if (!${expression(r, local)}) return "discarded";`),
|
|
1406
|
-
` return ${expression(p.claim, local)} ? "held" : "refuted";`,
|
|
1407
|
-
" };",
|
|
1408
|
-
`})(${helpers.bool}, ${helpers.same}, ${helpers.scope}, ${helpers.unresolved})`
|
|
1409
|
-
].join("\n");
|
|
1410
|
-
}
|
|
1411
|
-
// packages/prover/src/checked.ts
|
|
1412
|
-
/** Thrown by `lemmaBool` when an atom is not a genuine boolean: the property is malformed, not false. */
|
|
1413
|
-
var AtomNotBoolean = class extends TypeError {};
|
|
1414
|
-
/** Thrown by `lemmaUnresolved`: the property names something neither its module nor the globals define. */
|
|
1415
|
-
var UnresolvedName = class extends ReferenceError {};
|
|
1416
|
-
/** What generated predicates call when free names did not resolve. */
|
|
1417
|
-
function lemmaUnresolved(names) {
|
|
1418
|
-
throw new UnresolvedName(`the property refers to ${names.join(", ")}, which neither its module exports nor the globals define`);
|
|
1419
|
-
}
|
|
1420
|
-
/** The atom check generated predicates call: its argument when it is a boolean. */
|
|
1421
|
-
function lemmaBool(value) {
|
|
1422
|
-
if (typeof value !== "boolean") throw new AtomNotBoolean(`a Lemma atom evaluated to ${render(value)}, not a boolean`);
|
|
1423
|
-
return value;
|
|
1424
|
-
}
|
|
1425
|
-
/** A value as a JavaScript literal, exact for the binder domains. */
|
|
1426
|
-
function render(value) {
|
|
1427
|
-
switch (typeof value) {
|
|
1428
|
-
case "bigint": return `${value}n`;
|
|
1429
|
-
case "number": return Object.is(value, -0) ? "-0" : String(value);
|
|
1430
|
-
case "string": return JSON.stringify(value);
|
|
1431
|
-
case "undefined": return "undefined";
|
|
1432
|
-
case "boolean": return String(value);
|
|
1433
|
-
case "function": return "[function]";
|
|
1434
|
-
}
|
|
1435
|
-
try {
|
|
1436
|
-
const text = JSON.stringify(value, (_, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
1437
|
-
return text === void 0 ? String(value) : text.length > 200 ? `${text.slice(0, 200)}…` : text;
|
|
1438
|
-
} catch {
|
|
1439
|
-
return String(value);
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
/**
|
|
1443
|
-
* One assignment through a predicate. An atom that is not a boolean, or a
|
|
1444
|
-
* name nothing defines, is a malformed property. Any other throw, the
|
|
1445
|
-
* function's own included (JSON.parse's SyntaxError, say), means the claim
|
|
1446
|
-
* is not true there. A ReferenceError counts as malformed: compiled
|
|
1447
|
-
* TypeScript under test hardly throws one, a name the property's scope
|
|
1448
|
-
* analysis missed does, and that must never pass for a refutation.
|
|
1449
|
-
*/
|
|
1450
|
-
function evaluate(predicate, values) {
|
|
1451
|
-
try {
|
|
1452
|
-
return { outcome: predicate(...values) };
|
|
1453
|
-
} catch (err) {
|
|
1454
|
-
if (err instanceof AtomNotBoolean || err instanceof ReferenceError) return { malformed: String(err) };
|
|
1455
|
-
return {
|
|
1456
|
-
outcome: "refuted",
|
|
1457
|
-
error: String(err)
|
|
1458
|
-
};
|
|
1459
|
-
}
|
|
1460
|
-
}
|
|
1461
|
-
/** A finite integer endpoint's value, exact: underscores and the `n` suffix dropped. */
|
|
1462
|
-
function endpointBigInt(value) {
|
|
1463
|
-
return BigInt(value.replace(/_/g, "").replace(/n$/, ""));
|
|
1464
|
-
}
|
|
1465
|
-
function bound(value, integral) {
|
|
1466
|
-
if (integral && !value.endsWith("∞")) return endpointBigInt(value);
|
|
1467
|
-
return endpointNumber(value);
|
|
1468
|
-
}
|
|
1469
|
-
/** How every rung words an `UNKNOWN` for a property that cannot be evaluated; the ladder recognizes it. */
|
|
1470
|
-
const malformedReason = (detail) => `the property could not be evaluated: ${detail}`;
|
|
1471
|
-
/** Whether a value lies in a binder's domain and guard. */
|
|
1472
|
-
function inDomain(b, v) {
|
|
1473
|
-
switch (b.domain) {
|
|
1474
|
-
case "int":
|
|
1475
|
-
if (!(typeof v === "number" && Number.isInteger(v))) return false;
|
|
1476
|
-
break;
|
|
1477
|
-
case "nat":
|
|
1478
|
-
if (!(typeof v === "number" && Number.isInteger(v) && v >= 0)) return false;
|
|
1479
|
-
break;
|
|
1480
|
-
case "number":
|
|
1481
|
-
if (typeof v !== "number") return false;
|
|
1482
|
-
break;
|
|
1483
|
-
case "bigint":
|
|
1484
|
-
if (typeof v !== "bigint") return false;
|
|
1485
|
-
break;
|
|
1486
|
-
case "string":
|
|
1487
|
-
if (typeof v !== "string") return false;
|
|
1488
|
-
break;
|
|
1489
|
-
case "boolean": return typeof v === "boolean";
|
|
1490
|
-
}
|
|
1491
|
-
const g = b.guard;
|
|
1492
|
-
if (!g) return true;
|
|
1493
|
-
if (g.kind === "regex") return new RegExp(`^(?:${g.pattern})$`, g.flags).test(v);
|
|
1494
|
-
const integral = b.domain !== "number";
|
|
1495
|
-
const x = integral ? BigInt(v) : v;
|
|
1496
|
-
const lo = bound(g.lower.value, integral);
|
|
1497
|
-
const hi = bound(g.upper.value, integral);
|
|
1498
|
-
const aboveLower = g.lower.inclusive ? x >= lo : x > lo;
|
|
1499
|
-
const belowUpper = g.upper.inclusive ? x <= hi : x < hi;
|
|
1500
|
-
return aboveLower && belowUpper;
|
|
1501
|
-
}
|
|
1502
|
-
function checkedRung(input) {
|
|
1503
|
-
const { module, fn, property } = input;
|
|
1504
|
-
const base = {
|
|
1505
|
-
module,
|
|
1506
|
-
function: fn.name,
|
|
1507
|
-
property: property.name,
|
|
1508
|
-
text: property.text
|
|
1509
|
-
};
|
|
1510
|
-
const unknown = (cause, reason) => ({
|
|
1511
|
-
...base,
|
|
1512
|
-
kind: "UNKNOWN",
|
|
1513
|
-
cause,
|
|
1514
|
-
reason,
|
|
1515
|
-
assumptions: []
|
|
1516
|
-
});
|
|
1517
|
-
if (input.unobservable) return unknown("unavailable", `calls to ${fn.name} could not be observed: ${input.unobservable}`);
|
|
1518
|
-
const predicate = input.predicate;
|
|
1519
|
-
if (!predicate) return unknown("unavailable", "the property's predicate was not compiled");
|
|
1520
|
-
const binders = property.property.binders;
|
|
1521
|
-
const positions = [];
|
|
1522
|
-
for (const b of binders) {
|
|
1523
|
-
const at = fn.params.indexOf(b.name);
|
|
1524
|
-
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`);
|
|
1525
|
-
positions.push(at);
|
|
1526
|
-
}
|
|
1527
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1528
|
-
let held = 0;
|
|
1529
|
-
let discarded = 0;
|
|
1530
|
-
for (const call of input.calls) {
|
|
1531
|
-
const values = positions.map((p) => call.args[p]);
|
|
1532
|
-
const key = values.map(render).join(", ");
|
|
1533
|
-
if (seen.has(key)) continue;
|
|
1534
|
-
seen.add(key);
|
|
1535
|
-
if (!binders.every((b, i) => inDomain(b, values[i]))) {
|
|
1536
|
-
discarded++;
|
|
1537
|
-
continue;
|
|
1538
|
-
}
|
|
1539
|
-
const counterexample = (error) => ({
|
|
1540
|
-
binders: binders.map((b, i) => ({
|
|
1541
|
-
name: b.name,
|
|
1542
|
-
value: render(values[i])
|
|
1543
|
-
})),
|
|
1544
|
-
call: `${fn.name}(${call.args.map(render).join(", ")})`,
|
|
1545
|
-
...error === void 0 ? {} : { error }
|
|
1546
|
-
});
|
|
1547
|
-
const result = evaluate(predicate, values);
|
|
1548
|
-
if ("malformed" in result) return unknown("unsupported", malformedReason(result.malformed));
|
|
1549
|
-
if (result.outcome === "refuted") return {
|
|
1550
|
-
...base,
|
|
1551
|
-
kind: "REFUTED",
|
|
1552
|
-
rung: "CHECKED",
|
|
1553
|
-
counterexample: counterexample("error" in result ? result.error : void 0),
|
|
1554
|
-
assumptions: []
|
|
1555
|
-
};
|
|
1556
|
-
if (result.outcome === "discarded") discarded++;
|
|
1557
|
-
else held++;
|
|
1558
|
-
}
|
|
1559
|
-
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)`);
|
|
1560
|
-
return {
|
|
1561
|
-
...base,
|
|
1562
|
-
kind: "CHECKED",
|
|
1563
|
-
inputs: held,
|
|
1564
|
-
discarded,
|
|
1565
|
-
assumptions: [`held on the ${held} distinct input(s) the simulator passed to ${fn.name}; inputs it never passed are not covered`]
|
|
1566
|
-
};
|
|
1567
|
-
}
|
|
1568
|
-
/** Verdicts for the markers the CHECKED rung cannot decide. */
|
|
1569
|
-
function markerVerdicts(module, fn) {
|
|
1570
|
-
const out = [];
|
|
1571
|
-
const base = {
|
|
1572
|
-
module,
|
|
1573
|
-
function: fn.name,
|
|
1574
|
-
assumptions: []
|
|
1575
|
-
};
|
|
1576
|
-
if (fn.total) out.push({
|
|
1577
|
-
...base,
|
|
1578
|
-
property: "@total",
|
|
1579
|
-
text: "@total",
|
|
1580
|
-
kind: "UNKNOWN",
|
|
1581
|
-
cause: "unsupported",
|
|
1582
|
-
reason: "termination is not observable on the inputs the simulator passed; @total needs a prover"
|
|
1583
|
-
});
|
|
1584
|
-
if (fn.throws) out.push({
|
|
1585
|
-
...base,
|
|
1586
|
-
property: "@throws",
|
|
1587
|
-
text: "@throws",
|
|
1588
|
-
kind: "UNKNOWN",
|
|
1589
|
-
cause: "unsupported",
|
|
1590
|
-
reason: "Lemma has not fixed what @throws claims yet"
|
|
1591
|
-
});
|
|
1592
|
-
return out;
|
|
1593
|
-
}
|
|
1594
|
-
// packages/prover/src/regex-gen.ts
|
|
1595
|
-
/**
|
|
1596
|
-
* Strings that match a regular expression, drawn with core's seeded
|
|
1597
|
-
* generator: the pattern is parsed into alternatives, sequences, classes
|
|
1598
|
-
* and quantified terms, and each draw walks that tree. The subset is what
|
|
1599
|
-
* guards use: literals, escapes, classes with ranges and negation, groups,
|
|
1600
|
-
* alternation, the usual quantifiers and anchors. Lookaround,
|
|
1601
|
-
* backreferences and Unicode property escapes are refused with
|
|
1602
|
-
* `UnsupportedRegex`, and the caller falls back to sampling. Every draw is
|
|
1603
|
-
* checked against the real `RegExp` by the caller, so a gap here can only
|
|
1604
|
-
* cost a draw, never admit a value outside the guard.
|
|
1605
|
-
*/
|
|
1606
|
-
var UnsupportedRegex = class extends Error {};
|
|
1607
|
-
const digits$1 = [..."0123456789"];
|
|
1608
|
-
const word = [..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"];
|
|
1609
|
-
const space = [..." \n\r\f\v"];
|
|
1610
|
-
/** What `.` and negated classes draw from: printable ASCII, a few others, no newline. */
|
|
1611
|
-
const anyChar = [..." !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~üß日"];
|
|
1612
|
-
/** A plentiful count for an unbounded quantifier: keeps drawn strings short. */
|
|
1613
|
-
const unboundedExtra = 4;
|
|
1614
|
-
/** The most a counted quantifier may ask for; a guard wanting more is left to sampling. */
|
|
1615
|
-
const maxRepeat = 1e3;
|
|
1616
|
-
/** The longest string one draw may build before it is given up. */
|
|
1617
|
-
const maxLength = 1e4;
|
|
1618
|
-
/** The most nodes one draw may visit: nested repetitions of empty matches multiply work without length. */
|
|
1619
|
-
const maxSteps = 1e5;
|
|
1620
|
-
/** A class node over the characters it can draw; negation is resolved here, once. */
|
|
1621
|
-
function classNode(chars, negated) {
|
|
1622
|
-
const pool = negated ? anyChar.filter((c) => !chars.includes(c)) : chars;
|
|
1623
|
-
if (pool.length === 0) throw new UnsupportedRegex("an empty class matches nothing");
|
|
1624
|
-
return {
|
|
1625
|
-
kind: "class",
|
|
1626
|
-
pool
|
|
1627
|
-
};
|
|
1628
|
-
}
|
|
1629
|
-
var Parser = class {
|
|
1630
|
-
at = 0;
|
|
1631
|
-
src;
|
|
1632
|
-
dotAll;
|
|
1633
|
-
unicode;
|
|
1634
|
-
constructor(src, flags) {
|
|
1635
|
-
this.src = src;
|
|
1636
|
-
this.dotAll = flags.includes("s");
|
|
1637
|
-
this.unicode = flags.includes("u");
|
|
1638
|
-
}
|
|
1639
|
-
parse() {
|
|
1640
|
-
const node = this.alternation();
|
|
1641
|
-
if (this.at < this.src.length) throw new UnsupportedRegex(`unexpected ${this.src[this.at]}`);
|
|
1642
|
-
return node;
|
|
1643
|
-
}
|
|
1644
|
-
peek() {
|
|
1645
|
-
return this.src[this.at];
|
|
1646
|
-
}
|
|
1647
|
-
take() {
|
|
1648
|
-
const c = this.src[this.at++];
|
|
1649
|
-
if (c === void 0) throw new UnsupportedRegex("pattern ends early");
|
|
1650
|
-
return c;
|
|
1651
|
-
}
|
|
1652
|
-
alternation() {
|
|
1653
|
-
const options = [this.sequence()];
|
|
1654
|
-
while (this.peek() === "|") {
|
|
1655
|
-
this.at++;
|
|
1656
|
-
options.push(this.sequence());
|
|
1657
|
-
}
|
|
1658
|
-
return options.length === 1 ? options[0] : {
|
|
1659
|
-
kind: "alt",
|
|
1660
|
-
options
|
|
1661
|
-
};
|
|
1662
|
-
}
|
|
1663
|
-
sequence() {
|
|
1664
|
-
const items = [];
|
|
1665
|
-
while (this.at < this.src.length && this.peek() !== "|" && this.peek() !== ")") {
|
|
1666
|
-
const atom = this.atom();
|
|
1667
|
-
items.push(this.quantified(atom));
|
|
1668
|
-
}
|
|
1669
|
-
return items.length === 1 ? items[0] : {
|
|
1670
|
-
kind: "seq",
|
|
1671
|
-
items
|
|
1672
|
-
};
|
|
1673
|
-
}
|
|
1674
|
-
quantified(atom) {
|
|
1675
|
-
const c = this.peek();
|
|
1676
|
-
let min;
|
|
1677
|
-
let max;
|
|
1678
|
-
if (c === "*") [min, max] = [0, unboundedExtra];
|
|
1679
|
-
else if (c === "+") [min, max] = [1, 5];
|
|
1680
|
-
else if (c === "?") [min, max] = [0, 1];
|
|
1681
|
-
else if (c === "{") {
|
|
1682
|
-
const m = /^\{(\d+)(,(\d*))?\}/.exec(this.src.slice(this.at));
|
|
1683
|
-
if (!m) return atom;
|
|
1684
|
-
min = Number(m[1]);
|
|
1685
|
-
max = m[2] === void 0 ? min : m[3] === "" || m[3] === void 0 ? min + unboundedExtra : Number(m[3]);
|
|
1686
|
-
if (max < min) throw new UnsupportedRegex(`quantifier {${min},${max}} is empty`);
|
|
1687
|
-
if (max > maxRepeat) throw new UnsupportedRegex(`quantifier {${min},${max}} repeats too much to draw`);
|
|
1688
|
-
this.at += m[0].length - 1;
|
|
1689
|
-
} else return atom;
|
|
1690
|
-
this.at++;
|
|
1691
|
-
if (this.peek() === "?") this.at++;
|
|
1692
|
-
return {
|
|
1693
|
-
kind: "repeat",
|
|
1694
|
-
item: atom,
|
|
1695
|
-
min,
|
|
1696
|
-
max
|
|
1697
|
-
};
|
|
1698
|
-
}
|
|
1699
|
-
atom() {
|
|
1700
|
-
const c = this.take();
|
|
1701
|
-
switch (c) {
|
|
1702
|
-
case "(": {
|
|
1703
|
-
if (this.src.startsWith("?:", this.at)) this.at += 2;
|
|
1704
|
-
else if (this.src.startsWith("?<", this.at) && !"=!".includes(this.src[this.at + 2] ?? "")) {
|
|
1705
|
-
const close = this.src.indexOf(">", this.at);
|
|
1706
|
-
if (close < 0) throw new UnsupportedRegex("unterminated group name");
|
|
1707
|
-
this.at = close + 1;
|
|
1708
|
-
} else if (this.peek() === "?") throw new UnsupportedRegex("lookaround or modifier group");
|
|
1709
|
-
const inner = this.alternation();
|
|
1710
|
-
if (this.take() !== ")") throw new UnsupportedRegex("unbalanced parenthesis");
|
|
1711
|
-
return inner;
|
|
1712
|
-
}
|
|
1713
|
-
case "[": return this.charClass();
|
|
1714
|
-
case ".": return classNode(this.dotAll ? [...anyChar, "\n"] : anyChar, false);
|
|
1715
|
-
case "^":
|
|
1716
|
-
case "$": return { kind: "empty" };
|
|
1717
|
-
case "\\": return this.escape(false);
|
|
1718
|
-
case ")":
|
|
1719
|
-
case "|":
|
|
1720
|
-
case "*":
|
|
1721
|
-
case "+":
|
|
1722
|
-
case "?": throw new UnsupportedRegex(`unexpected ${c}`);
|
|
1723
|
-
default: return {
|
|
1724
|
-
kind: "literal",
|
|
1725
|
-
text: c
|
|
1726
|
-
};
|
|
1727
|
-
}
|
|
1728
|
-
}
|
|
1729
|
-
/** An escape after `\`; inside a class, `\b` is a backspace. */
|
|
1730
|
-
escape(inClass) {
|
|
1731
|
-
const e = this.take();
|
|
1732
|
-
switch (e) {
|
|
1733
|
-
case "d": return classNode(digits$1, false);
|
|
1734
|
-
case "D": return classNode(digits$1, true);
|
|
1735
|
-
case "w": return classNode(word, false);
|
|
1736
|
-
case "W": return classNode(word, true);
|
|
1737
|
-
case "s": return classNode(space, false);
|
|
1738
|
-
case "S": return classNode(space, true);
|
|
1739
|
-
case "n": return {
|
|
1740
|
-
kind: "literal",
|
|
1741
|
-
text: "\n"
|
|
1742
|
-
};
|
|
1743
|
-
case "t": return {
|
|
1744
|
-
kind: "literal",
|
|
1745
|
-
text: " "
|
|
1746
|
-
};
|
|
1747
|
-
case "r": return {
|
|
1748
|
-
kind: "literal",
|
|
1749
|
-
text: "\r"
|
|
1750
|
-
};
|
|
1751
|
-
case "f": return {
|
|
1752
|
-
kind: "literal",
|
|
1753
|
-
text: "\f"
|
|
1754
|
-
};
|
|
1755
|
-
case "v": return {
|
|
1756
|
-
kind: "literal",
|
|
1757
|
-
text: "\v"
|
|
1758
|
-
};
|
|
1759
|
-
case "0": return {
|
|
1760
|
-
kind: "literal",
|
|
1761
|
-
text: "\0"
|
|
1762
|
-
};
|
|
1763
|
-
case "b":
|
|
1764
|
-
if (inClass) return {
|
|
1765
|
-
kind: "literal",
|
|
1766
|
-
text: "\b"
|
|
1767
|
-
};
|
|
1768
|
-
return { kind: "empty" };
|
|
1769
|
-
case "B": return { kind: "empty" };
|
|
1770
|
-
case "x": {
|
|
1771
|
-
const hex = this.src.slice(this.at, this.at + 2);
|
|
1772
|
-
if (!/^[0-9a-fA-F]{2}$/.test(hex)) throw new UnsupportedRegex("bad \\x escape");
|
|
1773
|
-
this.at += 2;
|
|
1774
|
-
return {
|
|
1775
|
-
kind: "literal",
|
|
1776
|
-
text: String.fromCharCode(Number.parseInt(hex, 16))
|
|
1777
|
-
};
|
|
1778
|
-
}
|
|
1779
|
-
case "u": {
|
|
1780
|
-
if (this.peek() === "{") {
|
|
1781
|
-
if (!this.unicode) throw new UnsupportedRegex("\\u{} without the u flag");
|
|
1782
|
-
const close = this.src.indexOf("}", this.at);
|
|
1783
|
-
const hex = this.src.slice(this.at + 1, close);
|
|
1784
|
-
if (close < 0 || !/^[0-9a-fA-F]{1,6}$/.test(hex)) throw new UnsupportedRegex("bad \\u escape");
|
|
1785
|
-
this.at = close + 1;
|
|
1786
|
-
return {
|
|
1787
|
-
kind: "literal",
|
|
1788
|
-
text: String.fromCodePoint(Number.parseInt(hex, 16))
|
|
1789
|
-
};
|
|
1790
|
-
}
|
|
1791
|
-
const hex = this.src.slice(this.at, this.at + 4);
|
|
1792
|
-
if (!/^[0-9a-fA-F]{4}$/.test(hex)) throw new UnsupportedRegex("bad \\u escape");
|
|
1793
|
-
this.at += 4;
|
|
1794
|
-
return {
|
|
1795
|
-
kind: "literal",
|
|
1796
|
-
text: String.fromCharCode(Number.parseInt(hex, 16))
|
|
1797
|
-
};
|
|
1798
|
-
}
|
|
1799
|
-
case "p":
|
|
1800
|
-
case "P":
|
|
1801
|
-
case "k":
|
|
1802
|
-
case "c": throw new UnsupportedRegex(`\\${e} escapes`);
|
|
1803
|
-
default:
|
|
1804
|
-
if (/[1-9]/.test(e)) throw new UnsupportedRegex("backreferences");
|
|
1805
|
-
return {
|
|
1806
|
-
kind: "literal",
|
|
1807
|
-
text: e
|
|
1808
|
-
};
|
|
1809
|
-
}
|
|
1810
|
-
}
|
|
1811
|
-
/** `[...]` after its opening bracket. `[]` is empty and `[]a]` is an empty class then `a]`, as in JavaScript. */
|
|
1812
|
-
charClass() {
|
|
1813
|
-
let negated = false;
|
|
1814
|
-
if (this.peek() === "^") {
|
|
1815
|
-
negated = true;
|
|
1816
|
-
this.at++;
|
|
1817
|
-
}
|
|
1818
|
-
const chars = /* @__PURE__ */ new Set();
|
|
1819
|
-
while (this.peek() !== "]") {
|
|
1820
|
-
const c = this.take();
|
|
1821
|
-
let member;
|
|
1822
|
-
if (c === "\\") {
|
|
1823
|
-
const node = this.escape(true);
|
|
1824
|
-
if (node.kind === "class") {
|
|
1825
|
-
for (const x of node.pool) chars.add(x);
|
|
1826
|
-
continue;
|
|
1827
|
-
}
|
|
1828
|
-
if (node.kind !== "literal") throw new UnsupportedRegex("escape in class");
|
|
1829
|
-
member = node.text;
|
|
1830
|
-
} else member = c;
|
|
1831
|
-
if (this.peek() === "-" && this.src[this.at + 1] !== "]" && this.src[this.at + 1] !== void 0) {
|
|
1832
|
-
this.at++;
|
|
1833
|
-
const to = this.take() === "\\" ? this.escape(true) : {
|
|
1834
|
-
kind: "literal",
|
|
1835
|
-
text: this.src[this.at - 1]
|
|
1836
|
-
};
|
|
1837
|
-
if (to.kind !== "literal") throw new UnsupportedRegex("range to a class");
|
|
1838
|
-
const lo = member.codePointAt(0);
|
|
1839
|
-
const hi = to.text.codePointAt(0);
|
|
1840
|
-
if (hi < lo) throw new UnsupportedRegex("range out of order");
|
|
1841
|
-
if (hi - lo > 8192) throw new UnsupportedRegex("range too wide to enumerate");
|
|
1842
|
-
for (let p = lo; p <= hi; p++) chars.add(String.fromCodePoint(p));
|
|
1843
|
-
continue;
|
|
1844
|
-
}
|
|
1845
|
-
chars.add(member);
|
|
1846
|
-
}
|
|
1847
|
-
this.at++;
|
|
1848
|
-
return classNode([...chars], negated);
|
|
1849
|
-
}
|
|
1850
|
-
};
|
|
1851
|
-
/** The pattern as a tree, or `UnsupportedRegex`. */
|
|
1852
|
-
function parseRegex(pattern, flags) {
|
|
1853
|
-
if (flags.includes("v")) throw new UnsupportedRegex("the v flag");
|
|
1854
|
-
return new Parser(pattern, flags).parse();
|
|
1855
|
-
}
|
|
1856
|
-
/** One string from the tree, with the state that follows. */
|
|
1857
|
-
function drawRegex(node, state) {
|
|
1858
|
-
let s = state;
|
|
1859
|
-
let steps = 0;
|
|
1860
|
-
const go = (n) => {
|
|
1861
|
-
if (++steps > maxSteps) throw new UnsupportedRegex("a draw visited more than 100000 nodes");
|
|
1862
|
-
switch (n.kind) {
|
|
1863
|
-
case "empty": return "";
|
|
1864
|
-
case "literal": return n.text;
|
|
1865
|
-
case "alt": {
|
|
1866
|
-
const [i, next] = nextInt(s, n.options.length);
|
|
1867
|
-
s = next;
|
|
1868
|
-
return go(n.options[i]);
|
|
1869
|
-
}
|
|
1870
|
-
case "seq": return n.items.map(go).join("");
|
|
1871
|
-
case "repeat": {
|
|
1872
|
-
const [extra, next] = nextInt(s, n.max - n.min + 1);
|
|
1873
|
-
s = next;
|
|
1874
|
-
let out = "";
|
|
1875
|
-
for (let i = 0; i < n.min + extra; i++) {
|
|
1876
|
-
out += go(n.item);
|
|
1877
|
-
if (out.length > maxLength) throw new UnsupportedRegex("a draw grew past 10000 characters");
|
|
1878
|
-
}
|
|
1879
|
-
return out;
|
|
1880
|
-
}
|
|
1881
|
-
case "class": {
|
|
1882
|
-
const [i, next] = nextInt(s, n.pool.length);
|
|
1883
|
-
s = next;
|
|
1884
|
-
return n.pool[i];
|
|
1885
|
-
}
|
|
1886
|
-
}
|
|
1887
|
-
};
|
|
1888
|
-
return [go(node), s];
|
|
1889
|
-
}
|
|
1890
|
-
// packages/prover/src/generate.ts
|
|
1891
|
-
/**
|
|
1892
|
-
* Inputs for the `TESTED` rung: values drawn from a binder's domain and
|
|
1893
|
-
* guard with core's seeded generator, and the smaller values a shrinker
|
|
1894
|
-
* tries in place of one that broke a property. Everything here is pure:
|
|
1895
|
-
* the same state draws the same value, so a `TESTED` verdict and its
|
|
1896
|
-
* counterexample come out the same on every machine.
|
|
1897
|
-
*/
|
|
1898
|
-
/** Thrown when no value of a binder's domain could be drawn within the guard. */
|
|
1899
|
-
var CannotGenerate = class extends Error {};
|
|
1900
|
-
/** Integers are drawn as doubles, so they stay exact. */
|
|
1901
|
-
const safe = 2n ** 53n - 1n;
|
|
1902
|
-
/** How far an unguarded side of a bigint range reaches. */
|
|
1903
|
-
const bigintReach = 1n << 128n;
|
|
1904
|
-
/**
|
|
1905
|
-
* The closed integer interval a binder admits: within what a double holds
|
|
1906
|
-
* exactly for `int` and `nat`; for `bigint`, the guard's ends, an
|
|
1907
|
-
* unguarded side reaching 2^128 past the other. Undefined when no integer
|
|
1908
|
-
* lies in it, which the parser does not rule out (`int ∈ (0, 1)`).
|
|
1909
|
-
*/
|
|
1910
|
-
function integerRange(b) {
|
|
1911
|
-
const g = b.guard?.kind === "interval" ? b.guard : void 0;
|
|
1912
|
-
const guardLo = g && !g.lower.value.endsWith("∞") ? endpointBigInt(g.lower.value) + (g.lower.inclusive ? 0n : 1n) : void 0;
|
|
1913
|
-
const guardHi = g && !g.upper.value.endsWith("∞") ? endpointBigInt(g.upper.value) - (g.upper.inclusive ? 0n : 1n) : void 0;
|
|
1914
|
-
let lo;
|
|
1915
|
-
let hi;
|
|
1916
|
-
if (b.domain === "bigint") {
|
|
1917
|
-
lo = guardLo ?? (guardHi === void 0 ? -bigintReach : min(-bigintReach, guardHi - bigintReach));
|
|
1918
|
-
hi = guardHi ?? (guardLo === void 0 ? bigintReach : max(bigintReach, guardLo + bigintReach));
|
|
1919
|
-
} else {
|
|
1920
|
-
lo = max(b.domain === "nat" ? 0n : -safe, guardLo ?? -safe);
|
|
1921
|
-
hi = min(safe, guardHi ?? safe);
|
|
1922
|
-
}
|
|
1923
|
-
return lo <= hi ? {
|
|
1924
|
-
lo,
|
|
1925
|
-
hi
|
|
1926
|
-
} : void 0;
|
|
1927
|
-
}
|
|
1928
|
-
const max = (a, b) => a > b ? a : b;
|
|
1929
|
-
const min = (a, b) => a < b ? a : b;
|
|
1930
|
-
/** Uniform in [lo, hi], however wide: enough 32-bit words, masked, rejected when past the span. */
|
|
1931
|
-
function uniformBigInt(lo, hi, state) {
|
|
1932
|
-
const span = hi - lo + 1n;
|
|
1933
|
-
if (span <= 0n) throw new RangeError(`empty integer interval [${lo}, ${hi}]`);
|
|
1934
|
-
if (span === 1n) return [lo, state];
|
|
1935
|
-
let bits = 0;
|
|
1936
|
-
for (let x = span - 1n; x > 0n; x >>= 1n) bits++;
|
|
1937
|
-
const words = Math.ceil(bits / 32);
|
|
1938
|
-
const mask = (1n << BigInt(bits)) - 1n;
|
|
1939
|
-
let s = state;
|
|
1940
|
-
for (;;) {
|
|
1941
|
-
let r = 0n;
|
|
1942
|
-
for (let i = 0; i < words; i++) {
|
|
1943
|
-
const [u, next] = nextU32(s);
|
|
1944
|
-
s = next;
|
|
1945
|
-
r = r << 32n | BigInt(u);
|
|
1946
|
-
}
|
|
1947
|
-
r &= mask;
|
|
1948
|
-
if (r < span) return [lo + r, s];
|
|
1949
|
-
}
|
|
1950
|
-
}
|
|
1951
|
-
/** One of a list, uniformly. */
|
|
1952
|
-
function pick(items, state) {
|
|
1953
|
-
const [i, s] = nextInt(state, items.length);
|
|
1954
|
-
return [items[i], s];
|
|
1955
|
-
}
|
|
1956
|
-
/**
|
|
1957
|
-
* An integer from the binder's range, biased: most draws are small, since
|
|
1958
|
-
* properties fail at boundaries and small cases far more often than deep in
|
|
1959
|
-
* the range, some are medium or large, and a few are the range's edges.
|
|
1960
|
-
*/
|
|
1961
|
-
function drawInteger(b, state) {
|
|
1962
|
-
const range = integerRange(b);
|
|
1963
|
-
if (!range) throw new CannotGenerate(b.domain === "bigint" ? `no integer lies inside the guard of ${b.name}` : `no integer lies inside the guard of ${b.name} within ±2^53, which is as far as draws of ${b.domain} go`);
|
|
1964
|
-
const { lo, hi } = range;
|
|
1965
|
-
const [cls, s] = nextInt(state, 100);
|
|
1966
|
-
const within = (a, z) => {
|
|
1967
|
-
const l = max(lo, a);
|
|
1968
|
-
const h = min(hi, z);
|
|
1969
|
-
return l <= h ? [l, h] : void 0;
|
|
1970
|
-
};
|
|
1971
|
-
if (cls < 5) return pick([
|
|
1972
|
-
lo,
|
|
1973
|
-
hi,
|
|
1974
|
-
0n,
|
|
1975
|
-
1n,
|
|
1976
|
-
-1n,
|
|
1977
|
-
lo + 1n,
|
|
1978
|
-
hi - 1n
|
|
1979
|
-
].filter((v) => v >= lo && v <= hi), s);
|
|
1980
|
-
const [l, h] = (cls < 55 ? within(-16n, 16n) : cls < 80 ? within(-(1n << 16n), 1n << 16n) : cls < 95 ? within(-(1n << 31n), 1n << 31n) : void 0) ?? [lo, hi];
|
|
1981
|
-
return uniformBigInt(l, h, s);
|
|
1982
|
-
}
|
|
1983
|
-
/** The double interval a `number` binder admits. */
|
|
1984
|
-
function numberRange(b) {
|
|
1985
|
-
const g = b.guard;
|
|
1986
|
-
if (g?.kind !== "interval") return {
|
|
1987
|
-
lo: Number.NEGATIVE_INFINITY,
|
|
1988
|
-
hi: Number.POSITIVE_INFINITY,
|
|
1989
|
-
guarded: false
|
|
1990
|
-
};
|
|
1991
|
-
return {
|
|
1992
|
-
lo: endpointNumber(g.lower.value),
|
|
1993
|
-
hi: endpointNumber(g.upper.value),
|
|
1994
|
-
guarded: true
|
|
1995
|
-
};
|
|
1996
|
-
}
|
|
1997
|
-
function drawNumber(b, state) {
|
|
1998
|
-
const { lo, hi, guarded } = numberRange(b);
|
|
1999
|
-
const [cls, s0] = nextInt(state, 100);
|
|
2000
|
-
if (cls < 8) {
|
|
2001
|
-
const edges = [
|
|
2002
|
-
0,
|
|
2003
|
-
-0,
|
|
2004
|
-
1,
|
|
2005
|
-
-1,
|
|
2006
|
-
.5,
|
|
2007
|
-
Number.MIN_VALUE,
|
|
2008
|
-
Number.EPSILON,
|
|
2009
|
-
Number.MAX_VALUE,
|
|
2010
|
-
-Number.MAX_VALUE,
|
|
2011
|
-
Number.POSITIVE_INFINITY,
|
|
2012
|
-
Number.NEGATIVE_INFINITY,
|
|
2013
|
-
...guarded ? [lo, hi] : [NaN]
|
|
2014
|
-
].filter((v) => inDomain(b, v));
|
|
2015
|
-
if (edges.length > 0) return pick(edges, s0);
|
|
2016
|
-
}
|
|
2017
|
-
if (cls < 35) {
|
|
2018
|
-
const [k, s1] = nextInt(s0, 33);
|
|
2019
|
-
return [k - 16, s1];
|
|
2020
|
-
}
|
|
2021
|
-
if (cls < 60) {
|
|
2022
|
-
const [d, s1] = pick([
|
|
2023
|
-
2,
|
|
2024
|
-
4,
|
|
2025
|
-
8,
|
|
2026
|
-
10,
|
|
2027
|
-
100
|
|
2028
|
-
], s0);
|
|
2029
|
-
const [k, s2] = nextInt(s1, 65);
|
|
2030
|
-
return [(k - 32) / d, s2];
|
|
2031
|
-
}
|
|
2032
|
-
if (cls < 80 && Number.isFinite(lo) && Number.isFinite(hi)) {
|
|
2033
|
-
const [f, s1] = nextFloat(s0);
|
|
2034
|
-
return [lo + f * (hi - lo), s1];
|
|
2035
|
-
}
|
|
2036
|
-
const [f, s1] = nextFloat(s0);
|
|
2037
|
-
const [e, s2] = nextInt(s1, 2100);
|
|
2038
|
-
const [neg, s3] = nextInt(s2, 2);
|
|
2039
|
-
return [(neg ? -1 : 1) * (1 + f) * 2 ** (e - 1074), s3];
|
|
2040
|
-
}
|
|
2041
|
-
const letters = "abcdefghijklmnopqrstuvwxyz";
|
|
2042
|
-
const digits = "0123456789";
|
|
2043
|
-
const plainAlphabet = [...`${letters}${letters.toUpperCase()}${digits} _-.,:/\n'"üß日😀`];
|
|
2044
|
-
const stringEdges = [
|
|
2045
|
-
"",
|
|
2046
|
-
" ",
|
|
2047
|
-
"a",
|
|
2048
|
-
"0",
|
|
2049
|
-
"-1",
|
|
2050
|
-
"\n",
|
|
2051
|
-
"\0",
|
|
2052
|
-
"ü",
|
|
2053
|
-
"😀",
|
|
2054
|
-
"a".repeat(100)
|
|
2055
|
-
];
|
|
2056
|
-
/**
|
|
2057
|
-
* The characters a regex names: literals, the ranges and escapes of its
|
|
2058
|
-
* classes. Strings drawn from them match the pattern far more often than
|
|
2059
|
-
* strings drawn from everything, which is what the rejection below needs.
|
|
2060
|
-
*/
|
|
2061
|
-
function regexAlphabet(pattern) {
|
|
2062
|
-
const chars = /* @__PURE__ */ new Set();
|
|
2063
|
-
const add = (s) => {
|
|
2064
|
-
for (const c of s) chars.add(c);
|
|
2065
|
-
};
|
|
2066
|
-
for (let i = 0; i < pattern.length; i++) {
|
|
2067
|
-
const c = pattern[i];
|
|
2068
|
-
if (c === "\\") {
|
|
2069
|
-
const e = pattern[++i];
|
|
2070
|
-
if (e === "d") add(digits);
|
|
2071
|
-
else if (e === "w") add(`${letters}${letters.toUpperCase()}${digits}_`);
|
|
2072
|
-
else if (e === "s") add(" ");
|
|
2073
|
-
else if (e === "n") add("\n");
|
|
2074
|
-
else if (e === "t") add(" ");
|
|
2075
|
-
else if (e !== void 0 && !/[DWSbBuxpP]/.test(e)) add(e);
|
|
2076
|
-
continue;
|
|
2077
|
-
}
|
|
2078
|
-
if (pattern[i + 1] === "-" && i + 2 < pattern.length && pattern[i + 2] !== "]") {
|
|
2079
|
-
const from = c.codePointAt(0);
|
|
2080
|
-
const to = pattern[i + 2].codePointAt(0);
|
|
2081
|
-
if (from < to && to - from <= 4096) for (let p = from; p <= to; p++) chars.add(String.fromCodePoint(p));
|
|
2082
|
-
i += 2;
|
|
2083
|
-
continue;
|
|
2084
|
-
}
|
|
2085
|
-
if (!"^$()[]{}|*+?.-".includes(c)) chars.add(c);
|
|
2086
|
-
}
|
|
2087
|
-
return [...chars];
|
|
2088
|
-
}
|
|
2089
|
-
/** A string of `length` characters from `alphabet`. */
|
|
2090
|
-
function drawChars(alphabet, length, state) {
|
|
2091
|
-
let s = state;
|
|
2092
|
-
let out = "";
|
|
2093
|
-
for (let i = 0; i < length; i++) {
|
|
2094
|
-
const [c, next] = pick(alphabet, s);
|
|
2095
|
-
s = next;
|
|
2096
|
-
out += c;
|
|
2097
|
-
}
|
|
2098
|
-
return [out, s];
|
|
2099
|
-
}
|
|
2100
|
-
function drawString(b, state) {
|
|
2101
|
-
const g = b.guard;
|
|
2102
|
-
if (g?.kind !== "regex") {
|
|
2103
|
-
const [cls, s0] = nextInt(state, 100);
|
|
2104
|
-
if (cls < 15) return pick(stringEdges, s0);
|
|
2105
|
-
const [length, s1] = nextInt(s0, 25);
|
|
2106
|
-
return drawChars(plainAlphabet, length, s1);
|
|
2107
|
-
}
|
|
2108
|
-
let s = state;
|
|
2109
|
-
let tree;
|
|
2110
|
-
try {
|
|
2111
|
-
tree = parseRegex(g.pattern, g.flags);
|
|
2112
|
-
} catch (err) {
|
|
2113
|
-
if (!(err instanceof UnsupportedRegex)) throw err;
|
|
2114
|
-
}
|
|
2115
|
-
for (let attempt = 0; tree && attempt < 16; attempt++) try {
|
|
2116
|
-
const [candidate, s1] = drawRegex(tree, s);
|
|
2117
|
-
s = s1;
|
|
2118
|
-
if (inDomain(b, candidate)) return [candidate, s];
|
|
2119
|
-
} catch (err) {
|
|
2120
|
-
if (!(err instanceof UnsupportedRegex)) throw err;
|
|
2121
|
-
break;
|
|
2122
|
-
}
|
|
2123
|
-
const fromPattern = regexAlphabet(g.pattern);
|
|
2124
|
-
const alphabet = fromPattern.length > 0 ? fromPattern : plainAlphabet;
|
|
2125
|
-
for (let attempt = 0; attempt < 64; attempt++) {
|
|
2126
|
-
const [length, s1] = nextInt(s, 13);
|
|
2127
|
-
const [candidate, s2] = drawChars(alphabet, length, s1);
|
|
2128
|
-
s = s2;
|
|
2129
|
-
if (inDomain(b, candidate)) return [candidate, s];
|
|
2130
|
-
}
|
|
2131
|
-
throw new CannotGenerate(`no string matching /${g.pattern}/${g.flags} was found in 64 draws over its characters`);
|
|
2132
|
-
}
|
|
2133
|
-
/** A value of the binder's domain within its guard. */
|
|
2134
|
-
function generate(b, state) {
|
|
2135
|
-
switch (b.domain) {
|
|
2136
|
-
case "boolean": {
|
|
2137
|
-
const [bit, s] = nextInt(state, 2);
|
|
2138
|
-
return [bit === 1, s];
|
|
2139
|
-
}
|
|
2140
|
-
case "int":
|
|
2141
|
-
case "nat": {
|
|
2142
|
-
const [v, s] = drawInteger(b, state);
|
|
2143
|
-
return [Number(v), s];
|
|
2144
|
-
}
|
|
2145
|
-
case "bigint": return drawInteger(b, state);
|
|
2146
|
-
case "number": {
|
|
2147
|
-
let s = state;
|
|
2148
|
-
for (let attempt = 0; attempt < 64; attempt++) {
|
|
2149
|
-
const [v, next] = drawNumber(b, s);
|
|
2150
|
-
s = next;
|
|
2151
|
-
if (inDomain(b, v)) return [v, s];
|
|
2152
|
-
}
|
|
2153
|
-
throw new CannotGenerate(`no number inside the guard of ${b.name} was found in 64 draws`);
|
|
2154
|
-
}
|
|
2155
|
-
case "string": return drawString(b, state);
|
|
2156
|
-
}
|
|
2157
|
-
}
|
|
2158
|
-
/** The integer nearest to zero in [lo, hi]. */
|
|
2159
|
-
function nearestZero(lo, hi) {
|
|
2160
|
-
return lo > 0n ? lo : hi < 0n ? hi : 0n;
|
|
2161
|
-
}
|
|
2162
|
-
/** Steps from `v` toward `target`: the target itself, then the halfway points back toward `v`. */
|
|
2163
|
-
function towards(v, target) {
|
|
2164
|
-
const out = [];
|
|
2165
|
-
if (v === target) return out;
|
|
2166
|
-
out.push(target);
|
|
2167
|
-
let d = (v - target) / 2n;
|
|
2168
|
-
while (d !== 0n) {
|
|
2169
|
-
out.push(v - d);
|
|
2170
|
-
d /= 2n;
|
|
2171
|
-
}
|
|
2172
|
-
return out;
|
|
2173
|
-
}
|
|
2174
|
-
/**
|
|
2175
|
-
* Smaller values to try in place of `v`, simplest first, all within the
|
|
2176
|
-
* binder's domain and guard and none equal to `v`. Numbers move toward
|
|
2177
|
-
* zero (or the guard's end nearest it), strings get shorter and plainer,
|
|
2178
|
-
* `true` becomes `false`.
|
|
2179
|
-
*/
|
|
2180
|
-
function shrinkCandidates(b, v) {
|
|
2181
|
-
const keep = (xs) => {
|
|
2182
|
-
const seen = /* @__PURE__ */ new Set();
|
|
2183
|
-
return xs.filter((x) => {
|
|
2184
|
-
if (Object.is(x, v) || seen.has(x) || !inDomain(b, x)) return false;
|
|
2185
|
-
seen.add(x);
|
|
2186
|
-
return true;
|
|
2187
|
-
});
|
|
2188
|
-
};
|
|
2189
|
-
switch (b.domain) {
|
|
2190
|
-
case "boolean": return keep([false]);
|
|
2191
|
-
case "int":
|
|
2192
|
-
case "nat": {
|
|
2193
|
-
const range = integerRange(b);
|
|
2194
|
-
if (typeof v !== "number" || !Number.isInteger(v) || !range) return [];
|
|
2195
|
-
return keep(towards(BigInt(v), nearestZero(range.lo, range.hi)).map(Number));
|
|
2196
|
-
}
|
|
2197
|
-
case "bigint": {
|
|
2198
|
-
const range = integerRange(b);
|
|
2199
|
-
if (typeof v !== "bigint" || !range) return [];
|
|
2200
|
-
return keep(towards(v, nearestZero(range.lo, range.hi)));
|
|
2201
|
-
}
|
|
2202
|
-
case "number": {
|
|
2203
|
-
if (typeof v !== "number") return [];
|
|
2204
|
-
const { lo, hi } = numberRange(b);
|
|
2205
|
-
const target = lo > 0 ? lo : Math.min(hi, 0);
|
|
2206
|
-
if (!Number.isFinite(v)) return keep([
|
|
2207
|
-
target,
|
|
2208
|
-
1,
|
|
2209
|
-
-1,
|
|
2210
|
-
Number.MAX_VALUE,
|
|
2211
|
-
-Number.MAX_VALUE
|
|
2212
|
-
]);
|
|
2213
|
-
const out = [target, Math.trunc(v)];
|
|
2214
|
-
let d = (v - target) / 2;
|
|
2215
|
-
for (let i = 0; i < 24 && d !== 0; i++) {
|
|
2216
|
-
out.push(v - d);
|
|
2217
|
-
d /= 2;
|
|
2218
|
-
}
|
|
2219
|
-
return keep(out);
|
|
2220
|
-
}
|
|
2221
|
-
case "string": {
|
|
2222
|
-
if (typeof v !== "string") return [];
|
|
2223
|
-
const chars = [...v];
|
|
2224
|
-
const out = ["", chars.slice(0, Math.floor(chars.length / 2)).join("")];
|
|
2225
|
-
out.push(chars.slice(Math.ceil(chars.length / 2)).join(""));
|
|
2226
|
-
for (const at of [
|
|
2227
|
-
0,
|
|
2228
|
-
chars.length - 1,
|
|
2229
|
-
Math.floor(chars.length / 2)
|
|
2230
|
-
]) out.push([...chars.slice(0, at), ...chars.slice(at + 1)].join(""));
|
|
2231
|
-
const first = chars.findIndex((c) => c !== "a");
|
|
2232
|
-
if (first >= 0) out.push([
|
|
2233
|
-
...chars.slice(0, first),
|
|
2234
|
-
"a",
|
|
2235
|
-
...chars.slice(first + 1)
|
|
2236
|
-
].join(""));
|
|
2237
|
-
return keep(out);
|
|
2238
|
-
}
|
|
2239
|
-
}
|
|
2240
|
-
}
|
|
2241
|
-
// packages/prover/src/tested.ts
|
|
2242
|
-
/**
|
|
2243
|
-
* The `TESTED` rung: a property evaluated on assignments drawn from its
|
|
2244
|
-
* binders' domains and guards, with a seed, so the same seed draws the same
|
|
2245
|
-
* assignments everywhere. An assignment where the claim is false is shrunk
|
|
2246
|
-
* toward smaller values that still break it and reported as a
|
|
2247
|
-
* counterexample. Holding on every drawn assignment is `TESTED`: evidence
|
|
2248
|
-
* from sampling, which says nothing about the inputs never drawn.
|
|
2249
|
-
*/
|
|
2250
|
-
/** How many evaluations shrinking may spend on one counterexample. */
|
|
2251
|
-
const shrinkBudget = 500;
|
|
2252
|
-
function testedRung(input) {
|
|
2253
|
-
const { module, fn, property } = input;
|
|
2254
|
-
const base = {
|
|
2255
|
-
module,
|
|
2256
|
-
function: fn.name,
|
|
2257
|
-
property: property.name,
|
|
2258
|
-
text: property.text
|
|
2259
|
-
};
|
|
2260
|
-
const unknown = (cause, reason) => ({
|
|
2261
|
-
...base,
|
|
2262
|
-
kind: "UNKNOWN",
|
|
2263
|
-
cause,
|
|
2264
|
-
reason,
|
|
2265
|
-
assumptions: []
|
|
2266
|
-
});
|
|
2267
|
-
const predicate = input.predicate;
|
|
2268
|
-
if (!predicate) return unknown("unavailable", "the property's predicate was not compiled");
|
|
2269
|
-
const binders = property.property.binders;
|
|
2270
|
-
const wanted = input.inputs ?? 100;
|
|
2271
|
-
if (!Number.isInteger(wanted) || wanted < 1) throw new RangeError(`inputs must be a positive integer, got ${String(input.inputs)}`);
|
|
2272
|
-
const seed = `${input.seed}:${module}:${fn.name}:${property.name}`;
|
|
2273
|
-
let state = seedPrng(seed);
|
|
2274
|
-
let held = 0;
|
|
2275
|
-
let discarded = 0;
|
|
2276
|
-
const seen = /* @__PURE__ */ new Set();
|
|
2277
|
-
const counterexample = (values, error) => ({
|
|
2278
|
-
binders: binders.map((b, i) => ({
|
|
2279
|
-
name: b.name,
|
|
2280
|
-
value: render(values[i])
|
|
2281
|
-
})),
|
|
2282
|
-
...error === void 0 ? {} : { error }
|
|
2283
|
-
});
|
|
2284
|
-
for (let draws = 0; held < wanted && draws < wanted * 20; draws++) {
|
|
2285
|
-
const values = [];
|
|
2286
|
-
try {
|
|
2287
|
-
for (const b of binders) {
|
|
2288
|
-
const [v, next] = generate(b, state);
|
|
2289
|
-
state = next;
|
|
2290
|
-
values.push(v);
|
|
2291
|
-
}
|
|
2292
|
-
} catch (err) {
|
|
2293
|
-
if (err instanceof CannotGenerate) return unknown("gave_up", err.message);
|
|
2294
|
-
throw err;
|
|
2295
|
-
}
|
|
2296
|
-
const key = values.map(render).join(", ");
|
|
2297
|
-
if (seen.has(key)) continue;
|
|
2298
|
-
seen.add(key);
|
|
2299
|
-
const result = evaluate(predicate, values);
|
|
2300
|
-
if ("malformed" in result) return unknown("unsupported", malformedReason(result.malformed));
|
|
2301
|
-
if (result.outcome === "discarded") {
|
|
2302
|
-
discarded++;
|
|
2303
|
-
continue;
|
|
2304
|
-
}
|
|
2305
|
-
if (result.outcome === "refuted") {
|
|
2306
|
-
const smallest = shrink(predicate, binders, values, result);
|
|
2307
|
-
return {
|
|
2308
|
-
...base,
|
|
2309
|
-
kind: "REFUTED",
|
|
2310
|
-
rung: "TESTED",
|
|
2311
|
-
counterexample: counterexample(smallest.values, smallest.error),
|
|
2312
|
-
assumptions: [`found on an input generated with seed ${input.seed}, then shrunk`]
|
|
2313
|
-
};
|
|
2314
|
-
}
|
|
2315
|
-
held++;
|
|
2316
|
-
}
|
|
2317
|
-
if (held === 0) return unknown("gave_up", `all ${discarded} distinct generated inputs were outside the claim: its preconditions admit too few of the binders' domains to sample`);
|
|
2318
|
-
return {
|
|
2319
|
-
...base,
|
|
2320
|
-
kind: "TESTED",
|
|
2321
|
-
inputs: held,
|
|
2322
|
-
discarded,
|
|
2323
|
-
seed: input.seed,
|
|
2324
|
-
assumptions: [`held on ${held} distinct input(s) generated from the binders' domains with seed ${input.seed}; inputs never drawn are not covered`, `${fn.name} was called on generated inputs outside the simulator, so it is assumed pure`]
|
|
2325
|
-
};
|
|
2326
|
-
}
|
|
2327
|
-
/** The refuting assignment made as small as the candidates allow, one binder at a time. */
|
|
2328
|
-
function shrink(predicate, binders, start, startEvaluation) {
|
|
2329
|
-
let values = [...start];
|
|
2330
|
-
let error = refutes(startEvaluation) ?? {};
|
|
2331
|
-
let spent = 0;
|
|
2332
|
-
let progress = true;
|
|
2333
|
-
while (progress && spent < shrinkBudget) {
|
|
2334
|
-
progress = false;
|
|
2335
|
-
for (let i = 0; i < binders.length && spent < shrinkBudget; i++) {
|
|
2336
|
-
const b = binders[i];
|
|
2337
|
-
let moved = true;
|
|
2338
|
-
while (moved && spent < shrinkBudget) {
|
|
2339
|
-
moved = false;
|
|
2340
|
-
for (const candidate of shrinkCandidates(b, values[i])) {
|
|
2341
|
-
if (spent >= shrinkBudget) break;
|
|
2342
|
-
spent++;
|
|
2343
|
-
const trial = values.with(i, candidate);
|
|
2344
|
-
const still = refutes(evaluate(predicate, trial));
|
|
2345
|
-
if (still === void 0) continue;
|
|
2346
|
-
values = trial;
|
|
2347
|
-
error = still;
|
|
2348
|
-
moved = true;
|
|
2349
|
-
progress = true;
|
|
2350
|
-
break;
|
|
2351
|
-
}
|
|
2352
|
-
}
|
|
2353
|
-
}
|
|
2354
|
-
}
|
|
2355
|
-
return error.error === void 0 ? { values } : {
|
|
2356
|
-
values,
|
|
2357
|
-
error: error.error
|
|
2358
|
-
};
|
|
2359
|
-
}
|
|
2360
|
-
/** Whether an evaluation still refutes: an object (holding the error text, if any), or undefined. */
|
|
2361
|
-
function refutes(e) {
|
|
2362
|
-
if ("malformed" in e || e.outcome !== "refuted") return void 0;
|
|
2363
|
-
return "error" in e ? { error: e.error } : {};
|
|
2364
|
-
}
|
|
2365
|
-
// packages/prover/src/ladder.ts
|
|
2366
|
-
/** One property through the ladder's rungs. */
|
|
2367
|
-
function specLadder(input, options = {}) {
|
|
2368
|
-
const checked = checkedRung(input);
|
|
2369
|
-
if (checked.kind === "REFUTED" || options.tested === false || options.tested === void 0) return checked;
|
|
2370
|
-
if (checked.kind === "UNKNOWN" && checked.reason.startsWith(malformedReason(""))) return checked;
|
|
2371
|
-
const tested = testedRung({
|
|
2372
|
-
module: input.module,
|
|
2373
|
-
fn: input.fn,
|
|
2374
|
-
property: input.property,
|
|
2375
|
-
predicate: input.predicate,
|
|
2376
|
-
...options.tested
|
|
2377
|
-
});
|
|
2378
|
-
if (tested.kind === "REFUTED") return tested;
|
|
2379
|
-
if (tested.kind === "UNKNOWN" && tested.reason.startsWith(malformedReason(""))) return tested;
|
|
2380
|
-
if (tested.kind === "TESTED") {
|
|
2381
|
-
if (checked.kind !== "CHECKED") return {
|
|
2382
|
-
...tested,
|
|
2383
|
-
assumptions: [...tested.assumptions, reason(checked)]
|
|
2384
|
-
};
|
|
2385
|
-
return {
|
|
2386
|
-
...tested,
|
|
2387
|
-
checked: checked.inputs,
|
|
2388
|
-
assumptions: [...tested.assumptions, ...checked.assumptions]
|
|
2389
|
-
};
|
|
2390
|
-
}
|
|
2391
|
-
if (checked.kind !== "UNKNOWN") return {
|
|
2392
|
-
...checked,
|
|
2393
|
-
assumptions: [...checked.assumptions, `not TESTED: ${reason(tested)}`]
|
|
2394
|
-
};
|
|
2395
|
-
if (reason(tested) === checked.reason) return checked;
|
|
2396
|
-
return {
|
|
2397
|
-
...checked,
|
|
2398
|
-
reason: `${checked.reason}; generated inputs: ${reason(tested)}`
|
|
2399
|
-
};
|
|
2400
|
-
}
|
|
2401
|
-
function reason(v) {
|
|
2402
|
-
return v.kind === "UNKNOWN" ? v.reason : v.kind;
|
|
2403
|
-
}
|
|
2404
|
-
/** Every spec verdict for one module: each property through the ladder, the markers as the rungs leave them. */
|
|
2405
|
-
function checkSpecModule(module, specs, predicates, observed, options = {}) {
|
|
2406
|
-
return specs.functions.flatMap((fn) => {
|
|
2407
|
-
const calls = observed.calls.filter((c) => c.function === fn.name);
|
|
2408
|
-
const unobservable = Object.hasOwn(observed.unobservable, fn.name) ? observed.unobservable[fn.name] : void 0;
|
|
2409
|
-
const own = Object.hasOwn(predicates, fn.name) ? predicates[fn.name] : void 0;
|
|
2410
|
-
return [...fn.ensures.map((property) => specLadder({
|
|
2411
|
-
module,
|
|
2412
|
-
fn,
|
|
2413
|
-
property,
|
|
2414
|
-
calls,
|
|
2415
|
-
predicate: own && Object.hasOwn(own, property.name) ? own[property.name] : void 0,
|
|
2416
|
-
...unobservable === void 0 ? {} : { unobservable }
|
|
2417
|
-
}, options)), ...markerVerdicts(module, fn)];
|
|
2418
|
-
});
|
|
2419
|
-
}
|
|
2420
|
-
// packages/prover/src/provers.ts
|
|
2421
|
-
/**
|
|
2422
|
-
* The simulator's verdicts for a module (CHECKED, TESTED, REFUTED, UNKNOWN,
|
|
2423
|
-
* markers included) combined with what the engines proved. A simulator
|
|
2424
|
-
* counterexample stands. One engine proving is `PROVEN`, the lower rungs'
|
|
2425
|
-
* counts kept beside it; an engine refuting where nothing else did is
|
|
2426
|
-
* `REFUTED` at the PROVEN rung; one proving and another refuting is
|
|
2427
|
-
* `CONFLICT`. An engine that gave up, could not run or does not take the
|
|
2428
|
-
* function leaves the verdict as it was, with why among the assumptions.
|
|
2429
|
-
* `@total` is proved by acceptance where the engine says so.
|
|
2430
|
-
*/
|
|
2431
|
-
function aggregate(module, verdicts, reports) {
|
|
2432
|
-
return verdicts.map((v) => {
|
|
2433
|
-
if (v.module !== module) return v;
|
|
2434
|
-
const says = reports.map((r) => ({
|
|
2435
|
-
engine: r.engine,
|
|
2436
|
-
outcome: r.proofs.results.find((p) => p.function === v.function && p.property === v.property)?.outcome
|
|
2437
|
-
})).filter((x) => x.outcome !== void 0);
|
|
2438
|
-
if (says.length === 0) return v;
|
|
2439
|
-
const proven = says.find((x) => x.outcome.kind === "PROVEN");
|
|
2440
|
-
const refuted = says.find((x) => x.outcome.kind === "REFUTED");
|
|
2441
|
-
if (v.kind === "REFUTED" || v.kind === "CONFLICT") {
|
|
2442
|
-
if (v.kind === "REFUTED" && proven?.outcome.kind === "PROVEN") return {
|
|
2443
|
-
module: v.module,
|
|
2444
|
-
function: v.function,
|
|
2445
|
-
property: v.property,
|
|
2446
|
-
text: v.text,
|
|
2447
|
-
kind: "CONFLICT",
|
|
2448
|
-
proven: proven.outcome.engine,
|
|
2449
|
-
refuted: {
|
|
2450
|
-
name: "simulator",
|
|
2451
|
-
version: v.rung,
|
|
2452
|
-
counterexample: v.counterexample
|
|
2453
|
-
},
|
|
2454
|
-
assumptions: [...v.assumptions, ...proven.outcome.assumptions]
|
|
2455
|
-
};
|
|
2456
|
-
return v;
|
|
2457
|
-
}
|
|
2458
|
-
if (proven?.outcome.kind === "PROVEN" && refuted?.outcome.kind === "REFUTED") return {
|
|
2459
|
-
module: v.module,
|
|
2460
|
-
function: v.function,
|
|
2461
|
-
property: v.property,
|
|
2462
|
-
text: v.text,
|
|
2463
|
-
kind: "CONFLICT",
|
|
2464
|
-
proven: proven.outcome.engine,
|
|
2465
|
-
refuted: {
|
|
2466
|
-
name: refuted.outcome.engine.name,
|
|
2467
|
-
version: refuted.outcome.engine.version,
|
|
2468
|
-
...refuted.outcome.counterexample ? { counterexample: refuted.outcome.counterexample } : {}
|
|
2469
|
-
},
|
|
2470
|
-
assumptions: [...proven.outcome.assumptions, ...refuted.outcome.assumptions]
|
|
2471
|
-
};
|
|
2472
|
-
if (refuted?.outcome.kind === "REFUTED") return {
|
|
2473
|
-
module: v.module,
|
|
2474
|
-
function: v.function,
|
|
2475
|
-
property: v.property,
|
|
2476
|
-
text: v.text,
|
|
2477
|
-
kind: "REFUTED",
|
|
2478
|
-
rung: "PROVEN",
|
|
2479
|
-
engine: refuted.outcome.engine,
|
|
2480
|
-
counterexample: refuted.outcome.counterexample ?? { binders: [] },
|
|
2481
|
-
assumptions: refuted.outcome.assumptions
|
|
2482
|
-
};
|
|
2483
|
-
if (proven?.outcome.kind === "PROVEN") return {
|
|
2484
|
-
module: v.module,
|
|
2485
|
-
function: v.function,
|
|
2486
|
-
property: v.property,
|
|
2487
|
-
text: v.text,
|
|
2488
|
-
kind: "PROVEN",
|
|
2489
|
-
engine: proven.outcome.engine,
|
|
2490
|
-
...v.kind === "TESTED" ? { tested: v.inputs } : {},
|
|
2491
|
-
...v.kind === "TESTED" && v.checked !== void 0 ? { checked: v.checked } : v.kind === "CHECKED" ? { checked: v.inputs } : {},
|
|
2492
|
-
assumptions: [...proven.outcome.assumptions, ...v.assumptions]
|
|
2493
|
-
};
|
|
2494
|
-
const notes = says.map((x) => x.outcome.kind === "UNKNOWN" ? `not PROVEN by ${x.engine}: ${x.outcome.cause}: ${x.outcome.reason}` : `${x.engine}: ${x.outcome.kind}`);
|
|
2495
|
-
return {
|
|
2496
|
-
...v,
|
|
2497
|
-
assumptions: [...v.assumptions, ...notes]
|
|
2498
|
-
};
|
|
2499
|
-
});
|
|
2500
|
-
}
|
|
2501
|
-
// packages/report/src/regression.ts
|
|
2502
|
-
/**
|
|
2503
|
-
* The Vitest regression emitter. Given a violation, produce a test file that
|
|
2504
|
-
* replays its shrunk schedule on the restricted scenario. By default the
|
|
2505
|
-
* test expects the invariants to hold, so it fails while the bug exists and
|
|
2506
|
-
* passes once it is fixed. With `expect: "violates"` it asserts the recorded
|
|
2507
|
-
* violation instead, which is how Eutaxy's own corpus exercises the emitter.
|
|
2508
|
-
*
|
|
2509
|
-
* The emitter knows nothing about any platform: the caller supplies the
|
|
2510
|
-
* import lines and the expression that builds the platform.
|
|
2511
|
-
*/
|
|
2512
|
-
function emitRegressionTest(verdict, options) {
|
|
2513
|
-
const engineModule = options.engineModule ?? "eutaxy";
|
|
2514
|
-
const mode = options.expect ?? "holds";
|
|
2515
|
-
const schedule = canonicalJson(verdict.schedule);
|
|
2516
|
-
const kept = canonicalJson([...verdict.keptEvents]);
|
|
2517
|
-
const keys = canonicalJson([...verdict.violation.keys]);
|
|
2518
|
-
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`;
|
|
2519
|
-
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}]);`;
|
|
2520
|
-
const observe = options.observe ?? [];
|
|
2521
|
-
const replayLines = observe.length === 0 ? [" const log = await replay(spec, platform, schedule);"] : [
|
|
2522
|
-
` const observers = [${observe.map((o) => `observeCore(${o.namespace}, ${JSON.stringify(o.functions)})`).join(", ")}];`,
|
|
2523
|
-
" const log = await replay(spec, platform, schedule).finally(() => {",
|
|
2524
|
-
" for (const o of observers) o.restore();",
|
|
2525
|
-
" });"
|
|
2526
|
-
];
|
|
2527
|
-
return [
|
|
2528
|
-
"// Generated by Eutaxy. Replays a recorded violation; regenerate rather than edit.",
|
|
2529
|
-
`// Scenario ${verdict.scenario}, seed ${String(verdict.seed)}: ${verdict.violation.message}`,
|
|
2530
|
-
`import { checkInvariants, ${observe.length > 0 ? "observeCore, " : ""}replay, restrictSpec } from ${JSON.stringify(engineModule)};`,
|
|
2531
|
-
...options.imports,
|
|
2532
|
-
"import { expect, test } from \"vitest\";",
|
|
2533
|
-
"",
|
|
2534
|
-
`const schedule = ${schedule} as const;`,
|
|
2535
|
-
`const keptEvents = ${kept} as const;`,
|
|
2536
|
-
"",
|
|
2537
|
-
`test(${JSON.stringify(title)}, async () => {`,
|
|
2538
|
-
` const platform = ${options.platform};`,
|
|
2539
|
-
` const spec = restrictSpec(platform, ${options.scenario}, keptEvents);`,
|
|
2540
|
-
...replayLines,
|
|
2541
|
-
" const violations = checkInvariants(log, spec.invariants);",
|
|
2542
|
-
assertion,
|
|
2543
|
-
"});",
|
|
2544
|
-
""
|
|
2545
|
-
].join("\n");
|
|
2546
|
-
}
|
|
2547
|
-
/**
|
|
2548
|
-
* The JSON report: one object, canonical, with a schema version at the top.
|
|
2549
|
-
* Spec verdicts, when there are any, sit beside the scenario's, never in it.
|
|
2550
|
-
*/
|
|
2551
|
-
function renderJson(verdict, specs = []) {
|
|
2552
|
-
return canonicalJson({
|
|
2553
|
-
schemaVersion: 1,
|
|
2554
|
-
generator: "eutaxy",
|
|
2555
|
-
verdict,
|
|
2556
|
-
...specs.length > 0 ? { specs } : {}
|
|
2557
|
-
});
|
|
2558
|
-
}
|
|
2559
|
-
/** Spec verdicts as text: one line each, a counterexample under a refutation. */
|
|
2560
|
-
function renderSpecs(specs) {
|
|
2561
|
-
if (specs.length === 0) return "";
|
|
2562
|
-
const out = ["Specs (CHECKED means held on the inputs the simulator passed, TESTED on generated ones too; neither is a proof. PROVEN is one, by the named engine)"];
|
|
2563
|
-
for (const v of specs) {
|
|
2564
|
-
const what = `${v.function} ${v.property}`;
|
|
2565
|
-
switch (v.kind) {
|
|
2566
|
-
case "CHECKED":
|
|
2567
|
-
out.push(` CHECKED ${what}: held on ${v.inputs} input(s)${v.discarded > 0 ? `, ${v.discarded} outside the claim` : ""}`);
|
|
2568
|
-
break;
|
|
2569
|
-
case "TESTED":
|
|
2570
|
-
out.push(` TESTED ${what}: held on ${v.inputs} generated input(s)${v.checked === void 0 ? "" : ` and ${v.checked} the simulator passed`}${v.discarded > 0 ? `, ${v.discarded} outside the claim` : ""}`);
|
|
2571
|
-
break;
|
|
2572
|
-
case "PROVEN": {
|
|
2573
|
-
const beside = [...v.tested === void 0 ? [] : [`${v.tested} generated input(s)`], ...v.checked === void 0 ? [] : [`${v.checked} the simulator passed`]];
|
|
2574
|
-
out.push(` PROVEN ${what}: by ${v.engine.name} ${v.engine.version}${beside.length > 0 ? `; also held on ${beside.join(" and ")}` : ""}`);
|
|
2575
|
-
break;
|
|
2576
|
-
}
|
|
2577
|
-
case "CONFLICT":
|
|
2578
|
-
out.push(` CONFLICT ${what}: ${v.proven.name} ${v.proven.version} proves it, ${v.refuted.name} ${v.refuted.version} refutes it; not resolved automatically`);
|
|
2579
|
-
if (v.refuted.counterexample) out.push(` ${v.refuted.counterexample.binders.map((b) => `${b.name} = ${b.value}`).join(", ")}`);
|
|
2580
|
-
break;
|
|
2581
|
-
case "REFUTED": {
|
|
2582
|
-
const c = v.counterexample;
|
|
2583
|
-
const where = v.rung === "PROVEN" ? `by ${v.engine?.name ?? "an engine"}` : c.call === void 0 ? "on a generated input" : `at ${c.call}`;
|
|
2584
|
-
out.push(` REFUTED ${what}: false ${where}${c.error ? `, which threw ${c.error}` : ""}`);
|
|
2585
|
-
if (c.binders.length > 0) out.push(` ${c.binders.map((b) => `${b.name} = ${b.value}`).join(", ")}`);
|
|
2586
|
-
out.push(` ${v.text.replace(/\s+/g, " ")}`);
|
|
2587
|
-
break;
|
|
2588
|
-
}
|
|
2589
|
-
case "UNKNOWN": out.push(` UNKNOWN ${what}: ${v.cause}: ${v.reason}`);
|
|
2590
|
-
}
|
|
2591
|
-
for (const a of v.assumptions) if (v.kind === "PROVEN" || a.startsWith("not PROVEN by ")) out.push(` ${a}`);
|
|
2592
|
-
}
|
|
2593
|
-
return out.join("\n");
|
|
2594
|
-
}
|
|
2595
|
-
function clip(s, max) {
|
|
2596
|
-
return s.length <= max ? s : `${s.slice(0, max - 1)}…`;
|
|
2597
|
-
}
|
|
2598
|
-
function pad(s, width) {
|
|
2599
|
-
return s.length >= width ? s : s + " ".repeat(width - s.length);
|
|
2600
|
-
}
|
|
2601
|
-
/** The trace as a fixed-width table; evidence rows are marked with `*`. */
|
|
2602
|
-
function renderTrace(log, evidence = []) {
|
|
2603
|
-
const rows = log.entries.map((e) => [
|
|
2604
|
-
evidence.includes(e.seq) ? "*" : " ",
|
|
2605
|
-
String(e.seq),
|
|
2606
|
-
e.invocation,
|
|
2607
|
-
e.kind,
|
|
2608
|
-
e.target,
|
|
2609
|
-
e.op,
|
|
2610
|
-
clip(canonicalJson(e.payload), 72)
|
|
2611
|
-
]);
|
|
2612
|
-
const header = [
|
|
2613
|
-
" ",
|
|
2614
|
-
"seq",
|
|
2615
|
-
"invocation",
|
|
2616
|
-
"kind",
|
|
2617
|
-
"target",
|
|
2618
|
-
"op",
|
|
2619
|
-
"payload"
|
|
2620
|
-
];
|
|
2621
|
-
const widths = header.map((h, i) => Math.max(h.length, ...rows.map((r) => r[i].length)));
|
|
2622
|
-
const line = (cells) => cells.map((c, i) => i === cells.length - 1 ? c : pad(c, widths[i])).join(" ").trimEnd();
|
|
2623
|
-
return [line(header), ...rows.map(line)].join("\n");
|
|
2624
|
-
}
|
|
2625
|
-
/** A readable report: verdict, what it rests on, and for a violation the schedule and trace. */
|
|
2626
|
-
function renderText(verdict) {
|
|
2627
|
-
const out = [];
|
|
2628
|
-
const r = verdict.reached;
|
|
2629
|
-
out.push(`${verdict.kind} ${verdict.scenario} (seed ${String(verdict.seed)})`);
|
|
2630
|
-
switch (verdict.kind) {
|
|
2631
|
-
case "VIOLATED":
|
|
2632
|
-
out.push("");
|
|
2633
|
-
out.push(`Invariant ${verdict.violation.invariant} is violated: ${verdict.violation.message}`);
|
|
2634
|
-
out.push(`Keys: ${verdict.violation.keys.join(", ")}`);
|
|
2635
|
-
out.push("");
|
|
2636
|
-
out.push(`Schedule (run seed ${verdict.schedule.seed}), events kept: ${verdict.keptEvents.join(", ")}`);
|
|
2637
|
-
if (verdict.schedule.faults.length === 0) out.push(" no injected faults");
|
|
2638
|
-
for (const f of verdict.schedule.faults) out.push(` ${f.fault} at ${f.invocation} effect #${f.ordinal} (${f.target} ${f.op})`);
|
|
2639
|
-
out.push(`Shrunk: ${verdict.shrunk.eventsRemoved} event(s) and ${verdict.shrunk.faultsRemoved} fault(s) removed in ${verdict.shrunk.runs} run(s)`);
|
|
2640
|
-
out.push("");
|
|
2641
|
-
out.push("Trace:");
|
|
2642
|
-
out.push(renderTrace(verdict.trace, verdict.violation.evidence));
|
|
2643
|
-
break;
|
|
2644
|
-
case "BOUNDED": {
|
|
2645
|
-
const b = verdict.bounds;
|
|
2646
|
-
out.push("");
|
|
2647
|
-
out.push("No violation found within the stated bounds. This is not a proof.");
|
|
2648
|
-
out.push(`Bounds: faults per run ≤ ${b.maxFaultsPerRun}, redeliveries ≤ ${b.maxRedeliveries}, concurrent invocations ≤ ${b.maxConcurrentInvocations}, budget ${b.budgetMs} ms`);
|
|
2649
|
-
break;
|
|
2650
|
-
}
|
|
2651
|
-
case "UNSUPPORTED":
|
|
2652
|
-
out.push("");
|
|
2653
|
-
out.push(`Eutaxy could not decide this scenario (${verdict.cause}): ${verdict.reason}`);
|
|
2654
|
-
}
|
|
2655
|
-
out.push("");
|
|
2656
|
-
out.push(`Explored: ${r.runs} run(s), ${r.effectPoints} effect point(s), ${r.faultsInjected} fault(s) injected, ${r.elapsedMs} ms`);
|
|
2657
|
-
const e = r.exhaustive;
|
|
2658
|
-
if (e) {
|
|
2659
|
-
const stopped = {
|
|
2660
|
-
VIOLATED: "stopped at the first violation",
|
|
2661
|
-
BOUNDED: "stopped at the budget before every schedule ran",
|
|
2662
|
-
UNSUPPORTED: "stopped where the scenario could not be decided"
|
|
2663
|
-
}[verdict.kind];
|
|
2664
|
-
out.push(`Exhaustive: ${e.complete ? "every schedule within the bounds ran" : stopped} (${e.schedules} schedules${e.reduction ? ", partial-order reduction" : ""})`);
|
|
2665
|
-
}
|
|
2666
|
-
out.push("Assumptions:");
|
|
2667
|
-
for (const a of verdict.assumptions) out.push(` - ${a}`);
|
|
2668
|
-
return out.join("\n");
|
|
2669
|
-
}
|
|
2670
|
-
export { TokenizeError as _, emitRegressionTest as a, describeVerdict as b, specLadder as c, lemmaBool as d, lemmaUnresolved as f, parseProperty as g, LemmaSyntaxError as h, renderTrace as i, testedRung as l, extractSpecs as m, renderSpecs as n, aggregate as o, predicateSource as p, renderText as r, checkSpecModule as s, renderJson as t, checkedRung as u, templateSubstitutions as v, tokenize as y };
|
|
2671
|
-
|
|
2672
|
-
//# sourceMappingURL=src-Cs4Mg55e.mjs.map
|