examples using built-in state monad

Marcin 'Qrczak' Kowalczyk qrczak@knm.org.pl
28 Feb 2001 15:28:29 GMT


Mon, 26 Feb 2001 13:07:51 -0800, Konst Sushenko <konsu@microsoft.com> 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 * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK