tla-ts 0.0.3-dev.3c70a97 → 0.0.3-dev.3ce2f00
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +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 -17
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +33 -17
- package/dist/transfer.js +33 -17
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/builtins.ts +9 -3
- package/src/check.ts +455 -93
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +115 -19
- package/src/errors.ts +78 -1
- package/src/evaluate.ts +182 -26
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +4 -4
- package/src/levels.ts +82 -36
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +117 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +5 -2
- package/src/parser.ts +57 -16
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +103 -18
- package/src/runtime.ts +2 -0
- package/src/spec-description.ts +12 -6
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/substitution-levels.ts +35 -11
- package/src/successors.ts +169 -33
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +27 -2
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +242 -61
package/src/liveness.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
|
-
import {
|
|
2
|
+
import { STACK_OVERFLOW, TlaEvaluationError, TlaModuleError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
3
|
+
import { EXIT_STATUS, getOwnExitCode, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
describeLocation,
|
|
6
|
+
describeRange,
|
|
5
7
|
bindDefinitions,
|
|
6
8
|
bindOperatorArguments,
|
|
7
9
|
evaluate,
|
|
@@ -36,6 +38,10 @@ export interface ScopedExpression {
|
|
|
36
38
|
scope: Scope | undefined;
|
|
37
39
|
}
|
|
38
40
|
|
|
41
|
+
export interface NamedExpression extends ScopedExpression {
|
|
42
|
+
name: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
39
45
|
export interface TemporalProperty extends ScopedExpression {
|
|
40
46
|
name: string;
|
|
41
47
|
}
|
|
@@ -55,6 +61,23 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
|
|
|
55
61
|
next: undefined,
|
|
56
62
|
});
|
|
57
63
|
|
|
64
|
+
// TLC evaluates the constant parts of a property while it processes the spec. Its top-level handler
|
|
65
|
+
// prints a failure without a TLC error code of its own as an unexpected exception.
|
|
66
|
+
const evaluateConstant = (expression: Expression, context: Context) => {
|
|
67
|
+
try {
|
|
68
|
+
return evaluate(expression, context);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (!(error instanceof TlaEvaluationError) || error instanceof TlaModuleError) throw error;
|
|
71
|
+
const code = getOwnExitCode(error);
|
|
72
|
+
throw code === undefined
|
|
73
|
+
? setExitCode(
|
|
74
|
+
new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
|
|
75
|
+
EXIT_STATUS.error,
|
|
76
|
+
)
|
|
77
|
+
: setExitCode(error, code);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
58
81
|
const createCannotHandleError = (expression: Expression) =>
|
|
59
82
|
new TlaEvaluationError(
|
|
60
83
|
`TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -144,21 +167,27 @@ const getDefinitionLevel = (definition: Definition, environment: Environment) =>
|
|
|
144
167
|
return level;
|
|
145
168
|
};
|
|
146
169
|
|
|
170
|
+
export const TAUTOLOGY_MESSAGE = "Temporal formula is a tautology (its negation is unsatisfiable).";
|
|
171
|
+
|
|
172
|
+
// TLC names each part of a property after the operator it expands, or after the location of the
|
|
173
|
+
// conjunct or quantifier body it splits off.
|
|
147
174
|
export const expandTemporal = (
|
|
148
175
|
expression: Expression,
|
|
149
176
|
scope: Scope | undefined,
|
|
150
177
|
environment: Environment,
|
|
151
|
-
|
|
178
|
+
isProperty: boolean,
|
|
179
|
+
name = "",
|
|
152
180
|
expanding = new Set<Definition>(),
|
|
153
|
-
):
|
|
181
|
+
): NamedExpression[] => {
|
|
154
182
|
const expand = (
|
|
155
183
|
child: Expression,
|
|
156
184
|
childScope: Scope | undefined,
|
|
185
|
+
childName = name,
|
|
157
186
|
childExpanding = expanding,
|
|
158
|
-
):
|
|
159
|
-
expandTemporal(child, childScope, environment,
|
|
187
|
+
): NamedExpression[] =>
|
|
188
|
+
expandTemporal(child, childScope, environment, isProperty, childName, childExpanding);
|
|
160
189
|
if (expression.kind === "And") {
|
|
161
|
-
return expression.operands.flatMap((operand) => expand(operand, scope));
|
|
190
|
+
return expression.operands.flatMap((operand) => expand(operand, scope, describeRange(operand)));
|
|
162
191
|
}
|
|
163
192
|
if (expression.kind === "Label") return expand(expression.body, scope);
|
|
164
193
|
if (expression.kind === "Let") {
|
|
@@ -179,35 +208,64 @@ export const expandTemporal = (
|
|
|
179
208
|
getDefinitionLevel(definition, environment) > 0
|
|
180
209
|
) {
|
|
181
210
|
if (expanding.has(definition)) {
|
|
182
|
-
throw new
|
|
211
|
+
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
183
212
|
}
|
|
184
|
-
|
|
213
|
+
const expanded = expand(
|
|
214
|
+
definition.body,
|
|
215
|
+
undefined,
|
|
216
|
+
expression.name,
|
|
217
|
+
new Set([...expanding, definition]),
|
|
218
|
+
);
|
|
219
|
+
// TLC takes the body of a state-level definition in a specification as one initial predicate.
|
|
220
|
+
return !isProperty && getDefinitionLevel(definition, environment) < ACTION_LEVEL
|
|
221
|
+
? [{ expression: definition.body, scope: undefined, name: expression.name }]
|
|
222
|
+
: expanded;
|
|
185
223
|
}
|
|
186
224
|
}
|
|
187
225
|
const context = getConstantContext(environment, scope);
|
|
188
|
-
|
|
226
|
+
// A property expands every operator it applies and unrolls every \A over constant sets, but a
|
|
227
|
+
// specification only expands the temporal ones.
|
|
228
|
+
const isExpanded = (child: Expression) =>
|
|
229
|
+
isProperty || getLevel(child, scope, environment) === TEMPORAL_LEVEL;
|
|
230
|
+
if (expression.kind === "Apply" && isExpanded(expression)) {
|
|
189
231
|
const operator = resolveApplyOperator(expression, context);
|
|
190
232
|
const definition = isLocallyResolved(expression)
|
|
191
233
|
? undefined
|
|
192
234
|
: getDefinition(expression.operator, environment);
|
|
193
|
-
|
|
194
|
-
|
|
235
|
+
const isExpandable =
|
|
236
|
+
operator?.kind === "Closure" &&
|
|
237
|
+
operator.params.length === expression.args.length &&
|
|
238
|
+
!definition?.isRecursive;
|
|
239
|
+
if (isExpandable) {
|
|
240
|
+
return expand(
|
|
241
|
+
operator.body,
|
|
242
|
+
bindOperatorArguments(operator, expression.args, context),
|
|
243
|
+
expression.operator,
|
|
244
|
+
);
|
|
195
245
|
}
|
|
196
246
|
}
|
|
197
247
|
const isUnrolled =
|
|
198
|
-
|
|
248
|
+
isProperty &&
|
|
199
249
|
expression.kind === "Forall" &&
|
|
200
|
-
getLevel(expression.body, scope, environment) === TEMPORAL_LEVEL &&
|
|
201
250
|
expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === 0);
|
|
202
251
|
if (isUnrolled) {
|
|
203
|
-
const expanded:
|
|
252
|
+
const expanded: NamedExpression[] = [];
|
|
253
|
+
const bindings = { count: 0 };
|
|
204
254
|
forEachBinding(expression.bounds, context, (boundScope) => {
|
|
205
|
-
|
|
255
|
+
bindings.count++;
|
|
256
|
+
expanded.push(...expand(expression.body, boundScope, describeRange(expression.body)));
|
|
206
257
|
return false;
|
|
207
258
|
});
|
|
259
|
+
// TLC rejects a property that quantifies over no elements before it checks anything.
|
|
260
|
+
if (bindings.count === 0) {
|
|
261
|
+
throw setExitCode(
|
|
262
|
+
new TlaEvaluationError(TAUTOLOGY_MESSAGE),
|
|
263
|
+
EXIT_STATUS.livenessEvaluationFailure,
|
|
264
|
+
);
|
|
265
|
+
}
|
|
208
266
|
return expanded;
|
|
209
267
|
}
|
|
210
|
-
return [{ expression, scope }];
|
|
268
|
+
return [{ expression, scope, name }];
|
|
211
269
|
};
|
|
212
270
|
|
|
213
271
|
const bindLetDefinitions = (
|
|
@@ -241,7 +299,7 @@ export const toLiveExpression = (
|
|
|
241
299
|
): LiveExpression => {
|
|
242
300
|
const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
|
|
243
301
|
if (atomLevel === 0) {
|
|
244
|
-
const value =
|
|
302
|
+
const value = evaluateConstant(expression, getConstantContext(environment, scope));
|
|
245
303
|
if (typeof value !== "boolean") {
|
|
246
304
|
throw new TlaEvaluationError(
|
|
247
305
|
`TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -262,9 +320,7 @@ export const toLiveExpression = (
|
|
|
262
320
|
scope: Scope | undefined,
|
|
263
321
|
) => {
|
|
264
322
|
if (expansion.depth >= MAXIMUM_EXPANSION_DEPTH) {
|
|
265
|
-
throw new TlaEvaluationError(
|
|
266
|
-
`TLC overflowed the stack while expanding the temporal formula at ${describeLocation(expression)}`,
|
|
267
|
-
);
|
|
323
|
+
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
268
324
|
}
|
|
269
325
|
expansion.depth++;
|
|
270
326
|
const result = convert(body, bodyScope);
|
|
@@ -319,9 +375,45 @@ export const toLiveExpression = (
|
|
|
319
375
|
return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
|
|
320
376
|
};
|
|
321
377
|
|
|
378
|
+
// TLC translates the operands of a connective even when the whole formula is a state or action
|
|
379
|
+
// predicate, so it fails on a constant operand before it checks any state.
|
|
380
|
+
const convertOperands = (expression: Expression, scope: Scope | undefined) => {
|
|
381
|
+
switch (expression.kind) {
|
|
382
|
+
case "Label":
|
|
383
|
+
convert(expression.body, scope);
|
|
384
|
+
return;
|
|
385
|
+
case "Let":
|
|
386
|
+
convert(expression.body, bindLetDefinitions(expression, scope, environment));
|
|
387
|
+
return;
|
|
388
|
+
case "And":
|
|
389
|
+
case "Or":
|
|
390
|
+
for (const operand of expression.operands) convert(operand, scope);
|
|
391
|
+
return;
|
|
392
|
+
case "Apply":
|
|
393
|
+
if (
|
|
394
|
+
expression.operator === "=>" ||
|
|
395
|
+
(expression.operator === "~" && !isLocallyResolved(expression))
|
|
396
|
+
) {
|
|
397
|
+
for (const operand of expression.args) convert(operand, scope);
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
400
|
+
case "If": {
|
|
401
|
+
const condition = convert(expression.condition, scope);
|
|
402
|
+
const branches =
|
|
403
|
+
condition.kind === "Boolean"
|
|
404
|
+
? [condition.value ? expression.consequent : expression.alternative]
|
|
405
|
+
: [expression.consequent, expression.alternative];
|
|
406
|
+
for (const branch of branches) convert(branch, scope);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
322
411
|
const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
|
|
323
412
|
const level = getLevel(expression, scope, environment);
|
|
324
|
-
if (level <= ACTION_LEVEL)
|
|
413
|
+
if (level <= ACTION_LEVEL) {
|
|
414
|
+
convertOperands(expression, scope);
|
|
415
|
+
return toAtom(expression, scope, level);
|
|
416
|
+
}
|
|
325
417
|
switch (expression.kind) {
|
|
326
418
|
case "Label":
|
|
327
419
|
return convert(expression.body, scope);
|
|
@@ -364,7 +456,10 @@ export const toLiveExpression = (
|
|
|
364
456
|
return convertQuantifier(expression, scope);
|
|
365
457
|
case "If": {
|
|
366
458
|
if (getLevel(expression.condition, scope, environment) === 0) {
|
|
367
|
-
const condition =
|
|
459
|
+
const condition = evaluateConstant(
|
|
460
|
+
expression.condition,
|
|
461
|
+
getConstantContext(environment, scope),
|
|
462
|
+
);
|
|
368
463
|
if (typeof condition === "boolean") {
|
|
369
464
|
return convert(condition ? expression.consequent : expression.alternative, scope);
|
|
370
465
|
}
|
package/src/parallel-worker.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
WorkerMessage,
|
|
16
16
|
} from "./parallel";
|
|
17
17
|
import { getSuccessors, type Successor } from "./successors";
|
|
18
|
-
import { getTlcStringOrder,
|
|
18
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
19
19
|
import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
20
20
|
import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
|
|
21
21
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
@@ -61,7 +61,7 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
61
61
|
});
|
|
62
62
|
const environment = specification.environment;
|
|
63
63
|
const search = createSearch(start.config, environment, () =>
|
|
64
|
-
|
|
64
|
+
getTlcDeclarationOrder(start.source, loadModule, "variable"),
|
|
65
65
|
);
|
|
66
66
|
const nextAction = search?.spec.next;
|
|
67
67
|
if (!search || nextAction === undefined)
|
package/src/parallel.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
type SharedFingerprints,
|
|
21
21
|
} from "./fingerprints";
|
|
22
22
|
import { getInitialStates } from "./successors";
|
|
23
|
-
import { getTlcStringOrder,
|
|
23
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
24
24
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
25
25
|
import { createStateEncoder, type EncodedStates } from "./transfer";
|
|
26
26
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
@@ -114,9 +114,12 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
114
114
|
});
|
|
115
115
|
};
|
|
116
116
|
const count = options.workers ?? 1;
|
|
117
|
+
// TLC's workers write the -dump state graph in whichever order they find it, so one worker
|
|
118
|
+
// writes the graph a single TLC worker does.
|
|
117
119
|
const isSequential =
|
|
118
120
|
count < 2 ||
|
|
119
121
|
typeof SharedArrayBuffer !== "function" ||
|
|
122
|
+
options.dump !== undefined ||
|
|
120
123
|
config.view !== undefined ||
|
|
121
124
|
config.postconditions.length > 0 ||
|
|
122
125
|
config.possible.length > 0;
|
|
@@ -147,7 +150,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
147
150
|
});
|
|
148
151
|
const environment = specification.environment;
|
|
149
152
|
const search = createSearch(config, environment, () =>
|
|
150
|
-
|
|
153
|
+
getTlcDeclarationOrder(options.source, loadModule, "variable"),
|
|
151
154
|
);
|
|
152
155
|
if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
|
|
153
156
|
if (
|
package/src/parser.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
StepBody,
|
|
23
23
|
UseOrHide,
|
|
24
24
|
} from "./ast";
|
|
25
|
-
import { TlaSyntaxError } from "./errors";
|
|
25
|
+
import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
|
|
26
26
|
import { getNumeralValue, tokenize, type Token } from "./lexer";
|
|
27
27
|
|
|
28
28
|
interface InfixOperator {
|
|
@@ -628,14 +628,24 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
628
628
|
const body = parseExpression();
|
|
629
629
|
return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
|
|
630
630
|
}
|
|
631
|
-
|
|
631
|
+
const argument = parseExpression();
|
|
632
|
+
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
633
|
+
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
634
|
+
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
635
|
+
return argument;
|
|
632
636
|
};
|
|
633
637
|
|
|
638
|
+
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
639
|
+
|
|
640
|
+
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
641
|
+
|
|
634
642
|
const parseArguments = () =>
|
|
635
643
|
withoutOffside(() => {
|
|
644
|
+
const start = cursor.index;
|
|
636
645
|
expect("(");
|
|
637
646
|
const args = parseSeparated(parseArgument);
|
|
638
647
|
expect(")");
|
|
648
|
+
lastArguments.range = getRange(start);
|
|
639
649
|
return args;
|
|
640
650
|
});
|
|
641
651
|
|
|
@@ -662,9 +672,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
662
672
|
if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
|
|
663
673
|
const kind = isAt("|->", 1) ? "Record" : "RecordSet";
|
|
664
674
|
const fields = parseSeparated(() => {
|
|
675
|
+
const fieldStart = cursor.index;
|
|
676
|
+
const fieldLocation = getLocation(peek());
|
|
665
677
|
const name = expectIdentifier();
|
|
678
|
+
fieldLocation.range = getRange(fieldStart);
|
|
666
679
|
expect(kind === "Record" ? "|->" : ":");
|
|
667
|
-
return { name, value: parseExpression() };
|
|
680
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
668
681
|
});
|
|
669
682
|
expect("]");
|
|
670
683
|
return { kind, fields, location };
|
|
@@ -823,7 +836,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
823
836
|
};
|
|
824
837
|
|
|
825
838
|
const parsePrimary = (): Expression => {
|
|
826
|
-
const
|
|
839
|
+
const start = cursor.index;
|
|
840
|
+
const isFactStart = start === factStart.index;
|
|
827
841
|
const token = advance();
|
|
828
842
|
const location = getLocation(token);
|
|
829
843
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -856,13 +870,18 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
856
870
|
const args = hasArguments ? parseArguments() : [];
|
|
857
871
|
if (!isSelectorAhead()) {
|
|
858
872
|
return hasArguments
|
|
859
|
-
? { kind: "Apply", operator: name, args, location }
|
|
873
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
860
874
|
: { kind: "Identifier", name, location };
|
|
861
875
|
}
|
|
862
876
|
const instance = openModules
|
|
863
877
|
.flatMap((module) => module.instances)
|
|
864
878
|
.find((candidate) => candidate.name === name);
|
|
865
|
-
|
|
879
|
+
const isMissingArguments =
|
|
880
|
+
instance !== undefined &&
|
|
881
|
+
!isFactStart &&
|
|
882
|
+
!isTlapm &&
|
|
883
|
+
instance.params.length !== args.length;
|
|
884
|
+
if (isMissingArguments && args.length > 0) {
|
|
866
885
|
throw createError(
|
|
867
886
|
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
868
887
|
token,
|
|
@@ -876,18 +895,36 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
876
895
|
const names = selectors.flatMap((selector) =>
|
|
877
896
|
selector.kind === "Name" ? [selector.name] : [],
|
|
878
897
|
);
|
|
879
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
880
898
|
const operator = names.join("!");
|
|
881
899
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
882
900
|
"args" in selector ? selector.args : [],
|
|
883
901
|
);
|
|
884
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
885
902
|
const argumentCounts = selectors.map((selector) =>
|
|
886
903
|
"args" in selector ? selector.args.length : 0,
|
|
887
904
|
);
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
905
|
+
const argumentsRange = lastArguments.range;
|
|
906
|
+
const reference: Expression =
|
|
907
|
+
names.length < selectors.length
|
|
908
|
+
? { kind: "Subexpression", selectors, location }
|
|
909
|
+
: qualifiedArgs.length === 0
|
|
910
|
+
? { kind: "Identifier", name: operator, location }
|
|
911
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
912
|
+
? {
|
|
913
|
+
kind: "Apply",
|
|
914
|
+
operator,
|
|
915
|
+
args: qualifiedArgs,
|
|
916
|
+
argumentCounts,
|
|
917
|
+
argumentsRange,
|
|
918
|
+
location,
|
|
919
|
+
}
|
|
920
|
+
: { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
|
|
921
|
+
if (isMissingArguments) {
|
|
922
|
+
missingArguments.set(reference, {
|
|
923
|
+
location: formatRange(getRange(start)),
|
|
924
|
+
message: `The operator ${name} requires ${instance.params.length} arguments.`,
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
return reference;
|
|
891
928
|
}
|
|
892
929
|
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
893
930
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
@@ -1423,11 +1460,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1423
1460
|
location: getLocation(keyword),
|
|
1424
1461
|
};
|
|
1425
1462
|
const invalid =
|
|
1426
|
-
use.kind === "Hide" && !isTlapm ? use.facts.
|
|
1427
|
-
if (invalid) {
|
|
1428
|
-
throw
|
|
1429
|
-
|
|
1430
|
-
|
|
1463
|
+
use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1464
|
+
if (invalid.length > 0) {
|
|
1465
|
+
throw createSanyError(
|
|
1466
|
+
invalid.map((fact) => ({
|
|
1467
|
+
location: formatRange(fact.location.range ?? getRange(start)),
|
|
1468
|
+
message:
|
|
1469
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1470
|
+
})),
|
|
1431
1471
|
);
|
|
1432
1472
|
}
|
|
1433
1473
|
return withRange(use, start);
|
|
@@ -1705,6 +1745,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1705
1745
|
end: { line: token.line, column: token.endColumn },
|
|
1706
1746
|
};
|
|
1707
1747
|
}
|
|
1748
|
+
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1708
1749
|
openModules.pop();
|
|
1709
1750
|
recursiveScopes.pop();
|
|
1710
1751
|
return module;
|
package/src/resolve.ts
CHANGED
|
@@ -13,9 +13,16 @@ import {
|
|
|
13
13
|
type Proof,
|
|
14
14
|
type ProofStep,
|
|
15
15
|
type SourceLocation,
|
|
16
|
+
type SourceRange,
|
|
16
17
|
type Theorem,
|
|
17
18
|
} from "./ast";
|
|
18
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
createSanyError,
|
|
21
|
+
createSemanticError,
|
|
22
|
+
formatRange,
|
|
23
|
+
TlaSyntaxError,
|
|
24
|
+
type SanyError,
|
|
25
|
+
} from "./errors";
|
|
19
26
|
|
|
20
27
|
export interface ModuleSymbol {
|
|
21
28
|
key: string;
|
|
@@ -103,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
|
103
110
|
: unit.imported.get(name);
|
|
104
111
|
};
|
|
105
112
|
|
|
113
|
+
// SANY reports a name at the range of its text.
|
|
114
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
115
|
+
module: location.range?.module ?? "",
|
|
116
|
+
start: location,
|
|
117
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
121
|
+
const getFixity = (expression: Expression) => {
|
|
122
|
+
if (expression.kind !== "Apply") return undefined;
|
|
123
|
+
if (expression.args.length === 2) return "infix";
|
|
124
|
+
if (expression.args.length !== 1) return undefined;
|
|
125
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
126
|
+
};
|
|
127
|
+
|
|
106
128
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
107
129
|
const isOperatorSymbol = (name: string) =>
|
|
108
130
|
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
@@ -135,7 +157,7 @@ const getSymbolArity = (
|
|
|
135
157
|
}
|
|
136
158
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
137
159
|
// arguments.
|
|
138
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
160
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
139
161
|
const isSelected =
|
|
140
162
|
prefix !== "" &&
|
|
141
163
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -144,11 +166,23 @@ const getSymbolArity = (
|
|
|
144
166
|
if (isSelected) {
|
|
145
167
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
146
168
|
}
|
|
147
|
-
const
|
|
148
|
-
throw
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
169
|
+
const range = getNameRange(location, name === "-." ? "-" : name);
|
|
170
|
+
if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
|
|
171
|
+
const missing = {
|
|
172
|
+
location: formatRange(range),
|
|
173
|
+
message: `Could not find declaration or definition of symbol '${name}'.`,
|
|
174
|
+
};
|
|
175
|
+
const fixity = getFixity(expression);
|
|
176
|
+
throw createSanyError(
|
|
177
|
+
fixity && location.range
|
|
178
|
+
? [
|
|
179
|
+
missing,
|
|
180
|
+
{
|
|
181
|
+
location: formatRange(location.range),
|
|
182
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
|
|
183
|
+
},
|
|
184
|
+
]
|
|
185
|
+
: [missing],
|
|
152
186
|
);
|
|
153
187
|
};
|
|
154
188
|
|
|
@@ -235,13 +269,13 @@ const resolveApply = (
|
|
|
235
269
|
scope: LocalName | undefined,
|
|
236
270
|
unit: Unit,
|
|
237
271
|
) => {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
272
|
+
const operator = expression.operator;
|
|
273
|
+
const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
|
|
274
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
242
275
|
if (arity !== expression.args.length) {
|
|
243
|
-
throw
|
|
244
|
-
`The operator ${
|
|
276
|
+
throw createSemanticError(
|
|
277
|
+
`The operator ${operator} requires ${arity} arguments.`,
|
|
278
|
+
expression.argumentsRange ?? getNameRange(expression.location, operator),
|
|
245
279
|
);
|
|
246
280
|
}
|
|
247
281
|
expression.args.forEach((arg, index) => {
|
|
@@ -275,10 +309,12 @@ const resolveExpression = (
|
|
|
275
309
|
case "ExceptAt":
|
|
276
310
|
return;
|
|
277
311
|
case "Identifier": {
|
|
278
|
-
const
|
|
312
|
+
const name = expression.name;
|
|
313
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
279
314
|
if (arity > 0) {
|
|
280
|
-
throw
|
|
281
|
-
`The operator ${
|
|
315
|
+
throw createSemanticError(
|
|
316
|
+
`The operator ${name} requires ${arity} arguments.`,
|
|
317
|
+
getNameRange(expression.location, name),
|
|
282
318
|
);
|
|
283
319
|
}
|
|
284
320
|
return;
|
|
@@ -347,14 +383,21 @@ const resolveExpression = (
|
|
|
347
383
|
resolveAll([expression.domain, expression.range]);
|
|
348
384
|
return;
|
|
349
385
|
case "Record":
|
|
350
|
-
case "RecordSet":
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
386
|
+
case "RecordSet": {
|
|
387
|
+
const repeated = expression.fields.filter((field, index) =>
|
|
388
|
+
expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
|
|
389
|
+
);
|
|
390
|
+
if (repeated.length > 0) {
|
|
391
|
+
throw createSanyError(
|
|
392
|
+
repeated.map((field) => ({
|
|
393
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
394
|
+
message: "Non-unique fields in constructor.",
|
|
395
|
+
})),
|
|
354
396
|
);
|
|
355
397
|
}
|
|
356
398
|
resolveAll(expression.fields.map((field) => field.value));
|
|
357
399
|
return;
|
|
400
|
+
}
|
|
358
401
|
case "Except":
|
|
359
402
|
resolveAll([
|
|
360
403
|
expression.function,
|
|
@@ -769,6 +812,29 @@ export const resolveSubstitutions = (
|
|
|
769
812
|
);
|
|
770
813
|
};
|
|
771
814
|
|
|
815
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module.
|
|
816
|
+
export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
|
|
817
|
+
const errors: SanyError[] = [];
|
|
818
|
+
units
|
|
819
|
+
.toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
820
|
+
.forEach((unit) => {
|
|
821
|
+
try {
|
|
822
|
+
unit.check();
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
|
|
825
|
+
error.sany.errors
|
|
826
|
+
.filter((item) =>
|
|
827
|
+
errors.every(
|
|
828
|
+
(existing) =>
|
|
829
|
+
existing.location !== item.location || existing.message !== item.message,
|
|
830
|
+
),
|
|
831
|
+
)
|
|
832
|
+
.forEach((item) => errors.push(item));
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
836
|
+
};
|
|
837
|
+
|
|
772
838
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
773
839
|
const symbols = createSymbols(module, unit.imported);
|
|
774
840
|
const facts = new Set(
|
|
@@ -782,13 +848,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
782
848
|
self,
|
|
783
849
|
facts,
|
|
784
850
|
});
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
851
|
+
collectSemanticErrors([
|
|
852
|
+
...module.definitions.map((definition) => ({
|
|
853
|
+
location: definition.location,
|
|
854
|
+
check: () =>
|
|
855
|
+
resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
|
|
856
|
+
})),
|
|
857
|
+
...module.assumptions.map((assumption) => ({
|
|
858
|
+
location: assumption.location,
|
|
859
|
+
check: () =>
|
|
860
|
+
resolveExpression(
|
|
861
|
+
assumption.expression,
|
|
862
|
+
undefined,
|
|
863
|
+
createUnit(assumption.location, undefined),
|
|
864
|
+
),
|
|
865
|
+
})),
|
|
866
|
+
...module.theorems.map((theorem) => ({
|
|
867
|
+
location: theorem.location,
|
|
868
|
+
check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
|
|
869
|
+
})),
|
|
870
|
+
]);
|
|
794
871
|
};
|