-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add newtype to lift classes to monad transformers #272
Open
turion
wants to merge
3
commits into
tweag:master
Choose a base branch
from
turion:dev_trans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -71,6 +71,7 @@ module Control.Monad.Bayes.Class | |
Measure, | ||
Kernel, | ||
Log (ln, Exp), | ||
MonadMeasureTrans (..), | ||
) | ||
where | ||
|
||
|
@@ -82,9 +83,11 @@ import Control.Monad.Identity (IdentityT) | |
import Control.Monad.List (ListT) | ||
import Control.Monad.Reader (ReaderT) | ||
import Control.Monad.State (StateT) | ||
import Control.Monad.Trans (MonadTrans) | ||
import Control.Monad.Writer (WriterT) | ||
import Data.Histogram qualified as H | ||
import Data.Histogram.Fill qualified as H | ||
import Data.Kind (Type) | ||
import Data.Matrix | ||
( Matrix, | ||
cholDecomp, | ||
|
@@ -342,68 +345,82 @@ histogramToList = H.asList | |
---------------------------------------------------------------------------- | ||
-- Instances that lift probabilistic effects to standard tranformers. | ||
|
||
instance MonadDistribution m => MonadDistribution (IdentityT m) where | ||
random = lift random | ||
bernoulli = lift . bernoulli | ||
deriving via (MonadMeasureTrans IdentityT m) instance MonadDistribution m => MonadDistribution (IdentityT m) | ||
|
||
instance MonadFactor m => MonadFactor (IdentityT m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans IdentityT m) instance MonadFactor m => MonadFactor (IdentityT m) | ||
|
||
instance MonadMeasure m => MonadMeasure (IdentityT m) | ||
|
||
instance MonadDistribution m => MonadDistribution (ExceptT e m) where | ||
random = lift random | ||
uniformD = lift . uniformD | ||
deriving via (MonadMeasureTrans (ExceptT e) m) instance MonadDistribution m => MonadDistribution (ExceptT e m) | ||
|
||
instance MonadFactor m => MonadFactor (ExceptT e m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans (ExceptT e) m) instance MonadFactor m => MonadFactor (ExceptT e m) | ||
|
||
instance MonadMeasure m => MonadMeasure (ExceptT e m) | ||
|
||
instance MonadDistribution m => MonadDistribution (ReaderT r m) where | ||
random = lift random | ||
bernoulli = lift . bernoulli | ||
deriving via (MonadMeasureTrans (ReaderT r) m) instance MonadDistribution m => MonadDistribution (ReaderT r m) | ||
|
||
instance MonadFactor m => MonadFactor (ReaderT r m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans (ReaderT r) m) instance MonadFactor m => MonadFactor (ReaderT r m) | ||
|
||
instance MonadMeasure m => MonadMeasure (ReaderT r m) | ||
|
||
instance (Monoid w, MonadDistribution m) => MonadDistribution (WriterT w m) where | ||
random = lift random | ||
bernoulli = lift . bernoulli | ||
categorical = lift . categorical | ||
deriving via (MonadMeasureTrans (WriterT w) m) instance (Monoid w, MonadDistribution m) => MonadDistribution (WriterT w m) | ||
|
||
instance (Monoid w, MonadFactor m) => MonadFactor (WriterT w m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans (WriterT w) m) instance (Monoid w, MonadFactor m) => MonadFactor (WriterT w m) | ||
|
||
instance (Monoid w, MonadMeasure m) => MonadMeasure (WriterT w m) | ||
|
||
instance MonadDistribution m => MonadDistribution (StateT s m) where | ||
random = lift random | ||
bernoulli = lift . bernoulli | ||
categorical = lift . categorical | ||
uniformD = lift . uniformD | ||
deriving via (MonadMeasureTrans (StateT s) m) instance MonadDistribution m => MonadDistribution (StateT s m) | ||
|
||
instance MonadFactor m => MonadFactor (StateT s m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans (StateT s) m) instance MonadFactor m => MonadFactor (StateT s m) | ||
|
||
instance MonadMeasure m => MonadMeasure (StateT s m) | ||
|
||
instance MonadDistribution m => MonadDistribution (ListT m) where | ||
random = lift random | ||
bernoulli = lift . bernoulli | ||
categorical = lift . categorical | ||
deriving via (MonadMeasureTrans ListT m) instance MonadDistribution m => MonadDistribution (ListT m) | ||
|
||
instance MonadFactor m => MonadFactor (ListT m) where | ||
score = lift . score | ||
deriving via (MonadMeasureTrans ListT m) instance MonadFactor m => MonadFactor (ListT m) | ||
|
||
instance MonadMeasure m => MonadMeasure (ListT m) | ||
|
||
instance MonadDistribution m => MonadDistribution (ContT r m) where | ||
deriving via (MonadMeasureTrans (ContT r) m) instance MonadDistribution m => MonadDistribution (ContT r m) | ||
|
||
deriving via (MonadMeasureTrans (ContT r) m) instance MonadFactor m => MonadFactor (ContT r m) | ||
|
||
instance MonadMeasure m => MonadMeasure (ContT r m) | ||
|
||
-- * Utility for deriving MonadDistribution, MonadFactor and MonadMeasure | ||
|
||
-- | Newtype to derive 'MonadDistribution', 'MonadFactor' and 'MonadMeasure' automatically for monad transformers. | ||
-- | ||
-- The typical usage is with the `StandaloneDeriving` and `DerivingVia` extensions. | ||
-- For example, to derive all instances for the 'IdentityT' transformer, one writes: | ||
-- | ||
-- @ | ||
-- deriving via (MonadMeasureTrans IdentityT m) instance MonadDistribution m => MonadDistribution (IdentityT m) | ||
-- deriving via (MonadMeasureTrans IdentityT m) instance MonadFactor m => MonadFactor (IdentityT m) | ||
-- instance MonadMeasure m => MonadMeasure (IdentityT m) | ||
-- @ | ||
-- (The final 'MonadMeasure' could also be derived `via`, but this isn't necessary because it doesn't contain any methods.) | ||
newtype MonadMeasureTrans (t :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) a = MonadMeasureTrans {getMonadMeasureTrans :: t m a} | ||
deriving (Functor, Applicative, Monad) | ||
|
||
instance MonadTrans t => MonadTrans (MonadMeasureTrans t) where | ||
lift = MonadMeasureTrans . lift | ||
|
||
instance (MonadTrans t, MonadDistribution m, Monad (t m)) => MonadDistribution (MonadMeasureTrans t m) where | ||
random = lift random | ||
uniform = (lift .) . uniform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly here, there may have been a reason that many of the instances were limited to mostly just |
||
normal = (lift .) . normal | ||
gamma = (lift .) . gamma | ||
beta = (lift .) . beta | ||
bernoulli = lift . bernoulli | ||
categorical = lift . categorical | ||
logCategorical = lift . logCategorical | ||
uniformD = lift . uniformD | ||
geometric = lift . geometric | ||
poisson = lift . poisson | ||
dirichlet = lift . dirichlet | ||
|
||
instance MonadFactor m => MonadFactor (ContT r m) where | ||
instance (MonadFactor m, MonadTrans t, Monad (t m)) => MonadFactor (MonadMeasureTrans t m) where | ||
score = lift . score | ||
|
||
instance MonadMeasure m => MonadMeasure (ContT r m) | ||
instance (MonadDistribution m, MonadFactor m, MonadTrans t, Monad (t m)) => MonadMeasure (MonadMeasureTrans t m) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only note here would be that some of the previous lifting instances appear to have varied slightly. This actually may have been my doing, because I recall that working in a transformed Enumerator didn't lift
bernoulli
, so that discrete distributions got calculated viarandom
and thus failed withenumerate
.