Opened 22 months ago
Closed 10 months ago
#9385 closed feature request (invalid)
Additions to Control.Monad
Reported by: | olf | Owned by: | ekmett |
---|---|---|---|
Priority: | low | Milestone: | |
Component: | Core Libraries | Version: | 7.8.2 |
Keywords: | report-impact | Cc: | hvr, ekmett, core-libraries-committee@… |
Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
Type of failure: | None/Unknown | Test Case: | |
Blocked By: | Blocking: | ||
Related Tickets: | Differential Rev(s): | ||
Wiki Page: |
Description
I'd like to propose the following additions/changes to Control.Monad: mfilter can be generalised:
gen_mfilter :: Monad m => (a -> m ()) -> m a -> m a gen_mfilter f ma = (\a -> liftM (const a) (f a)) =<< ma
Now we obtain the old mfilter as
gen_mfilter.(guard.)
Further, m () is a monoid for every monad, which would cause conflicts for [()], to name one example. (The usual monoid instance of [()] is addition of natural numbers, while the monadic monoid instance is multiplication.) More generally, the monoid m () acts on every type m a in the following way:
mtimes :: Monad m => m () -> m a -> m a mtimes = gen_mfilter.const = liftM2 (flip const) when = mtimes.guard
For example, each element of a list can be duplicated like this:
mtimes [(),()]
To see why these functions are useful, consider the DDist monad of the ProbabilityMonads package: Since DDist () is essentially the monoid of real numbers with multiplication, gen_mfilter f updates a distribution by multiplying the weight of x by f x. Another example is the state monad ST s, where type (a -> ST s ()) is essentially a -> s -> s, so these functions encode changes that, when used with gen_mfilter alter state, not the value.
Change History (5)
comment:1 Changed 22 months ago by ekmett
comment:2 Changed 20 months ago by thoughtpolice
- Component changed from libraries/base to Core Libraries
- Owner set to ekmett
Moving over to new owning component 'Core Libraries'.
comment:3 Changed 19 months ago by thomie
- Cc core-libraries-committee@… added
- Status changed from new to infoneeded
olf: did you submit any proposal to the libraries mailinglist for this?
See also: https://www.haskell.org/haskellwiki/Library_submissions.
comment:4 Changed 16 months ago by hvr
- Keywords report-impact added
comment:5 Changed 10 months ago by thomie
- Resolution set to invalid
- Status changed from infoneeded to closed
Please reopen with a mandate from the libraries@ list.
This sort of proposal should go through the libraries@… mailing list rather than the GHC Trac.
Please feel free to submit this for discussion there to invite wider discussion.