tla-ts 0.0.1 → 0.0.3-dev.037ac3b
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 +98 -39
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +225 -30
- package/dist/index.d.ts +225 -30
- 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 -1
- package/dist/run-cli.js +21 -1
- package/dist/transfer.cjs +400 -39
- package/dist/transfer.js +400 -39
- package/package.json +6 -2
- package/src/ast.ts +213 -6
- package/src/bags.ts +161 -71
- package/src/builtins.ts +203 -23
- package/src/bundled-modules.ts +63 -32
- package/src/check.ts +1132 -258
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +58 -34
- package/src/config.ts +39 -11
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +557 -153
- package/src/errors.ts +66 -1
- package/src/evaluate.ts +553 -258
- package/src/exit-codes.ts +53 -0
- package/src/fingerprints.ts +48 -13
- package/src/index.ts +9 -4
- package/src/instances.ts +235 -79
- package/src/java-overrides.ts +241 -0
- package/src/json.ts +153 -72
- package/src/levels.ts +850 -0
- package/src/lexer.ts +108 -29
- package/src/live-check.ts +692 -0
- package/src/liveness.ts +71 -596
- 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 +186 -201
- package/src/parallel.ts +179 -84
- package/src/parser.ts +970 -206
- package/src/pretty-print.ts +259 -0
- package/src/randomization.ts +24 -113
- package/src/resolve.ts +483 -51
- package/src/run-cli.ts +473 -62
- package/src/runtime.ts +65 -28
- package/src/spec-description.ts +521 -0
- package/src/standard-levels.ts +105 -0
- package/src/standard-modules.ts +218 -92
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +31 -45
- package/src/substitution-levels.ts +184 -0
- package/src/successors.ts +336 -57
- package/src/symmetry.ts +79 -23
- package/src/tableau.ts +93 -60
- package/src/tlc-ext.ts +148 -44
- package/src/tlc-module.ts +71 -66
- package/src/tlc-modules.ts +1022 -0
- package/src/tlc-order.ts +511 -0
- package/src/tlc-random.ts +414 -0
- package/src/tlc-summary.ts +124 -0
- package/src/transfer.ts +26 -5
- package/src/values.ts +1285 -179
|
@@ -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,17 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Definition,
|
|
5
|
-
Expression,
|
|
6
|
-
Instance,
|
|
7
|
-
Module,
|
|
8
|
-
OperatorParam,
|
|
9
|
-
Selector,
|
|
10
|
-
SourceLocation,
|
|
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,
|
|
11
11
|
} from "./ast";
|
|
12
12
|
import { TlaSyntaxError } from "./errors";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
13
|
+
import { getSourceName, mapChildren, mapDefinition } from "./instances";
|
|
14
|
+
import { getArity, getBoundNames, getPatternNames, LOCAL_RESOLUTION } from "./resolve";
|
|
15
15
|
|
|
16
16
|
type Lookup = (name: string) => Definition | undefined;
|
|
17
17
|
|
|
@@ -27,21 +27,10 @@ interface Selection {
|
|
|
27
27
|
location: SourceLocation;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
31
|
-
|
|
32
30
|
const SELECTION_NAME = "$subexpression";
|
|
33
31
|
|
|
34
|
-
const describeLocation = (location: SourceLocation) =>
|
|
35
|
-
`line ${location.line}, column ${location.column}`;
|
|
36
|
-
|
|
37
32
|
const createError = (message: string, location: SourceLocation) =>
|
|
38
|
-
new TlaSyntaxError(`${message} at ${
|
|
39
|
-
|
|
40
|
-
const getPatternNames = (pattern: BoundPattern) =>
|
|
41
|
-
pattern.kind === "Name" ? [pattern.name] : pattern.names;
|
|
42
|
-
|
|
43
|
-
const getBoundNames = (bounds: Bound[]) =>
|
|
44
|
-
bounds.flatMap((bound) => getPatternNames(bound.pattern));
|
|
33
|
+
new TlaSyntaxError(`${message} at line ${location.line}, column ${location.column}`);
|
|
45
34
|
|
|
46
35
|
const getBoundSets = (bounds: Bound[]) =>
|
|
47
36
|
bounds
|
|
@@ -164,11 +153,8 @@ const findLabel = (
|
|
|
164
153
|
return undefined;
|
|
165
154
|
};
|
|
166
155
|
|
|
167
|
-
const takeArguments = (
|
|
168
|
-
|
|
169
|
-
count: number,
|
|
170
|
-
pool: Expression[],
|
|
171
|
-
) => (selector.args.length === 0 && pool.length > 0 ? pool.splice(0, count) : selector.args);
|
|
156
|
+
const takeArguments = (args: Expression[], count: number, pool: Expression[]) =>
|
|
157
|
+
args.length === 0 && pool.length > 0 ? pool.splice(0, count) : args;
|
|
172
158
|
|
|
173
159
|
const wrapLets = (body: Expression, lets: Definition[][], location: SourceLocation) =>
|
|
174
160
|
lets.reduceRight<Expression>(
|
|
@@ -221,7 +207,7 @@ const createApplication = (selection: Selection, body: Expression): Expression =
|
|
|
221
207
|
};
|
|
222
208
|
};
|
|
223
209
|
|
|
224
|
-
const applyDefinition = (definition: Definition, selection: Selection)
|
|
210
|
+
const applyDefinition = (definition: Definition, selection: Selection) => {
|
|
225
211
|
const location = selection.location;
|
|
226
212
|
const params =
|
|
227
213
|
definition.kind === "Operator"
|
|
@@ -267,11 +253,11 @@ const selectSubexpression = (
|
|
|
267
253
|
names.push(selector.name);
|
|
268
254
|
args.push(...selector.args);
|
|
269
255
|
const name = names.join("!");
|
|
270
|
-
const definition = local
|
|
256
|
+
const definition = local
|
|
257
|
+
? local.find((candidate) => getSourceName(candidate) === name)
|
|
258
|
+
: lookup(name);
|
|
271
259
|
if (definition) {
|
|
272
|
-
|
|
273
|
-
const given = args.length === 0 && pool.length > 0 ? pool.splice(0, arity) : args;
|
|
274
|
-
return { definition, args: given };
|
|
260
|
+
return { definition, args: takeArguments(args, getArity(definition), pool) };
|
|
275
261
|
}
|
|
276
262
|
state.index++;
|
|
277
263
|
}
|
|
@@ -281,7 +267,7 @@ const selectSubexpression = (
|
|
|
281
267
|
const enterDefinition = (local: Definition[] | undefined) => {
|
|
282
268
|
const found = findDefinition(local);
|
|
283
269
|
const definition = found.definition;
|
|
284
|
-
const arity = definition
|
|
270
|
+
const arity = getArity(definition);
|
|
285
271
|
const isLast = state.index === selectors.length - 1;
|
|
286
272
|
if (found.args.length !== arity && found.args.length !== 0) {
|
|
287
273
|
throw createError(`The operator ${definition.name} requires ${arity} arguments`, location);
|
|
@@ -298,13 +284,14 @@ const selectSubexpression = (
|
|
|
298
284
|
};
|
|
299
285
|
|
|
300
286
|
const followLabels = () => {
|
|
301
|
-
while (state.index < selectors.length
|
|
302
|
-
const selector = selectors[state.index]
|
|
287
|
+
while (state.index < selectors.length) {
|
|
288
|
+
const selector = selectors[state.index];
|
|
289
|
+
if (selector.kind !== "Name") return;
|
|
303
290
|
const found = state.current && findLabel(state.current, selector.name, []);
|
|
304
291
|
if (!found) throw createError(`Cannot find label ${selector.name}`, location);
|
|
305
292
|
selection.lets.push(...found.lets);
|
|
306
293
|
selection.params.push(...found.label.params.map((name) => ({ name, arity: 0 })));
|
|
307
|
-
selection.args.push(...takeArguments(selector, found.label.params.length, pool));
|
|
294
|
+
selection.args.push(...takeArguments(selector.args, found.label.params.length, pool));
|
|
308
295
|
state.current = found.label.body;
|
|
309
296
|
state.index++;
|
|
310
297
|
}
|
|
@@ -331,7 +318,7 @@ const selectSubexpression = (
|
|
|
331
318
|
if (binder && (selector.kind === "Bound" || selector.kind === "At")) {
|
|
332
319
|
selection.params.push(...binder.names.map((name) => ({ name, arity: 0 })));
|
|
333
320
|
if (selector.kind === "Bound") {
|
|
334
|
-
selection.args.push(...takeArguments(selector, binder.names.length, pool));
|
|
321
|
+
selection.args.push(...takeArguments(selector.args, binder.names.length, pool));
|
|
335
322
|
}
|
|
336
323
|
state.current = binder.body;
|
|
337
324
|
return;
|
|
@@ -372,8 +359,7 @@ const selectSubexpression = (
|
|
|
372
359
|
throw createError("A name selector here must be from a LET clause", location);
|
|
373
360
|
}
|
|
374
361
|
selection.lets.push(state.current.definitions);
|
|
375
|
-
const
|
|
376
|
-
const last = enterDefinition(definitions);
|
|
362
|
+
const last = enterDefinition(state.current.definitions);
|
|
377
363
|
if (last) {
|
|
378
364
|
return applyDefinition(last.definition, {
|
|
379
365
|
...selection,
|
|
@@ -407,10 +393,10 @@ const containsReference = (expression: Expression): boolean =>
|
|
|
407
393
|
(expression.kind === "Apply" && expression.operator.includes("!")) ||
|
|
408
394
|
getChildren(expression).some(containsReference);
|
|
409
395
|
|
|
410
|
-
|
|
396
|
+
const resolveSubexpressions = (expression: Expression, lookup: Lookup): Expression => {
|
|
411
397
|
if (!containsReference(expression)) return expression;
|
|
412
398
|
if (expression.kind === "Let") {
|
|
413
|
-
const local: Lookup = (name
|
|
399
|
+
const local: Lookup = (name) =>
|
|
414
400
|
expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
|
|
415
401
|
return mapChildren(expression, (child) => resolveSubexpressions(child, local));
|
|
416
402
|
}
|
|
@@ -446,10 +432,10 @@ export const resolveInstanceSubexpressions = (instance: Instance, module: Module
|
|
|
446
432
|
|
|
447
433
|
export const resolveModuleSubexpressions = (module: Module, available: Definition[]): Module => {
|
|
448
434
|
const lookup = createLookup([...available, ...module.definitions]);
|
|
449
|
-
const
|
|
435
|
+
const hasReferences =
|
|
450
436
|
module.definitions.some((definition) => containsReference(definition.body)) ||
|
|
451
437
|
module.assumptions.some((assumption) => containsReference(assumption.expression));
|
|
452
|
-
if (!
|
|
438
|
+
if (!hasReferences) return module;
|
|
453
439
|
return {
|
|
454
440
|
...module,
|
|
455
441
|
definitions: module.definitions.map((definition) =>
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import type { Declaration, Definition, Expression, SourceLocation } from "./ast";
|
|
2
|
+
import { TlaSyntaxError } from "./errors";
|
|
3
|
+
import {
|
|
4
|
+
createLevelAnalyzer,
|
|
5
|
+
getArgumentKey,
|
|
6
|
+
MAX_LEVEL,
|
|
7
|
+
parseArgumentParamKey,
|
|
8
|
+
putArgConstraint,
|
|
9
|
+
putConstraint,
|
|
10
|
+
type OperatorLevels,
|
|
11
|
+
type Scope,
|
|
12
|
+
} from "./levels";
|
|
13
|
+
|
|
14
|
+
export interface InstanceCheck {
|
|
15
|
+
module: string;
|
|
16
|
+
instancer: string;
|
|
17
|
+
lets: Definition[];
|
|
18
|
+
substitutions: Map<string, Expression>;
|
|
19
|
+
location: SourceLocation;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface ModuleContext {
|
|
23
|
+
constants: Declaration[];
|
|
24
|
+
variables: string[];
|
|
25
|
+
definitions: Definition[];
|
|
26
|
+
assumptions: Expression[];
|
|
27
|
+
getDefinition: (name: string) => Definition | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ModuleLevels {
|
|
31
|
+
isConstant: boolean;
|
|
32
|
+
constraints: Map<string, number>;
|
|
33
|
+
argConstraints: Map<string, number>;
|
|
34
|
+
argParams: Set<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface Substituted {
|
|
38
|
+
name: string;
|
|
39
|
+
maximumLevel: number;
|
|
40
|
+
arity: number;
|
|
41
|
+
level: number;
|
|
42
|
+
operator: OperatorLevels | undefined;
|
|
43
|
+
isLeibniz: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const getMaxLevel = (operator: OperatorLevels, index: number) =>
|
|
47
|
+
operator.maxLevels[index] ?? MAX_LEVEL;
|
|
48
|
+
|
|
49
|
+
export const checkInstances = (
|
|
50
|
+
checks: InstanceCheck[],
|
|
51
|
+
getModule: (name: string) => ModuleContext,
|
|
52
|
+
) => {
|
|
53
|
+
const analyzers = new Map<string, ReturnType<typeof createLevelAnalyzer>>();
|
|
54
|
+
const getAnalyzer = (name: string) => {
|
|
55
|
+
const cached = analyzers.get(name);
|
|
56
|
+
if (cached) return cached;
|
|
57
|
+
const module = getModule(name);
|
|
58
|
+
const analyzer = createLevelAnalyzer({
|
|
59
|
+
constants: new Set(module.constants.map((constant) => constant.name)),
|
|
60
|
+
variables: new Set(module.variables),
|
|
61
|
+
getDefinition: module.getDefinition,
|
|
62
|
+
});
|
|
63
|
+
analyzers.set(name, analyzer);
|
|
64
|
+
return analyzer;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const moduleLevels = new Map<string, ModuleLevels>();
|
|
68
|
+
const getModuleLevels = (name: string) => {
|
|
69
|
+
const cached = moduleLevels.get(name);
|
|
70
|
+
if (cached) return cached;
|
|
71
|
+
const module = getModule(name);
|
|
72
|
+
const analyzer = getAnalyzer(name);
|
|
73
|
+
const definitions = module.definitions.map((definition) =>
|
|
74
|
+
analyzer.analyzeDefinition(definition, new Map()),
|
|
75
|
+
);
|
|
76
|
+
const levels: ModuleLevels = {
|
|
77
|
+
isConstant:
|
|
78
|
+
module.variables.length === 0 && definitions.every((definition) => definition.level === 0),
|
|
79
|
+
constraints: new Map(),
|
|
80
|
+
argConstraints: new Map(),
|
|
81
|
+
argParams: new Set(),
|
|
82
|
+
};
|
|
83
|
+
if (!levels.isConstant) {
|
|
84
|
+
module.constants.forEach((constant) => putConstraint(levels.constraints, constant.name, 0));
|
|
85
|
+
}
|
|
86
|
+
const addLevels = (
|
|
87
|
+
data: Omit<ModuleLevels, "isConstant">,
|
|
88
|
+
isKept: (operator: string, param: string) => boolean,
|
|
89
|
+
) => {
|
|
90
|
+
data.constraints.forEach((value, key) => putConstraint(levels.constraints, key, value));
|
|
91
|
+
data.argConstraints.forEach((value, key) =>
|
|
92
|
+
putArgConstraint(levels.argConstraints, key, value),
|
|
93
|
+
);
|
|
94
|
+
data.argParams.forEach((key) => {
|
|
95
|
+
const parameter = parseArgumentParamKey(key);
|
|
96
|
+
if (isKept(parameter.operator, parameter.param)) levels.argParams.add(key);
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
definitions.forEach((definition) =>
|
|
100
|
+
addLevels(definition, (operator, param) =>
|
|
101
|
+
definition.formals.every((formal) => formal.name !== operator && formal.name !== param),
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
module.assumptions.forEach((assumption) =>
|
|
105
|
+
addLevels(analyzer.analyze(assumption, new Map()), () => true),
|
|
106
|
+
);
|
|
107
|
+
moduleLevels.set(name, levels);
|
|
108
|
+
return levels;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const messages = new Set<string>();
|
|
112
|
+
checks.forEach((check) => {
|
|
113
|
+
const location = `at line ${check.location.line}, column ${check.location.column}`;
|
|
114
|
+
const addMessage = (message: string) => messages.add(`${message} ${location}`);
|
|
115
|
+
const instancee = getModule(check.module);
|
|
116
|
+
const levels = getModuleLevels(check.module);
|
|
117
|
+
const analyzer = getAnalyzer(check.instancer);
|
|
118
|
+
const scope: Scope = new Map();
|
|
119
|
+
check.lets.forEach((definition) =>
|
|
120
|
+
scope.set(definition.name, { kind: "Let", definition, scope }),
|
|
121
|
+
);
|
|
122
|
+
const declarations = [
|
|
123
|
+
...instancee.constants.map((constant) => ({ ...constant, maximumLevel: 0 })),
|
|
124
|
+
...instancee.variables.map((variable) => ({ name: variable, arity: 0, maximumLevel: 1 })),
|
|
125
|
+
];
|
|
126
|
+
const substituted = declarations.flatMap((declaration): Substituted[] => {
|
|
127
|
+
const expression = check.substitutions.get(declaration.name);
|
|
128
|
+
if (!expression) return [];
|
|
129
|
+
if (declaration.arity === 0) {
|
|
130
|
+
const data = analyzer.analyze(expression, scope);
|
|
131
|
+
return [{ ...declaration, level: data.level, operator: undefined, isLeibniz: true }];
|
|
132
|
+
}
|
|
133
|
+
const operand = analyzer.toOperand(expression, scope);
|
|
134
|
+
return [
|
|
135
|
+
{
|
|
136
|
+
...declaration,
|
|
137
|
+
level: operand.data.level,
|
|
138
|
+
operator: operand.operator,
|
|
139
|
+
isLeibniz: operand.operator === undefined || operand.operator.nonLeibniz.size === 0,
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
});
|
|
143
|
+
const getLevelMessage = (name: string, level: number) =>
|
|
144
|
+
`Level error in instantiating module '${check.module}': the level of the expression or operator substituted for '${name}' must be at most ${level}`;
|
|
145
|
+
substituted.forEach((item) => {
|
|
146
|
+
if (!levels.isConstant && item.level > item.maximumLevel) {
|
|
147
|
+
addMessage(getLevelMessage(item.name, item.maximumLevel));
|
|
148
|
+
}
|
|
149
|
+
if (!item.isLeibniz) {
|
|
150
|
+
addMessage(
|
|
151
|
+
`Error in instantiating module '${check.module}': a non-Leibniz operator substituted for '${item.name}'`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
substituted.forEach((item) => {
|
|
156
|
+
const maximum = levels.constraints.get(item.name);
|
|
157
|
+
if (maximum !== undefined && item.level > maximum)
|
|
158
|
+
addMessage(getLevelMessage(item.name, maximum));
|
|
159
|
+
const operator = item.operator;
|
|
160
|
+
if (!operator) return;
|
|
161
|
+
Array.from({ length: item.arity }, (_, index) => {
|
|
162
|
+
const minimum = levels.argConstraints.get(getArgumentKey(item.name, index));
|
|
163
|
+
if (minimum !== undefined && getMaxLevel(operator, index) < minimum) {
|
|
164
|
+
addMessage(
|
|
165
|
+
`Level error in instantiating module '${check.module}': the level of the argument ${index + 1} of the operator ${operator.name} must be at least ${minimum}`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
levels.argParams.forEach((key) => {
|
|
171
|
+
const parameter = parseArgumentParamKey(key);
|
|
172
|
+
const operator = substituted.find((item) => item.name === parameter.operator)?.operator;
|
|
173
|
+
const argument = substituted.find((item) => item.name === parameter.param);
|
|
174
|
+
if (!operator || !argument) return;
|
|
175
|
+
const maximum = getMaxLevel(operator, parameter.index);
|
|
176
|
+
if (argument.level > maximum) {
|
|
177
|
+
addMessage(
|
|
178
|
+
`Level error when instantiating module '${check.module}': the level of the argument ${parameter.index} of the operator ${parameter.operator}' must be at most ${maximum}`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
if (messages.size > 0) throw new TlaSyntaxError([...messages].join("\n"));
|
|
184
|
+
};
|