所有的Haskell函子都是终结子吗?
收藏

我有些困惑,需要有人把我弄直。让我们概述一下我目前的理解:

Where E is an endofunctor, and A is some category:

E : A -> A.

Since all types and morphisms in Haskell are in the Hask category, is not any functor in Haskell also an endofunctor? F : Hask -> Hask.

我有一种很好的感觉,那就是我错了,并且以某种方式简化了这一过程,我希望有人告诉我我是个白痴。谢谢。

最佳答案

You may want to clarify whether you're asking about "functors in Haskell", or Functors. It's not always clear what category is being assumed when Category Theory terms are used in Haskell.

但是,是的,默认假设是Hask,它被视为Haskell类型的范畴,具有函数作为态射。在那种情况下,在Hask上的endofunctor F将任何类型A映射为F(A),将两个A和B类型之间的任何函数f映射为某些F(A)和F(B)之间的函数F(f)。 。

If we then limit ourselves to only those endofunctors which map any type a to a type (f a) where f is a type constructor with kind * -> *, then we can describe the associated map for functions as a higher-order function with type (a -> b) -> (f a -> f b), which is of course the type class called Functor.

However, one can easily imagine well-behaved endofunctors on Hask which can't be written (directly) as an instance of Functor, such as a functor mapping a type a to Either a t. And while there's obviously not much sense in a functor from Hask to some other category entirely, it's reasonable to consider a (contravariant) functor from Hask to Haskop.

Beyond that, instances of Functor necessarily map from the entire category Hask onto some subset of it that, thus, also forms a category. But it's also reasonable to talk about functors between subsets of Hask. For instance, consider a functor that sends types Maybe a to [a].

You may wish to peruse the category-extras package, which provides some Category Theory-inspired structures embedded within Hask instead of assuming the entirety of it.

    公众号
    关注公众号订阅更多技术干货!