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,332 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/lp/nla_divisions.h, src/math/lp/nla_divisions.cpp), MIT license, see
|
|
2
|
+
// z3-license.txt. The lemmas of nla on div, mod and /: monotonicity in the dividend and the
|
|
3
|
+
// divisor, bounded divisions by numerals, and divisibility closures over a shared divisor. The
|
|
4
|
+
// check is skipped while nla uses nlsat's model.
|
|
5
|
+
import { getEuclideanDiv, Rational } from "../rational";
|
|
6
|
+
import { KIND_EQ, KIND_GE, KIND_GT, KIND_LE, KIND_LT, KIND_NE } from "./constraints";
|
|
7
|
+
import { NULL_LPVAR } from "./lar-solver";
|
|
8
|
+
import { LarTerm } from "./lar-term";
|
|
9
|
+
import { type NlaCore } from "./nla-core";
|
|
10
|
+
import { LemmaBuilder, mkIneq } from "./nla-types";
|
|
11
|
+
|
|
12
|
+
// A division q = x / y with r its remainder, or a divisibility fact r = mod(x, y), q = div(x, y).
|
|
13
|
+
export interface Division {
|
|
14
|
+
readonly quotient: number;
|
|
15
|
+
readonly dividend: number;
|
|
16
|
+
readonly divisor: number;
|
|
17
|
+
readonly remainder: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface DivisionValues {
|
|
21
|
+
readonly dividend: number;
|
|
22
|
+
readonly dividendValue: Rational;
|
|
23
|
+
readonly divisor: number;
|
|
24
|
+
readonly divisorValue: Rational;
|
|
25
|
+
readonly quotient: number;
|
|
26
|
+
readonly quotientValue: Rational;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// div on integers: Euclidean division, as mpz_manager::div.
|
|
30
|
+
const getIntDiv = (dividend: Rational, divisor: Rational) =>
|
|
31
|
+
Rational.of(getEuclideanDiv(dividend.num, divisor.num));
|
|
32
|
+
|
|
33
|
+
const hasNullColumn = (division: Division) =>
|
|
34
|
+
division.quotient === NULL_LPVAR ||
|
|
35
|
+
division.dividend === NULL_LPVAR ||
|
|
36
|
+
division.divisor === NULL_LPVAR ||
|
|
37
|
+
division.remainder === NULL_LPVAR;
|
|
38
|
+
|
|
39
|
+
const mkDifference = (first: number, second: number) =>
|
|
40
|
+
new LarTerm([
|
|
41
|
+
[first, Rational.one],
|
|
42
|
+
[second, Rational.minusOne],
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
export class Divisions {
|
|
46
|
+
private idivisions: Division[] = [];
|
|
47
|
+
private boundedDivisions: Division[] = [];
|
|
48
|
+
// Divisibility facts, whose quotient is div(x, y) and remainder mod(x, y).
|
|
49
|
+
private divisibility: Division[] = [];
|
|
50
|
+
|
|
51
|
+
constructor(private core: NlaCore) {}
|
|
52
|
+
|
|
53
|
+
private pushDivision(divisions: Division[], division: Division) {
|
|
54
|
+
divisions.push(division);
|
|
55
|
+
this.core.emons.trail.push(() => divisions.pop());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
addIdivision(division: Division) {
|
|
59
|
+
if (hasNullColumn(division)) return;
|
|
60
|
+
this.pushDivision(this.idivisions, division);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
addBoundedDivision(division: Division) {
|
|
64
|
+
if (hasNullColumn(division)) return;
|
|
65
|
+
const lra = this.core.lra;
|
|
66
|
+
if (lra.hasTerm(division.dividend) || lra.hasTerm(division.divisor)) return;
|
|
67
|
+
if (lra.hasTerm(division.quotient)) return;
|
|
68
|
+
this.pushDivision(this.boundedDivisions, division);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
addDivisibility(division: Division) {
|
|
72
|
+
if (hasNullColumn(division)) return;
|
|
73
|
+
this.pushDivision(this.divisibility, division);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// y1 >= y2 > 0 & 0 <= x1 <= x2 => x1/y1 <= x2/y2
|
|
77
|
+
private monotonicity1(first: DivisionValues, second: DivisionValues) {
|
|
78
|
+
if (
|
|
79
|
+
!(
|
|
80
|
+
first.divisorValue.compare(second.divisorValue) >= 0 &&
|
|
81
|
+
second.divisorValue.isPositive() &&
|
|
82
|
+
!first.dividendValue.isNegative() &&
|
|
83
|
+
first.dividendValue.compare(second.dividendValue) <= 0 &&
|
|
84
|
+
first.quotientValue.compare(second.quotientValue) > 0
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
return false;
|
|
88
|
+
const lemma = new LemmaBuilder(this.core);
|
|
89
|
+
lemma.addIneq(mkIneq(mkDifference(first.divisor, second.divisor), KIND_LT, 0));
|
|
90
|
+
lemma.addIneq(mkIneq(second.divisor, KIND_LE, 0));
|
|
91
|
+
lemma.addIneq(mkIneq(first.dividend, KIND_LT, 0));
|
|
92
|
+
lemma.addIneq(mkIneq(mkDifference(first.dividend, second.dividend), KIND_GT, 0));
|
|
93
|
+
lemma.addIneq(mkIneq(mkDifference(first.quotient, second.quotient), KIND_LE, 0));
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// y2 <= y1 < 0 & x1 >= x2 >= 0 => x1/y1 <= x2/y2
|
|
98
|
+
private monotonicity2(first: DivisionValues, second: DivisionValues) {
|
|
99
|
+
if (
|
|
100
|
+
!(
|
|
101
|
+
second.divisorValue.compare(first.divisorValue) <= 0 &&
|
|
102
|
+
first.divisorValue.isNegative() &&
|
|
103
|
+
first.dividendValue.compare(second.dividendValue) >= 0 &&
|
|
104
|
+
!second.dividendValue.isNegative() &&
|
|
105
|
+
first.quotientValue.compare(second.quotientValue) > 0
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return false;
|
|
109
|
+
const lemma = new LemmaBuilder(this.core);
|
|
110
|
+
lemma.addIneq(mkIneq(mkDifference(first.divisor, second.divisor), KIND_LT, 0));
|
|
111
|
+
lemma.addIneq(mkIneq(first.divisor, KIND_GE, 0));
|
|
112
|
+
lemma.addIneq(mkIneq(mkDifference(first.dividend, second.dividend), KIND_LT, 0));
|
|
113
|
+
lemma.addIneq(mkIneq(second.dividend, KIND_LT, 0));
|
|
114
|
+
lemma.addIneq(mkIneq(mkDifference(first.quotient, second.quotient), KIND_LE, 0));
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// y2 <= y1 < 0 & x1 <= x2 <= 0 => x1/y1 >= x2/y2
|
|
119
|
+
private monotonicity3(first: DivisionValues, second: DivisionValues) {
|
|
120
|
+
if (
|
|
121
|
+
!(
|
|
122
|
+
second.divisorValue.compare(first.divisorValue) <= 0 &&
|
|
123
|
+
first.divisorValue.isNegative() &&
|
|
124
|
+
first.dividendValue.compare(second.dividendValue) <= 0 &&
|
|
125
|
+
!second.dividendValue.isPositive() &&
|
|
126
|
+
first.quotientValue.compare(second.quotientValue) < 0
|
|
127
|
+
)
|
|
128
|
+
)
|
|
129
|
+
return false;
|
|
130
|
+
const lemma = new LemmaBuilder(this.core);
|
|
131
|
+
lemma.addIneq(mkIneq(mkDifference(first.divisor, second.divisor), KIND_LT, 0));
|
|
132
|
+
lemma.addIneq(mkIneq(first.divisor, KIND_GE, 0));
|
|
133
|
+
lemma.addIneq(mkIneq(mkDifference(first.dividend, second.dividend), KIND_GT, 0));
|
|
134
|
+
lemma.addIneq(mkIneq(second.dividend, KIND_GT, 0));
|
|
135
|
+
lemma.addIneq(mkIneq(mkDifference(first.quotient, second.quotient), KIND_GE, 0));
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private monotonicity(first: DivisionValues, second: DivisionValues) {
|
|
140
|
+
return (
|
|
141
|
+
this.monotonicity1(first, second) ||
|
|
142
|
+
this.monotonicity1(second, first) ||
|
|
143
|
+
this.monotonicity2(first, second) ||
|
|
144
|
+
this.monotonicity2(second, first) ||
|
|
145
|
+
this.monotonicity3(first, second) ||
|
|
146
|
+
this.monotonicity3(second, first)
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private getValues(division: Division): DivisionValues {
|
|
151
|
+
const core = this.core;
|
|
152
|
+
return {
|
|
153
|
+
dividend: division.dividend,
|
|
154
|
+
dividendValue: core.val(division.dividend),
|
|
155
|
+
divisor: division.divisor,
|
|
156
|
+
divisorValue: core.val(division.divisor),
|
|
157
|
+
quotient: division.quotient,
|
|
158
|
+
quotientValue: core.val(division.quotient),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// The monotonicity lemmas of the integer divisions whose quotients are wrong, against the others.
|
|
163
|
+
// Z3's theory_lra registers no real divisions, so the rdivisions it would also check stay empty.
|
|
164
|
+
private checkMonotonicity(divisions: Division[]) {
|
|
165
|
+
const core = this.core;
|
|
166
|
+
for (const division of divisions) {
|
|
167
|
+
if (!core.isRelevant(division.quotient)) continue;
|
|
168
|
+
const values = this.getValues(division);
|
|
169
|
+
const dividendValue = values.dividendValue;
|
|
170
|
+
const divisorValue = values.divisorValue;
|
|
171
|
+
if (divisorValue.isZero()) continue;
|
|
172
|
+
if (!dividendValue.isInt() || !divisorValue.isInt()) continue;
|
|
173
|
+
if (values.quotientValue.equals(getIntDiv(dividendValue, divisorValue))) continue;
|
|
174
|
+
for (const other of divisions) {
|
|
175
|
+
if (other.quotient === division.quotient || !core.isRelevant(other.quotient)) continue;
|
|
176
|
+
if (this.monotonicity(values, this.getValues(other))) return true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
check() {
|
|
183
|
+
if (this.core.isUsingNraModel) return;
|
|
184
|
+
if (this.checkMonotonicity(this.idivisions)) return;
|
|
185
|
+
this.checkModMult();
|
|
186
|
+
this.checkLinearDivisibility();
|
|
187
|
+
this.checkModCongruence();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// p <= q * div(r, q) + q - 1 => div(p, q) <= div(r, q), and p >= q * div(r, q) =>
|
|
191
|
+
// div(r, q) <= div(p, q), for a bounded p, a value q and r = eval(p).
|
|
192
|
+
checkBoundedDivisions() {
|
|
193
|
+
const core = this.core;
|
|
194
|
+
const offset = core.random();
|
|
195
|
+
const size = this.boundedDivisions.length;
|
|
196
|
+
for (let index = 0; index < size; index++) {
|
|
197
|
+
const division = this.boundedDivisions[(offset + index) % size];
|
|
198
|
+
const quotient = division.quotient;
|
|
199
|
+
if (!core.isRelevant(quotient)) continue;
|
|
200
|
+
const dividendValue = core.val(division.dividend);
|
|
201
|
+
const divisorValue = core.val(division.divisor);
|
|
202
|
+
if (dividendValue.isNegative() || !dividendValue.isInt()) continue;
|
|
203
|
+
if (!divisorValue.isPositive() || !divisorValue.isInt()) continue;
|
|
204
|
+
const divValue = getIntDiv(dividendValue, divisorValue);
|
|
205
|
+
if (core.val(quotient).equals(divValue)) continue;
|
|
206
|
+
const high = divisorValue.mul(divValue).add(divisorValue).sub(Rational.one);
|
|
207
|
+
const low = divisorValue.mul(divValue);
|
|
208
|
+
if (dividendValue.compare(high) > 0) {
|
|
209
|
+
const lemma = new LemmaBuilder(core);
|
|
210
|
+
lemma.addIneq(mkIneq(division.divisor, KIND_NE, divisorValue));
|
|
211
|
+
lemma.addIneq(mkIneq(division.dividend, KIND_GT, high));
|
|
212
|
+
lemma.addIneq(mkIneq(quotient, KIND_LE, divValue));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (dividendValue.compare(low) < 0) {
|
|
216
|
+
const lemma = new LemmaBuilder(core);
|
|
217
|
+
lemma.addIneq(mkIneq(division.divisor, KIND_NE, divisorValue));
|
|
218
|
+
lemma.addIneq(mkIneq(division.dividend, KIND_LT, low));
|
|
219
|
+
lemma.addIneq(mkIneq(quotient, KIND_GE, divValue));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// mod(factor, p) = 0 => mod(factor * k, p) = 0
|
|
226
|
+
private checkModMult() {
|
|
227
|
+
const core = this.core;
|
|
228
|
+
const offset = core.random();
|
|
229
|
+
const size = this.boundedDivisions.length;
|
|
230
|
+
for (let index = 0; index < size; index++) {
|
|
231
|
+
const division = this.boundedDivisions[(offset + index) % size];
|
|
232
|
+
const remainder = division.remainder;
|
|
233
|
+
if (!core.isRelevant(division.quotient)) continue;
|
|
234
|
+
if (core.varIsFixedToZero(remainder) || core.val(remainder).isZero()) continue;
|
|
235
|
+
if (!core.isMonicVar(division.dividend)) continue;
|
|
236
|
+
const divisorValue = core.val(division.divisor);
|
|
237
|
+
if (!divisorValue.isPositive() || !divisorValue.isInt()) continue;
|
|
238
|
+
for (const factor of core.emons.get(division.dividend).vars) {
|
|
239
|
+
for (const other of this.boundedDivisions) {
|
|
240
|
+
if (other.dividend !== factor) continue;
|
|
241
|
+
if (!core.val(other.divisor).equals(divisorValue)) continue;
|
|
242
|
+
if (!core.varIsFixedToZero(other.remainder)) continue;
|
|
243
|
+
const lemma = new LemmaBuilder(core);
|
|
244
|
+
lemma.addIneq(mkIneq(other.remainder, KIND_NE, 0));
|
|
245
|
+
lemma.addIneq(mkIneq(remainder, KIND_EQ, 0));
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// y != 0 & mod(a, y) = 0 & x = c * a, for an integer constant c, => mod(x, y) = 0
|
|
253
|
+
private checkLinearDivisibility() {
|
|
254
|
+
const core = this.core;
|
|
255
|
+
const size = this.divisibility.length;
|
|
256
|
+
for (let index = 0; index < size; index++) {
|
|
257
|
+
const fact = this.divisibility[index];
|
|
258
|
+
const x = fact.dividend;
|
|
259
|
+
const y = fact.divisor;
|
|
260
|
+
if (!core.isRelevant(fact.remainder)) continue;
|
|
261
|
+
if (core.val(fact.remainder).isZero()) continue;
|
|
262
|
+
const xValue = core.val(x);
|
|
263
|
+
const yValue = core.val(y);
|
|
264
|
+
if (yValue.isZero() || xValue.isZero()) continue;
|
|
265
|
+
for (let otherIndex = 0; otherIndex < size; otherIndex++) {
|
|
266
|
+
if (index === otherIndex) continue;
|
|
267
|
+
const other = this.divisibility[otherIndex];
|
|
268
|
+
const a = other.dividend;
|
|
269
|
+
const y2 = other.divisor;
|
|
270
|
+
if (y2 !== y && !core.val(y2).equals(yValue)) continue;
|
|
271
|
+
if (!core.isRelevant(other.remainder)) continue;
|
|
272
|
+
if (!core.val(other.remainder).isZero()) continue;
|
|
273
|
+
const aValue = core.val(a);
|
|
274
|
+
if (aValue.isZero()) continue;
|
|
275
|
+
const coeff = xValue.div(aValue);
|
|
276
|
+
if (!coeff.isInt() || coeff.isZero()) continue;
|
|
277
|
+
if (!xValue.equals(coeff.mul(aValue))) continue;
|
|
278
|
+
const lemma = new LemmaBuilder(core);
|
|
279
|
+
lemma.addIneq(mkIneq(y, KIND_EQ, 0));
|
|
280
|
+
if (y2 !== y) lemma.addIneq(mkIneq(mkDifference(y, y2), KIND_NE, 0));
|
|
281
|
+
lemma.addIneq(
|
|
282
|
+
mkIneq(
|
|
283
|
+
new LarTerm([
|
|
284
|
+
[x, Rational.one],
|
|
285
|
+
[a, coeff.neg()],
|
|
286
|
+
]),
|
|
287
|
+
KIND_NE,
|
|
288
|
+
0,
|
|
289
|
+
),
|
|
290
|
+
);
|
|
291
|
+
lemma.addIneq(mkIneq(other.remainder, KIND_NE, 0));
|
|
292
|
+
lemma.addIneq(mkIneq(fact.remainder, KIND_EQ, 0));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// div(x,y) - div(s,y) = delta => mod(x,y) - mod(s,y) = (x - s) - delta*y, for y != 0
|
|
299
|
+
private checkModCongruence() {
|
|
300
|
+
const core = this.core;
|
|
301
|
+
const size = this.divisibility.length;
|
|
302
|
+
for (let index = 0; index < size; index++) {
|
|
303
|
+
const fact = this.divisibility[index];
|
|
304
|
+
const y = fact.divisor;
|
|
305
|
+
if (!core.isRelevant(fact.remainder)) continue;
|
|
306
|
+
const yValue = core.val(y);
|
|
307
|
+
if (yValue.isZero()) continue;
|
|
308
|
+
for (let otherIndex = index + 1; otherIndex < size; otherIndex++) {
|
|
309
|
+
const other = this.divisibility[otherIndex];
|
|
310
|
+
const y2 = other.divisor;
|
|
311
|
+
if (!core.isRelevant(other.remainder)) continue;
|
|
312
|
+
if (y2 !== y && !core.val(y2).equals(yValue)) continue;
|
|
313
|
+
const delta = core.val(fact.quotient).sub(core.val(other.quotient));
|
|
314
|
+
const left = core.val(fact.remainder).sub(core.val(other.remainder));
|
|
315
|
+
const right = core.val(fact.dividend).sub(core.val(other.dividend)).sub(delta.mul(yValue));
|
|
316
|
+
if (left.equals(right)) continue;
|
|
317
|
+
const lemma = new LemmaBuilder(core);
|
|
318
|
+
lemma.addIneq(mkIneq(y, KIND_EQ, 0));
|
|
319
|
+
if (y2 !== y) lemma.addIneq(mkIneq(mkDifference(y, y2), KIND_NE, 0));
|
|
320
|
+
lemma.addIneq(mkIneq(mkDifference(fact.quotient, other.quotient), KIND_NE, delta));
|
|
321
|
+
const term = new LarTerm();
|
|
322
|
+
term.addMonomial(Rational.one, fact.remainder);
|
|
323
|
+
term.addMonomial(Rational.minusOne, other.remainder);
|
|
324
|
+
term.addMonomial(Rational.minusOne, fact.dividend);
|
|
325
|
+
term.addMonomial(Rational.one, other.dividend);
|
|
326
|
+
term.addMonomial(delta, y);
|
|
327
|
+
lemma.addIneq(mkIneq(term, KIND_EQ, 0));
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
// Ported from Z3 (src/math/lp/monic.h, src/math/lp/emonics.h, src/math/lp/emonics.cpp), MIT
|
|
2
|
+
// license, see z3-license.txt. The monomials v := v1 * ... * vn of nla, canonized over the
|
|
3
|
+
// variable equivalences, with the cyclic use lists of their variables and the congruence classes
|
|
4
|
+
// of monomials equal up to sign.
|
|
5
|
+
import { getSignedVarVar, isSignedVarNegated, negateSignedVar, VarEqs } from "./nla-var-eqs";
|
|
6
|
+
import { TrailStack, UnionFind } from "./union-find";
|
|
7
|
+
|
|
8
|
+
const compareNumbers = (first: number, second: number) => first - second;
|
|
9
|
+
|
|
10
|
+
export class Monic {
|
|
11
|
+
readonly vars: number[];
|
|
12
|
+
// The roots of the variables, sorted, and the parity of their signs.
|
|
13
|
+
rvars: number[] = [];
|
|
14
|
+
rsign = false;
|
|
15
|
+
visited = 0;
|
|
16
|
+
isPropagated = false;
|
|
17
|
+
isBoundPropagated = false;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
readonly variable: number,
|
|
21
|
+
vars: number[],
|
|
22
|
+
) {
|
|
23
|
+
this.vars = [...vars].sort(compareNumbers);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get size() {
|
|
27
|
+
return this.vars.length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
containsVar(lpVar: number) {
|
|
31
|
+
return this.vars.includes(lpVar);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// A cell of a cyclic use list: the index of a monic, and the next cell.
|
|
36
|
+
class Cell {
|
|
37
|
+
next: Cell = this;
|
|
38
|
+
|
|
39
|
+
constructor(readonly index: number) {}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface HeadTail {
|
|
43
|
+
head: Cell | null;
|
|
44
|
+
tail: Cell | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const getKey = (vars: number[]) => vars.join(",");
|
|
48
|
+
|
|
49
|
+
export class Emonics {
|
|
50
|
+
readonly trail = new TrailStack();
|
|
51
|
+
private unionFind = new UnionFind(this.trail);
|
|
52
|
+
readonly monics: Monic[] = [];
|
|
53
|
+
private var2index: number[] = [];
|
|
54
|
+
private visitedStamp = 0;
|
|
55
|
+
private useLists: HeadTail[] = [];
|
|
56
|
+
// The congruence table: the monics of each rooted variable list, the first its representative.
|
|
57
|
+
private cgTable = new Map<string, number[]>();
|
|
58
|
+
|
|
59
|
+
constructor(private varEqs: VarEqs) {
|
|
60
|
+
varEqs.setMergeHandler(this.varEqsHandler);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The handlers of var_eqs' union-find, over signed variables.
|
|
64
|
+
readonly varEqsHandler = {
|
|
65
|
+
afterMergeEh: (r2: number, r1: number) => {
|
|
66
|
+
if (
|
|
67
|
+
getSignedVarVar(r1) !== getSignedVarVar(r2) &&
|
|
68
|
+
this.varEqs.find(negateSignedVar(r1)) !== this.varEqs.find(negateSignedVar(r2))
|
|
69
|
+
)
|
|
70
|
+
return;
|
|
71
|
+
this.reserveUseLists(Math.max(getSignedVarVar(r2), getSignedVarVar(r1)) + 1);
|
|
72
|
+
this.rehashCg(getSignedVarVar(r1));
|
|
73
|
+
this.mergeCells(this.useLists[getSignedVarVar(r2)], this.useLists[getSignedVarVar(r1)]);
|
|
74
|
+
},
|
|
75
|
+
unmergeEh: (r2: number, r1: number) => {
|
|
76
|
+
if (
|
|
77
|
+
getSignedVarVar(r1) !== getSignedVarVar(r2) &&
|
|
78
|
+
this.varEqs.find(negateSignedVar(r1)) === this.varEqs.find(negateSignedVar(r2))
|
|
79
|
+
)
|
|
80
|
+
return;
|
|
81
|
+
this.unmergeCells(this.useLists[getSignedVarVar(r2)], this.useLists[getSignedVarVar(r1)]);
|
|
82
|
+
this.rehashCg(getSignedVarVar(r1));
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
get numMonics() {
|
|
87
|
+
return this.monics.length;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get(lpVar: number) {
|
|
91
|
+
return this.monics[this.var2index[lpVar]];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
isMonicVar(lpVar: number) {
|
|
95
|
+
const index = this.var2index[lpVar];
|
|
96
|
+
return index !== undefined && index !== -1;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
push() {
|
|
100
|
+
this.trail.pushScope();
|
|
101
|
+
this.varEqs.push();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
pop(count: number) {
|
|
105
|
+
for (let index = 0; index < count; index++) {
|
|
106
|
+
this.varEqs.pop(1);
|
|
107
|
+
this.trail.popScope(1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
add(lpVar: number, vars: number[]) {
|
|
112
|
+
this.varEqs.push();
|
|
113
|
+
const index = this.monics.length;
|
|
114
|
+
const monic = new Monic(lpVar, vars);
|
|
115
|
+
this.monics.push(monic);
|
|
116
|
+
this.doCanonize(monic);
|
|
117
|
+
this.trail.push(() => this.popMonic());
|
|
118
|
+
let lastVar = -1;
|
|
119
|
+
for (const rvar of monic.rvars) {
|
|
120
|
+
if (rvar === lastVar) continue;
|
|
121
|
+
this.reserveUseLists(rvar + 1);
|
|
122
|
+
this.insertCell(this.useLists[rvar], index);
|
|
123
|
+
lastVar = rvar;
|
|
124
|
+
}
|
|
125
|
+
while (this.var2index.length < lpVar) this.var2index.push(-1);
|
|
126
|
+
this.var2index[lpVar] = index;
|
|
127
|
+
this.insertCgMon(monic);
|
|
128
|
+
this.varEqs.push();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private popMonic() {
|
|
132
|
+
this.varEqs.pop(1);
|
|
133
|
+
const monic = this.monics[this.monics.length - 1];
|
|
134
|
+
this.removeCgMon(monic);
|
|
135
|
+
this.var2index[monic.variable] = -1;
|
|
136
|
+
this.doCanonize(monic);
|
|
137
|
+
let lastVar = -1;
|
|
138
|
+
for (const rvar of monic.rvars) {
|
|
139
|
+
if (rvar === lastVar) continue;
|
|
140
|
+
this.removeCell(this.useLists[rvar]);
|
|
141
|
+
lastVar = rvar;
|
|
142
|
+
}
|
|
143
|
+
this.varEqs.pop(1);
|
|
144
|
+
this.monics.pop();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setPropagated(monic: Monic) {
|
|
148
|
+
const target = this.get(monic.variable);
|
|
149
|
+
target.isPropagated = true;
|
|
150
|
+
this.trail.push(() => {
|
|
151
|
+
target.isPropagated = false;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setBoundPropagated(monic: Monic) {
|
|
156
|
+
const target = this.get(monic.variable);
|
|
157
|
+
target.isBoundPropagated = true;
|
|
158
|
+
this.trail.push(() => {
|
|
159
|
+
target.isBoundPropagated = false;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private reserveUseLists(size: number) {
|
|
164
|
+
while (this.useLists.length < size) this.useLists.push({ head: null, tail: null });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private incVisited() {
|
|
168
|
+
this.visitedStamp++;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private setVisited(monic: Monic) {
|
|
172
|
+
this.get(monic.variable).visited = this.visitedStamp;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private isVisited(monic: Monic) {
|
|
176
|
+
return this.get(monic.variable).visited === this.visitedStamp;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private removeCell(list: HeadTail) {
|
|
180
|
+
const head = list.head;
|
|
181
|
+
const tail = list.tail;
|
|
182
|
+
if (head === null || tail === null) return;
|
|
183
|
+
const oldHead = head.next;
|
|
184
|
+
if (oldHead === head) {
|
|
185
|
+
list.head = null;
|
|
186
|
+
list.tail = null;
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
list.head = oldHead;
|
|
190
|
+
tail.next = oldHead;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private insertCell(list: HeadTail, index: number) {
|
|
194
|
+
const cell = new Cell(index);
|
|
195
|
+
if (list.head !== null) cell.next = list.head;
|
|
196
|
+
list.head = cell;
|
|
197
|
+
list.tail ??= cell;
|
|
198
|
+
list.tail.next = cell;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private mergeCells(root: HeadTail, other: HeadTail) {
|
|
202
|
+
if (root === other) return;
|
|
203
|
+
if (root.head === null) {
|
|
204
|
+
root.head = other.head;
|
|
205
|
+
root.tail = other.tail;
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (other.head === null || other.tail === null || root.tail === null) return;
|
|
209
|
+
root.tail.next = other.head;
|
|
210
|
+
other.tail.next = root.head;
|
|
211
|
+
root.head = other.head;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private unmergeCells(root: HeadTail, other: HeadTail) {
|
|
215
|
+
if (root === other || other.head === null || other.tail === null) return;
|
|
216
|
+
if (root.tail === other.tail) {
|
|
217
|
+
root.head = null;
|
|
218
|
+
root.tail = null;
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (root.tail === null) return;
|
|
222
|
+
root.head = other.tail.next;
|
|
223
|
+
root.tail.next = root.head;
|
|
224
|
+
other.tail.next = other.head;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private getHead(lpVar: number) {
|
|
228
|
+
const root = getSignedVarVar(this.varEqs.findVar(lpVar));
|
|
229
|
+
this.reserveUseLists(root + 1);
|
|
230
|
+
return this.useLists[root].head;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// The monics in the use list of a variable's root, around the cycle from its head.
|
|
234
|
+
*getUseList(lpVar: number) {
|
|
235
|
+
const head = this.getHead(lpVar);
|
|
236
|
+
if (head === null) return;
|
|
237
|
+
let cell = head;
|
|
238
|
+
do {
|
|
239
|
+
yield this.monics[cell.index];
|
|
240
|
+
cell = cell.next;
|
|
241
|
+
} while (cell !== head);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
findCanonical(vars: number[]) {
|
|
245
|
+
const table = this.cgTable.get(getKey([...vars].sort(compareNumbers)));
|
|
246
|
+
return table === undefined || table.length === 0 ? undefined : this.get(table[0]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
isCanonicalMonic(lpVar: number) {
|
|
250
|
+
const index = this.var2index[lpVar];
|
|
251
|
+
return index >= this.unionFind.numVars || this.unionFind.find(index) === index;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private removeCg(lpVar: number) {
|
|
255
|
+
const first = this.useLists[lpVar].head;
|
|
256
|
+
if (first === null) return;
|
|
257
|
+
this.incVisited();
|
|
258
|
+
let cell = first;
|
|
259
|
+
do {
|
|
260
|
+
const monic = this.monics[cell.index];
|
|
261
|
+
cell = cell.next;
|
|
262
|
+
if (this.isVisited(monic)) continue;
|
|
263
|
+
this.setVisited(monic);
|
|
264
|
+
this.removeCgMon(monic);
|
|
265
|
+
} while (cell !== first);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private removeCgMon(monic: Monic) {
|
|
269
|
+
const key = getKey(monic.rvars);
|
|
270
|
+
const table = this.cgTable.get(key);
|
|
271
|
+
if (table === undefined) return;
|
|
272
|
+
if (table.length === 1) {
|
|
273
|
+
this.cgTable.delete(key);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
table.splice(table.indexOf(monic.variable), 1);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private insertCg(lpVar: number) {
|
|
280
|
+
const first = this.useLists[lpVar].head;
|
|
281
|
+
if (first === null) return;
|
|
282
|
+
this.incVisited();
|
|
283
|
+
let cell = first;
|
|
284
|
+
do {
|
|
285
|
+
const monic = this.monics[cell.index];
|
|
286
|
+
cell = cell.next;
|
|
287
|
+
if (this.isVisited(monic)) continue;
|
|
288
|
+
this.setVisited(monic);
|
|
289
|
+
this.insertCgMon(monic);
|
|
290
|
+
} while (cell !== first);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private insertCgMon(monic: Monic) {
|
|
294
|
+
this.doCanonize(monic);
|
|
295
|
+
const key = getKey(monic.rvars);
|
|
296
|
+
const table = this.cgTable.get(key);
|
|
297
|
+
if (table === undefined) {
|
|
298
|
+
this.cgTable.set(key, [monic.variable]);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (table.includes(monic.variable)) return;
|
|
302
|
+
const representative = table[0];
|
|
303
|
+
table.push(monic.variable);
|
|
304
|
+
const monicIndex = this.var2index[monic.variable];
|
|
305
|
+
const representativeIndex = this.var2index[representative];
|
|
306
|
+
const maxIndex = Math.max(monicIndex, representativeIndex);
|
|
307
|
+
while (this.unionFind.numVars <= maxIndex) this.unionFind.mkVar();
|
|
308
|
+
this.unionFind.merge(monicIndex, representativeIndex);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private rehashCg(lpVar: number) {
|
|
312
|
+
this.removeCg(lpVar);
|
|
313
|
+
this.insertCg(lpVar);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private doCanonize(monic: Monic) {
|
|
317
|
+
monic.rsign = false;
|
|
318
|
+
monic.rvars = [];
|
|
319
|
+
for (const lpVar of monic.vars) {
|
|
320
|
+
const root = this.varEqs.findVar(lpVar);
|
|
321
|
+
monic.rsign = monic.rsign !== isSignedVarNegated(root);
|
|
322
|
+
monic.rvars.push(getSignedVarVar(root));
|
|
323
|
+
}
|
|
324
|
+
monic.rvars.sort(compareNumbers);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
ensureCanonized() {
|
|
328
|
+
for (const monic of this.monics) this.doCanonize(monic);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// canonize_divides: whether the rooted variables of the first are a sub-multiset of the second's.
|
|
332
|
+
canonizeDivides(divisor: Monic, product: Monic) {
|
|
333
|
+
if (divisor.size > product.size) return false;
|
|
334
|
+
let divisorIndex = 0;
|
|
335
|
+
let productIndex = 0;
|
|
336
|
+
for (;;) {
|
|
337
|
+
if (divisorIndex === divisor.size) return true;
|
|
338
|
+
if (productIndex === product.size) return false;
|
|
339
|
+
const divisorVar = divisor.rvars[divisorIndex];
|
|
340
|
+
const productVar = product.rvars[productIndex];
|
|
341
|
+
if (divisorVar === productVar) {
|
|
342
|
+
divisorIndex++;
|
|
343
|
+
productIndex++;
|
|
344
|
+
} else if (divisorVar < productVar) return false;
|
|
345
|
+
else productIndex++;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// get_products_of: the monics, other than the given one, that it divides, or every monic using
|
|
350
|
+
// the variable, each once.
|
|
351
|
+
*getProductsOf(factor: Monic | number) {
|
|
352
|
+
this.incVisited();
|
|
353
|
+
const isMonic = factor instanceof Monic;
|
|
354
|
+
for (const monic of this.getUseList(isMonic ? factor.vars[0] : factor)) {
|
|
355
|
+
if (isMonic && (factor.variable === monic.variable || !this.canonizeDivides(factor, monic)))
|
|
356
|
+
continue;
|
|
357
|
+
if (this.isVisited(monic)) continue;
|
|
358
|
+
this.setVisited(monic);
|
|
359
|
+
yield monic;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// enum_sign_equiv_monics: the monics equal to this one up to sign, around its class's cycle.
|
|
364
|
+
*getSignEquivMonics(monic: Monic) {
|
|
365
|
+
const start = this.var2index[monic.variable];
|
|
366
|
+
let index = start;
|
|
367
|
+
do {
|
|
368
|
+
yield this.monics[index];
|
|
369
|
+
if (index >= this.unionFind.numVars) return;
|
|
370
|
+
index = this.unionFind.next(index);
|
|
371
|
+
} while (index !== start);
|
|
372
|
+
}
|
|
373
|
+
}
|