From GHC user guide it seems like most Pat
can be PBangPat
, but there are a few exceptions. e.g. top-level bangs in a module (like !main
) aren't allowed and x : !xs
fails to parsex : (!xs) parses
thanks @chi. What is the formal specification about where the bangs can be added? I've looked into some chapters of the user guide and the Report but found nothing.
There is no accepted formal specification for BangPatterns since they are not a part of any Haskell Report. The closest thing we have to a specification is the User's Guide along with the haskell-prime proposal it links to.
Both of those sources explicitly mention that a bang pattern is not allowed at the top level of a module.
As for x : !xs
, the User's Guide has this to say about the syntax of bang patterns:
We add a single new production to the syntax of patterns:
pat ::= !pat
It should be read in conjunction with the Haskell 2010 Report:
pat ::= lpat qconop pat | lpat lpat ::= apat | - (integer | float) | gcon apat_1 ... apat_k apat ::= var [ @ apat] | ... | ( pat ) | ...
According to these rules x : !xs
actually should parse (since !xs
is a pat
, the whole thing is lpat qconop pat
). So either the User's Guide (and the haskell-prime proposal) is wrong or GHC is wrong on this point.
I believe that in practice the syntax accepted by GHC is "anything that looks like a valid expression" including interpreting (!x)
as a section of the operator !
. For example (! Just x)
is accepted as a pattern but (! ! x)
is not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With