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

\title{Programming Language Theory}
\subtitle{Higher-Order Functions}
\begin{document}

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

\section{Simply Typed $\lambda$-Calculus: Statics}

\begin{frame}{Typing judgement}
  A \alert{typing judgement} is of the form
  \[
    \Gamma \vdash M : \sigma
  \]
  saying the \emph{term $M$ is of type $\sigma$ under the context $\Gamma$}
  where
  \begin{description}
    \item[context $\Gamma$] free variables $x : \tau$ available in $M$
    \item[term $M$] possibly with free variables in $\Gamma$,
    \item[type $\sigma$] for $M$
  \end{description}

  \[
    x_1:\tau_1, x_2: \tau_2 \vdash x_1 : \tau_1
  \]
  `\emph{Under the context consisting of variables $x_1:\tau_1, x_2:\tau_2$, the term $x_1$ is of type $\tau_1$.}'
\end{frame}

\begin{frame}{Context}

\begin{definition}
    A \emph{typing context}~$\Gamma$
    is a sequence
    \[
      \Gamma \equiv x_1 : \sigma_1,\; x_2 : \sigma_2,\; \ldots,\; x_n : \sigma_n
    \]
    of \alert{\emph{distinct variables}} $x_i$ of type $\sigma_i$.
\end{definition}

\begin{definition}
  The membership judgement $\Gamma \ni (x : \sigma)$ is defined inductively as follows.

  \begin{multicols}{2}
    \begin{prooftree}
      \AXC{$\vphantom{\Gamma}$}
      \RightLabel{(here)}
      \UIC{$\Gamma, x : \sigma \ni (x : \sigma)$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$\Gamma \ni (x : \sigma)$}
      \RightLabel{(there)}
      \UIC{$\Gamma, y : \tau \ni (x : \sigma)$}
    \end{prooftree}
  \end{multicols}
\end{definition}
\end{frame}

\begin{frame}{Higher-order function type}

\begin{definition}
  Define the judgement $\tau : \type$ by
  \begin{multicols}{2}
    \begin{prooftree}
      \AXC{$\sigma$ is a type variable}
      \RightLabel{(tvar)}
      \UIC{$\sigma : \type$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$\sigma : \type$}
      \AXC{$\tau   : \type$}
      \RightLabel{(fun)}
      \BIC{$\sigma \to \tau : \type$}
    \end{prooftree}
  \end{multicols}
  where $\sigma\to\tau$ represents a function type from $\sigma$ to $\tau$.

  Also $\sigma_1 \to \tau_1 = \sigma_2 \to \tau_2$ if and only if $\sigma_1 = \sigma_2 \text{ and } \tau_1 = \tau_2$.
\end{definition}
\mode<presentation>{\vfill}
\begin{block}{Convention}
\[
  \sigma_1 \to \sigma_2 \to \dots \sigma_n \quad  \defeq\quad \sigma_1 \to
  (\sigma_2 \to ( \dots \to (\sigma_{n-1} \to \sigma_n)\dots))
\]
\end{block}

\end{frame}

\begin{frame}
  The function type is \alert{higher-order}, because
  \begin{enumerate}
    \item functions can be arguments of another function;
    \item functions can be the result of a computation.
  \end{enumerate}
  \begin{example}
    \begin{description}
      \item[$\;(\sigma_1 \to \sigma_2) \to \tau$] a function type whose argument is of type $\sigma_1 \to \sigma_2$;
      \item[$\;\sigma_1 \to (\sigma_2 \to \tau)$] a function whose return type is $\sigma_2 \to \tau$.
    \end{description}
  \end{example}

\mode<presentation>{\vfill}
For a term $M$, how to construct a \emph{typing judgement}
\[
  \Gamma \vdash M : \sigma \to \tau
\]
\end{frame}

%\begin{frame}{Hypothetical judgement}
%  A \emph{hypothetical judgement} (or, an inference rule) is of the form
%  \begin{prooftree}
%    \AXC{$J_1$}
%    \AXC{$J_2$}
%    \AXC{$\dots$}
%    \AXC{$J_n$}
%    \RightLabel{(rule)}
%    \QuaternaryInfC{$J$}
%  \end{prooftree}
%  consisting of
%  \begin{description}
%    \item[hypothesis] a set of judgements $J_i$, for $1 \leq i \leq n$
%    \item[name] the name for the reason why $J_i$'s imply $J$
%    \item[conclusion] a single judgement $J$
%  \end{description}
%  Examples include
%  \begin{enumerate}
%    \item The formation of $\lambda$-terms (var), (app), and (abs).
%    \item The formation of $\alpha$-equivalence $=_\alpha$.
%    \item The formation of simple types $\mathbb{T}$.
%  \end{enumerate}
%\end{frame}

\begin{frame}{Typing rule -- Curry-style typing system}
  A \emph{typing rule} is an inference rule with its conclusion a
  typing judgement.

  \begin{prooftree}
    \AXC{$\Gamma \ni (x : \sigma)$}
    \RightLabel{(var)}
    \UIC{$\Gamma \vdash_i x : \sigma$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Gamma, x : \sigma \vdash_i M : \tau$}
    \RightLabel{(abs)}
    \UIC{$\Gamma \vdash_i \lambda x.\; M : \sigma \to \tau$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Gamma \vdash_i M : \sigma \to \tau$}
    \AXC{$\Gamma \vdash_i N : \sigma$}
    \RightLabel{(app)}
    \BIC{$\Gamma \vdash_i M\;N : \tau$}
  \end{prooftree}

\mode<presentation>{\vfill}
It is known as the \alert{implicit typing} system since the typing information is an add-on to the term.
\end{frame}

%\begin{frame}{Derivation}
%  Given a set $\mathcal{R}$ of hypothetical judgements, a \emph{derivation} of
%  \begin{prooftree}
%    \AXC{$J_1$}
%    \AXC{$J_2$}
%    \AXC{$\dots$}
%    \AXC{$J_n$}
%    \QuaternaryInfC{$J$}
%  \end{prooftree}
%  is a tree of instances of rules in $\mathcal{R}$ composed with $J_i$'s as the top-level hypothesises.
%
%  A judgement $J$ is \emph{derivable} (with assumptions $J_i$'s) if there is derivation whose root is $J$ (with assumptions $J_i$'s).
%
%  \begin{example}
%    The fact that
%    \[
%      \lambda x\,y.\, y
%    \]
%    is a $\lambda$-term means the judgement $\lambda x\,y.\, y$ is derivable
%    from the formation rules of $\lambda$-terms without any assumption.
%  \end{example}
%\end{frame}

\begin{frame}{Typing derivation}
  The judgement $\vdash \lambda x.\, x : \sigma \to \sigma$, for all $\sigma
  \in \mathbb{T}$ has a derivation
  \begin{prooftree}
    \AXC{}
    \RightLabel{(var)}
    \UIC{$x : \sigma \vdash_i x : \sigma$}
    \RightLabel{(abs)}
    \UIC{$\vdash_i \lambda x.\; x : (\sigma \to \sigma)$}
  \end{prooftree}

  The judgement $ \vdash \lambda x\,y.\, x : \sigma \to \tau \to \sigma$
  has a derivation
\begin{prooftree}
  \AXC{}
  \RightLabel{(var)}
  \UIC{$x : \sigma, y: \tau \vdash_i x : \sigma$}
  \RightLabel{(abs)}
  \UIC{$x : \sigma \vdash_i \lambda y.\, x : \tau \to \sigma$}
  \RightLabel{(abs)}
  \UIC{$\vdash_i \lambda x\,y.\, x : \sigma \to \tau \to \sigma$}
\end{prooftree}

  Not every $\lambda$-term has a type:
  \[
    \lambda x.\, x\,x
  \]
  there is no $\tau$ satisfying $\vdash \lambda x.\, x\;x : \tau$.
\end{frame}

\begin{frame}{Syntax-directedness}
  A typing system is \emph{syntax-directed} if it has \emph{exactly} one typing rule
  for each term construct. Therefore,

  \begin{lemma}[Typing inversion]
    Suppose
    \[
      \Gamma \vdash_i M : \tau
    \]
    is derivable. If
    \begin{description}
      \item[$M \equiv x$] then $x : \tau$ occurs in $\Gamma$.
      \item[$M \equiv \lambda x.\, M'$] then $\tau = \sigma \to \tau'$ for some $\sigma$ and $\Gamma, x:\sigma \vdash_i M' : \tau'$.
      \item[$M \equiv L\;N$] there is some $\sigma$ such that $\Gamma \vdash_i L : \sigma \to \tau$ and $\Gamma \vdash_i N : \sigma$.
    \end{description}
  \end{lemma}
\end{frame}

\begin{frame}{Explicit typing: Typed terms}
\begin{definition}[Typed terms]
  The formation $M : \term_{\lambda_\to}$ of typed terms is defined by
    \begin{prooftree}
      \AXC{$x \in V$}
      \UIC{$x : \term_{\lambda_\to}$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$M : \term_{\lambda_\to}$}
      \AXC{$N : \term_{\lambda_\to}$}
      \BIC{$M\, N : \term_{\lambda_\to}$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$M : \term_{\lambda_\to}$}
      \AXC{$x \in V$}
      \AXC{$\tau : \type$}
      \TIC{$\lambda {\color{red}x:\tau}.\; M : \term_{\lambda_\to}$}
    \end{prooftree}
\end{definition}
\end{frame}

\begin{frame}{Explicit typing: Typing rules}
\begin{definition}[Typing Rules]
  Typing derivations on \emph{typed terms} are defined by
  \begin{prooftree}
    \AXC{$\Gamma \ni (x : \sigma)$}
    \RightLabel{(var)}
    \UIC{$\Gamma \vdash_e x : \sigma$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Gamma \vdash_e M : \sigma \to \tau$}
    \AXC{$\Gamma \vdash_e N : \sigma$}
    \RightLabel{(app)}
    \BIC{$\Gamma \vdash_e M\;N : \tau$}
  \end{prooftree}
  \begin{prooftree}
    \AXC{$\Gamma, {\color{red}x : \sigma} \vdash_e M : \tau$}
    \RightLabel{(abs)}
    \UIC{$\Gamma \vdash_e \lambda {\color{red}x : \sigma} .\; M : \sigma \to \tau$}
  \end{prooftree}
\end{definition}

\end{frame}
\begin{frame}{Explicit typing: Unicity}
\begin{proposition}
  For every typed term $M$, context~$\Gamma$, and types $\sigma_i$,
  \[
    \Gamma \vdash_e M : \sigma_1
    \quad\text{and}\quad
    \Gamma \vdash_e M : \sigma_2
    \implies
    \sigma_1 = \sigma_2
  \]
\end{proposition}
\begin{proof}[Proof sketch]
  Use the inversion lemma and the structural induction on $M$.

  E.g., suppose that $M$ is of the form
  \[
    L\;M'
  \]
  By inversion there are $\tau_i$ such that $\Gamma \vdash_e L: \tau_i \to
  \sigma_i$ and $\Gamma \vdash_e M': \tau_i$. By induction hypothesis, $\tau_1 \to
  \sigma_1 = \tau_2 \to \sigma_2$, so $\sigma_1 = \sigma_2$.
\end{proof}
\end{frame}

\begin{frame}{Exercise}
  \begin{enumerate}
    \item Derive the judgement
      \[
        \vdash \lambda f\,g\,x.\, f\,x\, (g\,x) : (\sigma \to \tau \to \rho) \to
        (\sigma\to\tau) \to \sigma\to\rho
      \]
      for every $\sigma, \tau, \rho \in \mathbb{T}$.

    \item Describe all possible types for Church numeral $\bc_{n}$.

  \end{enumerate}

\end{frame}

\begin{frame}{Type erasure}
  An \emph{erasing map} $|-|\colon \term_{\lambda_\to} \to \term_{\lambda}$ is defined by
  \begin{align*}
    |x| & = x \\
    |M\; N| & = |M|\;|N| \\
    |\lambda x:\sigma.\, M| & = \lambda x.\, |M|
  \end{align*}
  \begin{example}
    \begin{enumerate}
      \item $|\lambda (f: \sigma \to \tau)\,(x: \sigma).\, f\;x| = \lambda f\, x.\, f\;x$
      \item $|(\lambda (x: \sigma)\,(y: \tau). y)\;z| = (\lambda x\,y.\, y)\; z$
    \end{enumerate}
  \end{example}

  $|-|$ is an translation from $\term_{\lambda_\to}$ to $\term_{\lambda}$.
  Does $|-|$ respect the behaviour of $\term_{\lambda_\to}$?
\end{frame}
\begin{frame}{From typed terms to untyped and back}
\begin{proposition}
  Let $M$ and $N$ be typed $\lambda$-terms in~$\term_{\lambda_\to}$. Then,
  \begin{align*}
    \Gamma  \vdash_e M : \sigma & \text{ implies } \Gamma \vdash_i |M| :
    \sigma \\
    M \reduce N & \text{ implies } |M| \reduce |N|
  \end{align*}
\end{proposition}

\begin{proposition}
  Let $M$ and $N$ be $\lambda$-terms in~$\term_{\lambda}$. Then,
  \begin{enumerate}
  \item If $\Gamma \vdash_i M : \sigma$, then there is $M' : \term_{\lambda_\to}$ with
        $|M'| = M
        \quad\text{and}\quad
        \Gamma \vdash_e M' : \sigma$
      \item If $M \reduce N$ and $M = |M'|$ for some $M' : \term_{\lambda_\to}$,
      then there exists $N'$ with $|N'| = N$ and $M' \reduce N'$.
    \end{enumerate}
\end{proposition}
\end{frame}

\begin{frame}{Type inference}
  Can we answer the following questions
  \begin{description}
    \item[Typability] Given a closed term $M$, is there a type $\sigma$
      such that $\vdash M : \sigma$?
    \item[Type checking] Given $\Gamma$ and $\sigma$, is $\Gamma \vdash M : \sigma$ derivable?
  \end{description}
  algorithmically?

  Typability is reducible to type checking problem of
  \[
    x_0: \tau \vdash \textbf{K}_1\;x_0\;M : \tau
  \]

  \begin{theorem}
    Type checking is \emph{decidable}
    in simply typed $\lambda$-calculus.
  \end{theorem}

\mode<presentation>{\vfill}
Check \emph{bidirectional type inference}.
\subsection*{Exercise}

\end{frame}

\section{Programming in Simply Typed $\lambda$-Calculus}

\begin{frame}[allowframebreaks]{Church encodings of natural numbers}
The type of natural numbers is of the form
\[
  \nat_\tau \defeq (\tau \to \tau) \to \tau \to \tau
\]
for every type $\tau \in \T$.

  \begin{description}
    \item[Church numerals]
      \begin{align*}
        & \bc_n \defeq \lambda f\,x.\,
        f^n x \\
        \vdash{} & \bc_n : \nat_\tau
      \end{align*}
    \item[Successor]
      \begin{align*}
        & \suc \defeq \lambda n \,f\,x\,.\;f\;(n\;f\;x) \\
        \vdash{} & \suc : \nat_\tau \to \nat_\tau
      \end{align*}
    \item[Addition]
      \begin{align*}
        & \add \defeq \lambda n\,m\,f\,x.\; (m\;f)\;(n\;f\;x) \\
        \vdash{} & \add : \nat_\tau \to \nat_\tau \to \nat_\tau
      \end{align*}
    \item[Muliplication]
      \begin{align*}
        & \mul \defeq \lambda n\,m\,f\,x.\, (m\;(n\;f))\;x\\
      \vdash{} & \mul : \nat_\tau \to \nat_\tau \to \nat_\tau
      \end{align*}
    \item[Conditional]
      \begin{align*}
        & \ifz \defeq \lambda n\,x\,y.\, n\;(\lambda z.\, x)\;y\\
        \vdash {} & \ifz : ?
      \end{align*}
  \end{description}
The type of~$\ifz$ may not be as obvious as you may expect.
Try to find one as general as possible and justify your guess.

\end{frame}
\begin{frame}{Church encodings of boolean values}
We can also define the type of Boolean values
for each type variable as
\[
  \bool_\tau \defeq \tau \to \tau \to \tau
\]
\begin{description}
  \item[Boolean values]
      \[
        \true \defeq \lambda x\,y.\,x
        \quad\text{and}\quad
        \false \defeq \lambda x\,y.\,y
      \]
  \item[Conditional]
    \begin{align*}
      & \cond \defeq \lambda b\,x\,y.\, b\,x\,y\\
      \vdash {} & \cond : \bool_{\tau} \to \tau \to \tau \to \tau
    \end{align*}
\end{description}
\end{frame}

\begin{frame}{Exercise}
  \begin{enumerate}
    \item Define conjunction $\mathtt{and}$, disjunction $\mathtt{or}$, and
      negation $\mathtt{not}$ in simply typed lambda calculus.

    \item Prove that $\mathtt{and}$, $\mathtt{or}$, and $\mathtt{not}$ are well-typed.
  \end{enumerate}

\end{frame}

\section{Properties of Simply Typed $\lambda$-Calculus}

\begin{frame}[c]{Type safety = Preservation + Progress}

\begin{quote}
  ``Well-typed programs cannot `go wrong'.''\\
  \hfill ---(Milner, 1978)
\end{quote}

\begin{description}
  \item[Preservation] If $\Gamma \vdash M : \sigma$ is derivable and $M \onereduce N$, then $\Gamma \vdash N : \sigma$.
  \item[Progress] If $\Gamma \vdash M : \sigma$ is derivable, then either $M$ is in \emph{normal form} or there is $N$ with $M \onereduce N$.
\end{description}

\end{frame}

\begin{frame}[allowframebreaks]{Converse of Preservation}
\begin{example}
  Recall that
  \begin{enumerate}
    \item $\mathbf{I} = \lambda x.\, x$
    \item $\mathbf{K}_1 = \lambda x\,y.\, x$
    \item $\Omega = (\lambda x.\, x\,x)\,(\lambda x.\, x\,x)$
  \end{enumerate}
  and $\mathbf{K}_1\,\mathbf{I}\,\Omega \reduce \mathbf{I}$. However,
  \[
    \vdash \mathbf{I} : \sigma \to \sigma
    \notimplies
    \vdash \mathbf{K}_1\;\mathbf{I}\;\Omega : \sigma \to \sigma.
  \]
\end{example}
How to prove it?
\framebreak
\begin{lemma}[Typability of subterms]
  Let $M$ be a term with $\Gamma \vdash M : \tau$ derivable. Then, for every
  subterm $M'$ of~$M$ there exists $\Gamma'$ such that
  \[
    \Gamma' \vdash M' : \sigma'.
  \]
\end{lemma}
\begin{proof}
  By induction on $\Gamma \vdash M : \sigma$.
\end{proof}
  $\Omega$ is not typable, so $\mathbf{K}_1\,\mathbf{I}\,\Omega$ is not typable.
\end{frame}

\begin{frame}{A prelude to the preservation proof}
    \begin{description}
      \item[Weakening] If $\Gamma \vdash M : \tau$ and $x \not\in \Gamma$, then
        $\Gamma, x : \sigma \vdash M : \tau$.
      \item[Substitution] If $\Gamma, x : \tau \vdash M : \sigma$ and $\Gamma
        \vdash N : \tau$ then $\Gamma \vdash M\subst{N}{x} : \sigma$.
    \end{description}

  \begin{corollary}[Variable renaming]
    If $\Gamma, x : \tau \vdash M :\sigma$ and $y \not\in \mathrm{dom}(\Gamma)$, then $ \Gamma, y : \tau \vdash M\subst{y}{x} :
    \sigma$
    where $\mathrm{dom}(\Gamma)$
    denotes the set of variables which occur in $\Gamma$.
  \end{corollary}
  \begin{proof}
    $y$ is not in $\Gamma$, so
      $\Gamma, y : \tau, x : \tau \vdash M$
    by weakening and by definition $\Gamma, y : \tau \vdash y : \tau$.
    Thus, by substitution, we have
    \[
      \Gamma, y : \tau \vdash M\subst{x}{y} : \sigma
    \]
  \end{proof}
\end{frame}

\begin{frame}{Preservation Theorem}
  \begin{theorem}
    If $\Gamma \vdash M : \sigma$ is derivable and $M \onereduce N$, then $\Gamma \vdash N : \sigma$.
  \end{theorem}
  \begin{proof}[Proof sketch]
  By induction on both the derivation of $\Gamma \vdash M :
  \sigma$ and $M \onereduce N$.

  The only non-trivial case is
  \[
    \Gamma \vdash (\lambda  x_1 : \tau .\, M_1)\; N : \sigma%    \quad\text{and}\quad %    (\lambda (x_1 : \tau).\, M_1)\;N \onereduce M_2\subst{x_2}{N}
  \]
  with the substitution lemma applied to
  \[
    \Gamma, x_1 : \tau \vdash M_1 : \sigma
    \quad\text{and}\quad
    \Gamma \vdash N : \tau.
  \]
\end{proof}

\end{frame}

\begin{frame}{Normal form}
  The notion of normal form can be characterised syntactically:
  \begin{definition}
    Define judgements $\texttt{Neutral}\;M$ and $\texttt{Normal}\;M$ mutually by
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\vphantom{\Gamma}$}
        \UIC{$\texttt{Neutral}\;x$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\neutral\;M$}
        \AXC{$\normal\;N$}
        \BIC{$\neutral\;M\;N$}
      \end{prooftree}
      \columnbreak
      \begin{prooftree}
        \AXC{$\neutral\;M$}
        \UIC{$\normal\;M$}
      \end{prooftree}
      \begin{prooftree}
        \AXC{$\normal\;M$}
        \UIC{$\normal\;\lambda x.\,M$}
      \end{prooftree}
    \end{multicols}
  \end{definition}
  \textbf{Idea.} $\neutral\;M$ (resp.\, $\normal\;M$) is derivable
  iff
  \[
    M \equiv x\;N_1 \cdots N_k
    \quad\text{and}\quad
    M \equiv \lambda x_1 \cdots x_n .\, x\;N_1 \cdots N_k
  \]
  respectively where $N_i$'s are in normal form.
\end{frame}

\begin{frame}{Soundness and completeness of the inductive characterisation}
  \begin{lemma}
    Let $M$ be an untyped term.
    \begin{description}
      \item[Soundness] If $\normal\;M$ (resp.\ $\neutral\;M$) is derivable, then $M$ is in normal form.
      \item[Completeness]
        If $M$ is in normal form, then $\normal\;M$ is derivable.
    \end{description}
  \end{lemma}
  \begin{proof}[Proof sketch.]
    \begin{description}
      \item[Soundness] By mutual induction on the derivation of
    $\normal\;M$ and $\neutral\;M$.
      \item[Completeness] By induction on the formation of $M$.
    \end{description}
  \end{proof}

\end{frame}
\begin{frame}{Progress}
  \begin{theorem}
    If $\Gamma \vdash M : \sigma$ is derivable, then $\normal\;M$ or there is $N$ with $M \onereduce N$.
  \end{theorem}
  \begin{proof}[Proof sketch]
    By induction on the derivation of $\Gamma \vdash M : \sigma$.
  \end{proof}
\end{frame}

\begin{frame}{Weak normalisation}
  \begin{definition}
    $M$ is \emph{weakly normalising} denoted by $M\downarrow$ if
    \begin{multicols}{2}
      \begin{prooftree}
        \AXC{$\normal\; M$}
        \UIC{$M \downarrow$}
      \end{prooftree}
      \columnbreak
      \begin{prooftree}
        \AXC{$M \onereduce N$}
        \AXC{$N \downarrow$}
        \BIC{$M \downarrow$}
      \end{prooftree}
    \end{multicols}
  \end{definition}
  That is, $M$ is weakly normalising if there is a sequence
  \[
    M \onereduce M_1 \onereduce M_2 \onereduce \dots N \notonereduce
  \]
  \begin{theorem}[Weak normalisation]
    Every term $M$ with $\Gamma \vdash M : \tau$ is weakly normalising.
  \end{theorem}

\end{frame}

\begin{frame}{Strong normalisation}
  \begin{definition}
    $M$ is \emph{strongly normalising} denoted by $M \Downarrow$ if
    \begin{prooftree}
      \AXC{$\forall N.\, (M \onereduce N \implies N \Downarrow)$}
      \UIC{$M \Downarrow$}
    \end{prooftree}

  \end{definition}
  Intuitively, \emph{strong normalisation} says every sequence
    \[
      M \onereduce M_1 \onereduce M_2 \cdots
    \]
  terminates.
  \begin{theorem}
    Every term $M$ with $\Gamma \vdash M : \tau$ is strongly normalising.
  \end{theorem}

\end{frame}

\begin{frame}{Definability}
%Within simply typed lambda calculus, we wonder how many computable functions can
%be defined, as it excludes non-terminating $\lambda$-terms.
  A function $f\colon \mathbb{N}^k \to \mathbb{N}$ is called
  \alert{\emph{$\lambda_\to$-definable}} if there is a $\lambda$-term $F$ of
  type $\nat \to \nat \to \dots \nat \to \nat$ such that
  \[
    F\,\bc_{n_1}\ldots\bc_{n_k} \reduce \bc_{f(n_1, \dots, n_k)}
  \]
  for every sequence $(n_1, n_2, \ldots, n_k) \in \mathbb{N}^k$.
  Diagrammatically,

\[
  \xymatrix{
    (n_1, n_2, \ldots, n_k) \ar@{|->}[rr] \ar@{|->}[d]_{(\bc_{-})^k} & & f(n_1,
    n_2, \ldots, n_k) \ar@{|->}[d]^{\bc_{-}}\\
    (\bc_{n_1}, \bc_{n_2}, \ldots, \bc_{n_k}) \ar@{|->}[rr] & &
    F\;\bc_{n_1}\; \bc_{n_2}\; \ldots\;\bc_{n_k}
    = \bc_{f(n_1, n_2, \ldots, n_k)}
  }
\]
\end{frame}
\begin{frame}{The limit of $\lambda_\to$}
\begin{theorem}
  The $\lambda_\to$-definable functions are the class of functions
  of the form $f\colon \mathbb{N}^k \to \mathbb{N}$ closed under
  compositions
  which contains
  \begin{itemize}
    \item the constant functions,
    \item projections,
    \item additions,
    \item multiplications,
    \item and the conditional
  \[
    \mathrm{ifz}(n_0, n_1, n_2) =
    \begin{cases}
      n_1 & \text{if } n_0 = 0\\
      n_2 & \text{otherwise.}
    \end{cases}
  \]
  \end{itemize}
\end{theorem}
\end{frame}

%\subsection{System T}
%It is also convenient to add some primitive types to our typed lambda calculus (or
%so-called built-in types in programming languages used in practice) as well as
%\emph{primitive terms}. For example, for a system with natural numbers, we
%include numerals in the generation of syntax:
%  \begin{multicols}{2}
%    \begin{prooftree}
%      \AXC{$x \in V$}
%      \UIC{$x \in \Lambda_{\T, \nat}$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{\phantom{$n \in \mathbb{N}$}}
%      \UIC{$\zero\in \Lambda_{\T, \nat}$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$M \in \Lambda_{\T, \nat}$}
%      \UIC{$\suc\;M\in \Lambda_{\T, \nat}$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$M \in \Lambda_{\T,\nat}$}
%      \AXC{$N \in \Lambda_{\T,\nat}$}
%      \BIC{$(M\, N) \in \Lambda_{\T, \nat}$}
%    \end{prooftree}
%    \begin{prooftree}
%      \AXC{$M \in \Lambda_{\T, \nat}$}
%      \AXC{$x \in V$}
%      \AXC{$\tau \in \mathbb{T}$}
%      \TIC{$\lambda (x:\tau).\; M \in \Lambda_{\T, \nat}$}
%    \end{prooftree}
%  \end{multicols}
%\noindent and let $\mathbb{G} \defeq \{\nat\}$ with an addition set of typing
%rules:
%\begin{multicols}{2}
%\begin{prooftree}
%  \AXC{\phantom{$\Gamma$}}
%  \UIC{$\Gamma \vdash \zero : \nat$}
%\end{prooftree}
%\begin{prooftree}
%  \AXC{$\Gamma \vdash M : \nat$}
%  \UIC{$\Gamma \vdash \suc\:M : \nat$}
%\end{prooftree}
%\end{multicols}
%\noindent In this way, we can derive that every $\underline{n} \defeq
%\suc^n\;\zero$ has the type of $\nat$:
%\[
%  \Gamma \vdash \underline{n} : \nat
%\]
%
%However, it still remains to add primitive operations such as addition,
%multiplication, division, and so on with a proper set of reduction rules. As we
%have seen that recursions on natural numbers in untyped lambda calculus can be
%defined with the fixpoint operator. Something similar can be done:
%\begin{definition}
%  In addition to typing rules introduced so far, we add the following
%  \begin{prooftree}
%    \AXC{$\Gamma \vdash M : \nat$}
%    \AXC{$\Gamma \vdash M_0 : \sigma$}
%    \AXC{$\Gamma \vdash F : \nat \to (\sigma \to \sigma)$}
%    \TIC{$\Gamma \vdash \fold\;M_0\;F : \nat \to \sigma $}
%  \end{prooftree}
%  with additional reductions
%  \[
%    \fold\;M_0\;F\;\zero
%    \xrightarrow{\beta}
%    M_0
%    \quad\text{and}\quad
%    \fold\;M_0\;F\;(\suc\;M)
%    \xrightarrow{\beta}
%    F\;M\;(\fold\;F\;M).
%  \]
%\end{definition}
%\begin{definition}
%  \todo[inline]{define addition, multiplication using $\fold$}
%\end{definition}
%\begin{frame}{Remark}
%With the decidability of type checking, Preservation Theorem, Progress Theorem
%(a well-typed term is either a ``value'' or a reducible term), and the strong
%normalisation, we actually have exhibited a decidable evaluator of simply typed
%lambda calculus that always reduce a well-typed term of type~$\sigma$ to a value
%of type~$\sigma$.
%\end{frame}

\begin{frame}{Homework}
  \begin{enumerate}
    \item (2.5\%) Show the Preservation Theorem. \\
      \textbf{Hint.} Apply the Substitution Lemma if applicable.
    \item (2.5\%) Show the Progress Theorem.
    \item (2.5\%) Show that if $M$ is in normal form then $\normal\;M$ is derivable.
  \end{enumerate}

\end{frame}

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

\appendix
\section{Appendix Takahashi's Proof of confluence}

\begin{frame}{Confluence: Parallel reduction}
  Consider untyped $\lambda$-calculus.

  Let $M \parreduce N$ denote the \emph{parallel reduction} defined by
  \begin{multicols}{2}
    \begin{prooftree}
      \AXC{$\vphantom{M\reduce M}$}
      \UIC{$x \parreduce x$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$M \parreduce N$}
      \UIC{$\lambda x.\, M \parreduce \lambda x.\, N$}
    \end{prooftree}
    \columnbreak
    \begin{prooftree}
      \AXC{$M \parreduce M'$}
      \AXC{$N \parreduce N'$}
      \BIC{$M\;N \parreduce M'\;N'$}
    \end{prooftree}
    \begin{prooftree}
      \AXC{$M \parreduce M'$}
      \AXC{$N \parreduce N'$}
      \BIC{$(\lambda x.\, M)\;N \parreduce M'\subst{N'}{x}$}
    \end{prooftree}
  \end{multicols}
  For example,
  \[
    \underline{(\lambda x.\, (\lambda y.\, y)\;x)}\;\underline{((\lambda x.\, x)\;\false)}
    \parreduce
    \false
  \]
  because $(\lambda y.\,y)\;x \parreduce x$ and $(\lambda x.\,x)\;\false \parreduce \false$.
\end{frame}

\begin{frame}{Confluence: Properties of parallel reduction}
  \begin{lemma}
    \begin{enumerate}
      \item $M \parreduce M$ holds for any term~$M$,
      \item $M \onereduce N$ implies $M \parreduce N$, and
      \item $M \parreduce N$ implies $M \reduce N$.
    \end{enumerate}
  \end{lemma}
  Therefore, $M \parreduce^* N$ is equivalent to $M \reduce N$.
  \begin{lemma}[Substitution respects parallel reduction]
      $M \parreduce M'$ and $N \parreduce N'$ imply $M\subst{N}{x} \parreduce
      M'\subst{N'}{x}$.
  \end{lemma}
  \begin{proof}[Proof sketch]
    By induction on the derivation of $M \parreduce M'$.

  \end{proof}

\end{frame}

\begin{frame}{Complete development}
  The \emph{complete development} $M^*$ of a $\lambda$-term $M$ is defined by
  \begin{align*}
    x^*      & = x \\
    (\lambda x.\, M)^* & = \lambda x.\, M^* \\
    \left((\lambda x.\, M)\;N\right)^* & = M^*\subst{N^*}{x} \\
    (M\;N)^* & = M^*\;N^* && \text{ if $M \not\equiv \lambda x.\,M'$ }
  \end{align*}
  \begin{theorem}[Triangle property]
    If $M \parreduce N$, then $N \parreduce M^*$.
  \end{theorem}
  \begin{proof}[Proof sketch]
    By induction on $M \parreduce N$.

  \end{proof}
\end{frame}

\begin{frame}{Strip Lemma}
  \begin{theorem}
    If $L \parreduce^* M_1$ and $L \parreduce M_2$, then there exists $N$
    satisfying that $M_1 \parreduce N$ and $M_2 \parreduce^* N$, i.e.\
    \[
      \xymatrix{
        & L \ar@{=>}[rd] \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\
        M_1 \ar@{=>}[rd] & & M_2 \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\
            & N
      }
    \]
  \end{theorem}
  \begin{proof}[Proof sketch]
    By induction on $L \parreduce^* M_1$.
  \end{proof}
\end{frame}

\begin{frame}{Confluence}
  \begin{theorem}
    If $L \parreduce^* M_1$ and $L \parreduce^* M_2$, then there exists $N$ such that $M_1 \parreduce^* N$ and $M_2 \parreduce^* N$.
    \[
      \xymatrix{
        & L \ar@{=>}[rd]^(.8){*}_(.8){\beta} \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\
        M_1 \ar@{=>}[rd]^(.8){*}_(.8){\beta} & & M_2 \ar@{=>}[ld]^(.8){*}_(.8){\beta} \\
            & N
      }
    \]
  \end{theorem}
  \begin{corollary}
    The confluence of $\reduce$ holds.

  \end{corollary}

\end{frame}
\end{document}


% !TEX root = ../../../proposal.tex

%\section{Introduction}
%
%Diffie-Hellman key exchange is one of the most common public-key cryptographic
%methods in use in the Internet. It is a fundamental building block for IPsec,
%SSH, and TLS\@. In the textbook presentation of finite field Diffie-Hellman,
%Alice and Bob agree on a large prime $p$ and an integer $g$ modulo $p$. Alice
%chooses a secret integer $x_a$ and transmits a public value $g^{x_a} \bmod p$;
%Bob chooses a secret integer $x_b$ and transmits his public value $g^{x_b}
%\bmod p$. Both Alice and Bob can reconstruct a shared secret $g^{x_a x_b} \bmod
%p$, but the best known way for a passive eavesdropper to reconstruct this
%secret is to compute the discrete log of either Alice or Bob's public value.
%Specifically, given $g$, $p$, and $g^x \bmod p$, an attacker must calculate
%$x$.

This chapter is adapted from a joint publication with Valenta et al.\ that
originally appeared in the proceedings of the 21st Network and Distributed
System Security Symposium (NDSS '17)~\cite{subgroup-2017}.

In order for the discrete log problem $\bmod p$ to be hard, Diffie-Hellman parameters
must be chosen carefully. A typical recommendation is that $p$ should be a
``safe'' prime, that is, that $p = 2q+1$ for some prime $q$, and that $g$
should generate the group of order $q$ modulo $p$. For $p$ that are not safe,
the group order $q$ can be much smaller than $p$. For security, $q$ must still
be large enough to thwart known attacks, which for prime $q$ run in time
$O(\sqrt{q})$. A common parameter choice is to use a 160-bit $q$ with a
1024-bit $p$ or a 224-bit $q$ with a 2048-bit $p$, to match the security level
under different cryptanalytic attacks. Diffie-Hellman parameters with $p$ and
$q$ of these sizes were suggested for use and standardized in DSA
signatures~\cite{fips186}. For brevity, we will refer to these non-safe primes as
DSA primes, and to groups using DSA primes with smaller values of $q$ as  DSA
groups.

A downside of using DSA primes instead of safe primes for Diffie-Hellman is
that implementations must perform additional validation checks to ensure the
key exchange values they receive from the other party are contained in the
correct subgroup modulo $p$. The validation consists of performing an extra
exponentiation step. If implementations fail to validate, a 1997 attack of Lim
and Lee~\cite{lim-1997} can allow an attacker to recover a static exponent by
repeatedly sending key exchange values that are in very small subgroups. We
describe several variants of small subgroup confinement attacks that allow an
attacker with access to authentication secrets to mount a much more efficient
man-in-the-middle attack against clients and servers that do not validate group
orders. Despite the risks posed by these well-known attacks on DSA groups, NIST SP 800-56A, ``Recommendations
for Pair-Wise Key Establishment Schemes Using Discrete Logarithm
Cryptography''~\cite{sp800} specifically recommends DSA group parameters
for Diffie-Hellman, rather than recommending using safe primes. RFC
5114~\cite{rfc5114} includes several DSA groups for use in IETF standards.

We observe
that few Diffie-Hellman implementations actually validate subgroup orders, in spite of the fact
that small subgroup attacks and countermeasures are well-known and specified
in every standard suggesting the use of DSA groups for Diffie-Hellman, and DSA
groups are commonly implemented and supported in popular protocols. For some protocols, including
TLS and SSH, that enable the server to unilaterally specify the group used for
key exchange, this validation step is not possible for clients to perform with
DSA primes---there is no way for the server to communicate to the client the
intended order of the group. Many standards involving DSA groups further
suggest that the order of the subgroup should be matched to the length of the
private exponent.  Using shorter private exponents yields faster exponentiation
times, and is a commonly implemented optimization. However, these standards
provide no security justification for decreasing the size of the subgroup to
match the size of the exponents, rather than using as large a subgroup as
possible. We discuss possible motivations for these recommendations
later in the paper.

We conclude that adopting the Diffie-Hellman group recommendations from RFC
5114 and NIST SP 800-56A may create vulnerabilities for organizations using
existing cryptographic implementations, as many libraries allow
user-configurable groups but have unsafe default behaviors.  This highlights the
need to consider developer usability and implementation fragility when designing
or updating cryptographic standards.

\paragraph{Our Contributions}
We study the implementation landscape of Diffie-Hellman from several
perspectives and measure the security impact of the widespread
failure of implementations to follow best security practices:
\begin{itemize}
\item We summarize the concrete impact of small-subgroup confinement attacks
    and small subgroup key recovery attacks on TLS, IKE, and SSH handshakes.
\item We examined the code of a wide variety of cryptographic libraries to
  understand their implementation choices. We find feasible full private
  exponent recovery vulnerabilities in OpenSSL and the Unbound DNS resolver,
  and a partial private exponent recovery vulnerability for the parameters used
  by the Amazon Elastic Load Balancer. We observe that \emph{no} implementation
  that we examined validated group order for subgroups of order larger than two
  by default prior to January 2016, leaving users potentially vulnerable to
  small subgroup confinement attacks.
  %In addition, we observed that nearly every implementation uses short
  %exponents by default, and several use ephemeral-static keys.
\item We performed Internet-wide scans of HTTPS, POP3S, SMTP with STARTTLS,
  SSH, IKEv1, and IKEv2, to provide a snapshot of the deployment of DSA groups
  and other non-``safe'' primes for Diffie-Hellman, quantify the incidence of
  repeated public exponents in the wild, and quantify the lack of validation
  checks even for safe primes.
  %Our work adds to the growing literature of empirical
  %studies of cryptographic implementation behavior on the Internet.
%\item We surveyed the protocol-level susceptibility to small subgroup attack
%    scenarios for TLS, IKE, and SSH.  While several of these attacks are well
%    known or have been described elsewhere, we are unaware of a comprehensive
%    reference that summarizes the state of protocol landscape from this
%    perspective. We describe variants of these attacks that we have not
%    seen elsewhere.
\item We performed a best-effort attempt to factor $p-1$ for all non-safe primes that we found in the
    wild, using \textasciitilde100,000 core-hours
    of computation. Group 23 from RFC 5114, a 2048-bit prime, is particularly vulnerable
    to small subgroup key recovery attacks; for TLS a full key recovery
    requires $2^{33}$ online work and $2^{47}$ offline work to recover a
    224-bit exponent.
\end{itemize}

\paragraph{Disclosure and Mitigations}
We reported the small subgroup key recovery vulnerability to OpenSSL in January
2016~\cite{asanso-unredacted}. OpenSSL issued a patch to add additional
validation checks and generate single-use private exponents by
default~\cite{cve-2016-0701}. We reported the Amazon load balancer
vulnerability in November 2015. Amazon responded to our report informing us
that they have removed Diffie-Hellman from their recommmended ELB security
policy, and have reached out to their customers to recommend that they use
these latest policies. Based on scans performed in February and May 2016, 88\%
of the affected hosts appear to have corrected their exponent generation
behavior. We found several libraries that had vulnerable combinations of
behaviours, including Unbound DNS, GnuTLS, LibTomCrypt, and Exim. We disclosed
to the developers of these libraries. Unbound issued a patch, GnuTLS
acknowledged the report but did not patch, and LibTomCrypt did not respond.
Exim responded to our bug report stating that they would use their own
generated Diffie-Hellman groups by default, without specifying subgroup order
for validation~\cite{exim-blog,exim-bug-report}.  We found products from Cisco,
Microsoft, and VMWare lacking validation that key exchange values were in
the range $(1,p-1)$. We informed these companies, and discuss their responses
in Section~\ref{sec:tls-measurements}.

%In May 2016, we submitted bug reports to the developers
%of several applications and libraries that had vulnerable combinations of
%behaviours, including the Unbound DNS resolver, the Exim mail server, and the
%LibTomCrypt and GnuTLS libraries.  Developers of the Unbound DNS resolver
%informed us that a fix would be included in the next release. The GnuTLS
%developers acknowledged the issue but have not yet applied patches. In October
%2016, Exim responded to our bug report stating that they would use their own
%generated Diffie-Hellman groups by default, without specifying subgroup order
%for validation~\cite{exim-blog,exim-bug-report}.  There was no response from
%LibTomCrypt. We found that products of several noteworthy companies, including
%Microsoft, Cisco, and VMWare, do not validate that a key exchange value is in
%the range $(1, p-1)$.  We informed these companies and others about this
%missing check, and discuss their responses in
%Section~\ref{sec:tls-measurements}.


\documentclass{article}
\usepackage{amsmath}
%\usepackage{geometry}
\usepackage[utf8]{inputenc}

\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}

\begin{document}

\section{Power of point}
The power of point $P$ with respect to circle with centre $O$ and radius $r$ is defined as
$$ p= PO^2-r^2 $$

\textbf{Theorem 1: }
For any line through point $P$ which intersects the circle at points $A$ and $B$
$$p= PA \times PB$$

\textbf{Theorem 2: }
For tangent $PC$ where $C$ is tangent point
$$p=PC^2$$

\begin{enumerate}
\item
Square $ABCD$ of side length $a$ has a circle inscribed in it. Let $M$ be the midpoint of $AB$ Find the length of that portion of the segment $MC$ that lies outside of the circle.

\item  % Prasolov 3.11
Line $OA$ is tangent to a circle at point $A$ and chord $BC$ is parallel to $OA$. Lines	$OB$ and $OC$ intersect the circle for the second time at points $K$ and $L$, respectively. Prove
that line $KL$ divides segment $OA$ in halves.

\item % LP 2003 XI-3
We have a triangle $ABC$. Points $K$, $L$ and $M$ are chosen on the sides $BC$, $AC$ and $AB$, such that $AK$, $BL$ and $CM$ intersect in one point. We know that $ALKB$ and $BMLC$ are cyclic quadrilaterals. Show that $AMKC$ is also a cyclic quadrilateral.

\item % Prasolov 3.12
On the longer diagonal $AC$ of parallelogram $ABCD$ point $M$ is chosen, such that $BCDM$ is a cyclic quadrilateral. Show that $BD$ is tangent to circumcircles of triangles $AMD$ and $AMB$.

\item % IMO shortlist 2013
Let $ABC$ be a triangle with $\angle B > \angle C$. Let $P$ and $Q$ be two different points on line $AC$ such that $\angle PBA = \angle QBA = \angle ACB$ and $A$ is located between $P$ and $C$. Suppose that there exists an interior point $D$ of segment $BQ$ for which $PD=PB$. Let the ray $AD$ intersect the circle $ABC$ at $R \ne A$. Prove that $QB = QR$.
\end{enumerate}

\newpage
\section{Radical axis}
Radical axis is the locus of points at which tangents drawn to both circles are equal.

\textbf{Theorem 1: }
The power of points on radical axis is equal with respect to both circles.

\textbf{Theorem 2: }
Radical axis is a line.

\textbf{Theorem 3: }
The three radical axes for three circles intersect in one point called the radical centre.

\begin{enumerate}

\item %Prasolov 3.58a
Prove that the midpoints of the four common tangents to two non-intersecting circles lie on one line.

\item %puutujakuusnurga diagonaalid Prasolov 3.66
Prove that the diagonals $AD$, $BE$ and $CF$ of circumscribed hexagon $ABCDEF$ intersect in one point. (Brianchon theorem)

\item % TVV	2016 6
Circles $k_1$ and $k_2$ intersect at points  $M$ and $N$. Line $l$ intersects circle $k_1$ at points $A$ and $C$ and circle $k_2$ at points $B$ and $D$, such that points $A$,  $B$, $C$ and $D$ lie on the line $l$ in that order. Let $X$ be such point on line $MN$ that $M$ lies between $X$ and $N$. Rays $AX$ and $BM$ intersect at point $P$, rays $DX$ and $CM$ at point $Q$. Prove that $PQ \parallel l$.

\item % BT treening 2009-5
Point $E$ is chosen on the median $CD$ of triangle $ABC$. Line $AB$ is tangent to circle $c_1$ at point $A$ and to circle $c_2$ at point $B$ such that both circles go through point $E$. The second intersection of $c_1$ and  $AC$ is $M$. The second intersection of $c_1$ and $BC$ is $N$. Prove that tangent lines to circles $c_1$ and $c_2$ at points $M$ and $N$ respectively intersect on line $CD$.

\item % 3.6l
Three circles intersect pairwise at points $A_1$ and $A_2$, $B_1$ and $B_2$, $C_1$ and $C_2$. Prove that $A_1B_2 \times B_1C_2 \times C_1A_2 = A_2B_1 \times B_2C_1 \times C_2A_1$.

\item % Prasolov 3.60
The extensions of sides $AB$ and $CD$ of quadrilateral $ABCD$ meet at point $F$ and
the extensions of sides $BC$ and $AD$ meet at point $E$. Prove that the circles with diameters
$AC$, $BD$ and $EF$ have a common radical axis and the orthocenters of triangles $ABE$, $CDE$,
$ADF$ and $BCF$ lie on it.

\end{enumerate}

\newpage
\section*{Hints}

\textbf{Power of point}

\begin{enumerate}
	\item Write down power of point $C$.
	\item Identify similar triangles and write down power of the intersection of $KL$ and $AB$
	\item Write down the power of the intersection for each circle
	\item Write down the power of the intersection of the diagonals
	\item Prove that quadrilateral $DRCQ$ is cyclic
\end{enumerate}

 \noindent
 \textbf{Radical axis}

\begin{enumerate}
	\item
	\item Find three circles for which the diagonals are radical lines for.
	\item What's the power of $X$ with respect to each circle? What other points lie on the circle $PQM$?
	\item Prove that quadrilateral $ABNM$ is cyclic.
	\item Where's the radical centre? Find 3 pairs of similar triangles.
	\item For each triangle, what's the power of orthocentre with respect to each circles?
\end{enumerate}

\end{document}


\chapter{Specifying your geometry with a Z-Matrix}

A Z-Matrix is a convenient way to specify the geometry of a molecule or
crystal in terms of bond lengths, bond angles, and dihedral angles.
There are several styles of Z-matrix used in various programs,
\calcprog\ uses a format similar to that used in Gaussian.
This is intended to be a brief introduction to how a Z-matrix works.

If you already know how to use a Z-matrix, here's all you need to know
about the implementation in \calcprog:
\begin{itemize}
\item The first atom is put at the origin.
\item The second atom is put along the Z axis.
\item The third atom is in the XZ plane.
\item Dihedrals are evaluated using the right hand rule.
\end{itemize}

The easiest way to explain a Z-matrix is to show one and then explain
it, so that's what we'll do.  Before we start, however, we need to
briefly define a dihedral angle.  A dihedral is specified by 4
atoms, we'll call them A, B, C, and D.  The dihedral A--B--C--D is the
angle between the plane defined by A--B--C and the plane defined by
B--C--D.
Here's an alternative explanation: the dihedral A--B--C--D is the angle
between the lines C--D and B--A if you are looking down the line C--B.
There's one more piece of information we need to fully understand the
dihedral: there is a handedness associate with them.  If you think
about it, looking down the line C--B there are two different angles
between lines C--D and B--A: $\theta$ and 360-$\theta$.  The dihedrals
in \calcprog\ are defined using the right hand rule:  Take your right
hand and point the thumb down the line C--B, now align your fingers
with the line C--D, curling your fingers shows the direction in which
the dihedral angle is measured.
%
This is all about a million times easier to understand using a
picture, here's a picture demonstrating both views of dihedrals and
their handedness.
%
\begin{center}
\epsfig{file=dihedral.eps,width=5.0in}
\end{center}
%

With that definition under our belt, here's the Geometry specification for a
square pyramidal (CH$_3$)BiI$_4$ fragment, where the CH$_3$ group is
along the Z axis and the Bi and four I's lie in the XY plane:

\shrinkspacing
\begin{verbatim}
Geometry Z Matrix
9
1 Bi
2 C 1 2.1
3 I 1 2.7  2  90.0
4 I 1 2.7  2  90.0  3  90.0
5 I 1 2.7  2  90.0  3 180.0
6 I 1 2.7  2  90.0  3 270.0
7 H 2 1.1  1 109.5  3   0.0
8 H 2 1.1  1 109.5  3 120.0
9 H 2 1.1  1 109.5  3 240.0
\end{verbatim}
\resumespacing

Let's look at the first few entries in more detail.
\begin{enumerate}
\item The first atom is a Bi and it's placed at the origin.
Cartesian:  (0 0 0).

\item Atom two is a C.  It's placed on the Z axis, 2.1 \AA\ away from
atom 1. Cartesian: (0 0 2.1).

\item Atom three is an I.  It's placed 2.7 \AA\ away from atom 1 and
the angle between atoms 3--1--2 in the XZ plane is 90.0 degrees.
Cartesian: (2.7 0 0).

\item Atom four is an I.  It's placed 2.7 \AA\ away from atom 1, the
angle 4--1--2 is 90 degrees.  This angle puts us in the XY plane.  At
this point we know that atom 4 lies on a circle in the XY plane with
radius 2.7 \AA. The
dihedral 4--1--2--3 (90 degrees) tells us where on the circle we are.
This dihedral is particularly easy to see:  if we look down the bond
2--1 (which is looking down the Z axis), the angle between the bond
3--1 and the bond 4--1 is 90 degrees.  So atom 4 lies on the Y axis.
Taking the right-handedness of dihedrals into account, we know that
atom 4 lies on the negative Y axis.  Cartesian (0 -2.7 0).

\item Atom five is an I.  It's 2.7 \AA\ away from atom 1, making an
angle of 90 degrees with 2 and a dihedral of 180 with 3.  This puts us
on the negative X axis.  Cartesian (-2.7 0 0).

\end{enumerate}

If you find the handedness of dihedrals confusing, just play around
with a couple of molecules defined using Z matrices, you'll get the
hang of it fairly quickly.


%% LyX 2.0.3 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[twoside,english]{paper}
\usepackage{lmodern}
\renewcommand{\ttdefault}{lmodern}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=3cm,bmargin=2.5cm,lmargin=2cm,rmargin=2cm}
\usepackage{color}
\usepackage{babel}
\usepackage{float}
\usepackage{bm}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{esint}
\usepackage[unicode=true,pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 0},backref=false,colorlinks=false]
 {hyperref}
\usepackage{breakurl}
\usepackage{mathrsfs}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\numberwithin{equation}{section}
\numberwithin{figure}{section}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{babel}

\@ifundefined{showcaptionsetup}{}{%
 \PassOptionsToPackage{caption=false}{subfig}}
\usepackage{subfig}
\makeatother

\begin{document}

\title{Combining evolution and DIS operators}

\maketitle

\section{The structure of the observables}

In all cases the inclusive DIS structure functions are conveniently
expressed in terms of PDF combinations in the so-called physical basis
$\{q_i^\pm\}$, with $q_i^\mp = q_i\pm\overline{q}_i$, where $q_i$ and
$\overline{q}_i$ are the PDFs of the $i-th$ quark-flavour, with
$i=u,d,s,c,b,t$, and its antiflavour, respectively. Schematically, a
DIS structure function can be written as:
\begin{equation}
F = C_g g + \sum_{i}\left(C_i^+q_i^++C_i^-q_i^-\right)\,,
\end{equation}
being $C$ the appropriate coefficient functions. Conversely, the
evolution of PDFs is usually compute in the so-called QCD evolution
basis $\{d_i^\pm\}$, with $d^+_1=\Sigma$, $d^+_2=-T_3$, $d^+_3=T_8$,
$d^+_4=T_{15}$, $d^+_5=T_{24}$, and $d^+_6=T_{35}$ and $d^-_1=V$,
$d^-_2=-V_3$, $d^-_3=V_8$, $d^-_4=V_{15}$, $d^-_5=V_{24}$, and
$d^-_6=V_{35}$. The gluon remains unchanged.
\begin{equation}
g = \Gamma_{gg}g_0 + \Gamma_{gq}d_{1,0}^+
\end{equation}
while:
\begin{equation}
\begin{array}{rcl}
d_i^\pm&=&\displaystyle \theta_{i2}\theta(Q-m_i)\Gamma^{\pm}d_{i,0}^\pm+\theta(m_i-Q)
\left\{\begin{array}{ll}
\Gamma_{qq}d_{1,0}^++\Gamma_{qg}g_0&\quad\mbox{for }+\\
\Gamma^vd_{1,0}^-&\quad\mbox{for }-
\end{array}\right.\\
\\
&=&\left\{\begin{array}{l}
\Gamma_{qq}d_{1,0}^++\Gamma_{qg}g_0\\
\Gamma^vd_{1,0}^-
\end{array}\right.+\theta(Q-m_i) \left[\theta_{i2}\Gamma^{\pm}d_{i,0}^\pm-
\left\{\begin{array}{l}
\Gamma_{qq}d_{1,0}^++\Gamma_{qg}g_0\\
\Gamma^vd_{1,0}^-
\end{array}\right.\right]\,.
\end{array}
\end{equation}
so that:
\begin{equation}
\begin{array}{rcl}
\displaystyle  q_i^- &=& \displaystyle \delta_{i1}
  \Gamma^vd_{1,0}^-+\Gamma^{\pm}\sum_{j=2}^6
  \theta_{ji}\frac{1-\delta_{ij}j}{j(j-1)}
  \theta(Q-m_j)d_{j,0}^\pm
\end{array}
\end{equation}

Therefore, we need to relate these two bases. This is done through the
linear transformation:
\begin{equation}\label{TranformationBella}
q_i^\pm = \sum_{j=1}^6M_{ij}d^\pm_j\,,
\end{equation}
where the trasformation matrix $M_{ij}$ can be written as:
\begin{equation}\label{TransDef}
\begin{array}{l}
\displaystyle M_{ij}=\theta_{ji}\frac{1-\delta_{ij}j}{j(j-1)}\quad j\geq 2\,,\\
\\
\displaystyle M_{i1} = \frac{1}{6}\,,
\end{array}
\end{equation}
with $\theta_{ji}=1$ for $j\geq i$ and zero otherwise.

Using eq.~(\ref{TranformationBella}) we can make the following
identifications:
\begin{equation}
D^{\pm} = q_{2j-1}^\pm\quad\mbox{and}\quad U^{\pm} =
q_{2j}^\pm\,,\quad j=1,2,3\,,
\end{equation}
so that we can write:
\begin{equation}
F^\pm=
\frac12\sum_{i=1}^3\sum_{j=1}^3|V_{2i,(2j-1)}|^2\left[C_\pm\left(q_{2j-1}^\pm
    \pm q_{2i}^\pm\right) + 4P^{\pm} C_g g\right]\,.
\end{equation}
Using the definition of $M_{ij}$ in eq.~(\ref{TransDef}), we can
rewrite $F^{\pm}$ in terms of PDFs in the evolution basis as:
\begin{equation}\label{eq:decompF2L}
F^\pm=
\sum_{i=1}^3\sum_{j=1}^3|V_{2i,(2j-1)}|^2 F_{ij}^\pm\,,
\end{equation}
with:
\begin{equation}\label{F2Ldef}
F_{ij}^\pm=
C_g 2P^\pm g
+
C_\pm^{\rm S} P^\pm \frac16 d_1^\pm
+ C_\pm\sum_{k=2}^6\frac{\theta_{k,2j-1}(1-\delta_{2j-1,k}k)\pm \theta_{k,2i}(1-\delta_{2i,k}k) }{2k(k-1)}d_k^\pm\,.
\end{equation}

Eq.~(\ref{F2Ldef}) is valid only for $F_2$ and $F_3$. In order to
obtain a similar equation also for $F_3$, one needs to change sign to
the antiquark distributions, $i.e.$
$\overline{q}_i\rightarrow - \overline{q}_i$. In the QCD evolution
basis, this has the consequence of exchanging the $T$-like
distributions with the $V$-like ones, that is to say
$d_k^+\leftrightarrow d_k^-$. It is the easy to see that:
\begin{equation}
F_3^\pm=
\sum_{i=1}^3\sum_{j=1}^3|V_{2i,(2j-1)}|^2 F_{3,ij}^\pm\,,
\end{equation}
with:
\begin{equation}\label{F3def}
F_{3,ij}^\pm=
C_g 2P^\pm g
+
C_\pm^{\rm S} P^\mp \frac16 d_1^\pm
+ C_\pm\sum_{k=2}^6\frac{\theta_{k,2j-1}(1-\delta_{2j-1,k}k)\mp \theta_{k,2i}(1-\delta_{2i,k}k) }{2k(k-1)}d_k^\pm\,.
\end{equation}

It is now useful to consider the inclusive structure functions and
exploit the unitarity of the CKM matrix elements $V_{UD}$:
\begin{equation}
\sum_{i=1}^3|V_{2i,(2j-1)}|^2 = \sum_{j=1}^3|V_{2i,(2j-1)}|^2 = 1\quad\Rightarrow\quad \sum_{i=1}^3\sum_{j=1}^3|V_{2i,(2j-1)}|^2 = 3\,.
\end{equation}
Summing over $i$ and $j$ in eq.~(\ref{eq:decompF2L}) and using
eq.~(\ref{F2Ldef}), one obtains:
\begin{equation}
F^\pm=
C_g 6P^\pm g
+
C_\pm^{\rm S} P^\pm \frac12 d_1^\pm
+ \frac12 C_\pm\sum_{k=2}^6 d_k^\pm\sum_{l=1}^6(\pm 1)^{l+1}M_{lk}\,.
\end{equation}
Considering separately $F^+$ and $F^-$ and using
eq.~(\ref{eq:properties}), one finds:
\begin{equation}
F^+= C_g 6g + C_+^{\rm S} \frac12 d_1^+
\end{equation}
and:
\begin{equation}
F^-= \frac12 C_-\sum_{k=2}^6\left[\frac{P^+}{k-1}-\frac{P^-}{k}\right]d_k^-\,,
\end{equation}
with the even/odd projectors defined as:
\begin{equation}
P_k^{\pm} = \frac{1\pm(-1)^k}{2}\,.
\end{equation}

It should be pointed out that such simple expressions (independent of
the CMK matrix elements) is achievable only if it is possible to
factorize the non-singlet coefficient functions as implicitly done in
eqs.~(\ref{F2Ldef}) and~(\ref{F3def}). In fact, this is possible only
in the ZM case in which the coefficient functions of each PDF
combination is the same.

For $F_3$ we find:
\begin{equation}
F_3^+= C_g 6g + C_-^{\rm S} \frac12 d_1^-
\end{equation}
and:
\begin{equation}
F_3^-= \frac12 C_+\sum_{k=2}^6\left[\frac{P_k^+}{k-1}-\frac{P_k^-}{k}\right]d_k^+\,.
\end{equation}

\end{document}


\chapter{Introduction}
\label{ch_msvst_intro}

Several techniques have been proposed in the literature to estimate Poisson intensity in 2D. A major class of methods adopt a multiscale bayesian framework specifically tailored for Poisson data~\citep{Nowak2000}, independently initiated by \citet{wave:timmermann99} and \citet{Kolaczyk1999}. \citet{Lefkimmiatis} proposed an improved bayesian framework for analyzing Poisson processes, based on a multiscale representation of the Poisson process in which the ratios of the underlying Poisson intensities in adjacent scales are modeled as mixtures of conjugate parametric distributions. Another approach includes preprocessing the count data by a variance stabilizing transform (VST) such as the Anscombe \citep{rest:anscombe48} and the Fisz \citep{rest:nason04} transforms, applied respectively
in the spatial~\citep{rest:donoho93_2} or in the wavelet domain~\citep{Fryzlewicz2004}. The transform reforms the data so that the noise approximately becomes Gaussian with a constant variance. Standard techniques for independant identically distributed Gaussian noise are then used for denoising. \citet{starck:zhang07} proposed a powerful method called Multi-Scale Variance Stabilizing Tranform (MS-VST). It consists in combining a VST with a multiscale transform (wavelets, ridgelets or curvelets), yielding asymptotically normally distributed coefficients with known variances. The choice of the multi-scale method depends on the morphology of the data. Wavelets represent more efficiently regular structures and isotropic singularities, whereas ridgelets are designed to represent global lines in an image, and curvelets represent efficiently curvilinear contours. Significant coefficients are then detected with binary hypothesis testing, and the final estimate is reconstructed with an iterative scheme. In \citet{Starck09:fermi3d}, it was shown that sources can be detected in 3D FERMI LAT data (2D+time or 2D+energy) using a specific 3D extension of the MS-VST.
 \citet{Schmitt} proposed a method for Poisson intensity estimation on spherical data called Multi-Scale Variance Stabilizing Transform on the Sphere (MS-VSTS). This MS-VSTS (Multi-Scale Variance Stabilizing Transform on the Sphere) package offers a Poisson denoising method on the sphere, designed for Fermi photon counts maps.
This method is based on the MS-VST~\citep{starck:zhang07} and on multi-scale transforms on the sphere \citep{starck2006,inpainting:abrial06,starck:abrial08}.
 Chapter~\ref{ch_msvsts} introduces the MS-VSTS.
 Chapter~\ref{ch_denoising} applies the MS-VSTS to spherical data restoration. Chapter~\ref{ch_inpainting} applies the MS-VSTS to inpainting. Chapter~\ref{ch_background} applies the MS-VSTS to background extraction. An accurate description of the IDL routines that makeup this package is given in Chapter~\ref{ch_idlproc}. An extension to multichannel denoising and deconvolution is given in Chapter~\ref{ch_multichannel} Conclusions are drawn in Chapter~\ref{ch_conclusion}. All experiments were performed on HEALPix maps with $nside=128$~\citep{pixel:healpix}, which corresponds to a good pixelisation choice for  data such as the GLAST/FERMI resolution. The performance of the method is not dependent on the nside parameter. For a given data set, if nside is small, it just means that we don't want to investigate the finest scales. If nside is large, the number of counts per pixel will be very small, and we may not have enough statistics to get any information at the finest resolution levels. But it will not have any bad effect on the solution. Indeed,  the finest scales
will be smoothed, since our algorithm will not detect any significant wavelet coefficients in the finest scales. Hence, starting with a fine pixelisation (i.e. large nside), our method will provide a kind of automatic binning, by thresholding wavelets coefficients at scales and at spatial positions where the number of counts is not sufficient.

% \newpage


Plasma Cash is a plasma construction with much less user data checking \cite{plasma_cash}. It
utilizes Non-Fungible-Tokens (NFTs) to reduce the
user checking requirements to only the NFTs that they own\footnote{Plasma-MVP requires users to be constantly monitoring the plasmachain for fraudulent state transitions, while Plasma Cash requires that users only watch the mainchain about fraudulent exits of coins that they own}. The system's security relies on users fully authenticating a coin's history before accepting it as a payment by utilizing Sparse Merkle Trees, which allow the efficient verification of inclusion and non-inclusion of a transaction in a block, as explained in the next subsection.

\subsection{Sparse Merkle Trees}

A Merkle Tree is a data structure which allows to succinctly commit to a dataset and prove the inclusion of a part of the committed dataset in $O(log_2(N))$ steps instead of $O(N)$, where $N$ is the number of elements in the dataset, via \textit{Merkle Proofs}. The committed value is called a \textit{Merkle Root}. A Sparse Merkle Tree (SMTs) \cite{sparse_merkle_trees} is an ordered merkle tree, where each element of the dataset is placed at the leaf with the index corresponding the element's index in the dataset. If an element of the dataset was not included in the Merkle Root, its leaf is set to a special default value.

The inclusion of a transaction spending a coin in a block can be efficiently proven through a Merkle Proof. The same method can be used to prove the non-inclusion of a spend of a coin in a block. A coin can only be spent once per block because only 1 transaction at its slot can ever exist. If a coin was not spent, the leaf is set to the hash of 0. A visual representation of the above is given in Figure \ref{fig:smt}.

\input{figures/smt}

Further optimizations can be done to the verifier
as suggested in \cite{smt_compact_proofs} by precomputing the default values of
the SMT and by introducing a bitfield in the proof which acts as a
switch between choosing the next 32 bytes during the verification from the
proof or from the SMT's default hashes. A reasonable estimation for a block
containing 2378 transactions results in proof sizes being 320 bytes, compared
to normal proofs which would be 2048 (64 * 32) bytes, for a SMT of size 64.

\subsection{Periodic Merkle Commitments}
All Plasma designs rely on a plasmachain operator that commits the
Merkle Root of each generated block to the mainchain. If a Proof of Stake system is used, publishing a block must be accompanied by a number of validator signatures, exceeding a pre-agreed threshold.
Whenever a Plasma Block's root is committed to the rootchain, all valid
transactions in that given block can be considered finalized upon availability of the related witness data\footnote{By witness data we refer to the merkle proof of inclusion of that coin in the specified block} for the inclusion of these transactions. Since only the
block root is committed to the mainchain instead of all the included
transactions, plasma can bundle together any number of transactions, the only limit to the number of included transactions being the size of the plasma block. The minimum finality time that
can be achieved is the block time of the rootchain (15 seconds in Ethereum).
Given that every block must be published to the mainchain, the operational costs for this process can become large. Operators can be expected to trade bigger finality time for less maintenance costs by committing block roots less often.

\subsection{Non Fungible Tokens and Depositing a Coin}

When value gets deposited in the plasma smart contract, a unique id and metadata key-value pair gets generated for that token and is saved in the contract's storage. The id is the unique serial number of the coin, and is what makes the deposited coin non-fungible. As a result, depositing 5 ETH two times creates two coins which have their own unique transaction history and are independent from each other. The unique id can also be thought of as a serial number, compared to fiat cash money.

After the coin gets saved in the smart contract, a block containing only the deposit transaction is appended to the plasmachain\footnote{Deposit blocks including only one transaction is an optimization, https://ethresear.ch/t/one-plasma-cash-block-per-deposit-why/2674/}.

% Figure \ref{fig:plasma_cash_deposit} illustrates the process for depositing a
% coin (Ether, ERC20, ERC721, or any other standard) to a Plasma Cash chain.
%
% \input{protocols/prot_deposit}
\subsection{Transferring a Coin and Verifying its History}
\label{verify_coin_history}
Each coin in Plasma Cash has its own unique coin history. A coin receiver  must
verify that the coin they are receiving has a valid history in order to accept
it. A coin with invalid history is counterfeit
and cannot be withdrawn safely. In order to validate a coin's
history, a set of merkle proofs of inclusion
and non-inclusion for the coin since its initial deposit must be sent to the receiver. The receiver can then
proceed to verify that there were no invalid spends of the coin in the coin's
history. This is done by verifying that the proofs of inclusion and non-inclusion are valid
against the plasma block merkle roots that were published to the mainchain.

This imposes a heavy storage and bandwith burden on senders that
want to transfer a coin. Specifically, the proofs required to send a coin are
$O(t * log_{2}(N))$ where $t$ is the number of blocks since a coin's deposit and $N$ is the number
of coins the Plasma Cash chain supports.

In a real world scenario where a buyer wants to buy a product from a vendor the
following is expected to happen, in a non-fraudulent case:
\begin{enumerate}
    \item Buyer broadcasts transaction giving ownership of their coin to the
        seller
    \item Transaction gets included in a block and witness data about its inclusion is made available
    \item Buyer verifies that the transaction was included in the block
    \item Buyer sends the proofs of inclusion and non-inclusion to the vendor.
    \item Vendor verifies the history of the coin along with the correct inclusion of the coin's transaction in the block.
    \item Vendor gives the product to buyer
\end{enumerate}

A transaction is a tuple: \texttt{Tx(slot, parentBlock, newOwner, prevOwnerSignature)}. A transaction that was included in a block is a combination of the previous tuple and a merkle proof for the block: \texttt{IncludedTx(tx, blkNumber, proof)}.

The algorithm for verifying the history of a coin is given in Figure \ref{fig:transfer_coin}

\input{protocols/prot_transfer_coin}

\subsection{Exiting and Withdrawing a Coin} \label{exiting_withdrawing}
As described in Section \ref{ch2:classic_plasma}, exits are the mechanism by
which a coin can be withdrawn from the plasmachain, and allow it to be
transferred back to its owner's account on the mainchain.

Starting an exit for a coin requires providing the transaction that gave the
exitor ownership of the coin signed by the previous owner in the coin's history, \textbf{tx}, as well
as a direct ancestor of that transaction (the reason the parent transaction must also be provided is explained in Section 4). Merkle proofs of inclusion need to also be provided for both transactions.

\begin{figure}[H]
	\makebox[\linewidth]{
		\scalebox{0.6}{
		\includegraphics[width=\linewidth]{figures/coin_exit.pdf}
		}
		}
	\caption{
        Alice deposits a coin from the mainchain to the plasmachain in Block 1. Alice sends the coin to Bob in Block 2. Bob verifies the inclusion of the coin in Block 2. Block 3 gets submitted, without including the coin. Bob sends the coin to Charlie in Block 4. Charlie has to verify the inclusion of the coin in Block 1 and 2, and the non-inclusion of the coin in Block 3.
        In order for Charlie to exit the coin received by Bob he has to provide
        the signed transaction from Bob as well as a direct ancestor, in this
        case the transaction from Alice to Bob. Charlie also needs to supply
        merkle proofs of inclusion for both of these transactions at their
        respective blocks.
	}
    \label{fig:exit_lifetime}
\end{figure}

A coin can be modelled by a state machine. After starting an exit, the coin transitions to the \texttt{EXITING}
state. After the challenge (or maturity) period passes,
the coin's exit can be finalized and it can transition to the \texttt{EXITED}
state, from which it can be withdrawn to a user's wallet,
as shown in Figure
\ref{fig:exit_state_machine}. Figure \ref{fig:exit_lifetime} illustrates the
lifetime of an exit from its initialization to its finalization.
We further discuss challenges in Section \ref{ch:attacks}.
\begin{figure}[H]
	\makebox[\linewidth]{
		\scalebox{0.7}{
		\includegraphics[width=\linewidth]{figures/exit_state_machine.pdf}
		}
		}
	\caption{
        The stages of an exit. After a coin transitions to the \texttt{EXITED}
        state, it can be withdrawn to its owner's wallet.
	}
    \label{fig:exit_state_machine}
\end{figure}

\begin{figure}[H]
	\makebox[\linewidth]{
		\scalebox{0.8}{
		\includegraphics[width=\linewidth]{figures/exit_lifetime.pdf}
		}
		}
	\caption{
		During its lifetime, an exit can be challenged during the maturity period. After its maturity period is over, it can be
        finalized and the exiting coin can be withdrawn.
	}
    \label{fig:exit_lifetime}
\end{figure}


\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi)
\usepackage{fullpage} % changes the margin
\usepackage{graphicx}
\begin{document}
%Header-Make sure you update this information!!!!
\noindent
\large\textbf{Homework Assignment X} \hfill \textbf{Anirudh Ganesh} \\
\normalsize Computer Vision for HCI \hfill CSE5524 (Au `18) \\
Prof. Jim Davis \hfill Score: \_\_\_/12 \\
TA: Sayan Mandal \hfill Due Date: 11/13/18

\section*{PART A: Back Propagation}

\begin{figure}[h]
\centering
  \includegraphics[scale=0.4]{backprop.png}
  \caption{The given network.}
\end{figure}

Goal is to determine the new weight parameter $w_5$ after one cycle of Gradient Descent using the single example given to us.

$$\frac{\partial E_{total}}{\partial w_5} = \frac{\partial E_{total}}{\partial out_{01}} * \frac{\partial out_{01}}{\partial net_{01}} * \frac{\partial net_{01}}{\partial w_5}$$

\subsection*{Calculating gradient of error}

The error function is given as,

$$ E_{total} = \frac{1}{2}(target_{01} - out_{01})^2 + \frac{1}{2}(target_{02} - out_{02})^2$$

\noindent Thus, the gradient of error is given as,

$$ \frac{\partial E_{total}}{\partial out_{01}} =  -(target_{01} - out_{01}) = -(0.5 - 0.624) = 0.124$$

\subsection*{Calculating $\Delta out_{01}$}

Output layer is given by,

$$ out_{01} = \frac{1}{1+e^{-net_{01}}}$$

\noindent Thus the $\Delta out_{01}$ is given by,

$$ \frac{\partial out_{01}}{\partial net_{01}} = out_{01} * (1 - out_{01}) = (0.624) * (1 - 0.624) = 0.235$$

\subsection*{Calculating $\Delta net_{01}$}

Net activation for the layer is given by,

$$ net_{01} = w_5 * out_{h1} + w_6 * out_{h2} + b_2$$

\noindent Thus the $\Delta net_{01}$ can be computed as,

$$ \frac{\partial net_{01}}{\partial w_5} = out_{h1} $$

\noindent In order to compute $out_{h1}$, we need $net_{h1}$,

$$ net_{h1} = i_1*w_1 + i_2 * w_2 + b_1 = (0.4*0.1) + (0.6*0.2) + 0.4 = 0.04 + 0.12 + 0.4 = 0.56$$

\noindent From this we compute $out_{h1}$ as,

$$ out_{h1} = \frac{1}{1+e^{-net_{h1}}} = \frac{1}{1+e^{-0.56}} = 0.636$$

\noindent Coming back to our $\Delta net_{01}$,

$$ \frac{\partial net_{01}}{\partial w_5} = out_{h1} = 0.636$$

\subsection*{Compute the step for gradient descent}

$$\frac{\partial E_{total}}{\partial w_5} = \frac{\partial E_{total}}{\partial out_{01}} * \frac{\partial out_{01}}{\partial net_{01}} * \frac{\partial net_{01}}{\partial w_5} = 0.124 * 0.235 * 0.636 = 0.019$$

\subsection*{Update the weight, $\alpha = 0.3$}

$$w_5 = w_5 - \alpha \frac{\partial E_{total}}{\partial w_5} = 0.3 - 0.3 * 0.019 = 0.294$$

\section*{PART B: Convolutional Neural Networks}

So we start off straight off the shelf with a pretty good baseline of $96.76\%$. In-order to improve the accuracy from the baseline, I tried a couple of things.

First of all was experimenting the the different filter size of the first convolutional layer. After some tweaking a filter size of 3, and a filter number of 32 was found to be the optimal. This change bumped up the models performance to $96.96\%$.

The next major change was to add a fully connected layer of 128 nodes before the classifying layer. To mitigate the slowdown in training, batch size was bumped up to 512. These changes give us our best performance of $97.98\%$.

Now, in-order to further increase performance, it was clear that I had to incorporate multiple convolutional layers. But try as I might, in different orders and combinations, the code given utilized the convolutional layer in a format that was unfamiliar to me. So I re-created the similar model with a similar performance in Keras and Python using a Tensorflow backend with a new baseline of $95.48\%$. After this, I was able to add two sets of 64,3 2d convolutional layer, followed by a maxpool of 2x2, followed by two convolution of 128, 3 followed by another maxpool of 2x2. Finally a dense network of 256 and a softmax (all with ReLU activation by default). This gives the best performance of $99.73\%$. I then converted these weights from Keras's h5 format to mat format. Though I don't know how grader is going to utilize this hence this model is submitted separately.

\end{document}


% !TeX root = constructions.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Trisecting an Angle}\label{c.trisection}

\section{Abe's trisection of an angle}\label{s.tri1}

This construction is based upon the presentation in \cite{newton}. The second proof is based upon \cite{oriah}.

\subsection{The construction}

\begin{center}
\begin{tikzpicture}[scale=1]

% Place points P, Q, R
\coordinate (P) at (60:10cm); %(5,8.67);
\coordinate (Q) at (0,0);
\coordinate (R) at (10,0);
\fill (P) circle (2pt) node[below right] {$P$};
\fill (Q) circle (2pt) node[left] {$Q$};
\fill (R) circle (2pt) node[right] {$R$};

% Draw PQR
\draw [very thick] (P)  -- (Q) -- (R);

% Draw perpendicular to QR
\draw [thick] (Q) -- node[left,very near end] {$p$} +(0,11);

% Draw parallel to QR and parallel halfway
\coordinate (A) at (0,5);
\coordinate (B) at (0,2.5);
\draw [thick] (A) -- node[above,very near end] {$q$} +(10,0);
\draw [thick] (B) -- node[above,very near end] {$r$} +(10,0);
\fill (A) circle (1.5pt) node[left] {$A$};
\fill (B) circle (1.5pt) node[left] {$B$};
\path (Q) -- node[left] {$a$} (B) -- node[left] {$a$} (A);
\draw (A) rectangle +(8pt,8pt);
\draw (B) rectangle +(8pt,8pt);

% Tangent line y = -2.75x + 10.69

% Draw fold
\coordinate (D) at (0,10.69);
\coordinate (fold-x) at (3.89,0);
\coordinate (AP) at (3.65,6.33);
\coordinate (QP) at (6.87,2.5);
\coordinate (BP) at (5.26,4.42);
\fill (D) circle (1.5pt) node[left] {$D$};
\fill (AP) circle (1.5pt) node[above,yshift=6pt] {$A'$};
\fill (QP) circle (1.5pt) node[above,yshift=6pt] {$Q'$};
\fill (BP) circle (1.5pt) node[above,xshift=2pt,yshift=2pt] {$B'$};
\draw [very thick,dashed] (D) -- node[left,near start] {$l$} (fold-x);

% Draw line of reflections
\draw [very thick, dotted] (D) -- (QP);

% Draw trisecting lines
\draw [very thick,dotted] (Q) -- ($(Q)!1.3!(QP)$);
\draw [very thick,dotted] (Q) -- ($(Q)!1.3!(BP)$);

% Complete triangle
\draw [very thick,dotted] (A) -- (QP);

\end{tikzpicture}
\end{center}

Given an acute angle $\angle PQR$, let $p$ be the perpendicular to $\overline{QR}$ at $Q$. Let $q$ be a perpendicular to $p$ that intersects $\overline{PQ}$ at point $A$, and let $r$ be the perpendicular to $p$ at $B$ that is halfway between $Q$ and $A$.

Using Axiom~6, construct a fold $l$ that places $A$ at $A'$ on $\overline{PQ}$ and $Q$ at $Q'$ on $r$. Let $B'$ be the reflection of $B$ around $l$.

Draw the lines $\overline{QB'}$ and $QQ'$. We claim that $\angle PQB'$, $\angle B'QQ'$ and $\angle Q'QR$ are a trisection of $\angle PQR$.

\subsection{First proof}

\begin{center}
\begin{tikzpicture}[scale=1]

% Place points P, Q, R
\coordinate (P) at (60:10cm);
\coordinate (Q) at (0,0);
\coordinate (R) at (10,0);
\fill (P) circle (1.5pt) node[below right] {$P$};
\fill (Q) circle (1.5pt) node[left,xshift=-4pt] {$Q$};
\fill (R) circle (1.5pt) node[right] {$R$};

% Draw PQR
\draw [very thick] (Q) -- (R);

% Draw perpendicular to QR
\draw [thick] (Q) -- node[left,very near end] {$p$} +(0,11);

% Draw parallel to QR and parallel halfway
\coordinate (A) at (0,5);
\coordinate (B) at (0,2.5);
\draw [thick] (A) -- node[above,very near end] {$q$} +(10,0);
\draw [thick] (B) -- node[above,very near end] {$r$} +(10,0);
\fill (A) circle (1.5pt) node[left,xshift=-4pt] {$A$};
\fill (B) circle (1.5pt) node[left,xshift=-4pt] {$B$};
\path (Q) -- node[left,xshift=-4pt] {$a$} (B) -- node[left,xshift=-4pt] {$a$} (A);
\draw (A) rectangle +(8pt,8pt);
\draw (B) rectangle +(8pt,8pt);

% Tangent line y = -2.75x + 10.69

% Draw fold
\coordinate (D) at (0,10.69);
\coordinate (fold-x) at (3.89,0);
\coordinate (AP) at (3.65,6.33);
\coordinate (QP) at (6.87,2.5);
\coordinate (BP) at (5.26,4.42);
\fill (D) circle (1.5pt) node[left] {$D$};
\fill (AP) circle (1.5pt) node[above,yshift=6pt] {$A'$};
\fill (QP) circle (1.5pt) node[above,xshift=2pt,yshift=6pt] {$Q'$};
\fill (BP) circle (1.5pt) node[above,xshift=4pt,yshift=2pt] {$B'$};
\draw [very thick,dashed] (D) -- node[left,near start] {$l$} (fold-x);

% Draw line of reflections
\draw [very thick, dotted] (D) -- (AP);

% Draw trisecting lines
\draw [very thick,dotted] (Q) -- ($(Q)!1.3!(BP)$);

\draw [very thick,loosely dash dot,red] (Q) -- (QP);
\draw [very thick,loosely dash dot,red] (QP) -- (AP);
\draw [very thick,loosely dash dot,red] (AP) -- (Q);
\draw [very thick,loosely dash dot dot,blue] ($(Q)+(0,-4pt)$) -- ($(QP)+(0,-4pt)$);
\draw [very thick,dash dot dot,blue] ($(QP)+(0,-4pt)$) -- ($(A)+(0,-4pt)$);
\draw [very thick,dash dot dot,blue] ($(A)+(-4pt,0)$) -- ($(Q)+(-4pt,0)$);

\draw [thick,dotted] (A) -- (AP);

\node[left,xshift=-40pt,yshift=7pt] at (QP) {$\alpha$};
\node[left,xshift=-40pt,yshift=-6pt] at (QP) {$\alpha$};
\node[right,xshift=40pt,yshift=6pt] at (Q) {$\alpha$};
\node[right,xshift=40pt,yshift=28pt] at (Q) {$\alpha$};
\node[right,xshift=30pt,yshift=42pt] at (Q) {$\alpha$};

\end{tikzpicture}
\end{center}

Since $A', B', Q'$ are all reflections around the same line $l$ of the points $A,B,Q$ on one line $DQ$, they are all on one line $\overline{DQ'}$. By construction, $\overline{AB}=\overline{BQ}$, $\overline{BQ'}$ is perpendicular to $AQ$; $\overline{BQ'}$ is a common side, so $\triangle ABQ'\cong \triangle QBQ'$ by side-angle-side. Therefore, $\angle AQ'B=\angle QQ'B=\alpha$, since $\overline{Q'B}$ is the perpendicular bisector of the isoceles triangle $\triangle AQ'Q$.

By alternating interior angles, $\angle Q'QR=\angle QQ'B=\alpha$.

By reflection, $\triangle AQ'Q\cong \triangle A'QQ'$.\footnote{The two triangles have been emphasized using different patterns of dashes and dots, as well as using color.}
\begin{quote}
The fold $l$ is the perpendicular bisector of both $\overline{AA'}$ and $\overline{QQ'}$; drop perpendiculars from $A$ and $A'$ to $\overline{QQ'}$; then $\overline{AQ}=\overline{A'Q'}$ follows by congruent right triangles. $\overline{AA'Q'Q}$ is an isoceles trapezoid so its diagonals are equal $\overline{AQ'}=\overline{A'Q}$.
\end{quote}
Therefore, $\overline{QB'}$, the reflection of $\overline{Q'B}$, is the perpendicular bisector of an isoceles triangle and $\angle A'QB'=\angle B'QQ'=\angle QQ'B=\angle Q'QR=\alpha$.

\subsection{Second proof}

\begin{center}
\begin{tikzpicture}[scale=1]

% Place points P, Q, R
\coordinate (P) at (60:10cm); %(5,8.67);
\coordinate (Q) at (0,0);
\coordinate (R) at (10,0);
\fill (P) circle (1.5pt) node[below right] {$P$};
\fill (Q) circle (1.5pt) node[left] {$Q$};
\fill (R) circle (1.5pt) node[right] {$R$};

% Draw PQR
\draw [very thick] (P)  -- (Q) -- (R);

% Draw perpendicular to QR
\draw [thick] (Q) -- node[left,very near end] {$p$} +(0,11);

% Draw parallel to QR and parallel halfway
\coordinate (A) at (0,5);
\coordinate (B) at (0,2.5);
\draw [thick] (A) -- node[above,very near end] {$q$} +(10,0);
\draw [thick] (B) -- node[above,very near end] {$r$} +(10,0);
\fill (A) circle (1.5pt) node[left] {$A$};
\fill (B) circle (1.5pt) node[left] {$B$};
\path (Q) -- node[left] {$a$} (B) -- node[left] {$a$} (A);
\draw (A) rectangle +(8pt,8pt);
\draw (B) rectangle +(8pt,8pt);

% Tangent line y = -2.75x + 10.69

% Draw fold
\coordinate (D) at (0,10.69);
\coordinate (fold-x) at (3.89,0);
\coordinate (AP) at (3.65,6.33);
\coordinate (QP) at (6.87,2.5);
\coordinate (BP) at (5.26,4.42);
\fill (D) circle (1.5pt) node[left] {$D$};
\fill (AP) circle (1.5pt) node[above,yshift=6pt] {$A'$};
\fill (QP) circle (1.5pt) node[above,yshift=6pt] {$Q'$};
\fill (BP) circle (1.5pt) node[above,xshift=2pt,yshift=2pt] {$B'$};
\draw [very thick,dashed,name path=fold] (D) -- node[left,near start] {$l$} (fold-x);

% Draw line of reflections
\draw [very thick, dotted] (D) -- (QP);

% Draw trisecting lines
\draw [very thick,dotted,name path=Qr] (Q) -- ($(Q)!1.3!(QP)$);
\draw [very thick,dotted,name path=Qq] (Q) -- ($(Q)!1.3!(BP)$);

% Draw indications of right angles
\draw[rotate=-140] (BP) rectangle +(8pt,8pt);
\path [name intersections = {of = fold and Qr, by = {U}}];
\fill (U) circle (1.5pt) node[above left,xshift=-2pt,yshift=-2pt] {$U$};
\draw[rotate=20] (U) rectangle +(8pt,8pt);
\path [name intersections = {of = fold and Qq, by = {V}}];
\fill (V) circle (1.5pt) node[above left,xshift=-2pt,yshift=-2pt] {$V$};

\path (Q) -- node[below,near end] {$b$} (U);
\path (U) -- node[below] {$b$} (QP);

\node[left,xshift=-40pt,yshift=-6pt] at (QP) {$\alpha$};
\node[right,xshift=40pt,yshift=6pt] at (Q) {$\alpha$};
\node[right,xshift=40pt,yshift=28pt] at (Q) {$\alpha$};
\node[right,xshift=30pt,yshift=42pt] at (Q) {$\alpha$};
\end{tikzpicture}
\end{center}

Since $l$ is a fold, it is the perpendicular bisector of $\overline{QQ'}$. Denote the intersection of $l$ with $\overline{QQ'}$ by $U$, and its intersection with $\overline{QB'}$ by $V$. $\triangle VUQ\cong \triangle VUQ'$ by side-angle-side since $\overline{VU}$ is a common side,  the angles at $U$ are right angles and $\overline{QU}=\overline{Q'U}=b$. Therefore, $\angle VQU=\angle VQ'U=\alpha$ and then $\angle Q'QR=\angle VQ'U=\alpha$ by alternating interior angles.

As in Proof 1, $A', B', Q'$ are all reflections around $l$, so they are all on one line $\overline{DQ'}$, and $\overline{A'B'}=\overline{AB}=\overline{BQ}=\overline{B'Q'}=a$. Then $\triangle A'B'Q\cong\triangle Q'B'Q$ and $\angle A'QB'=\angle Q'QB'=\alpha$.

\newpage

\section{Martin's trisection of an angle}\label{s.tri2}

\subsection{The construction}

\begin{center}
\begin{tikzpicture}[scale=.9]

% Place points P, Q, R
\coordinate (P) at (60:10cm); %(5,8.67);
\coordinate (Q) at (0,0);
\coordinate (R) at (10,0);
\fill (P) circle (2pt) node[below right] {$P$};
\fill (Q) circle (2pt) node[above left] {$Q$};
\fill (R) circle (2pt) node[right] {$R$};

% Draw PQR
\draw [very thick] (R)  -- (Q);
\draw [very thick,name path=pq] (Q) -- (P);

% M is the midpoint of PQ
\coordinate (M) at (2.5, 4.33);
\fill (M) circle (2pt) node[above left,xshift=2pt] {$M$};
\draw [rotate=-90] (M) rectangle +(8pt,8pt);

% Drop a perpendicular from M to QR and extend the line upwards
% This is the given line p
\coordinate (pQR) at (M |- Q);
\draw [thick,name path=p] (pQR) --
   node[left, very near end,yshift=28pt] {$p$}
   ($(pQR)!2!(M)$);
\draw (pQR) rectangle +(8pt,8pt);

% Construct q perpendicular to p through M
\draw [thick,name path=q] ($(M)+(-2,0)$) --
   node[above, very near start,xshift=-30pt] {$q$}
   ($(M)+(10,0)$);

% Construct the fold line t
% Its equation is y = -2.75x + 18.51, as obtained from Geogebra
\coordinate (t1) at (6.7,.085);
\coordinate (t2) at (3.5,8.89);
\draw [very thick,dashed,name path=t] (t1) --
   node[very near end,left] {$l$}
   (t2);

% Construct a perpendicular to t through P
\coordinate (perp-p) at ($(t1)!(P)!(t2)$);
\path [name path=perp-p] (P) -- ($(P)!2.5!(perp-p)$);

% Get its intersection with t denoted Pt
% and its intersection with p named PP
\path [name intersections = {of = t and perp-p, by = {Pt}}];
\path [name intersections = {of = p and perp-p, by = {PP}}];
\fill (PP) circle(2pt) node[left] {$P'$};
\draw [rotate=22] (Pt) rectangle +(8pt,8pt);

% Draw PT
\draw [very thick,dotted] (P) -- (PP);

% Construct a perpendicular to t through Q
\coordinate (perp-q) at ($(t1)!(Q)!(t2)$);
\path[name path=perp-q] (Q) -- ($(Q)!2.1!(perp-q)$);

% Get its intersection with t denoted V
% and its intersection with q denoted S=Q'
\path [name intersections = {of = t and perp-q, by = {V}}];
\path [name intersections = {of = q and perp-q, by = {QP}}];
\fill (QP) circle(2pt) node[above,yshift=4pt] {$Q'$};
\fill (V) circle(2pt) node[above left,xshift=-4pt,yshift=-2pt] {$V$};
\draw [rotate=22] (V) rectangle +(8pt,8pt);

% Draw Q QP
\draw [very thick,dotted,name path=qs] (Q) -- (QP);

% Get the intersection of QS with p denoted U
\path [name intersections = {of = p and qs, by = {U}}];
\fill (U) circle(2pt) node[above left] {$U$};

% Draw PP QP
\draw [very thick,dotted,name path=ts] (PP) -- (QP);

% Get its intersection with QP denoted W
\path [name intersections = {of = ts and pq, by = {W}}];
\fill (W) circle(2pt) node[right,xshift=4pt,yshift=4pt] {$W$};

% Label line segments
\path (P) -- node[left] {$a$} (M);
\path (M) -- node[left]  {$a$} (Q);
\path (PP) -- node[left]  {$b$} (M);
\path (M) -- node[right] {$b$} (U);
\path (Q) -- node[below,near end] {$c$} (V);
\path (V) -- node[below] {$c$} (QP);

% Label angles
\node [xshift=5pt,yshift=20pt]        at (M) {$\gamma$};
\node [xshift=-5pt,yshift=-20pt]      at (M) {$\gamma$};
\node [xshift=18pt,yshift=15pt]       at (Q) {$\beta$};
\node [xshift=-18pt,yshift=-15pt]     at (P) {$\beta$};
\node [left,xshift=-30pt,yshift=7pt]  at (QP) {$\alpha$};
\node [left,xshift=-30pt,yshift=-7pt] at (QP) {$\alpha$};
\node [right,xshift=34pt,yshift=7pt]  at (Q) {$\alpha$};
\end{tikzpicture}
\end{center}

Given the acute angle $\angle PQR$, let $M$ be the midpoint of $\overline{PQ}$. Construct $p$ the perpendicular to $\overline{QR}$ through $M$ and construct $q$ perpendicular to $p$ through $M$. $q$ is parallel to $\overline{QR}$.

Using Axiom 6, construct a fold $l$ that places $P$ at $P'$ on $p$ and $Q$ at $Q'$ on $q$. More than one fold may be possible; choose the one that intersects $\overline{PM}$.

Draw the lines $\overline{PP'}$ and $\overline{QQ'}$. Denote the intersection of $\overline{QQ'}$ with $p$ by $U$ and its intersection with $l$ by $V$. Denote the intersection of $\overline{PQ}$ and $P'Q'$ with $l$ by $W$.\footnote{It is not immediate that both $\overline{PQ}$ and $P'Q'$ intersect $l$ at the same point. $\triangle PP'W \sim \triangle QQ'W$ so the altitudes divide the vertical angles $\angle PWP', \angle QWQ'$ similarly and thus must be on the same line.}

\subsection{Proof}

$\triangle QMU\cong \triangle PMP'$ by angle-side-angle:  $\angle P'PM=\angle UQM=\beta$ by alternate interior angles; $\overline{QM}=\overline{MP}=a$ since $M$ is the midpoint of $\overline{PQ}$; $\angle QMU=\angle PMP'$ are vertical angles. Therefore, $\overline{P'M}=\overline{MU}=b$.

$\triangle P'MQ'\cong \triangle UMQ'$ by side-angle-side: we have shown that $\overline{P'M}=\overline{MU}=b$; the angles at $M$ are right angles; $\overline{MQ'}$ is a common side. Since the altitude of the isoceles triangle $\triangle P'Q'U$ is the bisector of $\angle P'Q'U$, so $\angle P'Q'M=\angle UQ'M=\alpha$.

$\triangle QWV\cong\triangle Q'WV$ by side-angle-side: $\overline{QV}=\overline{VQ'}=c$; the angles at $V$ are right angles since the fold is the perpendicular bisector of $\overline{QQ'}$; $\overline{VW}$ is a common side. Therefore, $\angle WQV=\beta=\angle WQ'V=2\alpha$. By alternate interior angles $\angle Q'QR=\angle MQ'Q=\alpha$. We have $\angle PQR = \beta + \alpha = 2\alpha+\alpha=3\alpha$ so $\angle Q'QR$ is one-third of $\angle PQR$.