{-# LANGUAGE GeneralizedNewtypeDeriving, TypeFamilies,
MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
module Control.Monad.Trans.Memo.StateCache
(
StateCache,
runStateCache,
container,
setContainer,
evalStateCache
) where
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Array.MArray
import Data.Function
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Fix
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Strict
import Data.Array.Base
import Data.Array.IO
import Data.Array.ST
newtype StateCache c m a = StateCache { toStateT :: StateT c m a }
deriving (Functor, Applicative, Alternative, Monad, MonadPlus, MonadFix, MonadTrans, MonadIO)
{-# INLINE runStateCache #-}
runStateCache :: StateCache s m a -> s -> m (a, s)
runStateCache = runStateT . toStateT
evalStateCache :: Monad m => StateCache c m a -> c -> m a
{-# INLINE evalStateCache #-}
evalStateCache = evalStateT . toStateT
container :: Monad m => StateCache c m c
{-# INLINE container #-}
container = StateCache get
setContainer :: Monad m => c -> StateCache c m ()
{-# INLINE setContainer #-}
setContainer = StateCache . put
instance PrimMonad m => PrimMonad (StateCache c m) where
type PrimState (StateCache c m) = PrimState m
primitive = lift . primitive
instance MArray IOArray e (StateCache c IO) where
getBounds = lift . getBounds
getNumElements = lift . getNumElements
newArray a = lift . newArray a
unsafeRead a = lift . unsafeRead a
unsafeWrite a i = lift . unsafeWrite a i
instance MArray IOUArray e IO => MArray IOUArray e (StateCache c IO) where
getBounds = lift . getBounds
getNumElements = lift . getNumElements
newArray a = lift . newArray a
unsafeRead a = lift . unsafeRead a
unsafeWrite a i = lift . unsafeWrite a i
instance MArray (STArray s) e (StateCache c (ST s)) where
getBounds = lift . getBounds
getNumElements = lift . getNumElements
newArray a = lift . newArray a
unsafeRead a = lift . unsafeRead a
unsafeWrite a i = lift . unsafeWrite a i
instance MArray (STUArray s) e (ST s) => MArray (STUArray s) e (StateCache c (ST s)) where
getBounds = lift . getBounds
getNumElements = lift . getNumElements
newArray a = lift . newArray a
unsafeRead a = lift . unsafeRead a
unsafeWrite a i = lift . unsafeWrite a i