tla-ts 0.0.1 → 0.0.3-dev.19c763f
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 +1086 -244
- 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 +547 -255
- 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 +710 -0
- package/src/liveness.ts +37 -599
- 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 +223 -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 +276 -53
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +43 -37
- 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 +411 -0
- package/src/tlc-summary.ts +109 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1245 -176
package/src/environment.ts
CHANGED
|
@@ -1,29 +1,52 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type Assumption,
|
|
4
|
+
type Declaration,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type Instance,
|
|
8
|
+
type Module,
|
|
9
|
+
type OperatorParam,
|
|
10
|
+
type SourceLocation,
|
|
10
11
|
} from "./ast";
|
|
11
12
|
import { BUNDLED_MODULES } from "./bundled-modules";
|
|
12
13
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
13
14
|
import type { FileSystem } from "./json";
|
|
14
15
|
import { TlaConfigError, TlaEvaluationError, TlaSyntaxError } from "./errors";
|
|
15
|
-
import {
|
|
16
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
17
|
+
import {
|
|
18
|
+
createBigNumberError,
|
|
19
|
+
createRealNumberError,
|
|
20
|
+
evaluate,
|
|
21
|
+
getDefinition,
|
|
22
|
+
isTlcInteger,
|
|
23
|
+
type Builtin,
|
|
24
|
+
type Environment,
|
|
25
|
+
} from "./evaluate";
|
|
16
26
|
import {
|
|
17
27
|
createInstantiation,
|
|
18
28
|
getEnabledVariableName,
|
|
19
29
|
renameLocals,
|
|
20
30
|
withLetInstances,
|
|
21
31
|
} from "./instances";
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
32
|
+
import { overrideDefinitions } from "./java-overrides";
|
|
33
|
+
import { extractModule, parseModule } from "./parser";
|
|
34
|
+
import {
|
|
35
|
+
checkRedefinitions,
|
|
36
|
+
getArity,
|
|
37
|
+
isBefore,
|
|
38
|
+
KEY_SEPARATOR,
|
|
39
|
+
LOCAL_RESOLUTION,
|
|
40
|
+
resolveModule,
|
|
41
|
+
resolveSubstitutions,
|
|
42
|
+
type ModuleSymbol,
|
|
43
|
+
type Symbols,
|
|
44
|
+
} from "./resolve";
|
|
24
45
|
import { createRuntime } from "./runtime";
|
|
25
|
-
import { CORE_OPERATORS, getStandardModule } from "./standard-modules";
|
|
46
|
+
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
26
47
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
48
|
+
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
49
|
+
import { checkInstances, type InstanceCheck } from "./substitution-levels";
|
|
27
50
|
import {
|
|
28
51
|
checkInteger,
|
|
29
52
|
createModelValue,
|
|
@@ -33,21 +56,36 @@ import {
|
|
|
33
56
|
type Value,
|
|
34
57
|
} from "./values";
|
|
35
58
|
|
|
36
|
-
|
|
59
|
+
interface EnvironmentOptions {
|
|
60
|
+
source: string;
|
|
37
61
|
loadModule: ((name: string) => string | undefined) | undefined;
|
|
62
|
+
loadClass?: (name: string) => Uint8Array | undefined;
|
|
38
63
|
print: (text: string) => void;
|
|
39
64
|
files: FileSystem;
|
|
65
|
+
seed?: bigint;
|
|
40
66
|
}
|
|
41
67
|
|
|
42
|
-
|
|
68
|
+
interface Specification {
|
|
43
69
|
environment: Environment;
|
|
44
70
|
assumptions: Assumption[];
|
|
71
|
+
variableLocations: SourceLocation[];
|
|
72
|
+
instanceActions: Map<string, InstanceAction>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface InstanceAction {
|
|
76
|
+
name: string;
|
|
77
|
+
location: SourceLocation;
|
|
45
78
|
}
|
|
46
79
|
|
|
47
80
|
const toValue = (value: ConfigValue): Value => {
|
|
48
81
|
switch (value.kind) {
|
|
49
82
|
case "Number":
|
|
50
|
-
|
|
83
|
+
try {
|
|
84
|
+
return checkInteger(value.value);
|
|
85
|
+
} catch (error) {
|
|
86
|
+
// TLC fails with an unexpected exception on a configuration number too big for it.
|
|
87
|
+
throw setExitCode(error, EXIT_STATUS.error);
|
|
88
|
+
}
|
|
51
89
|
case "String":
|
|
52
90
|
case "Boolean":
|
|
53
91
|
return value.value;
|
|
@@ -62,11 +100,29 @@ interface LinkedModule {
|
|
|
62
100
|
module: Module;
|
|
63
101
|
constants: Declaration[];
|
|
64
102
|
variables: string[];
|
|
103
|
+
variableLocations: SourceLocation[];
|
|
65
104
|
visible: Symbols;
|
|
66
105
|
exported: Symbols;
|
|
106
|
+
modules: Map<string, LinkedModule>;
|
|
107
|
+
extended: LinkedModule[];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface ModuleScope {
|
|
111
|
+
id: string;
|
|
112
|
+
symbols: Symbols;
|
|
113
|
+
modules: Map<string, LinkedModule>;
|
|
114
|
+
nested: Module[];
|
|
67
115
|
}
|
|
68
116
|
|
|
69
|
-
const
|
|
117
|
+
const getDeclarations = (linked: LinkedModule) => [
|
|
118
|
+
...linked.constants,
|
|
119
|
+
...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
const createModuleRedefinedError = (module: Module) =>
|
|
123
|
+
new TlaSyntaxError(
|
|
124
|
+
`Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
|
|
125
|
+
);
|
|
70
126
|
|
|
71
127
|
const createLocalFunction = (definition: Definition, key: string): Definition => ({
|
|
72
128
|
kind: "Operator",
|
|
@@ -88,37 +144,42 @@ const createLocalFunction = (definition: Definition, key: string): Definition =>
|
|
|
88
144
|
location: definition.location,
|
|
89
145
|
});
|
|
90
146
|
|
|
91
|
-
const createOperatorConstant = (name: string, rows: [Value[], Value][]): Builtin =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
147
|
+
const createOperatorConstant = (name: string, arity: number, rows: [Value[], Value][]): Builtin => {
|
|
148
|
+
const table = `{ ${rows.map((row) => `<${[...row[0], row[1]].map((value) => formatValue(value)).join(", ")}>`).join(", ")}}`;
|
|
149
|
+
return {
|
|
150
|
+
kind: "Builtin",
|
|
151
|
+
name,
|
|
152
|
+
arity,
|
|
153
|
+
evaluate: (args) => {
|
|
154
|
+
const row = rows.find((candidate) => {
|
|
155
|
+
if (candidate[0].length !== args.length) {
|
|
156
|
+
throw new TlaEvaluationError(
|
|
157
|
+
`Attempted to apply the operator ${table}\nwith wrong number of arguments.`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
return candidate[0].every((value, index) => {
|
|
161
|
+
const argument = args[index];
|
|
162
|
+
return (
|
|
163
|
+
!(typeof argument === "object" && argument.kind === "Operator") &&
|
|
164
|
+
valuesEqual(value, argument)
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
if (row) return row[1];
|
|
169
|
+
throw new TlaEvaluationError(
|
|
170
|
+
`Attempted to apply operator:\n${table}\nto arguments (${args.map((argument) => (typeof argument === "object" && argument.kind === "Operator" ? argument.name : formatValue(argument))).join(", ")}), which is undefined.`,
|
|
171
|
+
);
|
|
172
|
+
},
|
|
173
|
+
evaluateUnary: undefined,
|
|
174
|
+
evaluateBinary: undefined,
|
|
175
|
+
};
|
|
176
|
+
};
|
|
113
177
|
|
|
114
178
|
const mentionsName = (expression: Expression, names: Set<string>): boolean =>
|
|
115
179
|
(expression.kind === "Identifier" && names.has(expression.name)) ||
|
|
116
180
|
(expression.kind === "Apply" && names.has(expression.operator)) ||
|
|
117
181
|
getChildren(expression).some((child) => mentionsName(child, names));
|
|
118
182
|
|
|
119
|
-
const getArity = (definition: Definition) =>
|
|
120
|
-
definition.kind === "Operator" ? definition.params.length : 0;
|
|
121
|
-
|
|
122
183
|
const getVariableIndex = (variables: string[], declared: string[]) => {
|
|
123
184
|
const index = new Map(variables.map((name, position) => [name, position]));
|
|
124
185
|
new Set(declared).forEach((name) =>
|
|
@@ -127,20 +188,55 @@ const getVariableIndex = (variables: string[], declared: string[]) => {
|
|
|
127
188
|
return index;
|
|
128
189
|
};
|
|
129
190
|
|
|
191
|
+
const getSubstitutions = (instances: Instance[]) =>
|
|
192
|
+
instances.flatMap((instance) =>
|
|
193
|
+
instance.substitutions.map((substitution) => substitution.expression),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const rejectLiterals = (expressions: Expression[]): void =>
|
|
197
|
+
expressions.forEach((expression) => {
|
|
198
|
+
if (expression.kind === "Decimal") throw createRealNumberError(expression);
|
|
199
|
+
if (expression.kind === "Number" && !isTlcInteger(expression.value)) {
|
|
200
|
+
throw createBigNumberError(expression);
|
|
201
|
+
}
|
|
202
|
+
rejectLiterals(
|
|
203
|
+
expression.kind === "Let"
|
|
204
|
+
? [...getChildren(expression), ...getSubstitutions(expression.instances ?? [])]
|
|
205
|
+
: getChildren(expression),
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
130
209
|
export const createEnvironment = (
|
|
131
|
-
root: Module,
|
|
132
210
|
config: ModelConfig,
|
|
133
211
|
options: EnvironmentOptions,
|
|
134
212
|
): Specification => {
|
|
213
|
+
const root = parseModule(options.source);
|
|
135
214
|
const definitions = new Map<string, Definition>();
|
|
136
215
|
const parameterized = new Map<string, boolean>();
|
|
216
|
+
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
217
|
+
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
137
218
|
const builtins = new Map<string, Builtin>(
|
|
138
219
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
139
220
|
);
|
|
140
221
|
const standardExports = new Map<string, Symbols>();
|
|
141
|
-
const runtime = createRuntime(config.checkDeadlock);
|
|
222
|
+
const runtime = createRuntime(config.checkDeadlock, options.seed);
|
|
142
223
|
const linkedModules = new Map<string, LinkedModule>();
|
|
224
|
+
const nestedModules = new Map<string, LinkedModule>();
|
|
225
|
+
const linkedList: LinkedModule[] = [];
|
|
226
|
+
const dependencies = new Map<string, Set<string>>();
|
|
227
|
+
const loading: Set<string>[] = [];
|
|
228
|
+
// The top-level modules being linked, to report a cycle of EXTENDS or INSTANCE.
|
|
229
|
+
const linking: string[] = [];
|
|
230
|
+
|
|
231
|
+
const useModule = (name: string, used: Iterable<string>) =>
|
|
232
|
+
loading.forEach((closure) => {
|
|
233
|
+
closure.add(name);
|
|
234
|
+
[...used].forEach((dependency) => closure.add(dependency));
|
|
235
|
+
});
|
|
143
236
|
const copyCount = { value: 0 };
|
|
237
|
+
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
238
|
+
const instanceChecks: InstanceCheck[] = [];
|
|
239
|
+
const instanceActions = new Map<string, InstanceAction>();
|
|
144
240
|
|
|
145
241
|
const addStandardModule = (name: string): Symbols | undefined => {
|
|
146
242
|
const cached = standardExports.get(name);
|
|
@@ -161,65 +257,108 @@ export const createEnvironment = (
|
|
|
161
257
|
return exported;
|
|
162
258
|
};
|
|
163
259
|
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
260
|
+
const getStandardSymbols = (name: string) => {
|
|
261
|
+
const symbols = addStandardModule(name);
|
|
262
|
+
if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
|
|
263
|
+
return symbols;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// Like TLC, reject a decimal or a big integer anywhere in a module, even where nothing evaluates it.
|
|
267
|
+
const rejectModuleLiterals = (module: Module): Module => {
|
|
268
|
+
rejectLiterals([
|
|
269
|
+
...module.definitions.flatMap((definition) =>
|
|
270
|
+
definition.kind === "Function"
|
|
271
|
+
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
272
|
+
: [definition.body],
|
|
273
|
+
),
|
|
274
|
+
...module.assumptions.map((assumption) => assumption.expression),
|
|
275
|
+
...module.theorems.flatMap((theorem) =>
|
|
276
|
+
theorem.statement.kind === "AssumeProve" ? [] : [theorem.statement],
|
|
277
|
+
),
|
|
278
|
+
...getSubstitutions(module.instances),
|
|
279
|
+
]);
|
|
280
|
+
module.modules.forEach(rejectModuleLiterals);
|
|
172
281
|
return module;
|
|
173
282
|
};
|
|
174
|
-
|
|
283
|
+
rejectModuleLiterals(root);
|
|
175
284
|
|
|
176
|
-
const loadModule = (name: string) => {
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
|
|
285
|
+
const loadModule = (name: string, importer: string) => {
|
|
286
|
+
const source =
|
|
287
|
+
options.loadModule?.(name) ??
|
|
288
|
+
BUNDLED_MODULES.get(name) ??
|
|
289
|
+
extractModule(options.source, name);
|
|
180
290
|
if (source === undefined)
|
|
181
|
-
throw new TlaSyntaxError(
|
|
182
|
-
|
|
291
|
+
throw new TlaSyntaxError(
|
|
292
|
+
`Cannot find source file for module ${name} imported in module ${importer}.`,
|
|
293
|
+
);
|
|
294
|
+
const module = parseModule(source);
|
|
295
|
+
if (module.name !== name) {
|
|
296
|
+
throw new TlaSyntaxError(
|
|
297
|
+
`File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
return rejectModuleLiterals(module);
|
|
183
301
|
};
|
|
184
302
|
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return getDefinitionKey(outer, name);
|
|
303
|
+
const linkTopLevel = (parsed: Module) => {
|
|
304
|
+
const cycleStart = linking.indexOf(parsed.name);
|
|
305
|
+
if (cycleStart !== -1) {
|
|
306
|
+
const cycle = [...linking.slice(cycleStart), parsed.name].map((name) => `${name}.tla`);
|
|
307
|
+
throw new TlaSyntaxError(
|
|
308
|
+
`Circular dependency among .tla files; dependency cycle is: ${cycle.join(" --> ")}`,
|
|
309
|
+
);
|
|
193
310
|
}
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
311
|
+
const closure = new Set<string>();
|
|
312
|
+
const nested: Module[] = [];
|
|
313
|
+
linking.push(parsed.name);
|
|
314
|
+
loading.push(closure);
|
|
315
|
+
const linked = linkModule(parsed, {
|
|
316
|
+
id: parsed.name,
|
|
317
|
+
symbols: new Map(),
|
|
318
|
+
modules: new Map(),
|
|
319
|
+
nested,
|
|
320
|
+
});
|
|
321
|
+
loading.pop();
|
|
322
|
+
linking.pop();
|
|
323
|
+
const duplicate = nested.find((module) => closure.has(module.name));
|
|
324
|
+
if (duplicate) throw createModuleRedefinedError(duplicate);
|
|
325
|
+
return { linked, closure };
|
|
197
326
|
};
|
|
198
327
|
|
|
199
|
-
const getLinkedModule = (name: string
|
|
328
|
+
const getLinkedModule = (name: string, importer: string): LinkedModule => {
|
|
329
|
+
const cached = linkedModules.get(name);
|
|
330
|
+
if (cached) {
|
|
331
|
+
useModule(name, dependencies.get(name) ?? []);
|
|
332
|
+
return cached;
|
|
333
|
+
}
|
|
334
|
+
const top = linkTopLevel(loadModule(name, importer));
|
|
335
|
+
linkedModules.set(name, top.linked);
|
|
336
|
+
dependencies.set(name, top.closure);
|
|
337
|
+
useModule(name, top.closure);
|
|
338
|
+
return top.linked;
|
|
339
|
+
};
|
|
200
340
|
|
|
201
341
|
const instantiate = (
|
|
202
342
|
instance: Instance,
|
|
203
343
|
module: Module,
|
|
204
344
|
unit: {
|
|
205
345
|
imported: Symbols;
|
|
346
|
+
importedAt: Map<string, SourceLocation>;
|
|
206
347
|
definitions: Map<string, Definition>;
|
|
207
348
|
getKey: (definition: Definition) => string;
|
|
208
349
|
},
|
|
209
350
|
isParameterFree: boolean,
|
|
351
|
+
nested: LinkedModule | undefined,
|
|
210
352
|
): Symbols => {
|
|
211
|
-
const standardSymbols =
|
|
353
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
212
354
|
if (standardSymbols) {
|
|
213
355
|
if (instance.name === undefined) return standardSymbols;
|
|
214
356
|
return new Map(
|
|
215
357
|
[...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
|
|
216
358
|
);
|
|
217
359
|
}
|
|
218
|
-
const linked = getLinkedModule(instance.module);
|
|
219
|
-
const declarations =
|
|
220
|
-
...linked.constants,
|
|
221
|
-
...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
|
|
222
|
-
];
|
|
360
|
+
const linked = nested ?? getLinkedModule(instance.module, module.name);
|
|
361
|
+
const declarations = getDeclarations(linked);
|
|
223
362
|
const suffix = `#${copyCount.value++}`;
|
|
224
363
|
const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
|
|
225
364
|
const substitutions = resolveSubstitutions(
|
|
@@ -228,6 +367,13 @@ export const createEnvironment = (
|
|
|
228
367
|
module,
|
|
229
368
|
unit,
|
|
230
369
|
);
|
|
370
|
+
instanceChecks.push({
|
|
371
|
+
module: instance.module,
|
|
372
|
+
instancer: module.name,
|
|
373
|
+
lets: [],
|
|
374
|
+
substitutions,
|
|
375
|
+
location: instance.location,
|
|
376
|
+
});
|
|
231
377
|
const instantiation = createInstantiation({
|
|
232
378
|
params: instance.params.map((param) => ({ ...param, name: `${param.name}${suffix}` })),
|
|
233
379
|
substitutions: new Map(
|
|
@@ -236,23 +382,32 @@ export const createEnvironment = (
|
|
|
236
382
|
variables: new Set(linked.variables),
|
|
237
383
|
getDefinition: (key) => definitions.get(key),
|
|
238
384
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
239
|
-
addDefinition: (name, create) => {
|
|
240
|
-
const key = `${name}@${module.name}
|
|
385
|
+
addDefinition: (source, name, create) => {
|
|
386
|
+
const key = createName(`${name}@${module.name}`);
|
|
241
387
|
definitions.set(key, create(key));
|
|
242
388
|
parameterized.set(key, !isParameterFree);
|
|
389
|
+
copySources.set(key, { source, prefix: instance.params.length });
|
|
243
390
|
return key;
|
|
244
391
|
},
|
|
392
|
+
createName,
|
|
245
393
|
});
|
|
246
394
|
const declared = new Set(declarations.map((declaration) => declaration.name));
|
|
247
395
|
const symbols: Symbols = new Map();
|
|
248
396
|
linked.exported.forEach((symbol, name) => {
|
|
249
397
|
if (declared.has(name)) return;
|
|
250
398
|
if (instance.name === undefined) {
|
|
251
|
-
|
|
399
|
+
const key = instantiation.getKey(symbol.key);
|
|
400
|
+
if (declarations.length > 0)
|
|
401
|
+
instanceActions.set(key, { name, location: instance.location });
|
|
402
|
+
symbols.set(name, { key, arity: symbol.arity });
|
|
252
403
|
return;
|
|
253
404
|
}
|
|
405
|
+
const key = instantiation.getQualifiedKey(symbol.key);
|
|
406
|
+
if (declarations.length > 0) {
|
|
407
|
+
instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
|
|
408
|
+
}
|
|
254
409
|
symbols.set(`${instance.name}!${name}`, {
|
|
255
|
-
key
|
|
410
|
+
key,
|
|
256
411
|
arity: symbol.arity + instance.params.length,
|
|
257
412
|
});
|
|
258
413
|
});
|
|
@@ -291,7 +446,7 @@ export const createEnvironment = (
|
|
|
291
446
|
|
|
292
447
|
const getSubstitutionBody = (
|
|
293
448
|
replacement: Expression,
|
|
294
|
-
declaration:
|
|
449
|
+
declaration: OperatorParam,
|
|
295
450
|
location: SourceLocation,
|
|
296
451
|
): { params: OperatorParam[]; body: Expression } => {
|
|
297
452
|
if (declaration.arity === 0) return { params: [], body: replacement };
|
|
@@ -314,10 +469,14 @@ export const createEnvironment = (
|
|
|
314
469
|
};
|
|
315
470
|
};
|
|
316
471
|
|
|
317
|
-
const instantiateLocally = (
|
|
472
|
+
const instantiateLocally = (
|
|
473
|
+
instance: Instance,
|
|
474
|
+
lets: Definition[],
|
|
475
|
+
instancer: string,
|
|
476
|
+
nested: LinkedModule | undefined,
|
|
477
|
+
): Definition[] => {
|
|
318
478
|
const location = instance.location;
|
|
319
|
-
const
|
|
320
|
-
const standardSymbols = addStandardModule(instance.module);
|
|
479
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
321
480
|
if (standardSymbols) {
|
|
322
481
|
if (instance.name === undefined) return [];
|
|
323
482
|
return [...standardSymbols].map(([name, symbol]) => {
|
|
@@ -325,23 +484,29 @@ export const createEnvironment = (
|
|
|
325
484
|
return createLocalAlias(
|
|
326
485
|
`${instance.name}!${name}`,
|
|
327
486
|
symbol.key,
|
|
328
|
-
[...
|
|
487
|
+
[...instance.params, ...forwarded],
|
|
329
488
|
forwarded,
|
|
330
489
|
location,
|
|
331
490
|
);
|
|
332
491
|
});
|
|
333
492
|
}
|
|
334
|
-
const linked = getLinkedModule(instance.module);
|
|
493
|
+
const linked = nested ?? getLinkedModule(instance.module, instancer);
|
|
335
494
|
const suffix = `#${copyCount.value++}`;
|
|
336
495
|
const explicit = new Map(instance.substitutions.map((item) => [item.name, item.expression]));
|
|
337
|
-
const declarations =
|
|
338
|
-
|
|
339
|
-
...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
|
|
340
|
-
];
|
|
341
|
-
const getReplacement = (declaration: Declaration): Expression =>
|
|
496
|
+
const declarations = getDeclarations(linked);
|
|
497
|
+
const getReplacement = (declaration: OperatorParam): Expression =>
|
|
342
498
|
explicit.get(declaration.name) ?? { kind: "Identifier", name: declaration.name, location };
|
|
343
|
-
|
|
344
|
-
|
|
499
|
+
instanceChecks.push({
|
|
500
|
+
module: instance.module,
|
|
501
|
+
instancer,
|
|
502
|
+
lets,
|
|
503
|
+
substitutions: new Map(
|
|
504
|
+
declarations.map((declaration) => [declaration.name, getReplacement(declaration)]),
|
|
505
|
+
),
|
|
506
|
+
location,
|
|
507
|
+
});
|
|
508
|
+
const instanceNames = new Set(instance.params.map((param) => param.name));
|
|
509
|
+
const isParameterDependent = (declaration: OperatorParam) =>
|
|
345
510
|
mentionsName(getReplacement(declaration), instanceNames);
|
|
346
511
|
const substitutionDefinitions = declarations.map((declaration): Definition => {
|
|
347
512
|
const substitution = getSubstitutionBody(getReplacement(declaration), declaration, location);
|
|
@@ -349,7 +514,7 @@ export const createEnvironment = (
|
|
|
349
514
|
kind: "Operator",
|
|
350
515
|
name: `${declaration.name}${suffix}`,
|
|
351
516
|
params: [
|
|
352
|
-
...(isParameterDependent(declaration) ?
|
|
517
|
+
...(isParameterDependent(declaration) ? instance.params : []),
|
|
353
518
|
...substitution.params,
|
|
354
519
|
],
|
|
355
520
|
body: substitution.body,
|
|
@@ -360,7 +525,7 @@ export const createEnvironment = (
|
|
|
360
525
|
});
|
|
361
526
|
const copies: Definition[] = [];
|
|
362
527
|
const instantiation = createInstantiation({
|
|
363
|
-
params:
|
|
528
|
+
params: instance.params,
|
|
364
529
|
substitutions: new Map(
|
|
365
530
|
declarations.map((declaration): [string, Expression] => [
|
|
366
531
|
declaration.name,
|
|
@@ -374,7 +539,7 @@ export const createEnvironment = (
|
|
|
374
539
|
: {
|
|
375
540
|
kind: "Apply",
|
|
376
541
|
operator: `${declaration.name}${suffix}`,
|
|
377
|
-
args: getLocalArguments(
|
|
542
|
+
args: getLocalArguments(instance.params, location),
|
|
378
543
|
location,
|
|
379
544
|
resolution: LOCAL_RESOLUTION,
|
|
380
545
|
},
|
|
@@ -383,7 +548,7 @@ export const createEnvironment = (
|
|
|
383
548
|
variables: new Set(linked.variables),
|
|
384
549
|
getDefinition: (key) => definitions.get(key),
|
|
385
550
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
386
|
-
addDefinition: (name, create) => {
|
|
551
|
+
addDefinition: (_source, name, create) => {
|
|
387
552
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
|
388
553
|
const definition = create(key);
|
|
389
554
|
copies.push(
|
|
@@ -393,6 +558,7 @@ export const createEnvironment = (
|
|
|
393
558
|
);
|
|
394
559
|
return key;
|
|
395
560
|
},
|
|
561
|
+
createName,
|
|
396
562
|
});
|
|
397
563
|
const declared = new Set(declarations.map((declaration) => declaration.name));
|
|
398
564
|
const aliases = [...linked.exported]
|
|
@@ -400,7 +566,7 @@ export const createEnvironment = (
|
|
|
400
566
|
.map(([name, symbol]) => {
|
|
401
567
|
const key = instantiation.getKey(symbol.key);
|
|
402
568
|
const own = getArguments(symbol.arity);
|
|
403
|
-
const params = [...
|
|
569
|
+
const params = [...instance.params, ...own];
|
|
404
570
|
return createLocalAlias(
|
|
405
571
|
instance.name === undefined ? name : `${instance.name}!${name}`,
|
|
406
572
|
key,
|
|
@@ -418,61 +584,139 @@ export const createEnvironment = (
|
|
|
418
584
|
return definition ? [{ ...definition, name }] : [];
|
|
419
585
|
});
|
|
420
586
|
|
|
421
|
-
const linkModule = (
|
|
422
|
-
const
|
|
423
|
-
|
|
587
|
+
const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
|
|
588
|
+
const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
|
|
589
|
+
const visible: Symbols = new Map([
|
|
590
|
+
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
424
591
|
operator.name,
|
|
425
592
|
{ key: operator.name, arity: operator.arity },
|
|
426
593
|
]),
|
|
427
|
-
|
|
594
|
+
...scope.symbols,
|
|
595
|
+
]);
|
|
428
596
|
const exported: Symbols = new Map();
|
|
597
|
+
const modules = new Map<string, LinkedModule>();
|
|
598
|
+
const extended: LinkedModule[] = [];
|
|
429
599
|
const constants = [...parsed.constants];
|
|
430
|
-
const variables =
|
|
600
|
+
const variables = parsed.variables.map((variable) => variable.name);
|
|
601
|
+
const variableLocations = parsed.variables.map((variable) => variable.location);
|
|
431
602
|
const addSymbols = (symbols: Symbols, isExported: boolean) =>
|
|
432
603
|
symbols.forEach((symbol, name) => {
|
|
433
604
|
visible.set(name, symbol);
|
|
434
605
|
if (isExported) exported.set(name, symbol);
|
|
435
606
|
});
|
|
436
|
-
parsed.outer?.extends.forEach((name) =>
|
|
437
|
-
addSymbols(addStandardModule(name) ?? getLinkedModule(name).exported, false),
|
|
438
|
-
);
|
|
439
|
-
parsed.outer?.symbols.forEach((symbol) =>
|
|
440
|
-
visible.set(symbol.name, { key: getOuterKey(parsed, symbol.name), arity: symbol.arity }),
|
|
441
|
-
);
|
|
442
607
|
parsed.extends.forEach((name) => {
|
|
443
|
-
const
|
|
608
|
+
const nested = scope.modules.get(name);
|
|
609
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(name);
|
|
444
610
|
if (standardSymbols) {
|
|
445
611
|
addSymbols(standardSymbols, true);
|
|
446
612
|
return;
|
|
447
613
|
}
|
|
448
|
-
const linked = getLinkedModule(name);
|
|
614
|
+
const linked = nested ?? getLinkedModule(name, parsed.name);
|
|
449
615
|
constants.push(
|
|
450
616
|
...linked.constants.filter((constant) =>
|
|
451
617
|
constants.every((existing) => existing.name !== constant.name),
|
|
452
618
|
),
|
|
453
619
|
);
|
|
454
|
-
|
|
620
|
+
linked.variables.forEach((variable, index) => {
|
|
621
|
+
if (variables.includes(variable)) return;
|
|
622
|
+
variables.push(variable);
|
|
623
|
+
variableLocations.push(linked.variableLocations[index]);
|
|
624
|
+
});
|
|
455
625
|
addSymbols(linked.exported, true);
|
|
626
|
+
linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
|
|
627
|
+
extended.push(linked);
|
|
456
628
|
});
|
|
457
629
|
parsed.constants.forEach((constant) =>
|
|
458
|
-
exported.set(constant.name, {
|
|
630
|
+
exported.set(constant.name, {
|
|
631
|
+
key: constant.name,
|
|
632
|
+
arity: constant.arity,
|
|
633
|
+
declaration: "Constant",
|
|
634
|
+
}),
|
|
635
|
+
);
|
|
636
|
+
parsed.variables.forEach((variable) =>
|
|
637
|
+
exported.set(variable.name, { key: variable.name, arity: 0, declaration: "Variable" }),
|
|
459
638
|
);
|
|
460
|
-
parsed.variables.forEach((variable) => exported.set(variable, { key: variable, arity: 0 }));
|
|
461
639
|
const isParameterFree = constants.length === 0 && variables.length === 0;
|
|
462
|
-
const getKey = (definition: Definition) =>
|
|
640
|
+
const getKey = (definition: Definition) =>
|
|
641
|
+
parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
|
|
642
|
+
const importedAt = new Map<string, SourceLocation>();
|
|
463
643
|
const getUnit = (module: Module) => ({
|
|
464
644
|
imported: visible,
|
|
645
|
+
importedAt,
|
|
465
646
|
definitions: new Map(module.definitions.map((definition) => [definition.name, definition])),
|
|
466
647
|
getKey,
|
|
467
648
|
});
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
649
|
+
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
650
|
+
const getSymbolsBefore = (location: SourceLocation): Symbols =>
|
|
651
|
+
new Map([
|
|
652
|
+
...visible,
|
|
653
|
+
...[
|
|
654
|
+
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
655
|
+
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
656
|
+
]
|
|
657
|
+
.filter((declaration) => isBefore(declaration.location, location))
|
|
658
|
+
.map((declaration): [string, ModuleSymbol] => [
|
|
659
|
+
declaration.name,
|
|
660
|
+
{ key: declaration.name, arity: declaration.arity, declaration: declaration.kind },
|
|
661
|
+
]),
|
|
662
|
+
...parsed.definitions
|
|
663
|
+
.filter((definition) => isBefore(definition.location, location))
|
|
664
|
+
.map((definition): [string, ModuleSymbol] => [
|
|
665
|
+
definition.name,
|
|
666
|
+
{ key: getKey(definition), arity: getArity(definition) },
|
|
667
|
+
]),
|
|
668
|
+
]);
|
|
669
|
+
const linkNested = (nested: Module) => {
|
|
670
|
+
const visibleModules = getVisibleModules();
|
|
671
|
+
if (visibleModules.has(nested.name)) throw createModuleRedefinedError(nested);
|
|
672
|
+
scope.nested.push(nested);
|
|
673
|
+
const linked = linkModule(nested, {
|
|
674
|
+
id: `${scope.id}/${nested.name}`,
|
|
675
|
+
symbols: getSymbolsBefore(nested.location),
|
|
676
|
+
modules: visibleModules,
|
|
677
|
+
nested: scope.nested,
|
|
678
|
+
});
|
|
679
|
+
modules.set(nested.name, linked);
|
|
680
|
+
if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
|
|
681
|
+
};
|
|
682
|
+
[
|
|
683
|
+
...parsed.instances.map((instance) => ({
|
|
684
|
+
location: instance.location,
|
|
685
|
+
link: () => {
|
|
686
|
+
const symbols = instantiate(
|
|
687
|
+
instance,
|
|
688
|
+
parsed,
|
|
689
|
+
getUnit(parsed),
|
|
690
|
+
isParameterFree,
|
|
691
|
+
getVisibleModules().get(instance.module),
|
|
692
|
+
);
|
|
693
|
+
symbols.forEach((_, name) => {
|
|
694
|
+
if (!visible.has(name)) importedAt.set(name, instance.location);
|
|
695
|
+
});
|
|
696
|
+
addSymbols(symbols, !instance.isLocal);
|
|
697
|
+
},
|
|
698
|
+
})),
|
|
699
|
+
...parsed.modules.map((nested) => ({
|
|
700
|
+
location: nested.location,
|
|
701
|
+
link: () => linkNested(nested),
|
|
702
|
+
})),
|
|
703
|
+
]
|
|
704
|
+
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
705
|
+
.forEach((unit) => unit.link());
|
|
706
|
+
checkRedefinitions(parsed, getUnit(parsed));
|
|
707
|
+
const isInstancedAfterDefinition = (nested: Module) => {
|
|
708
|
+
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
709
|
+
return first !== undefined && isBefore(nested.location, first.location);
|
|
710
|
+
};
|
|
711
|
+
const getLetModule = (instance: Instance) => {
|
|
712
|
+
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
713
|
+
if (!own) return getVisibleModules().get(instance.module);
|
|
714
|
+
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
715
|
+
};
|
|
474
716
|
const module = resolveModuleSubexpressions(
|
|
475
|
-
withLetInstances(parsed,
|
|
717
|
+
withLetInstances(parsed, (instance, lets) =>
|
|
718
|
+
instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
|
|
719
|
+
),
|
|
476
720
|
getAvailableDefinitions(visible),
|
|
477
721
|
);
|
|
478
722
|
resolveModule(module, getUnit(module));
|
|
@@ -484,12 +728,26 @@ export const createEnvironment = (
|
|
|
484
728
|
visible.set(definition.name, symbol);
|
|
485
729
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
486
730
|
});
|
|
487
|
-
|
|
488
|
-
|
|
731
|
+
checkModuleLevels(module, {
|
|
732
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
733
|
+
variables: new Set(variables),
|
|
734
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
735
|
+
});
|
|
736
|
+
const linked = {
|
|
737
|
+
module,
|
|
738
|
+
constants,
|
|
739
|
+
variables,
|
|
740
|
+
variableLocations,
|
|
741
|
+
visible,
|
|
742
|
+
exported,
|
|
743
|
+
modules,
|
|
744
|
+
extended,
|
|
745
|
+
};
|
|
746
|
+
linkedList.push(linked);
|
|
489
747
|
return linked;
|
|
490
748
|
};
|
|
491
749
|
|
|
492
|
-
const linkedRoot =
|
|
750
|
+
const linkedRoot = linkTopLevel(root).linked;
|
|
493
751
|
const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
|
|
494
752
|
linkedRoot.visible.forEach((symbol, name) => {
|
|
495
753
|
const definition = definitions.get(symbol.key);
|
|
@@ -497,47 +755,80 @@ export const createEnvironment = (
|
|
|
497
755
|
});
|
|
498
756
|
|
|
499
757
|
const assumptions: Assumption[] = [];
|
|
500
|
-
const addAssumptions = (
|
|
501
|
-
if (visited.has(
|
|
502
|
-
visited.add(
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
if (linked) addAssumptions(linked.module, visited);
|
|
506
|
-
});
|
|
507
|
-
assumptions.push(...module.assumptions);
|
|
758
|
+
const addAssumptions = (linked: LinkedModule, visited: Set<LinkedModule>) => {
|
|
759
|
+
if (visited.has(linked)) return;
|
|
760
|
+
visited.add(linked);
|
|
761
|
+
linked.extended.forEach((extended) => addAssumptions(extended, visited));
|
|
762
|
+
assumptions.push(...linked.module.assumptions);
|
|
508
763
|
};
|
|
509
|
-
addAssumptions(linkedRoot
|
|
764
|
+
addAssumptions(linkedRoot, new Set());
|
|
510
765
|
const declaredConstants = linkedRoot.constants.map((constant) => constant.name);
|
|
511
766
|
const variables = linkedRoot.variables;
|
|
512
767
|
|
|
513
|
-
const getScopedKeys = (item: { name: string; module?: string })
|
|
768
|
+
const getScopedKeys = (item: { name: string; module?: string }) => {
|
|
514
769
|
if (item.module === undefined) return [item.name, getRootKey(item.name)];
|
|
515
770
|
if (item.module === root.name || declaredConstants.includes(item.name)) return [];
|
|
516
|
-
|
|
771
|
+
const linked = linkedModules.get(item.module) ?? nestedModules.get(item.module);
|
|
772
|
+
return [linked?.visible.get(item.name)?.key ?? item.name];
|
|
773
|
+
};
|
|
774
|
+
const getKeyArity = (key: string) => {
|
|
775
|
+
const definition = definitions.get(key);
|
|
776
|
+
return definition?.kind === "Operator"
|
|
777
|
+
? definition.params.length
|
|
778
|
+
: (linkedRoot.constants.find((constant) => constant.name === key)?.arity ?? 0);
|
|
517
779
|
};
|
|
518
780
|
const constants = new Map<string, Value>();
|
|
781
|
+
const operatorKeys = new Set<string>();
|
|
782
|
+
const setOperator = (key: string, operator: Builtin) => {
|
|
783
|
+
builtins.set(key, operator);
|
|
784
|
+
definitions.delete(key);
|
|
785
|
+
constants.delete(key);
|
|
786
|
+
operatorKeys.add(key);
|
|
787
|
+
};
|
|
519
788
|
config.constants
|
|
520
789
|
.filter((assignment) => assignment.args === undefined)
|
|
521
790
|
.forEach((assignment) => {
|
|
522
791
|
const value = toValue(assignment.value);
|
|
523
|
-
getScopedKeys(assignment).forEach((key) =>
|
|
792
|
+
getScopedKeys(assignment).forEach((key) => {
|
|
793
|
+
const arity = getKeyArity(key);
|
|
794
|
+
if (arity === 0) constants.set(key, value);
|
|
795
|
+
else
|
|
796
|
+
setOperator(key, {
|
|
797
|
+
kind: "Builtin",
|
|
798
|
+
name: key,
|
|
799
|
+
arity,
|
|
800
|
+
evaluate: () => value,
|
|
801
|
+
evaluateUnary: undefined,
|
|
802
|
+
evaluateBinary: undefined,
|
|
803
|
+
isLazy: true,
|
|
804
|
+
});
|
|
805
|
+
});
|
|
524
806
|
});
|
|
525
|
-
const
|
|
807
|
+
const tables = new Map<string, { name: string; module?: string; rows: [Value[], Value][] }>();
|
|
526
808
|
config.constants.forEach((assignment) => {
|
|
527
809
|
if (assignment.args === undefined) return;
|
|
528
|
-
const
|
|
529
|
-
|
|
810
|
+
const tableKey = `${assignment.module ?? ""}!${assignment.name}`;
|
|
811
|
+
const table = tables.get(tableKey) ?? {
|
|
812
|
+
name: assignment.name,
|
|
813
|
+
module: assignment.module,
|
|
814
|
+
rows: [],
|
|
815
|
+
};
|
|
816
|
+
if (table.rows.length > 0 && table.rows[0][0].length !== assignment.args.length) {
|
|
530
817
|
throw new TlaConfigError(
|
|
531
|
-
`The operator ${assignment.name} is
|
|
818
|
+
`The arity of the operator ${assignment.name} is inconsistent in the configuration file.`,
|
|
532
819
|
);
|
|
533
820
|
}
|
|
534
|
-
rows.push([assignment.args.map(toValue), toValue(assignment.value)]);
|
|
535
|
-
|
|
536
|
-
});
|
|
537
|
-
operatorConstants.forEach((rows, name) => {
|
|
538
|
-
builtins.set(name, createOperatorConstant(name, rows));
|
|
539
|
-
constants.delete(name);
|
|
821
|
+
table.rows.push([assignment.args.map(toValue), toValue(assignment.value)]);
|
|
822
|
+
tables.set(tableKey, table);
|
|
540
823
|
});
|
|
824
|
+
tables.forEach((table) =>
|
|
825
|
+
getScopedKeys(table).forEach((key) =>
|
|
826
|
+
setOperator(
|
|
827
|
+
key,
|
|
828
|
+
createOperatorConstant(table.name, getKeyArity(key) || table.rows[0][0].length, table.rows),
|
|
829
|
+
),
|
|
830
|
+
),
|
|
831
|
+
);
|
|
541
832
|
const substitutions = new Map(
|
|
542
833
|
config.substitutions.flatMap((substitution) =>
|
|
543
834
|
getScopedKeys(substitution).map((key): [string, string] => [
|
|
@@ -546,8 +837,79 @@ export const createEnvironment = (
|
|
|
546
837
|
]),
|
|
547
838
|
),
|
|
548
839
|
);
|
|
840
|
+
config.substitutions
|
|
841
|
+
.filter((substitution) => substitution.module === undefined)
|
|
842
|
+
.forEach((substitution) => {
|
|
843
|
+
const name = substitution.name;
|
|
844
|
+
const target = substitution.target;
|
|
845
|
+
if (!declaredConstants.includes(name) && !linkedRoot.visible.has(name)) {
|
|
846
|
+
throw new TlaConfigError(
|
|
847
|
+
`In the configuration file, the identifier ${name} does not appear in the specification.`,
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
if (config.substitutions.some((other) => other.name === target)) {
|
|
851
|
+
throw new TlaConfigError(
|
|
852
|
+
`In the configuration file, the identifier ${target} appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.`,
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
const targetKey = getRootKey(target);
|
|
856
|
+
const definition = definitions.get(targetKey);
|
|
857
|
+
const isDefined =
|
|
858
|
+
definition !== undefined ||
|
|
859
|
+
builtins.has(targetKey) ||
|
|
860
|
+
constants.has(targetKey) ||
|
|
861
|
+
declaredConstants.includes(target) ||
|
|
862
|
+
variables.includes(target);
|
|
863
|
+
if (!isDefined) {
|
|
864
|
+
throw new TlaConfigError(
|
|
865
|
+
`The configuration file substitutes for ${name} with the undefined identifier ${target}.`,
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
if (
|
|
869
|
+
definition?.kind === "Operator" &&
|
|
870
|
+
definition.params.length !==
|
|
871
|
+
(linkedRoot.constants.find((constant) => constant.name === name)?.arity ??
|
|
872
|
+
linkedRoot.visible.get(name)?.arity)
|
|
873
|
+
) {
|
|
874
|
+
throw new TlaConfigError(
|
|
875
|
+
`The configuration file substitutes for ${name} with ${target} of different number of arguments.`,
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
// TLC sets a module-scoped entry on the body of the module's definition, which every instance of
|
|
880
|
+
// the module shares.
|
|
881
|
+
const scopedKeys = new Set(
|
|
882
|
+
[...config.constants, ...config.substitutions]
|
|
883
|
+
.filter((item) => item.module !== undefined)
|
|
884
|
+
.flatMap(getScopedKeys),
|
|
885
|
+
);
|
|
886
|
+
const getOverride = (
|
|
887
|
+
key: string,
|
|
888
|
+
prefix: number,
|
|
889
|
+
): { key: string; prefix: number } | undefined => {
|
|
890
|
+
if (scopedKeys.has(key)) return { key, prefix };
|
|
891
|
+
const copy = copySources.get(key);
|
|
892
|
+
return copy && getOverride(copy.source, prefix + copy.prefix);
|
|
893
|
+
};
|
|
894
|
+
copySources.forEach((copy, key) => {
|
|
895
|
+
const override = getOverride(copy.source, copy.prefix);
|
|
896
|
+
const definition = definitions.get(key);
|
|
897
|
+
if (!override || !definition) return;
|
|
898
|
+
const params = definition.kind === "Operator" ? definition.params : [];
|
|
899
|
+
const alias = createLocalAlias(
|
|
900
|
+
key,
|
|
901
|
+
override.key,
|
|
902
|
+
params,
|
|
903
|
+
params.slice(override.prefix),
|
|
904
|
+
definition.location,
|
|
905
|
+
);
|
|
906
|
+
definitions.set(key, alias);
|
|
907
|
+
linkedRoot.visible.forEach((symbol, name) => {
|
|
908
|
+
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
909
|
+
});
|
|
910
|
+
});
|
|
549
911
|
declaredConstants.forEach((name) => {
|
|
550
|
-
if (!constants.has(name) && !substitutions.has(name) && !
|
|
912
|
+
if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
|
|
551
913
|
throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
|
|
552
914
|
}
|
|
553
915
|
});
|
|
@@ -561,23 +923,65 @@ export const createEnvironment = (
|
|
|
561
923
|
variables,
|
|
562
924
|
variableIndex: getVariableIndex(
|
|
563
925
|
variables,
|
|
564
|
-
|
|
926
|
+
linkedList.flatMap((linked) => linked.module.variables.map((variable) => variable.name)),
|
|
565
927
|
),
|
|
566
928
|
definitionValues: new Map(),
|
|
567
929
|
constantLevels: new Map(),
|
|
568
930
|
evaluators: new WeakMap(),
|
|
569
931
|
};
|
|
932
|
+
checkInstances(instanceChecks, (name) => {
|
|
933
|
+
const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
|
|
934
|
+
return {
|
|
935
|
+
constants: linked.constants,
|
|
936
|
+
variables: linked.variables,
|
|
937
|
+
definitions: [
|
|
938
|
+
...new Set(
|
|
939
|
+
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
940
|
+
),
|
|
941
|
+
],
|
|
942
|
+
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
943
|
+
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
944
|
+
};
|
|
945
|
+
});
|
|
946
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
947
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
948
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
949
|
+
const analyzer = createLevelAnalyzer({
|
|
950
|
+
constants: new Set(constants.keys()),
|
|
951
|
+
variables: new Set([
|
|
952
|
+
...variables,
|
|
953
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
954
|
+
]),
|
|
955
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
956
|
+
});
|
|
957
|
+
const isEvaluable = (body: Expression) => {
|
|
958
|
+
try {
|
|
959
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
960
|
+
return true;
|
|
961
|
+
} catch (error) {
|
|
962
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
963
|
+
throw error;
|
|
964
|
+
}
|
|
965
|
+
};
|
|
570
966
|
substitutions.forEach((target, name) => {
|
|
571
967
|
const definition = definitions.get(target);
|
|
572
968
|
if (
|
|
573
969
|
declaredConstants.includes(name) &&
|
|
574
|
-
definition &&
|
|
575
|
-
|
|
970
|
+
definition?.kind === "Operator" &&
|
|
971
|
+
definition.params.length === 0 &&
|
|
972
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
|
|
973
|
+
!isEvaluable(definition.body)
|
|
576
974
|
) {
|
|
577
975
|
throw new TlaConfigError(
|
|
578
976
|
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
579
977
|
);
|
|
580
978
|
}
|
|
581
979
|
});
|
|
582
|
-
|
|
980
|
+
runtime.isStarted = true;
|
|
981
|
+
return {
|
|
982
|
+
environment,
|
|
983
|
+
assumptions,
|
|
984
|
+
variableLocations: linkedRoot.variableLocations,
|
|
985
|
+
instanceActions,
|
|
986
|
+
};
|
|
583
987
|
};
|