[GHC] #8581: Add support for explicitly-bidirectional pattern synonyms

GHC ghc-devs at haskell.org
Thu Aug 21 17:01:45 UTC 2014


#8581: Add support for explicitly-bidirectional pattern synonyms
-------------------------------------+-------------------------------------
              Reporter:  cactus      |            Owner:  cactus
                  Type:  feature     |           Status:  closed
  request                            |        Milestone:  7.10.1
              Priority:  normal      |          Version:
             Component:  Compiler    |         Keywords:
            Resolution:  fixed       |     Architecture:  Unknown/Multiple
      Operating System:              |       Difficulty:  Unknown
  Unknown/Multiple                   |       Blocked By:  5144
       Type of failure:              |  Related Tickets:
  None/Unknown                       |
             Test Case:              |
              Blocking:              |
Differential Revisions:              |
-------------------------------------+-------------------------------------

Comment (by mpickering):

 I just tried out this patch and it seems strange to me that the
 constructor synonym has the same class constraints as the pattern. Is this
 by design? Here is an example which would have worked really nicely if not
 for this restriction. I know you can get around this by defining your own
 constructors with the right types.

 Note that there is no `View` instance for `Holey` which makes sense but we
 can still define a `Construct` instance.


 {{{
 {-# LANGUAGE PatternSynonyms, ViewPatterns #-}

 data ExpF a = AddF a a | NumF Int deriving Show

 class Construct a where
   construct :: ExpF a -> a

 class View a where
   view :: a -> ExpF a

 newtype Exp = Exp (ExpF Exp) deriving (Show)

 instance Construct Exp where
   construct e = Exp e

 instance View Exp where
   view (Exp e) = e

 data Holey = Hole | NonHole (ExpF Holey)

 instance Construct Holey where
   construct = NonHole

 data AttrExpr = AttrExpr [String] (ExpF AttrExpr)

 pattern Add a b <- (view -> AddF a b) where
   Add a b = (construct (AddF a b))

 pattern Num n <- (view -> NumF n) where
   Num n = (construct (NumF n))

 }}}

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


More information about the ghc-tickets mailing list