gpt4 book ai didi

haskell - Haskell 中的 ApplicativeDo

转载 作者:行者123 更新时间:2023-12-04 22:43:00 26 4
gpt4 key购买 nike

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?



papertrac page 是最好的信息来源。几点:
  • ApplicativeDo 尽可能使用 applicatives - 包括在某些情况下将它们与 >>= 混合,其中只有 do 块的一部分是 applicative
  • 构造了一种依赖关系的有向图,以查看哪些部分可以“并行化”
  • 有时此图没有明显的最佳翻译!在这种情况下,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?



    Here is an answer 关于 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,并在 ApplicativeDo 确实比相应的应用表达式更易于编写时(就像在 Facebook 的某些情况下肯定是这样)。

    关于haskell - Haskell 中的 ApplicativeDo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689441/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com