{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
module Control.Monad.Bayes.Sequential.Coroutine
( Sequential,
suspend,
finish,
advance,
finished,
hoistFirst,
hoist,
sequentially,
sis,
)
where
import Control.Monad.Bayes.Class
( MonadDistribution (bernoulli, categorical, random),
MonadFactor (..),
MonadMeasure,
)
import Control.Monad.Coroutine
( Coroutine (..),
bounce,
mapMonad,
pogoStick,
)
import Control.Monad.Coroutine.SuspensionFunctors
( Await (..),
await,
)
import Control.Monad.Trans (MonadIO, MonadTrans (..))
import Data.Either (isRight)
newtype Sequential m a = Sequential {forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential :: Coroutine (Await ()) m a}
deriving newtype (forall a b. a -> Sequential m b -> Sequential m a
forall a b. (a -> b) -> Sequential m a -> Sequential m b
forall (m :: * -> *) a b.
Functor m =>
a -> Sequential m b -> Sequential m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Sequential m a -> Sequential m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Sequential m b -> Sequential m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> Sequential m b -> Sequential m a
fmap :: forall a b. (a -> b) -> Sequential m a -> Sequential m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Sequential m a -> Sequential m b
Functor, forall a. a -> Sequential m a
forall a b. Sequential m a -> Sequential m b -> Sequential m a
forall a b. Sequential m a -> Sequential m b -> Sequential m b
forall a b.
Sequential m (a -> b) -> Sequential m a -> Sequential m b
forall a b c.
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
forall {m :: * -> *}. Monad m => Functor (Sequential m)
forall (m :: * -> *) a. Monad m => a -> Sequential m a
forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m a
forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m b
forall (m :: * -> *) a b.
Monad m =>
Sequential m (a -> b) -> Sequential m a -> Sequential m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Sequential m a -> Sequential m b -> Sequential m a
$c<* :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m a
*> :: forall a b. Sequential m a -> Sequential m b -> Sequential m b
$c*> :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m b
liftA2 :: forall a b c.
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
<*> :: forall a b.
Sequential m (a -> b) -> Sequential m a -> Sequential m b
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
Sequential m (a -> b) -> Sequential m a -> Sequential m b
pure :: forall a. a -> Sequential m a
$cpure :: forall (m :: * -> *) a. Monad m => a -> Sequential m a
Applicative, forall a. a -> Sequential m a
forall a b. Sequential m a -> Sequential m b -> Sequential m b
forall a b.
Sequential m a -> (a -> Sequential m b) -> Sequential m b
forall (m :: * -> *). Monad m => Applicative (Sequential m)
forall (m :: * -> *) a. Monad m => a -> Sequential m a
forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m b
forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> (a -> Sequential m b) -> Sequential m b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Sequential m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> Sequential m a
>> :: forall a b. Sequential m a -> Sequential m b -> Sequential m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m b
>>= :: forall a b.
Sequential m a -> (a -> Sequential m b) -> Sequential m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> (a -> Sequential m b) -> Sequential m b
Monad, forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: forall (m :: * -> *) a. Monad m => m a -> Sequential m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> Sequential m a
MonadTrans, forall a. IO a -> Sequential m a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
forall {m :: * -> *}. MonadIO m => Monad (Sequential m)
forall (m :: * -> *) a. MonadIO m => IO a -> Sequential m a
liftIO :: forall a. IO a -> Sequential m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> Sequential m a
MonadIO)
extract :: Await () a -> a
(Await () -> a
f) = () -> a
f ()
instance MonadDistribution m => MonadDistribution (Sequential m) where
random :: Sequential m Double
random = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall (m :: * -> *). MonadDistribution m => m Double
random
bernoulli :: Double -> Sequential m Bool
bernoulli = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
categorical :: forall (v :: * -> *).
Vector v Double =>
v Double -> Sequential m Int
categorical = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical
instance MonadFactor m => MonadFactor (Sequential m) where
score :: Log Double -> Sequential m ()
score Log Double
w = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score Log Double
w) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). Monad m => Sequential m ()
suspend
instance MonadMeasure m => MonadMeasure (Sequential m)
suspend :: Monad m => Sequential m ()
suspend :: forall (m :: * -> *). Monad m => Sequential m ()
suspend = forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential forall (m :: * -> *) x. Monad m => Coroutine (Await x) m x
await
finish :: Monad m => Sequential m a -> m a
finish :: forall (m :: * -> *) a. Monad m => Sequential m a -> m a
finish = forall (m :: * -> *) (s :: * -> *) x.
Monad m =>
(s (Coroutine s m x) -> Coroutine s m x) -> Coroutine s m x -> m x
pogoStick forall a. Await () a -> a
extract forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential
advance :: Monad m => Sequential m a -> Sequential m a
advance :: forall (m :: * -> *) a. Monad m => Sequential m a -> Sequential m a
advance = forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (s :: * -> *) x.
(Monad m, Functor s) =>
(s (Coroutine s m x) -> Coroutine s m x)
-> Coroutine s m x -> Coroutine s m x
bounce forall a. Await () a -> a
extract forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential
finished :: Monad m => Sequential m a -> m Bool
finished :: forall (m :: * -> *) a. Monad m => Sequential m a -> m Bool
finished = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. Either a b -> Bool
isRight forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: * -> *) (m :: * -> *) r.
Coroutine s m r -> m (Either (s (Coroutine s m r)) r)
resume forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential
hoistFirst :: (forall x. m x -> m x) -> Sequential m a -> Sequential m a
hoistFirst :: forall (m :: * -> *) a.
(forall x. m x -> m x) -> Sequential m a -> Sequential m a
hoistFirst forall x. m x -> m x
f = forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: * -> *) (m :: * -> *) r.
m (Either (s (Coroutine s m r)) r) -> Coroutine s m r
Coroutine forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x. m x -> m x
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: * -> *) (m :: * -> *) r.
Coroutine s m r -> m (Either (s (Coroutine s m r)) r)
resume forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential
hoist ::
(Monad m, Monad n) =>
(forall x. m x -> n x) ->
Sequential m a ->
Sequential n a
hoist :: forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
(forall x. m x -> n x) -> Sequential m a -> Sequential n a
hoist forall x. m x -> n x
f = forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: * -> *) (m :: * -> *) (m' :: * -> *) x.
(Functor s, Monad m, Monad m') =>
(forall y. m y -> m' y) -> Coroutine s m x -> Coroutine s m' x
mapMonad forall x. m x -> n x
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential
composeCopies :: Int -> (a -> a) -> (a -> a)
composeCopies :: forall a. Int -> (a -> a) -> a -> a
composeCopies Int
k a -> a
f = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall a. a -> a
id (forall a. Int -> a -> [a]
replicate Int
k a -> a
f)
sequentially,
sis ::
Monad m =>
(forall x. m x -> m x) ->
Int ->
Sequential m a ->
m a
sequentially :: forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sequentially forall x. m x -> m x
f Int
k = forall (m :: * -> *) a. Monad m => Sequential m a -> m a
finish forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> (a -> a) -> a -> a
composeCopies Int
k (forall (m :: * -> *) a. Monad m => Sequential m a -> Sequential m a
advance forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(forall x. m x -> m x) -> Sequential m a -> Sequential m a
hoistFirst forall x. m x -> m x
f)
sis :: forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sis = forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sequentially