Skip to content

Commit 1ed8921

Browse files
author
Daniel Kroening
committed
smt2_solver: implement get-model
1 parent 64b4db3 commit 1ed8921

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
get-model1.smt2
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^sat$
7+
^\(\(define-fun var_x \(\) \(_ BitVec 8\) \(_ bv1 8\)\)$
8+
--
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(set-logic QF_BV)
2+
(set-option :produce-models true)
3+
4+
(declare-const var_x (_ BitVec 8)) ; nullary function
5+
(declare-const var_y (_ BitVec 8)) ; nullary function
6+
(declare-const var_z (_ BitVec 8)) ; nullary function
7+
8+
(assert (= var_x #x01))
9+
(assert (= var_y #x02))
10+
(assert (= var_z (bvadd var_x var_y)))
11+
12+
(check-sat)
13+
(get-model)

src/solvers/smt2/smt2_solver.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,43 @@ void smt2_solvert::command(const std::string &c)
262262
}
263263
std::cout << ')' << '\n';
264264
}
265+
else if(c == "get-model")
266+
{
267+
// print a model for all identifiers
268+
269+
if(status != SAT)
270+
throw error("model is not available");
271+
272+
const symbol_tablet symbol_table;
273+
const namespacet ns(symbol_table);
274+
275+
bool first = true;
276+
277+
std::cout << '(';
278+
for(const auto &id : id_map)
279+
{
280+
const symbol_exprt name(id.first, id.second.type);
281+
const auto value = simplify_expr(solver.get(name), ns);
282+
283+
if(value.is_not_nil())
284+
{
285+
if(first)
286+
first = false;
287+
else
288+
std::cout << '\n' << ' ';
289+
290+
std::cout << "(define-fun " << smt2_format(name) << ' ';
291+
292+
if(id.second.type.id() == ID_mathematical_function)
293+
throw error("models for functions unimplemented");
294+
else
295+
std::cout << "() " << smt2_format(id.second.type);
296+
297+
std::cout << ' ' << smt2_format(value) << ')';
298+
}
299+
}
300+
std::cout << ')' << '\n';
301+
}
265302
else if(c == "simplify")
266303
{
267304
// this is a command that Z3 appears to implement
@@ -287,7 +324,6 @@ void smt2_solvert::command(const std::string &c)
287324
| ( define-sort hsymboli ( hsymboli ??? ) hsorti )
288325
| ( get-assertions )
289326
| ( get-info hinfo_flag i )
290-
| ( get-model )
291327
| ( get-option hkeywordi )
292328
| ( get-proof )
293329
| ( get-unsat-assumptions )

0 commit comments

Comments
 (0)