Skip to content

"ghc: Out of memory" upon compiling combination of map/foldl/setBit

The following code snippet:

module Test where

import Data.Bits (setBit)

f :: Integer
f = foldl setBit 0 toSet
  where
    toSet = [n | (n, _) <- zip [0..] [1]]

Fails to compile, yielding:

martijn@qbltop:~/code/scratch$ ghc Test.hs -O1
[1 of 1] Compiling Test             ( Test.hs, Test.o )
ghc: Out of memory

A few observations:

  • Compiling with no optimizations (-O0) works fine
  • I have not found a function other than setBit which triggers this behavior
  • Changing foldl to its strict brother foldl' does not help
  • Changing the type signature of "f" from "Integer" to "Int" causes the code to compile just fine
  • Changing [0..] to something silly like [0..2^1024] does not trigger the bug

I have been unable to reduce this example any further. I am using GHC 8.2.1.

Edited by martijnbastiaan
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information