[Haskell-cafe] Haskell-Cafe Digest, Vol 106, Issue 38
John Lato
jwlato at gmail.com
Wed Jun 27 05:18:16 CEST 2012
On Wed, Jun 27, 2012 at 9:15 AM, Richard O'Keefe <ok at cs.otago.ac.nz> wrote:
>
> On 27/06/2012, at 12:51 PM, John Lato wrote:
>>
>> data Tree a = Leaf a | Branch (Tree a) ( Tree a)
>> deriving (Foldable, Show)
>
> While I am familiar with deriving (Show),
> I am not familiar with deriving (Foldable),
> which looks rather useful.
>
> http://www.haskell.org/ghc/docs/7.4.2/html/users_guide/deriving.html
> just says "With -XDeriveFoldable, you can derive instances of the
> class Foldable, defined in Data.Foldable." but it provides no details.
>
> Would you care to explain more about deriving (Foldable)?
There's not much to explain, DeriveFoldable basically does just that;
automatically provide an instance of the Foldable class for a data
type. I think the original proposal for DeriveFoldable was from Twan
van Laarhoven, http://www.mail-archive.com/haskell-prime@haskell.org/msg02116.html,
and there's a little bit of history on GHC's trac,
http://hackage.haskell.org/trac/ghc/ticket/2953. The current
implementation probably hasn't changed much since Simon PJ's original
patch, although there's probably substantial overlap with ghc's
generics these days.
As for the Foldable class itself, the docs at
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Foldable.html
are pretty good. It lets you perform a number of folds (left, right,
monoidal) over arbitrary datatypes, not just lists.
I think that's about it. Although I'm not the best person to explain
either the DeriveFoldable algorithm or the different uses of folds;
maybe someone else would be able to fill in anything I've missed.
John L.
More information about the Haskell-Cafe
mailing list