[英]“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.这个问题有两个微妙的错误。
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:假设我提出了以下数学结构:
Is this structure a category?这个结构是一个类别吗? As you correctly observed in your question, the answer is yes , because:
正如您在问题中正确观察到的那样,答案是yes ,因为:
Numbers with addition带加法的数字
Here is a somewhat simpler structure:这是一个稍微简单的结构:
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:在这种情况下,答案是肯定的,我们可以将其称为组合(并将整个结构称为类别),因为:
Positive numbers with addition带加法的正数
How about this slight modification to the previous example:对上一个示例的这个轻微修改如何:
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:另一种结构:
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.