{-# 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
  ( Traced,
    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 (Density)
import Control.Monad.Bayes.Traced.Common
  ( Trace (..),
    bind,
    mhTransFree,
    scored,
    singleton,
  )
import Control.Monad.Bayes.Weighted (Weighted)
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 Traced m a = Traced {forall (m :: * -> *) a.
Traced m a -> m (Weighted (Density m) a, Trace a)
runTraced :: m (Weighted (Density m) a, Trace a)}

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

instance Monad m => Functor (Traced m) where
  fmap :: forall a b. (a -> b) -> Traced m a -> Traced m b
fmap a -> b
f (Traced m (Weighted (Density m) a, Trace a)
c) = m (Weighted (Density m) b, Trace b) -> Traced m b
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) b, Trace b) -> Traced m b)
-> m (Weighted (Density m) b, Trace b) -> Traced m b
forall a b. (a -> b) -> a -> b
$ do
    (Weighted (Density m) a
m, Trace a
t) <- m (Weighted (Density m) a, Trace a)
c
    let m' :: Weighted (Density m) b
m' = (a -> b) -> Weighted (Density m) a -> Weighted (Density m) b
forall a b.
(a -> b) -> Weighted (Density m) a -> Weighted (Density m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Weighted (Density 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
    (Weighted (Density m) b, Trace b)
-> m (Weighted (Density m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Weighted (Density m) b
m', Trace b
t')

instance Monad m => Applicative (Traced m) where
  pure :: forall a. a -> Traced m a
pure a
x = m (Weighted (Density m) a, Trace a) -> Traced m a
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) a, Trace a) -> Traced m a)
-> m (Weighted (Density m) a, Trace a) -> Traced m a
forall a b. (a -> b) -> a -> b
$ (Weighted (Density m) a, Trace a)
-> m (Weighted (Density m) a, Trace a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Weighted (Density m) a
forall a. a -> Weighted (Density 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)
  (Traced m (Weighted (Density m) (a -> b), Trace (a -> b))
cf) <*> :: forall a b. Traced m (a -> b) -> Traced m a -> Traced m b
<*> (Traced m (Weighted (Density m) a, Trace a)
cx) = m (Weighted (Density m) b, Trace b) -> Traced m b
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) b, Trace b) -> Traced m b)
-> m (Weighted (Density m) b, Trace b) -> Traced m b
forall a b. (a -> b) -> a -> b
$ do
    (Weighted (Density m) (a -> b)
mf, Trace (a -> b)
tf) <- m (Weighted (Density m) (a -> b), Trace (a -> b))
cf
    (Weighted (Density m) a
mx, Trace a
tx) <- m (Weighted (Density m) a, Trace a)
cx
    (Weighted (Density m) b, Trace b)
-> m (Weighted (Density m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Weighted (Density m) (a -> b)
mf Weighted (Density m) (a -> b)
-> Weighted (Density m) a -> Weighted (Density m) b
forall a b.
Weighted (Density m) (a -> b)
-> Weighted (Density m) a -> Weighted (Density m) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Weighted (Density 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 (Traced m) where
  (Traced m (Weighted (Density m) a, Trace a)
cx) >>= :: forall a b. Traced m a -> (a -> Traced m b) -> Traced m b
>>= a -> Traced m b
f = m (Weighted (Density m) b, Trace b) -> Traced m b
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) b, Trace b) -> Traced m b)
-> m (Weighted (Density m) b, Trace b) -> Traced m b
forall a b. (a -> b) -> a -> b
$ do
    (Weighted (Density m) a
mx, Trace a
tx) <- m (Weighted (Density m) a, Trace a)
cx
    let m :: Weighted (Density m) b
m = Weighted (Density m) a
mx Weighted (Density m) a
-> (a -> Weighted (Density m) b) -> Weighted (Density m) b
forall a b.
Weighted (Density m) a
-> (a -> Weighted (Density m) b) -> Weighted (Density m) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m (Weighted (Density m) b) -> Weighted (Density m) b
forall (m :: * -> *) a.
Monad m =>
m (Weighted (Density m) a) -> Weighted (Density m) a
pushM (m (Weighted (Density m) b) -> Weighted (Density m) b)
-> (a -> m (Weighted (Density m) b)) -> a -> Weighted (Density m) b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Weighted (Density m) b, Trace b) -> Weighted (Density m) b)
-> m (Weighted (Density m) b, Trace b)
-> m (Weighted (Density m) b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Weighted (Density m) b, Trace b) -> Weighted (Density m) b
forall a b. (a, b) -> a
fst (m (Weighted (Density m) b, Trace b) -> m (Weighted (Density m) b))
-> (a -> m (Weighted (Density m) b, Trace b))
-> a
-> m (Weighted (Density m) b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Traced m b -> m (Weighted (Density m) b, Trace b)
forall (m :: * -> *) a.
Traced m a -> m (Weighted (Density m) a, Trace a)
runTraced (Traced m b -> m (Weighted (Density m) b, Trace b))
-> (a -> Traced m b) -> a -> m (Weighted (Density m) b, Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Traced 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` (((Weighted (Density m) b, Trace b) -> Trace b)
-> m (Weighted (Density 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 (Weighted (Density m) b, Trace b) -> Trace b
forall a b. (a, b) -> b
snd (m (Weighted (Density m) b, Trace b) -> m (Trace b))
-> (a -> m (Weighted (Density m) b, Trace b)) -> a -> m (Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Traced m b -> m (Weighted (Density m) b, Trace b)
forall (m :: * -> *) a.
Traced m a -> m (Weighted (Density m) a, Trace a)
runTraced (Traced m b -> m (Weighted (Density m) b, Trace b))
-> (a -> Traced m b) -> a -> m (Weighted (Density m) b, Trace b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Traced m b
f)
    (Weighted (Density m) b, Trace b)
-> m (Weighted (Density m) b, Trace b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Weighted (Density m) b
m, Trace b
t)

instance MonadTrans Traced where
  lift :: forall (m :: * -> *) a. Monad m => m a -> Traced m a
lift m a
m = m (Weighted (Density m) a, Trace a) -> Traced m a
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) a, Trace a) -> Traced m a)
-> m (Weighted (Density m) a, Trace a) -> Traced m a
forall a b. (a -> b) -> a -> b
$ (a -> (Weighted (Density m) a, Trace a))
-> m a -> m (Weighted (Density 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 ((,) (Density m a -> Weighted (Density m) a
forall (m :: * -> *) a. Monad m => m a -> Weighted m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Density m a -> Weighted (Density m) a)
-> Density m a -> Weighted (Density m) a
forall a b. (a -> b) -> a -> b
$ m a -> Density m a
forall (m :: * -> *) a. Monad m => m a -> Density m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m a
m) (Trace a -> (Weighted (Density m) a, Trace a))
-> (a -> Trace a) -> a -> (Weighted (Density 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 (Traced m) where
  random :: Traced m Double
random = m (Weighted (Density m) Double, Trace Double) -> Traced m Double
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) Double, Trace Double) -> Traced m Double)
-> m (Weighted (Density m) Double, Trace Double) -> Traced m Double
forall a b. (a -> b) -> a -> b
$ (Double -> (Weighted (Density m) Double, Trace Double))
-> m Double -> m (Weighted (Density 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 ((,) Weighted (Density m) Double
forall (m :: * -> *). MonadDistribution m => m Double
random (Trace Double -> (Weighted (Density m) Double, Trace Double))
-> (Double -> Trace Double)
-> Double
-> (Weighted (Density 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 (Traced m) where
  score :: Log Double -> Traced m ()
score Log Double
w = m (Weighted (Density m) (), Trace ()) -> Traced m ()
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) (), Trace ()) -> Traced m ())
-> m (Weighted (Density m) (), Trace ()) -> Traced m ()
forall a b. (a -> b) -> a -> b
$ (Trace () -> (Weighted (Density m) (), Trace ()))
-> m (Trace ()) -> m (Weighted (Density 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 -> Weighted (Density 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 (Traced m)

hoist :: (forall x. m x -> m x) -> Traced m a -> Traced m a
hoist :: forall (m :: * -> *) a.
(forall x. m x -> m x) -> Traced m a -> Traced m a
hoist forall x. m x -> m x
f (Traced m (Weighted (Density m) a, Trace a)
c) = m (Weighted (Density m) a, Trace a) -> Traced m a
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) a, Trace a)
-> m (Weighted (Density m) a, Trace a)
forall x. m x -> m x
f m (Weighted (Density m) a, Trace a)
c)

-- | Discard the trace and supporting infrastructure.
marginal :: Monad m => Traced m a -> m a
marginal :: forall (m :: * -> *) a. Monad m => Traced m a -> m a
marginal (Traced m (Weighted (Density m) a, Trace a)
c) = ((Weighted (Density m) a, Trace a) -> a)
-> m (Weighted (Density 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)
-> ((Weighted (Density m) a, Trace a) -> Trace a)
-> (Weighted (Density m) a, Trace a)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Weighted (Density m) a, Trace a) -> Trace a
forall a b. (a, b) -> b
snd) m (Weighted (Density m) a, Trace a)
c

-- | Freeze all traced random choices to their current values and stop tracing
-- them.
freeze :: Monad m => Traced m a -> Traced m a
freeze :: forall (m :: * -> *) a. Monad m => Traced m a -> Traced m a
freeze (Traced m (Weighted (Density m) a, Trace a)
c) = m (Weighted (Density m) a, Trace a) -> Traced m a
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) a, Trace a) -> Traced m a)
-> m (Weighted (Density m) a, Trace a) -> Traced m a
forall a b. (a -> b) -> a -> b
$ do
  (Weighted (Density m) a
_, Trace a
t) <- m (Weighted (Density m) a, Trace a)
c
  let x :: a
x = Trace a -> a
forall a. Trace a -> a
output Trace a
t
  (Weighted (Density m) a, Trace a)
-> m (Weighted (Density m) a, Trace a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Weighted (Density m) a
forall a. a -> Weighted (Density 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 => Traced m a -> Traced m a
mhStep :: forall (m :: * -> *) a.
MonadDistribution m =>
Traced m a -> Traced m a
mhStep (Traced m (Weighted (Density m) a, Trace a)
c) = m (Weighted (Density m) a, Trace a) -> Traced m a
forall (m :: * -> *) a.
m (Weighted (Density m) a, Trace a) -> Traced m a
Traced (m (Weighted (Density m) a, Trace a) -> Traced m a)
-> m (Weighted (Density m) a, Trace a) -> Traced m a
forall a b. (a -> b) -> a -> b
$ do
  (Weighted (Density m) a
m, Trace a
t) <- m (Weighted (Density m) a, Trace a)
c
  Trace a
t' <- Weighted (Density m) a -> Trace a -> m (Trace a)
forall (m :: * -> *) a.
MonadDistribution m =>
Weighted (Density m) a -> Trace a -> m (Trace a)
mhTransFree Weighted (Density m) a
m Trace a
t
  (Weighted (Density m) a, Trace a)
-> m (Weighted (Density m) a, Trace a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Weighted (Density m) a
m, Trace a
t')

-- | Full run of the Trace Metropolis-Hastings algorithm with a specified
-- number of steps.
mh :: MonadDistribution m => Int -> Traced m a -> m [a]
mh :: forall (m :: * -> *) a.
MonadDistribution m =>
Int -> Traced m a -> m [a]
mh Int
n (Traced m (Weighted (Density m) a, Trace a)
c) = do
  (Weighted (Density m) a
m, Trace a
t) <- m (Weighted (Density 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 <- Weighted (Density m) a -> Trace a -> m (Trace a)
forall (m :: * -> *) a.
MonadDistribution m =>
Weighted (Density m) a -> Trace a -> m (Trace a)
mhTransFree Weighted (Density 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)