tla-ts 0.0.4-dev.7c70fed → 0.0.4-dev.81d0769

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.4-dev.7c70fed",
3
+ "version": "0.0.4-dev.81d0769",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/check.ts CHANGED
@@ -641,6 +641,7 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
641
641
  state: undefined,
642
642
  next: undefined,
643
643
  cache: undefined,
644
+ isStreamed: false,
644
645
  },
645
646
  parent,
646
647
  },
package/src/evaluate.ts CHANGED
@@ -54,6 +54,7 @@ import {
54
54
  markLambdaFunction,
55
55
  isReducibleSet,
56
56
  isSetValue,
57
+ isSettledValue,
57
58
  MAXIMUM_INTEGER,
58
59
  MINIMUM_INTEGER,
59
60
  toFiniteFunction,
@@ -113,6 +114,9 @@ export interface Thunk {
113
114
  state: State | undefined;
114
115
  next: State | undefined;
115
116
  cache: Value | undefined;
117
+ // Whether the expression may build a set that streams until TLC caches it, so that the thunk
118
+ // caches only a settled value.
119
+ isStreamed: boolean;
116
120
  }
117
121
 
118
122
  type Binding = { kind: "Value"; value: Value } | Thunk | { kind: "Operator"; operator: Operator };
@@ -847,7 +851,7 @@ const forceThunk = (thunk: Thunk, context: Context) => {
847
851
  const isSameState = thunk.state === context.state && thunk.next === context.next;
848
852
  if (isSameState && thunk.cache !== undefined) return thunk.cache;
849
853
  const value = evaluate(thunk.expression, withScope(context, thunk.scope));
850
- if (isSameState) thunk.cache = value;
854
+ if (isSameState && (!thunk.isStreamed || isSettledValue(value))) thunk.cache = value;
851
855
  return value;
852
856
  };
853
857
 
@@ -968,6 +972,7 @@ const createThunk = (expression: Expression, context: Context): Thunk => ({
968
972
  state: context.state,
969
973
  next: context.next,
970
974
  cache: undefined,
975
+ isStreamed: false,
971
976
  });
972
977
 
973
978
  export const findGlobalName = (name: string, environment: Environment): GlobalName | undefined => {
@@ -1064,10 +1069,16 @@ const callOperator = (operator: Operator, args: Expression[], context: Context)
1064
1069
  );
1065
1070
  };
1066
1071
 
1072
+ // The LET definitions that a reference evaluates again, and those that cache only a settled value.
1073
+ interface ReevaluatedDefinitions {
1074
+ impure: ReadonlySet<Definition>;
1075
+ streamed: ReadonlySet<Definition>;
1076
+ }
1077
+
1067
1078
  export const bindDefinitions = (
1068
1079
  definitions: Definition[],
1069
1080
  context: Context,
1070
- reevaluated?: ReadonlySet<Definition>,
1081
+ reevaluated?: ReevaluatedDefinitions,
1071
1082
  ): Scope | undefined =>
1072
1083
  definitions.reduce<Scope | undefined>((scope, definition) => {
1073
1084
  const node: Scope = {
@@ -1089,10 +1100,11 @@ export const bindDefinitions = (
1089
1100
  state: context.state,
1090
1101
  next: context.next,
1091
1102
  cache: undefined,
1103
+ isStreamed: false,
1092
1104
  };
1093
1105
  return node;
1094
1106
  }
1095
- if (definition.params.length > 0 || reevaluated?.has(definition)) {
1107
+ if (definition.params.length > 0 || reevaluated?.impure.has(definition)) {
1096
1108
  node.binding = {
1097
1109
  kind: "Operator",
1098
1110
  operator: {
@@ -1112,6 +1124,7 @@ export const bindDefinitions = (
1112
1124
  state: context.state,
1113
1125
  next: context.next,
1114
1126
  cache: undefined,
1127
+ isStreamed: reevaluated?.streamed.has(definition) ?? false,
1115
1128
  };
1116
1129
  return node;
1117
1130
  }, context.scope);
@@ -1228,14 +1241,15 @@ const STREAMED_SET: Impurity = {
1228
1241
  // TLC's next-state generator does not bind LET definitions to values, so each reference to one
1229
1242
  // evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
1230
1243
  // with UNION or \cup starts out uncached.
1231
- export const getReevaluatedDefinitions = (definitions: Definition[], environment: Environment) =>
1232
- new Set(
1233
- definitions.filter(
1234
- (definition) =>
1235
- isImpureDefinition(definition, environment) ||
1236
- isImpureDefinition(definition, environment, STREAMED_SET),
1237
- ),
1238
- );
1244
+ export const getReevaluatedDefinitions = (
1245
+ definitions: Definition[],
1246
+ environment: Environment,
1247
+ ): ReevaluatedDefinitions => ({
1248
+ impure: new Set(definitions.filter((definition) => isImpureDefinition(definition, environment))),
1249
+ streamed: new Set(
1250
+ definitions.filter((definition) => isImpureDefinition(definition, environment, STREAMED_SET)),
1251
+ ),
1252
+ });
1239
1253
 
1240
1254
  export const isImpure = (environment: Environment) =>
1241
1255
  [...environment.definitions.values()].some((definition) =>
package/src/values.ts CHANGED
@@ -234,7 +234,13 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
234
234
  const rightRange = right.range;
235
235
  if (leftRange === rightRange) return 0;
236
236
  for (let index = 0; index < leftRange.length; index++) {
237
- const valueComparison = compareValues(leftRange[index], rightRange[index]);
237
+ const leftValue = leftRange[index];
238
+ const rightValue = rightRange[index];
239
+ // V8 can't inline the recursive compareValues here, so two numbers skip the call.
240
+ const valueComparison =
241
+ typeof leftValue === "number" && typeof rightValue === "number"
242
+ ? leftValue - rightValue
243
+ : compareValues(leftValue, rightValue);
238
244
  if (valueComparison !== 0) return valueComparison;
239
245
  }
240
246
  return 0;
@@ -1595,6 +1601,38 @@ const getCombinedSets = (value: ObjectValue) =>
1595
1601
  ? [value.range]
1596
1602
  : undefined;
1597
1603
 
1604
+ // Whether a value TLC builds alike at each evaluation and that enumerating, comparing or
1605
+ // fingerprinting it leaves alike, so that one evaluation can stand for the next. It holds no UNION or
1606
+ // \cup, which streams until TLC caches it, no lambda, which fingerprinting turns into a record, and
1607
+ // no record a comparison reorders.
1608
+ export const isSettledValue = (value: Value): boolean => {
1609
+ if (typeof value !== "object") return true;
1610
+ switch (value.kind) {
1611
+ case "ModelValue":
1612
+ case "Interval":
1613
+ return true;
1614
+ case "Set":
1615
+ return value.elements.every(isSettledValue);
1616
+ case "Function":
1617
+ return (
1618
+ value.functionClass !== "lambda" &&
1619
+ value.writtenKeys === undefined &&
1620
+ value.domain.every(isSettledValue) &&
1621
+ value.range.every(isSettledValue)
1622
+ );
1623
+ case "SetOperation":
1624
+ return (
1625
+ value.operator !== "\\cup" && isSettledValue(value.left) && isSettledValue(value.right)
1626
+ );
1627
+ case "FilteredSet":
1628
+ return isSettledValue(value.base);
1629
+ case "ProductSet":
1630
+ return value.factors.every(isSettledValue);
1631
+ default:
1632
+ return false;
1633
+ }
1634
+ };
1635
+
1598
1636
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1599
1637
  // tuples, records, or functions it is a component of has to stream too.
1600
1638
  const isStreamedApart = (value: Value): boolean => {