Product and coproduct
A product is an object in a category that is equipped with two morphisms, it's projections, and . This corresponds to the Cartesian product of the two objects and in the category .
The coproduct is an object in a category that is equipped with two morphisms, it's injections, and . This corresponds to the disjoint union of the two objects and in the category . Technically the elements of and are tagged in order to signify their origin, and the union of the tagged versions of the sets is taken:
For a pair of objects and of a category we define a wedge to the pair as an object equipped with two morphisms . We also define a wedge from the pair is an object equipped with two morphisms :
For a given pair there may be many such wedges, on one side or the other. Our goal is to look for a best possible wedge, or technically a universal wedge. This means that for a given pair of objects in a category the product and coproduct of the pair is a wedge with the following universal property that for each wedge there is a unique morphism for the product and for the coproduct. This mediator for each wedge (which is unique for each wedge on ) means that the two respective wedges commute:
Lemma 1
Given the product and coproduct wedges in the category and an endomorphism then it holds that . This verifies the uniqueness of the universal solution.
Lemma 2
For objects and in a category , a pair of product and coproduct wedges then and are uniquely isomorphic over the wedges. That is, the unique morphisms between are an inverse pair of isomorphisms, and similarly for the isomorphisms between . This means that if a pair of objects has a product or coproduct then it is essentially unique, and which leads to us speaking of the product or coproduct of a pair of objects.
The existence of products and coproducts varies from category to category.
What does this mean in programming?
In very general terms it means we can take various types in the category and pick the best possible type.
Product example
If we have the types , , and and two functions (projections) from each type ( and ), then the product tells us that all three are isomorphic, and the best candidate is the pair . That is, there is a unique morphism from any candidate to the Cartesian product . It simply combines the two projections into a pair. In Haskell:
This morphism can also be produced by a factorizing function, again in Haskell:
Coproduct example
The coproduct is the reverse of the product, the projections become injections so given an object with the two injections and , we say that object is better than some other object equipped with the morphisms and , if there is a morphism that factorizes the injections.
In this example we use the sum type with the two injections and . In Haskell:
In Haskell, the coproduct is implemented as a datatype called that is parameterized by two types and , and has the two type constructors and .
So given that and are isomorphic with the morphism :
This means that given a candidate type and two injections and the factorizer for is implemented as:
In Haskell: