简体   繁体   English

Functor -> Applicative -> Monad 层次结构的意义是什么

[英]What is the point of the Functor -> Applicative -> Monad hierarchy

What is the point of making Functor a super class of Applicative and Monad.让 Functor 成为 Applicative 和 Monad 的超级 class 有什么意义。 Both Applicative and Monad immediately imply the only implementation of Functor that abides by the laws as far as I can tell.据我所知,Applicative 和 Monad 都立即暗示了 Functor 的唯一实现。 But rather I have to type out the same implementation of Functor every time.但是我每次都必须输入相同的 Functor 实现。 Is there a way to avoid doing this?有没有办法避免这样做?

Further more Monad implies the only implementation of Applicative that abides by the laws so why make Applicative a super class of Monad?此外,Monad 意味着 Applicative 的唯一实现是遵守法律的,那么为什么让 Applicative 成为 Monad 的超级 class 呢? Again it makes implementing Applicative for new data types redundant.它再次使得为新数据类型实现 Applicative 变得多余。

Is there a way to make a Monad without having to implement Applicative and Functor (as its operations are already the most general).有没有一种方法可以在不必实现 Applicative 和 Functor 的情况下制作 Monad(因为它的操作已经是最通用的了)。 And to make an Applicative without having to implement Functor.并且无需实现 Functor 即可制作 Applicative。

I see the benefit of the class hierarchy as what I just said explains the "is a" relation between them.我看到了 class 层次结构的好处,因为我刚才所说的解释了它们之间的“是”关系。 But at the same time having to implement each is annoying.但同时必须实现每一个都很烦人。 I just want to define return and >>= and get all the operations of all 3 back.我只想定义return>>=并获取所有 3 的所有操作。

You can get those instances this way:您可以通过以下方式获取这些实例:

import Control.Applicative
data Whatever = {- ... -} deriving (Functor, Applicative) via (WrappedMonad Whatever)

It would have been nice to add DefaultSignatures defaults to the Functor and Applicative classes at the time the compiler broke from the spec, so that you wouldn't even need to write via WrappedMonad , but now that it's done I suspect we'll never get them.在编译器违反规范时将DefaultSignatures默认值添加到FunctorApplicative类会很好,这样你甚至不需要via WrappedMonad编写,但现在已经完成了,我怀疑我们永远不会得到他们。 Too bad.太糟糕了。

The cost of having to write the Applicative and Monad instances is paid only once, by the implementer.编写ApplicativeMonad实例的成本只需由实现者支付一次。 The cost of not having those instances, however, is borne by the users, time and time again.然而,没有这些实例的成本由用户一次又一次地承担。 Consider a combinator which uses Applicative , such as traverse :考虑一个使用Applicative的组合器,例如traverse

traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)

Without the superclass relationship, our choice of f being a Monad wouldn't be enough to let us use traverse .如果没有超类关系,我们选择f作为Monad将不足以让我们使用traverse We'd end up needing a Monad -specific version of traverse (that is, mapM ), and to switch from one to the other on the grounds of a distinction which is almost always irrelevant.我们最终需要一个特定于Monadtraverse版本(即mapM ),并基于几乎总是不相关的区别从一个切换到另一个。 Having Applicative as a superclass of Monad means we need not bother with that.Applicative作为Monad的超类意味着我们不必为此烦恼。

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

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