\documentclass[a4paper]{article}

\def\npart{Part IB}

\def\ntitle{Metric \& Topological Spaces}
\def\nlecturer{P.\ M. H.\ Wilson}

\def\nterm{Easter}
\def\nyear{2017}

\input{header}

\begin{document}

\input{titlepage}

\tableofcontents

\section{Metric Spaces}

\subsection{Metric}

Euclidean space $\mathbb{R}^n$ equipped with standard Euclidean inner product $(\cdot,\cdot)$, given $\V x, \V y ∈ \mathbb{R}^n$ with coordinates $x_i, y_i$ respectively,

\[
  (\V x, \V y) = \V x \cdot \V y = \sum_{i=1}^{n}x_i y_i
\]

Euclidean norm $\norm{\V x} = (\V x, \V x)^{1/2}$ which is the length of $\V x$.

Euclidean distance:
\begin{align*}
  d_2: \mathbb{R}^n \times \mathbb{R}^n &\to \mathbb{R} \\
  d_2(\V x, \V y) &= \norm{\V x - \V y} = (\sum_{i=1}^n (x_i - y_i))^{1/2}
\end{align*}

This is an example of a metric.

\begin{definition}
	A metric space $(X, d)$ consists of a set $X$ and a function $d: X \times X \to \mathbb{R}$ such that
	\begin{enumerate}
		\item $d(P, Q) \geq 0$ with equality if and only if $P = Q$,
		\item $\forall P, Q \in X, d(P, Q) = d(Q, P)$,
		\item $\forall P, Q, R \in X, d(P, Q) + d(Q, R) \geq d(P, R)$. (triangle inequality)
	\end{enumerate}
\end{definition}

3 says that, for the Euclidean metric, for any triangle (possibly degenerate) with vertices $P, Q, R$, the sum of lengths of two sides is larger than the length of the third side.

\begin{proposition}
	The Euclidean distance function $d_2$ on $\mathbb{R}^n$ is a metric.
\end{proposition}

\begin{proof}
	1 and 2 are obvious. For 3 we use Cauchy-Schwarz inequality
	\[
		\left(\sum x_i y_i\right)^2 \leq \left(\sum x_i^2\right)\left(\sum y_i^2\right),
	\]
	or in inner product notation
	\[
		(\V x, \V y)^2 ≤ \norm{\V x}^2 \norm{\V y}^2.
	\]

	We may take $P = 0 \in \mathbb{R}^n$ to be the origin, $Q$ has position vector $\V x$ with respect to $P$ and $R$ has position vector $\V y$ with respect to $Q$, and has position vector $\V x + \V y$ with respect to $P$ :
	\begin{align*}
      \norm{\V x + \V y}^2 &= (\V x + \V y, \V x + \V y) \\
                           &= \norm{\V x+\V y, \V x+\V y} \\
                           &= \norm{\V x}^2 + 2(\V x,\V y) + \norm{\V y}^2 \\
                           &\leq \norm{\V x}^2 + 2\norm{\V x} \cdot \norm{\V y} + \norm{\V y}^2 \\
                           &= (\norm{\V x} + \norm{\V y})^2
	\end{align*}
\end{proof}

\begin{lemma}[Cauchy-Schwarz Inequality]
  \[
    \forall \V x,\V y \in \mathbb{R}^n, (\V x,\V y)^2 \leq \norm{\V x}^2 \times \norm{\V y}^2.
  \]
\end{lemma}

\begin{proof}
  The quadratic polynomial on real variable $\lambda$
  \[
    (\lambda \V x + \V y, \lambda \V x + \V y) = \norm{\V x}^2 \lambda^2 + 2(\V x,\V y)\lambda + \norm{\V y}^2
  \]
  is positive semi-definite. Thus by considering the determinant the result follows.
\end{proof}

More examples of metric spaces:
\begin{eg}\leavevmode
  \begin{enumerate}
  \item $X = \mathbb{R}^n, d_1(\V x,\V y) = \sum_{i=1}^n |x_i-y_i|$ and $d_\infty(\V x,\V y) = \max_i |x_i - y_i|$ are both metrics.
  \item For any set $X$, define the \emph{discrete metric}
    \[
      d(x, y) =
      \begin{cases}
        1 & \text{if } x \neq y \\
        0 & \text{if } x = y
      \end{cases}
    \]
  \item $X= C[0,1]$, the set of real continuous functions on $[0,1]$. We can define $d_1, d_2,\ldots d_\infty$ by
    \begin{align*}
      d_1(f,g) &= \int_0^1 |f-g| dx \\
      d_2(f,g) &= \left(\int_0^1(f-g)^2 dx\right)^{1/2} \\
      &\vdots \\
      d_\infty(f,g) &= \sup|f-g|
    \end{align*}
  \item British rail metric: consider $\mathbb{R}^n$ with Euclidean metric, and let $0$ denote the origin. Define a new metric on $\mathbb{R}^n$ by
    \[
      \rho(P,Q) =
      \begin{cases}
        d(P,0) + d(0,Q) &\text{if } P\neq Q \\
        0 &\text{if } P=Q
      \end{cases}
    \]
  \end{enumerate}
\end{eg}

Some metrics in fact satisfy a stronger triangle inequality:

\begin{definition}[Ultra-metric]
  A metric space $(X,d)$ is called an \emph{ultra-metric} if for all \(P, Q, R \in X\),
  \[
    d(P,R) \leq \max\{d(P,Q), d(Q,R)\}.
  \]
\end{definition}

\begin{ex}
  $X = \mathbb{Z}$, $p$ prime. The \emph{$p$-adic metric} is defined by
  \[
    d(m,n) =
    \begin{cases}
      0 & m = n \\
      \frac{1}{p^r} & m \neq n \: \text{where } r = \max\{s\in\mathbb{N}: p^s \divides (m-n)\}
    \end{cases}
  \]
  Claim $d$ is an ultra-metric.
\end{ex}

This extends to a $p$-adic metric on $\mathbb{Q}$. For $x\neq y \in \mathbb{Q}$, write $x-y = p^r \frac{m}{n}, r\in\mathbb{Z}$, $m, n$ coprime ot $p$ and define $d(x,y) = \frac{1}{p^r}$.

\begin{ex}
  The sequence $a_n = 1+p+p^2+ \dots + p^n$ is a convergent sequence in $(\mathbb{Q},d_p)$ with limit $a = \frac{1}{1-p}$ since $p^n | (a_n-a) = \frac{p^n}{p-1}$ for all $n$.
\end{ex}

\begin{definition}[Lipschitz equivalence]
  Two metrics $\rho_1, \rho_2$ on a set $X$ are \emph{Lipschitz equivalent} if there exist \(0 < \lambda_1 \leq \lambda_2\) in \(\R\) such that
  \[
    \lambda_1 \rho_1 \leq \rho_2 \leq \lambda_2\rho_2.
  \]
\end{definition}

This is an equivalence relation.

\begin{remark}
  For metrics $d_1, d_2, d_\infty$ on $\mathbb{R}^n$, one can show that
  \[
    d_1 \geq d_2 \geq d_\infty \geq \frac{d_2}{\sqrt n} \geq \frac{d_1}{n}
  \]
  and so are Lipschitz equivalent.
\end{remark}

\begin{proposition}
  $d_1$ and $d_\infty$ on $C[0,1]$ are not Lipschitz equivalent.
\end{proposition}

\begin{proof}
  For $n \geq 2$, let $f_n \in C[0,1]$ be given by the function as shown. Thus $d_1(f_n,0) =$ area of triangle $\to 0$ as while $d_\infty(f_n,0) \to \sqrt n = \infty$ as $n \to \infty$.
\end{proof}

\begin{center}
  \begin{tikzpicture}
    \draw[->] (-.5,0) -- (3,0);
    \draw[->] (0,-.5) -- (0,3);
    \draw (0,0) -- (1,2) -- (2,0);
    \draw (1,0) node[below] {$\frac{1}{n}$} (2,0) node[below] {$\frac{2}{n}$} (0,2) node[left] {$\sqrt n$};
  \end{tikzpicture}
\end{center}

\begin{ex}
  Show that $d_2(f_n,0) = \sqrt{\frac{2}{3}}$ for all $n$ is not equivalent to either.
\end{ex}

\subsection{Open Ball and Open Set}

Let $(X,d)$ be a metric space, $P\in X, \delta > 0$. The \emph{open ball}
\[
  B_d(P,\delta) = \{Q\in X: d(P,Q) < \delta\}
\]
Often we omit the meric and write $B(P,\delta)$ or $B_\delta(P)$.

\begin{definition}[Open subset]
  A subset $U \subseteq X$ of a metric space $(X,d)$ is called \emph{open} if for all \(P \in U\), there exists \(\delta > 0\) such that $B(P,\delta) \subseteq U$, i.e.\ an open subset is just a union of open balls.
\end{definition}

\begin{definition}[Closed subset]
  A subset $F\subseteq X$ is \emph{closed} if $X\setminus F$ is open.
\end{definition}

Open balls are open and closed balls are closed.

\begin{lemma}\leavevmode
  \begin{enumerate}
  \item $\emptyset, X \subseteq X$ are open subsets of $(X,d)$.
  \item If $\{U_i\}_{i\in I}$ are open sets then so is $\bigcup_{i\in I} U_i$.
    \item If $U_1, U_2$ are open then so is $U_1 \cap U_2$.
  \end{enumerate}
\end{lemma}

\begin{definition}[Open neighbourhood]
  Give $P$ a point of $(X,d)$, an \emph{open neighbourhood} of $P$ is an open set $U \ni P$.
\end{definition}

\subsection{Limit and Continuity}

Suppose $(x_n)$ is a sequence of points in a metric space $(X,d)$. We say $x_n \to x\in X$ (converges to limit $x$) if $d(x_n,x) \to 0$ as $n \to \infty$. Equivalently, $\forall \varepsilon>0, \exists N \forall n\geq N x_n \in B(x, \varepsilon)$.

\begin{remark}
  $x_n \to x$ in $(X,d)$ if and only if for any open neighbourhood $U \ni x \exists N \forall n\geq N x_n \in U$.
\end{remark}
\end{document}


\chapter{Structural Induction}
\label{chapter:structural-induciton}
To illustrate the notions we introduce in this chapter we are going to prove
that in the game from \Cref{section:strong-induction-recursive} it is impossible
to guess the number correctly using less than $10$ questions.

To prove such a statement we need a formal definition of an algorithm that
Bob may use.

\section{Recursive Definitions}
First note that any question for Alice can be formulated as follows:
``Is the value of a function $f$ at your number equal to $\ltrue$?'', where
$f$ is a function from $\Z$ to $\set{0, 1}$ (here and in the sequel
we interpret $1$ as ``yes'' and $0$ as ``no'').

Hence, there are two possible behaviours of any algorithm for Bob.
\begin{itemize}
  \item The algorithm prescribes Bob to just say that the answer is some
    number $x \in \Z$, or
  \item The algorithm prescribes Bob to ask whether $f$ at Alice's number is
    equal to $\ltrue$. If the answer is yes, then the algorithm prescribes Bob
    to proceed according to an algorithm $A_1$, otherwise the algorithm
    prescribe Bob to proceed according to an algorithm $A_0$.
\end{itemize}

Hence, any algorithm for Bob can be described using the following object.
\begin{definition}
  We say that $T$ is a $B$-decision tree if
  \begin{description}
    \item [(base case)] either $T$ is equal to $\DTReturn{y}$, where $y \in \Z$;
      or
    \item [(recursion step)] $T$ is equal to $\DTIf{f}{T_0}{T_1}$,
      where $f : \Z \to \set{0, 1}$, and $T_0$ and $T_1$
      are $B$-decision trees.
  \end{description}
\end{definition}
Note that this definition is not quite formal since it is recursive and
we usually do not allow recursive definitions. So we will need to give a more
formal way to define $B$-decision trees. However, this definition allows us to
prove that
\[
  T =
  \DTIf{f_1}{
    \DTIf{f_2}{
      \DTReturn{1}
     }{
      \DTReturn{2}
     }
  }{
    \DTReturn{3}
  }
\]
is a $B$-decision tree, where
\begin{gather*}
  f_1(x) =
    \begin{cases}
      1 & \text{if } x \le 2 \\
      0 & \text{otherwise}
    \end{cases}, \\
  \text{and} \\
  f_2(x) =
    \begin{cases}
      1 & \text{if } x = 1 \\
      0 & \text{otherwise}
    \end{cases}.
\end{gather*}
This can be explained as follows.
\begin{itemize}
  \item It is clear that $\DTReturn{1}$, $\DTReturn{2}$, and $\DTReturn{3}$ are
    $B$-decision trees by the base case.
  \item Hence, by the recursion step case,
    $\DTIf{f_2}{\DTReturn{1}}{\DTReturn{2}}$ is a $B$-decision tree.
  \item Finally, by the recursion step case, $T$ is a $B$-decision tree.
\end{itemize}

In other words we proved that $T$ is a $B$-decision tree by providing $T_1$,
\dots, $T_5$ such that $T_5 = T$ and for each $i \in [5]$,
$T_i$ is either equal to $\DTReturn{y}$ (for $y \in \Z$) or
$T_i$ is equal to $\DTIf{f}{T_j}{T_k}$ (for $j, k < i$ and
$f : \Z \to \set{0, 1}$).

This idea leads to the framework that would allows us to give a formal
definition of $B$-decision trees.
\begin{definition}
  Let $U$ be a set, let $S_0 \subseteq U$, and let
  \[
    \mathcal{F} =
    \set{F_1 : U^{\ell_1} \to U, \dots, F_n : U^{\ell_n} \to U, \dots}.
  \]
  Then we say that the set $S$ is generated by $\mathcal{F}$ from $S_0$ if
  it is the set of all $u \in U$ such that there is a sequence
  $u_1$, \dots, $u_m$ satisfying the following constraints: $u_m = u$ and
  for each $i \in \range{m}$,
  \begin{itemize}
    \item either $u_i \in S_0$, or
    \item $u_i = F(u_{k_1}, \dots, u_{k_\ell})$
      for $F \in \mathcal{F}$ and $k_1, \dots, k_\ell < i$.
  \end{itemize}
\end{definition}
In case of $B$-decision trees $U$ is the set of all sequences of numbers,
$\mathbf{if}$, $\mathbf{then}$, $\mathbf{else}$, and functions from $\Z$ to $\set{0, 1}$,
$S_0$ is the set of all sequences $\DTReturn{y}$ for $y \in \Z$, and
\[
  \mathcal{F} =
  \set[
    {
      f \text{ is a function from } \Z
        \text{ to } \set{0, 1}
    }
  ]{F_f},
\]
where $F_f(T_0, T_1)$ is equal to $\DTIf{f}{T_0}{T_1}$.

\begin{remark}
  Let $U$ be a set, let $S_0 \subseteq U$, and let
  \[
    \mathcal{F} =
    \set{F_1 : U^{\ell_1} \to U, \dots, F_n : U^{\ell_n} \to U, \dots}.
  \]
  Let us consider the sets $S_0, \dots, S_n, \dots \subseteq U$ such that
  \[
    S_{i + 1} = S_i \cup
    \set[u_1, \dots, u_\ell \in S_i, F \in \mathcal{F}]{F(u_1, \dots, u_\ell)}.
  \]
  It is clear that $\bigcup_{i \ge 0} S_i$ is the set generated by $\mathcal{F}$
  from $S_0$.
\end{remark}

Using similar ideas we may define some functions on the objects defined
recursively.
\begin{definition}
  Let $T$ be a $B$-decision tree. Then the height $\DTHeight{T}$ of $T$ can be
  defined as follows.
  \begin{description}
    \item [(base case)] If $T$ is equal to $\DTReturn{y}$ for $y \in \Z$,
      then $\DTHeight{T} = 0$.
    \item[(recursion step)] Let $T$ be equal to $\DTIf{f}{T_0}{T_1}$, where
      $T_0$ and $T_1$ are $B$-decision trees.
      Then $\DTHeight{T} = \max(\DTHeight{T_0}, \DTHeight{T_1}) + 1$.
  \end{description}
\end{definition}
Note that $\DTHeight{T}$ correpsonds to the worst-case number of queries made by
Bob if we interpret $T$ as a description of Bob's algorithm.

However, before we explain how to formalize such a definition, we need to note
that in the general case such definition may be contradictory. Consider
$U = \R$, $S_0 = \set{0}$, and $\mathcal{F} = \set{f, g}$, where $f(x, y) = xy$
and $g(x) = x + 1$. We define $v : U \to \R$ as follows.
\begin{description}
  \item [(base case)] $v(0) = 0$.
  \item[(recursion step)] $v(f(x, y)) = f(v(x), v(y))$ and
    $v(g(x)) = v(x) + 2$.
\end{description}
Note that $v(f(g(0), g(0))) = f(v(g(0)), v(g(0))) = \big(v(g(0))\big)^2 = 4$
and $v(g(0)) = v(0) + 2 = 2$. However, $g(0) = 1$ and $f(g(0), g(0)) = 1$.

Therefore handle such an issue, we consider $S$ that is \emph{freely} generated
from $S_0$ by by $\mathcal{F}$.
\begin{definition}
  The set $S$ is freely generated by $\mathcal{F}$ from $S_0$ iff it is
  generated by $\mathcal{F}$ from $S_0$,
  $S_0 \cap \Im F = \emptyset$, and $\Im F \cap \Im G = \emptyset$ for any
  $F, G \in \mathcal{F}$.
\end{definition}

The following theorem claims existence of functions defined recursively.
\begin{theorem}
\label{theorem:recursion-principle}
  Let $S \subseteq U$ be the set freely generated from $S_0 \subseteq U$ by
  $\mathcal{F} =
    \set{F_1 : U^{\ell_1} \to U, \dots, F_n : U^{\ell_n} \to U, \dots}$.
  In addition, let $G_0 : S_0 \to V$ and
  $G_1 : V^{\ell_1} \to V, \dots, G_n : V^{\ell_n} \to V$, \dots
  be some functions.

  Then there is a function $h : S \to V$ such that
  \begin{description}
        \item [(base case)] $h(u) = G_0(u)$ for any $u \in S_0$.
        \item[(recursion step)] $h(F_i(u_1, \dots, u_{\ell_i})) =
            G_i(h(u_1), \dots, h(u_{\ell_i}))$ for any $i$ and
            $u_1, \dots, u_{\ell_i} \in S$.
  \end{description}
\end{theorem}

\begin{exercise}
  Prove \Cref{theorem:recursion-principle}.
\end{exercise}

$B$-decision tree are used in this chapter to represent Bob's algorithms;
hence, we need to define values of $B$-decision trees.
\begin{definition}
  The value $\DTValue{T}{n}$ of a $B$-decision tree $T$ at an integer $n$
  can be defined as follows.
  \begin{description}
      \item [(base case)] If $T$ is equal to $\DTReturn{y}$ for $y \in \Z$,
        then $\DTValue{T}{n} = y$.
      \item[(recursion step)] Let $T$ be equal to $\DTIf{f}{T_0}{T_1}$, where
        $T_0$ and $T_1$ are $B$-decision trees, and $f : \Z \to \set{0, 1}$.
        Then
        \[
          \DTValue{T}{n} =
          \begin{cases}
            \mathrm{val}(T_0, n) & \text{if } f(x) = 0 \\
            \mathrm{val}(T_1, n) & \text{otherwise}
          \end{cases}.
        \]
  \end{description}
\end{definition}

Using all these notions we can reformulate the results about Alice and Bob's
game.
\begin{theorem}
\label{theorem:guess-the-number}
  \begin{enumerate}
    \item There is a $B$-decision tree $T$ such that
      \begin{itemize}
        \item $\DTHeight(T) \le 10$ and
        \item $\DTValue{T}{n} = n$ for all $n \in \range{1000}$.
      \end{itemize}
    \item Let $T$ be a $B$-decision tree such that $\DTValue{T}{n} = n$ for
      all $n \in \range{1000}$. Then $\DTHeight{T} \ge 10$.
  \end{enumerate}
\end{theorem}

\begin{exercise}
  Prove the first part of \Cref{theorem:guess-the-number}.
\end{exercise}

\section{Structural Induction Theorem}
To prove the second part of \Cref{theorem:guess-the-number} we need
to introduce the notion of structural induction.

\begin{theorem}[The Structural Induction Principle]
\label{theorem:structural-induction}
    Let $S \subseteq U$ be the set freely generated from $S_0 \subseteq U$ by
    $\mathcal{F} =
      \set{F_1 : U^{\ell_1} \to U, \dots, F_n : U^{\ell_n} \to U, \dots }$.

    Assume that $S' \subseteq U$ is a set such that the following constraints
    are true.
    \begin{description}
        \item [(base case)] $S_0 \subseteq S'$
        \item[(induction step)]
          $F_i(u_1, \dots, u_{\ell_i}) \in S'$ for any
          $u_1, \dots, u_{\ell_i} \in S'$ and $i \in \N$.
    \end{description}
    Then $S \subseteq S'$.
\end{theorem}

Using this result, we may prove \Cref{theorem:guess-the-number}.
\begin{proof}[Proof of \Cref{theorem:guess-the-number}]
  We need to prove only the second part of the statement.
  Let $V(T) = \set[n \in \Z]{\DTValue{T}{n}}$, where $T$ is a $B$-decision
  tree. This proof is based on the following observation.
  \begin{claim}
  \label{claim:guess-the-number}
    For any $B$-decision tree $T$, size of $V(T)$ is at most $2^{\DTHeight{T}}$.
  \end{claim}

  Assume that $T$ is a $B$-decision tree such that $\DTValue{T}{n} = n$ for
  any $n \in \range{1000}$. Whence $\range{1000} \subseteq V(T)$. Therefore
  $V(T)$ has at least $1000$ elements. As a result, $2^{\DTHeight{T}} \ge 1000$;
  which implies that $\DTHeight{T} \ge 10$ since $\DTHeight{T}$ is an integer.

  So we just need to prove \Cref{claim:guess-the-number}. We prove it using
  structural induction. Let $S'$ be the set of decision trees $T$ such that size
  of $V(T)$ is at most $2^{h(T)}$.
  \begin{description}
    \item[(base case)] Let $T$ be equal to $\DTReturn{y}$, where $y \in Z$, then
      $\DTValue{T}{n} = n$ for any $n \in \Z$. Hence, the size of $V(T)$ is
      equal to $1 = 2^0 = 2^{\DTHeight{T}}$.
    \item[(induction step)] Assume that $T$ is equal to $\DTIf{f}{T_0}{T_1}$ for
      some $T_0, T_1 \in S'$. We know that the size of $V(T_0)$ is at most
      $2^{\DTHeight{T_0}}$ and the size of $V(T_0)$ is at most
      $2^{\DTHeight{T_1}}$. In addtion, it is clear that
      $V(T) \subseteq V(T_0) \cup V(T_1)$. Therefore,
      the size of $V(T)$ is at most\footnote{%
        Formally speaking, we use \Cref{theorem:additive-principle} to prove
        this; i.e. we use the fact that the size of a set $A \cup B$ is at most
        the size of $A$ plus the size of $B$.
      }
      \[
        2^{\DTHeight{T_0}} + 2^{\DTHeight{T_1}} \le
          2^{\max(\DTHeight{T_0}, \DTHeight{T_1}) + 1} = 2^{\DTHeight{T}}.
      \]
  \end{description}
  Hence, by \Cref{theorem:structural-induction}, $S'$ is equal to the set of all
  $B$-decision trees.
\end{proof}

Now we are ready to prove \Cref{theorem:structural-induction}.
\begin{proof}[Proof of \Cref{theorem:structural-induction}]
    We prove the statement using induction. More precisely, we prove using
    induction by $m$ that if there is a sequence
    $u_1$, \dots, $u_m$ such that
    for each $i \in \range{m}$, $u_i \in S_0$ or $u_i = F(u_{k_1}, \dots, u_{k_\ell})$
    for $F \in \mathcal{F}$ and $k_1, \dots, k_\ell < i$, then $u_m \in S'$.

    The case when $m = 1$ is clear since in this case $u_1 \in S_0$ which implies
    that it is in $S'$.

    Let us now prove the induction step. Assume that the statement is true for any
    $k \le m$. Consider a sequence $u_1$, \dots, $u_{m + 1}$ such that
    for each $i \in [m + 1]$,
    $u_i \in S_0$ or $u_i = F(u_{k_1}, \dots, u_{k_\ell})$
    for $F \in \mathcal{F}$ and $k_1, \dots, k_\ell < i$. Let us consider
    $F \in \mathcal{F}$ and $k_1, \dots, k_\ell < m + 1$ such that
    $u_{m + 1} = f(u_{k_1}, \dots, u_{k_\ell})$. By the induction hypothesis,
    $u_{k_1}, \dots, u_{k_\ell} \in S'$. Therefore, by the properties of $S'$,
    $u_{m + 1} \in S'$.
\end{proof}

\begin{chapterendexercises}
  \exercise[recommended] Let $S \subseteq \Z$ be a set of size at least $2^k$,
    and let $T$ be a decision tree such that $\DTValue{T}{n} = n$ for any $n
    \in S$. Show that $\DTHeight{T} \ge k$.
  \exercise[recommended] Let $S$ be a set of integers defined recursively such
    that
    \begin{itemize}
      \item $3 \in S$, and
      \item if $x, y \in S$, then $x + y \in S$.
    \end{itemize}
    Show that $S = \set[k \in \N]{3k}$.
  \exercise[recommended]
    Using recursive definition we can define an arithmetic formula on the
    variables $x_1$, \dots, $x_n$,
    \begin{description}
      \item [(base case)] $x_i$ is an arithmetic formula on the variables $x_1$,
        \dots, $x_n$ for all $i$; if $c$ is a real number, then $c$ is also an
        arithmetic formula on the variables $x_1$, \dots, $x_n$.
      \item[(recursion step)] If $P$ and $Q$ are arithmetic formulas on the
        variables $x_1$, \dots, $x_n$, then $(P + Q)$ and $P \cdot Q$ are
        arithmetic formulas on the variables $x_1$, \dots, $x_n$.
    \end{description}

    We can also define recursively the value of such a formula.
    Let $v_1$, \dots, $v_n$ be some integers.
    \begin{description}
      \item[(base cases)] $x_i\big\rvert_{x_1 = v_1, \dots, x_n = v_n} = v_i$;
        in other words, the value of the arithmetic formula $x_i$ is equal to
        $v_i$ when $x_1 = v_1$, \dots, $x_n = v_n$; if $c$ is a real number,
        then $c\rvert_{x_1 = v_1, \dots, x_n = v_n} = c$.
      \item[(recursion steps)] If $P$ and $Q$ are arithmetic formulas on the
        variables $x_1$, \dots, $x_n$, then
        \begin{gather*}
          (P + Q)\big\rvert_{x_1 = v_1, \dots, x_n = v_n} =
          P\big\rvert_{x_1 = v_1, \dots, x_n = v_n} +
          Q\big\rvert_{x_1 = v_1, \dots, x_n = v_n} \\
        \text{and} \\
         (P \cdot Q)\big\rvert_{x_1 = v_1, \dots, x_n = v_n} =
         P\big\rvert_{x_1 = v_1, \dots, x_n = v_n} \cdot
         Q\big\rvert_{x_1 = v_1, \dots, x_n = v_n}.
        \end{gather*}
    \end{description}

    Prove that for any arithmetic formula $A$ on $x$, there is a polynomial
    $p$ such that $p(v) = A\big\rvert_{x = v}$ for any $v \in \R$.
  \exercise
    \begin{itemize}
      \item Define arithmetic formulas with division and define their value (make
        sure that you handled divisions by $0$).
      \item Show that for any arithmetic formula with division $A$ on $x$,
        there are polynomials $p$ and $q$ such that $\frac{p(v)}{q(v)} =
        A\big\rvert_{x = v}$ or $A\big\rvert_{x = v}$ is not defined for
        any real value $v$.
    \end{itemize}
  \exercise[recommended]
    We say that $L$ is a $B$-decision list
    \begin{description}
      \item[(base case)] if either $L$ is equal to $\DLReturn{y}$, where
        $y \in \Z$; or
      \item[(recursion step)] $L$ is equal to $\DLIf{f}{v}{L'}$, where
        $f : \Z \to \set{0, 1}$, $v \in \Z$, and $L$ is a $B$-decision list.
    \end{description}

    We can also define the value $\DLValue{L}{x}$ of a $B$-decision list $L$ at
    $x \in \mathbb{Z}$.
    \begin{description}
      \item[(base case)] Let $L$ be equal to $\DLReturn{y}$, where $y \in \Z$.
        Then $\DLValue{L}{x} = y$.
      \item[(recursion step)] Let $L$ be equal to $\DLIf{f}{v}{L'}$, then
        \[
          \DLValue{L}{x} =
          \begin{cases}
            v & \text{if } f(x) = 1 \\
            \DLValue{L'}{x} & \text{otherwise}
          \end{cases}.
        \]
    \end{description}

    Similarly one may define the length $\DLLength{L}$ of a $B$-decition list $L$.
    \begin{description}
      \item[(base case)] If $L$ is equal to $\DLReturn{y}$, then
        $\DLLength{L} = 1$; and
      \item[(recursion step)] if $L$ is equal to
        $\DLIf{f}{v}{L'}$, then $\DLLength{L} = \DLLength{L'} + 1$.
    \end{description}

    Assume that $\DLValue{L}{x} = x$ for any $x \in [1000]$; show that
    $\DLLength{L} \ge 1000$.
  \exercise
    Let us consider the following game modification of the game studied in this
    chapter. Alice have chosen a number from $1$ to $1000$. Bob wants to
    guess the number so he is asking Alice ``yes'' or ``no'' questions.
    However, he cannot get more than $\ell$ ``yes'' answers; i.e., as soon as Alice
    says $\ell$th ``yes'', Bob is supposed to be able to guess her number.
    \begin{enumerate}
      \item Show that if $\ell = 1$, then Bob needs at least $1000$ quesitons.
      \item Show that if $\ell = 2$, then Bob needs at least $\sqrt{1000}$
        quesions.
      \item Show that if Bob needs at least $\sqrt[\ell]{1000}$ questions.
      \item Show that if $\ell = 2$, there is an algorithm for Bob such that he
        is able to guess the number using $2\ceil{\sqrt{1000}}$ questions.
    \end{enumerate}
  \exercise You have a $100$-story building and two eggs. When you drop an egg
    from any floor of the building, the egg will either break or survive the
    fall. If the egg survives, then it would have survived any lesser fall. If
    the egg breaks, then any greater fall would have broken it as well. The eggs
    are all identical and interchangeable. You’d like to find the minimum height
    that will break an egg. What is the fewest number of drops in which you are
    guaranteed to find the right floor?
\end{chapterendexercises}


\chapter{\projmr3 \ Three Dimensional Data Set}

\section{\projmr3 \ Multiresolution}

\subsection{Introduction}
\subsubsection{The 3D \`a trous Wavelet Transform}
The 3D \`a trous WT of a cube produces $J$ bands, each one being
a cube with the same dimension as the input cube.
The 3D WT of a cube $C(*,*,*)$ is therefore a 4D data set
$W(*,*,*,0:J-1)$, where $J$ is the number of
resolutions used in the decomposition.

\subsubsection{The 3D bi-orthogonal Wavelet Transform}

A one-level 3D sub-band decomposition transforms a cube of $N^3$ pixels into
eight sub-cubes, also called bands, of  $({N\over 2})^3$ pixels.
One of these bands corresponds to the original cube at a lower resolution
(smoothed cube), while the seven other bands are the detail
information, i.e.,
information lost between the two resolution levels, original
and smoothed data resolution level.
A reconstruction of the original input cube can be performed from the
eight sub-bands. The 3D wavelet transform (WT) iterates the decomposition
process on the smoothed cube. For a 3D WT with $P$ scales,
the final number of bands is equal to $7(P-1) + 1$, i.e., seven
detail bands for the first $P-1$ scales, plus the last smoothed array.
For an $N^3$ pixels cube $D(0..N-1, 0..N-1, 0..N-1)$, the eight bands
produced by
one-level decomposition (two scales) are stored in the following way:
\begin{enumerate}
\item Band 1: $B_1({N\over 2}:N-1,0:{N\over 2}-1,0:{N\over 2}-1)$ is obtained
by 1D convolution with filters $ g_x, h_y, h_z$.
\item Band 2: $B_2(0:{N\over 2}-1, {N\over 2}:N-1,0:{N\over 2}-1)$ is obtained
by  1D convolution with filters $ h_x, g_y, h_z$.
\item Band 3: $B_3({N\over 2}:N-1, {N\over 2}:N-1,0:{N\over 2}-1)$ is obtained
by  1D convolution with filters $ g_x, g_y, h_z$.
\item Band 4: $B_4({N\over 2}:N-1,0:{N\over 2}-1, {N\over 2}:N-1)$ is obtained
by  1D convolution with filters $ g_x, h_y, g_z$.
\item Band 5: $B_5(0:{N\over 2}-1, {N\over 2}:N-1,{N\over 2}:N-1)$ is obtained
by  1D convolution with filters $ h_x, g_y, g_z$.
\item Band 6: $B_6({N\over 2}:N-1, {N\over 2}:N-1,{N\over 2}:N-1)$ is obtained
by  1D convolution with filters $ g_x, g_y, g_z$.
\item Band 7: $B_7( 0:{N\over 2}-1, 0:{N\over 2}-1,{N\over 2}:N-1)$ is obtained
by  1D convolution with filters $ h_x h_y g_z $.
\item Band 8: $B_8( 0:{N\over 2}-1, 0:{N\over 2}-1,0:{N\over 2}-1)$ is obtained
by  1D convolution with filters $ h_x, h_y, h_z$.
\end{enumerate}
For the three-scale decomposition, $B_8$ pixels of the 2-scale
decomposition are replaced by bands 8 to 15, each having a size of
 $({N\over 4})^3$  pixels.

\subsection{Multiresolution transform of cube: mr3d\_trans}
\index{mr3d\_trans}
\label{sect_trans3d}
The program
{\em mr3d\_trans} computes the multiresolution transform of a cube by
the \`a trous WT or a non-redundant transform.
The output file which contains the transformation has a
suffix, .mr. If the output file name
given by the user does not contain this suffix, it is automatically
added. The ``.mr'' file is a FITS format file, and can be manipulated by
any package dealing with FITS format, or using the
{\em mr3d\_extract} program.
{\bf
\begin{center}
 USAGE: mr3d\_trans option cube\_in multiresolution\_transform\_out
\end{center}}
where options are:
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-t type\_of\_multiresolution\_transform]}
{\small
\begin{enumerate}
\item  (bi-) orthogonal wavelet transform.
Antonini 7/9 filters ~\cite{wave:antonini92} are used by default, with an
$L_1$ normalization. The filters can be changed using the ``-T'' option, and
an $L_2$ normalization is obtained by the ``-L'' option.
\item Wavelet transform via lifting scheme
\item A trous wavelet transform
\end{enumerate}}
Default is 1.
\item {\bf [-n number\_of\_scales]} \\
 Number of scales used in the multiresolution transform. \\
 Default is 4.
\item {\bf [-T type\_of\_filters]}
{\small
\begin{enumerate}
\itemsep=0.1truecm
\item Antonini 7/9 filters.
\item Daubechies filter 4.
\item Biorthogonal 2/6 Haar filters.
\item Biorthogonal 2/10 Haar filters.
\item Odegard 7/9 filters.
\item User's filters.
\end{enumerate}}
Default is Antonini 7/9 filters.
\item {\bf [-L]} \\
Use an $L_2$ normalization. Default is $L_1$.
\item {\bf [-l type\_of\_lifting\_transform]}
{\small
\begin{enumerate}
\baselineskip=0.4truecm
\item Lifting scheme: CDF WT.
\item Lifting scheme: median prediction.
\item Lifting scheme: integer Haar WT.
\item Lifting scheme: integer CDF WT.
\item Lifting scheme: integer (4,2) interpolating transform.
\item Lifting scheme:  Antonini 7/9 filters.
\item Lifting scheme: integer Antonini 7/9 filters.
\end{enumerate}}
 Default is Lifting scheme: integer Haar WT.
\item {\bf [-i]} \\
Print statistical information about each band.
\end{itemize}
The result is stored in a file (suffix ``.mr''),
and cube (or scales) of the transformation can be extracted
by using the {\em mr3d\_extract} program. \\
\subsubsection*{Examples:}
\begin{itemize}
\itemsep=0.1truecm
\item mr3d\_trans cube.fits cube\_trans.mr \\
Apply the wavelet transform transform to a cube, and store the
result in cube\_trans.mr.
\item mr3d\_trans -T 3 -i cube.fits cube\_trans.mr \\
Same as before, but use Haar filters, and
  print statistical information.
\item mr3d\_trans -t 2 -l 1 cube.fits cube\_trans.mr \\
Apply a wavelet transform via the lifting scheme.
\end{itemize}

\subsection{Extraction of a scale: mr3d\_extract}
\index{mr3d\_extract}
\label{sect_extr3d}

The program
{\em mr3d\_extract} allows the user to extract a  band from
a multiresolution transform file (suffix ``.mr").
{\bf
\begin{center}
 USAGE: mr3d\_extract options multiresolution\_file  output\_cube
\end{center}}
where options are:
\begin{itemize}
\itemsep=0.1truecm
 \item {\bf  [-b band\_number] } \\
 Band number to extract. Default is 1.
\end{itemize}
\subsubsection*{Example:}
\begin{itemize}
\item mr3d\_extract -b 2 mr\_file.mr cube\_band\_2.fits \\
Extract the second band of the wavelet transform, and write as a .fits
file of name ``cube\_band\_2.d''.
\end{itemize}

\subsection{Insertion of an image: mr3d\_insert}
\index{mr3d\_insert}
The program
{\em mr3d\_insert} replaces a band by some cube, by inserting it
in the multiresolution transform file. The band and the cube
must have the same size.
{\bf
\begin{center}
 USAGE: mr3d\_insert options multiresolution\_file input\_cube
\end{center}}
where options are:
\begin{itemize}
\itemsep=0.1truecm
\item  {\bf  [-b band\_number] } \\
 Band number to insert. Default is 1.
\end{itemize}
{\em multiresolution\_file} is the file (.mr) which contains
the multiresolution transformation, {\em input\_cube} is the cube
which must replace the band cube. {\em band \_number} specifies
the band  number to be replaced. Default is 1. \\
The multiresolution transform is updated. \\
\subsubsection*{Example:}
\begin{itemize}
\item mr3d\_insert -b 3 mr\_file.mr cube.fits \\
Insert a cube at the third band of the multiresolution file.
\end{itemize}

\subsection{Reconstruction: mr3d\_recons}
\index{mr3d\_recons}
The program
{\em mr3d\_recons}  reconstructs a cube from its multiresolution
transform.
{\bf
\begin{center}
 USAGE: mr3d\_recons  multiresolution\_file  cube\_out
\end{center}}
{\em multiresolution\_file} is the file (.mr) which contains
the multiresolution transformation, {\em output\_cube} is  the
output reconstructed cube.

\section{\projmr3 Denoising: mr3d\_filter}
\index{filtering}
\subsection{Gaussian noise filtering: mr3d\_filter}
\index{mr3d\_filter}
Program {\em mr\_filter} filters a cube. Only Gaussian noise
is considered.
{\bf
\begin{center}
 USAGE: mr3d\_filter option cube\_in cube\_out
\end{center}}
where options are
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-t type\_of\_multiresolution\_transform]}
{\small
\begin{enumerate}
\item  (bi-) orthogonal wavelet transform.
Antonini 7/9 filters ~\cite{wave:antonini92} are used by default, with an
$L_1$ normalization. The filters can be changed using the ``-T'' option, and
an $L_2$ normalization is obtained by the ``-L'' option.
\item A trous wavelet transform
\end{enumerate}}
\item {\bf [-n number\_of\_scales]} \\
Number of scales used in the multiresolution transform. Default is 4.
\item {\bf [-T type\_of\_filters]}  \\
see~\ref{sect_trans3d}.
\item {\bf [-g sigma]} \\
 The image contains Gaussian noise, and the standard deviation is
given by {\em sigma }. The option should be set only if the user
knows the standard deviation of the noise.
\item {\bf [-s NSigma]} \\
The detection level at each scale is determined by the product
of the standard deviation of the noise by the {\em NSigma}.
{\em NSigma} fixes the confidence interval we want. By default,
{\em NSigma} is equal to 3.
\item {\bf [-C]} \\
Correlated noise.
\end{itemize}
\subsubsection*{Examples:}
\begin{itemize}
\item mr3d\_filter cube\_in.fits cube\_out.fits \\
Filters a cube by multiresolution  thresholding, assuming
Gaussian noise (its standard deviation is automatically estimated).
\item mr3d\_filter -s 4 -g 10. cube\_in.fits cube\_out.fits \\
Same as before, but the noise standard deviation is fixed to 10, and
a  $4\sigma$ thresholding is performed.
\item mr3d\_filter -C cube\_in.fits cube\_out.fits \\
Consider correlated noise instead of Gaussian noise. The noise standard
deviation is estimated at each scale using the MAD (median absolute
deviation) estimator.
\end{itemize}

\newpage

\section{3D Point Clouds Analysis}
We will call {\em Catalog} a set of points in the 3D space, each point
being represented by its three coordinates $X,Y,Z$.

\subsubsection*{ASCII Catalog}
The catalog format is the following:
\subsubsection*{Catalogue format}
\begin{itemize}
\item the first line must contain the number of points $N$, the
  dimension $D$ (i.e. 3), and the coordinate system $S$ ($S$ = 1 or
  2).  The recognized coordinate systems are:
  \begin{enumerate}
  \item $X$ and/or $Y$ and/or $Z$ Euclidien system.
  \item Angular coordinate system (longitude, latitude and/or
    distance; that could be the equatorial system -- right ascension
    $\alpha$, declination $\delta$, the galactic coordinate system
    with galactic longitude $l$, galactic latitude $b$, the
    supergalactic coordinate system with $SL$ and $SB$).
  \end{enumerate}
\item the $D$ following lines must contains three values: the range of
  variation of the $i-th$ coordinate (min,max) and a flag indicating
  the generation model for the corresponding coordinate: 0 -- uniform
  between [min,max], 1 -- bootstrapping the coordinate and 2 -- uniform
  on sphere between [min,max] in degrees. When the user have supplied
  its own generated random catalogs, then those three lines are
  ignored. This last value is used only by programs which need to
  simulate data from the input data, and can generally be set to zero.
\item the $N$ following lines contains the coordinates of the points.
\end{itemize}
An example of a 3D catalogue, with 10 points in the Euclidien system,
each coordinate being defined in the interval $[0,100[$ and random
catalog coordinates uniform in $[0,100[$ for each axis, is:
\begin{verbatim}
       10       3           1
       0      100.000       0
       0      100.000       0
       0      100.000       0
      42.1782      13.4610      73.7444
      41.6855      9.82727      75.3605
      42.3580      14.7867      73.1548
      42.0255      12.3347      74.2453
      42.7474      17.6581      71.8777
      41.9410      11.7113      74.5226
      65.5637      9.84036      71.3585
      65.7140      12.6019      70.5645
      65.5843      10.2196      71.2495
      65.4521      7.79074      71.9479
\end{verbatim}
The file name must have the suffixe ``\.cat''.

\subsection{ASCII Catalog to 3D Cube in FITS Format: mr3d\_cat2fits}
\index{mr3d\_cat2fits}
\label{sect_cat2fits}

The program {\em mr3d\_cat2fits} allows the user to convert a catalog
into a 3D cube in the FITS format using a $B_3$ spline scaling function.
When using the \`a trous Wavelet Transform, this step corresponds to
the projection in the $V_0$ space, and garanties that the obtained
wavelet coefficients are exactly the scalar products of the input
irregularly spaced data with the wavelet function. Assuming Poisson
noise, very robust threshold levels can be derived
in the wavelet space (program {\em mr\_phisto}), which can be used for the
detection of clusters (program {\em mr\_psupport}) or for the
filtering (program {\em mr\_pfilter}). See \cite{starck:book02} for
more details.

{\bf
\begin{center}
 USAGE: mr3d\_cat2fits options catalog cube\_out
\end{center}}
where options are:
\begin{itemize}
\itemsep=0.1truecm
\item {\bf  [-B Bin] } \\
 Bin gives the resolution. Default is 1.
\item {\bf  [-p] } \\
Do not interpolate the data. By default, a interpolation by a spline
a degree 3 is performed. It corresponds to the projection in the
space $V_0$ in the wavelet theory.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mr3d\_cat2fits data.cat cube\_out.fits \\
Convert a catalog into a 3D fits file.
\item mr3d\_cat2fits -p -b5 data.cat cube\_out.fits \\
Ditto, but do not use an interpolation, and take a pixel resolution of 5
(units are the same as in the input catalog).
\item mr3d\_transform -t3 cube\_out.fits trans.mr\\
Wavelet transform of the catalog. The wavelet coefficients in {\em trans.mr}
are exactly the scalar products of the irregularly spaced data with
the wavelet function.
\end{itemize}

\subsection{Wavelet Histogram Autoconvolution: mr3d\_phisto}
\index{mr3d\_phisto}
Program {\em mr3d\_phisto} precomputes a set tables which
is used by {\em mr3d\_psupport} and
{\em mr3d\_pfilter}. The tables are saved
in the FITS table format. They allows the estimation of the detection
levels for a wavelet coefficient caculated from $n$ evenements.
To derive these thresholds, the wavelet funciton histogram must be
autoconvolved $n$ times.
The filenames  are:
\begin{itemize}
\item \_Aba\_histo.fits: 2D array $H[n, 2049]$, which contains
$n$ autoconvolutions.
\item \_histobin.fits: 2D array $B_1[n, 2]$, where $B_2[n,0]$ is the bin
for $H[n,*]$ and $B_1[n,1]$ is the number of points where the $H[n,*]$ is
 different from zero.
\item \_histobound.fits: 2D array $B_2[n, 2]$, where
$B_2[n,0]$ and $B_2[n,1]$  are respectively
the min and the max of the nth histogram.
\item \_histodistrib.fits: 2D array $D[n, 2049]$,  where $D[n,*]$ is
the repartition function of the histogram distribution $H[n,*]$.
\item \_param.fits: 1D array $P[2]$, where $P[0]$ and $P[0]$
are respectively the $r$ (rythm variable) and the $n$
(number of calculated histograms) parameters,
\end{itemize}
% h = rim('_Aba_histo.fits')
% b = rim('_histobin.fits')
% m = rim('_histobound.fits')
% x = indgen(2049,/float)
% x = x * b(10,0) + m(10,0)
% plot,x,h(10,*)
% d = rim('_histodistrib.fits')
% plot, x, d(10,*)

{\bf
\begin{center}
 USAGE: mr3d\_phisto option
\end{center}}
where options are
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-n number\_of\_convolution]} \\
Total number of calculated histograms. Default is 30
(the total of the cube must be lower than $2^{n}$).
\item {\bf [-r rythm]}  \\
 Parameter relative to the rythm of autoconvolution (default=0).
The program computes the $2^n$ autoconvolutions of the wavelet histogram.
It computes also $2^r$ convolutions between $2^n$ and $2^(n+1)$.
\item {\bf [-d]}  \\
Use all default options.
\item {\bf [-v]}  \\
Verbose. Default is no.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item mr3d\_phisto -d \\
Estimates the autoconvoled histograms, and calculates the threshold levels.
\end{itemize}

\subsection{Multiresolution Support Calculation: mr3d\_psupport}
\label{sect_event3D}
\index{mr3d\_psupport}
Program
{\em mr3d\_psupport} applies a wavelet transform using the \`a trous
algorithm to 3D data, assuming that the noise follows a Poisson distribution,
and in the case where we have only few events per pixel \cite{starck:book02}.
Data can either be given by an ASCII table of coordinates in real values, or
by a cube in the fits format.
{\bf
\begin{center}
 USAGE: mr3d\_psupport options  in\_cube  out\_file
\end{center}}
where options are
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-F first\_detection\_scale]} \\
First scale used for the detection. Default is 1.
\item {\bf [-e minimum\_of\_events]}  \\
Minimum number of events for a detection. Default is 5. If a wavelet
coefficient has been calculated from fewer events than this minimum
value, it is not considered as significant.
% \item {\bf [-s SignifStructureAnalysis\_FileName]} \\
% Analyze the detected wavelet coefficients, and write in the file:
% \begin{itemize}
% \baselineskip=0.4truecm
% \item Number of detected structures per scale
% \item Percentage of significant wavelet coefficients
% \item Mean deviation of shape from sphericity
% \item For each detected structure, its volume, its surface, and
% \item its deviation of shape from sphericity,
% \item its angles, its elongation in the three axis directions
% \end{itemize}
% \item {\bf [-t SignifStructureAnalysis\_FileName]} \\
% Same as -s option, but results are stored in an ASCII table format.
% The table contains: scale number, structure number,
% Max\_x, Max\_y, Max\_z, Volume, Surface, Morpho,
% Angle\_Theta, Angle\_Phi, Sigma\_1, Sigma\_2, Sigma\_3.
\item {\bf [-p]}  \\
Detect only positive structure.
% \item {\bf [-f filename\_filtered\_data]}  \\
% File with filtered input data.
\item {\bf [-n number\_of\_scales]}  \\
Number of scales used in the multiresolution transform.
Default is 6. The maximum number of scales depends on the
cube size. For a $32 \times 32 \times 32$ cube, three scales are allowed (four
scale for a $64 \times 64 \times 64$ cube,
five for a $128 \times 128 \times 128$ cube, \dots).
\item {\bf [-E epsilon]}  \\
Value of epsilon used for the threshold (default=1e-3 ).
\item {\bf [-k]}  \\
Suppress isolated pixels in the support. Default is no.
\item {\bf [-R]}  \\
  Remove the structures near the borders.
\item {\bf [-B bin]}  \\
 If the input data is a catalogue, it gives the bin of it (default=1).
% \item {\bf [-w]]}  \\
% Write the multiscale support to the disk.
% \item {\bf [-S]}  \\
% Read the support already computed.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item mr3d\_phisto -d \\
Compute a set a table which will be used by {\em mr3d\_psupport}.
\item mr3d\_psupport -v simcub32.fits w1   \\
Multiresolution support estimation. \\
Creates the two files
w1\_support\_1.fits and w1\_support\_2.fits cor\-res\-pon\-ding
the support of two wavelet scales.
\item mr3d\_psupport -R -k -E1e-4 -v simcub32.fits w1   \\
Ditto, but remove isolated pixels, structures at the border,
and increase the detection the level.
\end{itemize}

\subsection{Data Cube Filtering: mr3d\_pfilter}
\index{mr3d\_pfilter}
Program {\em mr3d\_pfilter} filters an image (as does {\em mr3d\_filter})
assuming  that the noise follows a Poisson distribution and in the case
where we have only few events per pixel.
Data can either be given by an image or by
an ASCII table of coordinates in real values.
As for {\em mr3d\_psupport}, the pre-computed table must exist (i.e.
the program {\em mr3d\_phisto must be executed.}
If the ``-c'' option is used, the user gives a second input image file name.
Then the multiresolution support is estimated from the first image
(given by option ``-a'' or ``-I''), and the second image is filtered
using the multiresolution support of the first.
{\bf
\begin{center}
 USAGE: mr3d\_pfilter option image\_out
\end{center}}
where options are:
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item {\bf [-n number\_of\_scales]}  \\
Number of scales used in the multiresolution transform.
Default is 6.
\item {\bf [-F first\_detection\_scale]} \\
First scale used for the detection. Default is 1.
\item {\bf [-E epsilon]}  \\
Value of epsilon used for the threshold (default=1e-3 ).
\item {\bf [-m minimum\_of\_events]}  \\
Minimum number of events for a detection. Default is 5. If a wavelet
coefficient has been calculated from fewer events than this minimum
value, it is not considered as significant.
\item {\bf [-i number\_of\_iterations]}  \\
 Maximum number of iterations. Default is 0.
 If this option is set, then an iterative reconstruction method is used to
 restore the cube.
\item {\bf [-B bin]}  \\
 If the input data is a catalogue, it gives the bin of it (default=1).
\item {\bf [-R]}  \\
  Remove the structures near the borders.
\item {\bf [-C]}  \\
Smoothness constraint.
\item {\bf [-p]}  \\
Detect only positive structure.
\item {\bf [-w]}  \\
Write the multiscale support to the disk. Each scale of the multiresolution
support is written separately in a FITS file,
with name ``x\_support\_j.fits'', where ``x'' is the output file name
and $j$ is the scale number.
\item {\bf [-S]}  \\
Read the multiresolution support already computed. When we want to
apply the filtering several times with different options, the
multiresolution can calculated the first time ans saved on the disk
with the ``-w'' option, and for the other experiments can read the
pre-computed multiresolution support and do not have to recalculate it.
\item {\bf [-k]}  \\
Suppress isolated pixels in the support. Default is no
\item {\bf [-K]} \\
Suppress the last scale.
\end{itemize}

\noindent

\subsubsection*{Examples:}
\begin{itemize}
\baselineskip=0.4truecm
\itemsep=0.1truecm
\item mr3d\_phisto -d \\
Compute a set a table which will be used by {\em mr3d\_pfilter}.
\item mr3d\_pfilter cube\_in cube\_out \\
Filtering with all default options.
\item mr3d\_pfilter -R -k -E1e-4 cube\_in cube\_out \\
Ditto, but remove isolated pixels and in structures at the border
the multiresolution support, and increase the detection the level.
\end{itemize}


\documentclass[12pt]{cdblatex}
\usepackage{examples}

\begin{document}

\section*{The Riemann curvature tensor}

\begin{cadabra}

   {a,b,c,d,e,f,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   \partial_{#}::PartialDerivative.

   \Gamma^{a}_{b c}::Depends(\partial{#}).
   \Gamma^{a}_{b c}::TableauSymmetry(shape={2}, indices={1,2});

   ;::Symbol;  # Suggsted by Kasper as a way to (possibly) make use of ; legal
               # see https://cadabra.science/qa/473/is-this-legal-syntax?show=478
               # this code works with and without this trick

   # generic rule for first two covariant derivs of a downstairs-vector

   deriv1 := A?_{a ; b} -> \partial_{b}{A?_{a}} - \Gamma^{c}_{a b} A?_{c}.
   deriv2 := A?_{a ; b ; c} -> \partial_{c}{A?_{a ; b}}
                             - \Gamma^{d}_{a c} A?_{d ; b}
                             - \Gamma^{d}_{b c} A?_{a ; d}.

   substitute (deriv2,deriv1)     # cdb (ex01, deriv2)

   Mabc := M_{a ; b ; c}.         # cdb (ex02, Mabc)

   substitute (Mabc,deriv2)       # cdb (ex03, Mabc)

   distribute   (Mabc)            # cdb (ex04, Mabc)
   product_rule (Mabc)            # cdb (ex05, Mabc)

   Macb := M_{a ; c ; b}.         # cdb (ex06, Macb)

   substitute (Macb,deriv2)       # cdb (ex07, Macb)

   distribute   (Macb)            # cdb (ex08, Macb)
   product_rule (Macb)            # cdb (ex09, Macb)

   diff := @(Mabc) - @(Macb).     # cdb (ex10, diff)

   sort_product   (diff)          # cdb (ex11, diff)
   rename_dummies (diff)          # cdb (ex12, diff)
   canonicalise   (diff)          # cdb (ex13, diff)
   sort_sum       (diff)          # cdb (ex14, diff)
   factor_out     (diff,$M_{a?}$) # cdb (ex15, diff)

\end{cadabra}

\begin{minipage}[t]{0.65\textwidth}
\begin{gather*}
   \cdb{ex01}\\
   M_{a;bc} = \cdb{ex02} = \cdb{ex03}\\
   M_{a;cb} = \cdb{ex06} = \cdb{ex07}\\[10pt]
   M_{a;bc} - M_{a;cb} = \cdb{ex15}
\end{gather*}
\end{minipage}
\hskip 0.5cm
\lower16pt\hbox{%
\begin{minipage}[t]{0.35\textwidth}
\begin{latex}
   \begin{gather*}
      \cdb{ex01}\\
      M_{a;bc} = \cdb{ex02} = \cdb{ex03}\\
      M_{a;cb} = \cdb{ex06} = \cdb{ex07}\\[10pt]
      M_{a;bc} - M_{a;cb} = \cdb{ex15}
   \end{gather*}
\end{latex}
\end{minipage}}

\end{document}


\chapter[\texorpdfstring{UN$^\infty$ in Weakly Orthogonal Systems}{UN
  in Weakly Orthogonal Systems}]{\texorpdfstring{Unique Normal Forms
    in\\Weakly Orthogonal Systems}{Unique Normal Forms in Weakly
    Orthogonal Systems}}\label{chap:unwo}

Finitary orthogonal term rewriting systems have unique normal forms.
In fact, weak orthogonality is enough to establish this
property for finitary systems \citep[Chapter 4]{terese-03}.
To what extent can these results be lifted to infinitary rewriting?

In the infinitary setting, orthogonal TRSs exhibit the infinitary
unique normal forms (UN$^\infty$) property
\citep{kennaway-95,klop-de-vrijer-05}. We might expect this property
to generalise to weakly orthogonal systems. After all, the motivation
for allowing trivial critical pairs in these systems is that,
intuitively, they are witnesses of harmless overlap. However, this
intuition turns out to be unjust for the infinitary case.

\section{A Counterexample}\label{sec:counterexample}

We describe a simple counterexample showing that weak orthogonality
does not imply the UN$^\infty$ property \citep{endrullis-10}.

We work in a signature with unary function symbols $D$ and
$U$.\footnote{We can think of $D$ and $U$ as `down' and `up'. The
  original formulation of this TRS uses $P$ and $S$ (`predecessor' and
  `successor'), but to avoid notational conflicts with the
  \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
  constructor for
  \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
  in \Coq, we proceed with this modification.}
In the notation of terms, we omit the brackets around arguments and
assume right-associativity of function symbol application,
e.g.\ writing $DUx$ for $D(U(x))$. A notation for finite repetitions
of a function symbol $f$ terminated by a term $t$ is defined by
\begin{inparaenum}[(i)]
\item $f^0 t = t$ and
\item $f^{n+1} = ff^nt$.
\end{inparaenum}
The infinite nesting $fff \ldots$ of $f$ is written $f^\omega$.

Consider the TRS consisting of the two left-linear rewrite rules
$\rho_1$ and $\rho_2$:
\begin{align*}
  \rho_1 \, : \, DUx \to x \qquad \qquad \qquad
  \rho_2 \, : \, UDx \to x
\end{align*}
This system has two critical pairs $\langle Dx, Dx \rangle$ and
$\langle Ux, Ux \rangle$, both of which are trivial, establishing
weak orthogonality. The infinite term $\du = D^1 U^2 D^3 U^4 \ldots$
has two normal forms. It rewrites to $U^\omega$ in $\omega$ many
$\rho_1$-steps and to $D^\omega$ in $\omega$ many $\rho_2$-steps.
This contradicts UN$^\rewrites$ and therefore also UN$^\infty$.
% give the overlap from which the critical pairs come?

Other interesting properties of this TRS (e.g.\ weak normalisation is
not preserved under rewriting) and a translation to the infinitary
$\lambda \beta \eta$-calculus are discussed by \citet{endrullis-10}.

\subsection{\texorpdfstring{Rewriting $\du$ to
    $U^\omega$}{Rewriting DUUDDD... to
    UUU...}}\label{sub:counterexample}

We show briefly what rewriting $\du$ to $U^\omega$ amounts
to. Rewriting $\du$ to $D^\omega$ is done in a similar way.
An obvious way to define $\du$ by corecursion is via auxiliary terms
$\du'_n$ parameterised by $n$ as follows:
\begin{align*}
  \du = \du'_0 \qquad \qquad \qquad
  \du'_n = U^n D^{n + 1} \du'_{n + 2}
\end{align*}
But a more useful definition for our present purposes, and the one we
stick with, is the slight reformulation: % TODO: why more useful?
\begin{align*}
  \du = \du'_0 \qquad \qquad \qquad
  \du'_n = D^{2 n + 1} U^{2 n + 2} \du'_{n + 1}
\end{align*}
For any term $t$ and natural numbers $n,m$ we have $U^n D^{m+1}
U^{m+1} t \rightarrow_{\rho_1} U^n D^m U^m t$ and thus $U^n D^m U^m t
\rewrites U^n t$ by iterating $m$ such steps. Instantiating
$m$ with $2 n + 1$ and $t$ with $U \du'_{n + 1}$, we obtain
%$S^n P^{2 n + 1} S^{2 n + 1} S \du'_{n + 1} \equiv S^n \du'_n$
$U^n \du'_n \rewrites U^{n+1} \du'_{n + 1}$ for any $n$.
Concatenating these sequences, iterating $n$ from $0$ onwards, we
arrive at $\du \rewrites U^\omega$.
% TODO: why not use definitions:
% vu(n) = U^n vd(n+1)
% vd(n) = D^n vu(n+1)

\section{The Counterexample in \Coq}

We implement the counterexample from Section~\ref{sec:counterexample}
using the \Coq development described in
Chapter~\ref{chap:implementation}.

The rewrite rules $\rho_1$ and $\rho_2$ are straightforwardly defined
and shown to be left-linear. By a simple proof we obtain that all
critical pairs are trivial and hence that the TRS is weakly
orthogonal.
\begin{singlespace}
\begin{coqdoccode}
%\coqdocnoindent
%\coqdockw{Definition}
%\coqdef{ExampleUNWO.UNWOtrs}{UNWO\_trs}{\coqdocdefinition{$\mathcal{R}$}}
%:=
%\coqdocdefinition{$\rho_1$} ::
%\coqdocdefinition{$\rho_2$} ::
%\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nil}{\coqdocconstructor{nil}}.\coqdoceol
%\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdoclemma{wo$_\mathcal{R}$} :
\coqref{Rewriting.weaklyorthogonal}{\coqdocdefinition{weakly\_orthogonal}}
%\coqref{ExampleUNWO.UNWOtrs}{\coqdocdefinition{$\mathcal{R}$}}.\coqdoceol
\coqdocdefinition{$\mathcal{R}$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
We introduce the notation \begin{coqdoccode}\coqdocvariable{f} @
  \coqdocvariable{t}\end{coqdoccode} to mean
\begin{coqdoccode}\coqref{Term.Fun}{\coqdocconstructor{Fun}}
  \coqdocvariable{f} (\coqdocdefinition{vcons} \coqdocvariable{t}
  (\coqdocdefinition{vnil}
  \coqref{Term.term}{\coqdocinductive{term}}))\end{coqdoccode}.
For brevity, in the following discussion we focus on the function symbol $U$
and omit analoguous definitions using the function symbol $D$.
The infinite term $U^\omega$ is defined
by corecursion and finite repetitions $U^n t$ are defined by recursion
(and are assumed to generalise to contexts with the same notation).
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{CoFixpoint}
\coqdef{ExampleUNWO.repeatU}{repeat\_U}{\coqdocdefinition{U$^\omega$}}
: \coqref{Term.term}{\coqdocinductive{term}} :=
\coqdocconstructor{U} @
\coqref{ExampleUNWO.repeatU}{\coqdocdefinition{U$^\omega$}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{ExampleUNWO.Unt}{Unt}{\coqdocdefinition{U}}$^\coqdocvar{n}$
\coqdocvar{t} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{n} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{O}{\coqdocconstructor{O}}
\ensuremath{\Rightarrow} \coqdocvariable{t}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvar{n} \ensuremath{\Rightarrow}
\coqdocconstructor{U} @
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocvariable{t})\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Unfortunately, $\du$ is not as easily defined. Although clearly
productive, direct translations of the corecursive definitions in
Subsection~\ref{sub:counterexample} do not satisfy \Coq's guardedness
condition (see also Section~\ref{sec:guardedness}). The conclusion of
a \emph{trial and error} approach is that we must use anonymous cofix
constructions. The definition we proceed with is the following:
% wording 'trial and error approach' is not so nice
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{CoFixpoint}
\coqdef{ExampleUNWO.psi'}{psi'}{\coqdocdefinition{$\du'$}} \coqdocvar{n}
: \coqref{Term.term}{\coqdocinductive{term}} :=\coqdoceol
\coqdocindent{1.00em}
(\coqdocvar{cofix} \coqdocvar{Ds} (\coqdocvar{d} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}})
:=\coqdoceol
\coqdocindent{2.00em}
\coqdockw{match} \coqdocvariable{d} \coqdockw{with}\coqdoceol
\coqdocindent{2.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{O}{\coqdocconstructor{O}}
\ensuremath{\Rightarrow} \coqdocconstructor{D}
@ (\coqdocvar{cofix} \coqdocvar{Us} (\coqdocvar{u} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}})
:=\coqdoceol
\coqdocindent{7.50em}
\coqdockw{match} \coqdocvariable{u} \coqdockw{with}\coqdoceol
\coqdocindent{7.50em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{O}{\coqdocconstructor{O}}
\ensuremath{\Rightarrow}
\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}}
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{n})\coqdoceol
\coqdocindent{7.50em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvar{u} \ensuremath{\Rightarrow}
\coqdocconstructor{U} @
\coqdocconstructor{U} @ (\coqdocvariable{Us}
\coqdocvariable{u})\coqdoceol
\coqdocindent{7.50em}
\coqdockw{end})
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{n})\coqdoceol
\coqdocindent{2.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvar{d} \ensuremath{\Rightarrow}
\coqdocconstructor{D} @
\coqdocconstructor{D} @
(\coqdocvariable{Ds} \coqdocvariable{d})\coqdoceol
\coqdocindent{2.00em}
\coqdockw{end}) \coqdocvariable{n}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{ExampleUNWO.psi}{psi}{\coqdocdefinition{$\du$}} :=
\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}} 0.\coqdoceol
\end{coqdoccode}
\end{singlespace}

We now prove that $U^\omega$ and $D^\omega$ are (distinct) normal
forms. This is a tedious proof that essentially consists of analysing
the occurrence of a redex in these terms, yielding that there can not
be such an occurrence.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma} \coqdoclemma{nf$_{\text{U}^\omega}$} :
\coqref{Rewriting.normalform}{\coqdocdefinition{normal\_form}}
(\coqdocvar{system} :=
\coqdocdefinition{$\mathcal{R}$})
\coqref{ExampleUNWO.repeatU}{\coqdocdefinition{U$^\omega$}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma} \coqdoclemma{nf$_{\text{D}^\omega}$} :
\coqdocdefinition{normal\_form}
(\coqdocvar{system} :=
\coqdocdefinition{$\mathcal{R}$})
\coqdocdefinition{D$^\omega$}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdoclemma{neq$^{\text{U}^\omega}_{\text{D}^\omega}$} :
\ensuremath{\lnot}
\coqref{ExampleUNWO.repeatU}{\coqdocdefinition{U$^\omega$}}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocdefinition{D$^\omega$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

Constructing a rewrite sequence from $\du$ to $U^\omega$ is done in
much the same way as described in
Subsection~\ref{sub:counterexample}. First, we define the parameterised
step that is used in the rewrite sequence. It eliminates one pair of $D,
U$ constructors in a term of the form $U^n D^{m+1} U^{m+1} t$. The
omitted argument of the \coqref{Rewriting.Step}{\coqdocconstructor{Step}}
constructor (denoted by \coqdoclemma{\_}) is a proof of $\rho_1 \in
\mathcal{R}$. Note that \coqdocvariable{x} is the variable that is
used in both rewrite rules.
%TODO: names of definitions are not so nice and we need some notes
%about the fact lemmas
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{ExampleUNWO.sigma}{sigma}{\coqdocdefinition{$\sigma$}}
\coqdocvar{t} (\coqdocvar{y} :
\coqdocdefinition{X}) :
\coqref{Term.term}{\coqdocinductive{term}} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocdefinition{beq\_var} \coqdocvariable{y} \coqdocvariable{x} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{true}{\coqdocconstructor{true}} \ensuremath{\Rightarrow}
\coqdocvariable{t}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{false}{\coqdocconstructor{false}} \ensuremath{\Rightarrow}
\coqref{Term.Var}{\coqdocconstructor{Var}}
\coqdocvariable{y}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{ExampleUNWO.facttermbisUmDSnUSnt}{fact\_term\_bis\_UmDSnUSnt}{\coqdoclemma{fact$_\pi^\text{source}$}}
:
\ensuremath{\forall} (\coqdocvar{n} \coqdocvar{m} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}})
(\coqdocvar{t} :
\coqref{Term.term}{\coqdocinductive{term}}),\coqdoceol
\coqdocindent{1.00em}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$
$\Box$)[\coqref{Substitution.substitute}{\coqdocdefinition{substitute}}
  (\coqref{ExampleUNWO.sigma}{\coqdocdefinition{$\sigma$}}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$
    \coqdocvariable{t})) (\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocdefinition{$\rho_1$})] \coqref{TermEquality.termbis}{$\bis$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}} \coqdocvariable{m}}$
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{m}}$
\coqdocvariable{t}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{ExampleUNWO.facttermbisUmDnUnt}{fact\_term\_bis\_UmDnUnt}{\coqdoclemma{fact$_\pi^\text{target}$}}
:
\ensuremath{\forall} (\coqdocvar{n} \coqdocvar{m} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}})
(\coqdocvar{t} :
\coqref{Term.term}{\coqdocinductive{term}}),\coqdoceol
\coqdocindent{1.00em}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$
$\Box$)[\coqref{Substitution.substitute}{\coqdocdefinition{substitute}}
  (\coqref{ExampleUNWO.sigma}{\coqdocdefinition{$\sigma$}}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$
    \coqdocvariable{t})) (\coqref{Rewriting.rhs}{\coqdocprojection{rhs}}
\coqdocdefinition{$\rho_1$})] \coqref{TermEquality.termbis}{$\bis$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$
\coqdocvariable{t}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{ExampleUNWO.pi}{pi}{\coqdocdefinition{$\pi$}}
\coqdocvar{n} \coqdocvar{m} \coqdocvar{t} :
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}} \coqdocvariable{m}}$
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{m}}$
\coqdocvariable{t} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$
\coqdocvariable{t} :=\coqdoceol
\coqdocindent{1.00em}
\coqref{Rewriting.Step}{\coqdocconstructor{Step}}
\coqdocdefinition{$\rho_1$}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$ $\Box$)
(\coqref{ExampleUNWO.sigma}{\coqdocdefinition{$\sigma$}}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$ \coqdocvariable{t}))
\coqdoclemma{\_}
(\coqref{ExampleUNWO.facttermbisUmDSnUSnt}{\coqdoclemma{fact$_\pi^\text{source}$}}
\coqdocvariable{n} \coqdocvariable{m} \coqdocvariable{t})
(\coqref{ExampleUNWO.facttermbisUmDnUnt}{\coqdoclemma{fact$_\pi^\text{target}$}}
\coqdocvariable{n} \coqdocvariable{m} \coqdocvariable{t}).\coqdoceol
\end{coqdoccode}
\end{singlespace}
Generalising these rewrite steps
\coqref{ExampleUNWO.pi}{\coqdocdefinition{$\pi$}}, we construct
the rewrite sequences
\coqref{ExampleUNWO.phia}{\coqdocdefinition{$\varphi_a$}}. In their
recursive definition, the \coqdocdefinition{snoc}
function is used to
prepend \begin{coqdoccode}(\coqref{ExampleUNWO.pi}{\coqdocdefinition{$\pi$}}
\coqdocvariable{n} \coqdocvariable{m}
\coqdocvariable{t})\end{coqdoccode} to
\begin{coqdoccode}(\coqref{ExampleUNWO.phia}{\coqdocdefinition{$\varphi_a$}}
\coqdocvariable{n} \coqdocvariable{m}
\coqdocvariable{t})\end{coqdoccode}. Doing some arithmetic, we obtain
that these rewrite sequences can be used to define rewrite sequences
\coqref{ExampleUNWO.phib}{\coqdocdefinition{$\varphi_b$}} of a more
useful type.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{ExampleUNWO.phia}{phia}{\coqdocdefinition{$\varphi_a$}}
\coqdocvar{n} \coqdocvar{m} \coqdocvar{t} :
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocdefinition{D}$^\coqdocvariable{m}$
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{m}$
\coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocvariable{t} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{m} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdocconstructor{O}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Nil}{\coqdocconstructor{Nil}}
(\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
\coqdocvariable{t})\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdocconstructor{S}
\coqdocvar{m} \ensuremath{\Rightarrow}
\coqref{Rewriting.snoc}{\coqdocdefinition{snoc}}
(\coqref{ExampleUNWO.pi}{\coqdocdefinition{$\pi$}}
\coqdocvariable{n} \coqdocvariable{m} \coqdocvariable{t})
(\coqref{ExampleUNWO.phia}{\coqdocdefinition{$\varphi_a$}}
\coqdocvariable{n} \coqdocvariable{m} \coqdocvariable{t})\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
%\coqdockw{Program Definition}
\coqdockw{Definition} % here the S(2 n) is not correct (?) -- i think it is
\coqdef{ExampleUNWO.phib}{phib}{\coqdocdefinition{$\varphi_b$}}
\coqdocvar{n} : \coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
(\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}}
\coqdocvariable{n}) \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}} \coqdocvariable{n}}$
(\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}}
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{n})) :=\coqdoceol
\coqdocindent{1.00em}
\coqref{ExampleUNWO.phia}{\coqdocdefinition{$\varphi_a$}}
\coqdocvariable{n} (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
(2
$\times$ \coqdocvariable{n})) (\coqdocdefinition{U} @
\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}} (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{n})).\coqdoceol
\end{coqdoccode}
\end{singlespace}
We concatenate all rewrite sequences
\coqref{ExampleUNWO.phib}{\coqdocdefinition{$\varphi_b$}} to construct
rewrite sequences from $\du$ to a term that is equal to $U^\omega$ up
to any given depth.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{ExampleUNWO.phic}{phic}{\coqdocdefinition{$\varphi_c$}}
\coqdocvar{n} : \coqref{ExampleUNWO.psi}{\coqdocdefinition{$\du$}}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
(\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}}
\coqdocvariable{n}) :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{n} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdocconstructor{O}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Nil}{\coqdocconstructor{Nil}}
\coqref{ExampleUNWO.psi}{\coqdocdefinition{$\du$}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdocconstructor{S}
\coqdocvar{n} \ensuremath{\Rightarrow}
\coqref{Rewriting.append}{\coqdocdefinition{concat}}
(\coqref{ExampleUNWO.phic}{\coqdocdefinition{$\varphi_c$}}
\coqdocvariable{n})
(\coqref{ExampleUNWO.phib}{\coqdocdefinition{$\varphi_b$}}
\coqdocvariable{n})\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

The definition of the final rewrite sequence
\coqref{ExampleUNWO.phi}{\coqdocdefinition{$\varphi$}} is done by combining
\coqref{ExampleUNWO.phic}{\coqdocdefinition{$\varphi_c$}} with a proof
that the target terms converge to $U^\omega$.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{ExampleUNWO.conv}{conv}{\coqdoclemma{conv$_{\varphi_c}$}}
: \coqref{Rewriting.converges}{\coqdocdefinition{converges}}
(\coqdockw{fun} \coqdocvar{n} \ensuremath{\Rightarrow}
\coqref{ExampleUNWO.Unt}{\coqdocdefinition{U}}$^\coqdocvariable{n}$
(\coqref{ExampleUNWO.psi'}{\coqdocdefinition{$\du'$}}
\coqdocvariable{n}))
\coqref{ExampleUNWO.repeatU}{\coqdocdefinition{U$^\omega$}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{ExampleUNWO.phi}{phi}{\coqdocdefinition{$\varphi$}}
: \coqref{ExampleUNWO.psi}{\coqdocdefinition{$\du$}}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqref{ExampleUNWO.repeatU}{\coqdocdefinition{U$^\omega$}} :=
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}
\coqref{ExampleUNWO.phic}{\coqdocdefinition{$\varphi_c$}}
\coqref{ExampleUNWO.conv}{\coqdoclemma{conv$_{\varphi_c}$}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdoclemma{wf$_\varphi$}
: \coqref{Rewriting.wf}{\coqdocdefinition{wf}}
\coqref{ExampleUNWO.phi}{\coqdocdefinition{$\varphi$}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

We can prove $\du \rewrites D^\omega$ in a similar way and
conclude by proving our main theorem.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdoclemma{no\_un$_\mathcal{R}$}
: \ensuremath{\lnot}
\coqref{Rewriting.uniquenormalforms}{\coqdocdefinition{unique\_normal\_forms}}
\coqdocdefinition{$\mathcal{R}$}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Theorem} \coqdoclemma{no\_un\_wo}
: \ensuremath{\lnot} \ensuremath{\forall} \coqdocvar{F} \coqdocvar{X}
\coqdocvar{$\mathcal{R}$},\coqdoceol
\coqdocindent{1.00em}
\coqdocdefinition{weakly\_orthogonal}
(\coqdocvar{F} := \coqdocvariable{F}) (\coqdocvar{X} :=
\coqdocvariable{X}) \coqdocvariable{$\mathcal{R}$}
\ensuremath{\rightarrow}
\coqdocdefinition{unique\_normal\_forms}
\coqdocvariable{$\mathcal{R}$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}