-
Notifications
You must be signed in to change notification settings - Fork 274
SMT2 solver: implement get-value #3461
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,13 @@ Author: Daniel Kroening, [email protected] | |
|
||
\*******************************************************************/ | ||
|
||
#include "smt2_parser.h" | ||
|
||
#include "smt2_format.h" | ||
|
||
#include <fstream> | ||
#include <iostream> | ||
|
||
#include "smt2_parser.h" | ||
|
||
#include <util/message.h> | ||
#include <util/namespace.h> | ||
#include <util/replace_symbol.h> | ||
|
@@ -23,11 +25,8 @@ Author: Daniel Kroening, [email protected] | |
class smt2_solvert:public smt2_parsert | ||
{ | ||
public: | ||
smt2_solvert( | ||
std::istream &_in, | ||
decision_proceduret &_solver): | ||
smt2_parsert(_in), | ||
solver(_solver) | ||
smt2_solvert(std::istream &_in, decision_proceduret &_solver) | ||
: smt2_parsert(_in), solver(_solver), status(NOT_SOLVED) | ||
{ | ||
} | ||
|
||
|
@@ -39,6 +38,13 @@ class smt2_solvert:public smt2_parsert | |
void expand_function_applications(exprt &); | ||
|
||
std::set<irep_idt> constants_done; | ||
|
||
enum | ||
{ | ||
NOT_SOLVED, | ||
SAT, | ||
UNSAT | ||
} status; | ||
}; | ||
|
||
void smt2_solvert::define_constants(const exprt &expr) | ||
|
@@ -139,14 +145,17 @@ void smt2_solvert::command(const std::string &c) | |
{ | ||
case decision_proceduret::resultt::D_SATISFIABLE: | ||
std::cout << "sat\n"; | ||
status = SAT; | ||
break; | ||
|
||
case decision_proceduret::resultt::D_UNSATISFIABLE: | ||
std::cout << "unsat\n"; | ||
status = UNSAT; | ||
break; | ||
|
||
case decision_proceduret::resultt::D_ERROR: | ||
std::cout << "error\n"; | ||
status = NOT_SOLVED; | ||
} | ||
} | ||
else if(c=="check-sat-assuming") | ||
|
@@ -162,6 +171,63 @@ void smt2_solvert::command(const std::string &c) | |
std::cout << e.pretty() << '\n'; // need to do an 'smt2_format' | ||
} | ||
} | ||
else if(c == "get-value") | ||
{ | ||
std::vector<exprt> ops; | ||
|
||
if(next_token() != OPEN) | ||
throw "get-value expects list as argument"; | ||
|
||
while(peek() != CLOSE && peek() != END_OF_FILE) | ||
ops.push_back(expression()); // any term | ||
|
||
if(next_token() != CLOSE) | ||
throw "get-value expects ')' at end of list"; | ||
|
||
if(status != SAT) | ||
throw "model is not available"; | ||
|
||
std::vector<exprt> values; | ||
values.reserve(ops.size()); | ||
|
||
for(const auto &op : ops) | ||
{ | ||
if(op.id() != ID_symbol) | ||
throw "get-value expects symbol"; | ||
|
||
const auto &identifier = to_symbol_expr(op).get_identifier(); | ||
|
||
const auto id_map_it = id_map.find(identifier); | ||
|
||
if(id_map_it == id_map.end()) | ||
throw "unexpected symbol " + id2string(identifier); | ||
|
||
exprt value; | ||
|
||
if(id_map_it->second.definition.is_nil()) | ||
value = solver.get(op); | ||
else | ||
value = solver.get(id_map_it->second.definition); | ||
|
||
if(value.is_nil()) | ||
throw "no value for " + id2string(identifier); | ||
|
||
values.push_back(value); | ||
} | ||
|
||
std::cout << '('; | ||
|
||
for(std::size_t op_nr = 0; op_nr < ops.size(); op_nr++) | ||
{ | ||
if(op_nr != 0) | ||
std::cout << "\n "; | ||
|
||
std::cout << '(' << smt2_format(ops[op_nr]) << ' ' | ||
<< smt2_format(values[op_nr]) << ')'; | ||
} | ||
|
||
std::cout << ")\n"; | ||
} | ||
else if(c=="simplify") | ||
{ | ||
// this is a command that Z3 appears to implement | ||
|
@@ -195,7 +261,6 @@ void smt2_solvert::command(const std::string &c) | |
| ( get-proof ) | ||
| ( get-unsat-assumptions ) | ||
| ( get-unsat-core ) | ||
| ( get-value ( htermi + ) ) | ||
| ( pop hnumerali ) | ||
| ( push hnumerali ) | ||
| ( reset ) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need yet another status enum? We have one for decision procedures, one in prop.h - wouldn't the latter be the right one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No; this isn't a result from a solver, but the state of the SMT2-LIB protocol between the solver and the client. The key question is whether (check-sat) has been called or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe such things could be documented in future? The code only did a straightforward translation from enum values of one type to enum values of another type.