tla-ts 0.0.10-dev.d618a2d79 → 0.0.10-dev.e5c8dc315
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 +36 -36
- package/dist/deciders.js +53 -53
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +18 -4
- package/dist/index.d.ts +18 -4
- 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 +56 -56
- package/dist/prove.js +78 -78
- package/dist/run.cjs +40 -44
- package/dist/run.js +42 -46
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +97 -92
- package/dist/transfer.js +97 -92
- 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 +50 -29
- package/src/builtins.ts +4 -3
- package/src/check-failure.ts +13 -11
- package/src/check-trace.ts +146 -93
- package/src/check.ts +518 -281
- package/src/cli/runtime-invariants.ts +20 -56
- package/src/cli/tlc-arguments.ts +2 -2
- package/src/cli/tlc-output.ts +18 -4
- package/src/cli/tlc.ts +18 -12
- 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 +101 -36
- package/src/environment.ts +155 -66
- package/src/errors.ts +7 -0
- package/src/evaluate.ts +424 -135
- package/src/exit-codes.ts +28 -14
- package/src/instances.ts +152 -25
- package/src/java-hash.ts +14 -0
- package/src/java-overrides.ts +2 -2
- package/src/levels.ts +675 -236
- package/src/lexer.ts +12 -2
- package/src/liveness-checker.ts +179 -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/parallel-worker.ts +8 -2
- package/src/parallel.ts +5 -0
- package/src/parser.ts +180 -149
- 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/ackermannize-bv-tactic.ts +3 -2
- 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 +171 -58
- package/src/prover/smt/solver/ast-lt.ts +1 -1
- package/src/prover/smt/solver/ast.ts +66 -14
- package/src/prover/smt/solver/auf-solver.ts +23 -12
- package/src/prover/smt/solver/bit-blast-tactic.ts +28 -2
- 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/bv1-blast.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 +84 -15
- package/src/prover/smt/solver/ctx-simplify.ts +9 -4
- 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 +9 -8
- package/src/prover/smt/solver/dt2bv-tactic.ts +66 -28
- 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 +31 -28
- package/src/prover/smt/solver/eq2bv-tactic.ts +15 -6
- 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 +4 -11
- 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/expr2polynomial.ts +204 -79
- package/src/prover/smt/solver/factor.ts +36 -14
- package/src/prover/smt/solver/fd-solver.ts +608 -139
- package/src/prover/smt/solver/fix-dl-var.ts +2 -3
- package/src/prover/smt/solver/fm-tactic.ts +9 -7
- package/src/prover/smt/solver/fpa-rewriter.ts +1 -2
- package/src/prover/smt/solver/fpa2bv-converter.ts +3 -1
- package/src/prover/smt/solver/fpa2bv-rewriter.ts +1 -0
- package/src/prover/smt/solver/fpa2bv-tactic.ts +1 -1
- package/src/prover/smt/solver/goal.ts +6 -3
- package/src/prover/smt/solver/goal2nlsat.ts +57 -30
- package/src/prover/smt/solver/hashtable.ts +4 -0
- package/src/prover/smt/solver/internalizer.ts +1 -1
- package/src/prover/smt/solver/lia2pb.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-core.ts +1 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +158 -80
- package/src/prover/smt/solver/lp/nra-solver.ts +45 -39
- package/src/prover/smt/solver/lp/pdd-interval.ts +6 -2
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +60 -20
- package/src/prover/smt/solver/lp/pdd-solver.ts +72 -39
- package/src/prover/smt/solver/lp/pdd.ts +725 -290
- 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 +22 -44
- 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 +2 -4
- package/src/prover/smt/solver/muz/spacer-pdr.ts +1 -0
- package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
- package/src/prover/smt/solver/nlqsat.ts +2 -1
- package/src/prover/smt/solver/nlsat/algebraic.ts +196 -92
- package/src/prover/smt/solver/nlsat/atoms.ts +52 -33
- package/src/prover/smt/solver/nlsat/explain.ts +375 -217
- package/src/prover/smt/solver/nlsat/levelwise.ts +460 -240
- package/src/prover/smt/solver/nlsat/polynomial.ts +2366 -681
- package/src/prover/smt/solver/nlsat/solver.ts +74 -64
- package/src/prover/smt/solver/nlsat/transcendentals.ts +41 -17
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +8 -4
- package/src/prover/smt/solver/nlsat/upolynomial.ts +41 -18
- package/src/prover/smt/solver/nlsat-tactic.ts +1 -1
- package/src/prover/smt/solver/nnf.ts +6 -2
- package/src/prover/smt/solver/normalize-bounds.ts +2 -2
- package/src/prover/smt/solver/occf.ts +1 -1
- package/src/prover/smt/solver/opt-context.ts +21 -7
- package/src/prover/smt/solver/opt-nlsat.ts +29 -18
- 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 +10 -6
- 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/pb2bv-tactic.ts +9 -3
- 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/purify-arith.ts +51 -49
- 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/quantifier-manager.ts +14 -9
- package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
- package/src/prover/smt/solver/reduce-args.ts +2 -2
- 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 +75 -12
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +149 -47
- 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 +54 -25
- package/src/prover/smt/solver/sat/sat2goal.ts +75 -50
- 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 +2 -2
- 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 +48 -7
- package/src/prover/smt/solver/smt-setup.ts +106 -13
- package/src/prover/smt/solver/smtlib-parser.ts +589 -224
- package/src/prover/smt/solver/smtlib-script.ts +718 -213
- package/src/prover/smt/solver/solve-eqs.ts +36 -37
- package/src/prover/smt/solver/solver.ts +18 -4
- package/src/prover/smt/solver/tactic.ts +389 -199
- 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 +12 -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 +15 -5
- 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 +1311 -268
- package/src/runtime.ts +21 -2
- package/src/sany/builtins.ts +10 -10
- package/src/sany/generation.ts +5 -15
- package/src/sany/generator-def-entries.ts +123 -36
- package/src/sany/generator-definitions.ts +5 -3
- package/src/sany/generator-expressions.ts +60 -37
- package/src/sany/generator-proofs.ts +19 -12
- package/src/sany/generator.ts +12 -6
- 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 +12 -6
- package/src/sany/xml.ts +2 -3
- package/src/search.ts +321 -135
- 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 +666 -147
- package/src/successors.ts +50 -20
- package/src/symmetry.ts +2 -1
- package/src/tlc/jar-data.ts +10 -0
- package/src/tlc/messages.ts +3 -3
- package/src/tlc/order.ts +14 -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 +44 -15
- 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/resolve.ts
CHANGED
|
@@ -18,9 +18,14 @@ import {
|
|
|
18
18
|
type Selector,
|
|
19
19
|
type SourceLocation,
|
|
20
20
|
type SourceRange,
|
|
21
|
+
type StepBody,
|
|
21
22
|
type Theorem,
|
|
23
|
+
type UseOrHide,
|
|
22
24
|
} from "./ast";
|
|
23
25
|
import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
|
|
26
|
+
import { defEntryErrors, getDefEntryBinder, parenthesizedRanges } from "./parser";
|
|
27
|
+
import { formatSanyLocation } from "./sany/nodes";
|
|
28
|
+
import { resolveSynonym } from "./tlc/jar-data";
|
|
24
29
|
|
|
25
30
|
export type SymbolKind =
|
|
26
31
|
| "Builtin"
|
|
@@ -37,6 +42,7 @@ interface Binding {
|
|
|
37
42
|
arity: number;
|
|
38
43
|
params?: number[];
|
|
39
44
|
range?: SourceRange;
|
|
45
|
+
functionArity?: number;
|
|
40
46
|
}
|
|
41
47
|
|
|
42
48
|
export interface ModuleSymbol extends Binding {
|
|
@@ -49,6 +55,7 @@ interface Goal {
|
|
|
49
55
|
statement: Expression | AssumeProve;
|
|
50
56
|
isSuffices: boolean;
|
|
51
57
|
isInProof: boolean;
|
|
58
|
+
keyword: string | undefined;
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
interface LocalName extends Binding {
|
|
@@ -56,7 +63,7 @@ interface LocalName extends Binding {
|
|
|
56
63
|
parent: LocalName | undefined;
|
|
57
64
|
goal?: Goal;
|
|
58
65
|
isPending?: boolean;
|
|
59
|
-
|
|
66
|
+
nonFactStep?: "Use" | "Hide" | "Instance" | "Define";
|
|
60
67
|
}
|
|
61
68
|
|
|
62
69
|
interface ModuleBinding extends Binding {
|
|
@@ -69,6 +76,7 @@ interface Unit {
|
|
|
69
76
|
getInstance: (instance: Instance) => InstancedModule;
|
|
70
77
|
definitions: Map<string, Definition>;
|
|
71
78
|
getKey: (definition: Definition) => string;
|
|
79
|
+
isModuleKnown: (name: string, location: SourceLocation) => boolean;
|
|
72
80
|
location: SourceLocation;
|
|
73
81
|
self: Definition | undefined;
|
|
74
82
|
facts: Set<string>;
|
|
@@ -84,8 +92,16 @@ interface Unit {
|
|
|
84
92
|
isGenerated: boolean;
|
|
85
93
|
}
|
|
86
94
|
|
|
87
|
-
interface
|
|
95
|
+
export interface SelectionArguments {
|
|
96
|
+
operator: string;
|
|
97
|
+
range: SourceRange | undefined;
|
|
88
98
|
args: Expression[];
|
|
99
|
+
params: number[];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface SelectionError {
|
|
103
|
+
applications: SelectionArguments[];
|
|
104
|
+
nulls: NullSelection[];
|
|
89
105
|
error: SanyError;
|
|
90
106
|
}
|
|
91
107
|
|
|
@@ -99,8 +115,43 @@ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" }
|
|
|
99
115
|
|
|
100
116
|
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
101
117
|
|
|
102
|
-
export const
|
|
118
|
+
export const SELECTION_NAME = "$subexpression";
|
|
119
|
+
|
|
120
|
+
export interface NullSelection {
|
|
121
|
+
node: Expression;
|
|
122
|
+
error: SanyError;
|
|
123
|
+
isEmpty: boolean;
|
|
124
|
+
applications: SelectionArguments[];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface SelectionArgument {
|
|
128
|
+
operator: string;
|
|
129
|
+
index: number;
|
|
130
|
+
range: SourceRange | undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const selections = new WeakMap<Expression, Expression>();
|
|
103
134
|
export const selectionErrors = new WeakMap<Expression, SelectionError>();
|
|
135
|
+
export const operatorSelectionErrors = new WeakMap<Expression, SanyError>();
|
|
136
|
+
export const factReferences = new WeakMap<Fact[], Fact[]>();
|
|
137
|
+
export const selectionArguments = new WeakMap<Expression, SelectionArgument>();
|
|
138
|
+
export const nullSelections = new WeakMap<Expression, NullSelection[]>();
|
|
139
|
+
export const generatedArgumentCounts = new WeakMap<Expression, number>();
|
|
140
|
+
const unresolvedExpressions = new WeakSet<Expression>();
|
|
141
|
+
const emptyExpressions = new WeakSet<Expression>();
|
|
142
|
+
|
|
143
|
+
// SANY's failed selection is an operator application with no operands.
|
|
144
|
+
const isEmptyExpression = (expression: Expression): boolean =>
|
|
145
|
+
emptyExpressions.has(expression) || findNullError(nullSelections.get(expression)) !== undefined;
|
|
146
|
+
|
|
147
|
+
const findNullSelection = (nulls: NullSelection[] | undefined) =>
|
|
148
|
+
nulls?.find((selection) =>
|
|
149
|
+
selection.isEmpty
|
|
150
|
+
? emptyExpressions.has(selection.node)
|
|
151
|
+
: unresolvedExpressions.has(selection.node),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const findNullError = (nulls: NullSelection[] | undefined) => findNullSelection(nulls)?.error;
|
|
104
155
|
|
|
105
156
|
const renamedArities = new WeakMap<Expression, number>();
|
|
106
157
|
|
|
@@ -111,13 +162,20 @@ const isKey = (name: string) => {
|
|
|
111
162
|
return index > 0 && index < name.length - 1;
|
|
112
163
|
};
|
|
113
164
|
|
|
114
|
-
const CORE_SYMBOLS: Symbols = new Map(
|
|
115
|
-
["~>", "-+->", "\\cdot"].map((name)
|
|
116
|
-
|
|
165
|
+
const CORE_SYMBOLS: Symbols = new Map([
|
|
166
|
+
...["~>", "-+->", "\\cdot", "\\X"].map((name): [string, ModuleSymbol] => [
|
|
167
|
+
name,
|
|
168
|
+
{ key: name, arity: 2, kind: "Builtin" },
|
|
169
|
+
]),
|
|
170
|
+
["'", { key: "'", arity: 1, kind: "Builtin" }],
|
|
171
|
+
]);
|
|
117
172
|
|
|
118
173
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
119
174
|
left.line < right.line || (left.line === right.line && left.column < right.column);
|
|
120
175
|
|
|
176
|
+
export const compareLocations = (left: SourceLocation, right: SourceLocation) =>
|
|
177
|
+
isBefore(left, right) ? -1 : isBefore(right, left) ? 1 : 0;
|
|
178
|
+
|
|
121
179
|
export const getArity = (definition: Definition) =>
|
|
122
180
|
definition.kind === "Operator" ? definition.params.length : 0;
|
|
123
181
|
|
|
@@ -126,6 +184,11 @@ export const getParamArities = (definition: Definition) =>
|
|
|
126
184
|
|
|
127
185
|
const isInstantiated = (definition: Definition) => definition.name.includes("!");
|
|
128
186
|
|
|
187
|
+
export const getFunctionArity = (definition: Definition) =>
|
|
188
|
+
definition.kind === "Function" && !isInstantiated(definition)
|
|
189
|
+
? definition.bounds.length
|
|
190
|
+
: undefined;
|
|
191
|
+
|
|
129
192
|
const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
130
193
|
for (let current = scope; current; current = current.parent) {
|
|
131
194
|
if (current.name === name) return current;
|
|
@@ -157,7 +220,7 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
|
|
|
157
220
|
if (current.kind === "Builtin") {
|
|
158
221
|
unit.errors.push({
|
|
159
222
|
location,
|
|
160
|
-
message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
|
|
223
|
+
message: `Symbol ${resolveSynonym(name)} is a built-in operator, and cannot be redefined.`,
|
|
161
224
|
});
|
|
162
225
|
} else if (
|
|
163
226
|
added.kind === "Parameter" ||
|
|
@@ -166,12 +229,12 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
|
|
|
166
229
|
) {
|
|
167
230
|
unit.errors.push({
|
|
168
231
|
location,
|
|
169
|
-
message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
232
|
+
message: `Multiply-defined symbol '${resolveSynonym(name)}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
170
233
|
});
|
|
171
234
|
} else {
|
|
172
235
|
unit.warnings.push({
|
|
173
236
|
location,
|
|
174
|
-
message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
|
|
237
|
+
message: `Multiple declarations or definitions for symbol ${resolveSynonym(name)}. \nThis duplicates the one at ${existing}.`,
|
|
175
238
|
});
|
|
176
239
|
}
|
|
177
240
|
return false;
|
|
@@ -230,26 +293,44 @@ export const getBoundNames = (bounds: Bound[]) =>
|
|
|
230
293
|
|
|
231
294
|
const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
232
295
|
const definition = unit.definitions.get(name);
|
|
296
|
+
const binding = getModuleBinding(name, unit, undefined);
|
|
233
297
|
const isVisible =
|
|
234
298
|
definition !== undefined &&
|
|
235
299
|
(definition === unit.self
|
|
236
|
-
? definition.
|
|
237
|
-
: definition.isRecursive
|
|
300
|
+
? definition.isRecursive
|
|
301
|
+
: definition.isRecursive
|
|
302
|
+
? binding !== undefined
|
|
303
|
+
: isBefore(definition.location, unit.location) &&
|
|
304
|
+
(binding === undefined || !isBefore(binding.position, definition.location)));
|
|
238
305
|
return isVisible ? definition : undefined;
|
|
239
306
|
};
|
|
240
307
|
|
|
241
308
|
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
242
309
|
const definition = getVisibleDefinition(name, unit);
|
|
243
|
-
|
|
244
|
-
?
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
310
|
+
if (definition) {
|
|
311
|
+
const declared = definition.isRecursive ? unit.bindings.get(name) : undefined;
|
|
312
|
+
return {
|
|
313
|
+
key: unit.getKey(definition),
|
|
314
|
+
arity: declared?.arity ?? getArity(definition),
|
|
315
|
+
params: declared?.params ?? getParamArities(definition),
|
|
316
|
+
kind: "Definition",
|
|
317
|
+
range: definition.location.range,
|
|
318
|
+
functionArity: getFunctionArity(definition),
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
const binding = getModuleBinding(name, unit, undefined);
|
|
322
|
+
if (binding?.isPending) {
|
|
323
|
+
return { key: name, arity: binding.arity, params: binding.params, kind: binding.kind };
|
|
324
|
+
}
|
|
325
|
+
return binding ? unit.imported.get(name) : undefined;
|
|
251
326
|
};
|
|
252
327
|
|
|
328
|
+
const isFact = (name: string, unit: Unit) =>
|
|
329
|
+
unit.facts.has(name) || unit.bindings.get(name)?.kind === "Theorem";
|
|
330
|
+
|
|
331
|
+
const getGoal = (name: string, unit: Unit) =>
|
|
332
|
+
unit.bindings.get(name)?.kind === "Theorem" ? unit.goals.get(name) : undefined;
|
|
333
|
+
|
|
253
334
|
const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
|
|
254
335
|
lookupLocal(scope, name) ?? findSymbol(name, unit);
|
|
255
336
|
|
|
@@ -271,12 +352,22 @@ const getSpelledNameRange = (
|
|
|
271
352
|
};
|
|
272
353
|
|
|
273
354
|
const getFixity = (expression: Expression) => {
|
|
274
|
-
if (expression.kind !== "Apply") return undefined;
|
|
355
|
+
if (expression.kind !== "Apply" || expression.argumentsRange) return undefined;
|
|
275
356
|
if (expression.args.length === 2) return "infix";
|
|
276
357
|
if (expression.args.length !== 1) return undefined;
|
|
277
|
-
return
|
|
358
|
+
return expression.operator === "-." ? "prefix" : "postfix";
|
|
278
359
|
};
|
|
279
360
|
|
|
361
|
+
const BUILTIN_LOCATION = "In module --TLA+ BUILTINS--";
|
|
362
|
+
|
|
363
|
+
const SELECTABLE_KINDS = new Set<SymbolKind>(["Definition", "Instance", "Theorem"]);
|
|
364
|
+
|
|
365
|
+
const reportUnselectable = (name: string, range: SourceRange, unit: Unit) =>
|
|
366
|
+
unit.errors.push({
|
|
367
|
+
location: formatRange(range),
|
|
368
|
+
message: `Cannot take subexpression of \`${resolveSynonym(name)}'.`,
|
|
369
|
+
});
|
|
370
|
+
|
|
280
371
|
const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
|
|
281
372
|
|
|
282
373
|
const isStepName = (name: string) =>
|
|
@@ -356,13 +447,30 @@ export const getBinder = (
|
|
|
356
447
|
}
|
|
357
448
|
};
|
|
358
449
|
|
|
450
|
+
// SANY parses `a /\ b /\ c` as `(a /\ b) /\ c`, so an infix junction has two operands.
|
|
451
|
+
const getJunctionOperands = (expression: Expression & { kind: "And" | "Or" }) => {
|
|
452
|
+
const operands = expression.operands;
|
|
453
|
+
const range = expression.location.range;
|
|
454
|
+
const end = expression.prefixEnds?.[operands.length - 3];
|
|
455
|
+
if (expression.isBulleted || !range || !end) return operands;
|
|
456
|
+
return [
|
|
457
|
+
{
|
|
458
|
+
...expression,
|
|
459
|
+
operands: operands.slice(0, -1),
|
|
460
|
+
prefixEnds: expression.prefixEnds?.slice(0, -1),
|
|
461
|
+
location: { ...expression.location, range: { ...range, end } },
|
|
462
|
+
},
|
|
463
|
+
operands[operands.length - 1],
|
|
464
|
+
];
|
|
465
|
+
};
|
|
466
|
+
|
|
359
467
|
export const getOperands = (expression: Expression): Expression[] | undefined => {
|
|
360
468
|
switch (expression.kind) {
|
|
361
469
|
case "Apply":
|
|
362
470
|
return expression.args;
|
|
363
471
|
case "And":
|
|
364
472
|
case "Or":
|
|
365
|
-
return expression
|
|
473
|
+
return getJunctionOperands(expression);
|
|
366
474
|
case "If":
|
|
367
475
|
return [expression.condition, expression.consequent, expression.alternative];
|
|
368
476
|
case "Tuple":
|
|
@@ -388,6 +496,7 @@ export const getOperands = (expression: Expression): Expression[] | undefined =>
|
|
|
388
496
|
case "Enabled":
|
|
389
497
|
case "Always":
|
|
390
498
|
case "Eventually":
|
|
499
|
+
case "Selection":
|
|
391
500
|
return [expression.expression];
|
|
392
501
|
case "BoxAction":
|
|
393
502
|
case "AngleAction":
|
|
@@ -416,12 +525,19 @@ const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
|
|
|
416
525
|
|
|
417
526
|
const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
|
|
418
527
|
if (expression.kind === "Label") return expression.name === name ? expression : undefined;
|
|
419
|
-
if (expression.kind === "Lambda") return undefined;
|
|
528
|
+
if (expression.kind === "Lambda" || expression.kind === "Selection") return undefined;
|
|
420
529
|
if (expression.kind === "Let") return findLabel(expression.body, name);
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
undefined
|
|
424
|
-
)
|
|
530
|
+
const generated = generatedArgumentCounts.get(expression);
|
|
531
|
+
if (expression.kind === "Apply" && generated === undefined && isEmptyExpression(expression))
|
|
532
|
+
return undefined;
|
|
533
|
+
if (expression.kind === "FunctionApplication" && isEmptyExpression(expression))
|
|
534
|
+
return findLabel(expression.function, name);
|
|
535
|
+
return getChildren(expression)
|
|
536
|
+
.slice(0, generated)
|
|
537
|
+
.reduce<LabelExpression | undefined>(
|
|
538
|
+
(found, child) => found ?? findLabel(child, name),
|
|
539
|
+
undefined,
|
|
540
|
+
);
|
|
425
541
|
};
|
|
426
542
|
|
|
427
543
|
const findClauseLabel = (
|
|
@@ -453,7 +569,6 @@ const checkSelection = (
|
|
|
453
569
|
reference: Reference,
|
|
454
570
|
selectors: Selector[],
|
|
455
571
|
goal: Goal,
|
|
456
|
-
scope: LocalName | undefined,
|
|
457
572
|
unit: Unit,
|
|
458
573
|
isFact: boolean,
|
|
459
574
|
) => {
|
|
@@ -462,6 +577,7 @@ const checkSelection = (
|
|
|
462
577
|
(index === undefined ? undefined : reference.selectorRanges?.[index]) ??
|
|
463
578
|
reference.location.range;
|
|
464
579
|
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
580
|
+
return undefined;
|
|
465
581
|
};
|
|
466
582
|
const isOutOfScope = goal.isSuffices === goal.isInProof;
|
|
467
583
|
const statement = goal.statement;
|
|
@@ -470,6 +586,8 @@ const checkSelection = (
|
|
|
470
586
|
isGoal: true,
|
|
471
587
|
isSufficesOpen: goal.isSuffices,
|
|
472
588
|
};
|
|
589
|
+
let params = 0;
|
|
590
|
+
let args = 0;
|
|
473
591
|
for (const [offset, selector] of selectors.slice(1).entries()) {
|
|
474
592
|
const index = offset + 1;
|
|
475
593
|
const node = state.node;
|
|
@@ -479,9 +597,7 @@ const checkSelection = (
|
|
|
479
597
|
node.kind !== "NewSymbol" &&
|
|
480
598
|
node.kind !== "AssumeProve" &&
|
|
481
599
|
getLabelBody(node).kind === "Let";
|
|
482
|
-
return isLet
|
|
483
|
-
? undefined
|
|
484
|
-
: report(index - 1, "A name selector here must be from a LET clause.");
|
|
600
|
+
return isLet ? node : report(index - 1, "A name selector here must be from a LET clause.");
|
|
485
601
|
}
|
|
486
602
|
const clauses =
|
|
487
603
|
statement.kind === "AssumeProve"
|
|
@@ -519,10 +635,30 @@ const checkSelection = (
|
|
|
519
635
|
state.isSufficesOpen = false;
|
|
520
636
|
continue;
|
|
521
637
|
}
|
|
522
|
-
if (selector.kind
|
|
523
|
-
|
|
638
|
+
if (selector.kind === "Colon") {
|
|
639
|
+
if (selectors[index - 1].kind !== "Name") {
|
|
640
|
+
return report(index - 1, "!: should not follow an operand selector.");
|
|
641
|
+
}
|
|
642
|
+
const next = selectors[index + 1];
|
|
643
|
+
if (next === undefined ? index !== 1 : next.kind !== "Name") {
|
|
644
|
+
return report(
|
|
645
|
+
index,
|
|
646
|
+
"`!:' can be used only after a name and either at the end after an\noperator name or before an operator name.",
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
const operand =
|
|
652
|
+
selector.kind === "Operand"
|
|
653
|
+
? getOperandName(selector.operand)
|
|
654
|
+
: selector.kind === "At"
|
|
655
|
+
? "@"
|
|
656
|
+
: "!(...)";
|
|
657
|
+
const getPosition = (count: number) =>
|
|
658
|
+
selector.kind === "Operand" ? getOperandIndex(selector.operand, count) : -1;
|
|
659
|
+
const unexpectedNull = `An unexpected null node specified by '${operand}'.\nThis is probably due to a previous error.`;
|
|
524
660
|
if (state.isSufficesOpen) {
|
|
525
|
-
if (
|
|
661
|
+
if (getPosition(1) !== 0) {
|
|
526
662
|
return report(
|
|
527
663
|
index,
|
|
528
664
|
node.kind === "AssumeProve"
|
|
@@ -533,20 +669,37 @@ const checkSelection = (
|
|
|
533
669
|
state.isSufficesOpen = false;
|
|
534
670
|
continue;
|
|
535
671
|
}
|
|
536
|
-
if (node.kind === "NewSymbol") return;
|
|
672
|
+
if (node.kind === "NewSymbol") return node;
|
|
537
673
|
if (node.kind !== "AssumeProve") {
|
|
538
674
|
const expression = getLabelBody(node);
|
|
675
|
+
if (unresolvedExpressions.has(expression)) {
|
|
676
|
+
return report(
|
|
677
|
+
index,
|
|
678
|
+
"Subexpression selection failed, probably due to an error in the selected expression.",
|
|
679
|
+
);
|
|
680
|
+
}
|
|
539
681
|
if (["Number", "Decimal", "String"].includes(expression.kind)) {
|
|
540
682
|
return report(index, "Selecting subexpression of expression that has none.");
|
|
541
683
|
}
|
|
542
|
-
|
|
543
|
-
expression.kind === "Identifier"
|
|
544
|
-
? findBinding(expression.name, scope, unit)?.kind
|
|
545
|
-
: undefined;
|
|
546
|
-
if (kind === "Variable" || kind?.startsWith("New")) {
|
|
684
|
+
if (isEmptyExpression(expression)) {
|
|
547
685
|
return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
|
|
548
686
|
}
|
|
549
|
-
const
|
|
687
|
+
const binder = getBinder(expression);
|
|
688
|
+
if (binder && selector.kind !== "Operand") {
|
|
689
|
+
const count = selector.kind === "Bound" ? selector.args.length : 0;
|
|
690
|
+
if (selector.kind === "Bound" && count !== binder.names.length) {
|
|
691
|
+
return report(
|
|
692
|
+
index,
|
|
693
|
+
`Selector with ${count} argument(s) used for quantifier with ${binder.names.length} bound identifier(s).`,
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
params += binder.names.length;
|
|
697
|
+
args += count;
|
|
698
|
+
state.node = binder.body;
|
|
699
|
+
state.isGoal = false;
|
|
700
|
+
continue;
|
|
701
|
+
}
|
|
702
|
+
const sets = binder?.sets;
|
|
550
703
|
const operands =
|
|
551
704
|
expression.kind === "Identifier"
|
|
552
705
|
? []
|
|
@@ -554,18 +707,21 @@ const checkSelection = (
|
|
|
554
707
|
? sets.length > 0
|
|
555
708
|
? sets
|
|
556
709
|
: undefined
|
|
557
|
-
: expression
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
const position = getOperandIndex(selector.operand, operands.length);
|
|
710
|
+
: getOperands(expression);
|
|
711
|
+
if (!operands) return node;
|
|
712
|
+
const isExcept = expression.kind === "Except";
|
|
713
|
+
const position = getPosition(isExcept ? expression.updates.length + 1 : operands.length);
|
|
562
714
|
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
715
|
+
if (isExcept && position > 0) {
|
|
716
|
+
return report(index, "Selecting subexpression of an EXCEPT not yet implemented.");
|
|
717
|
+
}
|
|
563
718
|
state.node = operands[position];
|
|
564
719
|
state.isGoal = false;
|
|
720
|
+
if (unresolvedExpressions.has(state.node)) return report(index, unexpectedNull);
|
|
565
721
|
continue;
|
|
566
722
|
}
|
|
567
723
|
const clauses = [...node.assumptions, node.conclusion];
|
|
568
|
-
const position =
|
|
724
|
+
const position = getPosition(clauses.length);
|
|
569
725
|
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
570
726
|
if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
|
|
571
727
|
return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
|
|
@@ -576,14 +732,24 @@ const checkSelection = (
|
|
|
576
732
|
}
|
|
577
733
|
state.node = clause;
|
|
578
734
|
state.isGoal = false;
|
|
735
|
+
if (
|
|
736
|
+
clause.kind !== "NewSymbol" &&
|
|
737
|
+
clause.kind !== "AssumeProve" &&
|
|
738
|
+
unresolvedExpressions.has(clause)
|
|
739
|
+
) {
|
|
740
|
+
return report(index, unexpectedNull);
|
|
741
|
+
}
|
|
579
742
|
}
|
|
580
|
-
if (isFact) return;
|
|
581
743
|
if (state.node.kind === "AssumeProve") {
|
|
582
|
-
report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
744
|
+
return isFact ? state.node : report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
583
745
|
}
|
|
584
746
|
if (state.node.kind === "NewSymbol") {
|
|
585
|
-
|
|
747
|
+
return isFact
|
|
748
|
+
? state.node
|
|
749
|
+
: report(undefined, "Selected a NEW declaration as an expression or operator.");
|
|
586
750
|
}
|
|
751
|
+
if (params !== args) return report(undefined, `Expected ${params} arguments but found ${args}.`);
|
|
752
|
+
return state.node;
|
|
587
753
|
};
|
|
588
754
|
|
|
589
755
|
const isLabelReported = (label: LabelExpression, unit: Unit) => {
|
|
@@ -659,10 +825,18 @@ const checkReference = (
|
|
|
659
825
|
const first = selectors[0];
|
|
660
826
|
const goal =
|
|
661
827
|
selectors.length > 1 && first.kind === "Name"
|
|
662
|
-
? (lookupLocal(scope, first.name)?.goal ??
|
|
828
|
+
? (lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit))
|
|
663
829
|
: undefined;
|
|
664
|
-
|
|
665
|
-
|
|
830
|
+
return goal && { node: checkSelection(expression, selectors, goal, unit, isFact) };
|
|
831
|
+
};
|
|
832
|
+
|
|
833
|
+
const isInstanceName = (name: string, scope: LocalName | undefined, unit: Unit) => {
|
|
834
|
+
const binding = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
|
|
835
|
+
if (binding) return binding.kind === "Instance";
|
|
836
|
+
return (
|
|
837
|
+
findInstance(name, scope, unit) !== undefined &&
|
|
838
|
+
[...unit.bindings.keys()].some((key) => key.startsWith(`${name}!`))
|
|
839
|
+
);
|
|
666
840
|
};
|
|
667
841
|
|
|
668
842
|
const getSymbolArity = (
|
|
@@ -677,18 +851,53 @@ const getSymbolArity = (
|
|
|
677
851
|
if (renamed !== undefined) return renamed;
|
|
678
852
|
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
679
853
|
const local = lookupLocal(scope, name);
|
|
680
|
-
if (local?.
|
|
854
|
+
if (local?.nonFactStep) {
|
|
681
855
|
unit.errors.push({
|
|
682
856
|
location: formatRange(getSpelledNameRange(expression, name)),
|
|
683
857
|
message: "Step number of non-expression step used as an expression.",
|
|
684
858
|
});
|
|
685
859
|
return undefined;
|
|
686
860
|
}
|
|
861
|
+
const keyword = local?.goal?.keyword;
|
|
862
|
+
if (keyword) {
|
|
863
|
+
unit.errors.push({
|
|
864
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
865
|
+
message: `${keyword} proof step selected instead of expression.`,
|
|
866
|
+
});
|
|
867
|
+
return undefined;
|
|
868
|
+
}
|
|
687
869
|
if (checkReference(expression, scope, unit, false) || (isStepName(name) && local)) {
|
|
688
870
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
689
871
|
}
|
|
872
|
+
if (isInstanceName(name, scope, unit)) {
|
|
873
|
+
const instance = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
|
|
874
|
+
const count = expression.kind === "Apply" ? expression.args.length : 0;
|
|
875
|
+
if (instance?.kind === "Instance" && instance.arity !== count) {
|
|
876
|
+
unit.errors.push({
|
|
877
|
+
location: formatRange(
|
|
878
|
+
(expression.kind === "Apply" && expression.argumentsRange) ||
|
|
879
|
+
getSpelledNameRange(expression, name),
|
|
880
|
+
),
|
|
881
|
+
message: `The operator ${name} requires ${instance.arity} arguments.`,
|
|
882
|
+
});
|
|
883
|
+
return undefined;
|
|
884
|
+
}
|
|
885
|
+
const range = getSpelledNameRange(expression, name);
|
|
886
|
+
unit.errors.push({
|
|
887
|
+
location: formatRange({
|
|
888
|
+
...range,
|
|
889
|
+
start: {
|
|
890
|
+
line: range.end.line,
|
|
891
|
+
column: range.end.column - name.slice(name.lastIndexOf("!") + 1).length + 1,
|
|
892
|
+
},
|
|
893
|
+
}),
|
|
894
|
+
message: `Operator name ${name} is incomplete.`,
|
|
895
|
+
});
|
|
896
|
+
return undefined;
|
|
897
|
+
}
|
|
690
898
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
691
899
|
if (local) return local.arity;
|
|
900
|
+
if (getGoal(name, unit)?.statement.kind === "AssumeProve") return 0;
|
|
692
901
|
const symbol = findSymbol(name, unit);
|
|
693
902
|
if (symbol) {
|
|
694
903
|
if (expression.kind === "Identifier") expression.name = symbol.key;
|
|
@@ -697,29 +906,58 @@ const getSymbolArity = (
|
|
|
697
906
|
return symbol.arity;
|
|
698
907
|
}
|
|
699
908
|
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
700
|
-
const
|
|
909
|
+
const selected =
|
|
910
|
+
prefix === "" ? undefined : (lookupLocal(scope, prefix) ?? findSymbol(prefix, unit));
|
|
911
|
+
if (
|
|
912
|
+
selected &&
|
|
913
|
+
(!SELECTABLE_KINDS.has(selected.kind) || lookupLocal(scope, prefix)?.nonFactStep)
|
|
914
|
+
) {
|
|
915
|
+
reportUnselectable(
|
|
916
|
+
prefix,
|
|
917
|
+
(expression.kind !== "OperatorReference" && expression.selectorRanges?.[0]) ||
|
|
918
|
+
getSpelledNameRange(expression, name),
|
|
919
|
+
unit,
|
|
920
|
+
);
|
|
921
|
+
return undefined;
|
|
922
|
+
}
|
|
923
|
+
if (
|
|
924
|
+
expression.kind !== "OperatorReference" &&
|
|
701
925
|
prefix !== "" &&
|
|
702
|
-
(
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
926
|
+
isPendingSelectionReported(
|
|
927
|
+
prefix,
|
|
928
|
+
expression.kind === "Apply" && expression.argumentCounts
|
|
929
|
+
? expression.args.slice(0, expression.argumentCounts[0])
|
|
930
|
+
: [],
|
|
931
|
+
expression.selectorRanges?.[0],
|
|
932
|
+
(expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ?? location.range,
|
|
933
|
+
scope,
|
|
934
|
+
unit,
|
|
935
|
+
)
|
|
936
|
+
) {
|
|
937
|
+
return undefined;
|
|
938
|
+
}
|
|
939
|
+
if (selected || (prefix !== "" && isFact(prefix, unit))) {
|
|
706
940
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
707
941
|
}
|
|
708
|
-
const
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
: getSpelledNameRange(expression, name),
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
|
|
942
|
+
const fixity = getFixity(expression);
|
|
943
|
+
const isOperatorApplication = expression.kind === "OperatorReference" || fixity !== undefined;
|
|
944
|
+
if (!isOperatorSymbol(name) || isStepName(name) || !isOperatorApplication) {
|
|
945
|
+
unit.errors.push({
|
|
946
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
947
|
+
message: `Unknown operator: \`${resolveSynonym(name)}'.`,
|
|
948
|
+
});
|
|
716
949
|
return undefined;
|
|
717
950
|
}
|
|
951
|
+
const spelling = expression.spelling ?? name;
|
|
718
952
|
unit.errors.push({
|
|
719
|
-
location:
|
|
953
|
+
location: formatRange(
|
|
954
|
+
fixity === "prefix" && spelling === "-."
|
|
955
|
+
? getNameRange(location, "-")
|
|
956
|
+
: getSpelledNameRange(expression, name),
|
|
957
|
+
),
|
|
720
958
|
message: `Could not find declaration or definition of symbol '${spelling}'.`,
|
|
721
959
|
});
|
|
722
|
-
|
|
960
|
+
if (fixity) unresolvedExpressions.add(expression);
|
|
723
961
|
if (fixity && location.range) {
|
|
724
962
|
unit.errors.push({
|
|
725
963
|
location: formatRange(location.range),
|
|
@@ -774,15 +1012,30 @@ const resolveDefinition = (
|
|
|
774
1012
|
const isDeclared = current?.isPending === true && recursive.has(current);
|
|
775
1013
|
if (current?.isPending && !isDeclared) {
|
|
776
1014
|
report(
|
|
777
|
-
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
|
|
1015
|
+
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${resolveSynonym(name)} defined at wrong LET/IN level.`,
|
|
778
1016
|
);
|
|
779
1017
|
} else if (current && !isDeclared) {
|
|
780
1018
|
report(
|
|
781
1019
|
definition.kind === "Function"
|
|
782
1020
|
? `Function name \`${name}' already defined or declared.`
|
|
783
|
-
: `Operator ${name} already defined or declared.`,
|
|
1021
|
+
: `Operator ${resolveSynonym(name)} already defined or declared.`,
|
|
1022
|
+
);
|
|
1023
|
+
} else if (current && definition.kind === "Function" && current.arity > 0) {
|
|
1024
|
+
report(`Function ${resolveSynonym(name)} has operator arguments in its RECURSIVE declaration.`);
|
|
1025
|
+
} else if (
|
|
1026
|
+
current &&
|
|
1027
|
+
definition.kind === "Operator" &&
|
|
1028
|
+
// Deviation from SANY: Generator#processOperator compares only the last parameter's arity with the RECURSIVE declaration's.
|
|
1029
|
+
(current.arity !== definition.params.length ||
|
|
1030
|
+
definition.params.some((param) => param.arity > 0))
|
|
1031
|
+
) {
|
|
1032
|
+
report(
|
|
1033
|
+
`Definition of ${resolveSynonym(name)} has different arity than its RECURSIVE declaration.`,
|
|
784
1034
|
);
|
|
785
1035
|
}
|
|
1036
|
+
if (current && isDeclared && definition.kind === "Operator") {
|
|
1037
|
+
current.params = getParamArities(definition);
|
|
1038
|
+
}
|
|
786
1039
|
if (definition.kind === "Function") {
|
|
787
1040
|
resolveExpression(
|
|
788
1041
|
definition.body,
|
|
@@ -793,8 +1046,10 @@ const resolveDefinition = (
|
|
|
793
1046
|
definition.isSelfReferential = isReferenced(definition.body, name);
|
|
794
1047
|
} else resolveExpression(definition.body, inner, unit);
|
|
795
1048
|
if (current && isDeclared) {
|
|
796
|
-
current.
|
|
797
|
-
|
|
1049
|
+
if (definition.kind === "Operator" || current.arity === 0) {
|
|
1050
|
+
current.isPending = false;
|
|
1051
|
+
current.range = range;
|
|
1052
|
+
}
|
|
798
1053
|
return false;
|
|
799
1054
|
}
|
|
800
1055
|
return definition.kind === "Function"
|
|
@@ -812,10 +1067,15 @@ const resolveDefinitions = (
|
|
|
812
1067
|
scope: LocalName | undefined,
|
|
813
1068
|
unit: Unit,
|
|
814
1069
|
instances: Instance[] = [],
|
|
1070
|
+
declarations: Declaration[] = [],
|
|
815
1071
|
) => {
|
|
816
1072
|
const recursive = new Set<Binding>();
|
|
817
1073
|
const declared = definitions
|
|
818
|
-
.filter(
|
|
1074
|
+
.filter(
|
|
1075
|
+
(definition) =>
|
|
1076
|
+
definition.isRecursive &&
|
|
1077
|
+
declarations.every((declaration) => declaration.name !== definition.name),
|
|
1078
|
+
)
|
|
819
1079
|
.reduce<LocalName | undefined>((parent, definition) => {
|
|
820
1080
|
if (lookupLocal(parent, definition.name)?.isPending) return parent;
|
|
821
1081
|
const local: LocalName = {
|
|
@@ -829,28 +1089,62 @@ const resolveDefinitions = (
|
|
|
829
1089
|
recursive.add(local);
|
|
830
1090
|
return local;
|
|
831
1091
|
}, scope);
|
|
832
|
-
const
|
|
833
|
-
const
|
|
1092
|
+
const declaredLocals = new Map<Declaration, LocalName | undefined>();
|
|
1093
|
+
const bindDeclaration = (parent: LocalName | undefined, declaration: Declaration) => {
|
|
1094
|
+
const binding: Binding = {
|
|
1095
|
+
kind: "Definition",
|
|
1096
|
+
arity: declaration.arity,
|
|
1097
|
+
params: Array(declaration.arity).fill(0),
|
|
1098
|
+
range: declaration.location.range,
|
|
1099
|
+
};
|
|
1100
|
+
const local: LocalName | undefined = isBindable(
|
|
1101
|
+
declaration.name,
|
|
1102
|
+
lookupLocal(parent, declaration.name) ??
|
|
1103
|
+
getModuleBinding(declaration.name, unit, binding.range),
|
|
1104
|
+
binding,
|
|
1105
|
+
unit,
|
|
1106
|
+
)
|
|
1107
|
+
? { ...binding, name: declaration.name, parent, isPending: true }
|
|
1108
|
+
: undefined;
|
|
1109
|
+
if (local) recursive.add(local);
|
|
1110
|
+
declaredLocals.set(declaration, local);
|
|
1111
|
+
return local ?? parent;
|
|
1112
|
+
};
|
|
1113
|
+
const pending = [...(unit.isGenerated ? [] : instances), ...declarations].toSorted(
|
|
1114
|
+
(left, right) => compareLocations(left.location, right.location),
|
|
1115
|
+
);
|
|
1116
|
+
const resolvePending = (
|
|
834
1117
|
current: LocalName | undefined,
|
|
835
|
-
isDue: (
|
|
1118
|
+
isDue: (item: Instance | Declaration) => boolean,
|
|
836
1119
|
) => {
|
|
837
|
-
const count = pending.findIndex((
|
|
1120
|
+
const count = pending.findIndex((item) => !isDue(item));
|
|
838
1121
|
return pending
|
|
839
1122
|
.splice(0, count === -1 ? pending.length : count)
|
|
840
|
-
.reduce(
|
|
1123
|
+
.reduce(
|
|
1124
|
+
(parent, item) =>
|
|
1125
|
+
"kind" in item ? resolveLocalInstance(item, parent, unit) : bindDeclaration(parent, item),
|
|
1126
|
+
current,
|
|
1127
|
+
);
|
|
841
1128
|
};
|
|
842
1129
|
const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
|
|
843
1130
|
const current = isKey(definition.name)
|
|
844
1131
|
? previous
|
|
845
|
-
:
|
|
1132
|
+
: resolvePending(previous, (item) => !isBefore(definition.location, item.location));
|
|
846
1133
|
const local: LocalName = {
|
|
847
1134
|
name: definition.name,
|
|
848
1135
|
arity: getArity(definition),
|
|
849
1136
|
kind: "Definition",
|
|
850
1137
|
range: definition.location.range,
|
|
851
1138
|
parent: current,
|
|
1139
|
+
functionArity: getFunctionArity(definition),
|
|
852
1140
|
};
|
|
853
|
-
if (isInstantiated(definition))
|
|
1141
|
+
if (isInstantiated(definition)) {
|
|
1142
|
+
return {
|
|
1143
|
+
...local,
|
|
1144
|
+
params: getParamArities(definition),
|
|
1145
|
+
functionArity: lookupLocal(current, definition.name)?.functionArity,
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
854
1148
|
const isInstanced = instances.some((instance) => instance.location === definition.location);
|
|
855
1149
|
const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
|
|
856
1150
|
const inner = isInstanced
|
|
@@ -862,140 +1156,277 @@ const resolveDefinitions = (
|
|
|
862
1156
|
? { ...local, params: getParamArities(definition) }
|
|
863
1157
|
: current;
|
|
864
1158
|
}, declared);
|
|
865
|
-
|
|
1159
|
+
const resolvedScope = resolvePending(resolved, () => true);
|
|
1160
|
+
declaredLocals.forEach((local, declaration) => {
|
|
1161
|
+
const range = declaration.location.range;
|
|
1162
|
+
if (range && !unit.isGenerated && (!local || local.isPending)) {
|
|
1163
|
+
unit.errors.push({
|
|
1164
|
+
location: formatRange(range),
|
|
1165
|
+
message: `Symbol ${resolveSynonym(declaration.name)} declared in RECURSIVE statement but not defined.`,
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
});
|
|
1169
|
+
return resolvedScope;
|
|
866
1170
|
};
|
|
867
1171
|
|
|
868
|
-
const
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
prefix
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
};
|
|
1172
|
+
const getInstances = (names: string[], scope: LocalName | undefined, unit: Unit) =>
|
|
1173
|
+
names.slice(0, -1).map((_name, index) => {
|
|
1174
|
+
const prefix = names.slice(0, index + 1).join("!");
|
|
1175
|
+
const binding = lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined);
|
|
1176
|
+
return binding?.kind === "Instance"
|
|
1177
|
+
? { name: prefix, arity: binding.arity, range: binding.range }
|
|
1178
|
+
: undefined;
|
|
1179
|
+
});
|
|
1180
|
+
|
|
1181
|
+
const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
|
|
1182
|
+
getInstances(name.split("!"), scope, unit)[0];
|
|
876
1183
|
|
|
877
1184
|
const isInstanceArityReported = (
|
|
878
1185
|
expression: Expression,
|
|
879
1186
|
scope: LocalName | undefined,
|
|
880
1187
|
unit: Unit,
|
|
881
1188
|
) => {
|
|
882
|
-
if (expression
|
|
883
|
-
const
|
|
884
|
-
|
|
1189
|
+
if (!isReference(expression)) return false;
|
|
1190
|
+
const selectors = getSelectors(expression);
|
|
1191
|
+
const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
|
|
1192
|
+
const names = selectors
|
|
1193
|
+
.slice(0, nameCount === -1 ? selectors.length : nameCount)
|
|
1194
|
+
.flatMap((selector) => (selector.kind === "Name" ? [selector] : []));
|
|
1195
|
+
const instances = getInstances(
|
|
1196
|
+
names.map((name) => name.name),
|
|
885
1197
|
scope,
|
|
886
1198
|
unit,
|
|
887
1199
|
);
|
|
888
|
-
const
|
|
889
|
-
|
|
1200
|
+
const index = instances.findIndex(
|
|
1201
|
+
(instance, position) =>
|
|
1202
|
+
instance !== undefined &&
|
|
1203
|
+
(instances[position - 1]?.arity ?? 0) + names[position].args.length !== instance.arity,
|
|
1204
|
+
);
|
|
1205
|
+
const instance = instances[index];
|
|
1206
|
+
if (!instance) return false;
|
|
890
1207
|
const range =
|
|
891
|
-
(expression.kind === "
|
|
1208
|
+
(expression.kind === "Identifier" ? undefined : expression.argumentRanges?.[index]) ??
|
|
892
1209
|
expression.location.range;
|
|
893
1210
|
if (range) {
|
|
894
1211
|
unit.errors.push({
|
|
895
1212
|
location: formatRange(range),
|
|
896
|
-
message: `The operator ${instance.name} requires ${instance.arity} arguments.`,
|
|
1213
|
+
message: `The operator ${instance.name} requires ${instance.arity - (instances[index - 1]?.arity ?? 0)} arguments.`,
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
return true;
|
|
1217
|
+
};
|
|
1218
|
+
|
|
1219
|
+
const isArgumentCountReported = (
|
|
1220
|
+
callee: Binding | undefined,
|
|
1221
|
+
name: string,
|
|
1222
|
+
count: number,
|
|
1223
|
+
range: SourceRange | undefined,
|
|
1224
|
+
unit: Unit,
|
|
1225
|
+
) => {
|
|
1226
|
+
const params = callee?.kind === "Definition" ? callee.params : undefined;
|
|
1227
|
+
if (!callee || !params || params.length === callee.arity) return false;
|
|
1228
|
+
if (range && !unit.isGenerated) {
|
|
1229
|
+
unit.errors.push({
|
|
1230
|
+
location: formatRange(range),
|
|
1231
|
+
message: `Wrong number of arguments (${count}) given to operator '${resolveSynonym(name)}', \nwhich requires ${params.length} arguments.`,
|
|
897
1232
|
});
|
|
898
1233
|
}
|
|
899
1234
|
return true;
|
|
900
1235
|
};
|
|
901
1236
|
|
|
1237
|
+
const resolveExpressionArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1238
|
+
const range = arg.location.range;
|
|
1239
|
+
const report = (message: string) => {
|
|
1240
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
1241
|
+
};
|
|
1242
|
+
const selectionError = operatorSelectionErrors.get(arg);
|
|
1243
|
+
// Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
|
|
1244
|
+
if (arg.kind === "Lambda" && arg.operator !== undefined) {
|
|
1245
|
+
report(`The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
|
|
1246
|
+
} else if (selectionError) {
|
|
1247
|
+
unit.errors.push(selectionError);
|
|
1248
|
+
} else if (arg.kind === "Lambda" && range) {
|
|
1249
|
+
unresolvedExpressions.add(arg);
|
|
1250
|
+
report("LAMBDA expression used where an expression is required.");
|
|
1251
|
+
} else if (arg.kind === "OperatorReference") {
|
|
1252
|
+
const argArity = getSymbolArity(arg, arg.operator, scope, unit);
|
|
1253
|
+
if (argArity !== undefined) {
|
|
1254
|
+
report(`The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`);
|
|
1255
|
+
}
|
|
1256
|
+
} else resolveExpression(arg, scope, unit);
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
const getSelectorArgumentError = (reference: Expression, isDefEntry = false) => {
|
|
1260
|
+
if (!isReference(reference)) return undefined;
|
|
1261
|
+
// SANY reads a reference that ends in arguments as an application, not as a selector's arguments, outside a DEF clause.
|
|
1262
|
+
const selectors =
|
|
1263
|
+
isDefEntry ||
|
|
1264
|
+
reference.kind === "Subexpression" ||
|
|
1265
|
+
(reference.kind === "Apply" && reference.argumentCounts?.at(-1) === 0)
|
|
1266
|
+
? getSelectors(reference)
|
|
1267
|
+
: [];
|
|
1268
|
+
const index = selectors.findIndex((selector) => "args" in selector && selector.args.length > 0);
|
|
1269
|
+
if (index === -1) return undefined;
|
|
1270
|
+
const selector = selectors[index];
|
|
1271
|
+
return {
|
|
1272
|
+
index,
|
|
1273
|
+
range: reference.selectorRanges?.[index],
|
|
1274
|
+
message: `Selector \`${selector.kind === "Name" ? selector.name : "!(...)"}' should not have argument(s).`,
|
|
1275
|
+
};
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
const matchArgument = (
|
|
1279
|
+
arg: Expression,
|
|
1280
|
+
expected: number,
|
|
1281
|
+
index: number,
|
|
1282
|
+
operator: string,
|
|
1283
|
+
operatorRange: SourceRange | undefined,
|
|
1284
|
+
scope: LocalName | undefined,
|
|
1285
|
+
unit: Unit,
|
|
1286
|
+
) => {
|
|
1287
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
1288
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
1289
|
+
};
|
|
1290
|
+
const reportOperatorArity = (name: string, arity: number) => {
|
|
1291
|
+
const definition =
|
|
1292
|
+
findInstance(operator, scope, unit)?.range ?? findBinding(operator, scope, unit)?.range;
|
|
1293
|
+
if (!definition) return;
|
|
1294
|
+
report(
|
|
1295
|
+
operatorRange,
|
|
1296
|
+
`Operator with incorrect arity passed as argument. \nOperator '${resolveSynonym(name)}' of arity ${arity} is argument number ${index + 1} (counting from 1) to operator \`${formatRange(definition)}', \nbut an operator of arity ${expected} was expected.`,
|
|
1297
|
+
);
|
|
1298
|
+
};
|
|
1299
|
+
const range = arg.location.range;
|
|
1300
|
+
if (expected === 0) {
|
|
1301
|
+
resolveExpressionArgument(arg, scope, unit);
|
|
1302
|
+
return true;
|
|
1303
|
+
}
|
|
1304
|
+
const parenthesized = parenthesizedRanges.get(arg);
|
|
1305
|
+
if (parenthesized) {
|
|
1306
|
+
report(
|
|
1307
|
+
parenthesized,
|
|
1308
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
1309
|
+
);
|
|
1310
|
+
return false;
|
|
1311
|
+
}
|
|
1312
|
+
const reference = selections.get(arg) ?? arg;
|
|
1313
|
+
const argumentError = getSelectorArgumentError(reference);
|
|
1314
|
+
if (argumentError) {
|
|
1315
|
+
report(argumentError.range, argumentError.message);
|
|
1316
|
+
return false;
|
|
1317
|
+
}
|
|
1318
|
+
if (arg.kind === "Lambda") {
|
|
1319
|
+
if (selections.has(arg)) resolveExpression(arg, scope, unit);
|
|
1320
|
+
else resolveLambda(arg, scope, unit);
|
|
1321
|
+
if (findNullError(nullSelections.get(arg))) return false;
|
|
1322
|
+
const isMatched = arg.params.length === expected;
|
|
1323
|
+
if (!isMatched && selections.has(arg)) {
|
|
1324
|
+
report(range, `Expected arity ${expected} but found operator of arity ${arg.params.length}.`);
|
|
1325
|
+
} else if (!isMatched && range && arg.operator === undefined) {
|
|
1326
|
+
report(
|
|
1327
|
+
operatorRange,
|
|
1328
|
+
`Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
|
|
1329
|
+
);
|
|
1330
|
+
} else if (!isMatched && arg.operator !== undefined) {
|
|
1331
|
+
reportOperatorArity(arg.operator, arg.params.length);
|
|
1332
|
+
}
|
|
1333
|
+
return isMatched;
|
|
1334
|
+
}
|
|
1335
|
+
if (reference !== arg) {
|
|
1336
|
+
const failed = findNullError(nullSelections.get(arg));
|
|
1337
|
+
if (failed) unit.errors.push(failed);
|
|
1338
|
+
if (!failed) {
|
|
1339
|
+
report(reference.location.range, `Expected arity ${expected} but found operator of arity 0.`);
|
|
1340
|
+
}
|
|
1341
|
+
return false;
|
|
1342
|
+
}
|
|
1343
|
+
if (arg.kind === "Subexpression") {
|
|
1344
|
+
resolveExpression(arg, scope, unit);
|
|
1345
|
+
return false;
|
|
1346
|
+
}
|
|
1347
|
+
if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
|
|
1348
|
+
report(
|
|
1349
|
+
range,
|
|
1350
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
1351
|
+
);
|
|
1352
|
+
return false;
|
|
1353
|
+
}
|
|
1354
|
+
const name = arg.kind === "Identifier" ? arg.name : arg.operator;
|
|
1355
|
+
const argParams = findBinding(name, scope, unit)?.params ?? [];
|
|
1356
|
+
const argArity = getSymbolArity(arg, name, scope, unit);
|
|
1357
|
+
if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
|
|
1358
|
+
report(
|
|
1359
|
+
getSpelledNameRange(arg, name),
|
|
1360
|
+
`Expected arity ${expected} but found operator of arity ${argArity}.`,
|
|
1361
|
+
);
|
|
1362
|
+
}
|
|
1363
|
+
if (arg.kind === "OperatorReference" && argArity !== undefined && argArity !== expected) {
|
|
1364
|
+
reportOperatorArity(name, argArity);
|
|
1365
|
+
}
|
|
1366
|
+
// Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
|
|
1367
|
+
return argArity === expected && argParams.every((param) => param === 0);
|
|
1368
|
+
};
|
|
1369
|
+
|
|
902
1370
|
const resolveApply = (
|
|
903
1371
|
expression: Expression & { kind: "Apply" },
|
|
904
1372
|
scope: LocalName | undefined,
|
|
905
1373
|
unit: Unit,
|
|
906
1374
|
) => {
|
|
907
|
-
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
908
1375
|
const operator = expression.operator;
|
|
909
1376
|
const callee = findBinding(operator, scope, unit);
|
|
910
|
-
const arity =
|
|
911
|
-
|
|
912
|
-
|
|
1377
|
+
const arity = isInstanceArityReported(expression, scope, unit)
|
|
1378
|
+
? undefined
|
|
1379
|
+
: getSymbolArity(expression, operator, scope, unit);
|
|
1380
|
+
if (arity !== undefined && arity !== expression.args.length) {
|
|
913
1381
|
unit.errors.push({
|
|
914
1382
|
location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
|
|
915
|
-
message: `The operator ${operator} requires ${arity - (
|
|
1383
|
+
message: `The operator ${resolveSynonym(operator)} requires ${arity - (getInstances(operator.split("!"), scope, unit).findLast((instance) => instance !== undefined)?.arity ?? 0)} arguments.`,
|
|
916
1384
|
});
|
|
1385
|
+
}
|
|
1386
|
+
if (arity !== expression.args.length) {
|
|
1387
|
+
emptyExpressions.add(expression);
|
|
917
1388
|
return;
|
|
918
1389
|
}
|
|
919
|
-
const params =
|
|
1390
|
+
const params =
|
|
1391
|
+
callee && (callee.kind === "Definition" ? callee.params : expression.args.map(() => 0));
|
|
920
1392
|
if (params === undefined) {
|
|
921
1393
|
expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
922
1394
|
return;
|
|
923
1395
|
}
|
|
924
|
-
const
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
expression.args
|
|
928
|
-
.
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
}
|
|
959
|
-
return isMatched;
|
|
960
|
-
}
|
|
961
|
-
if (arg.kind === "Subexpression") {
|
|
962
|
-
resolveExpression(arg, scope, unit);
|
|
963
|
-
return true;
|
|
964
|
-
}
|
|
965
|
-
if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
|
|
966
|
-
const selectorIndex = arg.argumentCounts.findIndex((count) => count > 0);
|
|
967
|
-
report(
|
|
968
|
-
arg.selectorRanges?.[selectorIndex],
|
|
969
|
-
`Selector \`${arg.operator.split("!")[selectorIndex]}' should not have argument(s).`,
|
|
970
|
-
);
|
|
971
|
-
return false;
|
|
972
|
-
}
|
|
973
|
-
if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
|
|
974
|
-
report(
|
|
975
|
-
range,
|
|
976
|
-
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
977
|
-
);
|
|
978
|
-
return false;
|
|
979
|
-
}
|
|
980
|
-
const name = arg.kind === "Identifier" ? arg.name : arg.operator;
|
|
981
|
-
const argParams = findBinding(name, scope, unit)?.params ?? [];
|
|
982
|
-
const argArity = getSymbolArity(arg, name, scope, unit);
|
|
983
|
-
if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
|
|
984
|
-
report(
|
|
985
|
-
getSpelledNameRange(arg, name),
|
|
986
|
-
`Expected arity ${expected} but found operator of arity ${argArity}.`,
|
|
987
|
-
);
|
|
988
|
-
}
|
|
989
|
-
return argArity === expected && argParams.every((param) => param === 0);
|
|
990
|
-
})
|
|
991
|
-
.forEach((isMatched, index) => {
|
|
992
|
-
if (!isMatched) {
|
|
993
|
-
report(
|
|
994
|
-
expression.location.range,
|
|
995
|
-
`Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
|
|
996
|
-
);
|
|
997
|
-
}
|
|
998
|
-
});
|
|
1396
|
+
const operatorRange =
|
|
1397
|
+
expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator);
|
|
1398
|
+
const isSelection = operator === SELECTION_NAME;
|
|
1399
|
+
const matches = expression.args.slice(0, params.length).map((arg, index) => {
|
|
1400
|
+
const source = isSelection ? selectionArguments.get(arg) : undefined;
|
|
1401
|
+
return matchArgument(
|
|
1402
|
+
arg,
|
|
1403
|
+
params[index],
|
|
1404
|
+
source?.index ?? index,
|
|
1405
|
+
source?.operator ?? operator,
|
|
1406
|
+
source?.range ?? operatorRange,
|
|
1407
|
+
scope,
|
|
1408
|
+
isSelection ? { ...unit, isGenerated: false } : unit,
|
|
1409
|
+
);
|
|
1410
|
+
});
|
|
1411
|
+
if (
|
|
1412
|
+
isArgumentCountReported(
|
|
1413
|
+
callee,
|
|
1414
|
+
operator,
|
|
1415
|
+
expression.args.length,
|
|
1416
|
+
expression.location.range,
|
|
1417
|
+
unit,
|
|
1418
|
+
)
|
|
1419
|
+
) {
|
|
1420
|
+
return;
|
|
1421
|
+
}
|
|
1422
|
+
matches.forEach((isMatched, index) => {
|
|
1423
|
+
if (!isMatched && expression.location.range) {
|
|
1424
|
+
unit.errors.push({
|
|
1425
|
+
location: formatRange(expression.location.range),
|
|
1426
|
+
message: `Argument number ${index + 1} to operator '${isSelection ? "LAMBDA" : operator}' \nshould be a ${params[index]}-parameter operator.`,
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
999
1430
|
};
|
|
1000
1431
|
|
|
1001
1432
|
const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
@@ -1003,20 +1434,62 @@ const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Un
|
|
|
1003
1434
|
else resolveExpression(arg, scope, unit);
|
|
1004
1435
|
};
|
|
1005
1436
|
|
|
1437
|
+
const matchApplications = (
|
|
1438
|
+
applications: SelectionArguments[],
|
|
1439
|
+
scope: LocalName | undefined,
|
|
1440
|
+
unit: Unit,
|
|
1441
|
+
) =>
|
|
1442
|
+
applications.forEach((application) =>
|
|
1443
|
+
application.args
|
|
1444
|
+
.slice(0, application.params.length)
|
|
1445
|
+
.forEach((arg, index) =>
|
|
1446
|
+
matchArgument(
|
|
1447
|
+
arg,
|
|
1448
|
+
application.params[index],
|
|
1449
|
+
index,
|
|
1450
|
+
application.operator,
|
|
1451
|
+
application.range,
|
|
1452
|
+
scope,
|
|
1453
|
+
unit,
|
|
1454
|
+
),
|
|
1455
|
+
),
|
|
1456
|
+
);
|
|
1457
|
+
|
|
1006
1458
|
const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1007
1459
|
const failed = selectionErrors.get(expression);
|
|
1008
1460
|
if (!failed) return false;
|
|
1009
|
-
failed.
|
|
1010
|
-
unit.errors.push(failed.error);
|
|
1461
|
+
matchApplications(failed.applications, scope, unit);
|
|
1462
|
+
unit.errors.push(findNullError(failed.nulls) ?? failed.error);
|
|
1011
1463
|
return true;
|
|
1012
1464
|
};
|
|
1013
1465
|
|
|
1466
|
+
const resolveLambda = (
|
|
1467
|
+
expression: Expression & { kind: "Lambda" },
|
|
1468
|
+
scope: LocalName | undefined,
|
|
1469
|
+
unit: Unit,
|
|
1470
|
+
) =>
|
|
1471
|
+
resolveExpression(
|
|
1472
|
+
expression.body,
|
|
1473
|
+
bindNames(scope, expression.params, expression.paramRanges, unit),
|
|
1474
|
+
{
|
|
1475
|
+
...unit,
|
|
1476
|
+
labelParams: [...unit.labelParams, ...expression.params],
|
|
1477
|
+
},
|
|
1478
|
+
);
|
|
1479
|
+
|
|
1014
1480
|
const resolveExpression = (
|
|
1015
1481
|
expression: Expression,
|
|
1016
1482
|
scope: LocalName | undefined,
|
|
1017
1483
|
unit: Unit,
|
|
1018
1484
|
): void => {
|
|
1019
1485
|
if (selections.has(expression) && !unit.isGenerated) {
|
|
1486
|
+
const failed = findNullSelection(nullSelections.get(expression));
|
|
1487
|
+
if (failed) {
|
|
1488
|
+
emptyExpressions.add(expression);
|
|
1489
|
+
matchApplications(failed.applications, scope, unit);
|
|
1490
|
+
unit.errors.push(failed.error);
|
|
1491
|
+
return;
|
|
1492
|
+
}
|
|
1020
1493
|
resolveExpression(expression, scope, { ...unit, isGenerated: true });
|
|
1021
1494
|
return;
|
|
1022
1495
|
}
|
|
@@ -1030,18 +1503,51 @@ const resolveExpression = (
|
|
|
1030
1503
|
case "Number":
|
|
1031
1504
|
case "Decimal":
|
|
1032
1505
|
case "String":
|
|
1506
|
+
return;
|
|
1033
1507
|
case "ExceptAt":
|
|
1508
|
+
if (!unit.isInExcept && expression.location.range) {
|
|
1509
|
+
emptyExpressions.add(expression);
|
|
1510
|
+
unit.errors.push({
|
|
1511
|
+
location: formatRange(expression.location.range),
|
|
1512
|
+
message: "@ used where its meaning is not defined.",
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1034
1515
|
return;
|
|
1035
1516
|
case "Identifier": {
|
|
1036
|
-
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
1037
1517
|
const name = expression.name;
|
|
1038
|
-
const arity =
|
|
1039
|
-
|
|
1518
|
+
const arity = isInstanceArityReported(expression, scope, unit)
|
|
1519
|
+
? undefined
|
|
1520
|
+
: getSymbolArity(expression, name, scope, unit);
|
|
1521
|
+
if (arity === 0) {
|
|
1522
|
+
const binding = findBinding(name, scope, unit);
|
|
1523
|
+
const kind = binding?.kind;
|
|
1524
|
+
// SANY's selector chooses no operand of a variable, theorem, step or NEW symbol, unlike of a constant or definition.
|
|
1525
|
+
if (
|
|
1526
|
+
kind === "Variable" ||
|
|
1527
|
+
kind === "Theorem" ||
|
|
1528
|
+
kind?.startsWith("New") ||
|
|
1529
|
+
isFact(name, unit)
|
|
1530
|
+
) {
|
|
1531
|
+
emptyExpressions.add(expression);
|
|
1532
|
+
}
|
|
1533
|
+
const local = lookupLocal(scope, name);
|
|
1534
|
+
if ((local ? local.goal : getGoal(name, unit))?.statement.kind === "AssumeProve") {
|
|
1535
|
+
unit.errors.push({
|
|
1536
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
1537
|
+
message: "ASSUME/PROVE used where an expression is required.",
|
|
1538
|
+
});
|
|
1539
|
+
return;
|
|
1540
|
+
}
|
|
1541
|
+
isArgumentCountReported(binding, name, 0, getSpelledNameRange(expression, name), unit);
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
if (arity !== undefined) {
|
|
1040
1545
|
unit.errors.push({
|
|
1041
1546
|
location: formatRange(getSpelledNameRange(expression, name)),
|
|
1042
|
-
message: `The operator ${name} requires ${arity} arguments.`,
|
|
1547
|
+
message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
|
|
1043
1548
|
});
|
|
1044
1549
|
}
|
|
1550
|
+
emptyExpressions.add(expression);
|
|
1045
1551
|
return;
|
|
1046
1552
|
}
|
|
1047
1553
|
case "OperatorReference":
|
|
@@ -1051,11 +1557,14 @@ const resolveExpression = (
|
|
|
1051
1557
|
resolveApply(expression, scope, unit);
|
|
1052
1558
|
return;
|
|
1053
1559
|
case "Lambda":
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1560
|
+
if (selections.has(expression)) resolveLambda(expression, scope, unit);
|
|
1561
|
+
else if (expression.location.range) {
|
|
1562
|
+
unresolvedExpressions.add(expression);
|
|
1563
|
+
unit.errors.push({
|
|
1564
|
+
location: formatRange(expression.location.range),
|
|
1565
|
+
message: "LAMBDA expression used where an expression is required.",
|
|
1566
|
+
});
|
|
1567
|
+
}
|
|
1059
1568
|
return;
|
|
1060
1569
|
case "And":
|
|
1061
1570
|
case "Or":
|
|
@@ -1078,7 +1587,13 @@ const resolveExpression = (
|
|
|
1078
1587
|
case "Let":
|
|
1079
1588
|
resolveExpression(
|
|
1080
1589
|
expression.body,
|
|
1081
|
-
resolveDefinitions(
|
|
1590
|
+
resolveDefinitions(
|
|
1591
|
+
expression.definitions,
|
|
1592
|
+
scope,
|
|
1593
|
+
unit,
|
|
1594
|
+
expression.instances,
|
|
1595
|
+
expression.recursive,
|
|
1596
|
+
),
|
|
1082
1597
|
unit,
|
|
1083
1598
|
);
|
|
1084
1599
|
return;
|
|
@@ -1146,9 +1661,30 @@ const resolveExpression = (
|
|
|
1146
1661
|
resolveExpression(update.value, scope, { ...unit, isInExcept: true });
|
|
1147
1662
|
});
|
|
1148
1663
|
return;
|
|
1149
|
-
case "FunctionApplication":
|
|
1150
|
-
|
|
1664
|
+
case "FunctionApplication": {
|
|
1665
|
+
const applied = expression.function;
|
|
1666
|
+
const name =
|
|
1667
|
+
applied.kind === "Identifier"
|
|
1668
|
+
? applied.name
|
|
1669
|
+
: applied.kind === "Apply"
|
|
1670
|
+
? applied.operator
|
|
1671
|
+
: undefined;
|
|
1672
|
+
const arity = name === undefined ? undefined : findBinding(name, scope, unit)?.functionArity;
|
|
1673
|
+
resolveExpression(applied, scope, unit);
|
|
1674
|
+
const count = expression.args.length;
|
|
1675
|
+
if (arity === undefined || count < 2 || arity === count || isEmptyExpression(applied)) {
|
|
1676
|
+
resolveAll(expression.args);
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
emptyExpressions.add(expression);
|
|
1680
|
+
if (expression.location.range) {
|
|
1681
|
+
unit.errors.push({
|
|
1682
|
+
location: formatRange(expression.location.range),
|
|
1683
|
+
message: `Function '${name}' is defined with ${arity} parameters, but is applied to ${count} arguments.`,
|
|
1684
|
+
});
|
|
1685
|
+
}
|
|
1151
1686
|
return;
|
|
1687
|
+
}
|
|
1152
1688
|
case "FieldAccess":
|
|
1153
1689
|
resolveExpression(expression.record, scope, unit);
|
|
1154
1690
|
return;
|
|
@@ -1168,34 +1704,366 @@ const resolveExpression = (
|
|
|
1168
1704
|
resolveExpression(expression.body, scope, { ...unit, labelParams: [], labels: new Set() });
|
|
1169
1705
|
checkLabel(expression, scope, unit);
|
|
1170
1706
|
return;
|
|
1171
|
-
case "Subexpression":
|
|
1172
|
-
if (
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1707
|
+
case "Subexpression":
|
|
1708
|
+
if (isSubexpressionReported(expression, scope, unit)) emptyExpressions.add(expression);
|
|
1709
|
+
return;
|
|
1710
|
+
case "Selection":
|
|
1711
|
+
return;
|
|
1712
|
+
}
|
|
1713
|
+
};
|
|
1714
|
+
|
|
1715
|
+
const isPendingSelectionReported = (
|
|
1716
|
+
name: string,
|
|
1717
|
+
args: Expression[],
|
|
1718
|
+
selectorRange: SourceRange | undefined,
|
|
1719
|
+
argumentsRange: SourceRange | undefined,
|
|
1720
|
+
scope: LocalName | undefined,
|
|
1721
|
+
unit: Unit,
|
|
1722
|
+
) => {
|
|
1723
|
+
const pending = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
|
|
1724
|
+
if (!pending?.isPending || !selectorRange || !argumentsRange) return false;
|
|
1725
|
+
if (args.length !== pending.arity) {
|
|
1726
|
+
unit.errors.push({
|
|
1727
|
+
location: formatRange(argumentsRange),
|
|
1728
|
+
message: `The operator ${name} requires ${pending.arity} arguments.`,
|
|
1729
|
+
});
|
|
1730
|
+
return true;
|
|
1731
|
+
}
|
|
1732
|
+
args.forEach((arg) => resolveExpression(arg, scope, unit));
|
|
1733
|
+
unit.errors.push({
|
|
1734
|
+
location: formatRange(selectorRange),
|
|
1735
|
+
message: `Subexpression of \`${name}' used inside the operator's definition.`,
|
|
1736
|
+
});
|
|
1737
|
+
return true;
|
|
1738
|
+
};
|
|
1739
|
+
|
|
1740
|
+
const isSubexpressionReported = (
|
|
1741
|
+
expression: Expression & { kind: "Subexpression" },
|
|
1742
|
+
scope: LocalName | undefined,
|
|
1743
|
+
unit: Unit,
|
|
1744
|
+
) => {
|
|
1745
|
+
if (
|
|
1746
|
+
isInstanceArityReported(expression, scope, unit) ||
|
|
1747
|
+
isSelectionReported(expression, scope, unit)
|
|
1748
|
+
) {
|
|
1749
|
+
return true;
|
|
1750
|
+
}
|
|
1751
|
+
const head = expression.selectors[0];
|
|
1752
|
+
const range = expression.selectorRanges?.[0];
|
|
1753
|
+
const nameCount = expression.selectors.findIndex((selector) => selector.kind !== "Name");
|
|
1754
|
+
const names = expression.selectors
|
|
1755
|
+
.slice(0, nameCount)
|
|
1756
|
+
.flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []));
|
|
1757
|
+
const symbolIndex = names.findIndex(
|
|
1758
|
+
(_name, index) =>
|
|
1759
|
+
index > 0 &&
|
|
1760
|
+
(findSymbol(names.slice(0, index + 1).join("!"), unit)?.kind ?? "Instance") !== "Instance",
|
|
1761
|
+
);
|
|
1762
|
+
const isInstance =
|
|
1763
|
+
names.length > 1 &&
|
|
1764
|
+
(lookupLocal(scope, names[0]) ?? getModuleBinding(names[0], unit, undefined))?.kind ===
|
|
1765
|
+
"Instance";
|
|
1766
|
+
const lastRange = expression.selectorRanges?.[nameCount - 1];
|
|
1767
|
+
if (isInstance && symbolIndex === -1 && range && lastRange) {
|
|
1768
|
+
unit.errors.push({
|
|
1769
|
+
location: formatRange({ module: range.module, start: range.start, end: lastRange.end }),
|
|
1770
|
+
message: `Unknown operator: \`${resolveSynonym(names.join("!"))}'.`,
|
|
1771
|
+
});
|
|
1772
|
+
return true;
|
|
1773
|
+
}
|
|
1774
|
+
const name = names.slice(0, symbolIndex + 1).join("!");
|
|
1775
|
+
const symbol = symbolIndex > 0 ? findSymbol(name, unit) : undefined;
|
|
1776
|
+
const argumentCount = expression.selectors
|
|
1777
|
+
.slice(0, symbolIndex + 1)
|
|
1778
|
+
.flatMap((selector) => ("args" in selector ? selector.args : [])).length;
|
|
1779
|
+
const argumentRange = expression.argumentRanges?.[symbolIndex] ?? expression.location.range;
|
|
1780
|
+
if (symbol && symbol.arity !== argumentCount && argumentRange) {
|
|
1781
|
+
unit.errors.push({
|
|
1782
|
+
location: formatRange(argumentRange),
|
|
1783
|
+
message: `The operator ${resolveSynonym(name)} requires ${symbol.arity} arguments.`,
|
|
1784
|
+
});
|
|
1785
|
+
return true;
|
|
1786
|
+
}
|
|
1787
|
+
if (head.kind === "Name" && expression.selectors[1].kind !== "Name" && range) {
|
|
1788
|
+
const local = lookupLocal(scope, head.name);
|
|
1789
|
+
const binding = local ?? findSymbol(head.name, unit);
|
|
1790
|
+
if ((binding === undefined && !isFact(head.name, unit)) || binding?.kind === "Instance") {
|
|
1791
|
+
unit.errors.push({
|
|
1792
|
+
location: formatRange(range),
|
|
1793
|
+
message: `Unknown operator: \`${resolveSynonym(head.name)}'.`,
|
|
1794
|
+
});
|
|
1795
|
+
return true;
|
|
1796
|
+
}
|
|
1797
|
+
if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.nonFactStep)) {
|
|
1798
|
+
reportUnselectable(head.name, range, unit);
|
|
1799
|
+
return true;
|
|
1177
1800
|
}
|
|
1178
1801
|
}
|
|
1802
|
+
if (
|
|
1803
|
+
head.kind === "Name" &&
|
|
1804
|
+
isPendingSelectionReported(
|
|
1805
|
+
head.name,
|
|
1806
|
+
head.args,
|
|
1807
|
+
range,
|
|
1808
|
+
expression.argumentRanges?.[0] ?? expression.location.range,
|
|
1809
|
+
scope,
|
|
1810
|
+
unit,
|
|
1811
|
+
)
|
|
1812
|
+
) {
|
|
1813
|
+
return true;
|
|
1814
|
+
}
|
|
1815
|
+
expression.selectors
|
|
1816
|
+
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
1817
|
+
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1818
|
+
const selected = checkReference(expression, scope, unit, false);
|
|
1819
|
+
return selected ? selected.node === undefined : false;
|
|
1179
1820
|
};
|
|
1180
1821
|
|
|
1181
|
-
const
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1822
|
+
const reportUnknownModule = (fact: Fact & { kind: "ModuleFact" }, unit: Unit) => {
|
|
1823
|
+
const range = fact.location.range;
|
|
1824
|
+
if (unit.isModuleKnown(fact.name, fact.location) || !range) return;
|
|
1825
|
+
unit.errors.push({
|
|
1826
|
+
location: formatRange({
|
|
1827
|
+
...range,
|
|
1828
|
+
start: { line: range.end.line, column: range.end.column - fact.name.length + 1 },
|
|
1829
|
+
}),
|
|
1830
|
+
message: `Module \`${fact.name}' used without being extended or instantiated.`,
|
|
1831
|
+
});
|
|
1832
|
+
};
|
|
1833
|
+
|
|
1834
|
+
const checkFact = (fact: Fact, reference: Fact, scope: LocalName | undefined, unit: Unit) => {
|
|
1835
|
+
if (fact.kind === "ModuleFact") {
|
|
1836
|
+
reportUnknownModule(fact, unit);
|
|
1837
|
+
return undefined;
|
|
1838
|
+
}
|
|
1839
|
+
if (fact.kind === "ExceptAt" && fact.location.range) {
|
|
1840
|
+
unit.errors.push({
|
|
1841
|
+
location: formatRange(fact.location.range),
|
|
1842
|
+
message: "Unknown operator: `@'.",
|
|
1843
|
+
});
|
|
1844
|
+
return BUILTIN_LOCATION;
|
|
1845
|
+
}
|
|
1846
|
+
const hidden = getHiddenExpressionLocation(fact, reference, scope, unit);
|
|
1847
|
+
if (selections.has(fact)) {
|
|
1848
|
+
resolveExpression(fact, scope, unit);
|
|
1849
|
+
return hidden;
|
|
1850
|
+
}
|
|
1851
|
+
if (isInstanceArityReported(fact, scope, unit)) return hidden;
|
|
1852
|
+
const selected = checkReference(fact, scope, unit, true);
|
|
1853
|
+
if (selected) {
|
|
1854
|
+
const kind = selected.node?.kind;
|
|
1855
|
+
if (kind === undefined) return BUILTIN_LOCATION;
|
|
1856
|
+
return kind === "AssumeProve" || kind === "NewSymbol"
|
|
1857
|
+
? undefined
|
|
1858
|
+
: fact.location.range && formatRange(fact.location.range);
|
|
1859
|
+
}
|
|
1860
|
+
if (fact.kind === "Subexpression") {
|
|
1861
|
+
return isSubexpressionReported(fact, scope, unit) ? BUILTIN_LOCATION : hidden;
|
|
1862
|
+
}
|
|
1863
|
+
const name = fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
|
|
1864
|
+
if (fact.kind === "Identifier" && lookupLocal(scope, name)?.nonFactStep) {
|
|
1865
|
+
unit.errors.push({
|
|
1866
|
+
location: formatRange(getSpelledNameRange(fact, name)),
|
|
1867
|
+
message: "Step number of non-fact used as a fact",
|
|
1868
|
+
});
|
|
1869
|
+
return BUILTIN_LOCATION;
|
|
1870
|
+
}
|
|
1871
|
+
if (
|
|
1872
|
+
!isFact(name, unit) &&
|
|
1873
|
+
!lookupLocal(scope, name)?.goal &&
|
|
1874
|
+
!(fact.kind === "Identifier" && isInstanceName(name, scope, unit))
|
|
1875
|
+
) {
|
|
1876
|
+
resolveExpression(fact, scope, unit);
|
|
1877
|
+
}
|
|
1878
|
+
return hidden;
|
|
1879
|
+
};
|
|
1880
|
+
|
|
1881
|
+
const isDefinitionEntry = (def: Fact, scope: LocalName | undefined, unit: Unit) => {
|
|
1882
|
+
if (def.kind === "ModuleFact") {
|
|
1883
|
+
reportUnknownModule(def, unit);
|
|
1884
|
+
return unit.isModuleKnown(def.name, def.location);
|
|
1885
|
+
}
|
|
1886
|
+
const entryErrors = defEntryErrors.get(def) ?? [];
|
|
1887
|
+
unit.errors.push(
|
|
1888
|
+
...entryErrors.map((error) => ({
|
|
1889
|
+
location: formatSanyLocation(error.location),
|
|
1890
|
+
message: error.message,
|
|
1891
|
+
})),
|
|
1892
|
+
);
|
|
1893
|
+
if (entryErrors.length > 0) return false;
|
|
1894
|
+
const binder = getDefEntryBinder(def);
|
|
1895
|
+
const reference = binder ?? selections.get(def) ?? def;
|
|
1896
|
+
const range = reference.location.range;
|
|
1897
|
+
if (!range) return true;
|
|
1898
|
+
const name =
|
|
1899
|
+
reference.kind === "Identifier"
|
|
1900
|
+
? reference.name
|
|
1901
|
+
: reference.kind === "Apply" || reference.kind === "OperatorReference"
|
|
1902
|
+
? reference.operator
|
|
1903
|
+
: "";
|
|
1904
|
+
const local = lookupLocal(scope, name);
|
|
1905
|
+
const binding = local ?? findSymbol(name, unit) ?? getModuleBinding(name, unit, range);
|
|
1906
|
+
if (binding === undefined && isFact(name, unit)) return true;
|
|
1907
|
+
// Deviation from SANY: Generator#genIdToSelector throws an internal error on the arguments of a name after a prefix, as in DEF A!B(1); tla-ts rejects them as Generator#selectorToNode rejects a selector's arguments in a DEF clause.
|
|
1908
|
+
const isDefined =
|
|
1909
|
+
isReference(reference) &&
|
|
1910
|
+
getSelectors(reference).length > 1 &&
|
|
1911
|
+
(binding === undefined || getSelectorArgumentError(reference, true))
|
|
1912
|
+
? isDefinitionSelected(def, reference, scope, unit)
|
|
1913
|
+
: name === "" || isDefinitionName(name, local, binding, range, unit);
|
|
1914
|
+
if (isDefined) return true;
|
|
1915
|
+
unit.errors.push({
|
|
1916
|
+
location: formatRange((binder ? def : reference).location.range ?? range),
|
|
1917
|
+
message: "DEF clause entry should describe a defined operator.",
|
|
1918
|
+
});
|
|
1919
|
+
return false;
|
|
1920
|
+
};
|
|
1921
|
+
|
|
1922
|
+
const isDefinitionName = (
|
|
1923
|
+
name: string,
|
|
1924
|
+
local: LocalName | undefined,
|
|
1925
|
+
binding: Binding | undefined,
|
|
1926
|
+
range: SourceRange,
|
|
1927
|
+
unit: Unit,
|
|
1928
|
+
) => {
|
|
1929
|
+
if (binding === undefined) {
|
|
1930
|
+
unit.errors.push({
|
|
1931
|
+
location: formatRange(range),
|
|
1932
|
+
message: `Unknown operator: \`${resolveSynonym(name)}'.`,
|
|
1933
|
+
});
|
|
1934
|
+
}
|
|
1935
|
+
if (local?.nonFactStep === "Define") return true;
|
|
1936
|
+
if (local?.nonFactStep) {
|
|
1937
|
+
unit.errors.push({
|
|
1938
|
+
location: formatRange(range),
|
|
1939
|
+
message: "DEF clause entry refers to a non-definition step.",
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
return binding !== undefined && SELECTABLE_KINDS.has(binding.kind) && !isStepName(name);
|
|
1943
|
+
};
|
|
1944
|
+
|
|
1945
|
+
const isDefinitionSelected = (
|
|
1946
|
+
def: Expression,
|
|
1947
|
+
reference: Reference,
|
|
1948
|
+
scope: LocalName | undefined,
|
|
1949
|
+
unit: Unit,
|
|
1950
|
+
) => {
|
|
1951
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
1952
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
1953
|
+
return false;
|
|
1954
|
+
};
|
|
1955
|
+
const argumentError = getSelectorArgumentError(reference, true);
|
|
1956
|
+
if (argumentError?.index === 0) return report(argumentError.range, argumentError.message);
|
|
1957
|
+
const selectors = getSelectors(reference);
|
|
1958
|
+
const ranges = reference.selectorRanges;
|
|
1959
|
+
const failed = selectionErrors.get(def);
|
|
1960
|
+
if (!failed && !selections.has(def)) {
|
|
1961
|
+
const head = selectors[0];
|
|
1962
|
+
const name = head.kind === "Name" ? head.name : "";
|
|
1963
|
+
const local = lookupLocal(scope, name);
|
|
1964
|
+
const binding = local ?? findSymbol(name, unit);
|
|
1965
|
+
if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.nonFactStep) && ranges?.[0]) {
|
|
1966
|
+
reportUnselectable(name, ranges[0], unit);
|
|
1967
|
+
return false;
|
|
1189
1968
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1969
|
+
if (argumentError) return report(argumentError.range, argumentError.message);
|
|
1970
|
+
if (checkReference(reference, scope, unit, false)) return false;
|
|
1971
|
+
const first = ranges?.[0];
|
|
1972
|
+
const last = ranges?.[selectors.findLastIndex((selector) => selector.kind === "Name")];
|
|
1973
|
+
const names = selectors.flatMap((selector) =>
|
|
1974
|
+
selector.kind === "Name" ? [selector.name] : [],
|
|
1975
|
+
);
|
|
1976
|
+
return report(
|
|
1977
|
+
first && last && { ...first, end: last.end },
|
|
1978
|
+
`Unknown operator: \`${resolveSynonym(names.join("!"))}'.`,
|
|
1979
|
+
);
|
|
1980
|
+
}
|
|
1981
|
+
if (argumentError) return report(argumentError.range, argumentError.message);
|
|
1982
|
+
const error = failed
|
|
1983
|
+
? (findNullError(failed.nulls) ?? failed.error)
|
|
1984
|
+
: findNullError(nullSelections.get(def));
|
|
1985
|
+
if (error) {
|
|
1986
|
+
unit.errors.push(error);
|
|
1987
|
+
return false;
|
|
1988
|
+
}
|
|
1989
|
+
return selectors.at(-1)?.kind === "Name" && selectors.at(-2)?.kind !== "Name";
|
|
1990
|
+
};
|
|
1991
|
+
|
|
1992
|
+
const getHiddenExpressionLocation = (
|
|
1993
|
+
fact: Expression,
|
|
1994
|
+
reference: Fact,
|
|
1995
|
+
scope: LocalName | undefined,
|
|
1996
|
+
unit: Unit,
|
|
1997
|
+
) => {
|
|
1998
|
+
const range = fact.location.range && formatRange(fact.location.range);
|
|
1999
|
+
if (selections.has(fact)) {
|
|
2000
|
+
return findNullError(nullSelections.get(fact))
|
|
2001
|
+
? BUILTIN_LOCATION
|
|
2002
|
+
: reference.location.range && formatRange(reference.location.range);
|
|
2003
|
+
}
|
|
2004
|
+
if (selectionErrors.has(fact) || fact.kind === "ExceptAt") return BUILTIN_LOCATION;
|
|
2005
|
+
switch (fact.kind) {
|
|
2006
|
+
case "Number":
|
|
2007
|
+
case "String":
|
|
2008
|
+
case "Decimal":
|
|
2009
|
+
case "Let":
|
|
2010
|
+
case "Label":
|
|
2011
|
+
return undefined;
|
|
2012
|
+
case "Identifier":
|
|
2013
|
+
case "Apply":
|
|
2014
|
+
case "Subexpression": {
|
|
2015
|
+
const first = getSelectors(fact)[0];
|
|
2016
|
+
if (fact.kind === "Subexpression" || first.kind !== "Name") return undefined;
|
|
2017
|
+
const name = fact.kind === "Identifier" ? fact.name : fact.operator;
|
|
2018
|
+
const goal = lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit);
|
|
2019
|
+
if (
|
|
2020
|
+
isFact(name, unit) ||
|
|
2021
|
+
goal ||
|
|
2022
|
+
(isStepName(name) && lookupLocal(scope, name)) ||
|
|
2023
|
+
(fact.kind === "Identifier" && isInstanceName(name, scope, unit))
|
|
2024
|
+
) {
|
|
2025
|
+
return undefined;
|
|
2026
|
+
}
|
|
2027
|
+
const binding = findBinding(name, scope, unit);
|
|
2028
|
+
const count = fact.kind === "Apply" ? fact.args.length : 0;
|
|
2029
|
+
// Deviation from SANY: UseOrHideNode#factCheck crashes on a fact whose operator Generator#generateExpression could not resolve, where tla-ts reports it like a null node.
|
|
2030
|
+
return binding?.arity === count ? range : BUILTIN_LOCATION;
|
|
2031
|
+
}
|
|
2032
|
+
default:
|
|
2033
|
+
return range;
|
|
2034
|
+
}
|
|
2035
|
+
};
|
|
2036
|
+
|
|
2037
|
+
const checkUseOrHide = (
|
|
2038
|
+
statement: UseOrHide | Extract<Proof, { kind: "By" }>,
|
|
2039
|
+
scope: LocalName | undefined,
|
|
2040
|
+
unit: Unit,
|
|
2041
|
+
) => {
|
|
2042
|
+
const references = factReferences.get(statement.facts) ?? statement.facts;
|
|
2043
|
+
const hidden = statement.facts.map((fact, index) =>
|
|
2044
|
+
checkFact(fact, references[index], scope, unit),
|
|
2045
|
+
);
|
|
2046
|
+
const factCount = statement.facts.filter(
|
|
2047
|
+
(fact) => fact.kind !== "ModuleFact" || unit.isModuleKnown(fact.name, fact.location),
|
|
2048
|
+
).length;
|
|
2049
|
+
const defCount = statement.defs.filter((def) => isDefinitionEntry(def, scope, unit)).length;
|
|
2050
|
+
hidden.forEach((location) => {
|
|
2051
|
+
if (location && statement.kind === "Hide") {
|
|
1193
2052
|
unit.errors.push({
|
|
1194
|
-
location
|
|
1195
|
-
message:
|
|
2053
|
+
location,
|
|
2054
|
+
message:
|
|
2055
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1196
2056
|
});
|
|
1197
|
-
}
|
|
2057
|
+
}
|
|
1198
2058
|
});
|
|
2059
|
+
const range = statement.location.range;
|
|
2060
|
+
if (factCount + defCount === 0 && range) {
|
|
2061
|
+
unit.errors.push({
|
|
2062
|
+
location: formatRange(range),
|
|
2063
|
+
message: statement.kind === "By" ? "Empty BY" : "Empty USE or HIDE statement.",
|
|
2064
|
+
});
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
1199
2067
|
|
|
1200
2068
|
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
1201
2069
|
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
@@ -1244,7 +2112,7 @@ const resolveStatement = (
|
|
|
1244
2112
|
|
|
1245
2113
|
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
|
|
1246
2114
|
const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
|
|
1247
|
-
if (proof?.kind === "By")
|
|
2115
|
+
if (proof?.kind === "By") checkUseOrHide(proof, scope, unit);
|
|
1248
2116
|
if (proof?.kind !== "Steps") return;
|
|
1249
2117
|
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
1250
2118
|
};
|
|
@@ -1261,6 +2129,42 @@ const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined,
|
|
|
1261
2129
|
);
|
|
1262
2130
|
};
|
|
1263
2131
|
|
|
2132
|
+
const getStepGoal = (body: StepBody, location: SourceLocation): Goal | undefined => {
|
|
2133
|
+
const createGoal = (keyword: string, statement: Expression) => ({
|
|
2134
|
+
statement,
|
|
2135
|
+
isSuffices: false,
|
|
2136
|
+
isInProof: false,
|
|
2137
|
+
keyword,
|
|
2138
|
+
});
|
|
2139
|
+
switch (body.kind) {
|
|
2140
|
+
case "Have":
|
|
2141
|
+
return createGoal("HAVE", { kind: "Tuple", elements: [body.expression], location });
|
|
2142
|
+
case "Case":
|
|
2143
|
+
return createGoal("CASE", { kind: "Tuple", elements: [body.expression], location });
|
|
2144
|
+
case "Witness":
|
|
2145
|
+
return createGoal("WITNESS", { kind: "Tuple", elements: body.expressions, location });
|
|
2146
|
+
case "Take":
|
|
2147
|
+
return createGoal("TAKE", { kind: "Tuple", elements: getBoundSets(body.bounds), location });
|
|
2148
|
+
case "Pick":
|
|
2149
|
+
return createGoal(
|
|
2150
|
+
"PICK",
|
|
2151
|
+
body.bounds.length > 0
|
|
2152
|
+
? { kind: "Exists", bounds: body.bounds, body: body.body, location }
|
|
2153
|
+
: {
|
|
2154
|
+
kind: "UnboundedQuantifier",
|
|
2155
|
+
quantifier: "Exists",
|
|
2156
|
+
names: body.names,
|
|
2157
|
+
body: body.body,
|
|
2158
|
+
location,
|
|
2159
|
+
},
|
|
2160
|
+
);
|
|
2161
|
+
case "Qed":
|
|
2162
|
+
return createGoal("QED step", { kind: "Tuple", elements: [], location });
|
|
2163
|
+
default:
|
|
2164
|
+
return undefined;
|
|
2165
|
+
}
|
|
2166
|
+
};
|
|
2167
|
+
|
|
1264
2168
|
const resolveStep = (
|
|
1265
2169
|
step: ProofStep,
|
|
1266
2170
|
scope: LocalName | undefined,
|
|
@@ -1271,12 +2175,16 @@ const resolveStep = (
|
|
|
1271
2175
|
step.name === undefined
|
|
1272
2176
|
? unit
|
|
1273
2177
|
: { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
|
|
2178
|
+
const goal = getStepGoal(body, step.location);
|
|
1274
2179
|
const bindStep = (parent: LocalName | undefined): LocalName | undefined =>
|
|
1275
|
-
step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent };
|
|
1276
|
-
const bindNonFactStep = (
|
|
2180
|
+
step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent, goal };
|
|
2181
|
+
const bindNonFactStep = (
|
|
2182
|
+
parent: LocalName | undefined,
|
|
2183
|
+
nonFactStep: LocalName["nonFactStep"],
|
|
2184
|
+
): LocalName | undefined =>
|
|
1277
2185
|
step.name === undefined
|
|
1278
2186
|
? parent
|
|
1279
|
-
: { name: step.name, arity: 0, kind: "Theorem", parent,
|
|
2187
|
+
: { name: step.name, arity: 0, kind: "Theorem", parent, nonFactStep };
|
|
1280
2188
|
switch (body.kind) {
|
|
1281
2189
|
case "Assert": {
|
|
1282
2190
|
const assumed = resolveStatement(body.statement, scope, stepUnit);
|
|
@@ -1286,17 +2194,32 @@ const resolveStep = (
|
|
|
1286
2194
|
resolveProof(step.proof, proofScope, unit);
|
|
1287
2195
|
return body.isSuffices ? assumed : scope;
|
|
1288
2196
|
}
|
|
1289
|
-
const
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
2197
|
+
const assertion = {
|
|
2198
|
+
statement: body.statement,
|
|
2199
|
+
isSuffices: body.isSuffices,
|
|
2200
|
+
isInProof: true,
|
|
2201
|
+
keyword: body.isSuffices && body.statement.kind !== "AssumeProve" ? "SUFFICES" : undefined,
|
|
2202
|
+
};
|
|
2203
|
+
resolveProof(
|
|
2204
|
+
step.proof,
|
|
2205
|
+
{ name, arity: 0, kind: "Theorem", parent: proofScope, goal: assertion },
|
|
2206
|
+
unit,
|
|
2207
|
+
);
|
|
2208
|
+
assertion.isInProof = false;
|
|
2209
|
+
return {
|
|
2210
|
+
name,
|
|
2211
|
+
arity: 0,
|
|
2212
|
+
kind: "Theorem",
|
|
2213
|
+
parent: body.isSuffices ? assumed : scope,
|
|
2214
|
+
goal: assertion,
|
|
2215
|
+
};
|
|
1293
2216
|
}
|
|
1294
2217
|
case "Use":
|
|
1295
2218
|
case "Hide":
|
|
1296
|
-
|
|
1297
|
-
return bindNonFactStep(scope);
|
|
2219
|
+
checkUseOrHide(body, scope, unit);
|
|
2220
|
+
return bindNonFactStep(scope, body.kind);
|
|
1298
2221
|
case "Instance":
|
|
1299
|
-
return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
|
|
2222
|
+
return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit), body.kind);
|
|
1300
2223
|
case "Define":
|
|
1301
2224
|
return bindNonFactStep(
|
|
1302
2225
|
body.definitions.reduce(
|
|
@@ -1306,6 +2229,7 @@ const resolveStep = (
|
|
|
1306
2229
|
: resolveDefinitions([definition], current, unit),
|
|
1307
2230
|
scope,
|
|
1308
2231
|
),
|
|
2232
|
+
body.kind,
|
|
1309
2233
|
);
|
|
1310
2234
|
case "Have":
|
|
1311
2235
|
case "Case":
|
|
@@ -1378,6 +2302,7 @@ export interface ModuleUnit {
|
|
|
1378
2302
|
bound: Symbols;
|
|
1379
2303
|
definitions: Map<string, Definition>;
|
|
1380
2304
|
getKey: (definition: Definition) => string;
|
|
2305
|
+
isModuleKnown: (name: string, location: SourceLocation) => boolean;
|
|
1381
2306
|
instanced: Map<Instance, Symbols>;
|
|
1382
2307
|
facts: Set<string>;
|
|
1383
2308
|
reports: ModuleReport[];
|
|
@@ -1416,18 +2341,94 @@ const checkSubstitutions = (
|
|
|
1416
2341
|
(parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
|
|
1417
2342
|
outer,
|
|
1418
2343
|
);
|
|
2344
|
+
const reportArity = (declaration: Declaration, location: string) =>
|
|
2345
|
+
unit.errors.push({
|
|
2346
|
+
location,
|
|
2347
|
+
message: `An operator must be substituted for symbol '${resolveSynonym(declaration.name)}', and it must have arity ${declaration.arity}.`,
|
|
2348
|
+
});
|
|
1419
2349
|
const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
|
|
1420
|
-
|
|
2350
|
+
const range = expression.location.range;
|
|
2351
|
+
if (declaration.arity === 0) {
|
|
2352
|
+
resolveExpressionArgument(expression, scope, unit);
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
const parenthesized = parenthesizedRanges.get(expression);
|
|
2356
|
+
const reference = parenthesized ? undefined : selections.get(expression);
|
|
2357
|
+
if (reference) {
|
|
2358
|
+
const arity = expression.kind === "Lambda" ? expression.params.length : 0;
|
|
2359
|
+
const failed = findNullError(nullSelections.get(expression));
|
|
2360
|
+
if (expression.kind === "Lambda") resolveExpression(expression, scope, unit);
|
|
2361
|
+
else if (failed) unit.errors.push(failed);
|
|
2362
|
+
if (!failed && arity !== declaration.arity && reference.location.range) {
|
|
2363
|
+
unit.errors.push({
|
|
2364
|
+
location: formatRange(reference.location.range),
|
|
2365
|
+
message: `Expected arity ${declaration.arity} but found operator of arity ${arity}.`,
|
|
2366
|
+
});
|
|
2367
|
+
}
|
|
2368
|
+
if (failed || arity !== declaration.arity) reportArity(declaration, BUILTIN_LOCATION);
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
if (expression.kind === "Subexpression" && !parenthesized) {
|
|
1421
2372
|
resolveExpression(expression, scope, unit);
|
|
2373
|
+
reportArity(declaration, BUILTIN_LOCATION);
|
|
2374
|
+
return;
|
|
2375
|
+
}
|
|
2376
|
+
if (expression.kind === "Lambda" && !parenthesized) {
|
|
2377
|
+
resolveLambda(expression, scope, unit);
|
|
2378
|
+
if (expression.params.length !== declaration.arity && range) {
|
|
2379
|
+
reportArity(declaration, formatRange(range));
|
|
2380
|
+
}
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
if (expression.kind === "OperatorReference" && !parenthesized) {
|
|
2384
|
+
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
2385
|
+
if (arity === undefined) reportArity(declaration, BUILTIN_LOCATION);
|
|
2386
|
+
else if (arity !== declaration.arity && range) reportArity(declaration, formatRange(range));
|
|
2387
|
+
return;
|
|
2388
|
+
}
|
|
2389
|
+
if (expression.kind !== "Identifier" || parenthesized) {
|
|
2390
|
+
const errorRange = parenthesized ?? range;
|
|
2391
|
+
if (errorRange) {
|
|
2392
|
+
unit.errors.push({
|
|
2393
|
+
location: formatRange(errorRange),
|
|
2394
|
+
message: `Arity ${declaration.arity} operator (not an expression) is expected \nto substitute for CONSTANT '${resolveSynonym(declaration.name)}'.`,
|
|
2395
|
+
});
|
|
2396
|
+
}
|
|
2397
|
+
reportArity(declaration, BUILTIN_LOCATION);
|
|
1422
2398
|
return;
|
|
1423
2399
|
}
|
|
1424
2400
|
const arity = getSymbolArity(expression, expression.name, scope, unit);
|
|
1425
2401
|
if (arity !== undefined && arity !== declaration.arity) {
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
2402
|
+
unit.errors.push({
|
|
2403
|
+
location: formatRange(getSpelledNameRange(expression, expression.name)),
|
|
2404
|
+
message: `Expected arity ${declaration.arity} but found operator of arity ${arity}.`,
|
|
2405
|
+
});
|
|
1429
2406
|
}
|
|
2407
|
+
if (arity !== declaration.arity) reportArity(declaration, BUILTIN_LOCATION);
|
|
1430
2408
|
};
|
|
2409
|
+
const instanceRange = instance.location.range;
|
|
2410
|
+
const isFound = (declaration: Declaration) =>
|
|
2411
|
+
lookupLocal(scope, declaration.name) !== undefined ||
|
|
2412
|
+
findSymbol(declaration.name, unit) !== undefined;
|
|
2413
|
+
const implicit = declarations.filter(isFound).map((declaration) => {
|
|
2414
|
+
const expression: Expression & { kind: "Identifier" } = {
|
|
2415
|
+
kind: "Identifier",
|
|
2416
|
+
name: declaration.name,
|
|
2417
|
+
location: instance.location,
|
|
2418
|
+
};
|
|
2419
|
+
return { declaration, expression };
|
|
2420
|
+
});
|
|
2421
|
+
implicit
|
|
2422
|
+
.filter((item) => item.declaration.arity === 0)
|
|
2423
|
+
.forEach((item) => {
|
|
2424
|
+
const arity = getSymbolArity(item.expression, item.declaration.name, scope, unit);
|
|
2425
|
+
if (arity && instanceRange) {
|
|
2426
|
+
unit.errors.push({
|
|
2427
|
+
location: formatRange(instanceRange),
|
|
2428
|
+
message: `Wrong number of arguments (0) given to operator '${resolveSynonym(item.declaration.name)}', \nwhich requires ${arity} arguments.`,
|
|
2429
|
+
});
|
|
2430
|
+
}
|
|
2431
|
+
});
|
|
1431
2432
|
const substitutions = new Map<string, Expression>();
|
|
1432
2433
|
instance.substitutions.forEach((substitution) => {
|
|
1433
2434
|
const name = substitution.name;
|
|
@@ -1450,30 +2451,29 @@ const checkSubstitutions = (
|
|
|
1450
2451
|
});
|
|
1451
2452
|
}
|
|
1452
2453
|
});
|
|
1453
|
-
const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
|
|
1454
|
-
const isMissing = (declaration: Declaration) =>
|
|
1455
|
-
!lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, unit);
|
|
1456
2454
|
implicit
|
|
1457
|
-
.filter((
|
|
2455
|
+
.filter((item) => !substitutions.has(item.declaration.name))
|
|
2456
|
+
.forEach((item) => {
|
|
2457
|
+
const arity =
|
|
2458
|
+
item.declaration.arity === 0
|
|
2459
|
+
? 0
|
|
2460
|
+
: getSymbolArity(item.expression, item.declaration.name, scope, unit);
|
|
2461
|
+
if (arity !== item.declaration.arity && instanceRange) {
|
|
2462
|
+
reportArity(item.declaration, formatRange(instanceRange));
|
|
2463
|
+
}
|
|
2464
|
+
substitutions.set(item.declaration.name, item.expression);
|
|
2465
|
+
});
|
|
2466
|
+
declarations
|
|
2467
|
+
.filter((declaration) => !substitutions.has(declaration.name))
|
|
1458
2468
|
.forEach((declaration) => {
|
|
1459
|
-
const
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
2469
|
+
const declared = declaration.location.range;
|
|
2470
|
+
if (instanceRange && declared) {
|
|
2471
|
+
unit.errors.push({
|
|
2472
|
+
location: formatRange(instanceRange),
|
|
2473
|
+
message: `Substitution missing for symbol ${resolveSynonym(declaration.name)} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
|
|
2474
|
+
});
|
|
2475
|
+
}
|
|
1466
2476
|
});
|
|
1467
|
-
implicit.filter(isMissing).forEach((declaration) => {
|
|
1468
|
-
const range = instance.location.range;
|
|
1469
|
-
const declared = declaration.location.range;
|
|
1470
|
-
if (range && declared) {
|
|
1471
|
-
unit.errors.push({
|
|
1472
|
-
location: formatRange(range),
|
|
1473
|
-
message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
|
|
1474
|
-
});
|
|
1475
|
-
}
|
|
1476
|
-
});
|
|
1477
2477
|
return substitutions;
|
|
1478
2478
|
};
|
|
1479
2479
|
|
|
@@ -1481,7 +2481,7 @@ const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit:
|
|
|
1481
2481
|
const instanced = unit.getInstance(instance);
|
|
1482
2482
|
checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
|
|
1483
2483
|
return [...instanced.symbols].reduce(
|
|
1484
|
-
(parent,
|
|
2484
|
+
(parent, entry) => bindName(parent, entry[0], entry[1], unit),
|
|
1485
2485
|
scope,
|
|
1486
2486
|
);
|
|
1487
2487
|
};
|
|
@@ -1493,6 +2493,12 @@ export const resolveSubstitutions = (
|
|
|
1493
2493
|
unit: ModuleUnit,
|
|
1494
2494
|
) => {
|
|
1495
2495
|
const imported = createSymbols(module, unit.imported);
|
|
2496
|
+
const positions = new Map(
|
|
2497
|
+
[...module.constants, ...module.variables].map((declaration) => [
|
|
2498
|
+
declaration.name,
|
|
2499
|
+
declaration.location,
|
|
2500
|
+
]),
|
|
2501
|
+
);
|
|
1496
2502
|
const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
|
|
1497
2503
|
unit.reports.push(report);
|
|
1498
2504
|
return checkSubstitutions(instance, declarations, module.name, undefined, {
|
|
@@ -1500,12 +2506,16 @@ export const resolveSubstitutions = (
|
|
|
1500
2506
|
getInstance: unit.getInstance,
|
|
1501
2507
|
definitions: unit.definitions,
|
|
1502
2508
|
getKey: unit.getKey,
|
|
2509
|
+
isModuleKnown: unit.isModuleKnown,
|
|
1503
2510
|
location: instance.location,
|
|
1504
2511
|
self: undefined,
|
|
1505
2512
|
facts: new Set(),
|
|
1506
2513
|
goals: new Map(),
|
|
1507
2514
|
bindings: new Map(
|
|
1508
|
-
[...imported].map((
|
|
2515
|
+
[...imported].map((entry) => [
|
|
2516
|
+
entry[0],
|
|
2517
|
+
{ ...entry[1], position: positions.get(entry[0]) ?? MODULE_START },
|
|
2518
|
+
]),
|
|
1509
2519
|
),
|
|
1510
2520
|
errors: report.errors,
|
|
1511
2521
|
warnings: report.warnings,
|
|
@@ -1535,13 +2545,7 @@ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError
|
|
|
1535
2545
|
}
|
|
1536
2546
|
};
|
|
1537
2547
|
units
|
|
1538
|
-
.toSorted((left, right) =>
|
|
1539
|
-
isBefore(left.location, right.location)
|
|
1540
|
-
? -1
|
|
1541
|
-
: isBefore(right.location, left.location)
|
|
1542
|
-
? 1
|
|
1543
|
-
: 0,
|
|
1544
|
-
)
|
|
2548
|
+
.toSorted((left, right) => compareLocations(left.location, right.location))
|
|
1545
2549
|
.forEach((unit) => {
|
|
1546
2550
|
const reported: SanyError[] = [];
|
|
1547
2551
|
const warned: SanyError[] = [];
|
|
@@ -1561,21 +2565,27 @@ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError
|
|
|
1561
2565
|
|
|
1562
2566
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
1563
2567
|
const symbols = createSymbols(module, unit.imported);
|
|
1564
|
-
const facts = new Set([
|
|
1565
|
-
...unit.facts,
|
|
1566
|
-
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
1567
|
-
]);
|
|
1568
2568
|
const goals = new Map(
|
|
1569
2569
|
module.theorems.flatMap((theorem): [string, Goal][] =>
|
|
1570
2570
|
theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
|
|
1571
|
-
? [
|
|
2571
|
+
? [
|
|
2572
|
+
[
|
|
2573
|
+
theorem.name,
|
|
2574
|
+
{
|
|
2575
|
+
statement: theorem.statement,
|
|
2576
|
+
isSuffices: false,
|
|
2577
|
+
isInProof: false,
|
|
2578
|
+
keyword: undefined,
|
|
2579
|
+
},
|
|
2580
|
+
],
|
|
2581
|
+
]
|
|
1572
2582
|
: [],
|
|
1573
2583
|
),
|
|
1574
2584
|
);
|
|
1575
2585
|
const bindings = new Map<string, ModuleBinding>(
|
|
1576
|
-
[...CORE_SYMBOLS, ...unit.bound].map((
|
|
1577
|
-
|
|
1578
|
-
{ ...
|
|
2586
|
+
[...CORE_SYMBOLS, ...unit.bound].map((entry) => [
|
|
2587
|
+
entry[0],
|
|
2588
|
+
{ ...entry[1], position: MODULE_START },
|
|
1579
2589
|
]),
|
|
1580
2590
|
);
|
|
1581
2591
|
const recursive = new Set<Binding>();
|
|
@@ -1593,9 +2603,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1593
2603
|
getInstance: unit.getInstance,
|
|
1594
2604
|
definitions: unit.definitions,
|
|
1595
2605
|
getKey: unit.getKey,
|
|
2606
|
+
isModuleKnown: unit.isModuleKnown,
|
|
1596
2607
|
location,
|
|
1597
2608
|
self,
|
|
1598
|
-
facts,
|
|
2609
|
+
facts: unit.facts,
|
|
1599
2610
|
goals,
|
|
1600
2611
|
bindings,
|
|
1601
2612
|
errors,
|
|
@@ -1620,6 +2631,37 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1620
2631
|
);
|
|
1621
2632
|
return collectSemanticErrors(
|
|
1622
2633
|
[
|
|
2634
|
+
...module.units.flatMap((item): SemanticUnit[] => {
|
|
2635
|
+
const section =
|
|
2636
|
+
item.kind === "Variables"
|
|
2637
|
+
? { type: "A VARIABLE declaration", location: item.location }
|
|
2638
|
+
: item.kind === "Constants"
|
|
2639
|
+
? { type: "A declaration", location: item.location }
|
|
2640
|
+
: item.kind === "Module"
|
|
2641
|
+
? { type: "A MODULE ", location: item.module.location }
|
|
2642
|
+
: item.kind === "Theorem"
|
|
2643
|
+
? { type: "A THEOREM", location: item.theorem.location }
|
|
2644
|
+
: item.kind === "Assumption"
|
|
2645
|
+
? { type: "An ASSUME", location: item.assumption.location }
|
|
2646
|
+
: item.kind === "Use"
|
|
2647
|
+
? { type: "A USE or HIDE", location: item.use.location }
|
|
2648
|
+
: undefined;
|
|
2649
|
+
const range = section?.location.range;
|
|
2650
|
+
if (!section || !range) return [];
|
|
2651
|
+
return [
|
|
2652
|
+
{
|
|
2653
|
+
location: section.location,
|
|
2654
|
+
check: (errors) => {
|
|
2655
|
+
if (declarations.some((declared) => declared.binding?.isPending)) {
|
|
2656
|
+
errors.push({
|
|
2657
|
+
location: formatRange(range),
|
|
2658
|
+
message: `${section.type} may not appear within a recursive definition section.`,
|
|
2659
|
+
});
|
|
2660
|
+
}
|
|
2661
|
+
},
|
|
2662
|
+
},
|
|
2663
|
+
];
|
|
2664
|
+
}),
|
|
1623
2665
|
...unit.reports.map((report) => ({
|
|
1624
2666
|
location: report.location,
|
|
1625
2667
|
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
@@ -1636,6 +2678,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1636
2678
|
const binding: ModuleBinding = {
|
|
1637
2679
|
kind: "Definition",
|
|
1638
2680
|
arity: declaration.arity,
|
|
2681
|
+
params: Array(declaration.arity).fill(0),
|
|
1639
2682
|
range: declaration.location.range,
|
|
1640
2683
|
position: declaration.location,
|
|
1641
2684
|
isPending: true,
|
|
@@ -1701,7 +2744,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1701
2744
|
),
|
|
1702
2745
|
...module.uses.map((use) =>
|
|
1703
2746
|
createCheck(use.location, undefined, false, (inner) =>
|
|
1704
|
-
|
|
2747
|
+
checkUseOrHide(use, undefined, inner),
|
|
1705
2748
|
),
|
|
1706
2749
|
),
|
|
1707
2750
|
{
|
|
@@ -1712,7 +2755,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1712
2755
|
if (range && (!item.binding || item.binding.isPending)) {
|
|
1713
2756
|
errors.push({
|
|
1714
2757
|
location: formatRange(range),
|
|
1715
|
-
message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
|
|
2758
|
+
message: `Symbol ${resolveSynonym(item.declaration.name)} declared in RECURSIVE statement but not defined.`,
|
|
1716
2759
|
});
|
|
1717
2760
|
}
|
|
1718
2761
|
}),
|