tla-ts 0.0.3-dev.52c5b61 → 0.0.3-dev.596cd91

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