tla-ts 0.0.8 → 0.0.9-dev.331c500

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 +23 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +48 -48
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +27 -9
  8. package/dist/index.d.ts +27 -9
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove.cjs +30 -30
  13. package/dist/prove.js +32 -32
  14. package/dist/run.cjs +28 -28
  15. package/dist/run.js +31 -31
  16. package/dist/tlapm.cjs +2 -2
  17. package/dist/tlapm.js +1 -1
  18. package/dist/transfer.cjs +30 -33
  19. package/dist/transfer.js +30 -33
  20. package/dist/worker.cjs +4 -4
  21. package/dist/worker.js +4 -4
  22. package/package.json +3 -1
  23. package/src/check.ts +77 -46
  24. package/src/cli/options.ts +2 -1
  25. package/src/cli/tlc.ts +26 -8
  26. package/src/cli/typescript.ts +26 -0
  27. package/src/codegen.ts +0 -5
  28. package/src/dependencies.ts +348 -24
  29. package/src/environment.ts +27 -11
  30. package/src/errors.ts +15 -0
  31. package/src/evaluate.ts +188 -712
  32. package/src/instances.ts +67 -6
  33. package/src/lexer.ts +140 -51
  34. package/src/liveness-checker.ts +6 -3
  35. package/src/liveness.ts +8 -18
  36. package/src/parallel-worker.ts +7 -5
  37. package/src/parallel.ts +18 -9
  38. package/src/prover/smt/solver/solver.ts +31 -27
  39. package/src/sany/generator.ts +9 -7
  40. package/src/sany/linter.ts +30 -21
  41. package/src/search.ts +48 -32
  42. package/src/spec-description.ts +1 -1
  43. package/src/successors.ts +6 -12
  44. package/src/symmetry.ts +32 -143
  45. package/src/tlc/fingerprint.ts +69 -116
  46. package/src/tlc/order.ts +27 -48
  47. package/src/tlc/replay.ts +1 -1
  48. package/src/typescript/actions.ts +494 -0
  49. package/src/typescript/collections.ts +615 -0
  50. package/src/typescript/compile.ts +70 -0
  51. package/src/typescript/context.ts +106 -0
  52. package/src/typescript/emit.ts +122 -0
  53. package/src/typescript/expressions.ts +508 -0
  54. package/src/typescript/formula.ts +160 -0
  55. package/src/typescript/functions.ts +295 -0
  56. package/src/typescript/model.ts +164 -0
  57. package/src/typescript/names.ts +116 -0
  58. package/src/typescript/program.ts +51 -0
  59. package/src/typescript/properties.ts +142 -0
  60. package/src/typescript/scope.ts +117 -0
  61. package/src/typescript/steps.ts +175 -0
  62. package/src/typescript/types.ts +178 -0
  63. package/src/values.ts +151 -85
package/src/values.ts CHANGED
@@ -7,6 +7,16 @@ export interface ModelValue {
7
7
  name: string;
8
8
  type: string | undefined;
9
9
  rank: number;
10
+ hash: number;
11
+ fingerprint: number;
12
+ }
13
+
14
+ // What TLC keeps on a set it builds lazily: the enumeration it converted to and cached, as
15
+ // SetPredValue#convertAndCache does, whether it has cached it, and whether it has fingerprinted it.
16
+ interface LazySet {
17
+ enumerated?: SetValue;
18
+ isCached?: boolean;
19
+ isFingerprinted?: boolean;
10
20
  }
11
21
 
12
22
  export interface SetValue {
@@ -19,7 +29,7 @@ export interface SetValue {
19
29
 
20
30
  // A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
21
31
  // only when it has to, so it still checks membership in one element by element.
22
- export interface MixedSetValue {
32
+ export interface MixedSetValue extends LazySet {
23
33
  kind: "MixedSet";
24
34
  elements: Value[];
25
35
  }
@@ -46,34 +56,34 @@ export interface InfiniteSetValue {
46
56
  name: "Nat" | "Int" | "STRING" | "Any";
47
57
  }
48
58
 
49
- export interface SubsetValue {
59
+ export interface SubsetValue extends LazySet {
50
60
  kind: "Subset";
51
61
  base: Value;
52
62
  }
53
63
 
54
- export interface FunctionSetValue {
64
+ export interface FunctionSetValue extends LazySet {
55
65
  kind: "FunctionSet";
56
66
  domain: Value;
57
67
  range: Value;
58
68
  }
59
69
 
60
- export interface RecordSetValue {
70
+ export interface RecordSetValue extends LazySet {
61
71
  kind: "RecordSet";
62
72
  names: string[];
63
73
  sets: Value[];
64
74
  }
65
75
 
66
- export interface ProductSetValue {
76
+ export interface ProductSetValue extends LazySet {
67
77
  kind: "ProductSet";
68
78
  factors: Value[];
69
79
  }
70
80
 
71
- export interface SequenceSetValue {
81
+ export interface SequenceSetValue extends LazySet {
72
82
  kind: "SequenceSet";
73
83
  base: Value;
74
84
  }
75
85
 
76
- export interface SetOperationValue {
86
+ export interface SetOperationValue extends LazySet {
77
87
  kind: "SetOperation";
78
88
  operator: "\\cup" | "\\cap" | "\\";
79
89
  left: Value;
@@ -86,7 +96,7 @@ interface FilteredSetSource {
86
96
  predicate: string;
87
97
  }
88
98
 
89
- export interface FilteredSetValue {
99
+ export interface FilteredSetValue extends LazySet {
90
100
  kind: "FilteredSet";
91
101
  base: Value;
92
102
  test: (element: Value) => boolean;
@@ -94,7 +104,7 @@ export interface FilteredSetValue {
94
104
  }
95
105
 
96
106
  // UNION S when some element of S isn't a set enumeration. TLC enumerates it only when it has to.
97
- export interface UnionValue {
107
+ export interface UnionValue extends LazySet {
98
108
  kind: "Union";
99
109
  base: Value;
100
110
  }
@@ -109,12 +119,16 @@ export interface LazyFunctionSource {
109
119
  body: string;
110
120
  }
111
121
 
122
+ // enumerated is the FcnRcdValue TLC converts the function to and keeps, as FcnLambdaValue#toFcnRcd
123
+ // does, from the first time it compares or fingerprints the function.
112
124
  export interface LazyFunctionValue {
113
125
  kind: "LazyFunction";
114
126
  domain: Value;
115
127
  apply: (argument: Value) => Value;
116
128
  source?: LazyFunctionSource;
117
129
  functionClass?: FunctionClass;
130
+ enumerated?: FunctionValue;
131
+ isFingerprinted?: boolean;
118
132
  }
119
133
 
120
134
  export type Value =
@@ -139,6 +153,26 @@ export type Value =
139
153
 
140
154
  export type ObjectValue = Exclude<Value, boolean | number | string>;
141
155
 
156
+ // The sets TLC builds lazily, which it converts and caches as it uses them.
157
+ type ConvertibleSetValue =
158
+ | MixedSetValue
159
+ | SubsetValue
160
+ | FunctionSetValue
161
+ | RecordSetValue
162
+ | ProductSetValue
163
+ | SequenceSetValue
164
+ | SetOperationValue
165
+ | FilteredSetValue
166
+ | UnionValue;
167
+
168
+ const isLazySet = (value: ObjectValue): value is ConvertibleSetValue =>
169
+ value.kind !== "ModelValue" &&
170
+ value.kind !== "Set" &&
171
+ value.kind !== "Function" &&
172
+ value.kind !== "Interval" &&
173
+ value.kind !== "InfiniteSet" &&
174
+ value.kind !== "LazyFunction";
175
+
142
176
  export const MAXIMUM_INTEGER = 2147483647;
143
177
  export const MINIMUM_INTEGER = -2147483648;
144
178
  const MAXIMUM_ENUMERATED_SUBSET_BASE = 30;
@@ -281,20 +315,39 @@ const checkModelValueTypes = (left: Value, right: Value, isEquality: boolean) =>
281
315
  );
282
316
  };
283
317
 
318
+ interface StringOrder {
319
+ ranks: Record<string, number>;
320
+ size: number;
321
+ // A parallel search's workers rank the strings they create in different orders, so the search
322
+ // notes the first string it orders against another string ranked from this rank on.
323
+ searchRank: number | undefined;
324
+ unorderedString: string | undefined;
325
+ }
326
+
284
327
  // TLC orders strings and model value names by when it first interned the text, so every
285
328
  // check sets the order of its spec's strings before it builds any value.
286
- const stringOrder: { ranks: Record<string, number>; size: number } = {
329
+ const stringOrder: StringOrder = {
287
330
  ranks: Object.create(null),
288
331
  size: 0,
332
+ searchRank: undefined,
333
+ unorderedString: undefined,
289
334
  };
290
335
 
291
336
  export const setStringOrder = (texts: Iterable<string>) => {
292
337
  stringOrder.ranks = Object.create(null);
293
338
  stringOrder.size = 0;
339
+ stringOrder.searchRank = undefined;
340
+ stringOrder.unorderedString = undefined;
294
341
  modelValues.clear();
295
342
  for (const text of texts) rankString(text);
296
343
  };
297
344
 
345
+ export const freezeStringOrder = () => {
346
+ stringOrder.searchRank = stringOrder.size;
347
+ };
348
+
349
+ export const getUnorderedString = () => stringOrder.unorderedString;
350
+
298
351
  // TLC 1.7.4 prints an empty function that isn't a tuple with a space between its brackets, and
299
352
  // looks a key up in a function differently, so every check also sets the version its values follow.
300
353
  const valueVersion = { is174: false };
@@ -308,8 +361,6 @@ export const rankString = (text: string) => {
308
361
  return text;
309
362
  };
310
363
 
311
- export const getRankedStringCount = () => stringOrder.size;
312
-
313
364
  export const getStringRank = (text: string) => stringOrder.ranks[rankString(text)];
314
365
 
315
366
  const compareStrings = (left: string, right: string) => {
@@ -317,6 +368,14 @@ const compareStrings = (left: string, right: string) => {
317
368
  const ranks = stringOrder.ranks;
318
369
  const leftRank = ranks[left];
319
370
  const rightRank = ranks[right];
371
+ const searchRank = stringOrder.searchRank;
372
+ if (
373
+ searchRank !== undefined &&
374
+ (leftRank === undefined || leftRank >= searchRank) &&
375
+ (rightRank === undefined || rightRank >= searchRank)
376
+ ) {
377
+ stringOrder.unorderedString ??= left;
378
+ }
320
379
  if (leftRank !== undefined && rightRank !== undefined) return leftRank - rightRank;
321
380
  if (leftRank !== undefined) return -1;
322
381
  if (rightRank !== undefined) return 1;
@@ -430,6 +489,8 @@ export const createModelValue = (name: string): ModelValue => {
430
489
  name,
431
490
  type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
432
491
  rank: getStringRank(name),
492
+ hash: finalizeHash(combineHash(hashString(name), 0x6d)),
493
+ fingerprint: finalizeFingerprint(fingerprintString(name) ^ 0x3c),
433
494
  };
434
495
  modelValues.set(name, value);
435
496
  return value;
@@ -480,12 +541,9 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
480
541
  functionValue: FunctionLike,
481
542
  ) => setFunctionClass(functionValue, "lambda");
482
543
 
483
- const fingerprintedValues = new WeakSet<ObjectValue>();
484
-
485
- // The lazy sets TLC has cached the elements of. TLC caches a set as the receiver of compareTo,
486
- // equals, size, and fingerPrint, not as their argument, and enumerates the sets an uncached one
487
- // combines, duplicates and all.
488
- const cachedSets = new WeakSet<ObjectValue>();
544
+ // TLC caches a lazy set as the receiver of compareTo, equals, size, and fingerPrint, not as their
545
+ // argument, and enumerates the sets an uncached one combines, duplicates and all.
546
+ const isCachedSet = (value: ObjectValue) => isLazySet(value) && value.isCached === true;
489
547
 
490
548
  // Whether the spec has built a UNION or \cup set, the sets that stream duplicates, so that a spec
491
549
  // without one skips tracking what TLC caches.
@@ -512,9 +570,10 @@ const cacheSet = (value: Value) => {
512
570
  if (
513
571
  streamedSetPresence.isPresent &&
514
572
  typeof value === "object" &&
573
+ isLazySet(value) &&
515
574
  STREAMING_KINDS.has(value.kind)
516
575
  ) {
517
- cachedSets.add(value);
576
+ value.isCached = true;
518
577
  }
519
578
  };
520
579
 
@@ -524,13 +583,11 @@ export const markFingerprinted = (value: Value): void => {
524
583
  if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
525
584
  return;
526
585
  }
527
- if (value.kind === "Function" || value.kind === "Set") {
528
- if (value.isFingerprinted) return;
529
- value.isFingerprinted = true;
530
- } else {
531
- if (fingerprintedValues.has(value)) return;
532
- fingerprintedValues.add(value);
586
+ if (value.kind === "ModelValue" || value.kind === "Interval" || value.kind === "InfiniteSet") {
587
+ return;
533
588
  }
589
+ if (value.isFingerprinted) return;
590
+ value.isFingerprinted = true;
534
591
  if (value.kind === "Function") {
535
592
  if (value.functionClass === "lambda") value.functionClass = undefined;
536
593
  value.domain.forEach(markFingerprinted);
@@ -556,12 +613,6 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
556
613
  }
557
614
  return compareValues(left, right) === 0;
558
615
  }
559
- const isHashed =
560
- (left.kind === "Set" || left.kind === "Function") &&
561
- (right.kind === "Set" || right.kind === "Function") &&
562
- left.hash !== 0 &&
563
- right.hash !== 0;
564
- if (isHashed && left.hash !== right.hash) return false;
565
616
  if (left.kind === "Function" && right.kind === "Function")
566
617
  return compareFunctions(left, right) === 0;
567
618
  if (left.kind === "Set" && right.kind === "Set") return compareSets(left, right) === 0;
@@ -1194,18 +1245,23 @@ export const createWrittenFunction = (pairs: [Value, Value][]) => {
1194
1245
  );
1195
1246
  };
1196
1247
 
1248
+ const MAX_SHARED_INTERVAL_LENGTH = 1024;
1197
1249
  const intervalDomains: Value[][] = [];
1250
+ const longIntervalDomains = new Map<number, WeakRef<Value[]>>();
1198
1251
  const intervalValues: IntervalValue[] = [];
1199
1252
  const intervalArrays = new WeakMap<Value[], IntervalValue>();
1200
1253
 
1201
1254
  // TLC keeps the domain of a tuple, or of a function built over an interval, as an interval. Every
1202
1255
  // function over 1..n shares one domain array per length, and the elements of any other interval
1203
1256
  // map to it, so the array's identity records it. Callers must not mutate the returned array.
1257
+ // Longer domains are held weakly: a sequence that grows one element at a time would otherwise keep
1258
+ // an array of every length it passed through, n * n / 2 numbers in all.
1204
1259
  const getIntervalDomain = (length: number): Value[] => {
1205
- const cached = intervalDomains[length];
1260
+ const cached = intervalDomains[length] ?? longIntervalDomains.get(length)?.deref();
1206
1261
  if (cached) return cached;
1207
1262
  const domain = Array.from({ length }, (_, index) => index + 1);
1208
- intervalDomains[length] = domain;
1263
+ if (length > MAX_SHARED_INTERVAL_LENGTH) longIntervalDomains.set(length, new WeakRef(domain));
1264
+ else intervalDomains[length] = domain;
1209
1265
  return domain;
1210
1266
  };
1211
1267
 
@@ -1220,7 +1276,10 @@ export const getIntervalElements = (value: IntervalValue): Value[] => {
1220
1276
  };
1221
1277
 
1222
1278
  const isIntervalDomain = (domain: Value[]) =>
1223
- domain.length > 0 && (domain === intervalDomains[domain.length] || intervalArrays.has(domain));
1279
+ domain.length > 0 &&
1280
+ (domain === intervalDomains[domain.length] ||
1281
+ domain === longIntervalDomains.get(domain.length)?.deref() ||
1282
+ intervalArrays.has(domain));
1224
1283
 
1225
1284
  // TLC keeps an interval domain only for a function built over an interval itself, so a set
1226
1285
  // such as (1..2) \cap S, whose elements are the shared array, gets a copy.
@@ -1546,8 +1605,14 @@ const enumerateSet = (
1546
1605
  `Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
1547
1606
  );
1548
1607
  }
1549
- if (value.operator === "\\cup")
1550
- return getSortedUnion(toFiniteSet(value.left), toFiniteSet(value.right)).elements;
1608
+ if (value.operator === "\\cup") {
1609
+ return computeLikeTlcOnFailure(
1610
+ (union) => getSortedUnion(toFiniteSet(union.left), toFiniteSet(union.right)).elements,
1611
+ (union) => sortLikeTlc(listUnionLikeTlc(union)),
1612
+ value,
1613
+ value,
1614
+ );
1615
+ }
1551
1616
  const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
1552
1617
  const enumerated = isLeftEnumerated ? value.left : value.right;
1553
1618
  const other = isLeftEnumerated ? value.right : value.left;
@@ -1565,15 +1630,13 @@ const enumerateSet = (
1565
1630
  }
1566
1631
  };
1567
1632
 
1568
- const finiteSets = new WeakMap<ObjectValue, SetValue>();
1569
-
1570
1633
  export const toFiniteSet = (value: Value): SetValue => {
1571
1634
  if (typeof value === "object" && value.kind === "Set") return value;
1572
1635
  if (typeof value === "object" && value.kind === "Interval") {
1573
1636
  return createSortedSet(enumerateSet(value));
1574
1637
  }
1575
- const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1576
- if (cached) return cached;
1638
+ const lazy = typeof value === "object" && isLazySet(value) ? value : undefined;
1639
+ if (lazy?.enumerated) return lazy.enumerated;
1577
1640
  const sets = setTrace.sets;
1578
1641
  sets?.push(value);
1579
1642
  const elements = enumerateSet(value);
@@ -1588,14 +1651,32 @@ export const toFiniteSet = (value: Value): SetValue => {
1588
1651
  value.kind === "FunctionSet" ||
1589
1652
  value.kind === "Subset");
1590
1653
  const set = isSorted ? createSortedSet(elements) : createSet(elements);
1591
- if (typeof value === "object") finiteSets.set(value, set);
1654
+ if (lazy) lazy.enumerated = set;
1592
1655
  return set;
1593
1656
  };
1594
1657
 
1658
+ // SetCupValue#toSetEnum.
1659
+ const listUnionLikeTlc = (union: SetOperationValue) => [
1660
+ ...toFiniteSet(union.left).elements,
1661
+ ...toFiniteSet(union.right).elements,
1662
+ ];
1663
+
1664
+ // EnumerableValue#elements(NORMALIZED) sorts a union outside its methods, so a failed comparison
1665
+ // isn't a fingerprint failure.
1666
+ export const getNormalizedSetElements = (value: Value) =>
1667
+ typeof value === "object" && value.kind === "SetOperation" && value.operator === "\\cup"
1668
+ ? computeLikeTlcOnFailure(
1669
+ (union) => toFiniteSet(union).elements,
1670
+ (union) => sortLikeTlc(callTracedMethod(union, () => listUnionLikeTlc(union))),
1671
+ value,
1672
+ value,
1673
+ )
1674
+ : toFiniteSet(value).elements;
1675
+
1595
1676
  // TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
1596
1677
  // so an element that two of them share comes up once for each.
1597
1678
  export const getStreamedElements = (value: Value): Value[] => {
1598
- if (typeof value !== "object" || cachedSets.has(value)) return toFiniteSet(value).elements;
1679
+ if (typeof value !== "object" || isCachedSet(value)) return toFiniteSet(value).elements;
1599
1680
  const sets = setTrace.sets;
1600
1681
  sets?.push(value);
1601
1682
  const elements = streamElements(value);
@@ -1647,7 +1728,7 @@ export const isSettledValue = (value: Value): boolean => {
1647
1728
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1648
1729
  // tuples, records, or functions it is a component of has to stream too.
1649
1730
  const isStreamedApart = (value: Value): boolean => {
1650
- if (typeof value !== "object" || cachedSets.has(value)) return false;
1731
+ if (typeof value !== "object" || isCachedSet(value)) return false;
1651
1732
  if (value.kind === "Union") return true;
1652
1733
  if (value.kind === "FilteredSet") return isStreamedApart(value.base);
1653
1734
  if (value.kind === "SetOperation") {
@@ -2102,15 +2183,17 @@ export const isUpdatedElementOf = (element: Value, member: Value, set: Value): b
2102
2183
  export const toFiniteFunction = (value: Value): FunctionValue => {
2103
2184
  if (typeof value === "object" && value.kind === "Function") return value;
2104
2185
  if (typeof value === "object" && value.kind === "LazyFunction") {
2186
+ if (value.enumerated) return value.enumerated;
2105
2187
  if (!isEnumerable(value.domain)) getCardinality(value.domain);
2106
2188
  const domain = getFunctionDomain(value.domain);
2107
- return setFunctionClass(
2189
+ value.enumerated = setFunctionClass(
2108
2190
  createSortedFunction(
2109
2191
  domain,
2110
2192
  domain.map((key) => value.apply(key)),
2111
2193
  ),
2112
2194
  getFunctionClass(value),
2113
2195
  );
2196
+ return value.enumerated;
2114
2197
  }
2115
2198
  throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
2116
2199
  };
@@ -2315,8 +2398,6 @@ export const updateFunction = (
2315
2398
  return updated;
2316
2399
  };
2317
2400
 
2318
- const valueHashes = new WeakMap<ObjectValue, number>();
2319
-
2320
2401
  const HASH_SEED = 0x811c9dc5;
2321
2402
 
2322
2403
  const finalizeHash = (hash: number) => {
@@ -2348,11 +2429,9 @@ const getEntryContribution = (keyHash: number, valueHash: number) =>
2348
2429
  // when TLC fingerprints it.
2349
2430
  const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
2350
2431
 
2351
- const computeValueHash = (value: ObjectValue) => {
2352
- if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
2353
- if (isSetValue(value)) {
2354
- if (!isEnumerable(value)) return UNFINGERPRINTABLE_HASH;
2355
- const elements = toFiniteSet(value).elements;
2432
+ const computeValueHash = (value: SetValue | FunctionValue) => {
2433
+ if (value.kind === "Set") {
2434
+ const elements = value.elements;
2356
2435
  let hash = SET_HASH_SEED;
2357
2436
  for (let index = 0; index < elements.length; index++) {
2358
2437
  const elementHash = getValueHash(elements[index]);
@@ -2361,12 +2440,10 @@ const computeValueHash = (value: ObjectValue) => {
2361
2440
  }
2362
2441
  return hash;
2363
2442
  }
2364
- if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
2365
- const functionValue = toFiniteFunction(value);
2366
2443
  let hash = FUNCTION_HASH_SEED;
2367
- for (let index = 0; index < functionValue.domain.length; index++) {
2368
- const keyHash = getValueHash(functionValue.domain[index]);
2369
- const itemHash = getValueHash(functionValue.range[index]);
2444
+ for (let index = 0; index < value.domain.length; index++) {
2445
+ const keyHash = getValueHash(value.domain[index]);
2446
+ const itemHash = getValueHash(value.range[index]);
2370
2447
  if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
2371
2448
  return UNFINGERPRINTABLE_HASH;
2372
2449
  }
@@ -2442,15 +2519,15 @@ export const getValueHash = (value: Value): number => {
2442
2519
  if (value.hash === 0) value.hash = computeValueHash(value);
2443
2520
  return value.hash;
2444
2521
  }
2445
- const cached = valueHashes.get(value);
2446
- if (cached !== undefined) return cached;
2447
- const hash = computeValueHash(value);
2448
- valueHashes.set(value, hash);
2449
- return hash;
2522
+ if (value.kind === "ModelValue") return value.hash;
2523
+ if (value.kind === "LazyFunction") {
2524
+ return isEnumerable(value.domain)
2525
+ ? getValueHash(toFiniteFunction(value))
2526
+ : UNFINGERPRINTABLE_HASH;
2527
+ }
2528
+ return isEnumerable(value) ? getValueHash(toFiniteSet(value)) : UNFINGERPRINTABLE_HASH;
2450
2529
  };
2451
2530
 
2452
- const valueFingerprints = new WeakMap<ObjectValue, number>();
2453
-
2454
2531
  const finalizeFingerprint = (hash: number) => {
2455
2532
  const first = Math.imul(hash ^ (hash >>> 15), 0x2c1b3c6d);
2456
2533
  const second = Math.imul(first ^ (first >>> 12), 0x297a2d39);
@@ -2472,23 +2549,18 @@ const getEntryFingerprint = (key: Value, value: Value) =>
2472
2549
  (Math.imul(getValueFingerprint(key), 0x165667b1) + getValueFingerprint(value)) | 0,
2473
2550
  );
2474
2551
 
2475
- const computeValueFingerprint = (value: ObjectValue) => {
2476
- if (value.kind === "ModelValue") return finalizeFingerprint(fingerprintString(value.name) ^ 0x3c);
2477
- if (isSetValue(value)) {
2478
- if (!isEnumerable(value)) return fingerprintString(getValueKey(value));
2479
- const elements = toFiniteSet(value).elements;
2552
+ const computeValueFingerprint = (value: SetValue | FunctionValue) => {
2553
+ if (value.kind === "Set") {
2554
+ const elements = value.elements;
2480
2555
  let fingerprint = 0x1b873593;
2481
2556
  for (let index = 0; index < elements.length; index++) {
2482
2557
  fingerprint = (fingerprint + getElementFingerprint(elements[index])) | 0;
2483
2558
  }
2484
2559
  return fingerprint;
2485
2560
  }
2486
- const functionValue = toFiniteFunction(value);
2487
2561
  let fingerprint = 0x4cf5ad43;
2488
- for (let index = 0; index < functionValue.domain.length; index++) {
2489
- fingerprint =
2490
- (fingerprint + getEntryFingerprint(functionValue.domain[index], functionValue.range[index])) |
2491
- 0;
2562
+ for (let index = 0; index < value.domain.length; index++) {
2563
+ fingerprint = (fingerprint + getEntryFingerprint(value.domain[index], value.range[index])) | 0;
2492
2564
  }
2493
2565
  return fingerprint;
2494
2566
  };
@@ -2501,24 +2573,18 @@ export const getValueFingerprint = (value: Value): number => {
2501
2573
  if (value.fingerprint === 0) value.fingerprint = computeValueFingerprint(value);
2502
2574
  return value.fingerprint;
2503
2575
  }
2504
- const cached = valueFingerprints.get(value);
2505
- if (cached !== undefined) return cached;
2506
- const fingerprint = computeValueFingerprint(value);
2507
- valueFingerprints.set(value, fingerprint);
2508
- return fingerprint;
2576
+ if (value.kind === "ModelValue") return value.fingerprint;
2577
+ if (value.kind === "LazyFunction") return getValueFingerprint(toFiniteFunction(value));
2578
+ return isEnumerable(value)
2579
+ ? getValueFingerprint(toFiniteSet(value))
2580
+ : fingerprintString(getValueKey(value));
2509
2581
  };
2510
2582
 
2511
- const valueKeys = new WeakMap<ObjectValue, string>();
2512
-
2513
2583
  export const getValueKey = (value: Value): string => {
2514
2584
  if (typeof value === "boolean") return value ? "T" : "F";
2515
2585
  if (typeof value === "number") return String(value);
2516
2586
  if (typeof value === "string") return JSON.stringify(value);
2517
- const cached = valueKeys.get(value);
2518
- if (cached !== undefined) return cached;
2519
- const key = computeValueKey(value);
2520
- valueKeys.set(value, key);
2521
- return key;
2587
+ return computeValueKey(value);
2522
2588
  };
2523
2589
 
2524
2590
  const computeValueKey = (value: ObjectValue) => {