tla-ts 0.0.3-dev.3504663 → 0.0.3-dev.3539317
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.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/run-cli.cjs +3 -3
- package/dist/run-cli.js +20 -20
- package/dist/transfer.cjs +24 -23
- package/dist/transfer.js +24 -23
- package/package.json +1 -1
- package/src/ast.ts +20 -3
- package/src/check.ts +7 -6
- package/src/environment.ts +264 -93
- package/src/errors.ts +15 -4
- package/src/instances.ts +1 -1
- package/src/levels.ts +31 -26
- package/src/parser.ts +103 -36
- package/src/resolve.ts +1105 -525
- package/src/run-cli.ts +2 -0
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +210 -162
- package/src/symmetry.ts +93 -35
package/src/environment.ts
CHANGED
|
@@ -14,10 +14,13 @@ import type { ConfigValue, ModelConfig } from "./config";
|
|
|
14
14
|
import type { FileSystem } from "./json";
|
|
15
15
|
import {
|
|
16
16
|
createSanyError,
|
|
17
|
+
formatRange,
|
|
18
|
+
formatSanyWarnings,
|
|
17
19
|
TlaConfigError,
|
|
18
20
|
TlaEvaluationError,
|
|
19
21
|
TlaModuleError,
|
|
20
22
|
TlaSyntaxError,
|
|
23
|
+
type SanyError,
|
|
21
24
|
} from "./errors";
|
|
22
25
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
23
26
|
import {
|
|
@@ -38,19 +41,26 @@ import {
|
|
|
38
41
|
import { overrideDefinitions } from "./java-overrides";
|
|
39
42
|
import { extractModule, parseModule } from "./parser";
|
|
40
43
|
import {
|
|
41
|
-
checkRedefinitions,
|
|
42
44
|
getArity,
|
|
45
|
+
getParamArities,
|
|
43
46
|
isBefore,
|
|
47
|
+
isTheoremDefinition,
|
|
44
48
|
KEY_SEPARATOR,
|
|
45
49
|
LOCAL_RESOLUTION,
|
|
50
|
+
MODULE_START,
|
|
46
51
|
resolveModule,
|
|
47
52
|
resolveSubstitutions,
|
|
53
|
+
type ModuleReport,
|
|
48
54
|
type ModuleSymbol,
|
|
55
|
+
type ModuleUnit,
|
|
56
|
+
type SymbolKind,
|
|
49
57
|
type Symbols,
|
|
50
58
|
} from "./resolve";
|
|
51
59
|
import { createRuntime } from "./runtime";
|
|
52
60
|
import { parseModuleFile } from "./sany-parse-error";
|
|
53
61
|
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
62
|
+
import { STANDARD_LEVELS } from "./standard-levels";
|
|
63
|
+
import { getStandardRange } from "./standard-ranges";
|
|
54
64
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
55
65
|
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
56
66
|
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
@@ -113,18 +123,28 @@ interface LinkedModule {
|
|
|
113
123
|
exported: Symbols;
|
|
114
124
|
modules: Map<string, LinkedModule>;
|
|
115
125
|
extended: LinkedModule[];
|
|
126
|
+
facts: Set<string>;
|
|
116
127
|
}
|
|
117
128
|
|
|
129
|
+
// errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
|
|
130
|
+
// the modules in its file, which SANY runs only when it reports no error.
|
|
118
131
|
interface ModuleScope {
|
|
119
132
|
id: string;
|
|
120
133
|
symbols: Symbols;
|
|
121
134
|
modules: Map<string, LinkedModule>;
|
|
122
135
|
nested: Module[];
|
|
136
|
+
errors: SanyError[];
|
|
137
|
+
warnings: SanyError[];
|
|
138
|
+
levelChecks: (() => void)[];
|
|
123
139
|
}
|
|
124
140
|
|
|
125
|
-
const getDeclarations = (linked: LinkedModule) => [
|
|
141
|
+
const getDeclarations = (linked: LinkedModule): Declaration[] => [
|
|
126
142
|
...linked.constants,
|
|
127
|
-
...linked.variables.map((variable) => ({
|
|
143
|
+
...linked.variables.map((variable, index) => ({
|
|
144
|
+
name: variable,
|
|
145
|
+
arity: 0,
|
|
146
|
+
location: linked.variableLocations[index],
|
|
147
|
+
})),
|
|
128
148
|
];
|
|
129
149
|
|
|
130
150
|
const createModuleRedefinedError = (module: Module) =>
|
|
@@ -132,6 +152,22 @@ const createModuleRedefinedError = (module: Module) =>
|
|
|
132
152
|
`Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
|
|
133
153
|
);
|
|
134
154
|
|
|
155
|
+
const getModuleRedefinition = (module: Module, existing: Module): SanyError | undefined =>
|
|
156
|
+
module.location.range &&
|
|
157
|
+
existing.location.range && {
|
|
158
|
+
location: formatRange(module.location.range),
|
|
159
|
+
message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
|
|
163
|
+
// declaration.
|
|
164
|
+
const getNodeWord = (kind: SymbolKind) =>
|
|
165
|
+
["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
|
|
166
|
+
|
|
167
|
+
// The Java class of the node SANY binds a symbol to.
|
|
168
|
+
const getNodeClass = (kind: SymbolKind) =>
|
|
169
|
+
kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
|
|
170
|
+
|
|
135
171
|
const createLocalFunction = (definition: Definition, key: string): Definition => ({
|
|
136
172
|
kind: "Operator",
|
|
137
173
|
name: key,
|
|
@@ -246,6 +282,8 @@ export const createEnvironment = (
|
|
|
246
282
|
const copyCount = { value: 0 };
|
|
247
283
|
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
248
284
|
const instanceChecks: InstanceCheck[] = [];
|
|
285
|
+
// The warnings SANY reports for the spec.
|
|
286
|
+
const warnings: SanyError[] = [];
|
|
249
287
|
const checkInstance = createInstanceChecker((name) => {
|
|
250
288
|
const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
|
|
251
289
|
return {
|
|
@@ -277,7 +315,13 @@ export const createEnvironment = (
|
|
|
277
315
|
);
|
|
278
316
|
standardModule.operators.forEach((operator) => {
|
|
279
317
|
builtins.set(operator.name, operator);
|
|
280
|
-
exported.set(operator.name, {
|
|
318
|
+
exported.set(operator.name, {
|
|
319
|
+
key: operator.name,
|
|
320
|
+
arity: operator.arity,
|
|
321
|
+
params: STANDARD_LEVELS.get(operator.name)?.formals.map((formal) => formal.arity),
|
|
322
|
+
kind: "Definition",
|
|
323
|
+
range: getStandardRange(name, operator.name) ?? exported.get(operator.name)?.range,
|
|
324
|
+
});
|
|
281
325
|
});
|
|
282
326
|
standardExports.set(name, exported);
|
|
283
327
|
return exported;
|
|
@@ -352,6 +396,8 @@ export const createEnvironment = (
|
|
|
352
396
|
}
|
|
353
397
|
const closure = new Set<string>();
|
|
354
398
|
const nested: Module[] = [];
|
|
399
|
+
const errors: SanyError[] = [];
|
|
400
|
+
const levelChecks: (() => void)[] = [];
|
|
355
401
|
linking.push(parsed.name);
|
|
356
402
|
loading.push(closure);
|
|
357
403
|
const linked = linkModule(parsed, {
|
|
@@ -359,11 +405,19 @@ export const createEnvironment = (
|
|
|
359
405
|
symbols: new Map(),
|
|
360
406
|
modules: new Map(),
|
|
361
407
|
nested,
|
|
408
|
+
errors,
|
|
409
|
+
warnings,
|
|
410
|
+
levelChecks,
|
|
362
411
|
});
|
|
363
412
|
loading.pop();
|
|
364
413
|
linking.pop();
|
|
414
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
365
415
|
const duplicate = nested.find((module) => closure.has(module.name));
|
|
416
|
+
const loaded = duplicate && linkedModules.get(duplicate.name);
|
|
417
|
+
const redefinition = loaded && getModuleRedefinition(duplicate, loaded.module);
|
|
418
|
+
if (redefinition) throw createSanyError([redefinition], false, undefined, warnings);
|
|
366
419
|
if (duplicate) throw createModuleRedefinedError(duplicate);
|
|
420
|
+
levelChecks.forEach((check) => check());
|
|
367
421
|
return { linked, closure };
|
|
368
422
|
};
|
|
369
423
|
|
|
@@ -383,23 +437,16 @@ export const createEnvironment = (
|
|
|
383
437
|
const instantiate = (
|
|
384
438
|
instance: Instance,
|
|
385
439
|
module: Module,
|
|
386
|
-
unit:
|
|
387
|
-
imported: Symbols;
|
|
388
|
-
importedAt: Map<string, SourceLocation>;
|
|
389
|
-
definitions: Map<string, Definition>;
|
|
390
|
-
getKey: (definition: Definition) => string;
|
|
391
|
-
},
|
|
440
|
+
unit: ModuleUnit,
|
|
392
441
|
isParameterFree: boolean,
|
|
393
442
|
nested: LinkedModule | undefined,
|
|
394
443
|
): Symbols => {
|
|
395
444
|
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
396
|
-
if (standardSymbols)
|
|
397
|
-
if (instance.name === undefined) return standardSymbols;
|
|
398
|
-
return new Map(
|
|
399
|
-
[...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
|
|
400
|
-
);
|
|
401
|
-
}
|
|
445
|
+
if (standardSymbols) return getStandardInstanceSymbols(instance, standardSymbols);
|
|
402
446
|
const linked = nested ?? getLinkedModule(instance.module, module.name);
|
|
447
|
+
linked.facts.forEach((name) =>
|
|
448
|
+
unit.facts.add(instance.name === undefined ? name : `${instance.name}!${name}`),
|
|
449
|
+
);
|
|
403
450
|
const declarations = getDeclarations(linked);
|
|
404
451
|
const suffix = `#${copyCount.value++}`;
|
|
405
452
|
const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
|
|
@@ -433,28 +480,69 @@ export const createEnvironment = (
|
|
|
433
480
|
},
|
|
434
481
|
createName,
|
|
435
482
|
});
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
symbols.set(name, { key, arity: symbol.arity });
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const key = instantiation.getQualifiedKey(symbol.key);
|
|
448
|
-
if (declarations.length > 0) {
|
|
449
|
-
instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
|
|
450
|
-
}
|
|
451
|
-
qualifiedNames.set(key, `${instance.name}!${name}`);
|
|
452
|
-
symbols.set(`${instance.name}!${name}`, {
|
|
453
|
-
key,
|
|
454
|
-
arity: symbol.arity + instance.params.length,
|
|
455
|
-
});
|
|
483
|
+
return getInstanceSymbols(instance, linked.exported, declarations, (key, name) => {
|
|
484
|
+
const copy =
|
|
485
|
+
instance.name === undefined
|
|
486
|
+
? instantiation.getKey(key)
|
|
487
|
+
: instantiation.getQualifiedKey(key);
|
|
488
|
+
if (declarations.length > 0) instanceActions.set(copy, { name, location: instance.location });
|
|
489
|
+
if (instance.name !== undefined) qualifiedNames.set(copy, name);
|
|
490
|
+
return copy;
|
|
456
491
|
});
|
|
457
|
-
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const getStandardInstanceSymbols = (instance: Instance, standardSymbols: Symbols): Symbols =>
|
|
495
|
+
instance.name === undefined
|
|
496
|
+
? standardSymbols
|
|
497
|
+
: new Map(
|
|
498
|
+
[...standardSymbols].map(([name, symbol]) => [
|
|
499
|
+
`${instance.name}!${name}`,
|
|
500
|
+
{ key: symbol.key, arity: symbol.arity, params: symbol.params, kind: symbol.kind },
|
|
501
|
+
]),
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
// The names an INSTANCE adds for the definitions of the module it instantiates, each with the key
|
|
505
|
+
// that getKey gives it. SANY binds a copy of a definition that depends on the parameters at the
|
|
506
|
+
// INSTANCE.
|
|
507
|
+
const getInstanceSymbols = (
|
|
508
|
+
instance: Instance,
|
|
509
|
+
exported: Symbols,
|
|
510
|
+
declarations: Declaration[],
|
|
511
|
+
getKey: (key: string, name: string) => string,
|
|
512
|
+
): Symbols => {
|
|
513
|
+
const declared = new Set(declarations.map((declaration) => declaration.name));
|
|
514
|
+
return new Map(
|
|
515
|
+
[...exported]
|
|
516
|
+
.filter(([name]) => !declared.has(name))
|
|
517
|
+
.map(([name, symbol]): [string, ModuleSymbol] => {
|
|
518
|
+
if (instance.name === undefined) {
|
|
519
|
+
return [
|
|
520
|
+
name,
|
|
521
|
+
{
|
|
522
|
+
key: getKey(symbol.key, name),
|
|
523
|
+
arity: symbol.arity,
|
|
524
|
+
params: symbol.params,
|
|
525
|
+
kind: symbol.kind,
|
|
526
|
+
range:
|
|
527
|
+
parameterized.get(symbol.key) === true ? instance.location.range : symbol.range,
|
|
528
|
+
},
|
|
529
|
+
];
|
|
530
|
+
}
|
|
531
|
+
const qualified = `${instance.name}!${name}`;
|
|
532
|
+
return [
|
|
533
|
+
qualified,
|
|
534
|
+
{
|
|
535
|
+
key: getKey(symbol.key, qualified),
|
|
536
|
+
arity: symbol.arity + instance.params.length,
|
|
537
|
+
params: symbol.params && [
|
|
538
|
+
...instance.params.map((param) => param.arity),
|
|
539
|
+
...symbol.params,
|
|
540
|
+
],
|
|
541
|
+
kind: symbol.kind,
|
|
542
|
+
},
|
|
543
|
+
];
|
|
544
|
+
}),
|
|
545
|
+
);
|
|
458
546
|
};
|
|
459
547
|
|
|
460
548
|
const getLocalArguments = (params: OperatorParam[], location: SourceLocation) =>
|
|
@@ -633,26 +721,55 @@ export const createEnvironment = (
|
|
|
633
721
|
const visible: Symbols = new Map([
|
|
634
722
|
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
635
723
|
operator.name,
|
|
636
|
-
{ key: operator.name, arity: operator.arity },
|
|
724
|
+
{ key: operator.name, arity: operator.arity, kind: "Builtin" },
|
|
637
725
|
]),
|
|
638
726
|
...scope.symbols,
|
|
639
727
|
]);
|
|
728
|
+
const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
|
|
729
|
+
const reports = [extendsReport];
|
|
730
|
+
// Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
|
|
731
|
+
// and warns of two definitions or two declarations but rejects a definition and a declaration.
|
|
732
|
+
const mergeExtended = (symbols: Symbols) =>
|
|
733
|
+
symbols.forEach((symbol, name) => {
|
|
734
|
+
const current = exported.get(name);
|
|
735
|
+
if (!current) {
|
|
736
|
+
visible.set(name, symbol);
|
|
737
|
+
exported.set(name, symbol);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
if (
|
|
741
|
+
!current.range ||
|
|
742
|
+
!symbol.range ||
|
|
743
|
+
formatRange(current.range) === formatRange(symbol.range)
|
|
744
|
+
) {
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
const location = formatRange(symbol.range);
|
|
748
|
+
const existing = formatRange(current.range);
|
|
749
|
+
if (getNodeClass(current.kind) === getNodeClass(symbol.kind)) {
|
|
750
|
+
extendsReport.warnings.push({
|
|
751
|
+
location,
|
|
752
|
+
message: `Warning: the ${getNodeWord(current.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
753
|
+
});
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
extendsReport.errors.push({
|
|
757
|
+
location,
|
|
758
|
+
message: `The ${getNodeWord(symbol.kind)} of '${name}' conflicts with \nits ${getNodeWord(current.kind)} at ${existing}.`,
|
|
759
|
+
});
|
|
760
|
+
});
|
|
640
761
|
const exported: Symbols = new Map();
|
|
762
|
+
const facts = new Set<string>();
|
|
641
763
|
const modules = new Map<string, LinkedModule>();
|
|
642
764
|
const extended: LinkedModule[] = [];
|
|
643
765
|
const constants = [...parsed.constants];
|
|
644
766
|
const variables = parsed.variables.map((variable) => variable.name);
|
|
645
767
|
const variableLocations = parsed.variables.map((variable) => variable.location);
|
|
646
|
-
const addSymbols = (symbols: Symbols, isExported: boolean) =>
|
|
647
|
-
symbols.forEach((symbol, name) => {
|
|
648
|
-
visible.set(name, symbol);
|
|
649
|
-
if (isExported) exported.set(name, symbol);
|
|
650
|
-
});
|
|
651
768
|
parsed.extends.forEach((name) => {
|
|
652
769
|
const nested = scope.modules.get(name);
|
|
653
770
|
const standardSymbols = nested ? undefined : getStandardSymbols(name);
|
|
654
771
|
if (standardSymbols) {
|
|
655
|
-
|
|
772
|
+
mergeExtended(standardSymbols);
|
|
656
773
|
return;
|
|
657
774
|
}
|
|
658
775
|
const linked = nested ?? getLinkedModule(name, parsed.name);
|
|
@@ -666,61 +783,114 @@ export const createEnvironment = (
|
|
|
666
783
|
variables.push(variable);
|
|
667
784
|
variableLocations.push(linked.variableLocations[index]);
|
|
668
785
|
});
|
|
669
|
-
|
|
786
|
+
mergeExtended(linked.exported);
|
|
787
|
+
linked.facts.forEach((name) => facts.add(name));
|
|
670
788
|
linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
|
|
671
789
|
extended.push(linked);
|
|
672
790
|
});
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
791
|
+
const bound = new Map(visible);
|
|
792
|
+
const declarations = [
|
|
793
|
+
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
794
|
+
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
795
|
+
];
|
|
796
|
+
const getDeclarationSymbol = (declaration: (typeof declarations)[number]): ModuleSymbol => ({
|
|
797
|
+
key: declaration.name,
|
|
798
|
+
arity: declaration.arity,
|
|
799
|
+
kind: declaration.kind,
|
|
800
|
+
range: declaration.location.range,
|
|
801
|
+
});
|
|
802
|
+
declarations.forEach((declaration) =>
|
|
803
|
+
exported.set(declaration.name, getDeclarationSymbol(declaration)),
|
|
682
804
|
);
|
|
805
|
+
const getDefinitionSymbol = (definition: Definition): ModuleSymbol => ({
|
|
806
|
+
key: getKey(definition),
|
|
807
|
+
arity: getArity(definition),
|
|
808
|
+
params: getParamArities(definition),
|
|
809
|
+
kind: isTheoremDefinition(parsed, definition) ? "Theorem" : "Definition",
|
|
810
|
+
range: definition.location.range,
|
|
811
|
+
});
|
|
683
812
|
const isParameterFree = constants.length === 0 && variables.length === 0;
|
|
684
813
|
const getKey = (definition: Definition) =>
|
|
685
814
|
parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
|
|
686
|
-
const
|
|
687
|
-
const
|
|
815
|
+
const instanced = new Map<Instance, Symbols>();
|
|
816
|
+
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
817
|
+
const isInstancedAfterDefinition = (nested: Module) => {
|
|
818
|
+
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
819
|
+
return first !== undefined && isBefore(nested.location, first.location);
|
|
820
|
+
};
|
|
821
|
+
const getLetModule = (instance: Instance) => {
|
|
822
|
+
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
823
|
+
if (!own) return getVisibleModules().get(instance.module);
|
|
824
|
+
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
825
|
+
};
|
|
826
|
+
const getUnit = (module: Module): ModuleUnit => ({
|
|
688
827
|
imported: visible,
|
|
689
|
-
|
|
690
|
-
|
|
828
|
+
getInstance: (instance) => {
|
|
829
|
+
const nested = getLetModule(instance);
|
|
830
|
+
const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
|
|
831
|
+
if (standardSymbols) {
|
|
832
|
+
return {
|
|
833
|
+
declarations: [],
|
|
834
|
+
symbols: getStandardInstanceSymbols(instance, standardSymbols),
|
|
835
|
+
instancer: parsed.name,
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
const linked = nested ?? getLinkedModule(instance.module, parsed.name);
|
|
839
|
+
const declarations = getDeclarations(linked);
|
|
840
|
+
return {
|
|
841
|
+
declarations,
|
|
842
|
+
symbols: getInstanceSymbols(instance, linked.exported, declarations, (key) => key),
|
|
843
|
+
instancer: parsed.name,
|
|
844
|
+
};
|
|
845
|
+
},
|
|
846
|
+
bound,
|
|
847
|
+
// SANY keeps the first of two definitions of a name.
|
|
848
|
+
definitions: new Map(
|
|
849
|
+
module.definitions.toReversed().map((definition) => [definition.name, definition]),
|
|
850
|
+
),
|
|
691
851
|
getKey,
|
|
852
|
+
instanced,
|
|
853
|
+
facts,
|
|
854
|
+
reports,
|
|
855
|
+
warnings: scope.warnings,
|
|
692
856
|
});
|
|
693
|
-
const getVisibleModules = () => new Map([...scope.modules, ...modules]);
|
|
694
857
|
const getSymbolsBefore = (location: SourceLocation): Symbols =>
|
|
695
858
|
new Map([
|
|
696
859
|
...visible,
|
|
697
|
-
...
|
|
698
|
-
...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
|
|
699
|
-
...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
|
|
700
|
-
]
|
|
860
|
+
...declarations
|
|
701
861
|
.filter((declaration) => isBefore(declaration.location, location))
|
|
702
862
|
.map((declaration): [string, ModuleSymbol] => [
|
|
703
863
|
declaration.name,
|
|
704
|
-
|
|
864
|
+
getDeclarationSymbol(declaration),
|
|
705
865
|
]),
|
|
706
866
|
...parsed.definitions
|
|
707
867
|
.filter((definition) => isBefore(definition.location, location))
|
|
708
868
|
.map((definition): [string, ModuleSymbol] => [
|
|
709
869
|
definition.name,
|
|
710
|
-
|
|
870
|
+
getDefinitionSymbol(definition),
|
|
711
871
|
]),
|
|
712
872
|
]);
|
|
873
|
+
// Generator.generateModule reports the errors of a nested module and goes on, and then
|
|
874
|
+
// SymbolTable.addModule reports a module already defined.
|
|
713
875
|
const linkNested = (nested: Module) => {
|
|
714
876
|
const visibleModules = getVisibleModules();
|
|
715
|
-
|
|
877
|
+
const existing = visibleModules.get(nested.name);
|
|
878
|
+
const redefinition = existing && getModuleRedefinition(nested, existing.module);
|
|
879
|
+
if (existing && !redefinition) throw createModuleRedefinedError(nested);
|
|
716
880
|
scope.nested.push(nested);
|
|
881
|
+
const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
|
|
882
|
+
reports.push(report);
|
|
717
883
|
const linked = linkModule(nested, {
|
|
718
884
|
id: `${scope.id}/${nested.name}`,
|
|
719
885
|
symbols: getSymbolsBefore(nested.location),
|
|
720
886
|
modules: visibleModules,
|
|
721
887
|
nested: scope.nested,
|
|
888
|
+
errors: report.errors,
|
|
889
|
+
warnings: report.warnings,
|
|
890
|
+
levelChecks: scope.levelChecks,
|
|
722
891
|
});
|
|
723
|
-
|
|
892
|
+
if (redefinition) report.errors.push(redefinition);
|
|
893
|
+
if (!existing) modules.set(nested.name, linked);
|
|
724
894
|
if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
|
|
725
895
|
};
|
|
726
896
|
[
|
|
@@ -734,10 +904,12 @@ export const createEnvironment = (
|
|
|
734
904
|
isParameterFree,
|
|
735
905
|
getVisibleModules().get(instance.module),
|
|
736
906
|
);
|
|
737
|
-
|
|
738
|
-
|
|
907
|
+
instanced.set(instance, symbols);
|
|
908
|
+
symbols.forEach((symbol, name) => {
|
|
909
|
+
if (visible.has(name)) return;
|
|
910
|
+
visible.set(name, symbol);
|
|
911
|
+
if (!instance.isLocal) exported.set(name, symbol);
|
|
739
912
|
});
|
|
740
|
-
addSymbols(symbols, !instance.isLocal);
|
|
741
913
|
},
|
|
742
914
|
})),
|
|
743
915
|
...parsed.modules.map((nested) => ({
|
|
@@ -747,28 +919,18 @@ export const createEnvironment = (
|
|
|
747
919
|
]
|
|
748
920
|
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
749
921
|
.forEach((unit) => unit.link());
|
|
750
|
-
checkRedefinitions(parsed, getUnit(parsed));
|
|
751
|
-
const isInstancedAfterDefinition = (nested: Module) => {
|
|
752
|
-
const first = parsed.instances.find((instance) => instance.module === nested.name);
|
|
753
|
-
return first !== undefined && isBefore(nested.location, first.location);
|
|
754
|
-
};
|
|
755
|
-
const getLetModule = (instance: Instance) => {
|
|
756
|
-
const own = parsed.modules.find((nested) => nested.name === instance.module);
|
|
757
|
-
if (!own) return getVisibleModules().get(instance.module);
|
|
758
|
-
return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
|
|
759
|
-
};
|
|
760
922
|
const module = resolveModuleSubexpressions(
|
|
761
923
|
withLetInstances(parsed, (instance, lets) =>
|
|
762
924
|
instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
|
|
763
925
|
),
|
|
764
926
|
getAvailableDefinitions(visible),
|
|
765
927
|
);
|
|
766
|
-
resolveModule(module, getUnit(module));
|
|
928
|
+
scope.errors.push(...resolveModule(module, getUnit(module)));
|
|
767
929
|
module.definitions.forEach((definition) => {
|
|
768
930
|
const key = getKey(definition);
|
|
769
931
|
definitions.set(key, definition);
|
|
770
932
|
parameterized.set(key, !isParameterFree);
|
|
771
|
-
const symbol =
|
|
933
|
+
const symbol = getDefinitionSymbol(definition);
|
|
772
934
|
visible.set(definition.name, symbol);
|
|
773
935
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
774
936
|
});
|
|
@@ -781,27 +943,36 @@ export const createEnvironment = (
|
|
|
781
943
|
exported,
|
|
782
944
|
modules,
|
|
783
945
|
extended,
|
|
946
|
+
facts: new Set([
|
|
947
|
+
...facts,
|
|
948
|
+
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
949
|
+
]),
|
|
784
950
|
};
|
|
785
951
|
if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
|
|
786
952
|
// SANY checks the instances of a module with its definitions and theorems, so it reports their
|
|
787
953
|
// errors together.
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
{
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
954
|
+
const checks = instanceChecks
|
|
955
|
+
.splice(firstCheck)
|
|
956
|
+
.map((check) => ({ location: check.location, check: () => checkInstance(check) }));
|
|
957
|
+
scope.levelChecks.push(() =>
|
|
958
|
+
checkModuleLevels(
|
|
959
|
+
module,
|
|
960
|
+
{
|
|
961
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
962
|
+
variables: new Set(variables),
|
|
963
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
964
|
+
getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
|
|
965
|
+
},
|
|
966
|
+
checks,
|
|
967
|
+
warnings,
|
|
968
|
+
),
|
|
799
969
|
);
|
|
800
970
|
linkedList.push(linked);
|
|
801
971
|
return linked;
|
|
802
972
|
};
|
|
803
973
|
|
|
804
974
|
const linkedRoot = linkTopLevel(root).linked;
|
|
975
|
+
if (warnings.length > 0) options.print(formatSanyWarnings(warnings));
|
|
805
976
|
const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
|
|
806
977
|
linkedRoot.visible.forEach((symbol, name) => {
|
|
807
978
|
const definition = definitions.get(symbol.key);
|
package/src/errors.ts
CHANGED
|
@@ -34,10 +34,12 @@ export interface SanyError {
|
|
|
34
34
|
message: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// The errors SANY reports for a spec. A fatal error stops it
|
|
38
|
-
// parser prints its message for a module it rejects before the
|
|
37
|
+
// The errors SANY reports for a spec, and the warnings it prints before them. A fatal error stops it
|
|
38
|
+
// before semantic analysis. SANY's parser prints its message for a module it rejects before the
|
|
39
|
+
// report.
|
|
39
40
|
export interface SanyReport {
|
|
40
41
|
errors: SanyError[];
|
|
42
|
+
warnings: SanyError[];
|
|
41
43
|
isFatal: boolean;
|
|
42
44
|
parseError?: string;
|
|
43
45
|
}
|
|
@@ -54,14 +56,23 @@ export class TlaSyntaxError extends TlaError {
|
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
export const createSanyError = (
|
|
59
|
+
export const createSanyError = (
|
|
60
|
+
errors: SanyError[],
|
|
61
|
+
isFatal = false,
|
|
62
|
+
parseError?: string,
|
|
63
|
+
warnings: SanyError[] = [],
|
|
64
|
+
) =>
|
|
58
65
|
new TlaSyntaxError(
|
|
59
66
|
errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
|
|
60
67
|
undefined,
|
|
61
68
|
undefined,
|
|
62
|
-
{ errors, isFatal, parseError },
|
|
69
|
+
{ errors, warnings, isFatal, parseError },
|
|
63
70
|
);
|
|
64
71
|
|
|
72
|
+
// How SANY prints the warnings it reports, which do not stop TLC.
|
|
73
|
+
export const formatSanyWarnings = (warnings: SanyError[]) =>
|
|
74
|
+
`*** Warnings: ${warnings.length}\n\n${warnings.map((warning) => `${warning.location}\n\n${warning.message}\n\n\n`).join("\n")}`;
|
|
75
|
+
|
|
65
76
|
// SANY's message for a module its parser rejects, such as a "***Parse Error***" with the
|
|
66
77
|
// productions it was in.
|
|
67
78
|
export class TlaParseError extends TlaError {
|
package/src/instances.ts
CHANGED
|
@@ -705,7 +705,7 @@ const expandLetInstances = (
|
|
|
705
705
|
const index = position === -1 ? current.length : position;
|
|
706
706
|
return [...current.slice(0, index), ...instantiate(instance, inner), ...current.slice(index)];
|
|
707
707
|
}, mapped.definitions);
|
|
708
|
-
return {
|
|
708
|
+
return { ...mapped, definitions };
|
|
709
709
|
};
|
|
710
710
|
|
|
711
711
|
// A named theorem's statement is linked as its definition.
|
package/src/levels.ts
CHANGED
|
@@ -885,6 +885,7 @@ export const checkModuleLevels = (
|
|
|
885
885
|
module: Module,
|
|
886
886
|
context: LevelContext,
|
|
887
887
|
instances: { location: SourceLocation; check: () => void }[],
|
|
888
|
+
warnings: SanyError[],
|
|
888
889
|
) => {
|
|
889
890
|
const analyzer = createLevelAnalyzer(context);
|
|
890
891
|
const getLevel = (expressions: Expression[]) =>
|
|
@@ -956,31 +957,35 @@ export const checkModuleLevels = (
|
|
|
956
957
|
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
957
958
|
];
|
|
958
959
|
});
|
|
959
|
-
collectSemanticErrors(
|
|
960
|
-
|
|
961
|
-
.
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
960
|
+
const errors = collectSemanticErrors(
|
|
961
|
+
[
|
|
962
|
+
...module.definitions
|
|
963
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
964
|
+
.map((definition) => ({
|
|
965
|
+
location: definition.location,
|
|
966
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
967
|
+
})),
|
|
968
|
+
...module.assumptions.map((assumption) => ({
|
|
969
|
+
location: assumption.location,
|
|
970
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
971
|
+
})),
|
|
972
|
+
...module.theorems.map((theorem) => ({
|
|
973
|
+
location: theorem.location,
|
|
974
|
+
check: () => {
|
|
975
|
+
const data =
|
|
976
|
+
theorem.statement.kind === "AssumeProve"
|
|
977
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
978
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
979
|
+
const errors = [
|
|
980
|
+
...checkProof(theorem.proof),
|
|
981
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
982
|
+
];
|
|
983
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
984
|
+
},
|
|
965
985
|
})),
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
location: theorem.location,
|
|
972
|
-
check: () => {
|
|
973
|
-
const data =
|
|
974
|
-
theorem.statement.kind === "AssumeProve"
|
|
975
|
-
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
976
|
-
: analyzer.analyze(theorem.statement, new Map());
|
|
977
|
-
const errors = [
|
|
978
|
-
...checkProof(theorem.proof),
|
|
979
|
-
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
980
|
-
];
|
|
981
|
-
if (errors.length > 0) throw createSanyError(errors);
|
|
982
|
-
},
|
|
983
|
-
})),
|
|
984
|
-
...instances,
|
|
985
|
-
]);
|
|
986
|
+
...instances,
|
|
987
|
+
],
|
|
988
|
+
warnings,
|
|
989
|
+
);
|
|
990
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
986
991
|
};
|