Functor,Applicative Functor和Monad之间的关系 [英] Relationship between Functor, Applicative Functor, and Monad

查看:119
本文介绍了Functor,Applicative Functor和Monad之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读类型课程时,我发现Functors,Applicative Functors和Monads之间的关系是严格增加权力的关系。函子是可以映射的类型。 Applicative Functors可以用某些效果来做同样的事情。 Monads与可能不受限制的效果相同。此外:

 每个Monad是一个应用Functor 
每个Applicative Functor是一个Functor

$ b $ p
$ b $ p





$ b

class Functor f =>应用f其中
pure :: a - > f a
(*):: f(a - > b) - > f a - > fb

但是Monad的定义是:

  class Monad m其中
return :: a - > m a
(>> =):: m a - > (a - > m b) - > m b
(>>):: m a - > m b - > m b
m>> n = m>> = \ _ - > n
fail :: String - > ma



根据Brent Yorgey的

  class Applicative m =>> haskellwiki / Typeclassopedia#Monad>> typeclassopedia 表示monad的另一个定义可以是: ; Monad'm其中
(>> =):: m a - > (a - > m b) - > m

明显更简单可以巩固Functor< Applicative Functor<单子。那为什么不是这个定义呢?我知道应用仿函数是新的,但根据 2010年Haskell报告第80页,这并没有改变。这是为什么?

解决方案

每个人都希望看到Applicative成为Monad的超类,但它会破坏很多代码(如果 return 被消除,每个当前的Monad实例都变得无效),每个人都希望延续这种语言,直到我们可以避免破坏代码(请参阅此处了解一项重要提案)。



Haskell 2010是一个保守的,渐进式的改进,标准化了一些没有争议的扩展,并破坏了一个领域,以使标准符合每一个现有的实施。实际上,Haskell 2010的图书馆甚至不包括Applicative - 少了人们对标准库标准化程度超出您的预期。



希望我们很快会看到情况有所改善,但幸好它通常只是一个轻微的不便(必须写 liftM 而不是 fmap 在泛型代码中)。


When reading up on type classes I have seen that the relationship between Functors, Applicative Functors, and Monads is that of strictly increasing power. Functors are types that can be mapped over. Applicative Functors can do the same things with certain effects. Monads the same with possibly unrestrictive effects. Moreover:

Every Monad is an Applicative Functor
Every Applicative Functor is a Functor

The definition of the Applicative Functor shows this clearly with:

class Functor f => Applicative f where
  pure  :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

But the definition of Monad is:

class Monad m where
  return :: a -> m a
  (>>=)  :: m a -> (a -> m b) -> m b
  (>>)   :: m a -> m b -> m b
  m >> n = m >>= \_ -> n
  fail   :: String -> m a

According to Brent Yorgey's great typeclassopedia that an alternative definition of monad could be:

class Applicative m => Monad' m where
  (>>=) :: m a -> (a -> m b) -> m b

which is obviously simpler and would cement that Functor < Applicative Functor < Monad. So why isn't this the definition? I know applicative functors are new, but according to the 2010 Haskell Report page 80, this hasn't changed. Why is this?

解决方案

Everyone wants to see Applicative become a superclass of Monad, but it would break so much code (if return is eliminated, every current Monad instance becomes invalid) that everyone wants to hold off until we can extend the language in such a way that avoids breaking the code (see here for one prominent proposal).

Haskell 2010 was a conservative, incremental improvement in general, standardising only a few uncontroversial extensions and breaking compatibility in one area to bring the standard in line with every existing implementation. Indeed, Haskell 2010's libraries don't even include Applicative — less of what people have come to expect from the standard library is standardised than you might expect.

Hopefully we'll see the situation improve soon, but thankfully it's usually only a mild inconvenience (having to write liftM instead of fmap in generic code, etc.).

这篇关于Functor,Applicative Functor和Monad之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆