Skip to content

Commit 48d065f

Browse files
author
Joel Allred
committed
Add invariants on the sign of string_constraintt bounds
Perform a sat_check the verify that the bounds of a string constraint cannot be negative.
1 parent 4b28bac commit 48d065f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/solvers/refinement/string_constraint.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88

99
#include "string_constraint.h"
1010

11+
#include <solvers/sat/satcheck.h>
12+
#include <util/symbol_table.h>
13+
14+
/// Runs a solver instance to verify whether an expression can only be
15+
// non-negative.
16+
/// \param expr: the expression to check for negativity
17+
/// \return true if `expr < 0` is unsatisfiable, false otherwise
18+
static bool cannot_be_neg(const exprt &expr)
19+
{
20+
satcheck_no_simplifiert sat_check;
21+
symbol_tablet symbol_table;
22+
namespacet ns(symbol_table);
23+
boolbvt solver(ns, sat_check);
24+
const exprt zero = from_integer(0, expr.type());
25+
const binary_relation_exprt non_neg(expr, ID_lt, zero);
26+
solver << non_neg;
27+
return solver() == decision_proceduret::resultt::D_UNSATISFIABLE;
28+
}
29+
1130
string_constraintt::string_constraintt(
1231
symbol_exprt _univ_var,
1332
exprt lower_bound,
@@ -18,4 +37,12 @@ string_constraintt::string_constraintt(
1837
upper_bound(upper_bound),
1938
body(body)
2039
{
40+
INVARIANT(
41+
cannot_be_neg(lower_bound),
42+
"String constraints must have non-negative lower bound.\n" +
43+
lower_bound.pretty());
44+
INVARIANT(
45+
cannot_be_neg(upper_bound),
46+
"String constraints must have non-negative upper bound.\n" +
47+
upper_bound.pretty());
2148
}

0 commit comments

Comments
 (0)