tla-ts 0.0.10-dev.b4a856304 → 0.0.10-dev.f16792fed

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 (72) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +34 -0
  3. package/dist/index.d.ts +34 -0
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +48 -48
  8. package/dist/prove.js +29 -29
  9. package/dist/run.cjs +40 -40
  10. package/dist/run.js +31 -31
  11. package/dist/transfer.cjs +36 -36
  12. package/dist/transfer.js +40 -40
  13. package/package.json +1 -1
  14. package/src/ast.ts +0 -5
  15. package/src/bags.ts +2 -5
  16. package/src/builtins.ts +1 -8
  17. package/src/check.ts +128 -193
  18. package/src/cli/options.ts +1 -1
  19. package/src/cli/run.ts +0 -2
  20. package/src/cli/runtime-invariants.ts +8 -7
  21. package/src/cli/shared.ts +0 -4
  22. package/src/cli/tlc-arguments.ts +24 -30
  23. package/src/cli/tlc-usage.ts +2 -3
  24. package/src/coverage.ts +3 -8
  25. package/src/evaluate.ts +26 -102
  26. package/src/java-hash.ts +7 -0
  27. package/src/java-overrides.ts +4 -9
  28. package/src/json-reader.ts +1 -2
  29. package/src/json.ts +4 -11
  30. package/src/levels.ts +7 -15
  31. package/src/lexer.ts +72 -83
  32. package/src/liveness-checker.ts +77 -106
  33. package/src/liveness.ts +10 -34
  34. package/src/parallel.ts +26 -33
  35. package/src/parser.ts +163 -226
  36. package/src/pcal/ast.ts +53 -65
  37. package/src/pcal/parse.ts +144 -191
  38. package/src/pcal/tokenize.ts +91 -115
  39. package/src/pcal/translate.ts +83 -98
  40. package/src/pcal/validate.ts +48 -50
  41. package/src/pretty-print.ts +6 -6
  42. package/src/randomization.ts +1 -4
  43. package/src/resolve.ts +12 -70
  44. package/src/runtime.ts +11 -18
  45. package/src/sany/builtins.ts +1 -3
  46. package/src/sany/generator.ts +74 -127
  47. package/src/sany/levels.ts +5 -9
  48. package/src/sany/lexer.ts +40 -44
  49. package/src/sany/linter.ts +0 -1
  50. package/src/sany/nodes.ts +4 -10
  51. package/src/search.ts +1 -1
  52. package/src/simulate.ts +50 -59
  53. package/src/spec-description.ts +23 -34
  54. package/src/standard-definitions.ts +0 -3
  55. package/src/standard-modules.ts +1 -12
  56. package/src/state-graph.ts +3 -15
  57. package/src/subexpressions.ts +38 -47
  58. package/src/successors.ts +14 -35
  59. package/src/symmetry.ts +4 -19
  60. package/src/tlc/fingerprint.ts +15 -21
  61. package/src/tlc/jar-data.ts +0 -1
  62. package/src/tlc/messages.ts +2 -21
  63. package/src/tlc/order.ts +22 -47
  64. package/src/tlc/random.ts +18 -30
  65. package/src/tlc/replay.ts +13 -33
  66. package/src/tlc/summary.ts +36 -45
  67. package/src/tlc/tlc-operators.ts +4 -14
  68. package/src/tlc/tlcext-operators.ts +11 -15
  69. package/src/tlc/trace-spec.ts +4 -9
  70. package/src/tlc/value-stream.ts +9 -13
  71. package/src/tlc/version.ts +0 -1
  72. package/src/values.ts +74 -186
@@ -165,6 +165,21 @@ const getThunk = (expression: Expression, scope: Scope | undefined) => {
165
165
  return binding?.kind === "Thunk" ? binding : undefined;
166
166
  };
167
167
 
168
+ const unwrapBinding = (
169
+ expression: Expression,
170
+ scope: Scope | undefined,
171
+ environment: Environment,
172
+ ) => {
173
+ if (expression.kind === "Let") {
174
+ return {
175
+ expression: expression.body,
176
+ scope: bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
177
+ };
178
+ }
179
+ const thunk = getThunk(expression, scope);
180
+ return thunk && { expression: thunk.expression, scope: thunk.scope };
181
+ };
182
+
168
183
  const getParameters = (closure: Closure | undefined, scope: Scope | undefined) =>
169
184
  (closure?.params ?? []).flatMap((param): [string, Value][] => {
170
185
  const binding = lookupScope(scope, param.name);
@@ -191,7 +206,6 @@ const getInstanceActions = (
191
206
  const callAction = (): SpecAction[] => [
192
207
  { name: instanceAction.name, location: expression.location, parameters: [], expression, scope },
193
208
  ];
194
- // TLC's action is the SubstInNode the instance puts around the operator's body.
195
209
  if (called && args.length === 0) {
196
210
  return [{ ...instanceAction, parameters: [], expression: called.body, scope: called.scope }];
197
211
  }
@@ -279,7 +293,6 @@ const getActions = (
279
293
  return getInstanceActions(instanceAction, expression, scope, called, environment);
280
294
  if (!called) return [createSplitAction()];
281
295
  const args = getArguments(expression);
282
- // TLC names an action after the operator it applies, as SANY names it in the spec.
283
296
  const calledName = isLocallyResolved(expression)
284
297
  ? called.name
285
298
  : (environment.instanceNames.get(getOperatorName(expression) ?? "") ?? called.name);
@@ -322,19 +335,11 @@ const describeSpecification = (
322
335
  ): void => {
323
336
  const recurse = (inner: Expression, innerScope: Scope | undefined, innerFrames = frames) =>
324
337
  describeSpecification(inner, innerScope, environment, description, next, innerFrames);
325
- // TLC wraps each conjunct it takes from an instance in the instance's SubstInNode.
326
338
  const createConjunct = (name: string, conjunct: Expression, conjunctScope: Scope | undefined) =>
327
339
  createAction(name, withSubstitutionFrames(conjunct, frames), conjunctScope);
328
- if (expression.kind === "Let") {
329
- recurse(
330
- expression.body,
331
- bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
332
- );
333
- return;
334
- }
335
- const thunk = getThunk(expression, scope);
336
- if (thunk) {
337
- recurse(thunk.expression, thunk.scope);
340
+ const unwrapped = unwrapBinding(expression, scope, environment);
341
+ if (unwrapped) {
342
+ recurse(unwrapped.expression, unwrapped.scope);
338
343
  return;
339
344
  }
340
345
  const called = resolveClosure(expression, scope, environment);
@@ -387,16 +392,9 @@ const describeProperty = (
387
392
  ): void => {
388
393
  const recurse = (inner: Expression, innerScope: Scope | undefined) =>
389
394
  describeProperty(configName, inner, innerScope, environment, description);
390
- if (expression.kind === "Let") {
391
- recurse(
392
- expression.body,
393
- bindDefinitions(expression.definitions, getConstantContext(environment, scope)),
394
- );
395
- return;
396
- }
397
- const thunk = getThunk(expression, scope);
398
- if (thunk) {
399
- recurse(thunk.expression, thunk.scope);
395
+ const unwrapped = unwrapBinding(expression, scope, environment);
396
+ if (unwrapped) {
397
+ recurse(unwrapped.expression, unwrapped.scope);
400
398
  return;
401
399
  }
402
400
  const called = resolveClosure(expression, scope, environment);
@@ -476,7 +474,6 @@ const getSpecDescription = (
476
474
  describeSpecification(specification.body, undefined, environment, description, next);
477
475
  next.forEach((item) =>
478
476
  description.actions.push(
479
- // TLC takes an instance's SubstInNode as one action, located at the instance.
480
477
  ...(item.frames.length > 0
481
478
  ? [
482
479
  {
@@ -528,14 +525,8 @@ export const getSubscriptVariables = (
528
525
  return expression.elements.every((element) => collect(element, innerScope));
529
526
  }
530
527
  if (expression.kind === "Label") return collect(expression.body, innerScope);
531
- if (expression.kind === "Let") {
532
- return collect(
533
- expression.body,
534
- bindDefinitions(expression.definitions, getConstantContext(environment, innerScope)),
535
- );
536
- }
537
- const thunk = getThunk(expression, innerScope);
538
- if (thunk) return collect(thunk.expression, thunk.scope);
528
+ const unwrapped = unwrapBinding(expression, innerScope, environment);
529
+ if (unwrapped) return collect(unwrapped.expression, unwrapped.scope);
539
530
  const argument =
540
531
  expression.kind === "Identifier" && isLocallyResolved(expression)
541
532
  ? lookupScope(innerScope, expression.name)
@@ -599,8 +590,6 @@ export const getSpecificationValue = (
599
590
  instanceActions: Map<string, InstanceAction>,
600
591
  ): Value => {
601
592
  const description = getSpecDescription(config, environment, instanceActions);
602
- // Describing a checked formula evaluates the constant definitions it names, so only
603
- // TLCGet("spec") describes them, not the actions TLC splits at startup.
604
593
  config.invariants.forEach((name) => {
605
594
  const definition = getOperatorDefinition(name, environment);
606
595
  if (definition) description.invariants.push(createAction(name, definition.body));
@@ -1,9 +1,6 @@
1
1
  import type { SourceRange } from "./ast";
2
2
  import type { OperatorLevels } from "./levels";
3
3
 
4
- // SANY's view of the dummy definitions in tla2sany/StandardModules of the operators tla-ts
5
- // implements natively: their levels, and where SANY reports a spec that defines one again.
6
-
7
4
  type Signature =
8
5
  | [string, number, number[], number[], number[]]
9
6
  | [string, number, number[], number[], number[], number[][], string[]];
@@ -87,7 +87,6 @@ const isSubsetOf = (left: Value[], right: Value) =>
87
87
  const BOOLEAN_SET = createSet([false, true]);
88
88
  markNormalized(BOOLEAN_SET);
89
89
 
90
- // A balanced merge compares each element about log2 of the set count times.
91
90
  const getBalancedUnion = (sets: SetValue[], start: number, end: number): SetValue => {
92
91
  if (end - start === 1) return sets[start];
93
92
  const middle = (start + end) >> 1;
@@ -100,11 +99,9 @@ type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
100
99
 
101
100
  const isMixedSet = (value: Value) => typeof value === "object" && value.kind === "MixedSet";
102
101
 
103
- // These keep the intersection and difference they compute normalized.
104
102
  const isNormalizedReducible = (set: ReducibleSet) =>
105
103
  set.kind === "Interval" || (set.kind === "Set" && set.isNormalized);
106
104
 
107
- // TLC combines a set enumeration or an interval with a set by testing each element's membership.
108
105
  const combineReducible = (
109
106
  operator: SetOperationValue["operator"],
110
107
  set: ReducibleSet,
@@ -165,7 +162,6 @@ export const CORE_OPERATORS: Builtin[] = [
165
162
  2,
166
163
  (args) => !isElementOf(getValue(args, 0, "\\notin"), getValue(args, 1, "\\notin")),
167
164
  ),
168
- // TLC tests each element of the left set for membership in the right value.
169
165
  defineBinary("\\subseteq", (left, right) => {
170
166
  if (!isSetValue(left)) {
171
167
  throw new TlaEvaluationError(`\\subseteq expects a set but received ${formatValue(left)}`);
@@ -191,7 +187,6 @@ export const CORE_OPERATORS: Builtin[] = [
191
187
  }
192
188
  return !someStreamedElement(left, (element) => !isElementOf(element, right));
193
189
  }),
194
- // TLC defers set operations on non-sets to enumerating or testing membership in the result.
195
190
  ...SET_OPERATORS.map((operator) =>
196
191
  defineOperator(operator, 2, (args) => {
197
192
  const left = getValue(args, 0, operator);
@@ -218,14 +213,12 @@ export const CORE_OPERATORS: Builtin[] = [
218
213
  : operator === "\\cap"
219
214
  ? getSortedIntersection
220
215
  : getSortedDifference;
221
- // The merge compares the elements, which TLC tests for membership one by one instead.
222
216
  return computeLikeTlcOnFailure(
223
217
  (finiteLeft, finiteRight) => {
224
218
  const leftSet = toFiniteSet(finiteLeft);
225
219
  const rightSet = toFiniteSet(finiteRight);
226
220
  const merged = merge(leftSet, rightSet);
227
221
  if (operator === "\\cup") {
228
- // TLC appends the right set's new elements to the left set's, unsorted.
229
222
  const written = getWrittenElements(leftSet);
230
223
  const added = getSortedDifference(rightSet, leftSet).elements;
231
224
  const isSorted =
@@ -268,7 +261,6 @@ export const CORE_OPERATORS: Builtin[] = [
268
261
  if (!isEnumeratedSet(outer) || sets.length !== written.length) {
269
262
  return markStreamedSet({ kind: "Union", base: outer });
270
263
  }
271
- // TLC adds each set's elements in written order, and only those not yet in the union.
272
264
  const added = sets.flatMap(getWrittenElements);
273
265
  const getUnionLikeTlc = () => {
274
266
  const union: MixedSetValue = { kind: "MixedSet", elements: [] };
@@ -298,7 +290,6 @@ export const CORE_OPERATORS: Builtin[] = [
298
290
 
299
291
  const NEG_SIGNATURE = getJavaSignature("Integers", "Neg", "IntValue", 1);
300
292
 
301
- // TLC computes this arithmetic in tlc2.module.Naturals, or Integers if loaded, named in errors.
302
293
  const createArithmeticOperators = (module: string) => {
303
294
  const dotDotSignature = getJavaSignature(module, "DotDot", "IntervalValue", 2);
304
295
  return [
@@ -341,14 +332,12 @@ const NATURALS_ARITHMETIC = createArithmeticOperators("Naturals");
341
332
 
342
333
  const INTEGERS_ARITHMETIC = createArithmeticOperators("Integers");
343
334
 
344
- // TLC loads Integers for a trace exploration spec, whose methods then replace these.
345
335
  export const getTraceSpecArithmetic = (builtin: Builtin) => {
346
336
  const index = NATURALS_ARITHMETIC.indexOf(builtin);
347
337
  return index === -1 ? undefined : INTEGERS_ARITHMETIC[index];
348
338
  };
349
339
 
350
- // Naturals#GEQ names itself > in its errors, and Integers#GT returns a BoolValue. Only a
351
- // spec that extends Integers gets Integers' comparisons.
340
+ // Naturals#GEQ names itself > in its errors, and Integers#GT returns a BoolValue.
352
341
  const createComparisonOperators = (module: string) => {
353
342
  const getSignature = (method: string, result = "tlc2.value.IBoolValue") =>
354
343
  `public static ${result} tlc2.module.${module}.${method}(tlc2.value.impl.Value,tlc2.value.impl.Value)`;
@@ -1,5 +1,6 @@
1
1
  // A port of TLC's -dump writers, tlc2.util.StateWriter and tlc2.util.DotStateWriter.
2
2
 
3
+ import { getJavaHashCode } from "./java-hash";
3
4
  import type { TlcVersion } from "./tlc/version";
4
5
 
5
6
  export interface StateDump {
@@ -52,8 +53,6 @@ const INITIAL_CAPACITY = 16;
52
53
  const getCapacity = (size: number, capacity = INITIAL_CAPACITY): number =>
53
54
  size <= capacity * LOAD_FACTOR ? capacity : getCapacity(size, capacity * 2);
54
55
 
55
- // The order in which a java.util.HashMap iterates over keys put in this order: by bucket, and in
56
- // insertion order within a bucket. A bucket only becomes a tree once 8 keys collide in it.
57
56
  const getJavaHashOrder = <Key>(keys: Key[], getHash: (key: Key) => number) => {
58
57
  const mask = getCapacity(keys.length) - 1;
59
58
  const getBucket = (key: Key) => {
@@ -69,12 +68,6 @@ const getJavaHashOrder = <Key>(keys: Key[], getHash: (key: Key) => number) => {
69
68
  const getLongHash = (value: bigint) =>
70
69
  Number(BigInt.asIntN(32, value ^ (BigInt.asUintN(64, value) >> 32n)));
71
70
 
72
- const getStringHash = (text: string) =>
73
- [...Array(text.length).keys()].reduce(
74
- (hash, index) => (Math.imul(31, hash) + text.charCodeAt(index)) | 0,
75
- 0,
76
- );
77
-
78
71
  const escapeDotString = (text: string) =>
79
72
  text.replaceAll("\\", "\\\\").replaceAll('"', '\\"').trim();
80
73
 
@@ -90,14 +83,11 @@ const createPlainWriter = (dump: StateDump, writeFile: WriteFile): StateWriter =
90
83
  if (!isSeen) writeState(successor);
91
84
  },
92
85
  writeConstrained: () => {},
93
- // TLC 1.7.4 writes the state again, as it takes the stuttering step for one to a new state.
94
86
  writeStuttering: writeState,
95
87
  close: () => writeFile?.(dump.path, states.join("")),
96
88
  };
97
89
  };
98
90
 
99
- // TLC 1.7.4 draws its nodes as ellipses, gives them no tooltip, and labels an edge with the name
100
- // of its action alone.
101
91
  const createDotWriter = (
102
92
  dump: StateDump,
103
93
  writeFile: WriteFile,
@@ -114,8 +104,7 @@ const createDotWriter = (
114
104
  ];
115
105
  const ranks = new Map<number, Set<bigint>>();
116
106
  const colors = new Map<string, number>();
117
- // TLC's DotStateWriter keys an edge by the XOR of its fingerprints, which merges every
118
- // stuttering step and an edge with its reverse, so tla-ts keys it by both.
107
+ // Deviation from TLC: it keys an edge by the XOR of its fingerprints, which merges distinct edges.
119
108
  const edges = new Set<string>();
120
109
  const isDuplicate = (state: GraphState, successor: GraphState) => {
121
110
  if (!dump.strict) return false;
@@ -128,7 +117,6 @@ const createDotWriter = (
128
117
  const rank = ranks.get(state.level) ?? new Set();
129
118
  ranks.set(state.level, rank.add(state.fingerprint));
130
119
  };
131
- // TLC colors an edge by the name of its action, numbering the names from 2 as it finds them.
132
120
  const getColor = (action: GraphAction | undefined) => {
133
121
  if (!action) return 1;
134
122
  const color = colors.get(action.name) ?? colors.size + 2;
@@ -139,7 +127,7 @@ const createDotWriter = (
139
127
  [
140
128
  `subgraph cluster_legend {graph[style=bold];label = "Next State Actions" style="solid"\n`,
141
129
  `node [ labeljust="l",colorscheme="${COLOR_SCHEME}",style=filled,shape=record ]\n`,
142
- ...getJavaHashOrder([...colors.keys()], getStringHash).map(
130
+ ...getJavaHashOrder([...colors.keys()], getJavaHashCode).map(
143
131
  (name) => `${name.replaceAll("!", ":")} [label="${name}",fillcolor=${colors.get(name)}]\n`,
144
132
  ),
145
133
  "}",
@@ -149,9 +149,6 @@ const applyDefinition = (definition: Definition, selection: Selection) => {
149
149
  return createApplication({ ...selection, params: [...selection.params, ...params] }, reference);
150
150
  };
151
151
 
152
- // Generator.selectorToNode reports the first part of a name that selects nothing, after it analyzes
153
- // the arguments of the parts before, and goes on after the name. An operator or a label must be given
154
- // all its arguments, except in a name that an operator argument may select whole.
155
152
  const selectSubexpression = (
156
153
  selectors: Selector[],
157
154
  lookup: Lookup,
@@ -160,25 +157,27 @@ const selectSubexpression = (
160
157
  ): Expression => {
161
158
  const location = reference.location;
162
159
  const selection: Selection = { params: [], args: [], lets: [], location };
163
- const state = { index: 0, current: undefined as Expression | undefined, isFailed: false };
160
+ let selectorIndex = 0;
161
+ let current = undefined as Expression | undefined;
162
+ let isFailed = false;
164
163
 
165
164
  const report = (range: SourceRange | undefined, message: string) => {
166
165
  if (!range) throw createError(message, location);
167
166
  selectionErrors.set(reference, {
168
167
  args: selectors
169
- .slice(0, state.index)
168
+ .slice(0, selectorIndex)
170
169
  .flatMap((selector) => ("args" in selector ? selector.args : [])),
171
170
  error: { location: formatRange(range), message },
172
171
  });
173
- state.isFailed = true;
172
+ isFailed = true;
174
173
  return undefined;
175
174
  };
176
175
 
177
176
  const findDefinition = (local: Definition[] | undefined) => {
178
177
  const names: string[] = [];
179
178
  const args: Expression[] = [];
180
- while (state.index < selectors.length) {
181
- const selector = selectors[state.index];
179
+ while (selectorIndex < selectors.length) {
180
+ const selector = selectors[selectorIndex];
182
181
  if (selector.kind !== "Name") throw createError("Expected an operator name", location);
183
182
  names.push(selector.name);
184
183
  args.push(...selector.args);
@@ -188,7 +187,7 @@ const selectSubexpression = (
188
187
  : lookup(name);
189
188
  if (definition)
190
189
  return { definition, name, args, earlier: args.length - selector.args.length };
191
- state.index++;
190
+ selectorIndex++;
192
191
  }
193
192
  throw createError(`Unknown operator ${names.join("!")}`, location);
194
193
  };
@@ -197,10 +196,10 @@ const selectSubexpression = (
197
196
  const found = findDefinition(local);
198
197
  const definition = found.definition;
199
198
  const arity = getArity(definition);
200
- const isLast = state.index === selectors.length - 1;
199
+ const isLast = selectorIndex === selectors.length - 1;
201
200
  if (found.args.length !== arity && (!isOperator || found.args.length !== 0)) {
202
201
  return report(
203
- reference.argumentRanges?.[state.index] ?? location.range,
202
+ reference.argumentRanges?.[selectorIndex] ?? location.range,
204
203
  `The operator ${found.name} requires ${arity - found.earlier} arguments.`,
205
204
  );
206
205
  }
@@ -210,17 +209,17 @@ const selectSubexpression = (
210
209
  }
211
210
  selection.params.push(...definition.params);
212
211
  selection.args.push(...found.args);
213
- state.current = definition.body;
214
- state.index++;
212
+ current = definition.body;
213
+ selectorIndex++;
215
214
  return undefined;
216
215
  };
217
216
 
218
217
  const followLabels = () => {
219
- while (state.index < selectors.length && !state.isFailed) {
220
- const selector = selectors[state.index];
218
+ while (selectorIndex < selectors.length && !isFailed) {
219
+ const selector = selectors[selectorIndex];
221
220
  if (selector.kind !== "Name") return;
222
- const range = reference.selectorRanges?.[state.index];
223
- const found = state.current && findLabel(state.current, selector.name, []);
221
+ const range = reference.selectorRanges?.[selectorIndex];
222
+ const found = current && findLabel(current, selector.name, []);
224
223
  if (!found) return report(range, `Cannot find label \`${selector.name}'.`);
225
224
  const params = found.label.params;
226
225
  if (!isOperator && selector.args.length !== params.length) {
@@ -229,17 +228,16 @@ const selectSubexpression = (
229
228
  selection.lets.push(...found.lets);
230
229
  selection.params.push(...params.map((name) => ({ name, arity: 0 })));
231
230
  selection.args.push(...selector.args);
232
- state.current = found.label.body;
233
- state.index++;
231
+ current = found.label.body;
232
+ selectorIndex++;
234
233
  }
235
234
  };
236
235
 
237
236
  const selectOperand = (selector: Selector) => {
238
- const current = state.current;
239
237
  if (!current) throw createError("Subexpression selection failed", location);
240
238
  if (selector.kind === "Colon") return;
241
239
  if (current.kind === "Label") {
242
- state.current = current.body;
240
+ current = current.body;
243
241
  selectOperand(selector);
244
242
  return;
245
243
  }
@@ -248,14 +246,14 @@ const selectSubexpression = (
248
246
  throw createError("A LET/IN expression has only one operand", location);
249
247
  }
250
248
  selection.lets.push(current.definitions);
251
- state.current = current.body;
249
+ current = current.body;
252
250
  return;
253
251
  }
254
252
  const binder = getBinder(current);
255
253
  if (binder && (selector.kind === "Bound" || selector.kind === "At")) {
256
254
  selection.params.push(...binder.names.map((name) => ({ name, arity: 0 })));
257
255
  if (selector.kind === "Bound") selection.args.push(...selector.args);
258
- state.current = binder.body;
256
+ current = binder.body;
259
257
  return;
260
258
  }
261
259
  if (current.kind === "Case" && selector.kind === "Operand") {
@@ -264,15 +262,15 @@ const selectSubexpression = (
264
262
  ...(current.other ? [{ condition: undefined, body: current.other }] : []),
265
263
  ];
266
264
  const arm = arms[getOperandIndex(selector.operand, arms.length)];
267
- const next = selectors[state.index + 1];
265
+ const next = selectors[selectorIndex + 1];
268
266
  if (!arm || next?.kind !== "Operand") {
269
267
  throw createError("A subexpression of CASE must have the form !i!j", location);
270
268
  }
271
- state.index++;
269
+ selectorIndex++;
272
270
  const part = getOperandIndex(next.operand, 2);
273
271
  const chosen = part === 0 ? arm.condition : part === 1 ? arm.body : undefined;
274
272
  if (!chosen) throw createError("Nonexistent CASE operand", location);
275
- state.current = chosen;
273
+ current = chosen;
276
274
  return;
277
275
  }
278
276
  if (selector.kind !== "Operand")
@@ -280,23 +278,23 @@ const selectSubexpression = (
280
278
  const operands = binder ? binder.sets : getOperands(current);
281
279
  const operand = operands?.[getOperandIndex(selector.operand, operands.length)];
282
280
  if (!operand) throw createError("Nonexistent operand specified by the selector", location);
283
- state.current = operand;
281
+ current = operand;
284
282
  };
285
283
 
286
284
  const first = enterDefinition(undefined);
287
- if (state.isFailed) return reference;
285
+ if (isFailed) return reference;
288
286
  if (first) return applyDefinition(first.definition, { ...selection, args: first.args });
289
287
  followLabels();
290
- while (state.index < selectors.length && !state.isFailed) {
291
- const selector = selectors[state.index];
288
+ while (selectorIndex < selectors.length && !isFailed) {
289
+ const selector = selectors[selectorIndex];
292
290
  if (selector.kind === "Name") {
293
- while (state.current?.kind === "Label") state.current = state.current.body;
294
- if (state.current?.kind !== "Let") {
291
+ while (current?.kind === "Label") current = current.body;
292
+ if (current?.kind !== "Let") {
295
293
  throw createError("A name selector here must be from a LET clause", location);
296
294
  }
297
- selection.lets.push(state.current.definitions);
298
- const last = enterDefinition(state.current.definitions);
299
- if (state.isFailed) return reference;
295
+ selection.lets.push(current.definitions);
296
+ const last = enterDefinition(current.definitions);
297
+ if (isFailed) return reference;
300
298
  if (last) {
301
299
  return applyDefinition(last.definition, {
302
300
  ...selection,
@@ -307,11 +305,11 @@ const selectSubexpression = (
307
305
  continue;
308
306
  }
309
307
  selectOperand(selector);
310
- state.index++;
308
+ selectorIndex++;
311
309
  }
312
- if (state.isFailed) return reference;
313
- if (!state.current) throw createError("Subexpression selection failed", location);
314
- return createApplication(selection, state.current);
310
+ if (isFailed) return reference;
311
+ if (!current) throw createError("Subexpression selection failed", location);
312
+ return createApplication(selection, current);
315
313
  };
316
314
 
317
315
  const getQualifiedSelectors = (
@@ -322,7 +320,7 @@ const getQualifiedSelectors = (
322
320
  ) => {
323
321
  if (!name.includes("!") || lookup(name)) return undefined;
324
322
  const parts = name.split("!");
325
- if (!parts.some((_, index) => lookup(parts.slice(0, index + 1).join("!")))) return undefined;
323
+ if (!parts.some((_part, index) => lookup(parts.slice(0, index + 1).join("!")))) return undefined;
326
324
  return getNameSelectors(name, args, counts);
327
325
  };
328
326
 
@@ -332,9 +330,6 @@ const containsReference = (expression: Expression): boolean =>
332
330
  (expression.kind === "Apply" && expression.operator.includes("!")) ||
333
331
  getChildren(expression).some(containsReference);
334
332
 
335
- // A reference that starts with a name isGoal holds for, such as a theorem whose statement is an
336
- // ASSUME/PROVE, stays for name resolution to check. isOperator holds where an operator may be given,
337
- // as the argument of an operator that may take one.
338
333
  const resolveSubexpressions = (
339
334
  expression: Expression,
340
335
  lookup: Lookup,
@@ -403,7 +398,6 @@ const createLookup = (definitions: Definition[]): Lookup => {
403
398
  export const resolveInstanceSubexpressions = (instance: Instance, module: Module): Instance =>
404
399
  resolveInstance(instance, createLookup(module.definitions), () => false);
405
400
 
406
- // Generator.selectorToNode resolves a name in a theorem, a proof, or a fact as it does in a definition.
407
401
  export const resolveModuleSubexpressions = (module: Module, available: Definition[]): Module => {
408
402
  const goals = new Set(
409
403
  module.theorems.flatMap((theorem) =>
@@ -442,8 +436,6 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
442
436
  definitionLookup,
443
437
  (name) => isGoal(name) && clauses.every((clause) => clause.name !== name),
444
438
  );
445
- // A name in a theorem, a proof, or a fact may also start with a step, a theorem, or a symbol that
446
- // no definition names.
447
439
  const isProofGoal = (name: string) => isGoal(name) || !lookup(name);
448
440
  const resolveInProof = (expression: Expression) =>
449
441
  resolveSubexpressions(expression, lookup, isProofGoal);
@@ -509,7 +501,6 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
509
501
  : proof;
510
502
  const resolved = {
511
503
  ...module,
512
- // A named theorem's statement is resolved as its definition.
513
504
  theorems: module.theorems.map((theorem) => ({
514
505
  ...theorem,
515
506
  statement: