[Haskell-cafe] Memory leak in infinite recursion

Dr.Koster drkoster at qq.com
Wed Jun 20 01:54:22 UTC 2018

In general infinite monadic recursion will leak, since the tail position is always >> or >>= instead of your own recursive function, but under certain situations, e.g. IO without arguments, the compiler will figure there's no need to push new stack frame. But anyway it's better to checkout yourself rather relying on some weak assumptions.


------------------ Original ------------------
From: Никита Фуфаев <kitttoran at gmail.com>
Date: Wed,Jun 20,2018 2:37 AM
To: haskell-cafe <haskell-cafe at haskell.org>
Subject: Re: [Haskell-cafe] Memory leak in infinite recursion

Hello everyone

In C you can't implement main loop with recursion like
void mainLoop() {
because without optimisations stack will overflow.
In haskell it's common to write
mainLoop = doSomething >> mainLoop, and it doesn't leak memory because of haskell's evaluation model.
Does memory leak or argument stack overflow happen in this case?
mainLoop = doSomething >> mainLoop >> exit ExitSuccess
What about this case?
mainLoopModeA = do
  when condition mainLoopModeB
mainLoopModeB = do
  when anotherCondition mainLoopModeA

or this case?
mainLoopModeA = do
  if condition
    then mainLoopModeB
    else mainLoopModeA
mainLoopModeB = do
  if anotherCondition 
    then mainLoopModeA
    else mainLoopModeB

Nikita Fufaev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20180620/dda33fc3/attachment.html>

More information about the Haskell-Cafe mailing list