tla-ts 0.0.3-dev.91b9cf4 → 0.0.3-dev.949cce6

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.
@@ -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/values.ts CHANGED
@@ -378,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
378
378
  `Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
379
379
  );
380
380
  }
381
- if (isLeftSet) return compareSets(toFiniteSet(left), toFiniteSet(right));
381
+ if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
382
382
  return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
383
383
  }
384
384
  if (typeof left === "number" && typeof right === "number") return left - right;
@@ -871,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
871
871
  }
872
872
  };
873
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.
874
876
  const isStrictlySorted = (elements: Value[]) => {
875
877
  for (let index = 1; index < elements.length; index++) {
876
- if (compareValues(elements[index - 1], elements[index]) >= 0) return false;
878
+ if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
877
879
  }
878
880
  return true;
879
881
  };
@@ -1328,6 +1330,7 @@ const enumerateSet = (value: Value): Value[] => {
1328
1330
  };
1329
1331
 
1330
1332
  const finiteSets = new WeakMap<ObjectValue, SetValue>();
1333
+ const comparedSets = new WeakMap<ObjectValue, SetValue>();
1331
1334
 
1332
1335
  export const toFiniteSet = (value: Value): SetValue => {
1333
1336
  if (typeof value === "object" && value.kind === "Set") return value;
@@ -1336,6 +1339,13 @@ export const toFiniteSet = (value: Value): SetValue => {
1336
1339
  }
1337
1340
  const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
1338
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) => {
1339
1349
  const elements = enumerateSet(value);
1340
1350
  const isSorted =
1341
1351
  typeof value === "object" &&
@@ -1346,8 +1356,20 @@ export const toFiniteSet = (value: Value): SetValue => {
1346
1356
  value.kind === "ProductSet" ||
1347
1357
  value.kind === "FunctionSet" ||
1348
1358
  value.kind === "Subset");
1349
- const set = isSorted ? createSortedSet(elements) : createSet(elements);
1350
- 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);
1351
1373
  return set;
1352
1374
  };
1353
1375