tla-ts 0.0.3-dev.51ddd47 → 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 +1393 -226
  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 +473 -132
  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 +16 -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 +46 -18
  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,7 +12,16 @@ import {
12
12
  import { BUNDLED_MODULES } from "./bundled-modules";
13
13
  import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
- import { 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,
@@ -29,24 +38,41 @@ import {
29
38
  renameLocals,
30
39
  withLetInstances,
31
40
  } from "./instances";
32
- import { overrideDefinitions } from "./java-overrides";
41
+ import { overrideDefinitions, type JavaOverride } from "./java-overrides";
33
42
  import { extractModule, parseModule } from "./parser";
34
43
  import {
35
- checkRedefinitions,
36
44
  getArity,
45
+ getParamArities,
37
46
  isBefore,
47
+ isTheoremDefinition,
38
48
  KEY_SEPARATOR,
39
49
  LOCAL_RESOLUTION,
50
+ MODULE_START,
40
51
  resolveModule,
41
52
  resolveSubstitutions,
53
+ type ModuleReport,
42
54
  type ModuleSymbol,
55
+ type ModuleUnit,
56
+ type SymbolKind,
43
57
  type Symbols,
44
58
  } from "./resolve";
45
59
  import { createRuntime } from "./runtime";
46
- import { 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";
47
71
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
48
72
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
49
- import { checkInstances, type InstanceCheck } from "./substitution-levels";
73
+ import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
74
+ import { isEvalException } from "./tlc-messages";
75
+ import { createTlcDefinitionOrder, getTlcDeclarationOrder } from "./tlc-order";
50
76
  import {
51
77
  checkInteger,
52
78
  createModelValue,
@@ -54,7 +80,9 @@ import {
54
80
  formatValue,
55
81
  valuesEqual,
56
82
  type Value,
83
+ markNormalized,
57
84
  } from "./values";
85
+ import type { TlcVersion } from "./tlc-version";
58
86
 
59
87
  interface EnvironmentOptions {
60
88
  source: string;
@@ -63,6 +91,15 @@ interface EnvironmentOptions {
63
91
  print: (text: string) => void;
64
92
  files: FileSystem;
65
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;
66
103
  }
67
104
 
68
105
  interface Specification {
@@ -70,6 +107,12 @@ interface Specification {
70
107
  assumptions: Assumption[];
71
108
  variableLocations: SourceLocation[];
72
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[];
73
116
  }
74
117
 
75
118
  export interface InstanceAction {
@@ -105,25 +148,73 @@ interface LinkedModule {
105
148
  exported: Symbols;
106
149
  modules: Map<string, LinkedModule>;
107
150
  extended: LinkedModule[];
151
+ facts: Set<string>;
108
152
  }
109
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.
110
156
  interface ModuleScope {
111
157
  id: string;
112
158
  symbols: Symbols;
113
159
  modules: Map<string, LinkedModule>;
114
160
  nested: Module[];
161
+ errors: SanyError[];
162
+ warnings: SanyError[];
163
+ levelChecks: (() => void)[];
115
164
  }
116
165
 
117
- const getDeclarations = (linked: LinkedModule) => [
166
+ const getDeclarations = (linked: LinkedModule): Declaration[] => [
118
167
  ...linked.constants,
119
- ...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
+ })),
120
173
  ];
121
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
+
122
197
  const createModuleRedefinedError = (module: Module) =>
123
198
  new TlaSyntaxError(
124
199
  `Multiply-defined module '${module.name}' at line ${module.location.line}, column ${module.location.column}`,
125
200
  );
126
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
+
127
218
  const createLocalFunction = (definition: Definition, key: string): Definition => ({
128
219
  kind: "Operator",
129
220
  name: key,
@@ -215,13 +306,19 @@ export const createEnvironment = (
215
306
  const parameterized = new Map<string, boolean>();
216
307
  // The definition each instance copy came from, and the instance parameters the copy adds.
217
308
  const copySources = new Map<string, { source: string; prefix: number }>();
309
+ const substitutedDefinitions = new Set<Definition>();
218
310
  const builtins = new Map<string, Builtin>(
219
311
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
220
312
  );
221
313
  const standardExports = new Map<string, Symbols>();
222
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";
223
318
  const linkedModules = new Map<string, LinkedModule>();
224
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>();
225
322
  const linkedList: LinkedModule[] = [];
226
323
  const dependencies = new Map<string, Set<string>>();
227
324
  const loading: Set<string>[] = [];
@@ -236,13 +333,35 @@ export const createEnvironment = (
236
333
  const copyCount = { value: 0 };
237
334
  const createName = (name: string) => `${name}#${copyCount.value++}`;
238
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
+ });
239
353
  const instanceActions = new Map<string, InstanceAction>();
354
+ const instanceNames = new Map<string, string>();
355
+ const qualifiedNames = new Map<string, string>();
240
356
 
241
357
  const addStandardModule = (name: string): Symbols | undefined => {
242
358
  const cached = standardExports.get(name);
243
359
  if (cached) return cached;
244
- const standardModule = getStandardModule(name, options.print, runtime, options.files);
360
+ const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
245
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);
246
365
  const exported: Symbols = new Map();
247
366
  standardModule.extends.forEach((extended) =>
248
367
  addStandardModule(extended)?.forEach((symbol, symbolName) =>
@@ -251,15 +370,28 @@ export const createEnvironment = (
251
370
  );
252
371
  standardModule.operators.forEach((operator) => {
253
372
  builtins.set(operator.name, operator);
254
- 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
+ });
255
380
  });
256
381
  standardExports.set(name, exported);
257
382
  return exported;
258
383
  };
259
384
 
260
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;
261
389
  const symbols = addStandardModule(name);
262
- 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");
263
395
  return symbols;
264
396
  };
265
397
 
@@ -287,14 +419,17 @@ export const createEnvironment = (
287
419
  options.loadModule?.(name) ??
288
420
  BUNDLED_MODULES.get(name) ??
289
421
  extractModule(options.source, name);
290
- if (source === undefined)
291
- throw new TlaSyntaxError(
292
- `Cannot find source file for module ${name} imported in module ${importer}.`,
293
- );
294
- const module = parseModule(source);
422
+ if (source === undefined) throw createMissingModuleError(name, importer);
423
+ const module = parseModuleFile(source, name);
295
424
  if (module.name !== name) {
296
- throw new TlaSyntaxError(
297
- `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,
298
433
  );
299
434
  }
300
435
  return rejectModuleLiterals(module);
@@ -310,6 +445,8 @@ export const createEnvironment = (
310
445
  }
311
446
  const closure = new Set<string>();
312
447
  const nested: Module[] = [];
448
+ const errors: SanyError[] = [];
449
+ const levelChecks: (() => void)[] = [];
313
450
  linking.push(parsed.name);
314
451
  loading.push(closure);
315
452
  const linked = linkModule(parsed, {
@@ -317,11 +454,19 @@ export const createEnvironment = (
317
454
  symbols: new Map(),
318
455
  modules: new Map(),
319
456
  nested,
457
+ errors,
458
+ warnings,
459
+ levelChecks,
320
460
  });
321
461
  loading.pop();
322
462
  linking.pop();
463
+ if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
323
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);
324
468
  if (duplicate) throw createModuleRedefinedError(duplicate);
469
+ levelChecks.forEach((check) => check());
325
470
  return { linked, closure };
326
471
  };
327
472
 
@@ -332,6 +477,17 @@ export const createEnvironment = (
332
477
  return cached;
333
478
  }
334
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
+ });
335
491
  linkedModules.set(name, top.linked);
336
492
  dependencies.set(name, top.closure);
337
493
  useModule(name, top.closure);
@@ -341,23 +497,16 @@ export const createEnvironment = (
341
497
  const instantiate = (
342
498
  instance: Instance,
343
499
  module: Module,
344
- unit: {
345
- imported: Symbols;
346
- importedAt: Map<string, SourceLocation>;
347
- definitions: Map<string, Definition>;
348
- getKey: (definition: Definition) => string;
349
- },
500
+ unit: ModuleUnit,
350
501
  isParameterFree: boolean,
351
502
  nested: LinkedModule | undefined,
352
503
  ): Symbols => {
353
504
  const standardSymbols = nested ? undefined : getStandardSymbols(instance.module);
354
- if (standardSymbols) {
355
- if (instance.name === undefined) return standardSymbols;
356
- return new Map(
357
- [...standardSymbols].map(([name, symbol]) => [`${instance.name}!${name}`, symbol]),
358
- );
359
- }
505
+ if (standardSymbols) return getStandardInstanceSymbols(instance, standardSymbols);
360
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
+ );
361
510
  const declarations = getDeclarations(linked);
362
511
  const suffix = `#${copyCount.value++}`;
363
512
  const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
@@ -384,34 +533,81 @@ export const createEnvironment = (
384
533
  isParameterized: (key) => parameterized.get(key) === true,
385
534
  addDefinition: (source, name, create) => {
386
535
  const key = createName(`${name}@${module.name}`);
387
- 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
+ }
388
542
  parameterized.set(key, !isParameterFree);
389
543
  copySources.set(key, { source, prefix: instance.params.length });
390
544
  return key;
391
545
  },
392
546
  createName,
393
547
  });
394
- const declared = new Set(declarations.map((declaration) => declaration.name));
395
- const symbols: Symbols = new Map();
396
- linked.exported.forEach((symbol, name) => {
397
- if (declared.has(name)) return;
398
- if (instance.name === undefined) {
399
- const key = instantiation.getKey(symbol.key);
400
- if (declarations.length > 0)
401
- instanceActions.set(key, { name, location: instance.location });
402
- symbols.set(name, { key, arity: symbol.arity });
403
- return;
404
- }
405
- const key = instantiation.getQualifiedKey(symbol.key);
406
- if (declarations.length > 0) {
407
- instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
408
- }
409
- symbols.set(`${instance.name}!${name}`, {
410
- key,
411
- arity: symbol.arity + instance.params.length,
412
- });
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;
413
556
  });
414
- 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
+ );
415
611
  };
416
612
 
417
613
  const getLocalArguments = (params: OperatorParam[], location: SourceLocation) =>
@@ -584,31 +780,70 @@ export const createEnvironment = (
584
780
  return definition ? [{ ...definition, name }] : [];
585
781
  });
586
782
 
783
+ const javaOverrides = new Map<string, JavaOverride[]>();
587
784
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
588
- 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;
589
793
  const visible: Symbols = new Map([
590
794
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
591
795
  operator.name,
592
- { key: operator.name, arity: operator.arity },
796
+ { key: operator.name, arity: operator.arity, kind: "Builtin" },
593
797
  ]),
594
798
  ...scope.symbols,
595
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
+ });
596
835
  const exported: Symbols = new Map();
836
+ const facts = new Set<string>();
597
837
  const modules = new Map<string, LinkedModule>();
598
838
  const extended: LinkedModule[] = [];
599
839
  const constants = [...parsed.constants];
600
840
  const variables = parsed.variables.map((variable) => variable.name);
601
841
  const variableLocations = parsed.variables.map((variable) => variable.location);
602
- const addSymbols = (symbols: Symbols, isExported: boolean) =>
603
- symbols.forEach((symbol, name) => {
604
- visible.set(name, symbol);
605
- if (isExported) exported.set(name, symbol);
606
- });
607
842
  parsed.extends.forEach((name) => {
608
843
  const nested = scope.modules.get(name);
609
844
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
610
845
  if (standardSymbols) {
611
- addSymbols(standardSymbols, true);
846
+ mergeExtended(standardSymbols);
612
847
  return;
613
848
  }
614
849
  const linked = nested ?? getLinkedModule(name, parsed.name);
@@ -622,61 +857,117 @@ export const createEnvironment = (
622
857
  variables.push(variable);
623
858
  variableLocations.push(linked.variableLocations[index]);
624
859
  });
625
- addSymbols(linked.exported, true);
860
+ mergeExtended(linked.exported);
861
+ linked.facts.forEach((name) => facts.add(name));
626
862
  linked.modules.forEach((module, moduleName) => modules.set(moduleName, module));
627
863
  extended.push(linked);
628
864
  });
629
- parsed.constants.forEach((constant) =>
630
- exported.set(constant.name, {
631
- key: constant.name,
632
- arity: constant.arity,
633
- declaration: "Constant",
634
- }),
635
- );
636
- parsed.variables.forEach((variable) =>
637
- 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)),
638
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
+ });
639
886
  const isParameterFree = constants.length === 0 && variables.length === 0;
640
887
  const getKey = (definition: Definition) =>
641
888
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
642
- const importedAt = new Map<string, SourceLocation>();
643
- 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 => ({
644
901
  imported: visible,
645
- importedAt,
646
- 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
+ ),
647
925
  getKey,
926
+ instanced,
927
+ facts,
928
+ reports,
929
+ warnings: scope.warnings,
648
930
  });
649
- const getVisibleModules = () => new Map([...scope.modules, ...modules]);
931
+ // SANY keeps the first of two symbols of a name.
650
932
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
651
- new Map([
652
- ...visible,
653
- ...[
654
- ...parsed.constants.map((constant) => ({ ...constant, kind: "Constant" as const })),
655
- ...parsed.variables.map((variable) => ({ ...variable, kind: "Variable" as const })),
656
- ]
933
+ [
934
+ ...declarations
657
935
  .filter((declaration) => isBefore(declaration.location, location))
658
936
  .map((declaration): [string, ModuleSymbol] => [
659
937
  declaration.name,
660
- { key: declaration.name, arity: declaration.arity, declaration: declaration.kind },
938
+ getDeclarationSymbol(declaration),
661
939
  ]),
662
940
  ...parsed.definitions
663
941
  .filter((definition) => isBefore(definition.location, location))
664
942
  .map((definition): [string, ModuleSymbol] => [
665
943
  definition.name,
666
- { key: getKey(definition), arity: getArity(definition) },
944
+ getDefinitionSymbol(definition),
667
945
  ]),
668
- ]);
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.
669
952
  const linkNested = (nested: Module) => {
670
953
  const visibleModules = getVisibleModules();
671
- 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);
672
957
  scope.nested.push(nested);
958
+ const report: ModuleReport = { location: nested.location, errors: [], warnings: [] };
959
+ reports.push(report);
673
960
  const linked = linkModule(nested, {
674
961
  id: `${scope.id}/${nested.name}`,
675
962
  symbols: getSymbolsBefore(nested.location),
676
963
  modules: visibleModules,
677
964
  nested: scope.nested,
965
+ errors: report.errors,
966
+ warnings: report.warnings,
967
+ levelChecks: scope.levelChecks,
678
968
  });
679
- modules.set(nested.name, linked);
969
+ if (redefinition) report.errors.push(redefinition);
970
+ if (!existing) modules.set(nested.name, linked);
680
971
  if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
681
972
  };
682
973
  [
@@ -690,10 +981,13 @@ export const createEnvironment = (
690
981
  isParameterFree,
691
982
  getVisibleModules().get(instance.module),
692
983
  );
693
- symbols.forEach((_, name) => {
694
- 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);
695
990
  });
696
- addSymbols(symbols, !instance.isLocal);
697
991
  },
698
992
  })),
699
993
  ...parsed.modules.map((nested) => ({
@@ -703,36 +997,30 @@ export const createEnvironment = (
703
997
  ]
704
998
  .sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
705
999
  .forEach((unit) => unit.link());
706
- checkRedefinitions(parsed, getUnit(parsed));
707
- const isInstancedAfterDefinition = (nested: Module) => {
708
- const first = parsed.instances.find((instance) => instance.module === nested.name);
709
- return first !== undefined && isBefore(nested.location, first.location);
710
- };
711
- const getLetModule = (instance: Instance) => {
712
- const own = parsed.modules.find((nested) => nested.name === instance.module);
713
- if (!own) return getVisibleModules().get(instance.module);
714
- return isInstancedAfterDefinition(own) ? modules.get(own.name) : undefined;
715
- };
716
1000
  const module = resolveModuleSubexpressions(
717
1001
  withLetInstances(parsed, (instance, lets) =>
718
1002
  instantiateLocally(instance, lets, parsed.name, getLetModule(instance)),
719
1003
  ),
720
1004
  getAvailableDefinitions(visible),
721
1005
  );
722
- resolveModule(module, getUnit(module));
1006
+ scope.errors.push(...resolveModule(module, getUnit(module)));
723
1007
  module.definitions.forEach((definition) => {
724
1008
  const key = getKey(definition);
725
1009
  definitions.set(key, definition);
726
1010
  parameterized.set(key, !isParameterFree);
727
- 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
+ }
728
1021
  visible.set(definition.name, symbol);
729
1022
  if (!definition.isLocal) exported.set(definition.name, symbol);
730
1023
  });
731
- checkModuleLevels(module, {
732
- constants: new Set(constants.map((constant) => constant.name)),
733
- variables: new Set(variables),
734
- getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
735
- });
736
1024
  const linked = {
737
1025
  module,
738
1026
  constants,
@@ -742,12 +1030,36 @@ export const createEnvironment = (
742
1030
  exported,
743
1031
  modules,
744
1032
  extended,
1033
+ facts: new Set([
1034
+ ...facts,
1035
+ ...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
1036
+ ]),
745
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
+ );
746
1057
  linkedList.push(linked);
747
1058
  return linked;
748
1059
  };
749
1060
 
750
1061
  const linkedRoot = linkTopLevel(root).linked;
1062
+ if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
751
1063
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
752
1064
  linkedRoot.visible.forEach((symbol, name) => {
753
1065
  const definition = definitions.get(symbol.key);
@@ -789,6 +1101,7 @@ export const createEnvironment = (
789
1101
  .filter((assignment) => assignment.args === undefined)
790
1102
  .forEach((assignment) => {
791
1103
  const value = toValue(assignment.value);
1104
+ markNormalized(value);
792
1105
  getScopedKeys(assignment).forEach((key) => {
793
1106
  const arity = getKeyArity(key);
794
1107
  if (arity === 0) constants.set(key, value);
@@ -908,11 +1221,26 @@ export const createEnvironment = (
908
1221
  if (symbol.key === key && name !== key) definitions.set(name, alias);
909
1222
  });
910
1223
  });
911
- declaredConstants.forEach((name) => {
912
- if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
913
- throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
914
- }
915
- });
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
+ }
916
1244
 
917
1245
  const environment: Environment = {
918
1246
  runtime,
@@ -920,6 +1248,7 @@ export const createEnvironment = (
920
1248
  builtins,
921
1249
  constants,
922
1250
  substitutions,
1251
+ instanceNames,
923
1252
  variables,
924
1253
  variableIndex: getVariableIndex(
925
1254
  variables,
@@ -927,22 +1256,9 @@ export const createEnvironment = (
927
1256
  ),
928
1257
  definitionValues: new Map(),
929
1258
  constantLevels: new Map(),
1259
+ substitutedDefinitions,
930
1260
  evaluators: new WeakMap(),
931
1261
  };
932
- checkInstances(instanceChecks, (name) => {
933
- const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
934
- return {
935
- constants: linked.constants,
936
- variables: linked.variables,
937
- definitions: [
938
- ...new Set(
939
- [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
940
- ),
941
- ],
942
- assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
943
- getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
944
- };
945
- });
946
1262
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
947
1263
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
948
1264
  // a minimum level as state-level, so the analyzer takes them for variables.
@@ -954,34 +1270,59 @@ export const createEnvironment = (
954
1270
  ]),
955
1271
  getDefinition: (name) => getDefinition(name, environment),
956
1272
  });
957
- const isEvaluable = (body: Expression) => {
1273
+ const getEvaluationError = (body: Expression) => {
958
1274
  try {
959
1275
  evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
960
- return true;
1276
+ return undefined;
961
1277
  } catch (error) {
962
- if (error instanceof TlaEvaluationError) return false;
1278
+ if (error instanceof TlaEvaluationError) return error;
963
1279
  throw error;
964
1280
  }
965
1281
  };
966
1282
  substitutions.forEach((target, name) => {
967
1283
  const definition = definitions.get(target);
968
- if (
1284
+ const error =
969
1285
  declaredConstants.includes(name) &&
970
1286
  definition?.kind === "Operator" &&
971
1287
  definition.params.length === 0 &&
972
- analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
973
- !isEvaluable(definition.body)
974
- ) {
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}`;
975
1294
  throw new TlaConfigError(
976
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
1295
+ `The configuration file substitutes constant ${name} with non-constant ${target}${addendum}`,
977
1296
  );
978
1297
  }
979
1298
  });
980
- runtime.isStarted = true;
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);
981
1307
  return {
982
1308
  environment,
983
1309
  assumptions,
984
1310
  variableLocations: linkedRoot.variableLocations,
985
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) ?? []),
986
1327
  };
987
1328
  };