tla-ts 0.0.3 → 0.0.4-dev.1a3acf6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -115
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +194 -8
- package/dist/index.d.ts +194 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +1006 -0
- package/dist/prove.js +1006 -0
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +1377 -18
- package/dist/transfer.js +1377 -18
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +24 -11
- package/src/ast.ts +149 -8
- package/src/bags.ts +26 -30
- package/src/builtins.ts +45 -19
- package/src/bundled-modules.ts +3 -3
- package/src/check.ts +1182 -900
- package/src/cli/options.ts +109 -0
- package/src/cli/run.ts +63 -0
- package/src/cli/shared.ts +106 -0
- package/src/cli/tlapm.ts +374 -0
- package/src/cli/tlc.ts +941 -0
- package/src/cli/xml-exporter.ts +221 -0
- package/src/cli.ts +9 -2
- package/src/codegen.ts +112 -51
- package/src/config.ts +254 -181
- package/src/dependencies.ts +5 -7
- package/src/environment.ts +579 -184
- package/src/errors.ts +287 -4
- package/src/evaluate.ts +1095 -365
- package/src/exit-codes.ts +22 -43
- package/src/fingerprints.ts +49 -14
- package/src/index.ts +11 -0
- package/src/instances.ts +73 -60
- package/src/java-overrides.ts +183 -66
- package/src/json.ts +31 -36
- package/src/levels.ts +197 -54
- package/src/lexer.ts +127 -30
- package/src/liveness-checker.ts +894 -0
- package/src/liveness.ts +246 -596
- package/src/{obligation-action.ts → obligations/action.ts} +248 -74
- package/src/{obligation-anon.ts → obligations/anonymize.ts} +55 -57
- package/src/obligations/equality.ts +260 -0
- package/src/obligations/expression.ts +618 -0
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/obligations/generate.ts +457 -0
- package/src/{obligations.ts → obligations/index.ts} +46 -32
- package/src/{obligation-levels.ts → obligations/levels.ts} +44 -40
- package/src/obligations/module.ts +445 -0
- package/src/obligations/normalize.ts +554 -0
- package/src/obligations/operators.ts +176 -0
- package/src/obligations/pragma.ts +195 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +65 -230
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/obligations/regroup.ts +100 -0
- package/src/{obligation-generate.ts → obligations/simplify.ts} +130 -536
- package/src/{obligation-source.ts → obligations/source.ts} +174 -216
- package/src/{obligation-deref.ts → obligations/subexpression.ts} +84 -115
- package/src/obligations/substitution.ts +261 -0
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/parallel-worker.ts +184 -235
- package/src/parallel.ts +156 -91
- package/src/parser.ts +490 -188
- package/src/pcal/ast.ts +755 -0
- package/src/pcal/parse.ts +1487 -0
- package/src/pcal/tokenize.ts +487 -0
- package/src/pcal/translate.ts +1151 -0
- package/src/pcal/validate.ts +444 -0
- package/src/prover/decide-pool.ts +95 -0
- package/src/prover/deciders.ts +33 -0
- package/src/prover/first-order.ts +175 -0
- package/src/prover/isabelle/LICENSE +36 -0
- package/src/prover/isabelle/LICENSE-tlapm +26 -0
- package/src/prover/isabelle/decide.ts +44 -0
- package/src/prover/isabelle/encode.ts +445 -0
- package/src/prover/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prover/isabelle/isar/method.ts +367 -0
- package/src/prover/isabelle/isar/proof.ts +28 -0
- package/src/prover/isabelle/isar/statement.ts +57 -0
- package/src/prover/isabelle/isar/theory.ts +192 -0
- package/src/prover/isabelle/isar/type-check.ts +278 -0
- package/src/prover/isabelle/provers/blast/blast.ts +258 -0
- package/src/prover/isabelle/provers/blast/branch.ts +322 -0
- package/src/prover/isabelle/provers/blast/prove.ts +347 -0
- package/src/prover/isabelle/provers/blast/rules.ts +330 -0
- package/src/prover/isabelle/provers/blast/state.ts +180 -0
- package/src/prover/isabelle/provers/blast/term.ts +546 -0
- package/src/prover/isabelle/provers/clasimp.ts +150 -0
- package/src/prover/isabelle/provers/classical.ts +389 -0
- package/src/prover/isabelle/provers/hypsubst.ts +433 -0
- package/src/prover/isabelle/pure/bicompose.ts +174 -0
- package/src/prover/isabelle/pure/consts.ts +32 -0
- package/src/prover/isabelle/pure/conv.ts +79 -0
- package/src/prover/isabelle/pure/drule.ts +264 -0
- package/src/prover/isabelle/pure/envir.ts +331 -0
- package/src/prover/isabelle/pure/goal.ts +53 -0
- package/src/prover/isabelle/pure/interrupt.ts +16 -0
- package/src/prover/isabelle/pure/list.ts +12 -0
- package/src/prover/isabelle/pure/logic.ts +330 -0
- package/src/prover/isabelle/pure/name.ts +98 -0
- package/src/prover/isabelle/pure/net.ts +215 -0
- package/src/prover/isabelle/pure/object-logic.ts +41 -0
- package/src/prover/isabelle/pure/pattern.ts +631 -0
- package/src/prover/isabelle/pure/raw-simplifier.ts +338 -0
- package/src/prover/isabelle/pure/rewriter.ts +602 -0
- package/src/prover/isabelle/pure/search.ts +68 -0
- package/src/prover/isabelle/pure/seq.ts +133 -0
- package/src/prover/isabelle/pure/simplifier.ts +106 -0
- package/src/prover/isabelle/pure/table.ts +201 -0
- package/src/prover/isabelle/pure/tactic.ts +168 -0
- package/src/prover/isabelle/pure/tactical.ts +264 -0
- package/src/prover/isabelle/pure/term-ord.ts +105 -0
- package/src/prover/isabelle/pure/term.ts +688 -0
- package/src/prover/isabelle/pure/thm.ts +783 -0
- package/src/prover/isabelle/pure/type.ts +121 -0
- package/src/prover/isabelle/pure/unify.ts +753 -0
- package/src/prover/isabelle/pure/variable.ts +25 -0
- package/src/prover/isabelle/tla/classical-setup.ts +101 -0
- package/src/prover/isabelle/tla/context.ts +74 -0
- package/src/prover/isabelle/tla/logic-data.ts +5 -0
- package/src/prover/isabelle/tla/logic.ts +215 -0
- package/src/prover/isabelle/tla/simpdata.ts +241 -0
- package/src/prover/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prover/ocaml.ts +22 -0
- package/src/prover/prep/action.ts +508 -0
- package/src/prover/prep/enabled-axioms.ts +189 -0
- package/src/prover/prep/fingerprint.ts +388 -0
- package/src/prover/prep/level-comparison.ts +529 -0
- package/src/prover/prep/md5.ts +60 -0
- package/src/prover/prep/methods.ts +221 -0
- package/src/prover/prep/normalize.ts +309 -0
- package/src/prover/prove-worker.ts +22 -0
- package/src/prover/prove.ts +492 -0
- package/src/prover/ptl/decide.ts +68 -0
- package/src/prover/ptl/encode.ts +244 -0
- package/src/prover/ptl/sat.ts +151 -0
- package/src/prover/ptl/tableau.ts +276 -0
- package/src/prover/smt/axioms.ts +209 -0
- package/src/prover/smt/context.ts +108 -0
- package/src/prover/smt/data.ts +636 -0
- package/src/prover/smt/encode.ts +275 -0
- package/src/prover/smt/extensionality.ts +65 -0
- package/src/prover/smt/flatten.ts +112 -0
- package/src/prover/smt/format.ts +154 -0
- package/src/prover/smt/instances.ts +181 -0
- package/src/prover/smt/rewrite.ts +766 -0
- package/src/prover/smt/solver/act-cache.ts +265 -0
- package/src/prover/smt/solver/arith-rewriter.ts +962 -0
- package/src/prover/smt/solver/array.ts +22 -0
- package/src/prover/smt/solver/asserted-formulas.ts +469 -0
- package/src/prover/smt/solver/ast-lt.ts +157 -0
- package/src/prover/smt/solver/ast-util.ts +91 -0
- package/src/prover/smt/solver/ast.ts +1315 -0
- package/src/prover/smt/solver/auf-solver.ts +572 -0
- package/src/prover/smt/solver/case-split-queue.ts +178 -0
- package/src/prover/smt/solver/cg-table.ts +175 -0
- package/src/prover/smt/solver/clause.ts +276 -0
- package/src/prover/smt/solver/conflict-resolution.ts +343 -0
- package/src/prover/smt/solver/context.ts +1292 -0
- package/src/prover/smt/solver/ctx-simplify.ts +334 -0
- package/src/prover/smt/solver/der.ts +224 -0
- package/src/prover/smt/solver/dyn-ack.ts +141 -0
- package/src/prover/smt/solver/e-matching.ts +659 -0
- package/src/prover/smt/solver/enode.ts +259 -0
- package/src/prover/smt/solver/expr-ref.ts +21 -0
- package/src/prover/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prover/smt/solver/fingerprints.ts +92 -0
- package/src/prover/smt/solver/flat-literals.ts +51 -0
- package/src/prover/smt/solver/goal.ts +138 -0
- package/src/prover/smt/solver/hash.ts +173 -0
- package/src/prover/smt/solver/hashtable.ts +166 -0
- package/src/prover/smt/solver/internalizer.ts +1381 -0
- package/src/prover/smt/solver/lbool.ts +5 -0
- package/src/prover/smt/solver/literal.ts +24 -0
- package/src/prover/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prover/smt/solver/lp/constraints.ts +78 -0
- package/src/prover/smt/solver/lp/containers.ts +296 -0
- package/src/prover/smt/solver/lp/core-solver.ts +504 -0
- package/src/prover/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prover/smt/solver/lp/dep-intervals.ts +642 -0
- package/src/prover/smt/solver/lp/dependency.ts +51 -0
- package/src/prover/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prover/smt/solver/lp/explanation.ts +46 -0
- package/src/prover/smt/solver/lp/gomory.ts +255 -0
- package/src/prover/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prover/smt/solver/lp/hnf.ts +281 -0
- package/src/prover/smt/solver/lp/int-cube.ts +295 -0
- package/src/prover/smt/solver/lp/int-solver.ts +514 -0
- package/src/prover/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prover/smt/solver/lp/lar-term.ts +62 -0
- package/src/prover/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prover/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prover/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prover/smt/solver/lp/nex.ts +120 -0
- package/src/prover/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prover/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prover/smt/solver/lp/nla-core.ts +968 -0
- package/src/prover/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prover/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prover/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prover/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prover/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prover/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prover/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prover/smt/solver/lp/nla-order.ts +307 -0
- package/src/prover/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prover/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prover/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prover/smt/solver/lp/nla-types.ts +124 -0
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prover/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prover/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prover/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prover/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prover/smt/solver/lp/pdd.ts +971 -0
- package/src/prover/smt/solver/lp/random-updater.ts +138 -0
- package/src/prover/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prover/smt/solver/lp/union-find.ts +96 -0
- package/src/prover/smt/solver/mam-compiler.ts +847 -0
- package/src/prover/smt/solver/mam-interpreter.ts +400 -0
- package/src/prover/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prover/smt/solver/model-checker.ts +347 -0
- package/src/prover/smt/solver/model-evaluator.ts +262 -0
- package/src/prover/smt/solver/model-finder.ts +155 -0
- package/src/prover/smt/solver/model-generator.ts +97 -0
- package/src/prover/smt/solver/model-macro-solver.ts +728 -0
- package/src/prover/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prover/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prover/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prover/smt/solver/nlsat/explain.ts +607 -0
- package/src/prover/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prover/smt/solver/nlsat/levelwise.ts +737 -0
- package/src/prover/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prover/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prover/smt/solver/nlsat/solver.ts +911 -0
- package/src/prover/smt/solver/nlsat/types.ts +43 -0
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prover/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prover/smt/solver/nnf.ts +469 -0
- package/src/prover/smt/solver/pattern-inference.ts +351 -0
- package/src/prover/smt/solver/poly-rewriter.ts +477 -0
- package/src/prover/smt/solver/propagate-values.ts +83 -0
- package/src/prover/smt/solver/proto-model.ts +551 -0
- package/src/prover/smt/solver/pull-quant.ts +139 -0
- package/src/prover/smt/solver/qe-lite-der.ts +526 -0
- package/src/prover/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prover/smt/solver/qe-lite.ts +381 -0
- package/src/prover/smt/solver/qi-queue.ts +362 -0
- package/src/prover/smt/solver/quant-tactics.ts +117 -0
- package/src/prover/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prover/smt/solver/quantifier-manager.ts +186 -0
- package/src/prover/smt/solver/random-gen.ts +10 -0
- package/src/prover/smt/solver/rational.ts +165 -0
- package/src/prover/smt/solver/ref-vector.ts +62 -0
- package/src/prover/smt/solver/relevancy.ts +247 -0
- package/src/prover/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prover/smt/solver/rewriter.ts +843 -0
- package/src/prover/smt/solver/rlimit.ts +79 -0
- package/src/prover/smt/solver/smtlib-parser.ts +526 -0
- package/src/prover/smt/solver/solver.ts +71 -0
- package/src/prover/smt/solver/sort.ts +35 -0
- package/src/prover/smt/solver/sticky-generations.ts +69 -0
- package/src/prover/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prover/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prover/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prover/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prover/smt/solver/theory-lra-model.ts +243 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prover/smt/solver/theory-lra-state.ts +43 -0
- package/src/prover/smt/solver/theory-lra.ts +56 -0
- package/src/prover/smt/solver/var-subst.ts +258 -0
- package/src/prover/smt/solver/z3-license.txt +10 -0
- package/src/prover/smt/table.ts +170 -0
- package/src/prover/tlapm/cache.ts +236 -0
- package/src/prover/tlapm/ocaml-marshal.ts +166 -0
- package/src/prover/tlapm/tlapm.ts +96 -0
- package/src/prover/tlaps-module.ts +510 -0
- package/src/prover/verdict.ts +13 -0
- package/src/prover/zenon/LICENSE +32 -0
- package/src/prover/zenon/check/check.ts +105 -0
- package/src/prover/zenon/check/definitions.ts +144 -0
- package/src/prover/zenon/check/equality.ts +182 -0
- package/src/prover/zenon/check/functions.ts +62 -0
- package/src/prover/zenon/check/keys.ts +125 -0
- package/src/prover/zenon/check/propositional.ts +104 -0
- package/src/prover/zenon/check/records.ts +188 -0
- package/src/prover/zenon/check/relations.ts +121 -0
- package/src/prover/zenon/check/rewrites.ts +224 -0
- package/src/prover/zenon/check/sets.ts +302 -0
- package/src/prover/zenon/check/terms.ts +127 -0
- package/src/prover/zenon/check/tla.ts +126 -0
- package/src/prover/zenon/decide.ts +107 -0
- package/src/prover/zenon/encode.ts +378 -0
- package/src/prover/zenon/eqrel.ts +300 -0
- package/src/prover/zenon/equality-rules.ts +435 -0
- package/src/prover/zenon/expr.ts +551 -0
- package/src/prover/zenon/index-table.ts +350 -0
- package/src/prover/zenon/isar-case.ts +513 -0
- package/src/prover/zenon/isar.ts +1249 -0
- package/src/prover/zenon/limits.ts +15 -0
- package/src/prover/zenon/llproof.ts +173 -0
- package/src/prover/zenon/mltoll.ts +1566 -0
- package/src/prover/zenon/node.ts +297 -0
- package/src/prover/zenon/ocaml.ts +169 -0
- package/src/prover/zenon/parse.ts +247 -0
- package/src/prover/zenon/phrase.ts +191 -0
- package/src/prover/zenon/proof.ts +198 -0
- package/src/prover/zenon/propositional-rules.ts +387 -0
- package/src/prover/zenon/rules.ts +94 -0
- package/src/prover/zenon/search.ts +288 -0
- package/src/prover/zenon/tla/functions.ts +151 -0
- package/src/prover/zenon/tla/llproof.ts +480 -0
- package/src/prover/zenon/tla/records.ts +225 -0
- package/src/prover/zenon/tla/rewrites.ts +447 -0
- package/src/prover/zenon/tla/sets.ts +304 -0
- package/src/prover/zenon/tla/terms.ts +162 -0
- package/src/prover/zenon/tla/tla.ts +506 -0
- package/src/randomization.ts +39 -27
- package/src/resolve.ts +1474 -426
- package/src/runtime.ts +25 -7
- package/src/sany/builtins.ts +147 -0
- package/src/sany/generator.ts +3111 -0
- package/src/sany/levels.ts +1205 -0
- package/src/sany/lexer.ts +544 -0
- package/src/sany/linter.ts +122 -0
- package/src/sany/modules.ts +1398 -0
- package/src/sany/nodes.ts +463 -0
- package/src/sany/parse-error.ts +2161 -0
- package/src/sany/report.ts +355 -0
- package/src/sany/selectors.ts +736 -0
- package/src/sany/source.ts +247 -0
- package/src/sany/xml.ts +1016 -0
- package/src/search.ts +969 -0
- package/src/spec-description.ts +66 -29
- package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
- package/src/standard-modules.ts +215 -164
- package/src/state-graph.ts +181 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +40 -19
- package/src/successors.ts +253 -125
- package/src/symmetry.ts +194 -54
- package/src/tableau.ts +60 -32
- package/src/tlc/fingerprint.ts +214 -0
- package/src/{tlc-modules.ts → tlc/jar-data.ts} +4 -0
- package/src/tlc/messages.ts +441 -0
- package/src/{tlc-order.ts → tlc/order.ts} +84 -16
- package/src/{tlc-random.ts → tlc/random.ts} +30 -189
- package/src/tlc/replay.ts +659 -0
- package/src/{tlc-summary.ts → tlc/summary.ts} +20 -5
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +59 -50
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +63 -45
- package/src/tlc/version.ts +19 -0
- package/src/transfer.ts +44 -32
- package/src/values.ts +843 -241
- package/src/worker.ts +62 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- package/dist/run-cli.cjs +0 -28
- package/dist/run-cli.js +0 -28
- package/src/canonical.ts +0 -39
- package/src/cli-options.ts +0 -58
- package/src/obligation-expression.ts +0 -1060
- package/src/obligation-module.ts +0 -1042
- package/src/run-cli.ts +0 -478
package/src/resolve.ts
CHANGED
|
@@ -1,46 +1,124 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type AssumeProve,
|
|
5
|
+
type Bound,
|
|
6
|
+
type BoundPattern,
|
|
7
|
+
type Declaration,
|
|
8
|
+
type Definition,
|
|
9
|
+
type Expression,
|
|
10
|
+
type Fact,
|
|
11
|
+
type Instance,
|
|
12
|
+
type LabelExpression,
|
|
13
|
+
type Module,
|
|
14
|
+
type NewSymbol,
|
|
15
|
+
type OperatorParam,
|
|
16
|
+
type Proof,
|
|
17
|
+
type ProofStep,
|
|
18
|
+
type Selector,
|
|
19
|
+
type SourceLocation,
|
|
20
|
+
type SourceRange,
|
|
21
|
+
type Theorem,
|
|
15
22
|
} from "./ast";
|
|
16
|
-
import { TlaSyntaxError } from "./errors";
|
|
17
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
23
|
+
import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
25
|
+
// The kinds of symbol SANY tells apart when it binds a name twice. A parameter is also a bound name,
|
|
26
|
+
// and a NEW symbol has a kind for each level.
|
|
27
|
+
export type SymbolKind =
|
|
28
|
+
| "Builtin"
|
|
29
|
+
| "Constant"
|
|
30
|
+
| "Variable"
|
|
31
|
+
| "Definition"
|
|
32
|
+
| "Instance"
|
|
33
|
+
| "Theorem"
|
|
34
|
+
| "Parameter"
|
|
35
|
+
| `New${NewSymbol["level"]}`;
|
|
36
|
+
|
|
37
|
+
// range is where SANY reports the symbol, and two symbols at one range are one node to SANY. params
|
|
38
|
+
// holds the arity of each parameter of a defined operator.
|
|
39
|
+
interface Binding {
|
|
40
|
+
kind: SymbolKind;
|
|
21
41
|
arity: number;
|
|
22
|
-
|
|
42
|
+
params?: number[];
|
|
43
|
+
range?: SourceRange;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ModuleSymbol extends Binding {
|
|
47
|
+
key: string;
|
|
23
48
|
}
|
|
24
49
|
|
|
25
50
|
export type Symbols = Map<string, ModuleSymbol>;
|
|
26
51
|
|
|
27
|
-
|
|
52
|
+
// A named theorem or proof step whose parts a name such as foo!lab or <1>2!1 selects. isInProof
|
|
53
|
+
// holds while SANY analyzes the proof of the theorem or step.
|
|
54
|
+
interface Goal {
|
|
55
|
+
statement: Expression | AssumeProve;
|
|
56
|
+
isSuffices: boolean;
|
|
57
|
+
isInProof: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// isPending holds for an operator a RECURSIVE declaration names until its definition.
|
|
61
|
+
// isNonFact holds for the name of a USE, HIDE, DEFINE or INSTANCE step, which SANY takes neither as
|
|
62
|
+
// a fact nor as an expression.
|
|
63
|
+
interface LocalName extends Binding {
|
|
28
64
|
name: string;
|
|
29
|
-
arity: number;
|
|
30
65
|
parent: LocalName | undefined;
|
|
66
|
+
goal?: Goal;
|
|
67
|
+
isPending?: boolean;
|
|
68
|
+
isNonFact?: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// A name the module binds, from position on.
|
|
72
|
+
interface ModuleBinding extends Binding {
|
|
73
|
+
position: SourceLocation;
|
|
74
|
+
isPending?: boolean;
|
|
31
75
|
}
|
|
32
76
|
|
|
77
|
+
// What SANY reports while it goes on analyzing a unit of a module, and the scope it analyzes it in.
|
|
33
78
|
interface Unit {
|
|
34
79
|
imported: Symbols;
|
|
80
|
+
getInstance: (instance: Instance) => InstancedModule;
|
|
35
81
|
definitions: Map<string, Definition>;
|
|
36
82
|
getKey: (definition: Definition) => string;
|
|
37
83
|
location: SourceLocation;
|
|
38
84
|
self: Definition | undefined;
|
|
39
85
|
facts: Set<string>;
|
|
86
|
+
goals: Map<string, Goal>;
|
|
87
|
+
bindings: Map<string, ModuleBinding>;
|
|
88
|
+
errors: SanyError[];
|
|
89
|
+
warnings: SanyError[];
|
|
90
|
+
// In a definition, a named theorem, or a numbered step.
|
|
91
|
+
hasLabelScope: boolean;
|
|
92
|
+
// In the scope of a NEW in a nested ASSUME.
|
|
93
|
+
isLabelForbidden: boolean;
|
|
94
|
+
isInExcept: boolean;
|
|
95
|
+
// The names bound since the innermost definition, label, or step, which a label must take.
|
|
96
|
+
labelParams: string[];
|
|
97
|
+
labels: Set<string>;
|
|
98
|
+
// In a definition generated for a LET INSTANCE, or a subexpression analyzed where it is defined.
|
|
99
|
+
isGenerated: boolean;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface SelectionError {
|
|
103
|
+
args: Expression[];
|
|
104
|
+
error: SanyError;
|
|
40
105
|
}
|
|
41
106
|
|
|
107
|
+
interface SelectionState {
|
|
108
|
+
node: Expression | NewSymbol | AssumeProve;
|
|
109
|
+
isGoal: boolean;
|
|
110
|
+
isSufficesOpen: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" };
|
|
114
|
+
|
|
42
115
|
export const LOCAL_RESOLUTION = { kind: "Local" } as const;
|
|
43
116
|
|
|
117
|
+
// The subexpressions that names select, and for a name that selects none, the error SANY reports and
|
|
118
|
+
// the arguments it analyzes before it.
|
|
119
|
+
export const selections = new WeakSet<Expression>();
|
|
120
|
+
export const selectionErrors = new WeakMap<Expression, SelectionError>();
|
|
121
|
+
|
|
44
122
|
const renamedArities = new WeakMap<Expression, number>();
|
|
45
123
|
|
|
46
124
|
export const KEY_SEPARATOR = "@";
|
|
@@ -52,7 +130,7 @@ const isKey = (name: string) => {
|
|
|
52
130
|
};
|
|
53
131
|
|
|
54
132
|
const CORE_SYMBOLS: Symbols = new Map(
|
|
55
|
-
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2 }]),
|
|
133
|
+
["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2, kind: "Builtin" }]),
|
|
56
134
|
);
|
|
57
135
|
|
|
58
136
|
export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
@@ -61,6 +139,9 @@ export const isBefore = (left: SourceLocation, right: SourceLocation) =>
|
|
|
61
139
|
export const getArity = (definition: Definition) =>
|
|
62
140
|
definition.kind === "Operator" ? definition.params.length : 0;
|
|
63
141
|
|
|
142
|
+
export const getParamArities = (definition: Definition) =>
|
|
143
|
+
definition.kind === "Operator" ? definition.params.map((param) => param.arity) : [];
|
|
144
|
+
|
|
64
145
|
const isInstantiated = (definition: Definition) => definition.name.includes("!");
|
|
65
146
|
|
|
66
147
|
const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
@@ -70,12 +151,97 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
|
|
|
70
151
|
return undefined;
|
|
71
152
|
};
|
|
72
153
|
|
|
73
|
-
|
|
74
|
-
|
|
154
|
+
// A name the module binds before the unit and before the range, which a copy of a subexpression
|
|
155
|
+
// that a reference selects keeps from its definition.
|
|
156
|
+
const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefined) => {
|
|
157
|
+
const binding = unit.bindings.get(name);
|
|
158
|
+
return binding &&
|
|
159
|
+
isBefore(binding.position, unit.location) &&
|
|
160
|
+
(!range || isBefore(binding.position, range.start))
|
|
161
|
+
? binding
|
|
162
|
+
: undefined;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
|
|
166
|
+
left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
|
|
167
|
+
|
|
168
|
+
// SymbolTable.addSymbol: rejects redefining a built-in, a bound name, or a symbol of another kind
|
|
169
|
+
// or arity, and warns of any other duplicate. A symbol SANY never sees, such as a generated one, binds.
|
|
170
|
+
const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
|
|
171
|
+
if (current === undefined) return true;
|
|
172
|
+
if (isSameRange(current.range, added.range)) return false;
|
|
173
|
+
const range = added.range;
|
|
174
|
+
if (range === undefined || unit.isGenerated || (current.kind !== "Builtin" && !current.range)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
const location = formatRange(range);
|
|
178
|
+
const existing = current.range && formatRange(current.range);
|
|
179
|
+
if (current.kind === "Builtin") {
|
|
180
|
+
unit.errors.push({
|
|
181
|
+
location,
|
|
182
|
+
message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
|
|
183
|
+
});
|
|
184
|
+
} else if (
|
|
185
|
+
added.kind === "Parameter" ||
|
|
186
|
+
current.kind !== added.kind ||
|
|
187
|
+
current.arity !== added.arity
|
|
188
|
+
) {
|
|
189
|
+
unit.errors.push({
|
|
190
|
+
location,
|
|
191
|
+
message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
|
|
192
|
+
});
|
|
193
|
+
} else {
|
|
194
|
+
unit.warnings.push({
|
|
195
|
+
location,
|
|
196
|
+
message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const bindName = (
|
|
203
|
+
scope: LocalName | undefined,
|
|
204
|
+
name: string,
|
|
205
|
+
added: Binding,
|
|
206
|
+
unit: Unit,
|
|
207
|
+
): LocalName | undefined =>
|
|
208
|
+
isBindable(
|
|
209
|
+
name,
|
|
210
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, added.range),
|
|
211
|
+
added,
|
|
212
|
+
unit,
|
|
213
|
+
)
|
|
214
|
+
? { ...added, name, parent: scope }
|
|
215
|
+
: scope;
|
|
216
|
+
|
|
217
|
+
// Binds the names of a module from the unit on.
|
|
218
|
+
const bindModuleName = (name: string, added: Binding, unit: Unit) => {
|
|
219
|
+
if (isBindable(name, unit.bindings.get(name), added, unit)) {
|
|
220
|
+
unit.bindings.set(name, { ...added, position: unit.location });
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const bindNames = (
|
|
225
|
+
scope: LocalName | undefined,
|
|
226
|
+
names: string[],
|
|
227
|
+
ranges: SourceRange[] | undefined,
|
|
228
|
+
unit: Unit,
|
|
229
|
+
) =>
|
|
230
|
+
names.reduce(
|
|
231
|
+
(parent, name, index) =>
|
|
232
|
+
bindName(parent, name, { kind: "Parameter", arity: 0, range: ranges?.[index] }, unit),
|
|
233
|
+
scope,
|
|
234
|
+
);
|
|
75
235
|
|
|
76
|
-
const bindParams = (scope: LocalName | undefined, params: OperatorParam[]) =>
|
|
77
|
-
params.reduce
|
|
78
|
-
(parent, param) =>
|
|
236
|
+
const bindParams = (scope: LocalName | undefined, params: OperatorParam[], unit: Unit) =>
|
|
237
|
+
params.reduce(
|
|
238
|
+
(parent, param) =>
|
|
239
|
+
bindName(
|
|
240
|
+
parent,
|
|
241
|
+
param.name,
|
|
242
|
+
{ kind: "Parameter", arity: param.arity, range: param.location?.range },
|
|
243
|
+
unit,
|
|
244
|
+
),
|
|
79
245
|
scope,
|
|
80
246
|
);
|
|
81
247
|
|
|
@@ -98,19 +264,452 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
|
|
|
98
264
|
const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
99
265
|
const definition = getVisibleDefinition(name, unit);
|
|
100
266
|
return definition
|
|
101
|
-
? {
|
|
267
|
+
? {
|
|
268
|
+
key: unit.getKey(definition),
|
|
269
|
+
arity: getArity(definition),
|
|
270
|
+
params: getParamArities(definition),
|
|
271
|
+
kind: "Definition",
|
|
272
|
+
}
|
|
102
273
|
: unit.imported.get(name);
|
|
103
274
|
};
|
|
104
275
|
|
|
276
|
+
const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
|
|
277
|
+
lookupLocal(scope, name) ?? findSymbol(name, unit);
|
|
278
|
+
|
|
279
|
+
// SANY reports a name at the range of its text.
|
|
280
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
281
|
+
module: location.range?.module ?? "",
|
|
282
|
+
start: location,
|
|
283
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// SANY reports a name at its tokens, such as M ! ⊕ for M!\oplus.
|
|
287
|
+
const getSpelledNameRange = (
|
|
288
|
+
expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
|
|
289
|
+
name: string,
|
|
290
|
+
): SourceRange => {
|
|
291
|
+
const selectorRange =
|
|
292
|
+
expression.kind === "OperatorReference" ? undefined : expression.selectorRanges?.at(-1);
|
|
293
|
+
return selectorRange
|
|
294
|
+
? { module: selectorRange.module, start: expression.location, end: selectorRange.end }
|
|
295
|
+
: getNameRange(expression.location, expression.spelling ?? name);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
299
|
+
const getFixity = (expression: Expression) => {
|
|
300
|
+
if (expression.kind !== "Apply") return undefined;
|
|
301
|
+
if (expression.args.length === 2) return "infix";
|
|
302
|
+
if (expression.args.length !== 1) return undefined;
|
|
303
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
304
|
+
};
|
|
305
|
+
|
|
105
306
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
106
|
-
const isOperatorSymbol = (name: string) =>
|
|
107
|
-
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
108
|
-
name[0] !== "_" &&
|
|
109
|
-
(name[0] < "0" || name[0] > "9");
|
|
307
|
+
const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
|
|
110
308
|
|
|
111
309
|
const isStepName = (name: string) =>
|
|
112
310
|
name.startsWith("<") && ((name[1] >= "0" && name[1] <= "9") || name[1] === "*");
|
|
113
311
|
|
|
312
|
+
export const getOperandIndex = (operand: number | "<<" | ">>", count: number) => {
|
|
313
|
+
if (typeof operand === "number") return operand >= 1 && operand <= count ? operand - 1 : -1;
|
|
314
|
+
if (operand === "<<") return count > 0 ? 0 : -1;
|
|
315
|
+
return count === 2 ? 1 : -1;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// The parts of a name such as F(x)!l(y), each with its own arguments. counts holds the number each
|
|
319
|
+
// part takes, when a part before the last takes some.
|
|
320
|
+
export const getNameSelectors = (
|
|
321
|
+
name: string,
|
|
322
|
+
args: Expression[],
|
|
323
|
+
counts: number[] | undefined,
|
|
324
|
+
): Selector[] => {
|
|
325
|
+
const parts = name.split("!");
|
|
326
|
+
const partCounts =
|
|
327
|
+
counts ?? parts.map((_, index) => (index === parts.length - 1 ? args.length : 0));
|
|
328
|
+
return parts.map((part, index) => {
|
|
329
|
+
const start = partCounts.slice(0, index).reduce((sum, count) => sum + count, 0);
|
|
330
|
+
return { kind: "Name", name: part, args: args.slice(start, start + partCounts[index]) };
|
|
331
|
+
});
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const getSelectors = (reference: Reference): Selector[] =>
|
|
335
|
+
reference.kind === "Subexpression"
|
|
336
|
+
? reference.selectors
|
|
337
|
+
: reference.kind === "Identifier"
|
|
338
|
+
? getNameSelectors(reference.name, [], undefined)
|
|
339
|
+
: getNameSelectors(reference.operator, reference.args, reference.argumentCounts);
|
|
340
|
+
|
|
341
|
+
const getBoundSets = (bounds: Bound[]) =>
|
|
342
|
+
bounds
|
|
343
|
+
.filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
|
|
344
|
+
.map((bound) => bound.set);
|
|
345
|
+
|
|
346
|
+
// The names a binder binds, its body, and the sets it binds them to, which a selector such as !1
|
|
347
|
+
// chooses from.
|
|
348
|
+
export const getBinder = (
|
|
349
|
+
expression: Expression,
|
|
350
|
+
): { names: string[]; body: Expression; sets: Expression[] } | undefined => {
|
|
351
|
+
switch (expression.kind) {
|
|
352
|
+
case "Forall":
|
|
353
|
+
case "Exists":
|
|
354
|
+
case "FunctionConstruction":
|
|
355
|
+
return {
|
|
356
|
+
names: getBoundNames(expression.bounds),
|
|
357
|
+
body: expression.body,
|
|
358
|
+
sets: getBoundSets(expression.bounds),
|
|
359
|
+
};
|
|
360
|
+
case "SetMap":
|
|
361
|
+
return {
|
|
362
|
+
names: getBoundNames(expression.bounds),
|
|
363
|
+
body: expression.expression,
|
|
364
|
+
sets: getBoundSets(expression.bounds),
|
|
365
|
+
};
|
|
366
|
+
case "SetFilter":
|
|
367
|
+
return {
|
|
368
|
+
names: getPatternNames(expression.bound.pattern),
|
|
369
|
+
body: expression.predicate,
|
|
370
|
+
sets: [expression.bound.set],
|
|
371
|
+
};
|
|
372
|
+
case "Choose":
|
|
373
|
+
return {
|
|
374
|
+
names: getPatternNames(expression.pattern),
|
|
375
|
+
body: expression.body,
|
|
376
|
+
sets: expression.set ? [expression.set] : [],
|
|
377
|
+
};
|
|
378
|
+
case "UnboundedQuantifier":
|
|
379
|
+
case "Lambda":
|
|
380
|
+
return {
|
|
381
|
+
names: expression.kind === "Lambda" ? expression.params : expression.names,
|
|
382
|
+
body: expression.body,
|
|
383
|
+
sets: [],
|
|
384
|
+
};
|
|
385
|
+
default:
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// The operands that a selector such as !1 chooses from, where they do not depend on bound names.
|
|
391
|
+
export const getOperands = (expression: Expression): Expression[] | undefined => {
|
|
392
|
+
switch (expression.kind) {
|
|
393
|
+
case "Apply":
|
|
394
|
+
return expression.args;
|
|
395
|
+
case "And":
|
|
396
|
+
case "Or":
|
|
397
|
+
return expression.operands;
|
|
398
|
+
case "If":
|
|
399
|
+
return [expression.condition, expression.consequent, expression.alternative];
|
|
400
|
+
case "Tuple":
|
|
401
|
+
case "SetEnumeration":
|
|
402
|
+
return expression.elements;
|
|
403
|
+
case "Record":
|
|
404
|
+
case "RecordSet":
|
|
405
|
+
return expression.fields.map((field) => field.value);
|
|
406
|
+
case "FunctionApplication":
|
|
407
|
+
return [
|
|
408
|
+
expression.function,
|
|
409
|
+
expression.args.length === 1
|
|
410
|
+
? expression.args[0]
|
|
411
|
+
: { kind: "Tuple", elements: expression.args, location: expression.location },
|
|
412
|
+
];
|
|
413
|
+
case "FieldAccess":
|
|
414
|
+
return [
|
|
415
|
+
expression.record,
|
|
416
|
+
{ kind: "String", value: expression.field, location: expression.location },
|
|
417
|
+
];
|
|
418
|
+
case "Prime":
|
|
419
|
+
case "Unchanged":
|
|
420
|
+
case "Enabled":
|
|
421
|
+
case "Always":
|
|
422
|
+
case "Eventually":
|
|
423
|
+
return [expression.expression];
|
|
424
|
+
case "BoxAction":
|
|
425
|
+
case "AngleAction":
|
|
426
|
+
return [expression.action, expression.subscript];
|
|
427
|
+
case "WeakFairness":
|
|
428
|
+
case "StrongFairness":
|
|
429
|
+
return [expression.subscript, expression.action];
|
|
430
|
+
case "FunctionSet":
|
|
431
|
+
return [expression.domain, expression.range];
|
|
432
|
+
case "Product":
|
|
433
|
+
return expression.factors;
|
|
434
|
+
case "Except":
|
|
435
|
+
return [expression.function];
|
|
436
|
+
default:
|
|
437
|
+
return undefined;
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const isReference = (expression: Expression): expression is Reference =>
|
|
442
|
+
expression.kind === "Identifier" ||
|
|
443
|
+
expression.kind === "Apply" ||
|
|
444
|
+
expression.kind === "Subexpression";
|
|
445
|
+
|
|
446
|
+
const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
|
|
447
|
+
assumeProve.assumptions.slice(0, clause).some((assumption) => assumption.kind === "NewSymbol");
|
|
448
|
+
|
|
449
|
+
const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
|
|
450
|
+
if (expression.kind === "Label") return expression.name === name ? expression : undefined;
|
|
451
|
+
if (expression.kind === "Lambda") return undefined;
|
|
452
|
+
if (expression.kind === "Let") return findLabel(expression.body, name);
|
|
453
|
+
return getChildren(expression).reduce<LabelExpression | undefined>(
|
|
454
|
+
(found, child) => found ?? findLabel(child, name),
|
|
455
|
+
undefined,
|
|
456
|
+
);
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
|
|
460
|
+
const findClauseLabel = (
|
|
461
|
+
clause: Expression | NewSymbol | AssumeProve,
|
|
462
|
+
name: string,
|
|
463
|
+
isForbidden: boolean,
|
|
464
|
+
): LabelExpression | undefined => {
|
|
465
|
+
if (clause.kind === "AssumeProve") {
|
|
466
|
+
return [...clause.assumptions, clause.conclusion].reduce<LabelExpression | undefined>(
|
|
467
|
+
(found, inner, index) =>
|
|
468
|
+
found ?? findClauseLabel(inner, name, isForbidden || isInScopeOfDeclaration(clause, index)),
|
|
469
|
+
undefined,
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
if (isForbidden) return undefined;
|
|
473
|
+
if (clause.kind === "NewSymbol") return clause.set && findLabel(clause.set, name);
|
|
474
|
+
return findLabel(clause, name);
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const getLabelBody = (expression: Expression): Expression =>
|
|
478
|
+
expression.kind === "Label" ? getLabelBody(expression.body) : expression;
|
|
479
|
+
|
|
480
|
+
const getOperandName = (operand: number | "<<" | ">>") =>
|
|
481
|
+
typeof operand === "number" ? `argument selector ${operand}` : operand;
|
|
482
|
+
|
|
483
|
+
const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
|
|
484
|
+
|
|
485
|
+
// Generator.selectorToNode for a name that selects part of a theorem or step. A part in the scope of
|
|
486
|
+
// a NEW in its ASSUME may be selected only within the proof of the theorem, or only after the proof
|
|
487
|
+
// of a SUFFICES step. A label takes its own arguments, and an operand must exist, unless the part
|
|
488
|
+
// selected depends on bound names. A fact may select an ASSUME/PROVE or a NEW clause.
|
|
489
|
+
const checkSelection = (
|
|
490
|
+
reference: Reference,
|
|
491
|
+
selectors: Selector[],
|
|
492
|
+
goal: Goal,
|
|
493
|
+
scope: LocalName | undefined,
|
|
494
|
+
unit: Unit,
|
|
495
|
+
isFact: boolean,
|
|
496
|
+
) => {
|
|
497
|
+
const report = (index: number | undefined, message: string) => {
|
|
498
|
+
const range =
|
|
499
|
+
(index === undefined ? undefined : reference.selectorRanges?.[index]) ??
|
|
500
|
+
reference.location.range;
|
|
501
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
502
|
+
};
|
|
503
|
+
const isOutOfScope = goal.isSuffices === goal.isInProof;
|
|
504
|
+
const statement = goal.statement;
|
|
505
|
+
const state: SelectionState = {
|
|
506
|
+
node: statement,
|
|
507
|
+
isGoal: true,
|
|
508
|
+
isSufficesOpen: goal.isSuffices,
|
|
509
|
+
};
|
|
510
|
+
for (const [offset, selector] of selectors.slice(1).entries()) {
|
|
511
|
+
const index = offset + 1;
|
|
512
|
+
const node = state.node;
|
|
513
|
+
if (selector.kind === "Name") {
|
|
514
|
+
if (selectors[index - 1].kind !== "Name") {
|
|
515
|
+
const isLet =
|
|
516
|
+
node.kind !== "NewSymbol" &&
|
|
517
|
+
node.kind !== "AssumeProve" &&
|
|
518
|
+
getLabelBody(node).kind === "Let";
|
|
519
|
+
return isLet
|
|
520
|
+
? undefined
|
|
521
|
+
: report(index - 1, "A name selector here must be from a LET clause.");
|
|
522
|
+
}
|
|
523
|
+
const clauses =
|
|
524
|
+
statement.kind === "AssumeProve"
|
|
525
|
+
? [...statement.assumptions, statement.conclusion]
|
|
526
|
+
: [statement];
|
|
527
|
+
const found =
|
|
528
|
+
index === 1
|
|
529
|
+
? clauses
|
|
530
|
+
.map((clause, position) => ({
|
|
531
|
+
position,
|
|
532
|
+
label: findClauseLabel(clause, selector.name, false),
|
|
533
|
+
}))
|
|
534
|
+
.find((item) => item.label !== undefined)
|
|
535
|
+
: {
|
|
536
|
+
position: 0,
|
|
537
|
+
label: node.kind === "Label" ? findLabel(node.body, selector.name) : undefined,
|
|
538
|
+
};
|
|
539
|
+
if (!found?.label) return report(index, `Cannot find label \`${selector.name}'.`);
|
|
540
|
+
if (
|
|
541
|
+
index === 1 &&
|
|
542
|
+
statement.kind === "AssumeProve" &&
|
|
543
|
+
isInScopeOfDeclaration(statement, found.position) &&
|
|
544
|
+
isOutOfScope
|
|
545
|
+
) {
|
|
546
|
+
return report(
|
|
547
|
+
1,
|
|
548
|
+
`Accessing subexpression labeled \`${selector.name}' of ASSUME/PROVE clause ${SCOPE_MESSAGE}`,
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
if (selector.args.length !== found.label.params.length) {
|
|
552
|
+
return report(index, `Label \`${selector.name}' used with wrong number of arguments.`);
|
|
553
|
+
}
|
|
554
|
+
state.node = found.label;
|
|
555
|
+
state.isGoal = false;
|
|
556
|
+
state.isSufficesOpen = false;
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
if (selector.kind !== "Operand") return;
|
|
560
|
+
const operand = getOperandName(selector.operand);
|
|
561
|
+
if (state.isSufficesOpen) {
|
|
562
|
+
if (getOperandIndex(selector.operand, 1) !== 0) {
|
|
563
|
+
return report(
|
|
564
|
+
index,
|
|
565
|
+
node.kind === "AssumeProve"
|
|
566
|
+
? "Accessing non-existent subexpression of a SUFFICES"
|
|
567
|
+
: `Nonexistent operand specified by \`${operand}'.`,
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
state.isSufficesOpen = false;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
if (node.kind === "NewSymbol") return;
|
|
574
|
+
if (node.kind !== "AssumeProve") {
|
|
575
|
+
const expression = getLabelBody(node);
|
|
576
|
+
if (["Number", "Decimal", "String"].includes(expression.kind)) {
|
|
577
|
+
return report(index, "Selecting subexpression of expression that has none.");
|
|
578
|
+
}
|
|
579
|
+
const kind =
|
|
580
|
+
expression.kind === "Identifier"
|
|
581
|
+
? findBinding(expression.name, scope, unit)?.kind
|
|
582
|
+
: undefined;
|
|
583
|
+
if (kind === "Variable" || kind?.startsWith("New")) {
|
|
584
|
+
return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
|
|
585
|
+
}
|
|
586
|
+
const sets = getBinder(expression)?.sets;
|
|
587
|
+
const operands =
|
|
588
|
+
expression.kind === "Identifier"
|
|
589
|
+
? []
|
|
590
|
+
: sets
|
|
591
|
+
? sets.length > 0
|
|
592
|
+
? sets
|
|
593
|
+
: undefined
|
|
594
|
+
: expression.kind === "Except"
|
|
595
|
+
? undefined
|
|
596
|
+
: getOperands(expression);
|
|
597
|
+
if (!operands) return;
|
|
598
|
+
const position = getOperandIndex(selector.operand, operands.length);
|
|
599
|
+
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
600
|
+
state.node = operands[position];
|
|
601
|
+
state.isGoal = false;
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
const clauses = [...node.assumptions, node.conclusion];
|
|
605
|
+
const position = getOperandIndex(selector.operand, clauses.length);
|
|
606
|
+
if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
|
|
607
|
+
if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
|
|
608
|
+
return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
|
|
609
|
+
}
|
|
610
|
+
const clause = clauses[position];
|
|
611
|
+
if (clause.kind === "NewSymbol" && index !== selectors.length - 1) {
|
|
612
|
+
return report(index, "Selected a subexpression of a NEW clause of an ASSUME.");
|
|
613
|
+
}
|
|
614
|
+
state.node = clause;
|
|
615
|
+
state.isGoal = false;
|
|
616
|
+
}
|
|
617
|
+
if (isFact) return;
|
|
618
|
+
if (state.node.kind === "AssumeProve") {
|
|
619
|
+
report(undefined, "Selected ASSUME/PROVE instead of expression.");
|
|
620
|
+
}
|
|
621
|
+
if (state.node.kind === "NewSymbol") {
|
|
622
|
+
report(undefined, "Selected a NEW declaration as an expression or operator.");
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
// Generator.generateLabel reports a label where it may not appear, and skips the label's body.
|
|
627
|
+
const isLabelReported = (label: LabelExpression, unit: Unit) => {
|
|
628
|
+
const message = unit.isGenerated
|
|
629
|
+
? undefined
|
|
630
|
+
: !unit.hasLabelScope
|
|
631
|
+
? "Label not in definition or proof step."
|
|
632
|
+
: unit.isLabelForbidden
|
|
633
|
+
? "Label not allowed within scope of declaration in nested ASSUME/PROVE."
|
|
634
|
+
: unit.isInExcept
|
|
635
|
+
? "Labels inside EXCEPT clauses are not yet implemented."
|
|
636
|
+
: undefined;
|
|
637
|
+
if (message === undefined || !label.location.range) return false;
|
|
638
|
+
unit.errors.push({ location: formatRange(label.location.range), message });
|
|
639
|
+
return true;
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
// Generator.generateLabel reports, after the label's body, each parameter that names no bound name,
|
|
643
|
+
// then formalParamsEqual each parameter given twice, each name bound since the label scope began
|
|
644
|
+
// that the label leaves out, and the parameters it adds, and then a label of a name already used.
|
|
645
|
+
const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit: Unit) => {
|
|
646
|
+
const range = label.location.range;
|
|
647
|
+
if (!range || unit.isGenerated) return;
|
|
648
|
+
const location = formatRange(range);
|
|
649
|
+
const legal = label.params.filter((param, index) => {
|
|
650
|
+
const isLegal = lookupLocal(scope, param)?.kind === "Parameter";
|
|
651
|
+
const paramRange = label.paramRanges?.[index];
|
|
652
|
+
if (!isLegal && paramRange) {
|
|
653
|
+
unit.errors.push({
|
|
654
|
+
location: formatRange(paramRange),
|
|
655
|
+
message: `Illegal parameter ${param} of label \`${label.name}'.`,
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
return isLegal;
|
|
659
|
+
});
|
|
660
|
+
legal
|
|
661
|
+
.filter((param, index) => legal.indexOf(param) !== index)
|
|
662
|
+
.forEach((param) =>
|
|
663
|
+
unit.errors.push({
|
|
664
|
+
location,
|
|
665
|
+
message: `Repeated formal parameter ${param} \nin label \`${label.name}'.`,
|
|
666
|
+
}),
|
|
667
|
+
);
|
|
668
|
+
unit.labelParams
|
|
669
|
+
.filter((param) => !legal.includes(param))
|
|
670
|
+
.forEach((param) =>
|
|
671
|
+
unit.errors.push({
|
|
672
|
+
location,
|
|
673
|
+
message: `Label ${label.name} must contain formal parameter \`${param}'.`,
|
|
674
|
+
}),
|
|
675
|
+
);
|
|
676
|
+
const extra = [
|
|
677
|
+
...new Set(legal.filter((param) => !unit.labelParams.includes(param))),
|
|
678
|
+
...label.params.filter((param) => !legal.includes(param)),
|
|
679
|
+
];
|
|
680
|
+
if (extra.length > 0) {
|
|
681
|
+
unit.errors.push({
|
|
682
|
+
location,
|
|
683
|
+
message: `Label ${label.name} declares extra parameter(s) ${extra.map((param) => `${param} `).join("")}`,
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
if (unit.labels.has(label.name)) {
|
|
687
|
+
unit.errors.push({ location, message: `Duplicate label \`${label.name}'.` });
|
|
688
|
+
}
|
|
689
|
+
unit.labels.add(label.name);
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
// Checks a name that selects part of a theorem or step, and tells whether it is one.
|
|
693
|
+
const checkReference = (
|
|
694
|
+
expression: Expression,
|
|
695
|
+
scope: LocalName | undefined,
|
|
696
|
+
unit: Unit,
|
|
697
|
+
isFact: boolean,
|
|
698
|
+
) => {
|
|
699
|
+
if (!isReference(expression)) return false;
|
|
700
|
+
const selectors = getSelectors(expression);
|
|
701
|
+
const first = selectors[0];
|
|
702
|
+
const goal =
|
|
703
|
+
selectors.length > 1 && first.kind === "Name"
|
|
704
|
+
? (lookupLocal(scope, first.name)?.goal ?? unit.goals.get(first.name))
|
|
705
|
+
: undefined;
|
|
706
|
+
if (goal) checkSelection(expression, selectors, goal, scope, unit, isFact);
|
|
707
|
+
return goal !== undefined;
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
// Generator.selectorToNode reports an unknown operator and goes on without analyzing its arguments,
|
|
711
|
+
// so its arity is undefined. It does the same for an operator given the wrong number of arguments,
|
|
712
|
+
// and for a name that selects nothing, after the arguments of the parts before.
|
|
114
713
|
const getSymbolArity = (
|
|
115
714
|
expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
|
|
116
715
|
name: string,
|
|
@@ -118,11 +717,21 @@ const getSymbolArity = (
|
|
|
118
717
|
unit: Unit,
|
|
119
718
|
) => {
|
|
120
719
|
const location = expression.location;
|
|
720
|
+
if (isSelectionReported(expression, scope, unit)) return undefined;
|
|
121
721
|
const renamed = renamedArities.get(expression);
|
|
122
722
|
if (renamed !== undefined) return renamed;
|
|
123
723
|
if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
|
|
124
|
-
if (isStepName(name)) return 0;
|
|
125
724
|
const local = lookupLocal(scope, name);
|
|
725
|
+
if (local?.isNonFact) {
|
|
726
|
+
unit.errors.push({
|
|
727
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
728
|
+
message: "Step number of non-expression step used as an expression.",
|
|
729
|
+
});
|
|
730
|
+
return undefined;
|
|
731
|
+
}
|
|
732
|
+
if (checkReference(expression, scope, unit, false) || (isStepName(name) && local)) {
|
|
733
|
+
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
734
|
+
}
|
|
126
735
|
if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
|
|
127
736
|
if (local) return local.arity;
|
|
128
737
|
const symbol = findSymbol(name, unit);
|
|
@@ -134,7 +743,7 @@ const getSymbolArity = (
|
|
|
134
743
|
}
|
|
135
744
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
136
745
|
// arguments.
|
|
137
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
746
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
138
747
|
const isSelected =
|
|
139
748
|
prefix !== "" &&
|
|
140
749
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -143,18 +752,35 @@ const getSymbolArity = (
|
|
|
143
752
|
if (isSelected) {
|
|
144
753
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
145
754
|
}
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
?
|
|
150
|
-
:
|
|
755
|
+
const spelling = (expression.kind === "Identifier" ? undefined : expression.spelling) ?? name;
|
|
756
|
+
const range = formatRange(
|
|
757
|
+
expression.kind === "Apply" && spelling === "-."
|
|
758
|
+
? getNameRange(location, "-")
|
|
759
|
+
: getSpelledNameRange(expression, name),
|
|
151
760
|
);
|
|
761
|
+
if (!isOperatorSymbol(name) || isStepName(name)) {
|
|
762
|
+
unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
|
|
763
|
+
return undefined;
|
|
764
|
+
}
|
|
765
|
+
unit.errors.push({
|
|
766
|
+
location: range,
|
|
767
|
+
message: `Could not find declaration or definition of symbol '${spelling}'.`,
|
|
768
|
+
});
|
|
769
|
+
const fixity = getFixity(expression);
|
|
770
|
+
if (fixity && location.range) {
|
|
771
|
+
unit.errors.push({
|
|
772
|
+
location: formatRange(location.range),
|
|
773
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${spelling}'.`,
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
return undefined;
|
|
152
777
|
};
|
|
153
778
|
|
|
154
779
|
const resolveBounds = (bounds: Bound[], scope: LocalName | undefined, unit: Unit) => {
|
|
155
780
|
bounds.forEach((bound) => resolveExpression(bound.set, scope, unit));
|
|
156
781
|
return bounds.reduce(
|
|
157
|
-
(current, bound) =>
|
|
782
|
+
(current, bound) =>
|
|
783
|
+
bindNames(current, getPatternNames(bound.pattern), bound.pattern.ranges, unit),
|
|
158
784
|
scope,
|
|
159
785
|
);
|
|
160
786
|
};
|
|
@@ -170,94 +796,286 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
|
|
|
170
796
|
getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
|
|
171
797
|
};
|
|
172
798
|
|
|
173
|
-
|
|
174
|
-
|
|
799
|
+
// Generator.processOperator binds the parameters, reports a name already bound unless a RECURSIVE
|
|
800
|
+
// declaration of this level awaits the definition, analyzes the body, and then binds the name.
|
|
801
|
+
// processFunction binds the bound names before it looks up the name, and binds the name only where
|
|
802
|
+
// nothing else does. recursive holds the declarations of this level. Tells whether to bind the name.
|
|
803
|
+
const resolveDefinition = (
|
|
804
|
+
definition: Definition,
|
|
805
|
+
scope: LocalName | undefined,
|
|
806
|
+
outer: Unit,
|
|
807
|
+
recursive: Set<Binding>,
|
|
808
|
+
) => {
|
|
809
|
+
const unit = {
|
|
810
|
+
...outer,
|
|
811
|
+
hasLabelScope: true,
|
|
812
|
+
labelParams: definition.kind === "Function" ? getBoundNames(definition.bounds) : [],
|
|
813
|
+
labels: new Set<string>(),
|
|
814
|
+
};
|
|
815
|
+
const name = definition.name;
|
|
816
|
+
const range = definition.location.range;
|
|
817
|
+
const report = (message: string) => {
|
|
818
|
+
if (range && !unit.isGenerated) unit.errors.push({ location: formatRange(range), message });
|
|
819
|
+
};
|
|
820
|
+
const inner =
|
|
821
|
+
definition.kind === "Function"
|
|
822
|
+
? resolveBounds(definition.bounds, scope, unit)
|
|
823
|
+
: bindParams(scope, definition.params, unit);
|
|
824
|
+
const current = lookupLocal(inner, name) ?? getModuleBinding(name, unit, range);
|
|
825
|
+
const isDeclared = current?.isPending === true && recursive.has(current);
|
|
826
|
+
if (current?.isPending && !isDeclared) {
|
|
827
|
+
report(
|
|
828
|
+
`Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
|
|
829
|
+
);
|
|
830
|
+
} else if (current && !isDeclared) {
|
|
831
|
+
report(
|
|
832
|
+
definition.kind === "Function"
|
|
833
|
+
? `Function name \`${name}' already defined or declared.`
|
|
834
|
+
: `Operator ${name} already defined or declared.`,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
175
837
|
if (definition.kind === "Function") {
|
|
176
|
-
resolveExpression(
|
|
177
|
-
|
|
178
|
-
|
|
838
|
+
resolveExpression(
|
|
839
|
+
definition.body,
|
|
840
|
+
{ name, kind: "Parameter", arity: 0, range, parent: inner },
|
|
841
|
+
unit,
|
|
842
|
+
);
|
|
843
|
+
markRecursiveConstructions(definition.body, name);
|
|
844
|
+
definition.isSelfReferential = isReferenced(definition.body, name);
|
|
845
|
+
} else resolveExpression(definition.body, inner, unit);
|
|
846
|
+
if (current && isDeclared) {
|
|
847
|
+
current.isPending = false;
|
|
848
|
+
current.range = range;
|
|
849
|
+
return false;
|
|
179
850
|
}
|
|
180
|
-
|
|
851
|
+
return definition.kind === "Function"
|
|
852
|
+
? current === undefined
|
|
853
|
+
: isBindable(
|
|
854
|
+
name,
|
|
855
|
+
lookupLocal(scope, name) ?? getModuleBinding(name, unit, range),
|
|
856
|
+
{ kind: "Definition", arity: getArity(definition), range },
|
|
857
|
+
unit,
|
|
858
|
+
);
|
|
181
859
|
};
|
|
182
860
|
|
|
861
|
+
// SANY analyzes an INSTANCE in a LET where it appears, before the definitions that follow it, which
|
|
862
|
+
// tla-ts generates at its location and analyzes without reporting.
|
|
183
863
|
const resolveDefinitions = (
|
|
184
864
|
definitions: Definition[],
|
|
185
865
|
scope: LocalName | undefined,
|
|
186
866
|
unit: Unit,
|
|
867
|
+
instances: Instance[] = [],
|
|
187
868
|
) => {
|
|
188
|
-
const
|
|
869
|
+
const recursive = new Set<Binding>();
|
|
870
|
+
const declared = definitions
|
|
189
871
|
.filter((definition) => definition.isRecursive)
|
|
190
|
-
.reduce<LocalName | undefined>(
|
|
191
|
-
(parent, definition
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
872
|
+
.reduce<LocalName | undefined>((parent, definition) => {
|
|
873
|
+
if (lookupLocal(parent, definition.name)?.isPending) return parent;
|
|
874
|
+
const local: LocalName = {
|
|
875
|
+
name: definition.name,
|
|
876
|
+
arity: getArity(definition),
|
|
877
|
+
params: getParamArities(definition),
|
|
878
|
+
kind: "Definition",
|
|
879
|
+
parent,
|
|
880
|
+
isPending: true,
|
|
881
|
+
};
|
|
882
|
+
recursive.add(local);
|
|
883
|
+
return local;
|
|
884
|
+
}, scope);
|
|
885
|
+
const pending = unit.isGenerated ? [] : [...instances];
|
|
886
|
+
const resolveInstances = (
|
|
887
|
+
current: LocalName | undefined,
|
|
888
|
+
isDue: (instance: Instance) => boolean,
|
|
889
|
+
) => {
|
|
890
|
+
const count = pending.findIndex((instance) => !isDue(instance));
|
|
891
|
+
return pending
|
|
892
|
+
.splice(0, count === -1 ? pending.length : count)
|
|
893
|
+
.reduce((parent, instance) => resolveLocalInstance(instance, parent, unit), current);
|
|
894
|
+
};
|
|
895
|
+
const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
|
|
896
|
+
const current = isKey(definition.name)
|
|
897
|
+
? previous
|
|
898
|
+
: resolveInstances(previous, (instance) => !isBefore(definition.location, instance.location));
|
|
899
|
+
const local: LocalName = {
|
|
900
|
+
name: definition.name,
|
|
901
|
+
arity: getArity(definition),
|
|
902
|
+
kind: "Definition",
|
|
903
|
+
range: definition.location.range,
|
|
904
|
+
parent: current,
|
|
905
|
+
};
|
|
906
|
+
if (isInstantiated(definition)) return local;
|
|
907
|
+
const isInstanced = instances.some((instance) => instance.location === definition.location);
|
|
908
|
+
const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
|
|
909
|
+
const inner = isInstanced
|
|
910
|
+
? { ...unit, isGenerated: true, errors: [], warnings: [] }
|
|
911
|
+
: isGenerated
|
|
912
|
+
? { ...unit, isGenerated }
|
|
913
|
+
: unit;
|
|
914
|
+
return resolveDefinition(definition, current, inner, recursive)
|
|
915
|
+
? { ...local, params: getParamArities(definition) }
|
|
916
|
+
: current;
|
|
917
|
+
}, declared);
|
|
918
|
+
return resolveInstances(resolved, () => true);
|
|
198
919
|
};
|
|
199
920
|
|
|
200
|
-
//
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
921
|
+
// The INSTANCE that a name such as K!C starts with, whose arguments SANY requires at its name.
|
|
922
|
+
const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
|
|
923
|
+
const prefix = name.split("!")[0];
|
|
924
|
+
const binding =
|
|
925
|
+
prefix === name
|
|
926
|
+
? undefined
|
|
927
|
+
: (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
|
|
928
|
+
return binding?.kind === "Instance" ? { name: prefix, arity: binding.arity } : undefined;
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
// Generator.selectorToNode reports an INSTANCE given the wrong number of arguments at its name,
|
|
932
|
+
// as in K!C or K(1, 2)!C, and goes on without analyzing them. Tells whether it reports one.
|
|
933
|
+
const isInstanceArityReported = (
|
|
934
|
+
expression: Expression,
|
|
935
|
+
scope: LocalName | undefined,
|
|
936
|
+
unit: Unit,
|
|
208
937
|
) => {
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
938
|
+
if (expression.kind !== "Identifier" && expression.kind !== "Apply") return false;
|
|
939
|
+
const instance = findInstance(
|
|
940
|
+
expression.kind === "Identifier" ? expression.name : expression.operator,
|
|
941
|
+
scope,
|
|
942
|
+
unit,
|
|
943
|
+
);
|
|
944
|
+
const count = expression.kind === "Apply" ? (expression.argumentCounts?.[0] ?? 0) : 0;
|
|
945
|
+
if (!instance || count === instance.arity) return false;
|
|
946
|
+
const range =
|
|
947
|
+
(expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ??
|
|
948
|
+
expression.location.range;
|
|
949
|
+
if (range) {
|
|
950
|
+
unit.errors.push({
|
|
951
|
+
location: formatRange(range),
|
|
952
|
+
message: `The operator ${instance.name} requires ${instance.arity} arguments.`,
|
|
953
|
+
});
|
|
222
954
|
}
|
|
955
|
+
return true;
|
|
223
956
|
};
|
|
224
957
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
: undefined;
|
|
231
|
-
|
|
958
|
+
// Generator.generateExprOrOpArg analyzes an argument as an expression, unless a defined operator
|
|
959
|
+
// takes an operator in its place, which a name, a built-in operator, or a LAMBDA of the parameter's
|
|
960
|
+
// arity must give. OpDefNode#match then reports each argument that gives no such operator, or one
|
|
961
|
+
// whose own parameters take operators. tla-ts lets an operator whose parameters it does not know
|
|
962
|
+
// take any argument.
|
|
232
963
|
const resolveApply = (
|
|
233
964
|
expression: Expression & { kind: "Apply" },
|
|
234
965
|
scope: LocalName | undefined,
|
|
235
966
|
unit: Unit,
|
|
236
967
|
) => {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const arity = getSymbolArity(expression,
|
|
968
|
+
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
969
|
+
const operator = expression.operator;
|
|
970
|
+
const callee = findBinding(operator, scope, unit);
|
|
971
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
972
|
+
if (arity === undefined) return;
|
|
241
973
|
if (arity !== expression.args.length) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
974
|
+
unit.errors.push({
|
|
975
|
+
location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
|
|
976
|
+
message: `The operator ${operator} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
|
|
977
|
+
});
|
|
978
|
+
return;
|
|
245
979
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
980
|
+
const params = callee && (callee.kind === "Definition" ? callee.params : []);
|
|
981
|
+
if (params === undefined) {
|
|
982
|
+
expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
const report = (range: SourceRange | undefined, message: string) => {
|
|
986
|
+
if (range) unit.errors.push({ location: formatRange(range), message });
|
|
987
|
+
};
|
|
988
|
+
expression.args
|
|
989
|
+
.map((arg, index) => {
|
|
990
|
+
const expected = params[index] ?? 0;
|
|
991
|
+
const range = arg.location.range;
|
|
992
|
+
if (expected === 0) {
|
|
993
|
+
// SANY fails on an operator here with an internal error, so tla-ts words it as it does for
|
|
994
|
+
// a name.
|
|
995
|
+
if (arg.kind === "Lambda" && arg.operator !== undefined) {
|
|
996
|
+
report(range, `The operator ${arg.operator} requires 1 arguments.`);
|
|
997
|
+
} else if (arg.kind === "Lambda" && range) {
|
|
998
|
+
report(range, "LAMBDA expression used where an expression is required.");
|
|
999
|
+
} else if (arg.kind === "OperatorReference") {
|
|
1000
|
+
const argArity = getSymbolArity(arg, arg.operator, scope, unit);
|
|
1001
|
+
if (argArity !== undefined) {
|
|
1002
|
+
report(range, `The operator ${arg.operator} requires ${argArity} arguments.`);
|
|
1003
|
+
}
|
|
1004
|
+
} else resolveExpression(arg, scope, unit);
|
|
1005
|
+
return true;
|
|
1006
|
+
}
|
|
1007
|
+
// A name that selects a subexpression gives an operator of the names it leaves unbound.
|
|
1008
|
+
if (arg.kind === "Lambda") {
|
|
1009
|
+
resolveExpression(arg, scope, unit);
|
|
1010
|
+
const isMatched = arg.params.length === expected;
|
|
1011
|
+
if (!isMatched && selections.has(arg)) {
|
|
1012
|
+
report(
|
|
1013
|
+
range,
|
|
1014
|
+
`Expected arity ${expected} but found operator of arity ${arg.params.length}.`,
|
|
1015
|
+
);
|
|
1016
|
+
} else if (!isMatched && range && arg.operator === undefined) {
|
|
1017
|
+
report(
|
|
1018
|
+
expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator),
|
|
1019
|
+
`Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
return isMatched;
|
|
1023
|
+
}
|
|
1024
|
+
if (arg.kind === "Subexpression") {
|
|
1025
|
+
resolveExpression(arg, scope, unit);
|
|
1026
|
+
return true;
|
|
1027
|
+
}
|
|
1028
|
+
// Generator.selectorToNode: a name such as K(1)!C names an operator, but none of its parts
|
|
1029
|
+
// may take arguments.
|
|
1030
|
+
if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
|
|
1031
|
+
const index = arg.argumentCounts.findIndex((count) => count > 0);
|
|
1032
|
+
report(
|
|
1033
|
+
arg.selectorRanges?.[index],
|
|
1034
|
+
`Selector \`${arg.operator.split("!")[index]}' should not have argument(s).`,
|
|
1035
|
+
);
|
|
1036
|
+
return false;
|
|
1037
|
+
}
|
|
1038
|
+
if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
|
|
1039
|
+
report(
|
|
1040
|
+
range,
|
|
1041
|
+
`An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
|
|
1042
|
+
);
|
|
1043
|
+
return false;
|
|
1044
|
+
}
|
|
1045
|
+
const name = arg.kind === "Identifier" ? arg.name : arg.operator;
|
|
1046
|
+
const argParams = findBinding(name, scope, unit)?.params ?? [];
|
|
1047
|
+
const argArity = getSymbolArity(arg, name, scope, unit);
|
|
1048
|
+
if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
|
|
1049
|
+
report(
|
|
1050
|
+
getSpelledNameRange(arg, name),
|
|
1051
|
+
`Expected arity ${expected} but found operator of arity ${argArity}.`,
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
return argArity === expected && argParams.every((param) => param === 0);
|
|
1055
|
+
})
|
|
1056
|
+
.forEach((isMatched, index) => {
|
|
1057
|
+
if (!isMatched) {
|
|
1058
|
+
report(
|
|
1059
|
+
expression.location.range,
|
|
1060
|
+
`Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
|
|
1061
|
+
);
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
253
1064
|
};
|
|
254
1065
|
|
|
255
1066
|
// An argument may name an operator that takes arguments of its own.
|
|
256
1067
|
const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
257
|
-
if (arg.kind === "Identifier")
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
1068
|
+
if (arg.kind === "Identifier") getSymbolArity(arg, arg.name, scope, unit);
|
|
1069
|
+
else resolveExpression(arg, scope, unit);
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
// Reports the error of a name that selects nothing, after the arguments SANY analyzes before it.
|
|
1073
|
+
const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
|
|
1074
|
+
const failed = selectionErrors.get(expression);
|
|
1075
|
+
if (!failed) return false;
|
|
1076
|
+
failed.args.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1077
|
+
unit.errors.push(failed.error);
|
|
1078
|
+
return true;
|
|
261
1079
|
};
|
|
262
1080
|
|
|
263
1081
|
const resolveExpression = (
|
|
@@ -265,8 +1083,16 @@ const resolveExpression = (
|
|
|
265
1083
|
scope: LocalName | undefined,
|
|
266
1084
|
unit: Unit,
|
|
267
1085
|
): void => {
|
|
1086
|
+
if (selections.has(expression) && !unit.isGenerated) {
|
|
1087
|
+
resolveExpression(expression, scope, { ...unit, isGenerated: true });
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
268
1090
|
const resolveAll = (expressions: Expression[]) =>
|
|
269
1091
|
expressions.forEach((child) => resolveExpression(child, scope, unit));
|
|
1092
|
+
const bindLabelParams = (names: string[]) => ({
|
|
1093
|
+
...unit,
|
|
1094
|
+
labelParams: [...unit.labelParams, ...names],
|
|
1095
|
+
});
|
|
270
1096
|
switch (expression.kind) {
|
|
271
1097
|
case "Number":
|
|
272
1098
|
case "Decimal":
|
|
@@ -274,11 +1100,14 @@ const resolveExpression = (
|
|
|
274
1100
|
case "ExceptAt":
|
|
275
1101
|
return;
|
|
276
1102
|
case "Identifier": {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
1103
|
+
if (isInstanceArityReported(expression, scope, unit)) return;
|
|
1104
|
+
const name = expression.name;
|
|
1105
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
1106
|
+
if (arity !== undefined && arity > 0) {
|
|
1107
|
+
unit.errors.push({
|
|
1108
|
+
location: formatRange(getSpelledNameRange(expression, name)),
|
|
1109
|
+
message: `The operator ${name} requires ${arity} arguments.`,
|
|
1110
|
+
});
|
|
282
1111
|
}
|
|
283
1112
|
return;
|
|
284
1113
|
}
|
|
@@ -289,7 +1118,11 @@ const resolveExpression = (
|
|
|
289
1118
|
resolveApply(expression, scope, unit);
|
|
290
1119
|
return;
|
|
291
1120
|
case "Lambda":
|
|
292
|
-
resolveExpression(
|
|
1121
|
+
resolveExpression(
|
|
1122
|
+
expression.body,
|
|
1123
|
+
bindNames(scope, expression.params, expression.paramRanges, unit),
|
|
1124
|
+
bindLabelParams(expression.params),
|
|
1125
|
+
);
|
|
293
1126
|
return;
|
|
294
1127
|
case "And":
|
|
295
1128
|
case "Or":
|
|
@@ -312,24 +1145,32 @@ const resolveExpression = (
|
|
|
312
1145
|
case "Let":
|
|
313
1146
|
resolveExpression(
|
|
314
1147
|
expression.body,
|
|
315
|
-
resolveDefinitions(expression.definitions, scope, unit),
|
|
1148
|
+
resolveDefinitions(expression.definitions, scope, unit, expression.instances),
|
|
316
1149
|
unit,
|
|
317
1150
|
);
|
|
318
1151
|
return;
|
|
319
1152
|
case "Forall":
|
|
320
1153
|
case "Exists":
|
|
321
1154
|
case "FunctionConstruction":
|
|
322
|
-
resolveExpression(
|
|
1155
|
+
resolveExpression(
|
|
1156
|
+
expression.body,
|
|
1157
|
+
resolveBounds(expression.bounds, scope, unit),
|
|
1158
|
+
bindLabelParams(getBoundNames(expression.bounds)),
|
|
1159
|
+
);
|
|
323
1160
|
return;
|
|
324
1161
|
case "UnboundedQuantifier":
|
|
325
|
-
resolveExpression(
|
|
1162
|
+
resolveExpression(
|
|
1163
|
+
expression.body,
|
|
1164
|
+
bindNames(scope, expression.names, expression.nameRanges, unit),
|
|
1165
|
+
bindLabelParams(expression.names),
|
|
1166
|
+
);
|
|
326
1167
|
return;
|
|
327
1168
|
case "Choose":
|
|
328
1169
|
if (expression.set) resolveExpression(expression.set, scope, unit);
|
|
329
1170
|
resolveExpression(
|
|
330
1171
|
expression.body,
|
|
331
|
-
bindNames(scope, getPatternNames(expression.pattern)),
|
|
332
|
-
|
|
1172
|
+
bindNames(scope, getPatternNames(expression.pattern), expression.pattern.ranges, unit),
|
|
1173
|
+
bindLabelParams(getPatternNames(expression.pattern)),
|
|
333
1174
|
);
|
|
334
1175
|
return;
|
|
335
1176
|
case "SetEnumeration":
|
|
@@ -337,31 +1178,40 @@ const resolveExpression = (
|
|
|
337
1178
|
resolveAll(expression.elements);
|
|
338
1179
|
return;
|
|
339
1180
|
case "SetFilter":
|
|
340
|
-
resolveExpression(
|
|
1181
|
+
resolveExpression(
|
|
1182
|
+
expression.predicate,
|
|
1183
|
+
resolveBounds([expression.bound], scope, unit),
|
|
1184
|
+
bindLabelParams(getPatternNames(expression.bound.pattern)),
|
|
1185
|
+
);
|
|
341
1186
|
return;
|
|
342
1187
|
case "SetMap":
|
|
343
|
-
resolveExpression(
|
|
1188
|
+
resolveExpression(
|
|
1189
|
+
expression.expression,
|
|
1190
|
+
resolveBounds(expression.bounds, scope, unit),
|
|
1191
|
+
bindLabelParams(getBoundNames(expression.bounds)),
|
|
1192
|
+
);
|
|
344
1193
|
return;
|
|
345
1194
|
case "FunctionSet":
|
|
346
1195
|
resolveAll([expression.domain, expression.range]);
|
|
347
1196
|
return;
|
|
348
1197
|
case "Record":
|
|
349
1198
|
case "RecordSet":
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
1199
|
+
expression.fields.forEach((field, index) => {
|
|
1200
|
+
if (expression.fields.slice(0, index).some((earlier) => earlier.name === field.name)) {
|
|
1201
|
+
unit.errors.push({
|
|
1202
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
1203
|
+
message: "Non-unique fields in constructor.",
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
resolveExpression(field.value, scope, unit);
|
|
1207
|
+
});
|
|
356
1208
|
return;
|
|
357
1209
|
case "Except":
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
]),
|
|
364
|
-
]);
|
|
1210
|
+
resolveExpression(expression.function, scope, unit);
|
|
1211
|
+
expression.updates.forEach((update) => {
|
|
1212
|
+
resolveAll(update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])));
|
|
1213
|
+
resolveExpression(update.value, scope, { ...unit, isInExcept: true });
|
|
1214
|
+
});
|
|
365
1215
|
return;
|
|
366
1216
|
case "FunctionApplication":
|
|
367
1217
|
resolveAll([expression.function, ...expression.args]);
|
|
@@ -381,34 +1231,77 @@ const resolveExpression = (
|
|
|
381
1231
|
resolveAll([expression.subscript, expression.action]);
|
|
382
1232
|
return;
|
|
383
1233
|
case "Label":
|
|
384
|
-
|
|
1234
|
+
if (isLabelReported(expression, unit)) return;
|
|
1235
|
+
resolveExpression(expression.body, scope, { ...unit, labelParams: [], labels: new Set() });
|
|
1236
|
+
checkLabel(expression, scope, unit);
|
|
385
1237
|
return;
|
|
386
|
-
case "Subexpression":
|
|
1238
|
+
case "Subexpression": {
|
|
1239
|
+
if (isSelectionReported(expression, scope, unit)) return;
|
|
387
1240
|
expression.selectors
|
|
388
1241
|
.flatMap((selector) => ("args" in selector ? selector.args : []))
|
|
389
1242
|
.forEach((arg) => resolveArgument(arg, scope, unit));
|
|
1243
|
+
checkReference(expression, scope, unit, false);
|
|
1244
|
+
}
|
|
390
1245
|
}
|
|
391
1246
|
};
|
|
392
1247
|
|
|
1248
|
+
// Generator.generateUseOrHide looks up a fact that is a name as it looks up a name in an expression,
|
|
1249
|
+
// except that the name may select an ASSUME/PROVE or a NEW clause or name a theorem or assumption,
|
|
1250
|
+
// and analyzes any other fact as an expression.
|
|
1251
|
+
const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
|
|
1252
|
+
facts.forEach((fact) => {
|
|
1253
|
+
if (
|
|
1254
|
+
fact.kind === "ModuleFact" ||
|
|
1255
|
+
isInstanceArityReported(fact, scope, unit) ||
|
|
1256
|
+
checkReference(fact, scope, unit, true)
|
|
1257
|
+
) {
|
|
1258
|
+
return;
|
|
1259
|
+
}
|
|
1260
|
+
const name =
|
|
1261
|
+
fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
|
|
1262
|
+
if (fact.kind === "Identifier" && lookupLocal(scope, name)?.isNonFact) {
|
|
1263
|
+
unit.errors.push({
|
|
1264
|
+
location: formatRange(getSpelledNameRange(fact, name)),
|
|
1265
|
+
message: "Step number of non-fact used as a fact",
|
|
1266
|
+
});
|
|
1267
|
+
} else if (!unit.facts.has(name)) resolveExpression(fact, scope, unit);
|
|
1268
|
+
});
|
|
1269
|
+
|
|
393
1270
|
const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
|
|
394
1271
|
if (symbol.set) resolveExpression(symbol.set, scope, unit);
|
|
395
|
-
return
|
|
1272
|
+
return bindName(
|
|
1273
|
+
scope,
|
|
1274
|
+
symbol.name,
|
|
1275
|
+
{ kind: `New${symbol.level}`, arity: symbol.arity, range: symbol.nameRange },
|
|
1276
|
+
unit,
|
|
1277
|
+
);
|
|
396
1278
|
};
|
|
397
1279
|
|
|
398
|
-
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows.
|
|
1280
|
+
// The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows. SANY
|
|
1281
|
+
// forbids a label in the scope of a NEW in a nested ASSUME.
|
|
399
1282
|
const resolveAssumeProve = (
|
|
400
1283
|
assumeProve: AssumeProve,
|
|
401
1284
|
scope: LocalName | undefined,
|
|
402
1285
|
unit: Unit,
|
|
1286
|
+
isNested = false,
|
|
403
1287
|
): LocalName | undefined => {
|
|
404
|
-
const assumed = assumeProve.assumptions.reduce(
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
1288
|
+
const assumed = assumeProve.assumptions.reduce(
|
|
1289
|
+
(current, assumption) => {
|
|
1290
|
+
if (assumption.kind === "NewSymbol") {
|
|
1291
|
+
return {
|
|
1292
|
+
scope: resolveNewSymbol(assumption, current.scope, current.unit),
|
|
1293
|
+
unit: isNested ? { ...current.unit, isLabelForbidden: true } : current.unit,
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
if (assumption.kind === "AssumeProve") {
|
|
1297
|
+
resolveAssumeProve(assumption, current.scope, current.unit, true);
|
|
1298
|
+
} else resolveExpression(assumption, current.scope, current.unit);
|
|
1299
|
+
return current;
|
|
1300
|
+
},
|
|
1301
|
+
{ scope, unit },
|
|
1302
|
+
);
|
|
1303
|
+
resolveExpression(assumeProve.conclusion, assumed.scope, assumed.unit);
|
|
1304
|
+
return assumed.scope;
|
|
412
1305
|
};
|
|
413
1306
|
|
|
414
1307
|
const resolveStatement = (
|
|
@@ -423,371 +1316,526 @@ const resolveStatement = (
|
|
|
423
1316
|
|
|
424
1317
|
// SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
|
|
425
1318
|
// theorems, assumptions, and modules.
|
|
426
|
-
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined,
|
|
1319
|
+
const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
|
|
1320
|
+
const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
|
|
1321
|
+
if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
|
|
427
1322
|
if (proof?.kind !== "Steps") return;
|
|
428
1323
|
proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
|
|
429
1324
|
};
|
|
430
1325
|
|
|
1326
|
+
// A LET or a step binds the name of the INSTANCE after the names it adds.
|
|
1327
|
+
const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
|
|
1328
|
+
const instanced = resolveInstance(instance, scope, unit);
|
|
1329
|
+
return instance.name === undefined
|
|
1330
|
+
? instanced
|
|
1331
|
+
: bindName(
|
|
1332
|
+
instanced,
|
|
1333
|
+
instance.name,
|
|
1334
|
+
{ kind: "Instance", arity: instance.params.length, range: instance.location.range },
|
|
1335
|
+
unit,
|
|
1336
|
+
);
|
|
1337
|
+
};
|
|
1338
|
+
|
|
1339
|
+
// Generator.generateProof binds the name of a step after its statement and before its proof, or
|
|
1340
|
+
// after the step where it takes no proof.
|
|
431
1341
|
const resolveStep = (
|
|
432
1342
|
step: ProofStep,
|
|
433
1343
|
scope: LocalName | undefined,
|
|
434
1344
|
unit: Unit,
|
|
435
1345
|
): LocalName | undefined => {
|
|
436
1346
|
const body = step.body;
|
|
1347
|
+
const stepUnit =
|
|
1348
|
+
step.name === undefined
|
|
1349
|
+
? unit
|
|
1350
|
+
: { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
|
|
1351
|
+
const bindStep = (parent: LocalName | undefined): LocalName | undefined =>
|
|
1352
|
+
step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent };
|
|
1353
|
+
const bindNonFactStep = (parent: LocalName | undefined): LocalName | undefined =>
|
|
1354
|
+
step.name === undefined
|
|
1355
|
+
? parent
|
|
1356
|
+
: { name: step.name, arity: 0, kind: "Theorem", parent, isNonFact: true };
|
|
437
1357
|
switch (body.kind) {
|
|
1358
|
+
// The NEW symbols of a SUFFICES ASSUME are in scope after its proof, and those of any other
|
|
1359
|
+
// ASSUME in its proof.
|
|
438
1360
|
case "Assert": {
|
|
439
|
-
const assumed = resolveStatement(body.statement, scope,
|
|
440
|
-
|
|
441
|
-
|
|
1361
|
+
const assumed = resolveStatement(body.statement, scope, stepUnit);
|
|
1362
|
+
const proofScope = body.isSuffices ? scope : assumed;
|
|
1363
|
+
const name = step.name;
|
|
1364
|
+
if (name === undefined) {
|
|
1365
|
+
resolveProof(step.proof, proofScope, unit);
|
|
1366
|
+
return body.isSuffices ? assumed : scope;
|
|
1367
|
+
}
|
|
1368
|
+
const goal = { statement: body.statement, isSuffices: body.isSuffices, isInProof: true };
|
|
1369
|
+
resolveProof(step.proof, { name, arity: 0, kind: "Theorem", parent: proofScope, goal }, unit);
|
|
1370
|
+
goal.isInProof = false;
|
|
1371
|
+
return { name, arity: 0, kind: "Theorem", parent: body.isSuffices ? assumed : scope, goal };
|
|
442
1372
|
}
|
|
1373
|
+
case "Use":
|
|
1374
|
+
case "Hide":
|
|
1375
|
+
checkFacts(body.facts, scope, unit);
|
|
1376
|
+
return bindNonFactStep(scope);
|
|
1377
|
+
case "Instance":
|
|
1378
|
+
return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
|
|
443
1379
|
case "Define":
|
|
444
|
-
return
|
|
445
|
-
body.definitions.
|
|
446
|
-
definition
|
|
1380
|
+
return bindNonFactStep(
|
|
1381
|
+
body.definitions.reduce(
|
|
1382
|
+
(current, definition) =>
|
|
1383
|
+
definition.kind === "Instance"
|
|
1384
|
+
? resolveLocalInstance(definition, current, stepUnit)
|
|
1385
|
+
: resolveDefinitions([definition], current, unit),
|
|
1386
|
+
scope,
|
|
447
1387
|
),
|
|
448
|
-
scope,
|
|
449
|
-
unit,
|
|
450
1388
|
);
|
|
451
1389
|
case "Have":
|
|
452
1390
|
case "Case":
|
|
453
|
-
resolveExpression(body.expression, scope,
|
|
454
|
-
resolveProof(step.proof, scope, unit);
|
|
455
|
-
return scope;
|
|
1391
|
+
resolveExpression(body.expression, scope, stepUnit);
|
|
1392
|
+
resolveProof(step.proof, bindStep(scope), unit);
|
|
1393
|
+
return bindStep(scope);
|
|
456
1394
|
case "Witness":
|
|
457
|
-
body.expressions.forEach((expression) => resolveExpression(expression, scope,
|
|
458
|
-
return scope;
|
|
1395
|
+
body.expressions.forEach((expression) => resolveExpression(expression, scope, stepUnit));
|
|
1396
|
+
return bindStep(scope);
|
|
459
1397
|
case "Take":
|
|
460
|
-
return
|
|
1398
|
+
return bindStep(
|
|
1399
|
+
bindNames(
|
|
1400
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
1401
|
+
body.names,
|
|
1402
|
+
body.nameRanges,
|
|
1403
|
+
stepUnit,
|
|
1404
|
+
),
|
|
1405
|
+
);
|
|
461
1406
|
case "Pick": {
|
|
462
|
-
const picked = bindNames(
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
1407
|
+
const picked = bindNames(
|
|
1408
|
+
resolveBounds(body.bounds, scope, stepUnit),
|
|
1409
|
+
body.names,
|
|
1410
|
+
body.nameRanges,
|
|
1411
|
+
stepUnit,
|
|
1412
|
+
);
|
|
1413
|
+
resolveExpression(body.body, picked, {
|
|
1414
|
+
...stepUnit,
|
|
1415
|
+
labelParams: [...stepUnit.labelParams, ...getBoundNames(body.bounds), ...body.names],
|
|
1416
|
+
});
|
|
1417
|
+
resolveProof(step.proof, bindStep(scope), unit);
|
|
1418
|
+
return bindStep(picked);
|
|
466
1419
|
}
|
|
467
1420
|
default:
|
|
468
|
-
resolveProof(step.proof, scope, unit);
|
|
469
|
-
return scope;
|
|
1421
|
+
resolveProof(step.proof, bindStep(scope), unit);
|
|
1422
|
+
return bindStep(scope);
|
|
470
1423
|
}
|
|
471
1424
|
};
|
|
472
1425
|
|
|
1426
|
+
// SANY binds the name of a theorem after its statement and before its proof.
|
|
473
1427
|
const resolveTheorem = (theorem: Theorem, unit: Unit) => {
|
|
474
1428
|
const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
|
|
475
1429
|
const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
|
|
1430
|
+
if (theorem.name !== undefined) {
|
|
1431
|
+
bindModuleName(
|
|
1432
|
+
theorem.name,
|
|
1433
|
+
{ kind: "Theorem", arity: 0, range: theorem.location.range },
|
|
1434
|
+
unit,
|
|
1435
|
+
);
|
|
1436
|
+
}
|
|
1437
|
+
const goal = unit.goals.get(theorem.name ?? "");
|
|
1438
|
+
if (goal) goal.isInProof = true;
|
|
476
1439
|
resolveProof(theorem.proof, scope, unit);
|
|
1440
|
+
if (goal) goal.isInProof = false;
|
|
477
1441
|
};
|
|
478
1442
|
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
1443
|
+
// The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
|
|
1444
|
+
// module that instantiates it.
|
|
1445
|
+
interface InstancedModule {
|
|
1446
|
+
declarations: Declaration[];
|
|
1447
|
+
symbols: Symbols;
|
|
1448
|
+
instancer: string;
|
|
484
1449
|
}
|
|
485
1450
|
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
1451
|
+
// What SANY reports at a location as it links a module, such as a conflict between the modules it
|
|
1452
|
+
// extends.
|
|
1453
|
+
export interface ModuleReport {
|
|
489
1454
|
location: SourceLocation;
|
|
490
|
-
|
|
1455
|
+
errors: SanyError[];
|
|
1456
|
+
warnings: SanyError[];
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
export interface ModuleUnit {
|
|
1460
|
+
imported: Symbols;
|
|
1461
|
+
getInstance: (instance: Instance) => InstancedModule;
|
|
1462
|
+
// The built-in operators and the names of the enclosing and extended modules.
|
|
1463
|
+
bound: Symbols;
|
|
1464
|
+
definitions: Map<string, Definition>;
|
|
1465
|
+
getKey: (definition: Definition) => string;
|
|
1466
|
+
// The names each INSTANCE adds.
|
|
1467
|
+
instanced: Map<Instance, Symbols>;
|
|
1468
|
+
// The theorems and assumptions the extended and instantiated modules export.
|
|
1469
|
+
facts: Set<string>;
|
|
1470
|
+
reports: ModuleReport[];
|
|
1471
|
+
warnings: SanyError[];
|
|
491
1472
|
}
|
|
492
1473
|
|
|
493
|
-
|
|
1474
|
+
export const MODULE_START: SourceLocation = { line: 0, column: 0 };
|
|
494
1475
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
1476
|
+
// A named theorem or assumption is linked as a definition too.
|
|
1477
|
+
export const isTheoremDefinition = (module: Module, definition: Definition) =>
|
|
1478
|
+
[...module.theorems, ...module.assumptions].some(
|
|
1479
|
+
(item) =>
|
|
1480
|
+
item.name === definition.name &&
|
|
1481
|
+
item.location.line === definition.location.line &&
|
|
1482
|
+
item.location.column === definition.location.column,
|
|
498
1483
|
);
|
|
499
1484
|
|
|
500
|
-
const
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
) =>
|
|
506
|
-
|
|
507
|
-
if (lookupLocal(parent, name) || isDefined(name)) throw createRedefinitionError(name, location);
|
|
508
|
-
return { name, arity: 0, parent };
|
|
509
|
-
}, scope);
|
|
510
|
-
|
|
511
|
-
const checkBoundNames = (
|
|
512
|
-
bounds: Bound[],
|
|
513
|
-
scope: LocalName | undefined,
|
|
514
|
-
isDefined: IsDefined,
|
|
515
|
-
location: SourceLocation,
|
|
516
|
-
) => {
|
|
517
|
-
bounds.forEach((bound) => checkNames(bound.set, scope, isDefined));
|
|
518
|
-
return bounds.reduce(
|
|
519
|
-
(current, bound) => bindUnique(current, getPatternNames(bound.pattern), isDefined, location),
|
|
520
|
-
scope,
|
|
1485
|
+
const createSymbols = (module: Module, imported: Symbols) => {
|
|
1486
|
+
const symbols = new Map([...CORE_SYMBOLS, ...imported]);
|
|
1487
|
+
module.constants.forEach((constant) =>
|
|
1488
|
+
symbols.set(constant.name, { key: constant.name, arity: constant.arity, kind: "Constant" }),
|
|
1489
|
+
);
|
|
1490
|
+
module.variables.forEach((variable) =>
|
|
1491
|
+
symbols.set(variable.name, { key: variable.name, arity: 0, kind: "Variable" }),
|
|
521
1492
|
);
|
|
1493
|
+
return symbols;
|
|
522
1494
|
};
|
|
523
1495
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
1496
|
+
// Generator.processSubst analyzes the explicit substitutions in the order they appear, then the
|
|
1497
|
+
// implicit ones, and then reports each declaration left without one. The parameters of the INSTANCE
|
|
1498
|
+
// are in scope.
|
|
1499
|
+
const checkSubstitutions = (
|
|
1500
|
+
instance: Instance,
|
|
1501
|
+
declarations: Declaration[],
|
|
1502
|
+
instancer: string,
|
|
1503
|
+
outer: LocalName | undefined,
|
|
1504
|
+
unit: Unit,
|
|
528
1505
|
) => {
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
checkNames(definition.body, bound, isDefined);
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
const params = bindUnique(
|
|
536
|
-
scope,
|
|
537
|
-
definition.params.map((param) => param.name),
|
|
538
|
-
isDefined,
|
|
539
|
-
definition.location,
|
|
1506
|
+
const scope = instance.params.reduce<LocalName | undefined>(
|
|
1507
|
+
(parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
|
|
1508
|
+
outer,
|
|
540
1509
|
);
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
const checkNames = (
|
|
545
|
-
expression: Expression,
|
|
546
|
-
scope: LocalName | undefined,
|
|
547
|
-
isDefined: IsDefined,
|
|
548
|
-
): void => {
|
|
549
|
-
const location = expression.location;
|
|
550
|
-
switch (expression.kind) {
|
|
551
|
-
case "Lambda":
|
|
552
|
-
checkNames(
|
|
553
|
-
expression.body,
|
|
554
|
-
bindUnique(scope, expression.params, isDefined, location),
|
|
555
|
-
isDefined,
|
|
556
|
-
);
|
|
557
|
-
return;
|
|
558
|
-
case "Let": {
|
|
559
|
-
const recursive = expression.definitions
|
|
560
|
-
.filter((definition) => definition.isRecursive)
|
|
561
|
-
.map((definition) => definition.name);
|
|
562
|
-
const inner = expression.definitions.reduce((current, definition) => {
|
|
563
|
-
if (lookupLocal(current, definition.name) || isDefined(definition.name)) {
|
|
564
|
-
throw createRedefinitionError(definition.name, definition.location);
|
|
565
|
-
}
|
|
566
|
-
checkDefinitionNames(definition, bindNames(current, recursive), isDefined);
|
|
567
|
-
return bindNames(current, [definition.name]);
|
|
568
|
-
}, scope);
|
|
569
|
-
checkNames(
|
|
570
|
-
expression.body,
|
|
571
|
-
bindUnique(
|
|
572
|
-
inner,
|
|
573
|
-
(expression.instances ?? []).flatMap((instance) =>
|
|
574
|
-
instance.name === undefined ? [] : [instance.name],
|
|
575
|
-
),
|
|
576
|
-
isDefined,
|
|
577
|
-
location,
|
|
578
|
-
),
|
|
579
|
-
isDefined,
|
|
580
|
-
);
|
|
1510
|
+
const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
|
|
1511
|
+
if (declaration.arity === 0 || expression.kind !== "Identifier") {
|
|
1512
|
+
resolveExpression(expression, scope, unit);
|
|
581
1513
|
return;
|
|
582
1514
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
expression.body,
|
|
588
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
589
|
-
isDefined,
|
|
590
|
-
);
|
|
591
|
-
return;
|
|
592
|
-
case "SetFilter":
|
|
593
|
-
checkNames(
|
|
594
|
-
expression.predicate,
|
|
595
|
-
checkBoundNames([expression.bound], scope, isDefined, location),
|
|
596
|
-
isDefined,
|
|
597
|
-
);
|
|
598
|
-
return;
|
|
599
|
-
case "SetMap":
|
|
600
|
-
checkNames(
|
|
601
|
-
expression.expression,
|
|
602
|
-
checkBoundNames(expression.bounds, scope, isDefined, location),
|
|
603
|
-
isDefined,
|
|
604
|
-
);
|
|
605
|
-
return;
|
|
606
|
-
case "UnboundedQuantifier":
|
|
607
|
-
checkNames(
|
|
608
|
-
expression.body,
|
|
609
|
-
bindUnique(scope, expression.names, isDefined, location),
|
|
610
|
-
isDefined,
|
|
611
|
-
);
|
|
612
|
-
return;
|
|
613
|
-
case "Choose":
|
|
614
|
-
if (expression.set) checkNames(expression.set, scope, isDefined);
|
|
615
|
-
checkNames(
|
|
616
|
-
expression.body,
|
|
617
|
-
bindUnique(scope, getPatternNames(expression.pattern), isDefined, location),
|
|
618
|
-
isDefined,
|
|
1515
|
+
const arity = getSymbolArity(expression, expression.name, scope, unit);
|
|
1516
|
+
if (arity !== undefined && arity !== declaration.arity) {
|
|
1517
|
+
throw new TlaSyntaxError(
|
|
1518
|
+
`An operator must be substituted for symbol '${declaration.name}', and it must have arity ${declaration.arity} in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`,
|
|
619
1519
|
);
|
|
620
|
-
|
|
621
|
-
default:
|
|
622
|
-
getChildren(expression).forEach((child) => checkNames(child, scope, isDefined));
|
|
623
|
-
}
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
const getModuleNames = (module: Module) => [
|
|
627
|
-
...module.definitions.map((definition): ModuleName => ({
|
|
628
|
-
name: definition.name,
|
|
629
|
-
kind: "Definition",
|
|
630
|
-
location: definition.location,
|
|
631
|
-
isRecursive: definition.isRecursive,
|
|
632
|
-
})),
|
|
633
|
-
...module.instances.flatMap((instance): ModuleName[] =>
|
|
634
|
-
instance.name === undefined
|
|
635
|
-
? []
|
|
636
|
-
: [
|
|
637
|
-
{
|
|
638
|
-
name: instance.name,
|
|
639
|
-
kind: "Definition",
|
|
640
|
-
location: instance.location,
|
|
641
|
-
isRecursive: false,
|
|
642
|
-
},
|
|
643
|
-
],
|
|
644
|
-
),
|
|
645
|
-
...module.constants.map((constant): ModuleName => ({
|
|
646
|
-
name: constant.name,
|
|
647
|
-
kind: "Constant",
|
|
648
|
-
location: constant.location,
|
|
649
|
-
isRecursive: false,
|
|
650
|
-
})),
|
|
651
|
-
...module.variables.map((variable): ModuleName => ({
|
|
652
|
-
name: variable.name,
|
|
653
|
-
kind: "Variable",
|
|
654
|
-
location: variable.location,
|
|
655
|
-
isRecursive: false,
|
|
656
|
-
})),
|
|
657
|
-
];
|
|
658
|
-
|
|
659
|
-
export const checkRedefinitions = (module: Module, unit: ModuleUnit) => {
|
|
660
|
-
const byName = new Map<string, ModuleName[]>();
|
|
661
|
-
getModuleNames(module)
|
|
662
|
-
.sort((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
663
|
-
.forEach((entry) => byName.set(entry.name, [...(byName.get(entry.name) ?? []), entry]));
|
|
664
|
-
const isImportedBefore = (name: string, location: SourceLocation) => {
|
|
665
|
-
if (!unit.imported.has(name) && !CORE_SYMBOLS.has(name)) return false;
|
|
666
|
-
const importedAt = unit.importedAt.get(name);
|
|
667
|
-
return importedAt === undefined || isBefore(importedAt, location);
|
|
1520
|
+
}
|
|
668
1521
|
};
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
);
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
1522
|
+
const substitutions = new Map<string, Expression>();
|
|
1523
|
+
instance.substitutions.forEach((substitution) => {
|
|
1524
|
+
const name = substitution.name;
|
|
1525
|
+
const declaration = declarations.find((item) => item.name === name);
|
|
1526
|
+
if (!declaration) {
|
|
1527
|
+
unit.errors.push({
|
|
1528
|
+
location: formatRange(substitution.nameRange),
|
|
1529
|
+
message: `Identifier '${name}' is not a legal target of a substitution. \nA legal target must be a declared CONSTANT or VARIABLE in the module being instantiated. \n(Also, check for warnings about multiple declarations of this same identifier.)`,
|
|
1530
|
+
});
|
|
1531
|
+
return;
|
|
1532
|
+
}
|
|
1533
|
+
const expression = substitution.expression;
|
|
1534
|
+
resolveSubstitute(declaration, expression);
|
|
1535
|
+
const range = expression.location.range;
|
|
1536
|
+
if (!substitutions.has(name)) substitutions.set(name, expression);
|
|
1537
|
+
else if (range) {
|
|
1538
|
+
unit.errors.push({
|
|
1539
|
+
location: formatRange(range),
|
|
1540
|
+
message: `Multiple substitutions for symbol '${name}' in substitution.`,
|
|
1541
|
+
});
|
|
1542
|
+
}
|
|
1543
|
+
});
|
|
1544
|
+
const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
|
|
1545
|
+
const isMissing = (declaration: Declaration) =>
|
|
1546
|
+
!lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, unit);
|
|
1547
|
+
implicit
|
|
1548
|
+
.filter((declaration) => !isMissing(declaration))
|
|
1549
|
+
.forEach((declaration) => {
|
|
1550
|
+
const expression: Expression = {
|
|
1551
|
+
kind: "Identifier",
|
|
1552
|
+
name: declaration.name,
|
|
1553
|
+
location: instance.location,
|
|
1554
|
+
};
|
|
1555
|
+
resolveSubstitute(declaration, expression);
|
|
1556
|
+
substitutions.set(declaration.name, expression);
|
|
1557
|
+
});
|
|
1558
|
+
implicit.filter(isMissing).forEach((declaration) => {
|
|
1559
|
+
const range = instance.location.range;
|
|
1560
|
+
const declared = declaration.location.range;
|
|
1561
|
+
if (range && declared) {
|
|
1562
|
+
unit.errors.push({
|
|
1563
|
+
location: formatRange(range),
|
|
1564
|
+
message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
708
1567
|
});
|
|
1568
|
+
return substitutions;
|
|
709
1569
|
};
|
|
710
1570
|
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
);
|
|
716
|
-
|
|
717
|
-
|
|
1571
|
+
// Generator.processSubst analyzes an INSTANCE in a LET or a proof, which then binds the names the
|
|
1572
|
+
// INSTANCE adds.
|
|
1573
|
+
const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
|
|
1574
|
+
const instanced = unit.getInstance(instance);
|
|
1575
|
+
checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
|
|
1576
|
+
return [...instanced.symbols].reduce(
|
|
1577
|
+
(parent, [name, symbol]) => bindName(parent, name, symbol, unit),
|
|
1578
|
+
scope,
|
|
718
1579
|
);
|
|
719
|
-
return symbols;
|
|
720
1580
|
};
|
|
721
1581
|
|
|
1582
|
+
// tla-ts reports what SANY goes on after with an INSTANCE of the module.
|
|
722
1583
|
export const resolveSubstitutions = (
|
|
723
1584
|
instance: Instance,
|
|
724
|
-
declarations:
|
|
1585
|
+
declarations: Declaration[],
|
|
725
1586
|
module: Module,
|
|
726
1587
|
unit: ModuleUnit,
|
|
727
1588
|
) => {
|
|
728
|
-
const
|
|
729
|
-
const
|
|
730
|
-
|
|
1589
|
+
const imported = createSymbols(module, unit.imported);
|
|
1590
|
+
const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
|
|
1591
|
+
unit.reports.push(report);
|
|
1592
|
+
return checkSubstitutions(instance, declarations, module.name, undefined, {
|
|
1593
|
+
imported,
|
|
1594
|
+
getInstance: unit.getInstance,
|
|
731
1595
|
definitions: unit.definitions,
|
|
732
1596
|
getKey: unit.getKey,
|
|
733
1597
|
location: instance.location,
|
|
734
1598
|
self: undefined,
|
|
735
1599
|
facts: new Set(),
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
1600
|
+
goals: new Map(),
|
|
1601
|
+
bindings: new Map(
|
|
1602
|
+
[...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
|
|
1603
|
+
),
|
|
1604
|
+
errors: report.errors,
|
|
1605
|
+
warnings: report.warnings,
|
|
1606
|
+
hasLabelScope: false,
|
|
1607
|
+
isLabelForbidden: false,
|
|
1608
|
+
isInExcept: false,
|
|
1609
|
+
labelParams: [],
|
|
1610
|
+
labels: new Set(),
|
|
1611
|
+
isGenerated: false,
|
|
745
1612
|
});
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
1613
|
+
};
|
|
1614
|
+
|
|
1615
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module, after
|
|
1616
|
+
// the warnings it reports for the spec. A unit reports to its lists what SANY goes on after, and
|
|
1617
|
+
// throws the error that stops it. An error tla-ts cannot word as SANY does yields to the errors the
|
|
1618
|
+
// unit reported before it. Returns the errors.
|
|
1619
|
+
export const collectSemanticErrors = (
|
|
1620
|
+
units: {
|
|
1621
|
+
location: SourceLocation;
|
|
1622
|
+
check: (reported: SanyError[], warned: SanyError[]) => void;
|
|
1623
|
+
}[],
|
|
1624
|
+
warnings: SanyError[],
|
|
1625
|
+
) => {
|
|
1626
|
+
const errors: SanyError[] = [];
|
|
1627
|
+
const addUnique = (list: SanyError[], item: SanyError) => {
|
|
1628
|
+
if (
|
|
1629
|
+
list.every(
|
|
1630
|
+
(existing) => existing.location !== item.location || existing.message !== item.message,
|
|
1631
|
+
)
|
|
1632
|
+
) {
|
|
1633
|
+
list.push(item);
|
|
1634
|
+
}
|
|
1635
|
+
};
|
|
1636
|
+
units
|
|
1637
|
+
.toSorted((left, right) =>
|
|
1638
|
+
isBefore(left.location, right.location)
|
|
1639
|
+
? -1
|
|
1640
|
+
: isBefore(right.location, left.location)
|
|
1641
|
+
? 1
|
|
1642
|
+
: 0,
|
|
1643
|
+
)
|
|
1644
|
+
.forEach((unit) => {
|
|
1645
|
+
const reported: SanyError[] = [];
|
|
1646
|
+
const warned: SanyError[] = [];
|
|
1647
|
+
try {
|
|
1648
|
+
unit.check(reported, warned);
|
|
1649
|
+
} catch (error) {
|
|
1650
|
+
if (!(error instanceof TlaSyntaxError) || (!error.sany && reported.length === 0)) {
|
|
1651
|
+
throw error;
|
|
1652
|
+
}
|
|
1653
|
+
reported.push(...(error.sany?.errors ?? []));
|
|
765
1654
|
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
1655
|
+
reported.forEach((item) => addUnique(errors, item));
|
|
1656
|
+
warned.forEach((item) => addUnique(warnings, item));
|
|
1657
|
+
});
|
|
1658
|
+
return errors;
|
|
769
1659
|
};
|
|
770
1660
|
|
|
1661
|
+
// SANY binds the names of a module in the order of its units, and reports a RECURSIVE declaration
|
|
1662
|
+
// that no definition follows at the end. Returns the errors.
|
|
771
1663
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
772
1664
|
const symbols = createSymbols(module, unit.imported);
|
|
773
|
-
const facts = new Set(
|
|
774
|
-
|
|
1665
|
+
const facts = new Set([
|
|
1666
|
+
...unit.facts,
|
|
1667
|
+
...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
|
|
1668
|
+
]);
|
|
1669
|
+
const goals = new Map(
|
|
1670
|
+
module.theorems.flatMap((theorem): [string, Goal][] =>
|
|
1671
|
+
theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
|
|
1672
|
+
? [[theorem.name, { statement: theorem.statement, isSuffices: false, isInProof: false }]]
|
|
1673
|
+
: [],
|
|
1674
|
+
),
|
|
775
1675
|
);
|
|
776
|
-
const
|
|
1676
|
+
const bindings = new Map<string, ModuleBinding>(
|
|
1677
|
+
[...CORE_SYMBOLS, ...unit.bound].map(([name, symbol]) => [
|
|
1678
|
+
name,
|
|
1679
|
+
{ ...symbol, position: MODULE_START },
|
|
1680
|
+
]),
|
|
1681
|
+
);
|
|
1682
|
+
const recursive = new Set<Binding>();
|
|
1683
|
+
const declarations: { declaration: Declaration; binding: ModuleBinding | undefined }[] = [];
|
|
1684
|
+
const createUnit = (
|
|
1685
|
+
location: SourceLocation,
|
|
1686
|
+
self: Definition | undefined,
|
|
1687
|
+
errors: SanyError[],
|
|
1688
|
+
warnings: SanyError[],
|
|
1689
|
+
hasLabelScope: boolean,
|
|
1690
|
+
): Unit => ({
|
|
777
1691
|
imported: symbols,
|
|
1692
|
+
getInstance: unit.getInstance,
|
|
778
1693
|
definitions: unit.definitions,
|
|
779
1694
|
getKey: unit.getKey,
|
|
780
1695
|
location,
|
|
781
1696
|
self,
|
|
782
1697
|
facts,
|
|
1698
|
+
goals,
|
|
1699
|
+
bindings,
|
|
1700
|
+
errors,
|
|
1701
|
+
warnings,
|
|
1702
|
+
hasLabelScope,
|
|
1703
|
+
isLabelForbidden: false,
|
|
1704
|
+
isInExcept: false,
|
|
1705
|
+
labelParams: [],
|
|
1706
|
+
labels: new Set(),
|
|
1707
|
+
isGenerated: false,
|
|
783
1708
|
});
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
1709
|
+
const bindDeclarations = (declarations: Declaration[], kind: "Constant" | "Variable") =>
|
|
1710
|
+
declarations.map((declaration) => ({
|
|
1711
|
+
location: declaration.location,
|
|
1712
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1713
|
+
bindModuleName(
|
|
1714
|
+
declaration.name,
|
|
1715
|
+
{ kind, arity: declaration.arity, range: declaration.location.range },
|
|
1716
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1717
|
+
),
|
|
1718
|
+
}));
|
|
1719
|
+
return collectSemanticErrors(
|
|
1720
|
+
[
|
|
1721
|
+
...unit.reports.map((report) => ({
|
|
1722
|
+
location: report.location,
|
|
1723
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1724
|
+
errors.push(...report.errors);
|
|
1725
|
+
warnings.push(...report.warnings);
|
|
1726
|
+
},
|
|
1727
|
+
})),
|
|
1728
|
+
...bindDeclarations(module.constants, "Constant"),
|
|
1729
|
+
...bindDeclarations(module.variables, "Variable"),
|
|
1730
|
+
...module.units
|
|
1731
|
+
.flatMap((item) => (item.kind === "Recursive" ? item.declarations : []))
|
|
1732
|
+
.map((declaration) => ({
|
|
1733
|
+
location: declaration.location,
|
|
1734
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1735
|
+
const binding: ModuleBinding = {
|
|
1736
|
+
kind: "Definition",
|
|
1737
|
+
arity: declaration.arity,
|
|
1738
|
+
range: declaration.location.range,
|
|
1739
|
+
position: declaration.location,
|
|
1740
|
+
isPending: true,
|
|
1741
|
+
};
|
|
1742
|
+
const isBound = isBindable(
|
|
1743
|
+
declaration.name,
|
|
1744
|
+
bindings.get(declaration.name),
|
|
1745
|
+
binding,
|
|
1746
|
+
createUnit(declaration.location, undefined, errors, warnings, false),
|
|
1747
|
+
);
|
|
1748
|
+
if (isBound) {
|
|
1749
|
+
bindings.set(declaration.name, binding);
|
|
1750
|
+
recursive.add(binding);
|
|
1751
|
+
}
|
|
1752
|
+
declarations.push({ declaration, binding: isBound ? binding : undefined });
|
|
1753
|
+
},
|
|
1754
|
+
})),
|
|
1755
|
+
...module.definitions.map((definition) => ({
|
|
1756
|
+
location: definition.location,
|
|
1757
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1758
|
+
const inner = createUnit(definition.location, definition, errors, warnings, true);
|
|
1759
|
+
if (isTheoremDefinition(module, definition)) {
|
|
1760
|
+
resolveExpression(definition.body, undefined, inner);
|
|
1761
|
+
} else if (resolveDefinition(definition, undefined, inner, recursive)) {
|
|
1762
|
+
bindings.set(definition.name, {
|
|
1763
|
+
kind: "Definition",
|
|
1764
|
+
arity: getArity(definition),
|
|
1765
|
+
range: definition.location.range,
|
|
1766
|
+
position: definition.location,
|
|
1767
|
+
});
|
|
1768
|
+
}
|
|
1769
|
+
},
|
|
1770
|
+
})),
|
|
1771
|
+
...module.instances.map((instance) => ({
|
|
1772
|
+
location: instance.location,
|
|
1773
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1774
|
+
const inner = createUnit(instance.location, undefined, errors, warnings, false);
|
|
1775
|
+
bindParams(undefined, instance.params, inner);
|
|
1776
|
+
unit.instanced
|
|
1777
|
+
.get(instance)
|
|
1778
|
+
?.forEach((symbol, name) => bindModuleName(name, symbol, inner));
|
|
1779
|
+
if (instance.name !== undefined) {
|
|
1780
|
+
bindModuleName(
|
|
1781
|
+
instance.name,
|
|
1782
|
+
{ kind: "Instance", arity: instance.params.length, range: instance.location.range },
|
|
1783
|
+
inner,
|
|
1784
|
+
);
|
|
1785
|
+
}
|
|
1786
|
+
},
|
|
1787
|
+
})),
|
|
1788
|
+
...module.assumptions.map((assumption) => ({
|
|
1789
|
+
location: assumption.location,
|
|
1790
|
+
check: (errors: SanyError[], warnings: SanyError[]) => {
|
|
1791
|
+
const inner = createUnit(
|
|
1792
|
+
assumption.location,
|
|
1793
|
+
undefined,
|
|
1794
|
+
errors,
|
|
1795
|
+
warnings,
|
|
1796
|
+
assumption.name !== undefined,
|
|
1797
|
+
);
|
|
1798
|
+
resolveExpression(assumption.expression, undefined, inner);
|
|
1799
|
+
if (assumption.name !== undefined) {
|
|
1800
|
+
bindModuleName(
|
|
1801
|
+
assumption.name,
|
|
1802
|
+
{ kind: "Theorem", arity: 0, range: assumption.location.range },
|
|
1803
|
+
inner,
|
|
1804
|
+
);
|
|
1805
|
+
}
|
|
1806
|
+
},
|
|
1807
|
+
})),
|
|
1808
|
+
...module.theorems.map((theorem) => ({
|
|
1809
|
+
location: theorem.location,
|
|
1810
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1811
|
+
resolveTheorem(
|
|
1812
|
+
theorem,
|
|
1813
|
+
createUnit(theorem.location, undefined, errors, warnings, theorem.name !== undefined),
|
|
1814
|
+
),
|
|
1815
|
+
})),
|
|
1816
|
+
...module.uses.map((use) => ({
|
|
1817
|
+
location: use.location,
|
|
1818
|
+
check: (errors: SanyError[], warnings: SanyError[]) =>
|
|
1819
|
+
checkFacts(
|
|
1820
|
+
use.facts,
|
|
1821
|
+
undefined,
|
|
1822
|
+
createUnit(use.location, undefined, errors, warnings, false),
|
|
1823
|
+
),
|
|
1824
|
+
})),
|
|
1825
|
+
{
|
|
1826
|
+
location: module.location.range?.end ?? module.location,
|
|
1827
|
+
check: (errors: SanyError[]) =>
|
|
1828
|
+
declarations.forEach((item) => {
|
|
1829
|
+
const range = item.declaration.location.range;
|
|
1830
|
+
if (range && (!item.binding || item.binding.isPending)) {
|
|
1831
|
+
errors.push({
|
|
1832
|
+
location: formatRange(range),
|
|
1833
|
+
message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
|
|
1834
|
+
});
|
|
1835
|
+
}
|
|
1836
|
+
}),
|
|
1837
|
+
},
|
|
1838
|
+
],
|
|
1839
|
+
unit.warnings,
|
|
792
1840
|
);
|
|
793
1841
|
};
|