Module Initialisation? (was Re: [Haskell] (no subject))

Remi Turk rturk at
Sun Oct 17 11:19:25 EDT 2004

On Sun, Oct 17, 2004 at 01:53:22PM +0100, Ben Rudiak-Gould wrote:
> >> Since a lot of the concerns expressed about this seem to centre
> >> around possible abuse of arbitrary IO operations in these top level
> >> constructions, maybe the problem could be addressed by insisting
> >> that a restricted monad was used, call it SafeIO say.
> How about (forall s. ST s)?
> We can require module init actions to have a type (forall s. ST s a) 
> instead of IO a. The compiler or RTS wraps the actions with stToIO 
> (which is a safe function) before executing them.
> Benefits:
>    * It's just as easy as before to allocate global refs (and global 
> mutable arrays).
>    * It's still possible to perform arbitrary IO actions (e.g. FFI 
> calls), but you have to wrap them in unsafeIOToST -- a good thing since 
> they really are unsafe. unsafeIOToST is much safer than unsafePerformIO 
> when used in this way.
> Problems:
>    * stToIO (newSTRef 'x') doesn't have type IO (IORef Char).
> This problem can be solved by adopting a reform that I've wanted for a 
> long time anyway: make IO, IORef, etc. aliases for (ST RealWorld), 
> (STRef RealWorld), etc. instead of completely different types. Then 
> stToIO is the identity function and we only need a single set of 
> state-thread functions instead of the parallel IO and ST equivalents 
> that we have currently.

It definitely sounds nice, but is it actually possible to
generalize e.g. MVar from "RealWorld" to "forall s" or are we
always going to have to say:

v <- unsafeIOToST (newMVar / newChan ... )

GHC's definition:
data MVar a = MVar (MVar# RealWorld a)

Nobody can be exactly like me. Even I have trouble doing it.

More information about the Haskell mailing list