Timeline for Weirdest way to produce a stack overflow
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 17, 2014 at 14:15 | comment | added | blabla999 | haskell is cool. | |
Feb 17, 2014 at 13:44 | comment | added | ceased to turn counterclockwis | BTW, none of this is really specified by the Haskell standard: it's merely required that evaluation is non-strict, i.e. a nonterminating computation won't block forever if the result is not fully required. How long it really blocks is up to the implementation, in standard lazy GHC it doesn't block at all until you request the result. | |
Feb 17, 2014 at 13:38 | comment | added | ceased to turn counterclockwis |
@blabla999: well, leaving out the print would simply mean foldl never even starts to do anything: there would be just a single thunk representing the entire sum (_) expression, and that would then be GC'ed in one go. But, yeah, the GC can in more interesting cases collect thunks as-you-go, that's where lazyness becomes really interesting (e.g. allowing you to process infinite lists).
|
|
Feb 17, 2014 at 13:23 | comment | added | blabla999 | aah - interesting, so if no one would wait for the thunk (i.e. leaving out the print), the garbage collector would collect them away (from front to back)? | |
Feb 17, 2014 at 13:13 | comment | added | ceased to turn counterclockwis |
@blabla999: tail-calls aren't that relevant in Haskell, it's mostly thunk buildup due to covert lazyness that's causing such problems. In this case, the issue is that sum is implemented in terms of foldl , which does use tail calls, but because it doesn't evaluate the accumulator strictly merely produces a pile of thunks as large as the original list. The problem disappears when switching to foldl' (+) , that evaluates strictly and thus returns a WHN in its tail call. Or, as I said, if you switch on GHC's optimisations!
|
|
Feb 17, 2014 at 11:55 | comment | added | blabla999 | shouldn't it do a tail call optimization automatically (in sum)? | |
Feb 17, 2014 at 11:09 | history | answered | ceased to turn counterclockwis | CC BY-SA 3.0 |