tla-ts 0.0.1 → 0.0.3-dev.037ac3b
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 +98 -39
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +225 -30
- package/dist/index.d.ts +225 -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 +21 -1
- package/dist/run-cli.js +21 -1
- package/dist/transfer.cjs +400 -39
- package/dist/transfer.js +400 -39
- package/package.json +6 -2
- package/src/ast.ts +213 -6
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +1132 -258
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +58 -34
- package/src/config.ts +39 -11
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +557 -153
- package/src/errors.ts +66 -1
- package/src/evaluate.ts +553 -258
- package/src/exit-codes.ts +53 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +235 -79
- 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/live-check.ts +692 -0
- package/src/liveness.ts +71 -596
- 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 +179 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +483 -51
- package/src/run-cli.ts +473 -62
- package/src/runtime.ts +65 -28
- package/src/spec-description.ts +521 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +218 -92
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +31 -45
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +336 -57
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +93 -60
- package/src/tlc-ext.ts +148 -44
- package/src/tlc-module.ts +71 -66
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +414 -0
- package/src/tlc-summary.ts +124 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1285 -179
package/src/runtime.ts
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createJavaRandom, getTlcFingerprint, type JavaRandom } from "./tlc-random";
|
|
2
|
+
import { createFunction, createRecord, type Value } from "./values";
|
|
2
3
|
|
|
3
4
|
export interface CheckerRuntime {
|
|
4
5
|
getLevel: (state: (Value | undefined)[]) => number;
|
|
5
6
|
getBehavior: (state: Value[]) => Value[][];
|
|
6
|
-
|
|
7
|
+
getSpecification: () => Value;
|
|
8
|
+
counterExample: Value | undefined;
|
|
9
|
+
setLivenessSource: (index: number | undefined) => void;
|
|
10
|
+
getCurrentState: () => Value[] | undefined;
|
|
11
|
+
getFingerprint: (state: Value[]) => bigint;
|
|
12
|
+
getRandom: () => JavaRandom;
|
|
13
|
+
evaluateAtStartup: <Result>(evaluate: () => Result) => Result;
|
|
14
|
+
pause: () => void;
|
|
15
|
+
seed: bigint;
|
|
16
|
+
isSeen: (state: Value[]) => boolean;
|
|
7
17
|
generatedStates: number;
|
|
8
18
|
distinctStates: number;
|
|
9
19
|
initialStates: number;
|
|
@@ -14,41 +24,68 @@ export interface CheckerRuntime {
|
|
|
14
24
|
properties: Map<string, string>;
|
|
15
25
|
possibleCounts: Map<string, number>;
|
|
16
26
|
isStopped: boolean;
|
|
27
|
+
isStarted: boolean;
|
|
17
28
|
checkDeadlock: boolean;
|
|
18
29
|
startedAt: number;
|
|
19
30
|
}
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
// TLC's RandomEnumerableValues keeps one java.util.Random per thread, seeded with -seed. The
|
|
33
|
+
// main thread evaluates constants, assumptions, and initial states with it. A worker reseeds
|
|
34
|
+
// its Random with the fingerprint of each state it expands, XORed with the seed, so a state's
|
|
35
|
+
// successors are the same whatever order the workers expand states in.
|
|
36
|
+
export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime => {
|
|
37
|
+
const startup = createJavaRandom(seed);
|
|
38
|
+
const worker = { random: createJavaRandom(seed), state: undefined as Value[] | undefined };
|
|
39
|
+
const phase = { isStartup: false };
|
|
40
|
+
const runtime: CheckerRuntime = {
|
|
41
|
+
getLevel: () => 0,
|
|
42
|
+
getBehavior: () => [],
|
|
43
|
+
getSpecification: () => createRecord([]),
|
|
44
|
+
counterExample: undefined,
|
|
45
|
+
isSeen: () => false,
|
|
46
|
+
setLivenessSource: () => {},
|
|
47
|
+
getCurrentState: () => undefined,
|
|
48
|
+
getFingerprint: getTlcFingerprint,
|
|
49
|
+
getRandom: () => {
|
|
50
|
+
const state = phase.isStartup ? undefined : runtime.getCurrentState();
|
|
51
|
+
if (state === undefined) return startup;
|
|
52
|
+
if (state !== worker.state) {
|
|
53
|
+
worker.random.setSeed(runtime.getFingerprint(state) ^ BigInt.asUintN(64, seed));
|
|
54
|
+
worker.state = state;
|
|
55
|
+
}
|
|
56
|
+
return worker.random;
|
|
57
|
+
},
|
|
58
|
+
evaluateAtStartup: (evaluate) => {
|
|
59
|
+
const wasStartup = phase.isStartup;
|
|
60
|
+
phase.isStartup = true;
|
|
61
|
+
try {
|
|
62
|
+
return evaluate();
|
|
63
|
+
} finally {
|
|
64
|
+
phase.isStartup = wasStartup;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
pause: () => {},
|
|
68
|
+
seed,
|
|
69
|
+
generatedStates: 0,
|
|
70
|
+
distinctStates: 0,
|
|
71
|
+
initialStates: 0,
|
|
72
|
+
queueSize: 0,
|
|
73
|
+
diameter: 1,
|
|
74
|
+
registers: new Map(),
|
|
75
|
+
namedRegisters: new Map(),
|
|
76
|
+
properties: new Map(),
|
|
77
|
+
possibleCounts: new Map(),
|
|
78
|
+
isStopped: false,
|
|
79
|
+
isStarted: false,
|
|
80
|
+
checkDeadlock,
|
|
81
|
+
startedAt: Date.now(),
|
|
28
82
|
};
|
|
83
|
+
return runtime;
|
|
29
84
|
};
|
|
30
85
|
|
|
31
|
-
export const createRuntime = (checkDeadlock: boolean): CheckerRuntime => ({
|
|
32
|
-
getLevel: () => 0,
|
|
33
|
-
getBehavior: () => [],
|
|
34
|
-
random: createRandom(0),
|
|
35
|
-
generatedStates: 0,
|
|
36
|
-
distinctStates: 0,
|
|
37
|
-
initialStates: 0,
|
|
38
|
-
queueSize: 0,
|
|
39
|
-
diameter: 1,
|
|
40
|
-
registers: new Map(),
|
|
41
|
-
namedRegisters: new Map(),
|
|
42
|
-
properties: new Map(),
|
|
43
|
-
possibleCounts: new Map(),
|
|
44
|
-
isStopped: false,
|
|
45
|
-
checkDeadlock,
|
|
46
|
-
startedAt: Date.now(),
|
|
47
|
-
});
|
|
48
|
-
|
|
49
86
|
export const NAMED_REGISTER_PREFIX = "s:";
|
|
50
87
|
|
|
51
|
-
|
|
88
|
+
const POSSIBLE_REGISTER = "s:_possible";
|
|
52
89
|
|
|
53
90
|
export const getPossibleCounts = (runtime: CheckerRuntime) =>
|
|
54
91
|
createFunction([...runtime.possibleCounts].map(([name, count]): [Value, Value] => [name, count]));
|
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isLocallyResolved,
|
|
3
|
+
type Definition,
|
|
4
|
+
type Expression,
|
|
5
|
+
type SourceLocation,
|
|
6
|
+
type SourceRange,
|
|
7
|
+
} from "./ast";
|
|
8
|
+
import type { ModelConfig } from "./config";
|
|
9
|
+
import type { InstanceAction } from "./environment";
|
|
10
|
+
import { TlaError } from "./errors";
|
|
11
|
+
import {
|
|
12
|
+
bindDefinitions,
|
|
13
|
+
bindOperatorArguments,
|
|
14
|
+
evaluate,
|
|
15
|
+
forEachBinding,
|
|
16
|
+
formatRange,
|
|
17
|
+
getDefinition,
|
|
18
|
+
isConstantLevel,
|
|
19
|
+
lookupScope,
|
|
20
|
+
resolveGlobalOperator,
|
|
21
|
+
type Closure,
|
|
22
|
+
type Environment,
|
|
23
|
+
type Scope,
|
|
24
|
+
} from "./evaluate";
|
|
25
|
+
import { TEMPORAL_LEVEL } from "./levels";
|
|
26
|
+
import { getLevel } from "./liveness";
|
|
27
|
+
import { createRecord, createSet, createTuple, formatValue, type Value } from "./values";
|
|
28
|
+
|
|
29
|
+
export interface SpecAction {
|
|
30
|
+
name: string;
|
|
31
|
+
location: SourceLocation;
|
|
32
|
+
parameters: [string, Value][];
|
|
33
|
+
expression: Expression;
|
|
34
|
+
scope: Scope | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface SpecDescription {
|
|
38
|
+
inits: SpecAction[];
|
|
39
|
+
actions: SpecAction[];
|
|
40
|
+
temporals: SpecAction[];
|
|
41
|
+
invariants: SpecAction[];
|
|
42
|
+
impliedInits: SpecAction[];
|
|
43
|
+
impliedActions: SpecAction[];
|
|
44
|
+
impliedTemporals: SpecAction[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const UNNAMED_ACTION = "UnnamedAction";
|
|
48
|
+
|
|
49
|
+
const BUILTIN_RANGE: SourceRange = {
|
|
50
|
+
module: "--TLA+ BUILTINS--",
|
|
51
|
+
start: { line: 0, column: 0 },
|
|
52
|
+
end: { line: 0, column: 0 },
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const toLocationValue = (location: SourceLocation) => {
|
|
56
|
+
const range = location.range ?? BUILTIN_RANGE;
|
|
57
|
+
return createRecord([
|
|
58
|
+
["beginLine", range.start.line],
|
|
59
|
+
["beginColumn", range.start.column],
|
|
60
|
+
["endLine", range.end.line],
|
|
61
|
+
["endColumn", range.end.column],
|
|
62
|
+
["module", range.module],
|
|
63
|
+
]);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const toActionValue = (action: SpecAction) => {
|
|
67
|
+
const fields: [string, Value][] = [
|
|
68
|
+
["name", action.name],
|
|
69
|
+
["location", toLocationValue(action.location)],
|
|
70
|
+
];
|
|
71
|
+
if (action.parameters.length === 0) return createRecord(fields);
|
|
72
|
+
return createRecord([
|
|
73
|
+
...fields,
|
|
74
|
+
["context", createRecord(action.parameters)],
|
|
75
|
+
["parameters", createTuple(action.parameters.map((parameter) => parameter[0]))],
|
|
76
|
+
]);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const createAction = (name: string, expression: Expression): SpecAction => ({
|
|
80
|
+
name,
|
|
81
|
+
location: expression.location,
|
|
82
|
+
parameters: [],
|
|
83
|
+
expression,
|
|
84
|
+
scope: undefined,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const getConstantContext = (environment: Environment, scope: Scope | undefined) => ({
|
|
88
|
+
environment,
|
|
89
|
+
scope,
|
|
90
|
+
state: undefined,
|
|
91
|
+
next: undefined,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const getOperatorName = (expression: Expression) =>
|
|
95
|
+
expression.kind === "Identifier"
|
|
96
|
+
? expression.name
|
|
97
|
+
: expression.kind === "Apply"
|
|
98
|
+
? expression.operator
|
|
99
|
+
: undefined;
|
|
100
|
+
|
|
101
|
+
const getArguments = (expression: Expression) =>
|
|
102
|
+
expression.kind === "Apply" ? expression.args : [];
|
|
103
|
+
|
|
104
|
+
const resolveClosure = (
|
|
105
|
+
expression: Expression,
|
|
106
|
+
scope: Scope | undefined,
|
|
107
|
+
environment: Environment,
|
|
108
|
+
) => {
|
|
109
|
+
const name = getOperatorName(expression);
|
|
110
|
+
if (name === undefined) return undefined;
|
|
111
|
+
if (isLocallyResolved(expression)) {
|
|
112
|
+
const binding = lookupScope(scope, name);
|
|
113
|
+
return binding?.kind === "Operator" && binding.operator.kind === "Closure"
|
|
114
|
+
? binding.operator
|
|
115
|
+
: undefined;
|
|
116
|
+
}
|
|
117
|
+
if (environment.constants.has(name) || environment.variableIndex.has(name)) return undefined;
|
|
118
|
+
const operator = resolveGlobalOperator(name, environment);
|
|
119
|
+
return operator?.kind === "Closure" ? operator : undefined;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const isEvaluable = (expression: Expression, environment: Environment) => {
|
|
123
|
+
try {
|
|
124
|
+
evaluate(expression, getConstantContext(environment, undefined));
|
|
125
|
+
return true;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof TlaError) return false;
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const isConstantValue = (definition: Definition | undefined, environment: Environment) =>
|
|
133
|
+
definition?.kind === "Operator" &&
|
|
134
|
+
definition.params.length === 0 &&
|
|
135
|
+
definition.body.kind !== "Number" &&
|
|
136
|
+
definition.body.kind !== "String" &&
|
|
137
|
+
isConstantLevel(definition, environment) &&
|
|
138
|
+
isEvaluable(definition.body, environment);
|
|
139
|
+
|
|
140
|
+
const isConstantDefinition = (closure: Closure, environment: Environment) => {
|
|
141
|
+
const definition = getDefinition(closure.name, environment);
|
|
142
|
+
return definition?.body === closure.body && isConstantValue(definition, environment);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
const getThunk = (expression: Expression, scope: Scope | undefined) => {
|
|
146
|
+
if (expression.kind !== "Identifier" || !isLocallyResolved(expression)) return undefined;
|
|
147
|
+
const binding = lookupScope(scope, expression.name);
|
|
148
|
+
return binding?.kind === "Thunk" ? binding : undefined;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const getParameters = (closure: Closure | undefined, scope: Scope | undefined) =>
|
|
152
|
+
(closure?.params ?? []).flatMap((param): [string, Value][] => {
|
|
153
|
+
const binding = lookupScope(scope, param.name);
|
|
154
|
+
return binding?.kind === "Value" ? [[param.name, binding.value]] : [];
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const tryCollect = <Result>(collect: () => Result, fallback: () => Result) => {
|
|
158
|
+
try {
|
|
159
|
+
return collect();
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if (error instanceof TlaError) return fallback();
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const getInstanceActions = (
|
|
167
|
+
instanceAction: InstanceAction,
|
|
168
|
+
expression: Expression,
|
|
169
|
+
scope: Scope | undefined,
|
|
170
|
+
called: Closure | undefined,
|
|
171
|
+
environment: Environment,
|
|
172
|
+
): SpecAction[] => {
|
|
173
|
+
const args = getArguments(expression);
|
|
174
|
+
const callAction = (): SpecAction[] => [
|
|
175
|
+
{ name: instanceAction.name, location: expression.location, parameters: [], expression, scope },
|
|
176
|
+
];
|
|
177
|
+
if (!called || args.length === 0) {
|
|
178
|
+
return [{ ...instanceAction, parameters: [], expression, scope }];
|
|
179
|
+
}
|
|
180
|
+
if (args.some((arg) => getLevel(arg, scope, environment) !== 0)) return callAction();
|
|
181
|
+
return tryCollect(
|
|
182
|
+
() => [
|
|
183
|
+
{
|
|
184
|
+
...instanceAction,
|
|
185
|
+
parameters: args.map((arg, index): [string, Value] => [
|
|
186
|
+
called.params[index].name.replace(/#\d+$/, ""),
|
|
187
|
+
evaluate(arg, getConstantContext(environment, scope)),
|
|
188
|
+
]),
|
|
189
|
+
expression,
|
|
190
|
+
scope,
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
callAction,
|
|
194
|
+
);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const getActions = (
|
|
198
|
+
expression: Expression,
|
|
199
|
+
scope: Scope | undefined,
|
|
200
|
+
closure: Closure | undefined,
|
|
201
|
+
environment: Environment,
|
|
202
|
+
instanceActions: Map<string, InstanceAction>,
|
|
203
|
+
): SpecAction[] => {
|
|
204
|
+
const createSplitAction = (): SpecAction => ({
|
|
205
|
+
name: closure?.name ?? UNNAMED_ACTION,
|
|
206
|
+
location: expression.location,
|
|
207
|
+
parameters: getParameters(closure, scope),
|
|
208
|
+
expression,
|
|
209
|
+
scope,
|
|
210
|
+
});
|
|
211
|
+
switch (expression.kind) {
|
|
212
|
+
case "Let":
|
|
213
|
+
return getActions(
|
|
214
|
+
expression.body,
|
|
215
|
+
bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
|
|
216
|
+
closure,
|
|
217
|
+
environment,
|
|
218
|
+
instanceActions,
|
|
219
|
+
);
|
|
220
|
+
case "Label":
|
|
221
|
+
return getActions(expression.body, scope, closure, environment, instanceActions);
|
|
222
|
+
case "Or":
|
|
223
|
+
return expression.operands.flatMap((operand) =>
|
|
224
|
+
getActions(operand, scope, closure, environment, instanceActions),
|
|
225
|
+
);
|
|
226
|
+
case "Exists":
|
|
227
|
+
return tryCollect(
|
|
228
|
+
() => {
|
|
229
|
+
const actions: SpecAction[] = [];
|
|
230
|
+
forEachBinding(
|
|
231
|
+
expression.bounds,
|
|
232
|
+
getConstantContext(environment, scope),
|
|
233
|
+
(boundScope) => {
|
|
234
|
+
actions.push(
|
|
235
|
+
...getActions(expression.body, boundScope, closure, environment, instanceActions),
|
|
236
|
+
);
|
|
237
|
+
return false;
|
|
238
|
+
},
|
|
239
|
+
);
|
|
240
|
+
return actions.length === 0 ? [createSplitAction()] : actions;
|
|
241
|
+
},
|
|
242
|
+
() => [createSplitAction()],
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
const instanceAction = isLocallyResolved(expression)
|
|
246
|
+
? undefined
|
|
247
|
+
: instanceActions.get(getOperatorName(expression) ?? "");
|
|
248
|
+
const called = resolveClosure(expression, scope, environment);
|
|
249
|
+
if (instanceAction)
|
|
250
|
+
return getInstanceActions(instanceAction, expression, scope, called, environment);
|
|
251
|
+
if (!called) return [createSplitAction()];
|
|
252
|
+
const args = getArguments(expression);
|
|
253
|
+
const callAction = (): SpecAction[] => [
|
|
254
|
+
{ name: called.name, location: expression.location, parameters: [], expression, scope },
|
|
255
|
+
];
|
|
256
|
+
if (isConstantDefinition(called, environment)) return callAction();
|
|
257
|
+
if (args.some((arg) => getLevel(arg, scope, environment) !== 0)) return callAction();
|
|
258
|
+
return tryCollect(() => {
|
|
259
|
+
const boundScope = args.reduce<Scope | undefined>(
|
|
260
|
+
(current, arg, index) => ({
|
|
261
|
+
name: called.params[index].name,
|
|
262
|
+
binding: {
|
|
263
|
+
kind: "Value",
|
|
264
|
+
value: evaluate(arg, getConstantContext(environment, scope)),
|
|
265
|
+
},
|
|
266
|
+
parent: current,
|
|
267
|
+
}),
|
|
268
|
+
called.scope,
|
|
269
|
+
);
|
|
270
|
+
return getActions(called.body, boundScope, called, environment, instanceActions);
|
|
271
|
+
}, callAction);
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const describeSpecification = (
|
|
275
|
+
expression: Expression,
|
|
276
|
+
scope: Scope | undefined,
|
|
277
|
+
environment: Environment,
|
|
278
|
+
description: SpecDescription,
|
|
279
|
+
next: { expression: Expression; scope: Scope | undefined }[],
|
|
280
|
+
): void => {
|
|
281
|
+
const recurse = (inner: Expression, innerScope: Scope | undefined) =>
|
|
282
|
+
describeSpecification(inner, innerScope, environment, description, next);
|
|
283
|
+
if (expression.kind === "Let") {
|
|
284
|
+
recurse(
|
|
285
|
+
expression.body,
|
|
286
|
+
bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
|
|
287
|
+
);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const thunk = getThunk(expression, scope);
|
|
291
|
+
if (thunk) {
|
|
292
|
+
recurse(thunk.expression, thunk.scope);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const called = resolveClosure(expression, scope, environment);
|
|
296
|
+
if (called && isConstantDefinition(called, environment)) return;
|
|
297
|
+
if (called && called.params.length === 0) {
|
|
298
|
+
if (getLevel(called.body, called.scope, environment) === 1) {
|
|
299
|
+
description.inits.push(createAction(called.name, called.body));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
recurse(called.body, called.scope);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (called && expression.kind === "Apply" && called.params.length === expression.args.length) {
|
|
306
|
+
recurse(
|
|
307
|
+
called.body,
|
|
308
|
+
bindOperatorArguments(called, expression.args, getConstantContext(environment, scope)),
|
|
309
|
+
);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
if (expression.kind === "And") {
|
|
313
|
+
expression.operands.forEach((operand) => recurse(operand, scope));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (expression.kind === "Always") {
|
|
317
|
+
if (expression.expression.kind === "BoxAction") {
|
|
318
|
+
next.push({ expression: expression.expression.action, scope });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
description.temporals.push(createAction(UNNAMED_ACTION, expression));
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
const level = getLevel(expression, scope, environment);
|
|
325
|
+
if (level <= 1) description.inits.push(createAction(UNNAMED_ACTION, expression));
|
|
326
|
+
else if (level === TEMPORAL_LEVEL) {
|
|
327
|
+
description.temporals.push(createAction(UNNAMED_ACTION, expression));
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const describeProperty = (
|
|
332
|
+
configName: string,
|
|
333
|
+
expression: Expression,
|
|
334
|
+
scope: Scope | undefined,
|
|
335
|
+
environment: Environment,
|
|
336
|
+
description: SpecDescription,
|
|
337
|
+
): void => {
|
|
338
|
+
const recurse = (inner: Expression, innerScope: Scope | undefined) =>
|
|
339
|
+
describeProperty(configName, inner, innerScope, environment, description);
|
|
340
|
+
if (expression.kind === "Let") {
|
|
341
|
+
recurse(
|
|
342
|
+
expression.body,
|
|
343
|
+
bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
|
|
344
|
+
);
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const thunk = getThunk(expression, scope);
|
|
348
|
+
if (thunk) {
|
|
349
|
+
recurse(thunk.expression, thunk.scope);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const called = resolveClosure(expression, scope, environment);
|
|
353
|
+
if (called && isConstantDefinition(called, environment)) return;
|
|
354
|
+
if (called && called.params.length === getArguments(expression).length) {
|
|
355
|
+
recurse(
|
|
356
|
+
called.body,
|
|
357
|
+
expression.kind === "Apply"
|
|
358
|
+
? bindOperatorArguments(called, expression.args, getConstantContext(environment, scope))
|
|
359
|
+
: called.scope,
|
|
360
|
+
);
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
if (expression.kind === "Forall") {
|
|
364
|
+
const isEnumerated = tryCollect(
|
|
365
|
+
() => {
|
|
366
|
+
forEachBinding(expression.bounds, getConstantContext(environment, scope), (boundScope) => {
|
|
367
|
+
recurse(expression.body, boundScope);
|
|
368
|
+
return false;
|
|
369
|
+
});
|
|
370
|
+
return true;
|
|
371
|
+
},
|
|
372
|
+
() => false,
|
|
373
|
+
);
|
|
374
|
+
if (isEnumerated) return;
|
|
375
|
+
}
|
|
376
|
+
if (expression.kind === "And") {
|
|
377
|
+
expression.operands.forEach((operand) => recurse(operand, scope));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (expression.kind === "Always") {
|
|
381
|
+
const inner = expression.expression;
|
|
382
|
+
if (inner.kind === "BoxAction") {
|
|
383
|
+
description.impliedActions.push(createAction(configName, inner));
|
|
384
|
+
} else if (getLevel(inner, scope, environment) < 2) {
|
|
385
|
+
description.invariants.push(createAction(configName, inner));
|
|
386
|
+
} else {
|
|
387
|
+
description.impliedTemporals.push(createAction(configName, expression));
|
|
388
|
+
}
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const level = getLevel(expression, scope, environment);
|
|
392
|
+
if (level <= 1) description.impliedInits.push(createAction(configName, expression));
|
|
393
|
+
else if (level === TEMPORAL_LEVEL) {
|
|
394
|
+
description.impliedTemporals.push(createAction(configName, expression));
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
const getOperatorDefinition = (name: string, environment: Environment) => {
|
|
399
|
+
const definition = getDefinition(name, environment);
|
|
400
|
+
return definition?.kind === "Operator" && !isConstantValue(definition, environment)
|
|
401
|
+
? definition
|
|
402
|
+
: undefined;
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
const describeDefinition = (definition: Definition) =>
|
|
406
|
+
createRecord([
|
|
407
|
+
["name", definition.name],
|
|
408
|
+
["location", toLocationValue(definition.location)],
|
|
409
|
+
]);
|
|
410
|
+
|
|
411
|
+
const describe = (
|
|
412
|
+
config: ModelConfig,
|
|
413
|
+
environment: Environment,
|
|
414
|
+
instanceActions: Map<string, InstanceAction>,
|
|
415
|
+
) => {
|
|
416
|
+
const description: SpecDescription = {
|
|
417
|
+
inits: [],
|
|
418
|
+
actions: [],
|
|
419
|
+
temporals: [],
|
|
420
|
+
invariants: [],
|
|
421
|
+
impliedInits: [],
|
|
422
|
+
impliedActions: [],
|
|
423
|
+
impliedTemporals: [],
|
|
424
|
+
};
|
|
425
|
+
const next: { expression: Expression; scope: Scope | undefined }[] = [];
|
|
426
|
+
const specification =
|
|
427
|
+
config.specification === undefined
|
|
428
|
+
? undefined
|
|
429
|
+
: getOperatorDefinition(config.specification, environment);
|
|
430
|
+
if (specification) {
|
|
431
|
+
describeSpecification(specification.body, undefined, environment, description, next);
|
|
432
|
+
next.forEach((item) =>
|
|
433
|
+
description.actions.push(
|
|
434
|
+
...getActions(item.expression, item.scope, undefined, environment, instanceActions),
|
|
435
|
+
),
|
|
436
|
+
);
|
|
437
|
+
}
|
|
438
|
+
const init =
|
|
439
|
+
config.init === undefined ? undefined : resolveGlobalOperator(config.init, environment);
|
|
440
|
+
if (init?.kind === "Closure") description.inits.push(createAction(config.init ?? "", init.body));
|
|
441
|
+
const nextOperator =
|
|
442
|
+
config.next === undefined ? undefined : resolveGlobalOperator(config.next, environment);
|
|
443
|
+
if (nextOperator?.kind === "Closure") {
|
|
444
|
+
description.actions.push(
|
|
445
|
+
...getActions(
|
|
446
|
+
nextOperator.body,
|
|
447
|
+
nextOperator.scope,
|
|
448
|
+
nextOperator,
|
|
449
|
+
environment,
|
|
450
|
+
instanceActions,
|
|
451
|
+
),
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
config.invariants.forEach((name) => {
|
|
455
|
+
const definition = getOperatorDefinition(name, environment);
|
|
456
|
+
if (definition) description.invariants.push(createAction(name, definition.body));
|
|
457
|
+
});
|
|
458
|
+
config.properties.forEach((name) => {
|
|
459
|
+
const definition = getOperatorDefinition(name, environment);
|
|
460
|
+
if (definition) describeProperty(name, definition.body, undefined, environment, description);
|
|
461
|
+
});
|
|
462
|
+
return description;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
// TLC's split of the next-state relation into the actions it names in a behavior.
|
|
466
|
+
export const getNextActions = (
|
|
467
|
+
config: ModelConfig,
|
|
468
|
+
environment: Environment,
|
|
469
|
+
instanceActions: Map<string, InstanceAction>,
|
|
470
|
+
) => describe(config, environment, instanceActions).actions;
|
|
471
|
+
|
|
472
|
+
const formatParameters = (action: SpecAction) =>
|
|
473
|
+
action.parameters.length === 0
|
|
474
|
+
? ""
|
|
475
|
+
: `(${action.parameters.map((parameter) => formatValue(parameter[1])).join(",")})`;
|
|
476
|
+
|
|
477
|
+
export const getActionLabel = (action: SpecAction) =>
|
|
478
|
+
`${action.name === UNNAMED_ACTION ? "Action" : action.name}${formatParameters(action)} ${formatRange(action.location.range ?? BUILTIN_RANGE)}`;
|
|
479
|
+
|
|
480
|
+
// TLC's Action.getInvocationSignature, which names the action and the values of its parameters.
|
|
481
|
+
export const getActionSignature = (action: SpecAction) =>
|
|
482
|
+
`${action.name}${formatParameters(action)}`;
|
|
483
|
+
|
|
484
|
+
export const getSpecificationValue = (
|
|
485
|
+
config: ModelConfig,
|
|
486
|
+
environment: Environment,
|
|
487
|
+
variableLocations: SourceLocation[],
|
|
488
|
+
instanceActions: Map<string, InstanceAction>,
|
|
489
|
+
): Value => {
|
|
490
|
+
const description = describe(config, environment, instanceActions);
|
|
491
|
+
const toSet = (actions: SpecAction[]) => createSet(actions.map(toActionValue));
|
|
492
|
+
const toDefinitionSet = (names: string[]) =>
|
|
493
|
+
createSet(
|
|
494
|
+
names.flatMap((name) => {
|
|
495
|
+
const definition = getOperatorDefinition(name, environment);
|
|
496
|
+
return definition ? [describeDefinition(definition)] : [];
|
|
497
|
+
}),
|
|
498
|
+
);
|
|
499
|
+
return createRecord([
|
|
500
|
+
["inits", toSet(description.inits)],
|
|
501
|
+
["actions", toSet(description.actions)],
|
|
502
|
+
["temporals", toSet(description.temporals)],
|
|
503
|
+
["invariants", toSet(description.invariants)],
|
|
504
|
+
["impliedinits", toSet(description.impliedInits)],
|
|
505
|
+
["impliedactions", toSet(description.impliedActions)],
|
|
506
|
+
["impliedtemporals", toSet(description.impliedTemporals)],
|
|
507
|
+
[
|
|
508
|
+
"variables",
|
|
509
|
+
createSet(
|
|
510
|
+
environment.variables.map((name, index) =>
|
|
511
|
+
createRecord([
|
|
512
|
+
["name", name],
|
|
513
|
+
["location", toLocationValue(variableLocations[index])],
|
|
514
|
+
]),
|
|
515
|
+
),
|
|
516
|
+
),
|
|
517
|
+
],
|
|
518
|
+
["actionconstraints", toDefinitionSet(config.actionConstraints)],
|
|
519
|
+
["constraints", toDefinitionSet(config.constraints)],
|
|
520
|
+
]);
|
|
521
|
+
};
|