|
| 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, [email protected] |
| 10 | +
|
| 11 | +\*******************************************************************/ |
| 12 | + |
| 13 | +#ifndef CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_H |
| 14 | +#define CPROVER_SOLVERS_REFINEMENT_STRING_CONSTRAINT_H |
| 15 | + |
| 16 | +#include <langapi/language_ui.h> |
| 17 | +#include <solvers/refinement/bv_refinement.h> |
| 18 | +#include <solvers/refinement/refined_string_type.h> |
| 19 | + |
| 20 | +class string_constraintt: public exprt |
| 21 | +{ |
| 22 | +public: |
| 23 | + // String constraints are of the form |
| 24 | + // forall univ_var in [lower_bound,upper_bound[. premise => body |
| 25 | + |
| 26 | + const exprt &premise() const |
| 27 | + { |
| 28 | + return op0(); |
| 29 | + } |
| 30 | + |
| 31 | + const exprt &body() const |
| 32 | + { |
| 33 | + return op1(); |
| 34 | + } |
| 35 | + |
| 36 | + const symbol_exprt &univ_var() const |
| 37 | + { |
| 38 | + return to_symbol_expr(op2()); |
| 39 | + } |
| 40 | + |
| 41 | + const exprt &upper_bound() const |
| 42 | + { |
| 43 | + return op3(); |
| 44 | + } |
| 45 | + |
| 46 | + const exprt &lower_bound() const |
| 47 | + { |
| 48 | + return operands()[4]; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + private: |
| 53 | + string_constraintt(); |
| 54 | + |
| 55 | + public: |
| 56 | + string_constraintt( |
| 57 | + const symbol_exprt &_univ_var, |
| 58 | + const exprt &bound_inf, |
| 59 | + const exprt &bound_sup, |
| 60 | + const exprt &prem, |
| 61 | + const exprt &body): |
| 62 | + exprt(ID_string_constraint) |
| 63 | + { |
| 64 | + copy_to_operands(prem, body); |
| 65 | + copy_to_operands(_univ_var, bound_sup, bound_inf); |
| 66 | + } |
| 67 | + |
| 68 | + // Default bound inferior is 0 |
| 69 | + string_constraintt( |
| 70 | + const symbol_exprt &_univ_var, |
| 71 | + const exprt &bound_sup, |
| 72 | + const exprt &prem, |
| 73 | + const exprt &body): |
| 74 | + string_constraintt( |
| 75 | + _univ_var, |
| 76 | + from_integer(0, _univ_var.type()), |
| 77 | + bound_sup, |
| 78 | + prem, |
| 79 | + body) |
| 80 | + {} |
| 81 | + |
| 82 | + // Default premise is true |
| 83 | + string_constraintt( |
| 84 | + const symbol_exprt &_univ_var, |
| 85 | + const exprt &bound_sup, |
| 86 | + const exprt &body): |
| 87 | + string_constraintt(_univ_var, bound_sup, true_exprt(), body) |
| 88 | + {} |
| 89 | + |
| 90 | + exprt univ_within_bounds() const |
| 91 | + { |
| 92 | + return and_exprt( |
| 93 | + binary_relation_exprt(lower_bound(), ID_le, univ_var()), |
| 94 | + binary_relation_exprt(upper_bound(), ID_gt, univ_var())); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +extern inline const string_constraintt &to_string_constraint(const exprt &expr) |
| 99 | +{ |
| 100 | + assert(expr.id()==ID_string_constraint && expr.operands().size()==5); |
| 101 | + return static_cast<const string_constraintt &>(expr); |
| 102 | +} |
| 103 | + |
| 104 | +extern inline string_constraintt &to_string_constraint(exprt &expr) |
| 105 | +{ |
| 106 | + assert(expr.id()==ID_string_constraint && expr.operands().size()==5); |
| 107 | + return static_cast<string_constraintt &>(expr); |
| 108 | +} |
| 109 | + |
| 110 | +class string_not_contains_constraintt: public exprt |
| 111 | +{ |
| 112 | +public: |
| 113 | + // string_not contains_constraintt are formula of the form: |
| 114 | + // forall x in [lb,ub[. p(x) => exists y in [lb,ub[. s1[x+y] != s2[y] |
| 115 | + |
| 116 | + string_not_contains_constraintt( |
| 117 | + exprt univ_lower_bound, |
| 118 | + exprt univ_bound_sup, |
| 119 | + exprt premise, |
| 120 | + exprt exists_bound_inf, |
| 121 | + exprt exists_bound_sup, |
| 122 | + exprt s0, |
| 123 | + exprt s1) : |
| 124 | + exprt(ID_string_not_contains_constraint) |
| 125 | + { |
| 126 | + copy_to_operands(univ_lower_bound, univ_bound_sup, premise); |
| 127 | + copy_to_operands(exists_bound_inf, exists_bound_sup, s0); |
| 128 | + copy_to_operands(s1); |
| 129 | + }; |
| 130 | + |
| 131 | + const exprt &univ_lower_bound() const |
| 132 | + { |
| 133 | + return op0(); |
| 134 | + } |
| 135 | + |
| 136 | + const exprt &univ_upper_bound() const |
| 137 | + { |
| 138 | + return op1(); |
| 139 | + } |
| 140 | + |
| 141 | + const exprt &premise() const |
| 142 | + { |
| 143 | + return op2(); |
| 144 | + } |
| 145 | + |
| 146 | + const exprt &exists_lower_bound() const |
| 147 | + { |
| 148 | + return op3(); |
| 149 | + } |
| 150 | + |
| 151 | + const exprt &exists_upper_bound() const |
| 152 | + { |
| 153 | + return operands()[4]; |
| 154 | + } |
| 155 | + |
| 156 | + const exprt &s0() const |
| 157 | + { |
| 158 | + return operands()[5]; |
| 159 | + } |
| 160 | + |
| 161 | + const exprt &s1() const |
| 162 | + { |
| 163 | + return operands()[6]; |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | +inline const string_not_contains_constraintt |
| 168 | +&to_string_not_contains_constraint(const exprt &expr) |
| 169 | +{ |
| 170 | + assert(expr.id()==ID_string_not_contains_constraint); |
| 171 | + assert(expr.operands().size()==7); |
| 172 | + return static_cast<const string_not_contains_constraintt &>(expr); |
| 173 | +} |
| 174 | + |
| 175 | +inline string_not_contains_constraintt |
| 176 | +&to_string_not_contains_constraint(exprt &expr) |
| 177 | +{ |
| 178 | + assert(expr.id()==ID_string_not_contains_constraint); |
| 179 | + assert(expr.operands().size()==7); |
| 180 | + return static_cast<string_not_contains_constraintt &>(expr); |
| 181 | +} |
| 182 | + |
| 183 | +#endif |
0 commit comments