ST monad and monad tranformers

Tyson Whitehead twhitehead at gmail.com
Mon Feb 2 17:03:28 EST 2009


On February 2, 2009 11:26:02 Tyson Whitehead wrote:
> The STT type above is a version of ST like the ReaderT, StateT, etc. types.
>
> newtype STT s m a = STT ( State# s -> m (STTBox s a) )
> data STTBox s a = STTBox {-#UNPACK#-} !(State# s) {-#UNPACK#-} !a
>
> runSTT :: (Monad m) => (forall s. STT s m a) -> m a
> runSTT m = case m of STT m' -> do STTBox _ x <- m' realWorld#
>                                   return x
>
> instance Monad m => Monad (STT s m) where
>     return x = STT $ \s -> return $ STTBox s x
>     (STT m) >>= k = STT $ \s -> do STTBox s' x <- m s
>                                    case k x of STT k' -> k' s'

Of course, I forgot the method to actually use state threaded code

stToSTT :: Monad m => ST s a -> STT s m a
stToSTT (ST m) = STT $ \s -> case m s of (# s',x #) -> return $ STTBox s' x

In re-reading my original email, I also thought I might not have been clear 
that I did write the instance methods (MonadCont, etc.), I just didn't include 
them as they would have made the email too lengthy.

Cheers!  -Tyson

PS:  Thanks for all the comments so far.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20090202/5b340e61/attachment.bin


More information about the Glasgow-haskell-users mailing list