tla-ts 0.0.9-dev.2813db5 → 0.0.9-dev.5790648
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.cjs +1 -1
- package/dist/run.js +1 -1
- package/dist/transfer.cjs +11 -11
- package/dist/transfer.js +18 -18
- package/package.json +1 -1
- package/src/environment.ts +4 -0
- package/src/instances.ts +67 -6
package/package.json
CHANGED
package/src/environment.ts
CHANGED
|
@@ -531,6 +531,8 @@ export const createEnvironment = (
|
|
|
531
531
|
),
|
|
532
532
|
variables: new Set(linked.variables),
|
|
533
533
|
getDefinition: (key) => definitions.get(key),
|
|
534
|
+
getOuterDefinition: (name) =>
|
|
535
|
+
module.definitions.find((definition) => definition.name === name),
|
|
534
536
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
535
537
|
addDefinition: (source, name, create) => {
|
|
536
538
|
const key = createName(`${name}@${module.name}`);
|
|
@@ -742,6 +744,8 @@ export const createEnvironment = (
|
|
|
742
744
|
),
|
|
743
745
|
variables: new Set(linked.variables),
|
|
744
746
|
getDefinition: (key) => definitions.get(key),
|
|
747
|
+
// Each substitution is a local definition, which the copies read by name.
|
|
748
|
+
getOuterDefinition: () => undefined,
|
|
745
749
|
isParameterized: (key) => parameterized.get(key) === true,
|
|
746
750
|
addDefinition: (_source, name, create) => {
|
|
747
751
|
const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
|
package/src/instances.ts
CHANGED
|
@@ -14,8 +14,6 @@ import {
|
|
|
14
14
|
import { TlaSyntaxError } from "./errors";
|
|
15
15
|
import { getArity, getBoundNames, getPatternNames, isBefore, LOCAL_RESOLUTION } from "./resolve";
|
|
16
16
|
|
|
17
|
-
export const substitutedExpressions = new WeakSet<Expression>();
|
|
18
|
-
|
|
19
17
|
// Applications inside an instance with substitutions or parameters, which TLC evaluates in a
|
|
20
18
|
// context that binds them.
|
|
21
19
|
export const contextualApplications = new WeakSet<Expression>();
|
|
@@ -224,6 +222,8 @@ interface Instantiation {
|
|
|
224
222
|
substitutions: Map<string, Expression>;
|
|
225
223
|
variables: Set<string>;
|
|
226
224
|
getDefinition: (key: string) => Definition | undefined;
|
|
225
|
+
// A definition of the instantiating module that a substitution names.
|
|
226
|
+
getOuterDefinition: (name: string) => Definition | undefined;
|
|
227
227
|
isParameterized: (key: string) => boolean;
|
|
228
228
|
addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
|
|
229
229
|
createName: (name: string) => string;
|
|
@@ -240,8 +240,40 @@ interface Rewrite {
|
|
|
240
240
|
copies: Map<string, string>;
|
|
241
241
|
mode: RewriteMode;
|
|
242
242
|
modes: Map<RewriteMode, Rewrite>;
|
|
243
|
+
// The substitutions an operator's copy reads, which it binds once, as TLC binds each to a lazy
|
|
244
|
+
// value when it evaluates the operator's body.
|
|
245
|
+
bindings?: Map<string, Expression>;
|
|
243
246
|
}
|
|
244
247
|
|
|
248
|
+
const getSubstitutionName = (name: string) => `${name}@SUBSTITUTION`;
|
|
249
|
+
|
|
250
|
+
const isVariableAlias = (expression: Expression): boolean =>
|
|
251
|
+
expression.kind === "Identifier" ||
|
|
252
|
+
(expression.kind === "Tuple" && expression.elements.every(isVariableAlias));
|
|
253
|
+
|
|
254
|
+
// A name, or a tuple of names, stays in place, so a step can assign it or leave it unchanged as
|
|
255
|
+
// TLC does through its lazy value. An operator or a literal costs nothing to evaluate again. A name
|
|
256
|
+
// of a definition that isn't such an alias is bound once.
|
|
257
|
+
const isBoundOnce = (replacement: Expression, instantiation: Instantiation) => {
|
|
258
|
+
if (replacement.kind !== "Identifier") {
|
|
259
|
+
return (
|
|
260
|
+
!isVariableAlias(replacement) &&
|
|
261
|
+
replacement.kind !== "OperatorReference" &&
|
|
262
|
+
replacement.kind !== "Lambda" &&
|
|
263
|
+
replacement.kind !== "Number" &&
|
|
264
|
+
replacement.kind !== "String"
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
const definition = isLocallyResolved(replacement)
|
|
268
|
+
? undefined
|
|
269
|
+
: instantiation.getOuterDefinition(replacement.name);
|
|
270
|
+
return (
|
|
271
|
+
definition?.kind === "Operator" &&
|
|
272
|
+
definition.params.length === 0 &&
|
|
273
|
+
!isVariableAlias(definition.body)
|
|
274
|
+
);
|
|
275
|
+
};
|
|
276
|
+
|
|
245
277
|
const getFreeNames = (expression: Expression, bound = new Set<string>()): string[] => {
|
|
246
278
|
const name =
|
|
247
279
|
expression.kind === "Identifier"
|
|
@@ -515,10 +547,15 @@ const substitute = (
|
|
|
515
547
|
if (enabled) return enabled;
|
|
516
548
|
if (expression.kind === "Identifier" && isFree(expression.name)) {
|
|
517
549
|
const replacement = instantiation.substitutions.get(expression.name);
|
|
518
|
-
|
|
519
|
-
|
|
550
|
+
const bindings = rewrite.bindings;
|
|
551
|
+
if (replacement && (bindings === undefined || !isBoundOnce(replacement, instantiation))) {
|
|
520
552
|
return replacement;
|
|
521
553
|
}
|
|
554
|
+
if (replacement && bindings) {
|
|
555
|
+
const name = getSubstitutionName(expression.name);
|
|
556
|
+
bindings.set(name, replacement);
|
|
557
|
+
return createLocalIdentifier(name, expression.location);
|
|
558
|
+
}
|
|
522
559
|
const copy = getCopy(rewrite, expression.name);
|
|
523
560
|
if (copy === undefined) return expression;
|
|
524
561
|
if (params.length === 0) return { ...expression, name: copy };
|
|
@@ -649,13 +686,37 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
|
|
|
649
686
|
return instantiation.addDefinition(key, definition.name, (copyKey) => {
|
|
650
687
|
rewrite.copies.set(key, copyKey);
|
|
651
688
|
const rename = createRenamer(rewrite);
|
|
689
|
+
const bindings = new Map<string, Expression>();
|
|
690
|
+
const bodyRewrite =
|
|
691
|
+
definition.kind === "Operator" && rewrite.mode === "Normal"
|
|
692
|
+
? { ...rewrite, bindings }
|
|
693
|
+
: rewrite;
|
|
652
694
|
const mapped = renameParams(
|
|
653
695
|
mapDefinition(definition, (child, bound) =>
|
|
654
|
-
substitute(child,
|
|
696
|
+
substitute(child, bodyRewrite, bindNames(new Map(), bound, rename)),
|
|
655
697
|
),
|
|
656
698
|
rename,
|
|
657
699
|
);
|
|
658
|
-
|
|
700
|
+
if (mapped.kind !== "Operator" || bindings.size === 0) {
|
|
701
|
+
return withInstanceParams(mapped, instantiation.params, definition.location);
|
|
702
|
+
}
|
|
703
|
+
const definitions = [...bindings].map(([name, replacement]): Definition => ({
|
|
704
|
+
kind: "Operator",
|
|
705
|
+
name,
|
|
706
|
+
params: [],
|
|
707
|
+
body: replacement,
|
|
708
|
+
isLocal: true,
|
|
709
|
+
isRecursive: false,
|
|
710
|
+
location: replacement.location,
|
|
711
|
+
}));
|
|
712
|
+
return withInstanceParams(
|
|
713
|
+
{
|
|
714
|
+
...mapped,
|
|
715
|
+
body: { kind: "Let", definitions, body: mapped.body, location: mapped.body.location },
|
|
716
|
+
},
|
|
717
|
+
instantiation.params,
|
|
718
|
+
definition.location,
|
|
719
|
+
);
|
|
659
720
|
});
|
|
660
721
|
};
|
|
661
722
|
|