简体   繁体   English

使用应用风格的应用仿函数是否真的独立?

[英]Are applicative functors composed with the applicative style really independent?

I've come to understand functors, applicative functors, and monads as follows: 我来了解仿函数,applicative仿函数和monad如下:

  • Functors: computations that can be mapped over. 函数:可以映射的计算。
  • Applicative functors: independent computations whose results can be combined together. 应用仿函数: 独立计算,其结果可以组合在一起。
  • Monad: (possibly, but not necessarily) dependent computations that can be chained. Monad :(可能但不一定)可以链接的依赖计算。

However, there is something about Applicative that conflicts with my understanding... Here is a Haskell example of a parser defined on the basis of more basic parsers using the applicative style: 但是,有一些关于Applicative的东西与我的理解相冲突......这是一个Haskell示例,它是在使用应用程序样式的更基本的解析器的基础上定义的解析器:

(,) <$> parseName <*> parseEmail

The effects of the two parsers, parseName and parseEmail , are not independent, because they both consume tokens from the same input stream, eg 两个解析器parseNameparseEmail效果不是独立的,因为它们都使用来自相同输入流的标记,例如

Jubobs jubobs@jubobs.io

parseEmail can only consume what hasn't been consumed by parseName . parseEmail只能使用parseEmail未使用的parseName How, then, can the two computations be said to be independent? 那么,如何说这两个计算是独立的呢?

The independence here is not saying that one computation cannot detect what other computations have been run - that is, it's not supposed to be the case that parseName has no effect on parseEmail. 这里的独立性并不是说一个计算无法检测到其他计算已经运行 - 也就是说,不应该是parseName对parseEmail没有影响的情况。 Rather, you cannot make use of the applicative value (the name parsed by parseName) to choose what applicative computation to run next: you can only ever parse a generic email, rather than, say, parsing an email address while checking that it does not contain the parsed name. 相反,你不能使用应用 (由parseName解析的名称)来选择下一个运行的应用计算 :你只能解析一般的电子邮件,而不是解析一个电子邮件地址,同时检查它不是包含已解析的名称。

Another way of putting it is that if you use only applicative functions, the overall "shape" of your computation is predetermined ahead of time: you will always parse a name, followed by an email address. 另一种方法是,如果你只使用应用函数,你的计算的整体“形状”是提前预先确定的:你将始终解析一个名字,然后是一个电子邮件地址。 If you used monadic functions, you could make decisions about what to parse next based on the results of previous parses, letting you change the shape of your computation as it is running. 如果您使用了monadic函数,则可以根据先前解析的结果决定接下来要解析的内容,以便在运行时更改计算的形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM