tla-ts 0.0.3-dev.b74f28c → 0.0.3-dev.bb5b28b
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 +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +53 -22
- package/dist/index.d.ts +53 -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 +19 -16
- package/dist/run-cli.js +20 -17
- package/dist/transfer.cjs +37 -16
- package/dist/transfer.js +37 -16
- package/package.json +1 -1
- package/src/ast.ts +93 -0
- package/src/check.ts +496 -98
- 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 +116 -28
- package/src/errors.ts +76 -3
- package/src/evaluate.ts +251 -156
- package/src/exit-codes.ts +39 -42
- package/src/instances.ts +16 -15
- package/src/levels.ts +62 -34
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +7 -3
- package/src/parallel.ts +20 -4
- package/src/parser.ts +33 -11
- package/src/resolve.ts +122 -44
- package/src/run-cli.ts +99 -19
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +9 -8
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +146 -35
- package/src/symmetry.ts +16 -6
- 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 +25 -7
- 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 +167 -52
package/src/runtime.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface CheckerRuntime {
|
|
|
6
6
|
getBehavior: (state: Value[]) => Value[][];
|
|
7
7
|
getSpecification: () => Value;
|
|
8
8
|
counterExample: Value | undefined;
|
|
9
|
-
setLivenessSource: (index: number) => void;
|
|
9
|
+
setLivenessSource: (index: number | undefined) => void;
|
|
10
10
|
getCurrentState: () => Value[] | undefined;
|
|
11
11
|
getFingerprint: (state: Value[]) => bigint;
|
|
12
12
|
getRandom: () => JavaRandom;
|
|
@@ -24,6 +24,7 @@ export interface CheckerRuntime {
|
|
|
24
24
|
properties: Map<string, string>;
|
|
25
25
|
possibleCounts: Map<string, number>;
|
|
26
26
|
isStopped: boolean;
|
|
27
|
+
isStarted: boolean;
|
|
27
28
|
checkDeadlock: boolean;
|
|
28
29
|
startedAt: number;
|
|
29
30
|
}
|
|
@@ -75,6 +76,7 @@ export const createRuntime = (checkDeadlock: boolean, seed = 0n): CheckerRuntime
|
|
|
75
76
|
properties: new Map(),
|
|
76
77
|
possibleCounts: new Map(),
|
|
77
78
|
isStopped: false,
|
|
79
|
+
isStarted: false,
|
|
78
80
|
checkDeadlock,
|
|
79
81
|
startedAt: Date.now(),
|
|
80
82
|
};
|
package/src/spec-description.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
isLocallyResolved,
|
|
3
|
+
type Definition,
|
|
4
|
+
type Expression,
|
|
5
|
+
type SourceLocation,
|
|
6
|
+
type SourceRange,
|
|
7
|
+
} from "./ast";
|
|
2
8
|
import type { ModelConfig } from "./config";
|
|
3
9
|
import type { InstanceAction } from "./environment";
|
|
4
|
-
import { TlaError } from "./errors";
|
|
10
|
+
import { formatRange, TlaError } from "./errors";
|
|
5
11
|
import {
|
|
6
12
|
bindDefinitions,
|
|
7
13
|
bindOperatorArguments,
|
|
8
14
|
evaluate,
|
|
9
15
|
forEachBinding,
|
|
10
|
-
formatRange,
|
|
11
16
|
getDefinition,
|
|
12
17
|
isConstantLevel,
|
|
13
|
-
isLocallyResolved,
|
|
14
18
|
lookupScope,
|
|
15
19
|
resolveGlobalOperator,
|
|
16
20
|
type Closure,
|
|
@@ -464,10 +468,17 @@ export const getNextActions = (
|
|
|
464
468
|
instanceActions: Map<string, InstanceAction>,
|
|
465
469
|
) => describe(config, environment, instanceActions).actions;
|
|
466
470
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
}
|
|
471
|
+
const formatParameters = (action: SpecAction) =>
|
|
472
|
+
action.parameters.length === 0
|
|
473
|
+
? ""
|
|
474
|
+
: `(${action.parameters.map((parameter) => formatValue(parameter[1])).join(",")})`;
|
|
475
|
+
|
|
476
|
+
export const getActionLabel = (action: SpecAction) =>
|
|
477
|
+
`${action.name === UNNAMED_ACTION ? "Action" : action.name}${formatParameters(action)} ${formatRange(action.location.range ?? BUILTIN_RANGE)}`;
|
|
478
|
+
|
|
479
|
+
// TLC's Action.getInvocationSignature, which names the action and the values of its parameters.
|
|
480
|
+
export const getActionSignature = (action: SpecAction) =>
|
|
481
|
+
`${action.name}${formatParameters(action)}`;
|
|
471
482
|
|
|
472
483
|
export const getSpecificationValue = (
|
|
473
484
|
config: ModelConfig,
|
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) =>
|
|
@@ -150,7 +145,13 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
150
145
|
: operator === "\\cap"
|
|
151
146
|
? intersectSorted
|
|
152
147
|
: differenceSorted;
|
|
153
|
-
|
|
148
|
+
// Merging compares elements across the sets. Where that fails, TLC's element-by-element
|
|
149
|
+
// membership test fails or succeeds on its own.
|
|
150
|
+
try {
|
|
151
|
+
return merge(toFiniteSet(left), toFiniteSet(right));
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
154
|
+
}
|
|
154
155
|
}
|
|
155
156
|
if (isReducible(left)) return combineReducible(operator, left, right);
|
|
156
157
|
if (operator !== "\\" && isReducible(right)) return combineReducible(operator, right, left);
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// A port of TLC's -dump writers, tlc2.util.StateWriter and tlc2.util.DotStateWriter.
|
|
2
|
+
|
|
3
|
+
export interface StateDump {
|
|
4
|
+
path: string;
|
|
5
|
+
dot?: boolean;
|
|
6
|
+
colorize?: boolean;
|
|
7
|
+
actionLabels?: boolean;
|
|
8
|
+
snapshot?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface GraphState {
|
|
12
|
+
fingerprint: bigint;
|
|
13
|
+
level: number;
|
|
14
|
+
// The state as TLC prints it after evaluating the alias, and as it prints it without the alias.
|
|
15
|
+
getLabel: () => string;
|
|
16
|
+
getText: () => string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface GraphAction {
|
|
20
|
+
name: string;
|
|
21
|
+
signature: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface StateWriter {
|
|
25
|
+
writeInitialState: (state: GraphState) => void;
|
|
26
|
+
writeSuccessor: (
|
|
27
|
+
state: GraphState,
|
|
28
|
+
successor: GraphState,
|
|
29
|
+
isSeen: boolean,
|
|
30
|
+
action: GraphAction | undefined,
|
|
31
|
+
) => void;
|
|
32
|
+
close: () => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type WriteFile = ((path: string, content: string) => void) | undefined;
|
|
36
|
+
|
|
37
|
+
const COLOR_SCHEME = "paired12";
|
|
38
|
+
const LOAD_FACTOR = 0.75;
|
|
39
|
+
const INITIAL_CAPACITY = 16;
|
|
40
|
+
|
|
41
|
+
const getCapacity = (size: number, capacity = INITIAL_CAPACITY): number =>
|
|
42
|
+
size <= capacity * LOAD_FACTOR ? capacity : getCapacity(size, capacity * 2);
|
|
43
|
+
|
|
44
|
+
// The order in which a java.util.HashMap iterates over keys put in this order: by bucket, and in
|
|
45
|
+
// insertion order within a bucket. A bucket only becomes a tree once 8 keys collide in it.
|
|
46
|
+
const getJavaHashOrder = <Key>(keys: Key[], getHash: (key: Key) => number) => {
|
|
47
|
+
const mask = getCapacity(keys.length) - 1;
|
|
48
|
+
const getBucket = (key: Key) => {
|
|
49
|
+
const hash = getHash(key);
|
|
50
|
+
return (hash ^ (hash >>> 16)) & mask;
|
|
51
|
+
};
|
|
52
|
+
return keys
|
|
53
|
+
.map((key, position) => ({ key, position, bucket: getBucket(key) }))
|
|
54
|
+
.sort((left, right) => left.bucket - right.bucket || left.position - right.position)
|
|
55
|
+
.map((entry) => entry.key);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const getLongHash = (value: bigint) =>
|
|
59
|
+
Number(BigInt.asIntN(32, value ^ (BigInt.asUintN(64, value) >> 32n)));
|
|
60
|
+
|
|
61
|
+
const getStringHash = (text: string) =>
|
|
62
|
+
[...Array(text.length).keys()].reduce(
|
|
63
|
+
(hash, index) => (Math.imul(31, hash) + text.charCodeAt(index)) | 0,
|
|
64
|
+
0,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const escape = (text: string) => text.replaceAll("\\", "\\\\").replaceAll('"', '\\"').trim();
|
|
68
|
+
|
|
69
|
+
const toDot = (text: string) => escape(text).replaceAll("\n", "\\n");
|
|
70
|
+
|
|
71
|
+
const createPlainWriter = (dump: StateDump, writeFile: WriteFile): StateWriter => {
|
|
72
|
+
const states: string[] = [];
|
|
73
|
+
const writeState = (state: GraphState) =>
|
|
74
|
+
states.push(`State ${states.length + 1}:\n${state.getText()}\n`);
|
|
75
|
+
return {
|
|
76
|
+
writeInitialState: writeState,
|
|
77
|
+
writeSuccessor: (_state, successor, isSeen) => {
|
|
78
|
+
if (!isSeen) writeState(successor);
|
|
79
|
+
},
|
|
80
|
+
close: () => writeFile?.(dump.path, states.join("")),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const createDotWriter = (dump: StateDump, writeFile: WriteFile): StateWriter => {
|
|
85
|
+
const lines = [
|
|
86
|
+
"strict digraph DiskGraph {\n",
|
|
87
|
+
"node [shape=box,style=rounded]\n",
|
|
88
|
+
dump.colorize ? `edge [colorscheme="${COLOR_SCHEME}"]\n` : "",
|
|
89
|
+
"nodesep=0.35;\n",
|
|
90
|
+
"subgraph cluster_graph {\n",
|
|
91
|
+
'color="white";\n',
|
|
92
|
+
];
|
|
93
|
+
const ranks = new Map<number, Set<bigint>>();
|
|
94
|
+
const colors = new Map<string, number>();
|
|
95
|
+
const addRank = (state: GraphState) => {
|
|
96
|
+
const rank = ranks.get(state.level) ?? new Set();
|
|
97
|
+
ranks.set(state.level, rank.add(state.fingerprint));
|
|
98
|
+
};
|
|
99
|
+
// TLC colors an edge by the name of its action, numbering the names from 2 as it finds them.
|
|
100
|
+
const getColor = (action: GraphAction | undefined) => {
|
|
101
|
+
if (!action) return 1;
|
|
102
|
+
const color = colors.get(action.name) ?? colors.size + 2;
|
|
103
|
+
colors.set(action.name, color);
|
|
104
|
+
return color;
|
|
105
|
+
};
|
|
106
|
+
const getLegend = () =>
|
|
107
|
+
[
|
|
108
|
+
`subgraph cluster_legend {graph[style=bold];label = "Next State Actions" style="solid"\n`,
|
|
109
|
+
`node [ labeljust="l",colorscheme="${COLOR_SCHEME}",style=filled,shape=record ]\n`,
|
|
110
|
+
...getJavaHashOrder([...colors.keys()], getStringHash).map(
|
|
111
|
+
(name) => `${name.replaceAll("!", ":")} [label="${name}",fillcolor=${colors.get(name)}]\n`,
|
|
112
|
+
),
|
|
113
|
+
"}",
|
|
114
|
+
].join("");
|
|
115
|
+
const getEnding = () =>
|
|
116
|
+
[
|
|
117
|
+
...getJavaHashOrder([...ranks], (rank) => rank[0]).map(
|
|
118
|
+
(rank) =>
|
|
119
|
+
`{rank = same; ${getJavaHashOrder([...rank[1]], getLongHash)
|
|
120
|
+
.map((fingerprint) => `${fingerprint};`)
|
|
121
|
+
.join("")}}\n`,
|
|
122
|
+
),
|
|
123
|
+
"}\n",
|
|
124
|
+
dump.colorize && colors.size > 1 ? getLegend() : "",
|
|
125
|
+
"}",
|
|
126
|
+
].join("");
|
|
127
|
+
const writeSnapshot = () => {
|
|
128
|
+
if (dump.snapshot) {
|
|
129
|
+
writeFile?.(dump.path.replaceAll(".dot", "_snapshot.dot"), lines.join("") + getEnding());
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
writeInitialState: (state) => {
|
|
134
|
+
lines.push(`${state.fingerprint} [label="${toDot(state.getLabel())}",style = filled]\n`);
|
|
135
|
+
addRank(state);
|
|
136
|
+
writeSnapshot();
|
|
137
|
+
},
|
|
138
|
+
writeSuccessor: (state, successor, isSeen, action) => {
|
|
139
|
+
const color = dump.colorize ? String(getColor(action)) : "black";
|
|
140
|
+
const label = dump.actionLabels && action ? escape(action.signature) : "";
|
|
141
|
+
lines.push(
|
|
142
|
+
`${state.fingerprint} -> ${successor.fingerprint} [label="${label}",color="${color}",fontcolor="${color}"];\n`,
|
|
143
|
+
);
|
|
144
|
+
if (!isSeen) {
|
|
145
|
+
lines.push(
|
|
146
|
+
`${successor.fingerprint} [label="${toDot(successor.getLabel())}",tooltip="${toDot(successor.getText())}"];\n`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
addRank(state);
|
|
150
|
+
writeSnapshot();
|
|
151
|
+
},
|
|
152
|
+
close: () => writeFile?.(dump.path, lines.join("") + getEnding()),
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const createStateWriter = (dump: StateDump, writeFile: WriteFile) =>
|
|
157
|
+
dump.dot ? createDotWriter(dump, writeFile) : createPlainWriter(dump, writeFile);
|
package/src/subexpressions.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type Bound,
|
|
4
|
+
type Definition,
|
|
5
|
+
type Expression,
|
|
6
|
+
type Instance,
|
|
7
|
+
type Module,
|
|
8
|
+
type OperatorParam,
|
|
9
|
+
type Selector,
|
|
10
|
+
type SourceLocation,
|
|
10
11
|
} from "./ast";
|
|
11
12
|
import { TlaSyntaxError } from "./errors";
|
|
12
|
-
import { getChildren } from "./evaluate";
|
|
13
13
|
import { getSourceName, mapChildren, mapDefinition } from "./instances";
|
|
14
14
|
import { getArity, getBoundNames, getPatternNames, LOCAL_RESOLUTION } from "./resolve";
|
|
15
15
|
|
package/src/successors.ts
CHANGED
|
@@ -1,28 +1,36 @@
|
|
|
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 { TlaEvaluationError } from "./errors";
|
|
10
|
+
import { TlaEvaluationError, TlaIllegalStateError, TlaIncompleteStateError } from "./errors";
|
|
4
11
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
5
12
|
import {
|
|
6
13
|
describeLocation,
|
|
14
|
+
describeRange,
|
|
7
15
|
bindArgument,
|
|
8
16
|
bindDefinitions,
|
|
9
17
|
bindOperatorArguments,
|
|
10
18
|
bindValue,
|
|
11
19
|
compileArgumentScope,
|
|
12
20
|
evaluate,
|
|
21
|
+
getEntryEvaluator,
|
|
13
22
|
expectBoolean,
|
|
14
23
|
forEachBinding,
|
|
15
24
|
getBoundsDomain,
|
|
16
|
-
getChildren,
|
|
17
25
|
getDefinition,
|
|
18
26
|
getEnumeratedElements,
|
|
19
27
|
getReevaluatedDefinitions,
|
|
20
28
|
lookupScope,
|
|
21
29
|
resolveApplyOperator,
|
|
22
30
|
resolveGlobalOperator,
|
|
23
|
-
isLocallyResolved,
|
|
24
31
|
type Context,
|
|
25
32
|
type Environment,
|
|
33
|
+
type Evaluator,
|
|
26
34
|
type Scope,
|
|
27
35
|
type State,
|
|
28
36
|
} from "./evaluate";
|
|
@@ -81,25 +89,54 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
81
89
|
return copy;
|
|
82
90
|
};
|
|
83
91
|
|
|
84
|
-
|
|
85
|
-
|
|
92
|
+
// TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
|
|
93
|
+
const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
|
|
86
94
|
if (typeof value === "boolean") return value;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
95
|
+
throw new TlaEvaluationError(
|
|
96
|
+
`In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
101
|
+
// TLC rejects a number in the next-state relation when it splits the relation into actions.
|
|
102
|
+
if (context.next !== undefined && expression.kind === "Number") {
|
|
103
|
+
throw setExitCode(
|
|
104
|
+
new TlaEvaluationError(
|
|
105
|
+
`The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
|
|
106
|
+
),
|
|
107
|
+
EXIT_STATUS.error,
|
|
108
|
+
);
|
|
92
109
|
}
|
|
110
|
+
return expectFormula(expression, value, context.next === undefined);
|
|
93
111
|
};
|
|
94
112
|
|
|
95
113
|
const isAssignmentOperator = (operator: string) =>
|
|
96
114
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
97
115
|
|
|
116
|
+
const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
|
|
117
|
+
|
|
118
|
+
const getRunners = (environment: Environment, isInit: boolean) => {
|
|
119
|
+
if (!environment.trace) return isInit ? initRunners : nextRunners;
|
|
120
|
+
const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
|
|
121
|
+
tracedRunners.set(environment, runners);
|
|
122
|
+
return runners[isInit ? 0 : 1];
|
|
123
|
+
};
|
|
124
|
+
|
|
98
125
|
const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
|
|
99
|
-
const runners = isInit
|
|
126
|
+
const runners = getRunners(environment, isInit);
|
|
100
127
|
const cached = runners.get(expression);
|
|
101
128
|
if (cached) return cached;
|
|
102
|
-
const
|
|
129
|
+
const compiled = compileAction(expression, environment, isInit);
|
|
130
|
+
const trace = environment.trace;
|
|
131
|
+
// TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
|
|
132
|
+
const runner: Runner = trace
|
|
133
|
+
? (run, scope, partial, action, isFixed, onComplete) => {
|
|
134
|
+
const depth = trace.length;
|
|
135
|
+
trace.push(expression);
|
|
136
|
+
compiled(run, scope, partial, action, isFixed, onComplete);
|
|
137
|
+
trace.length = depth;
|
|
138
|
+
}
|
|
139
|
+
: compiled;
|
|
103
140
|
runners.set(expression, runner);
|
|
104
141
|
return runner;
|
|
105
142
|
};
|
|
@@ -183,6 +220,23 @@ const getUnchangedIndices = (
|
|
|
183
220
|
return getUnchangedIndices(definition.body, environment, visited);
|
|
184
221
|
};
|
|
185
222
|
|
|
223
|
+
// TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
|
|
224
|
+
const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
|
|
225
|
+
if (expression.kind === "Tuple") {
|
|
226
|
+
return [
|
|
227
|
+
expression,
|
|
228
|
+
...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
|
|
229
|
+
];
|
|
230
|
+
}
|
|
231
|
+
const definition =
|
|
232
|
+
expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
|
|
233
|
+
? getZeroArityBody(expression.name, environment)
|
|
234
|
+
: undefined;
|
|
235
|
+
return definition
|
|
236
|
+
? [expression, ...getUnchangedFrames(definition.body, environment)]
|
|
237
|
+
: [expression];
|
|
238
|
+
};
|
|
239
|
+
|
|
186
240
|
const ACTION_KINDS = new Set([
|
|
187
241
|
"Label",
|
|
188
242
|
"And",
|
|
@@ -287,12 +341,35 @@ const toConjunct = (
|
|
|
287
341
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
288
342
|
};
|
|
289
343
|
|
|
344
|
+
// The leading guards of `\E x \in S, y \in T : P /\ Q` that read no inner bound hold for every inner
|
|
345
|
+
// binding alike, so they can run once for each element of the outermost bound.
|
|
346
|
+
const getHoistedGuards = (expression: QuantifierExpression, environment: Environment) => {
|
|
347
|
+
if (expression.bounds.length < 2 || expression.body.kind !== "And") return undefined;
|
|
348
|
+
const names = expression.bounds.flatMap((bound) =>
|
|
349
|
+
bound.pattern.kind === "Name" ? [bound.pattern.name] : [],
|
|
350
|
+
);
|
|
351
|
+
if (names.length !== expression.bounds.length) return undefined;
|
|
352
|
+
const innerNames = new Set(names.slice(0, -1));
|
|
353
|
+
const operands = expression.body.operands;
|
|
354
|
+
const index = operands.findIndex((operand) => {
|
|
355
|
+
if (getReadVariables(operand, environment) === undefined) return true;
|
|
356
|
+
if (hasBranches(operand, environment, new Set())) return true;
|
|
357
|
+
const readLocals = getReadLocals(operand, NO_NAMES);
|
|
358
|
+
return !readLocals || readLocals.some((name) => innerNames.has(name));
|
|
359
|
+
});
|
|
360
|
+
const count = index === -1 ? operands.length : index;
|
|
361
|
+
if (count === 0) return undefined;
|
|
362
|
+
return { names, guards: operands.slice(0, count), rest: operands.slice(count) };
|
|
363
|
+
};
|
|
364
|
+
|
|
290
365
|
const compileConjunction = (
|
|
291
366
|
operands: Expression[],
|
|
292
367
|
environment: Environment,
|
|
293
368
|
isInit: boolean,
|
|
294
369
|
): Runner => {
|
|
295
370
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
371
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
372
|
+
const trace = environment.trace;
|
|
296
373
|
const runFrom = (
|
|
297
374
|
start: number,
|
|
298
375
|
run: Run,
|
|
@@ -313,14 +390,25 @@ const compileConjunction = (
|
|
|
313
390
|
return;
|
|
314
391
|
}
|
|
315
392
|
const conjunct = conjuncts[index];
|
|
393
|
+
if (trace) {
|
|
394
|
+
trace.push(conjunct.expression);
|
|
395
|
+
if (conjunct.expression.kind === "Unchanged") {
|
|
396
|
+
trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
|
|
397
|
+
}
|
|
398
|
+
}
|
|
316
399
|
if (conjunct.kind === "Guard") {
|
|
317
400
|
context ??= getContext(run, scope, current);
|
|
318
|
-
|
|
401
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
402
|
+
conjunct.expression,
|
|
403
|
+
environment,
|
|
404
|
+
));
|
|
405
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
319
406
|
continue;
|
|
320
407
|
}
|
|
321
408
|
if (conjunct.kind === "Assign") {
|
|
322
409
|
context ??= getContext(run, scope, current);
|
|
323
|
-
const
|
|
410
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
411
|
+
const value = evaluator(context);
|
|
324
412
|
const assigned = current[conjunct.index];
|
|
325
413
|
if (assigned === undefined) {
|
|
326
414
|
current = assign(current, conjunct.index, value);
|
|
@@ -357,10 +445,7 @@ const compileConjunction = (
|
|
|
357
445
|
};
|
|
358
446
|
|
|
359
447
|
const holdsInInit = (expression: Expression, context: Context) =>
|
|
360
|
-
|
|
361
|
-
evaluate(expression, context),
|
|
362
|
-
`the initial predicate at ${describeLocation(expression)}`,
|
|
363
|
-
);
|
|
448
|
+
expectFormula(expression, evaluate(expression, context), true);
|
|
364
449
|
|
|
365
450
|
const compileGuard =
|
|
366
451
|
(expression: Expression): Runner =>
|
|
@@ -385,7 +470,7 @@ const compileMembership = (
|
|
|
385
470
|
expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
|
|
386
471
|
if (!isEnumerable(value)) {
|
|
387
472
|
throw new TlaEvaluationError(
|
|
388
|
-
`In computing next states, the right side of \\
|
|
473
|
+
`In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
|
|
389
474
|
);
|
|
390
475
|
}
|
|
391
476
|
const elements = getStreamedElements(value);
|
|
@@ -729,6 +814,34 @@ const compileAction = (
|
|
|
729
814
|
}
|
|
730
815
|
};
|
|
731
816
|
}
|
|
817
|
+
const hoisted =
|
|
818
|
+
isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
|
|
819
|
+
if (hoisted) {
|
|
820
|
+
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
821
|
+
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
822
|
+
const context = getContext(run, scope, partial);
|
|
823
|
+
const sets = expression.bounds.map((innerBound) =>
|
|
824
|
+
getEnumeratedElements(evaluate(innerBound.set, context), innerBound.set),
|
|
825
|
+
);
|
|
826
|
+
if (sets.some((elements) => elements.length === 0)) return;
|
|
827
|
+
const visit = (index: number, boundScope: Scope | undefined): void => {
|
|
828
|
+
if (index < 0) {
|
|
829
|
+
rest(run, boundScope, partial, action, isFixed, onComplete);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
for (const element of sets[index]) {
|
|
833
|
+
if (run.isStopped) return;
|
|
834
|
+
const elementScope = bindValue(boundScope, hoisted.names[index], element);
|
|
835
|
+
if (index === sets.length - 1) {
|
|
836
|
+
const guardContext = getContext(run, elementScope, partial);
|
|
837
|
+
if (!hoisted.guards.every((guard) => holds(guard, guardContext))) continue;
|
|
838
|
+
}
|
|
839
|
+
visit(index - 1, elementScope);
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
visit(sets.length - 1, scope);
|
|
843
|
+
};
|
|
844
|
+
}
|
|
732
845
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
733
846
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
734
847
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
@@ -804,7 +917,9 @@ const compileAction = (
|
|
|
804
917
|
);
|
|
805
918
|
const body = arm?.body ?? other;
|
|
806
919
|
if (!body)
|
|
807
|
-
throw new TlaEvaluationError(
|
|
920
|
+
throw new TlaEvaluationError(
|
|
921
|
+
`In computing ${run.isInit ? "initial" : "next"} states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
922
|
+
);
|
|
808
923
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
809
924
|
};
|
|
810
925
|
}
|
|
@@ -913,18 +1028,19 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
913
1028
|
);
|
|
914
1029
|
}
|
|
915
1030
|
const environment = context.environment;
|
|
916
|
-
const
|
|
1031
|
+
const run: Run = { environment, isInit: false, current: state, isStopped: false };
|
|
1032
|
+
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
917
1033
|
getRunner(expression, environment, false)(
|
|
918
|
-
|
|
1034
|
+
run,
|
|
919
1035
|
context.scope,
|
|
920
1036
|
getEmptyState(environment),
|
|
921
1037
|
undefined,
|
|
922
1038
|
false,
|
|
923
1039
|
() => {
|
|
924
|
-
|
|
1040
|
+
run.isStopped = true;
|
|
925
1041
|
},
|
|
926
1042
|
);
|
|
927
|
-
return
|
|
1043
|
+
return run.isStopped;
|
|
928
1044
|
};
|
|
929
1045
|
|
|
930
1046
|
export const forEachInitialState = (
|
|
@@ -943,14 +1059,7 @@ export const forEachInitialState = (
|
|
|
943
1059
|
(partial) => {
|
|
944
1060
|
if (run.isStopped) return;
|
|
945
1061
|
if (!isComplete(partial)) {
|
|
946
|
-
|
|
947
|
-
// an evaluation failure.
|
|
948
|
-
throw setExitCode(
|
|
949
|
-
new TlaEvaluationError(
|
|
950
|
-
`The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
|
|
951
|
-
),
|
|
952
|
-
EXIT_STATUS.error,
|
|
953
|
-
);
|
|
1062
|
+
throw setExitCode(new TlaIllegalStateError(partial), EXIT_STATUS.error);
|
|
954
1063
|
}
|
|
955
1064
|
run.isStopped = onState(partial);
|
|
956
1065
|
},
|
|
@@ -993,8 +1102,10 @@ export const forEachSuccessor = (
|
|
|
993
1102
|
(partial, action) => {
|
|
994
1103
|
if (run.isStopped) return;
|
|
995
1104
|
if (!isComplete(partial)) {
|
|
996
|
-
throw new
|
|
997
|
-
|
|
1105
|
+
throw new TlaIncompleteStateError(
|
|
1106
|
+
partial,
|
|
1107
|
+
action,
|
|
1108
|
+
getMissingVariables(partial, environment).sort(),
|
|
998
1109
|
);
|
|
999
1110
|
}
|
|
1000
1111
|
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,
|
|
@@ -47,9 +48,13 @@ const composePermutations = (first: Images, second: Images) => {
|
|
|
47
48
|
return composed;
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
// TLC checks the symmetry set outside any phase that reports an error itself.
|
|
52
|
+
const createSymmetryError = (message: string) =>
|
|
53
|
+
setExitCode(new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${message}`), EXIT_STATUS.error);
|
|
54
|
+
|
|
55
|
+
const toImages = (value: Value) => {
|
|
51
56
|
if (!isFunctionValue(value)) {
|
|
52
|
-
throw
|
|
57
|
+
throw createSymmetryError("The symmetry values must be function records.");
|
|
53
58
|
}
|
|
54
59
|
const functionValue = toFiniteFunction(value);
|
|
55
60
|
const permutation: Images = new Map();
|
|
@@ -65,8 +70,13 @@ const toImages = (value: Value, name: string) => {
|
|
|
65
70
|
|
|
66
71
|
export const getSymmetryGroup = (name: string, environment: Environment): Permutation[] => {
|
|
67
72
|
const definition = environment.definitions.get(name);
|
|
73
|
+
if (definition === undefined && !environment.constants.has(name)) {
|
|
74
|
+
throw new TlaConfigError(
|
|
75
|
+
`The symmetry function ${name} specified in the configuration file\nis not defined in the specification.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
68
78
|
if (definition?.kind !== "Operator" || definition.params.length > 0) {
|
|
69
|
-
throw
|
|
79
|
+
throw createSymmetryError(`The symmetry function ${name} must specify a set of permutations.`);
|
|
70
80
|
}
|
|
71
81
|
const value = evaluate(definition.body, {
|
|
72
82
|
environment,
|
|
@@ -75,9 +85,9 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
75
85
|
next: undefined,
|
|
76
86
|
});
|
|
77
87
|
if (!isSetValue(value) || !isEnumerable(value)) {
|
|
78
|
-
throw
|
|
88
|
+
throw createSymmetryError("The symmetry operator must specify a set of functions.");
|
|
79
89
|
}
|
|
80
|
-
const permutations = toFiniteSet(value).elements.map(
|
|
90
|
+
const permutations = toFiniteSet(value).elements.map(toImages);
|
|
81
91
|
const isDisabled =
|
|
82
92
|
definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
|
|
83
93
|
if (isDisabled) return [];
|