module Mcmc.Chain.Trace
( Trace,
replicateT,
fromVectorT,
pushT,
headT,
takeT,
freezeT,
thawT,
)
where
import Control.Monad.Primitive
import qualified Data.Stack.Circular as C
import qualified Data.Vector as VB
import Mcmc.Chain.Link
newtype Trace a = Trace {forall a. Trace a -> MStack Vector RealWorld (Link a)
fromTrace :: C.MStack VB.Vector RealWorld (Link a)}
replicateT :: Int -> Link a -> IO (Trace a)
replicateT :: forall a. Int -> Link a -> IO (Trace a)
replicateT Int
n Link a
l = MStack Vector RealWorld (Link a) -> Trace a
forall a. MStack Vector RealWorld (Link a) -> Trace a
Trace (MStack Vector RealWorld (Link a) -> Trace a)
-> IO (MStack Vector RealWorld (Link a)) -> IO (Trace a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Link a -> IO (MStack Vector (PrimState IO) (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Int -> a -> m (MStack v (PrimState m) a)
C.replicate Int
n Link a
l
fromVectorT :: VB.Vector (Link a) -> IO (Trace a)
fromVectorT :: forall a. Vector (Link a) -> IO (Trace a)
fromVectorT Vector (Link a)
xs = MStack Vector RealWorld (Link a) -> Trace a
forall a. MStack Vector RealWorld (Link a) -> Trace a
Trace (MStack Vector RealWorld (Link a) -> Trace a)
-> IO (MStack Vector RealWorld (Link a)) -> IO (Trace a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector (Link a) -> IO (MStack Vector (PrimState IO) (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
v a -> m (MStack v (PrimState m) a)
C.fromVector Vector (Link a)
xs
pushT :: Link a -> Trace a -> IO (Trace a)
pushT :: forall a. Link a -> Trace a -> IO (Trace a)
pushT Link a
x Trace a
t = do
MStack Vector RealWorld (Link a)
s' <- Link a
-> MStack Vector (PrimState IO) (Link a)
-> IO (MStack Vector (PrimState IO) (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
a -> MStack v (PrimState m) a -> m (MStack v (PrimState m) a)
C.push Link a
x (Trace a -> MStack Vector RealWorld (Link a)
forall a. Trace a -> MStack Vector RealWorld (Link a)
fromTrace Trace a
t)
Trace a -> IO (Trace a)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Trace a -> IO (Trace a)) -> Trace a -> IO (Trace a)
forall a b. (a -> b) -> a -> b
$ MStack Vector RealWorld (Link a) -> Trace a
forall a. MStack Vector RealWorld (Link a) -> Trace a
Trace MStack Vector RealWorld (Link a)
s'
{-# INLINEABLE pushT #-}
headT :: Trace a -> IO (Link a)
headT :: forall a. Trace a -> IO (Link a)
headT = MStack Vector RealWorld (Link a) -> IO (Link a)
MStack Vector (PrimState IO) (Link a) -> IO (Link a)
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
MStack v (PrimState m) a -> m a
C.get (MStack Vector RealWorld (Link a) -> IO (Link a))
-> (Trace a -> MStack Vector RealWorld (Link a))
-> Trace a
-> IO (Link a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trace a -> MStack Vector RealWorld (Link a)
forall a. Trace a -> MStack Vector RealWorld (Link a)
fromTrace
{-# INLINEABLE headT #-}
takeT :: Int -> Trace a -> IO (VB.Vector (Link a))
takeT :: forall a. Int -> Trace a -> IO (Vector (Link a))
takeT Int
k = Int
-> MStack Vector (PrimState IO) (Link a) -> IO (Vector (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Int -> MStack v (PrimState m) a -> m (v a)
C.take Int
k (MStack Vector RealWorld (Link a) -> IO (Vector (Link a)))
-> (Trace a -> MStack Vector RealWorld (Link a))
-> Trace a
-> IO (Vector (Link a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trace a -> MStack Vector RealWorld (Link a)
forall a. Trace a -> MStack Vector RealWorld (Link a)
fromTrace
freezeT :: Trace a -> IO (C.Stack VB.Vector (Link a))
freezeT :: forall a. Trace a -> IO (Stack Vector (Link a))
freezeT = MStack Vector RealWorld (Link a) -> IO (Stack Vector (Link a))
MStack Vector (PrimState IO) (Link a) -> IO (Stack Vector (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
MStack v (PrimState m) a -> m (Stack v a)
C.freeze (MStack Vector RealWorld (Link a) -> IO (Stack Vector (Link a)))
-> (Trace a -> MStack Vector RealWorld (Link a))
-> Trace a
-> IO (Stack Vector (Link a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Trace a -> MStack Vector RealWorld (Link a)
forall a. Trace a -> MStack Vector RealWorld (Link a)
fromTrace
thawT :: C.Stack VB.Vector (Link a) -> IO (Trace a)
thawT :: forall a. Stack Vector (Link a) -> IO (Trace a)
thawT Stack Vector (Link a)
t = MStack Vector RealWorld (Link a) -> Trace a
forall a. MStack Vector RealWorld (Link a) -> Trace a
Trace (MStack Vector RealWorld (Link a) -> Trace a)
-> IO (MStack Vector RealWorld (Link a)) -> IO (Trace a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stack Vector (Link a) -> IO (MStack Vector (PrimState IO) (Link a))
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, PrimMonad m) =>
Stack v a -> m (MStack v (PrimState m) a)
C.thaw Stack Vector (Link a)
t