%%% 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{20}                  %% Lecture number
\title{Domain Constructions}   %% Title of lecture
%\author{Michael O'Connor}       %% name of scribe
\date{16 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{\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}

\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\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]}

\begin{document}
\maketitle

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

\section{Reprise}

Last time, guided by the intuition that the programs $\whiledo bc$ and $\ifthenelse b{c;\whiledo bc}{"skip"}$ should be equivalent, we defined the denotation of the statement $\whiledo bc$ as the least solution to the equation
\(
\mathcal W &\definedas& {\lam{\sigma\in\Sigma}{
\begin{cases}
(\mathcal W)^*(\Tr Cc\sigma), & \mbox{if $\Tr Bb\sigma$,}\\
\sigma, & \mbox{otherwise}\\
\end{cases}
}}
\)
in $\Sigma\to\Sigma_\bot$; that is, the least fixpoint of the operator
\(
F &\definedas& \lam{w\in\Sigma\to\Sigma_\bot}{\lam{\sigma\in\Sigma}{
\begin{cases}
(w)^*(\Tr Cc\sigma), & \mbox{if $\Tr Bb\sigma$,}\\
\sigma, & \mbox{otherwise}\\
\end{cases}
}}
\)
of type $(\Sigma\to\Sigma_\bot) \to (\Sigma\to\Sigma_\bot)$.  More simply, we might write
\(
F &\definedas& \lam{w\in\Sigma\to\Sigma_\bot}{\lam{\sigma\in\Sigma}{\ifthenelse{\Tr Bb\sigma}{(w)^*(\Tr Cc\sigma)}\sigma}}
\)
with the understanding that the if-then-else here is purely mathematical.  Here if $w:\Sigma\to\Sigma_\bot$, then $(w)^*:\Sigma_\bot\to\Sigma_\bot$ is the _lift_ of $w$, which sends $\bot$ to $\bot$ and $x$ to $w(x)$ for $x\in\Sigma-\{\bot\}$.
In order to show that the least fixpoint of $F$ exists, we will apply the Knaster--Tarski theorem.  However, we only proved the Knaster--Tarski theorem for the partial order of subsets of some universal set ordered by set inclusion $\subseteq$.  We need to extend it to the more general case of chain-complete partial orders (CPOs).  To apply this theorem, we must know that the function space $\Sigma\to\Sigma_\bot$ is a CPO and that $F$ is a continuous map on this space.

\section{Chain-Complete Partial Orders and Continuous Functions}

Recall that a binary relation $\dlt$ on a set $X$ is a _partial order_ if it is
\begin{itemize}
\item _reflexive_: $x\dlt x$ for all $x\in X$;
\item _transitive_:  for all $x,y,z\in X$, if $x\dlt y$ and $y\dlt z$, then $x\dlt z$;
\item _antisymmetric_: for all $x,y\in X$, if $x\dlt y$ and $y\dlt x$, then $x=y$.
\end{itemize}
It is a _total order_ if for all $x,y\in X$, either $x\dlt y$ or $y\dlt x$.

If $A\subseteq X$, we say that $x$ is an _upper bound_ for $A$ if $y\dlt x$ for all $y\in A$.  We say that $x$ is a _least upper bound_ or _supremum_ of $A$ if $x$ is an upper bound for $A$, and for all other upper bounds $y$ of $A$, $x\dlt y$.  

Upper bounds and suprema need not exist.  For example, the set of natural numbers $\mathbb N$ under its natural order $\leq$ has no supremum in $\mathbb N$.  However, if the supremum of any set exists, it is unique.  A partially ordered set is said to be _complete_ if all subsets have suprema.  The supremum of a set $C$, if it exists, is denoted $\bigsqcup C$.

Note that all elements of $X$ are (vacuously) upper bounds of the empty set $\varnothing$, so if the supremum of $\varnothing$ exists, then it is necessarily the least element of the entire set.  In this case we give it the name $\bot$.

A _chain_ is a subset of $X$ that is totally ordered by $\dlt$.  For example, in the partial order of subsets of $\{0,1,2\}$ ordered by set inclusion, the set $\{\varnothing,\{2\},\{1,2\},\{0,1,2\}\}$ is a chain.  A partially ordered set is _chain-complete_ if all nonempty chains have suprema.  A chain-complete partially ordered set is called a CPO.  The empty chain $\varnothing$ is not included in the definition of chain-complete, but if the empty chain also has a supremum, then it is necessarily the least element $\bot$ of the CPO.  A CPO with a least element $\bot$ is called _pointed_.

Let $X$ and $Y$ be CPOs (we'll use $\dlt$ to denote the partial order in both $X$ and $Y$).  A function $f:X\to Y$ is _monotone_ if $f$ preserves order; that is, for all $x,y\in X$, if $x\dlt y$ then $f(x)\dlt f(y)$.  For example, the exponential function $\lam x{e^x}:\mathbb{R}\to\mathbb{R}$ is monotone.  A function $f:X\to Y$ is _continuous_ if $f$ preserves suprema of nonempty chains; that is, if $C\subseteq X$ is a nonempty chain in $X$, then $\bigsqcup_{x\in C} f(x)$ exists and equals $f(\bigsqcup C)$.  Here $\bigsqcup_{x\in C} f(x)$ is alternate notation for $\bigsqcup\,\{f(x)\mid x\in C\}$.

Every continuous map is monotone: if $x\dlt y$, then $y = \bigsqcup\{x,y\}$, so by continuity $f(y) = f(\bigsqcup\{x,y\}) = \bigsqcup\,\{f(x),f(y)\}$, which implies that $f(x)\dlt f(y)$.

In the definition of continuity, we excluded the empty chain $\varnothing$.  If it were included, then a continuous function would have to preserve $\bot$; that is, $f(\bot)=\bot$.  A continuous function that satisfies this property is called _strict_.  We do not include $\varnothing$ in the definition of continuous functions, because we wish to consider non-strict functions, such as the $F$ of section 1.

\section{The Knaster--Tarski Theorem in CPOs}

Let $F:D\to D$ be any continuous function on a pointed CPO $D$.  Then $F$ has a least fixpoint $\fix F \definedas \bigsqcup_n F^n(\bot)$.  The proof is a direct generalization of the proof for set operators given in Lecture 7, where $\bot$ was $\varnothing$ and $\bigsqcup$ was $\bigcup$.  In a nutshell: by monotonicity, the $F^n(\bot)$ form a chain; since $D$ is a CPO, the supremum $\fix F$ of this chain exists; and by continuity, $\fix F$ is preserved by $F$.

\section{Flat Domains}

Let $S$ be a set with the _discrete ordering_, which means that any two distinct elements of $S$ are $\dlt$-incomparable.  We can make $S$ into a pointed CPO $S_\bot$ by adding a new bottom element $\bot$ and defining $\bot\dlt\bot\dlt x\dlt x$ for all $x\in S$, but nothing else.  This is called a _flat domain_.  For example, $\mathbb N_\bot$ looks like
\begin{center}
\begin{picture}(0,24)(0,-5)
\put(0,-2){\makebox(0,0)[t]{$\bot$}}
\put(-5,0){\line(-5,2){26}}
\put(-3,0){\line(-5,3){17}}
\put(-1,0){\line(-1,1){10}}
\put(0,0){\line(0,1){10}}
\put(1,0){\line(1,1){10}}
\put(3,0){\line(5,3){17}}
\put(5,0){\line(5,2){26}}
\put(-30,12){\makebox(0,0)[b]{$0$}}
\put(-20,12){\makebox(0,0)[b]{$1$}}
\put(-10,12){\makebox(0,0)[b]{$2$}}
\put(0,12){\makebox(0,0)[b]{$3$}}
\put(10,12){\makebox(0,0)[b]{$4$}}
\put(20,12){\makebox(0,0)[b]{$5$}}
\put(30,12){\makebox(0,0)[b]{$6$}}
\put(45,12){\makebox(0,0)[b]{$\cdots$}}
\end{picture}
\end{center}
Any flat domain is chain-complete, since every chain is finite, and every finite nonempty chain has a maximum element, which is its supremum.

\section{Continuous Functions on CPOs Form a CPO}

Now we claim that if $C$ and $D$ are CPOs, then the space of continuous functions $f:C\to D$ is a CPO under the pointwise ordering
\(
f \dlt g &\stackrel\triangle\Longleftrightarrow& \forall x\in C\ f(x)\dlt g(x).
\)
This space is denoted $\cf{C\to D}$.  It is easily verified that $\dlt$ is a partial order on $\cf{C\to D}$.  If $D$ is pointed with bottom element $\bot$, then $\cf{C\to D}$ is also pointed with bottom element
$\bot \definedas \lam{x\in C}\bot$.

We need to show that $\cf{C\to D}$ is chain-complete.  Let $\mathcal C$ be a nonempty chain in $\cf{C\to D}$.  Define
\(
G &\definedas& \lam{x\in C}{\bigsqcup_{g\in\mathcal C} g(x)}.
\)
First, $G$ is a well-defined function, since for any $x\in C$, $\{g(x)\mid g\in\mathcal C\}$ is a chain in $D$, therefore its supremum $\bigsqcup_{g\in\mathcal C} g(x)$ exists.  Also, the function $G$ is continuous, since for any nonempty chain $E$ in $C$,
\reasoning{\bigsqcup_{g\in\mathcal C} g(\bigsqcup E)}
\(
G(\bigsqcup E) &=& \reason{\bigsqcup_{g\in\mathcal C} g(\bigsqcup E)}{by the definition of $G$}\\
&=& \reason{\bigsqcup_{g\in\mathcal C} \bigsqcup_{x\in E} g(x)}{since each $g\in\mathcal C$ is continuous}\\
&=& \reason{\bigsqcup_{x\in E} \bigsqcup_{g\in\mathcal C} g(x)}{by the lemma below}\\
&=& \reason{\bigsqcup_{x\in E} G(x)}{again by the definition of $G$.}
\)
The third step in the above argument uses the following lemma.

\medskip\noindent
\textbf{Lemma} If $a_{xy}$ is a doubly-indexed collection of members of a partially ordered set such that
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item
for all $x$, $\bigsqcup_y a_{xy}$ exists,
\item
for all $y$, $\bigsqcup_x a_{xy}$ exists, and
\item
$\bigsqcup_y \bigsqcup_x a_{xy}$ exists,
\end{enumerate}
then $\bigsqcup_x \bigsqcup_y a_{xy}$ exists and is equal to $\bigsqcup_y \bigsqcup_x a_{xy}$.

\medskip

_Proof_.  Clearly $\bigsqcup_y \bigsqcup_x a_{xy}$ is an upper bound for all $a_{xy}$, therefore it is an upper bound for all $\bigsqcup_y a_{xy}$; and if $b$ is any other upper bound for all $\bigsqcup_y a_{xy}$, then $a_{xy}\dlt b$ for all $x,y$, therefore $\bigsqcup_y \bigsqcup_x a_{xy}\dlt b$, so $\bigsqcup_y \bigsqcup_x a_{xy}$ is the least upper bound for all $\bigsqcup_y a_{xy}$; that is, $\bigsqcup_x \bigsqcup_y a_{xy}=\bigsqcup_y \bigsqcup_x a_{xy}$.\quad$\Box$

\medskip

To apply this lemma, we need to know that
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item
for all $g\in\mathcal C$, $\bigsqcup_{x\in E} g(x)$ exists,
\item
for all $x\in E$, $\bigsqcup_{g\in\mathcal C} g(x)$ exists, and
\item
$\bigsqcup_{g\in\mathcal C} \bigsqcup_{x\in E} g(x)$ exists.
\end{enumerate}
But (i) holds because all $g\in\mathcal C$ are continuous, therefore $\bigsqcup_{x\in E} g(x)=g(\bigsqcup\,E)$; (ii) holds because $\{g(x)\mid g\in\mathcal C\}$ is a chain in $D$, and $D$ is chain-complete; and (iii) follows from (i) and (ii) by taking $x=\bigsqcup\,E$.

\section{Fixpoints and the Semantics of \textsf{while-do}}

Now let's return to the denotational semantics of the "while" loop.  We previously defined the function
\(
F &:& (\Sigma\to\Sigma_\bot)\ \ \to\ \ (\Sigma\to\Sigma_\bot)\\
F &\definedas& \lam{w\in\Sigma\to\Sigma_\bot}{\lam{\sigma\in\Sigma}{\ifthenelse{\Tr Bb\sigma}{(w)^*(\Tr Cc\sigma)}\sigma}}.
\)
Any function $\Sigma\to\Sigma_\bot$ is continuous, since chains in the discrete space $\Sigma$ contain at most one element, thus the space of functions $\Sigma\to\Sigma_\bot$ is the same as the space of continuous functions $\cf{\Sigma\to\Sigma_\bot}$.  Moreover, the lift $(w)^*:\Sigma_\bot\to\Sigma_\bot$ of any function $w:\Sigma\to\Sigma_\bot$ is continuous.

By previous arguments, the function space $\cf{\Sigma\to\Sigma_\bot}$ is a pointed CPO, and $F$ maps this space to itself.  To obtain a least fixpoint by Knaster--Tarski, we need to know that $F$ is continuous.

Let's first check that it is monotone.  This will ensure that, when trying to check the definition of continuity, when $C$ is a chain, $\{F(d)\mid d\in C\}$ is also a chain, so that $\bigsqcup_{d\in C} F(d)$ exists.  Suppose $d\dlt d'$.  We want to show that $F(d)\dlt F(d')$.  But for all $\sigma$,
\(
F(d)(\sigma) &=& \ifthenelse{\Tr Bb\sigma}{(d)^*(\Tr Cc\sigma)}\sigma\\
&\dlt& \ifthenelse{\Tr Bb\sigma}{(d')^*(\Tr Cc\sigma)}\sigma\\
&=& F(d')(\sigma).
\)
Here we have used the fact that the operator $(\cdot)^*$ is monotone, which is easy to check.

Now let's check that $F$ is continuous.  Let $C$ be an arbitrary chain.
We want to show that $\bigsqcup_{d\in C} F(d) = F(\bigsqcup C)$. We have
\(
\bigsqcup_{d\in C} F(d) &=& \bigsqcup_{d\in C}\lam{\sigma}{\ifthenelse{\Tr Bb\sigma}{(d)^*(\Tr Cc\sigma)}\sigma}\\
&=& \lam{\sigma}{\bigsqcup_{d\in C}\ifthenelse{\Tr Bb\sigma}{(d)^*(\Tr Cc\sigma)}\sigma}\\
&=& \lam{\sigma}{\ifthenelse{\Tr Bb\sigma}{\bigsqcup_{d\in C} (d)^*(\Tr Cc\sigma)}\sigma}\\
&=& \lam{\sigma}{\ifthenelse{\Tr Bb\sigma}{(\bigsqcup\,C)^*(\Tr Cc\sigma)}\sigma}\ \ =\ \ F(\bigsqcup\,C),
\)
since $\Tr Bb\sigma$ does not depend on $d$ and since the lift operator $(\cdot)^*$ is continuous.

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