Notes (CTFP 05/31): Products and Coproducts
5 Products and Coproducts
5.1 Initial Object
initial object: The initial object is the object that has one and only one morphism going to any object in the category.
This is like Void.
5.2 Terminal Object
terminal object: The terminal object is the object with one and only one morphism coming to it from any object in the category.
This is like ()
poset: A partially ordered set.
5.3 Duality
opposite category: For any category C, the opposite or dual category C^op is C with all the arrows reversed. If f and g are arrows in C, then
g . f = f^(op) . g^(op)
5.4 Isomorphisms
inverse: If for two objects A and B there are two arrows f :: A -> B and g :: A -> B, such that:
f . g = id_A
g . f = id_B
f and g are inverses, or isomorphisms, and A and B are said to be isomorphic.
Theorem: Any category has at most one initial object (up to isomorphism)
Proof:
Suppose a category C has two initial objects I1 and I2. Then by definition it has the identity arrows:
id_I1 :: I1 -> I1
id_I2 :: I2 -> I2
And because I1 and I2 are initial objects, there must be only one arrow from I1 to I2, and only one arrow from I2 to I1:
f :: I1 -> I2
g :: I2 -> I1
But if f and g are arrows in C then so are
(f . g) :: I2 -> I2
(g . f) :: I1 -> 11
And since an initial object has only one arrow to every other object in the category, including itself:
(f . g) = id_I2 :: I2 -> I2
(g . f) = id_I1 :: I1 -> 11
And therefore f and g are inverses, so I1 and I2 are isomorphic.
Furthermore, since I1 and I2 are initial objects, f and g are the only arrows between them, and therefore are unique isomorphism.
5.5 Products
product: A product of two objects X1 and X2 in a category C is an object X with two arrows
p1 :: X -> X1, p2 :: X -> X2
with the property that for all objects Y in C with arrows
f1 :: Y -> X1, f2 :: Y -> X2
there is a unique morphism f :: Y -> X such that
f . p1 = f1, f . p2 = f2
One way to think about this is whenever a product of X1 and X2 exists, all the objects that have arrows to X1 and X2 have arrows to the product object X. In other words, X is the most “downwind” or terminal-like object in the collection of objects that have morphisms to X1 and X2
5.6 Coproduct
coproduct: The dual of the product. Take the definition of the product and reverse the arrows. In other words, the coproduct X of X1 and X2 is the object that has arrows to every object that both X1 and X2 have arrows to. It is the most “upwind”, or initial-like object in the collection of objects that have arrows from both X1 and X2.
5.8 Challenges
See Theorem in 5.4
Okay, so the arrows in a poset represent the less-than-or-equal operator
<=, so for any object in the posetA, all objectsBsuch thatA <= Bhave an arrow fromBtoA.For the product of two objects
XandY, let’s consider all the objectsZ_nthat have arrows to bothXandY.So any two objects in
Z_n,Z1, Z2have arrows:Z1 -> X, Z1 -> Y Z2 -> X, Z2 -> YNow since the poset is a partial order, we cannot assume that there are arrows between any objects in
Z_n. (Nor can we assume there are arrowsX -> Y, orY <- X.) Because of this in some posetal categories, the product may be undefined for two objects, for example, in the category:Objects: X, Y, Z1, Z2 Arrows: Z1 -> X, Z1 -> Y, Z1 -> Z1 Z2 -> X, Z2 -> Y, Z2 -> Z2 X -> X Y -> Y Diagram: Z1 -> X | ^ v | Y <- Z2This satisfies the category properties of composition and identity (since arrows in the above category can only be composed with identity arrows) and the partial order properties, but there is no defined product of
YandXFor the product of
XandYto exist, there must be some elementZofZ_nsuch that all objectsZ_iinZ_nhave arrowsZ_i -> Z. In other words, if arrows in a posetal category represent “less-than-or-equal”, the product ofXandYis the greatest of all objects less than or equal toXandY, or the greatest lower bound ofXandY.Interestingly, if e.g.
X -> YthenXis inZ_nbecause ofX -> X. And by definition is the greatest lower bound (because allZ_ihaveZ_i -> X). So if there is an arrow betweenXandY, the product is simply the minimum of the two.The coproduct of two objects
XandYin a posetal category is the least upper bound ofXandY, that is, the smalllest object that is greater than or equal to both. It’s exactly like the product, but with the arrows reversed.I have no favorite languages other than Haskell.
So despite the C++ code, I think I can translate this problem into something
I can do:
i :: Int -> Int i x = x j :: Bool -> Int j x = if x then 1 else 0Eitherin Haskell is:Either a b = Left a | Right b Left :: a -> Either a b Right :: b -> Either a bEither Int Boolis a “better” coproduct ofIntandBoolthanIntif there is a unique arrowunleftfromEither Int BooltoIntsuch that:(unleft . Left) = id_IntThis function exists:
unleft (Left x) = x unleft _ = 0Note that while
(unleft . Left) = id_x,(Left . unleft) \= id_Either, becauseLeft (unleft (Right True) = Left 0. If both composition equalled identity, then the two types would be isomorphic, but they are not. Either Int Bool is epimorphic (surjective) on Int, and Int is monomorphic (injective) on Either Int Bool.One thing that confuses me though, is that it seems like
unleftisn’t unique, because we could replace whatever number gets produced from aRightwith anyInt… So I’m clearly missing something here.Oh I see! The morphism from
Either Int Bool -> Intis unique given two injections fromInt -> IntandBool -> Int. So the morphism shouldn’t be namedunleftat all, but actuallyembed, because it embeds an Either inIntspace.Let’s look at our category:
Objects: Int, Int, Bool, Either Int Bool Arrows: i :: Int -> Int i n = n j :: Bool -> Int j True = 1 j False = 0 Left :: Int -> Either Int Bool Left n = Left n Right :: Bool -> Either Int Bool Right b = Right b embed :: Either Int Bool -> Int embed (Left n) = i n embed (Right b) = j bembedis unique for anyiandjbecause it’s just applyingiandjbased on which side of theEitherwe’re on.And that’s how we get the
factorizerfrom the text:factorizer :: (a -> c) -> (b -> c) -> Either a b -> c factorizer i j (Left a) = i a factorizer i j (Right b) = j bid_Int = embed . Left, butid_Either \= Left . embed, because(Left . embed) (Right True) = Left 1So there is an
Either Int Bool -> Intarrow that factorizesInt -> Int but not anInt -> Either Int Boolthat factorizesEither Int Bool -> Either Int Bool`Another way to look at this is that if we try to treat
Intas anEither, we don’t know if any given integer is supposed to be aLeftor aRight.Like, if
jsendsTrue, Falseto1, 0andisends1, 0to1, 0we don’t know if applyingm :: Int -> Either Int Boolto1should be aLeft 1or aRight True.Our Category:
i :: Int -> Int i n | n < 0 = n | otherwise = n + 2 j :: Bool -> Int j b = if b then 1 else 0 Left :: Int -> Either Int Bool Right :: Bool -> Either Int BoolSuppose Int is a “better” coproduct of Int and Bool with injections
iandjthanEither Int Bool. Then there must be a unique morphismm :: Int -> Either Int Boolsuch that:m . i = Left m . j = RightIf on the other hand there is another morphism
m' :: Int -> Either Int Bool,m /= m'such thatm' . i = Left m' . j = RightThen
mwould not be a unique factorizing morphism and thereforeIntcould not be a better coproduct ofIntandBoolthanEither Int BoolHere’s a candidate for
m:m :: Int -> Either Int Bool m n | n == 0 = Right False | n == 1 = Right True | n < 0 = Left n | otherwise = Left (n - 2)This looks pretty unique at first glance.
However.
We can show that there is a unique morphism
f = (factorize i j) :: Either Int Bool -> Int:factorize :: (Int -> Int) -> (Bool -> Int) -> Either Int Bool -> Int factorize i j (Left n) = i n factorize i j (Right b) = j bsuch that
f . Left = i f . Right = jSo if both
mandfare unique morphisms, thenf . Left = f . (m . i) = (f . m) . i = i f . Right = f . (m . j) = (f . m) . j = j => (f . m) = id_Int m . i = m . (f . Left) = (m . f) . Left = Left m . j = m . (f . Right) = (m . f) . Right = Right => (m . f) = id_EitherIntBoolwhich implies that
mandfare isomorphisms. And sincemandfare unique morphisms that fit the above conditions,mandfare unique isomorphisms. Which meansEither Int BoolandIntare uniquely isomorphic, and are equivalent up to unique isomorphism. In which caseIntcannot be a better coproduct thatEither Int Bool.On the other hand, if
mis not unique, there must be anotherm' :: Int -> Either Int Bool, in which caseIntdoes not satisfy the universal property of coproducts.Actually, I think this is a general argument which applies to any possible candidate coproduct. If we already know that there is a coproduct in the category that satisfies the universal property, then any other other coproduct that satisfies the property must be uniquely isomorphic to the first, since their isomorphisms uniquely factorize their injections.
Wow. That’s a mathy paragraph. I begin to see why math explanations sound as impenetrable as they usually do…
()is an inferior coproduct ofIntandBoolthanEither Int Bool, because there is only one unique morphismunit :: Either Int Bool -> (), but no morphisms from() -> Either Int Boolthat factorize injections fromunit :: Int -> ()andunit :: Bool -> ().But the question is asking for an inferior candidate that admits multiple morphisms between it and Either Int Bool.
Intis an inferior coproduct of Int and Bool. Suppose we have the morphisms:Left :: Int -> Either Int Bool Right :: Bool -> Either Int Bool p :: Int -> Int p = (+3) q :: Bool -> Int q True = 1 q False = 0Is there a unique morphism
m :: Int -> Either Int Boolsuch that
m . p = Left m . q = RightNo! Because
pandqdon’t map any values to2 :: Int, we can havem :: Int -> Either Int Bool m 0 = Right False m 1 = Right True m 2 = (a :: Either Int Bool) m n = n - 3We can return any
Either Int Boolform 2andmwill still factorize, so we actually have infinite morphisms that satisfy the property. Therefore, Int is a very bad coproduct for Either Int Bool.On the other hand, the fact that we can use
Intas a coproduct at all (even an inferior one) is convenient, because it lets use model coproducts (and other GADTs) on computers that only know about discrete quantites.