-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
44 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-- | 'PopulationT' turns a single sample into a collection of weighted samples. | ||
-- | ||
-- This module contains an _'Applicative'_ transformer corresponding to the Population monad transformer from the article. | ||
-- It is based on the old-fashioned 'ListT', which is not a valid monad transformer, but a valid applicative transformer. | ||
-- The corresponding monad transformer is contained in 'Control.Monad.Bayes.Population'. | ||
-- One can convert from the monad transformer to the applicative transformer by 'flatten'ing. | ||
module Control.Monad.Bayes.Population.Applicative where | ||
|
||
import Control.Applicative | ||
import Control.Applicative.List | ||
import Control.Monad.Trans.Writer.Strict | ||
import Data.Functor.Compose | ||
import Numeric.Log (Log) | ||
|
||
-- * Applicative Population transformer | ||
|
||
-- WriterT has to be used instead of WeightedT, | ||
-- since WeightedT uses StateT under the hood, | ||
-- which requires a Monad (ListT m) constraint. | ||
|
||
-- | A collection of weighted samples, or particles. | ||
newtype PopulationT m a = PopulationT {getPopulationT :: WriterT (Log Double) (ListT m) a} | ||
deriving newtype (Functor, Applicative, Alternative) | ||
|
||
runPopulationT :: PopulationT m a -> m [(a, Log Double)] | ||
runPopulationT = runListT . runWriterT . getPopulationT | ||
|
||
fromWeightedList :: m [(a, Log Double)] -> PopulationT m a | ||
fromWeightedList = PopulationT . WriterT . listT |