3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-08-05 02:40:24 +00:00

running updates to bv_solver (#4674)

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* dbg

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* bv

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* drat and fresh

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* move ackerman functionality

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* debugability

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* towards debugability

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* missing file

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* na

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>

* remove csp

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2020-09-07 20:35:32 -07:00 committed by GitHub
parent 4d1a2a2784
commit d02b0cde7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
63 changed files with 3060 additions and 3095 deletions

View file

@ -6,10 +6,10 @@ Module Name:
sat_drat.cpp
Abstract:
Produce DRAT proofs.
Check them using a very simple forward checker
Check them using a very simple forward checker
that interacts with external plugins.
Author:
@ -24,19 +24,18 @@ Notes:
namespace sat {
drat::drat(solver& s):
drat::drat(solver& s) :
s(s),
m_out(nullptr),
m_bout(nullptr),
m_inconsistent(false),
m_num_add(0),
m_num_del(0),
m_check_unsat(false),
m_check_sat(false),
m_check(false),
m_activity(false)
{
if (s.get_config().m_drat && s.get_config().m_drat_file != symbol()) {
if (s.get_config().m_drat && s.get_config().m_drat_file.is_non_empty_string()) {
std::cout << "DRAT " << s.get_config().m_drat_file << "\n";
auto mode = s.get_config().m_drat_binary ? (std::ios_base::binary | std::ios_base::out | std::ios_base::trunc) : std::ios_base::out;
m_out = alloc(std::ofstream, s.get_config().m_drat_file.str(), mode);
if (s.get_config().m_drat_binary) {
@ -61,7 +60,7 @@ namespace sat {
m_bout = nullptr;
}
void drat::updt_config() {
void drat::updt_config() {
m_check_unsat = s.get_config().m_drat_check_unsat;
m_check_sat = s.get_config().m_drat_check_sat;
m_check = m_check_unsat || m_check_sat;
@ -75,31 +74,41 @@ namespace sat {
out << "d";
else if (st.is_asserted())
out << "a";
else if (st.is_input())
out << "i";
if (!st.is_sat())
out << " " << m_theory[st.get_th()];
return out;
return out;
}
void drat::dump(unsigned n, literal const* c, status st) {
if (st.is_asserted() && !s.m_ext)
return;
if (m_activity && ((m_num_add % 1000) == 0))
if (m_activity && ((m_stats.m_num_add % 1000) == 0))
dump_activity();
char buffer[10000];
char digits[20]; // enough for storing unsigned
char* lastd = digits + sizeof(digits);
unsigned len = 0;
if (st.is_asserted()) {
buffer[len++] = 'a';
buffer[len++] = ' ';
buffer[len++] = ' ';
}
else if (st.is_deleted()) {
buffer[len++] = 'd';
buffer[len++] = ' ';
}
else if (st.is_input()) {
buffer[len++] = 'i';
buffer[len++] = ' ';
}
else if (st.is_redundant() && !st.is_sat()) {
buffer[len++] = 'r';
buffer[len++] = ' ';
}
if (!st.is_sat()) {
for (char ch : m_theory[st.get_th()])
@ -108,28 +117,28 @@ namespace sat {
}
for (unsigned i = 0; i < n; ++i) {
literal lit = c[i];
unsigned v = lit.var();
unsigned v = lit.var();
if (lit.sign()) buffer[len++] = '-';
char* d = lastd;
SASSERT(v > 0);
while (v > 0) {
while (v > 0) {
d--;
*d = (v % 10) + '0';
v /= 10;
SASSERT(d > digits);
}
SASSERT(len + lastd < sizeof(buffer) + d);
memcpy(buffer + len, d, lastd - d);
len += static_cast<unsigned>(lastd - d);
buffer[len++] = ' ';
if (len + 50 > sizeof(buffer)) {
m_out->write(buffer, len);
len = 0;
SASSERT(len + lastd < sizeof(buffer) + d);
memcpy(buffer + len, d, lastd - d);
len += static_cast<unsigned>(lastd - d);
buffer[len++] = ' ';
if (len + 50 > sizeof(buffer)) {
m_out->write(buffer, len);
len = 0;
}
}
buffer[len++] = '0';
buffer[len++] = '\n';
m_out->write(buffer, len);
}
buffer[len++] = '0';
buffer[len++] = '\n';
m_out->write(buffer, len);
}
@ -144,9 +153,9 @@ namespace sat {
void drat::bdump(unsigned n, literal const* c, status st) {
unsigned char ch = 0;
if (st.is_redundant())
ch = 'a';
ch = 'a';
else if (st.is_deleted())
ch = 'd';
ch = 'd';
else return;
char buffer[10000];
int len = 0;
@ -164,8 +173,7 @@ namespace sat {
m_bout->write(buffer, len);
len = 0;
}
}
while (v);
} while (v);
}
buffer[len++] = 0;
m_bout->write(buffer, len);
@ -188,7 +196,7 @@ namespace sat {
if (c[i] != last) {
out << c[i] << " ";
last = c[i];
}
}
}
out << "\n";
}
@ -198,7 +206,7 @@ namespace sat {
declare(l);
IF_VERBOSE(20, trace(verbose_stream(), 1, &l, st););
if (st.is_redundant()) {
if (st.is_redundant() && st.is_sat()) {
verify(1, &l);
}
if (st.is_deleted()) {
@ -213,17 +221,17 @@ namespace sat {
void drat::append(literal l1, literal l2, status st) {
TRACE("sat_drat", pp(tout, st) << " " << l1 << " " << l2 << "\n";);
declare(l1);
declare(l1);
declare(l2);
literal lits[2] = { l1, l2 };
IF_VERBOSE(20, trace(verbose_stream(), 2, lits, st););
if (st.is_deleted()) {
// noop
// don't record binary as deleted.
}
else {
if (st.is_redundant()) {
if (st.is_redundant() && st.is_sat()) {
verify(2, lits);
}
clause* c = m_alloc.mk_clause(2, lits, st.is_redundant());
@ -252,18 +260,18 @@ namespace sat {
(*m_out) << "b " << v << " " << n << " 0\n";
}
void drat::def_begin(unsigned n, symbol const& name) {
if (m_out)
(*m_out) << "n " << n << " " << name;
void drat::def_begin(unsigned n, std::string const& name) {
if (m_out)
(*m_out) << "e " << n << " " << name;
}
void drat::def_add_arg(unsigned arg) {
if (m_out)
if (m_out)
(*m_out) << " " << arg;
}
void drat::def_end() {
if (m_out)
if (m_out)
(*m_out) << " 0\n";
}
@ -300,12 +308,12 @@ namespace sat {
unsigned n = c.size();
IF_VERBOSE(20, trace(verbose_stream(), n, c.begin(), st););
if (st.is_redundant()) {
if (st.is_redundant() && st.is_sat()) {
verify(c);
}
m_status.push_back(st);
m_proof.push_back(&c);
m_proof.push_back(&c);
if (st.is_deleted()) {
if (n > 0) del_watch(c, c[0]);
if (n > 1) del_watch(c, c[1]);
@ -327,11 +335,11 @@ namespace sat {
}
}
switch (num_watch) {
case 0:
m_inconsistent = true;
case 0:
m_inconsistent = true;
break;
case 1:
assign_propagate(l1);
case 1:
assign_propagate(l1);
break;
default: {
SASSERT(num_watch == 2);
@ -345,7 +353,7 @@ namespace sat {
}
void drat::del_watch(clause& c, literal l) {
watch& w = m_watches[(~l).index()];
watch& w = m_watches[(~l).index()];
for (unsigned i = 0; i < w.size(); ++i) {
if (m_watched_clauses[w[i]].m_clause == &c) {
w[i] = w.back();
@ -368,7 +376,7 @@ namespace sat {
bool drat::is_drup(unsigned n, literal const* c) {
if (m_inconsistent || n == 0) return true;
unsigned num_units = m_units.size();
for (unsigned i = 0; !m_inconsistent && i < n; ++i) {
for (unsigned i = 0; !m_inconsistent && i < n; ++i) {
assign_propagate(~c[i]);
}
if (!m_inconsistent) {
@ -417,7 +425,7 @@ namespace sat {
}
svector<sat::solver::bin_clause> bin;
s.collect_bin_clauses(bin, true);
for (auto & b : bin) {
for (auto& b : bin) {
bool found = false;
if (m_assignment[b.first.var()] != (b.first.sign() ? l_true : l_false)) found = true;
if (m_assignment[b.second.var()] != (b.second.sign() ? l_true : l_false)) found = true;
@ -435,17 +443,16 @@ namespace sat {
}
m_units.shrink(num_units);
bool ok = m_inconsistent;
// IF_VERBOSE(9, verbose_stream() << "is-drup " << m_inconsistent << "\n");
m_inconsistent = false;
return ok;
}
bool drat::is_drat(unsigned n, literal const* c) {
if (m_inconsistent || n == 0) return true;
for (unsigned i = 0; i < n; ++i) {
if (is_drat(n, c, i)) return true;
}
if (m_inconsistent || n == 0)
return true;
for (unsigned i = 0; i < n; ++i)
if (is_drat(n, c, i))
return true;
return false;
}
@ -482,7 +489,7 @@ namespace sat {
if (st.is_sat() && j != c.size()) {
lits.append(j, c.begin());
lits.append(c.size() - j - 1, c.begin() + j + 1);
if (!is_drup(lits.size(), lits.c_ptr()))
if (!is_drup(lits.size(), lits.c_ptr()))
return false;
lits.resize(n);
}
@ -496,26 +503,33 @@ namespace sat {
if (!m_check_unsat) {
return;
}
for (unsigned i = 0; i < n; ++i) {
for (unsigned i = 0; i < n; ++i) {
declare(c[i]);
}
if (!is_drup(n, c) && !is_drat(n, c)) {
literal_vector lits(n, c);
IF_VERBOSE(0, verbose_stream() << "Verification of " << lits << " failed\n");
// s.display(std::cout);
std::string line;
std::getline(std::cin, line);
SASSERT(false);
INVOKE_DEBUGGER();
exit(0);
UNREACHABLE();
//display(std::cout);
TRACE("sat_drat",
tout << literal_vector(n, c) << "\n";
display(tout);
s.display(tout););
UNREACHABLE();
}
if (is_drup(n, c)) {
++m_stats.m_num_drup;
return;
}
if (is_drat(n, c)) {
++m_stats.m_num_drat;
return;
}
literal_vector lits(n, c);
IF_VERBOSE(0, verbose_stream() << "Verification of " << lits << " failed\n");
// s.display(std::cout);
std::string line;
std::getline(std::cin, line);
SASSERT(false);
INVOKE_DEBUGGER();
exit(0);
UNREACHABLE();
//display(std::cout);
TRACE("sat_drat",
tout << literal_vector(n, c) << "\n";
display(tout);
s.display(tout););
UNREACHABLE();
}
bool drat::contains(literal c, justification const& j) {
@ -529,7 +543,7 @@ namespace sat {
return contains(c, j.get_literal());
case justification::TERNARY:
return contains(c, j.get_literal1(), j.get_literal2());
case justification::CLAUSE:
case justification::CLAUSE:
return contains(s.get_clause(j));
default:
return true;
@ -546,7 +560,7 @@ namespace sat {
if (match(n, lits, c)) {
if (st.is_deleted()) {
num_del++;
}
}
else {
num_add++;
}
@ -578,7 +592,7 @@ namespace sat {
void drat::display(std::ostream& out) const {
out << "units: " << m_units << "\n";
for (unsigned i = 0; i < m_assignment.size(); ++i) {
lbool v = value(literal(i, false));
lbool v = value(literal(i, false));
if (v != l_undef) out << i << ": " << v << "\n";
}
for (unsigned i = 0; i < m_proof.size(); ++i) {
@ -599,12 +613,12 @@ namespace sat {
if (num_true == 0 && num_undef == 1) {
out << "Unit ";
}
pp(out, m_status[i]) << " " << i << ": " << *c << "\n";
pp(out, m_status[i]) << " " << i << ": " << *c << "\n";
}
}
for (unsigned i = 0; i < m_assignment.size(); ++i) {
watch const& w1 = m_watches[2*i];
watch const& w2 = m_watches[2*i + 1];
watch const& w1 = m_watches[2 * i];
watch const& w2 = m_watches[2 * i + 1];
if (!w1.empty()) {
out << i << " |-> ";
for (unsigned i = 0; i < w1.size(); ++i) out << *(m_watched_clauses[w1[i]].m_clause) << " ";
@ -626,7 +640,7 @@ namespace sat {
void drat::assign(literal l) {
lbool new_value = l.sign() ? l_false : l_true;
lbool old_value = value(l);
// TRACE("sat_drat", tout << "assign " << l << " := " << new_value << " from " << old_value << "\n";);
// TRACE("sat_drat", tout << "assign " << l << " := " << new_value << " from " << old_value << "\n";);
switch (old_value) {
case l_false:
m_inconsistent = true;
@ -645,7 +659,7 @@ namespace sat {
assign(l);
for (unsigned i = num_units; !m_inconsistent && i < m_units.size(); ++i) {
propagate(m_units[i]);
}
}
}
void drat::propagate(literal l) {
@ -676,10 +690,10 @@ namespace sat {
wc.m_l2 = lit;
m_watches[(~lit).index()].push_back(idx);
done = true;
}
}
}
if (done) {
continue;
continue;
}
else if (value(wc.m_l1) == l_false) {
m_inconsistent = true;
@ -693,19 +707,19 @@ namespace sat {
}
}
end_process_watch:
for (; it != end; ++it, ++it2)
*it2 = *it;
clauses.set_end(it2);
for (; it != end; ++it, ++it2)
*it2 = *it;
clauses.set_end(it2);
}
status drat::get_status(bool learned) const {
if (learned || s.m_searching)
return status::redundant();
return status::asserted();
return status::asserted();
}
void drat::add() {
++m_num_add;
++m_stats.m_num_add;
if (m_out) (*m_out) << "0\n";
if (m_bout) bdump(0, nullptr, status::redundant());
if (m_check_unsat) {
@ -713,7 +727,7 @@ namespace sat {
}
}
void drat::add(literal l, bool learned) {
++m_num_add;
++m_stats.m_num_add;
status st = get_status(learned);
if (m_out) dump(1, &l, st);
if (m_bout) bdump(1, &l, st);
@ -721,19 +735,19 @@ namespace sat {
}
void drat::add(literal l1, literal l2, status st) {
if (st.is_deleted())
++m_num_del;
++m_stats.m_num_del;
else
++m_num_add;
literal ls[2] = {l1, l2};
++m_stats.m_num_add;
literal ls[2] = { l1, l2 };
if (m_out) dump(2, ls, st);
if (m_bout) bdump(2, ls, st);
if (m_check) append(l1, l2, st);
}
void drat::add(clause& c, status st) {
if (st.is_deleted())
++m_num_del;
++m_stats.m_num_del;
else
++m_num_add;
++m_stats.m_num_add;
if (m_out) dump(c.size(), c.begin(), st);
if (m_bout) bdump(c.size(), c.begin(), st);
if (m_check) {
@ -743,9 +757,9 @@ namespace sat {
}
void drat::add(literal_vector const& lits, status st) {
if (st.is_deleted())
++m_num_del;
else
++m_num_add;
++m_stats.m_num_del;
else
++m_stats.m_num_add;
if (m_check) {
switch (lits.size()) {
case 0: add(); break;
@ -756,12 +770,12 @@ namespace sat {
break;
}
}
}
if (m_out)
}
if (m_out)
dump(lits.size(), lits.c_ptr(), st);
}
void drat::add(literal_vector const& c) {
++m_num_add;
++m_stats.m_num_add;
if (m_out) dump(c.size(), c.begin(), status::redundant());
if (m_bout) bdump(c.size(), c.begin(), status::redundant());
if (m_check) {
@ -780,15 +794,15 @@ namespace sat {
}
void drat::del(literal l) {
++m_num_del;
++m_stats.m_num_del;
if (m_out) dump(1, &l, status::deleted());
if (m_bout) bdump(1, &l, status::deleted());
if (m_check_unsat) append(l, status::deleted());
}
void drat::del(literal l1, literal l2) {
++m_num_del;
literal ls[2] = {l1, l2};
++m_stats.m_num_del;
literal ls[2] = { l1, l2 };
if (m_out) dump(2, ls, status::deleted());
if (m_bout) bdump(2, ls, status::deleted());
if (m_check) append(l1, l2, status::deleted());
@ -806,26 +820,47 @@ namespace sat {
m_seen[lit.index()] = false;
}
#endif
++m_num_del;
++m_stats.m_num_del;
if (m_out) dump(c.size(), c.begin(), status::deleted());
if (m_bout) bdump(c.size(), c.begin(), status::deleted());
if (m_check) {
clause* c1 = m_alloc.mk_clause(c.size(), c.begin(), c.is_learned());
clause* c1 = m_alloc.mk_clause(c.size(), c.begin(), c.is_learned());
append(*c1, status::deleted());
}
}
void drat::del(literal_vector const& c) {
++m_num_del;
++m_stats.m_num_del;
if (m_out) dump(c.size(), c.begin(), status::deleted());
if (m_bout) bdump(c.size(), c.begin(), status::deleted());
if (m_check) {
clause* c1 = m_alloc.mk_clause(c.size(), c.begin(), true);
clause* c1 = m_alloc.mk_clause(c.size(), c.begin(), true);
append(*c1, status::deleted());
}
}
void drat::check_model(model const& m) {
void drat::check_model(model const& m) {
}
std::ostream& operator<<(std::ostream& out, status const& st) {
if (st.is_deleted())
out << "d";
else if (st.is_input())
out << "i";
else if (st.is_asserted())
out << "a";
else if (st.is_redundant() && !st.is_sat())
out << "r";
if (!st.is_sat())
out << " th" << st.m_orig;
return out;
}
void drat::collect_statistics(statistics& st) const {
st.update("num-drup", m_stats.m_num_drup);
st.update("num-drat", m_stats.m_num_drat);
st.update("num-add", m_stats.m_num_add);
st.update("num-del", m_stats.m_num_del);
}
}