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

查看:28
本文介绍了为什么 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 α (for 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 β).

到目前为止,我想,我没有说任何新的东西.但重要的是 return :: α ->Monad 的 M α 不是你想象的 αM α 类型的映射器.关于 monad 的数学定义,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 成为内函子范畴中的幺半群)

(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.

如果你从标准的 bind >>= :: m α -> = :: 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 上的文章,了解计算机科学中的 monad.

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天全站免登陆