简体   繁体   English

LYHFGG:“Monads只是支持>> =”的应用函子。 这个陈述在什么意义上是真的?

[英]LYHFGG: “Monads are just applicative functors that support >>=”. In what sense is this statement true?

In LYHFGG the author states that " Monads are just applicative functors that support >>= " (see image below). LYHFGG中 ,作者声称“ Monads只是支持>> = ”的应用函子 (见下图)。 I don't see how this statement can be true if I look at the definition of Monad type class . 如果我看一下Monad类型的定义,我不会看到这个陈述是如何成真的。

The Monad type class seems to have no relation whatsoever to the Control.Applicative type class, for example Monad type classes are not subtypes of Applicative. Monad类型似乎与Control.Applicative类型没有任何关系,例如Monad类型类不是Applicative的子类型。 So it is clear that, technically, in Haskell, Monads and Applicative functors are completely independent type classes. 所以很明显,从技术上讲,在Haskell中,Monads和Applicative仿函数是完全独立的类型。 So if the author's statement is true then it must be true in a different context. 因此,如果作者的陈述是真的,那么它必须在不同的背景下成立。

Could someone please explain what the book author means by this seemingly untrue statement? 有人可以通过这个看似不真实的陈述来解释这位作者的意思吗?

How should his statement be interpreted ? 他的陈述应如何解释? In what context? 在什么情况下? In the context of category theory perhaps? 在类别理论的背景下或许?

In other words : I don't see how it is possible to turn any given Monad into an Applicative functor. 换句话说:我不知道如何将任何给定的Monad变成Applicative functor。 Because if the author's statement is true then every Monad can be turned mechanically (by using an algorithm) into an Applicative functor. 因为如果作者的陈述是真的,那么每个Monad都可以机械地(通过使用算法)转换为Applicative仿函数。 But is it really possible to do that? 但是真的可以这样做吗? If yes, how? 如果有,怎么样?

在此输入图像描述

You're right that the statement means that you can write an Applicative instance when all you know is that your type constructor is a monad. 你是对的,声明意味着当你所知道的是你的类型构造函数是一个monad时,你可以编写一个Applicative实例。 If M is a monad, then you can write: 如果M是monad,那么你可以写:

instance Applicative M where
  pure = return
  mf <*> mx =
    mf >>= \f ->
      mx >>= \x ->
        return (f x)

And actually, starting with GHC 7.10, Applicative will be a superclass of Monad , meaning that the concept that "Monad is Applicative plus ..." will be baked in the standard library. 实际上,从GHC 7.10开始, Applicative将成为Monad的超类,这意味着“Monad是Applicative plus ...”的概念将在标准库中出现。

The author is perfectly right. 作者是完全正确的。

So it is clear that, technically, in Haskell, Monads and Applicative functors are completely independent type classes. 所以很明显,从技术上讲,在Haskell中,Monads和Applicative仿函数是完全独立的类型。

In fact, Monad should be a "subclass" of Applicative . 事实上, Monad应该是Applicative的“子类”。 It has been proposed and will probably be standardized in Haskell 2014. Everybody agrees that it was a mistake not to do it this way from the start. 已被提议并且可能在Haskell 2014中被标准化。每个人都同意,从一开始就不这样做是错误的。

We know that a Monad is a monad if it defines: 我们知道Monad是monad,如果它定义:

  • return
  • >>=
  • >> (can be derived from >>= and return ) >> (可来源于>>=return

I'll leave fail aside intentionally. 我故意将fail搁置一边。

You can see that Applicative defines, amongst other things, pure , which is the same as return , and *> which is the same as >> . 您可以看到Applicative定义了pure ,与return相同, *>>>相同。 Therefore the only remaining difference is the definition of >>= . 因此唯一剩下的区别是>>=的定义。

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

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