[Haskell] Monadic Loops

Georg Martius mai99dgf at studserv.uni-leipzig.de
Thu Jun 17 05:55:20 EDT 2004


Hi,

On Thu, 17 Jun 2004 21:19:17 +1200, Vivian McPhail <vivian.mcphail at paradise.net.nz> wrote:

> Hi,
>
> I've implemented a Neural Net simulator which needs to repeat a training loop many times.

It seams we are doing the same thing. I will share my one quite soon.

For this I used a while function:
>
> while test body = do
>                   (cond,res) <- body
>                   if (test cond) then do rs <- while test body
>                                       return (res:rs)
>                   else return [res]
> However, when I run the program, the interpreter falls over after a few thousand iterations, running out of space. I think that this is because the Monadic implementation of the while loop actually nests functions of type (a -> M b) and there is a maximum ?stack size.

Well, there is a maximum stack size. I use ghc and I can give the runtime system the stacksize it shoudld use. However I guess you real problem is not the stack itself, it is the laziness. I guess you perform every trail or epoch some weight updates. That means you are doing some kind of network transformation. Because Haskell is lazy the result of your transformations won't get evaluated until you actually force it to. In case your condition  (cond) doesn't force the evaluation of all transformations you get large unevaluated stuff in you memory.

>
> Is there a better way to implement (possibly infinite) loops in Haskell?
>
I' am curious as well!

Cheers,
   Georg
-- 

---- Georg Martius,  Tel: (034297) 89434 ----
------- http://www.flexman.homeip.net ---------


More information about the Haskell mailing list