Skip to content

EnumFromThenTo is is not a good producer

{-# OPTIONS_GHC -Wall #-}

module Foo where
        
testFromTo :: Int -> Int
testFromTo n = length ([0..(10^n)] :: [Int])

testFromThenTo :: Int -> Int
testFromThenTo n = length ([0,2..(10^n)] :: [Int])
$ ghci -fobject-code -O 
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> :load Foo
:load Foo
[1 of 1] Compiling Foo              ( Foo.hs, Foo.o )
Ok, modules loaded: Foo (Foo.o).
Prelude Foo> :set +s
:set +s
Prelude Foo> testFromTo 5
testFromTo 5
100001
(0.02 secs, 97,992 bytes)
Prelude Foo> testFromThenTo 5
testFromThenTo 5
50001
(0.00 secs, 5,694,424 bytes)
Prelude Foo> testFromThenTo 6
testFromThenTo 6
500001
(0.02 secs, 56,095,288 bytes)
Prelude Foo> 

I set the Type to bug rather than feature request as the source code in http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Enum.html#enumFromTo seems to be trying to do list fusion:

-- efdInt and efdtInt deal with [a,b..] and [a,b..c].
-- The code is more complicated because of worries about Int overflow.

-- See Note [How the Enum rules work]
{-# RULES
"efdtInt"       [~1] forall x1 x2 y.
                     efdtInt x1 x2 y = build (\ c n -> efdtIntFB c n x1 x2 y)
"efdtIntUpList" [1]  efdtIntFB (:) [] = efdtInt
 #-}
Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority low
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system MacOS X
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information