tla-ts 0.0.3-dev.5ea4534 → 0.0.3-dev.5fd6731
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +59 -22
- package/dist/index.d.ts +59 -22
- 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 +14 -12
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +39 -21
- package/dist/transfer.js +39 -21
- package/package.json +1 -1
- package/src/ast.ts +27 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +357 -79
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +249 -87
- package/src/errors.ts +100 -1
- package/src/evaluate.ts +182 -26
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +177 -36
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +117 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +155 -49
- package/src/resolve.ts +911 -342
- package/src/run-cli.ts +61 -11
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +26 -14
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +169 -33
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-module.ts +6 -6
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +231 -61
package/src/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
|
+
formatRange,
|
|
18
|
+
formatSanyWarnings,
|
|
19
|
+
TlaConfigError,
|
|
20
|
+
TlaEvaluationError,
|
|
21
|
+
TlaModuleError,
|
|
22
|
+
TlaSyntaxError,
|
|
23
|
+
type SanyError,
|
|
24
|
+
} from "./errors";
|
|
16
25
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
17
26
|
import {
|
|
18
27
|
createBigNumberError,
|
|
@@ -32,21 +41,28 @@ import {
|
|
|
32
41
|
import { overrideDefinitions } from "./java-overrides";
|
|
33
42
|
import { extractModule, parseModule } from "./parser";
|
|
34
43
|
import {
|
|
35
|
-
checkRedefinitions,
|
|
36
44
|
getArity,
|
|
37
45
|
isBefore,
|
|
46
|
+
isTheoremDefinition,
|
|
38
47
|
KEY_SEPARATOR,
|
|
39
48
|
LOCAL_RESOLUTION,
|
|
49
|
+
MODULE_START,
|
|
40
50
|
resolveModule,
|
|
41
51
|
resolveSubstitutions,
|
|
52
|
+
type ModuleReport,
|
|
42
53
|
type ModuleSymbol,
|
|
54
|
+
type ModuleUnit,
|
|
55
|
+
type SymbolKind,
|
|
43
56
|
type Symbols,
|
|
44
57
|
} from "./resolve";
|
|
45
58
|
import { createRuntime } from "./runtime";
|
|
59
|
+
import { parseModuleFile } from "./sany-parse-error";
|
|
46
60
|
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
61
|
+
import { getStandardRange } from "./standard-ranges";
|
|
47
62
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
48
63
|
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
49
|
-
import {
|
|
64
|
+
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
65
|
+
import { getTlcDeclarationOrder } from "./tlc-order";
|
|
50
66
|
import {
|
|
51
67
|
checkInteger,
|
|
52
68
|
createModelValue,
|
|
@@ -107,16 +123,25 @@ interface LinkedModule {
|
|
|
107
123
|
extended: LinkedModule[];
|
|
108
124
|
}
|
|
109
125
|
|
|
126
|
+
// errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
|
|
127
|
+
// the modules in its file, which SANY runs only when it reports no error.
|
|
110
128
|
interface ModuleScope {
|
|
111
129
|
id: string;
|
|
112
130
|
symbols: Symbols;
|
|
113
131
|
modules: Map<string, LinkedModule>;
|
|
114
132
|
nested: Module[];
|
|
133
|
+
errors: SanyError[];
|
|
134
|
+
warnings: SanyError[];
|
|
135
|
+
levelChecks: (() => void)[];
|
|
115
136
|
}
|
|
116
137
|
|
|
117
|
-
const getDeclarations = (linked: LinkedModule) => [
|
|
138
|
+
const getDeclarations = (linked: LinkedModule): Declaration[] => [
|
|
118
139
|
...linked.constants,
|
|
119
|
-
...linked.variables.map((variable) => ({
|
|
140
|
+
...linked.variables.map((variable, index) => ({
|
|
141
|
+
name: variable,
|
|
142
|
+
arity: 0,
|
|
143
|
+
location: linked.variableLocations[index],
|
|
144
|
+
})),
|
|
120
145
|
];
|
|
121
146
|
|
|
122
147
|
const createModuleRedefinedError = (module: Module) =>
|
|
@@ -124,6 +149,22 @@ const createModuleRedefinedError = (module: Module) =>
|
|
|
124
149
|
`Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
|
|
125
150
|
);
|
|
126
151
|
|
|
152
|
+
const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
|
|
153
|
+
module.location.range &&
|
|
154
|
+
existing.location.range && {
|
|
155
|
+
location: formatRange(module.location.range),
|
|
156
|
+
message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
|
|
160
|
+
// declaration.
|
|
161
|
+
const getNodeWord = (kind: SymbolKind) =>
|
|
162
|
+
["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
|
|
163
|
+
|
|
164
|
+
// The Java class of the node SANY binds a symbol to.
|
|
165
|
+
const getNodeClass = (kind: SymbolKind) =>
|
|
166
|
+
kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
|
|
167
|
+
|
|
127
168
|
const createLocalFunction = (definition: Definition, key: string): Definition => ({
|
|
128
169
|
kind: "Operator",
|
|
129
170
|
name: key,
|
|
@@ -222,6 +263,8 @@ export const createEnvironment = (
|
|
|
222
263
|
const runtime = createRuntime(config.checkDeadlock, options.seed);
|
|
223
264
|
const linkedModules = new Map<string, LinkedModule>();
|
|
224
265
|
const nestedModules = new Map<string, LinkedModule>();
|
|
266
|
+
// Each module by name, from the time its level checks run.
|
|
267
|
+
const checkedModules = new Map<string, LinkedModule>();
|
|
225
268
|
const linkedList: LinkedModule[] = [];
|
|
226
269
|
const dependencies = new Map<string, Set<string>>();
|
|
227
270
|
const loading: Set<string>[] = [];
|
|
@@ -236,7 +279,25 @@ export const createEnvironment = (
|
|
|
236
279
|
const copyCount = { value: 0 };
|
|
237
280
|
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
238
281
|
const instanceChecks: InstanceCheck[] = [];
|
|
282
|
+
// The warnings SANY reports for the spec.
|
|
283
|
+
const warnings: SanyError[] = [];
|
|
284
|
+
const checkInstance = createInstanceChecker((name) => {
|
|
285
|
+
const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
|
|
286
|
+
return {
|
|
287
|
+
constants: linked.constants,
|
|
288
|
+
variables: linked.variables,
|
|
289
|
+
definitions: [
|
|
290
|
+
...new Set(
|
|
291
|
+
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
292
|
+
),
|
|
293
|
+
],
|
|
294
|
+
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
295
|
+
theorems: linked.module.theorems,
|
|
296
|
+
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
297
|
+
};
|
|
298
|
+
});
|
|
239
299
|
const instanceActions = new Map<string, InstanceAction>();
|
|
300
|
+
const qualifiedNames = new Map<string, string>();
|
|
240
301
|
|
|
241
302
|
const addStandardModule = (name: string): Symbols | undefined => {
|
|
242
303
|
const cached = standardExports.get(name);
|
|
@@ -251,7 +312,12 @@ export const createEnvironment = (
|
|
|
251
312
|
);
|
|
252
313
|
standardModule.operators.forEach((operator) => {
|
|
253
314
|
builtins.set(operator.name, operator);
|
|
254
|
-
exported.set(operator.name, {
|
|
315
|
+
exported.set(operator.name, {
|
|
316
|
+
key: operator.name,
|
|
317
|
+
arity: operator.arity,
|
|
318
|
+
kind: "Definition",
|
|
319
|
+
range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
|
|
320
|
+
});
|
|
255
321
|
});
|
|
256
322
|
standardExports.set(name, exported);
|
|
257
323
|
return exported;
|
|
@@ -259,7 +325,11 @@ export const createEnvironment = (
|
|
|
259
325
|
|
|
260
326
|
const getStandardSymbols = (name: string) => {
|
|
261
327
|
const symbols = addStandardModule(name);
|
|
262
|
-
|
|
328
|
+
const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
|
|
329
|
+
if (symbols) useModule(name, dependencies);
|
|
330
|
+
// TLC loads the Java class of each module a standard module uses, so one that uses Integers
|
|
331
|
+
// overrides the arithmetic of Naturals.
|
|
332
|
+
if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
|
|
263
333
|
return symbols;
|
|
264
334
|
};
|
|
265
335
|
|
|
@@ -288,13 +358,25 @@ export const createEnvironment = (
|
|
|
288
358
|
BUNDLED_MODULES.get(name) ??
|
|
289
359
|
extractModule(options.source, name);
|
|
290
360
|
if (source === undefined)
|
|
291
|
-
throw
|
|
292
|
-
|
|
361
|
+
throw createSanyError(
|
|
362
|
+
[
|
|
363
|
+
{
|
|
364
|
+
location: `In module ${importer}`,
|
|
365
|
+
message: `Cannot find source file for module ${name} imported in module ${importer}.`,
|
|
366
|
+
},
|
|
367
|
+
],
|
|
368
|
+
true,
|
|
293
369
|
);
|
|
294
|
-
const module =
|
|
370
|
+
const module = parseModuleFile(source, name);
|
|
295
371
|
if (module.name !== name) {
|
|
296
|
-
throw
|
|
297
|
-
|
|
372
|
+
throw createSanyError(
|
|
373
|
+
[
|
|
374
|
+
{
|
|
375
|
+
location: "Unknown location",
|
|
376
|
+
message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
true,
|
|
298
380
|
);
|
|
299
381
|
}
|
|
300
382
|
return rejectModuleLiterals(module);
|
|
@@ -310,6 +392,8 @@ export const createEnvironment = (
|
|
|
310
392
|
}
|
|
311
393
|
const closure = new Set<string>();
|
|
312
394
|
const nested: Module[] = [];
|
|
395
|
+
const errors: SanyError[] = [];
|
|
396
|
+
const levelChecks: (() => void)[] = [];
|
|
313
397
|
linking.push(parsed.name);
|
|
314
398
|
loading.push(closure);
|
|
315
399
|
const linked = linkModule(parsed, {
|
|
@@ -317,11 +401,19 @@ export const createEnvironment = (
|
|
|
317
401
|
symbols: new Map(),
|
|
318
402
|
modules: new Map(),
|
|
319
403
|
nested,
|
|
404
|
+
errors,
|
|
405
|
+
warnings,
|
|
406
|
+
levelChecks,
|
|
320
407
|
});
|
|
321
408
|
loading.pop();
|
|
322
409
|
linking.pop();
|
|
410
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
323
411
|
const duplicate = nested.find((module) => closure.has(module.name));
|
|
412
|
+
const loaded = duplicate && linkedModules.get(duplicate.name);
|
|
413
|
+
const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
|
|
414
|
+
if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
|
|
324
415
|
if (duplicate) throw createModuleRedefinedError(duplicate);
|
|
416
|
+
levelChecks.forEach((check) => check());
|
|
325
417
|
return { linked, closure };
|
|
326
418
|
};
|
|
327
419
|
|
|
@@ -341,12 +433,7 @@ export const createEnvironment = (
|
|
|
341
433
|
const instantiate = (
|
|
342
434
|
instance: Instance,
|
|
343
435
|
module: Module,
|
|
344
|
-
unit:
|
|
345
|
-
imported: Symbols;
|
|
346
|
-
importedAt: Map<string, SourceLocation>;
|
|
347
|
-
definitions: Map<string, Definition>;
|
|
348
|
-
getKey: (definition: Definition) => string;
|
|
349
|
-
},
|
|
436
|
+
unit: ModuleUnit,
|
|
350
437
|
isParameterFree: boolean,
|
|
351
438
|
nested: LinkedModule | undefined,
|
|
352
439
|
): Symbols => {
|
|
@@ -354,7 +441,10 @@ export const createEnvironment = (
|
|
|
354
441
|
if (standardSymbols) {
|
|
355
442
|
if (instance.name === undefined) return standardSymbols;
|
|
356
443
|
return new Map(
|
|
357
|
-
[...standardSymbols].map(([name, symbol]) => [
|
|
444
|
+
[...standardSymbols].map(([name, symbol]) => [
|
|
445
|
+
`${instance.name}!${name}`,
|
|
446
|
+
{ key: symbol.key, arity: symbol.arity, kind: symbol.kind },
|
|
447
|
+
]),
|
|
358
448
|
);
|
|
359
449
|
}
|
|
360
450
|
const linked = nested ?? getLinkedModule(instance.module, module.name);
|
|
@@ -399,16 +489,24 @@ export const createEnvironment = (
|
|
|
399
489
|
const key = instantiation.getKey(symbol.key);
|
|
400
490
|
if (declarations.length > 0)
|
|
401
491
|
instanceActions.set(key, { name, location: instance.location });
|
|
402
|
-
|
|
492
|
+
// SANY binds a copy of a definition that depends on the parameters at the INSTANCE.
|
|
493
|
+
symbols.set(name, {
|
|
494
|
+
key,
|
|
495
|
+
arity: symbol.arity,
|
|
496
|
+
kind: symbol.kind,
|
|
497
|
+
range: parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
|
|
498
|
+
});
|
|
403
499
|
return;
|
|
404
500
|
}
|
|
405
501
|
const key = instantiation.getQualifiedKey(symbol.key);
|
|
406
502
|
if (declarations.length > 0) {
|
|
407
503
|
instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
|
|
408
504
|
}
|
|
505
|
+
qualifiedNames.set(key, `${instance.name}!${name}`);
|
|
409
506
|
symbols.set(`${instance.name}!${name}`, {
|
|
410
507
|
key,
|
|
411
508
|
arity: symbol.arity + instance.params.length,
|
|
509
|
+
kind: symbol.kind,
|
|
412
510
|
});
|
|
413
511
|
});
|
|
414
512
|
return symbols;
|
|
@@ -586,29 +684,58 @@ export const createEnvironment = (
|
|
|
586
684
|
|
|
587
685
|
const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
|
|
588
686
|
const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
|
|
687
|
+
const firstCheck = instanceChecks.length;
|
|
589
688
|
const visible: Symbols = new Map([
|
|
590
689
|
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
591
690
|
operator.name,
|
|
592
|
-
{ key: operator.name, arity: operator.arity },
|
|
691
|
+
{ key: operator.name, arity: operator.arity, kind: "Builtin" },
|
|
593
692
|
]),
|
|
594
693
|
...scope.symbols,
|
|
595
694
|
]);
|
|
695
|
+
const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
|
|
696
|
+
const reports = [extendsReport];
|
|
697
|
+
// Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
|
|
698
|
+
// and warns of two definitions or two declarations but rejects a definition and a declaration.
|
|
699
|
+
const mergeExtended = (symbols: Symbols) =>
|
|
700
|
+
symbols.forEach((symbol, name) => {
|
|
701
|
+
const current = exported.get(name);
|
|
702
|
+
if (!current) {
|
|
703
|
+
visible.set(name, symbol);
|
|
704
|
+
exported.set(name, symbol);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
if (
|
|
708
|
+
!current.range ||
|
|
709
|
+
!symbol.range ||
|
|
710
|
+
formatRange(current.range) === formatRange(symbol.range)
|
|
711
|
+
) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const location = formatRange(symbol.range);
|
|
715
|
+
const existing = formatRange(current.range);
|
|
716
|
+
if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
|
|
717
|
+
extendsReport.warnings.push({
|
|
718
|
+
location,
|
|
719
|
+
message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
720
|
+
});
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
extendsReport.errors.push({
|
|
724
|
+
location,
|
|
725
|
+
message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
726
|
+
});
|
|
727
|
+
});
|
|
596
728
|
const exported: Symbols = new Map();
|
|
597
729
|
const modules = new Map<string, LinkedModule>();
|
|
598
730
|
const extended: LinkedModule[] = [];
|
|
599
731
|
const constants = [...parsed.constants];
|
|
600
732
|
const variables = parsed.variables.map((variable) => variable.name);
|
|
601
733
|
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
734
|
parsed.extends.forEach((name) => {
|
|
608
735
|
const nested = scope.modules.get(name);
|
|
609
736
|
const standardSymbols = nested ? undefined : getStandardSymbols(name);
|
|
610
737
|
if (standardSymbols) {
|
|
611
|
-
|
|
738
|
+
mergeExtended(standardSymbols);
|
|
612
739
|
return;
|
|
613
740
|
}
|
|
614
741
|
const linked = nested ?? getLinkedModule(name, parsed.name);
|
|
@@ -622,61 +749,84 @@ export const createEnvironment = (
|
|
|
622
749
|
variables.push(variable);
|
|
623
750
|
variableLocations.push(linked.variableLocations[index]);
|
|
624
751
|
});
|
|
625
|
-
|
|
752
|
+
mergeExtended(linked.exported);
|
|
626
753
|
linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
|
|
627
754
|
extended.push(linked);
|
|
628
755
|
});
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
756
|
+
const bound = new Map(visible);
|
|
757
|
+
const declarations = [
|
|
758
|
+
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
759
|
+
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
760
|
+
];
|
|
761
|
+
const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
|
|
762
|
+
key: declaration.name,
|
|
763
|
+
arity: declaration.arity,
|
|
764
|
+
kind: declaration.kind,
|
|
765
|
+
range: declaration.location.range,
|
|
766
|
+
});
|
|
767
|
+
declarations.forEach((declaration) =>
|
|
768
|
+
exported.set(declaration.name, getDeclarationSymbol(declaration)),
|
|
638
769
|
);
|
|
770
|
+
const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
|
|
771
|
+
key: getKey(definition),
|
|
772
|
+
arity: getArity(definition),
|
|
773
|
+
kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
|
|
774
|
+
range: definition.location.range,
|
|
775
|
+
});
|
|
639
776
|
const isParameterFree = constants.length === 0 && variables.length === 0;
|
|
640
777
|
const getKey = (definition: Definition) =>
|
|
641
778
|
parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
|
|
642
|
-
const
|
|
643
|
-
const getUnit = (module: Module) => ({
|
|
779
|
+
const instanced = new Map<Instance, Symbols>();
|
|
780
|
+
const getUnit = (module: Module): ModuleUnit => ({
|
|
644
781
|
imported: visible,
|
|
645
|
-
|
|
646
|
-
|
|
782
|
+
bound,
|
|
783
|
+
// SANY keeps the first of two definitions of a name.
|
|
784
|
+
definitions: new Map(
|
|
785
|
+
module.definitions.toReversed().map((definition) => [definition.name, definition]),
|
|
786
|
+
),
|
|
647
787
|
getKey,
|
|
788
|
+
instanced,
|
|
789
|
+
reports,
|
|
790
|
+
warnings: scope.warnings,
|
|
648
791
|
});
|
|
649
792
|
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
650
793
|
const getSymbolsBefore = (location: SourceLocation): Symbols =>
|
|
651
794
|
new Map([
|
|
652
795
|
...visible,
|
|
653
|
-
...
|
|
654
|
-
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
655
|
-
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
656
|
-
]
|
|
796
|
+
...declarations
|
|
657
797
|
.filter((declaration) => isBefore(declaration.location, location))
|
|
658
798
|
.map((declaration): [string, ModuleSymbol] => [
|
|
659
799
|
declaration.name,
|
|
660
|
-
|
|
800
|
+
getDeclarationSymbol(declaration),
|
|
661
801
|
]),
|
|
662
802
|
...parsed.definitions
|
|
663
803
|
.filter((definition) => isBefore(definition.location, location))
|
|
664
804
|
.map((definition): [string, ModuleSymbol] => [
|
|
665
805
|
definition.name,
|
|
666
|
-
|
|
806
|
+
getDefinitionSymbol(definition),
|
|
667
807
|
]),
|
|
668
808
|
]);
|
|
809
|
+
// Generator.generateModule reports the errors of a nested module and goes on, and then
|
|
810
|
+
// SymbolTable.addModule reports a module already defined.
|
|
669
811
|
const linkNested = (nested: Module) => {
|
|
670
812
|
const visibleModules = getVisibleModules();
|
|
671
|
-
|
|
813
|
+
const existing = visibleModules.get(nested.name);
|
|
814
|
+
const redefinition = existing && getModuleRedefinition(nested, existing.module);
|
|
815
|
+
if (existing && !redefinition) throw createModuleRedefinedError(nested);
|
|
672
816
|
scope.nested.push(nested);
|
|
817
|
+
const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
|
|
818
|
+
reports.push(report);
|
|
673
819
|
const linked = linkModule(nested, {
|
|
674
820
|
id: `${scope.id}/${nested.name}`,
|
|
675
821
|
symbols: getSymbolsBefore(nested.location),
|
|
676
822
|
modules: visibleModules,
|
|
677
823
|
nested: scope.nested,
|
|
824
|
+
errors: report.errors,
|
|
825
|
+
warnings: report.warnings,
|
|
826
|
+
levelChecks: scope.levelChecks,
|
|
678
827
|
});
|
|
679
|
-
|
|
828
|
+
if (redefinition) report.errors.push(redefinition);
|
|
829
|
+
if (!existing) modules.set(nested.name, linked);
|
|
680
830
|
if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
|
|
681
831
|
};
|
|
682
832
|
[
|
|
@@ -690,10 +840,12 @@ export const createEnvironment = (
|
|
|
690
840
|
isParameterFree,
|
|
691
841
|
getVisibleModules().get(instance.module),
|
|
692
842
|
);
|
|
693
|
-
|
|
694
|
-
|
|
843
|
+
instanced.set(instance, symbols);
|
|
844
|
+
symbols.forEach((symbol, name) => {
|
|
845
|
+
if (visible.has(name)) return;
|
|
846
|
+
visible.set(name, symbol);
|
|
847
|
+
if (!instance.isLocal) exported.set(name, symbol);
|
|
695
848
|
});
|
|
696
|
-
addSymbols(symbols, !instance.isLocal);
|
|
697
849
|
},
|
|
698
850
|
})),
|
|
699
851
|
...parsed.modules.map((nested) => ({
|
|
@@ -703,7 +855,6 @@ export const createEnvironment = (
|
|
|
703
855
|
]
|
|
704
856
|
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
705
857
|
.forEach((unit) => unit.link());
|
|
706
|
-
checkRedefinitions(parsed, getUnit(parsed));
|
|
707
858
|
const isInstancedAfterDefinition = (nested: Module) => {
|
|
708
859
|
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
709
860
|
return first !== undefined && isBefore(nested.location, first.location);
|
|
@@ -719,20 +870,15 @@ export const createEnvironment = (
|
|
|
719
870
|
),
|
|
720
871
|
getAvailableDefinitions(visible),
|
|
721
872
|
);
|
|
722
|
-
resolveModule(module, getUnit(module));
|
|
873
|
+
scope.errors.push(...resolveModule(module, getUnit(module)));
|
|
723
874
|
module.definitions.forEach((definition) => {
|
|
724
875
|
const key = getKey(definition);
|
|
725
876
|
definitions.set(key, definition);
|
|
726
877
|
parameterized.set(key, !isParameterFree);
|
|
727
|
-
const symbol =
|
|
878
|
+
const symbol = getDefinitionSymbol(definition);
|
|
728
879
|
visible.set(definition.name, symbol);
|
|
729
880
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
730
881
|
});
|
|
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
882
|
const linked = {
|
|
737
883
|
module,
|
|
738
884
|
constants,
|
|
@@ -743,11 +889,31 @@ export const createEnvironment = (
|
|
|
743
889
|
modules,
|
|
744
890
|
extended,
|
|
745
891
|
};
|
|
892
|
+
if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
|
|
893
|
+
// SANY checks the instances of a module with its definitions and theorems, so it reports their
|
|
894
|
+
// errors together.
|
|
895
|
+
const checks = instanceChecks
|
|
896
|
+
.splice(firstCheck)
|
|
897
|
+
.map((check) => ({ location: check.location, check: () => checkInstance(check) }));
|
|
898
|
+
scope.levelChecks.push(() =>
|
|
899
|
+
checkModuleLevels(
|
|
900
|
+
module,
|
|
901
|
+
{
|
|
902
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
903
|
+
variables: new Set(variables),
|
|
904
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
905
|
+
getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
|
|
906
|
+
},
|
|
907
|
+
checks,
|
|
908
|
+
warnings,
|
|
909
|
+
),
|
|
910
|
+
);
|
|
746
911
|
linkedList.push(linked);
|
|
747
912
|
return linked;
|
|
748
913
|
};
|
|
749
914
|
|
|
750
915
|
const linkedRoot = linkTopLevel(root).linked;
|
|
916
|
+
if (warnings.length > 0) options.print(formatSanyWarnings(warnings));
|
|
751
917
|
const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
|
|
752
918
|
linkedRoot.visible.forEach((symbol, name) => {
|
|
753
919
|
const definition = definitions.get(symbol.key);
|
|
@@ -908,11 +1074,19 @@ export const createEnvironment = (
|
|
|
908
1074
|
if (symbol.key === key && name !== key) definitions.set(name, alias);
|
|
909
1075
|
});
|
|
910
1076
|
});
|
|
911
|
-
declaredConstants.
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
1077
|
+
const unassigned = declaredConstants.filter(
|
|
1078
|
+
(name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
|
|
1079
|
+
);
|
|
1080
|
+
if (unassigned.length > 0) {
|
|
1081
|
+
// TLC reports the first constant it finds unassigned in SANY's order of declarations.
|
|
1082
|
+
const name =
|
|
1083
|
+
getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
|
|
1084
|
+
unassigned.includes(constant),
|
|
1085
|
+
) ?? unassigned[0];
|
|
1086
|
+
throw new TlaConfigError(
|
|
1087
|
+
`The constant parameter ${name} is not assigned a value by the configuration file.`,
|
|
1088
|
+
);
|
|
1089
|
+
}
|
|
916
1090
|
|
|
917
1091
|
const environment: Environment = {
|
|
918
1092
|
runtime,
|
|
@@ -929,20 +1103,6 @@ export const createEnvironment = (
|
|
|
929
1103
|
constantLevels: new Map(),
|
|
930
1104
|
evaluators: new WeakMap(),
|
|
931
1105
|
};
|
|
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
1106
|
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
947
1107
|
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
948
1108
|
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
@@ -954,26 +1114,28 @@ export const createEnvironment = (
|
|
|
954
1114
|
]),
|
|
955
1115
|
getDefinition: (name) => getDefinition(name, environment),
|
|
956
1116
|
});
|
|
957
|
-
const
|
|
1117
|
+
const getEvaluationError = (body: Expression) => {
|
|
958
1118
|
try {
|
|
959
1119
|
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
960
|
-
return
|
|
1120
|
+
return undefined;
|
|
961
1121
|
} catch (error) {
|
|
962
|
-
if (error instanceof TlaEvaluationError) return
|
|
1122
|
+
if (error instanceof TlaEvaluationError) return error;
|
|
963
1123
|
throw error;
|
|
964
1124
|
}
|
|
965
1125
|
};
|
|
966
1126
|
substitutions.forEach((target, name) => {
|
|
967
1127
|
const definition = definitions.get(target);
|
|
968
|
-
|
|
1128
|
+
const error =
|
|
969
1129
|
declaredConstants.includes(name) &&
|
|
970
1130
|
definition?.kind === "Operator" &&
|
|
971
1131
|
definition.params.length === 0 &&
|
|
972
|
-
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
973
|
-
|
|
974
|
-
|
|
1132
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
1133
|
+
? getEvaluationError(definition.body)
|
|
1134
|
+
: undefined;
|
|
1135
|
+
// TLC adds the reason unless a standard module's Java code failed.
|
|
1136
|
+
if (error) {
|
|
975
1137
|
throw new TlaConfigError(
|
|
976
|
-
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
1138
|
+
`The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
|
|
977
1139
|
);
|
|
978
1140
|
}
|
|
979
1141
|
});
|