-
Notifications
You must be signed in to change notification settings - Fork 149
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
Merge CTerm #4621
Merge CTerm #4621
Conversation
consequent = mlAnd(t.constraints) if t.constraints else TRUE | ||
new_constraints.append(mlImplies(antecedent, consequent)) | ||
|
||
return CTerm(new_config, new_constraints) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main implementation of merge_cterms
.
- Return
None
if the configuration structures are different. - Replace cell content into a new free variable if there are free variables or different between them.
- The constraints will be
CELL_NAME == SUBST /\ ... -> source.constraint /\ ...
pyk/src/pyk/cterm/cterm.py
Outdated
new_constraints: list[KInner] = [] | ||
for new_subst, t in [(new_t1_subst, t1), (new_t2_subst, t2)]: | ||
if new_subst: | ||
antecedent = mlAnd([mlEquals(KVariable(cell), new_subst[cell]) for cell in new_subst]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be simplified as well? See https://github.com/runtimeverification/k/blob/master/pyk/src/pyk/kast/inner.py#L753
antecedent = mlAnd([mlEquals(KVariable(cell), new_subst[cell]) for cell in new_subst]) | |
antecedent = new_subst.ml_pred |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for point it out! I've changed that!
def merge_cterms(t1: CTerm, t2: CTerm) -> CTerm | None: | ||
"""Return a `CTerm` which is the merge of the two input `CTerm` instances. | ||
|
||
Args: | ||
t1: First `CTerm` to merge. | ||
t2: Second `CTerm` to merge. | ||
|
||
Returns: | ||
A `CTerm` which is the merge of the two input `CTerm` instances. | ||
""" | ||
# check all cells in t1 and t1, if they are the same, keep them, otherwise, create a new free variable for them | ||
t1_config, t1_subst = split_config_from(t1.config) | ||
t2_config, t2_subst = split_config_from(t2.config) | ||
|
||
if t1_config != t2_config: | ||
# cannot merge two configurations with different structure | ||
return None | ||
|
||
new_subst = Subst({}) | ||
new_t1_subst = Subst({}) | ||
new_t2_subst = Subst({}) | ||
|
||
for cell in t1_subst: | ||
if t1_subst[cell] == t2_subst[cell]: | ||
# keep the cell if it is the same | ||
new_subst = new_subst * Subst({cell: t1_subst[cell]}) | ||
else: | ||
# create a new free variable for the cell | ||
new_t1_subst = new_t1_subst * Subst({cell: t1_subst[cell]}) | ||
new_t2_subst = new_t2_subst * Subst({cell: t2_subst[cell]}) | ||
new_config = new_subst(t1_config) | ||
|
||
new_constraints: list[KInner] = [] | ||
for new_subst, t in [(new_t1_subst, t1), (new_t2_subst, t2)]: | ||
if new_subst: | ||
new_constraints.append(mlImplies(new_subst.ml_pred, t.constraint)) | ||
|
||
return CTerm(new_config, new_constraints) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a less powerful version of CTerm.anti_unify
. Basically, instead of merge_cterms(cterm1, cterm2)
, you can do:
cterm, csubst1, csubst2 = cterm1.anti_unify(cterm2)
You'll have that csubst1(cterm) == cterm1
, and csubst2(cterm) == cterm2
, and you can use the data returned there to compute what you need for the merged nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are some tests of antiunification:
k/pyk/src/tests/integration/proof/test_imp.py
Line 1296 in 42e0354
def test_anti_unify_keep_values( |
k/pyk/src/tests/integration/proof/test_imp.py
Line 1247 in 42e0354
def test_anti_unify_forget_values( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I looked at the code and tests of anti_unify
and feel that anti_unify
can indeed be directly used. It's just that the generated csubst
I can't use. Perhaps I can close this PR.
~Blocked on: #4631 ~Blocked on: #4630 ~Blocked on: #4633 While reviewing and going over #4621 with @Stevengre , it became somewhat clear that how we handle turning substitions into ML predicates is a bit dirty. This attempts to clean this up a bit. Where potentially breaking changes to API are introduced here, I've checked if it affects the following repos when I mention "downstream" below: `evm-semantics kontrol wasm-semantics riscv-semantics mir-semantics`. In particular: - The function `CTerm.anti_unify` has a simplification where it reuses a function from `kast.manip` instead of reimplementing it. - The functions `CSubst.from_pred` and `CSubst.pred` are added, as replacements for `Subst.ml_pred`. This is because `Subst.ml_pred` doesn't have a good way to produce correctly sorted predicates, because it's in module `kast.inner`. - `Subst.ml_pred` is removed, and tests are updated to use the new `CSubst` variant. None of the downstream repositories use `Subst.ml_pred` directly. - The new `CSubst.pred` correctly sorts the generated `#Equals` clauses, defaulting to `K` sort or if a `KDefinition` is supplied using it to do sort inference. It also provides options for controlling whether we include the substitution or the constraints in the generated predicate. - A test is added for a `CSubst.pred` case which caused a bug in the integration tests dealing with identity substitutions. - The `CTermSymbolic.implies` function is updated to reuse `CSubst.from_pred` instead of reimplementing it. - On the case of duplicate entries, the first is kept and the latter are made as predicates.
Merge two cterms into one for merging node functionality #4425.