\documentclass[10pt]{article}

\usepackage{url}
\usepackage{proof}
\usepackage{fullpage}

%% ODER: format ==         = "\mathrel{==}"
%% ODER: format /=         = "\neq "
\makeatletter

\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}

\newlength{\lwidth}\setlength{\lwidth}{4.5cm}
\newlength{\cwidth}\setlength{\cwidth}{8mm} % 3mm

\newcommand{\Conid}[1]{\mathit{#1}}
\newcommand{\Varid}[1]{\mathit{#1}}
\newcommand{\anonymous}{\kern0.06em \vbox{\hrule\@width.5em}}
\newcommand{\plus}{\mathbin{+\!\!\!+}}
\newcommand{\bind}{\mathbin{>\!\!\!>\mkern-6.7mu=}}
\newcommand{\sequ}{\mathbin{>\!\!\!>}}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\makeatother
\bibliographystyle{plain}

\parskip=\medskipamount
\parindent=0pt

\newcommand{\isa}{\ensuremath{\; {:}{:}{=} \;}}
\newcommand{\ora}{\ensuremath{\;\mid\;}}
\newcommand{\IF}{\ensuremath{\mathtt{ if \;}}}
\newcommand{\THEN}{\ensuremath{\mathtt{\; then \;}}}
\newcommand{\ELSE}{\ensuremath{\mathtt{\; else \;}}}
\newcommand{\TRUE}{\ensuremath{\mathtt{ true \;}}}
\newcommand{\FALSE}{\ensuremath{\mathtt{\; false \;}}}
\newcommand{\BOOL}{\ensuremath{\mathtt{\; Bool \;}}}
\newcommand{\INT}{\ensuremath{\mathtt{\; Int \;}}}
\newcommand{\PLUS}{\ensuremath{\mathtt{ plus \;}}}
\newcommand{\MINUS}{\ensuremath{\mathtt{ sub \;}}}

\title{Monadic Typed Lambda Calculus Interpreter}
\author{Perry Alexander \\
  ITTC - The University of Kansas \\
  2335 Irving Hill Rd \\
  Lawrence, KS 66045 \\
  \texttt{alex@ittc.ku.edu}}

\begin{document}

\maketitle

\section{Introduction}

The objective of this project is to write an interpreter for an
extended simply typed lambda calculus ($\lambda_\rightarrow$) based on
definitions from \emph{Types and Programming Languages}~\cite{Pie02a},
Chapter 8, Figure 8-1 and Chapter 9, Figure 9-1.  We will enhance the
basic language to include integers and integer sum and difference in
addition to the basic operations.  The definition of the abstract
syntax provides the following forms for $\lambda_\rightarrow$ terms,
values and types in:

\begin{eqnarray*}
  t & \isa & x \ora v \ora \lambda x:T.t \ora t\; t \ora \PLUS t\; t \ora \MINUS t\;
  t \\
  v & \isa & \lambda x:T.t \ora \mathcal{I} \ora \mathtt{true} \ora \mathtt{false}\\
  T & \isa & \BOOL \ora \INT \ora T \rightarrow T
\end{eqnarray*}

The definition for call-by-value evaluation provides the following
evaluation rules that will define the evaluation function:

\[\vcenter{\infer[\textsc{E-App1}]
  {t_1 t_2 \longrightarrow t_1^{'} t_2}
  {t_1\longrightarrow t_1^{'}}}\]

\[\vcenter{\infer[\textsc{E-App2}]
  {t_1 t_2 \longrightarrow t_1 t_2^{'}}
  {t_2\longrightarrow t_2^{'}}}\]

\[\vcenter{\infer[\textsc{E-AppAbs}]
  {(\lambda x:T.t_{12})v_2 \longrightarrow [x\rightarrow v_2]t_{12}}
  {}}\]

\[\vcenter{\infer[\textsc{E-IfTrue}]
  {t_2}
  {\IF \TRUE \THEN t_2 \ELSE t_3}}\]

\[\vcenter{\infer[\textsc{E-IfFalse}]
  {t_3}
  {\IF \FALSE \THEN t_2 \ELSE t_3}}\]

\[\vcenter{\infer[\textsc{E-If}]
  {\IF t_1 \THEN t_2 \ELSE t_3 \rightarrow \IF t_1^{'} \THEN t_2 \ELSE t_3}
  {t_1\rightarrow t_1^{'}}}
\]

\[\vcenter{\infer[\textsc{E-Plus1}]
  {\PLUS t_1^{'}\; t_2^{'}}
  {t_1\rightarrow t_1^{'} & t_2\rightarrow t_2^{'}}}
\]

\[\vcenter{\infer[\textsc{E-Plus2}]
  {\mathcal{I}_1 + \mathcal{I}_2}
  {\PLUS \mathcal{I}_1\; \mathcal{I}_2}}
\]

\[\vcenter{\infer[\textsc{E-Minus1}]
  {\MINUS t_1^{'}\; t_2^{'}}
  {t_1\rightarrow t_1^{'} & t_2\rightarrow t_2^{'}}}
\]

\[\vcenter{\infer[\textsc{E-Minus2}]
  {\mathcal{I}_1 - \mathcal{I}_2}
  {\MINUS \mathcal{I}_1\; \mathcal{I}_2}}
\]

where $\mathcal{I}$ is any constant integer value.

The following typing rules that define the type inference function:

\[\vcenter{\infer[\textsc{T-Var}]{\Gamma\vdash x:T}{x:T\in\Gamma}}\]

\[\vcenter{\infer[\textsc{T-Abs}]
  {\Gamma\vdash\lambda x:T_1.t_2 : T_1\rightarrow T_2}
  {\Gamma,x:T_1\vdash t_2 : T_2}}
\]

\[\vcenter{\infer[\textsc{T-App}]
  {\Gamma\vdash t_1\; t_2 : T_{12}}
  {\Gamma\vdash t_1 : T_{11}\rightarrow T_{12} & \Gamma\vdash t_2:T_{11}}}
\]

\[\vcenter{\infer[\textsc{T-If}]
  {\Gamma\vdash \IF t_1 \THEN t_2 \ELSE t_3 : T}
  {\Gamma\vdash t_1 : \BOOL & \Gamma\vdash t_2 : T & \Gamma\vdash t_3 : T}}
\]

\[\vcenter{\infer[\textsc{T-True}]{\TRUE:\BOOL}{}}\]

\[\vcenter{\infer[\textsc{T-False}]{\FALSE:\BOOL}{}}\]

\[\vcenter{\infer[\textsc{T-Plus}]
  {\PLUS t_1\; t_2 : \INT}
  {t_1 : \INT & t_2 : \INT}}
\]

\[\vcenter{\infer[\textsc{T-Minus}]
  {\MINUS t_1\; t_2 : \INT}
  {t_1 : \INT & t_2 : \INT}}
\]

Our objective is to: (i) define a data structure for representing
$\lambda_\rightarrow$ terms embodying the abstract syntax; (ii) a type
derivation function for $\lambda_\rightarrow$ terms embodying the type
rules; and (iii) an evaluation function for $\lambda_\rightarrow$
terms embodying the evaluation rules.

\section{Abstract Syntax}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\mathbf{module}\;\Conid{TypedLambdaAST}}$\\
${\hskip2.00em\relax\mathbf{where}}$\\
${}$\\
${\mathbf{import}\;\Conid{LangUtils}}$
\end{tabbing}
\subsection{Type Language}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\mathbf{data}\;\Conid{TyBase}\;\Varid{ty}\mathrel{=}\Conid{TyBool}\mid \Conid{TyInt}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{data}\;\Conid{TyAbs}\;\Varid{ty}\mathrel{=}\Varid{ty}\mathbin{:->:}\Varid{ty}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{type}\;\Conid{TyLangSum}\mathrel{=}(\Conid{Sum}\;\Conid{TyBase}\;\Conid{TyAbs})}$\\
${}$\\
${\mathbf{type}\;\Conid{TyLang}\mathrel{=}\Conid{Rec}\;\Conid{TyLangSum}}$\\
${}$\\
${\mathbf{instance}\;\Conid{Eq}\;\Conid{TyLang}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{x}\equiv \Varid{y}\mathrel{=}(\Varid{unS}\;(\Varid{out}\;\Varid{x}))\equiv (\Varid{unS}\;(\Varid{out}\;\Varid{y}))}$
\end{tabbing}
\subsection{Term Language}

The term language include Boolean values and integer values, addition
and subtraction operators, if-then-else expressions, and lambda
expressions and lambda application.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\mathbf{data}\;\Conid{TmBool}\;\Varid{te}\mathrel{=}\Conid{TmTrue}\mid \Conid{TmFalse}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmBool}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TmTrue}\mathrel{=}\Conid{TmTrue}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TmFalse}\mathrel{=}\Conid{TmFalse}}$\\
${}$\\
${\mathbf{data}\;\Conid{TmInt}\;\Varid{te}\mathrel{=}\Conid{TmConstInt}\;\Conid{Int}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmInt}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmConstInt}\;\Varid{x})\mathrel{=}(\Conid{TmConstInt}\;\Varid{x})}$\\
${}$\\
${\mathbf{data}\;\Conid{TmOp}\;\Varid{te}\mathrel{=}\Conid{TmAdd}\;\Varid{te}\;\Varid{te}\mid \Conid{TmSub}\;\Varid{te}\;\Varid{te}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmOp}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}(\Conid{TmAdd}\;(\Varid{f}\;\Varid{x})\;(\Varid{f}\;\Varid{y}))}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}(\Conid{TmSub}\;(\Varid{f}\;\Varid{x})\;(\Varid{f}\;\Varid{y}))}$\\
${}$\\
${\mathbf{data}\;\Conid{TmIf}\;\Varid{te}\mathrel{=}\Conid{If}\;\Varid{te}\;\Varid{te}\;\Varid{te}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmIf}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}(\Conid{If}\;(\Varid{f}\;\Varid{c})\;(\Varid{f}\;\Varid{t})\;(\Varid{f}\;\Varid{e}))}$\\
${}$\\
${\mathbf{data}\;\Conid{TmVar}\;\Varid{t}\mathrel{=}\Conid{TmVar}\;\Conid{String}\;\mathbf{deriving}\;(\Conid{Show},\Conid{Eq})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmVar}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmVar}\;\Varid{x})\mathrel{=}(\Conid{TmVar}\;\Varid{x})}$\\
${}$\\
${\mathbf{data}\;\Conid{TmFn}\;\Varid{t}\mathrel{=}\Conid{TmLambda}\;\Conid{String}\;\Conid{TyLang}\;\Varid{t}}$\\
${\phantom{\mathbf{data}\;\Conid{TmFn}\;\Varid{t}\mbox{}}\mid \Conid{TmApp}\;\Varid{t}\;\Varid{t}}$\\
${\phantom{\mathbf{data}\;\Conid{TmFn}\;\Varid{t}\mbox{}}\phantom{\mid \mbox{}}\mathbf{deriving}\;(\Conid{Eq})}$\\
${}$\\
${\mathbf{instance}\;\Conid{Functor}\;\Conid{TmFn}\;\mathbf{where}}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;(\Varid{f}\;\Varid{te}))}$\\
${\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}(\Conid{TmApp}\;(\Varid{f}\;\Varid{te1})\;(\Varid{f}\;\Varid{te2}))}$\\
${}$\\
${\mathbf{type}\;\Conid{TmLangSum}\mathrel{=}(\Conid{Sum}\;\Conid{TmBool}}$\\
${\phantom{\mathbf{type}\;\Conid{TmLangSum}\mathrel{=}(\mbox{}}(\Conid{Sum}\;\Conid{TmInt}}$\\
${\phantom{\mathbf{type}\;\Conid{TmLangSum}\mathrel{=}(\mbox{}}\phantom{(\mbox{}}(\Conid{Sum}\;\Conid{TmOp}}$\\
${\phantom{\mathbf{type}\;\Conid{TmLangSum}\mathrel{=}(\mbox{}}\phantom{(\mbox{}}\phantom{(\mbox{}}(\Conid{Sum}\;\Conid{TmIf}}$\\
${\phantom{\mathbf{type}\;\Conid{TmLangSum}\mathrel{=}(\mbox{}}\phantom{(\mbox{}}\phantom{(\mbox{}}\phantom{(\mbox{}}(\Conid{Sum}\;\Conid{TmVar}\;\Conid{TmFn})))))}$\\
${}$\\
${\mathbf{type}\;\Conid{TmLang}\mathrel{=}\Conid{Rec}\;\Conid{TmLangSum}}$\\
${}$\\
${}$\\
${\Varid{toTmLang}\mathbin{::}(\Conid{Subsum}\;\Varid{f}\;\Conid{TmLangSum})\Rightarrow \Varid{f}\;\Conid{TmLang}\to \Conid{TmLang}}$\\
${\Varid{toTmLang}\mathrel{=}\Varid{toSum}}$
\end{tabbing}
\section{Environment}

This very simple module defines a standard environment parameterized
over a stored type.  It is used to define both \ensuremath{\Varid{\Gamma}} for the type
checking routine and the environment for the evaluation routine.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\mathbf{module}\;\Conid{TypedLambdaEnv}\;\mathbf{where}}$\\
${}$\\
${\mathbf{type}\;\Conid{Environment}\;\Varid{a}\mathrel{=}[\mskip1.5mu (\Conid{String},\Varid{a})\mskip1.5mu]}$\\
${}$\\
${\Varid{lookupEnv}\mathbin{::}(\Conid{Eq}\;\Varid{a})\Rightarrow \Conid{String}\to (\Conid{Environment}\;\Varid{a})\to (\Conid{Maybe}\;\Varid{a})}$\\
${\Varid{lookupEnv}\;\Varid{s}\;\Varid{e}\mathrel{=}\Varid{lookup}\;\Varid{s}\;\Varid{e}}$
\end{tabbing}
\section{Type Checking}

\subsection{Type Values}

These are the type values available in our language.  For the type
language, this will serve as the carrier set or value space for both
the type langauge and the term language under type checking.  \ensuremath{\Varid{\phi}}
for the type language is defined over \ensuremath{\Varid{Ty}_{\mathcal D}\;\Varid{a}} while \ensuremath{\Varid{\phi}} for the term
language type checker is defined over \ensuremath{\Varid{T}_{n-1}\;\Varid{a}}.  In effect, \ensuremath{\Varid{\phi}}
evaluates the term language to a type value rather than a term value.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\mathbf{module}\;\Conid{TypedLambdaTypesT}\;\mathbf{where}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{LangUtils}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaAST}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaEnv}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{Monad}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{\Conid{Control}.\Conid{Monad}.Error}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{\Conid{Control}.\Conid{Monad}.Reader}}$
\end{tabbing}
Note that values are not interpreted, so no \ensuremath{\Conid{Algebra}} is needed.
Technically, we could make \ensuremath{\Varid{\phi}\mathrel{=}\Varid{id}} for values, but it's not
necessary to think about this right now.

\subsubsection{Boolean and Integer Type Value}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{data}\;\Conid{TyBaseVal}\;\Varid{ty}\mathrel{=}\Conid{TyBoolVal}\mid \Conid{TyIntVal}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Functor}\;\Conid{TyBaseVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TyBoolVal}\mathrel{=}\Conid{TyBoolVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TyIntVal}\mathrel{=}\Conid{TyIntVal}}$
\end{tabbing}
\subsubsection{Abstraction Type Value}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{data}\;\Conid{TyAbsVal}\;\Varid{ty}\mathrel{=}\Conid{TyAbsVal}\;\Varid{ty}\;\Varid{ty}\;\mathbf{deriving}\;(\Conid{Eq},\Conid{Show})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Functor}\;\Conid{TyAbsVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Conid{TyAbsVal}\;\Varid{x}\;\Varid{y})\mathrel{=}\Conid{TyAbsVal}\;(\Varid{f}\;\Varid{x})\;(\Varid{f}\;\Varid{y})}$
\end{tabbing}
\subsubsection{Type Value}

The value space sum for types is the sum of the base values (integer
and boolean) and the abstraction value and is called \ensuremath{\Conid{TyValSum}}.  The
set of type values is the fixed point, \ensuremath{\Conid{TyVal}}.  \ensuremath{\Conid{TyVal}} is an
instance of \ensuremath{\Conid{Show}} and \ensuremath{\Conid{Eq}} to allow printing and comparing values.
\ensuremath{\Varid{toTyVal}} injects elements from \ensuremath{\Conid{TyVal}} components into the value
space.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{type}\;\Conid{TyValSum}\mathrel{=}(\Conid{Sum}\;\Conid{TyBaseVal}\;\Conid{TyAbsVal})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;(\Conid{Show}\;(\Varid{f}\;\Varid{a}),\Conid{Show}\;(\Varid{g}\;\Varid{a}))\Rightarrow \Conid{Show}\;(\Conid{Sum}\;\Varid{f}\;\Varid{g}\;\Varid{a})\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;(\Conid{S}\;(\Conid{\Conid{Prelude}.Left}\;\Varid{x}))\mathrel{=}(\text{\tt \char34 (Left~\char34}\plus (\Varid{show}\;\Varid{x})\plus \text{\tt \char34 )\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;(\Conid{S}\;(\Conid{\Conid{Prelude}.Right}\;\Varid{x}))\mathrel{=}(\text{\tt \char34 (Right~\char34}\plus (\Varid{show}\;\Varid{x})\plus \text{\tt \char34 )\char34})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{type}\;\Conid{TyVal}\mathrel{=}\Conid{Rec}\;\Conid{TyValSum}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Show}\;\Conid{TyVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;\Varid{x}\mathrel{=}\Varid{show}\;(\Varid{out}\;\Varid{x})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Eq}\;\Conid{TyVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{x}\equiv \Varid{y}\mathrel{=}(\Varid{unS}\;(\Varid{out}\;\Varid{x}))\equiv (\Varid{unS}\;(\Varid{out}\;\Varid{y}))}$\\
${}$\\
${\hskip1.00em\relax\Varid{toTyVal}\mathbin{::}(\Conid{Subsum}\;\Varid{f}\;\Conid{TyValSum})\Rightarrow \Varid{f}\;\Conid{TyVal}\to \Conid{TyVal}}$\\
${\hskip1.00em\relax\Varid{toTyVal}\mathrel{=}\Varid{toSum}}$
\end{tabbing}
\subsection{The Reader Error Monad}

The monad used for handling the environment and error messages will be
formed by composing a \ensuremath{\Conid{Reader}} with and \ensuremath{\Conid{ErrorMonad}}.  First we define
the error handling aspects, then embed the \ensuremath{\Conid{ErrorMonad}} in a \ensuremath{\Conid{Reader}}
using \ensuremath{\Conid{ReaderT}}.

The \ensuremath{\Conid{Either}} type constructor is already an instance of the
\ensuremath{\Conid{MonadError}} class.  Thus, it is not necessary to define \ensuremath{\Varid{throwError}}
and \ensuremath{\Varid{catchError}} explicitly for the type.  The definitions are
included here for documentation, but are not loaded.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{instance}\;\Conid{MonadError}\;(\Conid{Either}\;\Varid{e})\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{throwError}\mathrel{=}\Conid{Left}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{catchError}\;(\Conid{Left}\;\Varid{e})\;\Varid{handler}\mathrel{=}\Varid{handler}\;\Varid{e}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{catchError}\;\Varid{a}\;\anonymous \mathrel{=}\Varid{a}}$
\end{tabbing}
\ensuremath{\Conid{TyError}} is a simple data type for storing errors. We could simply
store the error string rather than create a type.  However, \ensuremath{\Conid{TyError}}
serves as a placeholder if we want to do fancier things later.
\ensuremath{\Conid{TyError}} is also an instance of the standard \ensuremath{\Conid{Error}}.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{data}\;\Conid{TyError}\mathrel{=}\Conid{Err}\;\Conid{String}\;\mathbf{deriving}\;(\Conid{Show},\Conid{Eq})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Error}\;\Conid{TyError}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{noMsg}\mathrel{=}\Conid{Err}\;\text{\tt \char34 Type~Error\char34}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{strMsg}\;\Varid{s}\mathrel{=}\Conid{Err}\;\Varid{s}}$
\end{tabbing}
\ensuremath{\Varid{\Gamma}} defines the data structure used for a binding list.  It is
simply a list of \ensuremath{(\Conid{String},\Conid{TyVal})} pairs.  Adding a binding appends it
to the front of a binding list and looking up a binding is handled in
the canonical fashion.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{type}\;\Varid{\Gamma}\mathrel{=}\Conid{Environment}\;\Conid{TyVal}}$\\
${}$\\
${\hskip1.00em\relax\Varid{addBinding}\mathbin{::}\Varid{\Gamma}\to (\Conid{String},\Conid{TyVal})\to \Varid{\Gamma}}$\\
${\hskip1.00em\relax\Varid{addBinding}\;\Varid{g}\;\Varid{t}\mathrel{=}(\Varid{t}\mathbin{:}\Varid{g})}$\\
${}$\\
${\hskip1.00em\relax\Varid{lookupGamma}\mathbin{::}\Conid{String}\to \Varid{\Gamma}\to \Conid{Maybe}\;\Conid{TyVal}}$\\
${\hskip1.00em\relax\Varid{lookupGamma}\mathrel{=}\Varid{lookup}}$
\end{tabbing}
\ensuremath{\Conid{TyMonad}} defines the actual monad used by the type checker.  The
signature of \ensuremath{\Conid{TyMonad}} is a bit odd.  It must be a type constructor
and thus must have one argument.  \ensuremath{\Conid{ReaderT}} is applied to a \ensuremath{\Varid{\Gamma}}
and \ensuremath{(\Conid{Either}\;\Conid{TyError})} leaving the last argument to \ensuremath{\Conid{TyError}} as an
argument to \ensuremath{\Conid{TyMonad}}.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{type}\;\Conid{TyMonad}\mathrel{=}\Conid{ReaderT}\;\Varid{\Gamma}\;(\Conid{Either}\;\Conid{TyError})\;\Conid{TyVal}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Subtype}\;\Conid{TyError}\;(\Conid{Either}\;\Conid{TyError}\;\Conid{TyVal})\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\uparrow}\;\Varid{x}\mathrel{=}(\Conid{Left}\;\Varid{x})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{Left}\;\Varid{x})\mathrel{=}\Conid{Just}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{Right}\;\Varid{x})\mathrel{=}\Conid{Nothing}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Subtype}\;\Conid{TyVal}\;(\Conid{Either}\;\Conid{TyError}\;\Conid{TyVal})\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\uparrow}\;\Varid{x}\mathrel{=}(\Conid{Right}\;\Varid{x})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{Right}\;\Varid{x})\mathrel{=}\Conid{Just}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{Left}\;\Varid{x})\mathrel{=}\Conid{Nothing}}$
\end{tabbing}
\subsection{Type Language}

The type language defines the language for types over the type values.
The type language will be \ensuremath{\Varid{f}} and defined over the type value space
serving as \ensuremath{\Varid{a}} in an algebra definition.

\subsubsection{Base Types}

The Base Types represent integer and boolean atomic types.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Functor}\;\Conid{TyBase}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TyBool}\mathrel{=}\Conid{TyBool}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;\Conid{TyInt}\mathrel{=}\Conid{TyInt}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TyBase}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TyBool}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyBoolVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TyInt}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyIntVal}}$
\end{tabbing}
\subsubsection{Abstraction Type}

Typically thought of as a function type, the abstraction type
represents a mapping from a range type to a domain type.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Functor}\;\Conid{TyAbs}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{map}_{f}\;\Varid{f}\;(\Varid{x}\mathbin{:->:}\Varid{y})\mathrel{=}(\Varid{f}\;\Varid{x})\mathbin{:->:}(\Varid{f}\;\Varid{y})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TyAbs}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Varid{x}\mathbin{:->:}\Varid{y})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{x'}\leftarrow \Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Varid{x}\mathbin{:->:}\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{y'}\leftarrow \Varid{y}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Varid{x}\mathbin{:->:}\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;(\Conid{TyAbsVal}\;\Varid{x'}\;\Varid{y'})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Varid{x}\mathbin{:->:}\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$
\end{tabbing}
Define a utility function for converting a type term into the type
language.  The \ensuremath{\Varid{evalTy}} function is a separate function for evaluating
elements of the type language.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\Varid{toTyLang}\mathbin{::}(\Conid{Subsum}\;\Varid{f}\;\Conid{TyLangSum})\Rightarrow \Varid{f}\;\Conid{TyLang}\to \Conid{TyLang}}$\\
${\hskip1.00em\relax\Varid{toTyLang}\mathrel{=}\Varid{toSum}}$\\
${}$\\
${\hskip1.00em\relax\Varid{evalTy}\mathbin{::}\Conid{TyLang}\to \Conid{TyMonad}}$\\
${\hskip1.00em\relax\Varid{evalTy}\mathrel{=}\Varid{cata}}$
\end{tabbing}
\subsection{Type Checking Functions}

The type checking functions are defined by defining an algebra from
\ensuremath{\Conid{TmLang}} to \ensuremath{\Conid{TyMonad}}.  Thus, \ensuremath{\Conid{TyMonad}} is the carrier set for the
\ensuremath{\Conid{TmLang}} algebra and \ensuremath{\Varid{\phi}} defines the evaluation function.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmBool}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TmTrue}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyBoolVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TmFalse}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyBoolVal}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmInt}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmConstInt}\;\Varid{x})\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyIntVal}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmOp}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{x'}\leftarrow \Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{y'}\leftarrow \Varid{y}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\mathbf{if}\;(\Varid{x'}\equiv (\Varid{toTyVal}\;\Conid{TyIntVal})\mathrel{\wedge}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mathbf{if}\;(\mbox{}}\Varid{y'}\equiv (\Varid{toTyVal}\;\Conid{TyIntVal}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{then}\;\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyIntVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{else}\;\Varid{throwError}\mathbin{\$}\Conid{Err}\;\text{\tt \char34 Argument~to~Add~not~Integer\char34}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{x'}\leftarrow \Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{y'}\leftarrow \Varid{y}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}};\mathbf{if}\;(\Varid{x'}\equiv (\Varid{toTyVal}\;\Conid{TyIntVal})\mathrel{\wedge}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mathbf{if}\;(\mbox{}}\Varid{y'}\equiv (\Varid{toTyVal}\;\Conid{TyIntVal}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{then}\;\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;\Conid{TyIntVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{else}\;\Varid{throwError}\mathbin{\$}\Conid{Err}\;\text{\tt \char34 Argument~to~Sub~not~Integer\char34}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmIf}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{c'}\leftarrow \Varid{c}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{t'}\leftarrow \Varid{t}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{e'}\leftarrow \Varid{e}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}};\mathbf{if}\;(\Varid{c'}\equiv (\Varid{toTyVal}\;\Conid{TyBoolVal})\mathrel{\wedge}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mathbf{if}\;(\mbox{}}\Varid{t'}\equiv \Varid{e'})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{then}\;\Varid{return}\mathbin{\$}\Varid{\uparrow}\;\Varid{t'}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\mathbf{else}\;\Varid{throwError}\mathbin{\$}\Conid{Err}\;\text{\tt \char34 Either~condition~is~not~boolean~or~then~and~else~are~not~of~same~type~in~If\char34}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{If}\;\Varid{c}\;\Varid{t}\;\Varid{e})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmVar}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmVar}\;\Varid{s})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{val}\leftarrow \Varid{asks}\;(\Varid{lookupGamma}\;\Varid{s})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{s})\mathrel{=}\mathbf{do}\;\mbox{}};\mathbf{case}\;\Varid{val}\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{s})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Just}\;\Varid{x}\to \Varid{return}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{s})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Nothing}\to \Varid{throwError}\mathbin{\$}\Conid{Err}\;(\text{\tt \char34 Variable~\char34}\plus (\Varid{s}\plus \text{\tt \char34 ~not~found\char34}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{s})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmFn}\;\Conid{TyMonad}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{\gamma}\leftarrow \Varid{ask}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{ty'}\leftarrow \Varid{evalTy}\;\Varid{ty}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{te'}\leftarrow \Varid{local}\;(\Varid{const}\;(\Varid{addBinding}\;\Varid{\gamma}\;(\Varid{s},\Varid{ty'})))\;\Varid{te}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}\Varid{toTyVal}\;(\Conid{TyAbsVal}\;\Varid{ty'}\;\Varid{te'})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{te1'}\leftarrow \Varid{te1}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{te2'}\leftarrow \Varid{te2}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}\mathbf{do}\;\mbox{}};\Varid{checkLambda}\;(\Varid{out}\;\Varid{te1'})\;\Varid{te2'}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\Varid{checkLambda}\;\Varid{l}\;\Varid{te2}\mathrel{=}\mathbf{case}\;(\Varid{\downarrow}_{S}\;\Varid{l})\;\mathbf{of}}$\\
${\hskip1.00em\relax\phantom{\Varid{checkLambda}\;\Varid{l}\;\Varid{te2}\mathrel{=}\mbox{}}(\Conid{Just}\;(\Conid{TyAbsVal}\;\Varid{tty}\;\Varid{tte}))\to \mathbf{if}\;\Varid{tty}\equiv \Varid{te2}}$\\
${\hskip1.00em\relax\phantom{\Varid{checkLambda}\;\Varid{l}\;\Varid{te2}\mathrel{=}\mbox{}}\hskip11.00em\relax\mathbf{then}\;\Varid{return}\mathbin{\$}\Varid{\uparrow}\;\Varid{tte}}$\\
${\hskip1.00em\relax\phantom{\Varid{checkLambda}\;\Varid{l}\;\Varid{te2}\mathrel{=}\mbox{}}\hskip11.00em\relax\mathbf{else}\;\Varid{throwError}\mathbin{\$}\Conid{Err}\;\text{\tt \char34 Actual~parameter~type~does~not~match~formal~parameter~type\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{checkLambda}\;\Varid{l}\;\Varid{te2}\mathrel{=}\mbox{}}\anonymous \to \Varid{throwError}\mathbin{\$}\Conid{Err}\;\text{\tt \char34 First~argument~to~application~must~be~a~Lambda\char34}}$
\end{tabbing}
The basic \ensuremath{\Varid{typeof}_{\mathcal D}} function is a catamorphism over the \ensuremath{\Conid{TmLang}\;\Conid{TyMonad}}.  The signature is specified to explicitly identify types.
The \ensuremath{\Varid{runTypeof}} function is a utilty function that evaluates the
\ensuremath{\Conid{Reader}} monad.  The initial environment is empty because there are no
predefined symbols in our language.  \ensuremath{\Varid{runTypeof}} should be used to
integrate the type checker with other language elements.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\Varid{typeof}_{\mathcal D}\mathbin{::}\Conid{TmLang}\to \Conid{TyMonad}}$\\
${\hskip1.00em\relax\Varid{typeof}_{\mathcal D}\mathrel{=}\Varid{cata}}$\\
${}$\\
${\hskip1.00em\relax\Varid{runTypeof}\;\Varid{t}\mathrel{=}(\Varid{runReaderT}\;(\Varid{typeof}_{\mathcal D}\;\Varid{t})\;[\mskip1.5mu \mskip1.5mu])}$
\end{tabbing}
\section{Evaluation}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{module}\;\Conid{TypedLambdaEval}\;\mathbf{where}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{LangUtils}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaEnv}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaAST}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{\Conid{Control}.\Conid{Monad}.Reader}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{\Conid{Control}.\Conid{Monad}.Error}}$
\end{tabbing}
\subsection{Value Representation}

There are three values associated with the Lambda language that all
interpretable functions must converge to - booleans, integers, and
lambda values.  Together, these are specified in the \ensuremath{\Conid{TmVal}}
constructed type.  Note that this type is recursive, unlike the term
language and type language specifications.  The \texttt{Haskell} types
used to represent primitive values are defined to be subtypes of the
aggregate \texttt{TmVal} type.  Thus, \ensuremath{\Varid{\downarrow}} and \ensuremath{\Varid{\uparrow}} are define
between types.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{data}\;\Conid{TmVal}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\mathrel{=}\Conid{TmBoolVal}\;\Conid{Bool}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\mid \Conid{TmIntVal}\;\Conid{Int}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\mid \Conid{LambdaVal}\;(\Conid{TmValEnv}\to \Conid{TmValEnv})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Show}\;\Conid{TmVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;(\Conid{TmBoolVal}\;\Varid{x})\mathrel{=}\Varid{show}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;(\Conid{TmIntVal}\;\Varid{x})\mathrel{=}\Varid{show}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{show}\;(\Conid{LambdaVal}\;\Varid{x})\mathrel{=}\text{\tt \char34 <Lambda~Value>\char34}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Subtype}\;\Conid{Bool}\;\Conid{TmVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\uparrow}\;\Varid{x}\mathrel{=}(\Conid{TmBoolVal}\;\Varid{x})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmBoolVal}\;\Varid{x})\mathrel{=}\Conid{Just}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmIntVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{LambdaVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Subtype}\;\Conid{Int}\;\Conid{TmVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\uparrow}\;\Varid{x}\mathrel{=}(\Conid{TmIntVal}\;\Varid{x})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmBoolVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmIntVal}\;\Varid{x})\mathrel{=}\Conid{Just}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{LambdaVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Subtype}\;(\Conid{TmValEnv}\to \Conid{TmValEnv})\;\Conid{TmVal}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\uparrow}\;\Varid{x}\mathrel{=}(\Conid{LambdaVal}\;\Varid{x})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmBoolVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{TmIntVal}\;\anonymous )\mathrel{=}\Conid{Nothing}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\downarrow}\;(\Conid{LambdaVal}\;\Varid{x})\mathrel{=}\Conid{Just}\;\Varid{x}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{type}\;\Conid{Env}\mathrel{=}\Conid{Environment}\;\Conid{TmVal}}$
\end{tabbing}
\subsection{The Evaluator Monad}

The monad used to support evaluation is a composition of the
\ensuremath{\Conid{ErrorMonad}} and the \ensuremath{\Conid{Reader}} monad with the \ensuremath{\Conid{ErrorMondad}}
encapsulated by the \ensuremath{\Conid{Reader}}.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{data}\;\Conid{TmError}\mathrel{=}\Conid{Err}\;\Conid{String}\;\mathbf{deriving}\;(\Conid{Show},\Conid{Eq})}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Error}\;\Conid{TmError}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{noMsg}\mathrel{=}\Conid{Err}\;\text{\tt \char34 Type~Error\char34}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{strMsg}\;\Varid{s}\mathrel{=}\Conid{Err}\;\Varid{s}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{type}\;\Conid{TmValEnv}\mathrel{=}\Conid{ReaderT}\;\Conid{Env}\;(\Conid{Either}\;\Conid{TmError})\;\Conid{TmVal}}$
\end{tabbing}
\subsection{Expressions as Algebras}

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmBool}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TmTrue}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\;\Conid{True}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;\Conid{TmFalse}\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\;\Conid{False}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmInt}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmConstInt}\;\Varid{x})\mathrel{=}\Varid{return}\mathbin{\$}\Varid{\uparrow}\;\Varid{x}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmOp}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmAdd}\;\Varid{x}\;\Varid{y})\mathrel{=}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\mathbf{do}\;\{\mskip1.5mu \Varid{x'}\leftarrow \Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\Varid{y'}\leftarrow \Varid{y}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\mathbf{case}\;(\Varid{\downarrow}\;\Varid{x'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Just}\;\Varid{x''}\to \mathbf{case}\;(\Varid{\downarrow}\;\Varid{y'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\phantom{\Conid{Just}\;\Varid{x''}\to \mbox{}}\Conid{Just}\;\Varid{y''}\to \Varid{return}\mathbin{\$}\Varid{\uparrow}\;((\Varid{x''}\mathbin{::}\Conid{Int})\mathbin{+}(\Varid{y''}\mathbin{::}\Conid{Int}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\phantom{\Conid{Just}\;\Varid{x''}\to \mbox{}}\Conid{Nothing}\to \Varid{error}\;((\Varid{show}\;\Varid{y'})\plus \text{\tt \char34 ~not~an~integer\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Nothing}\to \Varid{error}\;((\Varid{show}\;\Varid{x'})\plus \text{\tt \char34 ~not~an~integer\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmSub}\;\Varid{x}\;\Varid{y})\mathrel{=}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\mathbf{do}\;\{\mskip1.5mu \Varid{x'}\leftarrow \Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\Varid{y'}\leftarrow \Varid{y}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\mathbf{case}\;(\Varid{\downarrow}\;\Varid{x'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Just}\;\Varid{x''}\to \mathbf{case}\;(\Varid{\downarrow}\;\Varid{y'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\hskip5.50em\relax\Conid{Just}\;\Varid{y''}\to \Varid{return}\mathbin{\$}\Varid{\uparrow}\;((\Varid{x''}\mathbin{::}\Conid{Int})\mathbin{-}(\Varid{y''}\mathbin{::}\Conid{Int}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\hskip5.50em\relax\Conid{Nothing}\to \Varid{error}\;((\Varid{show}\;\Varid{y'})\plus \text{\tt \char34 ~not~an~integer\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Nothing}\to \Varid{error}\;((\Varid{show}\;\Varid{x'})\plus \text{\tt \char34 ~not~an~integer\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmIf}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{If}\;\Varid{b}\;\Varid{t}\;\Varid{e})\mathrel{=}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\mathbf{do}\;\{\mskip1.5mu \Varid{b'}\leftarrow \Varid{b}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\mathbf{case}\;(\Varid{\downarrow}\;\Varid{b'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Just}\;\Varid{b''}\to \mathbf{if}\;\Varid{b''}\;\mathbf{then}\;\Varid{t}\;\mathbf{else}\;\Varid{e}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Nothing}\to \Varid{error}\;((\Varid{show}\;\Varid{b'})\plus \text{\tt \char34 ~is~not~boolean\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmVar}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmVar}\;\Varid{v})\mathrel{=}\mathbf{do}\;\{\mskip1.5mu \Varid{val}\leftarrow \Varid{asks}\;(\Varid{lookup}\;\Varid{v})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{v})\mathrel{=}\mathbf{do}\;\mbox{}};\mathbf{case}\;\Varid{val}\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{v})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Just}\;\Varid{x}\to \Varid{return}\;\Varid{x}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{v})\mathrel{=}\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Conid{Nothing}\to \Varid{error}\;(\text{\tt \char34 Variable~\char34}\plus (\Varid{v}\plus \text{\tt \char34 ~not~found\char34}))}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;(\Conid{TmVar}\;\Varid{v})\mathrel{=}\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{instance}\;\Conid{Algebra}\;\Conid{TmFn}\;\Conid{TmValEnv}\;\mathbf{where}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmLambda}\;\Varid{s}\;\Varid{ty}\;\Varid{te})\mathrel{=}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\mathbf{do}\;\{\mskip1.5mu \Varid{env}\leftarrow \Varid{ask}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}(\lambda \Varid{v}\to \mathbf{do}\;\{\mskip1.5mu \Varid{v'}\leftarrow \Varid{v}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}(\lambda \Varid{v}\to \mathbf{do}\;\mbox{}};\Varid{local}\;(\Varid{const}\;((\Varid{s},\Varid{v'})\mathbin{:}\Varid{env}))\;\Varid{te}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\Varid{return}\mathbin{\$}\Varid{\uparrow}\mathbin{\$}(\lambda \Varid{v}\to \mathbf{do}\;\mbox{}}\mskip1.5mu\})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\Varid{\phi}\;(\Conid{TmApp}\;\Varid{te1}\;\Varid{te2})\mathrel{=}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\mathbf{do}\;\{\mskip1.5mu \Varid{te1'}\leftarrow \Varid{te1}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}};\mathbf{case}\;(\Varid{\downarrow}\;\Varid{te1'})\;\mathbf{of}}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}(\Conid{Just}\;(\Conid{LambdaVal}\;\Varid{f}))\to (\Varid{f}\;\Varid{te2})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\phantom{;\mbox{}}\Varid{a}\to \Varid{error}\;((\Varid{show}\;\Varid{a})\plus \text{\tt \char34 ~is~not~a~lambda~value\char34})}$\\
${\hskip1.00em\relax\hskip2.00em\relax\phantom{\Varid{\phi}\;\mbox{}}\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$
\end{tabbing}
The \ensuremath{\Varid{eval}_{\mathcal D}} function generates a monad from a term language element.
The monad is an \ensuremath{\Conid{ErrorMonad}} composed with a \ensuremath{\Conid{Reader}} monad, thus the
result of applying \ensuremath{\Varid{runReader}} is either a value or an error message.
\ensuremath{\Varid{runEval}} applies \ensuremath{\Varid{runReaderT}} to the \ensuremath{\Conid{Reader}} monad resulting from
\ensuremath{\Varid{eval}_{\mathcal D}} on an environment parameter.  \ensuremath{\Varid{execute}} applies \ensuremath{\Varid{runEval}} with
an empty environment.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\Varid{eval}_{\mathcal D}\mathbin{::}\Conid{TmLang}\to \Conid{TmValEnv}}$\\
${\hskip1.00em\relax\Varid{eval}_{\mathcal D}\mathrel{=}\Varid{cata}}$\\
${}$\\
${\hskip1.00em\relax\Varid{runEval}\;\Varid{t}\;\Varid{e}\mathrel{=}(\Varid{runReaderT}\;(\Varid{eval}_{\mathcal D}\;\Varid{t})\;\Varid{e})}$\\
${}$\\
${\hskip1.00em\relax\Varid{execute}\;\Varid{t}\mathrel{=}\Varid{runEval}\;\Varid{t}\;[\mskip1.5mu \mskip1.5mu]}$
\end{tabbing}
\section{Interpretation}

Here the type checker and the evaluator are put together to form an
interpreter.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\mathbf{module}\;\Conid{TypedLambdaInterpreter}\;\mathbf{where}}$\\
${}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{LangUtils}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaEnv}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaAST}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaEval}}$\\
${\hskip1.00em\relax\mathbf{import}\;\Conid{TypedLambdaTypesT}}$
\end{tabbing}
The \ensuremath{\Varid{interpret}} function is primarily a command line, testing
function.  It accepts a term and generates an \ensuremath{\Conid{IO}} monad representing
either the error message or value generated by the evaluator.  Most of
the work here is simply getting the output in a reasonably well
formatted form.

\begin{tabbing}
\qquad\=\hspace{\lwidth}\=\hspace{\cwidth}\=\+\kill
${\hskip1.00em\relax\Varid{interpret}\mathbin{::}\Conid{TmLang}\to \Conid{IO}\;()}$\\
${\hskip1.00em\relax\Varid{interpret}\;\Varid{t}\mathrel{=}\mathbf{case}\;(\Varid{runTypeof}\;\Varid{t})\;\mathbf{of}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}(\Conid{Left}\;(\Conid{\Conid{TypedLambdaTypesT}.Err}\;\Varid{y}))\to }$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip2.00em\relax\mathbf{do}\;\{\mskip1.5mu \Varid{putStr}\;\text{\tt \char34 Type~Error:~\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;\Varid{y};\Varid{putStr}\;\text{\tt \char34 \char92 n\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}(\Conid{Right}\;\Varid{y})\to \mathbf{case}\;(\Varid{runEval}\;\Varid{t}\;[\mskip1.5mu \mskip1.5mu])\;\mathbf{of}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax(\Conid{Left}\;(\Conid{\Conid{TypedLambdaEval}.Err}\;\Varid{z}))\to }$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\mathbf{do}\;\{\mskip1.5mu \Varid{putStr}\;\text{\tt \char34 Runtime~Error:~\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;(\Varid{show}\;\Varid{z})}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;\text{\tt \char34 \char92 n\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax(\Conid{Right}\;\Varid{z})\to }$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\mathbf{do}\;\{\mskip1.5mu \Varid{putStr}\;\text{\tt \char34 Value:~\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;(\Varid{show}\;\Varid{z})}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;\text{\tt \char34 ::~\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;(\Varid{show}\;\Varid{y})}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}};\Varid{putStr}\;\text{\tt \char34 \char92 n\char34}}$\\
${\hskip1.00em\relax\phantom{\Varid{interpret}\;\Varid{t}\mathrel{=}\mbox{}}\hskip6.00em\relax\hskip2.00em\relax\phantom{\mathbf{do}\;\mbox{}}\mskip1.5mu\}}$
\end{tabbing}
\bibliography{prog-langs}

\end{document}


\chapter{Summary and Conclusions}\label{c:conclude}

The \numbers\ program is a shell program which reads and stores data
from a finite element model described in the \exo\ database
format~\cite{EXODUS}. Within this shell program are several utility
routines which calculate information about the finite element model.
The utilities currently implemented in \numbers\ allow the analyst to
determine:
\begin{itemize}
\item the volume and coordinate limits of each of the materials in the model;
\item the mass properties of the model;
\item the minimum, maximum, and average element volumes for each material;
\item the volume and change in volume of a cavity;
\item the nodes or elements that are within a specified distance from a
user-defined point, line, or plane
\item an estimate of the explicit central-difference timestep for each
material;
\item the validity of contact surfaces or slidelines, that is, whether
two surfaces overlap at any point; and
\item the distance between two surfaces.
\end{itemize}

Since it is relatively easy to add a new utility to \numbers, its
capabilities should increase in the future.  Utilities that may be added
in the future include the calculation of element distortion parameters
which would be useful for validating automatically generated finite
element discretizations, determination of the surface area of side sets,
and additional verification of contact surfaces. Although \numbers\ does
not currently read any of the variables, except for the displacements,
from an \exo\ file, the code is structured such that this capability
could be easily added if needed.


\chapter{Results}\label{chap5}
In this section we present the preliminary results of the performance of the $\tauh$ identification algorithm using $\int\mathcal{L} dt=139.2$ fb$^{-1}$ of data recorded between 2015 and 2018.
The correction factors are applied to simulation in order to match the efficiency observed in data. These correction factors are defined as the ratio between the efficiency measured in data and in simulation.
\begin{equation}
C_{\text{ID}}=\frac{\mathcal{E}_{\text{MC}}}{\mathcal{E}_{\text{Data}}}.
\end{equation}
For this work, we present a preview of the value of the correction factors for \textit{Tight} ID working point for $\tauh$ candidates with $\pt$ above 45 GeV. Since this report is about a work in progress and we have not studied yet the effect of the systematic uncertainties on our results, we will take another approach on estimating the correction factors.

\section{Systematic uncertainties}
The values for the systematic uncertainties used to report the value of the correction factors are presented in Table \ref{Tab5}. These numbers have been provided by Terry Wyatt and Sam Dysch, based on their previous experience working on analysis that make use of $Z\to\tauh l$ events.
\begin{table}[htbp]
	\centering
	\begin{tabular}{cc}
		\hline
		\multicolumn{1}{|c|}{Source}        & \multicolumn{1}{c|}{Sys. Uncertainty (\%)} \\ \hline
		Electron ID efficiency              & 0.8                                        \\
		Muon ID Efficiency                  & 0.2                                        \\
		Electron $\pt$ scale and resolution & 0.4                                        \\
		Muon $\pt$ scale and resolution     & 0.3                                        \\
		Tau $\pt$ scale                     & 1.9                                        \\
		Electron trigger efficiency         & 0.1                                        \\
		Muon trigger efficiency             & 0.4                                        \\
		Integrated luminosity               & 1.7                                        \\ \hline
	\end{tabular}
	\caption{Systematic uncertainties used in this study.}
	\label{Tab5}
\end{table}
\section{$\mu\tau$ Final state}
We define the simulation correction factor as
\begin{equation}
	C_{\text{Tight-ID}}=\frac{N_{\text{MC}}}{N_{\text{Data}}}.
\end{equation}
Where $N_{\text{Data}}$ are the total number of events measured in data substracting all the contributions from fake $\tauh$ estimated in the MC and  $N_{\text{MC}}$ are the number of events in simulation that have a truth matched $\tauh$. The number of events that gets selected after all the cuts and without the $\pt(\tauh)>45$ GeV requirement are shown in Table \ref{Table6}.
\begin{table}[htbp]
	\centering
	\resizebox{\columnwidth}{!}{%
	\begin{tabular}{ccc}
		\hline
		\multicolumn{1}{c|}{Samples} & \multicolumn{1}{c|}{Before $\pt(\tauh)$\textgreater{}45 GeV cut} & After all cuts      \\ \hline
		Z$\to\tau\tau$               & 34224.49$\pm$420.83(stat)$\pm$581.81(lumi)$\pm$675.88(sys)                                              & 11899.10$\pm$182.66(stat)$\pm$202.28(lumi)$\pm$234.99(sys) \\
		Z+jets                       & 543.21$\pm$11.06(stat)$\pm$9.23(lumi)$\pm$10.73(sys)                                                 & 98.92$\pm$4.69(stat)$\pm$1.68(lumi)$\pm$1.95(sys)      \\
		W+jets                       & 586.70$\pm$77.99(stat)$\pm$9.97(lumi)$\pm$11.59(sys)                                                 & 37.11$\pm$18.75(stat)$\pm$0.63(lumi)$\pm$0.73(sys)     \\
		ttbar                        & 339.94$\pm$6.84(stat)$\pm$5.78(lumi)$\pm$6.70(sys)                                                  & 88.44$\pm$3.42(stat)$\pm$1.50(lumi)$\pm$1.75(sys)      \\
		Diboson                      & 470.69$\pm$4.72(stat)$\pm$8.00(lumi)$\pm$9.30(sys)                                                  & 187.07$\pm$2.82(stat)$\pm$3.18(lumi)$\pm$3.69(sys)     \\
		Single top                   & 63.03$\pm$3.07(stat)$\pm$1.07(lumi)$\pm$1.24(sys)                                                   & 14.35$\pm$1.50(stat)$\pm$0.24(lumi)$\pm$0.28(sys)      \\
		MJ                           & 2386.06$\pm$143.76(stat)$\pm$40.56(lumi)$\pm$47.12(sys)                                               & 231.43$\pm$75.25(stat)$\pm$3.93(lumi)$\pm$4.57(sys)    \\ \hline
		\multicolumn{1}{c|}{MC Total}   & \multicolumn{1}{c|}{38634.12$\pm$668.27(stat)$\pm$656.78(lumi)$\pm$762.96(sys)}                         & 12556.42$\pm$289.09(stat)$\pm$213.46(lumi)$\pm$247.97(sys) \\ \hline
		\multicolumn{1}{c|}{Data}    & \multicolumn{1}{c|}{39101$\pm$198(stat)}                               & 12692$\pm$112(stat)      \\ \hline
	\end{tabular}
	}
	\caption{Number of selected events in data and each of the simulation samples in the $Z\to\tauh\mu$. Statistical and systematic uncertainties are presented separately. Luminosity uncertainty is presented apart. Z+jets stands for the combination of $Z\to ee$ and $Z\to\mu\mu$ samples.}
	\label{Table6}
\end{table}
The value obtained for $C_{\text{Tight-ID}}$ in the final state that contains one muon and a $\tauh$ candidate is
\begin{equation}
C_{\text{Tight-ID}}=0.989\pm 0.012\text{(stat)}\pm 0.017\text{(lumi)}\pm 0.020\text{(sys)}.
\end{equation}
All the distributions of the relevant cuts for selecting our signal events after applying all the other cuts are shown in Fig.\ref{Fig17} (Appendix A).
\section{$e\tau$ Final state}
For the final state that contains one electron and a $\tauh$ candidate, the number of events that gets selected after all the cuts and without the $\pt(\tauh)>45$ GeV requirement are shown in Table \ref{Table8}.
\begin{table}[htbp]
	\centering
	\resizebox{\columnwidth}{!}{%
	\begin{tabular}{ccc}
		\hline
		\multicolumn{1}{c|}{Samples} & \multicolumn{1}{c|}{Before $\pt(\tauh)$\textgreater{}45 GeV cut} & After all cuts     \\ \hline
		Z$\to\tau\tau$               & 25269.30$\pm$362.92(stat)$\pm$429.58(lumi)$\pm$531.26(sys)                                              & 8787.97$\pm$150.77(stat)$\pm$149.39(lumi)$\pm$184.76(sys) \\
		Z+jets                       & 682.15$\pm$12.69(stat)$\pm$11.60(lumi)$\pm$14.34(sys)                                                 & 74.07$\pm$4.23(stat)$\pm$1.26(lumi)$\pm$1.56(sys)     \\
		W+jets                       & 348.28$\pm$56.10(stat)$\pm$5.92(lumi)$\pm$7.32(sys)                                                 & 8.08$\pm$8.08(stat)$\pm$0.14(lumi)$\pm$0.17(sys)      \\
		ttbar                        & 255.55$\pm$6.05(stat)$\pm$4.34(lumi)$\pm$5.37(sys)                                                  & 56.63$\pm$2.86(stat)$\pm$0.96(lumi)$\pm$1.19(sys)     \\
		Diboson                      & 384.25$\pm$4.19(stat)$\pm$6.53(lumi)$\pm$8.08(sys)                                                  & 152.92$\pm$2.58(stat)$\pm$2.60(lumi)$\pm$3.21(sys)    \\
		Single top                   & 47.48$\pm$2.71(stat)$\pm$0.81(lumi)$\pm$1.00(sys)                                                   & 11.29$\pm$1.32(stat)$\pm$0.19(lumi)$\pm$0.24(sys)     \\
		MJ                           & 1530.54$\pm$101.21(stat)$\pm$26.02(lumi)$\pm$32.18(sys)                                               & 229.34$\pm$65.04(stat)$\pm$3.90(lumi)$\pm$4.82(sys)   \\ \hline
		\multicolumn{1}{c|}{Total}   & \multicolumn{1}{c|}{28487.55$\pm$545.87(stat)$\pm$484.29(lumi)$\pm$598.91(sys)}                         & 9320.31$\pm$234.87(stat)$\pm$158.44(lumi)$\pm$195.95(sys) \\ \hline
		\multicolumn{1}{c|}{Data}    & \multicolumn{1}{c|}{29723$\pm$172(stat)}                               & 9559$\pm$98(stat)        \\ \hline
	\end{tabular}
	}
	\caption{Number of selected events in data and each of the simulation samples in the $Z\to\tauh e$. Statistical and systematic uncertainties are presented separately. Luminosity uncertainty is presented apart.}
	\label{Table8}
\end{table}
The value obtained for $C_{\text{Tight-ID}}$ is
\begin{equation}
C_{\text{Tight-ID}}=0.974\pm 0.014\text{(stat)}\pm 0.017\text{(lumi)}\pm 0.020\text{(sys)}.
\end{equation}
All the distributions of the relevant cuts for selecting our signal events after applying all the other cuts are shown in Fig.\ref{Fig18} (Appendix A).
\section{Discussion}
As this study makes use of events highly boosted in the transverse plane the Z$(\pt)$ modelling is very important. All the plots that have been shown in previous sections make use of the Sherpa generator for $Z\to\tauh l$ events. Some distributions related to the Z$(\pt)$ are shown in Fig.\ref{Fig19} for Sherpa. The complete set of plots showing the Z$(\pt)$ distribution for both e-tau and mu-tau final states for the two different type of topologies are shown in Fig.\ref{Fig21} and Fig.\ref{Fig22}. In the first stages of our analysis we used Powheg+Pythia8 to simulate $Z\to\tauh l$ events. Fig.\ref{Fig23} shows the Z$(\pt)$ and $\Delta\phi(\tauh,l)$ for the muon-tau final state for in-between events modelled by Powheg+Pythia8. As it can be seen the tendency in the MC is to underestimate the data for high-Z$(\pt)$ values. These results have been previously reported in ATLAS studies \cite{Aad:2019wmn}. Fig.\ref{Fig20} shows the Z$(\pt)$ modelling made by different generators. This has motivated us to use Sherpa to simulate our signal events.
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig19a}{\includegraphics[width=0.50\textwidth]{figures/Fig19a}}}\hfill
	\subfloat[]{\label{Fig19b}{\includegraphics[width=0.50\textwidth]{figures/Fig19b}}}
	\caption{Distribution of Z$(\pt)$ for in-between events (a) and $\Delta\phi(\tauh,l)$ (b) using Sherpa. All the other cuts have been applied apart from the one being plotted.}
	\label{Fig19}
\end{figure}
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig23a}{\includegraphics[width=0.50\textwidth]{figures/Fig23a}}}\hfill
	\subfloat[]{\label{Fig23b}{\includegraphics[width=0.50\textwidth]{figures/Fig23b}}}
	\caption{Distribution of Z$(\pt)$ for in-between events (a) and $\Delta\phi(\tauh,l)$ (b) using Powheg+Pythia8. All the other cuts have been applied apart from the one being plotted.}
	\label{Fig23}
\end{figure}
\begin{figure}[htbp]
	\centering
	\includegraphics[width=0.5\textwidth]{figures/Fig20}
	\caption{Comparison of the different modelling of the Z$(\pt)$ in Drell-Yan events made by different MC generators. As it can be seen Sherpa does a better job describing the Z boson transverse momentum for higher $(\pt)$ values than Powheg+Pythia8. However both generators underestimate the measured value in the high-Z$(\pt)$ region. Taken from \cite{Aad:2019wmn}.}
	\label{Fig20}
\end{figure}

Another challenging task will be to control the MJ background contribution when we go to looser regions in tau-ID. As it can be seen in Fig.\ref{Fig17c} and Fig.\ref{Fig17d} the MJ background contribution starts to be more important for 1-prong and 3-prong taus for looser values of the RNN score. We have explored the possible correlation that could arise in actual $Z\to\tauh l$ events in variables like the ratio between the transverse momentum of the tau and the lepton or the ratio between the Z$(\pt)$ and the leading jet. The distributions for this variables are shown in Fig.\ref{Fig24a} and Fig.\ref{Fig24b} respectively.
\begin{figure}[htbp]
	\centering
	\subfloat[]{\label{Fig24a}{\includegraphics[width=0.50\textwidth]{figures/Fig24a}}}\hfill
	\subfloat[]{\label{Fig24b}{\includegraphics[width=0.50\textwidth]{figures/Fig24b}}}
	\caption{$r_{\frac{\pt(\mu)}{\pt(\tau)}}$ (a) and $r_{\frac{\pt(j)}{\pt(Z)}}$ distributions (b) do not seem to offer a big discriminating power against MJ background events. }
	\label{Fig24}
\end{figure}
These variables though do not offer a discriminating power against the MJ background.


\section{\DYNAMICO is}

\DYNAMICO \footnotemark is a new dynamical core for LMD-Z, the atromspheric GCM part
of IPSL-CM Earth System Model.
%
\DYNAMICO is funded by the Indo-French Centre for the Promotion of
Advanced Research, by IPSL and by the G8 Research Councils Initiative on
Multilateral Research Funding, project ICOMEX.

\footnotetext{
This section is based on the \DYNAMICO Wiki page (\url{http://forge.ipsl.fr/dynamico/wiki})
}

The primary goal of \DYNAMICO is to re-formulate in LMD-Z the horizontal
advection and dynamics on a icosahedral grid, while preserving or
improving their qualities with respect to accuracy, conservation laws
and wave dispersion.
%
A broader goal is to revisit all fundamental features of the dynamical
core, especially the shallow-atmosphere/traditional approximation, the
vertical coordinate and the coupling with physics.
%
Also efficient implementation of present and future supercomputing
architectures is a key issue.

This manual describes the overview of \DYNAMICO and each kernel program
briefly.
%
For the details of \DYNAMICO, see
\cite{gmd-8-3131-2015},
etc.

Kernel programs for \DYNAMICO are taken from \DYNAMICO ver 1.0, r339.
%
Main feature of \DYNAMICO-1.0 are;
\begin{itemize}
 \item hydrostatic, traditional shallow atmosphere,
 \item icosahedral-hexagonal C-grid in horizontal, mass-based Lorentz
       staggering in vertical,
 \item Mimetic finite difference + slope-limited finite volume
       transport, and
 \item explicit Runge-Kutta time stepping.
\end{itemize}

\section{Governing equations}

Basic scheme of \DYNAMICO is the energy/voticity conserving schemes and
the curl (vector-invariant) form.
%
To deliver governing equations, \DYNAMICO adopts the Hamiltonian
formulation of the equations of motion.
%
This Hamiltonian theory has been extended for compressible hydrostatic
flows and for non-Eulerian vertical coordinates \citep{JAS-D-13-0339,MWR-D-14-00069}.
%
Derivation of governing equations is complicated, we skip it here. See
\cite{gmd-8-3131-2015} etc.

\section{Horizontal and vertical grid}

\DYNAMICO adopts the icosahedral-hexagonal C-grid in horizontal and
mass-based Lorentz staggering grid in vertical.
%
\autoref{f:ico_c_grid} shows horizontal and vertical grids.

Scalar variables, such as entropy $\Theta$, are defined on the center of
hexagonal control volume (circle points in the figure), velocities and fluxes,
are defined on the edge (square points), and tracer are
defined on the vertex (triangle points).
%
See \cite{gmd-8-3131-2015} for details.

\begin{figure}
 \centering
 \includegraphics[scale=1]{figs/AIMES_DYNAMICO-08-0.png}
 \caption{Icosahedral C-grid and Lorenz grid.}\label{f:ico_c_grid}
%http://www.lmd.polytechnique.fr/~dubos/Talks/2014DubosAGU.pdf
\end{figure}

\section{Parallelization}

Like \NICAM and \DYNAMICO, icosahedral grids on entire globe can be
separated by 10 ``diamonds'', each are consist of neighboring two
triangles of an icosahedron.
%
Each diamonds can be divided in \src{nsplit_i}$\times$\src{nsplit_j} areas,
and one of divided area,
called ``patch'' in \DYNAMICO, is the basis of domain decomposition.
%
\src{nsplit_i} and \src{nsplit_j} are control parameter and read from
configuration file on execution.
%
One MPI process can handle \src{ndomain} patches, which is decided by
the number of total patchs and the number of MPI processes.

\section{Data structure}\label{s:data_structure}

Basic data structure in \DYNAMICO is a \src{t_field}, as shown in
\autoref{l:t_field}.
%
One instance of \src{t_field} is to access one field within one patch.
%
One MPI process may handle several patches, and one field may be usually
an array of \src{t_field}.
%
Allocation and halo-exchange routines are work on \src{t_field(:)}
variables, and other high-level computational routines work on them, too.
%
See the next section as an example.

\begin{LstF90}[%
caption={\src{t_field} structure},%
label={l:t_field}%
]
  TYPE t_field
    CHARACTER(30)      :: name
    REAL(rstd),POINTER :: rval2d(:)
    REAL(rstd),POINTER :: rval3d(:,:)
    REAL(rstd),POINTER :: rval4d(:,:,:)

    INTEGER,POINTER :: ival2d(:)
    INTEGER,POINTER :: ival3d(:,:)
    INTEGER,POINTER :: ival4d(:,:,:)

    LOGICAL,POINTER :: lval2d(:)
    LOGICAL,POINTER :: lval3d(:,:)
    LOGICAL,POINTER :: lval4d(:,:,:)

    INTEGER :: ndim
    INTEGER :: field_type
    INTEGER :: data_type
    INTEGER :: dim3
    INTEGER :: dim4
  END TYPE t_field
\end{LstF90}

One of members of \src{t_field} are pointer to the array of
\src{REAL(rstd)}, \src{INTEGER} or \src{LOGICAL}, and whose dimension is
one, two or three.
%
If the field is horizontal, such as surface pressure or sea surface
temperature, \src{rval2d} is used.
%
Note that horizontal index $I$ and $J$ are merged to one dimension.

\autoref{f:aimes_dynamico-30-0} shows horizontal indexing in \DYNAMICO.
%
As shown in previous chapter, \DYNAMICO adopts icosahedral grid, and
control volume is hexagonal as usual.
%
One ``patch'' is rhomboid, and can be indexed as two-dimensional, each
size are \src{iim} and \src{jjm}, as shown in left figure of
\autoref{f:aimes_dynamico-30-0}.
%
These can be re-written as usual orthogonal i-j plane, shown in the
right figure of \autoref{f:aimes_dynamico-30-0}.
%
The \src{n} point in the figure is surrounded by
six neighbouring cells, named \src{right}, \src{rup}, etc.
%
So stencil calculation comes from finite difference in horizontal uses
seven points, not five as in usual orthogonal grid.

\begin{figure}[htpb]
\centering
\includegraphics[scale=0.7]{figs/AIMES_DYNAMICO-30-0.png}
\caption{Horizontal indexing}\label{f:aimes_dynamico-30-0}
\end{figure}

The number of the edge point is three times larger than that of the center point.
As shown in \autoref{f:rel_center_edge}, each center point manages three edge points.
These points are named \src{u_right}, \src{u_lup}, and \src{u_ldown}.

\begin{figure}[htpb]
\centering
\includegraphics[scale=0.5]{figs/AIMES_DYNAMICO-30-1.png}
\caption{Relationship of center points and edge points}\label{f:rel_center_edge}
\end{figure}

To allocate one \src{t_field} instance, subroutine \src{allocate_field}
is called.
%
Below is the example of allocating orography named ``phis''.

\begin{LstF90}
 ! Time-independant orography
    CALL allocate_field(f_phis,field_t,type_real,name='phis')
\end{LstF90}

\section{Code structure}

Global program structure of \DYNAMICO is as follows.

In the main program, after the various initialization, time step loop is
carried by a single subroutine \src{timeloop}.
%
\autoref{f:pad_timeloop} shows PAD (Problem Analysis
Diagram)\footnotemark of main processes in subroutine \src{timeloop}.
%
As seen in the top of this PAD,

\footnotetext{See \autoref{s:pad} for reading PAD.}

\begin{figure}[htb]
 \centering
 \includegraphics[scale=.4]{figs/timeloop.pdf}
 \caption{PAD of \src{timeloop}}\label{f:pad_timeloop}
\end{figure}

Main time step loop is described in first \src{it} loop, from step
\src{itau0} to \src{itau0+italmax}.
%
First IF block in this loop is for halo exchange of several fields,
using subroutine \src{send_message} and \src{wait_message}.
%
In the next \src{stage} loop subroutine \src{caldyn}, one of
\src{*_scheme} and \src{advect_tracer} are called sequentially.
%
Subroutine \src{caldyn} calculate dynamical terms such as potential
vorticity, etc.
%
Subroutine \src{*_scheme} is for time-advancing. For example,
\src{rk_scheme} uses Runge-Kutta scheme.
%
This is a default scheme for this kernel package.
%
Subroutine \src{advect_tracer} is to calculate advection of tracer
quantities.
%
Here variable \src{nb_stage} in the loop range is the number of
iteration necessary for each time-advancing scheme. For example,
\src{nb_stage=1} for Euler scheme, \src{nb_stage=4} for Runge-Kutta scheme.
%
Finally, if time step \src{it} is at \src{itau_physics}'th step,
subroutine \src{physics} is called to calculate physics part.

\autoref{l:definition_caldyn} is the definition part of subroutine
\src{caldyn}\footnotemark,
and \autoref{f:pad_caldyn} is the PAD of it.
%
\footnotetext{Here is the version in \file{caldyn_gcm.f90}}
%
Note that all of current four kernel program in this package is taken
from the subroutine called from this \src{caldyn} (See \autoref{s:kernelize}).
%
As mentioned in \autoref{s:data_structure}, all of fields used in this
subroutine is given as pointers of instance of \src{t_field}.

\begin{LstF90}[%
caption={Definition part of \src{caldyn}},%
label={l:definition_caldyn}%
]
  SUBROUTINE caldyn(write_out,f_phis, f_ps, f_mass, f_theta_rhodz, f_u, f_q, &
       f_hflux, f_wflux, f_dps, f_dmass, f_dtheta_rhodz, f_du)
    USE icosa
    USE disvert_mod, ONLY : caldyn_eta, eta_mass
    USE vorticity_mod
    USE kinetic_mod
    USE theta2theta_rhodz_mod
    USE wind_mod
    USE mpipara
    USE trace
    USE omp_para
    USE output_field_mod
    USE checksum_mod
    IMPLICIT NONE
    LOGICAL,INTENT(IN)    :: write_out
    TYPE(t_field),POINTER :: f_phis(:)
    TYPE(t_field),POINTER :: f_ps(:)
    TYPE(t_field),POINTER :: f_mass(:)
    TYPE(t_field),POINTER :: f_theta_rhodz(:)
    TYPE(t_field),POINTER :: f_u(:)
    TYPE(t_field),POINTER :: f_q(:)
    TYPE(t_field),POINTER :: f_hflux(:), f_wflux(:)
    TYPE(t_field),POINTER :: f_dps(:)
    TYPE(t_field),POINTER :: f_dmass(:)
    TYPE(t_field),POINTER :: f_dtheta_rhodz(:)
    TYPE(t_field),POINTER :: f_du(:)

    REAL(rstd),POINTER :: ps(:), dps(:)
    REAL(rstd),POINTER :: mass(:,:), theta_rhodz(:,:), dtheta_rhodz(:,:)
    REAL(rstd),POINTER :: u(:,:), du(:,:), hflux(:,:), wflux(:,:)
    REAL(rstd),POINTER :: qu(:,:)
    REAL(rstd),POINTER :: qv(:,:)

! temporary shared variable
    REAL(rstd),POINTER  :: theta(:,:)
    REAL(rstd),POINTER  :: pk(:,:)
    REAL(rstd),POINTER  :: geopot(:,:)
    REAL(rstd),POINTER  :: convm(:,:)
    REAL(rstd),POINTER  :: wwuu(:,:)

    INTEGER :: ind
    LOGICAL,SAVE :: first=.TRUE.
\end{LstF90}

\begin{figure}[htb]
\centering
\includegraphics[scale=.4]{figs/caldyn.pdf}
\caption{PAD of \src{caldyn}}\label{f:pad_caldyn}%
\end{figure}

In \autoref{f:pad_caldyn} there are some assignment from
\src{t_field} to real pointer, such as \src{ps = f_ps(ind)}.
%
This is defined as module procedure and generic subroutine
\src{get_val} and using interface assignment.
%
All of them are defined in module \src{field_mod} in \file{field.f90}.