Proposal: add ifM and whenM to Control.Monad

Casey McCann cam at uptoisomorphism.net
Mon Apr 21 16:41:42 UTC 2014


On Mon, Apr 21, 2014 at 12:01 PM, Dan Doel <dan.doel at gmail.com> wrote:
> Yes, I think the stated naming conventions are somewhat off on what is
> really in the module.
>
> mfoo is generalizing foo to monads-with-extra-structure, in Control.Monad at
> least.
>
> But fooM is generally about functions that 'sequence' multiple actions. For
> instance:
>
>     mapM f = sequence . map f
>
> This also explains replicateM, as:
>
>     replicateM n = sequence . replicate n
>
> And liftM through liftM5 are similar.
>
> ifM, whenM, unlessM, etc. do not involve monads with extra structure, but
> they are about sequencing extra monadic stuff, which seems to be what the M
> suffix is actually about. So they seem like the better names.

I would actually say that the M suffix is primarily used to indicate
that sequential control flow is involved. In Control.Monad that
amounts to very simple loop-like constructs, but in other code the
fooM convention seems to appear most often on functions that want to
do something to a data structure while binding only those monadic
values corresponding to constructors that would be created/evaluated
in the pure version. Thus why the M prefix is so commonly used when
reinventing simple conditional statements.

Not sure how that would apply to liftM & co., though.

- C.


More information about the Libraries mailing list