[Haskell] ST/STRef vs. IO/IORef

Glynn Clements glynn at gclements.plus.com
Wed Aug 3 05:11:37 EDT 2005

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. 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.

Glynn Clements <glynn at gclements.plus.com>

More information about the Haskell mailing list