[jhc] Monad.ST

John Meacham john at repetae.net
Sat Nov 14 19:15:40 EST 2009

On Sun, Nov 15, 2009 at 12:38:33AM +0100, Henning Thielemann wrote:
> Before looking into the actual source code of the ST monad, I thought 
> that ST must be something like
>   newtype ST s a = ST (IO a)
> where the strict ST monad is based on normal IO bind function and lazy ST 
> monad also uses unsafeInterleaveIO. Then runST would be just an  
> unsafePerformIO.
> However when looking into
>   http://darcs.haskell.org/packages/st
>  it is in Control.Monad.ST.Lazy
>    newtype ST s a = ST (State s -> (a, State s))
>    data State s = S# (State# s)
>  and in Hugs.ST:
>    newtype ST s a = ST (forall r. (a -> r) -> r)
> Both GHC and Hugs seem to use primitives, that are not available in JHC.  
> Would my IO definition above be correct, at all? It seems that much of 
> the code in the 'st' package could not be re-used in JHC.

In jhc, we would use something similar to what ghc does. the current IO
definition is:

newtype IO a = IO (World__ -> (# World__, a #))

So, the only difference would be ST takes any state, not just the world,

newtype ST s a = ST (s -> (# s, a #))

However, your definition might be better in practice, as it allows easy
transformations between IO and ST as you noted, and there is no way to
specify to jhc that the 's' parameter can be elided always so there is a
chance it may make it into the runtime.


John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/

More information about the jhc mailing list