[Haskell-cafe] A clarification about what happens under the hood with foldMap

Alfredo Di Napoli alfredo.dinapoli at gmail.com
Tue Oct 23 10:36:34 CEST 2012


---------- Forwarded message ----------
From: Alfredo Di Napoli <alfredo.dinapoli at gmail.com>
Date: 23 October 2012 10:35
Subject: Re: [Haskell-cafe] A clarification about what happens under the
hood with foldMap
To: Chaddaï Fouché <chaddai.fouche at gmail.com>


I'm sure I'm missing a point, but the "minimum" definition for a Foldable
instance is given in terms of foldMap, so I get the cake for free, foldr
included, right?
In the example I have defined my treeSum as:

treeSum = Data.Foldable.foldr (+) 0

So the only thing Haskell knows it that I want to fold over a Foldable for
which foldMap (and therefore foldr) is defined, and specifically I want to
fold using (+) as function.
But foldMap is defined in terms of f, which in this case is Sum, because I
want to sum things. It it were (*) f would have been Product, right?

So what I'm missing is the relation between foldMap and foldr, aka "How
Haskell infer from (+) that I want f = Sum and not something different"?

I hope to have been clearer, don't know if I'm missing something crucial,
though :)

Thanks,
A.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121023/9dc7718d/attachment.htm>


More information about the Haskell-Cafe mailing list