Portability | portable |
---|---|
Stability | experimental |
Maintainer | Reiner Pope <reiner.pope@gmail.com> |
Safe Haskell | Safe-Infered |
Control.Monad.ST.Freeze
Description
An indexed version of the ST monad with support for safely freezing multiple arrays. Compare to the usual ST monad, where the only support for safely freezing arrays is runSTUArray -- only one array may be frozen.
This version of the ST monad has two distinct stages of processing: the normal stage, and the freeze stage. Reading and writing are permitted in the normal stage; reading and freezing are permitted in the freeze stage. This policy ensures that no writes occur after the arrays have been frozen.
This ST is an indexed monad (see Control.Monad.Indexed) as well
as a normal monad. That is, each monadic value will have an
"ingoing" state thread as well as an "outgoing" state
thread. These state threads are similar to the ST monad's state
thread, except that they are now annotated with a stage name:
either Normal
or Freeze
.
- data Normal s
- data Freeze s
- class IxMonad st => MonadST st where
- type STNormal st s a = st (Normal s) (Normal s) a
- type STFreeze st s a = forall stg. st (stg s) (Freeze s) a
- type STRead st s a = forall stg. st (stg s) (stg s) a
- type ST = STT Identity
- runST :: (forall s. STT Identity (stg1 s) (stg2 s) a) -> a
- data STT m s t a
- class STTBase m where
Stage names
MonadST class
class IxMonad st => MonadST st whereSource
Methods
liftST :: ST s a -> STNormal st s aSource
For lifting any operations containing writes but no freezes.
liftRead :: ST s a -> STRead st s aSource
For lifting any operations containing reads but no writes or freezes.
liftUnsafeFreeze :: ST s a -> STFreeze st s aSource
For lifting an unsafeFreeze
operation
type STNormal st s a = st (Normal s) (Normal s) aSource
A computation containing some writes but no freezes: it starts
| and ends in the Normal
stage.
type STFreeze st s a = forall stg. st (stg s) (Freeze s) aSource
A computation containing some freezes but no writes: it starts in | any stage, but ends in the Freeze stage.
type STRead st s a = forall stg. st (stg s) (stg s) aSource
A computation containing only reads: it starts and ends in any stage, but does not change stage. (Note that there would be no loss of safety in allowing the stage to change, but it may result in ambiguous types, or extra type annotations being required.)
ST monad
ST transformer
An ST monad transformer. However, this is not a genuine monad
transformer, as that would be unsafe (see
http://www.haskell.org/pipermail/glasgow-haskell-users/2009-February/016554.html). To
retain safety, it may only act as a monad transformer over the
Identity
and IO
monads, enforced by the STTBase
typeclass.
Defining STT
as a monad transformer rather than just a monad
allows ST arrays to be used in the IO
monad, bringing safe
freezing also to the IO
monad.