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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{21}                  %% Lecture number
\title{Denotational Semantics of REC}   %% Title of lecture
%\author{Michael O'Connor}       %% name of scribe
\date{18 October 2006}     %% 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})

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

\newcommand{\ENV}{\impfnt{Env}}
\newcommand{\FENV}{\impfnt{FEnv}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\group}[1]{\left\langle{#1}\right\rangle}

\theoremstyle{definition}
\newtheorem*{defn}{Definition}
\newcommand{\nondet}{\left[\!\kern1pt\right]}
\renewcommand\phi\varphi
\renewcommand\wp[2]{\mathsf{wp}~{#1}~{#2}}
\newcommand\wlp[2]{\mathsf{wlp}~{#1}~{#2}}
\renewcommand\({\begin{eqnarray*}}
\renewcommand\){\end{eqnarray*}}

\newcommand\LOOKUP[2]{\mathrm{LOOKUP}~{#1}~{#2}}
\newcommand\UPDATE[3]{\mathrm{UPDATE}~{#1}~{#2}~{#3}}
\newcommand\MALLOC[2]{\mathrm{MALLOC}~{#1}~{#2}}
\newcommand\EMPTY{\mathrm{EMPTY\mbox{-}STORE}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}
\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\bigcdot{\mathrel{\raisebox{1pt}{$\scriptscriptstyle\bullet$}}}
\newcommand\holed[1]{[\,#1\,]}
\newcommand\hole{\holed\bigcdot}
\newcommand\context[1]{E\kern1pt\holed{#1}}
\newcommand\contextHole{\context\bigcdot}
\newcommand\goesto[2]{\underset{#2}{\overset{#1}\longrightarrow}}
\newcommand\ifthenelse[3]{\mathsf{if\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\ifpthenelse[3]{\mathsf{ifp\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\whiledo[2]{\mathsf{while\ }#1\mathsf{\ do\ }#2}
\newcommand\letin[3]{\mathsf{let\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\letrec[5]{\mathsf{letrec\ }#1 = #2\mathsf{\ and\ \ldots\ and\ }#3 = #4\mathsf{\ in\ }#5}
\newcommand\letrecone[3]{\mathsf{letrec\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathsf{true}}}
\newcommand\false{\ensuremath{\mathsf{false}}}
\newcommand\error{\ensuremath{\mathsf{error}}}
\newcommand\pca[3]{\{#1\}\kern1pt{#2}\kern1pt\{#3\}}
\newcommand\states{\Set{St}}
\newcommand\rtc{^{\textstyle *}}
\newcommand\sat\vDash
\newcommand\force\vdash

\newcommand\hyphen{\mbox{-}}
\newcommand\lookup[2]{\nm{LOOKUP}~#1~\mquote{#2}}
\newcommand\update[3]{\nm{UPDATE}~#1~\mquote{#2}~#3}
\newcommand\SBk[1]{\SB{#1}k}
\newcommand\fix[1]{\mathsf{fix}\,{#1}}

\newlength\reasonwidth
\setlength\reasonwidth{3cm}
\newcommand\reasoning[1]{\def\longest{#1}\settowidth{\reasonwidth}{$\displaystyle\longest$}\addtolength{\reasonwidth}{5mm}} %dck 2/12/98
\newcommand\reason[2]{\makebox[\reasonwidth][l]{$\displaystyle{#1}$}\mbox{#2}}

\renewcommand\inj[1]{\mathsf{in}_{#1}}
\newcommand\proj[1]{\pi_{#1}}
\newcommand{\dlt}{\sqsubseteq}
\newcommand\floor[1]{\lfloor{#1}\rfloor}
\newcommand\cf[1]{[\kern1pt{#1}\kern1pt]}
\newcommand\SBpr[1]{\SB{#1}\,\phi\,\rho}%

\begin{document}
\maketitle

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

\section{A Metalanguage for Domain Constructions}

Last time we did several constructions that required us to check that various domains were CPOs and that various associated operations were continuous.  How can we avoid doing this kind of check over and over again?  One solution is to create an abstract metalanguage consisting of some basic operations that will allow us to do domain constructions (like function spaces, direct products, etc.) and that will ensure that the domains that are constructed are CPOs and the associated functions are continuous.  We can compose these constructions to create more complicated domains from simpler ones and always be assured that the desired mathematical properties hold.

The simplest objects will be the discrete CPOs $\mathbb{Z}$, $\mathbb{N}$ and $\mathbb{U}$ for the integers, the natural numbers, and the unit domain, respectively.  The unit domain contains a single element "unit".  These all have the _discrete order_, meaning that if $x\dlt y$, then $x=y$.

For any domain $A$, we can construct a new domain $A_\bot$, which is $A$ adjoined with a new element $\bot$ below all the previous elements.  Note that $\bot$ is intended to be a new element, so we can actually iterate this operation.  The associated operations are the natural embedding $D\to D_\bot$ and the lifting operation $(\cdot)^*:(D\to E_\bot)\to(D_\bot\to E_\bot)$ defined by
\(
(d)^* (x) &\definedas&
\begin{cases}
d(x), & \mbox{if $x\neq\bot$,}\\
\bot, & \mbox{if $x=\bot$.}
\end{cases}
\)
Both these operations are continuous.

Given CPOs $D$ and $E$, we can form the product $D\times E$ consisting
of all ordered pairs $<d,e>$ with $d\in D$ and $e\in E$, ordered componentwise.
This is the set-theoretic Cartesian product of $D$ and $E$ with
$<d,e> \dlt <d',e'>$ iff $d\dlt d'$ and
$e\dlt e'$.  This is a CPO, and it is easy to check
that $\bigsqcup_{d\in X,\ e\in Y} <d,e> = <\bigsqcup X,\bigsqcup Y>$.
Along with the product constructor come the projections $\pi_1$ and $\pi_2$
defined by $\pi_1(<d,e>) = d$ and $\pi_2(<d,e>) = e$, which are continuous.
If $f:C\to D$ and $g:C\to E$, then the function $<f,g>:C\to D\times E$
defined by ${<f,g>} \definedas \lam x{<f(x),g(x)>}$
is continuous if $f$ and $g$ are.  This is the unique function satisfying the equations $f=\pi_1\circ<f,g>$ and $g=\pi_2\circ<f,g>$.  The binary product can be generalized to an arbitrary product $\prod_{x\in X} D_x$ with associated projections $\pi_y:\prod_{x\in X} D_x\to D_y$.

Given CPOs $D$ and $E$, we can form the _sum_ (or _coproduct_) $D+E$, corresponding to the
disjoint union of $D$ and $E$.  We would like to take the union of the sets $D$ and $E$, but
we need to mark the elements to make sure we can tell which set they originally came from in case
$D$ and $E$ have a nonempty intersection.  To do this, we define
\(
D+E &\definedas& \{\inj 1(d)\mid d\in D\}\cup\{\inj 2(e)\mid e\in E\},
\)
where $\inj 1$ and $\inj 2$ are any
one-to-one functions with disjoint ranges; for example,
we could take $\inj 1(x)=<1,x>$ and $\inj 2(x)=<2,x>$.
We define $\inj i(x)\dlt \inj j(y)$ iff $i=j$ and $x\dlt y$.  Any chain in $D+E$
must be completely contained in $\{\inj 1(x)\mid x\in D\}$ or $\{\inj 2(x)\mid x\in E\}$, so $D+E$ is a CPO.  The associated operations are the injections $\inj 1:D\to D+E$ and $\inj 2:E\to D+E$,
which are continuous.  If $f:D\to C$ and $g:E\to C$, then we can combine $f$ and $g$
into a function $f+g:D+E\to C$ using a "case" construct:
\(
f+g &\definedas& \lam x{"case"\ x\ "of"\ \inj 1(y) \to f(y) \mid \inj 2(y) \to g(y)}.
\)
This is continuous if $f$ and $g$ are, and it is the unique function satisfying the equations $f=(f+g)\circ\inj 1$ and $g=(f+g)\circ\inj 2$.  As with products and projections, the binary coproduct can be generalized to an arbitrary coproduct $\sum_{x\in X} D_x$ with associated injections $\inj y:D_y\to\sum_{x\in X} D_x$.

Finally, given CPOs $D$ and $E$, we can define the CPO $\cf{D\to E}$ of all continuous functions
from $D$ to $E$ with the pointwise ordering.  The corresponding operations are:
\begin{enumerate}
\item $"apply":\cf{D\to E}\times D\to E$ that applies a given function to a given argument;
\item $"compose":\cf{E\to F}\times \cf{D\to E}\to\cf{D\to F}$;
\item $"curry":\cf{D\times E\to F}\to\cf{D\to\cf{E\to F}}$;
\item $"uncurry":\cf{D\to\cf{E\to F}}\to\cf{D\times E\to F}$; and most importantly,
\item $"fix":\cf{D\to D}\to D$, defined by $\lam{g\in\cf{D\to D}}\bigsqcup g^n(\bot)$, that takes a function and returns its least fixpoint.  To apply "fix", $D$ must have a bottom element $\bot$.
\end{enumerate}
All these functions are continuous.

\section{Denotational Semantics for REC}

\subsection{REC Syntax}

\(
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 \letin x{e_1}{e_2} \bnf
        \ifpthenelse{e_0}{e_1}{e_2} \bnf f_i(e_1,\ldots, e_{a_i})
\)
The functions in $d$ are mutually recursive.  It is reasonable to expect that under most semantics, $\letin{f_1(x_1)}{f_1(x_1)}{f_1(0)}$ will loop infinitely, but $\letin{f_1(x_1)}{f_1(x_1)}{0}$ will halt and return $0$.

For example,
\(
"let" & &\\
     & & f_1(n,m) = \ifpthenelse{m^2-n}{1}{(n-m(n\ "div"\ m))\cdot f_1(n,m+1)}\\
     & & f_2(n) = \ifpthenelse{f_1(n,2)}{n}{f_2(n+1)}\\
"in"  & & \\
     & & f_2(1000)
\)

In this REC program, $f_2(n)$ finds the first prime number $p\geq n$.  The value of $n-m(n\ "div"\ m$ is positive iff $m$ does not divide $n$.

\subsection{CBV Denotational Semantics for REC}

The meaning function is $\SB{e} \in \FENV \rightarrow \ENV \rightarrow \Z_{\bot}$, where $\FENV$ and $\ENV$ denote the sets of variable environments and function environments, respectively, as used in REC.
\(
\rho &\in& \ENV\ \ =\ \ \Set{Var} \rightarrow \Z \\
\phi &\in& \FENV\ \ =\ \ (\Z^{a_1} \rightarrow \Z_{\bot})~\times~\cdots~\times~(\Z^{a_n} \rightarrow \Z_{\bot})
\)

Here \Set{Var} is a countable set of variables, $\Z$ is the set of integers, which are the values that can be bound to a variable in an environment, and $\Z^m = \underbrace{\Z \times \Z \times \cdots \times \Z}_{\mbox{$m$ times}}$.

\(
\SBpr{n} &\definedas& n\\[1ex]
\SBpr{x} &\definedas& \rho(x)\\[1ex]
\SBpr{e_1~\oplus~e_2} &\definedas& \letin{v_1\in\Z}{\SBpr{e_1}}{}\\
   & & ~~~\letin{v_2 \in \Z}{\SBpr{e_2}}{}\\
   & & ~~~~~~v_1~\oplus~v_2\\
   &=& \SBpr{e_1}\ \ \oplus_{\bot}\ \ \SBpr{e_2}\\[1ex]
\SBpr{\letin x{e_1}{e_2}} &\definedas& \letin{y \in \Z}{\SBpr{e_1}}{}\\
   & & ~~~~~\SB{e_2}\,\phi\,\rho[y/x]\\[1ex]
\SBpr{\ifpthenelse{e_0}{e_1}{e_2}} &\definedas& \letin{v_0 \in \Z}{\SBpr{e_0}}{}\\
   & & ~~~~~\ifthenelse{v_0 \gt 0}{\SBpr{e_1}}{\SBpr{e_2}}\\[1ex]
\SBpr{f_i(e_1,\ldots,e_{a_i})} &\definedas& \letin{v_1 \in \Z}{\SBpr{e_1}}{}\\
   & & \vdots\\
   & & \letin{v_{a_i}\in\Z}{\SBpr{e_{a_i}}}{}\\
   & & ~~~~~(\pi_i~\phi)<v_1,\ldots,v_{a_i}>
\)

The meaning of a program $"let"\ d\ "in"\ e$ is
\(
\SB{"let"\ d\ "in"\ e} &\definedas& \SBpr e,
\)
where $\rho$ is some initial environment containing default values for the variables, and
\(
\phi &=& "fix"~\lam{\psi\in\FENV}{<
\begin{array}[t]{@{\hspace{0pt}}l}
\lam{v_1\in\Z,\ldots,v_{a_1}\in\Z}{\SB{e_1}\,\psi\,\rho[v_1/x_1,\ldots,v_{a_1}/x_{a_1}]},\\
~~\vdots\\
\lam{v_1\in\Z,\ldots,v_{a_n}\in\Z}{\SB{e_n}\,\psi\,\rho[v_1/x_1,\ldots,v_{a_n}/x_{a_n}]}>.
\end{array}}
\)

For this fixpoint to exist, we need to know that $\FENV$ a pointed CPO.  But $\FENV$ is a product, and a product is a pointed CPO when each factor is a pointed CPO.  Each factor $\Z^{a_i}\rightarrow \Z_{\bot}$ is a pointed CPO, since 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.

We also need to know that the function $\FENV\to\FENV$ to which we are applying "fix" is continuous, but it is because is written using the metalanguage.

\subsection{CBN Denotational Semantics}

The denotational semantics for CBN is the same as for CBV with two exceptions:
\(
\SBpr{\letin x{e_1}{e_2}} &\definedas& \SB{e_2}\,\phi\,\rho[\SBpr{e_1}/x]\\[1ex]
\SBpr{f_i(e_1,\ldots,e_{a_i})} &\definedas& (\pi_i~\phi)<\SBpr{e_1},\ldots,\SBpr{e_{a_i}}>.
\)

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


