tla-ts 0.0.3 → 0.0.4-dev.4819fb7

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 (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +162 -8
  6. package/dist/index.d.ts +162 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +77 -37
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +994 -279
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +200 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +220 -108
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +741 -189
package/src/evaluate.ts CHANGED
@@ -1,54 +1,74 @@
1
- import type {
2
- Bound,
3
- BoundPattern,
4
- DecimalExpression,
5
- Definition,
6
- Expression,
7
- FunctionDefinition,
8
- NumberExpression,
9
- OperatorParam,
10
- RecordField,
11
- SourceRange,
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Bound,
5
+ type BoundPattern,
6
+ type DecimalExpression,
7
+ type Definition,
8
+ type Expression,
9
+ type FunctionDefinition,
10
+ type NumberExpression,
11
+ type OperatorParam,
12
+ type RecordField,
13
+ type SourceRange,
12
14
  } from "./ast";
13
15
  import { getKindString } from "./builtins";
14
- import { TlaEvaluationError } from "./errors";
16
+ import { isStackOverflow, TlaEvaluationError, TlaUnassignedError } from "./errors";
15
17
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
16
18
  import { generateEvaluator } from "./codegen";
19
+ import { MESSAGE_CODE } from "./tlc-messages";
17
20
  import type { CheckerRuntime } from "./runtime";
18
21
  import { getReadVariables } from "./dependencies";
19
22
  import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
20
- import { isEnabled } from "./successors";
23
+ import { prettyPrint } from "./pretty-print";
24
+ import { getReadLocals, isEnabled } from "./successors";
21
25
  import {
22
26
  applyFunction,
27
+ describeMissingArgument,
23
28
  combineHash,
24
29
  compareValues,
25
30
  createFunction,
31
+ createNonIntegerKeyError,
32
+ createRecord,
26
33
  createEnumeratedSet,
27
34
  createSortedFunction,
35
+ createSortedRecord,
28
36
  createSortedSet,
29
37
  createWrittenRecord,
30
38
  createTuple,
31
39
  formatValue,
40
+ getCardinality,
32
41
  getDomain,
33
42
  getFunctionDomain,
34
43
  getIntervalElements,
44
+ getLazySubsets,
45
+ getStreamedElements,
46
+ getFunctionClass,
35
47
  getSequenceElements,
36
48
  getValueHash,
49
+ isElementOf,
37
50
  isEnumerable,
38
51
  isFunctionValue,
39
52
  isOverriddenSet,
40
53
  isSameValue,
41
54
  markLambdaFunction,
55
+ isReducibleSet,
42
56
  isSetValue,
43
57
  MAXIMUM_INTEGER,
44
58
  MINIMUM_INTEGER,
59
+ toFiniteFunction,
45
60
  toFiniteSet,
46
61
  updateFunction,
47
62
  valuesEqual,
63
+ type FilteredSetValue,
48
64
  type LazyFunctionSource,
49
65
  type LazyFunctionValue,
66
+ type ObjectValue,
50
67
  type Value,
68
+ markNormalized,
69
+ setTrace,
51
70
  } from "./values";
71
+ import type { TlcVersion } from "./tlc-version";
52
72
 
53
73
  export type State = (Value | undefined)[];
54
74
 
@@ -71,6 +91,8 @@ export interface Builtin {
71
91
  evaluateUnary: ((value: Value) => Value) | undefined;
72
92
  evaluateBinary: ((left: Value, right: Value) => Value) | undefined;
73
93
  isStateLevel?: boolean;
94
+ // Whether TLC takes an application for a constant one although its value depends on the state.
95
+ isLevelConstant?: boolean;
74
96
  isLazy?: boolean;
75
97
  }
76
98
 
@@ -107,18 +129,89 @@ export interface Environment {
107
129
  builtins: Map<string, Builtin>;
108
130
  constants: Map<string, Value>;
109
131
  substitutions: Map<string, string>;
132
+ // The name SANY gives each definition that a named instance such as `M == INSTANCE Module`
133
+ // copies, such as M!Op, by the key of the copy.
134
+ instanceNames: Map<string, string>;
110
135
  variables: string[];
111
136
  variableIndex: Map<string, number>;
112
137
  definitionValues: Map<Definition, Value>;
113
138
  constantLevels: Map<Definition, boolean>;
139
+ // The definitions an INSTANCE copies from a module that declares constants or variables. SANY
140
+ // wraps the body of each in its substitutions.
141
+ substitutedDefinitions: Set<Definition>;
114
142
  evaluators: WeakMap<Expression, Evaluator>;
143
+ // The expressions being evaluated, outermost first, when the environment replays an error to
144
+ // find where it occurred, as TLC's CallStackTool does.
145
+ callStack?: CallStack;
146
+ }
147
+
148
+ export interface CallStack {
149
+ expressions: Expression[];
150
+ // TLC stops popping the stack once an evaluation fails, so it keeps where the failure occurred.
151
+ isFrozen: boolean;
152
+ // The expression that built each value of the replay that TLC's CallStackTool gives a source.
153
+ sources: WeakMap<ObjectValue, Expression>;
154
+ // The value each expression of the replay last evaluated to.
155
+ values: WeakMap<Expression, Value>;
156
+ // The expression each failure last left, which tells a failure of an operand from one of the
157
+ // method that computes the operator.
158
+ failures: WeakMap<Error, Expression>;
159
+ // The failure of a value with a source, which TLC raises as a FingerprintException.
160
+ fingerprint?: FingerprintFailure;
161
+ }
162
+
163
+ export interface FingerprintFailure {
164
+ // The sources of the values whose methods failed, outermost first.
165
+ sources: Expression[];
166
+ // The message of the error the innermost one raised.
167
+ reason: string;
115
168
  }
116
169
 
170
+ // The operators of Tool#evalAppl whose value TLC's CallStackTool gives a source.
171
+ const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
172
+
173
+ // CallStackTool copies the SetPredValue, FcnLambdaValue and OpLambdaValue that an evaluation
174
+ // returns without their source, and a function TLC builds from a FcnLambdaValue has none.
175
+ const SOURCED_KINDS = new Set<Expression["kind"]>([
176
+ "SetEnumeration",
177
+ "SetFilter",
178
+ "SetMap",
179
+ "Tuple",
180
+ "FunctionSet",
181
+ "Record",
182
+ "RecordSet",
183
+ "Product",
184
+ ]);
185
+
186
+ const isSourced = (expression: Expression) =>
187
+ SOURCED_KINDS.has(expression.kind) ||
188
+ (expression.kind === "Apply" && SOURCED_OPERATORS.has(expression.operator));
189
+
190
+ // The argument whose method Tool#evalAppl calls to compute an operator, as with S.member(e) for
191
+ // e \in S, f.apply(e) for f[e] when f is a function, or S.diff(T) for S \ T when S is Reducible.
192
+ const getReceiver = (expression: Expression, values: CallStack["values"]) => {
193
+ if (expression.kind === "FunctionApplication" || expression.kind === "Except") {
194
+ return expression.function;
195
+ }
196
+ if (expression.kind !== "Apply" || expression.args.length !== 2) return undefined;
197
+ if (["\\subseteq", "=", "#", "\\"].includes(expression.operator)) return expression.args[0];
198
+ if (expression.operator === "\\cap" || expression.operator === "\\cup") {
199
+ const left = values.get(expression.args[0]);
200
+ return left !== undefined && isReducibleSet(left) ? expression.args[0] : expression.args[1];
201
+ }
202
+ return expression.operator === "\\in" || expression.operator === "\\notin"
203
+ ? expression.args[1]
204
+ : undefined;
205
+ };
206
+
117
207
  export interface Context {
118
208
  environment: Environment;
119
209
  scope: Scope | undefined;
120
210
  state: State | undefined;
121
211
  next: State | undefined;
212
+ // EvalControl.KeepLazy, which Tool#evalAppl sets to evaluate the function it applies and which
213
+ // the evaluation passes on, so that a function constructor within builds a FcnLambdaValue.
214
+ isKeepLazy?: boolean;
122
215
  }
123
216
 
124
217
  export type Evaluator = (context: Context) => Value;
@@ -149,14 +242,20 @@ export const describeRange = (expression: Expression) => {
149
242
  return range ? formatRange(range) : describeLocation(expression);
150
243
  };
151
244
 
245
+ // SANY prints a numeral as written and any other expression by its location.
246
+ export const describeNode = (expression: Expression) =>
247
+ expression.kind === "Number"
248
+ ? (expression.text ?? String(expression.value))
249
+ : describeRange(expression);
250
+
152
251
  export const getConditionError = (expression: Expression, value: Value) =>
153
252
  new TlaEvaluationError(
154
253
  `A non-boolean expression (${getKindString(value)}) was used as the condition of an IF.\n${describeRange(expression)}`,
155
254
  );
156
255
 
157
- // TLC parses an infix chain of /\ as nested binary conjunctions and a bulleted list as one
158
- // conjunction list, and it words the error differently for each.
159
- export const getConjunctError = (
256
+ // TLC parses an infix chain of /\ or \/ as nested binary junctions and a bulleted list as one
257
+ // junction list, and it words the error differently for each.
258
+ export const getJunctionError = (
160
259
  expression: Expression & { kind: "And" | "Or" },
161
260
  index: number,
162
261
  value: Value,
@@ -166,22 +265,48 @@ export const getConjunctError = (
166
265
  range?.start.line === expression.location.line &&
167
266
  range.start.column === expression.location.column;
168
267
  if (isBulleted || !range) {
169
- const operand = expression.operands[index];
170
- // SANY prints a numeral as its value instead of its location.
171
- const description = operand.kind === "Number" ? String(operand.value) : describeRange(operand);
172
268
  return new TlaEvaluationError(
173
- `A non-boolean expression (${getKindString(value)}) was used as a formula in a conjunction.\n${description}`,
269
+ `A non-boolean expression (${getKindString(value)}) was used as a formula in a ${expression.kind === "And" ? "conjunction" : "disjunction"}.\n${describeNode(expression.operands[index])}`,
174
270
  );
175
271
  }
176
- const end = expression.operands[Math.max(index, 1)].location.range?.end ?? range.end;
272
+ const end = expression.prefixEnds?.[Math.max(index, 1) - 1] ?? range.end;
177
273
  return new TlaEvaluationError(
178
- `Attempted to evaluate an expression of form P /\\ Q when ${index === 0 ? "P" : "Q"} was\n${getKindString(value)}.\n${describeRange({ ...expression, location: { ...expression.location, range: { ...range, end } } })}`,
274
+ `Attempted to evaluate an expression of form P ${expression.kind === "And" ? "/\\" : "\\/"} Q when ${index === 0 ? "P" : "Q"} was\n${getKindString(value)}.\n${describeRange({ ...expression, location: { ...expression.location, range: { ...range, end } } })}`,
179
275
  );
180
276
  };
181
277
 
182
- export const getFieldError =
183
- (expression: Expression & { kind: "FieldAccess" }) => (record: Value) =>
184
- `Attempted to select nonexistent field "${expression.field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
278
+ // TLC selects a field of a record, and of another function from the function it converts it to,
279
+ // which for a tuple keeps its domain as an interval.
280
+ export const getFieldSelector = (expression: Expression & { kind: "FieldAccess" }) => {
281
+ const field = expression.field;
282
+ const describeMissing = (record: Value) =>
283
+ `Attempted to select nonexistent field "${field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
284
+ const describeNotInDomain = (functionValue: Value) =>
285
+ `Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument "${field}", which is not in the domain of the function.`;
286
+ return (value: Value) => {
287
+ if (!isFunctionValue(value)) {
288
+ throw new TlaEvaluationError(
289
+ `Attempted to select field "${field}" from a non-record value ${prettyPrint(formatValue(value))}\n${describeRange(expression)}`,
290
+ );
291
+ }
292
+ const functionClass = getFunctionClass(value);
293
+ if (functionClass === "tuple") throw createNonIntegerKeyError(field);
294
+ // Only TLC's RecordValue names the field it lacks. Any other function, such as a FcnRcdValue
295
+ // whose keys are strings, reports an argument outside its domain.
296
+ return applyFunction(
297
+ value,
298
+ field,
299
+ functionClass === "record" ? describeMissing : describeNotInDomain,
300
+ );
301
+ };
302
+ };
303
+
304
+ // TLC 1.7.4 words what a tuple or a record lacks differently.
305
+ export const getMissingArgumentDescriber = (version: TlcVersion | undefined) =>
306
+ version === "1.7.4"
307
+ ? (functionValue: Value, argument: Value) =>
308
+ describeMissingArgument(functionValue, argument, version)
309
+ : undefined;
185
310
 
186
311
  export const getDomainOperator = (expression: Expression) => (value: Value) => {
187
312
  if (!isFunctionValue(value)) {
@@ -246,11 +371,31 @@ const bindPattern = (scope: Scope | undefined, pattern: BoundPattern, value: Val
246
371
  );
247
372
  };
248
373
 
374
+ // Tool#evalAppl checks that it applies a function before it evaluates the argument.
375
+ export const expectFunction = (value: Value, application: Expression) => {
376
+ if (isFunctionValue(value)) return value;
377
+ throw new TlaEvaluationError(
378
+ `A non-function (${getKindString(value)}) was applied as a function.\n${describeRange(application)}`,
379
+ );
380
+ };
381
+
382
+ export const toKeepLazyContext = (context: Context): Context =>
383
+ context.isKeepLazy
384
+ ? context
385
+ : {
386
+ environment: context.environment,
387
+ scope: context.scope,
388
+ state: context.state,
389
+ next: context.next,
390
+ isKeepLazy: true,
391
+ };
392
+
249
393
  const withScope = (context: Context, scope: Scope | undefined): Context => ({
250
394
  environment: context.environment,
251
395
  scope,
252
396
  state: context.state,
253
397
  next: context.next,
398
+ isKeepLazy: context.isKeepLazy,
254
399
  });
255
400
 
256
401
  export const expectBoolean = (value: Value, description: string) => {
@@ -262,104 +407,29 @@ export const expectBoolean = (value: Value, description: string) => {
262
407
  return value;
263
408
  };
264
409
 
265
- export const getChildren = (expression: Expression): Expression[] => {
266
- switch (expression.kind) {
267
- case "Number":
268
- case "Decimal":
269
- case "String":
270
- case "Identifier":
271
- case "OperatorReference":
272
- case "ExceptAt":
273
- return [];
274
- case "Apply":
275
- return expression.args;
276
- case "Lambda":
277
- return [expression.body];
278
- case "And":
279
- case "Or":
280
- return expression.operands;
281
- case "Prime":
282
- case "Unchanged":
283
- case "Enabled":
284
- case "Always":
285
- case "Eventually":
286
- return [expression.expression];
287
- case "If":
288
- return [expression.condition, expression.consequent, expression.alternative];
289
- case "Case":
290
- return [
291
- ...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
292
- ...(expression.other ? [expression.other] : []),
293
- ];
294
- case "Let":
295
- return [
296
- ...expression.definitions.flatMap((definition) =>
297
- definition.kind === "Function"
298
- ? [...definition.bounds.map((bound) => bound.set), definition.body]
299
- : [definition.body],
300
- ),
301
- expression.body,
302
- ];
303
- case "Forall":
304
- case "Exists":
305
- return [...expression.bounds.map((bound) => bound.set), expression.body];
306
- case "UnboundedQuantifier":
307
- return [expression.body];
308
- case "Choose":
309
- return [...(expression.set ? [expression.set] : []), expression.body];
310
- case "SetEnumeration":
311
- case "Tuple":
312
- return expression.elements;
313
- case "SetFilter":
314
- return [expression.bound.set, expression.predicate];
315
- case "SetMap":
316
- return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
317
- case "FunctionConstruction":
318
- return [...expression.bounds.map((bound) => bound.set), expression.body];
319
- case "FunctionSet":
320
- return [expression.domain, expression.range];
321
- case "Record":
322
- case "RecordSet":
323
- return expression.fields.map((field) => field.value);
324
- case "Except":
325
- return [
326
- expression.function,
327
- ...expression.updates.flatMap((update) => [
328
- ...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
329
- update.value,
330
- ]),
331
- ];
332
- case "FunctionApplication":
333
- return [expression.function, ...expression.args];
334
- case "FieldAccess":
335
- return [expression.record];
336
- case "Product":
337
- return expression.factors;
338
- case "BoxAction":
339
- case "AngleAction":
340
- return [expression.action, expression.subscript];
341
- case "WeakFairness":
342
- case "StrongFairness":
343
- return [expression.subscript, expression.action];
344
- case "Label":
345
- return [expression.body];
346
- case "Subexpression":
347
- return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
348
- }
349
- };
350
-
351
- export const isLocallyResolved = (expression: Expression) =>
352
- (expression.kind === "Identifier" || expression.kind === "Apply") &&
353
- expression.resolution?.kind === "Local";
354
-
355
410
  export const getDefinition = (name: string, environment: Environment) =>
356
411
  environment.definitions.get(environment.substitutions.get(name) ?? name);
357
412
 
358
- const isStateDependent = (expression: Expression, environment: Environment): boolean => {
413
+ // TLC bounds the level of every action and temporal operator above constant, whatever its operands.
414
+ const NONCONSTANT_KINDS = new Set<Expression["kind"]>([
415
+ "Prime",
416
+ "Unchanged",
417
+ "Enabled",
418
+ "BoxAction",
419
+ "AngleAction",
420
+ "Always",
421
+ "Eventually",
422
+ "WeakFairness",
423
+ "StrongFairness",
424
+ ]);
425
+
426
+ const NONCONSTANT_OPERATORS = new Set(["~>", "-+->", "\\cdot"]);
427
+
428
+ const isNonconstant = (expression: Expression, environment: Environment): boolean => {
359
429
  if (
360
- expression.kind === "Prime" ||
361
- expression.kind === "Unchanged" ||
362
- expression.kind === "Enabled"
430
+ NONCONSTANT_KINDS.has(expression.kind) ||
431
+ (expression.kind === "UnboundedQuantifier" && expression.isTemporal === true) ||
432
+ (expression.kind === "Apply" && NONCONSTANT_OPERATORS.has(expression.operator))
363
433
  ) {
364
434
  return true;
365
435
  }
@@ -376,7 +446,7 @@ const isStateDependent = (expression: Expression, environment: Environment): boo
376
446
  if (definition && !environment.constants.has(name) && !isConstantLevel(definition, environment))
377
447
  return true;
378
448
  }
379
- return getChildren(expression).some((child) => isStateDependent(child, environment));
449
+ return getChildren(expression).some((child) => isNonconstant(child, environment));
380
450
  };
381
451
 
382
452
  export const isConstantLevel = (definition: Definition, environment: Environment): boolean => {
@@ -384,9 +454,9 @@ export const isConstantLevel = (definition: Definition, environment: Environment
384
454
  if (cached !== undefined) return cached;
385
455
  environment.constantLevels.set(definition, true);
386
456
  const isConstant =
387
- !isStateDependent(definition.body, environment) &&
457
+ !isNonconstant(definition.body, environment) &&
388
458
  (definition.kind === "Operator" ||
389
- !definition.bounds.some((bound) => isStateDependent(bound.set, environment)));
459
+ !definition.bounds.some((bound) => isNonconstant(bound.set, environment)));
390
460
  environment.constantLevels.set(definition, isConstant);
391
461
  return isConstant;
392
462
  };
@@ -409,38 +479,90 @@ const getBoundExplanation = (set: Value, text: string) => {
409
479
 
410
480
  // TLC enumerates every set except the ones it implements in Java, so any other set fails as its own
411
481
  // class does.
412
- export const getEnumeratedElements = (set: Value, expression: Expression) => {
482
+ const checkEnumerable = (set: Value, expression: Expression, version: TlcVersion) => {
413
483
  if (!isSetValue(set) || isOverriddenSet(set)) {
414
484
  const text = formatValue(set);
415
- const explanation = getBoundExplanation(set, text);
485
+ // TLC 1.7.4 doesn't explain why it can't enumerate a set.
486
+ const explanation = version === "1.7.4" ? undefined : getBoundExplanation(set, text);
416
487
  throw new TlaEvaluationError(
417
488
  explanation === undefined
418
- ? `TLC encountered a non-enumerable quantifier bound\n${text}.\n${describeRange(expression)}`
419
- : `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
489
+ ? `TLC encountered a non-enumerable quantifier bound\n${text}.\n${describeNode(expression)}`
490
+ : `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeNode(expression)}\n${explanation}`,
420
491
  );
421
492
  }
422
- return typeof set === "object" && set.kind === "Interval"
423
- ? getIntervalElements(set)
424
- : toFiniteSet(set).elements;
425
493
  };
426
494
 
427
- export const forEachBinding = (
495
+ const getEnumeratedElements = (set: Value, expression: Expression, version: TlcVersion) => {
496
+ checkEnumerable(set, expression, version);
497
+ if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
498
+ return getLazySubsets(set) ?? toFiniteSet(set).elements;
499
+ };
500
+
501
+ // The elements Tool.contexts binds to the bound of a quantifier or a set map. TLC 1.7.4 streams a
502
+ // lazy set's elements, where later versions normalize the set first.
503
+ export const getContextElements = (set: Value, expression: Expression, version: TlcVersion) => {
504
+ if (version !== "1.7.4") return getEnumeratedElements(set, expression, version);
505
+ checkEnumerable(set, expression, version);
506
+ return getLazySubsets(set) ?? getStreamedElements(set);
507
+ };
508
+
509
+ // Like TLC, a quantifier keeps an interval as an interval, so that it can stop at the first witness
510
+ // in a large interval such as 1..2000000000 without listing it.
511
+ const getKeptInterval = (set: Value) => {
512
+ if (typeof set !== "object" || set.kind !== "Interval") return undefined;
513
+ getCardinality(set);
514
+ return set;
515
+ };
516
+
517
+ export const getQuantifierElements = (set: Value, expression: Expression, version: TlcVersion) =>
518
+ getKeptInterval(set) ?? getContextElements(set, expression, version);
519
+
520
+ // CHOOSE and a function constructor visit the elements of the normalized set.
521
+ const getNormalizedElements = (set: Value, expression: Expression, version: TlcVersion) =>
522
+ getKeptInterval(set) ?? getEnumeratedElements(set, expression, version);
523
+
524
+ const someElement = (
525
+ elements: ReturnType<typeof getQuantifierElements>,
526
+ visit: (element: Value) => boolean,
527
+ ) => {
528
+ if (Array.isArray(elements)) return elements.some(visit);
529
+ for (let element = elements.low; element <= elements.high; element++) {
530
+ if (visit(element)) return true;
531
+ }
532
+ return false;
533
+ };
534
+
535
+ export const getBoundElements = (bounds: Bound[], context: Context) =>
536
+ bounds.map((bound) =>
537
+ getQuantifierElements(
538
+ evaluate(bound.set, context),
539
+ bound.set,
540
+ context.environment.runtime.version,
541
+ ),
542
+ );
543
+
544
+ // Visits each binding of the bounds to the elements of their sets, until visit returns true.
545
+ export const forEachBoundElement = (
428
546
  bounds: Bound[],
429
- context: Context,
547
+ sets: ReturnType<typeof getBoundElements>,
548
+ scope: Scope | undefined,
430
549
  visit: (scope: Scope | undefined) => boolean,
431
550
  ): boolean => {
432
- const sets = bounds.map((bound) =>
433
- getEnumeratedElements(evaluate(bound.set, context), bound.set),
434
- );
435
- const visitBound = (index: number, scope: Scope | undefined): boolean => {
436
- if (index < 0) return visit(scope);
437
- return sets[index].some((element) =>
438
- visitBound(index - 1, bindPattern(scope, bounds[index].pattern, element)),
551
+ const visitBound = (index: number, bound: Scope | undefined): boolean => {
552
+ if (index < 0) return visit(bound);
553
+ return someElement(sets[index], (element) =>
554
+ visitBound(index - 1, bindPattern(bound, bounds[index].pattern, element)),
439
555
  );
440
556
  };
441
- return visitBound(bounds.length - 1, context.scope);
557
+ return visitBound(bounds.length - 1, scope);
442
558
  };
443
559
 
560
+ export const forEachBinding = (
561
+ bounds: Bound[],
562
+ context: Context,
563
+ visit: (scope: Scope | undefined) => boolean,
564
+ ) => forEachBoundElement(bounds, getBoundElements(bounds, context), context.scope, visit);
565
+
444
566
  export const getBoundsDomain = (bounds: Bound[], context: Context): Value => {
445
567
  const sets = bounds.map((bound) => evaluate(bound.set, context));
446
568
  return sets.length === 1 ? sets[0] : { kind: "ProductSet", factors: sets };
@@ -455,6 +577,84 @@ export const bindArgument = (bounds: Bound[], scope: Scope | undefined, argument
455
577
  );
456
578
  };
457
579
 
580
+ // The parameters, body and scope of each function a constructor or a definition keeps lazy, whose
581
+ // body TLC's getInitStates and getNextStates walk for an argument, as they do a FcnLambdaValue's.
582
+ interface LazyFunction {
583
+ value: LazyFunctionValue;
584
+ bounds: Bound[];
585
+ body: Expression;
586
+ scope: Scope | undefined;
587
+ }
588
+
589
+ // The function keeps its parameters, body and scope itself, since a WeakMap entry for each
590
+ // short-lived function makes every garbage collection slower.
591
+ interface KeptLazyFunctionValue extends LazyFunctionValue {
592
+ lambda: LazyFunction;
593
+ }
594
+
595
+ const isKeptLazyFunction = (value: Value): value is KeptLazyFunctionValue =>
596
+ typeof value === "object" && value.kind === "LazyFunction" && "lambda" in value;
597
+
598
+ const keepLambda = (lambda: LazyFunction) => Object.assign(lambda.value, { lambda });
599
+
600
+ export const getLazyFunction = (value: Value) =>
601
+ isKeptLazyFunction(value) ? value.lambda : undefined;
602
+
603
+ // Tool.getFcnContext: the scope of a lazy function's body for the arguments of an application. It
604
+ // ignores arguments beyond the parameters.
605
+ export const bindLazyArguments = (
606
+ lambda: LazyFunction,
607
+ expression: Expression & { kind: "FunctionApplication" },
608
+ context: Context,
609
+ ) => {
610
+ const value = lambda.value;
611
+ const args = expression.args.map((arg) => evaluate(arg, context));
612
+ const argument = args.length === 1 ? args[0] : createTuple(args);
613
+ const describeArgument = (ordinal: string, element: Value, problem: string) =>
614
+ `In applying the function\n${prettyPrint(formatValue(value))},\nthe ${ordinal} is:\n${prettyPrint(formatValue(element))}${problem}.\n${describeRange(expression.function)}`;
615
+ const bounds = lambda.bounds;
616
+ if (bounds.length === 1) {
617
+ if (!isElementOf(argument, value.domain)) {
618
+ throw new TlaEvaluationError(
619
+ describeArgument("first argument", argument, "which is not in its domain"),
620
+ );
621
+ }
622
+ return bindArgument(bounds, lambda.scope, argument);
623
+ }
624
+ const elements = getSequenceElements(argument);
625
+ const domains =
626
+ typeof value.domain === "object" && value.domain.kind === "ProductSet"
627
+ ? value.domain.factors
628
+ : [];
629
+ if (elements === undefined || elements.length < bounds.length) {
630
+ throw new TlaEvaluationError(
631
+ `Attempted to apply a function to an argument not in its domain.\n${describeRange(expression.function)}`,
632
+ );
633
+ }
634
+ bounds.forEach((bound, index) => {
635
+ const element = elements[index];
636
+ const ordinal = `argument number ${index + 1}`;
637
+ if (!isElementOf(element, domains[index])) {
638
+ throw new TlaEvaluationError(
639
+ describeArgument(
640
+ ordinal,
641
+ element,
642
+ `${bound.pattern.kind === "Name" ? "" : "\n"}which is not in its domain`,
643
+ ),
644
+ );
645
+ }
646
+ if (
647
+ bound.pattern.kind === "Tuple" &&
648
+ getSequenceElements(element)?.length !== bound.pattern.names.length
649
+ ) {
650
+ throw new TlaEvaluationError(
651
+ describeArgument(ordinal, element, "which does not match its formal parameter"),
652
+ );
653
+ }
654
+ });
655
+ return bindArgument(bounds, lambda.scope, createTuple(elements.slice(0, bounds.length)));
656
+ };
657
+
458
658
  // TLC prints a function it can't enumerate as its parameters and the location of its body. It
459
659
  // prints only the first name of a group such as `m, n \in S`.
460
660
  interface LazyFunctionTemplate {
@@ -533,6 +733,22 @@ const createLazyFunction = (
533
733
  return self;
534
734
  };
535
735
 
736
+ const definitionSources = new WeakMap<FunctionDefinition, Expression>();
737
+
738
+ // The function constructor of a definition, which SANY locates at the whole definition.
739
+ const getDefinitionSource = (definition: FunctionDefinition) => {
740
+ const cached = definitionSources.get(definition);
741
+ if (cached) return cached;
742
+ const source: Expression = {
743
+ kind: "FunctionConstruction",
744
+ bounds: definition.bounds,
745
+ body: definition.body,
746
+ location: definition.location,
747
+ };
748
+ definitionSources.set(definition, source);
749
+ return source;
750
+ };
751
+
536
752
  const createRecursiveFunction = (definition: FunctionDefinition, context: Context) => {
537
753
  const domain = getBoundsDomain(definition.bounds, context);
538
754
  const functionValue = createLazyFunction(
@@ -547,13 +763,44 @@ const createRecursiveFunction = (definition: FunctionDefinition, context: Contex
547
763
  ),
548
764
  getLazyFunctionSource(definition.bounds, definition.body, domain),
549
765
  );
766
+ keepLambda({
767
+ value: functionValue,
768
+ bounds: definition.bounds,
769
+ body: definition.body,
770
+ scope: bindValue(undefined, definition.name, functionValue),
771
+ });
550
772
  return definition.bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
551
773
  };
552
774
 
553
- const evaluateDefinitionBody = (definition: Definition, context: Context) =>
554
- definition.kind === "Function"
775
+ // TLC computes a constant definition when it starts, without keeping a function it constructs
776
+ // lazy, so a function over an interval or an enumerated set is a FcnRcdValue.
777
+ const toStartupValue = (definition: Definition, value: Value): Value => {
778
+ if (typeof value !== "object" || getFunctionClass(value) !== "lambda") return value;
779
+ if (
780
+ definition.kind === "Operator" ||
781
+ definition.isSelfReferential ||
782
+ value.kind !== "LazyFunction" ||
783
+ !isReducibleSet(value.domain)
784
+ ) {
785
+ return value;
786
+ }
787
+ try {
788
+ const finite = toFiniteFunction(value);
789
+ return createSortedFunction(finite.domain, finite.range);
790
+ } catch (error) {
791
+ // TLC leaves a definition it fails to compute to evaluate where the spec uses it.
792
+ if (error instanceof TlaEvaluationError) return value;
793
+ throw error;
794
+ }
795
+ };
796
+
797
+ // TLC evaluates a function definition that isn't recursive as it does a function constructor.
798
+ const evaluateDefinitionBody = (definition: Definition, context: Context) => {
799
+ if (definition.kind !== "Function") return evaluate(definition.body, context);
800
+ return definition.isSelfReferential
555
801
  ? createRecursiveFunction(definition, context)
556
- : evaluate(definition.body, context);
802
+ : evaluate(getDefinitionSource(definition), context);
803
+ };
557
804
 
558
805
  const evaluateDefinitionValue = (definition: Definition, context: Context) => {
559
806
  const isConstant = isConstantLevel(definition, context.environment);
@@ -567,12 +814,35 @@ const evaluateDefinitionValue = (definition: Definition, context: Context) => {
567
814
  const topLevel = withScope(context, undefined);
568
815
  if (!isConstant) return evaluateDefinitionBody(definition, topLevel);
569
816
  const value = context.environment.runtime.evaluateAtStartup(() =>
570
- evaluateDefinitionBody(definition, topLevel),
817
+ toStartupValue(definition, evaluateDefinitionBody(definition, topLevel)),
571
818
  );
572
819
  context.environment.definitionValues.set(definition, value);
573
820
  return value;
574
821
  };
575
822
 
823
+ // TLC computes each constant definition the configuration doesn't replace when it starts, and
824
+ // leaves one it fails to compute to evaluate where the spec uses it.
825
+ export const evaluateStartupDefinitions = (definitions: Definition[], environment: Environment) =>
826
+ definitions.forEach((definition) => {
827
+ if (definition.kind === "Operator" && definition.params.length > 0) return;
828
+ const name = definition.name;
829
+ if (environment.constants.has(name) || environment.substitutions.has(name)) return;
830
+ if (!isConstantLevel(definition, environment)) return;
831
+ try {
832
+ markNormalized(
833
+ evaluateDefinitionValue(definition, {
834
+ environment,
835
+ scope: undefined,
836
+ state: undefined,
837
+ next: undefined,
838
+ }),
839
+ );
840
+ } catch (error) {
841
+ // TLC swallows whatever the definition throws, a stack overflow included.
842
+ if (!(error instanceof TlaEvaluationError) && !isStackOverflow(error)) throw error;
843
+ }
844
+ });
845
+
576
846
  const forceThunk = (thunk: Thunk, context: Context) => {
577
847
  const isSameState = thunk.state === context.state && thunk.next === context.next;
578
848
  if (isSameState && thunk.cache !== undefined) return thunk.cache;
@@ -859,11 +1129,7 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
859
1129
  const global = findGlobalName(name, context.environment);
860
1130
  if (global?.kind === "Variable") {
861
1131
  const value = context.state?.[global.index];
862
- if (value === undefined) {
863
- throw new TlaEvaluationError(
864
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
865
- );
866
- }
1132
+ if (value === undefined) throw createUnassignedError(name, expression);
867
1133
  return value;
868
1134
  }
869
1135
  if (global?.kind === "Value") return global.value;
@@ -889,14 +1155,29 @@ const IMPURE_BUILTINS = new Set([
889
1155
  export const isImpureBuiltin = (name: string, environment: Environment) =>
890
1156
  IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
891
1157
 
892
- const impureDefinitions = new WeakMap<Definition, boolean>();
1158
+ interface Impurity {
1159
+ builtins: Set<string>;
1160
+ definitions: WeakMap<Definition, boolean>;
1161
+ }
1162
+
1163
+ const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
1164
+
1165
+ // An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
1166
+ const REPEATED_EFFECT: Impurity = {
1167
+ builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
1168
+ definitions: new WeakMap(),
1169
+ };
893
1170
 
894
1171
  const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
895
1172
  definition.kind === "Operator" &&
896
1173
  definition.params.length === 0 &&
897
1174
  isConstantLevel(definition, environment);
898
1175
 
899
- const callsImpureBuiltin = (expression: Expression, environment: Environment): boolean => {
1176
+ const callsImpureBuiltin = (
1177
+ expression: Expression,
1178
+ environment: Environment,
1179
+ impurity: Impurity,
1180
+ ): boolean => {
900
1181
  const name =
901
1182
  expression.kind === "Identifier"
902
1183
  ? expression.name
@@ -905,42 +1186,68 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
905
1186
  : undefined;
906
1187
  if (name !== undefined && !isLocallyResolved(expression)) {
907
1188
  const definition = getDefinition(name, environment);
908
- if (!definition && isImpureBuiltin(name, environment)) return true;
1189
+ const isImpureCall =
1190
+ impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
1191
+ if (!definition && isImpureCall) return true;
909
1192
  if (
910
1193
  definition &&
911
1194
  !isEvaluatedOnce(definition, environment) &&
912
- isImpureDefinition(definition, environment)
1195
+ isImpureDefinition(definition, environment, impurity)
913
1196
  ) {
914
1197
  return true;
915
1198
  }
916
1199
  }
917
- return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
1200
+ return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
918
1201
  };
919
1202
 
920
1203
  // TLC evaluates a constant definition without parameters once, so only other definitions pass
921
1204
  // on the side effects and random choices of the builtins they call.
922
- const isImpureDefinition = (definition: Definition, environment: Environment): boolean => {
923
- const cached = impureDefinitions.get(definition);
1205
+ const isImpureDefinition = (
1206
+ definition: Definition,
1207
+ environment: Environment,
1208
+ impurity = ANY_EFFECT,
1209
+ ): boolean => {
1210
+ const cached = impurity.definitions.get(definition);
924
1211
  if (cached !== undefined) return cached;
925
- impureDefinitions.set(definition, false);
1212
+ impurity.definitions.set(definition, false);
926
1213
  const isImpure =
927
- callsImpureBuiltin(definition.body, environment) ||
1214
+ callsImpureBuiltin(definition.body, environment, impurity) ||
928
1215
  (definition.kind === "Function" &&
929
- definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
930
- impureDefinitions.set(definition, isImpure);
1216
+ definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
1217
+ impurity.definitions.set(definition, isImpure);
931
1218
  return isImpure;
932
1219
  };
933
1220
 
1221
+ // UNION and \cup build sets that stream the sets they combine until TLC caches them, and TLC
1222
+ // builds a new one at each evaluation.
1223
+ const STREAMED_SET: Impurity = {
1224
+ builtins: new Set(["UNION", "\\cup"]),
1225
+ definitions: new WeakMap(),
1226
+ };
1227
+
934
1228
  // TLC's next-state generator does not bind LET definitions to values, so each reference to one
935
- // that calls an impure builtin evaluates it again.
1229
+ // evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
1230
+ // with UNION or \cup starts out uncached.
936
1231
  export const getReevaluatedDefinitions = (definitions: Definition[], environment: Environment) =>
937
- new Set(definitions.filter((definition) => isImpureDefinition(definition, environment)));
1232
+ new Set(
1233
+ definitions.filter(
1234
+ (definition) =>
1235
+ isImpureDefinition(definition, environment) ||
1236
+ isImpureDefinition(definition, environment, STREAMED_SET),
1237
+ ),
1238
+ );
938
1239
 
939
1240
  export const isImpure = (environment: Environment) =>
940
1241
  [...environment.definitions.values()].some((definition) =>
941
1242
  isImpureDefinition(definition, environment),
942
1243
  );
943
1244
 
1245
+ // Whether evaluating a definition again can show, such as by printing twice.
1246
+ export const hasRepeatedEffects = (environment: Environment) =>
1247
+ [...environment.definitions.values()].some((definition) =>
1248
+ isImpureDefinition(definition, environment, REPEATED_EFFECT),
1249
+ );
1250
+
944
1251
  const closedConstants = new WeakMap<Expression, boolean>();
945
1252
 
946
1253
  export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
@@ -1004,7 +1311,11 @@ const computeIsClosedConstant = (
1004
1311
  return global?.kind === "Definition" && isEvaluatedOnce(global.definition, environment);
1005
1312
  }
1006
1313
  if (expression.kind === "Apply") {
1007
- if (isImpureBuiltin(expression.operator, environment) || expression.operator === "~>") {
1314
+ if (
1315
+ isImpureBuiltin(expression.operator, environment) ||
1316
+ STREAMED_SET.builtins.has(expression.operator) ||
1317
+ expression.operator === "~>"
1318
+ ) {
1008
1319
  return false;
1009
1320
  }
1010
1321
  const operator = resolveGlobalOperator(expression.operator, environment);
@@ -1076,12 +1387,128 @@ const isStateCached = (expression: Expression, environment: Environment) =>
1076
1387
  getReadVariables(expression, environment) !== undefined &&
1077
1388
  !hasFreeLocals(expression, NO_BOUND_NAMES);
1078
1389
 
1390
+ // A set filter TLC evaluates lazily, with how to rebuild it so that its predicate runs in another
1391
+ // environment.
1392
+ interface RebindableFilteredSet extends FilteredSetValue {
1393
+ rebind: (environment: Environment) => Value;
1394
+ }
1395
+
1396
+ const isRebindableFilteredSet = (value: Value): value is RebindableFilteredSet =>
1397
+ typeof value === "object" && value.kind === "FilteredSet" && "rebind" in value;
1398
+
1399
+ // TLC's CallStackTool rebinds the lazy values its evaluations return, so their later evaluation
1400
+ // records its call stack too.
1401
+ const traceEvaluator =
1402
+ (
1403
+ expression: Expression,
1404
+ evaluator: Evaluator,
1405
+ environment: Environment,
1406
+ callStack: CallStack,
1407
+ ): Evaluator =>
1408
+ (context) => {
1409
+ callStack.expressions.push(expression);
1410
+ const outerSets = setTrace.sets;
1411
+ const outerCallStack = setTrace.callStack;
1412
+ const sets = outerSets ?? [];
1413
+ const depth = sets.length;
1414
+ setTrace.sets = sets;
1415
+ setTrace.callStack = callStack;
1416
+ try {
1417
+ const computed = evaluator(context);
1418
+ const value = isRebindableFilteredSet(computed) ? computed.rebind(environment) : computed;
1419
+ callStack.values.set(expression, value);
1420
+ if (typeof value === "object" && value.kind !== "FilteredSet" && isSourced(expression)) {
1421
+ callStack.sources.set(value, expression);
1422
+ }
1423
+ return value;
1424
+ } catch (error) {
1425
+ const failed = error instanceof Error ? callStack.failures.get(error) : undefined;
1426
+ if (error instanceof Error) callStack.failures.set(error, expression);
1427
+ const receiver = getReceiver(expression, callStack.values);
1428
+ const received =
1429
+ receiver && !getChildren(expression).some((child) => child === failed)
1430
+ ? callStack.values.get(receiver)
1431
+ : undefined;
1432
+ const value =
1433
+ expression.kind === "FunctionApplication" &&
1434
+ received !== undefined &&
1435
+ !isFunctionValue(received)
1436
+ ? undefined
1437
+ : received;
1438
+ freezeCallStack(
1439
+ callStack,
1440
+ [value, ...sets.slice(depth).filter((set) => set !== value)],
1441
+ error,
1442
+ );
1443
+ throw error;
1444
+ } finally {
1445
+ if (!callStack.isFrozen) callStack.expressions.pop();
1446
+ sets.length = depth;
1447
+ setTrace.sets = outerSets;
1448
+ setTrace.callStack = outerCallStack;
1449
+ }
1450
+ };
1451
+
1452
+ // A method of a value with a source raises a FingerprintException, which each value with a source
1453
+ // whose method called it heads in turn. CallStack#freeze adds their sources to the stack, outermost
1454
+ // first. The values are those whose methods failed, outermost first.
1455
+ export const freezeCallStack = (
1456
+ callStack: CallStack,
1457
+ values: (Value | undefined)[],
1458
+ error: unknown,
1459
+ ) => {
1460
+ // A set that streams its elements enumerates itself too, so it can appear twice.
1461
+ const sources = [...new Set(values)].flatMap((item) => {
1462
+ const source = typeof item === "object" ? callStack.sources.get(item) : undefined;
1463
+ return source ? [source] : [];
1464
+ });
1465
+ // A value with a source raises a FingerprintException even when the stack is frozen already.
1466
+ if (sources.length > 0 && error instanceof TlaEvaluationError) {
1467
+ if (!callStack.isFrozen) callStack.expressions.push(...sources);
1468
+ const inner = callStack.fingerprint;
1469
+ callStack.fingerprint = {
1470
+ sources: [...sources, ...(inner?.sources ?? [])],
1471
+ reason: inner?.reason ?? error.message,
1472
+ };
1473
+ }
1474
+ callStack.isFrozen = true;
1475
+ };
1476
+
1477
+ // A FingerprintException that a method of a value with a source raises, or that passes through one,
1478
+ // takes the value's source as its new head.
1479
+ const headFingerprint = (callStack: CallStack, source: Expression, error: unknown) => {
1480
+ if (!(error instanceof TlaEvaluationError)) return;
1481
+ if (!callStack.isFrozen) callStack.expressions.push(source);
1482
+ callStack.isFrozen = true;
1483
+ const inner = callStack.fingerprint;
1484
+ callStack.fingerprint = {
1485
+ sources: [source, ...(inner?.sources ?? [])],
1486
+ reason: inner?.reason ?? error.message,
1487
+ };
1488
+ };
1489
+
1490
+ // A copy of the environment that records the call stack of each evaluation, with evaluators of its
1491
+ // own since the environment's evaluators record nothing.
1492
+ export const createTracingEnvironment = (environment: Environment): Environment => ({
1493
+ ...environment,
1494
+ evaluators: new WeakMap(),
1495
+ callStack: {
1496
+ expressions: [],
1497
+ isFrozen: false,
1498
+ sources: new WeakMap(),
1499
+ values: new WeakMap(),
1500
+ failures: new WeakMap(),
1501
+ },
1502
+ });
1503
+
1079
1504
  export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
1080
1505
  const cached = environment.evaluators.get(expression);
1081
1506
  if (cached) return cached;
1082
1507
  const compiled = compile(expression, environment);
1083
- const evaluator =
1084
- isWorthCaching(expression) && isClosedConstant(expression, environment)
1508
+ const callStack = environment.callStack;
1509
+ const evaluator = callStack
1510
+ ? traceEvaluator(expression, compiled, environment, callStack)
1511
+ : isWorthCaching(expression) && isClosedConstant(expression, environment)
1085
1512
  ? cacheFirstValue(compiled)
1086
1513
  : isStateCached(expression, environment)
1087
1514
  ? cacheStateValue(compiled)
@@ -1093,6 +1520,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
1093
1520
  const entryEvaluators = new WeakMap<Expression, Evaluator>();
1094
1521
 
1095
1522
  const getEntryEvaluator = (expression: Expression, environment: Environment) => {
1523
+ if (environment.callStack) return getEvaluator(expression, environment);
1096
1524
  const cached = entryEvaluators.get(expression);
1097
1525
  if (cached) return cached;
1098
1526
  const evaluator =
@@ -1116,11 +1544,7 @@ const compileIdentifier = (
1116
1544
  const index = global.index;
1117
1545
  return (context) => {
1118
1546
  const value = context.state?.[index];
1119
- if (value === undefined) {
1120
- throw new TlaEvaluationError(
1121
- `The variable ${name} is not assigned at ${describeLocation(expression)}`,
1122
- );
1123
- }
1547
+ if (value === undefined) throw createUnassignedError(name, expression);
1124
1548
  return value;
1125
1549
  };
1126
1550
  }
@@ -1269,75 +1693,236 @@ export const compileArgumentScope = (
1269
1693
  };
1270
1694
 
1271
1695
  interface MemoizedCall {
1696
+ sources: (Expression | undefined)[];
1272
1697
  args: Value[];
1273
1698
  value: Value;
1274
1699
  }
1275
1700
 
1276
1701
  const MEMOIZED_CALL_LIMIT = 1 << 16;
1277
1702
 
1278
- const callMemos = new WeakMap<Closure, { calls: Map<number, MemoizedCall[]>; size: number }>();
1703
+ // A call site that finds fewer than half its calls in a state's memo after this many new ones
1704
+ // stops paying for the lookups.
1705
+ const MEMOIZED_TRIAL_CALLS = 1 << 10;
1279
1706
 
1280
- const getCallMemo = (closure: Closure) => {
1707
+ interface CallMemo {
1708
+ calls: Map<number, MemoizedCall[]>;
1709
+ size: number;
1710
+ state: Context["state"];
1711
+ }
1712
+
1713
+ interface CallMemos {
1714
+ constant: CallMemo;
1715
+ state: CallMemo;
1716
+ }
1717
+
1718
+ const createCallMemo = (): CallMemo => ({ calls: new Map(), size: 0, state: undefined });
1719
+
1720
+ const callMemos = new WeakMap<Closure, CallMemos>();
1721
+
1722
+ const getCallMemos = (closure: Closure) => {
1281
1723
  const cached = callMemos.get(closure);
1282
1724
  if (cached) return cached;
1283
- const memo = { calls: new Map<number, MemoizedCall[]>(), size: 0 };
1284
- callMemos.set(closure, memo);
1285
- return memo;
1725
+ const memos = { constant: createCallMemo(), state: createCallMemo() };
1726
+ callMemos.set(closure, memos);
1727
+ return memos;
1728
+ };
1729
+
1730
+ const readsExceptAt = (expression: Expression): boolean =>
1731
+ expression.kind === "ExceptAt" || getChildren(expression).some(readsExceptAt);
1732
+
1733
+ // The locals an argument expression reads, when the state and their values decide its value. An @
1734
+ // reads the value its EXCEPT binds.
1735
+ const argumentLocals = new WeakMap<Expression, string[] | false>();
1736
+
1737
+ const getArgumentLocals = (expression: Expression, environment: Environment) => {
1738
+ const cached = argumentLocals.get(expression);
1739
+ if (cached !== undefined) return cached;
1740
+ const locals =
1741
+ getReadVariables(expression, environment) !== undefined &&
1742
+ getReadLocals(expression, NO_BOUND_NAMES);
1743
+ const unique = locals && [
1744
+ ...new Set(readsExceptAt(expression) ? [...locals, EXCEPT_AT] : locals),
1745
+ ];
1746
+ argumentLocals.set(expression, unique ?? false);
1747
+ return unique ?? false;
1748
+ };
1749
+
1750
+ // Hashing a lazy set or function would enumerate it, which TLC does only when it has to.
1751
+ const isEagerValue = (value: Value) =>
1752
+ typeof value !== "object" ||
1753
+ value.kind === "ModelValue" ||
1754
+ value.kind === "Set" ||
1755
+ value.kind === "Function" ||
1756
+ value.kind === "Interval";
1757
+
1758
+ // The key of the call being looked up, copied into the memo only when the call is new. Counts
1759
+ // mark its ends, since shortening an array is slow.
1760
+ const callKey = {
1761
+ sources: [] as (Expression | undefined)[],
1762
+ sourceCount: 0,
1763
+ args: [] as Value[],
1764
+ argCount: 0,
1765
+ hash: 0,
1766
+ isStateKeyed: false,
1767
+ };
1768
+
1769
+ // A model value's rank tells it apart without hashing its name.
1770
+ const getKeyHash = (value: Value) =>
1771
+ typeof value === "object" && value.kind === "ModelValue" ? value.rank : getValueHash(value);
1772
+
1773
+ const pushKeyValue = (value: Value) => {
1774
+ if (!isEagerValue(value)) return false;
1775
+ callKey.sources[callKey.sourceCount++] = undefined;
1776
+ callKey.args[callKey.argCount++] = value;
1777
+ callKey.hash = combineHash(callKey.hash, getKeyHash(value));
1778
+ return true;
1286
1779
  };
1287
1780
 
1288
- const getArgumentValues = (scope: Scope | undefined, arity: number) => {
1289
- const values: Value[] = [];
1290
- for (let current = scope; values.length < arity; current = current?.parent) {
1291
- if (current?.binding.kind !== "Value") return undefined;
1292
- values.push(current.binding.value);
1781
+ // An argument TLC has not evaluated yet stands for its expression and the values of the locals it
1782
+ // reads, so a call keeps the laziness of its arguments.
1783
+ const pushKeyExpression = (
1784
+ expression: Expression,
1785
+ locals: string[] | false,
1786
+ scope: Scope | undefined,
1787
+ ) => {
1788
+ if (!locals) return false;
1789
+ callKey.sources[callKey.sourceCount++] = expression;
1790
+ callKey.isStateKeyed = true;
1791
+ for (let index = 0; index < locals.length; index++) {
1792
+ const local = lookupScope(scope, locals[index]);
1793
+ if (local?.kind !== "Value" || !isEagerValue(local.value)) return false;
1794
+ callKey.args[callKey.argCount++] = local.value;
1795
+ callKey.hash = combineHash(callKey.hash, getKeyHash(local.value));
1293
1796
  }
1294
- return values;
1797
+ return true;
1295
1798
  };
1296
1799
 
1297
- const isSameArguments = (left: Value[], right: Value[]) =>
1298
- left.every((value, index) => isSameValue(value, right[index]));
1800
+ // Each argument adds to the key what compileArgumentBinder would bind its parameter to.
1801
+ const compileArgumentKey = (
1802
+ argument: Expression,
1803
+ environment: Environment,
1804
+ ): ((context: Context) => boolean) => {
1805
+ const isLiteral =
1806
+ argument.kind === "String" || (argument.kind === "Number" && isTlcInteger(argument.value));
1807
+ if (isLiteral) return () => pushKeyValue(argument.value);
1808
+ if (argument.kind === "Identifier") {
1809
+ const global = isLocallyResolved(argument)
1810
+ ? undefined
1811
+ : findGlobalName(argument.name, environment);
1812
+ if (global?.kind === "Value") return () => pushKeyValue(global.value);
1813
+ if (global === undefined) {
1814
+ return (context) => {
1815
+ const binding = lookupScope(context.scope, argument.name);
1816
+ if (binding?.kind === "Value") return pushKeyValue(binding.value);
1817
+ if (binding?.kind !== "Thunk" || binding.state !== context.state) return false;
1818
+ return pushKeyExpression(
1819
+ binding.expression,
1820
+ getArgumentLocals(binding.expression, environment),
1821
+ binding.scope,
1822
+ );
1823
+ };
1824
+ }
1825
+ }
1826
+ const locals = getArgumentLocals(argument, environment);
1827
+ return (context) => pushKeyExpression(argument, locals, context.scope);
1828
+ };
1829
+
1830
+ const isKnownCall = (call: MemoizedCall) => {
1831
+ if (call.args.length !== callKey.argCount || call.sources.length !== callKey.sourceCount) {
1832
+ return false;
1833
+ }
1834
+ for (let index = 0; index < callKey.sourceCount; index++) {
1835
+ if (call.sources[index] !== callKey.sources[index]) return false;
1836
+ }
1837
+ for (let index = 0; index < callKey.argCount; index++) {
1838
+ if (!isSameValue(call.args[index], callKey.args[index])) return false;
1839
+ }
1840
+ return true;
1841
+ };
1299
1842
 
1300
- // A constant-level operator without side effects yields the same value for the same arguments,
1301
- // so its calls with evaluated arguments share one result.
1302
- const isMemoizable = (expression: Expression & { kind: "Apply" }, environment: Environment) => {
1843
+ const findCall = (bucket: MemoizedCall[] | undefined) => {
1844
+ if (bucket === undefined) return undefined;
1845
+ for (let index = 0; index < bucket.length; index++) {
1846
+ if (isKnownCall(bucket[index])) return bucket[index];
1847
+ }
1848
+ return undefined;
1849
+ };
1850
+
1851
+ // An operator without side effects yields the same value for the same arguments, a state-level one
1852
+ // in the same state, so its calls share one result.
1853
+ const getMemoizedLevel = (expression: Expression & { kind: "Apply" }, environment: Environment) => {
1303
1854
  const definition = getDefinition(expression.operator, environment);
1304
- return (
1855
+ const isMemoizable =
1305
1856
  definition?.kind === "Operator" &&
1306
1857
  definition.params.length > 0 &&
1307
1858
  definition.params.every((param) => param.arity === 0) &&
1308
- isConstantLevel(definition, environment) &&
1309
- !isImpureDefinition(definition, environment)
1310
- );
1859
+ !isImpureDefinition(definition, environment);
1860
+ if (!isMemoizable) return undefined;
1861
+ if (isConstantLevel(definition, environment)) return "constant";
1862
+ return isStatePure(definition, environment) ? "state" : undefined;
1311
1863
  };
1312
1864
 
1313
1865
  const compileClosureCall = (
1314
1866
  closure: Closure,
1315
1867
  args: Expression[],
1316
1868
  environment: Environment,
1317
- isMemoized: boolean,
1869
+ memoizedLevel: "constant" | "state" | undefined,
1318
1870
  ): Evaluator => {
1319
1871
  const bindArguments = compileArgumentScope(closure, args, environment);
1320
1872
  const compiled = { body: undefined as Evaluator | undefined };
1321
1873
  const evaluateBody = (scope: Scope | undefined, context: Context) => {
1322
1874
  compiled.body ??= getEntryEvaluator(closure.body, environment);
1323
- return compiled.body({ environment, scope, state: context.state, next: context.next });
1875
+ return compiled.body({
1876
+ environment,
1877
+ scope,
1878
+ state: context.state,
1879
+ next: context.next,
1880
+ isKeepLazy: context.isKeepLazy,
1881
+ });
1324
1882
  };
1325
- if (!isMemoized) return (context) => evaluateBody(bindArguments(context), context);
1326
- const memo = getCallMemo(closure);
1883
+ if (!memoizedLevel || args.length !== closure.params.length) {
1884
+ return (context) => evaluateBody(bindArguments(context), context);
1885
+ }
1886
+ const memos = getCallMemos(closure);
1887
+ const keys = args.map((arg) => compileArgumentKey(arg, environment));
1888
+ const usage = { hits: 0, misses: 0 };
1327
1889
  return (context) => {
1328
- const scope = bindArguments(context);
1329
- const values = getArgumentValues(scope, args.length);
1330
- if (!values) return evaluateBody(scope, context);
1331
- let hash = values.length;
1332
- for (const value of values) hash = combineHash(hash, getValueHash(value));
1890
+ // A call TLC evaluates with EvalControl.KeepLazy builds what a memoized one wouldn't.
1891
+ if (context.isKeepLazy) return evaluateBody(bindArguments(context), context);
1892
+ callKey.sourceCount = 0;
1893
+ callKey.argCount = 0;
1894
+ callKey.hash = args.length;
1895
+ callKey.isStateKeyed = false;
1896
+ for (let index = 0; index < keys.length; index++) {
1897
+ if (!keys[index](context)) return evaluateBody(bindArguments(context), context);
1898
+ }
1899
+ const isStateKeyed = memoizedLevel === "state" || callKey.isStateKeyed;
1900
+ if (isStateKeyed && usage.misses > MEMOIZED_TRIAL_CALLS && usage.hits < usage.misses) {
1901
+ return evaluateBody(bindArguments(context), context);
1902
+ }
1903
+ const memo = isStateKeyed ? memos.state : memos.constant;
1904
+ if (isStateKeyed && memo.state !== context.state) {
1905
+ memo.state = context.state;
1906
+ memo.calls.clear();
1907
+ memo.size = 0;
1908
+ }
1909
+ // A small integer key keeps the Map from boxing it.
1910
+ const hash = callKey.hash & 0x3fffffff;
1333
1911
  const bucket = memo.calls.get(hash);
1334
- const known = bucket?.find((call) => isSameArguments(call.args, values));
1335
- if (known) return known.value;
1336
- const value = evaluateBody(scope, context);
1912
+ const known = findCall(bucket);
1913
+ if (known) {
1914
+ if (isStateKeyed) usage.hits++;
1915
+ return known.value;
1916
+ }
1917
+ if (isStateKeyed) usage.misses++;
1918
+ const sources = callKey.sources.slice(0, callKey.sourceCount);
1919
+ const keyArgs = callKey.args.slice(0, callKey.argCount);
1920
+ const value = evaluateBody(bindArguments(context), context);
1337
1921
  if (memo.size < MEMOIZED_CALL_LIMIT) {
1338
1922
  memo.size++;
1339
- if (bucket) bucket.push({ args: values, value });
1340
- else memo.calls.set(hash, [{ args: values, value }]);
1923
+ const call = { sources, args: keyArgs, value };
1924
+ if (bucket) bucket.push(call);
1925
+ else memo.calls.set(hash, [call]);
1341
1926
  }
1342
1927
  return value;
1343
1928
  };
@@ -1364,7 +1949,11 @@ const compileApply = (
1364
1949
  ? undefined
1365
1950
  : resolveGlobalOperator(expression.operator, environment);
1366
1951
  if (global?.kind === "Closure") {
1367
- return compileClosureCall(global, args, environment, isMemoizable(expression, environment));
1952
+ // The replay records the evaluation of each call, which TLC repeats.
1953
+ const memoizedLevel = environment.callStack
1954
+ ? undefined
1955
+ : getMemoizedLevel(expression, environment);
1956
+ return compileClosureCall(global, args, environment, memoizedLevel);
1368
1957
  }
1369
1958
  if (global?.kind === "Builtin" && global.name === "DOMAIN") {
1370
1959
  const operand = getEvaluator(args[0], environment);
@@ -1374,7 +1963,7 @@ const compileApply = (
1374
1963
  // TLC sorts the value of a constant TLCEval argument only where no context binds a name.
1375
1964
  if (global?.kind === "Builtin" && global.name === "TLCEval" && args.length === 1) {
1376
1965
  const operand = getEvaluator(args[0], environment);
1377
- if (contextualApplications.has(expression) || isStateDependent(args[0], environment)) {
1966
+ if (contextualApplications.has(expression) || isNonconstant(args[0], environment)) {
1378
1967
  return operand;
1379
1968
  }
1380
1969
  return (context) =>
@@ -1387,6 +1976,16 @@ const compileApply = (
1387
1976
  return (context) => subset(left(context), right(context));
1388
1977
  }
1389
1978
  if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
1979
+ // TLC refuses action composition unless a Java property enables its incomplete implementation.
1980
+ if (expression.operator === "\\cdot" && args.length === 2) {
1981
+ return () => {
1982
+ throw new TlaEvaluationError(
1983
+ environment.runtime.version === "1.7.4"
1984
+ ? "The current version of TLC does not support action composition."
1985
+ : "The current version of TLC does not support action composition. An incomplete implementation can be enabled via the tlc2.tool.impl.Tool.cdot=true java property.",
1986
+ );
1987
+ };
1988
+ }
1390
1989
  return (context) => {
1391
1990
  const operator = resolveOperator(expression.operator, context);
1392
1991
  if (operator) return callOperator(operator, args, context);
@@ -1412,15 +2011,15 @@ const compileQuantifier = (
1412
2011
  const name = bound.pattern.name;
1413
2012
  const set = getEvaluator(bound.set, environment);
1414
2013
  return (context) => {
1415
- const elements = getEnumeratedElements(set(context), bound.set);
1416
- for (let index = 0; index < elements.length; index++) {
1417
- const result = expectBoolean(
1418
- body(withScope(context, bindValue(context.scope, name, elements[index]))),
1419
- description,
1420
- );
1421
- if (result !== isForall) return result;
1422
- }
1423
- return isForall;
2014
+ const isFound = someElement(
2015
+ getQuantifierElements(set(context), bound.set, environment.runtime.version),
2016
+ (element) =>
2017
+ expectBoolean(
2018
+ body(withScope(context, bindValue(context.scope, name, element))),
2019
+ description,
2020
+ ) !== isForall,
2021
+ );
2022
+ return isFound ? !isForall : isForall;
1424
2023
  };
1425
2024
  }
1426
2025
  const bounds = expression.bounds;
@@ -1429,17 +2028,15 @@ const compileQuantifier = (
1429
2028
  const sets = bounds.map((item) => getEvaluator(item.set, environment));
1430
2029
  return (context) => {
1431
2030
  const lists = sets.map((set, index) =>
1432
- getEnumeratedElements(set(context), bounds[index].set),
2031
+ getQuantifierElements(set(context), bounds[index].set, environment.runtime.version),
1433
2032
  );
1434
2033
  const visit = (index: number, scope: Scope | undefined): boolean => {
1435
2034
  if (index < 0) {
1436
2035
  return expectBoolean(body(withScope(context, scope)), description) !== isForall;
1437
2036
  }
1438
- const list = lists[index];
1439
- for (let position = 0; position < list.length; position++) {
1440
- if (visit(index - 1, bindValue(scope, names[index], list[position]))) return true;
1441
- }
1442
- return false;
2037
+ return someElement(lists[index], (element) =>
2038
+ visit(index - 1, bindValue(scope, names[index], element)),
2039
+ );
1443
2040
  };
1444
2041
  return visit(bounds.length - 1, context.scope) ? !isForall : isForall;
1445
2042
  };
@@ -1487,28 +2084,26 @@ const compileFunctionConstruction = (
1487
2084
  const bound = bounds[0];
1488
2085
  const isSingleName = bounds.length === 1 && bound.pattern.kind === "Name";
1489
2086
  const set = getEvaluator(bound.set, environment);
1490
- return (context) => {
1491
- const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
1492
- if (expression.isRecursive || isKeptLazy || !isEnumerable(domain)) {
1493
- const functionValue = createLazyFunction(
1494
- domain,
1495
- (argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
1496
- getLazyFunctionSource(bounds, expression.body, domain),
1497
- );
1498
- return bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
1499
- }
2087
+ const construct = (domain: Value, context: Context) => {
1500
2088
  if (isSingleName && bound.pattern.kind === "Name") {
1501
2089
  const name = bound.pattern.name;
1502
2090
  const keys = getFunctionDomain(domain);
1503
- return markLambdaFunction(
1504
- createSortedFunction(
1505
- keys,
1506
- keys.map((key) => body(withScope(context, bindValue(context.scope, name, key)))),
1507
- ),
2091
+ const functionValue = createSortedFunction(
2092
+ keys,
2093
+ keys.map((key) => body(withScope(context, bindValue(context.scope, name, key)))),
1508
2094
  );
2095
+ // FcnLambdaValue#toFcnRcd builds a FcnRcdValue over an interval or an enumerated set.
2096
+ return isReducibleSet(domain) ? functionValue : markLambdaFunction(functionValue);
1509
2097
  }
1510
2098
  const pairs: [Value, Value][] = [];
1511
- forEachBinding(bounds, context, (scope) => {
2099
+ const sets = bounds.map((item) =>
2100
+ getNormalizedElements(
2101
+ evaluate(item.set, context),
2102
+ item.set,
2103
+ context.environment.runtime.version,
2104
+ ),
2105
+ );
2106
+ forEachBoundElement(bounds, sets, context.scope, (scope) => {
1512
2107
  const key =
1513
2108
  bounds.length === 1
1514
2109
  ? (lookupKey(bound.pattern, scope) ?? false)
@@ -1518,6 +2113,46 @@ const compileFunctionConstruction = (
1518
2113
  });
1519
2114
  return createFunction(pairs);
1520
2115
  };
2116
+ const createLazy = (domain: Value, context: Context) => {
2117
+ const functionValue = keepLambda({
2118
+ value: createLazyFunction(
2119
+ domain,
2120
+ (argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
2121
+ getLazyFunctionSource(bounds, expression.body, domain),
2122
+ ),
2123
+ bounds,
2124
+ body: expression.body,
2125
+ scope: context.scope,
2126
+ });
2127
+ return bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
2128
+ };
2129
+ const callStack = environment.callStack;
2130
+ return (context) => {
2131
+ const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
2132
+ if (expression.isRecursive || isKeptLazy || context.isKeepLazy || !isEnumerable(domain)) {
2133
+ return createLazy(domain, context);
2134
+ }
2135
+ // TLC builds a function over a domain other than a set enumeration or an interval lazily, so
2136
+ // its body fails only where TLC forces it, as the replay does. Building it at once is faster
2137
+ // otherwise.
2138
+ if (!isReducibleSet(domain)) {
2139
+ if (callStack) return createLazy(domain, context);
2140
+ try {
2141
+ return construct(domain, context);
2142
+ } catch (error) {
2143
+ if (!(error instanceof TlaEvaluationError)) throw error;
2144
+ return createLazy(domain, context);
2145
+ }
2146
+ }
2147
+ if (!callStack) return construct(domain, context);
2148
+ // TLC builds the function in a method of the FcnLambdaValue that CallStackTool gives a source.
2149
+ try {
2150
+ return construct(domain, context);
2151
+ } catch (error) {
2152
+ headFingerprint(callStack, expression, error);
2153
+ throw error;
2154
+ }
2155
+ };
1521
2156
  };
1522
2157
 
1523
2158
  const compileExcept = (
@@ -1563,7 +2198,20 @@ const compileExcept = (
1563
2198
  };
1564
2199
  return (context) => {
1565
2200
  let current = evaluateFunction(context);
1566
- for (const item of updates) current = update(current, item.path, 0, context, item.value);
2201
+ for (const item of updates) {
2202
+ // TLC leaves the value as it is when an update's path is not in it, and warns.
2203
+ const found = { value: false };
2204
+ current = update(current, item.path, 0, context, (inner) => {
2205
+ found.value = true;
2206
+ return item.value(inner);
2207
+ });
2208
+ if (!found.value) {
2209
+ context.environment.runtime.warn(
2210
+ MESSAGE_CODE.exceptAppliedToUnknownField,
2211
+ `The EXCEPT was applied to non-existing fields of the value at\n${describeRange(expression.function)}`,
2212
+ );
2213
+ }
2214
+ }
1567
2215
  return current;
1568
2216
  };
1569
2217
  };
@@ -1576,23 +2224,37 @@ const compileSetComprehension = (
1576
2224
  const predicate = getEvaluator(expression.predicate, environment);
1577
2225
  const set = getEvaluator(expression.bound.set, environment);
1578
2226
  const pattern = expression.bound.pattern;
2227
+ const range = expression.predicate.location.range;
2228
+ const source = range && {
2229
+ names: pattern.kind === "Name" ? pattern.name : pattern.names.join(", "),
2230
+ predicate: formatRange(range),
2231
+ };
2232
+ const createTest = (evaluator: Evaluator, context: Context) => (element: Value) =>
2233
+ expectBoolean(
2234
+ evaluator(withScope(context, bindPattern(context.scope, pattern, element))),
2235
+ "a set filter predicate",
2236
+ );
2237
+ const createFiltered = (
2238
+ base: Value,
2239
+ context: Context,
2240
+ evaluator: Evaluator,
2241
+ ): RebindableFilteredSet => ({
2242
+ kind: "FilteredSet",
2243
+ base,
2244
+ test: createTest(evaluator, context),
2245
+ source,
2246
+ rebind: (tracing) =>
2247
+ createFiltered(
2248
+ base,
2249
+ { ...context, environment: tracing },
2250
+ getEvaluator(expression.predicate, tracing),
2251
+ ),
2252
+ });
1579
2253
  return (context) => {
1580
2254
  const base = set(context);
1581
- const test = (element: Value) =>
1582
- expectBoolean(
1583
- predicate(withScope(context, bindPattern(context.scope, pattern, element))),
1584
- "a set filter predicate",
1585
- );
1586
- const isReducible =
1587
- typeof base === "object" &&
1588
- (base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
1589
- if (!isReducible) {
1590
- if (isSetValue(base)) return { kind: "FilteredSet", base, test };
1591
- throw new TlaEvaluationError(
1592
- `TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
1593
- );
1594
- }
1595
- return createSortedSet(toFiniteSet(base).elements.filter(test));
2255
+ // TLC filters any value it can't enumerate lazily, and fails only when it uses the result.
2256
+ if (!isReducibleSet(base)) return createFiltered(base, context, predicate);
2257
+ return createSortedSet(toFiniteSet(base).elements.filter(createTest(predicate, context)));
1596
2258
  };
1597
2259
  }
1598
2260
  if (expression.kind === "SetMap") {
@@ -1615,10 +2277,18 @@ const compileSetComprehension = (
1615
2277
  };
1616
2278
  }
1617
2279
  const body = getEvaluator(expression.body, environment);
1618
- const bound = { pattern: expression.pattern, set };
2280
+ const evaluateSet = getEvaluator(set, environment);
1619
2281
  return (context) => {
2282
+ const base = evaluateSet(context);
2283
+ if (!isSetValue(base) || isOverriddenSet(base)) {
2284
+ throw new TlaEvaluationError(
2285
+ `Attempted to compute the value of an expression of\nform CHOOSE x \\in S: P, but S was not enumerable.\n${describeRange(expression)}`,
2286
+ );
2287
+ }
1620
2288
  const chosen = { value: undefined as Value | undefined };
1621
- forEachBinding([bound], context, (scope) => {
2289
+ const elements = getNormalizedElements(base, set, context.environment.runtime.version);
2290
+ someElement(elements, (element) => {
2291
+ const scope = bindPattern(context.scope, expression.pattern, element);
1622
2292
  if (!expectBoolean(body(withScope(context, scope)), "the body of CHOOSE")) return false;
1623
2293
  chosen.value = lookupKey(expression.pattern, scope);
1624
2294
  return true;
@@ -1632,16 +2302,42 @@ const compileSetComprehension = (
1632
2302
  };
1633
2303
  };
1634
2304
 
1635
- const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
1636
- const fields = [...recordFields].sort((left, right) => compareValues(left.name, right.name));
1637
- const hasDuplicates = fields.some(
1638
- (field, index) => index > 0 && fields[index - 1].name === field.name,
2305
+ // TLC orders a record's fields by name.
2306
+ export const getSortedFields = (fields: RecordField[]) =>
2307
+ [...fields].sort((left, right) => compareValues(left.name, right.name));
2308
+
2309
+ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
2310
+ sortedFields.some((field, index) => index > 0 && sortedFields[index - 1].name === field.name);
2311
+
2312
+ // TLC's Tool#evalAppl fails to look up a variable the state doesn't assign.
2313
+ export const createUnassignedError = (name: string, expression: Expression) =>
2314
+ new TlaUnassignedError(
2315
+ `In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
2316
+ name,
2317
+ );
2318
+
2319
+ // TLC's Tool#enabled reads the subscript of <<A>>_v in the successor as a state of its own, and
2320
+ // names the fairness condition when A leaves a variable of v unassigned, unlike TLC 1.7.4's.
2321
+ export const toUnspecifiedSubscriptError = (error: unknown, version: TlcVersion | undefined) =>
2322
+ error instanceof TlaUnassignedError && version !== "1.7.4"
2323
+ ? new TlaEvaluationError(
2324
+ `The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
2325
+ )
2326
+ : error;
2327
+
2328
+ export const createPrimeOutsideActionError = (expression: Expression) =>
2329
+ new TlaEvaluationError(
2330
+ `A primed expression appears outside an action at ${describeLocation(expression)}`,
1639
2331
  );
2332
+
2333
+ const compileRecord = (recordFields: RecordField[], environment: Environment): Evaluator => {
2334
+ const fields = getSortedFields(recordFields);
2335
+ const hasDuplicates = hasDuplicateFields(fields);
1640
2336
  const values = fields.map((field) => getEvaluator(field.value, environment));
1641
2337
  if (hasDuplicates) {
1642
2338
  return (context) =>
1643
- createFunction(
1644
- fields.map((field, index): [Value, Value] => [field.name, values[index](context)]),
2339
+ createRecord(
2340
+ fields.map((field, index): [string, Value] => [field.name, values[index](context)]),
1645
2341
  );
1646
2342
  }
1647
2343
  const names = fields.map((field) => field.name);
@@ -1655,7 +2351,7 @@ const compileRecord = (recordFields: RecordField[], environment: Environment): E
1655
2351
  );
1656
2352
  }
1657
2353
  return (context) =>
1658
- createSortedFunction(
2354
+ createSortedRecord(
1659
2355
  names,
1660
2356
  values.map((value) => value(context)),
1661
2357
  );
@@ -1684,6 +2380,12 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1684
2380
  return compileIdentifier(expression, environment);
1685
2381
  case "Apply":
1686
2382
  return compileApply(expression, environment);
2383
+ case "Bracket":
2384
+ return () => {
2385
+ throw new TlaEvaluationError(
2386
+ `A bracket without a function or subscript is not an expression at ${describeLocation(expression)}`,
2387
+ );
2388
+ };
1687
2389
  case "OperatorReference":
1688
2390
  case "Lambda":
1689
2391
  return () => {
@@ -1696,7 +2398,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1696
2398
  return (context) => {
1697
2399
  for (let index = 0; index < operands.length; index++) {
1698
2400
  const value = operands[index](context);
1699
- if (typeof value !== "boolean") throw getConjunctError(expression, index, value);
2401
+ if (typeof value !== "boolean") throw getJunctionError(expression, index, value);
1700
2402
  if (!value) return false;
1701
2403
  }
1702
2404
  return true;
@@ -1706,7 +2408,9 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1706
2408
  const operands = expression.operands.map(child);
1707
2409
  return (context) => {
1708
2410
  for (let index = 0; index < operands.length; index++) {
1709
- if (expectBoolean(operands[index](context), "a disjunct")) return true;
2411
+ const value = operands[index](context);
2412
+ if (typeof value !== "boolean") throw getJunctionError(expression, index, value);
2413
+ if (value) return true;
1710
2414
  }
1711
2415
  return false;
1712
2416
  };
@@ -1714,16 +2418,13 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1714
2418
  case "Prime": {
1715
2419
  const inner = child(expression.expression);
1716
2420
  return (context) => {
1717
- if (!context.next) {
1718
- throw new TlaEvaluationError(
1719
- `A primed expression appears outside an action at ${describeLocation(expression)}`,
1720
- );
1721
- }
2421
+ if (!context.next) throw createPrimeOutsideActionError(expression);
1722
2422
  return inner({
1723
2423
  environment: context.environment,
1724
2424
  scope: context.scope,
1725
2425
  state: context.next,
1726
2426
  next: undefined,
2427
+ isKeepLazy: context.isKeepLazy,
1727
2428
  });
1728
2429
  };
1729
2430
  }
@@ -1748,16 +2449,25 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1748
2449
  }
1749
2450
  case "Case": {
1750
2451
  const arms = expression.arms.map((arm) => ({
2452
+ source: arm.condition,
1751
2453
  condition: child(arm.condition),
1752
2454
  body: child(arm.body),
1753
2455
  }));
1754
2456
  const other = expression.other ? child(expression.other) : undefined;
1755
2457
  return (context) => {
1756
2458
  for (const arm of arms) {
1757
- if (expectBoolean(arm.condition(context), "a CASE guard")) return arm.body(context);
2459
+ const value = arm.condition(context);
2460
+ if (typeof value !== "boolean") {
2461
+ throw new TlaEvaluationError(
2462
+ `A non-boolean expression (${getKindString(value)}) was used as a condition of a CASE. ${describeNode(arm.source)}`,
2463
+ );
2464
+ }
2465
+ if (value) return arm.body(context);
1758
2466
  }
1759
2467
  if (other) return other(context);
1760
- throw new TlaEvaluationError(`No CASE guard is true at ${describeLocation(expression)}`);
2468
+ throw new TlaEvaluationError(
2469
+ `Attempted to evaluate a CASE with no conditions true.\n${describeRange(expression)}`,
2470
+ );
1761
2471
  };
1762
2472
  }
1763
2473
  case "Let": {
@@ -1796,9 +2506,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1796
2506
  case "Record":
1797
2507
  return compileRecord(expression.fields, environment);
1798
2508
  case "RecordSet": {
1799
- const fields = [...expression.fields].sort((left, right) =>
1800
- compareValues(left.name, right.name),
1801
- );
2509
+ const fields = getSortedFields(expression.fields);
1802
2510
  const names = fields.map((field) => field.name);
1803
2511
  const sets = fields.map((field) => child(field.value));
1804
2512
  return (context) => ({ kind: "RecordSet", names, sets: sets.map((set) => set(context)) });
@@ -1817,20 +2525,27 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
1817
2525
  };
1818
2526
  case "FunctionApplication": {
1819
2527
  keepLazy(expression.function);
1820
- const evaluateFunction = child(expression.function);
2528
+ const evaluated = child(expression.function);
2529
+ const evaluateFunction = (context: Context) =>
2530
+ expectFunction(evaluated(toKeepLazyContext(context)), expression);
1821
2531
  const args = expression.args.map(child);
2532
+ const describeMissing = getMissingArgumentDescriber(environment.runtime.version);
1822
2533
  if (args.length === 1) {
1823
2534
  const argument = args[0];
1824
- return (context) => applyFunction(evaluateFunction(context), argument(context));
2535
+ return (context) =>
2536
+ applyFunction(evaluateFunction(context), argument(context), describeMissing);
1825
2537
  }
1826
2538
  return (context) =>
1827
- applyFunction(evaluateFunction(context), createTuple(args.map((arg) => arg(context))));
2539
+ applyFunction(
2540
+ evaluateFunction(context),
2541
+ createTuple(args.map((arg) => arg(context))),
2542
+ describeMissing,
2543
+ );
1828
2544
  }
1829
2545
  case "FieldAccess": {
1830
2546
  const record = child(expression.record);
1831
- const field = expression.field;
1832
- const describeMissing = getFieldError(expression);
1833
- return (context) => applyFunction(record(context), field, describeMissing);
2547
+ const select = getFieldSelector(expression);
2548
+ return (context) => select(record(context));
1834
2549
  }
1835
2550
  case "Product": {
1836
2551
  const factors = expression.factors.map(child);