\chapter{Conversions}
\label{avra_conv}

A {\it conversion\/} in \HOL\ is a rule that maps a term to a theorem
expressing the equality of that term to some other term.  An example is the
rule for $\beta$-conversion:

\[\ml{(}\verb%\%x\ml{.}t_1\ml{)}t_2\quad\mapsto\quad
\ml{|- (}\verb%\%x\ml{.}t_1\ml{)}t_2\;\; \ml{=}  \;\;t_1[t_2/x] \]

\noindent Theorems\index{equational theorems, in HOL logic@equational theorems, in \HOL\ logic!produced by conversions}\index{conversions!purpose of} of this sort are used in
\HOL\ in a variety of contexts, to justify the replacement of particular terms
by semantically equivalent terms.

The \ML\ type of conversions is \ml{conv}:

\begin{hol}\begin{verbatim}
type conv = term -> thm
\end{verbatim}\end{hol}

\noindent For example, \ml{BETA\_CONV}\index{BETA_CONV@\ml{BETA\_CONV}}
is an \ML\ function of type \ml{conv} (\ie\ a conversion) that
expresses $\beta$-conversion in \HOL.  It produces the appropriate
equational theorem on $\beta$-redexes and fails elsewhere.

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- BETA_CONV;
> val it = fn : term -> thm

- BETA_CONV ``(\x. (\y. (\z. x + y + z)3)2) 1``;
> val it = |- (\x. (\y. (\z. x + (y + z))3)2)1 = (\y. (\z. 1 + (y + z))3)2 :
  thm

- BETA_CONV ``(\y. (\z. 1 + (y + z))3) 2``;
> val it = |- (\y. (\z. 1 + (y + z))3)2 = (\z. 1 + (2 + z))3 : thm

- BETA_CONV ``(\z. 1 + (2 + z)) 3``;
> val it = |- (\z. 1 + (2 + z))3 = 1 + (2 + 3) : thm

- BETA_CONV ``1 + (2 + 3)``;
! Uncaught exception:
! HOL_ERR
\end{verbatim}
\end{session}

The basic conversions, as well as a number of those commonly used, are
provided in \HOL.  There are also groups of
application-specific\index{conversions!application specific}
conversions to be found in some of the libraries.  (Of those provided,
some are derived and some, like \ml{BETA\_CONV} are taken as
axiomatic\footnote{A list of the axiomatic rules was supplied in
  Section~\ref{avra_standard}.}.)  In addition, \HOL\ provides a
collection of \ML\ functions enabling users to define new conversions
(as well as new rules and tactics) as functions of existing ones.
Some of these are described in Sections~\ref{avra1} and~\ref{avra2}.
The notion of conversions is inherited from Cambridge
\LCF;\index{LCF@\LCF!Cambridge} the underlying principles are
described in \cite{lcp-rewrite,new-LCF-man}.

Conversions such as \ml{BETA\_CONV}\index{beta-conversion, in HOL
  logic@beta-conversion, in \HOL\ logic!not expressible as a
  theorem}\index{theorems, in HOL logic@theorems, in \HOL\ logic!rules
  inexpressible as}\index{conversions!as families of equations}
represent infinite families of equations\footnote{This was also
  mentioned in Section~\ref{avra_standard}.}. They are particularly
useful in cases in which it is impossible to state, within the logic,
a single axiom or theorem instantiable to every equation in a
family\index{families of inferences, in HOL logic@families of
  inferences, in \HOL\ logic}.\footnote{In the case of
  $\beta$-conversion specifically, it is the substitution of one term
  for another in a context that is inexpressible; but in general,
  there is a variety of reasons that arise.} Instead, an \ML\
procedure returns the instance of the desired theorem for any given
term. This is also the reason that quite a few of the other rules in
\HOL\ are not stated instead as axioms or theorems. As rules,
conversions are distinguished with an \ML\ type abbreviation simply
because there are relatively many of them with the same type, and
because they return equational\index{theorems, in HOL logic@theorems,
  in \HOL\ logic!equational}\index{equational theorems, in HOL
  logic@equational theorems, in \HOL\ logic!produced by conversions}
theorems that lend themselves directly to term rewriting.\footnote{In
  fact, some ML functions have names with the suffix `{\tt \_CONV}'
  but do not have the type {\tt conv}; {\tt SUBST\_CONV}, for example,
  has type {\tt (thm \# term) list -> term -> conv}. Those that
  eventually produce conversion are thought of as `conversion
  schemas'.} In many \HOL\ applications, the main use of conversions
is to produce these equational theorems.  A few examples of
conversions are illustrated below.

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- NOT_FORALL_CONV ``~!x. (f:'a->'a) x = g x``;
> val it = |- ~(!x. f x = g x) = (?x. ~(f x = g x)) : thm

- CONTRAPOS_CONV ``(!x. f x = g x) ==> ((f:'a->'a) = g)``;
> val it = |- (!x. f x = g x) ==> (f = g) = ~(f = g) ==> ~!x. f x = g x : thm

- SELECT_CONV ``(@f:'a->'a. f x = g x)x = g x``;
> val it = |- ((@f. f x = g x)x = g x) = ?f. f x = g x

- EXISTS_UNIQUE_CONV ``?!z. (f:'a->'a) z = g z``;
> val it =
    |- (?!z. f z = g z) =
       (?z. f z = g z) /\ !z z'. (f z = g z) /\ (f z' = g z') ==> (z = z') :
  thm
\end{verbatim}
\end{session}

\noindent An example of an application specific conversion is
\ml{numLib}'s \ml{num\_CONV}:

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- numLib.num_CONV ``2``;
> val it = |- 2 = SUC 1 : thm

- numLib.num_CONV ``1``;
> val it = |- 1 = SUC 0 : thm

- numLib.num_CONV ``0`` handle e => Raise e;

Exception raised at Num_conv.num_CONV:
Term either not a numeral or zero
! Uncaught exception:
! HOL_ERR
\end{verbatim}
\end{session}

Another application of conversions, related to the first, is in the
implementation of the existing rewriting tools, \ml{REWRITE\_CONV}
(Section~\ref{avra3}), \ml{REWRITE\_RULE} (Section~\ref{avra_rewrite})
and \ml{REWRITE\_TAC} (Chapter~\ref{tactics-and-tacticals}), which are
central to theorem proving in \HOL.  This use is explained in
Section~\ref{avra3}, both as an example and because users may have
occasion to construct rewriting tools of their own design, by similar
methods.  The next section introduces the conversion-building tools in
general.

\section{Indicating unchangedness}

All conversions may raise the special exception \ml{Conv.UNCHANGED} on
an input term \ml{t}, as a ``short-hand'' instead of returning the
reflexive theorem \ml{|-~t~=~t}.  This is done for efficiency reasons.
All of the connectives described below in Section~\ref{avra1} handle
this exception appropriately.  The standard function \ml{QCONV} is
provided to automatically handle this exception in contexts where it
would be inappropriate (typically where a conversion is being called
to provide a theorem directly).  \ml{QCONV}'s implementation is
\begin{alltt}
   fun QCONV c t = c t handle UNCHANGED => REFL t
\end{alltt}

\section{Conversion combining operators}
\label{avra1}

A term $u$ is said to {\it reduce\/}\index{reduction, by conversions}\index{conversions!reduction by} to a term $v$ by a conversion $c$ if
there exists a
finite sequence\index{sequencing!of conversions} of terms $t_1$, $t_2$, $\ldots$, $t_n$ such that:
\begin{myenumerate}
\item $u = t_1$ and $v = t_n$;
\item $c\ t_i$ evaluates to the theorem
$\ml{|- }t_i\;\ml{=}\;t_{i+1}$ for $1\leq i < n$;
\item The evaluation of $c\ t_n$ fails.
\end{myenumerate}

\noindent The first session of this chapter illustrates the reduction of
the term

\begin{hol}
\begin{verbatim}
   (\x. (\y. (\z. x + y + z)3)2)1
\end{verbatim}
\end{hol}

\noindent to \ml{1 + (2 + 3)} by the
conversion \ml{BETA\_CONV}, in a reduction sequence of length four:

\begin{hol}
\begin{verbatim}
   (\x. (\y. (\z. x + (y + z))3)2)1
   (\y. (\z. 1 + (y + z))3)2
   (\z. 1 + (2 + z))3
   1 + (2 + 3)
\end{verbatim}
\end{hol}

\noindent That is, \ml{BETA\_CONV} applies to each term of the sequence,
except the fourth and last, to give a theorem equating that term to
the next term. Therefore, each term of the sequence, from the second
on, can be extracted from the theorem for the previous term; namely,
it is the right hand side of the conclusion.  The whole reduction can
therefore be accomplished by repeated application of \ml{BETA\_CONV}
to the terms of the sequence as they are generated.

\index{alternation!of conversions|(} To transform \ml{BETA\_CONV} to
achieve this effect, two operators on
conversions\index{conversions!operators on|(} are introduced.  The
first one, infixed, is \ml{THENC}, which sequences conversions.

\begin{holboxed}
\index{THENC@\ml{THENC}|pin}
\begin{verbatim}
   op THENC : conv -> conv -> conv
\end{verbatim}
\end{holboxed}

\noindent If $c_1\ t_1$ evaluates to $\Gamma_1\ml{ |- }t_1\ml{=}t_2$ and
$c_2\ t_2$ evaluates to $\Gamma_2\ml{ |- }t_2\ml{=}t_3$, then
$\ml{(}c_1\ \ml{THENC}\ c_2\ml{)}\ t_1$ evaluates to $\Gamma_1\cup
\Gamma_2\ml{\ |-\ }t_1\ml{=}t_3$. If the evaluation of $c_1\ t_1$ or
the evaluation of $c_2\ t_2$ fails, then so does the evaluation of
$c_1\ \ml{THENC}\ c_2$. \ml{THENC} is justified by the transitivity of
equality.

The second, also infixed, is \ml{ORELSEC}; this applies a second
conversion if the application of the first one fails.

\begin{holboxed}
\index{ORELSEC@\ml{ORELSEC}|pin}
\begin{verbatim}
   op ORELSEC : conv -> conv -> conv
\end{verbatim}
\end{holboxed}

\noindent $\ml{(}c_1\ \ml{ORELSEC}\ c_2\ml{)}\ t$ evaluates to $c_1\ t$
if that evaluation succeeds, and to $c_2\ t$ otherwise. (The failure
to evaluate is detected via the \ML\ failure construct.)

The functions \ml{THENC} and \ml{ORELSEC} are used to define the
desired operator, \ml{REPEATC}\index{repetition!of conversions}, which
successively\index{successive application!conversion operator for}
applies a conversion until it fails:

\begin{holboxed}
\index{REPEATC@\ml{REPEATC}|pin}
\begin{verbatim}
   REPEATC : conv -> conv
\end{verbatim}
\end{holboxed}

\noindent \ml{REPEATC}\ $c$ is intuitively equivalent to:

\begin{hol}
\begin{alltt}
   (\m{c} THENC \m{c} THENC ... THENC \m{c} THENC ...) ORELSEC ALL_CONV
\end{alltt}
\end{hol}

\noindent It is defined recursively:\footnote{Note that because ML is a
  call-by-value language, the extra argument $t$ is needed in the
  definition of {\tt REPEATC}; without it the definition would loop.
  There is a similar problem with the tactical {\tt REPEAT}; see
  Chapter~\ref{tactics-and-tacticals}.}

\begin{hol}
\begin{verbatim}
   fun REPEATC c t = ((c THENC (REPEATC c)) ORELSEC ALL_CONV) t
\end{verbatim}
\end{hol}

The current example term can thus be completely reduced by use of
\ml{BETA\_CONV} transformed by the \ml{REPEATC} operator:

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- REPEATC BETA_CONV;
> val it = fn : term -> thm

- REPEATC BETA_CONV ``(\x. (\y. (\z. x + y + z)3)2)1``;
> val it = |- (\x. (\y. (\z. x + (y + z))3)2)1 = 1 + (2 + 3) : thm
\end{verbatim}
\end{session}
\index{alternation!of conversions|)}

\ml{BETA\_CONV} applies to terms of a certain top level form only,
namely to $\beta$-redexes, and fails on terms of any other form.  In
addition, no number of repetitions of \ml{BETA\_CONV} will
$\beta$-reduce {\it arbitrary\/} $\beta$-redexes embedded in terms.
For example, the term shown below fails even at the top level because
it is not a $\beta$-redex:

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- BETA_CONV ``(((\x.(\y.(\z. x + y + z))) 1) 2) 3``;
! Uncaught exception:
! HOL_ERR

- is_abs ``(((\x.(\y.(\z. x + y + z))) 1) 2)``;
> val it = false : bool
\end{verbatim}
\end{session}

\noindent
The $\beta$-redex {\small\verb|(\w.w)3|} is not affected in the third
input of the session shown below, because of its position in the
structure of the whole term.  This is so even though the whole term is
reduced, and the subterm at top level could be reduced:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#BETA_CONV "(\z. x + y + z)3";;
|- (\z. x + (y + z))3 = x + (y + 3)

#BETA_CONV "(\w.w)3";;
|- (\w. w)3 = 3

#REPEATC BETA_CONV "(\z. x + y + z)((\w.w)3)";;
|- (\z. x + (y + z))((\w. w)3) = x + (y + ((\w. w)3))
\end{verbatim}\end{session}

To produce, from a conversion $c$, a conversion that applies $c$ to every
subterm of a term, the function \ml{DEPTH\_CONV} can be applied to $c$:

\begin{holboxed}
\index{DEPTH_CONV@\ml{DEPTH\_CONV}|pin}
\begin{verbatim}
   DEPTH_CONV : conv -> conv
\end{verbatim}\end{holboxed}

\noindent \ml{DEPTH\_CONV}$\ c$ is a conversion

\[t\quad\mapsto\quad \ml{|- }t\ml{ = }t'\]

\noindent where $t'$ is obtained from $t$ by replacing every subterm
$u$ by $v$ if $u$ reduces to $v$ by $c$. (Subterms for which
$c\ u$ fails are left unchanged.) The definition leaves open the search strategy;
in fact,
\ml{DEPTH\_CONV}$\ c$\index{DEPTH_CONV@\ml{DEPTH\_CONV}!search strategy of}
 traverses a term\footnote{That is, it traverses
the abstract parse tree of the term.} `bottom up', once, and left-to-right,
repeatedly applying $c$ to each subterm until no longer applicable.
This helps with the two problems thus far:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#DEPTH_CONV BETA_CONV "(((\x.(\y.(\z. x + y + z))) 1) 2) 3";;
|- (\x y z. x + (y + z))1 2 3 = 1 + (2 + 3)

#DEPTH_CONV BETA_CONV "(\z. x + y + z)((\w.w)3)";;
|- (\z. x + (y + z))((\w. w)3) = x + (y + 3)
\end{verbatim}\end{session}

It may happen, however,
that the result of such a conversion still contains subterms
that could themselves be reduced at top level. For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let t = "(\f.\x.f x)(\n.n+1)";;
t = "(\f x. f x)(\n. n + 1)" : term

#DEPTH_CONV BETA_CONV t;;
|- (\f x. f x)(\n. n + 1) = (\x. (\n. n + 1)x)
\end{verbatim}\end{session}

\noindent The function \ml{TOP\_DEPTH\_CONV}
 does more
searching and reduction than
\ml{DEPTH\_CONV}: it replaces
every subterm
$u$ by $v'$ if $u$ reduces to $v$ by $c$ and $v$ {\it recursively\/} reduces
to $v'$ by {\tt TOP\_DEPTH\_CONV}$\ c$.\footnote{Readers interested
in characterizing the search strategy of {\tt TOP\_DEPTH\_CONV} should
study the \ML\ definitions near the end of this section.}

\begin{holboxed}
\index{TOP_DEPTH_CONV@\ml{TOP\_DEPTH\_CONV}|pin}
\begin{verbatim}
   TOP_DEPTH_CONV : conv -> conv
\end{verbatim}\end{holboxed}

\noindent Thus:

\begin{session}\begin{verbatim}
#TOP_DEPTH_CONV BETA_CONV t;;
|- (\f x. f x)(\n. n + 1) = (\x. x + 1)
\end{verbatim}\end{session}

Finally, the simpler function \ml{ONCE\_DEPTH\_CONV} is provided:

\begin{holboxed}
\index{ONCE_DEPTH_CONV@\ml{ONCE\_DEPTH\_CONV}|pin}
\begin{verbatim}
   ONCE_DEPTH_CONV : conv -> conv
\end{verbatim}\end{holboxed}

\noindent $\ml{ONCE\_DEPTH\_CONV}\ c\ t$ applies $c$ once to the first
term (and only the first term)
on which it succeeds in a top-down traversal:

\begin{session}\begin{verbatim}
#ONCE_DEPTH_CONV BETA_CONV t;;
|- (\f x. f x)(\n. n + 1) = (\x. (\n. n + 1)x)

#ONCE_DEPTH_CONV BETA_CONV "(\x. (\n. n + 1)x)";;
|- (\x. (\n. n + 1)x) = (\x. x + 1)
\end{verbatim}\end{session}

The equational theorems returned by conversions are not always
useful in equational form.  To make the results more useful for theorem
proving,
a conversion can be converted to a rule or a tactic, using the functions
\ml{CONV\_RULE} or \ml{CONV\_TAC}, respectively.

\begin{holboxed}
\index{CONV_RULE@\ml{CONV\_RULE}|pin}
\index{CONV_TAC@\ml{CONV\_TAC}|pin}
\begin{verbatim}
   CONV_RULE : conv -> thm -> thm
   CONV_TAC  : conv -> tactic
\end{verbatim}\end{holboxed}

\noindent $\ml{CONV\_RULE}\ c\ \ml{(|- }t\ml{)}$ returns $\ml{|- }t'$, where
$c\ t$ evaluates to the equation
$\ml{|-}\ t\ml{=}t'$.
$\ml{CONV\_TAC}\ c$ is a tactic that
converts the conclusion of a goal using $c$. \ml{CONV\_RULE} is defined by:

\begin{hol}\begin{verbatim}
   let CONV_RULE c th = EQ_MP (c(concl th)) th
\end{verbatim}\end{hol}

\noindent (The validation of \ml{CONV\_TAC} also
uses \ml{EQ\_MP}\footnote{For \ml{EQ\_MP}, see ~\ref{avra_eq_mp}.}.)
For example, the built-in rule \ml{BETA\_RULE} reduces some
of the $\beta$-redex subterms of a term.

\begin{holboxed}
\index{BETA_RULE@\ml{BETA\_RULE}|pin}
\begin{verbatim}
   BETA_RULE : thm -> thm
\end{verbatim}\end{holboxed}

\noindent It is defined by:

\begin{hol}\begin{verbatim}
   let BETA_RULE = CONV_RULE(DEPTH_CONV BETA_CONV)
\end{verbatim}\end{hol}

\noindent The search invoked by \ml{BETA\_RULE}
is adequate for some purposes but not others; for example,
the first use shown below but not the second:

\begin{session}\begin{verbatim}
#BETA_RULE(ASSUME "(((\x.(\y.(\z. x + y + z))) 1) 2) 3 < 10");;
. |- (1 + (2 + 3)) < 10

#let th = ASSUME "NEXT = ^t";;
th = . |- NEXT = (\f x. f x)(\n. n + 1)

#BETA_RULE th;;
. |- NEXT = (\x. (\n. n + 1)x)

#BETA_RULE(BETA_RULE th);;
. |- NEXT = (\x. x + 1)
\end{verbatim}\end{session}

\noindent A more powerful
$\beta$-reduction rule
that used the second  search strategy could be defined as shown below
(this is not built into \HOL).

\begin{session}\begin{verbatim}
#let TOP_BETA_RULE = CONV_RULE(TOP_DEPTH_CONV BETA_CONV);;
TOP_BETA_RULE = - : (thm -> thm)

#TOP_BETA_RULE th;;
. |- NEXT = (\x. x + 1)
\end{verbatim}\end{session}

\ml{TOP\_DEPTH\_CONV} is the traversal
strategy used by the \HOL\ rewriting tools described in Section~\ref{avra3}.
\index{conversions!operators on|)}

\section{Writing compound conversions}
\label{avra2}

\index{conversions!functions for building|(}
There are several other conversion operators in \HOL, which,
together with \ml{THENC}, \ml{ORELSEC} and \ml{REPEATC} are available
for building more complex conversions, as well as rules, tactics, and so on.
These are described below; several are good illustrations themselves
of how functions are built using conversions. The section culminates
with the explanation of how \ml{DEPTH\_CONV}, \ml{TOP\_DEPTH\_CONV}, and
\ml{ONCE\_DEPTH\_CONV} are built.

The conversion \ml{NO\_CONV} is an identity for
\ml{ORELSEC}\index{ORELSEC@\ml{ORELSEC}}, useful
in building functions.

\begin{holboxed}
\index{NO_CONV@\ml{NO\_CONV}|pin}
\begin{verbatim}
   NO_CONV : conv
\end{verbatim}\end{holboxed}

\noindent $\ml{NO\_CONV}\ t$ always fails.

The function \ml{FIRST\_CONV}
returns $c\ t$ for the first conversion $c$, in a list of conversions,
for which the evaluation of $c\ t$ succeeds.

\begin{holboxed}
\index{FIRST_CONV@\ml{FIRST\_CONV}|pin}
\begin{verbatim}
   FIRST_CONV : conv list -> conv
\end{verbatim}\end{holboxed}

\noindent $\ml{FIRST\_CONV [}c_1\ml{;}\ldots\ml{;}c_n\ml{]}$ is equivalent,
intuitively, to:

\begin{hol}
\index{ORELSEC@\ml{ORELSEC}}
\begin{alltt}
   \m{c\sb{1}} ORELSEC \m{c\sb{2}} ORELSEC \m{\ldots} ORELSEC \m{c\sb{n}}
\end{alltt}\end{hol}

\noindent It is defined by:

\begin{hol}\begin{verbatim}
   let FIRST_CONV cl t =
       itlist $ORELSEC cl NO_CONV t ? failwith `FIRST_CONV`;;
\end{verbatim}\end{hol}

The conversion \ml{ALL\_CONV} is an identity for \ml{THENC},\index{THENC@\ml{THENC}} useful
in building functions.

\begin{holboxed}
\index{ALL_CONV@\ml{ALL\_CONV}|pin}
\begin{verbatim}
   ALL_CONV : conv
\end{verbatim}\end{holboxed}

\noindent \ml{ALL\_CONV\ $t$} evaluates to \ml{|-\ $t$=$t$}. It is
defined as being identical to \ml{REFL}.\index{REFL@\ml{REFL}}

The function \ml{EVERY\_CONV} applies a list of conversions in sequence.

\begin{holboxed}
\index{EVERY_CONV@\ml{EVERY\_CONV}|pin}
\begin{verbatim}
   EVERY_CONV : conv list -> conv
\end{verbatim}\end{holboxed}

\noindent $\ml{EVERY\_CONV [}c_1\ml{;}\ldots\ml{;}c_n\ml{]}$ is equivalent,
intuitively, to:

\begin{hol}
\index{THENC@\ml{THENC}}
\begin{alltt}
   \m{c\sb{1}} THENC \m{c\sb{2}} THENC \m{\ldots} THENC \m{c\sb{n}}
\end{alltt}\end{hol}

\noindent It is defined by:

\begin{hol}\begin{verbatim}
   let EVERY_CONV cl t =
       itlist $THENC cl ALL_CONV t ? failwith `EVERY_CONV`
\end{verbatim}\end{hol}

The operator \ml{CHANGED\_CONV} converts one conversion to another that
fails on arguments that it cannot change.

\begin{holboxed}
\index{CHANGED_CONV@\ml{CHANGED\_CONV}|pin}
\begin{verbatim}
   CHANGED_CONV : conv -> conv
\end{verbatim}\end{holboxed}

\noindent If $c\ t$ evaluates to $\ml{|-}\ t\ml{=}t'$, then
$\ml{CHANGED\_CONV}\ c\ t$  also evaluates to $\ml{|-}\ t\ml{=}t'$,
unless $t$ and $t'$ are the same (up to $\alpha$-conversion),
in which case it fails.

The operator \ml{TRY\_CONV} maps one conversion to another that
always succeeds, by replacing failures with the identity conversion.

\begin{holboxed}
\index{TRY_CONV@\ml{TRY\_CONV}|pin}
\begin{verbatim}
   TRY_CONV : conv
\end{verbatim}\end{holboxed}

\noindent If $c\ t$ evaluates to $\ml{|-}\ t\ml{=}t'$, then
$\ml{TRY\_CONV}\ c\ t$  also evaluates to $\ml{|-}\ t\ml{=}t'$. If
$c\ t$ fails, then $\ml{TRY\_CONV}\ c\ t$ evaluates
to  $\ml{|-}\ t\ml{=}t$. \ml{TRY\_CONV} is implemented by:

\begin{hol}\begin{verbatim}
   let TRY_CONV c = c ORELSEC ALL_CONV
\end{verbatim}\end{hol}

\noindent It is used in the implementation of
\ml{TOP\_DEPTH\_CONV} (given later).

There are a number of operators for applying conversions to the
immediate subterms of a term. These use the \ML\ functions:

\begin{holboxed}
\index{MK_COMB@\ml{MK\_COMB}|pin}
\index{MK_ABS@\ml{MK\_ABS}|pin}
\begin{verbatim}
   MK_COMB : thm # thm -> thm
   MK_ABS  : thm -> thm
\end{verbatim}\end{holboxed}

\noindent \ml{MK\_COMB} and \ml{MK\_ABS} implement the following derived rules:

$${\Gamma_1 \ml{ |- } u_1\ml{=}v_1\qquad
 \Gamma_2\ml{ |- } u_2\ml{=}v_2 \over
\Gamma_1\cup\Gamma_2\ml{ |- } u_1\ u_2 \ml{=} v_1\ v_2}\quad\ml{MK\_COMB}$$

$${\Gamma\ml{ |- !}x\ml{.}u\ml{=}v \over
\Gamma\ml{ |- (}\verb%\%x\ml{.}u\ml)=(\verb%\%x\ml{.}v\ml{)}}\quad\ml{MK\_ABS}$$

\noindent The function \ml{SUB\_CONV}
applies a conversion to the immediate subterms
of a term.

\begin{holboxed}
\index{SUB_CONV@\ml{SUB\_CONV}|pin}
\begin{verbatim}
   SUB_CONV : conv
\end{verbatim}\end{holboxed}

\noindent In particular:

\begin{itemize}
\item \ml{SUB\_CONV}$\;\;c\;\;$\ml{"}$x$\ml{"} $\;=\;$\ \ml{|- }$x$\ml{=}$x$;
\item \ml{SUB\_CONV}$\;\;c\;\;$\ml{"}$u\;v$\ml{"} $\;=\;$ \ml{|- }$u\;v$\ml{=}$u'\;v'$,\ \  if
$c\;\;u$\ $=$\ \ml{|- }$u$\ml{=}$u'$ and
$c\;\;v$\ $=$\ \ml{|- }$v$\ml{=}$v'$;
\item\ml{SUB\_CONV}$\;\;c\;\;$\ml{"}{\small\verb%\%}$x$\ml{.}$u$\ml{"} $\;=\;$
\ml{|- (}{\small\verb%\%}$x$\ml{.}$u$\ml{) = (}{\small\verb%\%}$x$\ml{.}$u'$\ml{)}, \ \ if
$c\;\;u$\ $=$\ \ml{|- }$u$\ml{=}$u'$.
\end{itemize}

\noindent \ml{SUB\_CONV} is implemented\index{conversions!implementation of, in ML@implementation of, in \ML|(} in terms of
\ml{MK\_COMB} and \ml{MK\_ABS}:

\begin{hol}\begin{verbatim}
   let SUB_CONV c t =
       if is_comb t then
          (let rator,rand = dest_comb t in
           MK_COMB (c rator, c rand))
       if is_abs t then
          (let bv,body = dest_abs t in
           let bodyth = c body in
           MK_ABS (GEN bv bodyth))
                   else (ALL_CONV t)
\end{verbatim}\end{hol}

\noindent \ml{SUB\_CONV}, too, is used in the definitions of
\ml{DEPTH\_CONV} and \ml{TOP\_DEPTH\_CONV}.

Three other useful conversion operators, also for applying conversions
to the immediate subterms of a term, are as follows:

\begin{holboxed}
\index{RATOR_CONV@\ml{RATOR\_CONV}|pin}
\index{RAND_CONV@\ml{RAND\_CONV}|pin}
\index{ABS_CONV@\ml{ABS\_CONV}|pin}
\begin{verbatim}
   RATOR_CONV : conv -> conv
   RAND_CONV  : conv -> conv
   ABS_CONV   : conv -> conv
\end{verbatim}\end{holboxed}

\noindent \ml{RATOR\_CONV}$\ c$ converts the operator of an application using
$c$; \ml{RAND\_CONV}$\ c$ converts the operand of an application; and
\ml{ABS\_CONV}$\ c$ converts the body of an abstraction. Combinations
of these are useful for applying conversions to particular subterms.
These are implemented by:

\begin{hol}\begin{verbatim}
   let RATOR_CONV c t =
    (let rator,rand = dest_comb t in
     MK_COMB (c rator, REFL rand)) ? failwith `RATOR_CONV`

   let ABS_CONV c t =
    (let bv,body = dest_abs t in
     let bodyth = c body in
     MK_ABS (GEN bv bodyth)) ? failwith `ABS_CONV`
\end{verbatim}\end{hol}

\noindent The following
is an example session illustrating these immediate subterm conversions
(recalling that the expression $t_1\ml{+}t_2$
actually parses as $\ml{+}\ t_1\ t_2$).

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let t = "(\x.x+1)m + (\x.x+2)n";;
t = "((\x. x + 1)m) + ((\x. x + 2)n)" : term

#RAND_CONV BETA_CONV t;;
|- ((\x. x + 1)m) + ((\x. x + 2)n) = ((\x. x + 1)m) + (n + 2)

#RATOR_CONV (RAND_CONV BETA_CONV) t;;
|- ((\x. x + 1)m) + ((\x. x + 2)n) = (m + 1) + ((\x. x + 2)n)
\end{verbatim}\end{session}

\noindent Finally, the definitions of \ml{DEPTH\_CONV} and
\ml{TOP\_DEPTH\_CONV} are given below.

\begin{hol}
\index{DEPTH_CONV@\ml{DEPTH\_CONV}}
\index{TOP_DEPTH_CONV@\ml{TOP\_DEPTH\_CONV}}
\begin{verbatim}
   letrec DEPTH_CONV c t =
    (SUB_CONV (DEPTH_CONV c) THENC (REPEATC c)) t

   letrec TOP_DEPTH_CONV c t =
    (REPEATC c THENC
     (TRY_CONV
       (CHANGED_CONV (SUB_CONV (TOP_DEPTH_CONV c)) THENC
        TRY_CONV(c THENC TOP_DEPTH_CONV c)))) t

   letrec ONCE_DEPTH_CONV c t =
    (c ORELSEC (SUB_CONV (ONCE_DEPTH_CONV c))) t
\end{verbatim}\end{hol}
\index{conversions!functions for building|)}
\index{conversions!implementation of, in ML@implementation of, in \ML|)}

\noindent Note that the extra argument {\small\verb%t%} is needed to stop
these definitions looping (because \ML\ is a call-by-value language).
Note also that the actual definition of {\small\verb%ONCE_DEPTH_CONV%}
used in the system has been optimised to use failure to avoid
rebuilding unchanged subterms.

\section{Built in conversions}\label{built-in-conv}

Many conversions are predefined in \HOL; only those likely to be of
general interest are listed here.

\subsection{Generalized beta-reduction}\label{genbeta}

The conversion:

\begin{holboxed}\index{PAIRED_BETA_CONV@\ml{PAIRED\_BETA\_CONV}|pin}
\begin{verbatim}
   PAIRED_BETA_CONV : conv
\end{verbatim}\end{holboxed}

\noindent does
generalized beta-conversion of tupled lambda abstractions applied to
tuples.

Given the term:

\begin{hol}\begin{verbatim}
   "(\(x1, ... ,xn).t) (t1, ... ,tn)"
\end{verbatim}\end{hol}

\noindent \ml{PAIRED\_BETA\_CONV} proves that:

\begin{hol}\begin{verbatim}
   |- (\(x1, ... ,xn). t[x1,...,xn]) (t1, ... ,tn)  =  t[t1, ... ,tn]
\end{verbatim}\end{hol}

\noindent The conversion works for arbitrarily nested tuples.  For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#PAIRED_BETA_CONV "(\((a,b),(c,d)). [a;b;c;d]) ((1,2),(3,4))";;
|- (\((a,b),c,d). [a;b;c;d])((1,2),3,4) = [1;2;3;4]
\end{verbatim}\end{session}

\subsection{Arithmetical conversions}

The conversion:

\begin{holboxed}\index{ADD_CONV@\ml{ADD\_CONV}|pin}
\begin{verbatim}
   ADD_CONV : conv
\end{verbatim}\end{holboxed}

\noindent does addition by formal proof.
If $n$ and $m$ are numerals then
{\small\verb%ADD_CONV "%}$n\ $\ml{+}$\ m$\ml{"}
returns the theorem {\small\verb%|- %}$n\ $\ml{+}$\ m\ $\ml{=}$\  s$,
where $s$ is the numeral denoting the sum of $n$ and $m$.  For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#ADD_CONV "1 + 2";;
|- 1 + 2 = 3

#ADD_CONV "0 + 1000";;
|- 0 + 1000 = 1000

#ADD_CONV "101 + 102";;
|- 101 + 102 = 203
\end{verbatim}\end{session}

The next conversion decides the equality of natural number
constants.

\begin{holboxed}\index{num_EQ_CONV@\ml{num\_EQ\_CONV}|pin}
\begin{verbatim}
   num_EQ_CONV : conv
\end{verbatim}\end{holboxed}

\noindent If $n$ and $m$ are terms constructed from numeral constants
and the successor function \ml{SUC}, then:
{\small\verb%num_EQ_CONV "%}$n$\ml{=}$m$\ml{"}
returns:

\begin{hol}\begin{alltt}
   |- (\(n\)=\(m\)) = T   \(\mbox{if \(n\) and \(m\) represent the same number}\)
   |- (\(n\)=\(m\)) = F   \(\mbox{if \(n\) and \(m\) represent different numbers}\)
\end{alltt}\end{hol}

\noindent In addition, {\small\verb%num_EQ_CONV "%}$t\ $\ml{=}$\ t$\ml{"}
returns: {\small\verb%|- (%}$t$\ml{=}$t${\small\verb%) = T%}

\subsection{List processing conversions}

There are two useful built-in conversions for lists:

\begin{holboxed}
\index{LENGTH_CONV@\ml{LENGTH\_CONV}|pin}
\index{list_EQ_CONV@\ml{list\_EQ\_CONV}|pin}
\begin{verbatim}
   LENGTH_CONV : conv
   list_EQ_CONV: conv
\end{verbatim}\end{holboxed}

\ml{LENGTH\_CONV} computes the length of a list.
A call to:

\begin{hol}\begin{alltt}
   LENGTH_CONV "LENGTH[\(t\sb{1}\);\(\ldots\);\(t\sb{n}\)]"
\end{alltt}\end{hol}

\noindent generates the theorem:

\begin{hol}\begin{alltt}
   |- LENGTH [\(t\sb{1}\);\(\ldots\);\(t\sb{n}\)] = n
\end{alltt}\end{hol}

The other conversion, \ml{list\_EQ\_CONV}, proves or disproves the
equality of two lists, given
a conversion for deciding the equality of elements.
A call to:

\begin{hol}\begin{alltt}
   list_EQ_CONV \(conv\) "[\(u\sb{1}\);\(\ldots\);\(u\sb{n}\)] = [\(v\sb{1}\);\(\ldots\);\(v\sb{m}\)]"
\end{alltt}\end{hol}

\noindent returns: {\small\verb%|- ([%}$u_1$\m{;}$\ldots$\ml{;}$u_n$\ml{]\ =\ [}$v_1$\ml{;}$\ldots$\ml{;}$v_m$\ml{])\ =\ F} if:

\begin{myenumerate}
\item {\small\verb%~%}\ml{($n$=$m$)} or
\item \ml{$conv$} proves {\small\verb%|- (%}$u_i\ $\ml{=}$\ v_i$\ml{)\ =\ F}
for any $1\leq i \leq m$.
\end{myenumerate}

\noindent {\small\verb%|- ([%}$u_1$\m{;}$\ldots$\ml{;}$u_n$\ml{]\ =\ [}$v_1$\ml{;}$\ldots$\ml{;}$v_m$\ml{])\ =\ T} is returned if:

\begin{myenumerate}

\item \ml{($n$=$m$)} and \ml{$u_i$} is syntactically identical to
\ml{$v_i$} for $1\leq i \leq m$, or
\item \ml{($n$=$m$)} and \ml{$conv$} proves
{\small\verb%|- (%}$u_i$\ml{=}$v_i$\ml{)=T} for $1\leq i \leq n$.
\end{myenumerate}

\subsection{Simplifying {\tt let}-terms}\label{let-terms}
\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!simplification of}

A conversion for reducing {\tt let}-terms is now provided.

\begin{holboxed}\index{let_CONV@\ml{let\_CONV}|pin}
\begin{verbatim}
   let_CONV : conv
\end{verbatim}\end{holboxed}

\noindent Given a term:

\begin{hol}\begin{alltt}
   "let \(v\sb{1}\) = \(t\sb{1}\) and \(\cdots\) and \(v\sb{n}\) = \(t\sb{n}\) in \(t[v\sb{1},\ldots,v\sb{n}]\)"
\end{alltt}\end{hol}

\noindent \ml{let\_CONV} proves that:

\begin{hol}\begin{alltt}
   |- let \(v\sb{1}\) = \(t\sb{1}\) and \(\cdots\) and \(v\sb{n}\) = \(t\sb{n}\) in \(t[v\sb{1},\ldots,v\sb{n}]\)  =  \(t[t\sb{1},\ldots,t\sb{n}]\)
\end{alltt}\end{hol}

\noindent The \ml{$v_i$}'s can take any one of the following forms:

\begin{myenumerate}
\item Variables:    \ml{x} etc.
\item Tuples:   \ml{(x,y)}, \ml{(a,b,c)}, \ml{((a,b),(c,d))} etc.
\item Applications: \ml{f (x,y) z}, \ml{f x} etc.
\end{myenumerate}

\noindent Variables are just substituted for. With tuples, the substitution is
done component-wise, and function applications are effectively
rewritten in the body of the {\tt let}-term.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let_CONV "let x = 1 in x+y";;
|- (let x = 1 in x + y) = 1 + y

#let_CONV "let (x,y) = (1,2) in x+y";;
|- (let (x,y) = 1,2 in x + y) = 1 + 2

#let_CONV "let f x = 1 and f y = 2 in (f 10) + (f 20)";;
|- (let f x = 1 and f y = 2 in (f 10) + (f 20)) = 2 + 2

#let_CONV "let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))";;
|- (let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))) =
(((0 + 2) + 1) + 2) + 1

#CONV_RULE(DEPTH_CONV ADD_CONV)it;;
|- (let f x = x + 1 and g x = x + 2 in f(g(f(g 0)))) = 6

#let_CONV "let f x y = x+y in f 1";;  % NB: partial application %
|- (let f x y = x + y in f 1) = (\y. 1 + y)
\end{verbatim}\end{session}

\subsection{Skolemization}\index{Skolemization}

Two conversions are provided for a higher-order version of
Skolemization (using existentially quantified function variables
rather than first-order Skolem constants).

The conversion

\begin{holboxed}\index{X_SKOLEM_CONV@\ml{X\_SKOLEM\_CONV}|pin}
\begin{verbatim}
   X_SKOLEM_CONV : term -> conv
\end{verbatim}\end{holboxed}

\noindent takes a variable parameter, \ml{$f$} say, and
proves:

\begin{hol}\begin{alltt}
   |- (!\(x\sb{1}\) \(\ldots\) \(x\sb{n}\). ?\(y\). \(t[x\sb{1},\ldots,x\sb{n},y]\)  =  (?\(f\). !\(x\sb{1}\) \(\ldots\) \(x\sb{n}\). \(t[x\sb{1},\ldots,x\sb{n},f x\sb{1}\ldots x\sb{n}]\)
\end{alltt}\end{hol}

\noindent for any input term
\ml{!$x_1\ \ldots\ x_n$.\ ?$y$. $t[x_1,\ldots,x_n,y]$}.
Note that when $n=\ml{0}$, this
is equivalent to alpha-conversion:

\begin{hol}\begin{alltt}
  |- (?\(y\). \(t[y]\)) = (?\(f\). \(t[f]\))
\end{alltt}\end{hol}

\noindent and that the conversion fails if there is already a free
variable \ml{$f$} of the appropriate type in the input term. For example:

\begin{hol}\begin{alltt}
  X_SKOLEM_CONV "f:num->*" "!n:num. ?x:*. x = (f n)"
\end{alltt}\end{hol}

\noindent will fail.  The conversion \ml{SKOLEM\_CONV} is
like \ml{X\_SKOLEM\_CONV}, except that it
uses a primed variant of the name of the existentially quantified variable
as the name of the skolem function it introduces.  For example:

\begin{hol}\begin{alltt}
  SKOLEM_CONV "!x. ?y. P x y"
\end{alltt}\end{hol}

\noindent proves that:

\begin{hol}\begin{alltt}
  |- ?y. !x. P x (y x)
\end{alltt}\end{hol}

\subsection{Quantifier movement conversions}
\index{quantifiers!conversions for}
\index{conversions!quantifier movement}

A complete and systematically-named set of conversions for moving quantifiers
inwards and outwards through the logical connectives {\small\verb%~%},
{\small\verb%/\%}, {\small\verb%\/%}, and {\small\verb%==>%} is provided.
The naming scheme is based on the following
atoms:

\begin{hol}\begin{alltt}
   <\(quant\)> := FORALL | EXISTS
   <\(conn\)>  := NOT | AND | OR | IMP
   [\(dir\)]   := LEFT | RIGHT          \({\it (optional)}\)
\end{alltt}\end{hol}

The conversions for moving quantifiers inwards are called:

\begin{hol}\begin{alltt}
   <\(quant\)>_<\(conn\)>_CONV
\end{alltt}\end{hol}

\noindent where the quantifier \ml{<$quant$>} is to be moved inwards
through \ml{<$conn$>}.

The conversions for moving quantifiers outwards are called:

\begin{hol}\begin{alltt}
   [\(dir\)]_<\(conn\)>_<\(quant\)>_CONV
\end{alltt}\end{hol}

\noindent where \ml{<$quant$>} is to be moved outwards
through \ml{<$conn$>}, and the optional
\ml{[$dir$]} identifies which operand (left or right) contains the quantifier.
The complete set is:

\begin{hol}\begin{verbatim}
   NOT_FORALL_CONV    |- ~(!x.P) = ?x.~P
   NOT_EXISTS_CONV    |- ~(?x.P) = !x.~P
   EXISTS_NOT_CONV    |- (?x.~P) = ~!x.P
   FORALL_NOT_CONV    |- (!x.~P) = ~?x.P
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   FORALL_AND_CONV         |- (!x. P /\ Q) = (!x.P) /\ (!x.Q)
   AND_FORALL_CONV         |- (!x.P) /\ (!x.Q) = (!x. P /\ Q)
   LEFT_AND_FORALL_CONV    |- (!x.P) /\ Q = (!x'. P[x'/x] /\ Q)
   RIGHT_AND_FORALL_CONV   |- P /\ (!x.Q) = (!x'. P /\ Q[x'/x])
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   EXISTS_OR_CONV          |- (?x. P \/ Q) = (?x.P) \/ (?x.Q)
   OR_EXISTS_CONV          |- (?x.P) \/ (?x.Q) = (?x. P \/ Q)
   LEFT_OR_EXISTS_CONV     |- (?x.P) \/ Q = (?x'. P[x'/x] \/ Q)
   RIGHT_OR_EXISTS_CONV    |- P \/ (?x.Q) = (?x'. P \/ Q[x'/x])
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   FORALL_OR_CONV
     |- (!x.P \/ Q) = P \/ !x.Q          [x not free in P]
     |- (!x.P \/ Q) = (!x.P) \/ Q        [x not free in Q]
     |- (!x.P \/ Q) = (!x.P) \/ (!x.Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   OR_FORALL_CONV
     |- (!x.P) \/ (!x.Q) = (!x.P \/ Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   LEFT_OR_FORALL_CONV    |- (!x.P) \/ Q = !x'. P[x'/x] \/ Q
   RIGHT_OR_FORALL_CONV   |- P \/ (!x.Q)  = !x'. P \/ Q[x'/x]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   EXISTS_AND_CONV
     |- (?x.P /\ Q) = P /\ ?x.Q          [x not free in P]
     |- (?x.P /\ Q) = (?x.P) /\ Q        [x not free in Q]
     |- (?x.P /\ Q) = (?x.P) /\ (?x.Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   AND_EXISTS_CONV
     |- (?x.P) /\ (?x.Q) = (?x.P /\ Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   LEFT_AND_EXISTS_CONV    |- (?x.P) /\ Q = ?x'. P[x'/x] /\ Q
   RIGHT_AND_EXISTS_CONV   |- P /\ (?x.Q)  = ?x'. P /\ Q[x'/x]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   FORALL_IMP_CONV
     |- (!x.P ==> Q) = P ==> !x.Q          [x not free in P]
     |- (!x.P ==> Q) = (?x.P) ==> Q        [x not free in Q]
     |- (!x.P ==> Q) = (?x.P) ==> (!x.Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   LEFT_IMP_FORALL_CONV   |- (!x.P) ==> Q = !x'. P[x/'x] ==> Q
   RIGHT_IMP_FORALL_CONV  |- P ==> (!x.Q) = !x'. P ==> Q[x'/x]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   EXISTS_IMP_CONV
     |- (?x.P ==> Q) = P ==> ?x.Q          [x not free in P]
     |- (?x.P ==> Q) = (!x.P) ==> Q        [x not free in Q]
     |- (?x.P ==> Q) = (!x.P) ==> (?x.Q)   [x not free in P or Q]
\end{verbatim}\end{hol}

\begin{hol}\begin{verbatim}
   LEFT_IMP_EXISTS_CONV   |- (?x.P) ==> Q = !x'. P[x/'x] ==> Q
   RIGHT_IMP_EXISTS_CONV  |- P ==> (?x.Q) = ?x'. P ==> Q[x'/x]
\end{verbatim}\end{hol}

\section{Rewriting tools}
\label{avra3}

The rewriting tool\index{rewriting!as based on conversions|(}
\ml{REWRITE\_RULE} was introduced
in Chapter~\ref{derived-rules}.
There are also rewriting conversion like \ml{REWRITE\_CONV}.
All of the various rewriting tools provided in \HOL\
are implemented by use of conversions.
Certain new tools could also be built in a similar way.

The rewriting primitive in \HOL\ is \ml{REWR\_CONV}:

\begin{holboxed}
\index{REWR_CONV@\ml{REWR\_CONV}|pin}
\begin{verbatim}
   REWR_CONV : thm -> conv
\end{verbatim}\end{holboxed}

\noindent $\ml{REWR\_CONV (}\Gamma\ml{ |- }u \ml{=}v\ml{) }t$ evaluates to a
theorem $\Gamma\ml{ |- }t\ml{=}t'$
if $t$ is an instance (by type and/or variable instantiation)
of $u$ and $t'$ is the corresponding instance of $v$.
The first argument to \ml{REWR\_CONV} can be quantified.
Below is an illustration.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#REWR_CONV ADD1 "SUC 0";;
Theorem ADD1 autoloaded from theory `arithmetic`.
ADD1 = |- !m. SUC m = m + 1

|- SUC 0 = 0 + 1
\end{verbatim}\end{session}

\noindent All subterms of $t$ can be rewritten according to
an equation $th$ using\index{rewriting!use of DEPTH_CONV in@use of \ml{DEPTH\_CONV} in}\index{DEPTH_CONV@\ml{DEPTH\_CONV}!use of, in rewriting tools}

\[ \ml{DEPTH\_CONV(REWR\_CONV}\;\;th\ml{)}  \]

\noindent as shown below. The function \ml{"PRE"} is the usual
predecessor function.

\begin{session}\begin{verbatim}
#DEPTH_CONV (REWR_CONV ADD1) "SUC(SUC 0) = PRE(SUC 2)";;
|- (SUC(SUC 0) = PRE(SUC 2)) = ((0 + 1) + 1 = PRE(2 + 1))
\end{verbatim}\end{session}

In itself, this is not a very useful rewriting tool, but a collection
of others have been developed for use in \HOL.
All of the rewriting tools are, in fact, logically derived, and are
based on conversions similar to \ml{DEPTH\_CONV}.
They have been optimized in various ways, so their
implementation is in some cases rather complex and is not given here.
The conversions, rules and tactics for rewriting all take a list of
theorems\index{theorems, in HOL logic@theorems, in \HOL\ logic!as rewrite rules}
to be used as rewrites.
The theorems in the list need not be in simple equational form
(\eg\ a conjunction of equations is permissible); but are
converted to equational form
automatically (and internally).
(For example, a conjunction of equations is split into
its constituent conjuncts.)  There are also a number
of standard equations (representing common tautologies) held in
the \ML\ variable
\ml{basic\_rewrites},\index{basic-rewrites@\ml{basic-rewrites}} and these
are used by some of the rewriting tools. All the
built-in rewriting tools are listed below, for
reference, beginning with the rules.
(All are fully
described in \REFERENCE.)

The prefix `\ml{PURE\_}' indicates
that the built-in equations in \ml{basic\_rewrites} are not used,
(\ie\ only those
given explicitly are used).  The prefix `\ml{ONCE\_}' indicates that the
tool
makes only one rewriting pass through the expression
(this is useful to avoid divergence). It is based on \ml{ONCE\_DEPTH\_CONV},
while the other tools traverse using \ml{TOP\_DEPTH\_CONV}.

The rewriting converions are:

\begin{hol}\begin{verbatim}
   REWRITE_CONV                : thm list -> conv
   PURE_REWRITE_CONV           : thm list -> conv
   ONCE_REWRITE_CONV           : thm list -> conv
   PURE_ONCE_REWRITE_CONV      : thm list -> conv
\end{verbatim}\end{hol}

The basic rewriting rules are:

\begin{hol}\begin{verbatim}
   REWRITE_RULE                      : thm list -> thm -> thm
   PURE_REWRITE_RULE                 : thm list -> thm -> thm
   ONCE_REWRITE_RULE                 : thm list -> thm -> thm
   PURE_ONCE_REWRITE_RULE            : thm list -> thm -> thm
\end{verbatim}\end{hol}

\noindent The prefix `\ml{ASM\_}'
indicates that the rule rewrites using the assumptions
of the theorem as rewrites.

\begin{hol}\begin{verbatim}
   ASM_REWRITE_RULE                  : thm list -> thm -> thm
   PURE_ASM_REWRITE_RULE             : thm list -> thm -> thm
   ONCE_ASM_REWRITE_RULE             : thm list -> thm -> thm
   PURE_ONCE_ASM_REWRITE_RULE        : thm list -> thm -> thm
\end{verbatim}\end{hol}

\noindent The prefix `\ml{FILTER\_}'
indicates that the rule only rewrites with
those assumptions of the theorem satisfying the predicate supplied.

\begin{hol}\begin{verbatim}
   FILTER_ASM_REWRITE_RULE           : (term -> bool) -> thm list -> thm -> thm
   FILTER_PURE_ASM_REWRITE_RULE      : (term -> bool) -> thm list -> thm -> thm
   FILTER_ONCE_ASM_REWRITE_RULE      : (term -> bool) -> thm list -> thm -> thm
   FILTER_PURE_ONCE_ASM_REWRITE_RULE : (term -> bool) -> thm list -> thm -> thm
\end{verbatim}\end{hol}

\noindent Tactics\index{rewriting!list of tactics for|(} are introduced in Chapter~\ref{tactics-and-tacticals},
but are listed here for reference.
The tactics corresponding to the above rules are the following:

\begin{hol}\begin{verbatim}
   REWRITE_TAC                       : thm list -> tactic
   PURE_REWRITE_TAC                  : thm list -> tactic
   ONCE_REWRITE_TAC                  : thm list -> tactic
   PURE_ONCE_REWRITE_TAC             : thm list -> tactic
\end{verbatim}\end{hol}

\noindent The prefix `\ml{ASM\_}'
indicates that the tactic rewrites using the assumptions
of the goal as rewrites.

\begin{hol}\index{ASM_REWRITE_TAC@\ml{ASM\_REWRITE\_TAC}}
\begin{verbatim}
   ASM_REWRITE_TAC                   : thm list -> tactic
   PURE_ASM_REWRITE_TAC              : thm list -> tactic
   ONCE_ASM_REWRITE_TAC              : thm list -> tactic
   PURE_ONCE_ASM_REWRITE_TAC         : thm list -> tactic
\end{verbatim}\end{hol}

\noindent The prefix `\ml{FILTER\_}'
indicates that the tactic only rewrites with
those assumptions of the goal satisfying the predicate supplied.

\begin{hol}\begin{verbatim}
   FILTER_ASM_REWRITE_TAC            : (term -> bool) -> thm list -> tactic
   FILTER_PURE_ASM_REWRITE_TAC       : (term -> bool) -> thm list -> tactic
   FILTER_ONCE_ASM_REWRITE_TAC       : (term -> bool) -> thm list -> tactic
   FILTER_PURE_ONCE_ASM_REWRITE_TAC  : (term -> bool) -> thm list -> tactic
\end{verbatim}\end{hol}
\index{rewriting!as based on conversions|)}
\index{rewriting!list of tactics for|)}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "description"
%%% End:


\section{Results}
The first model, shown in Figure \ref{fig:ESN1}, we trained with 1000 hours of
historical data. The blue line is a snapshot of historical demand data. The
dashed line indicates the end of the model training and the beginning of the
model prediction. The red line is the prediction made by the \gls{ESN}. By
inspection, the \gls{ESN} successfully predicted a general
trend but had difficulty with accuracy at the desired hourly resolution.

\begin{figure}[h]
  \centering
  \includegraphics[width=\columnwidth]{scaled_esn_network2.png}
  \caption{A simple ESN with a prediction of 100 hours into the future after
  training on 1000 hours of historical data.}
  \label{fig:ESN1}
\end{figure}

The second model, shown in Figure \ref{fig:ESN2}, we trained with the same
dataset as before and extended the training to 3500 hours of
historical data. This \gls{ESN} made better predictions than the first model
and is most likely due to longer training. The effect of training
length on model accuracy will be explored in future work.

\begin{figure}[h]
  \centering
  \includegraphics[width=\columnwidth]{scaled_esn_network.png}
  \caption{A simple ESN with a prediction of 100 hours into the future. After
  training on 3500 hours of historical data.}
  \label{fig:ESN2}
\end{figure}

Both models use training data with an hourly resolution. It is possible
that data at the 15-minute or 30-minute timescale will improve prediction
accuracy.


\section{Introduction}

Algorand is a platform for secure and decentralized digital currencies and transactions~\cite{Micali2019}. At the core of the Algorand platform is the Algorand consensus protocol~\cite{Chen2018,Chen2019}, a pure Proof-of-Stake (PoS) protocol that provides efficient, secure, and scalable operation while remaining decentralized. The basic idea in pure PoS is to make the security of the system dependent only on how honest the majority of its asset owners are, without having to rely on any specific subset of participants or having to lock up assets and penalize users.

Distributed consensus protocols are complex, and subject to general limitations even in non-adversarial settings~\cite{Fischer1985}. They typically involve interaction between many independent and potentially untrusted nodes in a large network where asynchronous and failure-prone communication is the norm rather than the exception.
Algorand, in particular, specifies not only the node-local behavior, but also deals with message propagation and delivery, to be able to address two types of attacks: (1) attacks that corrupt participating nodes so that they no longer follow the protocol, e.g., to produce fake transactions or to cast votes for the wrong transactions, and (2) attacks on the underlying network in which the system is deployed, for instance by intercepting, manipulating, and delaying messages. When an attacker gains full control of message delivery in a network, the network is said to be partitioned. Unlike Algorand, most existing consensus protocols do not consider what happens when a network is partitioned or after the network recovers from a partition.

Consensus systems are now the backbone of large cryptocurrencies worth billions of US dollars. This means that vulnerabilities can have catastrophic consequences. Consequently, ensuring correctness and resilience against malicious behavior while designing a consensus protocol is an important and challenging task. Having strong guarantees of correctness and developing a thorough understanding of protocol assumptions can significantly reduce the risk of catastrophic events.

In this report, we describe our effort to model and verify the Algorand consensus protocol using deductive verification. In this approach, systems are modeled and specified inside expressive formal logical systems, and verified in a similar way to how mathematicians prove theorems - in principle by elaborating proofs of statements step-by-step. In the spectrum of formal verification techniques, deductive verification provides the strongest guarantees, and thus the highest degree of trustworthiness, but at the expense of being the most demanding. Specifically, we use Coq~\cite{CoqArt}, a proof assistant developed for more than 30 years and used in several large-scale verification projects~\cite{Leroy2009,Woos2016}.

We developed a model of Algorand consensus in Coq in the form of a \emph{transition system}, over which we stated and ultimately proved the \emph{asynchronous safety} property: no two honest nodes certify two different blocks, even when the adversary has complete control of message delivery in the network. We also precisely pin down the assumptions under which the safety theorem holds. Beyond the safety theorem, we intend for this formalization to lay the foundation for further modeling and verification efforts related to the Algorand consensus protocol.

%Coq is developed for more than 30 years and very well supported. Coq has previously been used to develop a verified C compiler, formally prove mathematical results such as the four-color theorem, and verify distributed protocols (see, for example, our previous Casper formal verification effort for the Ethereum Foundation).

%We developed a model of the Algorand consensus protocol in Coq, a proof assistant based on type theory~\cite{}

%and proved a slew of its properties that we then used to ultimately show the asynchronous safety property: no two honest nodes certify two different blocks, even when the adversary has complete control of message delivery in the network.

%This report describes our effort to model the protocol and specify and verify its properties. Moreover, we describe the formalization of the assumptions under which the safety theorem holds. Beyond the safety theorem, we intend for this formalization in Coq to lay the foundation for further future modeling and verification efforts of the Algorand consensus protocol.

%A key characteristic of Algorand is that it almost never forks. Forking happens when consensus on a single block for a round is not reached and multiple candidate blocks are available for that round. Having different subsets of nodes decide on appending different blocks to the chain means that transactions appearing in these conflicting blocks are not finalized since only one of these blocks will eventually belong to the canonical chain, the chain that is deemed most accepted by the nodes (there are different methods for deciding the canonical chain, e.g. the longest chain in Bitcoin). Algorand avoids this problem by design: at most one block can receive the majority of votes in a round. This property implies that once a block of transactions appear in the chain, they can immediately be considered final. This has the potential of allowing extremely high levels of scalability. This no-forking property is referred to as Asynchronous safety, which states that no two honest nodes will certify two different blocks, even when the adversary has complete control of message delivery in the network.


\documentclass{article}

\usepackage{amssymb, amsmath, verbatim}
\usepackage{graphicx}

\begin{document}
\title{Machine Translation HW2}
\author{Gary Qian (gqian1)}
\maketitle
$\ $\\
Code: https://github.com/GaryQian/MachineTranslation\\
Commit: dccc5ac4033075fe256290c4f8c663e442c2f9b2

\section{IBM Model 1}

I implemented an IBM Model 1 aligner, which was fairly straightforwards. It used a standard EM algorithm. I initialized the starting probabilities of each parings of words with a uniform distribution over all possible pairings, and ran the algorithm for 30 iterations.\\

IBM Model 1 attempts to optimize the parameters as defined by:

\[
\theta = argmax_\theta \Pi^N_{n=1}p_\theta (f^n, a^n | e^n)
\]

Where theta are the probability parameters, N is the number of sentences, and $p_\theta$ is the probability a French word f occurs given an alignment set a and English sentence e. We attempt to approximate this using an EM algorithm because the problem cannot be solved closed form. Thus, for each iteration, we will keep track of the instances each (f,e) word pairing occurs, as well as the total number of times the english word is paired with any french word. This will provide us with an updated p(f|e) by dividing: $count(f|e) / count(e)$. More formally, during the E step, we will find:

\[
p(a_i = j|f,e) = \frac{p(f_i|e_i)}{\sum^J_{a_i=1}p(f_i|e_{a_i})}
\]

We then update in the M step:

\[
p(f|e) = \frac{p(a_i = j|f,e)}{p(e)}
\]

To output the final results, I simply found the highest probability pairing for each french word.\\

In my implementation, I decided to use a uniform distribution as initialization over all of the possible word pairings.\\

I found that my IBM Model 1 implementation produces an AER of 0.37-0.36 depending on how many iterations I ran. I experimented between 5 and 150 iterations and found that the AER lower bound for my implementation to be around 0.35.

\section{Two-way independent (E to F and F to E) Model}

I implemented a two way model that made use of two independently trained IBM Model 1 instances. The model had a double EM algorithm with separate probability distributions for each direction.\\

Let the $p_1(f|e)$ and $p_2(e|f)$ represent the two trained models using IBM Model 1. The final probability of any pairing was simply the product of these:

\[
p_{final}=p_1(f|e)p_2(e|f)
\]

This improved the AER performance significantly, dropping the AER down to 0.33-0.32 with iterations ranging from 5 to 150. The combined model outperformed a single IBM Model 1 significantly. However, the run time of this model became much longer.

\section{Diagonal Weighing}

I then applied a simple diagonal weighing function to the distributions to highly favor alignments close to the diagonal. Let $\lambda = $ a constant found through experimentation. My new prediction function became:

\[
p_{final}=p_1(f|e)p_2(e|f) (\frac{1}{|i-j| / \lambda})
\]

I found though experimentation that a constant $\lambda = 8$ outperformed dynamically sized lambdas. The results of this were slight but noticeable, bringing the double IBM Model 1 from above's performance AER to about 0.32-0.315

\section{Dice Coefficient Initialization}

Previously, all of the probability distributions were initialized uniformly, however, I believed that there could be a fast and much better solution to determine the intitalization of the probability distribution. I attempted to apply the Dice Coefficient as starting points for the probability distribution. The idea is that the results from Dice are not completely accurate, so by starting with Dice's Coefficient, we can get to a semi-trained state that would have otherwise taken much longer using a uniformly initialized EM algorithm.\\

After initialization, we continue to follow the EM algorithm, which is now starting at a state that is of AER 0.6 instead of a uniform distribution.

\section{Two way IBM Model 2}

To improve upon the results of the previous improvements, I implemented IBM Model 2, which now takes into account the length of the sentence as well as the probability of the word pairs in combination with the lengths of the sentences. This should improve upon the results of IBM Model 1. I also integrated my diagonalization improvements since that is only applied at the end and was relatively simple to add.\\

In Model 2, we follow a similar EM algorithm as Model 1 with a few key changes. We define $q(j|i,l_k,m_k)$ to be the probability the word pairing of i and j occur with sentences of length l and m, with k being the index of the sentence pair in the corpus. The probabilites found with IBM Model 1 are now represented as $t(f|e)$ to remain consistent with the work of Michael Collins which formed the basis of my Model 2 implementation. In the E step, we find a $\delta(k,i,j)$ "count weight" defined as:

\[
\delta(k,i,j) = \frac{q(j|i,l_k,m_k)t(f^k_i,e^k_j)}{\sum^{l_k}_{j=0}q(j|i,l_k,m_k)t(f^k_i,e^k_j}
\]

We then increment the counts by $\delta$ for each of the following counters:\\
$c(e,f), \ c(e),\ c(j|i,l,m),\ c(i,l,m)$\\

In the M step, we define the new q and t as follows:

\[
t' = \frac{c(e,f)}{c(e)}
\]

\[
q' = \frac{c(j|i,l,m)}{c(i,l,m)}
\]

My model 2 (single instance) resulted in a AER improvement to 0.301\\

Building upon the results of my experimentation above, I also implemented a double IBM Model 2 system that trains 2 models, one f-e and the other e-f and combined them in a similar fashion as above. The results were improvements on the models alone and brought the AER even lower to 0.262.

\end{document}


\documentclass[12pt]{article}

%\usepackage{fullpage}
%\usepackage[top=1in, bottom=1in, left=1in, left=1in, right=1in]{geometry}
\usepackage[margin=1in, paperwidth=8.5in, paperheight=11in]{geometry}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{amsmath}
\usepackage{mathtools}

\newcommand{\argmin}{\operatornamewithlimits{argmin}}

\begin{document}

\title{UT Machine Learning: HomeWork1}
\author{Mohamad amin Katebsaber}
\date{\today}
\maketitle

\section{Class Summary}

Machine learning is a field of study in which we design proper instructions (algorithms) for computers, enabling them to learn for themselfs. Generally speaking based on the way computers improve using each algorithm, we can divide them into three types:

\begin{enumerate}
\item Supervised Learning
\item Unsupervised Learning
\item Reinforcement Learning
\end{enumerate}

\subsection{Supervised Learning}
Supervised Learning algorithms can be considered as functions that map $input$ space to $output$ space. In this manner, $input$ space ($X$) is a set of examples, each of which consisting one or several features that the $output$ space ($Y$) depends upon. Here the objective is to best approximate the mapping function $(f)$ such that new datapoints ($x\in\mathcal{X}$) can be accurately mapped to $output$ space $Y$.Generaly there are two types of Supervised Learning based on the $output$ space:
\begin{enumerate}
\item Regression: If the $output$ space is numerical (continuous). i.e: Prdiction of Housing price in Tehran based on house area and number of rooms.
\item Classification: If the $output$ space is categorical (descrete). i.e: Identification of digits (Predicting a label between 0-9) based on pixel intensities.
\end{enumerate}

In Regression setting we aim to predict a real value on the calculated hyperplane but in Classification setting the calculated hyperplane is used as a boundary to devide input space to several zones (Figure \ref{fig:reg_clas}).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.4\linewidth}
    \includegraphics[width=\linewidth]{./images/regression.png}
    \caption{Regression}
  \end{subfigure}
  \begin{subfigure}[b]{0.4\linewidth}
    \includegraphics[width=\linewidth]{./images/classification.png}
    \caption{Classification}
  \end{subfigure}
  \caption{Regression vs Classification}
  \label{fig:reg_clas}
\end{figure}

\subsection{Unsupervised Learning}
Unsupervised Learning is referred to a class of algorithms capable of learning previously unknows patterns among $input data$ (there is no defined label for data points). These kind of algorithms are generally used for addressing two types of problem settings:
\begin{enumerate}
\item Clustering: In this setting we aim to group data space based upon features that represent the $input data$. Clustering is used when the $input$ space is discrete (Figure \ref{fig:tsne}-a).
\item Dimensionality reduction: In Machine Learning problems there are often too many features to be considered for defining the $input$ space. In many cases most of these features are by some means correlated and therefore redundant. Dimensionality reduction is the process of reducing the number of features decribing $input$ space based on afformentioned correlation, resulting in model's simplicity. This technique is often used when our $input$ space is continuous (Figure \ref{fig:tsne}-b).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.4\linewidth}
  	\centering
    \includegraphics[width=\linewidth]{./images/clustering.png}
    \caption{Clustering}
  \end{subfigure}
  \begin{subfigure}[b]{0.4\linewidth}
    \includegraphics[width=\linewidth]{./images/reduction.jpg}
    \caption{Dimensionality reduction}
  \end{subfigure}
  \caption{Dimentionality Reduction}
  \label{fig:tsne}
\end{figure}
\end{enumerate}

As an example for Uunsupervised Learning we can name Blind Source Separation. Assume that you are in cocktail party where lots of individuals are talking together. In this house there are four microphones recording conversations from different points in the room. It can be concluded that depending on the position of each microphone and its proximity to a conversation, each microphone will record a specific signal. Problem of interest is about separation of signals related to each conversation from other signals (Figure \ref{fig:bss}).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.4\linewidth}
    \includegraphics[width=\linewidth]{./images/bss.jpg}
  \end{subfigure}
  \caption{Blind source separation}
  \label{fig:bss}
\end{figure}

The idea behind Blind Source Separation (BSS) can be generalized to a spectrum of problems in Bioinformatics. One of the Bioinformatics problems that can be framed in BSS settings is separation of Cancer Signatures. We know that the root of any cancer is some kind of somatic mutation. There are only six distinct base substitutions (C to A, C to G, C to T, T to A, T to C, T to G) but too many cancer types. Hence it can be concluded that accumulation of these substitutions is the key difference to cause different cancers.

Analysis of the total number of each base substitution type in cancerous genoms reveals that for example in skin cancers, C to T mutations are in majority which makes sense because we know from experiment that UV causes C to T mutations. Considering the six types of base substitutions in the context of immediate previous and next base in the sequence (for example C to T mutation when previous base is A and next base is T) we would have 96 different classes of mutations. Different distributions of mutation classes are considered as cancer signatures (Figure \ref{fig:sig}).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.5\linewidth}
    \includegraphics[width=\linewidth]{./images/sig.png}
    \caption{Mutation Classes}
  \end{subfigure}
  \begin{subfigure}[b]{0.5\linewidth}
    \includegraphics[width=\linewidth]{./images/sig2.png}
    \caption{A cancer signature}
  \end{subfigure}
  \caption{From Mutation clases to cancer signatures}
  \label{fig:sig}
\end{figure}

Now our challenge is to find the contribution of each cancer signature to a specific cancer type when our $input$ data is the accumulated version of base substitutions in a sequence extracted from a cancer cell.

\subsection{Reinforcement Learning}
The key idea behind Reinforcement Learning (RL) algorithms is the game of "carrot and sticks". In this context we have an agent (here our RL algorithm) taking some actions and getting rewards (and of course punishments!) from the interpreter who observes the game state consequent to taking that action from the agent. Think of a mouse (agent) in a maze (environment) trying his best (taking actions) to avoid traps (major and minor punishments) and getting to cheese (reward). In this case the designer of the game is the interpreter who either automatically or manually punishes or rewards the mouse (Figure \ref{fig:rl}).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.7\linewidth}
    \includegraphics[width=\linewidth]{./images/rl.png}
  \end{subfigure}
  \caption{Reinforcement Learning}
  \label{fig:rl}
\end{figure}

After understanding the basics of Machine Learning it is time to roll up our sleeves and deep dive into the math behind the stuff.

\subsection{Linear regression}
Assume that we have some training data:
$$(x[1],y[1])$$
$$(x[2],y[2])$$
$$(x[3],y[3])$$
{\centering...\par}
$$(x[n],y[n])$$
where $n$ denotes the number of our samples. We want to create a model which passes through all our data points and given a x can output its respective y. In a context where the data is 2-dimensional we can fit a simple line to our sample points:$$y=\beta_0 + \beta_1 x$$
This model consists of two parameters $\beta_0 and \beta_1$ which their value depend on our data. To find the best parameters, we have to have some notion of accuracy to evaluate our correctness (or in Machine Learning notation Falseness!). To address this problem we can define a function which takes our data and model (parameters) as input and outputs a number indicating how off we are:$$L(\beta_0,\beta_1, data) = \frac{1}{2} \sum_{i=1}^{n} y[i]-(\beta_0 + \beta_1 x[i])^2$$
Our objective is to find $\beta_0$ and $\beta_1$ so our Loss function ($L$) is at its minimum. Mathematically we can frame our objective as:
$$\beta = \beta_0, \beta_1$$
$$ \hat{\beta_0}, \hat{\beta_1} = \argmin_\beta {L}(\beta)$$
In order to get to find best candidate for $\beta$, we can use several methods:
\begin{enumerate}
\item Gradient descent
\item Analytic method
\end{enumerate}
\subsubsection{Gradient descent}
Gradient descent is an iterative algorithm for finding best $\beta$. We start with an initial guess for $\beta$ and use the following equations to update our initial guess:
$$\beta_0 \coloneqq \beta_0 - \alpha \frac{\partial L(\beta)}{\partial \beta_0}$$
$$\beta_1 \coloneqq \beta_1 - \alpha \frac{\partial L(\beta)}{\partial \beta_1}$$
In equations above, $\alpha$ is a parameter called "Learning rate" which we will discuss later in this course. For the partial derivatives in our case we have:
$$\frac{\partial L(\beta)}{\partial \beta_0} = \frac{1}{2} \sum_{i=1}^{n} (2) (-1) (y[i]-\beta_0 + \beta_1 x[i])$$
$$\frac{\partial L(\beta)}{\partial \beta_1} = \frac{1}{2} \sum_{i=1}^{n} (2) (-x[i]) (y[i]-\beta_0 + \beta_1 x[i])$$
So the complete form of gradient descent algorithm would be:
$$\beta_0 \coloneqq \beta_0 + \alpha \sum_{i=1}^{n} (y[i]-\beta_0 + \beta_1 x[i])$$
$$\beta_1 \coloneqq \beta_1 + \alpha \sum_{i=1}^{n} (x[i]) (y[i]-\beta_0 + \beta_1 x[i])$$

This flavor of gradient descent is called "Batch". In order to update our parameters in this form, we need to go through all of our data once. This task is computation intensive and as our data grows, It would be almost impossible to compute parameters using this equations. To face this challenge we introduce another form of gradient descent called "Stochastic":
$$\beta_0 \coloneqq \beta_0 + \alpha (y[i]-\beta_0 + \beta_1 x[i])$$
$$\beta_1 \coloneqq \beta_1 + \alpha (x[i]) (y[i]-\beta_0 + \beta_1 x[i])$$

General notion of Stochastic Gradient Descent (SGD) is to update our parameters for each data point. In fact this alteration adds some noise to the minimization process but on the other hand, makes it possible to actually compute parameters using the formula (Figure \ref{fig:sgd}).
\begin{figure}[h!]
  \centering
  \begin{subfigure}[b]{0.5\linewidth}
    \includegraphics[width=\linewidth]{./images/sgd.png}
  \end{subfigure}
  \caption{Batch vs Stochastic Gradient Descent}
  \label{fig:sgd}
\end{figure}
\end{document}


\section{Creating topologies from sets}


\documentclass{beamer}
\usefonttheme[onlymath]{serif}
\usepackage[english]{babel}							%For internationalization
\usepackage[utf8]{inputenc}							%For character encoding
\usepackage{amsmath}								%For mathematical typesetting
\usepackage{amssymb}								%For mathematical typesetting
\usepackage{graphicx}								%For handling graphics

\newcommand{\be}{\begin{equation}}
\newcommand{\bea}{\begin{equation*}}
\newcommand{\ben}[1]{\begin{equation}\label{#1}}
\newcommand{\ee}{\end{equation}}
\newcommand{\eea}{\end{equation*}}
\newcommand{\aq}{\overset{\sim}{q}}

\title
{An Introduction to Discontinuous Galerkin Methods}
\subtitle{Module 2: A Simple 1D DG Solver}
\author[Bevan] % (optional, for multiple authors)
{J.~Bevan}
\institute[UMass Lowell]
{
  Department of Mechanical Engineering, Grad Student\\
  University of Massachusetts at Lowell
}
\date[Fall 2014]
{}
\subject{Discontinuous Galerkin}

\begin{document}
\frame{\titlepage}
\frame{\frametitle{Module 2: A Simple 1D DG Solver}\tableofcontents}

%NEW SECTION
\section{Linear Solution Approximation}
\subsection{Definition}
\frame{\frametitle{\textbf{\secname}: \subsecname}
\begin{itemize}
\item The exact sol'n $q(x)$ is not usually obtainable, the best we can do is an approximate sol'n $\aq(x)$
\item We can think of the approximate solution belonging to some finite vector space $\aq \in V_h$, this vector space must have a set of basis vectors $\psi$ such that
\be \aq(x) = \sum_{i=0}^M a_i \psi_i(x) \ee
\item We will choose $V_h$ in this simple example to be the space of all polynomials of order 1 and less: $\mathbb{P}^M, M=1$
\item Because $M=1$, we must have two basis functions. We shall choose two ramp functions, let's see what they look like...
\end{itemize}
}

\subsection{Bases and Resultant Approximation}
\frame{\frametitle{\subsecname}
\begin{itemize}
\item Basis funs (blue) $\psi_0(x) = 1-x$ and (red) is $\psi_1(x)=x$. Resultant approximation is linear: $\aq(x) = a_0(1-x) + a_1(x)$
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=4in]{linearBasis.PNG}
\end{figure}
}

%NEW SECTION
\section{Test Function Choice (Galerkin)}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item We have now defined our approximation space, but what about the weighting function in the weak form: $\phi$?
\item We will discuss this in greater detail, but as in a FEM, DG chooses the space of weighting functions to be the same as the approximation space, $\phi \in V_h$
\item This means our weighting functions are ramp functions as well $\phi_0(x) = \psi_0(x), \phi_1(x) = \psi_1(x)$
\item In order to solve for the approximation, our weak form sol'n must be satisfied for \textit{each} weighting function.
\end{itemize}
}

%NEW SECTION
\section{Upwind Flux}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item If our flow is incompressible and the global domain is 1D, we must have a constant flow with velocity $c$ everywhere.
\item Let's assume that $c>1$, the numerical flux is then $c\aq^-$ at all boundaries
\item Note that there are two distinct numerical fluxes for element $k$, on the left it is $cq^-(x_L)$ and the right $cq^-(x_R)$ , where $q^-(x_L)$ doesn't necessarily equal $q^-(x_R)$
\end{itemize}
\begin{figure}
\centering
\includegraphics[width=1.5in]{qBoundary.PNG}
\end{figure}
}

%NEW SECTION
\section{Mass Matrix}
\frame{\frametitle{\textbf{\secname}}
\be \int_k\! \frac{\partial \aq}{\partial t} \,\phi_j \,dx +
	[f(\aq)\phi_j] \Big\rvert_{x_L}^{x_{R}} -
	\int_k\! f(\aq) \,\frac{d\phi_j}{dx} \,dx = 0 \quad for\, all\, j\leq N\ee
\begin{itemize}
\item From convention for dynamic systems the left integral in the weak form is called the \textit{mass matrix}
\item We now have explicit formula for $\aq$ and $\phi$, substituting
\be \int_k\! \frac{\partial \aq}{\partial t} \,\phi_n \,dx = \sum_{i=0}^M \frac{d a_i(t)}{dt} \int_k \psi_i(x) \phi_j(x) \ee
\end{itemize}
}
\frame{\frametitle{\secname}
\bea \int_k\! \frac{\partial \aq}{\partial t} \,\phi_n \,dx = \sum_{i=0}^M \frac{d a_i(t)}{dt} \int_k \psi_i(x) \phi_j(x)\,dx \eea
\begin{itemize}
\item Let's substitute in our expressions for approximation and the first weighting basis and analytically integrate
\be \int_k \psi_i(x) \phi_0(x)\,dx=\qquad\qquad\qquad\qquad \Big|_0^1\ee
\item Do this for each weighting basis to get 2x2 matrix: $\mathbf{M}_{j,i}$
\item If the time derivative of each of the approximation coefficients is a vector $\mathbf{a'}$ then
\be \sum_{i=0}^M \frac{d a_i(t)}{dt} \int_k \psi_i(x) \phi_j(x) \,dx = \mathbf{M}\,\mathbf{a'}  \ee
\end{itemize}
}

%NEW SECTION
\section{Stiffness Matrix}
\frame{\frametitle{\textbf{\secname}}
\bea \int_k\! \frac{\partial q}{\partial t} \,\phi \,dx +
	[f(q)\phi] \Big\rvert_{x_L}^{x_{R}} -
	\int_k\! f(q) \,\frac{d\phi}{dx} \,dx = 0 \eea
\begin{itemize}
\item Similar to mass matrix, substitute our approximation and bases into the third term.
\be \int_k\! f(q) \,\frac{d\phi}{dx} \,dx = \sum_{i=0}^M c a_i(t) \int_k \psi_i(x) \phi'_j(x) \,dx \ee
\item The integral for all weighting bases combine to form the stiffness matrix $\mathbf{K}$, we can analytically integrate for this simple case
\end{itemize}
}

\frame{\frametitle{\secname}
\begin{itemize}
\item Similar to the mass matrix, we have a vector of our element approximation coefficients $\mathbf{a}$, such that
\be \sum_{i=0}^M c a_i(t) \int_k \psi_i(x) \phi'_j(x) \,dx =  \mathbf{K}\,\mathbf{a}\ee
\item Let's substitute in our expressions for approximation and the first weighting basis and analytically integrate
\be \int_k \psi_i(x) \phi'_0(x)\,dx=\qquad\qquad\qquad\qquad \Big|_0^1\ee
\end{itemize}
}

%NEW SECTION
\section{Putting it all Together}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item Just need to calculate the numerical flux vector now.
\be [\hat{f}(q)\phi] \Big\rvert_{x_L}^{x_{R}} = cq_k(x_R)\phi_j(x_R)- cq_{k-1}(x_R)\phi_j(x_L)\ee
\item $\phi_j$ will be zero sometimes, depending on $j$
\item So what do we get?
\item $\mathbf{\hat{f}}=$
\end{itemize}
}

%NEW SECTION
\section{Semi-discrete system}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item We can now express the semi-discrete system compactly in matrix/vector form
\be \mathbf{M}\,\mathbf{a}'  + \mathbf{\hat{f}} - c\mathbf{K}\,\mathbf{a} =0 \ee
\item For each time step we have $\mathbf{a}$, starting with $\mathbf{a}_0$ from ICs
\item We'd like to calculate the time rate of change of our coefficients to use in our time discretization, so solve for $\mathbf{a}'$
\be \,\mathbf{a}'  = \left[c\mathbf{K}\,\mathbf{a} -\mathbf{\hat{f}}\right]\mathbf{M}^{-1} \ee
\end{itemize}
}

%NEW SECTION
\section{Time Discretization: Forward Euler}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item Now we need to complete the method by discretizing in time the semi-discrete system
\item Let's use a Forward Euler approach; very simple explicit ODE solver
\be \mathbf{a}_{t+1} = \mathbf{a}_t+ \mathbf{a}'_t \Delta t\ee
\item We can overwrite our previous timestep's coefficients with the new ones to save memory
\item We may want to periodically save $\mathbf{a}$ for plotting
\item We must choose $\Delta t$ carefully, can you predict why?
\end{itemize}
}

%NEW SECTION
\section{Investigation- h-Convergence}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item What do we expect for h-convergence?
\item How coarse can our elements be and still get a "good-enough" answer?
\item What effect does the shape/smoothness of the solution have on convergence?
\end{itemize}
}

%NEW SECTION
\section{Investigation- t-Convergence}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item How big can we make $\Delta t$?
\item Do we expect periodic solutions to stay steady over time?
\item How do h and t convergence relate?
\end{itemize}
}

%NEW SECTION
\section{Investigation- Stability (CFL)}
\frame{\frametitle{\textbf{\secname}}
\begin{itemize}
\item As was seen, too large of a timestep lead to issues, why?
\item What happens if a parcel of quantity moves fast enough to skip elements between timesteps?
\item We need to ensure
\be CFL = \frac{c \Delta t}{\Delta x} \leq C_{max}=1 \ee
for explicit methods. Implicit methods relax this restriction to permit higher values.
\end{itemize}
}

\end{document}


\subsection{NonUniformHexahedronFEMForceFieldAndMass}
\graphicspath{{../modules/}}  % to include images

\subsubsection{Concepts}

This force field implement the article :

\begin{verbatim}
      @InProceedings{NPF06,
      author       = "Nesme, Matthieu and Payan, Yohan and Faure, Fran\c{c}ois",
      title        = "Animating Shapes at Arbitrary Resolution with Non-Uniform Stiffness",
      booktitle    = "Eurographics Workshop in Virtual Reality Interaction and Physical Simulation (VRIPHYS)",
      month        = "nov",
      year         = "2006",
      organization = "Eurographics",
      address      = "Madrid",
      url          = "http://www-evasion.imag.fr/Publications/2006/NPF06"}
 \end{verbatim}

The basic idea, illustrated in figure \ref{fig:condensation}, is :

\begin{figure}
\begin{center}
	\includegraphics[width=\linewidth]{forcefield/nonUniformHexahedron/condensation.pdf}
\end{center}
	\caption{The condensation principle.}
	\label{fig:condensation}
\end{figure}

\begin{itemize}
\item the use of finer virtual levels of SparseGrid
\item the computation of classical mechanical matrices (mass and stiffness) at the finest resolution and the condensation of theses matrices to the current coarse mechanical resolution
\end{itemize}

\textbf{Warning} : actually the NonUniformHexahedronFEMForceFieldAndMass is only working with SparseGridTopology, and need enough finer virtual levels to compute the condensation.

\subsubsection{Data Fields}

\begin{itemize}
\item From HexahedronFEMForceFieldAndMass
	\begin{itemize}
	\item method (char) : large/polar, the corotationnal method (default = large)
	\item poissonRatio, youngModulus, density (float) : mechanical properties (density = volumetric mass in english $kg.m^{-3}$)
	\item assembling (bool) : assembling the global system matrix ? (default = false)
	\end{itemize}
\item Specific to NonUniformHexahedronFEMForceFieldAndMass
	\begin{itemize}
	\item nbVirtualFinerLevels (int) : how many finer virtual levels are employed in the condensation stage ? (default = 0)
	\end{itemize}
\item A hack on masses (for debugging)
		\begin{itemize}
	\item useMass (bool) : are the condensated mass matrices are used ? (if not, scalar masses concentrated on particles are used) (default = 0)
	\item totalMass (float) : if useMass=false, the scalar mass of the object
	\end{itemize}
\end{itemize}

\subsubsection{Example}

\begin{verbatim}

<Node name="non uniform">
   <Object type="SparseGrid"
                   n="4 4 4"
                   filename="mesh/mymesh.obj"
                   nbVirtualFinerLevels="2"  />
   <Object type="MechanicalObject"/>
   <Object type="NonUniformHexahedronFEMForceFieldAndMass"
                   nbVirtualFinerLevels="2"
                   youngModulus="20000"
                   poissonRatio="0.3"
                   density="10" />
</Node>

\end{verbatim}

\textbf{Important} : note that the SparseGrid has nbVirtualFinerLevels=2 in order to built enough finer virtual levels. This SparseGrid$->$nbVirtualFinerLevels has to be greater or equal to the NonUniformHexahedronFEMForceFieldAndMass$->$nbVirtualFinerLevels.
\\

A more complex example can be found in : examples/Components/forcefield/NonUniformHexahedronFEMForceFieldAndMass.scn where a comparison with a classical HexahedronFEMForceFieldAndMassForceField is done.


\chapter{Programmierungen}\label{chap:programming}
\section{{\TeX} if/ else}\label{sec:tex}
Grundlegender Aufbau:\\
\begin{lstlisting}[language=TeX]
\if <token-1><token-2> <tex-code-1> [\else <tex-code-2>] \fi
\end{lstlisting}
Bsp1:\\
\begin{lstlisting}[language=TeX]
\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi
\end{lstlisting}

\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi
\\Bsp2:\\
\begin{lstlisting}[language=TeX]
\def\mycmd{1}

\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi

\end{lstlisting}

\def\mycmd{1}

\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi
\\Bsp3:\\
\begin{lstlisting}[language=TeX]
\def\mycmd{0}

\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi
\end{lstlisting}

\def\mycmd{0}

\ifx\mycmd\undefined
	undefed
\else
	\if\mycmd1
		defed, 1
	\else
		defed
	\fi
\fi

\section{forPGF}\label{sec:pgf}

\section{Lua}\label{sec:lua}
\begin{lstlisting}[language={[5.0]Lua}]
\count75=1564 % Data existing in the "TeX World"
\directlua{
local x=\number\count75 \space % Transfer TeX data to the "Lua World"
tex.print("x= "..x)
local y = (2*x-65)/5
tex.print(" and y = "..y)
}
\end{lstlisting}

%\count75=1564 % Data existing in the "TeX World"
%\directlua{
%local x=\number\count75 \space % Transfer TeX data to the "Lua World"
%tex.print("x= "..x)
%local y = (2*x-65)/5
%tex.print(" and y = "..y)
%}

Die Kreiszahl $\pi$ hat den Wert \directlua{tex.print(math.pi)}.

\def\aa{tex}
\def\bb{.}
\def\cc{print}
\def\dd{("Hello wie gehts")}
\directlua{\aa\bb\cc\dd}

\directlua{
	tex.print(math.random())
	tex.print(math.random())
}


\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[osf]{libertine}
\usepackage[scaled=0.8]{beramono}
\usepackage[margin=1.5in]{geometry}
\usepackage{url}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{nicefrac}
\usepackage{microtype}
\usepackage{subcaption}
\usepackage{bm}

\usepackage{amsthm}
\newtheorem{defn}{Definition}

\usepackage{sectsty}
\sectionfont{\large}
\subsectionfont{\normalsize}

\usepackage{titlesec}
\titlespacing{\section}{0pt}{10pt plus 2pt minus 2pt}{0pt plus 2pt minus 0pt}
\titlespacing{\subsection}{0pt}{5pt plus 2pt minus 2pt}{0pt plus 2pt minus 0pt}

\usepackage{pgfplots}
\pgfplotsset{
  compat=newest,
  plot coordinates/math parser=false,
  tick label style={font=\footnotesize, /pgf/number format/fixed},
  label style={font=\small},
  legend style={font=\small},
  every axis/.append style={
    tick align=outside,
    clip mode=individual,
    scaled ticks=false,
    thick,
    tick style={semithick, black}
  }
}

\pgfkeys{/pgf/number format/.cd, set thousands separator={\,}}

\usepgfplotslibrary{external}
\tikzexternalize[prefix=tikz/]

\newlength\figurewidth
\newlength\figureheight

\setlength{\figurewidth}{12cm}
\setlength{\figureheight}{6cm}

\newlength\squarefigurewidth
\newlength\squarefigureheight

\setlength{\squarefigurewidth}{4cm}
\setlength{\squarefigureheight}{4cm}

\newlength\smallsquarefigurewidth
\newlength\smallsquarefigureheight

\setlength{\smallsquarefigurewidth}{3.25cm}
\setlength{\smallsquarefigureheight}{3.25cm}

\newlength\smallfigurewidth
\newlength\smallfigureheight

\setlength{\smallfigurewidth}{6.25cm}
\setlength{\smallfigureheight}{4cm}

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

\newcommand{\acro}[1]{\textsc{\MakeLowercase{#1}}}
\newcommand{\given}{\mid}
\newcommand{\mc}[1]{\mathcal{#1}}
\newcommand{\data}{\mc{D}}
\newcommand{\intd}[1]{\,\mathrm{d}{#1}}
\newcommand{\inv}{^{-1}}
\newcommand{\trans}{^\top}
\newcommand{\mat}[1]{\bm{\mathrm{#1}}}
\renewcommand{\vec}[1]{\bm{\mathrm{#1}}}
\newcommand{\R}{\mathbb{R}}
\renewcommand{\epsilon}{\varepsilon}
\newcommand{\Exp}{\mathbb{E}}

\DeclareMathOperator{\var}{var}
\DeclareMathOperator{\cov}{cov}
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}

\begin{document}

\section*{Gaussian Processes}

We previously considered the \emph{kernel trick,} which allowed
us to define the \emph{kernel function}
\begin{equation*}
  K(\vec{x}, \vec{x}')
  =
  \phi(\vec{x})\trans\mat{\Sigma}\phi(\vec{x}').
\end{equation*}
Then, when doing Bayesian linear regression with prior
\begin{equation*}
  p(\vec{w}) = \mc{N}(\vec{w}; \vec{0}, \mat{\Sigma}),
\end{equation*}
and data $\data$, we derived the predictive distribution
\begin{equation*}
  p(\vec{y}_\ast \given \mat{X}_\ast, \data, \sigma^2)
  =
  \mc{N}(
  \vec{y}_\ast;
  \vec{\mu}_{\vec{y}_\ast \given \data},
  \mat{K}_{\vec{y}_\ast \given \data}),
\end{equation*}
where
\begin{align*}
  \vec{\mu}_{\vec{y}_\ast \given \data}
  &=
  \mat{K}_\ast\trans
  (\mat{K} + \sigma^2\mat{I})\inv
  \vec{y}
  \\
  \mat{K}_{\vec{y}_\ast \given \data}
  &=
  \mat{K}_{\ast\ast}
  -
  \mat{K}_\ast\trans
  (\mat{K} + \sigma^2\mat{I})\inv
  \mat{K}_\ast,
\end{align*}
and we have defined
\begin{equation*}
  \mat{K} = K(\mat{X}, \mat{X})
  \qquad
  \mat{K}_\ast = K(\mat{X}, \mat{X}_\ast)
  \qquad
  \mat{K}_{\ast\ast} = K(\mat{X}_\ast, \mat{X}_\ast).
\end{equation*}

Examining the form of this predictive distribution, we barely need to
perform inference over the weight vector $\vec{w}$ at all; its
covariance simply appears in the definition of $K$.  If we treat $K$
as a black-box, maybe we can skip the inference over $\vec{w}$
entirely and simply work with $K$ directly.  This line of thinking
motivates \emph{Gaussian processes.}

Let us again write down the abstract problem of regression.  We have
an unknown function $f\colon \mc{X} \to \R$, where $\mc{X}$ is an
arbitrary input space (for example $\mc{X} = \R^d$).  Given noisy
training observations $\data = (\mat{X}, \vec{y})$, we wish to infer
the value of $f$ at a set of test points $\mat{X}_\ast$.

Here we will continue to assume that the values $\vec{y}$ are
generated by adding zero-mean, independent Gaussian noise with
variance $\sigma^2$ to the true function values $\vec{f} =
f(\mat{X})$.  Probabilistically, we write
\begin{equation*}
  p(\vec{y} \given \vec{f}, \sigma^2)
  =
  \mc{N}(\vec{y}; \vec{f}, \sigma^2 \mat{I}).
\end{equation*}
In linear regression, we made the assumption that $f$ was linear:
$f(\vec{x}) = \phi(\vec{x})\trans\vec{w}$.  In this case we would have
$\vec{f} = \mat{\Phi}\vec{w}$, and a trivial rewriting of this
likelihood would reveal it to be the same as we have been using all
along, except that we are removing any explicit assumptions about the
nature of the function $f$.

When discussing Bayesian linear regression, we also derived the prior
distribution for the vector of latent values $\vec{f} =
\mat{\Phi}\vec{w}$ given the input locations $\mat{X}$.  In
particular, given the prior $p(\vec{w}) = \mc{N}(\vec{w}; \vec{0},
\mat{\Sigma})$, we have
\begin{equation*}
  p(\vec{f} \given \mat{X})
  =
  \mc{N}(\vec{f}; \vec{0}, \mat{\Phi}\mat{\Sigma}\mat{\Phi}\trans)
  =
  \mc{N}(\vec{f}; \vec{0}, \mat{K}),
\end{equation*}
where we have used the definition of the kernel function above.
Again, if we treat $K$ as a black-box, we have avoided explicit
reasoning about $\vec{w}$ or $\phi$.

\emph{Gaussian processes} approach the problem of regression using
these ideas in a somewhat more abstract approach.  We construct an
explicit prior distribution for the \emph{function} $f$, that we
reason about via the Bayesian method.  This seems like it might be
difficult, because $f$ might in general be infinite-dimensional.  A
Gaussian process provides a natural extension of the familiar
multivariate Gaussian distribution to potentially infinite-dimensional
function spaces.  The key definition is the following.
\begin{defn}
  A \emph{Gaussian process} is a (potentially infinite) collection of
  random variables such that the joint distribution of any finite
  number of them is multivariate Gaussian.
\end{defn}
There are infinitely many random variables are the values of the
function $f$ at every location $x \in \mc{X}$.  The way we extend the
finite-dimensional Gaussian distribution to such a collection of
variables is by allowing arbitrary inputs but only ever reasoning
about finitely many of them at a time.

A Gaussian process prior distribution on $f$ is written
\begin{equation*}
  p(f) = \mc{GP}(f; \mu, K),
\end{equation*}
and just like the multivariate Gaussian distribution, is
parameterized by its first two moments (now functions):
\begin{itemize}
\item $\Exp[f] = \mu\colon \mc{X} \to \R$, the \emph{mean function,} and
\item $\Exp\bigl[\bigl(f(x) - \mu(x)\bigr)\bigl(f(x') -
  \mu(x')\bigr)\bigr] = K\colon \mc{X} \times \mc{X} \to \R$, a
  positive semidefinite \emph{covariance function} or
  \emph{kernel.}\footnote{A function is \emph{positive semidefinite}
    if, for every finite set of points $\mat{X}$, the \emph{Gram
      matrix} $\mat{K} = K(\mat{X}, \mat{X})$ is positive
    semidefinite.  Note that a matrix is a valid covariance matrix if
    and only if it is positive semidefinite.}
\end{itemize}
The function $K$ should look familiar!  Rather than explicitly
construct $K$, we simply select any positive semidefinite function,
any one of which will define a Gaussian process.

The mean function encodes the central tendency of the function, and is
often assumed to be a constant (usually zero).  The covariance
function encodes information about the shape and structure we expect
the function to have.  A simple and very common example is the
\emph{squared exponential} covariance:
\begin{equation*}
  K(\vec{x}, \vec{x}'; \lambda, \ell)
  =
  \lambda^2
  \exp\biggl(-\frac{\lVert \vec{x} - \vec{x}' \rVert^2}{2\ell^2}\biggr),
\end{equation*}
which encodes the notation that ``nearby points should have similar
function values.''

Suppose we have selected a GP prior $\mc{GP}(f; \mu, K)$ for the
function $f$.  Consider a finite set of points $\mat{X} \subseteq
\mc{X}$.  The GP prior on $f$, by definition, implies the following
joint distribution on the associated function values $\vec{f} =
f(\mat{X})$:
\begin{equation*}
  p(\vec{f} \given \mat{X})
  =
  \mc{N}(\vec{f}; \mu(\mat{X}), K(\mat{X}, \mat{X})\bigr).
\end{equation*}
That is, we simply evaluate the mean and covariance functions at
$\mat{X}$ and take the associated multivariate Gaussian distribution.
Very simple!  Notice the similarity to the expression above.

Let us return to the Bayesian linear regression setting.  We will see
that we have been specifying a Gaussian process on a latent function
$f(\vec{x}) = \phi(\vec{x})\trans \vec{w}$ all along.  Let the feature
expansion function $\phi$ be given, as well as the Gaussian prior on
$\vec{w}$: $p(\vec{w}) = \mc{N}(\vec{w}; \vec{\mu}, \mat{K})$.
Consider a finite number of input locations $\mat{X}$.  We may derive
the prior distribution for the associated latent function values
$\vec{f} = f(\mat{X}) = \mat{\Phi}\vec{w}$ by recognizing it as a
linear transformation of the Gaussian-distributed vector $\vec{w}$:
\begin{equation*}
  p(\vec{f} \given \mat{X})
  =
  \mc{N}(\vec{f}; \mat{\Phi}\vec{\mu}, \mat{\Phi}\mat{\Sigma}\mat{\Phi}\trans).
\end{equation*}
Therefore we have shown that the joint distribution of any finite
combination of our random variables $\{f(\vec{x})\}_{\vec{x} \in
  \mc{X}}$ is multivariate Gaussian, and therefore according to the
above definition we have a Gaussian process!  We may identify the mean
and covariance functions of the Bayesian linear regression Gaussian
process:
\begin{align*}
  \mu(\vec{x})
  &=
  \phi(\vec{x})\trans
  \vec{\mu}
  \\
  K(\vec{x}, \vec{x}')
  &=
  \phi(\vec{x})\trans
  \mat{\Sigma}
  \phi(\vec{x}).
\end{align*}
By starting over ``from the beginning,'' we may free ourselves from
the constraint of needing to explicitly specify the feature expansion
function $\phi$.  Instead, we may choose arbitrary mean and covariance
functions $\mu$ and $K$ and reason about $f$ in an essentially
identical manner.

\end{document}


% $Header$
% $Author: fager $
% $Date: 2009-04-28 20:51:04 +0200 (ti, 28 apr 2009) $
% $Revision: 99 $
% $Log$
% Revision 1.4  2005/02/27 18:32:56  fager
% Correction from BH implemented.
%
% Revision 1.3  2005/02/22 07:31:00  fager
% Corrections from M.Kelly implemented
%
% Revision 1.2  2004/10/21 18:59:06  fager
% Version logging added. Comments from KA implemented.
%
\section{X-parameter data access and manipulation}\label{sec:SPMod}
The most valuable feature of MuWave is for most users the
possibility to handle and manipulate S/Y/Z/T\footnote{Hereafter
``X-parameter'' is used to refer either of the
S/Y/Z/T-parameters.} in a convenient way. This section gives an
overview of the variety of possibilities offered.

\subsection{Direct parameter access}
Any of the X-parameters can be directly accessed from the
\verb"meassp" object by using the ``dot-operator'' notation or a
get command:

\begin{small}
\begin{verbatim}
    >> msp.Z11
    ans =
    1.0e+002 *
    1.0467 - 5.6792i
    0.8499 - 3.9010i
    ...

    >> 20*log10(abs(get(msp,'S21')))
    ans =
    11.9806
    12.0251
    11.9318
    ...
\end{verbatim}
\end{small}

By this it was also illustrated how MuWave automatically converts
from the S-para\-meters in \verb"msp" into the desired type, as in
\verb"msp.Z11". Note that these functions all return a row vector
that can be involved in any complex mathematical vector
manipulation that \matlab supports.

\subsection{Parameter assignment}
It is also possible to assign new values for a particular
X-parameter data using a simple assignment:
\begin{small}
\begin{verbatim}
    >> msp.S11 = 0.1*msp.S11;
\end{verbatim}
\end{small}
If the parameter type being assigned differs from the type of the
\verb"meassp" object it is operating on, a conversion takes place
to match the type of parameter that is being assigned:
\begin{small}
\begin{verbatim}
    >> msp
    ...
    xparam-object
    	 type:	S
    	 reference:	50
    	 ports:	2
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
    ...
    >> msp.Z11 = zeros(length(msp.freq),1);
    >> msp
    ...
    xparam-object
    	 type:	Z
    	 reference:	50
    	 ports:	2
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
\end{verbatim}
\end{small}

\subsection{Matrix operations}
There are three levels one can choose from when doing matrix
operations on one or several X-parameter objects. Which to choose
depends on the complexity of the operations involved.

\subsubsection{xparam}
On the highest level it is possible to operate directly with the
\verb"xparam"-objects. The \verb"xparam"-object is extracted from
the \verb"meassp" object using:
\begin{small}
\begin{verbatim}
    xparam-object
    	 type:	S
    	 reference:	50
    	 ports:	2
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
\end{verbatim}
\end{small}
where, again, the dot-operator has been applied to the
\verb"meassp" object.

The \verb"xparam" objects contain information about i.e. the type
of data (S/Z/Y/T). MuWave therefore automatically converts between
these types so that they match i.e. when adding two objects.
\begin{small}
\begin{verbatim}
    >> Zp = msp.Z;
    >> Yp = msp.Y;
    >> Zp_sum = Zp + Yp
    Warning: XPARAM.PLUS: Arguments not of same type. Conversion performed
    > In xparam.plus at 25
    xparam-object
    	 type:	Z
    	 reference:	50
    	 ports:	2
    	 elements:	101
    	 freq:	1E+009 -> 5E+010
\end{verbatim}
\end{small}
Although only shown for addition here, a lot of other algebraic
manipulations have been implemented for the \verb"xparam" class.
However, be careful that the conversions sometimes taking place
when operating on \verb"xparam" objects of different type may give
undesired results. Please check out the online help for details on
how each operation works.

It often turns out to be more reliable to operate on the data
directly by accessing the underlying \verb"arraymatrix" object.

\subsubsection{arraymatrix}
The \verb"arraymatrix" is accessed from the \verb"xparam" object:
\begin{small}
\begin{verbatim}
    >> Sp = msp.S;
    >> Sp_array = get(Sp,'arraymatrix')
    arraymatrix-object
        dimension:  2 x 2
        elements:   101
    First matrix element:
    0.9921 - 0.0697i   0.0009 + 0.0037i
    -3.9645 + 0.2474i   0.8495 - 0.0128i
\end{verbatim}
\end{small}
The \verb"arraymatrix" can also be directly accessed from the
\verb"meassp" object by
\begin{small}
\begin{verbatim}
    >> Sp_array = get(msp,'arraymatrix');
\end{verbatim}
\end{small}

Individual parameters of the \verb"arraymatrix" object are
accessed and assigned using parentheses \verb"()",
\begin{small}
\begin{verbatim}
    >> Sp_array2 = Sp_array;
    >> Sp_array2(1,1) = Sp_array2(2,2)
    arraymatrix-object
        dimension:  2 x 2
        elements:   101
    First matrix element:
    0.8495 - 0.0128i   0.0009 + 0.0037i
    -3.9645 + 0.2474i   0.8495 - 0.0128i
\end{verbatim}
\end{small}

Matrix operations are carried out in a similar straightforward fashion,
\begin{small}
\begin{verbatim}
    >> Sp_array3 = Sp_array2*Sp_array
    arraymatrix-object
        dimension:  2 x 2
        elements:   101
    First matrix element:
    0.8376 - 0.0866i   0.0016 + 0.0063i
    -7.2809 + 0.7830i   0.7172 - 0.0365i
\end{verbatim}
\end{small}
where the matrix multiplication of \verb"Sp_array2" and
\verb"Sp_array" is carried out independently for each frequency
point. A variety of other algebraic operations are implemented for
the \verb"arraymatrix" class.

Finally, it is usually to put the manipulated \verb"arraymatrix"
object back into the \verb"meassp" context:
\begin{small}
\begin{verbatim}
    >> Sp = xparam(Sp_array3,'S',50);
    >> msp_new = msp;
    >> msp_new.data = Sp;
\end{verbatim}
\end{small}

\subsubsection{3d-matrix}
In rare cases, the functions implemented for the
\verb"arraymatrix" class are not sufficient to solve a particular
problem. The way out is then to access the X-parameter data in its
raw form, i.e. in the way it is \emph{actually} stored in \matlab.

The data is stored as a 3-dimensional matrix, where the first two
dimensions correspond to the row and columns of the X-parameter
matrix at each frequency. The third dimension is the frequency.
This means that, e.g. a two-port S-parameter measurement with 101
frequency points will be stored as a 2x2x101-sized complex vector.

This raw data matrix can be accessed directly from the
\verb"xparam", \verb"arraymatrix", or the \verb"meassp" objects by
getting the \verb"mtrx" property:
\begin{small}
\begin{verbatim}
    >> Sp = msp.S;
    >> raw_data = get(Sp,'mtrx')
    raw_data(:,:,1) =
    0.9921 - 0.0697i   0.0009 + 0.0037i
    -3.9645 + 0.2474i   0.8495 - 0.0128i

    raw_data(:,:,2) =
    0.9887 - 0.1027i   0.0011 + 0.0053i
    -3.9762 + 0.3613i   0.8527 - 0.0303i
    ...
    ...
    raw_data(:,:,101) =
    -0.5639 - 0.5608i   0.0775 + 0.0227i
    1.1758 + 1.5074i   0.2195 - 0.7032i
\end{verbatim}
\end{small}
After manipulation, it can be used to create an \verb"arraymatrix"
object,
\begin{small}
\begin{verbatim}
    >> new_array = arraymatrix(raw_data);
\end{verbatim}
\end{small}
which, in turn can be converted back into \verb"xparam" and
\verb"meassp" objects as was described above.