tla-ts 0.0.3-dev.cf3b82c → 0.0.3-dev.d9ff6f2
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/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +26 -20
- package/dist/transfer.js +26 -20
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/check.ts +128 -22
- package/src/config.ts +224 -170
- package/src/environment.ts +23 -5
- package/src/errors.ts +30 -1
- package/src/evaluate.ts +38 -9
- package/src/exit-codes.ts +4 -4
- package/src/instances.ts +1 -1
- package/src/levels.ts +55 -27
- package/src/live-check.ts +3 -1
- package/src/liveness.ts +66 -6
- package/src/parser.ts +21 -10
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +35 -8
- package/src/spec-description.ts +1 -2
- package/src/successors.ts +47 -3
- package/src/values.ts +3 -1
package/src/exit-codes.ts
CHANGED
|
@@ -35,6 +35,9 @@ export const setOwnExitCode = <Thrown extends Error>(error: Thrown, code: number
|
|
|
35
35
|
return error;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
export const getOwnExitCode = (error: unknown) =>
|
|
39
|
+
error instanceof Error ? ownExitCodes.get(error) : undefined;
|
|
40
|
+
|
|
38
41
|
// TLC exits with 255 when it fails to process or evaluate a liveness property.
|
|
39
42
|
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
40
43
|
try {
|
|
@@ -56,10 +59,7 @@ export const withLivenessExitCode = (
|
|
|
56
59
|
try {
|
|
57
60
|
checker.addInitialState(index, state);
|
|
58
61
|
} catch (error) {
|
|
59
|
-
throw setExitCode(
|
|
60
|
-
error,
|
|
61
|
-
(error instanceof Error && ownExitCodes.get(error)) || EXIT_STATUS.error,
|
|
62
|
-
);
|
|
62
|
+
throw setExitCode(error, getOwnExitCode(error) ?? EXIT_STATUS.error);
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
addState: (index, state, successors) =>
|
package/src/instances.ts
CHANGED
|
@@ -164,7 +164,7 @@ export const mapChildren = (
|
|
|
164
164
|
case "RecordSet":
|
|
165
165
|
return {
|
|
166
166
|
...expression,
|
|
167
|
-
fields: expression.fields.map((field) => ({
|
|
167
|
+
fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
|
|
168
168
|
};
|
|
169
169
|
case "Except":
|
|
170
170
|
return {
|
package/src/levels.ts
CHANGED
|
@@ -5,11 +5,10 @@ import {
|
|
|
5
5
|
type Expression,
|
|
6
6
|
type Module,
|
|
7
7
|
type OperatorParam,
|
|
8
|
-
type SourceLocation,
|
|
9
8
|
} from "./ast";
|
|
10
|
-
import { TlaSyntaxError } from "./errors";
|
|
9
|
+
import { createSemanticError, TlaSyntaxError } from "./errors";
|
|
11
10
|
import { mapChildren } from "./instances";
|
|
12
|
-
import { getBoundNames, getPatternNames } from "./resolve";
|
|
11
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
13
12
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
14
13
|
|
|
15
14
|
interface LevelData {
|
|
@@ -176,8 +175,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
175
|
|
|
177
176
|
export const TEMPORAL_LEVEL = 3;
|
|
178
177
|
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
179
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
180
|
+
expression.location.range
|
|
181
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
182
|
+
: new TlaSyntaxError(
|
|
183
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
184
|
+
);
|
|
181
185
|
|
|
182
186
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
187
|
|
|
@@ -237,13 +241,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
241
|
if (operand.operator || operand.declared) return;
|
|
238
242
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
243
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
244
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
245
|
);
|
|
242
246
|
}
|
|
243
247
|
});
|
|
244
248
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
249
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
250
|
+
`Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
|
|
247
251
|
);
|
|
248
252
|
}
|
|
249
253
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +448,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
448
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
449
|
: applyDefined(resolved.levels, operands);
|
|
446
450
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
451
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
452
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
453
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
454
|
+
throw createLevelError(
|
|
455
|
+
"Formula has both temporal formula and action as arguments",
|
|
456
|
+
expression,
|
|
457
|
+
);
|
|
451
458
|
}
|
|
452
459
|
};
|
|
453
460
|
|
|
@@ -456,10 +463,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
463
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
464
|
throw createLevelError(
|
|
458
465
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
466
|
+
expression,
|
|
460
467
|
);
|
|
461
468
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
469
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
470
|
};
|
|
464
471
|
|
|
465
472
|
const checkTemporal = (
|
|
@@ -473,7 +480,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
480
|
expression.kind === "Always"
|
|
474
481
|
? "[] followed by action not of form [A]_v"
|
|
475
482
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
483
|
+
expression,
|
|
477
484
|
);
|
|
478
485
|
};
|
|
479
486
|
|
|
@@ -501,7 +508,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
508
|
return data;
|
|
502
509
|
};
|
|
503
510
|
|
|
511
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
512
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
513
|
+
try {
|
|
514
|
+
return analyzeNode(expression, scope);
|
|
515
|
+
} catch (error) {
|
|
516
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
517
|
+
throw createLevelError(error.message, expression);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
522
|
switch (expression.kind) {
|
|
506
523
|
case "Number":
|
|
507
524
|
case "Decimal":
|
|
@@ -521,7 +538,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
538
|
case "And":
|
|
522
539
|
case "Or": {
|
|
523
540
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
541
|
+
checkJunction(operands, expression);
|
|
525
542
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
543
|
}
|
|
527
544
|
case "Prime":
|
|
@@ -561,13 +578,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
578
|
expression.bounds.map((bound) => bound.set),
|
|
562
579
|
getBoundNames(expression.bounds),
|
|
563
580
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
581
|
+
const actionBound =
|
|
582
|
+
data.level === TEMPORAL_LEVEL
|
|
583
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
584
|
+
: undefined;
|
|
585
|
+
if (actionBound) {
|
|
568
586
|
throw createLevelError(
|
|
569
587
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
588
|
+
actionBound.set,
|
|
571
589
|
);
|
|
572
590
|
}
|
|
573
591
|
return data;
|
|
@@ -838,13 +856,23 @@ const analyzeAssumeProve = (
|
|
|
838
856
|
// SANY checks the level of every definition, assumption, and theorem of a module.
|
|
839
857
|
export const checkModuleLevels = (module: Module, context: LevelContext) => {
|
|
840
858
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
859
|
+
collectSemanticErrors([
|
|
860
|
+
...module.definitions
|
|
861
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
862
|
+
.map((definition) => ({
|
|
863
|
+
location: definition.location,
|
|
864
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
865
|
+
})),
|
|
866
|
+
...module.assumptions.map((assumption) => ({
|
|
867
|
+
location: assumption.location,
|
|
868
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
869
|
+
})),
|
|
870
|
+
...module.theorems.map((theorem) => ({
|
|
871
|
+
location: theorem.location,
|
|
872
|
+
check: () =>
|
|
873
|
+
theorem.statement.kind === "AssumeProve"
|
|
874
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
875
|
+
: analyzer.analyze(theorem.statement, new Map()),
|
|
876
|
+
})),
|
|
877
|
+
]);
|
|
850
878
|
};
|
package/src/live-check.ts
CHANGED
|
@@ -100,7 +100,9 @@ const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
// Without a tableau a node is a state, so a successor the search hasn't expanded yet may lie past
|
|
104
|
+
// the per-node arrays.
|
|
105
|
+
const isWritten = (check: Check, node: number) => (check.offsets[node] ?? -1) !== -1;
|
|
104
106
|
|
|
105
107
|
const getPending = (check: Check, node: number) => (check.pending[node] ??= []);
|
|
106
108
|
|
package/src/liveness.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isLocallyResolved, type Definition, type Expression } from "./ast";
|
|
2
|
-
import { STACK_OVERFLOW, TlaEvaluationError } from "./errors";
|
|
3
|
-
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
2
|
+
import { STACK_OVERFLOW, TlaEvaluationError, TlaModuleError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
3
|
+
import { EXIT_STATUS, getOwnExitCode, setExitCode } from "./exit-codes";
|
|
4
4
|
import {
|
|
5
5
|
describeLocation,
|
|
6
6
|
describeRange,
|
|
@@ -61,6 +61,23 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
|
|
|
61
61
|
next: undefined,
|
|
62
62
|
});
|
|
63
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
|
+
|
|
64
81
|
const createCannotHandleError = (expression: Expression) =>
|
|
65
82
|
new TlaEvaluationError(
|
|
66
83
|
`TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -193,12 +210,16 @@ export const expandTemporal = (
|
|
|
193
210
|
if (expanding.has(definition)) {
|
|
194
211
|
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
195
212
|
}
|
|
196
|
-
|
|
213
|
+
const expanded = expand(
|
|
197
214
|
definition.body,
|
|
198
215
|
undefined,
|
|
199
216
|
expression.name,
|
|
200
217
|
new Set([...expanding, definition]),
|
|
201
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;
|
|
202
223
|
}
|
|
203
224
|
}
|
|
204
225
|
const context = getConstantContext(environment, scope);
|
|
@@ -278,7 +299,7 @@ export const toLiveExpression = (
|
|
|
278
299
|
): LiveExpression => {
|
|
279
300
|
const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
|
|
280
301
|
if (atomLevel === 0) {
|
|
281
|
-
const value =
|
|
302
|
+
const value = evaluateConstant(expression, getConstantContext(environment, scope));
|
|
282
303
|
if (typeof value !== "boolean") {
|
|
283
304
|
throw new TlaEvaluationError(
|
|
284
305
|
`TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
|
|
@@ -354,9 +375,45 @@ export const toLiveExpression = (
|
|
|
354
375
|
return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
|
|
355
376
|
};
|
|
356
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
|
+
|
|
357
411
|
const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
|
|
358
412
|
const level = getLevel(expression, scope, environment);
|
|
359
|
-
if (level <= ACTION_LEVEL)
|
|
413
|
+
if (level <= ACTION_LEVEL) {
|
|
414
|
+
convertOperands(expression, scope);
|
|
415
|
+
return toAtom(expression, scope, level);
|
|
416
|
+
}
|
|
360
417
|
switch (expression.kind) {
|
|
361
418
|
case "Label":
|
|
362
419
|
return convert(expression.body, scope);
|
|
@@ -399,7 +456,10 @@ export const toLiveExpression = (
|
|
|
399
456
|
return convertQuantifier(expression, scope);
|
|
400
457
|
case "If": {
|
|
401
458
|
if (getLevel(expression.condition, scope, environment) === 0) {
|
|
402
|
-
const condition =
|
|
459
|
+
const condition = evaluateConstant(
|
|
460
|
+
expression.condition,
|
|
461
|
+
getConstantContext(environment, scope),
|
|
462
|
+
);
|
|
403
463
|
if (typeof condition === "boolean") {
|
|
404
464
|
return convert(condition ? expression.consequent : expression.alternative, scope);
|
|
405
465
|
}
|
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 {
|
|
@@ -631,11 +631,15 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
631
631
|
return parseExpression();
|
|
632
632
|
};
|
|
633
633
|
|
|
634
|
+
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
635
|
+
|
|
634
636
|
const parseArguments = () =>
|
|
635
637
|
withoutOffside(() => {
|
|
638
|
+
const start = cursor.index;
|
|
636
639
|
expect("(");
|
|
637
640
|
const args = parseSeparated(parseArgument);
|
|
638
641
|
expect(")");
|
|
642
|
+
lastArguments.range = getRange(start);
|
|
639
643
|
return args;
|
|
640
644
|
});
|
|
641
645
|
|
|
@@ -662,9 +666,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
662
666
|
if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
|
|
663
667
|
const kind = isAt("|->", 1) ? "Record" : "RecordSet";
|
|
664
668
|
const fields = parseSeparated(() => {
|
|
669
|
+
const fieldStart = cursor.index;
|
|
670
|
+
const fieldLocation = getLocation(peek());
|
|
665
671
|
const name = expectIdentifier();
|
|
672
|
+
fieldLocation.range = getRange(fieldStart);
|
|
666
673
|
expect(kind === "Record" ? "|->" : ":");
|
|
667
|
-
return { name, value: parseExpression() };
|
|
674
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
668
675
|
});
|
|
669
676
|
expect("]");
|
|
670
677
|
return { kind, fields, location };
|
|
@@ -856,7 +863,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
856
863
|
const args = hasArguments ? parseArguments() : [];
|
|
857
864
|
if (!isSelectorAhead()) {
|
|
858
865
|
return hasArguments
|
|
859
|
-
? { kind: "Apply", operator: name, args, location }
|
|
866
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
860
867
|
: { kind: "Identifier", name, location };
|
|
861
868
|
}
|
|
862
869
|
const instance = openModules
|
|
@@ -885,9 +892,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
885
892
|
const argumentCounts = selectors.map((selector) =>
|
|
886
893
|
"args" in selector ? selector.args.length : 0,
|
|
887
894
|
);
|
|
895
|
+
const argumentsRange = lastArguments.range;
|
|
888
896
|
return argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
889
|
-
? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
|
|
890
|
-
: { kind: "Apply", operator, args: qualifiedArgs, location };
|
|
897
|
+
? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
|
|
898
|
+
: { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
|
|
891
899
|
}
|
|
892
900
|
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
893
901
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
@@ -1423,11 +1431,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1423
1431
|
location: getLocation(keyword),
|
|
1424
1432
|
};
|
|
1425
1433
|
const invalid =
|
|
1426
|
-
use.kind === "Hide" && !isTlapm ? use.facts.
|
|
1427
|
-
if (invalid) {
|
|
1428
|
-
throw
|
|
1429
|
-
|
|
1430
|
-
|
|
1434
|
+
use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1435
|
+
if (invalid.length > 0) {
|
|
1436
|
+
throw createSanyError(
|
|
1437
|
+
invalid.map((fact) => ({
|
|
1438
|
+
location: formatRange(fact.location.range ?? getRange(start)),
|
|
1439
|
+
message:
|
|
1440
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1441
|
+
})),
|
|
1431
1442
|
);
|
|
1432
1443
|
}
|
|
1433
1444
|
return withRange(use, start);
|
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
|
};
|