%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (c) 2003-2018 by The University of Queensland
% http://www.uq.edu.au
%
% Primary Business: Queensland, Australia
% Licensed under the Apache License, version 2.0
% http://www.apache.org/licenses/LICENSE-2.0
%
% Development until 2012 by Earth Systems Science Computational Center (ESSCC)
% Development 2012-2013 by School of Earth Sciences
% Development from 2014 by Centre for Geoscience Computing (GeoComp)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\chapter{The \finley Module}\label{chap:finley}
%\declaremodule{extension}{finley}
%\modulesynopsis{Solving linear, steady partial differential equations using finite elements}

The \finley library allows the creation of domains for solving
linear, steady partial differential
equations\index{partial differential equations} (PDEs) or systems
of PDEs using isoparametrical finite elements\index{FEM!isoparametrical}.
It supports unstructured 1D, 2D and 3D meshes.
The PDEs themselves are represented by the \LinearPDE class
of \escript.
\finley is parallelized under both \OPENMP and \MPI.
A more restricted form of this library ({\it dudley}) is described in
Section~\ref{sec:dudley}.

\section{Formulation}
For a single PDE that has a solution with a single component the linear PDE is
defined in the following form:
\begin{equation}\label{FINLEY.SINGLE.1}
\begin{array}{cl} &
\displaystyle{
\int_{\Omega}
A_{jl} \cdot v_{,j}u_{,l}+ B_{j} \cdot v_{,j} u+ C_{l} \cdot v u_{,l}+D \cdot vu \; d\Omega }  \\
+ & \displaystyle{\int_{\Gamma} d \cdot vu \; d{\Gamma} }
+  \displaystyle{\int_{\Gamma^{contact}} d^{contact} \cdot [v][u] \; d{\Gamma} } \\
= & \displaystyle{\int_{\Omega}  X_{j} \cdot v_{,j}+ Y \cdot v \; d\Omega }\\
+ & \displaystyle{\int_{\Gamma} y \cdot v \; d{\Gamma}}  +
\displaystyle{\int_{\Gamma^{contact}} y^{contact}\cdot [v] \; d{\Gamma}} \\
\end{array}
\end{equation}

\section{Meshes}
\label{FINLEY MESHES}

\begin{figure}
\centerline{\includegraphics{FinleyMesh}}
\caption{Subdivision of an Ellipse into triangles order 1 (\finleyelement{Tri3})}
\label{FINLEY FIG 0}
\end{figure}

To understand the usage of \finley one needs to have an understanding of how
the finite element meshes\index{FEM!mesh} are defined.
\fig{FINLEY FIG 0} shows an example of the subdivision of an ellipse into
so-called elements\index{FEM!elements}\index{element}.
In this case, triangles have been used but other forms of subdivisions can be
constructed, e.g. quadrilaterals or, in the three-dimensional case, into
tetrahedra and hexahedra. The idea of the finite element method is to
approximate the solution by a function which is a polynomial of a certain order
and is continuous across its boundary to neighbour elements.
In the example of \fig{FINLEY FIG 0} a linear polynomial is used on each
triangle. As one can see, the triangulation is quite a poor approximation of
the ellipse. It can be improved by introducing a midpoint on each element edge
then positioning those nodes located on an edge expected to describe the
boundary, onto the boundary.
In this case the triangle gets a curved edge which requires a parameterization
of the triangle using a quadratic polynomial.
For this case, the solution is also approximated by a piecewise quadratic
polynomial (which explains the name isoparametrical elements),
see \Ref{Zienc,NumHand} for more details.
\finley also supports macro elements\index{macro elements}.
For these elements a piecewise linear approximation is used on an element which
is further subdivided (in the case of \finley halved).
As such, these elements do not provide more than a further mesh refinement but
should be used in the case of incompressible flows, see \class{StokesProblemCartesian}.
For these problems a linear approximation of the pressure across the element is
used (use the \ReducedSolutionFS) while the refined element is used to
approximate velocity. So a macro element provides a continuous pressure
approximation together with a velocity approximation on a refined mesh.
This approach is necessary to make sure that the incompressible flow has a
unique solution.

The union of all elements defines the domain of the PDE.
Each element is defined by the nodes used to describe its shape.
In \fig{FINLEY FIG 0} the element, which has type \finleyelement{Tri3}, with
element reference number $19$\index{element!reference number} is defined by the
nodes with reference numbers $9$, $11$ and $0$\index{node!reference number}.
Notice that the order is counterclockwise.
The coefficients of the PDE are evaluated at integration nodes with each
individual element.
For quadrilateral elements a Gauss quadrature scheme is used.
In the case of triangular elements a modified form is applied.
The boundary of the domain is also subdivided into elements\index{element!face}.
In \fig{FINLEY FIG 0} line elements with two nodes are used.
The elements are also defined by their describing nodes, e.g. the face element
with reference number $20$, which has type \finleyelement{Line2}, is defined by
the nodes with the reference numbers $11$ and $0$.
Again the order is crucial, if moving from the first to second node the domain
has to lie on the left hand side (in the case of a two-dimensional surface
element the domain has to lie on the left hand side when moving
counterclockwise). If the gradient on the surface of the domain is to be
calculated rich face elements need to be used. Rich elements on a face are
identical to interior elements but with a modified order of nodes such that the
'first' face of the element aligns with the surface of the domain.
In \fig{FINLEY FIG 0} elements of the type \finleyelement{Tri3Face} are used.
The face element reference number $20$ as a rich face element is defined by the
nodes with reference numbers $11$, $0$ and $9$.
Notice that the face element $20$ is identical to the interior element $19$
except that, in this case, the order of the node is different to align the first
edge of the triangle (which is the edge starting with the first node) with the
boundary of the domain.

Be aware that face elements and elements in the interior of the domain must
match, i.e. a face element must be the face of an interior element or, in case
of a rich face element, it must be identical to an interior element.
If no face elements are specified \finley implicitly assumes homogeneous
natural boundary conditions\index{natural boundary conditions!homogeneous},
i.e. \var{d}=$0$ and \var{y}=$0$, on the entire boundary of the domain.
For inhomogeneous natural boundary conditions\index{natural boundary conditions!inhomogeneous},
the boundary must be described by face elements.

\begin{figure}
\centerline{\includegraphics{FinleyContact}}
\caption{Mesh around a contact region (\finleyelement{Rec4})}
\label{FINLEY FIG 01}
\end{figure}

If discontinuities of the PDE solution are considered, contact
elements\index{element!contact}\index{contact conditions} are introduced to
describe the contact region $\Gamma^{contact}$ even if $d^{contact}$ and
$y^{contact}$ are zero.
\fig{FINLEY FIG 01} shows a simple example of a mesh of rectangular elements
around a contact region $\Gamma^{contact}$\index{element!contact}.
The contact region is described by the elements $4$, $3$ and $6$.
Their element type is \finleyelement{Line2_Contact}.
The nodes $9$, $12$, $6$ and $5$ define contact element $4$, where the
coordinates of nodes $12$ and $5$ and nodes $4$ and $6$ are identical, with the
idea that nodes $12$ and $9$ are located above and nodes $5$ and $6$ below the
contact region.
Again, the order of the nodes within an element is crucial.
There is also the option of using rich elements if the gradient is to be
calculated on the contact region. Similarly to the rich face elements these
are constructed from two interior elements by reordering the nodes such that
the 'first' face of the element above and the 'first' face of the element below
the contact regions line up. The rich version of element $4$ is of type
\finleyelement{Rec4Face_Contact} and is defined by the nodes $9$, $12$, $16$,
$18$, $6$, $5$, $0$ and $2$.
\tab{FINLEY TAB 1} shows the interior element types and the corresponding
element types to be used on the face and contacts.
\fig{FINLEY.FIG:1}, \fig{FINLEY.FIG:2} and \fig{FINLEY.FIG:4} show the ordering
of the nodes within an element.

\begin{table}
\centering
\begin{tabular}{l|llll}
\textbf{interior}&\textbf{face}&\textbf{rich face}&\textbf{contact}&\textbf{rich contact}\\
\hline
\finleyelement{Line2} & \finleyelement{Point1} & \finleyelement{Line2Face} & \finleyelement{Point1_Contact} & \finleyelement{Line2Face_Contact}\\
\finleyelement{Line3} & \finleyelement{Point1} & \finleyelement{Line3Face} & \finleyelement{Point1_Contact} & \finleyelement{Line3Face_Contact}\\
\finleyelement{Tri3} & \finleyelement{Line2} & \finleyelement{Tri3Face} & \finleyelement{Line2_Contact} & \finleyelement{Tri3Face_Contact}\\
\finleyelement{Tri6} & \finleyelement{Line3} & \finleyelement{Tri6Face} & \finleyelement{Line3_Contact} & \finleyelement{Tri6Face_Contact}\\
\finleyelement{Rec4} & \finleyelement{Line2} & \finleyelement{Rec4Face} & \finleyelement{Line2_Contact} & \finleyelement{Rec4Face_Contact}\\
\finleyelement{Rec8} & \finleyelement{Line3} & \finleyelement{Rec8Face} & \finleyelement{Line3_Contact} & \finleyelement{Rec8Face_Contact}\\
\finleyelement{Rec9} & \finleyelement{Line3} & \finleyelement{Rec9Face} & \finleyelement{Line3_Contact} & \finleyelement{Rec9Face_Contact}\\
\finleyelement{Tet4} & \finleyelement{Tri6} & \finleyelement{Tet4Face} & \finleyelement{Tri6_Contact} & \finleyelement{Tet4Face_Contact}\\
\finleyelement{Tet10} & \finleyelement{Tri9} & \finleyelement{Tet10Face} & \finleyelement{Tri9_Contact} & \finleyelement{Tet10Face_Contact}\\
\finleyelement{Hex8} & \finleyelement{Rec4} & \finleyelement{Hex8Face} & \finleyelement{Rec4_Contact} & \finleyelement{Hex8Face_Contact}\\
\finleyelement{Hex20} & \finleyelement{Rec8} & \finleyelement{Hex20Face} & \finleyelement{Rec8_Contact} & \finleyelement{Hex20Face_Contact}\\
\finleyelement{Hex27} & \finleyelement{Rec9} & N/A & N/A & N/A\\
\finleyelement{Hex27Macro} & \finleyelement{Rec9Macro} & N/A & N/A & N/A\\
\finleyelement{Tet10Macro} & \finleyelement{Tri6Macro} & N/A & N/A & N/A\\
\finleyelement{Rec9Macro} & \finleyelement{Line3Macro} & N/A & N/A & N/A\\
\finleyelement{Tri6Macro} & \finleyelement{Line3Macro} & N/A & N/A & N/A\\
\end{tabular}
\caption{Finley elements and corresponding elements to be used on domain faces
and contacts.
The rich types have to be used if the gradient of the function is to be
calculated on faces and contacts, respectively.}
\label{FINLEY TAB 1}
\end{table}

The native \finley file format is defined as follows.
Each node \var{i} has \var{dim} spatial coordinates \var{Node[i]}, a reference
number \var{Node_ref[i]}, a degree of freedom \var{Node_DOF[i]} and a tag
\var{Node_tag[i]}.
In most cases \var{Node_DOF[i]}=\var{Node_ref[i]} however, for periodic
boundary conditions, \var{Node_DOF[i]} is chosen differently, see example below.
The tag can be used to mark nodes sharing the same properties.
Element \var{i} is defined by the \var{Element_numNodes} nodes
\var{Element_Nodes[i]} which is a list of node reference numbers.
The order of these is crucial. Each element has a reference number
\var{Element_ref[i]} and a tag \var{Element_tag[i]}.
The tag can be used to mark elements sharing the same properties.
For instance elements above a contact region are marked with tag $2$ and
elements below a contact region are marked with tag $1$.
\var{Element_Type} and \var{Element_Num} give the element type and the number
of elements in the mesh.
Analogue notations are used for face and contact elements.
The following \PYTHON script prints the mesh definition in the \finley file
format:
\begin{python}
  print("%s\n"%mesh_name)
  # node coordinates:
  print("%dD-nodes %d\n"%(dim, numNodes))
  for i in range(numNodes):
     print("%d %d %d"%(Node_ref[i], Node_DOF[i], Node_tag[i]))
     for j in range(dim): print(" %e"%Node[i][j])
     print("\n")
  # interior elements
  print("%s %d\n"%(Element_Type, Element_Num))
  for i in range(Element_Num):
     print("%d %d"%(Element_ref[i], Element_tag[i]))
     for j in range(Element_numNodes): print(" %d"%Element_Nodes[i][j])
     print("\n")
  # face elements
  print("%s %d\n"%(FaceElement_Type, FaceElement_Num))
  for i in range(FaceElement_Num):
     print("%d %d"%(FaceElement_ref[i], FaceElement_tag[i]))
     for j in range(FaceElement_numNodes): print(" %d"%FaceElement_Nodes[i][j])
     print("\n")
  # contact elements
  print("%s %d\n"%(ContactElement_Type, ContactElement_Num))
  for i in range(ContactElement_Num):
     print("%d %d"%(ContactElement_ref[i], ContactElement_tag[i]))
     for j in range(ContactElement_numNodes):
         print(" %d"%ContactElement_Nodes[i][j])
     print("\n")
  # point sources (not supported yet)
  print("Point1 0")
\end{python}

The following example of a mesh file defines the mesh shown in \fig{FINLEY FIG 01}:
\begin{verbatim}
Example 1
2D Nodes 16
0   0 0 0.   0.
2   2 0 0.33 0.
3   3 0 0.66 0.
7   4 0 1.   0.
5   5 0 0.   0.5
6   6 0 0.33 0.5
8   8 0 0.66 0.5
10 10 0 1.0  0.5
12 12 0 0.   0.5
9   9 0 0.33 0.5
13 13 0 0.66 0.5
15 15 0 1.0  0.5
16 16 0 0.   1.0
18 18 0 0.33 1.0
19 19 0 0.66 1.0
20 20 0 1.0  1.0
Rec4 6
 0 1  0  2  6  5
 1 1  2  3  8  6
 2 1  3  7 10  8
 5 2 12  9 18 16
 7 2 13 19 18  9
10 2 20 19 13 15
Line2 0
Line2_Contact 3
 4 0  9 12  6 5
 3 0 13  9  8 6
 6 0 15 13 10 8
Point1 0
\end{verbatim}
Notice that the order in which the nodes and elements are given is arbitrary.
In the case that rich contact elements are used the contact element section
gets the form
\begin{verbatim}
Rec4Face_Contact 3
 4 0  9 12 16 18  6  5  0  2
 3 0 13  9 18 19  8  6  2  3
 6 0 15 13 19 20 10  8  3  7
\end{verbatim}
Periodic boundary conditions\index{boundary conditions!periodic} can be
introduced by altering \var{Node_DOF}.
It allows identification of nodes even if they have different physical locations.
For instance, to enforce periodic boundary conditions at the face $x_0=0$ and
$x_0=1$ one identifies the degrees of freedom for nodes $0$, $5$, $12$ and $16$
with the degrees of freedom for $7$, $10$, $15$ and $20$, respectively.
The node section of the \finley mesh now reads:
\begin{verbatim}
2D Nodes 16
0   0 0 0.   0.
2   2 0 0.33 0.
3   3 0 0.66 0.
7   0 0 1.   0.
5   5 0 0.   0.5
6   6 0 0.33 0.5
8   8 0 0.66 0.5
10  5 0 1.0  0.5
12 12 0 0.   0.5
9   9 0 0.33 0.5
13 13 0 0.66 0.5
15 12 0 1.0  0.5
16 16 0 0.   1.0
18 18 0 0.33 1.0
19 19 0 0.66 1.0
20 16 0 1.0  1.0
\end{verbatim}

\clearpage
\input{finleyelements}
\clearpage

\section{Macro Elements}
\label{SEC FINLEY MACRO}

\begin{figure}[th]
\begin{center}
\includegraphics{FinleyMacroLeg}\\
\subfigure[Triangle]{\label{FINLEY MACRO TRI}\includegraphics{FinleyMacroTri}}\quad
\subfigure[Quadrilateral]{\label{FINLEY MACRO REC}\includegraphics{FinleyMacroRec}}
\end{center}
\caption{Macro elements in \finley}
\end{figure}

\finley supports the usage of macro elements\index{macro elements} which can be
used to achieve LBB compliance when solving incompressible fluid flow problems.
LBB compliance is required to get a problem which has a unique solution for
pressure and velocity. For macro elements the pressure and velocity are
approximated by a polynomial of order 1 but the velocity approximation bases on
a refinement of the elements. The nodes of a triangle and quadrilateral element
are shown in Figures~\ref{FINLEY MACRO TRI} and~\ref{FINLEY MACRO REC},
respectively. In essence, the velocity uses the same nodes like a quadratic
polynomial approximation but replaces the quadratic polynomial by piecewise
linear polynomials. In fact, this is the way \finley defines the macro elements.
In particular \finley uses the same local ordering of the nodes for the macro
element as for the corresponding quadratic element. Another interpretation is
that one uses a linear approximation of the velocity together with a linear
approximation of the pressure but on elements created by combining elements to
macro elements. Notice that the macro elements still use quadratic
interpolation to represent the element and domain boundary.
However, if elements have linear boundaries a macro element approximation for
the velocity is equivalent to using a linear approximation on a mesh which is
created through a one-step global refinement.
Typically macro elements are only required to use when an incompressible fluid
flow problem is solved, e.g. the Stokes problem in \Sec{STOKES PROBLEM}.
Please see \Sec{FINLEY MESHES} for more details on the supported macro elements.

\section{Linear Solvers in \SolverOptions}

Table~\ref{TAB FINLEY SOLVER OPTIONS 1} and
Table~\ref{TAB FINLEY SOLVER OPTIONS 2} show the solvers and preconditioners
supported by \finley through the \PASO library.
Currently direct solvers are not supported under \MPI.
By default, \finley uses the iterative solvers \PCG for symmetric and \BiCGStab
for non-symmetric problems.
If the direct solver is selected, which can be useful when solving very
ill-posed equations, \finley uses the \MKL\footnote{If the stiffness matrix is
non-regular \MKL may return without a proper error code. If you observe
suspicious solutions when using \MKL, this may be caused by a non-invertible
operator.} solver package. If \MKL is not available \UMFPACK is used.
If \UMFPACK is not available a suitable iterative solver from \PASO is used.

\begin{table}
\centering
{\scriptsize
\begin{tabular}{l||c|c|c|c|c|c|c|c}
\member{setSolverMethod} & \member{DIRECT}& \member{PCG} & \member{GMRES} & \member{TFQMR} & \member{MINRES} & \member{PRES20} & \member{BICGSTAB} & lumping \\
\hline
 \hline
 \member{setReordering} & $\checkmark$ & & & & & &\\
 \hline  \member{setRestart} &  & & $\checkmark$ & & & $20$ & \\
 \hline\member{setTruncation} &  & & $\checkmark$ & & & $5$ & \\
   \hline\member{setIterMax} &  & $\checkmark$& $\checkmark$ & $\checkmark$& $\checkmark$& $\checkmark$ & $\checkmark$ \\
 \hline\member{setTolerance} &  & $\checkmark$& $\checkmark$ & $\checkmark$& $\checkmark$& $\checkmark$ & $\checkmark$ \\
 \hline\member{setAbsoluteTolerance} &  & $\checkmark$& $\checkmark$ & $\checkmark$& $\checkmark$& $\checkmark$ & $\checkmark$ \\
\hline\member{setReordering} & $\checkmark$ & & & & & & & \\
\end{tabular}
}
\caption{Solvers available for \finley and the \PASO package and the relevant
options in \class{SolverOptions}.
\MKL supports \member{MINIMUM_FILL_IN}\index{linear solver!minimum fill-in ordering}\index{minimum fill-in ordering}
and \member{NESTED_DISSECTION}\index{linear solver!nested dissection ordering}\index{nested dissection}
reordering.
Currently the \UMFPACK interface does not support any reordering.
\label{TAB FINLEY SOLVER OPTIONS 1}}
\end{table}

\begin{table}
\begin{center}
{\scriptsize
\begin{tabular}{l||c|c|c|c|c|c|c}
\member{NO_PRECONDITIONER}&
\member{AMG}&
\member{JACOBI}&
\member{GAUSS_SEIDEL}&
\member{REC_ILU}&
\member{RILU}&
\member{ILU0}&
\member{DIRECT}\\
\hline
status:& $\checkmark$ &$\checkmark$&$\checkmark$&$\checkmark$&later&$\checkmark$&later\\
\hline
\hline
\member{setLevelMax}&$\checkmark$& & & & & &\\
\hline
\member{setCoarseningThreshold}&$\checkmark$& & & & & &\\
\hline
\member{setMinCoarseMatrixSize}&$\checkmark$& & & & & &\\
\hline
\member{setMinCoarseMatrixSparsity}&$\checkmark$& & & & & &\\
\hline
\member{setNumSweeps}& &$\checkmark$&$\checkmark$& & & &\\
\hline
\member{setNumPreSweeps}&$\checkmark$& & & & & &\\
\hline
\member{setNumPostSweeps}&$\checkmark$& & & & & &\\
\hline
\member{setDiagonalDominanceThreshold}&$\checkmark$& & & & & &\\
\hline
\member{setAMGInterpolation}&$\checkmark$& & & & & &\\
\hline
\member{setRelaxationFactor}& & & & &$\checkmark$& &\\
\end{tabular}
}
\caption{Preconditioners available for \finley and the \PASO package and the
relevant options in \class{SolverOptions}.
\label{TAB FINLEY SOLVER OPTIONS 2}}
\end{center}
\end{table}

\section{Functions}
\begin{funcdesc}{ReadMesh}{fileName \optional{, \optional{integrationOrder=-1}, optimize=True}}
creates a \Domain object from the FEM mesh defined in file \var{fileName}.
The file must be in the \finley file format.
If \var{integrationOrder} is positive, a numerical integration scheme is chosen
which is accurate on each element up to a polynomial of degree
\var{integrationOrder}\index{integration order}.
Otherwise an appropriate integration order is chosen independently.
By default the labeling of mesh nodes and element distribution is optimized.
Set \var{optimize=False} to switch off relabeling and redistribution.
\end{funcdesc}

\begin{funcdesc}{ReadGmsh}{fileName, numDim, \optional{, \optional{integrationOrder=-1}, optimize=True\optional{, useMacroElements=False}}}
creates a \Domain object from the FEM mesh defined in file \var{fileName} for
a domain of dimension \var{numDim}.
The file must be in the \gmshextern file format.
If \var{integrationOrder} is positive, a numerical integration scheme is chosen
which is accurate on each element up to a polynomial of degree
\var{integrationOrder}\index{integration order}.
Otherwise an appropriate integration order is chosen independently.
By default the labeling of mesh nodes and element distribution is optimized.
Set \var{optimize=False} to switch off relabeling and redistribution.
If \var{useMacroElements} is set, second order elements are interpreted as
macro elements\index{macro elements}.
\end{funcdesc}

\begin{funcdesc}{MakeDomain}{design\optional{, integrationOrder=-1\optional{, optimizeLabeling=True\optional{, useMacroElements=False}}}}
creates a \finley \Domain from a \pycad \class{Design} object using \gmshextern.
The \class{Design} \var{design} defines the geometry.
If \var{integrationOrder} is positive, a numerical integration scheme is chosen
which is accurate on each element up to a polynomial of degree
\var{integrationOrder}\index{integration order}.
Otherwise an appropriate integration order is chosen independently.
Set \var{optimizeLabeling=False} to switch off relabeling and redistribution
(not recommended).
If \var{useMacroElements} is set, macro elements\index{macro elements} are used.
Currently \function{MakeDomain} does not support \MPI.
\end{funcdesc}

\begin{funcdesc}{load}{fileName}
recovers a \Domain object from a dump file \var{fileName} created by the
\function{dump} method of a \Domain object.
\end{funcdesc}

\begin{funcdesc}{Rectangle}{n0,n1,order=1,l0=1.,l1=1., integrationOrder=-1, \\
  periodic0=\False, periodic1=\False, useElementsOnFace=\False, optimize=\False}
generates a \Domain object representing a two-dimensional rectangle between
$(0,0)$ and $(l0,l1)$ with orthogonal edges.
The rectangle is filled with \var{n0} elements along the $x_0$-axis and
\var{n1} elements along the $x_1$-axis.
For \var{order}=1 and \var{order}=2, elements of type \finleyelement{Rec4} and
\finleyelement{Rec8} are used, respectively.
In the case of \var{useElementsOnFace}=\False, \finleyelement{Line2} and
\finleyelement{Line3} are used to subdivide the edges of the rectangle, respectively.
If \var{order}=-1, \finleyelement{Rec8Macro} and \finleyelement{Line3Macro}\index{macro elements}
are used. This option should be used when solving incompressible fluid flow
problems, e.g. \class{StokesProblemCartesian}.
In the case of \var{useElementsOnFace}=\True (this option should be used if
gradients are calculated on domain faces), \finleyelement{Rec4Face} and
\finleyelement{Rec8Face} are used on the edges, respectively.
If \var{integrationOrder} is positive, a numerical integration scheme is chosen
which is accurate on each element up to a polynomial of degree
\var{integrationOrder}\index{integration order}.
Otherwise an appropriate integration order is chosen independently.
If \var{periodic0}=\True, periodic boundary conditions\index{periodic boundary conditions}
along the $x_0$-direction are enforced.
That means for any solution of a PDE solved by \finley the values on the line
$x_0=0$ will be identical to the values on $x_0=\var{l0}$.
Correspondingly, \var{periodic1}=\True sets periodic boundary conditions in the
$x_1$-direction.
If \var{optimize}=\True mesh node relabeling will be attempted to reduce the
computation and also ParMETIS will be used to improve the mesh partition if
running on multiple CPUs with \MPI.
\end{funcdesc}

\begin{funcdesc}{Brick}{n0,n1,n2,order=1,l0=1.,l1=1.,l2=1., integrationOrder=-1,
  periodic0=\False, periodic1=\False, \\ periodic2=\False, useElementsOnFace=\False,useFullElementOrder=\False, optimize=\False}
generates a \Domain object representing a three-dimensional brick between
$(0,0,0)$ and $(l0,l1,l2)$ with orthogonal faces. The brick is filled with
\var{n0} elements along the $x_0$-axis,
\var{n1} elements along the $x_1$-axis and
\var{n2} elements along the $x_2$-axis.
For \var{order}=1 and \var{order}=2, elements of type \finleyelement{Hex8} and
\finleyelement{Hex20} are used, respectively.
In the case of \var{useElementsOnFace}=\False, \finleyelement{Rec4} and
\finleyelement{Rec8} are used to subdivide the faces of the brick, respectively.
In the case of \var{useElementsOnFace}=\True (this option should be used if
gradients are calculated on domain faces), \finleyelement{Hex8Face} and
\finleyelement{Hex20Face} are used on the brick faces, respectively.
If \var{order}=-1, \finleyelement{Hex20Macro} and \finleyelement{Rec8Macro}\index{macro elements}
are used. This option should be used when solving incompressible fluid flow
problems, e.g. \class{StokesProblemCartesian}.
If \var{integrationOrder} is positive, a numerical integration scheme is chosen
which is accurate on each element up to a polynomial of degree
\var{integrationOrder}\index{integration order}.
Otherwise an appropriate integration order is chosen independently.
If \var{periodic0}=\True, periodic boundary conditions\index{periodic boundary conditions}
along the $x_0$-direction are enforced.
That means for any solution of a PDE solved by \finley the values on the plane
$x_0=0$ will be identical to the values on $x_0=\var{l0}$.
Correspondingly, \var{periodic1}=\True and \var{periodic2}=\True sets periodic
boundary conditions in the $x_1$-direction and $x_2$-direction, respectively.
If \var{optimize}=\True mesh node relabeling will be attempted to reduce the
computation and also ParMETIS will be used to improve the mesh partition if
running on multiple CPUs with \MPI.
\end{funcdesc}

\begin{funcdesc}{GlueFaces}{meshList, tolerance=1.e-13}
generates a new \Domain object from the list \var{meshList} of \finley meshes.
Nodes in face elements whose difference of coordinates is less than
\var{tolerance} times the diameter of the domain are merged.
The corresponding face elements are removed from the mesh.
\function{GlueFaces} is not supported under \MPI with more than one rank.
\end{funcdesc}

\begin{funcdesc}{JoinFaces}{meshList, tolerance=1.e-13}
generates a new \Domain object from the list \var{meshList} of \finley meshes.
Face elements whose node coordinates differ by less than \var{tolerance} times
the diameter of the domain are combined to form a contact element\index{element!contact}.
The corresponding face elements are removed from the mesh.
\function{JoinFaces} is not supported under \MPI with more than one rank.
\end{funcdesc}

\section{\dudley}
\label{sec:dudley}
The {\it dudley} library is a restricted version of {\it finley}.
So in many ways it can be used as a ``drop-in'' replacement.
Dudley domains are simpler in that only triangular (2D), tetrahedral (3D) and line elements are supported.
Note, this also means that dudley does not support:
\begin{itemize}
\item dirac delta functions
\item contact elements
\item macro elements
\end{itemize}


\documentclass[ 12pt, a4paper]{article}
% Use the option doublespacing or reviewcopy to obtain double line spacing
% \documentclass[doublespacing]{elsart}

\usepackage[utf8]{inputenc}
\usepackage[backend = biber, maxcitenames=2,uniquelist=minyear]{biblatex}

\AtEveryBibitem{\clearfield{number}}
\AtEveryBibitem{\clearfield{doi}}
\AtEveryBibitem{\clearfield{url}}
\AtEveryBibitem{\clearfield{issn}}
\AtEveryBibitem{\clearfield{isbn}}

\addbibresource{references.bib}

\usepackage{color,graphicx,tikz}
\usetikzlibrary{positioning,arrows}
% The amssymb package provides various useful mathematical symbols
\usepackage{mathtools,amssymb,amsmath,mathdots}
\usepackage[mathscr]{eucal} %just for the font \mathscr
\usepackage{setspace}
\usepackage{hyperref}

\usepackage{tikz}

\renewcommand{\vec}[1]{\boldsymbol{#1}}
\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\input{macros}

\begin{document}

\title{Multiple scattering of waves}
\author{
Artur L. Gower$^{a}$,\\
\footnotesize{$^{a}$ School of Mathematics, University of Manchester, Oxford Road, Manchester, M13 9PL,UK}
}
\date{\today}
\maketitle

\begin{abstract}
Here we show and deduce the T-matrix and a general multiple scattering formulation which can be adapted to acoustics, electromagnetism, and elasticity. For details on each specific physical medium see the other documents.
\end{abstract}

\noindent
{\textit{Keywords:} Multiple scattering, T-matrix, Scattering matrix}

\section{Using a T-matrix}
A T-matrix denotes how one single particle scatters waves~\parencite{ganesh_far-field_2010,ganesh_algorithm_2017}.

For convenience and generality we denote:
\begin{equation}
\begin{aligned}
    & \mathrm u_{n}(k\rv) = \text{outgoing spherical waves},
    \label{eqn:outgoing_waves_and_regular_waves}
    \\
    & \mathrm v_{n}(k\rv)= \text{regular spherical waves},
 \end{aligned}
\end{equation}
where $n$ denotes a multi index which depends on the dimension and if the waves are scalar or vector fields.

Any incident wave and scattered wave\footnote{For the scattered wave we need only use outgoing spherical waves when measuring the field outside of a sphere which completely encompasses the particle.}, centred at the same coordinate axis, can be written as
\begin{align}
  & \ui = \sum_{n} g_n \mathrm v_{n}(k\rv),
  \\
  & \us = \sum_{n} f_n \mathrm u_{n}(k\rv).
\end{align}
The T-matrix is an infinite matrix such that
\begin{equation}
  f_n = \sum_{n'} T_{nn'} g_{n'}.
\end{equation}
Such a matrix $T$ exists when scattering is a linear operation (elastic scattering).

We can also estimate the field inside the particle by assuming that the field is smooth and continuous. This approximation is exact for homogeneous spheres and cylinders, but not for a \href{acoustics.pdf}{Circular cylindrical capsule}.

Assume the field inside the particle can be described by a regular spherical series:
\begin{equation}
  \vi = \sum_n b_n \mathrm v_n(k_o \rv),
\end{equation}
where $k_o$ if the particles wavenumber. Now if we assume that the total field is continuous everywhere so that $\ui + \us = \vi$ on the boundary of the particle. If the field was smooth enough, we could analytically extend the field $\vi$ to a spherical boundary, with radius $a$, which contains the particle. Let's take this as an assumption and equate $\ui + \us = \vi$ for $r=a$. Due to orthogonality of the angular components of the basis functions this will result in
\begin{equation}
   g_n \mathrm v_{n}(k\rv) + f_n \mathrm u_{n}(k\rv) = b_n \mathrm v_n(k_o \rv), \quad \text{for} \;\; |\rv| = a
\end{equation}
using the T-matrix we can then write $g_n = T_{nm}^{-1} f_m$, which substituted above leads to
\begin{equation}
    b_n = \frac{1}{\mathrm v_n(k_o \rv)}[ \mathrm v_{n}(k\rv) T_{nm}^{-1} f_m + \mathrm u_{n}(k\rv) f_n], \quad \text{for} \;\; |\rv| = a.
\end{equation}

\section{Multiple scattering in general}

For multiple scattering in higher dimensions and for vector wave equations we use the notation given in \cite{gower2020effective}.

For a point $\rv$, outside of the circumscribed spheres of all particles, we can write the total field $u(\rv)$ as a sum of the incident wave $\ui(\rv)$ and all scattered waves in the form~\cite{Kristensson2015a,Kristensson2016,Linton+Martin2006}
\begin{equation}
    u(\rv) = \ui(\rv) + \us(\rv), \quad \us(\rv) =  \sum_{i=1}^N \sum_n f_n^i \mathrm u_n (k \rv - k \rv_i),
    \label{eqn:total_discrete_wave}
\end{equation}
where we assumed $ |\rv - \rv_i| > a_i $ for $i=1,2,\ldots N$, the $f_n^i$ are coefficients we need to determine, where again:
\begin{equation}
\left\{\begin{aligned}
    & \mathrm u_{n}(k\rv) = \text{outgoing spherical waves},
    \label{eqn:outgoing_waves_and_regular_waves}
    \\
    & \mathrm v_{n}(k\rv)= \text{regular spherical waves},
 \end{aligned}\right.
\end{equation}
where $n$ denotes a multi index which depends on the dimension and if the waves are scalar or vector fields.

In general, we can write the multiple scattering system in the form:
\begin{equation}\label{eqn:multiple-scattering-general}
   \alpha_n^i=g_{n}^i
    +\sum_{\substack{j=1\\j\neq i}}^N \sum_{n' n''}\mathcal{U}_{n''n}(k\rv_i - k\rv_j)T_{n''n'}^j \alpha_{n'}^j,
\end{equation}
for $i=1,2,\ldots,N$, where $f_n^i = \sum_{n'} T^i_{nn'}\alpha_{n'}^i$ and $\mathcal{U}_{nn'}$ is a translation matrix \cite{Bostrom+Kristensson+Strom1991,Friedman+Russek1954}. Let $\rv'=\rv+\dv$, then
the translation matrices for a translation $\dv$ can be defined by the property~\cite{Bostrom+Kristensson+Strom1991}
  \begin{equation}\label{eq:translation_spherical_waves}
 \left\{\begin{aligned}
   &\mathrm{v}_n(k\rv')=\sum_{n'}\mathcal{V}_{nn'}(k\dv)\mathrm{v}_{n'}(k\rv),\quad \text{ for all }\dv
   \\
 &\mathrm{u}_n(k\rv')=\sum_{n'}\mathcal{V}_{nn'}(k\dv)\mathrm{u}_{n'}(k\rv),\quad |\rv|>|\dv|
 \\
   &\mathrm{u}_n(k\rv')=\sum_{n'}\mathcal{U}_{nn'}(k\dv)\mathrm{v}_{n'}(k\rv),\quad |\rv|<|\dv|
   \\
 \end{aligned}\right.
 \end{equation}

\subsection{Turing equations into code}
For easy implementation we need the functions:
\[
\psi_\inc \mapsto g^m_j \quad \text{and} \quad \text{particle} \mapsto T^{nm}_j.
\]

For efficient implementation we rewrite~\eqref{eqn:multiple-scattering-general} as a matrix equation. Let
\begin{align}
  &(\vec \alpha_j)_n =  \alpha_n^j, \quad (\vec g_j)_n =  g_n^j,
  \\
  &(\vec T_j)_{nn'} = T_{nn'}^j, \quad (\vec {\mathcal U}_{j \ell})_{n'n} = \mathcal U_{n'n}(k \rv_j - k\rv_\ell),
  % H_{n'-n}(k R_{\ell j})\ee^{\ii(n'-n)\Theta_{\ell j}},
\end{align}

 Then
\begin{equation}
 \sum_{\ell}(\delta_{j \ell} +  (\delta_{j \ell}-1) \vec {\mathcal U}_{j \ell}^{\mathrm T} \vec T_\ell) \vec \alpha_\ell  =  \vec g_j,
\end{equation}
where $\cdot ^{\mathrm T}$ is the transpose operation. The above then leads to a block matrix equation:
\begin{equation}
  \begin{bmatrix}
    \vec I & - \vec {\mathcal U}_{1 2}^{\mathrm T}\vec T_2 & \cdots & - \vec {\mathcal U}_{1 (N-1)}^{\mathrm T} \vec T_{N-1} & - \vec {\mathcal U}_{1 N}^{\mathrm T} \vec T_N \\
    - \vec {\mathcal U}_{2 1}^{\mathrm T} \vec T_1 & \vec I &  - \vec {\mathcal U}_{2 3}^{\mathrm T} \vec T_3 & \cdots & - \vec {\mathcal U}_{2 N}^{\mathrm T} \vec T_N \\
     & \vdots & & & \vdots \\
     - \vec {\mathcal U}_{N 1}^{\mathrm T} \vec T_1  & \cdots & \cdots & -  \vec {\mathcal U}_{N (N-1)}^{\mathrm T} \vec T_{N-1} & \vec I
  \end{bmatrix}
  \begin{bmatrix}
    \vec \alpha_1 \\
    \vec \alpha_2 \\
    \vdots \\
    \vec \alpha_N
  \end{bmatrix}
   = \begin{bmatrix}
     \vec g_1 \\
     \vdots \\
     \vec g_N
   \end{bmatrix}
\end{equation}

\printbibliography

\end{document}


\chapter{Introduction}  %Title of the First Chapter

Using past experience to update ones behaviour is what differentiates intelligent beings from other creatures in our world. To artificially create said systems, that can learn from data acquired through experience, is the crowning goal of the field of Artificial Intelligence (AI) and Machine Learning (ML) \citep{turing}.
Realising this goal will have a large impact on the world and society we live in, as it will liberate humans from tasks that require cognition, like driving cars, booking appointments, and interpreting and acting on medical records, to give a few examples. Though, arguably, the field has still a long way to go before fulfilling its full potential \citep{grace2018will}.

An elegant approach to formalise the concept of learning through acquired experience is that of \emph{Bayesian learning} \citep{murphy}. In this framework, one specifies beliefs about quantities of interest, and updates them after observing data. The beliefs are represented using probability distributions: the \emph{prior} describes what is known about the quantity before any data is observed, and the so-called \emph{likelihood} describes the relation between the observed data and the quantity of interest. The framework provides a recipe for obtaining an updated belief over the quantity of interest after data has been observed. This distribution is known as the \emph{posterior}. Bayesian learning makes decisions atop these beliefs, and uses the posterior to reason about the optimality of a decision or the cost associated to a certain action.

The performance of a decision-making system will depend on the speed at which it can learn and the optimality of the decisions made---quantified by the \emph{regret}. It can be shown that, under certain assumptions, Bayesian learning gives rise to the optimal regret \citep{Lattimore}. However, such excellence comes at a high computational cost, which in many scenarios renders Bayesian learning---in its purest form---impractical. Fortunately, the literature has proposed many approximate methods which have lightened the computational complexity of the Bayesian paradigm \citep{Neal1993Probabilistic,jordan1999introduction,minka2001expectation}.

Unquestionably, models for decision-making systems need to deal with \emph{uncertainty}. They need to be able to quantify what is known, and what is not known. The importance of quantifying uncertainty for decision-making systems becomes clear from the many sources it can stem from. For example, there can be multiple different settings of a model that explain the data, so one needs to be uncertain about the setting that actually generated it. One also needs to be uncertain about the model itself, as most probably the model at hand is a simplification of the real-world process. Moreover, the environment in which the system operates may also be inherently uncertain, in which case even an infinite amount of data (i.e. experience) would not make the system any smarter (e.g., trying to predict the outcome of rolling a fair dice).

Gaussian Processes (GPs) \citep{rasmussen2006} can be argued to provide the perfect compromise between computational complexity and Bayesian rigour. Their non-parametric nature makes them complex enough to model a wide range of problems, while their kernel formulation makes them applicable to many different domains: graphs, vectors, images, etc. Instead of representing probability distributions on weights, Gaussian processes can be used to represent uncertainty directly on the function that the weights represents. The \emph{function-space} view of Gaussian processes makes them more amenable to mathematical analysis, which in turn leads to strong guarantees on the future performance of the system and overall robustness. This may be necessary for deploying these systems in critical applications. For these reasons studying Gaussian processes is very worthwhile.

This reports presents two pieces of research in the domain of approximate Bayesian inference for GP models conducted during the first year of my PhD degree. Namely, \cref{chapter:vish} introduces an interdomain inducing variable approach that speeds up inference and prediction in GPs by two orders of magnitude by making use of the spectral properties of the kernel. In \cref{chapter:dnn-as-point-estimate-for-dgps} we marry the strengths of deep neural networks and deep GPs by establishing an equivalence between the forward passes of both models. This results in models that can either be seen as neural networks with improved uncertainty prediction or deep GPs with increased prediction accuracy. The final part of this report, \cref{chapter:future-research}, elaborates on a future research agenda. Prior to all of this, we start with covering the theoretical background in \cref{chapter:theoretical-framework}.

The material presented in \cref{chapter:vish,chapter:dnn-as-point-estimate-for-dgps} is either published or is currently under review:
\begin{enumerate}
    \item \fullcite{Dutordoir2020spherical}
    \item \fullcite{dutordoir2021deep}
    \item \fullcite{dutordoir2021gpflux}
\end{enumerate}


% !TEX root = ../main.tex
% chktex-file 21
\section{Introduction}%
\label{sec:intro}

\pagenumbering{arabic}			% arabic page numbering
\setcounter{page}{1}			% set page counter

Over the last few years Big Data processing has become increasingly important in many domains.
This increase in the data volume also poses new challenges for machine learning applications.
The training time of learners is usually polynomially dependent on the size of the training dataset \(\Dtrain\), i.~e. \(\mathcal{O}(|\Dtrain|^\alpha), \alpha \geq 1\).
Since training has to be repeated for every iteration of cross-validation and hyperparameter search, always using the entire dataset quickly becomes infeasible.
This paper gives an overview of approaches to tackle this problem with a focus on the domain of classification problems.

The process of finding a hypothesis can in general be split into three phases:
\begin{enumerate}
	\item \textbf{Model selection:}
		At first a model has to be selected that determines the class of hypothesis spaces out of which the final hypothesis will be selected.
		The choice of model is often implicitly encoded in the class of hypothesis spaces of a learner \(L\).
		Automating this step is non-trivial.
		In practice the model is typically selected by experts with domain specific knowledge about the problem at hand.
	\item \textbf{Hyperparameter search:}
		Optimizing a vector \(\lambda\) in the hyperparameter space \(\Lambda_L\) of the learner \(L\) representing a hypothesis space \(\mathcal{H}_{\lambda}\).
		A na{\"\i}ve approach to do this is to systematically try configurations using a grid search or a random search over \(\Lambda_L\).
		To evaluate the quality of a given \(\lambda\), \(L\) is usually trained on a training dataset \(\Dtrain\) using \(\lambda\). This yields a hypothesis \(\hat{h}_\lambda \in \mathcal{H}_{\lambda}\) that is evaluated using a validation dataset \(\Dvalid\).
		The goal of hyperparameter optimization is to minimize the loss \(l(\lambda)\) of \(\hat{h}_\lambda\) on \(\Dvalid\), i.~e.\@ to find an approximation \(\hat{\lambda}\) of \(\lambda^* := \arg\min_{\lambda}{l(\lambda)}\).
	\item \textbf{Training or parameter search:}
		Let \(w\) be a vector in the parameter space \(W_{\mathcal{H}_\lambda}\), describing a hypothesis \(h_{\lambda, w} \in \mathcal{H}_\lambda\) given a hyperparameter configuration \(\lambda\).
		The goal of parameter search is to find an approximation \(\hat{h}_\lambda\) of the hypothesis \(h^*_\lambda := \arg\min_{h_{\lambda, w}}{\ell(\Dtrain | h_{\lambda, w})}\), with \(\ell(\Dtrain | h_{\lambda, w})\) being the empirical loss of \(h_{\lambda, w}\) on a given training dataset \(\Dtrain\) according to some loss function \(\ell\).
		Depending on the learner \(L\), various kinds of optimization methods are used to find this minimum, e.~g.\@ Bayesian optimization, quadratic programming or, if \(\nabla_w e(\Dtrain | h_{\lambda, w})\) is computable, gradient descent.
		The quality \(l\) of \(\hat{h}_\lambda\) is measured by the loss on a validation or test dataset, i.~e.\@ \(l(\lambda) := \ell(\Dvalid | \hat{h}_\lambda)\).
\end{enumerate}
This paper is structured according to the last two phases, i.~e.\@ we will assume that the learner \(L\) is given.
Section~\ref{sec:hyperparams} describes ways to speed up the hyperparameter search.
Section~\ref{sec:params} then describes how to improve the training methods of existing learners.
Most of the techniques described in this paper improve upon independent components of the hypothesis finding process, allowing them to be combined.


\newcommand\lbxmonad{\texttt{xmonad}\xspace}

\NV{(R3)
 Regarding your XMonad experience, you might want to compare your approach to the work of Wouter Swierstra: "Xmonad in Coq (Experience Report)
}
\section{XMonad: From Testing to Static Verification}\label{sec:xmonad}

In this section we discuss our experience in the on-going endeavour of
static verification of \lbxmonad.
%
We start by summarizing the data structures used in \lbxmonad, as described in the documentation.
%
We continue by specifying and proving \lbxmonad's invariant, \ie, that
virtual workspaces are unique
%
Finally, we comment on proving \lbxmonad properties to conclude
that static verification can be not much harder than testing.

\subsection{The structure}

\lbxmonad is a dynamically tiling \texttt{X11}
window manager that is written and configured in Haskell.

The window (@a@) is a set of virtual workspaces.
On each workspace is a stack of windows.
A given workspace is always current, and a given
window on each workspace has focus.
The focused window on the current
workspace is the one which will take user input.
%
\lbxmonad uses a Zipper~\citep{zipper} data structure
to directly track focus on the virtual workspaces,
and calls this structure @Stack a@.

A workspace is just a @Stack@ of virtual workspaces
tagged with a tag @i@ and its layout @l@
@Workspace i l a@.

To view a workspace on a physical screen one needs to
associate the workspace with physical screen's id @sid@
and details @sd@,
forming the new data structure @Screen i l a sid sd@.

Xinerama in X11 allows viewing multiple virtual workspaces
simultaneously.
%
While only one the current one will ever be in focus (i.e. will
receive keyboard events), other workspaces may be passively
viewable.
%
We thus need to track which virtual workspaces are
associated (viewed) on which physical screens.
%
To keep track of
this, \lbxmonad's main data structure  @StackSet i l a sid sd@
keeps, apart from the current screen,
separate lists of visible but non-focused
workspaces (@Screen@) , and non-visible workspaces (@Workspace@).

\subsection{Uniqueness of windows}
We proved that each window @a@ appears once in the @StackSet i l a sid sd@
bottom-up:
We proved that each @Stack@ has unique elements
and that all the stacks that appear in @StackSet@ are disjoint.

\mypara{Unique Stack}
Assume the existence of a predicate
@ULNeq (X::a) (XS::[a])@ that ensures that
(1)~the list @XS@ has no duplicates and
(2)~@X@ is not an element of @XS@.
%
With that magical predicate we define an \emph{almost unique}
stack:
\begin{code}
data Stack a = Stack { focus :: a
                     , up    :: ULNEq a focus
                     , down  :: ULNEq a focus }
\end{code}
The stack has a @focus@ element @a@ and
and two unique lists of @a@'s @up@ and @down@ of the focus.

The above Stack is almost unique, as an element
may appear both in the @up@ and the @down@ lists.
%
We define a type alias for @U@nique@Stack@
that rejects with possibility:

\begin{code}
type UStack a = {v:(Stack a) |
  (LDisjoint (up v) (down v))}

predicate LDisjoint X Y =
  (Set_emp (Set_cap (listElts X) (listElts Y)))
\end{code}

where @listElts@ is a recursively defined measure that
returns the Set of elements of the input list,
that is build in \toolname.
\begin{code}
measure listElts  :: [a] -> (Set a)
   listElts([])   = {v | (Set_emp v) }
   listElts(x:xs) = {v | v =
     (Set_cup (Set_sng x) (listElts xs))}
\end{code}

Note that the above definitions crucially depend on set theoretic properties.
Thus, verification of \lbxmonad is achieved using an SMT back-end
that supports set theory (like Z3~\citep{z3}).

We slightly modify the above measure definition
to define @listDup@, a measure that returns the duplicate
elements of a list.
%
\begin{code}
measure listDup :: [a] -> (Set a)
 listDup([])   = {v | (Set_emp v)}
 listDup(x:xs) = {v | v =
   if (Set_mem x (listElts xs))
    then (Set_cup (Set_sng x) (listDup xs))
    else (listDup xs))                      }
\end{code}

Using @listDup@ we define the magical list type @ULNEq a N@
as a list @v@ that has no duplicates, \ie the set @listDup v@
is empty, and @N@ does not belong to the set @listElts v@.

\begin{code}
type ULNEq a N = {v:[a] | ( (UL v) && (not (LElt N v))}

predicate LElt N LS  = (Set_mem N (listElts LS))
predicate UL     LS  = (Set_emp (listDup LS)   )
\end{code}

Throughout verification
we need to establish and use the invariant
that each Stack is Unique.
%
This is achieved by the following annotation
\begin{code}
using (Stack a) as (UStack a)
\end{code}
that allows \toolname to
(1)~ use the invariant:
each time a Stack value is retrieved from the environment
\toolname strengthens its type with the disjointness information;
(2)~ prove the invariant:
each time @Stack@ data constructor is used,
an disjoint constraint should be proved.
Failure to prove this constraint will raise an
``Invariant Check'' error.

\mypara{Unique StackSet}
Establishing Uniqueness on StackSets is a generalization of the above procedure.

The definition of a @StackSet@ includes
the @current@ Screen,
the list of @visible@ screens,
and the list of @hidden@ workspaces.
\begin{code}
data StackSet i l a sid sd = StackSet
   { lcurrent  ::  Screen i l a sid sd
   , lvisible  :: [Screen i l a sid sd]
   , lhidden   :: [Workspace i l a]
   , lfloating :: M.Map a RationalRect
   }
\end{code}
%
\toolname automatically turns the record selectors of refined data types
to measures that return the appropriate fields.
%
Thus we infix the refined selectors with an @l@
to distinguish between the haskell (\eg, @current@)
and the logical (\eg, @lcurrent@) selectors.

To prove absence of duplicates we need to @use@
only @StackSet@s that have no duplicates:
\begin{code}
using (StackSet i l a sid sd)
 as  {v:StackSet i l a sid sd|(NoDuplicates v)}
\end{code}

@NoDuplicates@ ensures that the elements of
@hidden@, @current@, and @visible@
workspaces are mutually disjoint
and that the @visible@ and @hidden@ workspaces
have no duplicates.
%
\begin{code}
predicate NoDuplicates SS =
    (Disjoint3  (workspacesElts (lhidden  SS))
                (screenElts     (lcurrent SS))
                (screensElts    (lvisible SS)))
  &&
    (Set_emp (screensDups    (lvisible SS)))
  &&
    (Set_emp (workspacesDups (lhidden  SS)))
\end{code}
%
Again we used recursively defined measures to grap
the elements and the duplicates of the structures.
For example, @screenElts@ returns
the elements of the stack of the workspace of the screen,
and is used by @screensDup@ to grap the duplicates of
a list of Screens.

\mypara{Verification Procedure}
Using the above unique types make \toolname verify the absence of duplicates.
%
It was not surprising that when we first run \toolname
against these stronger types many type errors where created.
%
The
can be summarized as follows:
\begin{itemize}
	\item\emph{Strengthening library functions.}
      \lbxmonad repeatedly concatenates the list fields of a Stack.
      %
      To prove that for some `s::UStack a`, `(up s ++ down s)` is a unique list,
      the type of `(++)`
      needs to capture that concatenation of two unique and disjoint lists is a unique list.
      %
      For verification, we assumed that Prelude's `(++)` satisfies this property.
      %
      But, not all arguments of `(++)` are unique disjoint lists:
      @"StackSet" ++ "error"@ is a trivial example that does not satisfy
      the assumed preconditions of `(++)` thus creating a type error.
      %
      Sadly, \toolname does not currently support sum types,
      thus we used an unrefined `(++.)` variant of `(++)` for such cases.
%%
%%	Thus function @mapLayout@ seemed en auto-provable one,
%%	as it only modify the layout elements @l@
%%	which cannot affect the uniqueness of @a@ elements.
%%	%
%%\begin{code}
%%mapLayout :: (l -> l')
%%          -> StackSet i l a s sd
%%          -> StackSet i l' a s sd
%%\end{code}

	\item\emph{Restrict the functions' domain.}
	@modify@ is a @maybe@ like function
	that given a default value @x@,
	a function @f@ and a StackSet @s@,
	applies @f@ on the @Maybe (Stack a)@ values inside @s@.
	%
\begin{code}
modify ::
    x:{v:Maybe (UStack a) | (isNothing v)}
 -> f:(y:(UStack a)
     -> Maybe {v:UStack a) | (SubElts v y)})
 -> s:StackSet i l a s sd
 -> StackSet i l a s sd
\end{code}
	%
	Since inside the StackSet each @y:Maybe (Stack a)@
	could be replaced with either the default value @x@
	or @f y@ we need to ensure that both there alternatives
	will not insert duplicates.
	%
	This imposes the interesting precondition that the default
	value should be @Nothing@, which dramatically restricts
	@modify@'s domain.
	%
	Given this restriction, \toolname should verify
	that all user functions satisfy it.

	\item\emph{Code inlining}
    %
    Given a tag @i@ and a StackSet @s@,
    @view i s@ will set the current Screen
    to the screen with tag @i@,
    if such screen exists in @s@.
    %
    Below is the original definition for @view@
    in case when a screen with tag @i@ exists in
    visible screens
    %
\begin{code}
view :: (Eq s, Eq i) => i
    -> StackSet i l a s sd -> StackSet i l a s sd
view i s
  | Just x <- L.find ((i==).tag.workspace)
                     (visible s)
  = s { current = x
      , visible = current s :
          L.deleteBy (equating screen) x
                     (visible s)}
\end{code}
    %
    Verification of this code is difficult
    as the properties of all intermediate values
    are too complicated to be expressed.
    %
    Instead we replaces this code with a call to a
    recursive function @raiseIfVisible i s@
    that in-place replaces @x@ with the current screen.

\end{itemize}

\subsection{QuickCheck Properties}

\lbxmonad is tested against $113$ \texttt{quickcheck} properties.
%
Of those $15$ check the uniqueness invariant
and the rest $113$ check various functional properties.
%
We started the endeavour of verifying these properties with \toolname.
%
We looked at a sample of $15$ properties to conclude that
which we categorized as follows:
\begin{itemize}
\item\emph{Easy to be proved.}
Consider the \texttt{quickcheck} property that checks that @view@ing
a @StackSet a@ is idempotent:
\begin{code}
prop_view_idem (x :: T) (i :: NonNegative Int)
  = i `tagMember` x ==> view i (view i x) == (view i x)
\end{code}
%
The above property directly translated to a haskell function
\begin{code}
type Valid     = {v:Bool | (Prop v) }

prop_view_idem :: StackSet i l a sid sd -> i -> Valid
prop_view_idem x i
  | i `tagMember` x = view i (view i) == v
  | otherwise       = True
\end{code}
%
By the above type signature,
\ie by the result type @Valid@,
we specify that the function should always returns True.
%
When typechecking the above function,
\toolname proves that the property holds.
%
\toolname is able to verify this property as the result type of @view@
is strengthens with a refinement
(in this case @EqTag x i => x = v@)
that directly implies this property.

The above is generalizing to (10/17) properties that we checked:
strengthening the function types by refinements that \toolname can prove
is sufficient to verify these properties.

\item\emph{Can be estimated.}
In some other properties (like checking that @view@ing is reversible),
two StackSets (@s1@ and @s2@) were normalized before being compared,
that is their elements were first sorted.
%
In our logic we do not support any operation that can normalize structures in such a way.
%
Thus we cannot prove this exact property.
%
Instead we approximated it, by proving that proving that @s1@ and @s2@
have the same sets of elements.
%
We approximated (3/17) of the properties.

\item\emph{Their proof cannot be supported, currently.} [1]
One \texttt{quickcheck} property checks
that @i@ cannot belong to an empty stackset.
%
We used abstract refinements to encode empty stacksets.
%
Proving the above property would be easy
if we could mix abstract and concrete refinements in logical formulas
or if \toolname supported sum types.
%
Both the alternatives constitutes features that
we would like to extend \toolname with in the near future.
%
Still, currently we are not able to prove such kind of properties.
\item\emph{Cannot be expressed}
Other properties %, like @prop_focus_left_master@
check that the order of the windows is not affected by certain operations.
%
Though not infeasible we acknowledge that \toolname
is not appropriate for reasoning about order preserving
and verification of such properties
would require many code modifications.
%
(3/17) are order preserving properties.
\end{itemize}


% arara: pdflatex
% arara: bibtex
% arara: pdflatex
\documentclass[letterpaper]{article}
\usepackage{graphicx}
\usepackage{amsfonts,amsmath,amssymb}
\usepackage{natbib}
\usepackage{url}
\usepackage{hyperref}
\hypersetup{colorlinks=false,pdfborder={0 0 0}}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

%\frenchspacing
\setlength{\pdfpagewidth}{8.5in}
\setlength{\pdfpageheight}{11in}

%\setcounter{secnumdepth}{0}

\newcommand{\VT}{\ensuremath{V_\mathrm{T}}}
\newcommand{\VTO}{\ensuremath{V_\mathrm{T0}}}
\newcommand{\VTm}{\ensuremath{V_\mathrm{T}^-}}
\newcommand{\VTp}{\ensuremath{V_\mathrm{T}^+}}
\newcommand{\VDD}{\ensuremath{V_\mathrm{DD}}}
\newcommand{\Vth}{\ensuremath{V_\mathrm{th}}}
\newcommand{\VGS}{\ensuremath{V_\mathrm{GS}}}
\newcommand{\VGSi}{\ensuremath{V_\mathrm{GSi}}}
\newcommand{\VDS}{\ensuremath{V_\mathrm{DS}}}
\newcommand{\VDSi}{\ensuremath{V_\mathrm{DSi}}}
\newcommand{\VMIT}{\ensuremath{V_\mathrm{MIT}}}
\newcommand{\VIMT}{\ensuremath{V_\mathrm{IMT}}}
\newcommand{\Vmet}{\ensuremath{V_\mathrm{met}}}
\newcommand{\vmet}{\ensuremath{v_\mathrm{met}}}

\newcommand{\Cinv}{\ensuremath{C_\mathrm{inv}}}

\newcommand{\ID}{\ensuremath{I_\mathrm{D}}}
\newcommand{\IOFF }{\ensuremath{I_\mathrm{OFF}}}
\newcommand{\ION}{\ensuremath{I_\mathrm{ON}}}
\newcommand{\IMIT}{\ensuremath{I_\mathrm{MIT}}}
\newcommand{\IIMT}{\ensuremath{I_\mathrm{IMT}}}

\newcommand{\Rins}{\ensuremath{R_\mathrm{ins}}}
\newcommand{\Rmet}{\ensuremath{R_\mathrm{met}}}
\newcommand{\Rinsp}{\ensuremath{R_\mathrm{ins}'}}
\newcommand{\Rmetp}{\ensuremath{R_\mathrm{met}'}}

\newcommand{\pins}{\ensuremath{\rho_\mathrm{ins}}}
\newcommand{\pmet}{\ensuremath{\rho_\mathrm{met}}}
\newcommand{\pinsp}{\ensuremath{\rho_\mathrm{ins}'}}
\newcommand{\pmetp}{\ensuremath{\rho_\mathrm{met}'}}

\begin{document}

\title{The Design of HyperFETs}
\author{Sam Bader, Debdeep Jena}
\author{Sam Bader$^1$, Debdeep Jena$^{2,3}$\\ $^1$Cornell University, Applied and Engineering Physics\\$^2$Cornell University, Electrical and Computer Engineering\\$^3$Cornell University, Materials Science and Engineering }

\maketitle

\section{Model}
\subsection{Transistor}
The transistor is modeled generically by a heavily simplified virtual-source (short-channel) MOSFET model \cite{Khakifirooz_2009}.  Although this model was first defined for Silicon transistors, it has been successfully adapted to numerous other contexts, including Graphene \cite{Han_Wang_2011} and Gallium Nitride devices, both HEMTs \cite{RadhakrishnaThesis} and MOSHEMT+VO$_2$ HyperFETs \cite{Verma_2017}.  Following Khakifirooz \cite{Khakifirooz_2009}, the drain current $\ID$ is expressed
\begin{equation}
\frac{\ID}{W}=Q_{ix_0}v_{x_0}F_s
\end{equation}
where $Q_{iz_0}$ is the charge at the virtual source point, $v_{x_0}$ is the virtual source saturation velocity, and $F_s$ is an empirically fitted ``saturation function'' which smoothly transitions between linear ($F_s\propto V_{DS}/V_{DSSAT}$) and saturation ($F_s\approx 1$) regimes.  The charge in the channel is described via the following semi-empirical form first proposed for CMOS-VLSI modeling \cite{Wright_1985} and employed frequently since (often with modifications, eg \cite{Khakifirooz_2009, RadhakrishnaThesis}):
\begin{equation}
Q_{ix_0}=C_\mathrm{inv}nV_\mathrm{th}\ln\left[1+\exp\left\{\frac{\VGSi-\VT}{nV_\mathrm{th}}\right\}\right]
\end{equation}
where $C_\mathrm{inv}$ is an effective inversion capacitance for the gate, $n\Vth \ln10$ is the subthreshold swing of the transistor, $\VGS$ is the transistor gate-to-source voltage, $\VT$ is the threshold voltage, and $V_\mathrm{th}$ is the thermal voltage $kT/q$.

For precise modeling, Khakifirooz includes further adjustments of $\VT$ due to the drain voltage (DIBL parameter $\delta$) and the gate voltage (strong vs weak inversion shift),
\begin{equation}
  \VT=\VTO-\delta \VDSi-\alpha \Vth F_f(\VGSi)
  \label{eq:DIBL}
\end{equation}
where $\delta$ is the DIBL parameter, and $F_f$ is a smoothing function which goes from 1 below \VT\ to 0 above \VT.  We will not employ any specific form of $F_f$ for analytic work.  Futher, we will  assume the supply voltage is maintained above the gate overdrive such that $F_s\approx 1$. However, we will add on a leakage floor with conductance $G_\mathrm{leak}$.  Altogether, the final current expression (for the analytical part of this analysis) is
\begin{equation}
\frac{\ID}{W}=nv_{x_0}C_\mathrm{inv}\Vth \ln\left[1+\exp\left\{\frac{\VGSi-\VT}{n\Vth }\right\}\right]+\frac{G_\mathrm{leak}}{W}V_\mathrm{DSi}\label{eq:transistor_iv}
\end{equation}
And we will often use the shorthands
\begin{equation}
  \VTm=\VTO-\delta\VDS-\alpha\Vth, \quad
  \VTp=\VTO-\delta\VDS, \quad
  k=v_{x_0}C_\mathrm{inv}W
  \label{eq:short}
\end{equation}
for the deep-subthreshold threshold voltage at $\VDSi=\VDD$, the inversion threshold voltage at $\VDSi=\VDD$, and the inversion transconductivity respectively.
\subsection{Phase-change resistor}
\label{ss:PCR}
The phase-change material is included by a similarly generic and brutally simple model.  As done with the transistor, the goal is to capture only the most relevant feature: here, an abrupt change in resistance.  However, for a concrete example, the material most frequently used in HyperFET research \cite{Pergament_2013,Shukla_2015} is Vanadium Dioxide (VO$_2$), which features an S-style (ie current-controlled) and hysteretic negative differential resistance (NDR) region \cite{Pergament_arxiv2016,Zimmers_2013} due to an insulator-metal transition (IMT), the underlying mechanism of which has been a source of long-running controversy \cite{Pergament_2013}.  Though the literature contains numerous examples of voltage-swept I-V curves \cite{Shukla_2015,Zimmers_2013,Radu_2015,Yoon_2014}, proper modeling of a current-controlled NDR device in a circuit requires a current-swept I-V, examples of which can be found in \cite{Zimmers_2013,Kumar_2013,Pergament_arxiv2016} and schematically in the supplementary materials of \cite{Shukla_2015}.  The cleanest of these is Figure 1(b) of Kumar \cite{Kumar_2013}, which is suggested to the reader as a concrete realization of the model used herein.

The phase-change resistor (PCR) will be described by a piecewise-linear model:
\begin{equation}
V_R=\left\{\begin{array}{llr}
I_R\Rins &, & I_R < \IIMT \\
V_\mathrm{met}+I_R\Rmet &, & I_R > \IMIT \\
\end{array}\right\}
\label{eq:PCR_iv}
\end{equation}
Note that $\IMIT <\IIMT $ (as in \cite{Kumar_2013}) implies hysteresis in the PCR itself, while $\IMIT >\IIMT $ implies a disallowed current range and can lead to oscillation as discussed in the supplementary materials of \cite{Shukla_2015}.  For convenience, we define voltage thresholds, $V_\mathrm{IMT}=\IIMT \Rins $ and $V_\mathrm{MIT}=\IMIT \Rmet +V_\mathrm{met}$.  Finally, we require $V_\mathrm{met}+\IIMT \Rmet <V_\mathrm{IMT}$ and $\IMIT \Rins >V_\mathrm{MIT}$ to ensure that the absolute resistance of the metallic state is lower than that of the insulating state wherever they are both defined.

\section{HyperFET Regimes}
When the PCR is attached in series with the source of the transistor, the total device satisfies the above equations with the additional matching $I=\ID=I_R$ and $\VGS=V_{GS}-V_R$ where $I$ is the current through the device and $V_{GS}$ is the voltage between HyperFET gate (the transistor gate) and the HyperFET source (the exterior node of the resistor).  We can immediately solve for several regions of the HyperFET model.  For this section, it is assumed that the transistor and PCR are scaled such that the left end of the hysteretic region and the lower branch are entirely contained within subthreshold, and above the leakage floor; these choices will be discussed in the next section.

\subsection{Leakage floor}
When the transistor is completely off, only the leakage term of \eqref{eq:transistor_iv} remains, and combines with the PCR off-state resistance, leading to
\begin{equation}
I=G_\mathrm{off}V_{DS},\quad G_\mathrm{off}^{-1}=\Rins +1/G_\mathrm{leak}
\end{equation}
\subsection{Insulating (lower) branch of hysteretic region}
For the lower branch (in the region above the leakage floor), we plug $\VGSi=V_\mathrm{GS}-I\Rins $ and $V_\mathrm{DSi}=V_\mathrm{DS}-I\Rins $ into the transistor I-V \eqref{eq:transistor_iv}, and take the subthreshold limit: $\ln(1+e^x)\approx e^x$ for $-x \gg 1$.
\begin{equation}
  \frac{I}{W}=nC_\mathrm{inv}v_{x_0}\Vth\exp\left\{\frac{V_\mathrm{GS}-I\Rinsp -\VTm}{n\Vth }\right\} + \frac{G_\mathrm{leak}}{W}(V_\mathrm{DS}-I\Rins )
\label{eq:insbranch_preW}
\end{equation}
where $\Rinsp=(1+\delta)\Rins$.
This can be rearranged and solved in terms of the Lambert $\mathcal{W}$ function
\begin{equation}
  I=\frac{n\Vth }{\Rinsp }\mathcal{W}\left[\frac{k\Rins' }{1+G_\mathrm{leak}\Rins }\exp\left\{\frac{V_\mathrm{GS}-\VTm-\frac{G_\mathrm{leak} V_\mathrm{DS}\Rins }{(1+G_\mathrm{leak}\Rins )W}}{n\Vth}\right\}\right]+\frac{G_\mathrm{leak}V_\mathrm{DS}}{1+G_\mathrm{leak}\Rins }
\label{eq:insbranch_wleak}
\end{equation}
If $I$ is well above the leakage floor, this reduces to
\begin{equation}
I=\frac{n\Vth }{\Rins'}\mathcal{W}\left[k\Rins'\exp\left\{\frac{V_\mathrm{GS}-\VTm}{n\Vth}\right\}\right]
\label{eq:insbranch}
\end{equation}
\subsection{Metallic (upper) branch of the hysteretic region}
For the upper branch (in deep subthreshold), we follow the same procedure to find
\begin{equation}
  I=\frac{n\Vth }{\Rmetp}\mathcal{W}\left[k\Rmetp \exp\left\{\frac{V_\mathrm{GS}-\VTm-\Vmet}{n\Vth }\right\}\right]
\label{eq:metbranch}
\end{equation}
where $\Rmetp=(1+\delta)\Rmet$.
%Note that if the metal-state resistance is small and we are in subthreshold $I\Rmet \ll n\Vth $, we approximate
%\begin{equation}
%\frac{I}{W}\approx n\Vth C_\mathrm{inv}v_{x_0}\exp\left\{\frac{V_\mathrm{GS}-V_\mathrm{met}-\VT}{n\Vth }\right\}
%\label{eq:met_smallR}
%\end{equation}
\subsection{Strong inversion}
Again we plug $\VGSi=V_\mathrm{GS}-V_\mathrm{met}-I\Rmet $ into the transistor I-V \eqref{eq:transistor_iv}, but this time, we take the strong inversion limit $\ln(1+e^x)\approx x$ for $x\gg 1$.
\begin{equation}
  \frac{I}{W}=v_{x_0}C_\mathrm{inv}(V_\mathrm{GS}-V_\mathrm{met}-I\Rmetp -\VTp)
  \label{eq:Isat_pre}
\end{equation}
which gives
\begin{equation}
  I=\frac{k}{1+k\Rmetp}(V_\mathrm{GS}-V_\mathrm{met}-\VTp)
  \label{eq:Isat}
\end{equation}

\subsection{Voltage boundaries of the hysteretic region}
The leftmost point of the upper branch is defined by the minimum current below which no metallic-state solution can exist: $I=\IMIT $.  By \eqref{eq:PCR_iv}, $V_\mathrm{R}=V_\mathrm{MIT}$.  Plugging this point into \eqref{eq:transistor_iv} and solving yields
\begin{equation}
V_\mathrm{left}-\VT=V_\mathrm{MIT}+n\Vth \ln\left[\exp\left\{\frac{\IMIT }{nk\Vth }\right\}-1\right]
\end{equation}
We have not yet made an assumption of subthreshold in the above equation, but if we do, we arrive at
\begin{equation}
V_\mathrm{left}-\VTm\approx (1+\delta)V_\mathrm{MIT}-n\Vth \ln\left[\frac{nk\Vth }{\IMIT }\right]
\label{eq:Vleft_sub}
\end{equation}
Since $V_\mathrm{left}$ may or may not be near $\VT$ depending on the parameters of the devices in question, we will take one self-consistent adjustment to account for strong/weak inversion shift
\begin{equation}
  V_\mathrm{left}\rightarrow V_\mathrm{left}+\alpha\Vth \left[1-F_f\right]_{\VDSi=\VDD-\VMIT, \VGSi=V_\mathrm{left}-\VMIT}
\label{eq:Vleft_sub2}
\end{equation}
If $V_\mathrm{left}$ is far below $\VT$, then this does nothing, but as $V_\mathrm{left}$ approaches threshold, this shifts the left side rightward on the order of $\alpha\Vth$.

The rightmost point of the upper branch is defined by the maximum current beyond which no insulating state solution can exist: $I=\IIMT $.  By definition then, $V_\mathrm{R}=V_\mathrm{IMT}$.  Plugging this point in
\begin{equation}
V_\mathrm{right}-\VT=V_\mathrm{IMT}+n\Vth \ln\left[\exp\left\{\frac{\IIMT }{nk\Vth }\right\}-1\right]
\end{equation}
where again, we have delayed the assumption of subthreshold until this point:
\begin{equation}
V_\mathrm{right}-\VTm\approx (1+\delta)V_\mathrm{IMT}-n\Vth \ln\left[\frac{nk\Vth }{\IIMT }\right]
\label{eq:Vright_sub}
\end{equation}
Note that $V_\mathrm{right}$ depends only on the properties of the insulating branch, so even if the top right corner of the hysteresis loop is in strong inversion, this expression may still be entirely valid because the lower branch is likely in subthreshold.
Equations \ref{eq:Vleft_sub} and \eqref{eq:Vright_sub} can be combined into a simple form (ignoring the $\alpha$ shift):
\begin{equation}
  V_\mathrm{hyst}=V_\mathrm{right}-V_\mathrm{left}=(1+\delta)(V_\mathrm{IMT}-V_\mathrm{MIT})+n\Vth \log\frac{\IIMT }{\IMIT }
  \label{eq:Vhyst_sub}
\end{equation}
Assuming the voltage scale of the PCR is sufficiently larger than thermal voltage, this reduces to
\begin{equation}
  V_\mathrm{hyst}\approx (1+\delta)(V_\mathrm{IMT}-V_\mathrm{MIT})
  \label{eq:Vhyst_simple}
\end{equation}
Note that it is formally possible for \eqref{eq:Vhyst_sub} to become negative, ie $V_\mathrm{left}>V_\mathrm{right}$. Examining the definitions of $V_\mathrm{left}$ and $V_\mathrm{right}$, this implies that, in such circumstances, the range between the two will contain no valid solution, ie it will be unstable (oscillatory) rather than bistable (hysteretic).  See the supplementary materials of \cite{Shukla_2015} for a discussion of this behavior in the context of resistors only.

\subsection{Current boundaries of the hysteresis}
The current at the left boundary of the metallic branch is at $I_\mathrm{left, met}=\IMIT $.  Now plugging  the subthreshold expression for the left boundary \eqref{eq:Vleft_sub} into the expression for the insulating branch \eqref{eq:insbranch}, we can get an expression for the current at the same point on the insulating branch:
\begin{equation}
I_\mathrm{left,ins}=\frac{n\Vth }{\Rinsp}\mathcal{W}\left[\frac{\IMIT \Rinsp}{n\Vth }\exp\left\{\frac{V_\mathrm{MIT}}{n\Vth }\right\}\right]
\label{eq:Ileftins}
\end{equation}
And note that the inequality $\IMIT \Rins >V_\mathrm{MIT}$ we demanded in Sec \ref{ss:PCR} ensures that the current on the insulating branch at the boundary is strictly lower, so there will be a discontinuous jump:
\begin{equation}
I_\mathrm{left, ins}<\frac{n\Vth }{\Rinsp}\mathcal{W}\left[\frac{\IMIT \Rinsp}{n\Vth }\exp\left\{\frac{\IMIT \Rinsp }{n\Vth }\right\}\right]=\IMIT
  \label{eq:leftjump}
\end{equation}
The current at the right boundary of the insulating branch is $I_\mathrm{right, ins}=\IIMT $.  Now plugging the the subthreshold expression for the right boundary \eqref{eq:Vright_sub} into the expression of the metallic branch \eqref{eq:metbranch}, we can get an expression for the current at the same point on the metallic branch:
\begin{equation}
  I_\mathrm{right,met}=\frac{n\Vth }{\Rmetp}\mathcal{W}\left[\frac{\IIMT \Rmetp}{n\Vth }\exp\left\{\frac{V_\mathrm{IMT}-V_\mathrm{met}}{n\Vth }\right\}\right]
\label{eq:Irightmet}
\end{equation}
And note that the inequality $V_\mathrm{met}+\IIMT\Rmet <V_\mathrm{IMT}$ we demanded in Sec \ref{ss:PCR} ensures that the current on the metallic branch at the boundary is strictly higher, so there will be a discontinuous jump:
  (NOTE this paragraph is technically not true any more; once I include DIBL, the requirements are slightly higher\dots I need that to hold for $\Rmetp$, not just $\Rmet$, but small difference\dots)
\begin{equation}
  I_\mathrm{right,met}>\frac{n\Vth }{\Rmet }\mathcal{W}\left[\frac{\IIMT \Rmet }{n\Vth }\exp\left\{\frac{\IIMT \Rmet }{n\Vth }\right\}\right]=\IIMT
  \label{eq:rightjump}
\end{equation}
%\section{Super-Boltzmann behavior}
%As evidenced by \eqref{eq:leftjump} and \eqref{eq:rightjump}, there are two points of discontinuity.  A $V_\mathrm{GS}$ sweep from OFF to ON may then move continuously along the insulating branch, but then will have to jump up to the metallic branch at the right end of the hysteresis.  Conversely, a $V_\mathrm{GS}$ sweep from ON to OFF may move continuously along the metallic branch, but then will have to jump down to the insulating branch at the left end of the hysteresis.  At these two localized points, the current changes a finite amount over an infinitesimally small voltage difference, surpassing the drift-diffusion (``Boltzmann'') limit of $\frac{dV}{d \log I} >\frac{kT}{q}$
%How do these local violations of the Boltzmann limit at the hysteretic boundaries connect to the useful, global steepness of the I-V curve, as measured outside hysteresis?
%
%\subsection{Immediate steepness}
%Immediately to the left of the hysteresis, the current is $I_\mathrm{left,ins}$, and immediately to the right, the current is $I_\mathrm{right, met}$, which, from expressions \eqref{eq:Ileftins}, \eqref{eq:Irightmet} gives a log-ratio of currents
%
%
%\begin{equation}
%  \log{\frac{I_\mathrm{right}}{I_\mathrm{left}}}=\log\frac{\Rins }{\Rmet }+\log\frac{\mathcal{W}\left[\frac{\IIMT \Rmet }{n\Vth }\exp\left\{\frac{V_\mathrm{IMT}-V_\mathrm{met}}{n\Vth }\right\}\right]}{\mathcal{W}\left[\frac{\IMIT \Rins }{n\Vth }\exp\left\{\frac{V_\mathrm{MIT}}{n\Vth }\right\}\right]}
%\label{eq:logI_sub}
%\end{equation}
%
%Inverting the $\mathcal{W}$'s in \eqref{eq:Ileftins}, \eqref{eq:Irightmet} allows us to rewrite this in a suggestive form:
%\begin{align}
%  V_\mathrm{hyst}&=n\Vth \ln\left[ \frac{I_\mathrm{right}}{I_\mathrm{left}} \right]-I_\mathrm{left}\Rins +I_\mathrm{right}\Rmet +V_\mathrm{met}\\
%  &=n\Vth \ln\left[ \frac{I_\mathrm{right}}{I_\mathrm{left}} \right]-V_\mathrm{PCR, left}+V_\mathrm{PCR, right}
%  \label{}
%\end{align}
%where $V_\mathrm{PCR}$ indicates the voltage across the PCR at the left or right hysteresis boundary.  The first term is the intrinsic swing of the transistor.  So the condition for the PCR improving the transistor swing is that more voltage is dropped on the PCR in the insulating state at the left of the hysteresis than dropped there in the metallic state at the right side.
%
%Put another way, we can define a unitless ``loaded subthreshold steepness''
%\begin{equation}
%  \Sigma_n=\Vth \frac{\log{\frac{I_\mathrm{right}}{I_\mathrm{left}}}}{V_\mathrm{hyst}}
%  \label{eq:sigman}
%\end{equation}
%The PCR improves the subthreshold swing of the transistor (in the immediate vicinity of the hysteresis) if $\Sigma_n> 1/n$, and enables super-Boltzmann switching if $\Sigma_n>1$.
%
%Furthermore, we note that \eqref{eq:logI_sub} and \eqref{eq:Vhyst_sub} are independent of all the transistor properties except $n$ (because we've assumed the hysteresis occurs within subthreshold).  So setting $n=1$, we can consider $\Sigma_1$, the ``unloaded steepness'', as a transistor-independent figure of merit for a PCR which expresses its ability to enable super-Boltzmann switching in a HyperFET configuration.
%\subsection{Adjacent steepness}
%Taking the log and differentiating both sides of Eq \eqref{eq:insbranch_preW}, we find the slope of the insulating branch:
%\begin{equation}
%  \left.\frac{d\log I}{dV_\mathrm{GS}}\right|_\mathrm{ins}=\frac{1}{nV_\mathrm{th}}\frac{1}{1+(I\Rins /n\Vth )}
%  \label{eq:steep_ins}
%\end{equation}
%Similarly, the slope of the metallic branch is
%\begin{equation}
%  \left.\frac{d\log I}{dV_\mathrm{GS}}\right|_\mathrm{met}=\frac{1}{nV_\mathrm{th}}\frac{1}{1+(I\Rmet /n\Vth )}
%  \label{eq:steep_met}
%\end{equation}
%Note that the quantity in parentheses in \eqref{eq:steep_ins} is $V_\mathrm{PCR}$ and the same in \eqref{eq:steep_met} is $V_\mathrm{PCR}-V_\mathrm{met}$.  As discussed in the previous section, for a useful PCR, $V_\mathrm{PCR, right}<V_\mathrm{PCR, left}$, so it's reasonable to expect, for an optimal PCR design, that the region immediately to the right of the hysteresis will be steeper than that to the left.
%
%
\section{Shifted comparison}
The addition of a PCR reduces both the on- and off- current of a transistor, so in demonstrating the premise of the HyperFET, Shukla et al \cite{Shukla_2015} use a procedure of shifting the threshold voltage of a HyperFET to re-equalize the off-currents.  Since the off-current will be exponentially suppresed by a shift while the on-current (in inversion) will change linearly, this can, under reasonable circumstances, result in a large increase in the on-current at constant off-current.  The entire procedure can be performed analytically within this model, shedding light on precisely when a PCR is able to improve a transistor.

\subsection{The shift}
To this end, we imagine a transistor $\mathcal{T}$ with threshold voltage $\VTO$ is reengineered to an identical transistor $\mathcal{T'}$ with threshold $\VTO'=\VTO-\Delta \VTO$, and then combined with a PCR to form a HyperFET with the same off-current $\IOFF $ as the original transistor.  This condition is expressed by
\begin{align}
  \IOFF &=\frac{n V_\mathrm{th}}{\Rinsp}\mathcal{W}\left[ k\Rinsp\exp\left\{ \frac{-V_\mathrm{T}'^{-}}{n\Vth} \right\} \right]\\
  &=\frac{n V_\mathrm{th}}{\Rinsp}\mathcal{W}\left[ \frac{\IOFF \Rinsp}{n\Vth}\exp\left\{ \frac{\Delta \VTO}{n\Vth} \right\} \right]
  \label{}
\end{align}
where the first equality follows from the HyperFET current equations \eqref{eq:insbranch} and the second follows from plugging in the normal transistor current equation \eqref{eq:transistor_iv} in the subthreshold limit.  This is easily inverted to yield
\begin{equation}
  \Delta \VTO=\IOFF \Rinsp
  \label{eq:shift}
\end{equation}
\subsection{Shifting gain}
This shift increases the on-current at constant $V_\mathrm{DD}$.  Plugging \eqref{eq:shift} into the strong inversion expression, we find the new HyperFET on-current
\begin{equation}
  \ION'=\frac{k}{1+k\Rmetp }\left( V_\mathrm{DD}-\VTp+\IOFF \Rinsp -V_\mathrm{met} \right)
  \label{eq:shiftedon}
\end{equation}
The ratio of the HyperFET on-current to the original transistor on-current, ie the ``shifting gain'', is then
\begin{equation}
  \frac{\ION'}{\ION}=\frac{1+\left( \IOFF \Rinsp -V_\mathrm{met}\right)/(V_\mathrm{DD}-\VTp )}{1+k\Rmetp }
  \label{eq:Ion_rat}
\end{equation}
So we find that the PCR enables an increased on-current if
\begin{equation}
  \IOFF \Rinsp -V_\mathrm{met} >\ION\Rmetp
  \label{eq:inconcond}
\end{equation}
Namely, the voltage across the PCR in ON-state is smaller than that in the OFF state.  Setting \Vmet\ to zero for a moment, this condition becomes the simple statement that the insulator/metal resitance ratio of the PCR must be larger than the desired HyperFET \ION/\IOFF ratio in order to be useful.  For example, HyperFETs have been demonstrated in conjunction with GaN HEMTs, but despite that GaN HEMTs can easily achieve a dozen orders of ON-OFF ratio, making a HyperFET from one will necessarily involve limiting the ON-OFF to the PCR resistivity ratio (at most five orders).

(Note: The above discussion does assume that $\IOFF <I_\mathrm{left}$.)
\subsection{Shifting supply reduction}
Whereas the above subsection held $\IOFF $ and $V_\mathrm{DD}$ constant and noted the increase in $\ION$, one could alternatively hold $\IOFF $ and $\ION$ constant, and allow $V_\mathrm{DD}$ to change.  Comparing \eqref{eq:shiftedon} to to the strong inversion expression, we find that holding $\ION$ constant requires the supply voltage to change to
\begin{equation}
  V_\mathrm{DD}'-\VTp=(V_\mathrm{DD}-\VTp)(1+k\Rmetp)+V_\mathrm{met}-\IOFF \Rinsp
  \label{eq:shiftedVDD}
\end{equation}
ie
\begin{equation}
  \Delta V_\mathrm{DD}'=(V_\mathrm{DD}-\VTp)(k\Rmetp)+V_\mathrm{met}-\IOFF \Rinsp
  \label{eq:VDDshift}
\end{equation}
The conditions for this shift to be useful (ie negative) are the same as in the previous subsection.

\section{Optimization}
Taking the expression for the shifting gain \eqref{eq:Ion_rat}, we can refine the region in the PCR parameter space best suited to a HyperFET.  We will assume that the various current parameters scale proportional to the PCR cross-sectional area $wt$ (ie $I_\mathrm{IMT, MIT}= w t J_\mathrm{IMT, MIT})$, and the various voltages scale proportional to the PCR length $l$ (ie $V_\mathrm{IMT}=lJ_\mathrm{IMT}\pins$, $V_\mathrm{met}=lv_\mathrm{met}$, $V_\mathrm{MIT}=lJ_\mathrm{MIT}\pmet+l\vmet$).

With these definitions, we now consider the shifted gain as a function of $l$; it takes the form $(A_1+B_1l)/(C_1+D_1l)$.  Similarly, we could consider the shifted gain as a function of $\frac{1}{wt}$, and we find it takes the form $(A_2+\frac{B_2}{wt})/(C_2+\frac{D_2}{wt})$.  The derivative $\frac{d}{dx}\left[(A+Bx)/(C+Dx)\right]=(BC-DA)/(C+Dx)^2$ is monotonic with sign given by $BC-DA$.  In the cases of $l$ and $wt$, these signs are
\begin{equation}
  \frac{\partial \ION'}{\partial l}\propto\IOFF\Rinsp-\ION\Rmetp-\Vmet
  \label{eq:lderiv}
\end{equation}
\begin{equation}
  \frac{\partial \ION'}{\partial \frac{1}{wt}}\propto\IOFF\Rinsp-\ION\Rmetp-\Vmet+(1+k\Rmetp)\Vmet
  \label{eq:1wtderiv}
\end{equation}
By \eqref{eq:inconcond} and \eqref{eq:lderiv}, we know that, if we are in a regime where the HyperFET is useful, then we can always improve it \ION\ further by increasing $l$.  By \eqref{eq:inconcond} and \eqref{eq:1wtderiv}, we see that shrinking $wt$ will always improve \ION, unless \vmet\ is signficantly large and negative, in which case increasing $wt$ will always improve \ION.

Since $l$ should always be larger and $wt$ should always be smaller, it's clear that the optimal parameters will always lie along a problem boundary set by some other constraint.  The most obvious constraint is that the boundaries of the HyperFET transfer curve at $\VGS=0$ and $\VGS=\VDD$ should be hysteresis-free for a practical logic device.  In this regard, increasing $l$ increases the right voltage boundary of the hysteresis, and decreasing $wt$ lowers the left current of the hysteresis.  So the trade-off is, broadly, larger hysteresis for the larger \ION.  Given this, it is reasonable to estimate the scale of the optimal $l$ and $wt$ by the extremizing until the hysteresis nears the left and right limits of device operation.

The left current boundary \eqref{eq:Ileftins} can be reexpressed
\begin{equation}
  I_\mathrm{left, ins}=\frac{wtn\Vth}{l\pinsp}\mathcal{W}\left[ \frac{lJ_\mathrm{MIT}\pinsp}{n\Vth}\exp\left\{ \frac{lJ_\mathrm{MIT}\pmet+l\vmet}{n\Vth} \right\} \right]
  \label{eq:Ill_geom}
\end{equation}
so the condition of maximal hysteresis gives
\begin{equation}
  \frac{1}{wt}=\frac{n\Vth}{l\pinsp\IOFF}\mathcal{W}\left[ \frac{lJ_\mathrm{MIT}\pinsp}{n\Vth}\exp\left\{ \frac{lJ_\mathrm{MIT}\pmet+l\vmet}{n\Vth} \right\} \right]
  \label{eq:Ill_geom_ext}
\end{equation}
and the right boundary (after being shifted) can be expressed
\begin{equation}
  V_\mathrm{right}-\VTm=J_\mathrm{IMT}\pinsp l-n\Vth \ln\left[ \frac{nk\Vth}{wtJ_\mathrm{IMT}} \right]-\IOFF \frac{l\pinsp }{wt}
  \label{eq:Vright_geom}
\end{equation}
so the condition of maximal hysteresis gives
\begin{equation}
  V_\mathrm{DD}-\VTm=J_\mathrm{IMT}\pinsp l-n\Vth \ln\left[ \frac{nk\Vth}{wtJ_\mathrm{IMT}} \right]-\IOFF \frac{l\pinsp }{wt}
  \label{eq:Vright_geom_ext}
\end{equation}
Plugging in \eqref{eq:Ill_geom} and making use of the identity $\ln\mathcal{W}(z)=\ln z- \mathcal{W}(z)$ results in a cancelation of both the Lambert terms
\begin{multline*}
  V_\mathrm{DD}-\VTm=J_\mathrm{IMT}\pinsp l-n\Vth \ln\left[ \frac{k(n\Vth)^2}{l\pinsp \IOFF J_\mathrm{IMT}} \right]\\
  -n\Vth\ln\left[ \frac{lJ_\mathrm{MIT}\pinsp}{n\Vth}\exp\left\{ \frac{lJ_\mathrm{MIT}\pmet+l\vmet}{n\Vth} \right\} \right]
\end{multline*}
which becomes
\begin{multline*}
  V_\mathrm{DD}-\VTm=J_\mathrm{IMT}\pinsp l-n\Vth \ln\left[ \frac{kn\Vth J_\mathrm{MIT}}{ \IOFF J_\mathrm{IMT}} \right]
  -\left( lJ_\mathrm{MIT}\pmet+l\vmet \right)
\end{multline*}
and can be solved for $l$
\begin{equation}
  l=\frac{V_\mathrm{DD}-\VTm+n\Vth \ln\left[ \frac{kn\Vth J_\mathrm{MIT}}{ \IOFF J_\mathrm{IMT}} \right]}{J_\mathrm{IMT}\pinsp - J_\mathrm{MIT}\pmet-\vmet}
  \label{eq:lopt}
\end{equation}
and this result can be plugged back into \eqref{eq:Ill_geom_ext} so that the optimal geometry is obtained.

This derivation does have one (correctable) caveat: as $V_\mathrm{right}$ pushes toward \VDD, \VIMT\ is also moving toward \VDD, and when \VIMT\ is within \Vth\ of \VDD\ or larger, the voltage across the transistor drain-source in the near-\VDD\ insulating branch reaches \Vth\ or lower and it becomes impossible to saturate the transistor, so the entire model, with its assumption of saturation (neglect of the $F_s$ factor) breaks down.  In fact, it should be clear that, once $\VIMT>\VDD$, there can be no $I\rightarrow M$ jump (even though the formulas for $V_\mathrm{right}$ continue to provide a finite location).  While this is a region unsatisfactorally handled by the model, it is also a terrible region in which to design the device: over a short range of variation in material geometry, the right boundary of hysteresis rapidly moves from some reasonable location potentially well below $\VDD$ to entirely inaccessible.  So it makes sense to set a further constraint that $\VIMT$ stays well below $\VDD$ (numerical examination suggests $\VIMT < \VDD - \Vth/2$ actually suffices).  This constraint is simple to express as
\begin{equation}
  l=\frac{\VDD-\Vth/2}{J_\mathrm{IMT}\pins}
  \label{eq:lmax}
\end{equation}
The proper $l$ to use is the minimum of those suggested by \eqref{eq:lopt} and \eqref{eq:lmax}.  Experience thus far suggests that \eqref{eq:lmax} typically sets the minimum unless a very large safety margin $M_r$ (see below) is chosen.

As a practical concern of course, one may not wish to set the boundary of the hysteresis right at \VDD\ and 0, since these are the operating points.  It's easy to plug a safety margin into these formulas by replacing $\VDD$ with $\VDD-M_r\Vth$ in \eqref{eq:lopt}, where $M_r$ is a quantity of order unity chosen by the reliability engineer.  This does not change the exactness of the solution.  One could also, to reasonable approximation, multiply $wt$ by a safety factor $M_l+1$ (which increases $I_\mathrm{left}$ by the same factor and $M_l$ is of order unity) and then plug back into \eqref{eq:Vright_geom_ext} to resolve for $l$.  (In principle, this should process should be iterated to solve $l$ and $wt$ simultaneously, but at the precision of this discussion, one iteration is generally sufficient.)

%\section{Device considerations}
%Given the above expressions, we now analyze the effect of the PCR on $V_\mathrm{on}$ and $I_\mathrm{on}/I_\mathrm{off}$.  We begin with some basic observations of the HyperFET I-V and how they shape the design space for a steep-switching device.
%\begin{enumerate}
%\item In all of the above, $V_\mathrm{GS}$ only ever appears in the combination $V_\mathrm{GS}-\VT$, so a device engineer can shift the entire I-V curve horizontally by threshold-engineering, just as in a conventional transistor.  Thus, we will assume, without loss of generality, that the HyperFET operates between 0V and $V_\mathrm{ON}$, and this range (of width $V_\mathrm{ON}$) can be shifted to any desired location on the HyperFET I-V curve.
%\item If the HyperFET is to operate as a conventional logic device (with enhanced steepness), then the $V_\mathrm{OFF}=0\mathrm{V}$ must be to the left of the hysteresis, and $V_\mathrm{ON}$ must be to the right of the hysteresis.  For a given PCR, this requires a minimum $V_\mathrm{ON}>V_\mathrm{hyst}$.  From that minimum, $V_\mathrm{ON}$ will be expanded to ensure sufficient $\ION/\IOFF $ ratio (unless this ratio is already satisfied at the boundaries of the hysteretic region).
%\item If it is necessary to expand $V_\mathrm{ON}$ beyond $V_\mathrm{hyst}$, it is preferable (if the devices can be scaled properly) to expand to the right.  The insulating branch to the left of hysteresis, with its swing $>nkT/q$ will either rejoin the original transistor IV or hit the leakage floor (and then continue at essentially a constant ratio versus the original transistor IV).  If $V_\mathrm{OFF}$ is place\ldots
%
%
%%
%%If the OFF point (0V) is too far from the
%\end{enumerate}
%
%
%The procedure is essentially to choose a desired $I_\mathrm{on}/I_\mathrm{off}$, then find the minimum $V_\mathrm{on}$ compatible with this choice, given a fixed PCR.  We will assume that the device engineer is free to manipulate the  $\VT$ of the transistor, so that, effectively, the OFF point can be placed anywhere on the HyperFET I-V, and then the component devices are scaled.
%
%
%sliding it left and right to choose where on the HyperFET I-V the OFF current ($V_\mathrm{GS}=0$) on the as necessary to optimize the design.  Second, the device engineer may scale the transistor
%
%
%We assume the component devices have been scaled with the optimization of these parameters in mind.
%
%Thoughts for the morning:
%
%(1) Defend why want hysteresis in subthreshold
%    (a) To be able to climb toward right
%    (b) Does shape of hysteresis change at lower values?
%(2) Derive Von at fixed Ion/Ioff.  (width of hyst provided min Von, then grow at nVth*(Ion/Ioff- [Ion/Ioff]\_hyst)
%    (a) could probably get a better expression for [Von versus (Ion/Ioff)] or at least (Ion/Ioff)hyst by dividing Eq 6 and [the skipped equation leading to Eq 8].  That seems more likely to yield a good result than messing with W functions.
%(3) Mention in intro: Boltzmann only violated at one VGS per branch.  This paper gives the connection between local violation and global properties of Ion/Ioff vs Von.
%
%(4) Compute with right-end in sat. Near-Threshold Computing: Reclaiming Moore's Law Through Energy Efficient Integrated Circuits
%
\bibliography{biblio}{}
\bibliographystyle{plain}

\end{document}


% Generated by GrindEQ Word-to-LaTeX 2008
% ========== UNREGISTERED! ========== Please register! ==========
% LaTeX/AMS-LaTeX

\documentclass[a4paper]{article}
\usepackage{anysize}
\marginsize{1cm}{1cm}{1cm}{1cm}

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[dvips]{graphicx}
\usepackage{listings}
\lstset{language=haskell}
\lstset{commentstyle=\textit}
\lstset{mathescape=true}
%\lstset{labelstep=1}
%\lstset{backgroundcolor=,framerulecolor=}
\lstset{backgroundcolor=,rulecolor=}
\linespread{2.0}

\begin{document}

\noindent
\section{Subtyping Heaps and References}

\noindent Let us consider the types of the references of the example above, given $Heap\ h_0\ ref$ for some initial heap $h_0$ and some reference type$\ ref$:

\begin{lstlisting}
i : ref (New $h_0$ Int) Int
s : ref (New (New $h_0$ Int) String) String
\end{lstlisting}

The heap $h_0$ is the starting heap at the beginning of the program. If the program is used as a ``main'', that is it is just launched, then $h_0$ will be an empty heap; in other cases, like using our computation inside another, larger computation, will mean that  $h_0$ will be the current heap that is available where the example is launched.

\noindent Now let us consider the two statements:

\begin{lstlisting}
v $\leftarrow$ s
x $\leftarrow$ i
\end{lstlisting}

These two are incompatible, since the first statement forces our monad to be of type $ref\ \left(New\ \left(New\ h_0\ Int\right)\ String\right)$ and thus this type will have to be in all subsequent statements of the same monadic program, but the second statement expects our monad to be of type $ref\ \left(New\ h_0\ Int\right)$. This is absolutely reasonable on the part of the type system, but it is quite unacceptable given our circumstances: why cannot we use a reference that refers to a smaller heap where we have a larger heap available? Indeed, all the values that reference $i$ requires are available (plus some more that are irrelevant to $i$) in the heap that we have when the reference $s$ is in scope. It would be interesting to make it possible to use a reference that requires a smaller (less defined) heap when a larger (more specified) heap is available. This kind of notion is clearly a notion of subtyping between heaps and references.

\noindent To define a subtyping relationship between heaps, let us start by giving a subtyping predicate:

\begin{lstlisting}
Subtype $\alpha$ $\beta$
\end{lstlisting}

Which for brevity we will also write

\begin{lstlisting}
$\alpha$ $\le$ $\beta$
\end{lstlisting}
The fact that $\alpha $ is a subtype of $\beta $ implies that $\alpha $ is more specified than $\beta $, and as such it may be used in any context where a $\beta $ is expected. For this reason we also give a casting (or coercion) function that converts from a value of type $\alpha $ to a value of its supertype $\beta $:

\begin{lstlisting}
downcast :$\alpha$ $\to$ $\beta$
\end{lstlisting}
We also know that the subtyping relation is reflexive and transitive, so we will add these rules as instances of the subtyping predicate:

\begin{lstlisting}
Subtype $\alpha$ $\alpha$ downcast=$\lambda$ x.x
\end{lstlisting}

\begin{lstlisting}
Subtype $\alpha$ $\beta$ $\wedge$ Subtype $\beta$ $\gamma$ $\Rightarrow$ Subtype $\alpha$ $\gamma$ downcast=downcast $\circ$ downcast
\end{lstlisting}

Since we are mostly interested in subtyping between references, when is it safe to downcast them? As already discussed, a first criterion for downcasting a reference is that the heap $h$ that the reference manipulates is actually smaller than the current heap; this means that:

\begin{lstlisting}
Heap h ref $\wedge$ Heap $h'$ ref $\wedge$ $h'$ $\le$ h $\Rightarrow$ ref h $\alpha$ $\le$ ref $h'$ $\alpha$
\end{lstlisting}

This means that references are contravariant with respect to their heap. Also, the second argument of references allows for the intuitive kind of conversion:

\begin{lstlisting}
$\alpha$ $\le$ $\alpha'$ $\Rightarrow$ ref h $\alpha$ $\le$ ref h $\alpha'$
\end{lstlisting}

This second rule shows that a reference to a value $\alpha $ behaves exactly like that value, thereby allowing conversions that mirror the behavior of the value that our reference is standing for.

These rules are quite hard to concretely instance. This is why we will not be able to instance these rules once and for all in a highly parametric fashion, but instead we will just give some specific instances that are of particular use to us when we are dealing with concrete heaps and references. Also, we will mostly deal with subtyping between values and not subtyping between functions.

At this point we can rewrite the ``incriminated'' example above so that it compiles:

\begin{lstlisting}[frame=tb,mathescape]{somecode}
$ex_2'$ =
	do 10 $>>=$ ($\lambda$i.
	do "hello " $>>=$ ($\lambda$s.
	do s *= ($\lambda$x.x++"world")
	   let $i'$ = downcast i
	   v$\leftarrow$eval s
	   x$\leftarrow$eval $i'$
	   return v ++ show x))
\end{lstlisting}

Now the type of $ex_2'$ will not only reflect the type of the result of the computation, but also the requirement of subtyping between heaps built with the $New$ type operator:

\begin{lstlisting}
Heap $h_0$ ref $\wedge$ ref (New $h_0$ Int) Int $\le$ ref (New (New $h_0$ Int) String) Int $\Rightarrow$ $ex_2'$ : ref $h_0$ String
\end{lstlisting}

\end{document}

% == UNREGISTERED! == GrindEQ Word-to-LaTeX 2008 ==