{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Mutable where
import Control.Monad.Primitive (PrimMonad, PrimState)
import Control.Monad.ST (ST, runST)
class Thaw a where
type Mutable s a
thaw :: (PrimMonad m, PrimState m ~ s) => a -> m (Mutable s a)
freeze :: (PrimMonad m, PrimState m ~ s) => Mutable s a -> m a
unsafeThaw :: (PrimMonad m, PrimState m ~ s) => a -> m (Mutable s a)
unsafeThaw = a -> m (Mutable s a)
forall a (m :: * -> *) s.
(Thaw a, PrimMonad m, PrimState m ~ s) =>
a -> m (Mutable s a)
thaw
unsafeFreeze :: (PrimMonad m, PrimState m ~ s) => Mutable s a -> m a
unsafeFreeze = Mutable s a -> m a
forall a (m :: * -> *) s.
(Thaw a, PrimMonad m, PrimState m ~ s) =>
Mutable s a -> m a
freeze
create :: Thaw a => (forall s. ST s (Mutable s a)) -> a
create :: (forall s. ST s (Mutable s a)) -> a
create forall s. ST s (Mutable s a)
st = (forall s. ST s a) -> a
forall a. (forall s. ST s a) -> a
runST (ST s (Mutable s a)
forall s. ST s (Mutable s a)
st ST s (Mutable s a) -> (Mutable s a -> ST s a) -> ST s a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Mutable s a -> ST s a
forall a (m :: * -> *) s.
(Thaw a, PrimMonad m, PrimState m ~ s) =>
Mutable s a -> m a
unsafeFreeze)
createT :: (Traversable f, Thaw a) => (forall s. ST s (f (Mutable s a))) -> f a
createT :: (forall s. ST s (f (Mutable s a))) -> f a
createT forall s. ST s (f (Mutable s a))
st = (forall s. ST s (f a)) -> f a
forall a. (forall s. ST s a) -> a
runST (ST s (f (Mutable s a))
forall s. ST s (f (Mutable s a))
st ST s (f (Mutable s a))
-> (f (Mutable s a) -> ST s (f a)) -> ST s (f a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Mutable s a -> ST s a) -> f (Mutable s a) -> ST s (f a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Mutable s a -> ST s a
forall a (m :: * -> *) s.
(Thaw a, PrimMonad m, PrimState m ~ s) =>
Mutable s a -> m a
unsafeFreeze)