3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-22 22:03:39 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2019-12-26 14:31:05 -08:00
parent 2c6e6b1fdb
commit 914856b9ba
5 changed files with 216 additions and 63 deletions

View file

@ -23,13 +23,13 @@ Revision History:
namespace dd {
pdd_manager::pdd_manager(unsigned num_vars) {
pdd_manager::pdd_manager(unsigned num_vars, semantics s) {
m_spare_entry = nullptr;
m_max_num_nodes = 1 << 24; // up to 16M nodes
m_mark_level = 0;
m_disable_gc = false;
m_is_new_node = false;
m_mod2_semantics = false;
m_semantics = s;
// add dummy nodes for operations, and 0, 1 pdds.
for (unsigned i = 0; i < pdd_no_op; ++i) {
@ -61,7 +61,7 @@ namespace dd {
}
pdd pdd_manager::add(pdd const& a, pdd const& b) { return pdd(apply(a.root, b.root, pdd_add_op), this); }
pdd pdd_manager::sub(pdd const& a, pdd const& b) { pdd m(minus(b)); return pdd(apply(a.root, m.root, pdd_add_op), this); }
pdd pdd_manager::sub(pdd const& a, pdd const& b) { return pdd(apply(a.root, b.root, pdd_sub_op), this); }
pdd pdd_manager::mul(pdd const& a, pdd const& b) { return pdd(apply(a.root, b.root, pdd_mul_op), this); }
pdd pdd_manager::reduce(pdd const& a, pdd const& b) { return pdd(apply(a.root, b.root, pdd_reduce_op), this); }
pdd pdd_manager::mk_val(rational const& r) { return pdd(imk_val(r), this); }
@ -71,6 +71,8 @@ namespace dd {
pdd pdd_manager::one() { return pdd(one_pdd, this); }
pdd pdd_manager::mk_or(pdd const& p, pdd const& q) { return p + q - (p*q); }
pdd pdd_manager::mk_xor(pdd const& p, pdd const& q) { return (p*q*2) - p - q; }
pdd pdd_manager::mk_not(pdd const& p) { return 1 - p; }
pdd_manager::PDD pdd_manager::apply(PDD arg1, PDD arg2, pdd_op op) {
bool first = true;
@ -108,6 +110,11 @@ namespace dd {
pdd_manager::PDD pdd_manager::apply_rec(PDD p, PDD q, pdd_op op) {
switch (op) {
case pdd_sub_op:
if (is_zero(q)) return p;
if (is_val(p) && is_val(q)) return imk_val(val(p) - val(q));
if (m_semantics != mod2_e) break;
op = pdd_add_op;
case pdd_add_op:
if (is_zero(p)) return q;
if (is_zero(q)) return p;
@ -163,6 +170,35 @@ namespace dd {
npop = 1;
}
break;
case pdd_sub_op:
if (is_val(p)) {
push(apply_rec(p, lo(q), op));
r = make_node(level_q, read(1), hi(q));
npop = 1;
}
else if (is_val(q)) {
push(apply_rec(lo(p), q, op));
r = make_node(level_p, read(1), hi(p));
npop = 1;
}
else if (level_p == level_q) {
push(apply_rec(lo(p), lo(q), op));
push(apply_rec(hi(p), hi(q), op));
r = make_node(level_p, read(2), read(1));
}
else if (level_p > level_q) {
// x*hi(p) + (lo(p) - q)
push(apply_rec(lo(p), q, op));
r = make_node(level_p, read(1), hi(p));
npop = 1;
}
else {
// x*hi(q) + (p - lo(q))
push(apply_rec(p, lo(q), op));
r = make_node(level_q, read(1), hi(q));
npop = 1;
}
break;
case pdd_mul_op:
SASSERT(!is_val(p));
if (is_val(q)) {
@ -171,17 +207,18 @@ namespace dd {
r = make_node(level_p, read(2), read(1));
}
else if (level_p == level_q) {
if (m_mod2_semantics) {
if (m_semantics != free_e) {
//
// (xa+b)*(xc+d) mod2 == x(ac+bc+ad) + bd
// == x((a+b)(c+d)+bd) + bd
// (xa+b)*(xc+d) == x(ac+bc+ad) + bd
// == x((a+b)(c+d)-bd) + bd
// because x*x = x
//
push(apply_rec(lo(p), lo(q), pdd_mul_op));
unsigned bd = read(1);
push(apply_rec(hi(p), lo(p), pdd_add_op));
push(apply_rec(hi(q), lo(q), pdd_add_op));
push(apply_rec(read(1), read(2), pdd_mul_op));
push(apply_rec(read(1), bd, pdd_add_op));
push(apply_rec(read(1), bd, pdd_sub_op));
r = make_node(level_p, bd, read(1));
npop = 5;
}
@ -243,7 +280,7 @@ namespace dd {
}
pdd pdd_manager::minus(pdd const& a) {
if (m_mod2_semantics) {
if (m_semantics == mod2_e) {
return a;
}
bool first = true;
@ -264,7 +301,7 @@ namespace dd {
}
pdd_manager::PDD pdd_manager::minus_rec(PDD a) {
SASSERT(!m_mod2_semantics);
SASSERT(m_semantics != mod2_e);
if (is_zero(a)) return zero_pdd;
if (is_val(a)) return imk_val(-val(a));
op_entry* e1 = pop_entry(a, a, pdd_minus_op);
@ -358,7 +395,7 @@ namespace dd {
while (!is_val(x)) p.push_back(var(x)), x = hi(x);
pc = val(x);
qc = val(y);
if (!m_mod2_semantics && pc.is_int() && qc.is_int()) {
if (m_semantics != mod2_e && pc.is_int() && qc.is_int()) {
rational g = gcd(pc, qc);
pc /= g;
qc /= g;
@ -461,6 +498,17 @@ namespace dd {
return is_binary(p.root);
}
/**
Determine if p is a monomial.
*/
bool pdd_manager::is_monomial(PDD p) {
while (true) {
if (is_val(p)) return true;
if (!is_zero(lo(p))) return false;
p = hi(p);
}
}
/*
\brief determine if v occurs as a leaf variable.
*/
@ -522,7 +570,7 @@ namespace dd {
pdd_manager::PDD pdd_manager::imk_val(rational const& r) {
if (r.is_zero()) return zero_pdd;
if (r.is_one()) return one_pdd;
if (m_mod2_semantics) return imk_val(mod(r, rational(2)));
if (m_semantics == mod2_e) return imk_val(mod(r, rational(2)));
const_info info;
if (!m_mpq_table.find(r, info)) {
init_value(info, r);

View file

@ -41,6 +41,9 @@ namespace dd {
class pdd;
class pdd_manager {
public:
enum semantics { free_e, mod2_e, zero_one_vars_e };
private:
friend pdd;
typedef unsigned PDD;
@ -52,10 +55,11 @@ namespace dd {
enum pdd_op {
pdd_add_op = 2,
pdd_minus_op = 3,
pdd_mul_op = 4,
pdd_reduce_op = 5,
pdd_no_op = 6
pdd_sub_op = 3,
pdd_minus_op = 4,
pdd_mul_op = 5,
pdd_reduce_op = 6,
pdd_no_op = 7
};
struct node {
@ -151,7 +155,7 @@ namespace dd {
bool m_disable_gc;
bool m_is_new_node;
unsigned m_max_num_nodes;
bool m_mod2_semantics;
semantics m_semantics;
unsigned_vector m_free_vars;
unsigned_vector m_free_values;
rational m_freeze_value;
@ -230,10 +234,9 @@ namespace dd {
struct mem_out {};
pdd_manager(unsigned nodes);
pdd_manager(unsigned nodes, semantics s = free_e);
~pdd_manager();
void set_mod2_semantics() { m_mod2_semantics = true; }
void set_max_num_nodes(unsigned n) { m_max_num_nodes = n; }
void set_level2var(unsigned_vector const& level2var);
unsigned_vector const& get_level2var() const { return m_level2var; }
@ -249,6 +252,8 @@ namespace dd {
pdd mul(pdd const& a, pdd const& b);
pdd mul(rational const& c, pdd const& b);
pdd mk_or(pdd const& p, pdd const& q);
pdd mk_xor(pdd const& p, pdd const& q);
pdd mk_not(pdd const& p);
pdd reduce(pdd const& a, pdd const& b);
bool is_linear(PDD p);
@ -257,6 +262,8 @@ namespace dd {
bool is_binary(PDD p);
bool is_binary(pdd const& p);
bool is_monomial(PDD p);
// create an spoly r if leading monomials of a and b overlap
bool try_spoly(pdd const& a, pdd const& b, pdd& r);
@ -293,22 +300,28 @@ namespace dd {
bool is_zero() const { return m.is_zero(root); }
bool is_linear() const { return m.is_linear(root); }
bool is_binary() const { return m.is_binary(root); }
bool is_monomial() const { return m.is_monomial(root); }
bool var_is_leaf(unsigned v) const { return m.var_is_leaf(root, v); }
pdd minus() const { return m.minus(*this); }
pdd operator-() const { return m.minus(*this); }
pdd operator+(pdd const& other) const { return m.add(*this, other); }
pdd operator-(pdd const& other) const { return m.sub(*this, other); }
pdd operator*(pdd const& other) const { return m.mul(*this, other); }
pdd operator&(pdd const& other) const { return m.mul(*this, other); }
pdd operator|(pdd const& other) const { return m.mk_or(*this, other); }
pdd operator^(pdd const& other) const { return m.mk_xor(*this, other); }
pdd operator*(rational const& other) const { return m.mul(other, *this); }
pdd operator+(rational const& other) const { return m.add(other, *this); }
pdd operator|(pdd const& other) const { return m.mk_or(*this, other); }
pdd operator~() const { return m.mk_not(*this); }
pdd rev_sub(rational const& r) const { return m.sub(m.mk_val(r), *this); }
pdd reduce(pdd const& other) const { return m.reduce(*this, other); }
bool different_leading_term(pdd const& other) const { return m.different_leading_term(*this, other); }
std::ostream& display(std::ostream& out) const { return m.display(out, *this); }
bool operator==(pdd const& other) const { return root == other.root; }
bool operator!=(pdd const& other) const { return root != other.root; }
bool operator<(pdd const& b) const { return m.lt(*this, b); }
bool operator<(pdd const& other) const { return m.lt(*this, other); }
unsigned dag_size() const { return m.dag_size(*this); }
double tree_size() const { return m.tree_size(*this); }
@ -323,16 +336,17 @@ namespace dd {
inline pdd operator+(int x, pdd const& b) { return b + rational(x); }
inline pdd operator+(pdd const& b, int x) { return b + rational(x); }
inline pdd operator-(rational const& r, pdd const& b) { return r + b.minus(); }
inline pdd operator-(rational const& r, pdd const& b) { return b.rev_sub(r); }
inline pdd operator-(int x, pdd const& b) { return rational(x) - b; }
inline pdd operator-(pdd const& b, int x) { return b + (-rational(x)); }
inline pdd& operator&=(pdd & p, pdd const& q) { p = p & q; return p; }
inline pdd& operator^=(pdd & p, pdd const& q) { p = p ^ q; return p; }
inline pdd& operator*=(pdd & p, pdd const& q) { p = p * q; return p; }
inline pdd& operator|=(pdd & p, pdd const& q) { p = p | q; return p; }
inline pdd& operator-=(pdd & p, pdd const& q) { p = p - q; return p; }
inline pdd& operator+=(pdd & p, pdd const& q) { p = p + q; return p; }
std::ostream& operator<<(std::ostream& out, pdd const& b);
}