%%% 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{amsmath}

\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\ID{\mathrm{ID}}
\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\true{\ensuremath{\mathbf{true}}}
\newcommand\false{\ensuremath{\mathbf{false}}}
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{9}                  %% Lecture number
\title{Semantics via Translation}   %% Title of lecture
%\author{Olga Belomestnykh}       %% name of scribe
\date{18 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!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Overview}

Our goal is to study basic programming language features using the semantic techniques we know: 
\begin{itemize}
\item small-step operational semantics;
\item big-step operational semantics;
\item translation.
\end{itemize}
We will mostly use small-step semantics and translation. 

\section{Translation}

For translation, we map well-formed programs in the original language into items in a \emph{meaning space}. These items may be 
\begin{itemize}
\item programs in an another language (definitional translation);
\item mathematical objects (denotational semantics); an example is taking $\lam{x:\mathsf{int}}x$ to $\{(0,0),(1,1),\ldots\}$.
\end{itemize}

Because they define the meaning of a program, these translations are also known as \emph{meaning functions} or \emph{semantic functions}.  We usually denote the semantic function under consideration by $\SB\cdot$.  An object $e$ in the original language is mapped to an object $\SB e$ in the meaning space under the semantic function.  We may occasionally add an annotation to distinguish between different semantic functions, as for example $\SB e_{\mathrm{cbn}}$ or $\Tr Ce$.

\section{Translating CBN $\lambda$-Calculus into CBV $\lambda$-Calculus}

The call-by-name (lazy) $\lambda$-calculus was defined with the following reduction rule and evaluation contexts:
\begin{eqnarray*}
(\lam x{e_1})~e_2\ \rightarrow\ \subst{e_1}{e_2}x
&\quad&
E\ ::=\ \hole \bnf E~e.
\end{eqnarray*}
The call-by-value (eager) $\lambda$-calculus was similarly defined with
\begin{eqnarray*}
(\lam xe)~v\ \rightarrow\ \subst evx
&\quad&
E\ ::=\ \hole \bnf E~e \bnf v~E.
\end{eqnarray*}

To translate from the CBN $\lambda$-calculus to the CBV $\lambda$-calculus, we define the semantic function $\SB{\cdot}$ by induction on the syntactic structure: 
\begin{eqnarray*}
\SB x &\definedas& x\cdot\ID\\
\SB{\lam xe} &\definedas& \lam x{\SB e}\\ 
\SB{e_1~e_2} &\definedas& \SB{e_1}~(\lam z{\SB{e_2}}),\quad\mbox{where $z\notin\FV{\SB{e_2}}$.}
\end{eqnarray*}

The idea is to wrap the parameters to functions inside $\lambda$-abstractions to delay their evaluation, then to finally pass in a dummy parameter to expand them out. 

For an example, recall that we defined: 
\begin{eqnarray*}
\TRUE  &\definedas& \lam{xy}x\\
\FALSE  &\definedas& \lam{xy}y\\ 
\IF &\definedas& \lam{xyz}xyz.
\end{eqnarray*}
The problem with this construction in the CBV $\lambda$-calculus is that $\IF~b~e_1~e_2$ evaluates both $e_1$ and $e_2$, regardless of the truth value of $b$.  Perhaps the conversion above can be used to fix these to evaluate them lazily.
\begin{eqnarray*}
\SB{\true} &=& \SB{\lam{xy}x}\\
&=& \lam{xy}{\SB x}\\
&=& \lam{xy}{x~\ID}\\
\SB{\false} &=& \lam{xy}{y~\ID}\\
\SB{\IF} &=& \SB{\lam{xyz}{xyz}}\\
&=& \lam{xyz}{\SB{(xy)z}}\\
&=& \lam{xyz}{\SB{xy}~(\lam d{\SB z}})\\
&=& \lam{xyz}{\SB x~(\lam d{\SB y})~(\lam d{\SB z}})\\
&=& \lam{xyz}{(x~\ID)~(\lam d{y~\ID})~(\lam d{z~\ID}}).
\end{eqnarray*}
This is not a complete solution, as the conversion does not work for all expressions, but only fully converted ones.  But if used as intended, it has the desired effect.  For example, evaluating under the CBV rules,
\begin{eqnarray*}
\SB{\IF~\true~e_1~e_2}
&=& \SB{\IF}~(\lam d{\SB{\true}})~(\lam d{\SB{e_1}})~(\lam d{\SB{e_2}})\\
&=& (\lam{xyz}{(x~\ID)~(\lam d{y~\ID})~(\lam d{z~\ID})})~(\lam d{\SB{\true}})~(\lam d{\SB{e_1}})~(\lam d{\SB{e_2}})\\
&\rightarrow& ((\lam d{\SB{\true}})~\ID)~(\lam d{(\lam d{\SB{e_1}})~\ID})~(\lam d{(\lam d{\SB{e_2}})~\ID})\\
&\rightarrow& {\SB{\true}}~(\lam d{\SB{e_1}})~(\lam d{\SB{e_2}})\\
&=& (\lam{xy}{x~\ID})~(\lam d{\SB{e_1}})~(\lam d{\SB{e_2}})\\
&\rightarrow& (\lam d{\SB{e_1}})~\ID\\
&\rightarrow& \SB{e_1},
\end{eqnarray*}
and $e_2$ was never evaluated.

\section{Adequacy}

Both the CBV and CBN $\lambda$-calculus are \emph{deterministic} systems in the sense that there is at most one reduction that can be performed on any term.  When an expression $e$ in a language is evaluated in a deterministic system, one of three things can happen:
\begin{enumerate}
\item
The computation can converge to a value: $e\Downarrow v$.
\item
The computation can converge to a non-value.  When this happens, we say the computation is \emph{stuck}.
\item
The computation can diverge: $e\Uparrow$.
\end{enumerate}

A semantic translation is \emph{adequate} if these three behaviors in the source system are accurately reflected in the target system, and vice versa.  One aspect of this relationship is captured in the following diagram:
\begin{center}
\begin{picture}(100,50)
%\thicklines
\put(0,0){\makebox(0,0)[c]{$\SB e$}}
\put(0,50){\makebox(0,0)[c]{$e$}}
\put(50,0){\makebox(0,0)[c]{$v'$}}
\put(50,50){\makebox(0,0)[c]{$v$}}
\put(100,0){\makebox(0,0)[c]{$\SB v$}}
\put(0,8){\vector(0,1){36}}
\put(0,8){\vector(0,-1){0}}
\put(50,8){\vector(0,1){36}}
\put(50,8){\vector(0,-1){0}}
\put(8,0){\vector(1,0){36}}
\put(8,50){\vector(1,0){36}}
\put(54,46){\vector(1,-1){38}}
\put(25,3){\makebox(0,0)[b]{$*$}}
\put(25,53){\makebox(0,0)[b]{$*$}}
\put(75,0){\makebox(0,0)[c]{$\approx$}}
\end{picture}
\end{center}
If an expression $e$ converges to a value $v$ in zero or more steps in the source language, then $\SB e$ must converge to some value $v'$ that is equivalent (e.g.~$\beta$-equivalent) to $\SB v$, and vice-versa.  This is formally stated as two properties, \emph{soundness} and \emph{completeness}.  For our CBN-to-CBV translation, these properties take the following form:

\subsection{Soundness}

\begin{eqnarray*}
\SB e \goesto *{\mathrm{cbv}} v' &\Rightarrow& \exists v\ e \goesto *{\mathrm{cbn}} v \wedge v'\approx\SB v
\end{eqnarray*}
In other words, any computation in the CBV domain starting from the image $\SB e$ of a CBN program $e$ must accurately reflect the computation in the CBN domain.

\subsection{Completeness}

\begin{eqnarray*}
e \goesto *{\mathrm{cbn}} v &\Rightarrow& \exists v'\ \SB e \goesto *{\mathrm{cbv}} v' \wedge v'\approx\SB v
\end{eqnarray*}
In other words, any computation in the CBN domain starting from $e$ must be accurately reflected by the computation in the CBV domain starting from the image $\SB e$.

\subsection{Nontermination}
 
It must also be the case that the source and target agree on nonterminating executions. We write $e\Uparrow$ and say that $e$ \emph{diverges} if there exists an infinite sequence of expressions $e_1,e_2,\ldots$ such that $e\rightarrow e_1\rightarrow e_2\rightarrow\ldots$~.  The additional condition for adequacy is
\begin{eqnarray*}
e\Uparrow_{\mathrm{cbn}} &\Leftrightarrow& \SB e\Uparrow_{\mathrm{cbv}}.
\end{eqnarray*}
The direction $\Leftarrow$ of this implication can be considered part of the requirement for soundness, and the direction $\Rightarrow$ can be considered part of the requirement for completeness.  \emph{Adequacy} is the combination of soundness and completeness.  

\section{Untyped ML (uML)}

Let's construct an example by augmenting the $\lambda$-calculus with some more conventional programming constructs and defining its translation to the CBV $\lambda$-calculus.  We call this language uML since it resembles ML, with the ``u'' standing for ``untyped''. 

\subsection{Expressions}

\begin{eqnarray*}
e &::=& \lam{x_1\ldots x_n}e \bnf e_0~\cdots~e_n \bnf x \bnf n \bnf \true \bnf \false\\
&& \bnf (e_1,\ldots,e_n) \bnf \#n~e \bnf \ifthenelse{e_0}{e_1}{e_2}\\
&& \bnf \letin x{e_1}{e_2}\\
&& \bnf \letrec{f_1}{\lam{x_1}{e_1}}{f_n}{\lam{x_n}{e_n}}e 
\end{eqnarray*}

\subsection{Values}

\begin{eqnarray*}
v &::=& \lam{x_1\ldots x_n}e \bnf n \bnf \true \bnf \false \bnf (v_1,\ldots,v_n) 
\end{eqnarray*}

\subsection{Evaluation Contexts}
 
\begin{eqnarray*}
E &::=& \hole \bnf v_0~\cdots~v_m~E~e_{m+2}~\cdots~e_n \bnf \#n~E\\ 
&& \bnf \ifthenelse E{e_1}{e_2}\\
&& \bnf \letin xEe\\
&& \bnf (v_1,\ldots,v_m,E,e_{m+2},\ldots,e_n) 
\end{eqnarray*}

\subsection{Reductions}

\begin{eqnarray*}
(\lam{x_1\ldots x_n}e)~v_1~\cdots~v_n &\rightarrow& \subst{\subst e{v_1}{x_1}}{v_2}{x_2}~\subst{\cdots}{v_n}{x_n}\\
\#n~(v_1,\ldots,v_m) &\rightarrow& v_n,\quad\mbox{where $1\leq n\leq m$}\\
\ifthenelse{\true}{e_1}{e_2} &\rightarrow& e_1\\
\ifthenelse{\false}{e_1}{e_2} &\rightarrow& e_2\\
\letin xve &\rightarrow& \subst evx\\
\mathbf{letrec}\ \ldots &\rightarrow& \mbox{\textit{to be continued}}
\end{eqnarray*}

We can already see that types will be important for establishing soundness. For example, what happens with the expression $\ifthenelse 310$?  The evaluation is stuck, because there is no reduction rule that applies to this term.

\subsection{Translating uML to the CBV $\lambda$-Calculus}
 
We define some of the translation rules: 

\begin{eqnarray*}
\SB{\lam{x_1\ldots x_n}e} &\definedas& \lam{x_1\ldots x_n}{\SB e}\\
\SB{e_0~\cdots~e_n} &\definedas& \SB{e_0}~\SB{e_1}~\SB{e_2}~\cdots~\SB{e_n}\\
\SB x &\definedas& x\\
\SB n &\definedas& \lam{fx}{f^n x}\\
\SB\true &\definedas& \lam{xy}{x~\ID}\\
\SB\false &\definedas& \lam{xy}{y~\ID}\\
\SB{\ifthenelse{e_0}{e_1}{e_2}} &\definedas& \SB{e_0}~(\lam z{\SB{e_1}})~(\lam z{\SB{e_2}}).
\end{eqnarray*}

Revisiting our earlier example $\ifthenelse 310$, we see that the translation to CBV is not sound, because its image $\SB{\ifthenelse 310}$ reduces to a value under the CBV rules---there is no way for a closed term to get stuck in the CBV or CBN $\lambda$-calculus, as we proved last time.  However, this value does not correspond to the stuck non-value $\ifthenelse 310$ in the uML language.

One possible solution to this difficulty is to introduce rules that reduce stuck expressions to a special error value. This is essentially the same as runtime type checking. Another approach is to rule out offending programs by constraining the syntax using typing rules.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
