tla-ts 0.0.3-dev.19c763f → 0.0.3-dev.1a99ab7
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/check.ts +45 -15
- package/src/evaluate.ts +10 -7
- package/src/live-check.ts +2 -1
- package/src/liveness.ts +51 -14
- package/src/standard-modules.ts +2 -7
- package/src/successors.ts +60 -4
- package/src/values.ts +48 -11
package/package.json
CHANGED
package/src/check.ts
CHANGED
|
@@ -446,12 +446,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
446
446
|
config.properties.forEach((name) => {
|
|
447
447
|
const definition = getConfigPredicate("property", name, environment);
|
|
448
448
|
if (!definition) return;
|
|
449
|
-
expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
|
|
449
|
+
expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
|
|
450
450
|
const conjunct = item.expression;
|
|
451
451
|
if (isConstantTrue(item, environment, "property")) return;
|
|
452
452
|
if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
|
|
453
453
|
properties.actionProperties.push({
|
|
454
|
-
name,
|
|
454
|
+
name: item.name,
|
|
455
455
|
box: conjunct.expression,
|
|
456
456
|
action: conjunct.expression.action,
|
|
457
457
|
subscript: conjunct.expression.subscript,
|
|
@@ -463,12 +463,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
|
|
|
463
463
|
conjunct.kind === "Always" &&
|
|
464
464
|
getLevel(conjunct.expression, item.scope, environment) <= 1
|
|
465
465
|
) {
|
|
466
|
-
|
|
466
|
+
// TLC names the invariant []P after P when P is an operator name.
|
|
467
|
+
const expression = conjunct.expression;
|
|
468
|
+
properties.invariants.push({
|
|
469
|
+
name: expression.kind === "Identifier" ? expression.name : item.name,
|
|
470
|
+
expression,
|
|
471
|
+
scope: item.scope,
|
|
472
|
+
});
|
|
467
473
|
return;
|
|
468
474
|
}
|
|
469
475
|
const level = getLevel(conjunct, item.scope, environment);
|
|
470
476
|
if (level < ACTION_LEVEL) {
|
|
471
|
-
properties.initialPredicates.push({
|
|
477
|
+
properties.initialPredicates.push({
|
|
478
|
+
name: item.name,
|
|
479
|
+
expression: conjunct,
|
|
480
|
+
scope: item.scope,
|
|
481
|
+
});
|
|
472
482
|
return;
|
|
473
483
|
}
|
|
474
484
|
if (level === ACTION_LEVEL) {
|
|
@@ -528,7 +538,9 @@ const getMembership = (
|
|
|
528
538
|
const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
|
|
529
539
|
named.flatMap((item) =>
|
|
530
540
|
getConjuncts(item.expression, environment).map((expression) => {
|
|
531
|
-
|
|
541
|
+
// A LET or an argument in the scope can read variables the expression doesn't name.
|
|
542
|
+
const variables =
|
|
543
|
+
item.scope === undefined ? getReadVariables(expression, environment) : undefined;
|
|
532
544
|
return {
|
|
533
545
|
name: item.name,
|
|
534
546
|
expression,
|
|
@@ -1035,7 +1047,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1035
1047
|
const parents: number[] = [];
|
|
1036
1048
|
const actions: (string | undefined)[] = [];
|
|
1037
1049
|
const depths: number[] = [];
|
|
1038
|
-
|
|
1050
|
+
// A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
|
|
1051
|
+
// and its index plus one side by side, probed by the hash.
|
|
1052
|
+
const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
|
|
1039
1053
|
const canonicalizeState = createCanonicalizer();
|
|
1040
1054
|
|
|
1041
1055
|
const toRecord = (state: Value[]): Record<string, Value> =>
|
|
@@ -1195,19 +1209,35 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1195
1209
|
}
|
|
1196
1210
|
return true;
|
|
1197
1211
|
};
|
|
1212
|
+
const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
|
|
1198
1213
|
const findStateIndex = (hash: number, state: Value[]) => {
|
|
1199
|
-
const
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1214
|
+
const mask = seen.slots.length - 1;
|
|
1215
|
+
for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
|
|
1216
|
+
const entry = seen.slots[slot + 1];
|
|
1217
|
+
if (entry === 0) return -1;
|
|
1218
|
+
if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
|
|
1219
|
+
return entry - 1;
|
|
1220
|
+
}
|
|
1203
1221
|
}
|
|
1204
|
-
|
|
1222
|
+
};
|
|
1223
|
+
const insertSeen = (hash: number, entry: number) => {
|
|
1224
|
+
const mask = seen.slots.length - 1;
|
|
1225
|
+
let slot = getSlot(hash);
|
|
1226
|
+
while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
|
|
1227
|
+
seen.slots[slot] = hash;
|
|
1228
|
+
seen.slots[slot + 1] = entry;
|
|
1205
1229
|
};
|
|
1206
1230
|
const markSeen = (hash: number, index: number) => {
|
|
1207
|
-
|
|
1208
|
-
if (
|
|
1209
|
-
|
|
1210
|
-
|
|
1231
|
+
seen.size++;
|
|
1232
|
+
if (seen.size * 4 > seen.slots.length) {
|
|
1233
|
+
const slots = seen.slots;
|
|
1234
|
+
seen.slots = new Int32Array(slots.length * 2);
|
|
1235
|
+
seen.shift--;
|
|
1236
|
+
for (let slot = 0; slot < slots.length; slot += 2) {
|
|
1237
|
+
if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
insertSeen(hash, index + 1);
|
|
1211
1241
|
};
|
|
1212
1242
|
const countDistinctState = (depth: number) => {
|
|
1213
1243
|
runtime.distinctStates = states.length + 1;
|
package/src/evaluate.ts
CHANGED
|
@@ -36,11 +36,11 @@ import {
|
|
|
36
36
|
getIntervalElements,
|
|
37
37
|
getSequenceElements,
|
|
38
38
|
getValueHash,
|
|
39
|
-
isEnumerable,
|
|
40
39
|
isFunctionValue,
|
|
41
40
|
isOverriddenSet,
|
|
42
41
|
isSameValue,
|
|
43
42
|
markLambdaFunction,
|
|
43
|
+
isReducible,
|
|
44
44
|
isSetValue,
|
|
45
45
|
MAXIMUM_INTEGER,
|
|
46
46
|
MINIMUM_INTEGER,
|
|
@@ -1027,7 +1027,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1027
1027
|
|
|
1028
1028
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1029
1029
|
|
|
1030
|
-
const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1030
|
+
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1031
1031
|
const cached = entryEvaluators.get(expression);
|
|
1032
1032
|
if (cached) return cached;
|
|
1033
1033
|
const evaluator =
|
|
@@ -1420,7 +1420,13 @@ const compileFunctionConstruction = (
|
|
|
1420
1420
|
const set = getEvaluator(bound.set, environment);
|
|
1421
1421
|
return (context) => {
|
|
1422
1422
|
const domain = isSingleName ? set(context) : getBoundsDomain(bounds, context);
|
|
1423
|
-
|
|
1423
|
+
// TLC builds the function only when it reduces every domain, and evaluates the body lazily
|
|
1424
|
+
// otherwise.
|
|
1425
|
+
const domains =
|
|
1426
|
+
typeof domain === "object" && domain.kind === "ProductSet" && bounds.length > 1
|
|
1427
|
+
? domain.factors
|
|
1428
|
+
: [domain];
|
|
1429
|
+
if (expression.isRecursive || isKeptLazy || !domains.every(isReducible)) {
|
|
1424
1430
|
const functionValue = createLazyFunction(
|
|
1425
1431
|
domain,
|
|
1426
1432
|
(argument) => body(withScope(context, bindArgument(bounds, context.scope, argument))),
|
|
@@ -1514,10 +1520,7 @@ const compileSetComprehension = (
|
|
|
1514
1520
|
predicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1515
1521
|
"a set filter predicate",
|
|
1516
1522
|
);
|
|
1517
|
-
|
|
1518
|
-
typeof base === "object" &&
|
|
1519
|
-
(base.kind === "Set" || base.kind === "MixedSet" || base.kind === "Interval");
|
|
1520
|
-
if (!isReducible) {
|
|
1523
|
+
if (!isReducible(base)) {
|
|
1521
1524
|
if (isSetValue(base)) return { kind: "FilteredSet", base, test };
|
|
1522
1525
|
throw new TlaEvaluationError(
|
|
1523
1526
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
package/src/live-check.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { TlaEvaluationError } from "./errors";
|
|
|
2
2
|
import { hasRepeatedEffects, type Environment } from "./evaluate";
|
|
3
3
|
import {
|
|
4
4
|
evaluateLive,
|
|
5
|
+
TAUTOLOGY_MESSAGE,
|
|
5
6
|
evaluateOnLasso,
|
|
6
7
|
toLiveExpression,
|
|
7
8
|
type ScopedExpression,
|
|
@@ -121,7 +122,7 @@ export const createLivenessChecker = (
|
|
|
121
122
|
fairness.length === 0 ? violation : createJunction("And", [...fairness, violation]);
|
|
122
123
|
const solutions = getSolutions(formula);
|
|
123
124
|
if (solutions.length === 0) {
|
|
124
|
-
throw new TlaEvaluationError(
|
|
125
|
+
throw new TlaEvaluationError(TAUTOLOGY_MESSAGE);
|
|
125
126
|
}
|
|
126
127
|
const actionIds = new Map<string, number>();
|
|
127
128
|
const checks = solutions.map((solution): Check => ({
|
package/src/liveness.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
2
|
import { TlaConfigError, TlaEvaluationError } from "./errors";
|
|
3
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
describeLocation,
|
|
6
|
+
describeRange,
|
|
5
7
|
bindDefinitions,
|
|
6
8
|
bindOperatorArguments,
|
|
7
9
|
evaluate,
|
|
@@ -36,6 +38,10 @@ export interface ScopedExpression {
|
|
|
36
38
|
scope: Scope | undefined;
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
export interface NamedExpression extends ScopedExpression {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
export interface TemporalProperty extends ScopedExpression {
|
|
40
46
|
name: string;
|
|
41
47
|
}
|
|
@@ -144,21 +150,27 @@ const getDefinitionLevel = (definition: Definition, environment: Environment) =>
|
|
|
144
150
|
return level;
|
|
145
151
|
};
|
|
146
152
|
|
|
153
|
+
export const TAUTOLOGY_MESSAGE = "Temporal formula is a tautology (its negation is unsatisfiable).";
|
|
154
|
+
|
|
155
|
+
// TLC names each part of a property after the operator it expands, or after the location of the
|
|
156
|
+
// conjunct or quantifier body it splits off.
|
|
147
157
|
export const expandTemporal = (
|
|
148
158
|
expression: Expression,
|
|
149
159
|
scope: Scope | undefined,
|
|
150
160
|
environment: Environment,
|
|
151
|
-
|
|
161
|
+
isProperty: boolean,
|
|
162
|
+
name = "",
|
|
152
163
|
expanding = new Set<Definition>(),
|
|
153
|
-
):
|
|
164
|
+
): NamedExpression[] => {
|
|
154
165
|
const expand = (
|
|
155
166
|
child: Expression,
|
|
156
167
|
childScope: Scope | undefined,
|
|
168
|
+
childName = name,
|
|
157
169
|
childExpanding = expanding,
|
|
158
|
-
):
|
|
159
|
-
expandTemporal(child, childScope, environment,
|
|
170
|
+
): NamedExpression[] =>
|
|
171
|
+
expandTemporal(child, childScope, environment, isProperty, childName, childExpanding);
|
|
160
172
|
if (expression.kind === "And") {
|
|
161
|
-
return expression.operands.flatMap((operand) => expand(operand, scope));
|
|
173
|
+
return expression.operands.flatMap((operand) => expand(operand, scope, describeRange(operand)));
|
|
162
174
|
}
|
|
163
175
|
if (expression.kind === "Label") return expand(expression.body, scope);
|
|
164
176
|
if (expression.kind === "Let") {
|
|
@@ -181,33 +193,58 @@ export const expandTemporal = (
|
|
|
181
193
|
if (expanding.has(definition)) {
|
|
182
194
|
throw new TlaConfigError(`The definition of ${expression.name} refers to itself`);
|
|
183
195
|
}
|
|
184
|
-
return expand(
|
|
196
|
+
return expand(
|
|
197
|
+
definition.body,
|
|
198
|
+
undefined,
|
|
199
|
+
expression.name,
|
|
200
|
+
new Set([...expanding, definition]),
|
|
201
|
+
);
|
|
185
202
|
}
|
|
186
203
|
}
|
|
187
204
|
const context = getConstantContext(environment, scope);
|
|
188
|
-
|
|
205
|
+
// A property expands every operator it applies and unrolls every \A over constant sets, but a
|
|
206
|
+
// specification only expands the temporal ones.
|
|
207
|
+
const isExpanded = (child: Expression) =>
|
|
208
|
+
isProperty || getLevel(child, scope, environment) === TEMPORAL_LEVEL;
|
|
209
|
+
if (expression.kind === "Apply" && isExpanded(expression)) {
|
|
189
210
|
const operator = resolveApplyOperator(expression, context);
|
|
190
211
|
const definition = isLocallyResolved(expression)
|
|
191
212
|
? undefined
|
|
192
213
|
: getDefinition(expression.operator, environment);
|
|
193
|
-
|
|
194
|
-
|
|
214
|
+
const isExpandable =
|
|
215
|
+
operator?.kind === "Closure" &&
|
|
216
|
+
operator.params.length === expression.args.length &&
|
|
217
|
+
!definition?.isRecursive;
|
|
218
|
+
if (isExpandable) {
|
|
219
|
+
return expand(
|
|
220
|
+
operator.body,
|
|
221
|
+
bindOperatorArguments(operator, expression.args, context),
|
|
222
|
+
expression.operator,
|
|
223
|
+
);
|
|
195
224
|
}
|
|
196
225
|
}
|
|
197
226
|
const isUnrolled =
|
|
198
|
-
|
|
227
|
+
isProperty &&
|
|
199
228
|
expression.kind === "Forall" &&
|
|
200
|
-
getLevel(expression.body, scope, environment) === TEMPORAL_LEVEL &&
|
|
201
229
|
expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === 0);
|
|
202
230
|
if (isUnrolled) {
|
|
203
|
-
const expanded:
|
|
231
|
+
const expanded: NamedExpression[] = [];
|
|
232
|
+
const bindings = { count: 0 };
|
|
204
233
|
forEachBinding(expression.bounds, context, (boundScope) => {
|
|
205
|
-
|
|
234
|
+
bindings.count++;
|
|
235
|
+
expanded.push(...expand(expression.body, boundScope, describeRange(expression.body)));
|
|
206
236
|
return false;
|
|
207
237
|
});
|
|
238
|
+
// TLC rejects a property that quantifies over no elements before it checks anything.
|
|
239
|
+
if (bindings.count === 0) {
|
|
240
|
+
throw setExitCode(
|
|
241
|
+
new TlaEvaluationError(TAUTOLOGY_MESSAGE),
|
|
242
|
+
EXIT_STATUS.livenessEvaluationFailure,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
208
245
|
return expanded;
|
|
209
246
|
}
|
|
210
|
-
return [{ expression, scope }];
|
|
247
|
+
return [{ expression, scope, name }];
|
|
211
248
|
};
|
|
212
249
|
|
|
213
250
|
const bindLetDefinitions = (
|
package/src/standard-modules.ts
CHANGED
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
isEnumerable,
|
|
41
41
|
isFiniteSet,
|
|
42
42
|
isFunctionValue,
|
|
43
|
+
isReducible,
|
|
43
44
|
isSetValue,
|
|
44
45
|
toFiniteSet,
|
|
45
46
|
unionSorted,
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
createEnumeratedSet,
|
|
48
49
|
EMPTY_SET,
|
|
49
50
|
isEnumeratedSet,
|
|
50
|
-
type
|
|
51
|
+
type ReducibleSet,
|
|
51
52
|
type MixedSetValue,
|
|
52
53
|
type SetOperationValue,
|
|
53
54
|
type SetValue,
|
|
@@ -75,12 +76,6 @@ const unionInTree = (sets: SetValue[], start: number, end: number): SetValue =>
|
|
|
75
76
|
|
|
76
77
|
const SET_OPERATORS: SetOperationValue["operator"][] = ["\\cup", "\\cap", "\\"];
|
|
77
78
|
|
|
78
|
-
type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
79
|
-
|
|
80
|
-
const isReducible = (value: Value): value is ReducibleSet =>
|
|
81
|
-
typeof value === "object" &&
|
|
82
|
-
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
83
|
-
|
|
84
79
|
const isMixedSet = (value: Value) => typeof value === "object" && value.kind === "MixedSet";
|
|
85
80
|
|
|
86
81
|
const getReducibleElements = (set: ReducibleSet) =>
|
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";
|
|
@@ -17,6 +18,7 @@ import {
|
|
|
17
18
|
bindValue,
|
|
18
19
|
compileArgumentScope,
|
|
19
20
|
evaluate,
|
|
21
|
+
getEntryEvaluator,
|
|
20
22
|
expectBoolean,
|
|
21
23
|
forEachBinding,
|
|
22
24
|
getBoundsDomain,
|
|
@@ -28,6 +30,7 @@ import {
|
|
|
28
30
|
resolveGlobalOperator,
|
|
29
31
|
type Context,
|
|
30
32
|
type Environment,
|
|
33
|
+
type Evaluator,
|
|
31
34
|
type Scope,
|
|
32
35
|
type State,
|
|
33
36
|
} from "./evaluate";
|
|
@@ -86,8 +89,7 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
86
89
|
return copy;
|
|
87
90
|
};
|
|
88
91
|
|
|
89
|
-
const holds = (expression: Expression, context: Context) => {
|
|
90
|
-
const value = evaluate(expression, context);
|
|
92
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
91
93
|
if (typeof value === "boolean") return value;
|
|
92
94
|
try {
|
|
93
95
|
return expectBoolean(value, `the action at ${describeLocation(expression)}`);
|
|
@@ -292,12 +294,34 @@ const toConjunct = (
|
|
|
292
294
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
293
295
|
};
|
|
294
296
|
|
|
297
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
298
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
299
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
300
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
301
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
302
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
303
|
+
);
|
|
304
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
305
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
306
|
+
const operands = expression.body.operands;
|
|
307
|
+
const index = operands.findIndex((operand) => {
|
|
308
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
309
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
310
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
311
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
312
|
+
});
|
|
313
|
+
const count = index === -1 ? operands.length : index;
|
|
314
|
+
if (count === 0) return undefined;
|
|
315
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
316
|
+
};
|
|
317
|
+
|
|
295
318
|
const compileConjunction = (
|
|
296
319
|
operands: Expression[],
|
|
297
320
|
environment: Environment,
|
|
298
321
|
isInit: boolean,
|
|
299
322
|
): Runner => {
|
|
300
323
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
324
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
301
325
|
const runFrom = (
|
|
302
326
|
start: number,
|
|
303
327
|
run: Run,
|
|
@@ -320,12 +344,17 @@ const compileConjunction = (
|
|
|
320
344
|
const conjunct = conjuncts[index];
|
|
321
345
|
if (conjunct.kind === "Guard") {
|
|
322
346
|
context ??= getContext(run, scope, current);
|
|
323
|
-
|
|
347
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
348
|
+
conjunct.expression,
|
|
349
|
+
environment,
|
|
350
|
+
));
|
|
351
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
324
352
|
continue;
|
|
325
353
|
}
|
|
326
354
|
if (conjunct.kind === "Assign") {
|
|
327
355
|
context ??= getContext(run, scope, current);
|
|
328
|
-
const
|
|
356
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
357
|
+
const value = evaluator(context);
|
|
329
358
|
const assigned = current[conjunct.index];
|
|
330
359
|
if (assigned === undefined) {
|
|
331
360
|
current = assign(current, conjunct.index, value);
|
|
@@ -734,6 +763,33 @@ const compileAction = (
|
|
|
734
763
|
}
|
|
735
764
|
};
|
|
736
765
|
}
|
|
766
|
+
const hoisted = isInit ? undefined : getHoistedGuards(expression, environment);
|
|
767
|
+
if (hoisted) {
|
|
768
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
769
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
770
|
+
const context = getContext(run, scope, partial);
|
|
771
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
772
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
773
|
+
);
|
|
774
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
775
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
776
|
+
if (index < 0) {
|
|
777
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
for (const element of sets[index]) {
|
|
781
|
+
if (run.isStopped) return;
|
|
782
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
783
|
+
if (index === sets.length - 1) {
|
|
784
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
785
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
786
|
+
}
|
|
787
|
+
visit(index - 1, elementScope);
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
visit(sets.length - 1, scope);
|
|
791
|
+
};
|
|
792
|
+
}
|
|
737
793
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
738
794
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
739
795
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
package/src/values.ts
CHANGED
|
@@ -137,6 +137,13 @@ export const isSetValue = (value: Value) =>
|
|
|
137
137
|
value.kind !== "Function" &&
|
|
138
138
|
value.kind !== "LazyFunction";
|
|
139
139
|
|
|
140
|
+
export type ReducibleSet = SetValue | MixedSetValue | IntervalValue;
|
|
141
|
+
|
|
142
|
+
// TLC reduces a set enumeration or an interval eagerly, and keeps every other set lazy.
|
|
143
|
+
export const isReducible = (value: Value): value is ReducibleSet =>
|
|
144
|
+
typeof value === "object" &&
|
|
145
|
+
(value.kind === "Set" || value.kind === "MixedSet" || value.kind === "Interval");
|
|
146
|
+
|
|
140
147
|
export const isFunctionValue = (value: Value): value is FunctionValue | LazyFunctionValue =>
|
|
141
148
|
typeof value === "object" && (value.kind === "Function" || value.kind === "LazyFunction");
|
|
142
149
|
|
|
@@ -371,7 +378,7 @@ export const compareValues = (left: Value, right: Value): number => {
|
|
|
371
378
|
`Attempted to compare ${describeKind(left)} ${formatValue(left)} with ${describeKind(right)} ${formatValue(right)}`,
|
|
372
379
|
);
|
|
373
380
|
}
|
|
374
|
-
if (isLeftSet) return compareSets(toFiniteSet(left),
|
|
381
|
+
if (isLeftSet) return compareSets(toFiniteSet(left), toComparedSet(right));
|
|
375
382
|
return compareFunctions(toFiniteFunction(left), toFiniteFunction(right));
|
|
376
383
|
}
|
|
377
384
|
if (typeof left === "number" && typeof right === "number") return left - right;
|
|
@@ -864,9 +871,11 @@ export const isEqualLikeTlc = (left: Value, right: Value) => {
|
|
|
864
871
|
}
|
|
865
872
|
};
|
|
866
873
|
|
|
874
|
+
// TLC sorts a set by comparing each element with the ones before it, so the later element converts
|
|
875
|
+
// a lazy set it compares and caches it.
|
|
867
876
|
const isStrictlySorted = (elements: Value[]) => {
|
|
868
877
|
for (let index = 1; index < elements.length; index++) {
|
|
869
|
-
if (compareValues(elements[index
|
|
878
|
+
if (compareValues(elements[index], elements[index - 1]) <= 0) return false;
|
|
870
879
|
}
|
|
871
880
|
return true;
|
|
872
881
|
};
|
|
@@ -1069,13 +1078,16 @@ const isTlcEnumerable = (value: Value) => isSetValue(value) && !isOverriddenSet(
|
|
|
1069
1078
|
const formatUnion = (value: UnionValue, isStrict = false) =>
|
|
1070
1079
|
`UNION(${formatValue(value.base, isStrict)})`;
|
|
1071
1080
|
|
|
1072
|
-
const getUnionSets = (
|
|
1081
|
+
const getUnionSets = (
|
|
1082
|
+
value: UnionValue,
|
|
1083
|
+
getElements = (set: Value) => toFiniteSet(set).elements,
|
|
1084
|
+
) => {
|
|
1073
1085
|
if (!isTlcEnumerable(value.base)) {
|
|
1074
1086
|
throw new TlaEvaluationError(
|
|
1075
1087
|
`Attempted to enumerate the nonenumerable set:\n${formatUnion(value)}`,
|
|
1076
1088
|
);
|
|
1077
1089
|
}
|
|
1078
|
-
return
|
|
1090
|
+
return getElements(value.base);
|
|
1079
1091
|
};
|
|
1080
1092
|
|
|
1081
1093
|
const getUnionSet = (set: Value, index: number, union: UnionValue) => {
|
|
@@ -1111,7 +1123,7 @@ export const isEnumerable = (value: Value): boolean => {
|
|
|
1111
1123
|
case "ProductSet":
|
|
1112
1124
|
return value.factors.every(isEnumerable) || getEmptiness(value) === true;
|
|
1113
1125
|
case "Union":
|
|
1114
|
-
return isEnumerable(value.base) &&
|
|
1126
|
+
return isEnumerable(value.base) && getStreamedElements(value.base).every(isEnumerable);
|
|
1115
1127
|
case "SetOperation":
|
|
1116
1128
|
if (value.operator === "\\cup") return isEnumerable(value.left) && isEnumerable(value.right);
|
|
1117
1129
|
if (value.operator === "\\cap") return isEnumerable(value.left) || isEnumerable(value.right);
|
|
@@ -1318,6 +1330,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1318
1330
|
};
|
|
1319
1331
|
|
|
1320
1332
|
const finiteSets = new WeakMap<ObjectValue, SetValue>();
|
|
1333
|
+
const comparedSets = new WeakMap<ObjectValue, SetValue>();
|
|
1321
1334
|
|
|
1322
1335
|
export const toFiniteSet = (value: Value): SetValue => {
|
|
1323
1336
|
if (typeof value === "object" && value.kind === "Set") return value;
|
|
@@ -1326,6 +1339,13 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1326
1339
|
}
|
|
1327
1340
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1328
1341
|
if (cached) return cached;
|
|
1342
|
+
const set =
|
|
1343
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ?? enumerateFiniteSet(value);
|
|
1344
|
+
if (typeof value === "object") finiteSets.set(value, set);
|
|
1345
|
+
return set;
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
const enumerateFiniteSet = (value: Value) => {
|
|
1329
1349
|
const elements = enumerateSet(value);
|
|
1330
1350
|
const isSorted =
|
|
1331
1351
|
typeof value === "object" &&
|
|
@@ -1336,8 +1356,20 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1336
1356
|
value.kind === "ProductSet" ||
|
|
1337
1357
|
value.kind === "FunctionSet" ||
|
|
1338
1358
|
value.kind === "Subset");
|
|
1339
|
-
|
|
1340
|
-
|
|
1359
|
+
return isSorted ? createSortedSet(elements) : createSet(elements);
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// TLC converts the set a comparison receives as its argument without caching it, so a lazy set
|
|
1363
|
+
// compared that way still streams the sets it combines.
|
|
1364
|
+
const toComparedSet = (value: Value) => {
|
|
1365
|
+
const isStreamed =
|
|
1366
|
+
typeof value === "object" &&
|
|
1367
|
+
(value.kind === "Union" || value.kind === "SetOperation" || value.kind === "FilteredSet");
|
|
1368
|
+
if (!isStreamed || finiteSets.has(value)) return toFiniteSet(value);
|
|
1369
|
+
const compared = comparedSets.get(value);
|
|
1370
|
+
if (compared) return compared;
|
|
1371
|
+
const set = enumerateFiniteSet(value);
|
|
1372
|
+
comparedSets.set(value, set);
|
|
1341
1373
|
return set;
|
|
1342
1374
|
};
|
|
1343
1375
|
|
|
@@ -1345,8 +1377,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1345
1377
|
// so an element that two of them share comes up once for each.
|
|
1346
1378
|
export const getStreamedElements = (value: Value): Value[] => {
|
|
1347
1379
|
if (typeof value !== "object" || finiteSets.has(value)) return toFiniteSet(value).elements;
|
|
1380
|
+
// TLC streams the sets of a UNION it hasn't enumerated, so a set that appears twice there yields
|
|
1381
|
+
// its elements twice.
|
|
1348
1382
|
if (value.kind === "Union") {
|
|
1349
|
-
return getUnionSets(value).flatMap((set, index) =>
|
|
1383
|
+
return getUnionSets(value, getStreamedElements).flatMap((set, index) =>
|
|
1350
1384
|
getStreamedElements(getUnionSet(set, index, value)),
|
|
1351
1385
|
);
|
|
1352
1386
|
}
|
|
@@ -2101,8 +2135,11 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2101
2135
|
return `[${parameters} |-> <expression ${source.body}>]`;
|
|
2102
2136
|
}
|
|
2103
2137
|
const functionValue = toFiniteFunction(value);
|
|
2138
|
+
// TLC normalizes a lambda's domain to print it, but evaluates its body afterward.
|
|
2139
|
+
const formatRange = (inner: Value) =>
|
|
2140
|
+
formatValue(inner, isStrict, isNormalized && value.kind === "Function");
|
|
2104
2141
|
if (functionValue.domain.every((key, index) => key === index + 1)) {
|
|
2105
|
-
return `<<${functionValue.range.map(
|
|
2142
|
+
return `<<${functionValue.range.map(formatRange).join(", ")}>>`;
|
|
2106
2143
|
}
|
|
2107
2144
|
if (functionValue.domain.every((key) => typeof key === "string" && isFieldName(key))) {
|
|
2108
2145
|
const isSorted =
|
|
@@ -2110,9 +2147,9 @@ export const formatValue = (value: Value, isStrict = false, isNormalized = false
|
|
|
2110
2147
|
const pairs = isSorted
|
|
2111
2148
|
? functionValue.domain.map((key, index) => [key, functionValue.range[index]])
|
|
2112
2149
|
: getWrittenPairs(functionValue);
|
|
2113
|
-
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${
|
|
2150
|
+
return `[${pairs.map((pair) => `${String(pair[0])} |-> ${formatRange(pair[1])}`).join(", ")}]`;
|
|
2114
2151
|
}
|
|
2115
|
-
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${
|
|
2152
|
+
return `(${functionValue.domain.map((key, index) => `${format(key)} :> ${formatRange(functionValue.range[index])}`).join(" @@ ")})`;
|
|
2116
2153
|
}
|
|
2117
2154
|
}
|
|
2118
2155
|
};
|