为什么`pure`只适用于Applicative而不是Functor? [英] Why is `pure` only required for Applicative and not already for Functor?

查看:143
本文介绍了为什么`pure`只适用于Applicative而不是Functor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 Wikibook关于Haskell和Category Theory的基础知识,我了解了Functors:


函子本质上是一个类别之间的转换,所以给出
类别C和D,一个函子F:C - > D

将C中的任何对象A映射到F(A),D中。

映射态射f:A- > B在C到F(F):F(A) - > F(B)在D。

...哪听起来很好。后面提供了一个例子:


我们还有一个示例实例:




  instance Functor也许其中
fmap f(Just x)= Just(fx)
fmap _ Nothing = Nothing




以下是关键部分:类型构造函数Maybe可以将任何类型T转换为
新类型Maybe T。另外,fmap仅限于Maybe类型可能需要一个
函数a - > b函数Maybe a - > Maybe b。但就是这样!我们有
定义了两个部分,这些部分将Hask中的对象接收到
中的另一个类别(在Maybe
类型中定义的类型和函数的类型),以及Hask中的态射以
这个类别的态射。所以也许是一个仿函数。


我明白 fmap 的定义是键。我对类型构造函数Maybe如何提供第一部分感到困惑。我希望像纯的。



如果我理解正确, Maybe 相反地将 C 映射为 D 。 (因此,在类别级别上的态射,这可能是一个Functor的要求)

我猜你可以像这样重新修改我的问题:是否有Functor不有一个明显的实现

解决方案

我认为你是在类型和值之间混淆。这是一个仿函数的定义:


C D 类别。从 C D 函子 F 是一个映射:




  • 与每个对象关联 X∈C 对象 F(X)∈D
    (f):F(X)→F(Y)∈D 这样的
  • 关联到每个态射f:X→Y∈C 以下条件成立:


    • F(id:X→X)= id:F(X)→F(X )对于每个对象X∈C
    • F(g∘f)= F(g)∘F(f) / em>所有态射 f:X→Y g:Y→z



  • $ b

    类别由对象之间的对象和态射构成。 Haskell中的所有代码都是 Hask ,Haskell类。在 Hask 中:


    1. 类型是对象

    2. 函数是态射因此,Haskell中的所有 Functor 实例都是来自Hask的函子。
    3. 到Hask(即它们是endofunctors)。



      更严格地说,Haskell中所有 Functor 的实例:


      1. C = Hask

      2. D = Hask

      现在,每个函子 F 是与每个对象关联的映射。 X∈C 一个对象 F(X)∈D


      1. 请注意, X F(X) C D

      2. 因为 C D 都是 Hask ,所以 X em>和 F(X)是类型而不是值。
      3. 因此, F:Type→Type 或Haskell f:* - > *

      的确,这正是 Functor type类:

        class Functor(f:*  - > *)其中
      fmap ::(x - > y) - > (fx - > fy)

      这里, fmap 是仿函数的第二部分。这是一个从值到值的函数。然而, Functor 本身是一个类型构造函数(即从类型到类型的映射)。这就是原因可能是一个函子, [] 是一个函数,但 Maybe Int [Int] 不是函子。



      请注意不构成函子定义的第一部分,因为它是从 X 的实例到 F(X)的一个实例的映射。即它是从值到值的函数)。但是,我们需要从 X F(X)的映射(即从类型到类型的映射)。

      Reading this Wikibook about Haskell and Category Theory basics, I learn about Functors:

      A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D

      maps any object A in C to F(A), in D.

      maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D.

      ... which sounds all nice. Later an example is provided:

      Let's have a sample instance, too:

      instance Functor Maybe where
        fmap f (Just x) = Just (f x)
        fmap _ Nothing  = Nothing
      

      Here's the key part: the type constructor Maybe takes any type T to a new type, Maybe T. Also, fmap restricted to Maybe types takes a function a -> b to a function Maybe a -> Maybe b. But that's it! We've defined two parts, something that takes objects in Hask to objects in another category (that of Maybe types and functions defined on Maybe types), and something that takes morphisms in Hask to morphisms in this category. So Maybe is a functor.

      I understand how the definition of fmap is key. I am confused about how the "type constructor Maybe" provides the first part. I would have rather expected something like pure.

      If I get it right, Maybe rather maps C to D. (Thus being a morphism on category level, which might be a requirement for a Functor)

      I guess you could rephrase my question like this: Is there a Functor that does not have an obvious implementation of pure?

      解决方案

      I think you're getting confused between types and values. Here's the definition of a functor:

      Let C and D be categories. A functor F from C to D is a mapping that:

      • associates to each object X ∈ C an object F(X) ∈ D.
      • associates to each morphism f : X → Y ∈ C a morphism F(f) : F(X) → F(Y) ∈ D such that the following conditions hold:

        • F(id : X → X) = id : F(X) → F(X) for every object X ∈ C.
        • F(g ∘ f) = F(g) ∘ F(f) for all morphisms f : X → Y and g : Y → Z.

      A category consists of objects and morphisms between objects.

      All code in Haskell is a part of Hask, the Haskell category. In Hask:

      1. Types are objects.
      2. Functions are morphisms between types.

      Hence, all Functor instances in Haskell are functors from Hask to Hask (i.e. they are endofunctors).

      To put it more rigorously, for all instances of Functor in Haskell:

      1. C = Hask.
      2. D = Hask.

      Now, each functor F is a mapping that associates to each object X ∈ C an object F(X) ∈ D.

      1. Note that X and F(X) are objects of C and D respectively.
      2. Since both C and D are Hask, both X and F(X) are types and not values.
      3. Thus, F : Type → Type or in Haskell f : * -> *.

      Indeed, this is precisely how the Functor type class is defined in Haskell:

      class Functor (f : * -> *) where
          fmap :: (x -> y) -> (f x -> f y)
      

      Here, fmap is the second part of the functor. It's a function from values to values. However, the Functor itself is a type constructor (i.e. a mapping from types to types). This is the reason Maybe is a functor and [] is a functor but Maybe Int and [Int] are not functors.

      Note that pure does not form the first part of the functor definition because it's a mapping from an instance of X to an instance of F(X) (i.e. it's a function from values to values). However, we need a mapping from X to F(X) (i.e. a mapping from types to types).

      这篇关于为什么`pure`只适用于Applicative而不是Functor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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