@@ -53,7 +53,9 @@ import Control.Monad.Bayes.Weighted
5353 weighted ,
5454 withWeight ,
5555 )
56- import Control.Monad.List (ListT (.. ), MonadIO , MonadTrans (.. ))
56+ import Control.Monad.IO.Class
57+ import Control.Monad.Trans
58+ import Control.Monad.Trans.Free.Ap
5759import Data.List (unfoldr )
5860import Data.List qualified
5961import Data.Maybe (catMaybes )
@@ -64,27 +66,27 @@ import Numeric.Log qualified as Log
6466import Prelude hiding (all , sum )
6567
6668-- | A collection of weighted samples, or particles.
67- newtype Population m a = Population (Weighted (ListT m ) a )
69+ newtype Population m a = Population (Weighted (FreeT [] m ) a )
6870 deriving newtype (Functor , Applicative , Monad , MonadIO , MonadDistribution , MonadFactor , MonadMeasure )
6971
7072instance MonadTrans Population where
7173 lift = Population . lift . lift
7274
7375-- | Explicit representation of the weighted sample with weights in the log
7476-- domain.
75- population , runPopulation :: Population m a -> m [(a , Log Double )]
76- population (Population m) = runListT $ weighted m
77+ population , runPopulation :: Monad m => Population m a -> m [(a , Log Double )]
78+ population (Population m) = iterT (( fmap concat . sequence )) $ fmap pure $ weighted m
7779
7880-- | deprecated synonym
7981runPopulation = population
8082
8183-- | Explicit representation of the weighted sample.
82- explicitPopulation :: Functor m => Population m a -> m [(a , Double )]
84+ explicitPopulation :: Monad m => Population m a -> m [(a , Double )]
8385explicitPopulation = fmap (map (second (exp . ln))) . population
8486
8587-- | Initialize 'Population' with a concrete weighted sample.
8688fromWeightedList :: Monad m => m [(a , Log Double )] -> Population m a
87- fromWeightedList = Population . withWeight . ListT
89+ fromWeightedList = Population . withWeight . FreeT . fmap ( Free . fmap pure )
8890
8991-- | Increase the sample size by a given factor.
9092-- The weights are adjusted such that their sum is preserved.
@@ -269,7 +271,7 @@ popAvg f p = do
269271
270272-- | Applies a transformation to the inner monad.
271273hoist ::
272- Monad n =>
274+ ( Monad m , Monad n ) =>
273275 (forall x . m x -> n x ) ->
274276 Population m a ->
275277 Population n a
0 commit comments