tla-ts 0.0.1 → 0.0.3-dev.2032e26
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 +95 -40
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +216 -30
- package/dist/index.d.ts +216 -30
- 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 +20 -1
- package/dist/run-cli.js +20 -1
- package/dist/transfer.cjs +392 -39
- package/dist/transfer.js +392 -39
- package/package.json +6 -2
- package/src/ast.ts +122 -5
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +950 -238
- package/src/cli-options.ts +58 -0
- package/src/codegen.ts +52 -18
- package/src/config.ts +39 -11
- package/src/environment.ts +485 -140
- package/src/errors.ts +64 -1
- package/src/evaluate.ts +492 -138
- package/src/exit-codes.ts +75 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +221 -66
- package/src/java-overrides.ts +241 -0
- package/src/json.ts +153 -72
- package/src/levels.ts +850 -0
- package/src/lexer.ts +108 -29
- package/src/liveness.ts +47 -91
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +186 -201
- package/src/parallel.ts +176 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +472 -41
- package/src/run-cli.ts +427 -61
- package/src/runtime.ts +63 -28
- package/src/spec-description.ts +509 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +223 -92
- package/src/subexpressions.ts +21 -35
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +266 -48
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +43 -37
- package/src/tlc-ext.ts +120 -33
- package/src/tlc-module.ts +71 -47
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +411 -0
- package/src/tlc-summary.ts +109 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1234 -176
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { Declaration, Definition, Expression, SourceLocation } from "./ast";
|
|
2
|
+
import { TlaSyntaxError } from "./errors";
|
|
3
|
+
import {
|
|
4
|
+
createLevelAnalyzer,
|
|
5
|
+
getArgumentKey,
|
|
6
|
+
MAX_LEVEL,
|
|
7
|
+
parseArgumentParamKey,
|
|
8
|
+
putArgConstraint,
|
|
9
|
+
putConstraint,
|
|
10
|
+
type OperatorLevels,
|
|
11
|
+
type Scope,
|
|
12
|
+
} from "./levels";
|
|
13
|
+
|
|
14
|
+
export interface InstanceCheck {
|
|
15
|
+
module: string;
|
|
16
|
+
instancer: string;
|
|
17
|
+
lets: Definition[];
|
|
18
|
+
substitutions: Map<string, Expression>;
|
|
19
|
+
location: SourceLocation;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ModuleContext {
|
|
23
|
+
constants: Declaration[];
|
|
24
|
+
variables: string[];
|
|
25
|
+
definitions: Definition[];
|
|
26
|
+
assumptions: Expression[];
|
|
27
|
+
getDefinition: (name: string) => Definition | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ModuleLevels {
|
|
31
|
+
isConstant: boolean;
|
|
32
|
+
constraints: Map<string, number>;
|
|
33
|
+
argConstraints: Map<string, number>;
|
|
34
|
+
argParams: Set<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Substituted {
|
|
38
|
+
name: string;
|
|
39
|
+
maximumLevel: number;
|
|
40
|
+
arity: number;
|
|
41
|
+
level: number;
|
|
42
|
+
operator: OperatorLevels | undefined;
|
|
43
|
+
isLeibniz: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const getMaxLevel = (operator: OperatorLevels, index: number) =>
|
|
47
|
+
operator.maxLevels[index] ?? MAX_LEVEL;
|
|
48
|
+
|
|
49
|
+
export const checkInstances = (
|
|
50
|
+
checks: InstanceCheck[],
|
|
51
|
+
getModule: (name: string) => ModuleContext,
|
|
52
|
+
) => {
|
|
53
|
+
const analyzers = new Map<string, ReturnType<typeof createLevelAnalyzer>>();
|
|
54
|
+
const getAnalyzer = (name: string) => {
|
|
55
|
+
const cached = analyzers.get(name);
|
|
56
|
+
if (cached) return cached;
|
|
57
|
+
const module = getModule(name);
|
|
58
|
+
const analyzer = createLevelAnalyzer({
|
|
59
|
+
constants: new Set(module.constants.map((constant) => constant.name)),
|
|
60
|
+
variables: new Set(module.variables),
|
|
61
|
+
getDefinition: module.getDefinition,
|
|
62
|
+
});
|
|
63
|
+
analyzers.set(name, analyzer);
|
|
64
|
+
return analyzer;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const moduleLevels = new Map<string, ModuleLevels>();
|
|
68
|
+
const getModuleLevels = (name: string) => {
|
|
69
|
+
const cached = moduleLevels.get(name);
|
|
70
|
+
if (cached) return cached;
|
|
71
|
+
const module = getModule(name);
|
|
72
|
+
const analyzer = getAnalyzer(name);
|
|
73
|
+
const definitions = module.definitions.map((definition) =>
|
|
74
|
+
analyzer.analyzeDefinition(definition, new Map()),
|
|
75
|
+
);
|
|
76
|
+
const levels: ModuleLevels = {
|
|
77
|
+
isConstant:
|
|
78
|
+
module.variables.length === 0 && definitions.every((definition) => definition.level === 0),
|
|
79
|
+
constraints: new Map(),
|
|
80
|
+
argConstraints: new Map(),
|
|
81
|
+
argParams: new Set(),
|
|
82
|
+
};
|
|
83
|
+
if (!levels.isConstant) {
|
|
84
|
+
module.constants.forEach((constant) => putConstraint(levels.constraints, constant.name, 0));
|
|
85
|
+
}
|
|
86
|
+
const addLevels = (
|
|
87
|
+
data: Omit<ModuleLevels, "isConstant">,
|
|
88
|
+
isKept: (operator: string, param: string) => boolean,
|
|
89
|
+
) => {
|
|
90
|
+
data.constraints.forEach((value, key) => putConstraint(levels.constraints, key, value));
|
|
91
|
+
data.argConstraints.forEach((value, key) =>
|
|
92
|
+
putArgConstraint(levels.argConstraints, key, value),
|
|
93
|
+
);
|
|
94
|
+
data.argParams.forEach((key) => {
|
|
95
|
+
const parameter = parseArgumentParamKey(key);
|
|
96
|
+
if (isKept(parameter.operator, parameter.param)) levels.argParams.add(key);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
definitions.forEach((definition) =>
|
|
100
|
+
addLevels(definition, (operator, param) =>
|
|
101
|
+
definition.formals.every((formal) => formal.name !== operator && formal.name !== param),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
module.assumptions.forEach((assumption) =>
|
|
105
|
+
addLevels(analyzer.analyze(assumption, new Map()), () => true),
|
|
106
|
+
);
|
|
107
|
+
moduleLevels.set(name, levels);
|
|
108
|
+
return levels;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const messages = new Set<string>();
|
|
112
|
+
checks.forEach((check) => {
|
|
113
|
+
const location = `at line ${check.location.line}, column ${check.location.column}`;
|
|
114
|
+
const addMessage = (message: string) => messages.add(`${message} ${location}`);
|
|
115
|
+
const instancee = getModule(check.module);
|
|
116
|
+
const levels = getModuleLevels(check.module);
|
|
117
|
+
const analyzer = getAnalyzer(check.instancer);
|
|
118
|
+
const scope: Scope = new Map();
|
|
119
|
+
check.lets.forEach((definition) =>
|
|
120
|
+
scope.set(definition.name, { kind: "Let", definition, scope }),
|
|
121
|
+
);
|
|
122
|
+
const declarations = [
|
|
123
|
+
...instancee.constants.map((constant) => ({ ...constant, maximumLevel: 0 })),
|
|
124
|
+
...instancee.variables.map((variable) => ({ name: variable, arity: 0, maximumLevel: 1 })),
|
|
125
|
+
];
|
|
126
|
+
const substituted = declarations.flatMap((declaration): Substituted[] => {
|
|
127
|
+
const expression = check.substitutions.get(declaration.name);
|
|
128
|
+
if (!expression) return [];
|
|
129
|
+
if (declaration.arity === 0) {
|
|
130
|
+
const data = analyzer.analyze(expression, scope);
|
|
131
|
+
return [{ ...declaration, level: data.level, operator: undefined, isLeibniz: true }];
|
|
132
|
+
}
|
|
133
|
+
const operand = analyzer.toOperand(expression, scope);
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
...declaration,
|
|
137
|
+
level: operand.data.level,
|
|
138
|
+
operator: operand.operator,
|
|
139
|
+
isLeibniz: operand.operator === undefined || operand.operator.nonLeibniz.size === 0,
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
});
|
|
143
|
+
const getLevelMessage = (name: string, level: number) =>
|
|
144
|
+
`Level error in instantiating module '${check.module}': the level of the expression or operator substituted for '${name}' must be at most ${level}`;
|
|
145
|
+
substituted.forEach((item) => {
|
|
146
|
+
if (!levels.isConstant && item.level > item.maximumLevel) {
|
|
147
|
+
addMessage(getLevelMessage(item.name, item.maximumLevel));
|
|
148
|
+
}
|
|
149
|
+
if (!item.isLeibniz) {
|
|
150
|
+
addMessage(
|
|
151
|
+
`Error in instantiating module '${check.module}': a non-Leibniz operator substituted for '${item.name}'`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
substituted.forEach((item) => {
|
|
156
|
+
const maximum = levels.constraints.get(item.name);
|
|
157
|
+
if (maximum !== undefined && item.level > maximum)
|
|
158
|
+
addMessage(getLevelMessage(item.name, maximum));
|
|
159
|
+
const operator = item.operator;
|
|
160
|
+
if (!operator) return;
|
|
161
|
+
Array.from({ length: item.arity }, (_, index) => {
|
|
162
|
+
const minimum = levels.argConstraints.get(getArgumentKey(item.name, index));
|
|
163
|
+
if (minimum !== undefined && getMaxLevel(operator, index) < minimum) {
|
|
164
|
+
addMessage(
|
|
165
|
+
`Level error in instantiating module '${check.module}': the level of the argument ${index + 1} of the operator ${operator.name} must be at least ${minimum}`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
levels.argParams.forEach((key) => {
|
|
171
|
+
const parameter = parseArgumentParamKey(key);
|
|
172
|
+
const operator = substituted.find((item) => item.name === parameter.operator)?.operator;
|
|
173
|
+
const argument = substituted.find((item) => item.name === parameter.param);
|
|
174
|
+
if (!operator || !argument) return;
|
|
175
|
+
const maximum = getMaxLevel(operator, parameter.index);
|
|
176
|
+
if (argument.level > maximum) {
|
|
177
|
+
addMessage(
|
|
178
|
+
`Level error when instantiating module '${check.module}': the level of the argument ${parameter.index} of the operator ${parameter.operator}' must be at most ${maximum}`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
if (messages.size > 0) throw new TlaSyntaxError([...messages].join("\n"));
|
|
184
|
+
};
|
package/src/successors.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { Bound, Definition, Expression } from "./ast";
|
|
2
|
-
import {
|
|
2
|
+
import { getReadVariables } from "./dependencies";
|
|
3
|
+
import { TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
4
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
5
|
import {
|
|
6
|
+
describeLocation,
|
|
7
|
+
describeRange,
|
|
4
8
|
bindArgument,
|
|
5
9
|
bindDefinitions,
|
|
6
10
|
bindOperatorArguments,
|
|
@@ -10,7 +14,10 @@ import {
|
|
|
10
14
|
expectBoolean,
|
|
11
15
|
forEachBinding,
|
|
12
16
|
getBoundsDomain,
|
|
17
|
+
getChildren,
|
|
13
18
|
getDefinition,
|
|
19
|
+
getEnumeratedElements,
|
|
20
|
+
getReevaluatedDefinitions,
|
|
14
21
|
lookupScope,
|
|
15
22
|
resolveApplyOperator,
|
|
16
23
|
resolveGlobalOperator,
|
|
@@ -20,12 +27,13 @@ import {
|
|
|
20
27
|
type Scope,
|
|
21
28
|
type State,
|
|
22
29
|
} from "./evaluate";
|
|
30
|
+
import { mapChildren } from "./instances";
|
|
23
31
|
import {
|
|
24
32
|
createTuple,
|
|
25
33
|
formatValue,
|
|
34
|
+
getStreamedElements,
|
|
26
35
|
isElementOf,
|
|
27
36
|
isEnumerable,
|
|
28
|
-
toFiniteSet,
|
|
29
37
|
valuesEqual,
|
|
30
38
|
type Value,
|
|
31
39
|
} from "./values";
|
|
@@ -39,6 +47,7 @@ interface Run {
|
|
|
39
47
|
environment: Environment;
|
|
40
48
|
isInit: boolean;
|
|
41
49
|
current: Value[] | undefined;
|
|
50
|
+
isStopped: boolean;
|
|
42
51
|
}
|
|
43
52
|
|
|
44
53
|
type Continuation = (partial: State, action: string | undefined) => void;
|
|
@@ -62,10 +71,7 @@ type Conjunct = { expression: Expression } & (
|
|
|
62
71
|
const initRunners = new WeakMap<Expression, Runner>();
|
|
63
72
|
const nextRunners = new WeakMap<Expression, Runner>();
|
|
64
73
|
|
|
65
|
-
const
|
|
66
|
-
`line ${expression.location.line}, column ${expression.location.column}`;
|
|
67
|
-
|
|
68
|
-
const getContext = (run: Run, scope: Scope | undefined, partial: State): Context =>
|
|
74
|
+
const getContext = (run: Run, scope: Scope | undefined, partial: State) =>
|
|
69
75
|
run.isInit
|
|
70
76
|
? { environment: run.environment, scope, state: partial, next: undefined }
|
|
71
77
|
: { environment: run.environment, scope, state: run.current, next: partial };
|
|
@@ -79,7 +85,12 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
79
85
|
const holds = (expression: Expression, context: Context) => {
|
|
80
86
|
const value = evaluate(expression, context);
|
|
81
87
|
if (typeof value === "boolean") return value;
|
|
82
|
-
|
|
88
|
+
try {
|
|
89
|
+
return expectBoolean(value, `the action at ${describeLocation(expression)}`);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// TLC fails with an unexpected exception when the next-state relation isn't a boolean.
|
|
92
|
+
throw context.next === undefined ? error : setExitCode(error, EXIT_STATUS.error);
|
|
93
|
+
}
|
|
83
94
|
};
|
|
84
95
|
|
|
85
96
|
const isAssignmentOperator = (operator: string) =>
|
|
@@ -188,16 +199,12 @@ const ACTION_KINDS = new Set([
|
|
|
188
199
|
"FunctionApplication",
|
|
189
200
|
]);
|
|
190
201
|
|
|
191
|
-
const isGuard = (expression: Expression, environment: Environment, isInit: boolean)
|
|
202
|
+
const isGuard = (expression: Expression, environment: Environment, isInit: boolean) => {
|
|
192
203
|
if (ACTION_KINDS.has(expression.kind)) return false;
|
|
193
204
|
if (expression.kind === "Identifier") {
|
|
194
|
-
|
|
195
|
-
environment.variableIndex.has(expression.name) ||
|
|
196
|
-
|
|
197
|
-
) {
|
|
198
|
-
return true;
|
|
199
|
-
}
|
|
200
|
-
return false;
|
|
205
|
+
return (
|
|
206
|
+
environment.variableIndex.has(expression.name) || environment.constants.has(expression.name)
|
|
207
|
+
);
|
|
201
208
|
}
|
|
202
209
|
if (expression.kind !== "Apply") return true;
|
|
203
210
|
if (expression.operator === "=>") return false;
|
|
@@ -212,6 +219,56 @@ const isGuard = (expression: Expression, environment: Environment, isInit: boole
|
|
|
212
219
|
return definition === undefined && environment.builtins.has(expression.operator);
|
|
213
220
|
};
|
|
214
221
|
|
|
222
|
+
const NO_NAMES: ReadonlySet<string> = new Set();
|
|
223
|
+
|
|
224
|
+
// The local names an expression reads from enclosing scopes, or undefined when it applies a local
|
|
225
|
+
// operator, whose body may assign variables.
|
|
226
|
+
const getReadLocals = (
|
|
227
|
+
expression: Expression,
|
|
228
|
+
bound: ReadonlySet<string>,
|
|
229
|
+
): string[] | undefined => {
|
|
230
|
+
if (isLocallyResolved(expression)) {
|
|
231
|
+
if (expression.kind !== "Identifier") return undefined;
|
|
232
|
+
return bound.has(expression.name) ? [] : [expression.name];
|
|
233
|
+
}
|
|
234
|
+
const names: string[] = [];
|
|
235
|
+
const result = { isReadOnly: true };
|
|
236
|
+
mapChildren(expression, (child, childNames) => {
|
|
237
|
+
const childLocals = result.isReadOnly
|
|
238
|
+
? getReadLocals(child, childNames.length === 0 ? bound : new Set([...bound, ...childNames]))
|
|
239
|
+
: [];
|
|
240
|
+
if (childLocals) names.push(...childLocals);
|
|
241
|
+
else result.isReadOnly = false;
|
|
242
|
+
return child;
|
|
243
|
+
});
|
|
244
|
+
return result.isReadOnly ? names : undefined;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// TLC generates a successor for every disjunct and witness that holds, so an expression only
|
|
248
|
+
// checks like a guard when it has neither.
|
|
249
|
+
const hasBranches = (
|
|
250
|
+
expression: Expression,
|
|
251
|
+
environment: Environment,
|
|
252
|
+
visited: Set<Definition>,
|
|
253
|
+
): boolean => {
|
|
254
|
+
if (expression.kind === "Or" || expression.kind === "Exists") return true;
|
|
255
|
+
const name =
|
|
256
|
+
expression.kind === "Identifier"
|
|
257
|
+
? expression.name
|
|
258
|
+
: expression.kind === "Apply"
|
|
259
|
+
? expression.operator
|
|
260
|
+
: undefined;
|
|
261
|
+
const definition =
|
|
262
|
+
name === undefined || isLocallyResolved(expression)
|
|
263
|
+
? undefined
|
|
264
|
+
: getDefinition(name, environment);
|
|
265
|
+
if (definition && !visited.has(definition)) {
|
|
266
|
+
visited.add(definition);
|
|
267
|
+
if (hasBranches(definition.body, environment, visited)) return true;
|
|
268
|
+
}
|
|
269
|
+
return getChildren(expression).some((child) => hasBranches(child, environment, visited));
|
|
270
|
+
};
|
|
271
|
+
|
|
215
272
|
const toConjunct = (
|
|
216
273
|
expression: Expression,
|
|
217
274
|
environment: Environment,
|
|
@@ -332,8 +389,8 @@ const compileMembership = (
|
|
|
332
389
|
`In computing next states, the right side of \\in is not enumerable: ${formatValue(value)} at ${describeLocation(expression)}`,
|
|
333
390
|
);
|
|
334
391
|
}
|
|
335
|
-
const elements =
|
|
336
|
-
for (let position = 0; position < elements.length; position++) {
|
|
392
|
+
const elements = getStreamedElements(value);
|
|
393
|
+
for (let position = 0; position < elements.length && !run.isStopped; position++) {
|
|
337
394
|
onComplete(assign(partial, index, elements[position]), action);
|
|
338
395
|
}
|
|
339
396
|
};
|
|
@@ -542,6 +599,104 @@ const compileIdentifier = (
|
|
|
542
599
|
};
|
|
543
600
|
};
|
|
544
601
|
|
|
602
|
+
// A PlusCal action starts with a guard such as pc[self] = "L1", and a process is a disjunction of
|
|
603
|
+
// its actions. The labels an expression requires of one variable at one bound name let a
|
|
604
|
+
// disjunction skip the operands whose guard is false without evaluating them.
|
|
605
|
+
interface LabelGuard {
|
|
606
|
+
variable: number;
|
|
607
|
+
argument: string;
|
|
608
|
+
labels: Set<string>;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const getLabelGuard = (
|
|
612
|
+
expression: Expression,
|
|
613
|
+
environment: Environment,
|
|
614
|
+
visited: Set<Expression>,
|
|
615
|
+
): LabelGuard | undefined => {
|
|
616
|
+
if (visited.has(expression)) return undefined;
|
|
617
|
+
visited.add(expression);
|
|
618
|
+
if (expression.kind === "And") {
|
|
619
|
+
const first = expression.operands[0];
|
|
620
|
+
if (first.kind !== "Apply" || first.operator !== "=" || isLocallyResolved(first)) {
|
|
621
|
+
return undefined;
|
|
622
|
+
}
|
|
623
|
+
const target = first.args[0];
|
|
624
|
+
const label = first.args[1];
|
|
625
|
+
if (target.kind !== "FunctionApplication" || label.kind !== "String") return undefined;
|
|
626
|
+
const argument = target.args.length === 1 ? target.args[0] : undefined;
|
|
627
|
+
if (argument?.kind !== "Identifier" || !isLocallyResolved(argument)) return undefined;
|
|
628
|
+
const variable = getStaticVariableIndex(target.function, environment, false);
|
|
629
|
+
if (variable === undefined) return undefined;
|
|
630
|
+
return { variable, argument: argument.name, labels: new Set([label.value]) };
|
|
631
|
+
}
|
|
632
|
+
if (expression.kind === "Or") {
|
|
633
|
+
const guards = expression.operands.map((operand) =>
|
|
634
|
+
getLabelGuard(operand, environment, visited),
|
|
635
|
+
);
|
|
636
|
+
const first = guards[0];
|
|
637
|
+
const isShared = guards.every(
|
|
638
|
+
(guard) =>
|
|
639
|
+
guard !== undefined &&
|
|
640
|
+
guard.variable === first?.variable &&
|
|
641
|
+
guard.argument === first.argument,
|
|
642
|
+
);
|
|
643
|
+
if (!first || !isShared) return undefined;
|
|
644
|
+
const labels = new Set(guards.flatMap((guard) => [...(guard?.labels ?? [])]));
|
|
645
|
+
return { variable: first.variable, argument: first.argument, labels };
|
|
646
|
+
}
|
|
647
|
+
if (expression.kind !== "Apply" || isLocallyResolved(expression)) return undefined;
|
|
648
|
+
const closure = resolveGlobalOperator(expression.operator, environment);
|
|
649
|
+
if (closure?.kind !== "Closure" || closure.scope !== undefined) return undefined;
|
|
650
|
+
const guard = getLabelGuard(closure.body, environment, visited);
|
|
651
|
+
const position = closure.params.findIndex(
|
|
652
|
+
(param) => param.arity === 0 && param.name === guard?.argument,
|
|
653
|
+
);
|
|
654
|
+
const argument = expression.args[position];
|
|
655
|
+
if (!guard || argument?.kind !== "Identifier" || !isLocallyResolved(argument)) {
|
|
656
|
+
return undefined;
|
|
657
|
+
}
|
|
658
|
+
return { variable: guard.variable, argument: argument.name, labels: guard.labels };
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
const getLabel = (run: Run, scope: Scope | undefined, guard: LabelGuard) => {
|
|
662
|
+
const binding = lookupScope(scope, guard.argument);
|
|
663
|
+
const functionValue = run.current?.[guard.variable];
|
|
664
|
+
if (
|
|
665
|
+
binding?.kind !== "Value" ||
|
|
666
|
+
typeof functionValue !== "object" ||
|
|
667
|
+
functionValue.kind !== "Function"
|
|
668
|
+
) {
|
|
669
|
+
return undefined;
|
|
670
|
+
}
|
|
671
|
+
const position = functionValue.domain.indexOf(binding.value);
|
|
672
|
+
const label = position === -1 ? undefined : functionValue.range[position];
|
|
673
|
+
return typeof label === "string" ? label : undefined;
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const compileDisjunction = (
|
|
677
|
+
operands: Expression[],
|
|
678
|
+
environment: Environment,
|
|
679
|
+
isInit: boolean,
|
|
680
|
+
): Runner => {
|
|
681
|
+
const runners = operands.map((operand) => getRunner(operand, environment, isInit));
|
|
682
|
+
const guards = isInit
|
|
683
|
+
? []
|
|
684
|
+
: operands.map((operand) => getLabelGuard(operand, environment, new Set()));
|
|
685
|
+
const dispatch = guards.find((guard) => guard !== undefined);
|
|
686
|
+
const labels = guards.map((guard) =>
|
|
687
|
+
guard?.variable === dispatch?.variable && guard?.argument === dispatch?.argument
|
|
688
|
+
? guard?.labels
|
|
689
|
+
: undefined,
|
|
690
|
+
);
|
|
691
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
692
|
+
const label = dispatch && getLabel(run, scope, dispatch);
|
|
693
|
+
for (let index = 0; index < runners.length && !run.isStopped; index++) {
|
|
694
|
+
if (label !== undefined && labels[index]?.has(label) === false) continue;
|
|
695
|
+
runners[index](run, scope, partial, action, isFixed, onComplete);
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
};
|
|
699
|
+
|
|
545
700
|
const compileAction = (
|
|
546
701
|
expression: Expression,
|
|
547
702
|
environment: Environment,
|
|
@@ -551,26 +706,19 @@ const compileAction = (
|
|
|
551
706
|
switch (expression.kind) {
|
|
552
707
|
case "And":
|
|
553
708
|
return compileConjunction(expression.operands, environment, isInit);
|
|
554
|
-
case "Or":
|
|
555
|
-
|
|
556
|
-
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
557
|
-
for (const operand of operands) operand(run, scope, partial, action, isFixed, onComplete);
|
|
558
|
-
};
|
|
559
|
-
}
|
|
709
|
+
case "Or":
|
|
710
|
+
return compileDisjunction(expression.operands, environment, isInit);
|
|
560
711
|
case "Exists": {
|
|
561
712
|
const body = child(expression.body);
|
|
562
713
|
const bound = expression.bounds[0];
|
|
563
714
|
if (expression.bounds.length === 1 && bound.pattern.kind === "Name") {
|
|
564
715
|
const name = bound.pattern.name;
|
|
565
716
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
}
|
|
572
|
-
const elements = toFiniteSet(set).elements;
|
|
573
|
-
for (let index = 0; index < elements.length; index++) {
|
|
717
|
+
const elements = getEnumeratedElements(
|
|
718
|
+
evaluate(bound.set, getContext(run, scope, partial)),
|
|
719
|
+
bound.set,
|
|
720
|
+
);
|
|
721
|
+
for (let index = 0; index < elements.length && !run.isStopped; index++) {
|
|
574
722
|
body(
|
|
575
723
|
run,
|
|
576
724
|
bindValue(scope, name, elements[index]),
|
|
@@ -585,12 +733,18 @@ const compileAction = (
|
|
|
585
733
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
586
734
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
587
735
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
588
|
-
return
|
|
736
|
+
return run.isStopped;
|
|
589
737
|
});
|
|
590
738
|
};
|
|
591
739
|
}
|
|
592
740
|
case "Forall": {
|
|
593
741
|
const body = child(expression.body);
|
|
742
|
+
const readLocals =
|
|
743
|
+
!isInit &&
|
|
744
|
+
getReadVariables(expression, environment) !== undefined &&
|
|
745
|
+
!hasBranches(expression.body, environment, new Set())
|
|
746
|
+
? getReadLocals(expression, NO_NAMES)
|
|
747
|
+
: undefined;
|
|
594
748
|
return (run, scope, partial, action, _isFixed, onComplete) => {
|
|
595
749
|
const boundScopes: (Scope | undefined)[] = [];
|
|
596
750
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
@@ -598,6 +752,16 @@ const compileAction = (
|
|
|
598
752
|
return false;
|
|
599
753
|
});
|
|
600
754
|
const scopes = [...boundScopes.slice(0, 1), ...boundScopes.slice(1).reverse()];
|
|
755
|
+
const isBoundToValues = readLocals?.every(
|
|
756
|
+
(name) => lookupScope(scope, name)?.kind === "Value",
|
|
757
|
+
);
|
|
758
|
+
if (isBoundToValues) {
|
|
759
|
+
const isSatisfied = scopes.every((boundScope) =>
|
|
760
|
+
holds(expression.body, getContext(run, boundScope, partial)),
|
|
761
|
+
);
|
|
762
|
+
if (isSatisfied) onComplete(partial, action);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
601
765
|
const visit = (index: number, nextPartial: State, nextAction: string | undefined): void => {
|
|
602
766
|
if (index === scopes.length) {
|
|
603
767
|
onComplete(nextPartial, nextAction);
|
|
@@ -641,16 +805,19 @@ const compileAction = (
|
|
|
641
805
|
);
|
|
642
806
|
const body = arm?.body ?? other;
|
|
643
807
|
if (!body)
|
|
644
|
-
throw new TlaEvaluationError(
|
|
808
|
+
throw new TlaEvaluationError(
|
|
809
|
+
`${UNEXPECTED_EXCEPTION}In computing next states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
810
|
+
);
|
|
645
811
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
646
812
|
};
|
|
647
813
|
}
|
|
648
814
|
case "Let": {
|
|
649
815
|
const body = child(expression.body);
|
|
816
|
+
const reevaluated = getReevaluatedDefinitions(expression.definitions, environment);
|
|
650
817
|
return (run, scope, partial, action, isFixed, onComplete) =>
|
|
651
818
|
body(
|
|
652
819
|
run,
|
|
653
|
-
bindDefinitions(expression.definitions, getContext(run, scope, partial)),
|
|
820
|
+
bindDefinitions(expression.definitions, getContext(run, scope, partial), reevaluated),
|
|
654
821
|
partial,
|
|
655
822
|
action,
|
|
656
823
|
isFixed,
|
|
@@ -751,7 +918,7 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
751
918
|
const environment = context.environment;
|
|
752
919
|
const found = { value: false };
|
|
753
920
|
getRunner(expression, environment, false)(
|
|
754
|
-
{ environment, isInit: false, current: state },
|
|
921
|
+
{ environment, isInit: false, current: state, isStopped: false },
|
|
755
922
|
context.scope,
|
|
756
923
|
getEmptyState(environment),
|
|
757
924
|
undefined,
|
|
@@ -763,48 +930,99 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
763
930
|
return found.value;
|
|
764
931
|
};
|
|
765
932
|
|
|
766
|
-
export const
|
|
767
|
-
|
|
768
|
-
|
|
933
|
+
export const forEachInitialState = (
|
|
934
|
+
expression: Expression,
|
|
935
|
+
environment: Environment,
|
|
936
|
+
onState: (state: Value[]) => boolean,
|
|
937
|
+
scope?: Scope,
|
|
938
|
+
) => {
|
|
939
|
+
const run: Run = { environment, isInit: true, current: undefined, isStopped: false };
|
|
769
940
|
getRunner(expression, environment, true)(
|
|
770
941
|
run,
|
|
771
|
-
|
|
942
|
+
scope,
|
|
772
943
|
getEmptyState(environment),
|
|
773
944
|
undefined,
|
|
774
945
|
false,
|
|
775
946
|
(partial) => {
|
|
947
|
+
if (run.isStopped) return;
|
|
776
948
|
if (!isComplete(partial)) {
|
|
777
|
-
|
|
778
|
-
|
|
949
|
+
// TLC reports an initial state that isn't completely specified as an illegal state, not as
|
|
950
|
+
// an evaluation failure.
|
|
951
|
+
throw setExitCode(
|
|
952
|
+
new TlaEvaluationError(
|
|
953
|
+
`The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
|
|
954
|
+
),
|
|
955
|
+
EXIT_STATUS.error,
|
|
779
956
|
);
|
|
780
957
|
}
|
|
781
|
-
|
|
958
|
+
run.isStopped = onState(partial);
|
|
782
959
|
},
|
|
783
960
|
);
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
export const getInitialStates = (
|
|
964
|
+
expression: Expression,
|
|
965
|
+
environment: Environment,
|
|
966
|
+
scope?: Scope,
|
|
967
|
+
): Value[][] => {
|
|
968
|
+
const states: Value[][] = [];
|
|
969
|
+
forEachInitialState(
|
|
970
|
+
expression,
|
|
971
|
+
environment,
|
|
972
|
+
(state) => {
|
|
973
|
+
states.push(state);
|
|
974
|
+
return false;
|
|
975
|
+
},
|
|
976
|
+
scope,
|
|
977
|
+
);
|
|
784
978
|
return states;
|
|
785
979
|
};
|
|
786
980
|
|
|
787
|
-
export const
|
|
981
|
+
export const forEachSuccessor = (
|
|
788
982
|
expression: Expression,
|
|
789
983
|
state: Value[],
|
|
790
984
|
environment: Environment,
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
985
|
+
onSuccessor: (successor: Successor) => boolean,
|
|
986
|
+
scope?: Scope,
|
|
987
|
+
) => {
|
|
988
|
+
const run: Run = { environment, isInit: false, current: state, isStopped: false };
|
|
989
|
+
const counter = { successors: 0 };
|
|
794
990
|
getRunner(expression, environment, false)(
|
|
795
991
|
run,
|
|
796
|
-
|
|
992
|
+
scope,
|
|
797
993
|
getEmptyState(environment),
|
|
798
994
|
undefined,
|
|
799
995
|
false,
|
|
800
996
|
(partial, action) => {
|
|
997
|
+
if (run.isStopped) return;
|
|
801
998
|
if (!isComplete(partial)) {
|
|
802
999
|
throw new TlaEvaluationError(
|
|
803
1000
|
`Successor state is not completely specified by action ${action ?? "Next"}. The following variables are not assigned: ${getMissingVariables(partial, environment).join(", ")}`,
|
|
804
1001
|
);
|
|
805
1002
|
}
|
|
806
|
-
successors
|
|
1003
|
+
counter.successors++;
|
|
1004
|
+
run.isStopped = onSuccessor({ state: partial, action });
|
|
1005
|
+
},
|
|
1006
|
+
);
|
|
1007
|
+
return counter.successors;
|
|
1008
|
+
};
|
|
1009
|
+
|
|
1010
|
+
export const getSuccessors = (
|
|
1011
|
+
expression: Expression,
|
|
1012
|
+
state: Value[],
|
|
1013
|
+
environment: Environment,
|
|
1014
|
+
scope?: Scope,
|
|
1015
|
+
): Successor[] => {
|
|
1016
|
+
const successors: Successor[] = [];
|
|
1017
|
+
forEachSuccessor(
|
|
1018
|
+
expression,
|
|
1019
|
+
state,
|
|
1020
|
+
environment,
|
|
1021
|
+
(successor) => {
|
|
1022
|
+
successors.push(successor);
|
|
1023
|
+
return false;
|
|
807
1024
|
},
|
|
1025
|
+
scope,
|
|
808
1026
|
);
|
|
809
1027
|
return successors;
|
|
810
1028
|
};
|