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

Miscompilation of in operator in pattern matching code #725

Closed
yggdr opened this issue Apr 3, 2023 · 1 comment
Closed

Miscompilation of in operator in pattern matching code #725

yggdr opened this issue Apr 3, 2023 · 1 comment

Comments

@yggdr
Copy link

yggdr commented Apr 3, 2023

It seems the in operator compiles to the wrong order of its parameters in pattern matching. I have code like this

a={'Test'}
addpattern def f({'t': t `(. in a)`}):
 print t

or alternatively

a={'Test'}
addpattern def f({'t': t `(in)` a}):
 print t

In both cases I'd expect the call f({'t': 'Test'}) to pass the pattern matching and reach the print statement. Instead, TypeError: 'in <string>' requires string as left operand, not set is raised.

It seems that the (. in a) gets compiled with the arguments switched around to:

>>> import coconut.convenience as c
>>> "`(. in a)`" |> c.parse$(mode="block") |> print
(_coconut_partial(_coconut.operator.contains, {1: a}, 2, ())))()
>>> ((\_coconut_partial(\_coconut.operator.contains, {1: a}, 2, ())))
<built-in function contains>$(?, {'Test'})

In the last line you can see the problem: the contains function takes the container as the first arguments, and the test subject as the second, so it needs its parameters switched compared to other operator functions.
Interestingly. the not in variant doesn't suffer this problem as the _coconut_not_in function it gets compiled to takes the container as its second argument.

Coconut: Version 3.0.0-a_dev19 running on Python 3.11.2 and Cython cPyparsing v2.4.7.1.2.0

@evhub evhub added the bug label Apr 4, 2023
@evhub evhub added this to the v3.0.0 milestone Apr 4, 2023
evhub added a commit that referenced this issue Apr 4, 2023
@evhub
Copy link
Owner

evhub commented Apr 4, 2023

Thanks for the bug report! Fixed on coconut-develop>=3.0.0-a_dev20.

Temporary workaround:

operator in_
(in_) = flip(in)
assert (1 in_ .)([1])

@evhub evhub closed this as completed Apr 4, 2023
@evhub evhub added the resolved label Apr 7, 2023
@evhub evhub mentioned this issue May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants