Skip to content

Commit 9415a24

Browse files
Refactor add_axioms_for_set_char
1 parent 9762886 commit 9415a24

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

src/solvers/refinement/string_constraint_generator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ class string_constraint_generatort final
170170
const exprt &input_int,
171171
const exprt &radix,
172172
size_t max_size = 0);
173+
exprt add_axioms_for_set_char(
174+
const array_string_exprt &res,
175+
const array_string_exprt &str,
176+
const exprt &position,
177+
const exprt &character);
173178

174179
private:
175180
symbol_exprt fresh_boolean(const irep_idt &prefix);

src/solvers/refinement/string_constraint_generator_transformation.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -380,19 +380,10 @@ exprt string_constraint_generatort::add_axioms_for_to_upper_case(
380380
}
381381

382382
/// Set a character to a specific value at an index of the string
383-
///
384-
/// Add axioms ensuring that the result `res` is similar to input string `str`
385-
/// where the character at index `pos` is set to `char`.
386-
/// These axioms are:
387-
/// 1. \f$ |{\tt res}| = |{\tt str}|\f$
388-
/// 2. \f$ {\tt res}[{\tt pos}]={\tt char}\f$
389-
/// 3. \f$ \forall i < min(|{\tt res}|, pos). {\tt res}[i] = {\tt str}[i]\f$
390-
/// 4. \f$ \forall pos+1 <= i < |{\tt res}|.\ {\tt res}[i] = {\tt str}[i]\f$
383+
/// \copydoc string_constraint_generatort::add_axioms_for_set_char
391384
/// \param f: function application with arguments integer `|res|`, character
392385
/// pointer `&res[0]`, refined_string `str`, integer `pos`,
393386
/// and character `char`
394-
/// \return an integer expression which is `1` when `pos` is out of bounds and
395-
/// `0` otherwise
396387
exprt string_constraint_generatort::add_axioms_for_char_set(
397388
const function_application_exprt &f)
398389
{
@@ -402,28 +393,45 @@ exprt string_constraint_generatort::add_axioms_for_char_set(
402393
char_array_of_pointer(f.arguments()[1], f.arguments()[0]);
403394
const exprt &position = f.arguments()[3];
404395
const exprt &character = f.arguments()[4];
396+
return add_axioms_for_set_char(res, str, position, character);
397+
}
405398

399+
/// Add axioms ensuring that the result `res` is similar to input string `str`
400+
/// where the character at index `pos` is set to `char`.
401+
///
402+
/// These axioms are:
403+
/// 1. \f$ |{\tt res}| = |{\tt str}|\f$
404+
/// 2. \f$ {\tt res}[{\tt pos}]={\tt char}\f$
405+
/// 3. \f$ \forall i < min(|{\tt res}|, pos). {\tt res}[i] = {\tt str}[i]\f$
406+
/// 4. \f$ \forall pos+1 <= i < |{\tt res}|.\ {\tt res}[i] = {\tt str}[i]\f$
407+
/// \return an integer expression which is `1` when `pos` is out of bounds and
408+
/// `0` otherwise
409+
exprt string_constraint_generatort::add_axioms_for_set_char(
410+
const array_string_exprt &res,
411+
const array_string_exprt &str,
412+
const exprt &position,
413+
const exprt &character)
414+
{
406415
const binary_relation_exprt out_of_bounds(position, ID_ge, str.length());
407-
const equal_exprt a1(res.length(), str.length());
408-
lemmas.push_back(a1);
409-
const equal_exprt a2(res[position], character);
410-
lemmas.push_back(a2);
411-
412-
const symbol_exprt q = fresh_univ_index("QA_char_set", position.type());
413-
const equal_exprt a3_body(res[q], str[q]);
414-
const string_constraintt a3(
415-
q, minimum(zero_if_negative(res.length()), position), a3_body);
416-
constraints.push_back(a3);
417-
418-
const symbol_exprt q2 = fresh_univ_index("QA_char_set2", position.type());
419-
const plus_exprt lower_bound(position, from_integer(1, position.type()));
420-
const equal_exprt a4_body(res[q2], str[q2]);
421-
const string_constraintt a4(
422-
q2, lower_bound, zero_if_negative(res.length()), a4_body);
423-
constraints.push_back(a4);
424-
416+
lemmas.push_back(equal_exprt(res.length(), str.length()));
417+
lemmas.push_back(equal_exprt(res[position], character));
418+
constraints.push_back([&] {
419+
const symbol_exprt q = fresh_univ_index("QA_char_set", position.type());
420+
const equal_exprt a3_body(res[q], str[q]);
421+
return string_constraintt(
422+
q, minimum(zero_if_negative(res.length()), position), a3_body);
423+
}());
424+
constraints.push_back([&] {
425+
const symbol_exprt q2 = fresh_univ_index("QA_char_set2", position.type());
426+
const plus_exprt lower_bound(position, from_integer(1, position.type()));
427+
const equal_exprt a4_body(res[q2], str[q2]);
428+
return string_constraintt(
429+
q2, lower_bound, zero_if_negative(res.length()), a4_body);
430+
}());
425431
return if_exprt(
426-
out_of_bounds, from_integer(1, f.type()), from_integer(0, f.type()));
432+
out_of_bounds,
433+
from_integer(1, get_return_code_type()),
434+
from_integer(0, get_return_code_type()));
427435
}
428436

429437
/// Convert two expressions to pair of chars

0 commit comments

Comments
 (0)