tla-ts 0.0.6-dev.fd7689c → 0.0.7-dev.bd12468

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.6-dev.fd7689c",
3
+ "version": "0.0.7-dev.bd12468",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/search.ts CHANGED
@@ -761,6 +761,8 @@ export const createSearch = (
761
761
  );
762
762
  };
763
763
  const isFingerprintedLikeTlc = hasWideString(config, environment);
764
+ // One fingerprinter for every level keeps the values it cached for the states of earlier levels.
765
+ let storedFingerprinter: ReturnType<typeof createTlcFingerprinter> | undefined;
764
766
  runtime.getFingerprint = getTlcStateFingerprint;
765
767
  const findUnwitnessedPossible = () => {
766
768
  const unwitnessed = possible.find((item) => !runtime.possibleCounts.get(item.name));
@@ -784,13 +786,8 @@ export const createSearch = (
784
786
  if (view !== undefined || isFingerprintedLikeTlc) {
785
787
  return BigInt64Array.from(states, getTlcStateFingerprint);
786
788
  }
787
- const fingerprints = new BigInt64Array(fingerprintStates.length);
788
- const fingerprint = createTlcFingerprinter(
789
- getTlcOrder(),
790
- new Int32Array(fingerprints.buffer),
791
- );
792
- fingerprintStates.forEach(fingerprint);
793
- return fingerprints;
789
+ storedFingerprinter ??= createTlcFingerprinter(getTlcOrder());
790
+ return storedFingerprinter(fingerprintStates);
794
791
  },
795
792
  getFingerprintState: (state) => {
796
793
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
package/src/successors.ts CHANGED
@@ -417,15 +417,20 @@ const compileConjunction = (
417
417
  if (conjunct.kind === "Unchanged") {
418
418
  const state = run.current;
419
419
  if (!state) return;
420
+ let unchanged: State | undefined;
420
421
  for (const variable of conjunct.indices) {
421
- const assigned = current[variable];
422
+ const assigned = (unchanged ?? current)[variable];
422
423
  if (assigned === undefined) {
423
- current = assign(current, variable, state[variable]);
424
- context = undefined;
424
+ unchanged ??= current.slice();
425
+ unchanged[variable] = state[variable];
425
426
  } else if (!valuesEqual(assigned, state[variable])) {
426
427
  return;
427
428
  }
428
429
  }
430
+ if (unchanged) {
431
+ current = unchanged;
432
+ context = undefined;
433
+ }
429
434
  continue;
430
435
  }
431
436
  const next = index + 1;
@@ -657,13 +662,54 @@ const compileIdentifier = (
657
662
 
658
663
  // A PlusCal action starts with a guard such as pc[self] = "L1", and a process is a disjunction of
659
664
  // its actions. The labels an expression requires of one variable at one bound name let a
660
- // disjunction skip the operands whose guard is false without evaluating them.
665
+ // disjunction skip the operands whose guard is false without evaluating them. A hand-written
666
+ // action often puts other guards first, such as running = self /\ pc[self] = "L1". TLC evaluates
667
+ // those before the label's guard, so a skipped operand still runs them, for their errors.
661
668
  interface LabelGuard {
662
669
  variable: number;
663
670
  argument: string;
664
671
  labels: Set<string>;
672
+ before: ((run: Run, scope: Scope | undefined, partial: State) => void) | undefined;
665
673
  }
666
674
 
675
+ const toLabelGuard = (expression: Expression, environment: Environment) => {
676
+ if (expression.kind !== "Apply" || expression.operator !== "=" || isLocallyResolved(expression)) {
677
+ return undefined;
678
+ }
679
+ const target = expression.args[0];
680
+ const label = expression.args[1];
681
+ if (target.kind !== "FunctionApplication" || label.kind !== "String") return undefined;
682
+ const argument = target.args.length === 1 ? target.args[0] : undefined;
683
+ if (argument?.kind !== "Identifier" || !isLocallyResolved(argument)) return undefined;
684
+ const variable = getStaticVariableIndex(target.function, environment, false);
685
+ if (variable === undefined) return undefined;
686
+ return { variable, argument: argument.name, labels: new Set([label.value]) };
687
+ };
688
+
689
+ const getConjuncts = (expression: Expression): Expression[] =>
690
+ expression.kind === "And" ? expression.operands.flatMap(getConjuncts) : [expression];
691
+
692
+ // What may come before the label's guard: a comparison or a name that a conjunction checks as a
693
+ // guard. An operand with another conjunct first, such as an assignment or a name defined as an
694
+ // action that may branch, runs in full.
695
+ const isLeadingGuard = (expression: Expression, environment: Environment) =>
696
+ (expression.kind === "Identifier" ||
697
+ (expression.kind === "Apply" &&
698
+ (expression.operator === "=" || expression.operator === "#"))) &&
699
+ isGuard(expression, environment, false);
700
+
701
+ // Checks the guards before a label's guard as a conjunction does, for TLC's errors.
702
+ const compileLeadingGuards = (guards: Expression[]) => {
703
+ const evaluators: (Evaluator | undefined)[] = [];
704
+ return (run: Run, scope: Scope | undefined, partial: State) => {
705
+ const context = getContext(run, scope, partial);
706
+ for (let index = 0; index < guards.length; index++) {
707
+ const guard = guards[index];
708
+ if (!holdsValue(run, guard, evaluateCached(evaluators, index, guard, context))) return;
709
+ }
710
+ };
711
+ };
712
+
667
713
  const getLabelGuard = (
668
714
  expression: Expression,
669
715
  environment: Environment,
@@ -672,18 +718,16 @@ const getLabelGuard = (
672
718
  if (visited.has(expression)) return undefined;
673
719
  visited.add(expression);
674
720
  if (expression.kind === "And") {
675
- const first = expression.operands[0];
676
- if (first.kind !== "Apply" || first.operator !== "=" || isLocallyResolved(first)) {
677
- return undefined;
678
- }
679
- const target = first.args[0];
680
- const label = first.args[1];
681
- if (target.kind !== "FunctionApplication" || label.kind !== "String") return undefined;
682
- const argument = target.args.length === 1 ? target.args[0] : undefined;
683
- if (argument?.kind !== "Identifier" || !isLocallyResolved(argument)) return undefined;
684
- const variable = getStaticVariableIndex(target.function, environment, false);
685
- if (variable === undefined) return undefined;
686
- return { variable, argument: argument.name, labels: new Set([label.value]) };
721
+ const conjuncts = getConjuncts(expression);
722
+ const position = conjuncts.findIndex(
723
+ (conjunct) => !isLeadingGuard(conjunct, environment) || toLabelGuard(conjunct, environment),
724
+ );
725
+ const guard = position === -1 ? undefined : toLabelGuard(conjuncts[position], environment);
726
+ if (!guard) return undefined;
727
+ return {
728
+ ...guard,
729
+ before: position === 0 ? undefined : compileLeadingGuards(conjuncts.slice(0, position)),
730
+ };
687
731
  }
688
732
  if (expression.kind === "Or") {
689
733
  const guards = expression.operands.map((operand) =>
@@ -693,12 +737,13 @@ const getLabelGuard = (
693
737
  const isShared = guards.every(
694
738
  (guard) =>
695
739
  guard !== undefined &&
740
+ guard.before === undefined &&
696
741
  guard.variable === first?.variable &&
697
742
  guard.argument === first.argument,
698
743
  );
699
744
  if (!first || !isShared) return undefined;
700
745
  const labels = new Set(guards.flatMap((guard) => [...(guard?.labels ?? [])]));
701
- return { variable: first.variable, argument: first.argument, labels };
746
+ return { variable: first.variable, argument: first.argument, labels, before: undefined };
702
747
  }
703
748
  if (expression.kind !== "Apply" || isLocallyResolved(expression)) return undefined;
704
749
  const closure = resolveGlobalOperator(expression.operator, environment);
@@ -711,7 +756,18 @@ const getLabelGuard = (
711
756
  if (!guard || argument?.kind !== "Identifier" || !isLocallyResolved(argument)) {
712
757
  return undefined;
713
758
  }
714
- return { variable: guard.variable, argument: argument.name, labels: guard.labels };
759
+ const before = guard.before;
760
+ const bindArguments = before && compileArgumentScope(closure, expression.args, environment);
761
+ return {
762
+ variable: guard.variable,
763
+ argument: argument.name,
764
+ labels: guard.labels,
765
+ before:
766
+ before &&
767
+ bindArguments &&
768
+ ((run, scope, partial) =>
769
+ before(run, bindArguments(getContext(run, scope, partial)), partial)),
770
+ };
715
771
  };
716
772
 
717
773
  const getLabel = (run: Run, scope: Scope | undefined, guard: LabelGuard) => {
@@ -739,15 +795,19 @@ const compileDisjunction = (
739
795
  ? []
740
796
  : operands.map((operand) => getLabelGuard(operand, environment, new Set()));
741
797
  const dispatch = guards.find((guard) => guard !== undefined);
742
- const labels = guards.map((guard) =>
798
+ const dispatched = guards.map((guard) =>
743
799
  guard?.variable === dispatch?.variable && guard?.argument === dispatch?.argument
744
- ? guard?.labels
800
+ ? guard
745
801
  : undefined,
746
802
  );
747
803
  return (run, scope, partial, action, isFixed, onComplete) => {
748
804
  const label = dispatch && getLabel(run, scope, dispatch);
749
805
  for (let index = 0; index < runners.length && !run.isStopped; index++) {
750
- if (label !== undefined && labels[index]?.has(label) === false) continue;
806
+ const guard = dispatched[index];
807
+ if (label !== undefined && guard?.labels.has(label) === false) {
808
+ guard.before?.(run, scope, partial);
809
+ continue;
810
+ }
751
811
  runners[index](run, scope, partial, action, isFixed, onComplete);
752
812
  }
753
813
  };
@@ -4,6 +4,7 @@ import {
4
4
  isModelValue,
5
5
  toFiniteFunction,
6
6
  toFiniteSet,
7
+ type ModelValue,
7
8
  type Value,
8
9
  } from "../values";
9
10
 
@@ -53,24 +54,37 @@ const SET_ENUM_VALUE = 5;
53
54
  const FCN_RCD_VALUE = 9;
54
55
  const MODEL_VALUE = 21;
55
56
 
56
- // Value#fingerPrint for each of TLC's value classes. A model value extends the fingerprint with
57
- // its UniqueString token, and TLC numbers tokens from 1 in intern order.
58
- const extendValue = (value: Value) => {
57
+ // Value#fingerPrint for TLC's booleans, integers, strings, and model values, which returns how
58
+ // many bytes it extends the fingerprint by. A model value extends the fingerprint with its
59
+ // UniqueString token, and TLC numbers tokens from 1 in intern order.
60
+ const extendScalar = (value: boolean | number | string | ModelValue) => {
59
61
  if (typeof value === "boolean") {
60
62
  extendByte(BOOL_VALUE);
61
63
  extendByte(value ? 116 : 102);
62
- } else if (typeof value === "number") {
64
+ return 2;
65
+ }
66
+ if (typeof value === "number") {
63
67
  extendByte(INT_VALUE);
64
68
  extendInt(value);
65
- } else if (typeof value === "string") {
69
+ return 5;
70
+ }
71
+ if (typeof value === "string") {
66
72
  extendByte(STRING_VALUE);
67
73
  extendInt(value.length);
68
74
  for (let index = 0; index < value.length; index++) {
69
75
  extendByte(value.charCodeAt(index));
70
76
  }
71
- } else if (isModelValue(value)) {
72
- extendByte(MODEL_VALUE);
73
- extendInt(getStringRank(value.name) + 1);
77
+ return 5 + value.length;
78
+ }
79
+ extendByte(MODEL_VALUE);
80
+ extendInt(getStringRank(value.name) + 1);
81
+ return 5;
82
+ };
83
+
84
+ // Value#fingerPrint for each of TLC's value classes.
85
+ const extendValue = (value: Value) => {
86
+ if (typeof value !== "object" || isModelValue(value)) {
87
+ extendScalar(value);
74
88
  } else if (isFunctionValue(value)) {
75
89
  const functionValue = toFiniteFunction(value);
76
90
  extendByte(FCN_RCD_VALUE);
@@ -100,24 +114,6 @@ export const getTlcFingerprint = (state: Value[]) => {
100
114
  return fingerprint;
101
115
  };
102
116
 
103
- const getFingerprintLength = (value: Value): number => {
104
- if (typeof value === "boolean") return 2;
105
- if (typeof value === "number" || isModelValue(value)) return 5;
106
- if (typeof value === "string") return 5 + value.length;
107
- if (isFunctionValue(value)) {
108
- const functionValue = toFiniteFunction(value);
109
- return functionValue.domain.reduce<number>(
110
- (length, key, index) =>
111
- length + getFingerprintLength(key) + getFingerprintLength(functionValue.range[index]),
112
- 5,
113
- );
114
- }
115
- return toFiniteSet(value).elements.reduce<number>(
116
- (length, element) => length + getFingerprintLength(element),
117
- 5,
118
- );
119
- };
120
-
121
117
  // The fingerprint after `length` zero bytes, as one table per byte of the fingerprint it shifts.
122
118
  const createShiftTable = (length: number) => {
123
119
  const table = new Int32Array(8 * 256 * 2);
@@ -137,16 +133,22 @@ const createShiftTable = (length: number) => {
137
133
  return table;
138
134
  };
139
135
 
136
+ // The functions and sets a fingerprinter caches before it starts over, which bounds the values it
137
+ // keeps alive.
138
+ const CACHED_VALUE_LIMIT = 1 << 22;
139
+
140
140
  // Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
141
141
  // XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
142
- // The fingerprinter caches each distinct function or set it meets, nested ones included, for one
143
- // pass over the stored states, as states share most of their values. It reads a state's variables
144
- // in `order` and writes each fingerprint's low and high words into `words`, an Int32Array over a
145
- // little-endian BigInt64Array.
146
- export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
142
+ // The fingerprinter caches each distinct function or set it meets, nested ones included, across
143
+ // every batch of states it fingerprints, as states share most of their values, even states of
144
+ // different levels. It reads a state's variables in `order`.
145
+ export const createTlcFingerprinter = (order: number[], limit = CACHED_VALUE_LIMIT) => {
147
146
  const shiftTables = new Map<number, Int32Array>();
148
- const indices = new Map<Value, number>();
147
+ const ids = new Map<Value, number>();
148
+ // The byte count and fingerprint from zero of each cached value, three numbers per id, and the
149
+ // shift table for its byte count.
149
150
  const contributions: number[] = [];
151
+ const contributionShiftTables: Int32Array[] = [];
150
152
  const getShiftTable = (length: number) => {
151
153
  const cached = shiftTables.get(length);
152
154
  if (cached) return cached;
@@ -156,27 +158,26 @@ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
156
158
  };
157
159
  // Extends the fingerprint by a value's bytes and returns how many there are.
158
160
  const extendElement = (value: Value): number => {
159
- if (typeof value !== "object" || isModelValue(value)) {
160
- extendValue(value);
161
- return getFingerprintLength(value);
162
- }
163
- const contribution = getContribution(value);
164
- const shifted = { high, low };
165
- const table = getShiftTable(contributions[contribution]);
166
- high = contributions[contribution + 1];
167
- low = contributions[contribution + 2];
161
+ if (typeof value !== "object" || isModelValue(value)) return extendScalar(value);
162
+ const id = getId(value);
163
+ const table = contributionShiftTables[id];
164
+ const shiftedHigh = high;
165
+ const shiftedLow = low;
166
+ high = contributions[3 * id + 1];
167
+ low = contributions[3 * id + 2];
168
168
  for (let position = 0; position < 8; position++) {
169
- const word = position < 4 ? shifted.low : shifted.high;
169
+ const word = position < 4 ? shiftedLow : shiftedHigh;
170
170
  const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
171
171
  high ^= table[entry];
172
172
  low ^= table[entry + 1];
173
173
  }
174
- return contributions[contribution];
174
+ return contributions[3 * id];
175
175
  };
176
- const getContribution = (value: Value) => {
177
- const cached = indices.get(value);
176
+ const getId = (value: Value) => {
177
+ const cached = ids.get(value);
178
178
  if (cached !== undefined) return cached;
179
- const outer = { high, low };
179
+ const outerHigh = high;
180
+ const outerLow = low;
180
181
  high = 0;
181
182
  low = 0;
182
183
  const length = { value: 5 };
@@ -194,21 +195,35 @@ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
194
195
  extendInt(elements.length);
195
196
  for (const element of elements) length.value += extendElement(element);
196
197
  }
197
- const contribution = contributions.length;
198
- indices.set(value, contribution);
198
+ const id = contributionShiftTables.length;
199
+ ids.set(value, id);
199
200
  contributions.push(length.value, high, low);
200
- high = outer.high;
201
- low = outer.low;
202
- return contribution;
201
+ contributionShiftTables.push(getShiftTable(length.value));
202
+ high = outerHigh;
203
+ low = outerLow;
204
+ return id;
203
205
  };
204
- return (state: Value[], position: number) => {
205
- const outer = { high, low };
206
- high = INITIAL_HIGH;
207
- low = INITIAL_LOW;
208
- for (const index of order) extendElement(state[index]);
209
- words[2 * position] = low;
210
- words[2 * position + 1] = high;
211
- high = outer.high;
212
- low = outer.low;
206
+ return (states: Value[][]) => {
207
+ const fingerprints = new BigInt64Array(states.length);
208
+ // Each fingerprint's low and high words, as a BigInt64Array is little-endian.
209
+ const words = new Int32Array(fingerprints.buffer);
210
+ const outerHigh = high;
211
+ const outerLow = low;
212
+ states.forEach((state, position) => {
213
+ if (ids.size >= limit) {
214
+ shiftTables.clear();
215
+ ids.clear();
216
+ contributions.length = 0;
217
+ contributionShiftTables.length = 0;
218
+ }
219
+ high = INITIAL_HIGH;
220
+ low = INITIAL_LOW;
221
+ for (const index of order) extendElement(state[index]);
222
+ words[2 * position] = low;
223
+ words[2 * position + 1] = high;
224
+ });
225
+ high = outerHigh;
226
+ low = outerLow;
227
+ return fingerprints;
213
228
  };
214
229
  };