Skip to content

Simplify quantified expressions over constants #8608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,10 @@ simplify_exprt::resultt<> simplify_exprt::simplify_node(const exprt &node)
{
r = simplify_prophecy_pointer_in_range(*prophecy_pointer_in_range);
}
else if(expr.id() == ID_exists || expr.id() == ID_forall)
{
r = simplify_quantifier_expr(to_quantifier_expr(expr));
}

if(!no_change_join_operands)
r = changed(r);
Expand Down
22 changes: 22 additions & 0 deletions src/util/simplify_expr_boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,25 @@

return unchanged(expr);
}

simplify_exprt::resultt<>
simplify_exprt::simplify_quantifier_expr(const quantifier_exprt &expr)
{
const exprt &where = expr.where();

if(!expr.is_boolean() || !where.is_boolean())
{
return unchanged(expr);

Check warning on line 388 in src/util/simplify_expr_boolean.cpp

View check run for this annotation

Codecov / codecov/patch

src/util/simplify_expr_boolean.cpp#L388

Added line #L388 was not covered by tests
}

if(where.is_false())
{
return false_exprt{};
}
else if(where.is_true())
{
return true_exprt{};
}

return unchanged(expr);
}
4 changes: 4 additions & 0 deletions src/util/simplify_expr_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class popcount_exprt;
class power_exprt;
class prophecy_pointer_in_range_exprt;
class prophecy_r_or_w_ok_exprt;
class quantifier_exprt;
class refined_string_exprt;
class shift_exprt;
class sign_exprt;
Expand Down Expand Up @@ -254,6 +255,9 @@ class simplify_exprt
[[nodiscard]] resultt<>
simplify_prophecy_pointer_in_range(const prophecy_pointer_in_range_exprt &);

/// Try to simplify exists/forall to a constant expression.
[[nodiscard]] resultt<> simplify_quantifier_expr(const quantifier_exprt &);

// auxiliary
bool simplify_if_implies(
exprt &expr, const exprt &cond, bool truth, bool &new_truth);
Expand Down
24 changes: 24 additions & 0 deletions unit/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,27 @@ TEST_CASE("Simplify power", "[core][util]")
simplify_expr(power_exprt{a, from_integer(1, integer_typet{})}, ns) == a);
}
}

TEST_CASE("Simplify quantifier", "[core][util]")
{
const symbol_tablet symbol_table;
const namespacet ns{symbol_table};

SECTION("Simplification for exists")
{
symbol_exprt a{"a", integer_typet{}};

REQUIRE(simplify_expr(exists_exprt{a, false_exprt{}}, ns) == false_exprt{});

REQUIRE(simplify_expr(exists_exprt{a, true_exprt{}}, ns) == true_exprt{});
}

SECTION("Simplification for forall")
{
symbol_exprt a{"a", integer_typet{}};

REQUIRE(simplify_expr(forall_exprt{a, false_exprt{}}, ns) == false_exprt{});

REQUIRE(simplify_expr(forall_exprt{a, true_exprt{}}, ns) == true_exprt{});
}
}
Loading