examples using built-in state monad

Marcin 'Qrczak' Kowalczyk [email protected]
28 Feb 2001 15:28:29 GMT


Mon, 26 Feb 2001 13:07:51 -0800, Konst Sushenko <[email protected]> pisze:

> newtype State s m a     = ST (s -> m (a,s))
> 
> ghc and hugs contain built in implementation of state monad ST.
>  
> is it the same thing?

No. GHC's and Hugs' ST allows dynamic creation of arbitrary number
of mutable variables of arbitrary types, using operations
    newSTRef   :: a -> ST s (STRef s a)
    readSTRef  :: STRef s a -> ST s a
    writeSTRef :: STRef s a -> a -> ST s ()

The type variable 's' is used in a very tricky way, to ensure
safety when
    runST :: (forall s. ST s a) -> a
is used to wrap the ST-monadic computation in a purely functional
interface. It does not correspond to the type of data being
manipulated.

GHC >= 4.06 contains also a monad like yours, in module MonadState,
available when -package lang option is passed to the compiler.

-- 
 __("<  Marcin Kowalczyk * [email protected] http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK