%%% This is the scribe notes template for CS611
%%% There are several comments preceded by CS611: and boxed in %%%%'s 
%%% which indicate where macros should be altered to set up the header
%%% for the paper.  Your Notes should go at the comment SCRIBE NOTES GO HERE!.

%%% In the various .sty files that accompany this .tex file you will    
%%% find LaTeX macros that make it easier to typeset inference rules    
%%% and programming language constructs.  You must make sure that the   
%%% file proof.sty is in a path searched by LaTeX when you try to       
%%% use this file.  Take a look to see what macros are available--it    
%%% will save you time and make the notes look better.  Feel free to    
%%% extend the set of macros--post them to the newsgroup and contact    
%%% the course staff if you come up with some good ones so they can be  
%%% added to the template.                                              

%%% This template includes examples of how to use some of the macros
%%% to give you an idea of how they work.  (Delete the examples when
%%% you do your scribing.)

\documentclass{article} \usepackage{611-lecture}
\usepackage{amsthm}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{29}                  %% Lecture number
\title{Standard semantics and first-class continuations}   %% Title of lecture
\author{Yisong Yue}       %% name of scribe
\date{7, November, 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)
%          	(if x is more than a single character, use \Tr{x}{y})

\begin{document}

\maketitle

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Delete this example text...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{CPS Language}
Recall:
\begin{eqnarray*}
 e & ::= & x \bnf b \bnf "halt" \bnf \lam{x_1\ldots x_n}{s} \\
 s & ::= & e_0~e_1\ldots e_n
\end{eqnarray*}
When we do an application, it has no return value, so what is the type
of CPS statements?  We will use the type $0$, the void type
(not to be confused with the unit type). This will be type of the
``result'' of applying a continuation:
\[  \lam{x_1\ldots x_n}{s} \ : \ \tau_1*\ldots*\tau_n \rightarrow 0  \]
The unit type gives a result (i.e. you know when it finishes).  The void
type gives no result at all: it means that not even control returns to
the caller. So it could be the type of a nonterminating expression
(e.g., $"rec"~y:"int"\to 0.\lam{x}{y~x}$) or
the type of an expression that transfers control elsewhere.
Using this type we can define the typing derivation rules for CPS.
\[ \tau \ ::= \ B \bnf \tau_1*\ldots*\tau_n\rightarrow 0 \]
\[
\begin{array}{cc}
\infer{\Gamma \vdash "halt" \ : \ \tau \rightarrow 0}{} \\ \\
\infer{\Gamma \vdash b \ : \ B}{} &
\infer{\Gamma,x\ty\tau \vdash  x : \tau}{} \\ \\
\infer{\Gamma \vdash \lam{x_1\ty\tau_1\ldots x_n\ty\tau_n}{s} \ : \
\tau_1*\ldots*\tau_n \rightarrow 0}{\Gamma,x_1\ty\tau_1,\ldots,x_n\ty\tau_n \vdash s \ : \ 0} &
\infer{\Gamma \vdash e_0~e_1\ldots e_n : 0}%
{\Gamma \vdash e_0:\tau_1*\ldots*\tau_n \rightarrow 0 \ \ \ \Gamma \vdash
e_i:\tau_i ^{(i \in 1..n)}}
\end{array}
\]
We didn't care what 0 was, thus showing the value of something of type
0 is immaterial.
\section{CPS Translation}
$\lambda^{\rightarrow} \rightarrow \lambda^{\rightarrow}_{CPS}$ type translations: 
\[\SB{B} = B \ \ \ \ \SB{\tau \rightarrow \tau'} =
(\SB{\tau'}\rightarrow 0)*\SB{\tau}\rightarrow 0\] 
Where $\SB{\tau'}\rightarrow 0$ is the continuation and $\SB{\tau}$ is the function argument.

We can also translate typing derivations.  Note that there are three different types of translations being used here.  $\SB{\Gamma}$, by the extensional representation, is the set of pairs $(x_i,\SB{\tau_i}), \ \forall (x_i,\tau_i) \in \Gamma$
\[\SB{\Gamma} \vdash \SB{\Gamma \vdash e \ : \ \tau} \ : \
(\SB{\tau}\rightarrow 0)\rightarrow 0\]

Translation rules:
$\SB{\Gamma,x:\tau\vdash x \ : \ \tau} = $
\[\lam{k\ty\SB{\tau}\rightarrow 0}{k~x}\]
$\SB{\Gamma\vdash \lam{x\ty\tau}{e} \ : \ \tau\rightarrow\tau'} = $
\[\lam{k\ty((\SB{\tau'}\rightarrow 0)*\SB{\tau}\rightarrow 0)\rightarrow
0}{(\lam{k'\ty(\SB{\tau'}\rightarrow
0),x\ty\SB{\tau}}{\SB{\Gamma,x\ty\tau\vdash e \ : \ \tau'}~k'})}\]
$\SB{\Gamma\vdash e_0~e_1 \ : \ \tau'} = $
\[\lam{k\ty(\SB{\tau'}\rightarrow 0)}{\SB{\Gamma\vdash e_0 \ : \
\tau\rightarrow\tau'}(\lam{f\ty((\SB{\tau'}\rightarrow
0)*\SB{\tau}\rightarrow 0)}{\SB{\Gamma\vdash e_1 \ : \
\tau}(\lam{v\ty\SB{\tau}}{f~k~v})}}\]

\section{Continuation (Standard) Semantics}
This is almost the same as the operational semantics, with just some symbols replaced.
\begin{center}
\begin{tabular}{ccccccc}
$:\rightarrow\in$ & \ & $0\rightarrow \Set{Ans}$ & \ &
$\SB{int}\rightarrow \mathbb{Z}$ & \ &$\SB{\tau\rightarrow\tau'} =
(\SB{\tau'}\rightarrow \Set{Ans})\times(\SB{\tau}\rightarrow \Set{Ans})$ \\
\end{tabular}
\end{center}

Here the domain $\Set{Ans}$ represents the codomain of a continuation,
corresponding to the type 0.  Nothing in the semantics depends on the
choice of this domain---in a sense, the semantics are parameterized
with respect to the choice of this domain, and with respect to the
meaning of "halt" (which must be a function in $\SB{\tau}\to\Set{Ans}$).

\section{First-Class Continuations}

First-class continuations exist in many languages, such as Scheme and SML/NJ.  In SML/NJ cont module, continuations have type \texttt{$\alpha$ cont}, and the following two functions are used for continuations:

\begin{eqnarray*}
"callcc" & : & (\alpha~"cont" \rightarrow \alpha)\rightarrow\alpha \\
"throw" & : & \alpha~"cont" \rightarrow \alpha \rightarrow \beta
\end{eqnarray*}

Suppose that you're given some error-prone code (which wouldn't be all that hard to imagine, given the state of the general populace's coding acumen).  You'll want to have error checking done within the body of your code.  Should an error be encountered, it should be handled gracefully and the outer code should continue to execute.  

\ \ \texttt{fun f(x)(k) = \ldots \ldots throw k result \ldots}
\ \ \texttt{"callcc" (f x)}

"callcc" captures the current continuation and passes it into (f x)
\section{Adding SML/NJ continuations to $\lambda^{\rightarrow}$}
$\SB{\tau~"cont"} = $
\[\SB{\tau}\rightarrow 0\]

$\SB{\Gamma\vdash "callcc" \ e \ : \ \tau}k = $
\[\SB{\Gamma\vdash e  : \tau~"cont" \rightarrow \tau}
(\lam{f:((\SB{\tau}\rightarrow 0)*(\SB{\tau}\rightarrow 0)\rightarrow
0)}{f~k~k})\]
The first $k$ in the $(f~k~k)$ term refers to the continuation passed
into $f$ and used by any "throw" statements.
The second $k$ is the normal continuation.

$\SB{\Gamma\vdash "throw"~e_k~e_v : \tau_0}k =$
\[\SB{\Gamma\vdash e_k \ : \ \tau~"cont"}(\lam{k:(\SB{\tau}\rightarrow
0)}{\SB{\Gamma \vdash e_v \ : \ \tau}(\lam{v:\SB{\tau}}{k~v})})\]

\section{Implementing threads with continuations}

First-class continuations are more powerful than they might appear
at first glance. They can be used for much more than error handling.
For example, they can be used to implement nonpreemptive threads.
Here, "spawn" creates a new thread that executes the application
of "f" in parallel with the current thread.

\begin{verbatim}
  type thread = unit cont
  val ready = thread queue

  fun enqueue(t) = insert(ready, t)
  
  fun dispath() = throw(dequeue ready)()
  
  fun spawn(f:unit->unit) = callcc(
     fun(curr:thread)=> enqueue(curr); f(); dispatch() )

  fun yield = callcc(
    fun(curr:thread)=> enqueue(curr); dispatch() )
\end{verbatim}


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