tla-ts 0.0.10-dev.37a75eb5b → 0.0.10-dev.45c0eaef5
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/compile.cjs +3 -3
- package/dist/compile.js +1 -1
- package/dist/deciders.cjs +33 -33
- package/dist/deciders.js +53 -53
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +17 -3
- package/dist/index.d.ts +17 -3
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +55 -55
- package/dist/prove.js +78 -78
- package/dist/run.cjs +40 -42
- package/dist/run.js +42 -44
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +96 -91
- package/dist/transfer.js +95 -90
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +1 -1
- package/src/ast.ts +31 -3
- package/src/bags.ts +12 -10
- package/src/builtins.ts +4 -3
- package/src/check-failure.ts +13 -11
- package/src/check-trace.ts +146 -93
- package/src/check.ts +418 -246
- package/src/cli/tlc-output.ts +17 -2
- package/src/cli/tlc.ts +5 -4
- package/src/cli/xml-exporter.ts +1 -1
- package/src/codegen.ts +23 -10
- package/src/coverage.ts +255 -18
- package/src/dependencies.ts +68 -15
- package/src/dfid.ts +93 -35
- package/src/environment.ts +106 -39
- package/src/errors.ts +7 -0
- package/src/evaluate.ts +415 -132
- package/src/exit-codes.ts +28 -15
- package/src/instances.ts +135 -11
- package/src/java-hash.ts +14 -0
- package/src/java-overrides.ts +2 -2
- package/src/levels.ts +674 -236
- package/src/lexer.ts +12 -2
- package/src/liveness-checker.ts +161 -60
- package/src/liveness.ts +55 -10
- package/src/obligations/index.ts +1 -1
- package/src/obligations/normalize.ts +1 -4
- package/src/obligations/printer.ts +5 -5
- package/src/obligations/source.ts +2 -0
- package/src/parser.ts +110 -111
- package/src/prover/isabelle/isar/attrib.ts +4 -4
- package/src/prover/isabelle/isar/rule-cases.ts +8 -6
- package/src/prover/isabelle/provers/clasimp.ts +1 -1
- package/src/prover/isabelle/provers/hypsubst.ts +2 -2
- package/src/prover/isabelle/pure/rewriter.ts +3 -3
- package/src/prover/isabelle/pure/term.ts +3 -3
- package/src/prover/prep/md5.ts +26 -29
- package/src/prover/smt/rewrite.ts +2 -7
- package/src/prover/smt/simplify.ts +1 -1
- package/src/prover/smt/solver/arith-rewriter.ts +11 -3
- package/src/prover/smt/solver/array-rewriter.ts +12 -2
- package/src/prover/smt/solver/asserted-formulas.ts +164 -58
- package/src/prover/smt/solver/ast-lt.ts +1 -1
- package/src/prover/smt/solver/ast.ts +65 -13
- package/src/prover/smt/solver/auf-solver.ts +23 -12
- package/src/prover/smt/solver/bit-blast-tactic.ts +27 -1
- package/src/prover/smt/solver/blast-term-ite.ts +47 -0
- package/src/prover/smt/solver/bound-simplifier.ts +11 -13
- package/src/prover/smt/solver/bv-decl-plugin.ts +13 -10
- package/src/prover/smt/solver/bv-rewriter.ts +1 -1
- package/src/prover/smt/solver/card2bv.ts +35 -18
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -24
- package/src/prover/smt/solver/context.ts +65 -14
- package/src/prover/smt/solver/ctx-simplify.ts +1 -2
- package/src/prover/smt/solver/datatype-decl-plugin.ts +15 -7
- package/src/prover/smt/solver/dependent-expr-state.ts +28 -16
- package/src/prover/smt/solver/diff-neq.ts +1 -2
- package/src/prover/smt/solver/dt2bv-tactic.ts +57 -21
- package/src/prover/smt/solver/elim-bounds.ts +89 -0
- package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +5 -8
- package/src/prover/smt/solver/euf/arith-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/array-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/bv-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/egraph.ts +3 -9
- package/src/prover/smt/solver/euf/euf-solver.ts +1 -2
- package/src/prover/smt/solver/euf/mam.ts +1 -5
- package/src/prover/smt/solver/euf/q-ematch.ts +1 -2
- package/src/prover/smt/solver/euf/th-solver.ts +0 -7
- package/src/prover/smt/solver/fd-solver.ts +608 -141
- package/src/prover/smt/solver/fix-dl-var.ts +1 -2
- package/src/prover/smt/solver/fpa2bv-rewriter.ts +1 -0
- package/src/prover/smt/solver/goal.ts +4 -3
- package/src/prover/smt/solver/hashtable.ts +4 -0
- package/src/prover/smt/solver/internalizer.ts +1 -1
- package/src/prover/smt/solver/logic-tactics.ts +15 -5
- package/src/prover/smt/solver/lp/core-solver.ts +12 -8
- package/src/prover/smt/solver/lp/lar-solver.ts +1 -1
- package/src/prover/smt/solver/lp/lp-settings.ts +6 -2
- package/src/prover/smt/solver/lp/nla-grobner.ts +2 -2
- package/src/prover/smt/solver/lp/pdd.ts +2 -3
- package/src/prover/smt/solver/lp/union-find.ts +1 -1
- package/src/prover/smt/solver/mam-compiler.ts +1 -1
- package/src/prover/smt/solver/maxcore.ts +2 -1
- package/src/prover/smt/solver/maxsmt-base.ts +1 -0
- package/src/prover/smt/solver/model-based-opt.ts +1 -1
- package/src/prover/smt/solver/model-checker.ts +1 -1
- package/src/prover/smt/solver/model-evaluator.ts +2 -0
- package/src/prover/smt/solver/model-generator.ts +85 -43
- package/src/prover/smt/solver/mpf.ts +2 -4
- package/src/prover/smt/solver/mus.ts +2 -3
- package/src/prover/smt/solver/muz/dl-context.ts +14 -42
- package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
- package/src/prover/smt/solver/muz/dl-mk-slice.ts +3 -3
- package/src/prover/smt/solver/muz/dl-rule.ts +8 -9
- package/src/prover/smt/solver/muz/hnf.ts +2 -2
- package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +1 -1
- package/src/prover/smt/solver/muz/horn-tactic.ts +15 -2
- package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
- package/src/prover/smt/solver/muz/spacer-context.ts +1 -4
- package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
- package/src/prover/smt/solver/nlqsat.ts +1 -1
- package/src/prover/smt/solver/nlsat/algebraic.ts +4 -5
- package/src/prover/smt/solver/nlsat/levelwise.ts +0 -4
- package/src/prover/smt/solver/nlsat/polynomial.ts +1 -36
- package/src/prover/smt/solver/nlsat/upolynomial.ts +1 -1
- package/src/prover/smt/solver/nlsat-tactic.ts +1 -1
- package/src/prover/smt/solver/nnf.ts +6 -2
- package/src/prover/smt/solver/opt-context.ts +16 -7
- package/src/prover/smt/solver/opt-pareto.ts +19 -0
- package/src/prover/smt/solver/opt-preprocess.ts +1 -1
- package/src/prover/smt/solver/opt-solver.ts +4 -3
- package/src/prover/smt/solver/optsmt.ts +1 -1
- package/src/prover/smt/solver/parallel-tactic.ts +1 -1
- package/src/prover/smt/solver/pb-rewriter.ts +1 -4
- package/src/prover/smt/solver/poly-rewriter.ts +75 -18
- package/src/prover/smt/solver/probes.ts +88 -23
- package/src/prover/smt/solver/proto-model.ts +8 -8
- package/src/prover/smt/solver/push-app-ite.ts +37 -0
- package/src/prover/smt/solver/qe-arith-plugin.ts +2 -4
- package/src/prover/smt/solver/qe-lite.ts +2 -2
- package/src/prover/smt/solver/qe.ts +3 -2
- package/src/prover/smt/solver/qsat.ts +23 -8
- package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
- package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
- package/src/prover/smt/solver/rewriter.ts +31 -25
- package/src/prover/smt/solver/rlimit.ts +7 -3
- package/src/prover/smt/solver/sat/config.ts +1 -1
- package/src/prover/smt/solver/sat/goal2sat.ts +65 -12
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +129 -43
- package/src/prover/smt/solver/sat/model-converter.ts +1 -1
- package/src/prover/smt/solver/sat/pb-constraint.ts +7 -0
- package/src/prover/smt/solver/sat/pb-solver.ts +26 -7
- package/src/prover/smt/solver/sat/sat-tactic.ts +15 -6
- package/src/prover/smt/solver/sat/sat2goal.ts +72 -48
- package/src/prover/smt/solver/sat/simplifier.ts +1 -1
- package/src/prover/smt/solver/sat/solver.ts +27 -47
- package/src/prover/smt/solver/seq-axioms.ts +20 -35
- package/src/prover/smt/solver/seq-decl-plugin.ts +1 -1
- package/src/prover/smt/solver/seq-derive.ts +1 -1
- package/src/prover/smt/solver/seq-eq-solver.ts +2 -2
- package/src/prover/smt/solver/seq-monadic.ts +7 -5
- package/src/prover/smt/solver/seq-re-rewriter.ts +2 -1
- package/src/prover/smt/solver/seq-re-util.ts +1 -1
- package/src/prover/smt/solver/seq-regex-bisim.ts +1 -1
- package/src/prover/smt/solver/seq-regex-live.ts +2 -2
- package/src/prover/smt/solver/seq-rewriter.ts +31 -29
- package/src/prover/smt/solver/seq-skolem.ts +0 -23
- package/src/prover/smt/solver/seq-view-witness.ts +4 -4
- package/src/prover/smt/solver/smt-consequences.ts +1 -1
- package/src/prover/smt/solver/smt-kernel.ts +40 -6
- package/src/prover/smt/solver/smt-setup.ts +106 -13
- package/src/prover/smt/solver/smtlib-parser.ts +579 -214
- package/src/prover/smt/solver/smtlib-script.ts +600 -170
- package/src/prover/smt/solver/solve-eqs.ts +36 -37
- package/src/prover/smt/solver/solver.ts +18 -3
- package/src/prover/smt/solver/tactic.ts +342 -196
- package/src/prover/smt/solver/theory-bv.ts +13 -3
- package/src/prover/smt/solver/theory-fpa.ts +1 -1
- package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
- package/src/prover/smt/solver/theory-lra-internalize.ts +2 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +11 -3
- package/src/prover/smt/solver/theory-lra-state.ts +1 -1
- package/src/prover/smt/solver/theory-lra.ts +1 -1
- package/src/prover/smt/solver/theory-pb.ts +24 -7
- package/src/prover/smt/solver/theory-seq-solve.ts +1 -1
- package/src/prover/smt/solver/theory-seq-state.ts +2 -21
- package/src/prover/smt/solver/theory-without-terms.ts +24 -0
- package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
- package/src/prover/tlapm/cache.ts +8 -8
- package/src/prover/zenon/index-table.ts +5 -2
- package/src/prover/zenon/isar-case.ts +1 -1
- package/src/prover/zenon/isar.ts +2 -2
- package/src/prover/zenon/phrase.ts +1 -1
- package/src/resolve.ts +1226 -246
- package/src/runtime.ts +19 -2
- package/src/sany/builtins.ts +10 -10
- package/src/sany/generation.ts +4 -2
- package/src/sany/generator-def-entries.ts +123 -36
- package/src/sany/generator-definitions.ts +4 -2
- package/src/sany/generator-expressions.ts +51 -20
- package/src/sany/generator-proofs.ts +17 -11
- package/src/sany/generator.ts +11 -5
- package/src/sany/lexer.ts +15 -7
- package/src/sany/modules.ts +4 -2
- package/src/sany/nodes.ts +1 -1
- package/src/sany/selectors.ts +11 -6
- package/src/sany/xml.ts +2 -3
- package/src/search.ts +283 -126
- package/src/simulate.ts +247 -185
- package/src/spec-description.ts +15 -7
- package/src/standard-definitions.ts +1 -0
- package/src/standard-modules.ts +56 -22
- package/src/subexpressions.ts +563 -139
- package/src/successors.ts +50 -20
- package/src/symmetry.ts +2 -1
- package/src/tlc/jar-data.ts +1 -3
- package/src/tlc/messages.ts +3 -3
- package/src/tlc/random.ts +2 -1
- package/src/tlc/replay.ts +147 -46
- package/src/tlc/tlc-operators.ts +10 -8
- package/src/tlc/tlcext-operators.ts +26 -10
- package/src/tlc/trace-spec.ts +9 -22
- package/src/trace-liveness.ts +529 -0
- package/src/typed-tier.ts +52 -31
- package/src/typescript/types.ts +2 -2
- package/src/values.ts +150 -79
package/src/levels.ts
CHANGED
|
@@ -1,28 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getChildren,
|
|
3
3
|
type AssumeProve,
|
|
4
|
+
type Bound,
|
|
4
5
|
type Definition,
|
|
5
6
|
type Expression,
|
|
7
|
+
type Fact,
|
|
8
|
+
type LetExpression,
|
|
6
9
|
type Module,
|
|
10
|
+
type ModuleUnit,
|
|
11
|
+
type NewSymbol,
|
|
7
12
|
type OperatorParam,
|
|
8
13
|
type Proof,
|
|
9
14
|
type ProofStep,
|
|
10
15
|
type SourceLocation,
|
|
16
|
+
type SourceRange,
|
|
11
17
|
} from "./ast";
|
|
12
|
-
import {
|
|
13
|
-
createSanyError,
|
|
14
|
-
createSemanticError,
|
|
15
|
-
formatRange,
|
|
16
|
-
TlaSyntaxError,
|
|
17
|
-
type SanyError,
|
|
18
|
-
} from "./errors";
|
|
18
|
+
import { formatRange, type SanyError } from "./errors";
|
|
19
19
|
import { forEachChild } from "./instances";
|
|
20
|
-
import {
|
|
20
|
+
import { parenthesizedRanges } from "./parser";
|
|
21
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames, isBefore } from "./resolve";
|
|
21
22
|
import { resolveSynonym } from "./tlc/jar-data";
|
|
22
23
|
import { STANDARD_LEVELS } from "./standard-definitions";
|
|
23
24
|
|
|
24
25
|
interface LevelData {
|
|
25
26
|
level: number;
|
|
27
|
+
isCorrect: boolean;
|
|
26
28
|
params: Set<string>;
|
|
27
29
|
constraints: Map<string, number>;
|
|
28
30
|
argConstraints: Map<string, number>;
|
|
@@ -47,7 +49,7 @@ interface LevelContext {
|
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
type ScopeEntry =
|
|
50
|
-
| { kind: "Formal"; arity: number }
|
|
52
|
+
| { kind: "Formal"; arity: number; level: number }
|
|
51
53
|
| { kind: "Let"; definition: Definition; scope: Scope };
|
|
52
54
|
|
|
53
55
|
export type Scope = Map<string, ScopeEntry>;
|
|
@@ -56,6 +58,19 @@ type Resolved =
|
|
|
56
58
|
| { kind: "Declared"; name: string; level: number; arity: number }
|
|
57
59
|
| { kind: "Defined"; levels: OperatorLevels };
|
|
58
60
|
|
|
61
|
+
interface SectionMember {
|
|
62
|
+
definition: Definition;
|
|
63
|
+
scope: Scope;
|
|
64
|
+
isRechecked: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface RecursiveSection {
|
|
68
|
+
location: SourceLocation;
|
|
69
|
+
members: SectionMember[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
type ScopeFrame = Definition | LetExpression | string[];
|
|
73
|
+
|
|
59
74
|
type Operand =
|
|
60
75
|
| { kind: "Value"; data: LevelData }
|
|
61
76
|
| { kind: "Operator"; data: OperatorLevels }
|
|
@@ -66,6 +81,14 @@ export const VARIABLE_LEVEL = 1;
|
|
|
66
81
|
export const ACTION_LEVEL = 2;
|
|
67
82
|
export const TEMPORAL_LEVEL = 3;
|
|
68
83
|
|
|
84
|
+
const NEW_SYMBOL_LEVELS = {
|
|
85
|
+
Constant: CONSTANT_LEVEL,
|
|
86
|
+
Variable: VARIABLE_LEVEL,
|
|
87
|
+
State: VARIABLE_LEVEL,
|
|
88
|
+
Action: ACTION_LEVEL,
|
|
89
|
+
Temporal: TEMPORAL_LEVEL,
|
|
90
|
+
} satisfies Record<NewSymbol["level"], number>;
|
|
91
|
+
|
|
69
92
|
const SEPARATOR = "\u0000";
|
|
70
93
|
|
|
71
94
|
export const getArgumentKey = (operator: string, index: number) =>
|
|
@@ -84,6 +107,7 @@ const getConditionKey = (operator: number, operand: number, argument: number) =>
|
|
|
84
107
|
|
|
85
108
|
const createData = (level = 0): LevelData => ({
|
|
86
109
|
level,
|
|
110
|
+
isCorrect: true,
|
|
87
111
|
params: new Set(),
|
|
88
112
|
constraints: new Map(),
|
|
89
113
|
argConstraints: new Map(),
|
|
@@ -155,27 +179,50 @@ const CORE_SIGNATURES = new Map(
|
|
|
155
179
|
["BOOLEAN", 0, [], []],
|
|
156
180
|
["STRING", 0, [], []],
|
|
157
181
|
] as const
|
|
158
|
-
).map((
|
|
159
|
-
|
|
182
|
+
).map((signature): [string, OperatorLevels] => [
|
|
183
|
+
signature[0],
|
|
160
184
|
{
|
|
161
|
-
...createSignature(
|
|
162
|
-
nonLeibniz: new Set(
|
|
185
|
+
...createSignature(signature[0], signature[1], [...signature[2]], [...signature[3]]),
|
|
186
|
+
nonLeibniz: new Set(signature[1] > 1 ? [0, 1] : []),
|
|
163
187
|
},
|
|
164
188
|
]),
|
|
165
189
|
);
|
|
166
190
|
|
|
167
|
-
const SET_LEVELS = createSignature("$Set", 0, [2], [1]);
|
|
168
|
-
const JUNCTION_LEVELS = createSignature("$Junction", 0, [3], [1]);
|
|
169
|
-
const CHOOSE_LEVELS = createSignature("$Choose", 0, [2], [1]);
|
|
170
|
-
const FUNCTION_LEVELS = createSignature("$Function", 0, [3], [1]);
|
|
171
|
-
const APPLY_LEVELS = createSignature("$FcnApply", 0, [2, 2], [1, 1]);
|
|
172
|
-
const SELECT_LEVELS = createSignature("$RcdSelect", 0, [2, 0], [1, 1]);
|
|
173
191
|
const PRIME_LEVELS = createSignature("'", 2, [1], [0]);
|
|
192
|
+
const UNCHANGED_LEVELS = createSignature("UNCHANGED", 2, [1], [0]);
|
|
174
193
|
const ENABLED_LEVELS = createSignature("ENABLED", 1, [2], [0]);
|
|
175
|
-
const
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
const
|
|
194
|
+
const ALWAYS_LEVELS = createSignature("[]", 3, [3], [0]);
|
|
195
|
+
const EVENTUALLY_LEVELS = createSignature("<>", 3, [3], [0]);
|
|
196
|
+
const AND_LEVELS = createSignature("$ConjList", 0, [3], [1]);
|
|
197
|
+
const OR_LEVELS = createSignature("$DisjList", 0, [3], [1]);
|
|
198
|
+
const IF_LEVELS = createSignature("$IfThenElse", 0, [3], [1]);
|
|
199
|
+
const CASE_LEVELS = createSignature("$Case", 0, [3], [1]);
|
|
200
|
+
const FORALL_LEVELS = createSignature("$BoundedForall", 0, [3], [1]);
|
|
201
|
+
const EXISTS_LEVELS = createSignature("$BoundedExists", 0, [3], [1]);
|
|
202
|
+
const UNBOUNDED_FORALL_LEVELS = createSignature("$UnboundedForall", 0, [3], [1]);
|
|
203
|
+
const UNBOUNDED_EXISTS_LEVELS = createSignature("$UnboundedExists", 0, [3], [1]);
|
|
204
|
+
const TEMPORAL_FORALL_LEVELS = createSignature("$TemporalForall", 3, [3], [0]);
|
|
205
|
+
const TEMPORAL_EXISTS_LEVELS = createSignature("$TemporalExists", 3, [3], [0]);
|
|
206
|
+
const BOUNDED_CHOOSE_LEVELS = createSignature("$BoundedChoose", 0, [2], [1]);
|
|
207
|
+
const UNBOUNDED_CHOOSE_LEVELS = createSignature("$UnboundedChoose", 0, [2], [1]);
|
|
208
|
+
const SET_ENUMERATE_LEVELS = createSignature("$SetEnumerate", 0, [2], [1]);
|
|
209
|
+
const TUPLE_LEVELS = createSignature("$Tuple", 0, [2], [1]);
|
|
210
|
+
const SET_OF_FCNS_LEVELS = createSignature("$SetOfFcns", 0, [2], [1]);
|
|
211
|
+
const CARTESIAN_PROD_LEVELS = createSignature("$CartesianProd", 0, [2], [1]);
|
|
212
|
+
const SUBSET_OF_LEVELS = createSignature("$SubsetOf", 0, [2], [1]);
|
|
213
|
+
const SET_OF_ALL_LEVELS = createSignature("$SetOfAll", 0, [2], [1]);
|
|
214
|
+
const FCN_CONSTRUCTOR_LEVELS = createSignature("$FcnConstructor", 0, [3], [1]);
|
|
215
|
+
const RCD_CONSTRUCTOR_LEVELS = createSignature("$RcdConstructor", 0, [3], [1]);
|
|
216
|
+
const SET_OF_RCDS_LEVELS = createSignature("$SetOfRcds", 0, [2], [1]);
|
|
217
|
+
const EXCEPT_LEVELS = createSignature("$Except", 0, [2], [1]);
|
|
218
|
+
const PAIR_LEVELS = createSignature("$Pair", 0, [3], [1]);
|
|
219
|
+
const SEQ_LEVELS = createSignature("$Seq", 0, [2], [1]);
|
|
220
|
+
const APPLY_LEVELS = createSignature("$FcnApply", 0, [2, 2], [1, 1]);
|
|
221
|
+
const SELECT_LEVELS = createSignature("$RcdSelect", 0, [2, 0], [1, 1]);
|
|
222
|
+
const SQUARE_ACT_LEVELS = createSignature("$SquareAct", 2, [2, 1], [0, 0]);
|
|
223
|
+
const ANGLE_ACT_LEVELS = createSignature("$AngleAct", 2, [2, 1], [0, 0]);
|
|
224
|
+
const WF_LEVELS = createSignature("$WF", 3, [1, 2], [0, 0]);
|
|
225
|
+
const SF_LEVELS = createSignature("$SF", 3, [1, 2], [0, 0]);
|
|
179
226
|
|
|
180
227
|
const bindFormals = (scope: Scope, formals: OperatorParam[]) =>
|
|
181
228
|
formals.length === 0
|
|
@@ -184,31 +231,204 @@ const bindFormals = (scope: Scope, formals: OperatorParam[]) =>
|
|
|
184
231
|
...scope,
|
|
185
232
|
...formals.map((formal): [string, ScopeEntry] => [
|
|
186
233
|
formal.name,
|
|
187
|
-
{ kind: "Formal", arity: formal.arity },
|
|
234
|
+
{ kind: "Formal", arity: formal.arity, level: CONSTANT_LEVEL },
|
|
188
235
|
]),
|
|
189
236
|
]);
|
|
190
237
|
|
|
191
238
|
const toFormals = (names: string[]) => names.map((name) => ({ name, arity: 0 }));
|
|
192
239
|
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
240
|
+
const getFormals = (definition: Definition) =>
|
|
241
|
+
definition.kind === "Operator" ? definition.params : [];
|
|
242
|
+
|
|
243
|
+
const getDefinitionBody = (definition: Definition): Expression =>
|
|
244
|
+
definition.kind === "Function"
|
|
245
|
+
? {
|
|
246
|
+
kind: "FunctionConstruction",
|
|
247
|
+
bounds: definition.bounds,
|
|
248
|
+
body: definition.body,
|
|
249
|
+
location: definition.location,
|
|
250
|
+
}
|
|
251
|
+
: definition.body;
|
|
252
|
+
|
|
253
|
+
const createLetScope = (expression: LetExpression, scope: Scope) => {
|
|
254
|
+
const inner: Scope = new Map(scope);
|
|
255
|
+
expression.definitions.forEach((definition) =>
|
|
256
|
+
inner.set(definition.name, { kind: "Let", definition, scope: inner }),
|
|
257
|
+
);
|
|
258
|
+
return inner;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const createPlaceholder = (definition: Definition, maxLevel: number, weight: number) => {
|
|
262
|
+
const formals = getFormals(definition);
|
|
263
|
+
return {
|
|
264
|
+
...createData(),
|
|
265
|
+
name: definition.name,
|
|
266
|
+
formals,
|
|
267
|
+
maxLevels: formals.map(() => maxLevel),
|
|
268
|
+
weights: formals.map(() => weight),
|
|
269
|
+
minMaxLevels: formals.map(() => []),
|
|
270
|
+
conditions: new Set<string>(),
|
|
271
|
+
nonLeibniz: new Set<number>(),
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const getRecursiveSections = (
|
|
276
|
+
base: Scope,
|
|
277
|
+
root: ModuleUnit[] | LetExpression,
|
|
278
|
+
hasRecursiveLet = true,
|
|
279
|
+
) => {
|
|
280
|
+
const sections: RecursiveSection[] = [];
|
|
281
|
+
const pending: Set<string>[] = [];
|
|
282
|
+
const frames: ScopeFrame[] = [];
|
|
283
|
+
const scopes: (Scope | undefined)[] = [];
|
|
284
|
+
let pendingCount = 0;
|
|
285
|
+
let depth = 0;
|
|
286
|
+
|
|
287
|
+
const getScope = () => {
|
|
288
|
+
let start = frames.length;
|
|
289
|
+
while (start > 0 && scopes[start - 1] === undefined) start--;
|
|
290
|
+
let scope = scopes[start - 1] ?? base;
|
|
291
|
+
for (let index = start; index < frames.length; index++) {
|
|
292
|
+
const frame = frames[index];
|
|
293
|
+
scope = Array.isArray(frame)
|
|
294
|
+
? bindFormals(scope, toFormals(frame))
|
|
295
|
+
: frame.kind === "Let"
|
|
296
|
+
? createLetScope(frame, scope)
|
|
297
|
+
: frame.kind === "Function"
|
|
298
|
+
? bindFormals(scope, toFormals(getBoundNames(frame.bounds)))
|
|
299
|
+
: bindFormals(scope, frame.params);
|
|
300
|
+
scopes[index] = scope;
|
|
301
|
+
}
|
|
302
|
+
return scope;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const walkInFrame = (frame: ScopeFrame, expression: Expression) => {
|
|
306
|
+
frames.push(frame);
|
|
307
|
+
scopes.push(undefined);
|
|
308
|
+
walkExpression(expression);
|
|
309
|
+
frames.pop();
|
|
310
|
+
scopes.pop();
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const visitChild = (child: Expression, bound: string[]) =>
|
|
314
|
+
bound.length === 0 ? walkExpression(child) : walkInFrame(bound, child);
|
|
315
|
+
|
|
316
|
+
const walkExpression = (expression: Expression): void => {
|
|
317
|
+
if (expression.kind !== "Let") return forEachChild(expression, visitChild);
|
|
318
|
+
frames.push(expression);
|
|
319
|
+
scopes.push(undefined);
|
|
320
|
+
depth++;
|
|
321
|
+
if (expression.recursive) walkUnits(getLetUnits(expression));
|
|
322
|
+
else {
|
|
323
|
+
for (let index = 0; index < expression.definitions.length; index++) {
|
|
324
|
+
walkDefinition(expression.definitions[index]);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
depth--;
|
|
328
|
+
walkExpression(expression.body);
|
|
329
|
+
frames.pop();
|
|
330
|
+
scopes.pop();
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
const walkDefinition = (definition: Definition) => {
|
|
334
|
+
if (definition.kind === "Function") {
|
|
335
|
+
for (let index = 0; index < definition.bounds.length; index++) {
|
|
336
|
+
walkExpression(definition.bounds[index].set);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
walkInFrame(definition, definition.body);
|
|
340
|
+
if (pendingCount === 0) return;
|
|
341
|
+
const names = (pending[depth] ??= new Set());
|
|
342
|
+
if (definition.isRecursive && names.delete(definition.name)) {
|
|
343
|
+
pendingCount--;
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
sections.at(-1)?.members.push({ definition, scope: getScope(), isRechecked: names.size > 0 });
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const declareRecursive = (definitions: Definition[], location: SourceLocation) => {
|
|
350
|
+
if (pendingCount === 0) sections.push({ location, members: [] });
|
|
351
|
+
const names = (pending[depth] ??= new Set());
|
|
352
|
+
definitions.forEach((definition) => {
|
|
353
|
+
if (names.has(definition.name)) return;
|
|
354
|
+
names.add(definition.name);
|
|
355
|
+
pendingCount++;
|
|
356
|
+
});
|
|
357
|
+
definitions.forEach((definition) =>
|
|
358
|
+
sections.at(-1)?.members.push({ definition, scope: getScope(), isRechecked: true }),
|
|
359
|
+
);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const walkUnits = (units: ModuleUnit[]) => {
|
|
363
|
+
const recursiveDefinitions = new Map<string, Definition[]>();
|
|
364
|
+
units.forEach((unit) => {
|
|
365
|
+
if (unit.kind !== "Definition" || !unit.definition.isRecursive) return;
|
|
366
|
+
const named = recursiveDefinitions.get(unit.definition.name);
|
|
367
|
+
if (named) named.push(unit.definition);
|
|
368
|
+
else recursiveDefinitions.set(unit.definition.name, [unit.definition]);
|
|
369
|
+
});
|
|
370
|
+
units.forEach((unit) => {
|
|
371
|
+
if (unit.kind === "Recursive") {
|
|
372
|
+
declareRecursive(
|
|
373
|
+
unit.declarations.flatMap(
|
|
374
|
+
(declaration) => recursiveDefinitions.get(declaration.name) ?? [],
|
|
375
|
+
),
|
|
376
|
+
unit.location,
|
|
377
|
+
);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
if (pendingCount === 0 && !hasRecursiveLet) return;
|
|
381
|
+
if (unit.kind === "Assumption") walkExpression(unit.assumption.expression);
|
|
382
|
+
else if (unit.kind === "Theorem" && unit.theorem.statement.kind !== "AssumeProve") {
|
|
383
|
+
walkExpression(unit.theorem.statement);
|
|
384
|
+
} else if (unit.kind === "Definition") walkDefinition(unit.definition);
|
|
385
|
+
});
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
if (Array.isArray(root)) walkUnits(root);
|
|
389
|
+
else walkExpression(root);
|
|
390
|
+
return sections;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const getLetUnits = (expression: LetExpression) =>
|
|
394
|
+
[
|
|
395
|
+
...(expression.recursive ?? []).map((declaration) => ({
|
|
396
|
+
location: declaration.location,
|
|
397
|
+
unit: {
|
|
398
|
+
kind: "Recursive",
|
|
399
|
+
declarations: [declaration],
|
|
400
|
+
location: declaration.location,
|
|
401
|
+
} satisfies ModuleUnit,
|
|
402
|
+
})),
|
|
403
|
+
...expression.definitions.map((definition) => ({
|
|
404
|
+
location: definition.location,
|
|
405
|
+
unit: { kind: "Definition", definition } satisfies ModuleUnit,
|
|
406
|
+
})),
|
|
407
|
+
]
|
|
408
|
+
.toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
409
|
+
.map((entry) => entry.unit);
|
|
199
410
|
|
|
200
411
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
201
412
|
|
|
413
|
+
const isMixed = (levels: number[]) =>
|
|
414
|
+
levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL);
|
|
415
|
+
|
|
416
|
+
const getWrittenRange = (expression: Expression) =>
|
|
417
|
+
parenthesizedRanges.get(expression) ?? expression.location.range;
|
|
418
|
+
|
|
202
419
|
const isOperatorApplication = (expression: Expression) =>
|
|
203
420
|
expression.kind !== "Let" && expression.kind !== "Label";
|
|
204
421
|
|
|
205
422
|
export const createLevelAnalyzer = (context: LevelContext) => {
|
|
206
423
|
const definitionLevels = new Map<Definition, OperatorLevels>();
|
|
424
|
+
const selectionLevels = new Map<Expression, LevelData>();
|
|
425
|
+
const errors: SanyError[] = [];
|
|
426
|
+
const messages: string[] = [];
|
|
207
427
|
|
|
208
428
|
const resolve = (name: string, scope: Scope): Resolved => {
|
|
209
429
|
const entry = scope.get(name);
|
|
210
430
|
if (entry?.kind === "Formal") {
|
|
211
|
-
return { kind: "Declared", name, level:
|
|
431
|
+
return { kind: "Declared", name, level: entry.level, arity: entry.arity };
|
|
212
432
|
}
|
|
213
433
|
if (entry?.kind === "Let") {
|
|
214
434
|
return { kind: "Defined", levels: analyzeDefinition(entry.definition, entry.scope) };
|
|
@@ -231,6 +451,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
231
451
|
|
|
232
452
|
const applyDeclared = (name: string, level: number, operands: Operand[]) => {
|
|
233
453
|
const data = createData(Math.max(level, ...operands.map((operand) => operand.data.level)));
|
|
454
|
+
data.isCorrect = operands.every((operand) => operand.data.isCorrect);
|
|
234
455
|
data.params.add(name);
|
|
235
456
|
operands.forEach((operand, index) => {
|
|
236
457
|
operand.data.params.forEach((param) => {
|
|
@@ -250,19 +471,18 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
250
471
|
bound: string[] = [],
|
|
251
472
|
) => {
|
|
252
473
|
const isBound = (name: string) => bound.includes(name);
|
|
253
|
-
operands.
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
474
|
+
const exceeding = operands.flatMap((operand, index) =>
|
|
475
|
+
operand.kind === "Value" && operand.data.level > getMaxLevel(operator, index)
|
|
476
|
+
? [{ operand, index }]
|
|
477
|
+
: [],
|
|
478
|
+
);
|
|
479
|
+
exceeding
|
|
480
|
+
.filter((entry) => operator.isCorrect && entry.operand.data.isCorrect)
|
|
481
|
+
.forEach((entry) =>
|
|
482
|
+
messages.push(
|
|
483
|
+
`Level error in applying operator ${resolveSynonym(operator.name)}:\nThe level of argument ${entry.index + 1} exceeds the maximum level allowed by the operator`,
|
|
484
|
+
),
|
|
264
485
|
);
|
|
265
|
-
}
|
|
266
486
|
const getOperand = (formal: string) => {
|
|
267
487
|
const index = operator.formals.findIndex((param) => param.name === formal);
|
|
268
488
|
return index === -1 ? undefined : operands[index];
|
|
@@ -278,6 +498,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
278
498
|
...ranges.map((range) => range.level),
|
|
279
499
|
),
|
|
280
500
|
);
|
|
501
|
+
data.isCorrect =
|
|
502
|
+
exceeding.length === 0 &&
|
|
503
|
+
operands.every((operand) => operand.data.isCorrect) &&
|
|
504
|
+
ranges.every((range) => range.isCorrect);
|
|
281
505
|
operator.params.forEach((param) => data.params.add(param));
|
|
282
506
|
operands.forEach((operand, index) => {
|
|
283
507
|
if (getWeight(operator, index) === 1) {
|
|
@@ -449,16 +673,23 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
449
673
|
operator: OperatorLevels,
|
|
450
674
|
expressions: Expression[],
|
|
451
675
|
scope: Scope,
|
|
452
|
-
|
|
453
|
-
bound
|
|
676
|
+
bounds: Bound[] = [],
|
|
677
|
+
bound = getBoundNames(bounds),
|
|
454
678
|
) => {
|
|
455
|
-
const
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
679
|
+
const operands = toOperands(expressions, bindFormals(scope, toFormals(bound)));
|
|
680
|
+
const ranges = bounds.map((range) => analyze(range.set, scope));
|
|
681
|
+
const data = applyDefined(operator, operands, ranges, bound);
|
|
682
|
+
ranges.forEach((range, index) => {
|
|
683
|
+
if (range.level <= ACTION_LEVEL) return;
|
|
684
|
+
data.isCorrect = false;
|
|
685
|
+
if (!range.isCorrect) return;
|
|
686
|
+
const pattern = bounds[index].pattern;
|
|
687
|
+
const symbol = pattern.ranges?.[0];
|
|
688
|
+
messages.push(
|
|
689
|
+
`Level error in applying operator ${operator.name}:\nThe level of the range for the bounded variable ${symbol ? formatRange(symbol) : getPatternNames(pattern)[0]} \nexceeds the maximum level allowed by the operator`,
|
|
690
|
+
);
|
|
691
|
+
});
|
|
692
|
+
return data;
|
|
462
693
|
};
|
|
463
694
|
|
|
464
695
|
const applyResolved = (resolved: Resolved, operands: Operand[]) =>
|
|
@@ -466,25 +697,21 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
466
697
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
467
698
|
: applyDefined(resolved.levels, operands);
|
|
468
699
|
|
|
469
|
-
const checkJunction = (
|
|
470
|
-
|
|
471
|
-
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
472
|
-
throw createLevelError(
|
|
473
|
-
"Formula has both temporal formula and action as arguments",
|
|
474
|
-
expression,
|
|
475
|
-
);
|
|
476
|
-
}
|
|
700
|
+
const checkJunction = (levels: number[], name: string) => {
|
|
701
|
+
if (isMixed(levels)) messages.push(`${name} has both temporal formula and action as arguments`);
|
|
477
702
|
};
|
|
478
703
|
|
|
479
704
|
const checkApply = (expression: Expression & { kind: "Apply" }, operands: Operand[]) => {
|
|
480
705
|
const isLeadsTo = expression.operator === "~>" || expression.operator === "-+->";
|
|
481
706
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
707
|
+
messages.push("Action used where only temporal formula or state predicate allowed");
|
|
708
|
+
}
|
|
709
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) {
|
|
710
|
+
checkJunction(
|
|
711
|
+
operands.map((operand) => operand.data.level),
|
|
712
|
+
resolveSynonym(expression.operator),
|
|
485
713
|
);
|
|
486
714
|
}
|
|
487
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
488
715
|
};
|
|
489
716
|
|
|
490
717
|
const checkTemporal = (
|
|
@@ -494,40 +721,56 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
494
721
|
const operand = expression.expression;
|
|
495
722
|
const form = expression.kind === "Always" ? "BoxAction" : "AngleAction";
|
|
496
723
|
if (level !== ACTION_LEVEL || !isOperatorApplication(operand) || operand.kind === form) return;
|
|
497
|
-
|
|
724
|
+
messages.push(
|
|
498
725
|
expression.kind === "Always"
|
|
499
726
|
? "[] followed by action not of form [A]_v"
|
|
500
727
|
: "<> followed by action not of form <<A>>_v",
|
|
501
|
-
expression,
|
|
502
728
|
);
|
|
503
729
|
};
|
|
504
730
|
|
|
505
|
-
const analyzeLet = (expression:
|
|
506
|
-
const inner =
|
|
507
|
-
expression.
|
|
508
|
-
|
|
731
|
+
const analyzeLet = (expression: LetExpression, scope: Scope) => {
|
|
732
|
+
const inner = createLetScope(expression, scope);
|
|
733
|
+
if (expression.recursive) {
|
|
734
|
+
getRecursiveSections(scope, expression).forEach((section) =>
|
|
735
|
+
checkRecursiveSection(section.members),
|
|
736
|
+
);
|
|
737
|
+
}
|
|
738
|
+
const definitions = expression.definitions.map((definition) =>
|
|
739
|
+
analyzeDefinition(definition, inner),
|
|
509
740
|
);
|
|
510
741
|
const body = analyze(expression.body, inner);
|
|
511
742
|
const data = {
|
|
512
743
|
level: body.level,
|
|
744
|
+
isCorrect: body.isCorrect && definitions.every((levels) => levels.isCorrect),
|
|
513
745
|
params: new Set(body.params),
|
|
514
746
|
constraints: new Map(body.constraints),
|
|
515
747
|
argConstraints: new Map(body.argConstraints),
|
|
516
748
|
argParams: new Set(body.argParams),
|
|
517
749
|
};
|
|
518
|
-
|
|
519
|
-
mergeConstraints(data, analyzeDefinition(definition, inner)),
|
|
520
|
-
);
|
|
750
|
+
definitions.forEach((levels) => mergeConstraints(data, levels));
|
|
521
751
|
return data;
|
|
522
752
|
};
|
|
523
753
|
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
754
|
+
const reportSince = (start: number, range: SourceRange | undefined, data: LevelData) => {
|
|
755
|
+
if (messages.length === start) return data;
|
|
756
|
+
if (range) {
|
|
757
|
+
messages
|
|
758
|
+
.splice(start)
|
|
759
|
+
.forEach((message) =>
|
|
760
|
+
errors.push({ location: formatRange(range), message: `${message}.` }),
|
|
761
|
+
);
|
|
530
762
|
}
|
|
763
|
+
return { ...data, isCorrect: false };
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
const reportAt = (range: SourceRange | undefined, getData: () => LevelData) => {
|
|
767
|
+
const start = messages.length;
|
|
768
|
+
return reportSince(start, range, getData());
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
const analyze = (expression: Expression, scope: Scope) => {
|
|
772
|
+
const start = messages.length;
|
|
773
|
+
return reportSince(start, expression.location.range, analyzeNode(expression, scope));
|
|
531
774
|
};
|
|
532
775
|
|
|
533
776
|
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
@@ -549,30 +792,65 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
549
792
|
return toOperand(expression, scope).data;
|
|
550
793
|
case "And":
|
|
551
794
|
case "Or": {
|
|
552
|
-
const
|
|
553
|
-
|
|
554
|
-
|
|
795
|
+
const isAnd = expression.kind === "And";
|
|
796
|
+
if (expression.isBulleted) {
|
|
797
|
+
const operands = toOperands(expression.operands, scope);
|
|
798
|
+
checkJunction(
|
|
799
|
+
operands.map((operand) => operand.data.level),
|
|
800
|
+
`${isAnd ? "Conjunction" : "Disjunction"} list`,
|
|
801
|
+
);
|
|
802
|
+
return applyDefined(isAnd ? AND_LEVELS : OR_LEVELS, operands);
|
|
803
|
+
}
|
|
804
|
+
// SANY nests a chain of infix /\ or \/ to the left, so it checks each prefix of the chain.
|
|
805
|
+
const name = isAnd ? "\\land" : "\\lor";
|
|
806
|
+
const start = getWrittenRange(expression.operands[0]);
|
|
807
|
+
const operands: Operand[] = [];
|
|
808
|
+
let prefixLevel = CONSTANT_LEVEL;
|
|
809
|
+
let isPrefixMixed = false;
|
|
810
|
+
expression.operands.forEach((operand, index) => {
|
|
811
|
+
const analyzed = toOperand(operand, scope);
|
|
812
|
+
operands.push(analyzed);
|
|
813
|
+
const levels = [prefixLevel, analyzed.data.level];
|
|
814
|
+
prefixLevel = Math.max(prefixLevel, analyzed.data.level);
|
|
815
|
+
if (index === 0) return;
|
|
816
|
+
const end = getWrittenRange(operand);
|
|
817
|
+
if (index === expression.operands.length - 1 || !start || !end) {
|
|
818
|
+
checkJunction(levels, name);
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
if (!isMixed(levels)) return;
|
|
822
|
+
errors.push({
|
|
823
|
+
location: formatRange({ ...start, end: end.end }),
|
|
824
|
+
message: `${name} has both temporal formula and action as arguments.`,
|
|
825
|
+
});
|
|
826
|
+
isPrefixMixed = true;
|
|
827
|
+
});
|
|
828
|
+
const data = applyDefined(isAnd ? AND_LEVELS : OR_LEVELS, operands);
|
|
829
|
+
return isPrefixMixed ? { ...data, isCorrect: false } : data;
|
|
555
830
|
}
|
|
556
831
|
case "Prime":
|
|
557
|
-
case "Unchanged":
|
|
558
832
|
return applyBuiltin(PRIME_LEVELS, [expression.expression], scope);
|
|
833
|
+
case "Unchanged":
|
|
834
|
+
return applyBuiltin(UNCHANGED_LEVELS, [expression.expression], scope);
|
|
559
835
|
case "Enabled":
|
|
560
836
|
return applyBuiltin(ENABLED_LEVELS, [expression.expression], scope);
|
|
561
837
|
case "Always":
|
|
562
838
|
case "Eventually": {
|
|
563
839
|
const operand = toOperand(expression.expression, scope);
|
|
564
840
|
checkTemporal(expression, operand.data.level);
|
|
565
|
-
return applyDefined(
|
|
841
|
+
return applyDefined(expression.kind === "Always" ? ALWAYS_LEVELS : EVENTUALLY_LEVELS, [
|
|
842
|
+
operand,
|
|
843
|
+
]);
|
|
566
844
|
}
|
|
567
845
|
case "If":
|
|
568
846
|
return applyBuiltin(
|
|
569
|
-
|
|
847
|
+
IF_LEVELS,
|
|
570
848
|
[expression.condition, expression.consequent, expression.alternative],
|
|
571
849
|
scope,
|
|
572
850
|
);
|
|
573
851
|
case "Case":
|
|
574
852
|
return applyBuiltin(
|
|
575
|
-
|
|
853
|
+
CASE_LEVELS,
|
|
576
854
|
[
|
|
577
855
|
...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
|
|
578
856
|
...(expression.other ? [expression.other] : []),
|
|
@@ -584,27 +862,33 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
584
862
|
case "Forall":
|
|
585
863
|
case "Exists": {
|
|
586
864
|
const data = applyBuiltin(
|
|
587
|
-
|
|
865
|
+
expression.kind === "Forall" ? FORALL_LEVELS : EXISTS_LEVELS,
|
|
588
866
|
[expression.body],
|
|
589
867
|
scope,
|
|
590
|
-
expression.bounds
|
|
591
|
-
getBoundNames(expression.bounds),
|
|
868
|
+
expression.bounds,
|
|
592
869
|
);
|
|
593
870
|
const actionBound =
|
|
594
871
|
data.level === TEMPORAL_LEVEL
|
|
595
872
|
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
596
873
|
: undefined;
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
874
|
+
const range = actionBound?.set.location.range;
|
|
875
|
+
if (range) {
|
|
876
|
+
errors.push({
|
|
877
|
+
location: formatRange(range),
|
|
878
|
+
message: "Action-level bound of quantified temporal formula.",
|
|
879
|
+
});
|
|
602
880
|
}
|
|
603
|
-
return data;
|
|
881
|
+
return actionBound ? { ...data, isCorrect: false } : data;
|
|
604
882
|
}
|
|
605
883
|
case "UnboundedQuantifier":
|
|
606
884
|
return applyBuiltin(
|
|
607
|
-
expression.isTemporal
|
|
885
|
+
expression.isTemporal
|
|
886
|
+
? expression.quantifier === "Forall"
|
|
887
|
+
? TEMPORAL_FORALL_LEVELS
|
|
888
|
+
: TEMPORAL_EXISTS_LEVELS
|
|
889
|
+
: expression.quantifier === "Forall"
|
|
890
|
+
? UNBOUNDED_FORALL_LEVELS
|
|
891
|
+
: UNBOUNDED_EXISTS_LEVELS,
|
|
608
892
|
[expression.body],
|
|
609
893
|
scope,
|
|
610
894
|
[],
|
|
@@ -612,79 +896,74 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
612
896
|
);
|
|
613
897
|
case "Choose":
|
|
614
898
|
return applyBuiltin(
|
|
615
|
-
|
|
899
|
+
expression.set ? BOUNDED_CHOOSE_LEVELS : UNBOUNDED_CHOOSE_LEVELS,
|
|
616
900
|
[expression.body],
|
|
617
901
|
scope,
|
|
618
|
-
expression.set ? [expression.set] : [],
|
|
902
|
+
expression.set ? [{ pattern: expression.pattern, set: expression.set }] : [],
|
|
619
903
|
getPatternNames(expression.pattern),
|
|
620
904
|
);
|
|
621
905
|
case "SetEnumeration":
|
|
906
|
+
return applyBuiltin(SET_ENUMERATE_LEVELS, expression.elements, scope);
|
|
622
907
|
case "Tuple":
|
|
908
|
+
return applyBuiltin(TUPLE_LEVELS, expression.elements, scope);
|
|
623
909
|
case "FunctionSet":
|
|
910
|
+
return applyBuiltin(SET_OF_FCNS_LEVELS, [expression.domain, expression.range], scope);
|
|
624
911
|
case "Product":
|
|
625
|
-
return applyBuiltin(
|
|
626
|
-
SET_LEVELS,
|
|
627
|
-
expression.kind === "FunctionSet"
|
|
628
|
-
? [expression.domain, expression.range]
|
|
629
|
-
: expression.kind === "Product"
|
|
630
|
-
? expression.factors
|
|
631
|
-
: expression.elements,
|
|
632
|
-
scope,
|
|
633
|
-
);
|
|
912
|
+
return applyBuiltin(CARTESIAN_PROD_LEVELS, expression.factors, scope);
|
|
634
913
|
case "SetFilter":
|
|
635
|
-
return applyBuiltin(
|
|
636
|
-
SET_LEVELS,
|
|
637
|
-
[expression.predicate],
|
|
638
|
-
scope,
|
|
639
|
-
[expression.bound.set],
|
|
640
|
-
getPatternNames(expression.bound.pattern),
|
|
641
|
-
);
|
|
914
|
+
return applyBuiltin(SUBSET_OF_LEVELS, [expression.predicate], scope, [expression.bound]);
|
|
642
915
|
case "SetMap":
|
|
643
|
-
return applyBuiltin(
|
|
644
|
-
SET_LEVELS,
|
|
645
|
-
[expression.expression],
|
|
646
|
-
scope,
|
|
647
|
-
expression.bounds.map((bound) => bound.set),
|
|
648
|
-
getBoundNames(expression.bounds),
|
|
649
|
-
);
|
|
916
|
+
return applyBuiltin(SET_OF_ALL_LEVELS, [expression.expression], scope, expression.bounds);
|
|
650
917
|
case "FunctionConstruction":
|
|
651
|
-
return applyBuiltin(
|
|
652
|
-
FUNCTION_LEVELS,
|
|
653
|
-
[expression.body],
|
|
654
|
-
scope,
|
|
655
|
-
expression.bounds.map((bound) => bound.set),
|
|
656
|
-
getBoundNames(expression.bounds),
|
|
657
|
-
);
|
|
918
|
+
return applyBuiltin(FCN_CONSTRUCTOR_LEVELS, [expression.body], scope, expression.bounds);
|
|
658
919
|
case "Record":
|
|
659
920
|
return applyBuiltin(
|
|
660
|
-
|
|
921
|
+
RCD_CONSTRUCTOR_LEVELS,
|
|
661
922
|
expression.fields.map((field) => field.value),
|
|
662
923
|
scope,
|
|
663
924
|
);
|
|
664
925
|
case "RecordSet":
|
|
665
926
|
return applyBuiltin(
|
|
666
|
-
|
|
927
|
+
SET_OF_RCDS_LEVELS,
|
|
667
928
|
expression.fields.map((field) => field.value),
|
|
668
929
|
scope,
|
|
669
930
|
);
|
|
670
931
|
case "Except":
|
|
671
|
-
return
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
932
|
+
return applyDefined(EXCEPT_LEVELS, [
|
|
933
|
+
toOperand(expression.function, scope),
|
|
934
|
+
...expression.updates.map((update): Operand => ({
|
|
935
|
+
kind: "Value",
|
|
936
|
+
data: applyDefined(PAIR_LEVELS, [
|
|
937
|
+
{
|
|
938
|
+
kind: "Value",
|
|
939
|
+
data: reportAt(update.range, () =>
|
|
940
|
+
applyDefined(
|
|
941
|
+
SEQ_LEVELS,
|
|
942
|
+
update.path.map((step): Operand =>
|
|
943
|
+
step.kind === "Field"
|
|
944
|
+
? { kind: "Value", data: createData() }
|
|
945
|
+
: step.args.length === 1
|
|
946
|
+
? toOperand(step.args[0], scope)
|
|
947
|
+
: {
|
|
948
|
+
kind: "Value",
|
|
949
|
+
data: reportAt(step.tupleRange, () =>
|
|
950
|
+
applyBuiltin(TUPLE_LEVELS, step.args, scope),
|
|
951
|
+
),
|
|
952
|
+
},
|
|
953
|
+
),
|
|
954
|
+
),
|
|
955
|
+
),
|
|
956
|
+
},
|
|
957
|
+
toOperand(update.value, scope),
|
|
678
958
|
]),
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
);
|
|
959
|
+
})),
|
|
960
|
+
]);
|
|
682
961
|
case "FunctionApplication":
|
|
683
962
|
return applyDefined(APPLY_LEVELS, [
|
|
684
963
|
toOperand(expression.function, scope),
|
|
685
964
|
expression.args.length === 1
|
|
686
965
|
? toOperand(expression.args[0], scope)
|
|
687
|
-
: { kind: "Value", data: applyBuiltin(
|
|
966
|
+
: { kind: "Value", data: applyBuiltin(TUPLE_LEVELS, expression.args, scope) },
|
|
688
967
|
]);
|
|
689
968
|
case "Bracket":
|
|
690
969
|
return createData();
|
|
@@ -695,12 +974,28 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
695
974
|
]);
|
|
696
975
|
case "BoxAction":
|
|
697
976
|
case "AngleAction":
|
|
698
|
-
return applyBuiltin(
|
|
977
|
+
return applyBuiltin(
|
|
978
|
+
expression.kind === "BoxAction" ? SQUARE_ACT_LEVELS : ANGLE_ACT_LEVELS,
|
|
979
|
+
[expression.action, expression.subscript],
|
|
980
|
+
scope,
|
|
981
|
+
);
|
|
699
982
|
case "WeakFairness":
|
|
700
983
|
case "StrongFairness":
|
|
701
|
-
return applyBuiltin(
|
|
984
|
+
return applyBuiltin(
|
|
985
|
+
expression.kind === "WeakFairness" ? WF_LEVELS : SF_LEVELS,
|
|
986
|
+
[expression.subscript, expression.action],
|
|
987
|
+
scope,
|
|
988
|
+
);
|
|
702
989
|
case "Label":
|
|
703
990
|
return analyze(expression.body, scope);
|
|
991
|
+
case "Selection": {
|
|
992
|
+
const selected = expression.expression;
|
|
993
|
+
const known = selectionLevels.get(selected);
|
|
994
|
+
if (known) return known;
|
|
995
|
+
const data = analyze(selected, scope);
|
|
996
|
+
selectionLevels.set(selected, data);
|
|
997
|
+
return data;
|
|
998
|
+
}
|
|
704
999
|
case "Subexpression":
|
|
705
1000
|
return createData();
|
|
706
1001
|
}
|
|
@@ -734,6 +1029,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
734
1029
|
return names;
|
|
735
1030
|
case "Lambda":
|
|
736
1031
|
case "Let":
|
|
1032
|
+
case "Selection":
|
|
737
1033
|
return names;
|
|
738
1034
|
case "Prime":
|
|
739
1035
|
case "Unchanged":
|
|
@@ -770,9 +1066,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
770
1066
|
const inner = bindFormals(scope, formals);
|
|
771
1067
|
const data = analyze(body, inner);
|
|
772
1068
|
const isFormal = (param: string) => formals.some((formal) => formal.name === param);
|
|
773
|
-
const nonLeibnizNames = getNonLeibnizNames(
|
|
1069
|
+
const nonLeibnizNames = getNonLeibnizNames(
|
|
1070
|
+
body.kind === "Selection" ? body.expression : body,
|
|
1071
|
+
inner,
|
|
1072
|
+
);
|
|
774
1073
|
const levels: OperatorLevels = {
|
|
775
1074
|
...createData(data.level),
|
|
1075
|
+
isCorrect: data.isCorrect,
|
|
776
1076
|
name,
|
|
777
1077
|
formals,
|
|
778
1078
|
nonLeibniz: new Set(
|
|
@@ -816,35 +1116,87 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
816
1116
|
return levels;
|
|
817
1117
|
};
|
|
818
1118
|
|
|
1119
|
+
const staleDefinitions = new Set<Definition>();
|
|
1120
|
+
|
|
819
1121
|
const analyzeDefinition = (definition: Definition, scope: Scope): OperatorLevels => {
|
|
820
1122
|
const cached = definitionLevels.get(definition);
|
|
821
|
-
if (cached) return cached;
|
|
822
|
-
const
|
|
823
|
-
definitionLevels.set(definition,
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
1123
|
+
if (cached && !staleDefinitions.delete(definition)) return cached;
|
|
1124
|
+
const prior = cached ?? createPlaceholder(definition, TEMPORAL_LEVEL, 0);
|
|
1125
|
+
definitionLevels.set(definition, prior);
|
|
1126
|
+
const levels = analyzeOperator(
|
|
1127
|
+
definition.name,
|
|
1128
|
+
getFormals(definition),
|
|
1129
|
+
getDefinitionBody(definition),
|
|
1130
|
+
scope,
|
|
1131
|
+
);
|
|
1132
|
+
const merged = {
|
|
1133
|
+
...levels,
|
|
1134
|
+
level: Math.max(prior.level, levels.level),
|
|
1135
|
+
params: new Set([...prior.params, ...levels.params]),
|
|
1136
|
+
argParams: new Set([...prior.argParams, ...levels.argParams]),
|
|
1137
|
+
maxLevels: levels.maxLevels.map((level, index) => Math.min(level, prior.maxLevels[index])),
|
|
1138
|
+
weights: levels.weights.map((weight, index) => Math.max(weight, prior.weights[index])),
|
|
1139
|
+
nonLeibniz: new Set([...prior.nonLeibniz, ...levels.nonLeibniz]),
|
|
1140
|
+
};
|
|
1141
|
+
definitionLevels.set(definition, merged);
|
|
1142
|
+
return merged;
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
const checkPrimedArguments = (members: SectionMember[]) =>
|
|
1146
|
+
members.forEach((member) => {
|
|
1147
|
+
const range = member.definition.location.range;
|
|
1148
|
+
if (!member.definition.isRecursive || !range) return;
|
|
1149
|
+
analyzeDefinition(member.definition, member.scope).maxLevels.forEach((level, index) => {
|
|
1150
|
+
if (level >= ACTION_LEVEL) return;
|
|
1151
|
+
errors.push({
|
|
1152
|
+
location: formatRange(range),
|
|
1153
|
+
message: `Argument ${index + 1} of recursive operator ${resolveSynonym(member.definition.name)} is primed`,
|
|
1154
|
+
});
|
|
1155
|
+
});
|
|
832
1156
|
});
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
1157
|
+
|
|
1158
|
+
const checkRecursiveSection = (members: SectionMember[]) => {
|
|
1159
|
+
if (members.length === 0 || definitionLevels.has(members[0].definition)) return;
|
|
1160
|
+
const recursive = members.filter((member) => member.definition.isRecursive);
|
|
1161
|
+
recursive.forEach((member) =>
|
|
1162
|
+
definitionLevels.set(
|
|
1163
|
+
member.definition,
|
|
1164
|
+
createPlaceholder(member.definition, ACTION_LEVEL, 1),
|
|
1165
|
+
),
|
|
1166
|
+
);
|
|
1167
|
+
const recheck = (member: SectionMember) => {
|
|
1168
|
+
if (member.definition.isRecursive) staleDefinitions.add(member.definition);
|
|
1169
|
+
return analyzeDefinition(member.definition, member.scope);
|
|
1170
|
+
};
|
|
1171
|
+
members.forEach((member) => {
|
|
1172
|
+
recheck(member);
|
|
1173
|
+
checkPrimedArguments([member]);
|
|
1174
|
+
});
|
|
1175
|
+
const firstPass = recursive.map((member) => analyzeDefinition(member.definition, member.scope));
|
|
1176
|
+
const level = Math.max(CONSTANT_LEVEL, ...firstPass.map((levels) => levels.level));
|
|
1177
|
+
const params = firstPass.flatMap((levels) => [...levels.params]);
|
|
1178
|
+
members.forEach((member) => {
|
|
1179
|
+
const levels = analyzeDefinition(member.definition, member.scope);
|
|
1180
|
+
definitionLevels.set(member.definition, {
|
|
1181
|
+
...levels,
|
|
1182
|
+
level: Math.max(levels.level, level),
|
|
1183
|
+
params: new Set([...levels.params, ...params]),
|
|
1184
|
+
});
|
|
1185
|
+
if (member.isRechecked && !member.definition.isRecursive) {
|
|
1186
|
+
staleDefinitions.add(member.definition);
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
members.forEach(recheck);
|
|
845
1190
|
};
|
|
846
1191
|
|
|
847
|
-
return {
|
|
1192
|
+
return {
|
|
1193
|
+
analyze,
|
|
1194
|
+
analyzeDefinition,
|
|
1195
|
+
checkPrimedArguments,
|
|
1196
|
+
checkRecursiveSection,
|
|
1197
|
+
toOperand,
|
|
1198
|
+
errors,
|
|
1199
|
+
};
|
|
848
1200
|
};
|
|
849
1201
|
|
|
850
1202
|
export const analyzeAssumeProve = (
|
|
@@ -856,7 +1208,18 @@ export const analyzeAssumeProve = (
|
|
|
856
1208
|
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
857
1209
|
if (assumption.kind === "NewSymbol") {
|
|
858
1210
|
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
859
|
-
|
|
1211
|
+
const range = assumption.location.range;
|
|
1212
|
+
if (set?.level === TEMPORAL_LEVEL && range) {
|
|
1213
|
+
analyzer.errors.push({
|
|
1214
|
+
location: formatRange(range),
|
|
1215
|
+
message: "Level error:\nTemporal formula used as set.",
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
inner.set(assumption.name, {
|
|
1219
|
+
kind: "Formal",
|
|
1220
|
+
arity: assumption.arity,
|
|
1221
|
+
level: NEW_SYMBOL_LEVELS[assumption.level],
|
|
1222
|
+
});
|
|
860
1223
|
return set ? [set] : [];
|
|
861
1224
|
}
|
|
862
1225
|
return [
|
|
@@ -881,27 +1244,35 @@ export const checkModuleLevels = (
|
|
|
881
1244
|
warnings: SanyError[],
|
|
882
1245
|
) => {
|
|
883
1246
|
const analyzer = createLevelAnalyzer(context);
|
|
884
|
-
const getLevel = (expressions: Expression[]) =>
|
|
885
|
-
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression,
|
|
886
|
-
const getStepLevel = (step: ProofStep) => {
|
|
1247
|
+
const getLevel = (expressions: Expression[], scope: Scope) =>
|
|
1248
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, scope).level));
|
|
1249
|
+
const getStepLevel = (step: ProofStep, scope: Scope) => {
|
|
887
1250
|
const body = step.body;
|
|
888
|
-
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
889
|
-
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
890
|
-
if (body.kind === "Take")
|
|
1251
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression], scope);
|
|
1252
|
+
if (body.kind === "Witness") return getLevel(body.expressions, scope);
|
|
1253
|
+
if (body.kind === "Take")
|
|
1254
|
+
return getLevel(
|
|
1255
|
+
body.bounds.map((bound) => bound.set),
|
|
1256
|
+
scope,
|
|
1257
|
+
);
|
|
891
1258
|
if (body.kind === "Pick") {
|
|
892
|
-
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
1259
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)], scope);
|
|
893
1260
|
}
|
|
894
1261
|
if (body.kind !== "Assert") return 0;
|
|
895
1262
|
return body.statement.kind === "AssumeProve"
|
|
896
|
-
? analyzeAssumeProve(body.statement,
|
|
897
|
-
: getLevel([body.statement]);
|
|
1263
|
+
? analyzeAssumeProve(body.statement, scope, analyzer).level
|
|
1264
|
+
: getLevel([body.statement], scope);
|
|
898
1265
|
};
|
|
1266
|
+
const checkFacts = (facts: Fact[], scope: Scope) =>
|
|
1267
|
+
facts.forEach((fact) => {
|
|
1268
|
+
if (fact.kind !== "ModuleFact") analyzer.analyze(fact, scope);
|
|
1269
|
+
});
|
|
899
1270
|
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
900
1271
|
proof?.kind !== "Steps"
|
|
901
1272
|
? []
|
|
902
1273
|
: proof.steps.flatMap((step) => {
|
|
903
1274
|
const location = formatRange(step.statement);
|
|
904
|
-
const isConstant = () => getStepLevel(step) === 0;
|
|
1275
|
+
const isConstant = () => getStepLevel(step, new Map()) === 0;
|
|
905
1276
|
if (
|
|
906
1277
|
step.body.kind === "Have" ||
|
|
907
1278
|
step.body.kind === "Take" ||
|
|
@@ -921,62 +1292,129 @@ export const checkModuleLevels = (
|
|
|
921
1292
|
}
|
|
922
1293
|
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
923
1294
|
});
|
|
924
|
-
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
925
|
-
proof?.kind
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
message: "Non-constant bound of temporal PICK.",
|
|
940
|
-
},
|
|
941
|
-
]
|
|
942
|
-
: [],
|
|
943
|
-
)
|
|
944
|
-
: [];
|
|
945
|
-
return [
|
|
946
|
-
...checkProof(step.proof),
|
|
947
|
-
...pickErrors,
|
|
948
|
-
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
949
|
-
];
|
|
1295
|
+
const checkProof = (proof: Proof | undefined, scope: Scope): SanyError[] => {
|
|
1296
|
+
if (proof?.kind === "By") {
|
|
1297
|
+
checkFacts(proof.facts, scope);
|
|
1298
|
+
return analyzer.errors.splice(0);
|
|
1299
|
+
}
|
|
1300
|
+
if (proof?.kind !== "Steps") return [];
|
|
1301
|
+
const inner = new Map(scope);
|
|
1302
|
+
return proof.steps.flatMap((step) => {
|
|
1303
|
+
const body = step.body;
|
|
1304
|
+
if (body.kind === "Use" || body.kind === "Hide") checkFacts(body.facts, inner);
|
|
1305
|
+
if (body.kind === "Define") {
|
|
1306
|
+
body.definitions.forEach((definition) => {
|
|
1307
|
+
if (definition.kind === "Instance") return;
|
|
1308
|
+
inner.set(definition.name, { kind: "Let", definition, scope: inner });
|
|
1309
|
+
analyzer.analyzeDefinition(definition, inner);
|
|
950
1310
|
});
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
.
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
1311
|
+
}
|
|
1312
|
+
const isTemporal = getStepLevel(step, inner) === TEMPORAL_LEVEL;
|
|
1313
|
+
const pickErrors =
|
|
1314
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal
|
|
1315
|
+
? body.bounds
|
|
1316
|
+
.filter((bound) => getLevel([bound.set], inner) !== 0)
|
|
1317
|
+
.flatMap((bound) =>
|
|
1318
|
+
bound.set.location.range
|
|
1319
|
+
? [
|
|
1320
|
+
{
|
|
1321
|
+
location: formatRange(bound.set.location.range),
|
|
1322
|
+
message: "Non-constant bound of temporal PICK.",
|
|
1323
|
+
},
|
|
1324
|
+
]
|
|
1325
|
+
: [],
|
|
1326
|
+
)
|
|
1327
|
+
: [];
|
|
1328
|
+
return [
|
|
1329
|
+
...analyzer.errors.splice(0),
|
|
1330
|
+
...checkProof(step.proof, inner),
|
|
1331
|
+
...pickErrors,
|
|
1332
|
+
...(step.proof?.kind === "Steps" && isTemporal ? checkTemporalGoal(step.proof) : []),
|
|
1333
|
+
];
|
|
1334
|
+
});
|
|
1335
|
+
};
|
|
1336
|
+
const statementLevels = new Map<Expression | AssumeProve, number>();
|
|
1337
|
+
const getStatementLevel = (statement: Expression | AssumeProve) => {
|
|
1338
|
+
const cached = statementLevels.get(statement);
|
|
1339
|
+
if (cached !== undefined) return cached;
|
|
1340
|
+
const level =
|
|
1341
|
+
statement.kind === "AssumeProve"
|
|
1342
|
+
? analyzeAssumeProve(statement, new Map(), analyzer).level
|
|
1343
|
+
: analyzer.analyze(statement, new Map()).level;
|
|
1344
|
+
statementLevels.set(statement, level);
|
|
1345
|
+
return level;
|
|
1346
|
+
};
|
|
1347
|
+
const statements = [
|
|
1348
|
+
...module.theorems,
|
|
1349
|
+
...module.assumptions.map((assumption) => ({
|
|
1350
|
+
...assumption,
|
|
1351
|
+
statement: assumption.expression,
|
|
1352
|
+
})),
|
|
1353
|
+
];
|
|
1354
|
+
const statementBodies = new Set(statements.map((statement) => statement.statement));
|
|
1355
|
+
const sections = getRecursiveSections(new Map(), module.units, module.hasRecursiveLet);
|
|
1356
|
+
const createCheck = (location: SourceLocation, run: () => void) => ({
|
|
1357
|
+
location,
|
|
1358
|
+
check: (reported: SanyError[]) => {
|
|
1359
|
+
run();
|
|
1360
|
+
reported.push(...analyzer.errors.splice(0));
|
|
1361
|
+
},
|
|
1362
|
+
});
|
|
1363
|
+
return {
|
|
1364
|
+
sections: sections.map((section) =>
|
|
1365
|
+
createCheck(section.location, () => analyzer.checkRecursiveSection(section.members)),
|
|
1366
|
+
),
|
|
1367
|
+
check: (isInner: boolean) =>
|
|
1368
|
+
[
|
|
1369
|
+
(isInner ? sections : []).map((section) =>
|
|
1370
|
+
createCheck(section.location, () => analyzer.checkPrimedArguments(section.members)),
|
|
1371
|
+
),
|
|
1372
|
+
module.definitions
|
|
1373
|
+
.filter(
|
|
1374
|
+
(definition) => !definition.name.includes("!") && !statementBodies.has(definition.body),
|
|
1375
|
+
)
|
|
1376
|
+
.map((definition) =>
|
|
1377
|
+
createCheck(definition.location, () =>
|
|
1378
|
+
analyzer.analyzeDefinition(definition, new Map()),
|
|
1379
|
+
),
|
|
1380
|
+
),
|
|
1381
|
+
statements
|
|
1382
|
+
.filter((statement) => statement.name !== undefined)
|
|
1383
|
+
.map((statement) =>
|
|
1384
|
+
createCheck(statement.location, () => getStatementLevel(statement.statement)),
|
|
1385
|
+
),
|
|
1386
|
+
[
|
|
1387
|
+
...module.assumptions.map((assumption) => ({
|
|
1388
|
+
location: assumption.location,
|
|
1389
|
+
check: (reported: SanyError[]) => {
|
|
1390
|
+
const level = getStatementLevel(assumption.expression);
|
|
1391
|
+
reported.push(...analyzer.errors.splice(0));
|
|
1392
|
+
const range = assumption.location.range;
|
|
1393
|
+
if (level !== CONSTANT_LEVEL && range) {
|
|
1394
|
+
reported.push({
|
|
1395
|
+
location: formatRange(range),
|
|
1396
|
+
message: `Level error: assumptions must be level 0 (Constant), \nbut this one has level ${level}.`,
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
},
|
|
1400
|
+
})),
|
|
1401
|
+
...module.theorems.map((theorem) => ({
|
|
1402
|
+
location: theorem.location,
|
|
1403
|
+
check: (reported: SanyError[]) => {
|
|
1404
|
+
const level = getStatementLevel(theorem.statement);
|
|
1405
|
+
reported.push(
|
|
1406
|
+
...analyzer.errors.splice(0),
|
|
1407
|
+
...checkProof(theorem.proof, new Map()),
|
|
1408
|
+
...(level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
1409
|
+
...analyzer.errors.splice(0),
|
|
1410
|
+
);
|
|
1411
|
+
},
|
|
1412
|
+
})),
|
|
1413
|
+
...module.uses.map((use) =>
|
|
1414
|
+
createCheck(use.location, () => checkFacts(use.facts, new Map())),
|
|
1415
|
+
),
|
|
1416
|
+
...instances,
|
|
1417
|
+
],
|
|
1418
|
+
].flatMap((units) => collectSemanticErrors(units, warnings)),
|
|
1419
|
+
};
|
|
982
1420
|
};
|