%%% 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,amsmath,amssymb}

\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]{\mathbf{if\ }#1\mathbf{\ then\ }#2\mathbf{\ else\ }#3}
\newcommand\letin[3]{\mathbf{let\ }#1 = #2\mathbf{\ in\ }#3}
\newcommand\letrec[5]{\mathbf{letrec\ }#1 = #2\mathbf{\ and\ \ldots\ and\ }#3 = #4\mathbf{\ in\ }#5}
\newcommand\letrecone[3]{\mathbf{letrec\ }#1 = #2\mathbf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathbf{true}}}
\newcommand\false{\ensuremath{\mathbf{false}}}
\newcommand\error{\ensuremath{\mathbf{error}}}

\newcommand\LIST{\nm{LIST}}
\newcommand\HEAD{\nm{HEAD}}
\newcommand\TAIL{\nm{TAIL}}
\newcommand\NULL{\nm{NULL}}
\newcommand\EMPTY{\nm{EMPTY}}
\renewcommand\FALSE{\nm{FALSE}}
\renewcommand\TRUE{\nm{TRUE}}
\renewcommand\IF{\nm{IF}}
\newcommand\PAIR{\nm{PAIR}}
\newcommand\FIRST{\nm{FIRST}}
\newcommand\SECOND{\nm{SECOND}}
\newcommand\EQ{\nm{EQ}}
\newcommand\ERROR{\nm{ERROR}}
\newcommand\ID{\nm{ID}}

\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\Cl{\Set{Cl}}
\newcommand\Env{\Set{Env}}
\newcommand\Val{\Set{Val}}
\newcommand\Var{\Set{Var}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{11}                  %% Lecture number
\title{Computing with Closures}   %% Title of lecture
%\author{Yookyung Jo, Lars Backstrom}       %% name of scribe
\date{22 September 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})

\begin{document}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In implementations of ML, Scheme, or other functional languages with static scoping, functions $\lam xe$ are paired with the environments $\rho$ in which they were defined.  The pair $\p{\lam xe}\rho$ is called a \emph{closure}.  The environment $\rho$ tells how to evaluate the free variables of $\lam xe$.  Usually the environment is a partial function from variables to values that is defined on (at least) all the free variables of $\lam xe$.  It is obtained from the syntactic context in which $\lam xe$ appears.

When a function $\lam xe$ is called on some argument $a$, the argument is bound to the formal parameter $x$, and this new binding is appended to the environment $\rho$ in the closure, then the body is evaluated in that environment.

The process of converting a function to take an environment as an extra argument is called \emph{closure conversion}.  In this lecture we will see why this works by proving a theorem that shows how closures adequately represent static scoping.

\section*{Closures and Environments}

Formally, a \emph{value} of $\lambda$-CBV is a closed CBV-irreducible $\lambda$-term, which is just a closed term of the form $\lam xe$.  Let \Val\ denote the set of $\lambda$-CBV values, and let \Set L\ denote the set of all closed $\lambda$-terms (not necessarily CBV-irreducible).

Now we define a new language whose values are closures $\p{\lam xe}\rho$, where $\lam xe$ is permitted to have free variables, provided they are all in the domain of $\rho$.  Because the definitions of closures and environments depend on each other, we must define them by mutual induction.  We denote the set of closures and the set of environments by \Cl\ and \Env, respectively.  They are defined to be the smallest sets such that
\begin{eqnarray*}
\Env &=& \{\mbox{partial functions $\rho:\Var\rightarrow\Cl$ with finite domain}\},\\
\Cl &=& \{\p{\lam xe}\rho \mid \rho\in\Env,\ \FV{\lam xe}\subseteq\dom\rho\}.
\end{eqnarray*}
This definition may seem circular, but actually it does have a basis.  We have $\bot\in\Env$, where $\bot$ is the null environment with domain $\varnothing$, therefore $\p{\lam xe}\bot\in\Cl$, where $\lam xe$ is closed.  Once we know that \Cl\ and \Env\ are nonempty, we can form some nonnull environments $\rho:\Var\rightarrow\Cl$ and closures $\p{\lam xe}\rho$ where $\lam xe$ is not closed, provided $\rho$ is defined on all free variables of $\lam xe$.  This allows us to form even more closures and environments, and so on.  After countably many steps, we reach a fixpoint.

Permitting environments to be partial functions is essential, but the restriction to finite domain is not.  We need to allow partial functions so that the induction will get off the ground, i.e.\ $\Cl\neq\varnothing$.  The restriction to finite domain makes the monotone map in the definition finitary, which ensures that the construction closes at $\omega$.  Without this restriction we would have to use transfinite induction.

More concretely, define
\begin{eqnarray*}
\Cl_0 &\definedas& \varnothing\\
\Env_n &\definedas& \{\mbox{partial functions $\rho:\Var\rightarrow\Cl_n$ with finite domain}\}\\
\Cl_{n+1} &\definedas& \{\p{\lam xe}\rho \mid \rho\in\Env_n,\ \FV{\lam xe}\subseteq\dom\rho\}\\
\Env &\definedas& \bigcup_{n\geq 0} \Env_n\\
\Cl &\definedas& \bigcup_{n\geq 0} \Cl_n.
\end{eqnarray*}
Note that
\begin{eqnarray*}
\Env_0 &=& \{\bot\}\\
\Cl_1 &=& \{\p{\lam xe}\bot \mid \mbox{$\lam xe$ is closed}\}.
\end{eqnarray*}

\section*{Iterated Substitution}

Let \Set C\ denote the set of pairs $\p e\rho$, where $e$ is any $\lambda$-term (not necessarily closed or CBV-irreducible) and $\rho$ an environment such that $\FV e\subseteq\dom\rho$.  Every such pair gives rise to a closed $\lambda$-term obtained by ``iterated substitution''.  This is given by a map $F:\Set C\rightarrow\Set L$ defined inductively as follows:
\begin{eqnarray*}
F(\p e\rho) &\definedas& \subst e{F(\rho(y))}{y,\ y\in\dom\,\rho}.
\end{eqnarray*}
Again, this may seem like a circular definition, but it's not.

\medskip\noindent
\textbf{Lemma}\quad
$F$ is well-defined on \Set C\ and takes values in \Set L.  On inputs in \Cl, $F$ takes values in \Val.

\medskip
\textit{Proof}.
Induction on the stage of definition of $\rho\in\Env$.  Consider first $\p e\rho\in\Set C$ with $\rho\in\Env_0$. Then $\rho=\bot$, $e$ is closed, and
\begin{eqnarray*}
F(\p e\bot) &\definedas& \subst e{F(\bot(y))}{y,\ y\in\dom\bot}\ \ =\ \ e.
\end{eqnarray*}
If $\rho\in\Env_{n+1}$, then $\rho$ takes values in $\Cl_{n+1}$.  Then for all $y\in\dom\rho$, $\rho(y)=\p{\lam xd}\sigma$ for some $\sigma\in\Env_n$, $\FV{\lam xd}\subseteq\dom\sigma$.  By the induction hypothesis, $F(\rho(y))\in\Set L$.  Then
\begin{eqnarray*}
F(\p e\rho) &=& \subst e{F(\rho(y))}{y,\ y\in\dom\rho}\ \ \in\ \ \Set L.
\end{eqnarray*}

$F$ takes values in \Val\ on inputs in \Cl, because 
\begin{eqnarray*}
F(\p{\lam xe}\rho) &=& \subst{(\lam xe)}{F(\rho(y))}{y,\ y\in\dom\rho},
\end{eqnarray*}
which is closed and CBV-irreducible, thus a value of $\lambda$-CBV.
\mbox{\ }\hfill$\Box$

\section*{$\lambda$-Cl}

The terms of $\lambda$-Cl consist of the elements of \Set C.  The values of $\lambda$-Cl are elements of \Cl.  We now give a set of evaluation rules defining a big-step SOS for a binary relation $\Downarrow\;\subseteq\Set C\times\Cl$.  The statement $\p e\rho\Downarrow v$ should be interpreted as: When $e$ is evaluated in the environment $\rho$, the result is $v$.  Given this informal meaning, these rules below reflect the usual evaluation rules for functional expressions in Scheme or ML.
\[
\p x\sigma\Downarrow\sigma(x) \hspace{1cm} \p{\lam xe}\rho\Downarrow\p{\lam xe}\rho \hspace{1cm}
\frac{\p{e_1}\sigma\Downarrow\p{\lam xe}\tau,\quad\p{e_2}\sigma\Downarrow u,\quad\p e{\tau[u/x]}\Downarrow v}{\p{e_1\,e_2}\sigma\Downarrow v}.
\]
Note that the rule for $\lambda$-abstractions is just the identity relation!  This rule says that evaluating $\lam xe$ in the environment $\rho$ results in the closure $\p{\lam xe}\rho$.

The third rule is the usual rule for evaluation of a function application: to evaluate $e_1\,e_2$ in the environment $\sigma$, first evaluate the function $e_1$ in environment $\sigma$ to get a closure $\p{\lam xe}\tau$, then evaluate the argument $e_2$ in environment $\sigma$, then bind the value of the argument to the formal parameter $x$ in the environment of the closure $\tau$ and evaluate the body of the function in that environment.

By the lemma above, the ``iterated substitution'' map $F$ translates $\lambda$-Cl expressions to $\lambda$-CBV expressions and $\lambda$-Cl values to $\lambda$-CBV values.  The following theorem asserts adequacy of this translation.

\medskip\noindent
\textbf{Theorem}\quad
$F(\p e\sigma)\goesto *{\mathrm{cbv}} v\ \ \Leftrightarrow\ \ \exists w\ \p e\sigma\Downarrow w \wedge v=F(w)$.

\medskip
\textit{Proof}.
($\Leftarrow$)\quad
We wish to show that if $\p e\sigma\Downarrow w$, then $F(\p e\sigma)\goesto *{\mathrm{cbv}} F(w)$.  The proof is by induction on the derivation $\p e\sigma\Downarrow w$.

For the case $e=x$, we have $\p x\sigma\Downarrow\sigma(x)$.  In this case $F(\p x\sigma)=\subst x{F(\sigma(y))}{y,\ y\in\dom\sigma}=F(\sigma(x))$, so $F(\p x\sigma)\goesto *{\mathrm{cbv}} F(\sigma(x))$. 

For the case $\lam xe$, we have $\p{\lam xe}\sigma\Downarrow\p{\lam xe}\sigma$.  In this case $F(\p x\sigma)\goesto *{\mathrm{cbv}} F(\p x\sigma)$ trivially. 

Finally, for the case $e_1\,e_2$, if $\p{e_1\,e_2}\sigma\Downarrow w$, for some $\lam xd$, $\tau$, and $u$, we must have
\begin{itemize}
\item
$\p{e_1}\sigma\Downarrow\p{\lam xd}\tau$;
\item
$\p{e_2}\sigma\Downarrow u$;
\item
$\p d{\tau[u/x]}\Downarrow w$.
\end{itemize}
By the induction hypothesis,
\begin{itemize}
\item
$F(\p{e_1}\sigma)\goesto *{\mathrm{cbv}} F(\p{\lam xd}\tau)$;
\item
$F(\p{e_2}\sigma)\goesto *{\mathrm{cbv}} F(u)$;
\item
$F(\p d{\tau[u/x]})\goesto *{\mathrm{cbv}} F(w)$.
\end{itemize}
Under CBV semantics, we have
\begin{eqnarray*}
F(\p{e_1\,e_2}\sigma) &=& F(\p{e_1}\sigma)~F(\p{e_2}\sigma)\\
&\goesto *{\mathrm{cbv}}& F(\p{\lam xd}\tau)~F(u)\\
&=& \subst{(\lam xd)}{F(\tau(y))}{y,\ y\in\dom\tau}~F(u)\\
&=& (\lam x{\subst d{F(\tau(y))}{y,\ y\in\dom\tau,\ y\neq x}})~F(u)\\
&\goesto{}{\beta}& \subst{\subst d{F(\tau[u/x](y))}{y,\ y\in\dom\tau,\ y\neq x}}{F(\tau[u/x](x))}x\\
&=& \subst d{F(\tau[u/x](y))}{y,\ y\in\dom\tau}\\
&=& F(\p d{\tau[u/x]})\\
&\goesto *{\mathrm{cbv}}& F(w).
\end{eqnarray*}

($\Rightarrow$)\quad
Suppose $F(\p e\sigma)\goesto *{\mathrm{cbv}} v$.  We proceed by induction on the length of the derivation.

For the case $e=x$, we have $\p x\sigma\Downarrow\sigma(x)$ and
\begin{eqnarray*}
F(\sigma(x)) &=& \subst x{F(\sigma(y))}{y,\ y\in\dom\sigma}\ \ =\ \ F(\p x\sigma)\ \ \goesto *{\mathrm{cbv}}\ \ v.
\end{eqnarray*}
By the lemma, $F(\sigma(x))\in\Val$, therefore $F(\sigma(x))=v$, so we can take $w=\sigma(x)$.

For the case $\lam xe$, we have $\p{\lam xe}\sigma\Downarrow\p{\lam xe}\sigma$ and
\begin{eqnarray*}
F(\p{\lam xe}\sigma) &=& \subst{(\lam xe)}{F(\sigma(y))}{y,\ y\in\dom\sigma}\ \ \in\ \ \Val,
\end{eqnarray*}
thus $F(\p{\lam xe}\sigma)=v$, so we can take $w=\p{\lam xe}\sigma$.

Finally, for the case $e_1\,e_2$, we have
\begin{eqnarray*}
F(\p{e_1\,e_2}\sigma) &=& F(\p{e_1}\sigma)~F(\p{e_2}\sigma)\ \ \goesto *{\mathrm{cbv}}\ \ v.
\end{eqnarray*}
Under the CBV reduction strategy, the only way this can happen is if 
\begin{itemize}
\item $F(\p{e_1}\sigma) \goesto *{\mathrm{cbv}} \lam xd$;
\item $F(\p{e_2}\sigma) \goesto *{\mathrm{cbv}} u$;
\item $\subst dux \goesto *{\mathrm{cbv}} v$
\end{itemize}
for some $\lam xd$ and $u\in\Val$.  These are all shorter derivations than the original derivation $F(\p e\sigma)\goesto *{\mathrm{cbv}} v$, so by the induction hypothesis there exist $\lam xe$, $\rho$, and $t\in\Cl$ such that
\begin{itemize}
\item $\p{e_1}\sigma \Downarrow \p{\lam xe}\rho$ and $F(\p{\lam xe}\rho)=\lam xd$;
\item $\p{e_2}\sigma \Downarrow t$ and $F(t)=u$.
\end{itemize}
Then
\begin{eqnarray*}
\lam xd &=& \subst{(\lam xe)}{F(\rho(y))}{y,\ y\in\dom\rho}\\
&=& \lam x{(\subst e{F(\rho(y))}{y,\ y\in\dom\rho,\ y\neq x})},
\end{eqnarray*}
therefore $d = \subst e{F(\rho(y))}{y,\ y\in\dom\rho,\ y\neq x}$, and
\begin{eqnarray*}
\subst dux &=& \subst{\subst e{F(\rho(y))}{y,\ y\in\dom\rho,\ y\neq x}}ux\\
&=& \subst{\subst e{F(\rho(y))}{y,\ y\in\dom\rho,\ y\neq x}}{F(t)}x\\
&=& \subst{\subst e{F(\rho[t/x](y))}{y,\ y\in\dom\rho,\ y\neq x}}{F(\rho[t/x](x))}x\\
&=& \subst e{F(\rho[t/x](y))}{y,\ y\in\dom\rho}\\
&=& F(\p e{\rho[t/x]}).
\end{eqnarray*}
By the induction hypothesis, $\p e{\rho[t/x]}\Downarrow w$ and $F(w)=v$ for some $w$, therefore $\p{e_1\,e_2}{\rho[t/x]}\Downarrow w$.
\mbox{\ }\hfill$\Box$

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
