本文介绍了为什么在Haskell中,协函子和反函子之间没有区别,但范畴论却没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从类别理论的角度来看,这个答案包括以下陈述:

...

更详细地讲,从类别C到类别D的逆变函子F仅仅是对立类别F :< C →D类型的(协变)函子C归类为D.

More in details a contravariant functor F from a category C to a category D is nothing more than a (covariant) functor of type F : C→D, from the opposite category of C to the category D.

另一方面,Haskell的 Functor Contravariant 仅分别要求为实例定义fmapcontramap.这表明,从Haskell的角度来看,存在Contravariant但不是Functor的对象(反之亦然).

On the other hand, Haskell's Functor and Contravariant merely require fmap and contramap, respectively, to be defined for an instance. This suggests that, from the perspective of Haskell, there exists objects that are Contravariant but are not Functors (and vice versa).

因此,似乎在分类论中协函子和逆函子之间没有真正的区别",而在Haskell中,ContravariantFunctor之间存在区别.

So it seems that in Category Theory "there's no real distinction between co and contravariant functors" while in Haskell there is a distinction between Contravariant and Functor.

我怀疑这种差异与Haskell中在Hask中执行的所有操作有关,但我不确定.

I suspect that this difference has something to with all implementation in Haskell happening in Hask, but I'm not sure.

我想我自己理解了类别理论和Haskell的每种观点,但是我正在努力寻找一种将两者联系起来的直觉.

I think I understand each of the Category Theory and Haskell perspectives on their own, but I'm struggling to find an intuition that connects the two.

推荐答案

这是为了方便.

使用更一般Functor 类,并为Hask上的endofunctors实例(对应于我们现有的Functor)以及从Hask ^ op到Hask的函子(对应于我们现有的Contravariant)定义实例.但这要付出比喻性的认知成本和相当大的语法成本:然后必须依靠类型推断或类型注释来选择实例,并且在标准库中有显式转换(在标准库中分别命名为OpgetOp)并从Hask ^ op中移出.

One could get by with a more general Functor class, and define instances for endofunctors on Hask (corresponding to our existing Functor) and functors from Hask^op to Hask (corresponding to our existing Contravariant). But this comes at a figurative cognitive cost and a quite literal syntactical cost: one must then rely on type inference or type annotations to select an instance, and there are explicit conversions (named Op and getOp in the standard library) into and out of Hask^op.

使用名称fmapcontramap降低了两个成本:读者无需费心地运行Hindley-Milner来确定在确定实例时选择的实例,并且编写者无需进行明确的转换.或输入注释以选择一个模棱两可的实例.

Using the names fmap and contramap relaxes both costs: readers do not need to run Hindley-Milner in their head to decide which instance is being selected when it is unambiguous, and writers do not need to give explicit conversions or type annotations to select an instance in cases where it is ambiguous.

(我实际上是在这里重写历史. real 的原因是因为语言设计师认为专门的Functor会很有用,并且没有想象或没有必要对于更通用的Functor.人们后来才发现它有时是有用的,但是使用通用化的Functor类的经验表明这可能很乏味,而对于最常见的情况,专门化的类却证明了这一点.出于上述原因,毕竟毕竟出人意料地非常适合.)

(I am actually rewriting history a little bit here. The real reason is because the language designers thought the specialized Functor would be useful and hadn't imagined or didn't see a need for a more general Functor. People came along later and noticed it would be useful, sometimes. But experience with the generalized Functor class shows that can be tedious, and that specialized classes for the most common cases turns out to be a surprisingly good fit after all, for the reasons described above.)

这篇关于为什么在Haskell中,协函子和反函子之间没有区别,但范畴论却没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 12:49