%! TEX program = xelatex
%! TEX root = lecture3_slide.tex

\usepackage{listings}
\title{Programming Language Theory}
\subtitle{Primitive Recursion, General Recursion, and Polymorphism}
\begin{document}

{\usebackgroundtemplate{\includegraphics[width=\paperwidth]{image.png}}
\begin{frame}\maketitle\end{frame}}

%\begin{frame}[fragile]{Polymorphism}
%  \emph{Abstraction principle~\cite{Pierce2002}}:
%  \begin{quotation}
%    Each significant piece of functionality in a program should be implemented
%    in just one place in the source code. Where similar functions are carried
%    out by distinct pieces of code, it is generally beneficial to combine them
%    into one by abstracting out the varying parts.
%  \end{quotation}
%
%  In Haskell
%  \begin{semiverbatim}
%    head :: [a] -> a
%    head (x:_) = x
%  \end{semiverbatim}
%\end{frame}

\section{G\"odel's \textbf{T}: Simply typed $\lambda$-calculus with naturals
and primitive recursion}
\begin{frame}{The un defineability of $\lambda_\to$}
  Can you write this in $\lambda_\to$ using Church numerals?
  \begin{align*}
    \mathit{sum}(0)     & = 0 \\
    \mathit{sum}(1 + n) & = (1 + n) + \mathit{sum}(n)
  \end{align*}
  It is not definable in $\lambda_\to$, since fixpoint operator is not allowed any more.

  But, this is definable via \emph{primitive recursion}:
  for some $c$ and function $g$
  \begin{align*}
    \mathit{prec}(0, c, g(x, y))       &= c \\
    \mathit{prec}(1 + n, c, g(x, y))   &= g(n, \mathit{prec}(n, c, g(x, y)))
  \end{align*}

  $\lambda_\to$ with primitive recursion is called G\"odel's \textbf{T}.
\end{frame}

\begin{frame}{\textbf{T}: Types and terms}
  \begin{definition}[Types]
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$B \in \mathbb{V}$}
        \RightLabel{(tvar)}
        \UIC{$B : \type$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\vphantom{B}$}
        \RightLabel{(nat)}
        \UIC{$\mathbb{N} : \type$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\sigma : \type$}
        \AXC{$\tau   : \type$}
        \RightLabel{(fun)}
        \BIC{$\sigma \to \tau : \type$}
      \end{prooftree}
    \end{multicols}
  \end{definition}
  \begin{definition}[Terms]
    Additional term formation rules are added to $\lambda_\to$ as follows.
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\vphantom{M}$}
        \UIC{$\zero : \term_{\textbf{T}}$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$M$}
        \UIC{$\suc\;M : \term_{\textbf{T}}$}
      \end{prooftree}
    \end{multicols}
      \begin{prooftree}
        \AXC{$L : \term_{\textbf{T}}$}
        \AXC{$M : \term_{\textbf{T}}$}
        \AXC{$N : \term_{\textbf{T}}$}
        \AXC{$x \in V$}
        \AXC{$y \in V$}
        \QuinaryInfC{$\mathtt{rec}(M; x.\,y.\,N)\; L : \term_{\textbf{T}}$}
      \end{prooftree}
  \end{definition}
\end{frame}

\begin{frame}{\textbf{T}: Typing rules}
  \begin{definition}
    Additional term typing rules are added to $\lambda_\to$ as follows.
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\vphantom{\Gamma}$}
        \UIC{$\Gamma \vdash \zero : \mathbb{N}$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\Gamma \vdash M : \mathbb{N}$}
        \UIC{$\Gamma \vdash \suc\;M : \mathbb{N}$}
      \end{prooftree}
    \end{multicols}
    \begin{prooftree}
      \AXC{$\Gamma \vdash L : \mathbb{N}$}
      \AXC{$\Gamma \vdash M : \tau$}
      \AXC{$\Gamma, x : \mathbb{N}, y : \tau \vdash N : \tau$}
      \TrinaryInfC{$\Gamma \vdash \mathtt{rec}(M; x.\,y.\,N)\;L : \tau$}
    \end{prooftree}
  \end{definition}
  \begin{itemize}
    \item Substitution for \textbf{T} is defined similarly.
    \item Substitution respects typing judgements, i.e.\
      $\Gamma \vdash N : \tau$ and $\Gamma, x : \tau \vdash M : \sigma$, then
      $\Gamma \vdash M\subst{N}{x} : \sigma$.
  \end{itemize}
\end{frame}

\begin{frame}{\textbf{T}: Dynamics}
  $\beta$-conversion for \textbf{T} is extended with two rules
  \begin{align*}
    \mathtt{rec}(M, x.\,y.\, N)\;\zero & \longrightarrow_\beta M \\
    \mathtt{rec}(M, x.\,y.\, N)\;\suc\;L & \longrightarrow_\beta N\subst{L , \mathtt{rec}(M; x.\,y.\,N)\;L}{x, y}
  \end{align*}
  Similarly, a $\beta$-reduction $\onereduce$ extends $\longrightarrow_\beta$ to all parts of a term and $\reduce$ indicates finitely many $\beta$-reductions.

  \mode<presentation>{\vfill}
  \begin{theorem}
    \textbf{T} enjoys the strong and weak normalisation properties as well as type safety.
  \end{theorem}
\end{frame}

\begin{frame}{Example: Addition and summation}
  $\mathtt{add} : \mathbb{N} \to \mathbb{N} \to \mathbb{N}$ can be defined in \textbf{T} as
  \[
    \lambda n.\,\lambda m.\,\mathtt{rec}\;(m; x.\,y.\, \suc\;y)\;n\;m
  \]

  $\mathtt{sum} : \mathbb{N} \to \mathbb{N}$ can be defined in \textbf{T} as
  \[
    \lambda n.\, \mathtt{rec}\;(\zero; x.\,y.\, \add\;(\suc\;x)\;y)\;n
  \]
\mode<presentation>{\vfill}
  \begin{block}{Exercise}
    Evaluate $\mathtt{sum}\;(\suc\;\zero)$.
  \end{block}
\end{frame}

\section{\PCF --- System of Recursive Functions}

\begin{frame}{\textbf{PCF}: $\lambda_\to$ with naturals and general recursion}
  \textbf{T} does not include all computable functions, since all terms terminate eventually.
  Programming language in reality allows us to do \emph{general recursion}
  including \emph{infinite loops}.

  \textbf{PCF} has the same class of types as \textbf{T}.

  \begin{definition}[Terms]
    Additional term formation rules are added to $\lambda_\to$ as follows.
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\vphantom{M}$}
        \UIC{$\zero : \term_{\PCF}$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$M : \term_{\PCF}$}
        \UIC{$\suc\;M : \term_{\PCF}$}
      \end{prooftree}
    \end{multicols}
      \begin{prooftree}
      \color{red}
        \AXC{$L : \term_{\PCF}$}
        \AXC{$M : \term_{\PCF}$}
        \AXC{$N : \term_{\PCF}$}
        \AXC{$x \in V$}
        \QuaternaryInfC{$\ifz(M; x.\,N)\; L$}
      \end{prooftree}
      \begin{prooftree}
      \color{red}
        \AXC{$M : \term_{\PCF}$}
        \AXC{$x \in V$}
        \BIC{$\fix\,x.\,M : \term_{\PCF}$}
      \end{prooftree}
  \end{definition}
\end{frame}

\begin{frame}{\textbf{PCF}: Typing rules}
  \begin{definition}
    Additional term typing rules are added to $\lambda_\to$ as follows.
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\vphantom{\Gamma}$}
        \UIC{$\Gamma \vdash \zero : \mathbb{N}$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\Gamma \vdash M : \mathbb{N}$}
        \UIC{$\Gamma \vdash \suc\;M : \mathbb{N}$}
      \end{prooftree}
    \end{multicols}
    \begin{prooftree}
      \color{red}
      \AXC{$\Gamma \vdash L : \mathbb{N}$}
      \AXC{$\Gamma \vdash M : \tau$}
      \AXC{$\Gamma, x : \mathbb{N} \vdash N : \tau$}
      \TrinaryInfC{$\Gamma \vdash \ifz(M; x.\,N)\;L : \tau$}
    \end{prooftree}
    \begin{prooftree}
      \color{red}
      \AXC{$\Gamma, x : \tau \vdash M : \tau$}
      \UIC{$\Gamma \vdash \fix\,x.\,M : \tau$}
    \end{prooftree}
  \end{definition}
  \begin{itemize}
    \item Substitution for \textbf{PCF} is defined similarly.
    \item Substitution respects typing judgements, i.e.\
      $\Gamma \vdash N : \tau$ and $\Gamma, x : \tau \vdash M : \sigma$, then
      $\Gamma \vdash M\subst{N}{x} : \sigma$.
  \end{itemize}
\end{frame}

\begin{frame}{\textbf{PCF}: Dynamics}
  $\beta$-conversion for \textbf{PCF} is extended with three rules
  \begin{align*}
    \fix\,x.\,M & \longrightarrow_\beta M\subst{\fix\,x.\,M}{x} \\
    \ifz(M; x.\, N)\;\zero & \longrightarrow_\beta M \\
    \ifz(M; x.\, N)\;(\suc M) & \longrightarrow_\beta N\subst{M}{x}
  \end{align*}
  Similarly, a $\beta$-reduction $\onereduce$ extends $\longrightarrow_\beta$ to all parts of a term and $\reduce$ indicates finitely many $\beta$-reductions.

  \mode<presentation>{\vfill}
  \begin{theorem}
    \textbf{PCF} enjoys type safety.
  \end{theorem}
\end{frame}

\begin{frame}{Example}
  A term which never terminates can be defined easily.
  \begin{align*}
             & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\
    \equiv{} & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\
    \equiv{} & \fix\,x.\,x & \onereduce x\subst{\fix\,x.\,x}{x} \\
    \equiv{} & \dots
  \end{align*}
\end{frame}

\begin{frame}{Example: Predecessor and negation}
  \begin{align*}
    \pred & \defeq \lambda n : \mathbb{N}.\,\ifz(\zero; x.\, x)\;n & : \mathbb{N} \to \mathbb{N} \\
               \mathtt{not} & \defeq \lambda n : \mathbb{N}.\, \ifz(\suc\;\zero; x.\, \zero)\; n & : \mathbb{N} \to \mathbb{N}
  \end{align*}
  \begin{block}{Exercise}
    Evaluate the following terms to their normal forms.
    \begin{enumerate}
      \item $\pred\;\zero$
      \item $\pred\;(\suc\;\suc\;\suc\;\zero)$
      \item $\mathtt{not}\;(\suc\;\suc\;\zero)$
    \end{enumerate}

  \end{block}
\end{frame}

\section{\textbf{F} --- Polymorphic Typed $\lambda$-Calculus}
\begin{frame}{Polymorphic types}
    Given type variables~$\mathbb{V}$, $\tau : \type$ is defined by
    defined by
      \begin{prooftree}
        \AXC{$t \in \mathbb{V}$}
        \RightLabel{(tvar)}
        \UIC{$t : \type$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\sigma : \type$}
        \AXC{$\tau   : \type$}
        \RightLabel{(fun)}
        \BIC{$\sigma \to \tau : \type$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\sigma : \type$}
        \AXC{$t   \in \mathbb{V}$}
        \RightLabel{(poly)}
        \BIC{$\forall t.\, \sigma : \type$}
      \end{prooftree}
  where $t$ may or may not appear in~$\sigma$.

  The polymorphic type $\forall t.\, \sigma$ provides a generic type for every
  instance $\sigma[\tau/t]$ whenever $t$ is instantiated by an actual type $\tau$.

%For example, the type $\forall t.\, t \to t$ corresponds to a type variable
%universally quantified in GHC with extension \texttt{RankNType}
%\begin{semiverbatim}
%  forall a. a -> a
%\end{semiverbatim}
%or simply
%\begin{semiverbatim}
%  a -> a
%\end{semiverbatim}
\end{frame}

\begin{frame}{Examples}

  \begin{itemize}
    \item $\mathtt{id} : \forall t.\, t \to t$

    \item $\mathtt{proj}_1 : \forall t.\,\forall u.\, t \to u \to t$

    \item $\mathtt{proj}_2 : \forall t.\,\forall u.\, t \to u \to u$

    \item $\mathtt{length} : \forall t.\, \List\;t \to \nat$

    \item $\mathtt{singleton} : \forall t. t \to \List(t)$
  \end{itemize}

\end{frame}

\begin{frame}{Free and bound variables, again}
\begin{definition}
  The \emph{free variable} $\FV(\tau)$ of $\tau$ is defined inductively by
  \begin{align*}
    \FV(t) & {} = t \\
    \FV(\sigma \to \tau) & {} = \FV(\sigma) \cup \FV(\tau) \\
    \FV(\forall t.\, \sigma) & {} = \FV(\sigma) - \{t\}
  \end{align*}
  For convenience, the function extends to contexts:
  \[
    \FV(\Gamma) = \set{ t \in \mathbb{V}}{ \exists (x : \sigma) \in \Gamma
      \wedge t \in \FV(\sigma) }.
  \]
\end{definition}
  \begin{enumerate}
    \item $\FV(t_1) = \{t_1\}$.
    \item $\FV(\forall t.\, (t \to t) \to t \to t) = \emptyset$.
    \item $\FV(x : t_1, y : t_2, z : \forall t.\,t)
      = \{t_1, t_2\}$.
  \end{enumerate}
\end{frame}
\begin{frame}{Capture-avoiding substitution for type}
  \begin{definition}
  The \emph{(capture-avoidance) substitution} of a type $\rho$ for the free
  occurrence of a type variable~$t$ is defined by
  \begin{align*}
    t\subst{\rho}{t} & = \rho \\
    u\subst{\rho}{t} & = u && \text{if $u \neq t$}\\
    (\sigma\to\tau)\subst{\rho}{t} & =
    \sigma\subst{\rho}{t} \to
    \tau\subst{\rho}{t} \\
    (\forall t.\sigma)\subst{\rho}{t} & = \forall t.\sigma \\
    (\forall u.\sigma)\subst{\rho}{t} & = \forall u.\sigma\subst{\rho}{t}
                                      &&
    \text{if $u \neq t, u \not\in \FV(\rho)$}
  \end{align*}
  \end{definition}
  Recall that $u \not\in \FV(\rho)$ means that $u$ is \emph{fresh} for $\rho$.
\end{frame}

\begin{frame}{Typed terms}
\begin{definition}
  On top of $\lambda_\to$, \textbf{F} has additional term formation rules as follows.
    \begin{prooftree}
      \AXC{$M : \term_F$}
      \AXC{$t : \mathbb{V}$}
      \RightLabel{(gen)}
      \BIC{$\Lambda\, t.\; M : \term_F$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$M : \term_F$}
      \AXC{$\tau : \type$}
      \RightLabel{(inst)}
      \BIC{$M\;\tau : \term_F$}
    \end{prooftree}
\end{definition}

  \begin{enumerate}
    \item $\Lambda t.\, M$ for type abstraction, or \emph{generalisation}.
    \item $M\;\tau$ for type application, or \emph{instantiation}.
  \end{enumerate}
%\framebreak
%GHC with the extension \texttt{ScopedTypeVariables} allows you to use type
%variables.
%\begin{semiverbatim}
% f :: forall a. [a] -> [a]
%
% f = \\(xs :: [a]) -> reverse xs
%\end{semiverbatim}
%GHC with the extension \texttt{TypeApplications} allows you to apply a type
%argument.
%\begin{semiverbatim}
%  Prelude> :t id
%
%  id :: a -> a
%
%  Prelude> :t id @Int
%
%  id @Int :: Int -> Int
%\end{semiverbatim}
%
%\framebreak
%  Can you see the corresponding untyped/simply typed $\lambda$-terms of the
%  following $\lambda$-terms in System F?
%
%  \begin{enumerate}
%    \item $\Lambda t.\,\lambda (x : t).\, x$
%    \item $\Lambda t.\, \lambda (x : t)(y : t).\, x$
%    \item $(\Lambda t.\, \lambda (x : t)(y : t).\, x)\;\sigma\;M\;N$
%     where  $M, N$ are terms and $\sigma$ a type.
%    \item $\Lambda t.\, \lambda (f : t \to t)(x : t).\, f\;(f\;x)$
%  \end{enumerate}
%System $F$ is more expressive than simply typed lambda calculus.
\end{frame}
\begin{frame}{Example}
  Suppose $\mathtt{length} : \forall t.\,\List\;t \to \nat$.

  Then,
  \begin{enumerate}
    \item $\mathtt{length}\;\nat$
    \item $\mathtt{length}\;\bool$
    \item $\mathtt{length}\;(\nat \to \nat)$
  \end{enumerate}
  are instances of $\mathtt{length}$ with types
  \begin{enumerate}
     \item $\List\; \nat \to \nat$
     \item $\List\;\bool \to \nat$
     \item $\List\;(\nat \to \nat) \to \nat$
  \end{enumerate}
\end{frame}

\begin{frame}{System F: Typing judgement}
  A \emph{type context} is a sequence of type variable
  \[
    t_1, t_2, \dots, t_n
  \]

  \textbf{F} has two kinds of typing judgements.
  \begin{itemize}
    \item $\Delta \vdash \tau$ for $\tau$ for a valid type under the type context $\Delta$
    \item $\Delta; \Gamma \vdash M : \tau$ for a well-typed term under the context $\Gamma$ and the type context~$\Delta$.
  \end{itemize}
  For example,
    \[
      t \vdash t \to t
    \]
    is a judgement that $t \to $ is a valid type under the type context, $t$.

\end{frame}

\begin{frame}{System F: Type formation}
  The justification of $\Delta \vdash \tau$ is constructed inductively by following rules.
  \begin{multicols}{2}
    \begin{prooftree}
      \AXC{$t$ occurs in $\Delta$}
      \UIC{$\Delta \vdash t$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$\Delta \vdash \tau_1$}
      \AXC{$\Delta \vdash \tau_2$}
      \BIC{$\Delta \vdash \tau_1 \to \tau_2$}
    \end{prooftree}
    \columnbreak
    \begin{prooftree}
      \AXC{$\Delta, t \vdash \tau$}
      \UIC{$\Delta \vdash \forall t.\, \tau$}
    \end{prooftree}
  \end{multicols}

  \begin{block}{Exercise}
    Derive the judgement
    \[
        t : \tau \vdash t \to t
    \]
  \end{block}
\end{frame}

\begin{frame}{System F: Typing rules}

  The justification of $\Delta; \Gamma \vdash M : \sigma$ is defined inductively by following rules.
  \begin{multicols}{2}
  \begin{prooftree}
    \AXC{$x : \sigma \in \Gamma$}
    \UIC{$\Delta ; \Gamma \vdash x : \sigma$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Delta; \Gamma \vdash M : \sigma \to \tau$}
    \AXC{$\Delta; \Gamma \vdash N : \sigma$}
    \BIC{$\Delta; \Gamma \vdash M\;N : \tau$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{{\color{red}$\Delta \vdash \sigma$}}
    \AXC{$\Delta; \Gamma, x : \sigma \vdash M : \tau$}
    \BIC{$\Delta; \Gamma \vdash \lambda x : \sigma.\; M : \sigma \to \tau$}
  \end{prooftree}
  \color{red}
  \begin{prooftree}
    \AXC{$\Delta, t; \Gamma \vdash M : \sigma$}
    \RightLabel{($\forall$-intro)}
    \UIC{$\Delta; \Gamma \vdash \Lambda t.\;M : \forall t.\, \sigma$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Delta; \Gamma \vdash M : \forall t.\, \sigma$}
    \AXC{$\Delta \vdash \tau$}
    \RightLabel{($\forall$-elim)}
    \BIC{$\Delta; \Gamma \vdash M\;\tau : \sigma\subst{\tau}{t}$}
  \end{prooftree}
  \end{multicols}

  For convenience,
  $\vdash M : \tau$ stands for $\cdot ; \cdot \vdash M : \tau$.

\end{frame}

\begin{frame}{Typing derivation}

The typing judgement ${}\vdash\Lambda t.\, \Lambda u.\, \lambda (x : t)(y : u).\, x : \forall
t.\;t \to u \to t$ is derivable from the following derivation:
\begin{prooftree}
  \AXC{}
  \UIC{$t , u \vdash t$}
  \AXC{}
  \UIC{$t , u \vdash u$}
  \AXC{}
  \UIC{$t , u ; x : t, y : u \vdash x : t$}
  \BIC{$t , u ; x : t \vdash \lambda (y : u).\; x : u \to t$}
  \BIC{$t , u ; \cdot \vdash \lambda (x : t)(y : u).\; x : t \to u \to t$}
  \UIC{$t ; \cdot \vdash \Lambda u.\,\lambda (x : t)(y : u).\; x :\forall u.\,t \to u \to
    t$}
  \UIC{$\vdash \Lambda t.\, \Lambda u.\, \lambda (x : t)(y : u).\; x : \forall t.\,\forall u.\,
  t \to u \to t$}
\end{prooftree}

\end{frame}

\begin{frame}{Exercise}
  Derive the following judgements:
  \begin{enumerate}
    \item ${}\vdash\Lambda t.\,\lambda (x : t).\, x : \forall t.\;t\to t$
    \item $\sigma ; a : \sigma
      \vdash (\Lambda t.\, \lambda (x : t)(y : t).\, x)\;\sigma\;a
      : \sigma \to \sigma$
    \item ${}\vdash\Lambda t.\, \lambda (f : t \to t)(x : t).\, f\;(f\;x) :
      \forall t.\;(t\to t) \to t\to t$
    \end{enumerate}
  Hint. \textbf{F} is syntax-directed, so the type inversion holds.

\end{frame}

\begin{frame}{System F: $\beta$-reduction}

The $\beta$-conversion has two rules
\[
  (\lambda (x : \tau).\, M)\,N \longrightarrow_{\beta}
  M\subst{x}{N}
  \quad\text{and}\quad
  \color{red} (\Lambda t.\, M)\;\tau \longrightarrow_{\beta} M \subst{\tau}{t}
\]

For example,
\[
  (\Lambda t.\lambda x : t.\, x)\; \tau\; a
  \longrightarrow_\beta
  (\lambda x : t.\, x)\subst{\tau}{t}\;a
  \equiv
  (\lambda x : \tau.\, x)\;a
  \longrightarrow_\beta
  x\subst{a}{x}
  \equiv a
\]

Similarly, $\beta$-conversion extends to subterms of a given term, introducing symbols $\onereduce$ and $\reduce$ in the same way.
\end{frame}

%\begin{frame}{System F: Evaluation}
%
%The full $\beta$-reduction is a relation on $\lambda$-terms defined by
%\begin{multicols}{2}
%    \begin{prooftree}
%      \AXC{$M_1 \longrightarrow_{\beta} M_2$}
%      \UIC{$M_1 \onereduce M_2$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$M_1 \onereduce M_2$}
%      \UIC{$\lambda (x:\tau).\, M_1 \onereduce \lambda (x:\tau).\, M_2$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{{\color{red}$M_1 \onereduce M_2$}}
%      \UIC{{\color{red}$\Lambda t.\, M_1 \onereduce \Lambda t.\, M_2$}}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$M_1 \onereduce M_2$}
%      \UIC{$M_1\,N \onereduce M_2\,N$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$N_1\onereduce  N_2$}
%      \UIC{$M\,N_1 \onereduce M\,N_2$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{{\color{red}$M_1 \onereduce M_2$}}
%      \UIC{{\color{red}$M_1\,\tau \onereduce M_2\,\tau$}}
%    \end{prooftree}
%\end{multicols}
%If $M \onereduce N$, then $M$ and $N$ \alert{denotes} the same value. So,
%\[
%  M =_\beta N
%\]
%where $=_\beta$ is the congruence and equivalence closure of
%$\longrightarrow_\beta$.
%\end{frame}

\begin{frame}{Self application}
Self-application is not typable in simply typed $\lambda$-calculus.
  \[
    \lambda (x : t).\, x\; x
  \]
  However, self-application is possible in System F.
  \[
    \lambda (x : \forall t. t\to t).\, x\;(\forall t. t\to t)\;x
  \]
\mode<presentation>{\vfill}
  \begin{block}{Exercise}
    Instantiate the first $t$ with the type $\forall t.\, t \to t$.
  \end{block}
\end{frame}

\begin{frame}{Sum type}

\begin{definition}
  The \emph{sum type} is defined by
  \[
    \sigma + \tau \defeq \forall t. (\sigma \to t) \to (\tau \to t) \to t
  \]
\end{definition}
It has two injection functions: the first injection is defined by
\begin{align*}
  \mathtt{left}_{\sigma + \tau} & \defeq \lambda (x : \sigma).\;\Lambda t.\,\lambda (f : \sigma\to
  t)(g : \tau\to t).\, f\;x \\
  \mathtt{right}_{\sigma + \tau} & \defeq \lambda (y : \tau).\;\Lambda t.\,\lambda (f : \sigma\to
  t)(g : \tau\to t).\, g\;y
\end{align*}

%With the sum type, we can implement the usual construct \texttt{either}:
%\[
%  \mathtt{either} : \forall\,t.\;(\sigma \to t) \to (\tau \to t)
%  \to (\sigma + \tau) \to t
%\]
%which corresponds to the Haskell function
%{\small
%\begin{semiverbatim}
%  either :: (a -> c) -> (b -> c) -> Either a b -> c
%
%  either f g (Left  a) = f a
%
%  either f g (Right b) = g b
%\end{semiverbatim}}

\mode<presentation>{\vfill}
\begin{block}{Exercise}
  Define
  \[
    \mathtt{either} : \forall u.\, (\sigma \to u) \to (\tau \to u) \to \sigma + \tau \to u
  \]
\end{block}
\end{frame}

\begin{frame}{Product type}
\begin{definition}[Product Type]
  The product type is defined by
  \[
    \sigma \times \tau \defeq \forall t. (\sigma \to \tau \to t) \to t
  \]
\end{definition}
The pairing function is defined by
\begin{align*}
  \left< \_, \_\right> \defeq \lambda (x : \sigma)(y : \tau).\,\Lambda t.\,\lambda (f : \sigma \to \tau \to t).\,
  f\;x\;y
\end{align*}
\begin{block}{Exercise}
Define projections
\[
  \mathtt{proj}_1 : \sigma \times \tau \to \sigma
  \quad\text{and}\quad
  \mathtt{proj}_2 : \sigma\times \tau \to \tau
\]
\end{block}
\end{frame}
\begin{frame}[allowframebreaks]{Natural Numbers}
The type of Church numerals is defined by
\[
  \nat \defeq \forall t.\, (t\to t)\to t\to t
\]
  \begin{description}
    \item[Church numerals]
      \begin{align*}
        \bc_n & : \nat \\
        \bc_n & \defeq \Lambda t.\,\lambda (f:t \to t)\,(x:t).\,
        f^n\;x
      \end{align*}
    \item[Successor]
      \begin{align*}
        \suc & : \nat \to \nat \\
        \suc & \defeq \,\lambda (n : \nat).\,\Lambda t.\,\lambda
        (f : t \to t)\,(x:t)\,.\;f\;(n\;t\;f\;x)
      \end{align*}
    \item[Addition]
      \begin{align*}
        \add & : \nat \to \nat \to \nat \\
        \add & \defeq \lambda (n : \nat)\,(m:\nat)\,&& \Lambda t.\, \lambda
        (f:t\to t)\,(x: t).\\
        &&& (m\;t\;f)\;(n\;t\;f\;x)
      \end{align*}
    \item[Multiplication]
      \begin{align*}
       \mul & : \nat\to \nat \to\nat\\
       \mul & \defeq\,?
      \end{align*}
    \item[Conditional]
      \begin{align*}
       \ifz & : \forall t.\,\nat \to t \to t \to t \\
       \ifz & \defeq\,?
      \end{align*}
  \end{description}
System $F$ allows us to define \emph{iterator} like \texttt{fold} in Haskell.
\begin{align*}
  \mathtt{fold}_{\nat} & : \forall t.\, (t \to t) \to t \to \nat \to t  \\
  \mathtt{fold}_{\nat} & \defeq \Lambda t.\, \lambda (f : t \to t)(e_0 : t)(n : \nat).
  n\;t\;f\;e_0
\end{align*}

%In Haskell, the above $\lambda$-term can be given as
%\begin{semiverbatim}
%  fold :: (a -> a) -> a -> [()] -> a
%
%  fold f e []            = e
%
%  fold f e (():xs) = f () (fold f e xs)
%\end{semiverbatim}
%
\begin{block}{Exercise}
  Define $\add$ and $\mul$ using $\mathtt{fold}_{\nat}$ and justify your
  answer.

  \begin{enumerate}
    \item $\add'\defeq \;? : \nat\to\nat\to\nat$
    \item $\mul'\defeq \;? : \nat\to\nat\to\nat$
  \end{enumerate}
\end{block}
\end{frame}

\begin{frame}{Lists}
\begin{definition}
  For any type $\sigma$, the type of lists over $\sigma$ is
\[
  \List\,\sigma\defeq \forall t.\, t \to (\sigma \to t \to t) \to t
\]
\end{definition}
with ``list constructors'':
\[
  \mathtt{nil}_\sigma \defeq \Lambda t.\lambda (h : t)(f : \sigma\to t \to t).\,
  h
\]
and
\[
  \mathtt{cons}_\sigma \defeq \lambda (x : \sigma)(xs : \List\,
  \sigma).\,\Lambda t.\lambda(h : t)(f : \sigma\to t \to t).f\,x\,(xs\; t\;
  h\; f)
\]
of type $\sigma \to \List\;\sigma \to \List\;\sigma$.

\end{frame}

%\begin{frame}[allowframebreaks]{Existential Type}
%  Abstraction can be described by an \emph{existential type}.
%  \[
%    \exists t.\, \tau
%  \]
%  where $\tau$ is a type in which $t$ may appear as a free variable.
%  A term $(\sigma, M)$ of type $\exists t.\, \tau$ consists of a \emph{type}
%  $\sigma$ and a term $M$ of type $\tau\subst{t}{\sigma}$.
%
%
%  \begin{example}
%    A \emph{stack} is a data structure with two operations:
%    \begin{itemize}
%      \item \texttt{push}
%      \item \texttt{pop}
%    \end{itemize}
%    Any implementation satisfying $\mathtt{pop} \circ (\mathtt{push}\, n\, st) =
%    (n, st)$ can be seen as a stack.
%  \end{example}
%
%  \begin{example}
%    The type of stacks over natural numbers can be defined by
%    \[
%      \exists t. (t \times \nat \to t) \times (t \to 1 + \nat \times t)
%    \]
%    \begin{enumerate}
%      \item The first component is the \text{push} function.
%      \item The second component is the \text{pop} function.
%    \end{enumerate}
%    An instance of a stack is an element $(\sigma, M)$ of the above type
%    \begin{enumerate}
%      \item $\sigma$ is a type
%      \item $\mathtt{proj}_1\, M \colon \sigma \times \nat \to \sigma$
%      \item $\mathtt{proj}_2\, M \colon \sigma \to 1 + \nat \times
%          \sigma$.
%    \end{enumerate}
%  \end{example}
%\end{frame}
%
%%\begin{frame}[fragile]{Encoding of Existential Type in Haskell}
%%  A limited form of existential type can be defined by
%%  \begin{semiverbatim}
%%    data Exists t where
%%      Ex :: t a -> Exists t
%%  \end{semiverbatim}
%%  and another version for typeclass
%%  \begin{semiverbatim}
%%    data Exists' c where
%%      Ex' :: c a => a -> Exists' c
%%  \end{semiverbatim}
%%  in GHC with extensions \texttt{GADTs} and \texttt{ConstraintKinds}.
%%
%%\mode<presentation>{\vfill}
%%  \begin{block}{Exercise}
%%    Define the top type $\top$ which contains every typable term.
%%  \end{block}
%%\end{frame}
%
%\begin{frame}{Encoding of Existential Type in System F}
%  The existential type can also be encoded in System F as
%  \[
%    \exists t.\, \tau \defeq \forall u.\; (\forall t.\; \tau \to u) \to u
%  \]
%%  using continuation passing style.\footnote{Recall that $\exists x.\,\varphi
%%  = (\forall x.\, (\varphi \to \bot)) \to \bot$ classically.  }
%\mode<presentation>{\vfill}
%  A witeness $(\sigma, M)$ of $\exists t.\, \tau$ is encoded as
%  \[
%    \Lambda u.\,\lambda (f : \forall t.\, \tau \to u).\, f\;\sigma\;M
%  \]
%
%  \begin{block}{Exercise}
%    Check that the above terms do make sense. How to use a term of some existential
%    type?
%  \end{block}
%
%\end{frame}

\begin{frame}{Type erasure}
\begin{definition}
  The \emph{erasing map} is a function defined by
  \begin{align*}
    |x| & = x \\
    |\lambda (x : \tau).\,M| & = \lambda x.\, |M| \\
    |M\;N| & = (|M|\;|N|) \\
    |\Lambda t.\, M| & = |M| \\
    |M\;\tau| & = |M|
  \end{align*}
\end{definition}

\begin{proposition}
  Within System F, if ${}\vdash M : \sigma$ and $|M|
  \onereduce N'$, then there exists a well-typed term~$N$ with
  ${}\vdash N : \sigma$ and $|N| = N'$.
\end{proposition}
\end{frame}

\begin{frame}{Type safety and normalisation}
  \begin{theorem}[Type safety]
    Suppose $\vdash M : \sigma$. Then,
    \begin{enumerate}
      \item $M \onereduce N$ implies $\vdash N : \sigma$;
      \item $M$ is in normal form or there exists $N$ such that $M \onereduce N$
    \end{enumerate}
  \end{theorem}
  Type safety is proved by induction on the derivation of $\vdash M : \sigma$.

  \begin{theorem}[Normalisation properties]
    \textbf{F} enjoys the weak and strong normalisation properties.
  \end{theorem}
  Proved by Girard's \emph{reducibility candidates}.
\end{frame}

\begin{frame}{Undecidability of type inference}
  \begin{theorem}
     It is undecidable whether, given a closed term $M$ of the untyped
     $lambda$-calculus, there is a well-typed term $M'$ in System F such that
     $|M'| = M$.
  \end{theorem}

  \begin{description}
    \item[Arbitrary Rank Polymorphism] $\forall$ can appear
      anywhere {\small (GHC with \texttt{-XRankNType})}.
    \item[Rank-1 Polymorphism]
      $\forall$ only appear in the outermost position.
  \end{description}
  \emph{Hindley-Milner type system} adapted by Haskell 98, Standard ML, etc.
  supports only rank-1 polymorphism, so type inference is still decidable.
\end{frame}

\begin{frame}{Parametricity}
  What functions can you write for the following type?
  \[
    \forall t.\,t \to t
  \]
  Since $t$ is arbitrary, we cannot inspect the content of $t$. What we can do
  with $t$ is simply return it.
  \begin{theorem}
    Every term $M$ of type $\forall t.\, t \to t$ is \emph{observationally equivalent}%
    \footnote{The notion of observational equivalence is beyond the scope of this lecture.}
      to $\Lambda t.\, \lambda x : t.\, x$.
  \end{theorem}
\end{frame}

\begin{frame}{Parametricity: Theorems for free\footnote{Philip Wadler. 1989. Theorems for free! In \emph{Proceedings of the fourth international conference on Functional programming languages and computer architecture (FPCA ’89)}. ACM, New York, NY, USA, 347–359.}}
  Assume \textbf{F} extended with the list type~$\List\;\tau$ for $\tau$ and
  the type $\mathbb{N}$ of naturals, denoted $\mathbf{F}_{\List, \mathbb{N}}$.

  Then $\mathtt{head} \circ \mathtt{map}\; f = f \circ \mathtt{head}$
      for any $f : \tau \to \sigma$ where $\mathtt{head} : \forall t.\, \List\;t \to t$
  can be proved by just reading the type of $\mathtt{head}$ and $\mathtt{tail}$!
  \begin{theorem}
    For any type $\sigma$ in \textbf{F} (with lists) and $\cdot  \vdash M : \sigma$, then
    \[
      M \sim M : \mathcal{R}_{\sigma, \sigma}
    \]
  \end{theorem}
\end{frame}

%\begin{frame}
%  $M \sim N : \mathcal{R}_{\sigma, \tau}$ is defined as follows.
%
%  \begin{enumerate}
%    \item $M \sim M : \mathcal{R}_{\mathbb{N}, \mathbb{N}}$ for every $M :
%      \mathbb{N}$.
%
%    \item For any $\mathcal{R}_{\tau, \sigma}$,
%      \[
%        M_i \sim N_i : \mathcal{R}_{\sigma, \tau} \; \text{for any $M_i : \sigma$ and $N_i : \tau$}
%        \iff
%        [ M_1, \dots, M_k ] \sim [ N_1, \dots, N_k ] : \mathcal{R}_{\List\;\sigma, \List\;\tau}
%      \]
%
%    \item For any $\mathcal{R}_{\sigma, \sigma'}$ and $\mathcal{S}_{\tau, \tau'}$,
%      $M \sim M' : \mathcal{R}_{\sigma, \sigma'} \implies L\; M
%      \sim L'\;M' : \mathcal{S}_{\tau, \tau'}$ for any $M$ and $M'$
%      if and only if
%      $L \sim L' : \mathcal{R}\to\mathcal{S}_{\sigma \to \tau, \sigma' \to \tau'}$
%
%    \item Assume $\mathcal{F}$ which sends $\mathcal{R}_{\sigma, \tau}$ to
%      $\mathcal{F}\mathcal{R}_{F\sigma, F\tau}$. Then
%      $f\;\sigma \sim g\;\tau : \mathcal{F}\mathcal{R}$ for all $\mathcal{R}_{\sigma, \tau}$
%      if and only if $f \sim g : \forall \mathcal{X}_{t, u}.\, {\mathcal{F}\mathcal{X}}_{Ft, Fu}$
%  \end{enumerate}
%\end{frame}

%\begin{frame}{System $F_\omega$}
%  Recall that for $\sigma, \tau \in \type$ the sum type of $\sigma$ and $\tau$
%  is
%  \[
%    \sigma + \tau \defeq \forall t. (\sigma \to t) \to (\tau \to t) \to t
%  \]
%  Can we internalise this \alert{type construction}?
%  \vfill
%  \alert{\emph{Kinds}} are like \emph{classes} in set theory:
%  \begin{enumerate}
%    \item $*$ : the kind of types
%    \item $* \Rightarrow *$ : the kind of type operators, e.g.,
%      $a \mapsto [a]$
%    \item \ldots
%  \end{enumerate}
%  System $F_\omega$ is an extension of System $F$ with type-level functions and
%  kinds. See \cite{Pierce2002} for further detail.
%\end{frame}
%
%\begin{frame}[allowframebreaks]{Impredicativity}
%  A definition is \alert{\emph{impredicative}} if it has a quantifier whose
%  domain includes itself (which is being defined).
%  \begin{block}{Russell's Paradox}
%    \[
%      R \defeq \set{x}{ x \not\in x }
%    \]
%  \end{block}
%  Recall the self-application ...
%  \[
%    (\Lambda t.\,\lambda (x: t). x)\;{\color{red}(\forall t.\,t \to t)}\;
%    (\Lambda t.\, \lambda (x: t). x)
%  \]
%  which is actually impredicative!
%  This form of polymorphism is called \alert{\emph{impredicative polymorphism}}.
%  \begin{block}{Girard's Paradox}
%    An encoding of Russell's paradox in extensions of
%    System F.
%  \end{block}
%
%  Martin-L\"of's type theory (on which Agda is based) was inconsistent, as it
%  included an axiom\footnote{It is only a simplified story...}:
%  \[
%    \mathsf{Set} : \mathsf{Set}
%  \]
%
%  \begin{block}{Inconsistency of Haskell}
%    Impredicativity $+$ Injectivity $+$ Type Case Analysis
%    $=$ Inconsistency (Russell paradox)
%    \footnote{\url{http://okmij.org/ftp/Haskell/impredicativity-bites.html}}
%
%  \end{block}
%\end{frame}
%\section{Parametricity}
%Polymorphism in System F is \emph{parametric} in the sense that every
%instance is uniformly defined. To put it differently, it cannot depend on any
%specific type. It is different from the \emph{ad hoc} polymorphism adopted in
%programming languages like C++ where programmers are allowed to give a
%specific implementation for some type.
%
%Why is a uniform definition important? Not only we can assure ourselves that a
%polymorphic function has a uniform result for each instance, but also it
%automatically satisfies certain properties which only depends on its type (not
%its instance).
%
%In the end of this lecture, we state a powerful result informally, originally
%called \emph{Abstraction Theorem} by Reynolds~\cite{Reynolds1983} and nowadays
%known as \emph{parametricity}. After that, we apply this theorem to characterise
%some data types introduced so far.
%
%\begin{proposition}
%  Let $\sigma$ be a type without any free variable and $\sigma^+$ the relation
%  on~$\sigma$ lifted from~$\sigma$ (defined below). Then,
%  \[
%    \vdash F : \sigma
%    \implies (F, F) \in \sigma^+.
%  \]
%\end{proposition}
%
%To decode this proposition, we introduce a new notation: a relation $R_\sigma
%\subset \sigma_1 \times \sigma_2$ is denoted by $R_\sigma\colon \sigma_1
%\not\to\sigma_2$. For each type in System F, we define its lifted relation
%$\sigma^+$
%\begin{definition}
%  For each type formation (construct), we define its corresponding formation of
%  relations
%  \begin{enumerate}
%    \item For $f_i:\sigma_i \to \tau_i$, $(f_1, f_2) \in R_\sigma \to S_\tau$ if
%      and only if for every $(M_1, M_2) \in R_\Sigma$ we have $(f_1\;M_1,
%      f_2\;M_2) \in S_\tau$.
%  \end{enumerate}
%\end{definition}
%
%
%
%\subsection*{Exercise}

\begin{frame}{Homework}
  \begin{enumerate}
    \item (25\%) Extend {\PCF} with the type $\mathbb{B}$ of boolean values with
      $\mathtt{ifz}(M; N)\;\true =_\beta M$ and $\mathtt{ifz}(M; N)\;\false
      =_\beta N$ including term formation rules, typing rules, and dynamics for
      $\mathbb{B}$.

    \item (25\%) Define \textbf{pred} in {\textbf{T}} such that $\pred\;\zero =\zero$
      and $\pred\;(\suc\;n) = n$.

    \item (25\%) Define \textbf{even} in {\PCF} such that $\mathbf{even}\;n =
      \suc\;\zero$ if $n$ is an even number; $\mathbf{even}\;n = \zero$
      otherwise.

    \item (25\%) Define $\mathtt{length}_\sigma : \List\;\sigma \to \nat$ calculating the length of a list.
    \item (0\%) Read the paper by Wadler (1989).

  \end{enumerate}

\end{frame}

%\begin{frame}{References}
%\bibliographystyle{amsalpha}
%\bibliography{library}
%\end{frame}

\end{document}


\subsection{Product topology}


\documentclass[9pt, a4paper, oneside]{amsart}

\usepackage[final]{pdfpages}
\usepackage{wrapfig}

\usepackage{enumitem}
\usepackage{parskip}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{multicol}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\newcommand{\hint}[1]{\footnote{\raggedleft\rotatebox{180}{Hint: #1\hfill}}}

\pagestyle{fancy}

\newlist{questions}{enumerate}{1}
\setlist[questions, 1]{label = \bf Q.\arabic*., itemsep=1em}

\lhead{\scshape Apurva Nakade}
\rhead{\scshape Honors Single Variable Calculus}
\renewcommand*{\thepage}{\small\arabic{page}}
\title{Problem Set 08}

\begin{document}

\maketitle
\thispagestyle{fancy}

\section*{Part 1 - Fundamental Theorem of Calculus}
\begin{questions}
	\item  Compute $ F'(x)$ for the following functions\hint{Don't forget the chain rule!}
	\begin{multicols}{2}
		\begin{enumerate}
			\item $ \int \limits_ a ^{x^3} \sin^3 t dt$
			\item $ \int \limits_ x ^{b} \dfrac{1}{1 + t^2 + \sin^2 t} dt$
			\item $ \int \limits_ a ^{b} \dfrac{x}{1 + t^2 + \sin^2 t} dt$
			\item $ \int \limits_ a ^{x} \left( \int \limits_ b ^{y} \dfrac{1}{1 + t^2 + \sin^2 t} dt \right) dy\qquad$
			\item $ \int \limits _ 0 ^ x \dfrac{1}{1+t^2} dt + \int \limits _ 0 ^ {1/x} \dfrac{1}{1+t^2} dt$
			\item $ \int \limits _ {-\cos x}^ {\sin x} \dfrac{1}{\sqrt{1 - t^2}} dt $
		\end{enumerate}
	\end{multicols}

	\item Find $ (f^{-1})'(0)$ if
	\begin{multicols}{2}
		\begin{enumerate}
			\item $ f(x) = \int \limits _ 0 ^ x 1 + \sin (\sin t) dt$
			\item $ f(x) = \int \limits _ 1 ^ x \cos (\cos t) dt$
		\end{enumerate}
	\end{multicols}

	\item Suppose $ f$ is differentiable with $ f(0) = 0$ and $ 0 < f'(x) \le 1$. Show that for all $ x \ge 0$
	\begin{align*}
		\int _0 ^ x f^3 \le \left ( \int_0^x f\right)^2
	\end{align*}

	\item \begin{enumerate}
	\item Find $ F'(x)$ if $ \quad F(x) = \int \limits_0^x x.f(t) dt \qquad$(Be careful: it's not $ x.f(x)$).
	\item Prove that\hint{Differentiate both sides with respect to $ x$.}
	\begin{align*}
		\int _0^x f(t)(x - t) dt & = \int_0^x \left(\int_0^u f(t) dt\right) du
	\end{align*}
	\item Prove that
	\begin{align*}
		\int _0^x f(t)(x - t)^2 dt & = 2\int_0^x \left(\int_0^{u_1} \left(\int_0^{u_2} f(t) dt\right) du_2\right) du_1
	\end{align*}
	\end{enumerate}

	\item
	\begin{enumerate}
		\item Suppose $ G'(x)=g(x)$ and $ F'(x)=f(x)$. Prove that the function $ y(x)$ satisfies the differential equation (\emph{a separable differential equation}) $$ g(y).y' = f(x)$$ for all $ x$ in some interval, if and only if there is a number $ c$ such that $$ G(y) = F(x) + c$$
		\item `Solve' the following differential equations
		      \begin{multicols}{2}
		      	\begin{enumerate}
		      		\item $ y' = \dfrac{1 + x^2}{1+y}$
		      		\item $ y' = \dfrac{-1}{1+5y^4}$
		      	\end{enumerate}
		      \end{multicols}
	\end{enumerate}
\end{questions}

\newpage
\section*{Part 2 - Improper Integrals}
\begin{questions}[resume]
	\item The limit $ \lim \limits_{x \rightarrow \infty} \int \limits_{a}^x f$, if it exists, is denoted by $\int \limits_{a}^ \infty f $ and called an `improper integral'. Similarly for the improper integral $\int \limits_{-\infty}^ a f $.
	\begin{enumerate}
		\item Find $\int \limits_{1}^\infty t^r dt$, when $ r < -1$.
		\item Using $\int \limits_{1}^a t^{-1} \: dt + \int \limits_{1}^b t^{-1}  \: dt = \int \limits_{1}^{ab} t^{-1}  \: dt $, show that $\int \limits_{1}^\infty t^{-1}  \: dt$ does not exist. \hint{What can you say about $\int \limits_{1}^{2^n} t^{-1}  \: dt$?}
	\end{enumerate}

	\item Assume the following the statement:
	\begin{quote}
		Suppose that $ f(x) \ge 0$ for $ x \ge 0$ and that $\int \limits_{a}^ \infty f $ exists. If $ 0 \le g(x) \le f(x)$ for all $ x \ge 0$ and $ g$ is integrable on the interval $ [0,N]$ for all $ N > 0$ then $\int \limits_{0}^ \infty g $ exists.
	\end{quote}
	\renewcommand\labelitemi{$\vcenter{\hbox{\tiny$\bullet$}}$}
	\begin{itemize}
		\item (Optional) Prove the above statement.
		\item For which of the following functions does the integral $\int \limits_{0}^ \infty f $ exist?\hint{You might have to break $ [0,\infty)$ into multiple intervals and analyze each interval separately.}
			      \begin{multicols}{2}
			      	\begin{enumerate}
			      		\item $ \dfrac{1}{1+x^2}$
			      		\item $ \dfrac{1}{\sqrt{1+x^{3}}}$
			      		\item $ \dfrac{x}{{1+x^{3/2}}}$
			      		\item $ \dfrac{1}{\sqrt{1+x}}$
			      	\end{enumerate}
			      \end{multicols}
		\end{itemize}

		\item The improper integral $\int \limits_{-\infty}^ {\infty} f $ is defined as $\int \limits_{-\infty}^ 0 f + \int \limits_{0}^ \infty f $ if both the integrals exist.
		\begin{enumerate}
			\item Show that $ \int \limits_{-\infty}^ {\infty} \dfrac{1}{1+x^2} dx$ exists.
			\item Determine the limit $ \lim \limits_{N \rightarrow \infty} \int \limits_{-N}^ {N} x \: dx$.
			\item Show that $ \int \limits_{-\infty}^ {\infty} x \: dx$ does not exist.
		\end{enumerate}

		\item It is possible to have another kind of improper integral, one where the function itself is unbounded but the limits are finite.

		For $ -1 < r < 0$ draw the graph of $ x^r $ and determine $ \lim \limits_{\epsilon \rightarrow 0^+} \int \limits_\epsilon^a x^r dx$. \\(This limit is written as $\int \limits_0^a x^r dx$.)
	\end{questions}

\end{document}


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

\begin{document}

% The asterix after \subsection disables section numbering
\subsection*{Algorithm Description}

The 2-opt algorithm is a simple local search algorithm that has application to solve the Traveling Salesman Problem. The main idea behind it is to take a route that crosses over itself and reorder that route so that it no longer overlaps. We first encountered K-optimal tours in ~\cite{skiena2008} and learned more through it's Wikipedia article ~\cite{wikipedia2opt} .  This is the only algorithm which we implemented in C.  We chose C over Python for the speed improvement.

\subsection*{Algorithm Discussion}

We decided on this algorithm because it was easy to implement conceptually, it's fairly fast, and it provides the most optimum tour lengths of any algorithm we implemented. Also the algorithm is relatively efficient to compute the path improvement for a given path swap which provides for a favorable runtime. The other algorithms we wrote up were constructive while 2-OPT is a local search heuristic meaning it improves on an exisiting path.  Because 2-OPT is a local search heuristic it needs to be combined with an efficient constructive algorithm which we choose nearest neighbor.  For the three example cases 2-opt found routes between 4\% and 6\% over optimal.

\subsection*{Algorithm Pseudo-code}

\begin{verbatim}
TSP_2OPT_SEARCH(adj_matrix, tour, tour_length, file_name, num_pts):
    improved = true
    old_tour_length = tour_length

    while improved:
        improved = false
        exit_early = false

        for i = 1 to num_pts - 1 and not exit_early:
            for j = i + 1 to num_pts - 1 and not exit_early:
                old_tour_length = tour_length
                TSP_2OPT_SWAP_EFFICIENT(adj_matrix, tour, tour_length, num_pts, i, j)
                if tour_length < old_tour_length:
                    improved = true
                    exit_early = true

                    write the improved tour to file

TSP_2OPT_SWAP(new_tour, tour, num_pts, nodeA, nodeB):
    min_node = MIN(nodeA, nodeB)
    max_node = MAX(nodeA, nodeB)
    new_tour_idx = 0

    for i = 0 to min_node:
        new_tour[new_tour_idx] = tour[i]
        new_tour_idx++

    for i = max_node to min_node:
        new_tour[new_tour_idx] = tour[i]
        new_tour_idx++

    for i = max_node + 1 to num_pts:
        new_tour[new_tour_idx] = tour[i]
        new_tour_idx++

TSP_2OPT_SWAP_EFFICIENT(adj_matrix, tour, tour_length, num_pts, nodeA, nodeB):
    min_node = MIN(nodeA, nodeB)
    max_node = MAX(nodeA, nodeB)
    new_tour_idx = 0

    removed_path_length = 0
    added_path_length = 0

    new_tour[num_pts]

    if max_node + 1 < num_pts:
        removed_path_length = adj_matrix[tour[min_node - 1]][tour[min_node]] +
            adj_matrix[tour[max_node]][tour[max_node + 1]]
        added_path_length = adj_matrix[tour[min_node - 1]][tour[max_node]] +
            adj_matrix[tour[min_node]][tour[max_node + 1]]
    else:
        removed_path_length = adj_matrix[tour[min_node-1]][tour[min_node]] +
        adj_matrix[tour[max_node]][tour[0]];

        added_path_length = adj_matrix[tour[min_node-1]][tour[max_node]] +
        adj_matrix[tour[min_node]][tour[0]];

    path_delta = removed_path_length - added_path_length

    if path_delta > 0:
        for i = 0 to min_node - 1:
            new_tour[new_tour_idx] = tour[i]
            new_tour_idx++

        for i = max_node to min_node:
            new_tour[new_tour_idx] = tour[i]
            new_tour_idx++

        for i = max_node + 1 to num_pts - 1:
            new_tour[new_tour_idx] = tour[i]
            new_tour_idx++

        tour_length -= path_delta
\end{verbatim}

\end{document}


\chapter{Fortran Programs}

%%%%%%%
%  Section
%%%%%%%
\section{Example Programs}

To do parallel programming using OpenMP or MPI (Message passing interface), we typically need to use a lower level language than Matlab such as Fortran. Another possible choice of language is C, however Fortran has superior array handling capabilities compared to C, and has a similar syntax to Matlab, so is typically easier to use for scientific computations which make heavy use of regular arrays. It is therefore useful to introduce a few simple programs in Fortran before we begin studying how to create parallel programs. A good recent reference on Fortran is Metcalf, Reid and Cohen~\cite{MetReiCoh11}.  We recognize that most people will be unfamiliar with Fortran and probably more familiar with Matlab\footnote{Although Matlab is written in C, it was originally written in Fortran and so has a similar style to Fortran.}, C or C++, but we expect that the example codes will make it easy for anyone with some introductory programming background.   A recent guide which describes how to write efficient parallel Fortran code is Levesque and Wagenbreth\cite{LevWag11}. Our programs are written to be run on the Flux cluster at the University of Michigan. More information on this cluster can be found at \url{http://cac.engin.umich.edu/resources/systems/flux/} and at \url{http://cac.engin.umich.edu/started/index.html}. Below are four files you will need to run this.

\begin{enumerate}
\item[1)] A makefile to compile the Fortran code on Flux in listing \ref{lst:MakefileHeat}. This should be saved as {\it makefile}. Before using the makefile to compile the code, you will need to type\\
\texttt{module load fftw/3.2.1-intel}\\
at the command line prompt once logged into Flux. Then place the makefile and heat.f90 in the same directory, the example files below assume this directory is\\
\texttt{\${HOME}/ParallelMethods/Heat}\\
 and type\\
\texttt{make}\\
to compile the file. Once the file is compiled type\\
\texttt{qsub fluxsubscript}\\
to get the cluster to run your program and then output the results. The programs that follow use the library FFTW to do the fast Fourier Transforms. More information on this library can be found at \url{http://www.fftw.org/}.

\lstinputlisting[style=make_style,language=make,label=lst:MakefileHeat,caption={An example makefile for compiling a Fourier spectral Fortran heat equation program.}]{./FortranPrograms/Programs/FortranHeatEquation/makefile}

\item[2)] The Fortran program in listing \ref{lst:FortranHeat} -- this should be saved as {\it heat.f90}
\lstinputlisting[style=fortran_style,language=Fortran,label=lst:FortranHeat,caption={A Fortran Fourier spectral program to solve the heat equation using backward Euler timestepping.}]{./FortranPrograms/Programs/FortranHeatEquation/heat.f90}

\item[3]) An example submission script to use on the cluster in Listing \ref{lst:Fluxsubheat} -- this should be saved as {\it fluxsubscript}. More examples can be found at \url{http://cac.engin.umich.edu/resources/software/pbs.html}. To use it, please change the email address from \url{your_uniqname@umich.edu} to an email address at which you can receive notifications of when jobs start and are finished.

\lstinputlisting[style=bash_style,language=bash,label=lst:Fluxsubheat,caption={An example submission script for use on Flux.}]{./FortranPrograms/Programs/FortranHeatEquation/fluxsubscript}

\item[4)] A Matlab plotting script\footnote{For many computational problems, one can visualize the results with 10-100 times less computational power than was needed to generate the results, so for problems which are not too large, it is much easier to use a high level language like Matlab to post-process the data.} to generate Fig.\ \ref{fig:FortranHeat} is in listing \ref{lst:PlotMatlab}.

\lstinputlisting[style=matlab_style,label=lst:PlotMatlab,caption={A Matlab program to plot the computed results.}]{./FortranPrograms/Programs/FortranHeatEquation/plotcreate.m}

\begin{figure}
\begin{center}
\includegraphics[scale=.35]{./FortranPrograms/heatPlot.jpg}
\caption{The solution to the heat equation computed by Fortran and post-processed by Matlab.} \label{fig:FortranHeat}
\end{center}
\end{figure}

\end{enumerate}

%%%%%%%
%  Section
%%%%%%%
\section{Exercises}
\begin{enumerate}
\item[1)] Please read the resources on the web page \url{http://cac.engin.umich.edu/started/index.html} to learn how to use the Flux cluster.
\item[2)] Modify the Fortran program for the 1-D heat equation to solve the Allen-Cahn equation, with your choice of time stepping scheme. Create a plot of the output of your run. Include the source code and plot in your solutions.
\item[3)] Modify the Fortran program for the 1-D heat equation to solve the  2-D heat equation with your choice of time stepping scheme. Your program should save the field at each time step rather than putting all the fields in a single large array. Create a plot of the initial and final states of your run. Include the source code and plots in your solutions.
\end{enumerate}


\section{Algorithms}

\lagraph~\cite{DBLP:conf/ipps/MattsonDKBMMY19} implements graph algorithms using the \grb C API~\cite{DBLP:conf/hpec/MattsonYMBM17}.

\input{list-of-algorithms}

\input{algorithms/bfs}

\input{algorithms/msbfs}

\input{algorithms/bc}

\input{algorithms/pagerank}

\input{algorithms/sssp-bf-basic}

\input{algorithms/sssp-delta-stepping}

\input{algorithms/apsp-floyd-warshall}

\input{algorithms/fastsv}

\input{algorithms/triangle-count}

\input{algorithms/lcc}

\input{algorithms/k-truss}

\input{algorithms/louvain}

\input{algorithms/cdlp}


\input{header.tex}

%\setlength{\headsep}{-10pt}
\setlength{\parskip}{0.2em}
%\setlength{\textheight}{11 in}
\setlength{\skip\footins}{20pt}

% opening
\title{Plotting large datasets, Part 1: Static plots}
\author{Colin Leach, March 2020}
\date{\vspace{-3ex}}

\hyphenpenalty=1000

\begin{document}

\maketitle

Galaxy simulations, even undergraduate projects, can involve upwards of $10^7$ particles. Tough to plot, so what are some options?

I made these notes for my own use but there may (?) be other students who are interested.

The dataset used is an array with shape (3, 975000), so 975,000 sets of $(x,y,z)$ coordinates, at a time when two galaxies are tidally disrupting one another.

\section{Matplotlib hist2d}

Covered in Lab 7 of ASTR400B, which also adds density contours. That only dealt with single disks but extending it to collisions is relatively simple. Think centering, orientation and (perhaps hardest!) what $x$ and $y$ limits to set.

The result may look something like this:

{\centering \includegraphics[scale=0.5]{mpl contours, 400b code} \par}

\section{Matplotlib plus mpl\_scatter\_density}

This isn't a new problem, so someone wrote an extension to matplotlib to make density plots easier.

Website: \url{https://github.com/astrofrog/mpl-scatter-density}

Borrowing heavily from their examples, this is a first attempt:

\lstdefinestyle{py}{
	belowcaptionskip=1\baselineskip,
	breaklines=true,
	frame=L,
	xleftmargin=\parindent,
	language=Python,
	showstringspaces=false,
	basicstyle=\footnotesize\ttfamily,
	keywordstyle=\bfseries\color{green!40!black},
	commentstyle=\itshape\color{purple!40!black},
	identifierstyle=\color{blue},
	stringstyle=\color{orange},
}

\lstset{style=py}
\begin{lstlisting}
# install with `conda install mpl-scatter-density`
import mpl_scatter_density

# Make the norm object to define the image stretch
from astropy.visualization import LogStretch
from astropy.visualization.mpl_normalize import ImageNormalize
norm = ImageNormalize(vmin=0., vmax=1000, stretch=LogStretch())

# Make the plot in the (almost) usual pyplot way
# Note the projection to invoke mpl_scatter_density
#  and the norm to use LogStretch

fig = plt.figure(figsize=(12,10))
ax = fig.add_subplot(1, 1, 1, projection='scatter_density')
density = ax.scatter_density(disks[0], disks[1], norm=norm)
plt.xlim(-120, 120)
plt.ylim(-120, 120)

# Add axis labels (standard pyplot)
fontsize = 18
plt.xlabel('x (kpc)', fontsize=fontsize)
plt.ylabel('y (kpc)', fontsize=fontsize)
fig.colorbar(density, label='Number of stars per pixel')
\end{lstlisting}

{\centering \includegraphics[scale=0.55]{msg} \par}

The colorbar needs some work, but we now get pixel-level resolution. It's a bit easier to see the tidal tails, at least on screen (printouts can be disappointing).

First impressions: a nice addition to matplotlib, quick and easy to set up and use.

\end{document}


\subsection{Boolean types. Comparisons}
Here are most of the built-in objects considered false:
\begin{itemize}
\item constants defined to be false: \mintinline{python}{None} and \mintinline{python}{False}.
\item zero of any numeric type: \mintinline{python}{0}, \mintinline{python}{0.0}, \mintinline{python}{0j}, \mintinline{python}{Decimal(0)}, \mintinline{python}{Fraction(0, 1)}
\item empty sequences and collections: \mintinline{python}{''}, \mintinline{python}{()}, \mintinline{python}{[]}, \mintinline{python}{{}}, \mintinline{python}{set()}, \mintinline{python}{range(0)}.
\end{itemize}

Warning: \mintinline{python}{not a == b} means \mintinline{python}{not (a == b)}.

Operations ordered by ascending priority (first two are short-circuit: evaluate second argument only if needed):
\begin{itemize}
	\item \mintinline{python}{p or q}: if p is false, then q, else p
	\item \mintinline{python}{p and q}: if p is false, then p, else q
	\item \mintinline{python}{not p}: if p is false, then \mintinline{python}{True}, else \mintinline{python}{False}
\end{itemize}

All eight operations have the same priority (higher than that of the Boolean operations).
Can be chained.
They are:
\mintinline{python}{<},
\mintinline{python}{<=},
\mintinline{python}{>},
\mintinline{python}{>=},
\mintinline{python}{==} (equal),
\mintinline{python}{!=} (not equal),
\mintinline{python}{is} (object identity),
\mintinline{python}{is not}.

For the string and bytes types, \mintinline{python}{x in y} checks if \mintinline{python}{x} is a substring of \mintinline{python}{y}.

%