tla-ts 0.0.2 → 0.0.3-dev.07eab97
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 +98 -15
- package/dist/index.d.ts +98 -15
- 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 +23 -2
- package/dist/run-cli.js +23 -2
- package/dist/transfer.cjs +36 -16
- package/dist/transfer.js +36 -16
- package/package.json +3 -1
- package/src/ast.ts +139 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +555 -107
- package/src/cli-options.ts +64 -0
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +156 -48
- package/src/errors.ts +123 -3
- package/src/evaluate.ts +258 -160
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +48 -12
- package/src/instances.ts +16 -15
- package/src/levels.ts +181 -45
- package/src/lexer.ts +11 -2
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- 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 +168 -212
- package/src/parallel.ts +78 -37
- package/src/parser.ts +350 -63
- package/src/resolve.ts +124 -44
- package/src/run-cli.ts +405 -45
- package/src/runtime.ts +3 -1
- package/src/spec-description.ts +19 -8
- package/src/standard-levels.ts +3 -3
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +10 -10
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +183 -39
- package/src/symmetry.ts +30 -8
- 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/transfer.ts +22 -1
- package/src/value-sources.ts +29 -0
- package/src/values.ts +300 -83
package/src/standard-modules.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
getNumber,
|
|
12
12
|
getOperator,
|
|
13
13
|
getSet,
|
|
14
|
-
|
|
14
|
+
getArithmeticSignature,
|
|
15
15
|
getJavaInteger,
|
|
16
16
|
getValue,
|
|
17
17
|
isOutOfRange,
|
|
@@ -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) =>
|
|
@@ -128,7 +123,7 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
128
123
|
),
|
|
129
124
|
defineOperator("\\subseteq", 2, (args) => {
|
|
130
125
|
const left = getSet(args, 0, "\\subseteq");
|
|
131
|
-
const right =
|
|
126
|
+
const right = getValue(args, 1, "\\subseteq");
|
|
132
127
|
const isPowerSets =
|
|
133
128
|
typeof left === "object" &&
|
|
134
129
|
left.kind === "Subset" &&
|
|
@@ -140,8 +135,8 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
140
135
|
}),
|
|
141
136
|
...SET_OPERATORS.map((operator) =>
|
|
142
137
|
defineOperator(operator, 2, (args) => {
|
|
143
|
-
const left =
|
|
144
|
-
const right =
|
|
138
|
+
const left = getValue(args, 0, operator);
|
|
139
|
+
const right = getValue(args, 1, operator);
|
|
145
140
|
if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
|
|
146
141
|
if (operator === "\\cup" && isEmptySet(left)) return right;
|
|
147
142
|
const merge =
|
|
@@ -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);
|
|
@@ -184,51 +185,59 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
184
185
|
}),
|
|
185
186
|
];
|
|
186
187
|
|
|
187
|
-
const
|
|
188
|
+
const NEG_SIGNATURE = getArithmeticSignature("Integers", "Neg", "IntValue", 1);
|
|
188
189
|
|
|
189
|
-
|
|
190
|
+
// TLC overrides the operators of Naturals with the Java class of Integers when a spec loads
|
|
191
|
+
// Integers, which names it in their errors.
|
|
192
|
+
const getArithmeticOperators = (javaClass: "Naturals" | "Integers") => {
|
|
193
|
+
const dotDotSignature = getArithmeticSignature(javaClass, "DotDot", "IntervalValue", 2);
|
|
194
|
+
return [
|
|
195
|
+
defineArithmetic(javaClass, "+", "Plus", (left, right) => left + right),
|
|
196
|
+
defineArithmetic(javaClass, "-", "Minus", (left, right) => left - right),
|
|
197
|
+
defineArithmetic(javaClass, "*", "Times", (left, right) => left * right),
|
|
198
|
+
defineArithmetic(javaClass, "^", "Expt", (base, exponent) => {
|
|
199
|
+
if (exponent < 0) {
|
|
200
|
+
throw new TlaModuleError(
|
|
201
|
+
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
205
|
+
return base ** exponent;
|
|
206
|
+
}),
|
|
207
|
+
defineArithmetic(javaClass, "\\div", "Divide", (left, right) => {
|
|
208
|
+
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
209
|
+
const quotient = Math.floor(left / right);
|
|
210
|
+
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
211
|
+
return quotient;
|
|
212
|
+
}),
|
|
213
|
+
defineArithmetic(javaClass, "%", "Mod", (left, right) => {
|
|
214
|
+
if (right <= 0) {
|
|
215
|
+
throw new TlaModuleError(
|
|
216
|
+
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return ((left % right) + right) % right;
|
|
220
|
+
}),
|
|
221
|
+
defineOperator("..", 2, (args) => ({
|
|
222
|
+
kind: "Interval",
|
|
223
|
+
low: getJavaInteger(getValue(args, 0, ".."), dotDotSignature),
|
|
224
|
+
high: getJavaInteger(getValue(args, 1, ".."), dotDotSignature),
|
|
225
|
+
})),
|
|
226
|
+
];
|
|
227
|
+
};
|
|
190
228
|
|
|
191
229
|
const NATURALS_OPERATORS = [
|
|
192
230
|
defineConstant("Nat", { kind: "InfiniteSet", name: "Nat" }),
|
|
193
|
-
|
|
194
|
-
defineArithmetic("-", "Minus", (left, right) => left - right),
|
|
195
|
-
defineArithmetic("*", "Times", (left, right) => left * right),
|
|
196
|
-
defineArithmetic("^", "Expt", (base, exponent) => {
|
|
197
|
-
if (exponent < 0) {
|
|
198
|
-
throw new TlaModuleError(
|
|
199
|
-
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
203
|
-
return base ** exponent;
|
|
204
|
-
}),
|
|
205
|
-
defineArithmetic("\\div", "Divide", (left, right) => {
|
|
206
|
-
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
207
|
-
const quotient = Math.floor(left / right);
|
|
208
|
-
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
209
|
-
return quotient;
|
|
210
|
-
}),
|
|
211
|
-
defineArithmetic("%", "Mod", (left, right) => {
|
|
212
|
-
if (right <= 0) {
|
|
213
|
-
throw new TlaModuleError(
|
|
214
|
-
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return ((left % right) + right) % right;
|
|
218
|
-
}),
|
|
231
|
+
...getArithmeticOperators("Naturals"),
|
|
219
232
|
defineComparison("<", (left, right) => left < right),
|
|
220
233
|
defineComparison(">", (left, right) => left > right),
|
|
221
234
|
defineComparison("<=", (left, right) => left <= right),
|
|
222
235
|
defineComparison(">=", (left, right) => left >= right),
|
|
223
|
-
defineOperator("..", 2, (args) => ({
|
|
224
|
-
kind: "Interval",
|
|
225
|
-
low: getJavaInteger(getValue(args, 0, ".."), DOT_DOT_SIGNATURE),
|
|
226
|
-
high: getJavaInteger(getValue(args, 1, ".."), DOT_DOT_SIGNATURE),
|
|
227
|
-
})),
|
|
228
236
|
];
|
|
229
237
|
|
|
230
238
|
const INTEGERS_OPERATORS = [
|
|
231
239
|
defineConstant("Int", { kind: "InfiniteSet", name: "Int" }),
|
|
240
|
+
...getArithmeticOperators("Integers"),
|
|
232
241
|
defineOperator("-.", 1, (args) => {
|
|
233
242
|
const value = getJavaInteger(getValue(args, 0, "-"), NEG_SIGNATURE);
|
|
234
243
|
if (isOutOfRange(-value)) throw createOverflowError(`-${value}`);
|
|
@@ -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
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Declaration, Definition, Expression, SourceLocation } from "./ast";
|
|
2
|
-
import {
|
|
1
|
+
import type { Declaration, Definition, Expression, SourceLocation, Theorem } from "./ast";
|
|
2
|
+
import { createSanyError, formatRange } from "./errors";
|
|
3
3
|
import {
|
|
4
|
+
ACTION_LEVEL,
|
|
5
|
+
analyzeAssumeProve,
|
|
4
6
|
createLevelAnalyzer,
|
|
5
7
|
getArgumentKey,
|
|
6
8
|
MAX_LEVEL,
|
|
@@ -24,6 +26,7 @@ interface ModuleContext {
|
|
|
24
26
|
variables: string[];
|
|
25
27
|
definitions: Definition[];
|
|
26
28
|
assumptions: Expression[];
|
|
29
|
+
theorems: Theorem[];
|
|
27
30
|
getDefinition: (name: string) => Definition | undefined;
|
|
28
31
|
}
|
|
29
32
|
|
|
@@ -46,10 +49,7 @@ interface Substituted {
|
|
|
46
49
|
const getMaxLevel = (operator: OperatorLevels, index: number) =>
|
|
47
50
|
operator.maxLevels[index] ?? MAX_LEVEL;
|
|
48
51
|
|
|
49
|
-
export const
|
|
50
|
-
checks: InstanceCheck[],
|
|
51
|
-
getModule: (name: string) => ModuleContext,
|
|
52
|
-
) => {
|
|
52
|
+
export const createInstanceChecker = (getModule: (name: string) => ModuleContext) => {
|
|
53
53
|
const analyzers = new Map<string, ReturnType<typeof createLevelAnalyzer>>();
|
|
54
54
|
const getAnalyzer = (name: string) => {
|
|
55
55
|
const cached = analyzers.get(name);
|
|
@@ -101,17 +101,38 @@ export const checkInstances = (
|
|
|
101
101
|
definition.formals.every((formal) => formal.name !== operator && formal.name !== param),
|
|
102
102
|
),
|
|
103
103
|
);
|
|
104
|
+
const addAssumedLevels = (data: ReturnType<typeof analyzeAssumeProve>) => {
|
|
105
|
+
addLevels(data, () => true);
|
|
106
|
+
module.constants.forEach((constant) => {
|
|
107
|
+
if (data.params.has(constant.name)) {
|
|
108
|
+
putConstraint(levels.constraints, constant.name, ACTION_LEVEL);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
};
|
|
104
112
|
module.assumptions.forEach((assumption) =>
|
|
105
|
-
|
|
113
|
+
addAssumedLevels(analyzer.analyze(assumption, new Map())),
|
|
114
|
+
);
|
|
115
|
+
module.theorems.forEach((theorem) =>
|
|
116
|
+
theorem.statement.kind === "AssumeProve"
|
|
117
|
+
? addAssumedLevels(analyzeAssumeProve(theorem.statement, new Map(), analyzer))
|
|
118
|
+
: addLevels(analyzer.analyze(theorem.statement, new Map()), () => true),
|
|
106
119
|
);
|
|
107
120
|
moduleLevels.set(name, levels);
|
|
108
121
|
return levels;
|
|
109
122
|
};
|
|
110
123
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
124
|
+
return (check: InstanceCheck) => {
|
|
125
|
+
const errors = new Map<string, { location: string; message: string }>();
|
|
126
|
+
// SANY reports each error at the whole instance.
|
|
127
|
+
const location = formatRange(
|
|
128
|
+
check.location.range ?? {
|
|
129
|
+
module: check.instancer,
|
|
130
|
+
start: check.location,
|
|
131
|
+
end: check.location,
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
const addMessage = (message: string) =>
|
|
135
|
+
errors.set(`${location}\n${message}`, { location, message });
|
|
115
136
|
const instancee = getModule(check.module);
|
|
116
137
|
const levels = getModuleLevels(check.module);
|
|
117
138
|
const analyzer = getAnalyzer(check.instancer);
|
|
@@ -141,14 +162,14 @@ export const checkInstances = (
|
|
|
141
162
|
];
|
|
142
163
|
});
|
|
143
164
|
const getLevelMessage = (name: string, level: number) =>
|
|
144
|
-
`Level error in instantiating module '${check.module}'
|
|
165
|
+
`Level error in instantiating module '${check.module}':\nThe level of the expression or operator substituted for '${name}' \nmust be at most ${level}.`;
|
|
145
166
|
substituted.forEach((item) => {
|
|
146
167
|
if (!levels.isConstant && item.level > item.maximumLevel) {
|
|
147
168
|
addMessage(getLevelMessage(item.name, item.maximumLevel));
|
|
148
169
|
}
|
|
149
170
|
if (!item.isLeibniz) {
|
|
150
171
|
addMessage(
|
|
151
|
-
`Error in instantiating module '${check.module}'
|
|
172
|
+
`Error in instantiating module '${check.module}':\n A non-Leibniz operator substituted for '${item.name}'.`,
|
|
152
173
|
);
|
|
153
174
|
}
|
|
154
175
|
});
|
|
@@ -162,7 +183,7 @@ export const checkInstances = (
|
|
|
162
183
|
const minimum = levels.argConstraints.get(getArgumentKey(item.name, index));
|
|
163
184
|
if (minimum !== undefined && getMaxLevel(operator, index) < minimum) {
|
|
164
185
|
addMessage(
|
|
165
|
-
`Level error in instantiating module '${check.module}'
|
|
186
|
+
`Level error in instantiating module '${check.module}':\nThe level of the argument ${index + 1} of the operator ${operator.name} \nmust be at least ${minimum}.`,
|
|
166
187
|
);
|
|
167
188
|
}
|
|
168
189
|
});
|
|
@@ -175,10 +196,10 @@ export const checkInstances = (
|
|
|
175
196
|
const maximum = getMaxLevel(operator, parameter.index);
|
|
176
197
|
if (argument.level > maximum) {
|
|
177
198
|
addMessage(
|
|
178
|
-
`Level error when instantiating module '${check.module}'
|
|
199
|
+
`Level error when instantiating module '${check.module}':\nThe level of the argument ${parameter.index} of the operator ${parameter.operator}' \nmust be at most ${maximum}.`,
|
|
179
200
|
);
|
|
180
201
|
}
|
|
181
202
|
});
|
|
182
|
-
|
|
183
|
-
|
|
203
|
+
if (errors.size > 0) throw createSanyError([...errors.values()]);
|
|
204
|
+
};
|
|
184
205
|
};
|