\documentclass{article} \usepackage{611-lecture}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{17}                 %% Lecture number
\title{Exceptions and Continuations}   %% Title of lecture
\author{Wyatt Schweizer, David Crandall}       %% name of scribe
\date{5 October 2005}     %% 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})

\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 (translation to uML + strongly typed)}

\subsection{Value translation}

To support strong typing, we introduce a translation $\Tr V{v}$ that
tags each value with its type.
For example:

\[
\begin{array}{rcll}
\Tr V{null} & = & (1, null) & (\mbox{null values})\\
\Tr V{true} & = & (2, true) & (\mbox{boolean values})\\
\Tr V{n} & = & (3, n) & (\mbox{integers})\\
\Tr V{(3,4)} & = & (4, (\Tr V 3,\Tr V 4)) & (\mbox{pairs})\\
\Tr V{\lam{x}{x}} & = & (5, \lam{x}{x}) & (\mbox{functions})\\
\end{array}
\]

\noindent
We can define the functions \nm{NULL}, $\nm{BOOL}(b)$, $\nm{INT}(n)$,
$\nm{PAIR}(p)$, $\nm{FUN}(f)$,
etc. to perform these tagging operations.  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 translated value is of the correct type, and to
return the original value.  For example, $\nm{CHECK-PAIR}$ is defined as:

\begin{eqnarray*}
\nm{CHECK-PAIR} = \lam{kv}{\IF~\#1~v = 4~\THEN~k~(\#2~v)~\ELSE~"halt"~\nm{ERROR}}\\
\end{eqnarray*}

\noindent
where the parameter $k$ is a continuation and the parameter $v$ is a translated
value (consisting of a pair with a value and a type tag).
If the tag equals 4 (the tag we have assigned to pairs), then
we pass the value to the continuation. Otherwise we have encountered a run-time type
error, so we halt and return an error value. The implementations of
\nm{CHECK-X} and \nm{X} need to satisfy the following equations:
$\nm{CHECK-X}~k~(X(v)) = k v$ but
$\nm{CHECK-X}~k~(X'(v)) = "halt"~\nm{ERROR}$  for $X \neq X'$.
If this is true then there is some flexibility about how we implement
the functions.  For example, we could let
$\nm{halt} = \lam{x}{x}$ and $\nm{ERROR}=(0,0)$.


\subsection{Expression translation}

We will be discussing translations of the form $\Tr E{e} \rho k$, 
which means ``send result of expression e in environment $\rho$ to continuation $k$.'' 
Then the translations are:

\begin{itemize}
\item Variable:  
	$$\Tr E{x} \, \rho \, k = k \, (\rho \, \mquote{x})$$ 

\item Constant: 
	$$\Tr E{n} \, \rho \, k = k~(\nm{INT}(n))$$ 
\item Pair: 
$$ \Tr E {(e_1,e_2)} \, \rho \, k = \Tr E {e_1} \, \rho \, \lam{v_1}{\Tr
E {e_2} \, \rho \,  (\lam{v_2}{k \,  ( \nm{PAIR}( v_1, \, v_2))})}$$ 

\item \# :  
$$		\Tr E {\#1 \, e} \, \rho \, k  =  \Tr E {e} \, \rho \,
(\nm{CHECK-PAIR}(\lam{p}{k~(\#1~p)}) $$

\item Lambda: 
	$$\Tr E {\lam{x}{e}} \, \rho \, k = k \, \nm{FUN}(\lam{yk'}{\Tr
	E {e} \, (\nm{EXTEND} \, \rho \, \mquote{x} \, y)  \, k')}$$	 

\item Application: 
	$$\Tr E {e_0 \, e_1} \, \rho \, k = \Tr E {e_0} \, \rho \, (\nm{CHECK-FUN}(\lam{f}{\Tr E {e_1} \, \rho \, (\lam{v}{f \, v \, k})}))$$ 
\item If-Then-Else: 
          $$\Tr E {\IF \, e_0 \, \THEN \, e_1 \, \ELSE \, e_2} \, \rho
	  \, k  = \Tr E {e_0} \, \rho \, (\nm{CHECK-BOOL}(\lam{b}{\IF \, b \, \THEN \, \Tr E {e_1} \, \rho \, k \, \ELSE \, \Tr E {e_2} \, \rho \, k}))$$ 
\end{itemize}

There are a few more cases, like let, that were not covered in lecture.


\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 E {e} \rho
k \sigma$, or ``send the result of $e$ under environment $\rho$ and
store $\sigma$ to the continuation $k$.''  A continuation is now a
function of a value and a store; i.e. a continuation $k$ has the
form: $$k=\lam{v}{\lam{\sigma}{\ldots}}$$ The translations for uML!
are:


\begin{itemize}
\item Variable Name:
	$$ \Tr E {x} \, \rho \, k \, \sigma = k \, (\rho \, \mquote{x} ) \, \sigma$$

Note that you can think about this translation as a function and $\eta$-reduce away the $\sigma$:
	\begin{eqnarray*}
	\Tr E {x} \, \rho \, k & = & (\lam{\sigma}{k \, (\rho \, \mquote{x})) \, \sigma}\\
	& = & k \, (\rho \, \mquote{x})\\
	\end{eqnarray*}
\end{itemize}

Notice 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 only need to add 3
translation rules for the functionality that is state-aware.
(We assume that we have functions \nm{LOC} and \nm{CHECK-LOC} for
tagging values as locations and checking those tags.)

\begin{itemize}
\item Creating a Reference:
\[
\Tr E {Ref e}~\rho~k~\sigma = \Tr E {e}~\rho~
              (\lam{v \sigma'}{\LET~(l,\sigma'')=(\nm{MALLOC}~\sigma'~v)
	                      ~\IN~k~(\nm{LOC}~l)~\sigma''})~\sigma
\]

\item Dereferencing:
\[
\Tr E {!e}~\rho~k = \Tr E {e}~\rho~(\nm{CHECK-LOC}(
                    \lam{l \sigma'}{k~(\nm{LOOKUP}~\sigma'~l)~\sigma'}))
\]

\item Assignment:
\[
    \Tr E {e_1 :=e_2}~\rho~k = \Tr E {e_1}~\rho~(\nm{CHECK-LOC}(
                    \lam{l}{\Tr E {e_2}~\rho~(\lam{v \sigma'}
		        {k~\nm{NULL}~(\nm{UPDATE}~\sigma'~l~v)})}))
\]

\noindent
(Note that a number of $\sigma$'s have been eta-reduced here.)

\end{itemize}

An initial program is $\Tr E {e}~\rho_0~"halt"~\sigma_0$.


\section{Exceptions}

An exception mechanism allows non-local transfer of control, typically for ``exceptional''
conditions. It can simplify code by allowing programmers to ``factor out'' uncommon cases, like error conditions.
We now add an exception handling mechanism to uML. First, we extend the syntax of uML as follows:

\begin{eqnarray*}
e & ::= & ... \\
  & |   & \mathsf{raise} \, s \, e \\
  & |   & \mathsf{try} \, e_1 \, \mathsf{catch} \, (s \, x) \, e_2
\end{eqnarray*}

\noindent
Informally, the idea here is that $\textsf{catch}$ provides
a handler $e_2$ to be invoked when the exception named $s$ is encountered
inside the expression $e_1$. To throw an exception, the program
calls $\textsf{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. That is, 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 define two
new functions: \nm{LOOKUP-HANDLER}, returns the continuation corresponding to an
exception name in an environment, and EXTEND-HANDLER,
which takes a handler environment, exception name, and continuation, and returns
a new handler environment in which the exception name is mapped to the continuation.

Now we can add exception support to our translation:

\begin{eqnarray*}
\Tr E { \mathsf{raise} \, s \, e} \, \rho \, k \, h & =  & \Tr E {e} \,
\rho \, (\nm{LOOKUP-HANDLER} \, h \, s) \,  h \\
\Tr E { \mathsf{try} \, e_1 \, \mathsf{catch} \, (s \, x) \, e_2 } \, \rho \, k \, h & = & 
  \Tr E {e_1} \, \rho \, k \, (\nm{EXTEND-HANDLER} \, h \, s \,
  (\lam{v}{\Tr E {e_2} (\nm{EXTEND} \, \rho \, ``x'' \, v) \, k \, h})) \\
\Tr E { \lam{x}{e} } \, \rho \, k & = & k \, (\nm{FUN} \, (\lam{y k'
h'}{\Tr E{e} \, (\nm{EXTEND} \, \rho \, ``x'' \, y) \, k'})) \\
\Tr E { e_0 \, e_1 } \, \rho \, k \, h & = & \Tr E {e_0} \, \rho \, (\nm{CHECK-FUN} (\lam{f}{\Tr E{e_1}} \, \rho \, (\lam{v}{f \, v \, k \, h}))) \\
\end{eqnarray*}

Note that there are some subtle design decisions captured by this
translation. For example, note that if $e_2$ throws exception $s$ in
$\mathsf{try} \, e_1 \, \mathsf{catch} \, (s \, x) \, e_2$, in this
translation $e_2$ will not be invoked again. That is, $e_2$ cannot be
invoked recursively. We could write a translation to do this, but this
would involve finding a fixed point of $e_2$.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
