|
11 | 11 | #include "namespace.h"
|
12 | 12 | #include "range.h"
|
13 | 13 |
|
| 14 | +#include <map> |
| 15 | + |
14 | 16 | bool constant_exprt::value_is_zero_string() const
|
15 | 17 | {
|
16 | 18 | const std::string val=id2string(get_value());
|
@@ -134,3 +136,130 @@ void let_exprt::validate(const exprt &expr, const validation_modet vm)
|
134 | 136 | "let bindings must be type consistent");
|
135 | 137 | }
|
136 | 138 | }
|
| 139 | + |
| 140 | +static optionalt<exprt> substitute_symbols_rec( |
| 141 | + const std::map<irep_idt, exprt> &substitutions, |
| 142 | + exprt src) |
| 143 | +{ |
| 144 | + if(src.id() == ID_symbol) |
| 145 | + { |
| 146 | + auto s_it = substitutions.find(to_symbol_expr(src).get_identifier()); |
| 147 | + if(s_it == substitutions.end()) |
| 148 | + return {}; |
| 149 | + else |
| 150 | + return s_it->second; |
| 151 | + } |
| 152 | + else if(src.id() == ID_forall || src.id() == ID_exists || |
| 153 | + src.id() == ID_lambda) |
| 154 | + { |
| 155 | + const auto &binding_expr = to_binding_expr(src); |
| 156 | + |
| 157 | + // bindings may be nested, |
| 158 | + // which may hide some of our substitutions |
| 159 | + auto new_substitutions = substitutions; |
| 160 | + for(const auto &variable : binding_expr.variables()) |
| 161 | + new_substitutions.erase(variable.get_identifier()); |
| 162 | + |
| 163 | + auto op_result = substitute_symbols_rec(new_substitutions, binding_expr.where()); |
| 164 | + if(op_result.has_value()) |
| 165 | + return binding_exprt(src.id(), binding_expr.variables(), op_result.value(), binding_expr.type()); |
| 166 | + else |
| 167 | + return {}; |
| 168 | + } |
| 169 | + else if(src.id() == ID_let) |
| 170 | + { |
| 171 | + auto new_let_expr = to_let_expr(src); // copy |
| 172 | + const auto &binding_expr = to_let_expr(src).binding(); |
| 173 | + |
| 174 | + // bindings may be nested, |
| 175 | + // which may hide some of our substitutions |
| 176 | + auto new_substitutions = substitutions; |
| 177 | + for(const auto &variable : binding_expr.variables()) |
| 178 | + new_substitutions.erase(variable.get_identifier()); |
| 179 | + |
| 180 | + bool op_changed = false; |
| 181 | + |
| 182 | + for(auto &op : new_let_expr.values()) |
| 183 | + { |
| 184 | + auto op_result = substitute_symbols_rec(new_substitutions, op); |
| 185 | + |
| 186 | + if(op_result.has_value()) |
| 187 | + { |
| 188 | + op = op_result.value(); |
| 189 | + op_changed = true; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + auto op_result = substitute_symbols_rec(new_substitutions, binding_expr.where()); |
| 194 | + if(op_result.has_value()) |
| 195 | + { |
| 196 | + new_let_expr.where() = op_result.value(); |
| 197 | + op_changed = true; |
| 198 | + } |
| 199 | + |
| 200 | + if(op_changed) |
| 201 | + return std::move(new_let_expr); |
| 202 | + else |
| 203 | + return {}; |
| 204 | + } |
| 205 | + |
| 206 | + if(!src.has_operands()) |
| 207 | + return {}; |
| 208 | + |
| 209 | + bool op_changed = false; |
| 210 | + |
| 211 | + for(auto &op : src.operands()) |
| 212 | + { |
| 213 | + auto op_result = substitute_symbols_rec(substitutions, op); |
| 214 | + |
| 215 | + if(op_result.has_value()) |
| 216 | + { |
| 217 | + op = op_result.value(); |
| 218 | + op_changed = true; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + if(op_changed) |
| 223 | + return src; |
| 224 | + else |
| 225 | + return {}; |
| 226 | +} |
| 227 | + |
| 228 | +exprt binding_exprt::instantiate(const operandst &values) const |
| 229 | +{ |
| 230 | + // number of values must match the number of bound variables |
| 231 | + auto &variables = this->variables(); |
| 232 | + PRECONDITION(variables.size() == values.size()); |
| 233 | + |
| 234 | + std::map<symbol_exprt, exprt> value_map; |
| 235 | + |
| 236 | + for(std::size_t i = 0; i < variables.size(); i++) |
| 237 | + { |
| 238 | + // types must match |
| 239 | + PRECONDITION(variables[i].type() == values[i].type()); |
| 240 | + value_map[variables[i]] = values[i]; |
| 241 | + } |
| 242 | + |
| 243 | + // build a subsitution map |
| 244 | + std::map<irep_idt, exprt> substitutions; |
| 245 | + |
| 246 | + for(std::size_t i = 0; i < variables.size(); i++) |
| 247 | + substitutions[variables[i].get_identifier()] = values[i]; |
| 248 | + |
| 249 | + // now recurse downwards and substitute in 'where' |
| 250 | + auto substitute_result = substitute_symbols_rec(substitutions, where()); |
| 251 | + |
| 252 | + if(substitute_result.has_value()) |
| 253 | + return substitute_result.value(); |
| 254 | + else |
| 255 | + return where(); // trivial case, variables not used |
| 256 | +} |
| 257 | + |
| 258 | +exprt binding_exprt::instantiate(const variablest &new_variables) const |
| 259 | +{ |
| 260 | + std::vector<exprt> values; |
| 261 | + values.reserve(new_variables.size()); |
| 262 | + for(const auto &new_variable : new_variables) |
| 263 | + values.push_back(new_variable); |
| 264 | + return instantiate(values); |
| 265 | +} |
0 commit comments