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

#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

#13957: Allow deriving multiparameter type classes with representationally equal arguments -------------------------------------+------------------------------------- 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): TODO What if the argument order of `SIEVE` were reversed, as with [https://hackage.haskell.org/package/profunctors-5.2/docs/Data-Profunctor- Sieve.html Sieve].. let's find a way to allow deriving that as well. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13957#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13957: Allow deriving multiparameter type classes with representationally equal arguments -------------------------------------+------------------------------------- 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: | -------------------------------------+------------------------------------- Description changed by Iceland_jack: @@ -41,1 +41,1 @@ - SIEVE I + (SIEVE I) New description: 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#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13957: Allow deriving multiparameter type classes with representationally equal arguments -------------------------------------+------------------------------------- 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 RyanGlScott): This doesn't strike me as a very good idea, for a simple reason: it's quite possible you might also have `instance SIEVE I (->)` in scope, which might behave differently from `instance Sieve Identity (->)`. Allowing this sort of thing would make deriving with MPTCs very unpredictable. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13957#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13957: Allow deriving multiparameter type classes with representationally equal arguments -------------------------------------+------------------------------------- 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): We cannot both have `SIEVE I (->)` and `SIEVE Identity (->)` if we assume a functional dependency, similar to #13404 where an analogous example would compile {{{#!hs class SIEVE p where type SIEVE_ty p :: Type -> Type sIEVE :: p a b -> (a -> SIEVE_ty p b) instance SIEVE (->) where type SIEVE_ty (->) = Identity sIEVE :: (a -> b) -> (a -> Identity b) sIEVE f = f >>> Identity newtype ARR a b = ARR (a -> b) deriving newtype SIEVE }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13957#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13957: Allow deriving multiparameter type classes with representationally equal arguments -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: | Keywords: deriving Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * keywords: => deriving -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13957#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC