\documentclass{article}
\usepackage{611-lecture}
\usepackage{amsthm,amsmath,amssymb}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{17}                  %% Lecture number
\title{CPS Conversion}   %% Title of lecture
%\author{Ian Kash, Yee Jiun Song}       %% name of scribe
\date{6 October 2006}     %% Date of lecture, e.g., 1 January 2001
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\theoremstyle{definition}
\newtheorem*{defn}{Definition}
\newcommand{\nondet}{\left[\!\kern1pt\right]}
\renewcommand\phi\varphi
\renewcommand\wp[2]{\mathsf{wp}~{#1}~{#2}}
\newcommand\wlp[2]{\mathsf{wlp}~{#1}~{#2}}
\renewcommand\({\begin{eqnarray*}}
\renewcommand\){\end{eqnarray*}}

\newcommand\LOOKUP[2]{\mathrm{LOOKUP}~{#1}~{#2}}
\newcommand\UPDATE[3]{\mathrm{UPDATE}~{#1}~{#2}~{#3}}
\newcommand\MALLOC[2]{\mathrm{MALLOC}~{#1}~{#2}}
\newcommand\EMPTY{\mathrm{EMPTY\mbox{-}STORE}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}
\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\bigcdot{\mathrel{\raisebox{1pt}{$\scriptscriptstyle\bullet$}}}
\newcommand\holed[1]{[\,#1\,]}
\newcommand\hole{\holed\bigcdot}
\newcommand\context[1]{E\kern1pt\holed{#1}}
\newcommand\contextHole{\context\bigcdot}
\newcommand\goesto[2]{\underset{#2}{\overset{#1}\longrightarrow}}
\newcommand\ifthenelse[3]{\mathsf{if\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\whiledo[2]{\mathsf{while\ }#1\mathsf{\ do\ }#2}
\newcommand\letin[3]{\mathsf{let\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\letrec[5]{\mathsf{letrec\ }#1 = #2\mathsf{\ and\ \ldots\ and\ }#3 = #4\mathsf{\ in\ }#5}
\newcommand\letrecone[3]{\mathsf{letrec\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathsf{true}}}
\newcommand\false{\ensuremath{\mathsf{false}}}
\newcommand\error{\ensuremath{\mathsf{error}}}
\newcommand\pca[3]{\{#1\}\kern1pt{#2}\kern1pt\{#3\}}
\newcommand\states{\Set{St}}
\newcommand\rtc{^{\textstyle *}}
\newcommand\sat\vDash
\newcommand\force\vdash

\newcommand\hyphen{\mbox{-}}
\newcommand\lookup[2]{\nm{LOOKUP}~#1~\mquote{#2}}
\newcommand\update[3]{\nm{UPDATE}~#1~\mquote{#2}~#3}
\newcommand\SBk[1]{\SB{#1}k}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Outline}

\begin{itemize}
\setlength\itemsep{0pt}
\item{Continuation-passing style (CPS)}
\item{CPS semantics}
\item{CPS conversion}
\item {uML error checking}
\end{itemize}

\section{Continuation-Passing Style}

Consider the statement $\ifthenelse{x\le 0}{x}{x+1}$.  We can think of this as $(\lam y{\ifthenelse yx{x+1}})~(x\le 0)$.  To evaluate this, we would first evaluate the argument $x\le 0$ to obtain a Boolean value, then apply the function $\lam y{\ifthenelse yx{x+1}}$ to this value.  The function $\lam y{\ifthenelse yx{x+1}}$ is called a {\em continuation}, because it specifies what is to be done with the result of the current computation in order to continue the computation.

Given an expression $e$, it is possible to transform the expression into a function that takes a continuation $k$ and applies it to the value of $e$.  The transformation is applied recursively.  This is called \emph{continuation-passing style} (CPS).  There are a number of advantages to this style:
\begin{itemize}
\item
The resulting expressions have a much simpler evaluation semantics, since the sequence of reductions to be performed is specified by a series of continuations.  The next reduction to be performed is always uniquely determined, and the remainder of the computation is handled by a continuation.  Thus evaluation contexts are not necessary to specify the evaluation order.  
\item
In practice, function calls and function returns can be handled in a uniform way.  Instead of returning, the called function simply calls the continuation.
\item
In a recursive function, any computation to be performed on the value returned by a recursive call can be bundled into the continuation.  Thus every recursive call becomes tail-recursive.  For example, the factorial function
\(
"fact"~n &=& \ifthenelse{n=0}{1}{n*"fact"~(n-1)}
\)
becomes
\(
"fact"'~n~k &=& \ifthenelse{n=0}{k~1}{"fact"'~(n-1)~(\lam v{k~(n*v)})}.
\)
One can show inductively that $"fact"'~n~k = k~("fact"~n)$, therefore $"fact"'~n~\lam xx = "fact"~n$.  This transformation effectively trades stack space for heap space in the implementation.
\item
Continuation-passing gives a convenient mechanism for non-local flow of control, such as goto statements and exception handling.
\end{itemize}

\section{CPS Semantics}

Our grammar for the $\lambda$-calculus was:
\begin{eqnarray*}
e &::=& x \bnf \lam xe \bnf e_0~e_1 
\end{eqnarray*}
Our grammar for the CPS $\lambda$-calculus will be:
\begin{eqnarray*}
v\ \::=\ \ x \bnf \lam xe &\qquad& e\ \ ::=\ \ v_0~v_1~\cdots~v_n
\end{eqnarray*}
This is a highly constrained syntax.  Barring reductions inside the scope of a $\lambda$-abstraction operator, the expressions $v$ are all irreducible.  The only reducible expression is $v_0~v_1~\cdots~v_n$.  If $n\geq 1$, there exactly one redex $v_0~v_1$, and both the function and the argument are already fully reduced.  The small step semantics has a single rule
\(
(\lam xe)~v &\rightarrow& \subst evx,
\)
and we do not need any evaluation contexts.

The big step semantics is also quite simple, with only a single rule:
\[
\frac{\subst evx \stepsto v'}{(\lam xe)~v \stepsto v'}.
\]
The resulting proof tree will not be very tree-like.  The rule has one premise, so a proof will be a stack of inferences, each one corresponding to a step in the small-step semantics.  This allows for a much simpler interpreter that can work in a straight line rather than having to make multiple recursive calls.

\section{CPS Conversion}

We now give a translation into the CPS calculus.  The translation takes an arbitrary $\lambda$-term $e$ and produces a CPS term $\SB e$ which is a function taking a continuation as argument.  Intuitively, $\SBk e$ applies $k$ to the value of $e$.

We want our translation to satisfy $e\goesto *{CBV} v \Leftrightarrow \SBk e\goesto *{CPS} \SBk v$ for primitive values $v$ and any variable $k\notin\FV e$, and $e\Uparrow_{CBV} \Leftrightarrow \SBk e \Uparrow_{CPS}$.

The translation is (adding numbers as primitive values):
\(
\SBk n &\definedas& k~n\\
\SBk x &\definedas& k~x \\
\SBk{\lam xe} &\definedas& k~(\lam x{\SB e})\ \ =\ \ k~(\lam{xk'}{\SB ek'})\\
\SBk{e_0~e_1} &\definedas& \SB{e_0}(\lam f{\SB{e_1}(\lam v{fvk})}).
\)
(Here we have defined $\SB e$ in the style of ML; thus $\SBk e \definedas e'$ really means $\SB e \definedas \lam k{e'}$.)

\subsection{An Example}

In the CBV $\lambda$-calculus, we have
\(
(\lam{xy}x)~1 &\rightarrow& \lam y1
\)
Let's evaluate the CPS-translation of the left-hand side using the CPS evaluation rules.
\(
\SBk{(\lam{xy}x)~1}
&=& \SB{\lam x{\lam yx}}(\lam f{\SB 1(\lam v{fvk})})\\
&\rightarrow& (\lam f{\SB 1(\lam v{fvk})})~(\lam x{\SB{\lam yx}})\\
&\rightarrow& \SB 1(\lam v{(\lam x{\SB{\lam yx}})~v~k})\\
&\rightarrow& (\lam v{(\lam x{\SB{\lam yx}})~v~k})~1\\
&\rightarrow& (\lam x{\SB{\lam yx}})~1~k\\
&\rightarrow& \SBk{\lam y1}.
\)

\section{Error Checking}

Now let us use CPS semantics to augment our previously defined uML language translation so that it supports error checking.  This time our translated expressions will be functions of $\rho$ and $k$ denoting an environment and a continuation, respectively.  The term $\Tr Ee\rho k$ represents the result of evaluating $e$ in the environment $\rho$ and sending the resulting value to the continuation $k$.

Assume that we have an encoding of variable names that will allow us to represent an environment $\rho$ as a list of (name,value) pairs, along with lookup and update functions $\lookup\rho x$ and $\update\rho x v$.

In addition, we want to catch type errors that may occur during the evaluation. We use the following tags to keep track of types:
\[
\begin{array}{ll@{\hspace{1cm}}ll@{\hspace{1cm}}ll}
\mbox{booleans} & 0 & \mbox{empty list} & 2 & \mbox{functions} & 4\\
\mbox{integers} & 1 & \mbox{pairs} & 3 & \mbox{error} & 5
\end{array}
\]
A \emph{tagged value} is a value paired with its type tag; for example, $(0,"true")$.  Using these tagged values, we can now define a translation that incorporates runtime type checking:
\begin{eqnarray*}
\Tr E x\rho k &\definedas& k~(\lookup\rho x)\\
\Tr E b\rho k &\definedas& k~(0,b)\\
\Tr E n\rho k &\definedas& k~(1,n)\\
\Tr E{"nil"}\rho k &\definedas& k~(2,0)\\
\Tr E{(e_0,e_1)}\rho k &\definedas& \Tr E{e_0}\,\rho\,(\lam{x_0}{\Tr E{e_1}\,\rho\,(\lam{x_1}{k~(3,(x_0,x_1))})})\\
\Tr E{\letin x{e_1}{e_2}}\rho k &\definedas& \Tr E{e_1}\,\rho\,(\lam p{\Tr E{e_2}\,(\update\rho x p)\,k})\\
\Tr E{\lam xe}\rho k &\definedas& k~(4,\lam{xk}{\Tr Ee\,(\update\rho x x)\,k})\\
\Tr E{"error"}\rho k &\definedas& k~(5,0).
\end{eqnarray*}
Now a functional application can check that it is actually applying a function:
\begin{eqnarray*}
\Tr E{e_0~e_1}\rho k &\definedas& \Tr E{e_0}\,\rho\,(\lam p{\letin{(t,f)}p{\ifthenelse{t\neq 4}{"error"}{\Tr E{e_1}\,\rho\,(\lam v{fvk})}}})
\end{eqnarray*}
We can simplify this by defining a helper function check-fn:
\(
"check\hyphen fn" &\definedas& \lam{kp}{\letin{(t,f)}p{\ifthenelse{t\neq 4}{"error"}{kf}}}.
\)
The helper function takes in a continuation and a tagged value, checks the type, strips off the tag, and passes the raw (untagged) value to the continuation.  Then
\(
\Tr E{e_0~e_1} \rho k &\definedas& \Tr E{e_0}\,\rho\,("check\hyphen fn"~(\lam f{\Tr E{e_1}\,\rho\,(\lam v{fvk})})).
\)
Similarly,
\(
\Tr E{\ifthenelse{e_0}{e_1}{e_2}}\rho k &\definedas& 
\Tr E{e_0}\,\rho\,({"check\hyphen bool"~(\lam b{\ifthenelse b{\Tr E{e_1}\rho k}{\Tr E{e_2}\rho k}})})\\
\Tr E{\#n~e}\rho k &\definedas& \Tr E e\,\rho\,({"check\hyphen pair"~(\lam t{k~(\#n~t)})}).
\)
                                                                
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

Compile-time optimizations:
\(
\update{(\update\rho x u)}y v &\rightarrow& \update{(\update\rho y v)}x u\\
\update{(\update\rho x u)}x v &\rightarrow& \update\rho x v\\
\lookup{(\update\rho x v)}x &\rightarrow& v.
\)
                                                                
