Skip to content

Commit f634a51

Browse files
author
Manuel Bärenz
committed
Free list transformer, without ApplicativeDo yet
1 parent 110e040 commit f634a51

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/Control/Monad/Bayes/Class.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ import Control.Monad (replicateM, when)
7979
import Control.Monad.Cont (ContT)
8080
import Control.Monad.Except (ExceptT, lift)
8181
import Control.Monad.Identity (IdentityT)
82-
import Control.Monad.List (ListT)
8382
import Control.Monad.Reader (ReaderT)
8483
import Control.Monad.State (StateT)
84+
import Control.Monad.Trans.Free.Ap (FreeT)
8585
import Control.Monad.Writer (WriterT)
8686
import Data.Histogram qualified as H
8787
import Data.Histogram.Fill qualified as H
@@ -390,15 +390,15 @@ instance MonadFactor m => MonadFactor (StateT s m) where
390390

391391
instance MonadMeasure m => MonadMeasure (StateT s m)
392392

393-
instance MonadDistribution m => MonadDistribution (ListT m) where
393+
instance (Applicative f, MonadDistribution m) => MonadDistribution (FreeT f m) where
394394
random = lift random
395395
bernoulli = lift . bernoulli
396396
categorical = lift . categorical
397397

398-
instance MonadFactor m => MonadFactor (ListT m) where
398+
instance (Applicative f, MonadFactor m) => MonadFactor (FreeT f m) where
399399
score = lift . score
400400

401-
instance MonadMeasure m => MonadMeasure (ListT m)
401+
instance (Applicative f, MonadMeasure m) => MonadMeasure (FreeT f m)
402402

403403
instance MonadDistribution m => MonadDistribution (ContT r m) where
404404
random = lift random

src/Control/Monad/Bayes/Population.hs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
5759
import Data.List (unfoldr)
5860
import Data.List qualified
5961
import Data.Maybe (catMaybes)
@@ -64,27 +66,27 @@ import Numeric.Log qualified as Log
6466
import 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

7072
instance 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
7981
runPopulation = 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)]
8385
explicitPopulation = fmap (map (second (exp . ln))) . population
8486

8587
-- | Initialize 'Population' with a concrete weighted sample.
8688
fromWeightedList :: 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.
271273
hoist ::
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

Comments
 (0)