
#10447: DeriveFoldable rejects instances with constraints in last argument of data type -------------------------------------+------------------------------------- Reporter: RyanGlScott | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: GHC rejects | Unknown/Multiple valid program | Test Case: Blocked By: | Blocking: Related Tickets: #8678 | Differential Revisions: -------------------------------------+------------------------------------- Comment (by RyanGlScott): The code that I would expect {{{DeriveFoldable}}} to derive for {{{T1}}}, {{{T2}}}, or {{{T3}}} would be: {{{#!hs instance Foldable T where foldr f z (MkT a) = f a z foldMap f (MkT a) = f a }}} which is the same code that you would get for {{{data T a = MkT a deriving Foldable}}}. I don't believe this would require any change to the {{{deriving Foldable}}} algorithm (but I'm not intimately familiar with the implementation details). I agree that it would be nice to have more documentation on the algorithms themselves. I gained a better intuition for the [http://git.haskell.org/ghc.git/blob/9f968e97a0de9c2509da00f6337b612dd72a0389... Functor], [http://git.haskell.org/ghc.git/blob/9f968e97a0de9c2509da00f6337b612dd72a0389... Foldable], and [http://git.haskell.org/ghc.git/blob/9f968e97a0de9c2509da00f6337b612dd72a0389... Traversable] algorithms by reading the comments in {{{TcGenDeriv.hs}}}. For example, here's a somewhat formal description of how {{{deriving Foldable}}} works: {{{ The cases are: $(foldr 'a 'b) = \x z -> z -- when b does not contain a $(foldr 'a 'a) = f $(foldr 'a '(b1,b2)) = \x z -> case x of (x1,x2) -> $(foldr 'a 'b1) x1 ( $(foldr 'a 'b2) x2 z ) $(foldr 'a '(T b1 b2)) = \x z -> foldr $(foldr 'a 'b2) z x -- when a only occurs in the last parameter, b2 }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10447#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler