tla-ts 0.0.3-dev.e5b05b9 → 0.0.3-dev.e8bef80

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 (79) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +150 -48
  5. package/dist/index.d.ts +150 -48
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -22
  12. package/dist/run-cli.js +1352 -23
  13. package/dist/transfer.cjs +83 -43
  14. package/dist/transfer.js +83 -43
  15. package/package.json +5 -1
  16. package/src/ast.ts +28 -4
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -17
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1317 -428
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -21
  24. package/src/config.ts +226 -207
  25. package/src/environment.ts +221 -75
  26. package/src/errors.ts +130 -15
  27. package/src/evaluate.ts +713 -259
  28. package/src/exit-codes.ts +6 -24
  29. package/src/fingerprints.ts +1 -2
  30. package/src/index.ts +1 -0
  31. package/src/instances.ts +3 -1
  32. package/src/java-overrides.ts +104 -25
  33. package/src/levels.ts +6 -1
  34. package/src/lexer.ts +122 -29
  35. package/src/live-check.ts +204 -148
  36. package/src/liveness.ts +191 -122
  37. package/src/obligation-expression.ts +2 -2
  38. package/src/obligation-source.ts +2 -0
  39. package/src/parallel-worker.ts +6 -1
  40. package/src/parallel.ts +57 -6
  41. package/src/parser.ts +327 -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 +252 -87
  49. package/src/run-cli.ts +1162 -200
  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 +410 -480
  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 +2033 -3015
  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 +25 -5
  64. package/src/standard-modules.ts +172 -129
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +121 -26
  67. package/src/successors.ts +141 -173
  68. package/src/symmetry.ts +168 -50
  69. package/src/tableau.ts +3 -1
  70. package/src/tlc-ext.ts +13 -17
  71. package/src/tlc-messages.ts +329 -0
  72. package/src/tlc-module.ts +49 -21
  73. package/src/tlc-modules.ts +0 -4
  74. package/src/tlc-order.ts +55 -9
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/values.ts +664 -317
package/src/resolve.ts CHANGED
@@ -58,11 +58,14 @@ interface Goal {
58
58
  }
59
59
 
60
60
  // isPending holds for an operator a RECURSIVE declaration names until its definition.
61
+ // isNonFact holds for the name of a USE, HIDE, DEFINE or INSTANCE step, which SANY takes neither as
62
+ // a fact nor as an expression.
61
63
  interface LocalName extends Binding {
62
64
  name: string;
63
65
  parent: LocalName | undefined;
64
66
  goal?: Goal;
65
67
  isPending?: boolean;
68
+ isNonFact?: boolean;
66
69
  }
67
70
 
68
71
  // A name the module binds, from position on.
@@ -78,8 +81,10 @@ interface ModuleBinding extends Binding {
78
81
  // names bound since the innermost definition, label, or step, which a label must take as parameters,
79
82
  // and labels the labels there. isGenerated holds in a definition tla-ts generates for a LET INSTANCE,
80
83
  // and in a subexpression that a name selects, which SANY analyzes where it is defined.
84
+ // getInstance gives what an INSTANCE in a LET or a proof instantiates.
81
85
  interface Unit {
82
86
  imported: Symbols;
87
+ getInstance: (instance: Instance) => InstancedModule;
83
88
  definitions: Map<string, Definition>;
84
89
  getKey: (definition: Definition) => string;
85
90
  location: SourceLocation;
@@ -283,6 +288,18 @@ const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
283
288
  end: { line: location.line, column: location.column + name.length - 1 },
284
289
  });
285
290
 
291
+ // SANY reports a name at its tokens, such as M ! ⊕ for M!\oplus.
292
+ const getSpelledNameRange = (
293
+ expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
294
+ name: string,
295
+ ): SourceRange => {
296
+ const selectorRange =
297
+ expression.kind === "OperatorReference" ? undefined : expression.selectorRanges?.at(-1);
298
+ return selectorRange
299
+ ? { module: selectorRange.module, start: expression.location, end: selectorRange.end }
300
+ : getNameRange(expression.location, expression.spelling ?? name);
301
+ };
302
+
286
303
  // SANY reports an operator symbol applied to operands as one it cannot resolve.
287
304
  const getFixity = (expression: Expression) => {
288
305
  if (expression.kind !== "Apply") return undefined;
@@ -292,10 +309,7 @@ const getFixity = (expression: Expression) => {
292
309
  };
293
310
 
294
311
  // SANY names an undefined operator symbol such as + differently from an undefined identifier.
295
- const isOperatorSymbol = (name: string) =>
296
- name[0].toLowerCase() === name[0].toUpperCase() &&
297
- name[0] !== "_" &&
298
- (name[0] < "0" || name[0] > "9");
312
+ const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
299
313
 
300
314
  const isStepName = (name: string) =>
301
315
  name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
@@ -712,10 +726,17 @@ const getSymbolArity = (
712
726
  const renamed = renamedArities.get(expression);
713
727
  if (renamed !== undefined) return renamed;
714
728
  if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
715
- if (checkReference(expression, scope, unit, false) || isStepName(name)) {
729
+ const local = lookupLocal(scope, name);
730
+ if (local?.isNonFact) {
731
+ unit.errors.push({
732
+ location: formatRange(getSpelledNameRange(expression, name)),
733
+ message: "Step number of non-expression step used as an expression.",
734
+ });
735
+ return undefined;
736
+ }
737
+ if (checkReference(expression, scope, unit, false) || (isStepName(name) && local)) {
716
738
  return expression.kind === "Apply" ? expression.args.length : 0;
717
739
  }
718
- const local = lookupLocal(scope, name);
719
740
  if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
720
741
  if (local) return local.arity;
721
742
  const symbol = findSymbol(name, unit);
@@ -736,20 +757,25 @@ const getSymbolArity = (
736
757
  if (isSelected) {
737
758
  return expression.kind === "Apply" ? expression.args.length : 0;
738
759
  }
739
- const range = formatRange(getNameRange(location, name === "-." ? "-" : name));
740
- if (!isOperatorSymbol(name)) {
760
+ const spelling = (expression.kind === "Identifier" ? undefined : expression.spelling) ?? name;
761
+ const range = formatRange(
762
+ expression.kind === "Apply" && spelling === "-."
763
+ ? getNameRange(location, "-")
764
+ : getSpelledNameRange(expression, name),
765
+ );
766
+ if (!isOperatorSymbol(name) || isStepName(name)) {
741
767
  unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
742
768
  return undefined;
743
769
  }
744
770
  unit.errors.push({
745
771
  location: range,
746
- message: `Could not find declaration or definition of symbol '${name}'.`,
772
+ message: `Could not find declaration or definition of symbol '${spelling}'.`,
747
773
  });
748
774
  const fixity = getFixity(expression);
749
775
  if (fixity && location.range) {
750
776
  unit.errors.push({
751
777
  location: formatRange(location.range),
752
- message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
778
+ message: `Couldn't resolve ${fixity} operator symbol \`${spelling}'.`,
753
779
  });
754
780
  }
755
781
  return undefined;
@@ -820,6 +846,7 @@ const resolveDefinition = (
820
846
  unit,
821
847
  );
822
848
  markRecursiveConstructions(definition.body, name);
849
+ definition.isSelfReferential = isReferenced(definition.body, name);
823
850
  } else resolveExpression(definition.body, inner, unit);
824
851
  if (current && isDeclared) {
825
852
  current.isPending = false;
@@ -836,10 +863,13 @@ const resolveDefinition = (
836
863
  );
837
864
  };
838
865
 
866
+ // SANY analyzes an INSTANCE in a LET where it appears, before the definitions that follow it, which
867
+ // tla-ts generates at its location and analyzes without reporting.
839
868
  const resolveDefinitions = (
840
869
  definitions: Definition[],
841
870
  scope: LocalName | undefined,
842
871
  unit: Unit,
872
+ instances: Instance[] = [],
843
873
  ) => {
844
874
  const recursive = new Set<Binding>();
845
875
  const declared = definitions
@@ -857,7 +887,20 @@ const resolveDefinitions = (
857
887
  recursive.add(local);
858
888
  return local;
859
889
  }, scope);
860
- return definitions.reduce<LocalName | undefined>((current, definition) => {
890
+ const pending = unit.isGenerated ? [] : [...instances];
891
+ const resolveInstances = (
892
+ current: LocalName | undefined,
893
+ isDue: (instance: Instance) => boolean,
894
+ ) => {
895
+ const count = pending.findIndex((instance) => !isDue(instance));
896
+ return pending
897
+ .splice(0, count === -1 ? pending.length : count)
898
+ .reduce((parent, instance) => resolveLocalInstance(instance, parent, unit), current);
899
+ };
900
+ const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
901
+ const current = isKey(definition.name)
902
+ ? previous
903
+ : resolveInstances(previous, (instance) => !isBefore(definition.location, instance.location));
861
904
  const local: LocalName = {
862
905
  name: definition.name,
863
906
  arity: getArity(definition),
@@ -866,16 +909,55 @@ const resolveDefinitions = (
866
909
  parent: current,
867
910
  };
868
911
  if (isInstantiated(definition)) return local;
912
+ const isInstanced = instances.some((instance) => instance.location === definition.location);
869
913
  const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
870
- return resolveDefinition(
871
- definition,
872
- current,
873
- isGenerated ? { ...unit, isGenerated } : unit,
874
- recursive,
875
- )
914
+ const inner = isInstanced
915
+ ? { ...unit, isGenerated: true, errors: [], warnings: [] }
916
+ : isGenerated
917
+ ? { ...unit, isGenerated }
918
+ : unit;
919
+ return resolveDefinition(definition, current, inner, recursive)
876
920
  ? { ...local, params: getParamArities(definition) }
877
921
  : current;
878
922
  }, declared);
923
+ return resolveInstances(resolved, () => true);
924
+ };
925
+
926
+ // The INSTANCE that a name such as K!C starts with, whose arguments SANY requires at its name.
927
+ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
928
+ const prefix = name.split("!")[0];
929
+ const binding =
930
+ prefix === name
931
+ ? undefined
932
+ : (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
933
+ return binding?.kind === "Instance" ? { name: prefix, arity: binding.arity } : undefined;
934
+ };
935
+
936
+ // Generator.selectorToNode reports an INSTANCE given the wrong number of arguments at its name,
937
+ // as in K!C or K(1, 2)!C, and goes on without analyzing them. Tells whether it reports one.
938
+ const isInstanceArityReported = (
939
+ expression: Expression,
940
+ scope: LocalName | undefined,
941
+ unit: Unit,
942
+ ) => {
943
+ if (expression.kind !== "Identifier" && expression.kind !== "Apply") return false;
944
+ const instance = findInstance(
945
+ expression.kind === "Identifier" ? expression.name : expression.operator,
946
+ scope,
947
+ unit,
948
+ );
949
+ const count = expression.kind === "Apply" ? (expression.argumentCounts?.[0] ?? 0) : 0;
950
+ if (!instance || count === instance.arity) return false;
951
+ const range =
952
+ (expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ??
953
+ expression.location.range;
954
+ if (range) {
955
+ unit.errors.push({
956
+ location: formatRange(range),
957
+ message: `The operator ${instance.name} requires ${instance.arity} arguments.`,
958
+ });
959
+ }
960
+ return true;
879
961
  };
880
962
 
881
963
  // Generator.generateExprOrOpArg analyzes an argument as an expression, unless a defined operator
@@ -888,16 +970,15 @@ const resolveApply = (
888
970
  scope: LocalName | undefined,
889
971
  unit: Unit,
890
972
  ) => {
973
+ if (isInstanceArityReported(expression, scope, unit)) return;
891
974
  const operator = expression.operator;
892
975
  const callee = findBinding(operator, scope, unit);
893
976
  const arity = getSymbolArity(expression, operator, scope, unit);
894
977
  if (arity === undefined) return;
895
978
  if (arity !== expression.args.length) {
896
979
  unit.errors.push({
897
- location: formatRange(
898
- expression.argumentsRange ?? getNameRange(expression.location, operator),
899
- ),
900
- message: `The operator ${operator} requires ${arity} arguments.`,
980
+ location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
981
+ message: `The operator ${operator} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
901
982
  });
902
983
  return;
903
984
  }
@@ -912,13 +993,15 @@ const resolveApply = (
912
993
  expression.args
913
994
  .map((arg, index) => {
914
995
  const expected = params[index] ?? 0;
915
- // A prefix operator argument such as SUBSET, which tla-ts reads as a LAMBDA, has no range.
916
996
  const range = arg.location.range;
917
997
  if (expected === 0) {
918
- if (arg.kind === "Lambda" && range) {
998
+ // SANY fails on an operator here with an internal error, so tla-ts words it as it does for
999
+ // a name.
1000
+ if (arg.kind === "Lambda" && arg.operator !== undefined) {
1001
+ report(range, `The operator ${arg.operator} requires 1 arguments.`);
1002
+ } else if (arg.kind === "Lambda" && range) {
919
1003
  report(range, "LAMBDA expression used where an expression is required.");
920
1004
  } else if (arg.kind === "OperatorReference") {
921
- // SANY fails here with an internal error, so tla-ts words it as it does for a name.
922
1005
  const argArity = getSymbolArity(arg, arg.operator, scope, unit);
923
1006
  if (argArity !== undefined) {
924
1007
  report(range, `The operator ${arg.operator} requires ${argArity} arguments.`);
@@ -935,9 +1018,9 @@ const resolveApply = (
935
1018
  range,
936
1019
  `Expected arity ${expected} but found operator of arity ${arg.params.length}.`,
937
1020
  );
938
- } else if (!isMatched && range) {
1021
+ } else if (!isMatched && range && arg.operator === undefined) {
939
1022
  report(
940
- expression.selectorRanges?.at(-1) ?? getNameRange(expression.location, operator),
1023
+ expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator),
941
1024
  `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
942
1025
  );
943
1026
  }
@@ -947,6 +1030,16 @@ const resolveApply = (
947
1030
  resolveExpression(arg, scope, unit);
948
1031
  return true;
949
1032
  }
1033
+ // Generator.selectorToNode: a name such as K(1)!C names an operator, but none of its parts
1034
+ // may take arguments.
1035
+ if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
1036
+ const index = arg.argumentCounts.findIndex((count) => count > 0);
1037
+ report(
1038
+ arg.selectorRanges?.[index],
1039
+ `Selector \`${arg.operator.split("!")[index]}' should not have argument(s).`,
1040
+ );
1041
+ return false;
1042
+ }
950
1043
  if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
951
1044
  report(
952
1045
  range,
@@ -959,7 +1052,7 @@ const resolveApply = (
959
1052
  const argArity = getSymbolArity(arg, name, scope, unit);
960
1053
  if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
961
1054
  report(
962
- getNameRange(arg.location, name),
1055
+ getSpelledNameRange(arg, name),
963
1056
  `Expected arity ${expected} but found operator of arity ${argArity}.`,
964
1057
  );
965
1058
  }
@@ -1012,11 +1105,12 @@ const resolveExpression = (
1012
1105
  case "ExceptAt":
1013
1106
  return;
1014
1107
  case "Identifier": {
1108
+ if (isInstanceArityReported(expression, scope, unit)) return;
1015
1109
  const name = expression.name;
1016
1110
  const arity = getSymbolArity(expression, name, scope, unit);
1017
1111
  if (arity !== undefined && arity > 0) {
1018
1112
  unit.errors.push({
1019
- location: formatRange(getNameRange(expression.location, name)),
1113
+ location: formatRange(getSpelledNameRange(expression, name)),
1020
1114
  message: `The operator ${name} requires ${arity} arguments.`,
1021
1115
  });
1022
1116
  }
@@ -1056,7 +1150,7 @@ const resolveExpression = (
1056
1150
  case "Let":
1057
1151
  resolveExpression(
1058
1152
  expression.body,
1059
- resolveDefinitions(expression.definitions, scope, unit),
1153
+ resolveDefinitions(expression.definitions, scope, unit, expression.instances),
1060
1154
  unit,
1061
1155
  );
1062
1156
  return;
@@ -1161,10 +1255,21 @@ const resolveExpression = (
1161
1255
  // and analyzes any other fact as an expression.
1162
1256
  const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
1163
1257
  facts.forEach((fact) => {
1164
- if (fact.kind === "ModuleFact" || checkReference(fact, scope, unit, true)) return;
1258
+ if (
1259
+ fact.kind === "ModuleFact" ||
1260
+ isInstanceArityReported(fact, scope, unit) ||
1261
+ checkReference(fact, scope, unit, true)
1262
+ ) {
1263
+ return;
1264
+ }
1165
1265
  const name =
1166
1266
  fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
1167
- if (!unit.facts.has(name)) resolveExpression(fact, scope, unit);
1267
+ if (fact.kind === "Identifier" && lookupLocal(scope, name)?.isNonFact) {
1268
+ unit.errors.push({
1269
+ location: formatRange(getSpelledNameRange(fact, name)),
1270
+ message: "Step number of non-fact used as a fact",
1271
+ });
1272
+ } else if (!unit.facts.has(name)) resolveExpression(fact, scope, unit);
1168
1273
  });
1169
1274
 
1170
1275
  const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
@@ -1223,6 +1328,21 @@ const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, ou
1223
1328
  proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
1224
1329
  };
1225
1330
 
1331
+ // A LET or a step binds the name of the INSTANCE after the names it adds.
1332
+ const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1333
+ const instanced = resolveInstance(instance, scope, unit);
1334
+ return instance.name === undefined
1335
+ ? instanced
1336
+ : bindName(
1337
+ instanced,
1338
+ instance.name,
1339
+ { kind: "Instance", arity: instance.params.length, range: instance.location.range },
1340
+ unit,
1341
+ );
1342
+ };
1343
+
1344
+ // Generator.generateProof binds the name of a step after its statement and before its proof, or
1345
+ // after the step where it takes no proof.
1226
1346
  const resolveStep = (
1227
1347
  step: ProofStep,
1228
1348
  scope: LocalName | undefined,
@@ -1233,6 +1353,12 @@ const resolveStep = (
1233
1353
  step.name === undefined
1234
1354
  ? unit
1235
1355
  : { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
1356
+ const bindStep = (parent: LocalName | undefined): LocalName | undefined =>
1357
+ step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent };
1358
+ const bindNonFactStep = (parent: LocalName | undefined): LocalName | undefined =>
1359
+ step.name === undefined
1360
+ ? parent
1361
+ : { name: step.name, arity: 0, kind: "Theorem", parent, isNonFact: true };
1236
1362
  switch (body.kind) {
1237
1363
  // The NEW symbols of a SUFFICES ASSUME are in scope after its proof, and those of any other
1238
1364
  // ASSUME in its proof.
@@ -1252,29 +1378,35 @@ const resolveStep = (
1252
1378
  case "Use":
1253
1379
  case "Hide":
1254
1380
  checkFacts(body.facts, scope, unit);
1255
- return scope;
1381
+ return bindNonFactStep(scope);
1382
+ case "Instance":
1383
+ return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
1256
1384
  case "Define":
1257
- return resolveDefinitions(
1258
- body.definitions.flatMap((definition) =>
1259
- definition.kind === "Instance" ? [] : [definition],
1385
+ return bindNonFactStep(
1386
+ body.definitions.reduce(
1387
+ (current, definition) =>
1388
+ definition.kind === "Instance"
1389
+ ? resolveLocalInstance(definition, current, stepUnit)
1390
+ : resolveDefinitions([definition], current, unit),
1391
+ scope,
1260
1392
  ),
1261
- scope,
1262
- unit,
1263
1393
  );
1264
1394
  case "Have":
1265
1395
  case "Case":
1266
1396
  resolveExpression(body.expression, scope, stepUnit);
1267
- resolveProof(step.proof, scope, unit);
1268
- return scope;
1397
+ resolveProof(step.proof, bindStep(scope), unit);
1398
+ return bindStep(scope);
1269
1399
  case "Witness":
1270
1400
  body.expressions.forEach((expression) => resolveExpression(expression, scope, stepUnit));
1271
- return scope;
1401
+ return bindStep(scope);
1272
1402
  case "Take":
1273
- return bindNames(
1274
- resolveBounds(body.bounds, scope, stepUnit),
1275
- body.names,
1276
- body.nameRanges,
1277
- stepUnit,
1403
+ return bindStep(
1404
+ bindNames(
1405
+ resolveBounds(body.bounds, scope, stepUnit),
1406
+ body.names,
1407
+ body.nameRanges,
1408
+ stepUnit,
1409
+ ),
1278
1410
  );
1279
1411
  case "Pick": {
1280
1412
  const picked = bindNames(
@@ -1287,12 +1419,12 @@ const resolveStep = (
1287
1419
  ...stepUnit,
1288
1420
  labelParams: [...stepUnit.labelParams, ...getBoundNames(body.bounds), ...body.names],
1289
1421
  });
1290
- resolveProof(step.proof, scope, unit);
1291
- return picked;
1422
+ resolveProof(step.proof, bindStep(scope), unit);
1423
+ return bindStep(picked);
1292
1424
  }
1293
1425
  default:
1294
- resolveProof(step.proof, scope, unit);
1295
- return scope;
1426
+ resolveProof(step.proof, bindStep(scope), unit);
1427
+ return bindStep(scope);
1296
1428
  }
1297
1429
  };
1298
1430
 
@@ -1313,6 +1445,14 @@ const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1313
1445
  if (goal) goal.isInProof = false;
1314
1446
  };
1315
1447
 
1448
+ // The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
1449
+ // module that instantiates it.
1450
+ export interface InstancedModule {
1451
+ declarations: Declaration[];
1452
+ symbols: Symbols;
1453
+ instancer: string;
1454
+ }
1455
+
1316
1456
  // What SANY reports at a location as it links a module, such as a conflict between the modules it
1317
1457
  // extends.
1318
1458
  export interface ModuleReport {
@@ -1323,10 +1463,12 @@ export interface ModuleReport {
1323
1463
 
1324
1464
  // bound holds the names in scope where the module starts: the built-in operators, the names of the
1325
1465
  // module that encloses it, and those of the modules it extends. instanced holds the names each
1326
- // INSTANCE adds, facts the theorems and assumptions the modules it extends or instantiates export,
1327
- // and warnings the warnings SANY reports for the spec.
1466
+ // INSTANCE adds, getInstance what an INSTANCE in a LET or a proof instantiates, facts the theorems
1467
+ // and assumptions the modules it extends or instantiates export, and warnings the warnings SANY
1468
+ // reports for the spec.
1328
1469
  export interface ModuleUnit {
1329
1470
  imported: Symbols;
1471
+ getInstance: (instance: Instance) => InstancedModule;
1330
1472
  bound: Symbols;
1331
1473
  definitions: Map<string, Definition>;
1332
1474
  getKey: (definition: Definition) => string;
@@ -1359,47 +1501,25 @@ const createSymbols = (module: Module, imported: Symbols) => {
1359
1501
  };
1360
1502
 
1361
1503
  // Generator.processSubst analyzes the explicit substitutions in the order they appear, then the
1362
- // implicit ones, and then reports each declaration left without one. tla-ts reports what SANY goes on
1363
- // after with the INSTANCE.
1364
- export const resolveSubstitutions = (
1504
+ // implicit ones, and then reports each declaration left without one. The parameters of the INSTANCE
1505
+ // are in scope.
1506
+ const checkSubstitutions = (
1365
1507
  instance: Instance,
1366
1508
  declarations: Declaration[],
1367
- module: Module,
1368
- unit: ModuleUnit,
1509
+ instancer: string,
1510
+ outer: LocalName | undefined,
1511
+ unit: Unit,
1369
1512
  ) => {
1370
1513
  const scope = instance.params.reduce<LocalName | undefined>(
1371
1514
  (parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
1372
- undefined,
1515
+ outer,
1373
1516
  );
1374
- const imported = createSymbols(module, unit.imported);
1375
- const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
1376
- unit.reports.push(report);
1377
- const context: Unit = {
1378
- imported,
1379
- definitions: unit.definitions,
1380
- getKey: unit.getKey,
1381
- location: instance.location,
1382
- self: undefined,
1383
- facts: new Set(),
1384
- goals: new Map(),
1385
- bindings: new Map(
1386
- [...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
1387
- ),
1388
- errors: report.errors,
1389
- warnings: report.warnings,
1390
- hasLabelScope: false,
1391
- isLabelForbidden: false,
1392
- isInExcept: false,
1393
- labelParams: [],
1394
- labels: new Set(),
1395
- isGenerated: false,
1396
- };
1397
1517
  const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
1398
1518
  if (declaration.arity === 0 || expression.kind !== "Identifier") {
1399
- resolveExpression(expression, scope, context);
1519
+ resolveExpression(expression, scope, unit);
1400
1520
  return;
1401
1521
  }
1402
- const arity = getSymbolArity(expression, expression.name, scope, context);
1522
+ const arity = getSymbolArity(expression, expression.name, scope, unit);
1403
1523
  if (arity !== undefined && arity !== declaration.arity) {
1404
1524
  throw new TlaSyntaxError(
1405
1525
  `An operator must be substituted for symbol '${declaration.name}', and it must have arity ${declaration.arity} in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`,
@@ -1411,7 +1531,7 @@ export const resolveSubstitutions = (
1411
1531
  const name = substitution.name;
1412
1532
  const declaration = declarations.find((item) => item.name === name);
1413
1533
  if (!declaration) {
1414
- report.errors.push({
1534
+ unit.errors.push({
1415
1535
  location: formatRange(substitution.nameRange),
1416
1536
  message: `Identifier '${name}' is not a legal target of a substitution. \nA legal target must be a declared CONSTANT or VARIABLE in the module being instantiated. \n(Also, check for warnings about multiple declarations of this same identifier.)`,
1417
1537
  });
@@ -1422,7 +1542,7 @@ export const resolveSubstitutions = (
1422
1542
  const range = expression.location.range;
1423
1543
  if (!substitutions.has(name)) substitutions.set(name, expression);
1424
1544
  else if (range) {
1425
- report.errors.push({
1545
+ unit.errors.push({
1426
1546
  location: formatRange(range),
1427
1547
  message: `Multiple substitutions for symbol '${name}' in substitution.`,
1428
1548
  });
@@ -1430,7 +1550,7 @@ export const resolveSubstitutions = (
1430
1550
  });
1431
1551
  const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
1432
1552
  const isMissing = (declaration: Declaration) =>
1433
- !lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, context);
1553
+ !lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, unit);
1434
1554
  implicit
1435
1555
  .filter((declaration) => !isMissing(declaration))
1436
1556
  .forEach((declaration) => {
@@ -1446,15 +1566,59 @@ export const resolveSubstitutions = (
1446
1566
  const range = instance.location.range;
1447
1567
  const declared = declaration.location.range;
1448
1568
  if (range && declared) {
1449
- report.errors.push({
1569
+ unit.errors.push({
1450
1570
  location: formatRange(range),
1451
- message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${module.name}.`,
1571
+ message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
1452
1572
  });
1453
1573
  }
1454
1574
  });
1455
1575
  return substitutions;
1456
1576
  };
1457
1577
 
1578
+ // Generator.processSubst analyzes an INSTANCE in a LET or a proof, which then binds the names the
1579
+ // INSTANCE adds.
1580
+ const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1581
+ const instanced = unit.getInstance(instance);
1582
+ checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
1583
+ return [...instanced.symbols].reduce(
1584
+ (parent, [name, symbol]) => bindName(parent, name, symbol, unit),
1585
+ scope,
1586
+ );
1587
+ };
1588
+
1589
+ // tla-ts reports what SANY goes on after with an INSTANCE of the module.
1590
+ export const resolveSubstitutions = (
1591
+ instance: Instance,
1592
+ declarations: Declaration[],
1593
+ module: Module,
1594
+ unit: ModuleUnit,
1595
+ ) => {
1596
+ const imported = createSymbols(module, unit.imported);
1597
+ const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
1598
+ unit.reports.push(report);
1599
+ return checkSubstitutions(instance, declarations, module.name, undefined, {
1600
+ imported,
1601
+ getInstance: unit.getInstance,
1602
+ definitions: unit.definitions,
1603
+ getKey: unit.getKey,
1604
+ location: instance.location,
1605
+ self: undefined,
1606
+ facts: new Set(),
1607
+ goals: new Map(),
1608
+ bindings: new Map(
1609
+ [...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
1610
+ ),
1611
+ errors: report.errors,
1612
+ warnings: report.warnings,
1613
+ hasLabelScope: false,
1614
+ isLabelForbidden: false,
1615
+ isInExcept: false,
1616
+ labelParams: [],
1617
+ labels: new Set(),
1618
+ isGenerated: false,
1619
+ });
1620
+ };
1621
+
1458
1622
  // SANY reports the errors of every unit it checks, in the order of the units in the module, after
1459
1623
  // the warnings it reports for the spec. A unit reports to its lists what SANY goes on after, and
1460
1624
  // throws the error that stops it. An error tla-ts cannot word as SANY does yields to the errors the
@@ -1532,6 +1696,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1532
1696
  hasLabelScope: boolean,
1533
1697
  ): Unit => ({
1534
1698
  imported: symbols,
1699
+ getInstance: unit.getInstance,
1535
1700
  definitions: unit.definitions,
1536
1701
  getKey: unit.getKey,
1537
1702
  location,