Skip to content
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

Move is_true and is_false to algorithm sub-package #534

Merged
merged 2 commits into from
Oct 3, 2024
Merged
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: 1 addition & 3 deletions claripy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from claripy import algorithm, ast
from claripy.algorithm import simplify
from claripy.algorithm import is_false, is_true, simplify
from claripy.annotation import Annotation, RegionAnnotation, SimplificationAvoidanceAnnotation
from claripy.ast.bool import (
And,
Expand All @@ -12,8 +12,6 @@
Or,
constraint_to_si,
false,
is_false,
is_true,
ite_cases,
ite_dict,
reverse_ite_cases,
Expand Down
7 changes: 6 additions & 1 deletion claripy/algorithm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from __future__ import annotations

from .bool_check import is_false, is_true
from .simplify import simplify

__all__ = ("simplify",)
__all__ = (
"is_false",
"is_true",
"simplify",
)
41 changes: 41 additions & 0 deletions claripy/algorithm/bool_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations

import logging
from contextlib import suppress
from typing import TYPE_CHECKING

import claripy
from claripy.errors import BackendError

if TYPE_CHECKING:
from claripy.ast.bool import Bool

log = logging.getLogger(__name__)


def is_true(expr: Bool) -> bool:
"""Checks if a boolean expression is trivially True.

A false result does not necessarily mean that the expression is False, but
rather that it is not trivially True.
"""

with suppress(BackendError):
return claripy.backends.concrete.is_true(expr)

log.debug("Unable to tell the truth-value of this expression")
return False


def is_false(expr: Bool) -> bool:
"""Checks if a boolean expression is trivially False.

A false result does not necessarily mean that the expression is False, but
rather that it is not trivially False.
"""

with suppress(BackendError):
return claripy.backends.concrete.is_false(expr)

log.debug("Unable to tell the truth-value of this expression")
return False
22 changes: 4 additions & 18 deletions claripy/ast/bool.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

import logging
from contextlib import suppress
from functools import lru_cache
from typing import TYPE_CHECKING, overload

import claripy
from claripy import operations
from claripy.algorithm.bool_check import is_false, is_true
from claripy.ast.base import ASTCacheKey, Base, _make_name
from claripy.errors import BackendError, ClaripyTypeError
from claripy.errors import ClaripyTypeError

from .bits import Bits

Expand All @@ -20,6 +20,8 @@


class Bool(Base):
"""Bool is the AST class for a boolean value."""

__slots__ = ()

@staticmethod
Expand Down Expand Up @@ -163,22 +165,6 @@ def If(cond, true_value, false_value):
Bool.__ror__ = Or


def is_true(e, exact=None): # pylint:disable=unused-argument
with suppress(BackendError):
return claripy.backends.concrete.is_true(e)

log.debug("Unable to tell the truth-value of this expression")
return False


def is_false(e, exact=None): # pylint:disable=unused-argument
with suppress(BackendError):
return claripy.backends.concrete.is_false(e)

log.debug("Unable to tell the truth-value of this expression")
return False


# For large tables, ite_dict that uses a binary search tree instead of a "linear" search tree.
# This improves Z3 search capability (eliminating branches) and decreases recursion depth:
# linear search trees make Z3 error out on tables larger than a couple hundred elements.)
Expand Down
Loading