Skip to content

selector thunks not working? space leak in standard example

I thought that GHC evaluates "THUNK_SELECTORS" and thus eliminates the space leak in this infamous example [Hughes83][Wadler87][Sparud93]:

module Main where

surprise xs = b1 ++ [0,0] ++ b2
              where
              (b1,b2) = break (==0) xs

strictFromTo :: Int -> Int -> [Int]
strictFromTo i m = takeWhile (<= m) $ strictFrom i

strictFrom :: Int -> [Int]
strictFrom x = let x' = x + 1 in seq x' $ x' : strictFrom x'
        
main = print (length $ surprise $ strictFromTo (-1000000) 1)
       -- space leak, w/ or w/o optimization
main2 = print (length $ strictFromTo (-1000000) 1)
       -- runs in constant space, w/ or w/o optimization

However, this program does not run in constant space, even with -O2. I get the same results on GHC6.6 Mac OS X Intel GHC6.2.1 Linux Intel GHC6.4.1 Linux Intel

Trac metadata
Trac field Value
Version 6.6
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Runtime System
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Multiple
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information