%%% Local Variables:
%%% mode: latex
%%% TeX-master: "program-analysis"
%%% End:

\chapter{Prerequisites}
\begin{chapquote}{H.G. Rice [1953], \textit{paraphrased by Anders Moller}}
  ``Everything interesting about the behaviour of programs is
  undecidable.''
\end{chapquote}

The goal of \textit{static program analysis} is to verify certain
\textsl{properties} (or \textsl{behaviours}, or
\textsl{specifications}, or \textsl{statements}, ...) of the target
program \textbf{without its execution}.

For program \textsl{P} and property \textsl{S},

\begin{itemize}
\item $ \SEM{P} $: Formal semantics of program \textsl{P}.

\item \textsl{S}: Semantic properties that we're interested in. This
  could be defined in various level, such as ``Division-by-zero will
  \textbf{never} occur'' or ``The variable \textit{i} is always 3''.

\item Soundness: $ analysis(P) = true \implies S $

\item Completeness: $ S \implies analysis(P) = true $

\item Scalability: Time complexity.
\end{itemize}

\textbf{No analysis} can be sound and complete at the same time. If an
analysis is sound, then it is also incomplete, and vice versa.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Relation Theory}
This note originates from
Proofwiki\footnote{https://proofwiki.org}.

\subsection{Relation}

Let $S \times T$ be the Cartesian product of two sets $S$ and $T$.A
\textbf{relation} on $S \times T$ is an ordered triple
$ \mathcal{R} = (S, T, R) $ where $R \subseteq S \times T$ is a subset
of the Cartesian product of $S$ and $T$.

What this means is that a \textbf{relation} \textit{relates} (certain)
elements of one set or class $S$ with (certain) elements of another,
$T$. Not all elements of $S$ need to be related to every (or even any)
element of $T$.

\paragraph{Notation}

If $(x, y)$ is an ordered pair such that $(x, y) \in \mathcal{R}$, we
use the notation: $ s \mathcal{R} t$ or $ \mathcal{R}(s, t)$ and can
say:
\begin{itemize}
\item $s$ \textbf{bears} $\mathcal{R}$ to $t$
\item $s$ \textbf{stands in} the relation $\mathcal{R}$ to $t$
\end{itemize}

\paragraph{General Definition}

Let
$\mathbb{S} = \displaystyle \prod_{i=1}^n S_i = S_1 \times S_2 \times ... \times S_n $
be the Cartesian product on $n$ sets $S_1, ..., S_n$.

An \textbf{$n$-ary relation on} $\mathbb{S}$ is an ordered
$n+1$-tuple $\mathcal{R}$ defined as

\begin{math}
  \begin{array}{c}
    \\
    \mathcal{R} := (S_1, S_2, ..., S_n, R)\\
    \\
  \end{array}
\end{math}

where $\mathcal{R}$ is an arbitrary subset
$\mathcal{R} \subseteq \mathbb{S}$.

To indicate that $(s_1, s_2, ..., s_n) \in R$, we write:
$\mathcal{R}(s_1, s_2, ..., s_n)$

\paragraph{Unary Relation}

As a special case of an $n$-ary relation on $S$, note that when $n=1$
we define a \textbf{unary relation} on $S$ as
$\mathcal{R} \subseteq S$. That is, a \textbf{unary relation} is a
subset of $S$.

\subsection{Domain}
\label{sec:domain}

Let $\mathcal{R} \subseteq S \times T$ be a relation. The
\textbf{domain} of $\mathcal{R}$ is defined and denoted as:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Dom}(\mathcal{R}) := \{ s \in S: \exists t \in T: (s, t) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

\paragraph{General Definition}

Let $\displaystyle \prod_{i=1}^n S_i$ be the Cartesian product of sets
$S_1$ to $S_n$. Let
$\mathcal{R} \subseteq \displaystyle \prod_{i=1}^n S_i$ be an $n$-ary
relation on $\displaystyle \prod_{i=1}^n S_i$.

The \textbf{domain of $\mathcal{R}$} is the set defined as:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Dom}(\mathcal{R}) := \{ (s_1, s_2, ..., s_{n-1}) \in \displaystyle \prod_{i=1}^{n-1} S_i : \exists s_n \in S_n : (s_1, s_2, ..., s_n) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

The concept is usually encountered when $\mathcal{R}$ is an
endorelation on $S$:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Dom}(\mathcal{R}) := \{ (s_1, s_2, ..., s_{n-1}) \in S^{n-1} : \exists s_n \in S : (s_1, s_2, ..., s_n) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

\subsection{Codomain}
\label{sec:codomain}

The \textbf{codomain} of a relation $\mathcal{R} \subseteq S \times T$
is the set $T$. It can be denoted as $\mathtt{Cdm}(\mathcal{R})$.

\subsection{Image}
\label{sec:image}

Let $\mathcal{R} \subseteq S \times T$ be a relation. The
\textbf{image} of $\mathcal{R}$ is the set:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Img}(\mathcal{R}) := \mathcal{R}[S] = \{ t \in T: \exists s \in S: (s, t) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

\paragraph{General Definition}

Let $\displaystyle \prod_{i=1}^n S_i$ be the Cartesian product of sets
$S_1$ to $S_n$. Let
$\mathcal{R} \subseteq \displaystyle \prod_{i=1}^n S_i$ be an $n$-ary
relation on $\displaystyle \prod_{i=1}^n S_i$.

The \textbf{image of $\mathcal{R}$} is the set defined as:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Img}(\mathcal{R}) := \{ s_n \in S_n: \exists (s_1, s_2, ..., s_{n-1} \in \displaystyle \prod_{i=1}^{n-1} S_i: (s_1, s_2, ..., s_n) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

The concept is usually encountered when $\mathcal{R}$ is an
endorelation on $S$:

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{Img}(\mathcal{R}) := \{ s_n \in S: \exists (s_1, s_2, ..., s_{n-1}) \in S^{n-1}: (s_1, s_2, ..., s_n) \in \mathcal{R} \}\\
    \\
  \end{array}
\end{math}

\subsection{Endorelation}
\label{sec:endorelation}
Let $S \times S$ be the Cartesian product of a set or class $S$ with
itself. Let $\mathcal{R}$ be a \textbf{relation} on $S \times S$. Then
$\mathcal{R}$ is referred to as an \textbf{endorelation on} $S$.

The term \textbf{endorelation} is rarely seen. Once it is established
that the \textit{domain} and \textit{codomain} of a given relation are
the \textbf{same set}, further comment is rarely needed.

An \textbf{endorelation} is also called a \textbf{relation in} $S$, or
a \textbf{relation on} $S$. The latter term is discouraged, though,
because it can also mean a left-total relation, and confusion can
arise.

Some sources use the term \textbf{binary relation} exclusively to
refer to a \textbf{binary endorelation}.

\subsection{Many-to-One Relation}
\label{sec:many-to-one}

A relation $\mathcal{R} \subseteq S \times T$ is \textbf{many-to-one}
if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in \mathtt{Dom}(\mathcal{R}): \forall y_1, y_2 \in \mathtt{Cdm}(\mathcal{R}): (x, y_1) \in \mathcal{R} \land (x, y_2) \in \mathcal{R} \implies y_1 = y_2 \\
    \\
  \end{array}
\end{math}

That is, every element of the domain of $\mathcal{R}$ relates to no
more than one element of its codomain.

\subsection{One-to-Many Relation}
\label{sec:one-to-many}

A relation $\mathcal{R} \subseteq S \times T$ is \textbf{one-to-many}
if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall y \in \mathtt{Img}(\mathcal{R}): (x_1, y) \in \mathcal{R} \land (x_2, y) \in \mathcal{R} \implies x_1 = x_2 \\
    \\
  \end{array}
\end{math}

That is, every element of the image of $\mathcal{R}$ relates to by
exactly one element of its domain. Note that the condition concerns
the elements in the \textit{image}, not the
\textit{codomain}\footnote{Thus, a one-to-many relation may leave some
  element(s) of the codomain unrelated}.

Also called as \textit{injective relation}\ref{sec:injection}.

\subsection{One-to-One Relation}
\label{sec:one-to-one}

A relation $\mathcal{R} \subseteq S \times T$ is \textbf{one-to-one}
if it is both many-to-one and one-to-many. That is, every element of
the domain of $\mathcal{R}$ relates to no more than one element of its
codomain, and every element of the image is related to by exactly one
element of its domain.

\subsection{Left-Total Relation}
\label{sec:left-total}

Let $S$ and $T$ be sets, and $\mathcal{R} \subseteq S \times T$ be a
relation in $S$ to $T$. Then $\mathcal{R}$ is \textbf{left-total} if
and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall s \in S: \exists t \in T: (s, t) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

That is, if and only if every element of $S$ relates to some element
of $T$, i.e., the domain of $\mathcal{R}$ equals to $S$.

\subsection{Right-Total Relation}
\label{sec:right-total}

Let $S$ and $T$ be sets, and $\mathcal{R} \subseteq S \times T$ be a
relation in $S$ to $T$. Then $\mathcal{R}$ is \textbf{right-total} if
and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall t \in T: \exists s \in S: (s, t) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

That is, if and only if every element of $T$ relates to by some
element of $S$, i.e., the image of $\mathcal{R}$ equals to $T$.

\subsection{Mapping}
\label{sec:mapping}

Let $S$ and $T$ be sets, and $S \times T$ be their Cartesian product.

\paragraph{Definition 1}

A \textbf{mapping} from $S$ to $T$ is a binary relation on
$S \times T$ which associates each element of $S$ with exactly one
element of $T$.

\paragraph{Definition 2}

A \textbf{mapping $f$ from $S$ to $T$}, denoted $f: S \to T$, is a
relation $f = (S, T, G)$ where $G \subseteq S \times T$ such that:

\begin{math}
  \begin{array}{l}
    \\
    \forall x \in S: \forall y_1, y_2 \in T: (x, y_1) \in G \land (x, y_2) \in G \implies y_1 = y_2 \\
    \text{and} \\
    \forall x \in S: \exists y in T: (x, y) \in G \\
    \\
  \end{array}
\end{math}

\paragraph{Definition 3}

A \textbf{mapping $f$ from $S$ to $T$}, denoted $f: S \to T$, is a
relation $f = (S, T, R)$ where $R \subseteq S \times T$ such that:

\begin{math}
  \begin{array}{l}
    \\
    \forall (x_1, y_1), (x_2, y_2) \in \mathcal{R}: y_1 \neq y_2 \implies x_1, x_2 \\
    \text{and} \\
    \forall x \in S: \exists y \in T: (x, y) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

\paragraph{Definition 4}

A \textbf{mapping from $S$ to $T$} is a relation on $S \times T$ which is:

\begin{itemize}
\item Many-to-one
\item Left-total, that is, defined for all elements in $S$
\end{itemize}

\paragraph{Self-Map}
\label{sec:self-map}

Let $S$ be a set. A \textbf{self-map on $S$} is a \textbf{mapping}
from $S$ to itself: $f: S \to S$.

\paragraph{Defined}
\label{sec:defined}

A mapping $f \subseteq S \times T$ is \textbf{defined} at $x \in S$ if
and only if:

\begin{math}
  \begin{array}{c}
    \\
    \exists y \in T: (x, y) \in f\\
    \\
  \end{array}
\end{math}

If for some $x \in S$, one has:

\begin{math}
  \begin{array}{c}
    \\
    \forall y \in T: (x, y) \notin f\\
    \\
  \end{array}
\end{math}

then $f$ is \textbf{not defined} or \textbf{undefined} at $x$, and
indeed, $f$ is not technically a mapping at all.

\subsection{Injection}
\label{sec:injection}

A mapping $f$ is an \textbf{injection} or \textbf{injective} if and
only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x_1, x_2 \in \mathtt{Dom}(f): f(x_1) = f(x_2) \implies x_1 = x_2 \\
    \\
  \end{array}
\end{math}

That is, an injection is a mapping such that the output
\textit{uniquely determines} its input.

\subsection{Surjection}
\label{sec:surjection}

Let $S$ and $T$ be sets and $f : S \to T$ be a mapping from $S$ to
$T$. $f$ is a \textbf{subjection} if and only if

\begin{math}
  \begin{array}{c}
    \\
    \forall y \in T: \exists x \in \mathtt{Dom}(f) : f(x) = y\\
    \\
  \end{array}
\end{math}

That is, if and only if $f$ is right-total.

Also called as \textbf{onto mapping}, or just \textbf{onto}\footnote{A
  mapping which is not surjective is hence described as \textbf{into}}.

\subsection{Bijection}
\label{sec:bijection}

A mapping $f: S \to T$ is a \textbf{bijection} if and only if both
\begin{itemize}
\item $f$ is an injection
\item $f$ is a surjection
\end{itemize}

\subsection{Symmetry}
\label{sec:symmetry}
The word \textit{symmetry} comes from Greek symmetria meaning
\textbf{measure together}.

\paragraph{Definition}

Let $\mathcal{R} \subseteq S \times S$ be a relation in $S$.

\subsubsection{Symmetric}

$\mathcal{R}$ is \textbf{symmetric} if and only if

\begin{math}
  \begin{array}{c}
    \\
    (x, y) \in \mathcal{R} \implies (y, x) \in \mathcal{R}\\
    \\
  \end{array}
\end{math}

\subsubsection{Asymmetric}

$\mathcal{R}$ is \textbf{asymmetric} if and only if

\begin{math}
  \begin{array}{c}
    \\
    (x, y) \in \mathcal{R} \implies (y, x) \notin \mathcal{R}\\
    \\
  \end{array}
\end{math}

\subsubsection{Antisymmetric}

$\mathcal{R}$ is \textbf{antisymmetric} if and only if

\begin{math}
  \begin{array}{ll}
    \\
    &(x,y) \in \mathcal{R} \land (y, x) \in \mathcal{R} \implies x = y\\
    \text{i.e., } & \{(x, y), (y, x) \} \subseteq \mathcal{R} \implies x = y \\
    \\
  \end{array}
\end{math}

Antisymettry eliminates uncertain cases when both $a$ precedes $b$ and
$b$ precedes $a$.

\subsubsection{Non-symmetric}

$\mathcal{R}$ is \textbf{non-symmetric} if and only if it is neither
\textit{symmetric} nor \textit{asymmetric}.

\paragraph{Antisymmetric and Asymmetric}

Note the difference between:
\begin{itemize}
\item An \textit{asymmetric relation}, in which the fact that
  $(x, y) \in \mathcal{R}$ means that $(y, x)$ is defintely
  \textbf{not} in $\mathcal{R}$
\item An \textit{antisymmetric relation}, in which there \textit{may}
  be instances of both $(x, y) \in \mathcal{R}$ and
  $(y, x) \in \mathcal{R}$, but if there are, then it means that $x$
  and $y$ have to be the same object.
\end{itemize}

\subsection{Reflexivity}
\label{sec:reflexivity}

\paragraph{Definition}

Let $\mathcal{R} \subseteq S \times S$ be a relation in $S$.

\subsubsection{Reflexive}

$\mathcal{R}$ is \textbf{reflexive} if and only if

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S : (x, x) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

\subsubsection{Coreflexive}

$\mathcal{R}$ is \textbf{coreflexive} if and only if

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S : (x, y) \in \mathcal{R} \implies x = y\\
    \\
  \end{array}
\end{math}

\subsubsection{Antireflexive}

$\mathcal{R}$ is \textbf{antireflexive} if and only if

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S: (x, x) \notin \mathcal{R}\\
    \\
  \end{array}
\end{math}

\subsubsection{Non-reflexive}

$\mathcal{R}$ is \textbf{non-reflexive} if and only if it is neither
\textit{reflexive} nor \textit{antireflexive}.

\subsection{Transitivity}
\label{sec:transitivity}

\paragraph{Definition}

Let $\mathcal{R} \subseteq S \times S$ be a relation in $S$.

\subsubsection{Transitive}

$\mathcal{R}$ is \textbf{transitive} if and only if

\begin{math}
  \begin{array}{ll}
    \\
    & (x, y) \in \mathcal{R} \land (y, z) \in \mathcal{R} \implies (x, z) \in \mathcal{R}\\
    \text{i.e., } & \{(x, y), (y, z)\} \subseteq \mathcal{R} \implies (x, z) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

\subsubsection{Antitransitive}

$\mathcal{R}$ is \textbf{antitransitive} if and only if

\begin{math}
  \begin{array}{ll}
    \\
    & (x, y) \in \mathcal{R} \land (y, z) \in \mathcal{R} \implies (x, z) \notin \mathcal{R}\\
    \text{i.e., }& \{ (x, y), (y, z) \} \subseteq \mathcal{R} \implies (x, z) \notin \mathcal{R}\\
    \\
  \end{array}
\end{math}

\subsubsection{Non-transitive}

$\mathcal{R}$ is \textbf{non-transitive} if and only if it is neither
\textit{transitive} nor \textit{antitransitive}.

\subsubsection{Transitive Closure}
\label{sec:transitive-closure}

Let $\mathcal{R}$ be a relation on a set $S$. The \textbf{transitive
  closure of $\mathcal{R}$} is defined as the smallest transitive
relation on $S$ which contains $\mathcal{R}$ as a subset.

Or, the \textbf{transitive closure of $\mathcal{R}$} is defined as the
intersection of all transitive relations on $S$ which contains
$\mathcal{R}$.

Or, the \textbf{transitive closure of $\mathcal{R}$} is the relation
$\mathcal{R}^+$ defined as follows:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S, x \mathcal{R}^+ y \iff \exists n \in \mathbb{N}_{>0} : \exists s_0, s_1, ..., s_n \in S: s_0 = x, s_n = y \\
    \\
    s_0 \mathcal{R} s_1\\
    s_1 \mathcal{R} s_2\\
    \vdots\\
    s_{n-1} \mathcal{R} s_n \\
    \\
  \end{array}
\end{math}

\subsection{Total Relation}
\label{sec:total-relation}

Let $\mathcal{R} \subseteq S \times S$ be a relation on a set
$S$. Then $\mathcal{R}$ is defined as \textbf{total} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall a, b \in S : (a, b) \in \mathcal{R} \lor (b, a) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

That is, if and only if every pair of elements is related.

Also called as \textbf{strictly connected}, or \textbf{complete
  relation}.

\subsection{Connected Relation}
\label{sec:connected-relation}

Let $\mathcal{R} \subseteq S \times S$ be a relation on a set
$S$. Then $\mathcal{R}$ is \textbf{connected} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall a, b \in S: a \neq b \implies (a, b) \in \mathcal{R} \lor (b,a) \in \mathcal{R} \\
    \\
  \end{array}
\end{math}

That is, if and only if every pair of \textit{distinct} elements is
related.

A relation having the \textbf{connexivity} means that any pair of
elements in the set of the relation are comparable under the
relation. This also means that the set can be diagrammed as a line of
elements, giving it the name \textit{linear}. The connexivity also
implies reflexivity, i.e., $a \leq a$.

Also called as \textbf{weakly connected}, while \textit{strictly
  connected} refers to \textit{total
  relation}\ref{sec:total-relation}.

\subsection{Relation Compatible with Operation}
\label{sec:compatibility}

Let $(S, \circ)$ be a magma. Let $\mathcal{R}$ be a relation on $S$.

Then $\mathcal{R}$ is \textbf{compatible with $\circ$} if and only if:

\begin{itemize}
\item $\forall x, y, z \in S: x \mathcal{R} y \implies (x \circ z) \mathcal{R} (y \circ z)$
\item $\forall x, y, z \in S: x \mathcal{R} y \implies (z \circ x) \mathcal{R} (z \circ y)$
\end{itemize}

\subsection{Relational Structure}
\label{sec:relational-structure}

A \textbf{relational structure} is an ordered pair $(S, \mathcal{R})$,
where:

\begin{itemize}
\item $S$ is a set
\item $\mathcal{R}$ is an endorelation\ref{sec:endorelation} on $S$
\end{itemize}

Also called as a \textbf{relational system}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Binary Operation}
\label{sec:binary-operation}

A \textbf{binary operation} is a mapping $\circ$ from the Cartesian
product of two sets $S \times T$ to a universe $\mathbb{U}$:

\begin{math}
  \begin{array}{c}
    \\
    \circ: S \times T \to \mathbb{U} : \circ(s, t) = y \in \mathbb{U} \\
    \\
  \end{array}
\end{math}

If $S = T$ , then $\circ$ can be referred to as a \textbf{binary
  opeation on $S$}.

Some authors specify that a binary operation is defined such that
\textit{codomain} of $\circ$ is the same underlying set as that which
forms the \textit{domain}, that is: $\circ: S \times S \to S$.

\subsection{Algebraic Structure}
\label{sec:algebraic-structure}

An \textbf{algebraic structure} is an ordered tuple:

\begin{math}
  \begin{array}{c}
    (S, \circ_1, \circ_2, ..., \circ_n)
  \end{array}
\end{math}

where $S$ is a set which has one or more binary operations
$\circ_1, \circ_2, ..., \circ_n$ defined on all the elements of
$S \times S$.

An algebraic structure with one (binary) operation is thus an ordered
pair which can be denoted as $(S, \circ)$ or $(T, *)$ or
$\pair{S, \circ}$, and so on.

The set $S$ is called \textbf{underlying set}.

\subsection{Closure}
\label{sec:closure}

Let $(S, \circ)$ be an algebraic structure. Then $S$ has the property
of \textbf{closure under $\circ$} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall (x, y) \in S \times S : x \circ y \in S\\
    \\
  \end{array}
\end{math}

$S$ is said to be \textbf{closed under $\circ$}, or just that
$(S, \circ)$ \textbf{is closed}.

\subsection{Magma}
\label{sec:magma}

A \textbf{magma} is an algebraic structure $(S, \circ)$ such that $S$
is closed unser $\circ$.

That is, a magma is a pair $\pair{S, \circ}$ where:

\begin{itemize}
\item $S$ is a set
\item $\circ: S \times S \to S $ is a binary operation on $S$.
\end{itemize}

\subsection{Commutativity}
\label{sec:commutativity}

Let $(S, \circ)$ be an algebraic structure. Then $\circ$ is
\textbf{commutative on $S$} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S: x \circ y = y \circ x\\
    \\
  \end{array}
\end{math}

\subsection{Absorption Law}
\label{sec:absorption}

Let $(S, \circ, *)$ be an algebraic structure. Let both $\circ$ and
$*$ be commutative.

Then $\circ$ \textbf{absorbs} $*$ if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall a, b \in S: a \circ (a * b) = a\\
    \\
  \end{array}
\end{math}

This equality is called the \textbf{absorption law of $\circ$ for
  $*$}.

\subsection{Associativity}
\label{sec:associativity}

Let $(S, \circ)$ be an algebraic structure. Then $\circ$ is
\textbf{associative on $S$} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y, z \in S: (x \circ y) \circ z = x \circ (y \circ z)\\
    \\
  \end{array}
\end{math}

\subsection{Idempotence}
\label{sec:idempotence}

\paragraph{Idempotent Element}
Let $(S, \circ)$ be a magma. Let $x \in S$ have the property that
$ x \circ x = x$.

Then $x \in S $ is described as \textbf{idempotent (element) under the
  operation $\circ$}.

\paragraph{Idempotent Operation}
Let $(S, \circ)$ be a magma.

If \textit{all} the elements of $S$ are \textit{idempotent} under
$\circ$, then the term can be applied to the operation itself. Thus, a
binary operation $\circ$ is \textbf{idempotent} if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S: x \circ x = x\\
    \\
  \end{array}
\end{math}

Examples are: set union, set intersection, ...

\subsection{Identity}
\label{sec:identity}

Let $(S, \circ)$ be an algebraic structure.

\paragraph{Left Identity}
An element $e_L \in S$ is called a \textbf{left identity} if and only
if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S: e_L \circ x = x\\
    \\
  \end{array}
\end{math}

\paragraph{Right Identity}
An element $e_R \in S$ is called a \textbf{right identity} if and only
if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S: x \circ e_R = x\\
    \\
  \end{array}
\end{math}

\paragraph{Identity}
An element $e \in S$ is called an \textbf{identity (element)} if and
only if it is both a \textit{left identity} and \textit{right
  identity}

\begin{math}
  \begin{array}{c}
    \\
    \forall x \in S: e \circ x = x \circ e =  x\\
    \\
  \end{array}
\end{math}

\paragraph{Uniqueness of Identity}

Suppose $e_1$ and $e_2$ are both identity elements of $(S, \circ)$.

Then by the definition of identity elements:

\begin{math}
  \begin{array}{c}
    \\
    \forall s \in S: s \circ e_1 = s = e_2 \circ s\\
    \\
  \end{array}
\end{math}

Then:

\begin{math}
  \begin{array}{c}
    e_1 = e_2 \circ e_1 = e_2 \\
    \therefore e_1 = e_2
  \end{array}
\end{math}

\subsection{Semigroup}
\label{sec:semigroup}

Let $(S, \circ)$ be a magma. Then this magma is a \textbf{semigroup}
if and only if $\circ$ is \textit{associative} on $S$.

That is, a \textbf{semigroup} is an algebraic structure which is
\textit{closed} and whos operation is \textit{associative}.

\subsection{Monoid}
\label{sec:monoid}

A \textbf{monoid} is a \textit{semigroup} with an \textit{identity
  element}.

\paragraph{Monoid is not Empty}

Let $(S, \circ)$ be a monoid. By definition:

\begin{math}
  \begin{array}{c}
    \text{Identity: } \exists e_S \in S : \forall a \in S: a \circ e_S = a = e_S \circ a
  \end{array}
\end{math}

So a monoid must \textit{at least} have an identity.

Therefore $e_S \in S $ and so $S$ is not the empty set.

\subsection{Inverse}
\label{sec:inverse}

Let $(S, \circ)$ be a monoid whose identity is $e_S$.

\paragraph{Left Inverse}
An element $x_L \in S$ is called a \textbf{left inverse} of $x$ if and
only if:

\begin{math}
  \begin{array}{c}
    x_L \circ x = e_S
  \end{array}
\end{math}

\paragraph{Right Inverse}
An element $x_R \in S$ is called a \textbf{right inverse} of $x$ if and
only if:

\begin{math}
  \begin{array}{c}
    x \circ x_R = e_S
  \end{array}
\end{math}

\paragraph{Inverse}

Let $x , y \in S $ be elements. The element $y$ is an \textbf{inverse
  of $x$} if and only if $y$ is both a \textit{left inverse} and
\textit{right inverse}.

\subsection{Group}
\label{sec:group}

A \textbf{group} is a \textit{semigroup} with an \textit{identity}
(that is, a \textit{monoid}) in which every element has an
\textit{inverse}.

\begin{itemize}
\item G0: Closure. $ \forall a, b \in G: a \circ b \in G$
\item G1: Associativity. $ \forall a, b, c \in G: a \circ (b \circ c) = (a \circ b) \circ c $
\item G2: Identity. $\exists e in G : \forall a in G: e \circ a = a = a \circ e $
\item G3: Inverse. $\forall a in G: \exists b in G: a \circ b = e = b \circ a$
\end{itemize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Set Theory}
\label{sec:set-theory}

\subsection{Set Equivalence}
\label{sec:set-eq}

Let $S$ and $T$ be sets. Then $S$ and $T$ are \textbf{equivalent} if
and only if there exists a \textbf{bijection} $f : S \to T$ between
the elements of $S$ and those of $T$. That is, if they have the
\textbf{same cardinality}. This can be written as $S \sim T$.

\subsection{Finite Set}
\label{sec:finite-set}

A set $S$ is defined as \textbf{finite} if and only if

\begin{math}
  \begin{array}{c}
    \\
    \exists n \in \mathbb{N}: S \sim \mathbb{N}_{< n}\\
    \\
  \end{array}
\end{math}

where $\sim$ denotes \textit{set equivalence}.

That is, if there exists an element $n$ of the set of natural numbers
$\mathbb{N}$ such that the set of all elements of $\mathbb{N}$ less
than $n$ is equivalent to $S$.

Equivalently, a finite set is a set with a count.

\subsection{Topology}
\label{sec:topology}

Let $S$ be a set such that $S \neq \emptyset$. A \textbf{topology on
  $S$} is a subset $\tau \subseteq \mathcal{P}(S)$ of the power set of
$S$ that satisfies the open set axioms:
\begin{itemize}
\item O1: The union of an arbitrary subset of $\tau$ is an element of
$\tau$
\item O2: The intersection of any two elements of $\tau$ is an element
of $\tau$
\item O3: $S$ is an element of $\tau$
\end{itemize}

If $\tau$ is a topology on $S$, then $(S, \tau)$ is called a
\textit{topological space}. The elements of $\tau$ are called the open
sets of $(S, \tau)$.

\subsection{Open Set}
\label{sec:open-set}

Let $T = (S, \tau)$ be a topological space. Then the elements of
$\tau$ are called the \textbf{open sets of $T$}. Thus, both are
equivalent statements:

\begin{math}
  \begin{array}{l}
    \\
    U \in \tau \\
    U \text{ is open in } T\\
    \\
  \end{array}
\end{math}

\paragraph{Open Set Axioms}
\label{sec:open-set-axioms}

Let $S$ be a set. The \textbf{open set axioms} are the conditions
under which elements of a subset $\tau \in \mathcal{P}(S)$ of the
power set of $S$ need to satisfy in order to be open sets of the
topology $\tau$ on $S$:

\begin{itemize}
\item O1: The union of an arbitrary subset of $\tau$ is an element of
$\tau$
\item O2: The intersection of any two elements of $\tau$ is an element
of $\tau$
\item O3: $S$ is an element of $\tau$
\end{itemize}

\subsection{Closed Set}
\label{sec:closed-set}

Let $T = (S, \tau)$ be a topological space. Let $H \subseteq S$. H is
\textbf{closed (in $T$)} if and only if its complements
$S \setminus H$ is open in $T$.

That is, $H$ is closed if and only if $(S \setminus H) \in \tau$,
i.e., $S \setminus H$ is an element of the topology of $T$.

\paragraph{Closed Set Axioms}

Let $S$ be a set. The \textbf{closed set axioms} are the conditions
under which a subset $F \subseteq \mathcal{P}(S)$ of the power set $S$
consists of the closed sets of a topology on $S$:

\begin{itemize}
\item C1: The intersection of an arbitrary subset of $F$ is an element
  of $F$
\item C2: The union of any two elements of $F$ is an element of $F$
\item C3: $\emptyset$ is an element of $F$
\end{itemize}

\subsection{Upper Set}
\label{sec:upper-set}

Let $(S, \preceq)$ be an ordered set, and $U \subseteq S$. $U$ is an
\textbf{upper set} in $S$ if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall u \in U : \forall s \in S: u \preceq s \implies s \in U\\
    \\
  \end{array}
\end{math}

\subsection{Lower Set}
\label{sec:lower-set}

Let $(S, \preceq)$ be an ordered set, and $L \subseteq S$. $U$ is an
\textbf{lower set} in $S$ if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall l \in L : \forall s \in S: s \preceq l \implies s \in L\\
    \\
  \end{array}
\end{math}

\subsection{Sequence}
\label{sec:sequence}

A \textbf{sequence} is a mapping\ref{sec:mapping} whose domain is a
subset of the set of natural numbers $\mathbb{N}$. It can be seen that
a sequence is an instance of a family of elements indexed by
$\mathbb{N}$.

Informally, a \textbf{sequence} is a set of objects which is listed in
a \textbf{specific order}.

\paragraph{Notation}

If $f : A \to S$ is a \textbf{sequence}, then a symbol (e.g. $a$) is
chosen to represents elements of this sequence. Then, for each
$k \in A$, $f(k)$ is denoted as $a_k$, and $f$ itself is denoted as
$\pair{a_k}_{k \in A}$ or $(a_k)_{k \in A}$.

Any expression can be used to denote the \textit{domain} of $f$ in
place of $k \in A$.

Also, a seqeunce itself may be defined by a simple formula, and so for
example:

\begin{math}
  \begin{array}{c}
    \\
    (k^3)_{2 \leq k \leq 6} \\
    \\
  \end{array}
\end{math}

is the same as :

\begin{math}
  \begin{array}{c}
    \\
    (a_k)_{2 \leq k \leq 6} \text{ where } \forall k \in {2,3, ..., 6}: a_k = k^3
    \\
  \end{array}
\end{math}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Ordering}
\label{sec:ordering}

\paragraph{Definition}

Let $S$ be a set. An \textbf{ordering on} $S$ is a relation
$\mathcal{R}$ on $S$ such that:

\begin{itemize}
\item $\mathcal{R}$ is \textbf{reflexive}\ref{sec:reflexivity}, i.e.,
  $\forall a \in S: a \mathcal{R} a$.
\item $\mathcal{R}$ is \textbf{transitive}\ref{sec:transitivity},
  i.e.,
  $\forall a, b, c \in S: a \mathcal{R} b \land b \mathcal{R} c
  \implies a \mathcal{R} c$.
\item $\mathcal{R}$ is \textbf{antisymmetric}\ref{sec:symmetry}, i.e.,
  $\forall a \in S : a \mathcal{R} b \land b \mathcal{R} a \implies a
  = b$.
\end{itemize}

It is not demanded for an ordering $\preceq$, defined in its most
general form on a set $S$, that \textit{every} pair of elements of $S$
is related by $\preceq$. They may be, or they may not be, depending on
the specific nature of both $S$ and $\preceq$.

If it \textit{is} the case that $\preceq$ is a \textbf{connected
  relation}, that is, that every pair of distinct elements is related
by $\preceq$, then $\preceq$ is called a \textbf{total ordering}.

If it is \textit{not} the case that $\preceq$ is connected, then
$\preceq$ is called a \textbf{partial ordering}.

\paragraph{Notation}

Symbols used to denote a general \textbf{ordering relation} are
usually variants on $\preceq$, $\leq$, and so on.

Thus, $a \preceq b$ can be read as:
\begin{itemize}
\item $a$ \textbf{precedes, or is the same as} $b$.
\item $b$ \textbf{succeeds, or is the same as} $a$.
\end{itemize}

\paragraph{Smaller and Larger}

An \textbf{ordering} can often be considered to be a comparison of the
\textbf{size} of objects, perhaps in some intuitive sense. This is
particularly applicable in the context of numbers. Thus the expression
$A \preceq B$ can in such contexts be interpreted as:
\begin{itemize}
\item $A$ is \textbf{smaller than} $B$
\item $A$ is \textbf{less than} $B$
\item $B$ is \textbf{larger than} $A$
\item $B$ is \textbf{greater than} $A$
\end{itemize}

In natural language, such terms are called \textbf{comparative
  adjectives}, or just \textbf{comparatives}.

Depending on the nature of the set being ordered, and depending on the
nature of the ordering relation, this interpretation of an ordering as
a comparison of size may not be intellectually sustainable.

\subsection{Ordered Set}
\label{sec:ordered-set}

An \textbf{ordered set} is a relational structure $(S, \preceq)$ such
that the relation $\preceq$ is an ordering.

\subsection{Ordered Structure}
\label{sec:ordered-structure}

An \textbf{ordered structure} $(S, \circ, \preceq)$ is a set $S$ such
that:

\begin{itemize}
\item $(S, \circ)$ is an algebraic structure
\item $(S, \preceq)$ is an ordered set
\item $\preceq$ is \textit{compatible}\ref{sec:compatibility} with
  $\circ$
\end{itemize}

\subsection{Monotone}

\paragraph{Increasing}
\label{sec:increasing}

Let $(S, \preceq_1)$ and $(T, \preceq_2)$ be ordered sets. Let
$\phi: S \to T$ be a mapping. Then $phi$ is \textbf{increasing} if and
only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S: x \preceq_1 y \implies \phi(x) \preceq_2 \phi(y) \\
    \\
  \end{array}
\end{math}

Note that this definition also holds if $S = T$.

It is also called as \textbf{order-preserving}, \textbf{isotone}, and
\textbf{non-decreasing}.

\paragraph{Decreasing}
\label{sec:decreasing}

Let $(S, \preceq_1)$ and $(T, \preceq_2)$ be ordered sets. Let
$\phi: S \to T$ be a mapping. Then $phi$ is \textbf{decreasing} if and
only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S: x \preceq_1 y \implies \phi(y) \preceq_2 \phi(x) \\
    \\
  \end{array}
\end{math}

Note that this definition also holds if $S = T$.

It is also called as \textbf{order-inverting},
\textbf{order-reversing}, \textbf{anitone}, and
\textbf{non-increasing}.

\paragraph{Monotone}
\label{sec:monotone}

Let $(S, \preceq_1)$ and $(T, \preceq_2)$ be ordered sets. Let
$\phi: S \to T$ be a mapping. Then $phi$ is \textbf{monotone} if and
only if it is either \textit{increasing} or \textit{decreasing}.

Note that this definition also holds if $S = T$.

\subsection{Upper Bound}
\label{sec:upper-bound}

Let $(S, \preceq)$ be an ordered set. Let $T \subseteq S$. An
\textbf{upper bound for $T$ in $S$} is an element $m \in S$ such that:

\begin{math}
  \begin{array}{c}
    \\
    \forall t \in T : t \preceq m\\
    \\
  \end{array}
\end{math}

That is, $m$ \textit{succeeds} every elements of $T$.

\subsection{Supremum}
\label{sec:supremum}

Let $(S, \preceq)$ be an ordered set. Let $T \subseteq S$. An element
$c \in S$ is the \textbf{supremum of $T$ in $S$} if and only if:

\begin{itemize}
\item $c$ is an \textit{upper bound} of $T$ in $S$
\item $c \preceq d$ for all upper bounds $d$ of $T$ in $S$
\end{itemize}

If there exists a \textbf{supremum} of $T$, we say that:

\begin{itemize}
\item $T$ admits a supremum (in $S$)
\item $T$ has a supremum (in $S$)
\end{itemize}

Particularly in the field of analysis, the supremum of a set $T$ is
often referred to as the \textbf{least upper bound of $T$} and denoted
$\mathtt{lub}(T)$.

\paragraph{Uniqueness of Supremum}

Let $c$ and $c'$ both be suprema of $T$ in $S$. From the definition of
supremum, $c$ and $c'$ are upper bounds of $T$ in $S$.

By that definition:

\begin{itemize}
\item $c$ is an upper bound of $T$ in $S$, and $c'$ is a supremum of
  $T$ in $S$ implies that $c' \preceq c$.
\item $c'$ is an upper bound of $T$ in $S$, and $c$ is supremum of $T$
  in $S$ implies that $c preceq c'$.
\end{itemize}

Therefore,

\begin{math}
  \begin{array}{ll}
    \\
    c' \preceq c \land c \preceq c'\\
    \therefore c = c' & \because \preceq \text{ is antisymmetry}\\
    \\
  \end{array}
\end{math}

Q.E.D.

\subsection{Join}
\label{sec:join}

Let $(S, \preceq)$ be an ordered set. Let $a, b \in S$. Let their
supremum $\mathtt{sup} \{a, b\}$ exist in $S$.

Then the \textbf{join of $a$ and $b$}\footnote{some sources refer to
  this as the \textbf{union} of $a$ and $b$} is defined as:

\begin{math}
  \begin{array}{c}
    \\
    a \vee b = \mathtt{sup} \{ a, b \}\\
    \\
  \end{array}
\end{math}

Expanding the definition of supremum, one sees that $c = a \vee b$ if
and only if:

\begin{math}
  \begin{array}{c}
    \\
    a \preceq c\text{ and }b \preceq c\text{ and }\forall s \in S: a \preceq s \land b \preceq s \implies c \preceq s\\
    \\
  \end{array}
\end{math}

\subsection{Lower Bound}
\label{sec:lower-bound}

Let $(S, \preceq)$ be an ordered set. Let $T \subseteq S$. A
\textbf{lower bound for $T$ in $S$} is an element $m \in S$ such that:

\begin{math}
  \begin{array}{c}
    \\
    \forall t \in T: m \preceq t\\
    \\
  \end{array}
\end{math}

That is, $m$ \textit{precedes} every elements of $T$.

\subsection{Infimum}
\label{sec:infimum}

Let $(S, \preceq)$ be an ordered set. Let $T \subseteq S$. An element
$c \in S$ is the \textbf{infimum of $T$ in $S$} if and only if:

\begin{itemize}
\item $c$ is a \textit{lower bound} of $T$ in $S$
\item $d \preceq c$ for all lower bounds $d$ of $T$ in $S$
\end{itemize}

If there exists an \textbf{infimum} of $T$, we say that:

\begin{itemize}
\item $T$ admits an infimum (in $S$)
\item $T$ has an infimum (in $S$)
\end{itemize}

Particularly in the field of analysis, the infimum of a set $T$ is
often referred to as the \textbf{greatest lower bound of $T$} and
denoted as $\mathtt{glb}(T)$.

\subsection{Meet}
\label{sec:meet}

Let $(S, \preceq)$ be an ordered set. Let $a, b \in S$. Let their
infimum $\mathtt{inf} \{a, b\}$ exist in $S$.

Then the \textbf{meet of $a$ and $b$}\footnote{some sources refer to
  this as the \textbf{intersection} of $a$ and $b$} is defined as:

\begin{math}
  \begin{array}{c}
    \\
    a \wedge b =\mathtt{inf} \{ a, b \}\\
    \\
  \end{array}
\end{math}

Expanding the definition of infimum, one sees that $c = a \wedge b$ if
and only if:

\begin{math}
  \begin{array}{c}
    \\
    c \preceq c\text{ and }c \preceq b\text{ and } \forall s \in S: s \preceq a \land s \preceq b \implies s \preceq c\\
    \\
  \end{array}
\end{math}

\subsection{Semilattice}
\label{sec:semilattice}

A semigroup $(S, \circ)$ is called a \textbf{semilattice} if and only
if $\circ$ is a \textit{commutative} and \textit{idempotent}
operation.

Thus, an algebraic structure is a \textbf{semilattice} if and only if
it satisfies the semilattice axioms:

\begin{itemize}
\item SL0: Closure for $\circ$. $\forall a, b \in S: a \circ b \in S$
\item SL1: Associativity of $\circ$.
  $\forall a, b, c \in S: (a \circ b) \circ c = a \circ (b \circ c)$
\item SL2: Commutativity of $\circ$.
  $\forall a, b \in S: a \circ b = b \circ a$
\item SL3: Idempotence of $\circ$. $\forall a \in S: a \circ a = a$
\end{itemize}

\subsubsection{Join Semilattice}

Let $(S, \vee, \preceq)$ be an ordered structure and $\vee$ is join
operator. Suppose that $\forall a, b \in S: a \vee b \in S$ where
$a \vee b$ is the join of $a$ and $b$ with respect to $\preceq$.

Then the ordered structure $(S, \vee, \preceq)$ is called a
\textbf{join semilattice}.

\subsubsection{Meet Semilattice}

Let $(S, \wedge, \preceq)$ be an ordered structure and $\wedge$ is
meet operator. Suppose that $\forall a, b \in S: a \wedge b \in S$
where $a \wedge b$ is the meet of $a$ and $b$ with respect to
$\preceq$.

Then the ordered structure $(S, \wedge, \preceq)$ is called a
\textbf{meet semilattice}.

\paragraph{Semilattice Induces Ordering}
\label{sec:semilattice-induces-ordering}

Let $(S, \circ)$ be a semlilattice. Let $\preceq$ be the relation on
$S$ defined by $\forall a, b \in S: a \preceq b \iff a \circ b = b$.

Then $\preceq$ is an ordering.

\subparagraph{Proof}

Let's verify that $\preceq$ satisfies the three conditions for an
ordering.

\subparagraph{Reflexivity}

Since $\circ$ is \textit{idempotent},
$\forall a \in S: a \circ a = a$. Hence $a \preceq a$. Thus $\preceq$
is \textit{reflexive}.

\subparagraph{Antisymmetry}

Suppose that $a \preceq b$ and $b \preceq a$. Then from the first
relation: $a \circ b = b$ and from the second: $b \circ a =a$.

Since $\circ$ is \textit{commutative}, it follows that $a = b$. Hence
$\preceq$ is \textit{antisymmetric}.

\subparagraph{Transitivity}

Suppose that $a \preceq b$ and $b \preceq c$.

Then:

\begin{math}
  \begin{array}{lcll}
    a \circ c & = & a \circ (b \circ c) & \because b \preceq c \\
              & = & (a \circ b) \circ c & \because \circ \text{ is associative} \\
              & = & b \circ c & \because a \preceq b \\
              & = & c & \because b \preceq c \\
    \therefore a \preceq c
  \end{array}
\end{math}

Thus, $\preceq$ is \textit{transitive}.

\subsection{Lattice}
\label{sec:lattice}

\paragraph{Definition 1}

Let $(S, \preceq)$ be an ordered set. Suppose that $S$ admits all
finite non-empty suprema and finite non-empty infima. Denote with
$\wedge$ and $\vee$ the join and meet operations on $S$, respectively.

Then the ordered structure $(S, \wedge, \vee, \preceq)$ is called a
\textbf{lattice}.

\paragraph{Definition 2}

Let $(S, \wedge, \vee, \preceq)$ be an ordered structure.

Then $(S, \wedge, \vee, \preceq)$ is called a \textbf{lattice} if and
only if:

\begin{itemize}
\item $(S,\vee, \preceq)$ is a join semilattice
\item $(S,\wedge, \preceq)$ is a meet semilattice
\end{itemize}

\paragraph{Definition 3}

Let $(S, \vee)$ and $(S, \wedge)$ be semilattices on a set $S$.

Suppose that $\vee$ and $\wedge$ satisfy the \textit{absorption laws}, that is,

\begin{itemize}
\item $\forall a, b \in S: a \vee (a \wedge b) = a$
\item $\forall a, b \in S: a \wedge (a \vee b) = a$
\end{itemize}

Let $\preceq$ be the ordering on $S$ defined by:

\begin{math}
  \begin{array}{c}
    \forall a, b \in S: a \preceq b \iff a \vee b = b
  \end{array}
\end{math}

as on Semilattice Induces
Ordering\ref{sec:semilattice-induces-ordering}.

Then the ordered structure $(S, \vee, \wedge, \preceq)$ is called a
\textbf{lattice}.

\subsection{Complete Lattice}
\label{sec:complete-lattice}

Let $(S, \preceq)$ be a lattice. Then, $(S, \preceq)$ is a
\textbf{complete lattice} if and only if:

\begin{math}
  \begin{array}{ll}
    \\
    & \forall T \subseteq S: T \text{ admits both a \textit{supremum} and an \textit{infimum}} \\
    \text{or} & \forall T \subseteq S: \mathtt{inf}T, \mathtt{sup}T \in S \\
    \\
  \end{array}
\end{math}

That is, if and only if all subsets $T$ of $S$ have both a supremum
and an infimum.

Also called as \textbf{complete ordered set}

\subsection{Pre-Ordering}
\label{sec:pre-ordering}

Let $\mathcal{R} \subseteq S \times S$ be a relation on a set
$S$. $\mathcal{R}$ is a \textbf{pre-ordering} if and only if:

\begin{itemize}
\item \textbf{Reflexive}: $\forall a \in S: a \mathcal{R} a $
\item \textbf{Transitive}:
  $\forall a,b,c \in S: a \mathcal{R} b \land b \mathcal{R} c \implies
  a \mathcal{R} c $
\end{itemize}

\paragraph{Pre-Ordered Set}

Let $S$ be a set, and $\precsim$ be a \textbf{preordering} on
$S$. Then the relational structure $(S, \precsim)$ is called a
\textbf{preordered set}.

\paragraph{Partial vs. Total}

Note that this definition of preordering does not demand that
\textit{every} pair of elements of $S$ is related by $\precsim$. The
way we have defined a preordering, they may be, or they may not be,
depending on the context.

If it \textit{is} the case that $\precsim$ is a \textit{connected
  relation}\ref{sec:connected-relation}, i.e., that \textit{every
  pair} of elements is related by $\precsim$, then $\precsim$ is
called a total preordering.

If it is \textit{not} the case that $\precsim$ is connected, then
$\precsim$ is called a partial preordering.

\subsection{Directed Set}
\label{sec:directed-set}

Let $(S, \precsim)$ be a preordered set. Then $(S, \precsim)$ is a
\textbf{directed set} if and only if every pair of elements of $S$ has
an \textbf{upper bound}\ref{sec:upper-bound} in $S$:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S: \exists m \in S: x \precsim m \land y \precsim m \\
    \\
  \end{array}
\end{math}

Also called as a \textbf{directed preorder}, \textbf{filtered set}, or
\textbf{upward directed set}.

\paragraph{Directed Subset}
\label{sec:directed-subset}

Let $(S, \precsim)$ be a preordered set. Let $H$ be a non-empty subset
of $S$. Then $H$ is a \textbf{directed subset} of $S$ if and only if:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in H: \exists m \in H: x \precsim m \land y \precsim m\\
    \\
  \end{array}
\end{math}

\subsection{Well-Ordering}
\label{sec:well-ordering}

Let $(S, \preceq)$ be an ordered set.

The ordering $\preceq$ is a \textbf{well-ordering} on $S$ if and only
if \textbf{every} non-empty subset of $S$ has a smallest element under
$\preceq$. That is,

\begin{math}
  \begin{array}{c}
    \\
    \forall T \subseteq S: \exists a \in T : \forall x \in T : a \preceq x
    \\
  \end{array}
\end{math}

\subsection{Strict Ordering}
\label{sec:strict-ordering}

Let $\mathcal{R}$ be a relation on a set $S$. Then $\mathcal{R}$ is a
\textbf{strict ordering} on $S$ if and only if:

\subsubsection{Definition 1}

\begin{itemize}
\item \textbf{Asymmetry}:
  $\forall a, b \in S : a \mathcal{R} b \implies \neg b \mathcal{R} a$
\item \textbf{Transitivity}:
  $\forall a, b, c \in S: a \mathcal{R} b \land b \mathcal{R} c
  \implies a \mathcal{R} c$
\end{itemize}

\subsubsection{Definition 2}

\begin{itemize}
\item \textbf{Antireflexivity}:
  $\forall a \in S: \neg (a \mathcal{R} a) $
\item \textbf{Transitivity}:
  $\forall a, b, c \in S: a \mathcal{R} b \land b \mathcal{R} c
  \implies a \mathcal{R} c$
\end{itemize}

Symbols used to denote a general strict ordering are usually variants
on $\prec$, $<$, and so on.

\subsection{Total Ordering}
\label{sec:total-ordering}

Let $\mathcal{R} \subseteq S \times S$ be a relation on a set $S$.

$\mathcal{R}$ is a \textbf{total ordering} on $S$ if and only if:

\begin{itemize}
\item $\mathcal{R}$ is an ordering\ref{sec:ordering} on $S$
\item $\mathcal{R}$ is connected\ref{sec:connected-relation}
\end{itemize}

That is, $\mathcal{R}$ is an ordering with no non-comparable pairs:

\begin{math}
  \begin{array}{c}
    \\
    \forall x, y \in S: x \mathcal{R} y \lor y \mathcal{R} x\\
    \\
  \end{array}
\end{math}

Also called as a \textbf{linear ordering}, or a \textbf{simple
  ordering}.

If it is necessary to emphasises that a total ordering is \textbf{not
  strict}, then the term \textbf{weak total ordering} may be used.

\subsubsection{Chain}
\label{sec:chain}

Let $(S, \preceq)$ be an ordered set. A \textbf{chain in $S$} is a
totally ordered subset of $S$. Thus, a totally ordered set is itself a
chain in its own right.

\subsection{Partial Ordering}
\label{sec:partial-ordering}

Let $(S, \preceq)$ be an ordered set. Then the ordering $\preceq$ is a
\textbf{partial ordering} on $S$ if and only if $\preceq$ is
\textbf{not connected}\ref{sec:connected-relation}.

That is, if and only if $(S, \preceq)$ has at least one pair which is
non-comparable:

\begin{math}
  \begin{array}{c}
    \\
    \exists x, y \in S : x \npreceq y \land y \npreceq x \\
    \\
  \end{array}
\end{math}

It it is necessary to emphasizes that a partial ordering is
\textbf{not strict}\ref{sec:strict-ordering}, then the term
\textbf{weak partial ordering} may be used.

\subsection{Partially Ordered Set}
\label{sec:poset}

A \textbf{partially ordered set} is a relational structure
$(S, \preceq)$ such that $\preceq$ is a partial ordering.

The partially ordered set $(S, \preceq)$ is said to be
\textbf{partially ordered by $\preceq$}.

\subsection{Complete Partial Ordering}
\label{sec:cpo}

A \textbf{complete partial order} abbreviated \textbf{cpo} can,
depending on context, refer to any of the following concepts:

\begin{itemize}
\item A partially ordered set is a \textbf{directed-complete partial
    order (dcpo)} if each of its \textit{directed
    subsets}\ref{sec:directed-subset} has a supremum.
\item A partially ordered set is a \textbf{point directed-complete
    partial order} if it is a dcpo with a \textit{least element}.
\item A partially ordered set is a \textbf{$\omega$-complete partial
    order ($\omega$-cpo)} if it is a poset in which every
  $\omega$-chain $(x_1 \leq x_2 \leq x_3 \leq ...)$ has a supremum
  that belongs to the underlying set of the poset. Every dcpo is an
  $\omega$-cop, since every $\omega$-chain is a directed set, but the
  converse is not true.
\end{itemize}

\paragraph{Properties}

An ordered set $P$ is a pointed dcpo if and only if every chain has a
supremum in $P$, i.e., $P$ is chain-complete. Alternatively, an
ordered set $P$ is an pointed dcpo if and only if every
order-preserving self-map of $P$ has a least fixpoint. Every set $S$
can be turned into a pointed dcpo by adding a least element $\bot$ and
introducing a flat order with $\bot \leq s$ and $s \leq s$ for every
$s \in S$ and no other order relations.

\subsubsection{(Scott) Continuous}
\label{sec:scott-continuous}

A function $f$ between two dcpos $P$ and $Q$ is called \textbf{(Scott)
  continuous} if it maps directed sets to directed sets while
preserving their suprema:

\begin{itemize}
\item $f(D) \subseteq Q$ is directed for every directed $D \subseteq P$
\item $f(\mathtt{sup}D) = \mathtt{sup} f(D)$ for every directed $D subseteq P$
\end{itemize}

Note that every continuous function between dcpos is a
\textit{monotone function}\ref{sec:monotone}.

\subsubsection{Kleene Fixpoint Theorem}
\label{sec:fixpoint-theorem}

Suppose $(L, \sle)$ is directed-complete partial order with a least
element, and $f: L \to L$ be a Scott-continuous (and therefore
monotone) function. Then $f$ has a least fixed point, which is the
supremum of the ascending Kleene chain of $f$.

The \textbf{ascending Kleene chain} of $f$ is the chain

\begin{math}
  \begin{array}{c}
    \\
    \bot \sle f(\bot) \sle f(f(\bot)) \sle ... \sle f^n(\bot) \sle ...\\
    \\
  \end{array}
\end{math}

obtained by iterating $f$ on the least element $\bot$ of $L$. The
theorem states that

\begin{math}
  \begin{array}{c}
    \\
    \mathtt{lfp}(f) = \mathtt{sup}( \{ f^n(\bot) | n \in \mathbb{N}\})\\
    \\
  \end{array}
\end{math}

where $\mathtt{lfp}$ denotes the least fixed point.

Every \textit{order-preserving}\ref{sec:monotone}
\textit{self-map}\ref{sec:self-map} $f$ of a cpo $(P, \bot)$ has a
least fixpoint. If $f$ is continuous, then this fixpoint is equal to
the supremum of the iterates $(\bot, f(\bot), f(f(\bot)), ...)$ of
$\bot$.

\subsection{Linear Extension}
\label{sec:linear-extension}

Given any partial orders $\preceq$ and $\preceq^*$ on a set $X$,
$\preceq^*$ is a \textbf{linear extension of $\preceq$} if and only if:

\begin{itemize}
\item $\preceq^*$ is a \textit{total order}\ref{sec:total-ordering}
\item $\forall x, y in X: x \preceq y \implies x \preceq^* y$
\end{itemize}

It is that second property that leads to describe $\preceq^*$ as
\textbf{extending} $\preceq$.

\subsection{Weak Topological Ordering}
\label{sec:wto}

\footnote{This origiates from
  https://en.wikipedia.org/wiki/Topological\_sorting}

A \textbf{topological sort}, or \textbf{(weak) topological ordering}
of a directed graph is a \textbf{linear
  ordering}\ref{sec:total-ordering} of its vertices such that for
every directed edge $uv$ from $u$ to $v$, $u$ comes before $v$ in the
ordering.

For example, the vertices may represent \textit{tasks} to be
performed, and the edges may represent \textit{constrains} that one
task must be performed before another; in this application, a
\textit{topological ordering} is just a \textbf{valid sequence} for
the tasks.

A topological ordering is possible if and only if the graph has
\textbf{no directed cycles}, that is, if it is a directed acyclic
graph (DAG). Any DAG has at least one topological ordering, and
algorithms are known for constructing a topological ordering of any
DAG in linear time.

Topological orderings are closely related to the concept of a
\textit{linear extension}\ref{sec:linear-extension} of a
\textit{partial order}\ref{sec:partial-ordering}.

A partially ordered set is just a set of objects together with a
definition of $\preceq$ inequality relation, satisfying the axioms of
reflexivity\ref{sec:reflexivity}, antisymmetry\ref{sec:symmetry}, and
transitivity\ref{sec:transitivity}. A total order is a partial order
with connexivity. Total orders are familiar in computer science as the
comparison operators needed to perform comparison sorting
algorithms. For finite sets, total orders may be identified with
linear sequences of objects, where $\preceq$ relation is \texttt{true}
whenever the first object precedes the second object in the order; a
comparison sorting algorithm may be used to convert a total order into
a sequence in this way. A linear extension of a partial order is a
total order that is compatible with if $x \preceq y$ in the partial
order, then $x \preceq y$ in the total order as well.

One can define a partial ordering from any DAG by letting the set of
objects be the vertices of the DAG, and defining $x \preceq y$ to be
true for any two vertices $x$ and $y$ whenever there exists a directed
path from $x \to y$; that is, whenever $y$ is \textit{reachable} from
$x$. With these definitions, a topological ordering of the DAG is the
same thing as a \textbf{linear extension of this partial
  order}. Conversely, any partial ordering on a finite set may be
defined as the \textit{reachability relation} in a DAG. One way of
doing this is to define a DAG that has a vertex for every object in
the partially ordered set, and an edge $xy$ for every pair of objects
for which $x \preceq y$. An alternative way of doing this is to use
\textit{transitive reduction} of the partial ordering; in general,
this produces DAGs with fewer edges, but the reachability relation in
these DAGs is still the same partial order. By using these
constructions, one can use topological ordering algorithms to find
linear extensions of partial orders.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage

\section{Graph Theory}
\label{sec:graph-theory}

\subsection{Directed Graph}
\label{sec:digraph}

A \textbf{directed graph}, or just \textbf{digraph} $D$ is a non-empty
set $V$ together with an \emph{antireflexive
  relation}\ref{sec:reflexivity} $E$ on $V$. The elements of $E$ are
the \textbf{arcs}\ref{sec:arc}.

\subsubsection{Arc}
\label{sec:arc}

Let $G = (V, E)$ be a digraph. The \textbf{arcs} are the elements of
$E$. If $e \in E$ is an \textbf{arc} joining the vertex $u$ to the
vertex $v$, it is denoted as $uv$.

\subsection{Directed Walk}
\label{sec:directed-walk}

Let $G = (V, A)$ be a directed graph. A \textbf{directed walk} in $G$
is a finite or infinite \textit{sequence}\ref{sec:sequence} $\pair{x_k}$
such that:

\begin{math}
  \begin{array}{c}
    \\
    \forall k \in \mathbb{N}: k+1 \in \mathtt{Dom}( \pair{x_k} ): (x_k, x_{k+1} ) \in A \\
    \\
  \end{array}
\end{math}

\subsection{Reachability}
\label{sec:reachability}

Let $G = (V, A)$ be a directed graph.

\paragraph{Definition 1}

Let $u, v \in V$. The $v$ is \textbf{reachable} from $u$ if and only
if there exists a \textit{directed walk} from $u$ to $v$.

\paragraph{Definition 2}

The \textbf{reachability relation} of $G$ is the \textit{transitive
  closure}\ref{sec:transitive-closure} of $A$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newpage
\section{Soundness and Completeness}

\subsection{Soundness}

It is called \textbf{sound} if an analysis for program \textsl{P} says
that it satisfies property \textsl{S}, then the program will truly
satisfy that property.

\textbf{Sound} but \textbf{incomplete} analysis have \textbf{false
  positive}. In other words, it does \textbf{not prove} programs that
satisfy the property.

\begin{figure}[h]
  \includegraphics[width=\textwidth]{sound}
  \caption{Sound Analysis}
  \label{fig:sound}
\end{figure}

Figure \ref{fig:sound} shows a sound analysis (the blue area). It
proves correctly for the programs $ p_1 $ and $ p_2 $ that satisfy the
specification ($S(P)$). However, since this analysis is
\textit{incomplete}, it has \textbf{false positives}: it \textbf{does
  not prove} for programs $ p_3 $ and $ p_4 $ that satisfy $S$. In
other words, it proves that $ p_3 $ and $ p_4 $ satisfy $ \neg S(P) $
by emiting \textbf{alarms}, even though they are actually satisfy
$ S(P) $. False positives are also called as \textbf{false alarms}.

\subsection{Completeness}

An analysis is called \textbf{complete} when a program satisfies a
property \textsl{S}, the analysis for that program says that it will
satisfy that property.

\textbf{Complete} but \textbf{unsound} analysis have \textbf{false
  negative}. In other words, it \textbf{wrongly proves} programs that
does not satisfy the property.

\begin{figure}[h]
  \includegraphics[width=\textwidth]{complete}
  \caption{Complete Analysis}
  \label{fig:complete}
\end{figure}

Figure \ref{fig:complete} shows a complete analysis (also the blue
area). It proves all the programs. In other words, it \textbf{wrongly
  proves} for programs $ p_5 $ and $ p_6 $ that actually do not
satisfy $ S(P) $ by accepting those programs (i.e., emitting no
alarms), thus it is \textit{unsound}. Those programs are \textbf{false
  negatives}.

The table \ref{tab:summary} shows the summary of soundness and
completeness.

\begin{table}[ht]
  \centering
  \caption{Sound \& Complete Summary}
  \label{tab:summary}

  \begin{tabular}[t]{l>{\raggedright}p{0.3\linewidth}>{\raggedright\arraybackslash}p{0.3\linewidth}}
    \hline
    & $ S(P) $ & $ \neg S(P) $ \\
    \hline
    Prove \texttt{(accept)} & \textbf{True negative} (\textsl{correct inference}) & False negative \\
    Not prove \texttt{(reject, alarm)} & False positive & \textbf{True positive} (\textsl{correct inference}) \\
    \hline
  \end{tabular}
\end{table}%


\section{Full proofs}

\begin{lemma}[Semantic security]
\end{lemma}

\begin{proof}
    We will prove that if the scheme is not semantically secure, then it is
    necessarily not reflection secure. Assume that the scheme is not
    semantically secure. Then there will exist a PPT adversary $\mathcal{A}$
    such that for all PPT simulators $\mathcal{S}$ we have that $\mathcal{A}$
    has non-negligible advantage to $\mathcal{S}$ in the semantic security game.

    We will now construct an adversary $\mathcal{A'}$ for the reflection
    security game. $\mathcal{A'}$ operates as follows: It makes one query to
    the reflection oracle setting $r_0 = \epsilon$ and receives a response $c_0
    = K_k(s, r_0) = K_k(s) = c$. It then passes that $c$ to the semantic
    security adversary $\mathcal{A}$. It answers all encryption and decryption
    queries of the semantic security adversary by relaying them to its own
    encryption and decryption oracle. Finally, when the semantic security
    adversary outputs $y = y'$, this $y'$ is output by the reflection security
    adversary. Note that $\mathcal{A}$ cannot distinguish whether they are
    playing against the actual semantic security game or being simulated by the
    reflection security adversary, as their view is identical. Therefore:

    \begin{equation}
        Pr[Game_{REF-SEC}^{\mathcal{A'}} = 1] = Pr[Game_{SEM-SEC}^{\mathcal{A}} = 1]
    \end{equation}

    It remains to prove that $\mathcal{A'}$ has significant advantage against
    any reflection simulator $\mathcal{S'}$. Indeed let $\mathcal{S'}$ be any
    reflection game simulator. We will construct a simulator $\mathcal{S}$ for
    the semantic security game. Initially, the simulator $\mathcal{S}$ receives
    the length of the ciphertext $|c|$. They then simulate the reflection game
    simulator $\mathcal{S'}$ as follows. Upon receiving query $r_i$ from the
    reflection game simulator, they answer with $|c| + |r_i|$. When the
    reflection security adversary outputs $y' = y$, this $y$ is output by the
    semantic security simulator. We observe that the reflection game simulator
    $\mathcal{S'}$ cannot distinguish whether they are playing against the
    actual simulated reflection game or being simulated by the semantic
    security simulator. To see this, note that from the length preservation
    assumption we have that $|c| + |r_i| = |K(s, r_i)| = |K(s', r_i)| =
    |K(0^{|s|}, r_i)|$. Therefore:

    \begin{equation}
        Pr[Game_{SEM-SIM}^{\mathcal{S}} = 1] = Pr[Game_{REF-SIM}^{\mathcal{S'}} = 1]
    \end{equation}

    From the assumption that the scheme is not semantically insecure, we know
    that:

    \begin{align*}
        |Pr[Game_{SEM-SEC}^{\mathcal{A}} = 1] - Pr[Game_{SEM-SIM}^{\mathcal{S}} = 1]| =\\
        Adv_{\mathcal{A}, \mathcal{S}} = \text{non-negl}
    \end{align*}

    And therefore, replacing both probabilities with their equals:

    \begin{align*}
        |Pr[Game_{REF-SEC}^{\mathcal{A'}} = 1] -
        Pr[Game_{REF-SIM}^{\mathcal{S'}} = 1]| =\\
        Adv_{\mathcal{A'}, \mathcal{S'}} = \text{non-negl}
    \end{align*}
\end{proof}

\begin{lemma}[Good compression is detectable]
\end{lemma}

\begin{proof}
We model our plaintext input to the compression function as the usual pair $(s,
r)$ consisting of the secret string $s$ and reflection string $r$. When $(s, r)$
are encoded by a compression function $\mathcal{K}$ ideal under some plaintext
distribution $\bar{\mathcal{M}}$, a simple predicate allows the distinction
between two secrets $s_1$ and $s_2$ using a reflection pair $(r_1, r_2)$.  We
make the assumption that the resulting reflection pair $(r_1, r_2)$ is
efficiently computable.

\begin{align*}
    \Pr[r = r_1|s = s_1] < \Pr[r = r_2|s = s_1]&\land\\
    \Pr[r = r_1|s = s_2] > \Pr[r = r_2|s = s_2]&
\end{align*}

Using the fact that $\mathcal{K}$ is ideal with respect to this distribution,
we deduce that:

\begin{align*}
    |\mathcal{K}(s_1, r_1)| > |\mathcal{K}(s_1, r_2)|&\land\\
    |\mathcal{K}(s_2, r_1)| < |\mathcal{K}(s_2, r_2)|&
\end{align*}

We then define $Q(s)$ to be the predicate ``$s = s_1$". This partitions
$\mathcal{M}$ into the distributions $\mathcal{M}_Q$ and
$\mathcal{M}_{\lnot Q}$ both of which are non-empty, as $\mathcal{M}_Q$
contains $s_1$ and $\mathcal{M}_{\lnot Q}$ contains $s_2$. From the fact that
$Q$ is not trivial, we deduce that $\pi < 1$.

Observe, then, that letting $\bar{r} = (r_1, r_2)$ we obtain:

\begin{align*}
    \Pr[cpr^Q_{\mathcal{K}}(s_1, \bar{r}, \mathcal{K}, \mathcal{M})
    \land
    cpr^Q_{\mathcal{K}}(s_2, \bar{r}, \mathcal{K}, \mathcal{M})] = 1
\end{align*}

This completes the proof.

\end{proof}

\begin{lemma}[Compression attack]
\end{lemma}

\begin{proof}

Let $g$ be the boolean function $Q$ on the plaintext. Define the adversary
$\mathcal{A}$ as follows:

\begin{lstlisting}[texcl,mathescape,basicstyle=\small]
def $\mathcal{A}(1^\lambda)$:
    $(r_1, r_2) \leftarrow \mathcal{O}_R(1^\lambda)$

    $l_1 = |\text{Reflect}^{k}_s(r_1)|$
    $l_2 = |\text{Reflect}^{k}_s(r_2)|$

    if $l_1 < l_2$:
        return True
    else:
        return False
\end{lstlisting}

Let $\mathcal{S}$ be an arbitrary simulator. Then we have:
\begin{align*}
    \Pr[\text{Game}_{\text{REF-SIM}}^{\mathcal{SE},\mathcal{S}}
        (\lambda) = 1] &=\\
    \Pr_{x \leftarrow \mathcal{M}, b \leftarrow \mathcal{S}(1^\lambda)}
        [Q(x) = b]
\end{align*}

Letting the random variables $b$ and $x$:
\begin{align*}
    x &\leftarrow \mathcal{M}\\
    b &\leftarrow \mathcal{S}(1^\lambda)
\end{align*}

Due to the independence of the simulator's output with the choice of $x$ we have:
\begin{align*}
    Pr[b = Q(x)] &=\\
    Pr[\lnot b|\lnot Q(x)]Pr[\lnot Q(x)] + Pr[b|Q(x)]Pr[Q(x)] &=\\
    Pr[\lnot b]Pr[\lnot Q(x)] + Pr[b]Pr[Q(x)] &=\\
    (1 - Pr[b])(1 - Pr[Q]) + Pr[b]Pr[Q(x)] &=\\
    1 - Pr[Q(x)] - Pr[b] + 2Pr[b]Pr[Q(x)]
\end{align*}

For a given $\Pr[Q]$, this function is monotonic in $\Pr[b]$ and therefore has
potential extrema at $\Pr[b] = 0$ or $\Pr[b] = 1$, for which cases the function
takes the values $1 - \Pr[Q(x)]$ and $\Pr[Q(x)]$ respectively. Therefore, the
maximum $\Pr[b]$ of the simulator is:
\begin{equation*}
    \Pr[Q(x) = b] = max(\Pr[Q(x)], 1 - \Pr[Q(x)]) = \pi
\end{equation*}

And therefore:
\begin{align*}
    \forall \mathcal{S}:\\
    \Pr[
        \text{Game}_{\text{REF-SIM}}^{\mathcal{SE},\mathcal{S}}
        (\lambda) = 1
    ]
    \leq\\
    max(Pr[Q(x)], 1 - Pr[Q(x)])
\end{align*}

From the compression detectability of Q we know that:
\begin{align*}
    \exists \alpha \text{ non-negl}:\\
    \Pr_{s_1 \leftarrow \mathcal{M}_Q,
         s_2 \leftarrow \mathcal{M}_{\lnot Q}}
         [cpr^Q_{\kappa}(s_1, \overbar{r}) \land
          cpr^Q_{\kappa}(s_2, \overbar{r})]
    \geq\\
    \pi + \alpha(\lambda)
\end{align*}

Therefore:
\begin{align*}
    \Pr_{s_1 \leftarrow \mathcal{M}_Q}
         [cpr^Q_{\kappa}(s_1, \overbar{r})]
    \geq
    \pi + \alpha(\lambda) \land\\
    \Pr_{s_2 \leftarrow \mathcal{M}_{\lnot Q}}
         [cpr^Q_{\kappa}(s_2, \overbar{r})]
    \geq
    \pi + \alpha(\lambda)
\end{align*}

And so:
\begin{align*}
    \Pr_{s \leftarrow \mathcal{M}}
         [cpr^Q_{\kappa}(s, \overbar{r})]
    =\\
    \Pr[cpr^Q_{\kappa}(s, \overbar{r})|Q(s)]\Pr[Q(s)]
    +\\
    \Pr[cpr^Q_{\kappa}(s, \overbar{r})|\lnot Q(s)]\Pr[\lnot Q(s)]
    \geq\\
    (\pi + \alpha(\lambda))(\Pr[Q(s)] + (1 - \Pr[Q(s)))
    =\\
    \pi + \alpha(\lambda)
\end{align*}

Let us now examine the event of $\mathcal{A}$ being successful, denoted Succ,
when $cpr^Q_{\kappa}(s, \overbar{r})$. Assuming $Q(s)$:
\begin{align*}
    \Pr[|\mathcal{K}(s, r_1)| < |\mathcal{K}(s, r_2)||Q(s)]
    = \pi + \alpha(\lambda)
\end{align*}

And from the strict length monotonicity of $\textrm{Enc}$ it follows that:
\begin{align*}
    \Pr[&|\textrm{Enc}(\mathcal{K}(s, r_1))| <\\&|\textrm{Enc}(\mathcal{K}(s, r_2))||Q(s)]
        \geq \pi + \alpha(\lambda)\\
    \Rightarrow \Pr[&
        |\text{Reflect}^{k}_s(r_1)|
        <
        |\text{Reflect}^{k}_s(r_2)||Q(s)
    ]
        \geq \pi + \alpha(\lambda)\\
    \Rightarrow \Pr[&l_1 < l_2|Q(s)]
        \geq \pi + \alpha(\lambda)\\
    \Rightarrow \Pr[&\text{Succ}|Q(s)]
        \geq \pi + \alpha(\lambda)\\
\end{align*}

The case for $\lnot Q(s)$ is the same, but with a different inequality direction:
\begin{align*}
    \Pr[|\mathcal{K}(s, r_1)| > |\mathcal{K}(s, r_2)||\lnot Q(s)]
        \geq\\
        \pi + \alpha(\lambda)\\
    \Rightarrow \Pr[\text{Succ}|\lnot Q(s)] \geq\\
    \pi + \alpha(\lambda)\\
\end{align*}

And so the probability of success is given:
\begin{align*}
    \Pr[\text{Succ}] =\\
    \Pr[\text{Succ}|Q(s)]\Pr[Q(s)]
    +
    \Pr[\text{Succ}|\lnot Q(s)]\Pr[\lnot Q(s)] \geq\\
    \pi + \alpha(\lambda)
\end{align*}

Therefore,
\begin{align*}
    \forall PPT \mathcal{S}:
    \text{Adv}_{\mathcal{SE}(\textrm{Enc}, \textrm{Com}), \mathcal{A}, \mathcal{S}}
        (1^\lambda)
    \geq\\
    |\pi + \alpha(\lambda) - \pi| = \alpha(\lambda)
\end{align*}

Which is non-negligible.

\end{proof}

\begin{lemma}[Amplification]
\end{lemma}

\begin{proof}

From the fact that $Q$ is compression-detectable and from the Compression Attack Theorem, we have that:
\begin{align*}
    \Pr_{s \leftarrow \mathcal{M}}
         [cpr^Q_{\kappa}(s, \overbar{r})]
    =\\
    \pi + \alpha(\lambda)
\end{align*}

Some elements $s \in \mathcal{M}$ allow for better compression detectability than others under the fixed
reflection vector $\overbar{r}$. Call these elements \textit{amplifiable} and define predicate:
\begin{align*}
    Amp(s) \defeq
    \Pr[cpr^Q_{\kappa}
     (s, \overbar{r})]
    \geq
    \frac{1}{2} + \frac{\alpha(\lambda)}{2}
\end{align*}

We will now obtain a lower bound on the probability of an element being amplifiable.

Let:

% B derivation: (where \beta(\lambda) = \alpha(\lambda) / 2)
% B + (\frac{1}{2} + \alpha(\lambda)/2)(1 - B) = \pi + \alpha(\lambda)
% B + \frac{1}{2} + \alpha(\lambda)/2 - B(\frac{1}{2} + \beta(\lambda)) = \pi + \alpha(\lambda)
% \frac{1}{2} + \beta(\lambda) + B(\frac{1}{2} - \beta(\lambda)) = \pi + \alpha(\lambda)
% B(\frac{1}{2} - \beta(\lambda)) = \pi + \alpha(\lambda) - \frac{1}{2} - \beta(\lambda)
% B = (\pi + \alpha(\lambda) - \frac{1}{2} - \beta(\lambda)) / (\frac{1}{2} - \beta(\lambda))
% B = (\pi + \alpha(\lambda) - \frac{1}{2} - \frac{\alpha(\lambda)}{2}) / (\frac{1}{2} - \frac{\alpha(\lambda)}{2})
% B = (\pi - \frac{1}{2} + \frac{\alpha(\lambda)}{2}) / (\frac{1}{2} - \frac{\alpha(\lambda)}{2})
% B = \pi / (\frac{1}{2} - \frac{\alpha(\lambda)}{2}) - (\frac{1}{2} - \frac{\alpha(\lambda)}{2}) / (\frac{1}{2} - \frac{\alpha(\lambda)}{2})
% B = \pi / (\frac{1}{2} - \frac{\alpha(\lambda)}{2}) - 1
\begin{align*}
    B = \frac{\pi}{\frac{1}{2} - \frac{\alpha(\lambda)}{2}} - 1
\end{align*}

$B$ is non-negligible in $\lambda$.

Assume, for the sake of contradiction, that:
\begin{align*}
    \Pr_{s \leftarrow \mathcal{M}}
    [Amp(s)] < B
\end{align*}

Then we have:
\begin{align*}
    \Pr_{s \leftarrow \mathcal{M}}
         [cpr^Q_{\kappa}(s, \overbar{r})]
    =\\
    \Pr_{s \leftarrow \mathcal{M}}
         [cpr^Q_{\kappa}(s, \overbar{r})|Amp(s)]\Pr[Amp(s)]
    +\\
    \Pr_{s \leftarrow \mathcal{M}}
         [cpr^Q_{\kappa}(s, \overbar{r})|\lnot Amp(s)]\Pr[\lnot Amp(s)]
    <\\
    B + (\frac{1}{2} + \frac{\alpha}{2})(1 - B)
\end{align*}

But then:
\begin{align*}
    B + (\frac{1}{2} + \frac{\alpha(\lambda)}{2})(1 - B) =
    \pi + \alpha(\lambda)
\end{align*}

And this contradicts the assumption that $Q$ is compression-detectable. Therefore:
\begin{align*}
    \Pr_{s \leftarrow \mathcal{M}}
    [Amp(s)] \geq B
\end{align*}

It remains to show that the advantage of the adversary can be
arbitrarily large, i.e. that for some negligible $C$ we have:
\begin{align*}
    \text{Adv}_{\mathcal{SE}(\textrm{Enc}, \textrm{Com}), \mathcal{A}, \mathcal{S}_{Amp}}
    (1^\lambda) = 1 - \pi - C
\end{align*}

Indeed, observe that the amplifying adversary performs a repeated Bernoulli
trial with $k$ repetitions and extracts a majority. Let $X$ be the number of
repetitions that are successful for the adversary. $X$ is defined:
\begin{align*}
    X \defeq |\{ i: cpr^Q_{\kappa}(s, \overbar{r}) \}|
\end{align*}

$X$ follows the binomial distribution, and therefore its expected value is:
\begin{align*}
    E[X] = \frac{k}{2} + \frac{\alpha k}{2}
\end{align*}

Let Succ denote the event of the amplified adversary succeeding. Then Succ
is equivalent to $X > \frac{k}{2}$.

Because $\alpha$ is non-negligible and due to the tail bounds of the binomial
distribution, we have:
\begin{align*}
    Pr[Succ] = 1 - C(k)
\end{align*}

Where $C$ is a negligible function.
\end{proof}

\begin{lemma}[Reflection-independent security]
    Let $E$ be a length-preserving semantically secure encryption schema and
    $Com$ be any function. Then $E(s)$ is reflection secure.
\end{lemma}

\begin{proof}
    Let $\mathcal{A}$ be a reflection security adversary for $E(s)$. We will
    construct a semantic security adversary $\mathcal{A'}$ for $E$. Our semantic
    security adversary will break semantic security for \textit{multiple
    messages}. Let $\mathcal{M}$ be the distribution that $\mathcal{A}$
    attacks. Because $\mathcal{A}$ is a probabilistic polynomial-time
    adversary, there is a polynomial bound $t(\lambda)$ for its execution time.
    The number of reflection queries conducted by $\mathcal{A}$ is then also
    bound by $t(\lambda)$.

    Define $\mathcal{M'}$ by the following procedure: First, $s$ is chosen
    out of the distribution $\mathcal{M}$. Then that same $s$ is repeated
    $t(\lambda)$ times to obtain the vector of plaintexts $\overbar{s}$.

    Upon execution, $\mathcal{A'}$ receives from its challenger the encrypted
    vector $\overbar{c}$ where the same plaintext has been independently
    encrypted $t(\lambda)$ times.
    $\mathcal{A'}$ answers encryption and decryption queries of $\mathcal{A}$
    by forwarding them to its own encryption and
    decryption oracles. For reflection queries, because the schema trivially
    ignores $r$ since $E(s)$ is independent of $r$, the semantic security
    adversary is able to answer each query $r_i$ with $c_i$.

    Clearly, the view of the simulated reflection adversary is the same as if
    it were run in an actual reflection game. Therefore we have:

    \begin{align*}
        Pr[\text{Game}_{\text{REF-SEC}}^{\mathcal{A}}(1^{\lambda})
        = 1]
        =
        Pr[\text{Game}_{\text{SEM-SEC}}^{\mathcal{A'}}(1^{\lambda})
        = 1]
    \end{align*}

    We now prove that the adversary $\mathcal{A'}$ obtains a non-negligible gap
    against any semantic security simulator $\mathcal{S'}$. Indeed let
    $\mathcal{S'}$ be any semantic security simulator. We will construct a
    reflection security simulator $\mathcal{S}$. $\mathcal{S}$ works as
    follows: It obtains $|Com(s)|$ from its challenger which it ignores. It
    then issues a single reflection query $r_1 = \epsilon$, to which the
    challenger responds with $c = E(s')$. But $|c| = |E(s')| = |s'| =
    |0^{|s|}| = |s| = |E(s)| = |c|$ due to length preservation. This $|c|$ is
    then used to start the semantic security simulator.

    The view of the simulated semantic simulator is the same as if it were run
    in an actual semantic simulation game. Therefore we have:

    \begin{align*}
        Pr[\text{Game}_{\text{REF-SIM}}^{\mathcal{S}}(1^{\lambda})
        = 1]
        =
        Pr[\text{Game}_{\text{SEM-SIM}}^{\mathcal{S'}}(1^{\lambda})
        = 1]
    \end{align*}

    Because there exists a non-negligible advantage between $\mathcal{A}$ and
    any simulator $\mathcal{S}$, this gap will also exist between the
    constructed simulator $\mathcal{S}$. Therefore, we have:

    \begin{align*}
        \text{non-negl} = \text{Adv}_{\text{REF} \mathcal{A}, \mathcal{S}}\\
        =
        Pr[\text{Game}_{\text{REF-SEC}}^{\mathcal{A}}
        = 1] -
        Pr[\text{Game}_{\text{REF-SIM}}^{\mathcal{S}}
        = 1]\\
        =
        Pr[\text{Game}_{\text{SEM-SEC}}^{\mathcal{A'}}
        = 1]
        -
        Pr[\text{Game}_{\text{SEM-SIM}}^{\mathcal{S'}}
        = 1]\\
        =
        \text{Adv}_{\text{SEM} \mathcal{A'}, \mathcal{S'}}
    \end{align*}

    For clarity, we have omitted the security parameters from the games.
    This completes the proof.
\end{proof}

\begin{lemma}
    Under the above assumptions, $E(s) || r$ is reflection secure.
\end{lemma}

\begin{proof}
    The construction is identical to the previous proof. However, the
    reflection queries of the reflection adversary $\mathcal{A}$ must be
    answered correctly. As $r_i$ is known to the semantic security adversary,
    this can be done in the obvious way by setting $c_i' = c_i || r_i$.

    The reflection security simulator is not affected, as it receives $|c| =
    |Enc(s) || \epsilon| = |Enc(s)|$.
\end{proof}

\begin{lemma}
    Under the above assumptions and letting $Com$ be a deterministic
    efficiently computable function, $E(s)$ $||$ $Com(r)$ is reflection secure.
\end{lemma}

\begin{proof}
    The construction is the same as before. Because the semantic security
    adversary has access to the $Com$ function, it can calculcate $Com(r_i)$
    upon receiving the query $r_i$. Then setting $c_i = E(s) || Com(r_i)$ it
    responds as usual.

    The reflection simulator on the other hand will be affected by this
    construction. Indeed $|c| = |E(s') || Com(\epsilon)|$. However,
    $|Com(\epsilon)|$ can be computed by the simulator, and therefore the value
    given to the semantic simulator can be obtained as $|c'| = |E(s)| = |E(s')|
    = |c| - |Com(\epsilon)|$.
\end{proof}

\begin{lemma}
    Under the above assumptions and additionally requiring that $Com$ is a
    bijection which is also efficiently reversible, $E(Com(s))$ is reflection secure.
\end{lemma}

\begin{proof}
    This construction is slightly more complicated. Assuming $\mathcal{A}$ is a
    reflection-security adversary which works against the distribution
    $\mathcal{M}$ we will construct a multiple messages semantic security
    adversary as before. However, the distribution $\mathcal{M'}$ will now be
    obtained by the following procedure: Initially, $s$ is chosen out of
    $\mathcal{M}$. Then $Com(s)$ is calculated. This value is then repeated
    $t(\lambda)$ times. Our semantic security adversary will work against
    $\mathcal{M'}$.

    When the challenger produces the encrypted vector $\overbar{c}$, it will
    contain $t(\lambda)$ encryptions of the plaintext $Com(s)$. These can
    readily now be passed to the reflection adversary as reflection query
    responses as before.

    The reflection security simulator now makes use of the $|Com(s)|$ input,
    which it uses to start up the semantic security simulator.

    We now argue that the advantage obtained pertains to a valid predicate on
    the compressed plaintexts space. Indeed, without loss of generality assume
    that $g$ is a predicate. Then $g$ partitions the plaintext space into two
    partitions. Due to $Com$ being a bijection, these partitions are preserved
    after compression and the plaintext predicate $g$ is isomorphic to a
    compressed text predicate $g'$. Therefore, the semantic security adversary
    is able to obtain a non-negligible advantage for a different predicate.
\end{proof}

\begin{lemma}
    Under the above assumptions, $E(Com(s)) || r)$, \break$E(Com(s)) || E(r))$, and
    $E(Com(s)) || E(Com(r))$ are all reflection secure.
\end{lemma}

\begin{proof}
    The proofs for the above schemas can be combined in the obvious way to
    obtain the result needed.
\end{proof}

\begin{lemma}
    Under the above assumptions, $E(s || r)$ is reflection secure.
\end{lemma}

\begin{proof}
    We provide a sketch of this proof.

    To obtain this proof, we construct a semantic security adversary which
    works against a distribution $\mathcal{M'}$ defined as follows. First, $s$
    is chosen out of $\mathcal{M}$. Because the reflection security adversary
    is bounded in time by $t(\lambda)$, therefore each of the reflection
    queries $r_i$ must be $|r_i| < t(\lambda)$. The distribution $\mathcal{M'}$
    then will contain $t(\lambda)$ different vectors, each of dimension
    $t(\lambda)$. We require that the $j$-th $t$-dimensional vector contains
    the same secret text $s$ and an independently chosen reflection text $r$
    with $|r| = j$.

    Then the semantic security adversary $\mathcal{A'}$ works as follows: Upon
    receiving the reflection query $r_i$ from the reflection adversary, it
    responds with a fresh element from the $i$-th vector, which contains the
    encryption of an incorrect reflection string chosen uniformly at random. We
    call these \textit{mock} reflections, and notice that the mock reflection
    has the same length as the actual reflection query.

    We now argue that either this semantic security adversary is able to obtain
    a valid predicate for $s$, or that we have obtained a distinguisher for the
    value $r$.

    To do this, invoke a hybrid argument on the sequence of adversaries $B_i$
    such that the $i$-th adversary works as follows: It responds to the
    reflection queries $r_1, r_2, \cdots, r_i$ as $\mathcal{A'}$ would,
    using the mock reflections, but responds to the queries $r_{i+1},
    \cdots, r_{t(\lambda)}$ using the correct reflections. Clearly,
    $\mathcal{A'} = B_{t(\lambda)}$. If for all $j$ we have that $B_j$'s output
    is computationally indistinguishable from $B_{j+1}$'s output, we are done,
    since this would allow the construction of $\mathcal{A'}$ that obtains a
    non-negligible advantage. However, if for some $j$ we have that $B_j$'s
    output is computationally distinguishable from $B_{j+1}$, then this
    directly allows the construction of a semantic security adversary which is
    able to distinguish between different $r$ values. The intuition behind this
    is that since the mock and actual reflection queries have the same length,
    the assumption of semantic security should not allow the reflection
    adversary to tell apart the two reflection responses.
\end{proof}

\begin{lemma}
    Under the above assumptions, $E(Com(s)$ $||$ $Com(r))$ is reflection secure.
\end{lemma}

\begin{proof}
    The above lemma follows directly from the fact that $E(s || r)$ is
    reflection secure using the techniques previously used to obtain the
    reflection security of $E(Com(s))$ and $E(Com(s))$ $||$ $E(Com(r))$. For the
    compression of the secret, the key intuition is that the reflection
    adversary has access to $|Com(s)|$, that the predicate attacked will be an
    isomorphic predicate on compression space instead of plaintext space, and
    that the distribution attacked will be the compression distribution instead
    of the plaintext distribution. This is readily obtained from the fact that
    $Com$ is a bijection. For the $Com(r)$ portion, the only
    requirement is that the compression function is accessible to both the
    semantic security adversary as well as the reflection security simulator,
    which is true under the assumption that $Com(r)$ is deterministic.
\end{proof}


\documentclass[11pt]{article}

\usepackage{reduce}
\usepackage{fancyhdr}
\usepackage{amsmath}

\title{ A new exp-log limits package for \REDUCE}
\author{Neil Langmead \\
        Konrad-Zuse-Zentrum f\"ur Informationstechnik (ZIB) \\
        Takustrasse 7  \\
        D- 14195 Berlin Dahlem \\
        Berlin
        Germany}
\date{January 1997}

\def\foottitle{Limits}
\pagestyle{fancy}
\lhead[]{{\footnotesize\leftmark}{}}
\rhead[]{\thepage}
\renewcommand{\headrulewidth}{0.6pt}
\renewcommand{\footrulewidth}{0.6pt}
\addtolength{\oddsidemargin}{-20 mm}
\addtolength{\textwidth}{25 mm}
\pagestyle{fancy}
\setlength{\headheight}{14pt}
\setlength{\topmargin}{1 mm}
\setlength{\footskip}{10 mm}
\setlength{\textheight}{220 mm}
\cfoot{}
\rfoot{\small\foottitle}

\begin{document}
\maketitle
\pagebreak
\tableofcontents
\pagebreak
\section{The Exp-Log Limits package}
This package  arises from the PhD thesis of Dominik Gruntz, of the ETH  Z\"{u}rich. He developed a new algorithm to compute limits of "exp-log" functions. Many of the examples he gave were unable to be computed by the present limits package in \REDUCE, the simplest example being the following, whose limit is obviously $0$:
\begin{verbatim}
load limits;

limit(x^7/e^x,x,infinity);

        7
       x
limit(----,x,infinity)
        x
       e
\end{verbatim}

This particular problem arises, because L'Hopital's rule for the computation of indefinite forms (such as $0/0$, or $\frac{\infty}{\infty}$) can only be applied in a CAS a finite number of times, and in \REDUCE~\cite{Red36}, this number is 3. Applied 7 times to the above problem would have yielded the correct answer 0.
The new algorithm solves this particular problem, and enables the computation of many more limit calculations in \REDUCE. We first define the domain in which we work, and then give a statement of the main algorithm that is used in this package. \\[\baselineskip]
Definition: \\
Let $\Re[x]$ be the ring of polynomials in $x$ with real coefficients, and let $f$ be an element in this ring. The field which is obtained from $\Re[x]$ by closing it under the operations $f \rightarrow \exp(f)$ and $f$ $\rightarrow\log |f|$ is called the $L$- field (or logarithmico-exponential field, or field of exp-log functions for short).
\\[\baselineskip]
Hardy proved that every $L$ function is ultimately continuous, of constant sign, monotonic, and tends to $\pm \infty$ or to a finite real constant as $x\rightarrow +\infty.$

Here are some examples of exp-log functions, which the package is able to deal with:
\begin{align*}
 f(x) &=e^{x}*\log(\log(x)) \\
 f(x) &=\frac{\log(\log(x+e^{-x}))}{e^{x^{2}}+\log(\log(x))}  \\
 f(x) &=\log(x)^{\log(x)}  \\
 f(x) &=e^{x*\log(x)}
\end{align*}

\section{The Algorithm}
A complete statement of the algorithm now follows:
Let $f$ be a log-exp function in $x$, whose limit we wish to compute as $x\rightarrow x_0.$ The main steps of the algorithm to do this are as follows:
\begin{itemize}
\item{Determine the set $\Omega$ of the most rapidly varying subexpressions of $f(x)$. Limits may have to be computed recursively at this stage.}
\item{Choose an expression $\omega$ such that $\omega>0$, $\lim_{x \rightarrow \infty} \omega=0 $ and $\omega$ is in the same comparability class as any element of $\Omega$. Rewrite the other expressions in $\Omega$ as $A(x)\omega^{c}$, where $A(x)$ only contains subexpressions in lower comparability classes than $\Omega$.}
\item{Let $f(\omega)$ be the function obtained from $f(x)$ by replacing all elements of $\Omega $ by their representation in terms of $\omega$. Consider all expressions independent of $\omega$ as constants and compute the leading term of the power series of f($\omega$) around $\omega=0^{+}$ }
\item{If the leading exponent $e_0>0$, then the limit is 0, and we stop. If the leading exponent $e_0<0$ then the limit is $\pm \infty$. The sign is defined by the sign of the leading coefficient $c_0$. If the leading exponent $e_0=0$ then the limit is the limit of the leading coeficient $c_0$. If $c_0$ $\not \in C$, where $C=\text{Const}(L)$, the set of exp-log constants, we apply the same algorithm recursively on $c_0$.}
\end{itemize}
%}}
The algorithm to compute the most rapidly varying subset (the mrv set) of a function f is given below:
%\vspace{5 mm}
\begin{tabbing}
procedure mrv(f) \= \\ % f an exp log function in $x$ \\
  if (not (depend(f,$x$)))  $\rightarrow$ return (\{\}) \\
  \> else if $f=x \rightarrow$   return(\{$x$\}) \\
 \> else if $f=gh$  $\rightarrow$   return(max(mrv(g),mrv(h))) \\
  else if $f=g+h$ $\rightarrow$   return(max(mrv(g),mrv(h))) \\
  else if $f=g^{c}$ and c $\in C \rightarrow$   return(mrv(g)) \\
  else if $f=log(g)$ $\rightarrow$   return(mrv(g)) \\
  else if \= $f=e^{g}$ $\rightarrow$  \\
   \>   if $\lim_{x \rightarrow \infty} g=\pm\infty \rightarrow$ \\
   \>        return(max(\{$e^{g}$\}, mrv(g))) \\
    \>  else $\rightarrow $ return mrv(g) \\
\bf{end}
\end{tabbing}

%\vspace{5 mm}
The function max() computes the maximum of the two sets of expressions. Max() compares two elements of its argument sets and returns the set which is in the higher comparability class or the union of both if they have the same order of variation. \\[\baselineskip]
%
For further details, proofs and explanations of the algorithm, please consult~\cite{Grn96}.
\pagebreak

For example, we have
\begin{align*}
&\text{mrv}(e^{x})=\{e^x\} \\
&\text{mrv}(log(log(log(x+x^2+x^3))))=\{x\}  \\
&\text{mrv}(x)=\{x\} \\
&\text{mrv}(e^x+e^{-x}+x^2+x \log(x))= \{e^x,e^{-x} \} \\
&\text{mrv}(e^{e^{-x}})=\{e^{-x} \}
\end{align*}

\subsection{Mrv\_limit Examples}
Consider the following in \REDUCE:
\begin{verbatim}
mrv_limit(e^x,x,infinity);

infinity

mrv_limit(1/log(x),x,infinity);

0

b:=e^x*(e^(1/x-e^-x)-e^(1/x));

           -1        - x
      x + x      - e
b := e       *(e         - 1)

mrv_limit(b,x,infinity);

-1

                                       -1
  ex:=  - log(log(log(log(x))) + log(x))  *log(x)

                       *(log(log(x)) - log(log(log(x)) + log(x)));

            - log(x)*(log(log(x)) - log(log(log(x)) + log(x)))
ex:=     -----------------------------------------------------
                    log(log(log(log(x))) + log(x))

off mcd;

mrv_limit(ex,x,infinity);

1

(log(x+e^-x)+log(1/x))/(log(x)*e^x);

  - x       -1       -1          - x
e    *log(x)  *(log(x  ) + log(e     + x));

mrv_limit(ws,x,infinity);

0

mrv_limit((log(x)*e^-x)/e^(log(x)+e^(x^2)),x,infinity);

0

\end{verbatim}
\normalsize
\section{The tracing facility}
The package provides a means of tracing the $mrv\_limit$ function at its main steps, and is intended to help the user if he encounters problems. Messages are displayed informing the user which Taylor expansion is being computed, all recursive calls are listed, and the value returned by the $mrv$ function is given. This information is displayed when  a switch $tracelimit$ is on. This is off by default, but can be switched on with the command
\begin{verbatim}
on tracelimit;
\end{verbatim}
For a more complete examination of the workings of the algorithm, the user could also try the command
\begin{verbatim}
tr mrv_limit;
\end{verbatim}
This is not recommended, as the amount of information returned is often huge and difficult to wade through.
Here is a simple example in \REDUCE:
\begin{verbatim}

Loading image file: /silo/cons/reduce35/Alpha/binary/redu37a.img
REDUCE Development Version,  4-Nov-96 ...

1: load mrvlimit;

2: on tracelimit;

3: mrv_limit(e^x,x,infinity);

mrv_f is {x}

                     x
After move_up, f is e

                        -1
performing taylor on: ww

                      -1
series expansion is ww

            -1
series is ww

exponent list is {expt,-1}

leading exponent e0 is {expt,-1}

           x
mrv_f is {e }

h is x

mrv_f is {x}

                     x
After move_up, f is e

                        -1
performing taylor on: ww

                      -1
series expansion is ww

            -1
series is ww

exponent list is {expt,-1}

leading exponent e0 is {expt,-1}

                            - x
small has been changed to e

                                 -1
After substitution to ww, f is ww

                        -1
performing taylor on: ww

                      -1
series expansion is ww

            -1
series is ww

exponent list is {expt,-1}

leading exponent e0 is {expt,-1}

infinity
\end{verbatim}
\vspace{10 mm}
Note that, due to the recursiveness of the functions $mrv$ and $mrv\_limit$, many calls to each function are made, and information is given on all calls when the \emph{tracelimit} switch is on.

\section{Comments, Bug reports and Suggestions}
This package was written when the author was a placement student at ZIB Berlin. Please address all comments, bugs and suggestions to Winfried Neun, ZIB, Takustrasse 7, D-14195 Berlin Dahlem, Germany, or e/mail neun@zib.de.
\pagebreak
\begin{thebibliography}{99}
\normalsize

\bibitem[Grn96]{Grn96} Gruntz, Dominik,
\textit{On Computing Limits in a Symbolik Manipulation System}, \\
PhD Thesis, ETH Z\"urich

\bibitem[Red36]{Red36} Hearn, Anthony C. and Fitch, John F.
\textit{REDUCE User's Manual 3.6}, \\ RAND Corporation, 1995

\end{thebibliography}
\end{document}


\documentclass{article}
\usepackage[section]{placeins}
\usepackage{graphicx, wrapfig, amsmath, amssymb, physics, hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    filecolor=magenta,
    urlcolor=cyan,
    }

\author{Yaghoub Shahmari}
\title{Report - Problem Set No 5}
\date{\today}
\graphicspath{ {../Figs/} }

\begin{document}
    \maketitle
    \section*{Problem 1}
    \textbf{Basic description:}

    In this problem, we're going to discuss 2D Random Walkers.
    As the lecture notes described, we expect our results to show these relations:

    \begin{gather*}
        \langle r^{2}\rangle =2dDt,D=\dfrac{l^{2}}{2dr}\\
        R_{g}=\sqrt{\langle r^{2}\rangle },\tau =l=1,d=2\\
        \Rightarrow R_{g}=\sqrt{t}
    \end{gather*}

    The simulation creates a list of random choices of steps
    and calculates the sum of total changes of location of the random walker.
    The number of chosen random steps is equal to $t$.
    We repeat the simulation for different $t$ many times
    and calculate the gyration radius of the all of final positions of each $t$.

    \textbf{Results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q1/Q1-Rg(t)}
        \label{fig:1.1}
        \caption{Gyration radius of each time step.}
    \end{figure}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q1/Q1-2DHist1}
        \label{fig:1.2}
        \includegraphics[scale = 0.4]{/Q1/Q1-2DHist2}
        \label{fig:1.3}
        \includegraphics[scale = 0.4]{/Q1/Q1-2DHist3}
        \label{fig:1.4}
        \caption{2D Histogram, Shows distribute of Random Walkers in 10th, 100th, and 1000th time step.}
    \end{figure}

    \pagebreak

    \section*{Problem 2}
    \textbf{Basic description:}

    In this problem, we're going to simulate Diffusion-Limited Aggregation and apply some limitations on 2D Random Walkers.
    We consider a network that has a periodic boundary from sides,
    a fixed floor, and a non-fixed boundary on top.
    We continue to release particles until the roof reaches a specific location.

    \textbf{Results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q2/Q2-HM}
        \label{fig:2.1}
        \caption{Distribution of Diffusion-Limited Aggregation. An animation is provided in Figs folder.}
    \end{figure}

    \pagebreak

    \section*{Problem 3}
    \textbf{Basic description:}

    In this problem,
    we're going to count the number of Self-Avoiding Walks of a Random walker.
    A normal random walker has a $4^N$ possible way to navigate $N$ step on a surface.
    It is a simple permutation of 4 choices.
    To find every possible way of navigation using an algorithm,
    we need N fors to check every way.
    Instead of using N fors we can just call a rescue function
    that calls itself within itself exactly N times.
    If we check avoidance then call the function,
    we can count the number of Self-Avoiding Walks.

    \textbf{Results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.25]{/Q3/Q3-1}
        \label{fig:3.1}
        \includegraphics[scale = 0.25]{/Q3/Q3-2}
        \label{fig:3.2}
        \includegraphics[scale = 0.5]{/Q3/Q3-3}
        \label{fig:3.3}
        \caption{Plots (log-log and normal scale plot and ratio plot) that shows the growth of the count of ways.}
    \end{figure}

    \pagebreak

    \section*{Problem 4}
    \textbf{Basic description:}

    In this problem, we're going to study the Random number generator.
    Initially, we make a sample of random numbers in [0,9] and show the distribution.
    Then check the coefficient of variation and its relation to the value of N.

    \textbf{Results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q4/Q4-Hist}
        \label{fig:4.1}
        \includegraphics[scale = 0.4]{/Q4/Q4-Scat}
        \label{fig:4.2}
        \caption{As you can see, the data shows the distribution of Numbers follows the $Y = \frac{N}{10}$ and for CV we have $\frac{\sigma}{N}~N^{-0.5}$}
    \end{figure}

    \pagebreak

    \section*{Problem 5}
    \textbf{Basic description:}

    In this problem, we do the same thing we have done for the previous Question,
    but instead of analyzing the sample we gathered from the random generator,
    we sample the numbers. We only choose the numbers came before the 4.

    \textbf{Results:}

    \begin{figure}[!htb]
        \centering
        \includegraphics[scale = 0.4]{/Q5/Q5-Hist}
        \label{fig:5.1}
        \includegraphics[scale = 0.4]{/Q5/Q5-Scat}
        \label{fig:5.2}
        \caption{As you can see, the data shows the distribution of Numbers follows the $Y = \frac{N}{10}$ and for CV we have $\frac{\sigma}{N}~N^{-0.5}$}
    \end{figure}

    \pagebreak

    \centering
    \textbf{The whole data I gathered is in \href{https://github.com/shahmari/ComputationalPhysics-Fall2021/tree/main/ProblemSet5/Data}{this link}}

    \textbf{Also check \href{https://www.youtube.com/watch?v=dQw4w9WgXcQ}{this link}}

    Thanks for watching :)
\end{document}


\subsection{Search algorithms}

A search algorithm takes a grid and identifies a path from a start point to an end point. Each node in the grid has connections to other nodes, with costs of moving between nodes.

\subsection{Types of nodes in a search algorithm}

In each search algorithm there are three types of nodes: unexplored nodes, explored nodes and frontier nodes. At the start of the algorithm the start node is explored, each node connected to the start node is a frontier node, and all other nodes are unexplored nodes.

When an algorithm explores a frontier node, it is added to the explored nodes, and all new nodes are added to the frontier nodes.