OR-patterns? #664
gefjon
started this conversation in
Ideas and Proposals
Replies: 1 comment 3 replies
-
What would be the behavior of branches not having the same variables? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm writing a red-black tree as the implementation of a data structure I need for work, and it turns out that
balance :: Tree -> Tree
(the function that restores the red-black invariant to a tree after an insertion has ruined it) is a classic example of a case where OR-patterns are a huge ergonomic improvement.For reference, here's an implementation of
balance
in Coalton without OR-patterns:I'll point out, because I don't think it's obvious at a glance, that all four interesting match arms bind the same variables from different locations on a tree, and then evaluate the same form to construct a balanced tree holding those variables.
Here's a version with a hypothetical
or
pattern, which I think makes what's going on much clearer:I believe this to be a huge improvement in clarity; in the original version, it takes a bit of reading to notice that all four interesting branches are doing the same thing for slightly different tree topologies, but in the second it's immediately obvious.
And best of all, Trivia already supports OR-patterns, so the only work we need to do to support this is in typechecking.
Beta Was this translation helpful? Give feedback.
All reactions