tla-ts 0.0.3-dev.5ea4534 → 0.0.3-dev.62cf3d1
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.d.cts +45 -22
- package/dist/index.d.ts +45 -22
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +1 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +30 -21
- package/dist/transfer.js +30 -21
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- 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 +55 -19
- package/src/errors.ts +78 -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 +63 -28
- 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 +21 -10
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +56 -9
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +54 -45
- 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/spec-description.ts
CHANGED
|
@@ -7,13 +7,12 @@ import {
|
|
|
7
7
|
} from "./ast";
|
|
8
8
|
import type { ModelConfig } from "./config";
|
|
9
9
|
import type { InstanceAction } from "./environment";
|
|
10
|
-
import { TlaError } from "./errors";
|
|
10
|
+
import { formatRange, TlaError } from "./errors";
|
|
11
11
|
import {
|
|
12
12
|
bindDefinitions,
|
|
13
13
|
bindOperatorArguments,
|
|
14
14
|
evaluate,
|
|
15
15
|
forEachBinding,
|
|
16
|
-
formatRange,
|
|
17
16
|
getDefinition,
|
|
18
17
|
isConstantLevel,
|
|
19
18
|
lookupScope,
|
package/src/standard-modules.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
getNumber,
|
|
12
12
|
getOperator,
|
|
13
13
|
getSet,
|
|
14
|
-
|
|
14
|
+
getArithmeticSignature,
|
|
15
15
|
getJavaInteger,
|
|
16
16
|
getValue,
|
|
17
17
|
isOutOfRange,
|
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
isEnumerable,
|
|
41
41
|
isFiniteSet,
|
|
42
42
|
isFunctionValue,
|
|
43
|
+
isReducible,
|
|
43
44
|
isSetValue,
|
|
44
45
|
toFiniteSet,
|
|
45
46
|
unionSorted,
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
createEnumeratedSet,
|
|
48
49
|
EMPTY_SET,
|
|
49
50
|
isEnumeratedSet,
|
|
50
|
-
type
|
|
51
|
+
type ReducibleSet,
|
|
51
52
|
type MixedSetValue,
|
|
52
53
|
type SetOperationValue,
|
|
53
54
|
type SetValue,
|
|
@@ -75,12 +76,6 @@ const unionInTree = (sets: SetValue[], start: number, end: number): SetValue =>
|
|
|
75
76
|
|
|
76
77
|
const SET_OPERATORS: SetOperationValue["operator"][] = ["\\cup", "\\cap", "\\"];
|
|
77
78
|
|
|
78
|
-
type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
79
|
-
|
|
80
|
-
const isReducible = (value: Value): value is ReducibleSet =>
|
|
81
|
-
typeof value === "object" &&
|
|
82
|
-
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
83
|
-
|
|
84
79
|
const isMixedSet = (value: Value) => typeof value === "object" && value.kind === "MixedSet";
|
|
85
80
|
|
|
86
81
|
const getReducibleElements = (set: ReducibleSet) =>
|
|
@@ -128,7 +123,7 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
128
123
|
),
|
|
129
124
|
defineOperator("\\subseteq", 2, (args) => {
|
|
130
125
|
const left = getSet(args, 0, "\\subseteq");
|
|
131
|
-
const right =
|
|
126
|
+
const right = getValue(args, 1, "\\subseteq");
|
|
132
127
|
const isPowerSets =
|
|
133
128
|
typeof left === "object" &&
|
|
134
129
|
left.kind === "Subset" &&
|
|
@@ -140,8 +135,8 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
140
135
|
}),
|
|
141
136
|
...SET_OPERATORS.map((operator) =>
|
|
142
137
|
defineOperator(operator, 2, (args) => {
|
|
143
|
-
const left =
|
|
144
|
-
const right =
|
|
138
|
+
const left = getValue(args, 0, operator);
|
|
139
|
+
const right = getValue(args, 1, operator);
|
|
145
140
|
if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
|
|
146
141
|
if (operator === "\\cup" && isEmptySet(left)) return right;
|
|
147
142
|
const merge =
|
|
@@ -150,7 +145,13 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
150
145
|
: operator === "\\cap"
|
|
151
146
|
? intersectSorted
|
|
152
147
|
: differenceSorted;
|
|
153
|
-
|
|
148
|
+
// Merging compares elements across the sets. Where that fails, TLC's element-by-element
|
|
149
|
+
// membership test fails or succeeds on its own.
|
|
150
|
+
try {
|
|
151
|
+
return merge(toFiniteSet(left), toFiniteSet(right));
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
154
|
+
}
|
|
154
155
|
}
|
|
155
156
|
if (isReducible(left)) return combineReducible(operator, left, right);
|
|
156
157
|
if (operator !== "\\" && isReducible(right)) return combineReducible(operator, right, left);
|
|
@@ -184,51 +185,59 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
184
185
|
}),
|
|
185
186
|
];
|
|
186
187
|
|
|
187
|
-
const
|
|
188
|
+
const NEG_SIGNATURE = getArithmeticSignature("Integers", "Neg", "IntValue", 1);
|
|
188
189
|
|
|
189
|
-
|
|
190
|
+
// TLC overrides the operators of Naturals with the Java class of Integers when a spec loads
|
|
191
|
+
// Integers, which names it in their errors.
|
|
192
|
+
const getArithmeticOperators = (javaClass: "Naturals" | "Integers") => {
|
|
193
|
+
const dotDotSignature = getArithmeticSignature(javaClass, "DotDot", "IntervalValue", 2);
|
|
194
|
+
return [
|
|
195
|
+
defineArithmetic(javaClass, "+", "Plus", (left, right) => left + right),
|
|
196
|
+
defineArithmetic(javaClass, "-", "Minus", (left, right) => left - right),
|
|
197
|
+
defineArithmetic(javaClass, "*", "Times", (left, right) => left * right),
|
|
198
|
+
defineArithmetic(javaClass, "^", "Expt", (base, exponent) => {
|
|
199
|
+
if (exponent < 0) {
|
|
200
|
+
throw new TlaModuleError(
|
|
201
|
+
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
205
|
+
return base ** exponent;
|
|
206
|
+
}),
|
|
207
|
+
defineArithmetic(javaClass, "\\div", "Divide", (left, right) => {
|
|
208
|
+
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
209
|
+
const quotient = Math.floor(left / right);
|
|
210
|
+
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
211
|
+
return quotient;
|
|
212
|
+
}),
|
|
213
|
+
defineArithmetic(javaClass, "%", "Mod", (left, right) => {
|
|
214
|
+
if (right <= 0) {
|
|
215
|
+
throw new TlaModuleError(
|
|
216
|
+
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return ((left % right) + right) % right;
|
|
220
|
+
}),
|
|
221
|
+
defineOperator("..", 2, (args) => ({
|
|
222
|
+
kind: "Interval",
|
|
223
|
+
low: getJavaInteger(getValue(args, 0, ".."), dotDotSignature),
|
|
224
|
+
high: getJavaInteger(getValue(args, 1, ".."), dotDotSignature),
|
|
225
|
+
})),
|
|
226
|
+
];
|
|
227
|
+
};
|
|
190
228
|
|
|
191
229
|
const NATURALS_OPERATORS = [
|
|
192
230
|
defineConstant("Nat", { kind: "InfiniteSet", name: "Nat" }),
|
|
193
|
-
|
|
194
|
-
defineArithmetic("-", "Minus", (left, right) => left - right),
|
|
195
|
-
defineArithmetic("*", "Times", (left, right) => left * right),
|
|
196
|
-
defineArithmetic("^", "Expt", (base, exponent) => {
|
|
197
|
-
if (exponent < 0) {
|
|
198
|
-
throw new TlaModuleError(
|
|
199
|
-
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
203
|
-
return base ** exponent;
|
|
204
|
-
}),
|
|
205
|
-
defineArithmetic("\\div", "Divide", (left, right) => {
|
|
206
|
-
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
207
|
-
const quotient = Math.floor(left / right);
|
|
208
|
-
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
209
|
-
return quotient;
|
|
210
|
-
}),
|
|
211
|
-
defineArithmetic("%", "Mod", (left, right) => {
|
|
212
|
-
if (right <= 0) {
|
|
213
|
-
throw new TlaModuleError(
|
|
214
|
-
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return ((left % right) + right) % right;
|
|
218
|
-
}),
|
|
231
|
+
...getArithmeticOperators("Naturals"),
|
|
219
232
|
defineComparison("<", (left, right) => left < right),
|
|
220
233
|
defineComparison(">", (left, right) => left > right),
|
|
221
234
|
defineComparison("<=", (left, right) => left <= right),
|
|
222
235
|
defineComparison(">=", (left, right) => left >= right),
|
|
223
|
-
defineOperator("..", 2, (args) => ({
|
|
224
|
-
kind: "Interval",
|
|
225
|
-
low: getJavaInteger(getValue(args, 0, ".."), DOT_DOT_SIGNATURE),
|
|
226
|
-
high: getJavaInteger(getValue(args, 1, ".."), DOT_DOT_SIGNATURE),
|
|
227
|
-
})),
|
|
228
236
|
];
|
|
229
237
|
|
|
230
238
|
const INTEGERS_OPERATORS = [
|
|
231
239
|
defineConstant("Int", { kind: "InfiniteSet", name: "Int" }),
|
|
240
|
+
...getArithmeticOperators("Integers"),
|
|
232
241
|
defineOperator("-.", 1, (args) => {
|
|
233
242
|
const value = getJavaInteger(getValue(args, 0, "-"), NEG_SIGNATURE);
|
|
234
243
|
if (isOutOfRange(-value)) throw createOverflowError(`-${value}`);
|
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 [];
|