{-# LANGUAGE RankNTypes #-}

-- |
-- Module      : Control.Monad.Bayes.Traced.Dynamic
-- Description : Distributions on execution traces that can be dynamically frozen
-- Copyright   : (c) Adam Scibior, 2015-2020
-- License     : MIT
-- Maintainer  : leonhard.markert@tweag.io
-- Stability   : experimental
-- Portability : GHC
module Control.Monad.Bayes.Traced.Dynamic
  ( TracedT,
    hoist,
    marginal,
    freeze,
    mhStep,
    mh,
  )
where

import Control.Monad (join)
import Control.Monad.Bayes.Class
  ( MonadDistribution (random),
    MonadFactor (..),
    MonadMeasure,
  )
import Control.Monad.Bayes.Density.Free (DensityT)
import Control.Monad.Bayes.Traced.Common
  ( Trace (..),
    bind,
    mhTransFree,
    scored,
    singleton,
  )
import Control.Monad.Bayes.Weighted (WeightedT)
import Control.Monad.Trans (MonadTrans (..))
import Data.List.NonEmpty as NE (NonEmpty ((:|)), toList)

-- | A tracing monad where only a subset of random choices are traced and this
-- subset can be adjusted dynamically.
newtype TracedT m a = TracedT {forall (m :: * -> *) a.
TracedT m a -> m (WeightedT (DensityT m) a, Trace a)
runTraced :: m (WeightedT (DensityT m) a, Trace a)}

pushM :: (Monad m) => m (WeightedT (DensityT m) a) -> WeightedT (DensityT m) a
pushM :: forall (m :: * -> *) a.
Monad m =>
m (WeightedT (DensityT m) a) -> WeightedT (DensityT m) a
pushM = WeightedT (DensityT m) (WeightedT (DensityT m) a)
-> WeightedT (DensityT m) a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (WeightedT (DensityT m) (WeightedT (DensityT m) a)
 -> WeightedT (DensityT m) a)
-> (m (WeightedT (DensityT m) a)
    -> WeightedT (DensityT m) (WeightedT (DensityT m) a))
-> m (WeightedT (DensityT m) a)
-> WeightedT (DensityT m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DensityT m (WeightedT (DensityT m) a)
-> WeightedT (DensityT m) (WeightedT (DensityT m) a)
forall (m :: * -> *) a. Monad m => m a -> WeightedT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DensityT m (WeightedT (DensityT m) a)
 -> WeightedT (DensityT m) (WeightedT (DensityT m) a))
-> (m (WeightedT (DensityT m) a)
    -> DensityT m (WeightedT (DensityT m) a))
-> m (WeightedT (DensityT m) a)
-> WeightedT (DensityT m) (WeightedT (DensityT m) a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (WeightedT (DensityT m) a)
-> DensityT m (WeightedT (DensityT m) a)
forall (m :: * -> *) a. Monad m => m a -> DensityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance (Monad m) => Functor (TracedT m) where
  fmap :: forall a b. (a -> b) -> TracedT m a -> TracedT m b
fmap a -> b
f (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) b, Trace b) -> TracedT m b)
-> m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall a b. (a -> b) -> a -> b
$ do
    (WeightedT (DensityT m) a
m, Trace a
t) <- m (WeightedT (DensityT m) a, Trace a)
c
    let m' :: WeightedT (DensityT m) b
m' = (a -> b) -> WeightedT (DensityT m) a -> WeightedT (DensityT m) b
forall a b.
(a -> b) -> WeightedT (DensityT m) a -> WeightedT (DensityT m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f WeightedT (DensityT m) a
m
    let t' :: Trace b
t' = (a -> b) -> Trace a -> Trace b
forall a b. (a -> b) -> Trace a -> Trace b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Trace a
t
    (WeightedT (DensityT m) b, Trace b)
-> m (WeightedT (DensityT m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WeightedT (DensityT m) b
m', Trace b
t')

instance (Monad m) => Applicative (TracedT m) where
  pure :: forall a. a -> TracedT m a
pure a
x = m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) a, Trace a) -> TracedT m a)
-> m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall a b. (a -> b) -> a -> b
$ (WeightedT (DensityT m) a, Trace a)
-> m (WeightedT (DensityT m) a, Trace a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> WeightedT (DensityT m) a
forall a. a -> WeightedT (DensityT m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x, a -> Trace a
forall a. a -> Trace a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
  (TracedT m (WeightedT (DensityT m) (a -> b), Trace (a -> b))
cf) <*> :: forall a b. TracedT m (a -> b) -> TracedT m a -> TracedT m b
<*> (TracedT m (WeightedT (DensityT m) a, Trace a)
cx) = m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) b, Trace b) -> TracedT m b)
-> m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall a b. (a -> b) -> a -> b
$ do
    (WeightedT (DensityT m) (a -> b)
mf, Trace (a -> b)
tf) <- m (WeightedT (DensityT m) (a -> b), Trace (a -> b))
cf
    (WeightedT (DensityT m) a
mx, Trace a
tx) <- m (WeightedT (DensityT m) a, Trace a)
cx
    (WeightedT (DensityT m) b, Trace b)
-> m (WeightedT (DensityT m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WeightedT (DensityT m) (a -> b)
mf WeightedT (DensityT m) (a -> b)
-> WeightedT (DensityT m) a -> WeightedT (DensityT m) b
forall a b.
WeightedT (DensityT m) (a -> b)
-> WeightedT (DensityT m) a -> WeightedT (DensityT m) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> WeightedT (DensityT m) a
mx, Trace (a -> b)
tf Trace (a -> b) -> Trace a -> Trace b
forall a b. Trace (a -> b) -> Trace a -> Trace b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Trace a
tx)

instance (Monad m) => Monad (TracedT m) where
  (TracedT m (WeightedT (DensityT m) a, Trace a)
cx) >>= :: forall a b. TracedT m a -> (a -> TracedT m b) -> TracedT m b
>>= a -> TracedT m b
f = m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) b, Trace b) -> TracedT m b)
-> m (WeightedT (DensityT m) b, Trace b) -> TracedT m b
forall a b. (a -> b) -> a -> b
$ do
    (WeightedT (DensityT m) a
mx, Trace a
tx) <- m (WeightedT (DensityT m) a, Trace a)
cx
    let m :: WeightedT (DensityT m) b
m = WeightedT (DensityT m) a
mx WeightedT (DensityT m) a
-> (a -> WeightedT (DensityT m) b) -> WeightedT (DensityT m) b
forall a b.
WeightedT (DensityT m) a
-> (a -> WeightedT (DensityT m) b) -> WeightedT (DensityT m) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m (WeightedT (DensityT m) b) -> WeightedT (DensityT m) b
forall (m :: * -> *) a.
Monad m =>
m (WeightedT (DensityT m) a) -> WeightedT (DensityT m) a
pushM (m (WeightedT (DensityT m) b) -> WeightedT (DensityT m) b)
-> (a -> m (WeightedT (DensityT m) b))
-> a
-> WeightedT (DensityT m) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((WeightedT (DensityT m) b, Trace b) -> WeightedT (DensityT m) b)
-> m (WeightedT (DensityT m) b, Trace b)
-> m (WeightedT (DensityT m) b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (WeightedT (DensityT m) b, Trace b) -> WeightedT (DensityT m) b
forall a b. (a, b) -> a
fst (m (WeightedT (DensityT m) b, Trace b)
 -> m (WeightedT (DensityT m) b))
-> (a -> m (WeightedT (DensityT m) b, Trace b))
-> a
-> m (WeightedT (DensityT m) b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TracedT m b -> m (WeightedT (DensityT m) b, Trace b)
forall (m :: * -> *) a.
TracedT m a -> m (WeightedT (DensityT m) a, Trace a)
runTraced (TracedT m b -> m (WeightedT (DensityT m) b, Trace b))
-> (a -> TracedT m b) -> a -> m (WeightedT (DensityT m) b, Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> TracedT m b
f
    Trace b
t <- Trace a -> m (Trace a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Trace a
tx m (Trace a) -> (a -> m (Trace b)) -> m (Trace b)
forall (m :: * -> *) a b.
Monad m =>
m (Trace a) -> (a -> m (Trace b)) -> m (Trace b)
`bind` (((WeightedT (DensityT m) b, Trace b) -> Trace b)
-> m (WeightedT (DensityT m) b, Trace b) -> m (Trace b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (WeightedT (DensityT m) b, Trace b) -> Trace b
forall a b. (a, b) -> b
snd (m (WeightedT (DensityT m) b, Trace b) -> m (Trace b))
-> (a -> m (WeightedT (DensityT m) b, Trace b)) -> a -> m (Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TracedT m b -> m (WeightedT (DensityT m) b, Trace b)
forall (m :: * -> *) a.
TracedT m a -> m (WeightedT (DensityT m) a, Trace a)
runTraced (TracedT m b -> m (WeightedT (DensityT m) b, Trace b))
-> (a -> TracedT m b) -> a -> m (WeightedT (DensityT m) b, Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> TracedT m b
f)
    (WeightedT (DensityT m) b, Trace b)
-> m (WeightedT (DensityT m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WeightedT (DensityT m) b
m, Trace b
t)

instance MonadTrans TracedT where
  lift :: forall (m :: * -> *) a. Monad m => m a -> TracedT m a
lift m a
m = m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) a, Trace a) -> TracedT m a)
-> m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall a b. (a -> b) -> a -> b
$ (a -> (WeightedT (DensityT m) a, Trace a))
-> m a -> m (WeightedT (DensityT m) a, Trace a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,) (DensityT m a -> WeightedT (DensityT m) a
forall (m :: * -> *) a. Monad m => m a -> WeightedT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (DensityT m a -> WeightedT (DensityT m) a)
-> DensityT m a -> WeightedT (DensityT m) a
forall a b. (a -> b) -> a -> b
$ m a -> DensityT m a
forall (m :: * -> *) a. Monad m => m a -> DensityT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m) (Trace a -> (WeightedT (DensityT m) a, Trace a))
-> (a -> Trace a) -> a -> (WeightedT (DensityT m) a, Trace a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Trace a
forall a. a -> Trace a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) m a
m

instance (MonadDistribution m) => MonadDistribution (TracedT m) where
  random :: TracedT m Double
random = m (WeightedT (DensityT m) Double, Trace Double) -> TracedT m Double
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) Double, Trace Double)
 -> TracedT m Double)
-> m (WeightedT (DensityT m) Double, Trace Double)
-> TracedT m Double
forall a b. (a -> b) -> a -> b
$ (Double -> (WeightedT (DensityT m) Double, Trace Double))
-> m Double -> m (WeightedT (DensityT m) Double, Trace Double)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,) WeightedT (DensityT m) Double
forall (m :: * -> *). MonadDistribution m => m Double
random (Trace Double -> (WeightedT (DensityT m) Double, Trace Double))
-> (Double -> Trace Double)
-> Double
-> (WeightedT (DensityT m) Double, Trace Double)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Trace Double
singleton) m Double
forall (m :: * -> *). MonadDistribution m => m Double
random

instance (MonadFactor m) => MonadFactor (TracedT m) where
  score :: Log Double -> TracedT m ()
score Log Double
w = m (WeightedT (DensityT m) (), Trace ()) -> TracedT m ()
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) (), Trace ()) -> TracedT m ())
-> m (WeightedT (DensityT m) (), Trace ()) -> TracedT m ()
forall a b. (a -> b) -> a -> b
$ (Trace () -> (WeightedT (DensityT m) (), Trace ()))
-> m (Trace ()) -> m (WeightedT (DensityT m) (), Trace ())
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Log Double -> WeightedT (DensityT m) ()
forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score Log Double
w,) (Log Double -> m ()
forall (m :: * -> *). MonadFactor m => Log Double -> m ()
score Log Double
w m () -> m (Trace ()) -> m (Trace ())
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Trace () -> m (Trace ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Log Double -> Trace ()
scored Log Double
w))

instance (MonadMeasure m) => MonadMeasure (TracedT m)

hoist :: (forall x. m x -> m x) -> TracedT m a -> TracedT m a
hoist :: forall (m :: * -> *) a.
(forall x. m x -> m x) -> TracedT m a -> TracedT m a
hoist forall x. m x -> m x
f (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) a, Trace a)
-> m (WeightedT (DensityT m) a, Trace a)
forall x. m x -> m x
f m (WeightedT (DensityT m) a, Trace a)
c)

-- | Discard the trace and supporting infrastructure.
marginal :: (Monad m) => TracedT m a -> m a
marginal :: forall (m :: * -> *) a. Monad m => TracedT m a -> m a
marginal (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = ((WeightedT (DensityT m) a, Trace a) -> a)
-> m (WeightedT (DensityT m) a, Trace a) -> m a
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Trace a -> a
forall a. Trace a -> a
output (Trace a -> a)
-> ((WeightedT (DensityT m) a, Trace a) -> Trace a)
-> (WeightedT (DensityT m) a, Trace a)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WeightedT (DensityT m) a, Trace a) -> Trace a
forall a b. (a, b) -> b
snd) m (WeightedT (DensityT m) a, Trace a)
c

-- | Freeze all traced random choices to their current values and stop tracing
-- them.
freeze :: (Monad m) => TracedT m a -> TracedT m a
freeze :: forall (m :: * -> *) a. Monad m => TracedT m a -> TracedT m a
freeze (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) a, Trace a) -> TracedT m a)
-> m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall a b. (a -> b) -> a -> b
$ do
  (WeightedT (DensityT m) a
_, Trace a
t) <- m (WeightedT (DensityT m) a, Trace a)
c
  let x :: a
x = Trace a -> a
forall a. Trace a -> a
output Trace a
t
  (WeightedT (DensityT m) a, Trace a)
-> m (WeightedT (DensityT m) a, Trace a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> WeightedT (DensityT m) a
forall a. a -> WeightedT (DensityT m) a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x, a -> Trace a
forall a. a -> Trace a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)

-- | A single step of the Trace Metropolis-Hastings algorithm.
mhStep :: (MonadDistribution m) => TracedT m a -> TracedT m a
mhStep :: forall (m :: * -> *) a.
MonadDistribution m =>
TracedT m a -> TracedT m a
mhStep (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall (m :: * -> *) a.
m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
TracedT (m (WeightedT (DensityT m) a, Trace a) -> TracedT m a)
-> m (WeightedT (DensityT m) a, Trace a) -> TracedT m a
forall a b. (a -> b) -> a -> b
$ do
  (WeightedT (DensityT m) a
m, Trace a
t) <- m (WeightedT (DensityT m) a, Trace a)
c
  Trace a
t' <- WeightedT (DensityT m) a -> Trace a -> m (Trace a)
forall (m :: * -> *) a.
MonadDistribution m =>
WeightedT (DensityT m) a -> Trace a -> m (Trace a)
mhTransFree WeightedT (DensityT m) a
m Trace a
t
  (WeightedT (DensityT m) a, Trace a)
-> m (WeightedT (DensityT m) a, Trace a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WeightedT (DensityT m) a
m, Trace a
t')

-- | Full run of the Trace Metropolis-Hastings algorithm with a specified
-- number of steps.
mh :: (MonadDistribution m) => Int -> TracedT m a -> m [a]
mh :: forall (m :: * -> *) a.
MonadDistribution m =>
Int -> TracedT m a -> m [a]
mh Int
n (TracedT m (WeightedT (DensityT m) a, Trace a)
c) = do
  (WeightedT (DensityT m) a
m, Trace a
t) <- m (WeightedT (DensityT m) a, Trace a)
c
  let f :: t -> m (NonEmpty (Trace a))
f t
k
        | t
k t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = NonEmpty (Trace a) -> m (NonEmpty (Trace a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Trace a
t Trace a -> [Trace a] -> NonEmpty (Trace a)
forall a. a -> [a] -> NonEmpty a
:| [])
        | Bool
otherwise = do
            (Trace a
x :| [Trace a]
xs) <- t -> m (NonEmpty (Trace a))
f (t
k t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
            Trace a
y <- WeightedT (DensityT m) a -> Trace a -> m (Trace a)
forall (m :: * -> *) a.
MonadDistribution m =>
WeightedT (DensityT m) a -> Trace a -> m (Trace a)
mhTransFree WeightedT (DensityT m) a
m Trace a
x
            NonEmpty (Trace a) -> m (NonEmpty (Trace a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Trace a
y Trace a -> [Trace a] -> NonEmpty (Trace a)
forall a. a -> [a] -> NonEmpty a
:| Trace a
x Trace a -> [Trace a] -> [Trace a]
forall a. a -> [a] -> [a]
: [Trace a]
xs)
  (NonEmpty (Trace a) -> [a]) -> m (NonEmpty (Trace a)) -> m [a]
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Trace a -> a) -> [Trace a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Trace a -> a
forall a. Trace a -> a
output ([Trace a] -> [a])
-> (NonEmpty (Trace a) -> [Trace a]) -> NonEmpty (Trace a) -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Trace a) -> [Trace a]
forall a. NonEmpty a -> [a]
NE.toList) (Int -> m (NonEmpty (Trace a))
forall {t}. (Ord t, Num t) => t -> m (NonEmpty (Trace a))
f Int
n)