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