Unexpected stack overflow prevented by superfluous type annotation
GHC versions affected:
OS versions tested:
- OS X Snow Leopard
- Ubuntu Linux 9.10 - Karmic Koala
The attached program computes Pi by integrating 4/(1+x*x) between 0 and 1.
The important line of code is this one:
h * (sum (map area [1..n]))
If you compile the program as it is, with this command:
ghc --make -O2 Pi.hs
it runs out of stack space when run like so:
./Pi 1000000 Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize -RTS' to increase it.
For GHC versions 6.10.4, 6.12.1 and 6.13.20090922 However, if you add what appears to be a superfluous type annotation to the line of code, like so:
h * ((sum (map area [1..n])) :: Double)
The program runs to completion with no stack overflow:
./Pi 1000000 3.1415926535897643
Indeed it works just fine for even larger inputs.
However version 6.13.20100915 overflows the stack regardless of whether the type annotation is present or not.
I don't think the type annotation should make any difference to the generated code because GHC should infer that the annotated expression is of type Double anyway (if you put an erroneous type in the same place, GHC complains that it should be a Double).