Roman Cheplyaka | 26 Jul 21:57 2011

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/
Albert Y. C. Lai | 26 Jul 23:41 2011
Picon

Re: Why no stack overflow in ghci?

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 <at> haskell.org/msg88368.html
Simon Marlow | 1 Aug 16:12 2011
Picon

Re: Why no stack overflow in ghci?

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 <at> 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 
(Continue reading)

Albert Y. C. Lai | 3 Aug 02:28 2011
Picon

Re: Why no stack overflow in ghci?

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 <at> 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

Gmane