mirror of
https://github.com/Z3Prover/z3
synced 2025-08-22 11:07:51 +00:00
separate egraph functionality
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
d183ac23d0
commit
8eea2488e2
9 changed files with 195 additions and 90 deletions
|
@ -389,11 +389,11 @@ namespace polysat {
|
|||
}
|
||||
}
|
||||
|
||||
void core::get_bitvector_suffixes(pvar v, pvar_vector& out) {
|
||||
void core::get_bitvector_suffixes(pvar v, justified_slices& out) {
|
||||
s.get_bitvector_suffixes(v, out);
|
||||
}
|
||||
|
||||
void core::get_fixed_bits(pvar v, svector<justified_fixed_bits>& fixed_bits) {
|
||||
void core::get_fixed_bits(pvar v, justified_fixed_bits& fixed_bits) {
|
||||
s.get_fixed_bits(v, fixed_bits);
|
||||
}
|
||||
|
||||
|
|
|
@ -83,8 +83,8 @@ namespace polysat {
|
|||
void propagate_unsat_core();
|
||||
void propagate(constraint_id id, signed_constraint& sc, lbool value, dependency const& d);
|
||||
|
||||
void get_bitvector_suffixes(pvar v, pvar_vector& out);
|
||||
void get_fixed_bits(pvar v, svector<justified_fixed_bits>& fixed_bits);
|
||||
void get_bitvector_suffixes(pvar v, justified_slices& out);
|
||||
void get_fixed_bits(pvar v, justified_fixed_bits& fixed_bits);
|
||||
bool inconsistent() const;
|
||||
|
||||
void add_watch(unsigned idx, unsigned var);
|
||||
|
|
|
@ -28,25 +28,31 @@ namespace polysat {
|
|||
};
|
||||
|
||||
using pvar_vector = unsigned_vector;
|
||||
using theory_var_pair = std::pair<theory_var, theory_var>;
|
||||
using theory_var_pairs = svector<theory_var_pair>;
|
||||
inline const pvar null_var = UINT_MAX;
|
||||
|
||||
class signed_constraint;
|
||||
|
||||
|
||||
class dependency {
|
||||
struct axiom_t {};
|
||||
std::variant<axiom_t, sat::literal, std::pair<theory_var, theory_var>> m_data;
|
||||
std::variant<axiom_t, sat::literal, theory_var_pair, theory_var_pairs> m_data;
|
||||
unsigned m_level;
|
||||
dependency(): m_data(axiom_t()), m_level(0) {}
|
||||
public:
|
||||
dependency(sat::literal lit, unsigned level) : m_data(lit), m_level(level) {}
|
||||
dependency(theory_var v1, theory_var v2, unsigned level) : m_data(std::make_pair(v1, v2)), m_level(level) {}
|
||||
dependency(theory_var_pairs& j, unsigned level) : m_data(j), m_level(level) {}
|
||||
static dependency axiom() { return dependency(); }
|
||||
bool is_null() const { return is_literal() && *std::get_if<sat::literal>(&m_data) == sat::null_literal; }
|
||||
bool is_axiom() const { return std::holds_alternative<axiom_t>(m_data); }
|
||||
bool is_eq() const { return std::holds_alternative<std::pair<theory_var, theory_var>>(m_data); }
|
||||
bool is_eqs() const { return std::holds_alternative<theory_var_pairs>(m_data); }
|
||||
bool is_eq() const { return std::holds_alternative<theory_var_pair>(m_data); }
|
||||
bool is_literal() const { return std::holds_alternative<sat::literal>(m_data); }
|
||||
sat::literal literal() const { SASSERT(is_literal()); return *std::get_if<sat::literal>(&m_data); }
|
||||
std::pair<theory_var, theory_var> eq() const { SASSERT(!is_literal()); return *std::get_if<std::pair<theory_var, theory_var>>(&m_data); }
|
||||
theory_var_pair eq() const { SASSERT(!is_literal()); return *std::get_if<theory_var_pair>(&m_data); }
|
||||
theory_var_pairs const& eqs() const { SASSERT(!is_literal()); return *std::get_if<theory_var_pairs>(&m_data); }
|
||||
unsigned level() const { return m_level; }
|
||||
void set_level(unsigned level) { m_level = level; }
|
||||
dependency operator~() const { SASSERT(is_literal()); return dependency(~literal(), level()); }
|
||||
|
@ -61,8 +67,14 @@ namespace polysat {
|
|||
return out << "axiom@" << d.level();
|
||||
else if (d.is_literal())
|
||||
return out << d.literal() << "@" << d.level();
|
||||
else
|
||||
else if (d.is_eq())
|
||||
return out << "v" << d.eq().first << " == v" << d.eq().second << "@" << d.level();
|
||||
else {
|
||||
char const* sep = "";
|
||||
for (auto [v1, v2] : d.eqs())
|
||||
out << sep << "v" << d.eq().first << " == v" << d.eq().second, sep = ", ";
|
||||
return out << " @" << d.level();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -78,7 +90,7 @@ namespace polysat {
|
|||
fixed_bits(unsigned hi, unsigned lo, rational value) : hi(hi), lo(lo), value(value) {}
|
||||
};
|
||||
|
||||
struct justified_fixed_bits : public fixed_bits, public dependency {};
|
||||
using justified_fixed_bits = vector<std::pair<fixed_bits, dependency>>;
|
||||
|
||||
using dependency_vector = vector<dependency>;
|
||||
using constraint_or_dependency = std::variant<signed_constraint, dependency>;
|
||||
|
@ -88,7 +100,8 @@ namespace polysat {
|
|||
using core_vector = std::initializer_list<constraint_or_dependency>;
|
||||
using constraint_id_vector = svector<constraint_id>;
|
||||
using constraint_id_list = std::initializer_list<constraint_id>;
|
||||
|
||||
using justified_slices = vector<std::pair<pvar, dependency>>;
|
||||
using eq_justification = svector<std::pair<theory_var, theory_var>>;
|
||||
|
||||
//
|
||||
// The interface that PolySAT uses to the SAT/SMT solver.
|
||||
|
@ -104,8 +117,8 @@ namespace polysat {
|
|||
virtual void propagate(dependency const& d, bool sign, constraint_id_vector const& deps) = 0;
|
||||
virtual trail_stack& trail() = 0;
|
||||
virtual bool inconsistent() const = 0;
|
||||
virtual void get_bitvector_suffixes(pvar v, pvar_vector& out) = 0;
|
||||
virtual void get_fixed_bits(pvar v, svector<justified_fixed_bits>& fixed_bits) = 0;
|
||||
virtual void get_bitvector_suffixes(pvar v, justified_slices& out) = 0;
|
||||
virtual void get_fixed_bits(pvar v, justified_fixed_bits& fixed_bits) = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -103,15 +103,15 @@ namespace polysat {
|
|||
return l_false; // conflict already added
|
||||
#endif
|
||||
|
||||
pvar_vector overlaps;
|
||||
justified_slices overlaps;
|
||||
c.get_bitvector_suffixes(v, overlaps);
|
||||
std::sort(overlaps.begin(), overlaps.end(), [&](pvar x, pvar y) { return c.size(x) > c.size(y); });
|
||||
std::sort(overlaps.begin(), overlaps.end(), [&](auto const& x, auto const& y) { return c.size(x.first) > c.size(y.first); });
|
||||
|
||||
uint_set widths_set;
|
||||
// max size should always be present, regardless of whether we have intervals there (to make sure all fixed bits are considered)
|
||||
widths_set.insert(c.size(v));
|
||||
|
||||
for (pvar v : overlaps)
|
||||
for (auto const& [v, j] : overlaps)
|
||||
for (layer const& l : m_units[v].get_layers())
|
||||
widths_set.insert(l.bit_width);
|
||||
|
||||
|
@ -147,7 +147,7 @@ namespace polysat {
|
|||
lbool viable::find_on_layers(
|
||||
pvar const v,
|
||||
unsigned_vector const& widths,
|
||||
pvar_vector const& overlaps,
|
||||
justified_slices const& overlaps,
|
||||
fixed_bits_info const& fbi,
|
||||
rational const& to_cover_lo,
|
||||
rational const& to_cover_hi,
|
||||
|
@ -176,7 +176,7 @@ namespace polysat {
|
|||
// however, we probably should rotate to avoid getting stuck in refinement loop on a 'bad' constraint
|
||||
bool refined = false;
|
||||
for (unsigned i = overlaps.size(); i-- > 0; ) {
|
||||
pvar x = overlaps[i];
|
||||
pvar x = overlaps[i].first;
|
||||
rational const& mod_value = c.var2pdd(x).two_to_N();
|
||||
rational x_val = mod(val, mod_value);
|
||||
if (!refine_viable(x, x_val)) {
|
||||
|
@ -201,7 +201,7 @@ namespace polysat {
|
|||
pvar const v,
|
||||
unsigned const w_idx,
|
||||
unsigned_vector const& widths,
|
||||
pvar_vector const& overlaps,
|
||||
justified_slices const& overlaps,
|
||||
fixed_bits_info const& fbi,
|
||||
rational const& to_cover_lo,
|
||||
rational const& to_cover_hi,
|
||||
|
@ -240,7 +240,7 @@ namespace polysat {
|
|||
|
||||
// find relevant interval lists
|
||||
svector<entry_cursor> ecs;
|
||||
for (pvar x : overlaps) {
|
||||
for (auto const& [x, j] : overlaps) {
|
||||
if (c.size(x) < w) // note that overlaps are sorted by variable size descending
|
||||
break;
|
||||
if (entry* e = m_units[x].get_entries(w)) {
|
||||
|
@ -614,17 +614,17 @@ namespace polysat {
|
|||
out_fbi.reset(v_sz);
|
||||
auto& [fixed, just_src, just_side_cond, just_slice] = out_fbi;
|
||||
|
||||
svector<justified_fixed_bits> fbs;
|
||||
justified_fixed_bits fbs;
|
||||
c.get_fixed_bits(v, fbs);
|
||||
|
||||
for (auto const& fb : fbs) {
|
||||
for (auto const& [fb, d] : fbs) {
|
||||
LOG("slicing fixed bits: v" << v << "[" << fb.hi << ":" << fb.lo << "] = " << fb.value);
|
||||
for (unsigned i = fb.lo; i <= fb.hi; ++i) {
|
||||
SASSERT(out_fbi.just_src[i].empty()); // since we don't get overlapping ranges from collect_fixed.
|
||||
SASSERT(out_fbi.just_side_cond[i].empty());
|
||||
SASSERT(out_fbi.just_slicing[i].empty());
|
||||
out_fbi.fixed[i] = to_lbool(fb.value.get_bit(i - fb.lo));
|
||||
out_fbi.just_slicing[i].push_back(fb);
|
||||
out_fbi.just_slicing[i].push_back({ fb, d });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,7 @@ namespace polysat {
|
|||
svector<lbool> fixed;
|
||||
vector<vector<signed_constraint>> just_src;
|
||||
vector<vector<signed_constraint>> just_side_cond;
|
||||
vector<svector<justified_fixed_bits>> just_slicing;
|
||||
vector<justified_fixed_bits> just_slicing;
|
||||
|
||||
bool is_empty() const {
|
||||
SASSERT_EQ(fixed.empty(), just_src.empty());
|
||||
|
@ -186,7 +186,7 @@ namespace polysat {
|
|||
lbool find_on_layers(
|
||||
pvar v,
|
||||
unsigned_vector const& widths,
|
||||
pvar_vector const& overlaps,
|
||||
justified_slices const& overlaps,
|
||||
fixed_bits_info const& fbi,
|
||||
rational const& to_cover_lo,
|
||||
rational const& to_cover_hi,
|
||||
|
@ -196,7 +196,7 @@ namespace polysat {
|
|||
pvar v,
|
||||
unsigned w_idx,
|
||||
unsigned_vector const& widths,
|
||||
pvar_vector const& overlaps,
|
||||
justified_slices const& overlaps,
|
||||
fixed_bits_info const& fbi,
|
||||
rational const& to_cover_lo,
|
||||
rational const& to_cover_hi,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue