tla-ts 0.0.3-dev.6979965 → 0.0.3-dev.6ac5593
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 +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +72 -22
- package/dist/index.d.ts +72 -22
- 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 +20 -17
- package/dist/run-cli.js +22 -19
- package/dist/transfer.cjs +46 -16
- package/dist/transfer.js +46 -16
- package/package.json +1 -1
- package/src/ast.ts +121 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +521 -105
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +410 -130
- package/src/errors.ts +109 -3
- package/src/evaluate.ts +443 -194
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +17 -16
- package/src/levels.ts +185 -44
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +102 -115
- package/src/parallel.ts +34 -23
- package/src/parser.ts +184 -54
- package/src/resolve.ts +1378 -420
- package/src/run-cli.ts +115 -20
- package/src/runtime.ts +3 -1
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +54 -45
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +184 -40
- package/src/symmetry.ts +123 -43
- 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/value-sources.ts +29 -0
- package/src/values.ts +249 -65
package/src/exit-codes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LivenessChecker } from "./
|
|
1
|
+
import type { LivenessChecker } from "./live-check";
|
|
2
2
|
|
|
3
3
|
// TLC's exit statuses, from EC.ExitStatus.
|
|
4
4
|
export const EXIT_STATUS = {
|
|
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
|
|
|
17
17
|
} as const;
|
|
18
18
|
|
|
19
19
|
const exitCodes = new WeakMap<Error, number>();
|
|
20
|
-
const moduleExitCodes = new WeakMap<Error, number>();
|
|
21
|
-
const phase = { isNextState: false };
|
|
22
|
-
|
|
23
|
-
export const setNextStatePhase = (isNextState: boolean) => {
|
|
24
|
-
phase.isNextState = isNextState;
|
|
25
|
-
};
|
|
26
20
|
|
|
27
21
|
// The innermost site that knows TLC's exit status for an error sets it first and wins.
|
|
28
22
|
export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
@@ -30,46 +24,49 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
|
|
|
30
24
|
return error;
|
|
31
25
|
};
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
export const getExitCode = (error: Error) => exitCodes.get(error);
|
|
28
|
+
|
|
29
|
+
const ownExitCodes = new WeakMap<Error, number>();
|
|
30
|
+
|
|
31
|
+
// The exit status of the error code TLC gives an EvalException, which TLC uses only where it
|
|
32
|
+
// doesn't report the error again as a nested expression.
|
|
33
|
+
export const setOwnExitCode = <Thrown extends Error>(error: Thrown, code: number) => {
|
|
34
|
+
ownExitCodes.set(error, code);
|
|
35
|
+
return error;
|
|
37
36
|
};
|
|
38
37
|
|
|
39
|
-
export const
|
|
38
|
+
export const getOwnExitCode = (error: unknown) =>
|
|
39
|
+
error instanceof Error ? ownExitCodes.get(error) : undefined;
|
|
40
40
|
|
|
41
|
-
// TLC exits with 255 when it fails to process or evaluate a liveness property
|
|
42
|
-
|
|
43
|
-
create: () => LivenessChecker | undefined,
|
|
44
|
-
): LivenessChecker | undefined => {
|
|
41
|
+
// TLC exits with 255 when it fails to process or evaluate a liveness property.
|
|
42
|
+
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
45
43
|
try {
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
checker && {
|
|
49
|
-
addInitialState: (index, state) => {
|
|
50
|
-
try {
|
|
51
|
-
checker.addInitialState(index, state);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
addState: (index, state, successors) => {
|
|
57
|
-
try {
|
|
58
|
-
return checker.addState(index, state, successors);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
findViolation: () => {
|
|
64
|
-
try {
|
|
65
|
-
return checker.findViolation();
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
}
|
|
71
|
-
);
|
|
44
|
+
return run();
|
|
72
45
|
} catch (error) {
|
|
73
46
|
throw setExitCode(error, EXIT_STATUS.error);
|
|
74
47
|
}
|
|
75
48
|
};
|
|
49
|
+
|
|
50
|
+
export const withLivenessExitCode = (
|
|
51
|
+
create: () => LivenessChecker | undefined,
|
|
52
|
+
): LivenessChecker | undefined => {
|
|
53
|
+
const checker = withErrorExitCode(create);
|
|
54
|
+
return (
|
|
55
|
+
checker && {
|
|
56
|
+
// TLC replays the initial states to report an error, but skips the liveness check of a
|
|
57
|
+
// state it has already seen, so it exits with the status of the error's own code.
|
|
58
|
+
addInitialState: (index, state) => {
|
|
59
|
+
try {
|
|
60
|
+
checker.addInitialState(index, state);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw setExitCode(error, getOwnExitCode(error) ?? EXIT_STATUS.error);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
addState: (index, state, successors) =>
|
|
66
|
+
withErrorExitCode(() => checker.addState(index, state, successors)),
|
|
67
|
+
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
|
68
|
+
getViolatedProperties: (path, cycleStart) =>
|
|
69
|
+
withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
};
|
package/src/fingerprints.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getValueFingerprint, getValueHash, type Value } from "./values";
|
|
1
|
+
import { assertFingerprintable, getValueFingerprint, getValueHash, type Value } from "./values";
|
|
2
2
|
|
|
3
3
|
const EMPTY = 0;
|
|
4
4
|
const INITIAL_CAPACITY = 1 << 12;
|
|
@@ -20,6 +20,7 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
|
|
|
20
20
|
first = Math.imul(first ^ getValueHash(state[index]), 0x01000193);
|
|
21
21
|
second = Math.imul(second ^ getValueFingerprint(state[index]), 0x5bd1e995);
|
|
22
22
|
}
|
|
23
|
+
assertFingerprintable(state);
|
|
23
24
|
fingerprint[0] = mix(first) || 1;
|
|
24
25
|
fingerprint[1] = mix(second);
|
|
25
26
|
};
|
package/src/instances.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type Definition,
|
|
7
|
+
type Expression,
|
|
8
|
+
type Instance,
|
|
9
|
+
type Module,
|
|
10
|
+
type OperatorParam,
|
|
11
|
+
type SourceLocation,
|
|
12
|
+
type Theorem,
|
|
11
13
|
} from "./ast";
|
|
12
14
|
import { TlaSyntaxError } from "./errors";
|
|
13
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
14
15
|
import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
|
|
15
16
|
|
|
16
17
|
export const substitutedExpressions = new WeakSet<Expression>();
|
|
@@ -163,7 +164,7 @@ export const mapChildren = (
|
|
|
163
164
|
case "RecordSet":
|
|
164
165
|
return {
|
|
165
166
|
...expression,
|
|
166
|
-
fields: expression.fields.map((field) => ({
|
|
167
|
+
fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
|
|
167
168
|
};
|
|
168
169
|
case "Except":
|
|
169
170
|
return {
|
|
@@ -218,7 +219,7 @@ interface Instantiation {
|
|
|
218
219
|
variables: Set<string>;
|
|
219
220
|
getDefinition: (key: string) => Definition | undefined;
|
|
220
221
|
isParameterized: (key: string) => boolean;
|
|
221
|
-
addDefinition: (name: string, create: (key: string) => Definition) => string;
|
|
222
|
+
addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
|
|
222
223
|
createName: (name: string) => string;
|
|
223
224
|
}
|
|
224
225
|
|
|
@@ -639,7 +640,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
|
|
|
639
640
|
const instantiation = rewrite.instantiation;
|
|
640
641
|
const definition = instantiation.getDefinition(key);
|
|
641
642
|
if (!definition) return key;
|
|
642
|
-
return instantiation.addDefinition(definition.name, (copyKey) => {
|
|
643
|
+
return instantiation.addDefinition(key, definition.name, (copyKey) => {
|
|
643
644
|
rewrite.copies.set(key, copyKey);
|
|
644
645
|
const rename = createRenamer(rewrite);
|
|
645
646
|
const mapped = renameParams(
|
|
@@ -678,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
|
|
|
678
679
|
if (copy !== undefined) return copy;
|
|
679
680
|
const definition = instantiation.getDefinition(key);
|
|
680
681
|
if (!definition) return key;
|
|
681
|
-
return instantiation.addDefinition(definition.name, () =>
|
|
682
|
+
return instantiation.addDefinition(key, definition.name, () =>
|
|
682
683
|
withInstanceParams(definition, instantiation.params, definition.location),
|
|
683
684
|
);
|
|
684
685
|
},
|
|
@@ -704,7 +705,7 @@ const expandLetInstances = (
|
|
|
704
705
|
const index = position === -1 ? current.length : position;
|
|
705
706
|
return [...current.slice(0, index), ...instantiate(instance, inner), ...current.slice(index)];
|
|
706
707
|
}, mapped.definitions);
|
|
707
|
-
return {
|
|
708
|
+
return { ...mapped, definitions };
|
|
708
709
|
};
|
|
709
710
|
|
|
710
711
|
// A named theorem's statement is linked as its definition.
|
package/src/levels.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type AssumeProve,
|
|
4
|
+
type Definition,
|
|
5
|
+
type Expression,
|
|
6
|
+
type Module,
|
|
7
|
+
type OperatorParam,
|
|
8
|
+
type Proof,
|
|
9
|
+
type ProofStep,
|
|
10
|
+
type SourceLocation,
|
|
8
11
|
} from "./ast";
|
|
9
|
-
import {
|
|
10
|
-
|
|
12
|
+
import {
|
|
13
|
+
createSanyError,
|
|
14
|
+
createSemanticError,
|
|
15
|
+
formatRange,
|
|
16
|
+
TlaSyntaxError,
|
|
17
|
+
type SanyError,
|
|
18
|
+
} from "./errors";
|
|
11
19
|
import { mapChildren } from "./instances";
|
|
12
|
-
import { getBoundNames, getPatternNames } from "./resolve";
|
|
20
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
13
21
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
14
22
|
|
|
15
23
|
interface LevelData {
|
|
@@ -34,6 +42,7 @@ interface LevelContext {
|
|
|
34
42
|
constants: Set<string>;
|
|
35
43
|
variables: Set<string>;
|
|
36
44
|
getDefinition: (name: string) => Definition | undefined;
|
|
45
|
+
getQualifiedName?: (name: string) => string | undefined;
|
|
37
46
|
}
|
|
38
47
|
|
|
39
48
|
type ScopeEntry =
|
|
@@ -176,8 +185,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
185
|
|
|
177
186
|
export const TEMPORAL_LEVEL = 3;
|
|
178
187
|
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
189
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
190
|
+
expression.location.range
|
|
191
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
192
|
+
: new TlaSyntaxError(
|
|
193
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
194
|
+
);
|
|
181
195
|
|
|
182
196
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
197
|
|
|
@@ -201,7 +215,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
201
215
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
202
216
|
const definition = context.getDefinition(name);
|
|
203
217
|
if (definition) {
|
|
204
|
-
|
|
218
|
+
const levels = analyzeDefinition(definition, new Map());
|
|
219
|
+
// SANY names a definition of an instance by its qualified name, as in I!Foo.
|
|
220
|
+
const qualifiedName = context.getQualifiedName?.(name);
|
|
221
|
+
return {
|
|
222
|
+
kind: "Defined",
|
|
223
|
+
levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
|
|
224
|
+
};
|
|
205
225
|
}
|
|
206
226
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
207
227
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -237,13 +257,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
257
|
if (operand.operator || operand.declared) return;
|
|
238
258
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
259
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
260
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
261
|
);
|
|
242
262
|
}
|
|
243
263
|
});
|
|
244
264
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
265
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
266
|
+
`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
267
|
);
|
|
248
268
|
}
|
|
249
269
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +464,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
464
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
465
|
: applyDefined(resolved.levels, operands);
|
|
446
466
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
467
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
468
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
469
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
470
|
+
throw createLevelError(
|
|
471
|
+
"Formula has both temporal formula and action as arguments",
|
|
472
|
+
expression,
|
|
473
|
+
);
|
|
451
474
|
}
|
|
452
475
|
};
|
|
453
476
|
|
|
@@ -456,10 +479,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
479
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
480
|
throw createLevelError(
|
|
458
481
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
482
|
+
expression,
|
|
460
483
|
);
|
|
461
484
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
485
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
486
|
};
|
|
464
487
|
|
|
465
488
|
const checkTemporal = (
|
|
@@ -473,7 +496,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
496
|
expression.kind === "Always"
|
|
474
497
|
? "[] followed by action not of form [A]_v"
|
|
475
498
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
499
|
+
expression,
|
|
477
500
|
);
|
|
478
501
|
};
|
|
479
502
|
|
|
@@ -501,7 +524,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
524
|
return data;
|
|
502
525
|
};
|
|
503
526
|
|
|
527
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
528
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
529
|
+
try {
|
|
530
|
+
return analyzeNode(expression, scope);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
533
|
+
throw createLevelError(error.message, expression);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
538
|
switch (expression.kind) {
|
|
506
539
|
case "Number":
|
|
507
540
|
case "Decimal":
|
|
@@ -521,7 +554,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
554
|
case "And":
|
|
522
555
|
case "Or": {
|
|
523
556
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
557
|
+
checkJunction(operands, expression);
|
|
525
558
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
559
|
}
|
|
527
560
|
case "Prime":
|
|
@@ -561,13 +594,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
594
|
expression.bounds.map((bound) => bound.set),
|
|
562
595
|
getBoundNames(expression.bounds),
|
|
563
596
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
597
|
+
const actionBound =
|
|
598
|
+
data.level === TEMPORAL_LEVEL
|
|
599
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
600
|
+
: undefined;
|
|
601
|
+
if (actionBound) {
|
|
568
602
|
throw createLevelError(
|
|
569
603
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
604
|
+
actionBound.set,
|
|
571
605
|
);
|
|
572
606
|
}
|
|
573
607
|
return data;
|
|
@@ -817,34 +851,141 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
817
851
|
return { analyze, analyzeDefinition, toOperand };
|
|
818
852
|
};
|
|
819
853
|
|
|
820
|
-
const analyzeAssumeProve = (
|
|
854
|
+
export const analyzeAssumeProve = (
|
|
821
855
|
assumeProve: AssumeProve,
|
|
822
856
|
scope: Scope,
|
|
823
857
|
analyzer: ReturnType<typeof createLevelAnalyzer>,
|
|
824
|
-
) => {
|
|
858
|
+
): LevelData => {
|
|
825
859
|
const inner = new Map(scope);
|
|
826
|
-
assumeProve.assumptions.
|
|
860
|
+
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
827
861
|
if (assumption.kind === "NewSymbol") {
|
|
828
|
-
|
|
862
|
+
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
829
863
|
inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
|
|
830
|
-
return;
|
|
864
|
+
return set ? [set] : [];
|
|
831
865
|
}
|
|
832
|
-
|
|
833
|
-
|
|
866
|
+
return [
|
|
867
|
+
assumption.kind === "AssumeProve"
|
|
868
|
+
? analyzeAssumeProve(assumption, inner, analyzer)
|
|
869
|
+
: analyzer.analyze(assumption, inner),
|
|
870
|
+
];
|
|
871
|
+
});
|
|
872
|
+
parts.push(analyzer.analyze(assumeProve.conclusion, inner));
|
|
873
|
+
const data = createData(Math.max(...parts.map((part) => part.level)));
|
|
874
|
+
parts.forEach((part) => {
|
|
875
|
+
part.params.forEach((param) => data.params.add(param));
|
|
876
|
+
part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
|
|
877
|
+
part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
|
|
878
|
+
part.argParams.forEach((key) => data.argParams.add(key));
|
|
834
879
|
});
|
|
835
|
-
|
|
880
|
+
return data;
|
|
836
881
|
};
|
|
837
882
|
|
|
838
|
-
// SANY checks the level of every definition, assumption, and
|
|
839
|
-
export const checkModuleLevels = (
|
|
883
|
+
// SANY checks the level of every definition, assumption, theorem, and instance of a module.
|
|
884
|
+
export const checkModuleLevels = (
|
|
885
|
+
module: Module,
|
|
886
|
+
context: LevelContext,
|
|
887
|
+
instances: { location: SourceLocation; check: () => void }[],
|
|
888
|
+
warnings: SanyError[],
|
|
889
|
+
) => {
|
|
840
890
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
891
|
+
const getLevel = (expressions: Expression[]) =>
|
|
892
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
|
|
893
|
+
const getStepLevel = (step: ProofStep) => {
|
|
894
|
+
const body = step.body;
|
|
895
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
896
|
+
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
897
|
+
if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
|
|
898
|
+
if (body.kind === "Pick") {
|
|
899
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
900
|
+
}
|
|
901
|
+
if (body.kind !== "Assert") return 0;
|
|
902
|
+
return body.statement.kind === "AssumeProve"
|
|
903
|
+
? analyzeAssumeProve(body.statement, new Map(), analyzer).level
|
|
904
|
+
: getLevel([body.statement]);
|
|
905
|
+
};
|
|
906
|
+
// SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
|
|
907
|
+
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
908
|
+
proof?.kind !== "Steps"
|
|
909
|
+
? []
|
|
910
|
+
: proof.steps.flatMap((step) => {
|
|
911
|
+
const location = formatRange(step.statement);
|
|
912
|
+
const isConstant = () => getStepLevel(step) === 0;
|
|
913
|
+
if (
|
|
914
|
+
step.body.kind === "Have" ||
|
|
915
|
+
step.body.kind === "Take" ||
|
|
916
|
+
step.body.kind === "Witness"
|
|
917
|
+
) {
|
|
918
|
+
return isConstant()
|
|
919
|
+
? []
|
|
920
|
+
: [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
|
|
921
|
+
}
|
|
922
|
+
if (step.body.kind === "Case") {
|
|
923
|
+
return [
|
|
924
|
+
...(isConstant()
|
|
925
|
+
? []
|
|
926
|
+
: [{ location, message: "Non-constant CASE for temporal goal." }]),
|
|
927
|
+
...checkTemporalGoal(step.proof),
|
|
928
|
+
];
|
|
929
|
+
}
|
|
930
|
+
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
931
|
+
});
|
|
932
|
+
// SANY checks each step as a theorem of its own after the steps of its proof.
|
|
933
|
+
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
934
|
+
proof?.kind !== "Steps"
|
|
935
|
+
? []
|
|
936
|
+
: proof.steps.flatMap((step) => {
|
|
937
|
+
const body = step.body;
|
|
938
|
+
const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
|
|
939
|
+
const pickErrors =
|
|
940
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
|
|
941
|
+
? body.bounds
|
|
942
|
+
.filter((bound) => getLevel([bound.set]) !== 0)
|
|
943
|
+
.flatMap((bound) =>
|
|
944
|
+
bound.set.location.range
|
|
945
|
+
? [
|
|
946
|
+
{
|
|
947
|
+
location: formatRange(bound.set.location.range),
|
|
948
|
+
message: "Non-constant bound of temporal PICK.",
|
|
949
|
+
},
|
|
950
|
+
]
|
|
951
|
+
: [],
|
|
952
|
+
)
|
|
953
|
+
: [];
|
|
954
|
+
return [
|
|
955
|
+
...checkProof(step.proof),
|
|
956
|
+
...pickErrors,
|
|
957
|
+
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
958
|
+
];
|
|
959
|
+
});
|
|
960
|
+
const errors = collectSemanticErrors(
|
|
961
|
+
[
|
|
962
|
+
...module.definitions
|
|
963
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
964
|
+
.map((definition) => ({
|
|
965
|
+
location: definition.location,
|
|
966
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
967
|
+
})),
|
|
968
|
+
...module.assumptions.map((assumption) => ({
|
|
969
|
+
location: assumption.location,
|
|
970
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
971
|
+
})),
|
|
972
|
+
...module.theorems.map((theorem) => ({
|
|
973
|
+
location: theorem.location,
|
|
974
|
+
check: () => {
|
|
975
|
+
const data =
|
|
976
|
+
theorem.statement.kind === "AssumeProve"
|
|
977
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
978
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
979
|
+
const errors = [
|
|
980
|
+
...checkProof(theorem.proof),
|
|
981
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
982
|
+
];
|
|
983
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
984
|
+
},
|
|
985
|
+
})),
|
|
986
|
+
...instances,
|
|
987
|
+
],
|
|
988
|
+
warnings,
|
|
849
989
|
);
|
|
990
|
+
if (errors.length > 0) throw createSanyError(errors, false, undefined, warnings);
|
|
850
991
|
};
|
package/src/lexer.ts
CHANGED
|
@@ -345,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
|
|
|
345
345
|
if (!headerMatch)
|
|
346
346
|
throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
|
|
347
347
|
const tokens: Token[] = [];
|
|
348
|
+
// Each name is one string object, so looking a name up in a scope compares pointers.
|
|
349
|
+
const names = new Map<string, string>();
|
|
348
350
|
const prefixLines = source.slice(0, headerMatch.index).split("\n");
|
|
349
351
|
const cursor = {
|
|
350
352
|
index: headerMatch.index,
|
|
@@ -481,7 +483,9 @@ export const tokenize = (source: string): Token[] => {
|
|
|
481
483
|
pushToken("Keyword", word.slice(0, 3), 3);
|
|
482
484
|
continue;
|
|
483
485
|
}
|
|
484
|
-
|
|
486
|
+
const name = names.get(word) ?? word;
|
|
487
|
+
names.set(name, name);
|
|
488
|
+
pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
|
|
485
489
|
continue;
|
|
486
490
|
}
|
|
487
491
|
const stepLength = matchStep(source, cursor.index);
|