Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

haskell - Difference between 'return' and 'pure'

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)?

question from:https://stackoverflow.com/questions/32788082/difference-between-return-and-pure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...