Why no stack overflow in ghci?

The code main = print $ foldl (+) 0 [1..1000000] when compiled (without optimizations) results in a stack overflow, which is expected. However, when run from ghci it succeeds. Why is it so? (Tested with GHC 6.12.something and 7.0.4) Is it related to "stack-squeezing"? I couldn't find any description of it, apart from a few bug reports. A related question: how can I control the stack size in evaluations inside ghci? Is it (roughly) the same as setting the ghci's own stack size by passing -K to it in the command line? -- Roman I. Cheplyaka :: http://ro-che.info/

On 11-07-26 03:57 PM, Roman Cheplyaka wrote:
The code
main = print $ foldl (+) 0 [1..1000000]
when compiled (without optimizations) results in a stack overflow, which is expected.
However, when run from ghci it succeeds. Why is it so? (Tested with GHC 6.12.something and 7.0.4)
To enjoy unlimited stack in compiled code, add +RTS -K10 To enjoy limited stack in ghci, add +RTS -K8M To know why, see my http://www.mail-archive.com/haskell-cafe@haskell.org/msg88368.html

On 26/07/2011 22:41, Albert Y. C. Lai wrote:
On 11-07-26 03:57 PM, Roman Cheplyaka wrote:
The code
main = print $ foldl (+) 0 [1..1000000]
when compiled (without optimizations) results in a stack overflow, which is expected.
However, when run from ghci it succeeds. Why is it so? (Tested with GHC 6.12.something and 7.0.4)
To enjoy unlimited stack in compiled code, add +RTS -K10
To enjoy limited stack in ghci, add +RTS -K8M
To know why, see my http://www.mail-archive.com/haskell-cafe@haskell.org/msg88368.html
The message linked here doesn't really explain *why*, rather it seems to be a bug report on haskell-cafe (which I don't usually read, so it doesn't get fixed!). GHC itself, and hence also GHCi, have a default maximum stack size of 512M, whereas the default for compiled programs is 8M. I think this is probably way too low, but the reason there is a maximum at all is because it is very easy to accidentally write a program with an infinite loop and some OSs cope rather badly when a program eats all the memory. I have no idea what was going on with the magic -K10 option, but it seems to be fixed in 7.2.1: $ ghc +RTS -K1 ghc-stage2: error in RTS option -K1: size outside allowed range (8 - 18446744073709551615) $ ghc +RTS -K8 stack overflow: use +RTS -K<size> to increase it $ ghc +RTS -K60 stack overflow: use +RTS -K<size> to increase it $ ghc +RTS -K80 stack overflow: use +RTS -K<size> to increase it Cheers, Simon

On 11-08-01 10:12 AM, Simon Marlow wrote:
On 26/07/2011 22:41, Albert Y. C. Lai wrote:
http://www.mail-archive.com/haskell-cafe@haskell.org/msg88368.html
The message linked here doesn't really explain *why*, rather it seems to be a bug report on haskell-cafe (which I don't usually read, so it doesn't get fixed!).
Indeed, I didn't want it fixed. I loved that backdoor. XD
participants (3)
-
Albert Y. C. Lai
-
Roman Cheplyaka
-
Simon Marlow