CBMC
string_constraint.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Defines string constraints. These are formulas talking about strings.
4  We implemented two forms of constraints: `string_constraintt`
5  are formulas of the form $\forall univ_var \in [lb,ub[. prem => body$,
6  and not_contains_constraintt of the form:
7  $\forall x in [lb,ub[. p(x) => \exists y in [lb,ub[. s1[x+y] != s2[y]$.
8 
9 Author: Romain Brenguier, romain.brenguier@diffblue.com
10 
11 \*******************************************************************/
12 
19 
20 #ifndef CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_H
21 #define CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_H
22 
23 #include <util/format_expr.h>
24 #include <util/string_expr.h>
26 
27 class message_handlert;
28 
55 class string_constraintt final
56 {
57 public:
58  // String constraints are of the form
59  // forall univ_var in [lower_bound,upper_bound[. body
64 
66  const symbol_exprt &_univ_var,
67  const exprt &lower_bound,
68  const exprt &upper_bound,
69  const exprt &body,
70  message_handlert &message_handler);
71 
72  // Default bound inferior is 0
76  exprt body,
77  message_handlert &message_handler)
79  univ_var,
80  from_integer(0, univ_var.type()),
82  body,
83  message_handler)
84  {
85  }
86 
88  {
89  return and_exprt(
92  }
93 
94  void replace_expr(union_find_replacet &replace_map)
95  {
96  replace_map.replace_expr(lower_bound);
97  replace_map.replace_expr(upper_bound);
98  replace_map.replace_expr(body);
99  }
100 
101  exprt negation() const
102  {
104  }
105 };
106 
110 inline std::string to_string(const string_constraintt &expr)
111 {
112  std::ostringstream out;
113  out << "forall " << format(expr.univ_var) << " in ["
114  << format(expr.lower_bound) << ", " << format(expr.upper_bound) << "). "
115  << format(expr.body);
116  return out.str();
117 }
118 
127 {
135 };
136 
137 std::string to_string(const string_not_contains_constraintt &expr);
138 
139 void replace(
140  const union_find_replacet &replace_map,
141  string_not_contains_constraintt &constraint);
142 
143 bool operator==(
145  const string_not_contains_constraintt &right);
146 
147 // NOLINTNEXTLINE [allow specialization within 'std']
148 namespace std
149 {
150 template <>
151 // NOLINTNEXTLINE [struct identifier 'hash' does not end in t]
153 {
154  size_t operator()(const string_not_contains_constraintt &constraint) const
155  {
156  return irep_hash()(constraint.univ_lower_bound) ^
157  irep_hash()(constraint.univ_upper_bound) ^
158  irep_hash()(constraint.exists_lower_bound) ^
159  irep_hash()(constraint.exists_upper_bound) ^
160  irep_hash()(constraint.premise) ^ irep_hash()(constraint.s0) ^
161  irep_hash()(constraint.s1);
162  }
163 };
164 } // namespace std
165 
166 #endif
format
static format_containert< T > format(const T &o)
Definition: format.h:37
string_not_contains_constraintt::univ_upper_bound
exprt univ_upper_bound
Definition: string_constraint.h:129
to_string
std::string to_string(const string_constraintt &expr)
Used for debug printing.
Definition: string_constraint.h:110
union_find_replace.h
string_constraintt
Definition: string_constraint.h:55
union_find_replacet::replace_expr
bool replace_expr(exprt &expr) const
Replace subexpressions of expr by the representative element of the set they belong to.
Definition: union_find_replace.cpp:28
and_exprt
Boolean AND.
Definition: std_expr.h:2070
exprt
Base class for all expressions.
Definition: expr.h:55
string_not_contains_constraintt::exists_upper_bound
exprt exists_upper_bound
Definition: string_constraint.h:132
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
string_constraintt::univ_var
symbol_exprt univ_var
Definition: string_constraint.h:60
string_not_contains_constraintt::premise
exprt premise
Definition: string_constraint.h:130
string_constraintt::upper_bound
exprt upper_bound
Definition: string_constraint.h:62
string_constraintt::body
exprt body
Definition: string_constraint.h:63
string_constraintt::lower_bound
exprt lower_bound
Definition: string_constraint.h:61
string_not_contains_constraintt::exists_lower_bound
exprt exists_lower_bound
Definition: string_constraint.h:131
replace
void replace(const union_find_replacet &replace_map, string_not_contains_constraintt &constraint)
Definition: string_constraint.cpp:70
std::hash< string_not_contains_constraintt >::operator()
size_t operator()(const string_not_contains_constraintt &constraint) const
Definition: string_constraint.h:154
message_handlert
Definition: message.h:27
irep_hash
Definition: irep.h:476
string_constraintt::negation
exprt negation() const
Definition: string_constraint.h:101
string_constraintt::string_constraintt
string_constraintt(symbol_exprt univ_var, exprt upper_bound, exprt body, message_handlert &message_handler)
Definition: string_constraint.h:73
string_not_contains_constraintt::univ_lower_bound
exprt univ_lower_bound
Definition: string_constraint.h:128
string_constraintt::univ_within_bounds
exprt univ_within_bounds() const
Definition: string_constraint.h:87
format_expr.h
union_find_replacet
Similar interface to union-find for expressions, with a function for replacing sub-expressions by the...
Definition: union_find_replace.h:16
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
string_constraintt::string_constraintt
string_constraintt(const symbol_exprt &_univ_var, const exprt &lower_bound, const exprt &upper_bound, const exprt &body, message_handlert &message_handler)
Definition: string_constraint.cpp:34
binary_relation_exprt
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:706
string_not_contains_constraintt::s0
array_string_exprt s0
Definition: string_constraint.h:133
string_not_contains_constraintt
Constraints to encode non containement of strings.
Definition: string_constraint.h:126
string_not_contains_constraintt::s1
array_string_exprt s1
Definition: string_constraint.h:134
string_constraintt::replace_expr
void replace_expr(union_find_replacet &replace_map)
Definition: string_constraint.h:94
string_expr.h
array_string_exprt
Definition: string_expr.h:66
operator==
bool operator==(const string_not_contains_constraintt &left, const string_not_contains_constraintt &right)
Definition: string_constraint.cpp:83
not_exprt
Boolean negation.
Definition: std_expr.h:2277