<div dir="ltr">The code as it currently stands only has two nested fmaps / foldMaps / traverses.<div><br></div><div>The reason he can do that is because Scope is defined as an "f (Var b a)". Since Scope is only a functor if f is also functor, that means you can fmap over f, regardless of what it is. But in addition to that Var is also a functor. So you can fmap over f, and then fmap over the Var inside the f, which ends up being two nested fmaps. That same condition exists for foldable and traversable.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 17, 2018 at 6:46 AM, Anthony Lee <span dir="ltr"><<a href="mailto:anthonynlee@gmail.com" target="_blank">anthonynlee@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>In Scope.hs there are some functions I feel difficult to understand,</div><div>Why fmap/foldmap/traverse is applied three times?<br></div><div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono","monospace",monospace,"Droid Sans Fallback";font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(86,156,214)">instance</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Functor</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">=></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Functor</span><span style="color:rgb(212,212,212)"> (</span><span style="color:rgb(86,156,214)">Scope</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">b</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)">) </span><span style="color:rgb(86,156,214)">where</span></div><div><span style="color:rgb(212,212,212)"> fmap f (Scope a) </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> Scope (fmap (fmap (fmap f)) a)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">{-# INLINE fmap #-}</span></div><br><div><span style="color:rgb(96,139,78)">-- | @'toList'@ is provides a list (with duplicates) of the free variables</span></div><div><span style="color:rgb(86,156,214)">instance</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Foldable</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">=></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Foldable</span><span style="color:rgb(212,212,212)"> (</span><span style="color:rgb(86,156,214)">Scope</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">b</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)">) </span><span style="color:rgb(86,156,214)">where</span></div><div><span style="color:rgb(212,212,212)"> foldMap f (Scope a) </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> foldMap (foldMap (foldMap f)) a</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">{-# INLINE foldMap #-}</span></div><br><div><span style="color:rgb(86,156,214)">instance</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Traversable</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">=></span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">Traversable</span><span style="color:rgb(212,212,212)"> (</span><span style="color:rgb(86,156,214)">Scope</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">b</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">f</span><span style="color:rgb(212,212,212)">) </span><span style="color:rgb(86,156,214)">where</span></div><div><span style="color:rgb(212,212,212)"> traverse f (Scope a) </span><span style="color:rgb(212,212,212)">=</span><span style="color:rgb(212,212,212)"> Scope </span><span style="color:rgb(212,212,212)"><$></span><span style="color:rgb(212,212,212)"> traverse (traverse (traverse f)) a</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(86,156,214)">{-# INLINE traverse #-}</span></div></div></div></div>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>