-
Notifications
You must be signed in to change notification settings - Fork 273
Introduce binding_exprt::instantiate #6192
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 all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ Author: Daniel Kroening, [email protected] | |
#include <util/optional.h> | ||
#include <util/range.h> | ||
#include <util/replace_expr.h> | ||
#include <util/replace_symbol.h> | ||
#include <util/simplify_expr.h> | ||
|
||
/// A method to detect equivalence between experts that can contain typecast | ||
|
@@ -212,17 +211,11 @@ literalt boolbvt::convert_quantifier(const quantifier_exprt &src) | |
auto fresh_symbols = fresh_binding(src); | ||
|
||
// replace in where() | ||
replace_symbolt replace_symbol; | ||
|
||
for(const auto &pair : make_range(src.variables()).zip(fresh_symbols)) | ||
replace_symbol.insert(pair.first, pair.second); | ||
|
||
exprt renamed_where = src.where(); | ||
replace_symbol(renamed_where); | ||
auto where_replaced = src.instantiate(fresh_symbols); | ||
|
||
// produce new quantifier expression | ||
auto new_src = | ||
quantifier_exprt(src.id(), std::move(fresh_symbols), renamed_where); | ||
quantifier_exprt(src.id(), std::move(fresh_symbols), where_replaced); | ||
|
||
const auto res = instantiate_quantifier(src, ns); | ||
|
||
|
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
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 |
---|---|---|
|
@@ -11,6 +11,8 @@ Author: Daniel Kroening, [email protected] | |
#include "namespace.h" | ||
#include "range.h" | ||
|
||
#include <map> | ||
|
||
bool constant_exprt::value_is_zero_string() const | ||
{ | ||
const std::string val=id2string(get_value()); | ||
|
@@ -134,3 +136,136 @@ void let_exprt::validate(const exprt &expr, const validation_modet vm) | |
"let bindings must be type consistent"); | ||
} | ||
} | ||
|
||
static optionalt<exprt> substitute_symbols_rec( | ||
const std::map<irep_idt, exprt> &substitutions, | ||
exprt src) | ||
{ | ||
if(src.id() == ID_symbol) | ||
{ | ||
auto s_it = substitutions.find(to_symbol_expr(src).get_identifier()); | ||
if(s_it == substitutions.end()) | ||
return {}; | ||
else | ||
return s_it->second; | ||
} | ||
else if( | ||
src.id() == ID_forall || src.id() == ID_exists || src.id() == ID_lambda) | ||
{ | ||
const auto &binding_expr = to_binding_expr(src); | ||
|
||
// bindings may be nested, | ||
// which may hide some of our substitutions | ||
auto new_substitutions = substitutions; | ||
for(const auto &variable : binding_expr.variables()) | ||
new_substitutions.erase(variable.get_identifier()); | ||
|
||
auto op_result = | ||
substitute_symbols_rec(new_substitutions, binding_expr.where()); | ||
if(op_result.has_value()) | ||
return binding_exprt( | ||
src.id(), | ||
binding_expr.variables(), | ||
op_result.value(), | ||
binding_expr.type()); | ||
else | ||
return {}; | ||
} | ||
else if(src.id() == ID_let) | ||
{ | ||
auto new_let_expr = to_let_expr(src); // copy | ||
const auto &binding_expr = to_let_expr(src).binding(); | ||
|
||
// bindings may be nested, | ||
// which may hide some of our substitutions | ||
auto new_substitutions = substitutions; | ||
for(const auto &variable : binding_expr.variables()) | ||
new_substitutions.erase(variable.get_identifier()); | ||
|
||
bool op_changed = false; | ||
|
||
for(auto &op : new_let_expr.values()) | ||
{ | ||
auto op_result = substitute_symbols_rec(new_substitutions, op); | ||
|
||
if(op_result.has_value()) | ||
{ | ||
op = op_result.value(); | ||
op_changed = true; | ||
} | ||
} | ||
|
||
auto op_result = | ||
substitute_symbols_rec(new_substitutions, binding_expr.where()); | ||
if(op_result.has_value()) | ||
{ | ||
new_let_expr.where() = op_result.value(); | ||
op_changed = true; | ||
} | ||
|
||
if(op_changed) | ||
return std::move(new_let_expr); | ||
else | ||
return {}; | ||
} | ||
|
||
if(!src.has_operands()) | ||
return {}; | ||
|
||
bool op_changed = false; | ||
|
||
for(auto &op : src.operands()) | ||
{ | ||
auto op_result = substitute_symbols_rec(substitutions, op); | ||
|
||
if(op_result.has_value()) | ||
{ | ||
op = op_result.value(); | ||
op_changed = true; | ||
} | ||
} | ||
|
||
if(op_changed) | ||
return src; | ||
else | ||
return {}; | ||
} | ||
|
||
exprt binding_exprt::instantiate(const operandst &values) const | ||
{ | ||
// number of values must match the number of bound variables | ||
auto &variables = this->variables(); | ||
PRECONDITION(variables.size() == values.size()); | ||
|
||
std::map<symbol_exprt, exprt> value_map; | ||
|
||
for(std::size_t i = 0; i < variables.size(); i++) | ||
{ | ||
// types must match | ||
PRECONDITION(variables[i].type() == values[i].type()); | ||
value_map[variables[i]] = values[i]; | ||
} | ||
|
||
// build a substitution map | ||
std::map<irep_idt, exprt> substitutions; | ||
|
||
for(std::size_t i = 0; i < variables.size(); i++) | ||
substitutions[variables[i].get_identifier()] = values[i]; | ||
|
||
// now recurse downwards and substitute in 'where' | ||
auto substitute_result = substitute_symbols_rec(substitutions, where()); | ||
|
||
if(substitute_result.has_value()) | ||
return substitute_result.value(); | ||
else | ||
return where(); // trivial case, variables not used | ||
} | ||
|
||
exprt binding_exprt::instantiate(const variablest &new_variables) const | ||
{ | ||
std::vector<exprt> values; | ||
values.reserve(new_variables.size()); | ||
for(const auto &new_variable : new_variables) | ||
values.push_back(new_variable); | ||
return instantiate(values); | ||
} |
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
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.
Is it necessary to pass by value here (a copy is taken below)?
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.
The copy happens in most cases -- so this is the pattern "use pass by value when copy is inevitable".