Browse Source

Add flawed CPS translation for shallow handlers.

master
Daniel Hillerström 5 years ago
parent
commit
e74fc3659f
  1. 6
      macros.tex
  2. 129
      thesis.tex

6
macros.tex

@ -246,7 +246,7 @@
\newcommand{\dlk}{s} % let continuations \newcommand{\dlk}{s} % let continuations
\newcommand{\dhf}{q} % handler frames \newcommand{\dhf}{q} % handler frames
\newcommand{\dhk}{ks} % handler continuations \newcommand{\dhk}{ks} % handler continuations
\newcommand{\dhkr}{rk} % reverse handler continuations
\newcommand{\dhkr}{rs} % reverse handler continuations
\newcommand{\dLet}{\dynamic{\Let}} \newcommand{\dLet}{\dynamic{\Let}}
\newcommand{\dIn}{\dynamic{\In}} \newcommand{\dIn}{\dynamic{\In}}
@ -280,3 +280,7 @@
% \newcommand{\dk}{\dRecord{fs,\dRecord{\vhret,\vhops}}} % \newcommand{\dk}{\dRecord{fs,\dRecord{\vhret,\vhops}}}
\newcommand{\dk}{k} \newcommand{\dk}{k}
%
\renewcommand{\hid}{V_\mathrm{forward}}
\newcommand{\kid}{V_\mathrm{id}}
\newcommand{\rid}{V_\mathrm{ret}}

129
thesis.tex

@ -2614,18 +2614,18 @@ resumption stack with the current continuation pair.
% %
\begin{equations} \begin{equations}
\cps{-} &:& \CompCat \to \SValCat^\ast \to \UCompCat\\ \cps{-} &:& \CompCat \to \SValCat^\ast \to \UCompCat\\
\cps{V\,W} &\defas& \slam \sks.\cps{V} \dapp \cps{W} \dapp \reify \sks \\
\cps{V\,T} &\defas& \slam \sks.\cps{V} \dapp \Record{} \dapp \reify \sks \\
\cps{V\,W} &\defas& \slam \sks.\cps{V} \dapp \cps{W} \dapp \reify \sks \\
\cps{V\,T} &\defas& \slam \sks.\cps{V} \dapp \Record{} \dapp \reify \sks \\
\cps{\Let\; \Record{\ell=x;y} = V \; \In \; N} &\defas& \slam \sks.\Let\; \Record{\ell=x;y} = \cps{V} \; \In \; \cps{N} \sapp \sks \\ \cps{\Let\; \Record{\ell=x;y} = V \; \In \; N} &\defas& \slam \sks.\Let\; \Record{\ell=x;y} = \cps{V} \; \In \; \cps{N} \sapp \sks \\
\cps{\Case~V~\{\ell~x \mapsto M; y \mapsto N\}} &\defas& \cps{\Case~V~\{\ell~x \mapsto M; y \mapsto N\}} &\defas&
\slam \sks.\Case~\cps{V}~\{\ell~x \mapsto \cps{M} \sapp \sks; y \mapsto \cps{N} \sapp \sks\} \\ \slam \sks.\Case~\cps{V}~\{\ell~x \mapsto \cps{M} \sapp \sks; y \mapsto \cps{N} \sapp \sks\} \\
\cps{\Absurd~V} &\defas& \slam \sks.\Absurd~\cps{V} \\ \cps{\Absurd~V} &\defas& \slam \sks.\Absurd~\cps{V} \\
\cps{\Return~V} &\defas& \slam \sk \scons \sks.\reify \sk \dapp \cps{V} \dapp \reify \sks \\ \cps{\Return~V} &\defas& \slam \sk \scons \sks.\reify \sk \dapp \cps{V} \dapp \reify \sks \\
\cps{\Let~x \revto M~\In~N} &\defas& \slam \sk \scons \sks.\cps{M} \sapp \cps{\Let~x \revto M~\In~N} &\defas& \slam \sk \scons \sks.\cps{M} \sapp
(\reflect (\dlam x\,ks.
(\reflect (\dlam x\,\dhk.
\ba[t]{@{}l} \ba[t]{@{}l}
\Let\;(h \dcons ks') = ks\;\In\\
\cps{N} \sapp (\sk \scons \reflect h \scons \reflect ks')) \scons \sks)
\Let\;(h \dcons \dhk') = \dhk\;\In\\
\cps{N} \sapp (\sk \scons \reflect h \scons \reflect \dhk')) \scons \sks)
\ea\\ \ea\\
\cps{\Do\;\ell\;V} \cps{\Do\;\ell\;V}
&\defas& \slam \sk \scons \sh \scons \sks.\reify \sh \dapp \Record{\ell,\Record{\cps{V}, \reify \sh \dcons \reify \sk \dcons \dnil}} \dapp \reify \sks\\ &\defas& \slam \sk \scons \sh \scons \sks.\reify \sh \dapp \Record{\ell,\Record{\cps{V}, \reify \sh \dcons \reify \sk \dcons \dnil}} \dapp \reify \sks\\
@ -2637,34 +2637,34 @@ resumption stack with the current continuation pair.
% %
\begin{equations} \begin{equations}
\cps{-} &:& \HandlerCat \to \UValCat\\ \cps{-} &:& \HandlerCat \to \UValCat\\
\cps{\{\Return \; x \mapsto N\}} &\defas& \dlam x\, ks.
\cps{\{\Return \; x \mapsto N\}} &\defas& \dlam x\, \dhk.
\ba[t]{@{~}l} \ba[t]{@{~}l}
\Let\; (h \dcons k \dcons h' \dcons ks') = ks \;\In\\
\cps{N} \sapp (\reflect k \scons \reflect h' \scons \reflect ks')
\Let\; (h \dcons \dk \dcons h' \dcons \dhk') = \dhk \;\In\\
\cps{N} \sapp (\reflect \dk \scons \reflect h' \scons \reflect \dhk')
\ea \ea
\\ \\
\cps{\{(\ell \; p \; r \mapsto N_\ell)_{\ell \in \mathcal{L}}\}} \cps{\{(\ell \; p \; r \mapsto N_\ell)_{\ell \in \mathcal{L}}\}}
&\defas& \bl
\dlam \Record{z,\Record{p,rs}}\,ks.\Case \;z\; \{
&\defas& \bl
\dlam \Record{z,\Record{p,\dhkr}}\,\dhk.\Case \;z\; \{
\ba[t]{@{}l@{}c@{~}l} \ba[t]{@{}l@{}c@{~}l}
&(\ell \mapsto& &(\ell \mapsto&
\ba[t]{@{}l} \ba[t]{@{}l}
\Let\;r=\Res\;rs \;\In\\
\Let\;(k \dcons h \dcons ks') = ks \;\In\\
\cps{N_{\ell}} \sapp (\reflect k \scons \reflect h \scons \reflect ks'))_{\ell \in \mathcal{L}};
\Let\;r=\Res\;\dhkr \;\In\\
\Let\;(\dk \dcons h \dcons \dhk') = \dhk \;\In\\
\cps{N_{\ell}} \sapp (\reflect \dk \scons \reflect h \scons \reflect \dhk'))_{\ell \in \mathcal{L}};
\ea\\ \ea\\
&y \mapsto& \hforward((y,p,rs),ks) \} \\
&y \mapsto& \hforward((y,p,\dhkr),\dhk) \} \\
\ea \\ \ea \\
\el \\ \el \\
\hforward((y,p,rs),ks)
&\defas&\Let\; (k' \dcons h' \dcons ks') = ks \;\In\; h' \dapp \Record{y,\Record{p,h' \dcons k' \dcons rs}} \dapp ks'
\hforward((y,p,\dhkr),\dhk)
&\defas&\Let\; (\dk' \dcons h' \dcons \dhk') = \dhk \;\In\; h' \dapp \Record{y,\Record{p,h' \dcons \dk' \dcons \dhkr}} \dapp \dhk'
\end{equations} \end{equations}
% %
\textbf{Top level program} \textbf{Top level program}
% %
\begin{equations} \begin{equations}
\pcps{-} &:& \CompCat \to \UCompCat\\ \pcps{-} &:& \CompCat \to \UCompCat\\
\pcps{M} &=& \cps{M} \sapp (\reflect (\dlam x\,ks.x) \scons \reflect (\dlam z\,ks.\Absurd~z) \scons \snil) \\
\pcps{M} &=& \cps{M} \sapp (\reflect (\dlam x\,\dhk.x) \scons \reflect (\dlam z\,\dhk.\Absurd~z) \scons \snil) \\
\end{equations} \end{equations}
\caption{Higher-order uncurried CPS translation of $\HCalc$.} \caption{Higher-order uncurried CPS translation of $\HCalc$.}
@ -2844,30 +2844,30 @@ in practice, consider the following example term.
=& \reason{static $\beta$-reduction}\\ =& \reason{static $\beta$-reduction}\\
&(\slam \sk \scons \sks. \reify \sk \dapp \cps{V} \dapp \reify \sks) &(\slam \sk \scons \sks. \reify \sk \dapp \cps{V} \dapp \reify \sks)
\sapp \sapp
(\reflect(\dlam x\,ks.
(\reflect(\dlam x\,\dhk.
\ba[t]{@{}l} \ba[t]{@{}l}
\Let\;(h \dcons ks') = ks \;\In\\
\Let\;(h \dcons \dhk') = \dhk \;\In\\
\cps{N} \sapp \cps{N} \sapp
\ba[t]{@{}l} \ba[t]{@{}l}
(\reflect (\dlam x\,ks.x) \scons \reflect h \scons \reflect ks'))\\
~~\scons \reflect (\dlam z\,ks.\Absurd~z) \scons \snil))
(\reflect (\dlam x\,\dhk.x) \scons \reflect h \scons \reflect \dhk'))\\
~~\scons \reflect (\dlam z\,\dhk.\Absurd~z) \scons \snil))
\ea \ea
\ea\\ \ea\\
=& \reason{static $\beta$-reduction}\\ =& \reason{static $\beta$-reduction}\\
&\ba[t]{@{}l@{~}l} &\ba[t]{@{}l@{~}l}
&(\dlam x\,ks.
\Let\;(h \dcons ks') = ks \;\In\;
&(\dlam x\,\dhk.
\Let\;(h \dcons \dhk') = \dhk \;\In\;
\cps{N} \sapp \cps{N} \sapp
(\reflect (\dlam x\,ks.x) \scons \reflect h \scons \reflect ks'))\\
\dapp& \cps{V} \dapp ((\dlam z\,ks.\Absurd~z) \dcons \dnil)\\
(\reflect (\dlam x\,\dhk.x) \scons \reflect h \scons \reflect \dhk'))\\
\dapp& \cps{V} \dapp ((\dlam z\,\dhk.\Absurd~z) \dcons \dnil)\\
\ea\\ \ea\\
\reducesto& \reason{\usemlab{App_2}}\\ \reducesto& \reason{\usemlab{App_2}}\\
&\Let\;(h \dcons ks') = (\dlam z\,ks.\Absurd~z) \dcons \dnil \;\In\;
&\Let\;(h \dcons \dhk') = (\dlam z\,\dhk.\Absurd~z) \dcons \dnil \;\In\;
\cps{N[V/x]} \sapp \cps{N[V/x]} \sapp
(\reflect (\dlam x\,ks.x) \scons \reflect h \scons \reflect ks'))\\
(\reflect (\dlam x\,\dhk.x) \scons \reflect h \scons \reflect \dhk'))\\
\reducesto^+& \reason{dynamic pattern matching and substitution}\\ \reducesto^+& \reason{dynamic pattern matching and substitution}\\
&\cps{N[V/x]} \sapp &\cps{N[V/x]} \sapp
(\reflect (\dlam x\,ks.x) \scons \reflect (\dlam z\,ks.\Absurd~z) \scons \reflect \dnil)
(\reflect (\dlam x\,\dhk.x) \scons \reflect (\dlam z\,\dhk.\Absurd~z) \scons \reflect \dnil)
\end{derivation} \end{derivation}
% %
The translation of $\Return$ provides the generated dynamic pure The translation of $\Return$ provides the generated dynamic pure
@ -2989,8 +2989,8 @@ translation eliminates the static redex at translation time.
% %
\begin{equations} \begin{equations}
\pcps{\Return\;\Record{}} \pcps{\Return\;\Record{}}
&=& (\slam \sk \scons \sks. \sk \dapp \Record{} \dapp \reify \sks) \sapp (\reflect (\dlam x\,ks.x) \scons \reflect (\dlam z\,ks.\Absurd\;z) \scons \snil)\\
&=& (\dlam x\,ks.x) \dapp \Record{} \dapp (\reflect (\dlam z\,ks.\Absurd\;z) \dcons \dnil)\\
&=& (\slam \sk \scons \sks. \sk \dapp \Record{} \dapp \reify \sks) \sapp (\reflect (\dlam x\,\dhk.x) \scons \reflect (\dlam z\,\dhk.\Absurd\;z) \scons \snil)\\
&=& (\dlam x\,\dhk.x) \dapp \Record{} \dapp (\reflect (\dlam z\,\dhk.\Absurd\;z) \dcons \dnil)\\
&\reducesto& \Record{} &\reducesto& \Record{}
\end{equations} \end{equations}
% %
@ -3195,9 +3195,73 @@ If $M \reducesto N$ then $\pcps{M} \reducesto^+ \areducesto^* \pcps{N}$.
% \end{proof} % \end{proof}
% %
\section{A simultaneous CPS transform for deep and shallow handlers}
\section{CPS transforming shallow effect handlers}
\label{sec:cps-deep-shallow} \label{sec:cps-deep-shallow}
\subsection{A flawed attempt}
We adapt our higher-order CPS translation to accommodate both shallow
and deep handlers.
\begin{equations}
\cps{-} &:& \CompCat \to \SValCat^\ast \to \UCompCat\\
\cps{\Do\;\ell\;V} &\defas& \slam \sk \scons \svhret \scons \svhops \scons \sks.
\reify\svhops \ba[t]{@{}l}
\dapp \Record{\ell, \Record{\cps{V}, \reify\svhops \dcons \reify\svhret \dcons \reify\sk \dcons \dnil}}\\
\dapp \reify \sks
\ea\\
\cps{\ShallowHandle \; M \; \With \; H} &\defas&
\slam \sks . \cps{M} \sapp (\reflect\kid \scons \reflect\cps{\hret} \scons \reflect\cps{\hops}^\dagger \scons \sks) \\
\cps{\{\Return \; x \mapsto N\}} &\defas& \dlam x\, \dhk.
\ba[t]{@{}l}
\Let\; (\_ \dcons \dk \dcons \vhret \dcons \vhops \dcons \dhk') = \dhk \;\In\\
\cps{N} \sapp (\reflect \dk \scons \reflect \vhret \scons \reflect \vhops \scons \reflect \dhk')
\ea \smallskip\\
\kid &\defas& \dlam x\, \dhk.\Let\; (\vhret \dcons \dhk') = \dhk \;\In\; \vhret \dapp x \dapp \dhk'
\end{equations}
%
\begin{equations}
\cps{-} &:& \HandlerCat \to \UValCat\\
\cps{\{(\ell \; p \; r \mapsto N_\ell)_{\ell \in \mathcal{L}}\}}^\dagger
&\defas&
\bl
\dlam \Record{z,\Record{p,\dhkr}}\,\dhk.\\
\qquad\Case \;z\; \{
\ba[t]{@{}l@{}c@{~}l}
(&\ell &\mapsto
\ba[t]{@{}l}
\Let\;(\dk \dcons \vhret \dcons \vhops \dcons \dhk') = \dhk\;\In\\
\Let\;(\_ \dcons \_ \dcons \dhkr') = \dhkr \;\In\\
\Let\;r = \Res\,(\hid \dcons \rid \dcons \dhkr') \;\In \\
\cps{N_{\ell}} \sapp (\reflect\dk \scons \reflect\vhret \scons \reflect\vhops \scons \reflect \dhk'))_{\ell \in \mathcal{L}} \\
\ea \\
&y &\mapsto \hforward((y,p,\dhkr),\dhk) \} \\
\ea
\el\\
\hforward((y, p, \dhkr), \dhk) &\defas& \bl
\Let\; (\dk \dcons \vhret \dcons \vhops \dcons \dhk') = \dhk \;\In \\
\vhops \dapp \Record{y, \Record{p, \vhops \dcons \vhret \dcons \dk \dcons \dhkr}} \dapp \dhk' \\
\el \\
\hid &\defas& \dlam\,\Record{z,\Record{p,\dhkr}}\,\dhk.\hforward((z,p,\dhkr),\dhk) \\
\rid &\defas& \dlam x\, \dhk.\Let\; (\vhops \dcons \dk \dcons \dhk') = \dhk \;\In\; \dk \dapp x \dapp \dhk' \medskip\\
\pcps{-} &:& \CompCat \to \UCompCat\\
\pcps{M} &\defas& \cps{M} \sapp (\reflect \kid \scons \reflect (\dlam x\,\dhk.x) \scons \reflect (\dlam z\,ks.\Absurd~z) \scons \snil) \\
\end{equations}
%
We adapt the representation of continuations so that the current pure
continuation is separate from the pure continuation corresponding to
the return clause.
%
Thus a stack of continuations is now a sequence of triples consisting
of two pure continuations followed by an effect continuation.
%
Crucially, this enables us to replace the current handler with a dummy
identity handler in the resumption of a shallow handler clause.
%
Unfortunately, inserting such dummy handlers can lead to memory
leaks. Avoiding these memory leaks requires a more sophisticated
approach, which we defer to future work.
\subsection{Simultaneous CPS transform for deep and shallow handlers}
\begin{figure} \begin{figure}
% %
\textbf{Values} \textbf{Values}
@ -3282,6 +3346,9 @@ If $M \reducesto N$ then $\pcps{M} \reducesto^+ \areducesto^* \pcps{N}$.
\caption{Adjustments to the higher-order uncurried CPS translation.} \caption{Adjustments to the higher-order uncurried CPS translation.}
\label{fig:cps-higher-order-uncurried} \label{fig:cps-higher-order-uncurried}
\end{figure} \end{figure}
%
\section{Related work} \section{Related work}
\label{sec:cps-related-work} \label{sec:cps-related-work}

Loading…
Cancel
Save