\chapter{IDL Routines}
\label{ch_mrsp_idl}
%\chapterhead{IDL Routines}
\markright{IDL Routines}

\section{Introduction}
A set of routines has been developed in IDL. Starting IDL using the script program {\em isap.pro} allows the user
to get the interactive sparse data analysis  environment, and all routines described in the following can be called.
An online help facility is also available by invoking the {\em isaph} program under IDL. See Chapter~\ref{ch_install} for more
details on the software installation.

\section{Functions for polarized spherical maps}
\index{polar}

\subsection{Reading a polarized spherical map from a file : mrsp\_read}
\index{IDL routines!mrsp\_read}
\index{polar!read polarized map}
Read a polarized spherical map in Healpix format.
{\bf
\begin{center}
     USAGE: map = mrsp\_read( file, noverb=noverb )
\end{center}}
where
\begin{itemize}
\item {\em file} : Input string, name of the file to be read. The pathname can be included in the string, by default 'file.fits' is equivalemt to './file.fits'
\item {\em noverb} : scalar, prevent the printing on the screen of the format (RING or NESTED) of the read map and the number of pixels.
\item {\em map} : Output 3D IDL array of Healpix map read. The map is setted to the NESTED format after reading.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item map = mrsp\_read( 'my\_file\_healpix\_pola.fits', noverb=noverb ) \\
Read the map stored into the file 'my\_file\_healpix\_pola.fits' and load it into map.
\end{itemize}

\subsection{Writing a polarized spherical map into a file : mrsp\_write}
\index{IDL routines!mrsp\_write}
\index{polar!write polarized map}
Write a polarized spherical map in Healpix format.
{\bf
\begin{center}
     USAGE: mrsp\_write, file, map, ring=ring
\end{center}}
where
\begin{itemize}
\item {\em file} : Input string, name of the file to be writen. The pathname can be included in the string, by default 'file.fits' is equivalemt to './file.fits'
\item {\em map} : Input 3D IDL array of Healpix map to be writen. The map is assumed to be in the NESTED format.
\item {\em ring} : scalar, if set convert the Healpix map data to the RING format for the writing.
\end{itemize}

\subsection{Conversion of a polarized spherical map from TQU scheme to TEB scheme : mrsp\_tqu2teb}
\index{IDL routines!mrsp\_tqu2teb}
\index{polar!conversion tqu2teb}
Convert a polarized map in Healpix nested format from TQU scheme to TEB scheme.
{\bf
\begin{center}
     USAGE: mrsp\_tqu2teb, map\_tqu, map\_teb
\end{center}}
where
\begin{itemize}
\item {\em map\_tqu} : Input 3D IDL array of healpix polarized map in TQU scheme.
\item {\em map\_teb} : Output 3D IDL array of healpix polarized map in TEB scheme.
\end{itemize}

\subsection{Conversion of a polarized spherical map from TEB scheme to TQU scheme : mrsp\_teb2tqu}
\index{IDL routines!mrsp\_teb2tqu}
\index{polar!conversion tqu2teb}
Convert a polarized map in Healpix nested format from TEB scheme to TQU scheme.
{\bf
\begin{center}
     USAGE: mrsp\_teb2tqu, map\_teb, map\_tqu
\end{center}}
where
\begin{itemize}
\item {\em map\_teb} : Input 3D IDL array of healpix polarized map in TEB scheme.
\item {\em map\_tqu} : Output 3D IDL array of healpix polarized map in TQU scheme.
\end{itemize}

\subsection{Resizing a polarized spherical map: mrsp\_resize}
\index{IDL routines!mrsp\_resize}
\index{polar!polarized map resizing}
Resize a polarized map in Healpix nested format.
{\bf
\begin{center}
     USAGE: resize\_map = mrsp\_resize( map, nside=nside, ViaAlm=ViaAlm, teb=teb )
\end{center}}
where
\begin{itemize}
\item {\em map} : Input 3D IDL array of healpix polarized map in TQU scheme to be transformed.
\item {\em resize\_map} : Output 3D IDL array of healpix polarized map in TQU scheme. Healpix input map and output resized map are in nested format.
\item {\em nside} : int, the new nside parameter of the output healpix resized map.
\item {\em ViaAlm} : scalar, if set use alm transform for the resizing, otherwise, use interpolation.
Ignored if nside keyword value is lower than imag nside.
\item {\em teb} : scalar, if set specifies that the input and output images are in TEB scheme.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item map2 = mrsp\_resize( map, nside = 256, /ViaAlm ) \\
resize an Healpix map.
\end{itemize}

\section{General transform/reconstruction routines}

\subsection{Transformations of a polarized spherical map : mrsp\_trans}
\index{IDL routines!mrsp\_trans}
\index{polar!transforms of polarized image}
Compute a transform (E-B mode decomposition, wavelet with spline, Meyer or needelet filters, curvelet, \ldots)
on a polarized map on the sphere in the Healpix representation (nested data representation) in TQU scheme.\\ \\
The transform can be:
\begin{enumerate}
\item A E/B decomposition using the spin\_2 transform.
\item An orthogonal wavelet on each T,Q,U component.
\item A pyramidal isotropic wavelet on each T,Q,U component.
\item An undecimated wavelet transform on each component.
\item A decimated modulus-Phase wavelet transform.
\item A undecimated modulus-Phase wavelet transform.
\item A curvelet transform.
\end{enumerate}
{\bf
\begin{center}
     USAGE: mrsp\_trans, Imag, Trans, NbrScale=NbrScale, lmax=lmax, MeyerWave=MeyerWave, ebdec=ebdec, Cur=Cur, uwt=uwt, owt=owt, mpdwt=mpdwt, mpuwt=mpuwt, PyrWT=PyrWT,
     DifInSH=DifInSH, Overlap=Overlap, FirstBlockSize=FirstBlockSize
\end{center}}
where
\begin{itemize}
\item {\em Imag} : Input 3D IDL array of healpix polarized map in TQU scheme. Input image to be transformed.
\item {\em Trans} : Output IDL structure with the following fields:
\begin{itemize}
\item {\em NbrScale} : int, number of scales.
\item {\em nside} : int, Healpix nside parameter.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space.
\item {\em npix} : long, number of pixels of the input image.
\item {\em MeyerWave} : int, 1 if the keyword MeyerWave used, otherwise 0.
\item {\em DifInSH} : int, 1 if the keyword DifInSH used, otherwise 0.
\item {\em pyrtrans} : int, 1 if a pyramidal decomposition has been applied, otherwise 0.
\item {\em ebdec} : int, 1 if an EB decomposiiton has been applied, otherwise 0.
\item {\em DEC1} : IDL structure, first component transformation (depends on the chosen transform).
\item {\em DEC2} : IDL structure, second component transformation (depends on the chosen transform).
\item {\em DEC3} : IDL structure, third component transformation (depends on the chosen transform).
\item {\em TransChoice} : string, code of the chosen transform.
\item {\em TabCodeTransform} : string array, array of transforms codes. TabCodeTransform = ['T\_EBDEC', 'T\_OWT', 'T\_PyrWT', 'T\_UWT', 'T\_MPDWT', 'T\_MPUWT', 'T\_CUR']
\item {\em TransName} : string, transform's name.
\item {\em TransTypeName} : string array, array of transforms names.
TransTypeName = ['EBDEC','Bi-Orthogonal WT', 'Pyramidal WT', 'Undecimated WT', 'Module-Phase Decimated Transform', 'Module-Phase Undecimated Transform', 'Curvelet']
\end{itemize}
\item {\em NbrScale} : int, number of scales of the wavelet transforms.
\item {\em ebdec} : scalar, if set an E/B decomposition is applied before the chosen multiscale decomposition.
If no transform is selected, it will be the default transformation.
\item {\em Cur} : scalar, if set perform a curvelet transform.
\item {\em uwt} : scalar, if set perform an undecimated isotropic wavelet transform.
\item {\em PyrWT} : scalar, if set perform a pyramidal isotropic wavelet transform.
\item {\em owt} : scalar, if set perform a bi-orthogonal wavelet transform on each face.
\item {\em mpdwt} : scalar, if set perform a decimated module-phase wavelet transform.
\item {\em mpuwt} : scalar, if set perform a undecimated module-phase wavelet transform.
\item {\em Overlap} : int, if equal to 1 if blocks are overlapping, only used with curvelet transform.
\item {\em FirstBlockSize} : int, block size in the ridgelet transform at the finest scale (default is 16), only used with curvelet transform.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space (for isoptropic wavelet transform only).
\item {\em DifInSH} : Input keyword parameter. If set, the wavelet coefficients are computed as the difference between two resolutions in the spherical harmonics representation.
Otherwise, the wavelet coefficients are computed as the difference between two resolutions in direct space. Only used with keyword uwt or PyrWT.
\item {\em MeyerWave} : If set, use Meyer wavelets and set the keyword DifInSH. Only used with keyword uwt or PyrWT.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item mrsp\_trans, Imag, WT, NbrScale=5, /uwt \\
Compute the undecimated wavelet transform of the map Imag with five scales. The result is stored in WT.
\end{itemize}

\subsection{Reconstructions of a polarized spherical map : mrsp\_rec}
\index{IDL routines!mrsp\_rec}
\index{polar!inverse transform of polarized image}
Compute a inverse transform (wavelet, curvelet, \ldots) to get a polarized map on the sphere in the Healpix representation
(nested data representation) in TQU scheme from its decomposition obtained by mrsp\_trans.\\ \\
The transform can be:
\begin{enumerate}
\item A E/B decomposition using the spin\_2 transform.
\item An orthogonal wavelet on each T,Q,U component.
\item A pyramidal isotropic wavelet on each T,Q,U component.
\item An undecimated wavelet transform on each component.
\item A decimated modulus-Phase wavelet transform.
\item A undecimated modulus-Phase wavelet transform.
\item A curvelet transform.
\end{enumerate}
{\bf
\begin{center}
     USAGE: mrsp\_rec, Trans, Rec
\end{center}}
where
\begin{itemize}
\item {\em Trans} : Input IDL structure, see mrsp\_trans for more details.
\item {\em Rec} : Output 3D IDL array of healpix polarized map in TQU scheme. Reconstructed image.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item mrsp\_trans, Imag, WT, NbrScale=5, /uwt \\
Compute the undecimated wavelet transform of the map Imag with five scales. The result is stored in WT.
\item mrsp\_rec, WT, RecIma \\
Reconstruct the image.
\end{itemize}

\section{Spin-2 spherical harmonic transform}

\subsection{ALM transform of a polarized spherical map : mrsp\_almtrans}
\index{IDL routines!mrsp\_almtrans}
\index{polar!polar ALM transform}
Computes the spherical harmonic transform of a polarized TQU map using the Healpix representation (nested data).
{\bf
\begin{center}
     USAGE: mrsp\_almtrans, Imag, Trans, lmax=lmax, tab=tab, complex=complex, norm=norm, fast=fast
\end{center}}
where
\begin{itemize}
\item {\em Imag} : Input 3D IDL array of healpix polarized map in TQU scheme to be transformed.
\item {\em Trans} : Output IDL structure with the following fields:
\begin{itemize}
\item {\em ALM} : array of the ALM coefficients
\begin{center}
ALM = fltarray[*,2,3] list of the real part (ALM[*,0,*]) and imaginary part (ALM[*,1,*]) of the ALM.
This is the default storage, ALM[*,*,0] is ALM T, ALM[*,*,1] is ALM E and ALM[*,*,2] is ALM B\\
ALM = cfarr[*,3] list of the ALM in complex values format if the keyword complex is set.
ALM[*,0] is ALM T, ALM[*,1] is ALM E and ALM[*,2] is ALM B\\
ALM = fltarray[NbrMaxM, NbrMaxL, 2, 3] table of the real part (ALM[*,*,0,*]) and imaginary part (ALM[*,*,1,*])
of the ALM if the keyword tab is set. ALM[*,*,*,0] is ALM T, ALM[*,*,*,1] is ALM E and ALM[*,*,*,2] is ALM B\\
ALM = cfarr[NbrMaxM, NbrMaxL, 3] table of the ALM in complex values format if the keywords complex and tab
are both setted. ALM[*,*,0] is ALM T, ALM[*,*,1] is ALM E and ALM[*,*,2] is ALM B\\
By default, NbrMaxM = NbrMaxL = lmax+1
\end{center}
\item {\em complex\_alm} : int, 0 (default value) if ALM array contains real and imaginary part separated.
1 if ALM is a complex array.
\item {\em PixelType} : int, 0 for a Healpix input map (1 for GLESP but not used).
\item {\em tab} : int, 0 for default ALM representation as a list (i.e. 1D IDL array) and 1 for 2D
representation as a table (i.e. l for the first dimension and m for the second).
\item {\em nside} : int, Healpix nside parameter.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space.
\item {\em npix} : long, number of pixels of the input image.
\item {\em TabNbrM} : int array[NbrMaxL], max number of m value for a given l, only used if keyword tab is set otherwise, 0.
\item {\em index} : long array, indicies of the ALM coefficients, used only if keyword tab is not set.
\item {\em NormVal} : float, normalization value applied to the alm coefficients (only if keyword norm used).
\item {\em norm} : int, 0 if no normalization has been aplied, else 1.
\end{itemize}
\item {\em lmax} : int, Number of spherical harmonics computed in the decomposition. For a Healpix map, default is
3*nside and should be between 2*nside and 4*nside.
\item {\em tab} : scalar, if set, ALM coefficients in Trans.alm are stored in a 2D array: Trans.alm[m,l] where m = 0..Trans.TabNbrM[l]-1  and l = 0..lmax-1
\item {\em complex} : scalar, if set Trans.alm will contain complex values instead of the real and imaginary parts.
\item {\em norm} : scalar, if set, a normalization is performed to the alm coefficients.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mrsp\_almtrans, Imag, Output \\
Compute the spherical harmonics transform of a polarized image, the result is stored in Output.
\end{itemize}

\subsection{ALM inverse transform of a polarized spherical map : mrsp\_almrec}
\index{IDL routines!mrsp\_almrec}
\index{polar!polar ALM inverse transform}
Computes the inverse spherical harmonic transform of a polarized TQU map using using the Healpix representation (nested data).
{\bf
\begin{center}
     USAGE: mrsp\_almrec, Trans, imag, pixel\_window=pixel\_window
\end{center}}
where
\begin{itemize}
\item {\em Trans} : Input IDL structure of ALM coefficients, see mrsp\_almtrans above for details.
\item {\em Imag} : Output 3D IDL array of healpix polarized map in TQU scheme. Image reconstructed in Healpix nested representation.
\item {\em pixel\_window} : scalar, if set the image is convolved by the healpix pixel window (only for Healpix map).
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mrsp\_almtrans, PolaImag, Output \\
Compute the spherical harmonics transform of a polarized image, the result is stored in Output.
\item mrsp\_almrec, Output, PolaRec \\
Reconstruct the image.
\end{itemize}

\subsection{Power spectrum and cross spectrum exctraction from polarized ALM~: mrsp\_alm2spec}
\index{IDL routines!mrsp\_alm2spec}
\index{polar!spectrum exctraction from polarized ALM}
Computes the power spectrums and cross spectrums of a polarized map from the polarized ALM coefficients.
{\bf
\begin{center}
     USAGE: spec = mrsp\_alm2spec( ALM, StdPS=StdPS )
\end{center}}
where
\begin{itemize}
\item {\em ALM} : Input IDL structure of ALM polarized coefficients, see mrsp\_almtrans above for details.
\item {\em spec} : Output 2D IDL float array[ALM.lmax+1,6], the TT, EE, BB, TE, TB, EB spectrums. P[k,i] = Mean( SPECTRUM[*,l,i] ) \quad i=0...5.
\item {\em StdPS} : Output 2D IDL float array[ALM.lmax+1,6]: estimated standard deviation of the spectrums coefficients.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mrsp\_almtrans, Imag, Output \\
Compute the spherical harmonics transform of a polarized image, the result is stored in Output.
\item spec = mrsp\_alm2spec( Output, StdPS=StdPS ) \\
Compute the spectrums of the image and it's associated standard deviation.
\end{itemize}

\subsection{Power spectrum and cross spectrum exctraction from a polarized image : mrsp\_spec}
\index{IDL routines!mrsp\_spec}
\index{polar!spectrum exctraction from a polarized image}
Computes the power spectrums and cross spectrums of a polarized map, using the HEALPix representation (nested data representation by default).
By default a normalisation is applied on the ALM coefficents.
{\bf
\begin{center}
     USAGE: spec = mrsp\_spec( Imag, nonorm=nonorm, teb=teb, NormVal=NormVal, StdPS=StdPS, lmax=lmax )
\end{center}}
where
\begin{itemize}
\item {\em Imag} : Input 3D IDL array of healpix polarized map in TQU scheme. Input image whose power spectrum will be extracted.
\item {\em spec} : Output 2D IDL float array[ALM.lmax+1,6], the TT, EE, BB, TE, TB, EB spectrums. P[k,i] = Mean( SPECTRUM[*,l,i] ) \quad i=0...5.
\item {\em Lmax} : int, number of spherical harmonics computed in the decomposition and size of the computed spectrum (Lmax+1). Default is 3*nside and should be between 2*nside and 4*nside.
\item {\em nonorm} : scalar, if set no normalisation is applied on the ALM computed.
\item {\em StdPS} : Output 2D IDL float array[ALM.lmax+1,6]: estimated standard deviation of the spectrums coefficients.
\item {\em NormVal} : float, normalization value applied to the alm coefficients.
\item {\em teb} : scalar, if set specifies that the input map is in TEB scheme.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item P = mrsp\_spec( Imag ) \\
Compute the spectrum of the polarized image.
\end{itemize}

\section{Polarized Wavelets}
\subsection{Undecimated Isotropic Wavelet Transform of a polarized spherical map : mrsp\_wttrans}
\index{IDL routines!mrsp\_wttrans}
\index{polar!undecimated wavelet transform of polarized image}
Computes the undecimated isotropic wavelet transform of polarized maps on the sphere in TQU scheme,
using the Healpix representation (nested data representation). The wavelet function is zonal and its
spherical harmonics coefficients $a_{l,0}$ follow a cubic box-spline profile. If the keyword DifInSH is
set, the wavelet coefficients are derived in the Spherical Harmonic Space, otherwise (default) they
are derived in the direct space.
{\bf
\begin{center}
     USAGE: mrsp\_wttrans, Imag, Trans, NbrScale=NbrScale, lmax=lmax, DifInSH=DifInSH, MeyerWave=MeyerWave
\end{center}}
where
\begin{itemize}
\item {\em Imag} : Input 3D IDL array of healpix polarized map in TQU scheme. Input image to be transformed.
\item {\em Trans} : Output IDL structure with the following fields:
\begin{itemize}
\item {\em NbrScale} : int, number of scales.
\item {\em nside} : int, Healpix nside parameter.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space.
\item {\em npix} : long, number of pixels of the input image.
\item {\em MeyerWave} : int, 1 if the keyword MeyerWave used, otherwise 0
\item {\em DifInSH} : int, 1 if the keyword DifInSH used, otherwise 0
\item {\em Coef} : fltarr[npix,NbrScale,3] wavelet transform of the data. Coef[*,*,0] = wavelet transform on T, Coef[*,*,1] = wavelet transform on E, Coef[*,*,2] = wavelet transform on B
\begin{center}
Coef[*,0,*] = wavelet coefficients of the finest scale (highest frequencies).\\
Coef[*,NbrScale-1,*] = coarsest scale (lowest frequencies).
\end{center}
\end{itemize}
\item {\em NbrScale} : int, optional input parameter specifying the number of scales (default is 4).
\item {\em Lmax} : int, optional input parameter specifying the maximum multipole number $l$ in the spherical harmonics decomposition
(default is $3\times \textrm{nside}$, should be between $2\times \textrm{nside}$ and $4\times \textrm{nside}$).
\item {\em DifInSH} : Input keyword parameter. If set, the wavelet coefficients are computed as the difference between two resolutions in the spherical harmonics representation.
Otherwise, the wavelet coefficients are computed as the difference between two resolutions in direct space.
\item {\em MeyerWave} : If set, use Meyer wavelets and set the keyword DifInSH.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mrsp\_wttrans, Imag, Output, NbrScale=5 \\
Compute the isotropic wavelet transform of the map Imag with five scales. The result is stored in Output.
\end{itemize}

\subsection{Undecimated Isotropic Wavelet Reconstruction of a polarized spherical map : mrsp\_wtrec}
\index{IDL routines!mrsp\_wtrec}
\index{polar!undecimated wavelet reconstruction of polarized image}
Reconstructs a polarized maps on the sphere in TQU scheme using the Healpix representation (nested data representation)
from its wavelet coefficients obtained with the undecimated isotropic wavelet transform on the sphere, described right above.
{\bf
\begin{center}
     USAGE: mrsp\_wtrec, Trans, Rec, filter=filter
\end{center}}
where
\begin{itemize}
\item {\em Trans}: Input IDL structures with the following fields:
\begin{itemize}
\item {\em NbrScale} : int, number of scales.
\item {\em nside} : int, Healpix nside parameter.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space.
\item {\em npix} : long, number of pixels of the input image.
\item {\em MeyerWave} : int, 1 if the keyword MeyerWave used, otherwise 0
\item {\em DifInSH} : int, 1 if the keyword DifInSH used, otherwise 0
\item {\em Coef} : fltarr[npix,NbrScale,3] wavelet transform of the data. Coef[*,*,0] = wavelet transform on T, Coef[*,*,1] = wavelet transform on E, Coef[*,*,2] = wavelet transform on B
\begin{center}
Coef[*,0,*] = wavelet coefficients of the finest scale (highest frequencies).\\
Coef[*,NbrScale-1,*] = coarsest scale (lowest frequencies).
\end{center}
\end{itemize}
\item {\em Rec} : Output 3D IDL array of healpix polarized map in TQU scheme. Reconstructed image from the wavelet coefficients.
\item {\em filter} : Input keyword parameter. Use filters for the reconstructions. If this keyword is not set, the reconstructed image is obtained
by a simple addition of all wavelet scales. Automaticaly applied if keyword MeyerWave or DifInSH were set at the wavelet decomposition.
\end{itemize}

\subsubsection*{Examples:}
\begin{itemize}
\item mrsp\_wttrans, Imag, Output, NbrScale=5 \\
Compute the isotropic wavelet transform of the map Imag with five scales. The result is stored in Output.
\item mrsp\_wtrec, Output, map \\
Reconstruct the map.
\end{itemize}

\subsection{Extract a scale from a polar decomposition : mrsp\_wtget}
\index{IDL routines!mrsp\_wtget}
\index{polar!polar wavelet extraction}
Return a band of a transform for Healpix polarized map (wavelet, curvelet\ldots) obtained by the command mrsp\_trans.
{\bf
\begin{center}
     USAGE:  Scale = mrsp\_wtget( Trans, Component, ScaleNumber, BandNumber=BandNumber, NormVal=NormVal )
\end{center}}
where
\begin{itemize}
\item {\em Trans} : Input IDL structure, see mrsp\_trans for more details.
\item {\em ScaleNumber} : int, scale number of the band to be extracted. The scale number must be between 0 and Trans.NbrScale-1.
\item {\em Component} : int, choice of the component, 0 is for T, 1 for E and 2 for B.
\item {\em NormVal} : float, optional normalization value of the band (for isotropic wavelet transform).
\item {\em BandNumber} : int, ridgelet band number (for curvelet transform).
\item {\em Scale} : return value IDL array of the band extracted. See more details on the 1D versions of the functions.
No band is extracted from Modulus-Phases transforms (return 0). For a E/B decomposition, it will be either the T map, the E map or the B map.
\end{itemize}

\subsection{Put a scale into a polar decomposition : mrsp\_wtput}
\index{IDL routines!mrsp\_wtput}
\index{polar!polar wavelet insertion}
Put a band into a transform for Healpix polarized map (wavelet, curvelet\ldots) obtained by the command mrsp\_trans.
{\bf
\begin{center}
     USAGE:   mrsp\_wtput, Trans, Scale, Component, ScaleNumber, BandNumber=BandNumber
\end{center}}
where
\begin{itemize}
\item {\em Trans} : Input IDL structure, see mrsp\_trans for more details.
\item {\em Scale} : Input IDL array of the band inserted. See more details on the 1D versions of the functions.
No band is inserted into Modulus-Phases transforms. For a E/B decomposition, it will be either the T map, the E map or the B map.
\item {\em ScaleNumber} : int, scale number of the band to be extracted. The scale number must be between 0 and Trans.NbrScale-1.
\item {\em Component} : int, choice of the component, 0 is for T, 1 for E and 2 for B.
\item {\em BandNumber} : int, ridgelet band number (for curvelet transform).
\end{itemize}

\section{Denoising}
\subsection{Wavelet filtering of a polarized spherical map : mrsp\_wtfilter}
\index{IDL routines!mrsp\_wtfilter}
\index{polar!polar wavelet filtering}
Wavelet denoising of a polarized image on the sphere using Healpix representation in TQU scheme (nested pixel representation).
By default Gaussian noise is considered. If the keyword SigmaNoise is not set, then the noise standard deviation is automatically
estimated. If the keyword MAD is set, then a correlated Gaussian noise is considered and the noise level at each scale is derived
from the Median Absolution Deviation (MAD) method. If the keyword KillLastScale is set, the coarsest resolution is set to zero.
The thresholded wavelet coefficients can be obtained using the keyword Trans. If the input keyword niter is set, then an iterative
algorithm is applied and if the pos keyword is also set, then a positivity constraint is added.
{\bf
\begin{center}
     USAGE:  mrsp\_wtfilter, Imag, Filter, NbrScale=NbrScale, NSigma=NSigma, SigmaNoise=SigmaNoise, KillLastScale=KillLastScale,
     pos=pos, mad=mad, Trans=Trans, niter=niter, FirstScale=FirstScale, Use\_FdrAll=Use\_FdrAll, soft=soft, fdr=fdr, lmax=lmax,
     FilterLast=FilterLast, mask=mask
\end{center}}
where
\begin{itemize}
\item {\em Imag} : Input 3D IDL array of healpix polarized map in TQU scheme. Input image to be filtered.
\item {\em Filter} : Output 3D IDL array of healpix polarized map in TQU scheme containing the filtered map.
\item {\em NbrScale} : int, number of scales (default is 4).
\item {\em NSigma} : float, level of thresholding (default is 3).
\item {\em SigmaNoise} : float, noise standard deviation. Default is automatically estimated.
\item {\em mad} : scalar, if set the noise level is derived at each scale using the MAD of the wavelet coefficient.
\item {\em KillLastScale} : scalar, if set the last scale is set to zero.
\item {\em niter} : int, number of iterations used in the reconstruction.
\item {\em pos} : scalar, if set the solution is assumed to be positive.
\item {\em FirstScale} : int, consider only scales larger than FirstScale. Default is 1 (i.e. all scales are used).
\item {\em Soft} : scalar, if set use soft thresholding instead of hard thresholding.
\item {\em fdr} : float between 0 (default) and 1 (max, if greater or equal to 1, set to 0.05), used to estimate a threshold level
instead of a NSigma threshold, threshold is applied from scale j=FirstScale to the last.
\item {\em Use\_FdrAll} : same as fdr but applied to all scales.
\item {\em FilterLast} : scalar, if set the last scale is filtered.
\item {\em mask} : IDL array of healpix map, input mask applied.
\item {\em lmax} : int, maximum l value in the Spherical Harmonic Space.
\item {\em Trans} : IDL structure: Thresholded wavelet decomposition of the input image.
\end{itemize}

\subsubsection*{Example:}
\begin{itemize}
\item mrsp\_wtfilter, Imag, Filter, NbrScale=5, Nsigma=5 \\
Wavelet filtering with five scales and a 5 sigma threshold.
\end{itemize}

\subsection{Thresholding in polarized wavelet or curvelet space  : mrsp\_threshold}
\index{IDL routines!mrsp\_threshold}
\index{polar!polar curvelet filtering}

Threshold the decomposition coefficients of a polarized healpix map. This routine works with several decompositions (see mrsp\_trans).
{\bf
\begin{center}
     USAGE:  mrsp\_threshold, Trans, NSigma=Nsigma, Mad=Mad, KillLastScale=KillLastScale
\end{center}}
where
where
\begin{itemize}
\item {\em Trans} : IDL structures obtained from the {\em mrsp\_trans} routine.
\item {\em Nsigma}: Level of thresholding (default is 3)
\item {\em KillLastScale}: if set, the last scale is set to zero
\end{itemize}

\subsubsection*{Examples:}
 Compute the undecimated wavelet transform of a vector field I with five scales (needlet filters).  The result is stored in WT, then wavelet coefficients are threshold at 2 sigma, and the filtered polarized map is reconstructed.
\begin{itemize}
\item  mrsp\_trans, I, WT, NbrScale=5, /UWT, /NeedletWave
\item  mrsp\_threshold, WT, NSigma=2
\item  mrsp\_rec, WT, RecPola
\end{itemize}
Compute the curvelet transform of a vector field I with five scales.  The result is stored in C, then curvelet coefficients are threshold at 5 sigma, and the filtered polarized map
is reconstructed.
\begin{itemize}
\item  mrsp\_trans, I, C, NbrScale=5, /Cur
\item  mrsp\_threshold, C, NSigma=5
\item  mrsp\_rec, C, RecPola
\end{itemize}


\chapter{Introduction} \label{ch:introduction}

A type system is a delicate balance between expressivity and well-behavedness.
More expressivity allows for more programs to be well typed,
while paradoxically allowing less code to be written,
because some of the onus of ensuring a program is valid is shifted
from the programmer to the process of type checking.
On the other hand, too much expressivity can destroy
desirable metatheoretical properties of the type system ---
strings get be treated as integers, booleans can be called like functions,
and the language falls to chaos and ruin.

The balancing act is particularly perilous for type systems
used by proof assistants for mechanized theorem proving,
because a lack of expressivity can make it too much of a burden to be practically useful,
while it's all to easy to add some convenient, seemingly innocuous feature
that turns out to dismantle the logic of the system,
rendering it unusable for proving.

This thesis deals with dependent type theory, which is commonly used for theorem proving,
together with sized types, a feature for inductively defined types
used to increase the expressivity of recursive functions,
and shows that dependent types can be extended with state-of-the-art sized types
without sacrificing key metatheoretical properties.

\section{Background}

Before we begin, I briefly summarize why dependent types are used for theorem proving,
as well as what sized types have to offer in the context of programming with dependent types.

\subsection{Theorem proving with dependent types} \label{tt}

Many contemporary proof assistants are founded on the Curry--Howard correspondence,
where propositions correspond to types,
proofs of these propositions to terms of those types,
and proof verification to type checking.
Different type theories correspond to different logical systems;
dependent type theories, in particular, correspond to predicate logics.
The primary feature of dependent type theory is the dependent function type,
because it encodes universal quantification by allowing types to \emph{depend on} terms:
the type $\funtype{x}{A}{\app{P}{x}}$ can be interpreted as the statement
``for all objects $x$ in $A$, $P$ holds of $x$'',
with the consequent $\app{P}{x}$ depending on the term $x$.
Other logical constructs can be encoded as well, for example:

\begin{itemize}
  \item Falsehood ($\bot$) as $\funtype{P}{\Prop}{P}$,
    (where $\Prop$ is the type of all propositions),
    the statement that \emph{all} propositions hold;
  \item Implication as $\funtype{\any}{A}{B}$
    (or $\arr*{A}{B}$ for short),
    where $B$ holds only when given a proof of $A$;
  \item Negation as $\arr*{A}{\bot}$,
    or that proving $A$ will yield a falsehood; and
  \item Truthhood as $\funtype{P}{\Prop}{\arr*{P}{P}}$\punctstack{,}%
    \footnote{This encoding is slightly simpler than
    the negation of falsehood $\arr*{\bot}{\bot}$,
    particularly when written out in full.}
    which is trivially inhabited by the identity function
    $\fun{P}{\Prop}{\fun{p}{P}{p}}$;
\end{itemize}
and many other logical connectives such as existential quantification, conjunction, disjunction, and equality.

A key metatheoretical property that a type theory must satisfy
for its interpretation as a logical system to be valid is \emph{consistency}:
falsehood cannot be proven.
In other words, there must not be any term whose type is $\funtype{P}{\Prop}{P}$.
Otherwise, any proposition would be provable,
including those we know to be obviously untrue,
such as $0 = 1$.

The two most important components of a formal description of a dependent type theory
are its typing judgement, whose rules describe when a term has a certain type,
and its equality judgement(s), whose rules describe when two terms are equal.
The latter is relevant in dependent types because they allow determining that,
for instance, a proof of \mbox{$\app{P}{(1 + 1)}$}
is in fact also a proof of $\app{P}{2}$.
Naturally, equality is sometimes described in terms of reduction judgements,
governed by the rules of computation.
This means that, in contrast to nondependent type systems,
whether type checking always terminates depends on whether the language is normalizing.
The issue gets even more complicated with more complex forms of reduction like recursion.

\subsection{Recursive programming with sized types} \label{ss}

Inductively defined data and recursive functions on them
are indispensable tools of programming,
allowing the expression of a variety of constructs, programs,
and in the case of dependent types, their properties.
A crucial restriction on recursive functions
in proof assistants such as Agda, Coq, Idris, Lean, and more
is that recursive functions must be \emph{guarded by destructors}\index{guardedness}
and only recur on structurally smaller arguments~\citep{guard}.
This guardedness check is statically done before or during type checking and,
to vastly simplify, ensures that recursive calls occur only on syntactic subarguments:
if the original argument is some natural $\app{\succ*}{n}$ that is the successor of $n$,
then the function recurs only on $n$;
if it's some cons list $\app{\cons*}{\hd}{\tl}$
consisting of an element and the rest of the list $\tl$,
then it recurs only on $\tl$; and so on.

Although this is usually referred to as a syntactic termination check,
and termination is essential in ensuring decidability of type checking,
the key desired property is rather consistency.
Indeed, a type theory may be consistent yet nonnormalizing
(as is the case with an impredicative\index{impredicativity},
definitionally proof-irrelevant $\Prop$ universe~\citep{impred-proof-irrel}),
but unrestricted recursion easily proves an inconsistency:
the recursive function $f$ defined by $\fun{P}{\Prop}{\app{f}{P}}$,
for instance, can be assigned type $\funtype{P}{\Prop}{P}$,
in addition to being nonterminating.

While the guardedness check, being based on the elimination principles of inductive types,
yields terminating functions and a consistent type theory,
it has known disadvantages:

\begin{itemize}
  \item Calling some other function on a subargument prior to a recursive call is disallowed,
    even if that function doesn't change the structure of that subargument,
    because the input of the recursive call is no longer exactly the syntactic subargument.
    For instance, a recursive function on lists cannot first map over the sublist,
    even though the usual mapping function doesn't change the length of a list
    and therefore presents no threat to termination.
  \item Some proof assistants (Coq, for one) will inline functions and even unfold recursive functions
    so that the guardedness check has more information
    and that more recursive functions pass the check.
    However, this too has its disadvantages:
    \begin{itemize}
      \item Programs become nonmodular and noncompositional:
        not only do recursive functions that require inlining
        depend on the implementation of inlined functions,
        they may also depend on their specific implementation details,
        and even a minor syntactic change otherwise equivalent to the original
        can break guardedness~\citep{CIC-hat-minus}.
      \item As aptly summarized by \citet{coqterm},
        \begin{quote}
        \begin{singlespace}
        \textit{{\rm [\ldots]} unfold{\rm [ing]} all the definitions used in the body of the function, do{\rm [ing]} reductions, e.t.c.
        {\rm [\ldots]} makes typechecking extremely slow at times.
        Also, the unfoldings can cause the code to bloat by orders of magnitude and become impossible to debug.}
        \end{singlespace}
        \end{quote}
    \end{itemize}
\end{itemize}

In cases where the guardedness check fails on terminating functions,
the programmer can refactor the function to recur on a different, related argument,
such as the length of a list in the example above,
but this requires additional work and may bloat programs
with extra code solely for satisfying the guardedness check
that detracts from the programs' original intent.
Alternatives to the syntactic guardedness check that avoid these issues are type-based checks,
where the type system itself is augmented so that successful type checking
immediately guarantees termination and consistency.

The one this thesis focuses on is \emph{sized typing}\punctstack{,}%
\footnote{The other common one uses \emph{guarded types}~\citep{guarded-types}.}
where inductive types are annotated with size information,
and constructors construct constructions whose size is larger than that of their subarguments.
In short, the notion of a ``smaller'' subargument is encoded within the types,
no longer requiring syntactic analysis to determine.
Finally, a recursive function is well typed only if it recurs on an argument
whose size is smaller than that of the original argument.

Because well-typedness is the only required condition,
calling some other function before a recursive call is allowed
as long as that function is \emph{size preserving}\index{size preservation}.
For the list mapping example above,
this means that if the sublist of elements of type $\tau$ has size $s$,
then the mapping function must have type $\arr*{\List{\tau}{s}}{\List{\tau}{s}}$.
Notice that the implementation of the mapping function isn't required,
merely its type, restoring modularity and compositionality.

This thesis then introduces Sizey \textsc{mc}Type Theory (\lang),
a sized dependent type theory that I prove to be logically consistent
and therefore suitable for both recursive programming and theorem proving.

\section{Overview}

But what need is there for yet another sized type system?
Since \citet{hughes}, there has been a little over two decades' worth of past work on sized types.
\citet{flationary} notes that it has been the topic of at least five dissertations.
This chapter alone names a dozen different type systems with sized types.
There have been many advancements along the way,
but none quite satisfactory to close the matter,
especially when it comes to dependent types.

\lang and this thesis as a whole does not aim to resolve
all of the remaining problems of sized types left open by these past works.
Instead, the purpose is threefold:

\begin{itemize}
  \item To fill an existing gap in sized dependent type theories.
    Two modern sized type features are higher-rank and bounded size quantification,
    and there exist a few dependent type theories with one or the other,
    but not both, to my knowledge.
    On the other hand, they are currently in Agda's implementation of sized types.
    By proving the consistency of \lang, a novel type theory with these two features,
    I bring the theoretical state-of-the-art closer to practice. \\
  \item To demonstrate the viability of using a syntactic model to prove consistency
    of a sized type theory.
    Doing so from scratch through a set-theoretic model is notoriously difficult,
    and often requires sacrifices to and constraints on the type theory;
    see Sacchini's dissertation~\citep{CIC-hat-minus} or \CIChatstar~\citep{CIC-hat-star},
    for instance.
    Instead, a syntactic model relies on the consistency of the language in which I'm modelling,
    allowing me to focus on only the interesting parts of the sized type theory
    rather than on consistency as a whole.
  \item To reopen the discussion on sized dependent types.
    Since the discovery of the inconsistency of Agda's sized types~\citep{infinity},
    progress appears to have stagnated a little.
    In \cref{ch:discussion}, I examine these difficulties as they might apply to \lang
    and its interpretation in the syntactic model.
\end{itemize}

In this section I describe and justify the design of
the dependent types and the sized types of \lang,
explain how a syntactic model is used to prove its consistency,
and briefly outline the structure of the proof.

\subsection{Dependent types}

As a simple but expressive foundation for dependent types,
I start with the Generalized Calculus of Constructions (\GCC)\index{Calculus of Constructions!Generalized \textasciitilde} \citep{GCC-Coquand},
which has the following features:

\begin{itemize}
  \item \textbf{Dependent function types}, as is standard in the Calculus of Constructions (CC)~\citep{CoC}\index{Calculus of Constructions};
  \item \textbf{Definitions}, \ie locally-named expressions;
  \item \textbf{Universes \ala Russell}\index{universes \ala Russell}, where the types of types are themselves terms
    (as opposed to universes \ala Tarski\index{universes \ala Tarski}, where their \emph{encodings} are terms);
  \item An \textbf{impredicative universe}\index{impredicativity} $\Prop$ such that function types into types in $\Prop$
    are themselves in $\Prop$;
  \item A \textbf{cumulative hierarchy of universes}\index{cumulativity} such that $\Type{i}: \Type{i+1}$,
    any term in $\Type{i}$ is also in $\Type{j}$ given \emph{universe levels}\index{universe level} $i \leq j$,
    and there is a subtyping relation\index{subtyping} on types induced by this inclusion; and
  \item \textbf{Untyped definitional equality} stating when two terms are judgementally equal to one another.
\end{itemize}

These features cover many modern proof assistants.
To name a few, in terms of universes,
Coq and Arend have all of the above;
Lean lacks cumulativity; and
Agda and \Fstar lack cumulativity and an impredicative universe.
On the other hand, these proof assistants all have some form of
\emph{universe level polymorphism},
but this is much more complex and largely orthogonal to sized types
and the syntactic model.

Perhaps the most contentious design decision so far is the use of untyped equality,
as can be found in Coq, over typed equality, as can be found in Agda.
While typed equality is considered to ``have clearer mathematical semantics''~\citep{typed-NbE},
its judgement depends on the typing judgement;
meanwhile, since types can depend on terms,
typing itself depends on equality to check whether one type can be used in place of another.
As we'll see in \cref{sec:syntactic-model},
these mutually-defined judgements would greatly complicate the proofs for the syntactic model,
so I settle for untyped equality instead.

On top of \GCC, I add two inductive definitions featured in Martin--L\"of type theory (MLTT)~\citep{MLTT}\index{Martin--L\"of type theory}:
the \emph{Peano naturals} and \emph{well-founded trees}\punctstack{,}%
\footnote{The types of well-founded trees are also known as \emph{W types}.}
augmented with sizes.
As the simplest nontrivial inductive,
the naturals make it easy to demonstrate intuitive uses of sized inductives.
On the other hand, well-founded trees are an example of \emph{generalized} inductives,
with recursive arguments that are functions that return well-founded trees.
They can encode all (nonnested) inductives,
as well as their induction principles \citep{whynotW} if there are dependent pair types
and a \emph{propositional equality}\index{propositional equality} type.
I don't add inductive types in general in their place
because the syntactic baggage that comes with handling the generalization
obscures the intuition behind sized inductive types and the syntactic model,
while it's easy to see how one \emph{could} go from the naturals and well-founded trees
to inductive types in general.

\subsection{Sized types}\label{sec:sized-types}

Sized types begin with explicit size quantification $\Funtype{\alpha}{\tau}$,
size abstraction $\Fun{\alpha}{e}$, and size application $\App{e}{s}$.
Sizes themselves consist of size variables, a \emph{base size},
and a \emph{size successor operator}.
This is the standard for size expressions in sized type systems.
Some augment the size grammar with addition of or scalar multiplication of size variables,
for instance, increasing expressivity at the expense of added complexity in size checking.
To keep things simple, I don't include these features and stick to successor sizes.
Size expressions here are \emph{not} terms,
and their quantifications, abstractions, and applications
are syntactically distinct from those of terms,
similar to how, in nondependent polymorphic type systems,
types are distinct from terms.

Having explicit sizes differs from some most prior sized type systems where,
extending the type polymorphism analogy,
there is only implicit \emph{rank-1} or
\emph{prenex} size quantification:
size quantifications never appear inside of a type,
and in fact all size abstractions and applications are fully inferred.
Explicit sizes, in contrast, let us express
\emph{higher-rank} size quantification,
which allow for more expressiveness:
for instance, supposing we have cons lists parametrized over some sized type $\tau$,
one could write a size-preserving mapping function over a list
that leaves the size of its elements untouched.
The type of such a function might be

\vspace{-0.25\baselineskip}
$$\Funtype{\alpha}{\arr*{(\Funtype{\beta}{\arr*{\App{\tau}{\beta}}{\App{\tau}{\beta}}})}{\app{\List*}{(\App{\tau}{\alpha})}}{\app{\List*}{(\App{\tau}{\alpha})}}}.$$

Along with higher-rank sizes, I also include \emph{bounded} size quantification $\Funtype<{\alpha}{s}{\tau}$
and abstraction $\Fun<{\alpha}{s}{e}$.
An order on sizes is induced by these bound instantiations and the successor operator;
this order has nothing to do with subtyping,
and in particular we do \emph{not} have subtyping relations between
$\N{\alpha}$ and $\N{\sss{\alpha}}$, for instance.
Fixpoint expressions recur on smaller sizes according to the order,
summarized by the below typing rule.
%
\begin{mathpar}
\inferrule[]{
  \check{\Phi, \alpha; \Gamma, f: \Funtype<{\beta}{\alpha}{\subst{\tau}{\alpha}{\beta}}}{e}{\tau}
}{
  \infer{\Phi; \Gamma}{\fix{f}{\alpha}{\tau}{e}}{\Funtype{\alpha}{\tau}}
}
\end{mathpar}

Bounded sizes were originally introduced to avoid inconsistencies
from pattern-matching on sizes~\citep{MiniAgda}.
Although \lang has no pattern-matching mechanism,
this style of recursion is more elegant because it corresponds neatly to well-founded induction on sizes,
and because it does away with complex \emph{semi-continuity} or approximative \emph{polarity}
requirements on fixpoints' types that would otherwise be needed for consistency.
Indeed, to \citet{flationary},

\begin{quote}
\begin{singlespace}
\textit{A technical condition like semi-continuity can kill a system
as a candidate for the foundation of logics and programming
{\rm [\ldots]} Most systems for type-based termination replace semi-continuity by a rough approximation,
trading expressivity for simplicity}.
\end{singlespace}
\end{quote}

In summary, the sized type features I include are:

\begin{itemize}[noitemsep]
  \item \textbf{Explicit size} quantification $\Funtype{\alpha}{\tau}$,
    abstraction $\Fun{\alpha}{e}$, and
    application $\App{e}{s}$;
  \item A \textbf{simple size grammar} with size variables $\alpha$, a base size $\circ$, and successors $\sss{s}$;
  \item \textbf{Higher-rank sizes}, \eg $\arr*{(\Funtype{\alpha}{\tau})}{\sigma}$;
  \item \textbf{Bounded size} quantification $\Funtype<{\alpha}{s}{\tau}$ and
  abstraction $\Fun<{\alpha}{s}{e}$.
\end{itemize}

Notably, these are all features found in Agda's implementation of sized types.
On the other hand, \lang excludes Agda's \emph{infinite size}\index{infinite size}
that is strictly larger than all sizes,
since this property is known to be inconsistent in Agda.
A demonstration of the inconsistency of a hypothetical infinite size in \lang
is given in \cref{sec:infinity}.
On the other hand, the lack of an infinite size prevents
certain infinitary constructs from being expressed;
\cref{sec:examples:limitations} gives a concrete example.

\subsection{Syntactic model}\label{sec:syntactic-model}

To show that \lang is consistent and therefore a suitable type theory for proofs,
I define for it a \emph{syntactic model}\index{syntactic model}~\citep{syntactic-models}.
This involves defining a translation from \lang into some target type theory
in whose consistency we have more confidence---in this case,
the \emph{Extensional Calculus of Inductive Constructions}\index{Calculus of Inductive Constructions!Extensional \textasciitilde}
(\CICE)~\citep{CICE}.
It, too, has a cumulative hierarchy of universes \ala Russell and an impredicative universe,
augmented with inductive types and \emph{equality reflection}\index{equality reflection},
where a definitional equality between terms can be derived from a propositional one between them.
The most notable difference from \lang, aside from these features and sized types,
is that \CICE uses a typed, declarative equality rather than an untyped, algorithmic equality
to better accommodate equality reflection.
For concision and to avoid confusion, I henceforth refer to the former as \emph{equivalence}\index{equivalence},
and to the latter as \emph{conversion}\index{conversion}.

This translation from \lang to \CICE must be \emph{type preserving}\index{type preservation}%
\footnote{This terminology comes from compilation;
in the context of syntactic modelling,
this is known as \emph{soundness} with respect to the translation target.}:
if some term $e$ is well typed under some environments $\Phi; \Gamma$ with some type $\tau$,
then the translated term $\compile{e}$ must also be well typed
under the translated environment $\compile{\Phi}, \compile{\Gamma}$
with the translated type $\compile{\tau}$.
By the consistency of \CICE and a type-preserving translation to it,
we prove the consistency of \lang.

\begin{postulate}[Consistency of \CICE]\label{fact:consistency-cice}
There exists no term $\eT$ such that
\mbox{$\type{\mt}{\eT}{\funtypeT{\PT}{\PropT}{\PT}}$}.
\end{postulate}

\begin{theorem}[Consistency of \lang]\label{thm:overview:consistency}
Suppose $\compile{\bot} = \funtypeT{\PT}{\PropT}{\PT}$.
Then there exists no term $e$ such that \mbox{$\type{\mt \mathbin{;} \mt}{e}{\bot}$}.
\end{theorem}
\begin{proof}
Suppose that there were such a term $e$.
By the type-preserving translation, we would have that
$\type{\mt}{\compile{e}}{\funtypeT{\PT}{\PropT}{\PT}}$ holds.
However, this contradicts \cref{fact:consistency-cice},
so there must not be such a term.
\end{proof}

What remains, then, is to define $\bot$ and an appropriate translation from \lang to \CICE
such that $\compile{\bot} = \funtypeT{\PT}{\PropT}{\PT}$,
and to show that this translation is type preserving.
I define my translation by induction on the typing derivations of \lang
rather than merely over its syntax,
ensuring that only well-typed terms need to be considered.

Type preservation of the translation is proven by induction on not only the typing derivations,
but also the derivations of the judgements on which the typing rules depend.
In particular, typing depends on subtyping\index{subtyping} $\preccurlyeq$,
which in turn depends on \emph{$\alpha$-cumulativity}\index{$\alpha$-cumulativity}
$\sqsubseteq$~\citep{MetaCoq}
and on the reflexive, transitive closure of reduction $\rhd^*$.
Finally, this closure of reduction depends on the reduction rules $\rhd$,
most of which are defined by substitution.
Below are the relevant rules showing these dependencies and an example reduction rule.
These judgements will be explained in detail shortly in the next chapter;
I present them here only to motivate the proof structure.
%
\begin{mathpar}
\inferrule[]{
  \dots \\\\
  \infer{\Phi; \Gamma}{e}{\sigma} \\\\
  \subtype{\Phi; \Gamma}{\sigma}{\tau}
}{
  \check{\Phi; \Gamma}{e}{\tau}
}

\inferrule[]{
  \acum{\sigma_1}{\sigma_2} \\\\
  \red*{\Phi; \Gamma}{\tau_1}{\sigma_1} \\\\
  \red*{\Phi; \Gamma}{\tau_2}{\sigma_2}
}{
  \subtype{\Phi; \Gamma}{\tau_1}{\tau_2}
}

\inferrule[]{
  \red{\Phi; \Gamma}{e_1}{e_2}
}{
  \red*{\Phi; \Gamma}{e_1}{e_2}
}

\inferrule[]{~}{
  \red{\Phi; \Gamma}{\app{(\fun{x}{\tau}{e})}{e'}}{\subst{e}{x}{e'}}
}
\end{mathpar}

We therefore need some lemmas showing that the translation respects
substitution, reduction, the closure of reduction, $\alpha$-cumulativity, and subtyping
to prove type preservation.
In short, substitution satisfies a \emph{compositionality}\index{compositionality} principle,
reduction of terms are equivalent by the \CICE equivalence judgement
$\defeq{\GammaT}{\eT_1}{\eT_2}{\tauT}$,
and $\alpha$-cumulative terms and subtypes are correspondingly \CICE subtypes.
I summarize them here, omitting some hypotheses and sublemmas,
solely to outline the proof architecture,
which roughly follows the structure of type-preserving compilation by~\citet{wjb}.

\begin{lemma}[Compositionality]\label{lem:overview:compositionality}\hfill
\begin{enumerate}[noitemsep]
  \item $\compile{\subst{e}{x}{e'}} = \subst{\compile{e}}{x}{\compile{e'}}$.
  \item $\compile{\subst{e}{\alpha}{s}} = \subst{\compile{e}}{\alpha}{\compile{s}}$
\end{enumerate}
\end{lemma}

\begin{proof}
By induction on the derivation of $\type{\Phi; \Gamma}{e}{\tau}$.
\end{proof}

\begin{lemma}[Preservation of reduction]\label{lem:overview:pres-red}
If $\red{\Phi; \Gamma}{e}{e'}$ and
$\type{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\tauT}$
then $\defeq{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\compile{e'}}{\tauT}$.
\end{lemma}

\begin{proof}
By cases on the derivation of $\red{\Phi; \Gamma}{e}{e'}$,
using inversion on the derivation of $\type{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\compile{\tau}}$,
\cref{lem:overview:compositionality},
and an application of equality reflection in the case for reduction of fixpoints.
\end{proof}

\begin{lemma}[Preservation of reflexive, transitive closure of reduction]\label{lem:overview:pres-red*}
If $\red*{\Phi; \Gamma}{e}{e'}$ and
$\type{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\tauT}$
then $\defeq{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\compile{e'}}{\tauT}$.
\end{lemma}

\begin{proof}
By induction on the derivation of $\red*{\Phi; \Gamma}{e}{e'}$,
using \cref{lem:overview:pres-red},
subject reduction of \lang,
and subject equivalence of \CICE.
\end{proof}

\begin{lemma}[Preservation of $\alpha$-cumulativity]\label{lem:overview:pres-acum}
If $\acum{\tau_1}{\tau_2}$ and
$\type{\compile{\Phi}\compile{\Gamma}}{\compile{\tau_i}}{\UT_i}$
then $\subtype{\compile{\Phi}\compile{\Gamma}}{\compile{\tau_1}}{\compile{\tau_2}}$.
\end{lemma}

\begin{proof}
By induction on the derivation of $\acum{\tau_1}{\tau_2}$,
using inversion on the derivations of $\type{\Phi; \Gamma}{\tau_i}{U_i}$ and
$\type{\compile{\Phi}\compile{\Gamma}}{\compile{\tau_i}}{\UT_i}$.
\end{proof}

\begin{lemma}[Preservation of subtyping]\label{lem:overview:pres-subtyping}
If $\subtype{\Phi; \Gamma}{\tau_1}{\tau_2}$
and $\type{\compile{\Phi}\compile{\Gamma}}{\compile{\tau_i}}{\compile{U}}$
then $\subtype{\compile{\Phi}\compile{\Gamma}}{\compile{\tau_1}}{\compile{\tau_2}}$.
\end{lemma}

\begin{proof}
By cases on the derivation of $\subtype{\Phi; \Gamma}{\tau_1}{\tau_2}$,
using \cref{lem:overview:pres-red*}, \cref{lem:overview:pres-acum},
subject reduction of \lang,
and subject equivalence of \CICE.
\end{proof}

\begin{theorem}[Type preservation]\label{lem:overview:pres-typing}\hfill
\begin{enumerate}[noitemsep]
  \item If $\wf{\Phi}{\Gamma}$ then $\wf{}{\compile{\Phi}\compile{\Gamma}}$.
  \item If $\type{\Phi; \Gamma}{e}{\tau}$ then $\type{\compile{\Phi}\compile{\Gamma}}{\compile{e}}{\compile{\tau}}$.
\end{enumerate}
\end{theorem}

\begin{proof}
By mutual induction on the derivations of $\wf{\Phi}{\Gamma}$ and $\type{\Phi; \Gamma}{e}{\tau}$,
using \cref{lem:overview:pres-subtyping}.
\end{proof}

The most important properties required of \lang are subject reduction\index{subject reduction},
used in \cref{lem:overview:pres-subtyping},
and confluence\index{confluence}, which is used to show that subtyping is transitive,
which in turn is used to prove the inversion principles for the typing judgement.

This proof structure is possible by virtue of the non-mutuality of the judgements of \lang
due to the use of untyped conversion\index{conversion} (and hence untyped reduction).
If it were instead typed, then it would mutually depend on typing and subtyping as well,
and proving the above lemmas would require a seven-part mutual induction on the derivations.

\section{Related Work}

The history and development of sized types spans over two decades of past work,
and that of dependent types far more than that.
I therefore mention here only past work that are directly relevant
or that I refer to again in later chapters.

\subsection{Dependent type theories}

\GCC\index{Calculus of Constructions!Generalized \textasciitilde}
was originally proposed by \citet{GCC-Coquand},
adding a universe hierarchy to CC\index{Calculus of Constructions} with untyped equality
and using \rref{cum} for cumulativity\index{cumulativity}.
The \emph{Extended Calculus of Constructions}\index{Calculus of Constructions!Extended \textasciitilde}
(ECC) by \citet{ECC} adds a dependent pair type
and uses a subtyping relation instead for cumulativity;
\rref{untyped-subtype-prop, untyped-subtype-pi} are the most notable subtyping rules.

\vspace{-\baselineskip}
\begin{mathpar}
\inferrule[\rlabel*{cum}]{
  \type{\Gamma}{e}{\Type{i}}
}{
  \type{\Gamma}{e}{\Type{i+1}}
}
\and
\inferrule[\rlabel{$\preccurlyeq$-prop}{untyped-subtype-prop}]{~}{
  \subtype{\Gamma}{\Prop}{\Type{i}}
}
\and
\inferrule[\rlabel{$\preccurlyeq$-pi}{untyped-subtype-pi}]{
  \Gamma \vdash \sigma_1 \approx \sigma_2 \\
  \subtype{\Gamma, \annot{x}{\sigma_2}}{\tau_1}{\tau_2}
}{
  \subtype{\Gamma}{\funtype{x}{\sigma_1}{\tau_1}}{\funtype{x}{\sigma_2}{\tau_2}}
}
\end{mathpar}

\citet{universes} introduce ways of adding universe polymorphism to \GCC
as well as to a version of \GCC with definitions.
For the purposes of this thesis, I use \GCC to refer to the type theory
with subtyping and definitions.

The \emph{Calculus of Inductive Constructions}\index{Calculus of Inductive Constructions} (CIC) \citep{CIC}
adds inductive types to CC,
while the \emph{Predicative Calculus of Inductive Constructions}\index{Calculus of Inductive Constructions!Predicative \textasciitilde} (pCIC)
further adds definitions and a cumulative universe hierarchy via subtyping\punctstack{.}%
\footnote{More precisely, pCIC refers to Coq's core CIC with the cumulative universe hierarchy
and without an \emph{impredicative $\Set$} universe from version 8 onwards \citep[Chapter~4]{Coq-manual}.
The Coq Reference Manual from version 8.5 onwards refers to it simply as CIC,
as do many others, \eg \citet{CIC-unifier}.
I prefer to use the specific name pCIC,
since most seem to use CIC to refer to whatever Coq's core calculus happens to be based on at the moment,
which is currently pCuIC.}
\citet{pCIC} give a set-theoretic model for a variant of pCIC with typed equality.
The \emph{Predicative Calculus of Cumulative Inductive Constructions}
\index{Predicative Calculus of Cumulative Inductive Constructions} (pCuIC) \citep{pCuIC}
further extends pCIC with additional cumulativity\index{cumulativity} between inductive types,
also providing a set-theoretic model,
and it serves as the modern core calculus of Coq.
Closely related is the MetaCoq project~\citep{MetaCoq}\index{MetaCoq},
which mechanizes pCuIC and various of its metatheorems within Coq itself,
with additional alternate untyped equality and subtyping judgements,
since Coq's implementation of definitional equality is untyped.

\citet{CCE} adds a propositional equality type with
equality reflection\index{equality reflection} to \GCC with \rref{cum} in \CCE,
although transitivity of definitional equality only holds for well-typed terms,
and gives a syntactic model in an extension of CIC.
Following that, \citet{CICE} improve upon the translation,
using ECC with a propositional equality type, typed equality,
and two extensional axioms,
translating from this type theory with equality reflection (ETT) to one without (ITT).
They implement the translation in Template Coq \citep{TemplateCoq},
extending it to include inductive types.

In contrast to these type systems based on CC,
MLTT\index{Martin--L\"of type theory} is a dependent type theory
with a noncumulative universe hierarchy without $\Prop$,
typed equality, and some basic types: the empty type, the unit type,
dependent pair types, disjoint sum types, and a propositional equality type,
as well as the types of naturals and well-founded trees as mentioned.
They can all be implemented as inductive types in pCIC.

The source language \lang and target language \CICE in this thesis aren't exactly
a single type theory from the literature augmented with sized types.
In short, \lang is based on \GCC but with MetaCoq's alternate subtyping rules
and augmented with naturals and well-founded trees,
while \CICE is based on pCIC with extensionality inspired by ETT.

\subsection{Sized type systems}

Sized dependent type systems date all the way back to \CCR by \citet{CCR},
although its sized types are formulated quite differently from ``modern'' sized types.
The first sized dependent type theory recognized as such is \CIChat by \citet{CIC-hat},
which adds prenex sizes
and a size inference algorithm to CIC.
In the intervening years, several nondependent sized type systems were developed,
notably the ML-like type system by \citet{hughes} (independently of \CCR),
\lambdahat~\citep{lambda-hat, lambda-hat-diss},
\Fhat~\citep{F-hat}, and
\Fhattimes~\citep{F-hat-times},
all of which focus on prenex, fully-inferrable sizes.
\Fhatomega~\citep{Abel-diss}, on the other hand,
has higher-rank, explicitly-quantified sizes.
Other sized type systems and more extensive discussions can be found in dissertations by
\citet{lambda-hat-diss} and \citet{Abel-diss}.

\begin{table}[h]
\centering
\iffalse
\begin{tabular}{l c c c l}
Type system & Explicit sizes? & Higher-rank? & Bounded? & Size algebra \\
\hline
\citet{hughes} & \crossmark & \crossmark & \crossmark & $\sss{s}, s + s, n \times s$ \\
\lambdahat~\citep{lambda-hat, lambda-hat-diss} & \crossmark & \crossmark & \crossmark & $\sss{s}$ \\
\Fhat~\citep{F-hat} & \crossmark & \crossmark & \crossmark & $\sss{s}$ \\
\Fhattimes~\citep{F-hat-times} & \crossmark & \crossmark & \crossmark & $\sss{s}, s + s$ \\
\Fhatomega~\citep{Abel-diss} & \checkmark* & \checkmark* & \crossmark & $\sss{s}$ \\
\Fcopomega~\citep{F-omega-cop} & \checkmark* & \checkmark* & \checkmark* & $\sss{s}$ \\
\end{tabular}
\fi

\begin{tabular}{l c c c c}
Type theory & Explicit sizes? & Higher-rank? & Bounded? & Consistent? \\
\hline
\CIChat~\citep{CIC-hat} & \crossmark & \crossmark & \crossmark & \interromark \\
\CIChatminus~\citep{CIC-hat-minus-nat, CIC-hat-minus} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CChatomega~\citep{CC-hat-omega} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CIChatl~\citep{CIC-hat-l} & \crossmark & \crossmark & \crossmark & \checkmark* \\
\CIChatsub~\citep{CIC-hat-sub} & \crossmark & \crossmark & \checkmark* & \checkmark* \\
\CIChatstar~\citep{CIC-hat-star} & \crossmark & \crossmark & \crossmark & \interromark \\
\citet{NbE} & \checkmark* & \checkmark* & \crossmark & \checkmark* \\
MiniAgda~\citep{MiniAgda, flationary} & \checkmark* & \checkmark* & \checkmark* & \crossmark \\
\textbf{\lang} & \checkmark* & \checkmark* & \checkmark* & \checkmark*%
\textsuperscript{\labelcref{foot:CICE-consistency}}
\end{tabular}
\caption{Sized dependent type theories and their properties}
\label{tab:sized-types}
\end{table}

\cref{tab:sized-types} lists the sized dependent type theories that follow \CIChat,
along with the main features I focus on:
explicit, higher-rank, bounded size quantification,
and with whether the type theory has been proven consistent.
The type theory of MiniAgda, on which the sized types implemented in Agda are based,
is the only prior one with all three sized type features,
but is known to be inconsistent~\citep{infinity}.
(Interestingly, \Fcopomega~\citep{F-omega-cop}, which extends System F$_\omega$ with (co)inductive types and sized types,
has all three properties \emph{and} has been proven to be strongly normalizing.)
One of the goals of \lang, as mentioned, is to fill in this gap
with a consistent sized dependent type theory with all of the desired features.
However, it's missing two properties that all of these type theories do have:
inductive types in the ``correct'' universes
(as we'll see shortly, the type of naturals and well-founded trees
are one universe higher than is conventional),
and the existence of an infinite size.

% TODO: fix manual footnote number
\footnotetext[6]{\label{foot:CICE-consistency} Under the postulate that \CICE is consistent.}

\begin{center}
\mbox{* * *}
\end{center}
\vspace*{-0.5\baselineskip}

\noindent In the upcoming \cref{ch:sized-dep-types}, I describe the syntax and judgements of \lang in detail
and provide example programs in \lang that use sized types.
Next, in \cref{ch:model} I describe \CICE and define the translation from \lang to \CICE.
Following that, in \cref{ch:proofs} I prove various necessary metatheoretical properties
including confluence and subject reduction,
and elaborate on the proof of type preservation and its associated lemmas.
I conclude in \cref{ch:discussion}, discussing some of the shortcomings of \lang,
namely the universe levels of the inductive types and
decreased expressivity arising from removing the infinite size,
as well directions for future investigation.


%---------------------------------------------------------------------
%  Writeup of the improved search of Run II data for single top quark
%  production at DZero.
%  Started: Oct 2006
%  Authors: The Single Top Working Group
%---------------------------------------------------------------------
%
\section{Signal Acceptances}
\label{signal-acceptances}

The signal acceptance is defined as:
$$
\cal{A} = \frac{\cal{B}}{N_{\rm initial}}
\displaystyle \sum_{N_{\rm selected}} \varepsilon_{\rm trigger}
\varepsilon_{\rm corrections} \varepsilon_{\rm TRF}
$$
\noindent where $\cal{B}$ is the branching fraction for the MC
sample, see Table~\ref{mcstats}, $N_{\rm initial}$ is the initial
number of events in each MC sample (also shown in
Table~\ref{mcstats}), $N_{\rm selected}$ is the number of MC events
remaining after selection, and the $\varepsilon$'s are efficiency
correction factors to account for differences between data and MC from
the trigger, particle ID, the $b$-tagging tag-rate functions, and so
forth (see Section~\ref{mc-modeling}).

Table~\ref{acceptances} shows the percentage of each single top quark
signal that remain after selection, and after $b$-tagged jets have
been required. We can compare the acceptances obtained in this
analysis with the published one~\cite{run2-d0-plb}. In that analysis,
we kept 2.6\% of the s-channel events before tagging (with 2--4 jets)
in the electron channel and 2.5\% in the muon channel. In this
analysis, we keep 2.8\% and 2.2\% in the electron and muon channels
respectively (2--4 jets). In the t-channel, in the published analysis
we kept 2.6\% and 2.4\% in the electron and muon channels, and now we
are keeping 2.5\% and 2.0\%. After $b$~tagging, before we kept 1.3\%
of the electron events and 1.3\% of the muon events in the s-channel;
we now keep 1.8\% and 1.4\% respectively. In the t-channel after
$b$~tagging, we kept before 0.9\% in each of the electron and muon
channels. We now keep 1.2\% and 0.9\% of these tagged events.  The
higher acceptances now are from the use of the more efficient neural
network $b$-tagging algorithm, and probably also from the lower
reconstruction threshold for jets.

% The following is commented out until the comparison is updated to
% be for their latest results.
%
%We can also compare these acceptances to those obtained by CDF in
%their most recent analysis. For s-channel and t-channel combined,
%electron and muon channels combined, and events with exactly two jets
%with one or both $b$~tagged, they keep 1.4\% of the expected
%signal. For the same requirements, we also keep 1.4\% of the expected
%signal. In addition we have available to analyze the acceptances in
%the 1, 3, and 4-jets channels, and the untagged 2, 3, and 4-jets
%channels, which CDF have not yet included in their results.

\vspace{0.2in}
\begin{table}[!h!tbp]
\begin{center}
\begin{minipage}{6.5in}
\begin{ruledtabular}
\begin{tabular}{l||ccccc|ccccc}
\multicolumn{11}{c}{\hspace{1in}\underline{Signal Acceptances}}\vspace{0.1in} \\
& \multicolumn{5}{c|}{Electron Channel} & \multicolumn{5}{c}{Muon Channel} \\
                     & 1 jet & 2 jets & 3 jets & 4 jets & 5+ jets
                     & 1 jet & 2 jets & 3 jets & 4 jets & 5 jets \\
\hline
Before $b$ tagging   &        &        &        &        &        &        &        &        &        &        \\
~~$tb$               & 0.55\% & 1.77\% & 0.83\% & 0.23\% & 0.06\% & 0.33\% & 1.36\% & 0.69\% & 0.19\% & 0.05\% \\
~~$tqb$              & 0.52\% & 1.49\% & 0.79\% & 0.25\% & 0.07\% & 0.36\% & 1.17\% & 0.64\% & 0.20\% & 0.05\% \\
Zero $b$-tagged jets &        &        &        &        &        &        &        &        &        &        \\
~~$tb$               & 0.32\% & 0.65\% & 0.31\% & 0.09\% & 0.02\% & 0.18\% & 0.48\% & 0.25\% & 0.07\% & 0.02\% \\
~~$tqb$              & 0.34\% & 0.86\% & 0.41\% & 0.12\% & 0.03\% & 0.23\% & 0.66\% & 0.32\% & 0.09\% & 0.02\% \\
One $b$-tagged jet   &        &        &        &        &        &        &        &        &        &        \\
~~$tb$               & 0.24\% & 0.82\% & 0.39\% & 0.11\% & 0.03\% & 0.15\% & 0.64\% & 0.32\% & 0.09\% & 0.02\% \\
~~$tqb$              & 0.18\% & 0.61\% & 0.34\% & 0.11\% & 0.03\% & 0.13\% & 0.50\% & 0.28\% & 0.09\% & 0.02\% \\
Two $b$-tagged jets  &        &        &        &        &        &        &        &        &        &        \\
~~$tb$               &  ---   & 0.29\% & 0.14\% & 0.04\% & 0.02\% &  ---   & 0.24\% & 0.12\% & 0.03\% & 0.01\% \\
~~$tqb$              &  ---   & 0.02\% & 0.05\% & 0.02\% & 0.01\% &  ---   & 0.01\% & 0.04\% & 0.02\% & 0.01\%
\end{tabular}
\end{ruledtabular}
\vspace{-0.1in}
\caption[acceptances]{Acceptances after selection.}
\label{acceptances}
\end{minipage}
\end{center}
\end{table}


%#!pdflatex Naruse_Esurf_2020.tex
\section{Discussion}

\subsection{Performance of inverse model}
The performance of the inverse model for turbidity currents is evaluated using the test data set, implying that this model can accurately reconstruct the flow characteristics of the turbidity currents from the spatial distribution of the thickness and grain size of turbidites (Figs. \ref{fig:test_scatter_plot} and \ref{fig:test_histogram_deviation}). The biases in the values reconstructed from the true input parameters are also very small and thus should not pose a serious issue when the method is applied to actual field data.

The inverse model not only reconstructed the initial conditions of turbidity currents accurately, but also the predicted time evolution of the flow behavior was sufficiently accurately and precisely. In the results of the forward model calculations using the predicted model input parameters that are relatively deviate from the true values (Table \ref{table:example_time_evolution}), the time evolution of the velocity and the thickness of the flow does not deviate significantly from the results using the true values (Fig. \ref{fig:test_example_time_evolution}).

Turbidity currents have a mechanism called the self-acceleration, which is caused by erosion and associated increase of the flow density \citep{parker1986self,Naruse2007,Sequeiros2009}. Therefore, even slight differences in the initial conditions of the flow can lead to very different results of the time evolution of the flow parameters. However, the results of this test imply that the accuracy of the inverse analysis in this study is enough to prevent to cause such a drastic change in the flow behavior.

The relationship between turbidity currents and characteristics of turbidites is nonlinear. Especially when the flow is self-accelerating, a small difference in the initial conditions can result in very different sedimentary characteristics. This means that it is easy to find the initial conditions of the flow by inverse analysis, because even if the characteristics of the deposits are very different, the initial conditions of the flow should not be so different. Thus, the inverse results in this case are expected to be robust even if there are some measurement errors in characteristics of deposits. In other words, there is a tradeoff between the robustness of the forward and inverse modeling.

This property of the inversion can be understood when we consider the opposite case. If the initial conditions of the flow are different but the characteristics of the turbidites are exactly the same, it is impossible to estimate the flow conditions from the turbidites. The inverse analysis of hydraulic conditions is possible because the depositional characteristics are sensitive to conditions of turbidity currents. The self-acceleration of turbidity flow is an extreme example of the sensitivity of turbidites to the flow initial conditions.

\subsection{Applicability to field-scale problems}
To apply this method to outcrops, the extent of the area that should be surveyed to collect data and the interval between outcrops should be determined. The tests with different sizes of sampling windows suggest that the survey region should be located more than 10 km from the proximal region (Fig. \ref{fig:training_different_number_length}). The loss function (i.e., the MSE of the estimates of the parameters) decreases as the length of the sampling window increases, and the best result is obtained at the 10 km-long window. Regarding the interval of the outcrops, the test results of sampling rates of more than 1.0\% with interpolation for data at non-sampled grids are not inferior to the full sample. Since the training data used in this study are computed on 5 m-spaced grids, extracting data from these grids with a 1.0\% probability is equivalent to conducting an inverse analysis from outcrop data that are distributed at 0.5 km intervals on average. Although the RMSEs of the model prediction certainly increase when the sampling rate decreases below 1.0 \%, the RMSE values does not drastically worsen until 0.5 \%. Therefore, even if the outcrop spacing is about 1 km, it should be possible to obtain a reasonable estimates of the flow characteristics.

These requirements for accurate inversion are attainable in the actual field. For example, \citet{Hirayama1977} correlated individual turbidites of the Pleistocene Otadai Formation distributed in the Boso Peninsula, Japan, on the basis of the key tuff beds. Their correlation covered a region over 30 km long with 33 outcrops. Thus, the average interval between outcrops was approximately 1 km. \citet{Amy2006} correlated individual beds in the Miocene Marnoso Arenacea Formation, Italy, using the Contessa MegaBed and an overlying ``columbine'' marker bed as the key beds. Their correlation covers 109 sections of approximately 30 m thick succession and extends over 120 km in a direction parallel to flow. Other studies in various regions (e.g., the Arnott Sandstone in France) also reported the correlation of individual turbidites in similar scale and frequency \citep{HESSE1974,Tokuhashi1979,Tokuhashi1989,Amy2000,Amy2004}. Furthermore, \citet{Bartolini1972} surveyed the Western Alboran Basin Plain, Mediterranean Sea, and discovered an individual turbidite on the sea floor at 49 cores over approximately 30 km. The records of cores in similar scale and intervals have also been reported by other studies of the modern submarine fans in different areas \citep{BORNHOLD1971, Pilkey1980}. In summary, although the method proposed in this study requires fairly high resolution data of turbidite individual beds correlated over a long distance, such conditions in ancient geological records as well as modern seafloor surveys can be achieved.

Besides these outcrop conditions, measurement errors in the field are another important factor for application. The test results suggest that the proposed inverse model of this study is very robust against random noise; random errors in the measured data have little effect on the results (Fig. \ref{fig:test_noise}). Therefore, even if localized and small-scale scouring and sedimentation occur due to some processes such as bottom currents after the deposition of a turbidite, results of inverse analysis will not be seriously affected. However, if deposits of multiple events are amalgamated to form a single thick massive sandstone, the hydraulic conditions reconstructed from the bed should be considerably different from the actual conditions. To avoid this situation, it is important to identify the erosional surface inside the bed carefully at the actual outcrop. In addition, it is safer not to analyze massive sandstones that are more than several meters thick, because they are likely to be amalgamated deposits.

Perhaps the most significant drawback to analyze actual turbidites is the assumption about the topography of the upstream submarine canyon. In this study, we tested doubling the length of the upstream slope and found that the predicted values for the concentration that were different from the original values (Fig. \ref{fig:test_slope_length}). In case of actual analysis, the upstream topography can be set correctly if the modern submarine fan is analyzed. Regarding ancient turbidites, however, some assumptions about the length and scale of the submarine canyons are necessary without measurements. In this case, it is recommended to set up various lengths of submarine canyons within a reasonable range, and to examine the degree to which these assumptions affect the inverse analysis results carefully. Nevertheless, it is worth noting that the test results were reasonable for velocity (Fig. \ref{fig:test_slope_length}), even if the assumption about the length of upstream slope was substantially different. This suggests that the inverse model proposed in this study can generally reconstruct the behavior of turbidity currents in sedimentary basins, even if the development process of turbidity currents upstream is different.

\subsection{Comparison with previous methodologies}
In existing inverse analysis methods of turbidity currents, the difference in depositional characteristics between the outputs of the forward model and the field observation is quantified as the objective function, and the initial and the boundary conditions of the forward model are determined by conducting optimization calculations to minimize the objective function \cite[e.g.,]{Nakao2017}. This is because models of turbidity currents are generally nonlinear and are difficult to linearize, especially when considering the entrainment of the basal sediment \citep{parker1986self}. Although the actual computational load depends on the choice of algorithm, this type of optimization calculation generally consists of multiple steps, and each step depends on the results of the previous calculation. Thus, the entire optimization procedure is difficult to parallelize. For instance, the kriging-based surrogate management method \citep{lesshafft2011towards} or the genetic algorithm \citep{Nakao2017} have been used to optimize the objective function for inversion of turbidity currents. In these methods, multiple calculations are conducted in each calculation step (generation), and the distribution of the objective function in the parametric space is iteratively estimated. Although the computations within each generation can be parallelized in this kind of algorithms, the next generation's computation depends on the results of the previous generation's computation, and therefore, the entire computation process cannot be parallelized. Thus, if the computational load of the forward model is high, the inverse analysis takes an unrealistic amount of time.

\citet{Parkinson2017} applied the adjoint method with the gradient-based optimization algorithm. Although the differentiation of the layer-averaged model by the adjoint method greatly reduces the load of the gradient calculation, this approach still requires an iterative calculation for optimization. Thus, the sediment entrainment process is omitted from their model. Their model does not consider resuspension (entrainment) process of sediment, whereas suspended sand in turbidity currents is maintained by balancing the effects of particle settling and diffusion from the bottom (i.e. entrainment). Their model only considers advection and settling of particles, so that the suspended sediment quickly settles and be lost over short distances at realistic flow thicknesses and concentrations. The only way to transport large amounts of suspended sediment for long distance and to deposit thick turbidites without resuspension is to make the flow extremely thick or to suppose unusually high velocity or concentration. This is the reason for that the extremely thick flow depth (more than 3000 m) was obtained in their results. Their inversion method requires iterations that cannot be parallelized, so that the forward model needs to be simplified for this purpose. In addition, gradient-based optimization tends to have problems with initial value dependency and escaping from local optimal solutions. For this reason, the results of their inverse analysis of turbidites were quite unrealistic. In contrast, we were able to adopt "full model" that incorporate the entrainment process of suspended sand into our model without any problems. As a result, our inversion did not produce any anomalous reconstructions even though most of our test data exhibit thickness and grain size distributions similar to realistic turbidites. This strongly suggests the robustness of our inverse model and its applicability to real turbidites.

Another potential approach to optimization is the Markov Chain Monte Carlo (MCMC) method, but even with this method, repetition of the forward model calculation is unavoidable, since MCMC usually requires repetition of calculations of objective function, which cannot be parallelized, more than the order of $10^4$ time. The layer-averaged model of unsteady turbidity currents is probably not suitable for the forward models due to their computational load.

The approach proposed in this study is obviously superior to existing methods in terms of applicability to the field, as it allows computationally demanding models to be applied as forward models. The general relationship between the bed and the input parameters is learned by NN rather than adjusting the input parameters of the numerical model to reproduce the characteristics of specific individual beds. The objective function used in the training of this NN is not the difference between the features of the sediment, but the precision of the inverse analysis results themselves. The most computationally demanding part of the inverse analysis method proposed here is the generation of the training data for the NN. However, since the computations of the forward models are completely independent of each other, the generation of the training data can be conducted in parallel. Thus, our method enables us to easily prepare a large number of training data by using PC clusters, even for very computationally demanding forward models. In addition, the number of calculations required for training is not as high as other methods, specifically only approximately 3,000. It is also advantageous that the proposed method enables us to perform various tests for robustness or precision of inversion before application to field examples, because the NN outputs results of inverse analysis extremely fast. For these reasons, we consider that this study successfully generated an inverse model using the layer-averaged model for unsteady turbidity currents that can be applied to the field.

\subsection{Limitations and future tasks}

The inverse model proposed in this study has several limitations. Inevitably, the accuracy of the inverse analysis is governed by the validity of the forward model that generates the training data. The present implementation of the inverse model uses the one dimensional layer-averaged model as the forward model, but this model is likely to be applicable only to sedimentary basins that are laterally constrained or to the inside of the submarine channels. The layer-averaged model of \citet{parker1986self} used in this study has been widely accepted, but various doubts have been recently raised such as the formulation of entrainment rates of basal sediment \citep{Dorrell2018} and ambient seawater \citep{Luchi2018}. The assumption of a lock exchange condition for the occurrence of turbidity currents may not be appropriate in some situations.

Although \citet{Luchi2018} suggested that the a single layer model may not be sufficient for considering behavior of turbidity currents maintained over long distances, it is expected that such turbidity currents do not leave turbidites and create a bypassing zone. Otherwise, the concentration in the lower layers of turbidity currents decrease, and therefore the currents stop within a relatively short distance. Thus, a two-layer model of turbidity currents is not always necessary for inversion of bed-scale turbidites. However, modeling of continuos sustained turbidity currents is necessary for inverse analysis of the development of submarine fans and channel-levee systems in a larger scale.

It is relatively easy to solve these problems described above. Without changing the framework of the proposed method, we can adapt to any situation by changing the forward model to generate the training data. For processes such as sediment transport, it is easy to revise the model to incorporate the state-of-the-art knowledge. By adopting computationally demanding models, inverse analysis using 2-D and 3-D forward models may be possible. In Future research, these issues should be addressed, and the methodology to actual field examples should be applied.

The analysis of ancient turbidites is an important issue in the future. However, even if ancient turbidites are analyzed, it is not possible to verify that the results obtained are correct, because the hydraulic conditions for ancient turbidity currents are unknown. Another way to verify the validity of the method is to reconstruct the hydraulic conditions of experimental turbidity currents from the turbidites deposited in the flume, and compare them with the measured values. The turbidity currents measured in the modern submarine canyons and their deposits would be another candidate to be used for the model verification.