\chapter{Universality, Adjoints and Closed Cartesian Categories}
\thispagestyle{empty}
In this chapter we are going to study universality and adjunctions. Despite a possible apparent difference in their definitions, both have the same use: they are tools that, given an arrow selected in a category, allow us to uniquely select an arrow in another category, generally fulfilling some special property. \\

We will start this section with the definition of universality, as well as the statement of the celebrated Yoneda's lemma. Then, we present the concept of adjunction. We will use these concepts to introduce new structures, in particular that of closed Cartesian category.

\section{Universality}
In this section we present the concept of universality. This concept is behind lots of mathematical properties. Intuitively, universality is an efficient way of expressing a one-to-one correspondence between arrows of different categories. This one-to-one relationship is usually expressed via ''given an arrow  $f$ it exists one and only one  arrow $\overline f$ such that <insert your favorite universal property>''.\\

Probably the first contact that any mathematician has with universality is defining a function  $f:\mathbb R \to \mathbb R^2$. It is easy to see that defining such a function is equivalent to define two $g,h: \mathbb R \to \mathbb R$. Furthermore, those $g,h$ are unique for each $f$. This uniqueness is the flavor that attempts to capture the concept of universality. Other examples of unique existence are those that occur in quotient groups or in bases of vector spaces. \\

\begin{definition}\label{def:univ-arrow}
  Let $S: D \to C$ be a functor and $c \in Ob(C)$. An \emph{universal arrow}  from $c$ to $S$ is a pair $(d,u)$ with $d\in Ob(D), u:c \to Sd \in Ar(C)$, such that for every $(e,f)$ with $e\in Ob(D)$  and $f:c\to Sd$ there exists an unique $f':r,d\in Ar(D)$ such that $Sf'\circ u = f$.

\end{definition}
In a diagram:

\[
  \begin{tikzcd}
    c\arrow[r, "u"] \arrow[rd, "f", swap]      & Sd \arrow[d, "Sf'", dashed]& d\arrow[d,"f'",dashed]\\
    &Se& e
  \end{tikzcd}
\]

Note that an universal arrow $(d,u)$ induces the unique existence of an arrow in $D$, but with interesting properties via it relationship with $S$. Usually, to provide an universal arrow we will only define the functor $S:D\to C$ and the arrow $u:c\to Sd$, letting all other information be deduced from the context.\\

The idea of universality is often regard via the notion of \emph{universal element}, in the special (and commonplace) case where $S:D\to Set$.

\begin{definition}
  Let $S: D \to Set$, an \emph{universal element}  for $S$ is a pair $(r,e)$ with $r \in Ob(D), e \in   Sr$, such that for every $(d,x)$ with $d\in Ob(D), x\in Sd$, there is an unique arrow $f:r\to d\in D$ such that $(Sf)e = x$.
\end{definition}

Note that having an universal element is a particular case of having an universal arrow. Considering $*$ the set with one point (as a category) an universal element is an universal arrow $*\to H$. This is clearly seen if we consider the diagram:
% c\arrow[r, "u"] \arrow[rd, "f"]
\[
  \begin{tikzcd}
    *\arrow[r, "u"] \arrow[rd, "f", swap] & Sr \arrow[d, "Sf", dashed]& r\arrow[d,"f", dash]\\
    &Sd& d
  \end{tikzcd}
\]

where $*$ is helping us select the elements of $Sd$ and $Se$ to enforce the property.\\

Conversely if $D$ has small homesets, we can consider an universal arrow to be a particular case of an universal element. With the same context as in \ref{def:univ-arrow} $(d,u: c\to Sd)$ is an universal arrow if, and only if, $(d, u\in  \hom_{D}(c, Sd))$ is an universal element.\\

The point of having these such similar definition is to have different interfaces for the concept. While most result can be easily adapted, once we got to the examples is clear when to use each concept.

\begin{example}\
  \begin{itemize}
  \item Quotient group: This property states that, for any two groups $N\lhd G$ with $\pi: G \to N$ the canonical projection, and any group homomorphism $f:G\to K$ such that $N\subset \ker f$ there exists an unique $\overline f: H \to K$ such that $\overline f  \circ \pi = f$.\\

    The question now is, how is this a universal property. This is an example of a universal element. Considering the functor $H: Grp\to Set$:
    $$HG' = \{f:G\to G'\in Ar(Grp) : F(N)=\{0\} \},$$

    Then, $(G/N, \pi)$ is an universal element for $H$. From this property alone the three isomorphism theorems can be deduced. Therefore, we only have to prove this result to have the full power of these theorems in any context (e.g. Rings, K-Algebra, or Topological spaces).

  \item Tensor product: Given an right $R$-module $A$ and a left $R$-module $B$ one can consider the tensor product as an abelian group $A\otimes_R B$ and an $R$-biadditive function
    $$h:A\times B\to A \otimes_R B,$$
    such that  for every $R$-biadditive $f:A\times B \to G$ it exists an unique $\overline f$ such that $\overline f\circ h=f$. Is easy to follow that tensor product can be reformulated as an universal arrow $\varphi:A\time B \to A\otimes_R B$ from $A\times B$ to the identity functor in the category of $R$-additive groups along with $R$-additive functions.
  \end{itemize}
\end{example}

Dually, we can consider an \emph{universal arrow from $S$ to $c$} or simply universal arrow from $S$:
\begin{definition}\label{def:univ-arrow2}
  Let $S: D \to C$ be a functor and $c \in Ob(C)$, an \emph{universal arrow}  from $c$ to $S$ is a pair $(d,u)$ with $d\in Ob(D), u:c \to Sd \in Ar(C)$, such that for every $(e,f)$ with $e\in Ob(D)$  and $f:c\to Sd$ there exists an unique $f':r,d\in Ar(D)$ such that $u\circ Sf' = f$.
\end{definition}
In a diagram:

\[
  \begin{tikzcd}
    c       & Sd \arrow[swap, l, "f"]\arrow[d, "Sf'", dashed]& d\arrow[d,"f'"]\\
    &Se\arrow[lu, "u"]& e
  \end{tikzcd}
\]

\begin{example}\label{example:prod}\
  \begin{itemize}
  \item In $Set$  we have a particular construction: the product of sets for any two sets $a,b$ along with the projections $\pi_1 a \times b \to a$ and $\pi_2 a \times b \to b$. Then, we can define the functor $\Delta: Set \to Set\times Set$ such that $\Delta c = c\times c$ for every $c\in Ob(Set)$ and $\Delta (f:c\to b): c\times c\to b\times b$ is the elements-wise application of $f$.\\

    Then, given any two sets $a,b\in Ob(Sets)$, $u=(\pi_1,\pi_2)$ is an universal arrow from $\Delta$ to $(a,b)$ as

    \[
      \begin{tikzcd}
        (a,b)       & \Delta d=(d,d) \arrow[swap, l, "f"]\arrow[d, "\Delta f'", dashed]& d\arrow[d,"f'"]\\
        &\Delta e=(e,e)\arrow[lu, "u"]& e
      \end{tikzcd}
    \]

    This construction is reproducible in, among many others, $Grp, Top$ or Banach Spaces with bounded linear transformation.
  \end{itemize}
\end{example}

Lastly, we will provide a characterization of universality:
\begin{proposition}\label{Yoneda-proposition}
  Let $S: D \to C$ be a functor and $u:c\to Sr\in Ar(C)$. Then $u$ is an universal arrow if and, only if, the function $\varphi:\hom_D(r,\cdot)\to \hom(c,S\cdot)$ such that $\varphi(d)(f)= Sf\circ u$ for all $f\in \hom_D(r,d)$ is a natural bijection. Conversely, every natural bijection is uniquely determined by an universal arrow $u:c\to Sr$.
\end{proposition}
\begin{proof}
  Let $u$ be universal. Then, $\varphi$ is clearly a bijection. For $\varphi$ to be a natural transformation the diagram
  \[
    \begin{tikzcd}
      \hom_D(r,d)\arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\varphi(d)} & \hom_C(c,Sd)\arrow{d}{\hom_C(c,Sg)}\\
      \hom_D(r,d')\arrow{r}{\varphi(d')} & \hom_C(c,Sd')
    \end{tikzcd}
  \]

  should commute for all $g\in Ar(D)$. As this is a diagram in the category of sets, we can check the commutativity by checking it element wise. For any $f \in\hom_D(r,d)$:
  \[
    \begin{tikzcd}
      f\arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\varphi(d)} & Sf\circ u \arrow{d}{\hom_C(c,Sg)}\\
      g\circ f \arrow{r}{\varphi(d')} & S(g\circ f) \circ u = Sg\circ Sf \circ u
    \end{tikzcd}
  \]

  So the diagram commutes, and $\varphi$ is natural. The bijectivity follows from $u$ being universal.\\

  Lets consider now that $\varphi$ is a natural bijection. We will define $u := \varphi(r)(1_r)$ and check that $(r,u)$ is an universal arrow. As $\varphi$ is natural we have that:

  \[
    \begin{tikzcd}
      \hom_D(r,r)\arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\varphi(r)} & \hom_C(r,Sr)\arrow{d}{\hom_C(r,Sg)}\\
      \hom_D(r,d)\arrow{r}{\varphi(d)} & \hom_C(r,Sd)
    \end{tikzcd}
  \]

  Writing the diagram for the element $1_r\in\hom_D(r,r)$ and any $d\in Ob(D), g:r\to d\in \hom_D(r,d)$:

  \[
    \begin{tikzcd}
      1_r \arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\varphi(r)} & u \arrow{d}{\hom_C(r,Sg)}\\
      g\arrow{r}{\varphi(d)} & \varphi(d)(g)= Sg\circ u
    \end{tikzcd}
  \]

  and since $\varphi$ is a bijection, for every $f\in \hom_C(r,Sd)$ there is an unique function $f' = \varphi(d)^{-1}(f)$ such that $Sf'\circ u = f$, thus being $u$ universal.

\end{proof}

This proposition, sometimes called Yoneda's proposition \cite[p. 81]{mac2013categories}, is of capital importance. Later on this chapter, we will study \emph{adjunctions} and this proposition will enable us to fully understand the relationship between adjunctions and universality. From result there is a definition that arises:

\begin{definition}
  Let $D$ be a category with small home-sets and let $F:D\to C$ be a functor. A representation of a functor $K:D\to Set$ is a pair $(r,\varphi)$ with $r \in Ob(D)$ and $ \varphi$ a natural isomorphism  such that
  $$D(r,\cdot) \equiv_{\varphi} F\cdot.$$
  A functor is said to be \emph{representable} whenever it has a representation.
\end{definition}

Note that therefore a universal arrow induces a natural isomorphism $D(r,d)\equiv C(c,Sd)$ and this induces a representation of the functor $C(c,S\cdot): D\to Set$, being these three equivalent.

\subsection{Yoneda's lemma}
This subsection  deals with Yoneda's Lemma. Mac Lane\cite{mac2013categories} assures the lemma first appeared in his private communication with Yoneda in 1954. With time, this result has became one of the most relevant one in Category Spaces. We will start by providing some intuition to it, followed by it proof and some use cases.\\

This results is due to Japanese professor Nobuo Yoneda. We know about Yoneda's life thanks to the elegy that was written by Yoshiki Kinoshita\cite{YonedaLife}. Yoneda was born in Japan in 1930, and received his doctorate in mathematics from Tokyo University in 1952. He was a reviewer for international mathematical journals. In addition to his contributions to the field of mathematics, he also devoted his research to computer science.\\

The idea behind the Yoneda lemma can be arid at first, if one does not have a prior understanding of what the purpose and usefulness of this lemma is. In order to illustrate this idea we will introduce a (simplified) definition of Moduli Spaces, so that we have a geometric understanding of Yoneda's Lemma.\\

The idea behind (some) Moduli spaces is to classify algebraic curves up to isomorphisms. In addition, Moduli spaces allow us to control complex mathematical objects (such as a quotient space of unknown objects) by simpler objects or objects with better properties (such as a concrete variety). A canonical example of this type of classification is:
\begin{align*}
  \{\text{Vector spaces of finite dimension}\}/\text{isomorphism } &\cong \N\\
  [V] &\to dim V.
\end{align*}
Where a complex object can be classified by another object of which we know more properties. We can start by defining:
$$\mathcal{M} = \{\text{smooth complex non singular curves} \}/\text{isomorphism}$$

Further on when we talk about curves we will refer to smooth complex non singular curves. Note that if two curves are isomorphic then they have the same genus. Therefore the function
\begin{align*}
  \gamma: \mathcal{M} &\mapsto \N\\
  \displaystyle  [V]&\mapsto \text{genus of V}
\end{align*}

is well defined and we can define $\mathcal{M}_g = \gamma^{-1}(g)$. An interesting classification of $\mathcal{M}_g$ is given when we consider that for every $g$ there exists a closed, connected, non-singular variety $U_g$ and a family $\{C_t : t \in U_g}$ such that a curve of genus $g$ will be a fibration of $C_t$.  Moreover there is a variety $M_g$ and a subjective morphism $\varphi: U_g \to M_g$ such that $\varphi(t_1)=\varphi(t_2)$ if $C_{t_1} \equiv C_{t_2}$. Therefore we are classifying the equivalence classes of $\mathcal{M}_g$ by points of the variety $M_g$ (thus generating a Moduli problem).\\

Similarly to this two example, with the Yoneda lemma we will have a functor $F:D\to Set$ and one representation of this functor. We will classify the natural transformation of these functors by the set in the image of $F$! Interestingly enough, there will be applications where the complex object is not the space of naturals transformations, but the images of $F$ (see \ref{Cayleys} for an example). Lets proceed to enunciate and proof the result.

\begin{theorem}\cite[Section 3.2]{mac2013categories}
  Let $D$ be a category with small home-sets, $K:D\to Set$ be a functor, and $r\in Ob(D)$. Then there is a bijection
  \begin{align*}
    \tau:Nat(\hom_D(r,\cdot), K\cdot) &\equiv Kr\\
    \tau(\alpha:\hom_D(r,\cdot)\to K\cdot)& = \alpha(r)(1_r)
  \end{align*}
  Where $\tau$ is natural in $K$(as an object of $Set^{D}$) and in $r$.
\end{theorem}
\begin{proof}
  As $\alpha$ is a natural transformation we have that, for every $g:r\to d\in Ar(D)$:
  \[
    \begin{tikzcd}
      \hom_D(r,r)\arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\alpha(r)} & Kr\arrow{d}{Kg}\\
      \hom_D(r,d)\arrow{r}{\alpha(d)} & Kd
    \end{tikzcd}
  \]
  Writing $\alpha(r)(1_r) = u$ we have that:
  \[
    \begin{tikzcd}
      1_r\arrow[swap]{d}{\hom_D(r,g)}\arrow{r}{\alpha(r)} & u\arrow{d}{Kg}\\
      g\circ 1_r = g\arrow{r}{\alpha(d)} & \alpha(d)(g)=Kg\circ u
    \end{tikzcd}
  \]

  Therefore every natural transformation is uniquely identified by the value of $u$, therefore $\tau$ is injective. Moreover, for every $u$ in $Kr$, we can define a natural transformation following the previous diagram, therefore, $\tau$ is bijective.\\

  To see that $\tau$ we is natural we have to consider for which functor it is natural. Consider the functor \emph{evaluation} $E: Set^D\times D$ that maps each $(F,c)\to Fc$, and the functor $N:Set^D\times D\to Nat(\hom_D(r,\cdot),K)$ the set of natural transformations. Finally, $\tau:N\to E$ is a natural transformation.
\end{proof}
\begin{remark}
  The existence of an evaluation will be revisited when we consider \emph{closed Cartesian categories} in  \ref{subsect:CCC}. This categories (in general, closed categories) will generalized the idea of a category having an exponential object suitable for evaluation. In this case, we are inadvertently using that $Cat$ is a closed Cartesian category.
\end{remark}

The first functor that we will want to apply this result is to the $\hom_{\cdot}$ functor. But this functor is a bifunctor, so to get the full result of this lemma applied to the full bifunctor we may restate this lemma to contravariant functors.

\begin{corollary}
  Let $D$ be a category with small home-sets, $F:D\to Set$ be a contravariant functor, and $r\in Ob(D)$. Then there is a bijection
  \begin{align*}
    \tau:Nat(\hom_D(\cdot,r), F\cdot) &\equiv Fr\\
    \tau(\alpha:\hom_D(\cdot,r)\to F\cdot)& = \alpha(r)(1_r)
  \end{align*}

  Where $\tau$ is natural in $K$(as an object of $Set^{D}$) and in $r$.
\end{corollary}
\begin{proof}
  We seek to use the Yoneda lemma in the functor $F':D^{op}\to Set$ induced by $F$. Then we have that:
  \begin{align*}
    \tau:Nat(\hom_{D^{op}}(r,\cdot), F'\cdot) &\equiv F'r\\
    \tau(\alpha:\hom_{D^{op}}(r,\cdot)\to F'\cdot)& = \alpha(r)(1_r)
  \end{align*}

  Taking into account that $F_{|Ob(D)}=F'_{|Ob(D^{op})}$, and that
  $\hom_{D^{op}}(r,\cdot) = \hom_D(\cdot,r)$ we have the result.
\end{proof}

As we have seen, the Yoneda lemma is a direct generalization of the moduli problem. In the same vein, Yoneda's lemma is the generalisation of other problems/theorems in mathematics, most notably Cayley's lemma. It states:
\begin{proposition} \label{Cayleys}
  Any group is isomorphic to a subgroup of a symmetric group.
\end{proposition}

To understand this, take a groups $G$ seen as a single-object category, and name that object $e$. Then, the functor $\hom_G(e, \cdot): G \to Set$ can be seen as a group action \ref{group-action}. Then the Yoneda lemma states that:

$$Nat(\hom_G(e,\cdot), Hom_G(e,\cdot)) \equiv_\varphi Hom_G(e,e).$$

Translating this result to group theory:
\begin{itemize}
\item Remember that $Hom_G(e,e)$ is the group $G$.
\item Every natural transformation is a equivariant map between G-sets.
\item This equivariant maps, forms an endormophism group under composition, being a subgroup of the group of permutations.
\item This natural isomorphism $\varphi$ define a group isomorphism.
\end{itemize}

So we have the isomorphism of groups that is stated in Cayley's Theorem.\\

We continue our exploration of Yoneda lemma by defining the \emph{Yoneda Embedding}. For that we define the contravariant functor $h_a = \hom_C(\cdot, a)$. Then the contravariant Yoneda lemma tell us that:
$$Nat(h_a,h_b) \equiv_{\tau_a} \hom(a,b).$$

We then can define a fully faithfuls embedding $\upsilon: C \to Set^{C^{op}}$ such that
\begin{align*}
  \upsilon a  &= \hom_C(A, \cdot)\qquad \forall a \in Ob(C), \\
  \upsilon f &= \tau_a^{-1} (f)\qquad\qquad \forall f:b\to a\in Ar(C).
\end{align*}

This functor allows us to view the category $C$ as a subcategory of the category of contravariant functors from $C$ to $Set$, which will be useful for determining "heritable" properties in $C$.
\subsection{Properties expressed in terms of universality}

After the examples given, we define a few constructions that are commonplace in Maths. We will outline the notions of limit, pullback and product, and the dual notions of colimit, pushout and coproduct.\\

The notions of product and pullback can be seen as particular cases of the notion of limit. To define limit we will introduce the concept of co-cone and the diagonal functor. \\

\begin{definition}\label{daigon-alley}
  Let $C,J$ be categories. We can define the functor $\Delta_J: C \to C^J$ that maps $c$ to the functor from $J$ to $C$ that is constantly $c$, and maps every arrow to the identity $1_c$.
\end{definition}

Whenever possible we will write only $\Delta$, and let the information of the category be deduced from context. $J$ is usually small and often finite. We can now consider a natural transformation $\tau: F \to \Delta c$. This can be represented as in the following diagram:
\[
  \begin{tikzcd}
    Fx_j\arrow[swap]{dr}{\tau x_j}\arrow{rr}{F g} &
    & Fx_k\arrow{dl}{\tau x_k}\\
    & c&
  \end{tikzcd}
\]

commutes for every $g:x_j\to x_k\in Ar(j)$, for that reason, such natural transformation is usually called a co-cone. The dual notion is called cone and  is represented as:
\[
  \begin{tikzcd}
    Fx_j\arrow{rr}{F g}&
    & Fx_k\\
    & c\arrow{ur}[swap]{\tau x_k}\arrow{ul}{\tau x_j} &
  \end{tikzcd}
\]

We can now define the concepts of limit and colimit. We introduce first the concept of colimit. This definition is that of an universal arrow, only in a category of functors. Following this definition, we will define the limit as its dual concept.
\begin{definition}
  A colimit is an object $r\in Ob(C)$ together with an universal arrow $u:F\to \Delta r \in Ar(C^J)$. The colimit is denoted by $$\lim_{\leftarrow} F = r = \colim F.$$
\end{definition}

The notation $\lim_{\leftarrow}$ is intuitive: in the colimit we have arrows to $F$. To represent this as a diagram, we have a co-cone $u\to \colim F$ such that for every other co-cone $\tau \to s$, it exist an unique $f$ such that the following commutes for every $x_j,x_k\in Ob(C)$:

\[
  \begin{tikzcd}
    {} & l& & \\
    & \colim F   \arrow[dashed]{u}[description]{f} \\
    x_j \arrow{ur}{u x_j} \arrow[bend left]{uur}{\tau x_j}\arrow[swap]{rr}{Fg} & &
    x_k \arrow[swap]{dr}{u x_k}\arrow[bend right,swap]{uul}{\tau x_k}\\
  \end{tikzcd}
\]

Now, thanks to the duality of categories, we can define what a limit is in a very synthetic way:

\begin{definition}
  A limit is the dual concept of a colimit. It is denoted as
  $$\lim_{\rightarrow} F = r = \lim F.$$
\end{definition}

A limit is represented by the following diagram:
\[
  \begin{tikzcd}
    {} & l
    \arrow[bend right,swap]{ddl}{\tau x_j}
    \arrow[bend left]{ddr}{\tau x_j} \arrow[dashed]{d}[description]{f}& & \\
    & \lim F \arrow{dr}{u x_k} \arrow{dl}[swap]{u x_j} \\
    x_j \arrow[swap]{rr}{Fg} & &
    x_k \\
  \end{tikzcd}
\]

Analogously as in the colimit notation, in the limit we have arrows from $F$, and thus the notation $\lim_{\rightarrow}$. Let focus for a while now in the notion of limit, in particular of two of its specials cases: the product and the pullback. From these cases we are going to provide most examples. \\

We have already talked about the product of categories. The product is a limit where $J$ is the 2-element discrete category, that is, where every functor $F:J\to C$ is merely choosing to object of $C$.

\begin{definition}\label{prod-univ}
  Let $C$ be a category, $J=\{0,1\}$ be the discrete category with two elements. The product $c_1\times c_2$ of two elements $c_0,c_1\in Ob(C)$ is the limit of the functor $F:J\to C$ such that $F0 = c_0, F1= c_1$.
\end{definition}

This construction means that providing an arrow to $c_0,c_1$ determines an unique arrow to $c_0\times c_1$ and vice versa by composition with $\pi_i$. In this case, the arrows $u0, u1$ are usually called \emph{projections} and denoted by $\pi_0, \pi_1$. The notation is due to the product being a generalization of the Cartesian product in the category $Set$. It is also sometimes note with $c_0 \Pi c_1$ with $\Pi$ being standard for the sequence product. Some examples of product object in categories are:
\begin{example}\
  \begin{itemize}
  \item Products from Example \ref{example:prod}.
  \item In Haskell, considering two integer types \texttt{A} and \texttt{B}, we can consider the duple type \texttt{(A,B)} that is the product, along with projections \texttt{fst} and \texttt{snd} to be a product. We can fairly easily check that, for any other integer type \texttt{C} and any morphism \texttt{f: C$\to$ (A,B)} it exist a unique \texttt{g} and \texttt{h} such that:
    \[
      \begin{tikzcd}
        {} & C \arrow[bend right,swap,dashed]{dl}{g}
        \arrow[bend left,dashed]{dr}{h} \arrow{d}[description]{f}& & \\
        A  &(A,B) \arrow{l}[swap]{fst} \arrow{r}{snd} &
        B \\
      \end{tikzcd}
    \]
    Note that we have requested \texttt{A} and \texttt{B} to be integer types. Quite surprisingly, this construction can not be generalized in Haskell to provide a product type for every object\cite{wiki:hask}. This will provide in the future a palpable difference between our theoretical $\lambda$-calculus, and its applicable version,  Haskell.\\

    This troubles arise from of implementation details of bottom values. Why have not anyone fix this? Well, when functions have to terminate and only finite values are considered this problems does not Arise. This condition is fairly acceptable in any engineering scenario.
  \end{itemize}
\end{example}

Analogously, we can define the coproduct, on which instead of defining an arrow to $c_0, c_1$, we define an object \emph{from} $c_0,c_1$.
\begin{definition}
  The coproduct is the dual definition of the product. It is denoted by $c_0 \sqcup c_1$.
\end{definition}
In this notation $\sqcup$ denotes an inverted $\Pi$, with the meaning of being the dual notion of the product.
\begin{example}\ \label{example:mitchell}
  \begin{itemize}
  \item In the category $Ab$ of Abelian Group and morphism, finite products and coproducts are equivalent. This equivalence also happens in others categories, such as $R$-Modules for a ring $R$ or vector spaces. This has a underlying idea of a type of category that relates this constructions: This are all \emph{Abelian Categories}, as in \cite[Section 5.5]{rotman2008introduction}.\\

    We will not delve further into this concept so as not to deviate from the subject at hand. However, for those readers who are familiar with the notation, we cannot resist including Mitchel's theorem\cite[Chapter IV]{mitchell1965theory} .

    \begin{theorem}[Mitchell's Theorem] If A is a small abelian category, then there is a covariant full faithful exact functor $F : A \to Ab$.
    \end{theorem}

    As a consequence, any statement ``$p$ implies $q$'' where $p$ and $q$ are categorical statement about a diagram in $A$, if is true in  $Ab$, it holds true in $A$.
  \end{itemize}
\end{example}

% From my personal experience I have to say that I have seen more difficulties learning this notion rather than learning the utterly similar notion of product, probably because we are more used to think in terms of arrays rather than in terms of universality for the product. I think that thinking in terms of universality should be the way in to these concept.

After learning about the product and the coproduct, we will focus now in the notion of pullback and its dual, the pushout. We will first define the category

\[
  P = \begin{tikzcd}
    x\arrow{r}{f} & z & y\arrow[swap]{l}{g}
  \end{tikzcd}
\]

Then we can define the pullback:

\begin{definition}\label{def:pullback}
  Let $C$ be a category and $F:P\to C$ be a functor. Then the pullback of $Fx$ and $F_y$ denoted as $Fx\times_{Fz}Fy$ is the limit of the functor $F$.
\end{definition}

We can represent this structure in the following diagram. For any object $q$ and arrows $f':q\to Fx,g':q\to Fy$ we have:

\[
  \begin{tikzcd}
    q
    \arrow[bend left]{drr}{g'}
    \arrow[bend right,swap]{ddr}{f'}
    \arrow[dashed]{dr}[description]{u} & & \\
    & Fx\times_{Fz}Fy \arrow{r}{p_2} \arrow{d}[swap]{p_1}
    & Fy \arrow{d}{Fg} \\
    & Fx \arrow[swap]{r}{Ff}
    & Fz
  \end{tikzcd}
\]

Analogously, we can define:

\[
  CoP = \begin{tikzcd}
    x & z\arrow{l}{f}\arrow[swap]{r}{g} & y
  \end{tikzcd}
\]

and define:
\begin{definition}
  Let $C$ be a category and $F:CoP\to C$ be a functor. Then the pushout of $Fx$ and $F_y$ denoted as $Fx\sqcup_{Fz}Fy$ is the colimit of the functor $F$.
\end{definition}

\begin{example}
  \begin{itemize}\label{fiber-product}
  \item Fiber Product: The fiber product is the canonical pullback of the $Top$ category. Let $X,Y,Z$ be topological spaces and let $f:X\to Z$ and $g: Y\to Z$ be continuous function. Then we define the fiber product as:
    $$X\times_ZY = \{(x,y) \in X\times Y: fx=gy\}.$$ Note that, although it not usually noted, $X\times_Z Y$ depend on both $f$ and $g$. In a diagram:
    \[
      \begin{tikzcd}
        X\times_Z Y\arrow{r}{\pi_2}\arrow[swap]{d}{\pi_1} & Y\arrow{d}{g}\\
        X\arrow{r}{f} & Z
      \end{tikzcd}
    \]
    where $\pi_i$ are the projection inherited from $X\times Y$. It is easy to check that, for any topological space $Q$ and any $f':Q\to X, g': Q \to Y$ such that
    \[
      \begin{tikzcd}
        Q\arrow{r}{f'}\arrow[swap]{d}{g'} & Y\arrow{d}{g}\\
        X\arrow{r}{f} & Z
      \end{tikzcd}
    \]
    there is an unique $u:Q\to X\times_Z Y$ that maps $q\mapsto (f'q,g'q)$ for every $q\in Q$.\\
  \item Seifert-Van-Kampen: This theorem is a classic result in Algebraic topology, that can be found in any classical source, such as \cite{munkres2000topology}. It was independently discovered by Seifert \cite{seifert1931konstruktion} and Van-Kampen \cite{van1933connection}.  We present the result as in  Brown's paper\cite{brown1967groupoids}.\\

    We denote by $\pi_1(X,A)$ the fundamental groupoid of the topological space $X$ over the set $A\subset X$. This notion was first showed by Brown. $\pi_1(X,X)$ would be the classical full fundamental groupoid, and $\pi_1(X,x_0)$ for some $x_0\in X$ would be the fundamental group of $X$ over $x_0$. Let us present the result.
    \begin{theorem}[Seifert-Van-Kampen]
      Let the topological space $X$  be covered by the interiors of two subspaces $X_1,X_2$ and let $A$
      be a set which meets each path component of $X_1, X_2$ and $X_0=X_1\cap X_2$, and $i_1: X_0 \to X_i,j_i: X_i \to X $ be the canonical inclusions. Then $A$  meets each path component of $X$  and the following diagram is a pushout in the category of groupoids:
      \[
        \begin{tikzcd}
          \pi_1(X_0,A)\arrow{r}{\pi_1(i_1)}\arrow[swap]{d}{\pi_1(i_2)} & \pi_1(X_1,A)\arrow{d}{\pi_1(j_1)}\\
          \pi_1(X_2,A)\arrow{r}{\pi_1(j_2)} & \pi_1(X,A).\\
        \end{tikzcd}
      \]

    \end{theorem}

  \end{itemize}
\end{example}

Note the  similarity of the pullback/pushout and the one of product/coproduct notation-wise. To understand these we have to consider the similarities of both construction. We are going to focus on the similarities of product and pullback, letting the coproduct/pushouts as duals.\\

In both case, the universal property consists of having an arrow to a generated object only if we have an arrow to each of its generators. In this line of reasoning, we can consider that the product is a pullback where we forget about the object $z$ and its arrows. One easy way to generate that case is to consider the construction when $Fz$ is a terminal object. In that case we have that the existence of $Ff$ and $Fg$ is a tautology, and we can consider

\[
  \begin{tikzcd}
    & q
    \arrow[bend left]{dr}{q_2}
    \arrow[bend right,swap]{dl}{q_1}
    \arrow[dashed]{d}[description]{u} & & \\
    Fx  & Fx\times_{Fz}Fy \arrow{r}{\pi_1} \arrow{l}[swap]{\pi_0}& Fy \\
  \end{tikzcd}
\]

% having a product structure.  One can proceed to consider an analogous consideration with pushout and coproduct.\\

% One last property that can be expressed in terms of universality, and in particular in terms of limit/colimit is the \emph{power/copower}. The first example of power that we have considered yet of such construction is, given $C$ a small categories an $J$ a discrete category, the category $C^J$ is the power of $C$ up to $J$. The notation is due to the standard tuple notation:  considering a set of  tuples in $\R^n$, is in fact the same as considering the functions from  $n= {0,...,n-1}$ to $\R$.\\

% There are two intuitive notion behind this definition:
% \begin{itemize}
% \item The one more close to the formalism: to consider as a infinite product of the same element.
% \item To consider this
% \end{itemize}

\section{Adjoints}

Adjointness is a fundamental theory within Category theory. It relates the homesets of two functors $F:C\to D, G:D\to C$. The importance of adjoints however, comes from it ubiquity among mathematics, often relate with the ubiquity of universality. This notion was first presented by Daniel M. Kan in \cite{kan1958adjoint}. For this section we follow the material presented in \cite{mac2013categories}. Adjoints are also called Adjunctions.\\

We  will start by providing some intuitive notions, before a formal definition. Take the Forgetful Functor $U:Grp\to Set$, and the Free group Functor $\mathcal{F}:Set\to Grp$. We can see that it is not difficult to consider that we can compose $F$ and $G$ to make endofunctors. These endofunctors are far from being identities, nonetheless we have still a great relation between them: for every set $X$ and group $G$, there is a function $f: X \to UG$ for each morphism $g:FX\to U$. Conversely, any function from $X$ to $UG$ induces a morphism. \\

Any avid reader will detect by this point the taste of universality. But there is a bit more, we have a bijective relation on every image home-set of both $F$ and $G$. This is the underlying property of adjointness - to relate home-sets. Lets formalize this idea:
\begin{definition}
  Let $D,C$ be categories. And adjunction is a triple $(F,G,\varphi):D\to C$ where $F,G$ are functors:
  \[
    \begin{tikzcd}
      D\arrow[r, shift left=.5ex, "G"{name=F}] &
      C\arrow[l, shift left=1ex, "F"{name=G}] \\
    \end{tikzcd}
  \]
  while $\varphi$ is a transformation that maps each $(c,d) \in Ob(C\times D)$ with a bijection:
  \[
    \varphi_{c,d}:\hom_A(Fd, c)\equiv\hom_X(d, Gc).
  \]
  which is natural in $c$ and $d$.
\end{definition}

\begin{remark}
  but we will stick with the notation of $F$ being the \emph{left adjoint} and $G$ being the \emph{right adjoint}, denoted as $F\dashv_\varphi G$ or just $F\dashv G$. When a functor \emph{is} a left (resp. right) adjoint it \emph{has} a right (resp. left) adjoint.\\
\end{remark}
We have a bit to unpack in this definition. We will start to understand that $\varphi:Hom_C(F\cdot, \cdot)\to \hom_D(\cdot, G\cdot)$ is a natural bijection in each variable. That is, for every $f:d\to d'\in C,k:c\to c' \in D$ the following diagrams commutes.
\[
  \begin{tikzcd}
    \hom_C(Fd,c) \arrow{d}{\hom_C(Fd,\cdot)(g)}\arrow{r}{\varphi_{c,d}}
    &\hom_D(d,Gc)\arrow{d}{\hom_D(d,G\cdot)(g)}&
    \hom_C(Fd,c) \arrow{d}{\hom_C(F\cdot,c)(f)}\arrow{r}{\varphi_{c,d}}
    &\hom_D(d,Gc)\arrow{d}{\hom_D(\cdot,Gc)(f)}\\
    \hom_C(Fd,c') \arrow{r}{\varphi_{c',d}}  &\hom_D(d,Gc')&
    \hom_C(Fd,c') \arrow{r}{\varphi_{c,d'}}  &\hom_D(d,Gc')
  \end{tikzcd}
\]

The definition of adjoint can be reworked as:
\begin{proposition}\label{description:adjoint}
  An adjoint can be described as a bijection that send each $f:Fx\to a\in A$ to $\varphi(f):x\to Ga$ such that, for every $h:x\to x'\in X, k: a\to a'\in A$:
  $$\varphi(k\circ f) = Gk \circ \varphi(f), \qquad \varphi(f\circ Fh) = \varphi(f)\circ h.$$
\end{proposition}
\begin{proof}
  These conditions ensures the naturality of the bijection.
\end{proof}
\begin{remark}
  It is equivalent to require $\varphi^{-1}$ to be natural.
\end{remark}

This proposition, that can be obviated and is rarely used by itself, is really important as it encapsulate the idea behind the commonplace concepts of \emph{unit} and \emph{counit}. The key reasoning behind these objects is to consider what would happen whenever $f$ is the identity, following the notation of the previous proposition. This provide us of full control of the bijection based only on arrow $\varphi(f)$. This is also the fundamental idea behind proposition \ref{Yoneda-proposition}.\\

We have already suggested the relationship between universality and adjointness. We can summary that relation in the following property:
\begin{proposition}\label{prop:univAdjoint}
  An adjunction $(F,G,\varphi): D\to C $ determines:
  \begin{itemize}
  \item a natural transformation $\eta: I_D \to GF$ such that $\eta(d):d\to GFd$ is universal to $G$ from $d$ for every $d\in Ob(D)$. Conversely, we can define:
    $$\varphi(f:Fd\to c) = Gf\circ \eta(d): d\to G c.$$
  \item a natural transformation $\epsilon: FG\to I_C$ such that $\varepsilon(c):FGc\to c$ is universal from $c$ to $F$ and we can define:
    $$\varphi(g:d\to Gc) = \varepsilon(c)\circ Fg: Fd\to c.$$
  \item We have that the natural transformation $\eta_{G\cdot} \circ G(\varepsilon_\cdot)$ is the identity natural transformation.
  \end{itemize}
\end{proposition}
\begin{remark}
  We can therefore consider an adjoint $(F,G,\varphi)$ as a quintuple  $(F,G,\varphi,\eta,\varepsilon)$. $\eta$ is usually called the \emph{unit} and $\varepsilon$ is called the \emph{counit}.
\end{remark}

\begin{proof}
  \begin{itemize}
  \item Let $a=Fd$, then we have a natural bijection $\varphi$:
    $$\hom_C(a, c)\equiv\hom_D(d, Gc).$$
    By proposition \ref{Yoneda-proposition} we have an universal arrow $\eta_d:d\to Ga=GFd$ is provided, where $\eta_d = \varphi(1_a)$. The function $d\to \eta(d)$ is natural $I_D\to GF$ as the following diagram commutes due to the naturality of $\varphi$:
    \[
      \begin{tikzpicture}
        \node {\begin{tikzcd}[column sep=20mm]
            d\ar[r,"\eta c"]\ar[d,"h"] & GFd\ar[d,"GFh"]\\
            d'\ar[r,"\eta c'"] & GFd''.
          \end{tikzcd}};
      \end{tikzpicture}
    \]

  \item Analogous.
  \item $1_Ga = G(\varepsilon_a)\circ \eta_{Ga}$.
  \end{itemize}
\end{proof}

From this proposition we can see that we may be able to define an adjoint based only on the universal arrows provided. We can summary a few equivalent definition of adjoint:
\begin{proposition}\label{prop:equivdefinition}
  Each adjoint $(F,G,\varphi,\eta,\varepsilon):D\to C$ is completely determined by the items in any one of the following list:
  \begin{enumerate}
  \item $F,G$ and a natural transformation $\eta:1_D\to GF$ such that $\eta(d)$ is universal for every $d\in Ob(D)$.
  \item $F,G$ and a natural transformation $\varepsilon:FG\to 1_C$ such that $\varepsilon(c)$ is universal for every $c\in \Ob(C)$.
  \item $G$ and for each $d\in Ob(D)$, an object $F_0(d)\in Ob(C)$ and an universal arrow $\nu(x):d \to GF_0 d$.
  \item $F$ and for each $c\in Ob(C)$, an object $G_0(c)\in Ob(D)$ and an universal arrow $\varepsilon(c):c \to GF_0 c$.
  \item  $F,G$ and two natural transformation $\eta: I_D\to GF$, $\varepsilon: FG\to I_C$ such that both composites are identities.
  \end{enumerate}
\end{proposition}

\begin{proof}
  \begin{enumerate}

  \item We need to define the natural bijection $\varphi: \hom_C(F\cdot,\cdot)\to \hom_D(\cdot,G\cdot)$. Let  for each $f:Fd \to c$:
    $$\varphi(g) = Gf\circ \eta(c): d \to Gc$$ Is well defined and it is a bijection  due to $\eta(d)$ universality.\\

    It is natural in $d$ because $\eta$ is natural and is natural in $c$ because $G$ s a functor.
  \item Dual of the previous.
  \item We will proof that there is only one functor with $F_0$ as object function such that $\eta (d): I_D\to GF$ is natural. The naturality and universality told us that each $h:d\to d'\in Ob(D)$ induces two arrows:
    \[
      \begin{tikzcd}
        F_0 d\arrow[dashed]{d} & d\arrow{d}{h}\arrow{r}{\eta (d)}&GF_0d\arrow[dashed]{d}\\
        F_0 d'& d'\arrow{r}{\eta (d')} & GF_0d'
      \end{tikzcd}
    \]
    So the only way to define $Fh= \eta (d) \circ h$. We conclude applying 1.
  \item Dual of the previous.
  \item We use $\eta$ and $\varsepsilon$ to define two functions:
    \[
      \begin{tikzcd}
        \hom_C(Fd,c) \arrow[r, shift left=.5ex, "\tau"{name=F}] &
        \hom_D(d,Gc)\arrow[l, shift left=1ex, "\sigma"{name=G}] \\
      \end{tikzcd}
    \]

    such that $\tau(f) = Gf\circ \eta_d$ and $\sigma(g)=\varpesilon_d \circ Fg$. as both composites are the identity, $\tau \circ \sigma$ and $\sigma \circ \tau$ are the identities. It is clearly natural due to proposition \ref{description:adjoint}.
  \end{enumerate}
\end{proof}
To further illustrate this concept we present some examples:
\begin{example}
  \begin{itemize}
  \item Free category from a graph and forgetful functor:  Given a graph $G$, we can define the free category $\mathcal{F}(G)$ by adding the arrows needed for the graph to satisfy the category axioms, that is:
    \begin{itemize}
    \item $\mathcal{F}(G)$ has as objects all nodes in $G$.
    \item $\mathcal{F}(G)$ has as arrows all identities in $G$, and every finite composition $f=f_1\circ f_2\circ ... \circ f_n$ where $f_i$ is either an identity or an arrow in $G$. Note that the concept of arrow composition in graph can be applied directly from it categorical definition.
    \end{itemize}

    Then, we can define a functor $\mathcal{F}:Graph \to Cat$ that maps every (small) graph $G$ to $\mathcal{F}(G)$ and every graph morphism $\varphi$ to a category morphism, that take
    $$f=f_1\circ f_2\circ ... \circ f_n \mapsto (\mathcal{F}\varphi) f = \varphi (f_1)\circ \varphi (f_2)\circ ... \circ \varphi (f_n).$$

    We can see that we have an adjoint $F \dashv U$ where the unit is given by the insertion of generators.
  \item \v{C}ech compactification\cite{wiki:stonecech}: We have presented in \ref{example:stone-cech} the $\beta: Hauss \to Comp$ Stone-\v{C}ech functor. If we consider the injection of categories $i: Comp \to Hauss$ we can check that is an adjoint $\beta \dashv i$.
  \end{itemize}
\end{example}

% TODO: Lastly, we are going to study the  and prove a theorem that allow us to include parameters in adjoints.

\subsection{Equivalence of Categories}
We can define an isomorphism of categories the same way that we define isomorphisms in any other category: an isomorphism is an arrow (Functor) with a two sided inverse. This definition, while standard, is quite restrictive. We are going to need some more lax concept of equivalence of categories, in the sense that while not being exactly the same, they are mostly the same. Formally:

\begin{definition}
  A functor $F:C\to D$ is an equivalence of categories if there exists a functor $G:D\to C$  and two natural isomorphisms $\varphi: I_C \to GF$ and $\zeta: I_D\to FG$
\end{definition}

For the canonical example we may introduce a really organic concept: the \emph{skeleton} of a category. Quite often in mathematics we do not consider all objects of certain type, but rather objects up to isomorphism. The skeleton category of a category $C$ is another category where we consider the objects up to isomorphism. Formally:
\begin{definition}
  Let $C$ be a category. The skeleton of $C$, namely $ske(C)$, is a subcategory  such that every object in $C$ is isomorphic to an object in $ske(C)$.
\end{definition}

The definition of equivalence of categories previously outline will allow us, for example, to consider $C\sim ske(C)$, while not being isomorphic. Continuing our discussion, it is not difficult to draw similarities between equivalences and adjoints. That motivates the further definition:
\begin{definition}
  Let $(F,G,\varphi,\eta,\varepsilon)$ be an adjoint. It is called an \emph{adjoint equivalence} whenever $\eta$ and $\varepsilon$ are natural equivalences.
\end{definition}
It is clear that every adjoint equivalence induces two equivalences in $F$ and $G$. We state this idea in the following proposition:
\begin{proposition}\cite[Theorem 1, 4.4]{mac2013categories}
  Let $F:C\to D$ be a functor. Then the following properties are equivalent:
  \begin{itemize}
  \item[i)] $F$ is an equivalence of categories.
  \item[ii)] $F$ is part of an adjoint equivalence.
  \item[iii)] $F$ is full and faithful, and each object $d\in D$ is isomorphic to $Fc$ for some object $c\in C$.
  \end{itemize}
\end{proposition}
\begin{proof}\
  \
  \begin{itemize}
  \item[$ii)\implies i)$] Given an equivalent adjoint $(F,G,\varphi,\eta,\varepsilon)$, then $F$ is an equivalence with $G,\eta,\varepsilon$.
  \item[$i)\implies iii)$] $\varphi: GF\equiv I_C$ implies that for every $c\in Ob(C)$ we can consider $c\equiv GFc$. Then considering the natural isomorphism $\zeta: FG\equiv I_D$ states for every $f:a\to a'\in D$
    \[
      \begin{tikzcd}
        FGa \arrow{d}{FG f}& a\arrow{d}{f}\arrow{l}{\zeta^{-1} a}\\
        FGa' \arrow{r}{\zeta a'}& a'
      \end{tikzcd}
    \]

    there is one $FG f$ for each $f$ and we get that $G$ is faithful. Faithfullnes of $F$ can be prove symmetrically. To see that $G$ is full we can consider any $h:Ga\to Ga'$ and defining $f=\zeta a\circ h \circ \zeta a'$ we follow hat $FG f = Gh$. As $T$ is faithfully, $Gf=h$ so that $G$ is full. Proceed symmetrically with $F$.
  \item[$iii)\implies ii)$]

    We need to construct $G$ so that $F$ is a left adjoint. The idea is to define $\eta$ and apply the previously developed characterizations.\\

    Due to the fully faithfulness of $F$ we can choose for every $d\in D$ an object $c\in C$ such that there is an isomorphism $\eta d: d \to Fc$ and for each $c'\in C, f:d\to Fc'\in D$ there is some $g\in Ar(C)$ (that exists because $F$ is full and is unique because of faithful) such that:
    \[
      \begin{tikzcd}
        d\arrow[leftrightarrow]{r}{\eta d}\arrow[swap]{dr}{f}& Fc\arrow{d}{Fg}\\
        & Fc'.
      \end{tikzcd}
    \]

    We define $G_0d = c$. Also note  that $\eta d$ is universal from $d$ to $F$. Therefore, $F$ is part of an adjoint by proposition \ref{prop:equivdefinition} $F$ is part of an adjoint $(F,G,\eta, \varepsilon)$. As with every adjoint, $F (\varepsilon c)\circ \eta (Fc) =1_{Gc}$. Thus $F(\varepsilon c)$ is invertible and by  fully faithfulness of $F$ so is $\epsilon c$, thus having an adjoint equivalence.

    % We name $\zeta: Ob(D)  \to Ob(C)$ to the mapping  such that $d\equiv F(\zeta d = c)$. Naming $\eta d: d \to c$ to the denoted isomorphism, we can see that
    % \[
    %   \begin{tikzcd}
    %     c=Fd& d\arrow[swap]{l}{\eta d}\arrow{d}{f}\\
    %     & d'
    %   \end{tikzcd}
    % \]
    % Now, we can apply $G$ to the diagram, and as it is faithfull, the isomorphism will be maitained it will be maintained (see example \ref{example:mitchell} for more on this idea):

  \end{itemize}
\end{proof}

In the next few subsection, we are going to introduce categories, with some additional structures onto them. This type of considerations, are commonplace in category theory, and will provide useful considerations.

% \subsection{Monad}

\subsection{Closed Cartesian Categories}\label{subsect:CCC}

The notion of product seen in \ref{prod-univ} is a notion that seek to catch the essence of what the Cartesian product is in the category of Set. In this category it happens that for any two objects, one can consider the product without any hesitance about it existence. A \emph{Cartesian category} will be a category that, in some sense, maintain this property (of being closed under Cartesian product). Formally:
\begin{definition}
  A Cartesian category is a category with a specified terminal object $T$ and for which every finite product exists.
\end{definition}
\begin{remark}
  Finite product are equivalent to binary product.
\end{remark}

Nonetheless, further on the text we will need categories with some even nicer properties. These categories are called \emph{closed Cartesian categories}. We will define them formally first, and after unpack the information that the definition contains:

\begin{definition}\label{def:CCC}
  A $C$ is closed Cartesian category, CCC for short, if each of the following functors:
  \begin{center}
    \begin{tabular}{p{0.3\linewidth}p{0.3\linewidth}p{0.3\linewidth}}
      $\qquad F_1:C\to 1$&$ F_2=\Delta: C \to C\times C $&$ F_{3}^b:C \to C$\\
      $\qquad \qquad c\to e$&$ \qquad \qquad c \to  (c,c)$&$ \qquad c \to c\times b$\\
    \end{tabular}
  \end{center}
  has a \emph{specified} right adjoint, denoted by:
  \begin{center}
    \begin{tabular}{p{0.3\linewidth}p{0.3\linewidth}p{0.3\linewidth}}
      $\qquad G_1:1\to C$&$ G_2:  C\times C\to C $&$ G_{3}^b:C \to C$\\
      $\qquad \qquad e\to t$&$ \  \qquad (c,b)\to c \times b$&$ \qquad c \to c^b$\\
    \end{tabular}
    where there is one $F_3^b\dashv G_3^b$ for every $b\in \Ob(C)$.
  \end{center}

  These adjoints provide us with a lot of information:
  \begin{itemize}
  \item From $F_1$ being an adjoint we get that $t$ is terminal as for every $c\in Ob(C)$:
    $$\{id_e\} \equiv \hom_1(F_1(c), e) \equiv \hom_C(c,Ge=t)   $$
  \item From $F_2$ we get that every pair $c,b$ has a product at is states the universal property of product:
    $$\hom_{C\times C}(F_2(c), (c',b'))\equiv \hom_{C\times C}((c,c), (c',b')) \equiv \hom_C(c,c'\times b')$$
    that is, for every object in $c\in C$, to provide a morphism to $c\to c'\times b'$ is the same that providing to morphisms $c\to c',c\to b'$.
  \item First of all, as every product exists, and it specified by $F_2$, we can define $F_3^b$ for every $b$. Then, it adjointness states that:
    $$\hom_C(c\times b, d) \equiv \hom_C(c,d^b)   $$

    This object $d^b$ is called the exponential object or map object. Note that to fully define the adjoint we can take one of two steps,  the functor $G_3^b$ is defined only on the objects:
    \begin{itemize}
    \item We can define how $G_3^b$ acts on arrows.
    \item We can  provide an arrow $\varepsilon$:
      $$\varepsilon: F_3^bG_3^b(c) = c^b\times b \to c = I(c)$$
      that is natural in $c$ and universal from $F_3^b$ to $c$.
    \end{itemize}

    The intuitive notion is that this object represent in some way the arrows from $b$ to $c$, being a generalization of the function set of two small sets. The best way to understand this adjoint is by the natural transformation $\varepsilon^b(c^b\times b) = c$. In this context, this is called \emph{evaluation arrow}. In the context of $Set$ this amount to the classic evaluation of a function.\\

  \end{itemize}
\end{definition}

\begin{remark}
  Note that a Closed Cartesian category grow in the notion of a Cartesian category. It is in fact a Cartesian category on which we have a function object, with an evaluation arrow that is universal.
\end{remark}

Cartesian Closed Categories will be reformulated in Chapter 4, proposition \ref{def2:CCC}. We will use this last ideas in other to equationally present the concept. For now on, lets provide some examples of Cartesian Closed Categories:

\begin{example}
  \begin{itemize}
  \item $Set$. We have the set $\{*\}$ as a terminal object, products as in example \ref{example:prod}. Given two sets $C, B$ we can define the exponential object $C^B= \hhom (B,C)$. The adjointness is then implied by the currying process:
    $$g: A\times B \to C \mapsto f: A \to B^C,$$
    where $f(a)(\cdot) = g(a,\cdot)$.
  \item We will prove in chapter \ref{chap:4} that $\lambda$-calculus is, in some sense, a closed Cartesian category.
  \end{itemize}
\end{example}

We can consider the duals:

\begin{definition}
  A \emph{Cartesian} category is a category with a specified initial object $\bot$ and for which every finite coproduct exists. A \emph{bi-Cartesian} category is a category that is both Cartesian and cocartesian.
\end{definition}

\begin{definition}A \emph{closed bi-Cartesian category} is a bicartesian category that is also a closed Cartesian category.
\end{definition}

As with almost every structure, a category can be deduced is the one of the objects along with structure-preserving functors.

\begin{definition}[Category of closed Fartesian categories]
  We can define the \emph{category of closed Cartesian categories} $Cart$ that has as objects all small closed Cartesian categories, and as objects all functors that preserve the specified terminal, products and exponential objects.
\end{definition}

The morphism of closed Cartesian categories are often called \emph{closed Cartesian functors.}


\section{Theoretical Background}

Moment of inertia of a rigid body about an axis is a quantitative
characteristics that defines the body’s resistance (inertia) to a change of
angular velocity in rotation about that axis.
This characteristics of the rigid body rotating about a fixed axis is determined
not only by the mass of the body, but also by its distribution.
The moment of inertia of a rigid body about a certain rotation axis can be
calculated analytically.
However, if the body has irregular shape or non-uniformly distributed mass, the
calculation may be di cult.
Experimental methods turn out to be more useful in such cases.

\subsection{Laws of Physics Used}

There are mainly two laws of Physics been used in the experiment. Second law of
dynamics for rotational motion can find the relationship between the rotational
acceleration and the torque at the object.

\subsubsection{Second Law of Dynamics for Rotational Motion}

The rotational motion about a fixed axis relates with the component of the
torque about the axis of rotation with the moment of inertia about this axis.
$$ \tau_z = I\beta_z$$
Therefore, the moment of inertia I can be found once the torque and the
resulting angular acceleration are measured.

The moment of inertia is an additive quantity, the moment of inertia of the
combined rigid body AB composed of A and B, about the same axis of rotation, is
$$ I_{ab} = I_A + I_B $$

\subsubsection{Parallel Axis Theorem}
If the moment of inertia of a rigid body with mass m about an axis through the
body’s center of mass is I0, then for any axis parallel to that axis, the moment
of inertia is
$$ I = I_0 + md^2 $$
where $d$ is the distance between the axes.


\chapter{Multivariate normality}
\label{mvn}

The multivariate normal distribution remains central to most work concerning multivariate continuous data.   Experience has suggested that it is usually at least an acceptable approximation, and of course one usually has recourse to the central limit theorem.   Although we will examine a few common alternatives, it is perhaps in the field of robust statistics where it's use has been modified most.

\section{Expectations and moments of continuous random functions}
\label{meancovar}

The mean and covariance can be defined in a similar way to to the univariate context

\input{defs/mvexpectation}

\section{Multivariate normality}
\label{mvndetail}

\input{defs/mvnpdf}

And it can be shown that $E(\boldsymbol{x}) = \boldsymbol{\mu}$, and that $Var(\boldsymbol{x}) = \boldsymbol{\Sigma}$, hence we can use the notation:

\begin{displaymath}
\boldsymbol{y} \sim MVN_{p}(\boldsymbol{\mu}, \boldsymbol{\Sigma})
\end{displaymath}

Finding the maximum likelihood estimators for $\boldsymbol{\mu}$ and $\boldsymbol{\Sigma}$ is not trivial, there are perhaps at least three derivations.   We briefly recap results from one of the more popular derivations here.

\input{defs/mvnlike}

\subsection{\textbf{R} estimation}

\verb+cov()+ and \verb+var()+ (equivalent calls) both give the unbiased estimate for the variance-covariance matrix, i.e. $\frac{1}{n-1} \sum_{i=1}^{n}(\boldsymbol{x}_{i} - \bar{\boldsymbol{x}})^{T}(\boldsymbol{x}_{i} - \bar{\boldsymbol{x}})$.   It is worth nothing that by default, \verb+cov.wt()+ (which will do a few other things as well) uses the divisor $\frac{1}{n}$

%\section{Skewness}
%\label{skewness}

%\section{Outliers}
%\label{outliers}

%\section{Missing Data}
%\label{missing}

\section{Transformations}
\label{transform}

\cite{Box+Cox:1964} modified earlier proposals by \cite{Tukey:1957} to yield the following transformation:

\input{defs/boxcox}

\input{defs/boxcoxmle}

A range of tranformations have been considered for multivariate data, mainly of the Box-Cox type.   If the variables $\boldsymbol{y} = (y_{1}, y_{2}, \ldots, y_{p}$ are are smooth transformation of $\boldsymbol{x}$, the frequency function for $\boldsymbol{y}$ can be given by:
\begin{displaymath}
g(\boldsymbol{y}) = f(\boldsymbol{x}(\boldsymbol{y}))\lvert \frac{\partial \boldsymbol{x}}{\partial \boldsymbol{y}} \rvert
\end{displaymath}
where $\boldsymbol{x}(\boldsymbol{y})$ is $\boldsymbol{x}$ expressed in terms of the elements of $\boldsymbol{y}$, and $J = \lvert \frac{\partial \boldsymbol{x}}{\partial \boldsymbol{y}} \rvert$ is the Jacobian % determinant of partial derivatives
which ensures the density is mapped correctly.

$\prod_{j=1}^{p} \prod_{i=1}^{n} x_{ij}^{\lambda_{j}-1}$

\input{defs/boxcoxmvmle}

%%% Local Variables: ***
%%% mode:latex ***
%%% TeX-master: "../book.tex"  ***
%%% End: ***


\chapter{The ORBM: A model of independent complex causes}

\section{A network equivalent to an RBM}

\subsection{Unrolling a Gibbs Chain, a different perspective on RBM inference}

\begin{wrapfigure}{r}{0.6\textwidth}
  \begin{center}
    \includegraphics[width=0.5\textwidth]{Assets/3_Layer_RBM.png}
  \end{center}
  \caption{A diagram illustrating `unrolling` an RBM by one Gibbs iteration. Note the connections between $H$ and $V$ layers are now directed.}
  \label{F:3-Layer-RBM}
\end{wrapfigure}

Before the ORBMs architecture and inference algorithm can be introduced, Gibbs sampling in an RBM must be presented in a different, yet equivalent way.
Hinton, when introducing the idea of training a Deep Belief Network showed that a Gibbs chain in an RBM is equivalent to a infinitely deep belief network, with an RBM on the top with tied weights~\cite{hinton2006reducing}. An unrolling of a single Gibbs iteration is illustrated in figure \ref{F:3-Layer-RBM}, the $U$ layer corresponding to the $V$ layer after one Gibbs iteration. The top two layers ($U$ and $H$) form a standard RBM, but the bottom connections ($V$ and $H$) form a Sigmoid Belief Network. To further clarify, the $U$ layer corresponds to $V_1$ in figure \ref{F:Gibbs_Chain}. Note in figure \ref{F:3-Layer-RBM} the weights between the $H$ and $U$ layer are shared between the $V$ and $H$ layers.

We can now show that Gibbs sampling in this RBM unrolled with a Sigmoid belief network is equivalent to Gibbs Sampling in a standard RBM.\@

\subsubsection{Sampling in this equivalent network}

Sampling in the ORBM network behaves a similar way to RBM sampling, where a Gibbs chain is run between the top two layers, $U$ and $H$, until the last iteration. At the last iteration a hidden pattern is sampled and then is pushed through the Sigmoid belief network between $H$ and $V$. This is illustrated in figure \ref{F:3-Layer-RBM-Gibbs}.

\begin{figure}[h]
\begin{center}
  \includegraphics[width = 0.8\textwidth]{Assets/ORBM-Gibbs-Chain.png}
\caption{A diagram showing sampling in the equivalent network, where normal sampling in the top 2 layers is performed until Gibbs iteration $t$, and the hidden state is pushed down through the bottom layers of the Sigmoid Belief Network.}
\label{F:3-Layer-RBM-Gibbs}
\end{center}
\end{figure}

To generate a sample from $h$ is equivalent we can use equation \ref{eq:Hid-Gibbs-Update}, by running the Gibbs chain between $h$ and $u$. To sample from $v$ in the SBN is by definition:
$$
P(v_i = 1|h) = \sigma_i(h)
$$
Thus Gibbs sampling from a simple RBM ending in a sample for $v$ is equivalent to sampling $h$ from the same RBM and using a SBN for the last step. However another useful way to draw samples in such a network that we will leverage in the ORBM. The log likelihood of the joint can be written as:
\begin{equation}\label{eq:Product-Joint}
\log P^\star(h,v) = \log P^\star(h) + \log P(v|h)
\end{equation}
The second term is defined in equation \ref{eq:SBN-v-given-h}. To find the first term, $P^\star(h)$, we need to marginalise the joint (eq~\ref{eq:Product-Joint}) over all $\mathbf{U}$ layer configurations:

$$
 \begin{aligned}
P^\star(h) &= \sum_{v_1=0}^1 \cdots \sum_{v_n=0}^1 \exp \bigg[  \log P^{\star}(h,v) \bigg] \\
&= \sum_{v_1=0}^1 \cdots \sum_{v_n=0}^1 \exp \bigg[  \sum_i  \sum_j h_j W_{ji} v_i \;\; + \;\; \sum_i W_{0i} v_i \;\; + \;\; \sum_j W_{j0} h_j \bigg] \\
&= \sum_{v_1=0}^1 \cdots \sum_{v_n=0}^1 \exp \bigg[  \sum_i v_i \phi_i(h)  \;\; + \;\; \sum_j W_{j0} h_j \bigg] \\
&= \exp\left[ \sum_j h_j  W_{j0} \right] \;\; \times \sum_{v_1=0}^1 \cdots \sum_{v_n=0}^1 \prod_i \exp\bigg[ v_i \phi_i(h) \bigg] \\
&= \exp\left[\sum_j h_j  W_{j0}\right] \;\; \times \prod_i \bigg( 1 + e^{\phi_i(h) } \bigg) \\
\text{and taking logs,}
\log P^\star(h) &= \sum_j h_j  W_{j0} \;\; +  \sum_i \log \bigg( 1 + e^{\phi_i(h) } \bigg)
\\
&= \sum_j h_j  W_{j0} \;\; + \; \sum_i \phi_i(h) \;  - \; \sum_i \log \sigma_i(h)
\end{aligned}
$$

$\log P^\star(h)$ for the RBM that is the `top layer` has now be defined. Given this, another way to write $\log P^\star(h,v)$ is
\begin{equation}\label{eq:equivalent-rbm-log-joint}
\log P^\star(h,v) = \underbrace{\sum_j h_j  W_{j0} \;\; + \; \sum_i \phi_i(h) \;  - \; \sum_i \log \sigma_i(h)}_{\log P^\star(h)} \;\;+\;\; \underbrace{\sum_i v_i \log \sigma_i(h) + (1-v_i) \log (1 - \sigma_i(h))}_{\log P(v \mid h)}
\end{equation}
By collecting terms and simplifying this matches the earlier form in equation~\ref{eq:LogPJoint}.

\section{A New Approach, The ORBM}

\subsection{Architecture}

Frean and Marsland extend on this idea of a `One Gibbs iteration unrolled RBM'. They propose adding another $U$ and $H$ layers to represent another rich source of data, which then combines with the original $U$ and $H$ layer via a SBN to form the visible layer, $V$. This architecture is illustrated in figure \ref{ORBM-Architecture}, where A and B are used to denote the two independent causes we are modeling. To clarify the ORBMs architecture, there are two RBMs, one for each cause. These RBMs are connected to a SBN into the visible layer, the weights of which are tied to the respective RBMs. Performing inference in this architecture is slightly simpler for performing inference, as the $U$ layers are not involved.

By building on RBMs and an SBN we can leverage existing algorithms and work on RBMs. Also the potential for extending this work to deeper architectures could be possible. Also the architecture supports `plug and play` with the models in that existing RBMs can be plugged into the architecture. The implications of this are exciting in that an RBM that has been trained on hand written digits could be plugged into an RBM that has been trained on a paper texture (the bumps and ridges of paper). Then using the ORBM a `clean` representation of the text and paper could be separated out given an image of a digit on paper.

The ORBM, like the RBM is difficult to evaluate empirically, but the same techniques that can be used to evaluate RBMs can be applied to the ORBM.\@

\begin{figure}[h]
\begin{center}
  \includegraphics[width = 1.2\textwidth]{Assets/ORBM-Full-Architecture.png}
\caption{The full ORBM architecture, where $A$ and $B$ are the two causes that combine to form the data.}
\label{ORBM-Architecture}
\end{center}
\end{figure}

\subsection{Gibbs Sampling in the ORBM}

\subsubsection{Sampling in the ORBM}

Sampling from the generative model in an RBM (Section \ref{S:Gibbs-Sampling-RBM}) involved running a Gibbs chain and then taking the last visible pattern in that chain. To perform Sampling in the ORBM we can leverage the generative power of the two RBMs and then combine their inputs in a simple way.
We know that joint in the unrolled RBM is expressed as eq~\ref{eq:Product-Joint}.
That is, the ORBM probability of $v$ given $h^{A} $ and $h^{B}$ is defined by:
$$ \log P(v|h^A,h^B) = \sum_i v_i \log \sigma (\phi^A_i + \phi^B_i) + (1-v_i) \log (1 - \sigma(\phi^A_i + \phi^B_i)$$
Where $\phi_i^A \text{ and } \phi_i^B $ are the weighted sums into the $ ith$ visible units from the models A and B respectively. In this generative model a visible unit is created by taking the weighted sum from both sources, adding their contribution and then passing through a Sigmoid function to give a probability.

\subsubsection{Inference in the ORBM}\label{S:ORBM-Inference}

In a standard RBM sampling from $P(h_j=1)$ is given by $P(h_j=1) = \sigma(\psi_i)$, where $\psi_i$ is defined in equation \ref{psi-gibbs-update-rbm}. In an ORBM we cannot consider the single RBM alone when trying to find $P(h_j)$, as the hidden layers of the two RBMs in the ORBM are dependent given a visible layer $v$. This amounts to explaining away as described in section \ref{S:Explaining-Away}. There is a nice feature of the ORBM in that the hidden representations ($ h^A \text{ and } h^B $) we extract from the visible layer require no interaction with the $U$ layers to sample from. The $ U $ layers are only needed to generate a composite $ V $ pattern (or independent reconstructions).

We aim to use Gibbs sampling to generate $ h^A \text{ and } h^B $ given a $ v $:
We need to calculate
$$
\psi^A_j = \log P^\star(h,v | h^A_j = 1) - \log P^\star (h,v| h^A_j = 0)
$$
We will use that fact that the weighted sum into a visible unit $i$ where some hidden unit $h_j$ is on, is equivalent to the same configuration except $h_j$ is off plus the weight between these two units. This is by definition true, and expressed below:
$$
\phi^A_i(h | h^A_j=1) = \phi^A_i(h | h^A_j=0) + W^A_{ji}
$$
We will abbreviate $\phi^A_i(h | h_j=0)$ to $\phi^{Aj0}_i$. Given these we obtain:
$$
\psi^A_j = \sum_i v_i \log \left( \frac{1+ e^{-\phi^{Aj0}_i - \phi^B_i}}{1+e^{-\phi^{Aj0}_i - W_{ji} -\phi^B_i}} \frac{1+ e^{\phi^{Aj0}_i + W_{ji} + \phi^B_i}}{1+e^{\phi_i^{Aj0} + \phi^B_i}}\right) \;\;+ \;\;\sum_i \log \left(\frac{1+e^{\phi_i^{Aj0} + W_{ji}}}{1+ e^{\phi_i^{Aj0}}}
\frac{1+e^{\phi_i^{Aj0} + \phi^B_i}}{1+ e^{\phi_i^{Aj0} + W_{ji} + \phi^B_i}} \right)
$$

Now $\phi = \log \frac{1+e^{\phi}}{1+e^{-\phi}}$, which is$ = \log \frac{\sigma(\phi)}{\sigma(-\phi)}$.
So the first term simplifies to
$ \sum_i v_i W_{ji}$, which is the same as that in an RBM. The second term can also be simplified, using the identity $\log(1-\sigma(\phi)) = \phi - \log(1+e^\phi)$. This leads to the following Gibbs Sampler probability of the j-th hidden unit in network $A$ being 1: $p_j = \sigma(\psi_j^A)$ with

$$
\psi_j^A = \underbrace{\sum_i W^A_{ji} v_i}_\text{vanilla RBM} \; + \; \underbrace{\sum_i C^A_{ji}}_\text{correction} $$
where the `full` Correction is:
\begin{equation}\label{eq:Full-Corretion}
\begin{aligned}
C^A_{ji} \; &= \;\log \bigg[ \frac{\sigma (\phi_i^{Aj0})}{\sigma (\phi_i^{Aj0} + W^A_{ji})} . \frac{\sigma (\phi_i^{Aj0} + W_{ji}^A + \phi_i^B) }{\sigma (\phi_i^{Aj0} + \phi_i^B)} \bigg]
\\
&= \log \sigma(\phi_i^{Aj0})  \; + \; \log \sigma (\phi_i^{Aj0} + W^A_{ji} + \phi_i^B) \;- \log \sigma (\phi_i^{Aj0} + W^A_{ji})  \; - \; \log \sigma ( \phi_i^{Aj0} + \phi_i^B)
\\
&= \log \bigg[ \frac{\sigma(\phi_i^{A} - h^A_i W^A_{ji})}{\sigma (\phi_i^{A} + (1-h^A_i) W^A_{ji})} \bigg]  \; - \; \log \bigg[ \frac{ \sigma ( \phi_i^{AB} - h^A_i W^A_{ji})}{\sigma (\phi_i^{AB} + (1-h^A_i) W^A_{ji})} \bigg]
\end{aligned}
\end{equation}
where $\phi_i^{AB} = \phi_i^{A} + \phi_i^{B}$. Note that $v$ plays no role in the correction. It is clear that adding $\phi^B_i$ has introduced a dependency between the entirety of $h$. This means that a Gibbs chain will need to be run, in practice this chain proves to be short, even in the larger dimension tasks like MNIST.

\subsubsection{Examining and approximating the Correction}

This `full' correction (eq \ref{eq:Full-Corretion}) is a non-trivial computation to make in that for every weight, a series of semi-large matrix operations have to be performed. As a result, Frean and Marsland propose an approximation for this `full' correction.

It is useful to see the correction for the $A$ model as contours on axes $\phi^A_i$ versus $\phi^{AB}_i$, for a positive weight $W^A_{ij}$.
There are two plots for the two cases $h^A_j=0 \text{ and } h^A_j = 1$ These are plotted in figure \ref{F:Correction-Plot}. This function has defined `plateaus` where the height of these plateaus is $\pm $ the weight respectively. The correction essentially adjusts the weight between $v_i$ and $h_j$ allowing it to be turned off, turned on, intensified, de-intensified, or have no effect. This is similar to adjusting the visible activation based on what the two RBMs are doing and hence Frean and Marsland propose the following approximation:

\begin{equation}\label{eq:Approx-Correction}
 C^A_{ji} = \sigma(\phi^{A_{j0}}_i + W^A_{ji}) - \sigma(\phi^{A_{j0}}_i W^A_{ji} + \phi^B_i)
\end{equation}

\begin{figure}[h]
\begin{center}
  \includegraphics[width = 0.8\textwidth]{Assets/correction.png}
\caption{A diagram that shows the structure of the correction over the weighted sums into the hiddens of one of the RBMS versus both. Note the plateaus that are $\pm weight$ in magnitude}
\label{F:Correction-Plot}
\end{center}
\end{figure}

\subsubsection{What happened to the biases?}\label{S:Biases}

The ORBM utilises the hidden biases present on the RBMs when calculating the hidden states $h^A$ and $h^B$. However, the visible biases are not used in sampling from the visible. The reasoning behind this being that the RBMs visible bias acts like the `background rate`, i.e. what visible reconstruction would we see from an all zero hidden activation. As visible bias are not captured in the ORBM, it is important that RBMs plugged into it's structure do not use a visible bias when being trained.

\subsubsection{Reconstructions and Dreams in the ORBM}

Reconstructions in the ORBM are a natural extension of the inference algorithm.
\begin{itemize}
  \item First hidden representations $h^A$ and $ h^B $ are generated given an input $v$.
  \item The RBMs (RBM A and B) use their respective hidden representations to generate visible patterns independently. That is, we can use the same way of performing Gibbs sampling we saw in equation \ref{eq:Vis-Gibbs-Update}.
\end{itemize}
This means that for a visible pattern there are potentially two reconstructions, one from each model.


\chapterimage{chapter_head_2.pdf}

\chapter{Objects and Data Structures}

\section{Data Abstraction}\index{Objects and Data Structures!Data Abstraction}

Data Abstraction represents more than just a data structure. The methods enforce an access policy. For example:

\begin{tcolorbox}[breakable, colback=red!10!white, colframe=red!85!black]
\begin{lstlisting}[language = java, basicstyle=\small]
public interface Point {
    double getX();
    double getY();
    void setCartesian(double x, double y);
    double getR();
    double getTheta();
    void setPolar(double r, double theta);
}
\end{lstlisting}
\end{tcolorbox}

You can read the individual coordinates independently, but \textbf{you must set the coordinates together as an atomic operation}.

\section{\textcolor{red}{Data/Object Anti-Symmetry(VERY IMPORTANT)}}\index{Objects and Data Structures!Data/Object Anti-Symmetry}

We know Java is an \textit{Object Oriented Language} as opposed to \textit{Procedure Language}, such as C. Let's start with 3 data structures and 1 procedure used in procedure language. Listing~\ref{object-and-data-structure-procedure-pair} show the difference between objects and data structures.

\begin{tcolorbox}[breakable, colback=green!10!white, colframe=green!85!black, sidebyside, label = object-and-data-structure-procedure-pair]

\begin{lstlisting}[language = java, basicstyle=\small]
public class Square {
    public Point topLeft;
    public double side;
}

public class Rectangle {
    public Point topLeft;
    public double height;
    public double width;
}

public class Circle {
    public Point center;
    public double radius;
}
\end{lstlisting}

\tcblower

\begin{lstlisting}[language = java, basicstyle=\small]
public class Geometry {
    public final double PI = 3.141592653589793;

    public double area(Object shape) throws NoSuchShapeException {
        if (shape instanceof Square) {
            Square s = (Square)shape;
            return s.side * s.side;
        } else if (shape instanceof Rectangle) {
            Rectangle r = (Rectangle)shape;
            return r.height * r.width;
        } else if (shape instanceof Circle) {
            Circle c = (Circle)shape;
            return PI * c.radius * c.radius;
        }
        throw new NoSuchShapeException();
    }
}
\end{lstlisting}
\end{tcolorbox}

Objects hide their data behind abstractions and expose functions that operate on that data. Data structure expose their data and have no meaningful functions.

The Geometry class operates on the three shape classes. The shape classes are simple data structures without any behavior. All the behavior is in the Geometry class.

Consider what would happen if a \inlinecode[green]{perimeter()} function were added to \inlinecode[green]{Geometry}, you notice the following:

\begin{itemize}
    \item The shape classes would be unaffected. Any other classes that depended upon the shapes would also be unaffected.
    \item On the other hand, if I add a new shape, I must change all the functions in \inlinecode[green]{Geometry} to deal with it.
\end{itemize}

Now hold the points above in your memory for just a moment and let's consider the object-oriented solution in Listing~\ref{object-and-data-structure-oo-solution}

\begin{tcolorbox}[breakable, colback=green!10!white, colframe=green!85!black, label = object-and-data-structure-oo-solution]
\begin{lstlisting}[language = java, basicstyle=\small]
public class Square implements Shape {

    private Point topLeft;
    private double side;

    public double area() {
        return side*side;
    }
}

public class Rectangle implements Shape {

    private Point topLeft;
    private double height;
    private double width;

    public double area() {
        return height * width;
    }
}

public class Circle implements Shape {

    public static final double PI = 3.141592653589793;

    private Point center;
    private double radius;

    public double area() {
        return PI * radius * radius;
    }
}
\end{lstlisting}
\end{tcolorbox}

When \inlinecode[green]{perimeter()} function is added to \inlinecode[green]{Geometry}, you notice the following:

\begin{itemize}
    \item The shape classes would be affected
    \item On the other hand, if I add a new shape,  \inlinecode[green]{Geometry} is not changed
\end{itemize}

\textit{\textbf{Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. OO code, on the other hand, makes it easy to add new classes without changing existing functions.}}

So, the things that are hard for OO are easy for procedures, and the things that are hard for procedures are easy for OO.

In any complex system there are going to be times when we want to add new data
types rather than new functions. For these cases objects and OO are most appropriate. On the other hand, there will also be times when we'll want to add new functions as opposed to data types. In that case procedural code and data structures will be more appropriate.

Mature programmers know that the idea that everything is an object is a myth. Sometimes you really do want simple data structures with procedures operating on them.

\begin{marker}
When we want to add new data types rather than new functions, Objects and OO are better

However if we want to add new functions, procesure code and data structure will be more appropriate

Mature programmers know that the idea that everything is an object is a myth. Sometimes you really do want simple data structures with procedures operating on them
\end{marker}

\section{The Law of Demeter}\index{Objects and Data Structures!The Law of Demeter}

A module should not know about the innards of the objects it manipulates. An object should not expose its internal structure through accessors because to do so is to expose, rather than to hide, its internal structure.

\begin{tcolorbox}[breakable, colback=green!10!white, colframe=green!85!black, center title, title = Law of Demeter]
\begin{center}
A method f of a class C should only call the methods of these:

\begin{itemize}
    \item C
    \item An object created by f
    \item An object passed as an argument to f
    \item An object held in an instance variable of C
    \item The method should not invoke methods on objects that are returned by any of the allowed functions. e.g. final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath(); violates this rule
\end{itemize}
\end{center}
\end{tcolorbox}

\section{Data Transfer Objects}\index{Objects and Data Structures!Data Transfer Objects}

The quintessential form of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO. \textbf{Somewhat more common in Java is the “bean” form}.


\newpage\section{Parallelogram Stuff}

	\theo{http://forumgeom.fau.edu/FG2005volume5/FG200510.pdf}{Maximality of the Area of a Cyclic Quadrilateral}{Among all quadrilaterals with given side lengths, the cyclic one has maximal area.
		\fig{.5}{quad-same_sides-diff_area}{The cyclic quad has the maximal area}
	}

	\prob{https://artofproblemsolving.com/community/c6h1508225_simple_parallelogram_geo}{IOM 2017 P1}{E}{Let $ABCD$ be a parallelogram in which angle at $B$ is obtuse and $AD>AB$. Points $K$ and $L$ on $AC$ such that $\angle ADL=\angle KBA$(the points $A, K, C, L$ are all different, with $K$ between $A$ and $L$). The line $BK$ intersects the circumcircle  $\omega$ of $ABC$ at points $B$ and $E$, and the line $EL$ intersects $\omega$ at points $E$ and $F$. Prove that $BF\parallel AC$.}

	\hl{Simplify}: Make the diagram easier to draw.

	\prob{https://artofproblemsolving.com/community/c6h148830p841255}{USA TST 2006 P6}{E}{Let $ABC$ be a triangle. Triangles $PAB$ and $QAC$ are constructed outside of triangle $ABC$ such that $AP = AB$ and $AQ = AC$ and $\angle{BAP}= \angle{CAQ}$. Segments $BQ$ and $CP$ meet at $R$. Let $O$ be the circumcenter of triangle $BCR$. Prove that $AO \perp PQ.$}

			\figdf{.5}{USATST2006P6}{USA TST 2006 P6, That is a prallelogram}