\documentclass{article}
\usepackage{611-lecture}
\usepackage{amsthm,amsmath,amssymb}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{18}                 %% Lecture number
\title{Continuations and Exceptions}   %% Title of lecture
%\author{Wyatt Schweizer, David Crandall}       %% name of scribe
\date{11 October 2006}     %% Date of lecture, e.g., 1 January 2001
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture. Here are a few of particular interest:
%
% "x"	 	x in keyword font (e.g., "if", "#t")
% _x_	 	x in italics
% \nm{n}   	n in slanted font (used for abbreviations)
% <e> 	 	e in angle brackets
% \lt 	 	less-than sign
% \gt 	 	greater-than sign
% \SB{x}	x in semantic brackets
% \Tr x{y} 	x[[y]] with x in calligraphic font
%          	(if x is more than a single character, use \Tr{x}{y})

\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}

\newcommand{\LET}{\impfnt{let}}
\newcommand{\IN}{\impfnt{in}}

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Last time we introduced CPS as a restriction on the
$\lambda$-calculus.  This was helpful because programs written in this
restricted $\lambda$-calculus have a much simpler operational
semantics.  In fact, we defined the operational semantics using only a
single rule. Another advantage to CPS is that evaluation order
decisions are already determined. In general, CPS style is a more
primitive model of computation and therefore easier to compile.

Today we give CPS semantics for uML as a translation to a 
restricted form of uML. Our translation will also produce strongly-typed
uML programs. Then we will extend the translation to uML!. Finally,
we show how to extend uML to support exception handling.

\section{CPS Semantics for uML with Strong Typing}

\subsection{Value Translation}

To support strong typing, we introduce type tags that can be used to tag each value with its type.
\[
\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}
\]
We can define functions \nm{NULL}, $\nm{BOOL}$, $\nm{INT}$, $\nm{PAIR}$, $\nm{FUN}$,
etc.\ to tag a raw value with its type; for example, $\nm{BOOL}~"true"=(0,"true")$.  These can all be defined in terms of a function $\nm{TAG}\definedas\lam{tx}{(t,x)}$.  Then $\nm{BOOL}=\nm{TAG}~0$, etc.

We also define functions
$\nm{CHECK-NULL}$, $\nm{CHECK-BOOL}$, $\nm{CHECK-INT}$,
$\nm{CHECK-PAIR}$, $\nm{CHECK-FUN}$, etc.\ to
check that a given tagged value is of the correct type, extract the original raw value, and pass it to a continuation.  For example, $\nm{CHECK-PAIR}$ is defined as:
\(
\nm{CHECK-PAIR} &\definedas& \lam{kv}{\ifthenelse{\#1~v = 3}{k~(\#2~v)}{"halt"~\nm{ERROR}}}
\)
where the parameter $k$ is a continuation and the parameter $v$ is a tagged value.
If the tag is 3, indicating that the raw value is a pair, then
we pass the raw value to the continuation. Otherwise we have encountered a runtime type
error, so we halt and return an error value.  We can also define these functions uniformly
in terms of a function
\(
\nm{CHECK} &\definedas& \lam{tkv}{\ifthenelse{\#1~v = t}{k~(\#2~v)}{"halt"~\nm{ERROR}}}
\)
Then $\nm{CHECK-PAIR}=\nm{CHECK}~3$, etc.  These implementations satisfy the equations
\(
\nm{CHECK}~t~k~(\nm{TAG}~t'~v) &=& 
\left\{\begin{array}{ll}
k~v, & \mbox{if $t=t'$},\\
"halt"~\nm{ERROR}, & \mbox{if $t\neq t'$}.
\end{array}\right.
\)

Note that the continuation-passing style affords some flexibility in the way errors are handled.  We need not call the continuation $k$, but may instead call a different continuation ("halt" in this example) corresponding to an error or exception handler.

\subsection{Expression Translation}

Translations are of the form $\Tr Ee\rho k$, 
which means, ``Send the value of the expression $e$ evaluated in the environment $\rho$ to the continuation $k$.''  The translations are:
\(
\Tr E{x}\,\rho\,k &\definedas& k\,(\lookup\rho x)\\
\Tr En\,\rho\,k &\definedas& k~(\nm{INT}~n)\\
\Tr E{(e_1,e_2)}\,\rho\,k &\definedas& \Tr E{e_1}\,\rho\,(\lam{v_1}{\Tr E{e_2}\,\rho\,(\lam{v_2}{k\,(\nm{PAIR}\,(v_1,\,v_2))})})\\
\Tr E{\#1\,e}\,\rho\,k &\definedas& \Tr Ee\,\rho \,(\nm{CHECK-PAIR}\,(\lam p{k~(\#1~p)})\\
\Tr E{\lam xe}\,\rho\,k &\definedas& k\,(\nm{FUN}(\lam{yk'}{\Tr Ee\,(\update\rho xy)\,k')})\\
&=& k\,(\nm{FUN}(\lam y{\Tr Ee\,(\update\rho xy))})\\
\Tr E{e_0~e_1}\,\rho\,k &\definedas& \Tr E{e_0}\,\rho\,(\nm{CHECK-FUN}\,(\lam f{\Tr E{e_1}\,\rho\,(\lam v{fvk})}))\\
\Tr E{\ifthenelse{e_0}{e_1}{e_2}}\,\rho\,k &\definedas& \Tr E{e_0}\,\rho\,(\nm{CHECK-BOOL}\,(\lam b{\ifthenelse b{\Tr E {e_1}\,\rho\,k}{\Tr E {e_2}\,\rho\,k}})).
\)

\section{CPS Semantics for uML!}

\subsection{Syntax}

Since uML! has references, we need to add a store $\sigma$ to our
notation. Thus we now have translations with the form $\Tr Ee\rho k \sigma$, which means,
``Evaluate $e$ in the environment $\rho$ with store $\sigma$ and send the resulting value and the new store to the continuation $k$.''  A continuation is now a function of a value and a store; that is, a continuation $k$ should have the form $\lam{v\sigma}{\cdots}$~.

The translation is:
\begin{itemize}
\item Variable: $\Tr Ex\,\rho\,k\,\sigma \definedas k~(\lookup\rho x)~\sigma$.

If we think about this translation as a function and $\eta$-reduce away the $\sigma$, we obtain
\begin{eqnarray*}
\Tr Ex\,\rho\,k &=& \lam\sigma{k~(\lookup\rho x)~\sigma}\ \ =\ \ k~(\lookup\rho x).
\end{eqnarray*}
\end{itemize}
Note that in the $\eta$-reduced version, we have the same translation
that we had when we translated uML.  In general, any expression in uML! that is
not state-aware can be $\eta$-reduced to the same translation as uML.
Thus in order to translate to uML!, we need to add
translation rules only for the functionality that is state-aware.

We assume that we have a type tag for locations and functions \nm{LOC} and \nm{CHECK-LOC} for
tagging values as locations and checking those tags.  We also assume that we have extended our $\nm{LOOKUP}$ and $\nm{UPDATE}$ functions to apply to stores.
\(
\Tr E{"ref"~e}\,\rho\,k\,\sigma &\definedas&
\Tr Ee\,\rho\,(\lam{v\sigma'}{\letin{(\ell,\sigma'')}{(\nm{MALLOC}~\sigma'~v)}{k~(\nm{LOC}~\ell)~\sigma''}})~\sigma\\
\Tr E{!e}\,\rho\,k &\definedas&
\Tr Ee\,\rho\,(\nm{CHECK-LOC}\,(\lam{\ell\sigma'}{k~(\lookup{\sigma'}\ell)~\sigma'}))\\
\Tr E{e_1:=e_2}\,\rho\,k &\definedas&
\Tr E{e_1}\,\rho\,(\nm{CHECK-LOC}\,(\lam\ell{\Tr E{e_2}\,\rho\,(\lam{v\sigma'}{k~(\nm{NULL}~0)~(\update{\sigma'}\ell v)})}))
\)

\section{Exceptions}

An exception mechanism allows non-local transfer of control in exceptional situations.  It is typically used to handle abnormal, unexpected, or rarely occurring events.  It can simplify code by allowing programmers to factor out these uncommon cases.

To add an exception handling mechanism to uML, we first extend the syntax:
\(
e &::=& \ldots \bnf "raise"~s~e \bnf "try"~e_1~"handle"~(s~x)~e_2
\)
Informally, the idea is that "handle" provides
a handler $e_2$ to be invoked when the exception named $s$ is encountered
inside the expression $e_1$. To raise an exception, the program
calls $"raise"~s~e$, where $s$ is the name of an exception
and $e$ is an expression that will be passed to the handler as its
argument $x$.

Most languages use a dynamic scoping mechanism to find the handler for a given exception.  When an exception is encountered, the language walks up the runtime call stack until a suitable exception handler is found.

\subsection{Exceptions in uML}

To add exception support to our CPS translation, we add a \emph{handler environment} $h$, which maps exception names to continuations.  We also extend our \nm{LOOKUP} and \nm{UPDATE} functions to accommodate handler environments.  Applied to a handler environment, \nm{LOOKUP} returns the continuation bound to a given exception name, and \nm{UPDATE} rebinds an exception name to a new continuation.

Now we can add exception support to our translation:
\(
\Tr E{"raise"\,s\,e}\,\rho\,k\,h &\definedas& \Tr Ee\,\rho\,(\lookup hs)\,h\\
\Tr E{"try"\,e_1\,"handle"\,(s\,x)\,e_2}\,\rho\,k\,h &\definedas& 
\Tr E{e_1}\,\rho\,k\,(\update hs{(\lam v{\Tr E{e_2}(\update\rho xv)\,k\,h})})\\
\Tr E{\lam xe}\,\rho\,k\,h &\definedas& k~(\nm{FUN}\,(\lam{yk'h'}{\Tr Ee\,(\update\rho xy)\,k'\,h'}))\\
&=& k~(\nm{FUN}\,(\lam y{\Tr Ee\,(\update\rho xy)}))\\
\Tr E{e_0~e_1}\,\rho\,k\,h &\definedas&
\Tr E{e_0}\,\rho\,(\nm{CHECK-FUN}\,(\lam f{\Tr E{e_1}}\,\rho\,(\lam v{fvkh})))
\)

There are some subtle design decisions captured by this translation. For example, if $e_2$ raises exception $s$ in $"try"~e_1~"handle"~(s\,x)~e_2$, in this translation $e_2$ will not be invoked again. That is, $e_2$ cannot be invoked recursively.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
