问题描述
单子从Functor
类型类获取fmap
.为什么comonads不需要在Cofunctor
类中定义的cofmap
方法?
Monads get fmap
from Functor
typeclass. Why comonads don't need a cofmap
method defined in a Cofunctor
class?
推荐答案
Functor
定义为:
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
Cofunctor
可以定义如下:
class Cofunctor f where
cofmap :: (b -> a) -> (f b -> f a)
因此,两者在技术上都是相同的,这就是Cofunctor
不存在的原因. '一般的functor'的双重概念仍然是'一般的functor'".
So, both are technically the same, and that's why Cofunctor
does not exist. "The dual concept of 'functor in general' is still 'functor in general'".
由于Functor
和Cofunctor
相同,所以通过使用Functor
定义单子和共母.但是,不要让那让您认为单子和子母是同一回事,它们不是同一回事.
Since Functor
and Cofunctor
are the same, both monads and comonads are defined by using Functor
. But don't let that make you think that monads and comonads are the same thing, they're not.
一个monad的定义(简化)为:
A monad is defined (simplifying) as:
class Functor m => Monad where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
一个通俗的(再次简化)是:
whether a comonad (again, simplified) is:
class Functor w => Comonad where
extract :: w a -> a
extend :: (w a -> b) -> w a -> w b
注意对称性".
另一件事是逆函子,定义为:
Another thing is a contravariant functor, defined as:
import Data.Functor.Contravariant
class Contravariant f where
contramap :: (b -> a) -> (f a -> f b)
这篇关于为什么Haskell中没有`Cofunctor`类型类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!