tla-ts 0.0.8-dev.d9bffda → 0.0.9-dev.0dd11f695

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 (119) hide show
  1. package/README.md +30 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +40 -40
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +80 -10
  8. package/dist/index.d.ts +80 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +1 -1
  20. package/dist/transfer.cjs +128 -103
  21. package/dist/transfer.js +128 -103
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +605 -311
  30. package/src/cli/options.ts +12 -3
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlc-arguments.ts +646 -0
  35. package/src/cli/tlc-usage.ts +362 -0
  36. package/src/cli/tlc.ts +571 -185
  37. package/src/cli/typescript.ts +26 -0
  38. package/src/cli/xml-exporter.ts +4 -10
  39. package/src/codegen.ts +190 -46
  40. package/src/config.ts +101 -68
  41. package/src/coverage.ts +855 -0
  42. package/src/dependencies.ts +20 -28
  43. package/src/dfid.ts +636 -0
  44. package/src/environment.ts +266 -98
  45. package/src/errors.ts +142 -45
  46. package/src/evaluate.ts +632 -298
  47. package/src/exit-codes.ts +37 -1
  48. package/src/index.ts +3 -0
  49. package/src/instances.ts +85 -9
  50. package/src/java-overrides.ts +2 -12
  51. package/src/json-reader.ts +456 -0
  52. package/src/json.ts +262 -139
  53. package/src/levels.ts +2 -3
  54. package/src/lexer.ts +17 -26
  55. package/src/liveness-checker.ts +10 -18
  56. package/src/liveness.ts +121 -49
  57. package/src/parallel-worker.ts +14 -10
  58. package/src/parallel.ts +41 -17
  59. package/src/parser.ts +436 -90
  60. package/src/pcal/ast.ts +7 -28
  61. package/src/pcal/parse.ts +11 -39
  62. package/src/pcal/tokenize.ts +4 -20
  63. package/src/pcal/translate.ts +10 -42
  64. package/src/pcal/validate.ts +3 -21
  65. package/src/pretty-print.ts +1 -2
  66. package/src/prover/smt/solver/solver.ts +31 -27
  67. package/src/randomization.ts +84 -35
  68. package/src/resolve.ts +18 -60
  69. package/src/runtime.ts +11 -4
  70. package/src/sany/builtins.ts +3 -4
  71. package/src/sany/generator.ts +128 -176
  72. package/src/sany/levels.ts +6 -30
  73. package/src/sany/lexer.ts +7 -23
  74. package/src/sany/linter.ts +31 -29
  75. package/src/sany/modules.ts +1 -5
  76. package/src/sany/nodes.ts +4 -28
  77. package/src/sany/parse-error.ts +44 -100
  78. package/src/sany/report.ts +25 -40
  79. package/src/sany/selectors.ts +8 -22
  80. package/src/sany/source.ts +8 -15
  81. package/src/sany/xml.ts +11 -42
  82. package/src/search.ts +547 -144
  83. package/src/simulate.ts +694 -0
  84. package/src/spec-description.ts +166 -48
  85. package/src/standard-definitions.ts +2 -3
  86. package/src/standard-modules.ts +264 -68
  87. package/src/state-graph.ts +51 -18
  88. package/src/subexpressions.ts +31 -5
  89. package/src/successors.ts +267 -77
  90. package/src/symmetry.ts +31 -8
  91. package/src/tableau.ts +1 -2
  92. package/src/tlc/fingerprint.ts +183 -61
  93. package/src/tlc/jar-data.ts +113 -6
  94. package/src/tlc/messages.ts +122 -37
  95. package/src/tlc/order.ts +33 -38
  96. package/src/tlc/random.ts +16 -16
  97. package/src/tlc/replay.ts +563 -211
  98. package/src/tlc/summary.ts +2 -4
  99. package/src/tlc/tlc-operators.ts +81 -36
  100. package/src/tlc/tlcext-operators.ts +138 -117
  101. package/src/tlc/trace-spec.ts +273 -0
  102. package/src/tlc/value-stream.ts +143 -0
  103. package/src/tlc/version.ts +2 -4
  104. package/src/typescript/actions.ts +494 -0
  105. package/src/typescript/collections.ts +615 -0
  106. package/src/typescript/compile.ts +70 -0
  107. package/src/typescript/context.ts +106 -0
  108. package/src/typescript/emit.ts +122 -0
  109. package/src/typescript/expressions.ts +508 -0
  110. package/src/typescript/formula.ts +160 -0
  111. package/src/typescript/functions.ts +295 -0
  112. package/src/typescript/model.ts +164 -0
  113. package/src/typescript/names.ts +116 -0
  114. package/src/typescript/program.ts +51 -0
  115. package/src/typescript/properties.ts +148 -0
  116. package/src/typescript/scope.ts +117 -0
  117. package/src/typescript/steps.ts +175 -0
  118. package/src/typescript/types.ts +178 -0
  119. package/src/values.ts +832 -255
@@ -9,7 +9,7 @@ import {
9
9
  type OperatorParam,
10
10
  type SourceLocation,
11
11
  } from "./ast";
12
- import { BUNDLED_MODULES } from "./bundled-modules";
12
+ import type { ModuleOperator } from "./check";
13
13
  import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
15
  import {
@@ -19,17 +19,23 @@ import {
19
19
  formatSanyWarnings,
20
20
  TlaConfigError,
21
21
  TlaEvaluationError,
22
+ TlaFeatureUnsupportedError,
22
23
  TlaModuleError,
23
24
  TlaSyntaxError,
25
+ UNEXPECTED_EXCEPTION,
24
26
  type SanyError,
25
27
  } from "./errors";
26
28
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
27
29
  import {
30
+ bindOperatorArguments,
28
31
  createBigNumberError,
29
32
  createRealNumberError,
30
33
  evaluate,
31
34
  getDefinition,
32
35
  type Builtin,
36
+ type BuiltinArgument,
37
+ type Closure,
38
+ type Context,
33
39
  type Environment,
34
40
  } from "./evaluate";
35
41
  import {
@@ -60,10 +66,10 @@ import {
60
66
  } from "./resolve";
61
67
  import { createRuntime } from "./runtime";
62
68
  import { getSanyStandardModules } from "./sany/modules";
63
- import { USER_STANDARD_MODULES } from "./tlc/jar-data";
64
69
  import { getSanyParseError } from "./sany/parse-error";
65
70
  import {
66
71
  CORE_OPERATORS,
72
+ getJavaOverrides,
67
73
  getStandardModule,
68
74
  getTraceSpecArithmetic,
69
75
  STANDARD_DEPENDENCIES,
@@ -78,9 +84,9 @@ import { createTlcOrder, type TlcOrder } from "./tlc/order";
78
84
  import {
79
85
  isTlcInteger,
80
86
  createModelValue,
81
- createSet,
82
87
  formatValue,
83
88
  valuesEqual,
89
+ toFiniteSet,
84
90
  type Value,
85
91
  markNormalized,
86
92
  } from "./values";
@@ -89,7 +95,6 @@ import type { TlcVersion } from "./tlc/version";
89
95
  interface EnvironmentOptions {
90
96
  source: string;
91
97
  loadModule: ((name: string) => string | undefined) | undefined;
92
- // The TLC orders of the same spec, when the caller already reads them.
93
98
  order?: TlcOrder;
94
99
  loadClass?: (name: string) => Uint8Array | undefined;
95
100
  print: (text: string) => void;
@@ -98,15 +103,19 @@ interface EnvironmentOptions {
98
103
  // Links the modules without a model, as SANY does, so a declared constant needs no value.
99
104
  isUnconfigured?: boolean;
100
105
  isTool?: boolean;
101
- // TLC loads Integers for the binary trace of its trace exploration spec, and Integers' Java
102
- // methods then compute the arithmetic of Naturals.
103
- isTraceRecorded?: boolean;
106
+ // TLC loads Integers for a trace exploration spec, whose methods then compute Naturals.
107
+ traceFile?: string;
104
108
  isSanyReported?: boolean;
105
109
  isWarningEnabled?: boolean;
106
110
  version?: TlcVersion;
111
+ moduleOperators?: ModuleOperator[];
107
112
  }
108
113
 
109
- interface Specification {
114
+ export interface ResolvedModuleOperator extends ModuleOperator {
115
+ key: string | undefined;
116
+ }
117
+
118
+ export interface Specification {
110
119
  environment: Environment;
111
120
  assumptions: Assumption[];
112
121
  variableLocations: SourceLocation[];
@@ -117,6 +126,8 @@ interface Specification {
117
126
  rootSymbols: Symbols;
118
127
  // The methods of the modules' classes, in the order TLC loads the classes.
119
128
  javaOverrides: JavaOverride[];
129
+ moduleOperators: ResolvedModuleOperator[];
130
+ rootName: string;
120
131
  }
121
132
 
122
133
  export interface InstanceAction {
@@ -124,13 +135,17 @@ export interface InstanceAction {
124
135
  location: SourceLocation;
125
136
  }
126
137
 
138
+ // The modules whose Java methods TLC binds through annotations rather than by name alone.
139
+ const ANNOTATED_OVERRIDE_MODULES = new Set(["IOUtils", "Json", "TLCExt"]);
140
+
127
141
  const toValue = (value: ConfigValue): Value => {
128
142
  switch (value.kind) {
129
143
  case "Number":
130
- // TLC fails with an unexpected exception on a configuration number too big for it.
144
+ // ModelConfig#parseValue lets Integer.parseInt fail on a number too big for TLC, where
145
+ // tla-ts reports it as TLC reports such a number in a module.
131
146
  if (!isTlcInteger(value.value)) {
132
147
  throw setExitCode(
133
- new TlaModuleError(`Integer overflow: ${value.value} is outside TLC's 32-bit range`),
148
+ new TlaModuleError(`TLC can't handle a number this big.\n${value.value}`),
134
149
  EXIT_STATUS.error,
135
150
  );
136
151
  }
@@ -141,7 +156,26 @@ const toValue = (value: ConfigValue): Value => {
141
156
  case "ModelValue":
142
157
  return createModelValue(value.name);
143
158
  case "Set":
144
- return createSet(value.elements.map(toValue));
159
+ return toFiniteSet({ kind: "MixedSet", elements: value.elements.map(toValue) });
160
+ }
161
+ };
162
+
163
+ // TLC sorts a config set as it starts, and reports a failure as an unexpected RuntimeException.
164
+ const toConstantValue = (value: ConfigValue) => {
165
+ try {
166
+ return toValue(value);
167
+ } catch (error) {
168
+ if (
169
+ !(error instanceof TlaEvaluationError) ||
170
+ error instanceof TlaModuleError ||
171
+ error instanceof TlaFeatureUnsupportedError
172
+ ) {
173
+ throw error;
174
+ }
175
+ throw setExitCode(
176
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`),
177
+ EXIT_STATUS.error,
178
+ );
145
179
  }
146
180
  };
147
181
 
@@ -157,8 +191,7 @@ interface LinkedModule {
157
191
  facts: Set<string>;
158
192
  }
159
193
 
160
- // errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
161
- // the modules in its file, which SANY runs only when it reports no error.
194
+ // SANY runs the level checks of a module's file only when it reports no error.
162
195
  interface ModuleScope {
163
196
  id: string;
164
197
  symbols: Symbols;
@@ -178,8 +211,7 @@ const getDeclarations = (linked: LinkedModule): Declaration[] => [
178
211
  })),
179
212
  ];
180
213
 
181
- // Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
182
- // does in TLC: SANY's message, then a fatal error that names the module.
214
+ // A module SANY rejects fails as in TLC: SANY's message, then a fatal error naming the module.
183
215
  const parseModuleFile = (source: string, name: string, order: TlcOrder) => {
184
216
  try {
185
217
  return order.parseModule(source);
@@ -212,12 +244,10 @@ const getModuleRedefinition = (module: Module, existing: Module): SanyError | un
212
244
  message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
213
245
  };
214
246
 
215
- // Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
216
- // declaration.
247
+ // SANY names a definition or a parameter a definition, and any other symbol a declaration.
217
248
  const getNodeWord = (kind: SymbolKind) =>
218
249
  ["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
219
250
 
220
- // The Java class of the node SANY binds a symbol to.
221
251
  const getNodeClass = (kind: SymbolKind) =>
222
252
  kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
223
253
 
@@ -306,9 +336,9 @@ export const createEnvironment = (
306
336
  const root = order.parseModule(options.source);
307
337
  const definitions = new Map<string, Definition>();
308
338
  const parameterized = new Map<string, boolean>();
309
- // The definition each instance copy came from, and the instance parameters the copy adds.
310
339
  const copySources = new Map<string, { source: string; prefix: number }>();
311
340
  const substitutedDefinitions = new Set<Definition>();
341
+ const instanceParams = new Set<OperatorParam>();
312
342
  const builtins = new Map<string, Builtin>(
313
343
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
314
344
  );
@@ -318,13 +348,12 @@ export const createEnvironment = (
318
348
  runtime.isTool = options.isTool ?? false;
319
349
  runtime.version = options.version ?? "master";
320
350
  const linkedModules = new Map<string, LinkedModule>();
321
- const nestedModules = new Map<string, LinkedModule>();
322
- // Each module by name, from the time its level checks run.
351
+ // From the time its level checks run.
323
352
  const checkedModules = new Map<string, LinkedModule>();
324
353
  const linkOrder: LinkedModule[] = [];
325
354
  const dependencies = new Map<string, Set<string>>();
326
355
  const loading: Set<string>[] = [];
327
- // The top-level modules being linked, to report a cycle of EXTENDS or INSTANCE.
356
+ // To report a cycle of EXTENDS or INSTANCE.
328
357
  const linking: string[] = [];
329
358
 
330
359
  const useModule = (name: string, used: Iterable<string>) =>
@@ -360,8 +389,7 @@ export const createEnvironment = (
360
389
  if (cached) return cached;
361
390
  const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
362
391
  if (!standardModule) return undefined;
363
- // TLC loads the modules a standard module instantiates too, and the Java methods of each
364
- // replace the operators of every module, as those of Integers replace those of Naturals.
392
+ // The Java methods of a standard module's dependencies replace operators too, as Integers' do.
365
393
  STANDARD_DEPENDENCIES.get(name)?.forEach(addStandardModule);
366
394
  const exported: Symbols = new Map();
367
395
  standardModule.extends.forEach((extended) =>
@@ -384,14 +412,17 @@ export const createEnvironment = (
384
412
  };
385
413
 
386
414
  const getStandardSymbols = (name: string) => {
387
- // SANY reads a module from the user's directories before its standard modules.
388
- if (USER_STANDARD_MODULES.has(name) && options.loadModule?.(name) !== undefined)
415
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
416
+ // SANY reads a user's module before a standard one, so a standard module using it is linked.
417
+ // TLCExt defines AssertEq and TLCGetAndSet in TLA+, so it is linked from its source too.
418
+ if (
419
+ name === "TLCExt" ||
420
+ [name, ...dependencies].some((module) => options.loadModule?.(module) !== undefined)
421
+ )
389
422
  return undefined;
390
423
  const symbols = addStandardModule(name);
391
- const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
392
424
  if (symbols) useModule(name, dependencies);
393
- // TLC loads the Java class of each module a standard module uses, so one that uses Integers
394
- // overrides the arithmetic of Naturals.
425
+ // TLC loads each dependency's Java class, so using Integers overrides Naturals' arithmetic.
395
426
  if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
396
427
  return symbols;
397
428
  };
@@ -418,7 +449,7 @@ export const createEnvironment = (
418
449
  const loadModule = (name: string, importer: string) => {
419
450
  const source =
420
451
  options.loadModule?.(name) ??
421
- BUNDLED_MODULES.get(name) ??
452
+ getSanyStandardModules(runtime.version).get(name) ??
422
453
  extractModule(options.source, name);
423
454
  if (source === undefined) throw createMissingModuleError(name, importer);
424
455
  const module = parseModuleFile(source, name, order);
@@ -478,14 +509,51 @@ export const createEnvironment = (
478
509
  return cached;
479
510
  }
480
511
  const top = linkTopLevel(loadModule(name, importer));
481
- // TLC replaces each definition of a module that has a standard module's name with the Java
482
- // method of that name, so a user's Bags.tla keeps the definitions that Bags has no method for.
483
- const standardModule = getSanyStandardModules(runtime.version).has(name)
484
- ? getStandardModule(name, options.print, runtime, options.files, builtins)
485
- : undefined;
486
- standardModule?.operators.forEach((operator) => {
512
+ const replaced = new Map<string, Definition>();
513
+ const evaluateReplaced = (
514
+ operator: string,
515
+ args: BuiltinArgument[],
516
+ context: Context | undefined,
517
+ ) => {
518
+ const definition = replaced.get(operator);
519
+ const expressions = args.flatMap((arg) =>
520
+ typeof arg === "object" && arg.kind === "Operator" && arg.expression
521
+ ? [arg.expression]
522
+ : [],
523
+ );
524
+ if (definition?.kind !== "Operator" || !context || expressions.length !== args.length) {
525
+ throw new TlaEvaluationError(`No definition of ${operator} to fall back to`);
526
+ }
527
+ const closure: Closure = {
528
+ kind: "Closure",
529
+ name: definition.name,
530
+ params: definition.params,
531
+ body: definition.body,
532
+ scope: undefined,
533
+ };
534
+ return evaluate(definition.body, {
535
+ environment: context.environment,
536
+ scope: bindOperatorArguments(closure, expressions, context),
537
+ state: context.state,
538
+ next: context.next,
539
+ isKeepLazy: context.isKeepLazy,
540
+ });
541
+ };
542
+ // TLC replaces only the definitions that have Java methods, so a user's Bags.tla keeps the
543
+ // rest.
544
+ getJavaOverrides(
545
+ name,
546
+ options.print,
547
+ runtime,
548
+ options.files,
549
+ builtins,
550
+ evaluateReplaced,
551
+ )?.forEach((operator) => {
487
552
  const symbol = top.linked.visible.get(operator.name);
488
- if (symbol?.kind !== "Definition" || symbol.arity !== operator.arity) return;
553
+ if (symbol?.kind !== "Definition") return;
554
+ if (symbol.arity !== operator.arity && ANNOTATED_OVERRIDE_MODULES.has(name)) return;
555
+ const definition = definitions.get(symbol.key);
556
+ if (definition) replaced.set(operator.name, definition);
489
557
  builtins.set(symbol.key, operator);
490
558
  definitions.delete(symbol.key);
491
559
  });
@@ -524,13 +592,23 @@ export const createEnvironment = (
524
592
  substitutions,
525
593
  location: instance.location,
526
594
  });
595
+ const params = instance.params.map((param) => ({ ...param, name: `${param.name}${suffix}` }));
596
+ params.forEach((param) => instanceParams.add(param));
597
+ const substitutionRange = instance.definitionRange
598
+ ? {
599
+ ...instance.definitionRange,
600
+ start: { line: instance.location.line, column: instance.location.column },
601
+ }
602
+ : instance.instanceRange;
527
603
  const instantiation = createInstantiation({
528
- params: instance.params.map((param) => ({ ...param, name: `${param.name}${suffix}` })),
604
+ params,
529
605
  substitutions: new Map(
530
606
  [...substitutions].map(([name, expression]) => [name, renameLocals(expression, renamed)]),
531
607
  ),
532
608
  variables: new Set(linked.variables),
533
609
  getDefinition: (key) => definitions.get(key),
610
+ getOuterDefinition: (name) =>
611
+ module.definitions.find((definition) => definition.name === name),
534
612
  isParameterized: (key) => parameterized.get(key) === true,
535
613
  addDefinition: (source, name, create) => {
536
614
  const key = createName(`${name}@${module.name}`);
@@ -545,6 +623,13 @@ export const createEnvironment = (
545
623
  return key;
546
624
  },
547
625
  createName,
626
+ ...(declarations.length > 0 && {
627
+ frame: {
628
+ kind: "Identifier",
629
+ name: instance.name ?? instance.module,
630
+ location: { ...substitutionRange.start, range: substitutionRange },
631
+ },
632
+ }),
548
633
  });
549
634
  return getInstanceSymbols(instance, linked.exported, declarations, (key, name) => {
550
635
  const copy =
@@ -567,9 +652,7 @@ export const createEnvironment = (
567
652
  ]),
568
653
  );
569
654
 
570
- // The names an INSTANCE adds for the definitions of the module it instantiates, each with the key
571
- // that getKey gives it. SANY binds a copy of a definition that depends on the parameters at the
572
- // INSTANCE.
655
+ // SANY binds a copy of a definition that depends on the parameters at the INSTANCE.
573
656
  const getInstanceSymbols = (
574
657
  instance: Instance,
575
658
  exported: Symbols,
@@ -742,6 +825,8 @@ export const createEnvironment = (
742
825
  ),
743
826
  variables: new Set(linked.variables),
744
827
  getDefinition: (key) => definitions.get(key),
828
+ // Each substitution is a local definition, which the copies read by name.
829
+ getOuterDefinition: () => undefined,
745
830
  isParameterized: (key) => parameterized.get(key) === true,
746
831
  addDefinition: (_source, name, create) => {
747
832
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
@@ -796,12 +881,11 @@ export const createEnvironment = (
796
881
  ]),
797
882
  ...scope.symbols,
798
883
  ]);
799
- // Where an INSTANCE first made each name it adds visible.
800
884
  const importedAt = new Map<string, SourceLocation>();
801
885
  const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
802
886
  const reports = [extendsReport];
803
- // Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
804
- // and warns of two definitions or two declarations but rejects a definition and a declaration.
887
+ // SANY keeps the first symbol of a name the extended modules share, warns of two definitions
888
+ // or two declarations, and rejects a definition and a declaration.
805
889
  const mergeExtended = (symbols: Symbols) =>
806
890
  symbols.forEach((symbol, name) => {
807
891
  const current = exported.get(name);
@@ -836,8 +920,12 @@ export const createEnvironment = (
836
920
  const modules = new Map<string, LinkedModule>();
837
921
  const extended: LinkedModule[] = [];
838
922
  const constants = [...parsed.constants];
839
- const variables = parsed.variables.map((variable) => variable.name);
840
- const variableLocations = parsed.variables.map((variable) => variable.location);
923
+ const declaredVariables = parsed.variables.filter(
924
+ (variable, index) =>
925
+ parsed.variables.findIndex((other) => other.name === variable.name) === index,
926
+ );
927
+ const variables = declaredVariables.map((variable) => variable.name);
928
+ const variableLocations = declaredVariables.map((variable) => variable.location);
841
929
  parsed.extends.forEach((name) => {
842
930
  const nested = scope.modules.get(name);
843
931
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
@@ -847,12 +935,16 @@ export const createEnvironment = (
847
935
  }
848
936
  const linked = nested ?? getLinkedModule(name, parsed.name);
849
937
  constants.push(
850
- ...linked.constants.filter((constant) =>
851
- constants.every((existing) => existing.name !== constant.name),
938
+ ...linked.constants.filter(
939
+ (constant) =>
940
+ constants.every((existing) => existing.name !== constant.name) &&
941
+ !variables.includes(constant.name),
852
942
  ),
853
943
  );
854
944
  linked.variables.forEach((variable, index) => {
855
- if (variables.includes(variable)) return;
945
+ const isDeclared =
946
+ variables.includes(variable) || constants.some((constant) => constant.name === variable);
947
+ if (isDeclared) return;
856
948
  variables.push(variable);
857
949
  variableLocations.push(linked.variableLocations[index]);
858
950
  });
@@ -887,14 +979,12 @@ export const createEnvironment = (
887
979
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
888
980
  const instanced = new Map<Instance, Symbols>();
889
981
  const getVisibleModules = () => new Map([...scope.modules, ...modules]);
890
- const isInstancedAfterDefinition = (nested: Module) => {
891
- const first = parsed.instances.find((instance) => instance.module === nested.name);
892
- return first !== undefined && isBefore(nested.location, first.location);
893
- };
894
982
  const getLetModule = (instance: Instance) => {
895
983
  const ownModule = parsed.modules.find((nested) => nested.name === instance.module);
896
984
  if (!ownModule) return getVisibleModules().get(instance.module);
897
- return isInstancedAfterDefinition(ownModule) ? modules.get(ownModule.name) : undefined;
985
+ return isBefore(ownModule.location, instance.location)
986
+ ? modules.get(ownModule.name)
987
+ : undefined;
898
988
  };
899
989
  const getUnit = (module: Module): ModuleUnit => ({
900
990
  imported: visible,
@@ -946,8 +1036,7 @@ export const createEnvironment = (
946
1036
  (symbols, [name, symbol]) => (symbols.has(name) ? symbols : symbols.set(name, symbol)),
947
1037
  new Map(visible),
948
1038
  );
949
- // Generator.generateModule reports the errors of a nested module and goes on, and then
950
- // SymbolTable.addModule reports a module already defined.
1039
+ // SANY reports the errors of a nested module and goes on, then reports a module defined twice.
951
1040
  const linkNested = (nested: Module) => {
952
1041
  const visibleModules = getVisibleModules();
953
1042
  const existing = visibleModules.get(nested.name);
@@ -967,7 +1056,6 @@ export const createEnvironment = (
967
1056
  });
968
1057
  if (redefinition) report.errors.push(redefinition);
969
1058
  if (!existing) modules.set(nested.name, linked);
970
- if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
971
1059
  };
972
1060
  [
973
1061
  ...parsed.instances.map((instance) => ({
@@ -1035,8 +1123,7 @@ export const createEnvironment = (
1035
1123
  ]),
1036
1124
  };
1037
1125
  if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
1038
- // SANY checks the instances of a module with its definitions and theorems, so it reports their
1039
- // errors together.
1126
+ // SANY checks instances with definitions and theorems, so it reports their errors together.
1040
1127
  const checks = instanceChecks
1041
1128
  .splice(firstCheck)
1042
1129
  .map((check) => ({ location: check.location, check: () => checkInstance(check) }));
@@ -1058,12 +1145,40 @@ export const createEnvironment = (
1058
1145
  };
1059
1146
 
1060
1147
  const linkedRoot = linkTopLevel(root).linked;
1148
+ const traceFile = options.traceFile;
1149
+ // TLC records the binary trace of a trace exploration spec with a postcondition it adds last.
1150
+ const traceOperators: ModuleOperator[] =
1151
+ traceFile === undefined
1152
+ ? []
1153
+ : [
1154
+ {
1155
+ kind: "postcondition",
1156
+ module: "_TLCTrace",
1157
+ operator: "_TLCTraceSilent",
1158
+ constants: { _TLCTraceFile: traceFile },
1159
+ },
1160
+ ];
1161
+ // SANY reads these modules as extendees of the root, so TLC finishes them before the root.
1162
+ const moduleOperators = [...(options.moduleOperators ?? []), ...traceOperators].map(
1163
+ (operator): ResolvedModuleOperator => {
1164
+ const symbol = getLinkedModule(operator.module, root.name).visible.get(operator.operator);
1165
+ return { ...operator, key: symbol?.kind === "Definition" ? symbol.key : undefined };
1166
+ },
1167
+ );
1168
+ linkOrder.push(...linkOrder.splice(linkOrder.indexOf(linkedRoot), 1));
1061
1169
  if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
1062
1170
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
1063
1171
  linkedRoot.visible.forEach((symbol, name) => {
1064
1172
  const definition = definitions.get(symbol.key);
1065
1173
  if (definition && symbol.key !== name) definitions.set(name, definition);
1066
1174
  });
1175
+ // TLC adds a constraint or a view it names on the command line to the root's definitions.
1176
+ moduleOperators.forEach((operator) => {
1177
+ const definition = operator.key === undefined ? undefined : definitions.get(operator.key);
1178
+ if (operator.kind !== "postcondition" && definition) {
1179
+ definitions.set(operator.operator, definition);
1180
+ }
1181
+ });
1067
1182
 
1068
1183
  const assumptions: Assumption[] = [];
1069
1184
  const addAssumptions = (linked: LinkedModule, visited: Set<LinkedModule>) => {
@@ -1076,17 +1191,30 @@ export const createEnvironment = (
1076
1191
  const declaredConstants = linkedRoot.constants.map((constant) => constant.name);
1077
1192
  const variables = linkedRoot.variables;
1078
1193
 
1079
- const getScopedKeys = (item: { name: string; module?: string }) => {
1194
+ const moduleSymbols = new Map([
1195
+ ...standardExports,
1196
+ ...[...linkedModules].map(([name, linked]): [string, Symbols] => [name, linked.visible]),
1197
+ [root.name, linkedRoot.visible],
1198
+ ]);
1199
+ const getScopedSymbol = (item: { name: string; module?: string }) => {
1200
+ const symbol = moduleSymbols.get(item.module ?? "")?.get(item.name);
1201
+ return symbol?.kind === "Definition" ? symbol : undefined;
1202
+ };
1203
+ const getSubstitutedKeys = (item: { name: string; module?: string }) => {
1080
1204
  if (item.module === undefined) return [item.name, getRootKey(item.name)];
1081
- if (item.module === root.name || declaredConstants.includes(item.name)) return [];
1082
- const linked = linkedModules.get(item.module) ?? nestedModules.get(item.module);
1083
- return [linked?.visible.get(item.name)?.key ?? item.name];
1205
+ const symbol = getScopedSymbol(item);
1206
+ return symbol ? [symbol.key] : [];
1084
1207
  };
1208
+ // TLC ignores a scoped value for a definition of the root module, though not a scoped override.
1209
+ const getScopedKeys = (item: { name: string; module?: string }) =>
1210
+ item.module === root.name ? [] : getSubstitutedKeys(item);
1085
1211
  const getKeyArity = (key: string) => {
1086
1212
  const definition = definitions.get(key);
1087
1213
  return definition?.kind === "Operator"
1088
1214
  ? definition.params.length
1089
- : (linkedRoot.constants.find((constant) => constant.name === key)?.arity ?? 0);
1215
+ : (linkedRoot.constants.find((constant) => constant.name === key)?.arity ??
1216
+ builtins.get(key)?.arity ??
1217
+ 0);
1090
1218
  };
1091
1219
  const constants = new Map<string, Value>();
1092
1220
  const operatorKeys = new Set<string>();
@@ -1099,7 +1227,7 @@ export const createEnvironment = (
1099
1227
  config.constants
1100
1228
  .filter((assignment) => assignment.args === undefined)
1101
1229
  .forEach((assignment) => {
1102
- const value = toValue(assignment.value);
1230
+ const value = toConstantValue(assignment.value);
1103
1231
  markNormalized(value);
1104
1232
  getScopedKeys(assignment).forEach((key) => {
1105
1233
  const arity = getKeyArity(key);
@@ -1135,20 +1263,38 @@ export const createEnvironment = (
1135
1263
  });
1136
1264
  tables.forEach((table) =>
1137
1265
  getScopedKeys(table).forEach((key) =>
1138
- setOperator(
1139
- key,
1140
- createOperatorTable(table.name, getKeyArity(key) || table.rows[0][0].length, table.rows),
1141
- ),
1266
+ setOperator(key, createOperatorTable(table.name, getKeyArity(key), table.rows)),
1142
1267
  ),
1143
1268
  );
1144
1269
  const substitutions = new Map(
1145
1270
  config.substitutions.flatMap((substitution) =>
1146
- getScopedKeys(substitution).map((key): [string, string] => [
1271
+ getSubstitutedKeys(substitution).map((key): [string, string] => [
1147
1272
  key,
1148
1273
  getRootKey(substitution.target),
1149
1274
  ]),
1150
1275
  ),
1151
1276
  );
1277
+ [...substitutions.keys()].forEach((key) => constants.delete(key));
1278
+ const checkSubstitutionTarget = (name: string, target: string, arity: number | undefined) => {
1279
+ const targetKey = getRootKey(target);
1280
+ const definition = definitions.get(targetKey);
1281
+ const isDefined =
1282
+ definition !== undefined ||
1283
+ builtins.has(targetKey) ||
1284
+ constants.has(targetKey) ||
1285
+ declaredConstants.includes(target) ||
1286
+ variables.includes(target);
1287
+ if (!isDefined) {
1288
+ throw new TlaConfigError(
1289
+ `The configuration file substitutes for ${name} with the undefined identifier ${target}.`,
1290
+ );
1291
+ }
1292
+ if (definition?.kind === "Operator" && definition.params.length !== arity) {
1293
+ throw new TlaConfigError(
1294
+ `The configuration file substitutes for ${name} with ${target} of different number of arguments.`,
1295
+ );
1296
+ }
1297
+ };
1152
1298
  config.substitutions
1153
1299
  .filter((substitution) => substitution.module === undefined)
1154
1300
  .forEach((substitution) => {
@@ -1164,32 +1310,45 @@ export const createEnvironment = (
1164
1310
  `In the configuration file, the identifier ${target} appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.`,
1165
1311
  );
1166
1312
  }
1167
- const targetKey = getRootKey(target);
1168
- const definition = definitions.get(targetKey);
1169
- const isDefined =
1170
- definition !== undefined ||
1171
- builtins.has(targetKey) ||
1172
- constants.has(targetKey) ||
1173
- declaredConstants.includes(target) ||
1174
- variables.includes(target);
1175
- if (!isDefined) {
1176
- throw new TlaConfigError(
1177
- `The configuration file substitutes for ${name} with the undefined identifier ${target}.`,
1178
- );
1179
- }
1180
- if (
1181
- definition?.kind === "Operator" &&
1182
- definition.params.length !==
1183
- (linkedRoot.constants.find((constant) => constant.name === name)?.arity ??
1184
- linkedRoot.visible.get(name)?.arity)
1185
- ) {
1313
+ checkSubstitutionTarget(
1314
+ name,
1315
+ target,
1316
+ linkedRoot.constants.find((constant) => constant.name === name)?.arity ??
1317
+ linkedRoot.visible.get(name)?.arity,
1318
+ );
1319
+ });
1320
+ // TLC replaces only the definitions of a named module, with definitions of the root module.
1321
+ const scopedSubstitutions = config.substitutions.filter(
1322
+ (substitution) => substitution.module !== undefined,
1323
+ );
1324
+ [...moduleSymbols.keys()].forEach((name) => {
1325
+ const inModule = scopedSubstitutions.filter((substitution) => substitution.module === name);
1326
+ inModule.forEach((substitution) => {
1327
+ const symbol = getScopedSymbol(substitution);
1328
+ if (!symbol) return;
1329
+ if (inModule.some((other) => other.name === substitution.target)) {
1186
1330
  throw new TlaConfigError(
1187
- `The configuration file substitutes for ${name} with ${target} of different number of arguments.`,
1331
+ `In the configuration file, the identifier ${substitution.target} appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.`,
1188
1332
  );
1189
1333
  }
1334
+ checkSubstitutionTarget(substitution.name, substitution.target, symbol.arity);
1190
1335
  });
1191
- // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
1192
- // the module shares.
1336
+ const missing = inModule.find((substitution) => !getScopedSymbol(substitution));
1337
+ if (missing) {
1338
+ throw new TlaConfigError(
1339
+ `In the configuration file, the identifier ${missing.name} does not appear in the specification.`,
1340
+ );
1341
+ }
1342
+ });
1343
+ const unknownModule = scopedSubstitutions.find(
1344
+ (substitution) => !moduleSymbols.has(substitution.module ?? ""),
1345
+ );
1346
+ if (unknownModule) {
1347
+ throw new TlaConfigError(
1348
+ `In the configuration file, the module name ${unknownModule.module} is not a module in the specification.`,
1349
+ );
1350
+ }
1351
+ // TLC sets a module-scoped entry on the definition's body, which every instance shares.
1193
1352
  const scopedKeys = new Set(
1194
1353
  [...config.constants, ...config.substitutions]
1195
1354
  .filter((item) => item.module !== undefined)
@@ -1220,6 +1379,12 @@ export const createEnvironment = (
1220
1379
  if (symbol.key === key && name !== key) definitions.set(name, alias);
1221
1380
  });
1222
1381
  });
1382
+ moduleOperators.forEach((operator) =>
1383
+ getLinkedModule(operator.module, root.name).constants.forEach((constant) => {
1384
+ const value = operator.constants?.[constant.name];
1385
+ if (value !== undefined) constants.set(constant.name, value);
1386
+ }),
1387
+ );
1223
1388
  const unassigned = declaredConstants.filter(
1224
1389
  (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
1225
1390
  );
@@ -1233,7 +1398,7 @@ export const createEnvironment = (
1233
1398
  );
1234
1399
  }
1235
1400
 
1236
- if (options.isTraceRecorded) {
1401
+ if (options.traceFile !== undefined) {
1237
1402
  builtins.forEach((builtin, name) => {
1238
1403
  const replacement = getTraceSpecArithmetic(builtin);
1239
1404
  if (replacement) builtins.set(name, replacement);
@@ -1264,13 +1429,13 @@ export const createEnvironment = (
1264
1429
  facts,
1265
1430
  startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
1266
1431
  substitutedDefinitions,
1432
+ instanceParams,
1267
1433
  evaluators: new WeakMap(),
1268
1434
  entryEvaluators: new WeakMap(),
1269
1435
  definitionEvaluators: new WeakMap(),
1270
1436
  };
1271
- // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
1272
- // computes is above constant. The bound counts the operators that TLC's Java code overrides with
1273
- // a minimum level as state-level, so the analyzer takes them for variables.
1437
+ // TLC rejects a substitute only when evaluating it at startup fails and its level bound is
1438
+ // above constant. TLC's Java overrides with a minimum level count as variables.
1274
1439
  const analyzer = createLevelAnalyzer({
1275
1440
  constants: new Set(constants.keys()),
1276
1441
  variables: new Set([
@@ -1307,8 +1472,7 @@ export const createEnvironment = (
1307
1472
  });
1308
1473
  // TLC 1.7.4 processes the whole spec before it creates its model checker, which TLCGet reads.
1309
1474
  if (runtime.version !== "1.7.4") runtime.isStarted = true;
1310
- // TLC computes the constant definitions of each module in the order SANY finishes the modules,
1311
- // except those of a module that an INSTANCE gives parameters to.
1475
+ // TLC computes constant definitions in the order SANY finishes modules, except instantiated ones.
1312
1476
  const instantiated = new Set(
1313
1477
  linkOrder.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
1314
1478
  );
@@ -1326,6 +1490,8 @@ export const createEnvironment = (
1326
1490
  getDefinitionOrder(linked === linkedRoot ? undefined : linked.module.name).flatMap(
1327
1491
  (name) => {
1328
1492
  const definition = definitions.get(linked.visible.get(name)?.key ?? name);
1493
+ // TLC doesn't compute the copy of a definition from a module instantiated with
1494
+ // substitutions.
1329
1495
  return definition ? [definition] : [];
1330
1496
  },
1331
1497
  ),
@@ -1333,5 +1499,7 @@ export const createEnvironment = (
1333
1499
  rootConstants: linkedRoot.constants,
1334
1500
  rootSymbols: linkedRoot.visible,
1335
1501
  javaOverrides: linkOrder.flatMap((linked) => javaOverrides.get(linked.module.name) ?? []),
1502
+ moduleOperators,
1503
+ rootName: root.name,
1336
1504
  };
1337
1505
  };