Proposal: add two more Applicative and Monad adapters

David Feuer david.feuer at gmail.com
Sat Aug 11 17:19:20 UTC 2018


We currently offer liftA, liftM, liftM2, and ap to implement Functor and
Applicative methods in terms of Applicative and Monad ones. But there are a
couple other functions in that general vein that are missing. I propose
that we should add at least replaceA, and perhaps also beforeM.

-- (<$) = replaceA
-- This may be better than the default if there is
-- an optimized definition of *> (which may be
-- based on an optimized >>).
replaceA :: Applicative f => a -> f x -> f a
replaceA a fa = fa *> pure a

-- (<*) = beforeM
-- This may be better than the default if there is
-- an optimized definition of <$, or if <$ is defined as
-- replaceA and *> is optimized.
beforeM :: Monad f => f a -> f x -> f a
beforeM fa fx = fa >>= \a -> a <$ fx

Why a <$ fx and not fx >> pure a? Because <$ could be implemented
specially, and is unlikely to be implemented by hand using <* if there
isn't a custom <*.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20180811/6e3cd40d/attachment.html>


More information about the Libraries mailing list