\hypertarget{lab-02-variables-arrays-and-scripts}{%
\section{Lab 02: Variables, Arrays, and
Scripts}\label{lab-02-variables-arrays-and-scripts}}

\hypertarget{variables}{%
\subsection{Variables}\label{variables}}

\begin{frame}{}
\protect\hypertarget{section}{}
Variables help us represent quantities or expressions in order to make
their use and re-use more convenient.
\end{frame}

\begin{frame}[fragile]{Naming Variables}
\protect\hypertarget{naming-variables}{}
\begin{itemize}[<+->]
\tightlist
\item
  Must start with a letter.
\item
  Followed by letters (a-z, A-Z) or numbers (0-9) or underscores (\_).
\item
  Maximum 65 characters (excluding the .m extension).
\item
  Must not be the same as any MATLAB reserved word.
\item
  Space is not permitted.
\item
  Case sensitive, i.e., \texttt{a\ \textasciitilde{}=\ A}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Naming Variables}
\protect\hypertarget{naming-variables-1}{}
\begin{itemize}[<+->]
\tightlist
\item
  Be as descriptive as possible with your variable names.
\item
  Avoid built-in function/variable names (reserved keywords) such as
  \texttt{pi}, \texttt{sin}, \texttt{exp}, etc.
\item
  Check if a name is already in use: \texttt{which\ variableName} or
  \texttt{exist\ variableName}.
\end{itemize}
\end{frame}

\begin{frame}{Naming Conventions}
\protect\hypertarget{naming-conventions}{}
\begin{itemize}[<+->]
\tightlist
\item
  snake\_case: writing compound words or phrases in which the elements
  are separated with one underscore character (\_) and no spaces,
  e.g.~``foo\_bar''.
\item
  camelCase: writing compound words or phrases such that each word or
  abbreviation in the middle of the phrase begins with a capital letter,
  with no intervening spaces or punctuation, e.g.~``fooBar''
\item
  Other conventions: Hungarian notation, positional notation, etc.
\item
  Reference:
  \url{https://en.wikipedia.org/wiki/Naming_convention_(programming)}
\end{itemize}
\end{frame}

\begin{frame}{Default Variable Definitions}
\protect\hypertarget{default-variable-definitions}{}
\begin{table}[!hbtp]
    \begin{tabular}{rl}
        Command & Description \\
        \hline
        \texttt{pi} & variable defining $\pi$ \\
        \texttt{i} or \texttt{1i} & imaginary number $i = \sqrt{-1}$ \\
        \texttt{j} or \texttt{1j} & imaginary number $j = \sqrt{-1}$
    \end{tabular}
\end{table}
\end{frame}

\hypertarget{arrays}{%
\subsection{Arrays}\label{arrays}}

\begin{frame}[fragile]{Array, Vector, and Matrix}
\protect\hypertarget{array-vector-and-matrix}{}
\begin{itemize}[<+->]
\tightlist
\item
  An array is a data form that can hold several values, all of one type.
\item
  A vector is a \(1\)-D array: we can define row vectors, column
  vectors.
\item
  A matrix is a \(2\)-D array.
\item
  Also, we can define \(N\)-D array.
\item
  The general notation for a vector or matrix is a list of values
  enclosed in square brackets \texttt{{[}{]}} separated by commas
  (space) or semi-colons (or the combination).
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Vector: \texttt{{[}{]}}}
\protect\hypertarget{vector}{}
\begin{itemize}[<+->]
\item
  Row vector: \(x = \begin{bmatrix} 1 & 2 & 3 & 4 \end{bmatrix}\)

\begin{verbatim}
x = [1,2,3,4]
x = [1 2 3 4]
\end{verbatim}
\item
  Column vector: \(y = \begin{bmatrix} 1 \\ 2 \\ 3 \\ 4 \end{bmatrix}\)
  or \(y = \begin{bmatrix} 1 & 2 & 3 & 4\end{bmatrix}^{\top}\) or
  \(y = x^{\top}\).

\begin{verbatim}
y = [1;2;3;4]
y = transpose([1 2 3 4])
y = [1 2 3 4]'
y = x'
y = x(:)
\end{verbatim}

  Note: \texttt{\textquotesingle{}} and \texttt{.\textquotesingle{}} are
  the infix notation for \texttt{ctrasnpose}, \texttt{transpose}
  operation.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Vector: \texttt{linspace} vs.~\texttt{colon}}
\protect\hypertarget{vector-linspace-vs.-colon}{}
\begin{itemize}[<+->]
\item
  \texttt{linspace(from,\ to,\ n)} generates \texttt{n} points between
  \texttt{from} (inclusive) and \texttt{to} (inclusive). For example,

  \texttt{a\ =\ linspace(2,\ 6,\ 5)\ \ \%\ same\ as\ a\ =\ {[}2\ 3\ 4\ 5\ 6{]}}
\item
  \texttt{colon(from,\ step,\ upper\_bound)} generates points between
  \texttt{from} (inclusive) and \texttt{upper\_bound} (may not be
  inclusive) with spacing \texttt{step}. For example,

\begin{verbatim}
a = colon(2, 1, 6)  % same as a = [2 3 4 5 6]
a = colon(2, 2, 6)  % same as a = [2 4 6]
a = colon(2, 1, 7)  % same as a = [2 3 4 5 6 7]
a = colon(2, 2, 7)  % same as a = [2 4 6]
\end{verbatim}
\item
  \texttt{from:step:upper\_bound} is same as
  \texttt{colon(from,\ step,\ upper\_bound)}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Vector: \texttt{linspace} vs.~\texttt{colon}}
\protect\hypertarget{vector-linspace-vs.-colon-1}{}
\begin{itemize}[<+->]
\tightlist
\item
  \texttt{linspace(from,\ to,\ n)} is equivalent to
  \texttt{colon(from,\ (to\ -\ from)\ /\ (n\ -\ 1),\ to)}
\item
  \texttt{colon(from,\ step,\ upper\_bound)} is equivalent to
  \texttt{linspace(from,\ floor((upper\_bound\ -\ from)\ /\ step)\ *\ step\ +\ from,\ floor((upper\_bound\ -\ from)\ /\ step))}
\item
  Use \texttt{linspace} when the number of points is given.
\item
  Use \texttt{colon} when the spacing/step size is given.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Matrix: \texttt{{[}{]}}}
\protect\hypertarget{matrix}{}
Define a \(2 \times 3\) matrix
\(A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}\)

\begin{verbatim}
A = [1,2,3;4,5,6]
\end{verbatim}

or

\begin{verbatim}
row1 = [1,2,3]
row2 = [4,5,6]
A = [row1;row2]
\end{verbatim}

or

\begin{verbatim}
col1 = [1;4]
col2 = [2;5]
col3 = [3;6]
A = [col1,col2,col3]
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Matrix: \texttt{zeros}, \texttt{ones},
\texttt{eye}, \texttt{rand}, \texttt{randn}, \texttt{magic}}
\protect\hypertarget{matrix-zeros-ones-eye-rand-randn-magic}{}
\begin{itemize}[<+->]
\item
  \texttt{zeros(m,\ n)}: define a \texttt{m}-by-\texttt{n} matrix with
  zeros.

\begin{verbatim}
zeroRowVec = zeros(5, 1)
zeroColVec = zeros(1, 5)
zeroMatrix = zeros(5, 5)
zeroMatrix = zeros(5)
\end{verbatim}
\item
  \texttt{ones(m,\ n)}: define a \texttt{m}-by-\texttt{n} matrix with
  ones.
\item
  \texttt{eye(m,\ n)}: define a \texttt{m}-by-\texttt{n} matrix with
  diagonals being ones.
\item
  \texttt{rand(m,\ n)}: define a \texttt{m}-by-\texttt{n} matrix with
  uniformly distributed numbers.
\item
  \texttt{randn(m,\ n)}: define a \texttt{m}-by-\texttt{n} matrix with
  normally distributed numbers.
\item
  \texttt{magic(n)}: define a \texttt{n}-by-\texttt{n} magic square with
  row sum, column sum and diagonal sum being equal.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Dimension: \texttt{size}, \texttt{length},
\texttt{reshape}}
\protect\hypertarget{dimension-size-length-reshape}{}
\begin{itemize}[<+->]
\item
  \texttt{size(array)}: size of \texttt{array}. If \texttt{array} is
  \texttt{n}-dimensional, \texttt{size} will return a vector of length
  \texttt{n}.
\item
  \texttt{size(array,\ 1)}: number of rows of \texttt{array}.
\item
  \texttt{size(array,\ 2)}: number of columns of \texttt{array}.
\item
  \texttt{length(vec)}: length of vector \texttt{vec}, equivalent to
  \texttt{max(size(vec))}.
\item
  \texttt{reshape(array,\ dim1,\ dim2,\ dim3,\ ...)}.

\begin{verbatim}
rowVec = 1:8
matrix = reshape(rowVec, 2, 4)
% same as matrix = [1,3,5,7;2,4,6,8]
\end{verbatim}
\item
  \texttt{reshape(array,\ prod(size(array)),\ 1)} is same as
  \texttt{array(:)}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{\(N\)-D array: \texttt{reshape}}
\protect\hypertarget{n-d-array-reshape}{}
Define 3-D array:

\begin{verbatim}
rowVec = 1:8
array = reshape(rowVec, 2, 2, 2);
length(size(array))  % check the dimension
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{1-D Array: Slicing}
\protect\hypertarget{d-array-slicing}{}
\begin{itemize}[<+->]
\item
  Define a row vector \texttt{rowVec}:

\begin{verbatim}
rowVec = [2,4,6,8,10]
rowVec = linspace(2,10,5)
rowVec = colon(2,2,10)    % or rowVec = 2:2:10
\end{verbatim}
\item
  \texttt{array(i)}: the \texttt{i}-th entry of \texttt{array}, where
  \texttt{i} is called the index:

  \begin{table}[!hbtp]
    \centering
    \begin{tabular}{cccccc}
      \toprule
      \verb|i|         & 1 & 2 & 3 & 4 & 5 \\
      \midrule
      \verb|rowVec(i)| & 2 & 4 & 6 & 8 & 10 \\
      \bottomrule
    \end{tabular}
  \end{table}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{1-D Array: Slicing}
\protect\hypertarget{d-array-slicing-1}{}
\begin{table}[!hbtp]
  \centering
  \begin{tabular}{cccccc}
    \toprule
    \verb|i|         & 1 & 2 & 3 & 4 & 5 \\
    \midrule
    \verb|rowVec(i)| & 2 & 4 & 6 & 8 & 10 \\
    \bottomrule
  \end{tabular}
\end{table}

\begin{itemize}[<+->]
\item
  Extract one entry from a vector: For example, to extract \texttt{6}
  from \texttt{rowVec} and assign it to \texttt{x}:

  \texttt{x\ =\ rowVec(3)}
\item
  Extract multiple entries from a vector: For example, to extract
  \texttt{2}, \texttt{6}, \texttt{8} from \texttt{rowVec} and assign it
  to \texttt{x}:

  \texttt{x\ =\ rowVec({[}1,3,4{]})}
\item
  Extract multiple continguous entries from a vector: For example, to
  extract \texttt{4}, \texttt{6}, \texttt{8} from \texttt{rowVec} and
  assign it to \texttt{x}:

\begin{verbatim}
x = rowVec([2,3,4])
x = rowVec(2:4)
\end{verbatim}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{2-D array: Slicing}
\protect\hypertarget{d-array-slicing-2}{}
\begin{itemize}[<+->]
\item
  Define a matrix \texttt{mat}

\begin{verbatim}
mat = reshape(1:8, 2, 4)
\end{verbatim}
\item
  \texttt{array(i,\ j)}: the entry of \texttt{array} at row \texttt{i}
  and column \texttt{j}, where \texttt{i} is colled row index,
  \texttt{j} is called column index:

  \begin{table}[!hbtp]
    \centering
    \begin{tabular}{c|cccc}
      \toprule
      \diagbox{\texttt{i}}{\texttt{mat(i, j)}}{\texttt{j}} & 1 & 2 & 3 & 4 \\
      \hline
      1 & 1 & 3 & 5 & 7 \\
      2 & 2 & 4 & 6 & 8 \\
      \bottomrule
    \end{tabular}
  \end{table}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{2-D array: Slicing}
\protect\hypertarget{d-array-slicing-3}{}
\begin{table}[!hbtp]
  \centering
  \begin{tabular}{c|cccc}
    \toprule
    \diagbox{\texttt{i}}{\texttt{mat(i, j)}}{\texttt{j}} & 1 & 2 & 3 & 4 \\
    \hline
    1 & 1 & 3 & 5 & 7 \\
    2 & 2 & 4 & 6 & 8 \\
    \bottomrule
  \end{tabular}
\end{table}

Extract multiple rows and multiple columns from \texttt{mat}: For
example, to extract entries at row \texttt{1}, row \texttt{2}, and
column \texttt{2}, column \texttt{4}:

\begin{verbatim}
A = mat([1,2], [2,4])
A = mat(1:2, [2,4])
A = mat(1:end, [2,4])
A = mat(:, [2,4])
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Generate a \(3\)-D Array using Slicing}
\protect\hypertarget{generate-a-3-d-array-using-slicing}{}
\begin{verbatim}
slice1 = [1,2;3,4]
slice2 = [5,6;7,8]
C(:,:,1) = slice1
C(:,:,2) = slice2
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Concatenate Arrays}
\protect\hypertarget{concatenate-arrays}{}
\begin{verbatim}
row1 = [1,2,3]
row2 = [4,5,6]
rowVec = [row1,row2]  % rowVec = [1,2,3,4,5,6]
matrix = [row1;row2]  % matrix = [1,2,3;4,5,6]
matrix1 = [matrix1;matrix2]
% same as matrix1 = [1,2,3;4,5,6;1,2,3;4,5,6]
matrix2 = [matrix1,matrix2]
% same as matrix2 = [1,2,3,1,2,3;4,5,6,4,5,6]
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{1-D Array: Append/Delete Element}
\protect\hypertarget{d-array-appenddelete-element}{}
\begin{verbatim}
% 1-D array
rowVec = 1:5
rowVec(end + 1) = 6  % append 6 to rowVec
rowVec = [rowVec,7]  % append 7 to rowVec
rowVec(5) = []       % delete 5 from rowVec
rowVec(2:4) = []     % delete 2, 3, 4 from rowVec
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{2-D Array: Append/Delete Element}
\protect\hypertarget{d-array-appenddelete-element-1}{}
\begin{verbatim}
% 2-D array
matrix = magic(5)
matrix(:, end + 1) = 1:5   % append a column vector
matrix = [matrix,[6:10]']  % append a column vector
matrix(end + 1, :) = 1:7   % append a row vector
matrix = [matrix;8:14]     % append a row vector
matrix(:,6) = []           % delete column 2
matrix(:,3:5) = []         % delete column 3, 4, 5
matrix(2:4,:) = []         % delete row 2, 3, 4
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Char Array vs.~String Array}
\protect\hypertarget{char-array-vs.-string-array}{}
\begin{verbatim}
str = "abc"
arrayOfChars1 = 'abc'
arrayOfChars2 = ['a','b','c']
arrayOfChars1 == arrayOfChars2 % return logical 1 (true)
arrayOfChars1 == str           % return logical 1 (true)
class(str)                     % string
class(arrayOfChars1)           % char
[arrayOfChars1,arrayOfChars2]  % return 'abcabc'
[arrayOfChars1;arrayOfChars2]  % return ['abc';'abc']
[str,str]                      % return ["abc","abc"]
[str;str]                      % return ["abc";"abc"]
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Cell Array: array of elements of different types}
\protect\hypertarget{cell-array-array-of-elements-of-different-types}{}
\begin{itemize}[<+->]
\item
  \texttt{cell(n)}: create 1-D cell array of length \texttt{n}
\item
  \texttt{cell(m,n)}: create 2-D cell array of size \texttt{m} by
  \texttt{n}
\item
  Create a cell array of types \texttt{char}, \texttt{string},
  \texttt{double}:

\begin{verbatim}
cellArray = {[1,2,3], "abc", 'def'}
cellArray{1}          % return [1,2,3]
cellArray{2}          % return "abc"
cellArray{3}          % return 'def'
cellArray{4} = 'ghi'
cellArray{4}          % return 'ghi'
\end{verbatim}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Array Operations: 1-D Array}
\protect\hypertarget{array-operations-1-d-array}{}
\begin{itemize}[<+->]
\tightlist
\item
  \texttt{sum(vec)}/\texttt{prod(vec)}: sum/product of all elements of
  \texttt{vec}.
\item
  \texttt{max(vec)}/\texttt{min(vec)}: maximum/minimum of \texttt{vec}.
\item
  \texttt{rowVec\ =\ rowVec1\ .*\ rowVec2}: elementwise multiplication,
  where \texttt{rowVec(i)\ =\ rowVec1(i)\ *\ rowVec2(i)}.
\item
  \texttt{rowVec\ .*\ colVec}: Kronecker product. If \texttt{rowVec} has
  length \texttt{m} and \texttt{colVec} has length \texttt{n}, then the
  resulting matrix is \texttt{m}-by-\texttt{n}.
\item
  \texttt{dot(vec1,\ vec2)}: dot product of \texttt{vec1} and
  \texttt{vec2}, \texttt{vec1} and \texttt{vec2} must be of the same
  length.
\item
  \texttt{sum(rowVec1\ .*\ rowVec2)}: \texttt{dot(rowVec1,\ rowVec2)}.
\item
  \texttt{rowVec1\ *\ rowVec2\textquotesingle{}}:
  \texttt{dot(rowVec1,\ rowVec2)}.
\item
  \texttt{indices\ =\ find(vec\ \textgreater{}\ n)}: find indices of
  elements greater than \texttt{n} in \texttt{vec}. Note:
  \texttt{\textgreater{}} can also be \texttt{\textless{}}, \texttt{==}.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Array Operations: 2-D Array}
\protect\hypertarget{array-operations-2-d-array}{}
\begin{itemize}[<+->]
\tightlist
\item
  \texttt{mat\ =\ mat1\ .*\ mat2}: elementwise multiplication, where
  \texttt{mat(i,\ j)\ =\ mat1(i,\ j)\ *\ mat2(i,\ j)}.
\item
  \texttt{mat\ =\ mat1\ *\ mat2}: matrix multiplication, where
  \texttt{mat1} is \texttt{m}-by-\texttt{p}, \texttt{mat2} is
  \texttt{p}-by-\texttt{n}, and \texttt{mat} is
  \texttt{m}-by-\texttt{n}.
\item
  \texttt{sum/prod(mat,\ \textquotesingle{}all\textquotesingle{})}:
  sum/product of all elements of \texttt{mat}.
\item
  \texttt{sum/prod(mat,\ 1)}: column sums/products.
\item
  \texttt{sum/prod(mat,\ 2)}: row sums/products.
\item
  \texttt{max/min(mat,\ {[}{]},\ \textquotesingle{}all\textquotesingle{})}:
  maximum/minimum of \texttt{mat}.
\item
  \texttt{max/min(mat,\ {[}{]},\ 1)}: column maximums/minimums.
\item
  \texttt{max/min(mat,\ {[}{]},\ 2)}: row maximums/minimums.
\item
  \texttt{{[}row,\ col{]}\ =\ find(mat\ \textgreater{}\ n)}: find
  indices of elements greater than \texttt{n} in \texttt{mat},
  \texttt{row}/\texttt{col} stores row/column indices.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Array Operations: 2-D Array}
\protect\hypertarget{array-operations-2-d-array-1}{}
\begin{itemize}[<+->]
\tightlist
\item
  \texttt{{[}V,\ D{]}\ =\ eig(mat)}: \texttt{V(:,\ i)} and
  \texttt{D(i,\ i)} are the \texttt{i}-th eigenvector and eigenvalue of
  \texttt{mat}.
\item
  \texttt{d\ =\ diag(mat,\ k)}: extract \texttt{k}-th diagonal elements
  that is above (\texttt{k\ \textgreater{}\ 0}) / below
  (\texttt{k\ \textless{}\ 0}) the main diagonal.
\item
  \texttt{mat\ =\ diag(d,\ k)}: construct a matrix with \texttt{k}-th
  diagonal elements being \texttt{d}.
\item
  \texttt{mat\ =\ diag(diag(mat,\ k))}: set elements to zero except the
  \texttt{k}-th diagonal elements.
\item
  \texttt{fliplr(mat)}: flip \texttt{mat} in left/right direction.
\item
  \texttt{flipud(mat)}: flip \texttt{mat} in up/down direction.
\item
  \texttt{rot90(mat,\ k)}: rotate \texttt{mat} \texttt{k\ *\ 90}
  degrees.
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Application: Image Processing}
\protect\hypertarget{application-image-processing}{}
\begin{itemize}[<+->]
\item
  A grayscale image is a 2-D array of pixels, each pixel has a integer
  value that represent depth of color.
\item
  A colored image is a 3-D array of pixels with RGB channels, each
  channel is a 2-D array.
\item
  \texttt{img\ =\ imread(filename)}: read image from graphics file
  \texttt{filename} and assign it \texttt{img}.
\item
  \texttt{imshow(img)}: display image \texttt{img} in handle graphics
  figure.
\item
  \texttt{imwrite(img,\ filename)}: write image \texttt{img} to graphics
  file named \texttt{filename}.

\begin{verbatim}
uw = imread('UW.png');
uwFlipud = flipud(uw);
imshow(uwFlipud);
imwrite(uwFlipud, 'UW_flipud.png');
\end{verbatim}
\end{itemize}
\end{frame}

\begin{frame}{Summary}
\protect\hypertarget{summary}{}
\begin{table}[!hbtp]
  \begin{tabular}{rl}
    Command                          & Description \\
    \hline
    \texttt{transpose} or \texttt{'} & Non-conjugate transpose of a vector \\
    \texttt{linspace}                & Linearly spaced vector \\
    \texttt{logspace}                & Logarithmically spaced vector \\
    \texttt{colon} or \texttt{:}     & Colon \\
    \texttt{zeros}                   & Zeros array \\
    \texttt{ones}                    & Ones array \\
    \texttt{eye}                     & Identity matrix \\
    \texttt{rand}                    & Uniformly distributed pseudorandom numbers \\
    \texttt{randn}                   & Normally distributed pseudorandom numbers \\
    \texttt{magic}                   & Magic square \\
    \texttt{size}                    & Size of array \\
    \texttt{length}                  & Length of vector \\
    \texttt{reshape}                 & Reshape array \\
  \end{tabular}
\end{table}
\end{frame}

\begin{frame}{Summary}
\protect\hypertarget{summary-1}{}
\begin{table}[!hbtp]
  \begin{tabular}{rl}
    Command                          & Description \\
    \hline
    \texttt{diag}                    & Diagonal matrices and diagonals of a matrix \\
    \texttt{cell}                    & Create cell array \\
    \texttt{sum}/\texttt{prod}       & Sum/Product of elements \\
    \texttt{min}/\texttt{max}        & Minimum/Maximum of elements \\
    \texttt{dot}                     & Vector dot product \\
    \texttt{find}                    & Find indices of nonzero elements \\
    \texttt{eig}                     & Find eigenvalues and eigenvectors \\
    \texttt{diag}                    & Diagonal matrices and diagonals of a matrix \\
    \texttt{fliplr}/\texttt{flipud}  & Flip an array \\
    \texttt{rot90}                   & Rotate an array 90 degrees \\
    \texttt{imread}/\texttt{imwrite} & Read/Write image from graphics file \\
    \texttt{imshow}                  & display image in Handle Graphics figure \\
    \texttt{uint8}                   & Convert to unsigned 8-bit integer \\
  \end{tabular}
\end{table}
\end{frame}

\begin{frame}{Additional Commands}
\protect\hypertarget{additional-commands}{}
\begin{table}[!hbtp]
  \begin{tabular}{rl}
    Command            & Description \\
    \hline
    \texttt{iskeyword} & Check if input is a keyword \\
    \texttt{who}       & List current variables \\
    \texttt{whos}      & List current variables, long form \\
    \texttt{which}     & Locate functions and files \\
    \texttt{clear}     & Clear variables and functions from memory \\
    \texttt{clc}       & Clear command window \\
    \texttt{clf}       & Clear current figure \\
    \texttt{close}     & Close figure \\
    \texttt{exist}     & Check existence of variable/script/function/folder/class \\
    \texttt{disp}      & Display array \\
  \end{tabular}
\end{table}
\end{frame}

\hypertarget{script-files}{%
\subsection{Script Files}\label{script-files}}

\begin{frame}{}
\protect\hypertarget{section-1}{}
A script file is simply a file that contains a chain of commands that
you edit in a separate window, then execute with a single mouse click or
command. This is where we can define variables, perform calculations and
leave comments to remind us what the file calculates.
\end{frame}

\begin{frame}{File Naming Conventions}
\protect\hypertarget{file-naming-conventions}{}
\begin{itemize}[<+->]
\tightlist
\item
  Start with a letter, followed by letters or numbers or underscore,
  maximum 63 characters (excluding the .m extension), and must not be
  the same as any MATLAB reserved word.
\item
  None of the conventions matter to MATLAB itself: they only matter to
  the people writing the code, and the people maintaining the code
  (usually a much harder task), and to the people paying for the code
  (you'd be amazed how much gets written into contract specifications.)
\item
  Reference:
  \url{https://www.mathworks.com/matlabcentral/answers/30223-what-are-the-rules-for-naming-script-files}
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Put Comments to Your Script File}
\protect\hypertarget{put-comments-to-your-script-file}{}
\begin{verbatim}
% MATH 3341, Semester Year
% Lab 02: Variables, Arrays, and Scripts
% Author: first_name last_name
% Date: mm/dd/yyyy
\end{verbatim}
\end{frame}

\begin{frame}{Useful MATLAB Shortcuts}
\protect\hypertarget{useful-matlab-shortcuts}{}
\begin{itemize}[<+->]
\tightlist
\item
  Windows shortcuts

  \begin{itemize}[<+->]
  \tightlist
  \item
    Press \fbox{\texttt{Ctrl}} + \fbox{\texttt{A}} to select all
  \item
    Press \fbox{\texttt{Ctrl}} + \fbox{\texttt{I}} to adjust indentation
  \item
    Press \fbox{\texttt{Ctrl}} + \fbox{\texttt{R}} to comment
  \item
    Press \fbox{\texttt{Ctrl}} + \fbox{\texttt{T}} to uncomment
  \end{itemize}
\item
  macOS shortcuts

  \begin{itemize}[<+->]
  \tightlist
  \item
    Press \fbox{\texttt{command}} + \fbox{\texttt{A}} to select all
  \item
    Press \fbox{\texttt{command}} + \fbox{\texttt{I}} to adjust
    indentation
  \item
    Press \fbox{\texttt{command}} + \fbox{\texttt{/}} to comment
  \item
    Press \fbox{\texttt{command}} + \fbox{\texttt{T}} to uncomment
  \end{itemize}
\end{itemize}
\end{frame}

\hypertarget{primer}{%
\subsection{\texorpdfstring{\LaTeX~Primer}{~Primer}}\label{primer}}

\begin{frame}[fragile]{\texttt{table} Environment}
\protect\hypertarget{table-environment}{}
\begin{verbatim}
\begin{table}[!hbtp]
  \caption{This is a table}
  \begin{tabular}{rcl}
  \toprule
  Column 1 & Column 2 & Column 3 \\
  \midrule
  1        & 1        & 1        \\
  12       & 12       & 12       \\
  123      & 123      & 123      \\
  \bottomrule
  \end{tabular}
\end{table}
\end{verbatim}
\end{frame}

\begin{frame}{\texttt{table} Environment}
\protect\hypertarget{table-environment-1}{}
\begin{table}[!hbtp]
  \caption{This is a table}
  \begin{tabular}{rcl}
  \toprule
  Column 1 & Column 2 & Column 3 \\
  \midrule
  1        & 1        & 1        \\
  12       & 12       & 12       \\
  123      & 123      & 123      \\
  \bottomrule
  \end{tabular}
\end{table}
\end{frame}

\begin{frame}[fragile]{\texttt{figure} Environment}
\protect\hypertarget{figure-environment}{}
\begin{verbatim}
\begin{figure}[!hbtp]
  \centering
  \includegraphics[height=0.3\textheight]{figure.pdf}
  \caption{Plot of $\sin{x}$}
  \label{fig:sin}
\end{figure}
\end{verbatim}

generates

\begin{figure}[!hbtp]
  \centering
  \includegraphics[height=0.3\textheight]{figure.pdf}
  \caption{Plot of $\sin{x}$}
  \label{fig:sin}
\end{figure}
\end{frame}

\begin{frame}[fragile]{\texttt{\textbackslash{}left} and
\texttt{\textbackslash{}right} vs.~\texttt{\textbackslash{}big},
\texttt{\textbackslash{}Big}, \texttt{\textbackslash{}Bigg}}
\protect\hypertarget{left-and-right-vs.-big-big-bigg}{}
\begin{verbatim}
\begin{align*}
\|x\|_2 & = \big(\sum_{i = 1}^{n} x_i^2 \big)^{1/2},
\|x\|_2 = \Big(\sum_{i = 1}^{n} x_i^2 \Big)^{1/2}, \\
\|x\|_2 & = \Bigg(\sum_{i = 1}^{n} x_i^2 \Bigg)^{1/2},
\|x\|_2 = \left(\sum_{i = 1}^{n} x_i^2 \right)^{1/2}.
\end{align*}
\end{verbatim}

generates \begin{align*}
\|x\|_2 & = \big(\sum_{i = 1}^{n} x_i^2 \big)^{1/2},
\|x\|_2 = \Big(\sum_{i = 1}^{n} x_i^2 \Big)^{1/2}, \\
\|x\|_2 & = \Bigg(\sum_{i = 1}^{n} x_i^2 \Bigg)^{1/2},
\|x\|_2 = \left(\sum_{i = 1}^{n} x_i^2 \right)^{1/2}.
\end{align*}
\end{frame}

\begin{frame}[fragile]{Links}
\protect\hypertarget{links}{}
\begin{verbatim}
\href{https://www.google.com}{Google}
\end{verbatim}

\href{https://www.google.com}{Google}

Or simply

\begin{verbatim}
\url{https://www.google.com}
\end{verbatim}

\url{https://www.google.com}
\end{frame}

\begin{frame}[fragile]{\texttt{case} Environment}
\protect\hypertarget{case-environment}{}
\begin{verbatim}
$$
f(x) =
\begin{cases}
5 x + 4   & \text{if~} x \leq 1, \\
3 x^2 + 6 & \text{if~} x > 1
\end{cases}
$$
\end{verbatim}

generates \[
f(x) =
\begin{cases}
5 x + 4   & \text{if~} x \leq 1, \\
3 x^2 + 6 & \text{if~} x > 1
\end{cases}
\]
\end{frame}

\begin{frame}[fragile]{Cross-Reference}
\protect\hypertarget{cross-reference}{}
\begin{verbatim}
\begin{equation}
\label{eq:ls}
A \mathbf{x} = \mathbf{b}.
\end{equation}

The expression \eqref{eq:ls} is a linear system.
\end{verbatim}

generates

\begin{equation}
\label{eq:ls}
A \mathbf{x} = \mathbf{b}.
\end{equation}

The expression \eqref{eq:ls} is a linear system.
\end{frame}

\begin{frame}[fragile]{Cross-Reference}
\protect\hypertarget{cross-reference-1}{}
\begin{verbatim}
\begin{table}[!hbtp]
\caption{$y = 2x$}
\label{tab:xy}
  \begin{tabular}{cc}
  \toprule
  $x$ & $y$ \\
  \midrule
  $6$ & $12$ \\
  $7$ & $14$ \\
  $8$ & $16$ \\
  \bottomrule
  \end{tabular}
\end{table}
Table \ref{tab:xy} gives the result of $y = 2x$.
\end{verbatim}
\end{frame}

\begin{frame}{Cross-Reference}
\protect\hypertarget{cross-reference-2}{}
\begin{table}[!hbtp]
\caption{$y = 2x$}
\label{tab:xy}
  \begin{tabular}{cc}
  \toprule
  $x$ & $y$ \\
  \midrule
  $6$ & $12$ \\
  $7$ & $14$ \\
  $8$ & $16$ \\
  \bottomrule
  \end{tabular}
\end{table}

Table \ref{tab:xy} gives the result of \(y = 2x\).
\end{frame}


\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{geometry}
\geometry{verbose,letterpaper,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
\usepackage{amsmath}

\makeatletter
\usepackage{babel}
\makeatother
\begin{document}

\section{FEM model}

\begin{quote}
The problem is

\begin{align*}
\Delta\bar{p}-\frac{1}{c_{0}^{2}}\frac{\partial^{2}\bar{p}}{\partial^{2}t} & =f\end{align*}

with initial condition

\begin{eqnarray*}
\bar{p}(0,\mathbf{r}) & = & b(r)\end{eqnarray*}

Let

\begin{eqnarray*}
v & = & \frac{\partial\bar{p}}{\partial t}\end{eqnarray*}

then we have

\begin{alignat*}{1}
\bar{p}_{t}-v & =0\\
\Delta\bar{p}-\frac{1}{c_{0}^{2}}\, v_{t} & =f\end{alignat*}

and absorbing boundary condition

\begin{eqnarray*}
\frac{\partial\bar{p}}{\partial\mathbf{n}} & =- & \frac{1}{c_{0}}\frac{\partial\bar{p}}{\partial t}\end{eqnarray*}

$\frac{\partial\bar{p}}{\partial\mathbf{n}}$ is the normal derivative
at the boundary. This is a the time-varying FEM model. by discretizing
according to $t$, we have

\begin{eqnarray*}
(\frac{\bar{p}^{n}-\bar{p}^{n-1}}{\delta t},\phi)_{\Omega}-\,(\theta v^{n}+(1-\theta)v^{n-1},\phi)_{\Omega} & = & 0\\
-(\Delta((\theta\bar{p}^{n}+(1-\theta)\bar{p}^{n-1}),\bigtriangledown\phi)_{\Omega}-\frac{1}{c_{0}}(\frac{\bar{p}^{n}-\bar{p}^{n-1}}{\delta t},\phi)_{\partial\Omega}-\frac{1}{c_{0}^{2}}(\frac{v^{n}-v^{n-1}}{\delta t},\phi)_{\Omega} & = & (\theta f^{n}+(1-\theta)f^{n-1},\phi)_{\Omega}\end{eqnarray*}

we obtain

\begin{eqnarray*}
M\bar{p}^{n}-(\delta t\,\theta)Mv^{n} & = & M\bar{p}^{n-1}+\delta t\,(1-\theta)\, M\, v^{n-1}\\
(-c_{0}^{2}\,\delta t\,\theta A-c_{0}\, B)\bar{p}^{n}-Mv^{n} & = & (c_{0}^{2}\,\delta t\,(1-\theta)A-c_{0}B)\bar{p}^{n-1}-M\, v^{n-1}+c_{0}^{2}\delta t(\theta F^{n}+(1-\theta)F^{n-1})\end{eqnarray*}

Write the above two equations as a matrix form

\begin{eqnarray*}
\left(\begin{array}{cc}
M & -(\delta t\,\theta)M\\
c_{0}^{2}\,\delta t\,\theta A+c_{0}\, B & M\end{array}\right)\left(\begin{array}{c}
\bar{p}^{n}\\
v^{n}\end{array}\right) & = & \left(\begin{array}{c}
G_{1}\\
G_{2}\end{array}\right)\end{eqnarray*}

where

\begin{center}$\left(\begin{array}{c}
G_{1}\\
G_{2}\end{array}\right)=\left(\begin{array}{c}
M\bar{p}^{n-1}+\delta t\,(1-\theta)Mv^{n-1}\\
(-c_{0}^{2}\,\delta t\,(1-\theta)A+c_{0}B)\bar{p}^{n-1}+M\, v^{n-1}-c_{0}^{2}\delta t(\theta F^{n}+(1-\theta)F^{n-1})\end{array}\right)$\end{center}

From the above matrix, we can obtain

\begin{eqnarray*}
(M+(\delta t\,\theta\, c_{0})^{2}A+c_{0}\,\delta t\,\theta\, B)\bar{p}^{n} & = & G_{1}+(\delta t\,\theta)G_{2}\\
Mv^{n} & = & -(c_{0}^{2}\,\delta t\,\theta\, A+c_{0}B)\bar{p}^{n}+G_{2}\end{eqnarray*}

\end{quote}

\end{document}


% Created 2018-02-16 vie 11:46
\documentclass[letterpaper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\usepackage{geometry}
\geometry{bmargin=12mm, tmargin=12mm}
\author{Kjartan Halvorsen}
\date{}
\title{Implementing on an Arduino}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 24.5.1 (Org mode 8.2.10)}}
\begin{document}

\maketitle

\section*{Implementation of the discrete lead compensator}
\label{sec-1}
\[F_d(z) = 10 \frac{0.81z - 0.60}{z-0.45}\]

\begin{verbatim}
// Sampling time
int hMilliSec =        // h=0.2 s

// values to be stored between sampling intervals

double
double

// Functions to read signals from input channels
double read_y_value(){
...
}
double read_yref_value(){
...
}
// Function to write control signal to output channel
void write_u_value(){
...
}

void loop() {
// Read inputs
y = read_y_value();
yref = read_yref_value();

// Compute control signal

double u =

// Write control signal
write_u_value(u);

// Store values for the next iteration

// Wait till next sampling instant
delay(hMillisec);
}
\end{verbatim}
% Emacs 24.5.1 (Org mode 8.2.10)
\end{document}


\documentclass{article}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\usepackage{proof}
\usepackage{cite}
\usepackage{hyperref}

\begin{document}

\title{CS292C Final Project proof of type soundness}
\author{Daniel Zhang}
\maketitle

\section{Introduction}

For this project I've decided to prove type soundness for and implement the Calculus of Constructions. Extensions of the Calculus of Constructions are used in Coq and Agda \cite{Casinghino10}, so understanding it will help with understanding those systems. I will not be describing extensions such as inductive types or universes.

The Calculus of Constructions is a Pure Type System. Pure Type Systems have a set of sorts. The Calculus of Constructions has three sorts: terms, types and kinds (the type of types).

$\star$ is a constant of sort kind. Note that it is not the only kind. $\star\rightarrow\star$ is a different kind. This corresponds to the type of a type constructor: a function that takes in a type and outputs a type.

The notation $\lambda x:A.B$ is used both for a function that takes in a term and a function that takes in a type.
$\Pi x:A.B$ is a dependent product. If $x$ does not occur in $B$, it is the same as $A\rightarrow B$. When $A$ is $\star$, it is equivalent to a universal type. \cite{Jones97}

An example of a term would be $\lambda T:*.(\lambda x:T.x)$. Types include expressions such as $\lambda x:\star.x$ and $\Pi x:\star.x$. An example of a kind would be $\Pi x:\star.\star$.

The Calculus of Constructions is the most powerful system in the Lambda Cube. Both types and terms can depend on types and terms.
The Calculus of Constructions is known to be strongly-normalizing, but the proof is considered difficult \cite{Casinghino10}.

\section{Syntax}

Expressions are considered equivalent up to $\alpha$-conversion.
Variables names are assumed to all be distinct (implementation uses de Bruijn indices, so substitution doesn't cause issues).

\[k\in \text{Const} \rightarrow \star \mid \square\]
\[e\in \text{Exp}\rightarrow k\in \text{Const} \mid x\in \text{Variable} \mid A B \mid \lambda x:A.B \mid \Pi x:A.B\]
This is the syntax for both terms and types.

\[v\in \text{Values}\rightarrow k\in \text{Const} \mid x\in \text{Variable} \mid \lambda x:A.B \mid \Pi x: A. B\]
where $A,B\in\text{Values}$.
\section{Semantics}

These rules are from \cite{Casinghino10}, modifed to be deterministic.

\[\infer[(LAM1)]{(\lambda x:A.B)\rightarrow (\lambda x:A'.B)}{A\rightarrow A'}\]
\[\infer[\text{if $A\in\text{Values}$}(LAM2)]{(\lambda x:A.B)\rightarrow (\lambda x:A.B')}{B\rightarrow B'}\]
\[\infer[(PI1)]{(\Pi x:A.B)\rightarrow (\Pi x:A'.B)}{A\rightarrow A'}\]
\[\infer[\text{if $A\in\text{Values}$}(PI2)]{(\Pi x:A.B)\rightarrow (\Pi x:A.B')}{B\rightarrow B'}\]
\[\infer[\text{if APP3 cannot apply}(APP1)]{A B\rightarrow A' B}{A\rightarrow A'}\]
\[\infer[\text{if $A\in \text{Values}$ and APP3 cannot apply}(APP2)]{A B\rightarrow A B'}{B\rightarrow B'}\]
\[\infer[(APP3)]{(\lambda x:A.B) C\rightarrow B[x\mapsto C]}{}\]

\section{Substitution}

\[k[x\mapsto D]=k\]
\[
y[x\mapsto D]=
\begin{cases}
  D & \text{if $x=y$}\\
  y & \text{if $x\ne y$}
\end{cases}
\]
\[(A B)[x\mapsto D]=A[x\mapsto D] B[x\mapsto D]\]
\[(\lambda y:A.B)[x\mapsto D]=\lambda y:A[x\mapsto D].B[x\mapsto D]\]
\[(\Pi y:A.B)[x\mapsto D]=\Pi y:A[x\mapsto D].B[x\mapsto D]\]

\[(\Gamma,y:B)[x\mapsto D]=\Gamma[x\mapsto D],y:B[x\mapsto D]\]

\section{Typing rules}

\[\infer[(STAR)]{\vdash\star:\square}{}\]
\[\infer[(VAR)]{\Gamma,x:A\vdash x:A}{\Gamma\vdash A:s}\]
\[\infer[(WEAK)]{\Gamma,x:A\vdash B:C}{\Gamma\vdash B:C & \Gamma\vdash A:s}\]
\[\infer[(APP)]{\Gamma\vdash f a:B[x\mapsto a]}{\Gamma\vdash f:(\Pi x:A.B) & \Gamma\vdash a:A}\]
\[\infer[(LAM)]{\Gamma\vdash(\lambda x:A.b):(\Pi x:A.B)}{\Gamma,x:A\vdash b:B & \Gamma\vdash(\Pi x:A.B):t}\]
\[\infer[(PI)]{\Gamma\vdash(\Pi x:A.B):t}{\Gamma\vdash A:s & \Gamma,x:A\vdash B:t & s,t\in\{\star,\square\}}\]
\[\infer[(CONV)]{\Gamma\vdash a:B}{\Gamma\vdash a:A & \Gamma\vdash B:s & A=_\beta B}\]

\[\infer[(=_\beta LAM)]{(\lambda x:A.B)C=_\beta B[x\mapsto C]}{}\]
and its symmetric transitive closure.

These rules are taken from \cite{Jones97} with some modifications. The notation for substitution was changed to match what was used in this class. The version in the paper had a relation on $s$ and $t$ in rule (PI) to restrict to other type systems in the Lambda Cube. The eight systems of the Lambda Cube correspond to all choices of pairs of valid $(s,t)$ in rule PI, where $(\star,\star)$ must be taken. The Calculus of Constructions allows all pairs of sorts on $\{\star,\square\}$.

\section{Proof of progress}

\begin{lemma}
  Let $e$ be a value.
  If $\Gamma\vdash e:\Pi x:A.B$, then $e=(\lambda x:C.D)$.
\end{lemma}

\begin{proof}
  Since $e$ is a value, it must either be of the form $\lambda x:C.D$ or $\Pi x:C.D$. Let $T$ the last type before trailing applications of CONV or WEAK: $\Gamma'\vdash e:T$ and $T=_\beta \Pi x.A:B$. If $e=\Pi x:C.D$, then $T\in\{\star,\square\}$ since $PI$ is the only rule that could be applied based on the form of $e$. But since neither of $\star$ or $\square$ is $\beta$-equivalent to $\Pi x:C.D$, this is a contradiction. Therefore, $e=\lambda x:C.D$.

\end{proof}

\begin{theorem}
  $(\forall e \in Exp)(\vdash e:\tau)\Rightarrow(e \in Values\vee(\exists e'\in Exp)(e\rightarrow e'))$
\end{theorem}

\begin{proof}
  Use structural induction on $e$:

  Case $e\in Const$: $e\in Values$

  Case $e\in Variable$: A variable can only have a type if it is in the context. Since we are in the empty context, a variable cannot be well-typed. Thus, this case cannot happen.

  Case $e=A B$:
  \begin{itemize}
  \item If $A$ is not a value, then $A\rightarrow A'$ by the inductive hypothesis. $e\rightarrow e'$ where $e'=A' B$ (rule APP1).
  \item If $A$ is a value but $B$ is not a value, then $B\rightarrow B'$ by the inductive hypothesis. Thus, $e\rightarrow e'$ where $e'=A B'$ (rule APP2).
  \item If $A$ and $B$ are both values: After removing trailing application of CONV from the typing derivation for $\tau$, we get $A B=\tau'$ where $\tau=_\beta \tau'$. The last rule used in the typing derivation for $\tau'$ is not CONV, so it must be APP. Thus, $A:\Pi x: C.D$. Then by Lemma 1, $A=(\lambda x:\tau.e_3)$, so $e=(\lambda x:\tau.e_3)B$. Finally, $e\rightarrow e'$ where $e'=e_3[x\mapsto B]$ (rule APP3).
  \end{itemize}

  Case $e=(\lambda x:A.B)$: If $A$ is not a value, then $A\rightarrow A'$ by the inductive hypothesis. $e\rightarrow e'$ where $e'=(\lambda x:A'.B)$. Otherwise, if $B$ is not a value, then $B\rightarrow B'$ by the inductive hypothesis. $e\rightarrow e'$ where $e'=(\lambda x:A.B')$. If both $A$ and $B$ are values, then $e\in Values$.

  Case $e=(\Pi x:A.B)$: If $A$ is not a value, then $A\rightarrow A'$ by the inductive hypothesis. $e\rightarrow e'$ where $e'=(\Pi x:A'.B)$. Otherwise, if $B$ is not a value, then $B\rightarrow B'$ by the inductive hypothesis. $e\rightarrow e'$ where $e'=(\Pi x:A.B')$. If both $A$ and $B$ are values, then $e\in Values$.

  This exhausts all the cases, so the theorem is true by induction.

\end{proof}

\section{Proof of preservation}

Compared to simply-typed lambda calculus, a major differences in the proving the Substitution Lemma is the type of a variable can depend on the value of another variable. This means order of variables in the context matters. Also, we must use the version of substitution that allows substituting arbitrary terms because rule APP requires it.

\begin{lemma}[Substitution Lemma for Terms]
  If $y$ is fresh in $D$,
  \[e[x\mapsto D][y\mapsto C[x\mapsto D]]=e[y\mapsto C][x\mapsto D]\]
\end{lemma}
\begin{proof}
  The proof is by induction on the structure of $e$.

  Case $e=k$: $k[x\mapsto D][y\mapsto C[x\mapsto D]]=k=k[y\mapsto C][x\mapsto D]$, as desired.

  Case $e=A B$:
  \begin{align*}
    (A B)[x\mapsto D][y\mapsto C[x\mapsto D]]&=A[x\mapsto D][y\mapsto C[x\mapsto D]]B[x\mapsto D][y\mapsto C[x\mapsto D]]\\
    &=A[y\mapsto C][x\mapsto D]B[y\mapsto C][x\mapsto D]\\
    &=(A B)[y\mapsto C][x\mapsto D]
  \end{align*}
  Using the inductive hypothesis on $A$ and $B$.

  Case $e=(\lambda x:A.B)$: identical to $A B$.

  Case $e=(\Pi x:A.B)$: identical to $A B$.

  Case $e=x$:
  \begin{align*}
    x[x\mapsto D][y\mapsto C[x\mapsto D]]&=D[y\mapsto C[x\mapsto D]]\\
    &=D\\
    &=x[x\mapsto D]\\
    &=x[y\mapsto C][x\mapsto D]
  \end{align*}

  Case $e=y$ where $y\ne x$:
  \begin{align*}
    y[x\mapsto D][y\mapsto C[x\mapsto D]]&=y[y\mapsto C[x\mapsto D]]\\
    &=C[x\mapsto D]\\
    &=y[y\mapsto C][x\mapsto D]
  \end{align*}

\end{proof}

\begin{lemma}[Substitution preserves $\beta$-equivalence]
  \[\text{If $A=_\beta B$, then $A[x\mapsto D]=_\beta B[x\mapsto D]$}\]
\end{lemma}
\begin{proof}
  It suffices to show that $((\lambda y:A.B)C)[x\mapsto D]=_\beta(B[y\mapsto C])[x\mapsto D]$

  \begin{align*}
  ((\lambda y:A.B)C)[x\mapsto D]&=((\lambda y:A.B)[x\mapsto D]C[x\mapsto D])\\
    &=((\lambda y:A[x\mapsto D].B[x\mapsto D])C[x\mapsto D])\\
    &=_\beta B[x\mapsto D][y\mapsto C[x\mapsto D]]\\
    &=B[y\mapsto C][x\mapsto D]
  \end{align*}
  since $y$ is fresh in $D$ (Substitution Lemma for Terms).
\end{proof}

\begin{lemma}[Substitution Lemma]
  \[\text{If $\Gamma,x:A,\Delta\vdash B:C$ and $\Gamma\vdash D:A$, then $\Gamma,\Delta[x\mapsto D]\vdash B[x\mapsto D]:C[x\mapsto D]$}\]
\end{lemma}
This is Lemma 5.2.1 in \cite{Barendregt92}.
\begin{proof}
  Use induction on the length of the derivation of $\Gamma,x:A,\Delta,\vdash B:C$.

  Abbreviate $M[x\mapsto D]$ as $M^*$. Note if $x$ is fresh in $M$, then $M=M^*$.

  Look at the last rule used in the derivation of $\Gamma,x:A,\Delta,\vdash B:C$.

  Case STAR: This case cannot apply because STAR only applies in the empty context, but the context contains $x$.

  Case WEAK: If $\Delta=<>$, the last rule was
  \[\infer{\Gamma,x:A\vdash B:C}{\Gamma\vdash B:C & \Gamma\vdash A:s}\]
  Since $x$ is fresh in $B$ and $C$, $B=B^*$, and $C=C^*$. Thus,
  \[\Gamma\vdash B^*:C^*\]
  as desired.
  On the other hand, if $\Delta=\Delta',y:E$, the last rule was
  \[\infer{\Gamma,x:A,\Delta',y:E\vdash B:C}{\Gamma,x:A,\Delta'\vdash B:C & \Gamma\vdash E:s}\]

  By the inductive hypothesis, $\Gamma,\Delta'^*\vdash B^*:C^*$. Also, since $x$ is fresh in $E$, $E=E^*$. By rule WEAK,

  \[\infer{\Gamma,\Delta'^*,y:E^*\vdash B^*:C^*}{\Gamma,\Delta'^*\vdash B^*:C^* & \Gamma\vdash E^*:s}\]

  Since $\Delta^*=\Delta'^*,y:E^*$,
  \[\Gamma,\Delta^*\vdash B^*:C^*\]
  as desired.

  Case VAR: If $\Delta=<>$, the last rule was
  \[\infer{\Gamma,x:A\vdash x:A}{\Gamma\vdash A:s}\]
  Since $\Gamma\vdash D:A$, $x^*=D$, and $A=A^*$,
  \[\Gamma\vdash x^*:A^*\]
  as desired.
  On the other hand, if $\Delta=\Delta',y:E$, the last rule was
  \[\infer{\Gamma,x:A,\Delta',y:E\vdash y:E}{\Gamma,x:A,\Delta'\vdash E:s}\]
  By the inductive hypothesis,
  \[\Gamma,\Delta'^*\vdash E^*,s^*\]
  Now apply rule VAR:
  \[\infer{\Gamma,\Delta'^*,y:E^*\vdash y:E^*}{\Gamma,\Delta'^*\vdash E^*,s^*}\]
  Since $\Delta^*=\Delta'^*,y:E^*$ and $y=y^*$,
  \[\Gamma,\Delta^*\vdash y^*:E^*\]

  as desired.

  Case PI: The last rule was
  \[\infer{\Gamma,x:A,\Delta\vdash(\Pi y:E.F):t}{\Gamma,x:A,\Delta\vdash A:s & \Gamma,x:A,\Delta,y:E\vdash F:t}\]
  By the inductive hypothesis, $\Gamma,\Delta^*\vdash A^*:s^*$ and $\Gamma,\Delta^*,y:E^*\vdash F^*:t^*$
  Applying rule PI, we get
  \[\infer{\Gamma,\Delta^*\vdash (\Pi y:E^*:F^*):t^*}{\Gamma,\Delta^*\vdash A^*:s^* & \Gamma,\Delta^*,y:E^*\vdash F^*:t^*}\]
  as desired

  Case LAM: The last rule was
  \[\infer{\Gamma,x:A,\Delta\vdash(\lambda y:E.f):(\Pi y:E.F)}{\Gamma,x:A,\Delta,y:E\vdash f:F & \Gamma,x:A,\Delta\vdash(\Pi y:E.F):t}\]
  By the inductive hypothesis, $\Gamma,\Delta^*,y:E^*\vdash f^*:F^*$ and $\Gamma,\Delta^*\vdash(\Pi y:E^*.F^*):t^*$
  Applying rule LAM,
  \[\infer{\Gamma,\Delta^*\vdash(\lambda y:E^*,f^*):(\Pi y:E^*:F^*)}{\Gamma,\Delta^*,y:E^*\vdash f^*:F^* & \Gamma,\Delta^*\vdash(\Pi y:E^*.F^*):t^*}\]
  as desired.

  Case CONV: The last rule was
  \[\infer{\Gamma,x:A,\Delta\vdash e:F}{\Gamma,x:A,\Delta\vdash e:E & \Gamma,x:A,\Delta\vdash F:s & E=_\beta F}\]
  By the inductive hypothesis, $\Gamma,\Delta^*\vdash e^*:E^*$ and $\Gamma,\Delta^*\vdash F^*:s^*$. Also, $E^*=_\beta F^*$ since substitution preserves $\beta$-equivalence. By rule LAM,
  \[\infer{\Gamma,\Delta^*\vdash e^*:F^*}{\Gamma,\Delta^*\vdash e^*:E^* & \Gamma,\Delta^*\vdash F^*:s^* & E^*=_\beta F^*}\]

  This exhausts all the cases, so the lemma is true by induction.
\end{proof}

\begin{theorem}[Preservation]
  $(\Gamma\vdash A:B)\wedge(A\rightarrow A')\Rightarrow(\Gamma\vdash A':B)$
\end{theorem}

This is based on Theorem 5.2.15 in \cite{Barendregt92}.

\begin{proof}
  Because the types of variables can contains other variables, we cannot just prove this by induction. Instead, we need to prove two statements simultaneously by induction on generation of $\Gamma\vdash A:B$:
  \[(\Gamma\vdash e:\tau)\wedge(e\rightarrow e')\Rightarrow(\Gamma\vdash e':\tau)\]
  \[(\Gamma\vdash e:\tau)\wedge(\Gamma\rightarrow \Gamma')\Rightarrow(\Gamma'\vdash e:\tau)\]
  where $\Gamma\rightarrow\Gamma'$ if $\Gamma=x_1:A_1,\ldots x_n:A_n$ and $\Gamma'=x_1:A_1'\ldots x_n:A_n'$ where $A_i\rightarrow A_i'$ for exactly one $i$ and for all $j\ne i$ $A_j=_\beta A_j'$. In other words, the type of exactly one variable in the context takes a step.

  Case STAR: This case cannot occur because neither the context nor the term may take a step.

  Case VAR:

  \[\infer{\Gamma,x:A\vdash x:A}{\Gamma\vdash A:s}\]

  Since $x$ cannot take a step, we only need to handle $(\Gamma,x:A)\rightarrow(\Gamma,x:A)'$
  This can happen in two ways:

  If $\Gamma\rightarrow\Gamma'$: By the inductive hypothesis, $\Gamma'\vdash A:s$. Thus,
  \[\infer{\Gamma',x:A\vdash x:A}{\Gamma'\vdash A:s}\]
  as desired.

  If $A \rightarrow A'$: By the inductive hypothesis, $\Gamma\vdash A':s$. Thus,
  \[\infer{\Gamma,x:A'\vdash x:A'}{\Gamma\vdash A':s}\]
  as desired.

  Case WEAK:

  \[\infer{\Gamma,x:A\vdash B:C}{\Gamma\vdash B:C & \Gamma\vdash A:s}\]

  First, consider the case where the term takes a step: $B\rightarrow B'$.
  By the inductive hypothesis, $\Gamma\vdash B':C$. By WEAK, $\Gamma,x:A\vdash B':C$, as desired.
  Now consider if the context takes a step. There are two cases. If $\Gamma\rightarrow\Gamma'$, then by the inductive hypothesis, $\Gamma'\vdash B:C$ and $\Gamma'\vdash A:s$. By WEAK, $\Gamma'\vdash B:C$.
  If $A\rightarrow A'$, then by the inductive hypothesis, $\Gamma\vdash A':s$. By WEAK, $\Gamma,x:A'\vdash B:C$, as desired.

  Case APP:
  \[\infer{\Gamma\vdash f a:B[x\mapsto a]}{\Gamma\vdash f:(\Pi:A.B) & \Gamma\vdash a:A}\]

  If $\Gamma\rightarrow \Gamma'$, by the inductive hypothesis, $\Gamma'\vdash f:(\Pi:A.B)$ and $\Gamma'\vdash a:A$. Thus,
  \[\Gamma\vdash f a:B[x\mapsto a]\]
  as desired.

  There are three ways for the term to take a step.

  If $e'=f' a$ where $f\rightarrow f'$ (APP1), then by the inductive hypothesis, $\Gamma\vdash f':(\Pi:A.B)$, so by APP, $\Gamma\vdash f' a:B[x\mapsto a]$, as desired.
  If $e'=f a'$ where $a\rightarrow a'$ (APP2), then by the inductive hypothesis, $\Gamma\vdash a':A$, so by APP, $\Gamma\vdash f a':B[x\mapsto a']$, as desired.

  If $e'=b[x\mapsto a]$ where $f=(\lambda x:A.b)$ (APP3), then by rules APP and LAM (and possible WEAK and CONV), we have

  \[\infer{\Gamma\vdash(\lambda x:A.b) a:B[x\mapsto a]}{\infer{\Gamma\vdash (\lambda x:A.b):(\Pi x:A.B)}{\infer{\cdots}{\infer{\Delta\vdash (\lambda x:A.b):(\Pi x:A.B)}{\Delta,x:A\vdash b:B & \Delta\vdash (\Pi x:A.B):t}}} & \Gamma\vdash x:A}\]

  We can add back variables to $\Delta$ to get $\Gamma, x:A\vdash b:B$. Also, $\Gamma\vdash a:A$. By the Substitution Lemma. $\Gamma\vdash b[x\mapsto a]:B[x\mapsto a]$, as desired.

  Case LAM: By rules LAM and PI (and possible WEAK and CONV).
  \[\infer{\Gamma\vdash(\lambda x:A.b):(\Pi x:A.B)}{\Gamma,x:A\vdash b:B & \infer{\Gamma\vdash (\Pi x:A.B):t}{\infer{\cdots}{\infer{\Delta\vdash (\Pi x:A.B):u}{\Delta\vdash A:s & \Delta,x:A\vdash B:u}}}}\]

  There are two ways for the term to take a step. If $e'=(\lambda x:A'.b)$ where $A\rightarrow A'$ (LAM1), then by the inductive hypothesis, $\Gamma,x:A'\vdash b:B$, $\Delta\vdash A':s$ and $\Delta,x:A'\vdash B:t$. Thus,

  \[\infer{\Gamma\vdash(\lambda x:A'.b):(\Pi x:A'.B)}{\Gamma,x:A'\vdash b:B & \infer{\Gamma\vdash (\Pi x:A'.B):t}{\infer{\cdots}{\infer{\Delta\vdash (\Pi x:A'.B):u}{\Delta\vdash A':s & \Delta,x:A'\vdash B:u}}}}\]
  as desired.

  If $e'=(\lambda x:A.b')$ where $b\rightarrow b'$ (LAM2), then by the inductive hypothesis, $\Gamma,x:A\vdash b':B$, Thus,

  \[\infer{\Gamma\vdash(\lambda x:A.b'):(\Pi x:A.B)}{\Gamma,x:A\vdash b':B & \Gamma\vdash (\Pi x:A.B):t}\]
    as desired.

    If $\Gamma\rightarrow\Gamma'$, by the inductive hypothesis, $\Gamma',x:A\vdash b:B$ and $\Gamma'\vdash (\Pi x:A.B):t$.

    Thus,
    \[\infer{\Gamma'\vdash(\lambda x:A.b):(\Pi x:A.B)}{\Gamma',x:A\vdash b:B & \Gamma'\vdash (\Pi x:A.B):t}\]
    as desired.

    Case PI: $e=(\Pi x:A_1.A_2)$.

  \[\infer{\Gamma\vdash(\Pi x:A_1.A_2):t}{\Gamma\vdash A_1:s & \Gamma,x:A_1\vdash A_2:t}\]

  Suppose $e\rightarrow e'$. This can happen in two ways.

  If $e'=A_1' A_2$ where $A_1\rightarrow A_1'$ (rule PI1): By the inductive hypothesis, $\Gamma\vdash A_1':s$, $\Gamma,x:A_1'\vdash A_2:t$. Thus, by rule PI,

  \[\infer{\Gamma\vdash(\Pi x:A_1'.A_2):t}{\Gamma\vdash A_1':s & \Gamma,x:A_1'\vdash A_2:t}\]
  as desired.

  If $e'=A_1 A_2'$ where $A_2\rightarrow A_2'$ (rule PI2): By the inductive hypothesis,  $\Gamma,x:A_1\vdash A_2':t$. Thus, by rule PI,
  \[\infer{\Gamma\vdash(\Pi x:A_1.A_2'):t}{\Gamma\vdash A_1:s & \Gamma,x:A_1\vdash A_2':t}\]
  as desired.

  Suppose $\Gamma\rightarrow\Gamma'$. By the inductive hypothesis, $\Gamma'\vdash A_1:s$, $\Gamma',x:A_1\vdash A_2:t$. Thus, by rule PI,

  \[\infer{\Gamma'\vdash(\Pi x:A_1.A_2):t}{\Gamma'\vdash A_1:s & \Gamma',x:A_1\vdash A_2:t}\]
  as desired.

  Case CONV:
  \[\infer{\Gamma\vdash a:B}{\Gamma\vdash a:A & \Gamma\vdash B:s & A=_\beta B}\]

  Suppose $a\rightarrow a'$.

  By the inductive hypothesis, $\Gamma\vdash a':A$, so by rule CONV,
  \[\infer{\Gamma\vdash a':B}{\Gamma\vdash a':A & \Gamma\vdash B:s & A=_\beta B}\]
  as desired.

  Suppose $\Gamma\rightarrow\Gamma'$.
  By the inductive hypothesis, $\Gamma'\vdash a:A$ and $\Gamma'\vdash B:s$, so by rule CONV,
  \[\infer{\Gamma\vdash a':B}{\Gamma\vdash a':A & \Gamma\vdash B:s & A=_\beta B}\]
  as desired.
\end{proof}

\bibliography{citations}{}
\bibliographystyle{plain}
\end{document}


\subsection{Evaluation Metrics}

\subsubsection{Multiclass classification}
For the multiclass dataset we use accuracy and macro-averaged F1 score.

\subsubsection{Accuracy} Accuracy is the percentage of labels for which the class is correctly predicted. Note that for multiclass classification the micro-averaged F1 score is equal to the accuracy.

\subsubsection{Macro-averaged F1 score}
Macro-averaged F1 score is computed by first computing the F1 score for each class independently and then take an averaging all the F1 scores. This metric treats all the classes as equal, independent of their frequency in the test set.

\subsubsection{Multilabel classification}
For multilabel classification we use three metrics - hamming loss, micro-averaged F1 score and macro-averaged F1 score.

\subsubsection{Hamming loss} Hamming loss is the proportion of mis-classified examples in the dataset.
\subsubsection{Micro-averaged F1 score} F-measure averaging on the prediction matrix.

\subsubsection{Macro-averaged F1 score} Macro-averaged F1 score is calculated by computing the F1 score for each of the labels, then averaging the label wise F1 scores.


\chapter{Derived Inference Rules}
\label{derived-rules}

The notion of {\it proof\/} is defined abstractly in the manual
\LOGIC: a proof of a sequent $(\Gamma,t)$ from a set of sequents
$\Delta$ (with respect to a deductive system ${\cal D}$) was defined
to be a chain of sequents culminating in $(\Gamma,t)$, such that every
element of the chain either belongs to $\Delta$ or else follows from
$\Delta$ and earlier elements of the chain by deduction.  The notion
of a {\it theorem\/} was also defined in \LOGIC: a theorem of a
deductive system is a sequent that follows from the empty set of
sequents by deduction; \ie, it is the last element of a proof from the
empty set of sequents, in the deductive system.  In this section,
proofs and theorems are made concrete in \HOL.

The deductive system of \HOL\
was sketched in Section~\ref{rules}, where
the eight families of primitive inferences making up the
deductive system were specified by diagrams. It was explained that
these families of inferences are represented in \HOL\ via
\ML\ functions, and that theorems
are represented by an \ML\ abstract type called \ml{thm}.\index{thm@\ml{thm}}
The eight \ML\ functions corresponding to the inferences
are operations of the type \ml{thm}, and each of the eight
returns a value of type \ml{thm}. It was explained that the
type \ml{thm} has primitive destructors, but no primitive
constructor; and that in that way, the logic is protected against
the computation of theorems except by functions representing
primitive inferences, or compositions of these.

Finally, the primitive \HOL\ logic was supplemented by three primitive
constants and four axioms, to form the basic logic.  The primitive
inferences, together with the primitive constants, the five axioms,
and a collection of definitions, give a starting point for
constructing proofs, and hence computing theorems. However, proving
even the simplest theorems from this minimal basis costs considerable
effort. The basis does not immediately provide the transitivity of
equality, for example, or a means of universal quantification; both of
these themselves have to be derived.

\section{Simple Derivations}

As an illustration of a proof in \HOL{}, the following chain of
theorems forms a proof (from the empty set, in the \HOL{} deductive
system), for the particular terms \ml{``}$t_1$\ml{``}%''
and \ml{``}$t_2$\ml{``},%''
both of \HOL\ type \ml{``:bool``}:%''

\begin{enumerate}
\item $t_1$\ml{ ==> }$t_2$\ml{ |- }$t_1$\ml{ ==> }$t_2$

\item $t_1$\ml{ |- }$t_1$

\item $t_1$\ml{ ==> }$t_2$\ml{, }$t_1$\ml{ |- }$t_2$
\end{enumerate}

\noindent That is, the third theorem follows from the first and second.

In the session below, the proof is performed in the \HOL\ system,
using the \ML\ functions \ml{ASSUME}\index{ASSUME@\ml{ASSUME}} and
\ml{MP}.

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
- show_assums := true;
> val it = () : unit

- val th1 = ASSUME ``t1 ==> t2``;
> val th1 = [t1 ==> t2] |- t1 ==> t2 : thm

- val th2 = ASSUME ``t1:bool``
> val th2 = [t1] |- t1 : thm

- MP th1 th2;
> val it = [t1 ==> t2, t1] |- t2 : thm
\end{verbatim}
\end{session}

\noindent More briefly, one could evaluate the following, and `count'\index{counting inferences, in HOL proofs@counting inferences, in \HOL{} proofs} the
invocations of functions representing primitive inferences.

\begin{session}
\begin{verbatim}
#set_flag(`timing`, true);;
false : bool
Run time: 0.0s

#MP(ASSUME "t1 ==> t2")(ASSUME "t1:bool");;
t1 ==> t2, t1 |- t2
Run time: 0.0s
Intermediate theorems generated: 3
\end{verbatim}
\end{session}

\noindent Each of the three inference steps of the abstract proof
corresponds to the application%
%
\index{inferences, in HOL logic@inferences, in \HOL{} logic!as ML function applications@as \ML\ function applications}%
\index{proof steps, as ML function applications@proof steps, as \ML\ function applications}%
\index{proof!the notion of, in HOL system@the notion
  of, in \HOL\ system}%
%
of an \ML\ function in the performance of the proof in \HOL; and each
of the \ML\ functions corresponds to a primitive inference of the
deductive system.

It is worth emphasising that, in either case, every
primitive inference in the proof chain is made, in the sense
that for each inference, the corresponding \ML\ function is evaluated.  That is,
\HOL\ permits no short-cut around the necessity of performing
complete proofs.  The short-cut provided by derived
\index{inferences, in HOL logic@inferences, in \HOL{} logic!in derived rules}
inference rules (as implemented in \ML) is around the necessity of
\emph{specifying} every step; something that would be impossible for
a proof of any length. It can be seen from this that the derived
rule,%
%
\index{proofs, in HOL logic@proofs, in \HOL{} logic!as ML function applications@as \ML\ function applications}
\index{proofs, in HOL logic@proofs, in \HOL{} logic!as generated by derived rules}
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!importance of}
%
and its representation as an \ML{} function, is essential to the
\HOL{} methodology; theorem proving would be otherwise impossible.

There are, of course, an infinite number of proofs, of the `form'
shown in the example, that can be conducted in \HOL: one for every
pair of \ml{``:bool``}-typed terms. %''
Moreover, every time a theorem of the form

$$t_1 \ \imp \ t_2, \ t_1 \ \vdash \ t_2$$

\noindent is required, its proof must be constructed anew. To capture the
general pattern of inference, an \ML\ function can be written to
implement an inference rule as a derivation from the primitive inferences.
Abstractly, a \emph{derived inference rule}
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!justification of}
%
is a rule that can be justified on the basis of the primitive
inference rules (and/or the axioms).  In the present case, the rule
required `undischarges' assumptions.  It is specified for \HOL{} by

\bigskip

\begin{center}
\begin{tabular}{c}
$\Gamma${\small\verb% |- %}$t_1${\small\verb% ==> %}$t_2$\\ \hline
$\Gamma\cup\{t_1\}${\small\verb% |- %}$t_2$
\end{tabular}
\end{center}

\bigskip

\noindent This general rule is valid because from a \HOL\ theorem of the
form $\Gamma${\small\verb% |- %}$t_1${\small\verb%==>%}$t_2$, the theorem
$\Gamma\cup\{t_1\}${\small\verb% |- %}$t_2$ can be derived
as for the specific instance above.
The rule can be implemented in \ML\ as a function (\ml{UNDISCH},
say)\index{UNDISCH@\ml{UNDISCH}} that calls the
appropriate sequence of primitive
inferences. The \ML\ definition of \ml{UNDISCH} is simply

\begin{session}
\begin{verbatim}
- val UNDISCH th = MP th (ASSUME(fst(dest_imp(concl th))));;
> val UNDISCH = fn : thm -> thm
\end{verbatim}
\end{session}

\noindent This provides a function that maps a theorem to a theorem;
that is, performs proofs in \HOL.
The following session illustrates the use of the derived rule, on
a consequence of the axiom \ml{IMP\_ANTISYM\_AX}. (The inferences are
counted.%
\index{counting inferences, in HOL proofs@counting inferences, in \HOL{} proofs}%
%
) Assume that the printing of theorems has been adjusted as above and
\ml{th} is bound as shown below:

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
#th;;
|- (t1 ==> t2) ==> (t2 ==> t1) ==> (t1 = t2)
Run time: 0.0s

#set_flag(`timing`,true);;
true : bool
Run time: 0.0s

#UNDISCH th;;
t1 ==> t2 |- (t2 ==> t1) ==> (t1 = t2)
Run time: 0.1s
Intermediate theorems generated: 2

#UNDISCH it;;
t1 ==> t2, t2 ==> t1 |- t1 = t2
Run time: 0.0s
Intermediate theorems generated: 2
\end{verbatim}
\end{session}

\noindent Each successful application of {\small\verb%UNDISCH%}
to a theorem invokes an application
of {\small\verb%ASSUME%}, followed by an application of {\small\verb%MP%};
\ml{UNDISCH} constructs the
2-step proof for any given theorem (of appropriate form).
As can be
seen, it relies on the class of \ML\ functions that access \HOL\ syntax:
in particular, \ml{concl} to produce the conclusion
of the theorem, \ml{dest\_imp} to separate the implication, and the
selector \ml{fst} to choose the antecedent.

This particular example is very simple, but a derived inference rule
can perform proofs of arbitrary length.  It can also make use of
previously defined rules.  In this way, the normal inference patterns
can be developed much more quickly and easily; transitivity,
generalization, and so on, support the familiar patterns of inference.

A number of derived inference rules are pre-defined when the \HOL\
system is entered (of which \ml{UNDISCH} is one of the first).  In
Section~\ref{avra_standard}, the abstract derivations are given for
the pre-defined rules that reflect the more usual inference patterns
of the predicate (and lambda) calculi.  Like those shown, some of the
pre-defined derived rules in \HOL\ generate relatively short proofs.
Others invoke thousands of primitive inferences, and clearly save a
great deal of effort. Furthermore, rules can be defined by the user to
make still larger steps, or to implement more specialized patterns.

All of the pre-defined derived rules in \HOL\ are described
in \REFERENCE.

\section{Rewriting}
\label{avra_rewrite}

\index{rewriting!rules for|(}
\index{REWRITE_RULE@\ml{REWRITE\_RULE}|(}
Included in the set of derived inferences that are pre-defined in
\HOL\ is a group of rules with complex definitions that do a limited
amount of `automatic' theorem-proving in the form of rewriting.  The
ideas and implementation were originally developed by
Milner\index{Milner, R.}  and Wadsworth\index{Wadsworth, C.} for
Edinburgh \LCF,\index{LCF@\LCF!Edinburgh} and were later implemented
more flexibly and efficiently by Paulson\index{Paulson, L.} and
Huet\index{Huet, G.} for Cambridge \LCF.\index{LCF@\LCF!Cambridge}
They appear in \HOL{} in the Cambridge form. The basic rewriting rule
is \ml{REWRITE\_RULE}.  All of the rewriting rules are described in
detail in \REFERENCE.

\ml{REWRITE\_RULE} uses a list of equational theorems
\index{equational theorems, in HOL logic@equational theorems, in \HOL{} logic!use of in rewriting}
\index{theorems, in HOL logic@theorems, in \HOL{} logic!equational}
(theorems whose conclusions can be regarded as having the form
$t_1${\small\verb% = %}$t_2$) to replace
any subterms of an object theorem that `match' $t_1$ by the
corresponding instance of $t_2$. The rule matches recursively and to any depth,
until no more replacements can be made,
using internally defined search, matching and
instantiation\index{type instantiation, in HOL logic@type instantiation, in \HOL{} logic!in rewriting rule} algorithms.  The validity
 of \ml{REWRITE\_RULE} rests
ultimately on the primitive rules \ml{SUBST} (for making the substitutions);
\ml{INST\_TYPE}\index{INST_TYPE@\ml{INST\_TYPE}} (for instantiating types); and the derived rules for
generalization and specialization (see Sections~\ref{avra_gen}
and \ref{avra_spec}) for instantiating terms.  The definition
of \ml{REWRITE\_RULE} in \ML\
also relies on a large number of general and \HOL-oriented
\ML\ functions.
%The implementation is partly described in Chapter~\ref{avra_conv}.

In practice, the derived rule \ml{REWRITE\_RULE} plays a central role
in proofs, because it takes over a very large number of inferences
which may happen in a complex and unpredictable order. It is unlike
any other primitive or pre-defined rule, first because of the number
of inferences it generates\footnote{The number of inferences performed
  by this rule is generally `inflated'; \ie\ is generally greater than
  the length of the proof itself, if the proof could be `seen'.  This
  is because, in the current implementation, some inference is done
  during the search phase that is not necessarily in support of
  successful replacements.}; and second because its outcome is often
unexpected. Its power is increased by the fact that any existing
equational theorem can be supplied as a `rewrite rule', including a
standard \HOL\ set of pre-proved tautologies; and these rewrite rules
can interact with each other in the rewriting process to transform the
original theorem.

The application of \ml{REWRITE\_RULE}, in the session below,
illustrates that replacements are made at all levels of the
structure of a term.
The example is numerical;
the infixes {\small\verb%"$>"%} and {\small\verb%"$<"%} are
the usual `greater than'  and `less than' relations, respectively,
and \ml{"SUC"}, the
usual successor function.
Use is made of the pre-existing definition of {\small\verb%"$>"%}:
\ml{GREATER} (see \REFERENCE).
The timing\index{counting inferences, in HOL proofs@counting inferences, in \HOL{} proofs} facility is used again, for interest, and the printing
of theorems is adjusted as above.

\setcounter{sessioncount}{1}
\begin{session}
\begin{verbatim}
#top_print print_all_thm;;
- : (thm -> void)

#set_flag(`timing`,true);;
false : bool
Run time: 0.0s

#REWRITE_RULE
 [GREATER]
 (ASSUME "SUC 4 > 0 = (SUC 3 > 0 = (SUC 2 > 0 = (SUC 1 > 0 = SUC 0 > 0)))");;
##Definition GREATER autoloaded from theory `arithmetic`.
GREATER = |- !m n. m > n = n < m
Run time: 1.5s
Intermediate theorems generated: 1

(SUC 4) > 0 =
((SUC 3) > 0 = ((SUC 2) > 0 = ((SUC 1) > 0 = (SUC 0) > 0)))
|- 0 < (SUC 4) =
   (0 < (SUC 3) = (0 < (SUC 2) = (0 < (SUC 1) = 0 < (SUC 0))))
Run time: 0.3s
Intermediate theorems generated: 23
\end{verbatim}
\end{session}

\noindent Notice that rewriting
equations can be extracted from
universally quantified theorems.
To construct the
proof step-wise, with all of the instantiations,
substitutions, and uses of transitivity, \etc,
would be a lengthy process. The rewriting rules make it easy,
and do so whilst still generating the entire chain of inferences.
\index{REWRITE_RULE@\ml{REWRITE\_RULE}|)}
\index{rewriting!rules for|)}

\section{Derivation of the Standard Rules}
\label{avra_standard}

\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!pre-defined|(}
%
The \HOL{} system provides all the standard introduction and
elimination rules of the predicate calculus pre-defined as derived
inferences.  It is these derived rules, rather than the primitive
rules, that one normally uses in practice.  In this section, the
derivations of some of the standard rules are given, in sequence.
These derivations only use the axioms and definitions in the theory
\theoryimp{bool} (see Section~\ref{boolfull}), the eight primitive
inferences of the \HOL\ logic, and inferences defined earlier in the
sequence.

Theorems,%
%
\index{theorems, in HOL logic@theorems, in \HOL{} logic!as inference rules}%
%
in accordance with the definition given at the beginning of this
chapter, are treated as rules without hypotheses; thus the derivation
of a theorem resembles the derivation of a rule except in not having
hypotheses. (The derivation of \ml{TRUTH}, Section~\ref{avra_T}, is
the only example given of this, but there are several others in \HOL.)
There are also some rules that are intrinsically more general than
theorems.  For example, for any two terms $t_1$ and $t_2$, the theorem
$\vdash(\lquant{x}t_1)t_2 = t_1[t_2/x]$ follows by the primitive rule
\rul{BETA\_CONV}. The rule \ml{BETA\_CONV} returns a theorem for each
pair of terms $t_1$ and $t_2$, and is therefore equivalent to an
infinite family%
%
\index{families of inferences, in HOL logic@families of inferences, in \HOL{} logic}%
%
of theorems. No single theorem can be expressed in the \HOL{} logic
that is equivalent to \rul{BETA\_CONV}.%
\index{theorems, in HOL logic@theorems, in \HOL{} logic!rules inexpressible as}
\index{beta-conversion, in HOL logic@beta-conversion, in \HOL{} logic!not expressible as a theorem}%
%
%(See Chapter~\ref{avra_conv} for further discussion of this point.)
(\ml{UNDISCH} is not a rule of this sort, as it can, in fact, be
expressed as a theorem.)

For each derivation given below, there is an \ML\ function definition
in the \HOL\ system that implements the derived rule as a procedure in
\ML. The actual implementation in the \HOL\ system differs in some
cases from the derivations given here, since the system code has been
optimised for improved performance.

In addition, for reasons that are mostly historical, not all the
inferences that are derived in terms of the abstract logic are
actually derived in the current version of the \HOL\ system.  That is,
there are currently about forty rules that are installed in the system
on an `axiomatic' basis, all of which should be derived by explicit
inference.  Although the current status of these rules is not
satisfactory, and it is planned, as a high priority, to derive them
properly in a future version, their current status does not actually
compromise the consistency of the logic.  In effect, the existing
\HOL\ system has a deductive system more comprehensive than the one
presented abstractly, but the model outlined in \LOGIC{} would easily
extend to cover it.%
%
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!pre-defined|)}

For reference, in \HOL\ Version 2.0 the following rules that should be
derived
%
\index{inference rules, of HOL logic@inference rules, of \HOL{} logic!some not properly derived|(}
\index{rules in HOL logic, some not properly derived@rules in \HOL{} logic, some not properly derived|(}
%
are not derived, but (for efficiency) are implemented as
primitives. The list includes some conversions and conversion-valued
functions. % (conversions are discussed in Chapter~\ref{avra_conv}).
\vfill \newpage

\begin{hol}
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!list of axiomatic}
\index{AP_TERM@\ml{AP\_TERM}}
\index{AP_THM@\ml{AP\_THM}}
\index{CCONTR@\ml{CCONTR}}
\index{CHOOSE@\ml{CHOOSE}}
\index{CONJ@\ml{CONJ}}
\index{CONJUNCT1@\ml{CONJUNCT1}}
\index{CONJUNCT2@\ml{CONJUNCT2}}
\index{DISJ_CASES@\ml{DISJ\_CASES}}
\index{DISJ1@\ml{DISJ1}}
\index{DISJ2@\ml{DISJ2}}
\index{EQ_IMP_RULE@\ml{EQ\_IMP\_RULE}}
\index{EQ_MP@\ml{EQ\_MP}}
\index{EQT_INTRO@\ml{EQT\_INTRO}}
\index{ETA_CONV@\ml{ETA\_CONV}}
\index{EXISTS@\ml{EXISTS}}
\index{EXT@\ml{EXT}}
\index{GEN@\ml{GEN}}
\index{MK_ABS@\ml{MK\_ABS}}
\index{MK_COMB@\ml{MK\_COMB}}
\index{num_CONV@\ml{num\_CONV}}
\index{SPEC@\ml{SPEC}}
\index{SUBS@\ml{SUBS}}
\index{SUBS_OCCS@\ml{SUBS\_OCCS}}
\index{SUBST_CONV@\ml{SUBST\_CONV}}
\index{SYM@\ml{SYM}}
\index{TRANS@\ml{TRANS}}
\begin{verbatim}
   ADD_ASSUM              CONTR                  IMP_ANTISYM_RULE
   ALPHA                  DEF_EXISTS_RULE        IMP_TRANS
   AP_TERM                DISJ_CASES             INST
   AP_THM                 DISJ1                  MK_ABS
   SUBS                   DISJ2                  MK_COMB
   SUBS_OCCS              EQ_IMP_RULE            MK_EXISTS
   CCONTR                 EQ_MP                  NOT_ELIM
   CHOOSE                 EQT_INTRO              NOT_INTRO
   CONJ                   ETA_CONV               num_CONV
   EXISTS                 SPEC                   TRANS
   EXT                    SUBST_CONV             CONJUNCT1
   GEN                    SYM                    CONJUNCT2
\end{verbatim}\end{hol}
\index{inference rules, of HOL logic@inference rules, of \HOL{} logic!some not properly derived|)}
\index{rules in HOL logic, some not properly derived@rules in \HOL{} logic, some not properly derived|)}

\index{inference rules, of HOL logic@inference rules, of \HOL{} logic!derived|(}
The derivations that follow consist of sequences of numbered steps each of
which
\begin{enumerate}
\item is an axiom, or
\item is a hypothesis of the rule being derived, or
\item follows from preceding steps by a rule of inference (either primitive
or previously derived).
\end{enumerate}

\noindent Note that the abbreviation \ml{conv} (standing for
`conversion') is used for the \ML\ type \ml{term ->
  thm}.%
% \footnote{This stands for `conversion', as explained in
% Chapter~\ref{avra_conv}.}

\subsection{Adding an assumption}
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!list and derivations of some|(}

\begin{holboxed}
\index{ADD_ASSUM@\ml{ADD\_ASSUM}|pin}
\begin{verbatim}
   ADD_ASSUM : term -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t\over \Gamma,\ t'\turn t$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $t'\turn t'$ \hfill [\rul{ASSUME}]
\item $\Gamma\turn t$ \hfill [Hypothesis]
\item $\Gamma\turn t'\imp t$ \hfill [\rul{DISCH} 2]
\item $\Gamma,\ t'\turn t$ \hfill [\rul{MP} 3,1]
\end{proof}

%\subsection{Undischarging [\rul{UNDISCH}]}
\subsection{Undischarging}

\begin{holboxed}
\index{implication, in HOL logic@implication, in \HOL{} logic!inference rules for}
\index{UNDISCH@\ml{UNDISCH}|pin}
\begin{verbatim}
   UNDISCH : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1\imp t_2 \over\Gamma,\ t_1\turn t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $t_1\turn t_1$ \hfill [\rul{ASSUME}]
\item $\Gamma\turn t_1\imp t_2$ \hfill [Hypothesis]
\item $\Gamma,\ t_1\turn t_2$ \hfill [\rul{MP} 2,1]
\end{proof}

\subsection{Symmetry of equality}

\begin{holboxed}
\index{SYM@\ml{SYM}|pin}
\index{symmetry of equality rule, in HOL logic@symmetry of equality rule, in \HOL{} logic}
\index{equality, in HOL logic@equality, in \HOL{} logic!symmetry rule for}
\begin{verbatim}
   SYM : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1 = t_2\over \Gamma\turn t_2 = t_1$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t_1=t_2$\hfill [Hypothesis]
\item $\turn t_1=t_1$ \hfill [\rul{REFL}]
\item $\Gamma\turn t_2=t_1$\hfill [\rul{SUBST} 1,2]
\end{proof}

\subsection{Transitivity of equality}

\begin{holboxed}
\index{transitivity of equality rule, in HOL logic@transitivity of equality rule, in \HOL{} logic}
\index{equality, in HOL logic@equality, in \HOL{} logic!transitivity rule for}
\index{TRANS@\ml{TRANS}|pin}
\begin{verbatim}
   TRANS : thm -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma_1\turn t_1=t_2\qquad\qquad\qquad \Gamma_2\turn t_2=t_3 \over
\Gamma_1\cup\Gamma_2\turn t_1=t_3$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma_2\turn t_2=t_3$\hfill [Hypothesis]
\item $\Gamma_1\turn t_1=t_2$\hfill [Hypothesis]
\item $\Gamma_1\cup\Gamma_2\turn t_1=t_3$\hfill [\rul{SUBST} 1,2]
\end{proof}

\subsection{Application of a term to a theorem}%
\index{function application, in HOL logic@function application, in \HOL{} logic!inference rules for}

\begin{holboxed}
\index{AP_TERM@\ml{AP\_TERM}|pin}
\begin{verbatim}
   AP_TERM : term -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1=t_2\over\Gamma\turn t\ t_1 = t\ t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t_1=t_2$\hfill [Hypothesis]
\item $\turn t\ t_1 = t\ t_1$ \hfill [\rul{REFL}]
\item $\Gamma\turn t\ t_1 = t\ t_2$ \hfill [\rul{SUBST} 1,2]
\end{proof}

\subsection{Application of a theorem to a term}

\begin{holboxed}
\index{AP_THM@\ml{AP\_THM}|pin}
\begin{verbatim}
   AP_THM : thm -> conv
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1=t_2\over \Gamma\turn t_1\ t = t_2\ t$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t_1=t_2$\hfill [Hypothesis]
\item$\turn t_1\ t = t_1\ t$\hfill [\rul{REFL}]
\item $\Gamma\turn t_1\ t = t_2\ t$\hfill [\rul{SUBST} 1,2]
\end{proof}

\subsection{Modus Ponens for equality}
\label{avra_eq_mp}

\begin{holboxed}
\index{EQ_MP@\ml{EQ\_MP}|pin}
\index{equality, in HOL logic@equality, in \HOL{} logic!MP rule for@\ml{MP} rule for}
\begin{verbatim}
   EQ_MP : thm -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma_1\turn t_1=t_2\qquad\qquad\qquad \Gamma_2\turn t_1\over
\Gamma_1\cup\Gamma_2\turn t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma_1\turn t_1=t_2$ \hfill [Hypothesis]
\item $\Gamma_2\turn t_1$ \hfill [Hypothesis]
\item $\Gamma_1\cup\Gamma_2\turn t_2$ \hfill [\rul{SUBST} 1,2]
\end{proof}

\subsection{Implication from equality}
\index{equality, in HOL logic@equality, in \HOL{} logic!other rules for|(}
\index{implication, in HOL logic@implication, in \HOL{} logic!inference rules for}
\begin{holboxed}
\index{EQ_IMP_RULE@\ml{EQ\_IMP\_RULE}|pin}
\begin{verbatim}
   EQ_IMP_RULE : thm -> (thm # thm)
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1=t_2\over
\Gamma\turn t_1\imp t_2 \qquad\qquad\qquad \Gamma\turn t_2\imp t_1$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t_1=t_2$ \hfill [Hypothesis]
\item $t_1\turn t_1$ \hfill [\rul{ASSUME}]
\item $\Gamma,\ t_1\turn t_2$ \hfill [\rul{EQ\_MP} 1,2]
\item $\Gamma\turn t_1\imp t_2$ \hfill [\rul{DISCH} 3]
\item $\Gamma\turn t_2=t_1$ \hfill [\rul{SYM} 1]
\item $t_2\turn t_2$ \hfill [\rul{ASSUME}]
\item $\Gamma,\ t_2\turn t_1$ \hfill [\rul{EQ\_MP} 5,6]
\item $\Gamma\turn t_2\imp t_1$ \hfill [\rul{DISCH} 7]
\item $\Gamma\turn t_1\imp t_2$ and $\Gamma\turn t_2\imp t_1$\hfill [4,8]
\end{proof}

\subsection{\T-Introduction}
\label{avra_T}

\index{T@\holtxt{T}!rules of inference for|(}
\begin{hol}
\begin{verbatim}
   TRUTH
\end{verbatim}
\end{hol}

\vspace{12pt plus2pt minus1pt}

$$\turn\T$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \T = ((\lquant{x}x)=(\lquant{x}x))$\hfill [Definition of \T]
\item $\turn ((\lquant{x}x)=(\lquant{x}x)) = \T$\hfill [\rul{SYM} 1]
\item $\turn (\lquant{x}x)=(\lquant{x}x)$\hfill [\rul{REFL}]
\item $\turn\T$ \hfill [\rul{EQ\_MP} 2,3]
\end{proof}

\subsection{Equality-with-\T\ elimination}

\begin{holboxed}
\index{EQT_ELIM@\ml{EQT\_ELIM}|pin}
\begin{verbatim}
   EQT_ELIM : thm -> thm
\end{verbatim}
\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t = \T\over \Gamma\turn t$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t = \T$\hfill [Hypothesis]
\item $\Gamma\turn \T = t$\hfill [\rul{SYM} 1]
\item $\turn \T$\hfill [\rul{TRUTH}]
\item $\Gamma\turn t$\hfill [\rul{EQ\_MP} 2,3]
\end{proof}

\subsection{\texorpdfstring{Specialization ($\forall$-elimination)}{Specialization (forall-elimination)}}

\begin{holboxed}
\index{SPEC@\ml{SPEC}|pin}
\index{specialization rule, in HOL logic@specialization rule, in \HOL{} logic}
\begin{verbatim}
   SPEC : term -> thm -> thm
\end{verbatim}
\end{holboxed}

\label{avra_spec}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn \uquant{x}t\over \Gamma\turn t[t'/x]$$
\begin{itemize}
\item $t[t'/x]$ denotes the result of substituting $t'$ for free\index{free variables, in HOL logic@free variables, in \HOL{} logic}
occurrences of $x$ in $t$, with the restriction that no free variables in $t'$
become bound after substitution.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \forall = (\lquant{P}P = (\lquant{x}\T))$ \hfill
[\rul{INST\_TYPE} applied to the definition of $\forall$]
\item $\Gamma\turn \forall(\lquant{x}t)$\hfill [Hypothesis]
\item $\Gamma\turn (\lquant{P}P=(\lquant{x}\T))(\lquant{x}t)$\hfill
[\rul{SUBST} 1,2]
\item $\turn  (\lquant{P}P=(\lquant{x}\T))(\lquant{x}t) =
((\lquant{x}t)=(\lquant{x}\T))$\hfill [\rul{BETA\_CONV}]
\item $\Gamma\turn (\lquant{x}t)=(\lquant{x}\T)$\hfill [\rul{EQ\_MP} 4,3]
\item $\Gamma\turn (\lquant{x}t)\ t' = (\lquant{x}\T)\ t'$ \hfill
[\rul{AP\_THM} 5]
\item $\turn (\lquant{x}t)\ t' = t[t'/x]$ \hfill [\rul{BETA\_CONV}]
\item $\Gamma\turn t[t'/x] = (\lquant{x}t)\ t'$ \hfill [\rul{SYM} 7]
\item $\Gamma\turn t[t'/x] = (\lquant{x}\T)\ t'$ \hfill [\rul{TRANS} 8,6]
\item $\turn (\lquant{x}\T)\ t' = \T$ \hfill [\rul{BETA\_CONV}]
\item $\Gamma\turn t[t'/x] = \T$ \hfill [\rul{TRANS} 9,10]
\item $\Gamma\turn t[t'/x]$ \hfill [\rul{EQT\_ELIM} 11]
\end{proof}

\subsection{Equality-with-\T\ introduction}

\begin{holboxed}
\index{EQT_INTRO@\ml{EQT\_INTRO}|pin}
\begin{verbatim}
   EQT_INTRO : thm -> thm
\end{verbatim}
\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t\over\Gamma\turn t=\T$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn\uquant{b_1\ b_2}(b_1\imp b_2)\imp(b_2\imp b_1)\imp(b_1=b_2)$
\hfill [Axiom]
\item $\turn\uquant{b_2}(t\imp b_2)\imp(b_2\imp t)\imp(t=b_2)$
\hfill [\rul{SPEC} 1]
\item $\turn(t\imp\T)\imp(\T\imp t)\imp(t=\T)$\hfill [\rul{SPEC} 2]
\item $\turn\T$\hfill [\rul{TRUTH}]
\item $\turn t\imp\T$\hfill [\rul{DISCH} 4]
\item $\turn(\T\imp t)\imp(t=\T)$\hfill [\rul{MP} 3,5]
\item $\Gamma \turn t$\hfill [Hypothesis]
\item $\Gamma\turn\T\imp t$\hfill [\rul{DISCH} 7]
\item $\Gamma\turn t=\T$\hfill [\rul{MP} 6,8]
\end{proof}
\index{equality, in HOL logic@equality, in \HOL{} logic!other rules for|)}
\index{T@\holtxt{T}!rules of inference for|)}

\subsection{\texorpdfstring{Generalization ($\forall$-introduction)}{Generalization (forall-introduction)}}%
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL{} logic!inference rules for}

\begin{holboxed}
\index{GEN@\ml{GEN}|pin}
\index{generalization rule, in HOL logic@generalization rule, in \HOL{} logic}
\begin{verbatim}
   GEN : term -> thm -> thm
\end{verbatim}
\end{holboxed}

\label{avra_gen}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t\over\Gamma\turn\uquant{x} t$$
\begin{itemize}
\item Where $x$ is not free in $\Gamma$.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t$\hfill [Hypothesis]
\item $\Gamma\turn t = \T$\hfill [\rul{EQT\_INTRO} 1]
\item $\Gamma\turn(\lquant{x}t)=(\lquant{x}\T)$\hfill [\rul{ABS} 2]
\item $\turn \forall(\lquant{x}t) = \forall(\lquant{x}t)$\hfill [\rul{REFL}]
\item $\turn \forall = (\lquant{P} P =(\lquant{x}\T))$\hfill
[\rul{INST\_TYPE} applied to the definition of $\forall$]
\item $\turn\forall(\lquant{x}t)=(\lquant{P} P=(\lquant{x}\T))(\lquant{x}t)$
\hfill [\rul{SUBST} 5,4]
\item $\turn(\lquant{P} P=(\lquant{x}\T))(\lquant{x}t)=((\lquant{x}t)
=(\lquant{x}\T))$\hfill [\rul{BETA\_CONV}]
\item $\turn\forall(\lquant{x}t) = ((\lquant{x}t)=(\lquant{x}\T))$
\hfill [\rul{TRANS} 6,7]
\item $\turn((\lquant{x}t)=(\lquant{x}\T)) = \forall(\lquant{x}\T)$
\hfill [\rul{SYM} 8]
\item $\Gamma\turn\forall(\lquant{x}t)$\hfill [\rul{EQ\_MP} 9,3]
\end{proof}

\subsection{\texorpdfstring{Simple $\alpha$-conversion}{Simple alpha-conversion}}

\begin{holboxed}
\begin{verbatim}
   SIMPLE_ALPHA
\end{verbatim}
\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\turn(\lquant{x_1}t\ x_1) = (\lquant{x_2}t\ x_2)$$
\begin{itemize}
\item Where neither $x_1$ nor $x_2$ occurs free in $t$.\footnote{\ml{SIMPLE\_ALPHA} is
included here because it is
used in a subsequent derivation, but it is not actually in the
\HOL\ system, as it is subsumed by other functions.}
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item$\turn(\lquant{x_1}t\ x_1)\ x = t\ x$\hfill [\rul{BETA\_CONV}]
\item$\turn(\lquant{x_2}t\ x_2)\ x = t\ x$\hfill [\rul{BETA\_CONV}]
\item $\turn t\ x = (\lquant{x_2}t\ x_2)\ x$\hfill [\rul{SYM} 2]
\item $\turn (\lquant{x_1}t\ x_1)\ x = (\lquant{x_2}t\ x_2)\ x$
\hfill [\rul{TRANS} 1,3]
\item $\turn(\lquant{x}(\lquant{x_1}t\ x_1)\ x) =
(\lquant{x}(\lquant{x_2}t\ x_2)\ x)$\hfill [\rul{ABS} 4]
\item $\turn\uquant{f}(\lquant{x}f\ x) = f$\hfill
[Appropriately type-instantiated axiom]
\item $\turn(\lquant{x}(\lquant{x_1}t\ x_1)x) = \lquant{x_1}t\ x_1$
\hfill [\rul{SPEC} 6]
\item $\turn(\lquant{x}(\lquant{x_2}t\ x_2)x) = \lquant{x_2}t\ x_2$
\hfill [\rul{SPEC} 6]
\item $\turn (\lquant{x_1}t\ x_1) = (\lquant{x}(\lquant{x_1}t\ x_1)x)$
\hfill [\rul{SYM} 7]
\item $\turn (\lquant{x_1}t\ x_1) = (\lquant{x}(\lquant{x_2}t\ x_2)x)$
\hfill [\rul{TRANS} 9,5]
\item $\turn(\lquant{x_1}t\ x_1)=(\lquant{x_2}t\ x_2)$\hfill
[\rul{TRANS} 10,8]
\end{proof}

\subsection{\texorpdfstring{$\eta$-conversion}{Eta-conversion}}

\begin{holboxed}
\index{ETA_CONV@\ml{ETA\_CONV}|pin}
\begin{verbatim}
   ETA_CONV : conv
\end{verbatim}
\end{holboxed}
\vspace{12pt plus2pt minus1pt}

$$\turn(\lquant{x'}t\ x') = t$$
\begin{itemize}
\item Where $x'$ does not occur free\index{free variables, in HOL logic@free variables, in \HOL{} logic} in $t$ (we use $x'$ rather than just $x$
to motivate the use of \rul{SIMPLE\_ALPHA} in the derivation below).
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn\uquant{f}(\lquant{x}f\ x) = f$\hfill
[Appropriately type-instantiated axiom]
\item  $\turn(\lquant{x}t\ x) = t$\hfill [\rul{SPEC} 1]
\item $\turn(\lquant{x'}t\ x')=(\lquant{x}t\ x)$\hfill [\rul{SIMPLE\_ALPHA}]
\item $\turn(\lquant{x'}t\ x')=t$\hfill [\rul{TRANS} 3,2]
\end{proof}

\subsection{Extensionality}
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL{} logic!inference rules for}

\begin{holboxed}
\index{EXT@\ml{EXT}|pin}
\index{extensionality rule, in HOL logic@extensionality rule, in \HOL{} logic}
\begin{verbatim}
   EXT : thm -> thm
\end{verbatim}
\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn\uquant{x} t_1\ x = t_2\ x\over\Gamma\turn t_1=t_2$$
\begin{itemize}
\item Where $x$ is not free\index{free variables, in HOL logic@free variables, in \HOL{} logic} in $t_1$ or $t_2$.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn\uquant{x}t_1\ x=t_2\ x$\hfill [Hypothesis]
\item $\Gamma\turn t_1\ x'=t_2\ x'$\hfill [\rul{SPEC} 1 ($x'$ is a fresh)]
\item $\Gamma\turn(\lquant{x'}t_1\ x') = (\lquant{x'}t_2\ x')$\hfill
        [\rul{ABS} 2]
\item $\turn(\lquant{x'}t_1\ x') = t_1$\hfill [\rul{ETA\_CONV}]
\item $\turn t_1 = (\lquant{x'}t_1\ x')$\hfill [\rul{SYM} 4]
\item $\Gamma\turn t_1 = (\lquant{x'}t_2\ x')$\hfill [\rul{TRANS} 5,3]
\item $\turn(\lquant{x'}t_2\ x') = t_2$\hfill [\rul{ETA\_CONV}]
\item $\Gamma\turn t_1=t_2$\hfill [\rul{TRANS} 6,7]
\end{proof}

\subsection{\texorpdfstring{$\hilbert$-introduction}{Hilbert-introduction}}

\begin{holboxed}
\index{choice operator, in HOL logic@choice operator, in \HOL{} logic!inference rules for}
\index{SELECT_INTRO@\ml{SELECT\_INTRO}|pin}
\begin{verbatim}
   SELECT_INTRO : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1\ t_2\over\Gamma\turn t_1(\hilbert\ t_1)$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn\uquant{P\ x}P\ x\imp P(\hilbert\ P)$\hfill [Suitably
type-instantiated axiom]
\item $\turn t_1\ t_2 \imp t_1(\hilbert\ t_1)$\hfill [\rul{SPEC} 1 (twice)]
\item $\Gamma\turn t_1\ t_2$\hfill [Hypothesis]
\item $\Gamma\turn t_1(\hilbert\ t_1)$\hfill [\rul{MP} 2,3]
\end{proof}

\subsection{\texorpdfstring{$\hilbert$-elimination}{Hilbert-elimination}}

\begin{holboxed}
\index{choice operator, in HOL logic@choice operator, in \HOL{} logic!inference rules for}
\index{SELECT_ELIM@\ml{SELECT\_ELIM}|pin}
\begin{verbatim}
   SELECT_ELIM : thm -> (term # thm) -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma_1\turn t_1(\hilbert\ t_1)\qquad\qquad\qquad\Gamma_2,\ t_1\ v\turn t
\over \Gamma_1\cup\Gamma_2\turn t$$
\begin{itemize}
\item Where $v$ occurs nowhere except in the assumption $t_1\ v$ of the second
hypothesis.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma_2,\ t_1\ v\turn t$ \hfill [Hypothesis]
\item $\Gamma_2\turn t_1\ v\imp t$\hfill [\rul{DISCH} 1]
\item $\Gamma_2\turn\uquant{v}t_1\ v\imp t$\hfill [\rul{GEN} 2]
\item $\Gamma_2\turn t_1(\hilbert\ t_1)\imp t$\hfill [\rul{SPEC} 3]
\item $\Gamma_1\turn t_1(\hilbert\ t_1)$\hfill [Hypothesis]
\item $\Gamma_1\cup\Gamma_2\turn t$\hfill [\rul{MP} 4,5]
\end{proof}

\subsection{\texorpdfstring{$\exists$-introduction}{Exists-introduction}}
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL{} logic!inference rules for|(}

\begin{holboxed}
\index{EXISTS@\ml{EXISTS}|pin}
\begin{verbatim}
   EXISTS : (term # term) -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1[t_2]\over \Gamma\turn \equant{x}t_1[x]$$
\begin{itemize}
\item Where $t_1[t_2]$ denotes a term $t_1$ with some free\index{free variables, in HOL logic@free variables, in \HOL{} logic}
occurrences of $t_2$
singled out, and $t_1[x]$ denotes the result of replacing these
occurrences of $t_1$ by $x$, subject to the restriction that $x$
doesn't become bound after substitution.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn(\lquant{x}t_1[x])t_2= t_1[t_2]$\hfill [\rul{BETA\_CONV}]
\item $\turn t_1[t_2] = (\lquant{x}t_1[x])t_2$\hfill [\rul{SYM} 1]
\item $\Gamma\turn t_1[t_2]$\hfill [Hypothesis]
\item $\Gamma\turn(\lquant{x}t_1[x])t_2$\hfill [\rul{EQ\_MP} 2,3]
\item $\Gamma\turn(\lquant{x}t_1[x])(\hilbert(\lquant{x}t_1[x]))$\hfill
[\rul{SELECT\_INTRO} 4]
\item $\turn \exists = \lquant{P} P(\hilbert\ P)$\hfill
[\rul{INST\_TYPE} applied to the definition of $\exists$]
\item $\turn\exists(\lquant{x}t_1[x]) =
(\lquant{P}P(\hilbert\ P))(\lquant{x}t_1[x])$\hfill [\rul{AP\_THM} 6]
\item $\turn(\lquant{P}P(\hilbert\ P))(\lquant{x}t_1[x]) =
(\lquant{x}t_1[x])(\hilbert(\lquant{x}t_1[x]))$\hfill [\rul{BETA\_CONV}]
\item $\turn\exists(\lquant{x}t_1[x]) =
(\lquant{x}t_1[x])(\hilbert(\lquant{x}t_1[x]))$\hfill [\rul{TRANS} 7,8]
\item $\turn(\lquant{x}t_1[x])(\hilbert(\lquant{x}t_1[x])) =
\exists(\lquant{x}t_1[x])$\hfill [\rul{SYM} 9]
\item $\Gamma\turn\exists(\lquant{x}t_1[x])$\hfill [\rul{EQ\_MP} 10,5]
\end{proof}

\subsection{\texorpdfstring{$\exists$-elimination}{Exists-elimination}}

\begin{holboxed}
\index{CHOOSE@\ml{CHOOSE}|pin}
\begin{verbatim}
   CHOOSE : (term # thm) -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma_1\turn\equant{x}t[x]\qquad\qquad\qquad \Gamma_2,\ t[v]\turn t'
\over \Gamma_1\cup\Gamma_2\turn t'$$
\begin{itemize}
\item Where $t[v]$ denotes a term $t$ with some free\index{free variables, in HOL logic@free variables, in \HOL{} logic}
occurrences of the variable $v$
singled out, and $t[x]$ denotes the result of replacing these
occurrences of $v$ by $x$, subject to the restriction that $x$ doesn't become
bound after substitution.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \exists = \lquant{P} P(\hilbert\ P)$\hfill
[\rul{INST\_TYPE} applied to the definition of $\exists$]
\item $\turn\exists(\lquant{x}t[x]) =
(\lquant{P}P(\hilbert\ P))(\lquant{x}t[x])$\hfill [\rul{AP\_THM} 1]
\item $\Gamma_1\turn\exists(\lquant{x}t[x])$\hfill [Hypothesis]
\item $\Gamma_1\turn (\lquant{P}P(\hilbert\ P))(\lquant{x}t[x])$
\hfill [\rul{EQ\_MP} 2,3]
\item $\turn(\lquant{P}P(\hilbert\ P))(\lquant{x}t[x]) =
(\lquant{x}t[x])(\hilbert(\lquant{x}t[x]))$\hfill [\rul{BETA\_CONV}]
\item $\Gamma_1\turn(\lquant{x}t[x])(\hilbert(\lquant{x}t[x])$\hfill
[\rul{EQ\_MP} 5,4]
\item $\turn(\lquant{x}t[x])v = t[v]$\hfill [\rul{BETA\_CONV}]
\item $\turn t[v] =(\lquant{x}t[x])v$\hfill [\rul{SYM} 7]
\item $\Gamma_2,\ t[v]\turn t'$\hfill [Hypothesis]
\item $\Gamma_2\turn t[v]\imp t'$\hfill [\rul{DISCH} 9]
\item $\Gamma_2\turn(\lquant{x}t[x])v\imp t'$\hfill [\rul{SUBST} 8,10]
\item $\Gamma_2,\ (\lquant{x}t[x])v\turn t'$\hfill [\rul{UNDISCH} 11]
\item $\Gamma_1\cup\Gamma_2\turn t'$\hfill [\rul{SELECT\_ELIM} 6,12]
\end{proof}
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL{} logic!inference rules for|)}

\subsection{Use of a definition}

\begin{holboxed}
\index{RIGHT_BETA@\ml{RIGHT\_BETA}|pin}
\begin{verbatim}
   RIGHT_BETA : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t = \lquant{x}t'[x]
\over \Gamma\turn t\ t = t'[t]$$
\begin{itemize}
\item Where  $t$ does not contain $x$.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t = \lquant{x} t'[x]$\hfill
[Suitably type-instantiated hypothesis]
\item $\Gamma\turn t\ t =
(\lquant{x}t'[x])\ t$\hfill
[\rul{AP\_THM} 1 ]
\item $\turn(\lquant{x}t'[x])\ t =
t'[t]$\hfill [\rul{BETA\_CONV}]
\item $\Gamma\turn t\ t = t'[t]$\hfill
[\rul{TRANS} 2,3]
\end{proof}

\subsection{Use of a definition}

\begin{holboxed}
\index{RIGHT_LIST_BETA@\ml{RIGHT\_LIST\_BETA}|pin}
\begin{verbatim}
   RIGHT_LIST_BETA : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t = \lquant{x_1\cdots x_n}t'[x_1,\ldots,x_n]
\over \Gamma\turn t\ t_1\cdots t_n = t'[t_1,\ldots,t_n]$$
\begin{itemize}
\item Where none of the $t_i$ contain any of the $x_i$.
\end{itemize}

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\Gamma\turn t = \lquant{x_1\cdots x_n} t'[x_1,\ldots,x_n]$\hfill
[Suitably type-instantiated hypothesis]
\item $\Gamma\turn t\ t_1\cdots t_n =
(\lquant{x_1\cdots x_n}t'[x_1,\ldots,x_n])\ t_1\cdots t_n$\hfill
[\rul{AP\_THM} 1 (n times)]
\item $\turn(\lquant{x_1\cdots x_n}t'[x_1,\ldots,x_n])\ t_1\cdots t_n =
t'[t_1,\ldots,t_n]$\hfill [\rul{BETA\_CONV} (n times)]
\item $\Gamma\turn t\ t_1\cdots t_n = t'[t_1,\ldots,t_n]$\hfill
[\rul{TRANS} 2,3]
\end{proof}

\subsection{\texorpdfstring{$\wedge$-introduction}{Conjunction-introduction}}
\label{avra_conj}

\begin{holboxed}
\index{CONJ@\ml{CONJ}|pin}
\index{conjunction, in HOL logic@conjunction, in \HOL{} logic!inference rule for}
\begin{verbatim}
   CONJ : thm -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma_1\turn t_1\qquad\qquad\qquad\Gamma_2\turn t_2\over
\Gamma_1\cup\Gamma_2 \turn t_1\conj t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \conj = \lquant{b_1\ b_2}\uquant{b}(b_1\imp(b_2\imp b))\imp b$
\hfill [Definition of $\conj$]
\item $\turn t_1\conj t_2 = \uquant{b}(t_1\imp(t_2\imp b))\imp b$\hfill
[\rul{RIGHT\_LIST\_BETA} 1]
\item $t_1\imp(t_2\imp b)\turn t_1\imp(t_2\imp b)$\hfill [\rul{ASSUME}]
\item $\Gamma_1\turn t_1$\hfill [Hypothesis]
\item $\Gamma_1,\ t_1\imp(t_2\imp b)\turn t_2\imp b$\hfill [\rul{MP} 3,4]
\item $\Gamma_2\turn t_2$\hfill [Hypothesis]
\item $\Gamma_1\cup\Gamma_2,\ t_1\imp(t_2\imp b)\turn b$\hfill [\rul{MP} 5,6]
\item $\Gamma_1\cup \Gamma_2\turn(t_1\imp(t_2\imp b))\imp b$\hfill
[\rul{DISCH} 7]
\item $\Gamma_1\cup \Gamma_2\turn \uquant{b}(t_1\imp(t_2\imp b))\imp b$\hfill
[\rul{GEN} 8]
\item $\Gamma_1\cup \Gamma_2\turn t_1\conj t_2$\hfill
[\rul{EQ\_MP} (\rul{SYM} 2),9]
\end{proof}

\subsection{\texorpdfstring{$\wedge$-elimination}{Conjunction-elimination}}

\begin{holboxed}
\index{CONJUNCT1@\ml{CONJUNCT1}|pin}
\index{CONJUNCT2@\ml{CONJUNCT2}|pin}
\begin{verbatim}
   CONJUNCT1 : thm -> thm, CONJUNCT2 : thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1\conj t_2\over
\Gamma\turn t_1\qquad\qquad\qquad \Gamma\turn t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \conj = \lquant{b_1\ b_2}\uquant{b}(b_1\imp(b_2\imp b))\imp b$
\hfill [Definition of $\conj$]
\item $\turn t_1\conj t_2 = \uquant{b}(t_1\imp(t_2\imp b))\imp b$\hfill
[\rul{RIGHT\_LIST\_BETA} 1]
\item $\Gamma\turn t_1\conj t_2$\hfill [Hypothesis]
\item $\Gamma\turn \uquant{b}(t_1\imp(t_2\imp b))\imp b$\hfill
[\rul{EQ\_MP} 2,3]
\item $\Gamma\turn (t_1\imp(t_2\imp t_1))\imp t_1$\hfill [\rul{SPEC} 4]
\item $t_1\turn t_1$\hfill [\rul{ASSUME}]
\item $t_1 \turn t_2\imp t_1$\hfill [\rul{DISCH} 6]
\item $\turn t_1\imp(t_2\imp t_1)$\hfill [\rul{DISCH} 7]
\item $\Gamma\turn t_1$\hfill [\rul{MP} 5,8]
\item $\Gamma\turn (t_1\imp(t_2\imp t_2))\imp t_2$\hfill [\rul{SPEC} 4]
\item $t_2\turn t_2$\hfill [\rul{ASSUME}]
\item $\turn t_2\imp t_2$\hfill [\rul{DISCH} 11]
\item $\turn t_1\imp(t_2\imp t_2)$\hfill [\rul{DISCH} 12]
\item $\Gamma\turn t_2$\hfill [\rul{MP} 10,13]
\item $\Gamma\turn t_1$ and $\Gamma\turn t_2$\hfill [9,14]
\end{proof}

\subsection{\texorpdfstring{Right $\vee$-introduction}{Right disjunction-introduction}}\index{disjunction, in HOL logic@disjunction, in \HOL{} logic!inference rule for|(}

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

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1\over \Gamma\turn t_1\disj t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \disj =
\lquant{b_1\ b_2}\uquant{b}(b_1\imp b)\imp(b_2\imp b)\imp b$
\hfill [Definition of $\disj$]
\item $\turn t_1\disj t_2 = \uquant{b}(t_1\imp b)\imp(t_2\imp b)\imp b$
\hfill [\rul{RIGHT\_LIST\_BETA} 1]
\item $\Gamma\turn t_1$\hfill [Hypothesis]
\item $t_1\imp b\turn t_1\imp b$\hfill [\rul{ASSUME}]
\item $\Gamma,\ t_1\imp b\turn b$\hfill [\rul{MP} 4,3]
\item $\Gamma,\ t_1\imp b\turn(t_2\imp b)\imp b$\hfill [\rul{DISCH} 5]
\item $\Gamma\turn (t_1\imp b)\imp(t_2\imp b)\imp b$\hfill [\rul{DISCH} 6]
\item $\Gamma\turn \uquant{b}(t_1\imp b)
\imp(t_2\imp b)\imp b$\hfill [\rul{GEN} 7]
\item $\Gamma\turn t_1\disj t_2$\hfill [\rul{EQ\_MP} (\rul{SYM} 2),8]
\end{proof}

\subsection{\texorpdfstring{Left $\vee$-introduction}{Left disjunction-introduction}}

\begin{holboxed}
\index{DISJ2@\ml{DISJ2}|pin}
\begin{verbatim}
   DISJ2 : term -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_2\over \Gamma\turn t_1\disj t_2$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \disj =
\lquant{b_1\ b_2}\uquant{b}(b_1\imp b)\imp(b_2\imp b)\imp b$
\hfill [Definition of $\disj$]
\item $\turn t_1\disj t_2 = \uquant{b}(t_1\imp b)\imp(t_2\imp b)\imp b$
\hfill [\rul{RIGHT\_LIST\_BETA} 1]
\item $\Gamma\turn t_2$\hfill [Hypothesis]
\item $t_2\imp b\turn t_2\imp b$\hfill [\rul{ASSUME}]
\item $\Gamma,\ t_2\imp b\turn b$\hfill [\rul{MP} 4,3]
\item $\Gamma\turn(t_2\imp b)\imp b$\hfill [\rul{DISCH} 5]
\item $\Gamma\turn (t_1\imp b)\imp(t_2\imp b)\imp b$\hfill [\rul{DISCH} 6]
\item $\Gamma\turn \uquant{b}(t_1\imp b)
\imp(t_2\imp b)\imp b$\hfill [\rul{GEN} 7]
\item $\Gamma\turn t_1\disj t_2$\hfill [\rul{EQ\_MP} (\rul{SYM} 2),8]
\end{proof}

\subsection{\texorpdfstring{$\vee$-elimination}{Disjunction-elimination}}

\begin{holboxed}
\index{DISJ_CASES@\ml{DISJ\_CASES}|pin}
\begin{verbatim}
   DISJ_CASES : thm -> thm -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma\turn t_1\disj t_2\qquad\qquad\qquad\Gamma_1,\ t_1\turn t
\qquad\qquad\qquad \Gamma_2,\ t_2\turn t\over
\Gamma\cup\Gamma_1\cup\Gamma_2\turn t$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \disj =
\lquant{b_1\ b_2}\uquant{b}(b_1\imp b)\imp(b_2\imp b)\imp b$
\hfill [Definition of $\disj$]
\item $\turn t_1\disj t_2 = \uquant{b}(t_1\imp b)\imp(t_2\imp b)\imp b$
\hfill [\rul{RIGHT\_LIST\_BETA} 1]
\item $\Gamma\turn t_1\disj t_2$\hfill [Hypothesis]
\item $\Gamma\turn\uquant{b}(t_1\imp b)\imp(t_2\imp b)\imp b$\hfill
[\rul{EQ\_MP} 2,3]
\item $\Gamma\turn(t_1\imp t)\imp(t_2\imp t)\imp t$\hfill [\rul{SPEC} 4]
\item $\Gamma_1,\ t_1\turn t$\hfill [Hypothesis]
\item $\Gamma_1\turn t_1\imp t$\hfill [\rul{DISCH} 6]
\item $\Gamma\cup \Gamma_1\turn (t_2\imp t)\imp t$\hfill [\rul{MP} 5,7]
\item $\Gamma_2,\ t_2\turn t$\hfill [Hypothesis]
\item $\Gamma_2\turn t_2\imp t$\hfill [\rul{DISCH} 9]
\item $\Gamma\cup \Gamma_1\cup \Gamma_2\turn t$\hfill [\rul{MP} 8,10]
\end{proof}
\index{disjunction, in HOL logic@disjunction, in \HOL{} logic!inference rule for|)}

\subsection{Classical contradiction rule}
\index{F (falsity), the HOL constant@\holtxt{F} (falsity), the \HOL{} constant!rules of inference for}

\begin{holboxed}
\index{CCONTR@\ml{CCONTR}|pin}
\index{contradiction rule, in HOL logic@contradiction rule, in \HOL{} logic}
\begin{verbatim}
   CCONTR : term -> thm -> thm
\end{verbatim}\end{holboxed}

\vspace{12pt plus2pt minus1pt}

$$\Gamma,\ \neg t\turn \F\over \Gamma\turn t$$

\vspace{12pt plus2pt minus1pt}

\begin{proof}
\item $\turn \neg = \lquant{b}b\imp\F$\hfill [Definition of $\neg$]
\item $\turn \neg t = t\imp\F$\hfill [\rul{RIGHT\_LIST\_BETA} 1]
\item $\Gamma,\ \neg t\turn\F$\hfill [Hypothesis]
\item $\Gamma\turn \neg t\imp\F$\hfill  [\rul{DISCH} 3]
\item $\Gamma\turn (t\imp\F)\imp\F$\hfill [\rul{SUBST} 2,4]
\item $t = \F\turn t = \F$\hfill [\rul{ASSUME}]
\item $\Gamma,\ t=\F\turn (\F\imp\F)\imp\F$\hfill [\rul{SUBST} 6,5]
\item $\F\turn\F$\hfill [\rul{ASSUME}]
\item $\turn \F\imp\F$\hfill [\rul{DISCH} 8]
\item $\Gamma,\ t=\F\turn\F$\hfill [\rul{MP} 7,9]
\item $\turn \F = \uquant{b}b$\hfill [Definition of $\F$]
\item $\Gamma,\ t=\F\turn \uquant{b}b$\hfill [\rul{SUBST} 11,10]
\item $\Gamma,\ t=\F\turn t$\hfill [\rul{SPEC} 12]
\item $\turn \uquant{b} (b = \T)\disj(b = \F)$\hfill [Axiom]
\item $\turn (t = \T)\disj(t = \F)$\hfill [\rul{SPEC} 14]
\item $t=\T\turn t=\T$\hfill [\rul{ASSUME}]
\item $t=\T\turn t$\hfill [\rul{EQT\_ELIM} 16]
\item $\Gamma\turn t$\hfill [\rul{DISJ\_CASES} 15,17,13]
\end{proof}
\index{derived rules, in HOL logic@derived rules, in \HOL{} logic!list and derivations of some|)}
\index{inference rules, of HOL logic@inference rules, of \HOL{} logic!derived|)}

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