tla-ts 0.0.3-dev.949cce6 → 0.0.3-dev.9b15ea6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +146 -8
- package/dist/index.d.ts +146 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +1351 -20
- package/dist/run-cli.js +1351 -20
- package/dist/transfer.cjs +86 -28
- package/dist/transfer.js +86 -28
- package/package.json +5 -1
- package/src/ast.ts +48 -6
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1352 -215
- package/src/cli.ts +8 -1
- package/src/codegen.ts +68 -18
- package/src/config.ts +252 -179
- package/src/environment.ts +451 -129
- package/src/errors.ts +217 -3
- package/src/evaluate.ts +724 -117
- package/src/exit-codes.ts +5 -4
- package/src/index.ts +1 -0
- package/src/instances.ts +4 -2
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +183 -37
- package/src/lexer.ts +14 -1
- package/src/live-check.ts +211 -89
- package/src/liveness.ts +192 -65
- package/src/obligation-expression.ts +2 -2
- package/src/obligation-source.ts +2 -0
- package/src/parallel-worker.ts +8 -3
- package/src/parallel.ts +56 -5
- package/src/parser.ts +423 -126
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1363 -405
- package/src/run-cli.ts +1167 -155
- package/src/runtime.ts +16 -0
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3112 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +354 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +26 -7
- package/src/standard-modules.ts +203 -146
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +29 -6
- package/src/subexpressions.ts +221 -161
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +162 -114
- package/src/symmetry.ts +180 -52
- package/src/tableau.ts +4 -2
- package/src/tlc-ext.ts +13 -17
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +46 -18
- package/src/tlc-order.ts +56 -6
- package/src/tlc-random.ts +28 -3
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-version.ts +18 -0
- package/src/transfer.ts +15 -3
- package/src/value-sources.ts +29 -0
- package/src/values.ts +684 -204
package/src/environment.ts
CHANGED
|
@@ -12,7 +12,16 @@ import {
|
|
|
12
12
|
import { BUNDLED_MODULES } from "./bundled-modules";
|
|
13
13
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
14
14
|
import type { FileSystem } from "./json";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
createSanyError,
|
|
17
|
+
createMissingModuleError,
|
|
18
|
+
formatRange,
|
|
19
|
+
formatSanyWarnings,
|
|
20
|
+
TlaConfigError,
|
|
21
|
+
TlaEvaluationError,
|
|
22
|
+
TlaSyntaxError,
|
|
23
|
+
type SanyError,
|
|
24
|
+
} from "./errors";
|
|
16
25
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
17
26
|
import {
|
|
18
27
|
createBigNumberError,
|
|
@@ -29,24 +38,39 @@ import {
|
|
|
29
38
|
renameLocals,
|
|
30
39
|
withLetInstances,
|
|
31
40
|
} from "./instances";
|
|
32
|
-
import { overrideDefinitions } from "./java-overrides";
|
|
41
|
+
import { overrideDefinitions, type JavaOverride } from "./java-overrides";
|
|
33
42
|
import { extractModule, parseModule } from "./parser";
|
|
34
43
|
import {
|
|
35
|
-
checkRedefinitions,
|
|
36
44
|
getArity,
|
|
45
|
+
getParamArities,
|
|
37
46
|
isBefore,
|
|
47
|
+
isTheoremDefinition,
|
|
38
48
|
KEY_SEPARATOR,
|
|
39
49
|
LOCAL_RESOLUTION,
|
|
50
|
+
MODULE_START,
|
|
40
51
|
resolveModule,
|
|
41
52
|
resolveSubstitutions,
|
|
53
|
+
type ModuleReport,
|
|
42
54
|
type ModuleSymbol,
|
|
55
|
+
type ModuleUnit,
|
|
56
|
+
type SymbolKind,
|
|
43
57
|
type Symbols,
|
|
44
58
|
} from "./resolve";
|
|
45
59
|
import { createRuntime } from "./runtime";
|
|
46
|
-
import {
|
|
60
|
+
import { getSanyParseError } from "./sany-parse-error";
|
|
61
|
+
import {
|
|
62
|
+
CORE_OPERATORS,
|
|
63
|
+
getStandardModule,
|
|
64
|
+
getTraceSpecArithmetic,
|
|
65
|
+
STANDARD_DEPENDENCIES,
|
|
66
|
+
} from "./standard-modules";
|
|
67
|
+
import { STANDARD_LEVELS } from "./standard-levels";
|
|
68
|
+
import { getStandardRange } from "./standard-ranges";
|
|
47
69
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
48
70
|
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
49
|
-
import {
|
|
71
|
+
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
72
|
+
import { isEvalException } from "./tlc-messages";
|
|
73
|
+
import { createTlcDefinitionOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
50
74
|
import {
|
|
51
75
|
checkInteger,
|
|
52
76
|
createModelValue,
|
|
@@ -54,7 +78,9 @@ import {
|
|
|
54
78
|
formatValue,
|
|
55
79
|
valuesEqual,
|
|
56
80
|
type Value,
|
|
81
|
+
markNormalized,
|
|
57
82
|
} from "./values";
|
|
83
|
+
import type { TlcVersion } from "./tlc-version";
|
|
58
84
|
|
|
59
85
|
interface EnvironmentOptions {
|
|
60
86
|
source: string;
|
|
@@ -63,6 +89,15 @@ interface EnvironmentOptions {
|
|
|
63
89
|
print: (text: string) => void;
|
|
64
90
|
files: FileSystem;
|
|
65
91
|
seed?: bigint;
|
|
92
|
+
// Links the modules without a model, as SANY does, so a declared constant needs no value.
|
|
93
|
+
isUnconfigured?: boolean;
|
|
94
|
+
isTool?: boolean;
|
|
95
|
+
// TLC loads Integers for the binary trace of its trace exploration spec, and Integers' Java
|
|
96
|
+
// methods then compute the arithmetic of Naturals.
|
|
97
|
+
isTraceRecorded?: boolean;
|
|
98
|
+
isSanyReported?: boolean;
|
|
99
|
+
isWarningEnabled?: boolean;
|
|
100
|
+
version?: TlcVersion;
|
|
66
101
|
}
|
|
67
102
|
|
|
68
103
|
interface Specification {
|
|
@@ -70,6 +105,12 @@ interface Specification {
|
|
|
70
105
|
assumptions: Assumption[];
|
|
71
106
|
variableLocations: SourceLocation[];
|
|
72
107
|
instanceActions: Map<string, InstanceAction>;
|
|
108
|
+
// The definitions TLC computes when it starts, in the order it computes them.
|
|
109
|
+
startupDefinitions: Definition[];
|
|
110
|
+
rootConstants: Declaration[];
|
|
111
|
+
rootSymbols: Symbols;
|
|
112
|
+
// The methods of the modules' classes, in the order TLC loads the classes.
|
|
113
|
+
javaOverrides: JavaOverride[];
|
|
73
114
|
}
|
|
74
115
|
|
|
75
116
|
export interface InstanceAction {
|
|
@@ -105,25 +146,73 @@ interface LinkedModule {
|
|
|
105
146
|
exported: Symbols;
|
|
106
147
|
modules: Map<string, LinkedModule>;
|
|
107
148
|
extended: LinkedModule[];
|
|
149
|
+
facts: Set<string>;
|
|
108
150
|
}
|
|
109
151
|
|
|
152
|
+
// errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
|
|
153
|
+
// the modules in its file, which SANY runs only when it reports no error.
|
|
110
154
|
interface ModuleScope {
|
|
111
155
|
id: string;
|
|
112
156
|
symbols: Symbols;
|
|
113
157
|
modules: Map<string, LinkedModule>;
|
|
114
158
|
nested: Module[];
|
|
159
|
+
errors: SanyError[];
|
|
160
|
+
warnings: SanyError[];
|
|
161
|
+
levelChecks: (() => void)[];
|
|
115
162
|
}
|
|
116
163
|
|
|
117
|
-
const getDeclarations = (linked: LinkedModule) => [
|
|
164
|
+
const getDeclarations = (linked: LinkedModule): Declaration[] => [
|
|
118
165
|
...linked.constants,
|
|
119
|
-
...linked.variables.map((variable) => ({
|
|
166
|
+
...linked.variables.map((variable, index) => ({
|
|
167
|
+
name: variable,
|
|
168
|
+
arity: 0,
|
|
169
|
+
location: linked.variableLocations[index],
|
|
170
|
+
})),
|
|
120
171
|
];
|
|
121
172
|
|
|
173
|
+
// Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
|
|
174
|
+
// does in TLC: SANY's message, then a fatal error that names the module.
|
|
175
|
+
const parseModuleFile = (source: string, name: string) => {
|
|
176
|
+
try {
|
|
177
|
+
return parseModule(source);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (!(error instanceof TlaSyntaxError)) throw error;
|
|
180
|
+
const parseError = getSanyParseError(source);
|
|
181
|
+
if (parseError === undefined) throw error;
|
|
182
|
+
throw createSanyError(
|
|
183
|
+
[
|
|
184
|
+
{
|
|
185
|
+
location: `In module ${name}`,
|
|
186
|
+
message: `Could not parse module ${name} from file ${name}.tla`,
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
true,
|
|
190
|
+
parseError,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
122
195
|
const createModuleRedefinedError = (module: Module) =>
|
|
123
196
|
new TlaSyntaxError(
|
|
124
197
|
`Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
|
|
125
198
|
);
|
|
126
199
|
|
|
200
|
+
const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
|
|
201
|
+
module.location.range &&
|
|
202
|
+
existing.location.range && {
|
|
203
|
+
location: formatRange(module.location.range),
|
|
204
|
+
message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
|
|
208
|
+
// declaration.
|
|
209
|
+
const getNodeWord = (kind: SymbolKind) =>
|
|
210
|
+
["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
|
|
211
|
+
|
|
212
|
+
// The Java class of the node SANY binds a symbol to.
|
|
213
|
+
const getNodeClass = (kind: SymbolKind) =>
|
|
214
|
+
kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
|
|
215
|
+
|
|
127
216
|
const createLocalFunction = (definition: Definition, key: string): Definition => ({
|
|
128
217
|
kind: "Operator",
|
|
129
218
|
name: key,
|
|
@@ -215,13 +304,19 @@ export const createEnvironment = (
|
|
|
215
304
|
const parameterized = new Map<string, boolean>();
|
|
216
305
|
// The definition each instance copy came from, and the instance parameters the copy adds.
|
|
217
306
|
const copySources = new Map<string, { source: string; prefix: number }>();
|
|
307
|
+
const substitutedDefinitions = new Set<Definition>();
|
|
218
308
|
const builtins = new Map<string, Builtin>(
|
|
219
309
|
CORE_OPERATORS.map((operator) => [operator.name, operator]),
|
|
220
310
|
);
|
|
221
311
|
const standardExports = new Map<string, Symbols>();
|
|
222
312
|
const runtime = createRuntime(config.checkDeadlock, options.seed);
|
|
313
|
+
runtime.isWarningEnabled = options.isWarningEnabled ?? true;
|
|
314
|
+
runtime.isTool = options.isTool ?? false;
|
|
315
|
+
runtime.version = options.version ?? "master";
|
|
223
316
|
const linkedModules = new Map<string, LinkedModule>();
|
|
224
317
|
const nestedModules = new Map<string, LinkedModule>();
|
|
318
|
+
// Each module by name, from the time its level checks run.
|
|
319
|
+
const checkedModules = new Map<string, LinkedModule>();
|
|
225
320
|
const linkedList: LinkedModule[] = [];
|
|
226
321
|
const dependencies = new Map<string, Set<string>>();
|
|
227
322
|
const loading: Set<string>[] = [];
|
|
@@ -236,13 +331,35 @@ export const createEnvironment = (
|
|
|
236
331
|
const copyCount = { value: 0 };
|
|
237
332
|
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
238
333
|
const instanceChecks: InstanceCheck[] = [];
|
|
334
|
+
// The warnings SANY reports for the spec.
|
|
335
|
+
const warnings: SanyError[] = [];
|
|
336
|
+
const checkInstance = createInstanceChecker((name) => {
|
|
337
|
+
const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
|
|
338
|
+
return {
|
|
339
|
+
constants: linked.constants,
|
|
340
|
+
variables: linked.variables,
|
|
341
|
+
definitions: [
|
|
342
|
+
...new Set(
|
|
343
|
+
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
344
|
+
),
|
|
345
|
+
],
|
|
346
|
+
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
347
|
+
theorems: linked.module.theorems,
|
|
348
|
+
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
349
|
+
};
|
|
350
|
+
});
|
|
239
351
|
const instanceActions = new Map<string, InstanceAction>();
|
|
352
|
+
const instanceNames = new Map<string, string>();
|
|
353
|
+
const qualifiedNames = new Map<string, string>();
|
|
240
354
|
|
|
241
355
|
const addStandardModule = (name: string): Symbols | undefined => {
|
|
242
356
|
const cached = standardExports.get(name);
|
|
243
357
|
if (cached) return cached;
|
|
244
|
-
const standardModule = getStandardModule(name, options.print, runtime, options.files);
|
|
358
|
+
const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
|
|
245
359
|
if (!standardModule) return undefined;
|
|
360
|
+
// TLC loads the modules a standard module instantiates too, and the Java methods of each
|
|
361
|
+
// replace the operators of every module, as those of Integers replace those of Naturals.
|
|
362
|
+
STANDARD_DEPENDENCIES.get(name)?.forEach(addStandardModule);
|
|
246
363
|
const exported: Symbols = new Map();
|
|
247
364
|
standardModule.extends.forEach((extended) =>
|
|
248
365
|
addStandardModule(extended)?.forEach((symbol, symbolName) =>
|
|
@@ -251,7 +368,13 @@ export const createEnvironment = (
|
|
|
251
368
|
);
|
|
252
369
|
standardModule.operators.forEach((operator) => {
|
|
253
370
|
builtins.set(operator.name, operator);
|
|
254
|
-
exported.set(operator.name, {
|
|
371
|
+
exported.set(operator.name, {
|
|
372
|
+
key: operator.name,
|
|
373
|
+
arity: operator.arity,
|
|
374
|
+
params: STANDARD_LEVELS.get(operator.name)?.formals.map((formal) => formal.arity),
|
|
375
|
+
kind: "Definition",
|
|
376
|
+
range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
|
|
377
|
+
});
|
|
255
378
|
});
|
|
256
379
|
standardExports.set(name, exported);
|
|
257
380
|
return exported;
|
|
@@ -259,7 +382,11 @@ export const createEnvironment = (
|
|
|
259
382
|
|
|
260
383
|
const getStandardSymbols = (name: string) => {
|
|
261
384
|
const symbols = addStandardModule(name);
|
|
262
|
-
|
|
385
|
+
const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
|
|
386
|
+
if (symbols) useModule(name, dependencies);
|
|
387
|
+
// TLC loads the Java class of each module a standard module uses, so one that uses Integers
|
|
388
|
+
// overrides the arithmetic of Naturals.
|
|
389
|
+
if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
|
|
263
390
|
return symbols;
|
|
264
391
|
};
|
|
265
392
|
|
|
@@ -287,14 +414,17 @@ export const createEnvironment = (
|
|
|
287
414
|
options.loadModule?.(name) ??
|
|
288
415
|
BUNDLED_MODULES.get(name) ??
|
|
289
416
|
extractModule(options.source, name);
|
|
290
|
-
if (source === undefined)
|
|
291
|
-
|
|
292
|
-
`Cannot find source file for module ${name} imported in module ${importer}.`,
|
|
293
|
-
);
|
|
294
|
-
const module = parseModule(source);
|
|
417
|
+
if (source === undefined) throw createMissingModuleError(name, importer);
|
|
418
|
+
const module = parseModuleFile(source, name);
|
|
295
419
|
if (module.name !== name) {
|
|
296
|
-
throw
|
|
297
|
-
|
|
420
|
+
throw createSanyError(
|
|
421
|
+
[
|
|
422
|
+
{
|
|
423
|
+
location: "Unknown location",
|
|
424
|
+
message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
|
|
425
|
+
},
|
|
426
|
+
],
|
|
427
|
+
true,
|
|
298
428
|
);
|
|
299
429
|
}
|
|
300
430
|
return rejectModuleLiterals(module);
|
|
@@ -310,6 +440,8 @@ export const createEnvironment = (
|
|
|
310
440
|
}
|
|
311
441
|
const closure = new Set<string>();
|
|
312
442
|
const nested: Module[] = [];
|
|
443
|
+
const errors: SanyError[] = [];
|
|
444
|
+
const levelChecks: (() => void)[] = [];
|
|
313
445
|
linking.push(parsed.name);
|
|
314
446
|
loading.push(closure);
|
|
315
447
|
const linked = linkModule(parsed, {
|
|
@@ -317,11 +449,19 @@ export const createEnvironment = (
|
|
|
317
449
|
symbols: new Map(),
|
|
318
450
|
modules: new Map(),
|
|
319
451
|
nested,
|
|
452
|
+
errors,
|
|
453
|
+
warnings,
|
|
454
|
+
levelChecks,
|
|
320
455
|
});
|
|
321
456
|
loading.pop();
|
|
322
457
|
linking.pop();
|
|
458
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
323
459
|
const duplicate = nested.find((module) => closure.has(module.name));
|
|
460
|
+
const loaded = duplicate && linkedModules.get(duplicate.name);
|
|
461
|
+
const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
|
|
462
|
+
if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
|
|
324
463
|
if (duplicate) throw createModuleRedefinedError(duplicate);
|
|
464
|
+
levelChecks.forEach((check) => check());
|
|
325
465
|
return { linked, closure };
|
|
326
466
|
};
|
|
327
467
|
|
|
@@ -341,23 +481,16 @@ export const createEnvironment = (
|
|
|
341
481
|
const instantiate = (
|
|
342
482
|
instance: Instance,
|
|
343
483
|
module: Module,
|
|
344
|
-
unit:
|
|
345
|
-
imported: Symbols;
|
|
346
|
-
importedAt: Map<string, SourceLocation>;
|
|
347
|
-
definitions: Map<string, Definition>;
|
|
348
|
-
getKey: (definition: Definition) => string;
|
|
349
|
-
},
|
|
484
|
+
unit: ModuleUnit,
|
|
350
485
|
isParameterFree: boolean,
|
|
351
486
|
nested: LinkedModule | undefined,
|
|
352
487
|
): Symbols => {
|
|
353
488
|
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
354
|
-
if (standardSymbols)
|
|
355
|
-
if (instance.name === undefined) return standardSymbols;
|
|
356
|
-
return new Map(
|
|
357
|
-
[...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
|
|
358
|
-
);
|
|
359
|
-
}
|
|
489
|
+
if (standardSymbols) return getStandardInstanceSymbols(instance, standardSymbols);
|
|
360
490
|
const linked = nested ?? getLinkedModule(instance.module, module.name);
|
|
491
|
+
linked.facts.forEach((name) =>
|
|
492
|
+
unit.facts.add(instance.name === undefined ? name : `${instance.name}!${name}`),
|
|
493
|
+
);
|
|
361
494
|
const declarations = getDeclarations(linked);
|
|
362
495
|
const suffix = `#${copyCount.value++}`;
|
|
363
496
|
const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
|
|
@@ -384,34 +517,81 @@ export const createEnvironment = (
|
|
|
384
517
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
385
518
|
addDefinition: (source, name, create) => {
|
|
386
519
|
const key = createName(`${name}@${module.name}`);
|
|
387
|
-
|
|
520
|
+
const definition = create(key);
|
|
521
|
+
definitions.set(key, definition);
|
|
522
|
+
if (declarations.length > 0) substitutedDefinitions.add(definition);
|
|
523
|
+
if (instance.name !== undefined) {
|
|
524
|
+
instanceNames.set(key, `${instance.name}!${instanceNames.get(source) ?? name}`);
|
|
525
|
+
}
|
|
388
526
|
parameterized.set(key, !isParameterFree);
|
|
389
527
|
copySources.set(key, { source, prefix: instance.params.length });
|
|
390
528
|
return key;
|
|
391
529
|
},
|
|
392
530
|
createName,
|
|
393
531
|
});
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
symbols.set(name, { key, arity: symbol.arity });
|
|
403
|
-
return;
|
|
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
|
-
}
|
|
409
|
-
symbols.set(`${instance.name}!${name}`, {
|
|
410
|
-
key,
|
|
411
|
-
arity: symbol.arity + instance.params.length,
|
|
412
|
-
});
|
|
532
|
+
return getInstanceSymbols(instance, linked.exported, declarations, (key, name) => {
|
|
533
|
+
const copy =
|
|
534
|
+
instance.name === undefined
|
|
535
|
+
? instantiation.getKey(key)
|
|
536
|
+
: instantiation.getQualifiedKey(key);
|
|
537
|
+
if (declarations.length > 0) instanceActions.set(copy, { name, location: instance.location });
|
|
538
|
+
if (instance.name !== undefined) qualifiedNames.set(copy, name);
|
|
539
|
+
return copy;
|
|
413
540
|
});
|
|
414
|
-
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
const getStandardInstanceSymbols = (instance: Instance, standardSymbols: Symbols): Symbols =>
|
|
544
|
+
instance.name === undefined
|
|
545
|
+
? standardSymbols
|
|
546
|
+
: new Map(
|
|
547
|
+
[...standardSymbols].map(([name, symbol]) => [
|
|
548
|
+
`${instance.name}!${name}`,
|
|
549
|
+
{ key: symbol.key, arity: symbol.arity, params: symbol.params, kind: symbol.kind },
|
|
550
|
+
]),
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
// The names an INSTANCE adds for the definitions of the module it instantiates, each with the key
|
|
554
|
+
// that getKey gives it. SANY binds a copy of a definition that depends on the parameters at the
|
|
555
|
+
// INSTANCE.
|
|
556
|
+
const getInstanceSymbols = (
|
|
557
|
+
instance: Instance,
|
|
558
|
+
exported: Symbols,
|
|
559
|
+
declarations: Declaration[],
|
|
560
|
+
getKey: (key: string, name: string) => string,
|
|
561
|
+
): Symbols => {
|
|
562
|
+
const declared = new Set(declarations.map((declaration) => declaration.name));
|
|
563
|
+
return new Map(
|
|
564
|
+
[...exported]
|
|
565
|
+
.filter(([name]) => !declared.has(name))
|
|
566
|
+
.map(([name, symbol]): [string, ModuleSymbol] => {
|
|
567
|
+
if (instance.name === undefined) {
|
|
568
|
+
return [
|
|
569
|
+
name,
|
|
570
|
+
{
|
|
571
|
+
key: getKey(symbol.key, name),
|
|
572
|
+
arity: symbol.arity,
|
|
573
|
+
params: symbol.params,
|
|
574
|
+
kind: symbol.kind,
|
|
575
|
+
range:
|
|
576
|
+
parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
|
|
577
|
+
},
|
|
578
|
+
];
|
|
579
|
+
}
|
|
580
|
+
const qualified = `${instance.name}!${name}`;
|
|
581
|
+
return [
|
|
582
|
+
qualified,
|
|
583
|
+
{
|
|
584
|
+
key: getKey(symbol.key, qualified),
|
|
585
|
+
arity: symbol.arity + instance.params.length,
|
|
586
|
+
params: symbol.params && [
|
|
587
|
+
...instance.params.map((param) => param.arity),
|
|
588
|
+
...symbol.params,
|
|
589
|
+
],
|
|
590
|
+
kind: symbol.kind,
|
|
591
|
+
},
|
|
592
|
+
];
|
|
593
|
+
}),
|
|
594
|
+
);
|
|
415
595
|
};
|
|
416
596
|
|
|
417
597
|
const getLocalArguments = (params: OperatorParam[], location: SourceLocation) =>
|
|
@@ -584,31 +764,70 @@ export const createEnvironment = (
|
|
|
584
764
|
return definition ? [{ ...definition, name }] : [];
|
|
585
765
|
});
|
|
586
766
|
|
|
767
|
+
const javaOverrides = new Map<string, JavaOverride[]>();
|
|
587
768
|
const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
|
|
588
|
-
const
|
|
769
|
+
const overridden = overrideDefinitions(
|
|
770
|
+
unlinked,
|
|
771
|
+
options.loadClass?.(unlinked.name),
|
|
772
|
+
unlinked === root,
|
|
773
|
+
);
|
|
774
|
+
javaOverrides.set(unlinked.name, overridden.overrides);
|
|
775
|
+
const parsed = overridden.module;
|
|
776
|
+
const firstCheck = instanceChecks.length;
|
|
589
777
|
const visible: Symbols = new Map([
|
|
590
778
|
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
591
779
|
operator.name,
|
|
592
|
-
{ key: operator.name, arity: operator.arity },
|
|
780
|
+
{ key: operator.name, arity: operator.arity, kind: "Builtin" },
|
|
593
781
|
]),
|
|
594
782
|
...scope.symbols,
|
|
595
783
|
]);
|
|
784
|
+
// Where an INSTANCE first made each name it adds visible.
|
|
785
|
+
const importedAt = new Map<string, SourceLocation>();
|
|
786
|
+
const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
|
|
787
|
+
const reports = [extendsReport];
|
|
788
|
+
// Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
|
|
789
|
+
// and warns of two definitions or two declarations but rejects a definition and a declaration.
|
|
790
|
+
const mergeExtended = (symbols: Symbols) =>
|
|
791
|
+
symbols.forEach((symbol, name) => {
|
|
792
|
+
const current = exported.get(name);
|
|
793
|
+
if (!current) {
|
|
794
|
+
visible.set(name, symbol);
|
|
795
|
+
exported.set(name, symbol);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
if (
|
|
799
|
+
!current.range ||
|
|
800
|
+
!symbol.range ||
|
|
801
|
+
formatRange(current.range) === formatRange(symbol.range)
|
|
802
|
+
) {
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const location = formatRange(symbol.range);
|
|
806
|
+
const existing = formatRange(current.range);
|
|
807
|
+
if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
|
|
808
|
+
extendsReport.warnings.push({
|
|
809
|
+
location,
|
|
810
|
+
message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
811
|
+
});
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
extendsReport.errors.push({
|
|
815
|
+
location,
|
|
816
|
+
message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
817
|
+
});
|
|
818
|
+
});
|
|
596
819
|
const exported: Symbols = new Map();
|
|
820
|
+
const facts = new Set<string>();
|
|
597
821
|
const modules = new Map<string, LinkedModule>();
|
|
598
822
|
const extended: LinkedModule[] = [];
|
|
599
823
|
const constants = [...parsed.constants];
|
|
600
824
|
const variables = parsed.variables.map((variable) => variable.name);
|
|
601
825
|
const variableLocations = parsed.variables.map((variable) => variable.location);
|
|
602
|
-
const addSymbols = (symbols: Symbols, isExported: boolean) =>
|
|
603
|
-
symbols.forEach((symbol, name) => {
|
|
604
|
-
visible.set(name, symbol);
|
|
605
|
-
if (isExported) exported.set(name, symbol);
|
|
606
|
-
});
|
|
607
826
|
parsed.extends.forEach((name) => {
|
|
608
827
|
const nested = scope.modules.get(name);
|
|
609
828
|
const standardSymbols = nested ? undefined : getStandardSymbols(name);
|
|
610
829
|
if (standardSymbols) {
|
|
611
|
-
|
|
830
|
+
mergeExtended(standardSymbols);
|
|
612
831
|
return;
|
|
613
832
|
}
|
|
614
833
|
const linked = nested ?? getLinkedModule(name, parsed.name);
|
|
@@ -622,61 +841,114 @@ export const createEnvironment = (
|
|
|
622
841
|
variables.push(variable);
|
|
623
842
|
variableLocations.push(linked.variableLocations[index]);
|
|
624
843
|
});
|
|
625
|
-
|
|
844
|
+
mergeExtended(linked.exported);
|
|
845
|
+
linked.facts.forEach((name) => facts.add(name));
|
|
626
846
|
linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
|
|
627
847
|
extended.push(linked);
|
|
628
848
|
});
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
849
|
+
const bound = new Map(visible);
|
|
850
|
+
const declarations = [
|
|
851
|
+
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
852
|
+
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
853
|
+
];
|
|
854
|
+
const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
|
|
855
|
+
key: declaration.name,
|
|
856
|
+
arity: declaration.arity,
|
|
857
|
+
kind: declaration.kind,
|
|
858
|
+
range: declaration.location.range,
|
|
859
|
+
});
|
|
860
|
+
declarations.forEach((declaration) =>
|
|
861
|
+
exported.set(declaration.name, getDeclarationSymbol(declaration)),
|
|
638
862
|
);
|
|
863
|
+
const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
|
|
864
|
+
key: getKey(definition),
|
|
865
|
+
arity: getArity(definition),
|
|
866
|
+
params: getParamArities(definition),
|
|
867
|
+
kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
|
|
868
|
+
range: definition.location.range,
|
|
869
|
+
});
|
|
639
870
|
const isParameterFree = constants.length === 0 && variables.length === 0;
|
|
640
871
|
const getKey = (definition: Definition) =>
|
|
641
872
|
parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
|
|
642
|
-
const
|
|
643
|
-
const
|
|
873
|
+
const instanced = new Map<Instance, Symbols>();
|
|
874
|
+
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
875
|
+
const isInstancedAfterDefinition = (nested: Module) => {
|
|
876
|
+
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
877
|
+
return first !== undefined && isBefore(nested.location, first.location);
|
|
878
|
+
};
|
|
879
|
+
const getLetModule = (instance: Instance) => {
|
|
880
|
+
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
881
|
+
if (!own) return getVisibleModules().get(instance.module);
|
|
882
|
+
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
883
|
+
};
|
|
884
|
+
const getUnit = (module: Module): ModuleUnit => ({
|
|
644
885
|
imported: visible,
|
|
645
|
-
|
|
646
|
-
|
|
886
|
+
getInstance: (instance) => {
|
|
887
|
+
const nested = getLetModule(instance);
|
|
888
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
889
|
+
if (standardSymbols) {
|
|
890
|
+
return {
|
|
891
|
+
declarations: [],
|
|
892
|
+
symbols: getStandardInstanceSymbols(instance, standardSymbols),
|
|
893
|
+
instancer: parsed.name,
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
const linked = nested ?? getLinkedModule(instance.module, parsed.name);
|
|
897
|
+
const declarations = getDeclarations(linked);
|
|
898
|
+
return {
|
|
899
|
+
declarations,
|
|
900
|
+
symbols: getInstanceSymbols(instance, linked.exported, declarations, (key) => key),
|
|
901
|
+
instancer: parsed.name,
|
|
902
|
+
};
|
|
903
|
+
},
|
|
904
|
+
bound,
|
|
905
|
+
// SANY keeps the first of two definitions of a name.
|
|
906
|
+
definitions: new Map(
|
|
907
|
+
module.definitions.toReversed().map((definition) => [definition.name, definition]),
|
|
908
|
+
),
|
|
647
909
|
getKey,
|
|
910
|
+
instanced,
|
|
911
|
+
facts,
|
|
912
|
+
reports,
|
|
913
|
+
warnings: scope.warnings,
|
|
648
914
|
});
|
|
649
|
-
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
650
915
|
const getSymbolsBefore = (location: SourceLocation): Symbols =>
|
|
651
916
|
new Map([
|
|
652
917
|
...visible,
|
|
653
|
-
...
|
|
654
|
-
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
655
|
-
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
656
|
-
]
|
|
918
|
+
...declarations
|
|
657
919
|
.filter((declaration) => isBefore(declaration.location, location))
|
|
658
920
|
.map((declaration): [string, ModuleSymbol] => [
|
|
659
921
|
declaration.name,
|
|
660
|
-
|
|
922
|
+
getDeclarationSymbol(declaration),
|
|
661
923
|
]),
|
|
662
924
|
...parsed.definitions
|
|
663
925
|
.filter((definition) => isBefore(definition.location, location))
|
|
664
926
|
.map((definition): [string, ModuleSymbol] => [
|
|
665
927
|
definition.name,
|
|
666
|
-
|
|
928
|
+
getDefinitionSymbol(definition),
|
|
667
929
|
]),
|
|
668
930
|
]);
|
|
931
|
+
// Generator.generateModule reports the errors of a nested module and goes on, and then
|
|
932
|
+
// SymbolTable.addModule reports a module already defined.
|
|
669
933
|
const linkNested = (nested: Module) => {
|
|
670
934
|
const visibleModules = getVisibleModules();
|
|
671
|
-
|
|
935
|
+
const existing = visibleModules.get(nested.name);
|
|
936
|
+
const redefinition = existing && getModuleRedefinition(nested, existing.module);
|
|
937
|
+
if (existing && !redefinition) throw createModuleRedefinedError(nested);
|
|
672
938
|
scope.nested.push(nested);
|
|
939
|
+
const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
|
|
940
|
+
reports.push(report);
|
|
673
941
|
const linked = linkModule(nested, {
|
|
674
942
|
id: `${scope.id}/${nested.name}`,
|
|
675
943
|
symbols: getSymbolsBefore(nested.location),
|
|
676
944
|
modules: visibleModules,
|
|
677
945
|
nested: scope.nested,
|
|
946
|
+
errors: report.errors,
|
|
947
|
+
warnings: report.warnings,
|
|
948
|
+
levelChecks: scope.levelChecks,
|
|
678
949
|
});
|
|
679
|
-
|
|
950
|
+
if (redefinition) report.errors.push(redefinition);
|
|
951
|
+
if (!existing) modules.set(nested.name, linked);
|
|
680
952
|
if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
|
|
681
953
|
};
|
|
682
954
|
[
|
|
@@ -690,10 +962,13 @@ export const createEnvironment = (
|
|
|
690
962
|
isParameterFree,
|
|
691
963
|
getVisibleModules().get(instance.module),
|
|
692
964
|
);
|
|
693
|
-
|
|
694
|
-
|
|
965
|
+
instanced.set(instance, symbols);
|
|
966
|
+
symbols.forEach((symbol, name) => {
|
|
967
|
+
if (visible.has(name)) return;
|
|
968
|
+
importedAt.set(name, instance.location);
|
|
969
|
+
visible.set(name, symbol);
|
|
970
|
+
if (!instance.isLocal) exported.set(name, symbol);
|
|
695
971
|
});
|
|
696
|
-
addSymbols(symbols, !instance.isLocal);
|
|
697
972
|
},
|
|
698
973
|
})),
|
|
699
974
|
...parsed.modules.map((nested) => ({
|
|
@@ -703,36 +978,30 @@ export const createEnvironment = (
|
|
|
703
978
|
]
|
|
704
979
|
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
705
980
|
.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
|
-
};
|
|
716
981
|
const module = resolveModuleSubexpressions(
|
|
717
982
|
withLetInstances(parsed, (instance, lets) =>
|
|
718
983
|
instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
|
|
719
984
|
),
|
|
720
985
|
getAvailableDefinitions(visible),
|
|
721
986
|
);
|
|
722
|
-
resolveModule(module, getUnit(module));
|
|
987
|
+
scope.errors.push(...resolveModule(module, getUnit(module)));
|
|
723
988
|
module.definitions.forEach((definition) => {
|
|
724
989
|
const key = getKey(definition);
|
|
725
990
|
definitions.set(key, definition);
|
|
726
991
|
parameterized.set(key, !isParameterFree);
|
|
727
|
-
const symbol =
|
|
992
|
+
const symbol = getDefinitionSymbol(definition);
|
|
993
|
+
const importedLocation = importedAt.get(definition.name);
|
|
994
|
+
// SANY binds the name of a fact to a fact the module imports before it.
|
|
995
|
+
if (
|
|
996
|
+
symbol.kind === "Theorem" &&
|
|
997
|
+
visible.get(definition.name)?.kind === "Theorem" &&
|
|
998
|
+
(importedLocation === undefined || isBefore(importedLocation, definition.location))
|
|
999
|
+
) {
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
728
1002
|
visible.set(definition.name, symbol);
|
|
729
1003
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
730
1004
|
});
|
|
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
1005
|
const linked = {
|
|
737
1006
|
module,
|
|
738
1007
|
constants,
|
|
@@ -742,12 +1011,36 @@ export const createEnvironment = (
|
|
|
742
1011
|
exported,
|
|
743
1012
|
modules,
|
|
744
1013
|
extended,
|
|
1014
|
+
facts: new Set([
|
|
1015
|
+
...facts,
|
|
1016
|
+
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
1017
|
+
]),
|
|
745
1018
|
};
|
|
1019
|
+
if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
|
|
1020
|
+
// SANY checks the instances of a module with its definitions and theorems, so it reports their
|
|
1021
|
+
// errors together.
|
|
1022
|
+
const checks = instanceChecks
|
|
1023
|
+
.splice(firstCheck)
|
|
1024
|
+
.map((check) => ({ location: check.location, check: () => checkInstance(check) }));
|
|
1025
|
+
scope.levelChecks.push(() =>
|
|
1026
|
+
checkModuleLevels(
|
|
1027
|
+
module,
|
|
1028
|
+
{
|
|
1029
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
1030
|
+
variables: new Set(variables),
|
|
1031
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
1032
|
+
getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
|
|
1033
|
+
},
|
|
1034
|
+
checks,
|
|
1035
|
+
warnings,
|
|
1036
|
+
),
|
|
1037
|
+
);
|
|
746
1038
|
linkedList.push(linked);
|
|
747
1039
|
return linked;
|
|
748
1040
|
};
|
|
749
1041
|
|
|
750
1042
|
const linkedRoot = linkTopLevel(root).linked;
|
|
1043
|
+
if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
|
|
751
1044
|
const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
|
|
752
1045
|
linkedRoot.visible.forEach((symbol, name) => {
|
|
753
1046
|
const definition = definitions.get(symbol.key);
|
|
@@ -789,6 +1082,7 @@ export const createEnvironment = (
|
|
|
789
1082
|
.filter((assignment) => assignment.args === undefined)
|
|
790
1083
|
.forEach((assignment) => {
|
|
791
1084
|
const value = toValue(assignment.value);
|
|
1085
|
+
markNormalized(value);
|
|
792
1086
|
getScopedKeys(assignment).forEach((key) => {
|
|
793
1087
|
const arity = getKeyArity(key);
|
|
794
1088
|
if (arity === 0) constants.set(key, value);
|
|
@@ -908,11 +1202,26 @@ export const createEnvironment = (
|
|
|
908
1202
|
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
909
1203
|
});
|
|
910
1204
|
});
|
|
911
|
-
declaredConstants.
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1205
|
+
const unassigned = declaredConstants.filter(
|
|
1206
|
+
(name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
|
|
1207
|
+
);
|
|
1208
|
+
if (unassigned.length > 0 && !options.isUnconfigured) {
|
|
1209
|
+
// TLC reports the first constant it finds unassigned in SANY's order of declarations.
|
|
1210
|
+
const name =
|
|
1211
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
|
|
1212
|
+
unassigned.includes(constant),
|
|
1213
|
+
) ?? unassigned[0];
|
|
1214
|
+
throw new TlaConfigError(
|
|
1215
|
+
`The constant parameter ${name} is not assigned a value by the configuration file.`,
|
|
1216
|
+
);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
if (options.isTraceRecorded) {
|
|
1220
|
+
builtins.forEach((builtin, name) => {
|
|
1221
|
+
const replacement = getTraceSpecArithmetic(builtin);
|
|
1222
|
+
if (replacement) builtins.set(name, replacement);
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
916
1225
|
|
|
917
1226
|
const environment: Environment = {
|
|
918
1227
|
runtime,
|
|
@@ -920,6 +1229,7 @@ export const createEnvironment = (
|
|
|
920
1229
|
builtins,
|
|
921
1230
|
constants,
|
|
922
1231
|
substitutions,
|
|
1232
|
+
instanceNames,
|
|
923
1233
|
variables,
|
|
924
1234
|
variableIndex: getVariableIndex(
|
|
925
1235
|
variables,
|
|
@@ -927,22 +1237,9 @@ export const createEnvironment = (
|
|
|
927
1237
|
),
|
|
928
1238
|
definitionValues: new Map(),
|
|
929
1239
|
constantLevels: new Map(),
|
|
1240
|
+
substitutedDefinitions,
|
|
930
1241
|
evaluators: new WeakMap(),
|
|
931
1242
|
};
|
|
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
1243
|
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
947
1244
|
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
948
1245
|
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
@@ -954,34 +1251,59 @@ export const createEnvironment = (
|
|
|
954
1251
|
]),
|
|
955
1252
|
getDefinition: (name) => getDefinition(name, environment),
|
|
956
1253
|
});
|
|
957
|
-
const
|
|
1254
|
+
const getEvaluationError = (body: Expression) => {
|
|
958
1255
|
try {
|
|
959
1256
|
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
960
|
-
return
|
|
1257
|
+
return undefined;
|
|
961
1258
|
} catch (error) {
|
|
962
|
-
if (error instanceof TlaEvaluationError) return
|
|
1259
|
+
if (error instanceof TlaEvaluationError) return error;
|
|
963
1260
|
throw error;
|
|
964
1261
|
}
|
|
965
1262
|
};
|
|
966
1263
|
substitutions.forEach((target, name) => {
|
|
967
1264
|
const definition = definitions.get(target);
|
|
968
|
-
|
|
1265
|
+
const error =
|
|
969
1266
|
declaredConstants.includes(name) &&
|
|
970
1267
|
definition?.kind === "Operator" &&
|
|
971
1268
|
definition.params.length === 0 &&
|
|
972
|
-
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
973
|
-
|
|
974
|
-
|
|
1269
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
1270
|
+
? getEvaluationError(definition.body)
|
|
1271
|
+
: undefined;
|
|
1272
|
+
if (error) {
|
|
1273
|
+
// TLC's SpecProcessor adds the message of a TLCRuntimeException, not of an EvalException.
|
|
1274
|
+
const addendum = isEvalException(error) ? "" : ` - specifically: ${error.message}`;
|
|
975
1275
|
throw new TlaConfigError(
|
|
976
|
-
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
1276
|
+
`The configuration file substitutes constant ${name} with non-constant ${target}${addendum}`,
|
|
977
1277
|
);
|
|
978
1278
|
}
|
|
979
1279
|
});
|
|
980
|
-
|
|
1280
|
+
// TLC 1.7.4 processes the whole spec before it creates its model checker, which TLCGet reads.
|
|
1281
|
+
if (runtime.version !== "1.7.4") runtime.isStarted = true;
|
|
1282
|
+
// TLC computes the constant definitions of each module in the order SANY finishes the modules,
|
|
1283
|
+
// except those of a module that an INSTANCE gives parameters to.
|
|
1284
|
+
const instantiated = new Set(
|
|
1285
|
+
linkedList.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
|
|
1286
|
+
);
|
|
1287
|
+
const getDefinitionOrder = createTlcDefinitionOrder(options.source, options.loadModule);
|
|
981
1288
|
return {
|
|
982
1289
|
environment,
|
|
983
1290
|
assumptions,
|
|
984
1291
|
variableLocations: linkedRoot.variableLocations,
|
|
985
1292
|
instanceActions,
|
|
1293
|
+
startupDefinitions: linkedList
|
|
1294
|
+
.filter(
|
|
1295
|
+
(linked) => !instantiated.has(linked.module.name) || getDeclarations(linked).length === 0,
|
|
1296
|
+
)
|
|
1297
|
+
.flatMap((linked) =>
|
|
1298
|
+
getDefinitionOrder(linked === linkedRoot ? undefined : linked.module.name).flatMap(
|
|
1299
|
+
(name) => {
|
|
1300
|
+
const definition = definitions.get(linked.visible.get(name)?.key ?? name);
|
|
1301
|
+
return definition ? [definition] : [];
|
|
1302
|
+
},
|
|
1303
|
+
),
|
|
1304
|
+
),
|
|
1305
|
+
rootConstants: linkedRoot.constants,
|
|
1306
|
+
rootSymbols: linkedRoot.visible,
|
|
1307
|
+
javaOverrides: linkedList.flatMap((linked) => javaOverrides.get(linked.module.name) ?? []),
|
|
986
1308
|
};
|
|
987
1309
|
};
|