[Haskell-cafe] Re: hmatrix

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue Jun 3 05:42:11 EDT 2008

Hello Alberto,

Tuesday, June 3, 2008, 12:56:50 PM, you wrote:

> Good! So you can easily "hide" the IO operations in the ST monad. I will
> definitely look into it.

from implementation POV ST monad is nothing but renamed IO monad which
exports only subset of its operations which are guaranteed to safe.
or, saying in other words, it's just type hackery around IO monad that
provides safe operations

it's possible to define ST monad and its operations as following:

newtype ST s a = forall s. ST_Constructor (IO a)

unsafeIOtoSt action = ST_Constructor action

runST (ST_Constructor action) = unsafePerformIO action

newtype STRef s a = forall s. STRef (IORef a)

readSTRef (STRef ref)  =  unsafeIOtoSt (readIORef ref)

and so on. GHC uses technically (but not ideologically!) different
implementation where both monads are specializations of one generic
type. while Hugs afair uses exactly this approach. you may also look
at ArrayRef lib which reimplements arrays/refs for both compilers in
more unified way

anyway, because ST is just IO monad modulo type tricks, you can
execute any IO action inside ST by lifting it with unsafeIOtoSt

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list