tla-ts 0.0.2 → 0.0.3-dev.07eab97
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 +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +98 -15
- package/dist/index.d.ts +98 -15
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +23 -2
- package/dist/run-cli.js +23 -2
- package/dist/transfer.cjs +36 -16
- package/dist/transfer.js +36 -16
- package/package.json +3 -1
- package/src/ast.ts +139 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +555 -107
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +156 -48
- package/src/errors.ts +123 -3
- package/src/evaluate.ts +258 -160
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +48 -12
- package/src/instances.ts +16 -15
- package/src/levels.ts +181 -45
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +350 -63
- package/src/resolve.ts +124 -44
- package/src/run-cli.ts +405 -45
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +183 -39
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/transfer.ts +22 -1
- package/src/value-sources.ts +29 -0
- package/src/values.ts +300 -83
package/src/tlc-module.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TlaModuleError } from "./errors";
|
|
1
|
+
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { EXIT_STATUS } from "./exit-codes";
|
|
3
3
|
import type { Builtin } from "./evaluate";
|
|
4
4
|
import { prettyPrint } from "./pretty-print";
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
getValue,
|
|
16
16
|
} from "./builtins";
|
|
17
17
|
import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "./runtime";
|
|
18
|
-
import { createActionRecord } from "./tlc-ext";
|
|
18
|
+
import { createActionRecord, setTlcValue } from "./tlc-ext";
|
|
19
19
|
import { getRandomElement } from "./tlc-random";
|
|
20
20
|
import {
|
|
21
21
|
ANY_SET,
|
|
@@ -39,11 +39,17 @@ const formatCalendarVersion = (date: Date) =>
|
|
|
39
39
|
const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
|
|
40
40
|
createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
|
|
41
41
|
|
|
42
|
+
// TLC creates its model checker after it processes the spec, so these are undefined before then.
|
|
43
|
+
const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
|
|
44
|
+
|
|
42
45
|
const getRuntimeValue = (
|
|
43
46
|
runtime: CheckerRuntime,
|
|
44
47
|
key: string,
|
|
45
48
|
state: (Value | undefined)[] | undefined,
|
|
46
49
|
) => {
|
|
50
|
+
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
52
|
+
}
|
|
47
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
48
54
|
switch (key) {
|
|
49
55
|
case "level":
|
|
@@ -98,11 +104,11 @@ const getRuntimeValue = (
|
|
|
98
104
|
}
|
|
99
105
|
if (key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
100
106
|
const value = getNamedRegisters(runtime).get(key);
|
|
101
|
-
if (value === undefined) throw new
|
|
107
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
102
108
|
return value;
|
|
103
109
|
}
|
|
104
110
|
if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
|
|
105
|
-
throw new
|
|
111
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
106
112
|
};
|
|
107
113
|
|
|
108
114
|
const getPermutations = (elements: Value[]): Value[][] =>
|
|
@@ -191,11 +197,11 @@ export const createTlcOperators = (
|
|
|
191
197
|
const key = getValue(args, 0, "TLCGet");
|
|
192
198
|
if (typeof key === "number" && key >= 0) {
|
|
193
199
|
const value = runtime.registers.get(key);
|
|
194
|
-
if (value === undefined) throw new
|
|
200
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
195
201
|
return value;
|
|
196
202
|
}
|
|
197
203
|
if (typeof key !== "string") {
|
|
198
|
-
throw new
|
|
204
|
+
throw new TlaEvaluationError(
|
|
199
205
|
`The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
200
206
|
);
|
|
201
207
|
}
|
|
@@ -203,33 +209,8 @@ export const createTlcOperators = (
|
|
|
203
209
|
}),
|
|
204
210
|
defineStateOperator("TLCSet", 2, (args) => {
|
|
205
211
|
const key = getValue(args, 0, "TLCSet");
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
runtime.registers.set(key, value);
|
|
209
|
-
return true;
|
|
210
|
-
}
|
|
211
|
-
if (key === "exit") {
|
|
212
|
-
if (value === true) runtime.isStopped = true;
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
if (key === "pause") {
|
|
216
|
-
if (value === true) {
|
|
217
|
-
print("Press enter to resume model checking.");
|
|
218
|
-
runtime.pause();
|
|
219
|
-
}
|
|
220
|
-
return true;
|
|
221
|
-
}
|
|
222
|
-
if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
223
|
-
runtime.namedRegisters.set(key, value);
|
|
224
|
-
return true;
|
|
225
|
-
}
|
|
226
|
-
if (typeof key === "string" && key.startsWith("-D")) {
|
|
227
|
-
runtime.properties.set(key.slice(2), formatValue(value));
|
|
228
|
-
return true;
|
|
229
|
-
}
|
|
230
|
-
throw new TlaModuleError(
|
|
231
|
-
`The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
232
|
-
);
|
|
212
|
+
setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
|
|
213
|
+
return true;
|
|
233
214
|
}),
|
|
234
215
|
defineConstant("Any", ANY_SET),
|
|
235
216
|
defineOperator("RandomElement", 1, (args) =>
|
package/src/tlc-modules.ts
CHANGED
|
@@ -1013,6 +1013,10 @@ export const TLC_JAVA_METHODS = new Map([
|
|
|
1013
1013
|
"RandomElement",
|
|
1014
1014
|
"public static tlc2.value.impl.Value tlc2.module.TLC.RandomElement(tlc2.value.impl.Value)",
|
|
1015
1015
|
],
|
|
1016
|
+
[
|
|
1017
|
+
"TLCGet",
|
|
1018
|
+
"public static tlc2.value.impl.Value tlc2.module.TLCGetSet.TLCGetEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|
|
1019
|
+
],
|
|
1016
1020
|
[
|
|
1017
1021
|
"TLCEval",
|
|
1018
1022
|
"public static tlc2.value.impl.Value tlc2.module.TLCEval.tlcEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|
package/src/tlc-order.ts
CHANGED
|
@@ -184,9 +184,18 @@ const isAdjacent = (left: Token, right: Token | undefined) =>
|
|
|
184
184
|
right.line === left.line &&
|
|
185
185
|
right.column === left.column + left.text.length;
|
|
186
186
|
|
|
187
|
+
const STRING_ESCAPES: Record<string, string> = {
|
|
188
|
+
"\\": "\\\\",
|
|
189
|
+
'"': '\\"',
|
|
190
|
+
"\n": "\\n",
|
|
191
|
+
"\t": "\\t",
|
|
192
|
+
"\r": "\\r",
|
|
193
|
+
"\f": "\\f",
|
|
194
|
+
};
|
|
195
|
+
|
|
187
196
|
// The texts SANY interns while parsing, in order. SANY reads a proof step label such as <1>2 and
|
|
188
|
-
// a module header such as ---- MODULE as one token
|
|
189
|
-
// during semantic analysis.
|
|
197
|
+
// a module header such as ---- MODULE as one token. It interns a string literal as written, quotes
|
|
198
|
+
// included, and interns the literal's contents only during semantic analysis.
|
|
190
199
|
const getParsedTexts = (tokens: Token[]) => {
|
|
191
200
|
const texts: string[] = [];
|
|
192
201
|
for (let index = 0; index < tokens.length; index++) {
|
|
@@ -202,7 +211,12 @@ const getParsedTexts = (tokens: Token[]) => {
|
|
|
202
211
|
continue;
|
|
203
212
|
}
|
|
204
213
|
const isHeader = token.text === "MODULE" && tokens[index - 1]?.kind === "Separator";
|
|
205
|
-
if (
|
|
214
|
+
if (isHeader) continue;
|
|
215
|
+
texts.push(
|
|
216
|
+
token.kind === "String"
|
|
217
|
+
? `"${token.text.replace(/[\\"\n\t\r\f]/g, (character) => STRING_ESCAPES[character])}"`
|
|
218
|
+
: token.text,
|
|
219
|
+
);
|
|
206
220
|
}
|
|
207
221
|
return texts;
|
|
208
222
|
};
|
|
@@ -264,9 +278,13 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
|
264
278
|
(name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name);
|
|
265
279
|
};
|
|
266
280
|
|
|
267
|
-
// The variables in the order
|
|
268
|
-
//
|
|
269
|
-
export const
|
|
281
|
+
// The constants or variables in the order of ModuleNode.getConstantDecls or getVariableDecls: the
|
|
282
|
+
// reverse of the root module's context enumeration. TLC compares a state's variables in this order.
|
|
283
|
+
export const getTlcDeclarationOrder = (
|
|
284
|
+
source: string,
|
|
285
|
+
loadModule: ModuleLoader,
|
|
286
|
+
kind: "constant" | "variable",
|
|
287
|
+
): string[] => {
|
|
270
288
|
const getParsed = createModuleSet(source, loadModule);
|
|
271
289
|
const getUnits = (unit: ParsedModule) => {
|
|
272
290
|
const tokens = unit.tokens;
|
|
@@ -386,7 +404,7 @@ export const getTlcVariableOrder = (source: string, loadModule: ModuleLoader): s
|
|
|
386
404
|
};
|
|
387
405
|
return getContext(undefined)
|
|
388
406
|
.enumerate()
|
|
389
|
-
.filter((entry) => entry.kind ===
|
|
407
|
+
.filter((entry) => entry.kind === kind)
|
|
390
408
|
.map((entry) => entry.name)
|
|
391
409
|
.reverse();
|
|
392
410
|
};
|
package/src/tlc-random.ts
CHANGED
|
@@ -181,8 +181,10 @@ const createShiftTable = (length: number) => {
|
|
|
181
181
|
|
|
182
182
|
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
183
183
|
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
184
|
-
// The fingerprinter caches each distinct variable value for one pass over the stored states.
|
|
185
|
-
|
|
184
|
+
// The fingerprinter caches each distinct variable value for one pass over the stored states. It
|
|
185
|
+
// reads a state's variables in `order` and writes each fingerprint's low and high words into
|
|
186
|
+
// `words`, an Int32Array over a little-endian BigInt64Array.
|
|
187
|
+
export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
186
188
|
const shiftTables = new Map<number, Int32Array>();
|
|
187
189
|
const indices = new Map<Value, number>();
|
|
188
190
|
const contributions: number[] = [];
|
|
@@ -204,11 +206,11 @@ export const createTlcFingerprinter = () => {
|
|
|
204
206
|
shiftTables.set(length, table);
|
|
205
207
|
return table;
|
|
206
208
|
};
|
|
207
|
-
return (state: Value[]) => {
|
|
209
|
+
return (state: Value[], position: number) => {
|
|
208
210
|
const outer = { high, low };
|
|
209
211
|
const fingerprint = { high: INITIAL_HIGH, low: INITIAL_LOW };
|
|
210
|
-
for (const
|
|
211
|
-
const contribution = getContribution(
|
|
212
|
+
for (const index of order) {
|
|
213
|
+
const contribution = getContribution(state[index]);
|
|
212
214
|
const table = getShiftTable(contributions[contribution]);
|
|
213
215
|
const extended = {
|
|
214
216
|
high: contributions[contribution + 1],
|
|
@@ -225,7 +227,8 @@ export const createTlcFingerprinter = () => {
|
|
|
225
227
|
}
|
|
226
228
|
high = outer.high;
|
|
227
229
|
low = outer.low;
|
|
228
|
-
|
|
230
|
+
words[2 * position] = fingerprint.low;
|
|
231
|
+
words[2 * position + 1] = fingerprint.high;
|
|
229
232
|
};
|
|
230
233
|
};
|
|
231
234
|
|
package/src/tlc-summary.ts
CHANGED
|
@@ -87,11 +87,26 @@ export const getFingerprintDistance = (
|
|
|
87
87
|
const probability = getOptimisticProbability(distinctStates, generatedStates);
|
|
88
88
|
if (probability < ACTUAL_PROBABILITY_THRESHOLD) return undefined;
|
|
89
89
|
const fingerprints = getFingerprints().sort();
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
// The low and high words of each fingerprint. A gap whose high words differ by at most 2^20 is
|
|
91
|
+
// below 2^53, so a number holds it exactly and only wider gaps need a BigInt.
|
|
92
|
+
const words = new Int32Array(fingerprints.buffer);
|
|
93
|
+
const nearest = { narrow: Number.POSITIVE_INFINITY, wide: 0x7fff_ffff_ffff_ffffn };
|
|
94
|
+
for (let index = 1; index < fingerprints.length; index++) {
|
|
95
|
+
const high = words[2 * index + 1];
|
|
96
|
+
const previousHigh = words[2 * index - 1];
|
|
97
|
+
if (high < 0 !== previousHigh < 0) continue;
|
|
98
|
+
const highGap = high - previousHigh;
|
|
99
|
+
if (highGap <= 0x10_0000) {
|
|
100
|
+
const gap = highGap * 0x1_0000_0000 + (words[2 * index] >>> 0) - (words[2 * index - 2] >>> 0);
|
|
101
|
+
nearest.narrow = Math.min(nearest.narrow, gap);
|
|
102
|
+
} else {
|
|
103
|
+
const gap = fingerprints[index] - fingerprints[index - 1];
|
|
104
|
+
if (gap < nearest.wide) nearest.wide = gap;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (nearest.narrow === Number.POSITIVE_INFINITY) return nearest.wide;
|
|
108
|
+
const narrow = BigInt(nearest.narrow);
|
|
109
|
+
return narrow < nearest.wide ? narrow : nearest.wide;
|
|
95
110
|
};
|
|
96
111
|
|
|
97
112
|
export const getOutDegreeSummary = (buckets: number[]) => {
|
package/src/transfer.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createModelValue,
|
|
3
|
+
createSortedFunction,
|
|
4
|
+
createSortedSet,
|
|
5
|
+
getDomainInterval,
|
|
6
|
+
getFunctionDomain,
|
|
7
|
+
type Value,
|
|
8
|
+
} from "./values";
|
|
2
9
|
|
|
3
10
|
const FALSE = 0;
|
|
4
11
|
const TRUE = 1;
|
|
@@ -7,6 +14,7 @@ const STRING = 3;
|
|
|
7
14
|
const MODEL_VALUE = 4;
|
|
8
15
|
const SET = 5;
|
|
9
16
|
const FUNCTION = 6;
|
|
17
|
+
const INTERVAL_FUNCTION = 7;
|
|
10
18
|
|
|
11
19
|
export interface EncodedStates {
|
|
12
20
|
words: Int32Array;
|
|
@@ -65,6 +73,13 @@ export const createStateEncoder = (): StateEncoder => {
|
|
|
65
73
|
return value.elements.every(encodeValue);
|
|
66
74
|
}
|
|
67
75
|
if (value.kind === "Function") {
|
|
76
|
+
const interval = getDomainInterval(value.domain);
|
|
77
|
+
if (interval) {
|
|
78
|
+
push(INTERVAL_FUNCTION);
|
|
79
|
+
push(interval.low);
|
|
80
|
+
push(value.range.length);
|
|
81
|
+
return value.range.every(encodeValue);
|
|
82
|
+
}
|
|
68
83
|
push(FUNCTION);
|
|
69
84
|
push(value.domain.length);
|
|
70
85
|
return value.domain.every(encodeValue) && value.range.every(encodeValue);
|
|
@@ -102,6 +117,12 @@ export const createStateDecoder =
|
|
|
102
117
|
if (tag === INTEGER) return words[cursor.index++];
|
|
103
118
|
if (tag === STRING) return encoded.strings[words[cursor.index++]];
|
|
104
119
|
if (tag === MODEL_VALUE) return createModelValue(encoded.strings[words[cursor.index++]]);
|
|
120
|
+
if (tag === INTERVAL_FUNCTION) {
|
|
121
|
+
const low = words[cursor.index++];
|
|
122
|
+
const length = words[cursor.index++];
|
|
123
|
+
const domain = getFunctionDomain({ kind: "Interval", low, high: low + length - 1 });
|
|
124
|
+
return createSortedFunction(domain, decodeElements(length));
|
|
125
|
+
}
|
|
105
126
|
const length = words[cursor.index++];
|
|
106
127
|
if (tag === SET) return createSortedSet(decodeElements(length));
|
|
107
128
|
const domain = decodeElements(length);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Expression } from "./ast";
|
|
2
|
+
import { TlaEvaluationError } from "./errors";
|
|
3
|
+
import type { Value } from "./values";
|
|
4
|
+
|
|
5
|
+
// While TLC replays a failure, it gives each set, tuple or record it builds the expression that
|
|
6
|
+
// built it. A method of such a value that fails prepends that expression to the error's trace, and
|
|
7
|
+
// TLC reports the error as one it was unable to fingerprint.
|
|
8
|
+
export const valueSources: { current: WeakMap<object, Expression> | undefined } = {
|
|
9
|
+
current: undefined,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const fingerprintTraces = new WeakMap<TlaEvaluationError, Expression[]>();
|
|
13
|
+
|
|
14
|
+
export const getFingerprintTrace = (error: unknown) =>
|
|
15
|
+
error instanceof TlaEvaluationError ? fingerprintTraces.get(error) : undefined;
|
|
16
|
+
|
|
17
|
+
export const withSource = <T>(value: Value, run: () => T): T => {
|
|
18
|
+
const sources = valueSources.current;
|
|
19
|
+
if (!sources || typeof value !== "object") return run();
|
|
20
|
+
try {
|
|
21
|
+
return run();
|
|
22
|
+
} catch (error) {
|
|
23
|
+
const source = sources.get(value);
|
|
24
|
+
if (source && error instanceof TlaEvaluationError) {
|
|
25
|
+
fingerprintTraces.set(error, [source, ...(fingerprintTraces.get(error) ?? [])]);
|
|
26
|
+
}
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
};
|