[Haskell-cafe] Composing functions with runST

Udo Stenzel u.stenzel at web.de
Mon Jan 1 15:32:46 EST 2007

Yitzchak Gale wrote:
> It seems to me that a natural notion of a state transformer
> in the ST monad is the type:
> STRef s st -> ST s a

Are there any useful functions of this type?  I guess, your intention is
that this "transformer" makes no other use of the ST monad than reading
or writing a single variable.  It seems, every such function better had
a purely functional interface anyway, even if it makes use of runST

> stToState :: MonadState st m => (STRef s st -> ST s a) -> m a
> The type signatures above do ensure (as far as I can see)
> that the opacity of the ST state thread is not violated.

I doubt that.  The "transformer" you pass in could have captured
references from a different state thread, which is exactly the problem
the rank-2 type should prevent.  I guess, the type signature you want is

stToState :: MonadState st m => (forall s . STRef s st -> ST s a) -> m a

which should actually work with runST and which would also be a bit
pointless (see above).  At least if I got rank-2 types correctly, which
isn't guaranteed.

> Any ideas? A better approach?

Uhm... use MonadState in the first place?  The converse is comparatively
easily accomplished:

stateToST :: STRef s st -> State st a -> ST s a
stateToST ref action = do (a, st') <- readSTRef ref >>= runState action
                          writeSTRef ref st'
			  return a

"Human legalese is the schema language of our society."
	-- Tim Berners-Lee in http://w3.org/DesignIssues/Evolution
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070101/fd4ba31e/attachment.bin

More information about the Haskell-Cafe mailing list