tla-ts 0.0.10-dev.28c823e4b → 0.0.10-dev.4a2a53c48
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 +2 -1
- package/dist/compile.cjs +6 -6
- package/dist/compile.js +10 -10
- package/dist/deciders.cjs +41 -37
- package/dist/deciders.js +57 -53
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +2683 -427
- package/dist/prove.js +2681 -425
- package/dist/run.cjs +45 -42
- package/dist/run.js +47 -44
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +42 -39
- package/dist/transfer.js +44 -41
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +1 -1
- package/src/ast.ts +41 -36
- package/src/builtins.ts +4 -13
- package/src/check-failure.ts +116 -0
- package/src/check-trace.ts +282 -0
- package/src/check.ts +353 -549
- package/src/cli/options.ts +6 -31
- package/src/cli/shared.ts +1 -1
- package/src/cli/tlapm.ts +433 -70
- package/src/cli/tlc-arguments.ts +222 -210
- package/src/cli/tlc-output.ts +687 -0
- package/src/cli/tlc-usage.ts +1 -2
- package/src/cli/tlc.ts +49 -702
- package/src/cli/xml-exporter.ts +23 -33
- package/src/codegen.ts +5 -10
- package/src/coverage.ts +3 -10
- package/src/dependencies.ts +30 -14
- package/src/dfid.ts +16 -16
- package/src/environment-config.ts +315 -0
- package/src/environment.ts +55 -338
- package/src/errors.ts +33 -5
- package/src/evaluate.ts +5 -4
- package/src/exit-codes.ts +1 -0
- package/src/fingerprints.ts +65 -1
- package/src/instances.ts +47 -10
- package/src/java-hash.ts +7 -0
- package/src/json.ts +4 -7
- package/src/levels.ts +39 -42
- package/src/lexer.ts +5 -3
- package/src/liveness-checker.ts +553 -237
- package/src/liveness.ts +94 -86
- package/src/obligations/action.ts +14 -17
- package/src/obligations/anonymize.ts +37 -16
- package/src/obligations/enabled-axioms.ts +218 -0
- package/src/obligations/equality.ts +1 -3
- package/src/obligations/expression.ts +36 -8
- package/src/obligations/format.ts +0 -2
- package/src/obligations/generate.ts +66 -54
- package/src/obligations/index.ts +38 -8
- package/src/obligations/levels.ts +24 -11
- package/src/obligations/module.ts +28 -41
- package/src/obligations/normalize.ts +26 -14
- package/src/obligations/operators.ts +0 -2
- package/src/obligations/pragma.ts +53 -16
- package/src/obligations/printer.ts +43 -13
- package/src/obligations/proof.ts +3 -2
- package/src/obligations/regroup.ts +0 -2
- package/src/obligations/simplify.ts +55 -50
- package/src/obligations/source.ts +118 -74
- package/src/obligations/subexpression.ts +45 -18
- package/src/obligations/substitution.ts +1 -3
- package/src/obligations/temporal.ts +5 -7
- package/src/obligations/tuples.ts +0 -2
- package/src/parallel-worker.ts +10 -30
- package/src/parallel.ts +24 -47
- package/src/parser.ts +580 -180
- package/src/pretty-print.ts +1 -1
- package/src/prover/decide-pool.ts +3 -3
- package/src/prover/deciders.ts +12 -9
- package/src/prover/isabelle/decide.ts +3 -25
- package/src/prover/isabelle/encode.ts +72 -42
- 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 +158 -7
- package/src/prover/isabelle/isar/method.ts +509 -212
- package/src/prover/isabelle/isar/proof.ts +570 -14
- package/src/prover/isabelle/isar/rule-cases.ts +224 -0
- package/src/prover/isabelle/isar/theory.ts +487 -76
- package/src/prover/isabelle/isar/toplevel.ts +331 -0
- package/src/prover/isabelle/isar/type-check.ts +89 -14
- package/src/prover/isabelle/provers/blast/blast.ts +7 -65
- package/src/prover/isabelle/provers/blast/branch.ts +0 -2
- package/src/prover/isabelle/provers/blast/prove.ts +0 -2
- package/src/prover/isabelle/provers/blast/rules.ts +0 -2
- package/src/prover/isabelle/provers/blast/state.ts +0 -2
- package/src/prover/isabelle/provers/blast/term.ts +0 -2
- package/src/prover/isabelle/provers/clasimp.ts +0 -2
- package/src/prover/isabelle/provers/classical.ts +45 -20
- package/src/prover/isabelle/provers/hypsubst.ts +17 -28
- package/src/prover/isabelle/pure/bicompose.ts +9 -27
- package/src/prover/isabelle/pure/consts.ts +0 -2
- package/src/prover/isabelle/pure/conv.ts +2 -10
- package/src/prover/isabelle/pure/drule.ts +72 -6
- package/src/prover/isabelle/pure/envir.ts +0 -2
- package/src/prover/isabelle/pure/goal.ts +1 -3
- package/src/prover/isabelle/pure/logic.ts +1 -3
- package/src/prover/isabelle/pure/name.ts +0 -2
- package/src/prover/isabelle/pure/net.ts +0 -2
- package/src/prover/isabelle/pure/object-logic.ts +13 -7
- package/src/prover/isabelle/pure/pattern.ts +0 -2
- package/src/prover/isabelle/pure/raw-simplifier.ts +4 -15
- package/src/prover/isabelle/pure/rewriter.ts +6 -17
- package/src/prover/isabelle/pure/search.ts +0 -2
- package/src/prover/isabelle/pure/seq.ts +0 -2
- package/src/prover/isabelle/pure/simplifier.ts +26 -7
- package/src/prover/isabelle/pure/table.ts +2 -3
- package/src/prover/isabelle/pure/tactic.ts +0 -2
- package/src/prover/isabelle/pure/tactical.ts +42 -37
- package/src/prover/isabelle/pure/term-ord.ts +0 -2
- package/src/prover/isabelle/pure/term.ts +0 -2
- package/src/prover/isabelle/pure/thm.ts +14 -13
- package/src/prover/isabelle/pure/type.ts +0 -2
- package/src/prover/isabelle/pure/unify.ts +0 -2
- package/src/prover/isabelle/pure/variable.ts +0 -2
- package/src/prover/isabelle/tla/classical-setup.ts +18 -16
- package/src/prover/isabelle/tla/context.ts +58 -11
- package/src/prover/isabelle/tla/logic-data.ts +1 -1
- package/src/prover/isabelle/tla/logic.ts +49 -21
- package/src/prover/isabelle/tla/simpdata.ts +1 -12
- package/src/prover/isabelle/tools/intuitionistic.ts +28 -12
- package/src/prover/ocaml.ts +4 -1
- package/src/prover/prep/action.ts +30 -31
- package/src/prover/prep/enabled-axioms.ts +11 -2
- package/src/prover/prep/fingerprint.ts +2 -5
- package/src/prover/prep/methods.ts +181 -51
- package/src/prover/prep/normalize.ts +38 -9
- package/src/prover/prove.ts +395 -94
- package/src/prover/ptl/decide.ts +95 -50
- package/src/prover/ptl/encode.ts +2 -3
- package/src/prover/ptl/tableau.ts +28 -2
- package/src/prover/smt/arithmetic.ts +310 -0
- package/src/prover/smt/axioms.ts +12 -8
- package/src/prover/smt/context.ts +43 -8
- package/src/prover/smt/data.ts +498 -379
- package/src/prover/smt/encode.ts +148 -57
- package/src/prover/smt/extensionality.ts +5 -3
- package/src/prover/smt/flatten.ts +4 -6
- package/src/prover/smt/format.ts +28 -4
- package/src/prover/smt/instances.ts +7 -1
- package/src/prover/smt/layout.ts +91 -0
- package/src/prover/smt/rewrite.ts +1021 -618
- 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 -1
- 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 +613 -6
- 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/asserted-formulas.ts +87 -16
- package/src/prover/smt/solver/ast-lt.ts +166 -12
- package/src/prover/smt/solver/ast-util.ts +160 -2
- package/src/prover/smt/solver/ast.ts +1427 -41
- package/src/prover/smt/solver/auf-solver.ts +2 -6
- 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 -2
- package/src/prover/smt/solver/cg-table.ts +39 -25
- package/src/prover/smt/solver/check-logic.ts +330 -0
- package/src/prover/smt/solver/clause.ts +0 -1
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +278 -0
- package/src/prover/smt/solver/conflict-resolution.ts +78 -2
- package/src/prover/smt/solver/context.ts +516 -115
- package/src/prover/smt/solver/ctx-simplify.ts +20 -10
- 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 +38 -10
- 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 -1
- package/src/prover/smt/solver/e-matching.ts +7 -8
- 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 -3
- 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 -1
- package/src/prover/smt/solver/expr-safe-replace.ts +26 -2
- 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 -15
- package/src/prover/smt/solver/fix-dl-var.ts +148 -0
- package/src/prover/smt/solver/flat-literals.ts +0 -1
- 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 +175 -15
- 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 -1
- package/src/prover/smt/solver/hashtable.ts +30 -16
- 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 +110 -38
- package/src/prover/smt/solver/lackr.ts +381 -0
- package/src/prover/smt/solver/lbool.ts +0 -1
- 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 -1
- package/src/prover/smt/solver/logic-tactics.ts +345 -0
- package/src/prover/smt/solver/lp/bound-propagator.ts +0 -1
- package/src/prover/smt/solver/lp/constraints.ts +0 -1
- package/src/prover/smt/solver/lp/containers.ts +0 -1
- package/src/prover/smt/solver/lp/core-solver.ts +18 -3
- package/src/prover/smt/solver/lp/cross-nested.ts +0 -1
- package/src/prover/smt/solver/lp/dep-intervals.ts +0 -3
- package/src/prover/smt/solver/lp/dependency.ts +0 -2
- package/src/prover/smt/solver/lp/dioph-eq.ts +1 -10
- package/src/prover/smt/solver/lp/explanation.ts +0 -1
- package/src/prover/smt/solver/lp/gomory.ts +0 -1
- package/src/prover/smt/solver/lp/hnf-cutter.ts +1 -2
- package/src/prover/smt/solver/lp/hnf.ts +0 -3
- package/src/prover/smt/solver/lp/int-cube.ts +3 -4
- package/src/prover/smt/solver/lp/int-solver.ts +0 -2
- package/src/prover/smt/solver/lp/lar-solver.ts +40 -4
- package/src/prover/smt/solver/lp/lar-term.ts +0 -1
- package/src/prover/smt/solver/lp/lp-settings.ts +0 -1
- package/src/prover/smt/solver/lp/maximize-term.ts +59 -10
- package/src/prover/smt/solver/lp/nex-creator.ts +0 -1
- package/src/prover/smt/solver/lp/nex.ts +0 -1
- package/src/prover/smt/solver/lp/nla-backoff.ts +0 -2
- package/src/prover/smt/solver/lp/nla-basics.ts +0 -2
- package/src/prover/smt/solver/lp/nla-core.ts +42 -11
- package/src/prover/smt/solver/lp/nla-divisions.ts +0 -2
- package/src/prover/smt/solver/lp/nla-emonics.ts +0 -1
- package/src/prover/smt/solver/lp/nla-factorization.ts +0 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +9 -4
- package/src/prover/smt/solver/lp/nla-horner.ts +0 -2
- package/src/prover/smt/solver/lp/nla-intervals.ts +0 -1
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +0 -2
- package/src/prover/smt/solver/lp/nla-monotone.ts +0 -1
- package/src/prover/smt/solver/lp/nla-order.ts +0 -2
- package/src/prover/smt/solver/lp/nla-patcher.ts +0 -1
- package/src/prover/smt/solver/lp/nla-powers.ts +220 -0
- package/src/prover/smt/solver/lp/nla-tangents.ts +0 -2
- package/src/prover/smt/solver/lp/nla-throttle.ts +0 -1
- package/src/prover/smt/solver/lp/nla-transcendentals.ts +324 -0
- package/src/prover/smt/solver/lp/nla-types.ts +0 -2
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +0 -1
- package/src/prover/smt/solver/lp/nra-solver.ts +25 -4
- package/src/prover/smt/solver/lp/numeric-pair.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-interval.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +0 -1
- package/src/prover/smt/solver/lp/pdd-solver.ts +0 -2
- package/src/prover/smt/solver/lp/pdd.ts +2 -2
- package/src/prover/smt/solver/lp/random-updater.ts +0 -2
- package/src/prover/smt/solver/lp/static-matrix.ts +0 -1
- package/src/prover/smt/solver/lp/union-find.ts +7 -2
- package/src/prover/smt/solver/macro-finder.ts +530 -0
- package/src/prover/smt/solver/mam-compiler.ts +122 -90
- package/src/prover/smt/solver/mam-interpreter.ts +0 -1
- 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 -6
- 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 +18 -27
- package/src/prover/smt/solver/model-evaluator.ts +188 -12
- package/src/prover/smt/solver/model-finder.ts +12 -18
- package/src/prover/smt/solver/model-generator.ts +56 -29
- package/src/prover/smt/solver/model-macro-solver.ts +49 -46
- 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 +125 -12
- package/src/prover/smt/solver/nlsat/atoms.ts +20 -6
- package/src/prover/smt/solver/nlsat/evaluator.ts +0 -1
- package/src/prover/smt/solver/nlsat/explain.ts +63 -23
- package/src/prover/smt/solver/nlsat/interval-set.ts +1 -2
- package/src/prover/smt/solver/nlsat/levelwise.ts +3 -9
- package/src/prover/smt/solver/nlsat/mpbq.ts +45 -1
- package/src/prover/smt/solver/nlsat/polynomial.ts +13 -6
- package/src/prover/smt/solver/nlsat/solver.ts +234 -36
- 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 -2
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -4
- package/src/prover/smt/solver/nlsat/upolynomial.ts +0 -2
- package/src/prover/smt/solver/nlsat-tactic.ts +82 -0
- package/src/prover/smt/solver/nnf.ts +105 -23
- 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 +19 -11
- 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 +202 -57
- package/src/prover/smt/solver/probes.ts +512 -0
- package/src/prover/smt/solver/propagate-values.ts +37 -21
- package/src/prover/smt/solver/proto-model.ts +648 -19
- package/src/prover/smt/solver/pull-quant.ts +28 -12
- 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 -3
- package/src/prover/smt/solver/qe-lite-fm.ts +3 -4
- package/src/prover/smt/solver/qe-lite.ts +37 -17
- 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 -1
- 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 +16 -56
- package/src/prover/smt/solver/quantifier-analyzer.ts +19 -20
- package/src/prover/smt/solver/quantifier-manager.ts +8 -16
- package/src/prover/smt/solver/quasi-macros.ts +243 -0
- package/src/prover/smt/solver/random-gen.ts +0 -1
- package/src/prover/smt/solver/rational.ts +45 -1
- 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 -1
- package/src/prover/smt/solver/relevancy.ts +1 -2
- package/src/prover/smt/solver/rewriter-tpl.ts +77 -10
- package/src/prover/smt/solver/rewriter.ts +475 -36
- package/src/prover/smt/solver/rlimit.ts +25 -2
- 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 +3126 -316
- 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 +113 -22
- package/src/prover/smt/solver/static-features.ts +314 -0
- package/src/prover/smt/solver/sticky-generations.ts +0 -1
- 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 +53 -2
- package/src/prover/smt/solver/theory-lra-bounds.ts +31 -6
- package/src/prover/smt/solver/theory-lra-final-check.ts +56 -70
- package/src/prover/smt/solver/theory-lra-internalize.ts +86 -27
- package/src/prover/smt/solver/theory-lra-model.ts +20 -35
- package/src/prover/smt/solver/theory-lra-propagate.ts +24 -26
- package/src/prover/smt/solver/theory-lra-state.ts +6 -22
- package/src/prover/smt/solver/theory-lra.ts +188 -1
- 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 +36 -14
- package/src/prover/smt/table.ts +3 -2
- package/src/prover/smt/types.ts +146 -0
- package/src/prover/tlapm/cache.ts +50 -19
- package/src/prover/tlapm/tlapm.ts +15 -9
- package/src/prover/tlapm-library.ts +3208 -0
- package/src/prover/verdict.ts +9 -0
- package/src/prover/zenon/check/check.ts +1 -2
- package/src/prover/zenon/check/records.ts +1 -1
- package/src/prover/zenon/check/relations.ts +2 -1
- package/src/prover/zenon/check/rewrites.ts +2 -15
- package/src/prover/zenon/check/sets.ts +40 -58
- package/src/prover/zenon/check/terms.ts +1 -2
- package/src/prover/zenon/decide.ts +10 -7
- package/src/prover/zenon/encode.ts +0 -2
- package/src/prover/zenon/eqrel.ts +2 -2
- package/src/prover/zenon/equality-rules.ts +0 -2
- package/src/prover/zenon/expr.ts +7 -2
- package/src/prover/zenon/index-table.ts +9 -21
- package/src/prover/zenon/isar-case.ts +0 -2
- package/src/prover/zenon/isar.ts +35 -37
- package/src/prover/zenon/llproof.ts +2 -13
- package/src/prover/zenon/mltoll.ts +12 -30
- package/src/prover/zenon/node.ts +20 -5
- package/src/prover/zenon/parse.ts +9 -8
- package/src/prover/zenon/phrase.ts +0 -2
- package/src/prover/zenon/proof.ts +0 -2
- package/src/prover/zenon/propositional-rules.ts +0 -2
- package/src/prover/zenon/rules.ts +0 -2
- package/src/prover/zenon/search.ts +11 -19
- package/src/prover/zenon/tla/functions.ts +2 -4
- package/src/prover/zenon/tla/llproof.ts +9 -13
- package/src/prover/zenon/tla/records.ts +1 -8
- package/src/prover/zenon/tla/rewrites.ts +0 -2
- package/src/prover/zenon/tla/sets.ts +22 -22
- package/src/prover/zenon/tla/terms.ts +0 -2
- package/src/prover/zenon/tla/tla.ts +7 -8
- package/src/resolve.ts +56 -74
- package/src/runtime.ts +4 -0
- 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 +155 -2625
- package/src/sany/levels.ts +0 -2
- package/src/sany/lexer.ts +17 -16
- package/src/sany/linter.ts +0 -2
- package/src/sany/nodes.ts +6 -13
- package/src/sany/report.ts +3 -6
- package/src/sany/selectors.ts +35 -38
- package/src/sany/xml.ts +23 -55
- package/src/search.ts +205 -43
- package/src/simulate.ts +160 -133
- package/src/spec-description.ts +33 -48
- package/src/standard-definitions.ts +4 -0
- package/src/standard-modules.ts +16 -32
- package/src/state-graph.ts +2 -9
- package/src/substitution-levels.ts +5 -4
- package/src/successors.ts +6 -8
- package/src/symmetry.ts +2 -2
- package/src/tableau.ts +2 -2
- package/src/tlc/fingerprint.ts +81 -9
- package/src/tlc/messages.ts +20 -15
- package/src/tlc/order.ts +2 -9
- package/src/tlc/random.ts +4 -4
- package/src/tlc/replay.ts +8 -15
- package/src/tlc/summary.ts +29 -14
- package/src/tlc/tlc-operators.ts +2 -3
- package/src/tlc/tlcext-operators.ts +44 -12
- package/src/tlc/trace-spec.ts +25 -45
- package/src/typed-tier.ts +6535 -0
- package/src/typescript/actions.ts +2 -4
- package/src/typescript/collections.ts +34 -20
- package/src/typescript/compile.ts +1 -3
- package/src/typescript/formula.ts +5 -3
- package/src/typescript/model.ts +4 -5
- package/src/typescript/steps.ts +20 -18
- package/src/values.ts +63 -158
- package/src/prover/isabelle/isar/statement.ts +0 -52
- 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
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
// Ported from Z3 (src/parsers/smt2/smt2parser.cpp), MIT license, see z3-license.txt.
|
|
2
1
|
import { SmtParseError, SmtUnsupportedError } from "../../../errors";
|
|
3
2
|
import {
|
|
4
3
|
type AstManager,
|
|
4
|
+
getSymbolHash,
|
|
5
5
|
FAMILY_ARITH,
|
|
6
6
|
FAMILY_BASIC,
|
|
7
|
+
KIND_APP,
|
|
7
8
|
KIND_QUANTIFIER,
|
|
8
9
|
KIND_VAR,
|
|
9
10
|
OP_ABS,
|
|
10
11
|
OP_ADD,
|
|
11
12
|
OP_AND,
|
|
13
|
+
OP_AT_LEAST_K,
|
|
14
|
+
OP_AT_MOST_K,
|
|
15
|
+
OP_PB_EQ,
|
|
16
|
+
OP_PB_GE,
|
|
17
|
+
OP_PB_LE,
|
|
18
|
+
OP_ARRAY_DEFAULT,
|
|
19
|
+
OP_ARRAY_MAP,
|
|
20
|
+
OP_CONST_ARRAY,
|
|
12
21
|
OP_DISTINCT,
|
|
13
22
|
OP_DIV,
|
|
23
|
+
OP_DT_ACCESSOR,
|
|
24
|
+
OP_DT_CONSTRUCTOR,
|
|
14
25
|
OP_EQ,
|
|
15
26
|
OP_GE,
|
|
16
27
|
OP_GT,
|
|
@@ -21,65 +32,523 @@ import {
|
|
|
21
32
|
OP_LE,
|
|
22
33
|
OP_LT,
|
|
23
34
|
OP_MOD,
|
|
35
|
+
OP_REM,
|
|
24
36
|
OP_MUL,
|
|
25
37
|
OP_NOT,
|
|
26
38
|
OP_OR,
|
|
39
|
+
OP_PATTERN,
|
|
40
|
+
OP_POWER,
|
|
41
|
+
OP_POWER0,
|
|
42
|
+
OP_SIN,
|
|
43
|
+
OP_COS,
|
|
44
|
+
OP_TAN,
|
|
45
|
+
OP_ASIN,
|
|
46
|
+
OP_ACOS,
|
|
47
|
+
OP_ATAN,
|
|
48
|
+
OP_SINH,
|
|
49
|
+
OP_COSH,
|
|
50
|
+
OP_TANH,
|
|
51
|
+
OP_ASINH,
|
|
52
|
+
OP_ACOSH,
|
|
53
|
+
OP_ATANH,
|
|
54
|
+
OP_EXP,
|
|
55
|
+
OP_ATAN2,
|
|
56
|
+
OP_LOG,
|
|
57
|
+
OP_SELECT,
|
|
58
|
+
OP_STORE,
|
|
27
59
|
OP_SUB,
|
|
28
60
|
OP_TO_INT,
|
|
29
61
|
OP_TO_REAL,
|
|
30
62
|
OP_TRUE,
|
|
31
63
|
OP_FALSE,
|
|
32
64
|
OP_UMINUS,
|
|
33
|
-
OP_UNINTERPRETED,
|
|
34
65
|
OP_XOR,
|
|
35
66
|
SORT_BOOL,
|
|
36
67
|
SORT_INT,
|
|
37
68
|
SORT_REAL,
|
|
38
69
|
isArithSort,
|
|
70
|
+
QUANTIFIER_EXISTS,
|
|
71
|
+
QUANTIFIER_FORALL,
|
|
72
|
+
QUANTIFIER_LAMBDA,
|
|
39
73
|
} from "./ast";
|
|
74
|
+
import { getArraySignature, getMapSignature, mkAsArray } from "./array-decl-plugin";
|
|
75
|
+
import { BV_OP_NAMES, getBvSignature } from "./bv-decl-plugin";
|
|
76
|
+
import {
|
|
77
|
+
type ConstructorDef,
|
|
78
|
+
type DatatypeDef,
|
|
79
|
+
getConstructorAccessors,
|
|
80
|
+
getConstructorIs,
|
|
81
|
+
getConstructorRecognizer,
|
|
82
|
+
getDatatypeConstructors,
|
|
83
|
+
getDatatypeDef,
|
|
84
|
+
instantiateAccessor,
|
|
85
|
+
instantiateConstructor,
|
|
86
|
+
mkDatatypeDef,
|
|
87
|
+
mkDatatypeSort,
|
|
88
|
+
mkDatatypes,
|
|
89
|
+
mkUpdateField,
|
|
90
|
+
removeDatatype,
|
|
91
|
+
substituteSort,
|
|
92
|
+
} from "./datatype-decl-plugin";
|
|
93
|
+
import {
|
|
94
|
+
FPA_OP_NAMES,
|
|
95
|
+
FPA_SORT_NAMES,
|
|
96
|
+
getFloatSortError,
|
|
97
|
+
getFpaSignature,
|
|
98
|
+
mkFpaAppOf,
|
|
99
|
+
} from "./fpa-decl-plugin";
|
|
100
|
+
import { createLogicChecker } from "./check-logic";
|
|
101
|
+
import { shrink } from "./array";
|
|
102
|
+
import { ObjMap } from "./hashtable";
|
|
103
|
+
import {
|
|
104
|
+
CHAR_OP_NAMES,
|
|
105
|
+
getSeqSignature,
|
|
106
|
+
mkSeqAppOf,
|
|
107
|
+
parseZstring,
|
|
108
|
+
SEQ_OP_NAMES,
|
|
109
|
+
SEQ_SORT_NAMES,
|
|
110
|
+
} from "./seq-decl-plugin";
|
|
40
111
|
import { Rational } from "./rational";
|
|
41
|
-
import {
|
|
112
|
+
import {
|
|
113
|
+
logicHasArith,
|
|
114
|
+
logicHasArray,
|
|
115
|
+
logicHasBv,
|
|
116
|
+
logicHasFpa,
|
|
117
|
+
logicHasRealsOnly,
|
|
118
|
+
logicHasSeq,
|
|
119
|
+
isSupportedLogic,
|
|
120
|
+
} from "./smt-logics";
|
|
121
|
+
import { setRecFunDefinition } from "./recfun-decl-plugin";
|
|
122
|
+
import { DEFAULT_PRINT_OPTIONS, printExpr } from "./smt2-printer";
|
|
123
|
+
import { replaceFreeVars, VarSubst } from "./var-subst";
|
|
124
|
+
import { UpolynomialManager } from "./nlsat/upolynomial";
|
|
125
|
+
|
|
126
|
+
export interface ParserOptions {
|
|
127
|
+
isIgnoringBadPatterns: boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const DEFAULT_PARSER_OPTIONS: ParserOptions = { isIgnoringBadPatterns: true };
|
|
131
|
+
|
|
132
|
+
// Z3 counts the columns of the first line from 1 and of the others from 0.
|
|
133
|
+
export interface SmtToken {
|
|
134
|
+
kind: "(" | ")" | "symbol" | "keyword" | "string" | "numeral" | "decimal" | "bitvector";
|
|
135
|
+
text: string;
|
|
136
|
+
line: number;
|
|
137
|
+
column: number;
|
|
138
|
+
offset: number;
|
|
139
|
+
endOffset: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface SmtList {
|
|
143
|
+
kind: "list";
|
|
144
|
+
items: SExpr[];
|
|
145
|
+
line: number;
|
|
146
|
+
column: number;
|
|
147
|
+
offset: number;
|
|
148
|
+
// Z3 reports an error in making an application at its closing parenthesis.
|
|
149
|
+
end: SmtToken;
|
|
150
|
+
// The token after a root-obj or an indexed or qualified identifier, where Z3 reports an error in
|
|
151
|
+
// making it.
|
|
152
|
+
follower?: SmtToken;
|
|
153
|
+
// The command that a list a command reader read is part of.
|
|
154
|
+
root?: SmtList;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type SExpr = SmtToken | SmtList;
|
|
158
|
+
|
|
159
|
+
// smt2parser's local: a term, and the number of binders around it where it was bound.
|
|
160
|
+
interface Local {
|
|
161
|
+
expr: number;
|
|
162
|
+
level: number;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
interface ParsedTerm {
|
|
166
|
+
expr: number;
|
|
167
|
+
isOwned: boolean;
|
|
168
|
+
root: SmtList | undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
interface Macro {
|
|
172
|
+
domain: number[];
|
|
173
|
+
range: number;
|
|
174
|
+
body: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface ConstantSortDecl {
|
|
178
|
+
kind: "sort";
|
|
179
|
+
sort: number;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface UserSortDecl {
|
|
183
|
+
kind: "user";
|
|
184
|
+
name: string;
|
|
185
|
+
arity: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface DatatypeSortDecl {
|
|
189
|
+
kind: "datatype";
|
|
190
|
+
name: string;
|
|
191
|
+
arity: number;
|
|
192
|
+
siblings: string[];
|
|
193
|
+
instances: Map<string, number>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
interface DefinedSortDecl {
|
|
197
|
+
kind: "definition";
|
|
198
|
+
parameters: string[];
|
|
199
|
+
body: SExpr;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Z3's List datatype, which exists when no logic is set, declared here at its first use.
|
|
203
|
+
interface ListSortDecl {
|
|
204
|
+
kind: "list";
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type SortDecl = ConstantSortDecl | UserSortDecl | DatatypeSortDecl | DefinedSortDecl | ListSortDecl;
|
|
42
208
|
|
|
43
|
-
|
|
209
|
+
const NO_BINDINGS: ReadonlyMap<string, number> = new Map();
|
|
44
210
|
|
|
45
|
-
|
|
46
|
-
| { kind: "var"; depth: number; sort: number }
|
|
47
|
-
| { kind: "let"; depth: number; expr: number };
|
|
211
|
+
const UPOLYNOMIALS = new UpolynomialManager();
|
|
48
212
|
|
|
49
213
|
interface SmtProblem {
|
|
50
214
|
assertions: number[];
|
|
51
215
|
hasCheckSat: boolean;
|
|
216
|
+
rlimit: number;
|
|
52
217
|
}
|
|
53
218
|
|
|
54
|
-
interface
|
|
55
|
-
|
|
56
|
-
|
|
219
|
+
interface QualifiedHead {
|
|
220
|
+
head: SmtToken;
|
|
221
|
+
indices: number[];
|
|
222
|
+
build?: (args: number[]) => number;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
interface ApplicationFrame extends QualifiedHead {
|
|
226
|
+
kind: "application";
|
|
227
|
+
list: SmtList;
|
|
228
|
+
isTail: boolean;
|
|
229
|
+
start: number;
|
|
230
|
+
next: number;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// An application whose head is a term, which selects from the array it stands for.
|
|
234
|
+
interface SelectionFrame {
|
|
235
|
+
kind: "selection";
|
|
236
|
+
list: SmtList;
|
|
237
|
+
isTail: boolean;
|
|
238
|
+
start: number;
|
|
239
|
+
next: number;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
interface LetFrame {
|
|
243
|
+
kind: "let";
|
|
244
|
+
list: SmtList;
|
|
245
|
+
bindings: SmtList;
|
|
246
|
+
names: string[];
|
|
247
|
+
isInDecls: boolean;
|
|
248
|
+
isTail: boolean;
|
|
249
|
+
start: number;
|
|
250
|
+
next: number;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
interface LetDeclFrame {
|
|
254
|
+
kind: "let-decl";
|
|
255
|
+
list: SmtList;
|
|
256
|
+
next: number;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
interface QuantifierFrame {
|
|
260
|
+
kind: "quantifier";
|
|
261
|
+
list: SmtList;
|
|
262
|
+
quantifierKind: number;
|
|
263
|
+
names: string[];
|
|
264
|
+
varSorts: number[];
|
|
265
|
+
weight: number;
|
|
266
|
+
qid: string;
|
|
267
|
+
skid: string | undefined;
|
|
268
|
+
isTail: boolean;
|
|
269
|
+
start: number;
|
|
270
|
+
patternStart: number;
|
|
271
|
+
noPatternStart: number;
|
|
272
|
+
next: number;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
interface AttributeFrame {
|
|
276
|
+
kind: "attribute";
|
|
277
|
+
list: SmtList;
|
|
278
|
+
quantifier: QuantifierFrame | undefined;
|
|
279
|
+
lastKeyword: string;
|
|
280
|
+
isTail: boolean;
|
|
57
281
|
start: number;
|
|
58
282
|
next: number;
|
|
59
283
|
}
|
|
60
284
|
|
|
285
|
+
interface PatternFrame {
|
|
286
|
+
kind: "pattern";
|
|
287
|
+
list: SmtList;
|
|
288
|
+
start: number;
|
|
289
|
+
next: number;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// smt2parser.cpp's expr_frame.
|
|
293
|
+
type TermFrame =
|
|
294
|
+
| ApplicationFrame
|
|
295
|
+
| SelectionFrame
|
|
296
|
+
| LetFrame
|
|
297
|
+
| LetDeclFrame
|
|
298
|
+
| QuantifierFrame
|
|
299
|
+
| AttributeFrame
|
|
300
|
+
| PatternFrame;
|
|
301
|
+
|
|
302
|
+
const QUANTIFIER_KINDS = new Map([
|
|
303
|
+
["forall", QUANTIFIER_FORALL],
|
|
304
|
+
["exists", QUANTIFIER_EXISTS],
|
|
305
|
+
["lambda", QUANTIFIER_LAMBDA],
|
|
306
|
+
]);
|
|
307
|
+
|
|
308
|
+
const LET_MESSAGE = "invalid let declaration, symbol expected";
|
|
309
|
+
|
|
310
|
+
// The attributes that consume_attributes takes only in the body of a quantifier.
|
|
311
|
+
const QUANTIFIER_ATTRIBUTES = new Set([":weight", ":skolemid", ":qid", ":pattern", ":no-pattern"]);
|
|
312
|
+
|
|
313
|
+
const IS_PARAMETER_MESSAGE =
|
|
314
|
+
"invalid parameter to datatype function arity == 1 && num_parameters == 1 && parameters[0].is_ast() && is_func_decl(parameters[0].get_ast())";
|
|
315
|
+
|
|
61
316
|
const BUILTIN_OPS = new Map<string, number>([
|
|
62
317
|
["and", OP_AND],
|
|
318
|
+
["-", OP_SUB],
|
|
63
319
|
["or", OP_OR],
|
|
64
320
|
["not", OP_NOT],
|
|
65
321
|
["=>", OP_IMPLIES],
|
|
66
322
|
["xor", OP_XOR],
|
|
67
323
|
["ite", OP_ITE],
|
|
324
|
+
["if", OP_ITE],
|
|
68
325
|
["distinct", OP_DISTINCT],
|
|
69
326
|
["+", OP_ADD],
|
|
70
327
|
["*", OP_MUL],
|
|
71
328
|
["/", OP_DIV],
|
|
72
329
|
["div", OP_IDIV],
|
|
73
330
|
["mod", OP_MOD],
|
|
331
|
+
["rem", OP_REM],
|
|
74
332
|
["abs", OP_ABS],
|
|
75
333
|
["to_real", OP_TO_REAL],
|
|
76
334
|
["to_int", OP_TO_INT],
|
|
77
335
|
["is_int", OP_IS_INT],
|
|
78
336
|
]);
|
|
79
337
|
|
|
80
|
-
|
|
338
|
+
// basic_decl_plugin's aliases, which Z3 knows only when the script sets no logic.
|
|
339
|
+
const LOGIC_FREE_ALIASES = new Map([
|
|
340
|
+
["implies", "=>"],
|
|
341
|
+
["iff", "="],
|
|
342
|
+
["if_then_else", "ite"],
|
|
343
|
+
["&&", "and"],
|
|
344
|
+
["||", "or"],
|
|
345
|
+
["equals", "="],
|
|
346
|
+
["equiv", "="],
|
|
347
|
+
]);
|
|
348
|
+
|
|
349
|
+
// Z3 declares these only without a logic, in ALL or in QF_NTA.
|
|
350
|
+
const TRANSCENDENTAL_OPS = new Map([
|
|
351
|
+
["^", OP_POWER],
|
|
352
|
+
["^0", OP_POWER0],
|
|
353
|
+
["sin", OP_SIN],
|
|
354
|
+
["cos", OP_COS],
|
|
355
|
+
["tan", OP_TAN],
|
|
356
|
+
["asin", OP_ASIN],
|
|
357
|
+
["acos", OP_ACOS],
|
|
358
|
+
["atan", OP_ATAN],
|
|
359
|
+
["sinh", OP_SINH],
|
|
360
|
+
["cosh", OP_COSH],
|
|
361
|
+
["tanh", OP_TANH],
|
|
362
|
+
["asinh", OP_ASINH],
|
|
363
|
+
["acosh", OP_ACOSH],
|
|
364
|
+
["atanh", OP_ATANH],
|
|
365
|
+
["exp", OP_EXP],
|
|
366
|
+
["atan2", OP_ATAN2],
|
|
367
|
+
["arctan2", OP_ATAN2],
|
|
368
|
+
["log", OP_LOG],
|
|
369
|
+
["ln", OP_LOG],
|
|
370
|
+
]);
|
|
371
|
+
const TRANSCENDENTALS = new Set([
|
|
372
|
+
...TRANSCENDENTAL_OPS.keys(),
|
|
373
|
+
"pi",
|
|
374
|
+
"euler",
|
|
375
|
+
"/0",
|
|
376
|
+
"div0",
|
|
377
|
+
"mod0",
|
|
378
|
+
"complex.make",
|
|
379
|
+
"complex.real",
|
|
380
|
+
"complex.imag",
|
|
381
|
+
]);
|
|
382
|
+
|
|
383
|
+
const hasTranscendentals = (logic: string | undefined) =>
|
|
384
|
+
logic === undefined || logic === "ALL" || logic === "QF_NTA";
|
|
385
|
+
|
|
386
|
+
const THEORY_SORTS = new Set([
|
|
387
|
+
"BitVec",
|
|
388
|
+
"Array",
|
|
389
|
+
"Set",
|
|
390
|
+
"Seq",
|
|
391
|
+
"String",
|
|
392
|
+
"StringSequence",
|
|
393
|
+
"RegEx",
|
|
394
|
+
"RegLan",
|
|
395
|
+
"Unicode",
|
|
396
|
+
"FloatingPoint",
|
|
397
|
+
"RoundingMode",
|
|
398
|
+
"Float16",
|
|
399
|
+
"Float32",
|
|
400
|
+
"Float64",
|
|
401
|
+
"Float128",
|
|
402
|
+
"FiniteSet",
|
|
403
|
+
"FiniteField",
|
|
404
|
+
"Complex",
|
|
405
|
+
]);
|
|
406
|
+
const PB_OP_NAMES = new Map([
|
|
407
|
+
["at-most", OP_AT_MOST_K],
|
|
408
|
+
["at-least", OP_AT_LEAST_K],
|
|
409
|
+
["pble", OP_PB_LE],
|
|
410
|
+
["pbge", OP_PB_GE],
|
|
411
|
+
["pbeq", OP_PB_EQ],
|
|
412
|
+
]);
|
|
413
|
+
const THEORY_OPERATOR = /^(bv|fp\.|str\.|seq\.|re\.|char\.|ff\.)/;
|
|
414
|
+
const THEORY_OPERATORS = new Set([
|
|
415
|
+
"bit0",
|
|
416
|
+
"bit1",
|
|
417
|
+
"concat",
|
|
418
|
+
"sign_extend",
|
|
419
|
+
"zero_extend",
|
|
420
|
+
"extract",
|
|
421
|
+
"repeat",
|
|
422
|
+
"rotate_left",
|
|
423
|
+
"rotate_right",
|
|
424
|
+
"ext_rotate_left",
|
|
425
|
+
"ext_rotate_right",
|
|
426
|
+
"bit2bool",
|
|
427
|
+
"ubv_to_int",
|
|
428
|
+
"sbv_to_int",
|
|
429
|
+
"int_to_bv",
|
|
430
|
+
"int2bv",
|
|
431
|
+
"mkbv",
|
|
432
|
+
"store",
|
|
433
|
+
"select",
|
|
434
|
+
"const",
|
|
435
|
+
"map",
|
|
436
|
+
"default",
|
|
437
|
+
"union",
|
|
438
|
+
"intersection",
|
|
439
|
+
"setminus",
|
|
440
|
+
"complement",
|
|
441
|
+
"subset",
|
|
442
|
+
"as-array",
|
|
443
|
+
"choice",
|
|
444
|
+
"array-ext",
|
|
445
|
+
"set-has-size",
|
|
446
|
+
"card",
|
|
447
|
+
"is",
|
|
448
|
+
"update-field",
|
|
449
|
+
"int.to.str",
|
|
450
|
+
"char",
|
|
451
|
+
"fp",
|
|
452
|
+
"to_fp",
|
|
453
|
+
"to_fp_unsigned",
|
|
454
|
+
"to_ieee_bv",
|
|
455
|
+
"+oo",
|
|
456
|
+
"-oo",
|
|
457
|
+
"+zero",
|
|
458
|
+
"-zero",
|
|
459
|
+
"NaN",
|
|
460
|
+
"roundNearestTiesToEven",
|
|
461
|
+
"roundNearestTiesToAway",
|
|
462
|
+
"roundTowardPositive",
|
|
463
|
+
"roundTowardNegative",
|
|
464
|
+
"roundTowardZero",
|
|
465
|
+
"RNE",
|
|
466
|
+
"RNA",
|
|
467
|
+
"RTP",
|
|
468
|
+
"RTN",
|
|
469
|
+
"RTZ",
|
|
470
|
+
"at-most",
|
|
471
|
+
"at-least",
|
|
472
|
+
"pble",
|
|
473
|
+
"pbge",
|
|
474
|
+
"pbeq",
|
|
475
|
+
"linear-order",
|
|
476
|
+
"partial-order",
|
|
477
|
+
"piecewise-linear-order",
|
|
478
|
+
"tree-order",
|
|
479
|
+
"transitive-closure",
|
|
480
|
+
]);
|
|
481
|
+
|
|
482
|
+
// func_decl_info of the builtin declarations: associative to both sides, flat, and associative to
|
|
483
|
+
// the left only. => alone is associative to the right only.
|
|
484
|
+
const ASSOCIATIVE_OPS = new Set([OP_AND, OP_OR, OP_XOR, OP_ADD, OP_MUL]);
|
|
485
|
+
const FLAT_OPS = new Set([OP_AND, OP_OR, OP_ADD, OP_MUL]);
|
|
486
|
+
const LEFT_ASSOCIATIVE_OPS = new Set([OP_SUB, OP_DIV, OP_IDIV]);
|
|
487
|
+
|
|
488
|
+
// arith_decl_plugin's use_coercion.
|
|
489
|
+
const COERCING_OPS = new Set([
|
|
490
|
+
OP_ADD,
|
|
491
|
+
OP_SUB,
|
|
492
|
+
OP_UMINUS,
|
|
493
|
+
OP_MUL,
|
|
494
|
+
OP_POWER,
|
|
495
|
+
OP_LE,
|
|
496
|
+
OP_GE,
|
|
497
|
+
OP_LT,
|
|
498
|
+
OP_GT,
|
|
499
|
+
]);
|
|
500
|
+
|
|
501
|
+
const REAL_FUNCTION_OPS = new Set([
|
|
502
|
+
OP_SIN,
|
|
503
|
+
OP_COS,
|
|
504
|
+
OP_TAN,
|
|
505
|
+
OP_ASIN,
|
|
506
|
+
OP_ACOS,
|
|
507
|
+
OP_ATAN,
|
|
508
|
+
OP_SINH,
|
|
509
|
+
OP_COSH,
|
|
510
|
+
OP_TANH,
|
|
511
|
+
OP_ASINH,
|
|
512
|
+
OP_ACOSH,
|
|
513
|
+
OP_ATANH,
|
|
514
|
+
OP_EXP,
|
|
515
|
+
OP_ATAN2,
|
|
516
|
+
OP_LOG,
|
|
517
|
+
]);
|
|
518
|
+
|
|
519
|
+
// The arity of each builtin declaration but distinct, which takes the arguments it is given.
|
|
520
|
+
const ARITIES = new Map<number, number>([
|
|
521
|
+
[OP_ITE, 3],
|
|
522
|
+
...[OP_NOT, OP_UMINUS, OP_ABS, OP_TO_REAL, OP_TO_INT, OP_IS_INT, ...REAL_FUNCTION_OPS]
|
|
523
|
+
.filter((op) => op !== OP_ATAN2)
|
|
524
|
+
.map((op): [number, number] => [op, 1]),
|
|
525
|
+
]);
|
|
81
526
|
|
|
82
|
-
const
|
|
527
|
+
const RANGES = new Map<number, number>([
|
|
528
|
+
[OP_TO_REAL, SORT_REAL],
|
|
529
|
+
[OP_POWER, SORT_REAL],
|
|
530
|
+
[OP_POWER0, SORT_REAL],
|
|
531
|
+
...[...REAL_FUNCTION_OPS].map((op): [number, number] => [op, SORT_REAL]),
|
|
532
|
+
[OP_DIV, SORT_REAL],
|
|
533
|
+
[OP_TO_INT, SORT_INT],
|
|
534
|
+
[OP_IDIV, SORT_INT],
|
|
535
|
+
[OP_MOD, SORT_INT],
|
|
536
|
+
[OP_REM, SORT_INT],
|
|
537
|
+
...[
|
|
538
|
+
OP_EQ,
|
|
539
|
+
OP_DISTINCT,
|
|
540
|
+
OP_AND,
|
|
541
|
+
OP_OR,
|
|
542
|
+
OP_XOR,
|
|
543
|
+
OP_NOT,
|
|
544
|
+
OP_IMPLIES,
|
|
545
|
+
OP_LE,
|
|
546
|
+
OP_GE,
|
|
547
|
+
OP_LT,
|
|
548
|
+
OP_GT,
|
|
549
|
+
].map((op): [number, number] => [op, SORT_BOOL]),
|
|
550
|
+
[OP_IS_INT, SORT_BOOL],
|
|
551
|
+
]);
|
|
83
552
|
|
|
84
553
|
const CHAINABLE_OPS = new Map<string, number>([
|
|
85
554
|
["=", OP_EQ],
|
|
@@ -88,205 +557,893 @@ const CHAINABLE_OPS = new Map<string, number>([
|
|
|
88
557
|
["<", OP_LT],
|
|
89
558
|
[">", OP_GT],
|
|
90
559
|
]);
|
|
560
|
+
const CHAINABLE_DECLS = new Set(CHAINABLE_OPS.values());
|
|
561
|
+
|
|
562
|
+
const BV_EXTENSION_NAMES = new Set([
|
|
563
|
+
"bvumul_noovfl",
|
|
564
|
+
"bvsmul_noovfl",
|
|
565
|
+
"bvsmul_noudfl",
|
|
566
|
+
"bvsdiv0",
|
|
567
|
+
"bvudiv0",
|
|
568
|
+
"bvsrem0",
|
|
569
|
+
"bvurem0",
|
|
570
|
+
"bvsmod0",
|
|
571
|
+
"ext_rotate_left",
|
|
572
|
+
"ext_rotate_right",
|
|
573
|
+
"int2bv",
|
|
574
|
+
"bv2int",
|
|
575
|
+
"bv2nat",
|
|
576
|
+
"mkbv",
|
|
577
|
+
]);
|
|
578
|
+
|
|
579
|
+
const BV_EXTENSION_LOGICS = new Set(["ALL", "QF_FD", "HORN"]);
|
|
580
|
+
|
|
581
|
+
const ARRAY_SORT_NAMES = new Set(["Array", "=>", "->"]);
|
|
582
|
+
|
|
583
|
+
// array_decl_plugin::get_op_names: select and store in every logic with arrays, the others only
|
|
584
|
+
// without a logic or in HORN and ALL.
|
|
585
|
+
const ARRAY_OPS = new Map([
|
|
586
|
+
["select", OP_SELECT],
|
|
587
|
+
["store", OP_STORE],
|
|
588
|
+
]);
|
|
589
|
+
const ARRAY_EXTENSION_OPS = new Map([["default", OP_ARRAY_DEFAULT]]);
|
|
590
|
+
|
|
591
|
+
const UNARY_BOOL_REFS = new Map([["not", OP_NOT]]);
|
|
592
|
+
const BINARY_BOOL_REFS = new Map([
|
|
593
|
+
["and", OP_AND],
|
|
594
|
+
["or", OP_OR],
|
|
595
|
+
["=>", OP_IMPLIES],
|
|
596
|
+
["xor", OP_XOR],
|
|
597
|
+
]);
|
|
598
|
+
|
|
599
|
+
const REAL_RANGE_OPS = new Set([OP_DIV, OP_TO_REAL]);
|
|
600
|
+
const INT_RANGE_OPS = new Set([OP_IDIV, OP_MOD, OP_REM, OP_TO_INT]);
|
|
601
|
+
const DOMAIN_RANGE_OPS = new Set([OP_ADD, OP_SUB, OP_MUL, OP_ABS]);
|
|
602
|
+
|
|
603
|
+
// mk_func_decl of a builtin ignores the range of the reference and gives its own.
|
|
604
|
+
const getBuiltinRange = (op: number, domain: number[]) =>
|
|
605
|
+
op === OP_ITE
|
|
606
|
+
? domain[1]
|
|
607
|
+
: REAL_RANGE_OPS.has(op)
|
|
608
|
+
? SORT_REAL
|
|
609
|
+
: INT_RANGE_OPS.has(op)
|
|
610
|
+
? SORT_INT
|
|
611
|
+
: DOMAIN_RANGE_OPS.has(op)
|
|
612
|
+
? domain[0]
|
|
613
|
+
: SORT_BOOL;
|
|
614
|
+
|
|
615
|
+
interface MappedDecl {
|
|
616
|
+
decl: number;
|
|
617
|
+
domain: readonly number[];
|
|
618
|
+
range: number;
|
|
619
|
+
}
|
|
620
|
+
const ARRAY_EXTENSION_LOGICS = new Set(["HORN", "ALL"]);
|
|
621
|
+
|
|
622
|
+
// arith_decl_plugin::get_sort_names gives these logics no Int.
|
|
623
|
+
const INT_FREE_LOGICS = new Set(["NRA", "QF_NRA", "QF_NTA", "QF_UFNRA"]);
|
|
624
|
+
|
|
625
|
+
const BUILTIN_NAMES = new Set([
|
|
626
|
+
"true",
|
|
627
|
+
"false",
|
|
628
|
+
"~",
|
|
629
|
+
...BV_OP_NAMES.keys(),
|
|
630
|
+
...CHAINABLE_OPS.keys(),
|
|
631
|
+
...BUILTIN_OPS.keys(),
|
|
632
|
+
...LOGIC_FREE_ALIASES.keys(),
|
|
633
|
+
...TRANSCENDENTALS,
|
|
634
|
+
...THEORY_OPERATORS,
|
|
635
|
+
...SEQ_OP_NAMES.keys(),
|
|
636
|
+
...CHAR_OP_NAMES.keys(),
|
|
637
|
+
]);
|
|
638
|
+
|
|
639
|
+
const FOLLOWED_HEADS = new Set(["root-obj", "_", "as"]);
|
|
640
|
+
|
|
641
|
+
const FF_LITERAL = /^#f-?[0-9]+m[0-9]+$/;
|
|
642
|
+
const FF_LOGICS = new Set(["QF_FF", "QF_FFA", "ALL", "HO_ALL"]);
|
|
643
|
+
|
|
644
|
+
// rational::is_unsigned of a numeral token.
|
|
645
|
+
const isUnsigned = (numeral: string) => BigInt(numeral) >= 0n && BigInt(numeral) <= 0xffffffffn;
|
|
646
|
+
|
|
647
|
+
const SYMBOL_CHARACTER = /[A-Za-z0-9~!@$%^&*_\-+=<>.?/,]/;
|
|
91
648
|
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
position = end === -1 ? text.length : end + 1;
|
|
100
|
-
} else if (char === "(" || char === ")") {
|
|
101
|
-
tokens.push(char);
|
|
102
|
-
position++;
|
|
103
|
-
} else if (char === " " || char === "\n" || char === "\t" || char === "\r") {
|
|
104
|
-
position++;
|
|
105
|
-
} else if (char === "|") {
|
|
106
|
-
const end = text.indexOf("|", position + 1);
|
|
107
|
-
if (end === -1) throw new SmtParseError("unterminated quoted symbol");
|
|
108
|
-
tokens.push(text.slice(position + 1, end));
|
|
109
|
-
position = end + 1;
|
|
110
|
-
} else if (char === '"') {
|
|
111
|
-
let end = position + 1;
|
|
112
|
-
while (end < text.length && (text[end] !== '"' || text[end + 1] === '"'))
|
|
113
|
-
end += text[end] === '"' ? 2 : 1;
|
|
114
|
-
tokens.push(text.slice(position, end + 1));
|
|
115
|
-
position = end + 1;
|
|
116
|
-
} else {
|
|
117
|
-
let end = position;
|
|
118
|
-
while (end < text.length && !' \n\t\r();|"'.includes(text[end])) end++;
|
|
119
|
-
tokens.push(text.slice(position, end));
|
|
120
|
-
position = end;
|
|
649
|
+
const createScanner = (text: string) => {
|
|
650
|
+
const state = { index: 0, line: 1, lineStart: -1 };
|
|
651
|
+
const getColumn = () => state.index - state.lineStart - (state.line === 1 ? 0 : 1);
|
|
652
|
+
const advance = () => {
|
|
653
|
+
if (text[state.index] === "\n") {
|
|
654
|
+
state.line++;
|
|
655
|
+
state.lineStart = state.index;
|
|
121
656
|
}
|
|
122
|
-
|
|
123
|
-
|
|
657
|
+
state.index++;
|
|
658
|
+
};
|
|
659
|
+
const readWhile = (pattern: RegExp) => {
|
|
660
|
+
while (state.index < text.length && pattern.test(text[state.index])) advance();
|
|
661
|
+
};
|
|
662
|
+
const next = (): SmtToken | undefined => {
|
|
663
|
+
for (;;) {
|
|
664
|
+
const character = text[state.index];
|
|
665
|
+
if (character === undefined) return undefined;
|
|
666
|
+
if (character === ";") {
|
|
667
|
+
while (state.index < text.length && text[state.index] !== "\n") advance();
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
if (character === " " || character === "\t" || character === "\r" || character === "\n") {
|
|
671
|
+
advance();
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
const start = state.index;
|
|
675
|
+
const position = { line: state.line, column: getColumn() };
|
|
676
|
+
const getToken = (kind: SmtToken["kind"], tokenText = text.slice(start, state.index)) => ({
|
|
677
|
+
kind,
|
|
678
|
+
text: tokenText,
|
|
679
|
+
...position,
|
|
680
|
+
offset: start,
|
|
681
|
+
endOffset: state.index,
|
|
682
|
+
});
|
|
683
|
+
if (character === "(" || character === ")") {
|
|
684
|
+
advance();
|
|
685
|
+
return getToken(character);
|
|
686
|
+
}
|
|
687
|
+
if (character === "|" || character === '"') {
|
|
688
|
+
advance();
|
|
689
|
+
for (;;) {
|
|
690
|
+
if (state.index >= text.length)
|
|
691
|
+
throw new SmtParseError(
|
|
692
|
+
character === "|" ? "unexpected end of quoted symbol" : "unexpected end of string",
|
|
693
|
+
{ line: state.line, column: getColumn() },
|
|
694
|
+
);
|
|
695
|
+
const current = text[state.index];
|
|
696
|
+
if (character === "|" && current === "\\")
|
|
697
|
+
throw new SmtParseError("invalid character '\\' in quoted symbol", {
|
|
698
|
+
line: state.line,
|
|
699
|
+
column: getColumn(),
|
|
700
|
+
});
|
|
701
|
+
advance();
|
|
702
|
+
// Z3's scanner starts the line before it reads past a newline in a quoted symbol or a
|
|
703
|
+
// string, so the columns of the rest of the line count one more.
|
|
704
|
+
if (current === "\n") state.lineStart--;
|
|
705
|
+
if (current !== character) continue;
|
|
706
|
+
if (character !== '"' || text[state.index] !== '"') break;
|
|
707
|
+
advance();
|
|
708
|
+
}
|
|
709
|
+
const inner = text.slice(start + 1, state.index - 1);
|
|
710
|
+
return character === "|"
|
|
711
|
+
? getToken("symbol", inner)
|
|
712
|
+
: getToken("string", inner.replaceAll('""', '"'));
|
|
713
|
+
}
|
|
714
|
+
if (/[0-9]/.test(character) || (character === "-" && /[0-9]/.test(text[start + 1] ?? ""))) {
|
|
715
|
+
advance();
|
|
716
|
+
readWhile(/[0-9]/);
|
|
717
|
+
if (text[state.index] !== ".") return getToken("numeral");
|
|
718
|
+
advance();
|
|
719
|
+
readWhile(/[0-9]/);
|
|
720
|
+
return getToken("decimal");
|
|
721
|
+
}
|
|
722
|
+
if (character === "#") {
|
|
723
|
+
advance();
|
|
724
|
+
const radix = text[state.index];
|
|
725
|
+
if (radix === "|") {
|
|
726
|
+
advance();
|
|
727
|
+
while (state.index < text.length) {
|
|
728
|
+
const current = text[state.index];
|
|
729
|
+
advance();
|
|
730
|
+
if (current !== "|" || text[state.index] !== "#") continue;
|
|
731
|
+
advance();
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
if (radix === "f") {
|
|
737
|
+
advance();
|
|
738
|
+
readWhile(/[0-9m-]/);
|
|
739
|
+
return getToken("symbol");
|
|
740
|
+
}
|
|
741
|
+
if (radix !== "x" && radix !== "b")
|
|
742
|
+
throw new SmtParseError("invalid bit-vector literal, expecting 'x' or 'b'", {
|
|
743
|
+
line: state.line,
|
|
744
|
+
column: getColumn(),
|
|
745
|
+
});
|
|
746
|
+
advance();
|
|
747
|
+
const digitsStart = state.index;
|
|
748
|
+
readWhile(radix === "x" ? /[0-9a-fA-F]/ : /[01]/);
|
|
749
|
+
if (state.index === digitsStart)
|
|
750
|
+
throw new SmtParseError("invalid empty bit-vector literal", {
|
|
751
|
+
line: state.line,
|
|
752
|
+
column: getColumn(),
|
|
753
|
+
});
|
|
754
|
+
return getToken("bitvector");
|
|
755
|
+
}
|
|
756
|
+
if (character === ":" || SYMBOL_CHARACTER.test(character)) {
|
|
757
|
+
advance();
|
|
758
|
+
readWhile(SYMBOL_CHARACTER);
|
|
759
|
+
return getToken(character === ":" ? "keyword" : "symbol");
|
|
760
|
+
}
|
|
761
|
+
advance();
|
|
762
|
+
throw new SmtParseError("unexpected character", position);
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
return { next, getPosition: () => ({ line: state.line, column: getColumn() }) };
|
|
124
766
|
};
|
|
125
767
|
|
|
126
|
-
const
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
768
|
+
export const createCommandReader = (text: string) => {
|
|
769
|
+
const scanner = createScanner(text);
|
|
770
|
+
const state: { lookahead: SmtToken | undefined; depth: number } = {
|
|
771
|
+
lookahead: undefined,
|
|
772
|
+
depth: 0,
|
|
773
|
+
};
|
|
774
|
+
const take = () => {
|
|
775
|
+
const token = state.lookahead ?? scanner.next();
|
|
776
|
+
state.lookahead = undefined;
|
|
777
|
+
return token;
|
|
778
|
+
};
|
|
779
|
+
const readList = (open: SmtToken): SmtList => {
|
|
780
|
+
const stack: SmtList[] = [];
|
|
781
|
+
const root: SmtList = {
|
|
782
|
+
kind: "list",
|
|
783
|
+
items: [],
|
|
784
|
+
line: open.line,
|
|
785
|
+
column: open.column,
|
|
786
|
+
offset: open.offset,
|
|
787
|
+
end: open,
|
|
788
|
+
};
|
|
789
|
+
let followed: SmtList | undefined;
|
|
790
|
+
for (let current = root; ;) {
|
|
791
|
+
const token = take();
|
|
792
|
+
if (token === undefined)
|
|
793
|
+
throw new SmtParseError("unexpected end of file", scanner.getPosition());
|
|
794
|
+
if (followed !== undefined) followed.follower = token;
|
|
795
|
+
followed = undefined;
|
|
796
|
+
if (token.kind === "(") {
|
|
797
|
+
const list: SmtList = {
|
|
798
|
+
kind: "list",
|
|
799
|
+
items: [],
|
|
800
|
+
line: token.line,
|
|
801
|
+
column: token.column,
|
|
802
|
+
offset: token.offset,
|
|
803
|
+
end: token,
|
|
804
|
+
root,
|
|
805
|
+
};
|
|
806
|
+
current.items.push(list);
|
|
807
|
+
stack.push(current);
|
|
808
|
+
state.depth++;
|
|
809
|
+
current = list;
|
|
810
|
+
continue;
|
|
811
|
+
}
|
|
812
|
+
if (token.kind !== ")") {
|
|
813
|
+
current.items.push(token);
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
current.end = token;
|
|
817
|
+
if (isSymbol(current.items[0]) && FOLLOWED_HEADS.has(current.items[0].text))
|
|
818
|
+
followed = current;
|
|
819
|
+
state.depth--;
|
|
820
|
+
const parent = stack.pop();
|
|
821
|
+
if (parent === undefined) return root;
|
|
822
|
+
current = parent;
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
const read = (): SmtList | undefined => {
|
|
826
|
+
const token = take();
|
|
827
|
+
if (token === undefined) return undefined;
|
|
828
|
+
if (token.kind !== "(") throw new SmtParseError("invalid command, '(' expected", token);
|
|
829
|
+
state.depth = 1;
|
|
830
|
+
return readList(token);
|
|
831
|
+
};
|
|
832
|
+
const skip = (report: (error: SmtParseError) => void) => {
|
|
833
|
+
for (;;) {
|
|
834
|
+
try {
|
|
835
|
+
const token = take();
|
|
836
|
+
if (token === undefined) return;
|
|
837
|
+
if (token.kind === "(" && state.depth === 0) {
|
|
838
|
+
state.lookahead = token;
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
if (token.kind === "(") state.depth++;
|
|
842
|
+
if (token.kind === ")") state.depth = Math.max(0, state.depth - 1);
|
|
843
|
+
} catch (error) {
|
|
844
|
+
if (!(error instanceof SmtParseError)) throw error;
|
|
845
|
+
report(error);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
return { read, skip };
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
export const isSymbol = (sexpr: SExpr | undefined, text?: string): sexpr is SmtToken =>
|
|
853
|
+
sexpr !== undefined && sexpr.kind === "symbol" && (text === undefined || sexpr.text === text);
|
|
854
|
+
|
|
855
|
+
// The identifier Z3's scanner read last, which check_next prints after "got".
|
|
856
|
+
const getLastId = (sexpr: SExpr | undefined): string | undefined => {
|
|
857
|
+
if (sexpr === undefined) return undefined;
|
|
858
|
+
if (sexpr.kind !== "list")
|
|
859
|
+
return sexpr.kind === "symbol" || sexpr.kind === "keyword" ? sexpr.text : undefined;
|
|
860
|
+
return getLastIdBefore(sexpr.items, sexpr.items.length);
|
|
861
|
+
};
|
|
862
|
+
|
|
863
|
+
const getLastIdBefore = (items: SExpr[], end: number): string | undefined => {
|
|
864
|
+
for (let index = end - 1; index >= 0; index--) {
|
|
865
|
+
const id = getLastId(items[index]);
|
|
866
|
+
if (id !== undefined) return id;
|
|
136
867
|
}
|
|
137
|
-
|
|
138
|
-
return stack[0];
|
|
868
|
+
return undefined;
|
|
139
869
|
};
|
|
140
870
|
|
|
141
|
-
const
|
|
142
|
-
|
|
871
|
+
const isSimpleIndex = (index: SExpr) =>
|
|
872
|
+
index.kind === "numeral" ||
|
|
873
|
+
index.kind === "bitvector" ||
|
|
874
|
+
index.kind === "decimal" ||
|
|
875
|
+
index.kind === "keyword";
|
|
876
|
+
|
|
877
|
+
const expectList = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtList => {
|
|
878
|
+
if (sexpr?.kind !== "list") throw new SmtParseError(message, sexpr ?? at);
|
|
143
879
|
return sexpr;
|
|
144
880
|
};
|
|
145
881
|
|
|
146
|
-
const expectSymbol = (sexpr: SExpr | undefined
|
|
147
|
-
if (
|
|
882
|
+
const expectSymbol = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtToken => {
|
|
883
|
+
if (!isSymbol(sexpr)) throw new SmtParseError(message, sexpr ?? at);
|
|
148
884
|
return sexpr;
|
|
149
885
|
};
|
|
150
886
|
|
|
151
|
-
export const
|
|
152
|
-
text: string,
|
|
887
|
+
export const createSmtlibReader = (
|
|
153
888
|
manager: AstManager,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
889
|
+
logic?: string,
|
|
890
|
+
options: ParserOptions = DEFAULT_PARSER_OPTIONS,
|
|
891
|
+
) => {
|
|
892
|
+
const hasArith = logic === undefined || logicHasArith(logic);
|
|
893
|
+
const hasBv = logic === undefined || logicHasBv(logic);
|
|
894
|
+
const logicChecker = createLogicChecker(manager, logic);
|
|
895
|
+
const sortDecls = new Map<string, SortDecl>([
|
|
896
|
+
["Bool", { kind: "sort", sort: SORT_BOOL }],
|
|
897
|
+
...(hasArith && !(logic !== undefined && INT_FREE_LOGICS.has(logic))
|
|
898
|
+
? [["Int", { kind: "sort", sort: SORT_INT }] as const]
|
|
899
|
+
: []),
|
|
900
|
+
...(hasArith ? [["Real", { kind: "sort", sort: SORT_REAL }] as const] : []),
|
|
901
|
+
...(logic === undefined ? [["List", { kind: "list" }] as const] : []),
|
|
160
902
|
]);
|
|
161
|
-
const funcs = new Map<string, number>();
|
|
162
|
-
const
|
|
163
|
-
const
|
|
164
|
-
|
|
903
|
+
const funcs = new Map<string, readonly number[]>();
|
|
904
|
+
const macros = new Map<string, Macro>();
|
|
905
|
+
const lastNamed = { name: "", expr: -1 };
|
|
906
|
+
const notifiedSorts = new Set<number>();
|
|
907
|
+
const undo: (() => void)[] = [];
|
|
908
|
+
const scopeStarts: number[] = [];
|
|
909
|
+
// smt2parser's m_env, a scoped symbol table: one map, and the trail of what each scope shadowed.
|
|
910
|
+
const locals = new Map<string, Local>();
|
|
911
|
+
const shadowedNames: string[] = [];
|
|
912
|
+
const shadowedLocals: (Local | undefined)[] = [];
|
|
913
|
+
const localScopes: number[] = [];
|
|
914
|
+
const binders = { depth: 0 };
|
|
915
|
+
// smt2parser's expr_stack, pattern_stack and nopattern_stack, which hold a reference to each term.
|
|
916
|
+
// -1 on the first stands for a pattern that Z3 ignores.
|
|
917
|
+
const exprStack: number[] = [];
|
|
918
|
+
const patternStack: number[] = [];
|
|
919
|
+
const noPatternStack: number[] = [];
|
|
920
|
+
// The term that parseTerm returns leaves the stack before its caller takes a reference. One that
|
|
921
|
+
// only the stack held keeps a reference in pending until a term of another command is parsed, and
|
|
922
|
+
// one that nothing held, in floating or, where its command is not known, in unrooted, gets one
|
|
923
|
+
// there when another term is parsed while its caller may still read it.
|
|
924
|
+
const parsed: ParsedTerm = { expr: -1, isOwned: false, root: undefined };
|
|
925
|
+
const floating: number[] = [];
|
|
926
|
+
const unrooted: number[] = [];
|
|
927
|
+
const pending: number[] = [];
|
|
928
|
+
|
|
929
|
+
const declare = <Value>(names: Map<string, Value>, name: string, value: Value) => {
|
|
930
|
+
const previous = names.get(name);
|
|
931
|
+
undo.push(() => {
|
|
932
|
+
if (previous === undefined) names.delete(name);
|
|
933
|
+
else names.set(name, previous);
|
|
934
|
+
});
|
|
935
|
+
names.set(name, value);
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
const hasArrays = logic === undefined || logicHasArray(logic);
|
|
939
|
+
const hasFpa = logic === undefined || logicHasFpa(logic);
|
|
940
|
+
const hasArrayExtensions = logic === undefined || ARRAY_EXTENSION_LOGICS.has(logic);
|
|
941
|
+
const hasSeq = logic === undefined || logicHasSeq(logic);
|
|
942
|
+
const restricted = {
|
|
943
|
+
isNumeralAsReal: logic !== undefined && logicHasRealsOnly(logic),
|
|
944
|
+
hasSetSort: hasArrayExtensions,
|
|
945
|
+
hasFf: logic === undefined || FF_LOGICS.has(logic),
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
const getFirstFunc = (name: string) => funcs.get(name)?.[0];
|
|
949
|
+
|
|
950
|
+
// func_decls::signatures_collide: the domain, and the range unless it is -1.
|
|
951
|
+
const hasSignature = (decl: number, domain: readonly number[], range: number) =>
|
|
952
|
+
(range === -1 || manager.funcDecls[decl].range === range) &&
|
|
953
|
+
manager.funcDecls[decl].domain.length === domain.length &&
|
|
954
|
+
manager.funcDecls[decl].domain.every((sort, index) => sort === domain[index]);
|
|
955
|
+
|
|
956
|
+
const checkDecl = (decl: number, at: SExpr) => {
|
|
957
|
+
const error = logicChecker.checkDecl(decl);
|
|
958
|
+
if (error !== undefined) throw new SmtParseError(error, at);
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
const insertFunc = (decl: number, at: SExpr) => {
|
|
962
|
+
checkDecl(decl, at);
|
|
963
|
+
const funcDecl = manager.funcDecls[decl];
|
|
964
|
+
const name = String(funcDecl.name);
|
|
965
|
+
if (macros.has(name))
|
|
966
|
+
throw new SmtParseError(
|
|
967
|
+
`invalid declaration, named expression already defined with this name ${name}`,
|
|
968
|
+
at,
|
|
969
|
+
);
|
|
970
|
+
const existing = funcs.get(name) ?? [];
|
|
971
|
+
if (existing.includes(decl))
|
|
972
|
+
throw new SmtParseError(
|
|
973
|
+
`invalid declaration, ${funcDecl.domain.length === 0 ? "constant" : "function"} '${name}' (with the given signature) already declared`,
|
|
974
|
+
at,
|
|
975
|
+
);
|
|
976
|
+
declare(funcs, name, [...existing, decl]);
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
const insertDatatypeDecls = (sort: number, at: SExpr) => {
|
|
980
|
+
for (const constructor of getDatatypeConstructors(manager, sort)) {
|
|
981
|
+
insertFunc(constructor, at);
|
|
982
|
+
insertFunc(getConstructorRecognizer(manager, constructor), at);
|
|
983
|
+
for (const accessor of getConstructorAccessors(manager, constructor))
|
|
984
|
+
insertFunc(accessor, at);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
const notifyDatatype = (sort: number, at: SExpr) => {
|
|
989
|
+
const parameters = manager.getSortParameters(sort);
|
|
990
|
+
if (parameters.length === 0 || notifiedSorts.has(sort)) return;
|
|
991
|
+
if (getDatatypeDef(manager, sort) === undefined) return;
|
|
992
|
+
if (parameters.every((parameter) => manager.getTypeParameterIndex(parameter) === -1))
|
|
993
|
+
insertDatatypeDecls(sort, at);
|
|
994
|
+
notifiedSorts.add(sort);
|
|
995
|
+
undo.push(() => notifiedSorts.delete(sort));
|
|
996
|
+
};
|
|
997
|
+
|
|
998
|
+
const getDatatypeInstance = (decl: DatatypeSortDecl, parameters: number[], at: SExpr) => {
|
|
999
|
+
const key = parameters.join(",");
|
|
1000
|
+
const sort = decl.instances.get(key) ?? mkDatatypeSort(manager, decl.name, parameters);
|
|
1001
|
+
decl.instances.set(key, sort);
|
|
1002
|
+
notifyDatatype(sort, at);
|
|
1003
|
+
return sort;
|
|
1004
|
+
};
|
|
165
1005
|
|
|
166
|
-
const
|
|
167
|
-
const sort =
|
|
168
|
-
|
|
1006
|
+
const instantiateDatatype = (decl: DatatypeSortDecl, parameters: number[], at: SExpr) => {
|
|
1007
|
+
const sort = getDatatypeInstance(decl, parameters, at);
|
|
1008
|
+
const def = getDatatypeDef(manager, sort);
|
|
1009
|
+
if (def === undefined || parameters.length === 0 || decl.siblings.length === 0) return sort;
|
|
1010
|
+
const marked = new Set([sort]);
|
|
1011
|
+
for (const constructor of def.constructors)
|
|
1012
|
+
for (const accessor of constructor.accessors) {
|
|
1013
|
+
const range = accessor.range;
|
|
1014
|
+
if (!manager.isDatatypeSort(range) || marked.has(range)) continue;
|
|
1015
|
+
marked.add(range);
|
|
1016
|
+
const sibling = sortDecls.get(manager.sortNames[range]);
|
|
1017
|
+
if (sibling?.kind !== "datatype" || !decl.siblings.includes(sibling.name)) continue;
|
|
1018
|
+
const instance = substituteSort(manager, range, def.parameters, parameters);
|
|
1019
|
+
getDatatypeInstance(sibling, [...manager.getSortParameters(instance)], at);
|
|
1020
|
+
}
|
|
169
1021
|
return sort;
|
|
170
1022
|
};
|
|
171
1023
|
|
|
172
|
-
const
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
1024
|
+
const getSortDeclArity = (decl: SortDecl) =>
|
|
1025
|
+
decl.kind === "sort"
|
|
1026
|
+
? 0
|
|
1027
|
+
: decl.kind === "list"
|
|
1028
|
+
? 1
|
|
1029
|
+
: decl.kind === "definition"
|
|
1030
|
+
? decl.parameters.length
|
|
1031
|
+
: decl.arity;
|
|
1032
|
+
|
|
1033
|
+
const instantiateSort = (decl: SortDecl, parameters: number[], at: SExpr): number => {
|
|
1034
|
+
if (decl.kind === "sort") return decl.sort;
|
|
1035
|
+
if (decl.kind === "list") {
|
|
1036
|
+
const element = manager.mkTypeParameter(0);
|
|
1037
|
+
mkDatatypes(manager, [
|
|
1038
|
+
mkDatatypeDef(
|
|
1039
|
+
manager,
|
|
1040
|
+
"List",
|
|
1041
|
+
[element],
|
|
1042
|
+
[
|
|
1043
|
+
{ name: "nil", recognizer: "is-nil", accessors: [] },
|
|
1044
|
+
{
|
|
1045
|
+
name: "insert",
|
|
1046
|
+
recognizer: "is-insert",
|
|
1047
|
+
accessors: [
|
|
1048
|
+
{ name: "head", range: element },
|
|
1049
|
+
{ name: "tail", range: -1 },
|
|
1050
|
+
],
|
|
1051
|
+
},
|
|
1052
|
+
],
|
|
1053
|
+
),
|
|
1054
|
+
]);
|
|
1055
|
+
const list: DatatypeSortDecl = {
|
|
1056
|
+
kind: "datatype",
|
|
1057
|
+
name: "List",
|
|
1058
|
+
arity: 1,
|
|
1059
|
+
siblings: [],
|
|
1060
|
+
instances: new Map(),
|
|
1061
|
+
};
|
|
1062
|
+
sortDecls.set("List", list);
|
|
1063
|
+
return instantiateSort(list, parameters, at);
|
|
176
1064
|
}
|
|
177
|
-
return
|
|
1065
|
+
if (decl.kind === "user") return manager.mkUninterpretedSort(decl.name, parameters);
|
|
1066
|
+
if (decl.kind === "datatype") return instantiateDatatype(decl, parameters, at);
|
|
1067
|
+
return parseSort(
|
|
1068
|
+
decl.body,
|
|
1069
|
+
"",
|
|
1070
|
+
new Map(decl.parameters.map((name, index) => [name, parameters[index]])),
|
|
1071
|
+
);
|
|
178
1072
|
};
|
|
179
1073
|
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
1074
|
+
const parseParametricSort = (
|
|
1075
|
+
sexpr: SmtList,
|
|
1076
|
+
context: string,
|
|
1077
|
+
bindings: ReadonlyMap<string, number>,
|
|
1078
|
+
): number => {
|
|
1079
|
+
const head = sexpr.items[0];
|
|
1080
|
+
if (!isSymbol(head)) throw new SmtParseError("invalid sort, symbol or '_' expected", head);
|
|
1081
|
+
const isArray =
|
|
1082
|
+
ARRAY_SORT_NAMES.has(head.text) || (head.text === "Set" && restricted.hasSetSort);
|
|
1083
|
+
if (isArray && hasArrays) {
|
|
1084
|
+
const parameters = sexpr.items.slice(1).map((item) => parseSort(item, context, bindings));
|
|
1085
|
+
if (head.text === "Set" ? parameters.length !== 1 : parameters.length < 2)
|
|
1086
|
+
throw new SmtParseError(
|
|
1087
|
+
"invalid array sort definition, invalid number of parameters",
|
|
1088
|
+
sexpr.end,
|
|
1089
|
+
);
|
|
1090
|
+
return manager.mkArraySort(head.text === "Set" ? [parameters[0], SORT_BOOL] : parameters);
|
|
1091
|
+
}
|
|
1092
|
+
const decl = sortDecls.get(head.text);
|
|
1093
|
+
if (decl === undefined && hasSeq && (head.text === "Seq" || head.text === "RegEx")) {
|
|
1094
|
+
const parameters = sexpr.items.slice(1).map((item) => parseSort(item, context, bindings));
|
|
1095
|
+
if (parameters.length !== 1)
|
|
1096
|
+
throw new SmtParseError(
|
|
1097
|
+
head.text === "Seq"
|
|
1098
|
+
? "Invalid sequence sort, expecting one parameter"
|
|
1099
|
+
: "Invalid regex sort, expecting one parameter",
|
|
1100
|
+
sexpr.end,
|
|
1101
|
+
);
|
|
1102
|
+
return head.text === "Seq"
|
|
1103
|
+
? manager.mkSeqSort(parameters[0])
|
|
1104
|
+
: manager.mkReSort(parameters[0]);
|
|
1105
|
+
}
|
|
1106
|
+
if (decl === undefined && hasFpa && FPA_SORT_NAMES.has(head.text)) {
|
|
1107
|
+
const parameters = sexpr.items.slice(1).map((item) => parseSort(item, context, bindings));
|
|
1108
|
+
if (head.text === "FloatingPoint") return mkFloatSort([], sexpr);
|
|
1109
|
+
if (parameters.length > 0)
|
|
1110
|
+
throw new SmtUnsupportedError("parameters on a floating-point sort");
|
|
1111
|
+
return parseSort(head, context, bindings);
|
|
1112
|
+
}
|
|
1113
|
+
if (decl === undefined) {
|
|
1114
|
+
if (THEORY_SORTS.has(head.text) && !isLackingSort(head.text))
|
|
1115
|
+
throw new SmtUnsupportedError(`the sort ${head.text}`);
|
|
1116
|
+
throw new SmtParseError(`unknown sort '${head.text}'`, head);
|
|
1117
|
+
}
|
|
1118
|
+
const parameters = sexpr.items.slice(1).map((item) => parseSort(item, context, bindings));
|
|
1119
|
+
if (parameters.length !== getSortDeclArity(decl))
|
|
1120
|
+
throw new SmtParseError("invalid number of parameters to sort constructor", sexpr.end);
|
|
1121
|
+
return instantiateSort(decl, parameters, sexpr);
|
|
184
1122
|
};
|
|
185
1123
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
1124
|
+
// parse_psort: Z3 makes the leaves and the indexed sorts of a sort definition, and an application
|
|
1125
|
+
// of a builtin sort, which takes any number of parameters, only when the definition is used.
|
|
1126
|
+
const checkPsort = (sexpr: SExpr, bindings: ReadonlyMap<string, number>) => {
|
|
1127
|
+
const head = sexpr.kind === "list" ? sexpr.items[0] : undefined;
|
|
1128
|
+
if (
|
|
1129
|
+
head === undefined ||
|
|
1130
|
+
!isSymbol(head) ||
|
|
1131
|
+
head.text === "_" ||
|
|
1132
|
+
sortDecls.has(head.text) ||
|
|
1133
|
+
!isBuiltinParametricSort(head.text)
|
|
1134
|
+
) {
|
|
1135
|
+
parseSort(sexpr, "", bindings);
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
for (const item of (sexpr as SmtList).items.slice(1)) checkPsort(item, bindings);
|
|
194
1139
|
};
|
|
195
1140
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
1141
|
+
// Z3 does not know the sorts of a theory that the logic lacks.
|
|
1142
|
+
const isLackingSort = (name: string) =>
|
|
1143
|
+
name === "Set"
|
|
1144
|
+
? !hasArrays || !restricted.hasSetSort
|
|
1145
|
+
: ARRAY_SORT_NAMES.has(name)
|
|
1146
|
+
? !hasArrays
|
|
1147
|
+
: name === "BitVec"
|
|
1148
|
+
? !hasBv
|
|
1149
|
+
: SEQ_SORT_NAMES.has(name) || name === "Unicode"
|
|
1150
|
+
? !hasSeq
|
|
1151
|
+
: FPA_SORT_NAMES.has(name) && !hasFpa;
|
|
1152
|
+
|
|
1153
|
+
const isBuiltinParametricSort = (name: string) =>
|
|
1154
|
+
(hasArrays && (ARRAY_SORT_NAMES.has(name) || (name === "Set" && restricted.hasSetSort))) ||
|
|
1155
|
+
(hasSeq && (name === "Seq" || name === "RegEx")) ||
|
|
1156
|
+
(hasFpa && FPA_SORT_NAMES.has(name));
|
|
1157
|
+
|
|
1158
|
+
const mkFloatSort = (indices: SExpr[], at: SExpr) => {
|
|
1159
|
+
const ebitsIndex = indices[0];
|
|
1160
|
+
const sbitsIndex = indices[1];
|
|
1161
|
+
if (indices.length !== 2 || ebitsIndex.kind !== "numeral" || sbitsIndex.kind !== "numeral")
|
|
1162
|
+
throw new SmtParseError(
|
|
1163
|
+
"expecting two integer parameters to floating point sort (ebits, sbits)",
|
|
1164
|
+
at.kind === "list" ? at.end : at,
|
|
1165
|
+
);
|
|
1166
|
+
const ebits = Number(ebitsIndex.text);
|
|
1167
|
+
const sbits = Number(sbitsIndex.text);
|
|
1168
|
+
const error = getFloatSortError(ebits, sbits);
|
|
1169
|
+
if (error !== undefined) throw new SmtParseError(error, at.kind === "list" ? at.end : at);
|
|
1170
|
+
return manager.mkFpSort(ebits, sbits);
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
const parseSort = (
|
|
1174
|
+
sexpr: SExpr,
|
|
1175
|
+
context: string,
|
|
1176
|
+
bindings: ReadonlyMap<string, number> = NO_BINDINGS,
|
|
1177
|
+
): number => {
|
|
1178
|
+
if (sexpr.kind === ")")
|
|
1179
|
+
throw new SmtParseError(`${context} invalid sort, unexpected ')'`, sexpr);
|
|
1180
|
+
if (sexpr.kind === "list") {
|
|
1181
|
+
if (!isSymbol(sexpr.items[0], "_")) return parseParametricSort(sexpr, context, bindings);
|
|
1182
|
+
// parse_indexed_sort.
|
|
1183
|
+
const name = sexpr.items[1];
|
|
1184
|
+
if (!isSymbol(name))
|
|
1185
|
+
throw new SmtParseError("invalid indexed sort, symbol expected", name ?? sexpr.end);
|
|
1186
|
+
if (hasFpa && name.text === "FloatingPoint") return mkFloatSort(sexpr.items.slice(2), sexpr);
|
|
1187
|
+
const indices = sexpr.items.slice(2);
|
|
1188
|
+
if (isLackingSort(name.text) || !(THEORY_SORTS.has(name.text) || sortDecls.has(name.text)))
|
|
1189
|
+
throw new SmtParseError(`unknown sort '${name.text}'`, indices[0] ?? sexpr.end);
|
|
1190
|
+
if (name.text !== "BitVec") throw new SmtUnsupportedError("indexed sorts");
|
|
1191
|
+
for (const index of indices) {
|
|
1192
|
+
if (index.kind !== "numeral")
|
|
1193
|
+
throw new SmtParseError("invalid indexed sort, integer or ')' expected", index);
|
|
1194
|
+
if (!isUnsigned(index.text))
|
|
1195
|
+
throw new SmtParseError(
|
|
1196
|
+
"invalid indexed identifier, index is too big to fit in an unsigned machine integer",
|
|
1197
|
+
index,
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
1200
|
+
const width = indices[0];
|
|
1201
|
+
if (indices.length !== 1 || width.kind !== "numeral")
|
|
1202
|
+
throw new SmtParseError("expecting one integer parameter to bit-vector sort", sexpr.end);
|
|
1203
|
+
if (BigInt(width.text) === 0n)
|
|
1204
|
+
throw new SmtParseError("bit-vector size must be greater than zero", sexpr.end);
|
|
1205
|
+
return manager.mkBvSort(Number(width.text));
|
|
1206
|
+
}
|
|
1207
|
+
const decl = sortDecls.get(sexpr.text);
|
|
1208
|
+
if (decl !== undefined) {
|
|
1209
|
+
if (getSortDeclArity(decl) !== 0)
|
|
1210
|
+
throw new SmtParseError("sort constructor expects parameters", sexpr);
|
|
1211
|
+
return instantiateSort(decl, [], sexpr);
|
|
1212
|
+
}
|
|
1213
|
+
const bound = bindings.get(sexpr.text);
|
|
1214
|
+
if (bound !== undefined) return bound;
|
|
1215
|
+
if (hasSeq && (SEQ_SORT_NAMES.has(sexpr.text) || sexpr.text === "Unicode")) {
|
|
1216
|
+
if (sexpr.text === "String" || sexpr.text === "StringSequence") return manager.mkStringSort();
|
|
1217
|
+
if (sexpr.text === "RegLan") return manager.mkReSort(manager.mkStringSort());
|
|
1218
|
+
if (sexpr.text === "Unicode") return manager.mkCharSort();
|
|
1219
|
+
throw new SmtParseError(
|
|
1220
|
+
sexpr.text === "Seq"
|
|
1221
|
+
? "Invalid sequence sort, expecting one parameter"
|
|
1222
|
+
: "Invalid regex sort, expecting one parameter",
|
|
1223
|
+
sexpr,
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
if (hasFpa && FPA_SORT_NAMES.has(sexpr.text)) {
|
|
1227
|
+
if (sexpr.text === "RoundingMode") return manager.mkRmSort();
|
|
1228
|
+
const bits = FPA_SORT_NAMES.get(sexpr.text);
|
|
1229
|
+
return bits === undefined ? mkFloatSort([], sexpr) : manager.mkFpSort(bits[0], bits[1]);
|
|
1230
|
+
}
|
|
1231
|
+
if (THEORY_SORTS.has(sexpr.text) && !isLackingSort(sexpr.text))
|
|
1232
|
+
throw new SmtUnsupportedError(`the sort ${sexpr.text}`);
|
|
1233
|
+
throw new SmtParseError(
|
|
1234
|
+
`${context === "" ? "" : `${context}: `}unknown sort '${sexpr.text}'`,
|
|
1235
|
+
sexpr,
|
|
1236
|
+
);
|
|
1237
|
+
};
|
|
1238
|
+
|
|
1239
|
+
const beginScope = () => localScopes.push(shadowedNames.length);
|
|
1240
|
+
|
|
1241
|
+
const insertLocal = (name: string, expr: number) => {
|
|
1242
|
+
shadowedNames.push(name);
|
|
1243
|
+
shadowedLocals.push(locals.get(name));
|
|
1244
|
+
locals.set(name, { expr, level: binders.depth });
|
|
1245
|
+
};
|
|
1246
|
+
|
|
1247
|
+
const endScope = () => {
|
|
1248
|
+
const start = localScopes.pop() ?? 0;
|
|
1249
|
+
for (let index = shadowedNames.length - 1; index >= start; index--) {
|
|
1250
|
+
const shadowed = shadowedLocals[index];
|
|
1251
|
+
if (shadowed === undefined) locals.delete(shadowedNames[index]);
|
|
1252
|
+
else locals.set(shadowedNames[index], shadowed);
|
|
1253
|
+
}
|
|
1254
|
+
shrink(shadowedNames, start);
|
|
1255
|
+
shrink(shadowedLocals, start);
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
const pushRef = (stack: number[], expr: number) => {
|
|
1259
|
+
if (expr !== -1) manager.incRef(expr);
|
|
1260
|
+
stack.push(expr);
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1263
|
+
// ref_vector::shrink, which releases from the new end on.
|
|
1264
|
+
const shrinkRefs = (stack: number[], size: number) => {
|
|
1265
|
+
for (let index = size; index < stack.length; index++)
|
|
1266
|
+
if (stack[index] !== -1) manager.decRef(stack[index]);
|
|
1267
|
+
shrink(stack, size);
|
|
1268
|
+
};
|
|
1269
|
+
|
|
1270
|
+
const releasePending = () => {
|
|
1271
|
+
for (const expr of pending) manager.decRef(expr);
|
|
1272
|
+
shrink(pending, 0);
|
|
1273
|
+
};
|
|
209
1274
|
|
|
1275
|
+
const protect = (exprs: number[]) => {
|
|
1276
|
+
for (const expr of exprs)
|
|
1277
|
+
if (manager.ids[expr] !== -1 && manager.getRefCount(expr) === 0) pushRef(pending, expr);
|
|
1278
|
+
shrink(exprs, 0);
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1281
|
+
// push_local: var_shifter moves the free variables of a term that was bound under fewer binders.
|
|
1282
|
+
// Its cache holds the terms it makes, which replaceFreeVars rebuilds.
|
|
1283
|
+
const getLocalTerm = (local: Local) => {
|
|
1284
|
+
if (local.level === binders.depth) return local.expr;
|
|
1285
|
+
if (manager.kinds[local.expr] === KIND_VAR)
|
|
1286
|
+
return manager.mkVar(
|
|
1287
|
+
manager.values[local.expr] + binders.depth - local.level,
|
|
1288
|
+
manager.sorts[local.expr],
|
|
1289
|
+
);
|
|
1290
|
+
return replaceFreeVars(manager, local.expr, (variable, index, shift) =>
|
|
1291
|
+
manager.mkVar(index + shift + binders.depth - local.level, manager.sorts[variable]),
|
|
1292
|
+
);
|
|
1293
|
+
};
|
|
1294
|
+
|
|
1295
|
+
// basic_decl_plugin::join.
|
|
1296
|
+
const getJoin = (left: number, right: number, at: SExpr) => {
|
|
1297
|
+
if (left === right) return left;
|
|
1298
|
+
if (isArithSort(left) && isArithSort(right)) return left === SORT_REAL ? left : right;
|
|
1299
|
+
if (left === SORT_BOOL && isArithSort(right)) return right;
|
|
1300
|
+
if (right === SORT_BOOL && isArithSort(left)) return left;
|
|
1301
|
+
throw new SmtParseError(
|
|
1302
|
+
`Sorts ${manager.getSortName(left)} and ${manager.getSortName(right)} are incompatible`,
|
|
1303
|
+
at,
|
|
1304
|
+
);
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1307
|
+
const getJoinOfArgs = (args: number[], at: SExpr) =>
|
|
1308
|
+
args.reduce((join, arg) => getJoin(join, manager.sorts[arg], at), manager.sorts[args[0]]);
|
|
1309
|
+
|
|
1310
|
+
// basic_decl_plugin::mk_func_decl and arith_decl_plugin::mk_func_decl: the sort of the arguments
|
|
1311
|
+
// that the declaration takes, the condition of ite aside, or -1 where the plugin makes none.
|
|
1312
|
+
const getDomainSort = (op: number, args: number[], at: SExpr) => {
|
|
1313
|
+
if (op === OP_ITE)
|
|
1314
|
+
return args.length === 3 ? getJoin(manager.sorts[args[1]], manager.sorts[args[2]], at) : -1;
|
|
1315
|
+
if (op === OP_EQ) return args.length >= 2 ? getJoinOfArgs(args, at) : -1;
|
|
1316
|
+
if (op === OP_DISTINCT) return args.length === 0 ? SORT_BOOL : getJoinOfArgs(args, at);
|
|
1317
|
+
if (manager.funcDecls[op].family === FAMILY_BASIC) return SORT_BOOL;
|
|
1318
|
+
if (COERCING_OPS.has(op))
|
|
1319
|
+
return args.some((arg) => manager.sorts[arg] === SORT_REAL) ? SORT_REAL : SORT_INT;
|
|
1320
|
+
if (op === OP_DIV || op === OP_IS_INT || REAL_FUNCTION_OPS.has(op)) return SORT_REAL;
|
|
1321
|
+
if (op === OP_IDIV || op === OP_MOD || op === OP_REM) return SORT_INT;
|
|
1322
|
+
return manager.sorts[args[0]] === SORT_REAL ? SORT_REAL : SORT_INT;
|
|
1323
|
+
};
|
|
1324
|
+
|
|
1325
|
+
// ast_manager::coerce_to.
|
|
210
1326
|
const coerce = (arg: number, sort: number) => {
|
|
211
1327
|
const argSort = manager.sorts[arg];
|
|
212
|
-
if (argSort === sort || !isArithSort(
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
1328
|
+
if (argSort === sort || !isArithSort(sort)) return arg;
|
|
1329
|
+
if (isArithSort(argSort))
|
|
1330
|
+
return sort === SORT_REAL
|
|
1331
|
+
? manager.mkApp(OP_TO_REAL, [arg], SORT_REAL)
|
|
1332
|
+
: manager.mkApp(OP_TO_INT, [arg], SORT_INT);
|
|
1333
|
+
if (argSort !== SORT_BOOL) return arg;
|
|
1334
|
+
const one = manager.mkNumeralOfSort(Rational.of(1n), sort);
|
|
1335
|
+
return manager.mkApp(OP_ITE, [arg, one, manager.mkNumeralOfSort(Rational.of(0n), sort)], sort);
|
|
216
1336
|
};
|
|
217
1337
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
1338
|
+
// A declaration of the script has its own domain, and a builtin one takes domainSort.
|
|
1339
|
+
const getArity = (decl: number, numArgs: number, domainSort: number) =>
|
|
1340
|
+
domainSort === -1
|
|
1341
|
+
? manager.funcDecls[decl].domain.length
|
|
1342
|
+
: decl === OP_DISTINCT
|
|
1343
|
+
? numArgs
|
|
1344
|
+
: (ARITIES.get(decl) ?? 2);
|
|
1345
|
+
|
|
1346
|
+
const getExpectedSort = (decl: number, index: number, domainSort: number) =>
|
|
1347
|
+
domainSort === -1
|
|
1348
|
+
? manager.funcDecls[decl].domain[index]
|
|
1349
|
+
: decl === OP_ITE && index === 0
|
|
1350
|
+
? SORT_BOOL
|
|
1351
|
+
: domainSort;
|
|
1352
|
+
|
|
1353
|
+
const formatDecl = (decl: number, numArgs: number, domainSort: number) => {
|
|
1354
|
+
const domain = Array.from({ length: getArity(decl, numArgs, domainSort) }, (_unused, index) =>
|
|
1355
|
+
manager.getSortName(getExpectedSort(decl, index, domainSort)),
|
|
228
1356
|
);
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
(
|
|
260
|
-
|
|
261
|
-
|
|
1357
|
+
const range =
|
|
1358
|
+
domainSort === -1 ? manager.funcDecls[decl].range : (RANGES.get(decl) ?? domainSort);
|
|
1359
|
+
return `(declare-fun ${String(manager.funcDecls[decl].name)} (${domain.join(" ")}) ${manager.getSortName(range)})`;
|
|
1360
|
+
};
|
|
1361
|
+
|
|
1362
|
+
// ast_manager::mk_app_core: coercion_needed, coerce_to and check_args.
|
|
1363
|
+
const mkAppCore = (decl: number, args: number[], at: SExpr, domainSort: number) => {
|
|
1364
|
+
const isAssociative = domainSort !== -1 && ASSOCIATIVE_OPS.has(decl);
|
|
1365
|
+
const isCoercing =
|
|
1366
|
+
(isAssociative || getArity(decl, args.length, domainSort) === args.length) &&
|
|
1367
|
+
args.some((arg, index) => {
|
|
1368
|
+
const expected = getExpectedSort(decl, index, domainSort);
|
|
1369
|
+
return isArithSort(expected) && expected !== manager.sorts[arg];
|
|
1370
|
+
});
|
|
1371
|
+
const coerced = isCoercing
|
|
1372
|
+
? args.map((arg, index) => coerce(arg, getExpectedSort(decl, index, domainSort)))
|
|
1373
|
+
: args;
|
|
1374
|
+
for (let index = 0; index < coerced.length; index++) {
|
|
1375
|
+
const sort = manager.sorts[coerced[index]];
|
|
1376
|
+
if (sort !== getExpectedSort(decl, index, domainSort))
|
|
1377
|
+
throw new SmtParseError(
|
|
1378
|
+
`Sort mismatch at argument #${index + 1} for function ${formatDecl(decl, args.length, domainSort)} supplied sort is ${manager.getSortName(sort)}`,
|
|
1379
|
+
at,
|
|
1380
|
+
);
|
|
1381
|
+
}
|
|
1382
|
+
// Z3 keeps xor, / and div of fewer than two arguments until its rewriters make false or the
|
|
1383
|
+
// argument of them, which those of this port do not.
|
|
1384
|
+
if (
|
|
1385
|
+
coerced.length < 2 &&
|
|
1386
|
+
domainSort !== -1 &&
|
|
1387
|
+
(decl === OP_XOR || LEFT_ASSOCIATIVE_OPS.has(decl))
|
|
1388
|
+
)
|
|
1389
|
+
return coerced[0] ?? manager.falseExpr;
|
|
262
1390
|
return manager.mkApp(
|
|
263
1391
|
decl,
|
|
264
|
-
|
|
265
|
-
|
|
1392
|
+
coerced,
|
|
1393
|
+
domainSort === -1 ? manager.funcDecls[decl].range : (RANGES.get(decl) ?? domainSort),
|
|
266
1394
|
);
|
|
267
1395
|
};
|
|
268
1396
|
|
|
269
|
-
|
|
270
|
-
|
|
1397
|
+
// ast_manager::mk_app.
|
|
1398
|
+
const mkApp = (decl: number, args: number[], at: SExpr, domainSort = -1): number => {
|
|
1399
|
+
const isBuiltin = domainSort !== -1;
|
|
1400
|
+
const isAssociative = isBuiltin && ASSOCIATIVE_OPS.has(decl);
|
|
1401
|
+
const isLeftAssociative = isAssociative || (isBuiltin && LEFT_ASSOCIATIVE_OPS.has(decl));
|
|
1402
|
+
const isRightAssociative = isAssociative || (isBuiltin && decl === OP_IMPLIES);
|
|
1403
|
+
const isChainable = isBuiltin && CHAINABLE_DECLS.has(decl);
|
|
1404
|
+
if (
|
|
1405
|
+
getArity(decl, args.length, domainSort) !== args.length &&
|
|
1406
|
+
((!isLeftAssociative && !isRightAssociative && !isChainable) ||
|
|
1407
|
+
(args.length < 2 && manager.funcDecls[decl].family === FAMILY_BASIC && !isAssociative))
|
|
1408
|
+
) {
|
|
1409
|
+
const environment = { options: DEFAULT_PRINT_OPTIONS, isUsed, isClashing };
|
|
1410
|
+
// Z3 prints an error without the newlines that end it.
|
|
1411
|
+
throw new SmtParseError(
|
|
1412
|
+
`Wrong number of arguments (${args.length}) passed to function ${formatDecl(decl, args.length, domainSort)} ${args
|
|
1413
|
+
.map((arg) => `\narg: ${printExpr(manager, environment, arg)}\n`)
|
|
1414
|
+
.join("")}`.replace(/\n+$/, ""),
|
|
1415
|
+
at,
|
|
1416
|
+
);
|
|
1417
|
+
}
|
|
1418
|
+
if (args.length === 1 && isChainable) return manager.trueExpr;
|
|
1419
|
+
if (args.length <= 2 || (isBuiltin && FLAT_OPS.has(decl)))
|
|
1420
|
+
return mkAppCore(decl, args, at, domainSort);
|
|
1421
|
+
if (isRightAssociative)
|
|
1422
|
+
return args
|
|
1423
|
+
.slice(0, -2)
|
|
1424
|
+
.reduceRight(
|
|
1425
|
+
(right, left) => mkAppCore(decl, [left, right], at, domainSort),
|
|
1426
|
+
mkAppCore(decl, args.slice(-2), at, domainSort),
|
|
1427
|
+
);
|
|
1428
|
+
if (isLeftAssociative)
|
|
1429
|
+
return args
|
|
1430
|
+
.slice(2)
|
|
1431
|
+
.reduce(
|
|
1432
|
+
(left, right) => mkAppCore(decl, [left, right], at, domainSort),
|
|
1433
|
+
mkAppCore(decl, args.slice(0, 2), at, domainSort),
|
|
1434
|
+
);
|
|
1435
|
+
if (!isChainable) return mkAppCore(decl, args, at, domainSort);
|
|
271
1436
|
const pairs: number[] = [];
|
|
272
|
-
for (let index =
|
|
273
|
-
pairs.push(
|
|
274
|
-
return
|
|
1437
|
+
for (let index = 1; index < args.length; index++)
|
|
1438
|
+
pairs.push(mkAppCore(decl, [args[index - 1], args[index]], at, domainSort));
|
|
1439
|
+
return mkApp(OP_AND, pairs, at, SORT_BOOL);
|
|
275
1440
|
};
|
|
276
1441
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const parsePattern = (sexpr: SExpr) => {
|
|
283
|
-
if (!Array.isArray(sexpr) || sexpr.length === 0) return undefined;
|
|
284
|
-
const head = sexpr[0];
|
|
285
|
-
if (Array.isArray(head)) return manager.mkPattern(sexpr.map(parseTerm));
|
|
286
|
-
const decl = head === "true" || head === "false" ? -1 : funcs.get(head);
|
|
287
|
-
if (decl === -1 || (decl !== undefined && manager.funcDecls[decl].domain.length === 0))
|
|
288
|
-
return undefined;
|
|
289
|
-
return manager.mkPattern([parseTerm(sexpr)]);
|
|
1442
|
+
// ast_manager::mk_pattern.
|
|
1443
|
+
const mkPattern = (terms: number[], at: SExpr) => {
|
|
1444
|
+
if (terms.some((term) => manager.kinds[term] !== KIND_APP))
|
|
1445
|
+
throw new SmtParseError("patterns cannot be variables or quantifiers", at);
|
|
1446
|
+
return manager.mkPattern(terms);
|
|
290
1447
|
};
|
|
291
1448
|
|
|
292
1449
|
const isValidPattern = (pattern: number, numDecls: number) => {
|
|
@@ -311,181 +1468,1834 @@ export const parseSmtlib = (
|
|
|
311
1468
|
return manager.everyArg(pattern, isValid) && found.size === numDecls;
|
|
312
1469
|
};
|
|
313
1470
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
1471
|
+
// parse_sorted_vars: the variables are created the first declared with the largest index, and
|
|
1472
|
+
// the stack holds them. previousId is the identifier before the list.
|
|
1473
|
+
const bindSortedVars = (sexpr: SExpr | undefined, at: SExpr, previousId: string) => {
|
|
1474
|
+
if (sexpr?.kind !== "list")
|
|
1475
|
+
throw new SmtParseError(
|
|
1476
|
+
`invalid list of sorted variables, '(' expected got ${getLastId(sexpr) ?? previousId}`,
|
|
1477
|
+
sexpr ?? at,
|
|
1478
|
+
);
|
|
1479
|
+
const names: string[] = [];
|
|
1480
|
+
const varSorts: number[] = [];
|
|
1481
|
+
for (const [index, declaration] of sexpr.items.entries()) {
|
|
1482
|
+
if (declaration.kind !== "list")
|
|
1483
|
+
throw new SmtParseError(
|
|
1484
|
+
`invalid sorted variable, '(' expected got ${getLastId(declaration) ?? getLastIdBefore(sexpr.items, index) ?? previousId}`,
|
|
1485
|
+
declaration,
|
|
1486
|
+
);
|
|
1487
|
+
const name = declaration.items[0];
|
|
1488
|
+
if (!isSymbol(name))
|
|
1489
|
+
throw new SmtParseError(
|
|
1490
|
+
"invalid sorted variable, symbol expected",
|
|
1491
|
+
name ?? declaration.end,
|
|
1492
|
+
);
|
|
1493
|
+
names.push(name.text);
|
|
1494
|
+
varSorts.push(parseSort(declaration.items[1] ?? declaration.end, "invalid sorted variables"));
|
|
1495
|
+
const extra = declaration.items[2];
|
|
1496
|
+
if (extra !== undefined)
|
|
1497
|
+
throw new SmtParseError(
|
|
1498
|
+
`invalid sorted variable, ')' expected got ${getLastId(extra) ?? getLastId(declaration.items[1]) ?? name.text}`,
|
|
1499
|
+
extra,
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
beginScope();
|
|
1503
|
+
binders.depth += varSorts.length;
|
|
1504
|
+
for (const [index, sort] of varSorts.entries()) {
|
|
1505
|
+
const boundVar = manager.mkVar(varSorts.length - 1 - index, sort);
|
|
1506
|
+
pushRef(exprStack, boundVar);
|
|
1507
|
+
insertLocal(names[index], boundVar);
|
|
1508
|
+
}
|
|
1509
|
+
return { names, varSorts };
|
|
1510
|
+
};
|
|
1511
|
+
|
|
1512
|
+
const unbindSortedVars = (count: number) => {
|
|
1513
|
+
endScope();
|
|
1514
|
+
binders.depth -= count;
|
|
1515
|
+
};
|
|
1516
|
+
|
|
1517
|
+
// pop_quant_frame, up to the quantifier.
|
|
1518
|
+
const mkParsedQuantifier = (frame: QuantifierFrame) => {
|
|
1519
|
+
const list = frame.list;
|
|
1520
|
+
const numDecls = frame.varSorts.length;
|
|
1521
|
+
if (exprStack.length - frame.start !== numDecls + 1)
|
|
1522
|
+
throw new SmtParseError(
|
|
1523
|
+
"invalid quantified expression, syntax error: (forall|exists ((<symbol> <sort>)*) <expr>) expected",
|
|
1524
|
+
list.end,
|
|
1525
|
+
);
|
|
1526
|
+
let numKept = frame.patternStart;
|
|
1527
|
+
for (let index = frame.patternStart; index < patternStack.length; index++) {
|
|
1528
|
+
const pattern = patternStack[index];
|
|
1529
|
+
if (!isValidPattern(pattern, numDecls)) {
|
|
1530
|
+
if (!options.isIgnoringBadPatterns) throw new SmtParseError("invalid pattern", list.end);
|
|
1531
|
+
continue;
|
|
1532
|
+
}
|
|
353
1533
|
manager.incRef(pattern);
|
|
354
|
-
manager.decRef(
|
|
355
|
-
|
|
1534
|
+
manager.decRef(patternStack[numKept]);
|
|
1535
|
+
patternStack[numKept] = pattern;
|
|
356
1536
|
numKept++;
|
|
357
1537
|
}
|
|
358
|
-
|
|
359
|
-
const
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
1538
|
+
shrinkRefs(patternStack, numKept);
|
|
1539
|
+
const body = exprStack[exprStack.length - 1];
|
|
1540
|
+
if (frame.quantifierKind !== QUANTIFIER_LAMBDA && manager.sorts[body] !== SORT_BOOL)
|
|
1541
|
+
throw new SmtParseError("quantifier body must be a Boolean expression", list.end);
|
|
1542
|
+
if (patternStack.length > frame.patternStart && noPatternStack.length > frame.noPatternStart)
|
|
1543
|
+
throw new SmtParseError("simultaneous patterns and no-patterns not supported", list.end);
|
|
1544
|
+
return manager.mkQuantifier({
|
|
1545
|
+
kind: frame.quantifierKind,
|
|
1546
|
+
sorts: frame.varSorts,
|
|
1547
|
+
names: frame.names,
|
|
363
1548
|
body,
|
|
364
|
-
patterns,
|
|
365
|
-
|
|
366
|
-
|
|
1549
|
+
patterns: patternStack.slice(frame.patternStart),
|
|
1550
|
+
noPatterns: noPatternStack.slice(frame.noPatternStart),
|
|
1551
|
+
weight: frame.weight,
|
|
1552
|
+
qid: frame.qid,
|
|
1553
|
+
...(frame.skid === undefined ? {} : { skid: frame.skid }),
|
|
367
1554
|
});
|
|
368
|
-
for (const pattern of patterns) manager.decRef(pattern);
|
|
369
|
-
for (const boundVar of boundVars) manager.decRef(boundVar);
|
|
370
|
-
return quantifier;
|
|
371
1555
|
};
|
|
372
1556
|
|
|
373
|
-
const
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
1557
|
+
const expandMacro = (macro: Macro, args: number[]) =>
|
|
1558
|
+
replaceFreeVars(manager, macro.body, (_variable, index, shift) => {
|
|
1559
|
+
const arg = args[args.length - 1 - index];
|
|
1560
|
+
return shift === 0
|
|
1561
|
+
? arg
|
|
1562
|
+
: replaceFreeVars(manager, arg, (inner, innerIndex, innerShift) =>
|
|
1563
|
+
manager.mkVar(innerIndex + innerShift + shift, manager.sorts[inner]),
|
|
1564
|
+
);
|
|
1565
|
+
});
|
|
1566
|
+
|
|
1567
|
+
const isBvOpName = (name: string) =>
|
|
1568
|
+
hasBv &&
|
|
1569
|
+
BV_OP_NAMES.has(name) &&
|
|
1570
|
+
(!BV_EXTENSION_NAMES.has(name) || logic === undefined || BV_EXTENSION_LOGICS.has(logic));
|
|
1571
|
+
|
|
1572
|
+
const mkBvApp = (name: string, args: number[], indices: number[], at: SExpr) => {
|
|
1573
|
+
const op = BV_OP_NAMES.get(name) ?? -1;
|
|
1574
|
+
const signature = getBvSignature(
|
|
1575
|
+
manager,
|
|
1576
|
+
op,
|
|
1577
|
+
args.map((arg) => manager.sorts[arg]),
|
|
1578
|
+
indices,
|
|
1579
|
+
);
|
|
1580
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1581
|
+
const decl = manager.mkBvDecl(op, signature.name, indices, signature.domain, signature.range);
|
|
1582
|
+
return manager.mkApp(decl, args, signature.range);
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
const isFpaOpName = (name: string) => hasFpa && FPA_OP_NAMES.has(name);
|
|
1586
|
+
|
|
1587
|
+
// pb_decl_plugin::get_op_names and mk_func_decl.
|
|
1588
|
+
const isPbOpName = (name: string) =>
|
|
1589
|
+
PB_OP_NAMES.has(name) &&
|
|
1590
|
+
(logic === undefined || logic === "QF_FD" || logic === "ALL" || logic === "HORN");
|
|
1591
|
+
|
|
1592
|
+
const mkPbTerm = (name: string, args: number[], parameters: Rational[], at: SExpr) => {
|
|
1593
|
+
const op = PB_OP_NAMES.get(name) ?? -1;
|
|
1594
|
+
if (args.some((arg) => manager.sorts[arg] !== SORT_BOOL))
|
|
1595
|
+
throw new SmtParseError("invalid non-Boolean sort applied to Pseudo-Boolean relation", at);
|
|
1596
|
+
const isCardinality = op === OP_AT_MOST_K || op === OP_AT_LEAST_K;
|
|
1597
|
+
if (isCardinality && parameters.length !== 1)
|
|
1598
|
+
throw new SmtParseError("function expects one non-negative integer parameter", at);
|
|
1599
|
+
if (!isCardinality && parameters.length !== 1 + args.length)
|
|
1600
|
+
throw new SmtParseError("function expects arity+1 rational parameters", at);
|
|
1601
|
+
const decl = manager.mkPbDecl(
|
|
1602
|
+
op,
|
|
1603
|
+
parameters,
|
|
1604
|
+
args.map(() => SORT_BOOL),
|
|
1605
|
+
);
|
|
1606
|
+
return manager.mkApp(decl, args, SORT_BOOL);
|
|
1607
|
+
};
|
|
1608
|
+
|
|
1609
|
+
const mkFpaTerm = (name: string, args: number[], indices: number[], at: SExpr) => {
|
|
1610
|
+
const op = FPA_OP_NAMES.get(name) ?? -1;
|
|
1611
|
+
const getSignature = (innerArgs: number[]) => {
|
|
1612
|
+
const signature = getFpaSignature(
|
|
1613
|
+
manager,
|
|
1614
|
+
op,
|
|
1615
|
+
indices,
|
|
1616
|
+
innerArgs.map((arg) => manager.sorts[arg]),
|
|
1617
|
+
);
|
|
1618
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1619
|
+
return signature;
|
|
1620
|
+
};
|
|
1621
|
+
const signature = getSignature(args);
|
|
1622
|
+
if (signature.domain.length === args.length) return mkFpaAppOf(manager, signature, args);
|
|
1623
|
+
const pairs: number[] = [];
|
|
1624
|
+
for (let index = 0; index + 1 < args.length; index++) {
|
|
1625
|
+
const pair = [args[index], args[index + 1]];
|
|
1626
|
+
pairs.push(mkFpaAppOf(manager, getSignature(pair), pair));
|
|
379
1627
|
}
|
|
380
|
-
|
|
381
|
-
const body = parseTerm(list[2]);
|
|
382
|
-
scopes.pop();
|
|
383
|
-
return body;
|
|
1628
|
+
return mkApp(OP_AND, pairs, at, SORT_BOOL);
|
|
384
1629
|
};
|
|
385
1630
|
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
1631
|
+
const isSeqOpName = (name: string) =>
|
|
1632
|
+
hasSeq && (SEQ_OP_NAMES.has(name) || CHAR_OP_NAMES.has(name));
|
|
1633
|
+
|
|
1634
|
+
const mkSeqTerm = (
|
|
1635
|
+
name: string,
|
|
1636
|
+
args: number[],
|
|
1637
|
+
indices: number[],
|
|
1638
|
+
at: SExpr,
|
|
1639
|
+
range?: number,
|
|
1640
|
+
) => {
|
|
1641
|
+
const signature = getSeqSignature(
|
|
1642
|
+
manager,
|
|
1643
|
+
SEQ_OP_NAMES.get(name) ?? CHAR_OP_NAMES.get(name) ?? -1,
|
|
1644
|
+
indices,
|
|
1645
|
+
args.map((arg) => manager.sorts[arg]),
|
|
1646
|
+
range,
|
|
1647
|
+
);
|
|
1648
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1649
|
+
return mkSeqAppOf(manager, signature, args);
|
|
1650
|
+
};
|
|
1651
|
+
|
|
1652
|
+
const parseUpolynomial = (sexpr: SExpr): bigint[] => {
|
|
1653
|
+
if (sexpr.kind === "numeral") return UPOLYNOMIALS.set([BigInt(sexpr.text)]);
|
|
1654
|
+
if (sexpr.kind === "decimal") {
|
|
1655
|
+
const parts = sexpr.text.split(".");
|
|
1656
|
+
const value = Rational.of(BigInt(parts[0] + parts[1]), 10n ** BigInt(parts[1].length));
|
|
1657
|
+
if (!value.isInt())
|
|
1658
|
+
throw new SmtParseError(
|
|
1659
|
+
"invalid univariate polynomial, integer coefficient expected",
|
|
1660
|
+
sexpr,
|
|
408
1661
|
);
|
|
409
|
-
|
|
410
|
-
if (sexpr === "false") return manager.falseExpr;
|
|
411
|
-
const binding = lookup(sexpr);
|
|
412
|
-
if (binding?.kind === "var") return manager.mkVar(depth - 1 - binding.depth, binding.sort);
|
|
413
|
-
if (binding !== undefined)
|
|
414
|
-
return depth === binding.depth
|
|
415
|
-
? binding.expr
|
|
416
|
-
: replaceFreeVars(manager, binding.expr, (variable, index, shift) =>
|
|
417
|
-
manager.mkVar(index + shift + depth - binding.depth, manager.sorts[variable]),
|
|
418
|
-
);
|
|
419
|
-
return parseApp(sexpr, []);
|
|
1662
|
+
return UPOLYNOMIALS.set([value.num]);
|
|
420
1663
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const nameIndex = sexpr.indexOf(":named");
|
|
426
|
-
if (nameIndex !== -1) {
|
|
427
|
-
manager.incRef(named);
|
|
428
|
-
manager.mkMacro(expectSymbol(sexpr[nameIndex + 1]), named);
|
|
1664
|
+
if (sexpr.kind === "symbol") {
|
|
1665
|
+
if (sexpr.text !== "x")
|
|
1666
|
+
throw new SmtParseError("invalid univariate polynomial, variable 'x' expected", sexpr);
|
|
1667
|
+
return [0n, 1n];
|
|
429
1668
|
}
|
|
430
|
-
|
|
1669
|
+
if (sexpr.kind !== "list")
|
|
1670
|
+
throw new SmtParseError("invalid univariate polynomial, unexpected ", sexpr);
|
|
1671
|
+
const head = sexpr.items[0];
|
|
1672
|
+
if (!isSymbol(head))
|
|
1673
|
+
throw new SmtParseError("invalid univariate polynomial, symbol expected", sexpr);
|
|
1674
|
+
const operands = sexpr.items.slice(1);
|
|
1675
|
+
if (head.text === "^") {
|
|
1676
|
+
if (operands.length !== 2)
|
|
1677
|
+
throw new SmtParseError(
|
|
1678
|
+
"invalid univariate polynomial, '^' operator expects two arguments",
|
|
1679
|
+
sexpr,
|
|
1680
|
+
);
|
|
1681
|
+
const exponent = operands[1];
|
|
1682
|
+
if (exponent.kind !== "numeral" || BigInt(exponent.text) > 0xffffffffn)
|
|
1683
|
+
throw new SmtParseError(
|
|
1684
|
+
"invalid univariate polynomial, exponent must be an unsigned integer",
|
|
1685
|
+
exponent,
|
|
1686
|
+
);
|
|
1687
|
+
return UPOLYNOMIALS.pw(parseUpolynomial(operands[0]), Number(exponent.text));
|
|
1688
|
+
}
|
|
1689
|
+
const combine =
|
|
1690
|
+
head.text === "+"
|
|
1691
|
+
? UPOLYNOMIALS.add
|
|
1692
|
+
: head.text === "-"
|
|
1693
|
+
? UPOLYNOMIALS.sub
|
|
1694
|
+
: head.text === "*"
|
|
1695
|
+
? UPOLYNOMIALS.mul
|
|
1696
|
+
: undefined;
|
|
1697
|
+
if (combine === undefined)
|
|
1698
|
+
throw new SmtParseError(
|
|
1699
|
+
"invalid univariate polynomial, '+', '-', '^' or '*' expected",
|
|
1700
|
+
sexpr,
|
|
1701
|
+
);
|
|
1702
|
+
if (operands.length === 0)
|
|
1703
|
+
throw new SmtParseError(
|
|
1704
|
+
`invalid univariate polynomial, '${head.text}' operator expects at least one argument`,
|
|
1705
|
+
sexpr,
|
|
1706
|
+
);
|
|
1707
|
+
const first = parseUpolynomial(operands[0]);
|
|
1708
|
+
if (head.text === "-" && operands.length === 1) return UPOLYNOMIALS.neg(first);
|
|
1709
|
+
return operands
|
|
1710
|
+
.slice(1)
|
|
1711
|
+
.reduce(
|
|
1712
|
+
(result, operand) => combine.call(UPOLYNOMIALS, result, parseUpolynomial(operand)),
|
|
1713
|
+
first,
|
|
1714
|
+
);
|
|
1715
|
+
};
|
|
1716
|
+
|
|
1717
|
+
const parseRootObj = (list: SmtList) => {
|
|
1718
|
+
const polynomial = list.items[1] ?? list.end;
|
|
1719
|
+
const index = list.items[2] ?? list.end;
|
|
1720
|
+
if (index.kind !== "numeral")
|
|
1721
|
+
throw new SmtParseError("invalid root-obj, (unsigned) integer expected", index);
|
|
1722
|
+
if (BigInt(index.text) > 0xffffffffn)
|
|
1723
|
+
throw new SmtParseError(
|
|
1724
|
+
"invalid root-obj, index must fit in an unsigned machine integer",
|
|
1725
|
+
index,
|
|
1726
|
+
);
|
|
1727
|
+
if (BigInt(index.text) === 0n)
|
|
1728
|
+
throw new SmtParseError("invalid root-obj, index must be >= 1", index);
|
|
1729
|
+
if (list.items.length > 3)
|
|
1730
|
+
throw new SmtParseError("invalid root-obj, ')' expected", list.items[3]);
|
|
1731
|
+
const coeffs = parseUpolynomial(polynomial);
|
|
1732
|
+
const follower = list.follower ?? list.end;
|
|
1733
|
+
if (coeffs.length === 0)
|
|
1734
|
+
throw new SmtParseError(
|
|
1735
|
+
"invalid root object, polynomial must not be the zero polynomial",
|
|
1736
|
+
follower,
|
|
1737
|
+
);
|
|
1738
|
+
const roots = manager.am.isolateUnivariateRoots(coeffs);
|
|
1739
|
+
const root = roots[Number(index.text) - 1];
|
|
1740
|
+
if (root === undefined)
|
|
1741
|
+
throw new SmtParseError(
|
|
1742
|
+
"invalid root object, polynomial does have sufficient roots",
|
|
1743
|
+
follower,
|
|
1744
|
+
);
|
|
1745
|
+
return manager.mkAlgebraicNumeral(root);
|
|
431
1746
|
};
|
|
432
1747
|
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
1748
|
+
const isKnownOperator = (name: string) => BUILTIN_NAMES.has(name) || THEORY_OPERATOR.test(name);
|
|
1749
|
+
|
|
1750
|
+
// parse_indexed_identifier_core.
|
|
1751
|
+
const getIndexedName = (list: SmtList) => {
|
|
1752
|
+
const name = list.items[1];
|
|
1753
|
+
if (!isSymbol(name))
|
|
1754
|
+
throw new SmtParseError("invalid indexed identifier, symbol expected", name ?? list.end);
|
|
1755
|
+
for (const index of list.items.slice(2)) {
|
|
1756
|
+
if (index.kind === "string")
|
|
1757
|
+
throw new SmtParseError(
|
|
1758
|
+
"invalid indexed identifier, integer, identifier or '(' expected",
|
|
1759
|
+
index,
|
|
1760
|
+
);
|
|
1761
|
+
// parse_func_decl_ref of an index that is a symbol.
|
|
438
1762
|
if (
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
head === "!"
|
|
1763
|
+
index.kind === "symbol" &&
|
|
1764
|
+
name.text !== "is" &&
|
|
1765
|
+
!isUsed(index.text) &&
|
|
1766
|
+
!macros.has(index.text)
|
|
444
1767
|
)
|
|
445
|
-
|
|
446
|
-
|
|
1768
|
+
throw new SmtParseError(
|
|
1769
|
+
`invalid function declaration reference, unknown function ${index.text}`,
|
|
1770
|
+
index,
|
|
1771
|
+
);
|
|
1772
|
+
}
|
|
1773
|
+
return name;
|
|
1774
|
+
};
|
|
1775
|
+
|
|
1776
|
+
// parse_qualified_identifier_core on (as <identifier> <sort>), up to the sort.
|
|
1777
|
+
const parseQualifierSort = (list: SmtList) => {
|
|
1778
|
+
const target = list.items[1];
|
|
1779
|
+
if (target === undefined || (target.kind !== "symbol" && target.kind !== "list"))
|
|
1780
|
+
throw new SmtParseError(
|
|
1781
|
+
`invalid (indexed) identifier, '(_' or symbol expected got ${getLastId(target) ?? "as"}`,
|
|
1782
|
+
target ?? list.end,
|
|
1783
|
+
);
|
|
1784
|
+
if (target.kind === "list") {
|
|
1785
|
+
if (!isSymbol(target.items[0], "_"))
|
|
1786
|
+
throw new SmtParseError(
|
|
1787
|
+
"invalid indexed identifier, '_' expected",
|
|
1788
|
+
target.items[0] ?? target.end,
|
|
1789
|
+
);
|
|
1790
|
+
getIndexedName(target);
|
|
1791
|
+
}
|
|
1792
|
+
const sort = parseSort(list.items[2] ?? list.end, "Invalid qualified identifier");
|
|
1793
|
+
const extra = list.items[3];
|
|
1794
|
+
if (extra !== undefined)
|
|
1795
|
+
throw new SmtParseError(
|
|
1796
|
+
`invalid qualified identifier, ')' expected got ${getLastId(extra) ?? getLastIdBefore(list.items, 3) ?? "as"}`,
|
|
1797
|
+
extra,
|
|
1798
|
+
);
|
|
1799
|
+
return sort;
|
|
1800
|
+
};
|
|
1801
|
+
|
|
1802
|
+
// parse_qualified_name on (_ <symbol> <index>*). Z3 makes the term once it has read past the
|
|
1803
|
+
// closing parenthesis, where at is.
|
|
1804
|
+
const parseIndexedTerm = (list: SmtList, at: SExpr, range?: number) => {
|
|
1805
|
+
const name = getIndexedName(list);
|
|
1806
|
+
const indices = list.items.slice(2);
|
|
1807
|
+
const local = locals.get(name.text);
|
|
1808
|
+
if (local !== undefined) {
|
|
1809
|
+
if (indices.length > 0)
|
|
1810
|
+
throw new SmtParseError("invalid indexed identifier, symbol is a local declaration", at);
|
|
1811
|
+
return getLocalTerm(local);
|
|
1812
|
+
}
|
|
1813
|
+
const value = /^bv([0-9]+)$/.exec(name.text);
|
|
1814
|
+
if (value !== null) {
|
|
1815
|
+
const width = indices[0];
|
|
1816
|
+
const digits =
|
|
1817
|
+
indices.length !== 1
|
|
1818
|
+
? "-1"
|
|
1819
|
+
: width.kind === "bitvector"
|
|
1820
|
+
? `0${width.text.slice(1)}`
|
|
1821
|
+
: width.kind === "numeral"
|
|
1822
|
+
? width.text
|
|
1823
|
+
: "-1";
|
|
1824
|
+
if (!isUnsigned(digits))
|
|
1825
|
+
throw new SmtParseError("invalid bit-vector constant, index expected", at);
|
|
1826
|
+
if (BigInt(digits) === 0n)
|
|
1827
|
+
throw new SmtParseError("bit-vector size must be greater than zero", at);
|
|
1828
|
+
return manager.mkBvNumeral(BigInt(value[1]), Number(BigInt(digits)));
|
|
1829
|
+
}
|
|
1830
|
+
const isNumbered = indices.every((index) => index.kind === "numeral");
|
|
1831
|
+
if (isNumbered && isSeqOpName(name.text))
|
|
1832
|
+
return mkSeqTerm(
|
|
1833
|
+
name.text,
|
|
1834
|
+
[],
|
|
1835
|
+
indices.map((index) => (index.kind === "list" ? 0 : Number(index.text))),
|
|
1836
|
+
at,
|
|
1837
|
+
);
|
|
1838
|
+
if (isNumbered && isFpaOpName(name.text)) {
|
|
1839
|
+
const signature = getFpaSignature(
|
|
1840
|
+
manager,
|
|
1841
|
+
FPA_OP_NAMES.get(name.text) ?? -1,
|
|
1842
|
+
indices.map((index) => (index.kind === "list" ? 0 : Number(index.text))),
|
|
1843
|
+
[],
|
|
1844
|
+
);
|
|
1845
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1846
|
+
return mkFpaAppOf(manager, signature, []);
|
|
1847
|
+
}
|
|
1848
|
+
if (isKnownOperator(name.text) || !indices.every(isSimpleIndex))
|
|
1849
|
+
throw new SmtUnsupportedError("indexed identifiers");
|
|
1850
|
+
return parseApp(name, [], at, [], range);
|
|
1851
|
+
};
|
|
1852
|
+
|
|
1853
|
+
// parse_qualified_name.
|
|
1854
|
+
const parseQualifiedName = (list: SmtList) => {
|
|
1855
|
+
const at = list.follower ?? list.end;
|
|
1856
|
+
if (isSymbol(list.items[0], "_")) return parseIndexedTerm(list, at);
|
|
1857
|
+
const sort = parseQualifierSort(list);
|
|
1858
|
+
const target = list.items[1];
|
|
1859
|
+
const local = isSymbol(target) ? locals.get(target.text) : undefined;
|
|
1860
|
+
if (isSymbol(target) && local === undefined)
|
|
1861
|
+
return hasArrays && target.text === "const"
|
|
1862
|
+
? mkArrayTerm(OP_CONST_ARRAY, [], [sort], at)
|
|
1863
|
+
: parseApp(target, [], at, [], sort);
|
|
1864
|
+
const term =
|
|
1865
|
+
local === undefined
|
|
1866
|
+
? parseIndexedTerm(
|
|
1867
|
+
expectList(target, "invalid indexed identifier, '_' expected", list),
|
|
1868
|
+
at,
|
|
1869
|
+
sort,
|
|
1870
|
+
)
|
|
1871
|
+
: getLocalTerm(local);
|
|
1872
|
+
if (manager.sorts[term] !== sort)
|
|
1873
|
+
throw new SmtParseError(
|
|
1874
|
+
`sort mismatch in qualified identifier, expected: ${manager.getSortName(sort)}, got: ${manager.getSortName(manager.sorts[term])}`,
|
|
1875
|
+
at,
|
|
1876
|
+
);
|
|
1877
|
+
return term;
|
|
1878
|
+
};
|
|
1879
|
+
|
|
1880
|
+
const mkArrayTerm = (op: number, inputArgs: number[], parameters: number[], at: SExpr) => {
|
|
1881
|
+
const signature = getArraySignature(
|
|
1882
|
+
manager,
|
|
1883
|
+
op,
|
|
1884
|
+
inputArgs.map((arg) => manager.sorts[arg]),
|
|
1885
|
+
parameters,
|
|
1886
|
+
);
|
|
1887
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1888
|
+
const args = inputArgs.map((arg, index) => coerce(arg, signature.domain[index]));
|
|
1889
|
+
const decl = manager.mkArrayDecl(
|
|
1890
|
+
op,
|
|
1891
|
+
signature.name,
|
|
1892
|
+
parameters,
|
|
1893
|
+
signature.domain,
|
|
1894
|
+
signature.range,
|
|
1895
|
+
);
|
|
1896
|
+
return manager.mkApp(decl, args, signature.range);
|
|
1897
|
+
};
|
|
1898
|
+
|
|
1899
|
+
const findFuncDecl = (sexpr: SmtToken) => {
|
|
1900
|
+
const decls = funcs.get(sexpr.text);
|
|
1901
|
+
if (decls !== undefined && decls.length > 1)
|
|
1902
|
+
throw new SmtParseError(
|
|
1903
|
+
`ambiguous function declaration reference, provide full signature to disambiguate (<symbol> (<sort>*) <sort>) ${sexpr.text}`,
|
|
1904
|
+
sexpr,
|
|
1905
|
+
);
|
|
1906
|
+
return decls?.[0];
|
|
1907
|
+
};
|
|
1908
|
+
|
|
1909
|
+
const throwAmbiguousConstant = (name: string, at: SExpr) => {
|
|
1910
|
+
throw new SmtParseError(
|
|
1911
|
+
`ambiguous constant reference, more than one constant with the same sort, use a qualified expression (as <symbol> <sort>) to disambiguate ${name}`,
|
|
1912
|
+
at,
|
|
1913
|
+
);
|
|
1914
|
+
};
|
|
1915
|
+
|
|
1916
|
+
// An exact signature wins, else the last one that matches once Int and Real coerce.
|
|
1917
|
+
const mkDeclaredApp = (
|
|
1918
|
+
name: string,
|
|
1919
|
+
decls: readonly number[],
|
|
1920
|
+
args: number[],
|
|
1921
|
+
range: number | undefined,
|
|
1922
|
+
at: SExpr,
|
|
1923
|
+
) => {
|
|
1924
|
+
if (args.length === 0 && range === undefined) {
|
|
1925
|
+
if (decls.length > 1) throwAmbiguousConstant(name, at);
|
|
1926
|
+
return manager.funcDecls[decls[0]].domain.length > 0
|
|
1927
|
+
? mkAsArray(manager, decls[0])
|
|
1928
|
+
: mkApp(decls[0], args, at);
|
|
1929
|
+
}
|
|
1930
|
+
const argSorts = args.map((arg) => manager.sorts[arg]);
|
|
1931
|
+
const candidates = decls.filter(
|
|
1932
|
+
(decl) =>
|
|
1933
|
+
(range === undefined || manager.funcDecls[decl].range === range) &&
|
|
1934
|
+
manager.funcDecls[decl].domain.length === args.length &&
|
|
1935
|
+
manager.funcDecls[decl].domain.every(
|
|
1936
|
+
(sort, index) =>
|
|
1937
|
+
sort === argSorts[index] || (isArithSort(sort) && isArithSort(argSorts[index])),
|
|
1938
|
+
),
|
|
1939
|
+
);
|
|
1940
|
+
const found = candidates.find((decl) => hasSignature(decl, argSorts, -1)) ?? candidates.at(-1);
|
|
1941
|
+
if (found !== undefined) return mkApp(found, args, at);
|
|
1942
|
+
if (args.length === 0 || range !== undefined) return undefined;
|
|
1943
|
+
if (decls.length > 1) throwAmbiguousConstant(name, at);
|
|
1944
|
+
const array = manager.funcDecls[decls[0]];
|
|
1945
|
+
if (array.domain.length > 0 || !manager.isArraySort(array.range)) return undefined;
|
|
1946
|
+
const parameters = manager.getSortParameters(array.range);
|
|
1947
|
+
if (
|
|
1948
|
+
parameters.length !== args.length + 1 ||
|
|
1949
|
+
argSorts.some((sort, index) => sort !== parameters[index])
|
|
1950
|
+
)
|
|
1951
|
+
return undefined;
|
|
1952
|
+
return mkArrayTerm(OP_SELECT, [mkApp(decls[0], [], at), ...args], [], at);
|
|
1953
|
+
};
|
|
1954
|
+
|
|
1955
|
+
const mkDatatypeInstanceApp = (name: string, args: number[], at: SExpr) => {
|
|
1956
|
+
const argSorts = args.map((arg) => manager.sorts[arg]);
|
|
1957
|
+
for (const def of manager.datatypeDefs.values())
|
|
1958
|
+
for (const [index, constructor] of def.constructors.entries()) {
|
|
1959
|
+
if (constructor.name !== name || constructor.accessors.length !== args.length) continue;
|
|
1960
|
+
const binding: number[] = [];
|
|
1961
|
+
const isMatch = constructor.accessors.every((accessor, position) => {
|
|
1962
|
+
if (accessor.range === argSorts[position]) return true;
|
|
1963
|
+
const parameter = manager.getTypeParameterIndex(accessor.range);
|
|
1964
|
+
if (parameter === -1) return false;
|
|
1965
|
+
if (binding[parameter] !== undefined && binding[parameter] !== argSorts[position])
|
|
1966
|
+
return false;
|
|
1967
|
+
binding[parameter] = argSorts[position];
|
|
1968
|
+
return true;
|
|
1969
|
+
});
|
|
1970
|
+
if (
|
|
1971
|
+
!isMatch ||
|
|
1972
|
+
binding.length !== def.parameters.length ||
|
|
1973
|
+
!def.parameters.every((_parameter, parameter) => binding[parameter] !== undefined)
|
|
1974
|
+
)
|
|
1975
|
+
continue;
|
|
1976
|
+
return mkApp(instantiateConstructor(manager, def, index, binding), args, at);
|
|
1977
|
+
}
|
|
1978
|
+
if (args.length !== 1 || !manager.isDatatypeSort(argSorts[0])) return undefined;
|
|
1979
|
+
for (const def of manager.datatypeDefs.values())
|
|
1980
|
+
for (const [index, constructor] of def.constructors.entries()) {
|
|
1981
|
+
const position = constructor.accessors.findIndex((accessor) => accessor.name === name);
|
|
1982
|
+
if (position === -1) continue;
|
|
1983
|
+
const sort = mkDatatypeSort(manager, def.name, [...manager.getSortParameters(argSorts[0])]);
|
|
1984
|
+
return mkApp(instantiateAccessor(manager, sort, index, position), args, at);
|
|
1985
|
+
}
|
|
1986
|
+
return undefined;
|
|
1987
|
+
};
|
|
1988
|
+
|
|
1989
|
+
const mkIsApp = (constructorName: SmtToken, args: number[], at: SExpr) => {
|
|
1990
|
+
const declared = findFuncDecl(constructorName);
|
|
1991
|
+
// datatype::decl::plugin::mk_is.
|
|
1992
|
+
if (declared !== undefined) {
|
|
1993
|
+
if (args.length !== 1) throw new SmtParseError(IS_PARAMETER_MESSAGE, at);
|
|
1994
|
+
if (!manager.isDatatypeSort(manager.sorts[args[0]]))
|
|
1995
|
+
throw new SmtParseError(
|
|
1996
|
+
"invalid parameter to datatype function u().is_datatype(domain[0])",
|
|
1997
|
+
at,
|
|
1998
|
+
);
|
|
1999
|
+
if (manager.sorts[args[0]] !== manager.funcDecls[declared].range)
|
|
2000
|
+
throw new SmtParseError("invalid sort argument passed to recognizer", at);
|
|
2001
|
+
if (manager.funcDecls[declared].op !== OP_DT_CONSTRUCTOR)
|
|
2002
|
+
throw new SmtParseError("expecting constructor argument to recognizer", at);
|
|
2003
|
+
return manager.mkApp(getConstructorIs(manager, declared), args, SORT_BOOL);
|
|
2004
|
+
}
|
|
2005
|
+
const argSort = args.length === 1 ? manager.sorts[args[0]] : -1;
|
|
2006
|
+
if (argSort === -1 || !manager.isDatatypeSort(argSort))
|
|
2007
|
+
throw new SmtParseError(IS_PARAMETER_MESSAGE, at);
|
|
2008
|
+
const constructor = getDatatypeConstructors(manager, argSort).find(
|
|
2009
|
+
(decl) => manager.funcDecls[decl].name === constructorName.text,
|
|
2010
|
+
);
|
|
2011
|
+
if (constructor === undefined)
|
|
2012
|
+
throw new SmtParseError(
|
|
2013
|
+
`invalid datatype recognizer, unknown constructor ${constructorName.text}`,
|
|
2014
|
+
at,
|
|
2015
|
+
);
|
|
2016
|
+
return mkApp(getConstructorIs(manager, constructor), args, at);
|
|
2017
|
+
};
|
|
2018
|
+
|
|
2019
|
+
const mkUpdateFieldApp = (accessorName: SmtToken, args: number[], at: SExpr) => {
|
|
2020
|
+
const accessor = findFuncDecl(accessorName);
|
|
2021
|
+
if (accessor === undefined)
|
|
2022
|
+
throw new SmtParseError(
|
|
2023
|
+
`invalid function declaration reference, unknown function ${accessorName.text}`,
|
|
2024
|
+
accessorName,
|
|
2025
|
+
);
|
|
2026
|
+
// datatype::decl::plugin::mk_update_field.
|
|
2027
|
+
if (args.length !== 2)
|
|
2028
|
+
throw new SmtParseError("invalid number of arguments for datatype field update", at);
|
|
2029
|
+
const funcDecl = manager.funcDecls[accessor];
|
|
2030
|
+
if (funcDecl.op !== OP_DT_ACCESSOR)
|
|
2031
|
+
throw new SmtParseError(
|
|
2032
|
+
"datatype field update requires a datatype accessor as the second argument",
|
|
2033
|
+
at,
|
|
2034
|
+
);
|
|
2035
|
+
if (funcDecl.domain[0] !== manager.sorts[args[0]])
|
|
2036
|
+
throw new SmtParseError("first argument to field update should be a data-type", at);
|
|
2037
|
+
if (funcDecl.range !== manager.sorts[args[1]])
|
|
2038
|
+
throw new SmtParseError(
|
|
2039
|
+
`second argument to field update should be ${manager.getSortName(funcDecl.range)} instead of ${manager.getSortName(manager.sorts[args[1]])}`,
|
|
2040
|
+
at,
|
|
2041
|
+
);
|
|
2042
|
+
return manager.mkApp(mkUpdateField(manager, accessor), args, funcDecl.domain[0]);
|
|
2043
|
+
};
|
|
2044
|
+
|
|
2045
|
+
const getUnknownConstantMessage = (name: string, args: number[], range: number | undefined) => {
|
|
2046
|
+
const argSorts =
|
|
2047
|
+
args.length === 0
|
|
2048
|
+
? ""
|
|
2049
|
+
: ` (${args.map((arg) => manager.getSortName(manager.sorts[arg])).join(" ")}) `;
|
|
2050
|
+
const declared = (funcs.get(name) ?? []).map((decl) => {
|
|
2051
|
+
const domain = manager.funcDecls[decl].domain.map((sort) => manager.getSortName(sort));
|
|
2052
|
+
return `\ndeclared: (declare-fun ${name} (${domain.join(" ")}) ${manager.getSortName(manager.funcDecls[decl].range)}) `;
|
|
2053
|
+
});
|
|
2054
|
+
return `unknown constant ${name}${argSorts}${range === undefined ? "" : `${manager.getSortName(range)} `}${declared.join("")}`;
|
|
2055
|
+
};
|
|
2056
|
+
|
|
2057
|
+
const parseFuncDeclRef = (sexpr: SExpr): MappedDecl => {
|
|
2058
|
+
if (sexpr.kind === "symbol") {
|
|
2059
|
+
const decl = findFuncDecl(sexpr);
|
|
2060
|
+
if (decl !== undefined) return { decl, ...manager.funcDecls[decl] };
|
|
2061
|
+
const unary = UNARY_BOOL_REFS.get(sexpr.text);
|
|
2062
|
+
if (unary !== undefined) return { decl: unary, domain: [SORT_BOOL], range: SORT_BOOL };
|
|
2063
|
+
const binary = BINARY_BOOL_REFS.get(sexpr.text);
|
|
2064
|
+
if (binary !== undefined)
|
|
2065
|
+
return { decl: binary, domain: [SORT_BOOL, SORT_BOOL], range: SORT_BOOL };
|
|
2066
|
+
throw new SmtUnsupportedError(`the declaration reference ${sexpr.text}`);
|
|
2067
|
+
}
|
|
2068
|
+
const name = sexpr.kind === "list" ? sexpr.items[0] : undefined;
|
|
2069
|
+
const domainList = sexpr.kind === "list" ? sexpr.items[1] : undefined;
|
|
2070
|
+
const rangeSort = sexpr.kind === "list" ? sexpr.items[2] : undefined;
|
|
2071
|
+
if (!isSymbol(name) || domainList?.kind !== "list" || rangeSort === undefined)
|
|
2072
|
+
throw new SmtUnsupportedError("the declaration reference");
|
|
2073
|
+
const domain = domainList.items.map((item) => parseSort(item, "Invalid function name"));
|
|
2074
|
+
const range = parseSort(rangeSort, "Invalid function name");
|
|
2075
|
+
const declared = funcs.get(name.text)?.find((decl) => hasSignature(decl, domain, range));
|
|
2076
|
+
if (declared !== undefined) return { decl: declared, domain, range };
|
|
2077
|
+
const builtin = CHAINABLE_OPS.get(name.text) ?? BUILTIN_OPS.get(name.text);
|
|
2078
|
+
if (builtin === undefined)
|
|
2079
|
+
throw new SmtUnsupportedError(`the declaration reference ${name.text}`);
|
|
2080
|
+
return { decl: builtin, domain, range: getBuiltinRange(builtin, domain) };
|
|
2081
|
+
};
|
|
2082
|
+
|
|
2083
|
+
const mkMapTerm = (mapped: MappedDecl, args: number[], at: SExpr) => {
|
|
2084
|
+
const signature = getMapSignature(
|
|
2085
|
+
manager,
|
|
2086
|
+
mapped,
|
|
2087
|
+
args.map((arg) => manager.sorts[arg]),
|
|
2088
|
+
);
|
|
2089
|
+
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
2090
|
+
const decl = manager.mkArrayDecl(
|
|
2091
|
+
OP_ARRAY_MAP,
|
|
2092
|
+
signature.name,
|
|
2093
|
+
[mapped.decl],
|
|
2094
|
+
signature.domain,
|
|
2095
|
+
signature.range,
|
|
2096
|
+
);
|
|
2097
|
+
return manager.mkApp(decl, args, signature.range);
|
|
2098
|
+
};
|
|
2099
|
+
|
|
2100
|
+
const getArrayOp = (name: string) =>
|
|
2101
|
+
hasArrays
|
|
2102
|
+
? (ARRAY_OPS.get(name) ?? (hasArrayExtensions ? ARRAY_EXTENSION_OPS.get(name) : undefined))
|
|
2103
|
+
: undefined;
|
|
2104
|
+
|
|
2105
|
+
const parseApp = (
|
|
2106
|
+
head: SmtToken,
|
|
2107
|
+
args: number[],
|
|
2108
|
+
at: SExpr,
|
|
2109
|
+
indices: number[] = [],
|
|
2110
|
+
range?: number,
|
|
2111
|
+
) => {
|
|
2112
|
+
const name = head.text;
|
|
2113
|
+
// A bound variable applied to arguments selects from the array it stands for, one at a time.
|
|
2114
|
+
const local = locals.get(name);
|
|
2115
|
+
if (args.length > 0 && local !== undefined)
|
|
2116
|
+
return args.reduce(
|
|
2117
|
+
(array, arg) => mkArrayTerm(OP_SELECT, [array, arg], [], at),
|
|
2118
|
+
getLocalTerm(local),
|
|
2119
|
+
);
|
|
2120
|
+
if (indices.length > 0)
|
|
2121
|
+
return isFpaOpName(name)
|
|
2122
|
+
? mkFpaTerm(name, args, indices, at)
|
|
2123
|
+
: isSeqOpName(name)
|
|
2124
|
+
? mkSeqTerm(name, args, indices, at)
|
|
2125
|
+
: mkBvApp(name, args, indices, at);
|
|
2126
|
+
const macro = macros.get(name);
|
|
2127
|
+
// macros_find: a macro takes its own sorts, and Int for Real or Real for Int. Any other
|
|
2128
|
+
// application leaves the name to a declaration.
|
|
2129
|
+
if (
|
|
2130
|
+
macro !== undefined &&
|
|
2131
|
+
macro.domain.length === args.length &&
|
|
2132
|
+
args.every(
|
|
2133
|
+
(arg, index) =>
|
|
2134
|
+
manager.sorts[arg] === macro.domain[index] ||
|
|
2135
|
+
(isArithSort(manager.sorts[arg]) && isArithSort(macro.domain[index])),
|
|
2136
|
+
)
|
|
2137
|
+
)
|
|
2138
|
+
return expandMacro(
|
|
2139
|
+
macro,
|
|
2140
|
+
args.map((arg, index) => coerce(arg, macro.domain[index])),
|
|
2141
|
+
);
|
|
2142
|
+
const decls = funcs.get(name);
|
|
2143
|
+
const declared = decls === undefined ? undefined : mkDeclaredApp(name, decls, args, range, at);
|
|
2144
|
+
if (declared !== undefined) return declared;
|
|
2145
|
+
// cmd_context::mk_app: a constant that starts with #f is a finite-field literal.
|
|
2146
|
+
if (args.length === 0 && name.startsWith("#f")) {
|
|
2147
|
+
if (!FF_LITERAL.test(name))
|
|
2148
|
+
throw new SmtParseError("invalid finite-field literal, expected #f<integer>m<prime>", at);
|
|
2149
|
+
if (!restricted.hasFf) throw new SmtParseError("logic does not support finite fields", at);
|
|
2150
|
+
throw new SmtUnsupportedError("finite-field literals");
|
|
2151
|
+
}
|
|
2152
|
+
if (isSeqOpName(name)) return mkSeqTerm(name, args, indices, at, range);
|
|
2153
|
+
if (range !== undefined)
|
|
2154
|
+
throw new SmtParseError(getUnknownConstantMessage(name, args, range), at);
|
|
2155
|
+
if (isBvOpName(name)) return mkBvApp(name, args, indices, at);
|
|
2156
|
+
if (isFpaOpName(name)) return mkFpaTerm(name, args, indices, at);
|
|
2157
|
+
const arrayOp = getArrayOp(name);
|
|
2158
|
+
if (arrayOp !== undefined) return mkArrayTerm(arrayOp, args, [], at);
|
|
2159
|
+
const builtin = (logic === undefined ? LOGIC_FREE_ALIASES.get(name) : undefined) ?? name;
|
|
2160
|
+
const isTranscendental = TRANSCENDENTALS.has(builtin) && hasTranscendentals(logic);
|
|
2161
|
+
if (isTranscendental && args.length === 0 && builtin === "pi") return manager.piExpr;
|
|
2162
|
+
if (isTranscendental && args.length === 0 && builtin === "euler") return manager.eulerExpr;
|
|
2163
|
+
const transcendentalOp = isTranscendental ? TRANSCENDENTAL_OPS.get(builtin) : undefined;
|
|
2164
|
+
if (isTranscendental && transcendentalOp === undefined)
|
|
2165
|
+
throw new SmtUnsupportedError(`the operator ${name}`);
|
|
2166
|
+
const named =
|
|
2167
|
+
builtin === "~"
|
|
2168
|
+
? OP_UMINUS
|
|
2169
|
+
: (CHAINABLE_OPS.get(builtin) ?? BUILTIN_OPS.get(builtin) ?? transcendentalOp);
|
|
2170
|
+
if (
|
|
2171
|
+
args.length === 0 &&
|
|
2172
|
+
named !== undefined &&
|
|
2173
|
+
manager.funcDecls[named].family === FAMILY_ARITH
|
|
2174
|
+
)
|
|
2175
|
+
throw new SmtParseError("no arguments supplied to arithmetical operator", head);
|
|
2176
|
+
// arith_decl_plugin::fix_kind.
|
|
2177
|
+
const op = named === OP_SUB && args.length === 1 ? OP_UMINUS : named;
|
|
2178
|
+
const domainSort = op === undefined ? -1 : getDomainSort(op, args, at);
|
|
2179
|
+
if (op !== undefined && domainSort !== -1) return mkApp(op, args, at, domainSort);
|
|
2180
|
+
if (op === undefined && (THEORY_OPERATOR.test(name) || THEORY_OPERATORS.has(name)))
|
|
2181
|
+
throw new SmtUnsupportedError(`the operator ${name}`);
|
|
2182
|
+
const instance = mkDatatypeInstanceApp(name, args, at);
|
|
2183
|
+
if (instance !== undefined) return instance;
|
|
2184
|
+
throw new SmtParseError(getUnknownConstantMessage(name, args, undefined), at);
|
|
2185
|
+
};
|
|
2186
|
+
|
|
2187
|
+
const parseLeaf = (sexpr: SmtToken): number => {
|
|
2188
|
+
if (sexpr.kind === "numeral")
|
|
2189
|
+
return restricted.isNumeralAsReal
|
|
2190
|
+
? manager.mkRealNumeral(Rational.of(BigInt(sexpr.text), 1n))
|
|
2191
|
+
: manager.mkNumeral(BigInt(sexpr.text));
|
|
2192
|
+
if (sexpr.kind === "decimal") {
|
|
2193
|
+
const parts = sexpr.text.split(".");
|
|
2194
|
+
return manager.mkRealNumeral(
|
|
2195
|
+
Rational.of(BigInt(parts[0] + parts[1]), 10n ** BigInt(parts[1].length)),
|
|
2196
|
+
);
|
|
2197
|
+
}
|
|
2198
|
+
if (sexpr.kind === "bitvector") {
|
|
2199
|
+
const isBinary = sexpr.text[1] === "b";
|
|
2200
|
+
const digits = sexpr.text.slice(2);
|
|
2201
|
+
return manager.mkBvNumeral(
|
|
2202
|
+
BigInt(`0${isBinary ? "b" : "x"}${digits}`),
|
|
2203
|
+
digits.length * (isBinary ? 1 : 4),
|
|
2204
|
+
);
|
|
2205
|
+
}
|
|
2206
|
+
if (sexpr.kind === "string") return manager.mkString(parseZstring(sexpr.text, manager.maxChar));
|
|
2207
|
+
if (sexpr.kind === "symbol") {
|
|
2208
|
+
const local = locals.get(sexpr.text);
|
|
2209
|
+
if (local !== undefined) return getLocalTerm(local);
|
|
2210
|
+
if (sexpr.text === "true") return manager.trueExpr;
|
|
2211
|
+
if (sexpr.text === "false") return manager.falseExpr;
|
|
2212
|
+
return parseApp(sexpr, [], sexpr);
|
|
2213
|
+
}
|
|
2214
|
+
throw new SmtParseError(
|
|
2215
|
+
sexpr.kind === "keyword"
|
|
2216
|
+
? "invalid expression, unexpected keyword"
|
|
2217
|
+
: "invalid expression, unexpected ')'",
|
|
2218
|
+
sexpr,
|
|
2219
|
+
);
|
|
2220
|
+
};
|
|
2221
|
+
|
|
2222
|
+
// name_expr and cmd_context::insert of a named term. Z3 reports an error at the token after the
|
|
2223
|
+
// name.
|
|
2224
|
+
const nameExpr = (expr: number, name: string, at: SExpr) => {
|
|
2225
|
+
if (manager.freeVarBounds[expr] > 0)
|
|
2226
|
+
throw new SmtParseError("invalid named expression, expression contains free variables", at);
|
|
2227
|
+
if (macros.get(name)?.domain.length === 0)
|
|
2228
|
+
throw new SmtParseError("named expression already defined", at);
|
|
2229
|
+
if ((funcs.get(name) ?? []).some((decl) => hasSignature(decl, [], manager.sorts[expr])))
|
|
2230
|
+
throw new SmtParseError(
|
|
2231
|
+
`invalid named expression, declaration already defined with this name ${name}`,
|
|
2232
|
+
at,
|
|
2233
|
+
);
|
|
2234
|
+
manager.incRef(expr);
|
|
2235
|
+
lastNamed.name = name;
|
|
2236
|
+
lastNamed.expr = expr;
|
|
2237
|
+
manager.mkMacro(name, expr);
|
|
2238
|
+
declare(macros, name, { domain: [], range: manager.sorts[expr], body: expr });
|
|
2239
|
+
};
|
|
2240
|
+
|
|
2241
|
+
// push_app_frame's parse_qualified_identifier_core, on a head that is (_ <symbol> <index>*) or
|
|
2242
|
+
// (as <identifier> <sort>).
|
|
2243
|
+
const parseQualifiedHead = (list: SmtList, head: SmtList): QualifiedHead => {
|
|
2244
|
+
if (isSymbol(head.items[0], "as")) {
|
|
2245
|
+
const range = parseQualifierSort(head);
|
|
2246
|
+
const target = head.items[1];
|
|
2247
|
+
if (!isSymbol(target)) throw new SmtUnsupportedError("indexed and qualified identifiers");
|
|
2248
|
+
return {
|
|
2249
|
+
head: target,
|
|
2250
|
+
indices: [],
|
|
2251
|
+
build:
|
|
2252
|
+
hasArrays && target.text === "const"
|
|
2253
|
+
? (args) => mkArrayTerm(OP_CONST_ARRAY, args, [range], list.end)
|
|
2254
|
+
: (args) => parseApp(target, args, list.end, [], range),
|
|
2255
|
+
};
|
|
2256
|
+
}
|
|
2257
|
+
const name = getIndexedName(head);
|
|
2258
|
+
const indices = head.items.slice(2);
|
|
2259
|
+
const indexed = indices[0];
|
|
2260
|
+
if (hasArrays && hasArrayExtensions && name.text === "map" && indices.length === 1) {
|
|
2261
|
+
const mapped = parseFuncDeclRef(indexed);
|
|
2262
|
+
return { head: name, indices: [], build: (args) => mkMapTerm(mapped, args, list.end) };
|
|
2263
|
+
}
|
|
2264
|
+
if ((name.text === "is" || name.text === "update-field") && indices.length === 1) {
|
|
2265
|
+
if (!isSymbol(indexed)) throw new SmtUnsupportedError("indexed and qualified identifiers");
|
|
2266
|
+
return {
|
|
2267
|
+
head: name,
|
|
2268
|
+
indices: [],
|
|
2269
|
+
build: (args) =>
|
|
2270
|
+
name.text === "is"
|
|
2271
|
+
? mkIsApp(indexed, args, list.end)
|
|
2272
|
+
: mkUpdateFieldApp(indexed, args, list.end),
|
|
2273
|
+
};
|
|
2274
|
+
}
|
|
2275
|
+
const isNumbered = indices.every((index) => index.kind === "numeral");
|
|
2276
|
+
if (isNumbered && isPbOpName(name.text)) {
|
|
2277
|
+
const parameters = indices.map((index) =>
|
|
2278
|
+
Rational.of(BigInt(index.kind === "list" ? 0 : index.text)),
|
|
2279
|
+
);
|
|
2280
|
+
return {
|
|
2281
|
+
head: name,
|
|
2282
|
+
indices: [],
|
|
2283
|
+
build: (args) => mkPbTerm(name.text, args, parameters, list.end),
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
if (isNumbered && (isBvOpName(name.text) || isFpaOpName(name.text) || isSeqOpName(name.text)))
|
|
2287
|
+
return {
|
|
2288
|
+
head: name,
|
|
2289
|
+
indices: indices.map((index) => (index.kind === "list" ? 0 : Number(index.text))),
|
|
2290
|
+
};
|
|
2291
|
+
// cmd_context::mk_app finds a name that no theory knows among the declarations, whatever its
|
|
2292
|
+
// indices are.
|
|
2293
|
+
if (isKnownOperator(name.text) || !indices.every(isSimpleIndex))
|
|
2294
|
+
throw new SmtUnsupportedError("indexed and qualified identifiers");
|
|
2295
|
+
return { head: name, indices: [] };
|
|
2296
|
+
};
|
|
2297
|
+
|
|
2298
|
+
// parse_expr. Z3 leaves the term on its stack until the command ends. A tail term is one whose
|
|
2299
|
+
// value is the value of the whole: it stays off the stack, in parsed, so that the caller of
|
|
2300
|
+
// parseTerm can have a term that nothing else holds without a reference of the parser's.
|
|
2301
|
+
const runTerm = (sexpr: SExpr, isRootTail: boolean) => {
|
|
2302
|
+
const root = sexpr.kind === "list" ? sexpr.root : undefined;
|
|
2303
|
+
if (root === undefined || root === parsed.root) protect(floating);
|
|
2304
|
+
else {
|
|
2305
|
+
releasePending();
|
|
2306
|
+
shrink(floating, 0);
|
|
2307
|
+
parsed.root = root;
|
|
2308
|
+
}
|
|
2309
|
+
protect(unrooted);
|
|
2310
|
+
const frames: TermFrame[] = [];
|
|
2311
|
+
// The value of a frame replaces what the frame left on the stack. Z3 holds it in an expr_ref
|
|
2312
|
+
// meanwhile.
|
|
2313
|
+
const deliver = (expr: number, isTail: boolean, start: number) => {
|
|
2314
|
+
const isOwned = !isTail || manager.getRefCount(expr) > 0;
|
|
2315
|
+
if (isOwned) manager.incRef(expr);
|
|
2316
|
+
shrinkRefs(exprStack, start);
|
|
2317
|
+
if (!isTail) {
|
|
2318
|
+
exprStack.push(expr);
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
parsed.expr = expr;
|
|
2322
|
+
parsed.isOwned = isOwned;
|
|
2323
|
+
};
|
|
2324
|
+
// push_pattern_frame.
|
|
2325
|
+
const enterPattern = (value: SExpr, frame: AttributeFrame) => {
|
|
2326
|
+
if (value.kind !== "list") {
|
|
2327
|
+
if (!options.isIgnoringBadPatterns)
|
|
2328
|
+
throw new SmtParseError("invalid pattern, '(' expected", value);
|
|
2329
|
+
// Z3 skips a bad pattern as an s-expression, which cannot start with ')'.
|
|
2330
|
+
if (value.kind === ")")
|
|
2331
|
+
throw new SmtParseError("invalid s-expression, unexpected ')'", value);
|
|
2332
|
+
frame.next++;
|
|
2333
|
+
exprStack.push(-1);
|
|
2334
|
+
return;
|
|
2335
|
+
}
|
|
2336
|
+
frame.next++;
|
|
2337
|
+
const first = value.items[0];
|
|
2338
|
+
if (first === undefined) {
|
|
2339
|
+
exprStack.push(-1);
|
|
2340
|
+
return;
|
|
2341
|
+
}
|
|
2342
|
+
if (first.kind === "list") {
|
|
2343
|
+
frames.push({ kind: "pattern", list: value, start: exprStack.length, next: 0 });
|
|
2344
|
+
return;
|
|
2345
|
+
}
|
|
2346
|
+
const decl = first.text === "true" || first.text === "false" ? -1 : getFirstFunc(first.text);
|
|
2347
|
+
if (decl === -1 || (decl !== undefined && manager.funcDecls[decl].domain.length === 0)) {
|
|
2348
|
+
if (!options.isIgnoringBadPatterns)
|
|
2349
|
+
throw new SmtParseError("invalid constant pattern", first);
|
|
2350
|
+
exprStack.push(-1);
|
|
2351
|
+
return;
|
|
2352
|
+
}
|
|
2353
|
+
if (first.kind !== "symbol")
|
|
2354
|
+
throw new SmtParseError("invalid pattern, '(' or identifier expected", first);
|
|
2355
|
+
frames.push({
|
|
2356
|
+
kind: "application",
|
|
2357
|
+
list: value,
|
|
2358
|
+
head: first,
|
|
2359
|
+
indices: [],
|
|
2360
|
+
isTail: false,
|
|
2361
|
+
start: exprStack.length,
|
|
2362
|
+
next: 1,
|
|
2363
|
+
});
|
|
2364
|
+
};
|
|
2365
|
+
const enter = (current: SExpr, isTail: boolean) => {
|
|
2366
|
+
if (current.kind !== "list") {
|
|
2367
|
+
deliver(parseLeaf(current), isTail, exprStack.length);
|
|
2368
|
+
return;
|
|
2369
|
+
}
|
|
2370
|
+
const head = current.items[0];
|
|
2371
|
+
if (head === undefined || (head.kind !== "symbol" && head.kind !== "list"))
|
|
2372
|
+
throw new SmtParseError("invalid expression, '(' or symbol expected", head ?? current.end);
|
|
2373
|
+
if (head.kind === "list") {
|
|
2374
|
+
// push_app_frame: Z3 reads a head that is neither indexed nor qualified as a term.
|
|
2375
|
+
if (isSymbol(head.items[0], "lambda"))
|
|
2376
|
+
throw new SmtUnsupportedError("indexed and qualified identifiers");
|
|
2377
|
+
frames.push(
|
|
2378
|
+
isSymbol(head.items[0], "_") || isSymbol(head.items[0], "as")
|
|
2379
|
+
? {
|
|
2380
|
+
kind: "application",
|
|
2381
|
+
list: current,
|
|
2382
|
+
...parseQualifiedHead(current, head),
|
|
2383
|
+
isTail,
|
|
2384
|
+
start: exprStack.length,
|
|
2385
|
+
next: 1,
|
|
2386
|
+
}
|
|
2387
|
+
: { kind: "selection", list: current, isTail, start: exprStack.length, next: 0 },
|
|
2388
|
+
);
|
|
2389
|
+
return;
|
|
2390
|
+
}
|
|
2391
|
+
if (head.text === "_" || head.text === "as") {
|
|
2392
|
+
deliver(parseQualifiedName(current), isTail, exprStack.length);
|
|
2393
|
+
return;
|
|
2394
|
+
}
|
|
2395
|
+
if (head.text === "root-obj") {
|
|
2396
|
+
deliver(parseRootObj(current), isTail, exprStack.length);
|
|
2397
|
+
return;
|
|
2398
|
+
}
|
|
2399
|
+
if (head.text === "match") throw new SmtUnsupportedError("the expression match");
|
|
2400
|
+
if (head.text === "let") {
|
|
2401
|
+
const bindings = current.items[1];
|
|
2402
|
+
if (bindings?.kind !== "list")
|
|
2403
|
+
throw new SmtParseError(
|
|
2404
|
+
`invalid let declaration, '(' expected got ${getLastId(bindings) ?? "let"}`,
|
|
2405
|
+
bindings ?? current.end,
|
|
2406
|
+
);
|
|
2407
|
+
frames.push({
|
|
2408
|
+
kind: "let",
|
|
2409
|
+
list: current,
|
|
2410
|
+
bindings,
|
|
2411
|
+
names: [],
|
|
2412
|
+
isInDecls: true,
|
|
2413
|
+
isTail,
|
|
2414
|
+
start: exprStack.length,
|
|
2415
|
+
next: 0,
|
|
2416
|
+
});
|
|
2417
|
+
return;
|
|
2418
|
+
}
|
|
2419
|
+
if (head.text === "!") {
|
|
2420
|
+
const parent = frames[frames.length - 1];
|
|
2421
|
+
frames.push({
|
|
2422
|
+
kind: "attribute",
|
|
2423
|
+
list: current,
|
|
2424
|
+
quantifier: parent?.kind === "quantifier" ? parent : undefined,
|
|
2425
|
+
lastKeyword: "",
|
|
2426
|
+
isTail,
|
|
2427
|
+
start: exprStack.length,
|
|
2428
|
+
next: 1,
|
|
2429
|
+
});
|
|
2430
|
+
return;
|
|
2431
|
+
}
|
|
2432
|
+
const quantifierKind = QUANTIFIER_KINDS.get(head.text);
|
|
2433
|
+
if (quantifierKind === undefined) {
|
|
2434
|
+
frames.push({
|
|
2435
|
+
kind: "application",
|
|
2436
|
+
list: current,
|
|
2437
|
+
head,
|
|
2438
|
+
indices: [],
|
|
2439
|
+
isTail,
|
|
2440
|
+
start: exprStack.length,
|
|
2441
|
+
next: 1,
|
|
2442
|
+
});
|
|
2443
|
+
return;
|
|
2444
|
+
}
|
|
2445
|
+
// push_quant_frame.
|
|
2446
|
+
const start = exprStack.length;
|
|
2447
|
+
const bound = bindSortedVars(current.items[1], current.end, head.text);
|
|
2448
|
+
if (bound.varSorts.length === 0)
|
|
2449
|
+
throw new SmtParseError(
|
|
2450
|
+
"invalid quantifier, list of sorted variables is empty",
|
|
2451
|
+
current.items[2] ?? current.end,
|
|
2452
|
+
);
|
|
2453
|
+
frames.push({
|
|
2454
|
+
kind: "quantifier",
|
|
2455
|
+
list: current,
|
|
2456
|
+
quantifierKind,
|
|
2457
|
+
names: bound.names,
|
|
2458
|
+
varSorts: bound.varSorts,
|
|
2459
|
+
weight: 1,
|
|
2460
|
+
qid: "",
|
|
2461
|
+
skid: undefined,
|
|
2462
|
+
isTail,
|
|
2463
|
+
start,
|
|
2464
|
+
patternStart: patternStack.length,
|
|
2465
|
+
noPatternStart: noPatternStack.length,
|
|
2466
|
+
next: 2,
|
|
2467
|
+
});
|
|
447
2468
|
};
|
|
448
|
-
enter(sexpr);
|
|
2469
|
+
enter(sexpr, isRootTail);
|
|
449
2470
|
while (frames.length > 0) {
|
|
450
2471
|
const frame = frames[frames.length - 1];
|
|
451
|
-
if (frame.
|
|
452
|
-
|
|
2472
|
+
if (frame.kind === "let") {
|
|
2473
|
+
if (frame.isInDecls) {
|
|
2474
|
+
const declaration = frame.bindings.items[frame.next];
|
|
2475
|
+
if (declaration !== undefined) {
|
|
2476
|
+
// push_let_decl_frame.
|
|
2477
|
+
if (declaration.kind !== "list")
|
|
2478
|
+
throw new SmtParseError(
|
|
2479
|
+
`invalid let declaration, '(' expected got ${getLastId(declaration) ?? getLastIdBefore(frame.bindings.items, frame.next) ?? "let"}`,
|
|
2480
|
+
declaration,
|
|
2481
|
+
);
|
|
2482
|
+
const name = declaration.items[0];
|
|
2483
|
+
if (!isSymbol(name)) throw new SmtParseError(LET_MESSAGE, name ?? declaration.end);
|
|
2484
|
+
frame.names.push(name.text);
|
|
2485
|
+
frame.next++;
|
|
2486
|
+
frames.push({ kind: "let-decl", list: declaration, next: 1 });
|
|
2487
|
+
continue;
|
|
2488
|
+
}
|
|
2489
|
+
// pop_let_frame at the end of the declarations.
|
|
2490
|
+
if (frame.names.length !== exprStack.length - frame.start)
|
|
2491
|
+
throw new SmtParseError("malformed let expression", frame.bindings.end);
|
|
2492
|
+
beginScope();
|
|
2493
|
+
for (const [index, name] of frame.names.entries())
|
|
2494
|
+
insertLocal(name, exprStack[frame.start + index]);
|
|
2495
|
+
frame.isInDecls = false;
|
|
2496
|
+
frame.next = 2;
|
|
2497
|
+
continue;
|
|
2498
|
+
}
|
|
2499
|
+
const body = frame.list.items[frame.next];
|
|
2500
|
+
if (body !== undefined) {
|
|
2501
|
+
frame.next++;
|
|
2502
|
+
enter(body, frame.isTail && frame.next === frame.list.items.length);
|
|
2503
|
+
continue;
|
|
2504
|
+
}
|
|
2505
|
+
frames.pop();
|
|
2506
|
+
// Z3 takes the last expression on its stack, so a let without a body is its last binding.
|
|
2507
|
+
if (frame.isTail && frame.list.items.length > 2) shrinkRefs(exprStack, frame.start);
|
|
2508
|
+
else if (exprStack.length === frame.start)
|
|
2509
|
+
throw new SmtParseError("invalid let expression", frame.list.end);
|
|
2510
|
+
else deliver(exprStack[exprStack.length - 1], frame.isTail, frame.start);
|
|
2511
|
+
endScope();
|
|
2512
|
+
continue;
|
|
2513
|
+
}
|
|
2514
|
+
const items = frame.list.items;
|
|
2515
|
+
if (frame.kind === "attribute") {
|
|
2516
|
+
// consume_attributes.
|
|
2517
|
+
if (exprStack.length === frame.start) {
|
|
2518
|
+
if (frame.next >= items.length)
|
|
2519
|
+
throw new SmtParseError("invalid expression", frame.list.end);
|
|
2520
|
+
enter(items[frame.next++], false);
|
|
2521
|
+
continue;
|
|
2522
|
+
}
|
|
2523
|
+
// process_last_symbol.
|
|
2524
|
+
const last = exprStack[exprStack.length - 1];
|
|
2525
|
+
if (frame.lastKeyword === ":pattern") {
|
|
2526
|
+
const at = items[frame.next] ?? frame.list.end;
|
|
2527
|
+
if (last === -1 && !options.isIgnoringBadPatterns)
|
|
2528
|
+
throw new SmtParseError("invalid empty pattern", at);
|
|
2529
|
+
if (last !== -1)
|
|
2530
|
+
pushRef(
|
|
2531
|
+
patternStack,
|
|
2532
|
+
manager.kinds[last] === KIND_APP && manager.decls[last] === OP_PATTERN
|
|
2533
|
+
? last
|
|
2534
|
+
: mkPattern([last], at),
|
|
2535
|
+
);
|
|
2536
|
+
shrinkRefs(exprStack, exprStack.length - 1);
|
|
2537
|
+
} else if (frame.lastKeyword === ":no-pattern") {
|
|
2538
|
+
pushRef(noPatternStack, last);
|
|
2539
|
+
shrinkRefs(exprStack, exprStack.length - 1);
|
|
2540
|
+
}
|
|
2541
|
+
frame.lastKeyword = "";
|
|
2542
|
+
if (frame.next >= items.length) {
|
|
2543
|
+
frames.pop();
|
|
2544
|
+
if (frame.isTail) deliver(exprStack[exprStack.length - 1], true, frame.start);
|
|
2545
|
+
continue;
|
|
2546
|
+
}
|
|
2547
|
+
while (frame.next < items.length && frame.lastKeyword === "") {
|
|
2548
|
+
const keyword = items[frame.next];
|
|
2549
|
+
if (keyword.kind !== "keyword")
|
|
2550
|
+
throw new SmtParseError("invalid attributed expression, keyword expected", keyword);
|
|
2551
|
+
const value = items[frame.next + 1] ?? frame.list.end;
|
|
2552
|
+
const annotated = exprStack[exprStack.length - 1];
|
|
2553
|
+
const quantifier = frame.quantifier;
|
|
2554
|
+
if (keyword.text === ":named") {
|
|
2555
|
+
if (!isSymbol(value))
|
|
2556
|
+
throw new SmtParseError("invalid attribute value, symbol expected", value);
|
|
2557
|
+
nameExpr(annotated, value.text, items[frame.next + 2] ?? frame.list.end);
|
|
2558
|
+
frame.next += 2;
|
|
2559
|
+
} else if (keyword.text === ":lblpos" || keyword.text === ":lblneg") {
|
|
2560
|
+
if (!isSymbol(value))
|
|
2561
|
+
throw new SmtParseError("invalid attribute value, symbol expected", value);
|
|
2562
|
+
if (manager.sorts[annotated] !== SORT_BOOL)
|
|
2563
|
+
throw new SmtParseError(
|
|
2564
|
+
"invalid labeled expression, expression must have Bool sort",
|
|
2565
|
+
value,
|
|
2566
|
+
);
|
|
2567
|
+
deliver(
|
|
2568
|
+
manager.mkLabel(keyword.text === ":lblpos", [value.text], annotated),
|
|
2569
|
+
false,
|
|
2570
|
+
exprStack.length - 1,
|
|
2571
|
+
);
|
|
2572
|
+
frame.next += 2;
|
|
2573
|
+
} else if (!QUANTIFIER_ATTRIBUTES.has(keyword.text)) {
|
|
2574
|
+
// Z3 warns of an unknown attribute and skips its value as an s-expression.
|
|
2575
|
+
if (value.kind === ")")
|
|
2576
|
+
throw new SmtParseError("invalid s-expression, unexpected ')'", value);
|
|
2577
|
+
frame.next += 2;
|
|
2578
|
+
} else if (quantifier === undefined)
|
|
2579
|
+
throw new SmtParseError("invalid attribute, not in the scope of a quantifier", keyword);
|
|
2580
|
+
else if (keyword.text === ":weight") {
|
|
2581
|
+
// check_int, and curr_unsigned.
|
|
2582
|
+
if (value.kind !== "numeral")
|
|
2583
|
+
throw new SmtParseError("invalid weight attribute, integer expected", value);
|
|
2584
|
+
if (!isUnsigned(value.text))
|
|
2585
|
+
throw new SmtParseError(
|
|
2586
|
+
"invalid indexed identifier, index is too big to fit in an unsigned machine integer",
|
|
2587
|
+
value,
|
|
2588
|
+
);
|
|
2589
|
+
quantifier.weight = Number(value.text);
|
|
2590
|
+
frame.next += 2;
|
|
2591
|
+
} else if (keyword.text === ":skolemid" || keyword.text === ":qid") {
|
|
2592
|
+
if (!isSymbol(value))
|
|
2593
|
+
throw new SmtParseError("invalid attribute value, symbol expected", value);
|
|
2594
|
+
if (keyword.text === ":qid") quantifier.qid = value.text;
|
|
2595
|
+
else quantifier.skid = value.text;
|
|
2596
|
+
frame.next += 2;
|
|
2597
|
+
} else {
|
|
2598
|
+
frame.lastKeyword = keyword.text;
|
|
2599
|
+
frame.next++;
|
|
2600
|
+
if (keyword.text === ":pattern") enterPattern(value, frame);
|
|
2601
|
+
else if (value.kind === ")")
|
|
2602
|
+
throw new SmtParseError("invalid expression, unexpected input", value);
|
|
2603
|
+
else enter(items[frame.next++], false);
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
continue;
|
|
2607
|
+
}
|
|
2608
|
+
if (frame.next < items.length) {
|
|
2609
|
+
enter(items[frame.next++], false);
|
|
453
2610
|
continue;
|
|
454
2611
|
}
|
|
455
2612
|
frames.pop();
|
|
456
|
-
|
|
2613
|
+
if (frame.kind === "let-decl") continue;
|
|
2614
|
+
if (frame.kind === "pattern") {
|
|
2615
|
+
deliver(mkPattern(exprStack.slice(frame.start), frame.list.end), false, frame.start);
|
|
2616
|
+
continue;
|
|
2617
|
+
}
|
|
2618
|
+
if (frame.kind === "quantifier") {
|
|
2619
|
+
// pop_quant_frame.
|
|
2620
|
+
deliver(mkParsedQuantifier(frame), frame.isTail, frame.start);
|
|
2621
|
+
shrinkRefs(patternStack, frame.patternStart);
|
|
2622
|
+
shrinkRefs(noPatternStack, frame.noPatternStart);
|
|
2623
|
+
unbindSortedVars(frame.varSorts.length);
|
|
2624
|
+
continue;
|
|
2625
|
+
}
|
|
2626
|
+
// pop_app_frame.
|
|
2627
|
+
const args = exprStack.slice(frame.start);
|
|
2628
|
+
if (args.length < (frame.kind === "selection" ? 2 : 1))
|
|
2629
|
+
throw new SmtParseError("invalid function application, arguments missing", frame.list.end);
|
|
2630
|
+
deliver(
|
|
2631
|
+
frame.kind === "selection"
|
|
2632
|
+
? args
|
|
2633
|
+
.slice(1)
|
|
2634
|
+
.reduce(
|
|
2635
|
+
(array, arg) => mkArrayTerm(OP_SELECT, [array, arg], [], frame.list.end),
|
|
2636
|
+
args[0],
|
|
2637
|
+
)
|
|
2638
|
+
: (frame.build?.(args) ?? parseApp(frame.head, args, frame.list.end, frame.indices)),
|
|
2639
|
+
frame.isTail,
|
|
2640
|
+
frame.start,
|
|
2641
|
+
);
|
|
457
2642
|
}
|
|
458
|
-
return results[0];
|
|
459
2643
|
};
|
|
460
2644
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
const
|
|
464
|
-
|
|
2645
|
+
const parseTerm = (sexpr: SExpr) => {
|
|
2646
|
+
runTerm(sexpr, true);
|
|
2647
|
+
const expr = parsed.expr;
|
|
2648
|
+
parsed.expr = -1;
|
|
2649
|
+
if (!parsed.isOwned)
|
|
2650
|
+
(sexpr.kind === "list" && sexpr.root !== undefined ? floating : unrooted).push(expr);
|
|
2651
|
+
else if (manager.getRefCount(expr) > 1) manager.decRef(expr);
|
|
2652
|
+
else pending.push(expr);
|
|
2653
|
+
return expr;
|
|
2654
|
+
};
|
|
2655
|
+
|
|
2656
|
+
const parseDomain = (sexpr: SExpr | undefined, at: SExpr) => {
|
|
2657
|
+
const context = "Parsing function declaration. Expecting sort list '('";
|
|
2658
|
+
return expectList(sexpr, context, at).items.map((item) => parseSort(item, context));
|
|
2659
|
+
};
|
|
2660
|
+
|
|
2661
|
+
const parseSortParameters = (sexpr: SExpr | undefined, message: string, at: SExpr) =>
|
|
2662
|
+
expectList(sexpr, "invalid sort declaration, parameters missing", at).items.map(
|
|
2663
|
+
(item) => expectSymbol(item, message, item).text,
|
|
2664
|
+
);
|
|
2665
|
+
|
|
2666
|
+
const bindTypeParameters = (names: string[]) =>
|
|
2667
|
+
new Map(names.map((name, index) => [name, manager.mkTypeParameter(index)]));
|
|
2668
|
+
|
|
2669
|
+
const parseAccessorSort = (
|
|
2670
|
+
sexpr: SExpr,
|
|
2671
|
+
bindings: ReadonlyMap<string, number>,
|
|
2672
|
+
datatypeIndexes: ReadonlyMap<string, number>,
|
|
2673
|
+
missing: string[],
|
|
2674
|
+
) => {
|
|
2675
|
+
if (sexpr.kind === "list") return parseSort(sexpr, "", bindings);
|
|
2676
|
+
if (sexpr.kind !== "symbol")
|
|
2677
|
+
throw new SmtParseError("invalid sort, symbol, '_' or '(' expected", sexpr);
|
|
2678
|
+
if (sortDecls.has(sexpr.text) || bindings.has(sexpr.text))
|
|
2679
|
+
return parseSort(sexpr, "", bindings);
|
|
2680
|
+
const index = datatypeIndexes.get(sexpr.text);
|
|
2681
|
+
if (index !== undefined) return -1 - index;
|
|
2682
|
+
missing.push(sexpr.text);
|
|
2683
|
+
return SORT_BOOL;
|
|
2684
|
+
};
|
|
2685
|
+
|
|
2686
|
+
const parseConstructors = (
|
|
2687
|
+
items: SExpr[],
|
|
2688
|
+
bindings: ReadonlyMap<string, number>,
|
|
2689
|
+
datatypeIndexes: ReadonlyMap<string, number>,
|
|
2690
|
+
missing: string[],
|
|
2691
|
+
at: SExpr,
|
|
2692
|
+
): ConstructorDef[] => {
|
|
2693
|
+
if (items.length === 0)
|
|
2694
|
+
throw new SmtParseError(
|
|
2695
|
+
"invalid datatype declaration, datatype does not have any constructors",
|
|
2696
|
+
at,
|
|
2697
|
+
);
|
|
2698
|
+
return items.map((item) => {
|
|
2699
|
+
if (isSymbol(item)) return { name: item.text, recognizer: `is-${item.text}`, accessors: [] };
|
|
2700
|
+
const constructor = expectList(
|
|
2701
|
+
item,
|
|
2702
|
+
"invalid datatype declaration, '(' or ')' expected",
|
|
2703
|
+
item,
|
|
2704
|
+
);
|
|
2705
|
+
const name = expectSymbol(
|
|
2706
|
+
constructor.items[0],
|
|
2707
|
+
"invalid constructor declaration, symbol (constructor name) expected",
|
|
2708
|
+
constructor,
|
|
2709
|
+
).text;
|
|
2710
|
+
const accessors = constructor.items.slice(1).map((accessorItem) => {
|
|
2711
|
+
const accessor = expectList(
|
|
2712
|
+
accessorItem,
|
|
2713
|
+
"invalid datatype declaration, '(' or ')' expected",
|
|
2714
|
+
accessorItem,
|
|
2715
|
+
);
|
|
2716
|
+
const accessorName = expectSymbol(
|
|
2717
|
+
accessor.items[0],
|
|
2718
|
+
"invalid accessor declaration, symbol (accessor name) expected",
|
|
2719
|
+
accessor,
|
|
2720
|
+
).text;
|
|
2721
|
+
const range = parseAccessorSort(
|
|
2722
|
+
accessor.items[1] ?? accessor.end,
|
|
2723
|
+
bindings,
|
|
2724
|
+
datatypeIndexes,
|
|
2725
|
+
missing,
|
|
2726
|
+
);
|
|
2727
|
+
if (accessor.items.length > 2)
|
|
2728
|
+
throw new SmtParseError("invalid accessor declaration, ')' expected", accessor.items[2]);
|
|
2729
|
+
return { name: accessorName, range };
|
|
2730
|
+
});
|
|
2731
|
+
return { name, recognizer: `is-${name}`, accessors };
|
|
2732
|
+
});
|
|
2733
|
+
};
|
|
2734
|
+
|
|
2735
|
+
const parseDatatypeBody = (
|
|
2736
|
+
sexpr: SExpr | undefined,
|
|
2737
|
+
datatypeIndexes: ReadonlyMap<string, number>,
|
|
2738
|
+
missing: string[],
|
|
2739
|
+
at: SExpr,
|
|
2740
|
+
) => {
|
|
2741
|
+
const body = expectList(sexpr, "invalid datatype declaration, '(' expected", at);
|
|
2742
|
+
if (!isSymbol(body.items[0], "par"))
|
|
2743
|
+
return {
|
|
2744
|
+
parameters: [],
|
|
2745
|
+
constructors: parseConstructors(body.items, NO_BINDINGS, datatypeIndexes, missing, body),
|
|
2746
|
+
};
|
|
2747
|
+
const parameters = parseSortParameters(
|
|
2748
|
+
body.items[1],
|
|
2749
|
+
"invalid sort parameter, symbol or ')' expected",
|
|
2750
|
+
body,
|
|
2751
|
+
);
|
|
2752
|
+
const constructors = expectList(
|
|
2753
|
+
body.items[2],
|
|
2754
|
+
"invalid constructor declaration after par, '(' expected",
|
|
2755
|
+
body,
|
|
2756
|
+
);
|
|
2757
|
+
return {
|
|
2758
|
+
parameters: parameters.map((_name, index) => manager.mkTypeParameter(index)),
|
|
2759
|
+
constructors: parseConstructors(
|
|
2760
|
+
constructors.items,
|
|
2761
|
+
bindTypeParameters(parameters),
|
|
2762
|
+
datatypeIndexes,
|
|
2763
|
+
missing,
|
|
2764
|
+
constructors,
|
|
2765
|
+
),
|
|
2766
|
+
};
|
|
2767
|
+
};
|
|
2768
|
+
|
|
2769
|
+
const commitDatatypes = (defs: DatatypeDef[], missing: string[], at: SExpr, end: SExpr) => {
|
|
2770
|
+
if (missing.length > 0)
|
|
2771
|
+
throw new SmtParseError(`invalid datatype declaration, unknown sort '${missing[0]}'`, at);
|
|
2772
|
+
const sorts = mkDatatypes(manager, defs);
|
|
2773
|
+
if (typeof sorts === "string") throw new SmtParseError(sorts, end);
|
|
2774
|
+
for (const def of defs) undo.push(() => removeDatatype(manager, def.name));
|
|
2775
|
+
defs.forEach((def, index) => {
|
|
2776
|
+
if (def.parameters.length === 0) insertDatatypeDecls(sorts[index], end);
|
|
2777
|
+
});
|
|
2778
|
+
for (const def of defs) {
|
|
2779
|
+
const names = def.constructors.flatMap((constructor) =>
|
|
2780
|
+
constructor.accessors.map((accessor) => accessor.name),
|
|
2781
|
+
);
|
|
2782
|
+
const duplicated = names.find((name, index) => names.indexOf(name) !== index);
|
|
2783
|
+
if (duplicated !== undefined)
|
|
2784
|
+
throw new SmtParseError(
|
|
2785
|
+
`invalid datatype declaration, repeated accessor identifier '${duplicated}'`,
|
|
2786
|
+
at,
|
|
2787
|
+
);
|
|
2788
|
+
}
|
|
2789
|
+
};
|
|
2790
|
+
|
|
2791
|
+
const declareSortDecl = (name: string, decl: SortDecl, at: SExpr) => {
|
|
2792
|
+
if (sortDecls.has(name)) throw new SmtParseError(`sort already defined ${name}`, at);
|
|
2793
|
+
declare(sortDecls, name, decl);
|
|
2794
|
+
};
|
|
2795
|
+
|
|
2796
|
+
const declareDatatypes = (list: SmtList) => {
|
|
2797
|
+
const declaration = expectList(
|
|
2798
|
+
list.items[1],
|
|
2799
|
+
"invalid sort declaration, parameters missing",
|
|
2800
|
+
list,
|
|
2801
|
+
);
|
|
2802
|
+
const isSmt26 = declaration.items[0]?.kind === "list";
|
|
2803
|
+
const datatypeIndexes = new Map<string, number>();
|
|
2804
|
+
const names: string[] = [];
|
|
2805
|
+
const arities: number[] = [];
|
|
2806
|
+
const parameterNames: string[] = [];
|
|
2807
|
+
declaration.items.forEach((item, index) => {
|
|
2808
|
+
if (!isSmt26) {
|
|
2809
|
+
parameterNames.push(
|
|
2810
|
+
expectSymbol(item, "invalid sort parameter, symbol or ')' expected", item).text,
|
|
2811
|
+
);
|
|
2812
|
+
return;
|
|
2813
|
+
}
|
|
2814
|
+
const sortDeclaration = expectList(item, "invalid sort declaration, '(' expected", item);
|
|
2815
|
+
const name = expectSymbol(
|
|
2816
|
+
sortDeclaration.items[0],
|
|
2817
|
+
"invalid sort name, identified expected",
|
|
2818
|
+
sortDeclaration,
|
|
2819
|
+
).text;
|
|
2820
|
+
const arity = sortDeclaration.items[1];
|
|
2821
|
+
if (arity?.kind !== "numeral")
|
|
2822
|
+
throw new SmtParseError(
|
|
2823
|
+
"invalid sort declaration, arity expected",
|
|
2824
|
+
arity ?? sortDeclaration.end,
|
|
2825
|
+
);
|
|
2826
|
+
if (sortDeclaration.items.length > 2)
|
|
2827
|
+
throw new SmtParseError("invalid sort declaration, ')' expected", sortDeclaration.items[2]);
|
|
2828
|
+
datatypeIndexes.set(name, index);
|
|
2829
|
+
names.push(name);
|
|
2830
|
+
arities.push(Number(arity.text));
|
|
2831
|
+
declareSortDecl(
|
|
2832
|
+
name,
|
|
2833
|
+
{ kind: "datatype", name, arity: Number(arity.text), siblings: [], instances: new Map() },
|
|
2834
|
+
sortDeclaration,
|
|
2835
|
+
);
|
|
2836
|
+
});
|
|
2837
|
+
const bodies = expectList(list.items[2], "invalid datatype declaration, '(' expected", list);
|
|
2838
|
+
// A datatype of the older syntax may refer to any datatype of its declaration.
|
|
2839
|
+
if (!isSmt26)
|
|
2840
|
+
bodies.items.forEach((item, index) => {
|
|
2841
|
+
const datatype = expectList(
|
|
2842
|
+
item,
|
|
2843
|
+
"invalid datatype declaration, '(' or ')' expected",
|
|
2844
|
+
item,
|
|
2845
|
+
);
|
|
2846
|
+
const name = expectSymbol(
|
|
2847
|
+
datatype.items[0],
|
|
2848
|
+
"invalid datatype declaration, symbol (datatype name) expected",
|
|
2849
|
+
datatype,
|
|
2850
|
+
).text;
|
|
2851
|
+
datatypeIndexes.set(name, index);
|
|
2852
|
+
names.push(name);
|
|
2853
|
+
});
|
|
2854
|
+
const missing: string[] = [];
|
|
2855
|
+
const typeParameters = parameterNames.map((_name, index) => manager.mkTypeParameter(index));
|
|
2856
|
+
const bindings = bindTypeParameters(parameterNames);
|
|
2857
|
+
const defs = bodies.items.map((item, index) => {
|
|
2858
|
+
if (isSmt26) {
|
|
2859
|
+
if (index >= names.length)
|
|
2860
|
+
throw new SmtParseError(
|
|
2861
|
+
"invalid datatype declaration, too many data-type bodies defined",
|
|
2862
|
+
item,
|
|
2863
|
+
);
|
|
2864
|
+
return mkDatatypeDef(
|
|
2865
|
+
manager,
|
|
2866
|
+
names[index],
|
|
2867
|
+
Array.from({ length: arities[index] }, (_unused, parameter) =>
|
|
2868
|
+
manager.mkTypeParameter(parameter),
|
|
2869
|
+
),
|
|
2870
|
+
parseDatatypeBody(item, datatypeIndexes, missing, list).constructors,
|
|
2871
|
+
);
|
|
2872
|
+
}
|
|
2873
|
+
const datatype = expectList(item, "invalid datatype declaration, '(' or ')' expected", item);
|
|
2874
|
+
return mkDatatypeDef(
|
|
2875
|
+
manager,
|
|
2876
|
+
names[index],
|
|
2877
|
+
typeParameters,
|
|
2878
|
+
parseConstructors(datatype.items.slice(1), bindings, datatypeIndexes, missing, datatype),
|
|
2879
|
+
);
|
|
2880
|
+
});
|
|
2881
|
+
if (defs.length < arities.length)
|
|
2882
|
+
throw new SmtParseError(
|
|
2883
|
+
"invalid datatype declaration, too few datatype definitions compared to declared sorts",
|
|
2884
|
+
bodies.end,
|
|
2885
|
+
);
|
|
2886
|
+
if (list.items.length > 3)
|
|
2887
|
+
throw new SmtParseError("invalid datatype declaration", list.items[3]);
|
|
2888
|
+
if (defs.length === 0) return;
|
|
2889
|
+
commitDatatypes(defs, missing, declaration, list.end);
|
|
2890
|
+
if (isSmt26) return;
|
|
2891
|
+
for (const def of defs)
|
|
2892
|
+
declareSortDecl(
|
|
2893
|
+
def.name,
|
|
2894
|
+
{
|
|
2895
|
+
kind: "datatype",
|
|
2896
|
+
name: def.name,
|
|
2897
|
+
arity: def.parameters.length,
|
|
2898
|
+
siblings: defs.length > 1 ? names : [],
|
|
2899
|
+
instances: new Map(),
|
|
2900
|
+
},
|
|
2901
|
+
list.end,
|
|
2902
|
+
);
|
|
2903
|
+
};
|
|
2904
|
+
|
|
2905
|
+
const declareDatatype = (list: SmtList) => {
|
|
2906
|
+
const name = expectSymbol(list.items[1], "unexpected token used as datatype name", list);
|
|
2907
|
+
const missing: string[] = [];
|
|
2908
|
+
const bodyList = list.items[2];
|
|
2909
|
+
const arity =
|
|
2910
|
+
bodyList?.kind === "list" &&
|
|
2911
|
+
isSymbol(bodyList.items[0], "par") &&
|
|
2912
|
+
bodyList.items[1]?.kind === "list"
|
|
2913
|
+
? bodyList.items[1].items.length
|
|
2914
|
+
: 0;
|
|
2915
|
+
declareSortDecl(
|
|
2916
|
+
name.text,
|
|
2917
|
+
{ kind: "datatype", name: name.text, arity, siblings: [], instances: new Map() },
|
|
2918
|
+
name,
|
|
2919
|
+
);
|
|
2920
|
+
const body = parseDatatypeBody(bodyList, new Map([[name.text, 0]]), missing, list);
|
|
2921
|
+
if (list.items.length > 3)
|
|
2922
|
+
throw new SmtParseError("invalid end of datatype declaration, ')' expected", list.items[3]);
|
|
2923
|
+
commitDatatypes(
|
|
2924
|
+
[mkDatatypeDef(manager, name.text, body.parameters, body.constructors)],
|
|
2925
|
+
missing,
|
|
2926
|
+
name,
|
|
2927
|
+
list.end,
|
|
2928
|
+
);
|
|
2929
|
+
};
|
|
2930
|
+
|
|
2931
|
+
// parse_rec_fun_decl: the variables are bound only while the range is parsed, and Z3 holds them
|
|
2932
|
+
// until the command ends, as the stack does here.
|
|
2933
|
+
const declareRecFun = (items: SExpr[], at: SExpr) => {
|
|
2934
|
+
const symbol = expectSymbol(
|
|
2935
|
+
items[0],
|
|
2936
|
+
"invalid function/constant definition, symbol expected",
|
|
2937
|
+
at,
|
|
2938
|
+
);
|
|
2939
|
+
const bound = bindSortedVars(items[1], at, symbol.text);
|
|
2940
|
+
const range = parseSort(items[2] ?? at, "Invalid recursive function definition");
|
|
2941
|
+
unbindSortedVars(bound.varSorts.length);
|
|
2942
|
+
return manager.mkRecFuncDecl(symbol.text, bound.varSorts, range);
|
|
2943
|
+
};
|
|
2944
|
+
|
|
2945
|
+
// parse_rec_fun_body: Z3 reports a body of another sort at the token after it.
|
|
2946
|
+
const defineRecFun = (decl: number, variables: SExpr, body: SExpr, next: SExpr) => {
|
|
2947
|
+
const start = exprStack.length;
|
|
2948
|
+
const bound = bindSortedVars(variables, body, "");
|
|
2949
|
+
runTerm(body, false);
|
|
2950
|
+
const parsedBody = exprStack[exprStack.length - 1];
|
|
2951
|
+
unbindSortedVars(bound.varSorts.length);
|
|
2952
|
+
const range = manager.funcDecls[decl].range;
|
|
2953
|
+
if (manager.sorts[parsedBody] !== range)
|
|
2954
|
+
throw new SmtParseError(
|
|
2955
|
+
`invalid function definition, sort mismatch. Expcected ${manager.getSortName(range)} but function body has sort ${manager.getSortName(manager.sorts[parsedBody])}`,
|
|
2956
|
+
next,
|
|
2957
|
+
);
|
|
2958
|
+
setRecFunDefinition(manager, manager.recFunDefs.get(decl)!, false, parsedBody);
|
|
2959
|
+
shrinkRefs(exprStack, start);
|
|
2960
|
+
};
|
|
2961
|
+
|
|
2962
|
+
// parse_define and insert_macro: Z3 declares a macro as a recursive function with one case, which
|
|
2963
|
+
// a reference without arguments takes as an array. The reference that the body with its variables
|
|
2964
|
+
// reversed keeps stands for Z3's macro_decls, and those of the variables for the definition's.
|
|
2965
|
+
const defineMacroFunc = (name: string, domain: number[], body: number) => {
|
|
2966
|
+
const count = domain.length;
|
|
2967
|
+
if (count > 1) {
|
|
2968
|
+
const subst = new VarSubst(manager, true);
|
|
2969
|
+
subst.apply(
|
|
2970
|
+
body,
|
|
2971
|
+
domain.map((sort, index) => manager.mkVar(index, sort)),
|
|
2972
|
+
);
|
|
2973
|
+
subst.release();
|
|
2974
|
+
}
|
|
2975
|
+
for (const [index, sort] of domain.entries()) manager.incRef(manager.mkVar(index, sort));
|
|
2976
|
+
const reversedVars = domain.map((_, index) => manager.mkVar(index, domain[count - index - 1]));
|
|
2977
|
+
for (const reversedVar of reversedVars) manager.incRef(reversedVar);
|
|
2978
|
+
const decl = manager.mkRecFuncDecl(name, domain, manager.sorts[body]);
|
|
2979
|
+
for (const reversedVar of reversedVars) manager.decRef(reversedVar);
|
|
2980
|
+
setRecFunDefinition(manager, manager.recFunDefs.get(decl)!, true, body);
|
|
2981
|
+
declare(funcs, name, [...(funcs.get(name) ?? []), decl]);
|
|
2982
|
+
};
|
|
2983
|
+
|
|
2984
|
+
const declareCommand = (list: SmtList) => {
|
|
2985
|
+
releasePending();
|
|
2986
|
+
const name = list.items[0].kind === "list" ? "" : list.items[0].text;
|
|
2987
|
+
if (name === "define-fun-rec") {
|
|
2988
|
+
const decl = declareRecFun(list.items.slice(1), list.end);
|
|
2989
|
+
insertFunc(decl, list.items[4] ?? list.end);
|
|
2990
|
+
defineRecFun(decl, list.items[2], list.items[4] ?? list.end, list.items[5] ?? list.end);
|
|
2991
|
+
shrinkRefs(exprStack, 0);
|
|
2992
|
+
return;
|
|
2993
|
+
}
|
|
2994
|
+
if (name === "define-funs-rec") {
|
|
2995
|
+
const message = "invalid recursive function definition, '(' expected";
|
|
2996
|
+
const signatures = expectList(list.items[1], message, list.end).items.map((signature) => {
|
|
2997
|
+
const declaration = expectList(signature, message, signature);
|
|
2998
|
+
const items = declaration.items;
|
|
2999
|
+
if (items.length > 3)
|
|
3000
|
+
throw new SmtParseError("invalid recursive function definition, ')' expected", items[3]);
|
|
3001
|
+
return { items, decl: declareRecFun(items, declaration.end) };
|
|
3002
|
+
});
|
|
3003
|
+
for (const signature of signatures) insertFunc(signature.decl, list.items[2] ?? list.end);
|
|
3004
|
+
const bodies = expectList(list.items[2], message, list.end);
|
|
3005
|
+
signatures.forEach((signature, index) => {
|
|
3006
|
+
const body = bodies.items[index];
|
|
3007
|
+
if (body === undefined)
|
|
3008
|
+
throw new SmtParseError(
|
|
3009
|
+
"the number of declarations does not match number of supplied definitions",
|
|
3010
|
+
bodies.end,
|
|
3011
|
+
);
|
|
3012
|
+
defineRecFun(
|
|
3013
|
+
signature.decl,
|
|
3014
|
+
signature.items[1],
|
|
3015
|
+
body,
|
|
3016
|
+
bodies.items[index + 1] ?? bodies.end,
|
|
3017
|
+
);
|
|
3018
|
+
});
|
|
3019
|
+
const extra = bodies.items[signatures.length];
|
|
3020
|
+
if (extra !== undefined)
|
|
3021
|
+
throw new SmtParseError("invalid recursive function definition, ')' expected", extra);
|
|
3022
|
+
shrinkRefs(exprStack, 0);
|
|
3023
|
+
return;
|
|
3024
|
+
}
|
|
3025
|
+
if (name === "declare-datatypes") {
|
|
3026
|
+
declareDatatypes(list);
|
|
3027
|
+
return;
|
|
3028
|
+
}
|
|
3029
|
+
if (name === "declare-datatype") {
|
|
3030
|
+
declareDatatype(list);
|
|
3031
|
+
return;
|
|
3032
|
+
}
|
|
3033
|
+
const symbol = expectSymbol(list.items[1], "invalid declaration, symbol expected", list);
|
|
465
3034
|
if (name === "declare-sort") {
|
|
466
|
-
if (
|
|
467
|
-
throw new
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
3035
|
+
if (sortDecls.has(symbol.text))
|
|
3036
|
+
throw new SmtParseError("invalid sort declaration, sort already declared/defined", symbol);
|
|
3037
|
+
const arity = list.items[2];
|
|
3038
|
+
if (arity !== undefined && arity.kind !== "numeral")
|
|
3039
|
+
throw new SmtParseError(
|
|
3040
|
+
"invalid sort declaration, arity (<numeral>) or ')' expected",
|
|
3041
|
+
arity,
|
|
3042
|
+
);
|
|
3043
|
+
if (list.items.length > 3)
|
|
3044
|
+
throw new SmtParseError("invalid sort declaration, ')' expected", list.items[3]);
|
|
3045
|
+
declare(
|
|
3046
|
+
sortDecls,
|
|
3047
|
+
symbol.text,
|
|
3048
|
+
arity === undefined || arity.text === "0"
|
|
3049
|
+
? { kind: "sort", sort: manager.mkSort(symbol.text) }
|
|
3050
|
+
: { kind: "user", name: symbol.text, arity: Number(arity.text) },
|
|
3051
|
+
);
|
|
3052
|
+
return;
|
|
3053
|
+
}
|
|
3054
|
+
if (name === "define-sort") {
|
|
3055
|
+
if (sortDecls.has(symbol.text))
|
|
3056
|
+
throw new SmtParseError("invalid sort definition, sort already declared/defined", symbol);
|
|
3057
|
+
const parameters = parseSortParameters(
|
|
3058
|
+
list.items[2],
|
|
3059
|
+
"invalid sort parameter, symbol or ')' expected",
|
|
3060
|
+
list,
|
|
3061
|
+
);
|
|
3062
|
+
const body = list.items[3] ?? list.end;
|
|
3063
|
+
checkPsort(body, bindTypeParameters(parameters));
|
|
3064
|
+
declare(sortDecls, symbol.text, { kind: "definition", parameters, body });
|
|
3065
|
+
return;
|
|
3066
|
+
}
|
|
3067
|
+
if (name === "define-fun" || name === "define-const") {
|
|
3068
|
+
const isConst = name === "define-const";
|
|
3069
|
+
const bound = isConst
|
|
3070
|
+
? { names: [], varSorts: [] }
|
|
3071
|
+
: bindSortedVars(list.items[2], list.end, symbol.text);
|
|
3072
|
+
const range = parseSort(
|
|
3073
|
+
list.items[isConst ? 2 : 3] ?? list.end,
|
|
3074
|
+
isConst ? "Invalid constant definition" : "Invalid function definition",
|
|
3075
|
+
);
|
|
3076
|
+
runTerm(list.items[isConst ? 3 : 4] ?? list.end, false);
|
|
3077
|
+
const body = exprStack[exprStack.length - 1];
|
|
3078
|
+
if (!isConst) unbindSortedVars(bound.varSorts.length);
|
|
3079
|
+
if (manager.sorts[body] !== range)
|
|
3080
|
+
throw new SmtParseError(
|
|
3081
|
+
isConst
|
|
3082
|
+
? "invalid constant definition, sort mismatch"
|
|
3083
|
+
: "invalid function/constant definition, sort mismatch",
|
|
3084
|
+
list.end,
|
|
3085
|
+
);
|
|
3086
|
+
manager.incRef(body);
|
|
3087
|
+
declare(macros, symbol.text, { domain: bound.varSorts, range, body });
|
|
3088
|
+
defineMacroFunc(symbol.text, bound.varSorts, body);
|
|
3089
|
+
shrinkRefs(exprStack, 0);
|
|
3090
|
+
return;
|
|
3091
|
+
}
|
|
3092
|
+
const domain = name === "declare-fun" ? parseDomain(list.items[2], list) : [];
|
|
3093
|
+
const range = parseSort(
|
|
3094
|
+
list.items[name === "declare-fun" ? 3 : 2] ?? list.end,
|
|
3095
|
+
name === "declare-fun" ? "Invalid function declaration" : "Invalid constant declaration",
|
|
3096
|
+
);
|
|
3097
|
+
// Z3's builtin_signature_collides and is_transcendental_shadow_decl each make and release fresh variables of the domain.
|
|
3098
|
+
for (let round = 0; round < 2; round++) {
|
|
3099
|
+
const vars = domain.map((sort, index) => manager.mkVar(index, sort));
|
|
3100
|
+
for (const domainVar of vars) manager.incRef(domainVar);
|
|
3101
|
+
for (const domainVar of vars) manager.decRef(domainVar);
|
|
3102
|
+
}
|
|
3103
|
+
const decl = manager.mkFuncDecl(symbol.text, domain, range);
|
|
3104
|
+
checkDecl(decl, list.end);
|
|
3105
|
+
if (macros.has(symbol.text))
|
|
3106
|
+
throw new SmtUnsupportedError(`the overloaded symbol ${symbol.text}`);
|
|
3107
|
+
const existing = funcs.get(symbol.text) ?? [];
|
|
3108
|
+
if (existing.some((other) => hasSignature(other, domain, range)))
|
|
3109
|
+
throw new SmtParseError(
|
|
3110
|
+
`invalid declaration, ${domain.length === 0 ? "constant" : "function"} '${symbol.text}' (with the given signature) already declared`,
|
|
3111
|
+
list.end,
|
|
3112
|
+
);
|
|
3113
|
+
declare(funcs, symbol.text, [...existing, decl]);
|
|
3114
|
+
};
|
|
3115
|
+
|
|
3116
|
+
// The caller takes the reference of the stack.
|
|
3117
|
+
const parseFormula = (sexpr: SExpr) => {
|
|
3118
|
+
runTerm(sexpr, false);
|
|
3119
|
+
const assertion = exprStack[exprStack.length - 1];
|
|
3120
|
+
if (manager.sorts[assertion] !== SORT_BOOL)
|
|
3121
|
+
throw new SmtParseError("invalid assert command, term is not Boolean", sexpr);
|
|
3122
|
+
exprStack.pop();
|
|
3123
|
+
return assertion;
|
|
3124
|
+
};
|
|
3125
|
+
|
|
3126
|
+
// An assertion is named when the last name of its term is its own.
|
|
3127
|
+
const parseAssertion = (sexpr: SExpr) => {
|
|
3128
|
+
releasePending();
|
|
3129
|
+
lastNamed.expr = -1;
|
|
3130
|
+
const formula = parseFormula(sexpr);
|
|
3131
|
+
return { formula, name: lastNamed.expr === formula ? lastNamed.name : undefined };
|
|
3132
|
+
};
|
|
3133
|
+
|
|
3134
|
+
const push = () => {
|
|
3135
|
+
releasePending();
|
|
3136
|
+
scopeStarts.push(undo.length);
|
|
3137
|
+
};
|
|
3138
|
+
|
|
3139
|
+
const pop = () => {
|
|
3140
|
+
releasePending();
|
|
3141
|
+
const start = scopeStarts.pop() ?? 0;
|
|
3142
|
+
for (const restore of undo.splice(start).reverse()) restore();
|
|
3143
|
+
};
|
|
3144
|
+
|
|
3145
|
+
// parser::reset, after an error: Z3 frees its stacks, and with them the terms that only the
|
|
3146
|
+
// command that failed held.
|
|
3147
|
+
const reset = () => {
|
|
3148
|
+
protect(floating);
|
|
3149
|
+
protect(unrooted);
|
|
3150
|
+
releasePending();
|
|
3151
|
+
shrinkRefs(exprStack, 0);
|
|
3152
|
+
if (parsed.expr !== -1) {
|
|
3153
|
+
if (!parsed.isOwned) manager.incRef(parsed.expr);
|
|
3154
|
+
manager.decRef(parsed.expr);
|
|
3155
|
+
parsed.expr = -1;
|
|
3156
|
+
}
|
|
3157
|
+
shrinkRefs(patternStack, 0);
|
|
3158
|
+
shrinkRefs(noPatternStack, 0);
|
|
3159
|
+
locals.clear();
|
|
3160
|
+
shrink(shadowedNames, 0);
|
|
3161
|
+
shrink(shadowedLocals, 0);
|
|
3162
|
+
shrink(localScopes, 0);
|
|
3163
|
+
binders.depth = 0;
|
|
3164
|
+
};
|
|
3165
|
+
|
|
3166
|
+
// cmd_context::set_logic on a manager that is in use: restrict_builtin_sorts takes the sorts the
|
|
3167
|
+
// logic lacks, and the declarations and the operators stay.
|
|
3168
|
+
const restrictSorts = (name: string) => {
|
|
3169
|
+
releasePending();
|
|
3170
|
+
if (logicHasRealsOnly(name)) restricted.isNumeralAsReal = true;
|
|
3171
|
+
if (!ARRAY_EXTENSION_LOGICS.has(name)) restricted.hasSetSort = false;
|
|
3172
|
+
if (INT_FREE_LOGICS.has(name)) sortDecls.delete("Int");
|
|
3173
|
+
restricted.hasFf = FF_LOGICS.has(name);
|
|
3174
|
+
};
|
|
3175
|
+
|
|
3176
|
+
const isUsed = (name: string) => funcs.has(name) || BUILTIN_NAMES.has(name);
|
|
3177
|
+
|
|
3178
|
+
const isClashing = (decl: number) => {
|
|
3179
|
+
const domain = manager.funcDecls[decl].domain;
|
|
3180
|
+
const decls = funcs.get(String(manager.funcDecls[decl].name)) ?? [];
|
|
3181
|
+
return (
|
|
3182
|
+
decls.includes(decl) &&
|
|
3183
|
+
decls.some((other) => other !== decl && hasSignature(other, domain, -1))
|
|
3184
|
+
);
|
|
3185
|
+
};
|
|
3186
|
+
|
|
3187
|
+
// The first declaration of each name, in the slot order of cmd_context's dictionary.
|
|
3188
|
+
const getDeclaredFuncs = () => {
|
|
3189
|
+
const table = new ObjMap<true>((decl) => getSymbolHash(manager.funcDecls[decl].name));
|
|
3190
|
+
for (const decls of funcs.values()) table.set(decls[0], true);
|
|
3191
|
+
return [...table.keys()];
|
|
3192
|
+
};
|
|
3193
|
+
|
|
3194
|
+
// Z3 registers each named term and defined function as a recursive function definition.
|
|
3195
|
+
const hasDefinitions = () =>
|
|
3196
|
+
macros.size > 0 ||
|
|
3197
|
+
[...funcs.values()].some((decls) => decls.some((decl) => manager.recFunDefs.has(decl)));
|
|
3198
|
+
|
|
3199
|
+
return {
|
|
3200
|
+
declareCommand,
|
|
3201
|
+
insertFunc,
|
|
3202
|
+
getFuncs: (name: string) => funcs.get(name) ?? [],
|
|
3203
|
+
hasDefinitions,
|
|
3204
|
+
parseTerm,
|
|
3205
|
+
parseSort: (sexpr: SExpr) => parseSort(sexpr, ""),
|
|
3206
|
+
parseFormula,
|
|
3207
|
+
parseAssertion,
|
|
3208
|
+
getLogicError: logicChecker.checkExpr,
|
|
3209
|
+
push,
|
|
3210
|
+
pop,
|
|
3211
|
+
reset,
|
|
3212
|
+
restrictSorts,
|
|
3213
|
+
isUsed,
|
|
3214
|
+
isClashing,
|
|
3215
|
+
getDeclaredFuncs,
|
|
3216
|
+
};
|
|
3217
|
+
};
|
|
3218
|
+
|
|
3219
|
+
export type SmtlibReader = ReturnType<typeof createSmtlibReader>;
|
|
3220
|
+
|
|
3221
|
+
export const DECLARATIONS = new Set([
|
|
3222
|
+
"declare-sort",
|
|
3223
|
+
"declare-datatypes",
|
|
3224
|
+
"declare-datatype",
|
|
3225
|
+
"declare-fun",
|
|
3226
|
+
"declare-const",
|
|
3227
|
+
"define-fun",
|
|
3228
|
+
"define-const",
|
|
3229
|
+
"define-sort",
|
|
3230
|
+
"define-fun-rec",
|
|
3231
|
+
"define-funs-rec",
|
|
3232
|
+
]);
|
|
3233
|
+
|
|
3234
|
+
// Z3 reports an error in a command, skips the command and goes on.
|
|
3235
|
+
export const parseSmtlib = (
|
|
3236
|
+
text: string,
|
|
3237
|
+
manager: AstManager,
|
|
3238
|
+
assertExpr: (assertion: number) => void,
|
|
3239
|
+
report: (error: SmtParseError) => void,
|
|
3240
|
+
): SmtProblem => {
|
|
3241
|
+
const commands = createCommandReader(text);
|
|
3242
|
+
const state = { reader: createSmtlibReader(manager), logic: "", rlimit: 0, isInUse: false };
|
|
3243
|
+
const assertions: number[] = [];
|
|
3244
|
+
const runCommand = (list: SmtList) => {
|
|
3245
|
+
const name = expectSymbol(list.items[0], "invalid command, symbol expected", list).text;
|
|
3246
|
+
const option = list.items[1];
|
|
3247
|
+
const value = list.items[2];
|
|
3248
|
+
if (
|
|
3249
|
+
name === "set-option" &&
|
|
3250
|
+
option?.kind === "keyword" &&
|
|
3251
|
+
option.text === ":rlimit" &&
|
|
3252
|
+
value?.kind === "numeral"
|
|
3253
|
+
)
|
|
3254
|
+
state.rlimit = Number(value.text);
|
|
3255
|
+
if (name === "set-logic") {
|
|
3256
|
+
if (option === undefined)
|
|
3257
|
+
throw new SmtParseError("invalid command, argument(s) missing", list.end);
|
|
3258
|
+
if (!isSymbol(option))
|
|
3259
|
+
throw new SmtParseError("invalid command argument, symbol expected", option);
|
|
3260
|
+
if (value !== undefined)
|
|
3261
|
+
throw new SmtParseError("invalid command, too many arguments", value);
|
|
3262
|
+
if (state.logic !== "") throw new SmtParseError("the logic has already been set", option);
|
|
3263
|
+
if (assertions.length > 0)
|
|
3264
|
+
throw new SmtParseError("logic must be set before initialization", option);
|
|
3265
|
+
if (!isSupportedLogic(option.text)) return;
|
|
3266
|
+
state.logic = option.text;
|
|
3267
|
+
if (state.isInUse) state.reader.restrictSorts(option.text);
|
|
3268
|
+
else state.reader = createSmtlibReader(manager, option.text);
|
|
3269
|
+
} else if (DECLARATIONS.has(name)) {
|
|
3270
|
+
state.isInUse = true;
|
|
3271
|
+
if (name.startsWith("define-"))
|
|
3272
|
+
throw new SmtUnsupportedError(`the command ${name} is not supported`);
|
|
3273
|
+
state.reader.declareCommand(list);
|
|
479
3274
|
} else if (name === "assert") {
|
|
480
|
-
|
|
481
|
-
|
|
3275
|
+
state.isInUse = true;
|
|
3276
|
+
const assertion = state.reader.parseFormula(list.items[1] ?? list.end);
|
|
3277
|
+
const logicError = state.reader.getLogicError(assertion);
|
|
3278
|
+
if (logicError !== undefined) {
|
|
3279
|
+
manager.decRef(assertion);
|
|
3280
|
+
throw new SmtParseError(logicError, list.end);
|
|
3281
|
+
}
|
|
482
3282
|
assertions.push(assertion);
|
|
483
3283
|
assertExpr(assertion);
|
|
484
|
-
} else if (name === "
|
|
485
|
-
hasCheckSat = true;
|
|
486
|
-
break;
|
|
487
|
-
} else if (name === "push" || name === "pop" || name === "define-fun" || name === "define-sort")
|
|
3284
|
+
} else if (name === "push" || name === "pop")
|
|
488
3285
|
throw new SmtUnsupportedError(`the command ${name} is not supported`);
|
|
3286
|
+
};
|
|
3287
|
+
for (;;) {
|
|
3288
|
+
try {
|
|
3289
|
+
const list = commands.read();
|
|
3290
|
+
if (list === undefined) return { assertions, hasCheckSat: false, rlimit: state.rlimit };
|
|
3291
|
+
if (isSymbol(list.items[0], "check-sat"))
|
|
3292
|
+
return { assertions, hasCheckSat: true, rlimit: state.rlimit };
|
|
3293
|
+
runCommand(list);
|
|
3294
|
+
} catch (error) {
|
|
3295
|
+
if (!(error instanceof SmtParseError)) throw error;
|
|
3296
|
+
report(error);
|
|
3297
|
+
state.reader.reset();
|
|
3298
|
+
commands.skip(report);
|
|
3299
|
+
}
|
|
489
3300
|
}
|
|
490
|
-
return { assertions, hasCheckSat };
|
|
491
3301
|
};
|