Well, Yes, If the consumer side consume the result properly (not
getting in the way of GC), a space leak could be avoided.
On Tue, Jun 19, 2018, 9:54 PM Dr.Koster <drkoster@qq.com> wrote:
In general infinite monadic recursion will leak, since the tail position is always >> or >>= instead of your own recursive function
That seems a bit strong. Monadic recursion *can* leak, but it need not. >>= is typically lazy in its second argument, so it can produce structure lazily. Consider the free monad, for example:
data Free f a = Pure a | Wrap (f (Free f a))
instance Functor f => Monad (Free f) wherePure a >>= f = f aWrap ff >>= f = Wrap $ fmap (>>= f) ff
See how we can produce Wrap without using f? As long as the base functor isn't too large, this shouldn't leak.
, 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@gmail.com>Date: Wed,Jun 20,2018 2:37 AMTo: haskell-cafe <haskell-cafe@haskell.org>Subject: Re: [Haskell-cafe] Memory leak in infinite recursion
Hello everyone
In C you can't implement main loop with recursion likevoid mainLoop() {doSomething();mainLoop();}because without optimisations stack will overflow.In haskell it's common to writemainLoop = 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 ExitSuccessWhat about this case?mainLoopModeA = dodoSomethingwhen condition mainLoopModeBmainLoopModeAmainLoopModeB = dodoSomethingElsewhen anotherCondition mainLoopModeAmainLoopModeB
or this case?mainLoopModeA = dodoSomethingif conditionthen mainLoopModeBelse mainLoopModeAmainLoopModeB = dodoSomethingElseif anotherConditionthen mainLoopModeAelse mainLoopModeB
--
Nikita Fufaev
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.