AFAIK one of the new additions to GHC8 is the ApplicativeDo
language extension, which desugars the do-notation to the corresponding Applicative
methods (<$>
, <*>
) if possible. I have the following questions.
How does it decide whether the desugaring to Applicative
methods is possible? From what I know, it does a dependency check (if the later depends on the result of former) to decide the eligibility. Are there any other criteria?
Although this addition makes applicative code easier to read for classes that don't have any Monad instance (maybe ?). But for structures that both have a Monad and an Applicative instance: Is this a recommended practice (from the readability perspective)? Are there any other benefits?
The paper and trac page are the best sources of information. Some points:
ApplicativeDo
uses applicatives wherever possible - including mixing them with>>=
in some cases where only part of thedo
block is applicativeHere is an answer about the difference between
Applicative
andMonad
. To quote directly:A prime example of this sort of thing is the
Haxl
monad (designed by Facebook) which is all about fetching data from some external source. UsingApplicative
, these requests can happen in parallel whileMonad
forces the requests to be sequential. In fact, this example is what motivated Simon Marlow at Facebook to make theApplicativeDo
extension in the first place and write the quoted paper about it.In general, most
Monad
instances don't necessarily benefit fromApplicative
. From the same answer I quoted above:So: use
Applicative
overMonad
when possible, and exploitApplicativeDo
when it really is nicer to write (like it must have been in some cases at Facebook) than the corresponding applicative expression.