-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use applicative population transformer
- Loading branch information
Showing
4 changed files
with
46 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,40 @@ | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
module Control.Applicative.List where | ||
|
||
-- base | ||
|
||
import Control.Applicative | ||
import Data.Functor.Compose | ||
|
||
-- transformers | ||
import Control.Monad.Trans.Writer.Strict | ||
|
||
-- log-domain | ||
import Numeric.Log (Log) | ||
|
||
-- * Applicative ListT | ||
|
||
-- | _Applicative_ transformer adding a list/nondeterminism/choice effect. | ||
-- It is not a valid monad transformer, but it is a valid 'Applicative'. | ||
newtype ListT m a = ListT {getListT :: Compose [] m a} | ||
newtype ListT m a = ListT {getListT :: Compose m [] a} | ||
deriving newtype (Functor, Applicative, Alternative) | ||
|
||
lift :: m a -> ListT m a | ||
lift = ListT . Compose . pure | ||
lift :: (Functor m) => m a -> ListT m a | ||
lift = ListT . Compose . fmap pure | ||
|
||
runListT :: ListT m a -> [m a] | ||
runListT :: ListT m a -> m [a] | ||
runListT = getCompose . getListT | ||
|
||
-- * 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. | ||
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 . Compose |
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