tla-ts 0.0.3-dev.33ae522 → 0.0.3-dev.3504663
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.cjs +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- 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 +13 -13
- package/dist/run-cli.js +16 -16
- package/dist/transfer.cjs +24 -16
- package/dist/transfer.js +24 -16
- package/package.json +1 -1
- package/src/ast.ts +8 -1
- package/src/check.ts +5 -2
- package/src/environment.ts +38 -21
- package/src/errors.ts +14 -3
- package/src/evaluate.ts +3 -3
- package/src/fingerprints.ts +2 -1
- package/src/levels.ts +124 -16
- package/src/parser.ts +52 -11
- package/src/resolve.ts +339 -39
- package/src/run-cli.ts +3 -2
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/standard-modules.ts +2 -2
- package/src/subexpressions.ts +25 -13
- package/src/substitution-levels.ts +38 -17
- package/src/values.ts +70 -8
package/package.json
CHANGED
package/src/ast.ts
CHANGED
|
@@ -33,9 +33,12 @@ export interface StringExpression {
|
|
|
33
33
|
location: SourceLocation;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// selectorRanges holds the range of each part of a name such as foo!lab, where SANY reports an
|
|
37
|
+
// error in selecting that part.
|
|
36
38
|
export interface IdentifierExpression {
|
|
37
39
|
kind: "Identifier";
|
|
38
40
|
name: string;
|
|
41
|
+
selectorRanges?: SourceRange[];
|
|
39
42
|
location: SourceLocation;
|
|
40
43
|
resolution?: LocalResolution;
|
|
41
44
|
}
|
|
@@ -49,6 +52,7 @@ export interface ApplyExpression {
|
|
|
49
52
|
argumentCounts?: number[];
|
|
50
53
|
// The range of the parenthesized arguments of a named operator, where SANY reports their count.
|
|
51
54
|
argumentsRange?: SourceRange;
|
|
55
|
+
selectorRanges?: SourceRange[];
|
|
52
56
|
location: SourceLocation;
|
|
53
57
|
resolution?: LocalResolution;
|
|
54
58
|
}
|
|
@@ -283,6 +287,7 @@ export type Selector =
|
|
|
283
287
|
export interface SubexpressionExpression {
|
|
284
288
|
kind: "Subexpression";
|
|
285
289
|
selectors: Selector[];
|
|
290
|
+
selectorRanges?: SourceRange[];
|
|
286
291
|
location: SourceLocation;
|
|
287
292
|
}
|
|
288
293
|
|
|
@@ -445,12 +450,14 @@ export type StepBody =
|
|
|
445
450
|
| { kind: "Assert"; isSuffices: boolean; statement: Expression | AssumeProve }
|
|
446
451
|
| { kind: "Qed" };
|
|
447
452
|
|
|
448
|
-
// head is the range of the step number and its statement, without the proof
|
|
453
|
+
// head is the range of the step number and its statement, without the proof, and statement is the
|
|
454
|
+
// range of the statement alone.
|
|
449
455
|
export interface ProofStep {
|
|
450
456
|
name: string | undefined;
|
|
451
457
|
token: string;
|
|
452
458
|
body: StepBody;
|
|
453
459
|
head: SourceRange;
|
|
460
|
+
statement: SourceRange;
|
|
454
461
|
proof: Proof | undefined;
|
|
455
462
|
location: SourceLocation;
|
|
456
463
|
}
|
package/src/check.ts
CHANGED
|
@@ -72,6 +72,7 @@ import { createCounterExample } from "./tlc-ext";
|
|
|
72
72
|
import { getFingerprintTrace, valueSources } from "./value-sources";
|
|
73
73
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
74
74
|
import {
|
|
75
|
+
assertFingerprintable,
|
|
75
76
|
combineHash,
|
|
76
77
|
createModelValue,
|
|
77
78
|
formatState,
|
|
@@ -1130,12 +1131,13 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1130
1131
|
failure: { ...result.failure, replay: { output: replayed, ...replayedStack, isBeforeError } },
|
|
1131
1132
|
};
|
|
1132
1133
|
};
|
|
1133
|
-
// TLC fingerprints a state outside CallStackTool, so a failure
|
|
1134
|
+
// TLC fingerprints a state outside CallStackTool, so a failure of a value with a source leaves
|
|
1135
|
+
// the stack empty. Any other failure keeps the stack of the initial predicate.
|
|
1134
1136
|
const fingerprintReplayed = (state: Value[], traced: Environment) => {
|
|
1135
1137
|
try {
|
|
1136
1138
|
getStateHash(getFingerprintState(state));
|
|
1137
1139
|
} catch (error) {
|
|
1138
|
-
if (traced.trace) traced.trace.length = 0;
|
|
1140
|
+
if (traced.trace && getFingerprintTrace(error)) traced.trace.length = 0;
|
|
1139
1141
|
throw error;
|
|
1140
1142
|
}
|
|
1141
1143
|
};
|
|
@@ -1388,6 +1390,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
|
|
|
1388
1390
|
let hash = 0x811c9dc5;
|
|
1389
1391
|
for (let index = 0; index < state.length; index++)
|
|
1390
1392
|
hash = combineHash(hash, getValueHash(state[index]));
|
|
1393
|
+
assertFingerprintable(state);
|
|
1391
1394
|
return hash & 0x3fffffff;
|
|
1392
1395
|
};
|
|
1393
1396
|
const isSameState = (stored: Value[], state: Value[]) => {
|
package/src/environment.ts
CHANGED
|
@@ -49,10 +49,11 @@ import {
|
|
|
49
49
|
type Symbols,
|
|
50
50
|
} from "./resolve";
|
|
51
51
|
import { createRuntime } from "./runtime";
|
|
52
|
+
import { parseModuleFile } from "./sany-parse-error";
|
|
52
53
|
import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
|
|
53
54
|
import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
|
|
54
55
|
import { checkModuleLevels, createLevelAnalyzer } from "./levels";
|
|
55
|
-
import {
|
|
56
|
+
import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
|
|
56
57
|
import { getTlcDeclarationOrder } from "./tlc-order";
|
|
57
58
|
import {
|
|
58
59
|
checkInteger,
|
|
@@ -229,6 +230,8 @@ export const createEnvironment = (
|
|
|
229
230
|
const runtime = createRuntime(config.checkDeadlock, options.seed);
|
|
230
231
|
const linkedModules = new Map<string, LinkedModule>();
|
|
231
232
|
const nestedModules = new Map<string, LinkedModule>();
|
|
233
|
+
// Each module by name, from the time its level checks run.
|
|
234
|
+
const checkedModules = new Map<string, LinkedModule>();
|
|
232
235
|
const linkedList: LinkedModule[] = [];
|
|
233
236
|
const dependencies = new Map<string, Set<string>>();
|
|
234
237
|
const loading: Set<string>[] = [];
|
|
@@ -243,7 +246,23 @@ export const createEnvironment = (
|
|
|
243
246
|
const copyCount = { value: 0 };
|
|
244
247
|
const createName = (name: string) => `${name}#${copyCount.value++}`;
|
|
245
248
|
const instanceChecks: InstanceCheck[] = [];
|
|
249
|
+
const checkInstance = createInstanceChecker((name) => {
|
|
250
|
+
const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
|
|
251
|
+
return {
|
|
252
|
+
constants: linked.constants,
|
|
253
|
+
variables: linked.variables,
|
|
254
|
+
definitions: [
|
|
255
|
+
...new Set(
|
|
256
|
+
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
257
|
+
),
|
|
258
|
+
],
|
|
259
|
+
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
260
|
+
theorems: linked.module.theorems,
|
|
261
|
+
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
262
|
+
};
|
|
263
|
+
});
|
|
246
264
|
const instanceActions = new Map<string, InstanceAction>();
|
|
265
|
+
const qualifiedNames = new Map<string, string>();
|
|
247
266
|
|
|
248
267
|
const addStandardModule = (name: string): Symbols | undefined => {
|
|
249
268
|
const cached = standardExports.get(name);
|
|
@@ -308,7 +327,7 @@ export const createEnvironment = (
|
|
|
308
327
|
],
|
|
309
328
|
true,
|
|
310
329
|
);
|
|
311
|
-
const module =
|
|
330
|
+
const module = parseModuleFile(source, name);
|
|
312
331
|
if (module.name !== name) {
|
|
313
332
|
throw createSanyError(
|
|
314
333
|
[
|
|
@@ -429,6 +448,7 @@ export const createEnvironment = (
|
|
|
429
448
|
if (declarations.length > 0) {
|
|
430
449
|
instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
|
|
431
450
|
}
|
|
451
|
+
qualifiedNames.set(key, `${instance.name}!${name}`);
|
|
432
452
|
symbols.set(`${instance.name}!${name}`, {
|
|
433
453
|
key,
|
|
434
454
|
arity: symbol.arity + instance.params.length,
|
|
@@ -609,6 +629,7 @@ export const createEnvironment = (
|
|
|
609
629
|
|
|
610
630
|
const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
|
|
611
631
|
const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
|
|
632
|
+
const firstCheck = instanceChecks.length;
|
|
612
633
|
const visible: Symbols = new Map([
|
|
613
634
|
...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
|
|
614
635
|
operator.name,
|
|
@@ -751,11 +772,6 @@ export const createEnvironment = (
|
|
|
751
772
|
visible.set(definition.name, symbol);
|
|
752
773
|
if (!definition.isLocal) exported.set(definition.name, symbol);
|
|
753
774
|
});
|
|
754
|
-
checkModuleLevels(module, {
|
|
755
|
-
constants: new Set(constants.map((constant) => constant.name)),
|
|
756
|
-
variables: new Set(variables),
|
|
757
|
-
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
758
|
-
});
|
|
759
775
|
const linked = {
|
|
760
776
|
module,
|
|
761
777
|
constants,
|
|
@@ -766,6 +782,21 @@ export const createEnvironment = (
|
|
|
766
782
|
modules,
|
|
767
783
|
extended,
|
|
768
784
|
};
|
|
785
|
+
if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
|
|
786
|
+
// SANY checks the instances of a module with its definitions and theorems, so it reports their
|
|
787
|
+
// errors together.
|
|
788
|
+
checkModuleLevels(
|
|
789
|
+
module,
|
|
790
|
+
{
|
|
791
|
+
constants: new Set(constants.map((constant) => constant.name)),
|
|
792
|
+
variables: new Set(variables),
|
|
793
|
+
getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
|
|
794
|
+
getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
|
|
795
|
+
},
|
|
796
|
+
instanceChecks
|
|
797
|
+
.splice(firstCheck)
|
|
798
|
+
.map((check) => ({ location: check.location, check: () => checkInstance(check) })),
|
|
799
|
+
);
|
|
769
800
|
linkedList.push(linked);
|
|
770
801
|
return linked;
|
|
771
802
|
};
|
|
@@ -960,20 +991,6 @@ export const createEnvironment = (
|
|
|
960
991
|
constantLevels: new Map(),
|
|
961
992
|
evaluators: new WeakMap(),
|
|
962
993
|
};
|
|
963
|
-
checkInstances(instanceChecks, (name) => {
|
|
964
|
-
const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
|
|
965
|
-
return {
|
|
966
|
-
constants: linked.constants,
|
|
967
|
-
variables: linked.variables,
|
|
968
|
-
definitions: [
|
|
969
|
-
...new Set(
|
|
970
|
-
[...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
|
|
971
|
-
),
|
|
972
|
-
],
|
|
973
|
-
assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
|
|
974
|
-
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
975
|
-
};
|
|
976
|
-
});
|
|
977
994
|
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
978
995
|
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
979
996
|
// a minimum level as state-level, so the analyzer takes them for variables.
|
package/src/errors.ts
CHANGED
|
@@ -34,10 +34,12 @@ export interface SanyError {
|
|
|
34
34
|
message: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// The errors SANY reports for a spec. A fatal error stops it before semantic analysis.
|
|
37
|
+
// The errors SANY reports for a spec. A fatal error stops it before semantic analysis. SANY's
|
|
38
|
+
// parser prints its message for a module it rejects before the report.
|
|
38
39
|
export interface SanyReport {
|
|
39
40
|
errors: SanyError[];
|
|
40
41
|
isFatal: boolean;
|
|
42
|
+
parseError?: string;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
export class TlaSyntaxError extends TlaError {
|
|
@@ -52,14 +54,23 @@ export class TlaSyntaxError extends TlaError {
|
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
55
|
-
export const createSanyError = (errors: SanyError[], isFatal = false) =>
|
|
57
|
+
export const createSanyError = (errors: SanyError[], isFatal = false, parseError?: string) =>
|
|
56
58
|
new TlaSyntaxError(
|
|
57
59
|
errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
|
|
58
60
|
undefined,
|
|
59
61
|
undefined,
|
|
60
|
-
{ errors, isFatal },
|
|
62
|
+
{ errors, isFatal, parseError },
|
|
61
63
|
);
|
|
62
64
|
|
|
65
|
+
// SANY's message for a module its parser rejects, such as a "***Parse Error***" with the
|
|
66
|
+
// productions it was in.
|
|
67
|
+
export class TlaParseError extends TlaError {
|
|
68
|
+
constructor(message: string) {
|
|
69
|
+
super(message);
|
|
70
|
+
this.name = "TlaParseError";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
63
74
|
export const formatRange = (range: SourceRange) =>
|
|
64
75
|
`line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
|
|
65
76
|
|
package/src/evaluate.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
getDomain,
|
|
36
36
|
getFunctionDomain,
|
|
37
37
|
getIntervalElements,
|
|
38
|
+
getLazySubsets,
|
|
38
39
|
getSequenceElements,
|
|
39
40
|
getValueHash,
|
|
40
41
|
isFunctionValue,
|
|
@@ -342,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
|
|
|
342
343
|
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
|
|
343
344
|
);
|
|
344
345
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
: toFiniteSet(set).elements;
|
|
346
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
347
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
348
348
|
};
|
|
349
349
|
|
|
350
350
|
export const forEachBinding = (
|
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/levels.ts
CHANGED
|
@@ -5,8 +5,17 @@ import {
|
|
|
5
5
|
type Expression,
|
|
6
6
|
type Module,
|
|
7
7
|
type OperatorParam,
|
|
8
|
+
type Proof,
|
|
9
|
+
type ProofStep,
|
|
10
|
+
type SourceLocation,
|
|
8
11
|
} from "./ast";
|
|
9
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
createSanyError,
|
|
14
|
+
createSemanticError,
|
|
15
|
+
formatRange,
|
|
16
|
+
TlaSyntaxError,
|
|
17
|
+
type SanyError,
|
|
18
|
+
} from "./errors";
|
|
10
19
|
import { mapChildren } from "./instances";
|
|
11
20
|
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
12
21
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
@@ -33,6 +42,7 @@ interface LevelContext {
|
|
|
33
42
|
constants: Set<string>;
|
|
34
43
|
variables: Set<string>;
|
|
35
44
|
getDefinition: (name: string) => Definition | undefined;
|
|
45
|
+
getQualifiedName?: (name: string) => string | undefined;
|
|
36
46
|
}
|
|
37
47
|
|
|
38
48
|
type ScopeEntry =
|
|
@@ -205,7 +215,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
205
215
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
206
216
|
const definition = context.getDefinition(name);
|
|
207
217
|
if (definition) {
|
|
208
|
-
|
|
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
|
+
};
|
|
209
225
|
}
|
|
210
226
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
211
227
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -835,27 +851,111 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
835
851
|
return { analyze, analyzeDefinition, toOperand };
|
|
836
852
|
};
|
|
837
853
|
|
|
838
|
-
const analyzeAssumeProve = (
|
|
854
|
+
export const analyzeAssumeProve = (
|
|
839
855
|
assumeProve: AssumeProve,
|
|
840
856
|
scope: Scope,
|
|
841
857
|
analyzer: ReturnType<typeof createLevelAnalyzer>,
|
|
842
|
-
) => {
|
|
858
|
+
): LevelData => {
|
|
843
859
|
const inner = new Map(scope);
|
|
844
|
-
assumeProve.assumptions.
|
|
860
|
+
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
845
861
|
if (assumption.kind === "NewSymbol") {
|
|
846
|
-
|
|
862
|
+
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
847
863
|
inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
|
|
848
|
-
return;
|
|
864
|
+
return set ? [set] : [];
|
|
849
865
|
}
|
|
850
|
-
|
|
851
|
-
|
|
866
|
+
return [
|
|
867
|
+
assumption.kind === "AssumeProve"
|
|
868
|
+
? analyzeAssumeProve(assumption, inner, analyzer)
|
|
869
|
+
: analyzer.analyze(assumption, inner),
|
|
870
|
+
];
|
|
852
871
|
});
|
|
853
|
-
analyzer.analyze(assumeProve.conclusion, inner);
|
|
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));
|
|
879
|
+
});
|
|
880
|
+
return data;
|
|
854
881
|
};
|
|
855
882
|
|
|
856
|
-
// SANY checks the level of every definition, assumption, and
|
|
857
|
-
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
|
+
) => {
|
|
858
889
|
const analyzer = createLevelAnalyzer(context);
|
|
890
|
+
const getLevel = (expressions: Expression[]) =>
|
|
891
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
|
|
892
|
+
const getStepLevel = (step: ProofStep) => {
|
|
893
|
+
const body = step.body;
|
|
894
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
895
|
+
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
896
|
+
if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
|
|
897
|
+
if (body.kind === "Pick") {
|
|
898
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
899
|
+
}
|
|
900
|
+
if (body.kind !== "Assert") return 0;
|
|
901
|
+
return body.statement.kind === "AssumeProve"
|
|
902
|
+
? analyzeAssumeProve(body.statement, new Map(), analyzer).level
|
|
903
|
+
: getLevel([body.statement]);
|
|
904
|
+
};
|
|
905
|
+
// SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
|
|
906
|
+
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
907
|
+
proof?.kind !== "Steps"
|
|
908
|
+
? []
|
|
909
|
+
: proof.steps.flatMap((step) => {
|
|
910
|
+
const location = formatRange(step.statement);
|
|
911
|
+
const isConstant = () => getStepLevel(step) === 0;
|
|
912
|
+
if (
|
|
913
|
+
step.body.kind === "Have" ||
|
|
914
|
+
step.body.kind === "Take" ||
|
|
915
|
+
step.body.kind === "Witness"
|
|
916
|
+
) {
|
|
917
|
+
return isConstant()
|
|
918
|
+
? []
|
|
919
|
+
: [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
|
|
920
|
+
}
|
|
921
|
+
if (step.body.kind === "Case") {
|
|
922
|
+
return [
|
|
923
|
+
...(isConstant()
|
|
924
|
+
? []
|
|
925
|
+
: [{ location, message: "Non-constant CASE for temporal goal." }]),
|
|
926
|
+
...checkTemporalGoal(step.proof),
|
|
927
|
+
];
|
|
928
|
+
}
|
|
929
|
+
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
930
|
+
});
|
|
931
|
+
// SANY checks each step as a theorem of its own after the steps of its proof.
|
|
932
|
+
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
933
|
+
proof?.kind !== "Steps"
|
|
934
|
+
? []
|
|
935
|
+
: proof.steps.flatMap((step) => {
|
|
936
|
+
const body = step.body;
|
|
937
|
+
const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
|
|
938
|
+
const pickErrors =
|
|
939
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
|
|
940
|
+
? body.bounds
|
|
941
|
+
.filter((bound) => getLevel([bound.set]) !== 0)
|
|
942
|
+
.flatMap((bound) =>
|
|
943
|
+
bound.set.location.range
|
|
944
|
+
? [
|
|
945
|
+
{
|
|
946
|
+
location: formatRange(bound.set.location.range),
|
|
947
|
+
message: "Non-constant bound of temporal PICK.",
|
|
948
|
+
},
|
|
949
|
+
]
|
|
950
|
+
: [],
|
|
951
|
+
)
|
|
952
|
+
: [];
|
|
953
|
+
return [
|
|
954
|
+
...checkProof(step.proof),
|
|
955
|
+
...pickErrors,
|
|
956
|
+
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
957
|
+
];
|
|
958
|
+
});
|
|
859
959
|
collectSemanticErrors([
|
|
860
960
|
...module.definitions
|
|
861
961
|
.filter((definition) => !definition.name.includes("!"))
|
|
@@ -869,10 +969,18 @@ export const checkModuleLevels = (module: Module, context: LevelContext) => {
|
|
|
869
969
|
})),
|
|
870
970
|
...module.theorems.map((theorem) => ({
|
|
871
971
|
location: theorem.location,
|
|
872
|
-
check: () =>
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
972
|
+
check: () => {
|
|
973
|
+
const data =
|
|
974
|
+
theorem.statement.kind === "AssumeProve"
|
|
975
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
976
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
977
|
+
const errors = [
|
|
978
|
+
...checkProof(theorem.proof),
|
|
979
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
980
|
+
];
|
|
981
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
982
|
+
},
|
|
876
983
|
})),
|
|
984
|
+
...instances,
|
|
877
985
|
]);
|
|
878
986
|
};
|
package/src/parser.ts
CHANGED
|
@@ -329,8 +329,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
329
329
|
const tokenEnds: SourceLocation[] = [];
|
|
330
330
|
const tokenRanges: SourceRange[] = [];
|
|
331
331
|
|
|
332
|
-
const getRange = (startIndex: number) => {
|
|
333
|
-
const endIndex = cursor.index - 1;
|
|
332
|
+
const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
|
|
334
333
|
const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
|
|
335
334
|
if (cached) return cached;
|
|
336
335
|
const last = tokens[endIndex];
|
|
@@ -628,9 +627,15 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
628
627
|
const body = parseExpression();
|
|
629
628
|
return withRange({ kind: "Lambda", params, body, location: getLocation(token) }, start);
|
|
630
629
|
}
|
|
631
|
-
|
|
630
|
+
const argument = parseExpression();
|
|
631
|
+
// SANY reads an argument that only names an operator of a parameterized instance, such as
|
|
632
|
+
// I!Op, as that operator, which takes the arguments of the instance too.
|
|
633
|
+
if (argument.kind === "Identifier") missingArguments.delete(argument);
|
|
634
|
+
return argument;
|
|
632
635
|
};
|
|
633
636
|
|
|
637
|
+
const missingArguments = new Map<Expression, { location: string; message: string }>();
|
|
638
|
+
|
|
634
639
|
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
635
640
|
|
|
636
641
|
const parseArguments = () =>
|
|
@@ -830,7 +835,8 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
830
835
|
};
|
|
831
836
|
|
|
832
837
|
const parsePrimary = (): Expression => {
|
|
833
|
-
const
|
|
838
|
+
const start = cursor.index;
|
|
839
|
+
const isFactStart = start === factStart.index;
|
|
834
840
|
const token = advance();
|
|
835
841
|
const location = getLocation(token);
|
|
836
842
|
if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
|
|
@@ -869,33 +875,65 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
869
875
|
const instance = openModules
|
|
870
876
|
.flatMap((module) => module.instances)
|
|
871
877
|
.find((candidate) => candidate.name === name);
|
|
872
|
-
|
|
878
|
+
const isMissingArguments =
|
|
879
|
+
instance !== undefined &&
|
|
880
|
+
!isFactStart &&
|
|
881
|
+
!isTlapm &&
|
|
882
|
+
instance.params.length !== args.length;
|
|
883
|
+
if (isMissingArguments && args.length > 0) {
|
|
873
884
|
throw createError(
|
|
874
885
|
`The operator ${name} requires ${instance.params.length} arguments`,
|
|
875
886
|
token,
|
|
876
887
|
);
|
|
877
888
|
}
|
|
878
889
|
const selectors: Selector[] = [{ kind: "Name", name, args }];
|
|
890
|
+
const selectorRanges = [getRange(start, start)];
|
|
879
891
|
while (isSelectorAhead()) {
|
|
880
892
|
advance();
|
|
893
|
+
selectorRanges.push(getRange(cursor.index, cursor.index));
|
|
881
894
|
selectors.push(parseSelector());
|
|
882
895
|
}
|
|
883
896
|
const names = selectors.flatMap((selector) =>
|
|
884
897
|
selector.kind === "Name" ? [selector.name] : [],
|
|
885
898
|
);
|
|
886
|
-
if (names.length < selectors.length) return { kind: "Subexpression", selectors, location };
|
|
887
899
|
const operator = names.join("!");
|
|
888
900
|
const qualifiedArgs = selectors.flatMap((selector) =>
|
|
889
901
|
"args" in selector ? selector.args : [],
|
|
890
902
|
);
|
|
891
|
-
if (qualifiedArgs.length === 0) return { kind: "Identifier", name: operator, location };
|
|
892
903
|
const argumentCounts = selectors.map((selector) =>
|
|
893
904
|
"args" in selector ? selector.args.length : 0,
|
|
894
905
|
);
|
|
895
906
|
const argumentsRange = lastArguments.range;
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
907
|
+
const reference: Expression =
|
|
908
|
+
names.length < selectors.length
|
|
909
|
+
? { kind: "Subexpression", selectors, selectorRanges, location }
|
|
910
|
+
: qualifiedArgs.length === 0
|
|
911
|
+
? { kind: "Identifier", name: operator, selectorRanges, location }
|
|
912
|
+
: argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
913
|
+
? {
|
|
914
|
+
kind: "Apply",
|
|
915
|
+
operator,
|
|
916
|
+
args: qualifiedArgs,
|
|
917
|
+
argumentCounts,
|
|
918
|
+
argumentsRange,
|
|
919
|
+
selectorRanges,
|
|
920
|
+
location,
|
|
921
|
+
}
|
|
922
|
+
: {
|
|
923
|
+
kind: "Apply",
|
|
924
|
+
operator,
|
|
925
|
+
args: qualifiedArgs,
|
|
926
|
+
argumentsRange,
|
|
927
|
+
selectorRanges,
|
|
928
|
+
location,
|
|
929
|
+
};
|
|
930
|
+
if (isMissingArguments) {
|
|
931
|
+
missingArguments.set(reference, {
|
|
932
|
+
location: formatRange(getRange(start)),
|
|
933
|
+
message: `The operator ${name} requires ${instance.params.length} arguments.`,
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
return reference;
|
|
899
937
|
}
|
|
900
938
|
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
901
939
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
@@ -1574,10 +1612,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1574
1612
|
const parseStep = (): ProofStep => {
|
|
1575
1613
|
const start = cursor.index;
|
|
1576
1614
|
const step = parseStepStart(isAt("QED", 1));
|
|
1615
|
+
const statementStart = cursor.index;
|
|
1577
1616
|
const scope = new Set<string>();
|
|
1578
1617
|
proofScopes.push(scope);
|
|
1579
1618
|
const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
|
|
1580
1619
|
const head = getRange(start);
|
|
1620
|
+
const statement = getRange(statementStart);
|
|
1581
1621
|
const hasProof = isProofStart(peek());
|
|
1582
1622
|
if (hasProof && !PROVABLE_STEPS.has(body.kind)) {
|
|
1583
1623
|
throw createError("Proof of a step that does not take a proof");
|
|
@@ -1585,7 +1625,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1585
1625
|
const proof = hasProof ? parseProof() : undefined;
|
|
1586
1626
|
proofScopes.pop();
|
|
1587
1627
|
if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
|
|
1588
|
-
return withRange({ ...step, body, head, proof }, start);
|
|
1628
|
+
return withRange({ ...step, body, head, statement, proof }, start);
|
|
1589
1629
|
};
|
|
1590
1630
|
|
|
1591
1631
|
const parseProofBody = (): Proof => {
|
|
@@ -1716,6 +1756,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1716
1756
|
end: { line: token.line, column: token.endColumn },
|
|
1717
1757
|
};
|
|
1718
1758
|
}
|
|
1759
|
+
if (missingArguments.size > 0) throw createSanyError([...missingArguments.values()]);
|
|
1719
1760
|
openModules.pop();
|
|
1720
1761
|
recursiveScopes.pop();
|
|
1721
1762
|
return module;
|