john at repetae.net
Tue Sep 1 04:01:08 EDT 2009
On Mon, Aug 24, 2009 at 11:59:18PM -0400, Dan Doel wrote:
> On Monday 24 August 2009 11:36:18 pm Bulat Ziganshin wrote:
> > Hello Samuel,
> > Tuesday, August 25, 2009, 4:36:31 AM, you wrote:
> > > Shouldn't Control.Monad.ST export ioToST :: IO a -> ST RealWorld a, as
> > > defined in GHC.IO?
> > no. ST is a subset of IO monad with a limited set of operators
> > guaranteeing referential transparence, as a result runST is a pure
> > operation. if you provide ability to run in ST monad arbitrary
> > operations, this guarantee will break. there are pother way to do the
> > same - use IO operations and unsafePerformIO
> The proposed ioToST yields a value of type 'ST RealWorld a', which is not
> usable with runST, as it is not sufficiently polymorphic, and thus cannot be
> used to write unsafePerformIO.
> The only way you can eventually do anything with a value of the above type
> (short of unsafeCoerce and such) is to use stToIO to put it back into IO, and
> run it from main.
A useful thing you can do with this is make 'pre-fusioned' monad
transformers, like if you want to combine a writer monad with the IO O
monad, you could do something like
newtype IOW w a = IOW (RealWorld -> (# RealWorld, w, a #)
instance Monoid w => Monad (IOW w) where
return x = IOW $ \w -> (# w, mempty, x #)
then use ioToST to write your MonadIO instance.
John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/
More information about the Libraries