Another space leak question

Srineet srineet@email.com
Tue, 3 Jul 2001 13:33:57 +0530


Thanks! Indeed some random numbers might not get always evaluated, the way
the code is in the first version. Lemme just put it here,

The function that steps one helicopter:
> stepHC    :: Int -> Helicopter -> Helicopter

> -- handling the case of a helicopter going left to right
> stepHC r (HC (Just x, y) DRight) | x >= (hcRight - hcBmpWd)  = HC
(Nothing, y) DRight
>                                                    | otherwise    =  HC
(Just (x+hcStep), y) DRight
> stepHC r (HC (Nothing, y) DRight) | r > 1 = HC (Nothing, y) DRight
>                                                        | otherwise = HC
(Just hcLeft, y) DRight

> -- now a helicopter going right to left
> stepHC r (HC (Just x, y) DLeft)   |  x <= (hcLeft + hcStep)   = HC
(Nothing, y) DLeft
>                                                    | otherwise = HC (Just
(x - hcStep), y) DLeft
> stepHC r (HC (Nothing, y) DLeft)  |  r > 1    =  HC (Nothing, y) DLeft
>                                                       | otherwise = HC
(Just (hcRight - hcBmpWd),y) DLeft
------------------------------------------
The function that steps all my helicopters:

> stepHCs   ::  PTState -> PTState
> stepHCs s = let
>   (rs, ns)    = stGetRandoms numHelicopters s
>   fns = map stepHC rs
>   hcs = zipWith ($) fns (stGetHCs ns)
---------------------------------------------

I have already given "stGetRandoms". It is:
> stGetRandom   :: PTState -> (Int, PTState)
> stGetRandom (bmps, r:rs, hcs) = (r, (bmps, rs, hcs))

Now, as we see, the random number doesn't always get evaluated. So I tried
two things:
1. Putting "seq" in stGetRandoms, so that "r" will be evaluated. Didn't help
:(
2. In stepHC above, putting "r > 0 && " in every guard (hoping "r" will
always get evaluated). But even this doesn't remove the space leak.

I still think, its something to do with this list of random numbers, since
removing it out of the PTSTate tuple and passing it as a parameter, has
removed the space leak. I wuld like to get to the bottom of this, just so
that I understand why there's a leak, and in general increase my Haskell
knowledge. Please help.

Thanks. Bye.

- Srineet.

P.S. I've only got HOOD, and yes, when I try and observe the state, the
random list component, is a huge infinite list with many unevaluated numbers
first, and some evaluated numbers sprinkled here and there. But why is this
happening?



----- Original Message -----
From: "Alastair David Reid" <reid@cs.utah.edu>
To: "Srineet" <srineet@email.com>
Cc: <haskell-cafe@haskell.org>; <kahl@heraklit.informatik.unibw-muenchen.de>
Sent: Tuesday, July 03, 2001 12:13 AM
Subject: Re: Another space leak question


>
> [Metacomment: the only way to really answer your question is to use
> one of the debugging tools mentioned previously in the space leak
> threads.  For large programs, anything else is just a list of
> guesses.]
>
> I think the space leak lies in the code you did not show us.
>
> What does step look like?
> If it looks like this:
>
>   step (a,b,c) = (a',b',c') where ...
>
> then the space leak is _not_ caused by bundling up the state - you
> could move the take and drop inside the definition of step (which
> would make your code a bit cleaner and more modular too.)
>
> How do you access the list of helicopters inside step?
> It could be that one of the random numbers you're asking for isn't being
> evaluated and you're being left with a thunk like this:
>
>   head randoms
>
> where, in the first version of your code, randoms is an infinite list
> and, in the second version, it is a small, finite list.
>
> Maybe all you need to do is change the definition of step to:
>
>   step (a,b,c) = c' `seq` (a',b',c') where ...
>
>
> Finally, if this doesn't work (and debugging tools fail you) my
> experience of arcade games in Haskell is that nearly all the state is
> used to generate an image at each step.  If your problem is a
> strictness bug, you might be able to find it just by thinking about
> what parts of the state are _not_ used to generate the image.
>
> --
> Alastair Reid        reid@cs.utah.edu        http://www.cs.utah.edu/~reid/
>