tla-ts 0.0.2 → 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/README.md +78 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +21 -2
- package/dist/run-cli.js +21 -2
- package/dist/transfer.cjs +27 -15
- package/dist/transfer.js +27 -15
- package/package.json +3 -1
- package/src/ast.ts +134 -4
- package/src/check.ts +274 -76
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +9 -19
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +75 -16
- package/src/errors.ts +46 -3
- package/src/evaluate.ts +88 -143
- package/src/exit-codes.ts +21 -43
- package/src/fingerprints.ts +46 -11
- package/src/instances.ts +15 -14
- package/src/levels.ts +8 -8
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +711 -0
- package/src/liveness.ts +54 -535
- package/src/obligation-action.ts +384 -0
- package/src/obligation-anon.ts +575 -0
- package/src/obligation-deref.ts +637 -0
- package/src/obligation-expression.ts +1060 -0
- package/src/obligation-format.ts +406 -0
- package/src/obligation-generate.ts +1074 -0
- package/src/obligation-levels.ts +329 -0
- package/src/obligation-module.ts +1042 -0
- package/src/obligation-printer.ts +889 -0
- package/src/obligation-proof.ts +76 -0
- package/src/obligation-source.ts +1084 -0
- package/src/obligation-tuples.ts +184 -0
- package/src/obligations.ts +316 -0
- package/src/parallel-worker.ts +166 -210
- package/src/parallel.ts +76 -35
- package/src/parser.ts +297 -53
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +353 -40
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +18 -6
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +2 -7
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +77 -13
- package/src/symmetry.ts +14 -2
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +9 -28
- package/src/tlc-order.ts +17 -3
- package/src/transfer.ts +22 -1
- package/src/values.ts +118 -34
|
@@ -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, UNEXPECTED_EXCEPTION } 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,8 +89,7 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
81
89
|
return copy;
|
|
82
90
|
};
|
|
83
91
|
|
|
84
|
-
const holds = (expression: Expression, context: Context) => {
|
|
85
|
-
const value = evaluate(expression, context);
|
|
92
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
86
93
|
if (typeof value === "boolean") return value;
|
|
87
94
|
try {
|
|
88
95
|
return expectBoolean(value, `the action at ${describeLocation(expression)}`);
|
|
@@ -287,12 +294,34 @@ const toConjunct = (
|
|
|
287
294
|
return { kind: "Branch", expression, runner: getRunner(expression, environment, isInit) };
|
|
288
295
|
};
|
|
289
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
|
+
|
|
290
318
|
const compileConjunction = (
|
|
291
319
|
operands: Expression[],
|
|
292
320
|
environment: Environment,
|
|
293
321
|
isInit: boolean,
|
|
294
322
|
): Runner => {
|
|
295
323
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
324
|
+
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
296
325
|
const runFrom = (
|
|
297
326
|
start: number,
|
|
298
327
|
run: Run,
|
|
@@ -315,12 +344,17 @@ const compileConjunction = (
|
|
|
315
344
|
const conjunct = conjuncts[index];
|
|
316
345
|
if (conjunct.kind === "Guard") {
|
|
317
346
|
context ??= getContext(run, scope, current);
|
|
318
|
-
|
|
347
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
348
|
+
conjunct.expression,
|
|
349
|
+
environment,
|
|
350
|
+
));
|
|
351
|
+
if (!holds(conjunct.expression, context, evaluator(context))) return;
|
|
319
352
|
continue;
|
|
320
353
|
}
|
|
321
354
|
if (conjunct.kind === "Assign") {
|
|
322
355
|
context ??= getContext(run, scope, current);
|
|
323
|
-
const
|
|
356
|
+
const evaluator = (evaluators[index] ??= getEntryEvaluator(conjunct.value, environment));
|
|
357
|
+
const value = evaluator(context);
|
|
324
358
|
const assigned = current[conjunct.index];
|
|
325
359
|
if (assigned === undefined) {
|
|
326
360
|
current = assign(current, conjunct.index, value);
|
|
@@ -729,6 +763,33 @@ const compileAction = (
|
|
|
729
763
|
}
|
|
730
764
|
};
|
|
731
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
|
+
}
|
|
732
793
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
733
794
|
forEachBinding(expression.bounds, getContext(run, scope, partial), (boundScope) => {
|
|
734
795
|
body(run, boundScope, partial, action, isFixed, onComplete);
|
|
@@ -804,7 +865,9 @@ const compileAction = (
|
|
|
804
865
|
);
|
|
805
866
|
const body = arm?.body ?? other;
|
|
806
867
|
if (!body)
|
|
807
|
-
throw new TlaEvaluationError(
|
|
868
|
+
throw new TlaEvaluationError(
|
|
869
|
+
`${UNEXPECTED_EXCEPTION}In computing next states, TLC encountered a CASE with no conditions true.\n${describeRange(expression)}`,
|
|
870
|
+
);
|
|
808
871
|
body(run, scope, partial, action, isFixed, onComplete);
|
|
809
872
|
};
|
|
810
873
|
}
|
|
@@ -913,18 +976,19 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
913
976
|
);
|
|
914
977
|
}
|
|
915
978
|
const environment = context.environment;
|
|
916
|
-
const
|
|
979
|
+
const run: Run = { environment, isInit: false, current: state, isStopped: false };
|
|
980
|
+
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
917
981
|
getRunner(expression, environment, false)(
|
|
918
|
-
|
|
982
|
+
run,
|
|
919
983
|
context.scope,
|
|
920
984
|
getEmptyState(environment),
|
|
921
985
|
undefined,
|
|
922
986
|
false,
|
|
923
987
|
() => {
|
|
924
|
-
|
|
988
|
+
run.isStopped = true;
|
|
925
989
|
},
|
|
926
990
|
);
|
|
927
|
-
return
|
|
991
|
+
return run.isStopped;
|
|
928
992
|
};
|
|
929
993
|
|
|
930
994
|
export const forEachInitialState = (
|
package/src/symmetry.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
isEnumerable,
|
|
10
10
|
isFunctionValue,
|
|
11
11
|
isModelValue,
|
|
12
|
+
isSameValue,
|
|
12
13
|
isSetValue,
|
|
13
14
|
toFiniteFunction,
|
|
14
15
|
toFiniteSet,
|
|
@@ -163,7 +164,10 @@ const permuteValue = (value: Value, permutation: Permutation): Value => {
|
|
|
163
164
|
if (size === 0 || size > CACHED_SIZE_LIMIT) return computePermutedValue(value, permutation);
|
|
164
165
|
const cached = permutation.cache.get(value);
|
|
165
166
|
if (cached !== undefined) return cached;
|
|
166
|
-
const
|
|
167
|
+
const computed = computePermutedValue(value, permutation);
|
|
168
|
+
// A permutation often maps a value to an equal one. Keeping the original lets the comparison
|
|
169
|
+
// with the smallest state so far stop at identity.
|
|
170
|
+
const permuted = computed !== value && isSameValue(computed, value) ? value : computed;
|
|
167
171
|
if (permutation.cacheSize >= permutation.cacheLimit) {
|
|
168
172
|
permutation.cache = new WeakMap();
|
|
169
173
|
permutation.cacheSize = 0;
|
|
@@ -184,7 +188,15 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
|
184
188
|
const domain = permuteElements(functionValue.domain, permutation);
|
|
185
189
|
const range = permuteElements(functionValue.range, permutation) ?? functionValue.range;
|
|
186
190
|
if (domain) {
|
|
187
|
-
|
|
191
|
+
const permuted = createFunction(
|
|
192
|
+
domain.map((key, index): [Value, Value] => [key, range[index]]),
|
|
193
|
+
);
|
|
194
|
+
// A permutation often maps the domain to itself, such as a function over the symmetric set.
|
|
195
|
+
// Sharing the original domain array lets comparisons with other images skip the domain.
|
|
196
|
+
const isSameDomain = permuted.domain.every(
|
|
197
|
+
(key, index) => key === functionValue.domain[index],
|
|
198
|
+
);
|
|
199
|
+
return isSameDomain ? createSortedFunction(functionValue.domain, permuted.range) : permuted;
|
|
188
200
|
}
|
|
189
201
|
return range === functionValue.range
|
|
190
202
|
? functionValue
|
package/src/tlc-ext.ts
CHANGED
|
@@ -10,16 +10,8 @@ import {
|
|
|
10
10
|
} from "./builtins";
|
|
11
11
|
import { TlaEvaluationError, TlaModuleError, TlaUnsupportedError } from "./errors";
|
|
12
12
|
import { getTlcFingerprint } from "./tlc-random";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
expectBoolean,
|
|
16
|
-
getChildren,
|
|
17
|
-
getDefinition,
|
|
18
|
-
isLocallyResolved,
|
|
19
|
-
type Builtin,
|
|
20
|
-
type Environment,
|
|
21
|
-
} from "./evaluate";
|
|
22
|
-
import type { Definition, Expression } from "./ast";
|
|
13
|
+
import { evaluate, expectBoolean, getDefinition, type Builtin, type Environment } from "./evaluate";
|
|
14
|
+
import { getChildren, isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
23
15
|
import type { TraceStep } from "./check";
|
|
24
16
|
import {
|
|
25
17
|
getNamedRegisters,
|
|
@@ -111,15 +103,35 @@ const getRegister = (runtime: CheckerRuntime, key: Value) => {
|
|
|
111
103
|
return undefined;
|
|
112
104
|
};
|
|
113
105
|
|
|
114
|
-
|
|
106
|
+
// TLC's TLCSet. TLCExt's TLCGetAndSet is a TLA+ definition that calls it.
|
|
107
|
+
export const setTlcValue = (
|
|
108
|
+
runtime: CheckerRuntime,
|
|
109
|
+
print: (text: string) => void,
|
|
110
|
+
key: Value,
|
|
111
|
+
value: Value,
|
|
112
|
+
) => {
|
|
115
113
|
if (typeof key === "number" && key >= 0) {
|
|
116
114
|
runtime.registers.set(key, value);
|
|
117
115
|
return;
|
|
118
116
|
}
|
|
117
|
+
if (key === "exit") {
|
|
118
|
+
if (value === true) runtime.isStopped = true;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (key === "pause") {
|
|
122
|
+
if (value !== true) return;
|
|
123
|
+
print("Press enter to resume model checking.");
|
|
124
|
+
runtime.pause();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
119
127
|
if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
120
128
|
runtime.namedRegisters.set(key, value);
|
|
121
129
|
return;
|
|
122
130
|
}
|
|
131
|
+
if (typeof key === "string" && key.startsWith("-D")) {
|
|
132
|
+
runtime.properties.set(key.slice(2), formatValue(value));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
123
135
|
throw new TlaModuleError(
|
|
124
136
|
`The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
125
137
|
);
|
|
@@ -203,7 +215,12 @@ export const createTlcExtOperators = (
|
|
|
203
215
|
const key = getValue(args, 0, "TLCGetAndSet");
|
|
204
216
|
const previous = getRegister(runtime, key) ?? getValue(args, 3, "TLCGetAndSet");
|
|
205
217
|
const update = getOperator(args, 1, "TLCGetAndSet");
|
|
206
|
-
|
|
218
|
+
setTlcValue(
|
|
219
|
+
runtime,
|
|
220
|
+
print,
|
|
221
|
+
key,
|
|
222
|
+
update.call([previous, getValue(args, 2, "TLCGetAndSet")]),
|
|
223
|
+
);
|
|
207
224
|
return previous;
|
|
208
225
|
}),
|
|
209
226
|
isStateLevel: true,
|
package/src/tlc-module.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
getValue,
|
|
16
16
|
} from "./builtins";
|
|
17
17
|
import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "./runtime";
|
|
18
|
-
import { createActionRecord } from "./tlc-ext";
|
|
18
|
+
import { createActionRecord, setTlcValue } from "./tlc-ext";
|
|
19
19
|
import { getRandomElement } from "./tlc-random";
|
|
20
20
|
import {
|
|
21
21
|
ANY_SET,
|
|
@@ -39,11 +39,17 @@ const formatCalendarVersion = (date: Date) =>
|
|
|
39
39
|
const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
|
|
40
40
|
createFunction([...registers].map(([key, value]): [Value, Value] => [key, createTuple([value])]));
|
|
41
41
|
|
|
42
|
+
// TLC creates its model checker after it processes the spec, so these are undefined before then.
|
|
43
|
+
const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
|
|
44
|
+
|
|
42
45
|
const getRuntimeValue = (
|
|
43
46
|
runtime: CheckerRuntime,
|
|
44
47
|
key: string,
|
|
45
48
|
state: (Value | undefined)[] | undefined,
|
|
46
49
|
) => {
|
|
50
|
+
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
+
throw new TlaModuleError(`TLCGet(${key}) was undefined.`);
|
|
52
|
+
}
|
|
47
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
48
54
|
switch (key) {
|
|
49
55
|
case "level":
|
|
@@ -203,33 +209,8 @@ export const createTlcOperators = (
|
|
|
203
209
|
}),
|
|
204
210
|
defineStateOperator("TLCSet", 2, (args) => {
|
|
205
211
|
const key = getValue(args, 0, "TLCSet");
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
runtime.registers.set(key, value);
|
|
209
|
-
return true;
|
|
210
|
-
}
|
|
211
|
-
if (key === "exit") {
|
|
212
|
-
if (value === true) runtime.isStopped = true;
|
|
213
|
-
return true;
|
|
214
|
-
}
|
|
215
|
-
if (key === "pause") {
|
|
216
|
-
if (value === true) {
|
|
217
|
-
print("Press enter to resume model checking.");
|
|
218
|
-
runtime.pause();
|
|
219
|
-
}
|
|
220
|
-
return true;
|
|
221
|
-
}
|
|
222
|
-
if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
223
|
-
runtime.namedRegisters.set(key, value);
|
|
224
|
-
return true;
|
|
225
|
-
}
|
|
226
|
-
if (typeof key === "string" && key.startsWith("-D")) {
|
|
227
|
-
runtime.properties.set(key.slice(2), formatValue(value));
|
|
228
|
-
return true;
|
|
229
|
-
}
|
|
230
|
-
throw new TlaModuleError(
|
|
231
|
-
`The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
232
|
-
);
|
|
212
|
+
setTlcValue(runtime, print, key, getValue(args, 1, "TLCSet"));
|
|
213
|
+
return true;
|
|
233
214
|
}),
|
|
234
215
|
defineConstant("Any", ANY_SET),
|
|
235
216
|
defineOperator("RandomElement", 1, (args) =>
|
package/src/tlc-order.ts
CHANGED
|
@@ -184,9 +184,18 @@ const isAdjacent = (left: Token, right: Token | undefined) =>
|
|
|
184
184
|
right.line === left.line &&
|
|
185
185
|
right.column === left.column + left.text.length;
|
|
186
186
|
|
|
187
|
+
const STRING_ESCAPES: Record<string, string> = {
|
|
188
|
+
"\\": "\\\\",
|
|
189
|
+
'"': '\\"',
|
|
190
|
+
"\n": "\\n",
|
|
191
|
+
"\t": "\\t",
|
|
192
|
+
"\r": "\\r",
|
|
193
|
+
"\f": "\\f",
|
|
194
|
+
};
|
|
195
|
+
|
|
187
196
|
// The texts SANY interns while parsing, in order. SANY reads a proof step label such as <1>2 and
|
|
188
|
-
// a module header such as ---- MODULE as one token
|
|
189
|
-
// during semantic analysis.
|
|
197
|
+
// a module header such as ---- MODULE as one token. It interns a string literal as written, quotes
|
|
198
|
+
// included, and interns the literal's contents only during semantic analysis.
|
|
190
199
|
const getParsedTexts = (tokens: Token[]) => {
|
|
191
200
|
const texts: string[] = [];
|
|
192
201
|
for (let index = 0; index < tokens.length; index++) {
|
|
@@ -202,7 +211,12 @@ const getParsedTexts = (tokens: Token[]) => {
|
|
|
202
211
|
continue;
|
|
203
212
|
}
|
|
204
213
|
const isHeader = token.text === "MODULE" && tokens[index - 1]?.kind === "Separator";
|
|
205
|
-
if (
|
|
214
|
+
if (isHeader) continue;
|
|
215
|
+
texts.push(
|
|
216
|
+
token.kind === "String"
|
|
217
|
+
? `"${token.text.replace(/[\\"\n\t\r\f]/g, (character) => STRING_ESCAPES[character])}"`
|
|
218
|
+
: token.text,
|
|
219
|
+
);
|
|
206
220
|
}
|
|
207
221
|
return texts;
|
|
208
222
|
};
|
package/src/transfer.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createModelValue,
|
|
3
|
+
createSortedFunction,
|
|
4
|
+
createSortedSet,
|
|
5
|
+
getDomainInterval,
|
|
6
|
+
getFunctionDomain,
|
|
7
|
+
type Value,
|
|
8
|
+
} from "./values";
|
|
2
9
|
|
|
3
10
|
const FALSE = 0;
|
|
4
11
|
const TRUE = 1;
|
|
@@ -7,6 +14,7 @@ const STRING = 3;
|
|
|
7
14
|
const MODEL_VALUE = 4;
|
|
8
15
|
const SET = 5;
|
|
9
16
|
const FUNCTION = 6;
|
|
17
|
+
const INTERVAL_FUNCTION = 7;
|
|
10
18
|
|
|
11
19
|
export interface EncodedStates {
|
|
12
20
|
words: Int32Array;
|
|
@@ -65,6 +73,13 @@ export const createStateEncoder = (): StateEncoder => {
|
|
|
65
73
|
return value.elements.every(encodeValue);
|
|
66
74
|
}
|
|
67
75
|
if (value.kind === "Function") {
|
|
76
|
+
const interval = getDomainInterval(value.domain);
|
|
77
|
+
if (interval) {
|
|
78
|
+
push(INTERVAL_FUNCTION);
|
|
79
|
+
push(interval.low);
|
|
80
|
+
push(value.range.length);
|
|
81
|
+
return value.range.every(encodeValue);
|
|
82
|
+
}
|
|
68
83
|
push(FUNCTION);
|
|
69
84
|
push(value.domain.length);
|
|
70
85
|
return value.domain.every(encodeValue) && value.range.every(encodeValue);
|
|
@@ -102,6 +117,12 @@ export const createStateDecoder =
|
|
|
102
117
|
if (tag === INTEGER) return words[cursor.index++];
|
|
103
118
|
if (tag === STRING) return encoded.strings[words[cursor.index++]];
|
|
104
119
|
if (tag === MODEL_VALUE) return createModelValue(encoded.strings[words[cursor.index++]]);
|
|
120
|
+
if (tag === INTERVAL_FUNCTION) {
|
|
121
|
+
const low = words[cursor.index++];
|
|
122
|
+
const length = words[cursor.index++];
|
|
123
|
+
const domain = getFunctionDomain({ kind: "Interval", low, high: low + length - 1 });
|
|
124
|
+
return createSortedFunction(domain, decodeElements(length));
|
|
125
|
+
}
|
|
105
126
|
const length = words[cursor.index++];
|
|
106
127
|
if (tag === SET) return createSortedSet(decodeElements(length));
|
|
107
128
|
const domain = decodeElements(length);
|