[GHC] #12001: RFC: Add pattern synonyms to base

GHC ghc-devs at haskell.org
Mon Jan 16 15:02:20 UTC 2017


#12001: RFC: Add pattern synonyms to base
-------------------------------------+-------------------------------------
        Reporter:  Iceland_jack      |                Owner:
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  libraries/base    |              Version:  7.10.3
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by Iceland_jack):

 For [https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List-
 NonEmpty.html Data.List.NonEmpty]

 {{{#!hs
 pattern NonEmpty :: NonEmpty a -> [a]
 pattern NonEmpty xs <- (nonEmpty -> Just xs)
   where NonEmpty xs = toList xs

 {-# COMPLETE [], NonEmpty  #-}
 }}}

 using PatternSynonyms/CompleteSigs.

 == Uses ==
 The pattern of destructing a `NonEmpty` value only to reassemble it pop up
 when using `NonEmpty`

 {{{#!hs
 f []     = ...
 f (x:xs) = ... (x:|xs)
 }}}

 An example is

 {{{#!hs
 -- match_groups :: [[(PatGroup,EquationInfo)]] -> DsM (NonEmpty
 MatchResult)
 -- match_groups []     = matchEmpty v ty
 -- match_groups (g:gs) = mapM match_group (g:|gs)

 match_groups :: [[(PatGroup,EquationInfo)]] -> DsM (NonEmpty MatchResult)
 match_groups []            = matchEmpty v ty
 match_groups (NonEmpty gs) = mapM match_group gs
 }}}

 Which can be abstracted into this rather useful eliminator:

 {{{#!hs
 -- match_groups = neElim (matchEmpty v ty) (mapM match_group)

 neElim :: c -> (NonEmpty a -> c) -> [a] -> c
 neElim c _ []            = c
 neElim _ f (NonEmpty xs) = f xs
 }}}

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


More information about the ghc-tickets mailing list