dejafu/Control/State.hs

38 lines
816 B
Haskell
Raw Normal View History

2015-02-10 01:04:28 +03:00
{-# LANGUAGE Rank2Types #-}
-- | Dealing with mutable state.
module Control.State where
import Control.Monad.ST (ST)
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Data.STRef (STRef, newSTRef, readSTRef, writeSTRef)
-- | Mutable references.
data Ref n r = Ref
{ newRef :: forall a. a -> n (r a)
, readRef :: forall a. r a -> n a
, writeRef :: forall a. r a -> a -> n ()
}
-- | Method dict for 'ST'.
refST :: Ref (ST t) (STRef t)
refST = Ref
{ newRef = newSTRef
, readRef = readSTRef
, writeRef = writeSTRef
}
-- | Method dict for 'IO'.
refIO :: Ref IO IORef
refIO = Ref
{ newRef = newIORef
, readRef = readIORef
, writeRef = writeIORef
}
-- | Wrapped mutable references.
data Wrapper n r m = Wrapper
{ wref :: Ref n r
, liftN :: forall a. n a -> m a
}