tla-ts 0.0.3-dev.6a30eee → 0.0.3-dev.6ac5593
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/README.md +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +70 -22
- package/dist/index.d.ts +70 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +20 -18
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +41 -15
- package/dist/transfer.js +41 -15
- package/package.json +1 -1
- package/src/ast.ts +121 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +480 -106
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +410 -130
- package/src/errors.ts +104 -3
- package/src/evaluate.ts +402 -182
- package/src/exit-codes.ts +21 -14
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +17 -16
- package/src/levels.ts +185 -44
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +118 -24
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +5 -2
- package/src/parser.ts +172 -53
- package/src/resolve.ts +1378 -420
- package/src/run-cli.ts +106 -18
- package/src/runtime.ts +2 -0
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +177 -37
- package/src/symmetry.ts +109 -41
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +242 -61
package/src/successors.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type QuantifierExpression,
|
|
8
|
+
} from "./ast";
|
|
2
9
|
import { getReadVariables } from "./dependencies";
|
|
3
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
TlaEvaluationError,
|
|
12
|
+
TlaIllegalStateError,
|
|
13
|
+
TlaIncompleteStateError,
|
|
14
|
+
TlaUnassignedError,
|
|
15
|
+
} from "./errors";
|
|
4
16
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
5
17
|
import {
|
|
6
18
|
describeLocation,
|
|
@@ -11,19 +23,19 @@ import {
|
|
|
11
23
|
bindValue,
|
|
12
24
|
compileArgumentScope,
|
|
13
25
|
evaluate,
|
|
26
|
+
getEntryEvaluator,
|
|
14
27
|
expectBoolean,
|
|
15
28
|
forEachBinding,
|
|
16
29
|
getBoundsDomain,
|
|
17
|
-
getChildren,
|
|
18
30
|
getDefinition,
|
|
19
31
|
getEnumeratedElements,
|
|
20
32
|
getReevaluatedDefinitions,
|
|
21
33
|
lookupScope,
|
|
22
34
|
resolveApplyOperator,
|
|
23
35
|
resolveGlobalOperator,
|
|
24
|
-
isLocallyResolved,
|
|
25
36
|
type Context,
|
|
26
37
|
type Environment,
|
|
38
|
+
type Evaluator,
|
|
27
39
|
type Scope,
|
|
28
40
|
type State,
|
|
29
41
|
} from "./evaluate";
|
|
@@ -46,6 +58,7 @@ export interface Successor {
|
|
|
46
58
|
interface Run {
|
|
47
59
|
environment: Environment;
|
|
48
60
|
isInit: boolean;
|
|
61
|
+
isEnabled: boolean;
|
|
49
62
|
current: Value[] | undefined;
|
|
50
63
|
isStopped: boolean;
|
|
51
64
|
}
|
|
@@ -82,25 +95,54 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
82
95
|
return copy;
|
|
83
96
|
};
|
|
84
97
|
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
// TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
|
|
99
|
+
const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
|
|
87
100
|
if (typeof value === "boolean") return value;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
101
|
+
throw new TlaEvaluationError(
|
|
102
|
+
`In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
107
|
+
// TLC rejects a number in the next-state relation when it splits the relation into actions.
|
|
108
|
+
if (context.next !== undefined && expression.kind === "Number") {
|
|
109
|
+
throw setExitCode(
|
|
110
|
+
new TlaEvaluationError(
|
|
111
|
+
`The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
|
|
112
|
+
),
|
|
113
|
+
EXIT_STATUS.error,
|
|
114
|
+
);
|
|
93
115
|
}
|
|
116
|
+
return expectFormula(expression, value, context.next === undefined);
|
|
94
117
|
};
|
|
95
118
|
|
|
96
119
|
const isAssignmentOperator = (operator: string) =>
|
|
97
120
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
98
121
|
|
|
122
|
+
const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
|
|
123
|
+
|
|
124
|
+
const getRunners = (environment: Environment, isInit: boolean) => {
|
|
125
|
+
if (!environment.trace) return isInit ? initRunners : nextRunners;
|
|
126
|
+
const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
|
|
127
|
+
tracedRunners.set(environment, runners);
|
|
128
|
+
return runners[isInit ? 0 : 1];
|
|
129
|
+
};
|
|
130
|
+
|
|
99
131
|
const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
|
|
100
|
-
const runners = isInit
|
|
132
|
+
const runners = getRunners(environment, isInit);
|
|
101
133
|
const cached = runners.get(expression);
|
|
102
134
|
if (cached) return cached;
|
|
103
|
-
const
|
|
135
|
+
const compiled = compileAction(expression, environment, isInit);
|
|
136
|
+
const trace = environment.trace;
|
|
137
|
+
// TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
|
|
138
|
+
const runner: Runner = trace
|
|
139
|
+
? (run, scope, partial, action, isFixed, onComplete) => {
|
|
140
|
+
const depth = trace.length;
|
|
141
|
+
trace.push(expression);
|
|
142
|
+
compiled(run, scope, partial, action, isFixed, onComplete);
|
|
143
|
+
trace.length = depth;
|
|
144
|
+
}
|
|
145
|
+
: compiled;
|
|
104
146
|
runners.set(expression, runner);
|
|
105
147
|
return runner;
|
|
106
148
|
};
|
|
@@ -184,6 +226,23 @@ const getUnchangedIndices = (
|
|
|
184
226
|
return getUnchangedIndices(definition.body, environment, visited);
|
|
185
227
|
};
|
|
186
228
|
|
|
229
|
+
// TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
|
|
230
|
+
const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
|
|
231
|
+
if (expression.kind === "Tuple") {
|
|
232
|
+
return [
|
|
233
|
+
expression,
|
|
234
|
+
...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
const definition =
|
|
238
|
+
expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
|
|
239
|
+
? getZeroArityBody(expression.name, environment)
|
|
240
|
+
: undefined;
|
|
241
|
+
return definition
|
|
242
|
+
? [expression, ...getUnchangedFrames(definition.body, environment)]
|
|
243
|
+
: [expression];
|
|
244
|
+
};
|
|
245
|
+
|
|
187
246
|
const ACTION_KINDS = new Set([
|
|
188
247
|
"Label",
|
|
189
248
|
"And",
|
|
@@ -223,7 +282,7 @@ const NO_NAMES: ReadonlySet<string> = new Set();
|
|
|
223
282
|
|
|
224
283
|
// The local names an expression reads from enclosing scopes, or undefined when it applies a local
|
|
225
284
|
// operator, whose body may assign variables.
|
|
226
|
-
const getReadLocals = (
|
|
285
|
+
export const getReadLocals = (
|
|
227
286
|
expression: Expression,
|
|
228
287
|
bound: ReadonlySet<string>,
|
|
229
288
|
): string[] | undefined => {
|
|
@@ -288,12 +347,35 @@ const toConjunct = (
|
|
|
288
347
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
289
348
|
};
|
|
290
349
|
|
|
350
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
351
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
352
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
353
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
354
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
355
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
356
|
+
);
|
|
357
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
358
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
359
|
+
const operands = expression.body.operands;
|
|
360
|
+
const index = operands.findIndex((operand) => {
|
|
361
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
362
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
363
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
364
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
365
|
+
});
|
|
366
|
+
const count = index === -1 ? operands.length : index;
|
|
367
|
+
if (count === 0) return undefined;
|
|
368
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
369
|
+
};
|
|
370
|
+
|
|
291
371
|
const compileConjunction = (
|
|
292
372
|
operands: Expression[],
|
|
293
373
|
environment: Environment,
|
|
294
374
|
isInit: boolean,
|
|
295
375
|
): Runner => {
|
|
296
376
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
377
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
378
|
+
const trace = environment.trace;
|
|
297
379
|
const runFrom = (
|
|
298
380
|
start: number,
|
|
299
381
|
run: Run,
|
|
@@ -314,14 +396,25 @@ const compileConjunction = (
|
|
|
314
396
|
return;
|
|
315
397
|
}
|
|
316
398
|
const conjunct = conjuncts[index];
|
|
399
|
+
if (trace) {
|
|
400
|
+
trace.push(conjunct.expression);
|
|
401
|
+
if (conjunct.expression.kind === "Unchanged") {
|
|
402
|
+
trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
317
405
|
if (conjunct.kind === "Guard") {
|
|
318
406
|
context ??= getContext(run, scope, current);
|
|
319
|
-
|
|
407
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
408
|
+
conjunct.expression,
|
|
409
|
+
environment,
|
|
410
|
+
));
|
|
411
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
320
412
|
continue;
|
|
321
413
|
}
|
|
322
414
|
if (conjunct.kind === "Assign") {
|
|
323
415
|
context ??= getContext(run, scope, current);
|
|
324
|
-
const
|
|
416
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
417
|
+
const value = evaluator(context);
|
|
325
418
|
const assigned = current[conjunct.index];
|
|
326
419
|
if (assigned === undefined) {
|
|
327
420
|
current = assign(current, conjunct.index, value);
|
|
@@ -358,10 +451,7 @@ const compileConjunction = (
|
|
|
358
451
|
};
|
|
359
452
|
|
|
360
453
|
const holdsInInit = (expression: Expression, context: Context) =>
|
|
361
|
-
|
|
362
|
-
evaluate(expression, context),
|
|
363
|
-
`the initial predicate at ${describeLocation(expression)}`,
|
|
364
|
-
);
|
|
454
|
+
expectFormula(expression, evaluate(expression, context), true);
|
|
365
455
|
|
|
366
456
|
const compileGuard =
|
|
367
457
|
(expression: Expression): Runner =>
|
|
@@ -386,7 +476,7 @@ const compileMembership = (
|
|
|
386
476
|
expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
|
|
387
477
|
if (!isEnumerable(value)) {
|
|
388
478
|
throw new TlaEvaluationError(
|
|
389
|
-
`In computing next states, the right side of \\
|
|
479
|
+
`In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
|
|
390
480
|
);
|
|
391
481
|
}
|
|
392
482
|
const elements = getStreamedElements(value);
|
|
@@ -730,6 +820,34 @@ const compileAction = (
|
|
|
730
820
|
}
|
|
731
821
|
};
|
|
732
822
|
}
|
|
823
|
+
const hoisted =
|
|
824
|
+
isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
|
|
825
|
+
if (hoisted) {
|
|
826
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
827
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
828
|
+
const context = getContext(run, scope, partial);
|
|
829
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
830
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
831
|
+
);
|
|
832
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
833
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
834
|
+
if (index < 0) {
|
|
835
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
836
|
+
return;
|
|
837
|
+
}
|
|
838
|
+
for (const element of sets[index]) {
|
|
839
|
+
if (run.isStopped) return;
|
|
840
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
841
|
+
if (index === sets.length - 1) {
|
|
842
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
843
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
844
|
+
}
|
|
845
|
+
visit(index - 1, elementScope);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
visit(sets.length - 1, scope);
|
|
849
|
+
};
|
|
850
|
+
}
|
|
733
851
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
734
852
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
735
853
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
@@ -806,7 +924,7 @@ const compileAction = (
|
|
|
806
924
|
const body = arm?.body ?? other;
|
|
807
925
|
if (!body)
|
|
808
926
|
throw new TlaEvaluationError(
|
|
809
|
-
|
|
927
|
+
`In computing ${run.isInit ? "initial" : "next"} states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
810
928
|
);
|
|
811
929
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
812
930
|
};
|
|
@@ -838,8 +956,17 @@ const compileAction = (
|
|
|
838
956
|
const unchanged = getUnchangedWrapper(expression.subscript);
|
|
839
957
|
return (run, scope, partial, action, isFixed, onComplete) =>
|
|
840
958
|
body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
|
|
841
|
-
|
|
842
|
-
|
|
959
|
+
// ENABLED evaluates the subscript on the successor that the action specifies. TLC names
|
|
960
|
+
// a variable that the action leaves without a value.
|
|
961
|
+
try {
|
|
962
|
+
if (!holds(unchanged, getContext(run, scope, nextPartial)))
|
|
963
|
+
onComplete(nextPartial, nextAction);
|
|
964
|
+
} catch (error) {
|
|
965
|
+
if (!run.isEnabled || !(error instanceof TlaUnassignedError)) throw error;
|
|
966
|
+
throw new TlaEvaluationError(
|
|
967
|
+
`The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
843
970
|
});
|
|
844
971
|
}
|
|
845
972
|
case "BoxAction":
|
|
@@ -916,7 +1043,13 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
916
1043
|
);
|
|
917
1044
|
}
|
|
918
1045
|
const environment = context.environment;
|
|
919
|
-
const run: Run = {
|
|
1046
|
+
const run: Run = {
|
|
1047
|
+
environment,
|
|
1048
|
+
isInit: false,
|
|
1049
|
+
isEnabled: true,
|
|
1050
|
+
current: state,
|
|
1051
|
+
isStopped: false,
|
|
1052
|
+
};
|
|
920
1053
|
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
921
1054
|
getRunner(expression, environment, false)(
|
|
922
1055
|
run,
|
|
@@ -937,7 +1070,13 @@ export const forEachInitialState = (
|
|
|
937
1070
|
onState: (state: Value[]) => boolean,
|
|
938
1071
|
scope?: Scope,
|
|
939
1072
|
) => {
|
|
940
|
-
const run: Run = {
|
|
1073
|
+
const run: Run = {
|
|
1074
|
+
environment,
|
|
1075
|
+
isInit: true,
|
|
1076
|
+
isEnabled: false,
|
|
1077
|
+
current: undefined,
|
|
1078
|
+
isStopped: false,
|
|
1079
|
+
};
|
|
941
1080
|
getRunner(expression, environment, true)(
|
|
942
1081
|
run,
|
|
943
1082
|
scope,
|
|
@@ -947,14 +1086,7 @@ export const forEachInitialState = (
|
|
|
947
1086
|
(partial) => {
|
|
948
1087
|
if (run.isStopped) return;
|
|
949
1088
|
if (!isComplete(partial)) {
|
|
950
|
-
|
|
951
|
-
// an evaluation failure.
|
|
952
|
-
throw setExitCode(
|
|
953
|
-
new TlaEvaluationError(
|
|
954
|
-
`The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
|
|
955
|
-
),
|
|
956
|
-
EXIT_STATUS.error,
|
|
957
|
-
);
|
|
1089
|
+
throw setExitCode(new TlaIllegalStateError(partial), EXIT_STATUS.error);
|
|
958
1090
|
}
|
|
959
1091
|
run.isStopped = onState(partial);
|
|
960
1092
|
},
|
|
@@ -986,7 +1118,13 @@ export const forEachSuccessor = (
|
|
|
986
1118
|
onSuccessor: (successor: Successor) => boolean,
|
|
987
1119
|
scope?: Scope,
|
|
988
1120
|
) => {
|
|
989
|
-
const run: Run = {
|
|
1121
|
+
const run: Run = {
|
|
1122
|
+
environment,
|
|
1123
|
+
isInit: false,
|
|
1124
|
+
isEnabled: false,
|
|
1125
|
+
current: state,
|
|
1126
|
+
isStopped: false,
|
|
1127
|
+
};
|
|
990
1128
|
const counter = { successors: 0 };
|
|
991
1129
|
getRunner(expression, environment, false)(
|
|
992
1130
|
run,
|
|
@@ -997,8 +1135,10 @@ export const forEachSuccessor = (
|
|
|
997
1135
|
(partial, action) => {
|
|
998
1136
|
if (run.isStopped) return;
|
|
999
1137
|
if (!isComplete(partial)) {
|
|
1000
|
-
throw new
|
|
1001
|
-
|
|
1138
|
+
throw new TlaIncompleteStateError(
|
|
1139
|
+
partial,
|
|
1140
|
+
action,
|
|
1141
|
+
getMissingVariables(partial, environment).sort(),
|
|
1002
1142
|
);
|
|
1003
1143
|
}
|
|
1004
1144
|
counter.successors++;
|
package/src/symmetry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { TlaConfigError } from "./errors";
|
|
1
|
+
import { TlaConfigError, TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
2
2
|
import { evaluate, type Environment } from "./evaluate";
|
|
3
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
compareValues,
|
|
5
6
|
createFunction,
|
|
@@ -30,6 +31,20 @@ interface Permutation {
|
|
|
30
31
|
cacheLimit: number;
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
// The images of one value under every permutation, the identity first, and how each image compares
|
|
35
|
+
// with the image of each permutation the search has held as the smallest.
|
|
36
|
+
interface Orbit {
|
|
37
|
+
images: Value[];
|
|
38
|
+
comparisons: (Int8Array | undefined)[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface SymmetryGroup {
|
|
42
|
+
permutations: Permutation[];
|
|
43
|
+
orbits: WeakMap<ObjectValue, Orbit>;
|
|
44
|
+
orbitCount: number;
|
|
45
|
+
orbitLimit: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
33
48
|
const getPermutationKey = (permutation: Images) =>
|
|
34
49
|
[...permutation]
|
|
35
50
|
.map(([name, image]) => `${name}>${image.name}`)
|
|
@@ -47,9 +62,13 @@ const composePermutations = (first: Images, second: Images) => {
|
|
|
47
62
|
return composed;
|
|
48
63
|
};
|
|
49
64
|
|
|
50
|
-
|
|
65
|
+
// TLC checks the symmetry set outside any phase that reports an error itself.
|
|
66
|
+
const createSymmetryError = (message: string) =>
|
|
67
|
+
setExitCode(new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${message}`), EXIT_STATUS.error);
|
|
68
|
+
|
|
69
|
+
const toImages = (value: Value) => {
|
|
51
70
|
if (!isFunctionValue(value)) {
|
|
52
|
-
throw
|
|
71
|
+
throw createSymmetryError("The symmetry values must be function records.");
|
|
53
72
|
}
|
|
54
73
|
const functionValue = toFiniteFunction(value);
|
|
55
74
|
const permutation: Images = new Map();
|
|
@@ -63,10 +82,18 @@ const toImages = (value: Value, name: string) => {
|
|
|
63
82
|
return permutation;
|
|
64
83
|
};
|
|
65
84
|
|
|
66
|
-
export const getSymmetryGroup = (
|
|
85
|
+
export const getSymmetryGroup = (
|
|
86
|
+
name: string,
|
|
87
|
+
environment: Environment,
|
|
88
|
+
): SymmetryGroup | undefined => {
|
|
67
89
|
const definition = environment.definitions.get(name);
|
|
90
|
+
if (definition === undefined && !environment.constants.has(name)) {
|
|
91
|
+
throw new TlaConfigError(
|
|
92
|
+
`The symmetry function ${name} specified in the configuration file\nis not defined in the specification.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
68
95
|
if (definition?.kind !== "Operator" || definition.params.length > 0) {
|
|
69
|
-
throw
|
|
96
|
+
throw createSymmetryError(`The symmetry function ${name} must specify a set of permutations.`);
|
|
70
97
|
}
|
|
71
98
|
const value = evaluate(definition.body, {
|
|
72
99
|
environment,
|
|
@@ -75,12 +102,12 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
75
102
|
next: undefined,
|
|
76
103
|
});
|
|
77
104
|
if (!isSetValue(value) || !isEnumerable(value)) {
|
|
78
|
-
throw
|
|
105
|
+
throw createSymmetryError("The symmetry operator must specify a set of functions.");
|
|
79
106
|
}
|
|
80
|
-
const permutations = toFiniteSet(value).elements.map(
|
|
107
|
+
const permutations = toFiniteSet(value).elements.map(toImages);
|
|
81
108
|
const isDisabled =
|
|
82
109
|
definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
|
|
83
|
-
if (isDisabled) return
|
|
110
|
+
if (isDisabled) return undefined;
|
|
84
111
|
const group = new Map<string, Images>();
|
|
85
112
|
permutations.forEach((permutation) => {
|
|
86
113
|
if (permutation.size > 0) group.set(getPermutationKey(permutation), permutation);
|
|
@@ -99,13 +126,19 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
99
126
|
);
|
|
100
127
|
frontier.start = end;
|
|
101
128
|
}
|
|
129
|
+
if (group.size === 0) return undefined;
|
|
102
130
|
const cacheLimit = Math.max(256, Math.floor(CACHE_LIMIT / group.size));
|
|
103
|
-
return
|
|
104
|
-
images
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
return {
|
|
132
|
+
permutations: [...group.values()].map((images) => ({
|
|
133
|
+
images,
|
|
134
|
+
cache: new WeakMap<ObjectValue, Value>(),
|
|
135
|
+
cacheSize: 0,
|
|
136
|
+
cacheLimit,
|
|
137
|
+
})),
|
|
138
|
+
orbits: new WeakMap(),
|
|
139
|
+
orbitCount: 0,
|
|
140
|
+
orbitLimit: cacheLimit,
|
|
141
|
+
};
|
|
109
142
|
};
|
|
110
143
|
|
|
111
144
|
const permuteElements = (elements: Value[], permutation: Permutation) => {
|
|
@@ -208,41 +241,76 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
|
208
241
|
return elements ? createSet(elements) : set;
|
|
209
242
|
};
|
|
210
243
|
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
244
|
+
const getOrbit = (value: ObjectValue, group: SymmetryGroup) => {
|
|
245
|
+
const cached = group.orbits.get(value);
|
|
246
|
+
if (cached !== undefined) return cached;
|
|
247
|
+
if (group.orbitCount >= group.orbitLimit) {
|
|
248
|
+
group.orbits = new WeakMap();
|
|
249
|
+
group.orbitCount = 0;
|
|
214
250
|
}
|
|
215
|
-
|
|
251
|
+
const orbit: Orbit = { images: [value], comparisons: [] };
|
|
252
|
+
group.orbits.set(value, orbit);
|
|
253
|
+
group.orbitCount++;
|
|
254
|
+
return orbit;
|
|
216
255
|
};
|
|
217
256
|
|
|
218
|
-
const
|
|
257
|
+
const getImage = (orbit: Orbit, permutation: number, group: SymmetryGroup) =>
|
|
258
|
+
(orbit.images[permutation] ??= permuteValue(
|
|
259
|
+
orbit.images[0],
|
|
260
|
+
group.permutations[permutation - 1],
|
|
261
|
+
));
|
|
262
|
+
|
|
263
|
+
// Two images of a value compare the same way every time, and comparing them again changes nothing,
|
|
264
|
+
// so an orbit compares each pair once.
|
|
265
|
+
const compareImages = (
|
|
266
|
+
value: ObjectValue,
|
|
267
|
+
permutation: number,
|
|
268
|
+
minimum: number,
|
|
269
|
+
group: SymmetryGroup,
|
|
270
|
+
) => {
|
|
271
|
+
const orbit = getOrbit(value, group);
|
|
272
|
+
const comparisons = (orbit.comparisons[minimum] ??= new Int8Array(group.permutations.length + 1));
|
|
273
|
+
if (comparisons[permutation] === 0) {
|
|
274
|
+
const comparison = compareValues(
|
|
275
|
+
getImage(orbit, permutation, group),
|
|
276
|
+
getImage(orbit, minimum, group),
|
|
277
|
+
);
|
|
278
|
+
comparisons[permutation] = Math.sign(comparison) + 2;
|
|
279
|
+
}
|
|
280
|
+
return comparisons[permutation] - 2;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const isSmallerImage = (
|
|
219
284
|
state: Value[],
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
285
|
+
permutation: number,
|
|
286
|
+
minimum: number,
|
|
287
|
+
group: SymmetryGroup,
|
|
288
|
+
order: number[],
|
|
223
289
|
) => {
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (comparison > 0) return undefined;
|
|
230
|
-
if (!order) permuted.push(value);
|
|
231
|
-
if (comparison < 0) {
|
|
232
|
-
return order
|
|
233
|
-
? state.map((item) => permuteValue(item, permutation))
|
|
234
|
-
: permuteRemaining(state, permuted, permutation);
|
|
235
|
-
}
|
|
290
|
+
for (const index of order) {
|
|
291
|
+
const value = state[index];
|
|
292
|
+
const comparison =
|
|
293
|
+
typeof value === "object" ? compareImages(value, permutation, minimum, group) : 0;
|
|
294
|
+
if (comparison !== 0) return comparison < 0;
|
|
236
295
|
}
|
|
237
|
-
return
|
|
296
|
+
return false;
|
|
238
297
|
};
|
|
239
298
|
|
|
299
|
+
// TLC keeps the smallest image of the state in its variable order, replacing it only with a
|
|
300
|
+
// strictly smaller one.
|
|
240
301
|
export const getRepresentative = (
|
|
241
302
|
state: Value[],
|
|
242
|
-
group:
|
|
243
|
-
order
|
|
244
|
-
): Value[] =>
|
|
245
|
-
group.reduce(
|
|
246
|
-
(
|
|
247
|
-
|
|
303
|
+
group: SymmetryGroup,
|
|
304
|
+
order: number[],
|
|
305
|
+
): Value[] => {
|
|
306
|
+
const minimum = group.permutations.reduce(
|
|
307
|
+
(smallest, _, index) =>
|
|
308
|
+
isSmallerImage(state, index + 1, smallest, group, order) ? index + 1 : smallest,
|
|
309
|
+
0,
|
|
248
310
|
);
|
|
311
|
+
return minimum === 0
|
|
312
|
+
? state
|
|
313
|
+
: state.map((value) =>
|
|
314
|
+
typeof value === "object" ? getImage(getOrbit(value, group), minimum, group) : value,
|
|
315
|
+
);
|
|
316
|
+
};
|