为什么`pure`只适用于Applicative而不是Functor? [英] Why is `pure` only required for Applicative and not already for 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 中:
- 类型是对象
- 函数是态射因此,Haskell中的所有
到Hask(即它们是endofunctors)。Functor
实例都是来自Hask的函子。
更严格地说,Haskell中所有
Functor
的实例:
C = Hask
。
D = Hask
。
现在,每个函子 F 是与每个对象关联的映射。 X∈C 一个对象 F(X)∈D 。
- 请注意, X 和 F(X)是 C 和 D
- 因为 C 和 D 都是 Hask ,所以 X em>和 F(X)是类型而不是值。
- 因此, F:Type→Type 或Haskell
f:* - > *
。
的确,这正是
Functor $ c Haskell中定义了$ c> 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 likepure
.If I get it right,
Maybe
rather mapsC
toD
. (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:
- Types are objects.
- 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:
C = Hask
.D = Hask
.Now, each functor F is a mapping that associates to each object X ∈ C an object F(X) ∈ D.
- Note that X and F(X) are objects of C and D respectively.
- Since both C and D are Hask, both X and F(X) are types and not values.
- 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, theFunctor
itself is a type constructor (i.e. a mapping from types to types). This is the reasonMaybe
is a functor and[]
is a functor butMaybe 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屋!