<div dir="auto">No, I don't think so. The trouble is that GHC can't assume the Applicative instance is valid. Optimizing this requires knowledge of things like<div dir="auto"><br></div><div dir="auto">liftA3 f (pure x) m (pure y) = (\m' -> f x m' y) <$> m</div><div dir="auto"><br></div><div dir="auto">GHC can only discover such facts when enough inlining and/or specialization happen. Also, figuring out how close you should get to the leaves before trying to coalesce actions may be a judgement call in some cases.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Jan 23, 2017 3:01 PM, "Mario Blažević" <<a href="mailto:mblazevic@stilo.com">mblazevic@stilo.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2017-01-19 02:57 PM, David Feuer wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2. Leaves are really painful. Data structures that store at most one<br>
element per leaf make obvious traversals expensive. For example, in<br>
Data.Map, the obvious implementation would be<br>
<br>
instance Traversable (Map k) where<br>
traverse _ Tip = pure Tip<br>
traverse f (Bin s k v l r) =<br>
liftA3 (Bin s k) (f v) (traverse f l) (traverse f r)<br>
<br>
The trouble is that we would generate a slew of `pure Tip`s that we'd<br>
then have to combine. For instance, (Bin 1 k v Tip Tip) would give<br>
liftA3 (Bin 1 k) (f v) (pure Tip) (pure Tip)<br>
<br>
<br>
It makes me rather sad to have to write disgustingly ugly Traversable<br>
instances just to avoid silly performance issues like this. Does anyone<br>
have an idea for fixing (2), and ideally simultaneously taking care of (1)?<br>
</blockquote>
<br>
I can't offer any immediate fix, but my impression of problem (2) is that it's for compiler to solve. I doesn't seem right that you should be manually enumerating all the simple cases close to the tips and inlining them explicitly.<br>
<br>
Since traverse is forcing the entire spine of the Map, there is not issue with the change in strictness. The compiler should in principle be smart enough to enumerate all possible node shapes and expand the function definitions for each of them. It would be similar to loop unrolling in some ways.<br>
<br>
Since this kind of transformation would probably be expensive at compile-time and would increase the generated code size, it should likely require a command-line option (-O2) or a pragma to activate.<br>
<br>
In short, the solution to problem #2 is to log a GHC proposal and get the burden off your shoulders.<br>
<br>
______________________________<wbr>_________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/libraries</a><br>
</blockquote></div></div>