Skip to content

Commit

Permalink
fix match_with_constraint and CSubst.appy
Browse files Browse the repository at this point in the history
fix `match_with_constraint` and `CSubst.appy`
  • Loading branch information
Stevengre committed Sep 20, 2024
1 parent 59293b8 commit ec7162c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 2 additions & 4 deletions pyk/src/pyk/cterm/cterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,14 @@ def match_with_constraint(self, cterm: CTerm) -> CSubst | None:
if subst is None:
return None

constraint = self._ml_impl(cterm.constraints, map(subst, self.constraints))

return CSubst(subst=subst, constraints=[constraint])
return CSubst(subst=subst, constraints=cterm.constraints)

@staticmethod
def _ml_impl(antecedents: Iterable[KInner], consequents: Iterable[KInner]) -> KInner:
antecedent = mlAnd(unique(antecedents), GENERATED_TOP_CELL)
consequent = mlAnd(unique(term for term in consequents if term not in set(antecedents)), GENERATED_TOP_CELL)

if mlTop(GENERATED_TOP_CELL) in {antecedent, consequent}:
if mlTop(GENERATED_TOP_CELL) in [antecedent, consequent]:
return consequent

return mlImplies(antecedent, consequent, GENERATED_TOP_CELL)
Expand Down
29 changes: 27 additions & 2 deletions pyk/src/tests/unit/test_cterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from pyk.prelude.kbool import TRUE
from pyk.prelude.kint import INT, intToken
from pyk.prelude.ml import mlAnd, mlEquals, mlEqualsTrue, mlTop

from .utils import a, b, c, f, g, ge_ml, h, k, lt_ml, x, y, z

if TYPE_CHECKING:
Expand All @@ -35,8 +34,12 @@
v1_sorted = KVariable('V1', sort=INT)


def generated_top(term: KInner) -> KApply:
return KApply(KLabel('<generatedTop>', GENERATED_TOP_CELL), term)


def _as_cterm(term: KInner) -> CTerm:
return CTerm(KApply(KLabel('<generatedTop>', GENERATED_TOP_CELL), term))
return CTerm(generated_top(term))


MATCH_TEST_DATA: Final[tuple[tuple[KInner, KInner], ...]] = (
Expand Down Expand Up @@ -75,6 +78,28 @@ def test_no_cterm_match(term: KInner, pattern: KInner) -> None:
assert subst is None


MATCH_WITH_CONSTRAINT_TEST_DATA: Final = (
(CTerm(generated_top(x)), CTerm(generated_top(x))),
(CTerm(generated_top(x)), CTerm(generated_top(y))),
(CTerm(generated_top(x)), CTerm(generated_top(y), (mlEqualsTrue(geInt(y, intToken(0))),))),
(
CTerm(generated_top(x), (mlEqualsTrue(geInt(y, intToken(0))),)),
CTerm(generated_top(y), (mlEqualsTrue(geInt(y, intToken(5))),)),
),
)


@pytest.mark.parametrize('t1, t2', MATCH_WITH_CONSTRAINT_TEST_DATA, ids=count())
def test_cterm_match_with_constraint(t1: CTerm, t2: CTerm) -> None:
# When
c_subst1 = t1.match_with_constraint(t2)
c_subst2 = t2.match_with_constraint(t1)

# Then
assert c_subst1 is not None and c_subst1.apply(t1) == t2
assert c_subst2 is not None and c_subst2.apply(t2) == t1


BUILD_RULE_TEST_DATA: Final = (
(
T(k(KVariable('K_CELL')), mem(KVariable('MEM_CELL'))),
Expand Down

0 comments on commit ec7162c

Please sign in to comment.