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
As-pattern synonyms #94
Conversation
(I had to clear up a bit of git messup)
I must have forgotten @rrnewton when writing this file, sorry for that!
I'm a bit lost here. The proposal talks about unidirectional pattern synonyms, but the very first example given is: patttern MP x y = x@(Just y) Which is a bidirectional pattern synonym. Should this be |
Also, what would happen if the user attempted to match on f (MP Nothing v) = e Is this case ever reachable? |
Sorry, I don't understand what's proposed at all. With proposals that are for syntactic sugar, Simon usually asks the proposer to give the translation/desugaring. There is no straightforward use case shown; only an awkward/degenerate case, per Trac #9793. If I write
then what would be an unproblematic use of pattern The Language Report for as-patterns has
So in using
It's an illegal pattern. Afterthought: This also explains Ryan's
is not just unreachable, but illegal. Re-reading the proposal's
Well, OK so those are illegal as-patterns but pattern synonyms wouldn't generate them as such. I think the Language Report has a sensible/simple syntactic-based rule to stop you writing code that's semantically pointless or even downright bewildering. Allowing non-var arguments to pattern synonyms in as-var positions means the syntax rule is in effect:
And that would allow patterns like
|
Can we combine as-patterns with record syntax in pattern synonyms? This just seems weird (perhaps I'm not squinting hard enough):
And I think @RyanGlScott's first comment is right we can't allow even explicit bi-directionals:
That error message is going to bewilder end-users: we're trying to make life easier for them. Then I'm dubious about the claim
We'll be able to remove one fussy restriction:
But need to support a slew of fussier restrictions:
Plus we really need to police the use of the pattern synonym in pattern matching:
|
Silly me. The proposal is only about unidirectional pattern synonyms. Sorry. Fixed.
Just follow the rules I gave. Match the argument
Quite right. But this is NOT a proposal about syntactic sugar. As the paper explains (in Section 5) a simple-minded syntactic expansion doesn't work. That's why I give the semantics in exactly the same way that the Haskell report explains the semantics of matching.
then what would be an unproblematic use of pattern MP?
THis would match arguments of form
Correct. And I am not proposing that. Indeed I am not proposing any change to the syntax of patterns. I'm only proposing to lift a restriction that GHC imposes, even though the user manual explicitly says the restriction does not exist.
(I switched to unidirectional, since that's what the proposal is about.) Yes, that's absolutely fine. For example, I could say
This pattern woudl match values of form
There are a whole slew of such patterns not allowed in teh RHS of bidirectonal pattern synonyms (e.g. view patterns), and as-patterns is already in that list. I do not propse any change there.
No, they are fine with record syntax.
No, I do not propose such a restriction
No, I do not propose such a restriction. TL;DR: I'm just proposing to LIFT a restriction, one that takes user-manual-space to document; code to implement; and offers no benefits. I'm not proposing to add any new restrictions. I'm not claiming that it's terribly useful, just that its simpler to have fewer restrictions, and simple, uniform rules. |
Thanks @simonpj . The trouble is: all your arguments are from the point of view of the compiler-writer, and somewhat from the pattern-writer. But the idea behind pattern synonyms is to make life easier for the pattern-user (and maybe a bit of implementation-hiding).
I'm claiming it is counter-useful for the pattern-user. Particularly not banning (or at least warning) using non-var arguments in as-var positions (which allows a pattern that will never match); also allowing explicitly bi-directional as-patterns. And probably confusing: the user sees two arguments which are in fact correlated (must be correlated in an expression), but in an opaque way.
I'm already up for documenting some of the fussy restrictions around pattern synonyms, as you request here. I'll volunteer to also document that as-patterns are allowed, but probably useful chiefly for perplexing users. |
I agree. And that is why I wanted to eliminate special cases from the user manual, and give simple, uniform rules that always work. I don't mind writing code in the compiler that benefits users. This code, in my opinion, does not. But let's see what others say! |
@simonpj, the discussion ebbed down. Do you want to submit it to the committee? Does the proposal need more work? |
Am I correct in the following (under this proposal): pattern And x y = x@y
-- I can now do:
f :: A -> B
f (x <- view1 `And` y <- view2) = …
-- I just created and-patterns! So if this goes through, may it be worth making At any rate, to the pattern user it will be mostly invisible that an |
@aspiwack, yes you are right. As the "side note" in the proposal says, a logical development would to be allow |
See Trac #9793 |
I've revised the proposal a bit, and have submitted it to the committee. |
AsPatternSynonyms.rst
Outdated
Effect and Interactions | ||
----------------------- | ||
None that I can see. It just lifts a restriction. | ||
patttern MP x y <- x@(Just y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 t
This patch implements GHC Proposal #94, described here ghc-proposals/ghc-proposals#94 The effect is simply to lift a totally-undocumented restriction to unidirecional pattern synonyms, namely that they can't have as-patterns or n+k patterns. The fix is easy: just remove the checks. I also took the opportunity to improve the manual entry for the semantics of pattern matching for pattern synonyms.
The proposal has been accepted; the following discussion is mostly of historic interest.
Here's a modest proposal to improve pattern synonyms
Rendered