\chapter{A Mechanical Formalisation}\label{chap:implementation}

% nogmaals url voor code noemen?

In this chapter we present a formalisation of some of the notions from
Chapter~\ref{chap:rewriting} in the \Coq proof
assistant. Section~\ref{sec:ordimp} translates the
tree ordinals from Subsection~\ref{sub:tree} to \Coq. Coinductive
terms are defined in Section~\ref{sec:terms}. In Section~\ref{sec:seq}
we present a novel representation of transfinite rewrite sequences
based on the structure of the tree ordinals. This we regard as the main
contribution of this thesis.

%\Coq has been used for sizeable projects such as CompCert and a
%verified proof of the Four Color Theorem.

%Examples of formalisations of mathematical theories in \Coq:
%\begin{compactenum}
%\item Logic: A proof of G\"odel's First Incompleteness Theorem (Russel
%  O'Connor).
%\item Analysis: Exact real arithmetic (Russel O'Connor).
%\end{compactenum}

A short introduction to \Coq is included in
Appendix~\ref{chap:coq}. In the \Coq code fragments, we take some
notational liberties in favour of readability. Sometimes we omit (part
of) the type information. We also freely use infix notations without
declaration. Furthermore, variable and definition names are typeset
liberally.

Some definitions have implicit arguments, meaning those arguments can
be inferred by the system from the context. As an example, consider
the inductive type
\coqref{CoqIntro.natpos}{\coqdocinductive{nat$^+$}} whose constructor
takes as arguments a natural number \coqdocvar{n} and a proof that
\coqdocvar{n} is greater than $0$.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive}
\coqdef{CoqIntro.natpos}{nat\_pos}{\coqdocinductive{nat$^+$}} :
\coqdockw{Set} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{CoqIntro.Pos}{Pos}{\coqdocconstructor{Pos}} :
\ensuremath{\forall} \coqdocvar{n} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}},
0 < \coqdocvariable{n} \ensuremath{\rightarrow}
\coqref{CoqIntro.natpos}{\coqdocinductive{nat$^+$}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
The argument \coqdocvar{n} of
\coqref{CoqIntro.Pos}{\coqdocconstructor{Pos}} can be implicit,
since it can be inferred from (the type of) the other argument. If
\coqdocvar{H} has type \begin{coqdoccode}0 < 3\end{coqdoccode}, we can
write \begin{coqdoccode}\coqref{CoqIntro.Pos}{\coqdocconstructor{Pos}}
  \coqdocvariable{H}\end{coqdoccode} instead
of \begin{coqdoccode}\coqref{CoqIntro.Pos}{\coqdocconstructor{Pos}} 3
  \coqdocvariable{H}\end{coqdoccode}.

Related work are the \CoLoR \citep{blanqui-koprowski-10} and \Coccinelle
\citep{contejean-07} projects, libraries on finitary rewriting and
termination, and the representation of ordinal numbers up to
$\epsilon_0$ and $\Gamma_0$ in Cantor and Veblen normal form by
\citet{casteran-06}.

\section{Ordinal Numbers}\label{sec:ordimp}

In the theory of infinitary rewriting, the lengths of rewrite sequences play
a central role. One might even suspect that any representation of transfinite
rewrite sequences needs a representation of ordinal numbers. But this is not
the case.

Consider as an illustration the example of finite lists. They can be
naturally represented inductively, without the need for a
representation of natural numbers. The usual inductive definition of lists,
using constructors \coqdocconstructor{Nil} and \coqdocconstructor{Cons}, can
be seen as a generalisation of the natural numbers, defined inductively using
constructors \coqdocconstructor{Zero} and \coqdocconstructor{Successor}. The
generalisation consists of labeling the \coqdocconstructor{Cons} constructors
with list members.

Likewise, we now turn to the definition of tree ordinals as a case study in
preparation for the definition of transfinite rewrite sequences in
Section~\ref{sec:seq}.

We define the ordinal numbers using the representation of tree
ordinals (cf.~Definition~\ref{def:ordinals}) in \Coq by
\coqref{Ordinal.ord}{\coqdocinductive{ord}}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive} \coqdef{Ordinal.ord}{ord}{\coqdocinductive{ord}} :
\coqdockw{Set} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Ordinal.Zero}{Zero}{\coqdocconstructor{Zero}}  :
\coqref{Ordinal.ord}{\coqdocinductive{ord}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Ordinal.Succ}{Succ}{\coqdocconstructor{Succ}}  :
\coqref{Ordinal.ord}{\coqdocinductive{ord}} \ensuremath{\rightarrow}
\coqref{Ordinal.ord}{\coqdocinductive{ord}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Ordinal.Limit}{Limit}{\coqdocconstructor{Limit}} :
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqref{Ordinal.ord}{\coqdocinductive{ord}})
\ensuremath{\rightarrow}
\coqref{Ordinal.ord}{\coqdocinductive{ord}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Arithmetic operations on ordinals, such as addition, are easily
defined.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint} \coqdef{Ordinal.add}{add}{$+$}
(\coqdocvar{$\alpha$} \coqdocvar{$\beta$} :
\coqref{Ordinal.ord}{\coqdocinductive{ord}}) :
\coqref{Ordinal.ord}{\coqdocinductive{ord}} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\beta$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Zero}{\coqdocconstructor{Zero}}
\ensuremath{\Rightarrow} \coqdocvariable{$\alpha$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Succ}{\coqdocconstructor{Succ}}
\coqdocvar{$\beta$} \ensuremath{\Rightarrow}
\coqref{Ordinal.Succ}{\coqdocconstructor{Succ}}
(\coqdocvariable{$\alpha$} \coqref{Ordinal.add}{$+$}
\coqdocvariable{$\beta$})\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Limit}{\coqdocconstructor{Limit}}
\coqdocvar{f}   \ensuremath{\Rightarrow}
\coqref{Ordinal.Limit}{\coqdocconstructor{Limit}} (\coqdockw{fun}
\coqdocvar{n} \ensuremath{\Rightarrow}
\coqdocvariable{$\alpha$} \coqref{Ordinal.add}{$+$}
(\coqdocvariable{f} \coqdocvariable{n}))\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
In fact, all definitions from Subsection~\ref{sub:tree} translate directly to
\Coq code. We can now prove basic properties of $\preceq$, for example
that it is transitive and that, for the finite ordinals, it coincides
with the standard order on the natural numbers.\footnote{Although
  \coqdocvariable{n} and \coqdocvariable{m} have type
  \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
  and $\preceq$ has type \coqref{Ordinal.ord}{\coqdocinductive{ord}}
  $\rightarrow$ \coqref{Ordinal.ord}{\coqdocinductive{ord}}
  $\rightarrow$ \coqdockw{Prop}, we can state the lemma in this
  concise way by defining the trivial coercion from
  \coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
  to \coqref{Ordinal.ord}{\coqdocinductive{ord}}.}
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{Ordinal.ordletrans}{ord\_le\_trans}{\coqdoclemma{\ensuremath{\preceq_{\text{trans}}}}}
:
\ensuremath{\forall} \coqdocvar{\ensuremath{\alpha}}
\coqdocvar{\ensuremath{\beta}}
\coqdocvar{\ensuremath{\gamma}}, \coqdocvariable{\ensuremath{\alpha}}
\ensuremath{\preceq} \coqdocvariable{\ensuremath{\beta}}
\ensuremath{\rightarrow}
\coqdocvariable{\ensuremath{\beta}} \ensuremath{\preceq}
\coqdocvariable{\ensuremath{\gamma}}
\ensuremath{\rightarrow} \coqdocvariable{\ensuremath{\alpha}}
\ensuremath{\preceq}
\coqdocvariable{\ensuremath{\gamma}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{Ordinal.ordlele}{ord\_le\_le}{\coqdoclemma{\ensuremath{\preceq_\text{nat}}}} :
\ensuremath{\forall} \coqdocvar{n} \coqdocvar{m}, \coqdocvariable{n}
\ensuremath{\le} \coqdocvariable{m} \ensuremath{\leftrightarrow}
\coqdocvariable{n} \ensuremath{\preceq} \coqdocvariable{m}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

Recalling our discussion in Subsection~\ref{sub:tree} of limit ordinals whose
sequences do not actually approximate to a limit ordinal, we consider the
lemma
\coqref{Ordinal.ordlezeroright}{\coqdoclemma{\ensuremath{\preceq_{\text{zero\_right}}}}}
as an example of this issue.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{Ordinal.ordlezeroright}{ord\_le\_zero\_right}{\coqdoclemma{\ensuremath{\preceq_{\text{zero\_right}}}}}
:
\ensuremath{\forall} \coqdocvar{\ensuremath{\alpha}} \coqdocvar{\ensuremath{\beta}},
\coqdocvariable{\ensuremath{\alpha}} \ensuremath{\preceq}
\coqref{Ordinal.Zero}{\coqdocconstructor{Zero}}
\ensuremath{\rightarrow}
\coqdocvariable{\ensuremath{\alpha}} \ensuremath{\preceq}
\coqdocvariable{\ensuremath{\beta}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
We would like to strengthen this, but cannot, since nothing denies
\coqdocvariable{$\alpha$} from being the tree ordinal $\sqcup ( 0, 0, 0,
\ldots )$ (which has the same rank as $0$). We therefore turn to a subset of
the tree ordinals where we restrict limit sequences to be strictly
monotonic. This restriction is encoded in the
\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} (well-formedness)
property. The $\Sigma$-type
\coqref{WfOrdinal.wford}{\coqdocdefinition{ord$^\text{wf}$}} defines
the resulting subset.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint} \coqdef{WfOrdinal.wf}{wf}{\coqdocdefinition{wf}}
\coqdocvar{\ensuremath{\alpha}} : \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{\ensuremath{\alpha}} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Zero}{\coqdocconstructor{Zero}}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Logic}{True}{\coqdocinductive{True}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Succ}{\coqdocconstructor{Succ}}
\coqdocvar{\ensuremath{\beta}} \ensuremath{\Rightarrow}
\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} \coqdocvariable{\ensuremath{\beta}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Ordinal.Limit}{\coqdocconstructor{Limit}} \coqdocvar{f}
\ensuremath{\Rightarrow} \ensuremath{\forall} \coqdocvar{n},
\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} (\coqdocvariable{f}
\coqdocvariable{n}) \ensuremath{\land} \ensuremath{\forall} \coqdocvar{m},
\coqdocvariable{n} < \coqdocvariable{m} \ensuremath{\rightarrow}
\coqdocvariable{f} \coqdocvariable{n} \ensuremath{\prec}
\coqdocvariable{f} \coqdocvariable{m}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{WfOrdinal.wford}{wf\_ord}{\coqdocdefinition{ord$^\text{wf}$}} : \coqdockw{Set}
:=
%\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{sig}{\coqdocinductive{sig}}
%\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}}.\coqdoceol
\{ \coqdocvariable{$\alpha$} :
\coqref{Ordinal.ord}{\coqdocinductive{ord}} \ensuremath{|}
\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} \coqdocvariable{$\alpha$}
\}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Now we can prove the stronger result we were looking
for.\footnote{Again, defining a simple coercion from
  \coqref{WfOrdinal.wford}{\coqdocdefinition{ord$^\text{wf}$}} to
  \coqref{Ordinal.ord}{\coqdocinductive{ord}} (first $\Sigma$-type
  projection) lets us state this lemma concisely.}
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{WfOrdinal.wfordlezeroright}{wf\_ord\_le\_zero\_right}{\coqdoclemma{\ensuremath{\preceq^{\text{wf}}_{\text{zero\_right}}}}}
:
\ensuremath{\forall} \coqdocvar{\ensuremath{\alpha}} :
\coqref{WfOrdinal.wford}{\coqdocdefinition{ord$^\text{wf}$}},
\coqdocvariable{\ensuremath{\alpha}} \ensuremath{\preceq}
\coqref{Ordinal.Zero}{\coqdocconstructor{Zero}}
\ensuremath{\rightarrow}
\coqdocvariable{\ensuremath{\alpha}} =
\coqref{Ordinal.Zero}{\coqdocconstructor{Zero}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

\section{Coinductive Terms}\label{sec:terms}

We define the type
\coqref{Term.term}{\coqdocinductive{term}} of infinite terms with
function symbols in \coqdocvar{F} and variables in \coqdocvar{X}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{CoInductive} \coqdef{Term.term}{term}{\coqdocinductive{term}} :
\coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Term.Var}{Var}{\coqdocconstructor{Var}} : \coqdocvar{X}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Term.Fun}{Fun}{\coqdocconstructor{Fun}} :
\ensuremath{\forall} \coqdocvar{f} : \coqdocvar{F},
\coqref{Vector.vector}{\coqdocdefinition{vector}}
\coqref{Term.term}{\coqdocinductive{term}}
(\coqdocprojection{arity} \coqdocvariable{f})
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
The objects of a coinductive type can only be built in some restricted way to
ensure productivity of their construction. This restriction implies a technical
difficulty in the definition of the
\coqref{Vector.vector}{\coqdocdefinition{vector}} type, which is discussed in
Section~\ref{sec:guardedness}.
For now, we assume \coqref{Vector.vector}{\coqdocdefinition{vector}} to
implement dependently typed lists (the type depending on their length).

The standard equality defined in \Coq, equivalent to Leibniz' equality and
written $=$, does not suffice
for establishing that two terms are equal, given that the only way to build
infinite objects is by corecursion. Because the amount of memory available is
finite, we can only unfold the corecursive definition finitely many times, and
then still be left with a non-normal form. Simply comparing such
definitions will not do, since the corecursive construction of any given
infinite object is not unique. To this end, we define two extensional
equalities on \coqref{Term.term}{\coqdocinductive{term}}, following
Definitions~\ref{def:bisimilarity} and \ref{def:equiv}. The coinductive
relation \coqref{TermEquality.termbis}{$\bis$} defines bisimilarity and
pointwise equality is defined by \coqref{TermEquality.termeq}{$\equiv$}
inductively.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{CoInductive}
\coqdef{TermEquality.termbis}{term\_bis}{$\bis$} :
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{TermEquality.Varbis}{Var\_bis}{\coqdocconstructor{$\biss{\text{Var}}$}} :
\ensuremath{\forall} \coqdocvar{x},
\coqref{Term.Var}{\coqdocconstructor{Var}} \coqdocvariable{x}
\coqref{TermEquality.termbis}{$\bis$}
\coqref{Term.Var}{\coqdocconstructor{Var}} \coqdocvariable{x}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{TermEquality.Funbis}{Fun\_bis}{\coqdocconstructor{$\biss{\text{Fun}}$}} :
\ensuremath{\forall} \coqdocvar{f} \coqdocvar{v} \coqdocvar{w},
(\ensuremath{\forall} \coqdocvar{i},
\coqdocvariable{v} \coqdocvariable{i}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{w} \coqdocvariable{i})
\ensuremath{\rightarrow}
\coqref{Term.Fun}{\coqdocconstructor{Fun}} \coqdocvariable{f}
\coqdocvariable{v}
\coqref{TermEquality.termbis}{$\bis$}
\coqref{Term.Fun}{\coqdocconstructor{Fun}} \coqdocvariable{f}
\coqdocvariable{w}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Any proof of two infinite terms being bisimilar is an infinite proof, in the
sense that the proof term is built by corecursion.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive}
\coqdef{TermEquality.termequpto}{term\_eq\_up\_to}{\equpto{}}
:
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}}
\ensuremath{\rightarrow} \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{TermEquality.teut0}{teut\_0}{\coqdocconstructor{teut$_0$}}   :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t},
\coqdocvariable{s} \coqref{TermEquality.termequpto}{\equpto{0}}
\coqdocvariable{t}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{TermEquality.teutvar}{teut\_var}{\coqdocconstructor{teut$_\text{Var}$}} :
\ensuremath{\forall} \coqdocvar{d} \coqdocvar{x},
\coqref{Term.Var}{\coqdocconstructor{Var}} \coqdocvariable{x}
\coqref{TermEquality.termequpto}{\equpto{\coqdocvariable{d}}}
\coqref{Term.Var}{\coqdocconstructor{Var}} \coqdocvariable{x}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{TermEquality.teutfun}{teut\_fun}{\coqdocconstructor{teut$_\text{Fun}$}} :
\ensuremath{\forall} \coqdocvar{d} \coqdocvar{f} \coqdocvar{v}
\coqdocvar{w},
(\ensuremath{\forall} \coqdocvar{i},
\coqdocvariable{v} \coqdocvariable{i}
\coqref{TermEquality.termequpto}{\equpto{\coqdocvariable{d}}}
\coqdocvariable{w} \coqdocvariable{i}) \ensuremath{\rightarrow}
\coqref{Term.Fun}{\coqdocconstructor{Fun}}
\coqdocvariable{f} \coqdocvariable{v}
\coqref{TermEquality.termequpto}{\equpto{\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
    \, \coqdocvariable{d}}}
\coqref{Term.Fun}{\coqdocconstructor{Fun}} \coqdocvariable{f}
\coqdocvariable{w}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdocvar{s}
\coqdef{TermEquality.termeq}{term\_eq}{$\equiv$}
\coqdocvar{t} :=
\ensuremath{\forall} \coqdocvar{d},
\coqdocvariable{s}
\coqref{TermEquality.termequpto}{\equpto{\coqdocvariable{d}}}
\coqdocvariable{t}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

We can prove that \coqref{TermEquality.termbis}{$\bis$} and
\coqref{TermEquality.termeq}{$\equiv$} are the same
relation, and that indeed it is an equivalence.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{TermEquality.termbistermeq}{term\_bis\_term\_eq}{\coqdoclemma{term\_bis\_term\_eq}}
: \ensuremath{\forall} \coqdocvar{s} \coqdocvar{t},
\coqdocvariable{s}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{t} \ensuremath{\leftrightarrow}
\coqdocvariable{s}
\coqref{TermEquality.termeq}{$\equiv$}
\coqdocvariable{t}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{TermEquality.termbisrefl}{term\_bis\_refl}{\coqdoclemma{$\biss{\text{refl}}$}}
: \ensuremath{\forall} \coqdocvar{t},
\coqdocvariable{t}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{t}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{TermEquality.termbissymm}{term\_bis\_symm}{\coqdoclemma{$\biss{\text{symm}}$}}
: \ensuremath{\forall} \coqdocvar{s} \coqdocvar{t},
\coqdocvariable{s}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{t} $\rightarrow$
\coqdocvariable{t}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{s}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{TermEquality.termbistrans}{term\_bis\_trans}{\coqdoclemma{$\biss{\text{trans}}$}}
: \ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u},
\coqdocvariable{s}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{t} $\rightarrow$
\coqdocvariable{t}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{u} $\rightarrow$
\coqdocvariable{s}
\coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{u}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

In Section~\ref{sec:seq} we need some notion of convergence for functions of
type
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
$\rightarrow$
\coqref{Term.term}{\coqdocinductive{term}}. We implement
Definition~\ref{def:cauchy} in \Coq for sequences of length $\omega$.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.converges}{converges}{\coqdocdefinition{converges}}
(\coqdocvar{f} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}})
(\coqdocvar{t} : \coqref{Term.term}{\coqdocinductive{term}}) :
\coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{\forall} \coqdocvar{d}, \ensuremath{\exists} \coqdocvar{n},
\ensuremath{\forall} \coqdocvar{m},
\coqdocvariable{n} \ensuremath{\le} \coqdocvariable{m}
\ensuremath{\rightarrow}
\coqdocvariable{f} \coqdocvariable{m}
\coqref{TermEquality.termequpto}{\equpto{\coqdocvariable{d}}}
\coqdocvariable{t}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
The definitions of finite term, rewrite rule, TRS, and left-linearity
from Subsection~\ref{sub:trs} translate to \Coq directly. We define
\coqdef{Rewriting.lhs}{lhs}{\coqdocprojection{lhs}} and
\coqdef{Rewriting.rhs}{rhs}{\coqdocprojection{rhs}} to be first and
second projection on rewrite rules, respectively.

The type of contexts is inductively defined, where the hole always
occurs at a finite depth.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive}
\coqdef{Context.context}{context}{\coqdocinductive{context}} :
\coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} $\Box$ :
\coqref{Context.context}{\coqdocinductive{context}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Context.CFun}{CFun}{\coqdocconstructor{CFun}} :
\ensuremath{\forall} (\coqdocvar{f} : \coqdocvar{F}) (\coqdocvar{i}
\coqdocvar{j} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}),
\coqdocvariable{i} +
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{S}{\coqdocconstructor{S}}
\coqdocvariable{j} =
\coqdocprojection{arity} \coqdocvariable{f}
\ensuremath{\rightarrow}\coqdoceol
\coqdocindent{5.50em}
\coqref{Vector.vector}{\coqdocdefinition{vector}}
\coqref{Term.term}{\coqdocinductive{term}} \coqdocvariable{i}
\ensuremath{\rightarrow}
\coqref{Context.context}{\coqdocinductive{context}}
\ensuremath{\rightarrow}
\coqref{Vector.vector}{\coqdocdefinition{vector}}
\coqref{Term.term}{\coqdocinductive{term}} \coqdocvariable{j}
\ensuremath{\rightarrow}
\coqref{Context.context}{\coqdocinductive{context}}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Applying a substitution \coqdocvariable{$\sigma$} to a term
\coqdocvariable{t} is defined by corecursion over
\coqdocvariable{t}. We also use the
notation \begin{coqdoccode}\coqdocvariable{t}$^\coqdocvariable{$\sigma$}$\end{coqdoccode}
for \begin{coqdoccode}\coqref{Substitution.substitute}{\coqdocdefinition{substitute}}
  \coqdocvariable{$\sigma$} \coqdocvariable{t}\end{coqdoccode}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Substitution.substitution}{substitution}{\coqdocdefinition{substitution}}
:= \coqdocvar{X} \ensuremath{\rightarrow}
\coqref{Term.term}{\coqdocinductive{term}}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{CoFixpoint}
\coqdef{Substitution.substitute}{substitute}{\coqdocdefinition{substitute}}
(\coqdocvar{$\sigma$} :
\coqref{Substitution.substitution}{\coqdocdefinition{substitution}})
(\coqdocvar{t} :
\coqref{Term.term}{\coqdocinductive{term}}) :
\coqref{Term.term}{\coqdocinductive{term}} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{t} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Term.Var}{\coqdocconstructor{Var}}
\coqdocvar{x}      \ensuremath{\Rightarrow} \coqdocvariable{$\sigma$}
\coqdocvariable{x}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Term.Fun}{\coqdocconstructor{Fun}}
\coqdocvar{f} \coqdocvar{args} \ensuremath{\Rightarrow}
\coqref{Term.Fun}{\coqdocconstructor{Fun}} \coqdocvariable{f}
(\coqref{Vector.vmap}{\coqdocdefinition{vmap}}
(\coqref{Substitution.substitute}{\coqdocdefinition{substitute}}
\coqdocvariable{$\sigma$}) \coqdocvariable{args})\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
We apply the recursive function
\coqdef{Context.fill}{fill}{\coqdocdefinition{fill}} (not shown here)
to a context \coqdocvariable{C} and a term \coqdocvariable{t}
(written \begin{coqdoccode}\coqdocvariable{C}[\coqdocvariable{t}]\end{coqdoccode})
to replace the hole in \coqdocvariable{C} with \coqdocvariable{t}.

Positions are represented by simple lists of natural numbers. This
means the subterm at some position in some term may not actually
exist. For this reason we employ
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{option}{\coqdocinductive{option}}
types in functions that do a lookup by position (functions in \Coq are
always \emph{total}). For further discussion of positions, see
Section~\ref{sec:design}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint} \coqdef{Context.dig}{dig}{\coqdocdefinition{dig}}
(\coqdocvar{t} : \coqref{Term.term}{\coqdocinductive{term}})
(\coqdocvar{p} :
\coqdocabbreviation{position})
\{\coqdockw{struct} \coqdocvar{p}\} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{option}{\coqdocinductive{option}}
\coqref{Context.context}{\coqdocinductive{context}} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{p} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nil}{\coqdocconstructor{nil}}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
$\Box$\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdocvar{n} :: \coqdocvar{p} \ensuremath{\Rightarrow}
\coqdockw{match} \coqdocvariable{t} \coqdockw{with}\coqdoceol
\coqdocindent{3.00em}
\ensuremath{|} \coqref{Term.Var}{\coqdocconstructor{Var}}
\coqdocvar{\_}      \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}\coqdoceol
\coqdocindent{3.00em}
\ensuremath{|} \coqref{Term.Fun}{\coqdocconstructor{Fun}}
\coqdocvar{f} \coqdocvar{args} \ensuremath{\Rightarrow}
\coqdockw{match}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Arith.Bool\_nat}{ltgedec}{\coqdocdefinition{lt\_ge\_dec}}
\coqdocvariable{n} (\coqdocprojection{arity}
\coqdocvariable{f}) \coqdockw{with}\coqdoceol
\coqdocindent{5.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{left}{\coqdocconstructor{left}}
\coqdocvar{h}  \ensuremath{\Rightarrow} \coqdockw{match}
\coqref{Context.dig}{\coqdocdefinition{dig}}
(\coqdocdefinition{vnth} \coqdocvariable{h}
\coqdocvariable{args}) \coqdocvariable{p} \coqdockw{with}\coqdoceol
\coqdocindent{7.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}\coqdoceol
\coqdocindent{7.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
\coqdocvar{C} \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
(\coqref{Context.CFun}{\coqdocconstructor{CFun}} \coqdocvariable{f}
(\coqdoclemma{lt\_plus\_minus\_r}
\coqdocvariable{h})\coqdoceol
\coqdocindent{14.00em}
(\coqdocdefinition{vtake}
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Arith.Lt}{ltleweak}{\coqdoclemma{lt\_le\_weak}}
\coqdocvariable{n} (\coqdocprojection{arity}
\coqdocvariable{f}) \coqdocvariable{h})
\coqdocvariable{args})\coqdoceol
\coqdocindent{14.00em}
\coqdocvariable{C}\coqdoceol
\coqdocindent{14.00em}
(\coqdocdefinition{vdrop} \coqdocvariable{h}
\coqdocvariable{args}))\coqdoceol
\coqdocindent{7.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{5.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{right}{\coqdocconstructor{right}}
\coqdocvar{\_} \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}\coqdoceol
\coqdocindent{5.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{3.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\coqdocemptyline
\end{coqdoccode}
\end{singlespace}
%\pagebreak[4]
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{Term.subterm}{subterm}{\coqdocdefinition{subterm}}
(\coqdocvar{t} : \coqref{Term.term}{\coqdocinductive{term}})
(\coqdocvar{p} :
\coqdocabbreviation{position})
\{\coqdockw{struct} \coqdocvar{p}\} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{option}{\coqdocinductive{option}}
\coqref{Term.term}{\coqdocinductive{term}} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{p} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nil}{\coqdocconstructor{nil}}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
\coqdocvariable{t}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdocvar{n} :: \coqdocvar{p} \ensuremath{\Rightarrow}
\coqdockw{match} \coqdocvariable{t} \coqdockw{with}\coqdoceol
\coqdocindent{7.00em}
\ensuremath{|} \coqref{Term.Var}{\coqdocconstructor{Var}}
\coqdocvar{\_}      \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}\coqdoceol
\coqdocindent{7.00em}
\ensuremath{|} \coqref{Term.Fun}{\coqdocconstructor{Fun}}
\coqdocvar{f} \coqdocvar{args} \ensuremath{\Rightarrow}
\coqdockw{match}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Arith.Bool\_nat}{ltgedec}{\coqdocdefinition{lt\_ge\_dec}}
\coqdocvariable{n} (\coqdocprojection{arity}
\coqdocvariable{f}) \coqdockw{with}\coqdoceol
\coqdocindent{15.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{left}{\coqdocconstructor{left}}
\coqdocvar{h}  \ensuremath{\Rightarrow}
\coqref{Term.subterm}{\coqdocdefinition{subterm}}
(\coqdocdefinition{vnth} \coqdocvariable{h}
\coqdocvariable{args}) \coqdocvariable{p}\coqdoceol
\coqdocindent{15.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{right}{\coqdocconstructor{right}}
\coqdocvar{\_} \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{None}{\coqdocconstructor{None}}\coqdoceol
\coqdocindent{15.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{7.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Now \begin{coqdoccode}\coqref{Term.subterm}{\coqdocdefinition{subterm}}
  \coqdocvariable{t} \coqdocvariable{p}\end{coqdoccode} gives the
subterm of \coqdocvariable{t} (if it exists)
and \begin{coqdoccode}\coqref{Context.dig}{\coqdocdefinition{dig}}
  \coqdocvariable{t} \coqdocvariable{p}\end{coqdoccode} gives the
context \coqdocvariable{C} (if it exists) that is \coqdocvariable{t}
with \begin{coqdoccode}\coqref{Term.subterm}{\coqdocdefinition{subterm}}
  \coqdocvariable{t} \coqdocvariable{p}\end{coqdoccode} replaced by
$\Box$ at position \coqdocvariable{p}.

\section{Transfinite Rewrite Sequences}\label{sec:seq}

In this section we present the essence of our development. Rewrite
sequences of length $\alpha$ are represented using the tree structure
of the tree ordinal $\alpha$. Much of the definitions on ordinals
are lifted to rewrite sequences and we once more come to a notion of
well-formedness. The resulting representation is discussed in relation
to the traditional theory of rewriting in
Section~\ref{sec:convergence}.

Throughout this section, we let $\mathcal{R}$ be a fixed TRS. We
define the type of steps using rewrite rules in $\mathcal{R}$,
parameterised by their source and target terms. Some flexibility in the form
of bisimilarity is allowed, motivated in Subsection~\ref{sub:bissteps}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive} \coqdef{Rewriting.step}{step}{$\rightarrow_\mathcal{R}$} :
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Rewriting.Step}{Step}{\coqdocconstructor{Step}} :
\ensuremath{\forall} (\coqdocvar{s} \coqdocvar{t} :
\coqref{Term.term}{\coqdocinductive{term}}) (\coqdocvar{$\rho$} :
\coqdocrecord{rule}) (\coqdocvar{C} :
\coqref{Context.context}{\coqdocinductive{context}}) (\coqdocvar{$\sigma$} :
\coqref{Substitution.substitution}{\coqdocdefinition{substitution}}),\coqdoceol
\coqdocindent{6.50em} \coqdocvariable{$\rho$}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Lists.List}{In}{\coqdocdefinition{$\in$}}
\coqdocvar{$\mathcal{R}$} \ensuremath{\rightarrow}\coqdoceol
\coqdocindent{6.50em}
\coqdocvariable{C}[(\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocvariable{$\rho$})\coqdocvariable{$^\sigma$}] \coqref{TermEquality.termbis}{$\bis$} \coqdocvariable{s}
\ensuremath{\rightarrow}\coqdoceol
\coqdocindent{6.50em}
\coqdocvariable{C}[(\coqref{Rewriting.rhs}{\coqdocprojection{rhs}}
\coqdocvariable{$\rho$})\coqdocvariable{$^\sigma$}] \coqref{TermEquality.termbis}{$\bis$} \coqdocvariable{t}
\ensuremath{\rightarrow}
(\coqdocvariable{s} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvariable{t}).\coqdoceol
\end{coqdoccode}
\end{singlespace}
For the translation of Definition~\ref{def:stepeq} (equality of steps)
to \Coq, we assume the lifting of bisimilarity to contexts and that
\coqdef{Substitution.substitutioneq}{substitution\_eq}{\coqdocdefinition{substitution\_eq}}
defines agreement of substitutions on a given list of variables.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.stepeq}{step\_eq}{$\approx$}
(\coqdocvar{s} \coqdocvar{t} :
\coqref{Term.term}{\coqdocinductive{term}}) (\coqdocvar{$\pi$} :
\coqdocvar{s} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$} \coqdocvar{t}) (\coqdocvar{u} \coqdocvar{v} :
\coqref{Term.term}{\coqdocinductive{term}}) (\coqdocvar{$o$} :
\coqdocvar{u} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvar{v}) : \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\pi$}, \coqdocvariable{$o$}
\coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Step}{\coqdocconstructor{Step}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{$\rho$} \coqdocvar{C}
\coqdocvar{$\sigma$} \coqdocvar{\_} \coqdocvar{\_} \coqdocvar{\_},
\coqref{Rewriting.Step}{\coqdocconstructor{Step}} \coqdocvar{\_}
\coqdocvar{\_} \coqdocvar{$\rho'$} \coqdocvar{C$'$} \coqdocvar{$\sigma'$}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{\_}
\ensuremath{\Rightarrow}\coqdoceol
\coqdocindent{2.00em}
\coqdocvariable{C} $\bis$ \coqdocvariable{C$'$} \ensuremath{\land}
\coqdocvariable{$\rho$} = \coqdocvariable{$\rho'$} \ensuremath{\land}
\coqref{Substitution.substitutioneq}{\coqdocdefinition{substitution\_eq}}
(\coqdocdefinition{vars}
(\coqref{Rewriting.lhs}{\coqdocprojection{lhs}} \coqdocvariable{$\rho$}))
\coqdocvariable{$\sigma$} \coqdocvariable{$\sigma'$}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

We describe a way to define rewrite sequences as an inductive type. A rewrite
sequence of length $\alpha$ can be represented by the tree ordinal $\alpha$
where we label every occurrence of the $^+$ constructor with a rewrite
step. To ensure that successive steps have the same target and source terms,
respectively, we include the source and target terms of the rewrite sequence
in its type and label accordingly.

At this point, it is not immediately clear what the type of the limit
constructor should be. Following the tree ordinals, we think of a rewrite
sequence as a countably branching tree with every branching node representing
the least upper bound of its branches. As a first step towards a type of
rewrite sequences, we write down an incomplete try. Note that the
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}} constructor appends
(not prepends) a step to a sequence.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive}
\coqdef{Rewriting.sequence}{sequence}{$\rewrites_\mathcal{R}$} :
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\rightarrow}
\coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqdef{Rewriting.Nil}{Nil}{\coqdocconstructor{Nil}} :
\ensuremath{\forall} \coqdocvar{t}, \coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t}\coqdoceol \coqdocindent{1.00em}
\ensuremath{|} \coqdef{Rewriting.Cons}{Cons}{\coqdocconstructor{Cons}}:
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u}, (\coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{t})
$\rightarrow$
(\coqdocvariable{t} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvar{u}) $\rightarrow$ (\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u})\coqdoceol \coqdocindent{1.00em}
\ensuremath{|} \coqdocconstructor{Lim}   :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t},
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdef{Rewriting.LimPlaceholder}{LimPlaceholder}{\textbf{?}}) $\rightarrow$
(\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t}).\coqdoceol
\end{coqdoccode}
\end{singlespace}
This is not yet satisfying, because we cannot fix a value for
\coqref{Rewriting.LimPlaceholder}{\textbf{?}}. We complete the type for
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} as follows. First, we
parameterise it with the target terms of the branches. Second, we add the
condition that these terms must converge to the target term
\coqdocvariable{t}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocindent{1.00em}\label{coq:lim}
\ensuremath{|} \coqdef{Rewriting.Lim}{Lim}{\coqdocconstructor{Lim}} :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t}
(\coqdocvar{ts} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}}),\coqdoceol
\coqdocindent{5.0em}
(\ensuremath{\forall} \coqdocvar{n}, \coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvar{ts} \coqdocvariable{n}) $\rightarrow$
\coqref{Rewriting.converges}{\coqdocdefinition{converges}} \coqdocvariable{ts}
\coqdocvariable{t} $\rightarrow$
(\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t})\coqdoceol
\end{coqdoccode}
\end{singlespace}

Of course, the branches of a \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}
constructor may still not actually approximate to a rewrite sequence (of
length a limit ordinal). The intuition is that each branch should extend on
its preceding ones. This would correspond to the
\coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} property we defined on
\coqref{Ordinal.ord}{\coqdocinductive{ord}}, where we lift $\prec$ to a
strict prefix relation on
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}.
We return to this issue in Subsection~\ref{sub:wf}, but
first consider the definition of an embedding relation on
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}.

\subsection{Embeddings of Rewrite Sequences}\label{sub:embedding}

We lift the notions of predecessor and predecessor indices to the domain of
transfinite rewrite sequences. The set of predecessor indices is
easily defined as
\coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}}.

%\footnote{We employ some notational overloading by reusing $I(\_)$ and
%  $[\_]\_$ for the corresponding definitions on rewrite sequences.}
%\pagebreak[4]
\pagebreak[3]

\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{Rewriting.predtype}{pred\_type}{\coqdocdefinition{pred\_type}}
\coqdocvar{s} \coqdocvar{t}
(\coqdocvar{$\varphi$} : \coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{t}) :
\coqdockw{Type} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\varphi$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Nil}{\coqdocconstructor{Nil}} \coqdocvar{\_}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Logic}{False}{\coqdocinductive{empty}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{\_} \coqdocvar{$\psi$} \coqdocvar{\_}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{unit}{\coqdocinductive{unit}}
+ \coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}}
\coqdocvariable{$\psi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} \coqdocvar{\_}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{f} \coqdocvar{\_}
\ensuremath{\Rightarrow} \{ \coqdocvar{n} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\& \coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}}
(\coqdocvariable{f} \coqdocvariable{n}) \}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

The predecessor indices defined by
\coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}} point to a specific
occurrence of the \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
constructor in a rewrite sequence. This constructor does not only contain a
rewrite sequence (analogous to an ordinal in the
\coqref{Ordinal.ord}{\coqdocinductive{ord}} case), but also a rewrite
step. The \coqref{Rewriting.pred}{\coqdocdefinition{pred}} function gives us
both the rewrite sequence and the step pointed to by a predecessor index. For
the type checker to accept the
definition, we use a $\Sigma$-type that contains this pair, parameterised by
the source and target terms of the rewrite step.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint} \coqdef{Rewriting.pred}{pred}{\coqdocdefinition{pred}}
\coqdocvar{s} \coqdocvar{t} (\coqdocvar{$\varphi$} : \coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{t})
(\coqdocvar{$\iota$} : \coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}}
\coqdocvariable{$\varphi$})
:\coqdoceol \coqdocindent{2.00em}
\{ \coqdocvar{ts} :
\coqref{Term.term}{\coqdocinductive{term}} \ensuremath{\times}
\coqref{Term.term}{\coqdocinductive{term}} \&
(\coqdocvariable{s} \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{fst}{\coqdocdefinition{fst}}
\coqdocvariable{ts}) \ensuremath{\times}
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{fst}{\coqdocdefinition{fst}}
\coqdocvariable{ts} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{snd}{\coqdocdefinition{snd}}
\coqdocvariable{ts}) \} :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\varphi$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Nil}{\coqdocconstructor{Nil}} \coqdocvar{\_}
\ensuremath{\Rightarrow}
(\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Logic}{Falserect}{\coqdocdefinition{empty\_rect}}
\coqdocvar{\_}) \coqdocvariable{$\iota$} \coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}} \coqdocvar{\_}
\coqdocvar{u} \coqdocvar{t} \coqdocvar{$\psi$} \coqdocvar{$\pi$}
\ensuremath{\Rightarrow}
\coqdockw{match} \coqdocvariable{$\iota$} \coqdockw{with}\coqdoceol
\coqdocindent{10.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{inl}{\coqdocconstructor{inl}}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{tt}{\coqdocconstructor{tt}}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{existT}{\coqdocconstructor{existT}}
\coqdocvar{\_} (\coqdocvariable{u}, \coqdocvariable{t})
(\coqdocvariable{$\psi$}, \coqdocvariable{$\pi$})\coqdoceol
\coqdocindent{10.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{inr}{\coqdocconstructor{inr}}
\coqdocvar{$\kappa$}  \ensuremath{\Rightarrow}
\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvariable{$\psi$}
\coqdocvariable{$\kappa$}\coqdoceol
\coqdocindent{10.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} \coqdocvar{\_}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{f} \coqdocvar{\_}
\ensuremath{\Rightarrow}
\coqdockw{match} \coqdocvariable{$\iota$} \coqdockw{with}\coqdoceol
\coqdocindent{10.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{existT}{\coqdocconstructor{existT}}
\coqdocvar{n} \coqdocvar{$\kappa$} \ensuremath{\Rightarrow}
\coqref{Rewriting.pred}{\coqdocdefinition{pred}} (\coqdocvariable{f}
\coqdocvariable{n}) \coqdocvariable{$\kappa$}\coqdoceol
\coqdocindent{10.00em}
\coqdockw{end}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

In an effort to prevent getting lost in a syntactical labyrinth, we define
the following notational shortcuts:
\begin{center}\begin{singlespace}
{\renewcommand{\arraystretch}{1.4}
\renewcommand{\tabcolsep}{8pt}
\begin{tabular}{lll}
\textsc{short} & \textsc{explanation} & \textsc{definition}\\
\hline
\coqdocvar{$\varphi$}[\coqdocvar{$\iota$}] & location in
\coqdocvar{$\varphi$} indexed by \coqdocvar{$\iota$} &
  \begin{coqdoccode}\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvar{$\varphi$} \coqdocvar{$\iota$}\end{coqdoccode} \\
\coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{seq}$ & rewrite sequence in
  \coqdocvar{$\varphi$} indexed by \coqdocvar{$\iota$}
  & \begin{coqdoccode}\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{fst}{\coqdocdefinition{fst}}
      (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{projT2}{\coqdocdefinition{projT2}}
      (\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvar{$\varphi$} \coqdocvar{$\iota$}))\end{coqdoccode}
  \\
\coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{stp}$ & step of \coqdocvar{$\varphi$} indexed by \coqdocvar{$\iota$} &
  \begin{coqdoccode}\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{snd}{\coqdocdefinition{snd}}
    (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{projT2}{\coqdocdefinition{projT2}}
    (\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvar{$\varphi$} \coqdocvar{$\iota$}))\end{coqdoccode}
  \\
\coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{l}$ & source term of \coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{stp}$
  & \begin{coqdoccode}\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{fst}{\coqdocdefinition{fst}}
      (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{projT1}{\coqdocdefinition{projT1}}
      (\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvar{$\varphi$} \coqdocvar{$\iota$}))\end{coqdoccode}
  \\
\coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{r}$ & target term of
  \coqdocvar{$\varphi$}[\coqdocvar{$\iota$}]$^\textsc{stp}$ & \begin{coqdoccode}\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{snd}{\coqdocdefinition{snd}}
    (\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Specif}{projT1}{\coqdocdefinition{projT1}}
    (\coqref{Rewriting.pred}{\coqdocdefinition{pred}} \coqdocvar{$\varphi$} \coqdocvar{$\iota$}))\end{coqdoccode}
\end{tabular}}
\end{singlespace}\end{center}

%\pagebreak[4]

As an example of predecessor indexing, consider the graphical
representation of a rewrite sequence $\varphi$ of length $\omega + 2$
and its predecessor index $\iota = \coqdocconstructor{inr} \;
(\coqdocconstructor{inr} \; \langle 4,
\coqdocconstructor{inl}\rangle)$ in Figure~\ref{fig:pred}. The initial
part of length $\omega$ is represented by a series of finite rewrite
sequences, each one extending on the previous one by one step. The
sequence of terms $\{ t_1,t_2, t_3, \ldots \}$ converges to the term
$t_\omega$. Here, $\varphi[\iota]^\textsc{seq}$ is a rewrite sequence
from $t_1$ to $t_3$ and $\varphi[\iota]^\textsc{stp}$ is a step from
$t_3$ to $t_4$.

\begin{figure}
\begin{center}
\begin{tikzpicture}[scale=0.85]
\input{figures/predecessor.tikz}
\end{tikzpicture}
\end{center}
\caption{Example of a rewrite sequence and predecessor
  index.}\label{fig:pred}
\end{figure}

Having a closer look at the order $\preceq$ on the tree ordinals, we can
see that it really defines embeddings of their tree structures. This is due to
clause {\sc \ref{def:order:succ}} of Definition~\ref{def:order}. In
this clause, two occurrences of the $^+$ constructor (one in both
ordinals) are cancelled out against each other, but the positions of
these occurrences in their respective ordinals do not necessarily
correspond. Since occurrences of $^+$ carry no additional information,
this has no effect on the resulting relation.

What this means for a translation of $\preceq$ to the domain of our
inductively defined rewrite sequences is that, indeed, we get an embedding
relation. We only have to make sure that in the
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}} case, we cancel out two
equal steps against each other.
We say that $\varphi$ is embedded in $\psi$ (written $\varphi
\sqsubseteq \psi$) if $\psi$ can be obtained from $\varphi$ by inserting
any number of steps in $\varphi$. We distinguish between inserting a step
\begin{inparaenum}[(i)]
  \item before the first step,
  \item after the last step, and
  \item in between steps
\end{inparaenum}
in a rewrite sequence. Note that any steps inserted consecutively in between
steps necessarily form a cycle, because of the typing constraints in
the definition of rewrite sequence.
% is this good or bad? (cycle)

\pagebreak[4]

\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Inductive} \coqdef{Rewriting.embed}{embed}{$\sqsubseteq$}
: \ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u}
\coqdocvar{v}, (\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvariable{t})
$\rightarrow$ (\coqdocvariable{u}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvariable{v})
$\rightarrow$ \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{Rewriting.EmbedNil}{Embed\_Nil}{\coqdocconstructor{$\sqsubseteq_\text{Nil}$}}  :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{u} \coqdocvar{v} (\coqdocvar{$\psi$}
: \coqdocvariable{u} \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{v}),\coqdoceol
\coqdocindent{9.50em}
\coqref{Rewriting.Nil}{\coqdocconstructor{Nil}} \coqdocvariable{s}
\coqref{Rewriting.embed}{$\sqsubseteq$} \coqdocvariable{$\psi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{Rewriting.EmbedCons}{Embed\_Cons}{\coqdocconstructor{$\sqsubseteq_\text{Cons}$}} :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u} \coqdocvar{v}
(\coqdocvar{$\psi$}
: \coqdocvariable{u} \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{v}) (\coqdocvar{$\iota$} :
\coqref{Rewriting.predtype}{\coqdocdefinition{pred\_type}}
\coqdocvariable{$\psi$})
(\coqdocvar{$\varphi$} : \coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{$\psi$}[\coqdocvariable{$\iota$}]$^\textsc{l}$)\coqdoceol
\coqdocindent{5.00em}
(\coqdocvar{$\pi$} :
\coqdocvariable{$\psi$}[\coqdocvariable{$\iota$}]$^\textsc{l}$
\coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvariable{t}),\coqdoceol
\coqdocindent{9.50em}
\coqdocvariable{$\varphi$} \coqref{Rewriting.embed}{$\sqsubseteq$}
\coqdocvariable{$\psi$}[\coqdocvariable{$\iota$}]$^\textsc{seq}$
\ensuremath{\rightarrow}\coqdoceol
\coqdocindent{9.50em}
\coqdocvariable{$\pi$} \coqref{Rewriting.stepeq}{$\approx$}
\coqdocvariable{$\psi$}[\coqdocvariable{$\iota$}]$^\textsc{stp}$
\ensuremath{\rightarrow}\coqdoceol
\coqdocindent{9.50em}
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
\coqdocvariable{$\varphi$} \coqdocvariable{$\pi$}
\coqref{Rewriting.embed}{$\sqsubseteq$}
\coqdocvariable{$\psi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|}
\coqdef{Rewriting.EmbedLim}{Embed\_Lim}{\coqdocconstructor{$\sqsubseteq_\text{Lim}$}}  :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u} \coqdocvar{v}
(\coqdocvar{ts} :
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nat}{\coqdocinductive{nat}}
\ensuremath{\rightarrow} \coqref{Term.term}{\coqdocinductive{term}})
(\coqdocvar{f} : \ensuremath{\forall} \coqdocvar{n},
\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{ts} \coqdocvariable{n})\coqdoceol
\coqdocindent{5.00em}
(\coqdocvar{c} :
\coqref{Rewriting.converges}{\coqdocdefinition{converges}} \coqdocvariable{ts}
\coqdocvar{t}) (\coqdocvar{$\psi$} : \coqdocvar{u}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvar{v}),\coqdoceol
\coqdocindent{9.50em}
(\ensuremath{\forall} \coqdocvar{n}, \coqdocvariable{f} \coqdocvariable{n}
\coqref{Rewriting.embed}{$\sqsubseteq$} \coqdocvariable{$\psi$})
\ensuremath{\rightarrow}\coqdoceol
\coqdocindent{9.50em}
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} \coqdocvariable{f}
\coqdocvariable{c} \coqref{Rewriting.embed}{$\sqsubseteq$}
\coqdocvariable{$\psi$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

Analogous to the strict order $\prec$ on ordinals, we define a strict
embedding relation $\sqsubset$ on rewrite sequences.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.embedstrict}{embed\_strict}{$\sqsubset$}
\coqdocvar{s} \coqdocvar{t} \coqdocvar{u} \coqdocvar{v}
(\coqdocvar{$\varphi$} : \coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t},
\coqdocvar{$\psi$} :
\coqdocvariable{u}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{v}) := \ensuremath{\exists} \coqdocvar{$\iota$},
\coqdocvariable{$\varphi$} \coqref{Rewriting.embed}{$\sqsubseteq$}
\coqdocvariable{$\psi$}[\coqdocvariable{$\iota$}]$^\textsc{seq}$.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Note that, while non-strictly embedded rewrite sequences may differ in
any of the three ways defined above, strictly embedded rewrite
sequences always differ in their last step. Thus, if $\varphi$ is
strictly embedded in $\psi$ then $\psi$ can be obtained from $\varphi$
by inserting any number of steps in $\varphi$, but at least one after
the last step.

\subsection{Well-formed Rewrite Sequences}\label{sub:wf}

The \coqref{WfOrdinal.wf}{\coqdocdefinition{wf}} property on
\coqref{Ordinal.ord}{\coqdocinductive{ord}} is defined in
Section~\ref{sec:ordimp} to rule out a
certain class of ordinal representations. This issue translates
directly to our inductive representation of rewrite sequences. We
define a well-formedness property
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} on rewrite sequences,
using the strict embedding relation $\sqsubset$.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint} \coqdef{Rewriting.wf}{wf}{\coqdocdefinition{wf}}
\coqdocvar{s} \coqdocvar{t}
(\coqdocvar{$\varphi$} : \coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t}) : \coqdockw{Prop}
:=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\varphi$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Nil}{\coqdocconstructor{Nil}}
\coqdocvar{\_}          \ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Logic}{True}{\coqdocinductive{True}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{$\psi$} \coqdocvar{\_}
\coqdocvar{\_} \ensuremath{\Rightarrow}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}}
\coqdocvariable{\coqdocvariable{$\psi$}}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{f} \coqdocvar{\_}
\coqdocvar{\_}  \ensuremath{\Rightarrow}
(\ensuremath{\forall} \coqdocvar{n},
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} (\coqdocvariable{f}
\coqdocvariable{n})) \ensuremath{\land}
\ensuremath{\forall} \coqdocvar{n} \coqdocvar{m}, \coqdocvariable{n}
< \coqdocvariable{m} \ensuremath{\rightarrow} \coqdocvariable{f}
\coqdocvariable{n}
\coqref{Rewriting.embedstrict}{$\sqsubset$}
\coqdocvariable{f} \coqdocvariable{m}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

On page~\pageref{coq:lim}, we define the
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} constructor with the
intuition that each of its branches should extend on the preceding
ones. Naturally, we would implement this condition using a strict prefix
relation on rewrite sequences, but the strict embedding relation
$\sqsubset$ is also satisfying for this purpose.

Consider an instance of
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}, satisfying
\coqref{Rewriting.wf}{\coqdocdefinition{wf}}, with branches
\coqdocvar{f}. For every \begin{coqdoccode}\coqdocvariable{n} <
  \coqdocvariable{m}\end{coqdoccode}, we
have \begin{coqdoccode}\coqdocvariable{f} \coqdocvariable{n}
  \coqref{Rewriting.embedstrict}{$\sqsubset$} \coqdocvariable{f}
  \coqdocvariable{m}\end{coqdoccode}. Thus there is a predecessor
sequence % \coqdocvariable{$\varphi$}
of \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{m}\end{coqdoccode} that can be obtained
from \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode} by inserting any number of steps
in \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode}. Steps inserted before the last
step of \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode} must form a cycle (note that all
branches start at the same
term). Therefore, \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{m}\end{coqdoccode} can be obtained
from \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode} by adding one or more steps at
the end of \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode} and possibly inserting cycles at
other positions of \begin{coqdoccode}\coqdocvariable{f}
  \coqdocvariable{n}\end{coqdoccode}. This shows that, ignoring
cycles, $\sqsubset$ actually defines a strict prefix relation on the
branches of \coqdocvar{f}.

%Vincent said this about it:
%\begin{quote}
%Overigens bedachten dat het in principe niet heel erg is $\sqsubseteq$ te
%definieren voor reducties zoals voor ordinalen. alleen zie je dan het meer een
%notie van embedding/deelreductie ipv een notie van prefix geeft, maar ook daar
%kun je denkelijk goed mee werken.
%
%\ldots
%
%als je een stuk invoegt in het midden in sigma moet dat noodzakelijkerwijs,
%vanwege de constraints op begin-en eindpunten, een reductie cykel zijn. ik zou
%verwachten dat dat uiteindelijk een goede notie van ordening (goede reducties)
%oplevert (``de cykels doen er niet toe voor convergente rijen, en kun je
%weglaten bij compressie''), die natuurlijk niet overeenkomt, ook niet in het
%eindige geval met de prefix notie; het is deelwoord, of (op reductie rijtjes
%als bomen) deelboom (en niet boom-factor).
%\end{quote}

There is still an important omission in our formalisation
though: even rewrite sequences satisfying
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} are not necessarily
convergent. How the convergence conditions from
Definition~\ref{def:convergence} relate to our formalisation is
discussed in Section~\ref{sec:convergence}.

\subsection{Combining Rewrite Sequences}\label{sub:combining}

With the \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
constructor, we can extend a rewrite sequence with one step at the
end. Dually, \coqref{Rewriting.snoc}{\coqdocdefinition{snoc}} extends
a rewrite sequence with one step at the start. It is the analogue of
$1 \coqref{Ordinal.add}{+} \alpha$ on ordinals.

\coqref{Rewriting.snoc}{\coqdocdefinition{snoc}} is recursive in its
right argument, but for the \Coq type checker to accept our
definition, we must write it such that it consumes this argument
first.\footnote{The reason for this is rather technical, but the idea
  is that the return type nicely follows the case analysis on the
  rewrite sequence in the \coqdockw{match} construction. We also give
  some hints to the \Coq type checker that are not shown here.}
Hence, we use an auxiliary function
\coqref{Rewriting.snocrec}{\coqdocdefinition{snoc\_rec}}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{Rewriting.snocrec}{snoc\_rec}{\coqdocdefinition{snoc\_rec}}
\coqdocvar{s} \coqdocvar{t} \coqdocvar{u} (\coqdocvar{$\varphi$} :
\coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u}) : (\coqdocvariable{s}
\coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvariable{t}) \ensuremath{\rightarrow} (\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u}) :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\varphi$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Nil}{\coqdocconstructor{Nil}}
\coqdocvar{\_}          \ensuremath{\Rightarrow} \coqdockw{fun}
\coqdocvar{$\pi$} \ensuremath{\Rightarrow}
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
(\coqref{Rewriting.Nil}{\coqdocconstructor{Nil}} \coqdocvariable{s})
\coqdocvariable{$\pi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{$\psi$} \coqdocvar{\_}
\coqdocvar{$o$} \ensuremath{\Rightarrow} \coqdockw{fun} \coqdocvar{$\pi$}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
(\coqref{Rewriting.snocrec}{\coqdocdefinition{snoc\_rec}}
\coqdocvariable{$\psi$} \coqdocvariable{$\pi$}) \coqdocvariable{$o$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{f} \coqdocvar{u}
\coqdocvar{c}  \ensuremath{\Rightarrow} \coqdockw{fun} \coqdocvar{$\pi$}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} (\coqdockw{fun}
\coqdocvar{$o$} \ensuremath{\Rightarrow}
\coqref{Rewriting.snocrec}{\coqdocdefinition{snoc\_rec}}
(\coqdocvariable{f} \coqdocvariable{$o$}) \coqdocvariable{$\pi$})
\coqdocvariable{c}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.snoc}{snoc}{\coqdocdefinition{snoc}} \coqdocvar{s}
\coqdocvar{t} \coqdocvar{u} (\coqdocvar{$\pi$}
: \coqdocvar{s} \coqref{Rewriting.step}{$\rightarrow_\mathcal{R}$}
\coqdocvar{t}) (\coqdocvar{$\varphi$} : \coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{u}) :
\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u} :=
\coqref{Rewriting.snocrec}{\coqdocdefinition{snoc\_rec}}
\coqdocvariable{$\varphi$} \coqdocvariable{$\pi$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
A related operation is concatenation of rewrite sequences, the
analogue of addition on ordinals. It is defined in the same way as
\coqref{Rewriting.snoc}{\coqdocdefinition{snoc}}.
\pagebreak[4]
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Fixpoint}
\coqdef{Rewriting.appendrec}{append\_rec}{\coqdocdefinition{concat\_rec}}
\coqdocvar{s} \coqdocvar{t} \coqdocvar{u} (\coqdocvar{$\psi$} :
\coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u}) : (\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{t}) \ensuremath{\rightarrow} (\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u}) :=\coqdoceol
\coqdocindent{1.00em}
\coqdockw{match} \coqdocvariable{$\psi$} \coqdockw{with}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Nil}{\coqdocconstructor{Nil}}
\coqdocvar{\_}         \ensuremath{\Rightarrow} \coqdockw{fun}
\coqdocvar{$\varphi$} \ensuremath{\Rightarrow} \coqdocvariable{$\varphi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{$\psi$} \coqdocvar{\_}
\coqdocvar{$\pi$} \ensuremath{\Rightarrow} \coqdockw{fun} \coqdocvar{$\varphi$}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Cons}{\coqdocconstructor{Cons}}
(\coqref{Rewriting.appendrec}{\coqdocdefinition{concat\_rec}}
\coqdocvariable{$\psi$} \coqdocvariable{$\varphi$}) \coqdocvariable{$\pi$}\coqdoceol
\coqdocindent{1.00em}
\ensuremath{|} \coqref{Rewriting.Lim}{\coqdocconstructor{Lim}}
\coqdocvar{\_} \coqdocvar{\_} \coqdocvar{f} \coqdocvar{u}
\coqdocvar{c}  \ensuremath{\Rightarrow} \coqdockw{fun} \coqdocvar{$\varphi$}
\ensuremath{\Rightarrow}
\coqref{Rewriting.Lim}{\coqdocconstructor{Lim}} (\coqdockw{fun}
\coqdocvar{$o$} \ensuremath{\Rightarrow}
\coqref{Rewriting.appendrec}{\coqdocdefinition{concat\_rec}}
(\coqdocvariable{f} \coqdocvariable{$o$}) \coqdocvariable{$\varphi$})
\coqdocvariable{c}\coqdoceol
\coqdocindent{1.00em}
\coqdockw{end}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.append}{append}{\coqdocdefinition{concat}}
\coqdocvar{s} \coqdocvar{t} \coqdocvar{u} (\coqdocvar{$\varphi$} :
\coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{t})
(\coqdocvar{$\psi$} : \coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvar{u}) :
\coqdocvariable{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvariable{u} :=\coqdoceol
\coqdocindent{1.0em}
\coqref{Rewriting.appendrec}{\coqdocdefinition{concat\_rec}}
\coqdocvariable{$\psi$} \coqdocvariable{$\varphi$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

Well-formedness is preserved under concatenation.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Lemma}
\coqdef{Rewriting.appendwf}{append\_wf}{\coqdoclemma{concat\_wf}} :
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u}
(\coqdocvar{$\varphi$} : \coqdocvar{s}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvar{t}) (\coqdocvar{$\psi$} : \coqdocvariable{t}
\coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$}
\coqdocvar{u}),\coqdoceol
\coqdocindent{9.00em}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} \coqdocvariable{$\varphi$}
\ensuremath{\rightarrow}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} \coqdocvariable{$\psi$}
\ensuremath{\rightarrow}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}}
(\coqref{Rewriting.append}{\coqdocdefinition{concat}}
\coqdocvariable{$\varphi$} \coqdocvariable{$\psi$}).\coqdoceol
\end{coqdoccode}
\end{singlespace}

\section{Properties of Terms and TRSs}

We define some predicates on terms and TRSs. Again, we let
$\mathcal{R}$ be a fixed TRS throughout this section.

We work with a somewhat relaxed definition of critical pairs. First,
we do not require the common instance to be a most general
one. Second, the substitution $\sigma$ might not be minimal and might
not introduce only fresh variables
(cf.\ Definition~\ref{def:overlap}). The effect of this relaxation is
that for every critical pair, we have a series of critical pairs by
this \Coq definition. This is precise enough for our present
purposes, however, since it has no effect on questions such as
\emph{are there critical pairs?} or \emph{are all critical pairs
  trivial?}.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.criticalpair}{critical\_pair}{\coqdocdefinition{critical\_pair}}
(\coqdocvar{$\mathcal{R}$} : \coqdocdefinition{trs})
(\coqdocvar{t$_1$} \coqdocvar{t$_2$} :
\coqref{Term.term}{\coqdocinductive{term}}) : \coqdockw{Prop}
:=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{\exists} \coqdocvar{$\rho_1$} :
\coqdocrecord{rule}, \ensuremath{\exists}
\coqdocvar{$\rho_2$} :
\coqdocrecord{rule},
\ensuremath{\exists} \coqdocvar{p} :
\coqdocabbreviation{position},
\ensuremath{\exists} \coqdocvar{$\sigma$},
\ensuremath{\exists} \coqdocvar{$\tau$},\coqdoceol
\coqdocindent{3.00em}
\coqdocvariable{$\rho_1$} \coqdocdefinition{$\in$}
\coqdocvariable{$\mathcal{R}$}
\ensuremath{\land}
\coqdocvariable{$\rho_2$} \coqdocdefinition{$\in$}
\coqdocvar{$\mathcal{R}$} \ensuremath{\land}
(\coqdocvariable{$\rho_1$} = \coqdocvariable{$\rho_2$}
\ensuremath{\rightarrow}
\coqdocvariable{p} \ensuremath{\not=}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{nil}{\coqdocconstructor{nil}})
\ensuremath{\land}\coqdoceol
\coqdocindent{3.00em}
\coqdockw{match}
\coqref{Term.subterm}{\coqdocdefinition{subterm}} (\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocvariable{$\rho_1$}) \coqdocvariable{p},
\coqref{Context.dig}{\coqdocdefinition{dig}} (\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocvariable{$\rho_1$})$^{\coqdocvariable{$\sigma$}}$ \coqdocvariable{p}
\coqdockw{with}\coqdoceol
\coqdocindent{3.00em}
\ensuremath{|}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
\coqdocvar{s},
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{Some}{\coqdocconstructor{Some}}
\coqdocvar{C} \ensuremath{\Rightarrow}
\coqdocdefinition{is\_var} \coqdocvariable{s} =
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Datatypes}{false}{\coqdocconstructor{false}}
\ensuremath{\land}
\coqdocvariable{s}$^{\coqdocvariable{$\sigma$}}$ \coqref{TermEquality.termbis}{$\bis$}
(\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocvariable{$\rho_2$})$^{\coqdocvariable{$\tau$}}$
\ensuremath{\land}\coqdoceol
\coqdocindent{12.00em}
\coqdocvariable{t$_1$} \coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{C}[(\coqref{Rewriting.rhs}{\coqdocprojection{rhs}}
\coqdocvariable{$\rho_2$})$^{\coqdocvariable{$\tau$}}$]
\ensuremath{\land}
\coqdocvariable{t$_2$} \coqref{TermEquality.termbis}{$\bis$}
(\coqref{Rewriting.rhs}{\coqdocprojection{rhs}}
\coqdocvariable{$\rho_1$})$^{\coqdocvariable{$\sigma$}}$\coqdoceol
\coqdocindent{3.00em}
\ensuremath{|} \coqdocvar{\_}, \coqdocvar{\_}
\ensuremath{\Rightarrow}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Init.Logic}{False}{\coqdocinductive{False}}\coqdoceol
\coqdocindent{3.00em}
\coqdockw{end}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Now we can in a straightforward manner define the properties of
orthogonality and weak orthogonality.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.orthogonal}{orthogonal}{\coqdocdefinition{orthogonal}} (\coqdocvar{$\mathcal{R}$} :
\coqdocdefinition{trs})
: \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\coqdocdefinition{trs\_left\_linear}
\coqdocvariable{$\mathcal{R}$} \ensuremath{\land}
\ensuremath{\forall} \coqdocvar{t$_1$} \coqdocvar{t$_2$},
\ensuremath{\lnot}
\coqref{Rewriting.criticalpair}{\coqdocdefinition{critical\_pair}}
\coqdocvariable{t$_1$} \coqdocvariable{t$_2$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

\pagebreak[3]

\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.weaklyorthogonal}{weakly\_orthogonal}{\coqdocdefinition{weakly\_orthogonal}} (\coqdocvar{$\mathcal{R}$} :
\coqdocdefinition{trs})
: \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\coqdocdefinition{trs\_left\_linear}
\coqdocvariable{$\mathcal{R}$} \ensuremath{\land}
\ensuremath{\forall} \coqdocvar{t$_1$} \coqdocvar{t$_2$},
\coqref{Rewriting.criticalpair}{\coqdocdefinition{critical\_pair}}
\coqdocvariable{t$_1$} \coqdocvariable{t$_2$} \ensuremath{\rightarrow}
\coqdocvariable{t$_1$} \coqref{TermEquality.termbis}{$\bis$} \coqdocvariable{t$_2$}.\coqdoceol
\end{coqdoccode}
\end{singlespace}

Next we define when a term is a normal form and when we have unique
normal forms.
\begin{singlespace}
\begin{coqdoccode}
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.normalform}{normal\_form}{\coqdocdefinition{normal\_form}}
\coqdocvar{t} : \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{\lnot} \ensuremath{\exists} \coqdocvar{C} : \coqref{Context.context}{\coqdocinductive{context}},
\ensuremath{\exists} \coqdocvar{$\rho$} : \coqdocrecord{rule},
\ensuremath{\exists} \coqdocvar{$\sigma$} :
\coqref{Substitution.substitution}{\coqdocdefinition{substitution}},
\coqdocvariable{$\rho$}
\coqexternalref{http://coq.inria.fr/stdlib/Coq.Lists.List}{In}{\coqdocdefinition{$\in$}}
\coqdocvar{$\mathcal{R}$} \ensuremath{\land}
\coqdocvariable{C}[(\coqref{Rewriting.lhs}{\coqdocprojection{lhs}}
\coqdocvariable{r})\coqdocvariable{$^\sigma$}] \coqref{TermEquality.termbis}{$\bis$}
\coqdocvariable{t}.\coqdoceol
\coqdocemptyline
\coqdocnoindent
\coqdockw{Definition}
\coqdef{Rewriting.uniquenormalforms}{unique\_normal\_forms}{\coqdocdefinition{unique\_normal\_forms}}
: \coqdockw{Prop} :=\coqdoceol
\coqdocindent{1.00em}
\ensuremath{\forall} \coqdocvar{s} \coqdocvar{t} \coqdocvar{u}
(\coqdocvar{$\varphi$} : \coqdocvariable{s} \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvariable{t})
(\coqdocvar{$\psi$} : \coqdocvariable{s} \coqref{Rewriting.sequence}{$\rewrites_\mathcal{R}$} \coqdocvariable{u}),\coqdoceol
\coqdocindent{2.00em}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} \coqdocvariable{$\varphi$}
\ensuremath{\rightarrow}
\coqref{Rewriting.wf}{\coqdocdefinition{wf}} \coqdocvariable{$\psi$}
\ensuremath{\rightarrow}
\coqref{Rewriting.normalform}{\coqdocdefinition{normal\_form}}
\coqdocvariable{t} \ensuremath{\rightarrow}
\coqref{Rewriting.normalform}{\coqdocdefinition{normal\_form}}
\coqdocvariable{u} \ensuremath{\rightarrow}
\coqdocvariable{t} \coqref{TermEquality.termbis}{$\bis$} \coqdocvariable{u}.\coqdoceol
\end{coqdoccode}
\end{singlespace}
Note that the
\coqref{Rewriting.uniquenormalforms}{\coqdocdefinition{unique\_normal\_forms}}
definition is only a translation of the $UN^\rewrites$ property, not
of the more general $UN^\infty$ property (see also
Definition~\ref{def:normalisation}).


%!TEX root = TDT4265-Summary.tex
\section{Spatial filtering}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fundamentals of spatial filtering}

\subsubsection{Mechanics of spatial filtering}
A spatial filter consists of a neighborhood and an operation performed on the pixels in the neighborhood. A \emph{linear filter} requires a linear operation, such as summation. Otherwise, it is a \emph{nonlinear filter}.

\subsubsection{Spatial correlation and convolution}
Neighborhood operations where each output pixel is a weighted sum of neighbors to the corresponding input pixel. The weights are stored in a matrix called filter, mask or kernel. Correlation and convolution are the same except for the fact that the filter is rotated $180 \degree$ for convolution.

If the input image is a discrete unit impulse (all zeros except a single `1' value), the output of convolution is the filter itself at the position of the unit impulse. Correlation has the same result, but the output is rotated 180 degrees. Convolution is commutative and associative:
\begin{gather}
    f \conv g = g \conv f \\
    f \conv (g \conv h) = (f \conv g) \conv h
\end{gather}
However, correlation is \emph{not}.

Because of the similarity of the operations, often only \eqref{eq:corr} is implemented, and the filter is rotated beforehand for convolution.

\paragraph{Correlation}
\begin{equation}\label{eq:corr}
    w(x,y) \corr f(x,y)
    =
    \sum_{s=-a}^{a}
    \sum_{t=-b}^{b}
    w(s,t) f(x+s, y+t)
\end{equation}

\paragraph{Convolution}
\begin{equation}\label{eq:conv}
    w(x,y) \conv f(x,y)
    =
    \sum_{s=-a}^{a}
    \sum_{t=-b}^{b}
    w(s,t) f(x-s, y-t)
\end{equation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Smoothing spatial filters}
Used for blurring and noise reduction. Blurring can e.g. remove small, unwanted details before object detection.

Smoothing linear filters take an average of pixels under the mask. Also called averaging or lowpass filters. A typical filter is
\begin{equation}
    \frac{1}{9}
    \cdot
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        1 & 1 & 1 \\
        1 & 1 & 1 \\
        1 & 1 & 1
    \end{BMAT}
    \, .
\end{equation}
Such filters where all coefficients are equal are called box filters. Another possibility is a weighted average such as
\begin{equation}
    \frac{1}{16}
    \cdot
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        1 & 2 & 1 \\
        2 & 4 & 2 \\
        1 & 2 & 1
    \end{BMAT}
    \, ,
\end{equation}
which can smooth an image with less blurring than a box filter.

\subsubsection{Median filter}
A nonlinear filter, good for removing e.g. salt-and-pepper noise. Does not blur sharp edges. Method:
\begin{enumerate}
    \item Order pixels in the neighborhood by intensity.
    \item Replace center pixel with median intensity.
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Sharpening spatial filters}

Just like smoothing/averaging is analogous to integration, sharpening can be done by differentiation. Some requirements for a sharpening filter based on the first derivative are
\begin{itemize}
    \item zero in constant areas,
    \item nonzero at the onset and end of an intensity ramp, and
    \item nonzero during an intensity ramp.
\end{itemize}
Requirements for a secord-order derivative filter are the same, with the difference that it must be zero during ramps. The one-dimensional definitions
\begin{equation}
\begin{split}
    \pd{f}{x}    &= f(x+1) - f(x) \\
    \pd[2]{f}{x} &= f(x+1) + f(x-1) - 2f(x)
\end{split}
\end{equation}
satisfy these requirements.

\subsubsection{Image sharpening with the Laplacian}
For 2D images, sharpening filters can be based on the Laplacian
\begin{equation}
    \nabla^2 f = \pd[2]{f}{x} + \pd[2]{f}{y} .
\end{equation}

Some common filters are:
\begin{equation}\label{eq:laplacian-filters}
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        0 &  1 & 0 \\
        1 & -4 & 1 \\
        0 &  1 & 0
    \end{BMAT}
    \quad
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        1 &  1 & 1 \\
        1 & -8 & 1 \\
        1 &  1 & 1
    \end{BMAT}
    \quad
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
         0 & -1 &  0 \\
        -1 &  4 & -1 \\
         0 & -1 &  0
    \end{BMAT}
    \quad
    \begin{BMAT}(e){|c|c|c|}{|c|c|c|}
        -1 & -1 & -1 \\
        -1 &  8 & -1 \\
        -1 & -1 & -1
    \end{BMAT}
\end{equation}

The sharpened image is obtained by taking the sum of the original and the filtered image:
\begin{equation}
    g(x,y) = f(x,y) + c [\nabla^2 f(x,y)]
\end{equation}
where $c$ is $-1$ if the center element of the filter is negative, and $1$ otherwise.

\subsubsection{Unsharp masking and highboost filtering}\label{sssec:unsharp-highboost-spatial}
Unsharp masking sort of goes in the opposite direction of Laplacian sharpening: First blur the image, and subtract the blurred from the original to get a an unsharp mask $g\sub{mask}(x,y) = f(x,y) - \overline{f}(x,y)$. Finally add the mask to the original:
\begin{equation}\label{eq:unsharp-masking}
    g(x,y) = f(x,y) + k \cdot g\sub{mask}(x,y)
\end{equation}
Unsharp masking is strictly speaking only for $k = 1$. With $k > 1$, we have highboost filtering, and you can also lessen the effect with $k < 1$. The result is a sharpened image, because you remove unsharpness, which increases corner contrast.

\subsubsection{Image sharpening with the Gradient}
Section \ref{sssec:edge-detection} defines the gradient. Its magnitude
\begin{equation}
    M(x,y) = \sqrt{g_x^2 + g_y^2} \approx \abs{g_x} + \abs{g_y}
\end{equation}
forms a gradient magnitude image. An example of masks that approximate this is the Sobel operator \eqref{eq:sobel}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection[Combining methods]{Combining spatial enhancement methods}
In the real world, you need to use several methods to solve a task. One example is to first sharpen the image, then enhance the dynamic range.


\chapter{Eigenfunctions of DT systems}
To summarize the course so far for DT analysis, given an input signal $x[n]$ and a LTI system described (equivalently) by a linear, constant coefficient difference equation, impulse response, or a block diagram, we can determine the output using convolution. This is referred to as \emph{discrete time-domain} analysis since the index $n$ usually refers to a time index.

Like in CT, the advantages of this approach are that the analysis is straightforward and applies to all LTI systems, stable or otherwise. Discrete time-domain representations of signals are also intuitive when viewed as equally-spaced samples of physical signals.

As in CT, there are disadvantages. It does not scale well to larger systems since analysis with block diagram decompositions requires convolution, and in the case of the feedback motif dealing with inverse systems or de-convolution. It is difficult to design an impulse responses for a given purpose. Finally implementing a DT system directly from an impulse response is not intuitive.

Similar to CT we can transform the domain of the signal representations to one in which the operation of DT convolution becomes one of multiplication.

\section{The Response of DT LTI Systems to Complex Exponentials}

Recall convolution can be viewed as a decomposition of a signal into an infinite sum of $\delta$ functions plus the linearity property.
\[
x[n] = \sum\limits_{m = -\infty}^{\infty} x[m]\delta[n-m] \;\longrightarrow\; y[n] = \sum\limits_{m = -\infty}^{\infty} x[m]h[n-m]
\]
We now consider a different decomposition based on the complex exponential, $z^n$ for $z \in \mathbb{C}$, rather than $\delta$ functions. As we will see this decomposition simplifies convolution, turning it into multiplication.

\subsection{Eigenfunction $z^n$ and Transfer Function $H(z)$}

Let $x[n] = z^n$ for $z\in \mathbb{C}$, then $y[n] = h[n] * x[n] = x[n] * h[n]$ and by the definition of DT convolution
\begin{align*}
  y[n] & = \sum\limits_{m = -\infty}^{\infty}h[m]x[n-m]\\
  &= \sum\limits_{m = -\infty}^{\infty}h[m]z^{n-m} = \sum\limits_{m = -\infty}^{\infty}h[m]z^{n}z^{-m}\\
  &= z^{n} \sum\limits_{m = -\infty}^{\infty}h[m]z^{-m}\\
  &= z^{n}H(z)
\end{align*}
where $H(z) = \sum\limits_{m = -\infty}^{\infty}h[m]z^{-m}$ is the \emph{Z Transform} of the impulse response, $h[n]$. $H(z)$ is called the \emph{transfer function} or \emph{Eigenvalue} of the system and $z^{n}$ is the \emph{Eigenfunction} for DT LTI systems.

Similar to the impulse function, the complex exponential is a special signal because it's response is easy to determine. It is just the same signal scaled by a multiplicative factor as illustrated below:

\begin{center}
  \includegraphics[scale=0.6]{graphics/lti-dt-complex-exp.pdf}
\end{center}

\begin{example}
  For example, suppose $H(z) = \frac{z}{z-\frac{1}{2}}$ and $x[n] = \left(-\frac{1}{4}\right)^n$. Then the output is
  \begin{align*}
    y[n] &= H\left(-\frac{1}{4}\right)\left(-\frac{1}{4}\right)^n\\
    &= \frac{-\frac{1}{4}}{-\frac{1}{4}-\frac{1}{2}}\left(-\frac{1}{4}\right)^n\\
    &= \frac{1}{3}\left(-\frac{1}{4}\right)^n \; ,
  \end{align*}
another complex exponential.\\
$\blacksquare$
\end{example}

Given $H(z)$ and inputs that are sums of complex exponentials, the output is easy to determine.

\begin{center}
  \includegraphics[scale=0.6]{graphics/dt-linear-response-complex-exp.pdf}
\end{center}
In some cases the sums are countably infinite while in others the uncountably infinite so that the sums become integrals.

\begin{example} Consider the DT system with impulse response response
  \[
  h[n] = \left(\frac{3}{4}\right)^{n}u[n]
  \]
  Determine the Eigenvalues that corresponds to the input $x[n] = \cos(n)$ and the output $y[n]$.\\

  Solution: We note the cosine can be decomposed into two complex exponentials as
  \[
  \cos(n) = \frac{1}{2}e^{jn} + \frac{1}{2}e^{-jn} = \frac{1}{2}\left(e^{j}\right)^n + \frac{1}{2}\left(e^{-j}\right)^n
  \]
  Thus in terms of the general decomposition there are two terms with complex constants $z_1 = e^{j}$ and $z_2 = e^{-j}$ and real constants $a_1 = a_2 = \frac{1}{2}$.
  \[
   x[n] = \sum_i a_i z_i^{n} = a_1 z_1^{n} + a_2 z_2^{n} = \frac{1}{2}\left(e^{j}\right)^n + \frac{1}{2}\left(e^{-j}\right)^n = \cos(n)
   \]
   Then the output is given by
   \[
   y[n] = \sum_i H(z_i) a_i z_i^{n} = H(z_1) a_1 z_1^{n} + H(z_2) a_2 z_2^{n} = H\left(e^{j}\right)\frac{1}{2}\left(e^{j}\right)^n + H\left(e^{-j}\right)\frac{1}{2}\left(e^{-j}\right)^n
   \]
   which requires we find the Eigenvalues $H\left(e^{j}\right)$ and $H\left(e^{-j}\right)$. To do so we use the Z transform summation
   \[
   H\left(e^{j}\right) = \sum\limits_{m = -\infty}^{\infty}h[m]\left(e^{j}\right)^{-m} = \sum\limits_{m = 0}^{\infty} \left(\frac{3}{4}\right)^{m}\left(e^{j}\right)^{-m} = \sum\limits_{m =0}^{\infty} \left(\frac{3}{4\left(e^{j}\right)}\right)^{m} = \frac{-1}{\left(\frac{3}{4e^{j}}\right)-1} = \frac{e^{j}}{e^{j}-\left(\frac{3}{4}\right)}
   \]
   Similarly
   \[
   H\left(e^{-j}\right) = \sum\limits_{m = -\infty}^{\infty}h[m]\left(e^{-j}\right)^{-m} = \sum\limits_{m = 0}^{\infty} \left(\frac{3}{4}\right)^{m}\left(e^{-j}\right)^{-m} = \sum\limits_{m =0}^{\infty} \left(\frac{3}{4\left(e^{-j}\right)}\right)^{m} = \frac{-1}{\left(\frac{3}{4e^{-j}}\right)-1} = \frac{e^{-j}}{e^{-j}-\left(\frac{3}{4}\right)}
   \]

   Substituting back into the output equation gives

   \begin{align*}
     y[n] &= H\left(e^{j}\right)\frac{1}{2}\left(e^{j}\right)^n + H\left(e^{-j}\right)\frac{1}{2}\left(e^{-j}\right)^n\\
     &= \frac{e^{j}}{e^{j}-\left(\frac{3}{4}\right)}\frac{1}{2}\left(e^{j}\right)^n + \frac{e^{-j}}{e^{-j}-\left(\frac{3}{4}\right)}\frac{1}{2}\left(e^{-j}\right)^n\\
   \end{align*}
   We can simplify this expression using the polar form of the Eigenvalues
   \begin{align*}
     y[n] &= \frac{e^{j}}{e^{j}-\left(\frac{3}{4}\right)}\frac{1}{2}\left(e^{j}\right)^n + \frac{e^{-j}}{e^{-j}-\left(\frac{3}{4}\right)}\frac{1}{2}\left(e^{-j}\right)^n\\
     &= Re^{j\theta} \frac{1}{2}e^{jn} + Re^{-j\theta} \frac{1}{2}e^{-jn}\\
     &= R \frac{1}{2}e^{jn + j\theta} + R \frac{1}{2}e^{-jn -j\theta}\\
     &= R\cos(n + \theta)
   \end{align*}
   where
   \[
   R = \left|\frac{e^{j}}{e^{j}-\left(\frac{3}{4}\right)}\right| \approx 1.153  \mbox{ and } \theta = \angle{\frac{e^{j}}{e^{j}-\left(\frac{3}{4}\right)}} \approx -0.815
   \]
   Note for this system, given a sinusoidal input, the output is a scaled and phase shifted sinusoid at the same frequency, where the scaling factor and phase shift is system dependent. It is illustrative to compare this analysis to the time-domain analysis of the same impulse response and input using convolution.
   $\blacksquare$
\end{example}

\section{Decomposition of signals using DT complex exponentials}

Similar to CT, in this course we consider the cases of stable DT systems. Recall a stable system is one in which a bounded input leads to a bounded output, or equivalently the impulse response is absolutely summable. We will consider two decompositions of the input:

  \begin{itemize}
  \item \emph{Fourier Series}: When $x[n]$ is periodic with fundamental frequency $\omega_0 = \frac{2\pi}{N}$, $|z| = 1$ so that $z = e^{jk\omega_0}$, and the decomposition is a finite sum. This gives the input-output relationship
    \[
 x[n] = \sum\limits_{k = N_0}^{N_0 + N-1} a_k e^{jk\omega_0n} \;\longrightarrow\;  y[n] = \sum\limits_{k = N_0}^{N_0 + N-1} H\left(e^{j k\omega_0}\right) a_k e^{jk\omega_0 n}
    \]
    where $H\left(e^{j k\omega_0}\right)$ are the Eigenvalues, also called the DT \emph{frequency response}.
  \item \emph{Inverse Fourier Transform}: When $x[n]$ is a-periodic, $|z| = 1$ so that $z = e^{j\omega}$, and the decomposition is an integral over a finite length set. This gives the input-output relationship
    \[
     x[n] = \frac{1}{2\pi} \int_{2\pi} X\left(e^{j\omega}\right) e^{j\omega n} \; d\omega \;\longrightarrow\;   y[n] = \frac{1}{2\pi} \int_{2\pi} H\left(e^{j\omega}\right) \, X\left(e^{j\omega}\right) e^{j\omega n} \; d\omega
    \]
    where $H\left(e^{j \omega}\right)$ are the Eigenvalues, again called the DT \emph{frequency response}.
  \end{itemize}

  Other courses such as ECE 3704 look at the general case of unstable systems and $z \in \mathbb{C}$ with decompositions:

  \begin{itemize}
  \item \emph{One-Sided Z Transform}: $x[n]$ is causal and the decomposition is an uncountably infinite sum (complex integral)
  \item \emph{Two-Sided (Bilateral) Z Transform}: $x[n]$ is non-causal and the decomposition is an uncountably infinite sum (complex integral). This is the most general case for DT LTI systems.
  \end{itemize}

  While the Z decompositions require complex integration, like for the Laplace transform in CT, they can be understood and computed using algebra and a table of forward transforms, which only require summations of a complex function over a real variable $n$ (this is the general approach taken in upper level courses). However, this is outside the scope of this course because of time limitations.

Instead, we will be spending the next few weeks going through the DT Fourier decompositions in some detail. You will also learn how to find the DT frequency response for a stable system, and see how to use both for analysis.


\subsection{Higher Order Derivatives}
\begin{definition}
	Let $n$ be a positive integer. The $n^{th}$ derivative of $f$ is
	\begin{equation*}
		f^{(n)}(x) = \dd{}{x}{f^{(n-1)}}(x),
	\end{equation*}
	where $f^{(n-1)}(x)$ is the ${n-1}^{th}$ derivative of $f$.
	Using equivalent notation,
	\begin{equation*}
		\dd{{}^n}{x^n}f = \dd{{}^{n-1}}{x^{n-1}}f.
	\end{equation*}
\end{definition}

\begin{example}
	Find the third derivative of $f(x) = x^3 + x^2$.
\end{example}
\begin{answer}
	Taking the derivative once using the power rule and sum and difference rule,
	\begin{equation*}
		f^\prime(x) = 3x^2 + 2x.
	\end{equation*}

	Taking the derivative a second time using the power, constant multiple rules, and sum and difference rules,
	\begin{equation*}
		f^{\prime\prime}(x) = 6x + 2.
	\end{equation*}

	Taking the derivative a final time using the power, constant multiple, constant, and sum and difference rules,
	\begin{equation*}
		f^{\prime\prime\prime}(x) = 6.
	\end{equation*}
\end{answer}


%!TEX root = /home/renaud/Documents/EPL/tfe/latex/tfe.tex
\chapter{The stability criterion for community detection} \label{chap:clustering}
% \section{The stability criterion for graph communities} \label{sec:stability}
The partition of a graph into communities (or clusters) has been widely studied those last two decades. Clustering comes indeed pretty handy to gain insight into the underlying structure of a system represented by a network. In some cases one can even build a simplified functional description of the system based on the clusters. Many partitioning methods have been proposed, each relying on a particular measure to quantify the quality of a community structure. Such methods include normalized cut, ($\alpha$,$\epsilon$) clustering or modularity and its variants and extensions. The reader may refer to \cite{fortunato2010community} for a 2010 survey of the different clustering methods. In this work, we choose the stability approach, which is based on the statistical properties of a dynamical process taking place on the network. This approach was initially presented in \cite{delvenne2010stability} and further expended in \cite{lambiotte2009laplacian} and \cite{delvenne2013stability}.

The stability method presents a number of advantages. First, it does not require the number of communities to be specified beforehand, ensuring a natural partitioning of the graph. Second, it is flexible in the sense that it does not seek a \textit{unique} optimal partition. Instead, it reveals several community structures, each appearing to be the most relevant at particular values of the Markov time: at a given time scale, natural clusters corresponds to sets of states from which escape is unlikely within that time scale. The stability method provides thus a dynamical interpretation of the partitioning problem. The Markov time acts as an intrinsic resolution parameter, as will be developed shortly. Finally, it is probably the most unifying approach since many of the standard partitioning measures find an interpretation through the stability framework.

In order to compute stability partitions in the next of this work, we make use of Michael Schaub's free software \textit{PartitionStability}. This C++ implementation of the stability method with a \matlab interface is available at \url{https://github.com/michaelschaub/PartitionStability}. It relies on the Louvain algorithm \cite{blondel2008fast} to optimize the stability quality function. This heuristic algorithm has been initially developed for modularity optimization. However one can show that stability can be written as the \textit{modularity} of a time-dependent network evolving under the Markov process \cite{lambiotte2009laplacian}. Hence, the Louvain method can almost straightforwardly be applied to stability optimization.

This chapter is devoted to the explanation of the stability measure, and how to find good clusterings using stability analysis. It acts as a theoretical part intended to cover everything that is needed to make a proper, informed use of the stability toolbox. Notice that the stability measure has initially been presented for discrete times in \cite{delvenne2010stability}. We follow the same approach here: discrete-time stability is developed in the first section of this chapter; it is then extended to continuous time in a second section; finally, a few tools to analyze the robustness of a partition are presented in the third section of the chapter.
\input{inputs/clustering/discretetime}
\input{inputs/clustering/continuoustime}
\input{inputs/clustering/robustness}


\subsection{Maximum Entropy approximation}\label{sec_maxent}

Having numerically computed the moments of the mRNA and protein distributions
as cells progress through the cell cycle, we now proceed to make an
approximate reconstruction of the full distributions given this limited
information. As hinted in \secref{sec_moments} the maximum entropy principle,
first proposed by E.T. Jaynes in 1957 \cite{Jaynes1957}, approximates the
entire distribution by maximizing the Shannon entropy subject to constraints
given by the values of the moments of the distribution \cite{Jaynes1957}. This
procedure leads to a probability distribution of the form (See
\siref{supp_maxent} for full derivation)
\begin{equation}
  P(m, p) = {1 \over \mathcal{Z}}
              \exp \left( - \sum_{(x,y)} \lambda_{(x,y)} m^x p^y \right),
  \label{eq_maxEnt_joint}
\end{equation}
where $\lambda_{(x,y)}$ is the Lagrange multiplier associated with the
constraint set by the moment $\ee{m^x p^y}$, and $\mathcal{Z}$ is a
normalization constant. The more moments $\ee{m^x p^y}$ included as
constraints, the more accurate the approximation resulting from
\eref{eq_maxEnt_joint} becomes.

The computational challenge then becomes an optimization routine in which the
values for the Lagrange multipliers $\lambda_{(x,y)}$ that are consistent with
the constraints set by the moment values $\ee{m^x p^y}$ need to be found. This
is computationally more efficient than sampling directly out of the master
equation with a stochastic algorithm (see \siref{supp_gillespie} for further
comparison between maximum entropy estimates and the Gillespie algorithm).
\siref{supp_maxent} details our implementation of a robust algorithm to find
the values of the Lagrange multipliers. \fref{fig4_maxent}(A) shows example
predicted protein distributions reconstructed using the first six moments of
the protein distribution for a suite of different biophysical parameters and
environmental inducer concentrations. As repressor-DNA binding affinity
(columns in \fref{fig4_maxent}(A)) and repressor copy number (rows in
\fref{fig4_maxent}(A)) are varied, the responses to different signals, i.e.
inducer concentrations, overlap to varying degrees. For example, the upper
right corner frame with a weak binding site ($\eR = -9.7 \; k_BT$) and a low
repressor copy number (22 repressors per cell) have virtually identical
distributions regardless of the input inducer concentration. This means that
cells with this set of parameters cannot resolve any difference in the
concentration of the signal. As the number of repressors is increased, the
degree of overlap between distributions decreases, allowing cells to better
resolve the value of the signal input. On the opposite extreme the lower left
panel shows a strong binding site ($\eR = -15.3 \; k_BT$) and a high repressor
copy number (1740 repressors per cell). This parameter combination shows
overlap between distributions since the high degree of repression centers all
distributions towards lower copy numbers, again giving little ability for the
cells to resolve the inputs. In \fref{fig4_maxent}(B) and \siref{supp_maxent}
we show the comparison of these predicted cumulative distributions with the
experimental single-cell fluorescence distributions. Given the systematic
deviation of our predictions for the protein copy number noise highlighted in
\fref{fig3_cell_cycle}(C), the theoretical distributions (dashed lines)
underestimate the width of the experimental data. We again direct the reader to
\siref{supp_empirical} for an exploration of empirical changes to the moments
that improve the agreement of the predictions. In the following section we
formalize the notion of how well cells can resolve different inputs from an
information theoretic perspective via the channel capacity.

\begin{figure}[h!]
	\centering \includegraphics
  {./fig/main/fig4_maxent.pdf}
	\caption{\textbf{Maximum entropy protein distributions for varying physical
	parameters.} (A) Predicted protein distributions under different inducer
	(IPTG) concentrations for different combinations of repressor-DNA
	affinities (columns) and repressor copy numbers (rows). The first six
	moments of the protein distribution used to constrain the maximum entropy
	approximation were computed by integrating \eref{eq_gral_mom} as cells
	progressed through the cell cycle as described in \secref{sec_cell_cycle}.
	(B) Theory-experiment comparison of predicted fold-change empirical
	cumulative distribution functions (ECDF). Each panel shows two example
	concentrations of inducer (colored curves) with their corresponding
	theoretical predictions (dashed lines). Distributions were normalized to the
	mean expression value of the unregulated strain in order to compare
	theoretical predictions in discrete protein counts with experimental
	fluorescent measurements in arbitrary units.}
  \label{fig4_maxent}
\end{figure}