{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- |
-- Slower than Control.Monad.Bayes.Density.Free, so not used by default,
-- but more elementary to understand. Just uses standard
-- monad transformer techniques.
module Control.Monad.Bayes.Density.State where

import Control.Monad.Bayes.Class (MonadDistribution (random))
import Control.Monad.State (MonadState (get, put), StateT, evalStateT)
import Control.Monad.Writer

newtype Density m a = Density {forall (m :: * -> *) a.
Density m a -> WriterT [Double] (StateT [Double] m) a
runDensity :: WriterT [Double] (StateT [Double] m) a} deriving newtype ((forall a b. (a -> b) -> Density m a -> Density m b)
-> (forall a b. a -> Density m b -> Density m a)
-> Functor (Density m)
forall a b. a -> Density m b -> Density m a
forall a b. (a -> b) -> Density m a -> Density m b
forall (m :: * -> *) a b.
Functor m =>
a -> Density m b -> Density m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> Density m a -> Density 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) -> Density m a -> Density m b
fmap :: forall a b. (a -> b) -> Density m a -> Density m b
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> Density m b -> Density m a
<$ :: forall a b. a -> Density m b -> Density m a
Functor, Functor (Density m)
Functor (Density m)
-> (forall a. a -> Density m a)
-> (forall a b. Density m (a -> b) -> Density m a -> Density m b)
-> (forall a b c.
    (a -> b -> c) -> Density m a -> Density m b -> Density m c)
-> (forall a b. Density m a -> Density m b -> Density m b)
-> (forall a b. Density m a -> Density m b -> Density m a)
-> Applicative (Density m)
forall a. a -> Density m a
forall a b. Density m a -> Density m b -> Density m a
forall a b. Density m a -> Density m b -> Density m b
forall a b. Density m (a -> b) -> Density m a -> Density m b
forall a b c.
(a -> b -> c) -> Density m a -> Density m b -> Density m c
forall {m :: * -> *}. Monad m => Functor (Density m)
forall (m :: * -> *) a. Monad m => a -> Density m a
forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m a
forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m b
forall (m :: * -> *) a b.
Monad m =>
Density m (a -> b) -> Density m a -> Density m b
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Density m a -> Density m b -> Density 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 -> Density m a
pure :: forall a. a -> Density m a
$c<*> :: forall (m :: * -> *) a b.
Monad m =>
Density m (a -> b) -> Density m a -> Density m b
<*> :: forall a b. Density m (a -> b) -> Density m a -> Density m b
$cliftA2 :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Density m a -> Density m b -> Density m c
liftA2 :: forall a b c.
(a -> b -> c) -> Density m a -> Density m b -> Density m c
$c*> :: forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m b
*> :: forall a b. Density m a -> Density m b -> Density m b
$c<* :: forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m a
<* :: forall a b. Density m a -> Density m b -> Density m a
Applicative, Applicative (Density m)
Applicative (Density m)
-> (forall a b. Density m a -> (a -> Density m b) -> Density m b)
-> (forall a b. Density m a -> Density m b -> Density m b)
-> (forall a. a -> Density m a)
-> Monad (Density m)
forall a. a -> Density m a
forall a b. Density m a -> Density m b -> Density m b
forall a b. Density m a -> (a -> Density m b) -> Density m b
forall (m :: * -> *). Monad m => Applicative (Density m)
forall (m :: * -> *) a. Monad m => a -> Density m a
forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m b
forall (m :: * -> *) a b.
Monad m =>
Density m a -> (a -> Density m b) -> Density 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 =>
Density m a -> (a -> Density m b) -> Density m b
>>= :: forall a b. Density m a -> (a -> Density m b) -> Density m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
Density m a -> Density m b -> Density m b
>> :: forall a b. Density m a -> Density m b -> Density m b
$creturn :: forall (m :: * -> *) a. Monad m => a -> Density m a
return :: forall a. a -> Density m a
Monad)

instance MonadTrans Density where
  lift :: forall (m :: * -> *) a. Monad m => m a -> Density m a
lift = WriterT [Double] (StateT [Double] m) a -> Density m a
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) a -> Density m a)
-> (m a -> WriterT [Double] (StateT [Double] m) a)
-> m a
-> Density m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT [Double] m a -> WriterT [Double] (StateT [Double] m) a
forall (m :: * -> *) a. Monad m => m a -> WriterT [Double] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT [Double] m a -> WriterT [Double] (StateT [Double] m) a)
-> (m a -> StateT [Double] m a)
-> m a
-> WriterT [Double] (StateT [Double] m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> StateT [Double] m a
forall (m :: * -> *) a. Monad m => m a -> StateT [Double] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

instance Monad m => MonadState [Double] (Density m) where
  get :: Density m [Double]
get = WriterT [Double] (StateT [Double] m) [Double] -> Density m [Double]
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) [Double]
 -> Density m [Double])
-> WriterT [Double] (StateT [Double] m) [Double]
-> Density m [Double]
forall a b. (a -> b) -> a -> b
$ StateT [Double] m [Double]
-> WriterT [Double] (StateT [Double] m) [Double]
forall (m :: * -> *) a. Monad m => m a -> WriterT [Double] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT [Double] m [Double]
 -> WriterT [Double] (StateT [Double] m) [Double])
-> StateT [Double] m [Double]
-> WriterT [Double] (StateT [Double] m) [Double]
forall a b. (a -> b) -> a -> b
$ StateT [Double] m [Double]
forall s (m :: * -> *). MonadState s m => m s
get
  put :: [Double] -> Density m ()
put = WriterT [Double] (StateT [Double] m) () -> Density m ()
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) () -> Density m ())
-> ([Double] -> WriterT [Double] (StateT [Double] m) ())
-> [Double]
-> Density m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT [Double] m () -> WriterT [Double] (StateT [Double] m) ()
forall (m :: * -> *) a. Monad m => m a -> WriterT [Double] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT [Double] m () -> WriterT [Double] (StateT [Double] m) ())
-> ([Double] -> StateT [Double] m ())
-> [Double]
-> WriterT [Double] (StateT [Double] m) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Double] -> StateT [Double] m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put

instance Monad m => MonadWriter [Double] (Density m) where
  tell :: [Double] -> Density m ()
tell = WriterT [Double] (StateT [Double] m) () -> Density m ()
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) () -> Density m ())
-> ([Double] -> WriterT [Double] (StateT [Double] m) ())
-> [Double]
-> Density m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Double] -> WriterT [Double] (StateT [Double] m) ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
  listen :: forall a. Density m a -> Density m (a, [Double])
listen = WriterT [Double] (StateT [Double] m) (a, [Double])
-> Density m (a, [Double])
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) (a, [Double])
 -> Density m (a, [Double]))
-> (Density m a
    -> WriterT [Double] (StateT [Double] m) (a, [Double]))
-> Density m a
-> Density m (a, [Double])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT [Double] (StateT [Double] m) a
-> WriterT [Double] (StateT [Double] m) (a, [Double])
forall a.
WriterT [Double] (StateT [Double] m) a
-> WriterT [Double] (StateT [Double] m) (a, [Double])
forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen (WriterT [Double] (StateT [Double] m) a
 -> WriterT [Double] (StateT [Double] m) (a, [Double]))
-> (Density m a -> WriterT [Double] (StateT [Double] m) a)
-> Density m a
-> WriterT [Double] (StateT [Double] m) (a, [Double])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Density m a -> WriterT [Double] (StateT [Double] m) a
forall (m :: * -> *) a.
Density m a -> WriterT [Double] (StateT [Double] m) a
runDensity
  pass :: forall a. Density m (a, [Double] -> [Double]) -> Density m a
pass = WriterT [Double] (StateT [Double] m) a -> Density m a
forall (m :: * -> *) a.
WriterT [Double] (StateT [Double] m) a -> Density m a
Density (WriterT [Double] (StateT [Double] m) a -> Density m a)
-> (Density m (a, [Double] -> [Double])
    -> WriterT [Double] (StateT [Double] m) a)
-> Density m (a, [Double] -> [Double])
-> Density m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT [Double] (StateT [Double] m) (a, [Double] -> [Double])
-> WriterT [Double] (StateT [Double] m) a
forall a.
WriterT [Double] (StateT [Double] m) (a, [Double] -> [Double])
-> WriterT [Double] (StateT [Double] m) a
forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass (WriterT [Double] (StateT [Double] m) (a, [Double] -> [Double])
 -> WriterT [Double] (StateT [Double] m) a)
-> (Density m (a, [Double] -> [Double])
    -> WriterT [Double] (StateT [Double] m) (a, [Double] -> [Double]))
-> Density m (a, [Double] -> [Double])
-> WriterT [Double] (StateT [Double] m) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Density m (a, [Double] -> [Double])
-> WriterT [Double] (StateT [Double] m) (a, [Double] -> [Double])
forall (m :: * -> *) a.
Density m a -> WriterT [Double] (StateT [Double] m) a
runDensity

instance MonadDistribution m => MonadDistribution (Density m) where
  random :: Density m Double
random = do
    [Double]
trace <- Density m [Double]
forall s (m :: * -> *). MonadState s m => m s
get
    Double
x <- case [Double]
trace of
      [] -> Density m Double
forall (m :: * -> *). MonadDistribution m => m Double
random
      Double
r : [Double]
xs -> [Double] -> Density m ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put [Double]
xs Density m () -> Density m Double -> Density m Double
forall a b. Density m a -> Density m b -> Density m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Double -> Density m Double
forall a. a -> Density m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Double
r
    [Double] -> Density m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Double
x]
    Double -> Density m Double
forall a. a -> Density m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Double
x

density :: Monad m => Density m b -> [Double] -> m (b, [Double])
density :: forall (m :: * -> *) b.
Monad m =>
Density m b -> [Double] -> m (b, [Double])
density (Density WriterT [Double] (StateT [Double] m) b
m) = StateT [Double] m (b, [Double]) -> [Double] -> m (b, [Double])
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (WriterT [Double] (StateT [Double] m) b
-> StateT [Double] m (b, [Double])
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT WriterT [Double] (StateT [Double] m) b
m)