-
Notifications
You must be signed in to change notification settings - Fork 39
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
Pattern with a repeated symbol should only match if all referenced values are equal #152
Comments
What you want is unification, which is slightly different from pattern matching. Unification needs well-defined equality for values, but pattern matching does not. The implication is, usually, a statically typed programming language reports an error for the pattern Elixir and some pure functional programming language does not suffer from this, as they can always define a proper equality for every two values from the the language's value domain. |
Thanks for replying! I think I'm describing non-linear pattern matching rather than unification? Perhaps MLStyle could use === to tell if the two values are identical? There will be some issues with pattern matching on vectors, but the docs could explain what's going on. Or MLStyle could refuse to allow repeated symbols on the LHS of the match? |
Yes, this should be called non-linear patterns which requires equality for the matching target. The proposal of using I think this is do-able, but I cannot evaluate how it would affect different scenarios. Maybe presenting this as an extension: using MLStyle
using MLStyle.NonLinearPatterns
@match (10, 10, 2) begin
(x, x, y) => ...
end |
What you want can be achieved in one line, by changing the behaviour from shadowing to
The real issue is about how to control the scope of the |
From the user side it could be something like this?
The later and more specific or
From an implementation point of view, ex2tf and other functions could be changed to accept a first argument of the match style (a type or something) and them MLStyle.NonLinearPatterns or other modules could use multiple dispatch to override the bits that they care about? Depends how much of ex2tf and the other things are reusable, I suppose? I don't understand much of the haskell mailing list discussion, so I can't comment on that. |
e.g.
I think the (x, x) pattern above should only match if a == b. This matches the behaviour of elixir:
The text was updated successfully, but these errors were encountered: