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/package.json
CHANGED
package/src/check.ts
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,
|
|
@@ -54,6 +54,7 @@ import {
|
|
|
54
54
|
markLambdaFunction,
|
|
55
55
|
isReducibleSet,
|
|
56
56
|
isSetValue,
|
|
57
|
+
isSettledValue,
|
|
57
58
|
MAXIMUM_INTEGER,
|
|
58
59
|
MINIMUM_INTEGER,
|
|
59
60
|
toFiniteFunction,
|
|
@@ -113,6 +114,9 @@ export interface Thunk {
|
|
|
113
114
|
state: State | undefined;
|
|
114
115
|
next: State | undefined;
|
|
115
116
|
cache: Value | undefined;
|
|
117
|
+
// Whether the expression may build a set that streams until TLC caches it, so that the thunk
|
|
118
|
+
// caches only a settled value.
|
|
119
|
+
isStreamed: boolean;
|
|
116
120
|
}
|
|
117
121
|
|
|
118
122
|
type Binding = { kind: "Value"; value: Value } | Thunk | { kind: "Operator"; operator: Operator };
|
|
@@ -380,7 +384,15 @@ export const expectFunction = (value: Value, application: Expression) => {
|
|
|
380
384
|
};
|
|
381
385
|
|
|
382
386
|
export const toKeepLazyContext = (context: Context): Context =>
|
|
383
|
-
context.isKeepLazy
|
|
387
|
+
context.isKeepLazy
|
|
388
|
+
? context
|
|
389
|
+
: {
|
|
390
|
+
environment: context.environment,
|
|
391
|
+
scope: context.scope,
|
|
392
|
+
state: context.state,
|
|
393
|
+
next: context.next,
|
|
394
|
+
isKeepLazy: true,
|
|
395
|
+
};
|
|
384
396
|
|
|
385
397
|
const withScope = (context: Context, scope: Scope | undefined): Context => ({
|
|
386
398
|
environment: context.environment,
|
|
@@ -578,15 +590,19 @@ interface LazyFunction {
|
|
|
578
590
|
scope: Scope | undefined;
|
|
579
591
|
}
|
|
580
592
|
|
|
581
|
-
|
|
593
|
+
// The function keeps its parameters, body and scope itself, since a WeakMap entry for each
|
|
594
|
+
// short-lived function makes every garbage collection slower.
|
|
595
|
+
interface KeptLazyFunctionValue extends LazyFunctionValue {
|
|
596
|
+
lambda: LazyFunction;
|
|
597
|
+
}
|
|
582
598
|
|
|
583
|
-
const
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
};
|
|
599
|
+
const isKeptLazyFunction = (value: Value): value is KeptLazyFunctionValue =>
|
|
600
|
+
typeof value === "object" && value.kind === "LazyFunction" && "lambda" in value;
|
|
601
|
+
|
|
602
|
+
const keepLambda = (lambda: LazyFunction) => Object.assign(lambda.value, { lambda });
|
|
587
603
|
|
|
588
604
|
export const getLazyFunction = (value: Value) =>
|
|
589
|
-
|
|
605
|
+
isKeptLazyFunction(value) ? value.lambda : undefined;
|
|
590
606
|
|
|
591
607
|
// Tool.getFcnContext: the scope of a lazy function's body for the arguments of an application. It
|
|
592
608
|
// ignores arguments beyond the parameters.
|
|
@@ -835,7 +851,7 @@ const forceThunk = (thunk: Thunk, context: Context) => {
|
|
|
835
851
|
const isSameState = thunk.state === context.state && thunk.next === context.next;
|
|
836
852
|
if (isSameState && thunk.cache !== undefined) return thunk.cache;
|
|
837
853
|
const value = evaluate(thunk.expression, withScope(context, thunk.scope));
|
|
838
|
-
if (isSameState) thunk.cache = value;
|
|
854
|
+
if (isSameState && (!thunk.isStreamed || isSettledValue(value))) thunk.cache = value;
|
|
839
855
|
return value;
|
|
840
856
|
};
|
|
841
857
|
|
|
@@ -956,6 +972,7 @@ const createThunk = (expression: Expression, context: Context): Thunk => ({
|
|
|
956
972
|
state: context.state,
|
|
957
973
|
next: context.next,
|
|
958
974
|
cache: undefined,
|
|
975
|
+
isStreamed: false,
|
|
959
976
|
});
|
|
960
977
|
|
|
961
978
|
export const findGlobalName = (name: string, environment: Environment): GlobalName | undefined => {
|
|
@@ -1052,10 +1069,16 @@ const callOperator = (operator: Operator, args: Expression[], context: Context)
|
|
|
1052
1069
|
);
|
|
1053
1070
|
};
|
|
1054
1071
|
|
|
1072
|
+
// The LET definitions that a reference evaluates again, and those that cache only a settled value.
|
|
1073
|
+
interface ReevaluatedDefinitions {
|
|
1074
|
+
impure: ReadonlySet<Definition>;
|
|
1075
|
+
streamed: ReadonlySet<Definition>;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1055
1078
|
export const bindDefinitions = (
|
|
1056
1079
|
definitions: Definition[],
|
|
1057
1080
|
context: Context,
|
|
1058
|
-
reevaluated?:
|
|
1081
|
+
reevaluated?: ReevaluatedDefinitions,
|
|
1059
1082
|
): Scope | undefined =>
|
|
1060
1083
|
definitions.reduce<Scope | undefined>((scope, definition) => {
|
|
1061
1084
|
const node: Scope = {
|
|
@@ -1077,10 +1100,11 @@ export const bindDefinitions = (
|
|
|
1077
1100
|
state: context.state,
|
|
1078
1101
|
next: context.next,
|
|
1079
1102
|
cache: undefined,
|
|
1103
|
+
isStreamed: false,
|
|
1080
1104
|
};
|
|
1081
1105
|
return node;
|
|
1082
1106
|
}
|
|
1083
|
-
if (definition.params.length > 0 || reevaluated?.has(definition)) {
|
|
1107
|
+
if (definition.params.length > 0 || reevaluated?.impure.has(definition)) {
|
|
1084
1108
|
node.binding = {
|
|
1085
1109
|
kind: "Operator",
|
|
1086
1110
|
operator: {
|
|
@@ -1100,6 +1124,7 @@ export const bindDefinitions = (
|
|
|
1100
1124
|
state: context.state,
|
|
1101
1125
|
next: context.next,
|
|
1102
1126
|
cache: undefined,
|
|
1127
|
+
isStreamed: reevaluated?.streamed.has(definition) ?? false,
|
|
1103
1128
|
};
|
|
1104
1129
|
return node;
|
|
1105
1130
|
}, context.scope);
|
|
@@ -1216,14 +1241,15 @@ const STREAMED_SET: Impurity = {
|
|
|
1216
1241
|
// TLC's next-state generator does not bind LET definitions to values, so each reference to one
|
|
1217
1242
|
// evaluates it again: one that calls an impure builtin repeats its effects, and a set one builds
|
|
1218
1243
|
// with UNION or \cup starts out uncached.
|
|
1219
|
-
export const getReevaluatedDefinitions = (
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
),
|
|
1226
|
-
)
|
|
1244
|
+
export const getReevaluatedDefinitions = (
|
|
1245
|
+
definitions: Definition[],
|
|
1246
|
+
environment: Environment,
|
|
1247
|
+
): ReevaluatedDefinitions => ({
|
|
1248
|
+
impure: new Set(definitions.filter((definition) => isImpureDefinition(definition, environment))),
|
|
1249
|
+
streamed: new Set(
|
|
1250
|
+
definitions.filter((definition) => isImpureDefinition(definition, environment, STREAMED_SET)),
|
|
1251
|
+
),
|
|
1252
|
+
});
|
|
1227
1253
|
|
|
1228
1254
|
export const isImpure = (environment: Environment) =>
|
|
1229
1255
|
[...environment.definitions.values()].some((definition) =>
|
|
@@ -1375,9 +1401,14 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1375
1401
|
getReadVariables(expression, environment) !== undefined &&
|
|
1376
1402
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1377
1403
|
|
|
1378
|
-
//
|
|
1404
|
+
// A set filter TLC evaluates lazily, with how to rebuild it so that its predicate runs in another
|
|
1379
1405
|
// environment.
|
|
1380
|
-
|
|
1406
|
+
interface RebindableFilteredSet extends FilteredSetValue {
|
|
1407
|
+
rebind: (environment: Environment) => Value;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
const isRebindableFilteredSet = (value: Value): value is RebindableFilteredSet =>
|
|
1411
|
+
typeof value === "object" && value.kind === "FilteredSet" && "rebind" in value;
|
|
1381
1412
|
|
|
1382
1413
|
// TLC's CallStackTool rebinds the lazy values its evaluations return, so their later evaluation
|
|
1383
1414
|
// records its call stack too.
|
|
@@ -1398,11 +1429,7 @@ const traceEvaluator =
|
|
|
1398
1429
|
setTrace.callStack = callStack;
|
|
1399
1430
|
try {
|
|
1400
1431
|
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;
|
|
1432
|
+
const value = isRebindableFilteredSet(computed) ? computed.rebind(environment) : computed;
|
|
1406
1433
|
callStack.values.set(expression, value);
|
|
1407
1434
|
if (typeof value === "object" && value.kind !== "FilteredSet" && isSourced(expression)) {
|
|
1408
1435
|
callStack.sources.set(value, expression);
|
|
@@ -1680,52 +1707,180 @@ export const compileArgumentScope = (
|
|
|
1680
1707
|
};
|
|
1681
1708
|
|
|
1682
1709
|
interface MemoizedCall {
|
|
1710
|
+
sources: (Expression | undefined)[];
|
|
1683
1711
|
args: Value[];
|
|
1684
1712
|
value: Value;
|
|
1685
1713
|
}
|
|
1686
1714
|
|
|
1687
1715
|
const MEMOIZED_CALL_LIMIT = 1 << 16;
|
|
1688
1716
|
|
|
1689
|
-
|
|
1717
|
+
// A call site that finds fewer than half its calls in a state's memo after this many new ones
|
|
1718
|
+
// stops paying for the lookups.
|
|
1719
|
+
const MEMOIZED_TRIAL_CALLS = 1 << 10;
|
|
1720
|
+
|
|
1721
|
+
interface CallMemo {
|
|
1722
|
+
calls: Map<number, MemoizedCall[]>;
|
|
1723
|
+
size: number;
|
|
1724
|
+
state: Context["state"];
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
interface CallMemos {
|
|
1728
|
+
constant: CallMemo;
|
|
1729
|
+
state: CallMemo;
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
const createCallMemo = (): CallMemo => ({ calls: new Map(), size: 0, state: undefined });
|
|
1690
1733
|
|
|
1691
|
-
const
|
|
1734
|
+
const callMemos = new WeakMap<Closure, CallMemos>();
|
|
1735
|
+
|
|
1736
|
+
const getCallMemos = (closure: Closure) => {
|
|
1692
1737
|
const cached = callMemos.get(closure);
|
|
1693
1738
|
if (cached) return cached;
|
|
1694
|
-
const
|
|
1695
|
-
callMemos.set(closure,
|
|
1696
|
-
return
|
|
1739
|
+
const memos = { constant: createCallMemo(), state: createCallMemo() };
|
|
1740
|
+
callMemos.set(closure, memos);
|
|
1741
|
+
return memos;
|
|
1742
|
+
};
|
|
1743
|
+
|
|
1744
|
+
const readsExceptAt = (expression: Expression): boolean =>
|
|
1745
|
+
expression.kind === "ExceptAt" || getChildren(expression).some(readsExceptAt);
|
|
1746
|
+
|
|
1747
|
+
// The locals an argument expression reads, when the state and their values decide its value. An @
|
|
1748
|
+
// reads the value its EXCEPT binds.
|
|
1749
|
+
const argumentLocals = new WeakMap<Expression, string[] | false>();
|
|
1750
|
+
|
|
1751
|
+
const getArgumentLocals = (expression: Expression, environment: Environment) => {
|
|
1752
|
+
const cached = argumentLocals.get(expression);
|
|
1753
|
+
if (cached !== undefined) return cached;
|
|
1754
|
+
const locals =
|
|
1755
|
+
getReadVariables(expression, environment) !== undefined &&
|
|
1756
|
+
getReadLocals(expression, NO_BOUND_NAMES);
|
|
1757
|
+
const unique = locals && [
|
|
1758
|
+
...new Set(readsExceptAt(expression) ? [...locals, EXCEPT_AT] : locals),
|
|
1759
|
+
];
|
|
1760
|
+
argumentLocals.set(expression, unique ?? false);
|
|
1761
|
+
return unique ?? false;
|
|
1762
|
+
};
|
|
1763
|
+
|
|
1764
|
+
// Hashing a lazy set or function would enumerate it, which TLC does only when it has to.
|
|
1765
|
+
const isEagerValue = (value: Value) =>
|
|
1766
|
+
typeof value !== "object" ||
|
|
1767
|
+
value.kind === "ModelValue" ||
|
|
1768
|
+
value.kind === "Set" ||
|
|
1769
|
+
value.kind === "Function" ||
|
|
1770
|
+
value.kind === "Interval";
|
|
1771
|
+
|
|
1772
|
+
// The key of the call being looked up, copied into the memo only when the call is new. Counts
|
|
1773
|
+
// mark its ends, since shortening an array is slow.
|
|
1774
|
+
const callKey = {
|
|
1775
|
+
sources: [] as (Expression | undefined)[],
|
|
1776
|
+
sourceCount: 0,
|
|
1777
|
+
args: [] as Value[],
|
|
1778
|
+
argCount: 0,
|
|
1779
|
+
hash: 0,
|
|
1780
|
+
isStateKeyed: false,
|
|
1781
|
+
};
|
|
1782
|
+
|
|
1783
|
+
// A model value's rank tells it apart without hashing its name.
|
|
1784
|
+
const getKeyHash = (value: Value) =>
|
|
1785
|
+
typeof value === "object" && value.kind === "ModelValue" ? value.rank : getValueHash(value);
|
|
1786
|
+
|
|
1787
|
+
const pushKeyValue = (value: Value) => {
|
|
1788
|
+
if (!isEagerValue(value)) return false;
|
|
1789
|
+
callKey.sources[callKey.sourceCount++] = undefined;
|
|
1790
|
+
callKey.args[callKey.argCount++] = value;
|
|
1791
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(value));
|
|
1792
|
+
return true;
|
|
1793
|
+
};
|
|
1794
|
+
|
|
1795
|
+
// An argument TLC has not evaluated yet stands for its expression and the values of the locals it
|
|
1796
|
+
// reads, so a call keeps the laziness of its arguments.
|
|
1797
|
+
const pushKeyExpression = (
|
|
1798
|
+
expression: Expression,
|
|
1799
|
+
locals: string[] | false,
|
|
1800
|
+
scope: Scope | undefined,
|
|
1801
|
+
) => {
|
|
1802
|
+
if (!locals) return false;
|
|
1803
|
+
callKey.sources[callKey.sourceCount++] = expression;
|
|
1804
|
+
callKey.isStateKeyed = true;
|
|
1805
|
+
for (let index = 0; index < locals.length; index++) {
|
|
1806
|
+
const local = lookupScope(scope, locals[index]);
|
|
1807
|
+
if (local?.kind !== "Value" || !isEagerValue(local.value)) return false;
|
|
1808
|
+
callKey.args[callKey.argCount++] = local.value;
|
|
1809
|
+
callKey.hash = combineHash(callKey.hash, getKeyHash(local.value));
|
|
1810
|
+
}
|
|
1811
|
+
return true;
|
|
1697
1812
|
};
|
|
1698
1813
|
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1814
|
+
// Each argument adds to the key what compileArgumentBinder would bind its parameter to.
|
|
1815
|
+
const compileArgumentKey = (
|
|
1816
|
+
argument: Expression,
|
|
1817
|
+
environment: Environment,
|
|
1818
|
+
): ((context: Context) => boolean) => {
|
|
1819
|
+
const isLiteral =
|
|
1820
|
+
argument.kind === "String" || (argument.kind === "Number" && isTlcInteger(argument.value));
|
|
1821
|
+
if (isLiteral) return () => pushKeyValue(argument.value);
|
|
1822
|
+
if (argument.kind === "Identifier") {
|
|
1823
|
+
const global = isLocallyResolved(argument)
|
|
1824
|
+
? undefined
|
|
1825
|
+
: findGlobalName(argument.name, environment);
|
|
1826
|
+
if (global?.kind === "Value") return () => pushKeyValue(global.value);
|
|
1827
|
+
if (global === undefined) {
|
|
1828
|
+
return (context) => {
|
|
1829
|
+
const binding = lookupScope(context.scope, argument.name);
|
|
1830
|
+
if (binding?.kind === "Value") return pushKeyValue(binding.value);
|
|
1831
|
+
if (binding?.kind !== "Thunk" || binding.state !== context.state) return false;
|
|
1832
|
+
return pushKeyExpression(
|
|
1833
|
+
binding.expression,
|
|
1834
|
+
getArgumentLocals(binding.expression, environment),
|
|
1835
|
+
binding.scope,
|
|
1836
|
+
);
|
|
1837
|
+
};
|
|
1838
|
+
}
|
|
1839
|
+
}
|
|
1840
|
+
const locals = getArgumentLocals(argument, environment);
|
|
1841
|
+
return (context) => pushKeyExpression(argument, locals, context.scope);
|
|
1842
|
+
};
|
|
1843
|
+
|
|
1844
|
+
const isKnownCall = (call: MemoizedCall) => {
|
|
1845
|
+
if (call.args.length !== callKey.argCount || call.sources.length !== callKey.sourceCount) {
|
|
1846
|
+
return false;
|
|
1704
1847
|
}
|
|
1705
|
-
|
|
1848
|
+
for (let index = 0; index < callKey.sourceCount; index++) {
|
|
1849
|
+
if (call.sources[index] !== callKey.sources[index]) return false;
|
|
1850
|
+
}
|
|
1851
|
+
for (let index = 0; index < callKey.argCount; index++) {
|
|
1852
|
+
if (!isSameValue(call.args[index], callKey.args[index])) return false;
|
|
1853
|
+
}
|
|
1854
|
+
return true;
|
|
1706
1855
|
};
|
|
1707
1856
|
|
|
1708
|
-
const
|
|
1709
|
-
|
|
1857
|
+
const findCall = (bucket: MemoizedCall[] | undefined) => {
|
|
1858
|
+
if (bucket === undefined) return undefined;
|
|
1859
|
+
for (let index = 0; index < bucket.length; index++) {
|
|
1860
|
+
if (isKnownCall(bucket[index])) return bucket[index];
|
|
1861
|
+
}
|
|
1862
|
+
return undefined;
|
|
1863
|
+
};
|
|
1710
1864
|
|
|
1711
|
-
//
|
|
1712
|
-
// so its calls
|
|
1713
|
-
const
|
|
1865
|
+
// An operator without side effects yields the same value for the same arguments, a state-level one
|
|
1866
|
+
// in the same state, so its calls share one result.
|
|
1867
|
+
const getMemoizedLevel = (expression: Expression & { kind: "Apply" }, environment: Environment) => {
|
|
1714
1868
|
const definition = getDefinition(expression.operator, environment);
|
|
1715
|
-
|
|
1869
|
+
const isMemoizable =
|
|
1716
1870
|
definition?.kind === "Operator" &&
|
|
1717
1871
|
definition.params.length > 0 &&
|
|
1718
1872
|
definition.params.every((param) => param.arity === 0) &&
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
);
|
|
1873
|
+
!isImpureDefinition(definition, environment);
|
|
1874
|
+
if (!isMemoizable) return undefined;
|
|
1875
|
+
if (isConstantLevel(definition, environment)) return "constant";
|
|
1876
|
+
return isStatePure(definition, environment) ? "state" : undefined;
|
|
1722
1877
|
};
|
|
1723
1878
|
|
|
1724
1879
|
const compileClosureCall = (
|
|
1725
1880
|
closure: Closure,
|
|
1726
1881
|
args: Expression[],
|
|
1727
1882
|
environment: Environment,
|
|
1728
|
-
|
|
1883
|
+
memoizedLevel: "constant" | "state" | undefined,
|
|
1729
1884
|
): Evaluator => {
|
|
1730
1885
|
const bindArguments = compileArgumentScope(closure, args, environment);
|
|
1731
1886
|
const compiled = { body: undefined as Evaluator | undefined };
|
|
@@ -1739,23 +1894,49 @@ const compileClosureCall = (
|
|
|
1739
1894
|
isKeepLazy: context.isKeepLazy,
|
|
1740
1895
|
});
|
|
1741
1896
|
};
|
|
1742
|
-
if (!
|
|
1743
|
-
|
|
1897
|
+
if (!memoizedLevel || args.length !== closure.params.length) {
|
|
1898
|
+
return (context) => evaluateBody(bindArguments(context), context);
|
|
1899
|
+
}
|
|
1900
|
+
const memos = getCallMemos(closure);
|
|
1901
|
+
const keys = args.map((arg) => compileArgumentKey(arg, environment));
|
|
1902
|
+
const usage = { hits: 0, misses: 0 };
|
|
1744
1903
|
return (context) => {
|
|
1745
|
-
const scope = bindArguments(context);
|
|
1746
|
-
const values = getArgumentValues(scope, args.length);
|
|
1747
1904
|
// A call TLC evaluates with EvalControl.KeepLazy builds what a memoized one wouldn't.
|
|
1748
|
-
if (
|
|
1749
|
-
|
|
1750
|
-
|
|
1905
|
+
if (context.isKeepLazy) return evaluateBody(bindArguments(context), context);
|
|
1906
|
+
callKey.sourceCount = 0;
|
|
1907
|
+
callKey.argCount = 0;
|
|
1908
|
+
callKey.hash = args.length;
|
|
1909
|
+
callKey.isStateKeyed = false;
|
|
1910
|
+
for (let index = 0; index < keys.length; index++) {
|
|
1911
|
+
if (!keys[index](context)) return evaluateBody(bindArguments(context), context);
|
|
1912
|
+
}
|
|
1913
|
+
const isStateKeyed = memoizedLevel === "state" || callKey.isStateKeyed;
|
|
1914
|
+
if (isStateKeyed && usage.misses > MEMOIZED_TRIAL_CALLS && usage.hits < usage.misses) {
|
|
1915
|
+
return evaluateBody(bindArguments(context), context);
|
|
1916
|
+
}
|
|
1917
|
+
const memo = isStateKeyed ? memos.state : memos.constant;
|
|
1918
|
+
if (isStateKeyed && memo.state !== context.state) {
|
|
1919
|
+
memo.state = context.state;
|
|
1920
|
+
memo.calls.clear();
|
|
1921
|
+
memo.size = 0;
|
|
1922
|
+
}
|
|
1923
|
+
// A small integer key keeps the Map from boxing it.
|
|
1924
|
+
const hash = callKey.hash & 0x3fffffff;
|
|
1751
1925
|
const bucket = memo.calls.get(hash);
|
|
1752
|
-
const known = bucket
|
|
1753
|
-
if (known)
|
|
1754
|
-
|
|
1926
|
+
const known = findCall(bucket);
|
|
1927
|
+
if (known) {
|
|
1928
|
+
if (isStateKeyed) usage.hits++;
|
|
1929
|
+
return known.value;
|
|
1930
|
+
}
|
|
1931
|
+
if (isStateKeyed) usage.misses++;
|
|
1932
|
+
const sources = callKey.sources.slice(0, callKey.sourceCount);
|
|
1933
|
+
const keyArgs = callKey.args.slice(0, callKey.argCount);
|
|
1934
|
+
const value = evaluateBody(bindArguments(context), context);
|
|
1755
1935
|
if (memo.size < MEMOIZED_CALL_LIMIT) {
|
|
1756
1936
|
memo.size++;
|
|
1757
|
-
|
|
1758
|
-
|
|
1937
|
+
const call = { sources, args: keyArgs, value };
|
|
1938
|
+
if (bucket) bucket.push(call);
|
|
1939
|
+
else memo.calls.set(hash, [call]);
|
|
1759
1940
|
}
|
|
1760
1941
|
return value;
|
|
1761
1942
|
};
|
|
@@ -1783,8 +1964,10 @@ const compileApply = (
|
|
|
1783
1964
|
: resolveGlobalOperator(expression.operator, environment);
|
|
1784
1965
|
if (global?.kind === "Closure") {
|
|
1785
1966
|
// The replay records the evaluation of each call, which TLC repeats.
|
|
1786
|
-
const
|
|
1787
|
-
|
|
1967
|
+
const memoizedLevel = environment.callStack
|
|
1968
|
+
? undefined
|
|
1969
|
+
: getMemoizedLevel(expression, environment);
|
|
1970
|
+
return compileClosureCall(global, args, environment, memoizedLevel);
|
|
1788
1971
|
}
|
|
1789
1972
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1790
1973
|
const operand = getEvaluator(args[0], environment);
|
|
@@ -2069,22 +2252,18 @@ const compileSetComprehension = (
|
|
|
2069
2252
|
base: Value,
|
|
2070
2253
|
context: Context,
|
|
2071
2254
|
evaluator: Evaluator,
|
|
2072
|
-
):
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
};
|
|
2079
|
-
filteredSetRebinders.set(filtered, (tracing) =>
|
|
2255
|
+
): RebindableFilteredSet => ({
|
|
2256
|
+
kind: "FilteredSet",
|
|
2257
|
+
base,
|
|
2258
|
+
test: createTest(evaluator, context),
|
|
2259
|
+
source,
|
|
2260
|
+
rebind: (tracing) =>
|
|
2080
2261
|
createFiltered(
|
|
2081
2262
|
base,
|
|
2082
2263
|
{ ...context, environment: tracing },
|
|
2083
2264
|
getEvaluator(expression.predicate, tracing),
|
|
2084
2265
|
),
|
|
2085
|
-
|
|
2086
|
-
return filtered;
|
|
2087
|
-
};
|
|
2266
|
+
});
|
|
2088
2267
|
return (context) => {
|
|
2089
2268
|
const base = set(context);
|
|
2090
2269
|
// 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 => {
|