简体   繁体   English

Haskell wiki 中的“类别法则”

[英]“Category laws” in Haskell wiki

According to Haskell wiki,根据 Haskell 维基,

https://en.wikibooks.org/wiki/Haskell/Category_theory#Category_laws https://en.wikibooks.org/wiki/Haskell/Category_theory#Category_laws

Category laws There are three laws that categories need to follow.品类法则品类需要遵循三个法则。 Firstly, and most simply, the composition of morphisms needs to be associative.首先,也是最简单的,态射的组合需要是结合的。

However,然而,

Composition of relations is associative .关系的组合是结合的

https://en.wikipedia.org/wiki/Composition_of_relations#Properties https://en.wikipedia.org/wiki/Composition_of_relations#Properties

the composition of functions is always associative .函数的组合总是关联的

https://en.wikipedia.org/wiki/Function_composition#Properties https://en.wikipedia.org/wiki/Function_composition#Properties

So, in what scenario, Haskell community (or ones who wiki suppose) consider that the composition of morphisms is not associative breaking the rule?那么,在什么情况下,Haskell 社区(或 wiki 假设的人)认为态射的组合不是结合违反规则的?

Thanks.谢谢。

Here's a datatype/operation combination that is not a valid Category instance.这是一个不是有效Category实例的数据类型/操作组合。

The datatype consists simply in a function annotated with some Int value:数据类型简单地包含在一个用一些Int值注释的函数中:

import Prelude
import qualified Control.Category as C

data Subs a b = Subs Int (a -> b)

And here's the bogus Category instance.这是伪造的Category实例。 The composition performs subtraction of the annotations:组合执行注释的减法:

instance C.Category Subs where
    id = Subs 0 Prelude.id
    (Subs x f) . (Subs y g) = Subs (y - x) (f . g)

However, because subtraction is not associative , that instance is not valid!但是,因为减法不是关联的,所以该实例无效!

main :: IO ()
main = do
    let Subs u _ = (Subs 3 id) C.. ((Subs 10 id) C.. (Subs 2 id))
        Subs v _ = ((Subs 3 id) C.. (Subs 10 id)) C.. (Subs 2 id)
    print u
    print v

This returns这返回

-11
-5

showing that the order of composition matters, contrary to the Category laws.表明组合顺序很重要,这违反了Category法。

You correctly identify function composition and relation composition as associative operations, and then appear to ask this question:您正确地将函数组合和关系组合识别为关联操作,然后似乎在问这个问题:

Since all the operations that we call composition are already proven to be associative, why do we make associativity a requirement of composition operations?既然我们称之为组合的所有操作都已经被证明是关联的,为什么我们要让关联性成为组合操作的要求呢?

There are two subtle bugs in this question.这个问题有两个微妙的错误。

  1. You assume that the collection of operations that we call composition only contains function and relation composition.您假设我们称为组合的操作集合仅包含函数和关系组合。 But this is not so: I think I could, given time, write down 20-30 different classes of composition operations, with each class containing an infinite number of actual composition operations!但事实并非如此:我想,给定时间,我可以写下 20-30 种不同的合成操作,每个类都包含无限数量的实际合成操作!
  2. Your assumed causal relation is backwards.你假设的因果关系是倒退的。 We do not start by calling a bunch of different things composition, and then deciding to demand that they be associative;我们不是首先将一堆不同的东西组合起来,然后决定要求它们具有关联性; rather, first we identify some conditions that we like (associativity, having an identity, being well-typed) and then allow ourselves to attach the label "composition" to any operation that fulfills those conditions.相反,首先我们确定一些我们喜欢的条件(结合性、具有身份、类型良好),然后允许我们将标签“组合”附加到满足这些条件的任何操作上。

Let's do some examples, in the style of point (2).让我们按照第(2)点的风格来做一些例子。 We will first build up a mathematical structure of interest to us, then we will ask: can we call this a category if we want to?我们将首先建立一个我们感兴趣的数学结构,然后我们会问:如果我们愿意,我们可以称它为一个范畴吗?

Sets with function composition具有函数组合的集合

Suppose I proposed the following mathematical structure:假设我提出了以下数学结构:

  • There is a big collection of objects, one for each set that's possible.有一大堆对象,每个可能的集合一个。 (This statement can't be formulated in set theory! But that's okay. We don't have to work in set theory if we don't want to.) (这种说法不能制定一套理论!不过没关系,我们不必在,如果我们不想集合论的工作。)
  • There is a collection of arrows for each pair of objects;每对对象都有一组箭头; namely, for sets X and Y, there is one arrow of type X -> Y for each function whose domain is X and whose codomain is Y.即,对于集合 X 和 Y,对于定义域为 X 且余域为 Y 的每个函数,都有一个类型为 X -> Y 的箭头。
  • To compose two arrows, we use standard function composition from set theory.为了组合两个箭头,我们使用集合论中的标准函数组合。

Is this structure a category?这个结构是一个类别吗? As you correctly observed in your question, the answer is yes , because:正如您在问题中正确观察到的那样,答案是yes ,因为:

  1. For any object X, we can create an arrow of type X -> X for which composing with other arrows does nothing (namely, the function that returns its input unchanged).对于任何对象 X,我们可以创建一个类型为 X -> X 的箭头,与其他箭头组合对其没有任何作用(即返回其输入不变的函数)。
  2. As you correctly observed, we can prove that function composition is associative.正如您正确观察到的,我们可以证明函数组合是关联的。

Numbers with addition带加法的数字

Here is a somewhat simpler structure:这是一个稍微简单的结构:

  • There is a single object, named X.有一个对象,名为 X。
  • There is a collection of arrows of type X -> X;有一组 X -> X 类型的箭头; in this collection is one arrow for each natural number.在这个集合中,每个自然数都有一个箭头。
  • To compose the arrows representing numbers m and n, produce the arrow representing the number m+n.要组合代表数字 m 和 n 的箭头,请生成代表数字 m+n 的箭头。

Notice that in this structure, the composition operation we have defined is neither function composition nor relation composition!请注意,在这个结构中,我们定义的组合操作既不是函数组合也不是关系组合! The question we are asking now is: are we lying to ourselves when we attach the label composition to this operation, or is that a sensible thing to call it?我们现在要问的问题是:当我们将标签组合附加到这个操作时,我们是在自欺欺人,还是这样称呼它是明智的?

In this case, the answer is yes , we can call it composition (and call the whole structure a category), because:在这种情况下,答案是肯定的,我们可以将其称为组合(并将整个结构称为类别),因为:

  1. We can create an arrow of type X -> X for which composing with other arrows does nothing (namely, the arrow representing the number 0).我们可以创建一个类型为 X -> X 的箭头,它与其他箭头组合起来没有任何作用(即代表数字 0 的箭头)。
  2. As we all know, addition is associative.众所周知,加法是结合的。

Positive numbers with addition带加法的正数

How about this slight modification to the previous example:对上一个示例的这个轻微修改如何:

  • There is a single object, named X.有一个对象,名为 X。
  • There is a collection of arrows of type X -> X;有一组 X -> X 类型的箭头; in this collection is one arrow for each positive natural number.在这个集合中,每个正自然数都有一个箭头。
  • To compose the arrows representing numbers m and n, produce the arrow representing the number m+n.要组合代表数字 m 和 n 的箭头,请生成代表数字 m+n 的箭头。

Can I call this a category?我可以称之为一个类别吗? Is this relation something to which I can attach the label "composition"?这种关系是我可以附加标签“组合”的东西吗? (Note that the operation itself is the same operation as before!) In this case, the answer is no , we should not call this structure a category, and we should not call this operation a composition, because although the operation is associative, there is no arrow which leaves other arrows unchanged when composing with them. (注意,操作本身和之前的操作是一样的!)在这种情况下,答案是否定的,我们不应该把这个结构称为范畴,也不应该把这个操作称为组合,因为虽然操作是关联的,但没有箭头会在与其他箭头组合时使其他箭头保持不变。

Numbers with weird exponentiation具有奇怪指数的数字

One more structure:另一种结构:

  • There is a single object, named X.有一个对象,名为 X。
  • There is a collection of arrows of type X -> X;有一组 X -> X 类型的箭头; in this collection is one arrow for each natural number.在这个集合中,每个自然数都有一个箭头。
  • To compose arrows m and n:组合箭头 m 和 n:
    • If either of m or n is 0, return the other.如果 m 或 n 中的一个为 0,则返回另一个。 For example, the composition of 0 and 1 is 1;比如0和1的组合是1; the composition of 2 and 0 is 2; 2和0的合成为2; and the composition of 0 and 0 is 0.而0和0的合成为0。
    • Otherwise, take the exponentiation m^n.否则,取幂 m^n。

Can we call this structure a category?我们可以将这种结构称为类别吗? Is the final operation something we could label as a "composition"?我们可以将最终操作标记为“组合”吗? In this case, the answer is no , because although there is an arrow which leaves the others alone under composition (namely 0), the purported composition operation is not associative:在这种情况下,答案是否定的,因为虽然有一个箭头将其他人单独留在组合下(即 0),但声称的组合操作不是关联的:

2^(1^2) = 2^1 = 2
(2^1)^2 = 2^2 = 4

With some work, we can cook up fancier examples that fail the category laws in even subtler ways (for example, by being associative, but only having one-sided identities).通过一些工作,我们可以制作出更巧妙的例子,以更微妙的方式(例如,通过关联,但只有片面的身份)不符合类别定律。 But by now I hope the pattern is clear: the category laws are desiderata that we use, and the decision we make in each case is about whether a mathematical structure we are interested in is something we can call a "category".但现在我希望模式是明确的:类别定律是我们所需要的,我们在每种情况下做出的决定都是关于我们感兴趣的数学结构是否是我们可以称之为“类别”的东西。 (Of course, we are still allowed to be interested in it and study it even if the answer to that question is no!) (当然,即使这个问题的答案是否定的,我们仍然可以对它感兴趣并研究它!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM