tla-ts 0.0.4-dev.e4c2d0e → 0.0.4-dev.f48b575
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/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/codegen.ts +21 -4
- package/src/evaluate.ts +9 -1
- package/src/standard-modules.ts +5 -2
- package/src/values.ts +38 -37
package/package.json
CHANGED
package/src/codegen.ts
CHANGED
|
@@ -247,6 +247,23 @@ const isConstructionFree = (
|
|
|
247
247
|
}
|
|
248
248
|
};
|
|
249
249
|
|
|
250
|
+
// Whether the generated code evaluates an expression only in the context its frame names. A primed
|
|
251
|
+
// expression doesn't, since it shares the generated function's primed context.
|
|
252
|
+
const isReadOnly = (expression: Expression): boolean => {
|
|
253
|
+
switch (expression.kind) {
|
|
254
|
+
case "Identifier":
|
|
255
|
+
case "Number":
|
|
256
|
+
case "String":
|
|
257
|
+
return true;
|
|
258
|
+
case "FieldAccess":
|
|
259
|
+
return isReadOnly(expression.record);
|
|
260
|
+
case "FunctionApplication":
|
|
261
|
+
return [expression.function, ...expression.args].every(isReadOnly);
|
|
262
|
+
default:
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
250
267
|
const generateExpression = (generator: Generator, expression: Expression, frame: Frame): string => {
|
|
251
268
|
const generate = (node: Expression, nodeFrame = frame) =>
|
|
252
269
|
generateExpression(generator, node, nodeFrame);
|
|
@@ -287,16 +304,16 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
|
|
|
287
304
|
case "FunctionApplication": {
|
|
288
305
|
keepLazy(expression.function);
|
|
289
306
|
// The interpreter evaluates a function that may construct one with EvalControl.KeepLazy.
|
|
307
|
+
const keepLazyFrame = { ...frame, context: `keepLazyContext(${frame.context})` };
|
|
290
308
|
const evaluatedFunction = isConstructionFree(
|
|
291
309
|
expression.function,
|
|
292
310
|
frame,
|
|
293
311
|
generator.environment,
|
|
294
312
|
)
|
|
295
313
|
? generate(expression.function)
|
|
296
|
-
:
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
});
|
|
314
|
+
: isReadOnly(expression.function)
|
|
315
|
+
? generate(expression.function, keepLazyFrame)
|
|
316
|
+
: generateDelegate(generator, expression.function, keepLazyFrame);
|
|
300
317
|
const functionCode = `expectFunction(${evaluatedFunction}, ${addReference(generator, expression)})`;
|
|
301
318
|
const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
|
|
302
319
|
const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
|
package/src/evaluate.ts
CHANGED
|
@@ -380,7 +380,15 @@ export const expectFunction = (value: Value, application: Expression) => {
|
|
|
380
380
|
};
|
|
381
381
|
|
|
382
382
|
export const toKeepLazyContext = (context: Context): Context =>
|
|
383
|
-
context.isKeepLazy
|
|
383
|
+
context.isKeepLazy
|
|
384
|
+
? context
|
|
385
|
+
: {
|
|
386
|
+
environment: context.environment,
|
|
387
|
+
scope: context.scope,
|
|
388
|
+
state: context.state,
|
|
389
|
+
next: context.next,
|
|
390
|
+
isKeepLazy: true,
|
|
391
|
+
};
|
|
384
392
|
|
|
385
393
|
const withScope = (context: Context, scope: Scope | undefined): Context => ({
|
|
386
394
|
environment: context.environment,
|
package/src/standard-modules.ts
CHANGED
|
@@ -162,8 +162,11 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
162
162
|
: differenceSorted;
|
|
163
163
|
// The merge compares the elements, which TLC tests for membership one by one instead.
|
|
164
164
|
return computeLikeTlcOnFailure(
|
|
165
|
-
() => merge(toFiniteSet(
|
|
166
|
-
() =>
|
|
165
|
+
(finiteLeft, finiteRight) => merge(toFiniteSet(finiteLeft), toFiniteSet(finiteRight)),
|
|
166
|
+
(reducibleLeft, reducibleRight) =>
|
|
167
|
+
combineReducible(operator, reducibleLeft, reducibleRight),
|
|
168
|
+
left,
|
|
169
|
+
right,
|
|
167
170
|
);
|
|
168
171
|
}
|
|
169
172
|
if (isReducibleSet(left)) return combineReducible(operator, left, right);
|
package/src/values.ts
CHANGED
|
@@ -1025,10 +1025,7 @@ export const markNormalized = (value: Value): void => {
|
|
|
1025
1025
|
};
|
|
1026
1026
|
|
|
1027
1027
|
export const isEqualLikeTlc = (left: Value, right: Value) =>
|
|
1028
|
-
computeLikeTlcOnFailure(
|
|
1029
|
-
() => valuesEqual(left, right),
|
|
1030
|
-
() => equalsLikeTlc(left, right),
|
|
1031
|
-
);
|
|
1028
|
+
computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
|
|
1032
1029
|
|
|
1033
1030
|
const isStrictlySorted = (elements: Value[]) => {
|
|
1034
1031
|
for (let index = 1; index < elements.length; index++) {
|
|
@@ -1052,9 +1049,11 @@ export const createSet = (elements: Value[]): SetValue => {
|
|
|
1052
1049
|
// TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
|
|
1053
1050
|
// evaluates.
|
|
1054
1051
|
export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
|
|
1055
|
-
computeLikeTlcOnFailure<SetValue | MixedSetValue>(
|
|
1056
|
-
|
|
1057
|
-
() => ({ kind: "MixedSet", elements }),
|
|
1052
|
+
computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
|
|
1053
|
+
createSet,
|
|
1054
|
+
(mixed) => ({ kind: "MixedSet", elements: mixed }),
|
|
1055
|
+
elements,
|
|
1056
|
+
undefined,
|
|
1058
1057
|
);
|
|
1059
1058
|
|
|
1060
1059
|
export const EMPTY_SET = createSortedSet([]);
|
|
@@ -1870,17 +1869,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
|
|
|
1870
1869
|
};
|
|
1871
1870
|
|
|
1872
1871
|
// Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
|
|
1873
|
-
// the failure's leftovers in the replay's trace go first.
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1872
|
+
// the failure's leftovers in the replay's trace go first. Both paths take their operands as
|
|
1873
|
+
// arguments, so a hot caller passes functions it doesn't allocate.
|
|
1874
|
+
export const computeLikeTlcOnFailure = <Left, Right, Result>(
|
|
1875
|
+
compute: (left: Left, right: Right) => Result,
|
|
1876
|
+
computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
|
|
1877
|
+
left: Left,
|
|
1878
|
+
right: Right,
|
|
1877
1879
|
): Result => {
|
|
1878
1880
|
const sets = setTrace.sets;
|
|
1879
1881
|
const callStack = setTrace.callStack;
|
|
1880
1882
|
const depth = sets?.length ?? 0;
|
|
1881
1883
|
const trace = callStack && { ...callStack, length: callStack.expressions.length };
|
|
1882
1884
|
try {
|
|
1883
|
-
return compute();
|
|
1885
|
+
return compute(left, right);
|
|
1884
1886
|
} catch (error) {
|
|
1885
1887
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1886
1888
|
if (sets) sets.length = depth;
|
|
@@ -1889,7 +1891,7 @@ export const computeLikeTlcOnFailure = <Result>(
|
|
|
1889
1891
|
callStack.isFrozen = trace.isFrozen;
|
|
1890
1892
|
callStack.fingerprint = trace.fingerprint;
|
|
1891
1893
|
}
|
|
1892
|
-
return computeLikeTlc(error);
|
|
1894
|
+
return computeLikeTlc(left, right, error);
|
|
1893
1895
|
}
|
|
1894
1896
|
};
|
|
1895
1897
|
|
|
@@ -1902,19 +1904,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1902
1904
|
return isElement;
|
|
1903
1905
|
};
|
|
1904
1906
|
|
|
1907
|
+
const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
|
|
1908
|
+
|
|
1909
|
+
const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
|
|
1910
|
+
normalizedSets.has(set)
|
|
1911
|
+
? searchLikeTlc(element, set.elements).isFound
|
|
1912
|
+
: isElementOfLikeTlc(element, set.elements);
|
|
1913
|
+
|
|
1905
1914
|
const isElementOfSet = (element: Value, set: Value): boolean => {
|
|
1906
1915
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1907
1916
|
switch (set.kind) {
|
|
1908
1917
|
case "Set":
|
|
1909
1918
|
// SetEnumValue.member compares the element with the set's.
|
|
1910
1919
|
cacheSet(element);
|
|
1911
|
-
return computeLikeTlcOnFailure(
|
|
1912
|
-
() => findIndex(set.elements, element) !== -1,
|
|
1913
|
-
() =>
|
|
1914
|
-
normalizedSets.has(set)
|
|
1915
|
-
? searchLikeTlc(element, set.elements).isFound
|
|
1916
|
-
: isElementOfLikeTlc(element, set.elements),
|
|
1917
|
-
);
|
|
1920
|
+
return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
|
|
1918
1921
|
case "MixedSet":
|
|
1919
1922
|
return isElementOfLikeTlc(element, set.elements);
|
|
1920
1923
|
case "Interval":
|
|
@@ -2107,17 +2110,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
|
2107
2110
|
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2108
2111
|
};
|
|
2109
2112
|
|
|
2110
|
-
|
|
2113
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2114
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
2111
2115
|
const first = domain[0];
|
|
2112
2116
|
if (typeof argument === "number" && typeof first === "number") {
|
|
2113
2117
|
const offset = argument - first;
|
|
2114
2118
|
if (domain[offset] === argument) return offset;
|
|
2115
2119
|
}
|
|
2116
|
-
|
|
2117
|
-
for (let index = 0; index < domain.length; index++)
|
|
2118
|
-
if (domain[index] === argument) return index;
|
|
2119
|
-
}
|
|
2120
|
-
return findIndex(domain, argument);
|
|
2120
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2121
2121
|
};
|
|
2122
2122
|
|
|
2123
2123
|
export const createNonIntegerKeyError = (argument: Value) =>
|
|
@@ -2129,6 +2129,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
|
|
|
2129
2129
|
// binary search with compareTo.
|
|
2130
2130
|
const LINEAR_SEARCH_LIMIT = 32;
|
|
2131
2131
|
|
|
2132
|
+
const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
|
|
2133
|
+
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2134
|
+
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2135
|
+
} else {
|
|
2136
|
+
findIndex(domain, argument, compareLikeTlc);
|
|
2137
|
+
}
|
|
2138
|
+
throw error;
|
|
2139
|
+
};
|
|
2140
|
+
|
|
2132
2141
|
// TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
|
|
2133
2142
|
// wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
|
|
2134
2143
|
// TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
|
|
@@ -2149,17 +2158,9 @@ const findKeyIndexLikeTlc = (
|
|
|
2149
2158
|
throw createNonIntegerKeyError(argument);
|
|
2150
2159
|
}
|
|
2151
2160
|
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2156
|
-
domain.forEach((key) => equalsLikeTlc(key, argument));
|
|
2157
|
-
} else {
|
|
2158
|
-
findIndex(domain, argument, compareLikeTlc);
|
|
2159
|
-
}
|
|
2160
|
-
throw error;
|
|
2161
|
-
},
|
|
2162
|
-
);
|
|
2161
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2162
|
+
if (identical !== -1) return identical;
|
|
2163
|
+
return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
|
|
2163
2164
|
};
|
|
2164
2165
|
|
|
2165
2166
|
export const applyFunction = (
|