Skip to content

Commit 3509cc3

Browse files
authored
Merge pull request diffblue#6191 from diffblue/smt2_use_lambda
SMT2 parser: use lambda for function defintions
2 parents 5d79499 + 92d4a88 commit 3509cc3

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/solvers/smt2/smt2_parser.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ exprt::operandst smt2_parsert::operands()
131131
return result;
132132
}
133133

134-
void smt2_parsert::add_unique_id(const irep_idt &id, const exprt &expr)
134+
void smt2_parsert::add_unique_id(irep_idt id, exprt expr)
135135
{
136136
if(!id_map
137137
.emplace(
138138
std::piecewise_construct,
139139
std::forward_as_tuple(id),
140-
std::forward_as_tuple(idt::VARIABLE, expr))
140+
std::forward_as_tuple(idt::VARIABLE, std::move(expr)))
141141
.second)
142142
{
143143
// id already used
@@ -1484,7 +1484,7 @@ void smt2_parsert::setup_commands()
14841484
}
14851485

14861486
// now parse body with parameter ids in place
1487-
const auto body = expression();
1487+
auto body = expression();
14881488

14891489
// remove the parameter ids
14901490
for(auto &id : signature.parameters)
@@ -1512,11 +1512,12 @@ void smt2_parsert::setup_commands()
15121512
<< smt2_format(body.type()) << '\'';
15131513
}
15141514

1515-
// create the entry
1516-
add_unique_id(id, body);
1515+
// if there are parameters, this is a lambda
1516+
if(!signature.parameters.empty())
1517+
body = lambda_exprt(signature.binding_variables(), body);
15171518

1518-
id_map.at(id).type = signature.type;
1519-
id_map.at(id).parameters = signature.parameters;
1519+
// create the entry
1520+
add_unique_id(id, std::move(body));
15201521
};
15211522

15221523
commands["exit"] = [this]() { exit = true; };

src/solvers/smt2/smt2_parser.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class smt2_parsert
4949

5050
kindt kind;
5151
typet type;
52+
53+
// this is a lambda when the symbol is a function
5254
exprt definition;
53-
std::vector<irep_idt> parameters;
5455
};
5556

5657
using id_mapt=std::map<irep_idt, idt>;
@@ -95,7 +96,7 @@ class smt2_parsert
9596

9697
// add the given identifier to the id_map but
9798
// complain if that identifier is used already
98-
void add_unique_id(const irep_idt &, const exprt &);
99+
void add_unique_id(irep_idt, exprt);
99100

100101
struct signature_with_parameter_idst
101102
{
@@ -133,6 +134,15 @@ class smt2_parsert
133134
return result;
134135
}
135136
}
137+
138+
// convenience helper for constructing a binding
139+
binding_exprt::variablest binding_variables() const
140+
{
141+
binding_exprt::variablest result;
142+
for(auto &pair : ids_and_types())
143+
result.emplace_back(pair.first, pair.second);
144+
return result;
145+
}
136146
};
137147

138148
// expressions

src/solvers/smt2/smt2_solver.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,12 @@ void smt2_solvert::expand_function_applications(exprt &expr)
105105
// Does it have a definition? It's otherwise uninterpreted.
106106
if(!f.definition.is_nil())
107107
{
108-
replace_symbolt replace_symbol;
108+
exprt body = f.definition;
109109

110-
for(std::size_t i = 0; i < domain.size(); i++)
111-
{
112-
replace_symbol.insert(
113-
symbol_exprt(f.parameters[i], domain[i]), app.arguments()[i]);
114-
}
110+
if(body.id() == ID_lambda)
111+
body = to_lambda_expr(body).application(app.arguments());
115112

116-
exprt body = f.definition;
117-
replace_symbol(body);
118-
expand_function_applications(body);
113+
expand_function_applications(body); // rec. call
119114
expr = body;
120115
}
121116
}

0 commit comments

Comments
 (0)