Open yamaguchi1024 opened 1 year ago
Thanks for opening this issue! Just echoing some of the things from the synchronous meeting:
exo_match
operator. It gives us a lot of control on the syntax we expose and looks a lot more natural.@gilbo might have more thoughts on this so pinging here.
Yes, I was suggesting the exo_match
possibility. One implementation idea I had in mind (and why I put in the weird $
tokens) would proceed in two steps.
This has the benefit of adhering to the "Let's parse Python patterns using the Python parser" convention already used by Exo. (This convention has the additional virtue of preventing us from straying too far from normal, expected Python syntax)
A third possibility between option 1 (which can be made more powerful using dunder methods that hook into pattern matching) and option 2 (which requires using annoying multi-line strings) is to somehow perform an AST hijack. The downside of this option is that you need something like a function definition (not merely a statement) that you can decorate with some @exo_match
decorator. I briefly considered this but then decided it would probably be worse than the multi-line string.
By two steps, I obviously meant 3. LOL
Python's pattern matching doesn't support any extension of semantic equivalence (there's a draft for it). The only double-underscore method you can specify is
__match_args__
, which lets you specify which value you want to pattern match against. Therefore, without user-facing IR, we'll end up doing a structural matching on internal cursors, which is not possible because we don't want to expose internal cursors to users. Let's look at how consumer_tile in blur/unsharp masking example can be implemented using different pattern matching options.def consumer_tile(p: Proc, cons_c : ForSeqCursor): match consc.type(): case e.For(e.For(e.For() as gc) as cc): p = reorder(cc, gc) cons_c = cc