Quantcast

[GHC] #5144: Pattern synonyms

classic Classic list List threaded Threaded
10 messages Options
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

[GHC] #5144: Pattern synonyms

GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------
 Lennart would like pattern synonyms.  Something like
 {{{
 pattern con var1 … varN = pat
 }}}
 where ‘pattern` is a new keyword.
  * Perhaps there should be a way to give a type as well, so the `con`
 could be `(con :: type)`.
  * The `rhs` is type checked as a usual pattern, i.e., in the global
 environment.
  * The `pat` should bind exactly `var1` .. `varN`.
  * Recursive pattern synonyms are not allowed.

 With `con` in scope it can be used like any other constructor in a
 pattern, and the semantics is simply by expansion.

 It would have been very nice if `con` could be used in expressions as
 well, but I don’t see how that could work with view patterns.
 Perhaps view patterns could be extended to make them bidirectional.

 My rationale for wanting pattern synonyms is that I sometimes have pattern
 matching with a lot of complex repetition in them.
 I’ve even resorted to using CPP in the past, and that just shows that
 Haskell is lacking some abstraction mechanism.

 If pattern synonyms could be made to work in the presence of view pattern
 it would offer a mechanism for normal pattern matching on abstract types,
 since the abstract type could export some pattern synonyms and you’d not
 be able to tell of those were real constructors or not.

 I’ve not tried implementing this, but I think SHE has something like it.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------

Comment(by simonmar):

 Conor has lobbied for adding these, and I'd like them too.  Allowing
 pattern matching on abstract types would be a killer feature.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------

Comment(by simonmar):

 I've been thinking about how to design this extension to get the most
 bang for the buck.  Here's a slightly different proposal that I think
 has some nice properties, and is still quite simple.

 Pattern synonyms define new '''varids''' rather than '''conids'''.
 For example, we could add this to `Data.Sequence`:

 {{{
 pattern empty    = t | Nothing2 <- viewLTree t
 pattern x  <| xs = t | Just (x,xs) <- viewLTree t
 pattern xs |> x  = t | Just (x,xs) <- viewRTree t
 }}}

 which would subsume all the existing `ViewL`/`ViewR` stuff.  Note that
 the pattern right hand side may include a pattern guard (or view
 patterns).

 The big win here is that you can use the same varid as an existing
 function function, so you get to define both constructors and
 destructors, and they look identical.  Furthermore you get pattern
 matching (view-style) on abstract types.

 You could do this with conids instead of varids if there was also some
 way to define the expression form - that's the tricky part, which is
 why I thought varids would be a better choice.  Also varid patterns
 give you a clue that something magic is going on, but in a nicer way
 than view patterns.

 Not many changes to Haddock etc. would be required (I think if we used
 conids it would be a bit trickier: should they be indistinguishable
 from ordinary datatypes or not?)

 A design question is whether the pattern should be required to have
 the same type as the function, if both exist.  There could be all
 sorts of hairy issues there.  If they aren't the same, can you give a
 type signature to a pattern?

 This extension to pattern synonyms covers all the use cases of view
 patterns in which there is a single identifier to the left of the
 arrow (which is about 90% of the examples in ViewPatterns).  Perhaps
 there's some way to extend this to allow passing arguments from the
 pattern site too... it's just a syntax question (but a gnarly one).  So
 while with view
 patterns you can define an n+k pattern, with pattern synonyms it has
 to be restricted to a particular `k`:

 {{{
 pattern oneplus n = m | m > 0, let n = m - 1

 f 0           = ...
 f (oneplus n) = ...
 }}}

 I do think for the cases where pattern synonyms apply, the syntax is
 much nicer than view patterns.  In particular, there's no need for
 intermediate Maybes or tuples, which the view pattern proposal
 suggests to make implicit.

 Another one:

 {{{
 pattern x `and` y = it | x <- it, y <- it
 }}}

 (this was called `both` in ViewPatterns).

 In conclusion: I'm not sure whether this is the right thing, but from
 certain angles it looks very attractive.  Thoughts?

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------

Comment(by igloo):

 I'm not sure I follow exactly what you're proposing. What does
 {{{
 pattern x `and` y = it | x <- it, y <- it
 }}}
 mean exactly? And how would you use it? Could you use it to require an
 argument matches both `(Just x, _)` and `(_, True)`, or am I on completely
 the wrong track?

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------

Comment(by simonmar):

 Replying to [comment:3 igloo]:

 Lennart is going to put up a wiki page describing the proposed extension
 soon, and we plan to work on it at CamHac.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |        Owner:              
        Type:  bug               |       Status:  new        
    Priority:  normal            |    Milestone:  _|_        
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------

Comment(by Lennart):

 There's a start of a wiki page at
 [http://hackage.haskell.org/trac/ghc/wiki/PatternSynonyms].

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |       Owner:                  
        Type:  bug               |      Status:  new            
    Priority:  normal            |   Milestone:  _|_            
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:                    |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by illissius):

 * cc: illissius@… (added)


Comment:

 It occurs to me that for great symmetry you could call these data synonyms
 (data synonyms : data constructors :: type synonyms : type constructors).
 Unfortunately the 'data' keyword is taken.. :-)

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:6>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |       Owner:                  
        Type:  feature request   |      Status:  new            
    Priority:  normal            |   Milestone:  _|_            
   Component:  Compiler          |     Version:                  
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:                    |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by bgamari):

 * cc: bgamari@… (added)
  * version:  7.0.3 =>
  * type:  bug => feature request


Comment:

 Any news on this? It would be very nice to have these.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |       Owner:                  
        Type:  feature request   |      Status:  new            
    Priority:  normal            |   Milestone:  _|_            
   Component:  Compiler          |     Version:                  
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:                    |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by cgibbard):

 * cc: cgibbard@… (added)


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:8>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5144: Pattern synonyms

GHC
In reply to this post by GHC
#5144: Pattern synonyms
---------------------------------+------------------------------------------
    Reporter:  simonpj           |       Owner:                  
        Type:  feature request   |      Status:  new            
    Priority:  normal            |   Milestone:  _|_            
   Component:  Compiler          |     Version:                  
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by simonpj):

  * difficulty:  => Unknown


Comment:

 I'm afraid not.  I'm deep under water with other stuff (including GHC
 stuff), and this feature is relatively ambitious, both on the design side
 and implementation.  It might make a good intern project!

 Simon

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5144#comment:9>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
Loading...