tla-ts 0.0.8 → 0.0.9

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.
package/src/tlc/order.ts CHANGED
@@ -38,7 +38,6 @@ interface ParsedModule {
38
38
  tokens: Token[];
39
39
  start: number;
40
40
  end: number;
41
- isTlcSource: boolean;
42
41
  getModule: () => Module;
43
42
  }
44
43
 
@@ -126,7 +125,8 @@ const getDeclaredName = (tokens: Token[], start: number) => {
126
125
  return { name: first.text === "-" ? "-." : getCanonicalName(first.text), end: start + 2 };
127
126
  }
128
127
  if (second?.text !== "(") return { name: first.text, end: start + 1 };
129
- const close = tokens.findIndex((token, index) => index > start && token.text === ")");
128
+ let close = start + 2;
129
+ while (close < tokens.length && tokens[close].text !== ")") close++;
130
130
  return { name: first.text, end: close + 1 };
131
131
  };
132
132
 
@@ -268,6 +268,8 @@ interface ModuleText {
268
268
  getModule: () => Module;
269
269
  }
270
270
 
271
+ // A module's source is read into tokens once, and parsed from them once, for every order and for
272
+ // the environment that evaluates it.
271
273
  const readModuleText = (text: string | undefined): ModuleText => {
272
274
  const tokens = text === undefined ? [] : tokenize(text);
273
275
  const parsed = { module: undefined as Module | undefined };
@@ -279,23 +281,21 @@ const readModuleText = (text: string | undefined): ModuleText => {
279
281
  };
280
282
  };
281
283
 
282
- // TLC's own module sources never change, so every check shares their tokens and parse.
283
- const tlcModuleTexts = new Map<string, ModuleText>();
284
-
285
- const getTlcModuleText = (name: string, text: string): ModuleText => {
286
- const known = tlcModuleTexts.get(name);
287
- if (known) return known;
288
- const read = readModuleText(text);
289
- tlcModuleTexts.set(name, read);
290
- return read;
291
- };
292
-
293
284
  // The modules TLC reads for a spec: the root module, TLC's standard modules, the files the loader
294
285
  // returns, the modules that follow the root module, and the modules nested in any of these. A
295
286
  // module without a source has no tokens.
296
287
  const createModuleSet = (source: string, loadModule: ModuleLoader) => {
297
288
  const files = new Map<string, ParsedModule>();
298
289
  const nestedModules = new Map<string, ParsedModule>();
290
+ // The read of each module text, so that the environment parses a text the order read only once.
291
+ const texts = new Map<string, ModuleText>();
292
+ const readText = (text: string | undefined) => {
293
+ const known = text === undefined ? undefined : texts.get(text);
294
+ if (known) return known;
295
+ const read = readModuleText(text);
296
+ if (text !== undefined) texts.set(text, read);
297
+ return read;
298
+ };
299
299
  const getFile = (name: string | undefined) => {
300
300
  const cached = files.get(name ?? "");
301
301
  if (cached) return cached;
@@ -306,52 +306,32 @@ const createModuleSet = (source: string, loadModule: ModuleLoader) => {
306
306
  TLC_MODULE_SOURCES.get(name) ??
307
307
  loadModule?.(name) ??
308
308
  extractModule(source, name));
309
- const isTlcSource =
310
- name !== undefined && text !== undefined && text === TLC_MODULE_SOURCES.get(name);
311
- const { tokens, ranges, getModule } = isTlcSource
312
- ? getTlcModuleText(name, text)
313
- : readModuleText(text);
309
+ const { tokens, ranges, getModule } = readText(text);
314
310
  const range = ranges.get(getModuleName(tokens) ?? "") ?? { start: 0, end: 0 };
315
- const file = { tokens, ...range, isTlcSource, getModule };
311
+ const file = { tokens, ...range, getModule };
316
312
  files.set(name ?? "", file);
317
313
  ranges.forEach((nestedRange, nestedName) => {
318
314
  if (nestedRange.start === range.start || nestedModules.has(nestedName)) return;
319
315
  nestedModules.set(nestedName, {
320
316
  tokens,
321
317
  ...nestedRange,
322
- isTlcSource: false,
323
318
  getModule: () => findModule(file.getModule().modules, nestedName) ?? file.getModule(),
324
319
  });
325
320
  });
326
321
  return file;
327
322
  };
328
323
  getFile(undefined);
329
- return (name: string | undefined): ParsedModule =>
330
- (name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name);
324
+ return {
325
+ getParsed: (name: string | undefined): ParsedModule =>
326
+ (name === undefined ? undefined : nestedModules.get(name)) ?? getFile(name),
327
+ parseText: (text: string) => readText(text).getModule(),
328
+ };
331
329
  };
332
330
 
333
331
  // SANY's Context of each module, which the root module has under undefined.
334
- type ModuleSet = ReturnType<typeof createModuleSet>;
335
-
336
- // The Context of one of TLC's own modules is the same in every check when every module it extends
337
- // or instantiates is also TLC's own.
338
- const tlcContexts = new Map<string, SymbolTable>();
332
+ type ModuleSet = ReturnType<typeof createModuleSet>["getParsed"];
339
333
 
340
334
  const createContexts = (getParsed: ModuleSet) => {
341
- const sharedModules = new Map<string, boolean>();
342
- const isShared = (name: string): boolean => {
343
- const known = sharedModules.get(name);
344
- if (known !== undefined) return known;
345
- sharedModules.set(name, false);
346
- const unit = getParsed(name);
347
- const module = unit.isTlcSource ? unit.getModule() : undefined;
348
- const shared =
349
- module !== undefined &&
350
- module.extends.every((extended) => isShared(extended)) &&
351
- module.instances.every((instance) => isShared(instance.module));
352
- sharedModules.set(name, shared);
353
- return shared;
354
- };
355
335
  const getUnits = (unit: ParsedModule) => {
356
336
  const tokens = unit.tokens;
357
337
  const module = unit.getModule();
@@ -452,8 +432,7 @@ const createContexts = (getParsed: ModuleSet) => {
452
432
 
453
433
  const contexts = new Map<string, SymbolTable>();
454
434
  const getContext = (name: string | undefined): SymbolTable => {
455
- const isTlcContext = name !== undefined && isShared(name);
456
- const cached = contexts.get(name ?? "") ?? (isTlcContext ? tlcContexts.get(name) : undefined);
435
+ const cached = contexts.get(name ?? "");
457
436
  if (cached) return cached;
458
437
  const table = createSymbolTable();
459
438
  contexts.set(name ?? "", table);
@@ -468,7 +447,6 @@ const createContexts = (getParsed: ModuleSet) => {
468
447
  );
469
448
  table.ownStart = table.inserted.length;
470
449
  getUnits(unit).forEach((item) => item.apply(table));
471
- if (isTlcContext) tlcContexts.set(name, table);
472
450
  return table;
473
451
  };
474
452
  return {
@@ -586,8 +564,6 @@ const getStringOrder = (getParsed: ModuleSet, config: ModelConfig): string[] =>
586
564
  };
587
565
 
588
566
  export interface TlcOrder {
589
- // The root module, parsed once for the order and the environment.
590
- getRootModule: () => Module;
591
567
  // Every string TLC interns before model checking starts.
592
568
  getStringOrder: (config: ModelConfig) => string[];
593
569
  // The variables or constants of the root module in the reverse of its context enumeration, as
@@ -599,16 +575,18 @@ export interface TlcOrder {
599
575
  // adds the definitions of the module it instantiates in the order that module's Hashtable
600
576
  // enumerates them.
601
577
  createDefinitionOrder: () => (name: string | undefined) => string[];
578
+ // Parses a module's source, once for the order and the environment.
579
+ parseModule: (source: string) => Module;
602
580
  }
603
581
 
604
582
  // One check reads the spec's modules once, and every order shares their tokens, parses and
605
583
  // contexts.
606
584
  export const createTlcOrder = (source: string, loadModule: ModuleLoader): TlcOrder => {
607
- const getParsed = createModuleSet(source, loadModule);
585
+ const modules = createModuleSet(source, loadModule);
586
+ const getParsed = modules.getParsed;
608
587
  let contexts: ReturnType<typeof createContexts> | undefined;
609
588
  const getContexts = () => (contexts ??= createContexts(getParsed));
610
589
  return {
611
- getRootModule: () => getParsed(undefined).getModule(),
612
590
  getStringOrder: (config) => getStringOrder(getParsed, config),
613
591
  getDeclarationOrder: (kind) =>
614
592
  getContexts()
@@ -617,6 +595,7 @@ export const createTlcOrder = (source: string, loadModule: ModuleLoader): TlcOrd
617
595
  .filter((entry) => entry.kind === kind)
618
596
  .map((entry) => entry.name)
619
597
  .reverse(),
598
+ parseModule: modules.parseText,
620
599
  createDefinitionOrder: () => {
621
600
  const { getContext, getOwnEntries } = getContexts();
622
601
  // The root module's context reads every file of the spec, and so finds the modules nested
package/src/tlc/replay.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { Expression, SourceLocation } from "../ast";
2
2
  import { TlaEvaluationError } from "../errors";
3
+ import { isConstantLevel } from "../dependencies";
3
4
  import {
4
5
  bindDefinitions,
5
6
  bindLazyArguments,
@@ -9,7 +10,6 @@ import {
9
10
  freezeCallStack,
10
11
  getLazyFunction,
11
12
  toKeepLazyContext,
12
- isConstantLevel,
13
13
  lookupScope,
14
14
  resolveApplyOperator,
15
15
  resolveGlobalOperator,
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;
@@ -310,6 +344,9 @@ export const rankString = (text: string) => {
310
344
 
311
345
  export const getRankedStringCount = () => stringOrder.size;
312
346
 
347
+ export const getRankedString = (rank: number) =>
348
+ Object.keys(stringOrder.ranks).find((text) => stringOrder.ranks[text] === rank);
349
+
313
350
  export const getStringRank = (text: string) => stringOrder.ranks[rankString(text)];
314
351
 
315
352
  const compareStrings = (left: string, right: string) => {
@@ -430,6 +467,8 @@ export const createModelValue = (name: string): ModelValue => {
430
467
  name,
431
468
  type: name.length > 2 && name[1] === "_" ? name[0] : undefined,
432
469
  rank: getStringRank(name),
470
+ hash: finalizeHash(combineHash(hashString(name), 0x6d)),
471
+ fingerprint: finalizeFingerprint(fingerprintString(name) ^ 0x3c),
433
472
  };
434
473
  modelValues.set(name, value);
435
474
  return value;
@@ -480,12 +519,9 @@ export const markLambdaFunction = <FunctionLike extends FunctionValue | LazyFunc
480
519
  functionValue: FunctionLike,
481
520
  ) => setFunctionClass(functionValue, "lambda");
482
521
 
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>();
522
+ // TLC caches a lazy set as the receiver of compareTo, equals, size, and fingerPrint, not as their
523
+ // argument, and enumerates the sets an uncached one combines, duplicates and all.
524
+ const isCachedSet = (value: ObjectValue) => isLazySet(value) && value.isCached === true;
489
525
 
490
526
  // Whether the spec has built a UNION or \cup set, the sets that stream duplicates, so that a spec
491
527
  // without one skips tracking what TLC caches.
@@ -512,9 +548,10 @@ const cacheSet = (value: Value) => {
512
548
  if (
513
549
  streamedSetPresence.isPresent &&
514
550
  typeof value === "object" &&
551
+ isLazySet(value) &&
515
552
  STREAMING_KINDS.has(value.kind)
516
553
  ) {
517
- cachedSets.add(value);
554
+ value.isCached = true;
518
555
  }
519
556
  };
520
557
 
@@ -524,13 +561,11 @@ export const markFingerprinted = (value: Value): void => {
524
561
  if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
525
562
  return;
526
563
  }
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);
564
+ if (value.kind === "ModelValue" || value.kind === "Interval" || value.kind === "InfiniteSet") {
565
+ return;
533
566
  }
567
+ if (value.isFingerprinted) return;
568
+ value.isFingerprinted = true;
534
569
  if (value.kind === "Function") {
535
570
  if (value.functionClass === "lambda") value.functionClass = undefined;
536
571
  value.domain.forEach(markFingerprinted);
@@ -1546,8 +1581,14 @@ const enumerateSet = (
1546
1581
  `Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
1547
1582
  );
1548
1583
  }
1549
- if (value.operator === "\\cup")
1550
- return getSortedUnion(toFiniteSet(value.left), toFiniteSet(value.right)).elements;
1584
+ if (value.operator === "\\cup") {
1585
+ return computeLikeTlcOnFailure(
1586
+ (union) => getSortedUnion(toFiniteSet(union.left), toFiniteSet(union.right)).elements,
1587
+ (union) => sortLikeTlc(listUnionLikeTlc(union)),
1588
+ value,
1589
+ value,
1590
+ );
1591
+ }
1551
1592
  const isLeftEnumerated = value.operator === "\\" || isTlcEnumerable(value.left);
1552
1593
  const enumerated = isLeftEnumerated ? value.left : value.right;
1553
1594
  const other = isLeftEnumerated ? value.right : value.left;
@@ -1565,15 +1606,13 @@ const enumerateSet = (
1565
1606
  }
1566
1607
  };
1567
1608
 
1568
- const finiteSets = new WeakMap<ObjectValue, SetValue>();
1569
-
1570
1609
  export const toFiniteSet = (value: Value): SetValue => {
1571
1610
  if (typeof value === "object" && value.kind === "Set") return value;
1572
1611
  if (typeof value === "object" && value.kind === "Interval") {
1573
1612
  return createSortedSet(enumerateSet(value));
1574
1613
  }
1575
- const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1576
- if (cached) return cached;
1614
+ const lazy = typeof value === "object" && isLazySet(value) ? value : undefined;
1615
+ if (lazy?.enumerated) return lazy.enumerated;
1577
1616
  const sets = setTrace.sets;
1578
1617
  sets?.push(value);
1579
1618
  const elements = enumerateSet(value);
@@ -1588,14 +1627,32 @@ export const toFiniteSet = (value: Value): SetValue => {
1588
1627
  value.kind === "FunctionSet" ||
1589
1628
  value.kind === "Subset");
1590
1629
  const set = isSorted ? createSortedSet(elements) : createSet(elements);
1591
- if (typeof value === "object") finiteSets.set(value, set);
1630
+ if (lazy) lazy.enumerated = set;
1592
1631
  return set;
1593
1632
  };
1594
1633
 
1634
+ // SetCupValue#toSetEnum.
1635
+ const listUnionLikeTlc = (union: SetOperationValue) => [
1636
+ ...toFiniteSet(union.left).elements,
1637
+ ...toFiniteSet(union.right).elements,
1638
+ ];
1639
+
1640
+ // EnumerableValue#elements(NORMALIZED) sorts a union outside its methods, so a failed comparison
1641
+ // isn't a fingerprint failure.
1642
+ export const getNormalizedSetElements = (value: Value) =>
1643
+ typeof value === "object" && value.kind === "SetOperation" && value.operator === "\\cup"
1644
+ ? computeLikeTlcOnFailure(
1645
+ (union) => toFiniteSet(union).elements,
1646
+ (union) => sortLikeTlc(callTracedMethod(union, () => listUnionLikeTlc(union))),
1647
+ value,
1648
+ value,
1649
+ )
1650
+ : toFiniteSet(value).elements;
1651
+
1595
1652
  // TLC's Enumerator for a lazy set streams the sets it combines until the set caches its elements,
1596
1653
  // so an element that two of them share comes up once for each.
1597
1654
  export const getStreamedElements = (value: Value): Value[] => {
1598
- if (typeof value !== "object" || cachedSets.has(value)) return toFiniteSet(value).elements;
1655
+ if (typeof value !== "object" || isCachedSet(value)) return toFiniteSet(value).elements;
1599
1656
  const sets = setTrace.sets;
1600
1657
  sets?.push(value);
1601
1658
  const elements = streamElements(value);
@@ -1647,7 +1704,7 @@ export const isSettledValue = (value: Value): boolean => {
1647
1704
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1648
1705
  // tuples, records, or functions it is a component of has to stream too.
1649
1706
  const isStreamedApart = (value: Value): boolean => {
1650
- if (typeof value !== "object" || cachedSets.has(value)) return false;
1707
+ if (typeof value !== "object" || isCachedSet(value)) return false;
1651
1708
  if (value.kind === "Union") return true;
1652
1709
  if (value.kind === "FilteredSet") return isStreamedApart(value.base);
1653
1710
  if (value.kind === "SetOperation") {
@@ -2102,15 +2159,17 @@ export const isUpdatedElementOf = (element: Value, member: Value, set: Value): b
2102
2159
  export const toFiniteFunction = (value: Value): FunctionValue => {
2103
2160
  if (typeof value === "object" && value.kind === "Function") return value;
2104
2161
  if (typeof value === "object" && value.kind === "LazyFunction") {
2162
+ if (value.enumerated) return value.enumerated;
2105
2163
  if (!isEnumerable(value.domain)) getCardinality(value.domain);
2106
2164
  const domain = getFunctionDomain(value.domain);
2107
- return setFunctionClass(
2165
+ value.enumerated = setFunctionClass(
2108
2166
  createSortedFunction(
2109
2167
  domain,
2110
2168
  domain.map((key) => value.apply(key)),
2111
2169
  ),
2112
2170
  getFunctionClass(value),
2113
2171
  );
2172
+ return value.enumerated;
2114
2173
  }
2115
2174
  throw new TlaEvaluationError(`Expected a function but found ${formatValue(value)}`);
2116
2175
  };
@@ -2315,8 +2374,6 @@ export const updateFunction = (
2315
2374
  return updated;
2316
2375
  };
2317
2376
 
2318
- const valueHashes = new WeakMap<ObjectValue, number>();
2319
-
2320
2377
  const HASH_SEED = 0x811c9dc5;
2321
2378
 
2322
2379
  const finalizeHash = (hash: number) => {
@@ -2348,11 +2405,9 @@ const getEntryContribution = (keyHash: number, valueHash: number) =>
2348
2405
  // when TLC fingerprints it.
2349
2406
  const UNFINGERPRINTABLE_HASH = 0x2f6b1c4d;
2350
2407
 
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;
2408
+ const computeValueHash = (value: SetValue | FunctionValue) => {
2409
+ if (value.kind === "Set") {
2410
+ const elements = value.elements;
2356
2411
  let hash = SET_HASH_SEED;
2357
2412
  for (let index = 0; index < elements.length; index++) {
2358
2413
  const elementHash = getValueHash(elements[index]);
@@ -2361,12 +2416,10 @@ const computeValueHash = (value: ObjectValue) => {
2361
2416
  }
2362
2417
  return hash;
2363
2418
  }
2364
- if (value.kind === "LazyFunction" && !isEnumerable(value.domain)) return UNFINGERPRINTABLE_HASH;
2365
- const functionValue = toFiniteFunction(value);
2366
2419
  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]);
2420
+ for (let index = 0; index < value.domain.length; index++) {
2421
+ const keyHash = getValueHash(value.domain[index]);
2422
+ const itemHash = getValueHash(value.range[index]);
2370
2423
  if (keyHash === UNFINGERPRINTABLE_HASH || itemHash === UNFINGERPRINTABLE_HASH) {
2371
2424
  return UNFINGERPRINTABLE_HASH;
2372
2425
  }
@@ -2442,15 +2495,15 @@ export const getValueHash = (value: Value): number => {
2442
2495
  if (value.hash === 0) value.hash = computeValueHash(value);
2443
2496
  return value.hash;
2444
2497
  }
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;
2498
+ if (value.kind === "ModelValue") return value.hash;
2499
+ if (value.kind === "LazyFunction") {
2500
+ return isEnumerable(value.domain)
2501
+ ? getValueHash(toFiniteFunction(value))
2502
+ : UNFINGERPRINTABLE_HASH;
2503
+ }
2504
+ return isEnumerable(value) ? getValueHash(toFiniteSet(value)) : UNFINGERPRINTABLE_HASH;
2450
2505
  };
2451
2506
 
2452
- const valueFingerprints = new WeakMap<ObjectValue, number>();
2453
-
2454
2507
  const finalizeFingerprint = (hash: number) => {
2455
2508
  const first = Math.imul(hash ^ (hash >>> 15), 0x2c1b3c6d);
2456
2509
  const second = Math.imul(first ^ (first >>> 12), 0x297a2d39);
@@ -2472,23 +2525,18 @@ const getEntryFingerprint = (key: Value, value: Value) =>
2472
2525
  (Math.imul(getValueFingerprint(key), 0x165667b1) + getValueFingerprint(value)) | 0,
2473
2526
  );
2474
2527
 
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;
2528
+ const computeValueFingerprint = (value: SetValue | FunctionValue) => {
2529
+ if (value.kind === "Set") {
2530
+ const elements = value.elements;
2480
2531
  let fingerprint = 0x1b873593;
2481
2532
  for (let index = 0; index < elements.length; index++) {
2482
2533
  fingerprint = (fingerprint + getElementFingerprint(elements[index])) | 0;
2483
2534
  }
2484
2535
  return fingerprint;
2485
2536
  }
2486
- const functionValue = toFiniteFunction(value);
2487
2537
  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;
2538
+ for (let index = 0; index < value.domain.length; index++) {
2539
+ fingerprint = (fingerprint + getEntryFingerprint(value.domain[index], value.range[index])) | 0;
2492
2540
  }
2493
2541
  return fingerprint;
2494
2542
  };
@@ -2501,24 +2549,18 @@ export const getValueFingerprint = (value: Value): number => {
2501
2549
  if (value.fingerprint === 0) value.fingerprint = computeValueFingerprint(value);
2502
2550
  return value.fingerprint;
2503
2551
  }
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;
2552
+ if (value.kind === "ModelValue") return value.fingerprint;
2553
+ if (value.kind === "LazyFunction") return getValueFingerprint(toFiniteFunction(value));
2554
+ return isEnumerable(value)
2555
+ ? getValueFingerprint(toFiniteSet(value))
2556
+ : fingerprintString(getValueKey(value));
2509
2557
  };
2510
2558
 
2511
- const valueKeys = new WeakMap<ObjectValue, string>();
2512
-
2513
2559
  export const getValueKey = (value: Value): string => {
2514
2560
  if (typeof value === "boolean") return value ? "T" : "F";
2515
2561
  if (typeof value === "number") return String(value);
2516
2562
  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;
2563
+ return computeValueKey(value);
2522
2564
  };
2523
2565
 
2524
2566
  const computeValueKey = (value: ObjectValue) => {