Skip to content

Infinite recursion for a program with optimization

Summary

The following program eats all system memory when compiled with -O. Uncommenting and enabling the second take call in it avoids the issue. Compiling it with no optimization works as well.

Steps to reproduce

scoreWeek :: [Double] -> [Double]
scoreWeek xs = take 168 . drop 84 .
  map (sum . map (\(i, x) -> x*fromIntegral i) . zip [-83..84]) $
  scanr (:) [] $ {-take (3*168) $-} cycle xs

main = print $ length $ scoreWeek [0.1,0.2..16.8]

Compile with ghc --make -O.

Expected behavior

The program should print 168 fairly rapidly.

Environment

  • GHC version used: 8.4.4 (as packaged in Debian)

They reproduced it with 8.6.5 on #ghc.

Optional:

  • Operating System: Debian
  • System Architecture: amd64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information