tla-ts 0.0.9 → 0.0.10-dev.09e53c2a6
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 +30 -1
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +41 -37
- package/dist/deciders.js +57 -53
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +109 -10
- package/dist/index.d.ts +109 -10
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +2746 -410
- package/dist/prove.js +2745 -409
- package/dist/run.cjs +146 -31
- package/dist/run.js +146 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +132 -104
- package/dist/transfer.js +132 -104
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +3 -1
- package/src/ast.ts +45 -59
- package/src/bags.ts +99 -102
- package/src/builtins.ts +28 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check-failure.ts +116 -0
- package/src/check-trace.ts +282 -0
- package/src/check.ts +797 -782
- package/src/cli/options.ts +12 -32
- package/src/cli/run.ts +0 -2
- package/src/cli/runtime-invariants.ts +136 -0
- package/src/cli/shared.ts +19 -12
- package/src/cli/tlapm.ts +433 -82
- package/src/cli/tlc-arguments.ts +652 -0
- package/src/cli/tlc-output.ts +687 -0
- package/src/cli/tlc-usage.ts +360 -0
- package/src/cli/tlc.ts +323 -658
- package/src/cli/typescript.ts +23 -0
- package/src/cli/xml-exporter.ts +27 -49
- package/src/cli.ts +0 -1
- package/src/codegen.ts +196 -74
- package/src/config.ts +140 -109
- package/src/coverage.ts +830 -0
- package/src/dependencies.ts +36 -54
- package/src/dfid.ts +618 -0
- package/src/environment-config.ts +315 -0
- package/src/environment.ts +178 -339
- package/src/errors.ts +100 -57
- package/src/evaluate.ts +598 -340
- package/src/exit-codes.ts +38 -3
- package/src/fingerprints.ts +75 -11
- package/src/index.ts +3 -0
- package/src/instances.ts +126 -21
- package/src/java-hash.ts +7 -0
- package/src/java-overrides.ts +4 -19
- package/src/json-reader.ts +455 -0
- package/src/json.ts +252 -139
- package/src/levels.ts +46 -58
- package/src/lexer.ts +83 -101
- package/src/liveness-checker.ts +590 -311
- package/src/liveness.ts +180 -124
- package/src/obligations/action.ts +17 -44
- package/src/obligations/anonymize.ts +120 -95
- package/src/obligations/enabled-axioms.ts +218 -0
- package/src/obligations/equality.ts +1 -3
- package/src/obligations/expression.ts +37 -26
- package/src/obligations/format.ts +0 -10
- package/src/obligations/generate.ts +66 -64
- package/src/obligations/index.ts +38 -19
- package/src/obligations/levels.ts +27 -18
- package/src/obligations/module.ts +28 -55
- package/src/obligations/normalize.ts +30 -37
- package/src/obligations/operators.ts +0 -3
- package/src/obligations/pragma.ts +52 -24
- package/src/obligations/printer.ts +89 -58
- package/src/obligations/proof.ts +3 -6
- package/src/obligations/regroup.ts +0 -5
- package/src/obligations/simplify.ts +57 -63
- package/src/obligations/source.ts +119 -95
- package/src/obligations/subexpression.ts +49 -28
- package/src/obligations/substitution.ts +1 -5
- package/src/obligations/temporal.ts +12 -20
- package/src/obligations/tuples.ts +2 -10
- package/src/parallel-worker.ts +30 -60
- package/src/parallel.ts +59 -73
- package/src/parser.ts +1082 -399
- package/src/pcal/ast.ts +53 -86
- package/src/pcal/parse.ts +144 -219
- package/src/pcal/tokenize.ts +91 -131
- package/src/pcal/translate.ts +83 -130
- package/src/pcal/validate.ts +48 -68
- package/src/pretty-print.ts +7 -8
- package/src/prover/decide-pool.ts +8 -13
- package/src/prover/deciders.ts +12 -13
- package/src/prover/first-order.ts +0 -11
- package/src/prover/isabelle/decide.ts +3 -31
- package/src/prover/isabelle/encode.ts +82 -64
- package/src/prover/isabelle/isar/attrib.ts +250 -0
- package/src/prover/isabelle/isar/context.ts +335 -0
- package/src/prover/isabelle/isar/induct.ts +382 -0
- package/src/prover/isabelle/isar/inner-syntax.ts +262 -117
- package/src/prover/isabelle/isar/method.ts +510 -226
- package/src/prover/isabelle/isar/proof.ts +570 -23
- package/src/prover/isabelle/isar/rule-cases.ts +224 -0
- package/src/prover/isabelle/isar/theory.ts +488 -88
- package/src/prover/isabelle/isar/toplevel.ts +331 -0
- package/src/prover/isabelle/isar/type-check.ts +99 -38
- package/src/prover/isabelle/provers/blast/blast.ts +16 -105
- package/src/prover/isabelle/provers/blast/branch.ts +0 -33
- package/src/prover/isabelle/provers/blast/prove.ts +0 -36
- package/src/prover/isabelle/provers/blast/rules.ts +5 -45
- package/src/prover/isabelle/provers/blast/state.ts +1 -29
- package/src/prover/isabelle/provers/blast/term.ts +8 -37
- package/src/prover/isabelle/provers/clasimp.ts +0 -14
- package/src/prover/isabelle/provers/classical.ts +58 -61
- package/src/prover/isabelle/provers/hypsubst.ts +27 -52
- package/src/prover/isabelle/pure/bicompose.ts +24 -39
- package/src/prover/isabelle/pure/consts.ts +0 -8
- package/src/prover/isabelle/pure/conv.ts +2 -18
- package/src/prover/isabelle/pure/drule.ts +95 -45
- package/src/prover/isabelle/pure/envir.ts +2 -53
- package/src/prover/isabelle/pure/goal.ts +1 -9
- package/src/prover/isabelle/pure/interrupt.ts +0 -5
- package/src/prover/isabelle/pure/list.ts +0 -3
- package/src/prover/isabelle/pure/logic.ts +1 -16
- package/src/prover/isabelle/pure/name.ts +1 -10
- package/src/prover/isabelle/pure/net.ts +2 -60
- package/src/prover/isabelle/pure/object-logic.ts +15 -16
- package/src/prover/isabelle/pure/pattern.ts +1 -20
- package/src/prover/isabelle/pure/raw-simplifier.ts +16 -63
- package/src/prover/isabelle/pure/rewriter.ts +31 -74
- package/src/prover/isabelle/pure/search.ts +2 -11
- package/src/prover/isabelle/pure/seq.ts +0 -11
- package/src/prover/isabelle/pure/simplifier.ts +27 -16
- package/src/prover/isabelle/pure/table.ts +2 -33
- package/src/prover/isabelle/pure/tactic.ts +11 -23
- package/src/prover/isabelle/pure/tactical.ts +47 -48
- package/src/prover/isabelle/pure/term-ord.ts +1 -7
- package/src/prover/isabelle/pure/term.ts +1 -44
- package/src/prover/isabelle/pure/thm.ts +26 -48
- package/src/prover/isabelle/pure/type.ts +1 -16
- package/src/prover/isabelle/pure/unify.ts +19 -31
- package/src/prover/isabelle/pure/variable.ts +2 -8
- package/src/prover/isabelle/tla/classical-setup.ts +17 -25
- package/src/prover/isabelle/tla/context.ts +65 -26
- package/src/prover/isabelle/tla/logic-data.ts +1 -1
- package/src/prover/isabelle/tla/logic.ts +53 -32
- package/src/prover/isabelle/tla/simpdata.ts +45 -44
- package/src/prover/isabelle/tools/intuitionistic.ts +28 -21
- package/src/prover/ocaml.ts +4 -7
- package/src/prover/prep/action.ts +33 -66
- package/src/prover/prep/enabled-axioms.ts +22 -20
- package/src/prover/prep/fingerprint.ts +2 -21
- package/src/prover/prep/level-comparison.ts +8 -12
- package/src/prover/prep/md5.ts +0 -2
- package/src/prover/prep/methods.ts +189 -86
- package/src/prover/prep/normalize.ts +42 -37
- package/src/prover/prove-worker.ts +0 -2
- package/src/prover/prove.ts +398 -133
- package/src/prover/ptl/decide.ts +96 -56
- package/src/prover/ptl/encode.ts +15 -38
- package/src/prover/ptl/sat.ts +24 -32
- package/src/prover/ptl/tableau.ts +40 -28
- package/src/prover/smt/arithmetic.ts +310 -0
- package/src/prover/smt/axioms.ts +12 -15
- package/src/prover/smt/context.ts +60 -32
- package/src/prover/smt/data.ts +517 -401
- package/src/prover/smt/encode.ts +155 -85
- package/src/prover/smt/extensionality.ts +5 -13
- package/src/prover/smt/flatten.ts +7 -28
- package/src/prover/smt/format.ts +33 -14
- package/src/prover/smt/instances.ts +7 -11
- package/src/prover/smt/layout.ts +91 -0
- package/src/prover/smt/rewrite.ts +1026 -658
- package/src/prover/smt/simplify.ts +441 -0
- package/src/prover/smt/solver/ackermannize-bv-tactic.ts +61 -0
- package/src/prover/smt/solver/ackr-helper.ts +179 -0
- package/src/prover/smt/solver/ackr-model-converter.ts +105 -0
- package/src/prover/smt/solver/act-cache.ts +0 -16
- package/src/prover/smt/solver/add-bounds-tactic.ts +59 -0
- package/src/prover/smt/solver/aig-tactic.ts +52 -0
- package/src/prover/smt/solver/aig.ts +889 -0
- package/src/prover/smt/solver/arith-rewriter.ts +615 -53
- package/src/prover/smt/solver/arith-terms.ts +121 -0
- package/src/prover/smt/solver/array-decl-plugin.ts +175 -0
- package/src/prover/smt/solver/array-rewriter.ts +309 -0
- package/src/prover/smt/solver/array.ts +1 -3
- package/src/prover/smt/solver/asserted-formulas.ts +87 -58
- package/src/prover/smt/solver/ast-lt.ts +166 -22
- package/src/prover/smt/solver/ast-util.ts +160 -7
- package/src/prover/smt/solver/ast.ts +1424 -180
- package/src/prover/smt/solver/auf-solver.ts +7 -44
- package/src/prover/smt/solver/bit-blast-tactic.ts +582 -0
- package/src/prover/smt/solver/bit-blaster.ts +871 -0
- package/src/prover/smt/solver/bound-manager.ts +196 -0
- package/src/prover/smt/solver/bound-simplifier.ts +600 -0
- package/src/prover/smt/solver/bv-bound-chk.ts +492 -0
- package/src/prover/smt/solver/bv-decl-plugin.ts +338 -0
- package/src/prover/smt/solver/bv-divrem-bounds.ts +95 -0
- package/src/prover/smt/solver/bv-rewriter.ts +1901 -0
- package/src/prover/smt/solver/bv1-blast.ts +214 -0
- package/src/prover/smt/solver/bv2fpa-converter.ts +174 -0
- package/src/prover/smt/solver/bv2int-rewriter.ts +544 -0
- package/src/prover/smt/solver/bv2real-rewriter.ts +533 -0
- package/src/prover/smt/solver/card2bv.ts +110 -0
- package/src/prover/smt/solver/case-split-queue.ts +0 -10
- package/src/prover/smt/solver/cg-table.ts +39 -42
- package/src/prover/smt/solver/check-logic.ts +330 -0
- package/src/prover/smt/solver/clause.ts +0 -27
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +278 -0
- package/src/prover/smt/solver/conflict-resolution.ts +78 -11
- package/src/prover/smt/solver/context.ts +519 -175
- package/src/prover/smt/solver/ctx-simplify.ts +33 -50
- package/src/prover/smt/solver/ctx-solver-simplify.ts +122 -0
- package/src/prover/smt/solver/datatype-decl-plugin.ts +572 -0
- package/src/prover/smt/solver/datatype-rewriter.ts +82 -0
- package/src/prover/smt/solver/demodulator.ts +786 -0
- package/src/prover/smt/solver/dependent-expr-state.ts +81 -0
- package/src/prover/smt/solver/der.ts +39 -30
- package/src/prover/smt/solver/diff-neq.ts +206 -0
- package/src/prover/smt/solver/distribute-forall.ts +57 -0
- package/src/prover/smt/solver/dom-simplify.ts +511 -0
- package/src/prover/smt/solver/dt2bv-tactic.ts +260 -0
- package/src/prover/smt/solver/dyn-ack.ts +0 -11
- package/src/prover/smt/solver/e-matching.ts +9 -41
- package/src/prover/smt/solver/elim-small-bv-tactic.ts +129 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +545 -0
- package/src/prover/smt/solver/enode.ts +100 -23
- package/src/prover/smt/solver/eq2bv-tactic.ts +218 -0
- package/src/prover/smt/solver/euf/arith-axioms.ts +403 -0
- package/src/prover/smt/solver/euf/arith-internalize.ts +653 -0
- package/src/prover/smt/solver/euf/arith-solver.ts +889 -0
- package/src/prover/smt/solver/euf/array-axioms.ts +527 -0
- package/src/prover/smt/solver/euf/array-internalize.ts +428 -0
- package/src/prover/smt/solver/euf/array-solver.ts +306 -0
- package/src/prover/smt/solver/euf/bv-ackerman.ts +162 -0
- package/src/prover/smt/solver/euf/bv-internalize.ts +748 -0
- package/src/prover/smt/solver/euf/bv-solver.ts +449 -0
- package/src/prover/smt/solver/euf/dt-solver.ts +618 -0
- package/src/prover/smt/solver/euf/egraph.ts +907 -0
- package/src/prover/smt/solver/euf/euf-solver.ts +1205 -0
- package/src/prover/smt/solver/euf/fpa-solver.ts +317 -0
- package/src/prover/smt/solver/euf/mam.ts +1050 -0
- package/src/prover/smt/solver/euf/q-ematch.ts +830 -0
- package/src/prover/smt/solver/euf/q-mbi.ts +587 -0
- package/src/prover/smt/solver/euf/q-model-fixer.ts +296 -0
- package/src/prover/smt/solver/euf/q-solver.ts +361 -0
- package/src/prover/smt/solver/euf/th-solver.ts +335 -0
- package/src/prover/smt/solver/euf/th-solvers.ts +30 -0
- package/src/prover/smt/solver/expr-ref.ts +0 -5
- package/src/prover/smt/solver/expr-safe-replace.ts +26 -7
- package/src/prover/smt/solver/expr2polynomial.ts +146 -0
- package/src/prover/smt/solver/factor.ts +141 -0
- package/src/prover/smt/solver/fd-solver.ts +265 -0
- package/src/prover/smt/solver/fingerprints.ts +15 -26
- package/src/prover/smt/solver/fix-dl-var.ts +148 -0
- package/src/prover/smt/solver/flat-literals.ts +0 -9
- package/src/prover/smt/solver/fm-tactic.ts +884 -0
- package/src/prover/smt/solver/fpa-decl-plugin.ts +618 -0
- package/src/prover/smt/solver/fpa-rewriter.ts +640 -0
- package/src/prover/smt/solver/fpa2bv-converter.ts +2249 -0
- package/src/prover/smt/solver/fpa2bv-rewriter.ts +348 -0
- package/src/prover/smt/solver/fpa2bv-tactic.ts +73 -0
- package/src/prover/smt/solver/goal.ts +176 -28
- package/src/prover/smt/solver/goal2nlsat.ts +189 -0
- package/src/prover/smt/solver/guard-set.ts +219 -0
- package/src/prover/smt/solver/hash.ts +0 -9
- package/src/prover/smt/solver/hashtable.ts +26 -23
- package/src/prover/smt/solver/inf-eps.ts +63 -0
- package/src/prover/smt/solver/inj-axiom.ts +101 -0
- package/src/prover/smt/solver/internalizer.ts +112 -132
- package/src/prover/smt/solver/lackr.ts +381 -0
- package/src/prover/smt/solver/lbool.ts +0 -2
- package/src/prover/smt/solver/lia2card.ts +268 -0
- package/src/prover/smt/solver/lia2pb.ts +154 -0
- package/src/prover/smt/solver/literal.ts +0 -4
- package/src/prover/smt/solver/logic-tactics.ts +345 -0
- package/src/prover/smt/solver/lp/bound-propagator.ts +7 -26
- package/src/prover/smt/solver/lp/constraints.ts +1 -8
- package/src/prover/smt/solver/lp/containers.ts +11 -30
- package/src/prover/smt/solver/lp/core-solver.ts +23 -29
- package/src/prover/smt/solver/lp/cross-nested.ts +5 -20
- package/src/prover/smt/solver/lp/dep-intervals.ts +6 -24
- package/src/prover/smt/solver/lp/dependency.ts +0 -5
- package/src/prover/smt/solver/lp/dioph-eq.ts +8 -103
- package/src/prover/smt/solver/lp/explanation.ts +0 -6
- package/src/prover/smt/solver/lp/gomory.ts +8 -22
- package/src/prover/smt/solver/lp/hnf-cutter.ts +16 -23
- package/src/prover/smt/solver/lp/hnf.ts +10 -28
- package/src/prover/smt/solver/lp/int-cube.ts +17 -39
- package/src/prover/smt/solver/lp/int-solver.ts +3 -35
- package/src/prover/smt/solver/lp/lar-solver.ts +69 -82
- package/src/prover/smt/solver/lp/lar-term.ts +0 -5
- package/src/prover/smt/solver/lp/lp-settings.ts +0 -14
- package/src/prover/smt/solver/lp/maximize-term.ts +76 -52
- package/src/prover/smt/solver/lp/nex-creator.ts +4 -28
- package/src/prover/smt/solver/lp/nex.ts +0 -6
- package/src/prover/smt/solver/lp/nla-backoff.ts +0 -5
- package/src/prover/smt/solver/lp/nla-basics.ts +48 -30
- package/src/prover/smt/solver/lp/nla-core.ts +91 -135
- package/src/prover/smt/solver/lp/nla-divisions.ts +31 -43
- package/src/prover/smt/solver/lp/nla-emonics.ts +3 -21
- package/src/prover/smt/solver/lp/nla-factorization.ts +0 -4
- package/src/prover/smt/solver/lp/nla-grobner.ts +22 -72
- package/src/prover/smt/solver/lp/nla-horner.ts +2 -12
- package/src/prover/smt/solver/lp/nla-intervals.ts +1 -12
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +15 -42
- package/src/prover/smt/solver/lp/nla-monotone.ts +1 -6
- package/src/prover/smt/solver/lp/nla-order.ts +0 -13
- package/src/prover/smt/solver/lp/nla-patcher.ts +3 -7
- package/src/prover/smt/solver/lp/nla-powers.ts +220 -0
- package/src/prover/smt/solver/lp/nla-tangents.ts +0 -10
- package/src/prover/smt/solver/lp/nla-throttle.ts +0 -6
- package/src/prover/smt/solver/lp/nla-transcendentals.ts +324 -0
- package/src/prover/smt/solver/lp/nla-types.ts +0 -12
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -26
- package/src/prover/smt/solver/lp/nra-solver.ts +123 -112
- package/src/prover/smt/solver/lp/numeric-pair.ts +0 -4
- package/src/prover/smt/solver/lp/pdd-interval.ts +0 -3
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +1 -14
- package/src/prover/smt/solver/lp/pdd-solver.ts +2 -33
- package/src/prover/smt/solver/lp/pdd.ts +7 -70
- package/src/prover/smt/solver/lp/random-updater.ts +0 -9
- package/src/prover/smt/solver/lp/static-matrix.ts +8 -19
- package/src/prover/smt/solver/lp/union-find.ts +5 -5
- package/src/prover/smt/solver/macro-finder.ts +530 -0
- package/src/prover/smt/solver/mam-compiler.ts +129 -139
- package/src/prover/smt/solver/mam-interpreter.ts +0 -21
- package/src/prover/smt/solver/max-bv-sharing-tactic.ts +17 -0
- package/src/prover/smt/solver/max-cliques.ts +105 -0
- package/src/prover/smt/solver/maxcore.ts +309 -0
- package/src/prover/smt/solver/maximize-ac-sharing.ts +8 -21
- package/src/prover/smt/solver/maxlex.ts +115 -0
- package/src/prover/smt/solver/maxsmt-base.ts +105 -0
- package/src/prover/smt/solver/maxsmt.ts +92 -0
- package/src/prover/smt/solver/mbp/mbp-arith.ts +467 -0
- package/src/prover/smt/solver/mbp/mbp-basic-tg.ts +101 -0
- package/src/prover/smt/solver/mbp/mbp-plugin.ts +287 -0
- package/src/prover/smt/solver/mbp/mbp-qel.ts +48 -0
- package/src/prover/smt/solver/mbp/mbp-solve-plugin.ts +271 -0
- package/src/prover/smt/solver/mbp/mbp-term-graph.ts +606 -0
- package/src/prover/smt/solver/model-based-opt.ts +844 -0
- package/src/prover/smt/solver/model-checker.ts +19 -62
- package/src/prover/smt/solver/model-evaluator.ts +190 -40
- package/src/prover/smt/solver/model-finder.ts +14 -32
- package/src/prover/smt/solver/model-generator.ts +56 -40
- package/src/prover/smt/solver/model-macro-solver.ts +60 -114
- package/src/prover/smt/solver/model-printer.ts +214 -0
- package/src/prover/smt/solver/mpf.ts +711 -0
- package/src/prover/smt/solver/mus.ts +51 -0
- package/src/prover/smt/solver/muz/dl-context.ts +404 -0
- package/src/prover/smt/solver/muz/dl-mk-bit-blast.ts +97 -0
- package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +167 -0
- package/src/prover/smt/solver/muz/dl-mk-elim-term-ite.ts +125 -0
- package/src/prover/smt/solver/muz/dl-mk-interp-tail-simplifier.ts +299 -0
- package/src/prover/smt/solver/muz/dl-mk-rule-inliner.ts +455 -0
- package/src/prover/smt/solver/muz/dl-mk-slice.ts +340 -0
- package/src/prover/smt/solver/muz/dl-mk-subsumption-checker.ts +163 -0
- package/src/prover/smt/solver/muz/dl-rule-set.ts +273 -0
- package/src/prover/smt/solver/muz/dl-rule-transformer.ts +123 -0
- package/src/prover/smt/solver/muz/dl-rule.ts +496 -0
- package/src/prover/smt/solver/muz/hnf.ts +201 -0
- package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +176 -0
- package/src/prover/smt/solver/muz/horn-tactic.ts +201 -0
- package/src/prover/smt/solver/muz/rule-properties.ts +337 -0
- package/src/prover/smt/solver/muz/spacer-context.ts +1406 -0
- package/src/prover/smt/solver/muz/spacer-dl-interface.ts +52 -0
- package/src/prover/smt/solver/muz/spacer-generalizers.ts +241 -0
- package/src/prover/smt/solver/muz/spacer-manager.ts +127 -0
- package/src/prover/smt/solver/muz/spacer-pdr.ts +218 -0
- package/src/prover/smt/solver/muz/spacer-prop-solver.ts +333 -0
- package/src/prover/smt/solver/muz/spacer-util.ts +282 -0
- package/src/prover/smt/solver/muz/substitution.ts +203 -0
- package/src/prover/smt/solver/nla2bv-tactic.ts +285 -0
- package/src/prover/smt/solver/nlqsat.ts +484 -0
- package/src/prover/smt/solver/nlsat/algebraic.ts +177 -160
- package/src/prover/smt/solver/nlsat/atoms.ts +24 -24
- package/src/prover/smt/solver/nlsat/evaluator.ts +16 -22
- package/src/prover/smt/solver/nlsat/explain.ts +65 -69
- package/src/prover/smt/solver/nlsat/interval-set.ts +9 -51
- package/src/prover/smt/solver/nlsat/levelwise.ts +48 -135
- package/src/prover/smt/solver/nlsat/mpbq.ts +47 -14
- package/src/prover/smt/solver/nlsat/polynomial.ts +29 -161
- package/src/prover/smt/solver/nlsat/solver.ts +241 -110
- package/src/prover/smt/solver/nlsat/transcendental-eval.ts +310 -0
- package/src/prover/smt/solver/nlsat/transcendentals.ts +367 -0
- package/src/prover/smt/solver/nlsat/types.ts +0 -9
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -47
- package/src/prover/smt/solver/nlsat/upolynomial.ts +10 -93
- package/src/prover/smt/solver/nlsat-tactic.ts +82 -0
- package/src/prover/smt/solver/nnf.ts +107 -60
- package/src/prover/smt/solver/normalize-bounds.ts +53 -0
- package/src/prover/smt/solver/occf.ts +97 -0
- package/src/prover/smt/solver/opt-context.ts +787 -0
- package/src/prover/smt/solver/opt-nlsat.ts +375 -0
- package/src/prover/smt/solver/opt-pareto.ts +282 -0
- package/src/prover/smt/solver/opt-preprocess.ts +101 -0
- package/src/prover/smt/solver/opt-solver.ts +308 -0
- package/src/prover/smt/solver/opt-value.ts +55 -0
- package/src/prover/smt/solver/optsmt.ts +692 -0
- package/src/prover/smt/solver/parallel-tactic.ts +299 -0
- package/src/prover/smt/solver/pattern-inference.ts +21 -26
- package/src/prover/smt/solver/pb-rewriter.ts +284 -0
- package/src/prover/smt/solver/pb2bv-tactic.ts +540 -0
- package/src/prover/smt/solver/poly-rewriter.ts +204 -79
- package/src/prover/smt/solver/probes.ts +512 -0
- package/src/prover/smt/solver/propagate-values.ts +37 -30
- package/src/prover/smt/solver/proto-model.ts +650 -66
- package/src/prover/smt/solver/pull-quant.ts +28 -23
- package/src/prover/smt/solver/purify-arith.ts +632 -0
- package/src/prover/smt/solver/qe-arith-plugin.ts +1098 -0
- package/src/prover/smt/solver/qe-lite-der.ts +2 -31
- package/src/prover/smt/solver/qe-lite-fm.ts +4 -28
- package/src/prover/smt/solver/qe-lite.ts +39 -49
- package/src/prover/smt/solver/qe-mbp.ts +270 -0
- package/src/prover/smt/solver/qe.ts +1498 -0
- package/src/prover/smt/solver/qel.ts +25 -0
- package/src/prover/smt/solver/qi-queue.ts +0 -16
- package/src/prover/smt/solver/qsat.ts +814 -0
- package/src/prover/smt/solver/quant-hoist.ts +196 -0
- package/src/prover/smt/solver/quant-tactics.ts +17 -74
- package/src/prover/smt/solver/quantifier-analyzer.ts +26 -47
- package/src/prover/smt/solver/quantifier-manager.ts +8 -23
- package/src/prover/smt/solver/quasi-macros.ts +243 -0
- package/src/prover/smt/solver/random-gen.ts +0 -2
- package/src/prover/smt/solver/rational.ts +45 -8
- package/src/prover/smt/solver/recfun-decl-plugin.ts +261 -0
- package/src/prover/smt/solver/recfun-rewriter.ts +107 -0
- package/src/prover/smt/solver/reduce-args.ts +225 -0
- package/src/prover/smt/solver/reduce-bv-size.ts +119 -0
- package/src/prover/smt/solver/ref-vector.ts +0 -6
- package/src/prover/smt/solver/relevancy.ts +2 -16
- package/src/prover/smt/solver/rewriter-tpl.ts +52 -129
- package/src/prover/smt/solver/rewriter.ts +477 -120
- package/src/prover/smt/solver/rlimit.ts +39 -21
- package/src/prover/smt/solver/sat/asymm-branch.ts +325 -0
- package/src/prover/smt/solver/sat/big.ts +177 -0
- package/src/prover/smt/solver/sat/clause-use-list.ts +71 -0
- package/src/prover/smt/solver/sat/clause.ts +203 -0
- package/src/prover/smt/solver/sat/cleaner.ts +94 -0
- package/src/prover/smt/solver/sat/config.ts +122 -0
- package/src/prover/smt/solver/sat/consequences.ts +203 -0
- package/src/prover/smt/solver/sat/elim-eqs.ts +142 -0
- package/src/prover/smt/solver/sat/goal2sat.ts +526 -0
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +378 -0
- package/src/prover/smt/solver/sat/model-converter.ts +136 -0
- package/src/prover/smt/solver/sat/pb-constraint.ts +261 -0
- package/src/prover/smt/solver/sat/pb-solver.ts +1577 -0
- package/src/prover/smt/solver/sat/probing.ts +170 -0
- package/src/prover/smt/solver/sat/sat-tactic.ts +43 -0
- package/src/prover/smt/solver/sat/sat2goal.ts +165 -0
- package/src/prover/smt/solver/sat/scc.ts +142 -0
- package/src/prover/smt/solver/sat/simplifier.ts +1618 -0
- package/src/prover/smt/solver/sat/solver.ts +2127 -0
- package/src/prover/smt/solver/sat/var-queue.ts +117 -0
- package/src/prover/smt/solver/sat/watch-list.ts +140 -0
- package/src/prover/smt/solver/search-tree.ts +284 -0
- package/src/prover/smt/solver/seq-axioms.ts +1010 -0
- package/src/prover/smt/solver/seq-decl-plugin.ts +819 -0
- package/src/prover/smt/solver/seq-derive.ts +1301 -0
- package/src/prover/smt/solver/seq-eq-approx.ts +236 -0
- package/src/prover/smt/solver/seq-eq-solver.ts +439 -0
- package/src/prover/smt/solver/seq-factory.ts +89 -0
- package/src/prover/smt/solver/seq-monadic.ts +728 -0
- package/src/prover/smt/solver/seq-range-predicate.ts +235 -0
- package/src/prover/smt/solver/seq-re-rewriter.ts +1170 -0
- package/src/prover/smt/solver/seq-re-util.ts +758 -0
- package/src/prover/smt/solver/seq-regex-bisim.ts +144 -0
- package/src/prover/smt/solver/seq-regex-live.ts +200 -0
- package/src/prover/smt/solver/seq-rewriter.ts +2402 -0
- package/src/prover/smt/solver/seq-skolem.ts +317 -0
- package/src/prover/smt/solver/seq-subset.ts +165 -0
- package/src/prover/smt/solver/seq-view-witness.ts +353 -0
- package/src/prover/smt/solver/smt-consequences.ts +297 -0
- package/src/prover/smt/solver/smt-kernel.ts +83 -0
- package/src/prover/smt/solver/smt-logics.ts +66 -0
- package/src/prover/smt/solver/smt-setup.ts +496 -0
- package/src/prover/smt/solver/smt2-printer.ts +765 -0
- package/src/prover/smt/solver/smtlib-parser.ts +3140 -365
- package/src/prover/smt/solver/smtlib-script.ts +1631 -0
- package/src/prover/smt/solver/solve-eqs.ts +929 -0
- package/src/prover/smt/solver/solver.ts +119 -39
- package/src/prover/smt/solver/sort.ts +1 -5
- package/src/prover/smt/solver/static-features.ts +314 -0
- package/src/prover/smt/solver/sticky-generations.ts +0 -6
- package/src/prover/smt/solver/tactic.ts +1986 -0
- package/src/prover/smt/solver/term-enumeration.ts +205 -0
- package/src/prover/smt/solver/theory-array.ts +1240 -0
- package/src/prover/smt/solver/theory-bv.ts +1200 -0
- package/src/prover/smt/solver/theory-char.ts +355 -0
- package/src/prover/smt/solver/theory-datatype.ts +598 -0
- package/src/prover/smt/solver/theory-finite-set.ts +30 -0
- package/src/prover/smt/solver/theory-fpa.ts +389 -0
- package/src/prover/smt/solver/theory-lra-axioms.ts +55 -25
- package/src/prover/smt/solver/theory-lra-bounds.ts +31 -11
- package/src/prover/smt/solver/theory-lra-final-check.ts +59 -91
- package/src/prover/smt/solver/theory-lra-internalize.ts +94 -53
- package/src/prover/smt/solver/theory-lra-model.ts +23 -53
- package/src/prover/smt/solver/theory-lra-propagate.ts +24 -40
- package/src/prover/smt/solver/theory-lra-state.ts +6 -27
- package/src/prover/smt/solver/theory-lra.ts +188 -8
- package/src/prover/smt/solver/theory-pb.ts +934 -0
- package/src/prover/smt/solver/theory-polymorphism.ts +58 -0
- package/src/prover/smt/solver/theory-recfun.ts +307 -0
- package/src/prover/smt/solver/theory-seq-regex.ts +867 -0
- package/src/prover/smt/solver/theory-seq-solve.ts +1373 -0
- package/src/prover/smt/solver/theory-seq-state.ts +871 -0
- package/src/prover/smt/solver/theory-seq.ts +961 -0
- package/src/prover/smt/solver/theory.ts +189 -0
- package/src/prover/smt/solver/tseitin-cnf.ts +485 -0
- package/src/prover/smt/solver/var-subst.ts +37 -39
- package/src/prover/smt/table.ts +3 -10
- package/src/prover/smt/types.ts +146 -0
- package/src/prover/tlapm/cache.ts +67 -41
- package/src/prover/tlapm/ocaml-marshal.ts +19 -21
- package/src/prover/tlapm/tlapm.ts +21 -20
- package/src/prover/tlapm-library.ts +3208 -0
- package/src/prover/verdict.ts +9 -2
- package/src/prover/zenon/check/check.ts +1 -11
- package/src/prover/zenon/check/definitions.ts +5 -15
- package/src/prover/zenon/check/equality.ts +0 -8
- package/src/prover/zenon/check/functions.ts +0 -4
- package/src/prover/zenon/check/keys.ts +0 -7
- package/src/prover/zenon/check/propositional.ts +0 -3
- package/src/prover/zenon/check/records.ts +1 -6
- package/src/prover/zenon/check/relations.ts +2 -14
- package/src/prover/zenon/check/rewrites.ts +2 -21
- package/src/prover/zenon/check/sets.ts +40 -61
- package/src/prover/zenon/check/terms.ts +1 -19
- package/src/prover/zenon/check/tla.ts +0 -6
- package/src/prover/zenon/decide.ts +10 -19
- package/src/prover/zenon/encode.ts +0 -9
- package/src/prover/zenon/eqrel.ts +2 -15
- package/src/prover/zenon/equality-rules.ts +0 -6
- package/src/prover/zenon/expr.ts +8 -28
- package/src/prover/zenon/index-table.ts +26 -59
- package/src/prover/zenon/isar-case.ts +8 -20
- package/src/prover/zenon/isar.ts +35 -65
- package/src/prover/zenon/limits.ts +0 -4
- package/src/prover/zenon/llproof.ts +2 -22
- package/src/prover/zenon/mltoll.ts +66 -106
- package/src/prover/zenon/node.ts +20 -15
- package/src/prover/zenon/ocaml.ts +1 -17
- package/src/prover/zenon/parse.ts +9 -13
- package/src/prover/zenon/phrase.ts +1 -13
- package/src/prover/zenon/proof.ts +0 -11
- package/src/prover/zenon/propositional-rules.ts +0 -7
- package/src/prover/zenon/rules.ts +2 -8
- package/src/prover/zenon/search.ts +11 -35
- package/src/prover/zenon/tla/functions.ts +59 -33
- package/src/prover/zenon/tla/llproof.ts +67 -78
- package/src/prover/zenon/tla/records.ts +57 -35
- package/src/prover/zenon/tla/rewrites.ts +12 -21
- package/src/prover/zenon/tla/sets.ts +88 -59
- package/src/prover/zenon/tla/terms.ts +0 -8
- package/src/prover/zenon/tla/tla.ts +66 -66
- package/src/randomization.ts +81 -35
- package/src/resolve.ts +67 -185
- package/src/runtime.ts +26 -22
- package/src/sany/builtins.ts +2 -5
- package/src/sany/generation.ts +394 -0
- package/src/sany/generator-def-entries.ts +210 -0
- package/src/sany/generator-definitions.ts +686 -0
- package/src/sany/generator-expressions.ts +1100 -0
- package/src/sany/generator-proofs.ts +662 -0
- package/src/sany/generator.ts +191 -2764
- package/src/sany/levels.ts +5 -35
- package/src/sany/lexer.ts +58 -77
- package/src/sany/linter.ts +0 -10
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +5 -43
- package/src/sany/parse-error.ts +39 -117
- package/src/sany/report.ts +32 -69
- package/src/sany/selectors.ts +49 -76
- package/src/sany/source.ts +1 -17
- package/src/sany/xml.ts +35 -117
- package/src/search.ts +671 -169
- package/src/simulate.ts +712 -0
- package/src/spec-description.ts +192 -100
- package/src/standard-definitions.ts +4 -4
- package/src/standard-modules.ts +264 -95
- package/src/state-graph.ts +52 -33
- package/src/subexpressions.ts +69 -52
- package/src/substitution-levels.ts +7 -7
- package/src/successors.ts +267 -100
- package/src/symmetry.ts +35 -27
- package/src/tableau.ts +29 -26
- package/src/tlc/fingerprint.ts +267 -79
- package/src/tlc/jar-data.ts +112 -6
- package/src/tlc/messages.ts +125 -54
- package/src/tlc/order.ts +39 -69
- package/src/tlc/random.ts +37 -49
- package/src/tlc/replay.ts +553 -228
- package/src/tlc/summary.ts +64 -60
- package/src/tlc/tlc-operators.ts +78 -44
- package/src/tlc/tlcext-operators.ts +171 -122
- package/src/tlc/trace-spec.ts +249 -0
- package/src/tlc/value-stream.ts +139 -0
- package/src/tlc/version.ts +1 -4
- package/src/transfer.ts +22 -25
- package/src/typed-tier.ts +6535 -0
- package/src/typescript/actions.ts +479 -0
- package/src/typescript/collections.ts +614 -0
- package/src/typescript/compile.ts +63 -0
- package/src/typescript/context.ts +94 -0
- package/src/typescript/emit.ts +121 -0
- package/src/typescript/expressions.ts +496 -0
- package/src/typescript/formula.ts +154 -0
- package/src/typescript/functions.ts +284 -0
- package/src/typescript/model.ts +162 -0
- package/src/typescript/names.ts +114 -0
- package/src/typescript/program.ts +48 -0
- package/src/typescript/properties.ts +139 -0
- package/src/typescript/scope.ts +112 -0
- package/src/typescript/steps.ts +167 -0
- package/src/typescript/types.ts +171 -0
- package/src/values.ts +832 -484
- package/src/worker.ts +7 -8
- package/src/prover/isabelle/isar/statement.ts +0 -57
- package/src/prover/smt/solver/z3-license.txt +0 -10
- package/src/prover/tlaps-module.ts +0 -510
- package/src/prover/zenon/LICENSE +0 -32
package/src/sany/generator.ts
CHANGED
|
@@ -1,159 +1,71 @@
|
|
|
1
|
-
import { TEMPORAL_LEVEL } from "../levels";
|
|
2
1
|
import type { TlcVersion } from "../tlc/version";
|
|
3
|
-
import type {
|
|
4
|
-
AssumeProve,
|
|
5
|
-
Assumption,
|
|
6
|
-
Fact,
|
|
7
|
-
Bound,
|
|
8
|
-
Declaration,
|
|
9
|
-
Definition,
|
|
10
|
-
Expression,
|
|
11
|
-
FunctionDefinition,
|
|
12
|
-
Instance,
|
|
13
|
-
Module,
|
|
14
|
-
OperatorDefinition,
|
|
15
|
-
NewSymbol,
|
|
16
|
-
Proof,
|
|
17
|
-
ProofStep,
|
|
18
|
-
SourceLocation,
|
|
19
|
-
Theorem,
|
|
20
|
-
UseOrHide,
|
|
21
|
-
} from "../ast";
|
|
22
|
-
import { getChildren } from "../ast";
|
|
23
2
|
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
3
|
+
type AssumeProve,
|
|
4
|
+
type Expression,
|
|
5
|
+
getChildren,
|
|
6
|
+
type Instance,
|
|
7
|
+
type Module,
|
|
8
|
+
type Proof,
|
|
9
|
+
} from "../ast";
|
|
10
|
+
import { TlaMissingModuleError, TlaSanyFatalError, TlaSyntaxError } from "../errors";
|
|
29
11
|
import { parseSanyModule } from "../parser";
|
|
30
|
-
import {
|
|
12
|
+
import { isBefore } from "../resolve";
|
|
13
|
+
import { createBuiltins } from "./builtins";
|
|
31
14
|
import { getSany174LexicalError } from "./lexer";
|
|
32
15
|
import { getSanyParseError } from "./parse-error";
|
|
33
16
|
import {
|
|
34
17
|
addSanyMessage,
|
|
35
|
-
CONSTANT_DECL_KIND,
|
|
36
|
-
NEW_CONSTANT_KIND,
|
|
37
|
-
VARIABLE_DECL_KIND,
|
|
38
18
|
addToContext,
|
|
19
|
+
CONSTANT_DECL_KIND,
|
|
20
|
+
type Context,
|
|
39
21
|
createContext,
|
|
40
|
-
getContextModule,
|
|
41
22
|
formatSanyLocation,
|
|
23
|
+
getContextModule,
|
|
42
24
|
getContextSymbol,
|
|
43
|
-
getSymbolArity,
|
|
44
|
-
getDeclarations,
|
|
45
|
-
getHashtableValues,
|
|
46
|
-
isAbsent,
|
|
47
|
-
type AssumeNode,
|
|
48
|
-
type AssumeProveNode,
|
|
49
|
-
type Context,
|
|
50
|
-
type DefStepNode,
|
|
51
|
-
type ExprNode,
|
|
52
|
-
type ExprOrOpArgNode,
|
|
53
|
-
type FactNode,
|
|
54
|
-
type FormalParamNode,
|
|
55
|
-
type InstanceNode,
|
|
56
|
-
type LabelNode,
|
|
57
|
-
type LetInNode,
|
|
58
|
-
type LeafProofNode,
|
|
59
|
-
type LevelNode,
|
|
60
25
|
type ModuleNode,
|
|
61
|
-
type NewSymbNode,
|
|
62
|
-
type NonLeafProofNode,
|
|
63
|
-
type OpApplNode,
|
|
64
|
-
type OpArgNode,
|
|
65
|
-
type OpDeclNode,
|
|
66
|
-
type OpDefNode,
|
|
67
|
-
type ProofStepNode,
|
|
68
26
|
type SanyLocation,
|
|
69
27
|
type SanyMessage,
|
|
70
|
-
type Subst,
|
|
71
|
-
type SubstInNode,
|
|
72
28
|
type SymbolNode,
|
|
73
|
-
|
|
74
|
-
type ThmOrAssumpDefNode,
|
|
75
|
-
type UseOrHideNode,
|
|
29
|
+
VARIABLE_DECL_KIND,
|
|
76
30
|
} from "./nodes";
|
|
77
|
-
import {
|
|
78
|
-
createSelectorResolver,
|
|
79
|
-
getSelectorItems,
|
|
80
|
-
isGeneralId,
|
|
81
|
-
type SelectorItem,
|
|
82
|
-
} from "./selectors";
|
|
31
|
+
import { createSelectorResolver } from "./selectors";
|
|
83
32
|
import {
|
|
84
33
|
createSourceFile,
|
|
85
|
-
findNameTokens,
|
|
86
|
-
findToken,
|
|
87
|
-
getArgumentSpan,
|
|
88
|
-
getExpressionSpan,
|
|
89
|
-
getMatchingIndex,
|
|
90
|
-
getParenthesizedSpan,
|
|
91
34
|
getPreComments,
|
|
92
|
-
getRangeSpan,
|
|
93
|
-
getSpanLocation,
|
|
94
35
|
getTokenIndex,
|
|
95
|
-
getTokenLocation,
|
|
96
|
-
getTokenSource,
|
|
97
36
|
getUnboxedSource,
|
|
98
|
-
isBoxToken,
|
|
99
|
-
isToken,
|
|
100
37
|
type SourceFile,
|
|
101
|
-
type TokenSpan,
|
|
102
38
|
} from "./source";
|
|
103
|
-
import {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
depth: number;
|
|
134
|
-
isDeclared: boolean[];
|
|
135
|
-
goal: AssumeProveScope | undefined;
|
|
136
|
-
goalClause: number;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
interface LabelGoal {
|
|
140
|
-
scope: AssumeProveScope;
|
|
141
|
-
clause: number;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const NEW_SYMBOL_KINDS = {
|
|
145
|
-
Constant: { declKind: NEW_CONSTANT_KIND, level: 0 },
|
|
146
|
-
Variable: { declKind: 25, level: 1 },
|
|
147
|
-
State: { declKind: 26, level: 1 },
|
|
148
|
-
Action: { declKind: 27, level: 2 },
|
|
149
|
-
Temporal: { declKind: 28, level: 3 },
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
// Whether SANY names an operator by an identifier rather than by a symbol.
|
|
153
|
-
const isIdentifier = (name: string) => /^[A-Za-z0-9_]+$/.test(name);
|
|
154
|
-
|
|
155
|
-
// Whether an operator name selects through an instance, as M!Op does, unlike the infix !!.
|
|
156
|
-
const isSelector = (name: string) => name !== "!!" && name.includes("!");
|
|
39
|
+
import {
|
|
40
|
+
createFormalParam,
|
|
41
|
+
createNullNode,
|
|
42
|
+
createOpAppl,
|
|
43
|
+
createOpArg,
|
|
44
|
+
createOpDef,
|
|
45
|
+
getBuiltin,
|
|
46
|
+
getModule,
|
|
47
|
+
getTopContext,
|
|
48
|
+
getUnitLocation,
|
|
49
|
+
isSameOrigin,
|
|
50
|
+
matchArguments,
|
|
51
|
+
type ModuleGeneration,
|
|
52
|
+
resolveModule,
|
|
53
|
+
resolveSymbol,
|
|
54
|
+
resolveSynonym,
|
|
55
|
+
} from "./generation";
|
|
56
|
+
import {
|
|
57
|
+
checkForUndefinedRecursiveOps,
|
|
58
|
+
checkRecursiveSection,
|
|
59
|
+
createDeclaration,
|
|
60
|
+
createSubstIn,
|
|
61
|
+
generateInstance,
|
|
62
|
+
processFunction,
|
|
63
|
+
processModuleDefinition,
|
|
64
|
+
processOperator,
|
|
65
|
+
processRecursive,
|
|
66
|
+
} from "./generator-definitions";
|
|
67
|
+
import { generateArgument, generateExpression } from "./generator-expressions";
|
|
68
|
+
import { generateUseOrHide, processAssumption, processTheorem } from "./generator-proofs";
|
|
157
69
|
|
|
158
70
|
export interface SanySpecification {
|
|
159
71
|
root: ModuleNode;
|
|
@@ -162,96 +74,29 @@ export interface SanySpecification {
|
|
|
162
74
|
}
|
|
163
75
|
|
|
164
76
|
interface SanyGenerationOptions {
|
|
165
|
-
// The name of the root module's file, which SANY requires to match the module's name.
|
|
166
77
|
rootFileName?: string;
|
|
167
|
-
// The path of the root module's file as SANY's caller gives it, which SANY names when it cannot
|
|
168
|
-
// parse the file, rather than the file name it gives the other modules.
|
|
169
78
|
rootFilePath?: string;
|
|
170
|
-
// Called before SANY parses the file of each module, and before it analyzes each external
|
|
171
|
-
// module, in its order.
|
|
172
79
|
onParse?: (name: string) => void;
|
|
173
80
|
onAnalyze?: (name: string) => void;
|
|
174
|
-
// Called after each external module, in the order SANY analyzes them.
|
|
175
81
|
onModule?: (module: ModuleNode, messages: SanyMessage[]) => void;
|
|
176
|
-
// The TLC whose SANY to reproduce, which decides how SANY numbers the nodes it creates.
|
|
177
82
|
version?: TlcVersion;
|
|
178
|
-
// The modules TLC adds to the root module's EXTENDS for its post conditions, which SANY loads
|
|
179
|
-
// after the ones the root module itself extends.
|
|
180
83
|
rootExtendees?: string[];
|
|
181
84
|
}
|
|
182
85
|
|
|
183
|
-
interface SymbolTable {
|
|
184
|
-
contexts: Context[];
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
interface BoundSymbols {
|
|
188
|
-
bounded: FormalParamNode[][];
|
|
189
|
-
tupleOrs: boolean[];
|
|
190
|
-
ranges: ExprNode[];
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
interface OpApplBinding {
|
|
194
|
-
unbounded?: FormalParamNode[];
|
|
195
|
-
bounded?: FormalParamNode[][];
|
|
196
|
-
tupleOrs?: boolean[];
|
|
197
|
-
ranges?: ExprNode[];
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// A module name that SpecObj.loadSpec has yet to resolve, with the module that imports it and the
|
|
201
|
-
// module of that file whose EXTENDS or INSTANCE names it.
|
|
202
86
|
interface UnresolvedName {
|
|
203
87
|
name: string;
|
|
204
88
|
importer: string;
|
|
205
89
|
namingModule: string;
|
|
206
90
|
}
|
|
207
91
|
|
|
208
|
-
const getTopContext = (table: SymbolTable) => table.contexts[table.contexts.length - 1];
|
|
209
|
-
|
|
210
|
-
const resolveSymbol = (table: SymbolTable, name: string) => {
|
|
211
|
-
for (let index = table.contexts.length - 1; index >= 0; index--) {
|
|
212
|
-
const symbol = getContextSymbol(table.contexts[index], name);
|
|
213
|
-
if (symbol) return symbol;
|
|
214
|
-
}
|
|
215
|
-
return undefined;
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
// SANY's kind of a symbol, which a symbol that takes another's name must share.
|
|
219
|
-
const getSymbolKind = (symbol: SymbolNode) =>
|
|
220
|
-
symbol.kind === "OpDecl"
|
|
221
|
-
? `${symbol.kind}${symbol.declKind}`
|
|
222
|
-
: symbol.kind === "OpDef"
|
|
223
|
-
? symbol.defKind
|
|
224
|
-
: symbol.kind;
|
|
225
|
-
|
|
226
|
-
// Context.kindOfNode.
|
|
227
92
|
const getKindName = (symbol: SymbolNode) =>
|
|
228
93
|
symbol.kind === "OpDef" || symbol.kind === "FormalParam" ? "definition" : "declaration";
|
|
229
94
|
|
|
230
|
-
// SymbolNode.sameOriginallyDefinedInModule: two symbols are the same definition of a module without
|
|
231
|
-
// parameters.
|
|
232
|
-
const isSameOrigin = (symbol: SymbolNode, other: SymbolNode) => {
|
|
233
|
-
if (symbol.kind === "OpDef" && other.kind === "OpDef") {
|
|
234
|
-
const source = symbol.source ?? symbol;
|
|
235
|
-
if (source !== (other.source ?? other)) return false;
|
|
236
|
-
return source.module === undefined || isParameterFree(source.module);
|
|
237
|
-
}
|
|
238
|
-
if (symbol.kind === "ThmOrAssumpDef" && other.kind === "ThmOrAssumpDef") {
|
|
239
|
-
const source = symbol.source ?? symbol;
|
|
240
|
-
return source === (other.source ?? other) && isParameterFree(source.module);
|
|
241
|
-
}
|
|
242
|
-
return false;
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const isParameterFree = (module: ModuleNode) =>
|
|
246
|
-
getDeclarations(module, CONSTANT_DECL_KIND).length === 0 &&
|
|
247
|
-
getDeclarations(module, VARIABLE_DECL_KIND).length === 0;
|
|
248
|
-
|
|
249
95
|
const getLetInstances = (expression: Expression): Instance[] => [
|
|
250
96
|
...(expression.kind === "Let" ? (expression.instances ?? []) : []),
|
|
251
97
|
...getChildren(expression).flatMap(getLetInstances),
|
|
252
98
|
];
|
|
253
99
|
|
|
254
|
-
// The LET instances of a theorem or assumption's statement.
|
|
255
100
|
const getStatementInstances = (statement: Expression | AssumeProve): Instance[] =>
|
|
256
101
|
statement.kind === "AssumeProve"
|
|
257
102
|
? [...statement.assumptions, statement.conclusion].flatMap((part) =>
|
|
@@ -259,43 +104,36 @@ const getStatementInstances = (statement: Expression | AssumeProve): Instance[]
|
|
|
259
104
|
)
|
|
260
105
|
: getLetInstances(statement);
|
|
261
106
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
107
|
+
const getProofInstances = (proof: Proof | undefined): Instance[] =>
|
|
108
|
+
proof?.kind !== "Steps"
|
|
109
|
+
? []
|
|
110
|
+
: proof.steps.flatMap((step) => [
|
|
111
|
+
...(step.body.kind === "Instance" ? [step.body] : []),
|
|
112
|
+
...(step.body.kind === "Define"
|
|
113
|
+
? step.body.definitions.flatMap((definition) =>
|
|
114
|
+
definition.kind === "Instance" ? [definition] : [],
|
|
115
|
+
)
|
|
116
|
+
: []),
|
|
117
|
+
...getProofInstances(step.proof),
|
|
118
|
+
]);
|
|
119
|
+
|
|
120
|
+
const isInternalInstance = (module: Module, instance: Instance) => {
|
|
266
121
|
const seen = new Set<string>();
|
|
267
122
|
for (const unit of module.units) {
|
|
268
123
|
if (unit.kind === "Module") seen.add(unit.module.name);
|
|
269
|
-
if (unit.kind === "Instance" && unit.instance.module ===
|
|
124
|
+
if (unit.kind === "Instance" && unit.instance.module === instance.module) {
|
|
125
|
+
return seen.has(instance.module);
|
|
126
|
+
}
|
|
270
127
|
}
|
|
271
|
-
return
|
|
128
|
+
return module.modules.some(
|
|
129
|
+
(inner) => inner.name === instance.module && isBefore(inner.location, instance.location),
|
|
130
|
+
);
|
|
272
131
|
};
|
|
273
132
|
|
|
274
|
-
type LetUnit =
|
|
275
|
-
| { kind: "Recursive"; declarations: Declaration[] }
|
|
276
|
-
| { kind: "Instance"; instance: Instance }
|
|
277
|
-
| { kind: "Definition"; definition: Definition };
|
|
278
|
-
|
|
279
|
-
// The decimal digits of a numeral such as \hFF, which SANY keeps at any size.
|
|
280
|
-
const getNumeralDigits = (text: string) =>
|
|
281
|
-
BigInt(text.startsWith("\\") ? `0${text.slice(1).replace(/^h/i, "x")}` : text).toString();
|
|
282
|
-
|
|
283
|
-
// An entry of SANY's LS stack: the labels of a definition, theorem or label and the parameters
|
|
284
|
-
// bound within it around the expression being generated.
|
|
285
|
-
interface LabelScope {
|
|
286
|
-
labels: Map<string, LabelNode>;
|
|
287
|
-
params: FormalParamNode[][];
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
// The modules a file extends and instantiates, as SANY's ParseUnit collects them, and the module of
|
|
291
|
-
// the file that names each one. SANY looks at a module's own EXTENDS and INSTANCEs before those of
|
|
292
|
-
// its inner modules.
|
|
293
133
|
const getDependencies = (module: Module) => {
|
|
294
134
|
const extendees: string[] = [];
|
|
295
135
|
const instancees: string[] = [];
|
|
296
136
|
const importers = new Map<string, string>();
|
|
297
|
-
// ParseUnit.calculateContextWithinParseUnit: a module knows the inner modules defined before it in
|
|
298
|
-
// the modules enclosing it and the inner modules of the modules it extends, but not itself.
|
|
299
137
|
const visit = (current: Module, context: Map<string, Module>) => {
|
|
300
138
|
const isRoot = current === module;
|
|
301
139
|
const addImports = (names: string[], imports: string[]) =>
|
|
@@ -308,20 +146,24 @@ const getDependencies = (module: Module) => {
|
|
|
308
146
|
const instances = current.units.flatMap((unit) => {
|
|
309
147
|
if (unit.kind === "Instance") return [unit.instance];
|
|
310
148
|
if (unit.kind === "Definition") return getLetInstances(unit.definition.body);
|
|
311
|
-
if (unit.kind === "Theorem" && isRoot)
|
|
149
|
+
if (unit.kind === "Theorem" && isRoot) {
|
|
150
|
+
return [
|
|
151
|
+
...getStatementInstances(unit.theorem.statement),
|
|
152
|
+
...getProofInstances(unit.theorem.proof),
|
|
153
|
+
];
|
|
154
|
+
}
|
|
312
155
|
if (unit.kind === "Assumption" && isRoot) return getLetInstances(unit.assumption.expression);
|
|
313
156
|
return [];
|
|
314
157
|
});
|
|
315
158
|
addImports(
|
|
316
159
|
instances
|
|
317
|
-
.
|
|
318
|
-
.
|
|
160
|
+
.filter((instance) => isExternal(instance.module) && !isInternalInstance(current, instance))
|
|
161
|
+
.map((instance) => instance.module),
|
|
319
162
|
instancees,
|
|
320
163
|
);
|
|
321
164
|
const peers = new Map(context);
|
|
322
165
|
current.modules.forEach((inner) => {
|
|
323
166
|
const innerContext = new Map(peers);
|
|
324
|
-
// ParseUnit.handleExtensions.
|
|
325
167
|
const extenders = new Set<Module>();
|
|
326
168
|
const addExtendedInnerModules = (extender: Module) => {
|
|
327
169
|
if (extenders.has(extender)) return;
|
|
@@ -346,91 +188,6 @@ const getDependencies = (module: Module) => {
|
|
|
346
188
|
return { extendees, instancees, importers };
|
|
347
189
|
};
|
|
348
190
|
|
|
349
|
-
// The expressions that can name an operator, as Generator.generateExprOrOpArg tells them apart.
|
|
350
|
-
const OPERATOR_ARGUMENT_KINDS = new Set<Expression["kind"]>([
|
|
351
|
-
"Identifier",
|
|
352
|
-
"Subexpression",
|
|
353
|
-
"OperatorReference",
|
|
354
|
-
"Lambda",
|
|
355
|
-
]);
|
|
356
|
-
|
|
357
|
-
// A name such as K(1)!C, whose last part takes no arguments, is a GeneralId too.
|
|
358
|
-
const isOperatorArgument = (argument: Expression) => {
|
|
359
|
-
const items = argument.kind === "Apply" ? getSelectorItems(argument) : undefined;
|
|
360
|
-
return OPERATOR_ARGUMENT_KINDS.has(argument.kind) || (items !== undefined && isGeneralId(items));
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
// The parts of a DEF clause entry that isn't a name, as Generator.genIdToSelector reads them.
|
|
364
|
-
interface DefEntryParts {
|
|
365
|
-
hasChildlessFirstChild: boolean | undefined;
|
|
366
|
-
last: TokenSpan;
|
|
367
|
-
// The kind of SANY's syntax node for the last part, or for a token its text.
|
|
368
|
-
lastKind: string | undefined;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// The kind of the syntax node SANY's parser builds for an expression of each kind.
|
|
372
|
-
const SYNTAX_KINDS = new Map<Expression["kind"], string>([
|
|
373
|
-
["Identifier", "N_GeneralId"],
|
|
374
|
-
["Subexpression", "N_GeneralId"],
|
|
375
|
-
["Number", "N_Number"],
|
|
376
|
-
["Decimal", "N_Real"],
|
|
377
|
-
["SetEnumeration", "N_SetEnumerate"],
|
|
378
|
-
["SetFilter", "N_SubsetOf"],
|
|
379
|
-
["SetMap", "N_SetOfAll"],
|
|
380
|
-
["Tuple", "N_Tuple"],
|
|
381
|
-
["FunctionApplication", "N_FcnAppl"],
|
|
382
|
-
["FunctionConstruction", "N_FcnConst"],
|
|
383
|
-
["FunctionSet", "N_SetOfFcns"],
|
|
384
|
-
["Record", "N_RcdConstructor"],
|
|
385
|
-
["RecordSet", "N_SetOfRcds"],
|
|
386
|
-
["FieldAccess", "N_RecordComponent"],
|
|
387
|
-
["Except", "N_Except"],
|
|
388
|
-
["Prime", "N_PostfixExpr"],
|
|
389
|
-
["Always", "N_PrefixExpr"],
|
|
390
|
-
["Eventually", "N_PrefixExpr"],
|
|
391
|
-
["Enabled", "N_PrefixExpr"],
|
|
392
|
-
["Unchanged", "N_PrefixExpr"],
|
|
393
|
-
["Forall", "N_BoundedQuant"],
|
|
394
|
-
["Exists", "N_BoundedQuant"],
|
|
395
|
-
["UnboundedQuantifier", "N_UnboundedQuant"],
|
|
396
|
-
["Choose", "N_UnBoundedOrBoundedChoose"],
|
|
397
|
-
["If", "N_IfThenElse"],
|
|
398
|
-
["Case", "N_Case"],
|
|
399
|
-
["Let", "N_LetIn"],
|
|
400
|
-
["BoxAction", "N_ActionExpr"],
|
|
401
|
-
["AngleAction", "N_ActionExpr"],
|
|
402
|
-
["WeakFairness", "N_FairnessExpr"],
|
|
403
|
-
["StrongFairness", "N_FairnessExpr"],
|
|
404
|
-
]);
|
|
405
|
-
|
|
406
|
-
// The syntax nodes whose first child is a node with children of its own. The first child of any
|
|
407
|
-
// other is a token, or a node without children.
|
|
408
|
-
const NESTED_FIRST_CHILD_KINDS = new Set([
|
|
409
|
-
"N_GeneralId",
|
|
410
|
-
"N_OpApplication",
|
|
411
|
-
"N_PrefixExpr",
|
|
412
|
-
"N_InfixExpr",
|
|
413
|
-
"N_PostfixExpr",
|
|
414
|
-
"N_FcnAppl",
|
|
415
|
-
"N_RecordComponent",
|
|
416
|
-
]);
|
|
417
|
-
|
|
418
|
-
const POSTFIX_OPERATOR_TEXTS = new Set(["'", "^+", "^*", "^#"]);
|
|
419
|
-
|
|
420
|
-
// SANY's syntax node for an operator that appears where an expression is required. The parser reads
|
|
421
|
-
// a prefix operator other than ~ and -. as a LAMBDA.
|
|
422
|
-
const getOperatorNodeKind = (
|
|
423
|
-
expression: Expression & { kind: "OperatorReference" | "Lambda" },
|
|
424
|
-
text: string,
|
|
425
|
-
) => {
|
|
426
|
-
if (POSTFIX_OPERATOR_TEXTS.has(text)) return "N_GenPostfixOp";
|
|
427
|
-
return expression.kind === "Lambda" || text === "~" || text === "-."
|
|
428
|
-
? "N_GenNonExpPrefixOp"
|
|
429
|
-
: "N_GenInfixOp";
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
// Builds the semantic graph SANY builds for a specification and its modules, in the order SANY
|
|
433
|
-
// analyzes them: a module after the modules it extends and instantiates.
|
|
434
191
|
export const generateSanySpecification = (
|
|
435
192
|
rootSource: string,
|
|
436
193
|
loadModule: (name: string) => string | undefined,
|
|
@@ -441,51 +198,11 @@ export const generateSanySpecification = (
|
|
|
441
198
|
addSanyMessage(messages, { location, text, isWarning: false });
|
|
442
199
|
const addWarning = (location: SanyLocation | undefined, text: string) =>
|
|
443
200
|
addSanyMessage(messages, { location, text, isWarning: true });
|
|
444
|
-
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
// The UID of the nullOAN of the Generator of the module being generated.
|
|
448
|
-
const nullApplication = { id: 0 };
|
|
201
|
+
let lastId = options.version === "1.7.4" ? -1 : 0;
|
|
202
|
+
const getId = () => ++lastId;
|
|
203
|
+
let nullApplicationId = 0;
|
|
449
204
|
|
|
450
|
-
//
|
|
451
|
-
// an error unless the symbols agree in kind and arity, and then a warning unless they are the
|
|
452
|
-
// same definition of a module without parameters.
|
|
453
|
-
const addSymbol = (table: SymbolTable, symbol: SymbolNode) => {
|
|
454
|
-
const binding = resolveSymbol(table, symbol.name);
|
|
455
|
-
if (binding === symbol) return;
|
|
456
|
-
if (binding === undefined) {
|
|
457
|
-
addToContext(getTopContext(table), symbol);
|
|
458
|
-
return;
|
|
459
|
-
}
|
|
460
|
-
const isConflict =
|
|
461
|
-
symbol.kind === "FormalParam" ||
|
|
462
|
-
getSymbolKind(binding) !== getSymbolKind(symbol) ||
|
|
463
|
-
getSymbolArity(binding) !== getSymbolArity(symbol);
|
|
464
|
-
// TLC 1.7.4's SANY reports a conflict before the redefinition of a built-in operator.
|
|
465
|
-
const isBuiltIn = binding.kind === "OpDef" && binding.defKind === "BuiltIn";
|
|
466
|
-
if (isBuiltIn && !(isConflict && options.version === "1.7.4")) {
|
|
467
|
-
addError(
|
|
468
|
-
symbol.location,
|
|
469
|
-
`Symbol ${symbol.name} is a built-in operator, and cannot be redefined.`,
|
|
470
|
-
);
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
473
|
-
if (isConflict) {
|
|
474
|
-
addError(
|
|
475
|
-
symbol.location,
|
|
476
|
-
`Multiply-defined symbol '${symbol.name}': this definition or declaration conflicts \nwith the one at ${formatSanyLocation(binding.location)}.`,
|
|
477
|
-
);
|
|
478
|
-
} else if (!isSameOrigin(symbol, binding)) {
|
|
479
|
-
addWarning(
|
|
480
|
-
symbol.location,
|
|
481
|
-
`Multiple declarations or definitions for symbol ${symbol.name}. \nThis duplicates the one at ${formatSanyLocation(binding.location)}.`,
|
|
482
|
-
);
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
// SANY numbers its semantic nodes as it creates them, which the UIDs of its XML export show:
|
|
486
|
-
// Context's static initializer creates the builtins, SANY.frontEndInitialize creates them again
|
|
487
|
-
// for the specification, and the Generator class creates its InAssumeDummyNode. TLC 1.7.4's SANY
|
|
488
|
-
// creates the builtins only for the specification.
|
|
205
|
+
// SANY creates the builtins twice, in Context's static initializer and in frontEndInitialize.
|
|
489
206
|
if (options.version !== "1.7.4") createBuiltins(getId);
|
|
490
207
|
const builtins = createBuiltins(getId);
|
|
491
208
|
getId();
|
|
@@ -499,9 +216,8 @@ export const generateSanySpecification = (
|
|
|
499
216
|
new TlaSanyFatalError(`Could not parse module ${name} from file ${file}`, name, parseError);
|
|
500
217
|
const lexicalError = options.version === "1.7.4" ? getSany174LexicalError(text) : undefined;
|
|
501
218
|
if (lexicalError !== undefined) throw getParseFailure(lexicalError);
|
|
502
|
-
// HACK: the parser's errors are not SANY's, so a module that fails to parse gets the report
|
|
503
|
-
//
|
|
504
|
-
// such as for an error SANY finds after parsing.
|
|
219
|
+
// HACK: the parser's errors are not SANY's, so a module that fails to parse gets the report of
|
|
220
|
+
// SANY's parser, or the parser's own where SANY's parser would accept the module.
|
|
505
221
|
try {
|
|
506
222
|
return parseSanyModule(getUnboxedSource(text));
|
|
507
223
|
} catch (error) {
|
|
@@ -533,7 +249,6 @@ export const generateSanySpecification = (
|
|
|
533
249
|
const visiting = new Set<string>();
|
|
534
250
|
const path: string[] = [];
|
|
535
251
|
const calculateDependencies = (name: string, text: string | undefined, importer?: string) => {
|
|
536
|
-
// SpecObj.nonCircularityTest.
|
|
537
252
|
const cycleStart = path.indexOf(name);
|
|
538
253
|
if (cycleStart !== -1) {
|
|
539
254
|
throw new TlaSanyFatalError(
|
|
@@ -551,8 +266,6 @@ export const generateSanySpecification = (
|
|
|
551
266
|
path.pop();
|
|
552
267
|
order.push(name);
|
|
553
268
|
};
|
|
554
|
-
// SpecObj.loadSpec: SANY resolves the first unresolved name it finds from the root, every
|
|
555
|
-
// extended module before any instantiated one, each in the context of the module that names it.
|
|
556
269
|
const resolutions = new Set<string>();
|
|
557
270
|
const isResolved = (importer: string, name: string) => resolutions.has(`${importer} ${name}`);
|
|
558
271
|
const findUnresolved = (
|
|
@@ -590,8 +303,6 @@ export const generateSanySpecification = (
|
|
|
590
303
|
}
|
|
591
304
|
};
|
|
592
305
|
const rootName = options.rootFileName ?? parseSource("", rootSource).name;
|
|
593
|
-
// ParameterizedSpecObj.findOrCreateParsedUnit adds the post conditions' modules to the root
|
|
594
|
-
// module's own extendees, before those of its inner modules.
|
|
595
306
|
const getModuleDependencies = (name: string, module: Module) => {
|
|
596
307
|
const dependencies = getDependencies(module);
|
|
597
308
|
if (name !== rootName || options.rootExtendees === undefined) return dependencies;
|
|
@@ -608,52 +319,15 @@ export const generateSanySpecification = (
|
|
|
608
319
|
ast: Module,
|
|
609
320
|
file: SourceFile,
|
|
610
321
|
parent: ModuleNode | undefined,
|
|
611
|
-
|
|
322
|
+
contexts: Context[],
|
|
612
323
|
): ModuleNode => {
|
|
613
|
-
const state = {
|
|
614
|
-
curLevel: 0,
|
|
615
|
-
unresolvedCount: [0],
|
|
616
|
-
unresolvedSum: 0,
|
|
617
|
-
recursiveSectionCount: 0,
|
|
618
|
-
};
|
|
619
|
-
const functions: { name: string; node: OpApplNode }[] = [];
|
|
620
|
-
const exceptStack: OpApplNode[] = [];
|
|
621
|
-
const exceptSpecStack: OpApplNode[] = [];
|
|
622
|
-
const labelScopes: LabelScope[] = [];
|
|
623
|
-
// The contexts in which SANY's $$InAssume marks the scope of an ordinary ASSUME's assumptions.
|
|
624
|
-
const assumeContexts = new WeakSet<Context>();
|
|
625
|
-
const pushLabelScope = () => labelScopes.push({ labels: new Map(), params: [] });
|
|
626
|
-
const popLabels = () => labelScopes.pop()?.labels ?? new Map<string, LabelNode>();
|
|
627
|
-
// Generator.assumeProveDepth, inScopeOfAPDecl, currentGoal and currentGoalClause: where the
|
|
628
|
-
// node being generated lies among the ASSUME/PROVEs of a theorem or step.
|
|
629
|
-
const assumeProveState: AssumeProveState = {
|
|
630
|
-
depth: 0,
|
|
631
|
-
isDeclared: [],
|
|
632
|
-
goal: undefined,
|
|
633
|
-
goalClause: 0,
|
|
634
|
-
};
|
|
635
|
-
const assumeProveScopes = new WeakMap<AssumeProveNode, AssumeProveScope>();
|
|
636
|
-
const labelGoals = new WeakMap<LabelNode, LabelGoal>();
|
|
637
|
-
const endProof = (statement: LevelNode) => {
|
|
638
|
-
const scope = statement.kind === "AssumeProve" ? assumeProveScopes.get(statement) : undefined;
|
|
639
|
-
if (scope) scope.isInProof = false;
|
|
640
|
-
};
|
|
641
|
-
|
|
642
|
-
const getLocation = (expression: Expression) =>
|
|
643
|
-
getSpanLocation(file, getExpressionSpan(file, expression));
|
|
644
|
-
|
|
645
|
-
const getUnitSpan = (location: SourceLocation) =>
|
|
646
|
-
location.range
|
|
647
|
-
? getRangeSpan(file, location.range)
|
|
648
|
-
: { start: getTokenIndex(file, location), end: getTokenIndex(file, location) };
|
|
649
|
-
|
|
650
324
|
const currentModule: ModuleNode = {
|
|
651
325
|
kind: "Module",
|
|
652
326
|
id: getId(),
|
|
653
327
|
name: ast.name,
|
|
654
|
-
location:
|
|
328
|
+
location: getUnitLocation(file, ast.location),
|
|
655
329
|
extendees: [],
|
|
656
|
-
context: getTopContext(
|
|
330
|
+
context: getTopContext(contexts),
|
|
657
331
|
topLevel: [],
|
|
658
332
|
definitions: [],
|
|
659
333
|
opDefsInRecursiveSection: [],
|
|
@@ -665,2335 +339,99 @@ export const generateSanySpecification = (
|
|
|
665
339
|
records: [],
|
|
666
340
|
};
|
|
667
341
|
if (parent) parent.definitions.push(currentModule);
|
|
668
|
-
|
|
669
|
-
const resolveModule = (name: string) => {
|
|
670
|
-
for (let index = symbolTable.contexts.length - 1; index >= 0; index--) {
|
|
671
|
-
const module = getContextModule(symbolTable.contexts[index], name);
|
|
672
|
-
if (module?.kind === "Module") return module;
|
|
673
|
-
}
|
|
674
|
-
return externalModules.get(name);
|
|
675
|
-
};
|
|
676
|
-
|
|
677
|
-
const getModule = (name: string) => {
|
|
678
|
-
const module = resolveModule(name);
|
|
679
|
-
if (module === undefined) throw new TlaSyntaxError(`Could not find module ${name}`);
|
|
680
|
-
return module;
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
const withContext = <Result>(context: Context, generate: () => Result) => {
|
|
684
|
-
symbolTable.contexts.push(context);
|
|
685
|
-
const result = generate();
|
|
686
|
-
symbolTable.contexts.pop();
|
|
687
|
-
return result;
|
|
688
|
-
};
|
|
689
|
-
|
|
690
|
-
const createFormalParam = (
|
|
691
|
-
name: string,
|
|
692
|
-
arity: number,
|
|
693
|
-
location: SanyLocation | undefined,
|
|
694
|
-
isAdded: boolean,
|
|
695
|
-
): FormalParamNode => {
|
|
696
|
-
const param: FormalParamNode = {
|
|
697
|
-
...createLevelFields(getId()),
|
|
698
|
-
kind: "FormalParam",
|
|
699
|
-
name,
|
|
700
|
-
arity,
|
|
701
|
-
location,
|
|
702
|
-
};
|
|
703
|
-
if (isAdded) addSymbol(symbolTable, param);
|
|
704
|
-
return param;
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
const createOpDef = (
|
|
708
|
-
fields: Partial<OpDefNode> & Pick<OpDefNode, "name" | "location">,
|
|
709
|
-
): OpDefNode => ({
|
|
710
|
-
...createLevelFields(getId()),
|
|
711
|
-
kind: "OpDef",
|
|
712
|
-
defKind: "UserDefined",
|
|
713
|
-
arity: fields.params?.length ?? 0,
|
|
714
|
-
params: [],
|
|
715
|
-
body: undefined,
|
|
716
|
-
isLocal: false,
|
|
717
|
-
module: currentModule,
|
|
718
|
-
source: undefined,
|
|
719
|
-
preComments: [],
|
|
720
|
-
isDefined: true,
|
|
721
|
-
inRecursive: false,
|
|
722
|
-
inRecursiveSection: false,
|
|
723
|
-
recursiveSection: -1,
|
|
724
|
-
letInLevel: 0,
|
|
725
|
-
weights: (fields.params ?? []).map(() => 0),
|
|
726
|
-
isLeibnizArg: (fields.params ?? []).map(() => true),
|
|
727
|
-
isLeibniz: true,
|
|
728
|
-
opLevelCond: undefined,
|
|
729
|
-
maxLevels: (fields.params ?? []).map(() => TEMPORAL_LEVEL),
|
|
730
|
-
minMaxLevel: undefined,
|
|
731
|
-
labels: new Map(),
|
|
732
|
-
step: undefined,
|
|
733
|
-
...fields,
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
const setRecursionFields = (definition: OpDefNode) => {
|
|
737
|
-
definition.letInLevel = state.curLevel;
|
|
738
|
-
if (state.unresolvedSum > 0) {
|
|
739
|
-
definition.recursiveSection = state.recursiveSectionCount;
|
|
740
|
-
definition.inRecursiveSection = state.unresolvedCount[state.curLevel] > 0;
|
|
741
|
-
currentModule.opDefsInRecursiveSection.push(definition);
|
|
742
|
-
}
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
const createOpAppl = (
|
|
746
|
-
operator: SymbolNode,
|
|
747
|
-
operands: (ExprOrOpArgNode | undefined)[],
|
|
748
|
-
location: SanyLocation,
|
|
749
|
-
binding: OpApplBinding = {},
|
|
750
|
-
id = getId(),
|
|
751
|
-
): OpApplNode => ({
|
|
752
|
-
...createLevelFields(id),
|
|
753
|
-
kind: "OpAppl",
|
|
754
|
-
operator,
|
|
755
|
-
operands,
|
|
756
|
-
unbounded: binding.unbounded,
|
|
757
|
-
bounded: binding.bounded,
|
|
758
|
-
tupleOrs: binding.tupleOrs,
|
|
759
|
-
ranges: binding.ranges ?? [],
|
|
760
|
-
location,
|
|
761
|
-
});
|
|
762
|
-
|
|
763
|
-
// Generator.nullOAN, which SANY returns where it reported an error and carries on. It has no
|
|
764
|
-
// syntax tree node, so its location is that of the builtins.
|
|
765
|
-
const createNullNode = (): OpApplNode => ({
|
|
766
|
-
...createOpAppl(getBuiltin("$Nop"), [], BUILTIN_LOCATION, {}, nullApplication.id),
|
|
767
|
-
isNull: true,
|
|
768
|
-
});
|
|
769
|
-
|
|
770
|
-
const getBuiltin = (name: string) => {
|
|
771
|
-
const builtin = builtinsByName.get(name);
|
|
772
|
-
if (!builtin) throw new TlaUnsupportedError(`SANY has no builtin operator ${name}`);
|
|
773
|
-
return builtin;
|
|
774
|
-
};
|
|
775
|
-
|
|
776
|
-
const applyBuiltin = (
|
|
777
|
-
name: string,
|
|
778
|
-
operands: (ExprOrOpArgNode | undefined)[],
|
|
779
|
-
location: SanyLocation,
|
|
780
|
-
binding: OpApplBinding = {},
|
|
781
|
-
) => createOpAppl(getBuiltin(name), operands, location, binding);
|
|
782
|
-
|
|
783
|
-
const createString = (value: string, location: SanyLocation): ExprNode => ({
|
|
784
|
-
...createLevelFields(getId()),
|
|
785
|
-
kind: "String",
|
|
786
|
-
value,
|
|
787
|
-
location,
|
|
788
|
-
});
|
|
789
|
-
|
|
790
|
-
const createOpArg = (op: SymbolNode, location: SanyLocation): OpArgNode => ({
|
|
791
|
-
...createLevelFields(getId()),
|
|
792
|
-
kind: "OpArg",
|
|
793
|
-
op,
|
|
794
|
-
location,
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
const resolveName = (name: string, location: SanyLocation) => {
|
|
798
|
-
const symbol = resolveSymbol(symbolTable, resolveSynonym(name));
|
|
799
|
-
if (symbol === undefined || symbol.kind === "Module") {
|
|
800
|
-
throw new TlaSyntaxError(
|
|
801
|
-
`Unknown operator: \`${name}' at line ${location.beginLine}, column ${location.beginColumn} of module ${location.file}`,
|
|
802
|
-
);
|
|
803
|
-
}
|
|
804
|
-
return symbol;
|
|
805
|
-
};
|
|
806
|
-
|
|
807
|
-
// OpDefNode.match: a definition takes as many arguments as it has parameters, which differs
|
|
808
|
-
// from its arity only where its RECURSIVE declaration has another, and an operator it takes
|
|
809
|
-
// must be given an operator of its arity that takes no operators.
|
|
810
|
-
const matchArguments = (
|
|
811
|
-
operator: SymbolNode,
|
|
812
|
-
operands: ExprOrOpArgNode[],
|
|
813
|
-
location: SanyLocation,
|
|
814
|
-
) => {
|
|
815
|
-
if (operator.kind !== "OpDef" || operator.defKind !== "UserDefined") return;
|
|
816
|
-
const params = operator.params ?? [];
|
|
817
|
-
if (params.length !== operands.length) {
|
|
818
|
-
addError(
|
|
819
|
-
location,
|
|
820
|
-
`Wrong number of arguments (${operands.length}) given to operator '${operator.name}', \nwhich requires ${params.length} arguments.`,
|
|
821
|
-
);
|
|
822
|
-
return;
|
|
823
|
-
}
|
|
824
|
-
params.forEach((param, index) => {
|
|
825
|
-
const operand = operands[index];
|
|
826
|
-
if (param.arity === 0 && operand.kind === "OpArg") {
|
|
827
|
-
addError(
|
|
828
|
-
location,
|
|
829
|
-
`Operator used in argument number ${index + 1} has incorrect number of arguments.`,
|
|
830
|
-
);
|
|
831
|
-
}
|
|
832
|
-
const isMatching =
|
|
833
|
-
operand.kind === "OpArg" &&
|
|
834
|
-
getSymbolArity(operand.op) === param.arity &&
|
|
835
|
-
!(
|
|
836
|
-
operand.op.kind === "OpDef" &&
|
|
837
|
-
(operand.op.params ?? []).some((parameter) => parameter.arity > 0)
|
|
838
|
-
);
|
|
839
|
-
if (param.arity > 0 && !isMatching) {
|
|
840
|
-
addError(
|
|
841
|
-
location,
|
|
842
|
-
`Argument number ${index + 1} to operator '${operator.name}' \nshould be a ${param.arity}-parameter operator.`,
|
|
843
|
-
);
|
|
844
|
-
}
|
|
845
|
-
});
|
|
846
|
-
};
|
|
847
|
-
|
|
848
|
-
const selectorToNode = createSelectorResolver({
|
|
342
|
+
const generation: ModuleGeneration = {
|
|
849
343
|
version: options.version,
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
copySubstIn: (prefix, body) =>
|
|
874
|
-
createSubstIn(
|
|
875
|
-
"SubstIn",
|
|
876
|
-
prefix.substs,
|
|
877
|
-
body,
|
|
878
|
-
prefix.instantiatedModule,
|
|
879
|
-
prefix.location,
|
|
880
|
-
prefix.instantiatingModule,
|
|
881
|
-
),
|
|
882
|
-
getBuiltin,
|
|
883
|
-
addError: (location, text) => {
|
|
884
|
-
addError(location, text);
|
|
885
|
-
return createNullNode();
|
|
886
|
-
},
|
|
887
|
-
isOutOfLabelScope: (label) => {
|
|
888
|
-
const goal = labelGoals.get(label);
|
|
889
|
-
return (
|
|
890
|
-
goal !== undefined &&
|
|
891
|
-
goal.scope.isDeclaredBefore[goal.clause] &&
|
|
892
|
-
goal.scope.isSuffices === goal.scope.isInProof
|
|
893
|
-
);
|
|
894
|
-
},
|
|
895
|
-
isOutOfClauseScope: (assumeProve, position) => {
|
|
896
|
-
const scope = assumeProveScopes.get(assumeProve);
|
|
897
|
-
return (
|
|
898
|
-
scope !== undefined &&
|
|
899
|
-
scope.isDeclaredBefore[position - 1] &&
|
|
900
|
-
(!scope.hasGoal || scope.isSuffices === scope.isInProof)
|
|
901
|
-
);
|
|
344
|
+
addError,
|
|
345
|
+
addWarning,
|
|
346
|
+
getId,
|
|
347
|
+
builtinsByName,
|
|
348
|
+
externalModules,
|
|
349
|
+
nullApplicationId,
|
|
350
|
+
file,
|
|
351
|
+
contexts,
|
|
352
|
+
currentModule,
|
|
353
|
+
curLevel: 0,
|
|
354
|
+
unresolvedCount: [0],
|
|
355
|
+
unresolvedSum: 0,
|
|
356
|
+
recursiveSectionCount: 0,
|
|
357
|
+
functions: [],
|
|
358
|
+
exceptStack: [],
|
|
359
|
+
exceptSpecStack: [],
|
|
360
|
+
labelScopes: [],
|
|
361
|
+
assumeContexts: new WeakSet(),
|
|
362
|
+
assumeProveState: {
|
|
363
|
+
depth: 0,
|
|
364
|
+
isDeclared: [],
|
|
365
|
+
goal: undefined,
|
|
366
|
+
goalClause: 0,
|
|
902
367
|
},
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
const
|
|
915
|
-
|
|
916
|
-
return { ...item, location: getSpanLocation(file, { start: index, end: close }) };
|
|
917
|
-
}
|
|
918
|
-
cursor.value = index + 1;
|
|
919
|
-
const location = getTokenLocation(file, index);
|
|
920
|
-
if (item.args === undefined || !isToken(file, index + 1, "(")) return { ...item, location };
|
|
921
|
-
const close = getMatchingIndex(file, index + 1);
|
|
922
|
-
cursor.value = close + 1;
|
|
923
|
-
return {
|
|
924
|
-
...item,
|
|
925
|
-
location,
|
|
926
|
-
argsLocation: getSpanLocation(file, { start: index + 1, end: close }),
|
|
927
|
-
};
|
|
928
|
-
});
|
|
929
|
-
};
|
|
930
|
-
|
|
931
|
-
const generateSelector = (
|
|
932
|
-
expression: Expression,
|
|
933
|
-
expectedArity: number,
|
|
934
|
-
isFact: boolean,
|
|
935
|
-
isDef: boolean,
|
|
936
|
-
) => {
|
|
937
|
-
const items = getSelectorItems(expression);
|
|
938
|
-
if (items === undefined)
|
|
939
|
-
throw new TlaSyntaxError(`Expected a name, found ${expression.kind}`);
|
|
940
|
-
return selectorToNode(
|
|
941
|
-
locateSelectorItems(items, getExpressionSpan(file, expression)),
|
|
942
|
-
expectedArity,
|
|
943
|
-
isFact,
|
|
944
|
-
isDef,
|
|
945
|
-
getLocation(expression),
|
|
946
|
-
);
|
|
947
|
-
};
|
|
948
|
-
|
|
949
|
-
const generateSelectorExpression = (expression: Expression) => {
|
|
950
|
-
const node = generateSelector(expression, 0, false, false);
|
|
951
|
-
if (node.kind !== "OpAppl") throw new TlaSyntaxError("Expected an expression");
|
|
952
|
-
return node;
|
|
953
|
-
};
|
|
954
|
-
|
|
955
|
-
const checkRecursion = (node: ExprNode) => {
|
|
956
|
-
if (node.kind !== "OpAppl") return;
|
|
957
|
-
const recursive = functions.find((entry) => entry.name === node.operator.name);
|
|
958
|
-
if (recursive) recursive.node.operator = getBuiltin("$RecursiveFcnSpec");
|
|
959
|
-
};
|
|
960
|
-
|
|
961
|
-
// The bound groups of a quantifier, set filter, set map or function: tla-ts expands
|
|
962
|
-
// \A x, y \in S into a bound per name that shares the set, and SANY keeps one group.
|
|
963
|
-
const getBoundGroups = (bounds: Bound[]) =>
|
|
964
|
-
bounds.reduce<Bound[][]>((groups, bound) => {
|
|
965
|
-
const last = groups[groups.length - 1];
|
|
966
|
-
const isSameGroup =
|
|
967
|
-
last !== undefined &&
|
|
968
|
-
last[0].set === bound.set &&
|
|
969
|
-
bound.pattern.kind === "Name" &&
|
|
970
|
-
last[0].pattern.kind === "Name";
|
|
971
|
-
if (isSameGroup) last.push(bound);
|
|
972
|
-
else groups.push([bound]);
|
|
973
|
-
return groups;
|
|
974
|
-
}, []);
|
|
975
|
-
|
|
976
|
-
// processQuantBoundArgs: SANY generates every bounding set before it declares the symbols,
|
|
977
|
-
// each named at its token between the end of the previous group and its set.
|
|
978
|
-
const generateBounds = (bounds: Bound[], searchStart: number) =>
|
|
979
|
-
generateBoundsWithRanges(
|
|
980
|
-
bounds,
|
|
981
|
-
searchStart,
|
|
982
|
-
getBoundGroups(bounds).map((group) => generateExpression(group[0].set)),
|
|
983
|
-
);
|
|
984
|
-
|
|
985
|
-
// Generator.pushFormalParams: a label in the expression has to name the parameters it binds.
|
|
986
|
-
const generateWithParams = (params: FormalParamNode[], expression: Expression) => {
|
|
987
|
-
const scope = labelScopes.at(-1);
|
|
988
|
-
scope?.params.push(params);
|
|
989
|
-
const node = generateExpression(expression);
|
|
990
|
-
scope?.params.pop();
|
|
991
|
-
return node;
|
|
992
|
-
};
|
|
993
|
-
|
|
994
|
-
const generateLambda = (expression: Expression & { kind: "Lambda" }): OpDefNode => {
|
|
995
|
-
const span = getExpressionSpan(file, expression);
|
|
996
|
-
const nameIndexes = findNameTokens(file, span.start, span.end, expression.params);
|
|
997
|
-
return withContext(createContext(), () => {
|
|
998
|
-
const params = expression.params.map((name, index) =>
|
|
999
|
-
createFormalParam(name, 0, getTokenLocation(file, nameIndexes[index]), true),
|
|
1000
|
-
);
|
|
1001
|
-
const body = generateWithParams(params, expression.body);
|
|
1002
|
-
return createOpDef({
|
|
1003
|
-
name: "LAMBDA",
|
|
1004
|
-
params,
|
|
1005
|
-
body,
|
|
1006
|
-
location: getSpanLocation(file, span),
|
|
1007
|
-
preComments: getPreComments(file, span.start),
|
|
1008
|
-
});
|
|
1009
|
-
});
|
|
1010
|
-
};
|
|
1011
|
-
|
|
1012
|
-
// tla-ts desugars an operator symbol passed as an argument, such as SUBSET or -., into a LAMBDA
|
|
1013
|
-
// without a range.
|
|
1014
|
-
const getOperatorArgumentName = (argument: Expression) =>
|
|
1015
|
-
argument.kind === "OperatorReference"
|
|
1016
|
-
? argument.operator
|
|
1017
|
-
: argument.kind === "Lambda" && argument.body.kind === "Apply"
|
|
1018
|
-
? argument.body.operator
|
|
1019
|
-
: file.tokens[getTokenIndex(file, argument.location)].text;
|
|
1020
|
-
|
|
1021
|
-
// Generator.generateOpArg: after an error, SANY gives its nullOpArg.
|
|
1022
|
-
const generateOperatorArgument = (
|
|
1023
|
-
argument: Expression,
|
|
1024
|
-
target: OpDeclNode,
|
|
1025
|
-
): OpArgNode | OpApplNode => {
|
|
1026
|
-
const location = getLocation(argument);
|
|
1027
|
-
if (argument.kind === "Lambda") {
|
|
1028
|
-
if (argument.location.range !== undefined)
|
|
1029
|
-
return createOpArg(generateLambda(argument), location);
|
|
1030
|
-
return createOpArg(resolveName(getOperatorArgumentName(argument), location), location);
|
|
1031
|
-
}
|
|
1032
|
-
if (!isOperatorArgument(argument)) {
|
|
1033
|
-
addError(
|
|
1034
|
-
location,
|
|
1035
|
-
`Arity ${target.arity} operator (not an expression) is expected \nto substitute for CONSTANT '${target.name}'.`,
|
|
1036
|
-
);
|
|
1037
|
-
return createNullNode();
|
|
1038
|
-
}
|
|
1039
|
-
if (argument.kind === "OperatorReference") {
|
|
1040
|
-
return createOpArg(resolveName(argument.operator, location), location);
|
|
1041
|
-
}
|
|
1042
|
-
const node = generateSelector(argument, target.arity, false, false);
|
|
1043
|
-
if (node.kind === "OpArg" || (node.kind === "OpAppl" && node.isNull === true)) return node;
|
|
1044
|
-
throw new TlaSyntaxError(
|
|
1045
|
-
`Expected an operator argument at line ${location.beginLine}, column ${location.beginColumn}`,
|
|
1046
|
-
);
|
|
1047
|
-
};
|
|
1048
|
-
|
|
1049
|
-
// generateExprOrOpArg: an argument is an operator where the parameter takes arguments. SANY
|
|
1050
|
-
// reports an argument that doesn't fit at the operator's name and carries on.
|
|
1051
|
-
const generateArgument = (
|
|
1052
|
-
operator: SymbolNode,
|
|
1053
|
-
index: number,
|
|
1054
|
-
argument: Expression,
|
|
1055
|
-
operatorLocation: SanyLocation,
|
|
1056
|
-
): ExprOrOpArgNode => {
|
|
1057
|
-
const isDefinition =
|
|
1058
|
-
operator.kind === "OpDef" &&
|
|
1059
|
-
(operator.defKind === "UserDefined" || operator.defKind === "ModuleInstance");
|
|
1060
|
-
const expectedArity = isDefinition ? (operator.params?.[index]?.arity ?? 0) : 0;
|
|
1061
|
-
if (expectedArity === 0) return generateExpression(argument);
|
|
1062
|
-
const span = getExpressionSpan(file, argument);
|
|
1063
|
-
const location = getSpanLocation(file, span);
|
|
1064
|
-
const position = `${index + 1} (counting from 1)`;
|
|
1065
|
-
const argumentSpan = getArgumentSpan(file, span);
|
|
1066
|
-
if (argumentSpan !== span || !isOperatorArgument(argument)) {
|
|
1067
|
-
addError(
|
|
1068
|
-
getSpanLocation(file, argumentSpan),
|
|
1069
|
-
`An expression appears as argument number ${position} to operator '${operator.name}', in a position an operator is required.`,
|
|
1070
|
-
);
|
|
1071
|
-
return createNullNode();
|
|
1072
|
-
}
|
|
1073
|
-
if (
|
|
1074
|
-
argument.kind === "Identifier" ||
|
|
1075
|
-
argument.kind === "Subexpression" ||
|
|
1076
|
-
argument.kind === "Apply"
|
|
1077
|
-
) {
|
|
1078
|
-
// The selector names the operator, or reports why not and gives SANY's nullOAN.
|
|
1079
|
-
const node = generateSelector(argument, expectedArity, false, false);
|
|
1080
|
-
if (node.kind === "OpArg" || node.kind === "OpAppl") return node;
|
|
1081
|
-
throw new TlaSyntaxError(
|
|
1082
|
-
`Expected an operator argument at ${formatSanyLocation(location)}`,
|
|
1083
|
-
);
|
|
1084
|
-
}
|
|
1085
|
-
if (argument.kind === "Lambda" && argument.location.range !== undefined) {
|
|
1086
|
-
const lambda = generateLambda(argument);
|
|
1087
|
-
const arity = getSymbolArity(lambda);
|
|
1088
|
-
if (arity === expectedArity) return createOpArg(lambda, location);
|
|
1089
|
-
addError(
|
|
1090
|
-
operatorLocation,
|
|
1091
|
-
`Lambda expression with arity ${arity} used as argument ${index + 1} of operator \`${operator.name}', \nbut an operator of arity ${expectedArity} is required.`,
|
|
1092
|
-
);
|
|
1093
|
-
return createNullNode();
|
|
1094
|
-
}
|
|
1095
|
-
const name = getOperatorArgumentName(argument);
|
|
1096
|
-
const symbol = resolveSymbol(symbolTable, resolveSynonym(name));
|
|
1097
|
-
if (symbol === undefined) {
|
|
1098
|
-
addError(location, `Could not find declaration or definition of symbol '${name}'.`);
|
|
1099
|
-
return createNullNode();
|
|
1100
|
-
}
|
|
1101
|
-
const arity = getSymbolArity(symbol);
|
|
1102
|
-
if (arity === expectedArity) return createOpArg(symbol, location);
|
|
1103
|
-
addError(
|
|
1104
|
-
operatorLocation,
|
|
1105
|
-
`Operator with incorrect arity passed as argument. \nOperator '${symbol.name}' of arity ${arity} is argument number ${position} to operator \`${formatSanyLocation(operator.location)}', \nbut an operator of arity ${expectedArity} was expected.`,
|
|
1106
|
-
);
|
|
1107
|
-
return createNullNode();
|
|
1108
|
-
};
|
|
1109
|
-
|
|
1110
|
-
// A /\ B /\ C nests as ((A /\ B) /\ C) in SANY; tla-ts flattens it.
|
|
1111
|
-
const generateJunction = (expression: Expression & { kind: "And" | "Or" }) => {
|
|
1112
|
-
const location = getLocation(expression);
|
|
1113
|
-
if (expression.isBulleted) {
|
|
1114
|
-
return applyBuiltin(
|
|
1115
|
-
expression.kind === "And" ? "$ConjList" : "$DisjList",
|
|
1116
|
-
expression.operands.map(generateExpression),
|
|
1117
|
-
location,
|
|
1118
|
-
);
|
|
1119
|
-
}
|
|
1120
|
-
const span = getExpressionSpan(file, expression);
|
|
1121
|
-
const name = expression.kind === "And" ? "\\land" : "\\lor";
|
|
1122
|
-
const [first, ...rest] = expression.operands;
|
|
1123
|
-
return rest.reduce<ExprNode>((left, operand, index) => {
|
|
1124
|
-
const operandSpan = getParenthesizedSpan(file, getExpressionSpan(file, operand));
|
|
1125
|
-
const end = index === rest.length - 1 ? span.end : operandSpan.end;
|
|
1126
|
-
return applyBuiltin(
|
|
1127
|
-
name,
|
|
1128
|
-
[left, generateExpression(operand)],
|
|
1129
|
-
getSpanLocation(file, { start: span.start, end }),
|
|
1130
|
-
);
|
|
1131
|
-
}, generateExpression(first));
|
|
1132
|
-
};
|
|
1133
|
-
|
|
1134
|
-
const generateCase = (expression: Expression & { kind: "Case" }) => {
|
|
1135
|
-
const span = getExpressionSpan(file, expression);
|
|
1136
|
-
const arms = expression.arms.map((arm, index) => {
|
|
1137
|
-
const condition = getParenthesizedSpan(file, getExpressionSpan(file, arm.condition));
|
|
1138
|
-
const isLast = index === expression.arms.length - 1 && expression.other === undefined;
|
|
1139
|
-
const body = getParenthesizedSpan(file, getExpressionSpan(file, arm.body));
|
|
1140
|
-
const operands = [generateExpression(arm.condition), generateExpression(arm.body)];
|
|
1141
|
-
return applyBuiltin(
|
|
1142
|
-
"$Pair",
|
|
1143
|
-
operands,
|
|
1144
|
-
getSpanLocation(file, { start: condition.start, end: isLast ? span.end : body.end }),
|
|
1145
|
-
);
|
|
1146
|
-
});
|
|
1147
|
-
const other = expression.other;
|
|
1148
|
-
if (other) {
|
|
1149
|
-
const body = getParenthesizedSpan(file, getExpressionSpan(file, other));
|
|
1150
|
-
const otherIndex = findTokenBefore(body.start, "OTHER");
|
|
1151
|
-
arms.push(
|
|
1152
|
-
applyBuiltin(
|
|
1153
|
-
"$Pair",
|
|
1154
|
-
[undefined, generateExpression(other)],
|
|
1155
|
-
getSpanLocation(file, { start: otherIndex, end: span.end }),
|
|
1156
|
-
),
|
|
1157
|
-
);
|
|
1158
|
-
}
|
|
1159
|
-
return applyBuiltin("$Case", arms, getSpanLocation(file, span));
|
|
1160
|
-
};
|
|
1161
|
-
|
|
1162
|
-
const findTokenBefore = (index: number, text: string) => {
|
|
1163
|
-
for (let current = index; current >= 0; current--) {
|
|
1164
|
-
if (isToken(file, current, text)) return current;
|
|
1165
|
-
}
|
|
1166
|
-
return index;
|
|
1167
|
-
};
|
|
1168
|
-
|
|
1169
|
-
const generateExcept = (expression: Expression & { kind: "Except" }) => {
|
|
1170
|
-
const span = getExpressionSpan(file, expression);
|
|
1171
|
-
const location = getSpanLocation(file, span);
|
|
1172
|
-
const operands: ExprOrOpArgNode[] = [generateExpression(expression.function)];
|
|
1173
|
-
const exceptNode = applyBuiltin("$Except", operands, location);
|
|
1174
|
-
const functionSpan = getExpressionSpan(file, expression.function);
|
|
1175
|
-
const cursor = { value: findToken(file, functionSpan.end + 1, span.end, "EXCEPT") + 1 };
|
|
1176
|
-
expression.updates.forEach((update) => {
|
|
1177
|
-
const bang = findToken(file, cursor.value, span.end, "!");
|
|
1178
|
-
const valueSpan = getParenthesizedSpan(file, getExpressionSpan(file, update.value));
|
|
1179
|
-
const specLocation = getSpanLocation(file, { start: bang, end: valueSpan.end });
|
|
1180
|
-
const pathCursor = { value: bang + 1 };
|
|
1181
|
-
const components = update.path.map((step): ExprNode => {
|
|
1182
|
-
if (step.kind === "Field") {
|
|
1183
|
-
const nameIndex = findNameOrKeyword(pathCursor.value, step.name);
|
|
1184
|
-
pathCursor.value = nameIndex + 1;
|
|
1185
|
-
return createString(step.name, getTokenLocation(file, nameIndex));
|
|
1186
|
-
}
|
|
1187
|
-
const open = findToken(file, pathCursor.value, span.end, "[");
|
|
1188
|
-
const close = getMatchingIndex(file, open);
|
|
1189
|
-
pathCursor.value = close + 1;
|
|
1190
|
-
if (step.args.length === 1) return generateExpression(step.args[0]);
|
|
1191
|
-
return applyBuiltin(
|
|
1192
|
-
"$Tuple",
|
|
1193
|
-
step.args.map(generateExpression),
|
|
1194
|
-
getSpanLocation(file, { start: open, end: close }),
|
|
1195
|
-
);
|
|
1196
|
-
});
|
|
1197
|
-
const sequence = applyBuiltin("$Seq", components, specLocation);
|
|
1198
|
-
const pair = applyBuiltin("$Pair", [sequence], specLocation);
|
|
1199
|
-
exceptSpecStack.push(pair);
|
|
1200
|
-
exceptStack.push(exceptNode);
|
|
1201
|
-
pair.operands.push(generateExpression(update.value));
|
|
1202
|
-
exceptSpecStack.pop();
|
|
1203
|
-
exceptStack.pop();
|
|
1204
|
-
operands.push(pair);
|
|
1205
|
-
cursor.value = valueSpan.end + 1;
|
|
1206
|
-
});
|
|
1207
|
-
return exceptNode;
|
|
1208
|
-
};
|
|
1209
|
-
|
|
1210
|
-
const findNameOrKeyword = (start: number, name: string) => {
|
|
1211
|
-
for (let index = start; index < file.tokens.length; index++) {
|
|
1212
|
-
if (file.tokens[index].text === name && file.tokens[index].kind !== "String") return index;
|
|
1213
|
-
}
|
|
1214
|
-
return start;
|
|
1215
|
-
};
|
|
1216
|
-
|
|
1217
|
-
const generateRecordFields = (
|
|
1218
|
-
operator: "$RcdConstructor" | "$SetOfRcds",
|
|
1219
|
-
expression: Expression & { kind: "Record" | "RecordSet" },
|
|
1220
|
-
) => {
|
|
1221
|
-
const span = getExpressionSpan(file, expression);
|
|
1222
|
-
const cursor = { value: span.start + 1 };
|
|
1223
|
-
const pairs = expression.fields.map((field, fieldIndex) => {
|
|
1224
|
-
const nameIndex = findNameOrKeyword(cursor.value, field.name);
|
|
1225
|
-
const valueSpan = getParenthesizedSpan(file, getExpressionSpan(file, field.value));
|
|
1226
|
-
cursor.value = valueSpan.end + 1;
|
|
1227
|
-
if (expression.fields.slice(0, fieldIndex).some((other) => other.name === field.name)) {
|
|
1228
|
-
addError(getTokenLocation(file, nameIndex), "Non-unique fields in constructor.");
|
|
1229
|
-
}
|
|
1230
|
-
return applyBuiltin(
|
|
1231
|
-
"$Pair",
|
|
1232
|
-
[
|
|
1233
|
-
createString(field.name, getTokenLocation(file, nameIndex)),
|
|
1234
|
-
generateExpression(field.value),
|
|
1235
|
-
],
|
|
1236
|
-
getSpanLocation(file, { start: nameIndex, end: valueSpan.end }),
|
|
1237
|
-
);
|
|
1238
|
-
});
|
|
1239
|
-
const record = applyBuiltin(operator, pairs, getSpanLocation(file, span));
|
|
1240
|
-
currentModule.records.push(record);
|
|
1241
|
-
return record;
|
|
1242
|
-
};
|
|
1243
|
-
|
|
1244
|
-
const getTokenPosition = (index: number) => ({
|
|
1245
|
-
line: file.tokens[index].line,
|
|
1246
|
-
column: file.tokens[index].column,
|
|
1247
|
-
});
|
|
1248
|
-
|
|
1249
|
-
// The declarations of a RECURSIVE statement, which the syntax tree of a LET leaves out:
|
|
1250
|
-
// names, each with (_, ...) for its arguments.
|
|
1251
|
-
const getRecursiveDeclarations = (start: number) => {
|
|
1252
|
-
const declarations: Declaration[] = [];
|
|
1253
|
-
let index = start;
|
|
1254
|
-
while (file.tokens[index]?.kind === "Identifier") {
|
|
1255
|
-
const end = isToken(file, index + 1, "(") ? getMatchingIndex(file, index + 1) : index;
|
|
1256
|
-
const arity = file.tokens
|
|
1257
|
-
.slice(index + 1, end)
|
|
1258
|
-
.filter((token) => token.text === "_").length;
|
|
1259
|
-
declarations.push({
|
|
1260
|
-
name: file.tokens[index].text,
|
|
1261
|
-
arity,
|
|
1262
|
-
location: {
|
|
1263
|
-
...getTokenPosition(index),
|
|
1264
|
-
range: {
|
|
1265
|
-
module: file.name,
|
|
1266
|
-
start: getTokenPosition(index),
|
|
1267
|
-
end: getTokenPosition(end),
|
|
1268
|
-
},
|
|
1269
|
-
},
|
|
1270
|
-
});
|
|
1271
|
-
if (!isToken(file, end + 1, ",")) break;
|
|
1272
|
-
index = end + 2;
|
|
1273
|
-
}
|
|
1274
|
-
return declarations;
|
|
1275
|
-
};
|
|
1276
|
-
|
|
1277
|
-
// The units of a LET in source order, with its RECURSIVE statements found among its tokens.
|
|
1278
|
-
const getLetUnits = (expression: Expression & { kind: "Let" }) => {
|
|
1279
|
-
const units = [
|
|
1280
|
-
...expression.definitions.map((definition) => ({
|
|
1281
|
-
unit: { kind: "Definition", definition } satisfies LetUnit,
|
|
1282
|
-
span: getDefinitionSpan(definition),
|
|
1283
|
-
})),
|
|
1284
|
-
...(expression.instances ?? []).map((instance) => ({
|
|
1285
|
-
unit: { kind: "Instance", instance } satisfies LetUnit,
|
|
1286
|
-
span: getDefinitionSpan(instance),
|
|
1287
|
-
})),
|
|
1288
|
-
];
|
|
1289
|
-
const letStart = getExpressionSpan(file, expression).start;
|
|
1290
|
-
const bodyStart = getParenthesizedSpan(file, getExpressionSpan(file, expression.body)).start;
|
|
1291
|
-
const recursives: { unit: LetUnit; span: TokenSpan }[] = [];
|
|
1292
|
-
for (let index = letStart + 1; index < bodyStart; index++) {
|
|
1293
|
-
const isInUnit = units.some(
|
|
1294
|
-
(entry) => entry.span.start <= index && index <= entry.span.end,
|
|
1295
|
-
);
|
|
1296
|
-
if (isToken(file, index, "RECURSIVE") && !isInUnit) {
|
|
1297
|
-
recursives.push({
|
|
1298
|
-
unit: { kind: "Recursive", declarations: getRecursiveDeclarations(index + 1) },
|
|
1299
|
-
span: { start: index, end: index },
|
|
1300
|
-
});
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
return [...units, ...recursives]
|
|
1304
|
-
.toSorted((first, second) => first.span.start - second.span.start)
|
|
1305
|
-
.map((entry) => entry.unit);
|
|
1306
|
-
};
|
|
1307
|
-
|
|
1308
|
-
const generateLet = (expression: Expression & { kind: "Let" }): LetInNode => {
|
|
1309
|
-
const context = createContext();
|
|
1310
|
-
return withContext(context, () => {
|
|
1311
|
-
state.curLevel++;
|
|
1312
|
-
state.unresolvedCount[state.curLevel] = 0;
|
|
1313
|
-
const opDefs: (OpDefNode | ThmOrAssumpDefNode)[] = [];
|
|
1314
|
-
const instances: InstanceNode[] = [];
|
|
1315
|
-
getLetUnits(expression).forEach((unit) => {
|
|
1316
|
-
if (unit.kind === "Recursive") processRecursive(unit.declarations);
|
|
1317
|
-
else if (unit.kind === "Instance") {
|
|
1318
|
-
processModuleDefinition(unit.instance, opDefs, instances);
|
|
1319
|
-
} else if (unit.definition.kind === "Function") {
|
|
1320
|
-
processFunction(unit.definition, opDefs);
|
|
1321
|
-
} else processOperator(unit.definition, opDefs);
|
|
1322
|
-
});
|
|
1323
|
-
checkForUndefinedRecursiveOps();
|
|
1324
|
-
state.curLevel = Math.max(state.curLevel - 1, 0);
|
|
1325
|
-
const body = generateExpression(expression.body);
|
|
1326
|
-
return {
|
|
1327
|
-
...createLevelFields(getId()),
|
|
1328
|
-
kind: "LetIn",
|
|
1329
|
-
opDefs,
|
|
1330
|
-
instances,
|
|
1331
|
-
body,
|
|
1332
|
-
context,
|
|
1333
|
-
location: getLocation(expression),
|
|
1334
|
-
};
|
|
1335
|
-
});
|
|
1336
|
-
};
|
|
1337
|
-
|
|
1338
|
-
const generateChoose = (expression: Expression & { kind: "Choose" }) => {
|
|
1339
|
-
const span = getExpressionSpan(file, expression);
|
|
1340
|
-
const location = getSpanLocation(file, span);
|
|
1341
|
-
return withContext(createContext(), () => {
|
|
1342
|
-
const pattern = expression.pattern;
|
|
1343
|
-
const names = pattern.kind === "Name" ? [pattern.name] : pattern.names;
|
|
1344
|
-
if (expression.set === undefined) {
|
|
1345
|
-
const indexes = findNameTokens(file, span.start, span.end, names);
|
|
1346
|
-
// SANY locates the symbol of CHOOSE x : P at the CHOOSE keyword.
|
|
1347
|
-
const params = names.map((name, index) =>
|
|
1348
|
-
createFormalParam(
|
|
1349
|
-
name,
|
|
1350
|
-
0,
|
|
1351
|
-
getTokenLocation(file, pattern.kind === "Name" ? span.start : indexes[index]),
|
|
1352
|
-
true,
|
|
1353
|
-
),
|
|
1354
|
-
);
|
|
1355
|
-
const body = generateWithParams(params, expression.body);
|
|
1356
|
-
return applyBuiltin("$UnboundedChoose", [body], location, { unbounded: params });
|
|
1357
|
-
}
|
|
1358
|
-
const range = generateExpression(expression.set);
|
|
1359
|
-
const setSpan = getExpressionSpan(file, expression.set);
|
|
1360
|
-
const indexes = findNameTokens(file, span.start, setSpan.start, names);
|
|
1361
|
-
const params = names.map((name, index) =>
|
|
1362
|
-
createFormalParam(name, 0, getTokenLocation(file, indexes[index]), true),
|
|
1363
|
-
);
|
|
1364
|
-
const body = generateWithParams(params, expression.body);
|
|
1365
|
-
return applyBuiltin("$BoundedChoose", [body], location, {
|
|
1366
|
-
bounded: [params],
|
|
1367
|
-
tupleOrs: [pattern.kind === "Tuple"],
|
|
1368
|
-
ranges: [range],
|
|
1369
|
-
});
|
|
1370
|
-
});
|
|
1371
|
-
};
|
|
1372
|
-
|
|
1373
|
-
// An infix, prefix or postfix operator that no symbol defines, with the token SANY names it by.
|
|
1374
|
-
const getUnresolvedOperator = (expression: Expression & { kind: "Apply" }) => {
|
|
1375
|
-
if (
|
|
1376
|
-
isIdentifier(expression.operator) ||
|
|
1377
|
-
isSelector(expression.operator) ||
|
|
1378
|
-
expression.args.length > 2
|
|
1379
|
-
) {
|
|
1380
|
-
return undefined;
|
|
1381
|
-
}
|
|
1382
|
-
if (resolveSymbol(symbolTable, resolveSynonym(expression.operator)) !== undefined) {
|
|
1383
|
-
return undefined;
|
|
1384
|
-
}
|
|
1385
|
-
const span = getExpressionSpan(file, expression);
|
|
1386
|
-
// SANY reports an operator applied like a function, as in op(a, b), as an unknown operator.
|
|
1387
|
-
if (
|
|
1388
|
-
file.tokens[span.start].text === expression.operator &&
|
|
1389
|
-
isToken(file, span.start + 1, "(")
|
|
1390
|
-
) {
|
|
1391
|
-
return undefined;
|
|
1392
|
-
}
|
|
1393
|
-
const firstSpan = getParenthesizedSpan(file, getExpressionSpan(file, expression.args[0]));
|
|
1394
|
-
const isPrefix = expression.args.length === 1 && firstSpan.start !== span.start;
|
|
1395
|
-
const index = isPrefix ? span.start : firstSpan.end + 1;
|
|
1396
|
-
const text = getTokenSource(file, index);
|
|
1397
|
-
return {
|
|
1398
|
-
index,
|
|
1399
|
-
name: isPrefix && text === "-" ? "-." : text,
|
|
1400
|
-
fixity: isPrefix ? "prefix" : expression.args.length === 2 ? "infix" : "postfix",
|
|
1401
|
-
};
|
|
1402
|
-
};
|
|
1403
|
-
|
|
1404
|
-
const generateExpression = (expression: Expression): ExprNode => {
|
|
1405
|
-
const location = getLocation(expression);
|
|
1406
|
-
switch (expression.kind) {
|
|
1407
|
-
case "Number":
|
|
1408
|
-
return {
|
|
1409
|
-
...createLevelFields(getId()),
|
|
1410
|
-
kind: "Numeral",
|
|
1411
|
-
value: getNumeralDigits(expression.text ?? String(expression.value)),
|
|
1412
|
-
location,
|
|
1413
|
-
};
|
|
1414
|
-
case "Decimal": {
|
|
1415
|
-
const [integralPart, fractionalPart] = expression.text.split(".");
|
|
1416
|
-
const mantissa = BigInt(integralPart + fractionalPart);
|
|
1417
|
-
const isLong = mantissa <= 9223372036854775807n;
|
|
1418
|
-
return {
|
|
1419
|
-
...createLevelFields(getId()),
|
|
1420
|
-
kind: "Decimal",
|
|
1421
|
-
mantissa: mantissa.toString(),
|
|
1422
|
-
exponent: isLong ? String(-fractionalPart.length) : String(fractionalPart.length),
|
|
1423
|
-
integralPart,
|
|
1424
|
-
fractionalPart,
|
|
1425
|
-
location,
|
|
1426
|
-
};
|
|
1427
|
-
}
|
|
1428
|
-
case "String":
|
|
1429
|
-
return createString(expression.value, location);
|
|
1430
|
-
case "Identifier": {
|
|
1431
|
-
const node = generateSelectorExpression(expression);
|
|
1432
|
-
checkRecursion(node);
|
|
368
|
+
assumeProveScopes: new WeakMap(),
|
|
369
|
+
labelGoals: new WeakMap(),
|
|
370
|
+
recursiveDeclarations: [],
|
|
371
|
+
selectorToNode: createSelectorResolver({
|
|
372
|
+
version: options.version,
|
|
373
|
+
resolveSymbol: (name) => resolveSymbol(contexts, name),
|
|
374
|
+
resolveSynonym,
|
|
375
|
+
generateExpression: (expression) => generateExpression(generation, expression),
|
|
376
|
+
generateArgument: (operator, index, argument, location) =>
|
|
377
|
+
generateArgument(generation, operator, index, argument, location),
|
|
378
|
+
createOpAppl: (operator, operands, location) => {
|
|
379
|
+
const node = createOpAppl(generation, operator, operands, location);
|
|
380
|
+
matchArguments(generation, operator, operands, location);
|
|
1433
381
|
return node;
|
|
1434
|
-
}
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
addError(
|
|
1443
|
-
location,
|
|
1444
|
-
`Couldn't resolve ${unresolved.fixity} operator symbol \`${unresolved.name}'.`,
|
|
1445
|
-
);
|
|
1446
|
-
return { ...createNullNode(), isAbsent: true };
|
|
1447
|
-
}
|
|
1448
|
-
const node = generateSelectorExpression(expression);
|
|
1449
|
-
if (isSelector(expression.operator) || isIdentifier(expression.operator)) {
|
|
1450
|
-
checkRecursion(node);
|
|
1451
|
-
}
|
|
1452
|
-
return node;
|
|
1453
|
-
}
|
|
1454
|
-
case "Bracket":
|
|
1455
|
-
addError(
|
|
1456
|
-
getTokenLocation(file, getExpressionSpan(file, expression).start),
|
|
1457
|
-
"Unsupported expression type `['.",
|
|
1458
|
-
);
|
|
1459
|
-
return { ...createNullNode(), isAbsent: true };
|
|
1460
|
-
case "OperatorReference":
|
|
1461
|
-
case "Lambda": {
|
|
1462
|
-
const text = file.tokens[getExpressionSpan(file, expression).start].text;
|
|
1463
|
-
addError(
|
|
1464
|
-
location,
|
|
1465
|
-
text === "LAMBDA"
|
|
1466
|
-
? "LAMBDA expression used where an expression is required."
|
|
1467
|
-
: `Unsupported expression type \`${getOperatorNodeKind(expression, text)}'.`,
|
|
1468
|
-
);
|
|
1469
|
-
return { ...createNullNode(), isAbsent: true };
|
|
1470
|
-
}
|
|
1471
|
-
case "And":
|
|
1472
|
-
case "Or":
|
|
1473
|
-
return generateJunction(expression);
|
|
1474
|
-
case "Prime":
|
|
1475
|
-
return applyBuiltin("'", [generateExpression(expression.expression)], location);
|
|
1476
|
-
case "Unchanged":
|
|
1477
|
-
return applyBuiltin("UNCHANGED", [generateExpression(expression.expression)], location);
|
|
1478
|
-
case "Enabled":
|
|
1479
|
-
return applyBuiltin("ENABLED", [generateExpression(expression.expression)], location);
|
|
1480
|
-
case "Always":
|
|
1481
|
-
return applyBuiltin("[]", [generateExpression(expression.expression)], location);
|
|
1482
|
-
case "Eventually":
|
|
1483
|
-
return applyBuiltin("<>", [generateExpression(expression.expression)], location);
|
|
1484
|
-
case "If":
|
|
1485
|
-
return applyBuiltin(
|
|
1486
|
-
"$IfThenElse",
|
|
1487
|
-
[
|
|
1488
|
-
generateExpression(expression.condition),
|
|
1489
|
-
generateExpression(expression.consequent),
|
|
1490
|
-
generateExpression(expression.alternative),
|
|
1491
|
-
],
|
|
1492
|
-
location,
|
|
1493
|
-
);
|
|
1494
|
-
case "Case":
|
|
1495
|
-
return generateCase(expression);
|
|
1496
|
-
case "Let":
|
|
1497
|
-
return generateLet(expression);
|
|
1498
|
-
case "Forall":
|
|
1499
|
-
case "Exists": {
|
|
1500
|
-
const span = getExpressionSpan(file, expression);
|
|
1501
|
-
return withContext(createContext(), () => {
|
|
1502
|
-
const bounds = generateBounds(expression.bounds, span.start);
|
|
1503
|
-
const body = generateWithParams(bounds.bounded.flat(), expression.body);
|
|
1504
|
-
return applyBuiltin(
|
|
1505
|
-
expression.kind === "Forall" ? "$BoundedForall" : "$BoundedExists",
|
|
1506
|
-
[body],
|
|
1507
|
-
location,
|
|
1508
|
-
bounds,
|
|
1509
|
-
);
|
|
1510
|
-
});
|
|
1511
|
-
}
|
|
1512
|
-
case "UnboundedQuantifier": {
|
|
1513
|
-
const span = getExpressionSpan(file, expression);
|
|
1514
|
-
const bodySpan = getExpressionSpan(file, expression.body);
|
|
1515
|
-
const indexes = findNameTokens(file, span.start, bodySpan.start, expression.names);
|
|
1516
|
-
const name = expression.isTemporal
|
|
1517
|
-
? expression.quantifier === "Forall"
|
|
1518
|
-
? "$TemporalForall"
|
|
1519
|
-
: "$TemporalExists"
|
|
1520
|
-
: expression.quantifier === "Forall"
|
|
1521
|
-
? "$UnboundedForall"
|
|
1522
|
-
: "$UnboundedExists";
|
|
1523
|
-
return withContext(createContext(), () => {
|
|
1524
|
-
const params = expression.names.map((paramName, index) =>
|
|
1525
|
-
createFormalParam(paramName, 0, getTokenLocation(file, indexes[index]), true),
|
|
1526
|
-
);
|
|
1527
|
-
const body = generateWithParams(params, expression.body);
|
|
1528
|
-
return applyBuiltin(name, [body], location, { unbounded: params });
|
|
1529
|
-
});
|
|
1530
|
-
}
|
|
1531
|
-
case "Choose":
|
|
1532
|
-
return generateChoose(expression);
|
|
1533
|
-
case "SetEnumeration":
|
|
1534
|
-
return applyBuiltin(
|
|
1535
|
-
"$SetEnumerate",
|
|
1536
|
-
expression.elements.map(generateExpression),
|
|
1537
|
-
location,
|
|
1538
|
-
);
|
|
1539
|
-
case "Tuple":
|
|
1540
|
-
return applyBuiltin("$Tuple", expression.elements.map(generateExpression), location);
|
|
1541
|
-
case "SetFilter": {
|
|
1542
|
-
const range = generateExpression(expression.bound.set);
|
|
1543
|
-
const span = getExpressionSpan(file, expression);
|
|
1544
|
-
return withContext(createContext(), () => {
|
|
1545
|
-
const bounds = generateBoundsWithRanges([expression.bound], span.start, [range]);
|
|
1546
|
-
const body = generateWithParams(bounds.bounded.flat(), expression.predicate);
|
|
1547
|
-
return applyBuiltin("$SubsetOf", [body], location, bounds);
|
|
1548
|
-
});
|
|
1549
|
-
}
|
|
1550
|
-
case "SetMap": {
|
|
1551
|
-
const expressionSpan = getExpressionSpan(file, expression.expression);
|
|
1552
|
-
return withContext(createContext(), () => {
|
|
1553
|
-
const bounds = generateBounds(expression.bounds, expressionSpan.end + 1);
|
|
1554
|
-
const body = generateWithParams(bounds.bounded.flat(), expression.expression);
|
|
1555
|
-
return applyBuiltin("$SetOfAll", [body], location, bounds);
|
|
1556
|
-
});
|
|
1557
|
-
}
|
|
1558
|
-
case "FunctionConstruction": {
|
|
1559
|
-
const span = getExpressionSpan(file, expression);
|
|
1560
|
-
return withContext(createContext(), () => {
|
|
1561
|
-
const bounds = generateBounds(expression.bounds, span.start);
|
|
1562
|
-
const body = generateWithParams(bounds.bounded.flat(), expression.body);
|
|
1563
|
-
return applyBuiltin("$FcnConstructor", [body], location, bounds);
|
|
1564
|
-
});
|
|
1565
|
-
}
|
|
1566
|
-
case "FunctionSet":
|
|
1567
|
-
return applyBuiltin(
|
|
1568
|
-
"$SetOfFcns",
|
|
1569
|
-
[generateExpression(expression.domain), generateExpression(expression.range)],
|
|
1570
|
-
location,
|
|
1571
|
-
);
|
|
1572
|
-
case "Record":
|
|
1573
|
-
return generateRecordFields("$RcdConstructor", expression);
|
|
1574
|
-
case "RecordSet":
|
|
1575
|
-
return generateRecordFields("$SetOfRcds", expression);
|
|
1576
|
-
case "Except":
|
|
1577
|
-
return generateExcept(expression);
|
|
1578
|
-
case "ExceptAt": {
|
|
1579
|
-
const except = exceptStack[exceptStack.length - 1];
|
|
1580
|
-
const component = exceptSpecStack[exceptSpecStack.length - 1];
|
|
1581
|
-
if (except === undefined || component === undefined) {
|
|
1582
|
-
addError(location, "@ used where its meaning is not defined.");
|
|
1583
|
-
return createNullNode();
|
|
1584
|
-
}
|
|
1585
|
-
return {
|
|
1586
|
-
...createLevelFields(getId()),
|
|
1587
|
-
kind: "At",
|
|
1588
|
-
except,
|
|
1589
|
-
component,
|
|
1590
|
-
location: component.location,
|
|
1591
|
-
};
|
|
1592
|
-
}
|
|
1593
|
-
case "FunctionApplication": {
|
|
1594
|
-
const functionNode = generateExpression(expression.function);
|
|
1595
|
-
// SANY leaves the whole application null, without generating its arguments.
|
|
1596
|
-
if (isAbsent(functionNode)) return functionNode;
|
|
1597
|
-
checkRecursion(functionNode);
|
|
1598
|
-
const argument =
|
|
1599
|
-
expression.args.length === 1
|
|
1600
|
-
? generateExpression(expression.args[0])
|
|
1601
|
-
: applyBuiltin("$Tuple", expression.args.map(generateExpression), location);
|
|
1602
|
-
return applyBuiltin("$FcnApply", [functionNode, argument], location);
|
|
1603
|
-
}
|
|
1604
|
-
case "FieldAccess": {
|
|
1605
|
-
const record = generateExpression(expression.record);
|
|
1606
|
-
const span = getExpressionSpan(file, expression);
|
|
1607
|
-
return applyBuiltin(
|
|
1608
|
-
"$RcdSelect",
|
|
1609
|
-
[record, createString(expression.field, getTokenLocation(file, span.end))],
|
|
1610
|
-
location,
|
|
1611
|
-
);
|
|
1612
|
-
}
|
|
1613
|
-
case "Product":
|
|
1614
|
-
return applyBuiltin(
|
|
1615
|
-
"$CartesianProd",
|
|
1616
|
-
expression.factors.map(generateExpression),
|
|
1617
|
-
location,
|
|
1618
|
-
);
|
|
1619
|
-
case "BoxAction":
|
|
1620
|
-
case "AngleAction":
|
|
1621
|
-
return applyBuiltin(
|
|
1622
|
-
expression.kind === "BoxAction" ? "$SquareAct" : "$AngleAct",
|
|
1623
|
-
[generateExpression(expression.action), generateExpression(expression.subscript)],
|
|
1624
|
-
location,
|
|
1625
|
-
);
|
|
1626
|
-
case "WeakFairness":
|
|
1627
|
-
case "StrongFairness":
|
|
1628
|
-
return applyBuiltin(
|
|
1629
|
-
expression.kind === "WeakFairness" ? "$WF" : "$SF",
|
|
1630
|
-
[generateExpression(expression.subscript), generateExpression(expression.action)],
|
|
1631
|
-
location,
|
|
1632
|
-
);
|
|
1633
|
-
case "Label":
|
|
1634
|
-
return generateLabel(expression);
|
|
1635
|
-
case "Subexpression":
|
|
1636
|
-
return generateSelectorExpression(expression);
|
|
1637
|
-
}
|
|
1638
|
-
};
|
|
1639
|
-
|
|
1640
|
-
const generateBoundsWithRanges = (
|
|
1641
|
-
bounds: Bound[],
|
|
1642
|
-
searchStart: number,
|
|
1643
|
-
ranges: ExprNode[],
|
|
1644
|
-
): BoundSymbols => {
|
|
1645
|
-
const groups = getBoundGroups(bounds);
|
|
1646
|
-
const cursor = { value: searchStart };
|
|
1647
|
-
const bounded = groups.map((group) => {
|
|
1648
|
-
const setSpan = getExpressionSpan(file, group[0].set);
|
|
1649
|
-
const names = group.flatMap((bound) =>
|
|
1650
|
-
bound.pattern.kind === "Name" ? [bound.pattern.name] : bound.pattern.names,
|
|
1651
|
-
);
|
|
1652
|
-
const indexes = findNameTokens(file, cursor.value, setSpan.start, names);
|
|
1653
|
-
cursor.value = setSpan.end + 1;
|
|
1654
|
-
return names.map((name, index) =>
|
|
1655
|
-
createFormalParam(name, 0, getTokenLocation(file, indexes[index]), true),
|
|
1656
|
-
);
|
|
1657
|
-
});
|
|
1658
|
-
return {
|
|
1659
|
-
bounded,
|
|
1660
|
-
tupleOrs: groups.map((group) => group[0].pattern.kind === "Tuple"),
|
|
1661
|
-
ranges,
|
|
1662
|
-
};
|
|
1663
|
-
};
|
|
1664
|
-
|
|
1665
|
-
const generateLabel = (expression: Expression & { kind: "Label" }): LabelNode | OpApplNode => {
|
|
1666
|
-
const location = getLocation(expression);
|
|
1667
|
-
const scope = labelScopes.at(-1);
|
|
1668
|
-
if (scope === undefined) {
|
|
1669
|
-
addError(location, "Label not in definition or proof step.");
|
|
1670
|
-
return createNullNode();
|
|
1671
|
-
}
|
|
1672
|
-
// Generator.noLabelsAllowed: no label lies in the scope of a declaration of a nested
|
|
1673
|
-
// ASSUME/PROVE.
|
|
1674
|
-
if (assumeProveState.isDeclared.slice(2, assumeProveState.depth + 1).some(Boolean)) {
|
|
1675
|
-
addError(location, "Label not allowed within scope of declaration in nested ASSUME/PROVE.");
|
|
1676
|
-
return createNullNode();
|
|
1677
|
-
}
|
|
1678
|
-
if (exceptStack.length > 0 && exceptSpecStack.length > 0) {
|
|
1679
|
-
addError(location, "Labels inside EXCEPT clauses are not yet implemented.");
|
|
1680
|
-
return createNullNode();
|
|
1681
|
-
}
|
|
1682
|
-
pushLabelScope();
|
|
1683
|
-
const body = generateExpression(expression.body);
|
|
1684
|
-
const labels = popLabels();
|
|
1685
|
-
const paramIndexes = findNameTokens(
|
|
1686
|
-
file,
|
|
1687
|
-
getExpressionSpan(file, expression).start,
|
|
1688
|
-
getExpressionSpan(file, expression.body).start,
|
|
1689
|
-
expression.params,
|
|
1690
|
-
);
|
|
1691
|
-
const params = expression.params.map((name, index) => {
|
|
1692
|
-
const symbol = resolveSymbol(symbolTable, name);
|
|
1693
|
-
if (symbol?.kind === "FormalParam") return symbol;
|
|
1694
|
-
const paramLocation = getTokenLocation(file, paramIndexes[index]);
|
|
1695
|
-
addError(paramLocation, `Illegal parameter ${name} of label \`${expression.name}'.`);
|
|
1696
|
-
return createFormalParam(name, 0, paramLocation, false);
|
|
1697
|
-
});
|
|
1698
|
-
const label: LabelNode = {
|
|
1699
|
-
...createLevelFields(getId()),
|
|
1700
|
-
kind: "Label",
|
|
1701
|
-
name: expression.name,
|
|
1702
|
-
params,
|
|
1703
|
-
body,
|
|
1704
|
-
labels,
|
|
1705
|
-
isAssumeProve: false,
|
|
1706
|
-
location,
|
|
1707
|
-
};
|
|
1708
|
-
// Generator.formalParamsEqual: a label names each parameter bound around it once.
|
|
1709
|
-
const named = new Set<FormalParamNode>();
|
|
1710
|
-
params.forEach((param) => {
|
|
1711
|
-
if (named.has(param)) {
|
|
1712
|
-
addError(
|
|
1713
|
-
location,
|
|
1714
|
-
`Repeated formal parameter ${param.name} \nin label \`${label.name}'.`,
|
|
1715
|
-
);
|
|
1716
|
-
}
|
|
1717
|
-
named.add(param);
|
|
1718
|
-
});
|
|
1719
|
-
scope.params.flat().forEach((param) => {
|
|
1720
|
-
if (!named.delete(param)) {
|
|
1721
|
-
addError(location, `Label ${label.name} must contain formal parameter \`${param.name}'.`);
|
|
1722
|
-
}
|
|
1723
|
-
});
|
|
1724
|
-
if (named.size > 0) {
|
|
1725
|
-
const extras = [...named].map((param) => `${param.name} `).join("");
|
|
1726
|
-
addError(location, `Label ${label.name} declares extra parameter(s) ${extras}`);
|
|
1727
|
-
}
|
|
1728
|
-
if (scope.labels.has(label.name)) addError(location, `Duplicate label \`${label.name}'.`);
|
|
1729
|
-
else scope.labels.set(label.name, label);
|
|
1730
|
-
const goal = assumeProveState.goal;
|
|
1731
|
-
if (goal) labelGoals.set(label, { scope: goal, clause: assumeProveState.goalClause });
|
|
1732
|
-
return label;
|
|
1733
|
-
};
|
|
1734
|
-
|
|
1735
|
-
// The parameters of a definition's left-hand side, located as SANY locates them: a name, or a
|
|
1736
|
-
// name with its underscores like Op(_, _).
|
|
1737
|
-
const getParams = (
|
|
1738
|
-
definition: OperatorDefinition | Instance,
|
|
1739
|
-
span: TokenSpan,
|
|
1740
|
-
isAdded: boolean,
|
|
1741
|
-
) => {
|
|
1742
|
-
const cursor = { value: span.start };
|
|
1743
|
-
return definition.params.map((param) => {
|
|
1744
|
-
const index = param.location
|
|
1745
|
-
? getTokenIndex(file, param.location)
|
|
1746
|
-
: findNameOrKeyword(cursor.value, param.name);
|
|
1747
|
-
const paramSpan = getOperatorSpan(index, param.arity);
|
|
1748
|
-
cursor.value = paramSpan.end + 1;
|
|
1749
|
-
return createFormalParam(
|
|
1750
|
-
resolveSynonym(param.name),
|
|
1751
|
-
param.arity,
|
|
1752
|
-
getSpanLocation(file, paramSpan),
|
|
1753
|
-
isAdded,
|
|
1754
|
-
);
|
|
1755
|
-
});
|
|
1756
|
-
};
|
|
1757
|
-
|
|
1758
|
-
// The span of a definition or instance, with the LOCAL before it.
|
|
1759
|
-
const getDefinitionSpan = (definition: { location: SourceLocation; isLocal?: boolean }) => {
|
|
1760
|
-
const span = getUnitSpan(definition.location);
|
|
1761
|
-
return definition.isLocal &&
|
|
1762
|
-
!isToken(file, span.start, "LOCAL") &&
|
|
1763
|
-
isToken(file, span.start - 1, "LOCAL")
|
|
1764
|
-
? { start: span.start - 1, end: span.end }
|
|
1765
|
-
: span;
|
|
1766
|
-
};
|
|
1767
|
-
|
|
1768
|
-
const processOperator = (
|
|
1769
|
-
definition: OperatorDefinition,
|
|
1770
|
-
definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
|
|
1771
|
-
) => {
|
|
1772
|
-
const span = getDefinitionSpan(definition);
|
|
1773
|
-
const location = getSpanLocation(file, span);
|
|
1774
|
-
const name = resolveSynonym(definition.name);
|
|
1775
|
-
const paramsSpan = { start: definition.isLocal ? span.start + 1 : span.start, end: span.end };
|
|
1776
|
-
const generated = withContext(createContext(), () => {
|
|
1777
|
-
const params = getParams(definition, paramsSpan, true);
|
|
1778
|
-
const existing = resolveSymbol(symbolTable, name);
|
|
1779
|
-
if (existing?.kind === "OpDef" && existing.inRecursive && !existing.isDefined) {
|
|
1780
|
-
// SANY compares only the arity of the last parameter.
|
|
1781
|
-
const isMatching =
|
|
1782
|
-
existing.params?.length === params.length &&
|
|
1783
|
-
(params.length === 0 || params[params.length - 1].arity === 0);
|
|
1784
|
-
if (existing.letInLevel !== state.curLevel) {
|
|
1785
|
-
addError(location, `Recursive operator ${name} defined at wrong LET/IN level.`);
|
|
1786
|
-
} else {
|
|
1787
|
-
if (!isMatching) {
|
|
1788
|
-
addError(
|
|
1789
|
-
location,
|
|
1790
|
-
`Definition of ${existing.name} has different arity than its RECURSIVE declaration.`,
|
|
1791
|
-
);
|
|
1792
|
-
}
|
|
1793
|
-
// SANY sets the parameters before it analyzes the body.
|
|
1794
|
-
existing.params = params;
|
|
1795
|
-
}
|
|
1796
|
-
} else if (existing !== undefined) {
|
|
1797
|
-
addError(location, `Operator ${name} already defined or declared.`);
|
|
1798
|
-
}
|
|
1799
|
-
pushLabelScope();
|
|
1800
|
-
return { params, body: generateExpression(definition.body), existing };
|
|
1801
|
-
});
|
|
1802
|
-
const labels = popLabels();
|
|
1803
|
-
const existing = generated.existing;
|
|
1804
|
-
const params = generated.params;
|
|
1805
|
-
const preComments = getPreComments(file, span.start);
|
|
1806
|
-
if (
|
|
1807
|
-
existing?.kind === "OpDef" &&
|
|
1808
|
-
existing.inRecursive &&
|
|
1809
|
-
!existing.isDefined &&
|
|
1810
|
-
existing.letInLevel === state.curLevel
|
|
1811
|
-
) {
|
|
1812
|
-
existing.weights = params.map(() => 0);
|
|
1813
|
-
existing.maxLevels = params.map(() => TEMPORAL_LEVEL);
|
|
1814
|
-
existing.isLeibnizArg = params.map(() => true);
|
|
1815
|
-
existing.labels = labels;
|
|
1816
|
-
endOpDefNode(existing, generated.body, location, preComments);
|
|
1817
|
-
currentModule.definitions.push(existing);
|
|
1818
|
-
definitions?.push(existing);
|
|
1819
|
-
return;
|
|
1820
|
-
}
|
|
1821
|
-
const node = createOpDef({
|
|
1822
|
-
name,
|
|
1823
|
-
params,
|
|
1824
|
-
body: generated.body,
|
|
1825
|
-
isLocal: definition.isLocal,
|
|
1826
|
-
location,
|
|
1827
|
-
preComments,
|
|
1828
|
-
labels,
|
|
1829
|
-
});
|
|
1830
|
-
addSymbol(symbolTable, node);
|
|
1831
|
-
setRecursionFields(node);
|
|
1832
|
-
currentModule.definitions.push(node);
|
|
1833
|
-
definitions?.push(node);
|
|
1834
|
-
};
|
|
1835
|
-
|
|
1836
|
-
const endOpDefNode = (
|
|
1837
|
-
node: OpDefNode,
|
|
1838
|
-
body: ExprNode,
|
|
1839
|
-
location: SanyLocation,
|
|
1840
|
-
preComments: string[],
|
|
1841
|
-
) => {
|
|
1842
|
-
node.isDefined = true;
|
|
1843
|
-
node.body = body;
|
|
1844
|
-
node.location = location;
|
|
1845
|
-
node.preComments = preComments;
|
|
1846
|
-
if (node.inRecursive) {
|
|
1847
|
-
state.unresolvedCount[state.curLevel]--;
|
|
1848
|
-
state.unresolvedSum--;
|
|
1849
|
-
}
|
|
1850
|
-
};
|
|
1851
|
-
|
|
1852
|
-
const processFunction = (
|
|
1853
|
-
definition: FunctionDefinition,
|
|
1854
|
-
definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
|
|
1855
|
-
) => {
|
|
1856
|
-
const span = getDefinitionSpan(definition);
|
|
1857
|
-
const location = getSpanLocation(file, span);
|
|
1858
|
-
const context = createContext();
|
|
1859
|
-
const nameIndex = definition.isLocal ? span.start + 1 : span.start;
|
|
1860
|
-
const bounds = withContext(context, () => generateBounds(definition.bounds, nameIndex + 1));
|
|
1861
|
-
const existing = resolveSymbol(symbolTable, definition.name);
|
|
1862
|
-
const recursionParam = withContext(context, () =>
|
|
1863
|
-
createFormalParam(definition.name, 0, location, existing === undefined),
|
|
1864
|
-
);
|
|
1865
|
-
const node = applyBuiltin("$NonRecursiveFcnSpec", [], location, {
|
|
1866
|
-
unbounded: [recursionParam],
|
|
1867
|
-
...bounds,
|
|
1868
|
-
});
|
|
1869
|
-
const isPendingRecursive =
|
|
1870
|
-
existing?.kind === "OpDef" && existing.inRecursive && !existing.isDefined;
|
|
1871
|
-
const isRecursive = isPendingRecursive && existing.letInLevel === state.curLevel;
|
|
1872
|
-
if (isRecursive) {
|
|
1873
|
-
if (existing.arity !== 0) {
|
|
1874
|
-
addError(
|
|
1875
|
-
location,
|
|
1876
|
-
`Function ${existing.name} has operator arguments in its RECURSIVE declaration.`,
|
|
1877
|
-
);
|
|
1878
|
-
}
|
|
1879
|
-
} else if (isPendingRecursive) {
|
|
1880
|
-
addError(location, `Recursive function ${definition.name} defined at wrong LET/IN level.`);
|
|
1881
|
-
} else if (existing !== undefined) {
|
|
1882
|
-
addError(location, `Function name \`${definition.name}' already defined or declared.`);
|
|
1883
|
-
}
|
|
1884
|
-
const preComments = getPreComments(file, span.start);
|
|
1885
|
-
// Generator.processFunction's OpDefNode: a new one, or the one the name already has, though
|
|
1886
|
-
// defined, unless that is declared RECURSIVE at another level.
|
|
1887
|
-
const definitionNode =
|
|
1888
|
-
existing === undefined
|
|
1889
|
-
? createOpDef({
|
|
1890
|
-
name: definition.name,
|
|
1891
|
-
params: [],
|
|
1892
|
-
body: node,
|
|
1893
|
-
isLocal: definition.isLocal,
|
|
1894
|
-
location,
|
|
1895
|
-
preComments,
|
|
1896
|
-
})
|
|
1897
|
-
: existing.kind === "OpDef" && (isRecursive || !isPendingRecursive)
|
|
1898
|
-
? existing
|
|
1899
|
-
: undefined;
|
|
1900
|
-
// A function whose RECURSIVE declaration has parameters stays undefined.
|
|
1901
|
-
if (isRecursive) {
|
|
1902
|
-
if (existing.arity === 0) endOpDefNode(existing, node, location, preComments);
|
|
1903
|
-
} else if (existing === undefined && definitionNode !== undefined) {
|
|
1904
|
-
addSymbol(symbolTable, definitionNode);
|
|
1905
|
-
setRecursionFields(definitionNode);
|
|
1906
|
-
}
|
|
1907
|
-
if (definitionNode !== undefined) {
|
|
1908
|
-
currentModule.definitions.push(definitionNode);
|
|
1909
|
-
definitions?.push(definitionNode);
|
|
1910
|
-
}
|
|
1911
|
-
functions.unshift({ name: definition.name, node });
|
|
1912
|
-
pushLabelScope();
|
|
1913
|
-
// The bound identifiers are bound in the body only of a function with an OpDefNode.
|
|
1914
|
-
const generateBody = () => generateWithParams(bounds.bounded.flat(), definition.body);
|
|
1915
|
-
const body =
|
|
1916
|
-
definitionNode === undefined ? generateBody() : withContext(context, generateBody);
|
|
1917
|
-
const labels = popLabels();
|
|
1918
|
-
if (definitionNode !== undefined) definitionNode.labels = labels;
|
|
1919
|
-
functions.shift();
|
|
1920
|
-
node.operands = [body];
|
|
1921
|
-
if (node.operator.name === "$NonRecursiveFcnSpec") node.unbounded = undefined;
|
|
1922
|
-
};
|
|
1923
|
-
|
|
1924
|
-
const recursiveDeclarations: OpDefNode[] = [];
|
|
1925
|
-
const processRecursive = (declarations: Declaration[]) => {
|
|
1926
|
-
if (state.unresolvedSum === 0) state.recursiveSectionCount++;
|
|
1927
|
-
state.unresolvedCount[state.curLevel] += declarations.length;
|
|
1928
|
-
state.unresolvedSum += declarations.length;
|
|
1929
|
-
declarations.forEach((declaration) => {
|
|
1930
|
-
const span = getDeclarationSpan(declaration);
|
|
1931
|
-
// The OpDeclNode buildParameter makes of the declaration to read its name and arity.
|
|
1932
|
-
getId();
|
|
1933
|
-
const params = Array.from({ length: declaration.arity }, () =>
|
|
1934
|
-
createFormalParam("", 0, undefined, false),
|
|
1935
|
-
);
|
|
1936
|
-
const node = createOpDef({
|
|
1937
|
-
name: resolveSynonym(declaration.name),
|
|
1938
|
-
params,
|
|
1939
|
-
location: getSpanLocation(file, span),
|
|
1940
|
-
isDefined: false,
|
|
1941
|
-
inRecursive: true,
|
|
1942
|
-
inRecursiveSection: true,
|
|
1943
|
-
recursiveSection: state.recursiveSectionCount,
|
|
1944
|
-
letInLevel: state.curLevel,
|
|
1945
|
-
});
|
|
1946
|
-
addSymbol(symbolTable, node);
|
|
1947
|
-
recursiveDeclarations.push(node);
|
|
1948
|
-
currentModule.opDefsInRecursiveSection.push(node);
|
|
1949
|
-
});
|
|
1950
|
-
};
|
|
1951
|
-
|
|
1952
|
-
const checkForUndefinedRecursiveOps = () => {
|
|
1953
|
-
if (state.unresolvedCount[state.curLevel] === 0) return;
|
|
1954
|
-
recursiveDeclarations
|
|
1955
|
-
.filter((node) => node.letInLevel === state.curLevel && !node.isDefined)
|
|
1956
|
-
.forEach((node) =>
|
|
1957
|
-
addError(
|
|
1958
|
-
node.location,
|
|
1959
|
-
`Symbol ${node.name} declared in RECURSIVE statement but not defined.`,
|
|
1960
|
-
),
|
|
1961
|
-
);
|
|
1962
|
-
state.unresolvedSum -= state.unresolvedCount[state.curLevel];
|
|
1963
|
-
};
|
|
1964
|
-
|
|
1965
|
-
// SubstInNode's constructor and processSubst: an implicit substitution for each declaration of
|
|
1966
|
-
// the instantiated module whose name the instantiating module can resolve, in the order of the
|
|
1967
|
-
// instantiated module's Hashtable, then the explicit WITH substitutions.
|
|
1968
|
-
const processSubstitutions = (
|
|
1969
|
-
instance: Instance,
|
|
1970
|
-
instanceeContext: Context,
|
|
1971
|
-
location: SanyLocation,
|
|
1972
|
-
) => {
|
|
1973
|
-
// The SubstInNode processSubst creates to hold the substitutions, which only their
|
|
1974
|
-
// templates use.
|
|
1975
|
-
getId();
|
|
1976
|
-
const declarations = getHashtableValues(instanceeContext.table).filter(
|
|
1977
|
-
(symbol): symbol is OpDeclNode => symbol.kind === "OpDecl",
|
|
1978
|
-
);
|
|
1979
|
-
const substs: Subst[] = declarations.flatMap((declaration) => {
|
|
1980
|
-
const symbol = resolveSymbol(symbolTable, declaration.name);
|
|
1981
|
-
if (symbol === undefined) return [];
|
|
1982
|
-
const isExpression =
|
|
1983
|
-
declaration.declKind === VARIABLE_DECL_KIND ||
|
|
1984
|
-
(declaration.declKind === CONSTANT_DECL_KIND && declaration.arity === 0);
|
|
1985
|
-
return [
|
|
1986
|
-
{
|
|
1987
|
-
op: declaration,
|
|
1988
|
-
expr: isExpression ? createOpAppl(symbol, [], location) : createOpArg(symbol, location),
|
|
1989
|
-
isImplicit: true,
|
|
1990
|
-
},
|
|
1991
|
-
];
|
|
1992
|
-
});
|
|
1993
|
-
// Generator.checkSubstitutionForCorrectArity, which SANY locates in its builtins for the
|
|
1994
|
-
// nullOpArg it substitutes after an error.
|
|
1995
|
-
const checkArity = (target: OpDeclNode, substitute: ExprOrOpArgNode) => {
|
|
1996
|
-
const arity = substitute.kind === "OpArg" ? getSymbolArity(substitute.op) : 0;
|
|
1997
|
-
const isNullOpArg =
|
|
1998
|
-
target.arity > 0 && substitute.kind === "OpAppl" && substitute.isNull === true;
|
|
1999
|
-
if (arity !== target.arity && !isNullOpArg) {
|
|
2000
|
-
addError(
|
|
2001
|
-
substitute.location,
|
|
2002
|
-
`An operator must be substituted for symbol '${target.name}', and it must have arity ${target.arity}.`,
|
|
2003
|
-
);
|
|
2004
|
-
}
|
|
2005
|
-
};
|
|
2006
|
-
const span = getDefinitionSpan(instance);
|
|
2007
|
-
// The targets follow WITH, after the parameters of a definition that instantiates.
|
|
2008
|
-
const cursor = { value: findToken(file, span.start, span.end, "WITH") + 1 };
|
|
2009
|
-
instance.substitutions.forEach((substitution) => {
|
|
2010
|
-
const targetIndex = findToken(file, cursor.value, span.end, substitution.name);
|
|
2011
|
-
// The next target follows this substitution's expression, which may name it too.
|
|
2012
|
-
if (targetIndex !== -1) {
|
|
2013
|
-
cursor.value = getExpressionSpan(file, substitution.expression).end + 1;
|
|
2014
|
-
}
|
|
2015
|
-
const target = getContextSymbol(instanceeContext, resolveSynonym(substitution.name));
|
|
2016
|
-
if (target?.kind !== "OpDecl") {
|
|
2017
|
-
addError(
|
|
2018
|
-
targetIndex === -1 ? location : getTokenLocation(file, targetIndex),
|
|
2019
|
-
`Identifier '${targetIndex === -1 ? substitution.name : getTokenSource(file, targetIndex)}' 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.)`,
|
|
2020
|
-
);
|
|
2021
|
-
return;
|
|
2022
|
-
}
|
|
2023
|
-
const substitute =
|
|
2024
|
-
target.arity === 0
|
|
2025
|
-
? generateExpression(substitution.expression)
|
|
2026
|
-
: generateOperatorArgument(substitution.expression, target);
|
|
2027
|
-
checkArity(target, substitute);
|
|
2028
|
-
const existing = substs.find((subst) => subst.op.name === target.name);
|
|
2029
|
-
if (existing && !existing.isImplicit) {
|
|
2030
|
-
addError(
|
|
2031
|
-
getLocation(substitution.expression),
|
|
2032
|
-
`Multiple substitutions for symbol '${target.name}' in substitution.`,
|
|
2033
|
-
);
|
|
2034
|
-
} else if (existing) {
|
|
2035
|
-
existing.expr = substitute;
|
|
2036
|
-
existing.isImplicit = false;
|
|
2037
|
-
} else substs.push({ op: target, expr: substitute, isImplicit: false });
|
|
2038
|
-
});
|
|
2039
|
-
// TLC 1.7.4's SANY checks the arity of only the substitutions that the INSTANCE writes.
|
|
2040
|
-
substs
|
|
2041
|
-
.filter((subst) => subst.isImplicit && options.version !== "1.7.4")
|
|
2042
|
-
.forEach((subst) => checkArity(subst.op, subst.expr));
|
|
2043
|
-
// SubstInNode.matchAll.
|
|
2044
|
-
declarations
|
|
2045
|
-
.filter((declaration) => !substs.some((subst) => subst.op.name === declaration.name))
|
|
2046
|
-
.forEach((declaration) =>
|
|
2047
|
-
addError(
|
|
2048
|
-
location,
|
|
2049
|
-
`Substitution missing for symbol ${declaration.name} declared at ${formatSanyLocation(declaration.location)} \nand instantiated in module ${currentModule.name}.`,
|
|
2050
|
-
),
|
|
2051
|
-
);
|
|
2052
|
-
return substs;
|
|
2053
|
-
};
|
|
2054
|
-
|
|
2055
|
-
const createSubstIn = (
|
|
2056
|
-
kind: "SubstIn" | "APSubstIn",
|
|
2057
|
-
substs: Subst[],
|
|
2058
|
-
body: LevelNode,
|
|
2059
|
-
instantiatedModule: ModuleNode,
|
|
2060
|
-
location: SanyLocation,
|
|
2061
|
-
instantiatingModule = currentModule,
|
|
2062
|
-
): SubstInNode => {
|
|
2063
|
-
if (isAbsent(body)) {
|
|
2064
|
-
addError(
|
|
2065
|
-
location,
|
|
2066
|
-
kind === "SubstIn"
|
|
2067
|
-
? "Substitution error, probably due to error \nin module being instantiated."
|
|
2068
|
-
: "Substitution error, probably due to error in module being instantiated.",
|
|
2069
|
-
);
|
|
2070
|
-
}
|
|
2071
|
-
return {
|
|
2072
|
-
...createLevelFields(getId()),
|
|
2073
|
-
kind,
|
|
2074
|
-
substs,
|
|
2075
|
-
body,
|
|
2076
|
-
instantiatingModule,
|
|
2077
|
-
instantiatedModule,
|
|
2078
|
-
location,
|
|
2079
|
-
};
|
|
2080
|
-
};
|
|
2081
|
-
|
|
2082
|
-
const processModuleDefinition = (
|
|
2083
|
-
instance: Instance,
|
|
2084
|
-
definitions: (OpDefNode | ThmOrAssumpDefNode)[] | undefined,
|
|
2085
|
-
instances: InstanceNode[] | undefined,
|
|
2086
|
-
) => {
|
|
2087
|
-
const span = getDefinitionSpan(instance);
|
|
2088
|
-
const location = getSpanLocation(file, span);
|
|
2089
|
-
const preComments = getPreComments(file, span.start);
|
|
2090
|
-
const name = instance.name ?? "";
|
|
2091
|
-
const parameterContext = createContext();
|
|
2092
|
-
const nameIndex = instance.isLocal ? span.start + 1 : span.start;
|
|
2093
|
-
if (instance.params.length > 0) symbolTable.contexts.push(parameterContext);
|
|
2094
|
-
const args = getParams(
|
|
2095
|
-
instance,
|
|
2096
|
-
{ start: nameIndex, end: span.end },
|
|
2097
|
-
instance.params.length > 0,
|
|
2098
|
-
);
|
|
2099
|
-
const instanceeModule = getModule(instance.module);
|
|
2100
|
-
const instanceeContext = instanceeModule.context;
|
|
2101
|
-
const substs = processSubstitutions(instance, instanceeContext, location);
|
|
2102
|
-
if (instance.params.length > 0) symbolTable.contexts.pop();
|
|
2103
|
-
getHashtableValues(instanceeContext.table).forEach((symbol) => {
|
|
2104
|
-
if (symbol.kind !== "OpDef" || symbol.isLocal) return;
|
|
2105
|
-
if (symbol.defKind !== "UserDefined" && symbol.defKind !== "ModuleInstance") return;
|
|
2106
|
-
const params = [...args, ...(symbol.params ?? [])];
|
|
2107
|
-
const qualifiedName = `${name}!${symbol.name}`;
|
|
2108
|
-
const node =
|
|
2109
|
-
symbol.defKind === "UserDefined"
|
|
2110
|
-
? createOpDef({
|
|
2111
|
-
name: qualifiedName,
|
|
2112
|
-
params,
|
|
2113
|
-
body:
|
|
2114
|
-
substs.length > 0 && symbol.body
|
|
2115
|
-
? createSubstIn("SubstIn", substs, symbol.body, instanceeModule, location)
|
|
2116
|
-
: symbol.body,
|
|
2117
|
-
isLocal: instance.isLocal,
|
|
2118
|
-
location,
|
|
2119
|
-
source: symbol.source ?? symbol,
|
|
2120
|
-
preComments,
|
|
2121
|
-
labels: symbol.labels,
|
|
2122
|
-
})
|
|
2123
|
-
: createOpDef({
|
|
2124
|
-
defKind: "ModuleInstance",
|
|
2125
|
-
name: qualifiedName,
|
|
2126
|
-
params,
|
|
2127
|
-
isLocal: instance.isLocal,
|
|
2128
|
-
location,
|
|
2129
|
-
module: symbol.module,
|
|
2130
|
-
source: symbol.source ?? symbol,
|
|
2131
|
-
preComments,
|
|
2132
|
-
});
|
|
2133
|
-
addSymbol(symbolTable, node);
|
|
2134
|
-
if (symbol.defKind === "UserDefined") setRecursionFields(node);
|
|
2135
|
-
(definitions ?? currentModule.definitions).push(node);
|
|
2136
|
-
});
|
|
2137
|
-
getHashtableValues(instanceeContext.table).forEach((symbol) => {
|
|
2138
|
-
if (symbol.kind !== "ThmOrAssumpDef" || symbol.isLocal) return;
|
|
2139
|
-
// SANY creates the APSubstInNode before the ThmOrAssumpDefNode that holds it.
|
|
2140
|
-
const body =
|
|
2141
|
-
substs.length > 0
|
|
2142
|
-
? createSubstIn("APSubstIn", substs, symbol.body, instanceeModule, location)
|
|
2143
|
-
: symbol.body;
|
|
2144
|
-
const node: ThmOrAssumpDefNode = {
|
|
2145
|
-
...createLevelFields(getId()),
|
|
2146
|
-
kind: "ThmOrAssumpDef",
|
|
2147
|
-
name: `${name}!${symbol.name}`,
|
|
2148
|
-
isTheorem: symbol.isTheorem,
|
|
2149
|
-
body,
|
|
2150
|
-
params: [...args, ...symbol.params],
|
|
2151
|
-
isLocal: instance.isLocal,
|
|
2152
|
-
location,
|
|
2153
|
-
module: currentModule,
|
|
2154
|
-
source: symbol.source ?? symbol,
|
|
2155
|
-
labels: symbol.labels,
|
|
2156
|
-
isLeibnizArg: [],
|
|
2157
|
-
isLeibniz: false,
|
|
2158
|
-
opLevelCond: [],
|
|
2159
|
-
maxLevels: [],
|
|
2160
|
-
minMaxLevel: [],
|
|
2161
|
-
};
|
|
2162
|
-
addSymbol(symbolTable, node);
|
|
2163
|
-
(definitions ?? currentModule.definitions).push(node);
|
|
2164
|
-
});
|
|
2165
|
-
const instanceNode: InstanceNode = {
|
|
2166
|
-
...createLevelFields(getId()),
|
|
2167
|
-
kind: "Instance",
|
|
2168
|
-
name,
|
|
2169
|
-
isLocal: instance.isLocal,
|
|
2170
|
-
params: args,
|
|
2171
|
-
module: instanceeModule,
|
|
2172
|
-
substs,
|
|
2173
|
-
location,
|
|
2174
|
-
};
|
|
2175
|
-
(instances ?? currentModule.topLevel).push(instanceNode);
|
|
2176
|
-
const moduleDefinition = createOpDef({
|
|
2177
|
-
defKind: "ModuleInstance",
|
|
2178
|
-
name,
|
|
2179
|
-
params: args,
|
|
2180
|
-
isLocal: instance.isLocal,
|
|
2181
|
-
location,
|
|
2182
|
-
preComments,
|
|
2183
|
-
});
|
|
2184
|
-
addSymbol(symbolTable, moduleDefinition);
|
|
2185
|
-
// TLC 1.7.4's SANY leaves the definition out of the definitions of a LET.
|
|
2186
|
-
if (options.version !== "1.7.4") definitions?.push(moduleDefinition);
|
|
2187
|
-
};
|
|
2188
|
-
|
|
2189
|
-
// generateInstance: an INSTANCE step of a proof is located at the step's body and adds no
|
|
2190
|
-
// theorem definitions or instance to the module.
|
|
2191
|
-
const generateInstance = (
|
|
2192
|
-
instance: Instance,
|
|
2193
|
-
isTopLevel = true,
|
|
2194
|
-
stepLocation?: SanyLocation,
|
|
2195
|
-
) => {
|
|
2196
|
-
const span = getDefinitionSpan(instance);
|
|
2197
|
-
const location = stepLocation ?? getSpanLocation(file, span);
|
|
2198
|
-
const preComments = getPreComments(file, span.start);
|
|
2199
|
-
const instanceeModule = getModule(instance.module);
|
|
2200
|
-
const instanceeContext = instanceeModule.context;
|
|
2201
|
-
const substs = processSubstitutions(instance, instanceeContext, location);
|
|
2202
|
-
const isLocal = instance.isLocal;
|
|
2203
|
-
const isInstanceeParameterFree = isParameterFree(instanceeModule);
|
|
2204
|
-
getHashtableValues(instanceeContext.table).forEach((symbol) => {
|
|
2205
|
-
if (symbol.kind !== "OpDef" || symbol.defKind !== "UserDefined" || symbol.isLocal) return;
|
|
2206
|
-
const copyFrom = (module: ModuleNode | undefined, body: ExprNode | undefined) => {
|
|
2207
|
-
const node = createOpDef({
|
|
2208
|
-
name: symbol.name,
|
|
2209
|
-
params: symbol.params,
|
|
382
|
+
},
|
|
383
|
+
createOpArg: (operator, location) => createOpArg(generation, operator, location),
|
|
384
|
+
createFormalParam: (name, arity, location) =>
|
|
385
|
+
createFormalParam(generation, name, arity, location, false),
|
|
386
|
+
createLambda: (params, body, location) =>
|
|
387
|
+
createOpDef(generation, {
|
|
388
|
+
name: "LAMBDA",
|
|
389
|
+
params,
|
|
2210
390
|
body,
|
|
2211
|
-
isLocal,
|
|
2212
391
|
location,
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
labels: symbol.labels,
|
|
2217
|
-
});
|
|
2218
|
-
addSymbol(symbolTable, node);
|
|
2219
|
-
return node;
|
|
2220
|
-
};
|
|
2221
|
-
const reuse = () => {
|
|
2222
|
-
addSymbol(symbolTable, symbol);
|
|
2223
|
-
return symbol;
|
|
2224
|
-
};
|
|
2225
|
-
const isOriginParameterFree = symbol.module === undefined || isParameterFree(symbol.module);
|
|
2226
|
-
const node =
|
|
2227
|
-
isInstanceeParameterFree || isOriginParameterFree
|
|
2228
|
-
? isLocal
|
|
2229
|
-
? copyFrom(symbol.module, symbol.body)
|
|
2230
|
-
: reuse()
|
|
2231
|
-
: copyFrom(
|
|
2232
|
-
currentModule,
|
|
2233
|
-
symbol.body &&
|
|
2234
|
-
createSubstIn("SubstIn", substs, symbol.body, instanceeModule, location),
|
|
2235
|
-
);
|
|
2236
|
-
currentModule.definitions.push(node);
|
|
2237
|
-
setRecursionFields(node);
|
|
2238
|
-
});
|
|
2239
|
-
// SANY reuses a theorem of a parameter-free module without adding it to the symbol table
|
|
2240
|
-
// when the instantiated module has parameters.
|
|
2241
|
-
getHashtableValues(instanceeContext.table).forEach((symbol) => {
|
|
2242
|
-
if (symbol.kind !== "ThmOrAssumpDef" || symbol.isLocal) return;
|
|
2243
|
-
const copyFrom = (module: ModuleNode, body: LevelNode) => {
|
|
2244
|
-
const node: ThmOrAssumpDefNode = {
|
|
2245
|
-
...symbol,
|
|
2246
|
-
...createLevelFields(getId()),
|
|
2247
|
-
body,
|
|
2248
|
-
isLocal,
|
|
2249
|
-
location,
|
|
2250
|
-
module,
|
|
2251
|
-
source: symbol.source ?? symbol,
|
|
2252
|
-
};
|
|
2253
|
-
addSymbol(symbolTable, node);
|
|
2254
|
-
return node;
|
|
2255
|
-
};
|
|
2256
|
-
const reuse = () => {
|
|
2257
|
-
addSymbol(symbolTable, symbol);
|
|
2258
|
-
return symbol;
|
|
2259
|
-
};
|
|
2260
|
-
const node = !isInstanceeParameterFree
|
|
2261
|
-
? isParameterFree(symbol.module)
|
|
2262
|
-
? isLocal
|
|
2263
|
-
? copyFrom(currentModule, symbol.body)
|
|
2264
|
-
: symbol
|
|
2265
|
-
: copyFrom(
|
|
2266
|
-
currentModule,
|
|
2267
|
-
createSubstIn("APSubstIn", substs, symbol.body, instanceeModule, location),
|
|
2268
|
-
)
|
|
2269
|
-
: isLocal
|
|
2270
|
-
? copyFrom(symbol.module, symbol.body)
|
|
2271
|
-
: reuse();
|
|
2272
|
-
if (isTopLevel) currentModule.definitions.push(node);
|
|
2273
|
-
});
|
|
2274
|
-
const instanceNode: InstanceNode = {
|
|
2275
|
-
...createLevelFields(getId()),
|
|
2276
|
-
kind: "Instance",
|
|
2277
|
-
name: undefined,
|
|
2278
|
-
isLocal,
|
|
2279
|
-
params: [],
|
|
2280
|
-
module: instanceeModule,
|
|
2281
|
-
substs,
|
|
2282
|
-
location,
|
|
2283
|
-
};
|
|
2284
|
-
if (isTopLevel) currentModule.topLevel.push(instanceNode);
|
|
2285
|
-
return instanceNode;
|
|
2286
|
-
};
|
|
2287
|
-
|
|
2288
|
-
// SANY leaves out a module it can't resolve, after reporting it at the module's name.
|
|
2289
|
-
const getFactModules = (fact: Fact & { kind: "ModuleFact" }) => {
|
|
2290
|
-
const module =
|
|
2291
|
-
resolveModule(fact.name) ?? (fact.name === currentModule.name ? currentModule : undefined);
|
|
2292
|
-
if (module !== undefined) return [module];
|
|
2293
|
-
addError(
|
|
2294
|
-
getTokenLocation(file, getTokenIndex(file, fact.location) + 1),
|
|
2295
|
-
`Module \`${fact.name}' used without being extended or instantiated.`,
|
|
2296
|
-
);
|
|
2297
|
-
return [];
|
|
2298
|
-
};
|
|
2299
|
-
|
|
2300
|
-
// A fact is a module, or an expression such as the name of a theorem; a DEF names a defined
|
|
2301
|
-
// operator, theorem or assumption, or a module.
|
|
2302
|
-
const generateFacts = (facts: Fact[]) =>
|
|
2303
|
-
facts.flatMap((fact): FactNode[] => {
|
|
2304
|
-
if (fact.kind === "ModuleFact") return getFactModules(fact);
|
|
2305
|
-
const items = getSelectorItems(fact);
|
|
2306
|
-
if (items === undefined || !isGeneralId(items)) return [generateExpression(fact)];
|
|
2307
|
-
const node = generateSelector(fact, 0, true, false);
|
|
2308
|
-
if (node.kind === "ThmOrAssumpDef") throw new TlaSyntaxError("Expected a fact");
|
|
2309
|
-
return [node];
|
|
2310
|
-
});
|
|
2311
|
-
|
|
2312
|
-
const isParenthesized = (span: TokenSpan) =>
|
|
2313
|
-
isToken(file, span.start, "(") && getMatchingIndex(file, span.start) === span.end;
|
|
2314
|
-
const getOperandSpan = (expression: Expression) =>
|
|
2315
|
-
getParenthesizedSpan(file, getExpressionSpan(file, expression));
|
|
2316
|
-
// How SANY's parser shapes an application: as a prefix, infix or postfix operator's, or a name's.
|
|
2317
|
-
const getApplicationKind = (expression: Expression & { kind: "Apply" }, span: TokenSpan) => {
|
|
2318
|
-
if (getOperandSpan(expression.args[0]).start === span.start) {
|
|
2319
|
-
return expression.args.length === 2 ? "N_InfixExpr" : "N_PostfixExpr";
|
|
2320
|
-
}
|
|
2321
|
-
return file.tokens[span.start].kind === "Identifier" ? "N_OpApplication" : "N_PrefixExpr";
|
|
2322
|
-
};
|
|
2323
|
-
// The kind of SANY's syntax node for an expression, or for a token its text.
|
|
2324
|
-
const getSyntaxKind = (expression: Expression, span: TokenSpan) => {
|
|
2325
|
-
if (isParenthesized(span)) return "N_ParenExpr";
|
|
2326
|
-
if (expression.kind === "String") return getTokenSource(file, span.start);
|
|
2327
|
-
if (expression.kind === "Apply") return getApplicationKind(expression, span);
|
|
2328
|
-
return SYNTAX_KINDS.get(expression.kind);
|
|
2329
|
-
};
|
|
2330
|
-
// Whether the first child of an expression's syntax node is a node without children, which
|
|
2331
|
-
// Generator.genIdToSelector takes for an empty prefix of a name. A string is a token itself.
|
|
2332
|
-
const hasChildlessFirstChild = (expression: Expression, span: TokenSpan) => {
|
|
2333
|
-
if (expression.kind === "String" && !isParenthesized(span)) return false;
|
|
2334
|
-
if (expression.kind === "Identifier" && !expression.name.includes("!")) return true;
|
|
2335
|
-
const kind = getSyntaxKind(expression, span);
|
|
2336
|
-
return kind === undefined || NESTED_FIRST_CHILD_KINDS.has(kind) ? undefined : true;
|
|
2337
|
-
};
|
|
2338
|
-
// Generator.genIdToSelector reads the syntax node of a DEF clause entry that isn't a name as
|
|
2339
|
-
// a name: its first child as the name's prefix and its second as the name's last part.
|
|
2340
|
-
const getDefEntryParts = (
|
|
2341
|
-
expression: Expression,
|
|
2342
|
-
span: TokenSpan,
|
|
2343
|
-
): DefEntryParts | undefined => {
|
|
2344
|
-
const getPart = (operand: Expression, partSpan = getOperandSpan(operand)) => ({
|
|
2345
|
-
last: partSpan,
|
|
2346
|
-
lastKind: getSyntaxKind(operand, partSpan),
|
|
2347
|
-
});
|
|
2348
|
-
if (isParenthesized(span)) {
|
|
2349
|
-
const inner = { start: span.start + 1, end: span.end - 1 };
|
|
2350
|
-
return { hasChildlessFirstChild: false, ...getPart(expression, inner) };
|
|
2351
|
-
}
|
|
2352
|
-
switch (expression.kind) {
|
|
2353
|
-
case "If":
|
|
2354
|
-
case "BoxAction":
|
|
2355
|
-
case "AngleAction":
|
|
2356
|
-
return {
|
|
2357
|
-
hasChildlessFirstChild: false,
|
|
2358
|
-
...getPart(expression.kind === "If" ? expression.condition : expression.action),
|
|
2359
|
-
};
|
|
2360
|
-
case "Tuple":
|
|
2361
|
-
case "SetEnumeration": {
|
|
2362
|
-
const first = expression.elements[0];
|
|
2363
|
-
return first === undefined
|
|
2364
|
-
? {
|
|
2365
|
-
hasChildlessFirstChild: false,
|
|
2366
|
-
last: { start: span.end, end: span.end },
|
|
2367
|
-
lastKind: file.tokens[span.end].text,
|
|
2368
|
-
}
|
|
2369
|
-
: { hasChildlessFirstChild: false, ...getPart(first) };
|
|
2370
|
-
}
|
|
2371
|
-
case "Always":
|
|
2372
|
-
case "Eventually":
|
|
2373
|
-
case "Enabled":
|
|
2374
|
-
case "Unchanged":
|
|
2375
|
-
return {
|
|
2376
|
-
hasChildlessFirstChild: true,
|
|
2377
|
-
...getPart(expression.expression, { start: span.start + 1, end: span.end }),
|
|
2378
|
-
};
|
|
2379
|
-
case "Prime":
|
|
2380
|
-
return {
|
|
2381
|
-
hasChildlessFirstChild: hasChildlessFirstChild(
|
|
2382
|
-
expression.expression,
|
|
2383
|
-
getOperandSpan(expression.expression),
|
|
2384
|
-
),
|
|
2385
|
-
last: { start: span.end, end: span.end },
|
|
2386
|
-
lastKind: "N_GenPostfixOp",
|
|
2387
|
-
};
|
|
2388
|
-
case "FunctionApplication":
|
|
2389
|
-
case "FieldAccess": {
|
|
2390
|
-
const operand =
|
|
2391
|
-
expression.kind === "FunctionApplication" ? expression.function : expression.record;
|
|
2392
|
-
const operandSpan = getOperandSpan(operand);
|
|
2393
|
-
const next = operandSpan.end + 1;
|
|
2394
|
-
return {
|
|
2395
|
-
hasChildlessFirstChild: hasChildlessFirstChild(operand, operandSpan),
|
|
2396
|
-
last: { start: next, end: next },
|
|
2397
|
-
lastKind: file.tokens[next].text,
|
|
2398
|
-
};
|
|
2399
|
-
}
|
|
2400
|
-
case "Apply": {
|
|
2401
|
-
const kind = getApplicationKind(expression, span);
|
|
2402
|
-
if (kind === "N_OpApplication") {
|
|
2403
|
-
return expression.operator.includes("!")
|
|
2404
|
-
? undefined
|
|
2405
|
-
: {
|
|
2406
|
-
hasChildlessFirstChild: true,
|
|
2407
|
-
last: { start: span.start + 1, end: span.end },
|
|
2408
|
-
lastKind: "N_OpArgs",
|
|
2409
|
-
};
|
|
2410
|
-
}
|
|
2411
|
-
if (kind === "N_PrefixExpr") {
|
|
2412
|
-
return {
|
|
2413
|
-
hasChildlessFirstChild: true,
|
|
2414
|
-
...getPart(expression.args[0], { start: span.start + 1, end: span.end }),
|
|
2415
|
-
};
|
|
2416
|
-
}
|
|
2417
|
-
const left = getOperandSpan(expression.args[0]);
|
|
2418
|
-
const operator = left.end + 1;
|
|
2419
|
-
return {
|
|
2420
|
-
hasChildlessFirstChild: hasChildlessFirstChild(expression.args[0], left),
|
|
2421
|
-
last: { start: operator, end: operator },
|
|
2422
|
-
lastKind: kind === "N_InfixExpr" ? "N_GenInfixOp" : "N_GenPostfixOp",
|
|
2423
|
-
};
|
|
2424
|
-
}
|
|
2425
|
-
}
|
|
2426
|
-
return undefined;
|
|
2427
|
-
};
|
|
2428
|
-
// The errors of Generator.genIdToSelector and selectorToNode for a DEF clause entry that isn't
|
|
2429
|
-
// a name, or false where SANY's reading of the entry isn't modeled.
|
|
2430
|
-
const reportDefEntry = (expression: Expression) => {
|
|
2431
|
-
const span = getParenthesizedSpan(file, getExpressionSpan(file, expression));
|
|
2432
|
-
const isName =
|
|
2433
|
-
expression.kind === "Identifier" ||
|
|
2434
|
-
expression.kind === "Subexpression" ||
|
|
2435
|
-
expression.kind === "OperatorReference";
|
|
2436
|
-
if (isName && !isParenthesized(span)) return false;
|
|
2437
|
-
const parts = getDefEntryParts(expression, span);
|
|
2438
|
-
if (parts?.hasChildlessFirstChild === undefined || parts.lastKind === undefined) return false;
|
|
2439
|
-
const lastLocation = getSpanLocation(file, parts.last);
|
|
2440
|
-
if (parts.hasChildlessFirstChild) {
|
|
2441
|
-
addError(getSpanLocation(file, span), "Was expecting a GeneralId.");
|
|
2442
|
-
}
|
|
2443
|
-
if (parts.lastKind === "N_OpArgs") {
|
|
2444
|
-
addError(lastLocation, "Selector `!(...)' should not have argument(s).");
|
|
2445
|
-
} else {
|
|
2446
|
-
addError(lastLocation, "Unexpected token found.");
|
|
2447
|
-
addError(lastLocation, `Need name or step number here, not \`${parts.lastKind}'.`);
|
|
2448
|
-
}
|
|
2449
|
-
addError(getSpanLocation(file, span), "DEF clause entry should describe a defined operator.");
|
|
2450
|
-
return true;
|
|
2451
|
-
};
|
|
2452
|
-
|
|
2453
|
-
const generateDefs = (defs: Fact[]) =>
|
|
2454
|
-
defs.flatMap((fact): UseOrHideNode["defs"] => {
|
|
2455
|
-
if (fact.kind === "ModuleFact") return getFactModules(fact);
|
|
2456
|
-
if (reportDefEntry(fact)) return [];
|
|
2457
|
-
const symbol = generateSelector(
|
|
2458
|
-
fact.kind === "OperatorReference"
|
|
2459
|
-
? { kind: "Identifier", name: fact.operator, location: fact.location }
|
|
2460
|
-
: fact,
|
|
2461
|
-
-1,
|
|
2462
|
-
false,
|
|
2463
|
-
true,
|
|
2464
|
-
);
|
|
2465
|
-
// SANY rejects the name of a proof step in a DEF clause, and leaves out an entry it rejects.
|
|
2466
|
-
// Errors.addMessage drops the message when selectorToNode already reported it.
|
|
2467
|
-
const isDefinition =
|
|
2468
|
-
symbol.kind === "OpDef" ||
|
|
2469
|
-
(symbol.kind === "ThmOrAssumpDef" && !symbol.name.startsWith("<"));
|
|
2470
|
-
if (!isDefinition) {
|
|
2471
|
-
addError(getLocation(fact), "DEF clause entry should describe a defined operator.");
|
|
2472
|
-
return [];
|
|
2473
|
-
}
|
|
2474
|
-
return [symbol];
|
|
2475
|
-
});
|
|
2476
|
-
|
|
2477
|
-
// SANY's generateUseOrHide rejects a BY, USE, or HIDE with nothing after its keyword before it
|
|
2478
|
-
// reads the facts.
|
|
2479
|
-
const checkKeywordOnly = (
|
|
2480
|
-
statement: UseOrHide | Extract<Proof, { kind: "By" }>,
|
|
2481
|
-
location: SanyLocation,
|
|
2482
|
-
) => {
|
|
2483
|
-
if (statement.isOnly || statement.facts.length + statement.defs.length > 0) return;
|
|
2484
|
-
addError(location, "Empty BY, USE, or HIDE");
|
|
2485
|
-
};
|
|
2486
|
-
|
|
2487
|
-
const generateUseOrHide = (use: UseOrHide): UseOrHideNode => {
|
|
2488
|
-
const location = getSpanLocation(file, getUnitSpan(use.location));
|
|
2489
|
-
checkKeywordOnly(use, location);
|
|
2490
|
-
const facts = generateFacts(use.facts);
|
|
2491
|
-
const defs = generateDefs(use.defs);
|
|
2492
|
-
if (facts.length + defs.length === 0) addError(location, "Empty USE or HIDE statement.");
|
|
2493
|
-
// UseOrHideNode.factCheck, after SANY generates the node: a HIDE cites facts by name only.
|
|
2494
|
-
if (use.kind === "Hide") {
|
|
2495
|
-
facts
|
|
2496
|
-
.filter((fact) => fact.kind === "OpAppl" && fact.operator.kind !== "ThmOrAssumpDef")
|
|
2497
|
-
.forEach((fact) =>
|
|
2498
|
-
addError(
|
|
2499
|
-
fact.location,
|
|
2500
|
-
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
392
|
+
preComments: getPreComments(
|
|
393
|
+
file,
|
|
394
|
+
getTokenIndex(file, { line: location.beginLine, column: location.beginColumn }),
|
|
2501
395
|
),
|
|
396
|
+
}),
|
|
397
|
+
copySubstIn: (prefix, body) =>
|
|
398
|
+
createSubstIn(
|
|
399
|
+
generation,
|
|
400
|
+
"SubstIn",
|
|
401
|
+
prefix.substs,
|
|
402
|
+
body,
|
|
403
|
+
prefix.instantiatedModule,
|
|
404
|
+
prefix.location,
|
|
405
|
+
prefix.instantiatingModule,
|
|
406
|
+
),
|
|
407
|
+
getBuiltin: (name) => getBuiltin(generation, name),
|
|
408
|
+
addError: (location, text) => {
|
|
409
|
+
addError(location, text);
|
|
410
|
+
return createNullNode(generation);
|
|
411
|
+
},
|
|
412
|
+
isOutOfLabelScope: (label) => {
|
|
413
|
+
const goal = generation.labelGoals.get(label);
|
|
414
|
+
return (
|
|
415
|
+
goal !== undefined &&
|
|
416
|
+
goal.scope.isDeclaredBefore[goal.clause] &&
|
|
417
|
+
goal.scope.isSuffices === goal.scope.isInProof
|
|
2502
418
|
);
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
isHide: use.kind === "Hide",
|
|
2511
|
-
location,
|
|
2512
|
-
};
|
|
2513
|
-
};
|
|
2514
|
-
|
|
2515
|
-
// generateNewSymb: the set is generated before the symbol is declared.
|
|
2516
|
-
const generateNewSymbol = (symbol: NewSymbol): NewSymbNode => {
|
|
2517
|
-
const span = getUnitSpan(symbol.location);
|
|
2518
|
-
const set = symbol.set && generateExpression(symbol.set);
|
|
2519
|
-
const nameIndex = findNameOrKeyword(span.start, symbol.name);
|
|
2520
|
-
const kind = NEW_SYMBOL_KINDS[symbol.level];
|
|
2521
|
-
const declaration: OpDeclNode = {
|
|
2522
|
-
...createLevelFields(getId()),
|
|
2523
|
-
levelChecked: 1,
|
|
2524
|
-
level: kind.level,
|
|
2525
|
-
kind: "OpDecl",
|
|
2526
|
-
name: resolveSynonym(symbol.name),
|
|
2527
|
-
arity: symbol.arity,
|
|
2528
|
-
declKind: kind.declKind,
|
|
2529
|
-
location: getSpanLocation(file, getOperatorSpan(nameIndex, symbol.arity)),
|
|
2530
|
-
module: currentModule,
|
|
2531
|
-
};
|
|
2532
|
-
addSymbol(symbolTable, declaration);
|
|
2533
|
-
return {
|
|
2534
|
-
...createLevelFields(getId()),
|
|
2535
|
-
kind: "NewSymb",
|
|
2536
|
-
declaration,
|
|
2537
|
-
set,
|
|
2538
|
-
location: getSpanLocation(file, span),
|
|
2539
|
-
};
|
|
2540
|
-
};
|
|
2541
|
-
|
|
2542
|
-
// generateAssumeProve: an ASSUME/PROVE nested in another one has a context of its own.
|
|
2543
|
-
const generateAssumeProve = (
|
|
2544
|
-
assumeProve: AssumeProve,
|
|
2545
|
-
depth: number,
|
|
2546
|
-
goal?: { isSuffices: boolean },
|
|
2547
|
-
): AssumeProveNode => {
|
|
2548
|
-
const span = getUnitSpan(assumeProve.location);
|
|
2549
|
-
const isBoxed = isBoxToken(file, span.start - 1);
|
|
2550
|
-
const conclusionStart = getExpressionSpan(file, assumeProve.conclusion).start;
|
|
2551
|
-
const proveIndex = file.tokens.findLastIndex(
|
|
2552
|
-
(token, index) =>
|
|
2553
|
-
index < conclusionStart && token.kind === "Keyword" && token.text === "PROVE",
|
|
2554
|
-
);
|
|
2555
|
-
const assumeLocation = getSpanLocation(file, {
|
|
2556
|
-
start: isBoxed ? span.start - 1 : span.start,
|
|
2557
|
-
end: span.start,
|
|
2558
|
-
});
|
|
2559
|
-
if (isBoxed !== isBoxToken(file, proveIndex - 1)) {
|
|
2560
|
-
addError(
|
|
2561
|
-
assumeLocation,
|
|
2562
|
-
isBoxed
|
|
2563
|
-
? "[]ASSUME matched by PROVE instead of []PROVE"
|
|
2564
|
-
: "ASSUME matched by []PROVE instead of PROVE",
|
|
2565
|
-
);
|
|
2566
|
-
}
|
|
2567
|
-
// SANY creates the AssumeProveNode, numbering it, before it generates its clauses.
|
|
2568
|
-
const id = getId();
|
|
2569
|
-
if (depth > 1) symbolTable.contexts.push(createContext());
|
|
2570
|
-
if (isBoxed && symbolTable.contexts.some((context) => assumeContexts.has(context))) {
|
|
2571
|
-
addError(
|
|
2572
|
-
assumeLocation,
|
|
2573
|
-
"[]ASSUME used within the scope of an ordinary ASSUME's assumptions",
|
|
2574
|
-
);
|
|
2575
|
-
}
|
|
2576
|
-
if (!isBoxed) assumeContexts.add(getTopContext(symbolTable));
|
|
2577
|
-
const scope: AssumeProveScope = {
|
|
2578
|
-
isDeclaredBefore: [false],
|
|
2579
|
-
hasGoal: goal !== undefined,
|
|
2580
|
-
isSuffices: goal?.isSuffices ?? false,
|
|
2581
|
-
isInProof: true,
|
|
2582
|
-
};
|
|
2583
|
-
assumeProveState.depth = depth;
|
|
2584
|
-
assumeProveState.isDeclared[depth] = false;
|
|
2585
|
-
if (depth === 1) {
|
|
2586
|
-
assumeProveState.goal = goal && scope;
|
|
2587
|
-
assumeProveState.goalClause = 0;
|
|
2588
|
-
}
|
|
2589
|
-
const assumptions = assumeProve.assumptions.map((assumption, index): LevelNode => {
|
|
2590
|
-
const isNew = assumption.kind === "NewSymbol";
|
|
2591
|
-
const node =
|
|
2592
|
-
assumption.kind === "AssumeProve"
|
|
2593
|
-
? generateAssumeProve(assumption, depth + 1)
|
|
2594
|
-
: isNew
|
|
2595
|
-
? generateNewSymbol(assumption)
|
|
2596
|
-
: generateExpression(assumption);
|
|
2597
|
-
assumeProveState.depth = depth;
|
|
2598
|
-
if (isNew) assumeProveState.isDeclared[depth] = true;
|
|
2599
|
-
scope.isDeclaredBefore.push(scope.isDeclaredBefore[index] || isNew);
|
|
2600
|
-
if (depth === 1) assumeProveState.goalClause++;
|
|
2601
|
-
return node;
|
|
2602
|
-
});
|
|
2603
|
-
const conclusion = generateExpression(assumeProve.conclusion);
|
|
2604
|
-
if (depth > 1) symbolTable.contexts.pop();
|
|
2605
|
-
assumeProveState.depth = depth - 1;
|
|
2606
|
-
if (depth === 1) assumeProveState.goal = undefined;
|
|
2607
|
-
const node: AssumeProveNode = {
|
|
2608
|
-
...createLevelFields(id),
|
|
2609
|
-
kind: "AssumeProve",
|
|
2610
|
-
assumptions,
|
|
2611
|
-
goal: conclusion,
|
|
2612
|
-
isSuffices: scope.isSuffices,
|
|
2613
|
-
isBoxed,
|
|
2614
|
-
location: getSpanLocation(file, {
|
|
2615
|
-
start: isBoxed ? span.start - 1 : span.start,
|
|
2616
|
-
end: span.end,
|
|
2617
|
-
}),
|
|
2618
|
-
};
|
|
2619
|
-
assumeProveScopes.set(node, scope);
|
|
2620
|
-
return node;
|
|
2621
|
-
};
|
|
2622
|
-
|
|
2623
|
-
// SANY creates the ThmOrAssumpDefNode of a named theorem or proof step, numbering it, before it
|
|
2624
|
-
// generates what it asserts.
|
|
2625
|
-
const createTheoremDefinition = (
|
|
2626
|
-
id: number,
|
|
2627
|
-
name: string,
|
|
2628
|
-
body: LevelNode,
|
|
2629
|
-
location: SanyLocation,
|
|
2630
|
-
labels: Map<string, LabelNode>,
|
|
2631
|
-
isTheorem = true,
|
|
2632
|
-
): ThmOrAssumpDefNode => ({
|
|
2633
|
-
...createLevelFields(id),
|
|
2634
|
-
kind: "ThmOrAssumpDef",
|
|
2635
|
-
name,
|
|
2636
|
-
isTheorem,
|
|
2637
|
-
body,
|
|
2638
|
-
params: [],
|
|
2639
|
-
isLocal: false,
|
|
2640
|
-
location,
|
|
2641
|
-
module: currentModule,
|
|
2642
|
-
source: undefined,
|
|
2643
|
-
labels,
|
|
2644
|
-
isLeibnizArg: [],
|
|
2645
|
-
isLeibniz: false,
|
|
2646
|
-
opLevelCond: [],
|
|
2647
|
-
maxLevels: [],
|
|
2648
|
-
minMaxLevel: [],
|
|
2649
|
-
});
|
|
2650
|
-
|
|
2651
|
-
const generateLeafProof = (proof: Exclude<Proof, { kind: "Steps" }>): LeafProofNode => {
|
|
2652
|
-
const location = getSpanLocation(file, getUnitSpan(proof.location));
|
|
2653
|
-
if (proof.kind === "By") checkKeywordOnly(proof, location);
|
|
2654
|
-
const facts = proof.kind === "By" ? generateFacts(proof.facts) : [];
|
|
2655
|
-
const defs = proof.kind === "By" ? generateDefs(proof.defs) : [];
|
|
2656
|
-
// SANY reads a BY proof's facts and definitions from the UseOrHideNode it makes of them.
|
|
2657
|
-
if (proof.kind === "By") getId();
|
|
2658
|
-
if (proof.kind === "By" && facts.length + defs.length === 0) addError(location, "Empty BY");
|
|
2659
|
-
return {
|
|
2660
|
-
...createLevelFields(getId()),
|
|
2661
|
-
kind: "LeafProof",
|
|
2662
|
-
facts,
|
|
2663
|
-
defs,
|
|
2664
|
-
isOmitted: proof.kind === "Omitted",
|
|
2665
|
-
isOnly: proof.kind === "By" && proof.isOnly,
|
|
2666
|
-
location,
|
|
2667
|
-
};
|
|
2668
|
-
};
|
|
2669
|
-
|
|
2670
|
-
const generateStepBody = (step: ProofStep, bodySpan: TokenSpan) => {
|
|
2671
|
-
const body = step.body;
|
|
2672
|
-
const bodyLocation = getSpanLocation(file, bodySpan);
|
|
2673
|
-
switch (body.kind) {
|
|
2674
|
-
case "Have":
|
|
2675
|
-
return {
|
|
2676
|
-
node: applyBuiltin("$Have", [generateExpression(body.expression)], bodyLocation),
|
|
2677
|
-
isPick: false,
|
|
2678
|
-
};
|
|
2679
|
-
case "Case":
|
|
2680
|
-
return {
|
|
2681
|
-
node: applyBuiltin("$Pfcase", [generateExpression(body.expression)], bodyLocation),
|
|
2682
|
-
isPick: false,
|
|
2683
|
-
};
|
|
2684
|
-
case "Witness":
|
|
2685
|
-
return {
|
|
2686
|
-
node: applyBuiltin("$Witness", body.expressions.map(generateExpression), bodyLocation),
|
|
2687
|
-
isPick: false,
|
|
2688
|
-
};
|
|
2689
|
-
case "Qed":
|
|
2690
|
-
return { node: applyBuiltin("$Qed", [], bodyLocation), isPick: false };
|
|
2691
|
-
case "Take":
|
|
2692
|
-
case "Pick": {
|
|
2693
|
-
const isPick = body.kind === "Pick";
|
|
2694
|
-
const name = isPick ? "$Pick" : "$Take";
|
|
2695
|
-
if (isPick) symbolTable.contexts.push(createContext());
|
|
2696
|
-
const getOperands = (params: FormalParamNode[]) =>
|
|
2697
|
-
body.kind === "Pick" ? [generateWithParams(params, body.body)] : [];
|
|
2698
|
-
if (body.bounds.length > 0) {
|
|
2699
|
-
const bounds = generateBounds(body.bounds, bodySpan.start);
|
|
2700
|
-
const operands = getOperands(bounds.bounded.flat());
|
|
2701
|
-
return { node: applyBuiltin(name, operands, bodyLocation, bounds), isPick };
|
|
2702
|
-
}
|
|
2703
|
-
const indexes = findNameTokens(file, bodySpan.start, bodySpan.end + 1, body.names);
|
|
2704
|
-
const unbounded = body.names.map((paramName, index) =>
|
|
2705
|
-
createFormalParam(paramName, 0, getTokenLocation(file, indexes[index]), true),
|
|
419
|
+
},
|
|
420
|
+
isOutOfClauseScope: (assumeProve, position) => {
|
|
421
|
+
const scope = generation.assumeProveScopes.get(assumeProve);
|
|
422
|
+
return (
|
|
423
|
+
scope !== undefined &&
|
|
424
|
+
scope.isDeclaredBefore[position - 1] &&
|
|
425
|
+
(!scope.hasGoal || scope.isSuffices === scope.isInProof)
|
|
2706
426
|
);
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
}
|
|
2710
|
-
}
|
|
2711
|
-
return undefined;
|
|
2712
|
-
};
|
|
2713
|
-
|
|
2714
|
-
// An expression SANY parses as N_InfixExpr: a binary operator between its operands.
|
|
2715
|
-
const isInfixExpression = (expression: Expression) => {
|
|
2716
|
-
if ((expression.kind === "And" || expression.kind === "Or") && !expression.isBulleted)
|
|
2717
|
-
return true;
|
|
2718
|
-
if (expression.kind !== "Apply" || expression.args.length !== 2) return false;
|
|
2719
|
-
const left = getParenthesizedSpan(file, getExpressionSpan(file, expression.args[0]));
|
|
2720
|
-
const start = getExpressionSpan(file, expression).start;
|
|
2721
|
-
return left.start === start && !isToken(file, left.end + 1, "(");
|
|
2722
|
-
};
|
|
2723
|
-
|
|
2724
|
-
// generateProof: a proof's steps share a context, into which the step names, the symbols of
|
|
2725
|
-
// a TAKE or PICK, and the NEW symbols of a SUFFICES ASSUME go.
|
|
2726
|
-
const generateProof = (proof: Proof): LeafProofNode | NonLeafProofNode => {
|
|
2727
|
-
if (proof.kind !== "Steps") return generateLeafProof(proof);
|
|
2728
|
-
const proofContext = createContext();
|
|
2729
|
-
symbolTable.contexts.push(proofContext);
|
|
2730
|
-
const instances: InstanceNode[] = [];
|
|
2731
|
-
const sufficesContexts: Context[] = [];
|
|
2732
|
-
// An assertion step @ = e continues the infix assertion of the step before it, whose right
|
|
2733
|
-
// side the @ stands for.
|
|
2734
|
-
const infixStep: { previousRight: ExprOrOpArgNode | undefined } = {
|
|
2735
|
-
previousRight: undefined,
|
|
2736
|
-
};
|
|
2737
|
-
// A DEFINE, USE, HIDE or INSTANCE step.
|
|
2738
|
-
const generateOtherStep = (
|
|
2739
|
-
body: ProofStep["body"],
|
|
2740
|
-
stepSpan: TokenSpan,
|
|
2741
|
-
bodySpan: TokenSpan,
|
|
2742
|
-
): DefStepNode | UseOrHideNode | InstanceNode | undefined => {
|
|
2743
|
-
if (body.kind === "Define") {
|
|
2744
|
-
const defs = body.definitions.map((definition) => {
|
|
2745
|
-
const defined: (OpDefNode | ThmOrAssumpDefNode)[] = [];
|
|
2746
|
-
if (definition.kind === "Instance")
|
|
2747
|
-
processModuleDefinition(definition, defined, instances);
|
|
2748
|
-
else if (definition.kind === "Function") processFunction(definition, defined);
|
|
2749
|
-
else processOperator(definition, defined);
|
|
2750
|
-
const last = defined[definition.kind === "Instance" ? defined.length - 1 : 0];
|
|
2751
|
-
if (last?.kind !== "OpDef")
|
|
2752
|
-
throw new TlaSyntaxError("A DEFINE step defines no operator");
|
|
2753
|
-
return last;
|
|
2754
|
-
});
|
|
2755
|
-
return {
|
|
2756
|
-
...createLevelFields(getId()),
|
|
2757
|
-
kind: "DefStep",
|
|
2758
|
-
defs,
|
|
2759
|
-
location: getSpanLocation(file, bodySpan),
|
|
2760
|
-
};
|
|
2761
|
-
}
|
|
2762
|
-
if (body.kind === "Use" || body.kind === "Hide") {
|
|
2763
|
-
return { ...generateUseOrHide(body), location: getSpanLocation(file, stepSpan) };
|
|
2764
|
-
}
|
|
2765
|
-
if (body.kind === "Instance") {
|
|
2766
|
-
return generateInstance(body, false, getSpanLocation(file, bodySpan));
|
|
2767
|
-
}
|
|
2768
|
-
return undefined;
|
|
2769
|
-
};
|
|
2770
|
-
const steps = proof.steps.map((step): ProofStepNode => {
|
|
2771
|
-
const stepSpan = getUnitSpan(step.location);
|
|
2772
|
-
const headSpan = getRangeSpan(file, step.head);
|
|
2773
|
-
const bodySpan = { start: headSpan.start + 1, end: headSpan.end };
|
|
2774
|
-
const body = step.body;
|
|
2775
|
-
if (step.name !== undefined) pushLabelScope();
|
|
2776
|
-
const definitionId = step.name === undefined ? undefined : getId();
|
|
2777
|
-
if (body.kind !== "Assert") infixStep.previousRight = undefined;
|
|
2778
|
-
const otherStep = generateOtherStep(body, stepSpan, bodySpan);
|
|
2779
|
-
if (otherStep !== undefined) {
|
|
2780
|
-
// SANY puts a NumberedProofStep definition for a named step that asserts nothing in the
|
|
2781
|
-
// symbol table, for a DEF clause to name.
|
|
2782
|
-
if (step.name !== undefined) {
|
|
2783
|
-
addSymbol(
|
|
2784
|
-
symbolTable,
|
|
2785
|
-
createOpDef({
|
|
2786
|
-
defKind: "NumberedProofStep",
|
|
2787
|
-
name: step.name,
|
|
2788
|
-
location: getSpanLocation(file, stepSpan),
|
|
2789
|
-
labels: popLabels(),
|
|
2790
|
-
step: otherStep,
|
|
2791
|
-
}),
|
|
2792
|
-
);
|
|
2793
|
-
}
|
|
2794
|
-
return otherStep;
|
|
2795
|
-
}
|
|
2796
|
-
const isAssumeProve = body.kind === "Assert" && body.statement.kind === "AssumeProve";
|
|
2797
|
-
if (isAssumeProve) infixStep.previousRight = undefined;
|
|
2798
|
-
const isSuffices = body.kind === "Assert" && body.isSuffices;
|
|
2799
|
-
const assumeContext = createContext();
|
|
2800
|
-
const generateAssertion = (statement: Expression | AssumeProve): LevelNode => {
|
|
2801
|
-
if (statement.kind === "AssumeProve") {
|
|
2802
|
-
const node = withContext(assumeContext, () =>
|
|
2803
|
-
generateAssumeProve(
|
|
2804
|
-
statement,
|
|
2805
|
-
1,
|
|
2806
|
-
step.name === undefined ? undefined : { isSuffices },
|
|
2807
|
-
),
|
|
2808
|
-
);
|
|
2809
|
-
node.isSuffices = isSuffices;
|
|
2810
|
-
return node;
|
|
2811
|
-
}
|
|
2812
|
-
if (isSuffices) {
|
|
2813
|
-
const expression = generateExpression(statement);
|
|
2814
|
-
return applyBuiltin("$Suffices", [expression], getSpanLocation(file, bodySpan));
|
|
2815
|
-
}
|
|
2816
|
-
const previousRight = infixStep.previousRight;
|
|
2817
|
-
const isAtStep =
|
|
2818
|
-
previousRight !== undefined &&
|
|
2819
|
-
statement.kind === "Apply" &&
|
|
2820
|
-
isInfixExpression(statement) &&
|
|
2821
|
-
statement.args[0].kind === "ExceptAt";
|
|
2822
|
-
// SANY generates an @ step's right side before the $Nop that stands for its @.
|
|
2823
|
-
const atRight = isAtStep ? generateExpression(statement.args[1]) : undefined;
|
|
2824
|
-
const expression = isAtStep
|
|
2825
|
-
? createOpAppl(
|
|
2826
|
-
resolveName(statement.operator, getLocation(statement)),
|
|
2827
|
-
[applyBuiltin("$Nop", [previousRight], getLocation(statement.args[0])), atRight],
|
|
2828
|
-
getLocation(statement),
|
|
2829
|
-
)
|
|
2830
|
-
: generateExpression(statement);
|
|
2831
|
-
const right = expression.kind === "OpAppl" ? expression.operands[1] : undefined;
|
|
2832
|
-
infixStep.previousRight = isInfixExpression(statement) ? right : undefined;
|
|
2833
|
-
return expression;
|
|
2834
|
-
};
|
|
2835
|
-
const generated =
|
|
2836
|
-
body.kind === "Assert"
|
|
2837
|
-
? { node: generateAssertion(body.statement), isPick: false }
|
|
2838
|
-
: generateStepBody(step, bodySpan);
|
|
2839
|
-
if (generated === undefined) throw new TlaSyntaxError(`Unexpected proof step ${body.kind}`);
|
|
2840
|
-
const pickContext = generated.isPick ? symbolTable.contexts.pop() : undefined;
|
|
2841
|
-
const definition =
|
|
2842
|
-
step.name === undefined || definitionId === undefined
|
|
2843
|
-
? undefined
|
|
2844
|
-
: createTheoremDefinition(
|
|
2845
|
-
definitionId,
|
|
2846
|
-
step.name,
|
|
2847
|
-
generated.node,
|
|
2848
|
-
getTokenLocation(file, getTokenIndex(file, step.location)),
|
|
2849
|
-
popLabels(),
|
|
2850
|
-
);
|
|
2851
|
-
if (definition) addSymbol(symbolTable, definition);
|
|
2852
|
-
const isScoped = isAssumeProve && !isSuffices;
|
|
2853
|
-
if (isScoped) symbolTable.contexts.push(assumeContext);
|
|
2854
|
-
const stepProof = step.proof && generateProof(step.proof);
|
|
2855
|
-
if (isScoped) symbolTable.contexts.pop();
|
|
2856
|
-
endProof(generated.node);
|
|
2857
|
-
if (isAssumeProve && isSuffices) {
|
|
2858
|
-
sufficesContexts.push(assumeContext);
|
|
2859
|
-
symbolTable.contexts.push(assumeContext);
|
|
2860
|
-
}
|
|
2861
|
-
pickContext?.symbols.forEach((symbol) => addSymbol(symbolTable, symbol));
|
|
2862
|
-
return {
|
|
2863
|
-
...createLevelFields(getId()),
|
|
2864
|
-
kind: "Theorem",
|
|
2865
|
-
definition,
|
|
2866
|
-
body: generated.node,
|
|
2867
|
-
proof: stepProof,
|
|
2868
|
-
isSuffices,
|
|
2869
|
-
location: getSpanLocation(file, stepSpan),
|
|
2870
|
-
};
|
|
2871
|
-
});
|
|
2872
|
-
sufficesContexts.forEach(() => {
|
|
2873
|
-
symbolTable.contexts.pop()?.symbols.forEach((symbol) => addToContext(proofContext, symbol));
|
|
2874
|
-
});
|
|
2875
|
-
symbolTable.contexts.pop();
|
|
2876
|
-
return {
|
|
2877
|
-
...createLevelFields(getId()),
|
|
2878
|
-
kind: "NonLeafProof",
|
|
2879
|
-
steps,
|
|
2880
|
-
instances,
|
|
2881
|
-
proofLevel: proof.level,
|
|
2882
|
-
location: getSpanLocation(file, getUnitSpan(proof.location)),
|
|
2883
|
-
};
|
|
427
|
+
},
|
|
428
|
+
}),
|
|
2884
429
|
};
|
|
2885
430
|
|
|
2886
|
-
// processTheorem: a theorem's ASSUME/PROVE has a context of its own, which its proof sees and
|
|
2887
|
-
// its definition does not.
|
|
2888
|
-
const processTheorem = (theorem: Theorem) => {
|
|
2889
|
-
const location = getSpanLocation(file, getUnitSpan(theorem.location));
|
|
2890
|
-
const statement = theorem.statement;
|
|
2891
|
-
const assumeContext = createContext();
|
|
2892
|
-
if (theorem.name !== undefined) pushLabelScope();
|
|
2893
|
-
const definitionId = theorem.name === undefined ? undefined : getId();
|
|
2894
|
-
if (statement.kind === "AssumeProve") symbolTable.contexts.push(assumeContext);
|
|
2895
|
-
const body =
|
|
2896
|
-
statement.kind === "AssumeProve"
|
|
2897
|
-
? generateAssumeProve(
|
|
2898
|
-
statement,
|
|
2899
|
-
1,
|
|
2900
|
-
theorem.name === undefined ? undefined : { isSuffices: false },
|
|
2901
|
-
)
|
|
2902
|
-
: generateExpression(statement);
|
|
2903
|
-
const definition =
|
|
2904
|
-
theorem.name === undefined || definitionId === undefined
|
|
2905
|
-
? undefined
|
|
2906
|
-
: createTheoremDefinition(definitionId, theorem.name, body, location, popLabels());
|
|
2907
|
-
if (definition) {
|
|
2908
|
-
if (statement.kind === "AssumeProve") symbolTable.contexts.pop();
|
|
2909
|
-
addSymbol(symbolTable, definition);
|
|
2910
|
-
currentModule.definitions.push(definition);
|
|
2911
|
-
if (statement.kind === "AssumeProve") symbolTable.contexts.push(assumeContext);
|
|
2912
|
-
}
|
|
2913
|
-
const proof = theorem.proof && generateProof(theorem.proof);
|
|
2914
|
-
if (statement.kind === "AssumeProve") symbolTable.contexts.pop();
|
|
2915
|
-
endProof(body);
|
|
2916
|
-
const node: TheoremNode = {
|
|
2917
|
-
...createLevelFields(getId()),
|
|
2918
|
-
kind: "Theorem",
|
|
2919
|
-
definition,
|
|
2920
|
-
body,
|
|
2921
|
-
proof,
|
|
2922
|
-
isSuffices: false,
|
|
2923
|
-
location,
|
|
2924
|
-
};
|
|
2925
|
-
currentModule.topLevel.push(node);
|
|
2926
|
-
};
|
|
2927
|
-
|
|
2928
|
-
const processAssumption = (assumption: Assumption) => {
|
|
2929
|
-
const span = getUnitSpan(assumption.location);
|
|
2930
|
-
const location = getSpanLocation(file, span);
|
|
2931
|
-
if (assumption.name !== undefined) pushLabelScope();
|
|
2932
|
-
const body = generateExpression(assumption.expression);
|
|
2933
|
-
const definition =
|
|
2934
|
-
assumption.name === undefined
|
|
2935
|
-
? undefined
|
|
2936
|
-
: createTheoremDefinition(getId(), assumption.name, body, location, popLabels(), false);
|
|
2937
|
-
if (definition) {
|
|
2938
|
-
addSymbol(symbolTable, definition);
|
|
2939
|
-
currentModule.definitions.push(definition);
|
|
2940
|
-
}
|
|
2941
|
-
const node: AssumeNode = {
|
|
2942
|
-
...createLevelFields(getId()),
|
|
2943
|
-
kind: "Assume",
|
|
2944
|
-
definition,
|
|
2945
|
-
body,
|
|
2946
|
-
location,
|
|
2947
|
-
};
|
|
2948
|
-
currentModule.topLevel.push(node);
|
|
2949
|
-
};
|
|
2950
|
-
|
|
2951
|
-
// A declared operator or operator parameter is located with its arguments: F(_, _), _ + _,
|
|
2952
|
-
// -. _ or _ ^+.
|
|
2953
|
-
const getOperatorSpan = (start: number, arity: number) => {
|
|
2954
|
-
if (arity === 0) return { start, end: start };
|
|
2955
|
-
if (isToken(file, start, "_")) {
|
|
2956
|
-
return { start, end: isToken(file, start + 2, "_") ? start + 2 : start + 1 };
|
|
2957
|
-
}
|
|
2958
|
-
if (isToken(file, start + 1, "(")) return { start, end: getMatchingIndex(file, start + 1) };
|
|
2959
|
-
return { start, end: isToken(file, start + 1, "_") ? start + 1 : start };
|
|
2960
|
-
};
|
|
2961
|
-
|
|
2962
|
-
const getDeclarationSpan = (declaration: Declaration) =>
|
|
2963
|
-
getOperatorSpan(getUnitSpan(declaration.location).start, declaration.arity);
|
|
2964
|
-
|
|
2965
|
-
const createDeclaration = (declaration: Declaration, declKind: number): OpDeclNode => {
|
|
2966
|
-
const span = getDeclarationSpan(declaration);
|
|
2967
|
-
// Generator.processParameters makes the same check of a constant, of the name of its
|
|
2968
|
-
// declaration's syntax node, which is never @.
|
|
2969
|
-
if (declaration.name === "@" && declKind === VARIABLE_DECL_KIND) {
|
|
2970
|
-
addError(getSpanLocation(file, span), "Attempted to declare '@' as a variable.");
|
|
2971
|
-
}
|
|
2972
|
-
const node: OpDeclNode = {
|
|
2973
|
-
...createLevelFields(getId()),
|
|
2974
|
-
levelChecked: 1,
|
|
2975
|
-
level: declKind === VARIABLE_DECL_KIND ? 1 : 0,
|
|
2976
|
-
kind: "OpDecl",
|
|
2977
|
-
name: resolveSynonym(declaration.name),
|
|
2978
|
-
arity: declaration.arity,
|
|
2979
|
-
declKind,
|
|
2980
|
-
location: getSpanLocation(file, span),
|
|
2981
|
-
module: currentModule,
|
|
2982
|
-
};
|
|
2983
|
-
if (declKind === CONSTANT_DECL_KIND) {
|
|
2984
|
-
node.levelParams.add(node);
|
|
2985
|
-
node.allParams.add(node);
|
|
2986
|
-
}
|
|
2987
|
-
addSymbol(symbolTable, node);
|
|
2988
|
-
return node;
|
|
2989
|
-
};
|
|
2990
|
-
|
|
2991
|
-
// processExtendsList: the extended module's symbols join this module's context in the order
|
|
2992
|
-
// they joined the extended module's, apart from LOCAL ones and those already present.
|
|
2993
431
|
ast.extends.forEach((name) => {
|
|
2994
|
-
const extendee = getModule(name);
|
|
432
|
+
const extendee = getModule(generation, name);
|
|
2995
433
|
currentModule.extendees.push(extendee);
|
|
2996
|
-
const context = getTopContext(
|
|
434
|
+
const context = getTopContext(contexts);
|
|
2997
435
|
extendee.context.symbols.forEach((symbol) => {
|
|
2998
436
|
if (
|
|
2999
437
|
symbol.kind !== "Module" &&
|
|
@@ -3024,61 +462,52 @@ export const generateSanySpecification = (
|
|
|
3024
462
|
currentModule.topLevel.push(...extendee.topLevel);
|
|
3025
463
|
});
|
|
3026
464
|
|
|
3027
|
-
// Generator.checkIfInRecursiveSection: only definitions and instances may come between a
|
|
3028
|
-
// RECURSIVE declaration and the definition of what it declares.
|
|
3029
|
-
const checkRecursiveSection = (location: SourceLocation, type: string) => {
|
|
3030
|
-
if (state.unresolvedSum === 0) return;
|
|
3031
|
-
addError(
|
|
3032
|
-
getSpanLocation(file, getUnitSpan(location)),
|
|
3033
|
-
`${type} may not appear within a recursive definition section.`,
|
|
3034
|
-
);
|
|
3035
|
-
};
|
|
3036
|
-
|
|
3037
465
|
ast.units.forEach((unit) => {
|
|
3038
466
|
switch (unit.kind) {
|
|
3039
467
|
case "Variables":
|
|
3040
|
-
checkRecursiveSection(unit.location, "A VARIABLE declaration");
|
|
468
|
+
checkRecursiveSection(generation, unit.location, "A VARIABLE declaration");
|
|
3041
469
|
unit.declarations.forEach((declaration) =>
|
|
3042
|
-
createDeclaration(declaration, VARIABLE_DECL_KIND),
|
|
470
|
+
createDeclaration(generation, declaration, VARIABLE_DECL_KIND),
|
|
3043
471
|
);
|
|
3044
472
|
break;
|
|
3045
473
|
case "Constants":
|
|
3046
|
-
checkRecursiveSection(unit.location, "A declaration");
|
|
474
|
+
checkRecursiveSection(generation, unit.location, "A declaration");
|
|
3047
475
|
unit.declarations.forEach((declaration) =>
|
|
3048
|
-
createDeclaration(declaration, CONSTANT_DECL_KIND),
|
|
476
|
+
createDeclaration(generation, declaration, CONSTANT_DECL_KIND),
|
|
3049
477
|
);
|
|
3050
478
|
break;
|
|
3051
479
|
case "Recursive":
|
|
3052
|
-
processRecursive(unit.declarations);
|
|
480
|
+
processRecursive(generation, unit.declarations);
|
|
3053
481
|
break;
|
|
3054
482
|
case "Definition":
|
|
3055
|
-
if (unit.definition.kind === "Function")
|
|
3056
|
-
|
|
483
|
+
if (unit.definition.kind === "Function")
|
|
484
|
+
processFunction(generation, unit.definition, undefined);
|
|
485
|
+
else processOperator(generation, unit.definition, undefined);
|
|
3057
486
|
break;
|
|
3058
487
|
case "Instance":
|
|
3059
|
-
if (unit.instance.name === undefined) generateInstance(unit.instance);
|
|
3060
|
-
else processModuleDefinition(unit.instance, undefined, undefined);
|
|
488
|
+
if (unit.instance.name === undefined) generateInstance(generation, unit.instance);
|
|
489
|
+
else processModuleDefinition(generation, unit.instance, undefined, undefined);
|
|
3061
490
|
break;
|
|
3062
491
|
case "Assumption":
|
|
3063
|
-
checkRecursiveSection(unit.assumption.location, "An ASSUME");
|
|
3064
|
-
processAssumption(unit.assumption);
|
|
492
|
+
checkRecursiveSection(generation, unit.assumption.location, "An ASSUME");
|
|
493
|
+
processAssumption(generation, unit.assumption);
|
|
3065
494
|
break;
|
|
3066
495
|
case "Theorem":
|
|
3067
|
-
checkRecursiveSection(unit.theorem.location, "A THEOREM");
|
|
3068
|
-
processTheorem(unit.theorem);
|
|
496
|
+
checkRecursiveSection(generation, unit.theorem.location, "A THEOREM");
|
|
497
|
+
processTheorem(generation, unit.theorem);
|
|
3069
498
|
break;
|
|
3070
499
|
case "Use":
|
|
3071
|
-
checkRecursiveSection(unit.use.location, "A USE or HIDE");
|
|
3072
|
-
currentModule.topLevel.push(generateUseOrHide(unit.use));
|
|
500
|
+
checkRecursiveSection(generation, unit.use.location, "A USE or HIDE");
|
|
501
|
+
currentModule.topLevel.push(generateUseOrHide(generation, unit.use));
|
|
3073
502
|
break;
|
|
3074
503
|
case "Module": {
|
|
3075
|
-
checkRecursiveSection(unit.module.location, "A MODULE ");
|
|
3076
|
-
const
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
const binding = resolveModule(inner.name);
|
|
3081
|
-
if (binding === undefined) addToContext(getTopContext(
|
|
504
|
+
checkRecursiveSection(generation, unit.module.location, "A MODULE ");
|
|
505
|
+
const inner = generateModule(unit.module, file, currentModule, [
|
|
506
|
+
...contexts,
|
|
507
|
+
createContext(),
|
|
508
|
+
]);
|
|
509
|
+
const binding = resolveModule(generation, inner.name);
|
|
510
|
+
if (binding === undefined) addToContext(getTopContext(contexts), inner);
|
|
3082
511
|
else if (binding !== inner) {
|
|
3083
512
|
addError(
|
|
3084
513
|
inner.location,
|
|
@@ -3089,22 +518,20 @@ export const generateSanySpecification = (
|
|
|
3089
518
|
}
|
|
3090
519
|
}
|
|
3091
520
|
});
|
|
3092
|
-
|
|
521
|
+
|
|
522
|
+
checkForUndefinedRecursiveOps(generation);
|
|
523
|
+
|
|
3093
524
|
return currentModule;
|
|
3094
525
|
};
|
|
3095
526
|
|
|
3096
527
|
const modules = order.map((name) => {
|
|
3097
528
|
options.onAnalyze?.(name);
|
|
3098
529
|
// Each module's Generator creates its nullODN, nullOAN, nullOpArg and nullLabelNode.
|
|
3099
|
-
|
|
530
|
+
nullApplicationId = Array.from({ length: 4 }, getId)[1];
|
|
3100
531
|
const entry = getSource(name, undefined);
|
|
3101
532
|
const context = createContext();
|
|
3102
|
-
// A module's context starts as a copy of SANY's global context of builtins, whose list of
|
|
3103
|
-
// symbols runs from the last builtin to the first.
|
|
3104
533
|
builtins.toReversed().forEach((builtin) => addToContext(context, builtin));
|
|
3105
|
-
const module = generateModule(entry.module, entry.file, undefined,
|
|
3106
|
-
contexts: [context],
|
|
3107
|
-
});
|
|
534
|
+
const module = generateModule(entry.module, entry.file, undefined, [context]);
|
|
3108
535
|
externalModules.set(name, module);
|
|
3109
536
|
options.onModule?.(module, messages);
|
|
3110
537
|
return module;
|