tla-ts 0.0.4-dev.66582d4 → 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/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/run-cli.cjs +1 -1
- package/dist/run-cli.js +1 -1
- package/dist/transfer.cjs +27 -27
- package/dist/transfer.js +27 -27
- package/package.json +1 -1
- package/src/check.ts +1 -0
- package/src/codegen.ts +26 -7
- package/src/evaluate.ts +251 -72
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +1 -1
- package/src/tlc-random.ts +55 -32
- package/src/values.ts +89 -44
package/src/tlc-random.ts
CHANGED
|
@@ -182,24 +182,14 @@ const createShiftTable = (length: number) => {
|
|
|
182
182
|
|
|
183
183
|
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
184
184
|
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
185
|
-
// The fingerprinter caches each distinct
|
|
186
|
-
//
|
|
187
|
-
// `words`, an Int32Array over a
|
|
185
|
+
// The fingerprinter caches each distinct function or set it meets, nested ones included, for one
|
|
186
|
+
// pass over the stored states, as states share most of their values. It reads a state's variables
|
|
187
|
+
// in `order` and writes each fingerprint's low and high words into `words`, an Int32Array over a
|
|
188
|
+
// little-endian BigInt64Array.
|
|
188
189
|
export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
189
190
|
const shiftTables = new Map<number, Int32Array>();
|
|
190
191
|
const indices = new Map<Value, number>();
|
|
191
192
|
const contributions: number[] = [];
|
|
192
|
-
const getContribution = (value: Value) => {
|
|
193
|
-
const cached = indices.get(value);
|
|
194
|
-
if (cached !== undefined) return cached;
|
|
195
|
-
const length = getFingerprintLength(value);
|
|
196
|
-
high = 0;
|
|
197
|
-
low = 0;
|
|
198
|
-
extendValue(value);
|
|
199
|
-
indices.set(value, contributions.length);
|
|
200
|
-
contributions.push(length, high, low);
|
|
201
|
-
return contributions.length - 3;
|
|
202
|
-
};
|
|
203
193
|
const getShiftTable = (length: number) => {
|
|
204
194
|
const cached = shiftTables.get(length);
|
|
205
195
|
if (cached) return cached;
|
|
@@ -207,29 +197,62 @@ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
|
|
|
207
197
|
shiftTables.set(length, table);
|
|
208
198
|
return table;
|
|
209
199
|
};
|
|
210
|
-
|
|
200
|
+
// Extends the fingerprint by a value's bytes and returns how many there are.
|
|
201
|
+
const extendElement = (value: Value): number => {
|
|
202
|
+
if (typeof value !== "object" || isModelValue(value)) {
|
|
203
|
+
extendValue(value);
|
|
204
|
+
return getFingerprintLength(value);
|
|
205
|
+
}
|
|
206
|
+
const contribution = getContribution(value);
|
|
207
|
+
const shifted = { high, low };
|
|
208
|
+
const table = getShiftTable(contributions[contribution]);
|
|
209
|
+
high = contributions[contribution + 1];
|
|
210
|
+
low = contributions[contribution + 2];
|
|
211
|
+
for (let position = 0; position < 8; position++) {
|
|
212
|
+
const word = position < 4 ? shifted.low : shifted.high;
|
|
213
|
+
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
214
|
+
high ^= table[entry];
|
|
215
|
+
low ^= table[entry + 1];
|
|
216
|
+
}
|
|
217
|
+
return contributions[contribution];
|
|
218
|
+
};
|
|
219
|
+
const getContribution = (value: Value) => {
|
|
220
|
+
const cached = indices.get(value);
|
|
221
|
+
if (cached !== undefined) return cached;
|
|
211
222
|
const outer = { high, low };
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
223
|
-
extended.high ^= table[entry];
|
|
224
|
-
extended.low ^= table[entry + 1];
|
|
223
|
+
high = 0;
|
|
224
|
+
low = 0;
|
|
225
|
+
const length = { value: 5 };
|
|
226
|
+
if (isFunctionValue(value)) {
|
|
227
|
+
const functionValue = toFiniteFunction(value);
|
|
228
|
+
extendByte(FCN_RCD_VALUE);
|
|
229
|
+
extendInt(functionValue.domain.length);
|
|
230
|
+
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
231
|
+
length.value += extendElement(functionValue.domain[index]);
|
|
232
|
+
length.value += extendElement(functionValue.range[index]);
|
|
225
233
|
}
|
|
226
|
-
|
|
227
|
-
|
|
234
|
+
} else {
|
|
235
|
+
const elements = toFiniteSet(value).elements;
|
|
236
|
+
extendByte(SET_ENUM_VALUE);
|
|
237
|
+
extendInt(elements.length);
|
|
238
|
+
for (const element of elements) length.value += extendElement(element);
|
|
228
239
|
}
|
|
240
|
+
const contribution = contributions.length;
|
|
241
|
+
indices.set(value, contribution);
|
|
242
|
+
contributions.push(length.value, high, low);
|
|
243
|
+
high = outer.high;
|
|
244
|
+
low = outer.low;
|
|
245
|
+
return contribution;
|
|
246
|
+
};
|
|
247
|
+
return (state: Value[], position: number) => {
|
|
248
|
+
const outer = { high, low };
|
|
249
|
+
high = INITIAL_HIGH;
|
|
250
|
+
low = INITIAL_LOW;
|
|
251
|
+
for (const index of order) extendElement(state[index]);
|
|
252
|
+
words[2 * position] = low;
|
|
253
|
+
words[2 * position + 1] = high;
|
|
229
254
|
high = outer.high;
|
|
230
255
|
low = outer.low;
|
|
231
|
-
words[2 * position] = fingerprint.low;
|
|
232
|
-
words[2 * position + 1] = fingerprint.high;
|
|
233
256
|
};
|
|
234
257
|
};
|
|
235
258
|
|
package/src/values.ts
CHANGED
|
@@ -14,6 +14,7 @@ export interface SetValue {
|
|
|
14
14
|
elements: Value[];
|
|
15
15
|
hash: number;
|
|
16
16
|
fingerprint: number;
|
|
17
|
+
isFingerprinted: boolean;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
// A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
|
|
@@ -31,6 +32,7 @@ export interface FunctionValue {
|
|
|
31
32
|
fingerprint: number;
|
|
32
33
|
writtenKeys: Value[] | undefined;
|
|
33
34
|
functionClass: FunctionClass | undefined;
|
|
35
|
+
isFingerprinted: boolean;
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
export interface IntervalValue {
|
|
@@ -232,7 +234,13 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
|
|
|
232
234
|
const rightRange = right.range;
|
|
233
235
|
if (leftRange === rightRange) return 0;
|
|
234
236
|
for (let index = 0; index < leftRange.length; index++) {
|
|
235
|
-
const
|
|
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);
|
|
236
244
|
if (valueComparison !== 0) return valueComparison;
|
|
237
245
|
}
|
|
238
246
|
return 0;
|
|
@@ -433,6 +441,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
|
|
|
433
441
|
elements,
|
|
434
442
|
hash: 0,
|
|
435
443
|
fingerprint: 0,
|
|
444
|
+
isFingerprinted: false,
|
|
436
445
|
});
|
|
437
446
|
|
|
438
447
|
export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
|
|
@@ -443,6 +452,7 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
443
452
|
fingerprint: 0,
|
|
444
453
|
writtenKeys: undefined,
|
|
445
454
|
functionClass: undefined,
|
|
455
|
+
isFingerprinted: false,
|
|
446
456
|
});
|
|
447
457
|
|
|
448
458
|
// TLC words a failed function application by the Java class of the function: FcnLambdaValue for
|
|
@@ -512,14 +522,16 @@ const cacheSet = (value: Value) => {
|
|
|
512
522
|
// FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
|
|
513
523
|
// from then on, so a function TLC has fingerprinted is no longer a lambda.
|
|
514
524
|
export const markFingerprinted = (value: Value): void => {
|
|
515
|
-
if (
|
|
516
|
-
(!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
|
|
517
|
-
typeof value !== "object" ||
|
|
518
|
-
fingerprintedValues.has(value)
|
|
519
|
-
) {
|
|
525
|
+
if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
|
|
520
526
|
return;
|
|
521
527
|
}
|
|
522
|
-
|
|
528
|
+
if (value.kind === "Function" || value.kind === "Set") {
|
|
529
|
+
if (value.isFingerprinted) return;
|
|
530
|
+
value.isFingerprinted = true;
|
|
531
|
+
} else {
|
|
532
|
+
if (fingerprintedValues.has(value)) return;
|
|
533
|
+
fingerprintedValues.add(value);
|
|
534
|
+
}
|
|
523
535
|
if (value.kind === "Function") {
|
|
524
536
|
if (value.functionClass === "lambda") value.functionClass = undefined;
|
|
525
537
|
value.domain.forEach(markFingerprinted);
|
|
@@ -1019,10 +1031,7 @@ export const markNormalized = (value: Value): void => {
|
|
|
1019
1031
|
};
|
|
1020
1032
|
|
|
1021
1033
|
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1022
|
-
computeLikeTlcOnFailure(
|
|
1023
|
-
() => valuesEqual(left, right),
|
|
1024
|
-
() => equalsLikeTlc(left, right),
|
|
1025
|
-
);
|
|
1034
|
+
computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
|
|
1026
1035
|
|
|
1027
1036
|
const isStrictlySorted = (elements: Value[]) => {
|
|
1028
1037
|
for (let index = 1; index < elements.length; index++) {
|
|
@@ -1046,9 +1055,11 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
1046
1055
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
1047
1056
|
// evaluates.
|
|
1048
1057
|
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1049
|
-
computeLikeTlcOnFailure<SetValue | MixedSetValue>(
|
|
1050
|
-
|
|
1051
|
-
() => ({ kind: "MixedSet", elements }),
|
|
1058
|
+
computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1059
|
+
createSet,
|
|
1060
|
+
(mixed) => ({ kind: "MixedSet", elements: mixed }),
|
|
1061
|
+
elements,
|
|
1062
|
+
undefined,
|
|
1052
1063
|
);
|
|
1053
1064
|
|
|
1054
1065
|
export const EMPTY_SET = createSortedSet([]);
|
|
@@ -1590,6 +1601,38 @@ const getCombinedSets = (value: ObjectValue) =>
|
|
|
1590
1601
|
? [value.range]
|
|
1591
1602
|
: undefined;
|
|
1592
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
|
+
|
|
1593
1636
|
// Whether a set streams its elements in another order than the normalized set, so that a set of
|
|
1594
1637
|
// tuples, records, or functions it is a component of has to stream too.
|
|
1595
1638
|
const isStreamedApart = (value: Value): boolean => {
|
|
@@ -1864,17 +1907,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
|
|
|
1864
1907
|
};
|
|
1865
1908
|
|
|
1866
1909
|
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1867
|
-
// the failure's leftovers in the replay's trace go first.
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1910
|
+
// the failure's leftovers in the replay's trace go first. Both paths take their operands as
|
|
1911
|
+
// arguments, so a hot caller passes functions it doesn't allocate.
|
|
1912
|
+
export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
1913
|
+
compute: (left: Left, right: Right) => Result,
|
|
1914
|
+
computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
|
|
1915
|
+
left: Left,
|
|
1916
|
+
right: Right,
|
|
1871
1917
|
): Result => {
|
|
1872
1918
|
const sets = setTrace.sets;
|
|
1873
1919
|
const callStack = setTrace.callStack;
|
|
1874
1920
|
const depth = sets?.length ?? 0;
|
|
1875
1921
|
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1876
1922
|
try {
|
|
1877
|
-
return compute();
|
|
1923
|
+
return compute(left, right);
|
|
1878
1924
|
} catch (error) {
|
|
1879
1925
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1880
1926
|
if (sets) sets.length = depth;
|
|
@@ -1883,7 +1929,7 @@ export const computeLikeTlcOnFailure = <Result>(
|
|
|
1883
1929
|
callStack.isFrozen = trace.isFrozen;
|
|
1884
1930
|
callStack.fingerprint = trace.fingerprint;
|
|
1885
1931
|
}
|
|
1886
|
-
return computeLikeTlc(error);
|
|
1932
|
+
return computeLikeTlc(left, right, error);
|
|
1887
1933
|
}
|
|
1888
1934
|
};
|
|
1889
1935
|
|
|
@@ -1896,19 +1942,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1896
1942
|
return isElement;
|
|
1897
1943
|
};
|
|
1898
1944
|
|
|
1945
|
+
const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
|
|
1946
|
+
|
|
1947
|
+
const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
|
|
1948
|
+
normalizedSets.has(set)
|
|
1949
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1950
|
+
: isElementOfLikeTlc(element, set.elements);
|
|
1951
|
+
|
|
1899
1952
|
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1900
1953
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1901
1954
|
switch (set.kind) {
|
|
1902
1955
|
case "Set":
|
|
1903
1956
|
// SetEnumValue.member compares the element with the set's.
|
|
1904
1957
|
cacheSet(element);
|
|
1905
|
-
return computeLikeTlcOnFailure(
|
|
1906
|
-
() => findIndex(set.elements, element) !== -1,
|
|
1907
|
-
() =>
|
|
1908
|
-
normalizedSets.has(set)
|
|
1909
|
-
? searchLikeTlc(element, set.elements).isFound
|
|
1910
|
-
: isElementOfLikeTlc(element, set.elements),
|
|
1911
|
-
);
|
|
1958
|
+
return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
|
|
1912
1959
|
case "MixedSet":
|
|
1913
1960
|
return isElementOfLikeTlc(element, set.elements);
|
|
1914
1961
|
case "Interval":
|
|
@@ -2101,17 +2148,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
|
2101
2148
|
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2102
2149
|
};
|
|
2103
2150
|
|
|
2104
|
-
|
|
2151
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2152
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
2105
2153
|
const first = domain[0];
|
|
2106
2154
|
if (typeof argument === "number" && typeof first === "number") {
|
|
2107
2155
|
const offset = argument - first;
|
|
2108
2156
|
if (domain[offset] === argument) return offset;
|
|
2109
2157
|
}
|
|
2110
|
-
|
|
2111
|
-
for (let index = 0; index < domain.length; index++)
|
|
2112
|
-
if (domain[index] === argument) return index;
|
|
2113
|
-
}
|
|
2114
|
-
return findIndex(domain, argument);
|
|
2158
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2115
2159
|
};
|
|
2116
2160
|
|
|
2117
2161
|
export const createNonIntegerKeyError = (argument: Value) =>
|
|
@@ -2123,6 +2167,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
|
|
|
2123
2167
|
// binary search with compareTo.
|
|
2124
2168
|
const LINEAR_SEARCH_LIMIT = 32;
|
|
2125
2169
|
|
|
2170
|
+
const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
|
|
2171
|
+
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2172
|
+
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2173
|
+
} else {
|
|
2174
|
+
findIndex(domain, argument, compareLikeTlc);
|
|
2175
|
+
}
|
|
2176
|
+
throw error;
|
|
2177
|
+
};
|
|
2178
|
+
|
|
2126
2179
|
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2127
2180
|
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2128
2181
|
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
@@ -2143,17 +2196,9 @@ const findKeyIndexLikeTlc = (
|
|
|
2143
2196
|
throw createNonIntegerKeyError(argument);
|
|
2144
2197
|
}
|
|
2145
2198
|
}
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2150
|
-
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2151
|
-
} else {
|
|
2152
|
-
findIndex(domain, argument, compareLikeTlc);
|
|
2153
|
-
}
|
|
2154
|
-
throw error;
|
|
2155
|
-
},
|
|
2156
|
-
);
|
|
2199
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2200
|
+
if (identical !== -1) return identical;
|
|
2201
|
+
return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
|
|
2157
2202
|
};
|
|
2158
2203
|
|
|
2159
2204
|
export const applyFunction = (
|