%%% 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}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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{16 September, 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})

\begin{document}

\maketitle

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

\noindent This lecture concentrates on semantics by way of translation from a
 source language to a target language.  It includes two examples of 
 translation from Call By Name (CBN) semantics to Call By Value (CBV) 
 semantics and from a simplified version of untyped ML called uML into 
 CBV lambda calculus.  The notion of adequacy of translation is introduced.
 Big-step semantics for CBV lambda calculus are described.

\begin{itemize} % use enumerate for numbered list
\item Big-step semantics for CBV $\lambda$-calculus
\item Definition of translation
\item Translation of CBN into CBV
\item Adequacy of translation
\item Translation of uML into CBV
\end{itemize}


\section{Big-step semantics for CBV $\lambda$-calculus}

\begin{tabular}{ccc}
\infer{(\lam xe)~v \stepsto v'}{\subst evx \stepsto v'} &
\infer{e~e' \stepsto v''}
      {e \stepsto (\lam xe)~~~e'\stepsto v'~~~\subst e{v'}x \stepsto v''}
\end{tabular}


\section{Translation}

Translation from a well formed source language to a well understood 
target language is useful not only for reasoning about a program, 
but also for writing compilers.

\begin{itemize}
\item If the target language is a programming language then the translation
 is called a _definitional translation_.  Note that a definitional
 translation does not necessarily produce clean or efficient code.
\item If the target language is mathematical objects then the
translation is called _denotational semantics_.
\end{itemize}


\subsection{Notation}

\noindent $\SB{\cdot}$ is a function that translates an expression in 
the source language to something meaningful.  The things that have meaning
are in the target languages.  $\Tr C{e}$ or $\SB{e}_{CBV}$ notation can be 
used to name translation functions.


\subsection{CBN into CBV}
Recall CBN and CBV semantics with evaluation contexts:

\begin{itemize}
\item CBN
\end{itemize}

\[E ::= E~e \bnf [\cdot]\]
\[E[(\lam xe)~e'] \rightarrow E[\subst{e}{e'}{x}\}]\]

\begin{itemize}
\item CBV
\end{itemize}

\[E ::= [\cdot] \bnf E~e \bnf v~E\]
\[E[(\lam xe)~v] \rightarrow E[\subst evx]\]

\noindent Can we translate CBN into CBV?  In CBV an application $e~e'$ is not
evaluated unless $e'$ is a value.  To delay evaluation of $e'$, we can 
place it inside a value.  This trick effectively makes a thunk.

\begin{itemize}
\item CBV: $e~e'$
\item CBN: $e~(\lam{x}{e'})$
\end{itemize}

\noindent Using this trick, we can define a translation function from CBN into
CBV as follows:

\begin{eqnarray*}
\SB{e~e'} & = & \SB{e}(\lam z{\SB{e'}}) \\
\SB{(\lam x{e})} & = & \lam x{\SB{e}} \\
\SB{x} & = & x~ID \\
\end{eqnarray*}

\noindent Notice that the translation from CBN to CBV describes a CBN compiler.
This translation would have to be performed if CBN were to be implemented
in hardware.


\subsection{Short-circuiting $\IF$-statements in CBV}

\noindent Recall these definitions:
\begin{eqnarray*}
\TRUE & \definedas & \lambda x.\lambda y.x\\
\FALSE & \definedas & \lambda x.\lambda y.y\\
\IF & \definedas & \lambda b.\lambda x.\lambda y.bxy\\
\end{eqnarray*}

\noindent Using CBV semantics to evaluate the expression $\IF~b~e_1~e_2$
both $e_1$ and $e_2$ are evaluated before a choice is made.  As we have 
defined it so far, $\IF$ is _strict_ in $e_1$ and $e_2$; that is, if
either $e_1$ or $e_2$ fail to terminate, $\IF$ won't either.
Ordinarily, we would
expect $\IF$ to be strict only in $b$, and lazy in $e_1$ and $e_2$.

\noindent Example: $\FALSE ~ \Omega ~ ID$
\begin{itemize}
\item CBV semantics evaluate to $\Omega$
\item CBN semantics evaluate to ID
\end{itemize}

\noindent We can use CBN translation to achieve this effect.  First attempt:
\[\SB{\lambda x.\lambda y.x} = \lambda x.\SB{\lambda y.x} =
                               \lambda x.\lambda y.(x~ID)\]
\[\SB{\lambda x.\lambda y.x} = \lambda x.\SB{\lambda y.y} =
                               \lambda x.\lambda y.(y~ID)\]
\[\SB{\lambda b.\lambda x.\lambda y.byx} = \lambda b.\lambda x.\lambda y.\SB{bxy} = 
 \lambda b.\lambda x.\lambda y.\SB{bx}(\lambda z.\SB{y}) = 
 \lambda b.\lambda x.\lambda y.(b~ID)(\lambda z.\SB{x})(\lambda z.\SB{y}) \]
 
\noindent Notice that this translation is trying to be lazy in b, which is not
necessary.  Second attempt:
\[\SB{\IF~e_b~e_1~e_2}_{if} = \SB{e_b}_{if}(\lambda z.\SB{e_1}_{if})(\lambda z.\SB{e_2}_{if})\]


\subsection{Adequacy of translation}

\noindent What does it mean to say that a translation is correct?  Intuitively, we
think that if an expression $e$ in the source language steps in zero or more
steps to a values $v$ and the translation of $e$ steps in zero or more steps
to a value $v_t$ in the target language, then $v_t \approx \SB{v}$.
 
\begin{tabular}{ccc}
\infer{target}{source} &
\infer{\SB{e} \rightarrow^* v_t~\approx~\SB{v}}{e \rightarrow^* v}
\end{tabular}

\noindent There are two criteria for a translation to be considered _adequate_:
_soundness_ and _completeness_.

\begin{itemize}
\item Soundness: 
\[\SB{e} \rightarrow^* v_t~ \Rightarrow ~\exists v \bnf (e\rightarrow^*v)\wedge(\SB{v}\approx v_t))  \]
Every target evaluation represents a source evaluation.
\item Completeness: 
\[e \rightarrow^* v~ \Rightarrow ~\exists v_t \bnf (\SB{e}\rightarrow^*_t v_t)\wedge(v_t\approx\SB{v}))  \]
Every source evaluation represents a target evaluation.
\end{itemize}

\noindent There is one caveat: it is difficult to define equivalence on functions.  
One possible solution is to show that function agree on base types (integers, 
booleans, etc.).  The idea is that if the source and target language
disagree on some functions, we could use write another program that used
the disagreement on functions to force a disagreement on base types, by
applying those functions to values at which the function disagreed.

\subsection{uML} 

\noindent untyped ML syntax:
\begin{eqnarray*}
\ e::=n & \bnf& x \bnf \TRUE \bnf \FALSE \bnf \lambda x_1\ldots x_n.e
          \bnf  e_0~e_1~\ldots~e_n \bnf \impfnt{let}~x = e_1~\impfnt{in}~e_2 \\
        & \bnf& (e_1,e_2) \bnf \#1~e \bnf \#2~e \bnf \IF~e_0~\THEN~e_1~\ELSE~e_2 \\
\end{eqnarray*}

\noindent Let's define a translation function from uML to CBV $\lambda$-calculus

 \[ \SB{\cdot}:uML \rightarrow \lambda_{CBV} \]

\begin{eqnarray*}
\SB{n} & = & church~numeral~for~n \\
\SB{x} & = & x \\
\SB{\TRUE} & = & \lambda x.\lambda y.x \\
\SB{\FALSE} & = & \lambda x.\lambda y.y \\
\SB{\IF~b~\THEN~e_1~\ELSE~e_2} & = & 
   [\SB{b}(\lambda z.\SB{e_1})(\lambda z.\SB{e_2})]ID \\
\SB{\lambda x_1x_2\ldots x_n.e} & = & 
   \lambda x_1.\SB{\lambda x_2...\lambda x_n.e} \\
\SB{e_0~e_1\ldots e_n} & = & \SB{e_0\ldots e_{n-1}}\SB{e_n} \\
\SB{\impfnt{let}~x = e_1~\impfnt{in}~e_2} & = & 
   (\lambda x.\SB{e_2})\SB{e_1} \\
\SB{(e_1,e_2)} & = & CONS\SB{e_1}\SB{e_2} \\
\SB{\#1~e} & = & FIRST\SB{e} \\
\SB{\#2~e} & = & SECOND\SB{e} \\
\end{eqnarray*}

\noindent Unfortunately, this translation is not sound, because it
allows target-language evaluations that correspond to no source-
language evaluation. For example, the
the source code expression $\TRUE~\TRUE$ has no legal next step;
it gets _stuck_,  because there is no operational semantics rule in uML of the 
form $\TRUE~\TRUE \rightarrow_{uML} e$. However, the _translation_ of
$\TRUE~\TRUE$ is a valid lambda-calculus expression that evaluates to
another lambda-calculus expression. What happens in this evaluation
depends on the implementation of $\TRUE$ and cannot be understood from
the equational specification of booleans.

A possible solution to the soundness difficulty is to introduce 
many rules that transition stuck expressions to a special ``error''
value. For example, we might have a rule $\TRUE~e \rightarrow error$ to
cover the case just given. However,
we will still have to fix our translation to add run-time type checking,
in order to catch these errors, and we'll have to add a representation
of $\mathit{error}$.
Another solution is type checking and throwing a compile-time type error!

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