Objects and morphisms

Equality

The important question is how we can decide whether an object/morphism is equal to another object/morphism. The trivial answer is possible if the Object is a Set. In the case we can say that 2 objects are equal if they contain the equivalent collection of elements. Unfortunately we cannot do the same trick for categorical Objects as soon as they don’t have any internal structure, but can use a Categorical approach (see Categorical approach) i.e. if we cannot use a “microscope” let’s use a “telescope” and define the equality of objects and morphisms of a category \(\mathbf{C}\) in terms of the whole \(\mathrm{hom}(\mathbf{C})\).

Two Objects \(a\) and \(b\) in Category \(\mathbf{C}\) are equal if they are the same element of the Class \(\mathrm{ob}(\mathbf{C})\).

They are isomorphic if there exists a Isomorphism \(a \cong_f b\). This also means that there exists an isomorphism \(b \cong_g a\). These two Morphisms (\(f\) and \(g\)) are related to each other via the following equations: \(g \circ f = \mathbf{1}_{a \to a}\) and \(f \circ g = \mathbf{1}_{b \to b}\).

If there exists a unique isomorphism between \(a\) and \(b\), then \(a\) and \(b\) are canonically isomorphic. In this case the objects can be treated as the same object for categorical purposes, although they are not necessarily equal as objects of \(\mathbf{C}\).

Unlike Functions between Sets we don’t have any additional info 1 about Morphisms except category theory axioms which the morphisms satisfy ((https://math.stackexchange.com/users/142355/david-myers), n.d.). This leads us to the following definition of morphism equality:

Let \(f,g: a \to b\) be two Morphisms in Category \(\mathbf{C}\). They are equal if they are the same morphism from \(a\) to \(b\). If \(\mathbf{C}\) is a Locally small category, then this means that they are the same element of the Homset \(\mathrm{hom}_{\mathbf{C}}\left(a, b\right)\).

In a concrete proof the equality can be derived from the base axioms:

or Commutative diagrams that postulate the equality.

As an example let’s prove the following theorem.

The Identity morphism is unique.

Proof. Consider an Object \(a\) and its Identity morphism \(\mathbf{1}_{a \to a}\). Assume existence of a function \(f: a \to a\) such that \(f\) is also identity. (Identity morphism), for \(f\) as identity, gives us \[f \circ \mathbf{1}_{a \to a} = \mathbf{1}_{a \to a}.\] On the other hand (Identity morphism) for \(\mathbf{1}_{a \to a}\) is satisfied: \[f \circ \mathbf{1}_{a \to a} = f\] i.e. \[f = f \circ \mathbf{1}_{a \to a} = \mathbf{1}_{a \to a}\] or \(f = \mathbf{1}_{a \to a}\). ◻

Initial and terminal objects

Initial object

Let \(\mathbf{C}\) be a Category. The Object \(i \in \mathrm{ob}(\mathbf{C})\) is called initial object if \(\forall x \in \mathrm{ob}(\mathbf{C}) \exists! f_x: i \to x \in \mathrm{hom}(\mathbf{C})\).

Note that there is only one function from the empty set to any other set (ProofWiki 2018) that makes the empty set the Initial object in Set category.

Let \(\mathbf{C}\) be a category and let \(i, i' \in \mathrm{ob}(\mathbf{C})\) be two Initial objects. Then there exists a unique Isomorphism \(u: i \to i'\) (see Objects equality and canonical isomorphism).

Proof. Consider the following Commutative diagram (see Initial object is unique). Since \(i\) is an initial object, \(\exists! \, u: i \to i'\). On the other hand \(i'\) is also an initial object and therefore \(\exists! \, u^{-1}: i' \to i\). Combining them together via composition we can get \(u^{-1} \circ u: i \to i\) and \(u \circ u^{-1}: i' \to i'\). From the fact that \(i\) is initial object one can get that there exists only one morphism \(\mathbf{1}_{i \to i}: i \to i\). The same is true for \(i'\). Therefore \(u^{-1} \circ u = \mathbf{1}_{i \to i}\) and \(u \circ u^{-1} = \mathbf{1}_{i' \to i'}\). This completes the commutative diagram and finishes the proof.

Commutative diagram for initial object uniqueness proof

 ◻

Terminal object

Let \(\mathbf{C}\) is a Category, the Object \(t \in \mathrm{ob}(\mathbf{C})\) is called terminal object if \(\forall x \in \mathrm{ob}(\mathbf{C}) \exists! g_x: x \to t \in \mathrm{hom}(\mathbf{C})\).

Terminal object in Set category is a set with one element i.e Singleton.

As you can see the initial and terminal objects are opposite each other. I.e. if \(i\) is an Initial object in \(\mathbf{C}\) then it will be Terminal object in the Opposite category \(\mathbf{C^{op}}\).

Let \(\mathbf{C}\) is a category and \(t, t' \in \mathrm{ob}(\mathbf{C})\) two Terminal objects then there exists an unique Isomorphism \(v: t' \to t\) (see Objects equality and canonical isomorphism)

Proof. Just got to the Opposite category and revert Arrows in Initial object is unique. The result shown on Terminal object is unique and it proofs the theorem statement.

Commutative diagram for terminal object uniqueness proof

 ◻

Toy example

In our toy example Programming languages examples the type String is Initial object and type Bool is the Terminal object.

Product and sum

Product

The pair of 2 objects is defined via the Universal property in the following way:

Let we have a category \(\mathbf{C}\) and \(c_1, c_2 \in \mathrm{ob}(\mathbf{C})\) -two Objects then the product of the objects \(c_1, c_2\) is another object in \(\mathbf{C}\) \(c = c_1 \times c_2\) with 2 Morphisms \(\pi_1: c \to c_1, \pi_2: c \to c_2\) such that the following universal property is satisfied: \(\forall c' \in \mathrm{ob}(\mathbf{C})\) and morphisms \(\pi'_1: c' \to c_1, \pi'_2: c' \to c_2\), exists unique morphism \(h\) such that the following diagram (see Product) commutes, i.e. \[\begin{aligned} \pi'_1 = \pi_1 \circ h, \nonumber \\ \pi'_2 = \pi_2 \circ h. \end{aligned}\]

Product \(c = c_1 \times c_2\). \(\forall c', \exists! h \in \mathrm{hom}(\mathbf{C}): \pi'_1 = \pi_1 \circ h, \pi'_2 = \pi_2 \circ h\).

In other words \(h\) factorizes \(\pi'_{1,2}\).

Cartesian product: \(C = A \times B = \{(a,b)| a \in A, b \in B\}\) is the Product of two sets \(A\) and \(B\) in Set category. We have only one option for \(\pi_{1,2}\): \[\begin{aligned} \pi_1 : (a,b) \to a \in A, \nonumber \\ \pi_2 : (a,b) \to b \in B. \nonumber \end{aligned}\] Assume that \(A\) and \(B\) are non-empty and at least one of them has more than one element. Consider also another candidate: \(C' = A \times A \times B \times B = \{(a_1,a_2,b_1, b_2)| a_{1,2} \in A, b_{1,2} \in B\}\). There are different options for \(\pi'_1\) and \(\pi'_2\). Lets choose the following ones: \[\begin{aligned} \pi'_1 : (a_1,a_2,b_1, b_2) \to a_1 \in A, \nonumber \\ \pi'_2 : (a_1,a_2,b_1, b_2) \to b_2 \in B. \nonumber \end{aligned}\] We have only one morphism \(h\) that satisfied conditions (Product): \[h: (a_1,a_2,b_1, b_2) \to (a_1, b_2) \in A \times B\] that is accordingly with the Product definition for \(C = A \times B\).

If \(C'\) had been the Product then it would have satisfied the following factorization conditions: \[\begin{aligned} \pi_1 = \pi'_1 \circ h', \nonumber \\ \pi_2 = \pi'_2 \circ h', \end{aligned}\] where \(h'\) would have been an unique morphism. From other side there are a lot of morphisms \(h'\) which factorize \(\pi_{1,2}\) accordingly (Product): \[h' : (a,b) \to (a, \bar{a}, \bar{b}, b),\] where \(\bar{a}\) can be replaced with any element from \(A\) and \(\bar{b}\) can be replaced with any element of \(B\). Therefore \(C'\) can not be considered as the Product of \(A\) and \(B\).

The Product of objects will provide also a definition for product of morphisms

Let \(\mathbf{C}\) is a category and \(a,a' \in \mathrm{ob}(\mathbf{C})\) and \(b, b' \in \mathrm{ob}(\mathbf{C})\) are 2 pairs of Objects that admit Product. Consider 2 morphisms that connects the objects: \(f: a \to b, f': a' \to b'\) then we can create a new unique morphism that connects the products: \(f \times f': a \times a' \to b \times b'\) and makes the diagram commute (see Product of morphisms).

Product of morphisms.

Sum

If we invert Arrows in Product we shall got another object definition that is called sum

Let we have a category \(\mathbf{C}\) and \(c_1, c_2 \in \mathrm{ob}(\mathbf{C})\) -two Objects then the sum of the objects \(c_1, c_2\) is another object in \(\mathbf{C}\) \(c = c_1 \oplus c_2\) with 2 Morphisms \(i_1: c_1 \to c, i_2: c_2 \to c\) such that the following Universal property is satisfied: \(\forall c' \in \mathrm{ob}(\mathbf{C})\) and morphisms \(i'_1: c_1 \to c', i'_2: c_2 \to c'\), exists unique morphism \(h\) such that the following diagram (see Sum) commutes, i.e. \(i'_1 = h \circ i_1, i'_2 = h \circ i_2\).

Sum \(c = c_1 \oplus c_2\). \(\forall c', \exists! h \in \mathrm{hom}(\mathbf{C}): i'_1 = h \circ i_1, i'_2 = h \circ i_2\).

In other words \(h\) factorizes \(i'_{1,2}\).

The categorical sum is also called as coproduct.

Let \(\{A_i: i \in I\}\) be a family of sets indexed by \(I\). The disjoint union (Wikipedia 2017) of this family is the set \[\sqcup_{i \in I} A_i = \cup_{i \in I}\left\{ \left(x, i\right): x \in A_i \right\}.\] The elements of the disjoint union are ordered pairs \((x, i)\). Here \(i\) serves as an auxiliary index that indicates which \(A_i\) the element \(x\) came from.

Disjoint union is the Sum of two sets \(A\) and \(B\) in Set category.

In the book we shall use \(\oplus\) as the sign for the categorical Sum. The Disjoint union sign \(\sqcup\) is also used 2 as the sign for categorical sum (Wikipedia contributors 2019).

Category as a monoid

Consider the following definition from abstract algebra

The set of elements \(M\) with defined binary operation \(\circ\) we shall call as a monoid if the following conditions are satisfied.

  1. Closure: \(\forall a, b \in M\): \(a \circ b \in M\)

  2. Associativity: \(\forall a, b, c \in M:\) \[a \circ \left( b \circ c \right) = \left( a \circ b \right) \circ c\]

  3. Identity element: \(\exists e \in M\) such that \(\forall a \in M\): \[e \circ a = a \circ e = a\]

Monoid concept is widely spread in math. Especially integer numbers form a monoid under summation operation. They also form another monoid under multiplication operation. The element 0 is used as identity in summation and 1 is used as the identity in multiplication.

There is a declaration of Monoid in modern \(\mathbf{Hask}\):

class Semigroup m => Monoid m where
    mempty :: m
    mappend :: m -> m -> m
    mappend = (<>)
    mconcat :: [m] -> m
    mconcat = foldr mappend mempty

The binary operation is inherited from Semigroup and written as (<>), while mappend is the compatibility name for the same operation. The identity is mempty. As it was mentioned in the Monoid definition (see Monoid), the binary operation should satisfy the associativity (Monoid) and identity element (Monoid) properties. This is a responsibility of a particular implementation to satisfy the properties. For instance the standard list implementation satisfies them:

instance Monoid [a] where
    mappend = (++)
    mempty = []

The given definition of monoid is based on its internal structure i. e. there is a Non-categorical approach. In Categorical definition for monoid we shall continue the Monoid concept investigation and will give a Categorical approach of the concept. You can also find there some notes about the concept importance in different areas such as programming languages and math (see Monoid importance).

We can consider 2 Monoids. The first one has Product as the binary operation and Terminal object as the identity element. As result we just got an analog of multiplication in the category theory. This is why the terminal object is often denoted as \(\mathbf{1}\) and the operation is called as the product.

Another one is additional Monoid that has Initial object as the identity element and the Sum as the binary operation. The initial object in that case is often denoted as \(\mathbf{0}\). I.e. we can see a direct connection with addition in algebra.

If we do such consideration then we can make a step forward and look at the distributive law that sum and multiplication satisfy.

A category \(\mathbf{C}\) is distributive if (Wikipedia contributors 2018) it has finite Products and Sums such that \(\forall a,b,c \in \mathrm{ob}(\mathbf{C})\) the canonical morphism \[\left[\mathbf{1}_{a \to a} \times i_b, \mathbf{1}_{a \to a} \times i_c\right]: (a \times b) \oplus (a \times c) \to a \times (b \oplus c)\] is an Isomorphism, where \(i_b: b \to b \oplus c\) and \(i_c: c \to b \oplus c\) are the sum injections. Also the unique morphism \[\mathbf{0} \to a \times \mathbf{0}\] is an Isomorphism, where \(\mathbf{0}\) is the Initial object.

Set category is an example (Wikipedia contributors 2018) of Distributive category

From other hand not all categories which have both product and sum are distributive. One of such example is a category of all groups \(\mathbf{Grp}\) (Wikipedia contributors 2018) where groups are considered as objects and group homomorphisms as morphisms.

Exponential

We are going to talk about functions (aka morphisms) as Objects.

Definition and examples

Consider 2 sets \(A\) and \(B\) then the set of functions between the 2 sets forms a new set that is called as Homset and denoted as \(\mathrm{hom}\left(A, B\right)\). Thus if \(A,B \in \mathrm{ob}(\mathbf{Set})\) then \(\mathrm{hom}\left(A, B\right) \in \mathrm{ob}(\mathbf{Set})\).

The construction of Homset is applied to the Set category but not to an arbitrary category because the Homset is a Set and therefore the object in the Set category. I.e. if \(\mathbf{C}\) is a Locally small category and \(a, b \in \mathrm{ob}(\mathbf{C})\) then the Homset \(\mathrm{hom}\left(a, b\right) \in \mathrm{ob}(\mathbf{Set})\) but for an arbitrary category this collection does not have to be a set. Thus we now want to construct something like to the Homset but that is an object in \(\mathbf{C}\). This will be called as the function object. we shall use the universal construction (Universal property) for the object definition.

Exponential object

Let \(\mathbf{C}\) is a category and \(x, y \in \mathrm{ob}(\mathbf{C})\). We also assume that \(\mathbf{C}\) allows all Products with \(x\), i.e. \(\forall y' \in \mathrm{ob}(\mathbf{C}), \exists y' \times x\). An object \(y^x\) together with a Morphism \(e: y^x \times x \to y\) is an exponential object if for every \(y' \in \mathrm{ob}(\mathbf{C})\) and every Morphism \(e': y' \times x \to y\) there exists a unique morphism \(h: y' \to y^x\) such that the Commutative diagram shown in Exponential commutes: \[e' = e \circ \left(h \times \mathbf{1}_{x \to x} \right)\]

Lets look at the Exponential in \(\mathbf{Set}\). We want to show that the object corresponds to the function. Really if we want to define a function \(f: X \to Y\) then we should look at the Homset \(F = \mathrm{hom}\left(X, Y\right)\). \(f \in F\) - is an element of the Homset. For the function application we have to take the argument \(x \in X\) and the function we want to apply \(f \in F\). Then we construct the pair \((f,x) \in F \times X\). For the function application we have to call a Morphism \(e: F \times X \to Y\). 3 I.e. the application \(e(f, x)\) gives us \(e(f, x) = y \in Y\) - the function value.

The notation is used for “morphisms (functions) as objects” in the category theory has an explanation provided in the following remark.

The Homset \(\mathrm{hom}\left(X, Y\right)\) is often denoted as \(Y^X\). Why the strange notation is used? Lets \(X\) is a Singleton i.e. its Cardinality is \(1\): \(\left|X\right| = 1\). The set \(Y\) has only 2 elements, i.e. its Cardinality is \(2\): \(\left|Y\right|= 2\).

Consider a function \(f: X \to Y\). How many such functions do we have? There are really 2 functions (see Exponential notation). One of them \(f_1\) return the first element from \(Y\) (\(y_1\)) and the other \(f_2\) returns the second one (\(y_2\)). The number of functions can be written as \(2^1\). I.e. one can write for Cardinality of a set of all functions between \(X\) and \(Y\) as follows \[\left|Y^X\right| = \left|Y\right|^{\left|X\right|}.\]

\(\mathrm{hom}\left(X, Y\right)\) consists of 2 elements: \(\{f_1, f_2\}\). Thus the cardinality of the homset is \(2\)

Otherwise if we consider a function \(g: Y \to X\) then we have only one possible choice for it (see Exponential notation) : just return the only possible element from \(X\). I.e. \(\left|X^Y\right| = 1\) that correlates with (Exponential notation).

\(\mathrm{hom}\left(Y, X\right)\) consists of 1 element: \(\{g\}\). Thus the cardinality of the homset is \(1\)

Currying in \(\mathbf{Set}\)

In the section we shall be in the Set category. The definition of Exponential object is closely related to notion of currying that is defined for Sets as follows.

Consider a function of 2 arguments: \[f:(X\times Y) \to Z\] that maps a pair (Product) of 2 sets \(X\) and \(Y\) into another set \(Z\). The currying constructs a new function \[h : X \to (Y \to Z)\] such that the following equation holds \[h(x)(y) = f(x,y),\] where \(x \in X, y \in Y\). The currying will be denoted as \(h = curry(f)\).

If we consider \(Y^X\) is a set of functions \(f : X \to Y\) then the currying is the Bijection \[Y^{X_1 \times X_2} \cong \left(Y^{X_2}\right)^{X_1}.\]

If we look in Exponential then we can notice that \[curry(e): y^x \to (x \to y),\] i.e. currying is used to construct a morphisms from exponential object.

Cartesian closed category

If a category \(\mathbf{C}\) satisfies the following conditions then it is called Cartesian closed category

  1. It has Terminal object

  2. \(\forall a,b \in \mathrm{ob}(\mathbf{C})\) exists Product \(a \times b \in \mathrm{ob}(\mathbf{C})\).

  3. \(\forall a,b \in \mathrm{ob}(\mathbf{C})\) exists Exponential \(a^b \in \mathrm{ob}(\mathbf{C})\)

If \(\mathbf{C}\) is a Cartesian closed category with finite Sums then it is a Distributive category.

Proof. Let \(a,b,c,d \in \mathrm{ob}(\mathbf{C})\). We will use the universal property of the Exponential object in the following form for every \(y \in \mathrm{ob}(\mathbf{C})\): \[\mathrm{hom}_{\mathbf{C}}\left(a \times y, d\right) \cong \mathrm{hom}_{\mathbf{C}}\left(y, d^a\right).\] Indeed, the definition gives a bijection between morphisms \(y \times a \to d\) and morphisms \(y \to d^a\). On the other hand, \(a \times y\) and \(y \times a\) are canonically isomorphic because the Product is defined by a universal property and we can swap the projections.

Let \(i_b: b \to b \oplus c\) and \(i_c: c \to b \oplus c\) be the sum injections. Then we have morphisms \[\mathbf{1}_{a \to a} \times i_b: a \times b \to a \times (b \oplus c)\] and \[\mathbf{1}_{a \to a} \times i_c: a \times c \to a \times (b \oplus c).\] Consider an arbitrary object \(d\). We have the following chain of bijections: \[\begin{aligned} \mathrm{hom}_{\mathbf{C}}\left(a \times (b \oplus c), d\right) &\cong \mathrm{hom}_{\mathbf{C}}\left(b \oplus c, d^a\right) \\ &\cong \mathrm{hom}_{\mathbf{C}}\left(b, d^a\right) \times \mathrm{hom}_{\mathbf{C}}\left(c, d^a\right) \\ &\cong \mathrm{hom}_{\mathbf{C}}\left(a \times b, d\right) \times \mathrm{hom}_{\mathbf{C}}\left(a \times c, d\right). \end{aligned}\] The first and the last bijections come from the exponential object. The middle bijection comes from the universal property of the Sum. The composed bijection sends a morphism \(h: a \times (b \oplus c) \to d\) to the pair \[\left( h \circ (\mathbf{1}_{a \to a} \times i_b), h \circ (\mathbf{1}_{a \to a} \times i_c) \right).\] Therefore, for every pair of morphisms \(f: a \times b \to d\) and \(g: a \times c \to d\) there exists a unique morphism \(h: a \times (b \oplus c) \to d\) such that \[f = h \circ (\mathbf{1}_{a \to a} \times i_b)\] and \[g = h \circ (\mathbf{1}_{a \to a} \times i_c).\] Thus \(a \times (b \oplus c)\) satisfies the universal property of the sum of \(a \times b\) and \(a \times c\). The object \((a \times b) \oplus (a \times c)\) satisfies the same universal property by definition. Hence the uniqueness of the sum gives the canonical morphism \[\left[\mathbf{1}_{a \to a} \times i_b, \mathbf{1}_{a \to a} \times i_c\right]: (a \times b) \oplus (a \times c) \to a \times (b \oplus c)\] as an isomorphism. Therefore \[(a \times b) \oplus (a \times c) \cong a \times (b \oplus c).\]

It remains to prove the second condition from Distributive category. Let \(\mathbf{0}\) be the Initial object. For every \(d \in \mathrm{ob}(\mathbf{C})\) we have \[\mathrm{hom}_{\mathbf{C}}\left(a \times \mathbf{0}, d\right) \cong \mathrm{hom}_{\mathbf{C}}\left(\mathbf{0}, d^a\right).\] The last hom-set has exactly one element because \(\mathbf{0}\) is initial. Therefore, for every \(d\) there exists a unique morphism \(a \times \mathbf{0} \to d\). Thus \(a \times \mathbf{0}\) is also an initial object. By Initial object is unique, initial objects are canonically isomorphic, and therefore \[a \times \mathbf{0} \cong \mathbf{0}.\] Moreover, the unique morphism \(\mathbf{0} \to a \times \mathbf{0}\) is the canonical isomorphism between these initial objects. The assumptions already give finite products and finite sums. The two canonical isomorphisms above are exactly the remaining conditions from Distributive category, so \(\mathbf{C}\) is distributive. ◻

Programming language examples. Type algebra

\(\mathbf{Hask}\) category

If we avoid lazy evaluations in Haskell (see Haskell lazy evaluation) then we can found several types as candidates for initial and terminal object in Haskell. Initial object in Hask category is a type without values

data Void

i.e. you cannot construct a object of the type.

There is only one function from the initial object:

absurd :: Void -> a

The function is called absurd because it does absurd action. Nobody can proof that it does not exist. For the existence proof the following absurd argument can be used: “Just provide me an object type Void and I will provide you the result of evaluation”.

There is no function in opposite direction because it would had been used for the Void object creation.

Terminal object (unit) in Hask category keeps only one element

data () = ()

i.e. you can create only one element of the type. You can use the following function for the creation:

unit :: a -> ()
unit _ = ()

The Product in Hask category keeps a pair and the constructor defined as follows

(,) :: a -> b -> (a, b)
(,) x y = (x, y)

There are 2 projectors:

fst :: (a, b) -> a
fst (x, _) = x
snd :: (a, b) -> b
snd (_, y) = y

The Sum in Hask category defined as follows

data Either a b = Left a | Right b

The typical usage is via pattern matching for instance

factor :: (a -> c) -> (b -> c) -> Either a b -> c
factor f _ (Left x) = f x
factor _ g (Right y) = g y

As soon as \(\mathbf{Hask}\) is a Cartesian closed category then by Cartesian closed category it is a Distributive category i.e. one can conclude that

(a,Either b c)

is the same to

Either (a, b) (a, c)

It’s not surprisingly that the Exponential in \(\mathbf{Hask}\) is a function object i.e. \(b^a\) can be written as a -> b.

Exponential gives interesting results with types manipulations. For instance the type \(a^{b+c}\) can be written as

Either b c -> a

for the function we should have both functions b -> a and c -> a. I.e. the code is equivalent to the following one

(b -> a, c -> a)

These transformations correspond to the following simple algebraic equation \[a^{b+c} = a^b a^c.\] This is also called as type algebra.

\(\mathbf{C\texttt{++}}\) category

C++ does not provide a clean standard type that plays the role of an Initial object in our simplified category of types and functions. The type void is close to the idea of an empty type, but it cannot be used as an ordinary function argument type, so it does not provide morphisms void -> A in the same way as Void does in Hask category.

If we work in a restricted fragment, we can introduce an uninhabited wrapper type and use it as an approximation:

struct Void {
  Void() = delete;
};

Since no value of Void can be constructed, every pure total function from Void to another type is extensionally the same.

C++ 17 introduced a special type that keeps only one value - std::monostate:

namespace std {
  struct monostate {};
}

The Product in C++ category keeps a pair and the constructor defined as follows

namespace std {
  template< class A, class B > struct pair {
    A first;
    B second;
  };
}

There is a simple usage example

  std::pair<int, bool> p(0, false);

  std::cout << "First projector: " << p.first << std::endl;
  std::cout << "Second projector: " << p.second << std::endl;

Really any struct or class can be considered as a product.

If we consider Objects as types then Sum is an object that can be either one or another type. The corresponding C/C++ construction that provides an ability to keep one of two types is union.

C++17 suggests std::variant as a safe replacement for union. The example of the factor function is below

    template <typename A, typename B, typename C, typename D>
    auto factor(A f, B g, const std::variant<C, D>& either) {
      try {
        return f(std::get<C>(either));               
      }
      catch(...) {
        return g(std::get<D>(either));                              
      }
    };

The simple usage as follows:

    auto stringLength = [](std::string s) {
      return static_cast<int>(s.size()); };
    auto id = [](auto x) { return x; };

    std::variant<std::string, int> var = std::string("abc");
    std::cout << "String length:" <<
    factor<>(stringLength, id, var) << std::endl;
    var = 4;
    std::cout << "id(int):" <<
    factor<>(stringLength, id, var) << std::endl;    

Thus std::variant<C, D> plays the role of the sum type \(C \oplus D\). The function factor corresponds to the universal property of Sum: if we have functions from both alternatives to the same result type, then there is a unique function from the sum type that chooses the correct branch.

\(\mathbf{Scala}\) category

We used a same trick as for Initial object in Hask category and define Initial object in Scala category as a type without values:

Nothing

i.e. you cannot construct a value of the type. There is only one function from the initial object:

def absurd[A](value: Nothing): A = value

We used a same trick as for Terminal object in Hask category and define Terminal object in Scala category as a type with only one value

abstract final class Unit extends AnyVal

The only value of the type is written as follows:

val unit: Unit = ()

i.e. you can create only one element of the type.

The Product in Scala category keeps a pair. Scala has a built-in product type as follows:

val pair: (Int, Boolean) = (1, true)
val first: Int = pair._1
val second: Boolean = pair._2

The values pair._1 and pair._2 are the two projectors.

The Sum in Scala category is represented by Either. It keeps a value of the left type or a value of the right type:

val left: Either[String, Int] = Left("abc")
val right: Either[String, Int] = Right(4)

The universal property of the sum can be written as the following factor function:

def factor[A, B, C](f: A => C, g: B => C)
                   (either: Either[A, B]): C =
  either match {
    case Left(a) => f(a)
    case Right(b) => g(b)
  }

If we have functions from both alternatives to the same result type, then factor gives us a unique function from Either[A, B] to that result type.

Quantum mechanics

we shall use a Hilbert space of dimension 0 as the Initial object. For every finite dimensional Hilbert space \(\mathcal{A}\) there is exactly one linear map \(\mathcal{H}_0 \to \mathcal{A}\). It sends the only vector of \(\mathcal{H}_0\) to the zero vector of \(\mathcal{A}\).

we shall use a Hilbert space of dimension 0 as the Terminal object. For every finite dimensional Hilbert space \(\mathcal{A}\) there is exactly one linear map \(\mathcal{A} \to \mathcal{H}_0\). It maps every vector from \(\mathcal{A}\) to the zero vector of \(\mathcal{H}_0\).

Let \(\mathcal{A}\) and \(\mathcal{B}\) be finite dimensional Hilbert spaces. The same object \(\mathcal{A} \oplus \mathcal{B}\) is both the Product and the Sum in FdHilb category.

For the product structure we use the projections \[\pi_{\mathcal{A}}(a \oplus b) = a\] and \[\pi_{\mathcal{B}}(a \oplus b) = b.\] For the sum structure we use the injections \[i_{\mathcal{A}}(a) = a \oplus 0_{\mathcal{B}}\] and \[i_{\mathcal{B}}(b) = 0_{\mathcal{A}} \oplus b.\] The projections satisfy the universal property of the product, and the injections satisfy the universal property of the sum. Thus in \(\mathbf{FdHilb}\) the categorical product and the categorical sum coincide up to canonical isomorphism.

It is worth noting that the tensor product is the usual construction for compound quantum systems, but it is not the categorical product in FdHilb category. The categorical product is the direct sum above.

(https://math.stackexchange.com/users/142355/david-myers), David Myers. n.d. “How Should i Think about Morphism Equality?” Mathematics Stack Exchange. https://math.stackexchange.com/q/1346167.
ProofWiki. 2018. “Empty Mapping Is Unique.” https://proofwiki.org/wiki/Empty_Mapping_is_Unique.
Wikipedia. 2017. “Disjoint Union — Wikipedia, the Free Encyclopedia.” https://en.wikipedia.org/w/index.php?title=Disjoint_union&oldid=774047863.
Wikipedia contributors. 2018. “Distributive Category — Wikipedia, the Free Encyclopedia.” https://en.wikipedia.org/w/index.php?title=Distributive_category&oldid=851490594.
———. 2019. “Coproduct — Wikipedia, the Free Encyclopedia.” https://en.wikipedia.org/w/index.php?title=Coproduct&oldid=893649821.