[Haskell-cafe] Rewrite rules for enumFromTo

Michael Snoyman michael at snoyman.com
Wed Apr 18 18:21:35 CEST 2012

Hi all,

Following a little thread on Reddit[1], I'm trying to add rewrite
rules to conduit to make some simple usages of `yield` more efficient.
I've pushed these changes to a branch on Github[2]. However, I'm not
able to fully optimize the following program:

import Data.Conduit
import qualified Data.Conduit.List as CL

main :: IO ()
main = do
    x <- mapM_ yield [1..1000] $$ CL.fold (+) 0
    print (x :: Int)

Ideally, I would like to rewrite the entirety of `mapM_ yield
[1..1000]` to `Data.Conduit.List.enumFromTo 1 1000` and thereby avoid
the intermediate list. However, whenever I add such a rule, it doesn't
fire. Instead, -ddump-rule-firings tells me:

Rule fired: Class op enumFromTo
Rule fired: mapM_ yield
Rule fired: Class op +
Rule fired: Class op >>=
Rule fired: Class op show
Rule fired: eftIntList

I'm quite a novice at rewrite rules; can anyone recommend an approach
to get my rule to fire first?


PS: In case you're wondering, the `mapM_ yield` rule turns `mapM_
yield` into `yieldMany`. So ideally, I'd like to have another rule
that turns `yieldMany [x..y]` into `Data.Conduit.List.enumFromTo x y`.

[1] http://www.reddit.com/r/haskell/comments/sdzmx/many_ways_to_skin_a_conduit/c4dftb9
[2] https://github.com/snoyberg/conduit/tree/rewrite

More information about the Haskell-Cafe mailing list