Data.Foldable causes missed foldr/build opportunities

Simon Peyton-Jones simonpj at
Fri Jul 26 02:46:39 CEST 2013

If you get stuck with fusion (but only then :-), ask me specifically and I’ll help.


From: Libraries [mailto:libraries-bounces at] On Behalf Of Gabriel Gonzalez
Sent: 25 July 2013 18:56
To: Edward Kmett
Cc: libraries at
Subject: Re: Data.Foldable causes missed foldr/build opportunities

On 07/25/2013 10:41 AM, Edward Kmett wrote:
One of the open concerns about it is definitely ensuring that we get the fusion opportunities we can.

If you put an INLINE pragma on your Foldable version of each do the fusion rules fire after it gets inlined into a call site that uses it as a list?

I tried both INLINE and INLINABLE and neither causes the fusion rules to fire.  I also tried:

* adding an orphan SPECIALIZE rule for `Data.Foldable.mapM_` in the module where I defined `each`

* Specializing the type of `each` to consume lists, but still using the `Foldable` `mapM_`

* Defining a new copy of `each` (using the `Foldable` version) in the same module as the code that uses it, specializing the type signature to lists, and trying out INLINE/INLINABLE or no pragma.

None of those causes the rule to fire, either.

On Thu, Jul 25, 2013 at 1:22 PM, Gabriel Gonzalez <gabriel439 at<mailto:gabriel439 at>> wrote:
I'm now in favor of the `Data.Foldable` proposal, but I just wanted to mention that the proposal needs to include some extra pragma work to ensure that build/foldr optimizations fire.  I was just experimenting with the following combinator for `pipes` trying out the following two versions:

    each :: (Monad m) => [a] -> Producer a m ()
    each = mapM yield

    each :: (Monad m, Foldable f) => f a -> Producer a m ()
    each = Data.Foldable.mapM yield

When I do a pure `pipes`-based fold over both `Producers`s, the version specialized to lists triggers a firing of the build/foldr fusion rule and runs about 20% faster.  The true improvement for `mapM` by itself is probably even greater than that because I haven't optimized the folding code yet.  The latter version does not trigger the rule firing.  Either way I'm going to include the latter `Foldable` version but I just wanted to mention this because I remember people were asking if this would impact fusion or not.

Libraries mailing list
Libraries at<mailto:Libraries at>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list