tla-ts 0.0.2 → 0.0.3-dev.037ac3b

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 (63) hide show
  1. package/README.md +78 -12
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +60 -1
  4. package/dist/index.d.ts +60 -1
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +2 -2
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +21 -2
  11. package/dist/run-cli.js +21 -2
  12. package/dist/transfer.cjs +27 -15
  13. package/dist/transfer.js +27 -15
  14. package/package.json +3 -1
  15. package/src/ast.ts +134 -4
  16. package/src/check.ts +280 -80
  17. package/src/cli-options.ts +64 -0
  18. package/src/codegen.ts +9 -19
  19. package/src/dependencies.ts +2 -2
  20. package/src/environment.ts +75 -16
  21. package/src/errors.ts +46 -3
  22. package/src/evaluate.ts +88 -143
  23. package/src/exit-codes.ts +21 -43
  24. package/src/fingerprints.ts +46 -11
  25. package/src/instances.ts +15 -14
  26. package/src/levels.ts +8 -8
  27. package/src/lexer.ts +11 -2
  28. package/src/live-check.ts +692 -0
  29. package/src/liveness.ts +54 -535
  30. package/src/obligation-action.ts +384 -0
  31. package/src/obligation-anon.ts +575 -0
  32. package/src/obligation-deref.ts +637 -0
  33. package/src/obligation-expression.ts +1060 -0
  34. package/src/obligation-format.ts +406 -0
  35. package/src/obligation-generate.ts +1074 -0
  36. package/src/obligation-levels.ts +329 -0
  37. package/src/obligation-module.ts +1042 -0
  38. package/src/obligation-printer.ts +889 -0
  39. package/src/obligation-proof.ts +76 -0
  40. package/src/obligation-source.ts +1084 -0
  41. package/src/obligation-tuples.ts +184 -0
  42. package/src/obligations.ts +316 -0
  43. package/src/parallel-worker.ts +166 -210
  44. package/src/parallel.ts +76 -35
  45. package/src/parser.ts +297 -53
  46. package/src/resolve.ts +16 -15
  47. package/src/run-cli.ts +353 -40
  48. package/src/runtime.ts +3 -1
  49. package/src/spec-description.ts +18 -6
  50. package/src/standard-levels.ts +3 -3
  51. package/src/standard-modules.ts +2 -7
  52. package/src/state-graph.ts +157 -0
  53. package/src/subexpressions.ts +10 -10
  54. package/src/successors.ts +77 -13
  55. package/src/symmetry.ts +14 -2
  56. package/src/tableau.ts +50 -23
  57. package/src/tlc-ext.ts +29 -12
  58. package/src/tlc-module.ts +9 -28
  59. package/src/tlc-order.ts +17 -3
  60. package/src/tlc-random.ts +9 -6
  61. package/src/tlc-summary.ts +20 -5
  62. package/src/transfer.ts +22 -1
  63. package/src/values.ts +118 -34
package/src/tlc-random.ts CHANGED
@@ -181,8 +181,10 @@ const createShiftTable = (length: number) => {
181
181
 
182
182
  // Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
183
183
  // XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
184
- // The fingerprinter caches each distinct variable value for one pass over the stored states.
185
- export const createTlcFingerprinter = () => {
184
+ // The fingerprinter caches each distinct variable value for one pass over the stored states. It
185
+ // reads a state's variables in `order` and writes each fingerprint's low and high words into
186
+ // `words`, an Int32Array over a little-endian BigInt64Array.
187
+ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
186
188
  const shiftTables = new Map<number, Int32Array>();
187
189
  const indices = new Map<Value, number>();
188
190
  const contributions: number[] = [];
@@ -204,11 +206,11 @@ export const createTlcFingerprinter = () => {
204
206
  shiftTables.set(length, table);
205
207
  return table;
206
208
  };
207
- return (state: Value[]) => {
209
+ return (state: Value[], position: number) => {
208
210
  const outer = { high, low };
209
211
  const fingerprint = { high: INITIAL_HIGH, low: INITIAL_LOW };
210
- for (const value of state) {
211
- const contribution = getContribution(value);
212
+ for (const index of order) {
213
+ const contribution = getContribution(state[index]);
212
214
  const table = getShiftTable(contributions[contribution]);
213
215
  const extended = {
214
216
  high: contributions[contribution + 1],
@@ -225,7 +227,8 @@ export const createTlcFingerprinter = () => {
225
227
  }
226
228
  high = outer.high;
227
229
  low = outer.low;
228
- return (BigInt(fingerprint.high >>> 0) << 32n) | BigInt(fingerprint.low >>> 0);
230
+ words[2 * position] = fingerprint.low;
231
+ words[2 * position + 1] = fingerprint.high;
229
232
  };
230
233
  };
231
234
 
@@ -87,11 +87,26 @@ export const getFingerprintDistance = (
87
87
  const probability = getOptimisticProbability(distinctStates, generatedStates);
88
88
  if (probability < ACTUAL_PROBABILITY_THRESHOLD) return undefined;
89
89
  const fingerprints = getFingerprints().sort();
90
- return fingerprints.reduce((distance, fingerprint, index) => {
91
- const previous = fingerprints[index - 1];
92
- const isSameSet = index > 0 && previous < 0n === fingerprint < 0n;
93
- return isSameSet && fingerprint - previous < distance ? fingerprint - previous : distance;
94
- }, 0x7fff_ffff_ffff_ffffn);
90
+ // The low and high words of each fingerprint. A gap whose high words differ by at most 2^20 is
91
+ // below 2^53, so a number holds it exactly and only wider gaps need a BigInt.
92
+ const words = new Int32Array(fingerprints.buffer);
93
+ const nearest = { narrow: Number.POSITIVE_INFINITY, wide: 0x7fff_ffff_ffff_ffffn };
94
+ for (let index = 1; index < fingerprints.length; index++) {
95
+ const high = words[2 * index + 1];
96
+ const previousHigh = words[2 * index - 1];
97
+ if (high < 0 !== previousHigh < 0) continue;
98
+ const highGap = high - previousHigh;
99
+ if (highGap <= 0x10_0000) {
100
+ const gap = highGap * 0x1_0000_0000 + (words[2 * index] >>> 0) - (words[2 * index - 2] >>> 0);
101
+ nearest.narrow = Math.min(nearest.narrow, gap);
102
+ } else {
103
+ const gap = fingerprints[index] - fingerprints[index - 1];
104
+ if (gap < nearest.wide) nearest.wide = gap;
105
+ }
106
+ }
107
+ if (nearest.narrow === Number.POSITIVE_INFINITY) return nearest.wide;
108
+ const narrow = BigInt(nearest.narrow);
109
+ return narrow < nearest.wide ? narrow : nearest.wide;
95
110
  };
96
111
 
97
112
  export const getOutDegreeSummary = (buckets: number[]) => {
package/src/transfer.ts CHANGED
@@ -1,4 +1,11 @@
1
- import { createModelValue, createSortedFunction, createSortedSet, type Value } from "./values";
1
+ import {
2
+ createModelValue,
3
+ createSortedFunction,
4
+ createSortedSet,
5
+ getDomainInterval,
6
+ getFunctionDomain,
7
+ type Value,
8
+ } from "./values";
2
9
 
3
10
  const FALSE = 0;
4
11
  const TRUE = 1;
@@ -7,6 +14,7 @@ const STRING = 3;
7
14
  const MODEL_VALUE = 4;
8
15
  const SET = 5;
9
16
  const FUNCTION = 6;
17
+ const INTERVAL_FUNCTION = 7;
10
18
 
11
19
  export interface EncodedStates {
12
20
  words: Int32Array;
@@ -65,6 +73,13 @@ export const createStateEncoder = (): StateEncoder => {
65
73
  return value.elements.every(encodeValue);
66
74
  }
67
75
  if (value.kind === "Function") {
76
+ const interval = getDomainInterval(value.domain);
77
+ if (interval) {
78
+ push(INTERVAL_FUNCTION);
79
+ push(interval.low);
80
+ push(value.range.length);
81
+ return value.range.every(encodeValue);
82
+ }
68
83
  push(FUNCTION);
69
84
  push(value.domain.length);
70
85
  return value.domain.every(encodeValue) && value.range.every(encodeValue);
@@ -102,6 +117,12 @@ export const createStateDecoder =
102
117
  if (tag === INTEGER) return words[cursor.index++];
103
118
  if (tag === STRING) return encoded.strings[words[cursor.index++]];
104
119
  if (tag === MODEL_VALUE) return createModelValue(encoded.strings[words[cursor.index++]]);
120
+ if (tag === INTERVAL_FUNCTION) {
121
+ const low = words[cursor.index++];
122
+ const length = words[cursor.index++];
123
+ const domain = getFunctionDomain({ kind: "Interval", low, high: low + length - 1 });
124
+ return createSortedFunction(domain, decodeElements(length));
125
+ }
105
126
  const length = words[cursor.index++];
106
127
  if (tag === SET) return createSortedSet(decodeElements(length));
107
128
  const domain = decodeElements(length);
package/src/values.ts CHANGED
@@ -4,6 +4,8 @@ import { prettyPrint } from "./pretty-print";
4
4
  export interface ModelValue {
5
5
  kind: "ModelValue";
6
6
  name: string;
7
+ type: string | undefined;
8
+ rank: number;
7
9
  }
8
10
 
9
11
  export interface SetValue {
@@ -135,6 +137,13 @@ export const isSetValue = (value: Value) =>
135
137
  value.kind !== "Function" &&
136
138
  value.kind !== "LazyFunction";
137
139
 
140
+ export type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
141
+
142
+ // TLC reduces a set enumeration or an interval eagerly, and keeps every other set lazy.
143
+ export const isReducible = (value: Value): value is ReducibleSet =>
144
+ typeof value === "object" &&
145
+ (value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
146
+
138
147
  export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
139
148
  typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
140
149
 
@@ -233,8 +242,7 @@ const compareSets = (left: SetValue, right: SetValue) => {
233
242
  return 0;
234
243
  };
235
244
 
236
- const getModelValueType = (value: Value) =>
237
- isModelValue(value) && value.name.length > 2 && value.name[1] === "_" ? value.name[0] : undefined;
245
+ const getModelValueType = (value: Value) => (isModelValue(value) ? value.type : undefined);
238
246
 
239
247
  const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) => {
240
248
  const leftType = getModelValueType(left);
@@ -264,6 +272,7 @@ const stringOrder = { ranks: Object.create(null) as Record<string, number>, size
264
272
  export const setStringOrder = (texts: Iterable<string>) => {
265
273
  stringOrder.ranks = Object.create(null);
266
274
  stringOrder.size = 0;
275
+ modelValues.clear();
267
276
  for (const text of texts) rankString(text);
268
277
  };
269
278
 
@@ -349,11 +358,14 @@ export const compareValues = (left: Value, right: Value): number => {
349
358
  const otherSize = Math.max(0, right.high - right.low + 1);
350
359
  return size !== otherSize || size === 0 ? size - otherSize : left.low - right.low;
351
360
  }
361
+ if (leftKind === "ModelValue" && rightKind === "ModelValue") {
362
+ if (left.type !== undefined && right.type !== undefined) {
363
+ checkModelValueTypes(left, right, false);
364
+ }
365
+ return left.rank - right.rank;
366
+ }
352
367
  if (leftKind === "ModelValue" || rightKind === "ModelValue") {
353
368
  checkModelValueTypes(left, right, false);
354
- if (leftKind === "ModelValue" && rightKind === "ModelValue") {
355
- return compareStrings(left.name, right.name);
356
- }
357
369
  return leftKind === "ModelValue" ? -1 : 1;
358
370
  }
359
371
  if (left === ANY_SET || right === ANY_SET) return compareAny(left === ANY_SET ? right : left);
@@ -366,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
366
378
  `Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
367
379
  );
368
380
  }
369
- if (isLeftSet) return compareSets(toFiniteSet(left), toFiniteSet(right));
381
+ if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
370
382
  return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
371
383
  }
372
384
  if (typeof left === "number" && typeof right === "number") return left - right;
@@ -382,11 +394,16 @@ export const compareValues = (left: Value, right: Value): number => {
382
394
 
383
395
  const modelValues = new Map<string, ModelValue>();
384
396
 
397
+ // A check ranks every model value's name when it creates the value, and creates its own values.
385
398
  export const createModelValue = (name: string): ModelValue => {
386
- rankString(name);
387
399
  const known = modelValues.get(name);
388
400
  if (known) return known;
389
- const value: ModelValue = { kind: "ModelValue", name };
401
+ const value: ModelValue = {
402
+ kind: "ModelValue",
403
+ name,
404
+ type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
405
+ rank: getStringRank(name),
406
+ };
390
407
  modelValues.set(name, value);
391
408
  return value;
392
409
  };
@@ -555,7 +572,7 @@ const equalsModelValueWith = (modelValue: ModelValue, other: Value) => {
555
572
 
556
573
  const compareModelValue = (left: ModelValue, right: Value) => {
557
574
  const type = getModelValueType(left);
558
- if (type === undefined) return isModelValue(right) ? compareStrings(left.name, right.name) : -1;
575
+ if (type === undefined) return isModelValue(right) ? left.rank - right.rank : -1;
559
576
  if (!isModelValue(right)) {
560
577
  throw new TlaEvaluationError(
561
578
  `Attempted to compare the typed model value ${formatValue(left)} and non-model value\n${formatValue(right)}`,
@@ -567,7 +584,7 @@ const compareModelValue = (left: ModelValue, right: Value) => {
567
584
  `Attempted to compare the differently-typed model values ${formatValue(left)} and ${formatValue(right)}`,
568
585
  );
569
586
  }
570
- return compareStrings(left.name, right.name);
587
+ return left.rank - right.rank;
571
588
  };
572
589
 
573
590
  const equalsModelValue = (left: ModelValue, right: Value) => {
@@ -854,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
854
871
  }
855
872
  };
856
873
 
874
+ // TLC sorts a set by comparing each element with the ones before it, so the later element converts
875
+ // a lazy set it compares and caches it.
857
876
  const isStrictlySorted = (elements: Value[]) => {
858
877
  for (let index = 1; index < elements.length; index++) {
859
- if (compareValues(elements[index - 1], elements[index]) >= 0) return false;
878
+ if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
860
879
  }
861
880
  return true;
862
881
  };
@@ -1059,13 +1078,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
1059
1078
  const formatUnion = (value: UnionValue, isStrict = false) =>
1060
1079
  `UNION(${formatValue(value.base, isStrict)})`;
1061
1080
 
1062
- const getUnionSets = (value: UnionValue) => {
1081
+ const getUnionSets = (
1082
+ value: UnionValue,
1083
+ getElements = (set: Value) => toFiniteSet(set).elements,
1084
+ ) => {
1063
1085
  if (!isTlcEnumerable(value.base)) {
1064
1086
  throw new TlaEvaluationError(
1065
1087
  `Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
1066
1088
  );
1067
1089
  }
1068
- return toFiniteSet(value.base).elements;
1090
+ return getElements(value.base);
1069
1091
  };
1070
1092
 
1071
1093
  const getUnionSet = (set: Value, index: number, union: UnionValue) => {
@@ -1101,7 +1123,7 @@ export const isEnumerable = (value: Value): boolean => {
1101
1123
  case "ProductSet":
1102
1124
  return value.factors.every(isEnumerable) || getEmptiness(value) === true;
1103
1125
  case "Union":
1104
- return isEnumerable(value.base) && toFiniteSet(value.base).elements.every(isEnumerable);
1126
+ return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
1105
1127
  case "SetOperation":
1106
1128
  if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
1107
1129
  if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
@@ -1308,6 +1330,7 @@ const enumerateSet = (value: Value): Value[] => {
1308
1330
  };
1309
1331
 
1310
1332
  const finiteSets = new WeakMap<ObjectValue, SetValue>();
1333
+ const comparedSets = new WeakMap<ObjectValue, SetValue>();
1311
1334
 
1312
1335
  export const toFiniteSet = (value: Value): SetValue => {
1313
1336
  if (typeof value === "object" && value.kind === "Set") return value;
@@ -1316,6 +1339,13 @@ export const toFiniteSet = (value: Value): SetValue => {
1316
1339
  }
1317
1340
  const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1318
1341
  if (cached) return cached;
1342
+ const set =
1343
+ (typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
1344
+ if (typeof value === "object") finiteSets.set(value, set);
1345
+ return set;
1346
+ };
1347
+
1348
+ const enumerateFiniteSet = (value: Value) => {
1319
1349
  const elements = enumerateSet(value);
1320
1350
  const isSorted =
1321
1351
  typeof value === "object" &&
@@ -1326,8 +1356,20 @@ export const toFiniteSet = (value: Value): SetValue => {
1326
1356
  value.kind === "ProductSet" ||
1327
1357
  value.kind === "FunctionSet" ||
1328
1358
  value.kind === "Subset");
1329
- const set = isSorted ? createSortedSet(elements) : createSet(elements);
1330
- if (typeof value === "object") finiteSets.set(value, set);
1359
+ return isSorted ? createSortedSet(elements) : createSet(elements);
1360
+ };
1361
+
1362
+ // TLC converts the set a comparison receives as its argument without caching it, so a lazy set
1363
+ // compared that way still streams the sets it combines.
1364
+ const toComparedSet = (value: Value) => {
1365
+ const isStreamed =
1366
+ typeof value === "object" &&
1367
+ (value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
1368
+ if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
1369
+ const compared = comparedSets.get(value);
1370
+ if (compared) return compared;
1371
+ const set = enumerateFiniteSet(value);
1372
+ comparedSets.set(value, set);
1331
1373
  return set;
1332
1374
  };
1333
1375
 
@@ -1335,8 +1377,10 @@ export const toFiniteSet = (value: Value): SetValue => {
1335
1377
  // so an element that two of them share comes up once for each.
1336
1378
  export const getStreamedElements = (value: Value): Value[] => {
1337
1379
  if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
1380
+ // TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
1381
+ // its elements twice.
1338
1382
  if (value.kind === "Union") {
1339
- return getUnionSets(value).flatMap((set, index) =>
1383
+ return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
1340
1384
  getStreamedElements(getUnionSet(set, index, value)),
1341
1385
  );
1342
1386
  }
@@ -1667,6 +1711,25 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1667
1711
  }
1668
1712
  };
1669
1713
 
1714
+ // A function that agrees with a member of a function set outside some entries is a member when
1715
+ // those entries are.
1716
+ export const isUpdatedElementOf = (element: Value, member: Value, set: Value): boolean => {
1717
+ if (element === member) return true;
1718
+ if (
1719
+ typeof element !== "object" ||
1720
+ typeof member !== "object" ||
1721
+ typeof set !== "object" ||
1722
+ element.kind !== "Function" ||
1723
+ member.kind !== "Function" ||
1724
+ set.kind !== "FunctionSet" ||
1725
+ element.domain !== member.domain
1726
+ )
1727
+ return isElementOf(element, set);
1728
+ return element.range.every((item, index) =>
1729
+ isUpdatedElementOf(item, member.range[index], set.range),
1730
+ );
1731
+ };
1732
+
1670
1733
  export const toFiniteFunction = (value: Value): FunctionValue => {
1671
1734
  if (typeof value === "object" && value.kind === "Function") return value;
1672
1735
  if (typeof value === "object" && value.kind === "LazyFunction") {
@@ -1694,6 +1757,19 @@ const describeMissingArgument = (functionValue: Value, argument: Value) =>
1694
1757
  ? `In applying the function\n${prettyPrint(formatValue(functionValue))},\nthe first argument is:\n${prettyPrint(formatValue(argument))}\nwhich is not in its domain.\n`
1695
1758
  : `Attempted to apply function ${formatValue(functionValue)} to ${formatValue(argument)}, which is not in its domain`;
1696
1759
 
1760
+ const findKeyIndex = (domain: Value[], argument: Value) => {
1761
+ const first = domain[0];
1762
+ if (typeof argument === "number" && typeof first === "number") {
1763
+ const offset = argument - first;
1764
+ if (domain[offset] === argument) return offset;
1765
+ }
1766
+ if (domain.length <= 8) {
1767
+ for (let index = 0; index < domain.length; index++)
1768
+ if (domain[index] === argument) return index;
1769
+ }
1770
+ return findIndex(domain, argument);
1771
+ };
1772
+
1697
1773
  export const applyFunction = (
1698
1774
  functionValue: Value,
1699
1775
  argument: Value,
@@ -1714,17 +1790,7 @@ export const applyFunction = (
1714
1790
  `Attempted to apply ${formatValue(functionValue)}, which is not a function`,
1715
1791
  );
1716
1792
  }
1717
- const domain = functionValue.domain;
1718
- const first = domain[0];
1719
- if (typeof argument === "number" && typeof first === "number") {
1720
- const offset = argument - first;
1721
- if (domain[offset] === argument) return functionValue.range[offset];
1722
- }
1723
- if (domain.length <= 8) {
1724
- for (let index = 0; index < domain.length; index++)
1725
- if (domain[index] === argument) return functionValue.range[index];
1726
- }
1727
- const position = findIndex(domain, argument);
1793
+ const position = findKeyIndex(functionValue.domain, argument);
1728
1794
  if (position === -1) {
1729
1795
  throw new TlaEvaluationError(
1730
1796
  describeMissing?.(functionValue) ?? describeMissingArgument(functionValue, argument),
@@ -1733,12 +1799,16 @@ export const applyFunction = (
1733
1799
  return functionValue.range[position];
1734
1800
  };
1735
1801
 
1802
+ export const getDomainInterval = (domain: Value[]) =>
1803
+ isIntervalDomain(domain)
1804
+ ? (intervalArrays.get(domain) ?? getIntervalValue(domain.length))
1805
+ : undefined;
1806
+
1736
1807
  export const getDomain = (functionValue: Value): Value => {
1737
1808
  if (typeof functionValue === "object" && functionValue.kind === "LazyFunction")
1738
1809
  return functionValue.domain;
1739
1810
  const domain = toFiniteFunction(functionValue).domain;
1740
- if (!isIntervalDomain(domain)) return createSortedSet(domain);
1741
- return intervalArrays.get(domain) ?? getIntervalValue(domain.length);
1811
+ return getDomainInterval(domain) ?? createSortedSet(domain);
1742
1812
  };
1743
1813
 
1744
1814
  export const updateFunction = (
@@ -1762,7 +1832,7 @@ export const updateFunction = (
1762
1832
  };
1763
1833
  }
1764
1834
  const finite = toFiniteFunction(functionValue);
1765
- const position = findIndex(finite.domain, argument);
1835
+ const position = findKeyIndex(finite.domain, argument);
1766
1836
  if (position === -1) return finite;
1767
1837
  const range = [...finite.range];
1768
1838
  const previous = finite.range[position];
@@ -2065,8 +2135,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
2065
2135
  return `[${parameters} |-> <expression ${source.body}>]`;
2066
2136
  }
2067
2137
  const functionValue = toFiniteFunction(value);
2138
+ // TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
2139
+ const formatRange = (inner: Value) =>
2140
+ formatValue(inner, isStrict, isNormalized && value.kind === "Function");
2068
2141
  if (functionValue.domain.every((key, index) => key === index + 1)) {
2069
- return `<<${functionValue.range.map(format).join(", ")}>>`;
2142
+ return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
2070
2143
  }
2071
2144
  if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
2072
2145
  const isSorted =
@@ -2074,9 +2147,20 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
2074
2147
  const pairs = isSorted
2075
2148
  ? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
2076
2149
  : getWrittenPairs(functionValue);
2077
- return `[${pairs.map((pair) => `${String(pair[0])} |-> ${format(pair[1])}`).join(", ")}]`;
2150
+ return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
2078
2151
  }
2079
- return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${format(functionValue.range[index])}`).join(" @@ ")})`;
2152
+ return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
2080
2153
  }
2081
2154
  }
2082
2155
  };
2156
+
2157
+ // TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
2158
+ export const formatState = (state: Record<string, Value>) => {
2159
+ const entries = Object.entries(state);
2160
+ return entries
2161
+ .map(
2162
+ ([name, value]) =>
2163
+ `${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
2164
+ )
2165
+ .join("");
2166
+ };