tla-ts 0.0.3-dev.5ea4534 → 0.0.3-dev.5fd6731
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/dist/index.cjs +1 -1
- package/dist/index.d.cts +59 -22
- package/dist/index.d.ts +59 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +14 -12
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +39 -21
- package/dist/transfer.js +39 -21
- package/package.json +1 -1
- package/src/ast.ts +27 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +357 -79
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +249 -87
- package/src/errors.ts +100 -1
- package/src/evaluate.ts +182 -26
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +177 -36
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +117 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +155 -49
- package/src/resolve.ts +911 -342
- package/src/run-cli.ts +61 -11
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +26 -14
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +169 -33
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-module.ts +6 -6
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +231 -61
package/src/successors.ts
CHANGED
|
@@ -4,9 +4,15 @@ import {
|
|
|
4
4
|
type Bound,
|
|
5
5
|
type Definition,
|
|
6
6
|
type Expression,
|
|
7
|
+
type QuantifierExpression,
|
|
7
8
|
} from "./ast";
|
|
8
9
|
import { getReadVariables } from "./dependencies";
|
|
9
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
TlaEvaluationError,
|
|
12
|
+
TlaIllegalStateError,
|
|
13
|
+
TlaIncompleteStateError,
|
|
14
|
+
TlaUnassignedError,
|
|
15
|
+
} from "./errors";
|
|
10
16
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
11
17
|
import {
|
|
12
18
|
describeLocation,
|
|
@@ -17,6 +23,7 @@ import {
|
|
|
17
23
|
bindValue,
|
|
18
24
|
compileArgumentScope,
|
|
19
25
|
evaluate,
|
|
26
|
+
getEntryEvaluator,
|
|
20
27
|
expectBoolean,
|
|
21
28
|
forEachBinding,
|
|
22
29
|
getBoundsDomain,
|
|
@@ -28,6 +35,7 @@ import {
|
|
|
28
35
|
resolveGlobalOperator,
|
|
29
36
|
type Context,
|
|
30
37
|
type Environment,
|
|
38
|
+
type Evaluator,
|
|
31
39
|
type Scope,
|
|
32
40
|
type State,
|
|
33
41
|
} from "./evaluate";
|
|
@@ -50,6 +58,7 @@ export interface Successor {
|
|
|
50
58
|
interface Run {
|
|
51
59
|
environment: Environment;
|
|
52
60
|
isInit: boolean;
|
|
61
|
+
isEnabled: boolean;
|
|
53
62
|
current: Value[] | undefined;
|
|
54
63
|
isStopped: boolean;
|
|
55
64
|
}
|
|
@@ -86,25 +95,54 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
86
95
|
return copy;
|
|
87
96
|
};
|
|
88
97
|
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
// TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
|
|
99
|
+
const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
|
|
91
100
|
if (typeof value === "boolean") return value;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
101
|
+
throw new TlaEvaluationError(
|
|
102
|
+
`In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
107
|
+
// TLC rejects a number in the next-state relation when it splits the relation into actions.
|
|
108
|
+
if (context.next !== undefined && expression.kind === "Number") {
|
|
109
|
+
throw setExitCode(
|
|
110
|
+
new TlaEvaluationError(
|
|
111
|
+
`The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
|
|
112
|
+
),
|
|
113
|
+
EXIT_STATUS.error,
|
|
114
|
+
);
|
|
97
115
|
}
|
|
116
|
+
return expectFormula(expression, value, context.next === undefined);
|
|
98
117
|
};
|
|
99
118
|
|
|
100
119
|
const isAssignmentOperator = (operator: string) =>
|
|
101
120
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
102
121
|
|
|
122
|
+
const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
|
|
123
|
+
|
|
124
|
+
const getRunners = (environment: Environment, isInit: boolean) => {
|
|
125
|
+
if (!environment.trace) return isInit ? initRunners : nextRunners;
|
|
126
|
+
const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
|
|
127
|
+
tracedRunners.set(environment, runners);
|
|
128
|
+
return runners[isInit ? 0 : 1];
|
|
129
|
+
};
|
|
130
|
+
|
|
103
131
|
const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
|
|
104
|
-
const runners = isInit
|
|
132
|
+
const runners = getRunners(environment, isInit);
|
|
105
133
|
const cached = runners.get(expression);
|
|
106
134
|
if (cached) return cached;
|
|
107
|
-
const
|
|
135
|
+
const compiled = compileAction(expression, environment, isInit);
|
|
136
|
+
const trace = environment.trace;
|
|
137
|
+
// TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
|
|
138
|
+
const runner: Runner = trace
|
|
139
|
+
? (run, scope, partial, action, isFixed, onComplete) => {
|
|
140
|
+
const depth = trace.length;
|
|
141
|
+
trace.push(expression);
|
|
142
|
+
compiled(run, scope, partial, action, isFixed, onComplete);
|
|
143
|
+
trace.length = depth;
|
|
144
|
+
}
|
|
145
|
+
: compiled;
|
|
108
146
|
runners.set(expression, runner);
|
|
109
147
|
return runner;
|
|
110
148
|
};
|
|
@@ -188,6 +226,23 @@ const getUnchangedIndices = (
|
|
|
188
226
|
return getUnchangedIndices(definition.body, environment, visited);
|
|
189
227
|
};
|
|
190
228
|
|
|
229
|
+
// TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
|
|
230
|
+
const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
|
|
231
|
+
if (expression.kind === "Tuple") {
|
|
232
|
+
return [
|
|
233
|
+
expression,
|
|
234
|
+
...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
const definition =
|
|
238
|
+
expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
|
|
239
|
+
? getZeroArityBody(expression.name, environment)
|
|
240
|
+
: undefined;
|
|
241
|
+
return definition
|
|
242
|
+
? [expression, ...getUnchangedFrames(definition.body, environment)]
|
|
243
|
+
: [expression];
|
|
244
|
+
};
|
|
245
|
+
|
|
191
246
|
const ACTION_KINDS = new Set([
|
|
192
247
|
"Label",
|
|
193
248
|
"And",
|
|
@@ -292,12 +347,35 @@ const toConjunct = (
|
|
|
292
347
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
293
348
|
};
|
|
294
349
|
|
|
350
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
351
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
352
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
353
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
354
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
355
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
356
|
+
);
|
|
357
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
358
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
359
|
+
const operands = expression.body.operands;
|
|
360
|
+
const index = operands.findIndex((operand) => {
|
|
361
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
362
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
363
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
364
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
365
|
+
});
|
|
366
|
+
const count = index === -1 ? operands.length : index;
|
|
367
|
+
if (count === 0) return undefined;
|
|
368
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
369
|
+
};
|
|
370
|
+
|
|
295
371
|
const compileConjunction = (
|
|
296
372
|
operands: Expression[],
|
|
297
373
|
environment: Environment,
|
|
298
374
|
isInit: boolean,
|
|
299
375
|
): Runner => {
|
|
300
376
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
377
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
378
|
+
const trace = environment.trace;
|
|
301
379
|
const runFrom = (
|
|
302
380
|
start: number,
|
|
303
381
|
run: Run,
|
|
@@ -318,14 +396,25 @@ const compileConjunction = (
|
|
|
318
396
|
return;
|
|
319
397
|
}
|
|
320
398
|
const conjunct = conjuncts[index];
|
|
399
|
+
if (trace) {
|
|
400
|
+
trace.push(conjunct.expression);
|
|
401
|
+
if (conjunct.expression.kind === "Unchanged") {
|
|
402
|
+
trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
321
405
|
if (conjunct.kind === "Guard") {
|
|
322
406
|
context ??= getContext(run, scope, current);
|
|
323
|
-
|
|
407
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
408
|
+
conjunct.expression,
|
|
409
|
+
environment,
|
|
410
|
+
));
|
|
411
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
324
412
|
continue;
|
|
325
413
|
}
|
|
326
414
|
if (conjunct.kind === "Assign") {
|
|
327
415
|
context ??= getContext(run, scope, current);
|
|
328
|
-
const
|
|
416
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
417
|
+
const value = evaluator(context);
|
|
329
418
|
const assigned = current[conjunct.index];
|
|
330
419
|
if (assigned === undefined) {
|
|
331
420
|
current = assign(current, conjunct.index, value);
|
|
@@ -362,10 +451,7 @@ const compileConjunction = (
|
|
|
362
451
|
};
|
|
363
452
|
|
|
364
453
|
const holdsInInit = (expression: Expression, context: Context) =>
|
|
365
|
-
|
|
366
|
-
evaluate(expression, context),
|
|
367
|
-
`the initial predicate at ${describeLocation(expression)}`,
|
|
368
|
-
);
|
|
454
|
+
expectFormula(expression, evaluate(expression, context), true);
|
|
369
455
|
|
|
370
456
|
const compileGuard =
|
|
371
457
|
(expression: Expression): Runner =>
|
|
@@ -390,7 +476,7 @@ const compileMembership = (
|
|
|
390
476
|
expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
|
|
391
477
|
if (!isEnumerable(value)) {
|
|
392
478
|
throw new TlaEvaluationError(
|
|
393
|
-
`In computing next states, the right side of \\
|
|
479
|
+
`In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
|
|
394
480
|
);
|
|
395
481
|
}
|
|
396
482
|
const elements = getStreamedElements(value);
|
|
@@ -734,6 +820,34 @@ const compileAction = (
|
|
|
734
820
|
}
|
|
735
821
|
};
|
|
736
822
|
}
|
|
823
|
+
const hoisted =
|
|
824
|
+
isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
|
|
825
|
+
if (hoisted) {
|
|
826
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
827
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
828
|
+
const context = getContext(run, scope, partial);
|
|
829
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
830
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
831
|
+
);
|
|
832
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
833
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
834
|
+
if (index < 0) {
|
|
835
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
for (const element of sets[index]) {
|
|
839
|
+
if (run.isStopped) return;
|
|
840
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
841
|
+
if (index === sets.length - 1) {
|
|
842
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
843
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
844
|
+
}
|
|
845
|
+
visit(index - 1, elementScope);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
visit(sets.length - 1, scope);
|
|
849
|
+
};
|
|
850
|
+
}
|
|
737
851
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
738
852
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
739
853
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
@@ -810,7 +924,7 @@ const compileAction = (
|
|
|
810
924
|
const body = arm?.body ?? other;
|
|
811
925
|
if (!body)
|
|
812
926
|
throw new TlaEvaluationError(
|
|
813
|
-
|
|
927
|
+
`In computing ${run.isInit ? "initial" : "next"} states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
814
928
|
);
|
|
815
929
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
816
930
|
};
|
|
@@ -842,8 +956,17 @@ const compileAction = (
|
|
|
842
956
|
const unchanged = getUnchangedWrapper(expression.subscript);
|
|
843
957
|
return (run, scope, partial, action, isFixed, onComplete) =>
|
|
844
958
|
body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
|
|
845
|
-
|
|
846
|
-
|
|
959
|
+
// ENABLED evaluates the subscript on the successor that the action specifies. TLC names
|
|
960
|
+
// a variable that the action leaves without a value.
|
|
961
|
+
try {
|
|
962
|
+
if (!holds(unchanged, getContext(run, scope, nextPartial)))
|
|
963
|
+
onComplete(nextPartial, nextAction);
|
|
964
|
+
} catch (error) {
|
|
965
|
+
if (!run.isEnabled || !(error instanceof TlaUnassignedError)) throw error;
|
|
966
|
+
throw new TlaEvaluationError(
|
|
967
|
+
`The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
847
970
|
});
|
|
848
971
|
}
|
|
849
972
|
case "BoxAction":
|
|
@@ -920,7 +1043,13 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
920
1043
|
);
|
|
921
1044
|
}
|
|
922
1045
|
const environment = context.environment;
|
|
923
|
-
const run: Run = {
|
|
1046
|
+
const run: Run = {
|
|
1047
|
+
environment,
|
|
1048
|
+
isInit: false,
|
|
1049
|
+
isEnabled: true,
|
|
1050
|
+
current: state,
|
|
1051
|
+
isStopped: false,
|
|
1052
|
+
};
|
|
924
1053
|
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
925
1054
|
getRunner(expression, environment, false)(
|
|
926
1055
|
run,
|
|
@@ -941,7 +1070,13 @@ export const forEachInitialState = (
|
|
|
941
1070
|
onState: (state: Value[]) => boolean,
|
|
942
1071
|
scope?: Scope,
|
|
943
1072
|
) => {
|
|
944
|
-
const run: Run = {
|
|
1073
|
+
const run: Run = {
|
|
1074
|
+
environment,
|
|
1075
|
+
isInit: true,
|
|
1076
|
+
isEnabled: false,
|
|
1077
|
+
current: undefined,
|
|
1078
|
+
isStopped: false,
|
|
1079
|
+
};
|
|
945
1080
|
getRunner(expression, environment, true)(
|
|
946
1081
|
run,
|
|
947
1082
|
scope,
|
|
@@ -951,14 +1086,7 @@ export const forEachInitialState = (
|
|
|
951
1086
|
(partial) => {
|
|
952
1087
|
if (run.isStopped) return;
|
|
953
1088
|
if (!isComplete(partial)) {
|
|
954
|
-
|
|
955
|
-
// an evaluation failure.
|
|
956
|
-
throw setExitCode(
|
|
957
|
-
new TlaEvaluationError(
|
|
958
|
-
`The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
|
|
959
|
-
),
|
|
960
|
-
EXIT_STATUS.error,
|
|
961
|
-
);
|
|
1089
|
+
throw setExitCode(new TlaIllegalStateError(partial), EXIT_STATUS.error);
|
|
962
1090
|
}
|
|
963
1091
|
run.isStopped = onState(partial);
|
|
964
1092
|
},
|
|
@@ -990,7 +1118,13 @@ export const forEachSuccessor = (
|
|
|
990
1118
|
onSuccessor: (successor: Successor) => boolean,
|
|
991
1119
|
scope?: Scope,
|
|
992
1120
|
) => {
|
|
993
|
-
const run: Run = {
|
|
1121
|
+
const run: Run = {
|
|
1122
|
+
environment,
|
|
1123
|
+
isInit: false,
|
|
1124
|
+
isEnabled: false,
|
|
1125
|
+
current: state,
|
|
1126
|
+
isStopped: false,
|
|
1127
|
+
};
|
|
994
1128
|
const counter = { successors: 0 };
|
|
995
1129
|
getRunner(expression, environment, false)(
|
|
996
1130
|
run,
|
|
@@ -1001,8 +1135,10 @@ export const forEachSuccessor = (
|
|
|
1001
1135
|
(partial, action) => {
|
|
1002
1136
|
if (run.isStopped) return;
|
|
1003
1137
|
if (!isComplete(partial)) {
|
|
1004
|
-
throw new
|
|
1005
|
-
|
|
1138
|
+
throw new TlaIncompleteStateError(
|
|
1139
|
+
partial,
|
|
1140
|
+
action,
|
|
1141
|
+
getMissingVariables(partial, environment).sort(),
|
|
1006
1142
|
);
|
|
1007
1143
|
}
|
|
1008
1144
|
counter.successors++;
|
package/src/symmetry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { TlaConfigError } from "./errors";
|
|
1
|
+
import { TlaConfigError, TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
2
2
|
import { evaluate, type Environment } from "./evaluate";
|
|
3
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
compareValues,
|
|
5
6
|
createFunction,
|
|
@@ -47,9 +48,13 @@ const composePermutations = (first: Images, second: Images) => {
|
|
|
47
48
|
return composed;
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
// TLC checks the symmetry set outside any phase that reports an error itself.
|
|
52
|
+
const createSymmetryError = (message: string) =>
|
|
53
|
+
setExitCode(new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${message}`), EXIT_STATUS.error);
|
|
54
|
+
|
|
55
|
+
const toImages = (value: Value) => {
|
|
51
56
|
if (!isFunctionValue(value)) {
|
|
52
|
-
throw
|
|
57
|
+
throw createSymmetryError("The symmetry values must be function records.");
|
|
53
58
|
}
|
|
54
59
|
const functionValue = toFiniteFunction(value);
|
|
55
60
|
const permutation: Images = new Map();
|
|
@@ -65,8 +70,13 @@ const toImages = (value: Value, name: string) => {
|
|
|
65
70
|
|
|
66
71
|
export const getSymmetryGroup = (name: string, environment: Environment): Permutation[] => {
|
|
67
72
|
const definition = environment.definitions.get(name);
|
|
73
|
+
if (definition === undefined && !environment.constants.has(name)) {
|
|
74
|
+
throw new TlaConfigError(
|
|
75
|
+
`The symmetry function ${name} specified in the configuration file\nis not defined in the specification.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
68
78
|
if (definition?.kind !== "Operator" || definition.params.length > 0) {
|
|
69
|
-
throw
|
|
79
|
+
throw createSymmetryError(`The symmetry function ${name} must specify a set of permutations.`);
|
|
70
80
|
}
|
|
71
81
|
const value = evaluate(definition.body, {
|
|
72
82
|
environment,
|
|
@@ -75,9 +85,9 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
75
85
|
next: undefined,
|
|
76
86
|
});
|
|
77
87
|
if (!isSetValue(value) || !isEnumerable(value)) {
|
|
78
|
-
throw
|
|
88
|
+
throw createSymmetryError("The symmetry operator must specify a set of functions.");
|
|
79
89
|
}
|
|
80
|
-
const permutations = toFiniteSet(value).elements.map(
|
|
90
|
+
const permutations = toFiniteSet(value).elements.map(toImages);
|
|
81
91
|
const isDisabled =
|
|
82
92
|
definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
|
|
83
93
|
if (isDisabled) return [];
|
package/src/tableau.ts
CHANGED
|
@@ -162,22 +162,39 @@ const containsAction = (expression: LiveExpression): boolean => {
|
|
|
162
162
|
}
|
|
163
163
|
};
|
|
164
164
|
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
165
|
+
const termKeys = new WeakMap<LiveExpression, string>();
|
|
166
|
+
const atoms = { count: 0 };
|
|
167
|
+
|
|
168
|
+
// A term's structure as a string in which each atom stands for itself, so equal terms share a key.
|
|
169
|
+
export const getTermKey = (expression: LiveExpression): string => {
|
|
170
|
+
const cached = termKeys.get(expression);
|
|
171
|
+
if (cached !== undefined) return cached;
|
|
172
|
+
const key = (() => {
|
|
173
|
+
switch (expression.kind) {
|
|
174
|
+
case "Boolean":
|
|
175
|
+
return String(expression.value);
|
|
176
|
+
case "State":
|
|
177
|
+
case "Enabled":
|
|
178
|
+
case "Action":
|
|
179
|
+
return `#${atoms.count++}`;
|
|
180
|
+
case "And":
|
|
181
|
+
case "Or":
|
|
182
|
+
return `${expression.kind}(${expression.operands.map(getTermKey).join(",")})`;
|
|
183
|
+
default:
|
|
184
|
+
return `${expression.kind}(${getTermKey(expression.body)})`;
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
termKeys.set(expression, key);
|
|
188
|
+
return key;
|
|
177
189
|
};
|
|
178
190
|
|
|
179
|
-
const
|
|
180
|
-
|
|
191
|
+
const isEqual = (left: LiveExpression, right: LiveExpression) =>
|
|
192
|
+
left === right || getTermKey(left) === getTermKey(right);
|
|
193
|
+
|
|
194
|
+
const isMember = (items: LiveExpression[], expression: LiveExpression) => {
|
|
195
|
+
const key = getTermKey(expression);
|
|
196
|
+
return items.some((item) => item === expression || getTermKey(item) === key);
|
|
197
|
+
};
|
|
181
198
|
|
|
182
199
|
const getDual = (kind: LiveJunction["kind"]) => (kind === "And" ? "Or" : "And");
|
|
183
200
|
|
|
@@ -272,7 +289,7 @@ const toDisjunctiveNormalForm = (expression: LiveExpression): LiveExpression =>
|
|
|
272
289
|
total.value *= normal.operands.length;
|
|
273
290
|
if (total.value > MAXIMUM_DISJUNCTS) {
|
|
274
291
|
throw new TlaEvaluationError(
|
|
275
|
-
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form",
|
|
292
|
+
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form.",
|
|
276
293
|
);
|
|
277
294
|
}
|
|
278
295
|
}
|
|
@@ -446,13 +463,14 @@ const getTriples = (
|
|
|
446
463
|
return parts ? [{ whole: term, first: parts.first, second: parts.second }] : [];
|
|
447
464
|
});
|
|
448
465
|
|
|
449
|
-
|
|
450
|
-
|
|
466
|
+
// A particle is a set of terms, so its key ignores order and repeats.
|
|
467
|
+
const getParticleKey = (particle: LiveExpression[]) =>
|
|
468
|
+
[...new Set(particle.map(getTermKey))].sort().join("|");
|
|
451
469
|
|
|
452
|
-
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) =>
|
|
453
|
-
|
|
454
|
-
...right
|
|
455
|
-
|
|
470
|
+
const unionParticles = (left: LiveExpression[][], right: LiveExpression[][]) => {
|
|
471
|
+
const rightKeys = new Set(right.map(getParticleKey));
|
|
472
|
+
return [...left.filter((particle) => !rightKeys.has(getParticleKey(particle))), ...right];
|
|
473
|
+
};
|
|
456
474
|
|
|
457
475
|
const getParticleClosure = (terms: LiveExpression[]) => {
|
|
458
476
|
const closure = getPositiveClosure(terms);
|
|
@@ -516,14 +534,23 @@ const createTableauNode = (particle: LiveExpression[]): TableauNode => ({
|
|
|
516
534
|
const createTableau = (formula: LiveExpression) => {
|
|
517
535
|
const nodes = getParticleClosure([formula]).map(createTableauNode);
|
|
518
536
|
const initialCount = nodes.length;
|
|
537
|
+
const indices = new Map<string, number>();
|
|
538
|
+
nodes.forEach((node, index) => {
|
|
539
|
+
const key = getParticleKey(node.particle);
|
|
540
|
+
if (!indices.has(key)) indices.set(key, index);
|
|
541
|
+
});
|
|
519
542
|
for (let index = 0; index < nodes.length; index++) {
|
|
520
543
|
const implied = nodes[index].particle.flatMap((term) =>
|
|
521
544
|
term.kind === "Next" ? [term.body] : [],
|
|
522
545
|
);
|
|
523
546
|
for (const particle of getParticleClosure(implied)) {
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
547
|
+
const key = getParticleKey(particle);
|
|
548
|
+
const existing = indices.get(key);
|
|
549
|
+
if (existing === undefined) {
|
|
550
|
+
indices.set(key, nodes.length);
|
|
551
|
+
nodes.push(createTableauNode(particle));
|
|
552
|
+
}
|
|
553
|
+
nodes[index].successors.push(existing ?? nodes.length - 1);
|
|
527
554
|
}
|
|
528
555
|
}
|
|
529
556
|
return { nodes, initialCount };
|
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";
|
|
@@ -48,7 +48,7 @@ const getRuntimeValue = (
|
|
|
48
48
|
state: (Value | undefined)[] | undefined,
|
|
49
49
|
) => {
|
|
50
50
|
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
-
throw new
|
|
51
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
52
52
|
}
|
|
53
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
54
54
|
switch (key) {
|
|
@@ -104,11 +104,11 @@ const getRuntimeValue = (
|
|
|
104
104
|
}
|
|
105
105
|
if (key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
106
106
|
const value = getNamedRegisters(runtime).get(key);
|
|
107
|
-
if (value === undefined) throw new
|
|
107
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
108
108
|
return value;
|
|
109
109
|
}
|
|
110
110
|
if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
|
|
111
|
-
throw new
|
|
111
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
const getPermutations = (elements: Value[]): Value[][] =>
|
|
@@ -197,11 +197,11 @@ export const createTlcOperators = (
|
|
|
197
197
|
const key = getValue(args, 0, "TLCGet");
|
|
198
198
|
if (typeof key === "number" && key >= 0) {
|
|
199
199
|
const value = runtime.registers.get(key);
|
|
200
|
-
if (value === undefined) throw new
|
|
200
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
201
201
|
return value;
|
|
202
202
|
}
|
|
203
203
|
if (typeof key !== "string") {
|
|
204
|
-
throw new
|
|
204
|
+
throw new TlaEvaluationError(
|
|
205
205
|
`The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
206
206
|
);
|
|
207
207
|
}
|
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
|
@@ -278,9 +278,13 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
|
|
|
278
278
|
(name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name);
|
|
279
279
|
};
|
|
280
280
|
|
|
281
|
-
// The variables in the order
|
|
282
|
-
//
|
|
283
|
-
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[] => {
|
|
284
288
|
const getParsed = createModuleSet(source, loadModule);
|
|
285
289
|
const getUnits = (unit: ParsedModule) => {
|
|
286
290
|
const tokens = unit.tokens;
|
|
@@ -400,7 +404,7 @@ export const getTlcVariableOrder = (source: string, loadModule: ModuleLoader): s
|
|
|
400
404
|
};
|
|
401
405
|
return getContext(undefined)
|
|
402
406
|
.enumerate()
|
|
403
|
-
.filter((entry) => entry.kind ===
|
|
407
|
+
.filter((entry) => entry.kind === kind)
|
|
404
408
|
.map((entry) => entry.name)
|
|
405
409
|
.reverse();
|
|
406
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[]) => {
|