Haskell中的所有类型类都具有类别理论类比吗? [英] Do all Type Classes in Haskell Have a Category Theoretic Analogue?

查看:78
本文介绍了Haskell中的所有类型类都具有类别理论类比吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个成员为* -> *类型的类型类.例如:Functor类型类.众所周知的事实是,在Haskell中,此类型类与其数学(即,类别理论)类似物之间存在对应关系.概括:

Consider a type class whose members are of type * -> *. For example: the Functor typeclass. It is a well-known fact that, in Haskell, there is a correspondence between this typeclass and its mathematical (i.e., Category Theoretic) analogue. Generalizing:

问题1: Haskell中成员类型为* -> *的每个类型类是否都与类别之间的 some 函数相对应?

Question 1: Does every every typeclass in Haskell whose members are of kind * -> * correspond to some function between categories?

现在考虑一个成员为*类型的typeclass.例如,可以想象一个类型类Group与组的类别相对应(从技术上讲,GroupHask的子类,其对象包括所有Haskell类型).概括:

Now consider a typeclass whose members are of type *. For example, one could imagine a type class Group which corresponds to the category of Groups (technically, Group would be a subcategory of Hask whose objects comprise all of Haskell's types). Generalizing:

问题2: Haskell中成员类型为*的每个类型类都对应于 some 类别(技术上是Hask的某些子类别)吗?

Question 2: Does every typeclass in Haskell whose members are of kind * correspond to some category (technically: some subcategory of Hask)?

由此,可以问下一个一般性问题:

From this, the next general question can be asked:

问题3:等于或高于* -> * -> *的类型的类型类是否对应于某种类别理论概念?

Question 3: Do typeclasses of kind equal to or higher than * -> * -> * correspond to some category theoretic notion?

实际上,整个问题可以总结如下:

And really, this entire question could be summarized as follows:

一般问题:每个Haskell类型类是否都对应于某个类别理论概念?

General Question: Does every Haskell type class correspond to some category theoretic notion?

至少,您似乎可以说,由于每个类型类都包含一组Haskell类型作为其成员,因此您可以将每个类型类都视为 some Hask的em>子类别(在.下关闭并使用id).

At the very least, it seems you could say that since every type class contains some set of Haskell types as its members, you could view every type class as some subcategory of Hask (closed under . and making use of id).

推荐答案

从学问上充分解释后,所有这些问题的答案都是是",但出于无关紧要的原因.

When interpreted sufficiently pedantically, the answer to all of these questions is "yes", but for uninformatively trivial reasons.

每个类别C都限于一个离散的子类别| C |.具有与C相同的对象,但只有身份同构(因此没有有趣的结构).至少,对Haskell类型的操作可以无聊地解释为对离散类别|*|的操作.最近的角色"故事相当于(但不被当作)试图承认形态学很重要,而不仅仅是对象.类型的名义"角色相当于在|*|中工作,而不是在*中工作.

Every category C restricts to a discrete subcategory |C| with the same objects as C but only identity morphisms (and hence no interesting structure). At the very least, operations on Haskell types can be boringly interpreted as operations on the discrete category |*|. The recent "roles" story amounts to (but is not spun as) an attempt to acknowledge that the morphisms matter, not just the objects. The "nominal" role for types amounts to working in |*| rather than *.

(请注意,我不喜欢将"Hask"用作"Haskell类型和函数类别"的名称:我担心将一个类别标记为 the Haskell类别有不幸的一面-使我们对Haskell编程中大量 other 类别结构的视而不见的效果.这是一个陷阱.)

(Note, I dislike the use of "Hask" as the name of the "category of Haskell types and functions": I fear that labelling one category as the Haskell category has the unfortunate side-effect of blinding us to the wealth of other categorical structure in Haskell programming. It's a trap.)

作为一个古怪的人,我要指出的是,您可以将任何旧的废话作为类型类组合成任何旧的类,而没有任何有趣的结构(但是如果需要的话,可以用分类的简单形式来讨论).但是,您在库中找到的类通常结构丰富.在设计上,* -> *上的类通常是Functor的子类,除了fmap之外,还需要存在某些自然转换.

Being differently pedantic, I'd note that you can make up any old crap as a typeclass over any old kind, with no interesting structure whatsoever (but with trivial structure that can still be talked about categorically, if one must). However, the classes you find in the library are very often structure-rich. Classes over * -> * are often, by design, subclasses of Functor, requiring the existence of certain natural transformations in addition to fmap.

对于问题2.是的,当然,在*上的类给出了*的子类别.从类别中剔除对象没有问题,因为在给定对象的情况下,存在相同性和复合性的分类要求要求存在态射,但不要求存在哪些 objects .它很无聊的事实使其成为无聊的事实.但是,许多*上的Haskell类型类产生的趣味类别要比*的子类别引起的类别有趣得多.例如,Monoid类为我们提供了一个类别,其中的对象是Monoid的实例,而箭头是 monoid同态:不仅是从一个Monoid到另一个的任何旧函数f,但是保留结构的一个:f mempty = memptyf (mappend x y) = mappend (f x) (f y).

For question 2. Yes, of course a class over * gives a subcategory of *. It's no problem to chuck objects out of a category, because the categorical requirement that identities and composites exist require morphisms to exist, given objects, but make no demands about which objects exist. The fact that it's boringly possible makes it a boring fact. However, many Haskell typeclasses over * give rise to much more interesting categories than those arising just as subcategories of *. E.g., the Monoid class gives us a category where the objects are instances of Monoid and the arrows are monoid homomorphisms: not just any old function f from one Monoid to another, but one which preserves the structure: f mempty = mempty and f (mappend x y) = mappend (f x) (f y).

对于问题3,好吧,因为到处都有大量的分类结构,因此,在较高种类中肯定有(可能但不一定)有大量分类结构.我特别喜欢索引集的族之间的函子.

For question 3, well, in that there's a ton of categorical structure lurking everywhere, there's certainly a ton of categorical structure available (possibly but not necessarily) at higher kinds. I'm particularly fond of functors between indexed families of sets.

type (s :: k -> *) :-> (t :: k -> *) = forall x. s x -> t x

class FunctorIx (f :: (i -> *) -> (j -> *)) where
  mapIx :: (s :-> t) -> (f s :-> f t)

ij重合时,问这样的f是单子是明智的.即使我们已经将* -> *留在了后面,通常的分类定义也足够了.

When i and j coincide, it becomes sensible to ask when such an f is a monad. The usual categorical definition suffices, even though we've left * -> * behind.

信息是这样的:关于成为类型类的任何事情都不会固有地引起有趣的分类结构;有很多有趣的分类结构,可以通过各种类型的类型类来有用地表示.从*(集合和函数)到* -> *(functor和自然变换),肯定有最有趣的函子.不要对Haskell的类别结构的丰富性粗心大意地谈论"Hask".

The message is this: nothing about being a typeclass inherently induces interesting categorical structure; there is plenty of interesting categorical structure which can usefully be presented via type classes over all manner of kinds. There are most certainly interesting functors from * (sets and functions) to * -> * (functors and natural transformations). Don't be blinded by careless talk about "Hask" to the richness of categorical structure in Haskell.

这篇关于Haskell中的所有类型类都具有类别理论类比吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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