mirror of
https://github.com/Z3Prover/z3
synced 2025-04-24 09:35:32 +00:00
solver factories, cleanup solver API, simplified strategic solver, added combined solver
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
bfe6678ad2
commit
8198e62cbd
22 changed files with 720 additions and 492 deletions
289
src/solver/combined_solver.cpp
Normal file
289
src/solver/combined_solver.cpp
Normal file
|
@ -0,0 +1,289 @@
|
|||
/*++
|
||||
Copyright (c) 2012 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
combined_solver.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Implements the solver API by combining two solvers.
|
||||
|
||||
This is a replacement for the strategic_solver class.
|
||||
|
||||
Author:
|
||||
|
||||
Leonardo (leonardo) 2012-12-11
|
||||
|
||||
Notes:
|
||||
|
||||
--*/
|
||||
#include"solver.h"
|
||||
#include"scoped_timer.h"
|
||||
#include"combined_solver_params.hpp"
|
||||
#define PS_VB_LVL 15
|
||||
|
||||
/**
|
||||
\brief Implementation of the solver API that combines two given solvers.
|
||||
|
||||
The combined solver has two modes:
|
||||
- non-incremental
|
||||
- incremental
|
||||
In non-incremental mode, the first solver is used.
|
||||
In incremental mode, the second one is used.
|
||||
|
||||
A timeout for the second solver can be specified.
|
||||
If the timeout is reached, then the first solver is executed.
|
||||
|
||||
The object switches to incremental when:
|
||||
- push is used
|
||||
- assertions are peformed after a check_sat
|
||||
- parameter ignore_solver1==false
|
||||
*/
|
||||
class combined_solver : public solver {
|
||||
public:
|
||||
// Behavior when the incremental solver returns unknown.
|
||||
enum inc_unknown_behavior {
|
||||
IUB_RETURN_UNDEF, // just return unknown
|
||||
IUB_USE_TACTIC_IF_QF, // invoke tactic if problem is quantifier free
|
||||
IUB_USE_TACTIC // invoke tactic
|
||||
};
|
||||
|
||||
private:
|
||||
bool m_inc_mode;
|
||||
bool m_check_sat_executed;
|
||||
bool m_use_solver1_results;
|
||||
ref<solver> m_solver1;
|
||||
ref<solver> m_solver2;
|
||||
|
||||
bool m_ignore_solver1;
|
||||
inc_unknown_behavior m_inc_unknown_behavior;
|
||||
unsigned m_inc_timeout;
|
||||
|
||||
void switch_inc_mode() {
|
||||
m_inc_mode = true;
|
||||
}
|
||||
|
||||
struct aux_timeout_eh : public event_handler {
|
||||
solver * m_solver;
|
||||
volatile bool m_canceled;
|
||||
aux_timeout_eh(solver * s):m_solver(s), m_canceled(false) {}
|
||||
virtual void operator()() {
|
||||
m_solver->cancel();
|
||||
m_canceled = true;
|
||||
}
|
||||
};
|
||||
|
||||
void updt_local_params(params_ref const & _p) {
|
||||
combined_solver_params p(_p);
|
||||
m_inc_timeout = p.solver2_timeout();
|
||||
m_ignore_solver1 = p.ignore_solver1();
|
||||
m_inc_unknown_behavior = static_cast<inc_unknown_behavior>(p.solver2_unknown());
|
||||
}
|
||||
|
||||
bool has_quantifiers() const {
|
||||
unsigned sz = get_num_assertions();
|
||||
for (unsigned i = 0; i < sz; i++) {
|
||||
if (::has_quantifiers(get_assertion(i)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool use_solver1_when_undef() const {
|
||||
switch (m_inc_unknown_behavior) {
|
||||
case IUB_RETURN_UNDEF: return false;
|
||||
case IUB_USE_TACTIC_IF_QF: return !has_quantifiers();
|
||||
case IUB_USE_TACTIC: return true;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
combined_solver(solver * s1, solver * s2, params_ref const & p) {
|
||||
m_solver1 = s1;
|
||||
m_solver2 = s2;
|
||||
updt_local_params(p);
|
||||
m_inc_mode = false;
|
||||
m_check_sat_executed = false;
|
||||
m_use_solver1_results = true;
|
||||
}
|
||||
|
||||
virtual void updt_params(params_ref const & p) {
|
||||
m_solver1->updt_params(p);
|
||||
m_solver2->updt_params(p);
|
||||
updt_local_params(p);
|
||||
}
|
||||
|
||||
virtual void collect_param_descrs(param_descrs & r) {
|
||||
m_solver1->collect_param_descrs(r);
|
||||
m_solver2->collect_param_descrs(r);
|
||||
combined_solver_params::collect_param_descrs(r);
|
||||
}
|
||||
|
||||
virtual void set_produce_models(bool f) {
|
||||
m_solver1->set_produce_models(f);
|
||||
m_solver2->set_produce_models(f);
|
||||
}
|
||||
|
||||
virtual void assert_expr(expr * t) {
|
||||
if (m_check_sat_executed)
|
||||
switch_inc_mode();
|
||||
m_solver1->assert_expr(t);
|
||||
m_solver2->assert_expr(t);
|
||||
}
|
||||
|
||||
virtual void assert_expr(expr * t, expr * a) {
|
||||
if (m_check_sat_executed)
|
||||
switch_inc_mode();
|
||||
m_solver1->assert_expr(t, a);
|
||||
m_solver2->assert_expr(t, a);
|
||||
}
|
||||
|
||||
virtual void push() {
|
||||
switch_inc_mode();
|
||||
m_solver1->push();
|
||||
m_solver2->push();
|
||||
}
|
||||
|
||||
virtual void pop(unsigned n) {
|
||||
switch_inc_mode();
|
||||
m_solver1->pop(n);
|
||||
m_solver2->pop(n);
|
||||
}
|
||||
|
||||
virtual unsigned get_scope_level() const {
|
||||
return m_solver1->get_scope_level();
|
||||
}
|
||||
|
||||
virtual lbool check_sat(unsigned num_assumptions, expr * const * assumptions) {
|
||||
m_check_sat_executed = true;
|
||||
|
||||
if (num_assumptions > 0 || // assumptions were provided
|
||||
m_ignore_solver1) {
|
||||
// must use incremental solver
|
||||
switch_inc_mode();
|
||||
m_use_solver1_results = false;
|
||||
return m_solver2->check_sat(num_assumptions, assumptions);
|
||||
}
|
||||
|
||||
if (m_inc_mode) {
|
||||
if (m_inc_timeout == UINT_MAX) {
|
||||
IF_VERBOSE(PS_VB_LVL, verbose_stream() << "(combined-solver \"using solver 2 (without a timeout)\")\n";);
|
||||
lbool r = m_solver2->check_sat(0, 0);
|
||||
if (r != l_undef || !use_solver1_when_undef()) {
|
||||
m_use_solver1_results = false;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
else {
|
||||
IF_VERBOSE(PS_VB_LVL, verbose_stream() << "(combined-solver \"using solver 2 (with timeout)\")\n";);
|
||||
aux_timeout_eh eh(m_solver2.get());
|
||||
lbool r;
|
||||
{
|
||||
scoped_timer timer(m_inc_timeout, &eh);
|
||||
r = m_solver2->check_sat(0, 0);
|
||||
}
|
||||
if ((r != l_undef || !use_solver1_when_undef()) && !eh.m_canceled) {
|
||||
m_use_solver1_results = false;
|
||||
return r;
|
||||
}
|
||||
}
|
||||
IF_VERBOSE(PS_VB_LVL, verbose_stream() << "(combined-solver \"solver 2 failed, trying solver1\")\n";);
|
||||
}
|
||||
|
||||
IF_VERBOSE(PS_VB_LVL, verbose_stream() << "(combined-solver \"using solver 1\")\n";);
|
||||
m_use_solver1_results = true;
|
||||
return m_solver1->check_sat(0, 0);
|
||||
}
|
||||
|
||||
virtual void set_cancel(bool f) {
|
||||
m_solver1->set_cancel(f);
|
||||
m_solver2->set_cancel(f);
|
||||
}
|
||||
|
||||
virtual void set_progress_callback(progress_callback * callback) {
|
||||
m_solver1->set_progress_callback(callback);
|
||||
m_solver2->set_progress_callback(callback);
|
||||
}
|
||||
|
||||
virtual unsigned get_num_assertions() const {
|
||||
return m_solver1->get_num_assertions();
|
||||
}
|
||||
|
||||
virtual expr * get_assertion(unsigned idx) const {
|
||||
return m_solver1->get_assertion(idx);
|
||||
}
|
||||
|
||||
virtual void display(std::ostream & out) const {
|
||||
m_solver1->display(out);
|
||||
}
|
||||
|
||||
virtual void collect_statistics(statistics & st) const {
|
||||
if (m_use_solver1_results)
|
||||
m_solver1->collect_statistics(st);
|
||||
else
|
||||
m_solver2->collect_statistics(st);
|
||||
}
|
||||
|
||||
virtual void get_unsat_core(ptr_vector<expr> & r) {
|
||||
if (m_use_solver1_results)
|
||||
m_solver1->get_unsat_core(r);
|
||||
else
|
||||
m_solver2->get_unsat_core(r);
|
||||
}
|
||||
|
||||
virtual void get_model(model_ref & m) {
|
||||
if (m_use_solver1_results)
|
||||
m_solver1->get_model(m);
|
||||
else
|
||||
m_solver2->get_model(m);
|
||||
}
|
||||
|
||||
virtual proof * get_proof() {
|
||||
if (m_use_solver1_results)
|
||||
return m_solver1->get_proof();
|
||||
else
|
||||
return m_solver2->get_proof();
|
||||
}
|
||||
|
||||
virtual std::string reason_unknown() const {
|
||||
if (m_use_solver1_results)
|
||||
return m_solver1->reason_unknown();
|
||||
else
|
||||
return m_solver2->reason_unknown();
|
||||
}
|
||||
|
||||
virtual void get_labels(svector<symbol> & r) {
|
||||
if (m_use_solver1_results)
|
||||
return m_solver1->get_labels(r);
|
||||
else
|
||||
return m_solver2->get_labels(r);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
solver * mk_combined_solver(solver * s1, solver * s2, params_ref const & p) {
|
||||
return alloc(combined_solver, s1, s2, p);
|
||||
}
|
||||
|
||||
class combined_solver_factory : public solver_factory {
|
||||
scoped_ptr<solver_factory> m_f1;
|
||||
scoped_ptr<solver_factory> m_f2;
|
||||
public:
|
||||
combined_solver_factory(solver_factory * f1, solver_factory * f2):m_f1(f1), m_f2(f2) {}
|
||||
virtual ~combined_solver_factory() {}
|
||||
|
||||
virtual solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) {
|
||||
return mk_combined_solver((*m_f1)(m, p, proofs_enabled, models_enabled, unsat_core_enabled, logic),
|
||||
(*m_f2)(m, p, proofs_enabled, models_enabled, unsat_core_enabled, logic),
|
||||
p);
|
||||
}
|
||||
};
|
||||
|
||||
solver_factory * mk_combined_solver_factory(solver_factory * f1, solver_factory * f2) {
|
||||
return alloc(combined_solver_factory, f1, f2);
|
||||
}
|
32
src/solver/combined_solver.h
Normal file
32
src/solver/combined_solver.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*++
|
||||
Copyright (c) 2012 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
combined_solver.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Implements the solver API by combining two solvers.
|
||||
|
||||
This is a replacement for the strategic_solver class.
|
||||
|
||||
Author:
|
||||
|
||||
Leonardo (leonardo) 2012-12-11
|
||||
|
||||
Notes:
|
||||
|
||||
--*/
|
||||
#ifndef _COMBINED_SOLVER_H_
|
||||
#define _COMBINED_SOLVER_H_
|
||||
|
||||
#include"params.h"
|
||||
|
||||
class solver;
|
||||
class solver_factory;
|
||||
|
||||
solver * mk_combined_solver(solver * s1, solver * s2, params_ref const & p);
|
||||
solver_factory * mk_combined_solver_factory(solver_factory * f1, solver_factory * f2);
|
||||
|
||||
#endif
|
9
src/solver/combined_solver_params.pyg
Normal file
9
src/solver/combined_solver_params.pyg
Normal file
|
@ -0,0 +1,9 @@
|
|||
def_module_params('combined_solver',
|
||||
description='combines two solvers: non-incremental (solver1) and incremental (solver2)',
|
||||
export=True,
|
||||
params=(('solver2_timeout', UINT, UINT_MAX, "fallback to solver 1 after timeout even when in incremental model"),
|
||||
('ignore_solver1', BOOL, False, "if true, solver 2 is always used"),
|
||||
('solver2_unknown', UINT, 1, "what should be done when solver 2 returns unknown: 0 - just return unknown, 1 - execute solver 1 if quantifier free problem, 2 - execute solver 1")
|
||||
))
|
||||
|
||||
|
|
@ -23,6 +23,14 @@ Notes:
|
|||
#include"progress_callback.h"
|
||||
#include"params.h"
|
||||
|
||||
class solver;
|
||||
|
||||
class solver_factory {
|
||||
public:
|
||||
virtual ~solver_factory() {}
|
||||
virtual solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Abstract interface for making solvers available in the Z3
|
||||
API and front-ends such as SMT 2.0 and (legacy) SMT 1.0.
|
||||
|
@ -34,7 +42,6 @@ Notes:
|
|||
- statistics
|
||||
- results based on check_sat_result API
|
||||
- interruption (set_cancel)
|
||||
- resets
|
||||
*/
|
||||
class solver : public check_sat_result {
|
||||
public:
|
||||
|
@ -50,12 +57,6 @@ public:
|
|||
*/
|
||||
virtual void collect_param_descrs(param_descrs & r) {}
|
||||
|
||||
/**
|
||||
\brief Enable/Disable proof production for this solver object.
|
||||
|
||||
It is invoked before init(m, logic).
|
||||
*/
|
||||
virtual void set_produce_proofs(bool f) {}
|
||||
/**
|
||||
\brief Enable/Disable model generation for this solver object.
|
||||
|
||||
|
@ -63,23 +64,7 @@ public:
|
|||
The user may optionally invoke it after init(m, logic).
|
||||
*/
|
||||
virtual void set_produce_models(bool f) {}
|
||||
/**
|
||||
\brief Enable/Disable unsat core generation for this solver object.
|
||||
|
||||
It is invoked before init(m, logic).
|
||||
*/
|
||||
virtual void set_produce_unsat_cores(bool f) {}
|
||||
|
||||
/**
|
||||
\brief Initialize the solver object with the given ast_manager and logic.
|
||||
*/
|
||||
virtual void init(ast_manager & m, symbol const & logic) = 0;
|
||||
|
||||
/**
|
||||
\brief Reset the solver internal state. All assertions should be removed.
|
||||
*/
|
||||
virtual void reset() = 0;
|
||||
|
||||
/**
|
||||
\brief Add a new formula to the assertion stack.
|
||||
*/
|
||||
|
|
|
@ -22,8 +22,8 @@ Notes:
|
|||
#include"solver_na2as.h"
|
||||
#include"ast_smt2_pp.h"
|
||||
|
||||
solver_na2as::solver_na2as() {
|
||||
m_manager = 0;
|
||||
solver_na2as::solver_na2as(ast_manager & m):
|
||||
m_manager(m) {
|
||||
}
|
||||
|
||||
solver_na2as::~solver_na2as() {
|
||||
|
@ -35,23 +35,16 @@ void solver_na2as::assert_expr(expr * t, expr * a) {
|
|||
assert_expr(t);
|
||||
}
|
||||
else {
|
||||
SASSERT(m_manager != 0);
|
||||
SASSERT(is_uninterp_const(a));
|
||||
SASSERT(m_manager->is_bool(a));
|
||||
TRACE("solver_na2as", tout << "asserting\n" << mk_ismt2_pp(t, *m_manager) << "\n" << mk_ismt2_pp(a, *m_manager) << "\n";);
|
||||
m_manager->inc_ref(a);
|
||||
SASSERT(m_manager.is_bool(a));
|
||||
TRACE("solver_na2as", tout << "asserting\n" << mk_ismt2_pp(t, m_manager) << "\n" << mk_ismt2_pp(a, m_manager) << "\n";);
|
||||
m_manager.inc_ref(a);
|
||||
m_assumptions.push_back(a);
|
||||
expr_ref new_t(*m_manager);
|
||||
new_t = m_manager->mk_implies(a, t);
|
||||
expr_ref new_t(m_manager);
|
||||
new_t = m_manager.mk_implies(a, t);
|
||||
assert_expr(new_t);
|
||||
}
|
||||
}
|
||||
|
||||
void solver_na2as::init(ast_manager & m, symbol const & logic) {
|
||||
SASSERT(m_assumptions.empty());
|
||||
m_manager = &m;
|
||||
init_core(m, logic);
|
||||
}
|
||||
|
||||
struct append_assumptions {
|
||||
ptr_vector<expr> & m_assumptions;
|
||||
|
@ -89,9 +82,9 @@ void solver_na2as::pop(unsigned n) {
|
|||
}
|
||||
|
||||
void solver_na2as::restore_assumptions(unsigned old_sz) {
|
||||
SASSERT(old_sz == 0 || m_manager != 0);
|
||||
SASSERT(old_sz == 0);
|
||||
for (unsigned i = old_sz; i < m_assumptions.size(); i++) {
|
||||
m_manager->dec_ref(m_assumptions[i]);
|
||||
m_manager.dec_ref(m_assumptions[i]);
|
||||
}
|
||||
m_assumptions.shrink(old_sz);
|
||||
}
|
||||
|
@ -100,7 +93,3 @@ unsigned solver_na2as::get_scope_level() const {
|
|||
return m_scopes.size();
|
||||
}
|
||||
|
||||
void solver_na2as::reset() {
|
||||
reset_core();
|
||||
restore_assumptions(0);
|
||||
}
|
||||
|
|
|
@ -25,30 +25,26 @@ Notes:
|
|||
#include"solver.h"
|
||||
|
||||
class solver_na2as : public solver {
|
||||
ast_manager * m_manager;
|
||||
ast_manager & m_manager;
|
||||
ptr_vector<expr> m_assumptions;
|
||||
unsigned_vector m_scopes;
|
||||
void restore_assumptions(unsigned old_sz);
|
||||
public:
|
||||
solver_na2as();
|
||||
solver_na2as(ast_manager & m);
|
||||
virtual ~solver_na2as();
|
||||
|
||||
virtual void assert_expr(expr * t, expr * a);
|
||||
virtual void assert_expr(expr * t) = 0;
|
||||
|
||||
// Subclasses of solver_na2as should redefine the following *_core methods instead of these ones.
|
||||
virtual void init(ast_manager & m, symbol const & logic);
|
||||
virtual lbool check_sat(unsigned num_assumptions, expr * const * assumptions);
|
||||
virtual void push();
|
||||
virtual void pop(unsigned n);
|
||||
virtual unsigned get_scope_level() const;
|
||||
virtual void reset();
|
||||
protected:
|
||||
virtual void init_core(ast_manager & m, symbol const & logic) = 0;
|
||||
virtual lbool check_sat_core(unsigned num_assumptions, expr * const * assumptions) = 0;
|
||||
virtual void push_core() = 0;
|
||||
virtual void pop_core(unsigned n) = 0;
|
||||
virtual void reset_core() = 0;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ Author:
|
|||
Notes:
|
||||
|
||||
--*/
|
||||
#if 0
|
||||
#include"strategic_solver.h"
|
||||
#include"scoped_timer.h"
|
||||
#include"ast_smt2_pp.h"
|
||||
|
@ -526,6 +527,7 @@ void strategic_solver::display(std::ostream & out) const {
|
|||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -61,7 +61,8 @@ private:
|
|||
bool m_force_tactic; // use tactics even when auto_config = false
|
||||
bool m_inc_mode;
|
||||
bool m_check_sat_executed;
|
||||
scoped_ptr<solver> m_inc_solver;
|
||||
scoped_ptr<solver_factory> m_inc_solver_factory;
|
||||
ref<solver> m_inc_solver;
|
||||
unsigned m_inc_solver_timeout;
|
||||
inc_unknown_behavior m_inc_unknown_behavior;
|
||||
scoped_ptr<tactic_factory> m_default_fct;
|
||||
|
@ -107,12 +108,12 @@ private:
|
|||
bool use_tactic_when_undef() const;
|
||||
|
||||
public:
|
||||
strategic_solver();
|
||||
strategic_solver(ast_manager & m, bool produce_proofs, bool produce_models, bool produce_unsat_cores, symbol const & logic);
|
||||
~strategic_solver();
|
||||
|
||||
ast_manager & m() const { SASSERT(m_manager); return *m_manager; }
|
||||
|
||||
void set_inc_solver(solver * s);
|
||||
void set_inc_solver_factory(solver_factory * s);
|
||||
void set_inc_solver_timeout(unsigned timeout);
|
||||
void set_default_tactic(tactic_factory * fct);
|
||||
void set_tactic_for(symbol const & logic, tactic_factory * fct);
|
||||
|
|
|
@ -19,96 +19,115 @@ Author:
|
|||
Notes:
|
||||
|
||||
--*/
|
||||
#include"tactic2solver.h"
|
||||
#include"solver_na2as.h"
|
||||
#include"tactic.h"
|
||||
#include"ast_smt2_pp.h"
|
||||
|
||||
tactic2solver_core::ctx::ctx(ast_manager & m, symbol const & logic):
|
||||
m_logic(logic),
|
||||
/**
|
||||
\brief Simulates the incremental solver interface using a tactic.
|
||||
|
||||
Every query will be solved from scratch. So, this is not a good
|
||||
option for applications trying to solve many easy queries that a
|
||||
similar to each other.
|
||||
*/
|
||||
class tactic2solver : public solver_na2as {
|
||||
expr_ref_vector m_assertions;
|
||||
unsigned_vector m_scopes;
|
||||
ref<simple_check_sat_result> m_result;
|
||||
tactic_ref m_tactic;
|
||||
symbol m_logic;
|
||||
params_ref m_params;
|
||||
bool m_produce_models;
|
||||
bool m_produce_proofs;
|
||||
bool m_produce_unsat_cores;
|
||||
public:
|
||||
tactic2solver(ast_manager & m, tactic * t, params_ref const & p, bool produce_proofs, bool produce_models, bool produce_unsat_cores, symbol const & logic);
|
||||
virtual ~tactic2solver();
|
||||
|
||||
virtual void updt_params(params_ref const & p);
|
||||
virtual void collect_param_descrs(param_descrs & r);
|
||||
|
||||
virtual void set_produce_models(bool f) { m_produce_models = f; }
|
||||
|
||||
virtual void assert_expr(expr * t);
|
||||
|
||||
virtual void push_core();
|
||||
virtual void pop_core(unsigned n);
|
||||
virtual lbool check_sat_core(unsigned num_assumptions, expr * const * assumptions);
|
||||
|
||||
virtual void set_cancel(bool f);
|
||||
|
||||
virtual void collect_statistics(statistics & st) const;
|
||||
virtual void get_unsat_core(ptr_vector<expr> & r);
|
||||
virtual void get_model(model_ref & m);
|
||||
virtual proof * get_proof();
|
||||
virtual std::string reason_unknown() const;
|
||||
virtual void get_labels(svector<symbol> & r) {}
|
||||
|
||||
virtual void set_progress_callback(progress_callback * callback) {}
|
||||
|
||||
virtual unsigned get_num_assertions() const;
|
||||
virtual expr * get_assertion(unsigned idx) const;
|
||||
|
||||
virtual void display(std::ostream & out) const;
|
||||
};
|
||||
|
||||
tactic2solver::tactic2solver(ast_manager & m, tactic * t, params_ref const & p, bool produce_proofs, bool produce_models, bool produce_unsat_cores, symbol const & logic):
|
||||
solver_na2as(m),
|
||||
m_assertions(m) {
|
||||
|
||||
m_tactic = t;
|
||||
m_logic = logic;
|
||||
m_params = p;
|
||||
|
||||
m_produce_models = produce_models;
|
||||
m_produce_proofs = produce_proofs;
|
||||
m_produce_unsat_cores = produce_unsat_cores;
|
||||
}
|
||||
|
||||
tactic2solver_core::~tactic2solver_core() {
|
||||
tactic2solver::~tactic2solver() {
|
||||
}
|
||||
|
||||
void tactic2solver_core::init_core(ast_manager & m, symbol const & logic) {
|
||||
m_ctx = alloc(ctx, m, logic);
|
||||
}
|
||||
|
||||
void tactic2solver_core::updt_params(params_ref const & p) {
|
||||
void tactic2solver::updt_params(params_ref const & p) {
|
||||
m_params = p;
|
||||
}
|
||||
|
||||
void tactic2solver_core::collect_param_descrs(param_descrs & r) {
|
||||
if (m_ctx) {
|
||||
if (!m_ctx->m_tactic) {
|
||||
#pragma omp critical (tactic2solver_core)
|
||||
{
|
||||
m_ctx->m_tactic = get_tactic(m_ctx->m(), m_params);
|
||||
}
|
||||
|
||||
if (m_ctx->m_tactic) {
|
||||
m_ctx->m_tactic->collect_param_descrs(r);
|
||||
}
|
||||
|
||||
#pragma omp critical (tactic2solver_core)
|
||||
{
|
||||
m_ctx->m_tactic = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
m_ctx->m_tactic->collect_param_descrs(r);
|
||||
}
|
||||
}
|
||||
void tactic2solver::collect_param_descrs(param_descrs & r) {
|
||||
if (m_tactic.get())
|
||||
m_tactic->collect_param_descrs(r);
|
||||
}
|
||||
|
||||
void tactic2solver_core::reset_core() {
|
||||
SASSERT(m_ctx);
|
||||
m_ctx->m_assertions.reset();
|
||||
m_ctx->m_scopes.reset();
|
||||
m_ctx->m_result = 0;
|
||||
void tactic2solver::assert_expr(expr * t) {
|
||||
m_assertions.push_back(t);
|
||||
m_result = 0;
|
||||
}
|
||||
|
||||
void tactic2solver_core::assert_expr(expr * t) {
|
||||
SASSERT(m_ctx);
|
||||
m_ctx->m_assertions.push_back(t);
|
||||
m_ctx->m_result = 0;
|
||||
void tactic2solver::push_core() {
|
||||
m_scopes.push_back(m_assertions.size());
|
||||
m_result = 0;
|
||||
}
|
||||
|
||||
void tactic2solver_core::push_core() {
|
||||
SASSERT(m_ctx);
|
||||
m_ctx->m_scopes.push_back(m_ctx->m_assertions.size());
|
||||
m_ctx->m_result = 0;
|
||||
void tactic2solver::pop_core(unsigned n) {
|
||||
unsigned new_lvl = m_scopes.size() - n;
|
||||
unsigned old_sz = m_scopes[new_lvl];
|
||||
m_assertions.shrink(old_sz);
|
||||
m_scopes.shrink(new_lvl);
|
||||
m_result = 0;
|
||||
}
|
||||
|
||||
void tactic2solver_core::pop_core(unsigned n) {
|
||||
SASSERT(m_ctx);
|
||||
unsigned new_lvl = m_ctx->m_scopes.size() - n;
|
||||
unsigned old_sz = m_ctx->m_scopes[new_lvl];
|
||||
m_ctx->m_assertions.shrink(old_sz);
|
||||
m_ctx->m_scopes.shrink(new_lvl);
|
||||
m_ctx->m_result = 0;
|
||||
}
|
||||
|
||||
lbool tactic2solver_core::check_sat_core(unsigned num_assumptions, expr * const * assumptions) {
|
||||
SASSERT(m_ctx);
|
||||
ast_manager & m = m_ctx->m();
|
||||
params_ref p = m_params;
|
||||
#pragma omp critical (tactic2solver_core)
|
||||
{
|
||||
m_ctx->m_tactic = get_tactic(m, p);
|
||||
if (m_ctx->m_tactic) {
|
||||
m_ctx->m_result = alloc(simple_check_sat_result, m);
|
||||
}
|
||||
}
|
||||
if (!m_ctx->m_tactic)
|
||||
return l_undef;
|
||||
tactic & t = *(m_ctx->m_tactic);
|
||||
simple_check_sat_result & result = *(m_ctx->m_result);
|
||||
lbool tactic2solver::check_sat_core(unsigned num_assumptions, expr * const * assumptions) {
|
||||
if (m_tactic.get() == 0)
|
||||
return l_false;
|
||||
ast_manager & m = m_assertions.m();
|
||||
m_result = alloc(simple_check_sat_result, m);
|
||||
m_tactic->cleanup();
|
||||
m_tactic->updt_params(m_params);
|
||||
m_tactic->set_logic(m_logic);
|
||||
goal_ref g = alloc(goal, m, m_produce_proofs, m_produce_models, m_produce_unsat_cores);
|
||||
t.set_logic(m_ctx->m_logic);
|
||||
unsigned sz = m_ctx->m_assertions.size();
|
||||
|
||||
unsigned sz = m_assertions.size();
|
||||
for (unsigned i = 0; i < sz; i++) {
|
||||
g->assert_expr(m_ctx->m_assertions.get(i));
|
||||
g->assert_expr(m_assertions.get(i));
|
||||
}
|
||||
for (unsigned i = 0; i < num_assumptions; i++) {
|
||||
g->assert_expr(assumptions[i], m.mk_asserted(assumptions[i]), m.mk_leaf(assumptions[i]));
|
||||
|
@ -119,17 +138,17 @@ lbool tactic2solver_core::check_sat_core(unsigned num_assumptions, expr * const
|
|||
expr_dependency_ref core(m);
|
||||
std::string reason_unknown = "unknown";
|
||||
try {
|
||||
switch (::check_sat(t, g, md, pr, core, reason_unknown)) {
|
||||
switch (::check_sat(*m_tactic, g, md, pr, core, reason_unknown)) {
|
||||
case l_true:
|
||||
result.set_status(l_true);
|
||||
m_result->set_status(l_true);
|
||||
break;
|
||||
case l_false:
|
||||
result.set_status(l_false);
|
||||
m_result->set_status(l_false);
|
||||
break;
|
||||
default:
|
||||
result.set_status(l_undef);
|
||||
m_result->set_status(l_undef);
|
||||
if (reason_unknown != "")
|
||||
result.m_unknown = reason_unknown;
|
||||
m_result->m_unknown = reason_unknown;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -137,112 +156,115 @@ lbool tactic2solver_core::check_sat_core(unsigned num_assumptions, expr * const
|
|||
throw ex;
|
||||
}
|
||||
catch (z3_exception & ex) {
|
||||
TRACE("tactic2solver_core", tout << "exception: " << ex.msg() << "\n";);
|
||||
result.set_status(l_undef);
|
||||
result.m_unknown = ex.msg();
|
||||
TRACE("tactic2solver", tout << "exception: " << ex.msg() << "\n";);
|
||||
m_result->set_status(l_undef);
|
||||
m_result->m_unknown = ex.msg();
|
||||
}
|
||||
t.collect_statistics(result.m_stats);
|
||||
result.m_model = md;
|
||||
result.m_proof = pr;
|
||||
m_tactic->collect_statistics(m_result->m_stats);
|
||||
m_result->m_model = md;
|
||||
m_result->m_proof = pr;
|
||||
if (m_produce_unsat_cores) {
|
||||
ptr_vector<expr> core_elems;
|
||||
m.linearize(core, core_elems);
|
||||
result.m_core.append(core_elems.size(), core_elems.c_ptr());
|
||||
m_result->m_core.append(core_elems.size(), core_elems.c_ptr());
|
||||
}
|
||||
|
||||
#pragma omp critical (tactic2solver_core)
|
||||
{
|
||||
m_ctx->m_tactic = 0;
|
||||
}
|
||||
return result.status();
|
||||
m_tactic->cleanup();
|
||||
return m_result->status();
|
||||
}
|
||||
|
||||
void tactic2solver_core::set_cancel(bool f) {
|
||||
#pragma omp critical (tactic2solver_core)
|
||||
{
|
||||
if (m_ctx && m_ctx->m_tactic)
|
||||
m_ctx->m_tactic->set_cancel(f);
|
||||
}
|
||||
void tactic2solver::set_cancel(bool f) {
|
||||
if (m_tactic.get())
|
||||
m_tactic->set_cancel(f);
|
||||
}
|
||||
|
||||
void tactic2solver_core::collect_statistics(statistics & st) const {
|
||||
if (m_ctx->m_result.get())
|
||||
m_ctx->m_result->collect_statistics(st);
|
||||
void tactic2solver::collect_statistics(statistics & st) const {
|
||||
if (m_result.get())
|
||||
m_result->collect_statistics(st);
|
||||
}
|
||||
|
||||
void tactic2solver_core::get_unsat_core(ptr_vector<expr> & r) {
|
||||
if (m_ctx->m_result.get())
|
||||
m_ctx->m_result->get_unsat_core(r);
|
||||
void tactic2solver::get_unsat_core(ptr_vector<expr> & r) {
|
||||
if (m_result.get())
|
||||
m_result->get_unsat_core(r);
|
||||
}
|
||||
|
||||
void tactic2solver_core::get_model(model_ref & m) {
|
||||
if (m_ctx->m_result.get())
|
||||
m_ctx->m_result->get_model(m);
|
||||
void tactic2solver::get_model(model_ref & m) {
|
||||
if (m_result.get())
|
||||
m_result->get_model(m);
|
||||
}
|
||||
|
||||
proof * tactic2solver_core::get_proof() {
|
||||
if (m_ctx->m_result.get())
|
||||
return m_ctx->m_result->get_proof();
|
||||
proof * tactic2solver::get_proof() {
|
||||
if (m_result.get())
|
||||
return m_result->get_proof();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string tactic2solver_core::reason_unknown() const {
|
||||
if (m_ctx->m_result.get())
|
||||
return m_ctx->m_result->reason_unknown();
|
||||
std::string tactic2solver::reason_unknown() const {
|
||||
if (m_result.get())
|
||||
return m_result->reason_unknown();
|
||||
else
|
||||
return std::string("unknown");
|
||||
}
|
||||
|
||||
unsigned tactic2solver_core::get_num_assertions() const {
|
||||
if (m_ctx)
|
||||
return m_ctx->m_assertions.size();
|
||||
else
|
||||
return 0;
|
||||
unsigned tactic2solver::get_num_assertions() const {
|
||||
return m_assertions.size();
|
||||
}
|
||||
|
||||
expr * tactic2solver_core::get_assertion(unsigned idx) const {
|
||||
SASSERT(m_ctx);
|
||||
return m_ctx->m_assertions.get(idx);
|
||||
expr * tactic2solver::get_assertion(unsigned idx) const {
|
||||
return m_assertions.get(idx);
|
||||
}
|
||||
|
||||
void tactic2solver_core::display(std::ostream & out) const {
|
||||
if (m_ctx) {
|
||||
ast_manager & m = m_ctx->m_assertions.m();
|
||||
unsigned num = m_ctx->m_assertions.size();
|
||||
out << "(solver";
|
||||
for (unsigned i = 0; i < num; i++) {
|
||||
out << "\n " << mk_ismt2_pp(m_ctx->m_assertions.get(i), m, 2);
|
||||
}
|
||||
out << ")";
|
||||
void tactic2solver::display(std::ostream & out) const {
|
||||
ast_manager & m = m_assertions.m();
|
||||
unsigned num = m_assertions.size();
|
||||
out << "(solver";
|
||||
for (unsigned i = 0; i < num; i++) {
|
||||
out << "\n " << mk_ismt2_pp(m_assertions.get(i), m, 2);
|
||||
}
|
||||
else {
|
||||
out << "(solver)";
|
||||
out << ")";
|
||||
}
|
||||
|
||||
solver * mk_tactic2solver(ast_manager & m,
|
||||
tactic * t,
|
||||
params_ref const & p,
|
||||
bool produce_proofs,
|
||||
bool produce_models,
|
||||
bool produce_unsat_cores,
|
||||
symbol const & logic) {
|
||||
return alloc(tactic2solver, m, t, p, produce_proofs, produce_models, produce_unsat_cores, logic);
|
||||
}
|
||||
|
||||
class tactic2solver_factory : public solver_factory {
|
||||
ref<tactic> m_tactic;
|
||||
public:
|
||||
tactic2solver_factory(tactic * t):m_tactic(t) {
|
||||
}
|
||||
|
||||
virtual ~tactic2solver_factory() {}
|
||||
|
||||
virtual solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) {
|
||||
return mk_tactic2solver(m, m_tactic.get(), p, proofs_enabled, models_enabled, unsat_core_enabled, logic);
|
||||
}
|
||||
};
|
||||
|
||||
class tactic_factory2solver_factory : public solver_factory {
|
||||
scoped_ptr<tactic_factory> m_factory;
|
||||
public:
|
||||
tactic_factory2solver_factory(tactic_factory * f):m_factory(f) {
|
||||
}
|
||||
|
||||
virtual ~tactic_factory2solver_factory() {}
|
||||
|
||||
virtual solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) {
|
||||
tactic * t = (*m_factory)(m, p);
|
||||
return mk_tactic2solver(m, t, p, proofs_enabled, models_enabled, unsat_core_enabled, logic);
|
||||
}
|
||||
};
|
||||
|
||||
solver_factory * mk_tactic2solver_factory(tactic * t) {
|
||||
return alloc(tactic2solver_factory, t);
|
||||
}
|
||||
|
||||
tactic2solver::tactic2solver(tactic * t):
|
||||
m_tactic(t) {
|
||||
}
|
||||
|
||||
tactic2solver::~tactic2solver() {
|
||||
}
|
||||
|
||||
tactic * tactic2solver::get_tactic(ast_manager & m, params_ref const & p) {
|
||||
m_tactic->cleanup();
|
||||
m_tactic->updt_params(p);
|
||||
return m_tactic.get();
|
||||
}
|
||||
|
||||
tactic_factory2solver::~tactic_factory2solver() {
|
||||
}
|
||||
|
||||
void tactic_factory2solver::set_tactic(tactic_factory * f) {
|
||||
m_tactic_factory = f;
|
||||
}
|
||||
|
||||
tactic * tactic_factory2solver::get_tactic(ast_manager & m, params_ref const & p) {
|
||||
if (m_tactic_factory == 0)
|
||||
return 0;
|
||||
return (*m_tactic_factory)(m, p);
|
||||
solver_factory * mk_tactic_factory2solver_factory(tactic_factory * f) {
|
||||
return alloc(tactic_factory2solver_factory, f);
|
||||
}
|
||||
|
|
|
@ -22,88 +22,22 @@ Notes:
|
|||
#ifndef _TACTIC2SOLVER_H_
|
||||
#define _TACTIC2SOLVER_H_
|
||||
|
||||
#include"solver_na2as.h"
|
||||
#include"tactic.h"
|
||||
#include"params.h"
|
||||
class ast_manager;
|
||||
class tactic;
|
||||
class tactic_factory;
|
||||
class solver;
|
||||
class solver_factory;
|
||||
|
||||
/**
|
||||
\brief Simulates the incremental solver interface using a tactic.
|
||||
|
||||
Every query will be solved from scratch. So, this is not a good
|
||||
option for applications trying to solve many easy queries that a
|
||||
similar to each other.
|
||||
*/
|
||||
class tactic2solver_core : public solver_na2as {
|
||||
struct ctx {
|
||||
symbol m_logic;
|
||||
expr_ref_vector m_assertions;
|
||||
unsigned_vector m_scopes;
|
||||
ref<simple_check_sat_result> m_result;
|
||||
tactic_ref m_tactic;
|
||||
ctx(ast_manager & m, symbol const & logic);
|
||||
ast_manager & m() const { return m_assertions.m(); }
|
||||
};
|
||||
scoped_ptr<ctx> m_ctx;
|
||||
params_ref m_params;
|
||||
bool m_produce_models;
|
||||
bool m_produce_proofs;
|
||||
bool m_produce_unsat_cores;
|
||||
public:
|
||||
tactic2solver_core():m_ctx(0), m_produce_models(false), m_produce_proofs(false), m_produce_unsat_cores(false) {}
|
||||
virtual ~tactic2solver_core();
|
||||
|
||||
virtual tactic * get_tactic(ast_manager & m, params_ref const & p) = 0;
|
||||
|
||||
virtual void updt_params(params_ref const & p);
|
||||
virtual void collect_param_descrs(param_descrs & r);
|
||||
|
||||
virtual void set_produce_proofs(bool f) { m_produce_proofs = f; }
|
||||
virtual void set_produce_models(bool f) { m_produce_models = f; }
|
||||
virtual void set_produce_unsat_cores(bool f) { m_produce_unsat_cores = f; }
|
||||
|
||||
virtual void assert_expr(expr * t);
|
||||
|
||||
virtual void init_core(ast_manager & m, symbol const & logic);
|
||||
virtual void reset_core();
|
||||
virtual void push_core();
|
||||
virtual void pop_core(unsigned n);
|
||||
virtual lbool check_sat_core(unsigned num_assumptions, expr * const * assumptions);
|
||||
|
||||
virtual void set_cancel(bool f);
|
||||
|
||||
virtual void collect_statistics(statistics & st) const;
|
||||
virtual void get_unsat_core(ptr_vector<expr> & r);
|
||||
virtual void get_model(model_ref & m);
|
||||
virtual proof * get_proof();
|
||||
virtual std::string reason_unknown() const;
|
||||
virtual void get_labels(svector<symbol> & r) {}
|
||||
|
||||
virtual void set_progress_callback(progress_callback * callback) {}
|
||||
|
||||
virtual unsigned get_num_assertions() const;
|
||||
virtual expr * get_assertion(unsigned idx) const;
|
||||
|
||||
virtual void display(std::ostream & out) const;
|
||||
};
|
||||
|
||||
class tactic2solver : public tactic2solver_core {
|
||||
tactic_ref m_tactic;
|
||||
public:
|
||||
tactic2solver(tactic * t);
|
||||
virtual ~tactic2solver();
|
||||
virtual tactic * get_tactic(ast_manager & m, params_ref const & p);
|
||||
};
|
||||
|
||||
|
||||
class tactic_factory2solver : public tactic2solver_core {
|
||||
scoped_ptr<tactic_factory> m_tactic_factory;
|
||||
public:
|
||||
virtual ~tactic_factory2solver();
|
||||
/**
|
||||
\brief Set tactic that will be used to process the satisfiability queries.
|
||||
*/
|
||||
void set_tactic(tactic_factory * f);
|
||||
virtual tactic * get_tactic(ast_manager & m, params_ref const & p);
|
||||
};
|
||||
solver * mk_tactic2solver(ast_manager & m,
|
||||
tactic * t = 0,
|
||||
params_ref const & p = params_ref(),
|
||||
bool produce_proofs = false,
|
||||
bool produce_models = true,
|
||||
bool produce_unsat_cores = false,
|
||||
symbol const & logic = symbol::null);
|
||||
|
||||
solver_factory * mk_tactic2solver_factory(tactic * t);
|
||||
solver_factory * mk_tactic_factory2solver_factory(tactic_factory * f);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue