tla-ts 0.0.3 → 0.0.4-dev.2a873ee
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -115
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +194 -8
- package/dist/index.d.ts +194 -8
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +996 -0
- package/dist/prove.js +996 -0
- package/dist/run-cli.cjs +37 -20
- package/dist/run-cli.js +37 -20
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +1387 -18
- package/dist/transfer.js +1387 -18
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +24 -11
- package/src/ast.ts +149 -8
- package/src/bags.ts +4 -7
- package/src/builtins.ts +29 -11
- package/src/bundled-modules.ts +3 -3
- package/src/canonical.ts +4 -3
- package/src/check.ts +1540 -235
- package/src/cli-options.ts +12 -6
- package/src/cli.ts +8 -1
- package/src/codegen.ts +98 -39
- package/src/config.ts +254 -181
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +540 -140
- package/src/errors.ts +284 -4
- package/src/evaluate.ts +1043 -293
- package/src/exit-codes.ts +22 -43
- package/src/fingerprints.ts +46 -11
- package/src/index.ts +11 -0
- package/src/instances.ts +19 -16
- package/src/java-overrides.ts +104 -25
- package/src/levels.ts +191 -45
- package/src/lexer.ts +127 -30
- package/src/live-check.ts +867 -0
- package/src/liveness.ts +218 -572
- package/src/{obligation-action.ts → obligations/action.ts} +247 -69
- package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
- package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
- package/src/{obligations.ts → obligations/index.ts} +32 -16
- package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
- package/src/{obligation-module.ts → obligations/module.ts} +113 -121
- package/src/obligations/pickle.ts +98 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/{obligation-source.ts → obligations/source.ts} +156 -99
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/ocaml-marshal.ts +166 -0
- package/src/parallel-worker.ts +169 -224
- package/src/parallel.ts +140 -81
- package/src/parser.ts +456 -147
- package/src/pcal-ast.ts +770 -0
- package/src/pcal-parse.ts +1523 -0
- package/src/pcal-tokenize.ts +516 -0
- package/src/pcal-translate.ts +1230 -0
- package/src/pcal-validate.ts +454 -0
- package/src/prove/decide-pool.ts +95 -0
- package/src/prove/deciders.ts +33 -0
- package/src/prove/first-order.ts +78 -0
- package/src/prove/isabelle/LICENSE +36 -0
- package/src/prove/isabelle/LICENSE-tlapm +26 -0
- package/src/prove/isabelle/decide.ts +47 -0
- package/src/prove/isabelle/encode.ts +446 -0
- package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prove/isabelle/isar/method.ts +367 -0
- package/src/prove/isabelle/isar/proof.ts +28 -0
- package/src/prove/isabelle/isar/statement.ts +57 -0
- package/src/prove/isabelle/isar/theory.ts +192 -0
- package/src/prove/isabelle/isar/type-check.ts +278 -0
- package/src/prove/isabelle/provers/blast/blast.ts +258 -0
- package/src/prove/isabelle/provers/blast/branch.ts +322 -0
- package/src/prove/isabelle/provers/blast/prove.ts +347 -0
- package/src/prove/isabelle/provers/blast/rules.ts +330 -0
- package/src/prove/isabelle/provers/blast/state.ts +180 -0
- package/src/prove/isabelle/provers/blast/term.ts +546 -0
- package/src/prove/isabelle/provers/clasimp.ts +150 -0
- package/src/prove/isabelle/provers/classical.ts +389 -0
- package/src/prove/isabelle/provers/hypsubst.ts +433 -0
- package/src/prove/isabelle/pure/bicompose.ts +174 -0
- package/src/prove/isabelle/pure/consts.ts +32 -0
- package/src/prove/isabelle/pure/conv.ts +79 -0
- package/src/prove/isabelle/pure/drule.ts +264 -0
- package/src/prove/isabelle/pure/envir.ts +331 -0
- package/src/prove/isabelle/pure/goal.ts +53 -0
- package/src/prove/isabelle/pure/interrupt.ts +16 -0
- package/src/prove/isabelle/pure/list.ts +12 -0
- package/src/prove/isabelle/pure/logic.ts +330 -0
- package/src/prove/isabelle/pure/name.ts +98 -0
- package/src/prove/isabelle/pure/net.ts +215 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +631 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +338 -0
- package/src/prove/isabelle/pure/rewriter.ts +602 -0
- package/src/prove/isabelle/pure/search.ts +68 -0
- package/src/prove/isabelle/pure/seq.ts +133 -0
- package/src/prove/isabelle/pure/simplifier.ts +106 -0
- package/src/prove/isabelle/pure/table.ts +201 -0
- package/src/prove/isabelle/pure/tactic.ts +168 -0
- package/src/prove/isabelle/pure/tactical.ts +264 -0
- package/src/prove/isabelle/pure/term-ord.ts +105 -0
- package/src/prove/isabelle/pure/term.ts +688 -0
- package/src/prove/isabelle/pure/thm.ts +783 -0
- package/src/prove/isabelle/pure/type.ts +121 -0
- package/src/prove/isabelle/pure/unify.ts +753 -0
- package/src/prove/isabelle/pure/variable.ts +25 -0
- package/src/prove/isabelle/tla/classical-setup.ts +101 -0
- package/src/prove/isabelle/tla/context.ts +74 -0
- package/src/prove/isabelle/tla/logic-data.ts +5 -0
- package/src/prove/isabelle/tla/logic.ts +215 -0
- package/src/prove/isabelle/tla/simpdata.ts +241 -0
- package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prove/ocaml.ts +22 -0
- package/src/prove/prep/action.ts +508 -0
- package/src/prove/prep/defs.ts +33 -0
- package/src/prove/prep/elab.ts +156 -0
- package/src/prove/prep/enabled-axioms.ts +189 -0
- package/src/prove/prep/enabled.ts +43 -0
- package/src/prove/prep/fingerprint.ts +388 -0
- package/src/prove/prep/flatten.ts +70 -0
- package/src/prove/prep/level-comparison.ts +529 -0
- package/src/prove/prep/md5.ts +67 -0
- package/src/prove/prep/pragma.ts +221 -0
- package/src/prove/prep/trivial.ts +22 -0
- package/src/prove/prove-worker.ts +22 -0
- package/src/prove/prove.ts +498 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +255 -0
- package/src/prove/ptl/sat.ts +151 -0
- package/src/prove/ptl/tableau.ts +276 -0
- package/src/prove/smt/axiomatize.ts +69 -0
- package/src/prove/smt/axioms.ts +209 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +636 -0
- package/src/prove/smt/encode.ts +164 -0
- package/src/prove/smt/extensionality.ts +65 -0
- package/src/prove/smt/flatten.ts +112 -0
- package/src/prove/smt/format.ts +154 -0
- package/src/prove/smt/instances.ts +181 -0
- package/src/prove/smt/rewrite.ts +772 -0
- package/src/prove/smt/simplify.ts +57 -0
- package/src/prove/smt/solver/act-cache.ts +265 -0
- package/src/prove/smt/solver/arith-rewriter.ts +962 -0
- package/src/prove/smt/solver/array.ts +22 -0
- package/src/prove/smt/solver/asserted-formulas.ts +469 -0
- package/src/prove/smt/solver/ast-lt.ts +157 -0
- package/src/prove/smt/solver/ast-util.ts +91 -0
- package/src/prove/smt/solver/ast.ts +1315 -0
- package/src/prove/smt/solver/auf-solver.ts +572 -0
- package/src/prove/smt/solver/case-split-queue.ts +178 -0
- package/src/prove/smt/solver/cg-table.ts +175 -0
- package/src/prove/smt/solver/clause.ts +276 -0
- package/src/prove/smt/solver/conflict-resolution.ts +343 -0
- package/src/prove/smt/solver/context.ts +1292 -0
- package/src/prove/smt/solver/ctx-simplify.ts +334 -0
- package/src/prove/smt/solver/der.ts +224 -0
- package/src/prove/smt/solver/dyn-ack.ts +141 -0
- package/src/prove/smt/solver/e-matching.ts +659 -0
- package/src/prove/smt/solver/enode.ts +259 -0
- package/src/prove/smt/solver/expr-ref.ts +21 -0
- package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prove/smt/solver/fingerprints.ts +92 -0
- package/src/prove/smt/solver/flat-literals.ts +51 -0
- package/src/prove/smt/solver/goal.ts +138 -0
- package/src/prove/smt/solver/hash.ts +173 -0
- package/src/prove/smt/solver/hashtable.ts +166 -0
- package/src/prove/smt/solver/internalizer.ts +1381 -0
- package/src/prove/smt/solver/lbool.ts +5 -0
- package/src/prove/smt/solver/literal.ts +24 -0
- package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prove/smt/solver/lp/constraints.ts +78 -0
- package/src/prove/smt/solver/lp/containers.ts +296 -0
- package/src/prove/smt/solver/lp/core-solver.ts +504 -0
- package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prove/smt/solver/lp/dep-intervals.ts +642 -0
- package/src/prove/smt/solver/lp/dependency.ts +51 -0
- package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prove/smt/solver/lp/explanation.ts +46 -0
- package/src/prove/smt/solver/lp/gomory.ts +255 -0
- package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prove/smt/solver/lp/hnf.ts +281 -0
- package/src/prove/smt/solver/lp/int-cube.ts +295 -0
- package/src/prove/smt/solver/lp/int-solver.ts +514 -0
- package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prove/smt/solver/lp/lar-term.ts +62 -0
- package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prove/smt/solver/lp/nex.ts +120 -0
- package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prove/smt/solver/lp/nla-core.ts +968 -0
- package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prove/smt/solver/lp/nla-order.ts +307 -0
- package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prove/smt/solver/lp/nla-types.ts +124 -0
- package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prove/smt/solver/lp/pdd.ts +971 -0
- package/src/prove/smt/solver/lp/random-updater.ts +138 -0
- package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prove/smt/solver/lp/union-find.ts +96 -0
- package/src/prove/smt/solver/mam-compiler.ts +847 -0
- package/src/prove/smt/solver/mam-interpreter.ts +400 -0
- package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prove/smt/solver/model-checker.ts +347 -0
- package/src/prove/smt/solver/model-evaluator.ts +262 -0
- package/src/prove/smt/solver/model-finder.ts +155 -0
- package/src/prove/smt/solver/model-generator.ts +97 -0
- package/src/prove/smt/solver/model-macro-solver.ts +728 -0
- package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prove/smt/solver/nlsat/explain.ts +607 -0
- package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prove/smt/solver/nlsat/levelwise.ts +737 -0
- package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prove/smt/solver/nlsat/solver.ts +911 -0
- package/src/prove/smt/solver/nlsat/types.ts +43 -0
- package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prove/smt/solver/nnf.ts +469 -0
- package/src/prove/smt/solver/pattern-inference.ts +351 -0
- package/src/prove/smt/solver/poly-rewriter.ts +477 -0
- package/src/prove/smt/solver/propagate-values.ts +83 -0
- package/src/prove/smt/solver/proto-model.ts +551 -0
- package/src/prove/smt/solver/pull-quant.ts +139 -0
- package/src/prove/smt/solver/qe-lite-der.ts +526 -0
- package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prove/smt/solver/qe-lite.ts +381 -0
- package/src/prove/smt/solver/qi-queue.ts +362 -0
- package/src/prove/smt/solver/quant-tactics.ts +117 -0
- package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prove/smt/solver/quantifier-manager.ts +186 -0
- package/src/prove/smt/solver/random-gen.ts +10 -0
- package/src/prove/smt/solver/rational.ts +165 -0
- package/src/prove/smt/solver/ref-vector.ts +62 -0
- package/src/prove/smt/solver/relevancy.ts +247 -0
- package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prove/smt/solver/rewriter.ts +843 -0
- package/src/prove/smt/solver/rlimit.ts +79 -0
- package/src/prove/smt/solver/smtlib-parser.ts +526 -0
- package/src/prove/smt/solver/solver.ts +71 -0
- package/src/prove/smt/solver/sort.ts +35 -0
- package/src/prove/smt/solver/sticky-generations.ts +69 -0
- package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prove/smt/solver/theory-lra-model.ts +243 -0
- package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prove/smt/solver/theory-lra-state.ts +43 -0
- package/src/prove/smt/solver/theory-lra.ts +56 -0
- package/src/prove/smt/solver/var-subst.ts +258 -0
- package/src/prove/smt/solver/z3-license.txt +10 -0
- package/src/prove/smt/table.ts +170 -0
- package/src/prove/tlapm/tlapm.ts +96 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +18 -0
- package/src/prove/zenon/LICENSE +32 -0
- package/src/prove/zenon/check/check.ts +105 -0
- package/src/prove/zenon/check/definitions.ts +144 -0
- package/src/prove/zenon/check/equality.ts +182 -0
- package/src/prove/zenon/check/functions.ts +62 -0
- package/src/prove/zenon/check/keys.ts +125 -0
- package/src/prove/zenon/check/propositional.ts +104 -0
- package/src/prove/zenon/check/records.ts +188 -0
- package/src/prove/zenon/check/relations.ts +121 -0
- package/src/prove/zenon/check/rewrites.ts +224 -0
- package/src/prove/zenon/check/sets.ts +302 -0
- package/src/prove/zenon/check/terms.ts +127 -0
- package/src/prove/zenon/check/tla.ts +126 -0
- package/src/prove/zenon/decide.ts +113 -0
- package/src/prove/zenon/encode.ts +379 -0
- package/src/prove/zenon/eqrel.ts +300 -0
- package/src/prove/zenon/equality-rules.ts +435 -0
- package/src/prove/zenon/expr.ts +551 -0
- package/src/prove/zenon/index-table.ts +350 -0
- package/src/prove/zenon/isar-case.ts +513 -0
- package/src/prove/zenon/isar.ts +1249 -0
- package/src/prove/zenon/limits.ts +15 -0
- package/src/prove/zenon/llproof.ts +173 -0
- package/src/prove/zenon/mltoll.ts +1566 -0
- package/src/prove/zenon/node.ts +297 -0
- package/src/prove/zenon/ocaml.ts +169 -0
- package/src/prove/zenon/parse.ts +247 -0
- package/src/prove/zenon/phrase.ts +191 -0
- package/src/prove/zenon/proof.ts +198 -0
- package/src/prove/zenon/propositional-rules.ts +387 -0
- package/src/prove/zenon/rules.ts +94 -0
- package/src/prove/zenon/search.ts +288 -0
- package/src/prove/zenon/tla/functions.ts +151 -0
- package/src/prove/zenon/tla/llproof.ts +480 -0
- package/src/prove/zenon/tla/records.ts +225 -0
- package/src/prove/zenon/tla/rewrites.ts +447 -0
- package/src/prove/zenon/tla/sets.ts +304 -0
- package/src/prove/zenon/tla/terms.ts +162 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/randomization.ts +24 -7
- package/src/resolve.ts +1481 -426
- package/src/run-cli.ts +1483 -212
- package/src/runtime.ts +19 -1
- package/src/sany-builtins.ts +147 -0
- package/src/sany-generator.ts +3128 -0
- package/src/sany-levels.ts +1208 -0
- package/src/sany-lexer.ts +545 -0
- package/src/sany-linter.ts +122 -0
- package/src/sany-modules.ts +1398 -0
- package/src/sany-nodes.ts +463 -0
- package/src/sany-parse-error.ts +2161 -0
- package/src/sany-report.ts +356 -0
- package/src/sany-selectors.ts +738 -0
- package/src/sany-source.ts +248 -0
- package/src/sany-xml.ts +1017 -0
- package/src/spec-description.ts +42 -11
- package/src/standard-modules.ts +203 -148
- package/src/standard-ranges.ts +116 -0
- package/src/state-graph.ts +180 -0
- package/src/subexpressions.ts +231 -171
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +241 -110
- package/src/symmetry.ts +194 -54
- package/src/tableau.ts +54 -25
- package/src/tlapm-cache.ts +236 -0
- package/src/tlc-ext.ts +42 -29
- package/src/tlc-messages.ts +329 -0
- package/src/tlc-module.ts +52 -43
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +78 -10
- package/src/tlc-random.ts +84 -33
- package/src/tlc-replay.ts +655 -0
- package/src/tlc-summary.ts +20 -5
- package/src/tlc-version.ts +19 -0
- package/src/transfer.ts +15 -3
- package/src/values.ts +787 -190
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// Ported from Z3 (src/smt/theory_lra.cpp), MIT license, see z3-license.txt. The model of
|
|
2
|
+
// theory_lra: the values of its variables, and the equalities between shared terms with equal
|
|
3
|
+
// values that it assumes.
|
|
4
|
+
import {
|
|
5
|
+
FAMILY_ARITH,
|
|
6
|
+
FAMILY_BASIC,
|
|
7
|
+
OP_DIV,
|
|
8
|
+
OP_DIV0,
|
|
9
|
+
OP_IDIV,
|
|
10
|
+
OP_IDIV0,
|
|
11
|
+
OP_ITE,
|
|
12
|
+
OP_MOD,
|
|
13
|
+
OP_MOD0,
|
|
14
|
+
} from "./ast";
|
|
15
|
+
import { SmtUnsupportedError } from "../../../errors";
|
|
16
|
+
import { Enode } from "./enode";
|
|
17
|
+
import type { ProtoModel } from "./proto-model";
|
|
18
|
+
import { Rational } from "./rational";
|
|
19
|
+
import { NULL_LPVAR } from "./lp/lar-solver";
|
|
20
|
+
import type { NlaCore } from "./lp/nla-core";
|
|
21
|
+
import { Anum } from "./nlsat/algebraic";
|
|
22
|
+
import { randomUpdate } from "./lp/random-updater";
|
|
23
|
+
import { shrink } from "./array";
|
|
24
|
+
import { TheoryLraPropagate } from "./theory-lra-propagate";
|
|
25
|
+
|
|
26
|
+
export abstract class TheoryLraModel extends TheoryLraPropagate {
|
|
27
|
+
private assumeEqCandidates: [number, number][] = [];
|
|
28
|
+
private assumeEqHead = 0;
|
|
29
|
+
|
|
30
|
+
protected initVariableValues() {
|
|
31
|
+
this.isModelInitialized = false;
|
|
32
|
+
if (this.context.checkCanceled() || this.getNumVars() === 0) return;
|
|
33
|
+
const saved = this.isModelInitialized;
|
|
34
|
+
this.context.pushTrail(() => {
|
|
35
|
+
this.isModelInitialized = saved;
|
|
36
|
+
});
|
|
37
|
+
this.isModelInitialized = this.solver.initModel();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private isRelevantAndShared(node: Enode) {
|
|
41
|
+
return this.context.isRelevant(node.owner) && this.isShared(node);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// random_update: moves the values of shared variables that collide with others.
|
|
45
|
+
private randomUpdate() {
|
|
46
|
+
if (this.nla?.hasRelevantMonomial()) return;
|
|
47
|
+
const solver = this.solver;
|
|
48
|
+
const byValue = new Map<string, number>();
|
|
49
|
+
const tmpVarSet = new Set<number>();
|
|
50
|
+
const columns: number[] = [];
|
|
51
|
+
for (let thVar = 0; thVar < this.getNumVars(); thVar++) {
|
|
52
|
+
if (!this.isRelevantAndShared(this.getEnode(thVar))) continue;
|
|
53
|
+
const column = this.registerVar(thVar);
|
|
54
|
+
const key = `${this.isIntVar(thVar)}:${solver.getValue(column)}`;
|
|
55
|
+
const other = byValue.get(key);
|
|
56
|
+
if (other === undefined) {
|
|
57
|
+
byValue.set(key, thVar);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (this.isEqual(thVar, other)) continue;
|
|
61
|
+
if (!solver.isFixed(column)) columns.push(column);
|
|
62
|
+
else if (!tmpVarSet.has(other)) {
|
|
63
|
+
const otherColumn = solver.externalToLocalColumn(other);
|
|
64
|
+
if (!solver.isFixed(otherColumn)) {
|
|
65
|
+
tmpVarSet.add(other);
|
|
66
|
+
columns.push(otherColumn);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (columns.length > 0) randomUpdate(solver, columns);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// assume_eqs: model-based theory combination.
|
|
74
|
+
protected assumeEqs() {
|
|
75
|
+
if (this.delayedAssumeEqs()) return true;
|
|
76
|
+
this.randomUpdate();
|
|
77
|
+
const context = this.context;
|
|
78
|
+
const solver = this.solver;
|
|
79
|
+
const numVars = this.getNumVars();
|
|
80
|
+
const oldSize = this.assumeEqCandidates.length;
|
|
81
|
+
let numCandidates = 0;
|
|
82
|
+
const start = context.getRandomValue();
|
|
83
|
+
const byValue = new Map<string, number>();
|
|
84
|
+
const representatives: number[] = [];
|
|
85
|
+
for (let index = 0; index < numVars; index++) {
|
|
86
|
+
const thVar = (index + start) % numVars;
|
|
87
|
+
if (!this.isRelevantAndShared(this.getEnode(thVar))) continue;
|
|
88
|
+
const column = this.registerVar(thVar);
|
|
89
|
+
// m_model_eqs: with nlsat's model, the values are compared one by one.
|
|
90
|
+
const key = `${this.isIntVar(thVar)}:${solver.getValue(column)}`;
|
|
91
|
+
const other = this.nla?.isUsingNraModel
|
|
92
|
+
? representatives.find(
|
|
93
|
+
(representative) =>
|
|
94
|
+
this.isIntVar(representative) === this.isIntVar(thVar) &&
|
|
95
|
+
this.isEq(representative, thVar),
|
|
96
|
+
)
|
|
97
|
+
: byValue.get(key);
|
|
98
|
+
if (other === undefined) {
|
|
99
|
+
byValue.set(key, thVar);
|
|
100
|
+
representatives.push(thVar);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (this.isEqual(thVar, other)) continue;
|
|
104
|
+
this.assumeEqCandidates.push([thVar, other]);
|
|
105
|
+
numCandidates++;
|
|
106
|
+
}
|
|
107
|
+
if (numCandidates > 0)
|
|
108
|
+
context.pushTrail(() => {
|
|
109
|
+
shrink(this.assumeEqCandidates, oldSize);
|
|
110
|
+
});
|
|
111
|
+
return this.delayedAssumeEqs();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private delayedAssumeEqs() {
|
|
115
|
+
if (this.assumeEqHead === this.assumeEqCandidates.length) return false;
|
|
116
|
+
const saved = this.assumeEqHead;
|
|
117
|
+
this.context.pushTrail(() => {
|
|
118
|
+
this.assumeEqHead = saved;
|
|
119
|
+
});
|
|
120
|
+
while (this.assumeEqHead < this.assumeEqCandidates.length) {
|
|
121
|
+
const candidate = this.assumeEqCandidates[this.assumeEqHead];
|
|
122
|
+
this.assumeEqHead++;
|
|
123
|
+
if (
|
|
124
|
+
this.isEq(candidate[0], candidate[1]) &&
|
|
125
|
+
!this.isEqual(candidate[0], candidate[1]) &&
|
|
126
|
+
this.context.assumeEq(this.getEnode(candidate[0]), this.getEnode(candidate[1]))
|
|
127
|
+
)
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// init_model: the model knows the value of every theory variable without δ, so fresh numbers
|
|
134
|
+
// avoid them. Like get_value(enode*, expr_ref&), it skips an integer with a fractional value.
|
|
135
|
+
initModel(model: ProtoModel) {
|
|
136
|
+
this.initVariableValues();
|
|
137
|
+
if (!this.isModelInitialized) return;
|
|
138
|
+
for (let thVar = 0; thVar < this.getNumVars(); thVar++) {
|
|
139
|
+
const column = this.solver.externalToLocalColumn(thVar);
|
|
140
|
+
const value = column === NULL_LPVAR ? undefined : this.solver.getExactValue(column);
|
|
141
|
+
if (value === undefined) continue;
|
|
142
|
+
if (!this.isIntVar(thVar)) model.registerValue(this.manager.mkRealNumeral(value));
|
|
143
|
+
else if (value.isInt()) model.registerValue(this.manager.mkNumeral(value.num));
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// mk_value: the value of the class of a root, rounded down for an integer.
|
|
148
|
+
mkValue(node: Enode) {
|
|
149
|
+
if (node.thVar < 0) return undefined;
|
|
150
|
+
const nla = this.nla;
|
|
151
|
+
if (
|
|
152
|
+
nla === undefined ||
|
|
153
|
+
!nla.isUsingNraModel ||
|
|
154
|
+
this.solver.externalToLocalColumn(node.thVar) === NULL_LPVAR
|
|
155
|
+
)
|
|
156
|
+
return this.getValueExpr(node.thVar);
|
|
157
|
+
// nlsat's value, zero for an integer whose value is not one.
|
|
158
|
+
const value = this.getNlValue(nla, node.thVar).value;
|
|
159
|
+
if (this.isIntVar(node.thVar))
|
|
160
|
+
return this.manager.mkNumeral(value instanceof Rational && value.isInt() ? value.num : 0n);
|
|
161
|
+
if (!(value instanceof Rational)) throw new SmtUnsupportedError("an irrational model value");
|
|
162
|
+
return this.manager.mkRealNumeral(value);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// is_eq: whether two variables have the same value, in nlsat's model when nla uses it.
|
|
166
|
+
private isEq(first: number, second: number) {
|
|
167
|
+
const nla = this.nla;
|
|
168
|
+
if (nla !== undefined && nla.isUsingNraModel)
|
|
169
|
+
return nla.nra.am.compare(this.getNlValue(nla, first), this.getNlValue(nla, second)) === 0;
|
|
170
|
+
const solver = this.solver;
|
|
171
|
+
return solver
|
|
172
|
+
.getValue(this.registerVar(first))
|
|
173
|
+
.equals(solver.getValue(this.registerVar(second)));
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// nl_value: the value of a variable in nlsat's model, a term's the sum over its columns.
|
|
177
|
+
private getNlValue(nla: NlaCore, thVar: number) {
|
|
178
|
+
const solver = this.solver;
|
|
179
|
+
const am = nla.nra.am;
|
|
180
|
+
const column = solver.externalToLocalColumn(thVar);
|
|
181
|
+
if (!solver.hasTerm(column)) return nla.nra.value(column);
|
|
182
|
+
let result = new Anum(Rational.zero);
|
|
183
|
+
const todo: [number, Rational][] = [[column, Rational.one]];
|
|
184
|
+
while (todo.length > 0) {
|
|
185
|
+
const pending = todo.pop()!;
|
|
186
|
+
for (const entry of solver.getTerm(pending[0]).coeffs) {
|
|
187
|
+
const coeff = entry[1].mul(pending[1]);
|
|
188
|
+
if (solver.hasTerm(entry[0])) todo.push([entry[0], coeff]);
|
|
189
|
+
else result = am.add(am.mul(nla.nra.value(entry[0]), new Anum(coeff)), result);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private getValue(thVar: number) {
|
|
196
|
+
const column = this.solver.externalToLocalColumn(thVar);
|
|
197
|
+
return column === NULL_LPVAR ? Rational.zero : this.solver.getModelValue(column);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private getValueExpr(thVar: number) {
|
|
201
|
+
const value = this.getValue(thVar);
|
|
202
|
+
return this.isIntVar(thVar)
|
|
203
|
+
? this.manager.mkNumeral(value.floor().num)
|
|
204
|
+
: this.manager.mkRealNumeral(value);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private isShared(node: Enode) {
|
|
208
|
+
const root = node.root;
|
|
209
|
+
if (this.manager.getOp(root.owner) === OP_ITE) return true;
|
|
210
|
+
if (root.thVar < 0) return false;
|
|
211
|
+
if (this.context.qmanager.isShared(root)) return true;
|
|
212
|
+
for (const parent of root.parents) {
|
|
213
|
+
const family = this.manager.getFamily(parent.owner);
|
|
214
|
+
if (family !== FAMILY_ARITH && family !== FAMILY_BASIC) return true;
|
|
215
|
+
}
|
|
216
|
+
return this.isSharedWithUnderspecified(root);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// theory_lra::is_shared: whether the class is an argument of a division by a term or by zero.
|
|
220
|
+
private isSharedWithUnderspecified(root: Enode) {
|
|
221
|
+
const manager = this.manager;
|
|
222
|
+
const count = this.underspecifiedTerms.length;
|
|
223
|
+
if (count === 0) return false;
|
|
224
|
+
if (root.parents.length > 2 * count)
|
|
225
|
+
return this.underspecifiedTerms.some((term) =>
|
|
226
|
+
manager.someArg(term, (arg) => this.context.getEnode(arg)?.root === root),
|
|
227
|
+
);
|
|
228
|
+
return root.parents.some((parent) => this.isUnderspecified(parent.owner));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// arith_util::is_underspecified.
|
|
232
|
+
private isUnderspecified(expr: number) {
|
|
233
|
+
const op = this.manager.getOp(expr);
|
|
234
|
+
return (
|
|
235
|
+
op === OP_DIV ||
|
|
236
|
+
op === OP_IDIV ||
|
|
237
|
+
op === OP_MOD ||
|
|
238
|
+
op === OP_DIV0 ||
|
|
239
|
+
op === OP_IDIV0 ||
|
|
240
|
+
op === OP_MOD0
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
// Ported from Z3 (src/smt/theory_lra.cpp), MIT license, see z3-license.txt. How theory_lra
|
|
2
|
+
// propagates: asserted bounds go to the lp solver, the tableau is made feasible, and implied bounds
|
|
3
|
+
// and equalities of fixed columns come back as literals and equalities.
|
|
4
|
+
import { OP_ITE, OP_SUB, SORT_BOOL } from "./ast";
|
|
5
|
+
import { TheoryJustification } from "./enode";
|
|
6
|
+
import { getIdentityHash, ObjMap } from "./hashtable";
|
|
7
|
+
import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
|
|
8
|
+
import { Literal, negate } from "./literal";
|
|
9
|
+
import { Rational } from "./rational";
|
|
10
|
+
import { BoundPropagator, getImpliedKind, ImpliedBound } from "./lp/bound-propagator";
|
|
11
|
+
import { type ConstraintKind, KIND_GE, KIND_GT, KIND_LE, KIND_LT } from "./lp/constraints";
|
|
12
|
+
import { LP_INFEASIBLE } from "./lp/core-solver";
|
|
13
|
+
import { Dependency, linearize, mkJoin, mkLeaf } from "./lp/dependency";
|
|
14
|
+
import { Explanation } from "./lp/explanation";
|
|
15
|
+
import type { LarTerm } from "./lp/lar-term";
|
|
16
|
+
import { getAtomLiteral, TheoryLraBounds } from "./theory-lra-bounds";
|
|
17
|
+
import type { ApiBound } from "./theory-lra-internalize";
|
|
18
|
+
|
|
19
|
+
const NULL_THEORY_VAR = -1;
|
|
20
|
+
|
|
21
|
+
interface TermBound {
|
|
22
|
+
constraint: number;
|
|
23
|
+
value: Rational;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export abstract class TheoryLraPropagate extends TheoryLraBounds {
|
|
27
|
+
protected abstract propagateNla(isIncremental: boolean): void;
|
|
28
|
+
|
|
29
|
+
private bp = new BoundPropagator(this.solver, {
|
|
30
|
+
columnIsInteresting: (column) => this.columnIsInteresting(column),
|
|
31
|
+
boundIsInteresting: (column, kind, value) => this.boundIsInteresting(column, kind, value),
|
|
32
|
+
isEqual: (first, second) => this.isEqual(first, second),
|
|
33
|
+
addEq: (first, second, explanation, isFixed) => this.addEq(first, second, explanation, isFixed),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
protected assertedQhead = 0;
|
|
37
|
+
private delayIneqsQhead = 0;
|
|
38
|
+
protected explanation = new Explanation();
|
|
39
|
+
protected coreLiterals: Literal[] = [];
|
|
40
|
+
protected numConflicts = 0;
|
|
41
|
+
private lowerTerms: (TermBound | undefined)[] = [];
|
|
42
|
+
private upperTerms: (TermBound | undefined)[] = [];
|
|
43
|
+
protected isModelInitialized = false;
|
|
44
|
+
|
|
45
|
+
canPropagate() {
|
|
46
|
+
return (
|
|
47
|
+
this.assertedAtoms.length > this.assertedQhead ||
|
|
48
|
+
this.isNewDef ||
|
|
49
|
+
this.solver.columnsWithChangedBounds.size > 0 ||
|
|
50
|
+
this.delayIneqsQhead < this.delayIneqs.length
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
propagate() {
|
|
55
|
+
this.propagateCore();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// propagate_core: whether there was anything to propagate.
|
|
59
|
+
protected propagateCore() {
|
|
60
|
+
const context = this.context;
|
|
61
|
+
this.isModelInitialized = false;
|
|
62
|
+
this.flushBoundAxioms();
|
|
63
|
+
if (context.isInconsistent()) return true;
|
|
64
|
+
if (!this.canPropagate()) return false;
|
|
65
|
+
const manager = this.manager;
|
|
66
|
+
for (
|
|
67
|
+
;
|
|
68
|
+
this.delayIneqsQhead < this.delayIneqs.length &&
|
|
69
|
+
!context.isInconsistent() &&
|
|
70
|
+
!context.checkCanceled();
|
|
71
|
+
this.delayIneqsQhead++
|
|
72
|
+
) {
|
|
73
|
+
const saved = this.delayIneqsQhead;
|
|
74
|
+
context.pushTrail(() => {
|
|
75
|
+
this.delayIneqsQhead = saved;
|
|
76
|
+
});
|
|
77
|
+
const atom = this.delayIneqs[this.delayIneqsQhead];
|
|
78
|
+
const sort = manager.sorts[manager.getArg(atom, 0)];
|
|
79
|
+
const zero = manager.mkNumeralOfSort(Rational.zero, sort);
|
|
80
|
+
const difference = manager.mkApp(
|
|
81
|
+
OP_SUB,
|
|
82
|
+
[manager.getArg(atom, 0), manager.getArg(atom, 1)],
|
|
83
|
+
sort,
|
|
84
|
+
);
|
|
85
|
+
const normalized = manager.mkApp(manager.getOp(atom), [difference, zero], SORT_BOOL);
|
|
86
|
+
const first = this.mkLiteral(atom);
|
|
87
|
+
const second = this.mkLiteral(normalized);
|
|
88
|
+
this.mkAxiom([negate(first), second]);
|
|
89
|
+
this.mkAxiom([first, negate(second)]);
|
|
90
|
+
}
|
|
91
|
+
this.isNewDef = false;
|
|
92
|
+
while (
|
|
93
|
+
this.assertedQhead < this.assertedAtoms.length &&
|
|
94
|
+
!context.isInconsistent() &&
|
|
95
|
+
!context.checkCanceled()
|
|
96
|
+
) {
|
|
97
|
+
const atom = this.assertedAtoms[this.assertedQhead];
|
|
98
|
+
const bound = this.boolVarToBound[atom.boolVar];
|
|
99
|
+
if (bound !== undefined && !this.assertBound(atom.isTrue, bound)) {
|
|
100
|
+
this.setInfeasibilityConflict();
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
this.assertedQhead++;
|
|
104
|
+
}
|
|
105
|
+
if (context.isInconsistent()) return true;
|
|
106
|
+
const status = this.makeFeasible();
|
|
107
|
+
if (context.checkCanceled()) return true;
|
|
108
|
+
if (status === L_FALSE) this.setInfeasibilityConflict();
|
|
109
|
+
else if (status === L_TRUE) {
|
|
110
|
+
this.propagateNla(true);
|
|
111
|
+
this.propagateBoundsWithLpSolver();
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
protected makeFeasible() {
|
|
117
|
+
const status = this.solver.findFeasibleSolution();
|
|
118
|
+
if (this.solver.isFeasible()) return L_TRUE;
|
|
119
|
+
return status === LP_INFEASIBLE ? L_FALSE : L_UNDEF;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private assertBound(isTrue: boolean, bound: ApiBound) {
|
|
123
|
+
const solver = this.solver;
|
|
124
|
+
const constraint = isTrue ? bound.constraintTrue : bound.constraintFalse;
|
|
125
|
+
if (solver.constraintIsRedundant(constraint)) return solver.status !== LP_INFEASIBLE;
|
|
126
|
+
solver.activate(constraint);
|
|
127
|
+
if (solver.status === LP_INFEASIBLE) return false;
|
|
128
|
+
const kind = this.getBoundKind(bound, isTrue);
|
|
129
|
+
const value = isTrue
|
|
130
|
+
? bound.value
|
|
131
|
+
: bound.isLower
|
|
132
|
+
? bound.value.sub(Rational.one)
|
|
133
|
+
: bound.value.add(Rational.one);
|
|
134
|
+
if (bound.isInt || isTrue) this.propagateEqs(bound.column, constraint, kind, bound, value);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// propagate_eqs: a bound that fixes a column, with the opposite bound of the same value.
|
|
139
|
+
private propagateEqs(
|
|
140
|
+
column: number,
|
|
141
|
+
constraint: number,
|
|
142
|
+
kind: ConstraintKind,
|
|
143
|
+
bound: ApiBound,
|
|
144
|
+
value: Rational,
|
|
145
|
+
) {
|
|
146
|
+
const isLower = kind === KIND_GE;
|
|
147
|
+
if (kind !== KIND_GE && kind !== KIND_LE) return;
|
|
148
|
+
if (!this.setTermBound(column, constraint, value, isLower)) return;
|
|
149
|
+
const opposite = this.getFixingBound(column, value, !isLower);
|
|
150
|
+
if (opposite === undefined) return;
|
|
151
|
+
this.fixedVarEh(bound.thVar, column, mkJoin(mkLeaf(constraint), opposite), value);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// set_bound: records the bound of a term column, or checks that a column has the bound.
|
|
155
|
+
private setTermBound(column: number, constraint: number, value: Rational, isLower: boolean) {
|
|
156
|
+
const solver = this.solver;
|
|
157
|
+
if (!solver.hasTerm(column)) {
|
|
158
|
+
const bound = solver.getBound(column, !isLower);
|
|
159
|
+
return bound !== undefined && !bound.isStrict && bound.value.equals(value);
|
|
160
|
+
}
|
|
161
|
+
const terms = isLower ? this.lowerTerms : this.upperTerms;
|
|
162
|
+
const existing = terms[column];
|
|
163
|
+
if (
|
|
164
|
+
existing === undefined ||
|
|
165
|
+
(isLower ? existing.value.compare(value) < 0 : existing.value.compare(value) > 0)
|
|
166
|
+
) {
|
|
167
|
+
this.context.pushTrail(() => {
|
|
168
|
+
terms[column] = existing;
|
|
169
|
+
});
|
|
170
|
+
terms[column] = { constraint, value };
|
|
171
|
+
}
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// has_bound: the dependency of a bound of the column with the value, if it has one.
|
|
176
|
+
private getFixingBound(
|
|
177
|
+
column: number,
|
|
178
|
+
value: Rational,
|
|
179
|
+
isLower: boolean,
|
|
180
|
+
): Dependency | undefined {
|
|
181
|
+
const solver = this.solver;
|
|
182
|
+
if (solver.hasTerm(column)) {
|
|
183
|
+
const thVar = solver.getExternal(column);
|
|
184
|
+
if (thVar !== NULL_THEORY_VAR) {
|
|
185
|
+
const expr = this.getExpr(thVar);
|
|
186
|
+
if (this.manager.isNumeral(expr) && this.manager.getRationalValue(expr).equals(value))
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const termBound = (isLower ? this.lowerTerms : this.upperTerms)[column];
|
|
190
|
+
if (termBound === undefined || !termBound.value.equals(value)) return undefined;
|
|
191
|
+
return mkLeaf(termBound.constraint);
|
|
192
|
+
}
|
|
193
|
+
const bound = solver.getBound(column, !isLower);
|
|
194
|
+
if (bound === undefined || !bound.value.equals(value) || bound.isStrict) return undefined;
|
|
195
|
+
return bound.dep;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
protected isEqual(first: number, second: number) {
|
|
199
|
+
return this.getEnode(first).root === this.getEnode(second).root;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// report_equality_of_fixed_vars: two columns fixed to the same value are equal.
|
|
203
|
+
protected reportEqualityOfFixedVars(first: number, second: number) {
|
|
204
|
+
const solver = this.solver;
|
|
205
|
+
const v1 = solver.getExternal(first);
|
|
206
|
+
const v2 = solver.getExternal(second);
|
|
207
|
+
// Z3 assumes both columns have theory variables, but a cut column has none.
|
|
208
|
+
if (v1 === NULL_THEORY_VAR || v2 === NULL_THEORY_VAR) return;
|
|
209
|
+
if (this.isEqual(v1, v2) || this.isIntVar(v1) !== this.isIntVar(v2)) return;
|
|
210
|
+
const value = Rational.zero;
|
|
211
|
+
const deps = [
|
|
212
|
+
this.getFixingBound(first, value, true),
|
|
213
|
+
this.getFixingBound(first, value, false),
|
|
214
|
+
this.getFixingBound(second, value, true),
|
|
215
|
+
this.getFixingBound(second, value, false),
|
|
216
|
+
];
|
|
217
|
+
if (deps.some((dep) => dep === undefined)) return;
|
|
218
|
+
this.resetEvidence();
|
|
219
|
+
for (const dep of deps) if (dep !== undefined) this.setEvidenceOfDep(dep);
|
|
220
|
+
this.assignEq(v1, v2);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// fixed_var_eh: a column fixed to a value is equal to an offset term, or to the variable of the
|
|
224
|
+
// numeral with that value.
|
|
225
|
+
private fixedVarEh(thVar: number, column: number, dep: Dependency, value: Rational) {
|
|
226
|
+
if (this.propagateOffsetEq(column, dep, value)) return;
|
|
227
|
+
const isInt = this.isIntVar(thVar);
|
|
228
|
+
let other = this.value2var.get(`${isInt}:${value}`);
|
|
229
|
+
if (other === undefined) {
|
|
230
|
+
if (value.isZero()) other = this.solver.getExternal(isInt ? this.zeroVar : this.realZeroVar);
|
|
231
|
+
else if (value.equals(Rational.one))
|
|
232
|
+
other = this.solver.getExternal(isInt ? this.oneVar : this.realOneVar);
|
|
233
|
+
else return;
|
|
234
|
+
}
|
|
235
|
+
if (this.isIntVar(other) !== isInt || this.isEqual(thVar, other)) return;
|
|
236
|
+
this.resetEvidence();
|
|
237
|
+
this.setEvidenceOfDep(dep);
|
|
238
|
+
this.assignEq(thVar, other);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// propagate_offset_eq: x - y fixed to 0 makes x and y equal.
|
|
242
|
+
private propagateOffsetEq(column: number, dep: Dependency, value: Rational) {
|
|
243
|
+
if (!value.isZero() || !this.solver.hasTerm(column)) return false;
|
|
244
|
+
const coeffs = this.getTermCoeffs(this.solver.getTerm(column));
|
|
245
|
+
if (coeffs.length !== 2) return false;
|
|
246
|
+
const first = coeffs[0];
|
|
247
|
+
const second = coeffs[1];
|
|
248
|
+
if (!first[1].add(second[1]).isZero() || first[0] === second[0]) return false;
|
|
249
|
+
if (this.isEqual(first[0], second[0])) return false;
|
|
250
|
+
if (this.isIntVar(first[0]) !== this.isIntVar(second[0])) return false;
|
|
251
|
+
this.resetEvidence();
|
|
252
|
+
this.setEvidenceOfDep(dep);
|
|
253
|
+
this.assignEq(first[0], second[0]);
|
|
254
|
+
return true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// term2coeffs: the coefficients of the theory variables of a term, with term columns expanded,
|
|
258
|
+
// in the slot order of Z3's u_map.
|
|
259
|
+
protected getTermCoeffs(term: LarTerm) {
|
|
260
|
+
const coeffs = new ObjMap<Rational>(getIdentityHash);
|
|
261
|
+
const visit = (current: LarTerm, coeff: Rational) => {
|
|
262
|
+
for (const entry of current.coeffs) {
|
|
263
|
+
if (this.solver.hasTerm(entry[0])) {
|
|
264
|
+
visit(this.solver.getTerm(entry[0]), coeff.mul(entry[1]));
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const thVar = this.solver.getExternal(entry[0]);
|
|
268
|
+
const sum = (coeffs.get(thVar) ?? Rational.zero).add(entry[1].mul(coeff));
|
|
269
|
+
if (coeffs.has(thVar)) coeffs.replace(thVar, sum);
|
|
270
|
+
else coeffs.set(thVar, sum);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
visit(term, Rational.one);
|
|
274
|
+
return [...coeffs];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private assignEq(first: number, second: number) {
|
|
278
|
+
this.context.pushEq(this.getEnode(first), this.getEnode(second), {
|
|
279
|
+
kind: "justification",
|
|
280
|
+
justification: new TheoryJustification(this.coreLiterals, []),
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// add_eq: an equality that the lp solver implies, with its explanation.
|
|
285
|
+
protected addEq(first: number, second: number, explanation: Explanation, isFixed: boolean) {
|
|
286
|
+
if (this.context.isInconsistent()) return false;
|
|
287
|
+
const v1 = this.solver.getExternal(first);
|
|
288
|
+
const v2 = this.solver.getExternal(second);
|
|
289
|
+
if (v1 === NULL_THEORY_VAR || v2 === NULL_THEORY_VAR) return false;
|
|
290
|
+
if (this.isEqual(v1, v2) || this.isIntVar(v1) !== this.isIntVar(v2)) return false;
|
|
291
|
+
const manager = this.manager;
|
|
292
|
+
const e1 = this.getExpr(v1);
|
|
293
|
+
const e2 = this.getExpr(v2);
|
|
294
|
+
if (
|
|
295
|
+
!isFixed &&
|
|
296
|
+
!manager.isNumeral(e1) &&
|
|
297
|
+
!manager.isNumeral(e2) &&
|
|
298
|
+
(manager.getOp(e1) === OP_ITE || manager.getOp(e2) === OP_ITE)
|
|
299
|
+
)
|
|
300
|
+
return false;
|
|
301
|
+
this.resetEvidence();
|
|
302
|
+
for (const constraint of explanation) this.setEvidence(constraint);
|
|
303
|
+
this.assignEq(v1, v2);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private columnIsInteresting(column: number) {
|
|
308
|
+
const thVar = this.solver.getExternal(column);
|
|
309
|
+
return (
|
|
310
|
+
thVar !== NULL_THEORY_VAR &&
|
|
311
|
+
thVar < this.unassignedBounds.length &&
|
|
312
|
+
this.unassignedBounds[thVar] > 0
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private boundIsInteresting(column: number, kind: ConstraintKind, value: Rational) {
|
|
317
|
+
const thVar = this.solver.getExternal(column);
|
|
318
|
+
if (thVar === NULL_THEORY_VAR || thVar >= this.bounds.length) return false;
|
|
319
|
+
if (this.unassignedBounds[thVar] === 0) return false;
|
|
320
|
+
const context = this.context;
|
|
321
|
+
return this.bounds[thVar].some(
|
|
322
|
+
(bound) =>
|
|
323
|
+
context.getAssignment(getAtomLiteral(bound)) === L_UNDEF &&
|
|
324
|
+
this.isBoundImplied(kind, value, bound) !== undefined,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private propagateBoundsWithLpSolver() {
|
|
329
|
+
this.bp.init();
|
|
330
|
+
this.bp.propagateBoundsForTouchedRows();
|
|
331
|
+
if (this.context.checkCanceled()) return;
|
|
332
|
+
if (this.solver.status === LP_INFEASIBLE) {
|
|
333
|
+
this.setInfeasibilityConflict();
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
for (const implied of this.bp.ibounds) {
|
|
337
|
+
if (this.context.isInconsistent()) break;
|
|
338
|
+
this.propagateLpSolverBound(implied);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private propagateLpSolverBound(implied: ImpliedBound) {
|
|
343
|
+
const thVar = this.solver.getExternal(implied.column);
|
|
344
|
+
if (thVar === NULL_THEORY_VAR || this.unassignedBounds[thVar] === 0) return;
|
|
345
|
+
const context = this.context;
|
|
346
|
+
const kind = getImpliedKind(implied);
|
|
347
|
+
let isFirst = true;
|
|
348
|
+
for (const bound of this.bounds[thVar]) {
|
|
349
|
+
if (context.getAssignment(getAtomLiteral(bound)) !== L_UNDEF) continue;
|
|
350
|
+
const literal = this.isBoundImplied(kind, implied.bound, bound);
|
|
351
|
+
if (literal === undefined) continue;
|
|
352
|
+
if (isFirst) {
|
|
353
|
+
isFirst = false;
|
|
354
|
+
this.resetEvidence();
|
|
355
|
+
this.explanation.clear();
|
|
356
|
+
for (const entry of implied.explain())
|
|
357
|
+
for (const constraint of linearize([entry[1]])) {
|
|
358
|
+
this.setEvidence(constraint);
|
|
359
|
+
this.explanation.addPair(constraint, entry[0]);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
context.assign(literal, new TheoryJustification(this.coreLiterals, []));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// is_bound_implied: the literal of the bound that the implied bound makes true or false.
|
|
367
|
+
private isBoundImplied(kind: ConstraintKind, value: Rational, bound: ApiBound) {
|
|
368
|
+
const literal = getAtomLiteral(bound);
|
|
369
|
+
const comparison = value.compare(bound.value);
|
|
370
|
+
const isUpperKind = kind === KIND_LE || kind === KIND_LT;
|
|
371
|
+
const isLowerKind = kind === KIND_GE || kind === KIND_GT;
|
|
372
|
+
if (isUpperKind && !bound.isLower && comparison <= 0) return literal;
|
|
373
|
+
if (isLowerKind && bound.isLower && comparison >= 0) return literal;
|
|
374
|
+
if (kind === KIND_LE && bound.isLower && comparison < 0) return negate(literal);
|
|
375
|
+
if (kind === KIND_LT && bound.isLower && comparison <= 0) return negate(literal);
|
|
376
|
+
if (kind === KIND_GE && !bound.isLower && comparison > 0) return negate(literal);
|
|
377
|
+
if (kind === KIND_GT && !bound.isLower && comparison >= 0) return negate(literal);
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
protected resetEvidence() {
|
|
382
|
+
this.coreLiterals = [];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private setEvidenceOfDep(dep: Dependency) {
|
|
386
|
+
for (const constraint of linearize([dep])) this.setEvidence(constraint);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
protected setEvidence(constraint: number) {
|
|
390
|
+
const literal = this.constraintSources[constraint];
|
|
391
|
+
if (literal !== undefined && literal !== null) this.coreLiterals.push(literal);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
protected setInfeasibilityConflict() {
|
|
395
|
+
this.solver.getInfeasibilityExplanation(this.explanation);
|
|
396
|
+
this.setConflict();
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
protected setConflict() {
|
|
400
|
+
this.resetEvidence();
|
|
401
|
+
this.numConflicts++;
|
|
402
|
+
for (const constraint of this.explanation) this.setEvidence(constraint);
|
|
403
|
+
this.context.setConflict(new TheoryJustification(this.coreLiterals, []), null);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// Ported from Z3 (src/smt/theory_lra.cpp), MIT license, see z3-license.txt. The state of theory_lra
|
|
2
|
+
// that every part of it shares: the lp solver, the theory variables and their enodes, the bounds of
|
|
3
|
+
// atoms, and what the scopes undo. The theory is a chain of classes, one file each, from this state
|
|
4
|
+
// to TheoryLra, so each part reaches the ones before it.
|
|
5
|
+
import { AstManager } from "./ast";
|
|
6
|
+
import type { Context } from "./context";
|
|
7
|
+
import { Enode } from "./enode";
|
|
8
|
+
import { LarSolver } from "./lp/lar-solver";
|
|
9
|
+
|
|
10
|
+
export abstract class TheoryLraState {
|
|
11
|
+
protected varEnodes: Enode[] = [];
|
|
12
|
+
protected solver: LarSolver;
|
|
13
|
+
|
|
14
|
+
constructor(protected context: Context) {
|
|
15
|
+
this.solver = new LarSolver(() => context.checkCanceled());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected get manager(): AstManager {
|
|
19
|
+
return this.context.manager;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// get_enode, get_expr and get_num_vars: the theory reaches enodes only through these and
|
|
23
|
+
// isEqual.
|
|
24
|
+
getEnode(thVar: number) {
|
|
25
|
+
return this.varEnodes[thVar];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected getExpr(thVar: number) {
|
|
29
|
+
return this.varEnodes[thVar].owner;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected getNumVars() {
|
|
33
|
+
return this.varEnodes.length;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected isIntExpr(expr: number) {
|
|
37
|
+
return this.manager.isInt(expr);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
protected isIntVar(thVar: number) {
|
|
41
|
+
return this.isIntExpr(this.getExpr(thVar));
|
|
42
|
+
}
|
|
43
|
+
}
|