# Product and coproduct

A **product** $A \times B$ is an object in a category that is equipped with two morphisms, it's projections, $p$ and $q$. This corresponds to the Cartesian product of the two objects $A$ and $B$ in the category $Set$.

The coproduct $A + B$ is an object in a category that is equipped with two morphisms, it's injections, $i$ and $j$. This corresponds to the disjoint union of the two objects $A$ and $B$ in the category $Set$. Technically the elements of $A$ and $B$ are tagged in order to signify their origin, and the union of the tagged versions of the sets is taken:

$A + B = (A \times \{0\}) \cup (B \times \{1\})$For a pair of objects $A$ and $B$ of a category $C$ we define a **wedge to** the pair $A, B$ as an object $X$ equipped with two morphisms $X \rightarrow A, B$. We also define a **wedge from** the pair $A, B$ is an object $X$ equipped with two morphisms $A, B \rightarrow X$:

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 $A, B$ in a category $C$ the **product** and **coproduct** of the pair is a wedge with the following universal property that for each wedge there is a unique morphism $X \xrightarrow {m} S$ for the product and $S \xrightarrow {m} X$ for the coproduct. This **mediator** for each wedge (which is unique for each wedge on $X$) means that the two respective wedges commute:

**Lemma 1**

Given the product and coproduct wedges in the category $C$ and an endomorphism $S \xrightarrow {k} S$ then it holds that $k = id_S$. This verifies the uniqueness of the universal solution.

**Lemma 2**

For objects $A$ and $B$ in a category $C$, a pair of product and coproduct wedges then $P, Q$ and $I, J$ are uniquely isomorphic over the wedges. That is, the unique morphisms $f, g$ between $P, Q$ are an inverse pair of isomorphisms, and similarly for the isomorphisms $i, j$ between $I, J$. 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 $Hask$ and pick the best possible type.

**Product example**

If we have the types $Int$, $(Int, Bool)$, and $(Int, Int, Bool)$ and two functions (projections) from each type ($Int \xrightarrow {p} Int$ and $Int \xrightarrow {q} Bool$), then the product tells us that all three are isomorphic, and the best candidate is the pair $(Int, Bool)$. That is, there is a unique morphism $m$ from any candidate $c$ to the Cartesian product $(a, b)$. 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 $c$ with the two injections $a \xrightarrow {i} c$ and $b \xrightarrow {j} c$, we say that object $c$ is better than some other object $c'$ equipped with the morphisms $a \xrightarrow {i'} c'$ and $b \xrightarrow {j'} c'$, if there is a morphism $c \xrightarrow {m} c'$ that factorizes the injections.

In this example we use the sum type $Contact$ with the two injections $Phone$ and $Email$. In Haskell:

In Haskell, the coproduct is implemented as a datatype called $Either$ that is parameterized by two types $a$ and $b$, and has the two type constructors $Left$ and $Right$.

So given that $Either$ and $Contact$ are isomorphic with the morphism $m$:

This means that given a candidate type $c$ and two injections $i$ and $j$ the factorizer for $Either$ is implemented as:

In Haskell: