Skip to content

Commit f28976d

Browse files
thomasspriggsNlightNFotis
authored andcommitted
Refactor skolemisation
1 parent e675709 commit f28976d

File tree

2 files changed

+34
-58
lines changed

2 files changed

+34
-58
lines changed

src/goto-programs/skolemization.cpp

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,42 @@
66

77
#include "goto_model.h"
88

9-
#include <ansi-c/expr2c.h>
10-
11-
#include <util/expr_util.h>
129
#include <util/fresh_symbol.h>
1310
#include <util/irep.h> // for id2string
1411
#include <util/mathematical_expr.h>
1512

13+
/// Perform the first part of skolemisation, by identifying an existentially
14+
/// quantified statement and substituting the variables with skolem constants.
15+
/// \param condition The condition on which we search for quantifiers.
16+
/// \param symbol_table The symbol_table new symbols are inserted into.
17+
static void rename_variables_in_existential_quantifiers(
18+
exprt &condition,
19+
symbol_table_baset &symbol_table)
20+
{
21+
condition.visit_post([&symbol_table](exprt &sub_expression) {
22+
if(can_cast_expr<exists_exprt>(sub_expression))
23+
{
24+
quantifier_exprt &quantifier_expr = to_quantifier_expr(sub_expression);
25+
const auto &quantifier_sym = quantifier_expr.symbol();
26+
27+
const auto new_symbol = get_fresh_aux_symbol(
28+
quantifier_sym.type(),
29+
CPROVER_PREFIX,
30+
id2string(
31+
symbol_table.lookup(quantifier_sym.get_identifier())->base_name),
32+
quantifier_expr.source_location(),
33+
ID_C,
34+
symbol_table);
35+
const auto symbol_expr =
36+
symbol_exprt(new_symbol.name, quantifier_sym.type());
37+
const std::vector<exprt> new_sym_vec{symbol_expr};
38+
39+
sub_expression =
40+
exists_exprt(symbol_expr, quantifier_expr.instantiate(new_sym_vec));
41+
}
42+
});
43+
}
44+
1645
void rename_variables_in_existential_quantifiers(goto_modelt &goto_model)
1746
{
1847
for(auto &function : goto_model.goto_functions.function_map)
@@ -21,49 +50,9 @@ void rename_variables_in_existential_quantifiers(goto_modelt &goto_model)
2150
{
2251
if(instruction.has_condition())
2352
{
24-
auto &cond = instruction.condition_nonconst();
25-
recurse_on_condition(cond, goto_model);
53+
rename_variables_in_existential_quantifiers(
54+
instruction.condition_nonconst(), goto_model.symbol_table);
2655
}
2756
}
2857
}
2958
}
30-
31-
void recurse_on_condition(exprt &cond, goto_modelt &model)
32-
{
33-
// recurse on the condition's subexpressions first - that allows us
34-
// to correctly identify the nodes we care about in a compound
35-
// expression, for instance something like `1 == 1 && exists(i.i>=0 && i>1)`
36-
for(exprt &subexpr : cond.operands())
37-
{
38-
recurse_on_condition(subexpr, model);
39-
}
40-
41-
// if the expression we're in doesn't have any more operands,
42-
// it means it's a leaf node, so we go ahead and process it.
43-
rename_and_substitute(cond, model);
44-
}
45-
46-
void rename_and_substitute(exprt &cond, goto_modelt &model)
47-
{
48-
// perform the first part of skolemisation, by identifying an existentially
49-
// quantified statement and substituting the variables with skolem constants.
50-
if(can_cast_expr<exists_exprt>(cond))
51-
{
52-
quantifier_exprt &quantifier_expr = to_quantifier_expr(cond);
53-
const auto &quantifier_sym = quantifier_expr.symbol();
54-
55-
const auto new_symbol = get_fresh_aux_symbol(
56-
quantifier_sym.type(),
57-
CPROVER_PREFIX,
58-
id2string(
59-
model.symbol_table.lookup(quantifier_sym.get_identifier())->base_name),
60-
quantifier_expr.source_location(),
61-
ID_C,
62-
model.symbol_table);
63-
const auto symbol_expr =
64-
symbol_exprt(new_symbol.name, quantifier_sym.type());
65-
const std::vector<exprt> new_sym_vec{symbol_expr};
66-
67-
cond = exists_exprt(symbol_expr, quantifier_expr.instantiate(new_sym_vec));
68-
}
69-
}

src/goto-programs/skolemization.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,4 @@ class goto_modelt;
1414
/// for making changes to the symbol table at variable renaming time.
1515
void rename_variables_in_existential_quantifiers(goto_modelt &goto_model);
1616

17-
/// Helper function that recurses over the condition expression of instructions.
18-
/// \param cond The condition on which we recurse over the operands (subexpressions).
19-
/// \param model The goto_model we are inserting the new symbol to.
20-
void recurse_on_condition(exprt &cond, goto_modelt &model);
21-
22-
/// Helper function that does the actual renaming of the variables.
23-
/// This is called when we reach the atomic parts of the condition expression,
24-
/// to identify and rename the variables of any quantified statements that
25-
/// exist as part of the guard.
26-
/// \param cond The condition in which we are doing the renaming.
27-
/// \param model The goto_model we are inserting the new symbol to.
28-
void rename_and_substitute(exprt &cond, goto_modelt &model);
29-
3017
#endif

0 commit comments

Comments
 (0)