tla-ts 0.0.10-dev.c92e6a4ea → 0.0.10-dev.d166e0ae9
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 +54 -54
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +13 -3
- package/dist/index.d.ts +13 -3
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +68 -68
- package/dist/prove.js +78 -78
- package/dist/run.cjs +42 -42
- package/dist/run.js +44 -44
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +45 -43
- package/dist/transfer.js +45 -43
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +1 -1
- package/src/ast.ts +11 -1
- package/src/bags.ts +50 -29
- package/src/builtins.ts +4 -3
- package/src/check-failure.ts +9 -6
- package/src/check-trace.ts +116 -64
- package/src/check.ts +356 -173
- package/src/cli/tlc-output.ts +1 -0
- package/src/cli/tlc.ts +4 -3
- package/src/cli/xml-exporter.ts +1 -1
- package/src/codegen.ts +34 -13
- package/src/coverage.ts +3 -0
- package/src/dependencies.ts +61 -13
- package/src/dfid.ts +22 -10
- package/src/environment.ts +71 -35
- package/src/errors.ts +7 -0
- package/src/evaluate.ts +149 -54
- package/src/exit-codes.ts +29 -15
- package/src/instances.ts +16 -6
- package/src/java-hash.ts +14 -0
- package/src/java-overrides.ts +2 -2
- package/src/levels.ts +186 -97
- package/src/liveness-checker.ts +131 -83
- package/src/liveness.ts +52 -11
- package/src/obligations/index.ts +1 -1
- package/src/obligations/normalize.ts +1 -4
- package/src/obligations/printer.ts +5 -5
- package/src/obligations/source.ts +2 -0
- package/src/parser.ts +115 -145
- package/src/prover/isabelle/isar/attrib.ts +4 -4
- package/src/prover/isabelle/isar/rule-cases.ts +8 -6
- package/src/prover/isabelle/provers/clasimp.ts +1 -1
- package/src/prover/isabelle/provers/hypsubst.ts +2 -2
- package/src/prover/isabelle/pure/rewriter.ts +3 -3
- package/src/prover/isabelle/pure/term.ts +3 -3
- package/src/prover/prep/md5.ts +26 -29
- package/src/prover/smt/rewrite.ts +2 -7
- package/src/prover/smt/simplify.ts +1 -1
- package/src/prover/smt/solver/arith-rewriter.ts +11 -3
- package/src/prover/smt/solver/array-rewriter.ts +12 -2
- package/src/prover/smt/solver/asserted-formulas.ts +169 -58
- package/src/prover/smt/solver/ast-lt.ts +1 -1
- package/src/prover/smt/solver/ast.ts +63 -14
- package/src/prover/smt/solver/auf-solver.ts +23 -12
- package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
- package/src/prover/smt/solver/blast-term-ite.ts +47 -0
- package/src/prover/smt/solver/bound-simplifier.ts +11 -13
- package/src/prover/smt/solver/bv-decl-plugin.ts +13 -10
- package/src/prover/smt/solver/bv-rewriter.ts +1 -1
- package/src/prover/smt/solver/card2bv.ts +35 -18
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -24
- package/src/prover/smt/solver/context.ts +67 -13
- package/src/prover/smt/solver/ctx-simplify.ts +1 -2
- package/src/prover/smt/solver/datatype-decl-plugin.ts +15 -7
- package/src/prover/smt/solver/dependent-expr-state.ts +26 -15
- package/src/prover/smt/solver/diff-neq.ts +1 -2
- package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
- package/src/prover/smt/solver/elim-bounds.ts +89 -0
- package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +5 -8
- package/src/prover/smt/solver/euf/arith-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/array-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/bv-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/egraph.ts +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 +148 -39
- package/src/prover/smt/solver/fix-dl-var.ts +1 -2
- 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/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/logic-tactics.ts +9 -2
- package/src/prover/smt/solver/lp/lp-settings.ts +5 -1
- package/src/prover/smt/solver/lp/nla-core.ts +1 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +2 -2
- package/src/prover/smt/solver/lp/nra-solver.ts +45 -39
- package/src/prover/smt/solver/lp/pdd.ts +2 -3
- package/src/prover/smt/solver/lp/union-find.ts +1 -1
- package/src/prover/smt/solver/mam-compiler.ts +1 -1
- package/src/prover/smt/solver/maxcore.ts +2 -1
- package/src/prover/smt/solver/maxsmt-base.ts +1 -0
- package/src/prover/smt/solver/model-based-opt.ts +1 -1
- package/src/prover/smt/solver/model-checker.ts +1 -1
- package/src/prover/smt/solver/model-evaluator.ts +2 -0
- package/src/prover/smt/solver/model-generator.ts +85 -43
- package/src/prover/smt/solver/mpf.ts +2 -4
- package/src/prover/smt/solver/mus.ts +2 -3
- package/src/prover/smt/solver/muz/dl-context.ts +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 +1 -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/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/poly-rewriter.ts +75 -18
- package/src/prover/smt/solver/probes.ts +88 -23
- package/src/prover/smt/solver/proto-model.ts +8 -8
- package/src/prover/smt/solver/push-app-ite.ts +37 -0
- package/src/prover/smt/solver/qe-arith-plugin.ts +2 -4
- package/src/prover/smt/solver/qe-lite.ts +2 -2
- package/src/prover/smt/solver/qe.ts +3 -2
- package/src/prover/smt/solver/qsat.ts +23 -8
- package/src/prover/smt/solver/quantifier-manager.ts +14 -9
- package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
- package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
- package/src/prover/smt/solver/rewriter.ts +31 -25
- package/src/prover/smt/solver/rlimit.ts +4 -0
- package/src/prover/smt/solver/sat/config.ts +1 -1
- package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +81 -28
- package/src/prover/smt/solver/sat/model-converter.ts +1 -1
- package/src/prover/smt/solver/sat/pb-solver.ts +1 -1
- package/src/prover/smt/solver/sat/sat-tactic.ts +17 -7
- package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
- package/src/prover/smt/solver/sat/simplifier.ts +1 -1
- package/src/prover/smt/solver/sat/solver.ts +25 -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 +96 -10
- package/src/prover/smt/solver/smtlib-parser.ts +589 -224
- package/src/prover/smt/solver/smtlib-script.ts +674 -195
- package/src/prover/smt/solver/solve-eqs.ts +36 -37
- package/src/prover/smt/solver/solver.ts +18 -3
- package/src/prover/smt/solver/tactic.ts +332 -189
- package/src/prover/smt/solver/theory-bv.ts +13 -3
- package/src/prover/smt/solver/theory-fpa.ts +1 -1
- package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
- package/src/prover/smt/solver/theory-lra-internalize.ts +2 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +11 -3
- package/src/prover/smt/solver/theory-lra.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/tseitin-cnf.ts +1 -0
- package/src/prover/tlapm/cache.ts +8 -8
- package/src/prover/zenon/index-table.ts +5 -2
- package/src/prover/zenon/isar-case.ts +1 -1
- package/src/prover/zenon/isar.ts +2 -2
- package/src/prover/zenon/phrase.ts +1 -1
- package/src/resolve.ts +1071 -251
- package/src/runtime.ts +5 -1
- package/src/sany/builtins.ts +10 -10
- package/src/sany/generation.ts +2 -13
- package/src/sany/generator-definitions.ts +3 -1
- package/src/sany/generator-expressions.ts +38 -30
- package/src/sany/generator-proofs.ts +6 -3
- package/src/sany/generator.ts +2 -1
- package/src/sany/lexer.ts +3 -3
- package/src/sany/modules.ts +4 -2
- package/src/sany/nodes.ts +1 -1
- package/src/sany/selectors.ts +11 -6
- package/src/sany/xml.ts +2 -3
- package/src/search.ts +197 -117
- package/src/simulate.ts +165 -102
- package/src/spec-description.ts +8 -6
- package/src/standard-modules.ts +56 -22
- package/src/subexpressions.ts +518 -123
- package/src/successors.ts +10 -3
- package/src/symmetry.ts +2 -1
- package/src/tlc/jar-data.ts +10 -0
- package/src/tlc/messages.ts +3 -3
- package/src/tlc/random.ts +2 -1
- package/src/tlc/replay.ts +27 -14
- package/src/tlc/tlc-operators.ts +10 -8
- package/src/tlc/tlcext-operators.ts +26 -10
- package/src/tlc/trace-spec.ts +9 -22
- package/src/typed-tier.ts +44 -27
- package/src/typescript/types.ts +2 -2
- package/src/values.ts +135 -78
package/src/resolve.ts
CHANGED
|
@@ -18,9 +18,13 @@ 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 { parenthesizedRanges } from "./parser";
|
|
27
|
+
import { resolveSynonym } from "./tlc/jar-data";
|
|
24
28
|
|
|
25
29
|
export type SymbolKind =
|
|
26
30
|
| "Builtin"
|
|
@@ -49,6 +53,7 @@ interface Goal {
|
|
|
49
53
|
statement: Expression | AssumeProve;
|
|
50
54
|
isSuffices: boolean;
|
|
51
55
|
isInProof: boolean;
|
|
56
|
+
keyword: string | undefined;
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
interface LocalName extends Binding {
|
|
@@ -56,7 +61,7 @@ interface LocalName extends Binding {
|
|
|
56
61
|
parent: LocalName | undefined;
|
|
57
62
|
goal?: Goal;
|
|
58
63
|
isPending?: boolean;
|
|
59
|
-
|
|
64
|
+
nonFactStep?: "Use" | "Hide" | "Instance" | "Define";
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
interface ModuleBinding extends Binding {
|
|
@@ -69,6 +74,7 @@ interface Unit {
|
|
|
69
74
|
getInstance: (instance: Instance) => InstancedModule;
|
|
70
75
|
definitions: Map<string, Definition>;
|
|
71
76
|
getKey: (definition: Definition) => string;
|
|
77
|
+
isModuleKnown: (name: string, location: SourceLocation) => boolean;
|
|
72
78
|
location: SourceLocation;
|
|
73
79
|
self: Definition | undefined;
|
|
74
80
|
facts: Set<string>;
|
|
@@ -84,8 +90,16 @@ interface Unit {
|
|
|
84
90
|
isGenerated: boolean;
|
|
85
91
|
}
|
|
86
92
|
|
|
87
|
-
interface
|
|
93
|
+
export interface SelectionArguments {
|
|
94
|
+
operator: string;
|
|
95
|
+
range: SourceRange | undefined;
|
|
88
96
|
args: Expression[];
|
|
97
|
+
params: number[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface SelectionError {
|
|
101
|
+
applications: SelectionArguments[];
|
|
102
|
+
nulls: NullSelection[];
|
|
89
103
|
error: SanyError;
|
|
90
104
|
}
|
|
91
105
|
|
|
@@ -99,8 +113,39 @@ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" }
|
|
|
99
113
|
|
|
100
114
|
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
101
115
|
|
|
102
|
-
export const
|
|
116
|
+
export const SELECTION_NAME = "$subexpression";
|
|
117
|
+
|
|
118
|
+
export interface NullSelection {
|
|
119
|
+
node: Expression;
|
|
120
|
+
error: SanyError;
|
|
121
|
+
isEmpty: boolean;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface SelectionArgument {
|
|
125
|
+
operator: string;
|
|
126
|
+
index: number;
|
|
127
|
+
range: SourceRange | undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export const selections = new WeakMap<Expression, Expression>();
|
|
103
131
|
export const selectionErrors = new WeakMap<Expression, SelectionError>();
|
|
132
|
+
export const operatorSelectionErrors = new WeakMap<Expression, SanyError>();
|
|
133
|
+
export const factReferences = new WeakMap<Fact[], Fact[]>();
|
|
134
|
+
export const selectionArguments = new WeakMap<Expression, SelectionArgument>();
|
|
135
|
+
export const nullSelections = new WeakMap<Expression, NullSelection[]>();
|
|
136
|
+
const unresolvedExpressions = new WeakSet<Expression>();
|
|
137
|
+
const emptyExpressions = new WeakSet<Expression>();
|
|
138
|
+
|
|
139
|
+
// SANY's failed selection is an operator application with no operands.
|
|
140
|
+
const isEmptyExpression = (expression: Expression): boolean =>
|
|
141
|
+
emptyExpressions.has(expression) || findNullError(nullSelections.get(expression)) !== undefined;
|
|
142
|
+
|
|
143
|
+
const findNullError = (nulls: NullSelection[] | undefined) =>
|
|
144
|
+
nulls?.find((selection) =>
|
|
145
|
+
selection.isEmpty
|
|
146
|
+
? isEmptyExpression(selection.node)
|
|
147
|
+
: unresolvedExpressions.has(selection.node),
|
|
148
|
+
)?.error;
|
|
104
149
|
|
|
105
150
|
const renamedArities = new WeakMap<Expression, number>();
|
|
106
151
|
|
|
@@ -111,13 +156,20 @@ const isKey = (name: string) => {
|
|
|
111
156
|
return index > 0 && index < name.length - 1;
|
|
112
157
|
};
|
|
113
158
|
|
|
114
|
-
const CORE_SYMBOLS: Symbols = new Map(
|
|
115
|
-
["~>", "-+->", "\\cdot"].map((name)
|
|
116
|
-
|
|
159
|
+
const CORE_SYMBOLS: Symbols = new Map([
|
|
160
|
+
...["~>", "-+->", "\\cdot", "\\X"].map((name): [string, ModuleSymbol] => [
|
|
161
|
+
name,
|
|
162
|
+
{ key: name, arity: 2, kind: "Builtin" },
|
|
163
|
+
]),
|
|
164
|
+
["'", { key: "'", arity: 1, kind: "Builtin" }],
|
|
165
|
+
]);
|
|
117
166
|
|
|
118
167
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
119
168
|
left.line < right.line || (left.line === right.line && left.column < right.column);
|
|
120
169
|
|
|
170
|
+
export const compareLocations = (left: SourceLocation, right: SourceLocation) =>
|
|
171
|
+
isBefore(left, right) ? -1 : isBefore(right, left) ? 1 : 0;
|
|
172
|
+
|
|
121
173
|
export const getArity = (definition: Definition) =>
|
|
122
174
|
definition.kind === "Operator" ? definition.params.length : 0;
|
|
123
175
|
|
|
@@ -157,7 +209,7 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
|
|
|
157
209
|
if (current.kind === "Builtin") {
|
|
158
210
|
unit.errors.push({
|
|
159
211
|
location,
|
|
160
|
-
message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
|
|
212
|
+
message: `Symbol ${resolveSynonym(name)} is a built-in operator, and cannot be redefined.`,
|
|
161
213
|
});
|
|
162
214
|
} else if (
|
|
163
215
|
added.kind === "Parameter" ||
|
|
@@ -166,12 +218,12 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
|
|
|
166
218
|
) {
|
|
167
219
|
unit.errors.push({
|
|
168
220
|
location,
|
|
169
|
-
message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
221
|
+
message: `Multiply-defined symbol '${resolveSynonym(name)}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
170
222
|
});
|
|
171
223
|
} else {
|
|
172
224
|
unit.warnings.push({
|
|
173
225
|
location,
|
|
174
|
-
message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
|
|
226
|
+
message: `Multiple declarations or definitions for symbol ${resolveSynonym(name)}. \nThis duplicates the one at ${existing}.`,
|
|
175
227
|
});
|
|
176
228
|
}
|
|
177
229
|
return false;
|
|
@@ -233,23 +285,38 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
|
233
285
|
const isVisible =
|
|
234
286
|
definition !== undefined &&
|
|
235
287
|
(definition === unit.self
|
|
236
|
-
? definition.
|
|
237
|
-
: definition.isRecursive
|
|
288
|
+
? definition.isRecursive
|
|
289
|
+
: definition.isRecursive
|
|
290
|
+
? getModuleBinding(name, unit, undefined) !== undefined
|
|
291
|
+
: isBefore(definition.location, unit.location));
|
|
238
292
|
return isVisible ? definition : undefined;
|
|
239
293
|
};
|
|
240
294
|
|
|
241
295
|
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
242
296
|
const definition = getVisibleDefinition(name, unit);
|
|
243
|
-
|
|
244
|
-
?
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
297
|
+
if (definition) {
|
|
298
|
+
const declared = definition.isRecursive ? unit.bindings.get(name) : undefined;
|
|
299
|
+
return {
|
|
300
|
+
key: unit.getKey(definition),
|
|
301
|
+
arity: declared?.arity ?? getArity(definition),
|
|
302
|
+
params: declared?.params ?? getParamArities(definition),
|
|
303
|
+
kind: "Definition",
|
|
304
|
+
range: definition.location.range,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
const binding = getModuleBinding(name, unit, undefined);
|
|
308
|
+
if (binding?.isPending) {
|
|
309
|
+
return { key: name, arity: binding.arity, params: binding.params, kind: binding.kind };
|
|
310
|
+
}
|
|
311
|
+
return binding ? unit.imported.get(name) : undefined;
|
|
251
312
|
};
|
|
252
313
|
|
|
314
|
+
const isFact = (name: string, unit: Unit) =>
|
|
315
|
+
unit.facts.has(name) || unit.bindings.get(name)?.kind === "Theorem";
|
|
316
|
+
|
|
317
|
+
const getGoal = (name: string, unit: Unit) =>
|
|
318
|
+
unit.bindings.get(name)?.kind === "Theorem" ? unit.goals.get(name) : undefined;
|
|
319
|
+
|
|
253
320
|
const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
|
|
254
321
|
lookupLocal(scope, name) ?? findSymbol(name, unit);
|
|
255
322
|
|
|
@@ -271,12 +338,22 @@ const getSpelledNameRange = (
|
|
|
271
338
|
};
|
|
272
339
|
|
|
273
340
|
const getFixity = (expression: Expression) => {
|
|
274
|
-
if (expression.kind !== "Apply") return undefined;
|
|
341
|
+
if (expression.kind !== "Apply" || expression.argumentsRange) return undefined;
|
|
275
342
|
if (expression.args.length === 2) return "infix";
|
|
276
343
|
if (expression.args.length !== 1) return undefined;
|
|
277
|
-
return
|
|
344
|
+
return expression.operator === "-." ? "prefix" : "postfix";
|
|
278
345
|
};
|
|
279
346
|
|
|
347
|
+
const BUILTIN_LOCATION = "In module --TLA+ BUILTINS--";
|
|
348
|
+
|
|
349
|
+
const SELECTABLE_KINDS = new Set<SymbolKind>(["Definition", "Instance", "Theorem"]);
|
|
350
|
+
|
|
351
|
+
const reportUnselectable = (name: string, range: SourceRange, unit: Unit) =>
|
|
352
|
+
unit.errors.push({
|
|
353
|
+
location: formatRange(range),
|
|
354
|
+
message: `Cannot take subexpression of \`${resolveSynonym(name)}'.`,
|
|
355
|
+
});
|
|
356
|
+
|
|
280
357
|
const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
|
|
281
358
|
|
|
282
359
|
const isStepName = (name: string) =>
|
|
@@ -388,6 +465,7 @@ export const getOperands = (expression: Expression): Expression[] | undefined =>
|
|
|
388
465
|
case "Enabled":
|
|
389
466
|
case "Always":
|
|
390
467
|
case "Eventually":
|
|
468
|
+
case "Selection":
|
|
391
469
|
return [expression.expression];
|
|
392
470
|
case "BoxAction":
|
|
393
471
|
case "AngleAction":
|
|
@@ -416,7 +494,7 @@ const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
|
|
|
416
494
|
|
|
417
495
|
const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
|
|
418
496
|
if (expression.kind === "Label") return expression.name === name ? expression : undefined;
|
|
419
|
-
if (expression.kind === "Lambda") return undefined;
|
|
497
|
+
if (expression.kind === "Lambda" || expression.kind === "Selection") return undefined;
|
|
420
498
|
if (expression.kind === "Let") return findLabel(expression.body, name);
|
|
421
499
|
return getChildren(expression).reduce<LabelExpression | undefined>(
|
|
422
500
|
(found, child) => found ?? findLabel(child, name),
|
|
@@ -453,7 +531,6 @@ const checkSelection = (
|
|
|
453
531
|
reference: Reference,
|
|
454
532
|
selectors: Selector[],
|
|
455
533
|
goal: Goal,
|
|
456
|
-
scope: LocalName | undefined,
|
|
457
534
|
unit: Unit,
|
|
458
535
|
isFact: boolean,
|
|
459
536
|
) => {
|
|
@@ -462,6 +539,7 @@ const checkSelection = (
|
|
|
462
539
|
(index === undefined ? undefined : reference.selectorRanges?.[index]) ??
|
|
463
540
|
reference.location.range;
|
|
464
541
|
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
542
|
+
return undefined;
|
|
465
543
|
};
|
|
466
544
|
const isOutOfScope = goal.isSuffices === goal.isInProof;
|
|
467
545
|
const statement = goal.statement;
|
|
@@ -470,6 +548,8 @@ const checkSelection = (
|
|
|
470
548
|
isGoal: true,
|
|
471
549
|
isSufficesOpen: goal.isSuffices,
|
|
472
550
|
};
|
|
551
|
+
let params = 0;
|
|
552
|
+
let args = 0;
|
|
473
553
|
for (const [offset, selector] of selectors.slice(1).entries()) {
|
|
474
554
|
const index = offset + 1;
|
|
475
555
|
const node = state.node;
|
|
@@ -479,9 +559,7 @@ const checkSelection = (
|
|
|
479
559
|
node.kind !== "NewSymbol" &&
|
|
480
560
|
node.kind !== "AssumeProve" &&
|
|
481
561
|
getLabelBody(node).kind === "Let";
|
|
482
|
-
return isLet
|
|
483
|
-
? undefined
|
|
484
|
-
: report(index - 1, "A name selector here must be from a LET clause.");
|
|
562
|
+
return isLet ? node : report(index - 1, "A name selector here must be from a LET clause.");
|
|
485
563
|
}
|
|
486
564
|
const clauses =
|
|
487
565
|
statement.kind === "AssumeProve"
|
|
@@ -519,10 +597,18 @@ const checkSelection = (
|
|
|
519
597
|
state.isSufficesOpen = false;
|
|
520
598
|
continue;
|
|
521
599
|
}
|
|
522
|
-
if (selector.kind
|
|
523
|
-
const operand =
|
|
600
|
+
if (selector.kind === "Colon") return node;
|
|
601
|
+
const operand =
|
|
602
|
+
selector.kind === "Operand"
|
|
603
|
+
? getOperandName(selector.operand)
|
|
604
|
+
: selector.kind === "At"
|
|
605
|
+
? "@"
|
|
606
|
+
: "!(...)";
|
|
607
|
+
const getPosition = (count: number) =>
|
|
608
|
+
selector.kind === "Operand" ? getOperandIndex(selector.operand, count) : -1;
|
|
609
|
+
const unexpectedNull = `An unexpected null node specified by '${operand}'.\nThis is probably due to a previous error.`;
|
|
524
610
|
if (state.isSufficesOpen) {
|
|
525
|
-
if (
|
|
611
|
+
if (getPosition(1) !== 0) {
|
|
526
612
|
return report(
|
|
527
613
|
index,
|
|
528
614
|
node.kind === "AssumeProve"
|
|
@@ -533,20 +619,37 @@ const checkSelection = (
|
|
|
533
619
|
state.isSufficesOpen = false;
|
|
534
620
|
continue;
|
|
535
621
|
}
|
|
536
|
-
if (node.kind === "NewSymbol") return;
|
|
622
|
+
if (node.kind === "NewSymbol") return node;
|
|
537
623
|
if (node.kind !== "AssumeProve") {
|
|
538
624
|
const expression = getLabelBody(node);
|
|
625
|
+
if (unresolvedExpressions.has(expression)) {
|
|
626
|
+
return report(
|
|
627
|
+
index,
|
|
628
|
+
"Subexpression selection failed, probably due to an error in the selected expression.",
|
|
629
|
+
);
|
|
630
|
+
}
|
|
539
631
|
if (["Number", "Decimal", "String"].includes(expression.kind)) {
|
|
540
632
|
return report(index, "Selecting subexpression of expression that has none.");
|
|
541
633
|
}
|
|
542
|
-
|
|
543
|
-
expression.kind === "Identifier"
|
|
544
|
-
? findBinding(expression.name, scope, unit)?.kind
|
|
545
|
-
: undefined;
|
|
546
|
-
if (kind === "Variable" || kind?.startsWith("New")) {
|
|
634
|
+
if (isEmptyExpression(expression)) {
|
|
547
635
|
return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
|
|
548
636
|
}
|
|
549
|
-
const
|
|
637
|
+
const binder = getBinder(expression);
|
|
638
|
+
if (binder && selector.kind !== "Operand") {
|
|
639
|
+
const count = selector.kind === "Bound" ? selector.args.length : 0;
|
|
640
|
+
if (selector.kind === "Bound" && count !== binder.names.length) {
|
|
641
|
+
return report(
|
|
642
|
+
index,
|
|
643
|
+
`Selector with ${count} argument(s) used for quantifier with ${binder.names.length} bound identifier(s).`,
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
params += binder.names.length;
|
|
647
|
+
args += count;
|
|
648
|
+
state.node = binder.body;
|
|
649
|
+
state.isGoal = false;
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const sets = binder?.sets;
|
|
550
653
|
const operands =
|
|
551
654
|
expression.kind === "Identifier"
|
|
552
655
|
? []
|
|
@@ -554,18 +657,21 @@ const checkSelection = (
|
|
|
554
657
|
? sets.length > 0
|
|
555
658
|
? sets
|
|
556
659
|
: undefined
|
|
557
|
-
: expression
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
const position = getOperandIndex(selector.operand, operands.length);
|
|
660
|
+
: getOperands(expression);
|
|
661
|
+
if (!operands) return node;
|
|
662
|
+
const isExcept = expression.kind === "Except";
|
|
663
|
+
const position = getPosition(isExcept ? expression.updates.length + 1 : operands.length);
|
|
562
664
|
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
665
|
+
if (isExcept && position > 0) {
|
|
666
|
+
return report(index, "Selecting subexpression of an EXCEPT not yet implemented.");
|
|
667
|
+
}
|
|
563
668
|
state.node = operands[position];
|
|
564
669
|
state.isGoal = false;
|
|
670
|
+
if (unresolvedExpressions.has(state.node)) return report(index, unexpectedNull);
|
|
565
671
|
continue;
|
|
566
672
|
}
|
|
567
673
|
const clauses = [...node.assumptions, node.conclusion];
|
|
568
|
-
const position =
|
|
674
|
+
const position = getPosition(clauses.length);
|
|
569
675
|
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
570
676
|
if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
|
|
571
677
|
return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
|
|
@@ -576,14 +682,24 @@ const checkSelection = (
|
|
|
576
682
|
}
|
|
577
683
|
state.node = clause;
|
|
578
684
|
state.isGoal = false;
|
|
685
|
+
if (
|
|
686
|
+
clause.kind !== "NewSymbol" &&
|
|
687
|
+
clause.kind !== "AssumeProve" &&
|
|
688
|
+
unresolvedExpressions.has(clause)
|
|
689
|
+
) {
|
|
690
|
+
return report(index, unexpectedNull);
|
|
691
|
+
}
|
|
579
692
|
}
|
|
580
|
-
if (isFact) return;
|
|
581
693
|
if (state.node.kind === "AssumeProve") {
|
|
582
|
-
report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
694
|
+
return isFact ? state.node : report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
583
695
|
}
|
|
584
696
|
if (state.node.kind === "NewSymbol") {
|
|
585
|
-
|
|
697
|
+
return isFact
|
|
698
|
+
? state.node
|
|
699
|
+
: report(undefined, "Selected a NEW declaration as an expression or operator.");
|
|
586
700
|
}
|
|
701
|
+
if (params !== args) return report(undefined, `Expected ${params} arguments but found ${args}.`);
|
|
702
|
+
return state.node;
|
|
587
703
|
};
|
|
588
704
|
|
|
589
705
|
const isLabelReported = (label: LabelExpression, unit: Unit) => {
|
|
@@ -659,10 +775,18 @@ const checkReference = (
|
|
|
659
775
|
const first = selectors[0];
|
|
660
776
|
const goal =
|
|
661
777
|
selectors.length > 1 && first.kind === "Name"
|
|
662
|
-
? (lookupLocal(scope, first.name)?.goal ??
|
|
778
|
+
? (lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit))
|
|
663
779
|
: undefined;
|
|
664
|
-
|
|
665
|
-
|
|
780
|
+
return goal && { node: checkSelection(expression, selectors, goal, unit, isFact) };
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
const isInstanceName = (name: string, scope: LocalName | undefined, unit: Unit) => {
|
|
784
|
+
const binding = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
|
|
785
|
+
if (binding) return binding.kind === "Instance";
|
|
786
|
+
return (
|
|
787
|
+
findInstance(name, scope, unit) !== undefined &&
|
|
788
|
+
[...unit.bindings.keys()].some((key) => key.startsWith(`${name}!`))
|
|
789
|
+
);
|
|
666
790
|
};
|
|
667
791
|
|
|
668
792
|
const getSymbolArity = (
|
|
@@ -677,16 +801,38 @@ const getSymbolArity = (
|
|
|
677
801
|
if (renamed !== undefined) return renamed;
|
|
678
802
|
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
679
803
|
const local = lookupLocal(scope, name);
|
|
680
|
-
if (local?.
|
|
804
|
+
if (local?.nonFactStep) {
|
|
681
805
|
unit.errors.push({
|
|
682
806
|
location: formatRange(getSpelledNameRange(expression, name)),
|
|
683
807
|
message: "Step number of non-expression step used as an expression.",
|
|
684
808
|
});
|
|
685
809
|
return undefined;
|
|
686
810
|
}
|
|
811
|
+
const keyword = local?.goal?.keyword;
|
|
812
|
+
if (keyword) {
|
|
813
|
+
unit.errors.push({
|
|
814
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
815
|
+
message: `${keyword} proof step selected instead of expression.`,
|
|
816
|
+
});
|
|
817
|
+
return undefined;
|
|
818
|
+
}
|
|
687
819
|
if (checkReference(expression, scope, unit, false) || (isStepName(name) && local)) {
|
|
688
820
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
689
821
|
}
|
|
822
|
+
if (isInstanceName(name, scope, unit)) {
|
|
823
|
+
const range = getSpelledNameRange(expression, name);
|
|
824
|
+
unit.errors.push({
|
|
825
|
+
location: formatRange({
|
|
826
|
+
...range,
|
|
827
|
+
start: {
|
|
828
|
+
line: range.end.line,
|
|
829
|
+
column: range.end.column - name.slice(name.lastIndexOf("!") + 1).length + 1,
|
|
830
|
+
},
|
|
831
|
+
}),
|
|
832
|
+
message: `Operator name ${name} is incomplete.`,
|
|
833
|
+
});
|
|
834
|
+
return undefined;
|
|
835
|
+
}
|
|
690
836
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
691
837
|
if (local) return local.arity;
|
|
692
838
|
const symbol = findSymbol(name, unit);
|
|
@@ -697,29 +843,55 @@ const getSymbolArity = (
|
|
|
697
843
|
return symbol.arity;
|
|
698
844
|
}
|
|
699
845
|
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
700
|
-
const
|
|
846
|
+
const selected =
|
|
847
|
+
prefix === "" ? undefined : (lookupLocal(scope, prefix) ?? findSymbol(prefix, unit));
|
|
848
|
+
if (selected && !SELECTABLE_KINDS.has(selected.kind)) {
|
|
849
|
+
reportUnselectable(
|
|
850
|
+
prefix,
|
|
851
|
+
(expression.kind !== "OperatorReference" && expression.selectorRanges?.[0]) ||
|
|
852
|
+
getSpelledNameRange(expression, name),
|
|
853
|
+
unit,
|
|
854
|
+
);
|
|
855
|
+
return undefined;
|
|
856
|
+
}
|
|
857
|
+
if (
|
|
858
|
+
expression.kind !== "OperatorReference" &&
|
|
701
859
|
prefix !== "" &&
|
|
702
|
-
(
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
860
|
+
isPendingSelectionReported(
|
|
861
|
+
prefix,
|
|
862
|
+
expression.kind === "Apply" && expression.argumentCounts
|
|
863
|
+
? expression.args.slice(0, expression.argumentCounts[0])
|
|
864
|
+
: [],
|
|
865
|
+
expression.selectorRanges?.[0],
|
|
866
|
+
(expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ?? location.range,
|
|
867
|
+
scope,
|
|
868
|
+
unit,
|
|
869
|
+
)
|
|
870
|
+
) {
|
|
871
|
+
return undefined;
|
|
872
|
+
}
|
|
873
|
+
if (selected || (prefix !== "" && isFact(prefix, unit))) {
|
|
706
874
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
707
875
|
}
|
|
708
|
-
const
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
: getSpelledNameRange(expression, name),
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
|
|
876
|
+
const fixity = getFixity(expression);
|
|
877
|
+
const isOperatorApplication = expression.kind === "OperatorReference" || fixity !== undefined;
|
|
878
|
+
if (!isOperatorSymbol(name) || isStepName(name) || !isOperatorApplication) {
|
|
879
|
+
unit.errors.push({
|
|
880
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
881
|
+
message: `Unknown operator: \`${resolveSynonym(name)}'.`,
|
|
882
|
+
});
|
|
716
883
|
return undefined;
|
|
717
884
|
}
|
|
885
|
+
const spelling = expression.spelling ?? name;
|
|
718
886
|
unit.errors.push({
|
|
719
|
-
location:
|
|
887
|
+
location: formatRange(
|
|
888
|
+
fixity === "prefix" && spelling === "-."
|
|
889
|
+
? getNameRange(location, "-")
|
|
890
|
+
: getSpelledNameRange(expression, name),
|
|
891
|
+
),
|
|
720
892
|
message: `Could not find declaration or definition of symbol '${spelling}'.`,
|
|
721
893
|
});
|
|
722
|
-
|
|
894
|
+
if (fixity) unresolvedExpressions.add(expression);
|
|
723
895
|
if (fixity && location.range) {
|
|
724
896
|
unit.errors.push({
|
|
725
897
|
location: formatRange(location.range),
|
|
@@ -774,15 +946,30 @@ const resolveDefinition = (
|
|
|
774
946
|
const isDeclared = current?.isPending === true && recursive.has(current);
|
|
775
947
|
if (current?.isPending && !isDeclared) {
|
|
776
948
|
report(
|
|
777
|
-
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
|
|
949
|
+
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${resolveSynonym(name)} defined at wrong LET/IN level.`,
|
|
778
950
|
);
|
|
779
951
|
} else if (current && !isDeclared) {
|
|
780
952
|
report(
|
|
781
953
|
definition.kind === "Function"
|
|
782
954
|
? `Function name \`${name}' already defined or declared.`
|
|
783
|
-
: `Operator ${name} already defined or declared.`,
|
|
955
|
+
: `Operator ${resolveSynonym(name)} already defined or declared.`,
|
|
956
|
+
);
|
|
957
|
+
} else if (current && definition.kind === "Function" && current.arity > 0) {
|
|
958
|
+
report(`Function ${resolveSynonym(name)} has operator arguments in its RECURSIVE declaration.`);
|
|
959
|
+
} else if (
|
|
960
|
+
current &&
|
|
961
|
+
definition.kind === "Operator" &&
|
|
962
|
+
// Deviation from SANY: Generator#processOperator compares only the last parameter's arity with the RECURSIVE declaration's.
|
|
963
|
+
(current.arity !== definition.params.length ||
|
|
964
|
+
definition.params.some((param) => param.arity > 0))
|
|
965
|
+
) {
|
|
966
|
+
report(
|
|
967
|
+
`Definition of ${resolveSynonym(name)} has different arity than its RECURSIVE declaration.`,
|
|
784
968
|
);
|
|
785
969
|
}
|
|
970
|
+
if (current && isDeclared && definition.kind === "Operator") {
|
|
971
|
+
current.params = getParamArities(definition);
|
|
972
|
+
}
|
|
786
973
|
if (definition.kind === "Function") {
|
|
787
974
|
resolveExpression(
|
|
788
975
|
definition.body,
|
|
@@ -793,8 +980,10 @@ const resolveDefinition = (
|
|
|
793
980
|
definition.isSelfReferential = isReferenced(definition.body, name);
|
|
794
981
|
} else resolveExpression(definition.body, inner, unit);
|
|
795
982
|
if (current && isDeclared) {
|
|
796
|
-
current.
|
|
797
|
-
|
|
983
|
+
if (definition.kind === "Operator" || current.arity === 0) {
|
|
984
|
+
current.isPending = false;
|
|
985
|
+
current.range = range;
|
|
986
|
+
}
|
|
798
987
|
return false;
|
|
799
988
|
}
|
|
800
989
|
return definition.kind === "Function"
|
|
@@ -812,10 +1001,15 @@ const resolveDefinitions = (
|
|
|
812
1001
|
scope: LocalName | undefined,
|
|
813
1002
|
unit: Unit,
|
|
814
1003
|
instances: Instance[] = [],
|
|
1004
|
+
declarations: Declaration[] = [],
|
|
815
1005
|
) => {
|
|
816
1006
|
const recursive = new Set<Binding>();
|
|
817
1007
|
const declared = definitions
|
|
818
|
-
.filter(
|
|
1008
|
+
.filter(
|
|
1009
|
+
(definition) =>
|
|
1010
|
+
definition.isRecursive &&
|
|
1011
|
+
declarations.every((declaration) => declaration.name !== definition.name),
|
|
1012
|
+
)
|
|
819
1013
|
.reduce<LocalName | undefined>((parent, definition) => {
|
|
820
1014
|
if (lookupLocal(parent, definition.name)?.isPending) return parent;
|
|
821
1015
|
const local: LocalName = {
|
|
@@ -829,20 +1023,47 @@ const resolveDefinitions = (
|
|
|
829
1023
|
recursive.add(local);
|
|
830
1024
|
return local;
|
|
831
1025
|
}, scope);
|
|
832
|
-
const
|
|
833
|
-
const
|
|
1026
|
+
const declaredLocals = new Map<Declaration, LocalName | undefined>();
|
|
1027
|
+
const bindDeclaration = (parent: LocalName | undefined, declaration: Declaration) => {
|
|
1028
|
+
const binding: Binding = {
|
|
1029
|
+
kind: "Definition",
|
|
1030
|
+
arity: declaration.arity,
|
|
1031
|
+
params: Array(declaration.arity).fill(0),
|
|
1032
|
+
range: declaration.location.range,
|
|
1033
|
+
};
|
|
1034
|
+
const local: LocalName | undefined = isBindable(
|
|
1035
|
+
declaration.name,
|
|
1036
|
+
lookupLocal(parent, declaration.name) ??
|
|
1037
|
+
getModuleBinding(declaration.name, unit, binding.range),
|
|
1038
|
+
binding,
|
|
1039
|
+
unit,
|
|
1040
|
+
)
|
|
1041
|
+
? { ...binding, name: declaration.name, parent, isPending: true }
|
|
1042
|
+
: undefined;
|
|
1043
|
+
if (local) recursive.add(local);
|
|
1044
|
+
declaredLocals.set(declaration, local);
|
|
1045
|
+
return local ?? parent;
|
|
1046
|
+
};
|
|
1047
|
+
const pending = [...(unit.isGenerated ? [] : instances), ...declarations].toSorted(
|
|
1048
|
+
(left, right) => compareLocations(left.location, right.location),
|
|
1049
|
+
);
|
|
1050
|
+
const resolvePending = (
|
|
834
1051
|
current: LocalName | undefined,
|
|
835
|
-
isDue: (
|
|
1052
|
+
isDue: (item: Instance | Declaration) => boolean,
|
|
836
1053
|
) => {
|
|
837
|
-
const count = pending.findIndex((
|
|
1054
|
+
const count = pending.findIndex((item) => !isDue(item));
|
|
838
1055
|
return pending
|
|
839
1056
|
.splice(0, count === -1 ? pending.length : count)
|
|
840
|
-
.reduce(
|
|
1057
|
+
.reduce(
|
|
1058
|
+
(parent, item) =>
|
|
1059
|
+
"kind" in item ? resolveLocalInstance(item, parent, unit) : bindDeclaration(parent, item),
|
|
1060
|
+
current,
|
|
1061
|
+
);
|
|
841
1062
|
};
|
|
842
1063
|
const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
|
|
843
1064
|
const current = isKey(definition.name)
|
|
844
1065
|
? previous
|
|
845
|
-
:
|
|
1066
|
+
: resolvePending(previous, (item) => !isBefore(definition.location, item.location));
|
|
846
1067
|
const local: LocalName = {
|
|
847
1068
|
name: definition.name,
|
|
848
1069
|
arity: getArity(definition),
|
|
@@ -862,7 +1083,17 @@ const resolveDefinitions = (
|
|
|
862
1083
|
? { ...local, params: getParamArities(definition) }
|
|
863
1084
|
: current;
|
|
864
1085
|
}, declared);
|
|
865
|
-
|
|
1086
|
+
const resolvedScope = resolvePending(resolved, () => true);
|
|
1087
|
+
declaredLocals.forEach((local, declaration) => {
|
|
1088
|
+
const range = declaration.location.range;
|
|
1089
|
+
if (range && !unit.isGenerated && (!local || local.isPending)) {
|
|
1090
|
+
unit.errors.push({
|
|
1091
|
+
location: formatRange(range),
|
|
1092
|
+
message: `Symbol ${resolveSynonym(declaration.name)} declared in RECURSIVE statement but not defined.`,
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
return resolvedScope;
|
|
866
1097
|
};
|
|
867
1098
|
|
|
868
1099
|
const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
|
|
@@ -871,7 +1102,9 @@ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
|
|
|
871
1102
|
prefix === name
|
|
872
1103
|
? undefined
|
|
873
1104
|
: (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
|
|
874
|
-
return binding?.kind === "Instance"
|
|
1105
|
+
return binding?.kind === "Instance"
|
|
1106
|
+
? { name: prefix, arity: binding.arity, range: binding.range }
|
|
1107
|
+
: undefined;
|
|
875
1108
|
};
|
|
876
1109
|
|
|
877
1110
|
const isInstanceArityReported = (
|
|
@@ -879,16 +1112,22 @@ const isInstanceArityReported = (
|
|
|
879
1112
|
scope: LocalName | undefined,
|
|
880
1113
|
unit: Unit,
|
|
881
1114
|
) => {
|
|
882
|
-
if (expression
|
|
1115
|
+
if (!isReference(expression)) return false;
|
|
1116
|
+
const selectors = getSelectors(expression);
|
|
1117
|
+
const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
|
|
883
1118
|
const instance = findInstance(
|
|
884
|
-
|
|
1119
|
+
selectors
|
|
1120
|
+
.slice(0, nameCount === -1 ? selectors.length : nameCount)
|
|
1121
|
+
.flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []))
|
|
1122
|
+
.join("!"),
|
|
885
1123
|
scope,
|
|
886
1124
|
unit,
|
|
887
1125
|
);
|
|
888
|
-
const
|
|
1126
|
+
const first = selectors[0];
|
|
1127
|
+
const count = first.kind === "Name" ? first.args.length : 0;
|
|
889
1128
|
if (!instance || count === instance.arity) return false;
|
|
890
1129
|
const range =
|
|
891
|
-
(expression.kind === "
|
|
1130
|
+
(expression.kind === "Identifier" ? undefined : expression.argumentRanges?.[0]) ??
|
|
892
1131
|
expression.location.range;
|
|
893
1132
|
if (range) {
|
|
894
1133
|
unit.errors.push({
|
|
@@ -899,103 +1138,210 @@ const isInstanceArityReported = (
|
|
|
899
1138
|
return true;
|
|
900
1139
|
};
|
|
901
1140
|
|
|
1141
|
+
const isArgumentCountReported = (
|
|
1142
|
+
callee: Binding | undefined,
|
|
1143
|
+
name: string,
|
|
1144
|
+
count: number,
|
|
1145
|
+
range: SourceRange | undefined,
|
|
1146
|
+
unit: Unit,
|
|
1147
|
+
) => {
|
|
1148
|
+
const params = callee?.kind === "Definition" ? callee.params : undefined;
|
|
1149
|
+
if (!callee || !params || params.length === callee.arity) return false;
|
|
1150
|
+
if (range && !unit.isGenerated) {
|
|
1151
|
+
unit.errors.push({
|
|
1152
|
+
location: formatRange(range),
|
|
1153
|
+
message: `Wrong number of arguments (${count}) given to operator '${resolveSynonym(name)}', \nwhich requires ${params.length} arguments.`,
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
return true;
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
const resolveExpressionArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1160
|
+
const range = arg.location.range;
|
|
1161
|
+
const report = (message: string) => {
|
|
1162
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
1163
|
+
};
|
|
1164
|
+
const selectionError = operatorSelectionErrors.get(arg);
|
|
1165
|
+
// Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
|
|
1166
|
+
if (arg.kind === "Lambda" && arg.operator !== undefined) {
|
|
1167
|
+
report(`The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
|
|
1168
|
+
} else if (selectionError) {
|
|
1169
|
+
unit.errors.push(selectionError);
|
|
1170
|
+
} else if (arg.kind === "Lambda" && range) {
|
|
1171
|
+
unresolvedExpressions.add(arg);
|
|
1172
|
+
report("LAMBDA expression used where an expression is required.");
|
|
1173
|
+
} else if (arg.kind === "OperatorReference") {
|
|
1174
|
+
const argArity = getSymbolArity(arg, arg.operator, scope, unit);
|
|
1175
|
+
if (argArity !== undefined) {
|
|
1176
|
+
report(`The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`);
|
|
1177
|
+
}
|
|
1178
|
+
} else resolveExpression(arg, scope, unit);
|
|
1179
|
+
};
|
|
1180
|
+
|
|
1181
|
+
const matchArgument = (
|
|
1182
|
+
arg: Expression,
|
|
1183
|
+
expected: number,
|
|
1184
|
+
index: number,
|
|
1185
|
+
operator: string,
|
|
1186
|
+
operatorRange: SourceRange | undefined,
|
|
1187
|
+
scope: LocalName | undefined,
|
|
1188
|
+
unit: Unit,
|
|
1189
|
+
) => {
|
|
1190
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
1191
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
1192
|
+
};
|
|
1193
|
+
const reportOperatorArity = (name: string, arity: number) => {
|
|
1194
|
+
const definition =
|
|
1195
|
+
findInstance(operator, scope, unit)?.range ?? findBinding(operator, scope, unit)?.range;
|
|
1196
|
+
if (!definition) return;
|
|
1197
|
+
report(
|
|
1198
|
+
operatorRange,
|
|
1199
|
+
`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.`,
|
|
1200
|
+
);
|
|
1201
|
+
};
|
|
1202
|
+
const range = arg.location.range;
|
|
1203
|
+
if (expected === 0) {
|
|
1204
|
+
resolveExpressionArgument(arg, scope, unit);
|
|
1205
|
+
return true;
|
|
1206
|
+
}
|
|
1207
|
+
const parenthesized = parenthesizedRanges.get(arg);
|
|
1208
|
+
if (parenthesized) {
|
|
1209
|
+
report(
|
|
1210
|
+
parenthesized,
|
|
1211
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
1212
|
+
);
|
|
1213
|
+
return false;
|
|
1214
|
+
}
|
|
1215
|
+
const reference = selections.get(arg) ?? arg;
|
|
1216
|
+
// SANY reads a reference that ends in arguments as an application, not as a selector's arguments.
|
|
1217
|
+
const selectors =
|
|
1218
|
+
reference.kind === "Subexpression" ||
|
|
1219
|
+
(reference.kind === "Apply" && reference.argumentCounts?.at(-1) === 0)
|
|
1220
|
+
? getSelectors(reference)
|
|
1221
|
+
: [];
|
|
1222
|
+
const selectorIndex = selectors.findIndex(
|
|
1223
|
+
(selector) => "args" in selector && selector.args.length > 0,
|
|
1224
|
+
);
|
|
1225
|
+
if (selectorIndex !== -1 && isReference(reference)) {
|
|
1226
|
+
const selector = selectors[selectorIndex];
|
|
1227
|
+
report(
|
|
1228
|
+
reference.selectorRanges?.[selectorIndex],
|
|
1229
|
+
`Selector \`${selector.kind === "Name" ? selector.name : "!(...)"}' should not have argument(s).`,
|
|
1230
|
+
);
|
|
1231
|
+
return false;
|
|
1232
|
+
}
|
|
1233
|
+
if (arg.kind === "Lambda") {
|
|
1234
|
+
if (selections.has(arg)) resolveExpression(arg, scope, unit);
|
|
1235
|
+
else resolveLambda(arg, scope, unit);
|
|
1236
|
+
if (findNullError(nullSelections.get(arg))) return false;
|
|
1237
|
+
const isMatched = arg.params.length === expected;
|
|
1238
|
+
if (!isMatched && selections.has(arg)) {
|
|
1239
|
+
report(range, `Expected arity ${expected} but found operator of arity ${arg.params.length}.`);
|
|
1240
|
+
} else if (!isMatched && range && arg.operator === undefined) {
|
|
1241
|
+
report(
|
|
1242
|
+
operatorRange,
|
|
1243
|
+
`Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
|
|
1244
|
+
);
|
|
1245
|
+
} else if (!isMatched && arg.operator !== undefined) {
|
|
1246
|
+
reportOperatorArity(arg.operator, arg.params.length);
|
|
1247
|
+
}
|
|
1248
|
+
return isMatched;
|
|
1249
|
+
}
|
|
1250
|
+
if (reference !== arg) {
|
|
1251
|
+
const failed = findNullError(nullSelections.get(arg));
|
|
1252
|
+
if (failed) unit.errors.push(failed);
|
|
1253
|
+
if (!failed) {
|
|
1254
|
+
report(reference.location.range, `Expected arity ${expected} but found operator of arity 0.`);
|
|
1255
|
+
}
|
|
1256
|
+
return false;
|
|
1257
|
+
}
|
|
1258
|
+
if (arg.kind === "Subexpression") {
|
|
1259
|
+
resolveExpression(arg, scope, unit);
|
|
1260
|
+
return false;
|
|
1261
|
+
}
|
|
1262
|
+
if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
|
|
1263
|
+
report(
|
|
1264
|
+
range,
|
|
1265
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
1266
|
+
);
|
|
1267
|
+
return false;
|
|
1268
|
+
}
|
|
1269
|
+
const name = arg.kind === "Identifier" ? arg.name : arg.operator;
|
|
1270
|
+
const argParams = findBinding(name, scope, unit)?.params ?? [];
|
|
1271
|
+
const argArity = getSymbolArity(arg, name, scope, unit);
|
|
1272
|
+
if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
|
|
1273
|
+
report(
|
|
1274
|
+
getSpelledNameRange(arg, name),
|
|
1275
|
+
`Expected arity ${expected} but found operator of arity ${argArity}.`,
|
|
1276
|
+
);
|
|
1277
|
+
}
|
|
1278
|
+
if (arg.kind === "OperatorReference" && argArity !== undefined && argArity !== expected) {
|
|
1279
|
+
reportOperatorArity(name, argArity);
|
|
1280
|
+
}
|
|
1281
|
+
// Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
|
|
1282
|
+
return argArity === expected && argParams.every((param) => param === 0);
|
|
1283
|
+
};
|
|
1284
|
+
|
|
902
1285
|
const resolveApply = (
|
|
903
1286
|
expression: Expression & { kind: "Apply" },
|
|
904
1287
|
scope: LocalName | undefined,
|
|
905
1288
|
unit: Unit,
|
|
906
1289
|
) => {
|
|
907
|
-
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
908
1290
|
const operator = expression.operator;
|
|
909
1291
|
const callee = findBinding(operator, scope, unit);
|
|
910
|
-
const arity =
|
|
911
|
-
|
|
912
|
-
|
|
1292
|
+
const arity = isInstanceArityReported(expression, scope, unit)
|
|
1293
|
+
? undefined
|
|
1294
|
+
: getSymbolArity(expression, operator, scope, unit);
|
|
1295
|
+
if (arity !== undefined && arity !== expression.args.length) {
|
|
913
1296
|
unit.errors.push({
|
|
914
1297
|
location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
|
|
915
|
-
message: `The operator ${operator} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
|
|
1298
|
+
message: `The operator ${resolveSynonym(operator)} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
|
|
916
1299
|
});
|
|
1300
|
+
}
|
|
1301
|
+
if (arity !== expression.args.length) {
|
|
1302
|
+
emptyExpressions.add(expression);
|
|
917
1303
|
return;
|
|
918
1304
|
}
|
|
919
|
-
const params =
|
|
1305
|
+
const params =
|
|
1306
|
+
callee && (callee.kind === "Definition" ? callee.params : expression.args.map(() => 0));
|
|
920
1307
|
if (params === undefined) {
|
|
921
1308
|
expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
922
1309
|
return;
|
|
923
1310
|
}
|
|
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
|
-
});
|
|
1311
|
+
const operatorRange =
|
|
1312
|
+
expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator);
|
|
1313
|
+
const isSelection = operator === SELECTION_NAME;
|
|
1314
|
+
const matches = expression.args.slice(0, params.length).map((arg, index) => {
|
|
1315
|
+
const source = isSelection ? selectionArguments.get(arg) : undefined;
|
|
1316
|
+
return matchArgument(
|
|
1317
|
+
arg,
|
|
1318
|
+
params[index],
|
|
1319
|
+
source?.index ?? index,
|
|
1320
|
+
source?.operator ?? operator,
|
|
1321
|
+
source?.range ?? operatorRange,
|
|
1322
|
+
scope,
|
|
1323
|
+
isSelection ? { ...unit, isGenerated: false } : unit,
|
|
1324
|
+
);
|
|
1325
|
+
});
|
|
1326
|
+
if (
|
|
1327
|
+
isArgumentCountReported(
|
|
1328
|
+
callee,
|
|
1329
|
+
operator,
|
|
1330
|
+
expression.args.length,
|
|
1331
|
+
expression.location.range,
|
|
1332
|
+
unit,
|
|
1333
|
+
)
|
|
1334
|
+
) {
|
|
1335
|
+
return;
|
|
1336
|
+
}
|
|
1337
|
+
matches.forEach((isMatched, index) => {
|
|
1338
|
+
if (!isMatched && expression.location.range) {
|
|
1339
|
+
unit.errors.push({
|
|
1340
|
+
location: formatRange(expression.location.range),
|
|
1341
|
+
message: `Argument number ${index + 1} to operator '${isSelection ? "LAMBDA" : operator}' \nshould be a ${params[index]}-parameter operator.`,
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
});
|
|
999
1345
|
};
|
|
1000
1346
|
|
|
1001
1347
|
const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
@@ -1006,17 +1352,50 @@ const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Un
|
|
|
1006
1352
|
const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1007
1353
|
const failed = selectionErrors.get(expression);
|
|
1008
1354
|
if (!failed) return false;
|
|
1009
|
-
failed.
|
|
1010
|
-
|
|
1355
|
+
failed.applications.forEach((application) =>
|
|
1356
|
+
application.args
|
|
1357
|
+
.slice(0, application.params.length)
|
|
1358
|
+
.forEach((arg, index) =>
|
|
1359
|
+
matchArgument(
|
|
1360
|
+
arg,
|
|
1361
|
+
application.params[index],
|
|
1362
|
+
index,
|
|
1363
|
+
application.operator,
|
|
1364
|
+
application.range,
|
|
1365
|
+
scope,
|
|
1366
|
+
unit,
|
|
1367
|
+
),
|
|
1368
|
+
),
|
|
1369
|
+
);
|
|
1370
|
+
unit.errors.push(findNullError(failed.nulls) ?? failed.error);
|
|
1011
1371
|
return true;
|
|
1012
1372
|
};
|
|
1013
1373
|
|
|
1374
|
+
const resolveLambda = (
|
|
1375
|
+
expression: Expression & { kind: "Lambda" },
|
|
1376
|
+
scope: LocalName | undefined,
|
|
1377
|
+
unit: Unit,
|
|
1378
|
+
) =>
|
|
1379
|
+
resolveExpression(
|
|
1380
|
+
expression.body,
|
|
1381
|
+
bindNames(scope, expression.params, expression.paramRanges, unit),
|
|
1382
|
+
{
|
|
1383
|
+
...unit,
|
|
1384
|
+
labelParams: [...unit.labelParams, ...expression.params],
|
|
1385
|
+
},
|
|
1386
|
+
);
|
|
1387
|
+
|
|
1014
1388
|
const resolveExpression = (
|
|
1015
1389
|
expression: Expression,
|
|
1016
1390
|
scope: LocalName | undefined,
|
|
1017
1391
|
unit: Unit,
|
|
1018
1392
|
): void => {
|
|
1019
1393
|
if (selections.has(expression) && !unit.isGenerated) {
|
|
1394
|
+
const failed = findNullError(nullSelections.get(expression));
|
|
1395
|
+
if (failed) {
|
|
1396
|
+
unit.errors.push(failed);
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1020
1399
|
resolveExpression(expression, scope, { ...unit, isGenerated: true });
|
|
1021
1400
|
return;
|
|
1022
1401
|
}
|
|
@@ -1030,18 +1409,43 @@ const resolveExpression = (
|
|
|
1030
1409
|
case "Number":
|
|
1031
1410
|
case "Decimal":
|
|
1032
1411
|
case "String":
|
|
1412
|
+
return;
|
|
1033
1413
|
case "ExceptAt":
|
|
1414
|
+
if (!unit.isInExcept && expression.location.range) {
|
|
1415
|
+
emptyExpressions.add(expression);
|
|
1416
|
+
unit.errors.push({
|
|
1417
|
+
location: formatRange(expression.location.range),
|
|
1418
|
+
message: "@ used where its meaning is not defined.",
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1034
1421
|
return;
|
|
1035
1422
|
case "Identifier": {
|
|
1036
|
-
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
1037
1423
|
const name = expression.name;
|
|
1038
|
-
const arity =
|
|
1039
|
-
|
|
1424
|
+
const arity = isInstanceArityReported(expression, scope, unit)
|
|
1425
|
+
? undefined
|
|
1426
|
+
: getSymbolArity(expression, name, scope, unit);
|
|
1427
|
+
if (arity === 0) {
|
|
1428
|
+
const binding = findBinding(name, scope, unit);
|
|
1429
|
+
const kind = binding?.kind;
|
|
1430
|
+
// SANY's selector chooses no operand of a variable, theorem, step or NEW symbol, unlike of a constant or definition.
|
|
1431
|
+
if (
|
|
1432
|
+
kind === "Variable" ||
|
|
1433
|
+
kind === "Theorem" ||
|
|
1434
|
+
kind?.startsWith("New") ||
|
|
1435
|
+
isFact(name, unit)
|
|
1436
|
+
) {
|
|
1437
|
+
emptyExpressions.add(expression);
|
|
1438
|
+
}
|
|
1439
|
+
isArgumentCountReported(binding, name, 0, getSpelledNameRange(expression, name), unit);
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
if (arity !== undefined) {
|
|
1040
1443
|
unit.errors.push({
|
|
1041
1444
|
location: formatRange(getSpelledNameRange(expression, name)),
|
|
1042
|
-
message: `The operator ${name} requires ${arity} arguments.`,
|
|
1445
|
+
message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
|
|
1043
1446
|
});
|
|
1044
1447
|
}
|
|
1448
|
+
emptyExpressions.add(expression);
|
|
1045
1449
|
return;
|
|
1046
1450
|
}
|
|
1047
1451
|
case "OperatorReference":
|
|
@@ -1051,11 +1455,14 @@ const resolveExpression = (
|
|
|
1051
1455
|
resolveApply(expression, scope, unit);
|
|
1052
1456
|
return;
|
|
1053
1457
|
case "Lambda":
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1458
|
+
if (selections.has(expression)) resolveLambda(expression, scope, unit);
|
|
1459
|
+
else if (expression.location.range) {
|
|
1460
|
+
unresolvedExpressions.add(expression);
|
|
1461
|
+
unit.errors.push({
|
|
1462
|
+
location: formatRange(expression.location.range),
|
|
1463
|
+
message: "LAMBDA expression used where an expression is required.",
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1059
1466
|
return;
|
|
1060
1467
|
case "And":
|
|
1061
1468
|
case "Or":
|
|
@@ -1078,7 +1485,13 @@ const resolveExpression = (
|
|
|
1078
1485
|
case "Let":
|
|
1079
1486
|
resolveExpression(
|
|
1080
1487
|
expression.body,
|
|
1081
|
-
resolveDefinitions(
|
|
1488
|
+
resolveDefinitions(
|
|
1489
|
+
expression.definitions,
|
|
1490
|
+
scope,
|
|
1491
|
+
unit,
|
|
1492
|
+
expression.instances,
|
|
1493
|
+
expression.recursive,
|
|
1494
|
+
),
|
|
1082
1495
|
unit,
|
|
1083
1496
|
);
|
|
1084
1497
|
return;
|
|
@@ -1168,34 +1581,286 @@ const resolveExpression = (
|
|
|
1168
1581
|
resolveExpression(expression.body, scope, { ...unit, labelParams: [], labels: new Set() });
|
|
1169
1582
|
checkLabel(expression, scope, unit);
|
|
1170
1583
|
return;
|
|
1171
|
-
case "Subexpression":
|
|
1172
|
-
if (
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1584
|
+
case "Subexpression":
|
|
1585
|
+
if (isSubexpressionReported(expression, scope, unit)) emptyExpressions.add(expression);
|
|
1586
|
+
return;
|
|
1587
|
+
case "Selection":
|
|
1588
|
+
return;
|
|
1589
|
+
}
|
|
1590
|
+
};
|
|
1591
|
+
|
|
1592
|
+
const isPendingSelectionReported = (
|
|
1593
|
+
name: string,
|
|
1594
|
+
args: Expression[],
|
|
1595
|
+
selectorRange: SourceRange | undefined,
|
|
1596
|
+
argumentsRange: SourceRange | undefined,
|
|
1597
|
+
scope: LocalName | undefined,
|
|
1598
|
+
unit: Unit,
|
|
1599
|
+
) => {
|
|
1600
|
+
const pending = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
|
|
1601
|
+
if (!pending?.isPending || !selectorRange || !argumentsRange) return false;
|
|
1602
|
+
if (args.length !== pending.arity) {
|
|
1603
|
+
unit.errors.push({
|
|
1604
|
+
location: formatRange(argumentsRange),
|
|
1605
|
+
message: `The operator ${name} requires ${pending.arity} arguments.`,
|
|
1606
|
+
});
|
|
1607
|
+
return true;
|
|
1608
|
+
}
|
|
1609
|
+
args.forEach((arg) => resolveExpression(arg, scope, unit));
|
|
1610
|
+
unit.errors.push({
|
|
1611
|
+
location: formatRange(selectorRange),
|
|
1612
|
+
message: `Subexpression of \`${name}' used inside the operator's definition.`,
|
|
1613
|
+
});
|
|
1614
|
+
return true;
|
|
1615
|
+
};
|
|
1616
|
+
|
|
1617
|
+
const isSubexpressionReported = (
|
|
1618
|
+
expression: Expression & { kind: "Subexpression" },
|
|
1619
|
+
scope: LocalName | undefined,
|
|
1620
|
+
unit: Unit,
|
|
1621
|
+
) => {
|
|
1622
|
+
if (
|
|
1623
|
+
isInstanceArityReported(expression, scope, unit) ||
|
|
1624
|
+
isSelectionReported(expression, scope, unit)
|
|
1625
|
+
) {
|
|
1626
|
+
return true;
|
|
1627
|
+
}
|
|
1628
|
+
const head = expression.selectors[0];
|
|
1629
|
+
const range = expression.selectorRanges?.[0];
|
|
1630
|
+
const nameCount = expression.selectors.findIndex((selector) => selector.kind !== "Name");
|
|
1631
|
+
const names = expression.selectors
|
|
1632
|
+
.slice(0, nameCount)
|
|
1633
|
+
.flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []));
|
|
1634
|
+
const symbolIndex = names.findIndex(
|
|
1635
|
+
(_name, index) => index > 0 && findSymbol(names.slice(0, index + 1).join("!"), unit),
|
|
1636
|
+
);
|
|
1637
|
+
const isInstance =
|
|
1638
|
+
names.length > 1 &&
|
|
1639
|
+
(lookupLocal(scope, names[0]) ?? getModuleBinding(names[0], unit, undefined))?.kind ===
|
|
1640
|
+
"Instance";
|
|
1641
|
+
const lastRange = expression.selectorRanges?.[nameCount - 1];
|
|
1642
|
+
if (isInstance && symbolIndex === -1 && range && lastRange) {
|
|
1643
|
+
unit.errors.push({
|
|
1644
|
+
location: formatRange({ module: range.module, start: range.start, end: lastRange.end }),
|
|
1645
|
+
message: `Unknown operator: \`${resolveSynonym(names.join("!"))}'.`,
|
|
1646
|
+
});
|
|
1647
|
+
return true;
|
|
1648
|
+
}
|
|
1649
|
+
const name = names.slice(0, symbolIndex + 1).join("!");
|
|
1650
|
+
const symbol = symbolIndex > 0 ? findSymbol(name, unit) : undefined;
|
|
1651
|
+
const argumentCount = expression.selectors
|
|
1652
|
+
.slice(0, symbolIndex + 1)
|
|
1653
|
+
.flatMap((selector) => ("args" in selector ? selector.args : [])).length;
|
|
1654
|
+
const argumentRange = expression.argumentRanges?.[symbolIndex] ?? expression.location.range;
|
|
1655
|
+
if (symbol && symbol.arity !== argumentCount && argumentRange) {
|
|
1656
|
+
unit.errors.push({
|
|
1657
|
+
location: formatRange(argumentRange),
|
|
1658
|
+
message: `The operator ${resolveSynonym(name)} requires ${symbol.arity} arguments.`,
|
|
1659
|
+
});
|
|
1660
|
+
return true;
|
|
1661
|
+
}
|
|
1662
|
+
if (head.kind === "Name" && expression.selectors[1].kind !== "Name" && range) {
|
|
1663
|
+
const local = lookupLocal(scope, head.name);
|
|
1664
|
+
const binding = local ?? findSymbol(head.name, unit);
|
|
1665
|
+
if ((binding === undefined && !isFact(head.name, unit)) || binding?.kind === "Instance") {
|
|
1666
|
+
unit.errors.push({
|
|
1667
|
+
location: formatRange(range),
|
|
1668
|
+
message: `Unknown operator: \`${resolveSynonym(head.name)}'.`,
|
|
1669
|
+
});
|
|
1670
|
+
return true;
|
|
1671
|
+
}
|
|
1672
|
+
if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.nonFactStep)) {
|
|
1673
|
+
reportUnselectable(head.name, range, unit);
|
|
1674
|
+
return true;
|
|
1177
1675
|
}
|
|
1178
1676
|
}
|
|
1677
|
+
if (
|
|
1678
|
+
head.kind === "Name" &&
|
|
1679
|
+
isPendingSelectionReported(
|
|
1680
|
+
head.name,
|
|
1681
|
+
head.args,
|
|
1682
|
+
range,
|
|
1683
|
+
expression.argumentRanges?.[0] ?? expression.location.range,
|
|
1684
|
+
scope,
|
|
1685
|
+
unit,
|
|
1686
|
+
)
|
|
1687
|
+
) {
|
|
1688
|
+
return true;
|
|
1689
|
+
}
|
|
1690
|
+
expression.selectors
|
|
1691
|
+
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
1692
|
+
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1693
|
+
const selected = checkReference(expression, scope, unit, false);
|
|
1694
|
+
return selected ? selected.node === undefined : false;
|
|
1179
1695
|
};
|
|
1180
1696
|
|
|
1181
|
-
const
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1697
|
+
const reportUnknownModule = (fact: Fact & { kind: "ModuleFact" }, unit: Unit) => {
|
|
1698
|
+
const range = fact.location.range;
|
|
1699
|
+
if (unit.isModuleKnown(fact.name, fact.location) || !range) return;
|
|
1700
|
+
unit.errors.push({
|
|
1701
|
+
location: formatRange({
|
|
1702
|
+
...range,
|
|
1703
|
+
start: { line: range.end.line, column: range.end.column - fact.name.length + 1 },
|
|
1704
|
+
}),
|
|
1705
|
+
message: `Module \`${fact.name}' used without being extended or instantiated.`,
|
|
1706
|
+
});
|
|
1707
|
+
};
|
|
1708
|
+
|
|
1709
|
+
const checkFact = (fact: Fact, reference: Fact, scope: LocalName | undefined, unit: Unit) => {
|
|
1710
|
+
if (fact.kind === "ModuleFact") {
|
|
1711
|
+
reportUnknownModule(fact, unit);
|
|
1712
|
+
return undefined;
|
|
1713
|
+
}
|
|
1714
|
+
if (fact.kind === "ExceptAt" && fact.location.range) {
|
|
1715
|
+
unit.errors.push({
|
|
1716
|
+
location: formatRange(fact.location.range),
|
|
1717
|
+
message: "Unknown operator: `@'.",
|
|
1718
|
+
});
|
|
1719
|
+
return BUILTIN_LOCATION;
|
|
1720
|
+
}
|
|
1721
|
+
const hidden = getHiddenExpressionLocation(fact, reference, scope, unit);
|
|
1722
|
+
if (selections.has(fact)) {
|
|
1723
|
+
resolveExpression(fact, scope, unit);
|
|
1724
|
+
return hidden;
|
|
1725
|
+
}
|
|
1726
|
+
if (isInstanceArityReported(fact, scope, unit)) return hidden;
|
|
1727
|
+
const selected = checkReference(fact, scope, unit, true);
|
|
1728
|
+
if (selected) {
|
|
1729
|
+
const kind = selected.node?.kind;
|
|
1730
|
+
if (kind === undefined) return BUILTIN_LOCATION;
|
|
1731
|
+
return kind === "AssumeProve" || kind === "NewSymbol"
|
|
1732
|
+
? undefined
|
|
1733
|
+
: fact.location.range && formatRange(fact.location.range);
|
|
1734
|
+
}
|
|
1735
|
+
if (fact.kind === "Subexpression") {
|
|
1736
|
+
return isSubexpressionReported(fact, scope, unit) ? BUILTIN_LOCATION : hidden;
|
|
1737
|
+
}
|
|
1738
|
+
const name = fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
|
|
1739
|
+
if (fact.kind === "Identifier" && lookupLocal(scope, name)?.nonFactStep) {
|
|
1740
|
+
unit.errors.push({
|
|
1741
|
+
location: formatRange(getSpelledNameRange(fact, name)),
|
|
1742
|
+
message: "Step number of non-fact used as a fact",
|
|
1743
|
+
});
|
|
1744
|
+
return BUILTIN_LOCATION;
|
|
1745
|
+
}
|
|
1746
|
+
if (
|
|
1747
|
+
!isFact(name, unit) &&
|
|
1748
|
+
!lookupLocal(scope, name)?.goal &&
|
|
1749
|
+
!(fact.kind === "Identifier" && isInstanceName(name, scope, unit))
|
|
1750
|
+
) {
|
|
1751
|
+
resolveExpression(fact, scope, unit);
|
|
1752
|
+
}
|
|
1753
|
+
return hidden;
|
|
1754
|
+
};
|
|
1755
|
+
|
|
1756
|
+
const isDefinitionEntry = (def: Fact, scope: LocalName | undefined, unit: Unit) => {
|
|
1757
|
+
if (def.kind === "ModuleFact") {
|
|
1758
|
+
reportUnknownModule(def, unit);
|
|
1759
|
+
return unit.isModuleKnown(def.name, def.location);
|
|
1760
|
+
}
|
|
1761
|
+
const range = def.location.range;
|
|
1762
|
+
const name =
|
|
1763
|
+
def.kind === "Identifier" ? def.name : def.kind === "OperatorReference" ? def.operator : "";
|
|
1764
|
+
if (name === "" || !range) return true;
|
|
1765
|
+
const local = lookupLocal(scope, name);
|
|
1766
|
+
const binding = local ?? findSymbol(name, unit) ?? getModuleBinding(name, unit, range);
|
|
1767
|
+
if (binding === undefined && isFact(name, unit)) return true;
|
|
1768
|
+
if (binding === undefined) {
|
|
1769
|
+
unit.errors.push({
|
|
1770
|
+
location: formatRange(range),
|
|
1771
|
+
message: `Unknown operator: \`${resolveSynonym(name)}'.`,
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
if (local?.nonFactStep === "Define") return true;
|
|
1775
|
+
if (local?.nonFactStep) {
|
|
1776
|
+
unit.errors.push({
|
|
1777
|
+
location: formatRange(range),
|
|
1778
|
+
message: "DEF clause entry refers to a non-definition step.",
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
if (binding && SELECTABLE_KINDS.has(binding.kind) && !isStepName(name)) return true;
|
|
1782
|
+
unit.errors.push({
|
|
1783
|
+
location: formatRange(range),
|
|
1784
|
+
message: "DEF clause entry should describe a defined operator.",
|
|
1785
|
+
});
|
|
1786
|
+
return false;
|
|
1787
|
+
};
|
|
1788
|
+
|
|
1789
|
+
const getHiddenExpressionLocation = (
|
|
1790
|
+
fact: Expression,
|
|
1791
|
+
reference: Fact,
|
|
1792
|
+
scope: LocalName | undefined,
|
|
1793
|
+
unit: Unit,
|
|
1794
|
+
) => {
|
|
1795
|
+
const range = fact.location.range && formatRange(fact.location.range);
|
|
1796
|
+
if (selections.has(fact)) {
|
|
1797
|
+
return findNullError(nullSelections.get(fact))
|
|
1798
|
+
? BUILTIN_LOCATION
|
|
1799
|
+
: reference.location.range && formatRange(reference.location.range);
|
|
1800
|
+
}
|
|
1801
|
+
if (selectionErrors.has(fact) || fact.kind === "ExceptAt") return BUILTIN_LOCATION;
|
|
1802
|
+
switch (fact.kind) {
|
|
1803
|
+
case "Number":
|
|
1804
|
+
case "String":
|
|
1805
|
+
case "Decimal":
|
|
1806
|
+
case "Let":
|
|
1807
|
+
case "Label":
|
|
1808
|
+
return undefined;
|
|
1809
|
+
case "Identifier":
|
|
1810
|
+
case "Apply":
|
|
1811
|
+
case "Subexpression": {
|
|
1812
|
+
const first = getSelectors(fact)[0];
|
|
1813
|
+
if (fact.kind === "Subexpression" || first.kind !== "Name") return undefined;
|
|
1814
|
+
const name = fact.kind === "Identifier" ? fact.name : fact.operator;
|
|
1815
|
+
const goal = lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit);
|
|
1816
|
+
if (
|
|
1817
|
+
isFact(name, unit) ||
|
|
1818
|
+
goal ||
|
|
1819
|
+
(isStepName(name) && lookupLocal(scope, name)) ||
|
|
1820
|
+
(fact.kind === "Identifier" && isInstanceName(name, scope, unit))
|
|
1821
|
+
) {
|
|
1822
|
+
return undefined;
|
|
1823
|
+
}
|
|
1824
|
+
const binding = findBinding(name, scope, unit);
|
|
1825
|
+
const count = fact.kind === "Apply" ? fact.args.length : 0;
|
|
1826
|
+
// 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.
|
|
1827
|
+
return binding?.arity === count ? range : BUILTIN_LOCATION;
|
|
1189
1828
|
}
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1829
|
+
default:
|
|
1830
|
+
return range;
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
|
|
1834
|
+
const checkUseOrHide = (
|
|
1835
|
+
statement: UseOrHide | Extract<Proof, { kind: "By" }>,
|
|
1836
|
+
scope: LocalName | undefined,
|
|
1837
|
+
unit: Unit,
|
|
1838
|
+
) => {
|
|
1839
|
+
const references = factReferences.get(statement.facts) ?? statement.facts;
|
|
1840
|
+
const hidden = statement.facts.map((fact, index) =>
|
|
1841
|
+
checkFact(fact, references[index], scope, unit),
|
|
1842
|
+
);
|
|
1843
|
+
const factCount = statement.facts.filter(
|
|
1844
|
+
(fact) => fact.kind !== "ModuleFact" || unit.isModuleKnown(fact.name, fact.location),
|
|
1845
|
+
).length;
|
|
1846
|
+
const defCount = statement.defs.filter((def) => isDefinitionEntry(def, scope, unit)).length;
|
|
1847
|
+
hidden.forEach((location) => {
|
|
1848
|
+
if (location && statement.kind === "Hide") {
|
|
1193
1849
|
unit.errors.push({
|
|
1194
|
-
location
|
|
1195
|
-
message:
|
|
1850
|
+
location,
|
|
1851
|
+
message:
|
|
1852
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1196
1853
|
});
|
|
1197
|
-
}
|
|
1854
|
+
}
|
|
1198
1855
|
});
|
|
1856
|
+
const range = statement.location.range;
|
|
1857
|
+
if (factCount + defCount === 0 && range) {
|
|
1858
|
+
unit.errors.push({
|
|
1859
|
+
location: formatRange(range),
|
|
1860
|
+
message: statement.kind === "By" ? "Empty BY" : "Empty USE or HIDE statement.",
|
|
1861
|
+
});
|
|
1862
|
+
}
|
|
1863
|
+
};
|
|
1199
1864
|
|
|
1200
1865
|
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
1201
1866
|
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
@@ -1244,7 +1909,7 @@ const resolveStatement = (
|
|
|
1244
1909
|
|
|
1245
1910
|
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
|
|
1246
1911
|
const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
|
|
1247
|
-
if (proof?.kind === "By")
|
|
1912
|
+
if (proof?.kind === "By") checkUseOrHide(proof, scope, unit);
|
|
1248
1913
|
if (proof?.kind !== "Steps") return;
|
|
1249
1914
|
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
1250
1915
|
};
|
|
@@ -1261,6 +1926,42 @@ const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined,
|
|
|
1261
1926
|
);
|
|
1262
1927
|
};
|
|
1263
1928
|
|
|
1929
|
+
const getStepGoal = (body: StepBody, location: SourceLocation): Goal | undefined => {
|
|
1930
|
+
const createGoal = (keyword: string, statement: Expression) => ({
|
|
1931
|
+
statement,
|
|
1932
|
+
isSuffices: false,
|
|
1933
|
+
isInProof: false,
|
|
1934
|
+
keyword,
|
|
1935
|
+
});
|
|
1936
|
+
switch (body.kind) {
|
|
1937
|
+
case "Have":
|
|
1938
|
+
return createGoal("HAVE", { kind: "Tuple", elements: [body.expression], location });
|
|
1939
|
+
case "Case":
|
|
1940
|
+
return createGoal("CASE", { kind: "Tuple", elements: [body.expression], location });
|
|
1941
|
+
case "Witness":
|
|
1942
|
+
return createGoal("WITNESS", { kind: "Tuple", elements: body.expressions, location });
|
|
1943
|
+
case "Take":
|
|
1944
|
+
return createGoal("TAKE", { kind: "Tuple", elements: getBoundSets(body.bounds), location });
|
|
1945
|
+
case "Pick":
|
|
1946
|
+
return createGoal(
|
|
1947
|
+
"PICK",
|
|
1948
|
+
body.bounds.length > 0
|
|
1949
|
+
? { kind: "Exists", bounds: body.bounds, body: body.body, location }
|
|
1950
|
+
: {
|
|
1951
|
+
kind: "UnboundedQuantifier",
|
|
1952
|
+
quantifier: "Exists",
|
|
1953
|
+
names: body.names,
|
|
1954
|
+
body: body.body,
|
|
1955
|
+
location,
|
|
1956
|
+
},
|
|
1957
|
+
);
|
|
1958
|
+
case "Qed":
|
|
1959
|
+
return createGoal("QED step", { kind: "Tuple", elements: [], location });
|
|
1960
|
+
default:
|
|
1961
|
+
return undefined;
|
|
1962
|
+
}
|
|
1963
|
+
};
|
|
1964
|
+
|
|
1264
1965
|
const resolveStep = (
|
|
1265
1966
|
step: ProofStep,
|
|
1266
1967
|
scope: LocalName | undefined,
|
|
@@ -1271,12 +1972,16 @@ const resolveStep = (
|
|
|
1271
1972
|
step.name === undefined
|
|
1272
1973
|
? unit
|
|
1273
1974
|
: { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
|
|
1975
|
+
const goal = getStepGoal(body, step.location);
|
|
1274
1976
|
const bindStep = (parent: LocalName | undefined): LocalName | undefined =>
|
|
1275
|
-
step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent };
|
|
1276
|
-
const bindNonFactStep = (
|
|
1977
|
+
step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent, goal };
|
|
1978
|
+
const bindNonFactStep = (
|
|
1979
|
+
parent: LocalName | undefined,
|
|
1980
|
+
nonFactStep: LocalName["nonFactStep"],
|
|
1981
|
+
): LocalName | undefined =>
|
|
1277
1982
|
step.name === undefined
|
|
1278
1983
|
? parent
|
|
1279
|
-
: { name: step.name, arity: 0, kind: "Theorem", parent,
|
|
1984
|
+
: { name: step.name, arity: 0, kind: "Theorem", parent, nonFactStep };
|
|
1280
1985
|
switch (body.kind) {
|
|
1281
1986
|
case "Assert": {
|
|
1282
1987
|
const assumed = resolveStatement(body.statement, scope, stepUnit);
|
|
@@ -1286,17 +1991,32 @@ const resolveStep = (
|
|
|
1286
1991
|
resolveProof(step.proof, proofScope, unit);
|
|
1287
1992
|
return body.isSuffices ? assumed : scope;
|
|
1288
1993
|
}
|
|
1289
|
-
const
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1994
|
+
const assertion = {
|
|
1995
|
+
statement: body.statement,
|
|
1996
|
+
isSuffices: body.isSuffices,
|
|
1997
|
+
isInProof: true,
|
|
1998
|
+
keyword: body.isSuffices && body.statement.kind !== "AssumeProve" ? "SUFFICES" : undefined,
|
|
1999
|
+
};
|
|
2000
|
+
resolveProof(
|
|
2001
|
+
step.proof,
|
|
2002
|
+
{ name, arity: 0, kind: "Theorem", parent: proofScope, goal: assertion },
|
|
2003
|
+
unit,
|
|
2004
|
+
);
|
|
2005
|
+
assertion.isInProof = false;
|
|
2006
|
+
return {
|
|
2007
|
+
name,
|
|
2008
|
+
arity: 0,
|
|
2009
|
+
kind: "Theorem",
|
|
2010
|
+
parent: body.isSuffices ? assumed : scope,
|
|
2011
|
+
goal: assertion,
|
|
2012
|
+
};
|
|
1293
2013
|
}
|
|
1294
2014
|
case "Use":
|
|
1295
2015
|
case "Hide":
|
|
1296
|
-
|
|
1297
|
-
return bindNonFactStep(scope);
|
|
2016
|
+
checkUseOrHide(body, scope, unit);
|
|
2017
|
+
return bindNonFactStep(scope, body.kind);
|
|
1298
2018
|
case "Instance":
|
|
1299
|
-
return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
|
|
2019
|
+
return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit), body.kind);
|
|
1300
2020
|
case "Define":
|
|
1301
2021
|
return bindNonFactStep(
|
|
1302
2022
|
body.definitions.reduce(
|
|
@@ -1306,6 +2026,7 @@ const resolveStep = (
|
|
|
1306
2026
|
: resolveDefinitions([definition], current, unit),
|
|
1307
2027
|
scope,
|
|
1308
2028
|
),
|
|
2029
|
+
body.kind,
|
|
1309
2030
|
);
|
|
1310
2031
|
case "Have":
|
|
1311
2032
|
case "Case":
|
|
@@ -1378,6 +2099,7 @@ export interface ModuleUnit {
|
|
|
1378
2099
|
bound: Symbols;
|
|
1379
2100
|
definitions: Map<string, Definition>;
|
|
1380
2101
|
getKey: (definition: Definition) => string;
|
|
2102
|
+
isModuleKnown: (name: string, location: SourceLocation) => boolean;
|
|
1381
2103
|
instanced: Map<Instance, Symbols>;
|
|
1382
2104
|
facts: Set<string>;
|
|
1383
2105
|
reports: ModuleReport[];
|
|
@@ -1416,18 +2138,74 @@ const checkSubstitutions = (
|
|
|
1416
2138
|
(parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
|
|
1417
2139
|
outer,
|
|
1418
2140
|
);
|
|
2141
|
+
const reportArity = (declaration: Declaration, location: string) =>
|
|
2142
|
+
unit.errors.push({
|
|
2143
|
+
location,
|
|
2144
|
+
message: `An operator must be substituted for symbol '${resolveSynonym(declaration.name)}', and it must have arity ${declaration.arity}.`,
|
|
2145
|
+
});
|
|
1419
2146
|
const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
|
|
1420
|
-
|
|
1421
|
-
|
|
2147
|
+
const range = expression.location.range;
|
|
2148
|
+
if (declaration.arity === 0) {
|
|
2149
|
+
resolveExpressionArgument(expression, scope, unit);
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
const parenthesized = parenthesizedRanges.get(expression);
|
|
2153
|
+
if (expression.kind === "Lambda" && !parenthesized) {
|
|
2154
|
+
resolveLambda(expression, scope, unit);
|
|
2155
|
+
if (expression.params.length !== declaration.arity && range) {
|
|
2156
|
+
reportArity(declaration, formatRange(range));
|
|
2157
|
+
}
|
|
2158
|
+
return;
|
|
2159
|
+
}
|
|
2160
|
+
if (expression.kind === "OperatorReference" && !parenthesized) {
|
|
2161
|
+
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
2162
|
+
if (arity === undefined) reportArity(declaration, BUILTIN_LOCATION);
|
|
2163
|
+
else if (arity !== declaration.arity && range) reportArity(declaration, formatRange(range));
|
|
2164
|
+
return;
|
|
2165
|
+
}
|
|
2166
|
+
if (expression.kind !== "Identifier" || parenthesized) {
|
|
2167
|
+
const errorRange = parenthesized ?? range;
|
|
2168
|
+
if (errorRange) {
|
|
2169
|
+
unit.errors.push({
|
|
2170
|
+
location: formatRange(errorRange),
|
|
2171
|
+
message: `Arity ${declaration.arity} operator (not an expression) is expected \nto substitute for CONSTANT '${resolveSynonym(declaration.name)}'.`,
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
reportArity(declaration, BUILTIN_LOCATION);
|
|
1422
2175
|
return;
|
|
1423
2176
|
}
|
|
1424
2177
|
const arity = getSymbolArity(expression, expression.name, scope, unit);
|
|
1425
2178
|
if (arity !== undefined && arity !== declaration.arity) {
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
2179
|
+
unit.errors.push({
|
|
2180
|
+
location: formatRange(getSpelledNameRange(expression, expression.name)),
|
|
2181
|
+
message: `Expected arity ${declaration.arity} but found operator of arity ${arity}.`,
|
|
2182
|
+
});
|
|
1429
2183
|
}
|
|
2184
|
+
if (arity !== declaration.arity) reportArity(declaration, BUILTIN_LOCATION);
|
|
1430
2185
|
};
|
|
2186
|
+
const instanceRange = instance.location.range;
|
|
2187
|
+
const isFound = (declaration: Declaration) =>
|
|
2188
|
+
lookupLocal(scope, declaration.name) !== undefined ||
|
|
2189
|
+
findSymbol(declaration.name, unit) !== undefined;
|
|
2190
|
+
const implicit = declarations.filter(isFound).map((declaration) => {
|
|
2191
|
+
const expression: Expression & { kind: "Identifier" } = {
|
|
2192
|
+
kind: "Identifier",
|
|
2193
|
+
name: declaration.name,
|
|
2194
|
+
location: instance.location,
|
|
2195
|
+
};
|
|
2196
|
+
return { declaration, expression };
|
|
2197
|
+
});
|
|
2198
|
+
implicit
|
|
2199
|
+
.filter((item) => item.declaration.arity === 0)
|
|
2200
|
+
.forEach((item) => {
|
|
2201
|
+
const arity = getSymbolArity(item.expression, item.declaration.name, scope, unit);
|
|
2202
|
+
if (arity && instanceRange) {
|
|
2203
|
+
unit.errors.push({
|
|
2204
|
+
location: formatRange(instanceRange),
|
|
2205
|
+
message: `Wrong number of arguments (0) given to operator '${resolveSynonym(item.declaration.name)}', \nwhich requires ${arity} arguments.`,
|
|
2206
|
+
});
|
|
2207
|
+
}
|
|
2208
|
+
});
|
|
1431
2209
|
const substitutions = new Map<string, Expression>();
|
|
1432
2210
|
instance.substitutions.forEach((substitution) => {
|
|
1433
2211
|
const name = substitution.name;
|
|
@@ -1450,30 +2228,29 @@ const checkSubstitutions = (
|
|
|
1450
2228
|
});
|
|
1451
2229
|
}
|
|
1452
2230
|
});
|
|
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
2231
|
implicit
|
|
1457
|
-
.filter((
|
|
2232
|
+
.filter((item) => !substitutions.has(item.declaration.name))
|
|
2233
|
+
.forEach((item) => {
|
|
2234
|
+
const arity =
|
|
2235
|
+
item.declaration.arity === 0
|
|
2236
|
+
? 0
|
|
2237
|
+
: getSymbolArity(item.expression, item.declaration.name, scope, unit);
|
|
2238
|
+
if (arity !== item.declaration.arity && instanceRange) {
|
|
2239
|
+
reportArity(item.declaration, formatRange(instanceRange));
|
|
2240
|
+
}
|
|
2241
|
+
substitutions.set(item.declaration.name, item.expression);
|
|
2242
|
+
});
|
|
2243
|
+
declarations
|
|
2244
|
+
.filter((declaration) => !substitutions.has(declaration.name))
|
|
1458
2245
|
.forEach((declaration) => {
|
|
1459
|
-
const
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
2246
|
+
const declared = declaration.location.range;
|
|
2247
|
+
if (instanceRange && declared) {
|
|
2248
|
+
unit.errors.push({
|
|
2249
|
+
location: formatRange(instanceRange),
|
|
2250
|
+
message: `Substitution missing for symbol ${resolveSynonym(declaration.name)} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
1466
2253
|
});
|
|
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
2254
|
return substitutions;
|
|
1478
2255
|
};
|
|
1479
2256
|
|
|
@@ -1481,7 +2258,7 @@ const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit:
|
|
|
1481
2258
|
const instanced = unit.getInstance(instance);
|
|
1482
2259
|
checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
|
|
1483
2260
|
return [...instanced.symbols].reduce(
|
|
1484
|
-
(parent,
|
|
2261
|
+
(parent, entry) => bindName(parent, entry[0], entry[1], unit),
|
|
1485
2262
|
scope,
|
|
1486
2263
|
);
|
|
1487
2264
|
};
|
|
@@ -1493,6 +2270,12 @@ export const resolveSubstitutions = (
|
|
|
1493
2270
|
unit: ModuleUnit,
|
|
1494
2271
|
) => {
|
|
1495
2272
|
const imported = createSymbols(module, unit.imported);
|
|
2273
|
+
const positions = new Map(
|
|
2274
|
+
[...module.constants, ...module.variables].map((declaration) => [
|
|
2275
|
+
declaration.name,
|
|
2276
|
+
declaration.location,
|
|
2277
|
+
]),
|
|
2278
|
+
);
|
|
1496
2279
|
const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
|
|
1497
2280
|
unit.reports.push(report);
|
|
1498
2281
|
return checkSubstitutions(instance, declarations, module.name, undefined, {
|
|
@@ -1500,12 +2283,16 @@ export const resolveSubstitutions = (
|
|
|
1500
2283
|
getInstance: unit.getInstance,
|
|
1501
2284
|
definitions: unit.definitions,
|
|
1502
2285
|
getKey: unit.getKey,
|
|
2286
|
+
isModuleKnown: unit.isModuleKnown,
|
|
1503
2287
|
location: instance.location,
|
|
1504
2288
|
self: undefined,
|
|
1505
2289
|
facts: new Set(),
|
|
1506
2290
|
goals: new Map(),
|
|
1507
2291
|
bindings: new Map(
|
|
1508
|
-
[...imported].map((
|
|
2292
|
+
[...imported].map((entry) => [
|
|
2293
|
+
entry[0],
|
|
2294
|
+
{ ...entry[1], position: positions.get(entry[0]) ?? MODULE_START },
|
|
2295
|
+
]),
|
|
1509
2296
|
),
|
|
1510
2297
|
errors: report.errors,
|
|
1511
2298
|
warnings: report.warnings,
|
|
@@ -1535,13 +2322,7 @@ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError
|
|
|
1535
2322
|
}
|
|
1536
2323
|
};
|
|
1537
2324
|
units
|
|
1538
|
-
.toSorted((left, right) =>
|
|
1539
|
-
isBefore(left.location, right.location)
|
|
1540
|
-
? -1
|
|
1541
|
-
: isBefore(right.location, left.location)
|
|
1542
|
-
? 1
|
|
1543
|
-
: 0,
|
|
1544
|
-
)
|
|
2325
|
+
.toSorted((left, right) => compareLocations(left.location, right.location))
|
|
1545
2326
|
.forEach((unit) => {
|
|
1546
2327
|
const reported: SanyError[] = [];
|
|
1547
2328
|
const warned: SanyError[] = [];
|
|
@@ -1561,21 +2342,27 @@ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError
|
|
|
1561
2342
|
|
|
1562
2343
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
1563
2344
|
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
2345
|
const goals = new Map(
|
|
1569
2346
|
module.theorems.flatMap((theorem): [string, Goal][] =>
|
|
1570
2347
|
theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
|
|
1571
|
-
? [
|
|
2348
|
+
? [
|
|
2349
|
+
[
|
|
2350
|
+
theorem.name,
|
|
2351
|
+
{
|
|
2352
|
+
statement: theorem.statement,
|
|
2353
|
+
isSuffices: false,
|
|
2354
|
+
isInProof: false,
|
|
2355
|
+
keyword: undefined,
|
|
2356
|
+
},
|
|
2357
|
+
],
|
|
2358
|
+
]
|
|
1572
2359
|
: [],
|
|
1573
2360
|
),
|
|
1574
2361
|
);
|
|
1575
2362
|
const bindings = new Map<string, ModuleBinding>(
|
|
1576
|
-
[...CORE_SYMBOLS, ...unit.bound].map((
|
|
1577
|
-
|
|
1578
|
-
{ ...
|
|
2363
|
+
[...CORE_SYMBOLS, ...unit.bound].map((entry) => [
|
|
2364
|
+
entry[0],
|
|
2365
|
+
{ ...entry[1], position: MODULE_START },
|
|
1579
2366
|
]),
|
|
1580
2367
|
);
|
|
1581
2368
|
const recursive = new Set<Binding>();
|
|
@@ -1593,9 +2380,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1593
2380
|
getInstance: unit.getInstance,
|
|
1594
2381
|
definitions: unit.definitions,
|
|
1595
2382
|
getKey: unit.getKey,
|
|
2383
|
+
isModuleKnown: unit.isModuleKnown,
|
|
1596
2384
|
location,
|
|
1597
2385
|
self,
|
|
1598
|
-
facts,
|
|
2386
|
+
facts: unit.facts,
|
|
1599
2387
|
goals,
|
|
1600
2388
|
bindings,
|
|
1601
2389
|
errors,
|
|
@@ -1620,6 +2408,37 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1620
2408
|
);
|
|
1621
2409
|
return collectSemanticErrors(
|
|
1622
2410
|
[
|
|
2411
|
+
...module.units.flatMap((item): SemanticUnit[] => {
|
|
2412
|
+
const section =
|
|
2413
|
+
item.kind === "Variables"
|
|
2414
|
+
? { type: "A VARIABLE declaration", location: item.location }
|
|
2415
|
+
: item.kind === "Constants"
|
|
2416
|
+
? { type: "A declaration", location: item.location }
|
|
2417
|
+
: item.kind === "Module"
|
|
2418
|
+
? { type: "A MODULE ", location: item.module.location }
|
|
2419
|
+
: item.kind === "Theorem"
|
|
2420
|
+
? { type: "A THEOREM", location: item.theorem.location }
|
|
2421
|
+
: item.kind === "Assumption"
|
|
2422
|
+
? { type: "An ASSUME", location: item.assumption.location }
|
|
2423
|
+
: item.kind === "Use"
|
|
2424
|
+
? { type: "A USE or HIDE", location: item.use.location }
|
|
2425
|
+
: undefined;
|
|
2426
|
+
const range = section?.location.range;
|
|
2427
|
+
if (!section || !range) return [];
|
|
2428
|
+
return [
|
|
2429
|
+
{
|
|
2430
|
+
location: section.location,
|
|
2431
|
+
check: (errors) => {
|
|
2432
|
+
if (declarations.some((declared) => declared.binding?.isPending)) {
|
|
2433
|
+
errors.push({
|
|
2434
|
+
location: formatRange(range),
|
|
2435
|
+
message: `${section.type} may not appear within a recursive definition section.`,
|
|
2436
|
+
});
|
|
2437
|
+
}
|
|
2438
|
+
},
|
|
2439
|
+
},
|
|
2440
|
+
];
|
|
2441
|
+
}),
|
|
1623
2442
|
...unit.reports.map((report) => ({
|
|
1624
2443
|
location: report.location,
|
|
1625
2444
|
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
@@ -1636,6 +2455,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1636
2455
|
const binding: ModuleBinding = {
|
|
1637
2456
|
kind: "Definition",
|
|
1638
2457
|
arity: declaration.arity,
|
|
2458
|
+
params: Array(declaration.arity).fill(0),
|
|
1639
2459
|
range: declaration.location.range,
|
|
1640
2460
|
position: declaration.location,
|
|
1641
2461
|
isPending: true,
|
|
@@ -1701,7 +2521,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1701
2521
|
),
|
|
1702
2522
|
...module.uses.map((use) =>
|
|
1703
2523
|
createCheck(use.location, undefined, false, (inner) =>
|
|
1704
|
-
|
|
2524
|
+
checkUseOrHide(use, undefined, inner),
|
|
1705
2525
|
),
|
|
1706
2526
|
),
|
|
1707
2527
|
{
|
|
@@ -1712,7 +2532,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
1712
2532
|
if (range && (!item.binding || item.binding.isPending)) {
|
|
1713
2533
|
errors.push({
|
|
1714
2534
|
location: formatRange(range),
|
|
1715
|
-
message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
|
|
2535
|
+
message: `Symbol ${resolveSynonym(item.declaration.name)} declared in RECURSIVE statement but not defined.`,
|
|
1716
2536
|
});
|
|
1717
2537
|
}
|
|
1718
2538
|
}),
|