Skip to content

smt2_solver: implement get-model #3515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions regression/smt2_solver/get-model/get-model1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
get-model1.smt2

^EXIT=0$
^SIGNAL=0$
^sat$
^\(\(define-fun var_x \(\) \(_ BitVec 8\) \(_ bv1 8\)\)$
--
13 changes: 13 additions & 0 deletions regression/smt2_solver/get-model/get-model1.smt2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(set-logic QF_BV)
(set-option :produce-models true)

(declare-const var_x (_ BitVec 8)) ; nullary function
(declare-const var_y (_ BitVec 8)) ; nullary function
(declare-const var_z (_ BitVec 8)) ; nullary function

(assert (= var_x #x01))
(assert (= var_y #x02))
(assert (= var_z (bvadd var_x var_y)))

(check-sat)
(get-model)
38 changes: 37 additions & 1 deletion src/solvers/smt2/smt2_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,43 @@ void smt2_solvert::command(const std::string &c)
}
std::cout << ')' << '\n';
}
else if(c == "get-model")
{
// print a model for all identifiers

if(status != SAT)
throw error("model is not available");

const symbol_tablet symbol_table;
const namespacet ns(symbol_table);

bool first = true;

std::cout << '(';
for(const auto &id : id_map)
{
const symbol_exprt name(id.first, id.second.type);
const auto value = simplify_expr(solver.get(name), ns);

if(value.is_not_nil())
{
if(first)
first = false;
else
std::cout << '\n' << ' ';

std::cout << "(define-fun " << smt2_format(name) << ' ';

if(id.second.type.id() == ID_mathematical_function)
throw error("models for functions unimplemented");
else
std::cout << "() " << smt2_format(id.second.type);

std::cout << ' ' << smt2_format(value) << ')';
}
}
std::cout << ')' << '\n';
}
else if(c == "simplify")
{
// this is a command that Z3 appears to implement
Expand All @@ -287,7 +324,6 @@ void smt2_solvert::command(const std::string &c)
| ( define-sort hsymboli ( hsymboli ??? ) hsorti )
| ( get-assertions )
| ( get-info hinfo_flag i )
| ( get-model )
| ( get-option hkeywordi )
| ( get-proof )
| ( get-unsat-assumptions )
Expand Down