tla-ts 0.0.4-dev.4819fb7 → 0.0.4-dev.55f2117
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 +37 -126
- 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 +33 -1
- package/dist/index.d.ts +33 -1
- 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.cjs +45 -0
- package/dist/run.js +45 -0
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +38 -44
- package/dist/transfer.js +38 -44
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +21 -12
- package/src/ast.ts +2 -2
- package/src/check.ts +8 -7
- package/src/{cli-options.ts → cli/options.ts} +1 -1
- package/src/{run-cli.ts → cli/run.ts} +302 -100
- package/src/cli.ts +1 -1
- package/src/codegen.ts +26 -7
- package/src/config.ts +2 -2
- package/src/environment.ts +9 -9
- package/src/errors.ts +89 -30
- package/src/evaluate.ts +58 -23
- package/src/index.ts +10 -0
- package/src/levels.ts +1 -1
- package/src/live-check.ts +1 -1
- package/src/liveness.ts +1 -1
- 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} +154 -99
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/parallel-worker.ts +4 -20
- package/src/parallel.ts +13 -46
- package/src/parser.ts +1 -1
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -2
- package/src/{pcal-parse.ts → pcal/parse.ts} +3 -3
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +2 -2
- package/src/{pcal-translate.ts → pcal/translate.ts} +2 -2
- package/src/{pcal-validate.ts → pcal/validate.ts} +6 -6
- 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/cache.ts +236 -0
- package/src/prove/tlapm/ocaml-marshal.ts +166 -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 +1 -1
- package/src/resolve.ts +1 -1
- package/src/runtime.ts +2 -2
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +14 -14
- package/src/{sany-levels.ts → sany/levels.ts} +2 -2
- package/src/{sany-lexer.ts → sany/lexer.ts} +1 -1
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +15 -15
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -3
- package/src/{sany-source.ts → sany/source.ts} +6 -6
- package/src/{sany-xml.ts → sany/xml.ts} +5 -5
- package/src/spec-description.ts +1 -1
- package/src/{standard-levels.ts → standard/levels.ts} +1 -1
- package/src/{standard-modules.ts → standard/modules.ts} +17 -14
- package/src/{standard-ranges.ts → standard/ranges.ts} +1 -1
- package/src/state-graph.ts +1 -1
- package/src/successors.ts +25 -6
- package/src/symmetry.ts +1 -1
- package/src/{tlc-messages.ts → tlc/messages.ts} +2 -2
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +58 -35
- package/src/{tlc-replay.ts → tlc/replay.ts} +5 -5
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +14 -8
- package/src/{tlc-version.ts → tlc/version.ts} +8 -7
- package/src/values.ts +81 -36
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- package/dist/run-cli.cjs +0 -55
- package/dist/run-cli.js +0 -55
- package/src/value-sources.ts +0 -29
- /package/src/{sany-nodes.ts → sany/nodes.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
|
@@ -0,0 +1,904 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/lp/nla_grobner.h, src/math/lp/nla_grobner.cpp), MIT license, see
|
|
2
|
+
// z3-license.txt. nla's Grobner pass: the rows around the monomials to refine become polynomial
|
|
3
|
+
// equations, which dd::solver saturates, and the equations then yield lemmas: interval conflicts,
|
|
4
|
+
// quotient and residue bounds, failed gcd tests, fixed variables, factorizations, and new linear
|
|
5
|
+
// rows. Z3's nlsat conflict check (is_nla_conflict) is not ported, nor are the statistics.
|
|
6
|
+
import { PddMemoryOutError } from "../../../../errors";
|
|
7
|
+
import { getEuclideanMod, getGcd, getLcm, Rational } from "../rational";
|
|
8
|
+
import { KIND_EQ, KIND_GE, KIND_LE, KIND_LT, KIND_NE } from "./constraints";
|
|
9
|
+
import { COLUMN_BOXED, COLUMN_FIXED, COLUMN_LOWER, COLUMN_UPPER } from "./core-solver";
|
|
10
|
+
import {
|
|
11
|
+
add,
|
|
12
|
+
checkIntervalForConflictOnZero,
|
|
13
|
+
type DepsMode,
|
|
14
|
+
mkDepInterval,
|
|
15
|
+
mul,
|
|
16
|
+
power,
|
|
17
|
+
separatedFromZero,
|
|
18
|
+
set,
|
|
19
|
+
setValue,
|
|
20
|
+
} from "./dep-intervals";
|
|
21
|
+
import { type Dependency, mkJoin } from "./dependency";
|
|
22
|
+
import { Explanation } from "./explanation";
|
|
23
|
+
import { IndexedUintSet } from "./containers";
|
|
24
|
+
import { NULL_LPVAR } from "./lar-solver";
|
|
25
|
+
import { LarTerm } from "./lar-term";
|
|
26
|
+
import { CrossNested } from "./cross-nested";
|
|
27
|
+
import { type Nex, getDegree } from "./nex";
|
|
28
|
+
import { NexCreator } from "./nex-creator";
|
|
29
|
+
import { Impq } from "./numeric-pair";
|
|
30
|
+
import type { RowCell } from "./static-matrix";
|
|
31
|
+
import { NLA_PARAMS, type NlaCore } from "./nla-core";
|
|
32
|
+
import { type Ineq, LemmaBuilder, mkIneq } from "./nla-types";
|
|
33
|
+
import { evaluatePdd, type Pdd, PddManager } from "./pdd";
|
|
34
|
+
import { getPddInterval } from "./pdd-interval";
|
|
35
|
+
import { type Equation, getDefaultPddSolverConfig, PddSolver } from "./pdd-solver";
|
|
36
|
+
|
|
37
|
+
const UINT_MAX = 0xffffffff;
|
|
38
|
+
const ADAPTIVE_UNIT = 16;
|
|
39
|
+
const ADAPTIVE_MAX = 4 * 16;
|
|
40
|
+
const ADAPTIVE_BUMP_AFTER = 2;
|
|
41
|
+
const DELAY_CAP = 1000000;
|
|
42
|
+
|
|
43
|
+
// A dependency that pdd_expr and val_of_fixed_var_with_deps extend, where Z3 passes a reference.
|
|
44
|
+
interface DepRef {
|
|
45
|
+
dep: Dependency;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface LinearTerm {
|
|
49
|
+
term: LarTerm;
|
|
50
|
+
offset: Rational;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface SortedMonomial {
|
|
54
|
+
coeff: Rational;
|
|
55
|
+
vars: number[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const getRationalGcd = (first: Rational, second: Rational) =>
|
|
59
|
+
Rational.of(getGcd(first.num, second.num));
|
|
60
|
+
|
|
61
|
+
const getRationalLcm = (first: Rational, second: Rational) =>
|
|
62
|
+
Rational.of(getLcm(first.num, second.num));
|
|
63
|
+
|
|
64
|
+
const getDenominator = (value: Rational) => Rational.of(value.den);
|
|
65
|
+
|
|
66
|
+
// divides: whether the integer a divides the integer b, where 0 divides only 0.
|
|
67
|
+
const divides = (divisor: Rational, value: Rational) =>
|
|
68
|
+
divisor.isZero() ? value.isZero() : value.num % divisor.num === 0n;
|
|
69
|
+
|
|
70
|
+
// ps_root: the root a of a^2 * M^2, a perfect square with every variable of even multiplicity.
|
|
71
|
+
const getPerfectSquareRoot = (coeff: Rational, vars: number[]) => {
|
|
72
|
+
if (vars.length % 2 === 1) return undefined;
|
|
73
|
+
const root = coeff.root(2);
|
|
74
|
+
if (root === undefined) return undefined;
|
|
75
|
+
for (let index = 0; index < vars.length;) {
|
|
76
|
+
const start = index;
|
|
77
|
+
while (index < vars.length && vars[index] === vars[start]) index++;
|
|
78
|
+
if ((index - start) % 2 === 1) return undefined;
|
|
79
|
+
}
|
|
80
|
+
return root;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// ps_cross: whether (c12, v12) is the cross term -2ab*M1*M2 of the perfect squares a^2*M1^2 and
|
|
84
|
+
// b^2*M2^2.
|
|
85
|
+
const isPerfectSquareCross = (
|
|
86
|
+
first: Rational,
|
|
87
|
+
firstVars: number[],
|
|
88
|
+
second: Rational,
|
|
89
|
+
secondVars: number[],
|
|
90
|
+
crossCoeff: Rational,
|
|
91
|
+
crossVars: number[],
|
|
92
|
+
) => {
|
|
93
|
+
if (!crossCoeff.isNegative()) return false;
|
|
94
|
+
if (!crossCoeff.equals(Rational.fromNumber(-2).mul(first).mul(second))) return false;
|
|
95
|
+
if (firstVars.length + secondVars.length !== crossVars.length * 2) return false;
|
|
96
|
+
let firstIndex = 0;
|
|
97
|
+
let secondIndex = 0;
|
|
98
|
+
for (const crossVar of crossVars) {
|
|
99
|
+
if (firstIndex < firstVars.length && firstVars[firstIndex] === crossVar) firstIndex += 2;
|
|
100
|
+
else if (secondIndex < secondVars.length && secondVars[secondIndex] === crossVar)
|
|
101
|
+
secondIndex += 2;
|
|
102
|
+
else return false;
|
|
103
|
+
}
|
|
104
|
+
return firstIndex >= firstVars.length && secondIndex >= secondVars.length;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export class Grobner {
|
|
108
|
+
private readonly propagatesQuotients = NLA_PARAMS.grobnerPropagateQuotients;
|
|
109
|
+
private readonly hasGcdTest = NLA_PARAMS.grobnerGcdTest;
|
|
110
|
+
private readonly expandsTerms = NLA_PARAMS.grobnerExpandTerms;
|
|
111
|
+
private readonly manager: PddManager;
|
|
112
|
+
private readonly solver: PddSolver;
|
|
113
|
+
private readonly nexCreator = new NexCreator();
|
|
114
|
+
private readonly rows = new IndexedUintSet();
|
|
115
|
+
private quota: number = NLA_PARAMS.grQ;
|
|
116
|
+
private delayBase = 0;
|
|
117
|
+
private delay = 0;
|
|
118
|
+
private growthBoost = ADAPTIVE_UNIT;
|
|
119
|
+
private hitStreak = 0;
|
|
120
|
+
private readonly isAddingAllEqs = false;
|
|
121
|
+
private mon2var = new Map<string, number>();
|
|
122
|
+
|
|
123
|
+
constructor(private core: NlaCore) {
|
|
124
|
+
this.manager = new PddManager(core.lra.getColumnCount());
|
|
125
|
+
this.solver = new PddSolver(this.manager, () => core.isCanceled());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// operator(): runs the pass, unless the quota of misses delays it.
|
|
129
|
+
run() {
|
|
130
|
+
if (this.core.lra.getColumnCount() > 5000) return;
|
|
131
|
+
if (this.quota === 0) this.quota = NLA_PARAMS.grQ;
|
|
132
|
+
if (this.quota === 1) {
|
|
133
|
+
if (NLA_PARAMS.grobnerExpDelay) {
|
|
134
|
+
if (this.delayBase === 0) this.delayBase = 1;
|
|
135
|
+
else if (this.delayBase < DELAY_CAP)
|
|
136
|
+
this.delayBase = Math.min(this.delayBase * 2, DELAY_CAP);
|
|
137
|
+
this.delay = this.delayBase;
|
|
138
|
+
} else {
|
|
139
|
+
this.delay = ++this.delayBase;
|
|
140
|
+
}
|
|
141
|
+
this.quota = NLA_PARAMS.grQ;
|
|
142
|
+
}
|
|
143
|
+
if (this.delay > 0) {
|
|
144
|
+
this.delay--;
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this.findNlCluster();
|
|
148
|
+
if (!this.configure()) return;
|
|
149
|
+
let isProductive = this.catchMemoryOut(() => this.propagateGcdTest());
|
|
150
|
+
if (!isProductive) {
|
|
151
|
+
this.solver.saturate();
|
|
152
|
+
if (this.delayBase > 0) this.delayBase--;
|
|
153
|
+
isProductive = this.catchMemoryOut(
|
|
154
|
+
() =>
|
|
155
|
+
this.isConflicting() ||
|
|
156
|
+
this.propagateQuotients() ||
|
|
157
|
+
this.propagateGcdTest() ||
|
|
158
|
+
this.propagateEqs() ||
|
|
159
|
+
this.propagateFactorization() ||
|
|
160
|
+
this.propagateLinearEquations(),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (NLA_PARAMS.grobnerAdaptive) this.updateGrowthBoost(isProductive);
|
|
164
|
+
if (isProductive) return;
|
|
165
|
+
this.delayBase++;
|
|
166
|
+
if (this.quota > 0) this.quota--;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Z3 catches every exception here, of which a failed pdd allocation is the one that can occur.
|
|
170
|
+
private catchMemoryOut(propagate: () => boolean) {
|
|
171
|
+
try {
|
|
172
|
+
return propagate();
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (!(error instanceof PddMemoryOutError)) throw error;
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// update_growth_boost: two productive runs in a row raise the boost by half, up to the cap, and a
|
|
180
|
+
// miss takes back a quarter of the excess over the unit.
|
|
181
|
+
private updateGrowthBoost(isProductive: boolean) {
|
|
182
|
+
if (isProductive) {
|
|
183
|
+
if (++this.hitStreak < ADAPTIVE_BUMP_AFTER) return;
|
|
184
|
+
this.growthBoost = Math.min(this.growthBoost + (this.growthBoost >> 1), ADAPTIVE_MAX);
|
|
185
|
+
this.hitStreak = 0;
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this.hitStreak = 0;
|
|
189
|
+
if (this.growthBoost <= ADAPTIVE_UNIT) return;
|
|
190
|
+
const excess = this.growthBoost - ADAPTIVE_UNIT;
|
|
191
|
+
this.growthBoost = Math.max(this.growthBoost - Math.floor((excess + 3) / 4), ADAPTIVE_UNIT);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private countPropagations(propagate: (equation: Equation) => boolean) {
|
|
195
|
+
let changed = 0;
|
|
196
|
+
for (const equation of this.solver.equations()) {
|
|
197
|
+
if (propagate(equation) && ++changed >= this.solver.config.numberOfConflictsToReport)
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
return changed > 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private isConflicting() {
|
|
204
|
+
return this.solver.equations().some((equation) => this.isEquationConflicting(equation));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private propagateEqs() {
|
|
208
|
+
return this.countPropagations((equation) => this.propagateFixed(equation));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private propagateFactorization() {
|
|
212
|
+
return this.countPropagations((equation) => this.propagateFactorizationOf(equation));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private propagateGcdTest() {
|
|
216
|
+
if (!this.hasGcdTest) return false;
|
|
217
|
+
return this.countPropagations((equation) => this.propagateGcdTestOf(equation));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private propagateQuotients() {
|
|
221
|
+
if (!this.propagatesQuotients) return false;
|
|
222
|
+
return this.countPropagations((equation) => this.propagateQuotientsOf(equation));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// propagate_fixed: k*x = 0 gives x = 0, and x + b = 0 gives x = -b.
|
|
226
|
+
private propagateFixed(equation: Equation) {
|
|
227
|
+
const poly = equation.poly;
|
|
228
|
+
if (poly.isUnary()) {
|
|
229
|
+
const variable = poly.var();
|
|
230
|
+
if (this.core.varIsFixed(variable)) return false;
|
|
231
|
+
return this.addLemmaUnlessHolds(equation, mkIneq(variable, KIND_EQ, 0));
|
|
232
|
+
}
|
|
233
|
+
if (!poly.isOffset()) return false;
|
|
234
|
+
const variable = poly.var();
|
|
235
|
+
if (this.core.varIsFixed(variable)) return false;
|
|
236
|
+
const coeff = poly.hi().val();
|
|
237
|
+
const offset = poly.lo().val().neg();
|
|
238
|
+
const lcm = getRationalLcm(getDenominator(coeff), getDenominator(offset));
|
|
239
|
+
const ineq = mkIneq(new LarTerm([[variable, coeff.mul(lcm)]]), KIND_EQ, offset.mul(lcm));
|
|
240
|
+
return this.addLemmaUnlessHolds(equation, ineq);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private addLemmaUnlessHolds(equation: Equation, ineq: Ineq) {
|
|
244
|
+
if (this.core.ineqHolds(ineq)) return false;
|
|
245
|
+
this.addDependencies(new LemmaBuilder(this.core), equation).addIneq(ineq);
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// propagate_factorization: x*q = 0 gives x = 0 or q = 0, for a linear q.
|
|
250
|
+
private propagateFactorizationOf(equation: Equation) {
|
|
251
|
+
const factors = equation.poly.varFactors();
|
|
252
|
+
if (factors.vars.length === 0 || !factors.poly.isLinear()) return false;
|
|
253
|
+
const linear = this.linearToTerm(factors.poly);
|
|
254
|
+
const ineqs = factors.vars.map((variable) => mkIneq(variable, KIND_EQ, 0));
|
|
255
|
+
ineqs.push(mkIneq(linear.term, KIND_EQ, linear.offset.neg()));
|
|
256
|
+
if (ineqs.some((ineq) => this.core.ineqHolds(ineq))) return false;
|
|
257
|
+
const lemma = this.addDependencies(new LemmaBuilder(this.core), equation);
|
|
258
|
+
for (const ineq of ineqs) lemma.addIneq(ineq);
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// linear_to_term: the linear polynomial times the lcm of its denominators, as a term and an
|
|
263
|
+
// offset.
|
|
264
|
+
private linearToTerm(poly: Pdd): LinearTerm {
|
|
265
|
+
let lcm = Rational.one;
|
|
266
|
+
let current = poly;
|
|
267
|
+
while (!current.isVal()) {
|
|
268
|
+
lcm = getRationalLcm(lcm, getDenominator(current.hi().val()));
|
|
269
|
+
current = current.lo();
|
|
270
|
+
}
|
|
271
|
+
lcm = getRationalLcm(getDenominator(current.val()), lcm);
|
|
272
|
+
const term = new LarTerm();
|
|
273
|
+
current = poly;
|
|
274
|
+
while (!current.isVal()) {
|
|
275
|
+
term.addMonomial(lcm.mul(current.hi().val()), current.var());
|
|
276
|
+
current = current.lo();
|
|
277
|
+
}
|
|
278
|
+
return { term, offset: lcm.mul(current.val()) };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private getValue(poly: Pdd) {
|
|
282
|
+
return evaluatePdd(poly, (variable) => this.core.val(variable));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private isIntPoly(poly: Pdd) {
|
|
286
|
+
return (
|
|
287
|
+
this.core.varIsInt(poly.var()) &&
|
|
288
|
+
poly.freeVars().every((variable) => this.core.varIsInt(variable))
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// propagate_gcd_test: x^k - y = 0 limits y modulo k^2. For each residue r of x modulo k, the
|
|
293
|
+
// gcd of the coefficients of p with k*x + r for x must divide its constant term.
|
|
294
|
+
private propagateGcdTestOf(equation: Equation) {
|
|
295
|
+
let poly = equation.poly;
|
|
296
|
+
if (poly.isLinear() || poly.isVal() || !this.isIntPoly(poly)) return false;
|
|
297
|
+
if (this.getValue(poly).isZero()) return false;
|
|
298
|
+
const powers = poly.getPowers();
|
|
299
|
+
if (powers.length === 0) return false;
|
|
300
|
+
let lcm = Rational.one;
|
|
301
|
+
for (const value of poly.coefficients()) lcm = getRationalLcm(lcm, getDenominator(value.coeff));
|
|
302
|
+
if (!lcm.equals(Rational.one)) poly = poly.mul(lcm);
|
|
303
|
+
const manager = this.manager;
|
|
304
|
+
const failsGcdTest = (substituted: Pdd) => {
|
|
305
|
+
let gcd = Rational.zero;
|
|
306
|
+
let constant = Rational.zero;
|
|
307
|
+
for (const value of substituted.coefficients()) {
|
|
308
|
+
if (value.isConstant) {
|
|
309
|
+
constant = constant.add(value.coeff);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
gcd = getRationalGcd(gcd, value.coeff);
|
|
313
|
+
if (gcd.equals(Rational.one)) return false;
|
|
314
|
+
}
|
|
315
|
+
return !constant.isZero() && !divides(gcd, constant);
|
|
316
|
+
};
|
|
317
|
+
for (const entry of powers) {
|
|
318
|
+
if (entry.degree > 4) continue;
|
|
319
|
+
const scaled = manager.mkVar(entry.var).mul(manager.mkVal(Rational.fromNumber(entry.degree)));
|
|
320
|
+
let isGcdFailing = true;
|
|
321
|
+
for (let residue = 0; isGcdFailing && residue < entry.degree; residue++) {
|
|
322
|
+
const replacement = scaled.add(manager.mkVal(Rational.fromNumber(residue)));
|
|
323
|
+
if (!failsGcdTest(poly.substPdd(entry.var, replacement))) isGcdFailing = false;
|
|
324
|
+
}
|
|
325
|
+
if (isGcdFailing) {
|
|
326
|
+
this.addDependencies(new LemmaBuilder(this.core), equation);
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// propagate_quotients: for x*y + z = 0 over the integers, the residue of a linear variable and
|
|
334
|
+
// bounds that relate a factor to the rest.
|
|
335
|
+
private propagateQuotientsOf(equation: Equation) {
|
|
336
|
+
const poly = equation.poly;
|
|
337
|
+
if (poly.isLinear() || poly.isVal() || !this.isIntPoly(poly)) return false;
|
|
338
|
+
if (this.getValue(poly).isZero()) return false;
|
|
339
|
+
const nlVars = new Set<number>();
|
|
340
|
+
let lcm = Rational.one;
|
|
341
|
+
for (const monomial of poly) {
|
|
342
|
+
lcm = getRationalLcm(lcm, getDenominator(monomial.coeff));
|
|
343
|
+
if (monomial.vars.length === 1) continue;
|
|
344
|
+
for (const variable of monomial.vars) nlVars.add(variable);
|
|
345
|
+
}
|
|
346
|
+
if (this.propagateModResidue(equation)) return true;
|
|
347
|
+
let isLemmaFound = false;
|
|
348
|
+
for (const variable of nlVars) {
|
|
349
|
+
const factor = poly.factor(variable, 1);
|
|
350
|
+
if (!factor.rest.isLinear()) continue;
|
|
351
|
+
const lc = lcm.equals(Rational.one) ? factor.lc : factor.lc.mul(lcm);
|
|
352
|
+
const rest = lcm.equals(Rational.one) ? factor.rest : factor.rest.mul(lcm);
|
|
353
|
+
if (this.addQuotientLemma(equation, variable, lc, rest)) isLemmaFound = true;
|
|
354
|
+
}
|
|
355
|
+
return isLemmaFound;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// mod_residue: when every monomial but c_v*v is a multiple of M*c_v and the constant term is
|
|
359
|
+
// K*c_v, then v = -K modulo M, so 0 <= v < M forces v to the residue.
|
|
360
|
+
private propagateModResidue(equation: Equation) {
|
|
361
|
+
const poly = equation.poly;
|
|
362
|
+
for (const linear of poly) {
|
|
363
|
+
if (linear.vars.length !== 1) continue;
|
|
364
|
+
const variable = linear.vars[0];
|
|
365
|
+
if (!this.core.varIsInt(variable)) continue;
|
|
366
|
+
const coeff = linear.coeff;
|
|
367
|
+
let modulus = Rational.zero;
|
|
368
|
+
let constant = Rational.zero;
|
|
369
|
+
let isOk = true;
|
|
370
|
+
for (const monomial of poly) {
|
|
371
|
+
if (monomial.vars.length === 1 && monomial.vars[0] === variable) continue;
|
|
372
|
+
if (monomial.vars.length === 0) {
|
|
373
|
+
constant = monomial.coeff;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
const quotient = monomial.coeff.div(coeff);
|
|
377
|
+
if (!quotient.isInt()) {
|
|
378
|
+
isOk = false;
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
modulus = modulus.isZero() ? quotient.abs() : getRationalGcd(modulus, quotient.abs());
|
|
382
|
+
if (modulus.equals(Rational.one)) {
|
|
383
|
+
isOk = false;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (!isOk || modulus.isZero()) continue;
|
|
388
|
+
const offset = constant.div(coeff);
|
|
389
|
+
if (!offset.isInt()) continue;
|
|
390
|
+
const target = Rational.of(getEuclideanMod(offset.neg().num, modulus.num));
|
|
391
|
+
const value = this.core.val(variable);
|
|
392
|
+
if (value.isNegative() || value.compare(modulus) >= 0 || value.equals(target)) continue;
|
|
393
|
+
this.addDependencies(new LemmaBuilder(this.core), equation)
|
|
394
|
+
.addIneq(mkIneq(variable, KIND_LT, 0))
|
|
395
|
+
.addIneq(mkIneq(variable, KIND_GE, modulus))
|
|
396
|
+
.addIneq(mkIneq(variable, KIND_EQ, target));
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// The lemmas of p = v*lc + r for a linear r, by the values of v, lc and r.
|
|
403
|
+
private addQuotientLemma(equation: Equation, variable: number, lc: Pdd, rest: Pdd) {
|
|
404
|
+
const variableValue = this.core.val(variable);
|
|
405
|
+
const restValue = this.getValue(rest);
|
|
406
|
+
const lcValue = this.getValue(lc);
|
|
407
|
+
if (restValue.isZero()) {
|
|
408
|
+
if (variableValue.isZero() || lcValue.isZero() || !lc.isLinear()) return false;
|
|
409
|
+
// v = 0 or lc = 0 or r != 0
|
|
410
|
+
const lcTerm = this.linearToTerm(lc);
|
|
411
|
+
const restTerm = this.linearToTerm(rest);
|
|
412
|
+
this.addDependencies(new LemmaBuilder(this.core), equation)
|
|
413
|
+
.addIneq(mkIneq(variable, KIND_EQ, 0))
|
|
414
|
+
.addIneq(mkIneq(lcTerm.term, KIND_EQ, lcTerm.offset.neg()))
|
|
415
|
+
.addIneq(mkIneq(restTerm.term, KIND_NE, restTerm.offset.neg()));
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
if (variableValue.isZero()) {
|
|
419
|
+
// v = 0 => r = 0
|
|
420
|
+
const lemma = this.addDependencies(new LemmaBuilder(this.core), equation);
|
|
421
|
+
const restTerm = this.linearToTerm(rest);
|
|
422
|
+
lemma
|
|
423
|
+
.addIneq(mkIneq(variable, KIND_NE, 0))
|
|
424
|
+
.addIneq(mkIneq(restTerm.term, KIND_EQ, restTerm.offset.neg()));
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
if (lcValue.isZero()) {
|
|
428
|
+
if (!lc.isLinear()) return false;
|
|
429
|
+
// lc = 0 => r = 0
|
|
430
|
+
const lemma = this.addDependencies(new LemmaBuilder(this.core), equation);
|
|
431
|
+
const lcTerm = this.linearToTerm(lc);
|
|
432
|
+
const restTerm = this.linearToTerm(rest);
|
|
433
|
+
lemma
|
|
434
|
+
.addIneq(mkIneq(lcTerm.term, KIND_NE, lcTerm.offset.neg()))
|
|
435
|
+
.addIneq(mkIneq(restTerm.term, KIND_EQ, restTerm.offset.neg()));
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
if (divides(variableValue, restValue)) return false;
|
|
439
|
+
const isVariablePositive = variableValue.isPositive();
|
|
440
|
+
const isRestPositive = restValue.isPositive();
|
|
441
|
+
if (variableValue.abs().compare(restValue.abs()) > 0) {
|
|
442
|
+
// v*c + t = 0 with |v| > |t| != 0: v changes sign, t changes sign, or |v| <= |t|
|
|
443
|
+
const lemma = new LemmaBuilder(this.core);
|
|
444
|
+
const restTerm = this.linearToTerm(rest);
|
|
445
|
+
this.addDependencies(lemma, equation);
|
|
446
|
+
const offset = restTerm.offset.neg();
|
|
447
|
+
lemma.addIneq(mkIneq(variable, isVariablePositive ? KIND_LE : KIND_GE, 0));
|
|
448
|
+
lemma.addIneq(mkIneq(restTerm.term, isRestPositive ? KIND_LE : KIND_GE, offset));
|
|
449
|
+
const shifted = this.linearToTerm(rest).term;
|
|
450
|
+
shifted.addMonomial(
|
|
451
|
+
Rational.fromNumber(isVariablePositive === isRestPositive ? -1 : 1),
|
|
452
|
+
variable,
|
|
453
|
+
);
|
|
454
|
+
lemma.addIneq(mkIneq(shifted, isRestPositive ? KIND_GE : KIND_LE, offset));
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
const one = Rational.one;
|
|
458
|
+
if (
|
|
459
|
+
lcValue.abs().compare(one) > 0 &&
|
|
460
|
+
variableValue.abs().compare(one) > 0 &&
|
|
461
|
+
variableValue.abs().compare(restValue.abs()) >= 0
|
|
462
|
+
) {
|
|
463
|
+
// v*c + t = 0 & |c| > 1, |v| > 1 => |v| < |t|
|
|
464
|
+
const lemma = new LemmaBuilder(this.core);
|
|
465
|
+
const restTerm = this.linearToTerm(rest);
|
|
466
|
+
const lcTerm = this.linearToTerm(lc);
|
|
467
|
+
this.addDependencies(lemma, equation);
|
|
468
|
+
if (lcValue.compare(one) > 0) {
|
|
469
|
+
lemma.addIneq(mkIneq(lcTerm.term, KIND_LE, one.sub(lcTerm.offset)));
|
|
470
|
+
} else {
|
|
471
|
+
lemma.addIneq(mkIneq(lcTerm.term, KIND_GE, Rational.minusOne.sub(lcTerm.offset)));
|
|
472
|
+
}
|
|
473
|
+
lemma.addIneq(
|
|
474
|
+
isVariablePositive
|
|
475
|
+
? mkIneq(variable, KIND_LE, one)
|
|
476
|
+
: mkIneq(variable, KIND_GE, Rational.minusOne),
|
|
477
|
+
);
|
|
478
|
+
restTerm.term.addMonomial(
|
|
479
|
+
Rational.fromNumber(isVariablePositive === isRestPositive ? -1 : 1),
|
|
480
|
+
variable,
|
|
481
|
+
);
|
|
482
|
+
lemma.addIneq(
|
|
483
|
+
isRestPositive
|
|
484
|
+
? mkIneq(restTerm.term, KIND_GE, one.sub(restTerm.offset))
|
|
485
|
+
: mkIneq(restTerm.term, KIND_LE, Rational.minusOne.sub(restTerm.offset)),
|
|
486
|
+
);
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
private addDependencies(lemma: LemmaBuilder, equation: Equation) {
|
|
493
|
+
const explanation = new Explanation();
|
|
494
|
+
explanation.pushDependency(equation.dep);
|
|
495
|
+
return lemma.addExplanation(explanation);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
private configure() {
|
|
499
|
+
this.solver.reset();
|
|
500
|
+
try {
|
|
501
|
+
this.setLevel2var();
|
|
502
|
+
for (const lpVar of this.core.activeVarSet.elems) {
|
|
503
|
+
if (this.core.lra.isBase(lpVar)) {
|
|
504
|
+
this.addRow(this.core.lra.getRow(this.core.lra.core.heading[lpVar]));
|
|
505
|
+
}
|
|
506
|
+
if (this.core.isMonicVar(lpVar) && this.core.varIsFixed(lpVar)) this.addFixedMonic(lpVar);
|
|
507
|
+
}
|
|
508
|
+
} catch (error) {
|
|
509
|
+
if (!(error instanceof PddMemoryOutError)) throw error;
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
const config = getDefaultPddSolverConfig();
|
|
513
|
+
config.maxSteps = this.solver.equations().length;
|
|
514
|
+
config.maxSimplified = NLA_PARAMS.grobnerMaxSimplified;
|
|
515
|
+
config.eqsGrowth = NLA_PARAMS.grobnerEqsGrowth;
|
|
516
|
+
config.exprSizeGrowth = NLA_PARAMS.grobnerExprSizeGrowth;
|
|
517
|
+
config.exprDegreeGrowth = NLA_PARAMS.grobnerExprDegreeGrowth;
|
|
518
|
+
if (NLA_PARAMS.grobnerAdaptive && this.growthBoost !== ADAPTIVE_UNIT) {
|
|
519
|
+
const scale = (value: number) =>
|
|
520
|
+
Math.min(Math.floor((value * this.growthBoost) / ADAPTIVE_UNIT), UINT_MAX);
|
|
521
|
+
config.eqsGrowth = scale(config.eqsGrowth);
|
|
522
|
+
config.exprSizeGrowth = scale(config.exprSizeGrowth);
|
|
523
|
+
config.exprDegreeGrowth = scale(config.exprDegreeGrowth);
|
|
524
|
+
config.maxSimplified = scale(config.maxSimplified);
|
|
525
|
+
}
|
|
526
|
+
config.numberOfConflictsToReport = NLA_PARAMS.grobnerCnflToReport;
|
|
527
|
+
this.solver.config = config;
|
|
528
|
+
this.solver.adjustConfig();
|
|
529
|
+
this.manager.setMaxNumNodes(10000);
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// equation_is_true: the equation holds at the values, or a free column can make it hold.
|
|
534
|
+
private isEquationTrue(equation: Equation) {
|
|
535
|
+
if (equation.poly.freeVars().some((variable) => this.core.lra.isFree(variable))) return true;
|
|
536
|
+
return this.getValue(equation.poly).isZero();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
private getVarInterval = (variable: number, mode: DepsMode) => {
|
|
540
|
+
const interval = mkDepInterval();
|
|
541
|
+
this.core.intervals.setVarInterval(variable, interval, mode);
|
|
542
|
+
return interval;
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
// is_conflicting: the interval of the polynomial excludes 0, or a cross-nested form or a
|
|
546
|
+
// perfect square shows that it does.
|
|
547
|
+
private isEquationConflicting(equation: Equation) {
|
|
548
|
+
if (this.isEquationTrue(equation)) return false;
|
|
549
|
+
const interval = getPddInterval(equation.poly, "without-deps", this.getVarInterval);
|
|
550
|
+
if (!separatedFromZero(interval)) {
|
|
551
|
+
if (this.addHornerConflict(equation)) return true;
|
|
552
|
+
return NLA_PARAMS.grobnerPerfectSquares && this.addPerfectSquareConflict(equation);
|
|
553
|
+
}
|
|
554
|
+
const intervalWithDeps = getPddInterval(equation.poly, "with-deps", this.getVarInterval);
|
|
555
|
+
return checkIntervalForConflictOnZero(intervalWithDeps, equation.dep, (constraints) =>
|
|
556
|
+
this.addConflict(constraints),
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
private addConflict(constraints: number[]) {
|
|
561
|
+
const explanation = new Explanation();
|
|
562
|
+
for (const constraint of constraints) explanation.push(constraint);
|
|
563
|
+
new LemmaBuilder(this.core).addExplanation(explanation);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// monomial_interval: coeff times the product of the variables' intervals.
|
|
567
|
+
private getMonomialInterval(coeff: Rational, vars: number[], mode: DepsMode) {
|
|
568
|
+
const result = mkDepInterval();
|
|
569
|
+
setValue(result, coeff);
|
|
570
|
+
for (let index = 0; index < vars.length;) {
|
|
571
|
+
const variable = vars[index];
|
|
572
|
+
const start = index;
|
|
573
|
+
while (index < vars.length && vars[index] === variable) index++;
|
|
574
|
+
const powered = mkDepInterval();
|
|
575
|
+
power(this.getVarInterval(variable, mode), index - start, powered, mode);
|
|
576
|
+
const product = mkDepInterval();
|
|
577
|
+
mul(result, powered, product, mode);
|
|
578
|
+
set(result, product, mode);
|
|
579
|
+
}
|
|
580
|
+
return result;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// add_perfect_square_conflict: a triple of monomials a^2*M1^2, b^2*M2^2 and -2ab*M1*M2 is
|
|
584
|
+
// (a*M1 - b*M2)^2, at least 0, which replaces their interval when its lower bound is negative.
|
|
585
|
+
private addPerfectSquareConflict(equation: Equation) {
|
|
586
|
+
const monomials: SortedMonomial[] = [...equation.poly].map((monomial) => ({
|
|
587
|
+
coeff: monomial.coeff,
|
|
588
|
+
vars: monomial.vars.toSorted((first, second) => first - second),
|
|
589
|
+
}));
|
|
590
|
+
const count = monomials.length;
|
|
591
|
+
if (count < 3) return false;
|
|
592
|
+
const isDeleted = Array.from({ length: count }, () => false);
|
|
593
|
+
let isFound = false;
|
|
594
|
+
for (let first = 0; first < count; first++) {
|
|
595
|
+
if (isDeleted[first]) continue;
|
|
596
|
+
const firstRoot = getPerfectSquareRoot(monomials[first].coeff, monomials[first].vars);
|
|
597
|
+
if (firstRoot === undefined) continue;
|
|
598
|
+
for (let second = first + 1; second < count && !isDeleted[first]; second++) {
|
|
599
|
+
if (isDeleted[second]) continue;
|
|
600
|
+
const secondRoot = getPerfectSquareRoot(monomials[second].coeff, monomials[second].vars);
|
|
601
|
+
if (secondRoot === undefined) continue;
|
|
602
|
+
for (let cross = first + 1; cross < count; cross++) {
|
|
603
|
+
if (cross === second || isDeleted[cross]) continue;
|
|
604
|
+
if (
|
|
605
|
+
!isPerfectSquareCross(
|
|
606
|
+
firstRoot,
|
|
607
|
+
monomials[first].vars,
|
|
608
|
+
secondRoot,
|
|
609
|
+
monomials[second].vars,
|
|
610
|
+
monomials[cross].coeff,
|
|
611
|
+
monomials[cross].vars,
|
|
612
|
+
)
|
|
613
|
+
)
|
|
614
|
+
continue;
|
|
615
|
+
const partial = mkDepInterval();
|
|
616
|
+
const sum = mkDepInterval();
|
|
617
|
+
add(
|
|
618
|
+
this.getSortedInterval(monomials[first]),
|
|
619
|
+
this.getSortedInterval(monomials[second]),
|
|
620
|
+
partial,
|
|
621
|
+
"without-deps",
|
|
622
|
+
);
|
|
623
|
+
add(partial, this.getSortedInterval(monomials[cross]), sum, "without-deps");
|
|
624
|
+
if (sum.isLowerInf || sum.lower.isNegative()) {
|
|
625
|
+
isDeleted[first] = true;
|
|
626
|
+
isDeleted[second] = true;
|
|
627
|
+
isDeleted[cross] = true;
|
|
628
|
+
isFound = true;
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
if (!isFound) return false;
|
|
635
|
+
// [0, oo) for the perfect squares is an unconditional bound, without a dependency.
|
|
636
|
+
const sum = mkDepInterval();
|
|
637
|
+
sum.isLowerInf = false;
|
|
638
|
+
sum.lower = Rational.zero;
|
|
639
|
+
sum.isLowerOpen = false;
|
|
640
|
+
for (let index = 0; index < count; index++) {
|
|
641
|
+
if (isDeleted[index]) continue;
|
|
642
|
+
const monomial = monomials[index];
|
|
643
|
+
const next = mkDepInterval();
|
|
644
|
+
add(
|
|
645
|
+
sum,
|
|
646
|
+
this.getMonomialInterval(monomial.coeff, monomial.vars, "with-deps"),
|
|
647
|
+
next,
|
|
648
|
+
"with-deps",
|
|
649
|
+
);
|
|
650
|
+
set(sum, next, "with-deps");
|
|
651
|
+
}
|
|
652
|
+
return checkIntervalForConflictOnZero(sum, equation.dep, (constraints) =>
|
|
653
|
+
this.addConflict(constraints),
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
private getSortedInterval(monomial: SortedMonomial) {
|
|
658
|
+
return this.getMonomialInterval(monomial.coeff, monomial.vars, "without-deps");
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// add_horner_conflict: the cross-nested forms of the polynomial, whose intervals may exclude 0.
|
|
662
|
+
private addHornerConflict(equation: Equation) {
|
|
663
|
+
const nexCreator = this.nexCreator;
|
|
664
|
+
const freeVars = equation.poly.freeVars();
|
|
665
|
+
const var2nex: Nex[] = [];
|
|
666
|
+
for (const variable of freeVars) var2nex[variable] = nexCreator.mkVar(variable);
|
|
667
|
+
nexCreator.setNumberOfVars(Math.max(0, ...freeVars) + 1);
|
|
668
|
+
const children: Nex[] = [];
|
|
669
|
+
for (const monomial of equation.poly) {
|
|
670
|
+
if (monomial.vars.length === 0) {
|
|
671
|
+
children.push(nexCreator.mkScalar(monomial.coeff));
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
const factors = monomial.vars.map((innerVar) => var2nex[innerVar]);
|
|
675
|
+
children.push(nexCreator.mkMul(monomial.coeff, ...factors));
|
|
676
|
+
}
|
|
677
|
+
const expr = nexCreator.simplify(nexCreator.mkSum(children));
|
|
678
|
+
if (getDegree(expr) < 2 || expr.kind !== "sum") return false;
|
|
679
|
+
const dep = equation.dep;
|
|
680
|
+
const crossNested = new CrossNested(
|
|
681
|
+
(nex) => this.core.intervals.checkNex(nex, dep),
|
|
682
|
+
(variable) => this.core.varIsFixed(variable),
|
|
683
|
+
{ inc: () => !this.core.isCanceled() },
|
|
684
|
+
this.core.random(),
|
|
685
|
+
nexCreator,
|
|
686
|
+
);
|
|
687
|
+
crossNested.run(expr);
|
|
688
|
+
return crossNested.done();
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// propagate_linear_equations: an equation whose monomials all have columns and that the values
|
|
692
|
+
// violate becomes a new row fixed to its constant.
|
|
693
|
+
private propagateLinearEquations() {
|
|
694
|
+
this.mon2var.clear();
|
|
695
|
+
for (const monic of this.core.emons.monics)
|
|
696
|
+
this.mon2var.set(monic.vars.join(","), monic.variable);
|
|
697
|
+
let changed = 0;
|
|
698
|
+
for (const equation of this.solver.equations()) {
|
|
699
|
+
if (this.propagateLinearEquation(equation)) changed++;
|
|
700
|
+
}
|
|
701
|
+
return changed > 0;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// The monomial's column: Z3 looks up the variables in the pdd's order in the sorted variables of
|
|
705
|
+
// the monics.
|
|
706
|
+
private getMonomialColumn(vars: number[]) {
|
|
707
|
+
return this.mon2var.get(vars.join(","));
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
private propagateLinearEquation(equation: Equation) {
|
|
711
|
+
if (this.isEquationTrue(equation)) return false;
|
|
712
|
+
let value = Rational.zero;
|
|
713
|
+
const columns: number[] = [];
|
|
714
|
+
for (const monomial of equation.poly) {
|
|
715
|
+
if (monomial.vars.length === 0) {
|
|
716
|
+
value = value.add(monomial.coeff);
|
|
717
|
+
continue;
|
|
718
|
+
}
|
|
719
|
+
const column =
|
|
720
|
+
monomial.vars.length === 1 ? monomial.vars[0] : this.getMonomialColumn(monomial.vars);
|
|
721
|
+
if (column === undefined) return false;
|
|
722
|
+
columns.push(column);
|
|
723
|
+
value = value.add(monomial.coeff.mul(this.core.val(column)));
|
|
724
|
+
}
|
|
725
|
+
if (value.isZero()) return false;
|
|
726
|
+
let lcm = Rational.one;
|
|
727
|
+
for (const monomial of equation.poly) lcm = getRationalLcm(getDenominator(monomial.coeff), lcm);
|
|
728
|
+
const coeffs: [number, Rational][] = [];
|
|
729
|
+
let offset = Rational.zero;
|
|
730
|
+
for (const monomial of equation.poly) {
|
|
731
|
+
const coeff = lcm.mul(monomial.coeff);
|
|
732
|
+
if (monomial.vars.length === 0) offset = offset.sub(coeff);
|
|
733
|
+
else coeffs.push([columns[coeffs.length], coeff]);
|
|
734
|
+
}
|
|
735
|
+
const lra = this.core.lra;
|
|
736
|
+
const column = lra.addTerm(coeffs, NULL_LPVAR);
|
|
737
|
+
lra.updateColumnTypeAndBound(column, KIND_EQ, new Impq(offset), equation.dep);
|
|
738
|
+
this.core.isCheckFeasible = true;
|
|
739
|
+
return true;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// find_nl_cluster: the variables and rows reachable from the monomials to refine.
|
|
743
|
+
private findNlCluster() {
|
|
744
|
+
this.rows.clear();
|
|
745
|
+
this.core.activeVarSet.clear();
|
|
746
|
+
const queue = [...this.core.toRefine.elems];
|
|
747
|
+
for (let lpVar = queue.pop(); lpVar !== undefined; lpVar = queue.pop()) {
|
|
748
|
+
this.addVarAndItsFactorsToQueue(lpVar, queue);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// add_var_and_its_factors_to_q_and_collect_new_rows.
|
|
753
|
+
private addVarAndItsFactorsToQueue(lpVar: number, queue: number[]) {
|
|
754
|
+
const core = this.core;
|
|
755
|
+
const lra = core.lra;
|
|
756
|
+
if (core.activeVarSet.has(lpVar)) return;
|
|
757
|
+
core.activeVarSet.add(lpVar);
|
|
758
|
+
if (core.isMonicVar(lpVar)) {
|
|
759
|
+
for (const factorization of core.getFactorizations(core.emons.get(lpVar))) {
|
|
760
|
+
for (const factor of factorization.factors) queue.push(factor.variable);
|
|
761
|
+
}
|
|
762
|
+
} else if (lra.hasTerm(lpVar)) {
|
|
763
|
+
for (const entry of lra.getTerm(lpVar).coeffs) queue.push(entry[0]);
|
|
764
|
+
} else if (core.varIsFixed(lpVar)) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
for (const cell of lra.core.matrix.columns[lpVar]) {
|
|
768
|
+
const row = cell.row;
|
|
769
|
+
if (this.rows.has(row)) continue;
|
|
770
|
+
this.rows.add(row);
|
|
771
|
+
const basic = lra.core.basis[row];
|
|
772
|
+
// A free column over the reals can be assigned.
|
|
773
|
+
if (lra.isFree(basic) && basic !== lpVar) {
|
|
774
|
+
if (!lra.isInt(basic)) continue;
|
|
775
|
+
// Free integer columns count only for the gcd test, the quotients, or all equations.
|
|
776
|
+
if (!this.isAddingAllEqs && !this.hasGcdTest && !this.propagatesQuotients) continue;
|
|
777
|
+
// A free integer column with integer coefficients can be assigned.
|
|
778
|
+
if (!this.isAddingAllEqs && lra.getRow(row).every((rowCell) => rowCell.coeff.isInt()))
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
if (!this.isAddingAllEqs && lra.getRow(row).length > NLA_PARAMS.hornerRowLengthLimit)
|
|
782
|
+
continue;
|
|
783
|
+
for (const rowCell of lra.getRow(row)) this.addVarAndItsFactorsToQueue(rowCell.column, queue);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
private getValOfFixedVarWithDeps(lpVar: number, ref: DepRef) {
|
|
788
|
+
const witnesses = this.core.lra.getBoundWitnesses(lpVar);
|
|
789
|
+
if (witnesses !== null) ref.dep = mkJoin(ref.dep, witnesses);
|
|
790
|
+
return this.core.lra.getLowerBound(lpVar).x;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
private getTermPdd(term: LarTerm, ref: DepRef) {
|
|
794
|
+
let result = this.manager.mkVal(Rational.zero);
|
|
795
|
+
for (const entry of term.coeffs) result = result.add(this.getPdd(entry[1], entry[0], ref));
|
|
796
|
+
return result;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
// pdd_expr: coeff times the column, with fixed columns as values, terms expanded, and monics as
|
|
800
|
+
// the products of their variables.
|
|
801
|
+
private getPdd(coeff: Rational, lpVar: number, ref: DepRef) {
|
|
802
|
+
const core = this.core;
|
|
803
|
+
let result = this.manager.mkVal(coeff);
|
|
804
|
+
const vars = [lpVar];
|
|
805
|
+
const zeroRef = { dep: ref.dep };
|
|
806
|
+
for (let current = vars.pop(); current !== undefined; current = vars.pop()) {
|
|
807
|
+
if (NLA_PARAMS.grobnerSubsFixed > 0 && core.varIsFixedToZero(current)) {
|
|
808
|
+
result = this.manager.mkVal(this.getValOfFixedVarWithDeps(current, zeroRef));
|
|
809
|
+
ref.dep = zeroRef.dep;
|
|
810
|
+
return result;
|
|
811
|
+
}
|
|
812
|
+
if (NLA_PARAMS.grobnerSubsFixed === 1 && core.varIsFixed(current)) {
|
|
813
|
+
result = result.mul(this.getValOfFixedVarWithDeps(current, ref));
|
|
814
|
+
} else if (this.expandsTerms && core.lra.hasTerm(current)) {
|
|
815
|
+
result = result.mul(this.getTermPdd(core.lra.getTerm(current), ref));
|
|
816
|
+
} else if (!core.isMonicVar(current)) {
|
|
817
|
+
result = result.mul(this.manager.mkVar(current));
|
|
818
|
+
} else {
|
|
819
|
+
vars.push(...core.emons.get(current).vars);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return result;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// is_solved: p = 0 as v = r, for a linear p where v is the only variable bounded on one side
|
|
826
|
+
// while several are bounded on the other, so that r has no bound on that side.
|
|
827
|
+
private getSolvedForm(poly: Pdd) {
|
|
828
|
+
if (!poly.isLinear()) return null;
|
|
829
|
+
let numLo = 0;
|
|
830
|
+
let numHi = 0;
|
|
831
|
+
let lo = 0;
|
|
832
|
+
let hi = 0;
|
|
833
|
+
let loCoeff = Rational.zero;
|
|
834
|
+
let hiCoeff = Rational.zero;
|
|
835
|
+
for (let current = poly; !current.isVal(); current = current.lo()) {
|
|
836
|
+
const variable = current.var();
|
|
837
|
+
const value = current.hi().val();
|
|
838
|
+
const type = this.core.lra.core.getColumnType(variable);
|
|
839
|
+
if (type === COLUMN_LOWER || type === COLUMN_UPPER) {
|
|
840
|
+
if (value.isPositive() === (type === COLUMN_LOWER)) {
|
|
841
|
+
numLo++;
|
|
842
|
+
lo = variable;
|
|
843
|
+
loCoeff = value;
|
|
844
|
+
} else {
|
|
845
|
+
numHi++;
|
|
846
|
+
hi = variable;
|
|
847
|
+
hiCoeff = value;
|
|
848
|
+
}
|
|
849
|
+
} else if (type !== COLUMN_FIXED && type !== COLUMN_BOXED) {
|
|
850
|
+
return null;
|
|
851
|
+
}
|
|
852
|
+
if (numLo > 1 && numHi > 1) return null;
|
|
853
|
+
}
|
|
854
|
+
const isLo = numLo === 1 && numHi > 1;
|
|
855
|
+
if (!isLo && !(numHi === 1 && numLo > 1)) return null;
|
|
856
|
+
const variable = isLo ? lo : hi;
|
|
857
|
+
const coeff = isLo ? loCoeff : hiCoeff;
|
|
858
|
+
const solution = this.manager.mkVar(variable).mul(coeff).sub(poly);
|
|
859
|
+
return {
|
|
860
|
+
variable,
|
|
861
|
+
poly: coeff.equals(Rational.one) ? solution : solution.mul(Rational.one.div(coeff)),
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// add_eq: adds the equation in solved form when it has one.
|
|
866
|
+
private addEq(poly: Pdd, dep: Dependency) {
|
|
867
|
+
const simplified = this.solver.simplify(poly, dep);
|
|
868
|
+
const solved = this.getSolvedForm(simplified.poly);
|
|
869
|
+
if (solved === null) this.solver.add(simplified.poly, simplified.dep);
|
|
870
|
+
else this.solver.addSubst(solved.variable, solved.poly, simplified.dep);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
private addFixedMonic(lpVar: number) {
|
|
874
|
+
const ref: DepRef = { dep: null };
|
|
875
|
+
let product = this.manager.mkVal(Rational.one);
|
|
876
|
+
for (const factor of this.core.emons.get(lpVar).vars) {
|
|
877
|
+
product = product.mul(this.getPdd(Rational.one, factor, ref));
|
|
878
|
+
}
|
|
879
|
+
product = product.sub(this.getValOfFixedVarWithDeps(lpVar, ref));
|
|
880
|
+
this.addEq(product, ref.dep);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
private addRow(row: RowCell[]) {
|
|
884
|
+
const ref: DepRef = { dep: null };
|
|
885
|
+
let sum = this.manager.mkVal(Rational.zero);
|
|
886
|
+
for (const cell of row) sum = sum.add(this.getPdd(cell.coeff, cell.column, ref));
|
|
887
|
+
this.addEq(sum, ref.dep);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// set_level2var: the columns by weight, then index, from the lowest level.
|
|
891
|
+
private setLevel2var() {
|
|
892
|
+
const core = this.core;
|
|
893
|
+
const count = core.lra.getColumnCount();
|
|
894
|
+
const weights = Array.from({ length: count }, (_, lpVar) => core.getVarWeight(lpVar));
|
|
895
|
+
for (let lpVar = 0; lpVar < count; lpVar++) {
|
|
896
|
+
if (!core.isMonicVar(lpVar) || !core.toRefine.has(lpVar)) continue;
|
|
897
|
+
for (const factor of core.emons.get(lpVar).vars) weights[factor] += 6;
|
|
898
|
+
}
|
|
899
|
+
const level2var = Array.from({ length: count }, (_, lpVar) => lpVar).sort(
|
|
900
|
+
(first, second) => weights[first] - weights[second] || first - second,
|
|
901
|
+
);
|
|
902
|
+
this.manager.reset(level2var);
|
|
903
|
+
}
|
|
904
|
+
}
|