tla-ts 0.0.4-dev.66582d4 → 0.0.4-dev.702eb98
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 +37 -1
- package/dist/index.d.ts +37 -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 +280 -80
- 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 +97 -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 +78 -35
- package/src/standard-modules.ts +5 -2
- package/src/successors.ts +25 -6
- package/src/tlc-random.ts +55 -32
- package/src/tlc-version.ts +8 -7
- package/src/values.ts +97 -47
- 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,347 @@
|
|
|
1
|
+
// Ported from Z3 (src/smt/smt_model_checker.cpp), MIT license, see z3-license.txt. Checks whether
|
|
2
|
+
// the candidate model satisfies each relevant quantifier: the negated body, with the model's
|
|
3
|
+
// interpretations in place of the uninterpreted symbols, goes to an auxiliary context. A model of it
|
|
4
|
+
// is a counterexample that becomes an instance of the quantifier.
|
|
5
|
+
import { SmtInvariantError } from "../../../errors";
|
|
6
|
+
import { KIND_APP, OP_NOT, OP_OR, SORT_BOOL, type AstManager } from "./ast";
|
|
7
|
+
import { AssertedFormulas } from "./asserted-formulas";
|
|
8
|
+
import { Context } from "./context";
|
|
9
|
+
import type { Enode } from "./enode";
|
|
10
|
+
import { L_FALSE, L_TRUE } from "./lbool";
|
|
11
|
+
import type { ModelFinder } from "./model-finder";
|
|
12
|
+
import type { ProtoModel } from "./proto-model";
|
|
13
|
+
import type { DefaultQuantifierManager } from "./quantifier-manager";
|
|
14
|
+
import { RefVector } from "./ref-vector";
|
|
15
|
+
import { VarSubst } from "./var-subst";
|
|
16
|
+
|
|
17
|
+
// qi_params' mbqi_max_cexs and mbqi_max_iterations.
|
|
18
|
+
const MBQI_MAX_CEXS = 1;
|
|
19
|
+
const MBQI_MAX_ITERATIONS = 1000;
|
|
20
|
+
|
|
21
|
+
// An instance whose bindings start at offset in m_pinned_exprs.
|
|
22
|
+
interface Instance {
|
|
23
|
+
quantifier: number;
|
|
24
|
+
offset: number;
|
|
25
|
+
generation: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The auxiliary context: Z3 keeps one context for every check and pushes a scope per check, so the
|
|
29
|
+
// restricted checks continue from the complete check, with its phases and activities. It runs without
|
|
30
|
+
// MBQI and with relevancy level 0, and preprocesses the assertions of a check together.
|
|
31
|
+
class AuxContext {
|
|
32
|
+
private context: Context;
|
|
33
|
+
private asserted: AssertedFormulas;
|
|
34
|
+
// theory_polymorphism's m_qhead: the number of formulas when it last added an assumption. Its
|
|
35
|
+
// trail restores it when the scope is popped, and no formula is asserted outside the scopes.
|
|
36
|
+
private polyQhead = 0;
|
|
37
|
+
// theory_polymorphism's m_assumption, which holds its last assumption.
|
|
38
|
+
private polyAssumption = -1;
|
|
39
|
+
|
|
40
|
+
// The context makes its asserted formulas first. Its theories hold only constants that exist by
|
|
41
|
+
// now, so setup_context can run at once. init_aux_context copies the kernel's smt_params, so the
|
|
42
|
+
// context has a model finder too, though it never sees a quantifier.
|
|
43
|
+
constructor(private manager: AstManager) {
|
|
44
|
+
this.asserted = new AssertedFormulas(manager);
|
|
45
|
+
this.context = new Context(manager, { isMbqi: true, relevancyLevel: 0 });
|
|
46
|
+
this.context.setupContext();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
push() {
|
|
50
|
+
this.context.push();
|
|
51
|
+
this.asserted.push();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
pop() {
|
|
55
|
+
this.context.pop(1);
|
|
56
|
+
this.asserted.pop();
|
|
57
|
+
this.polyQhead = 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// assert_expr_core: outside the search, the scopes of the last search are popped first.
|
|
61
|
+
assert(formula: number) {
|
|
62
|
+
this.context.popToBaseLevel();
|
|
63
|
+
this.asserted.assertExpr(formula);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// A model of the assertions, or whether they are unsatisfiable or undecided. Z3 registers
|
|
67
|
+
// theory_polymorphism, which assumes a fresh constant whenever formulas were asserted since its
|
|
68
|
+
// last assumption.
|
|
69
|
+
check() {
|
|
70
|
+
const context = this.context;
|
|
71
|
+
context.popToBaseLevel();
|
|
72
|
+
context.internalizeAssertions(this.asserted);
|
|
73
|
+
const manager = this.manager;
|
|
74
|
+
const numFormulas = this.asserted.getNumFormulas();
|
|
75
|
+
const isAssuming = numFormulas !== this.polyQhead;
|
|
76
|
+
this.polyQhead = numFormulas;
|
|
77
|
+
if (isAssuming) {
|
|
78
|
+
const assumption = manager.mkFreshConst("poly", SORT_BOOL);
|
|
79
|
+
manager.incRef(assumption);
|
|
80
|
+
if (this.polyAssumption !== -1) manager.decRef(this.polyAssumption);
|
|
81
|
+
this.polyAssumption = assumption;
|
|
82
|
+
}
|
|
83
|
+
context.initAssumptions(isAssuming ? [this.polyAssumption] : []);
|
|
84
|
+
const result = context.search();
|
|
85
|
+
return result === L_TRUE ? context.getModel() : result === L_FALSE ? "unsat" : "unknown";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class ModelChecker {
|
|
90
|
+
private maxCexs = MBQI_MAX_CEXS;
|
|
91
|
+
private iterationIndex = 0;
|
|
92
|
+
private newInstances: Instance[] = [];
|
|
93
|
+
// m_pinned_exprs: the bindings and quantifier of each new instance, held until they are asserted.
|
|
94
|
+
private pinnedExprs: RefVector;
|
|
95
|
+
private root2value = new Map<Enode, number>();
|
|
96
|
+
private value2expr: Map<number, number> | null = null;
|
|
97
|
+
private freshExprs: number[] = [];
|
|
98
|
+
private aux: AuxContext | null = null;
|
|
99
|
+
|
|
100
|
+
constructor(
|
|
101
|
+
private context: Context,
|
|
102
|
+
private modelFinder: ModelFinder,
|
|
103
|
+
private qmanager: DefaultQuantifierManager,
|
|
104
|
+
) {
|
|
105
|
+
this.pinnedExprs = new RefVector(context.manager);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
private get manager() {
|
|
109
|
+
return this.context.manager;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private getValue2expr() {
|
|
113
|
+
if (this.value2expr !== null) return this.value2expr;
|
|
114
|
+
const value2expr = new Map<number, number>();
|
|
115
|
+
for (const entry of this.root2value) {
|
|
116
|
+
const node = this.getEqEnodeWithMinGen(entry[0]);
|
|
117
|
+
if (!this.manager.isUniqueValue(node.owner)) value2expr.set(entry[1], node.owner);
|
|
118
|
+
}
|
|
119
|
+
this.value2expr = value2expr;
|
|
120
|
+
return value2expr;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// enode::get_eq_enode_with_min_gen: the first node of the class with generation 0, or else the
|
|
124
|
+
// first with the least generation.
|
|
125
|
+
private getEqEnodeWithMinGen(root: Enode) {
|
|
126
|
+
const context = this.context;
|
|
127
|
+
let result = root;
|
|
128
|
+
let resultGeneration = context.getGeneration(root);
|
|
129
|
+
let current = root;
|
|
130
|
+
do {
|
|
131
|
+
const generation = context.getGeneration(current);
|
|
132
|
+
if (generation === 0) return current;
|
|
133
|
+
if (generation < resultGeneration) {
|
|
134
|
+
result = current;
|
|
135
|
+
resultGeneration = generation;
|
|
136
|
+
}
|
|
137
|
+
current = current.next;
|
|
138
|
+
} while (current !== root);
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// get_type_compatible_term: a term of the context that stands for a value, which may be a fresh
|
|
143
|
+
// constant.
|
|
144
|
+
private getTypeCompatibleTerm(value: number): number {
|
|
145
|
+
const manager = this.manager;
|
|
146
|
+
let freshTerm: number;
|
|
147
|
+
if (manager.kinds[value] === KIND_APP && manager.getNumArgs(value) > 0)
|
|
148
|
+
freshTerm = manager.mkApp(
|
|
149
|
+
manager.decls[value],
|
|
150
|
+
manager.mapArgs(value, (arg) => this.getTypeCompatibleTerm(arg)),
|
|
151
|
+
manager.sorts[value],
|
|
152
|
+
);
|
|
153
|
+
else {
|
|
154
|
+
const term = this.getValue2expr().get(value);
|
|
155
|
+
if (term !== undefined) return term;
|
|
156
|
+
const existing = this.freshExprs.find(
|
|
157
|
+
(fresh) => manager.sorts[fresh] === manager.sorts[value],
|
|
158
|
+
);
|
|
159
|
+
if (existing !== undefined) return existing;
|
|
160
|
+
freshTerm = manager.mkFreshConst("sk", manager.sorts[value]);
|
|
161
|
+
}
|
|
162
|
+
this.freshExprs.push(freshTerm);
|
|
163
|
+
if (!this.context.isEInternalized(freshTerm)) this.context.internalize(freshTerm, false);
|
|
164
|
+
return freshTerm;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// assert_neg_q_m: asserts the negated body under the model, with fresh constants for the
|
|
168
|
+
// variables, which sks holds.
|
|
169
|
+
private assertNegQM(aux: AuxContext, quantifier: number, model: ProtoModel, sks: RefVector) {
|
|
170
|
+
const manager = this.manager;
|
|
171
|
+
const data = manager.getQuantifier(quantifier);
|
|
172
|
+
const body = model.eval(data.body, true);
|
|
173
|
+
const numDecls = data.sorts.length;
|
|
174
|
+
sks.resize(numDecls);
|
|
175
|
+
for (let index = 0; index < numDecls; index++) {
|
|
176
|
+
const sort = data.sorts[numDecls - index - 1];
|
|
177
|
+
const sk = manager.mkFreshConst(null, sort);
|
|
178
|
+
sks.set(numDecls - index - 1, sk);
|
|
179
|
+
if (model.isFinite(sort)) {
|
|
180
|
+
// restrict_to_universe: ast_manager::mk_or, which keeps a single disjunct in an or.
|
|
181
|
+
const universe = model.getKnownUniverse(sort).map((value) => manager.mkEq(sk, value));
|
|
182
|
+
const constraint = manager.mkApp(OP_OR, universe, SORT_BOOL);
|
|
183
|
+
manager.incRef(constraint);
|
|
184
|
+
aux.assert(constraint);
|
|
185
|
+
manager.decRef(constraint);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const subst = new VarSubst(manager, true);
|
|
189
|
+
const skBody = subst.apply(body, sks.items);
|
|
190
|
+
// m.mk_not, which makes (not true) of a true body too.
|
|
191
|
+
const negated = manager.mkApp(OP_NOT, [skBody], SORT_BOOL);
|
|
192
|
+
manager.incRef(negated);
|
|
193
|
+
aux.assert(negated);
|
|
194
|
+
manager.decRef(negated);
|
|
195
|
+
manager.decRef(skBody);
|
|
196
|
+
subst.release();
|
|
197
|
+
manager.decRef(body);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private addInstance(quantifier: number, cex: ProtoModel, sks: number[], isUsingInv: boolean) {
|
|
201
|
+
if (sks.length === 0) return false;
|
|
202
|
+
const manager = this.manager;
|
|
203
|
+
const numDecls = manager.getQuantifier(quantifier).sorts.length;
|
|
204
|
+
const bindings: number[] = [];
|
|
205
|
+
let maxGeneration = 0;
|
|
206
|
+
for (let index = 0; index < numDecls; index++) {
|
|
207
|
+
const sk = sks[numDecls - index - 1];
|
|
208
|
+
let skValue = cex.getSomeConstInterp(manager.decls[sk]);
|
|
209
|
+
if (isUsingInv) {
|
|
210
|
+
const inv = this.modelFinder.getInv(quantifier, index, skValue, cex);
|
|
211
|
+
if (inv === null) return false;
|
|
212
|
+
maxGeneration = Math.max(maxGeneration, inv.generation);
|
|
213
|
+
skValue = inv.term;
|
|
214
|
+
} else skValue = this.getValue2expr().get(skValue) ?? skValue;
|
|
215
|
+
if (manager.containsModelValue(skValue)) skValue = this.getTypeCompatibleTerm(skValue);
|
|
216
|
+
bindings[numDecls - index - 1] = skValue;
|
|
217
|
+
}
|
|
218
|
+
const offset = this.pinnedExprs.size;
|
|
219
|
+
for (const binding of bindings) this.pinnedExprs.push(binding);
|
|
220
|
+
this.pinnedExprs.push(quantifier);
|
|
221
|
+
this.newInstances.push({
|
|
222
|
+
quantifier,
|
|
223
|
+
offset,
|
|
224
|
+
generation: Math.max(this.qmanager.getGeneration(quantifier), maxGeneration),
|
|
225
|
+
});
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// check(q): whether the model satisfies the quantifier. A counterexample becomes an instance.
|
|
230
|
+
private checkQuantifier(quantifier: number, model: ProtoModel) {
|
|
231
|
+
const manager = this.manager;
|
|
232
|
+
const aux = this.aux ?? new AuxContext(manager);
|
|
233
|
+
this.aux = aux;
|
|
234
|
+
aux.push();
|
|
235
|
+
// sks: the fresh constants of the variables, which are released before the scope is popped.
|
|
236
|
+
const sks = new RefVector(manager);
|
|
237
|
+
const isSatisfied = this.checkInScope(aux, quantifier, model, sks);
|
|
238
|
+
sks.reset();
|
|
239
|
+
aux.pop();
|
|
240
|
+
return isSatisfied;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private checkInScope(aux: AuxContext, quantifier: number, model: ProtoModel, skRefs: RefVector) {
|
|
244
|
+
const flatQ = this.modelFinder.getFlatQuantifier(quantifier);
|
|
245
|
+
this.assertNegQM(aux, flatQ, model, skRefs);
|
|
246
|
+
const sks = skRefs.items;
|
|
247
|
+
const completeCex = aux.check();
|
|
248
|
+
if (completeCex === "unsat") return true;
|
|
249
|
+
if (completeCex === "unknown") return false;
|
|
250
|
+
// complete_cex and cex hold their models.
|
|
251
|
+
completeCex.incRef();
|
|
252
|
+
// Try to find instances from the instantiation sets first.
|
|
253
|
+
this.modelFinder.restrictSksToInstSet(quantifier, sks, (expr) => aux.assert(expr));
|
|
254
|
+
let numNewInstances = 0;
|
|
255
|
+
for (;;) {
|
|
256
|
+
const cex = aux.check();
|
|
257
|
+
if (typeof cex === "string") break;
|
|
258
|
+
cex.incRef();
|
|
259
|
+
const isAdded = this.addInstance(quantifier, cex, sks, true);
|
|
260
|
+
if (isAdded) numNewInstances++;
|
|
261
|
+
if (isAdded && numNewInstances < this.maxCexs) this.addBlockingClause(aux, cex, sks);
|
|
262
|
+
cex.decRef();
|
|
263
|
+
if (!isAdded || numNewInstances >= this.maxCexs) break;
|
|
264
|
+
}
|
|
265
|
+
if (numNewInstances === 0) this.addInstance(quantifier, completeCex, sks, false);
|
|
266
|
+
completeCex.decRef();
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// add_blocking_clause: diseqs and blocking_clause hold their terms until it is asserted.
|
|
271
|
+
private addBlockingClause(aux: AuxContext, cex: ProtoModel, sks: number[]) {
|
|
272
|
+
const manager = this.manager;
|
|
273
|
+
const diseqs = new RefVector(manager);
|
|
274
|
+
for (const sk of sks)
|
|
275
|
+
diseqs.push(manager.mkNot(manager.mkEq(sk, cex.getSomeConstInterp(manager.decls[sk]))));
|
|
276
|
+
const blockingClause = manager.mkApp(OP_OR, diseqs.items, SORT_BOOL);
|
|
277
|
+
manager.incRef(blockingClause);
|
|
278
|
+
aux.assert(blockingClause);
|
|
279
|
+
manager.decRef(blockingClause);
|
|
280
|
+
diseqs.reset();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private checkQuantifiers(model: ProtoModel) {
|
|
284
|
+
const context = this.context;
|
|
285
|
+
let isFoundRelevant = false;
|
|
286
|
+
let numFailures = 0;
|
|
287
|
+
for (const quantifier of this.qmanager.quantifiers) {
|
|
288
|
+
// Z3's resource limit cancels the checks, and the search gives up.
|
|
289
|
+
if (context.checkCanceled()) return { isFoundRelevant, numFailures: numFailures + 1 };
|
|
290
|
+
if (
|
|
291
|
+
!context.isRelevant(quantifier) ||
|
|
292
|
+
context.getAssignment(context.getLiteral(quantifier)) !== L_TRUE
|
|
293
|
+
)
|
|
294
|
+
continue;
|
|
295
|
+
isFoundRelevant = true;
|
|
296
|
+
if (!this.checkQuantifier(quantifier, model)) numFailures++;
|
|
297
|
+
}
|
|
298
|
+
return { isFoundRelevant, numFailures };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// check(md, root2value): whether the model satisfies every relevant quantifier.
|
|
302
|
+
check(model: ProtoModel, root2value: Map<Enode, number>) {
|
|
303
|
+
this.root2value = root2value;
|
|
304
|
+
if (this.qmanager.quantifiers.length === 0) return true;
|
|
305
|
+
if (this.iterationIndex >= MBQI_MAX_ITERATIONS) return false;
|
|
306
|
+
this.value2expr = null;
|
|
307
|
+
model.compress();
|
|
308
|
+
const first = this.checkQuantifiers(model);
|
|
309
|
+
if (first.isFoundRelevant) this.iterationIndex++;
|
|
310
|
+
this.maxCexs += MBQI_MAX_CEXS;
|
|
311
|
+
let numFailures = first.numFailures;
|
|
312
|
+
if (numFailures === 0 && !this.context.validateModel(model))
|
|
313
|
+
numFailures = 1 + this.checkQuantifiers(model).numFailures;
|
|
314
|
+
if (numFailures === 0) model.cleanup();
|
|
315
|
+
return numFailures === 0;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
initSearchEh() {
|
|
319
|
+
this.maxCexs = MBQI_MAX_CEXS;
|
|
320
|
+
this.iterationIndex = 0;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
hasNewInstances() {
|
|
324
|
+
return this.newInstances.length > 0;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// restart_eh: assert_new_instances.
|
|
328
|
+
restartEh() {
|
|
329
|
+
const context = this.context;
|
|
330
|
+
for (const instance of this.newInstances) {
|
|
331
|
+
if (!context.isBInternalized(instance.quantifier)) continue;
|
|
332
|
+
const numDecls = this.manager.getQuantifier(instance.quantifier).sorts.length;
|
|
333
|
+
const bindings = this.pinnedExprs.items.slice(instance.offset, instance.offset + numDecls);
|
|
334
|
+
const nodes = bindings.map((binding) => {
|
|
335
|
+
if (!context.isEInternalized(binding))
|
|
336
|
+
context.internalize(binding, false, instance.generation);
|
|
337
|
+
const node = context.getEnode(binding);
|
|
338
|
+
if (node === undefined) throw new SmtInvariantError("the binding was not internalized");
|
|
339
|
+
return node;
|
|
340
|
+
});
|
|
341
|
+
this.qmanager.addInstance(instance.quantifier, nodes, instance.generation);
|
|
342
|
+
}
|
|
343
|
+
// reset_new_instances.
|
|
344
|
+
this.pinnedExprs.reset();
|
|
345
|
+
this.newInstances = [];
|
|
346
|
+
}
|
|
347
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// Ported from Z3 (src/model/model_evaluator.cpp), MIT license, see z3-license.txt. Evaluates an
|
|
2
|
+
// expression in a model with rewriter_tpl and evaluator_cfg: uninterpreted symbols take their
|
|
3
|
+
// interpretations, the interpretation of a function is expanded like a macro, and the rewriters fold
|
|
4
|
+
// the interpreted operators. With model completion, a symbol without an interpretation gets some
|
|
5
|
+
// value, so a ground expression always evaluates to a value. The cache persists between evaluations
|
|
6
|
+
// until the model completion flag changes.
|
|
7
|
+
import {
|
|
8
|
+
FAMILY_ARITH,
|
|
9
|
+
FAMILY_USER,
|
|
10
|
+
KIND_APP,
|
|
11
|
+
KIND_QUANTIFIER,
|
|
12
|
+
OP_AND,
|
|
13
|
+
OP_DIV,
|
|
14
|
+
OP_DIV0,
|
|
15
|
+
OP_EQ,
|
|
16
|
+
OP_IDIV,
|
|
17
|
+
OP_IDIV0,
|
|
18
|
+
OP_ITE,
|
|
19
|
+
OP_LE,
|
|
20
|
+
OP_MOD,
|
|
21
|
+
OP_MOD0,
|
|
22
|
+
SORT_BOOL,
|
|
23
|
+
} from "./ast";
|
|
24
|
+
import type { AstManager } from "./ast";
|
|
25
|
+
import { FuncInterp } from "./proto-model";
|
|
26
|
+
import type { ProtoModel } from "./proto-model";
|
|
27
|
+
import { Rewriter, TACTIC_REWRITER_OPTIONS } from "./rewriter";
|
|
28
|
+
import { RewriterTpl } from "./rewriter-tpl";
|
|
29
|
+
import type { Rewrite } from "./rewriter-tpl";
|
|
30
|
+
import { Rational } from "./rational";
|
|
31
|
+
import { VarSubst } from "./var-subst";
|
|
32
|
+
|
|
33
|
+
// The division by zero that a division stands for when its divisor is zero.
|
|
34
|
+
const BY_ZERO_OPS = new Map([
|
|
35
|
+
[OP_DIV, OP_DIV0],
|
|
36
|
+
[OP_IDIV, OP_IDIV0],
|
|
37
|
+
[OP_MOD, OP_MOD0],
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
// A th_rewriter with the default parameters.
|
|
41
|
+
export class ModelEvaluator extends RewriterTpl {
|
|
42
|
+
// m_b_rw and m_a_rw, whose reductions evaluator_cfg applies.
|
|
43
|
+
private rewriter: Rewriter;
|
|
44
|
+
private isModelCompletion = false;
|
|
45
|
+
// The arguments of a reduced arithmetic term, reused across evaluations.
|
|
46
|
+
private reducedArgs: number[] = [];
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
manager: AstManager,
|
|
50
|
+
private model: ProtoModel,
|
|
51
|
+
) {
|
|
52
|
+
super(manager);
|
|
53
|
+
this.rewriter = new Rewriter(manager, TACTIC_REWRITER_OPTIONS);
|
|
54
|
+
this.rewriter.releaseResult = () => this.setLastResult(-1);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// set_model_completion: the cache is reset when the flag changes.
|
|
58
|
+
setModelCompletion(isModelCompletion: boolean) {
|
|
59
|
+
if (this.isModelCompletion === isModelCompletion) return;
|
|
60
|
+
this.reset();
|
|
61
|
+
this.isModelCompletion = isModelCompletion;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
mkNested() {
|
|
65
|
+
const nested = new ModelEvaluator(this.manager, this.model);
|
|
66
|
+
nested.isModelCompletion = this.isModelCompletion;
|
|
67
|
+
return nested;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
reduceApp(decl: number, args: number[], sort: number) {
|
|
71
|
+
const manager = this.manager;
|
|
72
|
+
const outer = manager.beginTemporaries();
|
|
73
|
+
const result = this.reduceAppCore(decl, args, sort);
|
|
74
|
+
if (result !== -1) manager.incRef(typeof result === "number" ? result : result.expr);
|
|
75
|
+
manager.endTemporaries(outer);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// evaluator_cfg::reduce_app_core. -1 is BR_FAILED.
|
|
80
|
+
private reduceAppCore(decl: number, args: number[], sort: number): number | Rewrite {
|
|
81
|
+
const manager = this.manager;
|
|
82
|
+
const model = this.model;
|
|
83
|
+
const family = manager.funcDecls[decl].family;
|
|
84
|
+
if (family === FAMILY_USER) {
|
|
85
|
+
if (args.length > 0) {
|
|
86
|
+
const result = this.evaluate(decl, args);
|
|
87
|
+
return result === undefined ? -1 : { expr: result, depth: 1 };
|
|
88
|
+
}
|
|
89
|
+
const value = model.constInterps.get(decl);
|
|
90
|
+
if (value !== undefined)
|
|
91
|
+
return this.containsRedex(value) ? { expr: value, depth: Infinity } : value;
|
|
92
|
+
if (!this.isModelCompletion) return -1;
|
|
93
|
+
const some = model.getSomeValue(sort);
|
|
94
|
+
model.registerConst(decl, some);
|
|
95
|
+
return some;
|
|
96
|
+
}
|
|
97
|
+
if (manager.funcDecls[decl].op === OP_AND) {
|
|
98
|
+
const reduced = this.mkArithAndCore(args);
|
|
99
|
+
if (reduced !== undefined) return reduced;
|
|
100
|
+
}
|
|
101
|
+
const rewritten = this.rewriter.reduceAppCore(decl, args);
|
|
102
|
+
const reduced =
|
|
103
|
+
rewritten === -1 && family === FAMILY_ARITH
|
|
104
|
+
? this.evaluatePartialTheoryFunc(decl, args, sort)
|
|
105
|
+
: rewritten;
|
|
106
|
+
if (
|
|
107
|
+
family !== FAMILY_ARITH ||
|
|
108
|
+
typeof reduced !== "number" ||
|
|
109
|
+
reduced === -1 ||
|
|
110
|
+
manager.kinds[reduced] !== KIND_APP
|
|
111
|
+
)
|
|
112
|
+
return reduced;
|
|
113
|
+
const reducedArgs = this.reducedArgs;
|
|
114
|
+
reducedArgs.length = 0;
|
|
115
|
+
manager.pushArgs(reduced, reducedArgs);
|
|
116
|
+
const result = this.evaluate(manager.decls[reduced], reducedArgs);
|
|
117
|
+
return result === undefined ? reduced : { expr: result, depth: 1 };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// evaluate and eval_fi: the result of the entry for the arguments when they are values. Otherwise
|
|
121
|
+
// get_macro expands the interpretation.
|
|
122
|
+
private evaluate(decl: number, args: number[]) {
|
|
123
|
+
const manager = this.manager;
|
|
124
|
+
const interp = this.model.funcInterps.get(decl);
|
|
125
|
+
if (
|
|
126
|
+
interp === undefined ||
|
|
127
|
+
interp.entries.length === 0 ||
|
|
128
|
+
!args.every((arg) => manager.isUniqueValue(arg))
|
|
129
|
+
)
|
|
130
|
+
return undefined;
|
|
131
|
+
return interp.getEntry(args)?.result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// evaluate_partial_theory_func: div0, idiv0 and mod0 take their interpretations, or 0 without
|
|
135
|
+
// one, and so does a division by zero. A division by a term is its interpretation when the term
|
|
136
|
+
// is zero.
|
|
137
|
+
private evaluatePartialTheoryFunc(decl: number, args: number[], sort: number): number | Rewrite {
|
|
138
|
+
const manager = this.manager;
|
|
139
|
+
const model = this.model;
|
|
140
|
+
const op = manager.funcDecls[decl].op;
|
|
141
|
+
const byZero = BY_ZERO_OPS.get(op);
|
|
142
|
+
let interp = model.funcInterps.get(decl);
|
|
143
|
+
if (interp === undefined && byZero !== undefined && !manager.isNumeral(args[1])) {
|
|
144
|
+
const definition = model.funcInterps.get(byZero)?.getInterp();
|
|
145
|
+
if (definition === undefined || definition === null) return -1;
|
|
146
|
+
const result = this.substitute(definition, args);
|
|
147
|
+
const zero = manager.mkNumeralOfSort(Rational.zero, manager.sorts[args[1]]);
|
|
148
|
+
return manager.mkApp(
|
|
149
|
+
OP_ITE,
|
|
150
|
+
[manager.mkEq(zero, args[1]), result, manager.mkApp(decl, args, sort)],
|
|
151
|
+
sort,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
if (interp === undefined) {
|
|
155
|
+
const isByZero = byZero !== undefined && manager.getRationalValue(args[1]).isZero();
|
|
156
|
+
if (!isByZero && op !== OP_DIV0 && op !== OP_IDIV0 && op !== OP_MOD0) return -1;
|
|
157
|
+
interp = isByZero ? model.funcInterps.get(byZero) : undefined;
|
|
158
|
+
if (interp === undefined) return manager.mkNumeralOfSort(Rational.zero, sort);
|
|
159
|
+
}
|
|
160
|
+
if (interp.isPartial()) interp.setElse(model.getSomeValue(sort));
|
|
161
|
+
return { expr: this.substitute(interp.getInterp() as number, args), depth: Infinity };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// var_subst without the standard order, whose result the temporaries hold.
|
|
165
|
+
private substitute(expr: number, args: number[]) {
|
|
166
|
+
const subst = new VarSubst(this.manager, false);
|
|
167
|
+
const result = subst.apply(expr, args);
|
|
168
|
+
subst.release();
|
|
169
|
+
this.manager.releaseLater(result);
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// contains_redex: whether a constant's value has an equality to evaluate.
|
|
174
|
+
private containsRedex(expr: number) {
|
|
175
|
+
const manager = this.manager;
|
|
176
|
+
if (manager.kinds[expr] !== KIND_APP || manager.getNumArgs(expr) === 0) return false;
|
|
177
|
+
const visited = new Set<number>();
|
|
178
|
+
const todo = [expr];
|
|
179
|
+
while (todo.length > 0) {
|
|
180
|
+
const current = todo.pop() as number;
|
|
181
|
+
if (visited.has(current)) continue;
|
|
182
|
+
visited.add(current);
|
|
183
|
+
if (manager.getOp(current) === OP_EQ) return true;
|
|
184
|
+
if (manager.kinds[current] === KIND_APP) manager.pushArgs(current, todo);
|
|
185
|
+
else if (manager.kinds[current] === KIND_QUANTIFIER) {
|
|
186
|
+
const data = manager.getQuantifier(current);
|
|
187
|
+
todo.push(data.body, ...data.patterns);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// arith_rewriter::mk_and_core: of lower bounds (<= a y) and (<= b y) on the same term, the
|
|
194
|
+
// conjunction keeps the greatest. Z3 compares each bound with the first rather than with the
|
|
195
|
+
// greatest so far, which drops the greatest when a smaller one follows it. This port keeps the
|
|
196
|
+
// greatest.
|
|
197
|
+
private mkArithAndCore(args: number[]): Rewrite | undefined {
|
|
198
|
+
const manager = this.manager;
|
|
199
|
+
if (args.length <= 1 || manager.getOp(args[0]) !== OP_LE) return undefined;
|
|
200
|
+
const lower = manager.getArg(args[0], 0);
|
|
201
|
+
const term = manager.getArg(args[0], 1);
|
|
202
|
+
if (!manager.isNumeral(lower)) return undefined;
|
|
203
|
+
let bound = args[0];
|
|
204
|
+
const rest: number[] = [];
|
|
205
|
+
for (const arg of args.slice(1)) {
|
|
206
|
+
const isBound =
|
|
207
|
+
manager.getOp(arg) === OP_LE &&
|
|
208
|
+
manager.getArg(arg, 1) === term &&
|
|
209
|
+
manager.isNumeral(manager.getArg(arg, 0));
|
|
210
|
+
if (!isBound) rest.push(arg);
|
|
211
|
+
else if (
|
|
212
|
+
manager
|
|
213
|
+
.getRationalValue(manager.getArg(arg, 0))
|
|
214
|
+
.compare(manager.getRationalValue(manager.getArg(bound, 0))) > 0
|
|
215
|
+
)
|
|
216
|
+
bound = arg;
|
|
217
|
+
}
|
|
218
|
+
if (rest.length === args.length - 1) return undefined;
|
|
219
|
+
rest.push(bound);
|
|
220
|
+
return { expr: manager.mkApp(OP_AND, rest, SORT_BOOL), depth: 1 };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// get_macro: the interpretation of an uninterpreted function. With model completion, a partial
|
|
224
|
+
// interpretation gets an else value, and a function without one gets a constant interpretation.
|
|
225
|
+
getMacro(decl: number) {
|
|
226
|
+
const manager = this.manager;
|
|
227
|
+
const model = this.model;
|
|
228
|
+
const info = manager.funcDecls[decl];
|
|
229
|
+
const interp = model.funcInterps.get(decl);
|
|
230
|
+
if (interp !== undefined) {
|
|
231
|
+
if (interp.isPartial()) {
|
|
232
|
+
if (!this.isModelCompletion) return -1;
|
|
233
|
+
interp.setElse(model.getSomeValue(info.range));
|
|
234
|
+
}
|
|
235
|
+
return interp.getInterp() as number;
|
|
236
|
+
}
|
|
237
|
+
if (!this.isModelCompletion || info.family !== FAMILY_USER) return -1;
|
|
238
|
+
const some = model.getSomeValue(info.range);
|
|
239
|
+
const created = new FuncInterp(manager, info.domain.length);
|
|
240
|
+
created.setElse(some);
|
|
241
|
+
model.registerFunc(decl, created);
|
|
242
|
+
return some;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
isReducingMacro() {
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// evaluator_cfg::reduce_quantifier: a th_rewriter of its own reduces the quantifier.
|
|
250
|
+
reduceQuantifier(expr: number, body: number, patterns: number[]) {
|
|
251
|
+
const rewriter = new Rewriter(this.manager, TACTIC_REWRITER_OPTIONS);
|
|
252
|
+
const result = rewriter.reduceQuantifier(expr, body, patterns);
|
|
253
|
+
rewriter.release();
|
|
254
|
+
return result;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ~imp: evaluator_cfg goes before the caches of rewriter_tpl.
|
|
258
|
+
release() {
|
|
259
|
+
this.rewriter.release();
|
|
260
|
+
super.release();
|
|
261
|
+
}
|
|
262
|
+
}
|