tla-ts 0.0.4-dev.66582d4 → 0.0.4-dev.7c70fed
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/codegen.ts +26 -7
- package/src/evaluate.ts +226 -61
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +1 -1
- package/src/tlc-random.ts +55 -32
- package/src/values.ts +50 -43
package/package.json
CHANGED
package/src/codegen.ts
CHANGED
|
@@ -177,7 +177,7 @@ const generateQuantifier = (
|
|
|
177
177
|
}, test);
|
|
178
178
|
const parameters = outer.map((local) => `, ${local}`).join("");
|
|
179
179
|
generator.helpers.push(
|
|
180
|
-
`function ${name}(context, state, next${parameters}) { let primed; ${declarations.join(" ")} ${loops} return ${isForall}; }`,
|
|
180
|
+
`function ${name}(context, state, next${parameters}) { let primed; let applied; ${declarations.join(" ")} ${loops} return ${isForall}; }`,
|
|
181
181
|
);
|
|
182
182
|
return `${name}(${frame.context}, ${frame.state}, ${frame.isPrimed ? "undefined" : frame.next}${parameters})`;
|
|
183
183
|
};
|
|
@@ -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,17 +304,19 @@ 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
|
-
|
|
300
|
-
|
|
314
|
+
: isReadOnly(expression.function)
|
|
315
|
+
? generate(expression.function, keepLazyFrame)
|
|
316
|
+
: generateDelegate(generator, expression.function, keepLazyFrame);
|
|
317
|
+
// Tool#evalAppl checks that it applies a function before it evaluates the argument. The check
|
|
318
|
+
// is written out, as calling expectFunction for each application costs more.
|
|
319
|
+
const functionCode = `(applied = ${evaluatedFunction}, typeof applied === "object" && (applied.kind === "Function" || applied.kind === "LazyFunction") ? applied : expectFunction(applied, ${addReference(generator, expression)}))`;
|
|
301
320
|
const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
|
|
302
321
|
const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
|
|
303
322
|
if (expression.args.length === 1)
|
|
@@ -370,7 +389,7 @@ export const generateEvaluator = (
|
|
|
370
389
|
};
|
|
371
390
|
const body = generateExpression(generator, expression, frame);
|
|
372
391
|
const referenceNames = generator.references.map((_, index) => `reference${index}`);
|
|
373
|
-
const source = `${generator.helpers.join("\n")}\nreturn (context) => { const state = context.state; const next = context.next; let primed; return ${body}; };`;
|
|
392
|
+
const source = `${generator.helpers.join("\n")}\nreturn (context) => { const state = context.state; const next = context.next; let primed; let applied; return ${body}; };`;
|
|
374
393
|
runtime.value ??= createRuntime();
|
|
375
394
|
const helpers = runtime.value;
|
|
376
395
|
const helperNames = Object.keys(helpers);
|
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,
|
|
@@ -578,15 +586,19 @@ interface LazyFunction {
|
|
|
578
586
|
scope: Scope | undefined;
|
|
579
587
|
}
|
|
580
588
|
|
|
581
|
-
|
|
589
|
+
// The function keeps its parameters, body and scope itself, since a WeakMap entry for each
|
|
590
|
+
// short-lived function makes every garbage collection slower.
|
|
591
|
+
interface KeptLazyFunctionValue extends LazyFunctionValue {
|
|
592
|
+
lambda: LazyFunction;
|
|
593
|
+
}
|
|
582
594
|
|
|
583
|
-
const
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
};
|
|
595
|
+
const isKeptLazyFunction = (value: Value): value is KeptLazyFunctionValue =>
|
|
596
|
+
typeof value === "object" && value.kind === "LazyFunction" && "lambda" in value;
|
|
597
|
+
|
|
598
|
+
const keepLambda = (lambda: LazyFunction) => Object.assign(lambda.value, { lambda });
|
|
587
599
|
|
|
588
600
|
export const getLazyFunction = (value: Value) =>
|
|
589
|
-
|
|
601
|
+
isKeptLazyFunction(value) ? value.lambda : undefined;
|
|
590
602
|
|
|
591
603
|
// Tool.getFcnContext: the scope of a lazy function's body for the arguments of an application. It
|
|
592
604
|
// ignores arguments beyond the parameters.
|
|
@@ -1375,9 +1387,14 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1375
1387
|
getReadVariables(expression, environment) !== undefined &&
|
|
1376
1388
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1377
1389
|
|
|
1378
|
-
//
|
|
1390
|
+
// A set filter TLC evaluates lazily, with how to rebuild it so that its predicate runs in another
|
|
1379
1391
|
// environment.
|
|
1380
|
-
|
|
1392
|
+
interface RebindableFilteredSet extends FilteredSetValue {
|
|
1393
|
+
rebind: (environment: Environment) => Value;
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
const isRebindableFilteredSet = (value: Value): value is RebindableFilteredSet =>
|
|
1397
|
+
typeof value === "object" && value.kind === "FilteredSet" && "rebind" in value;
|
|
1381
1398
|
|
|
1382
1399
|
// TLC's CallStackTool rebinds the lazy values its evaluations return, so their later evaluation
|
|
1383
1400
|
// records its call stack too.
|
|
@@ -1398,11 +1415,7 @@ const traceEvaluator =
|
|
|
1398
1415
|
setTrace.callStack = callStack;
|
|
1399
1416
|
try {
|
|
1400
1417
|
const computed = evaluator(context);
|
|
1401
|
-
const rebind
|
|
1402
|
-
typeof computed === "object" && computed.kind === "FilteredSet"
|
|
1403
|
-
? filteredSetRebinders.get(computed)
|
|
1404
|
-
: undefined;
|
|
1405
|
-
const value = rebind ? rebind(environment) : computed;
|
|
1418
|
+
const value = isRebindableFilteredSet(computed) ? computed.rebind(environment) : computed;
|
|
1406
1419
|
callStack.values.set(expression, value);
|
|
1407
1420
|
if (typeof value === "object" && value.kind !== "FilteredSet" && isSourced(expression)) {
|
|
1408
1421
|
callStack.sources.set(value, expression);
|
|
@@ -1680,52 +1693,180 @@ export const compileArgumentScope = (
|
|
|
1680
1693
|
};
|
|
1681
1694
|
|
|
1682
1695
|
interface MemoizedCall {
|
|
1696
|
+
sources: (Expression | undefined)[];
|
|
1683
1697
|
args: Value[];
|
|
1684
1698
|
value: Value;
|
|
1685
1699
|
}
|
|
1686
1700
|
|
|
1687
1701
|
const MEMOIZED_CALL_LIMIT = 1 << 16;
|
|
1688
1702
|
|
|
1689
|
-
|
|
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 });
|
|
1690
1719
|
|
|
1691
|
-
const
|
|
1720
|
+
const callMemos = new WeakMap<Closure, CallMemos>();
|
|
1721
|
+
|
|
1722
|
+
const getCallMemos = (closure: Closure) => {
|
|
1692
1723
|
const cached = callMemos.get(closure);
|
|
1693
1724
|
if (cached) return cached;
|
|
1694
|
-
const
|
|
1695
|
-
callMemos.set(closure,
|
|
1696
|
-
return
|
|
1725
|
+
const memos = { constant: createCallMemo(), state: createCallMemo() };
|
|
1726
|
+
callMemos.set(closure, memos);
|
|
1727
|
+
return memos;
|
|
1697
1728
|
};
|
|
1698
1729
|
|
|
1699
|
-
const
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
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));
|
|
1704
1796
|
}
|
|
1705
|
-
return
|
|
1797
|
+
return true;
|
|
1706
1798
|
};
|
|
1707
1799
|
|
|
1708
|
-
|
|
1709
|
-
|
|
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
|
+
};
|
|
1842
|
+
|
|
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
|
+
};
|
|
1710
1850
|
|
|
1711
|
-
//
|
|
1712
|
-
// so its calls
|
|
1713
|
-
const
|
|
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) => {
|
|
1714
1854
|
const definition = getDefinition(expression.operator, environment);
|
|
1715
|
-
|
|
1855
|
+
const isMemoizable =
|
|
1716
1856
|
definition?.kind === "Operator" &&
|
|
1717
1857
|
definition.params.length > 0 &&
|
|
1718
1858
|
definition.params.every((param) => param.arity === 0) &&
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
);
|
|
1859
|
+
!isImpureDefinition(definition, environment);
|
|
1860
|
+
if (!isMemoizable) return undefined;
|
|
1861
|
+
if (isConstantLevel(definition, environment)) return "constant";
|
|
1862
|
+
return isStatePure(definition, environment) ? "state" : undefined;
|
|
1722
1863
|
};
|
|
1723
1864
|
|
|
1724
1865
|
const compileClosureCall = (
|
|
1725
1866
|
closure: Closure,
|
|
1726
1867
|
args: Expression[],
|
|
1727
1868
|
environment: Environment,
|
|
1728
|
-
|
|
1869
|
+
memoizedLevel: "constant" | "state" | undefined,
|
|
1729
1870
|
): Evaluator => {
|
|
1730
1871
|
const bindArguments = compileArgumentScope(closure, args, environment);
|
|
1731
1872
|
const compiled = { body: undefined as Evaluator | undefined };
|
|
@@ -1739,23 +1880,49 @@ const compileClosureCall = (
|
|
|
1739
1880
|
isKeepLazy: context.isKeepLazy,
|
|
1740
1881
|
});
|
|
1741
1882
|
};
|
|
1742
|
-
if (!
|
|
1743
|
-
|
|
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 };
|
|
1744
1889
|
return (context) => {
|
|
1745
|
-
const scope = bindArguments(context);
|
|
1746
|
-
const values = getArgumentValues(scope, args.length);
|
|
1747
1890
|
// A call TLC evaluates with EvalControl.KeepLazy builds what a memoized one wouldn't.
|
|
1748
|
-
if (
|
|
1749
|
-
|
|
1750
|
-
|
|
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;
|
|
1751
1911
|
const bucket = memo.calls.get(hash);
|
|
1752
|
-
const known = bucket
|
|
1753
|
-
if (known)
|
|
1754
|
-
|
|
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);
|
|
1755
1921
|
if (memo.size < MEMOIZED_CALL_LIMIT) {
|
|
1756
1922
|
memo.size++;
|
|
1757
|
-
|
|
1758
|
-
|
|
1923
|
+
const call = { sources, args: keyArgs, value };
|
|
1924
|
+
if (bucket) bucket.push(call);
|
|
1925
|
+
else memo.calls.set(hash, [call]);
|
|
1759
1926
|
}
|
|
1760
1927
|
return value;
|
|
1761
1928
|
};
|
|
@@ -1783,8 +1950,10 @@ const compileApply = (
|
|
|
1783
1950
|
: resolveGlobalOperator(expression.operator, environment);
|
|
1784
1951
|
if (global?.kind === "Closure") {
|
|
1785
1952
|
// The replay records the evaluation of each call, which TLC repeats.
|
|
1786
|
-
const
|
|
1787
|
-
|
|
1953
|
+
const memoizedLevel = environment.callStack
|
|
1954
|
+
? undefined
|
|
1955
|
+
: getMemoizedLevel(expression, environment);
|
|
1956
|
+
return compileClosureCall(global, args, environment, memoizedLevel);
|
|
1788
1957
|
}
|
|
1789
1958
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1790
1959
|
const operand = getEvaluator(args[0], environment);
|
|
@@ -2069,22 +2238,18 @@ const compileSetComprehension = (
|
|
|
2069
2238
|
base: Value,
|
|
2070
2239
|
context: Context,
|
|
2071
2240
|
evaluator: Evaluator,
|
|
2072
|
-
):
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
};
|
|
2079
|
-
filteredSetRebinders.set(filtered, (tracing) =>
|
|
2241
|
+
): RebindableFilteredSet => ({
|
|
2242
|
+
kind: "FilteredSet",
|
|
2243
|
+
base,
|
|
2244
|
+
test: createTest(evaluator, context),
|
|
2245
|
+
source,
|
|
2246
|
+
rebind: (tracing) =>
|
|
2080
2247
|
createFiltered(
|
|
2081
2248
|
base,
|
|
2082
2249
|
{ ...context, environment: tracing },
|
|
2083
2250
|
getEvaluator(expression.predicate, tracing),
|
|
2084
2251
|
),
|
|
2085
|
-
|
|
2086
|
-
return filtered;
|
|
2087
|
-
};
|
|
2252
|
+
});
|
|
2088
2253
|
return (context) => {
|
|
2089
2254
|
const base = set(context);
|
|
2090
2255
|
// TLC filters any value it can't enumerate lazily, and fails only when it uses the result.
|
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/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/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
|
|