[GHC] #13592: Newtype type class with compiler generated instances

GHC ghc-devs at haskell.org
Wed Apr 19 16:25:57 UTC 2017


#13592: Newtype type class with compiler generated instances
-------------------------------------+-------------------------------------
        Reporter:  Iceland_jack      |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.1
      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):

 The thing missing from that is the dependency, if I use
 [https://hackage.haskell.org/package/newtype-0.2/docs/Control-
 Newtype.html#v:ala ala] I can use

 {{{#!hs
 ala Sum foldMap [1,2]
   :: Num o => o
 }}}

 While with your formulation I get

 {{{#!hs
 ala Sum foldMap [1,2]
   :: (Coercible o o', Num o) => o
 }}}

 but it could certainly re-use `Coercible`

 {{{#!hs
 class Newtype n where
   type O n
   pack :: O n -> n
   pack = coerce
   default
     pack :: Coercible (O n) n => O n -> n

   unpack :: n -> O n
   unpack = coerce
   default
     unpack :: Coercible n (O n) => n -> O n
 }}}

 so that the compiler only needs to generate

 {{{#!hs
 instance Newtype (Sum a) where
   type O (Sum a) = a
 }}}

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


More information about the ghc-tickets mailing list