1
0
mirror of https://github.com/dhil/phd-dissertation synced 2026-03-13 11:08:25 +00:00

Interdefinability WIP2

This commit is contained in:
2021-03-16 23:58:14 +00:00
parent 28aaebf5ec
commit 284139375a

View File

@@ -9,6 +9,10 @@
\usepackage{url} \usepackage{url}
\usepackage[sort&compress,square,numbers]{natbib} % Bibliography \usepackage[sort&compress,square,numbers]{natbib} % Bibliography
\usepackage{bibentry} % Print bibliography entries inline. \usepackage{bibentry} % Print bibliography entries inline.
\makeatletter % Redefine bibentry to omit hyperrefs
\renewcommand\bibentry[1]{\nocite{#1}{\frenchspacing
\@nameuse{BR@r@#1\@extra@b@citeb}}}
\makeatother
\nobibliography* % use the bibliographic data from the standard BibTeX setup. \nobibliography* % use the bibliographic data from the standard BibTeX setup.
\usepackage{breakurl} \usepackage{breakurl}
\usepackage{amsmath} % Mathematics library \usepackage{amsmath} % Mathematics library
@@ -11513,9 +11517,11 @@ environments.
% %
\[ \[
\bl \bl
\sdtrans{-} : \TypeCat \to \TypeCat\\ \sdtrans{-} : \HandlerTypeCat \to \HandlerTypeCat\\
\sdtrans{C \Rightarrow D} \defas \sdtrans{A\eff E_1 \Rightarrow B\eff E_2} \defas
\sdtrans{C} \Rightarrow \Record{\UnitType \to \sdtrans{C}; \UnitType \to \sdtrans{D}} \medskip \medskip\\ \sdtrans{A\eff E_1} \Rightarrow \Record{\UnitType \to \sdtrans{C \eff E_1}; \UnitType \to \sdtrans{B \eff E_2}} \eff \sdtrans{E_2} \medskip \medskip\\
% \sdtrans{C \Rightarrow D} \defas
% \sdtrans{C} \Rightarrow \Record{\UnitType \to \sdtrans{C}; \UnitType \to \sdtrans{D}} \medskip \medskip\\
\sdtrans{-} : \CompCat \to \CompCat\\ \sdtrans{-} : \CompCat \to \CompCat\\
\sdtrans{\ShallowHandle \; M \; \With \; H} \defas \sdtrans{\ShallowHandle \; M \; \With \; H} \defas
\ba[t]{@{}l} \ba[t]{@{}l}
@@ -11545,7 +11551,67 @@ Each shallow handler is encoded as a deep handler that returns a pair
of thunks. The first forwards all operations, acting as the identity of thunks. The first forwards all operations, acting as the identity
on computations. The second interprets a single operation before on computations. The second interprets a single operation before
reverting to forwarding. reverting to forwarding.
%
The following example illustrates an application of the translation on
the $\Pipe$ operator from Section~\ref{sec:pipes} applied to the
producer and consumer pair
$\Record{\Rec\;ones\,\Unit.\Do\;\Yield~1;ones\,\Unit;\lambda\Unit.\Do\;\Await\,\Unit
+ \Do\;\Await}$
%
\[
\ba{@{~}l@{~}l}
&\mathcal{D}\left\llbracket
\ba[m]{@{}l}
\ShallowHandle\;(\lambda\Unit.\Do\;\Await\,\Unit + \Do\;\Await\,\Unit)\,\Unit\;\With\\
\quad\ba[m]{@{~}l@{~}c@{~}l}
\Return\;x &\mapsto& \Return\;x\\
\OpCase{\Await}{\Unit}{r} &\mapsto& \Copipe\,\Record{r;\Rec\;ones\,\Unit.\Do\;\Yield~1;ones\,\Unit}
\ea
\ea
\right\rrbracket \medskip\\
=& \bl
\Let\;z \revto \bl
\Handle\;(\lambda\Unit.\Do\;\Await\,\Unit + \Do\;\Await\,\Unit)\,\Unit\;\With\\
\quad\ba[t]{@{~}l@{~}c@{~}l}
\Return\;x &\mapsto& \Return\;\Record{\lambda\Unit.\Return\;x;\lambda\Unit.\Return\;x}\\
\OpCase{\Await}{\Unit}{r} &\mapsto&\\
\multicolumn{3}{l}{
\quad\bl
\Let\;r \revto \lambda x.\Let\;z \revto r~x\;\In\;\Let\;\Record{f;g} = z\;\In\;f\,\Unit\;\In\\
\Return\;\Record{\bl
\lambda\Unit.\Let\;x \revto \Do\;\ell~p\;\In\;r~x;\\
\lambda\Unit. \sdtrans{\Copipe}\,\Record{r;\Rec\;ones\,\Unit.\Do\;\Yield~1;ones\,\Unit}}\el
\el}
\ea
\el\\
\In\;\Let\;\Record{f;g} = z\;\In\;g\,\Unit
\el
\ea
\]
%
Evaluation of both the left hand side and right hand side of the
equals sign yields the value $2 : \Int$. The $\Return$-case in the
image contains a redundant pair, because the $\Return$-case of $\Pipe$
is the identity. The translation of the $\Await$-case sets up the
forwarding component and handling component of the pair of thunks.
The translation commutes with substitution and preserves typability.
%
\begin{lemma}\label{lem:sdtrans-subst}
Let $\sigma$ denote a substitution. The translation $\sdtrans{-}$
commutes with substitution, i.e.
%
\[
\sdtrans{V}\sdtrans{\sigma} = \sdtrans{V\sigma},\quad
\sdtrans{M}\sdtrans{\sigma} = \sdtrans{M\sigma},\quad
\sdtrans{H}\sdtrans{\sigma} = \sdtrans{H\sigma}.
\]
%
\end{lemma}
%
\begin{proof}
By induction on the structures of $V$, $M$, and $H$.
\end{proof}
% \paragraph{Example} We demonstrate the translation $\sdtrans{-}$ on % \paragraph{Example} We demonstrate the translation $\sdtrans{-}$ on
% the $\Pipe$ handler from Section~\ref{sec:shallow-handlers-tutorial} % the $\Pipe$ handler from Section~\ref{sec:shallow-handlers-tutorial}
% (recall that the variables $c$ and $p$ are bound to the consumer and % (recall that the variables $c$ and $p$ are bound to the consumer and
@@ -11556,11 +11622,11 @@ reverting to forwarding.
% \ba{@{~}l@{~}l@{}l} % \ba{@{~}l@{~}l@{}l}
% &\mathcal{D}&\left\llbracket % &\mathcal{D}&\left\llbracket
% \ba[m]{@{~}l} % \ba[m]{@{~}l}
% \ShallowHandle\; c\,\Unit \;\With\; \\ % \lambda\Record{p;c}.\ShallowHandle\; c\,\Unit \;\With\; \\
% \quad % \quad
% \ba[m]{@{}l@{~}c@{~}l@{}} % \ba[m]{@{}l@{~}c@{~}l@{}}
% \Return~x &\mapsto& \Return\; x \\ % \Return~x &\mapsto& \Return\; x \\
% \Await~\Unit~resume &\mapsto& \Copipe\; \Record{resume; p} \\ % \OpCase{\Await}{\Unit}{r} &\mapsto& \Copipe\; \Record{r;p} \\
% \ea \\ % \ea \\
% \ea\right\rrbracket = \\ % \ea\right\rrbracket = \\
% & % &
@@ -11586,7 +11652,6 @@ reverting to forwarding.
% \ea % \ea
% \] % \]
% %
\begin{theorem} \begin{theorem}
If $\Delta; \Gamma \vdash M : C$ then $\sdtrans{\Delta}; If $\Delta; \Gamma \vdash M : C$ then $\sdtrans{\Delta};
\sdtrans{\Gamma} \vdash \sdtrans{M} : \sdtrans{C}$. \sdtrans{\Gamma} \vdash \sdtrans{M} : \sdtrans{C}$.
@@ -11600,18 +11665,20 @@ If $\Delta; \Gamma \vdash M : C$ then $\sdtrans{\Delta};
\newcommand{\approxa}{\gtrsim} \newcommand{\approxa}{\gtrsim}
As with the implementation of deep handlers as shallow handlers, the As with the implementation of deep handlers as shallow handlers, the
implementation is again given by a local translation. However, this implementation is again given by a typability-preserving local
time the administrative overhead is more significant. Reduction up to translation. However, this time the administrative overhead is more
congruence is insufficient and we require a more semantic notion of significant. Reduction up to congruence is insufficient and we require
administrative reduction. a more semantic notion of administrative reduction.
\begin{definition}[Administrative evaluation contexts] \begin{definition}[Administrative evaluation contexts]
An evaluation context $\EC$ is administrative, $\admin(\EC)$, iff An evaluation context $\EC \in \EvalCat$ is administrative,
$\admin(\EC)$, when the following two criteria hold.
\begin{enumerate} \begin{enumerate}
\item For all values $V$, we have: $\EC[\Return\;V] \reducesto^\ast \item For all values $V \in \ValCat$, we have: $\EC[\Return\;V] \reducesto^\ast
\Return\;V$ \Return\;V$
\item For all evaluation contexts $\EC'$, operations $\ell \in BL(\EC) \item For all evaluation contexts $\EC' \in \EvalCat$, operations
\backslash BL(\EC')$, values $V$: $\ell \in \BL(\EC) \backslash \BL(\EC')$, and values
$V \in \ValCat$:
% %
\[ \[
\EC[\EC'[\Do\;\ell\;V]] \reducesto^\ast \Let\; x \revto \Do\;\ell\;V \;\In\; \EC[\EC'[\Return\;x]]. \EC[\EC'[\Do\;\ell\;V]] \reducesto^\ast \Let\; x \revto \Do\;\ell\;V \;\In\; \EC[\EC'[\Return\;x]].
@@ -11658,16 +11725,19 @@ that administrative reduction may occur anywhere within a term.
% %
The following lemma states that the forwarding component of the The following lemma states that the forwarding component of the
translation is administrative. translation is administrative.
%
\begin{lemma}\label{lem:sdtrans-admin} \begin{lemma}\label{lem:sdtrans-admin}
For all shallow handlers $H$, the following context is administrative: For all shallow handlers $H$, the following context is administrative:
\begin{displaymath} %
\[
\Let\; z \revto \Let\; z \revto
\Handle\; [~] \;\With\; \sdtrans{H}\; \Handle\; [~] \;\With\; \sdtrans{H}\;
\In\; \In\;
\Let\; \Record{f;\_} = z\; \In\; f\,\Unit. \Let\; \Record{f;\_} = z\; \In\; f\,\Unit.
\end{displaymath} \]
%
\end{lemma} \end{lemma}
%
\begin{theorem}[Simulation up to administrative reduction] \begin{theorem}[Simulation up to administrative reduction]
If $M' \approxa \sdtrans{M}$ and $M \reducesto N$ then there exists If $M' \approxa \sdtrans{M}$ and $M \reducesto N$ then there exists
$N'$ such that $N' \approxa \sdtrans{N}$ and $M' \reducesto^+ N'$. $N'$ such that $N' \approxa \sdtrans{N}$ and $M' \reducesto^+ N'$.
@@ -11696,8 +11766,8 @@ homomorphic cases and show only the interesting cases.
% %
\[ \[
\bl \bl
\PD{-} : \TypeCat \to \TypeCat\\ \PD{-} : \HandlerTypeCat \to \HandlerTypeCat\\
\PD{\Record{C, A} \Rightarrow^\param B \eff E} \defas \PD{C} \Rightarrow (\PD{A} \to \PD{B \eff E})\eff \PD{E} \medskip\\ \PD{\Record{C; A} \Rightarrow^\param B \eff E} \defas \PD{C} \Rightarrow (\PD{A} \to \PD{B \eff E})\eff \PD{E} \medskip\\
\PD{-} : \CompCat \to \CompCat\\ \PD{-} : \CompCat \to \CompCat\\
\PD{\ParamHandle\;M\;\With\;(q.\,H)(W)} \defas \left(\Handle\; \PD{M}\; \With\; \PD{H}_q\right)~\PD{W} \medskip\\ \PD{\ParamHandle\;M\;\With\;(q.\,H)(W)} \defas \left(\Handle\; \PD{M}\; \With\; \PD{H}_q\right)~\PD{W} \medskip\\
\PD{-} : \HandlerCat \times \ValCat \to \HandlerCat\\ \PD{-} : \HandlerCat \times \ValCat \to \HandlerCat\\
@@ -11705,20 +11775,74 @@ homomorphic cases and show only the interesting cases.
\PD{\{\Return\;x \mapsto M\}}_q &\defas& \{\Return\;x \mapsto \lambda q. \PD{M}\}\\ \PD{\{\Return\;x \mapsto M\}}_q &\defas& \{\Return\;x \mapsto \lambda q. \PD{M}\}\\
\PD{\{\OpCase{\ell}{p}{r} \mapsto M\}}_q &\defas& \PD{\{\OpCase{\ell}{p}{r} \mapsto M\}}_q &\defas&
\{\OpCase{\ell}{p}{r} \mapsto \lambda q. \{\OpCase{\ell}{p}{r} \mapsto \lambda q.
\Let\; r \revto \Return\;\lambda \Record{x,q'}. r~x~q'\; \Let\; r \revto \Return\;\lambda \Record{x;q'}. r~x~q'\;
\In\; \PD{M}\} \In\; \PD{M}\}
\ea \ea
\el \el
\] \]
% %
The parameterised $\ParamHandle$ construct becomes an application of a The parameterised $\ParamHandle$ construct becomes an application of a
$\Handle$ construct to the translation of the parameter. The bodies of $\Handle$ construct to the translation of the parameter. The
return and operation clauses are each enclosed in a lambda abstraction translation of $\Return$ and operation clauses are parameterised by
whose formal parameter is the handler parameter $q$. As a result the the name of the handler parameter as each clause body is enclosed in a
ordinary deep resumption $r$ is a curried function. However, the uses $\lambda$-abstraction whose formal parameter is the handler parameter
of $r$ in $M$ expects a binary function. To repair this discrepancy, $q$. As a result the ordinary deep resumption $r$ is a curried
we construct an uncurried interface of $r$ via the function $r'$. function. However, the uses of $r$ in $M$ expects a binary
function. To repair this discrepancy, we construct an uncurried
interface of $r$ by embedding it under a binary $\lambda$-abstraction.
%
To illustrate the translation in action consider the following example
program that adds the results obtained by performing two invocations
of some stateful operation $\dec{Incr} : \UnitType \opto \Int$, which
increments some global counter and returns its prior value.
%
\[
\ba{@{~}l@{~}l}
&\mathcal{P}\left\llbracket
\ba[m]{@{}l}
\ParamHandle\;\Do\;\dec{Incr}\,\Unit + \Do\;\dec{Incr}\,\Unit\;\With\\
\left( q.\ba[m]{@{~}l@{~}c@{~}l}
\Return\;x &\mapsto& \Return\;\Record{x;q}\\
\OpCase{\dec{Incr}}{\Unit}{r} &\mapsto& r\,\Record{q;q+1}
\ea\right)~40
\ea
\right\rrbracket \medskip\\
=& \left(
\ba[m]{@{}l}
\Handle\;\Do\;\dec{Incr}\,\Unit + \Do\;\dec{Incr}\,\Unit\;\With\\
\quad\ba[t]{@{~}l@{~}c@{~}l}
\Return\;x &\mapsto& \lambda q.\Return\;\Record{x;q}\\
\OpCase{\dec{Incr}}{\Unit}{r} &\mapsto& \lambda q. \Let\;r \revto \Return\;\lambda\Record{x;q}.r~x~q\;\In\;r\,\Record{q;q+1}
\ea
\ea\right)~40
\ea
\]
%
Evaluation of the program on either side of the equals sign yields
$\Record{81;42} : \Int$. The translation desugars the parameterised
handler into an ordinary deep handler that makes use of the
parameter-passing idiom to maintain the state of the handled
computation~\cite{Pretnar15}.
The translation commutes with substitution and preserves typability.
%
\begin{lemma}\label{lem:pd-subst}
Let $\sigma$ denote a substitution. The translation $\sdtrans{-}$
commutes with substitution, i.e.
%
\[
\sdtrans{V}\sdtrans{\sigma} = \sdtrans{V\sigma},\quad
\sdtrans{M}\sdtrans{\sigma} = \sdtrans{M\sigma},\quad
\sdtrans{H}\sdtrans{\sigma} = \sdtrans{(q.\,H)\sigma}.
\]
%
\end{lemma}
%
\begin{proof}
By induction on the structures of $V$, $M$, and $q.H$.
\end{proof}
%
\begin{theorem} \begin{theorem}
If $\Delta; \Gamma \vdash M : C$ then $\PD{\Delta}; If $\Delta; \Gamma \vdash M : C$ then $\PD{\Delta};
\sdtrans{\Gamma} \vdash \PD{M} : \PD{C}$. \sdtrans{\Gamma} \vdash \PD{M} : \PD{C}$.
@@ -11727,20 +11851,71 @@ If $\Delta; \Gamma \vdash M : C$ then $\PD{\Delta};
\begin{proof} \begin{proof}
By induction on typing derivations. By induction on typing derivations.
\end{proof} \end{proof}
%
This translation of parameterised handlers simulates the native This translation of parameterised handlers simulates the native
semantics. As with the simulation of deep handlers via shallow semantics. As with the simulation of deep handlers via shallow
handlers in Section~\ref{sec:deep-as-shallow}, this simulation is only up handlers in Section~\ref{sec:deep-as-shallow}, this simulation is only
to congruence due to the need for an application of a pure function to up to congruence due to the need for an application of a pure function
a variable to be reduced. The interesting cases of the proof appear in to a variable to be reduced.
Appendix~\ref{sec:param-proof}. %
\begin{theorem}[Simulation of parameterised handlers by deep \begin{theorem}[Simulation of parameterised handlers by deep
handlers] handlers]
\label{thm:param-simulation} \label{thm:param-simulation}
If $M \reducesto N$ then $\PD{M} \reducesto^+_{\mathrm{cong}} \PD{N}$. If $M \reducesto N$ then $\PD{M} \reducesto^+_{\mathrm{cong}} \PD{N}$.
\end{theorem} \end{theorem}
%
\begin{proof}
By induction on $M$. There are only two interesting cases to
consider.
\begin{description}
\item[Case]
$M = \ParamHandle\; \Return\;V\;\With\;(q.~H)(W) \reducesto
N[V/x,W/q]$, where $\hret = \{ \Return\; x \mapsto N \}$.
%
\begin{derivation}
&\PD{\ParamHandle\;\Return\;V\;\With\;(q.~H)(W)}\\
=& \reason{definition of $\PD{-}$}\\
&(\Handle\;\Return\;\PD{V}\;\With\;\PD{H}_q)~\PD{W}\\
\reducesto& \reason{$\semlab{Ret}$ with $\PD{\hret}_q = \{\Return~x \mapsto \lambda q. \PD{N}\}$}\\
&(\lambda q. \PD{N}[\PD{V}/x])~\PD{W}\\
\reducesto& \reason{$\semlab{App}$}\\
&\PD{N}[\PD{V}/x,\PD{W}/q]\\
=& \reason{lemma~\ref{lem:pd-subst}}\\
&\PD{N[V/x,W/q]}
\end{derivation}
\item[Case] $M = \ParamHandle\; \EC[\Do\;\ell~V]\;\With\;(q.~H)(W) \reducesto \\
\qquad
N[V/p,W/q,\lambda \Record{x,q'}. \ParamHandle\;\EC[\Return\;x]\;\With\;(q.~H)(q')/r]$, where $\hell = \{ \OpCase{\ell}{p}{r} \mapsto N \}$.
\begin{derivation}
&\PD{\ParamHandle\;\EC[\Do\;\ell~V]\;\With\;(q.~H)(W)}\\
=& \reason{definition of $\PD{-}$}\\
&(\Handle\;\EC[\Do\;\ell~\PD{V}]\;\With\;\PD{H}_q)~\PD{W}\\
\reducesto& \reason{$\semlab{Op}$ with $\PD{\hell}_q = \{\ell~p~r \mapsto \lambda q. \Let\,r' \revto \lambda \Record{x,q'}. r~x~q\,\In\, \PD{N}[r'/r]\}$}\\
&((\lambda q. \bl
\Let\;r' \revto \lambda \Record{x,q'}. r~x~q\; \In \\
\PD{N}[r'/r])[\PD{V}/p,\lambda x.\Handle\;\EC[\Return\;x]\;\With\;\PD{H}_q)~\PD{W}\\
\el \\
=& \reason{definition of $[-]$}\\
&(\lambda q. \bl
\Let\; r' \revto \lambda \Record{x,q'}. (\lambda x. \Handle\;\EC[\Return\;x]\;\With\; \PD{H}_q)~x~q'\;\In \\
\PD{N}[\PD{V}/p,r'/r])\\
\el \\
\reducesto& \reason{$\semlab{App}$}\\
&\Let\;r' \revto \lambda \Record{x,q'}. r~x~q'\; \In\; \PD{N}[r'/r,\PD{V}/p,\PD{V}/q]\\
\reducesto& \reason{$\semlab{Let}$}\\
&\PD{N}[\bl
\PD{V}/p,\PD{W}/q, \\
\lambda \Record{x,q'}. (\lambda x. \Handle\;\EC[\Return\;x]\;\With\; \PD{H}_q)~x~q'/r]\\
\el \\
\reducesto_{\mathrm{cong}}& \reason{$\semlab{App}$}\\
&\PD{N}[\PD{V}/p,\PD{W}/q,\lambda \Record{x,q'}. (\Handle\;\EC[\Return\;x]\;\With\; \PD{H}_q)~q'/r]\\
=& \reason{definition of $\PD{-}$}\\
&\PD{N}[\PD{V}/p,\PD{W}/q,\lambda \Record{x,q'}. \PD{\ParamHandle\;\EC[\Return\;x]\;\With\;(q.~H)(q')}/r]\\
=& \reason{lemma~\ref{lem:pd-subst}}\\
&\PD{N[V/p,W/q,\lambda \Record{x,q'}. \ParamHandle\;\EC[\Return\;x]\;\With\; (q.~H)(q')/r]}
\end{derivation}
\end{description}
\end{proof}
\section{Related work} \section{Related work}
\citet{ForsterKLP17,ForsterKLP19} \citet{PirogPS19}, \citet{Shan04} \citet{ForsterKLP17,ForsterKLP19} \citet{PirogPS19}, \citet{Shan04}