Haskell 中的 ApplicativeDo

ApplicativeDo in Haskell

AFAIK GHC8 的新增功能之一是 ApplicativeDo 语言扩展,它将 do-notation 脱糖为相应的 Applicative 方法(<$><*> ) 如果可能的话。我有以下问题。

如何判断是否可以对Applicative方法进行脱糖?据我所知,它会进行依赖性检查(如果后者取决于前者的结果)来决定是否合格。还有其他标准吗?

尽管此添加使没有任何 Monad 实例(也许?)的 类 的应用代码更易于阅读。但是对于同时具有 Monad 和 Applicative 实例的结构:这是推荐的做法(从可读性的角度来看)吗?还有其他好处吗?

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?

paper and trac page 是最好的信息来源。几点:

  • ApplicativeDo 尽可能使用应用程序——包括在某些情况下将它们与 >>= 混合,其中只有 do 块的一部分是应用程序
  • 构建了一种依赖关系的有向图,看看哪些部分可以"parallelized"
  • 有时候这个图没有明显的最佳翻译!在这种情况下,GHC 选择(粗略地说)局部最小的翻译

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?

关于ApplicativeMonad的区别。直接引用:

To deploy <*>, you choose two computations, one of a function, the other of an argument, then their values are combined by application. To deploy >>=, you choose one computation, and you explain how you will make use of its resulting values to choose the next computation. It is the difference between "batch mode" and "interactive" operation.

这种事情的一个主要例子是 Haxl monad(由 Facebook 设计),它是关于从一些外部源获取数据的。使用 Applicative,这些请求可以并行发生,而 Monad 强制请求是顺序的。事实上,正是这个例子激发了 Facebook 的 Simon Marlow 首先制作 ApplicativeDo 扩展,并撰写了关于它的引用论文。

一般来说,大多数 Monad 个实例不一定受益于 Applicative。来自我上面引用的相同答案:

I appreciate that ApplicativeDo is a great way to make more applicative (and in some cases that means faster) programs that were written in monadic style that you haven't the time to refactor. But otherwise, I'd argue applicative-when-you-can-but-monadic-when-you-must is also the better way to see what's going on.

因此:在可能的情况下使用 Applicative 而不是 Monad,并在确实比相应的应用表达式。