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

Remi Turk rturk at science.uva.nl
Thu Aug 4 14:02:16 EDT 2005

On Thu, Aug 04, 2005 at 10:09:06AM +0100, Axel Simon wrote:
> On Thu, 2005-08-04 at 10:58 +0200, Wolfgang Jeltsch wrote:
> > Am Donnerstag, 4. August 2005 10:21 schrieb Axel Simon:
> > > [...]
> > 
> > > Nowadays, you can use one of the MonadState monad
> > 
> > State transformer monads like State and StateT can be implemented without 
> > using special language features.  So there was always the opportunity to 
> > implement something like State or StateT.  So, in a way, we always could use 
> > the MonadState monads.  If ST could be replaced by MonadState monads, ST had 
> > never been included in the libraries, I suppose.
> Well, MonadState needs multi-parameter type classes, and hence, require
> much more than ST. 

MonadState needs multi-parameter type classes, State and StateT
don't. And ST needs rank-2 types (or at least one rank-2
constant) and, to be implemented _efficiently_, also needs
something like unsafePerformIO (or even lower-level unsafe
mutable state primitives).

> > > if you want lazy computation (on top of which you can implement state read
> > > and write accesses similar to IORefs) or IO-enabled computation (if you use
> > > MonadState.StateT and embed an IO monad at the core).
> > 
> > The point is that the MonadState monads don't give you update-in-place.  
> > Update-in-place is exactly the reason why ST is there.
> Ok, granted. In particular I take your point about array accesses.
> However, I am not quite convinced that using ST has any advantages over
> using IO directly. Of course, one could claim that programmers wants to
> protect themselves from themselves by disallowing arbitrary IO. But if
> that is the only "advantage" then I'd rather go for the flexibility to
> use arbitrary IO later on without having to rewrite my whole program.

Some algorithms are more naturally written imperatively and some
programs are more efficient when written imperatively. The ST
monad makes it possible to write programs which look imperative
and actually _are_ imperative too but still could have been
purely functional (given rank-2 types).

I think one could call the ST monad a safe yet still efficient
variant of unsafePerformIO + IORef's + IOArray's.


Nobody can be exactly like me. Even I have trouble doing it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell/attachments/20050804/ed040577/attachment.bin

More information about the Haskell mailing list