%%% 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{12}                            %% Lecture number
\title{State \& References}   %% Title of lecture
\author{Kevin Markman and Jon Guarino} %% name of scribe
\date{23 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!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Today's lecture introduced the idea of mutable state and the ``side effects''
caused by the evaluation of commands.  The following topics are covered:

\begin{itemize}
\item Functional vs. Imperative Programming
\item Extending uML to uML!
\item Translation of uML to uML!
\end{itemize}

\section{Programming Paradigms}
\subsection{Functional}
In a fully functional language, expressions are ``pure,'' in the sense
that resemble mathematical formulas.  This allows the programmer to
reason equationally, avoiding many of the pitfalls associated with
a constantly changing execution environment.  For example, in a functional
language, it is always the case that:

\[x = e \leftrightarrow f(x) = f(e)\]

\begin{flushleft}
Concurrency is easier to implement with a functional language because
of confluence (aka the diamond property).
\end{flushleft}

\subsection{Imperative}
Imperative programming more closely resembles the way we perceive the real
world in that there exists an underlying notion of identity.  Mutable variables
represent finite state which can be updated, and which can exist outside of the
simple substitution scoping rules of their functional counterparts.

\begin{flushleft}
Consider the following example:
\end{flushleft}

let $x = \textrm{ref}~1$ in

~~~let $y = x$ in

~~~~~~let $z = (x := 2)$ in

~~~~~~~~~$!y$

\begin{itemize}
\item
first $x$ points to a newly allocated location holding the value 1
\item
then $y$ is assigned $x$, the pointer to the location holding 1
\item
the value pointed to by $x$ is updated to be 2
\item
the value $y$ points to when it is dereferenced is now 2
\end{itemize}

\section{The uML! Language}
\subsection{Syntax}
The syntax for uML! is as follows.  $\ell$ represents a memory location, and
only appears in running programs as the result of evaluations.

\begin{eqnarray*}
\nm{uML!} & ::= & "uML" \bnf "ref~e" \bnf "!~e" \bnf "e_1~:=~e_2"
 \bnf "e_1~;~e_2" \bnf "\ell" \\
\end{eqnarray*}

\subsection{The Store}

We define a store as a function $\sigma:location\rightarrow value$.  We
use the following functions to manipulate stores:

\begin{flushleft}
\begin{tabular}{lcl}
LOOKUP $\sigma~\ell$ & = & $\sigma(\ell)$ \\
UPDATE $\sigma~\ell~v$ & = & $\sigma[\ell\mapsto v]$ \\
MALLOC $\sigma~v$ & = & $(\ell,~\sigma')$~   where~$\sigma'~=~\sigma[\ell\mapsto v]$
 ($\ell$ is a new location) \\
EMPTY STORE & = & $\sigma$    where dom($\sigma$) = $\emptyset$ \\
\end{tabular}
\end{flushleft}

\subsection{Small-step Semantics}

A program in uML! is evaluated as a configuration (e: uML!, $\sigma$: store).
The small-step rules are as follows:

\begin{eqnarray*}
\nm{E} & ::= & "E_{uML}" \bnf "ref~[\cdot]" \bnf "!~[\cdot]" \bnf "[\cdot]~:=~e"
 \bnf "v~:=~[\cdot]" \bnf "[\cdot];e" \\
\end{eqnarray*}

\begin{tabular}{llll}
\infer{E[e],\sigma \rightarrow E[e'],\sigma'}{e,\sigma \rightarrow e',\sigma'}
 & (Context Rule) &
\infer{"ref"~v,\sigma \rightarrow \ell,\sigma[\ell\mapsto v]}{}
 & (where $\ell \notin$ dom($\sigma$)) \\ \\
\infer{!\ell,\sigma \rightarrow \sigma(\ell),\sigma}{} 
 & ($\forall\ell|\ell\in\sigma$)\footnotemark &
\infer{\ell := v,\sigma \rightarrow null,\sigma[\ell\mapsto v]}{} \\ \\
\infer{v;e,\sigma \rightarrow e,\sigma}{}
\end{tabular}

\footnotetext{We can prove by induction that it is impossible to create
 dangling pointers in uML!.}
 
\section{Translating uML! to uML}

$\SB{e}\rho\sigma$ is an expression that computes (e',$\sigma'$) in an
 environment $\rho$ and state $\sigma$, where e' is the resulting
 expression and $\sigma'$ is the store.

\begin{eqnarray*}
"\SB{n}\rho\sigma" & "=" & "(n,\sigma)" \\
"\SB{x}\rho\sigma" & "=" & "(\rho~\mbox{``$x$''},\sigma)" \\
"\SB{if~e_b~then~e_1~else~e_2}\rho\sigma" & "=" &
 "let~(b,\sigma')\footnotemark = \SB{e_b}\rho\sigma~in" \\
 & & "if~b~then~\SB{e_1}\rho\sigma'~else~\SB{e_2}\rho\sigma'" \\
"\SB{ref~e}\rho\sigma" & "=" & "let~(x,\sigma') =
 \SB{e}\rho\sigma~in~MALLOC~\sigma'~x" \\
"\SB{~!e}\rho\sigma" & "=" & "let~(x,\sigma') =
 \SB{e}\rho\sigma~in~(LOOKUP~\sigma'~x,\sigma')" \\
"\SB{e_1~:=~e_2}\rho\sigma" & "=" & "let~(x_1,\sigma_1) =
 \SB{e_1}\rho\sigma~in" \\
 & & "let~(x_2,\sigma_2) = \SB{e_2}\rho\sigma_1~in" \\
 & & "(null,~UPDATE~\sigma_2~x_1~x_2)" \\
"\SB{e_1;e_2}\rho\sigma" & "=" & "let~(x,\sigma_1) =
 \SB{e_1}\rho\sigma~in~\SB{e_2}\rho\sigma_1" \\
"\SB{\lam{x}{e}}\rho_{lex}\sigma_{lex}" & "=" &
 "\lam{y\sigma_{dyn}}{\SB{e}~(EXTEND~\rho_{lex}~\mbox{``$x$''}~y)~\sigma_{dyn}}" \\
"\SB{e_1~e_2}\rho_{dyn}\sigma_{dyn}" & "=" & "let~(f,\sigma_1) =
 \SB{e_1}\rho_{dyn}\sigma_{dyn}~in~" \\
 & & "let~(v,\sigma_2) = \SB{e_2}\rho_{dyn}\sigma_1~in~" \\
 & & "f~v~\sigma_2" \\
\end{eqnarray*}


\footnotetext{
~`let (b,$\sigma'$) = $\SB{e_b}\rho\sigma$ in' here is syntactic sugar for
`let p = $\SB{e_b}\rho\sigma$ in let b = \#1 p in let $\sigma'$ = \#2 p in'.
}

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