Skip to content

SMT2: get-assignment #3503

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 3 commits 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-assignment/get-assignment1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
get-assignment1.smt2

^EXIT=0$
^SIGNAL=0$
^sat$
^\(\(y_equality true\)\)$
--
13 changes: 13 additions & 0 deletions regression/smt2_solver/get-assignment/get-assignment1.smt2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(set-logic QF_BV)
(set-option :produce-assignments 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) :named y_equality))
(assert (= var_z (bvadd var_x var_y)))

(check-sat)
(get-assignment)
33 changes: 33 additions & 0 deletions src/solvers/smt2/smt2_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,39 @@ exprt smt2_parsert::function_application()
throw error(msg.str());
}
}
else if(buffer == "!")
{
// these are "term attributes"
const auto term = expression();

while(peek() == KEYWORD)
{
next_token(); // eat the keyword
if(buffer == "named")
{
// 'named terms' must be Boolean
if(term.type().id() != ID_bool)
throw error("named terms must be Boolean");

if(next_token() == SYMBOL)
{
const symbol_exprt symbol_expr(buffer, bool_typet());
auto &named_term = named_terms[symbol_expr.get_identifier()];
named_term.term = term;
named_term.name = symbol_expr;
}
else
throw error("invalid name attribute, expected symbol");
}
else
throw error("unknown term attribute");
}

if(next_token() != CLOSE)
throw error("expected ')' at end of term attribute");
else
return term;
}
else
{
// non-indexed symbol; hash it
Expand Down
9 changes: 9 additions & 0 deletions src/solvers/smt2/smt2_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class smt2_parsert:public smt2_tokenizert
using id_mapt=std::map<irep_idt, idt>;
id_mapt id_map;

struct named_termt
{
exprt term;
symbol_exprt name;
};

using named_termst = std::map<irep_idt, named_termt>;
named_termst named_terms;

bool exit;

/// This skips tokens until all bracketed expressions are closed
Expand Down
31 changes: 30 additions & 1 deletion src/solvers/smt2/smt2_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,36 @@ void smt2_solvert::command(const std::string &c)

std::cout << smt2_format(constant_exprt(buffer, string_typet())) << '\n';
}
else if(c == "get-assignment")
{
// print satisfying assignment for all named expressions

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

bool first = true;

std::cout << '(';
for(const auto &named_term : named_terms)
{
const symbol_tablet symbol_table;
const namespacet ns(symbol_table);
const auto value =
simplify_expr(solver.get(named_term.second.term), ns);

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

std::cout << '(' << smt2_format(named_term.second.name) << ' '
<< smt2_format(value) << ')';
}
}
std::cout << ')' << '\n';
}
else if(c == "simplify")
{
// this is a command that Z3 appears to implement
Expand All @@ -256,7 +286,6 @@ void smt2_solvert::command(const std::string &c)
| ( define-funs-rec ( hfunction_deci n+1 ) ( htermi n+1 ) )
| ( define-sort hsymboli ( hsymboli ??? ) hsorti )
| ( get-assertions )
| ( get-assignment )
| ( get-info hinfo_flag i )
| ( get-model )
| ( get-option hkeywordi )
Expand Down
8 changes: 7 additions & 1 deletion src/solvers/smt2/smt2_tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ void smt2_tokenizert::get_token_from_stream()

case ':': // keyword
token = get_simple_symbol();
return;
if(token == SYMBOL)
{
token = KEYWORD;
return;
}
else
throw error("expecting symbol after colon");

case '#':
if(in->get(ch))
Expand Down
1 change: 1 addition & 0 deletions src/solvers/smt2/smt2_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class smt2_tokenizert:public parsert
STRING_LITERAL,
NUMERAL,
SYMBOL,
KEYWORD,
OPEN,
CLOSE
};
Expand Down