tla-ts 0.0.4-dev.e4c2d0e → 0.0.4-dev.e92649a
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 +26 -141
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +35 -1
- package/dist/index.d.ts +35 -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 +515 -0
- package/dist/prove.js +515 -0
- package/dist/run-cli.cjs +30 -47
- package/dist/run-cli.js +30 -47
- 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/check.ts +1 -0
- package/src/codegen.ts +26 -7
- package/src/errors.ts +86 -0
- package/src/evaluate.ts +63 -20
- package/src/index.ts +10 -0
- package/src/{obligation-action.ts → obligations/action.ts} +258 -37
- package/src/{obligation-anon.ts → obligations/anon.ts} +13 -7
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +2 -2
- package/src/{obligation-expression.ts → obligations/expression.ts} +54 -8
- package/src/{obligation-generate.ts → obligations/generate.ts} +134 -61
- package/src/{obligations.ts → obligations/index.ts} +26 -12
- package/src/{obligation-levels.ts → obligations/levels.ts} +12 -2
- package/src/{obligation-module.ts → obligations/module.ts} +46 -20
- package/src/obligations/pickle.ts +100 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +5 -59
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -1
- package/src/{obligation-source.ts → obligations/source.ts} +91 -35
- package/src/obligations/temporal.ts +319 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +1 -1
- package/src/parallel-worker.ts +2 -18
- package/src/parallel.ts +10 -43
- package/src/prove/decide-pool.ts +100 -0
- package/src/prove/deciders.ts +27 -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 +477 -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 +547 -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 +441 -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 +363 -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 +402 -0
- package/src/prove/isabelle/pure/name.ts +110 -0
- package/src/prove/isabelle/pure/net.ts +262 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +650 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +353 -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 +156 -0
- package/src/prove/isabelle/pure/simplifier.ts +112 -0
- package/src/prove/isabelle/pure/table.ts +208 -0
- package/src/prove/isabelle/pure/tactic.ts +276 -0
- package/src/prove/isabelle/pure/tactical.ts +347 -0
- package/src/prove/isabelle/pure/term-ord.ts +159 -0
- package/src/prove/isabelle/pure/term.ts +808 -0
- package/src/prove/isabelle/pure/thm.ts +806 -0
- package/src/prove/isabelle/pure/type.ts +133 -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 +520 -0
- package/src/prove/prep/defs.ts +27 -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 +552 -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 +29 -0
- package/src/prove/prove.ts +417 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +256 -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 +210 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +665 -0
- package/src/prove/smt/encode.ts +169 -0
- package/src/prove/smt/extensionality.ts +63 -0
- package/src/prove/smt/flatten.ts +127 -0
- package/src/prove/smt/format.ts +143 -0
- package/src/prove/smt/instances.ts +182 -0
- package/src/prove/smt/rewrite.ts +784 -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 +147 -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 +656 -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 +740 -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 +13 -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 +444 -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 +181 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/run-cli.ts +73 -20
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +24 -5
- package/src/tlc-random.ts +55 -32
- package/src/tlc-version.ts +8 -7
- package/src/values.ts +85 -41
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- /package/src/{obligation-format.ts → obligations/format.ts} +0 -0
|
@@ -0,0 +1,1381 @@
|
|
|
1
|
+
// Ported from Z3 (src/smt/smt_context.h, src/smt/smt_internalizer.cpp), MIT license, see
|
|
2
|
+
// z3-license.txt. The state of the SMT kernel and the translation of formulas into it: Boolean
|
|
3
|
+
// variables, enodes, clauses and relevancy watches. The search lives in context.ts.
|
|
4
|
+
import { SmtInvariantError } from "../../../errors";
|
|
5
|
+
import {
|
|
6
|
+
AstManager,
|
|
7
|
+
FAMILY_ARITH,
|
|
8
|
+
FAMILY_BASIC,
|
|
9
|
+
FAMILY_USER,
|
|
10
|
+
KIND_APP,
|
|
11
|
+
KIND_QUANTIFIER,
|
|
12
|
+
OP_AND,
|
|
13
|
+
OP_EQ,
|
|
14
|
+
OP_IMPLIES,
|
|
15
|
+
OP_ITE,
|
|
16
|
+
OP_NOT,
|
|
17
|
+
OP_OR,
|
|
18
|
+
OP_XOR,
|
|
19
|
+
SORT_BOOL,
|
|
20
|
+
isArithSort,
|
|
21
|
+
} from "./ast";
|
|
22
|
+
import { CaseSplitQueue } from "./case-split-queue";
|
|
23
|
+
import { CgTable, getCongruenceJustification, isCongruent } from "./cg-table";
|
|
24
|
+
import {
|
|
25
|
+
BinJustification,
|
|
26
|
+
BJustification,
|
|
27
|
+
CLS_AUX,
|
|
28
|
+
CLS_LEARNED,
|
|
29
|
+
CLS_TH_AXIOM,
|
|
30
|
+
CLS_TH_LEMMA,
|
|
31
|
+
type Clause,
|
|
32
|
+
ClauseStore,
|
|
33
|
+
} from "./clause";
|
|
34
|
+
import {
|
|
35
|
+
AXIOM_EQ_JUSTIFICATION,
|
|
36
|
+
Enode,
|
|
37
|
+
EqJustification,
|
|
38
|
+
EqPropagationJustification,
|
|
39
|
+
Justification,
|
|
40
|
+
NO_ENODES,
|
|
41
|
+
UnitResolutionJustification,
|
|
42
|
+
} from "./enode";
|
|
43
|
+
import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
|
|
44
|
+
import {
|
|
45
|
+
BoolVar,
|
|
46
|
+
FALSE_LITERAL,
|
|
47
|
+
getVar,
|
|
48
|
+
isNegated,
|
|
49
|
+
Literal,
|
|
50
|
+
mkLiteral,
|
|
51
|
+
negate,
|
|
52
|
+
TRUE_BOOL_VAR,
|
|
53
|
+
TRUE_LITERAL,
|
|
54
|
+
toBoolVar,
|
|
55
|
+
} from "./literal";
|
|
56
|
+
import type { DefaultQuantifierManager } from "./quantifier-manager";
|
|
57
|
+
import { RelevancyPropagator } from "./relevancy";
|
|
58
|
+
import { KERNEL_REWRITER_OPTIONS, Rewriter } from "./rewriter";
|
|
59
|
+
import { sortNumbers } from "./sort";
|
|
60
|
+
import type { TheoryLra } from "./theory-lra";
|
|
61
|
+
import { setAt, shrink } from "./array";
|
|
62
|
+
import { StickyGenerations } from "./sticky-generations";
|
|
63
|
+
|
|
64
|
+
// DEEP_EXPR_THRESHOLD: the depth above which internalize_deep sorts an expression's subterms.
|
|
65
|
+
const DEEP_EXPR_THRESHOLD = 1024;
|
|
66
|
+
const WHITE = 0;
|
|
67
|
+
const GREY = 1;
|
|
68
|
+
const BLACK = 2;
|
|
69
|
+
|
|
70
|
+
// The kinds of undo trail entries. Z3 allocates its trail objects in a region. Here an entry is its
|
|
71
|
+
// kind on trailKinds, with its operands on trailNodes and trailInts, so recording one allocates
|
|
72
|
+
// nothing. pushTrail's closures are one more kind.
|
|
73
|
+
const UNDO_CLOSURE = 0;
|
|
74
|
+
const UNDO_MK_BOOL_VAR = 1;
|
|
75
|
+
const UNDO_MK_ENODE = 2;
|
|
76
|
+
const UNDO_VAR_THEORY = 3;
|
|
77
|
+
const UNDO_TRUE_FIRST = 4;
|
|
78
|
+
const UNDO_CGC_GENERATIONS = 5;
|
|
79
|
+
const UNDO_MERGE_TF = 6;
|
|
80
|
+
const UNDO_ENODE_FLAG = 7;
|
|
81
|
+
|
|
82
|
+
// The first word of bool_var_data: the level of the assignment in the low 24 bits, then m_mark,
|
|
83
|
+
// m_phase_available, m_phase, m_eq, m_true_first, m_enode, m_quantifier and whether a theory is
|
|
84
|
+
// notified.
|
|
85
|
+
const LEVEL_MASK = 0xffffff;
|
|
86
|
+
const BV_MARK = 1 << 24;
|
|
87
|
+
export const BV_PHASE_AVAILABLE = 1 << 25;
|
|
88
|
+
export const BV_PHASE = 1 << 26;
|
|
89
|
+
export const BV_EQ = 1 << 27;
|
|
90
|
+
export const BV_TRUE_FIRST = 1 << 28;
|
|
91
|
+
export const BV_ENODE = 1 << 29;
|
|
92
|
+
export const BV_QUANTIFIER = 1 << 30;
|
|
93
|
+
export const BV_THEORY = 1 << 31;
|
|
94
|
+
export const UNDO_ADD_EQ = 8;
|
|
95
|
+
export const UNDO_TH_VAR = 9;
|
|
96
|
+
|
|
97
|
+
interface NewEq {
|
|
98
|
+
lhs: Enode;
|
|
99
|
+
rhs: Enode;
|
|
100
|
+
justification: EqJustification;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// m_conflict and m_not_l: the justification of the conflict, and the literal it falsifies, if any.
|
|
104
|
+
interface Conflict {
|
|
105
|
+
justification: BJustification;
|
|
106
|
+
notLiteral: Literal | null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export abstract class Internalizer {
|
|
110
|
+
readonly trueLiteral = TRUE_LITERAL;
|
|
111
|
+
readonly falseLiteral = FALSE_LITERAL;
|
|
112
|
+
readonly rewriter: Rewriter;
|
|
113
|
+
readonly relevancy: RelevancyPropagator;
|
|
114
|
+
readonly caseSplitQueue: CaseSplitQueue;
|
|
115
|
+
readonly cgTable = new CgTable();
|
|
116
|
+
private stickyGenerations = new StickyGenerations();
|
|
117
|
+
abstract readonly theory: TheoryLra;
|
|
118
|
+
abstract readonly qmanager: DefaultQuantifierManager;
|
|
119
|
+
|
|
120
|
+
// Per Boolean variable. Like Z3's vectors, the arrays keep their size when a variable is deleted,
|
|
121
|
+
// since V8 shrinks an array's storage when it pops and copies it again when it grows. Only the
|
|
122
|
+
// first numBoolVars entries are live.
|
|
123
|
+
numBoolVars = 0;
|
|
124
|
+
boolVar2Expr: number[] = [];
|
|
125
|
+
// bool_var_data: two words per variable. The first holds the level of its assignment and the BV_
|
|
126
|
+
// flags, the second the level of its internalization.
|
|
127
|
+
private boolVarData = new Int32Array(2048);
|
|
128
|
+
justifications: BJustification[] = [];
|
|
129
|
+
activity: number[] = [];
|
|
130
|
+
// Per literal.
|
|
131
|
+
protected assignment: number[] = [];
|
|
132
|
+
readonly clauses = new ClauseStore();
|
|
133
|
+
// Like Z3's watch lists, a literal's lists are created when the first clause is added.
|
|
134
|
+
protected watches: (Clause[] | undefined)[] = [];
|
|
135
|
+
// del_clauses' uint_set of the watch lists to filter, by literal, all 0 between calls.
|
|
136
|
+
private watchMarks = new Uint8Array(1024);
|
|
137
|
+
// The false literals that simplify_aux_clause_literals removes from the clause mkClause makes.
|
|
138
|
+
private removedLiterals: Literal[] = [];
|
|
139
|
+
// The literal_buffer of mk_and_cnstr and mk_or_cnstr, and the array that mk_gate_clause keeps on
|
|
140
|
+
// the stack. mkClause neither keeps them nor makes a gate, so the gates reuse them.
|
|
141
|
+
private gateBuffer: Literal[] = [];
|
|
142
|
+
private gateLiterals: Literal[] = [];
|
|
143
|
+
// The binary clauses of the binary clause optimization: the literals each literal propagates when it
|
|
144
|
+
// becomes true, which Z3 visits newest first and before the clauses.
|
|
145
|
+
protected binaryWatches: (Literal[] | undefined)[] = [];
|
|
146
|
+
// A binary justification holds only its literal, so undoMkBoolVar keeps it for the next variable
|
|
147
|
+
// with that literal. Clearing it would write past the end of this sparse array, which V8 then
|
|
148
|
+
// turns into a dictionary.
|
|
149
|
+
private binJustifications: (BinJustification | undefined)[] = [];
|
|
150
|
+
// Per expression.
|
|
151
|
+
// The Boolean variable of each expression, or -1. A typed array keeps the element loads of every
|
|
152
|
+
// context monomorphic, where the holes of a growing array give each its own elements kind.
|
|
153
|
+
private exprToBoolVar = new Int32Array(1024).fill(-1);
|
|
154
|
+
protected exprToEnode: (Enode | undefined)[] = [];
|
|
155
|
+
|
|
156
|
+
assignedLiterals: Literal[] = [];
|
|
157
|
+
scopeLevel = 0;
|
|
158
|
+
baseLevel = 0;
|
|
159
|
+
searchLevel = 0;
|
|
160
|
+
protected trailKinds: number[] = [];
|
|
161
|
+
protected trailNodes: Enode[] = [];
|
|
162
|
+
protected trailInts: number[] = [];
|
|
163
|
+
private trailClosures: (() => void)[] = [];
|
|
164
|
+
protected auxClauses: Clause[] = [];
|
|
165
|
+
protected lemmas: Clause[] = [];
|
|
166
|
+
protected clausesToReinit: Clause[][] = [];
|
|
167
|
+
protected conflict: Conflict | null = null;
|
|
168
|
+
protected eqPropagationQueue: NewEq[] = [];
|
|
169
|
+
enodes: Enode[] = [];
|
|
170
|
+
protected decl2enodes = new Map<number, Enode[]>();
|
|
171
|
+
trueEnode: Enode;
|
|
172
|
+
private isDiseqTmp = -1;
|
|
173
|
+
falseEnode: Enode;
|
|
174
|
+
protected generation = 0;
|
|
175
|
+
protected cachedGeneration = new Map<number, number>();
|
|
176
|
+
private randomSeed = 0;
|
|
177
|
+
isSearching = false;
|
|
178
|
+
|
|
179
|
+
constructor(
|
|
180
|
+
readonly manager: AstManager,
|
|
181
|
+
isRelevancyEnabled: boolean,
|
|
182
|
+
isDelayingNewCaseSplits: boolean,
|
|
183
|
+
) {
|
|
184
|
+
this.rewriter = new Rewriter(manager, KERNEL_REWRITER_OPTIONS);
|
|
185
|
+
this.relevancy = new RelevancyPropagator(
|
|
186
|
+
{
|
|
187
|
+
manager,
|
|
188
|
+
findAssignment: (expr) => this.findAssignment(expr),
|
|
189
|
+
findEnode: (expr) => this.exprToEnode[expr],
|
|
190
|
+
relevantEh: (expr) => this.relevantEh(expr),
|
|
191
|
+
},
|
|
192
|
+
isRelevancyEnabled,
|
|
193
|
+
);
|
|
194
|
+
this.caseSplitQueue = new CaseSplitQueue(
|
|
195
|
+
{
|
|
196
|
+
activity: this.activity,
|
|
197
|
+
getIsSearching: () => this.isSearching,
|
|
198
|
+
getRandomValue: () => this.getRandomValue(),
|
|
199
|
+
getNumBoolVars: () => this.numBoolVars,
|
|
200
|
+
isUnassigned: (boolVar) => this.assignment[mkLiteral(boolVar)] === L_UNDEF,
|
|
201
|
+
},
|
|
202
|
+
isDelayingNewCaseSplits,
|
|
203
|
+
);
|
|
204
|
+
this.mkBoolVar(manager.trueExpr);
|
|
205
|
+
this.assignment[0] = L_TRUE;
|
|
206
|
+
this.assignment[1] = L_FALSE;
|
|
207
|
+
this.justifications[0] = "axiom";
|
|
208
|
+
this.trueEnode = this.mkEnode(manager.trueExpr, true, true, false);
|
|
209
|
+
this.setExprBoolVar(manager.falseExpr, TRUE_BOOL_VAR);
|
|
210
|
+
this.falseEnode = this.mkEnode(manager.falseExpr, true, true, false);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
abstract assign(literal: Literal, justification: BJustification, isDecision?: boolean): void;
|
|
214
|
+
|
|
215
|
+
abstract relevantEh(expr: number): void;
|
|
216
|
+
|
|
217
|
+
protected abstract propagateBoolVarEnode(boolVar: BoolVar): void;
|
|
218
|
+
|
|
219
|
+
setConflict(justification: BJustification, notLiteral: Literal | null) {
|
|
220
|
+
if (this.conflict !== null) return;
|
|
221
|
+
this.conflict = { justification, notLiteral };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
pushEq(lhs: Enode, rhs: Enode, justification: EqJustification) {
|
|
225
|
+
this.eqPropagationQueue.push({ lhs, rhs, justification });
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// random_gen from Z3's util/util.h with the default seed 0.
|
|
229
|
+
getRandomValue() {
|
|
230
|
+
this.randomSeed = (Math.imul(this.randomSeed, 214013) + 2531011) >>> 0;
|
|
231
|
+
return (this.randomSeed >>> 16) & 0x7fff;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
pushTrail(undo: () => void) {
|
|
235
|
+
this.trailClosures.push(undo);
|
|
236
|
+
this.trailKinds.push(UNDO_CLOSURE);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
protected popTrail(limit: number) {
|
|
240
|
+
const kinds = this.trailKinds;
|
|
241
|
+
for (let index = kinds.length - 1; index >= limit; index--) this.undoTrailEntry(kinds[index]);
|
|
242
|
+
shrink(kinds, limit);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
protected popTrailNode() {
|
|
246
|
+
const nodes = this.trailNodes;
|
|
247
|
+
const node = nodes[nodes.length - 1];
|
|
248
|
+
nodes.pop();
|
|
249
|
+
return node;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
protected popTrailInt() {
|
|
253
|
+
const ints = this.trailInts;
|
|
254
|
+
const value = ints[ints.length - 1];
|
|
255
|
+
ints.pop();
|
|
256
|
+
return value;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
protected undoTrailEntry(kind: number) {
|
|
260
|
+
switch (kind) {
|
|
261
|
+
case UNDO_CLOSURE:
|
|
262
|
+
this.trailClosures.pop()?.();
|
|
263
|
+
return;
|
|
264
|
+
case UNDO_MK_BOOL_VAR:
|
|
265
|
+
this.undoMkBoolVar();
|
|
266
|
+
return;
|
|
267
|
+
case UNDO_MK_ENODE:
|
|
268
|
+
this.undoMkEnode();
|
|
269
|
+
return;
|
|
270
|
+
case UNDO_VAR_THEORY:
|
|
271
|
+
this.setBoolVarFlag(this.popTrailInt(), BV_THEORY, false);
|
|
272
|
+
return;
|
|
273
|
+
case UNDO_TRUE_FIRST:
|
|
274
|
+
this.setBoolVarFlag(this.popTrailInt(), BV_TRUE_FIRST, false);
|
|
275
|
+
return;
|
|
276
|
+
case UNDO_CGC_GENERATIONS: {
|
|
277
|
+
const second = this.popTrailNode();
|
|
278
|
+
const first = this.popTrailNode();
|
|
279
|
+
const secondGeneration = this.popTrailInt();
|
|
280
|
+
this.undoMergeCgcGenerations(first, this.popTrailInt(), second, secondGeneration);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
case UNDO_MERGE_TF:
|
|
284
|
+
this.popTrailNode().isMergeTf = false;
|
|
285
|
+
return;
|
|
286
|
+
case UNDO_ENODE_FLAG:
|
|
287
|
+
this.setBoolVarFlag(this.popTrailInt(), BV_ENODE, false);
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Assignment queries.
|
|
293
|
+
|
|
294
|
+
getAssignment(literal: Literal) {
|
|
295
|
+
return this.assignment[literal];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
getBoolVarAssignment(boolVar: BoolVar) {
|
|
299
|
+
return this.assignment[mkLiteral(boolVar)];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
getAssignLevel(literal: Literal) {
|
|
303
|
+
return this.getLevel(getVar(literal));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
isBInternalized(expr: number) {
|
|
307
|
+
return this.findBoolVar(expr) !== undefined;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
findBoolVar(expr: number) {
|
|
311
|
+
const boolVar = expr < this.exprToBoolVar.length ? this.exprToBoolVar[expr] : -1;
|
|
312
|
+
return boolVar === -1 ? undefined : toBoolVar(boolVar);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private setExprBoolVar(expr: number, boolVar: number) {
|
|
316
|
+
if (expr >= this.exprToBoolVar.length) {
|
|
317
|
+
const exprToBoolVar = new Int32Array(Math.max(2 * this.exprToBoolVar.length, expr + 1)).fill(
|
|
318
|
+
-1,
|
|
319
|
+
);
|
|
320
|
+
exprToBoolVar.set(this.exprToBoolVar);
|
|
321
|
+
this.exprToBoolVar = exprToBoolVar;
|
|
322
|
+
}
|
|
323
|
+
this.exprToBoolVar[expr] = boolVar;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
getBoolVar(expr: number) {
|
|
327
|
+
const boolVar = this.findBoolVar(expr);
|
|
328
|
+
if (boolVar === undefined) throw new SmtInvariantError("the formula has no Boolean variable");
|
|
329
|
+
return boolVar;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
getEnode(expr: number) {
|
|
333
|
+
return this.exprToEnode[expr];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
isEInternalized(expr: number) {
|
|
337
|
+
return this.exprToEnode[expr] !== undefined;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
findCongruent(decl: number, args: Enode[]) {
|
|
341
|
+
return this.cgTable.findFor(decl, args, decl === OP_EQ);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// is_diseq_core: the roots are distinct values, or an equality between the two classes is false.
|
|
345
|
+
// The congruence table is searched with m_is_diseq_tmp, an equality of the sort, which holds a
|
|
346
|
+
// reference until an equality of another sort replaces it.
|
|
347
|
+
isDiseq(first: Enode, second: Enode) {
|
|
348
|
+
const manager = this.manager;
|
|
349
|
+
if (manager.areDistinct(first.root.owner, second.root.owner)) return true;
|
|
350
|
+
const tmp = this.isDiseqTmp;
|
|
351
|
+
if (tmp === -1 || manager.sorts[manager.getArg(tmp, 0)] !== manager.sorts[first.owner]) {
|
|
352
|
+
if (tmp !== -1) manager.decRef(tmp);
|
|
353
|
+
this.isDiseqTmp = manager.mkEq(first.owner, second.owner);
|
|
354
|
+
manager.incRef(this.isDiseqTmp);
|
|
355
|
+
}
|
|
356
|
+
const root = this.cgTable.findFor(OP_EQ, [first, second], true)?.root;
|
|
357
|
+
if (root === undefined || root === this.trueEnode) return false;
|
|
358
|
+
return (
|
|
359
|
+
root === this.falseEnode ||
|
|
360
|
+
(this.isRelevant(root.owner) && this.getAssignmentOfExpr(root.owner) === L_FALSE)
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
enodesOf(decl: number) {
|
|
365
|
+
return this.decl2enodes.get(decl) ?? [];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
getLiteral(expr: number): Literal {
|
|
369
|
+
const manager = this.manager;
|
|
370
|
+
if (manager.isNot(expr)) return negate(this.getLiteral(manager.getArg(expr, 0)));
|
|
371
|
+
if (expr === manager.trueExpr) return this.trueLiteral;
|
|
372
|
+
if (expr === manager.falseExpr) return this.falseLiteral;
|
|
373
|
+
return mkLiteral(this.getBoolVar(expr));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private getAssignmentOfExpr(expr: number) {
|
|
377
|
+
return this.assignment[mkLiteral(this.getBoolVar(expr))];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
findAssignment(expr: number) {
|
|
381
|
+
const manager = this.manager;
|
|
382
|
+
if (manager.isNot(expr)) {
|
|
383
|
+
const arg = manager.getArg(expr, 0);
|
|
384
|
+
if (this.isBInternalized(arg)) return -this.getAssignmentOfExpr(arg);
|
|
385
|
+
if (arg === manager.falseExpr) return L_TRUE;
|
|
386
|
+
if (arg === manager.trueExpr) return L_FALSE;
|
|
387
|
+
return L_UNDEF;
|
|
388
|
+
}
|
|
389
|
+
if (this.isBInternalized(expr)) return this.getAssignmentOfExpr(expr);
|
|
390
|
+
if (expr === manager.falseExpr) return L_FALSE;
|
|
391
|
+
if (expr === manager.trueExpr) return L_TRUE;
|
|
392
|
+
return L_UNDEF;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
enodeToLiteral(node: Enode) {
|
|
396
|
+
const boolVar = this.getBoolVar(node.owner);
|
|
397
|
+
return mkLiteral(boolVar, this.assignment[mkLiteral(boolVar)] === L_FALSE);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
isRelevant(expr: number) {
|
|
401
|
+
return this.relevancy.isRelevant(expr);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// context::mark_as_relevant, which propagates the relevancy at once.
|
|
405
|
+
markAsRelevant(expr: number) {
|
|
406
|
+
this.relevancy.markAsRelevant(expr);
|
|
407
|
+
this.relevancy.propagate();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
markLiteralAsRelevant(literal: Literal) {
|
|
411
|
+
this.markAsRelevant(this.boolVar2Expr[getVar(literal)]);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
isInconsistent() {
|
|
415
|
+
return this.conflict !== null;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// get_generation: the generation of the congruence root. Equalities have generation 0, since
|
|
419
|
+
// patterns have no equalities.
|
|
420
|
+
getGeneration(node: Enode) {
|
|
421
|
+
return node.isEq ? 0 : this.getCgRoot(node).generation;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// get_cg_root: a node's cg can be stale, so a node that is not a congruence root looks the root up.
|
|
425
|
+
private getCgRoot(node: Enode) {
|
|
426
|
+
if (!node.usesCgTable() || node.isCgr()) return node;
|
|
427
|
+
return this.cgTable.findCongruent(node) ?? node;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private setGeneration(node: Enode, generation: number) {
|
|
431
|
+
if (!node.isEq) this.getCgRoot(node).generation = generation;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
private undoMergeCgcGenerations(
|
|
435
|
+
first: Enode,
|
|
436
|
+
firstGeneration: number,
|
|
437
|
+
second: Enode,
|
|
438
|
+
secondGeneration: number,
|
|
439
|
+
) {
|
|
440
|
+
this.setGeneration(first, firstGeneration);
|
|
441
|
+
this.setGeneration(second, secondGeneration);
|
|
442
|
+
// apply_sticky_updates: a lowered generation outlives the merge that it was made under. Only the
|
|
443
|
+
// updates of the two nodes' declarations can be congruent to them, and a node with an update
|
|
444
|
+
// uses the congruence table, so it has arguments and is no equality.
|
|
445
|
+
this.applyStickyUpdates(first, firstGeneration, second, secondGeneration);
|
|
446
|
+
if (second.decl !== first.decl) this.applyStickyUpdates(second, secondGeneration, null, 0);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
private applyStickyUpdates(
|
|
450
|
+
first: Enode,
|
|
451
|
+
firstGeneration: number,
|
|
452
|
+
second: Enode | null,
|
|
453
|
+
secondGeneration: number,
|
|
454
|
+
) {
|
|
455
|
+
const group = this.stickyGenerations.getGroup(first.decl);
|
|
456
|
+
if (group === undefined) return;
|
|
457
|
+
const firstRoot = first.getArg(0).root;
|
|
458
|
+
const secondRoot = second !== null && second.decl === first.decl ? second.getArg(0).root : null;
|
|
459
|
+
for (let index = 0; index < group.nodes.length; index++) {
|
|
460
|
+
const node = group.nodes[index];
|
|
461
|
+
if (node === null) continue;
|
|
462
|
+
const generation = group.generations[index];
|
|
463
|
+
const root = node.getArg(0).root;
|
|
464
|
+
if (generation < firstGeneration && root === firstRoot && isCongruent(node, first))
|
|
465
|
+
this.setGeneration(first, generation);
|
|
466
|
+
else if (
|
|
467
|
+
second !== null &&
|
|
468
|
+
generation < secondGeneration &&
|
|
469
|
+
root === secondRoot &&
|
|
470
|
+
isCongruent(node, second)
|
|
471
|
+
)
|
|
472
|
+
this.setGeneration(second, generation);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Boolean variables and enodes.
|
|
477
|
+
|
|
478
|
+
// mk_bool_var: m_b_internalized_stack holds a reference to the expression until the variable is
|
|
479
|
+
// deleted.
|
|
480
|
+
mkBoolVar(expr: number) {
|
|
481
|
+
const boolVar = toBoolVar(this.numBoolVars++);
|
|
482
|
+
const literal = mkLiteral(boolVar);
|
|
483
|
+
this.manager.incRef(expr);
|
|
484
|
+
this.setExprBoolVar(expr, boolVar);
|
|
485
|
+
this.boolVar2Expr[boolVar] = expr;
|
|
486
|
+
if (2 * boolVar + 1 >= this.boolVarData.length) {
|
|
487
|
+
const grown = new Int32Array(2 * this.boolVarData.length);
|
|
488
|
+
grown.set(this.boolVarData);
|
|
489
|
+
this.boolVarData = grown;
|
|
490
|
+
}
|
|
491
|
+
this.boolVarData[2 * boolVar] = 0;
|
|
492
|
+
this.boolVarData[2 * boolVar + 1] = this.scopeLevel;
|
|
493
|
+
this.justifications[boolVar] = null;
|
|
494
|
+
this.activity[boolVar] = this.isSearching ? -((this.getRandomValue() % 1000) / 1000) : 0;
|
|
495
|
+
this.assignment[literal] = L_UNDEF;
|
|
496
|
+
this.assignment[literal + 1] = L_UNDEF;
|
|
497
|
+
this.resetWatches(literal);
|
|
498
|
+
this.resetWatches(literal + 1);
|
|
499
|
+
this.caseSplitQueue.mkVar(boolVar);
|
|
500
|
+
this.trailKinds.push(UNDO_MK_BOOL_VAR);
|
|
501
|
+
return boolVar;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// The arrays keep the deleted variable's entries, which mkBoolVar overwrites. Those that hold
|
|
505
|
+
// objects are cleared, so the objects can be collected.
|
|
506
|
+
private undoMkBoolVar() {
|
|
507
|
+
const boolVar = toBoolVar(--this.numBoolVars);
|
|
508
|
+
const expr = this.boolVar2Expr[boolVar];
|
|
509
|
+
this.caseSplitQueue.delVar(boolVar);
|
|
510
|
+
if (this.hasBoolVarFlag(boolVar, BV_QUANTIFIER)) this.qmanager.del(expr);
|
|
511
|
+
this.exprToBoolVar[expr] = -1;
|
|
512
|
+
this.justifications[boolVar] = null;
|
|
513
|
+
this.manager.decRef(expr);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// The level of the variable's assignment.
|
|
517
|
+
getLevel(boolVar: number) {
|
|
518
|
+
return this.boolVarData[2 * boolVar] & LEVEL_MASK;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
protected setLevel(boolVar: number, level: number) {
|
|
522
|
+
const index = 2 * boolVar;
|
|
523
|
+
this.boolVarData[index] = (this.boolVarData[index] & ~LEVEL_MASK) | level;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// The scope level at which the variable was internalized.
|
|
527
|
+
getInternLevel(boolVar: number) {
|
|
528
|
+
return this.boolVarData[2 * boolVar + 1];
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
protected hasBoolVarFlag(boolVar: number, flag: number) {
|
|
532
|
+
return (this.boolVarData[2 * boolVar] & flag) !== 0;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
protected setBoolVarFlag(boolVar: number, flag: number, value: boolean) {
|
|
536
|
+
const index = 2 * boolVar;
|
|
537
|
+
this.boolVarData[index] = value
|
|
538
|
+
? this.boolVarData[index] | flag
|
|
539
|
+
: this.boolVarData[index] & ~flag;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
isMarked(boolVar: number) {
|
|
543
|
+
return this.hasBoolVarFlag(boolVar, BV_MARK);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
setMark(boolVar: number, value: boolean) {
|
|
547
|
+
this.setBoolVarFlag(boolVar, BV_MARK, value);
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// watch_list::reset: a new variable's lists keep the room of a deleted one's. Assigning undefined to
|
|
551
|
+
// a literal past the end keeps the arrays without holes.
|
|
552
|
+
private resetWatches(literal: number) {
|
|
553
|
+
const list = this.watches[literal];
|
|
554
|
+
if (list === undefined) this.watches[literal] = undefined;
|
|
555
|
+
else shrink(list, 0);
|
|
556
|
+
const binary = this.binaryWatches[literal];
|
|
557
|
+
if (binary === undefined) this.binaryWatches[literal] = undefined;
|
|
558
|
+
else shrink(binary, 0);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
protected isAtom(boolVar: BoolVar) {
|
|
562
|
+
return this.hasBoolVarFlag(boolVar, BV_EQ | BV_ENODE | BV_THEORY | BV_QUANTIFIER);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
setVarTheory(boolVar: BoolVar) {
|
|
566
|
+
if (this.scopeLevel > this.getInternLevel(boolVar)) {
|
|
567
|
+
this.trailInts.push(boolVar);
|
|
568
|
+
this.trailKinds.push(UNDO_VAR_THEORY);
|
|
569
|
+
}
|
|
570
|
+
this.setBoolVarFlag(boolVar, BV_THEORY, true);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
setTrueFirstFlag(boolVar: BoolVar) {
|
|
574
|
+
this.trailInts.push(boolVar);
|
|
575
|
+
this.trailKinds.push(UNDO_TRUE_FIRST);
|
|
576
|
+
this.setBoolVarFlag(boolVar, BV_TRUE_FIRST, true);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
isTrueFirst(boolVar: BoolVar) {
|
|
580
|
+
return this.hasBoolVarFlag(boolVar, BV_TRUE_FIRST);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
mkEnode(expr: number, isSuppressingArgs: boolean, isMergeTf: boolean, isCgcEnabled: boolean) {
|
|
584
|
+
const manager = this.manager;
|
|
585
|
+
const generation = this.cachedGeneration.get(expr) ?? this.generation;
|
|
586
|
+
const numArgs = isSuppressingArgs ? 0 : manager.getNumArgs(expr);
|
|
587
|
+
const moreArgs: Enode[] = numArgs > 2 ? [] : NO_ENODES;
|
|
588
|
+
for (let index = 2; index < numArgs; index++) moreArgs.push(this.getArgEnode(expr, index));
|
|
589
|
+
const isEq = !isSuppressingArgs && manager.getOp(expr) === OP_EQ;
|
|
590
|
+
const node = new Enode(
|
|
591
|
+
expr,
|
|
592
|
+
manager.decls[expr],
|
|
593
|
+
numArgs,
|
|
594
|
+
numArgs > 0 ? this.getArgEnode(expr, 0) : null,
|
|
595
|
+
numArgs > 1 ? this.getArgEnode(expr, 1) : null,
|
|
596
|
+
moreArgs,
|
|
597
|
+
generation,
|
|
598
|
+
isEq,
|
|
599
|
+
manager.sorts[expr] === SORT_BOOL,
|
|
600
|
+
isMergeTf,
|
|
601
|
+
isCgcEnabled,
|
|
602
|
+
this.scopeLevel,
|
|
603
|
+
);
|
|
604
|
+
for (let index = 0; index < numArgs; index++) node.getArg(index).root.addParent(node);
|
|
605
|
+
if (manager.isUniqueValue(expr)) node.isInterpreted = true;
|
|
606
|
+
setAt(this.exprToEnode, expr, node);
|
|
607
|
+
// m_e_internalized_stack holds a reference to the expression until the enode is deleted.
|
|
608
|
+
manager.incRef(expr);
|
|
609
|
+
this.trailKinds.push(UNDO_MK_ENODE);
|
|
610
|
+
this.enodes.push(node);
|
|
611
|
+
if (numArgs > 0) {
|
|
612
|
+
if (node.isTrueEq()) {
|
|
613
|
+
this.assign(
|
|
614
|
+
mkLiteral(this.getBoolVar(expr)),
|
|
615
|
+
new EqPropagationJustification(node.getArg(0), node.getArg(1)),
|
|
616
|
+
);
|
|
617
|
+
this.pushEq(node, this.trueEnode, AXIOM_EQ_JUSTIFICATION);
|
|
618
|
+
} else if (isCgcEnabled) {
|
|
619
|
+
const congruent = this.cgTable.insert(node);
|
|
620
|
+
if (congruent !== node) {
|
|
621
|
+
node.cg = congruent;
|
|
622
|
+
if (!isEq) this.mergeCgcGenerations(node, generation, congruent);
|
|
623
|
+
this.pushEq(node, congruent, getCongruenceJustification(node, congruent));
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (!isEq) {
|
|
627
|
+
const list = this.decl2enodes.get(node.decl);
|
|
628
|
+
if (list === undefined) this.decl2enodes.set(node.decl, [node]);
|
|
629
|
+
else list.push(node);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return node;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// The enode of an argument of expr, which is internalized before expr.
|
|
636
|
+
private getArgEnode(expr: number, index: number) {
|
|
637
|
+
const node = this.exprToEnode[this.manager.getArg(expr, index)];
|
|
638
|
+
if (node === undefined) throw new SmtInvariantError("an argument was not internalized");
|
|
639
|
+
return node;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
private undoMkEnode() {
|
|
643
|
+
const node = this.enodes.pop();
|
|
644
|
+
if (node === undefined) return;
|
|
645
|
+
this.exprToEnode[node.owner] = undefined;
|
|
646
|
+
if (node.isCgr() && node.usesCgTable()) this.cgTable.erase(node);
|
|
647
|
+
if (node.numArgs > 0 && !node.isEq) this.decl2enodes.get(node.decl)?.pop();
|
|
648
|
+
for (let index = 0; index < node.numArgs; index++) node.getArg(index).root.parents.pop();
|
|
649
|
+
this.manager.decRef(node.owner);
|
|
650
|
+
this.stickyGenerations.delete(node);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
protected mergeCgcGenerations(first: Enode, firstGeneration: number, second: Enode) {
|
|
654
|
+
this.trailInts.push(firstGeneration, second.generation);
|
|
655
|
+
this.trailNodes.push(first, second);
|
|
656
|
+
this.trailKinds.push(UNDO_CGC_GENERATIONS);
|
|
657
|
+
if (firstGeneration < second.generation) second.generation = firstGeneration;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
private setMergeTf(node: Enode, boolVar: BoolVar, isNewVar: boolean) {
|
|
661
|
+
if (node.isMergeTf) return;
|
|
662
|
+
if (!isNewVar) {
|
|
663
|
+
this.trailNodes.push(node);
|
|
664
|
+
this.trailKinds.push(UNDO_MERGE_TF);
|
|
665
|
+
}
|
|
666
|
+
node.isMergeTf = true;
|
|
667
|
+
const value = this.assignment[mkLiteral(boolVar)];
|
|
668
|
+
if (value === L_TRUE && node.root !== this.trueEnode.root)
|
|
669
|
+
this.pushEq(node, this.trueEnode, { kind: "equation", literal: mkLiteral(boolVar) });
|
|
670
|
+
else if (value === L_FALSE && node.root !== this.falseEnode.root)
|
|
671
|
+
this.pushEq(node, this.falseEnode, { kind: "equation", literal: mkLiteral(boolVar, true) });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
private setEnodeFlag(boolVar: BoolVar, isNewVar: boolean) {
|
|
675
|
+
if (this.hasBoolVarFlag(boolVar, BV_ENODE)) return;
|
|
676
|
+
if (!isNewVar) {
|
|
677
|
+
this.trailInts.push(boolVar);
|
|
678
|
+
this.trailKinds.push(UNDO_ENODE_FLAG);
|
|
679
|
+
}
|
|
680
|
+
this.setBoolVarFlag(boolVar, BV_ENODE, true);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
// update_generation: a node of a congruence class records its lower generation as sticky, so
|
|
684
|
+
// undoing a merge of the class keeps it.
|
|
685
|
+
private updateGeneration(node: Enode) {
|
|
686
|
+
if (node.isEq) return;
|
|
687
|
+
if (this.generation > 0 && this.generation < this.getGeneration(node)) {
|
|
688
|
+
this.setGeneration(node, this.generation);
|
|
689
|
+
if (node.usesCgTable()) this.stickyGenerations.set(node, this.generation);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// Internalization.
|
|
694
|
+
|
|
695
|
+
private isGate(expr: number) {
|
|
696
|
+
const op = this.manager.getOp(expr);
|
|
697
|
+
if (op === OP_AND || op === OP_OR || op === OP_ITE) return true;
|
|
698
|
+
return op === OP_EQ && this.manager.isBool(this.manager.getArg(expr, 0));
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// should_internalize_rec: a term, or a Boolean application of the basic family or an
|
|
702
|
+
// uninterpreted declaration.
|
|
703
|
+
private shouldInternalizeRec(expr: number) {
|
|
704
|
+
const manager = this.manager;
|
|
705
|
+
if (manager.kinds[expr] !== KIND_APP || manager.sorts[expr] !== SORT_BOOL) return true;
|
|
706
|
+
const family = manager.getFamily(expr);
|
|
707
|
+
return family === FAMILY_BASIC || family === FAMILY_USER;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// internalize_deep: internalizes the subterms of a deep expression bottom up, in the order of a
|
|
711
|
+
// topological sort, so the recursion of internalizeRec stays shallow.
|
|
712
|
+
private internalizeDeep(expr: number) {
|
|
713
|
+
if (
|
|
714
|
+
this.isEInternalized(expr) ||
|
|
715
|
+
this.manager.depths[expr] <= DEEP_EXPR_THRESHOLD ||
|
|
716
|
+
!this.shouldInternalizeRec(expr)
|
|
717
|
+
)
|
|
718
|
+
return;
|
|
719
|
+
const manager = this.manager;
|
|
720
|
+
// The color of each expression in a gate context and outside one: white while unvisited, grey
|
|
721
|
+
// while its children are visited, and black once it is sorted.
|
|
722
|
+
const colors = new Map<number, number>();
|
|
723
|
+
const getColor = (current: number, isGate: boolean) =>
|
|
724
|
+
colors.get(current * 2 + (isGate ? 1 : 0)) ?? WHITE;
|
|
725
|
+
const todo: [number, boolean][] = [[expr, true]];
|
|
726
|
+
const sorted: [number, boolean][] = [];
|
|
727
|
+
const visitChild = (child: number, isGate: boolean) => {
|
|
728
|
+
if (getColor(child, isGate) === WHITE) todo.push([child, isGate]);
|
|
729
|
+
};
|
|
730
|
+
// ts_visit_children, which Z3 calls again on grey expressions only in its assertions.
|
|
731
|
+
const visitChildren = (current: number) => {
|
|
732
|
+
if (manager.kinds[current] === KIND_QUANTIFIER || !this.shouldInternalizeRec(current)) return;
|
|
733
|
+
const node = this.exprToEnode[current];
|
|
734
|
+
if (node !== undefined) this.updateGeneration(node);
|
|
735
|
+
if (manager.sorts[current] === SORT_BOOL ? this.isBInternalized(current) : node !== undefined)
|
|
736
|
+
return;
|
|
737
|
+
if (manager.getOp(current) === OP_ITE && manager.sorts[current] !== SORT_BOOL) {
|
|
738
|
+
visitChild(manager.getArg(current, 0), true);
|
|
739
|
+
visitChild(manager.getArg(current, 1), false);
|
|
740
|
+
visitChild(manager.getArg(current, 2), false);
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
const isNewGate =
|
|
744
|
+
manager.sorts[current] === SORT_BOOL && (this.isGate(current) || manager.isNot(current));
|
|
745
|
+
for (let index = manager.getNumArgs(current) - 1; index >= 0; index--)
|
|
746
|
+
visitChild(manager.getArg(current, index), isNewGate);
|
|
747
|
+
};
|
|
748
|
+
while (todo.length > 0) {
|
|
749
|
+
const top = todo[todo.length - 1];
|
|
750
|
+
const color = getColor(top[0], top[1]);
|
|
751
|
+
if (color === BLACK) todo.pop();
|
|
752
|
+
else if (color === WHITE) {
|
|
753
|
+
colors.set(top[0] * 2 + (top[1] ? 1 : 0), GREY);
|
|
754
|
+
visitChildren(top[0]);
|
|
755
|
+
} else {
|
|
756
|
+
colors.set(top[0] * 2 + (top[1] ? 1 : 0), BLACK);
|
|
757
|
+
if (top[0] !== expr && !manager.isNot(top[0]) && this.shouldInternalizeRec(top[0]))
|
|
758
|
+
sorted.push(top);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
for (const entry of sorted) this.internalizeRec(entry[0], entry[1]);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
internalizeAssertion(expr: number, generation: number) {
|
|
765
|
+
const savedGeneration = this.generation;
|
|
766
|
+
this.generation = generation;
|
|
767
|
+
this.internalizeDeep(expr);
|
|
768
|
+
const manager = this.manager;
|
|
769
|
+
if (this.isGate(expr)) {
|
|
770
|
+
switch (manager.getOp(expr)) {
|
|
771
|
+
case OP_AND:
|
|
772
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
773
|
+
const arg = manager.getArg(expr, index);
|
|
774
|
+
this.internalizeRec(arg, true);
|
|
775
|
+
this.mkClause([this.getLiteral(arg)], null, CLS_AUX);
|
|
776
|
+
}
|
|
777
|
+
break;
|
|
778
|
+
case OP_OR: {
|
|
779
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
780
|
+
const arg = manager.getArg(expr, index);
|
|
781
|
+
this.internalizeRec(arg, true);
|
|
782
|
+
}
|
|
783
|
+
this.mkClause(
|
|
784
|
+
manager.mapArgs(expr, (innerArg) => this.getLiteral(innerArg)),
|
|
785
|
+
null,
|
|
786
|
+
CLS_AUX,
|
|
787
|
+
);
|
|
788
|
+
this.addOrRelWatches(expr);
|
|
789
|
+
break;
|
|
790
|
+
}
|
|
791
|
+
case OP_EQ: {
|
|
792
|
+
this.internalizeRec(manager.getArg(expr, 0), true);
|
|
793
|
+
this.internalizeRec(manager.getArg(expr, 1), true);
|
|
794
|
+
const first = this.getLiteral(manager.getArg(expr, 0));
|
|
795
|
+
const second = this.getLiteral(manager.getArg(expr, 1));
|
|
796
|
+
this.mkClause([first, negate(second)], null, CLS_AUX);
|
|
797
|
+
this.mkClause([negate(first), second], null, CLS_AUX);
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
default: {
|
|
801
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
802
|
+
const arg = manager.getArg(expr, index);
|
|
803
|
+
this.internalizeRec(arg, true);
|
|
804
|
+
}
|
|
805
|
+
const condition = this.getLiteral(manager.getArg(expr, 0));
|
|
806
|
+
this.mkClause(
|
|
807
|
+
[negate(condition), this.getLiteral(manager.getArg(expr, 1))],
|
|
808
|
+
null,
|
|
809
|
+
CLS_AUX,
|
|
810
|
+
);
|
|
811
|
+
this.mkClause([condition, this.getLiteral(manager.getArg(expr, 2))], null, CLS_AUX);
|
|
812
|
+
this.addIteRelWatches(expr);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
this.markAsRelevant(expr);
|
|
816
|
+
} else {
|
|
817
|
+
this.internalizeRec(expr, true);
|
|
818
|
+
const literal = this.getLiteral(expr);
|
|
819
|
+
if (literal === this.falseLiteral) this.setConflict("axiom", null);
|
|
820
|
+
else if (literal !== this.trueLiteral) {
|
|
821
|
+
this.assign(literal, "axiom");
|
|
822
|
+
this.markLiteralAsRelevant(literal);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
this.generation = savedGeneration;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
internalize(expr: number, isGateContext: boolean, generation = this.generation) {
|
|
829
|
+
const savedGeneration = this.generation;
|
|
830
|
+
this.generation = generation;
|
|
831
|
+
this.internalizeDeep(expr);
|
|
832
|
+
this.internalizeRec(expr, isGateContext);
|
|
833
|
+
this.generation = savedGeneration;
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
private internalizeRec(expr: number, isGateContext: boolean) {
|
|
837
|
+
const manager = this.manager;
|
|
838
|
+
if (manager.sorts[expr] === SORT_BOOL) this.internalizeFormula(expr, isGateContext);
|
|
839
|
+
else this.internalizeTerm(expr);
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
private internalizeFormula(expr: number, isGateContext: boolean) {
|
|
843
|
+
const manager = this.manager;
|
|
844
|
+
if (expr === manager.trueExpr || expr === manager.falseExpr) return;
|
|
845
|
+
if (manager.isNot(expr) && isGateContext) {
|
|
846
|
+
this.internalizeRec(manager.getArg(expr, 0), true);
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (this.isBInternalized(expr)) {
|
|
850
|
+
const boolVar = this.getBoolVar(expr);
|
|
851
|
+
const node = this.exprToEnode[expr];
|
|
852
|
+
if (node !== undefined) this.updateGeneration(node);
|
|
853
|
+
if (!isGateContext && manager.kinds[expr] === KIND_APP) {
|
|
854
|
+
if (node !== undefined) this.setMergeTf(node, boolVar, false);
|
|
855
|
+
else {
|
|
856
|
+
this.mkEnode(expr, true, true, false);
|
|
857
|
+
this.setEnodeFlag(boolVar, false);
|
|
858
|
+
if (this.assignment[mkLiteral(boolVar)] !== L_UNDEF) this.propagateBoolVarEnode(boolVar);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const op = manager.getOp(expr);
|
|
864
|
+
if (op === OP_EQ && !manager.isBool(manager.getArg(expr, 0)))
|
|
865
|
+
this.internalizeEq(expr, isGateContext);
|
|
866
|
+
else if (manager.kinds[expr] === KIND_APP && this.internalizeTheoryAtom(expr, isGateContext))
|
|
867
|
+
return;
|
|
868
|
+
else if (manager.kinds[expr] === KIND_QUANTIFIER) this.internalizeQuantifier(expr);
|
|
869
|
+
else this.internalizeFormulaCore(expr, isGateContext);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
private internalizeEq(expr: number, isGateContext: boolean) {
|
|
873
|
+
this.internalizeFormulaCore(expr, isGateContext);
|
|
874
|
+
const boolVar = this.getBoolVar(expr);
|
|
875
|
+
this.setBoolVarFlag(boolVar, BV_EQ, true);
|
|
876
|
+
if (this.manager.sorts[this.manager.getArg(expr, 0)] !== SORT_BOOL && this.isArithSort(expr))
|
|
877
|
+
this.theory.internalizeEqEh(expr);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
private isArithSort(eq: number) {
|
|
881
|
+
return isArithSort(this.manager.sorts[this.manager.getArg(eq, 0)]);
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
private internalizeTheoryAtom(expr: number, isGateContext: boolean) {
|
|
885
|
+
if (this.manager.getFamily(expr) !== FAMILY_ARITH || !this.theory.internalizeAtom(expr))
|
|
886
|
+
return false;
|
|
887
|
+
const boolVar = this.getBoolVar(expr);
|
|
888
|
+
if (!isGateContext) {
|
|
889
|
+
const node = this.exprToEnode[expr];
|
|
890
|
+
if (node === undefined) this.mkEnode(expr, true, true, false);
|
|
891
|
+
else {
|
|
892
|
+
this.setEnodeFlag(boolVar, true);
|
|
893
|
+
this.setMergeTf(node, boolVar, true);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (this.exprToEnode[expr] !== undefined) {
|
|
897
|
+
this.setEnodeFlag(boolVar, true);
|
|
898
|
+
if (this.assignment[mkLiteral(boolVar)] !== L_UNDEF) this.propagateBoolVarEnode(boolVar);
|
|
899
|
+
}
|
|
900
|
+
return true;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
private internalizeQuantifier(expr: number) {
|
|
904
|
+
const boolVar = this.mkBoolVar(expr);
|
|
905
|
+
const generation = this.cachedGeneration.get(expr) ?? this.generation;
|
|
906
|
+
this.setBoolVarFlag(boolVar, BV_QUANTIFIER, true);
|
|
907
|
+
this.qmanager.add(expr, generation);
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
protected internalizeFormulaCore(expr: number, isGateContext: boolean) {
|
|
911
|
+
const manager = this.manager;
|
|
912
|
+
const op = manager.getOp(expr);
|
|
913
|
+
const isGateNode = this.isGate(expr) || op === OP_NOT;
|
|
914
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
915
|
+
const arg = manager.getArg(expr, index);
|
|
916
|
+
this.internalizeRec(arg, isGateNode);
|
|
917
|
+
}
|
|
918
|
+
const isNewVar = !this.isBInternalized(expr);
|
|
919
|
+
const boolVar = isNewVar ? this.mkBoolVar(expr) : this.getBoolVar(expr);
|
|
920
|
+
const node = this.exprToEnode[expr];
|
|
921
|
+
if (node === undefined && (!isGateContext || (!isGateNode && manager.getNumArgs(expr) > 0))) {
|
|
922
|
+
this.mkEnode(expr, isGateNode, !isGateContext, true);
|
|
923
|
+
this.setEnodeFlag(boolVar, isNewVar);
|
|
924
|
+
} else if (!isGateContext && node !== undefined) this.setMergeTf(node, boolVar, isNewVar);
|
|
925
|
+
if (!isNewVar || manager.getFamily(expr) !== FAMILY_BASIC) return;
|
|
926
|
+
const literal = mkLiteral(boolVar);
|
|
927
|
+
switch (op) {
|
|
928
|
+
case OP_NOT: {
|
|
929
|
+
const child = this.getLiteral(manager.getArg(expr, 0));
|
|
930
|
+
this.mkGateClause(negate(literal), negate(child));
|
|
931
|
+
this.mkGateClause(literal, child);
|
|
932
|
+
break;
|
|
933
|
+
}
|
|
934
|
+
case OP_AND: {
|
|
935
|
+
const buffer = this.gateBuffer;
|
|
936
|
+
shrink(buffer, 0);
|
|
937
|
+
buffer.push(literal);
|
|
938
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
939
|
+
const arg = manager.getArg(expr, index);
|
|
940
|
+
const argLiteral = this.getLiteral(arg);
|
|
941
|
+
this.mkGateClause(negate(literal), argLiteral);
|
|
942
|
+
buffer.push(negate(argLiteral));
|
|
943
|
+
}
|
|
944
|
+
this.mkClause(buffer, null, CLS_AUX);
|
|
945
|
+
this.addAndRelWatches(expr);
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
case OP_OR: {
|
|
949
|
+
const buffer = this.gateBuffer;
|
|
950
|
+
shrink(buffer, 0);
|
|
951
|
+
buffer.push(negate(literal));
|
|
952
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
953
|
+
const arg = manager.getArg(expr, index);
|
|
954
|
+
const argLiteral = this.getLiteral(arg);
|
|
955
|
+
this.mkGateClause(literal, negate(argLiteral));
|
|
956
|
+
buffer.push(argLiteral);
|
|
957
|
+
}
|
|
958
|
+
this.mkClause(buffer, null, CLS_AUX);
|
|
959
|
+
this.addOrRelWatches(expr);
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
case OP_IMPLIES: {
|
|
963
|
+
const first = this.getLiteral(manager.getArg(expr, 0));
|
|
964
|
+
const second = this.getLiteral(manager.getArg(expr, 1));
|
|
965
|
+
this.mkGateClause(literal, first);
|
|
966
|
+
this.mkGateClause(literal, negate(second));
|
|
967
|
+
this.mkGateClause(negate(literal), negate(first), second);
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
case OP_EQ:
|
|
971
|
+
case OP_XOR: {
|
|
972
|
+
if (op === OP_EQ && !manager.isBool(manager.getArg(expr, 0))) break;
|
|
973
|
+
const gate = op === OP_XOR ? negate(literal) : literal;
|
|
974
|
+
const first = this.getLiteral(manager.getArg(expr, 0));
|
|
975
|
+
const second = this.getLiteral(manager.getArg(expr, 1));
|
|
976
|
+
this.mkGateClause(negate(gate), first, negate(second));
|
|
977
|
+
this.mkGateClause(negate(gate), negate(first), second);
|
|
978
|
+
this.mkGateClause(gate, first, second);
|
|
979
|
+
this.mkGateClause(gate, negate(first), negate(second));
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
case OP_ITE: {
|
|
983
|
+
const condition = this.getLiteral(manager.getArg(expr, 0));
|
|
984
|
+
const thenLiteral = this.getLiteral(manager.getArg(expr, 1));
|
|
985
|
+
const elseLiteral = this.getLiteral(manager.getArg(expr, 2));
|
|
986
|
+
this.mkGateClause(negate(literal), negate(condition), thenLiteral);
|
|
987
|
+
this.mkGateClause(negate(literal), condition, elseLiteral);
|
|
988
|
+
this.mkGateClause(literal, negate(condition), negate(thenLiteral));
|
|
989
|
+
this.mkGateClause(literal, condition, negate(elseLiteral));
|
|
990
|
+
this.addIteRelWatches(expr);
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
default:
|
|
994
|
+
break;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
// mk_gate_clause with two or three literals.
|
|
999
|
+
private mkGateClause(first: Literal, second: Literal, third?: Literal) {
|
|
1000
|
+
const literals = this.gateLiterals;
|
|
1001
|
+
shrink(literals, 0);
|
|
1002
|
+
literals.push(first, second);
|
|
1003
|
+
if (third !== undefined) literals.push(third);
|
|
1004
|
+
this.mkClause(literals, null, CLS_AUX);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
internalizeTerm(expr: number) {
|
|
1008
|
+
const manager = this.manager;
|
|
1009
|
+
const existing = this.exprToEnode[expr];
|
|
1010
|
+
if (existing !== undefined) {
|
|
1011
|
+
this.updateGeneration(existing);
|
|
1012
|
+
// The theory may have made a nested term, such as the 2 of (* 2 x), without a variable, which
|
|
1013
|
+
// it needs once the term occurs elsewhere.
|
|
1014
|
+
if (manager.getFamily(expr) === FAMILY_ARITH && !this.theory.isAttachedToVar(existing)) {
|
|
1015
|
+
this.theory.internalizeTerm(expr);
|
|
1016
|
+
}
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
if (manager.getOp(expr) === OP_ITE) {
|
|
1020
|
+
this.internalizeIteTerm(expr);
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
if (manager.getFamily(expr) === FAMILY_ARITH && this.theory.internalizeTerm(expr)) return;
|
|
1024
|
+
for (let index = 0; index < manager.getNumArgs(expr); index++) {
|
|
1025
|
+
const arg = manager.getArg(expr, index);
|
|
1026
|
+
this.internalizeRec(arg, false);
|
|
1027
|
+
}
|
|
1028
|
+
if (this.exprToEnode[expr] !== undefined) return;
|
|
1029
|
+
this.mkEnode(expr, false, false, true);
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
// mk_eq_atom: the side with the smaller id goes first.
|
|
1033
|
+
mkEqAtom(lhs: number, rhs: number) {
|
|
1034
|
+
const ids = this.manager.ids;
|
|
1035
|
+
return ids[lhs] < ids[rhs] ? this.manager.mkEq(lhs, rhs) : this.manager.mkEq(rhs, lhs);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
private internalizeIteTerm(expr: number) {
|
|
1039
|
+
const thenEq = this.mkEqAtom(expr, this.manager.getArg(expr, 1));
|
|
1040
|
+
const elseEq = this.mkEqAtom(expr, this.manager.getArg(expr, 2));
|
|
1041
|
+
this.mkEnode(expr, true, false, false);
|
|
1042
|
+
this.internalizeRec(this.manager.getArg(expr, 0), true);
|
|
1043
|
+
this.internalizeRec(this.manager.getArg(expr, 1), false);
|
|
1044
|
+
this.internalizeRec(this.manager.getArg(expr, 2), false);
|
|
1045
|
+
this.internalizeRec(thenEq, true);
|
|
1046
|
+
this.internalizeRec(elseEq, true);
|
|
1047
|
+
const condition = this.getLiteral(this.manager.getArg(expr, 0));
|
|
1048
|
+
this.mkClause([negate(condition), this.getLiteral(thenEq)], null, CLS_AUX);
|
|
1049
|
+
this.mkClause([condition, this.getLiteral(elseEq)], null, CLS_AUX);
|
|
1050
|
+
// ite_term_relevancy_eh: it marks the terms for the propagator, which propagates them later.
|
|
1051
|
+
const handler = () => {
|
|
1052
|
+
if (!this.isRelevant(expr)) return;
|
|
1053
|
+
this.relevancy.markAsRelevant(this.manager.getArg(expr, 0));
|
|
1054
|
+
const value = this.findAssignment(this.manager.getArg(expr, 0));
|
|
1055
|
+
if (value < 0) this.relevancy.markAsRelevant(elseEq);
|
|
1056
|
+
else if (value > 0) this.relevancy.markAsRelevant(thenEq);
|
|
1057
|
+
};
|
|
1058
|
+
this.addRelWatch(condition, handler);
|
|
1059
|
+
this.addRelWatch(negate(condition), handler);
|
|
1060
|
+
this.relevancy.addHandler(expr, handler);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
private addRelWatch(literal: Literal, handler: () => void) {
|
|
1064
|
+
this.relevancy.addWatch(this.boolVar2Expr[getVar(literal)], !isNegated(literal), handler);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
private addAndRelWatches(expr: number) {
|
|
1068
|
+
const handler = () => {
|
|
1069
|
+
if (this.isRelevant(expr)) this.relevancy.propagateAnd(expr);
|
|
1070
|
+
};
|
|
1071
|
+
for (let index = 0; index < this.manager.getNumArgs(expr); index++) {
|
|
1072
|
+
const arg = this.manager.getArg(expr, index);
|
|
1073
|
+
const literal = this.getLiteral(arg);
|
|
1074
|
+
if (literal > 1) this.addRelWatch(negate(literal), handler);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
private addOrRelWatches(expr: number) {
|
|
1079
|
+
const handler = () => {
|
|
1080
|
+
if (this.isRelevant(expr)) this.relevancy.propagateOr(expr);
|
|
1081
|
+
};
|
|
1082
|
+
for (let index = 0; index < this.manager.getNumArgs(expr); index++) {
|
|
1083
|
+
const arg = this.manager.getArg(expr, index);
|
|
1084
|
+
const literal = this.getLiteral(arg);
|
|
1085
|
+
if (literal > 1) this.addRelWatch(literal, handler);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
private addIteRelWatches(expr: number) {
|
|
1090
|
+
const handler = () => {
|
|
1091
|
+
if (this.isRelevant(expr)) this.relevancy.propagateIte(expr);
|
|
1092
|
+
};
|
|
1093
|
+
const literal = this.getLiteral(this.manager.getArg(expr, 0));
|
|
1094
|
+
if (literal <= 1) return;
|
|
1095
|
+
this.addRelWatch(literal, handler);
|
|
1096
|
+
this.addRelWatch(negate(literal), handler);
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
// Clauses.
|
|
1100
|
+
|
|
1101
|
+
mkThAxiom(literals: Literal[]) {
|
|
1102
|
+
this.mkClause(literals, null, CLS_TH_AXIOM);
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
private simplifyAuxClauseLiterals(literals: Literal[], removed: Literal[]) {
|
|
1106
|
+
sortNumbers(literals);
|
|
1107
|
+
let previous: Literal | null = null;
|
|
1108
|
+
let kept = 0;
|
|
1109
|
+
for (const literal of literals) {
|
|
1110
|
+
const value = this.assignment[literal];
|
|
1111
|
+
if (value === L_FALSE) {
|
|
1112
|
+
if (literal !== previous) {
|
|
1113
|
+
previous = literal;
|
|
1114
|
+
removed.push(negate(literal));
|
|
1115
|
+
}
|
|
1116
|
+
} else if (value === L_UNDEF) {
|
|
1117
|
+
if (previous !== null && literal === negate(previous)) return false;
|
|
1118
|
+
if (literal !== previous) {
|
|
1119
|
+
previous = literal;
|
|
1120
|
+
literals[kept++] = literal;
|
|
1121
|
+
}
|
|
1122
|
+
} else return false;
|
|
1123
|
+
}
|
|
1124
|
+
shrink(literals, kept);
|
|
1125
|
+
return true;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// simplify_aux_lemma_literals: unlike an aux clause, a theory lemma keeps its false literals, so it
|
|
1129
|
+
// needs no unit resolution justification.
|
|
1130
|
+
private simplifyAuxLemmaLiterals(literals: Literal[]) {
|
|
1131
|
+
sortNumbers(literals);
|
|
1132
|
+
let previous: Literal | null = null;
|
|
1133
|
+
let kept = 0;
|
|
1134
|
+
for (const literal of literals) {
|
|
1135
|
+
if (this.getLevel(getVar(literal)) <= this.baseLevel && this.assignment[literal] === L_TRUE)
|
|
1136
|
+
return false;
|
|
1137
|
+
if (previous !== null && literal === negate(previous)) return false;
|
|
1138
|
+
if (literal !== previous) {
|
|
1139
|
+
previous = literal;
|
|
1140
|
+
literals[kept++] = literal;
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
shrink(literals, kept);
|
|
1144
|
+
return true;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
protected selectWatchLiteral(clause: Clause, startingAt: number) {
|
|
1148
|
+
const clauses = this.clauses;
|
|
1149
|
+
let minTrue = -1;
|
|
1150
|
+
let maxFalse = -1;
|
|
1151
|
+
let unknown = -1;
|
|
1152
|
+
for (let index = startingAt; index < clauses.getNumLiterals(clause); index++) {
|
|
1153
|
+
const literal = clauses.getLiteral(clause, index);
|
|
1154
|
+
const value = this.assignment[literal];
|
|
1155
|
+
if (value === L_FALSE) {
|
|
1156
|
+
if (
|
|
1157
|
+
maxFalse === -1 ||
|
|
1158
|
+
this.getLevel(getVar(literal)) >
|
|
1159
|
+
this.getLevel(getVar(clauses.getLiteral(clause, maxFalse)))
|
|
1160
|
+
)
|
|
1161
|
+
maxFalse = index;
|
|
1162
|
+
} else if (value === L_UNDEF) unknown = index;
|
|
1163
|
+
else if (
|
|
1164
|
+
minTrue === -1 ||
|
|
1165
|
+
this.getLevel(getVar(literal)) < this.getLevel(getVar(clauses.getLiteral(clause, minTrue)))
|
|
1166
|
+
)
|
|
1167
|
+
minTrue = index;
|
|
1168
|
+
}
|
|
1169
|
+
if (minTrue !== -1) return minTrue;
|
|
1170
|
+
if (unknown !== -1) return unknown;
|
|
1171
|
+
return maxFalse;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
private selectLearnedWatchLiteral(clause: Clause) {
|
|
1175
|
+
const clauses = this.clauses;
|
|
1176
|
+
let maxFalse = -1;
|
|
1177
|
+
let maxLevel = 0;
|
|
1178
|
+
for (let index = 1; index < clauses.getNumLiterals(clause); index++) {
|
|
1179
|
+
const literal = clauses.getLiteral(clause, index);
|
|
1180
|
+
const level =
|
|
1181
|
+
this.assignment[literal] === L_FALSE ? this.getLevel(getVar(literal)) : this.scopeLevel;
|
|
1182
|
+
if (maxFalse === -1 || level > maxLevel) {
|
|
1183
|
+
maxFalse = index;
|
|
1184
|
+
maxLevel = level;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
return maxFalse;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
protected addWatchLiteral(clause: Clause, index: number) {
|
|
1191
|
+
this.addWatch(negate(this.clauses.getLiteral(clause, index)), clause);
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
// A literal's first watch gets a list of its own size, since growing an empty list allocates room
|
|
1195
|
+
// for many.
|
|
1196
|
+
protected addWatch(literal: Literal, clause: Clause) {
|
|
1197
|
+
const list = this.watches[literal];
|
|
1198
|
+
if (list === undefined) this.watches[literal] = [clause];
|
|
1199
|
+
else list.push(clause);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
protected removeWatchLiteral(clause: Clause, index: number) {
|
|
1203
|
+
const list = this.watches[negate(this.clauses.getLiteral(clause, index))];
|
|
1204
|
+
if (list === undefined) return;
|
|
1205
|
+
// A clause is deleted when its scope is popped, so it is most often near the end.
|
|
1206
|
+
const position = list.lastIndexOf(clause);
|
|
1207
|
+
if (position === -1) return;
|
|
1208
|
+
list.copyWithin(position, position + 1);
|
|
1209
|
+
list.pop();
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
protected getBinJustification(literal: Literal) {
|
|
1213
|
+
const existing = this.binJustifications[literal];
|
|
1214
|
+
if (existing !== undefined) return existing;
|
|
1215
|
+
const created = new BinJustification(literal);
|
|
1216
|
+
this.binJustifications[literal] = created;
|
|
1217
|
+
return created;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
// use_binary_clause_opt: a clause of two literals at the base level of the first scope, over
|
|
1221
|
+
// variables created there, becomes a pair of binary watches. With relevancy, lemmas stay clauses so
|
|
1222
|
+
// their propagations can mark literals as relevant.
|
|
1223
|
+
private isUsingBinaryClauseOpt(first: Literal, second: Literal, isLemma: boolean) {
|
|
1224
|
+
if (isLemma && this.relevancy.isEnabled) return false;
|
|
1225
|
+
if (this.baseLevel > 0) return false;
|
|
1226
|
+
if (!isLemma && this.scopeLevel > 0) return false;
|
|
1227
|
+
return this.getInternLevel(getVar(first)) === 0 && this.getInternLevel(getVar(second)) === 0;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
private getMaxIscopeLevel(literals: Literal[]) {
|
|
1231
|
+
let result = 0;
|
|
1232
|
+
for (const literal of literals) result = Math.max(result, this.getInternLevel(getVar(literal)));
|
|
1233
|
+
return result;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
private markForReinit(clause: Clause, level: number, isReinternalizingAtoms: boolean) {
|
|
1237
|
+
this.clauses.setReinit(clause, true);
|
|
1238
|
+
this.clauses.setReinternalizeAtoms(clause, isReinternalizingAtoms);
|
|
1239
|
+
while (this.clausesToReinit.length <= level) this.clausesToReinit.push([]);
|
|
1240
|
+
this.clausesToReinit[level].push(clause);
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
// mk_clause: like Z3's, it simplifies the literals in the array it is given.
|
|
1244
|
+
mkClause(
|
|
1245
|
+
input: Literal[],
|
|
1246
|
+
inputJustification: Justification | null,
|
|
1247
|
+
kind: number,
|
|
1248
|
+
delEh: (() => void) | null = null,
|
|
1249
|
+
) {
|
|
1250
|
+
const literals = input;
|
|
1251
|
+
let justification = inputJustification;
|
|
1252
|
+
if (kind === CLS_TH_LEMMA) {
|
|
1253
|
+
if (!this.simplifyAuxLemmaLiterals(literals)) return null;
|
|
1254
|
+
} else if (kind !== CLS_LEARNED) {
|
|
1255
|
+
const removed = this.removedLiterals;
|
|
1256
|
+
shrink(removed, 0);
|
|
1257
|
+
if (!this.simplifyAuxClauseLiterals(literals, removed)) return null;
|
|
1258
|
+
if (removed.length > 0)
|
|
1259
|
+
justification = new UnitResolutionJustification(justification, removed.slice());
|
|
1260
|
+
}
|
|
1261
|
+
if (literals.length === 0) {
|
|
1262
|
+
this.setConflict(justification ?? "axiom", null);
|
|
1263
|
+
return null;
|
|
1264
|
+
}
|
|
1265
|
+
if (literals.length === 1) {
|
|
1266
|
+
this.assign(literals[0], justification ?? "axiom");
|
|
1267
|
+
return null;
|
|
1268
|
+
}
|
|
1269
|
+
const isLemma = kind === CLS_LEARNED || kind === CLS_TH_LEMMA;
|
|
1270
|
+
if (literals.length === 2 && this.isUsingBinaryClauseOpt(literals[0], literals[1], isLemma)) {
|
|
1271
|
+
const first = literals[0];
|
|
1272
|
+
const second = literals[1];
|
|
1273
|
+
(this.binaryWatches[negate(first)] ??= []).push(second);
|
|
1274
|
+
(this.binaryWatches[negate(second)] ??= []).push(first);
|
|
1275
|
+
if (this.assignment[first] === L_FALSE)
|
|
1276
|
+
this.assign(second, this.getBinJustification(negate(first)));
|
|
1277
|
+
else if (this.assignment[second] === L_FALSE)
|
|
1278
|
+
this.assign(first, this.getBinJustification(negate(second)));
|
|
1279
|
+
return null;
|
|
1280
|
+
}
|
|
1281
|
+
const iscopeLevel = isLemma ? this.getMaxIscopeLevel(literals) : 0;
|
|
1282
|
+
const isSavingAtoms = isLemma && iscopeLevel > this.baseLevel;
|
|
1283
|
+
const atoms = isSavingAtoms
|
|
1284
|
+
? literals.map((literal) => this.boolVar2Expr[getVar(literal)])
|
|
1285
|
+
: null;
|
|
1286
|
+
for (const atom of atoms ?? []) this.manager.incRef(atom);
|
|
1287
|
+
const clauses = this.clauses;
|
|
1288
|
+
const clause = clauses.mk(literals, kind, justification, delEh, atoms);
|
|
1289
|
+
if (isLemma) {
|
|
1290
|
+
if (kind === CLS_LEARNED)
|
|
1291
|
+
clauses.swapLiterals(clause, 1, this.selectLearnedWatchLiteral(clause));
|
|
1292
|
+
else {
|
|
1293
|
+
clauses.swapLiterals(clause, 0, this.selectWatchLiteral(clause, 0));
|
|
1294
|
+
clauses.swapLiterals(clause, 1, this.selectWatchLiteral(clause, 1));
|
|
1295
|
+
}
|
|
1296
|
+
this.lemmas.push(clause);
|
|
1297
|
+
this.addWatchLiteral(clause, 0);
|
|
1298
|
+
this.addWatchLiteral(clause, 1);
|
|
1299
|
+
const first = clauses.getLiteral(clause, 0);
|
|
1300
|
+
const second = clauses.getLiteral(clause, 1);
|
|
1301
|
+
// A theory lemma that conflicts or propagates above the base level is checked again when the
|
|
1302
|
+
// level is backtracked.
|
|
1303
|
+
const isPropagating =
|
|
1304
|
+
this.assignment[first] === L_FALSE || this.assignment[second] === L_FALSE;
|
|
1305
|
+
if (this.assignment[first] === L_FALSE) this.setConflict(clause, null);
|
|
1306
|
+
else if (this.assignment[second] === L_FALSE) this.assign(first, clause);
|
|
1307
|
+
const isReinit = kind === CLS_TH_LEMMA && isPropagating && this.scopeLevel > this.baseLevel;
|
|
1308
|
+
if (isSavingAtoms || isReinit)
|
|
1309
|
+
this.markForReinit(clause, isReinit ? this.scopeLevel : iscopeLevel, isSavingAtoms);
|
|
1310
|
+
return clause;
|
|
1311
|
+
}
|
|
1312
|
+
this.auxClauses.push(clause);
|
|
1313
|
+
this.addWatchLiteral(clause, 0);
|
|
1314
|
+
this.addWatchLiteral(clause, 1);
|
|
1315
|
+
if (this.assignment[literals[0]] === L_FALSE) this.setConflict(clause, null);
|
|
1316
|
+
else if (this.assignment[literals[1]] === L_FALSE) this.assign(literals[0], clause);
|
|
1317
|
+
return clause;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// del_clause. A clause in the reinit stack is only marked deleted, and reinitClauses frees it.
|
|
1321
|
+
protected deleteClause(clause: Clause) {
|
|
1322
|
+
const clauses = this.clauses;
|
|
1323
|
+
if (clauses.isDeleted(clause)) return;
|
|
1324
|
+
this.removeWatchLiteral(clause, 0);
|
|
1325
|
+
this.removeWatchLiteral(clause, 1);
|
|
1326
|
+
clauses.markAsDeleted(clause);
|
|
1327
|
+
clauses.getDelEh(clause)?.();
|
|
1328
|
+
if (clauses.isReinternalizeAtoms(clause)) this.releaseAtoms(clause);
|
|
1329
|
+
if (!clauses.isReinit(clause)) clauses.free(clause);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
// release_atoms: the clause no longer holds its atoms.
|
|
1333
|
+
protected releaseAtoms(clause: Clause) {
|
|
1334
|
+
for (const atom of this.clauses.getAtoms(clause)) this.manager.decRef(atom);
|
|
1335
|
+
this.clauses.clearAtoms(clause);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// del_clauses: past 1,000 clauses, mark each deleted, filter each of their watch lists once, then
|
|
1339
|
+
// release their atoms, instead of searching a watch list per clause.
|
|
1340
|
+
protected deleteClauses(clauses: Clause[], oldSize: number) {
|
|
1341
|
+
if (clauses.length - oldSize <= 1000) {
|
|
1342
|
+
for (let index = clauses.length - 1; index >= oldSize; index--)
|
|
1343
|
+
this.deleteClause(clauses[index]);
|
|
1344
|
+
shrink(clauses, oldSize);
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (this.watchMarks.length < this.watches.length)
|
|
1348
|
+
this.watchMarks = new Uint8Array(Math.max(2 * this.watchMarks.length, this.watches.length));
|
|
1349
|
+
const watched: Literal[] = [];
|
|
1350
|
+
const markWatch = (literal: Literal) => {
|
|
1351
|
+
if (this.watchMarks[literal] === 1) return;
|
|
1352
|
+
this.watchMarks[literal] = 1;
|
|
1353
|
+
watched.push(literal);
|
|
1354
|
+
};
|
|
1355
|
+
const store = this.clauses;
|
|
1356
|
+
for (let index = clauses.length - 1; index >= oldSize; index--) {
|
|
1357
|
+
const clause = clauses[index];
|
|
1358
|
+
if (!store.isDeleted(clause)) {
|
|
1359
|
+
store.markAsDeleted(clause);
|
|
1360
|
+
store.getDelEh(clause)?.();
|
|
1361
|
+
}
|
|
1362
|
+
markWatch(negate(store.getLiteral(clause, 0)));
|
|
1363
|
+
markWatch(negate(store.getLiteral(clause, 1)));
|
|
1364
|
+
}
|
|
1365
|
+
for (const literal of watched) {
|
|
1366
|
+
this.watchMarks[literal] = 0;
|
|
1367
|
+
const list = this.watches[literal];
|
|
1368
|
+
if (list === undefined) continue;
|
|
1369
|
+
let kept = 0;
|
|
1370
|
+
for (let index = 0; index < list.length; index++)
|
|
1371
|
+
if (!store.isDeleted(list[index])) list[kept++] = list[index];
|
|
1372
|
+
shrink(list, kept);
|
|
1373
|
+
}
|
|
1374
|
+
for (let index = clauses.length - 1; index >= oldSize; index--) {
|
|
1375
|
+
const clause = clauses[index];
|
|
1376
|
+
if (store.isReinternalizeAtoms(clause)) this.releaseAtoms(clause);
|
|
1377
|
+
if (!store.isReinit(clause)) store.free(clause);
|
|
1378
|
+
}
|
|
1379
|
+
shrink(clauses, oldSize);
|
|
1380
|
+
}
|
|
1381
|
+
}
|