hax icon indicating copy to clipboard operation
hax copied to clipboard

Add match guard to AST

Open cmester0 opened this issue 1 year ago • 2 comments

I have added the match guard to the AST. Currently no backend supports these, so this makes the rejection of this feature explicit. Furthermore we can now add a phase for removing match conditionals. This should also allow us to rewrite record pattern matching into guarded match statements (should help with #180 / #168).

cmester0 avatar Mar 21 '24 14:03 cmester0

We can add a phase that unrolls (guarded) matches, e.g.

match foo {
    Foo { a, .. } if a > 10 => 1,
    Foo { b, .. } if b => 2,
    _ => 3,
};

becomes

if let Foo { a, .. } = foo && a > 10 {
  1
} else if let Foo { b, .. } = foo && b {
  2
} else if let _ = foo {
  3
} else {
  panic!()
};

Which should allow us to do matching on records by moving the projection to the right side

if let a = foo.a && a > 10 {
  1
} else if let b = foo.b && b {
  2
} else if let _ = foo {
  3
} else {
  panic!()
};

Which can be refolded into match statements, as support for let_chains (experimental feature) e.g. if let _ && _ also seems to be missing.

cmester0 avatar Mar 21 '24 17:03 cmester0

Hi Lasse, thanks for the PR, supporting guards on arms would indeed be great.

Both let guards on arms and let chains on if and arms are nightly: I wonder if we want to support any of those, maybe we should only be supporting .. if <bool-expr> => ...

But as you say, having (chainable) let guars on arms might help us fixing #168: that might be a nice intermediate representation. We could rewrite matches that includes patterns some backends don't support into matches with guards, and then have a phase that "multiplexes" such guarded matches into nested matches.

Otherwise the PR looks good, there's just a few TODO to fix. If you want i can fix the ones on the visitors, tell me. (I plan to write a PPX for generating those at some point, this code is not nice to update)

W95Psp avatar Apr 08 '24 07:04 W95Psp

This PR has been marked as stale due to a lack of activity for 60 days. If you believe this pull request is still relevant, please provide an update or comment to keep it open. Otherwise, it will be closed in 7 days.

github-actions[bot] avatar Sep 02 '24 02:09 github-actions[bot]

This PR has been closed due to a lack of activity since being marked as stale. If you believe this pull request is still relevant, please reopen it with an update or comment.

github-actions[bot] avatar Sep 12 '24 02:09 github-actions[bot]