tla-ts 0.0.4-dev.2be6058 → 0.0.4-dev.4819fb7
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 +21 -21
- package/dist/transfer.js +21 -21
- package/package.json +1 -1
- package/src/evaluate.ts +201 -37
- package/src/successors.ts +1 -1
- package/src/values.ts +18 -13
package/package.json
CHANGED
package/src/evaluate.ts
CHANGED
|
@@ -21,7 +21,7 @@ import type { CheckerRuntime } from "./runtime";
|
|
|
21
21
|
import { getReadVariables } from "./dependencies";
|
|
22
22
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
23
23
|
import { prettyPrint } from "./pretty-print";
|
|
24
|
-
import { isEnabled } from "./successors";
|
|
24
|
+
import { getReadLocals, isEnabled } from "./successors";
|
|
25
25
|
import {
|
|
26
26
|
applyFunction,
|
|
27
27
|
describeMissingArgument,
|
|
@@ -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,
|
|
@@ -1685,52 +1693,180 @@ export const compileArgumentScope = (
|
|
|
1685
1693
|
};
|
|
1686
1694
|
|
|
1687
1695
|
interface MemoizedCall {
|
|
1696
|
+
sources: (Expression | undefined)[];
|
|
1688
1697
|
args: Value[];
|
|
1689
1698
|
value: Value;
|
|
1690
1699
|
}
|
|
1691
1700
|
|
|
1692
1701
|
const MEMOIZED_CALL_LIMIT = 1 << 16;
|
|
1693
1702
|
|
|
1694
|
-
|
|
1703
|
+
// A call site that finds fewer than half its calls in a state's memo after this many new ones
|
|
1704
|
+
// stops paying for the lookups.
|
|
1705
|
+
const MEMOIZED_TRIAL_CALLS = 1 << 10;
|
|
1706
|
+
|
|
1707
|
+
interface CallMemo {
|
|
1708
|
+
calls: Map<number, MemoizedCall[]>;
|
|
1709
|
+
size: number;
|
|
1710
|
+
state: Context["state"];
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
interface CallMemos {
|
|
1714
|
+
constant: CallMemo;
|
|
1715
|
+
state: CallMemo;
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
const createCallMemo = (): CallMemo => ({ calls: new Map(), size: 0, state: undefined });
|
|
1695
1719
|
|
|
1696
|
-
const
|
|
1720
|
+
const callMemos = new WeakMap<Closure, CallMemos>();
|
|
1721
|
+
|
|
1722
|
+
const getCallMemos = (closure: Closure) => {
|
|
1697
1723
|
const cached = callMemos.get(closure);
|
|
1698
1724
|
if (cached) return cached;
|
|
1699
|
-
const
|
|
1700
|
-
callMemos.set(closure,
|
|
1701
|
-
return
|
|
1725
|
+
const memos = { constant: createCallMemo(), state: createCallMemo() };
|
|
1726
|
+
callMemos.set(closure, memos);
|
|
1727
|
+
return memos;
|
|
1702
1728
|
};
|
|
1703
1729
|
|
|
1704
|
-
const
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1730
|
+
const readsExceptAt = (expression: Expression): boolean =>
|
|
1731
|
+
expression.kind === "ExceptAt" || getChildren(expression).some(readsExceptAt);
|
|
1732
|
+
|
|
1733
|
+
// The locals an argument expression reads, when the state and their values decide its value. An @
|
|
1734
|
+
// reads the value its EXCEPT binds.
|
|
1735
|
+
const argumentLocals = new WeakMap<Expression, string[] | false>();
|
|
1736
|
+
|
|
1737
|
+
const getArgumentLocals = (expression: Expression, environment: Environment) => {
|
|
1738
|
+
const cached = argumentLocals.get(expression);
|
|
1739
|
+
if (cached !== undefined) return cached;
|
|
1740
|
+
const locals =
|
|
1741
|
+
getReadVariables(expression, environment) !== undefined &&
|
|
1742
|
+
getReadLocals(expression, NO_BOUND_NAMES);
|
|
1743
|
+
const unique = locals && [
|
|
1744
|
+
...new Set(readsExceptAt(expression) ? [...locals, EXCEPT_AT] : locals),
|
|
1745
|
+
];
|
|
1746
|
+
argumentLocals.set(expression, unique ?? false);
|
|
1747
|
+
return unique ?? false;
|
|
1748
|
+
};
|
|
1749
|
+
|
|
1750
|
+
// Hashing a lazy set or function would enumerate it, which TLC does only when it has to.
|
|
1751
|
+
const isEagerValue = (value: Value) =>
|
|
1752
|
+
typeof value !== "object" ||
|
|
1753
|
+
value.kind === "ModelValue" ||
|
|
1754
|
+
value.kind === "Set" ||
|
|
1755
|
+
value.kind === "Function" ||
|
|
1756
|
+
value.kind === "Interval";
|
|
1757
|
+
|
|
1758
|
+
// The key of the call being looked up, copied into the memo only when the call is new. Counts
|
|
1759
|
+
// mark its ends, since shortening an array is slow.
|
|
1760
|
+
const callKey = {
|
|
1761
|
+
sources: [] as (Expression | undefined)[],
|
|
1762
|
+
sourceCount: 0,
|
|
1763
|
+
args: [] as Value[],
|
|
1764
|
+
argCount: 0,
|
|
1765
|
+
hash: 0,
|
|
1766
|
+
isStateKeyed: false,
|
|
1767
|
+
};
|
|
1768
|
+
|
|
1769
|
+
// A model value's rank tells it apart without hashing its name.
|
|
1770
|
+
const getKeyHash = (value: Value) =>
|
|
1771
|
+
typeof value === "object" && value.kind === "ModelValue" ? value.rank : getValueHash(value);
|
|
1772
|
+
|
|
1773
|
+
const pushKeyValue = (value: Value) => {
|
|
1774
|
+
if (!isEagerValue(value)) return false;
|
|
1775
|
+
callKey.sources[callKey.sourceCount++] = undefined;
|
|
1776
|
+
callKey.args[callKey.argCount++] = value;
|
|
1777
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(value));
|
|
1778
|
+
return true;
|
|
1779
|
+
};
|
|
1780
|
+
|
|
1781
|
+
// An argument TLC has not evaluated yet stands for its expression and the values of the locals it
|
|
1782
|
+
// reads, so a call keeps the laziness of its arguments.
|
|
1783
|
+
const pushKeyExpression = (
|
|
1784
|
+
expression: Expression,
|
|
1785
|
+
locals: string[] | false,
|
|
1786
|
+
scope: Scope | undefined,
|
|
1787
|
+
) => {
|
|
1788
|
+
if (!locals) return false;
|
|
1789
|
+
callKey.sources[callKey.sourceCount++] = expression;
|
|
1790
|
+
callKey.isStateKeyed = true;
|
|
1791
|
+
for (let index = 0; index < locals.length; index++) {
|
|
1792
|
+
const local = lookupScope(scope, locals[index]);
|
|
1793
|
+
if (local?.kind !== "Value" || !isEagerValue(local.value)) return false;
|
|
1794
|
+
callKey.args[callKey.argCount++] = local.value;
|
|
1795
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(local.value));
|
|
1709
1796
|
}
|
|
1710
|
-
return
|
|
1797
|
+
return true;
|
|
1711
1798
|
};
|
|
1712
1799
|
|
|
1713
|
-
|
|
1714
|
-
|
|
1800
|
+
// Each argument adds to the key what compileArgumentBinder would bind its parameter to.
|
|
1801
|
+
const compileArgumentKey = (
|
|
1802
|
+
argument: Expression,
|
|
1803
|
+
environment: Environment,
|
|
1804
|
+
): ((context: Context) => boolean) => {
|
|
1805
|
+
const isLiteral =
|
|
1806
|
+
argument.kind === "String" || (argument.kind === "Number" && isTlcInteger(argument.value));
|
|
1807
|
+
if (isLiteral) return () => pushKeyValue(argument.value);
|
|
1808
|
+
if (argument.kind === "Identifier") {
|
|
1809
|
+
const global = isLocallyResolved(argument)
|
|
1810
|
+
? undefined
|
|
1811
|
+
: findGlobalName(argument.name, environment);
|
|
1812
|
+
if (global?.kind === "Value") return () => pushKeyValue(global.value);
|
|
1813
|
+
if (global === undefined) {
|
|
1814
|
+
return (context) => {
|
|
1815
|
+
const binding = lookupScope(context.scope, argument.name);
|
|
1816
|
+
if (binding?.kind === "Value") return pushKeyValue(binding.value);
|
|
1817
|
+
if (binding?.kind !== "Thunk" || binding.state !== context.state) return false;
|
|
1818
|
+
return pushKeyExpression(
|
|
1819
|
+
binding.expression,
|
|
1820
|
+
getArgumentLocals(binding.expression, environment),
|
|
1821
|
+
binding.scope,
|
|
1822
|
+
);
|
|
1823
|
+
};
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
const locals = getArgumentLocals(argument, environment);
|
|
1827
|
+
return (context) => pushKeyExpression(argument, locals, context.scope);
|
|
1828
|
+
};
|
|
1829
|
+
|
|
1830
|
+
const isKnownCall = (call: MemoizedCall) => {
|
|
1831
|
+
if (call.args.length !== callKey.argCount || call.sources.length !== callKey.sourceCount) {
|
|
1832
|
+
return false;
|
|
1833
|
+
}
|
|
1834
|
+
for (let index = 0; index < callKey.sourceCount; index++) {
|
|
1835
|
+
if (call.sources[index] !== callKey.sources[index]) return false;
|
|
1836
|
+
}
|
|
1837
|
+
for (let index = 0; index < callKey.argCount; index++) {
|
|
1838
|
+
if (!isSameValue(call.args[index], callKey.args[index])) return false;
|
|
1839
|
+
}
|
|
1840
|
+
return true;
|
|
1841
|
+
};
|
|
1715
1842
|
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1843
|
+
const findCall = (bucket: MemoizedCall[] | undefined) => {
|
|
1844
|
+
if (bucket === undefined) return undefined;
|
|
1845
|
+
for (let index = 0; index < bucket.length; index++) {
|
|
1846
|
+
if (isKnownCall(bucket[index])) return bucket[index];
|
|
1847
|
+
}
|
|
1848
|
+
return undefined;
|
|
1849
|
+
};
|
|
1850
|
+
|
|
1851
|
+
// An operator without side effects yields the same value for the same arguments, a state-level one
|
|
1852
|
+
// in the same state, so its calls share one result.
|
|
1853
|
+
const getMemoizedLevel = (expression: Expression & { kind: "Apply" }, environment: Environment) => {
|
|
1719
1854
|
const definition = getDefinition(expression.operator, environment);
|
|
1720
|
-
|
|
1855
|
+
const isMemoizable =
|
|
1721
1856
|
definition?.kind === "Operator" &&
|
|
1722
1857
|
definition.params.length > 0 &&
|
|
1723
1858
|
definition.params.every((param) => param.arity === 0) &&
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
);
|
|
1859
|
+
!isImpureDefinition(definition, environment);
|
|
1860
|
+
if (!isMemoizable) return undefined;
|
|
1861
|
+
if (isConstantLevel(definition, environment)) return "constant";
|
|
1862
|
+
return isStatePure(definition, environment) ? "state" : undefined;
|
|
1727
1863
|
};
|
|
1728
1864
|
|
|
1729
1865
|
const compileClosureCall = (
|
|
1730
1866
|
closure: Closure,
|
|
1731
1867
|
args: Expression[],
|
|
1732
1868
|
environment: Environment,
|
|
1733
|
-
|
|
1869
|
+
memoizedLevel: "constant" | "state" | undefined,
|
|
1734
1870
|
): Evaluator => {
|
|
1735
1871
|
const bindArguments = compileArgumentScope(closure, args, environment);
|
|
1736
1872
|
const compiled = { body: undefined as Evaluator | undefined };
|
|
@@ -1744,23 +1880,49 @@ const compileClosureCall = (
|
|
|
1744
1880
|
isKeepLazy: context.isKeepLazy,
|
|
1745
1881
|
});
|
|
1746
1882
|
};
|
|
1747
|
-
if (!
|
|
1748
|
-
|
|
1883
|
+
if (!memoizedLevel || args.length !== closure.params.length) {
|
|
1884
|
+
return (context) => evaluateBody(bindArguments(context), context);
|
|
1885
|
+
}
|
|
1886
|
+
const memos = getCallMemos(closure);
|
|
1887
|
+
const keys = args.map((arg) => compileArgumentKey(arg, environment));
|
|
1888
|
+
const usage = { hits: 0, misses: 0 };
|
|
1749
1889
|
return (context) => {
|
|
1750
|
-
const scope = bindArguments(context);
|
|
1751
|
-
const values = getArgumentValues(scope, args.length);
|
|
1752
1890
|
// A call TLC evaluates with EvalControl.KeepLazy builds what a memoized one wouldn't.
|
|
1753
|
-
if (
|
|
1754
|
-
|
|
1755
|
-
|
|
1891
|
+
if (context.isKeepLazy) return evaluateBody(bindArguments(context), context);
|
|
1892
|
+
callKey.sourceCount = 0;
|
|
1893
|
+
callKey.argCount = 0;
|
|
1894
|
+
callKey.hash = args.length;
|
|
1895
|
+
callKey.isStateKeyed = false;
|
|
1896
|
+
for (let index = 0; index < keys.length; index++) {
|
|
1897
|
+
if (!keys[index](context)) return evaluateBody(bindArguments(context), context);
|
|
1898
|
+
}
|
|
1899
|
+
const isStateKeyed = memoizedLevel === "state" || callKey.isStateKeyed;
|
|
1900
|
+
if (isStateKeyed && usage.misses > MEMOIZED_TRIAL_CALLS && usage.hits < usage.misses) {
|
|
1901
|
+
return evaluateBody(bindArguments(context), context);
|
|
1902
|
+
}
|
|
1903
|
+
const memo = isStateKeyed ? memos.state : memos.constant;
|
|
1904
|
+
if (isStateKeyed && memo.state !== context.state) {
|
|
1905
|
+
memo.state = context.state;
|
|
1906
|
+
memo.calls.clear();
|
|
1907
|
+
memo.size = 0;
|
|
1908
|
+
}
|
|
1909
|
+
// A small integer key keeps the Map from boxing it.
|
|
1910
|
+
const hash = callKey.hash & 0x3fffffff;
|
|
1756
1911
|
const bucket = memo.calls.get(hash);
|
|
1757
|
-
const known = bucket
|
|
1758
|
-
if (known)
|
|
1759
|
-
|
|
1912
|
+
const known = findCall(bucket);
|
|
1913
|
+
if (known) {
|
|
1914
|
+
if (isStateKeyed) usage.hits++;
|
|
1915
|
+
return known.value;
|
|
1916
|
+
}
|
|
1917
|
+
if (isStateKeyed) usage.misses++;
|
|
1918
|
+
const sources = callKey.sources.slice(0, callKey.sourceCount);
|
|
1919
|
+
const keyArgs = callKey.args.slice(0, callKey.argCount);
|
|
1920
|
+
const value = evaluateBody(bindArguments(context), context);
|
|
1760
1921
|
if (memo.size < MEMOIZED_CALL_LIMIT) {
|
|
1761
1922
|
memo.size++;
|
|
1762
|
-
|
|
1763
|
-
|
|
1923
|
+
const call = { sources, args: keyArgs, value };
|
|
1924
|
+
if (bucket) bucket.push(call);
|
|
1925
|
+
else memo.calls.set(hash, [call]);
|
|
1764
1926
|
}
|
|
1765
1927
|
return value;
|
|
1766
1928
|
};
|
|
@@ -1788,8 +1950,10 @@ const compileApply = (
|
|
|
1788
1950
|
: resolveGlobalOperator(expression.operator, environment);
|
|
1789
1951
|
if (global?.kind === "Closure") {
|
|
1790
1952
|
// The replay records the evaluation of each call, which TLC repeats.
|
|
1791
|
-
const
|
|
1792
|
-
|
|
1953
|
+
const memoizedLevel = environment.callStack
|
|
1954
|
+
? undefined
|
|
1955
|
+
: getMemoizedLevel(expression, environment);
|
|
1956
|
+
return compileClosureCall(global, args, environment, memoizedLevel);
|
|
1793
1957
|
}
|
|
1794
1958
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1795
1959
|
const operand = getEvaluator(args[0], environment);
|
package/src/successors.ts
CHANGED
|
@@ -265,7 +265,7 @@ const NO_NAMES: ReadonlySet<string> = new Set();
|
|
|
265
265
|
|
|
266
266
|
// The local names an expression reads from enclosing scopes, or undefined when it applies a local
|
|
267
267
|
// operator, whose body may assign variables.
|
|
268
|
-
const getReadLocals = (
|
|
268
|
+
export const getReadLocals = (
|
|
269
269
|
expression: Expression,
|
|
270
270
|
bound: ReadonlySet<string>,
|
|
271
271
|
): string[] | undefined => {
|
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 {
|
|
@@ -433,6 +435,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
|
|
|
433
435
|
elements,
|
|
434
436
|
hash: 0,
|
|
435
437
|
fingerprint: 0,
|
|
438
|
+
isFingerprinted: false,
|
|
436
439
|
});
|
|
437
440
|
|
|
438
441
|
export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
|
|
@@ -443,6 +446,7 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
|
|
|
443
446
|
fingerprint: 0,
|
|
444
447
|
writtenKeys: undefined,
|
|
445
448
|
functionClass: undefined,
|
|
449
|
+
isFingerprinted: false,
|
|
446
450
|
});
|
|
447
451
|
|
|
448
452
|
// TLC words a failed function application by the Java class of the function: FcnLambdaValue for
|
|
@@ -512,14 +516,16 @@ const cacheSet = (value: Value) => {
|
|
|
512
516
|
// FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
|
|
513
517
|
// from then on, so a function TLC has fingerprinted is no longer a lambda.
|
|
514
518
|
export const markFingerprinted = (value: Value): void => {
|
|
515
|
-
if (
|
|
516
|
-
(!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
|
|
517
|
-
typeof value !== "object" ||
|
|
518
|
-
fingerprintedValues.has(value)
|
|
519
|
-
) {
|
|
519
|
+
if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
|
|
520
520
|
return;
|
|
521
521
|
}
|
|
522
|
-
|
|
522
|
+
if (value.kind === "Function" || value.kind === "Set") {
|
|
523
|
+
if (value.isFingerprinted) return;
|
|
524
|
+
value.isFingerprinted = true;
|
|
525
|
+
} else {
|
|
526
|
+
if (fingerprintedValues.has(value)) return;
|
|
527
|
+
fingerprintedValues.add(value);
|
|
528
|
+
}
|
|
523
529
|
if (value.kind === "Function") {
|
|
524
530
|
if (value.functionClass === "lambda") value.functionClass = undefined;
|
|
525
531
|
value.domain.forEach(markFingerprinted);
|
|
@@ -2101,17 +2107,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
|
|
|
2101
2107
|
: `Attempted to apply record to a non-string argument ${shown}.`;
|
|
2102
2108
|
};
|
|
2103
2109
|
|
|
2104
|
-
|
|
2110
|
+
// A key the domain holds itself, which TLC finds without a comparison that can fail.
|
|
2111
|
+
const findIdenticalKey = (domain: Value[], argument: Value) => {
|
|
2105
2112
|
const first = domain[0];
|
|
2106
2113
|
if (typeof argument === "number" && typeof first === "number") {
|
|
2107
2114
|
const offset = argument - first;
|
|
2108
2115
|
if (domain[offset] === argument) return offset;
|
|
2109
2116
|
}
|
|
2110
|
-
|
|
2111
|
-
for (let index = 0; index < domain.length; index++)
|
|
2112
|
-
if (domain[index] === argument) return index;
|
|
2113
|
-
}
|
|
2114
|
-
return findIndex(domain, argument);
|
|
2117
|
+
return domain.length <= 8 ? domain.indexOf(argument) : -1;
|
|
2115
2118
|
};
|
|
2116
2119
|
|
|
2117
2120
|
export const createNonIntegerKeyError = (argument: Value) =>
|
|
@@ -2143,8 +2146,10 @@ const findKeyIndexLikeTlc = (
|
|
|
2143
2146
|
throw createNonIntegerKeyError(argument);
|
|
2144
2147
|
}
|
|
2145
2148
|
}
|
|
2149
|
+
const identical = findIdenticalKey(domain, argument);
|
|
2150
|
+
if (identical !== -1) return identical;
|
|
2146
2151
|
return computeLikeTlcOnFailure(
|
|
2147
|
-
() =>
|
|
2152
|
+
() => findIndex(domain, argument),
|
|
2148
2153
|
(error) => {
|
|
2149
2154
|
if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
|
|
2150
2155
|
domain.forEach((key) => equalsLikeTlc(key, argument));
|