runtimeverification / k

K Framework Tools 7.0
BSD 3-Clause "New" or "Revised" License
431 stars 142 forks source link

CTerm.match_with_constraint does not return expected CSubst.constraints #4496

Open Stevengre opened 2 weeks ago

Stevengre commented 2 weeks ago

Hi @tothtamas28 !

I am encountering an issue with CTerm.match_with_constraint where it does not provide the expected CSubst that can be used to transform self into other via CSubst.apply. Instead, I am receiving an empty constraint. I suspect there may be an issue with the _ml_impl method.

Here is my test harness:

def test_cterm_match_with_constraint() -> None:
    # Given
    merged_cterm = {
        'config': {
            'args': [{'name': 'X', 'node': 'KVariable'}],
            'arity': 1,
            'label': {'name': '<top>', 'node': 'KLabel', 'params': []},
            'node': 'KApply',
            'variable': False
        },
        'constraints': []
    }
    merged_cterm = CTerm.from_dict(merged_cterm)

    original_cterm = {
        'config': {
            'node': 'KApply',
            'label': {'node': 'KLabel', 'name': '<top>', 'params': []},
            'args': [{'node': 'KVariable', 'name': 'X'}],
            'arity': 1,
            'variable': False
        },
        'constraints': [{
            'node': 'KApply',
            'label': {
                'node': 'KLabel',
                'name': '#Equals',
                'params': [{'node': 'KSort', 'name': 'Bool'}, {'node': 'KSort', 'name': 'GeneratedTopCell'}]
            },
            'args': [
                {'node': 'KToken', 'token': 'true', 'sort': {'node': 'KSort', 'name': 'Bool'}},
                {
                    'node': 'KApply',
                    'label': {'node': 'KLabel', 'name': '_>=Int_', 'params': []},
                    'args': [
                        {'node': 'KVariable', 'name': 'X'},
                        {'node': 'KToken', 'token': '5', 'sort': {'node': 'KSort', 'name': 'Int'}}
                    ],
                    'arity': 2,
                    'variable': False
                }
            ],
            'arity': 2,
            'variable': False
        }]
    }
    original_cterm = CTerm.from_dict(original_cterm)

    # When
    csubst = merged_cterm.match_with_constraint(original_cterm)

    # Then
    assert csubst.apply(merged_cterm) == original_cterm

Could you please help me identify what might be going wrong?

Thank you!

tothtamas28 commented 2 weeks ago

Thanks for reporting this issue.

Let's simplify the example. You can

Also, please push a branch with the test so that the issue is easy to reproduce.

Stevengre commented 2 weeks ago

Thanks for your suggestion. I’ve already constructed a test in this branch: https://github.com/runtimeverification/k/tree/4496-ctermmatch_with_constraint-does-not-return-expected-csubstconstraints. I hope this can help.

tothtamas28 commented 2 weeks ago

I opened a PR with your test to start the discussion (does not yet fix the issue):

The fix should be straightforward, we just need to figure out what the intended semantics for CTerm.constraint is. For that, all uses need to be inspected.