tla-ts 0.0.3-dev.c949d82 → 0.0.3-dev.ccbff8c

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.
Files changed (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +167 -36
  5. package/dist/index.d.ts +167 -36
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -21
  12. package/dist/run-cli.js +1351 -21
  13. package/dist/transfer.cjs +83 -34
  14. package/dist/transfer.js +83 -34
  15. package/package.json +5 -1
  16. package/src/ast.ts +55 -7
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1344 -285
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +225 -206
  25. package/src/environment.ts +423 -127
  26. package/src/errors.ts +168 -9
  27. package/src/evaluate.ts +722 -146
  28. package/src/exit-codes.ts +6 -24
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +204 -148
  35. package/src/liveness.ts +190 -117
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +6 -1
  39. package/src/parallel.ts +57 -6
  40. package/src/parser.ts +412 -139
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1405 -416
  48. package/src/run-cli.ts +1167 -174
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +25 -5
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +158 -120
  69. package/src/symmetry.ts +168 -50
  70. package/src/tableau.ts +3 -1
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +49 -21
  74. package/src/tlc-modules.ts +0 -4
  75. package/src/tlc-order.ts +55 -9
  76. package/src/tlc-random.ts +28 -3
  77. package/src/tlc-replay.ts +655 -0
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +683 -256
@@ -14,10 +14,13 @@ import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
15
  import {
16
16
  createSanyError,
17
+ createMissingModuleError,
18
+ formatRange,
19
+ formatSanyWarnings,
17
20
  TlaConfigError,
18
21
  TlaEvaluationError,
19
- TlaModuleError,
20
22
  TlaSyntaxError,
23
+ type SanyError,
21
24
  } from "./errors";
22
25
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
23
26
  import {
@@ -35,25 +38,39 @@ import {
35
38
  renameLocals,
36
39
  withLetInstances,
37
40
  } from "./instances";
38
- import { overrideDefinitions } from "./java-overrides";
41
+ import { overrideDefinitions, type JavaOverride } 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
- import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
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";
53
69
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
54
70
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
55
- import { checkInstances, type InstanceCheck } from "./substitution-levels";
56
- import { getTlcDeclarationOrder } from "./tlc-order";
71
+ import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
72
+ import { isEvalException } from "./tlc-messages";
73
+ import { createTlcDefinitionOrder, getTlcDeclarationOrder } from "./tlc-order";
57
74
  import {
58
75
  checkInteger,
59
76
  createModelValue,
@@ -61,7 +78,9 @@ import {
61
78
  formatValue,
62
79
  valuesEqual,
63
80
  type Value,
81
+ markNormalized,
64
82
  } from "./values";
83
+ import type { TlcVersion } from "./tlc-version";
65
84
 
66
85
  interface EnvironmentOptions {
67
86
  source: string;
@@ -70,6 +89,15 @@ interface EnvironmentOptions {
70
89
  print: (text: string) => void;
71
90
  files: FileSystem;
72
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;
73
101
  }
74
102
 
75
103
  interface Specification {
@@ -77,6 +105,12 @@ interface Specification {
77
105
  assumptions: Assumption[];
78
106
  variableLocations: SourceLocation[];
79
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[];
80
114
  }
81
115
 
82
116
  export interface InstanceAction {
@@ -112,25 +146,73 @@ interface LinkedModule {
112
146
  exported: Symbols;
113
147
  modules: Map<string, LinkedModule>;
114
148
  extended: LinkedModule[];
149
+ facts: Set<string>;
115
150
  }
116
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.
117
154
  interface ModuleScope {
118
155
  id: string;
119
156
  symbols: Symbols;
120
157
  modules: Map<string, LinkedModule>;
121
158
  nested: Module[];
159
+ errors: SanyError[];
160
+ warnings: SanyError[];
161
+ levelChecks: (() => void)[];
122
162
  }
123
163
 
124
- const getDeclarations = (linked: LinkedModule) => [
164
+ const getDeclarations = (linked: LinkedModule): Declaration[] => [
125
165
  ...linked.constants,
126
- ...linked.variables.map((variable) => ({ name: variable, arity: 0 })),
166
+ ...linked.variables.map((variable, index) => ({
167
+ name: variable,
168
+ arity: 0,
169
+ location: linked.variableLocations[index],
170
+ })),
127
171
  ];
128
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
+
129
195
  const createModuleRedefinedError = (module: Module) =>
130
196
  new TlaSyntaxError(
131
197
  `Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
132
198
  );
133
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
+
134
216
  const createLocalFunction = (definition: Definition, key: string): Definition => ({
135
217
  kind: "Operator",
136
218
  name: key,
@@ -222,13 +304,19 @@ export const createEnvironment = (
222
304
  const parameterized = new Map<string, boolean>();
223
305
  // The definition each instance copy came from, and the instance parameters the copy adds.
224
306
  const copySources = new Map<string, { source: string; prefix: number }>();
307
+ const substitutedDefinitions = new Set<Definition>();
225
308
  const builtins = new Map<string, Builtin>(
226
309
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
227
310
  );
228
311
  const standardExports = new Map<string, Symbols>();
229
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";
230
316
  const linkedModules = new Map<string, LinkedModule>();
231
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>();
232
320
  const linkedList: LinkedModule[] = [];
233
321
  const dependencies = new Map<string, Set<string>>();
234
322
  const loading: Set<string>[] = [];
@@ -243,13 +331,35 @@ export const createEnvironment = (
243
331
  const copyCount = { value: 0 };
244
332
  const createName = (name: string) => `${name}#${copyCount.value++}`;
245
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
+ });
246
351
  const instanceActions = new Map<string, InstanceAction>();
352
+ const instanceNames = new Map<string, string>();
353
+ const qualifiedNames = new Map<string, string>();
247
354
 
248
355
  const addStandardModule = (name: string): Symbols | undefined => {
249
356
  const cached = standardExports.get(name);
250
357
  if (cached) return cached;
251
- const standardModule = getStandardModule(name, options.print, runtime, options.files);
358
+ const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
252
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);
253
363
  const exported: Symbols = new Map();
254
364
  standardModule.extends.forEach((extended) =>
255
365
  addStandardModule(extended)?.forEach((symbol, symbolName) =>
@@ -258,7 +368,13 @@ export const createEnvironment = (
258
368
  );
259
369
  standardModule.operators.forEach((operator) => {
260
370
  builtins.set(operator.name, operator);
261
- exported.set(operator.name, { key: operator.name, arity: operator.arity });
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
+ });
262
378
  });
263
379
  standardExports.set(name, exported);
264
380
  return exported;
@@ -266,7 +382,11 @@ export const createEnvironment = (
266
382
 
267
383
  const getStandardSymbols = (name: string) => {
268
384
  const symbols = addStandardModule(name);
269
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
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");
270
390
  return symbols;
271
391
  };
272
392
 
@@ -294,17 +414,8 @@ export const createEnvironment = (
294
414
  options.loadModule?.(name) ??
295
415
  BUNDLED_MODULES.get(name) ??
296
416
  extractModule(options.source, name);
297
- if (source === undefined)
298
- throw createSanyError(
299
- [
300
- {
301
- location: `In module ${importer}`,
302
- message: `Cannot find source file for module ${name} imported in module ${importer}.`,
303
- },
304
- ],
305
- true,
306
- );
307
- const module = parseModule(source);
417
+ if (source === undefined) throw createMissingModuleError(name, importer);
418
+ const module = parseModuleFile(source, name);
308
419
  if (module.name !== name) {
309
420
  throw createSanyError(
310
421
  [
@@ -329,6 +440,8 @@ export const createEnvironment = (
329
440
  }
330
441
  const closure = new Set<string>();
331
442
  const nested: Module[] = [];
443
+ const errors: SanyError[] = [];
444
+ const levelChecks: (() => void)[] = [];
332
445
  linking.push(parsed.name);
333
446
  loading.push(closure);
334
447
  const linked = linkModule(parsed, {
@@ -336,11 +449,19 @@ export const createEnvironment = (
336
449
  symbols: new Map(),
337
450
  modules: new Map(),
338
451
  nested,
452
+ errors,
453
+ warnings,
454
+ levelChecks,
339
455
  });
340
456
  loading.pop();
341
457
  linking.pop();
458
+ if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
342
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);
343
463
  if (duplicate) throw createModuleRedefinedError(duplicate);
464
+ levelChecks.forEach((check) => check());
344
465
  return { linked, closure };
345
466
  };
346
467
 
@@ -360,23 +481,16 @@ export const createEnvironment = (
360
481
  const instantiate = (
361
482
  instance: Instance,
362
483
  module: Module,
363
- unit: {
364
- imported: Symbols;
365
- importedAt: Map<string, SourceLocation>;
366
- definitions: Map<string, Definition>;
367
- getKey: (definition: Definition) => string;
368
- },
484
+ unit: ModuleUnit,
369
485
  isParameterFree: boolean,
370
486
  nested: LinkedModule | undefined,
371
487
  ): Symbols => {
372
488
  const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
373
- if (standardSymbols) {
374
- if (instance.name === undefined) return standardSymbols;
375
- return new Map(
376
- [...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
377
- );
378
- }
489
+ if (standardSymbols) return getStandardInstanceSymbols(instance, standardSymbols);
379
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
+ );
380
494
  const declarations = getDeclarations(linked);
381
495
  const suffix = `#${copyCount.value++}`;
382
496
  const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
@@ -403,34 +517,81 @@ export const createEnvironment = (
403
517
  isParameterized: (key) => parameterized.get(key) === true,
404
518
  addDefinition: (source, name, create) => {
405
519
  const key = createName(`${name}@${module.name}`);
406
- definitions.set(key, create(key));
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
+ }
407
526
  parameterized.set(key, !isParameterFree);
408
527
  copySources.set(key, { source, prefix: instance.params.length });
409
528
  return key;
410
529
  },
411
530
  createName,
412
531
  });
413
- const declared = new Set(declarations.map((declaration) => declaration.name));
414
- const symbols: Symbols = new Map();
415
- linked.exported.forEach((symbol, name) => {
416
- if (declared.has(name)) return;
417
- if (instance.name === undefined) {
418
- const key = instantiation.getKey(symbol.key);
419
- if (declarations.length > 0)
420
- instanceActions.set(key, { name, location: instance.location });
421
- symbols.set(name, { key, arity: symbol.arity });
422
- return;
423
- }
424
- const key = instantiation.getQualifiedKey(symbol.key);
425
- if (declarations.length > 0) {
426
- instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
427
- }
428
- symbols.set(`${instance.name}!${name}`, {
429
- key,
430
- arity: symbol.arity + instance.params.length,
431
- });
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;
432
540
  });
433
- return symbols;
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
+ );
434
595
  };
435
596
 
436
597
  const getLocalArguments = (params: OperatorParam[], location: SourceLocation) =>
@@ -603,31 +764,70 @@ export const createEnvironment = (
603
764
  return definition ? [{ ...definition, name }] : [];
604
765
  });
605
766
 
767
+ const javaOverrides = new Map<string, JavaOverride[]>();
606
768
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
607
- const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
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;
608
777
  const visible: Symbols = new Map([
609
778
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
610
779
  operator.name,
611
- { key: operator.name, arity: operator.arity },
780
+ { key: operator.name, arity: operator.arity, kind: "Builtin" },
612
781
  ]),
613
782
  ...scope.symbols,
614
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
+ });
615
819
  const exported: Symbols = new Map();
820
+ const facts = new Set<string>();
616
821
  const modules = new Map<string, LinkedModule>();
617
822
  const extended: LinkedModule[] = [];
618
823
  const constants = [...parsed.constants];
619
824
  const variables = parsed.variables.map((variable) => variable.name);
620
825
  const variableLocations = parsed.variables.map((variable) => variable.location);
621
- const addSymbols = (symbols: Symbols, isExported: boolean) =>
622
- symbols.forEach((symbol, name) => {
623
- visible.set(name, symbol);
624
- if (isExported) exported.set(name, symbol);
625
- });
626
826
  parsed.extends.forEach((name) => {
627
827
  const nested = scope.modules.get(name);
628
828
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
629
829
  if (standardSymbols) {
630
- addSymbols(standardSymbols, true);
830
+ mergeExtended(standardSymbols);
631
831
  return;
632
832
  }
633
833
  const linked = nested ?? getLinkedModule(name, parsed.name);
@@ -641,61 +841,117 @@ export const createEnvironment = (
641
841
  variables.push(variable);
642
842
  variableLocations.push(linked.variableLocations[index]);
643
843
  });
644
- addSymbols(linked.exported, true);
844
+ mergeExtended(linked.exported);
845
+ linked.facts.forEach((name) => facts.add(name));
645
846
  linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
646
847
  extended.push(linked);
647
848
  });
648
- parsed.constants.forEach((constant) =>
649
- exported.set(constant.name, {
650
- key: constant.name,
651
- arity: constant.arity,
652
- declaration: "Constant",
653
- }),
654
- );
655
- parsed.variables.forEach((variable) =>
656
- exported.set(variable.name, { key: variable.name, arity: 0, declaration: "Variable" }),
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)),
657
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
+ });
658
870
  const isParameterFree = constants.length === 0 && variables.length === 0;
659
871
  const getKey = (definition: Definition) =>
660
872
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
661
- const importedAt = new Map<string, SourceLocation>();
662
- const getUnit = (module: Module) => ({
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 => ({
663
885
  imported: visible,
664
- importedAt,
665
- definitions: new Map(module.definitions.map((definition) => [definition.name, definition])),
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
+ ),
666
909
  getKey,
910
+ instanced,
911
+ facts,
912
+ reports,
913
+ warnings: scope.warnings,
667
914
  });
668
- const getVisibleModules = () => new Map([...scope.modules, ...modules]);
915
+ // SANY keeps the first of two symbols of a name.
669
916
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
670
- new Map([
671
- ...visible,
672
- ...[
673
- ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
674
- ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
675
- ]
917
+ [
918
+ ...declarations
676
919
  .filter((declaration) => isBefore(declaration.location, location))
677
920
  .map((declaration): [string, ModuleSymbol] => [
678
921
  declaration.name,
679
- { key: declaration.name, arity: declaration.arity, declaration: declaration.kind },
922
+ getDeclarationSymbol(declaration),
680
923
  ]),
681
924
  ...parsed.definitions
682
925
  .filter((definition) => isBefore(definition.location, location))
683
926
  .map((definition): [string, ModuleSymbol] => [
684
927
  definition.name,
685
- { key: getKey(definition), arity: getArity(definition) },
928
+ getDefinitionSymbol(definition),
686
929
  ]),
687
- ]);
930
+ ].reduce(
931
+ (symbols, [name, symbol]) => (symbols.has(name) ? symbols : symbols.set(name, symbol)),
932
+ new Map(visible),
933
+ );
934
+ // Generator.generateModule reports the errors of a nested module and goes on, and then
935
+ // SymbolTable.addModule reports a module already defined.
688
936
  const linkNested = (nested: Module) => {
689
937
  const visibleModules = getVisibleModules();
690
- if (visibleModules.has(nested.name)) throw createModuleRedefinedError(nested);
938
+ const existing = visibleModules.get(nested.name);
939
+ const redefinition = existing && getModuleRedefinition(nested, existing.module);
940
+ if (existing && !redefinition) throw createModuleRedefinedError(nested);
691
941
  scope.nested.push(nested);
942
+ const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
943
+ reports.push(report);
692
944
  const linked = linkModule(nested, {
693
945
  id: `${scope.id}/${nested.name}`,
694
946
  symbols: getSymbolsBefore(nested.location),
695
947
  modules: visibleModules,
696
948
  nested: scope.nested,
949
+ errors: report.errors,
950
+ warnings: report.warnings,
951
+ levelChecks: scope.levelChecks,
697
952
  });
698
- modules.set(nested.name, linked);
953
+ if (redefinition) report.errors.push(redefinition);
954
+ if (!existing) modules.set(nested.name, linked);
699
955
  if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
700
956
  };
701
957
  [
@@ -709,10 +965,13 @@ export const createEnvironment = (
709
965
  isParameterFree,
710
966
  getVisibleModules().get(instance.module),
711
967
  );
712
- symbols.forEach((_, name) => {
713
- if (!visible.has(name)) importedAt.set(name, instance.location);
968
+ instanced.set(instance, symbols);
969
+ symbols.forEach((symbol, name) => {
970
+ if (visible.has(name)) return;
971
+ importedAt.set(name, instance.location);
972
+ visible.set(name, symbol);
973
+ if (!instance.isLocal) exported.set(name, symbol);
714
974
  });
715
- addSymbols(symbols, !instance.isLocal);
716
975
  },
717
976
  })),
718
977
  ...parsed.modules.map((nested) => ({
@@ -722,36 +981,30 @@ export const createEnvironment = (
722
981
  ]
723
982
  .sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
724
983
  .forEach((unit) => unit.link());
725
- checkRedefinitions(parsed, getUnit(parsed));
726
- const isInstancedAfterDefinition = (nested: Module) => {
727
- const first = parsed.instances.find((instance) => instance.module === nested.name);
728
- return first !== undefined && isBefore(nested.location, first.location);
729
- };
730
- const getLetModule = (instance: Instance) => {
731
- const own = parsed.modules.find((nested) => nested.name === instance.module);
732
- if (!own) return getVisibleModules().get(instance.module);
733
- return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
734
- };
735
984
  const module = resolveModuleSubexpressions(
736
985
  withLetInstances(parsed, (instance, lets) =>
737
986
  instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
738
987
  ),
739
988
  getAvailableDefinitions(visible),
740
989
  );
741
- resolveModule(module, getUnit(module));
990
+ scope.errors.push(...resolveModule(module, getUnit(module)));
742
991
  module.definitions.forEach((definition) => {
743
992
  const key = getKey(definition);
744
993
  definitions.set(key, definition);
745
994
  parameterized.set(key, !isParameterFree);
746
- const symbol = { key, arity: getArity(definition) };
995
+ const symbol = getDefinitionSymbol(definition);
996
+ const importedLocation = importedAt.get(definition.name);
997
+ // SANY binds the name of a fact to a fact the module imports before it.
998
+ if (
999
+ symbol.kind === "Theorem" &&
1000
+ visible.get(definition.name)?.kind === "Theorem" &&
1001
+ (importedLocation === undefined || isBefore(importedLocation, definition.location))
1002
+ ) {
1003
+ return;
1004
+ }
747
1005
  visible.set(definition.name, symbol);
748
1006
  if (!definition.isLocal) exported.set(definition.name, symbol);
749
1007
  });
750
- checkModuleLevels(module, {
751
- constants: new Set(constants.map((constant) => constant.name)),
752
- variables: new Set(variables),
753
- getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
754
- });
755
1008
  const linked = {
756
1009
  module,
757
1010
  constants,
@@ -761,12 +1014,36 @@ export const createEnvironment = (
761
1014
  exported,
762
1015
  modules,
763
1016
  extended,
1017
+ facts: new Set([
1018
+ ...facts,
1019
+ ...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
1020
+ ]),
764
1021
  };
1022
+ if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
1023
+ // SANY checks the instances of a module with its definitions and theorems, so it reports their
1024
+ // errors together.
1025
+ const checks = instanceChecks
1026
+ .splice(firstCheck)
1027
+ .map((check) => ({ location: check.location, check: () => checkInstance(check) }));
1028
+ scope.levelChecks.push(() =>
1029
+ checkModuleLevels(
1030
+ module,
1031
+ {
1032
+ constants: new Set(constants.map((constant) => constant.name)),
1033
+ variables: new Set(variables),
1034
+ getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
1035
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
1036
+ },
1037
+ checks,
1038
+ warnings,
1039
+ ),
1040
+ );
765
1041
  linkedList.push(linked);
766
1042
  return linked;
767
1043
  };
768
1044
 
769
1045
  const linkedRoot = linkTopLevel(root).linked;
1046
+ if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
770
1047
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
771
1048
  linkedRoot.visible.forEach((symbol, name) => {
772
1049
  const definition = definitions.get(symbol.key);
@@ -808,6 +1085,7 @@ export const createEnvironment = (
808
1085
  .filter((assignment) => assignment.args === undefined)
809
1086
  .forEach((assignment) => {
810
1087
  const value = toValue(assignment.value);
1088
+ markNormalized(value);
811
1089
  getScopedKeys(assignment).forEach((key) => {
812
1090
  const arity = getKeyArity(key);
813
1091
  if (arity === 0) constants.set(key, value);
@@ -930,7 +1208,7 @@ export const createEnvironment = (
930
1208
  const unassigned = declaredConstants.filter(
931
1209
  (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
932
1210
  );
933
- if (unassigned.length > 0) {
1211
+ if (unassigned.length > 0 && !options.isUnconfigured) {
934
1212
  // TLC reports the first constant it finds unassigned in SANY's order of declarations.
935
1213
  const name =
936
1214
  getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
@@ -941,12 +1219,20 @@ export const createEnvironment = (
941
1219
  );
942
1220
  }
943
1221
 
1222
+ if (options.isTraceRecorded) {
1223
+ builtins.forEach((builtin, name) => {
1224
+ const replacement = getTraceSpecArithmetic(builtin);
1225
+ if (replacement) builtins.set(name, replacement);
1226
+ });
1227
+ }
1228
+
944
1229
  const environment: Environment = {
945
1230
  runtime,
946
1231
  definitions,
947
1232
  builtins,
948
1233
  constants,
949
1234
  substitutions,
1235
+ instanceNames,
950
1236
  variables,
951
1237
  variableIndex: getVariableIndex(
952
1238
  variables,
@@ -954,22 +1240,9 @@ export const createEnvironment = (
954
1240
  ),
955
1241
  definitionValues: new Map(),
956
1242
  constantLevels: new Map(),
1243
+ substitutedDefinitions,
957
1244
  evaluators: new WeakMap(),
958
1245
  };
959
- checkInstances(instanceChecks, (name) => {
960
- const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
961
- return {
962
- constants: linked.constants,
963
- variables: linked.variables,
964
- definitions: [
965
- ...new Set(
966
- [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
967
- ),
968
- ],
969
- assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
970
- getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
971
- };
972
- });
973
1246
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
974
1247
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
975
1248
  // a minimum level as state-level, so the analyzer takes them for variables.
@@ -999,18 +1272,41 @@ export const createEnvironment = (
999
1272
  analyzer.analyzeDefinition(definition, new Map()).level > 0
1000
1273
  ? getEvaluationError(definition.body)
1001
1274
  : undefined;
1002
- // TLC adds the reason unless a standard module's Java code failed.
1003
1275
  if (error) {
1276
+ // TLC's SpecProcessor adds the message of a TLCRuntimeException, not of an EvalException.
1277
+ const addendum = isEvalException(error) ? "" : ` - specifically: ${error.message}`;
1004
1278
  throw new TlaConfigError(
1005
- `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
1279
+ `The configuration file substitutes constant ${name} with non-constant ${target}${addendum}`,
1006
1280
  );
1007
1281
  }
1008
1282
  });
1009
- runtime.isStarted = true;
1283
+ // TLC 1.7.4 processes the whole spec before it creates its model checker, which TLCGet reads.
1284
+ if (runtime.version !== "1.7.4") runtime.isStarted = true;
1285
+ // TLC computes the constant definitions of each module in the order SANY finishes the modules,
1286
+ // except those of a module that an INSTANCE gives parameters to.
1287
+ const instantiated = new Set(
1288
+ linkedList.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
1289
+ );
1290
+ const getDefinitionOrder = createTlcDefinitionOrder(options.source, options.loadModule);
1010
1291
  return {
1011
1292
  environment,
1012
1293
  assumptions,
1013
1294
  variableLocations: linkedRoot.variableLocations,
1014
1295
  instanceActions,
1296
+ startupDefinitions: linkedList
1297
+ .filter(
1298
+ (linked) => !instantiated.has(linked.module.name) || getDeclarations(linked).length === 0,
1299
+ )
1300
+ .flatMap((linked) =>
1301
+ getDefinitionOrder(linked === linkedRoot ? undefined : linked.module.name).flatMap(
1302
+ (name) => {
1303
+ const definition = definitions.get(linked.visible.get(name)?.key ?? name);
1304
+ return definition ? [definition] : [];
1305
+ },
1306
+ ),
1307
+ ),
1308
+ rootConstants: linkedRoot.constants,
1309
+ rootSymbols: linkedRoot.visible,
1310
+ javaOverrides: linkedList.flatMap((linked) => javaOverrides.get(linked.module.name) ?? []),
1015
1311
  };
1016
1312
  };