Skip to content

Commit ca96a49

Browse files
author
Owen Jones
committed
Refactor add_axioms_for_correct_number_format
# Conflicts: # src/solvers/refinement/string_constraint_generator_valueof.cpp
1 parent 3a13476 commit ca96a49

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/solvers/refinement/string_constraint_generator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class string_constraint_generatort
294294
const function_application_exprt &f);
295295

296296
exprt add_axioms_for_parse_int(const function_application_exprt &f);
297-
exprt add_axioms_for_correct_number_format(
297+
void add_axioms_for_correct_number_format(
298298
const string_exprt &str, const exprt &radix, std::size_t max_size);
299299
exprt add_axioms_for_to_char_array(const function_application_exprt &f);
300300
exprt add_axioms_for_compare_to(const function_application_exprt &f);

src/solvers/refinement/string_constraint_generator_valueof.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,9 @@ string_exprt string_constraint_generatort::add_axioms_for_value_of(
364364
/// \param str: string expression
365365
/// \param radix: the radix
366366
/// \param max_size: maximum number of characters
367-
/// \return a boolean expression saying whether the string does represent a
368-
/// number with the given radix
369-
exprt string_constraint_generatort::add_axioms_for_correct_number_format(
367+
void string_constraint_generatort::add_axioms_for_correct_number_format(
370368
const string_exprt &str, const exprt &radix, std::size_t max_size)
371369
{
372-
symbol_exprt correct=fresh_boolean("correct_number_format");
373370
const refined_string_typet &ref_type=to_refined_string_type(str.type());
374371
const typet &char_type=ref_type.get_char_type();
375372
const typet &index_type=ref_type.get_index_type();
@@ -386,31 +383,34 @@ exprt string_constraint_generatort::add_axioms_for_correct_number_format(
386383
// TODO: we should have implications in the other direction for correct
387384
// correct => |str| > 0
388385
exprt non_empty=str.axiom_for_is_longer_than(from_integer(1, index_type));
389-
axioms.push_back(implies_exprt(correct, non_empty));
386+
axioms.push_back(non_empty);
390387

391388
// correct => (str[0] = '+' or '-' || is_digit_with_radix(str[0], radix))
392389
or_exprt correct_first(
393390
or_exprt(starts_with_minus, starts_with_plus), starts_with_digit);
394-
axioms.push_back(implies_exprt(correct, correct_first));
391+
axioms.push_back(correct_first);
395392

396393
// correct => str[0]='+' or '-' ==> |str| > 1
397394
implies_exprt contains_digit(
398395
or_exprt(starts_with_minus, starts_with_plus),
399396
str.axiom_for_is_longer_than(from_integer(2, index_type)));
400-
axioms.push_back(implies_exprt(correct, contains_digit));
397+
axioms.push_back(contains_digit);
401398

402399
// correct => |str| < max_size
403-
axioms.push_back(
404-
implies_exprt(correct, str.axiom_for_is_shorter_than(max_size)));
405-
406-
// forall 1 <= qvar < |str| . correct => is_digit_with_radix(str[qvar], radix)
407-
symbol_exprt qvar=fresh_univ_index("number_format", index_type);
408-
exprt is_digit=is_digit_with_radix(str[qvar], radix);
409-
string_constraintt all_digits(
410-
qvar, from_integer(1, index_type), str.length(), correct, is_digit);
411-
axioms.push_back(all_digits);
400+
axioms.push_back(str.axiom_for_is_shorter_than(max_size));
412401

413-
return correct;
402+
// forall 1 <= i < |str| . correct => is_digit_with_radix(str[i], radix)
403+
// We unfold the above because we know that it will be used for all i up to
404+
// str.length()
405+
for(std::size_t index=1; index<max_size; ++index)
406+
{
407+
const exprt index_expr=from_integer(index, index_type);
408+
/// index < length && correct => is_digit(str[index])
409+
implies_exprt character_at_index_is_digit(
410+
binary_relation_exprt(index_expr, ID_lt, str.length()),
411+
is_digit_with_radix(str[index], radix));
412+
axioms.push_back(character_at_index_is_digit);
413+
}
414414
}
415415

416416
/// add axioms corresponding to the Integer.parseInt java function
@@ -443,9 +443,7 @@ exprt string_constraint_generatort::add_axioms_for_parse_int(
443443

444444
/// TODO: we should throw an exception when this does not hold:
445445
const std::size_t max_string_length=40;
446-
const exprt &correct=add_axioms_for_correct_number_format(
447-
str, radix, max_string_length);
448-
axioms.push_back(correct);
446+
add_axioms_for_correct_number_format(str, radix, max_string_length);
449447

450448
/// TODO(OJones): size should depend on the radix
451449
/// TODO(OJones): we should deal with overflow properly

0 commit comments

Comments
 (0)