Search code examples
haskelllifting

Partial lift for Monads?


Is there something like a partial lift for monads? I need the type signature to be this:

Monad m => (a1 -> r) -> a1 -> m r

Instead of this:

Monad m => (a1 -> r) -> m a1 -> m r

For use with >=> and other reasons. I'm looking for the canonical way of doing the above.


Solution

  • That's just (return .): all you need to do is inject the return value of your function into m, which return accomplishes. Usually, this is just written inline, as return . f or return $ f x, where f :: a -> r; I've never seen it defined.

    (Also, these days, that could be (pure .) instead, which has the more general type Applicative f => (a -> r) -> a -> f r.)