\chapter{Martin-L\"of theories and Seely's equality}
\label{chap:5}
\thispagestyle{empty}

In this chapter we continue our categorical presentation of the tip theory with the introduction of Martin L\"of's type theory. This type theory, also known as intuitionistic type theory, is initially proposed as a foundation of mathematics. This is really important because it implies a change of mentality, as we do no longer are so focus on formalizing computation systems. \\

This relationship with first-order logic arises by the hand of dependent typing. That is, in Martin Löf's type theory there is a formal structure for generating types dependent on other types. This is used (without needing to know the formalism) nowadays continuously in programming languages. \\

The most interesting part for us will be to study the new structures that arise in the theory of categories to understand this new typing. In particular, we will study local closed Cartesian category structures through hyperdoctrines. \\

The main sources for this chapter are  \cite{seely1984locally}, \cite{martinlof1973intuitionistic}  and \cite{mac2013categories}.

\section{Martin-Löf type theory}

In this section we introduce rather succinctly the foundations of the type theory. This presentation follows the one done in \cite{seely1984locally}, with added insights and remarks form \cite{martinlof1973intuitionistic} and \cite{sep-type-theory-intuitionistic}.

\subsection{First definition of Martin-Löf type theory}

After having previously introduced the calculations, in this one we will introduce Martin-L\"of type theory, ML for short, in a more systematic way, introducing type formation rules, term formation rules, and the equivalence rules. After that, we will introduce the category structure. The main source for this section is \cite{martinlof1973intuitionistic}, with summaries taken from \cite{seely1984locally}.\\

As a founding level of mathematics, we are going to define types and terms as before. The change in emphasis comes from the fact that we will manage the notation of the Curry-Howard from the beginning, as it was done while firstly introduced by Martin-L\"of in \cite{martinlof1973intuitionistic}. In this we will always consider \emph{types as propositions} and \emph{terms as proofs}, and we will made use of this analogy to explain the properties of the theory. Also, as done in $\lambda$-calculus, we should regard also \emph{proofs as objects}, with all the constructions previously done still in place.\\

One last particularity of the Martin-L\"of theory is the fact that we can include details of proofs in the statement of propositions. This will be formalized further on, but to explain why we need to delay the introduction of term and type rules until the definition of type-valued function.

\subsubsection{Terms and types}

The formal system that Martin-L\"of presented consists of a set of rules $a : A$, which, as it could be predicted, mean that a term $a$ is of type $A$. We will always understand this statement as the fact that the proposition $A$ is proven by the proof $a$. We also consider equality between term $a = b$ and equality between types $A = B$. Types will be preferably denoted by $A,B,C...$ and terms by $a,b,c$ characters. After defining the abstract concept of term and types, and before introducing the full term and type forming rule, we will introduce the concept of \emph{variables} and \emph{function constants}.\\

Variables are defined analogously as in the case of simply typed lambda calculus. The statements $x: A$ represents arbitrary object of a given type. As previously, an statement of $x: A$ where $x$ is a variable is called an \emph{assumption}. We will also annotate the type of variables as super-indices, $x^A$. As in simply typed lambda calculus, a term $a$ may depend on variables $x_1,...,x_n$, usually noted by $a(x_1,...,x_n)$.Variables will be preferably denoted by $x,y,z$ characters. Further on, each time we talk about any generic type or term dependent on variables, we will always suppose the it follows the condition on variables.\\

Quite importantly, as types can depend on variables, and each variable should have a type, we need to impose the so-called \emph{condition on variables}. This states that in the statement $a(x_1,..,x_n)$ it should happens that no variable $x_j$ depends on variables $x_i$ for any $i<j$. The results of substituting $x_1:A_1,...,x_n:A_n$ with $a_1:A_1,...,a_n:A_n$ is denoted by $a(a_1,...,a_n)$. Note that in this last expression, $A_n$ can depend on $x_1,...,x_{n-1}$, thus noted $A[x_1,...,x_{n-1}]$. \\

Note that types can depend on variables, that is, on terms. However, they do not depend on other types directly. This is the key difference with simply typed lambda calculus, in which the terms may depend on variables, but the the types cannot.\\

A function constant is a function from terms to terms. Each function constant $f$ has associated an index $n$ with the number of argument, and also $n$ types symbols $$A_1, A_2[x_1^{A_1}],...,A_n[x_1^{A_1},...,x_1^{A_{n-1}}].$$ A function $f$ of index $n$ is called to be an \emph{$n$-ary function constant}. A 0-ary function is called a \emph{constant}.  Function constants are preferably denoted by $f,g,h$ characters. \\

In addition to function constants, we can consider \emph{type valued function constants}. This have the same indication as above, and a 0-ary type valued function constant is called a type constant. In the analogy of terms and types with proof and proposition that have previously been discussed, type valued functions constants are \emph{properties}. Type valued function constants are preferably denoted by $F,G,H$ characters. By opposition, function constants are called \emph{term valued function constants}.

\begin{example}\label{example:primeML}
  Let $\N$ be the type of every natural number and the type valued function $P(x)$ be the function that represents the property "being a prime number". We can express the fact that 3 is a natural number as $3: \N$. We can then consider  type $P(3)$ will be the proposition ``3 is a prime number''.
\end{example}

\subsubsection{Full typing structure}
Now we are in place to describe the formation rules.

\begin{definition}[Type formation rules, definition 1.1.1 \cite{seely1984locally}]
  The following are to be types:
  \begin{enumerate}
  \item 1 is a type.
  \item If $F(x_1,..,x_n)$ is a type-valued function constant, then $F(a_1,...,a_n)$ is a type, with  $x_1:A_1,...,x_n:A_n$ and $a_1:A_1,...,a_n:A_n$.
  \item If $a,b$ are terms, then $I(a,b)$ is a type.
  \item If $A$ is a type and $B(x^A)$ are types, then $\Pi x^A. B(x^A)$ and  $\Sigma x^A. B(x^A)$ are types.
  \end{enumerate}
\end{definition}

\begin{definition}[Term formation rules, definition 1.1.2 \cite{seely1984locally}]\label{1.1.2-seely}
  The following are to be terms of the indicated type:
  \begin{enumerate}
  \item $*:1$.
  \item For every type $A$, there exists countable many variables $x_i^A : A$, $i\in \N$.
  \item If $f(x_1,..,x_n)$ is a term-valued function constant, then $f(a_1,...,a_n)$ is a term of appropriate type, with  $x_1:A_1,...,x_n:A_n$ and $a_1:A_1,...,a_n:A_n$.
  \item If $t(x^A) : B(x^A)$, then:
    $$\lambda x^A.t(x^A) : \Pi x^A. B(x^A).$$

    Conversely, if $f : \Pi x^A. B(x^A)$ and $a:A$, then  $f(a) : B(a)$ and $(\lambda x^A.t(x^A))(a) : B(a)$.
  \item If $a:A$ and $b:B(a)$, then $\langle a, b\rangle : \Sigma x^a. B(x)$. Conversely, given $c : \Sigma x^a. B(x)$, then $\pi (c) : A$ and $\pi'(c) : B(\pi(c))$.
  \item If $a:A$, then $r(a): I(a,a)$. If $a,b : A$, $c : I(a,b)$, $d : C(a,a,r(a))$, where $C(x^A,y^A,z^{I(a,a)})$, then $\sigma_d[a,b,c] : C(a,b,c)$.
  \end{enumerate}
\end{definition}

\begin{remark}
  Note that we only define terms to be dependent on the same variables as their types. See \cite[Section 2.2]{martinlof1973intuitionistic} for more information.
\end{remark}

We can now explain the intuition behind this structures:

\begin{itemize}
\item We can start with the \emph{Cartesian product of a family of types}, that is denoted by $\Pi x^A. B(x^A)$. In the analogy proposition-types, we have that this represents the statement $\forall x \in A : B(x)$. Similarly, a proof of such a statement is a function that takes an arbitrary object $a:A$ and returns a proof for $B(a)$, and thus we define the terms of this types as lambda terms of the form $\lambda x^A.t(x^A) : \Pi x^A. B(x^A).$.\\

\item We follow explaining the \emph{disjoint union of a family type}, that is denoted by the statement $\Sigma x^A. B(x^A)$ and represents the statement $\exists x \in A : B(x)$. As this kind of statements are proven by providing an object and a proof of such statement, we thus define the terms of this type as pairs $\langle x^A, y^{B(x^A)}\rangle$ of an element and its associated proof. As every pair type, it has projections $\pi,\pi'$. This type is also understood as the type of every element of $A$ such that $B(x)$ holds.
\end{itemize}
\begin{example}
  The set of every real number is:

  $$\R := \left(\Sigma x^{\N\to\QQ}\right).\left(\Pi n^\N\right).\left(\Pi m^\N\right).\left(|x_{m+n} - x_m| \le 2^{-m}\right),$$

  where using the density properties of $\QQ$ in $\R$, we can define the property of being a real number as the type.
\end{example}

In the particular case of $B(x^A)$ having the same type $B$ for every $a:A$, we denote:
$$A\to B:= \Pi x^A. B(x^A),\qquad A\times B := \Sigma x^A. B(x^A).$$

Finally, as a major difference with the previously introduced theories, we can consider the identity type $I(a,b)$, that represent tje  proposition of $a$ and $b$, now regarded as terms, are identical. The term $r(a) : I(a,a)$ introduced in the term formation rules represents a proof of reflexivity of $a$.

\begin{remark}
  As it happens in the previous chapter, we can consider an expansion with the sum type of $A+B$, as is presented in \cite[Section 1.6]{martinlof1973intuitionistic}. In the shake of brevity, we avoid it as it is not as interesting from a categorical viewpoint.
\end{remark}

\subsection{Equivalences and universes}
\subsubsection{Equivalences}

The most important difference in this exposition of type theory is that now we will not present the differentiated alpha/beta/eta equivalences, but we will present them all together as a single equivalence concept. This is due to two reasons. The first is that we consider that the objective of explaining the intuition and the multiple forms of equality is already satisfied. The second is that our main interest is in the relation to Category Theories, where we always consider all the equivalences together.

\begin{definition}[Equality rules, 1.1.3 \cite{seely1984locally}] \label{def:ml-equality}
  Using the notation from definition \ref{1.1.2-seely} point by point:
  \begin{enumerate}
  \item If $t:1$, then $t=*$.
  \item There is no equality rule imposed on variables.
  \item Every imposed equation in functions constants, translates to every interpretation of the function constant.
  \item $(\lambda x^A.t(x^A))(a) =  t(a)$. Also, we can consider function constants as terms, with the obvious equality $f = \lambda x^a.f(x)$.
  \item $\pi(\langle a,b\rangle) = a$,  $\pi'(\langle a, b\rangle) = b$, $c=\langle \pi(c),\pi'(c)\rangle$.
  \item Given $\sigma_d[a,a,r(a)]=d$, if $f(a,b,c)\in C(a,b,c)$ then $f=\sigma(f(a,a,r(a)))(a,b,c)$. Lastly, if $a(x), b(x):A(x)$ and $t(x) : I(a(x),b(x))$, then $a(x)=b(x)$ and $t(x)=r(a(x))$.
  \end{enumerate}

  Finally, we have the  usual internal coherence rules. That is, is reflexive, transitive and symmetric properties. In addition, if $a=b : A$, and $c:B(x^A)$ then $c(a)=c(b)$ and $a:A, b:B, a=b$ implies $A=B$. Reflexive, transitive and symmetric properties for equivalence $I(\cdot, \cdot)$ can be derived from the rules \cite[1.3]{seely1984locally}.
\end{definition}

% \footnote{. We nonetheless included them as a natural part of the definition of equality to favor readability.}.
\begin{remark}
  After having define the structure, as with typed lambda-calculus in section \ref{section:pureandimpure}, we point out that each system of terms and types that follow these rules is said to be a ML theory.
\end{remark}
\subsubsection{Seely's enlargement}

After having presented the Martin-L\"of as presented originally, in this subsection we will explain the modifications that Seely did to the structure. This modifications appeared in Seely's previous works \cite{seely1977hyperdoctrines}\footnote{Unlike the other works cited, Seely's doctoral thesis was not available digitally. We have only found available a physical version from the Cambridge Library and thus we were not able to read it. We trust its contents to the references made by Seely himself in his other works cited.} and \cite{seely1983hyperdoctrines}. Seely introduced the following definition:

\begin{definition}
  Let $z : \Sigma x^A.B(x)$ be a variable, and $C(z)$ a type that on $z$ but not on $x:A$ neither on $y:B(x^A)$. Then, for every $t(x,y) : C(\langle x, y\rangle)$ there exists $\tilde t (z) : C(z)$, defined by:
  $$\tilde t (z) := t(\pi(z),\pi'(z)): C(z).$$
\end{definition}

The point of this notion is simple, that is to more succinctly denote whenever a variable does not depend on a particular pair of variable but only on the moment when they work together. One particular example of this are the properties assign to satisfiable formulas.

With this definition, maintaining the notation, we also introduce the following equalities:
\begin{itemize}
\item $\tilde t(\langle x,y\rangle) = t(\langle x,y\rangle) $.
\item If $f(z): C(z)$, $t(x,y):C(\langle x,y\rangle)$ and $f(\langle x,y\rangle) = t(x,y)$ then $f=\tilde t$.

\end{itemize}

\subsubsection{Universes}
Now let us have a word on \emph{universes}. Let us begin this discussion with an illustrative example, as introduced by Martin-Löf. Let us imagine that we want to consider the type of sequences of natural numbers as

$$\Sigma x^\N. F(x),$$

where

\[
  \begin{cases}
    F(0):= N_1,\\
    F((s(x)):= F(x)\times \N.
  \end{cases}
\]

Here, $N_1$ is a placeholder type for empty sequences. This definition can be done by recursion on the types. But for this we would need some type $V$ which have to be  closed for the product. Similarly we can argue to have the same recursion property with a closed set for $A \to B$.\\

To consider this type we will introduce the type $V$ which will be called a universe, whose objects will be the types. In addition we will consider a reflection principle that allows us to work with all operations without leaving the universe.  The problem begins with the interpretation of Russel paradox in this context, given by Girard in \cite{girard1972interpretation}. To avoid this problem, we need to avoid messing with the equivalent in ML of the \emph{regularity axiom} in ZFC.\\

Then we consider that $V$ is not a type contained in $V$. Thus we can, similarly to what we did with categories, consider as \emph{small}, all the types within $V$, and $V$ itself and all types which are built from it are \emph{large} types. By iterating this construction we can obtain a sequence:

$$V = V_0 : V_1 : V_2 ...$$

where every $V_i$ is a type from the ``type type'' $V_{i+1}$.

\section{Locally Cartesian closed categories and hyperdoctrines}
\subsection{Locally Cartesian closed categories}
This new typing system will be related to the categorical structure of "locally closed Cartesian categories". Instead of following the classical process of introducing the concept formally so that in the course of a technical demonstration one can see why they are related, we believe that there is extra value in explaining the idea behind the formalism.\\

What is the key reason why ML can be considered as a larger structure? In part this is because we can include information about the terms in the typed ones, giving real information about the needs to make a proof system that underlies the mathematics. With this in mind we have to ask ourselves how we can modify the closed Cartesian Category structure so that it matches with this new typing.  The key idea then comes when considering slice categories (see Definition \ref{def:slice-cat})\footnote{Historically, the actual process was relating first order with LCC, and after that doing the same with ML theories, as done in \cite{seely1977hyperdoctrines}.}.\\

This allow us to consider arrows (terms) as objects (types) in local contexts, being (a bit roughly) translatable to consider objects dependent on arrows.  As many things in category theory, once you have the idea you still have to lay down the structures carefully so that everything relates properly. Let us start with the process.

\begin{definition}
  A category is said to have \emph{finite limits} if it has every limit over any finite category, that is, over any category $J$ with both $Ob(J)$ and $Ar(J)$ finite.
\end{definition}

For the next definition, the reader is invited to remember the definition of slice category in definition \ref{def:slice-cat}, if needed.
\begin{definition}
  A \emph{locally Cartesian closed category}, LCC for short,  is a category $C$ with finite limits, such that for any object $a$ of $C$, the slice category $C/a $ is Cartesian closed.
\end{definition}

\begin{remark}
  If a category $C$ has finite limits, then every category $C/a$ with $a\in Ob(C)$  has also finite limits. This needs not to happen with exponent objects, being this the main quality of LCCs.
\end{remark}
Lastly, we can introduce the category associated to this new structure.
\begin{definition}
  We define the category $LCC$ of all locally Cartesian closed categories along with structure preserving functors.
\end{definition}
\subsection{Hyperdoctrines}

As a tool for dealing with LCC we are going to introduce hyperdoctrines. These were introduced in by Lawvere as part of his influential work \cite{lawvere1969adjointness}. The notion of a hyperdoctrine is essentially an axiomatization of the collection of slices of a locally Cartesian closed category\cite{nlab:hyperdoctrine}.

\begin{definition}
  Let $C$ be a category with finite limits. A $C$-indexed category $P$ is a collection that  consists of:
  \begin{enumerate}
  \item For each $a\in Ob(C)$  a category $P(a)$.
  \item For each $f:a\to b, g:b\to c\in Ar(C)$, a functor $P(f) = f^*:P(B) \to P(A)$, such that $(1_a)^* = 1_{P(a)}$ for every  $a \in Ob(C)$ and $(gf)^*= f^*g^*$.
  \end{enumerate}
\end{definition}
\begin{remark}
  Any category $C$ with finite limits, is self-indexed, with $C(b)=C/b$ and $C(f:b_1\to b_2)=f^*: C/b_2 \to C/b_1$ defined by pullback. That is, given an object $g:a\to b_2 \in C/b_2$, by the pullback diagram:
  \[
    \begin{tikzpicture}
      \node {\begin{tikzcd}
          a \times_{b_2} b_1 \ar[r,"p_2"]\ar[d,"p_1"] & a\ar[d,"g"]\\
          b_1\ar[r,"f"] & b_2.
        \end{tikzcd}};
    \end{tikzpicture}
  \]

  we can define $f^*(g) = p_1$, with $p_i$ as in definition \ref{def:pullback}. This has clear inspirations in topology, as many things in this area, with notation $f^*$ for the contravariant pullback functor given as in Grothendieck's six operations \cite{nlab:six_operations}.\\
\end{remark}

We can see that $P$ is, in summary, an contravariant functor from $C$ to $Cat$. Properties from $(\cdot)^*$ derive from the fact that arrows in $Cat$ are functors. Therefore a $C$-indexes category is just an object of $Cat^C$.\\

\begin{definition}
  A $C$-indexes category $P$ is a hyperdoctrine if:
  \begin{enumerate}
  \item $P(a)$ is closed Cartesian for every $a\in Ob(C)$.
  \item $f^*$ preserves exponential objects for every $f\in Ar(C)$.
  \item $f^*$ has adjoints $\Sigma_f \dashv f^* \dashv \Pi_f$.
  \item $P$ satisfies the Beck condition: if the following diagram is a pullback in $C$, then for any object $\varphi$ of $P(c)$,  we have $\Sigma_k h^*(\varphi) \cong f^*\Sigma_g(\varphi)$.
    \[
      \begin{tikzpicture}
        \node {\begin{tikzcd}
            d\ar[r,"h"]\ar[d,"k"] & c\ar[d,"g"]\\
            a\ar[r,"f"] &   b.
          \end{tikzcd}};
      \end{tikzpicture}
    \]

  \end{enumerate}
\end{definition}
\begin{definition}
  Two $C$-indexed categories $P_1$ and $P_2$ are equivalent if:
  \begin{itemize}
  \item For each $a\in Ob(C)$, $P_1(a)\cong P_2(a)$.
  \item For each $f\in Ar(C)$, $P_1(f)(a)\cong P_2(f)(a)$.
  \end{itemize}
\end{definition}

Note that if $P_1\cong P_2$ as $C$-indexed categories, and $P_1$ is a hyperdoctrine, the $P_2$ is a hyperdoctrine.

\begin{remark}
  Similar to the definition of $f^*$ with the pullback, we can define the functor $\Sigma_f: C/b_1 \to C/b_2$ via post-composition.
\end{remark}

Now we proceed to present and explain the following theorem.

\begin{theorem}\label{theo:theox}
  If $C$ has finite limits, then $C$ is a LCC if, and only if, as a $C$-indexed category $C$ is a hyperdoctrine.
\end{theorem}

This result, as noted in \cites{seely1984locally}, can be deduced from results showed in \cite[Section 1.3]{freyd1972aspects}. Nonetheless, as Freyd never talks about hyperdoctrines, it is a bit blurry. Thus, we now proceed to detail a bit this proof, avoiding some proofs but carefully pointing out where to find their statements. During this discussion $C$ is a category, $a,b\in Ob(C)$ and $f:b_1\to b_2\in Ar(C)$.\\

\begin{definition}[Section 2, \cite{nlab:reflective_subcategory}]
  Definition 2.1. A full subcategory $i:C\hookrightarrow D$ is reflective if the inclusion functor $i$ has a left adjoint $T\dashv i$:
  \[
    \begin{tikzcd}
      C\arrow[hookrightarrow, shift left=.5ex, "i"{name=T}]{r}{} &
      D\arrow[ shift left=1ex, "T"{name=i}]{l}{} \\
    \end{tikzcd}
  \]
  The left adjoint is sometimes called the reflector.
\end{definition}

\begin{example}
  $Ab$ is a reflective subcategory of $Grp$, with reflector given by the abelianization of a group.
\end{example}

\begin{proposition}[Section 2, \cite{nlab:reflective_subcategory}]
  In reflective subcategories the inclusion $i:C\hookrightarrow D$ creates all limits of $D$ and $C$ has all colimits which $D$ admits.
\end{proposition}

\begin{theorem}[Proposition 1.31, \cite{freyd1972aspects}]
  Let $A$ be a closed Cartesian categories and $A'\subset A$ be a full reflexive subcategory with reflector  $R$. Then $R$ preserves products iff for all $b,a\in Ob(A')$, we have that $b^a\in A'$.
\end{theorem}
\begin{corollary}
  Let $C$ be a category. We define the functor $\sigma_b : C/b \to C$ as the forgetful functor for every $b\in Ob(C)$. Then $\sigma_b$ preserves and reflects colimits, equalizers, pullbacks and monomorphisms.
\end{corollary}

If $C$ has finite products, we can define the functor $\times_b: C \to C/b$ such that:
\begin{itemize}
\item For each $a\in Ob(C)$, $\times_b(a) := \pi':a\times b \to b$.
\item For each $g:a\to g(a)\in Ar(C)$, $\times_b(f) := g\times 1_{b}: \times b \to g(a)\times b$.
\end{itemize}

\begin{proposition}[Proposition 1.33, \cite{freyd1972aspects}]
There is an adjoint  $\sigma_b \dashv \times_b$.\\
\end{proposition}

Note that $(C/b_2)/(b_1\to b_2)$ is isomorphic to $C/b_1$. With this identification we have $$\times_{b_1\to b_2} = f^*,\qquad \text{and} \qquad\sigma_{(b_1\to b_2)} = \Sigma_f.$$

With this note we have two important things to point out:
\begin{itemize}
\item We have that each $f^*$ have a left adjoint $\Sigma_f$. It can be checked that in the context of $C$ having finite limits, they satisfy the Beck condition.
\item To prove that $f^*$ has a right adjoint for each $f$ if, and only if, for each $b\in Ob(C)$ we have that $C/b$ is Cartesian closed, it suffices to proof such for $\times_b$.\\
\end{itemize}

One implication of theorem \ref{theo:theox} directly derives from the following proposition.
\begin{proposition}[Proposition 1.34, \cite{freyd1972aspects}]
  For $C$ Cartesian closed, $\times_b: C\to C/b$ has a right adjoint.
\end{proposition}

The other implication can be check by noting that, if $\times_b$ has a right-adjoint $\#_b$, then we have:
$$(\cdot \times b, a) = \left( \sigma_b(\times_b(\cdot)), A\right) \cong^{1.} \left( \times_b(\cdot), \times_b(A)\right)  \cong^{2.} \left( \cdot, \#(\times_b(A)\right)), $$

where in 1. and 2. we use adjointness. Thus, we can define $a^b:= \#_b(\times_b(a))$.

\section{The LCC category of a Martin-L\"of theory}
\label{InternalLCC}
In this section we are going to explain how to create a locally Cartesian category. To do that, we define the function $\CC$ that takes a ML theory and returns a category, and them proceed to proof that such a category is an LCC.

\begin{definition}
  Let $M$ be a ML theory. We define the category $\CC(M)$ as the category that has:
  \begin{itemize}
  \item As objects all closed types of $M$.
  \item As arrows $f:a\to b$  all closed terms of type $a\to b$.
  \end{itemize}
\end{definition}

Further on, we will consider $M$ an arbitrary ML theory.
\begin{proposition}\label{prop:CM1}
  $\CC(M)$ is a Cartesian category.
\end{proposition}
\begin{proof}
  \begin{itemize}
  \item Operations:
    \begin{itemize}

    \item \emph{Identity}: given an object $a\in \CC(M)$, define $1_a := \lambda x^a.x$.
    \item \emph{Composition}: given a pair of arrows $(f:a\to b) =  \lambda x^a. \varphi(x)$ and $ (g:b \to c) = \lambda x^b. \psi(x)$ in $\CC(M)$, define:
      $$f\circ g := \lambda x^a. \psi (\varphi(x)). $$
    \end{itemize}

  \item Properties:
    \begin{itemize}
    \item \emph{Associative}: given an extra arrow $h:c\to d = \lambda x^c. \rho(x)$, we have that,
\begin{align*}
  h \circ (g\circ f)  = h\circ \lambda x^a.\psi(\varphi(x)) & = \lambda x^a.\rho(\psi(\varphi(x))) \\ & =  \lambda x^a.\rho(\psi(x)) \circ f =  (h\circ g)\circ f.
 \end{align*}
    \item \emph{Unit}:
      $$f \circ 1_a = \lambda x^a. \varphi(x) = f.$$
      Proceed analogously to check that $1_b \circ f =f$.
    \end{itemize}

  \item Terminal object: Type 1 is the terminal object, due to $t: 1$ implying $t=*$ and thus $\lambda x^a. *$ being the only term of type $a\to 1$.
  \item Product:  product for types $a$ and $b$ is given by $a\times b$, and projections by $\pi, \pi'$.
  \end{itemize}
\end{proof}

\begin{proposition}
  Given $f:a\to b, g:c\to b$, the pullback
  \[
    \begin{tikzpicture}
      \node {\begin{tikzcd}
          a\times_b c \ar[d,"p_1"]\ar[r,"p_2"] & c\ar[d,"g"]\\
          a\ar[r,"f"] &   b.
        \end{tikzcd}};
    \end{tikzpicture}
  \]
  is defined by $a\times_b c := \Sigma x^a. \Sigma y^c. I(f(x),g(y))$, with $p_1=\pi$ and $p_2=\pi'\pi$.
\end{proposition}
\begin{proof}
  Given $f',g'$ such that:

  \[
    \begin{tikzcd}
      q
      \arrow[bend left]{drr}{g'}
      % \arrow[dashed]{dr}[description]{u}x
      \arrow[bend right,swap]{ddr}{f'} & & \\
      & a\times_{b}c \arrow{r}{p_2} \arrow{d}[swap]{p_1} & c \arrow{d}{g} \\
      & c \arrow[swap]{r}{f}   & b
    \end{tikzcd}
  \]

  as $ff' = gg'$note that there is a term $\rho(x) = r(f(f'(x))) : I(f(f'(x)), g(g'(x)))$. We can define $$u: q\to a\times_b c = \lambda x^q. \langle f'(x),\langle g'(x),\rho(x)\rangle\rangle$$
  and by the equality rules we can check that is the only object of type $I(f(f'(x^q)), g(g'(x^q)))$ such that $up_1 = f',up_2=g'$.
\end{proof}
\begin{remark}
  Note how the pullback is designed exactly as the fiber product (see example \ref{fiber-product}), with the construction translated to ML theory.
\end{remark}

\begin{proposition}\label{prop:CM2}
  $\CC(M)$ is Cartesian closed.
\end{proposition}
\begin{proof}
  Given an object $b$, we can define of the adjoint $F_3^b\dashv G_3^b$ (see definition \ref{def:CCC}) by the bijection $\varphi: \hom(a\times b, c) \to \hom(a, c^b)$ given by:

  $$\varphi(t:a\times b\to c) = \lambda x^a.\lambda y^b. t(\langle x,y\rangle) : a\to c^b = a\to (b\to c),$$
  with inverse:
  $$\varphi^{-1}(t:a\times c^b) = \lambda x^{a\times b}. s(\pi'(z))(\pi(z)) : a\times b\to c.$$
\end{proof}
\begin{remark}
  Note that the evaluation is the classical term  $\varepsilon  = \lambda x^{(c^b\times b)}.\pi(x)(\pi'(x))$.
\end{remark}
Now, we have a nice structure for $C=\CC(M)$ as a closed Cartesian category. Further on, we are going to prove that it is, in fact, locally Cartesian closed.  The strategy will be the following:  we are going to define a $C$-indexed hyperdoctrine $P(M)$, and prove that $C$ and $P(M)$ are equal as $C$-indexed categories.

\begin{definition} $P(M)$ is the $C$-indexed category such that:
  \begin{itemize}
  \item For an object $a\in C$, $P(M)$ is the category with:
    \begin{itemize}
    \item objects are the types depending only on $x^a$.
    \item arrows $f: b\to c$ are all the terms of type $a(x)\to b(x)$ that only depend on $x^a$.
    \end{itemize}
  \item for an arrow $f:a\to b = \lambda x^a.\varphi(x)$, $f^*$ is defined by precomposition, i.e. given a type or term $e(x^b)$ in $P(M)(b)$, we set $f^*(e) = e(f(x^a))$.
  \end{itemize}
\end{definition}

\begin{remark}
  In the context of $P(M)(a)$ variable $x$ will be used as the ever available variable $x^a$, and thus we would use primarily $y$ as standard variable symbol.
\end{remark}
\begin{proposition}
  $P(M)(a)$ is Cartesian closed.
\end{proposition}
\begin{sproof}
  Repeat the proofs for proposition \ref{prop:CM1} and proposition \ref{prop:CM2} step by step considering in each term an extra parameter.
\end{sproof}

\begin{proposition}\label{lemma:hyperdoc}
  For each $a\in Ob(C)$, $C/a\cong P(M)(a)$.
\end{proposition}

To prove this proposition we are g with the help of two lemmas that the composition of these functors is naturally isomorphic to the identity.

\[
  \begin{tikzcd}
    C/a=\CC(M)/a\arrow[ shift left=.5ex, "\Phi"{name=T}]{r}{} &
    P(M)(a)\arrow[ shift left=1ex, "\Psi"{name=i}]{l}{} \\
  \end{tikzcd}
\]
\begin{itemize}
\item We define $\Phi$ as the functor such that:

  \begin{itemize}
  \item For every object $f:b\to a\in Ob(C/a)$ is sent to the type\footnote{Note that we are defining the inverse.}:
    $$\Phi(f)=\Sigma y^b.I(x,f(y)).$$
  \item For every arrow $h: (f:b\to a)\to (g:b\to a)$ is sent to the term:
    $$\Phi(h) = \lambda z^{\Phi(f)}. \langle h(\pi(z)), \rho\rangle,$$
    with $\rho : I(x,g(h(\pi(z))))$ defined from $$\pi'(z) : I(x,f(\pi(z)))\qquad\text{and}\qquad r(f(\pi(z))) : I (f(\pi(z)), g(h(\pi(z)))),$$ and transitivity of $I(\cdot,\cdot)$.
  \end{itemize}

\item We define $\Psi$ as the functor such that:
  \begin{itemize}
  \item For every object (type) $b(x^a)\in Ob(P(M)(a))$, $\Psi(b(x))$ is the projection $\pi:b(x)\to a \in C/a$ associated with $\Sigma x^a. b(x)$.
  \item For every arrow $t(x^a): b(x^a) \to c(x^a)$ is sent to the term:
    $$\Psi(t) = \lambda z^{\Sigma x^a. b(x)}. \langle \pi(z), t(\pi(z))(\pi'(z))\rangle.$$
  \end{itemize}
\end{itemize}
\begin{lemma}\label{lemma:CM3}
  Let $(f: b\to a)= \lambda x^b.f(x)$ be a term in $M$. Then $b$ and $\Sigma x^a.\Phi(f)(x)$ are isomorphic in $C$.
\end{lemma}
\begin{proof}
  We want to define two morphisms $i:b \to \Sigma x^a. \Sigma y^b. I(x,f(y))$, $j : \Sigma x^a. \Sigma y^b. I(x,f(y)) \to b$ in $C$\footnote{in the definition we do a notation simplification, $j$ shall receive a single variable an use $\pi$ and $\pi'$ to access the internal.} given by
  $$i = \lambda y^b. \langle f(y),\langle y, r(f(u))\rangle \rangle,$$
  and
  $$j = \lambda \langle x^a, \langle y^b, z^{I(x,f(y))} \rangle\rangle . y .$$
  Clearly $j(i(y)) = y$, and $i(j(\langle x^a, \langle y^b, z^{I(x,f(y))} \rangle\rangle)) = \langle f(y),\langle y, r(f(u))\rangle \rangle$, and equality arises from the definition of $\Phi$.\\
\end{proof}

\begin{remark}
  Note that lemma \ref{lemma:CM3} is based on the intuition of $\Phi(f)$ being the inverse of $f$.
\end{remark}

\begin{lemma}\label{lemma:CM4}
  For $b(x^a)$ a type, the objects $b(x^a)$ and $\Sigma y^{\Sigma x^a.b(x)}. I(x,\pi(y))$ are isomorphic in $P(M)(a)$.
\end{lemma}
\begin{proof}
  We want to define two morphisms $i(x^a):b(x) \to\Sigma y^{\Sigma x^a.b(x)}. I(x,\pi(y))$ and $j:\Sigma x^a. \Sigma y^b. I(x,f(y)) \to b$ in $P(M)(a)$:
  $$i(x^a) = \lambda z^{b(x)}. \langle\langle x, z\rangle, r(x) \rangle,$$
  and
  $$j(x^a) = \lambda \langle x_\#^a, z^{b(x_\#^a)} \rangle, v^{I(x,x_\#)}\rangle). z .$$
  Proceed analogously as in previous lemma to check isomorphism.
\end{proof}

% A proof for the following result can be found in \cite[Section 3, sublemma 3.2.3.3]{seely1984locally}.
% \begin{lemma}\label{lemma:CM5}
%   If $a$ is a closed type, $b(x^a)$ a type in $P(M)(a)$, then there is a bijection between the set of terms $$
% \end{lemma}

\begin{proof}[Proof of proposition \ref{lemma:hyperdoc}]
  We have to define two natural bijection. We check that $\Psi\circ\Phi(f:b\to a) = \pi: \Sigma x^a. \Sigma y^b.I(x,f(y)) \to a$. We define the counit $\varepsilon$ by the lemma \ref{lemma:CM3}, as we have that there is an equivalent $\tilde \pi : b \to a$ with an arrow given by the composition with the isomorphism. By the equivalence arrows we can check that in fact $\tilde \pi = f$, providing the counit. Naturality follows from the definitions.\\

For the  unit $\eta$ proceed similarly from lemma \ref{lemma:CM4}.

\end{proof}
\begin{theorem}
  $\CC(M)$ is locally Cartesian closed.
\end{theorem}
\begin{proof}
  We prove that, as $C$ indexed categories, $C\cong P(M)$. Finally, as $P(M)(a)$ is a closed Cartesian category, so is $\CC(M)/a$ for each $a$.\\

  Given proposition \ref{lemma:hyperdoc} it is only left to show that $\Phi$ and $\Psi$ commutes with $f^*$. It suffices to show that:
  \[
    \begin{tikzpicture}
      \node {\begin{tikzcd}
          C/a \ar[d,"\Phi"]\ar[r,"f_C^*"] & C/b\\
          P(M)(a)\ar[r,swap,"f_P^*"] &   P(M)(b)\ar[u,"\Psi"]
        \end{tikzcd}};
    \end{tikzpicture}
  \]
  where we use the notation $f_P^*$ and $f_C^*$ to differentiate  induced functors in different indexes categories.   Let $t:c\to a \in Ob(C/a)$. We can see that
  $$\Psi\circ f^*\circ \Phi(t) = \pi: \Sigma x^a. \Sigma y^b.I(f(x),t(y)) \to b,$$
  being this the definition of pullback of $t$ along $f$, thus equal to $f_C^*$.
\end{proof}

\section{Interpretation of Martin-L\"of theories}
\label{InterpretationLCC}
In this last section we proceed to introduce the concept of interpretation of a ML theory in a LCC. We consider this of particular interest, since we will show a different view than the one presented in the previous chapter on how to consider the equivalence/pairings arising between typing theories and categorical theories.\\

We define the key concept of an interpretation  as done by Seely. We decide to extend this definition with numerous remark, to provide another layer of understanding of the concept. Throughout this chapter, $M$ will represent a ML theory, and $C$ will represent a LCC. This chapter is based on \cite[Section 4]{seely1984locally}

% \begin{definition}[Definition]
%   An interpretation $\overline{\cdot}: M\to C$ consist of:
%   \begin{enumerate}
%   \item For a type-value function constant, $F$, with arguments of types $t_1,...,t_n,$ a morphism $\phi: \overline{F} \to \overline{t_n}$ of $C$.
%   \item For a term-valued function constant, $f$, with arguments of types $t_1,...,t_n$ and value of types $a$, a morphism $\oveline{f}:$
%   \end{enumerate}
% \end{definition}

\begin{definition}[Interpretation of a Martin-L\"of theory]
An interpretation $\overline{\cdot}:M\to C$, is a mapping such that:
  \begin{itemize}
  \item A closed type $b$ correspond to an object $\overline b\in C$.
  \item A type $b(x^a)$ will be interpreted as an object $\beta: \overline{b} \to \overline{a}$ in $C/\overline{a}$.
  \item For a type $b(x^a)$ mapped to $\beta : \overline b \to \overline a $, a term $t(x^a) : b(x^a)$ maps to a morphism $\overline t$ such that:
    \[
      \begin{tikzpicture}
        \node {\begin{tikzcd}
            \overline a \ar[rr,"\overline t"]\ar[rd,swap, "1_{\overline a}"] &  & \overline b\ar[ld, "\beta"] \\
            &   \overline a &
          \end{tikzcd}};
      \end{tikzpicture}
    \]
  \end{itemize}

\end{defintion}
\begin{remark}
  Note than a type $t$ not depending on any variable is, in fact, a type depending on a variable $x^1$. Remember the original foundation of ML theories as proof theories, and recall the fact that to prove a type is the same as to proof that it is derivable form truth itself. Similar observations can be done with a term.
\end{remark}

\begin{remark}
  Note that, when mapping a type, we have a codomain fixed, but not so the domain of the arrow and the arrow itself to which the type is mapped. the problem that, for each type we need to assign to it two new characters: the name of the domain and the name of the arrow. This causes a notational problem. We decided to alleviate this problem by sending a type in a Latin letter, to the Greek equivalent letter as arrow, and an overline Latin letter as codomain. Some of the pairing that will appear in this section are shown in figure \ref{faketable:mul}.

  \begin{figure}[!h]
    \begin{center}
      \begin{tabular}{|c|c|c|}
        \toprule
        Type & Arrow & Codomain \\
        \midrule
        $a$ & $\alpha$ & $\overline a$\\
        $b$ & $\beta$ & $\overline b$\\
        $r$ & $\rho$ & $\overline r$\\
        $s$ & $\sigma$ & $\overline s$\\
        $t$ & $\tau$ & $\overline t$\\
        $x$ & $\chi$ & $\overline x$\\
        \bottomrule
      \end{tabular}
    \end{center}
    \caption{Commons pairings.} \label{faketable:mul}
  \end{figure}

\end{remark}

\begin{remark}\label{multiple-def}
  Let us analyze the considerations that are held whenever considering a type $t_{n+1}(x_1^{t_1},...,x_n^{t_n})$ that satisfy the condition on variables.  A closed type corresponds to an object $\overline t_1\in C$. Then we proceed to consider $t_2(x_1^{t_1})$, so that $\overline t_2$ is a morphism $\tau_2:\overline t_2\to \overline t_1$. The type $t_{3}(x_1^{t_1}, x_2^{t_2})$ is mapped to $\tau_3: \overline{t_3}\to \overline{t_2}$. This also induces a morphism $\tau_3' = \langle \tau_2\tau_3, \tau_3\rangle$.\\

  Similarly for the terms, we will end up for any $f:t_3(x_1^{t_1}, x_2^{t_2})$ as:
  \[
    \begin{tikzpicture}
      \node {\begin{tikzcd}
          \overline t_2 \ar[rr,"\overline f"]\ar[rd,swap, "1_{\overline t_2}"] &  & \overline t_3 \ar[ld, "\tau_3"]\ar[rd, "\tau_2\tau_3"]  \\
          &   \overline t_2\ar[rr, swap, "\tau_2"] & & \overline t_1\\
        \end{tikzcd}};
    \end{tikzpicture}
  \]

  Thus, a term $f:t_2$ without any more context will be considered in the maximum generality as an arrow:
  \[
    \begin{tikzpicture}
      \node {\begin{tikzcd}
          \overline t_2 \ar[rr,"\overline f"]\ar[rd,swap, "\chi"] &  & \overline t_3 \ar[ld, "\tau"]  \\
          &   \overline t_1 & \\
        \end{tikzcd}};
    \end{tikzpicture}
  \]

\end{remark}

\begin{definition}[Interpretation of a Martin-L\"of theory - second part]\label{def:terminando}
  The following equalities must be true:
  \begin{itemize}
  \item The substitution of a variable in a type is defined as $\overline{a(t)} = \overline t^* \overline a$, where $a$ is type dependent on a variable $x^b$ and $t : b$ is a term.
  \item The following type formation rules hold:
    \begin{enumerate}
    \item $\overline 1 = \top$, the terminal object in $C$.
    \item $\overline{I(x_1^a,x_2^a)} = \delta_{\overline{a}}: \overline{a} \to \overline{a}\times \overline{a}}$ where $\Delta_a$ is the diagonal functor as in definition \ref{daigon-alley}.\footnote{In view of substitution, $I(a,b)$ would be the equalizer.}
  \item If $a,b(x^a)$ are types interpreted as $\alpha: \overline a \to \overline x$, $\beta: \overline b \to \overline a$ then:
    $$\overline{\Pi x^a. b(x)} = \Pi_\alpha \beta, \qquad \overline{\Sigma x^a. b(x)} = \Sigma_\alpha \beta$$
  \end{enumerate}
\item The following term formation rules hold:
  \begin{enumerate}
  \item $\overline * = id_\top: \top \to \top$.
  \item If $b(x^a) : t(x^a)$ then $\overline{\lambda x^a. b(x)} = \prod_\alpha \overline t$.\\

    Similarly, let $a$ be a type interpreted as $\alpha : \overline a \to \overline x$, where $x$ represent any possible variable upon which $a$ depend. Let also $b(y^a)$ be a type interpreted by $\beta : \overline b \to \overline a$ and $g: a$, $f:\Pi y^a.b(y)$,  interpreted by $\overline g: \chi \to \alpha $ and  $\overline f: \chi \to \Pi_\alpha \beta$, for some $ \chi: \overline z \to \overline x$. We define $\overline f(a): 1_{\overline z} \to \overline g^* \beta$ as follows.\footnote{Without loss of generality we assume that $g,f$ depend on the same variables. Are dummy variables if not}.

 Since $g$ is a term of type $a$ we have that, as in remark \ref{multiple-def}
      \[
        \begin{tikzpicture}
          \node {\begin{tikzcd}
              \overline z \ar[rr,"\overline g"]\ar[rd,swap, "\chi"] &  & \overline a\ar[ld, "\alpha"] \\
              &   \overline x &
            \end{tikzcd}};
        \end{tikzpicture}
      \]

      we can check that $\overline g \alpha = \chi$, and so there exists an object $\Sigma_\alpha \overline g = \chi$. By adjointness of $\Sigma_\alpha \dashv \alpha^*\overline g$, we can get a morphism $$\varphi: \overline g \to \alpha^*\chi \in Ob(C/ \overline a).$$
      We can proceed  analogously with the adjoint relative to $\overline f: \chi \to \Pi_\alpha \beta$, for a morphism $\psi: \alpha^* \chi \to \beta$. We finish by composition, and repeating adjointness.

    \item With $a,b(x^a)$ as in previous point, let $g: a$, $f:b(g)$,  interpreted as $\overline g: \chi \to \alpha $ and  $\overline f: 1_{\overline z} \to \overline g ^* \beta$, for some $ \chi: \overline z \to \overline x$, supposing again that $g,f$ depend on the same variables. Then $\overline{\langle g, f\rangle}$ is defined by the morphism induced by $\overline f$ seen as  $\overline f = \Sigma_{\overline g} 1_{\overline z}\to \beta$ by adjointness. Finish by applying $\Sigma_\alpha$ and using again that $\overline g \alpha = \chi.$\\

      For $\Sigma x^a.b(x)$, we define  $\overline \pi  = \beta: \Sigma_\alpha \beta \to \alpha$, and $\overline {\pi'}$ as the identity.
    \item $\overline {r(x^a)} = 1_{\overline a}$. Hence, for a type $a$ interpreted as $\alpha: \overline a \to \overline x$, and a term $g : a$ interpreted as $\overline g : \chi \to \alpha$ with $\chi : \overline z \to \overline x$ then $\overline {I(g,g)} = (Eq(g,g)\to \overline z)$ and $\overline {r(g)} = 1_{\overline z}$ with $Eq$ being the equalizer\cite{nlab:equalizer} of the arrows. For this, we want to define $C[x^a, y^1, z^{I(x,y)}]$, we want to define a \emph{substitution morphism} $b(f)\time I(f,g) \to b(g)$, with standard $a,b,g,f$.This is done like follow. If for some $\chi: \overline z \to \overline x$, we have that $g,f : a$ interpreted as $\overline g,\overline f:\chi \to \alpha$, then the substitution morphism is defined by $\overline g^*\beta \times \overline {\langle g,f \rangle} ^* \overline I.$
    \end{enumerate}
\end{itemize}
\end{definition}

Let us review the progress so far. In this section we have defined the concept of interpretation. We have a way of looking at a ML theory within a category. After that, we have seen how to define the operations proper to a ML theory within a category. So finally it remains for us to check that the equivalences hold.

\begin{proposition}[Soundness]
All equality rules in definition \ref{def:ml-equality} are valid under an interpretation.
\end{proposition}

This is what is known as soundness of interpretation. This result is enunciated in \cite[proposition 4.5]{seely1984locally} but is not proven in detail. A proof can be found in the equivalent form of the interpretation of LPCE in hyperdoctrines in \cite{seely1983hyperdoctrines}.

\begin{remark}\label{rem:induced-functor} From the soundness it can be derived an interesting property. Any interpretation $\overline{\cdot}: M \to C$ induces a LCC-preserving functor $F:\CC (M) \to C$, as under $\overline M$ one sends closed type of $M$ (object of $\CC (M)$) to object of $C$, and similarly with terms. The soundness of an interpretation thus provides the preservation of the LCC structure.
\end{remark}

Finally, we introduce the concept of canonical interpretation.

\begin{definition}[Canonical interpretation]
  Let $M$ be a Martin-L\"of theory, we can define the canonical interpretation $\tilde \cdot: M\to \CC(M)$ as the interpretation such that closed:
  \begin{itemize}
  \item $\tilde 1 = 1$.
  \item A closed type correspond to itself as seen in $\CC(M)$.
  \item If $a, b(x^a)$ are types, the $b(x)$ is interpreted as the projection  $\pi$ related to  $\Sigma x^a.b(x)$\footnote{This refers to the intuitive idea of identifying $b(x)$ as the subset of $a$ such that property $b$ is satisfied.}.
  \item $\widetilde{I(x,y)} = \Sigma x^a.\Sigma y^a. I(x,y) \to a\times a$, supposing that $a$ is closed. As they are closed $\Pi x^a.b(x)$ and $\Sigma x^a.b(x)$ are interpreted as themselves.
  \item Each term is interpreted as its associated morphism (remembering that a closed term $a$ is equivalent to a function term $a\to 1$).
  \end{itemize}
\end{definition}

\begin{proposition}\label{ultima-label}
  Given any interpretation $\overline \cdot$, there is a unique functor $F:\CC (M) \to C$ such that the following diagram commute:
        \[
        \begin{tikzpicture}
          \node {\begin{tikzcd}
              M \ar[rr,"\tilde \cdot "]\ar[rd,swap, "\overline \cdot "] &  & \CC (M) \ar[ld, dashed, "F"] \\
              &   C &
            \end{tikzcd}};
        \end{tikzpicture}
      \]
\end{proposition}
\begin{proof}
Define the functor $F$ as it is induced as in remark \ref{rem:induced-functor}. Commutativity of the diagram follows form the definition of $\tilde \cdot$.
\end{proof}

\begin{definition}
For any ML theory $M$ and any LCC category $C$, we define $\Int(M,C)$ as the set\footnote{We can consider bigger universes if necessary.} of interpretations $M\to C$.
\end{definition}

\begin{definition}
  For ML theories $M,M'$,  an interpretation $M\to M'$ is an interpretation $M\to \CC (M)$. Thus we define ML as the category of ML theory along with interpretation.
\end{definition}
\begin{remark}
  If $i_1: M -> M'$ y $i_2: M' -> M''$ are interpretations, by proposition \ref{ultima-label} there is a functor $F: \CC(M') -> \CC(M'')$ associated to $i_2$. Then we define $i_2 \circ i_1 = F \circ i_1$.
\end{remark}

\begin{proposition}\label{que-cansaito-que-estoy}
  \begin{enumerate}
  \item For any ML theory $M$, and any $LCC$ category $C$, we have that
    $$  \Int (M,C) \cong \hom_{LCC} (\CC(M), C)$$
  \item For ML theories $M,M'$ we have that:
    $$ \hom_{ML}(M,M') \cong \hom_{LCC}(\CC(M),\CC(m'))$$
  \end{enumerate}
\end{proposition}
\begin{proof}
  \begin{enumerate}
  \item Follows from the definition of interpretations $M\to M'$.
  \item Just observe that    $$ \hom_{ML}(M,M') \cong \Int(M, \CC(M'))\cong \hom_{LCC}(\CC(M),\CC(m'))$$
  \end{enumerate}
\end{proof}
\begin{remark}[remark 4.8.1 \cite{seely1984locally}]
  $\Int$ is in fact functorial in both variables, and equalities in proposition \ref{que-cansaito-que-estoy} are natural in each variables.
\end{remark}

Finally, we note that we can repeat the same ideas carried away for interpretations  to introduce the concept of \emph{the theory of a locally Cartesian category}. The idea is to consider the ML theory whose interpretation is the identity. As the ideas are similar to those previously explained, we will avoid most of the steps. A more extended explanation, (not much extended) can be found in \cite[Section 5]{seely1984locally}.

\begin{definition}
  Let $C$ be a LCC. We define the ML theory $\MM (C)$, as the Martin-L\"of theory such that:
  \begin{itemize}
  \item As closed types it has the objects of $C$, a type with a free variable $x^a$ is an object $C/a$, a successive constructions as in remark \ref{multiple-def}.
  \item Term-valued functions are the terms of $C$.
  \item Has type and terms formation rules as in definition \ref{def:terminando}.
  \end{itemize}
\end{definition}

\begin{proposition}
$\MM (C) $ is an ML theory.
\end{proposition}

\begin{definition}\label{def:ml-equivalent}

  Two ML theories $m,M'$ are equivalent if their functor $\Int(M,\cdot)$, $\Int(M',\cdot)$ are naturally isomorphic.
\end{definition}

Finally, we present the derived equivalences:

\begin{theorem}[Theorem 6.1 \cite{seely1984locally}]\label{ml:6.1}
If $C$ is an LCC, then $C\cong \CC(\MM(C))$.
\end{theorem}
\begin{proof}
  By definition, the closed type and terms of $\MM(C)$ (object and arrows of $\CC(\MM(C))$)  are the closed types and terms of $C$.
\end{proof}

\begin{theorem}[Theorem 6.2 \cite{seely1984locally}]\label{ml:6.2}
If $M$ is a Martin-L\"of theory, then $M\cong \MM(\CC(M))$.
\end{theorem}
\begin{proof}
It follows form theorem \ref{ml:6.1} and definition \ref{def:ml-equivalent}.
\end{proof}

Thus, both categories are equivalent.


\section{Input of Example Plugin for RAVEN}
In this section the developer of the ExternalModel plugin should report information
on how to use it in a RAVEN calculation. As mentioned in the previous section a simple
Model that performs a summation of exponential (over time (or any monotonic variable) has been developed:
\newline
 \begin{math}
        Xi(t)=\sum_{i=1}^{n} coef_i*e^{var_i*t}
  \end{math}
  \newline
In this section, the input of the plugins must be reported. As an example, we report the
input of the Example Plugin ``SumOfExponential'' that we use as template.

\subsection{Input of SumOfExponential ExternalModel plugin}

The input of SumOfExponential is an XML file. An example of the input structure is given in Listing \ref{lst:InputExample}. The following section will discuss the
 different keywords in the input and describe how they are used in the SumOfExponential plugin.

\begin{lstlisting}[style=XML,morekeywords={anAttribute},caption=SumOfExponential
  input example., label=lst:InputExample]
  <ExternalModel name="a_name" subType="SumOfExponential">
      <variables> Xi, monotonicVariable, var1, var2, var3</variables>
      <!-- xml portion for this plugin only -->
      <outputVariable>
        Xi
      </outputVariable>
      <monotonicVariable>
        time
      </monotonicVariable>
      <startMonotonicVariableValue>
        0.0
      </startMonotonicVariableValue>
      <endMonotonicVariableValue>
        1e6
      </endMonotonicVariableValue>
      <numberCalculationPoints>
        1000000
      </numberCalculationPoints>
      <coefficient varName="var1">1.1</coefficient>
      <coefficient varName="var2">-1.1</coefficient>
      <coefficient varName="var3">-1.1</coefficient>
 </ExternalModel>
\end{lstlisting}

As one can see, all the specifications of the SumOfExponential plugin are given in the
\xmlNode{ExternalModel} block. Inside the \xmlNode{ExternalModel} block,  the XML
nodes that belong to this plugin only (and not to the ExternalModel) are:
\begin{itemize}
  \item  \xmlNode{outputVariable}, \xmlDesc{string,
  required parameter}, the name of the output variable (e.g. $Xi$)
  \item  \xmlNode{monotonicVariable}, \xmlDesc{string,
  required parameter},  the name of the monotonic variable (e.g. $time$)
  \item  \xmlNode{startMonotonicVariableValue}, \xmlDesc{float,
  required parameter}, the starting value of the monotonic variable (e.g. time)
  \item  \xmlNode{endMonotonicVariableValue}, \xmlDesc{float,
  required parameter}, the ending value of the monotonic variable (e.g. time)
  \item  \xmlNode{numberCalculationPoints},\xmlDesc{int,
  required parameter}, the number of steps in the calculation (e.g. number of time
  steps).
  \item  \xmlNode{coefficient}, \xmlDesc{float,
  optional parameter}, the $i-th$ coefficient for the exponential function ($coef_i$).
  Default value is $1.0$.The user can input a coefficient for each variable  of the model.
  The mapping between the $coef_i$ and the associated variable is defined by the
  attribute $varName$:
  \begin{itemize}
    \item \xmlAttr{varName}, \xmlDesc{required string attribute}, variable this coefficient
    is linked to.
  \end{itemize}
\end{itemize}


\documentclass[../main.tex]{subfiles}

\begin{document}

\chapter{Hierarchical Clustering Model} \label{hierarchical_clustering_model}

% Quick overview of HCA again
% Distance metric

% Talk about maintaining "unsupervised" nature; build search space now evaluate next
% Criteria being varied in search space; linkage [4], number of sectors [15; range from 5-19]
% Visualize search space

In the previous chapter, we evaluated major families of learning methods and identified the Hierarchical Clustering Analysis (hereafter \textit{HCA}) algorithm to be the method best aligned with the research goals of the project. Here we outline the specifics of our approach to applying HCA to our model input data, and build a search space of candidate universes to be evaluated, fully addressing RG-1.

\section{HCA Overview}

As outlined in Section~\ref{learning_methods_survey:hca}, hierarchical clustering is a greedy learning algorithm which seeks to construct a hierarchy of clusters. The greedy nature of this algorithm results in extremely high computational complexity for any given model fit, but is extremely stable in its solution. Furthermore, it is an $\mathcal{O}(1)$ complexity operation to extract classifications of varying arity due to the persistent hierarchical nature of the algorithm.

One of the main requirements of our heuristic is the ability to create sector universes with varying numbers of sectors. Because of this, we elected to utilize an Agglomorative approach to clustering. That is, we utilize a \textit{bottom-up} HCA model, where each company begins in its own sector, with larger clusters derived at each successive step of the tree by merging existing pairs of clusters.

Any given HCA algorithm tree is parameterized by two distinct settings; the distance metric, and the linkage method. To best understand the potential candidate universes that may be generated by this HCA-driven classification heuristic, we analyzed each of these model settings in turn:

\subsection{Distance Metric}

The distance metric is the measure of the distance between pairs of observations. This setting primarily affects the shape of the clusters. Due to the fact that our model input data is exclusively in monetary units (i.e. United States Dollars), we do not intend to transform the existing metric of wealth reflected by the dollar value measurement. Thus, we chose to use the $\ell^2$ (i.e. Euclidean) distance metric for our heuristic.

\begin{gather*}
    \text{Let $\boldsymbol{p}, \boldsymbol{q}$} = \text{Cartesian coordinates $\boldsymbol{p} = (p_1, \ldots, p_n)$ and $\boldsymbol{q} = (q_1, \ldots, q_n)$ where $\{\boldsymbol{p}, \boldsymbol{q}\} \in \mathbb{R}^{n \times 2}$} \\
    \text{Let $dist(\boldsymbol{p},\boldsymbol{q})$} = \text{$\ell^2$ (i.e. Euclidean) distance between points $\boldsymbol{p}$ and $\boldsymbol{q}$} \\
    \\
    \Rightarrow dist(\boldsymbol{p}, \boldsymbol{q}) = dist(\boldsymbol{q}, \boldsymbol{p})
    = \sqrt{(p_1 - q_1)^2 + (p_2 - q_2)^2 + \cdots + (p_n - q_n)^2}
    = \sqrt{\sum_{i=1}^n (p_i - q_i)^2}
\end{gather*}

\subsection{Linkage Method}

The second setting governing the behavior of the HCA algorithm is the selection of a linkage method. The linkage is a measure of distance between sets of observations as a function of the pairwise distances between observations. There are four major linkage method choices that we evaluate in our HCA model:

    $$ \text{Let $A, B, C, X, Y$} = \text{Sets (i.e. clusters) of observations} $$
    $$ \text{Let $C$} = X \cup Y $$
    $$ \text{Let $N$} = |A| + |X| + |Y|, \; \text{where} \; |\alpha| = \text{Cardinality}(\alpha) $$

\hspace{7em} \textbf{Single Linkage\citeFormat{\cite{Sibson1973SLINK:Method}}:}
        $$ d_\text{SLINK}(A, B) = \text{min} \; dist(\boldsymbol{a}, \boldsymbol{b})  \; \forall \; \{ \boldsymbol{a}, \boldsymbol{b} : \boldsymbol{a} \in A, \boldsymbol{b} \in B \} $$

\hspace{7em} \textbf{Complete Linkage\citeFormat{\cite{Defays1977AnMethod}}:}
        $$ d_\text{CLINK}(A, B) = \text{max} \; dist(\boldsymbol{a}, \boldsymbol{b})  \; \forall \; \{ \boldsymbol{a}, \boldsymbol{b} : \boldsymbol{a} \in A, \boldsymbol{b} \in B \} $$

\hspace{7em} \textbf{Average Linkage\citeFormat{\cite{Seifoddini1989SingleApplications}}:}
        $$ d_\text{ALC}(A, B) = \frac{1}{|A| \cdot |B|} \sum_{\boldsymbol{a} \in A} \sum_{\boldsymbol{b} \in B} dist(\boldsymbol{a}, \boldsymbol{b}) $$

\hspace{7em} \textbf{Ward (variance minimization) Linkage\citeFormat{\cite{Ward1963HierarchicalFunction}}:}
        $$ d_\text{WARD}(C, A) = \sqrt{ \frac{|A| + |X|}{N} d_\text{WARD}(A, X)^2 + \frac{|A| + |Y|}{N} d_\text{WARD}(A, Y)^2 - \frac{|A|}{N} d_\text{WARD}(X, Y)^2 } $$

\section{Unsupervised Learning Approach}

\begin{wrapfigure}[14]{r}{0.45\textwidth}
    \centering
    \vspace{\wrapfigadjustment}
    \fbox{
    \includegraphics[width=.9\linewidth]{images/tree.png}
    }
    \caption{Dendrogram of a sample hierarchical clustering model result.}
    \label{fig:hierarchical_clustering_model:sample_dendogram}
\end{wrapfigure}

As per RG-1 (see Section~\ref{research_goals:specific_research_goals}), we seek to create an entirely objective classification heuristic. Logically, this implies that we utilize an entirely nonparametric approach when designing the classification heuristic. However, as discussed above, the HCA algorithm is parameterized by both the distance metric, and the linkage method (in addition to a posterior selection of the number of sectors).

To work around the semi-supervised nature of the learning method, we elected to utilize HCA to build a search space of potential candidate sector universes. Following this, we will address our second research goal, RG-2, to rank these candidate sector universes against each other to determine the optimal learned sector classification.

Note that this search-space generation varies the sector count and linkage method parameters of the HCA model, but not the distance metric. This is due to the fact that we wish to preserve the monotonic and geometric difference of magnitudes of wealth implied by the dollar values of our input data.

Figure~\ref{fig:hierarchical_clustering_model:sample_dendogram} is a dendrogram of a sample HCA model generated on a subset of the model input data. Despite having a very high time complexity for model training, the HCA model thrives in its ability to extract sector classifications of varying arity from a HCA model. Its ability to perform this action in constant time complexity greatly enhanced our ability to generate a large search space of candidate learned sector universes.

\pagebreak

\section{Learned Sector Universe Search Space}

\begin{figure}[h]
    \centering
    \fbox{
    \includegraphics[width=.8\linewidth]{images/search_space_partial.png}
    }
    \caption{Candidate learned sectors partial search space visualization.}
    \label{fig:hierarchical_clustering_model:partial_search_space}
\end{figure}

With the end goal of building a comprehensive search space of candidate learned sector classifications, we generated HCA models parameterized with each of the linkage methods, and then isolated sector classifications for varying numbers of sectors. Specifically, we varied the number of sectors in our search space universes with $N = \{5, 6, \ldots, 19 \}$ for each of the four linkage methods, for a total of 60 candidate learned sector universes. A subset of our search space is visualized in Figure~\ref{fig:hierarchical_clustering_model:partial_search_space}.

The HCA models were generated iteratively using the model input data discussed in Section~\ref{model_data}. We utilized the built-in Hierarchical Clustering Module in \textit{Scikit-learn}\citeFormat{\cite{Pedregosa2011Scikit-learn:Python}} to generate the models, and saved them in specially formatted CSV files (publicly available on the \textit{reIndexer} website\citeFormat{\cite{Weerawarana2019ReIndexerUniverses}}) for later ingestion by the ranking system we developed to identify the optimal learned sector classification universe.

\end{document}


\chapter{A chapter with equations, chemical formula, figures and tables}
\label{chap_main}

The previous chapter \ref{chap_intro} contained only some text. Here some sample
equations, chemical formula, figures, and tables
are introduced.

\section{Section with equations and chemical formula}
\label{sec_eq_fig_tab}

Materials science is highly interdisciplinary. So you will have to write complex
equations some condensed-matter physicists use like:

\begin{equation}
\expval{\hat{O}}{\Psi_e} = \int \dotsc \int \Psi_e^{*}(x_1,\dotsc,x_N) \hat{O}
\Psi_e(x_1,\dotsc ,x_N)  dx_1 \dotsi dx_N
\quad .
\label{main_eq1}
\end{equation}

and,

\begin{equation}
\braket{\Psi_e} = \int \dotsc \int |\Psi_e (x_1,\dotsc,x_N)|^2  dx_1\dotsi dx_N = 1
\quad .
\label{main_eq2}
\end{equation}

Sometimes, you would also have to write chemical formula like chemists:

\begin{equation}
\centering
\ch{H3O_{aq}+ + 2 H2O <-> H5O2_{(aq)}+ + H2O <-> H7O3_{(aq)}+} \quad .
\label{main_eq3}
\end{equation}

With the appropriate tex packages defined, all this is possible.

\section{Section with tables and figures}
\label{sec_tab_figs}

Here is a sample table and figure. For the table, I've taken data from my
thesis \cite{Surendralal2020} and references \cite{Singh-miller2009, Sakong2018, Kittel1976, Salmeron1983}.
Notice how in table \ref{main_tab1}, I've rendered crystallographic directions using
the "miller" tex package.

\begin{table}[!tbh]
\begin{center}
  \begin{tabular}{llc}
  \toprule
       &  a [\AA{}]  &  $\Phi\mathrm{_{Pt\hkl(111)}}$ [eV]  \\
  \midrule
  PBE (this work) &  3.97  &    5.69  \\
  PBE (ref. \cite{Singh-miller2009}) & 3.99  &    5.69   \\
  RPBE (this work) &  3.99 &   5.50 \\
  RPBE (ref. \cite{Sakong2018})&  3.99 &   5.51 \\
  Experiments &  3.92 \cite{Kittel1976} &   6.08 $\pm$ 0.15 \cite{Salmeron1983} \\
  \bottomrule
  \end{tabular}
\end{center}
\caption[Calculated lattice parameter of Pt and work function of the Pt\hkl(1 1 1)
surface.]{The calculated lattice parameter of bulk Pt and the work function of the
Pt\hkl(1 1 1) surface using different exchange-correlation functionals compared to
experimental values.}
\label{main_tab1}
\end{table}

\begin{figure}[!tbh]
\centering
\includegraphics[scale=1., height=8cm]{\figpath/sinx.pdf}
\caption[A sinus curve]{A sinus curve to show how to include figures along with
captions.} %The text in square brackets is what is rendered in the list of figures.
\label{main_fig1}
\end{figure}

\section{Section with code}
\label{sec_code}

If you are in a computational field, you might need to include some code in your
thesis. The "minted" TeX package is useful for this. For example, some python code
can be inserted the following way:

\begin{minted}{python}
    import numpy as np
    import matplotlib.pylab as plt
\end{minted}

You could also insert inline code like so: \mintinline{python}{numpy}.


\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{pst-plot}
\usepackage{graphicx}
\usepackage{endnotes}
\usepackage{graphics}
\usepackage{floatflt}
\usepackage{wrapfig}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{verbatim}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{pdflscape}
\usepackage{enumitem}
\usepackage[normalem]{ulem}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0 0}}

\pdfpagewidth 210mm
\pdfpageheight 297mm
\setlength\topmargin{0mm}
\setlength\headheight{0mm}
\setlength\headsep{0mm}
\setlength\textheight{250mm}
\setlength\textwidth{159.2mm}
\setlength\oddsidemargin{0mm}
\setlength\evensidemargin{0mm}
\setlength\parindent{7mm}
\setlength\parskip{0mm}

\newenvironment{exercise}[3]{\paragraph{Exercise #1: #2 (#3pt)}\ \\}{
\medskip}
\newcommand{\question}[2]{\setlength\parindent{0mm}\ \\$\mathbf{Q_#1:}$ #2\ \\}

\author{\large{Ardi Tampuu, Tambet Matiisen, Raul Vicente}}
\title{\huge{Introduction to Computational Neuroscience}\\\LARGE{Practice session on Artificial Neural Networks}}

\begin{document}
\maketitle

\textbf{A request:} Please track how long it will take to complete this set of exercises. Add this time to your final report.
\ \\

\textbf{For Pyhton users:} You can do this task with any neural network package you want (sklearn, Keras, Neon, Caffe). But to save your time - it is really easy to run the code in Matlab/Octave.\\
\ \\
\ \\
\ \\
%
% Intro
%
In this session we are going to have a brief look on artificial neural networks. We start with simplest artificial neuron model called perceptron. Then we will see how simple feed-forward neural networks can be thought of as universal function approximators and what their limitations are. Finally we will use artificial neural network to predict rat's location like we did in Machine Learning practice session.

%
% Perceptron
%
\begin{exercise}{1}{Perceptron}{1}

\begin{wrapfigure}{r}{0.3\textwidth}
	\centering
	\vspace{-12pt}
	\includegraphics[width=0.22\textwidth]{perceptron.png}
	\caption{Simple perceptron}
	\label{fig:perceptronexample}
	\vspace{-5pt}
\end{wrapfigure}

Perceptron is the simplest artificial network model invented by Frank Rosenblatt in late 1950s. He added a learning rule to McCulloch-Pitts neuron, that allows it to learn certain functions from example inputs and outputs.

Perceptron works on binary data – both its inputs $x_j$ and output $y$ are ones or zeros. Output 1 or 0 can be thought of as binary classification – whether object represented by the given input belongs to certain class or not.

Perceptron’s weights $w_j$ can be any real numbers. Its prediction is calculated with following formula:

$$
y = \left\{
	\begin{array}{l l}
		1, \text{if } x_1 w_1 + ... + x_m w_m + b \geq 0\\
		0, \text{otherwise}
	\end{array}
	\right.
$$

Here $b$ is the bias term, that is added to the sum. In practice it is easier to just add additional input, which is always one. Then we don’t have to treat bias as something special, it is just an additional weight to learn. Learning rule for the perceptron is very simple:

$$
w_j = w_j + (t_i - y_i) x_{ij}
$$

Index $i$ is used to denote $i$-th example data point. Learning rule must be applied for all data points and for each weight. You will continue updating the weights until all data points are classified correctly.

It turns out, that perceptron is always able to successfully learn a classification rule for datasets, which are \textit{linearly separable} -- data points with label 1 and label 0 can be separated by a line (in case of two inputs), plane (in case of three inputs) or hyperplane (in case of input of any dimensionality). If the dataset is not linearly separable, perceptron will never \textit{converge} (settle to a certain set of weight values). \newline

Example code for this exercise is in \texttt{perceptron.m}. Your task is to fill in the perceptron learning rule and decide for four example datasets (replace the dataset number on line 12) if they are linearly separable or not. Of course, in 2D one can do this by just looking at the data, but in higher dimensional data this would not be the case. In your report you must include the final image with the decision boundary (the black line) for all four datasets. \footnote{\textbf{Decision boundary} is a line (plane/hyperplane) that separates the positive and negative datapoints - positives will be one one side and negatives on the other side} For linearly separable datasets also add the approximate number of steps it took for the perceptron to convergence (to reach 0 errors).

\end{exercise}

%
% Sinusoid
%
\begin{exercise}{2}{Function Approximation}{1.5}

Universal Approximation Theorem states that any continuous function can be approximated to any desired precision by a feed-forward network with a single hidden layer containing a finite number of non-linear neurons. In practice this theorem is of no use, because:
\begin{itemize}
  \item it states only, that these functions can be \uline{represented} by feed-forward network with one hidden layer. The theorem doesn’t say anything about if this approximation is \uline{learnable} - if with our current algorithms we can find the necessary weight values;
  \item the construction used in the proof uses a huge number of neurons in the hidden layer. This would be unreasonable for any practical application;
  \item as the theorem doesn’t consider learnability, it also doesn’t state anything about how well the networks generalizes to samples beyond its training data.
\end{itemize}

Nevertheless the theorem is a nice concept to guide your thinking – if some problem can be described as a function calculating output based on several inputs, then it probably can be approximated reasonably well with an artificial neural network.\newline

\begin{wrapfigure}{r}{0.3\textwidth}
	\centering
	\vspace{-12pt}
	\includegraphics[width=0.20\textwidth]{sine.png}
	\label{fig:sineexample}
	\vspace{-5pt}
\end{wrapfigure}

In this task we are going to approximate sine function using a neural network. This neural network is very simple – it consists of just one input node (the $x$ value), several hidden nodes and one output node ($y = sin(x)$).

Hidden nodes use sigmoid activation function to achieve non-linearity (the nodes apply sigmoid function to the weighted sum of their inputs). Output node is linear, no activation function is applied. Loss function is simply squared error:

$$
L = \frac{1}{2n}\sum_{i=1}^{n} (target_i - predicted_i)^2
$$

It calculates average loss over all $n$ data points in the training set. \newline

For creating neural networks we are making use of DeepLearnToolbox toolkit by Rasmus Berg Palm. The tookit is already included with your download in folder \texttt{DeepLearnToolbox}. Feel free to explore the source in \texttt{DeepLearnToolbox/NN} folder, it is quite clean Matlab code.\newline

For this exercise you need to run the code in \texttt{sinusoid.m} with different number of nodes and answer the questions.

\question{1}{First run the \texttt{sinusoid.m} exactly as it is. The script creates a neural network that tries to approximate the sine function between $-2\pi\ and\ 2\pi$, based on 20 training data points. The neural network has 5 hidden nodes. All the weights and biases are initialized with random values. It would be best if you understand more of less what the code does (read the comments).
Notice that the script will generate a plot that shows the true shape of the sine, the training data points, and the current approximation made by the network. This plot is updated as we pass over the training data and learn the weights. After the learning has finished another plot appears, summarizing how the loss decreased during training. Add the two plots to the report. Answer the following questions:
\begin{itemize}
\item Does the neural network represent the function well within the range $-2\pi\ to\ 2\pi$? If not, how many of the curves of the sine does the network manage to capture well? (In some cases (especially in Matlab) it might capture the shape well, but if you run it many times it mostly shouldn't)
\end{itemize}
}
\question{2}{Now run the script \texttt{minimal\_sinusoid.m}. In this script we also have a network of 5 nodes, but the weights and biases are not initialized randomly, but with useful values given by me (not learned by an algorithm). In this case you should see that the network learns to capture the shape of the sine really well. As you can see - the function can be represented well by a 5-node network, but the learning algorithm we used in Q1 did not allow us to find this approximation.
\begin{itemize}
\item Hypothesize why doesn't the network always learn a good solution if we start with randomly initialized weights. In here uncommenting the line  \texttt{plot\_sinusoid\_components(test\_x,nn)} for the  two cases might provide insights.
\item How good is the network at extrapolation (predicting values outside the range it had training data for)? Why is that not surprising?
\end{itemize}
}
\question{3}{Increase the number of hidden nodes in \texttt{sinusoid.m}, by replacing the number 5 with the desired number on line 28.
\begin{itemize}
\item How many hidden nodes do you need so that starting from randomly initalized parameters the learning algorithm would converge to a good representation of the function (training loss$<0.1$). Run the code couple of times for each tested number of nodes and report the result if at least one trial is good. Add the plot with the datapoints and the approximation.
\end{itemize}
}
\end{exercise}

%
% MNIST
%
\begin{exercise}{3}{Rat position decoding}{1.5pt + 1.0pt + some bonus }

Finally we will use artificial neural network to predict a rat's location based on the recordings from its hippocampal neurons. Remember that in the Machine Learning practice session we worked with this dataset and linear discriminant analysis gave us an accuracy of around 35\% on test set.

For neural networks we are once again making use of DeepLearnToolbox toolkit.

This time our network has 71 inputs that are the spike counts in different neurons during a period of time. In this first part of the task we divide the space into 16 squares and predict in which region the rat is located (like we did in ML practice). This means the network has 16 outputs, each of which is a probability corresponding to one of the squares. Between the inputs and outputs we have one or more layers of hidden nodes with hyperbolic tangent nonlinearity. In order to guarantee that the 16 output probabilities sum up to 1, we use a function called softmax (Google it if interested). When teaching the network to predict the correct zone we minimize the cross-entropy loss (see lecture).

After you have run the code and got initial results, your goal is to improve classification accuracy. For that you would need to tune learning rate, number of hidden nodes, number of epochs (iterations over full data set) and other parameters. Finding good parameters for neural networks can be quite tedious and frustrating, so I will not ask you to do it, instead we will do it together:

\begin{enumerate}
  \item We start by modifying the \textbf{learning rate}. This parameter decides how big a step will we take in the direction of the gradient during gradient descent. With learning rate too high, our model will never converge. With LR too low, the model will take too long time to reacha  good solution and is also prone to getting stuck in a bad local minima. Try $LR=1$ first and see that the loss graph is not stable - there are major zig-zags. This indicates that the learning rate is too high. Next we try 0.1. Still jumpy, but a little bit better. Finally we try 0.01 - very smooth, but error goes down a lot slower. Add all plots and the prediction accuracies after 30 epochs (the code prints it out) to the report. Which LR would you choose (there is no absolute truth, so explain your choice)? Compare the results with baseline (LDA accuracy).
  \item \textbf{Momentum} is a method that that allows better and more stable learning by remembering the global trends in weight changes - if all previous datapoints have suggested that a weight should be lowered, we decrease it faster than normal. If datapoints do not agree on how to change a certain weight, we change it only slowly. \newline Set the value of momentum to 0.95. Run the code with LR=0.1 and LR=0.01 (you can even try 0.001). Which LR you judge more adequate now (there is no absolute truth, so explain your choice)?
  \item With momentum 0.95 and LR 0.01 you should see \textbf{overfitting}, which means that network learns to predict well on training examples, but this knowledge doesn't generalize to the test set. You can detect this from large gap between training and validation misclassification rate. To fight against overfitting we have two tools - the weight penalty and dropout.
If we brought in weight penalty, we would again need to search for a good learning rate. So instead we use dropout (in this case you might need to increase the number of nodes, but that's the next bulletpoint). Dropout means that at every training step we do not consider some nodes (a certain proportion) in our network - we simply do as if they were not there. In every training step the left out neurons are different. Dropout is so effective because it is similar to adding random noise to the network - it makes it impossible to simply remember the training data and forces the model to generalize. It also has similarity with ensembling techniques.\newline Your task is to try \textbf{dropout} percentages 0.2, 0.4, 0.6 and see if you can bring the validation error down (making the difference with training error smaller is not enough - only thing we care about is the validation accuracy). Add plots to the report.
    \item Once you have stabilized the learning, try to increase \textbf{number of hidden nodes} and see if testing error improves. You can also add more layers by simply adding a number in nnsetup - for example \texttt{$nn\ =\ nnsetup([71\ 100\ 100\ 16]);$}  creates two layers of 100 nodes. More layers might sound appealing, but it makes the learning slower to converge, so be cautious with it. Add the plots and accuracies that you achieved with bigger networks.
  \item Finally, if the loss for test set seems to still decrease at the end of training (end of 30 epochs), increase the \textbf{number of epochs} and see how low the error can go. Usually it plateaus at some point and there is no reason to train further. (no task in particular in here)
  \item \textbf{Your final task is} to get the validation accuracy above 0.42. There will be bonus points for the ones that get the best accuracy (it's a competition!).
\end{enumerate}

-----------------------------------------------------

In the second part of this exercise we will not predict the 16 areas where the rat is in, but the XY coordinates directly. This means we have 2 linear output nodes (nodes with no nonlinearity) predicting the coordinates rather than 16 outputs predicting probabilities (using softmax). When training the network we minimize the squared difference between true coordinates and the predicted coordinates (mean squared error loss).

\begin{itemize}
\item Run the code \texttt{rat\_regression.m}. The program outputs the baseline error (how wrong the prediction is on average when using a linear model). After training a network the script will also print out how big is the average error made by the neural network. Report the two values and which model is better. Add the plot of how error changed during training. Notice that the error on the graph (the thing we minimize) is mean squared error (MSE), whereas we finally care about mean distance.
\item Try to improve the result (get lower error). Make a hypothesis of what could help you - more neurons? higher or lower dropout? different learning rate? Write down your hypothesis, run the models and report if it worked out or not. It is absolutely not important that you actually improve the result. The task is to explore. HINT: change only one parameter at a time, otherwise you will not know which change caused the benefit/problem. You need to report at least 3 different things you tried.
\item Again bonus points for the people who achieve the lowest error.
\end{itemize}
\end{exercise}

\ \\
\ \\
\ \\
\ \\
\ \\
Please submit a \texttt{pdf} report with answers to the questions and comments about your solutions. YOU MUST INCLUDE THE CODE. Please mark how long it took to complete this set of exercises. Upload a zip/rar/etc with the \texttt{pdf} and the code to the practice session page on the course website.

\end{document}

2016/Practices/06 - Artificial Neural Networks/text/cns-ann.tex


\subsection{Totally ordered sets}\label{subsec:totally_ordered_sets}

\begin{definition}\label{def:totally_ordered_set}
  We say that a partially ordered set is \term{totally ordered} if either the nonstrict order \( \leq \) is \hyperref[def:binary_relation/total]{total} or if the strict order \( < \) is \hyperref[def:binary_relation/trichotomic]{trichotomic}.

  The theory, homomorphisms and category \( \cat{Tos} \) are obtained analogously to \fullref{def:partially_ordered_set}, but with either of these additional axiom sets.

  For a fixed \hyperref[def:grothendieck_universe]{Grothendieck universe}, the category of \( \ucat{Tos} \) is isomorphic to that of \( \mscrU \)-small thin skeletal connected categories --- see \fullref{thm:order_category_isomorphism/totally_ordered}.
\end{definition}
\begin{proof}
  Equivalence between nonstrict and strict total orders follows directly from the compatibility condition \eqref{def:partially_ordered_set/compatibility_nonstrict}.
\end{proof}

\begin{proposition}\label{thm:total_order_embedding_iff_strict}
  Let \( (P, \leq_P) \) and \( (Q, \leq_Q) \) be \hyperref[def:totally_ordered_set]{totally ordered sets} (more generally, \( (Q, \leq_Q) \) can be any \hyperref[def:preordered_set]{preordered set}).

  An \hyperref[def:partially_ordered_set/homomorphism]{order homomorphism} from \( P \) to \( Q \) is \hyperref[def:function_invertibility/injective]{injective} if and only if it is strict.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Follows from \fullref{thm:order_embedding_is_strict}.

  \NecessitySubProof Let \( f: P \to Q \) be a strict order homomorphism and suppose that \( f(x) = f(y) \) for some \( x \) and \( y \) in \( P \). We will use the \hyperref[def:binary_relation/trichotomic]{trichotomy} of \( <_P \).
  \begin{itemize}
    \item If \( x <_P y \), then \( f(x) <_Q f(y) \) since \( f \) is strictly monotone, which contradicts \( f(x) = f(y) \).

    \item If \( x >_P y \), similarly \( f(x) >_Q f(y) \) and we again obtain a contradiction.

    \item It remains for \( x \) to be equal to \( y \).
  \end{itemize}

  Since \( x \) and \( y \) were arbitrary, we conclude that \( f \) is injective.
\end{proof}

\begin{proposition}\label{thm:totally_ordered_strong_homomorphism}
  For totally ordered sets, \hyperref[def:partially_ordered_set/homomorphism]{strict order homomorphisms} are precisely the \hyperref[rem:first_order_strong_homomorphism]{strong order homomorphisms}.

  Let \( (P, \leq_P) \) and \( (Q, \leq_Q) \) be \hyperref[def:totally_ordered_set]{totally ordered sets} (more generally, \( (Q, \leq_Q) \) can be any \hyperref[def:preordered_set]{preordered set}) and let \( f: P \to Q \) be an order homomorphism between them. Then \( f \) is a strict order homomorphism if and only if it is a strong order homomorphism.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Suppose that \( f \) is a strict order homomorphism. We must show that \( f(x) \leq f(y) \) entails \( x \leq y \).

  If we suppose that \( x > y \) for some \( x \) and \( y \) in \( P \), then \( f(x) > f(y) \), which contradicts our assumption \( f(x) \leq f(y) \).

  Therefore, \( f \) is a strong order homomorphism.

  \NecessitySubProof Suppose that \( f \) is a strong order homomorphism. Let \( x < y \). It follows that \( f(x) \leq f(y) \).

  Suppose that \( f(x) = f(y) \). Then both \( f(x) \leq f(y) \) and \( f(x) \geq f(y) \) hold, which in turn imply that both \( x \leq y \) and \( x \geq y \) hold since \( f \) is a strong homomorphism. Thus, we obtain \( x = y \), which contradicts our assumption that \( x < y \).

  Therefore, \( f \) is a strict order homomorphism.
\end{proof}

\begin{corollary}\label{thm:totally_ordered_strict_isomorphisms}
  A strict order embedding between totally ordered sets is an isomorphism if and only if it is bijective.
\end{corollary}
\begin{proof}
  Follows from \fullref{thm:totally_ordered_strong_homomorphism} and \fullref{thm:automorphism_without_predicate_symbols}.
\end{proof}

\begin{proposition}\label{thm:totally_ordered_minimal_element_is_minimum}
  If \( (P, \leq) \) is a \hyperref[def:totally_ordered_set]{totally ordered set} and \( A \subseteq P \) is nonempty, then any \hyperref[def:partially_ordered_set_extremal_points/maximal_and_minimal_element]{minimal element} is a \hyperref[def:partially_ordered_set_extremal_points/maximum_and_minimum]{minimum}.
\end{proposition}
\begin{proof}
  Let \( x_0 \) be a minimal element of \( A \). If \( x_0 \) is the only element of \( A \), it is clearly the minimum of \( A \). Suppose that \( A \) is not a singleton set.

  By definition of total order, for any \( x \in A \) either \( x \leq x_0 \) or \( x_0 \leq x \). If \( x \leq x_0 \), then since \( x_0 \) is a minimal element, we have \( x = x_0 \).

  Therefore, for any \( x \in A \), either \( x = x_0 \) or \( x > x_0 \) (i.e. \( x \leq x_0 \)), proving that \( x_0 \) is a minimum of \( A \).
\end{proof}

\begin{proposition}\label{thm:totally_ordered_segment_isomorphism}
  Let \( (P, \leq) \) be a \hyperref[def:totally_ordered_set]{totally-ordered set}. Let \( Q \) be the set containing the \hyperref[def:partially_ordered_set_interval/ray]{strict initial segment} \( P_{<x} \) for every member \( x \) of \( P \).

  Then \( (P, \leq) \) is \hyperref[def:partially_ordered_set/homomorphism]{strictly order-isomorphic} to \( (Q, \subseteq) \).
\end{proposition}
\begin{proof}
  Explicitly define the isomorphism
  \begin{equation*}
    \begin{aligned}
      &f: P \to Q \\
      &f(x) \coloneqq P_{<x} = \set{ y \in P \given y < x }.
    \end{aligned}
  \end{equation*}

  We will first show that \( f \) is \hyperref[def:partially_ordered_set/homomorphism]{strictly monotone}. If \( x < y \), then \( x \in P_{<y} \). But \( x \not\in P_{<x} \), hence \( P_{<x} \) is a strict subset of \( P_{<y} \). Thus, \( f \) is strictly monotone.

  The function \( f \) is injective by \fullref{thm:total_order_embedding_iff_strict} and surjective by definition. Thus, \( f \) is a strict isomorphism between \( (P, \leq) \) and \( (Q, \subseteq) \).
\end{proof}

\begin{proposition}\label{thm:totally_ordered_cofinal_equivalences}
  Let \( (P, \leq) \) be an \hyperref[def:partially_ordered_set_extremal_points/upper_and_lower_bounds]{unbounded from above} totally ordered set and let \( A \subseteq P \). Then \( A \) is \hyperref[def:cofinal_set]{cofinal} if and only if it is itself unbounded from above.

  This equivalence is useful for \hyperref[def:regular_cardinal]{regular cardinals} --- for example \fullref{thm:cardinal_cofinality}.

  Compare this result with \fullref{thm:partially_ordered_cofinal_equivalences}.
\end{proposition}
\begin{proof}
  \SufficiencySubProof Let \( A \) be a cofinal set. Suppose that it is bounded from above. Then there must exist some \( x \in P \) such that \( x \) is an upper bound of \( A \).
  \begin{itemize}
    \item If \( x \not\in A \), this contradicts the cofinality of \( A \).
    \item If \( x \in A \), then it is a maximum. But since \( P \) is itself unbounded from above, we can find some \( y \in P \) such that \( y \) is again an upper bound of \( A \) and \( x < y \). But this contradicts the cofinality of \( A \).
  \end{itemize}

  \NecessitySubProof Let \( A \) be unbounded from above. Suppose that is is not cofinal. Since there exists no upper bound of \( A \), for every \( x\in P \) there exists some \( y \in A \) such that \( x \leq y \). Therefore, \( A \) is cofinal.
\end{proof}

\begin{proposition}\label{thm:total_lexicographic_order_is_total_order}
  If \( (P, \leq_P) \) and \( (Q, \leq_Q) \) are \hyperref[def:totally_ordered_set]{totally ordered sets}, then the \hyperref[eq:def:lexicographic_order]{lexicographic} and \hyperref[eq:def:lexicographic_order/reverse]{reverse lexicographic} orders on \( P \times Q \) are \hyperref[def:totally_ordered_set]{strict total order} relations.

  Compare this result to \fullref{thm:lexicographic_order_is_partial_order} and \fullref{thm:well_ordered_lexicographic_order_is_well_ordered}.
\end{proposition}
\begin{proof}
  We have already shown in \fullref{thm:lexicographic_order_is_partial_order} and these are partial orders. It only remains to check trichotomy.

  \SubProofOf[def:binary_relation/trichotomic]{trichotomy} Let \( \prec \) be the lexicographic order on \( P \times Q \). Let \( (a, b) \) and \( (c, d) \) be pairs in \( P \times Q \). Since \( <_P \) and \( <_Q \) are strict total orders, we only have the following possibilities:
  \begin{itemize}
    \item If \( a = c \) and \( b = d \), then \( (a, b) = (c, d) \).
    \item If \( a = c \) and \( b <_Q d \), then \( (a, b) \prec (c, d) \).
    \item If \( a = c \) and \( b >_Q d \), then \( (a, b) \succ (c, d) \).
    \item If \( a <_P c \), then \( (a, b) \prec (c, d) \).
    \item If \( a >_P c \), then \( (a, b) \succ (c, d) \).
  \end{itemize}

  The proof for the reverse lexicographic order is analogous.
\end{proof}

\begin{definition}\label{def:order_topology}
  Let \( P \) be a \hyperref[def:partially_ordered_set]{totally ordered set} with more than one element. The \term{order topology} induced by \( \leq \) is the topology generated by the \hyperref[def:topological_subbase]{subbase} of open \hyperref[def:partially_ordered_set_interval/ray]{rays}
  \begin{equation*}
    S \coloneqq \set[\Big]{ (a, \infty) \given a \in P } \cup \set[\Big]{ (-\infty, b) \given b \in P }.
  \end{equation*}

  The \hyperref[def:topological_base]{base} corresponding to this subbase is
  \begin{equation*}
    \mscrB = S \cup \set[\Big]{ \varnothing } \cup \set[\Big]{ (a, b) \given a, b \in P \T{and} a < b }.
  \end{equation*}

  See the proof of \hyperref[thm:topological_base_axioms/B1]{B1} for why \( P \) must have more than one element.
\end{definition}
\begin{defproof}
  \SubProof{Proof of compatibility of \( S \) and \( \mscrB \)} Define
  \begin{equation*}
    \mscrC = \set*{ \bigcap S \given* S \text{ is a nonempty finite subset of } S }.
  \end{equation*}

  We will show that \( \mscrB = \mscrC \).

  Let \( B \in \mscrB \). The cases \( B \in S \) and \( B = \varnothing \) are trivial. Suppose that \( B \not\in S \). Then there exist points \( a < b \) such that
  \begin{equation*}
    B = (a, b) = (-\infty, b) \cap (a, \infty).
  \end{equation*}

  This is an intersection of members of \( S \), hence \( B \in \mscrC \). Therefore, \( \mscrB \subseteq \mscrC \).

  Now let let \( C = S_1 \cap \cdots \cap S_n \), where \( S_1, \ldots, S_n \) are members of \( S \). We will show by induction on \( n > 0 \) that \( C \in \mscrB \). The case \( n = 1 \) is trivial. Suppose that all \( n \)-ary intersections belong to \( \mscrB \) and let
  \begin{equation*}
    C = S_1 \cap \cdots \cap S_n \cap S_{n+1}.
  \end{equation*}

  By the inductive hypothesis we have that \( D \coloneqq S_1 \cap \cdots \cap S_n \) belongs to \( \mscrB \) and thus we have three cases:
  \begin{itemize}
    \item If either \( D = (a, \infty) \) or \( D = (-\infty, b) \), then \( D \in S \).
    \item If \( D = \varnothing \), then \( D = (-\infty, a) \cap (a, \infty) \) for some \( a \in P \).
    \item If \( D = (a, b) \), then \( D = (-\infty, b) \cap (a, \infty) \).
  \end{itemize}

  In all cases both cases \( D \) and \( C = D \cap S_{n+1} \) are finite intersection of members of \( S \). Therefore, \( \mscrC \subseteq \mscrB \). Since we already have the inclusion in the other direction, we conclude that \( \mscrC = \mscrB \).

  \SubProof{Proof that \( \mscrB \) is a base} We will show that the axioms in \fullref{thm:topological_base_axioms/B1} hold.

  \SubProofOf*[thm:topological_base_axioms/B1]{B1} Let \( x \in P \).

  If \( x \) is a \hyperref[def:partially_ordered_set_extremal_points/maximum_and_minimum]{maximum}, then take any other value \( y < x \) and the set \( (y, \infty) \) will contain \( x \). We use here that there is more than one element in \( P \).

  If \( x \) is not a maximum, then \( x \) belongs to any interval \( (-\infty, y) \) whenever \( y > x \).

  In both cases there exists an interval in \( S \) containing \( x \). Thus, \( \bigcup S = P \).

  \SubProofOf*[thm:topological_base_axioms/B2]{B2} Let \( U \) and \( V \) be members of \( \mscrB \). We consider \( 14 \) cases:
  \begin{itemize}
    \item If either \( U = \varnothing \) or \( V = \varnothing \), then \( U \cap V = \varnothing \).
    \item If \( U = (-\infty, u) \) and \( V = (v, \infty) \), then
    \begin{itemize}
      \item If \( u \leq v \), then \( U \cap V = \varnothing \).
      \item If \( v < u \), then \( U \cap V = (v, u) \).
    \end{itemize}

    \item If \( U = (-\infty, u) \) and \( V = (v_1, v_2) \), then
    \begin{itemize}
      \item If \( u \leq v_1 \), then \( U \cap V = \varnothing \).
      \item If \( v_1 < v_2 \leq u \), then \( U \cap V = V =  (v_1, v_2) \).
      \item If \( v_1 \leq u < v_2 \), then \( U \cap V = (v_1, u) \).
    \end{itemize}

    \item If \( U = (u_1, u_2) \) and \( V = (v, \infty) \), then
    \begin{itemize}
      \item If \( u_2 \leq v \), then \( U \cap V = \varnothing \).
      \item If \( u_1 \leq v < u_2 \), then \( U \cap V = (v, u_2) \).
      \item If \( v \leq u_1 < u_2 \), then \( U \cap V = U = (u_1, v_1) \).
    \end{itemize}

    \item If \( U = (u_1, u_2) \) and \( V = (v_1, v_2) \), then
    \begin{itemize}
      \item If \( u_2 < v_1 \), then \( U \cap V = \varnothing \).
      \item If \( u_1 < v_1 < u_2 < v_2 \) then \( U \cap V = (v_1, u_2) \).
      \item If \( u_1 < v_1 < v_2 < u_2 \) then \( U \cap V = V = (v_1, v_2) \).
      \item If \( v_1 < u_1 < u_2 < v_2 \) then \( U \cap V = U = (u_1, u_2) \).
      \item If \( v_1 < u_1 < v_2 < u_2 \) then \( U \cap V = (u_1, v_2) \).
    \end{itemize}
  \end{itemize}

  In all cases, the intersection \( U \cap V \) belongs to \( \mscrB \).
\end{defproof}

\begin{example}\label{ex:def:order_topology}
  Examples of \hyperref[def:order_topology]{order topologies} include:
  \begin{itemize}
    \item The order topology on \( R \), which is equivalent to the \hyperref[def:metric_topology]{metric topology} as shown in \fullref{thm:real_metric_and_order_topologies_coincide}.

    \item All \hyperref[def:ordinal]{ordinals} greater than one induce topological spaces called the \hyperref[def:ordinal_space]{ordinal spaces}.
  \end{itemize}
\end{example}

\begin{definition}\label{def:ordinal_space}
  Let \( \alpha \) be an \hyperref[def:ordinal]{ordinal}. When regarded as the set of smaller ordinals, as shown valid in \fullref{thm:ordinal_is_set_of_smaller_ordinals}, \( \alpha \) is a \hyperref[def:totally_ordered_set]{totally order set} and hence we can endow it with the \hyperref[def:order_topology]{order topology} \( \mscrT \) to obtain a \hyperref[def:topological_space]{topological space}. We call the space \( (\alpha, \mscrT) \) an \term{ordinal space}.
\end{definition}

\begin{proposition}\label{thm:limit_ordinal_order_topology}
  Fix an \hyperref[def:ordinal_space]{ordinal space} \( (\alpha, \mscrT) \).

  An nonzero ordinal \( \beta \in \alpha \) is a \hyperref[def:successor_and_limit_ordinal]{limit ordinal} if and only if it is the \hyperref[def:net_convergence/limit]{limit point} of some net of ordinals in the space \( \alpha \).
\end{proposition}
\begin{proof}
  \SufficiencySubProof Let \( \beta \) be a limit ordinal. When regarded as a subset of \( \alpha \), it is itself a topological net because it is totally ordered. We will show that \( \beta \) as a member of \( \alpha \) is a limit point as a subset of \( \alpha \).

  By \fullref{thm:net_convergence_via_subbases} it is enough to show that \( \beta \) as a net is eventually in every set of the local subbase at \( \beta \) of the order topology. This local subbase consists of all initial and final segments of \( \alpha \) that contain \( \beta \).

  With the following we exhaust the local subbase at \( \beta \): for all \( \gamma \in \alpha \) that are distinct from \( \beta \):
  \begin{itemize}
    \item If \( \gamma > \beta \), then \( \gamma \) itself is a neighborhood of \( \beta \) and a member of the subbase as an initial segment of \( \alpha \). Since the entire net \( \beta \) is contained in \( \gamma \), it is eventually in the initial segment \( \alpha_{>\gamma} \).

    \item If \( \gamma < \beta \), then the final segment \( \alpha_{>\gamma} \) is a member of the local subbase of \( \beta \). Let \( \delta \) be some member of the net \( \beta \).
    \begin{itemize}
      \item If \( \delta > \gamma \), it is itself an ordinal such that \( \varepsilon > \delta \) implies \( \varepsilon \in \alpha_{>\gamma} \).
      \item If \( \delta \leq \gamma \), then \( \varepsilon > \op{succ}(\gamma) \) implies \( \varepsilon \in \alpha_{>\gamma} \). The successor of \( \gamma \) belongs to \( \beta \) because \( \beta \) is a limit ordinal and satisfies \fullref{def:successor_and_limit_ordinal/smaller_successor}.
    \end{itemize}

    Thus, again the net \( \beta \) is eventually in the final segment \( \alpha_{>\gamma} \).
  \end{itemize}

   We have shown that \( \beta \) as a net is eventually in some every set in the local subbase of \( \beta \), thus it is a limit of the net.

   \NecessitySubProof Let \( \beta \) be a limit of the net \( \seq{ \gamma_k }_{k \in \mscrK} \subseteq \alpha \).

   Let \( \delta \in \beta \). Consider the neighborhood \( (\delta, \beta) \) of \( \beta \). Since \( \beta \) is a limit point, there must exist some index \( k_0 \in \mscrK \) such that \( \gamma_k \in \alpha_{>\delta} \) for every \( k \geq k_0 \). Since \( \delta \) was an arbitrary member of \( \beta \), we conclude that \( \beta \) it satisfies \fullref{def:successor_and_limit_ordinal/smaller_successor} and is thus a limit ordinal.
\end{proof}