[Haskell-cafe] Composing functions with runST

Anatoly Zaretsky anatoly.zaretsky at gmail.com
Mon Jan 1 15:06:13 EST 2007


On 1/1/07, Yitzchak Gale <gale at sefer.org> wrote:
>
> stToState :: MonadState st m => (STRef s st -> ST s a) -> m a
>
> That would make it very convenient, for example, to
> use arrays inside a pure state monad.
>
> The type signatures above do ensure (as far as I can see)
> that the opacity of the ST state thread is not violated. But
> unfortunately, the protective shield in runST created by the
> higher-rank polymorphism is too thick.

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

E.g.

stToState :: MonadState st m => (forall s. STRef s st -> ST s a) -> m a
stToState f = do
  s <- get
  let (x, s') = runST (do
        r <- newSTRef s
        x <- f r
        s' <- readSTRef r
        return (x, s'))
  put s'
  return x

--
Best regerds,
Tolik


More information about the Haskell-Cafe mailing list