<div dir="ltr"><div>Look how instance for List is defined.<br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><pre><span class="gmail-hs-keyword">instance</span><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#Traversable"><span class="gmail-hs-identifier gmail-hs-type">Traversable</span></a><span> </span><span class="gmail-hs-special">[</span><span class="gmail-hs-special">]</span><span> </span><span class="gmail-hs-keyword">where</span><span>
</span><a name="line-185"></a><span>    </span><span class="gmail-hs-pragma">{-# INLINE traverse #-}</span><span> </span><span class="gmail-hs-comment">-- so that traverse can fuse</span><span>
</span><a name="line-186"></a><span>    </span><a name="local-8214565720324045720"></a><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#traverse" class="gmail-"><span class="gmail-hs-identifier">traverse</span></a><span> </span><a name="local-6989586621679270927"></a><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270927" class="gmail-"><span class="gmail-hs-identifier">f</span></a><span> </span><span class="gmail-hs-glyph">=</span><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#foldr" class="gmail-"><span class="gmail-hs-identifier gmail-hs-var">List</span><span class="gmail-hs-operator gmail-hs-var">.</span><span class="gmail-hs-identifier gmail-hs-var">foldr</span></a><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270928"><span class="gmail-hs-identifier gmail-hs-var">cons_f</span></a><span> </span><span class="gmail-hs-special">(</span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#pure"><span class="gmail-hs-identifier gmail-hs-var">pure</span></a><span> </span><span class="gmail-hs-special">[</span><span class="gmail-hs-special">]</span><span class="gmail-hs-special">)</span><span>
</span><a name="line-187"></a><span>      </span><span class="gmail-hs-keyword">where</span><span> </span><a name="local-6989586621679270928"></a><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270928"><span class="gmail-hs-identifier">cons_f</span></a><span> </span><a name="local-6989586621679270929"></a><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270929"><span class="gmail-hs-identifier">x</span></a><span> </span><a name="local-6989586621679270930"></a><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270930" class="gmail-"><span class="gmail-hs-identifier">ys</span></a><span> </span><span class="gmail-hs-glyph">=</span><span> </span><span class="gmail-hs-special">(</span><span class="gmail-hs-glyph">:</span><span class="gmail-hs-special">)</span><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Functor.html#%3C%24%3E" class="gmail-"><span class="gmail-hs-operator gmail-hs-var"><$></span></a><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270927" class="gmail-"><span class="gmail-hs-identifier gmail-hs-var">f</span></a><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270929"><span class="gmail-hs-identifier gmail-hs-var">x</span></a><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/GHC.Base.html#%3C%2A%3E" class="gmail-"><span class="gmail-hs-operator gmail-hs-var"><*></span></a><span> </span><a href="https://hackage.haskell.org/package/base-4.9.1.0/docs/src/Data.Traversable.html#local-6989586621679270930" class="gmail-"><span class="gmail-hs-identifier gmail-hs-var">ys</span></a><span></span></pre></blockquote><pre>It uses List.foldr. Many other instances do the<span></span> same.<br></pre><pre>Functions in all instances of class should have the same signatures. So we have to add Foldable constraint to the class.<br></pre><pre>Of cause we can implement 'foldr' internaly in 'traverse' if needed (as well as fmap). <br>But this is not so good and more important that in this case we don't know how to derive Traversable instances automatically. <br><br></pre><pre>So the answer - many instances wouldn't compile and DeriveTraversable wouldn't work.<br></pre></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-05-03 12:56 GMT+03:00 Jonathon Delgado <span dir="ltr"><<a href="mailto:voldermort@hotmail.com" target="_blank">voldermort@hotmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">OK, I understand why Traversable is useful here - thank you Chris and Dmitry!<br>
<br>
The next question is why Traversable requires Foldable. I looked at the source, and couldn't see where Foldable is being used, other than as a constraint on Traversable. To put the question differently, what would fail to compile if this constraint was removed?<br>
<br>
<br>
<br>
From: Dmitry Olshansky <<a href="mailto:olshanskydr@gmail.com">olshanskydr@gmail.com</a>><br>
Sent: 03 May 2017 09:53<br>
To: Jonathon Delgado<br>
<div class="HOEnZb"><div class="h5">Cc: <a href="mailto:haskell-cafe@haskell.org">haskell-cafe@haskell.org</a><br>
Subject: Re: [Haskell-cafe] Foldable for (,)<br>
 <br>
<br>
<br>
<br>
<br>
With fmap you can only change all values in some "container".<br>
<br>
 With Foldable you can "fold" it, i.e. calculate some "scalar" result.<br>
<br>
 With Traversable you can "change order of two containers":<br>
> sequenceA [[1,2,3],[4,5]]<br>
[[1,4],[1,5],[2,4],[2,5],[3,4]<wbr>,[3,5]]<br>
> sequenceA ("test",[2,3,4])<br>
[("test",2),("test",3),("test"<wbr>,4)]<br>
> sequenceA ("test",([1,2,3],[4,5,6]))<br>
([1,2,3],("test",[4,5,6]))<br>
<br>
<br>
<br>
<br>
<br>
2017-05-03 12:12 GMT+03:00 Jonathon Delgado  <<a href="mailto:voldermort@hotmail.com">voldermort@hotmail.com</a>>:<br>
 Why do you want to traverse a tuple instead of fmap? i.e. what can you do with Foldable/Traversable for (,) that you can't do with Functor?<br>
<br>
My background, as you can probably guess, is beginner.<br>
<br>
<br>
From: Haskell-Cafe <<a href="mailto:haskell-cafe-bounces@haskell.org">haskell-cafe-bounces@haskell.<wbr>org</a>> on behalf of Chris Smith <<a href="mailto:cdsmith@gmail.com">cdsmith@gmail.com</a>><br>
Sent: 03 May 2017 08:51<br>
To: Tony Morris<br>
Cc: <a href="mailto:haskell-cafe@haskell.org">haskell-cafe@haskell.org</a><br>
Subject: Re: [Haskell-cafe] Foldable for (,)<br>
 <br>
<br>
<br>
<br>
Replying to myself, I suppose one good answer is that whether or not you care about Foldable instances for tuples, you might care about Traversable instances, and those require Foldable as a superclass.<br>
<br>
<br>
For example, one possible specialization of `traverse` is:<br>
<br>
<br>
    traverse :: (a -> IO b) -> (SideValue, a) -> IO (SideValue, b)<br>
<br>
<br>
Jonathon, I don't know how much background you're coming from, so I'd be happy to explain that in more detail if you need it.<br>
<br>
<br>
On Wed, May 3, 2017 at 1:44 AM, Chris Smith  <<a href="mailto:cdsmith@gmail.com">cdsmith@gmail.com</a>> wrote:<br>
<br>
I'm also interested in Jonathon's question, so let me try to bring things back to the question.  Everyone agrees that there's only one reasonable way to define this instance if it exists.  But the question is: why is it defined at all?<br>
<br>
<br>
That's an easy question to answer for Functor, Applicative, and Monad.  But I am having trouble giving a simple or accessible answer for Foldable.  Do you know one?<br>
<br>
<br>
<br>
<br>
On Wed, May 3, 2017 at 1:32 AM, Tony Morris  <<a href="mailto:tonymorris@gmail.com">tonymorris@gmail.com</a>> wrote:<br>
 It's Foldable for ((,) a).<br>
<br>
It is not Foldable for any of these things:<br>
<br>
* (,)<br>
* tuples<br>
* pairs<br>
<br>
In fact, to talk about a Foldable for (,) or "tuples" is itself a kind<br>
error. There is no good English name for the type constructor ((,) a)<br>
which I suspect, along with being unfamiliar with utilising the<br>
practical purpose of types (and types of types) is the root cause of all<br>
the confusion in this discussion.<br>
<br>
Ask yourself what the length of this value is:<br>
<br>
[[1,2,3], [4,5,6]]<br>
<br>
Is it 6? What about this one:<br>
<br>
[(1, 'a'), (undefined, 77)]<br>
<br>
Is it 4? No, obviously not, which we can determine by:<br>
<br>
:kind Foldable :: (* -> *) -> Constraint<br>
:kind [] :: * -> *<br>
<br>
Therefore, there is no possible way that the Foldable instance for []<br>
can inspect the elements (and determine that they are pairs in this<br>
case). By this method, we conclude that the length of the value is 2. It<br>
cannot be anything else, some assumptions about length itself put aside.<br>
<br>
By this ubiquitous and very practical method of reasoning, the length of<br>
any ((,) a) is not only one, but very obviously so.<br>
<br>
<br>
<br>
On 03/05/17 17:21, Jonathon Delgado wrote:<br>
> I sent the following post to the Beginners list a couple of weeks ago (which failed to furnish an actual concrete example that answered the question). Upon request I'm reposting it to Café:<br>
><br>
> I've seen many threads, including the one going on now, about why we need to have:<br>
><br>
> length (2,3) = 1<br>
> product (2,3) = 3<br>
> sum (2,3) = 3<br>
> or (True,False) = False<br>
><br>
> but the justifications all go over my head. Is there a beginner-friendly explanation for why such seemingly unintuitive operations should be allowed by default?<br>
> ______________________________<wbr>_________________<br>
> Haskell-Cafe mailing list<br>
> To (un)subscribe, modify options or view archives go to:<br>
>   <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
> Only members subscribed via the mailman list are allowed to post.<br>
<br>
<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br>
<br>
<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</div></div></blockquote></div><br></div>