\documentclass[a4paper]{article}
% generated by Docutils <http://docutils.sourceforge.net/>
\usepackage{cmap} % fix search and cut-and-paste in Acrobat
\usepackage{ifthen}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{alltt}
\usepackage{color}
\setcounter{secnumdepth}{0}
\usepackage{textcomp} % text symbol macros

%%% Custom LaTeX preamble
% PDF Standard Fonts
\usepackage{mathptmx} % Times
\usepackage[scaled=.90]{helvet}
\usepackage{courier}

%%% User specified packages and stylesheets
\usepackage{alectryon}
\usepackage{tango_subtle}

%%% Fallback definitions for Docutils-specific commands
% basic code highlight:
\providecommand*\DUrolecomment[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\providecommand*\DUroledeleted[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}}
\providecommand*\DUrolekeyword[1]{\textbf{#1}}
\providecommand*\DUrolestring[1]{\textit{#1}}

% inline markup (custom roles)
% \DUrole{#1}{#2} tries \DUrole#1{#2}
\providecommand*{\DUrole}[2]{%
  \ifcsname DUrole#1\endcsname%
    \csname DUrole#1\endcsname{#2}%
  \else
    % backwards compatibility: try \docutilsrole#1{#2}
    \ifcsname docutilsrole#1\endcsname%
      \PackageWarningNoLine{docutils}{Command prefix "docutilsrole" is
         deprecated, \MessageBreak use `\protect\DUrole #1`}
      \csname docutilsrole#1\endcsname{#2}%
    \else%
      #2%
    \fi%
  \fi%
}

% transition (break, fancybreak, anonymous section)
\providecommand*{\DUtransition}{%
  \hspace*{\fill}\hrulefill\hspace*{\fill}
  \vskip 0.5\baselineskip
}

% hyperlinks:
\ifthenelse{\isundefined{\hypersetup}}{
  \usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
  \usepackage{bookmark}
  \urlstyle{same} % normal text font (alternatives: tt, rm, sf)
}{}
\hypersetup{
  pdftitle={Literate programming with Alectryon (Coq input)},
}

%%% Body
\begin{document}
\title{Literate programming with Alectryon (Coq input)%
  \label{literate-programming-with-alectryon-coq-input}}
\author{}
\date{}
\maketitle

Alectryon supports literate programs and documents (combinations of code and prose) written in Coq and reStructuredText.  Here is an example, written in Coq.  It can be converted to reST, HTML, or LaTeX using the following commands:

\begin{quote}
\begin{alltt}
alectryon literate_coq.v
  # Coq+reST → HTML;  produces ‘literate_coq.html’
alectryon literate_coq.v -{}-backend latex
  # Coq+reST → LaTeX; produces ‘literate_coq.tex’
alectryon literate_coq.v -{}-backend rst
  # Coq+reST → reST;  produces ‘literate_coq.v.rst’
\end{alltt}
\end{quote}

%___________________________________________________________________________
\DUtransition

Here's an \emph{inductive specification} of evenness:

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Inductive}~\PY{n+nf}{Even}~\PY{o}{:}~\PY{n}{nat}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{k+kt}{Prop}~\PY{o}{:=}\nl
      \PY{o}{|}~\PY{n}{EvenO}~\PY{o}{:}~\PY{n}{Even}~\PY{n}{O}\nl
      \PY{o}{|}~\PY{n}{EvenS}~\PY{o}{:}~\PY{k+kr}{forall}~\PY{n+nv}{n}\PY{o}{,}~\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)).}
    \end{input}
  \end{sentence}
\end{alectryon}

… and a corresponding decision procedure:

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Fixpoint}~\PY{n+nf}{even}~\PY{o}{(}\PY{n+nv}{n}\PY{o}{:}~\PY{n}{nat}\PY{o}{):}~\PY{n}{bool}~\PY{o}{:=}\nl
      ~~\PY{k+kr}{match}~\PY{n}{n}~\PY{k+kr}{with}\nl
      ~~\PY{o}{|}~\PY{l+m+mi}{0}~\PY{o}{=\PYZgt{}}~\PY{n}{true}\nl
      ~~\PY{o}{|}~\PY{l+m+mi}{1}~\PY{o}{=\PYZgt{}}~\PY{n}{false}\nl
      ~~\PY{o}{|}~\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=\PYZgt{}}~\PY{n}{even}~\PY{n}{n}\nl
      ~~\PY{k+kr}{end}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{txt}
    \nl
    \PY{c}{(*~Ensure~that~we~never~unfold~[even~(S~n)]~*)}\nl
  \end{txt}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Arguments}~\PY{n}{even}~\PY{o}{:}~\PY{n+nb}{simpl}~\PY{n}{nomatch}\PY{o}{.}
    \end{input}
  \end{sentence}
\end{alectryon}

Can we prove it correct?

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Lemma}~\PY{n+nf}{even\PYZus{}Even}~\PY{o}{:}\nl
      ~~\PY{k+kr}{forall}~\PY{n+nv}{n}\PY{o}{,}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Proof}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{n+nb}{induction}~\PY{n}{n}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{l+m+mi}{0}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{IHn}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{k+kp}{all}\PY{o}{:}~\PY{n+nb}{cbn}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{IHn}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~0~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{split}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}\end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{l+m+mi}{0}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{true}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{all}\PY{o}{:}~\PY{n+nb}{constructor}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~S~\PYZus{}~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{IHn}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{apply}~\PY{n}{IHn}\PY{o}{.}~\PY{c}{(*~stuck!~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{messages}
        \begin{message}
          \PY{n}{In}~\PY{n}{environment}\nl
          \PY{n}{n}~\PY{o}{:}~\PY{n}{nat}\nl
          \PY{n}{IHn}~\PY{o}{:}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\nl
          \PY{n}{H}~\PY{o}{:}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\nl
          \PY{n}{Unable}~\PY{n}{to}~\PY{n+nb}{unify}~\PY{l+s+s2}{\PYZdq{}Even~n\PYZdq{}}~\PY{k+kr}{with}\nl
          ~\PY{l+s+s2}{\PYZdq{}even~(S~n)~=~true~\PYZlt{}\PYZhy{}\PYZgt{}~Even~(S~n)\PYZdq{}}\PY{o}{.}
        \end{message}
      \end{messages}
    \end{output}
  \end{sentence}
\end{alectryon}

The induction hypothesis doesn't apply — maybe we need to destruct \texttt{n}?

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{txt}
    ~~~~
  \end{txt}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{n+nb}{destruct}~\PY{n}{n}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{IHn}{\PY{n}{even}~\PY{l+m+mi}{0}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{l+m+mi}{1}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{IHn}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{o}{+}~\PY{c}{(*~n~←~1~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{IHn}{\PY{n}{even}~\PY{l+m+mi}{0}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{l+m+mi}{1}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~~~\PY{n+nb}{split}\PY{o}{;}~\PY{n+nb}{inversion}~\PY{l+m+mi}{1}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{o}{+}~\PY{c}{(*~n~←~S~(S~\PYZus{})~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{IHn}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
\end{alectryon}

Stuck again!

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Abort}\PY{o}{.}
    \end{input}
  \end{sentence}
\end{alectryon}

\section{Strengthening the spec%
  \label{strengthening-the-spec}%
}

The usual approach is to strengthen the spec to work around the weakness of the inductive principle.

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Lemma}~\PY{n+nf}{even\PYZus{}Even}~\PY{o}{:}\nl
      ~~\PY{k+kr}{forall}~\PY{n+nv}{n}\PY{o}{,}~\PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{)}~\PY{o}{/\PYZbs{}}\nl
      ~~~~~~~\PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)).}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{)}~\PY{o}{/\PYZbs{}}\nl
            \PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Proof}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{)}~\PY{o}{/\PYZbs{}}\nl
            \PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{n+nb}{induction}~\PY{n}{n}\PY{o}{;}~\PY{n+nb}{cbn}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{o}{(}\PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}\PY{o}{)}~\PY{o}{/\PYZbs{}}~\PY{o}{(}\PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}\PY{o}{)}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{IHn}{\PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{)}~\PY{o}{/\PYZbs{}}~\PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{/\PYZbs{}}\nl
              \PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)))}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~0~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{o}{(}\PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}\PY{o}{)}~\PY{o}{/\PYZbs{}}~\PY{o}{(}\PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}\PY{o}{)}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{repeat}~\PY{n+nb}{split}\PY{o}{;}~\PY{n+nb}{cbn}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}\end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
            \end{conclusion}
          \end{goal}
          \sep
          \begin{goal}
            \begin{hyps}\end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{l+m+mi}{1}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{false}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{all}\PY{o}{:}~\PY{k+kp}{try}~\PY{n+nb}{constructor}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}\end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}\end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{l+m+mi}{1}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{false}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{all}\PY{o}{:}~\PY{n+nb}{inversion}~\PY{l+m+mi}{1}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~S~\PYZus{}~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{IHn}{\PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{)}~\PY{o}{/\PYZbs{}}~\PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{/\PYZbs{}}\nl
            \PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{destruct}~\PY{n}{IHn}~\PY{k+kr}{as}~\PY{o}{((}\PY{n}{Hne}~\PY{o}{\PYZam{}}~\PY{n}{HnE}\PY{o}{)}~\PY{o}{\PYZam{}}~\PY{o}{(}\PY{n}{HSne}~\PY{o}{\PYZam{}}~\PY{n}{HSnE}\PY{o}{)).}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
            \sep
            \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
            \sep
            \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{o}{(}\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{/\PYZbs{}}\nl
            \PY{o}{(}\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{repeat}~\PY{n+nb}{split}\PY{o}{;}~\PY{n+nb}{cbn}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
            \sep
            \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
            \sep
            \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
              \sep
              \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
              \sep
              \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
              \sep
              \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
          \sep
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
              \sep
              \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
              \sep
              \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
              \sep
              \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
            \end{conclusion}
          \end{goal}
          \sep
          \begin{goal}
            \begin{hyps}
              \hyp{n}{\PY{n}{nat}}
              \sep
              \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
              \sep
              \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
              \sep
              \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
              \sep
              \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{all}\PY{o}{:}~\PY{n+nb}{eauto~using}~\PY{n}{EvenS}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{n}{\PY{n}{nat}}
            \sep
            \hyp{Hne}{\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{HnE}{\PY{n}{Even}~\PY{n}{n}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}}
            \sep
            \hyp{HSne}{\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}}
            \sep
            \hyp{HSnE}{\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{)}~\PY{o}{=}~\PY{n}{true}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{inversion}~\PY{l+m+mi}{1}\PY{o}{;}~\PY{n+nb}{eauto}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Qed}\PY{o}{.}
    \end{input}
  \end{sentence}
\end{alectryon}

\section{Writing a fixpoint%
  \label{writing-a-fixpoint}%
}

But writing a fixpoint (either with the \texttt{\DUrole{code}{\DUrole{highlight}{\DUrole{coq}{\DUrole{kn}{Fixpoint}}}}} command or with the \texttt{\DUrole{code}{\DUrole{highlight}{\DUrole{coq}{\DUrole{kr}{fix}}}}} tactic) is much nicer:

\begin{alectryon}
  % Generator: Alectryon
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Fixpoint}~\PY{n+nf}{even\PYZus{}Even\PYZus{}fp}~\PY{o}{(}\PY{n+nv}{n}\PY{o}{:}~\PY{n}{nat}\PY{o}{):}\nl
      ~~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Proof}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{n+nb}{destruct}~\PY{n}{n}~\PY{k+kr}{as}~\PY{o}{[}~\PY{o}{|}~\PY{o}{[}~\PY{o}{|}~\PY{n}{n}~\PY{o}{]}~\PY{o}{];}~\PY{n+nb}{cbn}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
              \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
            \end{conclusion}
          \end{goal}
          \sep
          \begin{goal}
            \begin{hyps}
              \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
              \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
              \sep
              \hyp{n}{\PY{n}{nat}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~0~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{true}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{0}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{k+kp}{repeat}~\PY{n+nb}{constructor}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~1~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{false}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{l+m+mi}{1}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{split}\PY{o}{;}~\PY{n+nb}{inversion}~\PY{l+m+mi}{1}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~\PY{o}{\PYZhy{}}~\PY{c}{(*~n~←~S~(S~\PYZus{})~*)}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{n+nb}{split}\PY{o}{.}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
        \sep
        \begin{extragoals}
          \begin{goal}
            \begin{hyps}
              \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
              \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
              \sep
              \hyp{n}{\PY{n}{nat}}
            \end{hyps}
            \sep
            \infrule{}
            \sep
            \begin{conclusion}
              \PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}
            \end{conclusion}
          \end{goal}
        \end{extragoals}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{o}{+}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{n+nb}{constructor}\PY{o}{;}~\PY{n+nb}{apply}~\PY{n}{even\PYZus{}Even\PYZus{}fp}\PY{o}{;}~\PY{n+nb+bp}{assumption}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      ~~~~\PY{o}{+}
    \end{input}
    \sep
    \begin{output}
      \begin{goals}
        \begin{goal}
          \begin{hyps}
            \hyp{even\char`\_Even\char`\_fp}{\PY{k+kr}{forall}~\PY{n+nv}{n}~\PY{o}{:}~\PY{n}{nat}\PY{o}{,}\nl
            \PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}~\PY{o}{\PYZlt{}\PYZhy{}\PYZgt{}}~\PY{n}{Even}~\PY{n}{n}}
            \sep
            \hyp{n}{\PY{n}{nat}}
          \end{hyps}
          \sep
          \infrule{}
          \sep
          \begin{conclusion}
            \PY{n}{Even}~\PY{o}{(}\PY{n}{S}~\PY{o}{(}\PY{n}{S}~\PY{n}{n}\PY{o}{))}~\PY{o}{\PYZhy{}\PYZgt{}}~\PY{n}{even}~\PY{n}{n}~\PY{o}{=}~\PY{n}{true}
          \end{conclusion}
        \end{goal}
      \end{goals}
    \end{output}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{n+nb}{inversion}~\PY{l+m+mi}{1}\PY{o}{;}~\PY{n+nb}{apply}~\PY{n}{even\PYZus{}Even\PYZus{}fp}\PY{o}{;}~\PY{n+nb+bp}{assumption}\PY{o}{.}\nl
    \end{input}
  \end{sentence}
  \sep
  \begin{sentence}
    \begin{input}
      \PY{k+kn}{Qed}\PY{o}{.}
    \end{input}
  \end{sentence}
\end{alectryon}

Alectryon defined a \texttt{:coqid:} role to link to definitions in Coqdoc-generated documentation; for example:

\begin{quote}
Note that the standard library already contains a \href{https://coq.inria.fr/library/Coq.Init.Nat.html\#even}{boolean} \href{https://coq.inria.fr/library/Coq.Init.Nat.html\#even}{predicate} for \texttt{\DUrole{code}{\DUrole{highlight}{\DUrole{coq}{\DUrole{n}{even}}}}} (called \href{https://coq.inria.fr/library/Coq.Init.Nat.html\#even}{Coq.Init.Nat.even}, or \href{https://coq.inria.fr/library/Coq.Init.Nat.html\#even}{even} for short), as well as an \href{https://coq.inria.fr/library/Coq.Arith.PeanoNat.html\#Nat.Even}{inductive one} (called \href{https://coq.inria.fr/library/Coq.Arith.PeanoNat.html\#Nat.Even}{Nat.Even} in module \href{https://coq.inria.fr/library/Coq.Arith.PeanoNat.html\#}{Coq.Arith.PeanoNat}).
\end{quote}

If you have your own documentation, you can use a custom role to point to your own URL:

With this definition, for example, you can write \href{https://coq.inria.fr/library/Coq.Arith.PeanoNat.html\#Nat.Even}{Nat.Even} instead of \href{https://coq.inria.fr/library/Coq.Arith.PeanoNat.html\#Nat.Even}{Nat.Even}.

\end{document}


\chapter{List of Symbols}
\begin{center}
\doublespacing
\begin{tabular}{ll}
$b, c$ & constants\\
$C_f$ & local friction coefficient\\
\end{tabular}
\end{center}


\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{gensymb}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{mathtools}
\usetikzlibrary{arrows,positioning}
\tikzset{
    %Define standard arrow tip
    >=stealth',
    % Define arrow style
    pil/.style={
           ->,
           thick,
           shorten <=2pt,
           shorten >=2pt,}
}
\usepackage{geometry}
\geometry{
    left=2cm,
    right=0.64cm,
    top=0.64cm,
    bottom=2cm
}
\usepackage{multicol}
\setlength{\columnsep}{1cm}
\graphicspath{ {images/} }

\begin{document}

\chapter{Semester 2 Examination 2013-2014\\CZ4034 Information Retrieval}

\begin{multicols*}{2}

\section{Question 1}

\noindent \textbf{Question 1a} \\

\noindent \textbf{(i)} Document frequency is number of documents that contain the term. Rare term has low document frequency, and is more informative than frequent terms.\\

\noindent \textbf{(ii)} In boolean retrieval, we retrieve results by performing boolean operation on posting lists. By knowing the document frequency, we can optimise boolean queries by processing AND operation in order of increasing document frequency.\\

\noindent \textbf{(iii)} Maximum number of document for:
\begin{itemize}
    \item (blue OR sky) = 33108
    \item (red OR apple) = 25467
    \item (yellow OR pages) = 65026
\end{itemize}

\noindent We do AND operation for (red OR apple) and (blue OR sky) first, then do AND operation for (yellow OR pages)\\

\noindent \textbf{Question 1b}

\noindent By having good compression techniques, the spaces that are required to store stop words are very small. In addition, by having good query optimization techniques, we only need to pay a little performance trade-off to include stop words. Furthermore, we need stop words for phrase query (e.g. King of Denmark) and relational query (e.g. flight to London). \\

\noindent \textbf{Question 1c}

\begin{center}
\begin{tabular}{ | l | l  l  l  l  |}
    \hline
      &   & C & E & E \\
    \hline
      & 0 & 1 & 2 & 3 \\
    S & 1 & 1 & 2 & 3 \\
    C & 2 & 1 & 2 & 3 \\
    E & 3 & 2 & 1 & \textbf{2} \\
    \hline
\end{tabular}
\end{center}

\noindent \textbf{Question 1d}

\noindent Not in syllabus\\

\noindent \textbf{Question 1e}

$$M=kT^b$$
$$5000=k(10000)^b$$
$$50000=k(1000000)^b$$

\begin{equation*}
\begin{split}
    \frac{5000}{(10000)^b}1000000^b &= 50000 \\
    10^{-4b}\cdot 10^{6b} &= 10 \\
    10^{2b} &= 10\\
    b &= 0.5\\
    k &= 50
\end{split}
\end{equation*}

\noindent When $T=10^{11} \times 10^3$, $M=500 \times 10^6$

\section{Question 2}

\noindent \textbf{Question 2a}

\noindent \textbf{(i)} Length normalization: A vector can be normalized by dividing each of its components by its length. Dividing a vector by its $L_2$ norm makes it a unit vector. Long and short documents now have comparable weights. Let say we have a document vector $\vec{d} = [1,1,0]$. We normalize the vector by using the following formula:

$$\vec{d}_{\text{norm}} = \frac{\vec{d}}{|\vec{d}|}$$

\noindent $|\vec{d}| = \sqrt{2}$, hence $\vec{d}_{\text{norm}} = [0.707, 0.707, 0]$ \\

\noindent \textbf{(ii)} Lossy compression: In lossy compression, the lost information after compression cannot be recovered. Several of the preprocessing steps can be viewed as lossy compression, such as case folding, stopwords, stemming, number elimination. \\

\noindent \textbf{(iii)} XPath: (not in syllabus) XPath is a syntax for defining parts of an XML document. XPath uses path expressions to navigate in XML documents. \\

\noindent \textbf{Question 2b}

\begin{center}
\verb|abcd,abel,able,abolish,|
\verb|bore,bored,bores,boring|
\end{center}

\noindent \verb|4abcd4abel4able7abolish4bor*e2|$\diamond$\verb|ed2|$\diamond$\verb|es3|$\diamond$\verb|ing|
\noindent \verb|a                      b|

\begin{center}
\begin{tabular}{ | l | l | l |}
    \hline
    Frequency & Posting pointer & Term pointer \\
    \hline
    1 & & a \\
    1 & & \\
    1 & & \\
    1 & & \\
    1 & & b \\
    1 & & \\
    1 & & \\
    1 & & \\
    \hline
\end{tabular}
\end{center}

\noindent \textbf{Question 2c}

\noindent Not in syllabus\\

\noindent \textbf{Question 2d}

\begin{center}
Doc1: \verb|my love love star|\\
Doc2: \verb|I love star much|\\
Doc3: \verb|my star is your star|
\end{center}

\noindent Before TF-IDF
\begin{center}
\begin{tabular}{ | l | l l l |l|}
    \hline
    Term   & Doc1 & Doc2 & Doc3 & df \\
    \hline
    I      & 0    & 1    & 0    & 1  \\
    is     & 0    & 0    & 1    & 1  \\
    love   & 2    & 1    & 0    & 2  \\
    much   & 0    & 1    & 0    & 1  \\
    my     & 1    & 0    & 1    & 2  \\
    star   & 1    & 1    & 2    & 3  \\
    your   & 0    & 0    & 1    & 1  \\
    \hline
\end{tabular}
\end{center}

\noindent After TF-IDF:
\begin{center}
\begin{tabular}{ | l | l l l |l|}
    \hline
    Term   & Doc1 & Doc2 & Doc3 & idf   \\
    \hline
    I      &0     &0.477 &0     & 0.477 \\
    is     &0     &0     &0.477 & 0.477 \\
    love   &0.229 &0.176 &0     & 0.176 \\
    much   &0     &0.477 &0     & 0.477 \\
    my     &0.176 &0     &0.176 & 0.176 \\
    star   &0     &0     &0     & 0     \\
    your   &0     &0     &0.477 & 0.477 \\
    \hline
\end{tabular}
\end{center}

\noindent After normalization:
\noindent After TF-IDF:
\begin{center}
\begin{tabular}{ | l | l l l |l|}
    \hline
    Term   & Doc1 & Doc2 & Doc3 \\
    \hline
    I      &0     &0.684 &0     \\
    is     &0     &0     &0.684 \\
    love   &0.792 &0.253 &0     \\
    much   &0     &0.684 &0     \\
    my     &0.609 &0     &0.253 \\
    star   &0     &0     &0     \\
    your   &0     &0     &0.684 \\
    \hline
    length &0.289 &0.697 &0.697 \\
    \hline
\end{tabular}
\end{center}

$$\text{cosine}(\text{doc1},\text{doc2})=0.792 \times 0.253 = 0.200$$
$$\text{cosine}(\text{doc1},\text{doc3})=0.609 \times 0.253 = 0.154$$
$$\text{cosine}(\text{doc2},\text{doc3})=0$$

\noindent Doc1 and Doc2 are the closest

\section{Question 3}

\noindent \textbf{Question 3a}

\begin{center}
\begin{tabular}{ | l | l |l|}
    \hline
    ID & Words in user review & Informative? \\
    \hline
    1  & cool app fast excellent & NO \\
    2  & fast app drain battery & YES \\
    3  & cool fast crash bugs & YES \\
    4  & my favorite app bugs & YES \\
    \hline
\end{tabular}
\end{center}

\noindent \textbf{(i)}

\begin{center}\verb|fast cool bugs|\end{center}

\noindent There are 10 vocabulary:

$$P(NO) = \frac{1}{4}$$
$$P(YES) = \frac{3}{4}$$

$$P(\text{fast}|\text{YES}) = \frac{2 + 1}{12 + 10} = \frac{3}{22}$$
$$P(\text{fast}|\text{NO}) = \frac{1 + 1}{4 + 10} = \frac{2}{14}$$

$$P(\text{cool}|\text{YES}) = \frac{1 + 1}{12 + 10} = \frac{2}{22}$$
$$P(\text{cool}|\text{NO}) = \frac{1 + 1}{4 + 10} = \frac{2}{14}$$

$$P(\text{bugs}|\text{YES}) = \frac{2 + 1}{12 + 10} = \frac{3}{22}$$
$$P(\text{bugs}|\text{NO}) = \frac{0 + 1}{4 + 10} = \frac{1}{14}$$

\begin{equation*}
\begin{split}
P(\text{Yes}|\text{d}) &= \frac{\splitfrac{P(\text{fast}|\text{YES})P(\text{cool}|\text{YES})}{P(\text{bugs}|\text{YES})P(\text{YES})}}{P(\text{fast,cool,bugs})}\\
&= \frac{1}{P(\text{fast,cool,bugs})} \cdot \frac{3}{22} \cdot \frac{2}{22} \cdot \frac{3}{22} \cdot \frac{3}{4}\\
&= 0.00127k
\end{split}
\end{equation*}

\begin{equation*}
\begin{split}
P(\text{NO}|\text{d}) &= \frac{\splitfrac{P(\text{fast}|\text{NO})P(\text{cool}|\text{NO})}{P(\text{bugs}|\text{NO})P(\text{NO})}}{P(\text{fast,cool,bugs})}\\
&= \frac{1}{P(\text{fast,cool,bugs})} \cdot \frac{2}{14} \cdot \frac{2}{14} \cdot \frac{1}{14} \cdot \frac{1}{4}\\
&= 0.000364k
\end{split}
\end{equation*}

\noindent Since $P(\text{Yes}|\text{d}) > P(\text{NO}|\text{d})$, the user review is classify as informative (YES).

\noindent \textbf{(ii)}

\begin{center}\verb|cool excellent my favorite|\end{center}

\noindent Transform the table to vector space representation:

\begin{center}
\begin{tabular}{ | l | l l l l |}
    \hline
    Term      & id1 & id2 & id3 & id4 \\ \hline
    app       & 1   & 1   & 0   & 1   \\
    bugs      & 0   & 0   & 1   & 1   \\
    battery   & 0   & 1   & 0   & 0   \\
    cool      & 1   & 0   & 1   & 0   \\
    crash     & 0   & 0   & 1   & 0   \\
    drain     & 0   & 1   & 0   & 0   \\
    excellent & 1   & 0   & 0   & 0   \\
    fast      & 1   & 1   & 1   & 0   \\
    favorite  & 0   & 0   & 0   & 1   \\
    my        & 0   & 0   & 0   & 1   \\
    \hline
\end{tabular}
\end{center}

\noindent We do a normalization without TF-IDF (you can do TF-IDF too).

\begin{center}
\begin{tabular}{ | l | l l l l |}
    \hline
    Term      & id1 & id2 & id3 & id4 \\ \hline
    app       & 0.5 & 0.5 & 0   & 0.5 \\
    bugs      & 0   & 0   & 0.5 & 0.5 \\
    battery   & 0   & 0.5 & 0   & 0   \\
    cool      & 0.5 & 0   & 0.5 & 0   \\
    crash     & 0   & 0   & 0.5 & 0   \\
    drain     & 0   & 0.5 & 0   & 0   \\
    excellent & 0.5 & 0   & 0   & 0   \\
    fast      & 0.5 & 0.5 & 0.5 & 0   \\
    favorite  & 0   & 0   & 0   & 0.5 \\
    my        & 0   & 0   & 0   & 0.5 \\
    \hline
\end{tabular}
\end{center}

$$\mu_{\text{NO}} = [0.5, 0, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0]$$

\begin{equation*}
\begin{split}
\mu_{\text{YES}} &= [0.33, 0.33, 0.17, 0.17, 0.17, \\
&\ \ \ \ 0.17, 0, 0.33, 0.17, 0.17]
\end{split}
\end{equation*}

$$d_{\text{test}} = [0,0,0,0.5,0,0,0.5,0,0.5,0.5]$$

$$\text{cosine}(\mu_{\text{NO}}, d_{\text{test}}) = 0.5$$
$$\text{cosine}(\mu_{\text{YES}}, d_{\text{test}}) = 0.255$$

\noindent Since $\text{cosine}(\mu_{\text{NO}}, d_{\text{test}}) > \text{cosine}(\mu_{\text{YES}}, d_{\text{test}})$, we conclude that the review is not informative (NO). \\

\noindent \textbf{Question 3b}

\noindent The time complexity is $O(|C||V|+|D|L)$, where $L$ is the average length of documents. The term $|D|$ is expected to dominate the time complexity for most text collections. \\

\noindent \textbf{Question 3c}

\noindent kNN has high variance and low bias. NB has low variance and high bias.\\

\noindent \textbf{Question 3d}

\scriptsize
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
            & French     & Japanese   & Total \\
    \hline
    Male    & $A = 39$   & $B = 16$   & $A+B=55$ \\
    Female  & $C = 21$   & $D = 14$   & $C+D=35$ \\
    Total   & $A+C = 60$ & $B+D = 30$ & $N=90$ \\
    \hline
\end{tabular}
\end{center}
\normalsize

\begin{equation*}
\begin{split}
   \chi^2(t,c) &= \frac{N\times (AD - BC)^2}{(A+B) \times (C+D) \times (A+C) \times (B+D)} \\
   &= \frac{90\times ((39 \times 14) - (16 \times 21))^2}{55\times 35 \times 60 \times 30}\\
   &= 1.145 < 3.84
\end{split}
\end{equation*}

\noindent With 95\% confident, we accept that the choice of language subject is independent of sex.

\section{Question 4}

\noindent \textbf{Question 4a}

\begin{tikzpicture}
\begin{axis}[
    enlargelimits=false,
    axis equal
]
\addplot+[
    nodes near coords,
    only marks,
    point meta=explicit symbolic
]
table[meta=label] {
    x y label
    0 0 A
    8 0 B
    16 0 C
    0 6 D
    8 6 E
    16 6 F
};
\end{axis}
\end{tikzpicture}

\noindent By looking at the plot, we know that point $B$ belongs to $A$, point $E$ belongs to $D$, and point $F$ belongs to $C$. Then, we update the centroids:

$$\mu_1 = [4,0]$$
$$\mu_2 = [16,3]$$
$$\mu_3 = [4,6]$$

\begin{tikzpicture}
\begin{axis}[
    enlargelimits=false,
    axis equal
]
\addplot+[
    nodes near coords,
    only marks,
    point meta=explicit symbolic
]
table[meta=label] {
    x y label
    0 0 A
    8 0 B
    16 0 C
    0 6 D
    8 6 E
    16 6 F
    4 0 $\mu_1$
    16 3 $\mu_2$
    4 6 $\mu_3$
};
\end{axis}
\end{tikzpicture}

\noindent After the first iteration, $A$ and $B$ in cluster 1, $C$ and D in cluster 2, and $D$ and $F$ in cluster 3. So, it converges.\\

\noindent \textbf{Question 4b}

\noindent We can use agglomerative or divisive hierarchical clustering. Once the clustering task is completed, we can prune the hierarchical tree to achieve the number of cluster we want. The complexity of the algorithm is $O((N-1)(N^2))$ because we need $O(N^2)$ to update the proximity matrix, and we need to do it for $O(N-1)$ times. So, the complexity is $O(N^3)$.\\

\noindent \textbf{Question 4c}

\begin{center}
\begin{tikzpicture}[
roundnode/.style={circle, draw=black, thin},
]
%Nodes
\node[roundnode](A){A};
\node[above=of A](dummy){};

\node[roundnode](B)[left=of dummy]{B};

\node[roundnode](C)[right=of dummy]{C};

\path[pil] (B) edge [bend right=45] (A);
\path[pil] (C) edge [bend right=45] (A);
\path[pil] (A) edge [bend right=45] (C);
\path[pil] (B) edge [bend left=45] (C);

\end{tikzpicture}
\end{center}

\noindent \textbf{(i)} Transition matrix
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
      & A  & B  & C \\
    \hline
    A & 0  & 0  & 1  \\
    B & 1  & 0  & 1  \\
    C & 1  & 0  & 0  \\
    \hline
\end{tabular}
\end{center}

\noindent Transition probability matrix
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
      & A    & B  & C \\
    \hline
    A & 0    & 0  & 1  \\
    B & 0.5  & 0  & 0.5  \\
    C & 1    & 0  & 0  \\
    \hline
\end{tabular}
\end{center}

\noindent Transition probability matrix with teleporting
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
      & A    & B    & C      \\
    \hline
    A & 0.03  & 0.03 & 0.94  \\
    B & 0.485 & 0.03 & 0.485 \\
    C & 0.94  & 0.03 & 0.03  \\
    \hline
\end{tabular}
\end{center}

$$x = [1/3,1/3,1/3]$$
$$xP = [0.485, 0.03, 0.485]$$
$$xP^2 = [0.485, 0.03, 0.485]$$

\noindent \textbf{(ii)} First we initialize all hub and authority scores to 1
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
         & A & B & C \\
    \hline
    h(d) & 1 & 1 & 1 \\
    a(d) & 1 & 1 & 1 \\
    \hline
\end{tabular}
\end{center}

\noindent Compute the hub and authority score for each node
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
         & A & B & C \\
    \hline
    h(d) & 1 & 2 & 1 \\
    a(d) & 2 & 0 & 2 \\
    \hline
\end{tabular}
\end{center}

\noindent Normalize
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
         & A     & B     & C \\
    \hline
    h(d) & 0.408 & 0.816 & 0.408 \\
    a(d) & 0.707 & 0     & 0.707 \\
    \hline
\end{tabular}
\end{center}

\noindent Compute the hub and authority score for each node
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
         & A     & B     & C \\
    \hline
    h(d) & 0.707 & 1.414 & 0.707 \\
    a(d) & 1.224 & 0     & 1.224 \\
    \hline
\end{tabular}
\end{center}

\noindent Normalize
\begin{center}
\begin{tabular}{ |l|l|l|l| }
    \hline
         & A     & B     & C \\
    \hline
    h(d) & 0.408 & 0.816 & 0.408 \\
    a(d) & 0.707 & 0     & 0.707 \\
    \hline
\end{tabular}
\end{center}

\noindent \textbf{Question 4d}

\noindent We first find the size of B to A
$$A\cap B = \frac{45}{100}A = \frac{50}{100}B$$
$$\frac{B}{A} = \frac{9}{10}$$

\noindent However, the size of A and B are equal before duplicate elimination. So, there are $\frac{1}{10}$ of B indexes have been eliminated due to duplication. In conclusion, there are 90\% of the web consists of pages that do not have duplications.

\end{multicols*}
\end{document}


\chapter*{Introduction}
\addcontentsline{toc}{chapter}{Introduction}

While simulating a brain is not the goal of machine learning, neuroscience can provide useful inspiration for developing artificial neural networks. The back-propagation algorithm is the \textit{de facto} standard algorithm for computing gradients for learning deep feedforward neural networks, a problem known as the \VUname{credit assignment problem}. It is, however, implausible from a biological point of view (see \cite{crick_recent_1989}). \cite{bengio_towards_2015} presents several arguments for this implausibility:
\begin{enumerate*}[label=(\arabic*)]
	\item Back-propagation is linear, whereas biological neurons needn't be
	\item precise knowledge of gradients of the layers is needed in back-propagation
	\item The feedback path has to use the same weights as the forward path, a feature not possible in two independent neurons (this requirement was later lifted by the Feedback alignment algorithm, see \cite{lillicrap_random_2016} and \cite{nokland_direct_2016})
	\item Biological neurons communicate in spikes instead of continuous values
	\item In back-propagation, the forward and backward passes need to alternate, a mode of operation unlikely to happen biologically
	\item Back-propagation needs a clearly defined output target, a feature not present in biological systems
\end{enumerate*}.

Recent works in computational neuroscience (such as \cite{guerguiev_towards_2017}) point to \VUname{target propagation} as one of the algorithms which could simulate biological neurons. Target propagation can be used in a non-linear fashion, the computation of credit doesn't require the same weights as the actual neural network and theoretically doesn't need an alternating mode of operation. That means that target propagation potentially solves some of the previously outlined issues with back-propagation.

The aim of this work is to evaluate the viability of target propagation in the context of more difficult problems and to incorporate target propagation into an architecture more complicated than a simple multi-layer perceptron. Of special interest is the applicability of target propagation to models for multi-instance learning. The two most easily observed obstacles to a multi-instance target propagation model is the need to learn an approximate inverse of an aggregation function such as the arithmetic mean and the need for a distance function that is well-defined for bags (i. e. sets of vectors). A multi-instance target propagation model would pave the way for multi-instance auto-encoders and possibly broader applications of multi-instance learning in unsupervised an semi-supervised scenarios.

In the following chapters, first the multi-instance learning paradigm is described and formalised in a novel way which hasn't been published in English yet. An overview of multi-instance models and prior art is included. Then, an outline of the back-propagation algorithm is provided as a reference for future comparisons between back-propagation and target propagation. Chapter \ref{targetprop} explains the motivations behind target propagation and the inner workings of the algorithm. Difference target propagation, the most explored variant of target propagation, is described as well. Finally, the target propagation algorithm is evaluated on a simple dataset and the results are discussed.


% Created 2015-10-23 Fri 12:55
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\newcommand{\tustin}{\frac{2}{h}\frac{z-1}{z+1}}
\providecommand{\alert}[1]{\textbf{#1}}

\title{Computerized control - partial exam 2 (dummy)}
\author{Kjartan Halvorsen}
\date{2015-10-16}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs Org-mode version 7.9.3f}}

\begin{document}

\maketitle

\section*{Problem 1}
\label{sec-1}

Figure \ref{fig:step} shows the step response of the system
\[ G(s) = \frac{1}{(s+1)^2(s+3)} \]
for an experiment to determine the ultimate gain and ultimate period.
\begin{figure}
\begin{center}
\includegraphics[]{ultimate_gain_experiment}
\caption{Response of closed-loop system using proportional control with gain equal to the ultimate gain.}
\label{fig:step}
\end{center}
\end{figure}

  \textbf{(a)} What is the ultimate period and the phase-crossover frequency?

  \textbf{(b)} Use the transfer function and the phase-crossover frequency to determine the ultimate gain.

  \textbf{(c)} Determine a suitable continuous-time PID-controller using table 8.3 from Å\&W.

  \textbf{(d)} Obtain a sampled controller by using the backward difference for the D-part and Tustin's approximation for the I-part of the PID-controller.
\section*{Solutions}
\label{sec-2}

  \textbf{(a)}
  \begin{center}
  \includegraphics[]{ultimate_gain_experiment-solution}
  \end{center}
  The phase-crossover frequency is $\omega_p = \frac{2\pi}{T_u} = 2.65$

  \textbf{(b)}
  We know that the phase-crossover frequency is $2.65$ and that at this frequency the Nyquist curve of the open-loop transfer function crosses the negative real axis. Multiplying the transfer function $G(s)$ by the ultimate gain $K_u$ causes the Nyquist curve to cross the negative real axis at -1 (that is why there are self-sustained oscillations). We get
  \[ |K_uG(i\omega_p)| &= 1 \] or
  \begin{align*}
  K_u &= \frac{1}{|G(i\omega_p)|} = |i\omega_p + 1|^2|i\omega_p+3|\\
     &= (\omega_p^2 + 1)\sqrt{\omega_p^2 + 9} \approx 32.11
  \end{align*}

  \textbf{(c)} The controller parameters become
  \[ K_p = 0.6K_u = 19.27, \quad T_i = 0.5T_u = 1.185, \quad T_d = 0.125T_u = 0.296 \]

  \textbf{(d)}
  The controller is
  \begin{align*}
  F_d(z) &= K_p + \frac{K_p}{T_i s' } |_{s'= \tustin{}} + K_pT_ds'|_{s'= \frac{z-1}{zh}}\\
         &= K_p + \frac{K_p}{T_i\tustin{}} + \frac{K_pT_d(z-1)}{zh}\\
         &= K_p + \frac{K_ph(z+1)}{T_i(z-1)} + \frac{K_pT_d(z-1)}{zh}
  \end{align*}

\end{document}


\documentclass{beamer}

\usetheme{uhh}
\showtotalframenumber
\showuhhlogoeachframe
\showsections

\usepackage{amsmath}
\DeclareMathOperator*{\argmin}{arg\,min}

\usepackage{listings}
\lstset{
  language=python
  }

\title{Tensorflow -- DNNs}
\author{Fabian Barteld, Benjamin Milde, Prof. Dr. Chris Biemann}
\date[8.11.2018]{November 8, 2018}

\AtBeginSection[]
{
   %%%%% section title
   % This is how it would look like in Beamer:
   % \begin{frame}
   %     \frametitle{Overview}
   %     \tableofcontents[sections={2-3},currentsection,sectionstyle=show/hide,subsectionstyle=hide]
   % \end{frame}
  \begin{frame}[plain]
  \begin{tikzpicture}[overlay]
    \relax%
    \fill[blueuhh,opacity=1] (-10,-10)
    rectangle(\the\paperwidth,\the\paperheight);
  \end{tikzpicture}
   \begin{tikzpicture}[overlay]
    \relax%
    \fill[white,opacity=1] (-5,-1.2)
    rectangle(\the\paperwidth,0.5) node[pos=0.5,black]{\LARGE\insertsectionhead};
  \end{tikzpicture}
  \end{frame}

  %%%% add subsection to show navigation dots
  \subsection{}
}

\begin{document}

\maketitle

\section{Introduction}

\begin{frame}[fragile]
\frametitle{A POS tagger with DNNs}

\begin{lstlisting}
>>> pos_tag(["The","cat","sits","on","the","mat"])
[('The', 'DT'), ('cat', 'NN'), ('sits', 'VBZ'),
 ('on', 'IN'), ('the', 'DT'), ('mat', 'NN')]
\end{lstlisting}

\begin{itemize}
\item A POS-tagger is labeling words with Part-Of-Speech (POS) tags
\item Important building block in many NLP applications
\item We will create a POS-tagger using Deep Neural Networks (DNN) and word embeddings, step by step
\item Sequence labeling
\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{Sketch of a simple POS tagger model}
 \includegraphics[height=30ex]{postagger1.pdf}
\end{frame}

\section{Embedding layer}

\begin{frame}[fragile]
  \frametitle{Sparse encoding of one-hot values}

  [``A'', ``short'', ``example''] → [3011, 291, 14]
  \vspace{2ex}

  \lstinline{sklearn.preprocessing.LabelEncoder()} does this

  \pause
  .fit → extract the vocabulary from data and assign Integers\\
  .transform → apply the mapping

  \pause
  \textcolor{reduhh}{Does not handle unseen values}

  \pause
  \vspace{2ex}
  Can be used directly to compute the (softmax) cross entropy loss
\begin{lstlisting}
  tf.nn.sparse_softmax_cross_entropy_with_logits(
      labels=y_train, logits=pred)
\end{lstlisting}

  \lstinline{y_train} is expected to be an array of Integers

\end{frame}

%\begin{frame}
%  \frametitle{Cross entropy loss}
%
%  \begin{itemize}
% \item Generalization of the logistic loss to multiple classes:
%  \end{itemize}
%
%  $\xi(t,y) = - \sum_{i=1}^{n} \left[ t_i log(y_i) + (1-t_i)log(1-y_i) \right]$
%
%\end{frame}

\begin{frame}
  \frametitle{Embeddings}

  Embedding layer:\\$emb: V → \mathbb{R}^d$

  \vspace{2ex}
  Embedding matrix:\\$\left| V \right| \times d$ matrix

\end{frame}

 \begin{frame}[fragile]
  \frametitle{Embedding matrix}
  The embeddings matrix is a variable that we want to optimize:
  \begin{lstlisting}
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size,
embedding_size], -1.0, 1.0))
\end{lstlisting}
\end{frame}

 \begin{frame}[fragile]
  \frametitle{Embedding lookup}

  \begin{footnotesize}
 \begin{lstlisting}
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
\end{lstlisting}
\end{footnotesize}

e.g. If your list of sentences is: $\big[[0, 1], [0, 3]\big]$ (sentence 1 is $[0, 1]$, sentence 2 is $[0, 3]$, the function will compute a tensor of embeddings, which will be of shape $(2, 2, \text{embedding\_size})$ and will look like:
\\
$$[[\text{embedding0, embedding1}], [\text{embedding0, embedding3}]]$$

\end{frame}

\begin{frame}
  \frametitle{Flatten a sequence of inputs}

    \includegraphics[height=15ex]{sparse_lookup.png}

    \begin{itemize}
    	\item tf.nn.embedding\_lookup does a sparse matrix multiplication for the lookup
    \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Flatten a sequence of inputs}

  \begin{displaymath}
    \begin{pmatrix} x_{1,1} & \hdots & x_{1,embedding\_size}\\\vdots && \vdots\\x_{n,1} & \hdots & x_{n,embedding\_size}\end{pmatrix}
  \end{displaymath}

  →

  \begin{displaymath}
    \begin{pmatrix} x_{1,1} & \hdots & x_{1,embedding\_size} & \hdots & x_{n,1} & \hdots & x_{n,embedding\_size}\end{pmatrix}
  \end{displaymath}

  \pause\vspace{2ex}

  Tensorflow has \lstinline{tf.reshape} to do that

\end{frame}

\begin{frame}[fragile]
  \frametitle{Flatten a sequence of embeddings}

  Input: tensor of rank 3 ([examples, seq\textunderscore len, embedding\textunderscore size])\\
  Output: tensor of rank 2 ([examples, embedding\textunderscore size*seq\textunderscore len])

  \begin{footnotesize}
\begin{lstlisting}
# tensor 't' is [[[0.344, 1.12], [-0.12, 0.11]],
#                [[0.344, 1.12], [3.1, -1.78]]]
# tensor 't' has shape [2, 2, 2]
# (2 examples of length 2 with embeddings of dim 2)
reshape(t, [2, 4]) ==> [[0.344, 1.12, -0.12, 0.11],
                        [0.344, 1.12, 3.1, -1.78]]
\end{lstlisting}
  \end{footnotesize}

  \vspace{1ex} \pause
  Special value -1:
  If one component of shape is the special value -1, the size of that dimension
  is computed so that the total size remains constant. [\ldots] At most one component of shape can be -1.
  {\tiny\url{https://www.tensorflow.org/api_docs/python/tf/reshape}}

\end{frame}

\section{Multinomial logistic regression}

\begin{frame}
  \frametitle{Softmax function}
$\sigma(\bf{z})_j = \frac{e^{z_j}}{\sum_{k=1}^K e^{z_k}}$ for ''j'' = 1, …, ''K''.

  \begin{itemize}
 	 \item Transforms the vector z, so that all values add up to one
 	 \item Can be used as a probability distribution over outputs (=classes)
 	 \item Commonly used in neural networks as output function for classification tasks
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Cross entropy}

%  \begin{itemize}
%  \item Softmax is a generalization of the Sigmoid function for $n$ categories.
%  \item squashes the values in a vector into values between $0$ and $1$, the sum
%    of the values is $1$
%  \end{itemize}
%
%  \pause

  Cross-entropy (for $C$ classes):

  \begin{displaymath}
    - \sum_{i=1}^n\sum_{c=1}^C y_{ic}\log{p_{ic}}
  \end{displaymath}

$p_{i} p_{i}$ is the true label

  Note: For two classes this is binary-crossentropy

%  In tensorflow use: \lstinline{tf.nn.softmax_cross_entropy_with_logits}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Softmax and cross entropy in Tensorflow}

\begin{lstlisting}
  tf.nn.softmax_cross_entropy_with_logits(
  labels=y_train, logits=pred)
\end{lstlisting}

vs.

\begin{lstlisting}
  tf.nn.sparse_softmax_cross_entropy_with_logits(
  labels=y_train, logits=pred)
\end{lstlisting}

  \begin{itemize}
 \item Difference is how y\_train is given:
 \item e.g. $\big[[0 0 0 1 0], [0 0 0 0 1]\big]$ vs. $[3,4]$ or $[0,0,1,0]$ vs. $[2]$
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Hands on: A simple POS tagger}

  A POS tagger (Brown corpus)

  \begin{itemize}
  \item use an embedding layer
  \item predict the POS tag given the embeddings of the target word and $n$
    context words
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Getting the predicted class}

  \begin{itemize}
  \item Getting the class index:\\
    \lstinline{tf.argmax} returns the index of the largest value in a tensor\pause
  \item Getting the class names:\\
    Use \lstinline{.inverse_transform} of a LabelEncoder to transform sparse
    one-hot encodings back to non-numeric labels
  \end{itemize}

\end{frame}

\section{Deep Feed-Forward Networks}

\begin{frame}
  \frametitle{A feed-forward DNN}
  \begin{center}
    \includegraphics[height=30ex]{03-goldberg-ffn.png}
  \end{center}
  % Goldberg: A Primer on Neural Network Models for Natural Language Processing
\end{frame}

\begin{frame}[fragile]
  \frametitle{Multiple layers}

  First approach: just add layers
  \begin{footnotesize}
\begin{lstlisting}
last_dim = input_dim
for dim in [100, 100]:
    hidden = tf.Variable(tf.random_uniform(
        [last_dim, dim], -0.1, 0.1, dtype=tf.float32))
    b = tf.Variable(tf.random_uniform(
        [dim], -0.1, 0.1, dtype=tf.float32))
    x = tf.add(tf.matmul(x, hidden), b)
    last_dim = dim
\end{lstlisting}
  \end{footnotesize}
  \pause
  Won't add anything to the models capacity:\\
  as each layer is just a linear function this is just composing linear
  functions -- the result is a linear function

\end{frame}

\begin{frame}[fragile]
  \frametitle{Adding non-linearities}
  Different choices: Sigmoid, tanh, ReLU (Rectified Linear Unit)

  \begin{footnotesize}
\begin{lstlisting}
last_dim = input_dim
for dim in [100, 100]:
    hidden = tf.Variable(tf.random_uniform(
        [last_dim, dim], -0.1, 0.1, dtype=tf.float32))
    b = tf.Variable(tf.random_uniform(
        [dim], -0.1, 0.1, dtype=tf.float32))
    x = tf.add(tf.matmul(x, hidden), b)
    x = tf.nn.relu(x)
    last_dim = dim

\end{lstlisting}
  \end{footnotesize}
\end{frame}

\begin{frame}
  \frametitle{Non-linearities}
  %https://www.quora.com/Why-do-non-zero-mean-activations-induce-a-bias-shift-for-units-in-the-next-layer-and-why-is-that-bad
 \includegraphics[width=\textwidth]{nonlinear.png}

 \lstinline{tf.tanh} \hfill \lstinline{tf.sigmoid} \hfill \lstinline{tf.nn.relu}
\end{frame}

\begin{frame}
  \frametitle{MLP with one hidden layer}

  One hidden layer is enough to create a universal approximator.\\\pause

  However:\\
  Neural networks with more layers have been shown to learn functions better.

\end{frame}

\begin{frame}[fragile]
  \frametitle{Example}

  \begin{footnotesize}
\begin{lstlisting}
last_dim = input_dim
for dim in [100, 100]:
    ### add hidden layers as before

## define the output layer
out_w = tf.Variable(tf.random_uniform(
      [last_dim, out_dim], -0.1, 0.1,
      dtype=tf.float32))
b = tf.Variable(tf.random_uniform(
    [out_dim], -0.1, 0.1, dtype=tf.float32))
out = tf.add(tf.matmul(x, out_w), b)
\end{lstlisting}
  \end{footnotesize}
\end{frame}

\begin{frame}
  \frametitle{Hands on 1}

  Add hidden layers to the POS tagger.

\end{frame}

\begin{frame}[fragile]
  \frametitle{Dropout}

\begin{itemize}
\item   Srivastava, Nitish, et al. "Dropout: a simple way to prevent neural networks from overfitting." Journal of machine learning research 15.1 (2014): 1929-1958.
\end{itemize}

  In Tensorflow:
  \begin{verbatim}
   tf.nn.dropout( x,  keep_prob, noise_shape=None,
   									seed=None,   name=None)
  \end{verbatim}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Hands on 2}

  Extend POS tagger with a train a test set, measure accuracy. (Hint: \verb#sklearn.metrics.accuracy_score#)
  Add dropout to the POS tagger and compare train and validation loss+accuracy.

\end{frame}

\begin{frame}[fragile]

 \frametitle{Tensorboard}
  \begin{itemize}
		\item Visualize loss, embeddings and much more in your browser
		\item You need to add a few lines of code to tell Tensorboard what to log
		\item Make sure train\_summary\_dir is a new directory for every new experiment!
	\end{itemize}

	\begin{tiny}
\begin{lstlisting}
 loss_summary = tf.summary.scalar('loss', loss)
 train_summary_op = tf.summary.merge_all()
 summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph)
\end{lstlisting}
\end{tiny}

\end{frame}

\begin{frame}[fragile]

 \frametitle{Tensorboard}
  \begin{itemize}
		\item You need to regularly call the train\_summary\_op in training
		\item Not as often as the training step, because it will otherwise slowdown your training if you have more complex summaries
	\end{itemize}

\begin{tiny}
\begin{lstlisting}
if current_step % 100==0 and current_step != 0:
	summary_str = sess.run(train_summary_op, feed_dict=feed_dict)
	summary_writer.add_summary(summary_str, current_step)
	summary_writer.flush()
\end{lstlisting}
\end{tiny}

\end{frame}

\begin{frame}[fragile]

 \frametitle{Tensorboard}
  \begin{itemize}
		\item Some useful statistics on tensors (e.g. neural network weights):
	\end{itemize}
\begin{tiny}
\begin{lstlisting}
with tf.name_scope('x1'):
        tf.summary.scalar('mean', mean)
        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
        tf.summary.scalar('stddev', stddev)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))
        tf.summary.histogram('histogram', var)
\end{lstlisting}
\end{tiny}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Tensorboard - running it}
 \begin{tiny}
 \begin{lstlisting}
tensorboard  --logdir=w2v_summaries_1499773534
--host=127.0.0.1
or
python3 -m tensorboard.main  --logdir=w2v_summaries_1499773534
--host=127.0.0.1
\end{lstlisting}
\end{tiny}
 \includegraphics[width=0.4\textwidth]{04_loss}
\end{frame}

\begin{frame}[fragile]
 \frametitle{Tensorboard - embeddings}
   \begin{itemize}
		\item Possible to nicely visualize embeddigs, see \url{https://www.tensorflow.org/get_started/embedding_viz}
		\item Also checkout \url{http://projector.tensorflow.org/}, live demo of pretrained embeddings
		\includegraphics[width=0.5\textwidth]{04_embed_viz.png}
	\end{itemize}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% End: