[Haskell] Re: ST/STRef vs. IO/IORef

Srinivas Nedunuri nedunuri at cs.utexas.edu
Wed Aug 3 13:07:11 EDT 2005


"Glynn Clements" <glynn at gclements.plus.com> wrote in message
news:17136.35401.310280.912763 at cerise.gclements.plus.com...
>
> Srinivas Nedunuri wrote:
>
> > Hello, I have some code that manipulates STRefs within the ST monad. All
> > good and fine, until I come across some computation that uses lets say
> > IO and everything skids to a halt. At this point I have 3 choices:
> >
> > 1. Define a ST State Transformer monad and do all my previous ST
> > computations in that
> > 2. convert all subsequent ST computations into IO computations using
> > stToIO
> > 3. stop using the ST monad and do everything in the IO monad
> >
> > I was wondering what advice folks had. In particular, what are the
> > disadvantages to doing everything in the IO monad - ie why even bother
> > with the ST monad?
>
> The most obvious disadvantage is that the IO monad has no equivalent
> of runST.
OK, I'm missing something here. What is the big deal about runST? Can I not
get the IO equivalent by simply running the program at the top level
(assuming I don't have multiple threads going). Do you have a practical
example of needing runST in several places in your program?

> Also, there is no ioToST (only unsafeIOToST), so if you use
> the IO monad, the code can only be used within the IO monad. The IO
> monad is like a "trap"; once your inside, you can't get out.
True but I'm effectively trappend now anyway. I have a bunch of ST code, and
then somewhere in there I had the misfortune of needing to insert a file
copy and bam I'm now stuck with the dang IO monad which goes and "infects"
the entire program.

>
> -- 
> Glynn Clements <glynn at gclements.plus.com>





More information about the Haskell mailing list