1 /*++
2 Copyright (c) 2012 Microsoft Corporation
3 
4 Module Name:
5 
6     tactic2solver.cpp
7 
8 Abstract:
9 
10     Wrapper for implementing the solver interface
11     using a tactic.
12 
13     This is a light version of the strategic solver.
14 
15 Author:
16 
17     Leonardo (leonardo) 2012-01-23
18 
19 Notes:
20 
21 --*/
22 #include "ast/ast_translation.h"
23 #include "ast/ast_pp.h"
24 #include "tactic/tactic.h"
25 #include "solver/tactic2solver.h"
26 #include "solver/solver_na2as.h"
27 #include "solver/mus.h"
28 
29 /**
30    \brief Simulates the incremental solver interface using a tactic.
31 
32    Every query will be solved from scratch.  So, this is not a good
33    option for applications trying to solve many easy queries that a
34    similar to each other.
35 */
36 
37 namespace {
38 class tactic2solver : public solver_na2as {
39     expr_ref_vector              m_assertions;
40     expr_ref_vector              m_last_assertions;
41     unsigned                     m_last_assertions_valid;
42     unsigned_vector              m_scopes;
43     ref<simple_check_sat_result> m_result;
44     tactic_ref                   m_tactic;
45     ref<model_converter>         m_mc;
46     symbol                       m_logic;
47     bool                         m_produce_models;
48     bool                         m_produce_proofs;
49     bool                         m_produce_unsat_cores;
50     statistics                   m_stats;
51 
52 public:
53     tactic2solver(ast_manager & m, tactic * t, params_ref const & p, bool produce_proofs, bool produce_models, bool produce_unsat_cores, symbol const & logic);
54     ~tactic2solver() override;
55 
56     solver* translate(ast_manager& m, params_ref const& p) override;
57 
58     void updt_params(params_ref const & p) override;
59     void collect_param_descrs(param_descrs & r) override;
60 
set_produce_models(bool f)61     void set_produce_models(bool f) override { m_produce_models = f; }
62 
63     void assert_expr_core(expr * t) override;
64     ast_manager& get_manager() const override;
65 
66     void push_core() override;
67     void pop_core(unsigned n) override;
68     lbool check_sat_core2(unsigned num_assumptions, expr * const * assumptions) override;
69 
70     void collect_statistics(statistics & st) const override;
71     void get_unsat_core(expr_ref_vector & r) override;
72     void get_model_core(model_ref & m) override;
73     proof * get_proof() override;
74     std::string reason_unknown() const override;
75     void set_reason_unknown(char const* msg) override;
get_labels(svector<symbol> & r)76     void get_labels(svector<symbol> & r) override {}
77 
set_progress_callback(progress_callback * callback)78     void set_progress_callback(progress_callback * callback) override {}
79 
80     unsigned get_num_assertions() const override;
81     expr * get_assertion(unsigned idx) const override;
set_phase(expr * e)82     void set_phase(expr* e) override { }
get_phase()83     phase* get_phase() override { return nullptr; }
set_phase(phase * p)84     void set_phase(phase* p) override { }
move_to_front(expr * e)85     void move_to_front(expr* e) override { }
86 
87 
cube(expr_ref_vector & vars,unsigned)88     expr_ref_vector cube(expr_ref_vector& vars, unsigned ) override {
89         set_reason_unknown("cubing is not supported on tactics");
90         IF_VERBOSE(1, verbose_stream() << "cubing is not supported on tactics\n");
91         return expr_ref_vector(get_manager());
92     }
93 
get_model_converter() const94     model_converter_ref get_model_converter() const override { return m_mc; }
95 
get_levels(ptr_vector<expr> const & vars,unsigned_vector & depth)96     void get_levels(ptr_vector<expr> const& vars, unsigned_vector& depth) override {
97         throw default_exception("cannot retrieve depth from solvers created using tactics");
98     }
99 
get_trail()100     expr_ref_vector get_trail() override {
101         throw default_exception("cannot retrieve trail from solvers created using tactics");
102     }
103 };
104 
get_manager() const105 ast_manager& tactic2solver::get_manager() const { return m_assertions.get_manager(); }
106 
tactic2solver(ast_manager & m,tactic * t,params_ref const & p,bool produce_proofs,bool produce_models,bool produce_unsat_cores,symbol const & logic)107 tactic2solver::tactic2solver(ast_manager & m, tactic * t, params_ref const & p, bool produce_proofs, bool produce_models, bool produce_unsat_cores, symbol const & logic):
108     solver_na2as(m),
109     m_assertions(m),
110     m_last_assertions(m),
111     m_last_assertions_valid(false) {
112 
113     m_tactic = t;
114     m_logic  = logic;
115     solver::updt_params(p);
116 
117     m_produce_models      = produce_models;
118     m_produce_proofs      = produce_proofs;
119     m_produce_unsat_cores = produce_unsat_cores;
120 }
121 
~tactic2solver()122 tactic2solver::~tactic2solver() {
123 }
124 
updt_params(params_ref const & p)125 void tactic2solver::updt_params(params_ref const & p) {
126     solver::updt_params(p);
127 }
128 
collect_param_descrs(param_descrs & r)129 void tactic2solver::collect_param_descrs(param_descrs & r) {
130     solver::collect_param_descrs(r);
131     if (m_tactic.get())
132         m_tactic->collect_param_descrs(r);
133 }
134 
assert_expr_core(expr * t)135 void tactic2solver::assert_expr_core(expr * t) {
136     m_last_assertions_valid = false;
137     m_assertions.push_back(t);
138     m_result = nullptr;
139 }
140 
141 
push_core()142 void tactic2solver::push_core() {
143     m_last_assertions_valid = false;
144     m_scopes.push_back(m_assertions.size());
145     m_result = nullptr;
146     TRACE("pop", tout << m_scopes.size() << "\n";);
147 }
148 
pop_core(unsigned n)149 void tactic2solver::pop_core(unsigned n) {
150     m_last_assertions_valid = false;
151     TRACE("pop", tout << m_scopes.size() << " " << n << "\n";);
152     n = std::min(m_scopes.size(), n);
153     unsigned new_lvl = m_scopes.size() - n;
154     unsigned old_sz  = m_scopes[new_lvl];
155     m_assertions.shrink(old_sz);
156     m_scopes.shrink(new_lvl);
157     m_result = nullptr;
158 }
159 
check_sat_core2(unsigned num_assumptions,expr * const * assumptions)160 lbool tactic2solver::check_sat_core2(unsigned num_assumptions, expr * const * assumptions) {
161     if (m_tactic.get() == nullptr)
162         return l_false;
163     m_last_assertions_valid = false;
164     ast_manager & m = m_assertions.m();
165     m_result = alloc(simple_check_sat_result, m);
166     m_tactic->cleanup();
167     m_tactic->set_logic(m_logic);
168     m_tactic->updt_params(get_params()); // parameters are allowed to overwrite logic.
169     goal_ref g = alloc(goal, m, m_produce_proofs, m_produce_models, m_produce_unsat_cores);
170 
171     for (expr* e : m_assertions) {
172         g->assert_expr(e);
173     }
174     for (unsigned i = 0; i < num_assumptions; i++) {
175         proof_ref pr(m.mk_asserted(assumptions[i]), m);
176         expr_dependency_ref ans(m.mk_leaf(assumptions[i]), m);
177         g->assert_expr(assumptions[i], pr, ans);
178     }
179 
180     model_ref           md;
181     proof_ref           pr(m);
182     expr_dependency_ref core(m);
183     std::string         reason_unknown = "unknown";
184     labels_vec labels;
185     TRACE("tactic", g->display(tout););
186     try {
187         switch (::check_sat(*m_tactic, g, md, labels, pr, core, reason_unknown)) {
188         case l_true:
189             m_result->set_status(l_true);
190             break;
191         case l_false:
192             m_result->set_status(l_false);
193             break;
194         default:
195             m_result->set_status(l_undef);
196             if (!reason_unknown.empty())
197                 m_result->m_unknown = reason_unknown;
198             if (num_assumptions == 0 && m_scopes.empty()) {
199                 m_last_assertions.reset();
200                 g->get_formulas(m_last_assertions);
201                 m_last_assertions_valid = true;
202             }
203             break;
204         }
205         CTRACE("tactic", md.get(), tout << *md.get() << "\n";);
206         TRACE("tactic",
207               if (m_mc) m_mc->display(tout << "mc:\n");
208               if (g->mc()) g->mc()->display(tout << "\ng:\n");
209               if (md) tout << "\nmodel:\n" << *md.get() << "\n";
210               );
211         m_mc = g->mc();
212 
213     }
214     catch (z3_error & ex) {
215         TRACE("tactic2solver", tout << "exception: " << ex.msg() << "\n";);
216         m_result->m_proof = pr;
217         throw ex;
218     }
219     catch (z3_exception & ex) {
220         TRACE("tactic2solver", tout << "exception: " << ex.msg() << "\n";);
221         m_result->set_status(l_undef);
222         m_result->m_unknown = ex.msg();
223         m_result->m_proof = pr;
224     }
225     m_tactic->collect_statistics(m_result->m_stats);
226     m_tactic->collect_statistics(m_stats);
227     m_result->m_model = md;
228     m_result->m_proof = pr;
229     if (m_produce_unsat_cores) {
230         ptr_vector<expr> core_elems;
231         m.linearize(core, core_elems);
232         m_result->m_core.append(core_elems.size(), core_elems.data());
233     }
234     m_tactic->cleanup();
235     return m_result->status();
236 }
237 
238 
translate(ast_manager & m,params_ref const & p)239 solver* tactic2solver::translate(ast_manager& m, params_ref const& p) {
240     tactic* t = m_tactic->translate(m);
241     tactic2solver* r = alloc(tactic2solver, m, t, p, m_produce_proofs, m_produce_models, m_produce_unsat_cores, m_logic);
242     r->m_result = nullptr;
243     if (!m_scopes.empty()) {
244         throw default_exception("translation of contexts is only supported at base level");
245     }
246     ast_translation tr(m_assertions.get_manager(), m, false);
247 
248     for (unsigned i = 0; i < get_num_assertions(); ++i) {
249         r->m_assertions.push_back(tr(get_assertion(i)));
250     }
251     return r;
252 }
253 
254 
collect_statistics(statistics & st) const255 void tactic2solver::collect_statistics(statistics & st) const {
256     st.copy(m_stats);
257     //SASSERT(m_stats.size() > 0);
258 }
259 
get_unsat_core(expr_ref_vector & r)260 void tactic2solver::get_unsat_core(expr_ref_vector & r) {
261     if (m_result.get()) {
262         m_result->get_unsat_core(r);
263     }
264 }
265 
get_model_core(model_ref & m)266 void tactic2solver::get_model_core(model_ref & m) {
267     if (m_result.get()) {
268         m_result->get_model_core(m);
269     }
270 }
271 
get_proof()272 proof * tactic2solver::get_proof() {
273     if (m_result.get())
274         return m_result->get_proof();
275     else
276         return nullptr;
277 }
278 
reason_unknown() const279 std::string tactic2solver::reason_unknown() const {
280     if (m_result.get())
281         return m_result->reason_unknown();
282     else
283         return std::string("unknown");
284 }
285 
set_reason_unknown(char const * msg)286 void tactic2solver::set_reason_unknown(char const* msg) {
287     if (m_result.get()) {
288         m_result->set_reason_unknown(msg);
289     }
290 }
291 
get_num_assertions() const292 unsigned tactic2solver::get_num_assertions() const {
293     return m_last_assertions_valid ? m_last_assertions.size() : m_assertions.size();
294 }
295 
get_assertion(unsigned idx) const296 expr * tactic2solver::get_assertion(unsigned idx) const {
297     return m_last_assertions_valid ? m_last_assertions.get(idx) : m_assertions.get(idx);
298 }
299 }
300 
301 
mk_tactic2solver(ast_manager & m,tactic * t,params_ref const & p,bool produce_proofs,bool produce_models,bool produce_unsat_cores,symbol const & logic)302 solver * mk_tactic2solver(ast_manager & m,
303                           tactic * t,
304                           params_ref const & p,
305                           bool produce_proofs,
306                           bool produce_models,
307                           bool produce_unsat_cores,
308                           symbol const & logic) {
309     return alloc(tactic2solver, m, t, p, produce_proofs, produce_models, produce_unsat_cores, logic);
310 }
311 
312 namespace {
313 class tactic2solver_factory : public solver_factory {
314     ref<tactic> m_tactic;
315 public:
tactic2solver_factory(tactic * t)316     tactic2solver_factory(tactic * t):m_tactic(t) {
317     }
318 
~tactic2solver_factory()319     ~tactic2solver_factory() override {}
320 
operator ()(ast_manager & m,params_ref const & p,bool proofs_enabled,bool models_enabled,bool unsat_core_enabled,symbol const & logic)321     solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) override {
322         return mk_tactic2solver(m, m_tactic.get(), p, proofs_enabled, models_enabled, unsat_core_enabled, logic);
323     }
324 };
325 
326 class tactic_factory2solver_factory : public solver_factory {
327     tactic_factory m_factory;
328 public:
tactic_factory2solver_factory(tactic_factory f)329     tactic_factory2solver_factory(tactic_factory f):m_factory(f) {
330     }
331 
operator ()(ast_manager & m,params_ref const & p,bool proofs_enabled,bool models_enabled,bool unsat_core_enabled,symbol const & logic)332     solver * operator()(ast_manager & m, params_ref const & p, bool proofs_enabled, bool models_enabled, bool unsat_core_enabled, symbol const & logic) override {
333         tactic * t = (*m_factory)(m, p);
334         return mk_tactic2solver(m, t, p, proofs_enabled, models_enabled, unsat_core_enabled, logic);
335     }
336 };
337 }
338 
mk_tactic2solver_factory(tactic * t)339 solver_factory * mk_tactic2solver_factory(tactic * t) {
340     return alloc(tactic2solver_factory, t);
341 }
342 
mk_tactic_factory2solver_factory(tactic_factory f)343 solver_factory * mk_tactic_factory2solver_factory(tactic_factory f) {
344     return alloc(tactic_factory2solver_factory, f);
345 }
346 
347 
348