tla-ts 0.0.3-dev.30014f9 → 0.0.3-dev.31e0776

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/values.ts CHANGED
@@ -1069,13 +1069,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
1069
1069
  const formatUnion = (value: UnionValue, isStrict = false) =>
1070
1070
  `UNION(${formatValue(value.base, isStrict)})`;
1071
1071
 
1072
- const getUnionSets = (value: UnionValue) => {
1072
+ const getUnionSets = (
1073
+ value: UnionValue,
1074
+ getElements = (set: Value) => toFiniteSet(set).elements,
1075
+ ) => {
1073
1076
  if (!isTlcEnumerable(value.base)) {
1074
1077
  throw new TlaEvaluationError(
1075
1078
  `Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
1076
1079
  );
1077
1080
  }
1078
- return toFiniteSet(value.base).elements;
1081
+ return getElements(value.base);
1079
1082
  };
1080
1083
 
1081
1084
  const getUnionSet = (set: Value, index: number, union: UnionValue) => {
@@ -1111,7 +1114,7 @@ export const isEnumerable = (value: Value): boolean => {
1111
1114
  case "ProductSet":
1112
1115
  return value.factors.every(isEnumerable) || getEmptiness(value) === true;
1113
1116
  case "Union":
1114
- return isEnumerable(value.base) && toFiniteSet(value.base).elements.every(isEnumerable);
1117
+ return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
1115
1118
  case "SetOperation":
1116
1119
  if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
1117
1120
  if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
@@ -1345,8 +1348,10 @@ export const toFiniteSet = (value: Value): SetValue => {
1345
1348
  // so an element that two of them share comes up once for each.
1346
1349
  export const getStreamedElements = (value: Value): Value[] => {
1347
1350
  if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
1351
+ // TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
1352
+ // its elements twice.
1348
1353
  if (value.kind === "Union") {
1349
- return getUnionSets(value).flatMap((set, index) =>
1354
+ return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
1350
1355
  getStreamedElements(getUnionSet(set, index, value)),
1351
1356
  );
1352
1357
  }
@@ -2116,3 +2121,14 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
2116
2121
  }
2117
2122
  }
2118
2123
  };
2124
+
2125
+ // TLC's TLCState.toString: a line per variable, as a conjunction when there are several.
2126
+ export const formatState = (state: Record<string, Value>) => {
2127
+ const entries = Object.entries(state);
2128
+ return entries
2129
+ .map(
2130
+ ([name, value]) =>
2131
+ `${entries.length === 1 ? "" : "/\\ "}${name} = ${prettyPrint(formatValue(value))}\n`,
2132
+ )
2133
+ .join("");
2134
+ };