为什么Functor类没有返回功能? [英] Why Functor class has no return function?

查看:87
本文介绍了为什么Functor类没有返回功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从分类的角度来看,函子是遵循某些公理的两张地图的对(一个在对象之间,另一个在类别箭头之间).

From categorical point of view, functor is pair of two maps (one between objects and another between arrows of categories), following some axioms.

我假设,每个Functor实例都类似于数学定义,即可以映射对象和函数,但是Haskell的Functor类仅具有映射函数的函数fmap.

I have assumed, what every Functor instance is similar to mathematical definition i.e. can map both objects and functions, but Haskell's Functor class has only function fmap which maps functions.

为什么呢?

UPD 换句话说:

每个Monad类型M都有一个功能return :: a -> M a.

Every Monad type M has an function return :: a -> M a.

Functor类型的F没有功能return :: a -> F a,而只有F x构造函数.

And Functor type F has no function return :: a -> F a, but only F x constructor.

推荐答案

首先,有两个级别:类型和值.由于Hask的对象是类型,因此只能使用类型构造器来映射它们,该构造器具有* -> *类型:

First of all, there are two levels: types and values. As objects of Hask are types, you can map them only with type constructors, which have the * -> * kind:

  • α -> F α(对于Functor F),
  • β -> M β(对于Monad M).
  • α -> F α (for Functor F),
  • β -> M β (for Monad M).

然后对于函子,您需要一个关于态射的映射(即函数,即值):只是fmap :: (α -> β) -> (F α -> F β).

Then for a functor you need a map on morphisms (i.e. functions, which are values): it's just fmap :: (α -> β) -> (F α -> F β).

到目前为止,我想,我还没有说什么新的话.但是重要的是,Monadreturn :: α -> M α并不是您可能认为的α类型的映射器.关于单子的数学定义, return对应于从Id函子到M函子的自然变换.只是这个Id函子是隐式的. monad的标准定义还需要另一个自然转换M ◦ M -> M.因此将其翻译为Haskell就像

So far, I guess, I'm not saying anything new. But important thing is that return :: α -> M α of Monad is not a mapper of a type α to the M α as you may think. Regarding to the math definition of a monad, return corresponds to a natural transformation from Id functor to the M functor. Just that this Id functor is kind of implicit. The standard definition of monad requires also another natural transformation M ◦ M -> M. So translating it to Haskell would be like

class Functor m => Monad m where
    return :: Id α -> m α
    join :: m (m α) -> m α

(附带说明:这两个自然变换实际上是单位和乘法,这使monad成为endofunctors类别中的 monoid )

(As a side-note: these two natural transformations are actually the unit and multiplication, which make monad a monoid in the category of endofunctors)

实际定义不同,但等效.请参阅 Haskell/wiki .

The actual definition differs but is equivalent. See Haskell/wiki on that.

如果采用从标准绑定>>= :: m α -> (α -> m β) -> m β派生的类似构图的运算符:

If you take the composition-like operator derived form the standard bind >>= :: m α -> (α -> m β) -> m β:

(>=>) :: Monad m => (α -> m β) -> (β -> m γ) -> (α -> m γ)
f >=> g = \a => f a >>= g

您会看到,这实际上完全是有关 Kleisli类别的.另请参阅 nLab上的文章有关计算机科学中的单子.

you can see, that it's all actually about the Kleisli category. See also the article on nLab about monads in computer science.

这篇关于为什么Functor类没有返回功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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