[GHC] #14070: Allow ‘unsafe’ deriving strategy, deriving code with ‘unsafeCoerce’

GHC ghc-devs at haskell.org
Tue Aug 1 02:44:59 UTC 2017


#14070: Allow ‘unsafe’ deriving strategy, deriving code with ‘unsafeCoerce’
-------------------------------------+-------------------------------------
           Reporter:  Iceland_jack   |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 This

 {{{#!hs
 newtype M m a = M (m a)
   deriving newtype
     Functor
 }}}

 produces something similar to

 {{{#!hs
 instance Functor m => Functor (M m) where
   fmap :: forall a a'. (a -> a') -> (M m a -> M m a')
   fmap = coerce (fmap @m @a @a')
 }}}

 but as wiki:Roles2 points out, this fails for methods such as `join_`:

 {{{#!hs
 class MonadJoin m where
   join_ :: m (m a) -> m a

 newtype M m a = M (m a)
   deriving newtype
     MonadJoin
  -- Couldn't match representation of type ‘m (M m a)’ with that of ‘m (m
 a)’
 }}}

 I think the user should be given the option to respect the roles or not by
 supplying `unsafe` wiki:Commentary/Compiler/DerivingStrategies

 {{{#!hs
 newtype M m a = M (m a)
   deriving unsafe newtype
     MonadJoin

 -- -( Produces `unsafeCoerce' instead of `coerce' )-

 -- instance MonadJoin m => MonadJoin (M m) where
 --   join_ :: forall a. M m (M m a) -> M m a
 --   join_ = unsafeCoerce (join_ @m @a)
 }}}

 This lets us (`newtype`-) derive a host of instances that we currently can
 not: `Traversable`, `Distributive` (currently forbidden due to
 [https://hackage.haskell.org/package/distributive-0.5.2/docs/Data-
 Distributive.html#v:distribute distribute]),
 [https://hackage.haskell.org/package/linear-1.20.7/docs/Linear-
 Matrix.html#v:trace Trace],
 [http://hackage.haskell.org/package/profunctors-5/docs/Data-Profunctor-
 Monad.html ProfunctorMonad / ProfunctorComonad], ...

 It does not seem like lenses (`FunctorWithIndex`, `FoldableWithIndex`,
 `TraversableWithIndex`) work with this though

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


More information about the ghc-tickets mailing list