matchpy icon indicating copy to clipboard operation
matchpy copied to clipboard

One-to-one matching: symbols with variable_name do not work in commutative functions

Open hbarthels opened this issue 4 years ago • 2 comments

In one-to-one matching, symbols that have a variable_name do not work in commutative functions. They seem to work correctly in many-to-one matching. Example:

from matchpy import Operation, Symbol, Arity, Pattern, match, ManyToOneMatcher

f = Operation.new('f', Arity.binary, commutative=True)
a_x = Symbol('a', variable_name='x')
a = Symbol('a')
b = Symbol('b')

subject = f(a, b)
pattern = Pattern(f(a_x, b))

print(list(match(subject, pattern)))

matcher = ManyToOneMatcher(pattern)
print(list(matcher.match(subject)))

This results in

[]
[(Pattern(f(a: x, b)), {'x': Symbol('a')})]

but it should result in

[{'x': Symbol('a')}]
[(Pattern(f(a: x, b)), {'x': Symbol('a')})]

So far, we don't have any tests that verify this functionality. Once it is fixed, we should add some.

hbarthels avatar Jul 20 '20 16:07 hbarthels

commutative = True part is not yet handled, even

f = Operation.new('f', Arity.variadic, commutative=True)
a_x = Symbol('a', variable_name='x')
a = Symbol('a')

subject = f(a)
pattern = Pattern(f(a_x))

print(list(match(subject, pattern)))

gives the answer of []

samithkavishke avatar Mar 09 '24 13:03 samithkavishke

This ManyToOneMathcer has entirely different way to handle this situation as it uses a separate class defined for this, we need to reform the OneToOne matcher in a same way to ensure this functionality. I think the current implementation is not easy to change and take the desired outcome

samithkavishke avatar Mar 10 '24 09:03 samithkavishke