%%% 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{graphicx}
\usepackage{amsmath,amssymb,amsthm,amsfonts}
\usepackage[all]{xy}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

\newcommand{\LET}{\impfnt{let}~}
\newcommand{\IN}{~\impfnt{in}~}
\newcommand{\ENV}{~\impfnt{Env}~}
\newcommand{\FENV}{~\impfnt{FEnv}~}
\newcommand{\Z}{\mathbb{Z}}

\newcommand{\group}[1]{\left\langle{#1}\right\rangle}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{22}                  %% Lecture number
\title{Denotational Semantics of REC}   %% Title of lecture
\author{Bryant Adams and Olga Belomestnykh}       %% name of scribe
\date{19 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)
% <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{Continuous functions form a CPO}
\subsection{Motivation}
In a pointed CPO, we have a way to get our hands on least upper bounds. In our metalanguage, everything we work with is either a CPO or a continuous function. For the things that are already CPOs, taking fixed points is straightforward. What about for our operations, however? It would be nice to know that continuous functions themselves form a CPO. And, indeed, they do.

Essentially, we want to show that $\bigsqcup_{n} f_n$ is continuous (i.e. given chains $f_n, d_m$ that $(\bigsqcup_n f_n)(\bigsqcup_m d_m) =\bigsqcup_m ((\bigsqcup_n f_n) d_m)$.)
\subsection{Attempt}
It would be nice to try to argue as follows:
\begin{equation}\begin{split}
\textit{Start with} &(\bigsqcup_n f_n)(\bigsqcup_m d_m)\\
\textit{Since LUB is defined pointwise} &= (\bigsqcup_n (f_n \bigsqcup_m d_m))\\
\textit{By continuity of }f_n &= (\bigsqcup_n (\bigsqcup_m f_n(d_m)))\\
\textit{By wishful thinking} &= (\bigsqcup_m (\bigsqcup_n f_n(d_m)))\\
\textit{Since LUB is defined pointwise} &=\bigsqcup_m ((\bigsqcup_n f_n) d_m)\\
\end{split}\end{equation}

Alas, wishful thinking proves little. If we could just see that joins ($\bigsqcup$) commute, though... or, at least, that they commute when dealing with monotonic functions $f_n$ (which our continuous functions are)... then we'd be in good shape.

\subsection{Proof}
\begin{theorem}Given a chain of monotonic functions $f_n$ and a chain of arguments $d_m$, it is the case that $\bigsqcup_n \bigsqcup_m f_n(d_m)=\bigsqcup_m \bigsqcup_n f_n(d_m)$.
\end{theorem}
\begin{proof}
We will introduce a lemma which does all the real work of the theorem:
\begin{lemma} Given a bi-indexed infinite chain $e_{nm}$ such that $e_{nm}\sqleq e_{n'm'}$ iff ($n\leq n'$ and $m\leq m'$), it is the case that $\bigsqcup_n \bigsqcup_m e_{nm}=\bigsqcup_k e_{kk}=\bigsqcup_m \bigsqcup_n e_{nm}$\end{lemma}
The picture we are working within is an infinite 'square' (if we were in a finite case, we could have a rectangle, but if we were in a finite case, the least upper bounds would just be the maxima and there'd be nothing to worry about.)
\begin{center}\xymatrix{
&&\bigsqcup_n \bigsqcup_m e_{nm}&\bigsqcup_k e_{kk}\ar@{=}[l]\ar@{=}[r]&\bigsqcup_m \bigsqcup_n e_{nm}&&\\
&&\bigsqcup_m e_{m2}\ar@{.>}[ur]&&\bigsqcup_n e_{2n}\ar@{.>}[ul] && \\
&\bigsqcup_m e_{m1}\ar[ur]&&e_{22}\ar@{.>}[ul]\ar@{.>}[ur]\ar@{:>}[uu]&&\bigsqcup_n e_{1n}\ar[ul] & \\
\bigsqcup_m e_{m0}\ar[ur]&&e_{12}\ar@{.>}[ul]\ar[ur]&&e_{21}\ar[ul]\ar@{.>}[ur]&&\bigsqcup_n e_{0n}\ar[ul] \\
&e_{02}\ar@{.>}[ul]\ar[ur]&&e_{11}\ar[ul]\ar[ur]\ar@{=>}[uu]&&e_{20}\ar[ul]\ar@{.>}[ur]& \\
&&e_{01}\ar[ul]\ar[ur]&&e_{10}\ar[ul]\ar[ur]&& \\
&&&e_{00}\ar[ul]\ar[ur]\ar@{=>}[uu]&&&}\end{center}

\begin{proof} Consider some $k$. Certainly, $e_{kk}\sqleq \bigsqcup_n e_{nk}$ for each $k$, and so $\bigsqcup_m e_{mm} \sqleq \bigsqcup_m \bigsqcup_n e_{nm}$.

Now, note that for all $n$ and $m$, there exists $k$ such that $k\geq m$ and $k\geq n$ (in particular, $k=max(m,n)$) and thus for each $n,m$ there is $k$ such that $e_{nm} \sqleq e_{kk}$, so we also have $\bigsqcup_n e_{nm} \sqleq \bigsqcup_k e_{kk}$.

>From this, we see that $\bigsqcup_m \bigsqcup_n e_{nm}\sqleq \bigsqcup_m e_{mm}$, and thus conclude that $\bigsqcup_m e_{mm} = \bigsqcup_m \bigsqcup_n e_{nm}$

The case with reversed indices is identical, so the lemma is proved.
\end{proof}
To complete the proof of the theorem, we let $e_{nm}=f_n(d_m)$, and note that for $n\geq n', m\geq m'$ we have $e_{nm}\geq e_{n'm'}$ by $d_m$ and $f_n$ being chains, and the $f_n$ being monotonic, so we may apply the lemma.
\end{proof}



\section{Denotational Semantics for REC}
\begin{enumerate}
\item
REC Language

\begin{eqnarray*}
p &::=& \LET d \IN e \\
d &::=& f_1(x_1, \ldots, x_{a_1}) = e_1 \\
  &   & \vdots \\
  &   & f_n(x_1, \ldots, x_{a_n}) = e_n \\
e &::=& n \bnf x \bnf e_1 \oplus e_2 \bnf \LET x = e_1 \IN e_2 \bnf
        \IF_p~e_0~\THEN~e_1~\ELSE~e_2 \bnf f_i(e_1, \ldots, e_{a_i})
\end{eqnarray*}

\noindent It is reasonable to expect that under most semantics
$\LET~f_1(x_1) = f_1(x_1)~\IN~f_1(0)$  is likely to infinite loop, but $\LET~f_1(x_1) = f_1(x_1)~\IN~0$
to return $0$.$\\\\$
Example:
\begin{eqnarray*}
\LET & &\\
     & & f_1(n, m) = (n - m*m) \wedge ((n = m*(n~\impfnt{div}~m)) \vee f_1(n, m+1)) \\
     & & f_2(n) = f_1(n,2) \\
     & & f_3(n) = \IF_p~f_2(n)~\THEN~n~\ELSE~f_3(n+1) \\
\IN  & & \\
     & & f_3(1000)
\end{eqnarray*}

In this REC program $f_3(n)$ finds the first prime number $p$ such that $p \geq n$.

\item
CBV Denotational Semantics for REC

The meaning function is $\SB{e} \in \FENV \rightarrow \ENV \rightarrow \Z_{\bot}$.
First we must define two environments: one for variables and one for functions.
\begin{eqnarray*}
\rho &\in& \ENV = Var \rightarrow \Z \\
\phi &\in& \FENV = (\Z^{a_1} \rightarrow \Z_{\bot})~\times~\ldots~\times~(\Z^{a_n} \rightarrow \Z_{\bot})
\end{eqnarray*}

$Var$ is a countable set of variable names. $\Z$ is a set of possible bindings.
$\Z^n = \underbrace{\Z \times \Z \times \ldots \times \Z}_{n~times}$.

\begin{eqnarray*}
\SB{n} \phi \rho &=& \lift{n} \\
\SB{x} \phi \rho &=& \lift{\rho~x} \\
\SB{e_1~\oplus~e_2} \phi \rho &=& \LET v_1 \in \Z= \SB{e_1} \phi \rho \IN \\
                            & & \LET v_2 \in \Z= \SB{e_2} \phi \rho \IN \\
                            & & ~~~~~\lift{v_1~\oplus~v_2} \\
                            &=& (\SB{e_1} \phi \rho) \oplus_{\bot} (\SB{e_2} \phi \rho) \\
\SB{e_1 \wedge e_2} \phi \rho &=& \LET v_1 \in \Z = \SB{e_1} \phi \rho \IN \\
                              & & ~~~~~\IF~v_1 \leq 0~\THEN~0~\ELSE~\SB{e_2} \phi \rho \\
\SB{\LET x=e_1 \IN e_2} \phi \rho &=& \LET y \in \Z = \SB{e_1} \phi \rho \IN \\
                                  & & ~~~~~\SB{e_2}~\phi~\rho[x  \mapsto y] \\
\SB{\IF_p~e_0~\THEN~e_1~\ELSE~e_2} \phi \rho &=& \LET v_0 \in \Z = \SB{e_0} \phi \rho \IN \\
                                          & & ~~~~~\IF~v_0 \gt 0~\THEN \SB{e_1} \phi \rho~\ELSE~\SB{e_2} \phi \rho \\
\SB{f_i(e_1,\ldots,e_{a_i})} \phi \rho &=& \LET v_1 \in \Z = \SB{e_1} \phi \rho \IN \\
                                    & & \vdots \\
                                    & & \LET v_{a_i} \in \Z = \SB{e_{a_i}} \phi \rho \IN \\
                                    & & ~~~~~(\pi_i~\phi)\langle v_1,
				    \ldots , v_{a_i}\rangle
\end{eqnarray*}

\begin{itemize}
\item 
Where does $\phi$ come from?
\begin{eqnarray*}
\phi &=& {\langle F_1, \ldots , F_n\rangle} \\
     &=& \mathit{fix}~\lambda \phi \in \FENV .\langle \lambda y_1 \in \Z, \ldots , y_{a_1} \in \Z.\SB{e_1}~\phi~\{x_1 \mapsto y_1,\ldots, x_{a_1} \mapsto y_{a_1}\}, \\
     & &~~~~~~~~~~~~~~~~~~~~~~~~~\vdots \\
     & &~~~~~~~~~~~~~~~~~~~~~~~~~\lambda y_1 \in \Z, \ldots, y_{a_n} \in
     \Z.\SB{e_n}~\phi~\{x_1 \mapsto y_1, \ldots, x_{a_n} \mapsto
     y_{a_n}\}\rangle
\end{eqnarray*}

\item
Is $\FENV$ a pointed CPO?

$\FENV$ is a product.  A product is a pointed CPO when each $(\Z^{a_i}\rightarrow \Z_{\bot})$ is a pointed CPO.  A function is a pointed CPO when the codomain of that function is a pointed CPO and $\Z_{\bot}$ is a pointed CPO. Therefore, $\FENV$ is a pointed CPO.

\item
Is the function that we're applying $\mathit{fix}$ to continuous?

It is written using metalanguages, thus it is indeed continuous.
\end{itemize}

\item
CBN Denotational Semantics $\\$
The denotational semantics for CBN are the same as those for CBV with two exceptions:
\begin{eqnarray*}
\SB{\LET x=e_1 \IN e_2} \phi \rho &=& \SB{e_2}~\phi~\rho[x  \mapsto (\SB{e_1}~\phi~\rho)] \\
\SB{f_i(e_1,\ldots,e_{a_i})} \phi \rho &=& (\pi_i~\phi)\langle
~(\SB{e_1}~\phi~\rho),~\ldots,~\SB{e_{a_i}}~\phi~\rho)~\rangle   
\end{eqnarray*}
\end{enumerate}

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



