tla-ts 0.0.3-dev.5ea4534 → 0.0.3-dev.660addc
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/transfer.cjs +11 -11
- package/dist/transfer.js +11 -11
- package/package.json +1 -1
- package/src/successors.ts +49 -0
package/package.json
CHANGED
package/src/successors.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type Bound,
|
|
5
5
|
type Definition,
|
|
6
6
|
type Expression,
|
|
7
|
+
type QuantifierExpression,
|
|
7
8
|
} from "./ast";
|
|
8
9
|
import { getReadVariables } from "./dependencies";
|
|
9
10
|
import { TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
@@ -292,6 +293,27 @@ const toConjunct = (
|
|
|
292
293
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
293
294
|
};
|
|
294
295
|
|
|
296
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
297
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
298
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
299
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
300
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
301
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
302
|
+
);
|
|
303
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
304
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
305
|
+
const operands = expression.body.operands;
|
|
306
|
+
const index = operands.findIndex((operand) => {
|
|
307
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
308
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
309
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
310
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
311
|
+
});
|
|
312
|
+
const count = index === -1 ? operands.length : index;
|
|
313
|
+
if (count === 0) return undefined;
|
|
314
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
315
|
+
};
|
|
316
|
+
|
|
295
317
|
const compileConjunction = (
|
|
296
318
|
operands: Expression[],
|
|
297
319
|
environment: Environment,
|
|
@@ -734,6 +756,33 @@ const compileAction = (
|
|
|
734
756
|
}
|
|
735
757
|
};
|
|
736
758
|
}
|
|
759
|
+
const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
|
|
760
|
+
if (hoisted) {
|
|
761
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
762
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
763
|
+
const context = getContext(run, scope, partial);
|
|
764
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
765
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
766
|
+
);
|
|
767
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
768
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
769
|
+
if (index < 0) {
|
|
770
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
for (const element of sets[index]) {
|
|
774
|
+
if (run.isStopped) return;
|
|
775
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
776
|
+
if (index === sets.length - 1) {
|
|
777
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
778
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
779
|
+
}
|
|
780
|
+
visit(index - 1, elementScope);
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
visit(sets.length - 1, scope);
|
|
784
|
+
};
|
|
785
|
+
}
|
|
737
786
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
738
787
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
739
788
|
body(run, boundScope, partial, action, isFixed, onComplete);
|