\documentclass{beamer}
\usepackage[francais]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsrefs}
\usepackage{graphicx}

\mode<presenation>
{ \usetheme{boxes} }

%\AtBeginSection[]{
%  \begin{frame}<beamer>
%  \frametitle{R\'esum\'e}
%  \tableofcontents[currentsection]
%  \end{frame}
%}

\title{Knot thickness computation in parallel}
\author{Mathias Carlen}
\date{11 june 2009}

\begin{document}

\frame{\titlepage}
\frame{\frametitle{Content}\tableofcontents}

%skeleton
%\section{Test}
%\frame{
%  \frametitle{1. titre}
%  \begin{itemize}
%  \item<2-> Bla 1
%  \item<3-> Bla bla 2
%  \end{itemize}
%}

\section{Curves and thickness}

\begin{frame}
\frametitle{Curves and thickness}

\begin{itemize}
 \item<1-> closed curve $\gamma(s)$, with $s$ arclength param.
 \item<2-> knot class
 \item<3-> maximal thick tube $\longrightarrow$ thickness $\Delta$.
\end{itemize}
\vspace{1cm}
\pause
\pause
\pause
\includegraphics[height=2cm]{global.pdf}\hspace{.2cm}
\pause
\includegraphics[height=2cm]{local.pdf}
\pause
\[
\Delta = min ( min_s r(s) , min_{dc} \frac{\gamma(s)-\gamma(t)}{2} )
\]
\end{frame}

\section{Motivation}
\begin{frame}
\frametitle{Motivation}
\begin{itemize}
\item<1-> Why parallelize?
\item<2-> Minimize $L[\gamma]/\Delta[\gamma]$
\item<3-> Monte carlo
\item<4-> Animation
\end{itemize}
\end{frame}

\section{Thickness Algorithm}

\begin{frame}
\frametitle{Thickness Algorithm}
\begin{itemize}
\item[(1)]<1-> Discretize $\gamma$ with biarcs
\item[(2)]<2-> Compute smallest local radius $r_i$
\item[(3)]<3-> Prepare a set of candidate arc pairs $(a_i,b_i)$
\item[(4)]<4-> Compute minimal distance between arcs
\item[(5)]<5-> Double criticality test
\item[(6)]<6-> Distance test
\item[(7)]<7-> Bisect remaining arc pairs ($1$ pair -> $4$ pairs)
\item[(8)]<8-> If not below some tolerance goto 4
\item[(9)]<9-> Return thickness
\end{itemize}
\end{frame}

\section{Parallelization}
%\subsection{Master/Slave work}
%\subsection{MPI}
%\subsection{Implementation details}
%\subsubsection{Custom datatypes}
%\subsubsection{Batching}

\begin{frame}
\frametitle{Parallelization (1)}
\includegraphics[height=.8\textheight]{parallel_algo_flowchart.pdf}
\end{frame}

\begin{frame}
\frametitle{Parallelization (2)}
\begin{itemize}
\item<1-> MPI
\item<2-> custom datatype \texttt{struct Candidate}
\item<3-> batching candidates
\end{itemize}
\end{frame}

\section{Experiments and benchmarks}
%\subsection{Hardware/Software used}
%\subsection{CPUs}
%\subsection{Knot data points}
%\subsection{Batching}
%\subsection{Knot shapes}
%\subsection{Master/slaves timing}

\begin{frame}
\frametitle{Experiments and benchmarks (1)}
Computed on cluster \texttt{lcvmlc2} using \texttt{mpich-mx}. \\
\vspace{.5cm}
\pause
Time vs. number of CPUs \\
\includegraphics[width=.75\textwidth]{time_vs_cpus.pdf}
\end{frame}

\begin{frame}
\frametitle{Experiments and benchmarks (2)}
Time vs. discretization \\
\includegraphics[width=.75\textwidth]{time_vs_nodes.pdf}
\end{frame}

\begin{frame}
\frametitle{Experiments and benchmarks (3)}
Time vs. batching \\
\includegraphics[width=.75\textwidth]{time_vs_batch.pdf}
\end{frame}

\begin{frame}
\frametitle{Experiments and benchmarks (4)}
Time vs. knot type \\
\includegraphics[width=.75\textwidth]{time_vs_knots.pdf}
\end{frame}

\begin{frame}
\frametitle{Experiments and benchmarks (5)}
Master/Slave timing \\[1cm]
\begin{tabular}{lclll}
knot & data points & master (sec) & slaves (sec) & total (sec) \\
j3.1 & 512 & 0.165336 & 2.092650 & 2.258000 \\
k3.1 & 160 & 0.016368 & 0.016206 & 0.032578 \\
k4.1 & 208 & 0.024819 & 0.018046 & 0.042868 \\
k5.1 & 232 & 0.031302 & 0.000165 & 0.031471 \\
k6.1 & 280 & 0.042829 & 0.000162 & 0.042995 \\
\end{tabular}
\end{frame}

\section{Possible improvements}
%\subsection{Master idle or not}
%\subsection{Master computes, interrupt system?}
%\subsection{Best value broadcast}
%\subsection{Optimize master initialization step}

\begin{frame}
\frametitle{Possible improvements}
\begin{itemize}
\item<1-> Master idle/working
\item<2-> Master computing (interrupt system)
\item<3-> Broadcast best current value
\item<4-> Optimize master init step
\end{itemize}
\end{frame}

\end{document}


\section{Gaussian Distributions}

The behaviour of all quantitative traits of the dataset is assumed to adhere to a
Gaussian Distribution.
This assumption is taken due to how the values of the data set are presented.
As shown in Figures
% \ref{fig: sepal length normal distribution},
% \ref{fig: sepal width normal distribution},
\ref{fig: petal length normal distribution} and
\ref{fig: petal width normal distribution}
the distribution for each flower variety follows the
general behaviour of the Gaussian Distribution.

Having confirmed that all quantitative features of the dataset
adhere to Gaussian Distributions, it is possible to proceed with the
development of the classifiers.

% \begin{figure}[htb!]
%  \includegraphics[width=\textwidth]{normSepalLength}
%  \caption{Sepal Length}
%  \label{fig: sepal length normal distribution}
% \end{figure}
%
% \begin{figure}[htb!]
%  \includegraphics[width=\textwidth]{normSepalWidth}
%  \caption{Sepal Width}
%  \label{fig: sepal width normal distribution}
% \end{figure}

\begin{figure}[htb!]
 \includegraphics[width=\textwidth]{normPetalLength}
 \caption{Petal Length}
 \label{fig: petal length normal distribution}
\end{figure}

\begin{figure}[htb!]
 \includegraphics[width=\textwidth]{normPetalWidth}
 \caption{Petal Width}
 \label{fig: petal width normal distribution}
\end{figure}

\pagebreak
\newpage


\documentclass{article}

\usepackage{amsmath,amssymb,amsthm}
\usepackage{url}
\usepackage{comment}
\usepackage{proof}
\usepackage{stmaryrd}
\usepackage{parskip}
\usepackage{fullpage}

\newcommand{\choice}[0]{\zeta}
\newcommand{\elcap}[0]{\cap}
\newcommand{\abs}[4]{{#1}\, #2\! : \! #3.\, #4}
\newcommand{\absu}[3]{{#1}\, #2.\, #3}
\mathchardef\mhyph="2D % Define a "math hyphen"
\newcommand{\interp}[1]{\llbracket #1 \rrbracket}
\newcommand{\tpcheck}[0]{\Leftarrow}
\newcommand{\tpsynth}[0]{\Rightarrow}
\newcommand{\cbe}[0]{c\beta\eta}
\newcommand{\utp}[0]{\mathcal{U}}
\newcommand{\startcase}[1]{\vspace{#1} \noindent\textbf{\underline{Case:}}}

\newtheorem{theorem}{Theorem}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}

\begin{document}

\title{Syntax and Semantics of Cedille}

\author{Aaron Stump \\
  Computer Science \\
  The University of Iowa \\
\texttt{aaron-stump@uiowa.edu}}

\date{}

\maketitle

\section{Introduction}

The type theory of Cedille is called the Calculus of Dependent Lambda
Eliminations (CDLE).  This document presents the version of CDLE as of
June 1, 2018.  We have made many changes from the first paper on CDLE~\cite{stump17},
mostly in the form of dropping constructs we discovered (to our
surprise) could be derived~\cite{stump18}. I have also omitted
\emph{lifting} -- a technique for large eliminations with lambda
encodings -- in this document's version of CDLE.  Some uses of lifting
can be simulated other ways within the system, though the limits of
this are still under investigation.  We also include a construct
$\delta$, for deriving a contradiction from a proof that
lambda-encoded true equals lambda-encoded false.  This also
compensates somewhat for the lack of lifting.

At a high level, CDLE is an extrinsic (i.e., Curry-style) type theory
extending the Calculus of Constructions with three additional
constructs, which allow deriving induction principles within the
theory, for inductive datatypes.  The goal is to support usual
idioms of dependently typed programming and proving as in Agda or
similar tools, but using pure lambda encodings for all data, and
requiring a much smaller core theory.

The current Cedille implementation of CDLE extends the system
described below with a number of features intended to make programming
in the system more convenient and with less redundancy.  These
features all compile away to a slightly simplified version of the
theory presented in this document, called Cedille Core, described
here: \url{https://github.com/astump/cedille-core-spec}.

\section{Classification Rules}

The classification rules are given in
Figures~\ref{fig:superknd},~\ref{fig:knd}, and~\ref{fig:tp}.  For
brevity, we take these figures as implicitly specifying the syntax of
kinds $\kappa$, types $T$, and annotated terms $t$; these may use term
variables $x$ and type variables $X$, which we assume come from
distinct sets.  So terms and types are syntactically distinguished.
The typing rules (Figure~\ref{fig:tp}) are
bidirectional~\cite{pierce+00}, while the kinding and superkinding
rules (Figure~\ref{fig:knd} and~\ref{fig:superknd}) are only
synthesizing.  We write $\Leftrightarrow$ to range over
$\{\tpcheck,\tpsynth\}$. We follow the syntax of our implementation
Cedille, which distinguishes application of a term or type $e$ to a
type ($e \cdot T$), from application to a term ($e\ t$), and
application to an erased term argument ($e\ \mhyph t$).  The rules are
intended, with a few points of nondeterminism, to be read bottom-up
(in a standard way; cf.~\cite{peytonjones07}) as an algorithm for
computing a classifier from a context and an expression ($\tpsynth$)
or checking an expression against a classifier in context
($\tpcheck$).

The classification rules refer to an erasure function, defined in
Figure~\ref{fig:eraser}.  The type theory is \emph{extrinsic} (aka,
Curry-style), and hence we only consider erasures $|t|$ of terms when
testing for $\beta\eta$-equivalence.  This is done by the conversion
relation $T\cong T'$, whose central rules are given in
Figure~\ref{fig:conv}.  That figure omits the various congruence rules
needed to equate bigger expressions by equating subexpressions.  The
main ideas of conversion shown in the figure are to use
$\beta$-equivalence at the type level, and $\beta\eta$-equivalence of
erased terms at the term level.

\begin{figure}
  \[
  \begin{array}{llll}
    \infer{\Gamma \vdash \star}{\ } &
    \infer{\Gamma\vdash\abs{\Pi}{x}{T}{\kappa}}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash\kappa} &
    \infer{\Gamma\vdash\abs{\Pi}{X}{\kappa'}{\kappa}}{\Gamma \vdash \kappa' & \Gamma,X:\kappa'\vdash\kappa}
  \end{array}
  \]
  \caption{Rules for checking that a kind is well-formed ($\Gamma \vdash \kappa$)}
  \label{fig:superknd}
\end{figure}

\begin{figure}
  \[
  \begin{array}{ll}
    \infer{\Gamma \vdash X \tpsynth \kappa}{(X : \kappa) \in \Gamma} &
    \infer{\Gamma\vdash \abs{\forall}{X}{\kappa}{T} \tpsynth \star}{\Gamma \vdash \kappa & \Gamma,X:\kappa\vdash T \tpsynth \star} \\ \\
    \infer{\Gamma\vdash\abs{\forall}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star} &
    \infer{\Gamma\vdash\abs{\Pi}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star} \\ \\
    \infer{\Gamma\vdash\abs{\lambda}{x}{T}{T'} \tpsynth \abs{\Pi}{x}{T}{\kappa}}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T'\tpsynth\kappa} &
    \infer{\Gamma\vdash\abs{\lambda}{X}{\kappa}{T'} \tpsynth \abs{\Pi}{X}{\kappa}{\kappa'}}{\Gamma \vdash \kappa & \Gamma,X:\kappa\vdash T'\tpsynth\kappa'} \\ \\
    \infer{\Gamma\vdash T\ t \tpsynth [t/x]\kappa}{\Gamma\vdash T \tpsynth \abs{\Pi}{x}{T'}{\kappa} & \Gamma\vdash t \tpcheck T'} &
    \infer{\Gamma\vdash T\cdot T' \tpsynth [T'/X]\kappa}{\Gamma\vdash T \tpsynth \abs{\Pi}{X}{\kappa'}{\kappa} & \Gamma\vdash T' \tpsynth \kappa' & \kappa \cong \kappa'} \\ \\
    \infer{\Gamma\vdash\abs{\iota}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star} &
    \infer{\Gamma\vdash \{ t \simeq t' \} : \star}{\textit{FV}(t\ t')\subseteq\textit{dom}(\Gamma)}
  \end{array}
  \]
  \caption{Rules for synthesizing a kind for a type ($\Gamma \vdash T \tpsynth \kappa$)}
  \label{fig:knd}
\end{figure}

\begin{figure}
  \[
  \begin{array}{ll}
    \infer{\Gamma\vdash x\tpsynth T}{(x : t)\in\Gamma} &
    \infer{\Gamma\vdash t\tpcheck T'}{\Gamma\vdash t \tpcheck T & T' \leadsto^*_\beta T} \\ \\
    \infer{\Gamma\vdash t\tpsynth T'}{\Gamma\vdash t \tpsynth T & T \leadsto^*_\beta T'} &
    \infer{\Gamma\vdash t\tpcheck T}{\Gamma\vdash t\tpsynth T' & T' \cong T} \\ \\
    \infer{\Gamma\vdash \absu{\lambda}{x}{t} \tpcheck \abs{\Pi}{x}{T}{T'}}{\Gamma,x:T\vdash t\tpcheck T'} &
    \infer{\Gamma\vdash t\ t' \tpsynth [t'/x]T}{\Gamma\vdash t \tpsynth \abs{\Pi}{x}{T'}{T} & \Gamma\vdash t' \tpcheck T'} \\ \\

    \infer{\Gamma\vdash \absu{\Lambda}{X}{t} \tpcheck \abs{\forall}{X}{\kappa}{T}}{\Gamma,X:\kappa\vdash t \tpcheck T} &
    \infer{\Gamma\vdash t \cdot T' \tpsynth [T'/X]T}
          {\Gamma\vdash t \tpsynth \abs{\forall}{X}{\kappa}{T} & \Gamma\vdash T' \tpcheck\kappa} \\ \\

    \infer{\Gamma\vdash \absu{\Lambda}{x}{t} \tpcheck \abs{\forall}{x}{T'}{T}}{\Gamma,x:T'\vdash t \tpcheck T & x\not\in\textit{FV}(|t|)} &
    \infer{\Gamma\vdash t\ \mhyph t' \tpsynth [t'/x]T}{\Gamma\vdash t \tpsynth \abs{\forall}{x}{T'}{T} & \Gamma\vdash t' \tpcheck T'} \\ \\

    \infer{\Gamma\vdash [ t , t' ] \tpcheck \abs{\iota}{x}{T}{T'}}
          {\Gamma\vdash t \tpcheck T & \Gamma\vdash t' \tpcheck [t/x]T' & |t| =_{\beta\eta} |t'|} &
    \infer{\Gamma\vdash t.1 \tpsynth T}{\Gamma\vdash t \tpsynth \abs{\iota}{x}{T}{T'}} \\ \\
    \infer{\Gamma\vdash t.2 \tpsynth [t.1/x]T'}{\Gamma\vdash t \tpsynth \abs{\iota}{x}{T}{T'}} &

    \infer{\Gamma\vdash \beta\{t'\} \tpcheck \{ t \simeq t \}}{\Gamma\vdash \textit{FV}(t)\subseteq \textit{dom}(\Gamma)}  \\ \\
    \infer{\Gamma\vdash \delta\ t \tpcheck T}{\Gamma\vdash t\tpcheck \{ \absu{\lambda}{x}{\absu{\lambda}{y}{x}} \simeq \absu{\lambda}{x}{\absu{\lambda}{y}{y}}\}}  &
    \infer{\Gamma\vdash \rho\ t'\ \mhyph\ t \Leftrightarrow [t_2/x]T}
          {\Gamma\vdash t' \tpsynth t_1 \simeq t_2 & \Gamma \vdash t \Leftrightarrow [t_1/x]T} \\ \\
    \infer{\Gamma\vdash \chi\ T\ \mhyph\ t \tpcheck T'}
          {\Gamma\vdash T\tpcheck \star & \Gamma\vdash t \tpcheck T & T \cong T'} &
    \infer{\Gamma\vdash \chi\ T\ \mhyph\ t \tpsynth T}
          {\Gamma\vdash T\tpcheck \star & \Gamma\vdash t \tpsynth T' & T \cong T'} \\ \\
    \infer{\Gamma\vdash \phi\ t\ \mhyph\ t'\{t''\} \Leftrightarrow T}
          {\Gamma\vdash t\tpsynth \{t'\simeq t''\} & \Gamma\vdash t' \Leftrightarrow T}  & \
  \end{array}
  \]
\caption{Rules for checking a term against a well-kinded type ($\Gamma \vdash t \tpcheck T$)
           and synthesizing a type for a term ($\Gamma \vdash t \tpsynth T$)}
\label{fig:tp}
\end{figure}

\begin{figure}
  \[
  \begin{array}{lllllll}
    |x| & = & x &\ &
    |\abs{\lambda}{x}{t'}{t}| & = & \absu{\lambda}{x}{|t|} \\
    |t\ t'| & = & |t|\ |t'| &\ &
    |t\cdot T| & = & |t| \\
    |\absu{\Lambda}{x}{t'}{t}| & = & |t| &\ &
    |t\ \mhyph t'| & = & |t| \\
    |[t , t']| & = & |t| &\ &
    |t.1| & = & |t| \\
    |t.2| & = & |t| &\ &
    |\beta\{t\}| & = & |t|\\
    |\delta\ t| & = & |t|&\ &
    |\rho\ t\ \mhyph\ t'| & = & |t'| \\
    |\phi\ t\ \mhyph\ t'\ \{t''\}| & = & |t''| &\ &
    |\chi\ T\ \mhyph\ t'| & = & |t'|
  \end{array}
  \]
  \caption{Erasure for annotated terms}
  \label{fig:eraser}
\end{figure}

\begin{figure}
  \[
  \begin{array}{ll}
    \infer{T \cong T'}{T \leadsto^*_\beta T_1 & T' \leadsto^*_\beta T_2 & T_1\cong^t T_2}  &
    \infer{T \cong T'}{T \cong^t T'} \\ \\
    \infer{T\ t \cong^t T\ t'}{T \cong^t T' & |t| =_{\beta\eta} |t'|} &
    \infer{\{ t_1 \simeq t_2 \} \cong^t \{ t_1'\ \simeq t_2' \}}{|t_1| =_{\beta\eta} |t_1'| & |t_2| =_{\beta\eta} |t_2'|}
  \end{array}
  \]
  \caption{Non-congruence rules for conversion}
  \label{fig:conv}
\end{figure}

\subsection{Overview of the constructs}
\label{sec:overview}

CDLE has as a subsystem the extrinsic Calculus of
Constructions (CC).  We have dependent types
$\abs{\Pi}{x}{T}{T'}$ and kinds $\abs{\Pi}{x}{T}{\kappa}$, as well as
term- and type-level quantification over (possibly higher-kinded)
types $\abs{\forall}{X}{\kappa}{T}$ and
$\abs{\Pi}{X}{\kappa}{\kappa'}$.  We use $\forall$ when the
corresponding argument will be erased, and $\Pi$ when it will be
retained.  Since we do not erase term or type arguments from
type-level applications, we thus write $\abs{\Pi}{X}{\kappa}{\kappa'}$
instead of $\abs{\forall}{X}{\kappa}{\kappa'}$.  We write $\lambda$ to
correspond to $\Pi$ and $\Lambda$ to correspond to $\forall$.  As noted
above, application to a type is denoted with center dot ($\cdot$).

To Curry-style CC, CDLE adds: implicit products, introduced orginially
by Miquel~\cite{miquel01}; a primitive equality type $\{ t \simeq
t'\}$; and dependent intersection types $\abs{\iota}{x}{T}{T'}$,
introduced by Kopylov~\cite{kopylov03}.  Implicit products are used
for erased arguments to functions, found also in systems like Agda
(cf.~\cite{mishraLinger08}).  Dependent intersections are a rather
exotic construct allowing us to assign type $\abs{\iota}{x}{T'}{T}$ to
erased term $t$ when we can assign $T'$ to $t$, and also assign
$[t/x]T$ to $t$.  For an annotated introduction form, we write
$[t,t'$], where $t$ checks against type $T'$, $t'$ checks against
$[t/x]T$, and $t$ and $t'$ have identical (i.e., $\alpha$-equivalent)
erasures.  Dependent intersections thus enable a controlled form of
self-reference in the type.  Previous work showed how to use this to
derive induction for Church-encoded natural numbers~\cite{stump18}.
We will see below further uses of this construct.

The typing rules include conversion checks in a few places; e.g., as
standardly, when switching from checking to synthesizing mode.  Two
rules near the top of Figure~\ref{fig:tp} state that one may (nondeterministically)
$\beta$-reduce the type one is synthesizing or checking, before proceeding.
This allows reduction to head-normal form,
to match the form of type required by other rules.
Finally, we include the construct $\chi\ T\ \mhyph\ t$ to change the
synthesized or checked type $T'$ to $T$, if $T \cong T'$.  This may
be necessary to get the type into a specific form for purposes of rewriting
with the $\rho$ construct.

Finally, we have modified
the rules for equality types $\{ t \simeq t' \}$ so that we require
nothing of $t$ and $t'$ except that the set $\textit{dom}(\Gamma)$ of variables
declared by $\Gamma$ includes their free variables $\textit{FV}(t\ t')$.  Further modifications
over the version of CDLE in~\cite{stump18} are:
\begin{itemize}
\item To prove $\{ t \simeq t\}$, one now writes $\beta\{t'\}$,
  with the critical idea that $|\beta\{t\}|$ erases to $|t|$.  We call this the \textbf{Kleene trick},
  because it goes back to Kleene's numeric realizability, which accepts any number $n$ as a realizer
  of a true equation.  Here, we accept any closed term $t$ as a realizer
  of $\{ t \simeq t\}$.  This means that in Cedille, any such term -- even otherwise untypable
  terms, non-normalizing terms, etc. -- have type $\{ t \simeq t\}$ for any term $t$.
\item The $\rho$ construct allows one to rewrite occurrences of $t_1$ to $t_2$ in the synthesized or
  checked type, where $t_1$ and $t_2$ are provably equal.  In the Cedille implementation, we rewrite
  all matching occurrences.  This may be compared to \verb|rewrite| in Agda, except that it may be
  applied anywhere, not just as part of pattern matching~\cite{agda}.
\item We adopt a strong form of Nuprl's \textbf{direct computation rules}~\cite{constable+86}:
  If we have a term $t'$ of type $T$ and a proof $t$ that $\{ t' \simeq t''\}$, then we may conclude that
  $t''$ has type $T$ by writing the annotated term $\phi\ t\ \mhyph\ t'\{t''\}$, which
  erases to $t''$.
\item Where the previous version of CDLE uses $\beta$-equivalence for (erased) terms, we here adopt $\beta\eta$-equivalence.  This
  allows us to observe in many cases that retyping functions are actually $\beta\eta$-equivalent to $\absu{\lambda}{x}{x}$.
  While $\beta\eta$-equivalence takes more work to incorporate into intrinsic
  type theory~\cite{geuvers92},
  it raises no difficulties for our extrinsic one.
\item In this version, we add an explicit axiom $\delta$ saying that Church-encoded boolean \emph{true} is different from
  \emph{false}.  In the first version of CDLE, such an axiom was derivable from \emph{lifting}, a construct allowing
  simply typable terms to be lifted to the type level~\cite{stump17}.  We omit lifting in this new version of CDLE, because while
  sound, lifting as defined in that previous work is complicated and appears to be incomplete.  Developing a new
  form of lifting remains to future work.
\end{itemize}

The equality type remains \textbf{intensional}: we equate terms iff they are $\beta\eta$-equal.

\subsection{Semantics and metatheory}

Figure~\ref{fig:semtp} gives a realizability semantics for types and
kinds, following the semantics given in the previous papers on
CDLE~\cite{stump18,stump17}.  Details of this semantics are presented
further in Section~\ref{sec:snd} below.  Using the semantics and the
definition in Figure~\ref{fig:semctxt} of $\interp{\Gamma}$, we can
prove the following theorem:
\begin{theorem}[Soundness]
\label{thm:snd}
Suppose $(\sigma,\rho)\in\interp{\Gamma}$.  Then we have:
\begin{enumerate}
\item If $\Gamma\vdash \kappa$, then $\interp{\kappa}_{\sigma,\rho}$ is defined.
\item If $\Gamma\vdash T \tpsynth \kappa$, then $\interp{T}_{\sigma,\rho}\in\interp{\kappa}_{\sigma,\rho}$.
\item If $\Gamma\vdash t \tpsynth T$ then $[\sigma |t|]_{\cbe}\in\interp{T}_{\sigma,\rho}\in \mathcal{R}$.
\item If $\Gamma\vdash t \tpcheck T$ and $\interp{T}_{\sigma,\rho}\in \mathcal{R}$, then
    $[\sigma |t|]_{\cbe}\in\interp{T}_{\sigma,\rho}\in \mathcal{R}$.
\item If $T \cong T'$ or $T \cong^t T'$ and $\interp{T}_{\sigma,\rho}$ and $\interp{T'}_{\sigma,\rho}$ are both defined, then they are equal.
\end{enumerate}
\end{theorem}

An easy corollary, by the semantics of $\forall$-types, is then:

\begin{theorem}[Logical consistency]
\label{thm:consis}
  There is no term $t$ such that $\vdash t : \abs{\forall}{X}{\star}{X}$.
\end{theorem}

It may worry some readers that we have:
\begin{observation}
  There are typable terms $t$ which fail to normalize.
\end{observation}

Defining \verb|Top| to be $\{\absu{\lambda}{x}{x} \simeq
\absu{\lambda}{x}{x}\}$, we may assign \verb|Top| to any closed term \verb|t|,
including non-normalizing ones.  In our annotated syntax, we write \verb|β{t}|.
Even without this, the presence of
$\delta$ in combination with $\phi$ allows us to type non-normalizing
terms assuming an erased argument $x$ of type $\{ \textit{tt} \simeq
\textit{ff} \}$ for Church-encoded booleans \textit{tt} and
\textit{ff}.  For example, $\delta\ x$ has type
\verb|{λ x . x ≃ λ x . x x}|, and with $\phi$ we can use this to type
$\Omega$ by changing the typed term \verb|id · True id|, where
\verb|True| is \verb|∀ X : ★ . X ➔ X|. But failure of normalization
does not impinge on Theorem~\ref{thm:consis}.   Extensional Martin-L\"of type theory (MLTT) is
also non-normalizing, for a very similar reason, but fact
does not contradict its logical soundness~\cite{dybjer16}.  In CDLE, the guarantees one gets about the
behavior of terms are expressed almost entirely in their types.  If
the types are weak, then not much is guaranteed; but stronger types
can guarantee properties like normalization.

Given the lack of normalization, several checks in the typing rules --
for things like $t =_{\beta\eta} t'$ -- are formally undecidable.  In
practice, we simply impose a bound on the number of steps of reduction,
and thus restore formal decidability (we are checking ``typable within
a given budget'').  In practice, the same is done for Coq and Agda,
where type checking is decidable but, in general, infeasible (since one
may write astronomically slow terminating functions).

Finally, in line with ideas recently advocated by Dreyer, we
do not concern ourselves with syntactic
type preservation~\cite{dreyer18}, noting instead that by construction,
semantic types $\interp{T}_{\sigma,\rho}$ are preserved by $\beta\eta$-reduction:

  \begin{theorem}[Semantic type preservation]
    If $t \leadsto_{\beta\eta} t'$ and $t\in\interp{T}_{\sigma,\rho}$, then $t'\in\interp{T}_{\sigma,\rho}$.
    \end{theorem}

  Confluence of $\beta\eta$-reduction for (erased)
  terms is nothing other than confluence of untyped lambda calculus.
  This is because, as easily verified by inspecting
  Figure~\ref{fig:eraser}, the erasure function maps annotated terms
  $t$ to terms $|t|$ of pure untyped lambda calculus.
\begin{comment}
  \begin{lemma}
    If $t$ is an annotated term of CDLE, then $|t|$ is a term of pure untyped lambda calculus.
    \end{lemma}
\end{comment}

\begin{figure}
\[
\begin{array}{lll}
\interp{X}_{\sigma,\rho} & = & \rho(X) \\
\interp{\Pi x : T_1. T_2}_{\sigma,\rho} & = &
    [\{ \lambda x.t\ |\ \forall E\in\interp{T_1}_{\sigma,\rho}.\\
\ &\ &\ \ \ \  [[\choice(E)/x]t]_{\cbe}\in\interp{T_2}_{\sigma[x\mapsto \choice(E)],\rho} \ \wedge\ t = |t|\}]_{\cbe}
 \\
\interp{\forall X:\kappa.T}_{\sigma,\rho} & = &
  \elcap \{ \interp{T}_{\sigma,\rho[X\mapsto S]} |\ S\in\interp{\kappa}_{\sigma,\rho} \}  \\
\interp{\forall x:T.T'}_{\sigma,\rho} & = &
  \elcap_\star \{ \interp{T'}_{\sigma[x\mapsto \choice(E)],\rho}\ |\ E\in\interp{T}_{\sigma,\rho} \} \\
\interp{\iota x:T.T'}_{\sigma,\rho} & = & \{ E\in\interp{T}_{\sigma,\rho} |\ E \in \interp{T'}_{\sigma[x\mapsto \choice(E)],\rho} \} \\
\interp{\lambda X:\kappa.T}_{\sigma,\rho} & = & (S\in\interp{\kappa}_{\sigma,\rho} \mapsto \interp{T}_{\sigma,\rho[X\mapsto S]})
\\
\interp{\lambda x:T.T'}_{\sigma,\rho} & = &
    (E\in\interp{T}_{\sigma,\rho} \mapsto \interp{T'}_{\sigma[x\mapsto \choice(E)],\rho})
\\
\interp{T\ T'}_{\sigma,\rho} & = & \interp{T}_{\sigma,\rho}(\interp{T'}_{\sigma,\rho})
\\
\interp{T\ t}_{\sigma,\rho} & = & \interp{T}_{\sigma,\rho}([\sigma |t|]_{\cbe})
\\
\interp{t \simeq t'}_{\sigma,\rho} & = & [\{ t''\ |\ \sigma |t| =_{\beta\eta} \sigma |t'|\ \wedge\ t'' = |t''| \}]_{\cbe} \\
\ &\ &\ \ \ \textnormal{ if }\textit{FV}(t\ t')\subseteq\textit{dom}(\sigma)
\\
\interp{\star}_{\sigma,\rho} & = & \mathcal{R} \\
\interp{\Pi x:T.\kappa}_{\sigma,\rho} & = &
(E\in\interp{T}_{\sigma,\rho} \to \interp{\kappa}_{\sigma[x\mapsto \choice(E)],\rho}),\\
\ &\ &\ \ \ \textnormal{ if }\interp{T}_{\sigma,\rho}\in\mathcal{R} \\
\interp{\Pi x:\kappa.\kappa'}_{\sigma,\rho} & = & (S\in\interp{\kappa}_{\sigma,\rho} \to \interp{\kappa}_{\sigma,\rho[X\mapsto S]}) \\
\elcap_\star X & = & \left\{\begin{array}{l}
                                         \, \negthinspace\elcap X, \textnormal{ if } X\neq\emptyset\\
                                         \, \negthinspace[\mathcal{L}]_{\cbe},\textnormal{ otherwise}
                                       \end{array}\right. \\
\end{array}
\]
\caption{Semantics for types and kinds}
\label{fig:semtp}
\end{figure}

\begin{figure}
\[
\begin{array}{lll}
(\sigma\uplus[x\mapsto t],\rho)\in\interp{\Gamma,x:T} & \Leftrightarrow & (\sigma,\rho)\in\interp{\Gamma} \ \wedge\
 [t]_{\cbe}\in\interp{T}_{\sigma,\rho}\in\mathcal{R}\ \wedge\ t = |t| \\
(\sigma,\rho\uplus[X\mapsto S])\in\interp{\Gamma,X:\kappa} & \Leftrightarrow & (\sigma,\rho)\in\interp{\Gamma} \ \wedge\
S\in\interp{\kappa}_{\sigma,\rho} \\
(\emptyset,\emptyset)\in\interp{\cdot}
\end{array}
\]
\caption{Semantics of typing contexts $\Gamma$}
\label{fig:semctxt}
\end{figure}

\subsection{Some details about the semantics and the proof of Theorem~\ref{thm:snd}}
\label{sec:snd}

Following the development in~\cite{stump17}, we work with
set-theoretic partial functions for the semantics of higher-kinded
types.  Types are interpreted as $\beta\eta$-closed sets of closed
terms. Let $\mathcal{L}$ be the set of closed terms of pure lambda calculus
(differently from~\cite{stump17}, we include all terms at this point,
even non-normalizing ones).  We
write $=_{\cbe}$ for standard $\beta\eta$-equivalence of pure lambda calculus, restricted to
closed terms; and $[t]_{\cbe}$ for $\{ t'\ |\ t =_{\cbe} t'\}$.  This
is extended to sets $S$ of terms by writing $[S]_{\cbe}$ for
$\{[t]_{\cbe}\ |\ t\in S\}$.  In a few places we write
$\textit{nf}(t)$ for the (unique) $\beta\eta$-normal form of term $t$,
if it has one.  If (in our meta-language) we affirm a statement
involving application of a partial function, then it is to be
understood that that application is defined.

\begin{definition}[Reducibility candidates]
  $\mathcal{R} := \{ [S]_{\cbe}\ |\ S\subseteq \mathcal{L} \}$.
\end{definition}

Throughout the development we find it convenient to use a
\textbf{choice function} $\choice$.  Given any nonempty set $E$ of
terms, $\choice$ returns some element of $E$.  Note that if $a \in A
\in \mathcal{R}$, then $a$ is a nonempty set of terms of pure lambda
calculus; it can also happen that $A \in\mathcal{R}$ is empty.  The
proof of Theorem~\ref{thm:snd} (see appendix) is then a straightforward adaptation of~\cite{stump17}.

\textbf{Acknowledgments.}  This work was partially supported by the US
NSF support under award 1524519, and US DoD support under award
FA9550-16-1-0082 (MURI program).

\begin{thebibliography}{10}

\bibitem{constable+86}
Robert~L. Constable, Stuart~F. Allen, Mark Bromley, Rance Cleaveland, J.~F.
  Cremer, R.~W. Harper, Douglas~J. Howe, Todd~B. Knoblock, N.~P. Mendler,
  Prakash Panangaden, James~T. Sasaki, and Scott~F. Smith.
\newblock {\em Implementing mathematics with the Nuprl proof development
  system}.
\newblock Prentice Hall, 1986.

\bibitem{dreyer18}
Derek Dreyer.
\newblock {The Type Soundness Theorem That You Really Want to Prove (and Now
  You Can)}.
\newblock Milner Award Lecture, delivered at Principles of Programming
  Languages (POPL), 2018.

\bibitem{dybjer16}
Peter Dybjer and Erik Palmgren.
\newblock {Intuitionistic Type Theory}.
\newblock In Edward~N. Zalta, editor, {\em The Stanford Encyclopedia of
  Philosophy}. Metaphysics Research Lab, Stanford University, winter 2016
  edition, 2016.

\bibitem{geuvers92}
Herman Geuvers.
\newblock {The Church-Rosser Property for beta-eta-reduction in Typed
  lambda-Calculi}.
\newblock In {\em Proceedings of the Seventh Annual Symposium on Logic in
  Computer Science {(LICS} '92), Santa Cruz, California, USA, June 22-25,
  1992}, pages 453--460. {IEEE} Computer Society, 1992.

\bibitem{kopylov03}
Alexei Kopylov.
\newblock Dependent intersection: {A} new way of defining records in type
  theory.
\newblock In {\em 18th {IEEE} Symposium on Logic in Computer Science {(LICS)}},
  pages 86--95, 2003.

\bibitem{agda}
\mbox{The Agda development team}.
\newblock {\em Agda}, 2018.
\newblock Version 2.5.4.

\bibitem{miquel01}
Alexandre Miquel.
\newblock {The Implicit Calculus of Constructions Extending Pure Type Systems
  with an Intersection Type Binder and Subtyping}.
\newblock In Samson Abramsky, editor, {\em {Typed Lambda Calculi and
  Applications}}, volume 2044 of {\em Lecture Notes in Computer Science}, pages
  344--359. Springer, 2001.

\bibitem{mishraLinger08}
Nathan Mishra{-}Linger and Tim Sheard.
\newblock {Erasure and Polymorphism in Pure Type Systems}.
\newblock In Roberto~M. Amadio, editor, {\em Foundations of Software Science
  and Computational Structures, 11th International Conference, {FOSSACS} 2008,
  Held as Part of the Joint European Conferences on Theory and Practice of
  Software, {ETAPS} 2008, Budapest, Hungary, March 29 - April 6, 2008.
  Proceedings}, volume 4962 of {\em Lecture Notes in Computer Science}, pages
  350--364. Springer, 2008.

\bibitem{peytonjones07}
Simon Peyton~Jones, Dimitrios Vytiniotis, Stephanie Weirich, and Mark Shields.
\newblock {Practical Type Inference for Arbitrary-rank Types}.
\newblock {\em J. Funct. Program.}, 17(1):1--82, January 2007.

\bibitem{pierce+00}
Benjamin~C. Pierce and David~N. Turner.
\newblock Local type inference.
\newblock {\em {ACM} Trans. Program. Lang. Syst.}, 22(1):1--44, 2000.

\bibitem{stump17}
Aaron Stump.
\newblock {The Calculus of Dependent Lambda Eliminations}.
\newblock {\em J. Funct. Program.}, 27:e14, 2017.

\bibitem{stump18}
Aaron Stump.
\newblock {From Realizability to Induction via Dependent Intersection}, 2018.
\newblock in press.

\end{thebibliography}

%% Appendix
\appendix

\section{Proof of Theorem~\ref{thm:snd}}

First a few lemmas (easy proofs omitted):

\begin{lemma}
  $\interp{\kappa}_{\sigma,\rho}$ is nonempty if defined.
\end{lemma}

\begin{lemma}
\label{lem:choice}
If $E$ is nonempty, then $[\choice(E)]_{\cbe} = E$
\end{lemma}

\begin{lemma}
  The set $\mathcal{R}$ ordered by subset forms a complete lattice,
  with greatest element $[\mathcal{L}]_{\cbe}$ and greatest lower bound
  of a nonempty set of elements given by
  intersection.  Also, $\emptyset$ is the least element.
\end{lemma}

\begin{lemma}[Term substitution and interpretation]
\label{lem:termsubstinterp}
If $t' =_{\cbe} \sigma |t|$, then:
\begin{itemize}
\item $\interp{T}_{\sigma[x\mapsto t'],\rho} = \interp{[t/x]T}_{\sigma,\rho}$
\item $\interp{\kappa}_{\sigma[x\mapsto t'],\rho} = \interp{[t/x]\kappa}_{\sigma,\rho}$
\end{itemize}
\end{lemma}

\begin{lemma}[Type substitution and interpretation]
\label{lem:tpsubstinterp}
\begin{itemize}
\item $\interp{T}_{\sigma,\rho[X\mapsto\interp{T'}_{\sigma,\rho}]} = \interp{[T'/X]T}_{\sigma,\rho}$
\item $\interp{\kappa}_{\sigma,\rho[X\mapsto\interp{T'}_{\sigma,\rho}]} = \interp{[T'/X]\kappa}_{\sigma,\rho}$
\end{itemize}
\end{lemma}

\begin{lemma}
  \label{lem:interppres}
  If $T \leadsto^*_{\beta} T'$ and $\interp{T}_{\sigma,\rho}$ is defined, then $\interp{T'}_{\sigma,\rho}$ is also defined and equals $\interp{T}_{\sigma,\rho}$.
\end{lemma}
\begin{proof}
  This follows by induction on the reduction derivation, making use of the previous substitution lemmas.
  \end{proof}

\begin{proof}[Soundness (Theorem~\ref{thm:snd})]
  The following proof is adapted from~\cite{stump17}.  It proceeds by
  mutual induction on the assumed typing, kinding, or superkinding
  derivation, for each part of the lemma.  We prove the parts
  successively.

\subsection{Proof of part (1)}

\startcase{.2cm}
\[
\infer{\Gamma \vdash \star }{\ }
\]
$\interp{\star}_{\sigma,\rho}$ is just $\mathcal{R}$, which is
defined.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash\abs{\Pi}{x}{T}{\kappa}}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash\kappa}
\]
By the IH, $\interp{T}_{\sigma,\rho}\in\mathcal{R}$, and so
$\interp{\Pi x : T.\, \kappa}_{\sigma,\rho}$ is
$(E\in\interp{T}_{\sigma,\rho} \to \interp{\kappa}_{\sigma[x\mapsto  \choice(E)],\rho})$.
The latter quantity is defined if for all
$E\in\interp{T}_{\sigma,\rho}$, $\interp{\kappa}_{\sigma[x\mapsto \choice(E)],\rho})$ is, too.  Since
$\interp{T}_{\sigma,\rho}\in\mathcal{R}$, every element $E$ of
$\interp{T}_{\sigma,\rho}$ is nonempty, as noted above,
so $\choice(E)$ is defined.  We may apply the IH to the second
premise, since
$(\sigma[x\mapsto\choice(E)],\rho)\in\interp{\Gamma,x:T}$, because $E\in\interp{T}_{\sigma,\rho}$ (by assumption)
and $[\choice(E)]_{\cbe} = E$.  This gives definedness of the semantics
of the $\Pi$-kind.

\startcase{.2cm}
\[
   \infer{\Gamma\vdash\abs{\Pi}{X}{\kappa'}{\kappa}}{\Gamma \vdash \kappa' & \Gamma,X:\kappa'\vdash\kappa}
\]
We must show $(S\in\interp{\kappa}_{\sigma,\rho} \to \interp{\kappa}_{\sigma,\rho[X\mapsto S]})$ is defined.
This is true if $\interp{\kappa}_{\sigma,\rho}$ is defined, which is the case by
the IH applied to the first premise; and if for all
$S\in\interp{\kappa}_{\sigma,\rho}$,
$\interp{\kappa}_{\sigma,\rho[X\mapsto S]}$ is defined.  The latter is
true by the IH applied to the second premise.

\subsection{Proof of part (2)}

\startcase{.2cm}
\[
\infer{\Gamma \vdash X \tpsynth \kappa}{(X : \kappa) \in \Gamma}
\]
From the definition of $\interp{\Gamma}$, we obtain
$\rho(x)\in\interp{\kappa}_{\sigma,\rho}$.

\startcase{.2cm}
\[
   \infer{\Gamma\vdash\abs{\Pi}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star}
\]
We must show $\interp{\Pi x:T.T'}_{\sigma,\rho}\in\mathcal{R}$. The
semantics defines $\interp{\Pi x:T.T'}_{\sigma,\rho}$ to be
$[A]_{\cbe}$ for a certain $A$, where if $A$ is defined, then
$A\subseteq\mathcal{L}$.  So it suffices to shown definedness. By the IH
for the first premise, $\interp{T}_{\sigma,\rho}\in\mathcal{R}$.  This
means that if $E\in\interp{T}_{\sigma,\rho}$, $\choice(E)$ is defined.
We can then apply the IH to the second premise, since
$\sigma[x\mapsto\choice(E)]\in\interp{\Gamma,x:T}$, to obtain
definedness of $\interp{T'}_{\sigma[x\mapsto\choice(E),\rho}$.

\startcase{.2cm}
\[
   \infer{\Gamma\vdash\abs{\forall}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star}
\]
By the IH for the second premise, $\interp{T_2}_{\sigma[x\mapsto  \choice(E)],\rho}\in\mathcal{R}$, for every
$E\in\interp{T_1}_{\sigma,\rho}$ where
$\interp{T_1}_{\sigma,\rho}\in\mathcal{R}$.  By the IH for the first
premise, we indeed have $\interp{T_1}_{\sigma,\rho}\in\mathcal{R}$.
So if $\interp{T_1}_{\sigma,\rho}$ is non-empty, then the intersection of all the sets
$\interp{T_2}_{\sigma[x\mapsto \choice(E)],\rho}$ where $E\in\interp{T_1}_{\sigma,\rho}$ is a
reducibility candidate, since each of those sets is.  By the semantics
of $\forall$-types quantifying over terms, this is sufficient.  If $\interp{T_1}_{\sigma,\rho}$ is
empty, then the interpretation of the $\forall$-type is $[\mathcal{L}]_{\cbe}$ by the definition
of $\elcap_\star$, and this is in $\mathcal{R}$.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \abs{\forall}{X}{\kappa}{T} \tpsynth \star}{\Gamma \vdash \kappa & \Gamma,X:\kappa\vdash T \tpsynth \star}
\]
Similarly to the previous case: by the IH for the second premise,
$\interp{T_2}_{\sigma,\rho[X\mapsto S}\in\mathcal{R}$, for every
$S\in\interp{\kappa}_{\sigma,\rho}$.  By the IH part for the first
premise, $\interp{\kappa}_{\sigma,\rho}$ is defined.  So the
intersection of all the sets $\interp{T_2}_{\sigma,\rho[X\mapsto S]}$
where $S\in\interp{\kappa}_{\sigma,\rho}$ is a reducibility candidate,
since each of those sets is.  The intersection is nonempty, since $\interp{\kappa}_{\sigma,\rho}$ is (as stated in a lemma above).
By the semantics of $\forall$-types
quantifying over types, this is sufficient.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash\abs{\iota}{x}{T}{T'} \tpsynth \star}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T' \tpsynth \star}
\]
The set $\interp{\iota x:T.T'}_{\sigma,\rho}$ is explicitly defined to
be a subset of $\interp{T}_{\sigma,\rho}$, which is in $\mathcal{R}$,
by the IH applied to the first premise.  Since for any
$A\subseteq\mathcal{L}$, $[A]_{\cbe}$ is in $\mathcal{R}$, to show that
$\interp{\iota x:T.T'}_{\sigma,\rho}$ is also in $\mathcal{R}$ it suffices
to show definedness of $\interp{T'}_{\sigma[x\mapsto \choice(E)],\rho}\}$
(which is used in the predicate picking out the
particular subset of $\interp{T}_{\sigma,\rho}$), for
$E\in\interp{T}_{\sigma,\rho}$.  For such $E$, $\choice(E)$ is defined
(since $\interp{T}_{\sigma,\rho}\in\mathcal{R}$ and hence $E\in\interp{T}_{\sigma,\rho}$ is nonempty)
and in $E$,
so $\sigma[x\mapsto\choice(E)]\in\interp{\Gamma,x:T}$.  So
by the IH for the second premise,
$\interp{T'}_{\sigma[x\mapsto\choice(E),\rho]}$ is defined.

\startcase{.2cm}
\[
 \infer{\Gamma\vdash\abs{\lambda}{x}{T}{T'} \tpsynth \abs{\Pi}{x}{T}{\kappa}}{\Gamma \vdash T \tpsynth \star & \Gamma,x:T\vdash T'\tpsynth\kappa}
\]
By the semantics, $\interp{\lambda x:T.T'}_{\sigma,\rho}$ is
$(E\in\interp{T}_{\sigma,\rho} \mapsto \interp{T'}_{\sigma[x\mapsto
  \choice(E)],\rho})$.  We must show that this (meta-level) function
is in $\interp{\Pi x:T.\kappa}_{\sigma,\rho}$.  By the semantics of
kinds, the latter quantity, if defined, is
$(E\in\interp{T}_{\sigma,\rho} \to_{\cbe}
\interp{\kappa}_{\sigma[x\mapsto \choice(E)],\rho})$.
By the IH for the first premise, $\interp{T}_{\sigma,\rho}\in\mathcal{R}$.
So we must just show that for any $E\in\interp{T}_{\sigma,\rho}$,
$\interp{T'}_{\sigma[x\mapsto
  \choice(E)],\rho}\in\interp{\kappa}_{\sigma[x\mapsto
  \choice(E)],\rho}$.  But this follows by the IH for the second
premise.

\startcase{.2cm}
\[
\infer{\Gamma\vdash\abs{\lambda}{X}{\kappa}{T'} \tpsynth \abs{\Pi}{X}{\kappa}{\kappa'}}
      {\Gamma \vdash \kappa & \Gamma,X:\kappa\vdash T'\tpsynth\kappa'}
\]
This case is an easier version of the previous one.  It suffices to
assume an arbitrary $S\in\interp{\kappa}_{\sigma,\rho}$ and show
$\interp{T'}_{\sigma,\rho[X\mapsto S]}\in\interp{\kappa'}_{\sigma,\rho[X\mapsto S]}$.  But this follows
by the IH applied to the second premise.  And we have definedness of
$\interp{\kappa}_{\sigma,\rho}$ by the IH for the first premise.

\startcase{.2cm}
\[
   \infer{\Gamma\vdash T\ t \tpsynth [t/x]\kappa}{\Gamma\vdash T \tpsynth \abs{\Pi}{x}{T'}{\kappa} & \Gamma\vdash t \tpcheck T'}
\]
By the IH for the first premise,
$\interp{T}_{\sigma,\rho}\in\interp{\Pi x:T'.\kappa}_{\sigma,\rho}$.
By the semantics of $\Pi$-kinds, this means that
$\interp{T}_{\sigma,\rho}$ is a function which given any
$E\in\interp{T'}_{\sigma,\rho}$, will produce a result in
$\interp{\kappa}_{\sigma[x\mapsto \choice(E)],\rho}$.  By the
semantics of type applications, $\interp{T\ t}_{\sigma,\rho}$ is equal
to $\interp{T}_{\sigma,\rho}([\sigma |t|]_{\cbe})$.  This is defined,
since $[\sigma |t|]_{\cbe}\in\interp{T'}_{\sigma,\rho}$, by the IH for
the second premise; note that $\interp{T'}_{\sigma,\rho}$ is defined
since otherwise $\interp{\Pi x:T'.\kappa}_{\sigma,\rho}$ would not be defined.
The result of applying the function is thus indeed
in $\interp{[t/x]\kappa}_{\sigma,\rho}$, since by
Lemma~\ref{lem:termsubstinterp}, this equals
$\interp{\kappa}_{\sigma[x\mapsto \choice([\sigma |t|]_{\cbe})],\rho}$
(the codomain of the function being applied).

\startcase{.2cm}
\[
\infer{\Gamma\vdash T\cdot T' \tpsynth [T'/X]\kappa}
      {\Gamma\vdash T \tpsynth \abs{\Pi}{X}{\kappa'}{\kappa} & \Gamma\vdash T' \tpsynth \kappa' & \kappa \cong \kappa'}
\]
By the IH applied to the first premise,
$\interp{T}_{\sigma,\rho}\in\interp{\abs{\Pi}{X}{\kappa'}{\kappa}}_{\sigma,\rho}$.
By the semantics of $\Pi$-kinds, this means that for any
$S\in\interp{\kappa'}_{\sigma,\rho}$, $\interp{T}_{\sigma,\rho}\ S$ is in
$\interp{\kappa}_{\sigma,\rho[X\mapsto S]}$.  By the IH for the second premise, we have
$\interp{T'}\interp{\kappa'}_{\sigma,\rho}$, and by the IH for the third premise,
we have $\interp{\kappa}_{\sigma,\rho} = \interp{\kappa'}_{\sigma,\rho}$.  So
we get $\interp{T}_{\sigma,\rho}(\interp{T'}_{\sigma,\rho})\in \interp{\kappa}_{\sigma,\rho[X\mapsto \interp{T'}_{\sigma,\rho}]}$,
which suffices by Lemma~\ref{lem:tpsubstinterp}.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \{ t \simeq t' \} : \star}{\textit{FV}(t\ t')\subseteq\textit{dom}(\Gamma)}
\]
Either $\sigma |t| =_{\cbe} \sigma |t'|$ or not.  Either way, the interpretation is defined and in $\mathcal{R}$, since
$\textit{FV}(t\ t')\subseteq\textit{dom}(\sigma)$ (as an easy consequence of $(\sigma,\rho)\in\interp{\Gamma}$).

\subsection{Proof of parts (3) and (4)}

\startcase{.2cm}
\[
    \infer{\Gamma\vdash x\tpsynth T}{(x : t)\in\Gamma}
\]
This follows from the definition of $\interp{\Gamma}$.

\startcase{.2cm}
\[
 \infer{\Gamma\vdash t\tpcheck T'}{\Gamma\vdash t \tpcheck T & T' \leadsto^*_\beta T}
\]
We are assuming $\interp{T'}_{\sigma,\rho}$ is defined, since this is a checking judgment. The desired result then
follows from Lemma~\ref{lem:interppres}.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash t\tpsynth T'}{\Gamma\vdash t \tpsynth T & T \leadsto^*_\beta T'}
\]
This also follows from Lemma~\ref{lem:interppres} and the induction hypothesis for the first premise,
which implies $\interp{T}_{\sigma,\rho}\in\mathcal{R}$ (and hence defined).

\startcase{.2cm}
\[
\infer{\Gamma\vdash t\tpcheck T}{\Gamma\vdash t\tpsynth T' & T' \cong T}
\]
By the IH applied to the first premise, we have
$[\sigma t]_{\cbe}\in\interp{T'}_{\sigma,\rho}\in\mathcal{R}$. By assumption, $\interp{T}_{\sigma,\rho}\in\mathcal{R}$,
and so by the IH applied to the second premise, we have $[\sigma t]_{\cbe}\in\interp{T'}_{\sigma,\rho} = \interp{T}_{\sigma,\rho}$.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \absu{\lambda}{x}{t} \tpcheck \abs{\Pi}{x}{T}{T'}}{\Gamma,x:T\vdash t\tpcheck T'}
\]
To show $[\sigma \lambda x.t]_{\cbe}\in\interp{\Pi  x:T.T'}_{\sigma,\rho}$ (noting that the latter is defined
and in $\mathcal{R}$ by assumption), it suffices to assume an arbitrary
$E\in\interp{T}_{\sigma,\rho}$, and show
$[[\choice(E)/x]\sigma t]_{\cbe}\in\interp{T'}_{\sigma[x\mapsto\choice(E)],\rho}$.  By the IH,
we have
$[\sigma[x\mapsto\choice(E)]t]_{\cbe}\in\interp{T'}_{\sigma[x\mapsto\choice(E)],\rho}$.
But $[\sigma[x\mapsto\choice(E)]t]_{\cbe} = [[\choice(E)/x]\sigma t]_{\cbe}$,
so this is sufficient.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash t\ t' \tpsynth [t'/x]T}{\Gamma\vdash t \tpsynth \abs{\Pi}{x}{T'}{T} & \Gamma\vdash t' \tpcheck T'}
\]
By the IH applied to the first premise, $[\sigma
t]_{\cbe}\in\interp{\Pi x:T'.T}_{\sigma,\rho}\in\mathcal{R}$.  This
means that there exists a $\lambda$-abstraction $\lambda x.\hat{t}$
such that $\lambda x.\hat{t} =_{\cbe} \sigma t$, by the semantics of $\Pi$-types.
Furthermore, for any $E\in\interp{T'}_{\sigma,\rho}$,
$[[\choice(E)/x]\hat{t}]_{\cbe}\in\interp{T}_{\sigma[x\mapsto\choice(E)],\rho}$.
By the IH applied to the second premise, $[\sigma t']_{\cbe}\in\interp{T'}_{\sigma,\rho}$,
so we can instantiate the quantifier in the previous formula to obtain
\[
 [[\choice([\sigma t']_{\cbe})/x]\hat{t}]_{\cbe}\in\interp{T}_{\sigma[x\mapsto\choice([\sigma t']_{\cbe})],\rho}
\]
By Lemma~\ref{lem:termsubstinterp}, this is equivalent to
\[
 [[\choice([\sigma t']_{\cbe})/x]\hat{t}]_{\cbe}\in\interp{[t'/x]T_2}_{\sigma,\rho}
\]
Since $\sigma (t\ t') =_{\cbe} (\lambda x.\hat{t})\ \sigma t' =_{\cbe} [[\choice([\sigma t']_{\cbe})/x]\hat{t}$,
this is sufficient.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \absu{\Lambda}{X}{t} \tpcheck \abs{\forall}{X}{\kappa}{T}}{\Gamma,X:\kappa\vdash t \tpcheck T}
\]
By the IH, $[\sigma |t|]_{\cbe}\in\interp{T}_{\sigma,\rho[X\mapsto S]}$, for all $S\in\interp{\kappa}_{\sigma,\rho}$.
This is sufficient to prove $[\sigma |\absu{\Lambda}{X}{t}|]_{\cbe}\in\interp{\forall X:\kappa.T}_{\sigma,\rho}$, by the semantics
of $\forall$-types and definition of erasure.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash t \cdot T' \tpsynth [T'/X]T}
          {\Gamma\vdash t \tpsynth \abs{\forall}{X}{\kappa}{T} & \Gamma\vdash T' \tpcheck\kappa}
\]
By the semantics of $\forall$-types and the IH applied to the first
premise, we have $[\sigma |t|]_{\cbe}\in\interp{T}_{\sigma,\rho[X\mapsto
  S]}$, for all $S\in\interp{\kappa}_{\sigma,\rho}$.  Since
$\interp{T'}_{\sigma,\rho}\in\interp{\kappa}_{\sigma,\rho}$ by the IH
applied to the second premise, we can derive $[\sigma t]_{\cbe}\in\interp{T}_{\sigma,\rho[X\mapsto \interp{T'}_{\sigma,\rho}]}$.
By Lemma~\ref{lem:tpsubstinterp},
this is equivalent to the required $[\sigma |t|]_{\cbe}\in\interp{[T'/X]T}_{\sigma,\rho}$,
using also the definition of erasure.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \absu{\Lambda}{x}{t} \tpcheck \abs{\forall}{x}{T'}{T}}{\Gamma,x:T'\vdash t \tpcheck T & x\not\in\textit{FV}(|t|)}
\]

By the IH applied to the first premise, we have
$[\sigma[x\mapsto\choice(E)]
t]_{\cbe}\in\interp{T'}_{\sigma[x\mapsto\choice(E)],\rho}$, for any
$E\in\interp{T}_{\sigma,\rho}$.  This is because
$\interp{T}_{\sigma,\rho}\in\mathcal{R}$, since $\interp{\abs{\forall}{x}{T'}{T}}_{\sigma,\rho}$ is in $\mathcal{R}$ and
hence defined, by assumption.  Since $x\not\in\textit{FV}(t)$, we know
$[[\sigma[x\mapsto\choice(E)]t]_{\cbe} = [\sigma t]_{\cbe}$.  By the
semantics of $\forall$-types and definition of erasure, this suffices to show the desired
conclusion.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash t\ \mhyph t' \tpsynth [t'/x]T}{\Gamma\vdash t \tpsynth \abs{\forall}{x}{T'}{T} & \Gamma\vdash t' \tpcheck T'}
\]
The result follows easily by the IH applied to the premises, the
semantics of $\forall$-types, definition of erasure, and Lemma~\ref{lem:termsubstinterp}.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash [ t , t' ] \tpcheck \abs{\iota}{x}{T}{T'}}
          {\Gamma\vdash t \tpcheck T & \Gamma\vdash t' \tpcheck [t/x]T' & |t| =_{\beta\eta} |t'|}
\]
By the IH, we have $[\sigma |t|]_{\cbe}\in\interp{T}_{\sigma,\rho}$ and
$[\sigma |t|]_{\cbe}\in\interp{[t/x]T'}_{\sigma,\rho}$.  By
Lemma~\ref{lem:termsubstinterp}, the latter is equivalent to
$[\sigma t]_{\cbe}\in\interp{T'}_{\sigma[x\mapsto\choice([\sigma t]_{\cbe}),\rho}$.  These two facts about $[\sigma t]_{\cbe}$ are
sufficient, by the semantics of $\iota$-types, for the desired
conclusion, using also the fact (from the third premise) that $\sigma|t| =_{\cbe} \sigma|t'|$.

\startcase{.2cm}
\[
   \infer{\Gamma\vdash t.1 \tpsynth T}{\Gamma\vdash t \tpsynth \abs{\iota}{x}{T}{T'}}
\]
The desired conclusion follows easily from the IH and the semantics of $\iota$-types.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash t.2 \tpsynth [t.1/x]T'}{\Gamma\vdash t \tpsynth \abs{\iota}{x}{T}{T'}}
\]
Similar to the previous case, using Lemma~\ref{lem:termsubstinterp}.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \beta\{t'\} \tpcheck \{ t \simeq t \}}{\Gamma\vdash \textit{FV}(t)\subseteq \textit{dom}(\Gamma)}
\]
$[\sigma|t'|]_{\cbe}\in\interp{\{ t \simeq t \}}_{\sigma,\rho}$ follows directly from the semantics of equality types.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \delta\ t \tpcheck T}{\Gamma\vdash t\tpsynth \{ \absu{\lambda}{x}{\absu{\lambda}{y}{x}} \simeq \absu{\lambda}{x}{\absu{\lambda}{y}{y}}\}}
\]
By the semantics of equality types, $[\sigma|t'|]_{\cbe}$ cannot be in the interpretation of the equation in the premise,
since the two terms in question are closed and not $\beta\eta$-equal.  By the IH applied to the first premise, however,
$[\sigma|t'|]_{\cbe}$ is in the interpretation of that equation.  This is a contradiction.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \rho\ t'\ \mhyph\ t \Leftrightarrow [t_2/x]T}
          {\Gamma\vdash t' \tpsynth t_1 \simeq t_2 & \Gamma \vdash t \Leftrightarrow [t_1/x]T}
\]
By the IH applied to the first premise, $\sigma|t_1| =_{\beta\eta} \sigma|t_2|$.  The result then follows by the IH applied to the second premise,
and Lemma~\ref{lem:termsubstinterp}.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \chi\ T\ \mhyph\ t \tpcheck T'}
          {\Gamma\vdash T\tpcheck \star & \Gamma\vdash t \tpcheck T & T \cong T'}
\]
Using the IH for the first premise and the assumption that $\interp{T'}_{\sigma,\rho}$ is in $\mathcal{R}$ and hence defined,
we can apply the IH to the third premise to get $\interp{T}_{\sigma,\rho} = \interp{T'}_{\sigma,\rho}$.  Using this and the IH for second premise,
we get the desired conclusion, using also the definition of erasure.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \chi\ T\ \mhyph\ t \tpsynth T}
          {\Gamma\vdash T\tpcheck \star & \Gamma\vdash t \tpsynth T' & T \cong T'}
\]
By the IH applied to the second premise, we have $[\sigma|t|]_{\cbe}\in\interp{T'}_{\sigma,\rho}\in\mathcal{R}$.
Using definedness of $\interp{T'}_{\sigma,\rho}$ and the IH applied to the first premise, we can apply the
IH to the third premise to get $\interp{T}_{\sigma,\rho} = \interp{T'}_{\sigma,\rho}$, from which the desired
conclusion follows by definition of erasure.

\startcase{.2cm}
\[
    \infer{\Gamma\vdash \phi\ t\ \mhyph\ t'\{t''\} \Leftrightarrow T}
          {\Gamma\vdash t\tpsynth \{t'\simeq t''\} & \Gamma\vdash t' \Leftrightarrow T}
\]
By the IH for the first premise, $\sigma|t'| =_{\cbe} \sigma|t''|$.  By the IH for the second
premise, $[\sigma|t'|]_{\cbe}\in\interp{T}_{\sigma,\rho}$.  This suffices for the desired conclusion,
using also the definition of erasure ($|\phi\ t\ \mhyph\ t'\{t''\}| = |t''|$).

\subsection*{Proof of part (5) }

\startcase{.2cm}
\[
    \infer{T \cong T'}{T \leadsto^*_\beta T_1 & T' \leadsto^*_\beta T_2 & T_1\cong^t T_2}
\]
By Lemma~\ref{lem:interppres}, we have
\[
\begin{array}{lll}
  \interp{T}_{\sigma,\rho} & = & \interp{T_1}_{\sigma,\rho}\\
  \interp{T'}_{\sigma,\rho} & = & \interp{T_2}_{\sigma,\rho}
\end{array}
\]
By the IH for the third premise, we have $\interp{T_1}_{\sigma,\rho} = \interp{T_2}_{\sigma,\rho}$,
which suffices.

\startcase{.2cm}
\[
    \infer{T \cong T'}{T \cong^t T'}
\]
By the IH.

\startcase{.2cm}
\[
    \infer{T\ t \cong^t T'\ t'}{T \cong^t T' & |t| =_{\beta\eta} |t'|}
\]
By the semantics, $\interp{T\ t}_{\sigma,\rho} =
\interp{T}_{\sigma,\rho}([\sigma|t|]_{\cbe})$.  By the second premise
and the IH for the first premise, this equals
$\interp{T'}_{\sigma,\rho}([\sigma|t'|]_{\cbe})$, as required.

\startcase{.2cm}
\[
    \infer{\{ t_1 \simeq t_2 \} \cong^t \{ t_1'\ \simeq t_2' \}}{|t_1| =_{\beta\eta} |t_1'| & |t_2| =_{\beta\eta} |t_2'|}
\]
This follows easily from the premises and the semantics of equality types.

\end{proof}
\end{document}


\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb,graphicx,enumerate,latexsym}

%%%%%%%%%% Start TeXmacs macros
\newcommand{\dueto}[1]{\textup{\textbf{(#1) }}}
\newcommand{\longupdownarrow}{{\mbox{\rotatebox[origin=c]{-90}{$\longleftrightarrow$}}}}
\newcommand{\nobracket}{}
\newcommand{\tmaffiliation}[1]{\\ #1}
\newcommand{\tmemail}[1]{\\ \textit{Email:} \texttt{#1}}
\newcommand{\tmop}[1]{\ensuremath{\operatorname{#1}}}
\newenvironment{enumeratenumeric}{\begin{enumerate}[1.] }{\end{enumerate}}
\newenvironment{proof}{\noindent\textbf{Proof\ }}{\hspace*{\fill}$\Box$\medskip}
%%%%%%%%%% End TeXmacs macros

\begin{document}

\title{Topology}

\author{
  Liangchun Xu
  \tmaffiliation{Department of Mechanical Engineering, Tufts University\\
  574 Boston Avenue, Medford, 02155, US}
  \tmemail{liangchun.xu@tufts.edu}
}

\date{March 4, 2019}

\maketitle

{\tableofcontents}

\section{Cardinality and the Axiom of Choice}

{\definition*{{\dueto{number sets}}Throughout these notes we will use the
following notation:
\begin{enumeratenumeric}
  \item $\mathbb{N}= \tmop{the} \tmop{set} \tmop{of} \tmop{natural}
  \tmop{numbers}$ (i.e., the positive integers).

  \item $\mathbb{Z}= \tmop{the} \tmop{set} \tmop{of} \tmop{all}
  \tmop{integers}$.

  \item $\mathbb{Q}= \tmop{the} \tmop{set} \tmop{of} \tmop{rational}
  \tmop{numbers}$.

  \item $\mathbb{R}= \tmop{the} \tmop{set} \tmop{of} \tmop{real}
  \tmop{numbers}$.
\end{enumeratenumeric}}}

{\definition*{{\dueto{cardinality}}Two sets, $A \tmop{and} B$, have the same
cardinality iff there is a 1-1, onto function $f : X \rightarrow \{ 1, 2,
\ldots, n \}$ where $n$ is an element of $\mathbb{N}$. A set that's not finite
is infinite.}}

About the natural numbers $\mathbb{N}$: every non-empty set of natural numbers
has a least element.

{\theorem*{The even positive integers have the same cardinality as the natural
numbers.}}

\begin{proof}
  The even positive integers are
  \begin{eqnarray*}
    \mathbb{E}^+ & = & \{ \nobracket 2 n | n \in \mathbb{N} \}
  \end{eqnarray*}
  Construct function $f : \mathbb{N} \rightarrow \mathbb{E}^+$
  \[ f (n) = 2 n \]
  $f (n)$ is in $\mathbb{E}^+$ by definition. Claim $f$ is 1-1 and onto:

  1-1: Suppose $f (n) = f (n')$, then $2 n = 2 n'$ and $n = n'$;

  onto: Suppose $b \in \mathbb{E}^+$, then $b = 2 n$ for some $n \in
  \mathbb{N}$, $f (n) = 2 n = b$.

  As we have exhibited a $1 - 1$ onto function $f : \mathbb{N} \rightarrow
  \mathbb{E}^+$, $| \mathbb{N} | = | \mathbb{E}^+ |$.
\end{proof}

{\theorem*{$| \mathbb{N} | = | \mathbb{Z} |$.}}

\begin{proof}
  The correpondence is
  \[ \begin{array}{ccccccccccc}
       0 & - 1 & 1 & - 2 & 2 & - 3 & 3 & - 4 & 4 & - 5 & \ldots\\
       \longupdownarrow &  &  &  & \longupdownarrow &  &  &  &  &
       \longupdownarrow & \\
       1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \ldots
     \end{array} \]

  Construct function $f : \mathbb{N} \rightarrow \mathbb{Z}$
  \[ f (n) = \left\{\begin{array}{l}
       \frac{n - 1}{2}, \tmop{if} n \in \mathbb{E}^+\\
       - \frac{n}{2}, \tmop{if} n \in \mathbb{O}^+
     \end{array}\right. \]
  $f (n)$ is in $\mathbb{Z}$ by definition. Claim $f$ is 1-1 and onto:

  1-1: Suppose $f (n) = f (n')$, then
  \[ \left\{\begin{array}{l}
       \frac{n - 1}{2} = \frac{n' - 1}{2}, \tmop{if} n \in \mathbb{E}^+\\
       - \frac{n}{2} = - \frac{n'}{2}, \tmop{if} n \in \mathbb{O}^+
     \end{array}\right. \Rightarrow n = n' \]
  Since $\frac{n - 1}{2} \geqslant 0 \tmop{and} - \frac{n}{2} < 0$, we have
  $\frac{n - 1}{2} \neq - \frac{n'}{2}$; only the above two cases are
  possible.

  onto: Suppose $b \in \mathbb{Z}$, then
  \[ \left\{\begin{array}{l}
       b = \frac{n - 1}{2}, \tmop{if} b \geqslant 0\\
       b = - \frac{n}{2}, \tmop{if} b < 0
     \end{array}\right. \]
  for some $n \in \mathbb{N}$, $f (n) = b$.

  As we have exhibited a $1 - 1$ onto function $f : \mathbb{N} \rightarrow
  \mathbb{Z}$, $| \mathbb{N} | = | \mathbb{Z} |$.
\end{proof}

{\theorem*{{\dueto{pigeon-hole principle}}Suppose that $n$ is a natural number
and you have $n$ pidgeon-holes. If you have $m$ podgeons where $m > n$, and
you put each pidgeon in one of your $n$ pidgeon-holes, there will be some hole
which contains more than 1 pidgeon.}}

\begin{proof}
  Let $q_i$ be the number of pidgeons in each hole, then
  \[ \sum_{i = 1}^n q_i = m \]
  Suppose no hole contains more than 1 pidgeon, which is
  \[ 0 \leqslant q_i \leqslant 1, \applicationspace{1 \tmop{em}} i = 1, 2,
     \ldots n \]
  Then we have
  \[ \sum_{i = 1}^n q_i \leqslant \sum_{i = 1}^n 1 = n \]
  Since $m > n$, then
  \[ \sum_{i = 1}^n q_i < m \]
  which leads to contradiction. Therefore the assumption is not correct. There
  must be some hole which contains more than 1 pidgeon.
\end{proof}

{\theorem*{{\dueto{induction}}For each natural number $n$, let $S (n)$ be a
statement that is either true or false. Then suppose that
\begin{enumeratenumeric}
  \item $S (1)$ is true.

  \item If $S (k)$ is true, then $S (k + 1)$ is true.
\end{enumeratenumeric}
Then $S (n)$ is true for all natural numbers $n$.}}

\begin{proof}
  Suppose there is an non-empty set
  \[ X = \{ n \in \mathbb{N}, S (n) \tmop{is} \tmop{false} \} \]
  There exists a smallest element $x \in X$ since $X \subseteq \mathbb{N}$.

  According to assumption 1 and the definiton of $X$, $x \neq 1 \tmop{and} x
  > 1$.

  According to assumption 2, if $S (x)$ is false, then $S (x - 1)$ is also
  false. Since $x - 1 < x$ and $x - 1 \in \mathbb{N}$, $x - 1$ becomes the
  smallest element in $X$, which is contradictory to the fact that $x$ is the
  smallest element in $X$. Therefore the assumption is wrong. $X$ is empty and
  $S (n)$ is true for all natural numbers $n$.
\end{proof}

{\theorem*{Every subset of $\mathbb{N}$ is either finite or has the same
cardinality as $\mathbb{N}$.}}

\begin{proof}
  Let $X \subseteq \mathbb{N}$, construct function $f : \mathbb{N} \rightarrow
  X$
  \begin{eqnarray*}
    f (1) & = & \min (X)\\
    f (2) & = & \min (X\backslash \{ f (1) \})\\
    & \vdots & \\
    f (n) & = & \min (X\backslash \{ f (1), f (2), \ldots, f (n - 1) \})
  \end{eqnarray*}
  if $X\backslash \{ f (1), f (2), \ldots, f (n - 1) \}$ is empty, then we
  have a 1-1 bijection $f : \{ 1, 2, \ldots, n \} \rightarrow X$, which means
  $X$ is finite. If $X$ is infinite, then claim $f : \mathbb{N} \rightarrow X$
  is $1 - 1$ and onto:

  1-1: Suppose $f (n) = f (n')$, then $n = n'$.

  onto: Suppose $b \in X$, then $b = \min (X\backslash \{ f (1), f (2),
  \ldots, f (n - 1) \})$ for some $n \in \mathbb{N}$, $f (n) = b$.

  As we have exhibited a $1 - 1$ onto function $f : \mathbb{N} \rightarrow X$,
  $| \mathbb{N} | = | X |$.

  Therefore every subset of $\mathbb{N}$ is either finite or has the same
  cardinality as $\mathbb{N}$.
\end{proof}

{\definition*{{\dueto{countable set}}A set that has the same cardinality as a
subset of $\mathbb{N}$ is countable.}}

{\theorem*{Every infinite set has a countably infinite subset.}}

{\theorem*{A set is infinite iff there is a 1-1 function from the set into a
proper subset of itself.}}

{\theorem*{$\mathbb{Q}$ is countable.}}

\begin{proof}
  Construct function $f : \mathbb{N} \rightarrow \mathbb{Q}$

  \[ \begin{array}{lllllllllllll}
       & 0 &  & - 1 &  & 1 &  & - 2 &  & 2 &  & - 3 & \ldots\\
       1 & \frac{0}{1} & \rightarrow & - \frac{1}{1} & \rightarrow &
       \frac{1}{1} &  & - \frac{2}{1} & \rightarrow & \frac{2}{1} &  & -
       \frac{3}{1} & \ldots\\
       &  &  &  & \swarrow &  & \nearrow &  & \swarrow &  & \nearrow &  & \\
       2 &  &  & - \frac{1}{2} &  & \frac{1}{2} &  & - \frac{2}{2} &  &
       \frac{2}{2} &  &  & \\
       &  &  & \downarrow & \nearrow &  & \swarrow &  & \nearrow &  &  &  &
       \\
       3 &  &  & - \frac{1}{3} &  & \frac{1}{3} &  & - \frac{2}{3} &  &  &  &
       & \\
       &  &  &  & \swarrow &  & \nearrow &  &  &  &  &  & \\
       4 &  &  & - \frac{1}{4} &  & \frac{1}{4} &  &  &  &  &  &  & \\
       &  &  & \downarrow & \nearrow &  &  &  &  &  &  &  & \\
       5 &  &  & - \frac{1}{5} &  &  &  &  &  &  &  &  & \\
       \vdots &  &  &  &  &  &  &  &  &  &  &  &
     \end{array} \]

  $f (n)$ is in $\mathbb{Z}$ by definition. Claim $f$ is 1-1 and onto:

  1-1: Suppose $f (n) = f (n')$, then

  onto: Suppose $b \in \mathbb{Z}$, then

  \

  for some $n \in \mathbb{N}$, $f (n) = b$.

  As we have exhibited a $1 - 1$ onto function $f : \mathbb{N} \rightarrow
  \mathbb{Q}$, $| \mathbb{N} | = | \mathbb{Q} |$.
\end{proof}

\end{document}


% !TeX spellcheck = en_US
\documentclass{yLectureNote}

\title{Analyse I}
\subtitle{subtitle}
\author{Harvey Sheppard}
\date{Fall Semester 2016}
\yLanguage{English}

\professor{M. Professor}

\usepackage{lipsum}

\begin{document}
	\titleOne

	\yTableOfContent

	\chapter{Mathematical Argumentation and Proofs}
	\printMarginPartialToc

	\section{Mathematical Argumentation}
	\classDate{13}{9}{2016}
	\nextSerie

	\lipsum[1]

	\marginTips*{Don't remember to practice your math! It is the only way to get through!}

	\subsection{Using Conditions}
	\lipsum[2]
	\marginCritical*{I should reread this part, as I didn't understood it well... Maybe it's because it's in Latin?}

	\lipsum[3]\marginElement{\marginTitle{Title} \lipsum*[7]}

	\checkInfo{Yeaaaah!}{\lipsum[4]\lipsum[2]}

	\section{Mathematical Proofs}
	\begin{theorem}[Pythagoras]
		Assuming we have a rectangle triangle with the hypotenuse named c. Then:
		\[
			a^2 + b^2 = c^2
		\]
	\end{theorem}

	\marginInfo*{This formula is really important:
		\begin{equation}
		a^2 + b^2 = c^2
		\end{equation}
	}

	\yOrnament
\end{document}


\appendix

\section{Appendix: The \texttt{VPINN\_HelmholtzImpedanceRF} Network}
\label{app:deeprf}
\autoref{lst:deeprf} includes the code implementation of the proposed network on top of the parent class. In this architecture, we use a local version of ReLU as the activation function of the first hidden layer, and a sinusoidal (hyperbolic tangent) activation function for other hidden layers. The idea is to impose different behaviors for different regions of the domain. \autoref{fig:deeprftrain} shows that without any other consideration, training such a network fails. The solution of the failed training is shown in \autoref{fig:deeprfsol}. Some other similar architectures have been tried out, which also failed. Among those were putting the ReLU activation function only on the last layer, and non-local ReLU activation functions on the first two layers.

\begin{figure}[h!]
    \centering
    \includegraphics[width = 0.48\textwidth]{img/DeepRF-D004N012K038-training.png}
    \caption{Training loss and solution error of the \texttt{VPINN\_HelmholtzImpedanceRF} network with 4 hidden layers for wave number $k=8.0$.}
    \label{fig:deeprftrain}
\end{figure}

\begin{figure}[h!]
    \centering
    \includegraphics[width = 0.7\textwidth]{img/DeepRF-D004N012K038-sol.png}
    \caption{The solution of the \texttt{VPINN\_HelmholtzImpedanceRF} network with 4 hidden layers for wave number $k=8.0$.}
    \label{fig:deeprfsol}
\end{figure}

\lstinputlisting[
    language=Python,
    style=pythonstyle,
    label={lst:deeprf},
    caption={Code snippet of the \texttt{VPINN\_HelmholtzImpedanceRF} class which inherits from the main \texttt{VPINN\_HelmholtzImpedance} class.}
    ]
{lst/VPINN-HelmholtzImpedanceRF.py}

\section{Appendix: Evolution of the Solution}
\label{app:evolutions}
\autoref{fig:evolutionrandinitsol} illustrates the evolution of the solution of a network architecture that converges to the solution in 30000 epochs. We can see that from the initial random initialization, the network is able to capture the main features of the solution in 2000 epochs, and converges to the final solution in the following 28000 epochs. An interesting observation is that there is a bias from left to right which could also be captured in the initial result of the network (epoch 0). We can see that the solution converges faster in the ranges closer to the left boundary.

The same bias to the left boundary could also be observed in \autoref{fig:evolutionlsinitsol} which illustrates the evolution of solution of the same network with least-squares initialization in the initial 2000 epochs. In \autoref{fig:vpinnslsinitk8}, we can see that the solution error of this network does not improve much with least-square initialization after around epoch 2000. Another observation is that the most of the error comes from the imaginary part of the solution, which is not the case with random initialization.

\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-00000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-02000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-04000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-08000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-08000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-16000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_randinit/sol-30000.png}
    \end{subfigure}
    \caption{The evolution of the solution during the training of a shallow network with 20 nodes and 20 test functions for $k=8.0$.}
    \label{fig:evolutionrandinitsol}
\end{figure}

\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_lsinit/sol-00000.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_lsinit/sol-00350.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_lsinit/sol-00700.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_lsinit/sol-01400.png}
    \end{subfigure}
    \vfill
    \begin{subfigure}[b]{0.85\textwidth}
        \includegraphics[width=\textwidth]{img/evolution_lsinit/sol-02000.png}
    \end{subfigure}
    \caption{The evolution of the solution during the training of a shallow network with 20 nodes and 20 test functions with least-squares initialization (constant weights) for $k=8.0$.}
    \label{fig:evolutionlsinitsol}
\end{figure}


%=================================================================
\section{GPU Testbed and Selected CUDA kernels} \label{sec:methodChar}
Several different benchmarks have been proposed in the literature to measure and assess existing GPGPU heterogeneous architectures, such as Rodinia~\citep{Rodinia:Che:2009}, Parboil~\citep{Stratton:2012:Parboil} and SHOC~\citep{Danalis:2010:SHOC}. Rodinia benchmark was devised for heterogeneous parallel computing research, it has had a high level of acceptance in the community and its applications represent different high-level domains or behaviours,  called the Berkeley dwarfs~\cite{asanovic2009dwarfs}. In this work, we have selected  a set of GPU Applications from the Rodinia Benchmark suite and  other classical algorithms of linear algebra. First to all, we present in the next subsection the different GPUs that we used for our experiments. After in Subsection~\ref{ssec:useCases}, we characterize each one of the kernel that we used for our experiments.

%For each application we iterated over the values of one or two parameters, with some applications invoking the same kernel multiple times on each execution. During our evaluation, all applications were executed using the CUDA profiling tool \textit{nvprof}. Each experiment is presented as the average of ten executions, with a confidence interval of 95\%. In this section we describe the GPU applications, GPU testbed used to do this work.

%---------------------------------------------------------------------------

\subsection{GPU Testbed}\label{ssec:GPUTestbed}
For our experiments in the Chapter~\ref{chap:BSPmodel} and Chapter~\ref{Chap:ML} we used 9 different GPUs, described in Table~\ref{tab:GPUs}, with 5 belonging to Kepler architecture (Compute Capability 3.X), 3 to Maxwell (C.C. 5.X) and 1 to Pascal (C.C. 6.x). More information about these GPUs are presented in Table~\ref{tab:CC} and we have described the main changes of hardware and software between architectures in Section~\ref{ssec:GPUroadmap}.

%Experiments with the Tesla Pascal P-100 were performed accessing the Free OpenPOWER Cloud by Unicamp, running in an IBM OpenPOWER Server configured with 16 physical cores, up to 128 simultaneous treads or vcpus, and 2 x NVIDIA GPU Tesla-P100, connected via NVLINK 1.0.

% \todoRaph{You described where the Pascal experiments were performed. And the other boards? \textbf{Corrected - That was question of acknowledge}}

\begin{table}[htpb]
    \centering
    \scalebox{0.75}{
    \begin{tabular}{lccccccc}
        \toprule
        \textbf{Model}&\textbf{C.C.}&\textbf{Memory}&\textbf{Bus}&\textbf{Bandwidth}&\textbf{L2}&\textbf{Cores/SM}&\textbf{Clock} \\ \bottomrule
        GTX-680&3.0&2 GB&256-bit&192.2 GB/s&0.5 M&1536/8&1058 Mhz \\ \midrule
        Tesla-K40&3.5&12 GB&384-bit&276.5 GB/s&1.5 MB&2880/15&745 Mhz \\ \midrule
        Tesla-K20&3.5&4 GB&320-bit&200 GB/s&1 MB&2496/13&706 MHz\\ \midrule
        %Titan Black&3.5&6 GB&384-bit&336 GB/s&1.5 MB&2880/15 &980 Mhz \\ \midrule
        Titan&3.5&6 GB&384-bit&288.4 GB/s&1.5 MB&2688/14&876 Mhz\\ \midrule
        Quadro K5200&3.5&8 GB&256-bit&192.2 Gb/s&1 MB&2304/12&771 Mhz \\ \midrule
        Titan X&5.2&12 GB&384-bit&336.5 GB/s&3 MB&3072/24&1076 Mhz \\ \midrule
        GTX-970&5.2&4 GB&256-bit&224.3 GB/s&1.75 MB&1664/13&1279 Mhz\\ \midrule
        GTX-980&5.2&4 GB&256-bit&224.3 GB/s&2 MB&2048/16&1216 Mhz \\ \midrule
        Pascal-P100&6.0 GB&16 GB&4096-bit&732 GB/s&4 MB&3584/56&1328 Mhz\\ \midrule
    \end{tabular}}
    \caption{Hardware specifications of the GPUs in the testbed}
    \label{tab:GPUs}
\end{table}

\subsection{Selected CUDA kernels}\label{ssec:useCases}
The  source code for all the use cases, experiments and results are available\footnote{Hosted at GitHub: \texttt{\scriptsize https://github.com/marcosamaris/gpuperfpredict} [Accessed on March 2018]} under Creative Commons Public License for the sake of reproducibility.

Our benchmark contains 4 different strategies for \emph{matrix multiplication}~\citep{CUDAGuide}, 2 algorithms for \emph{matrix addition}, 1 dot product algorithm, 1 vector addition algorithm and 1 \emph{maximum sub-array problem} algorithm~\citep{Cleber:Thesis}, and 11 CUDA kernel functions belonging to 6 applications from Rodinia benchmarking suite (see Table~\ref{tab:Rodinia}). The remainder of this section discusses some details of these algorithms, and introduces a code of letters for each application, used in Chapter~\ref{chap:BSPmodel} and Chapter~\ref{Chap:ML}.

\subsubsection{Matrix Multiplication}
Matrix multiplication is the core of many scientific areas. This operation is highly used in areas like deep learning, visual computing and digital images processing, among others. The analysis and modeling of these algorithms brings a better understanding and help to researcher and developer of Job Management Systems to deal with this mathematical operations.

In this research, we used four different versions of matrix multiplication, these versions differ in memory access optimizations: global memory with non-coalesced accesses (MMGU); global memory with coalesced accesses (MMGC); shared memory with non-coalesced accesses (MMSU); and shared memory with coalesced accesses (MMSC). This algorithm has a high utilization of the Streaming Processors and it obtains a high throughput of communication in the different levels of memory, L2 cache and L1 cache for small matrices. We have adopted \texttt{block\_size}$^2$ threads per block and defined the number of blocks to be square of \texttt{(N + block\_size-1)/block\_size}, dynamically devised from the size of the problem ($N$) and the block size. Aiming to take advance of coalesced accesses the value of \texttt{block\_size} is equal to 16 in our experiments. In Section~\ref{sec:characterization} is presented an communication analysis of this application with different sizes of threads per block.

The asymptotic computational complexity of a square matrix multiplication of size $N$ is $O(N^3)$ in a sequential algorithm, in a CUDA algorithm this complexity is $O(N)$ using $N\times{}N$ threads. In this algorithm each thread requests $N$ elements from both matrices and perform a dot product with these arrays. This CUDA kernel performs $N$ reads from global memory for each matrix and $N$ arithmetic operations. A single write instruction is performed. shared memory is not used in two versions, MMGU and MMGC. In Figure~\ref{fig:matMulGMUN} is shown the source code of the kernel MMGU, we only changed the data access pattern, to permit coalesced accesses to data in global memory. In Figure \ref{fig:matMulGMUN} line 7 is changed to \texttt{Pvalue += A\_d[i * N + k] * B\_d[k * N + j];} and line 10 is changed to \texttt{C\_d[i * Width + j] = Pvalue;}.

\lstset{emph={[2]__global__}, emphstyle={[2]\color{red}\bf },language=C++, keywordstyle=\color{blue}, numbers=left, showspaces=false,
    showstringspaces=false, tabsize=1, breaklines=false,stringstyle=\color{red}, commentstyle=\color{red}, morecomment=[l][\color{magenta}]{\#}}

\begin{figure}[htpb]
\centering
{\scriptsize
\begin{lstlisting}
__global__ void matMul(float* C_d, float* A_d, float* B_d, int N) {
  float Pvalue = 0.0;
  int j = blockIdx.x * blockDim.x + threadIdx.x;
  int i = blockIdx.y * blockDim.y + threadIdx.y;

  for (int k = 0; k < N; ++k)
    Pvalue += A_d[j * N + k] * B_d[k * N + i];

  C_d[j * N + i] = Pvalue;
}

\end{lstlisting}}
\caption{Kernel in CUDA of matrix multiplication only with global memory and uncoalesced accesses (MMGU).}
\label{fig:matMulGMUN}
\end{figure}

The version MMSU and MMSC use shared memory to load data from global memory and to process them with a lower latency of communication. As shared memory is limited in GPU architectures, the implementations of matrix multiplication with shared memory must be tiled. The concept of tiling in shared memory is graphically described with Figure~\ref{fig:TilingMMSU}, with the matrix multiplication. Tiling is a common strategy to partition data into subset called tiles such that each tile fits into the shared memory. This technique splits our problem domain into phases. The tiled process is executed to guarantee that each thread can access data in shared memory to perform its part of the matrix multiplication.

In process shown in Figure~\ref{fig:TilingMMSU}, a tile process charges a subset from matrix A and Matrix B in the shared memory of the GPU. A barrier synchronization is used to guarantee that all data was loaded in the shared memory, after the calculations are performed, the barrier synchronization guarantees that the shared memory can be safely overwritten. In this application \texttt{tile\_size=block\_size}, consequently the sizes in bytes of both subsets of the matrices will be \texttt{(tile\_size)$^2 \times$FP\_Bytes}, where \texttt{FP\_Bytes} is the size of the single precision used for the application.

\begin{figure}[htpb]
	\centering
    \includegraphics[scale=.3]{images/square-tiling.png}
    \caption{Tiling technique of the Matrix multiplication using shared memory}
    \label{fig:TilingMMSU}
\end{figure}

\subsubsection{Matrix Addition}
For the Matrix addition algorithm, we used two different memory access optimizations: global memory with non-coalesced accesses (MAU); and global memory with coalesced accesses (MAC);  The run-time complexity for a sequential matrix addition algorithm using two matrices of size $N\times{}N$ is $O(N^2)$. In a CUDA implementation, the run-time complexity of the matrix addition is $O(1)$ using $N^2$ threads. In this algorithm each thread request $1$ element from each one of the matrix elements and perform a single addition. The implementation of this kernel is similar than matrix multiplication. In Figure \ref{fig:matMulGMUN} the loop of the line 6-7 is deleted and the statement with the addition is added \texttt{C[tid] = A[tid] + B[tid];}, where \texttt{tid} = \texttt{int tid = i*N + j;}. To take advances of coalesced accesses, \texttt{tid} is changed to \texttt{int tid = j*N + i;}.

Matrix addition has the same threads hierarchy than matrix multiplication. The blocks are bi-dimensional with \texttt{block\_size}$^2$ threads per block. The size of the grid is square of \texttt{N/block\_size}, dynamically devised from the size of the problem \texttt{N} and \texttt{block\_size}.

\subsubsection{Vector Addition Algorithm (vAdd)}
For two vectors $A$ and $B$, the Vector Addition $C = A + B$ is obtained by adding the corresponding components. In a CUDA implementation, the run-time complexity of the vector addition algorithm is $O(1)$ using N threads, so each threads perform an addition of a position of the vectors $A$ and $B$ and stores the result in the vector $C$. This algorithm is a simplified version of the matrix addition. This application has an uni-dimensional block, the block size used for the experiments is $256$ and the size of the grid is also uni-dimensional and dynamically devised from the size of the problem  \texttt{N} and \texttt{block\_size}. The source code of this kernel is similar than matrix addition, only change the dimension indexes of each threads, i.e. \texttt{int tid = blockDim.x * blockIdx.x + threadIdx.x;}, using only the dimension $x$ of \texttt{blockDim}.

\subsubsection{Dot Product Algorithm (dotP)}
For two vectors $A$ and $B$, the dot product $C = A \cdot B$ is obtained by adding the multiplication of corresponding components of the input, the result of this operation is a scalar. Unlike vector addition, dot product is a reduction from vectors to a scalar. In a GPU algorithm, each thread performs a multiplication of a position of the vectors $A$ and $B$ and stores the result shared variable. Then a reduction using the shared memory is performed and finally a vector with size equal to \texttt{N/block\_size} is transferred to the CPU memory for later processing. The block size used for the experiments is $256$ and the size of the grid is also uni-dimensional and dynamically devised from the size of the problem  \texttt{N} and \texttt{block\_size}.

\subsubsection{Maximum Sub-Array Problem (MSA)}
Let $X$ be a sequence of $N$ integer numbers $(x_1, ... , x_N)$. The Maximum Sub-Array Problem (MSA) consists of finding the contiguous sub-array within $X$ which has the largest sum of elements. The solution for this problem is frequently used in computational biology for gene identification, analysis of sequence of protein and DNAs, identification of hydrophobic regions, among others. The implementation used in this paper creates a kernel with 4096 threads, divided in 32 blocks with 128 threads~\citep{Cleber:Thesis}. The $N$ elements are divided in intervals of $N/t$, and each block receives a portion of the array. The blocks use the shared memory for storing segments, which are read from the global memory using coalesced accesses. Each interval is reduced to a set of 5 integer variables, which are stored in vector of size $5 \times t$ in global memory. This vector is then transferred to the CPU memory for later processing.

A summary of each one of these applications is shown in Table~\ref{tab:useCases}, in this table is shown the thread hierarchy and the request shared memory per block in each kernel. In column \texttt{dimGrid} and \texttt{dimBlock} is shown how each solution is associated to the dimension of the problem. Column Shared Mem shows the size of shared memory that each kernel needs per block.

\begin{table}[htpb]
    \centering
    \scalebox{.9}{
        \begin{tabular}{cccccc}
            \midrule\midrule
            \textbf{Application} & \textbf{Param} & \textbf{Kernel}& \textbf{\texttt{dimGrid}}&  \textbf{\texttt{dimBlock}}&\textbf{Shared Mem}\\\midrule\midrule
            \multirow{4}{*}{Matrix Mul} & \multirow{4}{*}{1} &MMGU &\multirow{4}{*}{(GS, GS, 1)}&\multirow{4}{*}{ (BS, BS, 1)}&\multirow{4}{*}{($BS^2\times{}2\times{}4B$)}\\
            &  &MMGC & & &\\
            &  &MMSU & & &\\
            &  &MMSC & & &\\\midrule
            \multirow{2}{*}{Matrix Add} & \multirow{2}{*}{1} &MAU &\multirow{2}{*}{(GS, GS, 1)}&\multirow{2}{*}{ (BS, BS, 1)}&\multirow{2}{*}{0}\\
            &  &MAC & & & \\\midrule
            Vector Add&1  &VAdd &(GS, 1, 1) &(BS, 1, 1)& 0 \\\midrule
            Dot Product&1  &dotP &(GS, 1, 1) &(BS, 1, 1) &($BS\times{}4B$)\\\midrule
            Max. Sub Array&1  &MSA & (48, 1, 1) &(128, 1, 1)&($4096\times{}4B$)\\\midrule
        \end{tabular}}
    \caption{Key linear algebra applications used in the experiments}
    \label{tab:useCases}
\end{table}

\subsubsection{Rodinia Benchmark Suite}
We selected 11 CUDA kernel functions belonging to 6 applications from Rodinia for the benchmarks (Table~\ref{tab:Rodinia}). Some applications invoke the same kernel multiple times on each execution, resulting in the number of collected samples shown in the last column. For each kernel we iterated over the values of one or two parameters, with the number of iterations indicated inside the brackets. For instance, the Hot Spot application has 2 parameters, with the first iterated among 5 values and the second 4 values, for a total of 20 executions in each machine. %In those applications only some part of them can be compute concurrently, above we will explain the performance behavior of each one of the selected Rodinia benchmarks kernels.

\begin{table*}[htpb]
    \centering
    \scalebox{.8}{
        \begin{tabular}{cccccc}
            \midrule\midrule
            \textbf{Application} & \textbf{Berkeley Dwarf} & \textbf{Domain}& \textbf{Param.}& \textbf{Kernels} & \textbf{Samples}\\\midrule\midrule
            \multirow{2}{*}{Back Propagation (BCK)} & \multirow{2}{*}{Unstructured Grid} & \multirow{2}{*}{Pattern Recognition} &\multirow{2}{*}{1 - [57]}&layerforward&\multirow{2}{*}{57}\\
            &  &  & &adjust-weights  \\\midrule
            \multirow{2}{*}{Gaussian Elimination (GAU)} & \multirow{2}{*}{Dense Linear Algebra} & \multirow{2}{*}{Linear Algebra} &\multirow{2}{*}{1 - [32]}& Fan1&\multirow{2}{*}{34800}  \\
            &  &  & & Fan2  \\\midrule
            Heart Wall (HWL) & Structured Grid & Medical Imaging &1 - [84]& heartWall&5270 \\\midrule
            Hot Spot (HOT) & Structured Grid & Physics Simulation &2 - [5,4]& calculate-temp&396288 \\\midrule
            % Hot Spot 3D (H3D) & Structured Grid & Physics Simulation &2 - [3,10]& hotspotOpt1&150150 \\\midrule
            %LavaMD (LMD) & N-Body & Molecular Dynamics &1 - [50]& LavaMD&50\\\midrule
            \multirow{3}{*}{LU Decomposition (LUD)} & \multirow{3}{*}{Dense Linear Algebra} & \multirow{3}{*}{Linear Algebra} &\multirow{3}{*}{1 - [32]}&diagonal&8448 \\
            & & &  & perimeter&8416 \\
        &  &  & & internal&8416 \\\midrule
            \multirow{2}{*}{Needleman-Wunsch (NDL)} & \multirow{2}{*}{Dynamic Programming} & \multirow{2}{*}{Bioinformatics} &\multirow{2}{*}{2 - [16,10]}& needle-1&21760 \\
             &  &  & & needle-2&21600 \\\midrule

        \end{tabular}}
    \caption{Rodinia applications used in the experiments}
    \label{tab:Rodinia}
\end{table*}

\begin{itemize}
    \item {\bf Back Propagation (BCK):}
BCK trains a layered neural network. The application is comprised of two kernels: Forward Phase (BCK-K1), in which the activation are propagated from the input to the output layer, and Backward Phase (BCK-K2), in which the error between the observed and requested values in the output layer is propagated backwards to adjust the weights and bias values. The time complexity of a back-propagation neural network algorithm on a single processor is of $O(W^3)$; where $W$ is the count of weights in the network. In this CUDA implementation both kernels have a complexity $O(log(BS))$ where $BS=16$. Each block has \texttt{BLOCK\_SIZE}$^2$ number of threads. The number of blocks in the grid is \texttt{(N/block\_size} nad it is dynamically calculated from the layer size ($N$) and the block size.

In kernel (BCK-K1), only one thread (with id 0) in the each block load an element of the input layer on the shared memory, after each thread load an element of the weight matrix on the shared memory. Then, the weight matrix is updated with the values of the input layer. Finally a loop reduction is done with a loop of size log2(block\_size). Inside this loop, log2(block\_size) power instructions are done and log2(block\_size) additions over data in the shared memory. Each interval is reduced to a set of \texttt{grid\_size}$\times$\texttt{block\_size} integer variables. This vector is then transferred to the main memory of the host for later processing. In kernel (BCK-K2), shared memory is not used. According to the source code of (BCK-K2), each thread performs O(1) reads and write in the global memory and does different computations over this data.

\item{\bf Gaussian Elimination (GAU):}
GAU solves a linear system $Ax = b$, the application analyzes an $n\times{}n$ matrix and an associated $1 x n$ vector to solve a
set of equations with $n$ variables and $n$ unknowns. Gaussian Elimination algorithms has a complexity $O(n^3)$. This application compute the results row by row. The algorithm synchronizes between iterations, but the values calculated in each iteration is computed in parallel, see Figure~\ref{fig:GauCode}.

, where $n$ is the size of the matrix elements in both dimensions, i.e. the matrix is $n\times{}n$.

The application has two different kernels Fan1 and Fan2, which we call (GAU-K1) and (GAU-K2) respectively. (GAU-1) calculate multiplier matrix and (GAU-2) modify the matrix A into LUD. In the experiments, we varied the size of the matrix.

In this implementation, First kernel GAU-K1, has a uni-dimensionmal block, each block \texttt{block\_size = 512} threads and the number of block is dynamic and computed with the next expression \texttt{(N/block\_size)}. Second kernel has a bi-dimensional block, its dimension is \texttt{block\_size}$^2$ and \texttt{block\_size = 4} in this implementation. The size of the grid also is dynamic depending to size of the problem $N$and the $BS$ (Block size), and it is computed as \texttt{(N/block\_size)}. Both kernel are iterative, it means that the same kernels are invoked multiple times on a single execution of the whole application. Any of these kernel use the shared memory of the streaming processors.

\lstset{emph={[2]__global__}, emphstyle={[2]\color{red}\bf },language=C++, keywordstyle=\color{blue}, numbers=left, showspaces=false,
    showstringspaces=false, tabsize=1, breaklines=false,stringstyle=\color{red}, commentstyle=\color{red}, morecomment=[l][\color{magenta}]{\#}}

\begin{figure}[htpb]
\centering
{\scriptsize
\begin{lstlisting}
for (t=0; t<(Size-1); t++) {
		Fan1<<<dimGrid1,dimBlock1>>>(m_cuda,a_cuda,Size,t);
		cudaThreadSynchronize();
		Fan2<<<dimGrid2,dimBlock2>>>(m_cuda,a_cuda,b_cuda,Size,t);
		cudaThreadSynchronize();
	}
\end{lstlisting}}
\caption{Kernel in CUDA of matrix multiplication only with global memory and uncoalesced accesses (MMGU).}
\label{fig:GauCode}
\end{figure}

\item {\bf Heart Wall (HWL):}
HWL tracks the movement of a mouse heart over a sequence of 104 609x590 ultrasound images to record response to the stimulus. In the experiments, we varied the number of frames to process. This kernel has two different stages, in the first stage the kernel performs operations on the first frame to detect initial, partial shapes of inner and outer heart walls in the second stage the kernel presents multiple nested loops that process batches of 10 frames and 51 points in each image. This kernel is very complicated to analyze, i.e. this kernel has 1300 codes lines more or less, it uses 8 variables in the shared memory and 44 variables to store all its computations in each streaming multiprocessor, obtaining a low latency in this process. it runs multiple times depending the number of frame to compute. As the number of threads in a block as the number of blocks in a thread ar uni-dimensionals. For all the experiments, this kernels has a \texttt{block\_size}$= 256$ and a \texttt{grid\_size}$ = 51$. For a total of 13056 threads in each one of the executions. This application modeled a set of  ordinary differential equations (ODEs) that are determined by more than 200 parameters. HWL requires the inclusion of some non-parallel computation into the kernel, leading to a slight warp under-utilization.

% \todoRaph{Describe something about the computations performed in the HWL kernel/application.}

\item {\bf HotSpot (HOT):}
HOT is a tool to estimate processor temperature based on an architectural floor plan and simulated power measurements. This application includes the 2D transient thermal simulation kernel which iteratively solves a series of differential equations to determine block temperatures. This kernel has a bi-dimensional grid and block. The dimension of the block is square of \texttt{block\_size} and \texttt{block\_size}$ = 16$. The size of the grid is the square of \texttt{N/block\_size} and it is dynamic depending the size of the problem and the size of the thread block. In the experiments, we varied two parameters: size of the problem and number of iterations. This application has a single kernel. It uses the shared memory.

\item {\bf LU Decomposition (LUD):}
this is a factorization algoritm, where ''LU'' means \emph{lower upper}, LUD is an algorithm to calculate the solutions of a set of linear equations. The LUD kernel decomposes a matrix as the product of a lower triangular matrix and an upper triangular matrix. This benchmark present tree different kernels (LUD-K1, LUD-K2 and LUD-K3). Similarly to the Gaussian Elimination, the matrix size of this experiment was also iterated. First kernel LUD-K1 (named diagonal) has a static size of threads, the size of threads of this kernels always 16. The second kernel LUD-K2 (named perimeter) has a uni-dimensional block the size of thread per block always is \texttt{block\_size}$*2$, the number of block in the grid also is uni-dimensional and it is computed dynamically with the next expression \texttt{(matrix\_dim-i)/block\_size-1}. The third kernel LUD-K3 (named internal) has a block bi-dimensional, this size is the square of \texttt{block\_size} and \texttt{block\_size}$ = 16$, the grid also is bi-dimensional and it is computed with the same expression than the second kernel, \texttt{(matrix\_dim-i)/block\_size-1}.

\item {\bf Needleman-Wunsch (NDL):}
is a nonlinear global optimization method for DNA sequence alignments. The potential pairs of sequences are organized in a 2D matrix. In the first step, the algorithm fills the matrix from top left to bottom right, step-by-step. In the second step, the maximum path is traced backward to deduce the optimal alignment. Needleman-Wunsch has two CUDA kernels, NDL-K1 and NDL-K2. We iterated over the matrix dimension and penalty positive integer parameters. In both kernels each block use $2180$ KB of shared memory. This implementation use a static number of threads per block of 16 and the grid is dynamically computed with this expression $(N-1)/16$. Where $N$ is the size of the input matrix.

% \lstset{language=C++, keywordstyle=\color{blue}, numbers=left, showspaces=false,
%     showstringspaces=false, tabsize=1, breaklines=false,stringstyle=\color{red}, commentstyle=\color{red}, morecomment=[l][\color{magenta}]{\#}}

% \begin{figure}[htpb]
% 	\centering
%     \lstset{emph={[2]dim3},
%             emphstyle={[2]\color{red}\bf},
%             emph={[3]dimGrid, dimBlock},
%             emphstyle={[3]\color{blue}\bf},
%             emphstyle={\bf}, basicstyle={\ttfamily \scriptsize},
%             commentstyle={\em},
%             keywordstyle={\color{blue}\bf},
%             numbers=left,
%             captionpos=b,
%             xleftmargin=1cm}
% 	\begin{lstlisting}[frame=trBL,language=C,linewidth=.92\linewidth]
% for( int i = 1 ; i <= block_width ; i++){
% 		needle_cuda_shared_1<<<dimGrid, dimBlock>>>(referrence_cuda, matrix_cuda
% 		                                      ,max_cols, penalty, i, block_width);
% 	}
% 	for( int i = block_width - 1  ; i >= 1 ; i--){
% 		dimGrid.x = i;
% 		dimGrid.y = 1;
% 		needle_cuda_shared_2<<<dimGrid, dimBlock>>>(referrence_cuda, matrix_cuda
% 		                                      ,max_cols, penalty, i, block_width);
% 	}

% 	\end{lstlisting}
% \caption{Launched kernel with its respective dimension variables dimBlock and dimGrid}
% \label{fig:dimGridBlock}
% \end{figure}

\end{itemize}