\section{Architecture}

In this chapter will be discussed deeply the architecture of the three main parts of the \textbf{Perceptron}. The general structure could be summarized by the following schema:
\begin{figure}[H]
	\centering
	\includegraphics[width=12cm]{img/architecture_general_schema.pdf}
	\caption{General Schema}
\end{figure}

\subsection{Multiplication Circuit Architecture}
The Multiplication Circuit, as said before, will be implemented through a Parallel Multiplier. The inputs $b_{i}$ and $w_{i}$ are composed respectively by $b_{x} = 8$ bits and $b_{w} = 9$ bits. In order to compute the multiplication in the correct way, the inputs need to be translated in the \textbf{unsigned form} and then is possible to perform the multiplication with the parallel multiplier.
In the following image is presented the general schema of the Parallel Multiplier:
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{img/architecture_general_parallel_multiplier.pdf}
	\caption{Parallel Multiplier Architecture}
\end{figure}
Notice that the \textbf{sign} of the result will be computed by a simple \textit{XOR} operation between the inputs signs.
The \textbf{Unsigned Parallel Multiplier} architecture is the following:
\begin{figure}[H]
	\centering
	\includegraphics[width=13cm]{img/architecture_parallel_multiplier.pdf}
	\caption{Unsigned Parallel Multiplier Architecture}
\end{figure}
Each logic block is translated with a related logic block:
\begin{figure}[H]
	\centering
	\includegraphics[width=0.7\textwidth]{img/architecture_parallel_multiplier_meaning.pdf}
	\caption{Unsigned Parallel Multiplier Architecture}
\end{figure}
\subsection{Adder Circuit Architecture}
In order to compute the equation (1.1) different sums need to be computed. The building block of this part will be the \textbf{Parallel Adder with Pipeline}: as said before, by adding some registers in between the Carry chains, the critical path impact can be reduced. Furthermore, by exploiting the parallel architecture, a single sum can be computed in a single clock cycle. In the next figure will be presented the Parallel Adder:
 \begin{figure}[H]
 	\centering
 	\makebox[\textwidth][c]{\includegraphics[width=1.5\textwidth]{img/architecture_full_adder_with_pipeline.pdf}}%
 	\caption{Parallel Adder Architecture}
 \end{figure}
In order to obtain an output, after an input drive, there is a need to wait $\floor*{\frac{N}{N_{pipeline}}}$ clock cycles, where $N$ represent the number of bits of $a$ or $b$ and $N_{pipeline}$ represent the maximum number of consecutive FA without a register in between.\\
To implement the whole sum of 11 terms, \textbf{in order to decrease the number of cycles needed} to compute the whole sum and to reduce the number of bits needed, a tree approach has been chosen. The schema of the tree parallel adder is the following:
\begin{figure}[H]
	\centering
	\includegraphics[width=\textwidth]{img/architecture_adder_tree.pdf}
	\caption{Parallel Multiplier Architecture}
\end{figure}

\textbf{Note some extension or left shifts (i.e. for b) were not represented.}\\
Some register has been put in between the sum to limit the critical path impact on the performances and clock period limit.
To obtain a good output after an input drive there is a need to sum to 3 (the maximum number of consecutive register in the previous architecture) each Ripple Carry Adder contribution in terms of number of clock as seen before.

\subsection{Activation Function Circuit Architecture}
At the end of the computation of the latter phase the output is composed by $21$ bits. The computation of the sigmoid function will be done through a \textbf{Look-Up-Table}, which will need \textbf{$2^{21} = $ 2097152 entries} of different outputs with $16$ bits. In order to reduce the size of the Look-Up-Table a truncation is needed: from $21$ bits to $12$ bits. In this case the Look-Up Table will be composed by \textbf{$2^{12} = $ 4096 entries}, but, by exploiting the \textbf{odd symmetry} of the sigmoid, only \textbf{$4096/2 = $ 2048 entries} are needed.
\begin{figure}[H]
	\centering
	\includegraphics[width=0.6\textwidth]{img/architecture_lut_optimized.pdf}
	\caption{Look-Up Table Architecture}
\end{figure}

All things considered, by performing the calculation showed before, there is a need of 26 \textbf{clock cycles} to obtain a correct output after driving an input.


\documentclass{article}
%\documentclass[prl,amsmath,amssymb]{revtex4} % PRL

% margins of 1 inch:
\setlength{\topmargin}{-.5in}
\setlength{\textheight}{9in}
\setlength{\oddsidemargin}{0in}
\setlength{\textwidth}{6.5in}

\usepackage[pdftex]{hyperref} % hyperlink equation and bibliographic citations
\usepackage[dvips]{graphicx,color}
\usepackage{amsmath} % advanced math
\usepackage{verbatim}
\usepackage{natbib} % bibilography
\usepackage{mciteplus} % collapse multiple citations in bibilography
\usepackage{multicol}
\usepackage[toc,page]{appendix} % http://tex.stackexchange.com/questions/49643/making-appendix-for-thesis

% from http://www.flakery.org/search/show/569
%\newcommand{\infint}{\ensuremath{\int_{-\infty}^{\infty}}}
\newcommand{\cf}{\textit{c.f.}} % "compare". In context the abbreviation advises readers to consult other material, drawing attention to related ideas that provide additional arguments or information.
\newcommand{\ie}{\textit{i.e.}} % i.e. is used to explain, clarify or rephrase a statement
\newcommand{\eg}{\textit{e.g.}} % “for the sake of example”. Used to introduce an example or list of examples to illustrate what is being discussed.
\newcommand{\eqn}[1]{Eq.\ (\ref{#1})}
\newcommand{\pfrac}[2]{\ensuremath{\frac{\partial #1}{\partial #2}}}
\newcommand{\pdg}{Physics Derivation Graph}

% http://tex.stackexchange.com/questions/59166/span-column-in-a-multicols-environment
\newcommand{\multicolinterrupt}[1]{% Stuff to span both rows
\end{multicols}
#1
\begin{multicols}{2}
}

\begin{document}
\title{User Manual for the \pdg}

\author{Ben Payne$^{1}$\footnote{Corresponding author: ben.is.located@gmail.com}, Michael Goff$^{2}$\\
{\it $^{1}$Department of Fun, University Name \& Town, city, State Zip}\\
{\it $^{2}$Department of , University of ,  }}

\date{\today}

\maketitle % declares end of title page
\begin{abstract}
Overview of current implementation. It is assumed here that the Introduction to the \pdg\ has been read.
\end{abstract}

\begin{multicols}{2}

%\tableofcontents

%\newpage

\section{Introduction}

% relevant project background
The \pdg\ is a project designed to document mathematical physics knowledge.
The mathematics necessary to describe physics can be written down, in this case in the form of a graph.
The core of this project is a set of databases storing that content and an associated set of scripts for various manipulations of the content.
Those scripts include the ability to generate visualizations of the graph, and to generate reports based on the graph content.

Section~\ref{sec:databases} describes the databases which store these values.

\section{Databases\label{sec:databases}}

The content associated with physics knowledge takes the form of expressions (\textit{equations and inequalities}), symbols, and inference rules.
\textit{An inference rule is an atomic transformation of one expression to another.}
An example is provided here.

Frequency $f$ and period $T$ are related by
\begin{equation}
T\ f = 1
\label{eq:period_and_freq}
\end{equation}
Thus, frequency in terms of the period is
\begin{equation}
f = 1/T
\label{eq:freq_is_inverse_period}
\end{equation}
The relation between \eqn{eq:period_and_freq} and \eqn{eq:freq_is_inverse_period} is that both sides of \eqn{eq:period_and_freq} were divided by $T$.

In this example, there are two mathematical expressions: \eqn{eq:period_and_freq} and \eqn{eq:freq_is_inverse_period}.
These expressions are related by an inference rule: ``Divide both sides of first equation by a value to yield the second equation.''
This inference rule takes an argument, referred to here as the ``feed'', which in this example is $T$.
This set of steps is shown graphically in~Fig.~\ref{fig:freq_period}.
This simple step between two expressions is documented in five separate databases: expressions, symbols, feeds, inference rules, and connections.

Each expression, symbol, and inference rule appears only once in the respective database.
Each time an expression, symbol, or inference rule is used in a derivation, that unique instance is referenced.
This referencing of unique expressions, symbols, and inference rules is done using a numeric identifier (alphanumeric for inference rules).

In the expressions database, each expression has an associated 10 digit number in base 10. For example, \eqn{eq:freq_is_inverse_period} is 2113211456 and \eqn{eq:period_and_freq} is 2131616531. Similarly, the symbols have unique 15 digit numeric identfiers. Period $T$ is 192938440120938 and frequency $f$ is 102938475990112. The database of expressions references these symbol identifiers -- expression 2113211456 uses symbols 192938440120938 and 102938475990112.

The connections database is the set of derivations. A derivation involving \eqn{eq:freq_is_inverse_period} refers to the expression identifier 2113211456 and relates it to 2131616531 by an inference rule which is identified by the alphanumeric string ``dividebothsidesby''. This inference rule takes an argument, referred to here as a ``feed.'' The feed database contains the value, in this example $T$ and a 7 digit identifier 8837284. Feeds are not unique, though the identifiers used are. The $T$ referred to for this use of the inference rule ``dividebothsidesby'' does not need to be associated with other instances of ``dividebothsidesby''.

The \pdg\ is designed to show one instance of each expression, but feeds and inference rules will have duplicates on the graph. To allow for this, the connections database has a unique 7 digit numeric identifier assocaited with each use of an inference rule.

%transition paragraph
The databases supporting the \pdg\ are implemented using comma-separated (CSV) plain-text files with no headers.

\subsection{Implementation: Symbols\label{sec:symbols}}

\begin{verbatim}
<symbol>
  <symbol_name></symbol_name>
  <symbol_punid></symbol_punid>
  <type></type>
  <description></description>
  <cas_sympy></cas_sympy>
  <units></units>
</symbol>
\end{verbatim}

\subsection{Implementation: Expressions\label{sec:expressions}}

\begin{verbatim}
3131111133,"T=1/f"
\end{verbatim}

\subsection{Implementation: Connections\label{sec:connections}}

In the connections database, the
\end{multicols}
\begin{figure}
\begin{verbatim}
"frequency relations",6, "expression",8482459,3131111133,"infrule",2939482,subRHSofExprXintoExprY
\end{verbatim}
\caption{Fig.~1: connections database content example.}
\end{figure}
\begin{multicols}{2}
\section{Database manipulation}

The databases are manipulated using software written in Python\cite{2015_Python} version 2.7.6.
Each script in the \texttt{bin} directory performs a single task.
Common functions are stored in \texttt{lib/lib\_physics\_graph.py}.
The scripts in \texttt{bin} are described here in order of increasing complexity.

\subsection{Listing}

The two simplest scripts are for listing content from the connections and inference rules databases, respectively.
\begin{verbatim}
bin/list_connection_sets.py
bin/list_inference_rules.py
\end{verbatim}

The output of listing the connection sets are the derivations which are listed in the connections database.
\begin{verbatim}
bin/list_connection_sets.py
    frequency_relations
    quadratic_equation_derivation
    euler_equation_proof
    euler_equation_trig_sqrt
    example_of_Godel_problem
    particle_in_a_1D_box
    ...
\end{verbatim}

\subsection{Statistical Analysis}
There are three scripts which build on the listing functionality and count instances of inference rules, expressions, and symbols, respectively.
\begin{verbatim}
popularity_of_inference_rules.py
popularity_of_statements.py
popularity_of_symbols.py
\end{verbatim}

\begin{verbatim}
bin/popularity_of_statements.py
    10 4938429483 \exp(i\ x) = \cos(x)+...
    6 3948574230 \psi(\vec{r},t) = ...
    5 5727578862 \frac{d^2}{dx^2} ...
    ...
\end{verbatim}
For example, \eqn{eq:freq_is_inverse_period} is referenced twice as of this writting.

\subsection{Utilities}
The utility scripts are meant for use by developers of the content.
\begin{verbatim}
create_pdf_of_statements.py
create_pictures_of_inference_rules.py
create_pictures_of_statements_and_feeds.py
generate_new_random_index.py
\end{verbatim}
Creating a PDF of the expressions can be used to validate the \LaTeX syntax of content in \texttt{databases/expressions\_database.csv}

\subsection{Primary functions}

\begin{verbatim}
check_connections_using_CAS_sympy.py
build_connection_set_pdf.py
build_connections_graph.py
\end{verbatim}

The \texttt{build\_*.py} scripts are described in detail in sections \ref{sec:reports} and \ref{sec:visualization}, respectively.

Checking the connections using SymPy is not fully supported.

\section{Report generation\label{sec:reports}}

\LaTeX\cite{1994_Latex} PDFs can be created by running \texttt{bin/build\_connection\_set\_pdf.py}

\section{Visualization of Graph\label{sec:visualization}}

DOT language\cite{2015_Graphviz}

\texttt{bin/build\_connections\_graph.py}

\section{Creating a new Derivation\label{sec:new_derivation}}

\subsection{New Symbol\label{sec:new_symbol}}

First, pick a symbol from your derivation.

Second, check whether that symbol exists in the symbol database. If it does, you are done.

If your symbol is not already in the symbol database, the third step is to get a new  random index (``available symbol numeric identifier'').

Copy the symbol CSV fields to create a new entry

Place the symbol and new numeric identifier into the appropriate field

\subsection{New expression\label{sec:new_expression}}

First, pick the expression to be added.

Second, check whether that expression exists in the expression database. If it does, you are done

If your expression is not already in the expression database, the third step is to get a new random index (``available expression numeric identifier'').

In the expressions database, copy the expression CSV fields into the appropriate derivation.

Place the \LaTeX expression and numeric identifier in the appropriate field.

Determine the SymPy equivalent. Include that in the CSV

Determine the symbols in the expression. See section~\ref{sec:new_symbol}. Include the symbol numeric identifiers in the expression database.

\subsection{New Connection\label{sec:new_connection}}

An inference rule connects expressions

\subsection{New Derivation\label{sec:how_derivation}}

A derivation is composed of a sequence of expressions connected by inference rules

\section{Summary}

\section{Bibliography}

\bibliographystyle{unsrt}
\bibliography{../bibliography} % external bibtex flat-file database
\end{multicols}

\newpage
\appendix
%\begin{appendices}
%\include{appendix_test_cases}
%\end{appendices}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 	Template for seminar reports
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\documentclass[a4paper,pdftex]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amstext}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{bm}
\usepackage{comment}
\usepackage{listings}

% no page number on first page
% \pagestyle{empty}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{acronym}

\newcommand{\dx}{\text{d}x}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 	Paper title
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{Designing interactive webapplications for mathematical visualization}
\subtitle{}

\author{
Christian Karpfinger, Benjamin Rüth\\
Technische Universit\"at M\"unchen\\
Email: benjamin.rueth@tum.de
}

\maketitle

%abstract
We developed a flexible open--source based workflow with the specific application of webdriven, platform independent, interactive applications for mathematical visualization in the context of university education. Our main goal is to realize mathematical visualization with no requirements of programming skills or program installation on the user--side. The Python plotting library Bokeh plays a crucial role in our approach.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 	Sections, Subsections,...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
We wanted to setup interactive webapplications for the visualization of math content, specially in the context of the math lecture for mechanical engineering students. The goal was to have an environment where as few user interaction as possible is necessary to get the application running and with as less visible code as possible. Both restrictions are in our opinion very important, because a non smooth start--up of the application often frustrates the user and especially undergraduate students often do not want or are not able to program (even few lines of code), sometimes even seeing code results in the user immediatelly leaving the webpage. Therefore we put up the following constraints to our application:
\begin{itemize}
\item no installation or log--in required
\item no visible code and no necessity to access or modify code
\item use of open source software
\item interactive modification of math visualizations
\item high flexibility with respect to plotting and interaction
\end{itemize}
Before starting this project we have used videos generated in MATLAB for visualizing mathematical content. At some point we found out that many topics cannot be visualized properly by creating a video. Often it has a much higher effect, if the user is able to play around with parameters and try out different combinations of parameters on ones own.

Our MATLAB videos were --- obviously --- not able to fulfil our constraints. Also other environments like "Wolfram CDF Player" (huge installation, not open source), "IPython Notebook" (too much code interaction) or "Geogebra" (only basic scripting language, not flexible) do not fulfil our criteria and turned out to be not appropriate for that specific task.

The Python framework is in general very flexible, open--source and also extendible, since many scientific applications base on Python. After some research we discovered the plotting library Bokeh, which is similar to matplotlib, but also supports interactive manipulation of the plots with different kinds of widgets as well as running these interactive plots on a server. In the following we want to describe our workflow for setting up an interactive webapp using Bokeh as well as the other used tools.

\section{Installation}
The Framework \href{}{Anaconda} turned out to be a good choice for the installation of all the necessary python modules. The installation of Anaconda is described at
\begin{center}
\href{http://continuum.io/downloads}{http://continuum.io/downloads}
\end{center}

After the installation of Anaconda one can install Bokeh with the following commands:
\begin{verbatim}
$ sudo conda install bokeh
\end{verbatim}
If installation fails due to access rights create environment.
\begin{verbatim}
$ conda create -n my_root
$ sudo activate my_root
\end{verbatim}
Exit environment with
\begin{verbatim}
$ source deactivate
\end{verbatim}
For development one can use the IDE spyder. Running Spyder from \verb!sudo! often causes problems, don't do this! If problems with the libraries occur run
\begin{verbatim}
$ spyder --reset
$ spyder
\end{verbatim}

\section{The idea}
Our goal was to implement an interactive Fourier--Series app. The Fourier--Series of a $2\pi$--periodic  function $f(x)$ is defined in the following way:
\begin{align*}
f\left(x \right) = \frac{a_0}{2}+\sum\limits_{k=1}^\infty \left[a_k \cos\left(kx \right) + b_k \sin\left(kx\right)\right],\\
\intertext{with}
a_k = \frac{1}{\pi}\int\limits_{0}^{2\pi} f\left(x \right)\cos\left(kx\right)\dx \text{\quad and\quad }
b_k = \frac{1}{\pi}\int\limits_{0}^{2\pi} f\left(x \right)\sin\left(kx\right)\dx.
\end{align*}
This representation is exact, if one uses infinitely many coefficients $a_k,b_k$. If one truncuates the series after $n$ coefficients, such that we set $a_k,b_k = 0 \forall k>n$ we do not get an exact representation of $f(x)$, but only an approximation:
\begin{align*}
f\left(x \right) \approx \frac{a_0}{2}+\sum\limits_{k=1}^n \left[a_k \cos\left(kx \right) + b_k \sin\left(kx\right)\right].
\end{align*}
We now wanted to implement a app, where the user has the possibility to choose the value of $n$ and can therefore interactively control the accuracy of the approximation of $f(x)$. An additional goal is to visualize the analytical representation of the fourier series at the same time, such that the user can see different phenomena:
\begin{itemize}
\item The higher we choose $n$ the longer our analytical expression is.
\item Even functions (symmetrical to the y--axis) produce only $\cos$ terms in the series expansion, odd functions (point--symmetrical to the origin) produce onyl $\sin$ terms in the series expansion.
\item The coefficients are decreasing along the series.
\end{itemize}
Of course one can also proof all these properties, but seeing them "in action" often has a bigger effect.

\section{Implementation}
The mathematical functionality has been implemented in the file \verb!fourierFunctions.py!; these functions just supply us with some example functions $f(x)$ and functions for the calculation of the coefficients and the evaluation of the fourier series. We will not explain these in detail.

The dynamic generation of \LaTeX\ strings depending on the degree of the fourier series and $f(x)$ is done in \verb!fourierTex.py!.

The most important part of our implementation is in \verb!fourierApp.py!. This file sets up the web app and uses the previously mentioned files to correctly handle user input.

These functions are only able to publish our app directly on the Bokeh plotting server. For more elaborate functionality (like the dynamic update of \LaTeX\ strings) we feed the output of the Bokeh plotting server to a \href{}{Flask} server(which is able to handle to \LaTeX\ strings sent via an request). The \LaTeX\ strings are finally embedded into the html file via \href{https://www.mathjax.org/}{MathJax}.
All source files can be found in the appendix as well as on GitHub: \href{}{}

\section{Running the App}
For running the app one firstly has to start the Bokeh plotting server via
\begin{verbatim}
$ bokeh-server -m --backend=memory
\end{verbatim}
Then one has to run the script starting the App on the plotting server
\begin{verbatim}
$ python fourierApp.py
\end{verbatim}
One can now access the App at the plotting server. For enabling displaying of dynamically generatex \LaTeX\ strings one has to run
\begin{verbatim}
$ python fourierFlask.py
\end{verbatim}
for starting the Flask server. The FourierApp can now be accessed from any Browser.

\section{Conclusions}
With Python and Bokeh we have the possibility to realize a large field of visualizations. Additionaly to sliders, Bokeh also supplies many other Widgets like buttons, checkboxes, dropdown--lists etc. as well as many different kinds of 2D plots.
Embedding These Bokeh plots in a Flask environment gives us additionaly the possibility to modify the html content directly from our app.
Currently we are working on running this app on a server as well as developing more interactive visualizations. Another already completed app is the visualization of different ODE solvers where the user is able to control parameters as step size and initial value.

\appendix
\section{Listings}
\subsection{fourierApp.py}
\lstinputlisting[basicstyle=\scriptsize]{fourierApp.py}
\subsection{fourierFunctions.py}
\lstinputlisting[basicstyle=\scriptsize]{fourierFunctions.py}
\subsection{fourierTex.py}
\lstinputlisting[basicstyle=\scriptsize]{fourierTex.py}
\subsection{fourierFlask.py}
\lstinputlisting[basicstyle=\scriptsize]{fourierFlask.py}
\end{document}


\chapter{The Lambek bijection}
\label{chap:4}
\thispagestyle{empty}
In this chapter we raise bridges between Category Theory and simply typed $\lambda$-calculus. The main sources for this chapter are \cite{lambek1988introduction}, \cite{lambek1985cartesian} and \cite[Chapter 6]{selinger2008lecture}.\\

In this chapter we do an important work of unification and organization of the sources used. In general, whenever the connection of category theory and $\lambda$-calculus is presented, these concepts are introduced in their most similar description. We consider that this approach, while requiring less work, loses in knowledge and intuition.\\

Avoiding this work means that the bijection does not convey a fundamental idea. A closed Cartesian category naturally extracts the essence of programming. In particular, (following the notation of the definition), it is the adjoint $G_3$ and the unity of this adjoint what allows us to consider functions and application of functions as elements of the category, thus providing a computational system.\\

In this chapter we will begin by giving the anticipated definition of a deductive system, and from this we will be able to give an equational definition of closed Cartesian categories.  After this we will define the categories of the various $\lambda$-calculi, and finally prove the existence of an adjoint equivalence between the two categories.\\

\section{Deduction systems}
Having introduced the notion $\lambda$-calculus, category theory and propositional intuitionistic calculus, is time to to go a step back to get the full picture.

\begin{definition}
  A \emph{deductive system} $D$ is a graph  with identity and composition of arrows.
\end{definition}

We can reformulated the previous logic with this optic. Prior to that we will make an illustrative example on how is this made. All the properties aforementioned (and paired) show that the requirements of typing/intuitionistic calculus can be expressed in simple requirements for the arrows. Let us show some examples.
\begin{example}
  \begin{enumerate}
  \item Deduction rule $\to_1$: There is an arrow $$\varepsilon_{A,B}: [B\land (B \to A)] \to A.$$
  \item From the deduction rule $\to_2$ it can be deduced that, from the assumption $C \land B \to A$:
    \begin{align*}
      \Gamma,\{\} \vdash & F= (C\land B)\to A;\\
      \Gamma, \{(C\land B)\} \vdash & A;\\
      \Gamma, \{C, B\} \vdash & A;\\
      \Gamma, \{C\} \vdash & B\to A;\\
      \Gamma, \{\} \vdash & F'=C \to (B \to A).
    \end{align*}
    That is provided an arrow $h: C\land B \to A$ there is an induced arrow
    $$h^*:C \to [A\to B].$$
  \end{enumerate}
\end{example}

We can check that the second example includes an idea about the relation between assumption and deduction systems. We can view each assumption of truth as a new deduction system. This also seems logical, since an assumption $\Gamma = \{A_i : i=1,...,n\}$  with $A_i$ formulas can be seen as an existence of an arrow $g_i: \top \to A_i$.\\

% We see that in our derivations we sometimes need to change the truth-assumption $\Gamma$. This is not mandatory, but it is useful to avoid including a large number of rules. However we do that in a way such that we start from a given $\Gamma$ and return to it at the end of the derivation. Thus knowing that if a $\Gamma$ derives the formula $F$, then it also derives the formula $F'$ and therefore there is an arrow in our deductive system. Similar reflections can be made with the rest of the rules discussed in the previous sections, and of course with the simply typed $\lambda$-calculus. \\

\subsection{Logical Systems as Deduction systems}
In  the fashion of the previous definitions we can reformulate logical systems as deduction systems.
\begin{definition}
  A conjunction calculus is a deductive system $D$ where there is a specified object $\top$ and an operation $\land: Ob(D)\times Ob(D) \to Ob(D)$ such that:
  \begin{enumerate}
  \item For each $A\in Ob(D)$ there exists an arrow $O_A:A\to \top$.
  \item For each $A_1,A_2\in Ob(D)$ there exist arrows $\pi_i^{A_1,A_2}: A_1 \land A_2 \to A_i$ for $i=1,2$.
  \item Let $f:C\to A$ and $g:C\to B$ be arrows of D . Then $\langle f,g\rangle : C\to A\land B$.
  \end{enumerate}
\end{definition}

Note that $\pi_i^{A_1,A_2}$ depends on both $A_1,A_2$. In the absence of ambiguity we will denote this arrow by $\pi_i$.

\begin{definition} \label{def:positivecalculus}
  A positive intuitionistic calculus is a conjunction calculus $D$ where there is binary an operator $\leadsto: Ob(D)\times Ob(D) \to Ob(D)$ such that:
  \begin{enumerate}
  \item For each $A,B \in Ob(D)$, there is an arrow $\varepsilon^{A,B}: [(B \leadsto A) \land B]\to A$.
  \item For each $g:C\land B \to A$ then there is an arrow $g^*: C\to [B\leadsto A]$.
  \end{enumerate}
\end{definition}
\begin{remark}
  In the future, in absence of ambiguity, we will use $\to$ instead to $\headsto$. We will also make use of $[]$ to avoid ambiguity whenever possible.
\end{remark}
Similarly to $\pi_i$, we will usually denote just $\varepsilon$.
\begin{definition}\label{def:intuipropcalculus}
  A \emph{intuitionistic propositional calculus} is a positive intuitionistic calculus where there is a specified object $\bot$ and an operation $\lor: Ob(D)\times Ob(D) \to Ob(D)$ such that:
  \begin{enumerate}
  \item For each $A\in Ob(D)$ there is an arrow $\square_A: \bot\to A$.
  \item For each $A_1,A_2\in Ob(D)$ there are arrows $\iin_i: A_i\to A_1 \lor A_2 $.
  \item For each $A,B,C \in Ob(D)$ there is an arrow $$h:[(B\leadsto A)\land (C\leadsto A)] \to [(C\lor B)\leadsto A].$$
  \end{enumerate}
\end{definition}

\begin{definition}
  Given a conjunction calculus $D$, for every $f:A\to B \in D$ there exists an arrow $f^\to: \top \to [A\to B]$, defined as
  $$f^\to = \left(f\circ \pi_2^{\Top,A}\right)^*.$$
\end{definition}

This last definition has an important meaning. It formalizes within our system the fact that if we have a proof that $A$ implies $B$, in the form of an arrow, it generates a proof that the statement "$A$ implies $B$", as an object, is also true (since it can be derived from truth value $\top$). We finish this section with the definition of assumption and the deduction theorem.\\

As we have seen during the last part of the previous chapter, there is a constant work in the deduction system in changing each truth assumption. So the question is: how can we adapt this idea to a deduction system $D$? This is done by introducing \emph{assumptions}.

\begin{definition}\label{assumptions}
These assumptions are arrows $x^A: \top \to A$, which does not have to be in $Ar(D)$\footnote{That is, an assumption can be considered as an extra edge in the graph created by the deduction system.}. These arrows allow us to consider new implications. Given $x^A$ we can consider proofs $\varphi(x^A)$ by composition of arrows in $D$ with $x$.
\end{definition}
This derive lastly in  a proposition, widely known in the context of propositional logic:

\begin{theorem}[Deduction Theorem]\label{deduction-theorem}
  In a conjunction calculus $D$, a proof $\varphi(x^A): B \to C$ dependent on an assumption $x^A: \top \to A$ implies the existence of a proof $\lambda x^A.\varphi (x^A): A\land B \to C$ not dependent on $x^A$.
\end{theorem}
\begin{sproof}
  As most proofs in the previous chapter, this proposition is proved by induction on the length on the proof, and studying the list of possible last steps taken. A full proof can be found in \cite[proposition 2.1]{lambek1988introduction}.
\end{sproof}
 We are implicitly using the idea of the adjoint $f:A\times B \to C \cong f: A \to C^B$, although it is not needed for the formalism.

\subsection{Categorical systems as Deduction systems}

We have already noted that a category is a particular case of a deduction system. We can consider how are the logical systems, in the view of the Curry-Howard bijection, but we only focus on category systems.

\begin{remark}Now that  we see categories as deduction systems, we have a notation problems. We will identify $A\land B$ with $A\times B$. While in a deductive system we have defined $\langle f_1,f_2\rangle : C \to A\land B$ we have done the same in categories, meaning $$\langle f_1,f_2\rangle : C \land C \to A \land B.$$  This was necessary in deduction system to not put a burden of having a diagonal arrow $C\to C\land C$. This make though some extra verbosity in functions like $\langle h\pi_1, \pi_2\rangle : C\times B \to A\times B$, that would otherwise be the much more legible $\langle h, 1_b\rangle: C\times B \to A \times B$.
\end{remark}

\begin{proposition}\label{prop:CartCategory2}
  A Cartesian category $\mathcal{C}$ is a deduction system that is both a conjunction system and a category satifying:
  \begin{enumerate}
  \item If $f:A\to T$ and $g:A\to T$ then $f=g$.
  \item Given $\langle f_1,f_2\rangle: C\to A\land B$ then $\pi_i \circ \langle f_1,f_2\rangle = f_i$.
  \item Given $h:C\to A\land B$ we have that $\langle \pi_1 h, \pi_2 h\rangle = h$.
  \end{enumerate}
\end{proposition}

\begin{proof}

  Lets begin by proving that a Cartesian category satisfy this conditions. It is clearly a category. Let us consider as terminal object $T=\top$ and let us take as product $A\times B = A\land B$ for every $A,B \in C$ along with the projections $pi_i$, the properties are satisfied.\\

  Conversely, considering again $T= \top$ we can check that it is a terminal object. Also, for every $h:C\to A\land B$ we can see that every $h$ derive the existence of unique $f_1,f_2$ such that:
  \[
    \begin{tikzcd}
      {} & C \arrow[bend right,swap,dashed]{dl}{f_1}
      \arrow[bend left,dashed]{dr}{f_2} \arrow{d}[description]{h}& & \\
      A  &A\land B \arrow{l}[swap]{\pi_1} \arrow{r}{\pi_2} &
      B \\
    \end{tikzcd}
  \]
  And thus we have finite product.
\end{proof}

Thus, we can start seeing the kind of bijection already shown in the Curry Howard Isomorphism. Hitherto we can adopt the notation $A\times B$ for the conjunction, in the context of Cartesian categories.
\begin{proposition}\label{def2:CCC}
  A closed Cartesian category $\mathcal{C}$ is a deduction system that is a positive intuitionistic calculus and a category, and which satisfies:
  \begin{enumerate}
  \item   For all $h: c\times b \to a$ , we have that $\varepsilon \langle h^* \pi_1, \pi_2\rangle = h$.
  \item   For all $k: c\to [b \to a]$ , we have that $(\varepsilon \langle k \pi_1, \pi_2\rangle)^* = k$.
  \end{enumerate}
\end{proposition}

\begin{proof}

  Let us begin by checking that $\mathcal{C}$ is a positive intuitionistic calculus and holds 1. and 2. As it is a Cartesian category, it is a conjunction calculus.
  \begin{itemize}
  \item We identify $a\to b$ with the exponential object $b^a$. For every $b\in Ob(C)$, following the notation from definition \ref{def:CCC}, we have an adjoint $F_3^b\dashv G_3^b = (F^b,G^b,\varphi^b,\eta^b,\varepsilon^b)$. Then, we define the arrows from definition \ref{def:positivecalculus} as
    $$\varepsilon^{a,b}: [(b \to a) \land b]\to a = \varepsilon^b(a), \qquad g^* = \varphi(g).$$
  \item Then, 1. is obtained by considering that, given a morphism $h: F_3^b c = c\times b \to a$, we have that, $\varepsilon^b(a) \circ F_3^b(\varphi(h)) = h$, due to the fact that $$(\varphi^b)^{-1}(h: d \to Gc) = \varepsilon^b(c)\circ F_3^b(h).$$
  \item Conversely, if part 1. was the left part of the inverse definition, 2. is the right part.
  \end{itemize}

  To prove that any intuitionistic calculus that is also a category is a closed Cartesian category, we just made the same pairing, $a\times b = a\land b$ and $a\to b = b^a$ to define $F_b^3$ on both objects and arrows\footnote{where $F_b^3(h:a\to a') = \langle h, 1_b\rangle $} define $G_3^b$ on objects, and define $\varepsilon^b(a)$ as $\varepsilon^{a,b}$.
\end{proof}

  % As it is a cartesian category, by it does satisfy the requirements for being a conjunction calculus. Pairing the exponential object $cA^B$ with the function object $B\to A$. As in definition \ref{def:CCC}, we have  3 adjoints, the first two of them are already considered as we have that $C$ is a cartesian category. We now study the third of them and remember that $F_3^B=F$ and $G_3^B=G$ are both adjoint, i.e.:
  % $$\hom_C(C \times B, A) \equiv \hom(C, A^B)$$

  % Therefore, we can check the second part in definition \ref{def:positivecalculus}. Then, remembering proposition \ref{prop:univAdjoint}, we can deduce the existence of a natural transformation  $\varepsilon: FG \to I_C$ i.e. $\varepsilon: A^B\times A = (B\to A)\land A \to A$, having the first part in definition \ref{def:positivecalculus}.\\

  % The proof of 1. and 2. are conditions of universality. Lets unpack 1. as 2. is the same reasoning on the other side of the adjoint. We have (remember that, in the notation of part I: $\langle h^*\pi_1, \pi_2\rangle = \langle h^*,1_B\rangle$, and we used to note $h^*=\varphi(h)$):
  % \[
  %   \begin{tikzcd}
  %     C\times B\arrow{d}{h}&C\arrow{d}{h^*} & C\times B\arrow{d}{\langle h^*,1_B\rangle} & A^B\times B\arrow{d}{\varepsilon}\\
  %     A & A^B = B\to A & A^B\times B & A
  %   \end{tikzcd}
  % \]

  % therefore, putting it all together, and remembering the \emph{evaluation} fashion of $\varepsilon$ (i.e., we have that the following diagram commutes. The

  % \[
  %   \begin{tikzcd}
  %     C\times B\arrow{d}{h} \arrow{r}{\langle h^*,1_B\rangle}& A^B\times B \arrow[swap]{dl}{\varepsilon}\\
  %     A&
  %   \end{tikzcd}
  % \]

  % To do the converse, is enough to made the same pairings and apply the notions of universality of $\varepsilon$ in  and apply \ref{prop:equivdefinition} to check that such adjoints are define via $\arepsilon$.

\begin{proposition} A \emph{closed bi-Cartesian category} is a deduction system that is both a intuitionistic propositional calculus and a category, with the additional equations:
  \begin{enumerate}
  \item $f:\bot \to A \implies f=\square_A$.
  \item Given $[f_1,f_2] : A\lor B \to C$ then $ [f_1,f_2]\circ \iin_i  = f_i$.
  \item Given $h:A\lor B\to C$ we have that $[ h\iin_1, h\iin_2] = h$.
  \end{enumerate}
\end{proposition}
\begin{remark}
  Is usual to denote $A\lor B$ as $A+B$ or $A\sqcup B$, as we are working with the coproduct.
\end{remark}
\begin{proof}
  We have proved that it is already a closed Cartesian category. Extra considerations are dual to those already considered for a Cartesian category.
\end{proof}
\subsection{Polynomials in closed Cartesian categories}

We want now to introduce the concept of logical implications in the context of a closed Cartesian category $C$. For that, we can use the naive idea to consider the terminal object $\top \in C$, as a value of truth, and then deduce that any object $a\in C$ such that there exists an arrow $f:\top \to a$, then $a$ is also true. In this idea, the natural progression is to consider a $a$ to be true by desire, so we add a new arrow $x:\top \to a$ to the category, and to complete the category so the axiom of metacategories are still satisfied. In this section we are going to formalize this idea.
\begin{definition}
  Given a (Cartesian, closed Cartesian) category $C$, we say that an \emph{indeterminate} arrow $x: a \to b; a,b \in C$ is an arrow $x:a \to b$  not necessarily included in $Ar(C)$.
\end{definition}
\begin{remark}\label{remark-indeterminates}
  Whenever we talk about an indeterminate arrow, we just talk about an arrow with the special quality that it does not have to exist within our predefined set of arrows. The new emphasis is that we do not care what the arrow is exactly, in a similar way as it is usually do with numeric variables.
\end{remark}
\begin{remark}
  Indeterminates $x: 1\to a$ are assumptions, as in definition \ref{assumptions}.
\end{remark}

\begin{definition}\cite[Part I, Chapter 5]{lambek1988introduction}
  Given a (Cartesian, closed Cartesian) category $C$, and a indeterminate $x: a \to b$, we define the \emph{category of $C$ based on the indeterminate $x:a\to b$}, denoted by $C[x]$, as the category that has:
  \begin{enumerate}
  \item As objects the same object as in $C$.
  \item As arrows, we have $Ar(C[x])$ is the collection of words with alphabet $Ar(C) \cup {x}$ quotiented by the equivalence relations:
    \begin{itemize}
    \item Preserve   $C$-equivalence: for all $g:b\to c,f:a\to b, h: a\to c\in C$,
      \begin{center}
        $gf=h \in C\qquad $ implies $\qquad gf \cong h\in C[x].$
      \end{center}

    \item Preserve composition: for all $f:a\to b,f':a\to b,g:b\to c,g': b \to c\in Ar(C[x])$,
      \begin{center}
        $f\cong f', g\cong g'\qquad$ implies $\qquad fg\cong f'g'$.
      \end{center}

    \item Preserve identity: for all $f:a\to b\in C[x]$ we have $f1_a\cong f \cong \con 1_bf$.
    \item Associative: for all $f:a\to b, g:b\to c, h: c\to d$ we have $(fg)h\cong f(gh)$.
    \end{itemize}
  \end{enumerate}
  Thus arrows are equivalence classes. Arrows in $C[x]$ are called \emph{polynomials} in $x$.
\end{definition}
\begin{remark}
We have defined the equivalence on arrows $\cong$ so that the inclusion $C\hookrightarrow C[x]$ is a functor.
\end{remark}
\begin{definition}
  Let $C$ be a (Cartesian, closed Cartesian) category and let $X=\{x_1:a_1 \to b_1, ... x_n:a_n \to b_n\}$ be a finite  set of indeterminates. We define the category $C[X]$, also denoted as $C[x_1,...,x_n]$, as $C[x_1][x_2]..[x_n]$.
\end{definition}
\begin{remark}
  Note that for every permutation $\sigma : \{1,...,n\}\to \{1,..,n\}$, we have that $C[x_1]...[x_n]\cong A[x_{\sigma(1)}]...[x_{\sigma(n)}]$.
\end{remark}

% \begin{remark}
%   Note that the terms can be formed by the arrows $1\to A\in C$, the indeterminates $x^A: 1 \to A$,  and the term forming operations:
%   \begin{align*}
%     a,b,f,\varphi(x^A) := \langle a,b\rangle: 1 \to A\times B \ |\ fa: 1 \to B\ |\ \lambda x^A. \varphi(x^A): 1 \to [A\to B]
%   \end{align*}
%   where we require $a: 1\to A, & b: 1 \to B,   f: A \to B, \varphi(x^a): 1 \to B$
% \end{remark}
\begin{proposition}\label{unique-polynomial-functor}Let $C,C'$ be (Cartesian, closed Cartesian) categories, let $F$ be a (Cartesian, closed Cartesian) functor, and $X=\{x_1,...,x_n\}, X'=\{x_1',...,x_n'\}$ be sets of assumptions for $C$ and $C'$ respectively. Then there is an unique (Cartesian, closed Cartesian) functor $F_{x_1,...,x_n}$ such that $F(x_i)=x_i'$ and the following diagram commutes.
  \[
    \begin{tikzcd}
      C[X]\arrow[dashed]{rr}{F_{X\to X'}}&& C'[X]\\
      C \arrow{rr}{F}\arrow[hookrightarrow]{u}{}&& C'\arrow[hookrightarrow]{u}{}
    \end{tikzcd}
  \]
\end{proposition}
\begin{proof}
  Immediate as $C[x_1,...,x_n]$ is the expansion of $C$ and ${x_1,...,x_n}$.
\end{proof}

This construction satisfies a nice property:

\begin{proposition}\label{univ-polynomial}
  Given a (resp. Cartesian, closed Cartesian) category $A$, an indeterminate $x:a_0\to a$ over $A$, a functor $F:A\to B$ and an arrow $ b:F(a_0) \to F(a) \in B$, there is a unique (Cartesian, closed Cartesian) functor $F': A[x]\to B$ such that the following diagram commutes:
  \[
    \begin{tikzcd}
      A\arrow[hookrightarrow]{r}{} \arrow[rd, "F", swap]      & A[x] \arrow[d, "F'", dashed]& x:a_0\to a\arrow[d,"f'",dashed]\\
      &B& b:F(a_0)\to F(a)
    \end{tikzcd}
  \]
\end{proposition}
\begin{proof}
  Observe that polynomials in closed Cartesian categories have to be defined inductively, with the following rules:
  \begin{enumerate}
  \item $x$ is a polynomial.
  \item Every arrow $f:a \to b\in A$ is a polynomial.
  \item The composition (resp.  [composition and product],  [composition, product and $*$-operation]) of every polynomial is a polynomial.
  \end{enumerate}
  Thus, $F'$ is defined by the values on $x$ and $Ar(A)$, so it exists. As in this case we have both  $x$ and $Ar(A)$ fixed, $F'$ is unique.
\end{proof}

We finish this subsection presenting a refinement of the proposition \ref{deduction-theorem}. This result is also proved by induction on the length of the proof.

\begin{proposition}[functional completeness]
  For every polynomial $\varphi(x^a): 1\to C$ over a closed cartesian category $C[x^a]$ with an indeterminate $x:1\to a$, there is an unique arrow $f$ such that $f\circ x=\varphi(x)$.
\end{proposition}

\section{Categorical understanding of simply typed $\lambda$-calculus}
After introducing the new optic explained in the previous section, we will use it to complete the Lambek equivalence. For this we will make a series of preparations. We first introduce the concept of natural numbers in categories and in $\lambda$-typed calculations.\\

The introduction of the natural numbers in $\lambda$-calculus has been delayed, because it is a construction that is inspired by our own construction for categories, and we believe that by explaining them straight away we can grasp the underlying idea of the natural numbers in a more efficient way.\\

We will proceed by introducing the basic $\lambda$-calculus as a category, and explaining the categorical equivalence between it and the category of closed Cartesian categories.

\subsection{Natural Numbers}

We begin this section by motivating the introduction of natural numbers. The importance of this concept lies in the fact that we want a computation system. These systems, in a modern practical case, are actually intended to compute a function from the natural numbers to the natural numbers (either on a tape in a Turing machine, a recursion as in Gödel's computational system, or an abstract formalization in $\lambda$-calculus).\\

We have already seen how natural numbers can be done in untyped calculus. However in this section we will approach the problem, formalizing the notion for typed calculus and introducing the concept of natural numbers in categories.

\subsubsection{Natural Numbers in Categories}

\begin{definition}
  Given a closed Cartesian category $C$, a tuple  $(N,S:N\to N)\in Ob(C)\times Ar(C)$ is said to be a \emph{natural number system} when, for every $f:A\to A\in C$, there is an unique $h$ such that
  \[
    \begin{tikzcd}
      1\arrow{dr}{a}\arrow{r}{0} & N\arrow{r}{S}\arrow[dashed]{d}{h} & N\arrow[dashed]{d}{h}\\
      & A\arrow{r}{f} & A
    \end{tikzcd}
  \]
  When $h$ exists but is not necessarily unique $(N,S)$ is said to be a \emph{weak natural number system}.
\end{definition}
Every time natural numbers are mentioned, induction have to appear in some part. The intuition of the categorical concept is that we have a object $N$ that is the set of every natural, and a successor arrow $S$.

We can consider natural numbers systems as an initial object. Having a closed Cartesian category $C$, we can consider it as a marked category $C_*$, such as in $Set_*$, with the specified object being $\top =1$. We can also consider the category $J$ provided by the diagram:

\[
  \begin{tikzcd}
    1\arrow{r}{0} & N\arrow{r}{S} & N
  \end{tikzcd}
\]
with the specified point being $1$. Then considering the functor category $C_*^J$ we can see that every arrow has the form:
\[
  \begin{tikzcd}
    1\arrow{r}{0}\arrow{d}{1_1} & N\arrow{r}{S}\arrow{d}{h} & N\arrow{d}{g}\\
    1\arrow{r}{a} & A\arrow{r}{f} & A
  \end{tikzcd}
\]
and is denoted by $(1,h,g)$. Considering the category $(C_*^J)'$ with objects as in $C_*^J$ and arrows  $\{(1,h,g)\in C_*^J: h=g\}$ we have that a natural number for $C$ is an initial object in $(C_*^J)'$. This observation allow us allows us to provide a simple proof of the following result a consequence of proposition \ref{prop:initial}:

\begin{proposition}
  Any two natural number objects are isomorphic.
\end{proposition}

\begin{definition}[Category of closed Cartesian categories]
  The \emph{category of closed Cartesian categories with natural numbers} $CCart_N$  is the category that has as objects all small closed Cartesian category with natural number systems, and as arrows all functors that preserve the structure of closed Cartesian categories and added natural numbers structure.\\
\end{definition}
% {\color{red} Maybe add the proposition and corollary 9.1 and 9.2 of Lambek.}

\subsubsection{Natural Numbers in simply typed $\lambda$-calculus}\label{section:natural-revisited}

As a consequence of the Curry-Howard isomorphism, we obtained an identification between logic and lambda-calculus. Afterwards, as we start considering different deduction system  (conjunction calculus, positive, intuitionistic) in logic. Analogously, we can consider different typed $\lambda$-calculus (basic, standard, expanded), each one paired with a deduction system. Finally, we restricted these deduction systems to the world of categories which gave us also another set of paired categories (Cartesian, closed Cartesian, closed bi Cartesian). \\

Having laid the intuition of what a natural number is in category theory we can proceed to explain what a natural number is in simply yped $\lambda$-calculus. Relating the notions of typed $\lambda$-calculus shown in \cite[Section 10]{lambek1988introduction}, \cite[Section 6]{selinger2008lecture} and \cite[Section 10]{cardone2006history}, we can see that it is opted by Lambek to  include, for simplicity, some structure over the basic type $\iota$  to provide with a natural number type:

\begin{remark}
  Remember that $\iota$ was a basic type with no added structure.
\end{remark}
\begin{definition}[Lambek natural numbers]
  \emph{Natural number in simply typed lambda-calculus} is a simply typed $\lambda$-calculus with the added structure for raw typed $\lambda$-terms:
  \begin{align*}
    A,B, C ::= ...\ |\ \mathfrak{o}\ |\ S(A)\ |\ I_t(A,B,C);
  \end{align*}
  for every type $t$, and the related typing rules:
  \begin{itemize}
  \item If $N$ is of type $\iota$ so is $S(N)$:
    $$  (S)\qquad  {\displaystyle\Gamma \vdash N:\iota \over \Gamma \vdash S(N):\iota}.$$
    In addition, $\mathfrak{o}$ is of type $\iota$:
    $$  (\mathfrak{o})\qquad  {\displaystyle\over \Gamma \vdash \mathfrak{o}:\iota}.$$
  \item If $M: A$, $H: A\to A$ and $N : \iota$ then we can apply $H$ to $M$, $N$ times and get a result in $A$:
    $$  (I)\qquad  {\displaystyle\Gamma \vdash N:\iota, \qquad \Gamma \vdash M:A,\qquad \Gamma \vdash H:A\to A \over \Gamma \vdash I_A(M,H,N):\iota}.$$
  \end{itemize}
\end{definition}

\begin{remark} \label{remark-natural-numbers}
  Natural number systems can be defined over any of the different typed $\lambda$-calculus since we made the definition by simply adding structure to the term formation rules.
\end{remark}

This option has some positive notes:
\begin{enumerate}
\item We have, as in categories, an object $N$ that is a natural number.
\item It encapsulates in a formalism the properties of Church numerals used in untyped $\lambda$-calculus.
\end{enumerate}

Nonetheless we have already carried out a construction over natural numbers in untyped $\lambda$-calculus. That construction is replicable in the context of typed $\lambda$-calculus.

\begin{definition}[Church numerals in typed $\lambda$-calculi]
 We define the $n$-th Church numeral in typed $\lambda$-calculi as, for a type $t$:
  $$\overline n^t = \lambda f^{t \to t}. \lambda x^{t}. f^{n}(x).$$

\end{definition}

\begin{remark}
  $\overline n^t$ is of type $(t\to t)\to(t\to t)$.
\end{remark}

As a consequence of this proposition, we can consider a type $N^t: (t \to t) -> (t \to t)$ such that $\overline n^t:N^t$ for every $n\in\mathbb{N}$ and every type $t$. Nonetheless:
\begin{enumerate}

\item To have a similar behaviour of multiple application of a function, the natural numbers are dependent on the type. In this setting, strictly speaking, there is a natural number family for each type $t$.
\item There is not a type exclusively for natural numbers, as not necessarily every element of $(t\to t)\to(t\to t)$ is a natural number.
\end{enumerate}
From this point on, we will use the Lambek description of natural numbers, letting Church numerals as a construction derived for untyped $\lambda$-calculus.\\

% Lastly, we can consider the effect of Lambek description in $\beta$-reduction.

\subsection{The category of simply typed $\lambda$-calculus}

The objective of this section is to finish forming an idea that has been present for some time in this work: the existence of several $\lambda$-calculations. \\

In chapter 3, we have described how there exists different $\lambda$-calculus, the same way that there exists multiple groups, as either $\lambda$-calculus and a group is only a structure,. We may continue by defining the category of $\lambda$-calculus. For that we first define the concept of morphism as a structure preserving mapping.

\begin{definition}[Morphisms of lambda-calculus]
  Let $L, : '$ be minimal (resp. basic, expanded) $\lambda$-calculus (resp. with natural numbers). A morphism $F: L \to L '$ is a function that
  \begin{enumerate}
  \item Maps types in $ L$ to types in $L '$, Terms in $L$ to terms in $L '$ and if $a\in L$ is a closed term, so is $F(a)$, and sends variables to variables. For example: $$F(1) = 1, \qquad F(*) = * \qquad or \qquad F(A+B) =F(A)+F(B).$$

  \item Preserves the structure of minimal (resp. basic, expanded) $\lambda$-calculus (resp. with natural numbers) up to $\alpha$-equivalence, $\beta$-equivalence and $\eta$-equivalence.

  \end{enumerate}

  Morphisms of $\lambda$-calculus will be also called $\lambda$-morphism for short, in cases of ambiguity.
\end{definition}

\begin{definition}[Category of lambda-calculus]
  We can define the category $\LC$ of minimal simply typed $\lambda$-calculus (resp. basic, expanded)  as the category that:
  \begin{itemize}
  \item Has as objects the different minimal (resp. basic, expanded) typed $\lambda$-calculus.
  \item Has as arrow the $\lambda$-calculus morphism.
  \end{itemize}
  Similarly, we can define the category of $\LC_N$ basic typed $\lambda$-calculus with natural numbers as in remark \ref{remark-natural-numbers}, adding that a morphism should also preserve natural numbers.
\end{definition}

\subsection{The internal language of a Closed Cartesian Category}

Up to this moment, we have related that a closed Cartesian category is a positive intuitionistic calculus, which is equivalent to a typed $\lambda$-calculus, with some extra structure.\\

Now we have to consider the original ideas that were the precursor of categorical thinking: the Poincaré functor. This functor, in short,  sees how to consider a group within a topology, and see that we can infer group morphisms. This is the original idea behind the functors, and we will now provide an example that is similar in flavor to the Poincaré functor: we will generate a $\lambda$-calculus from a closed Cartesian category.\\

\begin{definition}
  Let $C$ be a closed Cartesian category. Then its \emph{internal language} $\LL (C)$ is the basic $\lambda$-calculus that:
  \begin{itemize}
  \item Has as types the objects of $C$, with function types given by exponential types, conjunction types by product types, and the 1 type by the terminal object $\top \in Ob(C)$.% \begin{table}[!h]
    % \begin{center}
    %   \begin{tabular}{|l|c|c|}
    %     \hline
    %     Typing name & Types  & Closed Cartesian categories  \\
    %     \hline
    %     Minimal     & Function type $A\to B$   &  Exponential object $B^A$  \\
    %     \hline
    %     Basic      & Type 1 & Terminal object $\top$ \\
    %     & Product type $\times$ & Product object $\land$ \\
    %     \hline
    %   \end{tabular}
    %   \caption*{\label{tab:table-name} Pairing of Types and object in closed Cartesian categories.}
    % \end{center}
    % \end{table}A
  \item In $\LL(A)$ a variable $x^{t_i}$ of type $t$ is just an indeterminate  $x:1 \to t_i$, and terms of type $A$ are polynomial $\varphi(x_1^{t_1},...,x_n^{t_n}):1 \to A$, where each indeterminate $x_i: 1 \to t_i,$ for $i$ in $1,...,n$ represents a free variable.
  \item We endow this set with the structure of a simply typed $\lambda$ calculus as follows\footnote{Instead of typing rules we provide equivalent term forming operations.}:
    \begin{itemize}
    \item For every to polynomials $\varphi: 1 \to A, \psi: 1\to B$ we have the term $\langle \varphi, \psi \rangle: 1 \to A\times B$ defined by the universal property of the product. Projections $\pi_i$ in $\lambda$-calculus are found by composition with the projection in the context of the category.
    \item Given $\varphi(x^a):1 \to B$ we want to find $\lambda x^a.\varphi(x^a): 1 \to B^A$.  For that we first apply proposition \ref{deduction-theorem} to found $k_{x^A}\varphi(x^A): 1 \land A \to B$. After that, using the adjoint properties of the closed Cartesian category, we can find:
      $$\lambda x^A.\varphi(x^A) := \left( k_{x^A}\varphi(x^A)\right)^*: 1 \to B^A.$$
      % , i.e., is the unique arrow  such that:
      % \[
      %   \begin{tikzcd}
      %     {} & C \arrow[bend right,swap,dashed]{dl}{g}
      %     \arrow[bend left,dashed]{dr}{h} \arrow{d}[description]{f}& & \\
      %     A  &(A,B) \arrow{l}[swap]{fst} \arrow{r}{snd} &
      %     B \\
      %   \end{tikzcd}
      % \]
    \item Application is defined by $\epsilon$, the counit of the adjoint $F_3\dashv G_3$.
    \end{itemize}
  \end{itemize}

\end{definition}

\begin{remark}
  In any simply typed $\lambda$-calculus, we consider raw terms and then we consider only those that are subject to be typable. This was later understood as that any term being a proof of veracity of a type, via the Curry-Howard isomorphism. So, to maintain this idea of the arrows of the language being proof of veracity, we see them as arrows $f:\top \to A$, that is, as deductions from truth. \\
\end{remark}

Now, we have to define how the functor behave over morphisms.

\begin{definition}
  Let $F:C\to C'\in CCart_N$. We define $\LL (F)$ as theas the morphism of $\lambda$-calculi given by:
  \begin{itemize}
  \item $\LL (F) (a)= F(a)$ for every term $a$, that is, for every $a\in Ob(C)$.
  \item $\LL (x^a) = x^{F(a)}$ for every variables $x^a \in\LL(C)$, that is, for every assumption $x: 1 \to A$ in $C$.
  \item Let $X= \{x_1^{t_1},...,x_n^{t_n}\}$ be a set of indeterminates. Then for ever polynomial $\varphi$ over $X$ we have that
    $$\LL(F)(\varphi(X):1 \to A) = F_{X\to \LL(X)}(\varphi(X):1 \to A)$$  where $F_{X\to \LL(X)}$ is defined as in proposition \ref{unique-polynomial-functor}.
  \end{itemize}
\end{definition}

\begin{remark}
  Preservation of the structure of a $\lambda$-calculus is derived directely from the preservation of the closed cartesian category structure of $F$.
\end{remark}

\begin{proposition}\label{natural-numbers}
  If $C$ has weak natural numbers, $\LL (C)$ has natural numbers.
\end{proposition}
\begin{proof}
  Let $(N,S)$ be a weak natural number in $C$. We remember that in $\LL(C)$ a term of type $A$ corresponds to a polynomial $\varphi(x): 1 \to A$.We define a natural number system in $L(C)$ as follows:
  \begin{itemize}
  \item we identify the $\iota$ type with the object $N$.
  \item we identify the $\mathfrak{o}$ term with the arrow $0: 1 \to N$, seen as a polynomial.
  \item given a term $\varphi$ of type $N$, we define $S(N)$ as $S\circ\varphi$. Every term of type $N$ is of the form $S^n\circ \mathfrak o$
  \item given a term $a$ of type $A$, a term $f$ of type $A\to A$, and a term $n$  of type $N$, remembering the identification in the category $C$ we have that $h$ exists,
    \[
      \begin{tikzcd}
        1\arrow{dr}{a}\arrow{r}{0} & N\arrow{r}{S}\arrow[dashed]{d}{h} & N\arrow[dashed]{d}{h}\\
        & A\arrow{r}{f} & A
      \end{tikzcd}
    \]
  \end{itemize}

  Lastly, we idenfity $I_A(a,f,n)$ 1ith $h\circ n: 1 \to A$.

\end{proof}

\begin{proposition}
  If $F: C\to C \in CCart_N$ is an endofunctor of $CCart_N$, then $\LL (F)$ maintains natural numbers.
\end{proposition}

\begin{proof}

  The results directly follows from $F$ maintaining weak natural numbers.
\end{proof}

\subsection{The Cartesian Closed Category generated by a typed $\lambda$-calculus}

In this section we are going to do the converse process of the previous one, i.e., given a typed $\lambda$-calculus, we are going to construct a related closed Cartesian category.

\begin{definition}
  Given a typed $\lambda$-calculus $L$ we can construct the \emph{generated closed Cartesian category}, denoted by $\CC (L)$ as follow:
  \begin{itemize}
  \item As objects it has the types in $L$.
  \item As arrows $f:a\to b$ it has the terms $\lambda x^a. b(x)$ where $b(x)$ is a term that only has $x^a$ as a free variable. Arrows are considered as equivalence classes up to $\alpha\beta\eta$-equivalence.
  \end{itemize}
\end{definition}

The following proposition is discussed in plenty of sources. We choose to adapt some ideas done by \cite{seely1984locally} to dependent typing, instead of following directly the main source \cite{lambek1985cartesian} as we consider that it has a more natural approach.
\begin{proposition}
  $\CC (L)$ is a closed Cartesian  category.
\end{proposition}
\begin{proof}
  We start checking the category axioms:
  \begin{itemize}
  \item Identity is given by $\lambda x^a.x$.
  \item Composition of $\lambda x^a. f(x):  a\to b$ and $\lambda x^b. g(x^b): b \to c$ is given by $\lambda x^a. g(f(x)): a \to c$.
  \item Associativity follows from $\beta$-equivalence.\\
  \end{itemize}
  To provide the terminal object:
  \begin{itemize}
  \item The type $1$ is the terminal object. By $\eta_\times$ equivalence\footnote{as in definition \ref{def:eta_x}.}, the only term of type $1$ is $*$ up to equivalence. Therefore, any arrow $t:a \to 1$ must be equivalent to $\lambda x^a. *$.\\
  \end{itemize}
  To provide the product type for two types $a,b$.
  \begin{itemize}
  \item The type product is provided by $a \times b$.
  \item The projections are provided by $\beta_{x,i}$ with the projections $\pi_1,\pi_2$.
  \item Given three types $a,b,c$ and terms $\lambda x^a. f(x) : a\to b$, $\lambda x^a. g(x) : a\to c$ a term $\lambda x^{a\times b}.\langle f(\pi_1(x)),g(\pi_2(x))\rangle $.
  \item Properties 2. and 3. in proposition \ref{prop:CartCategory2} comes directly from $\eta_x$ and $\beta_{x,i}$ equivalence rules.\\
  \end{itemize}
  To provide the exponential type (and thus have a conjunction calculus):
  \begin{itemize}
  \item The exponential object $a^b$ comes from the type $b\to a$.
  \item Given $h = \lambda x^{a\times b}. f(x): a\times b \to c$  we define $h^* = \lambda x^a. \lambda y^b. f(\langle x,y\rangle)$.
  \item There is an arrow $\varepsilon^{a,b} = \lambda y^{a\to b \times a}. (\pi_1(y))(\pi_2(y))$.
  \item Properties in proposition \ref{def2:CCC} comes from extending the expresion by $\beta$-equivalence and then simplifying with $\eta$-equivalence.
  \end{itemize}
\end{proof}

\begin{proposition}
  If $L$ has a natural numbers system, then $\CC (L)$ has a natural number system.
\end{proposition}
\begin{proof}
  Repeat the process of proposition \ref{natural-numbers} in opposite direction.
\end{proof}
Now we repeat the process as with $\LL$ and define a functor $\CC: \LC \to CCart$.
\begin{definition}
  Let $L\in Ob(\LC)$, let $\CC (L)$ be the generated closed Cartesian category of $L$ and let $F:L\to L'\in \LC$.  We define the functor $\CC (F)$:
  \begin{itemize}
  \item $\CC (F) (a) = F(a)$ for every $a\in Ob(C)$.
  \item $\CC (F) (\lambda x^a. b(x) :a \to a') = \CC (L') (\lambda x^{F(a)}. F(b(x)))$.
  \end{itemize}
\end{definition}
\begin{remark}
  Preservation of structures of closed cartesian structures derives directly from preservation of structures of $\lambda$-calculus of $F$.
\end{remark}

We finally explain how to add an indeterminate to $\CC(L)$, and how to characterize this construction.

\begin{definition}
  Let $L$ be a simply typed $\lambda$-calculus and let $x : a$ be a variable in $L$. We can generate the language $L(x)$ by adding the \emph{parameter} $x$ that has the same types as $L$, and the same terms except that $x$ is no longer a variable. That is, now closed terms are terms that does not have any free variable other than $x$.
\end{definition}

\begin{proposition}
  $\CC(L)[x] \cong \CC(L(x)).$
\end{proposition}
\begin{proof}
  We can define an equivalence between categories. Let us show that $\CC(L(x))$ has the same property as $\CC(L)[x]$, and thus there only exists two functors $F:\CC(L(x))\to \CC(L)[x]$  and $G:\CC(L)[x] \to\CC(L(x))$ that maps $x\to x$ and thus they are inverses of each other.
  \[
  \begin{tikzcd}
    \CC(L)\arrow[hookrightarrow]{r}{H} \arrow[rd, "F", swap]      & \CC(L(x)) \arrow[d, "F'", dashed]& x:a_0\to a\arrow[d,"f'",dashed]\\
    &B& z:F(a_0)\to F(a)
  \end{tikzcd}
\]
We define $F'(a) = F(a)$ for every $b\in Ob(\CC(L))=Ob(\CC(L(x)))$. Then, for every morphism $f \in \CC(L(x))$ has the form $f = \lambda y^b. \varphi(y^b, x)$ for some variable $y$ of type $b$ and some term with $y,x$ as free variables. It is easy to check that, $$\varphi(y, x) = [\lambda x. \varphi(y,x ) ] x =^{1.} \psi(y)x,$$ under $\beta$-equivalence, where in 1. we define $\psi (y)= \lambda x. \varphi(y,x)$. Finally, using that $\lambda y^b. \psi(y^b) \in Ar(\CC(L))$, we define $F'(f):= F(\lambda y^b. \psi(y^b)) z$.
\end{proof}
\subsection{Equivalence}
To finish this chapter, we prove that these two functors makes an adjoint equivalence. We adapted the proof from \cite[Theorem 11.3]{lambek1988introduction}, using a more clear categorical language.

\begin{theorem}[Lambek Theorem]The categories $\LC$ and $CCart$ are equivalent. \end{theorem}
\begin{proof}
  We will prove that $\CC \LL \cong 1_{CCart_N}$ and  $ \LL\CC \cong 1_{\Lambda}$, to prove that $\CC$ and $\LL$ form an adjoint equivalence.

  \begin{enumerate}
  \item[\fbox{$\CC \LL \cong 1_{\mathcal A}$}] To prove this, we have to define the counit $\varepsilon$. Given a closed Cartesian category $\mathcal A$ we define a functor  $\varepsilon(\mathcal A)$ such that
    \begin{itemize}
    \item $\varepsilon(\mathcal A)    (a) = a$ for every $a\in Ob(\mathcal A)$.
    \item An arrow in $f:a\to b \in \CC \LL(\mathcal A)$ comes from a term of the form $\lambda x^a. b(x^a)$ where $b(x^a)$ is a term in $\LL (\mathcal A)$ with the only free variable being $x^a$. Thus, f comes from a term $f$ has the form $\lambda x^a. \varphi(x^a)$ for some $\varphi(x^a): 1 \to b \in \mathcal A [x^a]$. Then, we define $\varepsilon(\mathcal A)(f) = \varepsilon(\mathcal A)(\lambda x^a.\varphi(x^a))$ as the unique arrow $g$ in $\mathcal{A}$ such that $gx^a = \varphi(x^a) \in \mathcal A[x^a]$ by functional completeness.
    \end{itemize}
    It is easy to check that this is a closed Cartesian functor.  By the uniqueness of $g$ we infers that $$\hhom_{\CC\LL(\mathcal A)} (b,c)\cong \hhom_{\mathcal A} (b,c).$$
    We still have to check that $\varepsilon (\mathcal A)$ is a natural transformation, that is, or every to categories $B,C$ and  for every functor $F: C\to B\in CCart$ we have that
    \[
      \begin{tikzpicture}
        \node {\begin{tikzcd}[column sep=20mm]
            \CC\LL(B)\ar[r,"\varepsilon(B)"]\ar[d,"\CC\LL(F)"] & B\ar[d,"F"]\\
            \CC\LL(C)\ar[r,"\varepsilon(C)"] & C.
          \end{tikzcd}};
      \end{tikzpicture}
    \]

    Therefore, we have to check that $\varepsilon(C)\circ \CC\LL(F) = F \circ \varepsilon(B)$. This is trivially checked on objects. On arrows, we have to check that, for every $f = \lambda x^a.\varphi(x^a) \in \CC\LL(F) $ we have that $\varepsilon(C)\circ \CC\LL(F) (f)= F \circ \varepsilon(B)(f)$.

    \begin{itemize}
    \item  $F \circ \varepsilon(B)(f) = F(g)$ where $g$ is the only function  such that$$gx^a = \varphi(x^a) \in \mathcal B[x^a].$$
    \item $\CC\LL(F) = \lambda x^{F(a)}.F_{x^a\to F(x^a)} \left ( \varphi(x^a)\right )$. Then we have that $\varepsilon(C)\circ \CC\LL(F) = h$ where $h$ is the only function such that $$h\circ F(x^a) = F_{x^a\to (x^{F(a)})}\left ( \varphi(x^a)\right ).$$
    \end{itemize}
    We finish by considering that $F(g)\circ F(x^a) = F_{x^a\to (x^{F(a)})}(g \circ x^a) = F_{x^a\to (x^{F(a)})}(\varphi(x^a)) $, and therefore $\varepsilon$ is natural.\\

  \item[\fbox{$ 1_{\mathcal A}\cong \LL \CC $}] We start by studying the language $\LL\CC(L)$, given given a $\lambda$-calculus $L$. Is easy to check that the types in both calculi are the same.\\

    Remember that arrows $f:a\to b$ in $\CC(L)$ are closed terms $\lambda x^a.f(x)$ of type $a\to b$. On the other hand, every term $M$ of type $A$ in $\LL\CC(L)$ has the form $\varphi(x_1,...,x_n): 1 \to A$, where $\varphi(x^{t_1},...,x^{t_n})$ is an arrow in $\CC(L)$ dependent on some indeterminates. Thus $M =\lambda z^1.m(x_1,...,x_n)$\footnote{Here we are identifying $\CC(L)[x_1,...,x_n]$ with $\CC(L(x_1,...,x_n))$}, where $z^1$ is bounded variable of type of type 1.\\

    We define the unit $\eta$ of the adjoint equivalence similarly:
    \begin{itemize}
    \item For every $L\in \LC$ and every type $t$ of $L$ we have that $\eta(L)(t) = t$.
    \item For every term $\lambda z^1.\varphi (x_1^{t_1},...,x_n^{t_n})$, were every $x_i^{t_i}$ represents a free variable, to $\lambda z^1. \varphi (x_1^{t_1},...,x_n^{t_n}).$
    \end{itemize}

    It is easy to check that $\eta(L)$ is a $\lambda$-morphism. To check that it is an isomorphism on-home sets we can construct the inverse arrow $\nu(L)$ given by:

    \begin{itemize}
    \item  $\nu(L)(t) = t$ for every type $t \in \LL\CC(L).$
    \item  Let $\lambda z^1.\varphi(x_1,...,x_n)$ be a term in $\LL\CC(L)$ with $x_1,...,x_n$ as free variables. We define $\nu(L)(f)= f^* = \varphi(x_1,...,x_n)$.
    \end{itemize}

    Naturality is checked as in the first part of the proof,expanding each equality term.
  \end{enumerate}
\end{proof}

% \subsubsection{Deduction Theorem}
% We proceed with the tating that an arrow $f:\top \to A$ does exists in our deduction system. It can be deduced in both positive (without $\lor$ and $\bot$) an intuitionistic calculus that is $A \land B\vdash C$ then $A \vdash C\to B$. This theorem is more interesting to state in the new optics of deduction systems:
% \begin{theorem}[Proposition 2.1, \cite{lambek1988introduction}]
%   In a positive  calculus, if assuming the existence of an arrow $f:\top \to A$ implies the existence of an arrow $g: B\to C$, then there exists an arrow $h: A\land B\to C$ that does not depend on $f$.
% \end{theorem}

% \begin{sproof}
%   In a similar fashion of the Church-Rosser Theorem, it is solved by induction in the last rule used in the deduction.
% \end{sproof}

% As we have seen, a category is a deductive system with added structure.