Skip to content

GHC goes out of memory while compiling simple program with optimizations

When compiling the following program with ghc -O Main.hs, GHC goes out of memory.

import Data.Bits (bit)

main :: IO ()
main = putStrLn (show (f undefined))

f :: [Int] -> Int
f = sum . zipWith ((+) . bit) [0..] . map undefined . scanl undefined undefined

I have 6 GB RAM and 8 GB swap free, so that shouldn't be the problem. It only happens with optimizations on, and it happens during a simplifier. Any simpler expressions do work.

It even happens when [0..] is replaced by take 1 [0..], but it compiles with take 0 [0..].

A straightforward workaround is to replace zipWith f [0..] by \xs -> zipWith f [0..length xs] xs.

I ran into this problem when updating to GHC 8.2.1 from 8.0.2. But the given program doesn't compile on older versions as well. GHC 7.10.3 was the lowest version I could run before running into other problems. All GHC versions I tested come from the Debian repo.

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