{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}

-- |
-- Module      : Control.Monad.Bayes.Sequential
-- Description : Suspendable probabilistic computation
-- Copyright   : (c) Adam Scibior, 2015-2020
-- License     : MIT
-- Maintainer  : leonhard.markert@tweag.io
-- Stability   : experimental
-- Portability : GHC
--
-- 'Sequential' represents a computation that can be suspended.
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)

-- | Represents a computation that can be suspended at certain points.
-- The intermediate monadic effects can be extracted, which is particularly
-- useful for implementation of Sequential Monte Carlo related methods.
-- All the probabilistic effects are lifted from the transformed monad, but
-- also `suspend` is inserted after each `factor`.
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 -> b) -> Sequential m a -> Sequential m b)
-> (forall a b. a -> Sequential m b -> Sequential m a)
-> Functor (Sequential m)
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
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Sequential m a -> Sequential m b
fmap :: forall a b. (a -> b) -> Sequential m a -> Sequential m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> Sequential m b -> Sequential m a
<$ :: forall a b. a -> Sequential m b -> Sequential m a
Functor, Functor (Sequential m)
Functor (Sequential m)
-> (forall a. a -> Sequential m a)
-> (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 a b. Sequential m a -> Sequential m b -> Sequential m b)
-> (forall a b. Sequential m a -> Sequential m b -> Sequential m a)
-> Applicative (Sequential m)
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
$cpure :: forall (m :: * -> *) a. Monad m => a -> Sequential m a
pure :: forall a. a -> Sequential m a
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
Sequential m (a -> b) -> Sequential m a -> Sequential m b
<*> :: forall a b.
Sequential m (a -> b) -> Sequential m a -> Sequential m b
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
liftA2 :: forall a b c.
(a -> b -> c) -> Sequential m a -> Sequential m b -> Sequential m c
$c*> :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> Sequential m b -> Sequential m b
*> :: 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 a
<* :: forall a b. Sequential m a -> Sequential m b -> Sequential m a
Applicative, Applicative (Sequential m)
Applicative (Sequential m)
-> (forall a b.
    Sequential m a -> (a -> Sequential m b) -> Sequential m b)
-> (forall a b. Sequential m a -> Sequential m b -> Sequential m b)
-> (forall a. a -> Sequential m a)
-> Monad (Sequential m)
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
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
Sequential m a -> (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 -> Sequential m b -> Sequential m b
>> :: forall a b. Sequential m a -> Sequential m b -> Sequential m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> Sequential m a
return :: forall a. a -> Sequential m a
Monad, (forall (m :: * -> *) a. Monad m => m a -> Sequential m a)
-> MonadTrans Sequential
forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall (m :: * -> *) a. Monad m => m a -> Sequential m a
lift :: forall (m :: * -> *) a. Monad m => m a -> Sequential m a
MonadTrans, Monad (Sequential m)
Monad (Sequential m)
-> (forall a. IO a -> Sequential m a) -> MonadIO (Sequential m)
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
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> Sequential m a
liftIO :: forall a. IO a -> Sequential m a
MonadIO)

extract :: Await () a -> a
extract :: forall a. Await () a -> a
extract (Await () -> a
f) = () -> a
f ()

instance MonadDistribution m => MonadDistribution (Sequential m) where
  random :: Sequential m Double
random = m Double -> Sequential m Double
forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m Double
forall (m :: * -> *). MonadDistribution m => m Double
random
  bernoulli :: Double -> Sequential m Bool
bernoulli = m Bool -> Sequential m Bool
forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Bool -> Sequential m Bool)
-> (Double -> m Bool) -> Double -> Sequential m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> m Bool
forall (m :: * -> *). MonadDistribution m => Double -> m Bool
bernoulli
  categorical :: forall (v :: * -> *).
Vector v Double =>
v Double -> Sequential m Int
categorical = m Int -> Sequential m Int
forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m Int -> Sequential m Int)
-> (v Double -> m Int) -> v Double -> Sequential m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. v Double -> m Int
forall (v :: * -> *). Vector v Double => v Double -> m Int
forall (m :: * -> *) (v :: * -> *).
(MonadDistribution m, Vector v Double) =>
v Double -> m Int
categorical

-- | Execution is 'suspend'ed after each 'score'.
instance MonadFactor m => MonadFactor (Sequential m) where
  score :: Log Double -> Sequential m ()
score Log Double
w = m () -> Sequential m ()
forall (m :: * -> *) a. Monad m => m a -> Sequential m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Log Double -> m ()
forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score Log Double
w) Sequential m () -> Sequential m () -> Sequential m ()
forall a b. Sequential m a -> Sequential m b -> Sequential m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Sequential m ()
forall (m :: * -> *). Monad m => Sequential m ()
suspend

instance MonadMeasure m => MonadMeasure (Sequential m)

-- | A point where the computation is paused.
suspend :: Monad m => Sequential m ()
suspend :: forall (m :: * -> *). Monad m => Sequential m ()
suspend = Coroutine (Await ()) m () -> Sequential m ()
forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential Coroutine (Await ()) m ()
forall (m :: * -> *) x. Monad m => Coroutine (Await x) m x
await

-- | Remove the remaining suspension points.
finish :: Monad m => Sequential m a -> m a
finish :: forall (m :: * -> *) a. Monad m => Sequential m a -> m a
finish = (Await () (Coroutine (Await ()) m a) -> Coroutine (Await ()) m a)
-> Coroutine (Await ()) m a -> m a
forall (m :: * -> *) (s :: * -> *) x.
Monad m =>
(s (Coroutine s m x) -> Coroutine s m x) -> Coroutine s m x -> m x
pogoStick Await () (Coroutine (Await ()) m a) -> Coroutine (Await ()) m a
forall a. Await () a -> a
extract (Coroutine (Await ()) m a -> m a)
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequential m a -> Coroutine (Await ()) m a
forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential

-- | Execute to the next suspension point.
-- If the computation is finished, do nothing.
--
-- > finish = finish . advance
advance :: Monad m => Sequential m a -> Sequential m a
advance :: forall (m :: * -> *) a. Monad m => Sequential m a -> Sequential m a
advance = Coroutine (Await ()) m a -> Sequential m a
forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential (Coroutine (Await ()) m a -> Sequential m a)
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> Sequential m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Await () (Coroutine (Await ()) m a) -> Coroutine (Await ()) m a)
-> Coroutine (Await ()) m a -> Coroutine (Await ()) m a
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 Await () (Coroutine (Await ()) m a) -> Coroutine (Await ()) m a
forall a. Await () a -> a
extract (Coroutine (Await ()) m a -> Coroutine (Await ()) m a)
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> Coroutine (Await ()) m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequential m a -> Coroutine (Await ()) m a
forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential

-- | Return True if no more suspension points remain.
finished :: Monad m => Sequential m a -> m Bool
finished :: forall (m :: * -> *) a. Monad m => Sequential m a -> m Bool
finished = (Either (Await () (Coroutine (Await ()) m a)) a -> Bool)
-> m (Either (Await () (Coroutine (Await ()) m a)) a) -> m Bool
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Either (Await () (Coroutine (Await ()) m a)) a -> Bool
forall a b. Either a b -> Bool
isRight (m (Either (Await () (Coroutine (Await ()) m a)) a) -> m Bool)
-> (Sequential m a
    -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> Sequential m a
-> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coroutine (Await ()) m a
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall (s :: * -> *) (m :: * -> *) r.
Coroutine s m r -> m (Either (s (Coroutine s m r)) r)
resume (Coroutine (Await ()) m a
 -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequential m a -> Coroutine (Await ()) m a
forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential

-- | Transform the inner monad.
-- This operation only applies to computation up to the first suspension.
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 = Coroutine (Await ()) m a -> Sequential m a
forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential (Coroutine (Await ()) m a -> Sequential m a)
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> Sequential m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Either (Await () (Coroutine (Await ()) m a)) a)
-> Coroutine (Await ()) m a
forall (s :: * -> *) (m :: * -> *) r.
m (Either (s (Coroutine s m r)) r) -> Coroutine s m r
Coroutine (m (Either (Await () (Coroutine (Await ()) m a)) a)
 -> Coroutine (Await ()) m a)
-> (Sequential m a
    -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> Sequential m a
-> Coroutine (Await ()) m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Either (Await () (Coroutine (Await ()) m a)) a)
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall x. m x -> m x
f (m (Either (Await () (Coroutine (Await ()) m a)) a)
 -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> (Sequential m a
    -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> Sequential m a
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coroutine (Await ()) m a
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall (s :: * -> *) (m :: * -> *) r.
Coroutine s m r -> m (Either (s (Coroutine s m r)) r)
resume (Coroutine (Await ()) m a
 -> m (Either (Await () (Coroutine (Await ()) m a)) a))
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> m (Either (Await () (Coroutine (Await ()) m a)) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequential m a -> Coroutine (Await ()) m a
forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential

-- | Transform the inner monad.
-- The transformation is applied recursively through all the suspension points.
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 = Coroutine (Await ()) n a -> Sequential n a
forall (m :: * -> *) a. Coroutine (Await ()) m a -> Sequential m a
Sequential (Coroutine (Await ()) n a -> Sequential n a)
-> (Sequential m a -> Coroutine (Await ()) n a)
-> Sequential m a
-> Sequential n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. m x -> n x)
-> Coroutine (Await ()) m a -> Coroutine (Await ()) n a
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 m y -> n y
forall x. m x -> n x
f (Coroutine (Await ()) m a -> Coroutine (Await ()) n a)
-> (Sequential m a -> Coroutine (Await ()) m a)
-> Sequential m a
-> Coroutine (Await ()) n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequential m a -> Coroutine (Await ()) m a
forall (m :: * -> *) a. Sequential m a -> Coroutine (Await ()) m a
runSequential

-- | Apply a function a given number of times.
composeCopies :: Int -> (a -> a) -> (a -> a)
composeCopies :: forall a. Int -> (a -> a) -> a -> a
composeCopies Int
k a -> a
f = ((a -> a) -> (a -> a) -> a -> a) -> (a -> a) -> [a -> a] -> a -> a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) a -> a
forall a. a -> a
id (Int -> (a -> a) -> [a -> a]
forall a. Int -> a -> [a]
replicate Int
k a -> a
f)

-- | Sequential importance sampling.
-- Applies a given transformation after each time step.
sequentially,
  sis ::
    Monad m =>
    -- | transformation
    (forall x. m x -> m x) ->
    -- | number of time steps
    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 = Sequential m a -> m a
forall (m :: * -> *) a. Monad m => Sequential m a -> m a
finish (Sequential m a -> m a)
-> (Sequential m a -> Sequential m a) -> Sequential m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Sequential m a -> Sequential m a)
-> Sequential m a
-> Sequential m a
forall a. Int -> (a -> a) -> a -> a
composeCopies Int
k (Sequential m a -> Sequential m a
forall (m :: * -> *) a. Monad m => Sequential m a -> Sequential m a
advance (Sequential m a -> Sequential m a)
-> (Sequential m a -> Sequential m a)
-> Sequential m a
-> Sequential m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. m x -> m x) -> Sequential m a -> Sequential m a
forall (m :: * -> *) a.
(forall x. m x -> m x) -> Sequential m a -> Sequential m a
hoistFirst m x -> m x
forall x. m x -> m x
f)

-- | synonym
sis :: forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sis = (forall x. m x -> m x) -> Int -> Sequential m a -> m a
forall (m :: * -> *) a.
Monad m =>
(forall x. m x -> m x) -> Int -> Sequential m a -> m a
sequentially