%%% 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{611}
\usepackage{amsthm, amsmath, amsfonts, amssymb}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{14}                  %% Lecture number
\title{Mutable variables}	   %% Title of lecture
\author{Michael O'Connor, Mia Minnes}       %% name of scribe
\date{28 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)
%          	(if x is more than a single character, use \Tr{x}{y})

\begin{document}

\maketitle

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


\section{References in C}
Last time we considered references and their semantics via translation and SOS.  Let's take a closer look at what happens in a language like \textcd{C}.

\subsection{Syntax}
\begin{eqnarray*}
\nm{e} & ::= & \cdots "UML" \cdots \bnf e_{1} =  e_{2} \bnf \ast e \bnf \& e \bnf e_{1} ; e_{2}\\
\end{eqnarray*}
where we think of the constructs respectively as:
\begin{itemize}
\item $e_{1} = e_{2}$ is assignment where $e_{1}$ is an assignable variable name, for instance, an array reference.
\item $\ast e$ is used to dereference a pointer $e$ (acts sort of like
$!$ in UML$_!$).
\item $\& e$ gives a pointer to the location of $e$.
\end{itemize}

\subsection{Semantics via Translation}
The target language is $"uML"$.  The translation we define will take into account the fact that \textcd{C} has two kinds of values
\begin{itemize}
\item L-values : values which can appear in the LHS of assignment; and
\item R-values: ordinary values (i.e., those that can appear in RHS of assignment).
\end{itemize}

\noindent To capture this in the target language, for each expression $e$ of \textcd{C}, we define $\Tr L e \rho \sigma$ (the left value of $e$) \emph{and} $\Tr R e \rho \sigma$ (the right value of $e$).  As before, we need to keep track of the state of the computation so the results of the translations are pairs.  To summarize:
\begin{itemize}
\item $\Tr L e \rho \sigma = \left( \mbox{location of } e, \sigma' \right)$ 
\item $\Tr R e \rho \sigma = \left( \mbox{value of } e, \sigma' \right)$.
\end{itemize}

\noindent The translation is defined inductively on the structure of \textcd{C} programs.  Note that (unlike FORTRAN), some left value translations are not defined.  For instance $\Tr L n \rho \sigma$ is not defined.  This prevents reassigning of an integer $5$ to have the value $3$.

\begin{eqnarray*}
\Tr R {n} \rho \sigma & = & (n, \sigma)\\
\Tr L {x} \rho \sigma & = & (\rho \mbox{``}x\mbox{''}, \sigma)\\
\Tr R {e} \rho \sigma & = & "let" \, ( \ell, \sigma' ) = \Tr L {e} \rho \sigma~"in"~ ("LOOKUP"~\sigma ' \ell, \sigma ') \qquad \qquad \text{if $\Tr L {e} \rho$ is defined}\\
\end{eqnarray*}

\noindent For assignment:
\begin{eqnarray*}
\Tr R {e_{1} = e_{2}} \rho \sigma &=& "let" (\ell, \sigma') = \Tr L {e_{1}} \rho \sigma \, "in" \\
						&  & "let"( v, \sigma'') = \Tr {R} {e_{2}} \rho \sigma' \, "in"~(v, "UPDATE"~ \sigma'' \ell v)
\end{eqnarray*}
Comments:
\begin{itemize}
\item Force left to right evaluation.
\item Allowed to ``assign down the chain.'' For example, $e_{1} = e_{2} = e_{3} = e_{4} = 5$ will assign 5 to each $e_i$ in our translation, just as the piece of code
\textcd{e1 = e2 = e3 = e4 = 5;} will in \textcd{C}.
\item An assignment statement is not an lvalue in \textcd{C}.  Hence $(x=1)=3$ is not allowed in our language, and \textcd{(x = 1) = 3;} is not in \textcd{C}.
\end{itemize}

\noindent Addressing:
\begin{eqnarray*}
\Tr L { \ast e } \rho \sigma &=& \Tr R {e} \rho \sigma \\
\Tr R { \& e } \rho \sigma &=& \Tr L {e} \rho \sigma \\
\end{eqnarray*}
So the two operations, $\ast, \&$, are inverses of each other.  The semantics defined above let us write, for example  $\ast \& \ast \& \ast \&  x = 2$.  This will have
the same effect as $x=2$. Another example (written in \textcd{C}):
\begin{verbatim}
int y = 0;
int x = &y;
*x = 1;
\end{verbatim}
%\begin{eqnarray*}
%&"int" \, &\textcd{y = 0;} \\
%&"int" \, &\textcd{x = \&y;} \\
%& &\textcd{\ast x = 1;}
%\end{eqnarray*}
This piece of code assigns $1$ to $y$.  Note that it would be illegal to include a line \textcd{ \&y = 0x1002;} because we can't change the address of a variable.\\

\noindent Functions (ignoring the fact that $\lambda$ is not actually part of \textcd{C}):
\begin{eqnarray*}
\Tr R { \fn x. e } \rho \sigma &=& \left( \fn y \sigma_{\mbox{\scriptsize dyn}} .\, "let" \, (\ell, \sigma') = "MALLOC" \, \sigma_{\mbox{\scriptsize dyn}} \, y~"in"~\Tr R {e} ( "EXTEND"~\rho~\mbox{``}x\mbox{''}~\ell) \sigma' , \sigma \right)  \\
\Tr R { e_{1}~e_{2} } \rho \sigma &=& "let" \, ( f, \sigma' ) \, = \Tr R {e_{1}} \rho \sigma \, "in"\\
		&&"let" \, (v, \sigma'') \, = \Tr R{e_{2}} \rho \sigma' \, "in" \, f v \sigma''
\end{eqnarray*}
Discussion:
\begin{itemize}
\item Choices we made: function applications don't affect state, in function application use left to right evaluation.
\item In defining function application, the allocation is required because variables must be bound to locations, whereas the argument of the function is a value ({\bf "c" is call by value}).
\item The value that a function returns is an rvalue rather than an lvalue.  Why?  Returning an lvalue whould mean we could assign new values to locations allocated during computation, e.g.\ \textcd{"fibo" 5 = 1;}.  We choose a semantics where we get back the value of a computation rather than its location.
\item Note that we do not need to explicitly define the store in the translation of function application because $f v \sigma''$ returns a pair of the location and the store.
\end{itemize}

\section{Pass (Call) by Reference}
It is conceivable to define a language where arguments to functions are lvalues.  In this case, the body of functions can update the argument and therefore change the value stored in the location passed in.  Examples of such a language are PASCAL, MODULA, Ada, Matlab (sort of), FORTRAN.

\noindent Consider a simplified version:
\begin{eqnarray*}
\nm{e} & ::= & \cdots \bnf \fn_{r} x.\, e \bnf "rcall"~e_{1}~e_{2}\\
\end{eqnarray*}
Note that we are explicitly saying when we pass by reference since we do not have a type system which would take care of distinguishing between the two cases.

\noindent Example program:
\begin{eqnarray*}
&"let" \, &x=1~"in"\\
&"let" \, &f= \fn_{r} y.\,y=y+1~"in" \\
& &"rcall" \; f x
\end{eqnarray*}
In this program $y$ is an alias for $x$, so the value at the location of $x$ is updated to $2$.\\

\noindent To incorporate call by reference into the semantics, we just need to change the rules for functions. 
\begin{eqnarray*}
\Tr R { \fn_{r} x. e } \rho \sigma &=& \left( \fn y \sigma_{\mbox{\scriptsize{dyn}}} . \Tr R {e} ( "EXTEND"~\rho~\mbox{``}x\mbox{''}~y) \sigma_{\mbox{\scriptsize{dyn}}}, \sigma \right)  \\
\Tr R {"rcall"~e_{1}~e_{2} } \rho \sigma &=& "let" \, ( f, \sigma' ) \, = \Tr R {e_{1}} \rho \sigma \, "in"\\
		&&"let" \, (\ell, \sigma'') \, = \Tr L{e_{2}} \rho \sigma' \, "in" \, f \ell \sigma''
\end{eqnarray*}
Since we are given a location, we don't need to allocate new memory.

\section{Objects}
\subsection{Definition}
Objects combine naming and the notion of state.  We can think of objects as naming environments.  For instance, we might have
\begin{eqnarray*}
&"class" &C \{ \\
	&&"int" ~ x; \\
	&&"int" ~ y; \\
	&&"int" ~ m \, ("int" \, z) \{ \cdots x \cdots y \cdots "this" \cdots z \};\\
&\}&
\end{eqnarray*}
where $x,y$ are fields (which are {\bf mutable}) and $m$ is  method ( {\bf immutable}).  Each of the fields and methods may be {\bf public} or {\bf private}, and the class itself might be mentioned using the keyword {\bf this}.  This is reminiscent of modules (recall: modules are non-hierarchical scope which introduces names that may or may not be accessible outside the scope).

\noindent Let's try to encode objects using module constructs:
\begin{eqnarray*}
\nm{e} & ::= & \cdots "UML" \cdots \bnf ("fields" \, x_{1} = e_{1}, \ldots, x_{n} = e_{n}; \;"methods" \, f_{1} = \fn y_{1}.e_{1}', \ldots, f_{m}=\fn y_{m}.e_{m}' ) \bnf e.x\\
\end{eqnarray*}
where the first new clause defines the object and the second clause selects components of the object.

\subsection{Translation}
We will translate objects to a naming environment.
\[\begin{array}{l}
\SB {("fields"~\overrightarrow{x= e}, \, "methods"~\overrightarrow{f =
\fn y.e'})} \rho \sigma = \\
    \quad "let" \;\overrightarrow{z = \SB{e} \rho \sigma} \; "in" \\
		 \qquad "Y" ( \lam{\rho'}{\lam{x'}{\\
		 \qquad \qquad "if"~x' = x_{1}~"then"~z_{1}~"else" \\
		 \qquad \qquad "if"~x' = x_{2}~"then"~z_{2}~"else" \ldots \\
		 \qquad \qquad "if"~x' = f_{1}~"then"~\fn z_{1}'. \SB{e_{1}'} (
		"EXTEND"~("EXTEND"~\rho~\mbox{``}y\mbox{''}~z_{1}')~\mbox{``this''}~\rho') \ldots}} )
\end{array}\]

Note that we used the "Y" combinator to allow method bodies to talk about the object itself, i.e.\ the naming environment defined by the object.

\subsection{Problem}

Objects as recursive records breaks when we try to implement
inheritance, because the name ``this'' is bound in the methods
of the super-object to the wrong fixed point.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

