[GHC] #13957: Allow deriving multiparameter type classes with representationally equal arguments

GHC ghc-devs at haskell.org
Tue Jul 11 00:26:12 UTC 2017


#13957: Allow deriving multiparameter type classes with representationally equal
arguments
-------------------------------------+-------------------------------------
           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 currently works

 {{{#!hs
 {-# Language DerivingStrategies, MultiParamTypeClasses,
 FunctionalDependencies, GeneralizedNewtypeDeriving #-}

 import Data.Kind
 import Data.Functor.Identity

 class SIEVE f p | p -> f where
   sIEVE :: p a b -> a -> f b

 instance SIEVE Identity (->) where
   sIEVE = (Identity .)

 newtype ARR a b = ARR (a -> b)
   deriving newtype
     (SIEVE Identity)
 }}}

 But what if I want a `Sieve I ARR` instance, for `newtype I a = I a`
 (which is representationally equal to `newtype Identity a = Identity a`)?

 {{{#!hs
 {-# Language DerivingStrategies, MultiParamTypeClasses,
 FunctionalDependencies, GeneralizedNewtypeDeriving, RankNTypes,
 TypeApplications, ScopedTypeVariables, InstanceSigs #-}

 import Data.Kind
 import Data.Functor.Identity
 import Data.Coerce

 class SIEVE f p | p -> f where
   sIEVE :: p a b -> a -> f b

 instance SIEVE Identity (->) where
   sIEVE = (Identity .)

 newtype ARR a b = ARR (a -> b)
   deriving newtype
     SIEVE I

 newtype I a = I a
 }}}

 generating the following code (this is basically to code generated before,
 except replacing some `Identity`s with `I`s

 {{{#!hs
 instance SIEVE I ARR where
   sIEVE :: forall a b. ARR a b -> (a -> I b)
   sIEVE
     = coerce
       @((a -> b) -> a -> Identity b)
       @(ARR a b  -> a -> I b)
       sIEVE
 }}}

 GHC should be able to recover `Identity` due to the functional dependency.

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


More information about the ghc-tickets mailing list