Re: Re[2]: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Gregory Wright
Hi Bulat! On Aug 24, 2006, at 1:07 PM, Bulat Ziganshin wrote: Hello Brian, Thursday, August 24, 2006, 4:16:41 PM, you wrote: I would make all the fields strict here, to be sure that no lazyness can creep about unseen eg: data Tag s = Tag { tagID :: !Int, state ::

Re[2]: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Bulat Ziganshin
Hello Gregory, Thursday, August 24, 2006, 4:43:57 PM, you wrote: > I agree this should be a FAQ. we already have something like this on performance/strictness wikipage. although adding your example of misusing $! may be helpful - peoples are always better learned on (good and bad) examples rathe

Re[2]: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Bulat Ziganshin
Hello Brian, Thursday, August 24, 2006, 4:16:41 PM, you wrote: > I would make all the fields strict here, to be sure that no lazyness can > creep about unseen eg: > data Tag s = Tag { > tagID :: !Int, > state :: !(STRef s TagState), > count :: !(STRef s Integer) >

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Gregory Wright
Hi Udo, On Aug 24, 2006, at 7:22 AM, Udo Stenzel wrote: Hi Gregory, Gregory Wright wrote: step :: Tag s -> ST s (Maybe Integer) step t = do c <- readSTRef (count t) s <- readSTRef (state t) writeSTRef (count t) (c - 1) writeSTRef (state t) (nextState s)

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Udo Stenzel
Hi Gregory, Gregory Wright wrote: > step :: Tag s -> ST s (Maybe Integer) > step t = do > c <- readSTRef (count t) > s <- readSTRef (state t) > writeSTRef (count t) (c - 1) > writeSTRef (state t) (nextState s) > if (c <= 0) then return Nothing else return (J

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Gregory Wright
Hi Bulat, On Aug 24, 2006, at 7:52 AM, Bulat Ziganshin wrote: Hello Gregory, Thursday, August 24, 2006, 2:29:15 PM, you wrote: step t = do c <- readSTRef (count t) s <- readSTRef (state t) writeSTRef (count t) (c - 1) writeSTRef (state t) (nextState s)

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Brian Hulley
Gregory Wright wrote: -- A structure with internal state: -- data Tag s = Tag { tagID :: Int, state :: STRef s TagState, count :: STRef s Integer } data FrozenTag = FrozenTag { ft_tagID :: Int, ft_state :: TagState, ft_count :: Integer } deriving

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Bulat Ziganshin
Hello Gregory, Thursday, August 24, 2006, 2:29:15 PM, you wrote: > step t = do > c <- readSTRef (count t) > s <- readSTRef (state t) > writeSTRef (count t) (c - 1) > writeSTRef (state t) (nextState s) > if (c <= 0) then return Nothing else return (Just

Re: [Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Chris Kuklewicz
The write*Ref functions, like many "write into data structure" have the common problem of being much lazier than you want. The nextState calls form a lazy thunk. In fact it tries form 10^6 nested thunks to call nextState. So you have to use something like seq to reduce the laziness: step

[Haskell-cafe] stack overflow when using ST monad

2006-08-24 Thread Gregory Wright
Hi, I have a program, abstracted from a larger application that I am writing for a customer, that persistently overflows its stack. The program is a simulation of the communication protocol of a sensor tag. The code is below. The program mimics a hardware state machine. In the example below,