Skip to content

Commit 8dbfae1

Browse files
author
Daniel Kroening
committed
simplify prop_conv_solvert::get_bool signature
The previous signature has used a boolean to indicate error; the result was returned by reference. The new variant returns the result and indicates error using an optional.
1 parent 53765c7 commit 8dbfae1

File tree

2 files changed

+34
-48
lines changed

2 files changed

+34
-48
lines changed

src/solvers/prop/prop_conv_solver.cpp

+30-46
Original file line numberDiff line numberDiff line change
@@ -42,91 +42,77 @@ literalt prop_conv_solvert::get_literal(const irep_idt &identifier)
4242
return literal;
4343
}
4444

45-
/// get a boolean value from counter example if not valid
46-
bool prop_conv_solvert::get_bool(const exprt &expr, tvt &value) const
45+
optionalt<bool> prop_conv_solvert::get_bool(const exprt &expr) const
4746
{
4847
// trivial cases
4948

5049
if(expr.is_true())
5150
{
52-
value = tvt(true);
53-
return false;
51+
return true;
5452
}
5553
else if(expr.is_false())
5654
{
57-
value = tvt(false);
5855
return false;
5956
}
6057
else if(expr.id() == ID_symbol)
6158
{
6259
symbolst::const_iterator result =
6360
symbols.find(to_symbol_expr(expr).get_identifier());
6461

62+
// This may fail if the symbol isn't Boolean or
63+
// not in the formula.
6564
if(result == symbols.end())
66-
return true;
65+
return {};
6766

68-
value = prop.l_get(result->second);
69-
return false;
67+
return prop.l_get(result->second).is_true();
7068
}
7169

7270
// sub-expressions
7371

7472
if(expr.id() == ID_not)
7573
{
76-
if(expr.type().id() == ID_bool && expr.operands().size() == 1)
74+
if(expr.type().id() == ID_bool)
7775
{
78-
if(get_bool(expr.op0(), value))
79-
return true;
80-
value = !value;
81-
return false;
76+
auto tmp = get_bool(to_not_expr(expr).op());
77+
if(tmp.has_value())
78+
return !*tmp;
79+
else
80+
return {};
8281
}
8382
}
8483
else if(expr.id() == ID_and || expr.id() == ID_or)
8584
{
8685
if(expr.type().id() == ID_bool && expr.operands().size() >= 1)
8786
{
88-
value = tvt(expr.id() == ID_and);
89-
9087
forall_operands(it, expr)
9188
{
92-
tvt tmp;
93-
if(get_bool(*it, tmp))
94-
return true;
89+
auto tmp = get_bool(*it);
90+
if(!tmp.has_value())
91+
return {};
9592

9693
if(expr.id() == ID_and)
9794
{
98-
if(tmp.is_false())
99-
{
100-
value = tvt(false);
95+
if(*tmp == false)
10196
return false;
102-
}
103-
104-
value = value && tmp;
10597
}
10698
else // or
10799
{
108-
if(tmp.is_true())
109-
{
110-
value = tvt(true);
111-
return false;
112-
}
113-
114-
value = value || tmp;
100+
if(*tmp == true)
101+
return true;
115102
}
116103
}
117104

118-
return false;
105+
return expr.id() == ID_and;
119106
}
120107
}
121108

122109
// check cache
123110

124111
cachet::const_iterator cache_result = cache.find(expr);
125112
if(cache_result == cache.end())
126-
return true;
127-
128-
value = prop.l_get(cache_result->second);
129-
return false;
113+
return {}; // not in formula
114+
else
115+
return prop.l_get(cache_result->second).is_true();
130116
}
131117

132118
literalt prop_conv_solvert::convert(const exprt &expr)
@@ -459,18 +445,16 @@ decision_proceduret::resultt prop_conv_solvert::dec_solve()
459445

460446
exprt prop_conv_solvert::get(const exprt &expr) const
461447
{
462-
tvt value;
463-
464-
if(expr.type().id() == ID_bool && !get_bool(expr, value))
448+
if(expr.type().id() == ID_bool)
465449
{
466-
switch(value.get_value())
450+
auto value = get_bool(expr);
451+
452+
if(value.has_value())
467453
{
468-
case tvt::tv_enumt::TV_TRUE:
469-
return true_exprt();
470-
case tvt::tv_enumt::TV_FALSE:
471-
return false_exprt();
472-
case tvt::tv_enumt::TV_UNKNOWN:
473-
return false_exprt(); // default
454+
if(*value)
455+
return true_exprt();
456+
else
457+
return false_exprt();
474458
}
475459
}
476460

src/solvers/prop/prop_conv_solver.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ class prop_conv_solvert : public prop_convt, public solver_resource_limitst
111111

112112
bool post_processing_done = false;
113113

114-
// get a _boolean_ value from counterexample if not valid
115-
virtual bool get_bool(const exprt &expr, tvt &value) const;
114+
/// Get a _boolean_ value from the model if the formula is satisfiable.
115+
/// If the argument is not a boolean expression from the formula,
116+
/// {} is returned.
117+
virtual optionalt<bool> get_bool(const exprt &expr) const;
116118

117119
virtual literalt convert_rest(const exprt &expr);
118120
virtual literalt convert_bool(const exprt &expr);

0 commit comments

Comments
 (0)