Skip to content

Commit

Permalink
display model in add/del format
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
  • Loading branch information
NikolajBjorner committed Jun 7, 2022
1 parent a7b6f30 commit 0e6c645
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
41 changes: 27 additions & 14 deletions src/tactic/model_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ Module Name:
/*
* Add or overwrite value in model.
*/
void model_converter::display_add(std::ostream& out, ast_manager& m, func_decl* f, expr* e) const {
void model_converter::display_add(std::ostream& out, smt2_pp_environment& env, ast_manager& m, func_decl* f, expr* e) {
VERIFY(e);
smt2_pp_environment_dbg env(m);
smt2_pp_environment* _env = m_env ? m_env : &env;
VERIFY(f->get_range() == e->get_sort());
ast_smt2_pp(out, f, e, *_env, params_ref(), 0, "model-add") << "\n";
ast_smt2_pp(out, f, e, env, params_ref(), 0, "model-add") << "\n";
}

void model_converter::display_add(std::ostream& out, ast_manager& m, func_decl* f, expr* e) const {
smt2_pp_environment_dbg dbgenv(m);
smt2_pp_environment& env = m_env ? *m_env : dbgenv;
display_add(out, env, m, f, e);
}

/*
Expand Down Expand Up @@ -57,14 +61,22 @@ void model_converter::display_add(std::ostream& out, ast_manager& m) {
// default printer for converter that adds entries
model_ref mdl = alloc(model, m);
(*this)(mdl);
for (unsigned i = 0, sz = mdl->get_num_constants(); i < sz; ++i) {
func_decl* f = mdl->get_constant(i);
display_add(out, m, f, mdl->get_const_interp(f));
smt2_pp_environment_dbg dbgenv(m);
smt2_pp_environment& env = m_env ? *m_env : dbgenv;
display_add(out, env, *mdl);
}

void model_converter::display_add(std::ostream& out, smt2_pp_environment& env, model& mdl) {

ast_manager& m = mdl.get_manager();
for (unsigned i = 0, sz = mdl.get_num_constants(); i < sz; ++i) {
func_decl* f = mdl.get_constant(i);
display_add(out, env, m, f, mdl.get_const_interp(f));
}
for (unsigned i = 0, sz = mdl->get_num_functions(); i < sz; ++i) {
func_decl* f = mdl->get_function(i);
func_interp* fi = mdl->get_func_interp(f);
display_add(out, m, f, fi->get_interp());
for (unsigned i = 0, sz = mdl.get_num_functions(); i < sz; ++i) {
func_decl* f = mdl.get_function(i);
func_interp* fi = mdl.get_func_interp(f);
display_add(out, env, m, f, fi->get_interp());
}
}

Expand Down Expand Up @@ -153,9 +165,10 @@ class model2mc : public model_converter {
}

void display(std::ostream & out) override {
out << "(rmodel->model-converter-wrapper\n";
model_v2_pp(out, *m_model);
out << ")\n";
ast_manager& m = m_model->get_manager();
smt2_pp_environment_dbg dbgenv(m);
smt2_pp_environment& env = m_env ? *m_env : dbgenv;
model_converter::display_add(out, env, *m_model);
}

model_converter * translate(ast_translation & translator) override {
Expand Down
7 changes: 5 additions & 2 deletions src/tactic/model_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class smt2_pp_environment;

class model_converter : public converter {
protected:
smt2_pp_environment* m_env;
smt2_pp_environment* m_env;
static void display_add(std::ostream& out, smt2_pp_environment& env, ast_manager& m, func_decl* f, expr* e);
void display_add(std::ostream& out, ast_manager& m, func_decl* f, expr* e) const;
void display_del(std::ostream& out, func_decl* f) const;
void display_add(std::ostream& out, ast_manager& m);

public:

model_converter(): m_env(nullptr) {}
Expand All @@ -90,6 +90,9 @@ class model_converter : public converter {
*/

virtual void get_units(obj_map<expr, bool>& fmls) { UNREACHABLE(); }

static void display_add(std::ostream& out, smt2_pp_environment& env, model& mdl);

};

typedef ref<model_converter> model_converter_ref;
Expand Down

1 comment on commit 0e6c645

@NikolajBjorner
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When printing a solver state after pre-processing, the displayed solver state does not necessarily preserve models. To be able to reconstruct models of the original formula, z3 extends smtlib2 with model-add and model-del commands. For propositional clauses, model-add corresponds to a deleted clause with blocked literal. For example, if clause (a or b or c) is deleted and a is blocked, then it generates the (model-add a (or a (not (or b c)))) instruction.
Some of the pre-processing methods in z3 do not create a clean model-add/model-del command sequence (it is a legacy issue). It is possible for a pre-processing method to simply remove some symbols because they just occur in left side of equalities. The model converter for this case is just a "model" object. It was not printed with the model-add/model-del syntax and therefore the resulting SMTLIB2 solver state could not be parsed. This commit addresses this sub-case for model converter serialization.

Please sign in to comment.