Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can BangPatterns appear

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 parse x : (!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.

like image 942
rem Avatar asked Aug 03 '15 14:08

rem


1 Answers

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.

like image 55
Reid Barton Avatar answered Oct 15 '22 09:10

Reid Barton