%%% 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, amssymb, amsfonts}

\theoremstyle{definition}
\newtheorem*{defn}{Definition}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{19}                  %% Lecture number
\title{Denotational Semantics of "IMP"}		   %% Title of lecture
\author{James Worthington, Mia Minnes}       %% name of scribe
\date{12 October, 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

\section{Denotational Semantics}
\subsection{Introduction}

So far we've been looking at translations where the target is a simpler
or better understood language.  These are called {\bf definitional
translations}.  A closely related approach semantics, {\bf denotational semantics},
is translations to mathematical objects.  The objects in question will
be functions with well-defined extensional meaning, i.e.\ in terms of
sets.  The main challenge will be getting a precise understanding of
what sets these function operate over.
For example, consider the function $\lambda x.x$, which is
the identity function, but over which domain?  Another even
more problematic example is the function $\lam{x}{(x~x)}$.
In this case, $x \in D$ ($x$ is an element of the domain)
{\em and} $x \in D \to E$ ($x$ is a function from the domain to the
set of expressions, since we apply $x$ to an element of the domain).
How can both of these statements about $x$ be true?  Maybe $D = D \to
E$?  This is a fairly rigid requirement, so maybe it suffices to allow
isomorphic sets: $D \cong D \to E$.  However, a set cannot be isomorphic
to the set of functions from itself to $E$, unless $|E| = 1$.  This
follows by a diagonalization argument (if the sets are countable, and
the proof generalizes for uncountable sets as well).  To carry out the
proof, we list \begin{alignat*}4 & \qquad & d_{1}& \; d_{2} & d_{3}& \\
&f_{1} \qquad & e_{11}& \; e_{12} &\; e_{13}& \; \ldots\\ &f_{2} \qquad
& e_{21}& \; e_{22} &\; e_{23} &\; \ldots\\ &\vdots \qquad & \vdots&&
\end{alignat*} where $f_{i}( d_{j} ) = e_{ij}$.  Then a bijection would
associate exactly one $d_{i}$ with each function in the list.  But,
consider the function \[ \{ d_{1} \mapsto \neg e_{11} , d_{2} \mapsto
\neg e_{22} , \ldots \}. \] This function is in $D \to E$ but is not in
the list, thereby contradicting the existence of a bijection.
Similarly, functions like the $Y$ combinator are problematic because
they require an isomorphism between two sets of apparently different
cardinality.

\noindent This means that we need to take more care in which functions
we write down.  The solution to the cardinality conundrum
is that the set of _computable_ functions is smaller than the
set of all functions---almost all functions are not computable.
 
\subsection{Denotational Semantics for IMP}

We will use the notation $\lambda x \in D. e$ when writing down
denotational semantics.  The addition of the domain will make sure we
are precise in identifying the extension of functions. \\

\noindent {\it Aside: this is not a type declaration.  Later, we will
introduce types and write them as $\lambda x : \tau. e$ . The reason for
differentiating these is that types are pieces of language syntax
whereas sets are mathematical objects which are referred to with
mathematical notation.}

\noindent Recall:
\begin{eqnarray*}
\nm{a} & ::= & n \bnf x \bnf ("a_0~\oplus~a_1") \\
\nm{b} & ::= & \TRUE \bnf \FALSE \bnf ("\neg~b") \bnf ("b_0~\wedge~b_1") \bnf ("a_0~=~a_1") \bnf \cdots \\
\nm{c} & ::= & \SKIP \bnf x:=a \bnf ("c_0~;~c_1") \bnf
        (\IF~b~\THEN~c_1~\ELSE~c_2) \bnf (\WHILE~b~\DO~c~) \\
\end{eqnarray*}

\noindent To define the denotational semantics, we will make reference to states, which are functions $\Sigma = Var \to \mathbb{Z}$, in order to define the following functions.

\begin{eqnarray*}
\Tr{A}{"a"} &\in& \Sigma \to \mathbb{Z}\\
\Tr{B}{b} &\in& \Sigma \to \mathbb{T} \qquad \text{where $\mathbb{T} = \{ true, false \}$} \\
\Tr{C}{"c"} &\in& \Sigma \to ?\\
\end{eqnarray*}
Intuitively, we'd like the meaning of commands to be functions from states to states: given an initial state, produce the final state reached by applying the command.  But, this becomes problematic if there is no such final state, i.e.\ if the program resulting from issuing the command doesn't terminate (e.g.\ "while~\TRUE~DO~\SKIP").

\begin{defn}
For any set  $S$, let $S_{\bot} = S \cup \{ \bot \}$ where $\bot$ is a bottom element (more on this later).
\end{defn}

\noindent So, define $\Tr{C}{c} \in \Sigma \to \Sigma_{\bot}$, where we
will use $\bot$ to indicate non-termination.

\noindent As usual, we define denotational semantics by structural induction.  Note that this induction is a little more complicated since we are defining all three functions at once.  However, it is still well-founded because we only use the function value on sub-expressions in the definitions.

\begin{eqnarray*}
\Tr{A}{n} &=& \lambda \sigma \in \Sigma . n = \{ (\sigma, n) : \sigma \in \Sigma \} \\
\end{eqnarray*}
For the remaining definitions, we use the shorthand of defining the value of the function given some $\sigma \in \Sigma$.

\begin{eqnarray*}
\Tr{A}{x}\sigma &=& \sigma(x) \\
\Tr{A}{a_{1}~\oplus~a_{2}} \sigma &=& \Tr{A}{a_1} \sigma \oplus \Tr{A}{a_2} \sigma \\
\Tr{B}{\TRUE} \sigma &=& true \\
\Tr{B}{\FALSE} \sigma &=& false \\
\Tr{B}{\neg b} \sigma &=&
    \begin{cases}
	\mathit{true} \qquad \text{if $\Tr{B}{b} \sigma = false$} \\
	\mathit{false} \qquad \text{if $\Tr{B}{b} \sigma = true$} \\ 
    \end{cases}
\end{eqnarray*}
Note that we can express the translation of negation more compactly with
a mathematical {\it if then else} expression:
\[
\mathif{\Tr{B}{b} \sigma}{\mathit{false}}{\mathit{true}}
\]
Alternatively, we can write down the function extensionally,
\[
\{ (\sigma, true) : \sigma \in \Sigma \wedge \neg \Tr{B}{b} \sigma \}
\cup \{ (\sigma, false) : \sigma \in \Sigma \wedge \Tr{B}{b} \sigma \}.
\]
For the commands:
\begin{eqnarray*}
\Tr{C}{\SKIP}\sigma &=& \lift \sigma \\
\Tr{C}{x:=a} \sigma &=& \lift{ \sigma[x \mapsto \Tr{A}{a}\sigma] }\\
\Tr{C}{"\IF~b~\THEN~c_{1}~\ELSE~c_{2}"} \sigma &=& if \; \Tr{B}{b} \sigma \; then \; \Tr{C}{"c_{1}"} \sigma  \; else \; \Tr{C}{"c_{2}"} \sigma \\
\end{eqnarray*}
where we use the {\bf lift} function, $\lift{\;} : S \to S_{\bot}$ (for any set $S$), to distinguish between elements of these sets. \\

\noindent The denotational semantics of sequences of commands is a little more challenging since we need to {\bf reverse lift}:
\[
\Tr{C}{"c_{1}~;~c_{2}"} \sigma = \left ( if \; \Tr{C}{"c_{1}"} \sigma =
\bot \; then \; \bot \; else \;\Tr{C}{"c_{2}"}\left( \Tr{C}{"c_{1}"} \sigma \right) \right) .\\
\]
This isn't quite correct since $\Tr{C}{"c_{1}"} \in \Sigma_{\bot}$ but the domain of $\Tr{C}{"c_{2}"}$ is $\Sigma$.  To fix this, we introduce a piece of metalanguage:
\[
"let" \; \sigma' = \Tr{C}{"c_{1}"} \sigma \; "in" \; \Tr{C}{c_{2}} \sigma'
\]
where we define the semantics of the "let" statement such that if it is
applies to $\bot$ it returns $\bot$ and otherwise it uses the reverse
lifted object.  In the example above, this does what we want: if
$\Tr{C}{"c_{1}"} = \bot$ then the whole thing returns $\bot$, and otherwise $\sigma' $ gets the element of $\Sigma$ which maps to \Tr{C}{"c_{1}"} under the lifting map.

\noindent Another way of achieving this effect is by defining a lift
operator on functions, $( \; )^{*}: (D \to E_{\bot}) \to (D_{\bot} \to
E_{\bot})$ such that
\[
( \; )^{*} = \fn f \in (D \to E_{\bot}). \left( \fn x.~if~x = \bot \;
then \bot \; else \; f(x) \right).
\]\\

We have one command left: $\WHILE b \DO c$"do" $c$.  This is equivalent to "if" $b$ "then" $c$; "while" $b$ "do" $c$ "else skip", so a first
guess at a denotation might be:\\
\[
\Tr{C}{\mbox{"while" $b$ "do" $c$}} = "if"\text{ } \Tr{B}{b}\sigma \text{ }"then"\text{ } "let" \text{ }\sigma ' = 
\Tr{C}{c}\sigma \text{ }"in"\text{ } \Tr{C}{\mbox{"while" $b$ "do" $c$}}\sigma' \text{ } "else" \text{ }\sigma
\]

However, there is a problem here: this is not well-founded.  To fix this, we need to think differently about the meaning of a "while" statement.
Suppose: 

\[
\mathcal{W} = \Tr{C}{\mbox{"while" $b$ "do" $c$}}
\]
Then
\[
\mathcal{W} = \lambda \sigma \in \Sigma. "if"\text{ }\Tr{B}{b}\sigma \text{ }"then" \text{ }
"let" \text{ } \sigma' = \Tr{C}{c}\sigma \text{ } "in"\text{ } \mathcal{W} \sigma' \text{ }"else"\text{ }\lift{\sigma}
\]

But note that this is an equation, and not a definition.  If we define $\mathcal{F}$ as:\\
\[
\mathcal{F} = \lambda w \in \Sigma \rightarrow \Sigma_\bot. \lambda \sigma \in \Sigma \text{ }"if" \text{ }
\Tr{B}{b}\sigma \text{ } "then"\text{ }"let"\text{ }\sigma' = \Tr{C}{c} \sigma "in"\text{ }w \sigma' \text{ }"else"\lift{\sigma}
\]

Then $\mathcal{W} = \mathcal{FW}$, i.e., we are looking for a fixed point of $\mathcal{F}$.  But how do we take fixed points
without using the dreaded Y combinator?  
Eventually we will have $\mathcal{W}$ = fix $\mathcal{F}$, where
$\mathcal{F} \in (\Sigma \rightarrow \Sigma_\bot) \rightarrow
(\Sigma \rightarrow \Sigma_\bot)$.  The solution will be to think of a "while" statement as the limit of a sequence of functions.
Intuitively, by running through the loop more and more times, we will get better and better approximations.  Consider

\[
\mbox{"while" $b$ "do" $c$} \cong_0 \mbox{"if" $b$ "then" $c$}; \bot \mbox{"else" "skip"}
\]

where $\bot$ means diverge.  This simulates 0 iterations of the loop.  We could then simulate 1 iteration of the loop by:
\[
\mbox{"if" $b$ "then" $c$};(\mbox{"if" $b$ "then" $c$}; \bot \mbox{"else" "skip"}) \mbox{"else" "skip"}
\]

It should now be clear how to simulate $n$ iterations of the "while" loop. We now define a sequence of functions:

$d_0 = \lambda \sigma \in \Sigma.\bot$

$\mathcal{F}(d_0) =$ the denotation of "if" $b$ "then" $c$; $\bot$ "else" "skip"

Then $\mathcal{F}(\mathcal{F}(d_0))$ corresponds to unrolling the loop once.  Inductively, we define $d_n = \mathcal{F}^n(d_0)$,
which describes the loop up to $n-1$ iterations.  So, then denotation of
the "while" statement should be:

\[
\mathit{fix} \mathcal{F} = \mbox{`limit'}_{n \rightarrow \infty} \mathcal{F}^n(d_0)
\]

If we want to take the limit of a sequence of functions, we need some
structure in the space of functions.  We will define an ordering on
these functions, $d_0 \sqsubseteq d_1 \sqsubseteq d_2 \sqsubseteq
\cdots$ then find the least upper bound of this sequence and define
"fix" F = l.u.b. $\{ d_n \}$. We first introduce some general notions.\\


\noindent Recall that a {\it partial order} is a pair consisting of a
set $S$ (called the carrier set) and a relation $\sqsubseteq$ on $S$
such that:

$\sqsubseteq$ is reflexive (for all $d \in S, d \sqsubseteq d$)

$\sqsubseteq$ is transitive (for all $d,e,f \in S$: if $d \sqsubseteq e$
and $e \sqsubseteq f$, then $d \sqsubseteq f$)

$\sqsubseteq$ is antisymmetric (for all $d,e \in S: d \sqsubseteq e
\land e \sqsubseteq d \Rightarrow d = e$)\\

Examples include $(\mathbb{Z}, \leq)$, $(\mathbb{Z},=)$ (known as a
discrete partial order because the relation is equality), $(\mathbb{Z},
\geq)$, $(\{\mbox{"true, false"}\}, \rightarrow)$, if $(S, \sqsubseteq)$
is a partial order then so is $(S, \sqsupseteq)$, and $(2^{S},
\subseteq)$.

We can represent a partial order visually by drawing a {\it Hasse
diagram}.  Draw each element as a point, with the point representing
$d_2$ drawn above the point representing $d_1$ iff $d_1 \sqsubseteq
d_2$.  Finally, draw a line connecting any two elements if the relation
between them is not implied by reflexivity or transitivity.
 
Now, given any partial order $(S, \sqsubseteq)$, we can define a new
partial order, $(S_\bot, \sqsubseteq_\bot)$ such that $\lift{d_1}
\sqsubseteq_\bot \lift{d_2}$ if $d_1 \sqsubseteq d_2$, and $\bot
\sqsubseteq_\perp d$ for all $d \in S_\perp$.

So, if $S$ is the space of functions we were working with earlier,
$S_\perp$ is that space with a least element $\perp$ added.
Intuitively, non-termination is less than (contains less information
than) any function.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
