48

What is the difference between return and pure from Control.Applicative ? It seems that I can use pure even at the end of a do block?

So is there any situation where one should be preferred over the other (besides that everyone expects a return at the end of a do Block)?

5
  • 12
    There is no difference, they both exist for historical reasons (Monad has not always been a superclass of Applicative). There is a proposal to alias return to pure.
    – Lee
    Sep 25, 2015 at 18:06
  • 3
    To clarify, they are only the same if you know you are dealing with a Monad. pure is more general than return in that it returns an Applicative so you can't replace all instances of return with pure but you can replace pure with return.
    – Lee
    Sep 25, 2015 at 18:14
  • 15
    @Lee, I think it's the other way around in your second sentence. Sep 25, 2015 at 18:31
  • 3
    Relevant proposal and discussion on reddit from today, coincidentally: reddit.com/r/haskell/comments/3mb8lb/…
    – jberryman
    Sep 25, 2015 at 18:46
  • 1
    Some authors have pointed out that a less powerful algebra (in this case Applicative) can be better for a given application if the more powerful algebra (in this case monad) is overkill. (Reasons include clarifying intent, highlighting what really matters, discouraging creep, etc.) If that is true, does it make sense to keep both functions? Feb 24, 2022 at 6:42

2 Answers 2

59

In GHC 7.8 and before, Applicative was not a superclass of Monad. It was even possible for a Monad instance to not have an Applicative instance. There was, however, an expectation that pure and return should have the same behavior for types that are instances of both.

In GHC 7.10, due to the Functor-Applicative-Monad Proposal, Applicative is now a superclass of Monad (class Applicative m => Monad m) and it is now a rule that pure and return must be the same for all Monad instances. In fact, the default implementation of return is now pure, as seen in the source on hackage.

pure might be preferred to return because it does not incur a Monad constraint, only an Applicative constraint, thus making the function more general. return might be preferred to pure in do notation because of historical precedent, but pure could be used to exactly the same effect.

28

The Applicative typeclass was added after Monad and historically the Monad class has not been a subclass of Applicative. This was changed fairly recently in the Applicative-Monad-Proposal and this means that return a should be equivalent to pure a for every Monad instance.

There is a proposal to move return out of the Monad class and make it an alias for pure.

Therefore when dealing with a Monad you should always be able to use pure instead of return. You cannot go the other way however since pure has a more general type than return since it only returns an Applicative. For example the following

wontCheck :: Applicative f => f Int
wontCheck = return 4

won't type check since return requires f to be a Monad.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.