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

Till Mossakowski till at informatik.uni-bremen.de
Fri Aug 5 02:04:53 EDT 2005


Sebastian Sylvan wrote:
> On 8/4/05, Till Mossakowski <till at informatik.uni-bremen.de> wrote:
>>Remi Turk wrote:
>>>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).
>>>I think one could call the ST monad a safe yet still efficient
>>>variant of unsafePerformIO + IORef's + IOArray's.
>>No, the point of ST is that it is safe (as opposed to unsafePerformIO),
>>but still has the advantages of being both efficient and allowing
>>purely functional encapsulation via runST (as oppesed to IORefs
>>and IOArrays). The only price is that you need rank-2 polymorphism
>>and new language primitives for creating, reading and writing
>>references. But using these primitives is much better than using
>>unsafePerformIO - the latter entails a lot of harmful things.
> 
> Hmmm... Wasn't that what he said?

I disagree with the equation "primitives = unsafe" that
is implicit in sentence

  to be implemented _efficiently_, also needs
  something like unsafePerformIO (or even lower-level unsafe
  mutable state primitives).

The point is that ST uses *safe* primitives, and not "something
like unsafePerformIO".
To clarify things, here a little table about different possibilities
of using arrays in Haskell:

               MonadState  IOArray IOArray          ST
               with                with             with
               FiniteMap           unsafePerformIO  MutArr

safe           yes        yes         no            yes

efficient      no         yes         yes           yes

allows         yes        no          yes           yes
functional
encapsulation

avoids         yes        no          no            no
in-place-
update
primitives

avoids         yes        yes         yes           no
rank 2

-- 
Till Mossakowski               Phone +49-421-218-4683
Dept. of Computer Science      Fax +49-421-218-3054
University of Bremen           till at tzi.de
P.O.Box 330440, D-28334 Bremen http://www.tzi.de/~till


More information about the Haskell mailing list