tla-ts 0.0.3-dev.a50fc89 → 0.0.3-dev.a85c0bd
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 +22 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +32 -22
- package/dist/index.d.ts +32 -22
- 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 +18 -17
- package/dist/run-cli.js +14 -13
- package/dist/transfer.cjs +30 -15
- package/dist/transfer.js +30 -15
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +314 -80
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +94 -24
- package/src/errors.ts +35 -2
- package/src/evaluate.ts +126 -148
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +116 -543
- package/src/parallel-worker.ts +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +12 -1
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +58 -13
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +99 -25
- 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/values.ts +128 -24
package/src/successors.ts
CHANGED
|
@@ -1,28 +1,36 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type QuantifierExpression,
|
|
8
|
+
} from "./ast";
|
|
2
9
|
import { getReadVariables } from "./dependencies";
|
|
3
|
-
import { TlaEvaluationError } from "./errors";
|
|
10
|
+
import { TlaEvaluationError, TlaIncompleteStateError } from "./errors";
|
|
4
11
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
5
12
|
import {
|
|
6
13
|
describeLocation,
|
|
14
|
+
describeRange,
|
|
7
15
|
bindArgument,
|
|
8
16
|
bindDefinitions,
|
|
9
17
|
bindOperatorArguments,
|
|
10
18
|
bindValue,
|
|
11
19
|
compileArgumentScope,
|
|
12
20
|
evaluate,
|
|
21
|
+
getEntryEvaluator,
|
|
13
22
|
expectBoolean,
|
|
14
23
|
forEachBinding,
|
|
15
24
|
getBoundsDomain,
|
|
16
|
-
getChildren,
|
|
17
25
|
getDefinition,
|
|
18
26
|
getEnumeratedElements,
|
|
19
27
|
getReevaluatedDefinitions,
|
|
20
28
|
lookupScope,
|
|
21
29
|
resolveApplyOperator,
|
|
22
30
|
resolveGlobalOperator,
|
|
23
|
-
isLocallyResolved,
|
|
24
31
|
type Context,
|
|
25
32
|
type Environment,
|
|
33
|
+
type Evaluator,
|
|
26
34
|
type Scope,
|
|
27
35
|
type State,
|
|
28
36
|
} from "./evaluate";
|
|
@@ -81,15 +89,25 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
81
89
|
return copy;
|
|
82
90
|
};
|
|
83
91
|
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
// TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
|
|
93
|
+
const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
|
|
86
94
|
if (typeof value === "boolean") return value;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
95
|
+
throw new TlaEvaluationError(
|
|
96
|
+
`In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
101
|
+
// TLC rejects a number in the next-state relation when it splits the relation into actions.
|
|
102
|
+
if (context.next !== undefined && expression.kind === "Number") {
|
|
103
|
+
throw setExitCode(
|
|
104
|
+
new TlaEvaluationError(
|
|
105
|
+
`The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
|
|
106
|
+
),
|
|
107
|
+
EXIT_STATUS.error,
|
|
108
|
+
);
|
|
92
109
|
}
|
|
110
|
+
return expectFormula(expression, value, context.next === undefined);
|
|
93
111
|
};
|
|
94
112
|
|
|
95
113
|
const isAssignmentOperator = (operator: string) =>
|
|
@@ -287,12 +305,34 @@ const toConjunct = (
|
|
|
287
305
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
288
306
|
};
|
|
289
307
|
|
|
308
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
309
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
310
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
311
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
312
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
313
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
314
|
+
);
|
|
315
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
316
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
317
|
+
const operands = expression.body.operands;
|
|
318
|
+
const index = operands.findIndex((operand) => {
|
|
319
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
320
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
321
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
322
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
323
|
+
});
|
|
324
|
+
const count = index === -1 ? operands.length : index;
|
|
325
|
+
if (count === 0) return undefined;
|
|
326
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
327
|
+
};
|
|
328
|
+
|
|
290
329
|
const compileConjunction = (
|
|
291
330
|
operands: Expression[],
|
|
292
331
|
environment: Environment,
|
|
293
332
|
isInit: boolean,
|
|
294
333
|
): Runner => {
|
|
295
334
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
335
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
296
336
|
const runFrom = (
|
|
297
337
|
start: number,
|
|
298
338
|
run: Run,
|
|
@@ -315,12 +355,17 @@ const compileConjunction = (
|
|
|
315
355
|
const conjunct = conjuncts[index];
|
|
316
356
|
if (conjunct.kind === "Guard") {
|
|
317
357
|
context ??= getContext(run, scope, current);
|
|
318
|
-
|
|
358
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
359
|
+
conjunct.expression,
|
|
360
|
+
environment,
|
|
361
|
+
));
|
|
362
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
319
363
|
continue;
|
|
320
364
|
}
|
|
321
365
|
if (conjunct.kind === "Assign") {
|
|
322
366
|
context ??= getContext(run, scope, current);
|
|
323
|
-
const
|
|
367
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
368
|
+
const value = evaluator(context);
|
|
324
369
|
const assigned = current[conjunct.index];
|
|
325
370
|
if (assigned === undefined) {
|
|
326
371
|
current = assign(current, conjunct.index, value);
|
|
@@ -357,10 +402,7 @@ const compileConjunction = (
|
|
|
357
402
|
};
|
|
358
403
|
|
|
359
404
|
const holdsInInit = (expression: Expression, context: Context) =>
|
|
360
|
-
|
|
361
|
-
evaluate(expression, context),
|
|
362
|
-
`the initial predicate at ${describeLocation(expression)}`,
|
|
363
|
-
);
|
|
405
|
+
expectFormula(expression, evaluate(expression, context), true);
|
|
364
406
|
|
|
365
407
|
const compileGuard =
|
|
366
408
|
(expression: Expression): Runner =>
|
|
@@ -385,7 +427,7 @@ const compileMembership = (
|
|
|
385
427
|
expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
|
|
386
428
|
if (!isEnumerable(value)) {
|
|
387
429
|
throw new TlaEvaluationError(
|
|
388
|
-
`In computing next states, the right side of \\
|
|
430
|
+
`In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
|
|
389
431
|
);
|
|
390
432
|
}
|
|
391
433
|
const elements = getStreamedElements(value);
|
|
@@ -729,6 +771,33 @@ const compileAction = (
|
|
|
729
771
|
}
|
|
730
772
|
};
|
|
731
773
|
}
|
|
774
|
+
const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
|
|
775
|
+
if (hoisted) {
|
|
776
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
777
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
778
|
+
const context = getContext(run, scope, partial);
|
|
779
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
780
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
781
|
+
);
|
|
782
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
783
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
784
|
+
if (index < 0) {
|
|
785
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
for (const element of sets[index]) {
|
|
789
|
+
if (run.isStopped) return;
|
|
790
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
791
|
+
if (index === sets.length - 1) {
|
|
792
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
793
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
794
|
+
}
|
|
795
|
+
visit(index - 1, elementScope);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
visit(sets.length - 1, scope);
|
|
799
|
+
};
|
|
800
|
+
}
|
|
732
801
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
733
802
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
734
803
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
@@ -804,7 +873,9 @@ const compileAction = (
|
|
|
804
873
|
);
|
|
805
874
|
const body = arm?.body ?? other;
|
|
806
875
|
if (!body)
|
|
807
|
-
throw new TlaEvaluationError(
|
|
876
|
+
throw new TlaEvaluationError(
|
|
877
|
+
`In computing ${run.isInit ? "initial" : "next"} states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
878
|
+
);
|
|
808
879
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
809
880
|
};
|
|
810
881
|
}
|
|
@@ -913,18 +984,19 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
913
984
|
);
|
|
914
985
|
}
|
|
915
986
|
const environment = context.environment;
|
|
916
|
-
const
|
|
987
|
+
const run: Run = { environment, isInit: false, current: state, isStopped: false };
|
|
988
|
+
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
917
989
|
getRunner(expression, environment, false)(
|
|
918
|
-
|
|
990
|
+
run,
|
|
919
991
|
context.scope,
|
|
920
992
|
getEmptyState(environment),
|
|
921
993
|
undefined,
|
|
922
994
|
false,
|
|
923
995
|
() => {
|
|
924
|
-
|
|
996
|
+
run.isStopped = true;
|
|
925
997
|
},
|
|
926
998
|
);
|
|
927
|
-
return
|
|
999
|
+
return run.isStopped;
|
|
928
1000
|
};
|
|
929
1001
|
|
|
930
1002
|
export const forEachInitialState = (
|
|
@@ -993,8 +1065,10 @@ export const forEachSuccessor = (
|
|
|
993
1065
|
(partial, action) => {
|
|
994
1066
|
if (run.isStopped) return;
|
|
995
1067
|
if (!isComplete(partial)) {
|
|
996
|
-
throw new
|
|
997
|
-
|
|
1068
|
+
throw new TlaIncompleteStateError(
|
|
1069
|
+
partial,
|
|
1070
|
+
action,
|
|
1071
|
+
getMissingVariables(partial, environment).sort(),
|
|
998
1072
|
);
|
|
999
1073
|
}
|
|
1000
1074
|
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,
|
|
@@ -9,6 +10,7 @@ import {
|
|
|
9
10
|
isEnumerable,
|
|
10
11
|
isFunctionValue,
|
|
11
12
|
isModelValue,
|
|
13
|
+
isSameValue,
|
|
12
14
|
isSetValue,
|
|
13
15
|
toFiniteFunction,
|
|
14
16
|
toFiniteSet,
|
|
@@ -46,9 +48,13 @@ const composePermutations = (first: Images, second: Images) => {
|
|
|
46
48
|
return composed;
|
|
47
49
|
};
|
|
48
50
|
|
|
49
|
-
|
|
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) => {
|
|
50
56
|
if (!isFunctionValue(value)) {
|
|
51
|
-
throw
|
|
57
|
+
throw createSymmetryError("The symmetry values must be function records.");
|
|
52
58
|
}
|
|
53
59
|
const functionValue = toFiniteFunction(value);
|
|
54
60
|
const permutation: Images = new Map();
|
|
@@ -64,8 +70,13 @@ const toImages = (value: Value, name: string) => {
|
|
|
64
70
|
|
|
65
71
|
export const getSymmetryGroup = (name: string, environment: Environment): Permutation[] => {
|
|
66
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
|
+
}
|
|
67
78
|
if (definition?.kind !== "Operator" || definition.params.length > 0) {
|
|
68
|
-
throw
|
|
79
|
+
throw createSymmetryError(`The symmetry function ${name} must specify a set of permutations.`);
|
|
69
80
|
}
|
|
70
81
|
const value = evaluate(definition.body, {
|
|
71
82
|
environment,
|
|
@@ -74,9 +85,9 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
74
85
|
next: undefined,
|
|
75
86
|
});
|
|
76
87
|
if (!isSetValue(value) || !isEnumerable(value)) {
|
|
77
|
-
throw
|
|
88
|
+
throw createSymmetryError("The symmetry operator must specify a set of functions.");
|
|
78
89
|
}
|
|
79
|
-
const permutations = toFiniteSet(value).elements.map(
|
|
90
|
+
const permutations = toFiniteSet(value).elements.map(toImages);
|
|
80
91
|
const isDisabled =
|
|
81
92
|
definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
|
|
82
93
|
if (isDisabled) return [];
|
|
@@ -163,7 +174,10 @@ const permuteValue = (value: Value, permutation: Permutation): Value => {
|
|
|
163
174
|
if (size === 0 || size > CACHED_SIZE_LIMIT) return computePermutedValue(value, permutation);
|
|
164
175
|
const cached = permutation.cache.get(value);
|
|
165
176
|
if (cached !== undefined) return cached;
|
|
166
|
-
const
|
|
177
|
+
const computed = computePermutedValue(value, permutation);
|
|
178
|
+
// A permutation often maps a value to an equal one. Keeping the original lets the comparison
|
|
179
|
+
// with the smallest state so far stop at identity.
|
|
180
|
+
const permuted = computed !== value && isSameValue(computed, value) ? value : computed;
|
|
167
181
|
if (permutation.cacheSize >= permutation.cacheLimit) {
|
|
168
182
|
permutation.cache = new WeakMap();
|
|
169
183
|
permutation.cacheSize = 0;
|
|
@@ -184,7 +198,15 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
|
184
198
|
const domain = permuteElements(functionValue.domain, permutation);
|
|
185
199
|
const range = permuteElements(functionValue.range, permutation) ?? functionValue.range;
|
|
186
200
|
if (domain) {
|
|
187
|
-
|
|
201
|
+
const permuted = createFunction(
|
|
202
|
+
domain.map((key, index): [Value, Value] => [key, range[index]]),
|
|
203
|
+
);
|
|
204
|
+
// A permutation often maps the domain to itself, such as a function over the symmetric set.
|
|
205
|
+
// Sharing the original domain array lets comparisons with other images skip the domain.
|
|
206
|
+
const isSameDomain = permuted.domain.every(
|
|
207
|
+
(key, index) => key === functionValue.domain[index],
|
|
208
|
+
);
|
|
209
|
+
return isSameDomain ? createSortedFunction(functionValue.domain, permuted.range) : permuted;
|
|
188
210
|
}
|
|
189
211
|
return range === functionValue.range
|
|
190
212
|
? functionValue
|
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-ext.ts
CHANGED
|
@@ -10,16 +10,8 @@ import {
|
|
|
10
10
|
} from "./builtins";
|
|
11
11
|
import { TlaEvaluationError, TlaModuleError, TlaUnsupportedError } from "./errors";
|
|
12
12
|
import { getTlcFingerprint } from "./tlc-random";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
expectBoolean,
|
|
16
|
-
getChildren,
|
|
17
|
-
getDefinition,
|
|
18
|
-
isLocallyResolved,
|
|
19
|
-
type Builtin,
|
|
20
|
-
type Environment,
|
|
21
|
-
} from "./evaluate";
|
|
22
|
-
import type { Definition, Expression } from "./ast";
|
|
13
|
+
import { evaluate, expectBoolean, getDefinition, type Builtin, type Environment } from "./evaluate";
|
|
14
|
+
import { getChildren, isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
23
15
|
import type { TraceStep } from "./check";
|
|
24
16
|
import {
|
|
25
17
|
getNamedRegisters,
|
|
@@ -111,15 +103,35 @@ const getRegister = (runtime: CheckerRuntime, key: Value) => {
|
|
|
111
103
|
return undefined;
|
|
112
104
|
};
|
|
113
105
|
|
|
114
|
-
|
|
106
|
+
// TLC's TLCSet. TLCExt's TLCGetAndSet is a TLA+ definition that calls it.
|
|
107
|
+
export const setTlcValue = (
|
|
108
|
+
runtime: CheckerRuntime,
|
|
109
|
+
print: (text: string) => void,
|
|
110
|
+
key: Value,
|
|
111
|
+
value: Value,
|
|
112
|
+
) => {
|
|
115
113
|
if (typeof key === "number" && key >= 0) {
|
|
116
114
|
runtime.registers.set(key, value);
|
|
117
115
|
return;
|
|
118
116
|
}
|
|
117
|
+
if (key === "exit") {
|
|
118
|
+
if (value === true) runtime.isStopped = true;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (key === "pause") {
|
|
122
|
+
if (value !== true) return;
|
|
123
|
+
print("Press enter to resume model checking.");
|
|
124
|
+
runtime.pause();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
119
127
|
if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
120
128
|
runtime.namedRegisters.set(key, value);
|
|
121
129
|
return;
|
|
122
130
|
}
|
|
131
|
+
if (typeof key === "string" && key.startsWith("-D")) {
|
|
132
|
+
runtime.properties.set(key.slice(2), formatValue(value));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
123
135
|
throw new TlaModuleError(
|
|
124
136
|
`The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
125
137
|
);
|
|
@@ -203,7 +215,12 @@ export const createTlcExtOperators = (
|
|
|
203
215
|
const key = getValue(args, 0, "TLCGetAndSet");
|
|
204
216
|
const previous = getRegister(runtime, key) ?? getValue(args, 3, "TLCGetAndSet");
|
|
205
217
|
const update = getOperator(args, 1, "TLCGetAndSet");
|
|
206
|
-
|
|
218
|
+
setTlcValue(
|
|
219
|
+
runtime,
|
|
220
|
+
print,
|
|
221
|
+
key,
|
|
222
|
+
update.call([previous, getValue(args, 2, "TLCGetAndSet")]),
|
|
223
|
+
);
|
|
207
224
|
return previous;
|
|
208
225
|
}),
|
|
209
226
|
isStateLevel: true,
|
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
|
};
|