[Haskell-cafe] Plug space leak with seq. How?

Alexey Khudyakov alexey.skladnoy at gmail.com
Thu Jun 9 18:41:40 CEST 2011


On 09.06.2011 20:09, Yves Parès wrote:
> Is it not:
>
>  > noLeak :: State Int ()
>  > noLeak = do
>  >   a <- get
> *>* *let a' = (a + 1)
>  >   a' `seq` put a'*
>  >   noLeak
>
> ??
>

Indeed. Now I understand. It didn't work for me earlier because of
different behavior of 'forever' in ghci and compiled code.

This function leaks in ghci and do not in compiled code without 
optimizations (with optimizations GHC is smart enough  to make 
everything strict).

 > noLeak = forever $ do { a <- get; let a' = a+1; a' `seq` put a' }

Function with explicit recursion do not leak in both cases.

 > noLeak = do { a <- get; let a' = a+1; a' `seq` put a'; noLeak }

What causes this difference?



More information about the Haskell-Cafe mailing list