
#13328: Foldable, Functor, and Traversable deriving handle phantom types badly -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: 8.4.1 Component: Compiler | Version: 8.1 Resolution: | Keywords: deriving-perf Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): Two more thoughts to consider. One: how should this interact with your feature request in #13117? That is, you had previously requested that if you wrote this: {{{#!hs data V a deriving Functor }}} then GHC should generate this: {{{#!hs instance Functor V where fmap _ x = case x of {} }}} But `V`'s type parameter is phantom here, so you could just as well implement it like this (as Eric Mertens originally pointed out in https://mail.haskell.org/pipermail/libraries/2017-January/027603.html): {{{#!hs instance Functor V where fmap _ = coerce }}} Which choice should we make here? Two (a follow-up question to comment:1, after reading Eric's comment about role annotations in https://mail.haskell.org/pipermail/libraries/2017-January/027603.html): consider this code: {{{#!hs data Foo a = Foo deriving instance Foldable Foo }}} In this scenario, the type parameter to `Foo` is phantom, so the generated `Foldable` instance is: {{{#!hs instance Foldable Foo where foldMap _ _ = mempty }}} Now what happens if you add a role annotation to `Foo`? {{{#!hs type role Foo nominal data Foo a = Foo deriving instance Foldable Foo }}} Now, since `Foo`'s type parameter is no longer phantom, the generated `Foldable` instance will be: {{{#!hs instance Foldable Foo where foldMap _ Foo = mempty }}} That is to say, the choice of role can affect what the strictness of `foldMap` will be. Is this desirable? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13328#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler