[Haskell-cafe] STM and random numbers

2007-01-12 Thread Chad Scherrer
Hi, I'd like to be able to use randomIO, but I'm working within the context of STM. Is there a way to get these working together happily? For now, I guess I could kludgingly use unsafePerformIO inside STM (it's the other way around that's not allowed, right?), but I would need to be sure it does

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Lemmih
On 1/12/07, Chad Scherrer <[EMAIL PROTECTED]> wrote: Hi, I'd like to be able to use randomIO, but I'm working within the context of STM. Is there a way to get these working together happily? For now, I guess I could kludgingly use unsafePerformIO inside STM (it's the other way around that's not

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Robert Dockins
On Jan 12, 2007, at 10:58 AM, Chad Scherrer wrote: Hi, I'd like to be able to use randomIO, but I'm working within the context of STM. Is there a way to get these working together happily? For now, I guess I could kludgingly use unsafePerformIO inside STM (it's the other way around that's not

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Henning Thielemann
On Fri, 12 Jan 2007, Robert Dockins wrote: > Humm... I'd actually suggest you stop trying to break the rules, and use the > portion of the random interface that doesn't require IO. You can pretty > easily wrap a StdGen using StateT, and write your stuff in the monad (StateT > StdGen STM). > > O

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Chad Scherrer
Wow, lots of great ideas. Thanks, guys. Lemmih, I worry about the uncertainty in the semantics that seems to be introduced by the unsafe stuff. But I actually hadn't noticed GHC.Conc.unsafeIOToSTM before, so it's good to know it's there. Rich, Even if I use randomIO outside the STM code, I don't

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Rich Neswold
On 1/12/07, Chad Scherrer <[EMAIL PROTECTED]> wrote: Even if I use randomIO outside the STM code, I don't know of a (safe) way to bring it in. Define your STM action to be (Int -> STM s). Generate the random number and then pass it in: mySTM :: Int -> STM a mySTM n = do { ... } To use it: do

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Sebastian Sylvan
On 1/12/07, Rich Neswold <[EMAIL PROTECTED]> wrote: On 1/12/07, Chad Scherrer <[EMAIL PROTECTED]> wrote: > Even if I use randomIO outside the STM code, I don't know of a (safe) > way to bring it in. Define your STM action to be (Int -> STM s). Generate the random number and then pass it in: myS

Re: [Haskell-cafe] STM and random numbers

2007-01-12 Thread Matthew Brecknell
> Rather than having a separate thread computing the random numbers > using IO, why not just stick an StdGen in a TVar and write a function > like: > > type RandomVar = TVar StdGen > > rnd :: RandomVar -> STM a > rnd var = do > g <- readTVar var > let (r,g') = random g > writeTVar var g' > re

Re: [Haskell-cafe] STM and random numbers

2007-01-13 Thread Tomasz Zielonka
On Sat, Jan 13, 2007 at 01:49:36PM +1000, Matthew Brecknell wrote: > > Rather than having a separate thread computing the random numbers > > using IO, why not just stick an StdGen in a TVar and write a function > > like: > > > > type RandomVar = TVar StdGen > > > > rnd :: RandomVar -> STM a > > r