Hi Cafe,<div><br></div><div>I was playing with the classic example of a Foldable structure: Trees.</div><div>So the code you can find both on Haskell Wiki and LYAH is the following:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">
<div><div>data Tree a = Empty | Node (Tree a) a (Tree a) deriving (Show, Eq)</div></div><div><div><br></div></div><div><div>instance Foldable Tree where</div></div><div><div>    foldMap f Empty = mempty</div></div><div><div>
    foldMap f (Node l p r) = foldMap f l `mappend` f p `mappend` foldMap f r</div></div><div><br></div><div><div>treeSum :: Tree Int -&gt; Int</div></div><div><div>treeSum = Data.Foldable.foldr (+) 0</div></div><div><br></div>
<div><br></div></blockquote>What this code does is straighforward. I was struck from the following sentences in LYAH: <div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<span style="font-family:Arial,sans-serif;font-size:14.399999618530273px;line-height:25px;background-color:rgb(255,255,255)">Notice that we didn&#39;t have to provide the function that takes a value and returns a monoid value. <br>
</span><span style="font-family:Arial,sans-serif;font-size:14.399999618530273px;line-height:25px;background-color:rgb(255,255,255)">We receive that function as a parameter to </span><span class="fixed" style="margin:0px;padding:0px 3px;border:0px;outline:0px;font-size:14.399999618530273px;vertical-align:baseline;white-space:nowrap;font-family:Consolas,&#39;Courier New&#39;,monospace;background-color:rgb(221,221,221);font-weight:bold;line-height:25px">foldMap</span><span style="font-family:Arial,sans-serif;font-size:14.399999618530273px;line-height:25px;background-color:rgb(255,255,255)"> and all we have to decide is where to apply <br>
</span><span style="font-family:Arial,sans-serif;font-size:14.399999618530273px;line-height:25px;background-color:rgb(255,255,255)">that function and how to join up the resulting monoids from it.</span></blockquote><div><div>
<br></div><div>This is obvious, so in case of (+) f = Sum, so f 3 = Sum 3 which is a Monoid.</div></div><div>What I was wondering about is how Haskell knows that it has to pass, for example, Sum in case of (+) and Product in case of (*)?</div>
<div>In other term, I&#39;m missing the logical piece of the puzzle which maps the associative binary function passed to fold up to our foldMap declaration.</div><div><br></div><div>Thanks for any explanation :)</div><div>
<br></div><div>Cheers,</div><div>A.</div>