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

#13592: Newtype type class with compiler generated instances -------------------------------------+------------------------------------- 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: -------------------------------------+------------------------------------- Define a `Newtype` class, autmatically generating instances {{{#!hs class Newtype n where type O n :: Type pack :: O n -> n unpack :: n -> O n }}} as defined in Conal's [http://conal.net/papers/generic-parallel-functional /generic-parallel-functional.pdf Generic parallel functional programming] but also found in the [https://hackage.haskell.org/package/newtype-0.2 newtype] and [https://hackage.haskell.org/package/lens-4.15.1/docs /Control-Lens-Wrapped.html lens] packages. I run into this class every once in a while -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13592 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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 RyanGlScott): I'm leery about adding more magic classes to GHC, but fortunately, this magic already exists as `Data.Coerce.Coercible`: {{{#!hs import Data.Coerce pack :: Coercible on n => on -> n pack = coerce unpack :: Coercible n on => n -> on unpack = coerce newtype Example = Example String deriving Show main :: IO () main = do print (pack "hello" :: Example) print (unpack (Example "world") :: String) }}} Does this suit your needs? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13592#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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

#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): In a way it's a subset of `Coercible` but I don't know if there is any way to specify that more forcefully than ''default signatures''.. a `Coercible` superclass '''might''' work if we use fundeps {{{#!hs class (Coercible on n, Coercible n on) => Newtype n on | n -> on where }}} or {{{#!hs class (Coercible (O n) n, Coercible n (O n)) => Newtype n where type O n pack :: O n -> n pack = coerce unpack :: n -> O n unpack = coerce }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13592#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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): '''Even''' if we remove `type O` from the class the compiler only needs to generate the `type instance`s for `O` {{{#!hs type family O n class (Coercible (O n) n, Coercible n (O n)) => Newtype n where pack :: O n -> n pack = coerce unpack :: n -> O n unpack = coerce instance (Coercible (O n) n, Coercible n (O n)) => Newtype n }}} if you want to make `Sum` into a newtype, all the compiler has to do is generate {{{#!hs type instance O (Sum a) = a }}} pretty nifty! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13592#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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: | LevityPolymorphism 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 Iceland_jack): * keywords: => LevityPolymorphism -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13592#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC