%%% 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!.

%%% Below I've included a bunch of 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 me if you come up with some good ones so I
%%% can add them 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[10pt]{article} \usepackage{611-lecture}
\newcommand{\ifff}{\Leftrightarrow}
\newcommand\aexp{\bf{Aexp}}
\newcommand\bexp{\bf{Bexp}}
\newcommand\com{\bf{Com}}
\newcommand\Z{\mathbb{Z}}
\newcommand\Truth{\mathbb{T}}
%\newcommand\Nat{\mathbb{N}}
\usepackage{graphicx}
\usepackage{amsmath}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{16--17} %% Lecture number
\title{Denotational Semantics of IMP}   %% Title of lecture
\author{Andrew Myers (based on earlier student notes)}
%% name of scribe
\date{October 4--6, 2004}    %% Date of lecture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture
%
% quotes can be used for typewriter font
% <> can be used for angle brackets

\def\A#1{{\cal A}\SB{#1}}
\def\B#1{{\cal B}\SB{#1}}
\def\C#1{{\cal C}\SB{#1}}
%\newcommand\join{\bigsqcup}

\begin{document}

\maketitle

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

These notes cover the denotational semantics of IMP, partial orders, and
the fixed-point theorem.

%%%%% SLIDE 2 %%%%%
\section{Operational Semantics vs.~Denotational Semantics}

We have described the behavior of programs in an operational manner
by inductively defining transition relations to express evaluation and
execution.  It is fairly easy to to turn the description of the semantics into a
an interpreter for the programming language. However, we still have not
arrived at a extensional description of programs. We have explored
semantic definitions based on translation to a better-understood
language, but this in some sense just pushes the problem somewhere else.

The denotational semantics approach is also based on translation,
but the target of the translation is mathematical objects, usually
functions, rather than a programming language.  A semantic function will
map syntax to corresponding _denotations_ (meanings), arriving at an
extensional meaning for the program. Alternatively, we can think of the
semantic function as compiling the program into a mathematical object.

\section{Semantic Functions}

Denotational semantics operates on expressions to produce mathematical
objects that are the meaning of the expression.  The mechanism for this
is a {\it {semantic function}}, which is usually a mathematical
function.

\medskip
\textbf{Example:}  \[ \SB{(\lam xx)} = \lm xDx \]

Here $\SB{\cdot}$ is a semantic function that maps the expression
to its _denotation_, in this case a mathematical function from some
domain $D$ to the same codomain.
compiler. The semantic function takes as input $(\lam{x}{x})$,
which is a piece of abstract
syntax, which we know is really an abstract syntax tree.

The lambda expression on the right-hand side is a mathematical function,
so it can also be represented by its extension:
$\{(a,a) \mid a \in D\}$.

\section{Modeling issues}

We have already been using lambda terms to describe programs, but some
of the expressions we have written down have been problematic from the
mathematical perspective. When we write down denotations, we need to
make sure that all of the denotations are mathematically well-defined.

For example, consider the identity function
above. What is the domain $D$? We have not identified a set for $D$ that
can adequately model the lambda calculus and that can satisfy the
equational theory of the lambda calculus. And not just any $D$ will
work.  For the denotational semantics to be sound, the various
reductions cannot change the meaning according to the semantic function.
For example, we should have $\SB{(\lam{x}{e})e'} = \SB{e\{e'/x\}}$,
because these terms are beta-equivalent.

But coming up with such a $D$ is not easy. Consider the term
$\lam{x}{(x~x)}$, in which $x$ is applied to itself. If $x$ is drawn
from some domain $D$, then it seems that the domain $D$ must be
isomorphic to the function space $D\to D$ in order to be able to
apply $x$ to itself. But a simple diagonalization argument will
convince us that these domains cannot be isomorphic. Suppose we had
a one-to-one mapping $f$ from elements of $D$ to elements of $D\to E$,
where $E$ is any set containing at least two elements. But then
consider the function $\lam{x\in D}{\lnot(f(x)(x))}$, where $\lnot$
is some function on $E\to E$ that maps no element of $E$ to itself.
This function cannot possibly be the image in $f$ of any element
of $D$, because it gives a different answer than $f(x)$ for every
$x\in D$. So by contradiction the isomorphism cannot exist.

We have already been using translation to explain language semantics and
getting some insight. And the denotational semantics we will be
exploring will be similar. But some of the tricks we were using earlier
won't work. Because self-application leads to the above paradox (which
is really Russell's paradox), the $Y$ combinator is not acceptable as
part of the semantics.

%%%%% SLIDE 4 %%%%%

\section{Parsing lambda expressions and function domains}

We will continue to use some conventions to make our semantics more
concise. The expression $\lam{xyz}{e}$ means a function that takes
the three variables $x$, $y$, and $z$ as input and gives $e$ as output.
However, this function can be curried, applying one argument at a time,
so it is the same as $\lam{x}{\lam{y}{\lam{z}{e}}}$.

$\lambda-$expressions extend as far to the
right as possible. Thus, $\lam{x}{\lam{y}{\lam{z}{x~\lam ww}}} =
\lam{x}{(\lam{y}{(\lam{z}{(x~(\lam ww))})})}$. It doesn't hurt to
throw in extra parentheses to make things clearer, though!
Application is left associative.  Thus, $xyz = (xy)z$.

%%%%% SLIDE 5 %%%%%

We will usually
write the type (domain) of the argument of a function unless the
name of the argument makes it obvious.  For example, consider the
following function that adds two integers:

\[PLUS = \lm{x}{\Z}{\lm{y}{\Z}{x+y}} \]

This function takes $x \in \Z$ and $y \in \Z$
(one argument at a time) and
yields $x + y \in \Z$.  Thus, the domain is $Z$ and the codomain
is $Z \to Z$. So we have

\[ PLUS : \Z \to (\Z \to \Z) \]

While application associates to the left, the constructor
($\to$) associates to the right. For example, here
we do not need parentheses:

\[ PLUS : \Z \to \Z \to \Z \]

Note that we're giving the domain of a function argument using the
symbol $\in$. It's more typical to use a colon (:), but we'll reserve
that for when we are writing functions in a typed programming language.

%%%%% SLIDE 6-10 %%%%%
\section{Semantic Functions for IMP}

Recall now the three kinds of expressions in IMP:

\begin{itemize}
\item arithmetic expressions $\aexp$:

$a ::= n \bnf x \bnf a_0 + a_1 \bnf a_0 - a_1 \bnf a_0 \times a_1$
\item boolean expressions $\bexp$:

$b ::= \TRUE \bnf \FALSE \bnf a_0\leq a_1 \bnf a_0 = a_1 \bnf \lnot b \bnf b_0\land b_1 \bnf b_0\lor b_1$ 

\item commands $\com$:

$x:=a_0 \bnf \SKIP  \bnf \cond{b_0}{c_0}{c_1} \bnf \while{b_0}{c_0}$

\end{itemize}

What is the intrinsic meaning of these syntactic categories?

Take for example the arithmetic expressions. Our first thought when
we see an expression is to evaluate it and thus the meaning of an
arithmetic expression is an integer.  However, this doesn't explain
the meaning of an 
evaluation
depends on the particular store we have: given store $\sigma $, each 
expression $a$ denotes a unique integer, so the meaning of an arithmetic
expression is really a function from stores to integers. Hence we can
define a function
${\cal A}$ which translates the syntax of the arithmetic expressions into
their meaning:
\[\A{a} \sigma = n \ifff < a,\sigma >\stepsto n\]
Similarly, given a particular store $\sigma $ boolean expressions $b$ 
denotes a unique truth value $t\in\Truth = \{\nm{true}, \nm{false}\}$.
We can define:
\[\B{b} \sigma = t \ifff < b,\sigma >\stepsto t\]
Since a command $c$ maps one store into another,  we define:
\[\C{c} \sigma = \sigma' \ifff < c,\sigma >\stepsto \sigma'\]
Therefore, the meaning functions ${\cal A, B, C}$ have the following
types:
\begin{itemize}
\item ${\cal A}\in {\bf Aexp} \arrow (\Sigma \arrow \Nat)$
\item ${\cal B}\in {\bf Bexp} \arrow (\Sigma \arrow \Truth)$
\item ${\cal C}\in {\bf Com} \arrow (\Sigma \arrow \Sigma)$
\end{itemize}
We say that the arithmetic expression $a$ denotes $\A{a}$ and $\A{a}$ is a
denotation of $a$. Similarly, $\B{b}$ is a denotation of the boolean $b$ and 
$\C{c}$ is a denotation of command $c$. Each denotation is in fact a function:
\begin{itemize}
\item $\A{a}:\Sigma \arrow \Nat$
\item $\B{b}:\Sigma \arrow \Truth$
\item $\C{c}:\Sigma \arrow \Sigma$
\end{itemize}
This signature for ${\cal{C}}$ won't quite work, however, because of the
possibility of non-termination.  We'll see how to fix it shortly. \\ \\ 
The functions ${\cal A, B, C}$ are defined by structural induction.

\subsection{Arithmetic Denotations}
First, we define the denotation of arithmetic expressions ${\cal A}\in 
{\bf Aexp} \arrow (\Sigma \arrow \Nat)$ using structural induction:
\begin{itemize}
\item $\A{n}=\lam{\sigma \in \Sigma}{n}$
\end{itemize}
This means that the denotation of $n$ is a function which associates the
natural number $n$ to any state $\sigma $.

Similarly,
\begin{itemize}
\item $\A{x}=\lam{\sigma \in \Sigma}{\sigma x}$
\item $\A{a_0+a_1}=\lam{\sigma \in \Sigma}{\A{a_0}\sigma +\A{a_1}\sigma }$
\item $\A{a_0-a_1}=\lam{\sigma \in \Sigma}{\A{a_0}\sigma -\A{a_1}\sigma }$
\item $\A{a_0\times a_1}=\lam{\sigma \in \Sigma}{\A{a_0}\sigma \times \A{a_1}
\sigma }$
\end{itemize}
Notice that the signs $+,-,\times $ on the left-hand sides represent 
syntactic signs in IMP, whereas the signs on the right represent operations 
on numbers.

We can write the last three definitions as inductive definitions, similar to
the inference rules in the operational semantics:
\[
\infer
  {\A{a_0+a_1}=\lam{\sigma \in \Sigma }{f_0\sigma +f_1\sigma }}{
  \A{a_0}=f_0
  & 
  \A{a_1}=f_1
}\]

Instead of using $\lambda$ notation, we can present the definition of
the semantics as a relation between states and numbers:
\begin{itemize}
\item $\A{n}=\{(\sigma ,n) \mid \sigma \in \Sigma\}$
\item $\A{x}=\{(\sigma ,\sigma (x) \mid \sigma \in \Sigma\}$
\item $\A{a_0+a_1}=\{(\sigma,n_0+n_1) \mid (\sigma ,n_0)\in \A{a_0}\land 
(\sigma ,n_1)\in \A{a_1}\}$
\item $\A{a_0-a_1}=\{(\sigma,n_0-n_1) \mid (\sigma ,n_0)\in \A{a_0}\land 
(\sigma ,n_1)\in \A{a_1}\}$
\item $\A{a_0\times a_1}=\{(\sigma,n_0\times n_1) \mid (\sigma ,n_0)\in \A{a_0}
\land (\sigma ,n_1)\in \A{a_1}\}$
\end{itemize}

\subsection{Boolean Denotations}
%%%%% SLIDE 11 %%%%%
As for the arithmetic expressions, the function ${\cal B}: {\bexp}
\to (\Sigma \to T)$ is defined using induction on the
structure of expressions. \\ \\ 
We start by applying $\cal{B}$ to the booleans with no subexpressions as
follows:
\begin{itemize}
\item $\B{\TRUE} = \lambda \sigma \in \Sigma.\nm{true}$
\item $\B{\FALSE} = \lambda \sigma \in \Sigma.\nm{false}$
\end{itemize}
After applying both sides of the first function to $\sigma$, we get
$\B{\TRUE}\sigma = \nm{true}$ by a $\beta$ reduction.  Not only is this
notation more compact, it makes the meaning more clear. \\ \\
The rest of the rules for boolean denotations are as follows:

\begin{itemize}
\item $\B{a_0 = a_1} \sigma = \cond{\A{a_0}\sigma = \A{a_1}\sigma}{\nm{true}}{\nm{false}}$
\item $\B{a_0 \leq a_1} \sigma = \cond{\A{a_0}\sigma \leq \A{a_1}\sigma}{\nm{true}}{\nm{false}}$
\item $\B{b_0 \land b_1} \sigma = \cond{\B{b_0}\sigma \ \land \ \B{b_1}\sigma}{\nm{true}}{\nm{false}}$
\item $\B{b_0 \lor b_1} \sigma = \cond{\B{b_0}\sigma \ \lor \ \B{b_1}\sigma}{\nm{true}}{\nm{false}}$
\end{itemize} 

\subsection{Command Denotations}	
%%%%% SLIDE 12 %%%%%
In order to derive the rules for command denotations, we first note that
some commands $c$ do not terminate. Formally,
\[ (\lnot \exists \sigma '. <c,\sigma> \stepsto \sigma ') \]

An example of such a command is $\while{\TRUE}{\SKIP}$. One way to
think about commands is as partial functions from states to states $(\Sigma
\rightharpoonup \Sigma)$.  For example, consider the command
($\while{\ x=0 \ }{\ \SKIP}$).  Its denotation is given by
$\{(\sigma,\sigma) \ | \ \sigma(x) = 0\}$, which is not defined for $\sigma(x) \neq 0$.  Thus, the corresponding command
is not total.

In order to make denotations total, we add a new state, $\perp$, called _bottom_, to represent
non-termination. We write $\Sigma_\perp$ to mean the set $\Sigma$
augmented with the element $\perp$, that is, $\Sigma \cup \{\perp\}$.
This is read as the ``lift'' of $\Sigma$.
Commands become functions from $\Sigma$ to $\Sigma_\perp$, and 
${\cal{C}} \in {\com}
\to (\Sigma \to \Sigma_\perp)$.

Using $\perp$ we can now extensionally specify the behavior of commands,
including nontermination, in a way that wasn't possible with operational semantics.

The function $\cal{C}: \com \to \Sigma \to \Sigma_\perp$ is also defined using induction
on the structure of commands as follows:
\begin{itemize}
\item $\C{\SKIP}\sigma = \sigma$
\item $\C{x:=a}\sigma = \sigma[x \mapsto \A{a} \sigma]$
\item $\C{c_0;\,c_1} \sigma = \left\{ \begin{array}{ll}
	\C{c_1}( \C{c_0}\sigma) & (\mbox{if~} \C{c_0}\sigma) \neq \perp ) \\
\perp & (\mbox{otherwise}) \end{array} \right. $


\item $\C{\cond{b}{c_0}{c_1}} \sigma = \cond{\B{b}\sigma}{\C{c_0} \sigma}{\C{c_1} \sigma}$
\end{itemize}
Note:  It's ok to use $\cal{B}$ in the definition of the denotations, since it's not circular. \\

%%%%% SLIDE 14 and 15%%%%%
Let us now try to define the denotation for {\WHILE}. In a similar manner
with the above definitions, we would like to write:
\[\C{\while{b}{c}}\sigma=\cond{\ \lnot \B{b}\sigma }{\ \sigma }{\ \C{\while{b}
{c}}(\C{c}\sigma )}\]
Unfortunately, this definition is circular: it involves $\C{\while{b}{c}}$
on both sides. The above is actually not a definition, but an equation.
We can also write this equation as an equation about sets:
\vspace{1ex}

$\C{\while{b}{c}}=
\{ (\sigma ,\sigma)~\mid~\lnot ~\B{b}\sigma \} $

\hspace{2.25cm}
$ \cup ~~\{ (\sigma, \perp) ~\mid~ \B{b}\sigma ~ \land ~ \C{c}\sigma =
\perp \} $

\hspace{2.25cm}
$ \cup ~~\{  (\sigma ,\sigma^\prime) ~\mid ~ \B{b}\sigma ~ \land ~
\C{c}\sigma =
\sigma^{{\prime}{\prime}} \neq \perp ~ \land ~ \C{\while{b}{c}}
\sigma^{{\prime}{\prime}} = \sigma^\prime \}$

\vspace{5mm}\noindent
We can rewrite this as

$\C{\while{b}{c}}=
\{ (\sigma ,\sigma)~ \mid ~ \lnot \B{b}\sigma \} $

\hspace{2.25cm}
$ \cup ~~\{ (\sigma, \perp) ~\mid ~ \B{b}\sigma ~ \land ~ (\sigma, \perp)
~\in ~\C{c} \} $
%\C{c}\sigma =
%\perp \} $

\hspace{2.25cm}
$ \cup ~~\{  (\sigma ,\sigma^\prime) ~ \mid ~ \B{b}\sigma ~ \land ~
(\sigma ,
\sigma^{{\prime}{\prime}}) ~\in ~\C{c} ~ \land ~ 
(\sigma^{{\prime}{\prime}}, \sigma^\prime) ~\in ~
 \C{\while{b}{c}} \}$

where $\sigma^{{\prime}{\prime}} \neq \perp$. 

%%%%% SLIDE 16 and SLIDE 17%%%%%
We must come up with an alternative definition for $\WHILE$.
%Because the following: $$\C{\while{b}{c}} \sigma = \cond{\lnot \B{b}\sigma}{\sigma}{\C{\%while{b}{c}}}(\C{c} \sigma)$$ is an
%equation and not a definition, we must come up with an alternative definition for while.
In order to do this, we will construct a function $F$ such that
the denotation of {\WHILE} is a fixed point of
$F$.  We can define $F$ equivalently using either sets or
lambda notation (here $d$ is a command denotation):

\begin{align*}
F = \lm {d}{\Sigma\to \Sigma_\perp}{ &
    \{ (\sigma ,\sigma) ~ \mid ~ \lnot \B{b}\sigma \} \\
& \cup ~~\{ (\sigma, \perp) ~\mid ~ \B{b}\sigma ~ \land ~ (\sigma, \perp)
~\in ~\C{c} \}}  \\
%\C{c}\sigma =
%\perp \} $
& \cup ~~\{  (\sigma ,\sigma^\prime) ~ \mid ~ \B{b}\sigma ~ \land ~
(\sigma ,
\sigma^{{\prime}{\prime}}) ~ \in ~ \C{c} ~ \land ~ 
(\sigma^{{\prime}{\prime}}, \sigma^\prime) ~ \in
 ~ d \} \\
F = \lm{d}{\Sigma\to\Sigma_\perp}{&
\lm{\sigma}{\Sigma}{\nm{if}
\ \lnot{}{\cal B}\SB{b}\ \nm{then}\ \sigma\ \nm{else}\ d^*(\C{c}\sigma)}}.
\end{align*}

\noindent where given $f: D\to E_\perp$, we define $f^* : D_\perp\to E_\perp =
\lm{x}{D_\perp}{\nm{if}~x=\perp~\nm{then}~\perp~\nm{else}~f(x)}$
Then, the denotation of {\em while} is a fixed point of $F$, i.e.,
$\C{\while{b}{c}} = F(\C{\while{b}{c}})$.

\section{Finding fixed points}

Suppose we had an operator \nm{fix} that could find fixed points.
What we would like to do is define 
\begin{eqnarray*}
\C{\while{b}{c}} & = & \nm{fix}(F)\\
                 & = & \nm{fix}(\lm{d}{\Sigma\to\Sigma_\perp}
{\lam{\sigma\in\Sigma}{\nm{if}
\ \lnot{}{\cal B}\SB{b}\sigma\ \nm{then}\ \sigma\ \nm{else}\ d^*(\C{c}\sigma)}})
\end{eqnarray*}
But which fixed point of $F$ do we want?  We would like to take
the ``least'' fixed point, in the sense that we want
$\C{\while{b}{c}}$ to give a non-$\perp$ result only when required by
the intended semantics.  (For example, we want
$\C{\while{\TRUE}{\SKIP}}\sigma{}=\,\perp$ for all $\sigma$.)

First, let's get an idea what this least fixed point might look like.
Iterating $F$ applied to some ``minimal'' function
$d_\perp = \lam{\sigma}\perp$
yields a sequence of successively better approximations
to $\C{\while{b}{c}}$:
\begin{eqnarray*}
d_0 & = & d_\perp \\
d_1 & = & F(d_\perp)\\
    & = & \lam{\sigma}{\nm{if}\ \lnot
{\cal B}\SB{b}\ \nm{then}\ \sigma\ \nm{else}\ \perp}\\
d_2 & = & F(F(d_\perp))\\
    & = & \lam{\sigma}{\nm{if}\ \lnot
{\cal B}\SB{b}\sigma\ \nm{then}\ \sigma\ \nm{else}}\\
    &   & \ \ \ \ \ \ \ \nm{if}\ \lnot{}{\cal B}\SB{b}\C{c}\sigma\ \nm{then}\ \C{c}\sigma
\ \nm{else}\ \perp\\
d_3 & = & F(F(F(d_\perp)))\\
    & = & \lam{\sigma}{\nm{if}\ \lnot
{\cal B}\SB{b}\sigma\ \nm{then}\ \sigma\ \nm{else}}\\
    &   & \ \ \ \ \ \ \ \nm{if}\ \lnot{}{\cal B}\SB{b}\C{c}\sigma\ \nm{then}\ \C{c}\sigma
\ \nm{else}\\
    &   & \ \ \ \ \ \ \ \ \ \nm{if}\ \lnot{}{\cal B}\SB{b}\C{c}\C{c}\sigma
\ \nm{then}\ \C{c}\C{c}\sigma\ \nm{else}\ \perp\\
    & \vdots &\\
d_n & = & F^{n}(d_\perp)\\
    & \vdots &
\end{eqnarray*}

Here, $d_0$ doesn't really approximate {\WHILE} at all, but $d_1$
correctly describes a loop whose guard is evaluated once (to false),
$d_2$ correctly describes a loop whose guard is evaluated once or twice,
and in general $d_n$ describes a loop whose guard is evaluated up to $n$
times.

The ``limit'' of this sequence will be the denotation of $\while{b}{c}$.
To take this limit, and to show that it is the _least_ fixed point,
we will need more mathematical machinery, starting with the the theory
of _partial orders_.

\section{Partial Orders}

A _partial order_ (also known as a _partially ordered set_ or _poset_)
is a pair $(S, \sqsubseteq)$, where
\begin{list}{$\bullet$}{}
\item $S$ is a set of elements.
\item $\sqsubseteq$ is a relation on $S$ which is:
 \newcounter{littlelist}
 \begin{list}{\emph{\roman{littlelist}}.}{\usecounter{littlelist}}
   \item reflexive: $x\sqsubseteq{}x$
   \item transitive: $(x\sqsubseteq{}y \land y\sqsubseteq{}z)
                            \Rightarrow x\sqsubseteq{}z$
   \item antisymmetric: $(x\sqsubseteq{}y \land y\sqsubseteq{}x)
                            \Rightarrow x=y$
 \end{list}
\end{list}

\paragraph{Examples:}
\begin{list}{$\bullet$}{}
\item $(\Z, \leq)$, where $\Z$ is the integers and $\leq$ is
the usual ordering.
\item $(\Z, =)$  (Note that unequal elements are incomparable
  in this order.  Partial orders ordered by the identity relation,
  $=$, are called \emph{discrete}.)
\item $(2^S, \subseteq)$ (Here, $2^S$ denotes the powerset of $S$,
the set of all subsets of $S$,
  often written ${\cal P}(S)$, and in Winskel, ${\cal P}ow(S)$.)
\item $(2^S, \supseteq)$
\item $(S, \sqsupseteq)$, if we are given that $(S, \sqsubseteq)$ is a
partial order.
\item $(\omega, |)$, where $\omega = \{0, 1, 2, \ldots\}$
  and $a|b \Leftrightarrow (a$ divides $b) \Leftrightarrow
  (b = ka$ for some $k\in\omega)$.  Note that for any $n\in\omega$, we have
  $n|0$; we call $0$ an upper bound for $\omega$ (but only in this
  ordering, of course!).  
\end{list}

\paragraph{Non-examples:}
\begin{list}{$\bullet$}{}
\item $(\Z, \lt)$ is not a partial order, because $\lt$ is not
  reflexive.
\item $(\Z, \sqsubseteq)$, where
  $m\sqsubseteq{n}\Leftrightarrow |m|\leq|n|$, is not a partial order
  because $\sqsubseteq$ is not anti-symmetric: $-1\sqsubseteq{}1$ and
  $1\sqsubseteq{}-1$, but $-1\neq{}1$.

\end{list}

The ``partial'' in partial order comes from the fact that our
definition does not require these orders to be total; e.g., in
the partial order $(2^{\{a,b\}}, \subseteq)$, the elements $\{a\}$ and
$\{b\}$ are incomparable: neither $\{a\}\subseteq\{b\}$ nor
$\{b\}\subseteq\{a\}$ hold.

\paragraph{Hasse diagrams}
Partial orders can be described pictorially using 
\emph{Hasse diagrams}\footnote{Named after Helmut Hasse, 1898-1979.  Hasse
published fundamental results in algebraic number theory, including the
Hasse (or ``local-global'') principle. He succeeded Hilbert and Weyl as
the chair of the Mathematical Institute at G\"{o}ttingen.}.
In a Hasse diagram, each element of the partial order is displayed as
a (possibly labeled) point, and lines are drawn between these points,
according to these rules:
\begin{enumerate}
\item If $x$ and $y$ are elements of the partial order, and $x \sqsubseteq y$,
  then the point corresponding to $x$ is drawn lower in the diagram than 
  the point corresponding to $y$.
\item A line is drawn between the points representing two elements $x$ and $y$ 
  \nm{iff} $x \sqsubseteq y$ and $\lnot\exists z$ in the partial order, distinct
  from $x$ and $y$, such that 
  $x \sqsubseteq z$ and $z \sqsubseteq y$ 
  (i.e., the ordering relation between $x$ and $y$ is not due to
  transitivity).
\end{enumerate}
An example of a Hasse diagram for the partial order on the set $2^{\{a,b,c\}}$
using $\subseteq$ as the binary relation is:
\begin{center}
\includegraphics{hasse}
\end{center}

\paragraph{Least upper bounds} 
Given a partial order $(S,\sqsubseteq)$, and a subset $B \subseteq S$,
$y$ is an \emph{upper bound} of $B$ \nm{iff} $\forall x \in B . x \sqsubseteq y$.
In addition, $y$ is a \emph{least upper bound} \nm{iff} $y$ is an upper bound
and $y \sqsubseteq z$ for all upper bounds $z$ of $B$.  We may abbreviate
``least upper bound'' as LUB or lub.  We shall notate the LUB of a subset
$B$ as $\bigsqcup B$.  We may also make this an infix operator, as in
$\bigsqcup \{ x_1,\ldots,x_m \} = x_1 \sqcup\ldots\sqcup x_m$. This is
also known as the _join_ of elements $x_1,\ldots, x_m$.

\paragraph{Chains} A \emph{chain} is a pairwise comparable sequence of 
elements from a partial order (i.e., elements $x_0,x_1,x_2\ldots$
such that $x_0 \sqsubseteq x_1 \sqsubseteq x_2 \sqsubseteq\ldots$).  For any
finite chain, its LUB is its last element (e.g., 
$\bigsqcup \{ x_0, x_1,\ldots, x_n \} = x_n$).  Infinite chains (Winskel:
$\omega$-chains) may also have LUBs.

\paragraph{Complete partial orders} A \emph{complete partial order} (CPO)
is a partial order in which every chain has a LUB.  Note that the 
requirement for \emph{every} chain is trivial for finite chains (and thus
finite partial orders) -- it is the infinite chains that can cause trouble.

Some examples of CPOs:

\begin{itemize}
\item $(2^S, \subseteq)$ Here $S$ itself is the LUB for the chain of 
  all elements.
\item $(\omega \cup \{\infty\}, \leq)$ Here $\infty$ is the LUB for any 
  infinite chain: $\forall w \in \omega . w \leq \infty$.
\item $([0,1], \leq)$ where $[0,1]$ is the closed continuum, and 1 is a LUB
  for infinite chains.  Note that making the continuum open at the top -- 
  $[0,1)$ -- would cause this to no longer be a CPO, since there would be
  no LUB for infinite chains such as ${1\over2}, {2\over3}, {3\over4},\ldots$
\item $(S,=)$ This is a discrete CPO, just as it is a discrete partial order.
  The only infinite chains are of the sort $x_i \sqsubseteq x_i 
  \sqsubseteq x_i\ldots$, of which $x_i$ is itself a LUB.
\end{itemize}

Even if $(S,\sqsubseteq)$ is a CPO, $(S,\sqsupseteq)$ is not necessarily a CPO.  
Consider $((0,1],\leq)$, which is a CPO.  Reversing its binary relation yields 
$((0,1],\geq)$ which is not a CPO, just as $([0,1),\leq)$ above was not.

CPOs can also have a least element, written $\bot$, such that 
$\forall x . \bot \sqsubseteq x$. We call a CPO with such an element 
a \emph{pointed CPO}.  Winskel instead uses \emph{CPO with bottom}.




\section{Least fixed points of functions}

Recall that at the end of the last lecture we were attempting to define the
least fixed point operator \nm{fix} over the domain $(\Sigma \to 
\Sigma_\bot)$ so that we could determine calculate fixed points of $F: 
(\Sigma \to \Sigma_\bot)\to (\Sigma 
\to \Sigma_\bot)$.  It was unclear, however, what the ``least'' fixed
point of this domain would be---how is one function from states to states
``less'' than another?  We've now developed the theory to answer that
question.

We define the ordering of states by \emph{information content}: $\sigma  
\sqsubseteq \sigma'$ \nm{iff} $\sigma$ gives less (or at most as much)
information than $\sigma'$. 
Non-termination is defined to provide less information than any other state:
$\forall \sigma \in \Sigma . \bot \sqsubseteq \sigma$.  In addition,
we have that $\sigma \sqsubseteq \sigma$.  No other
pairs of states are defined to be comparable.  The lifted
set of possible states $\Sigma_\bot$ can now be characterized as a flat
CPO (a lifted discrete CPO):

\begin{itemize}
\item Its elements are elements of $\Sigma \cup \{\bot\}$.
\item The ordering relation $\sqsubseteq$ satisfies the reflexive,
  transitive, and anti-symmetric properties.
\item There are three types of infinite chains, each with a LUB:
  \begin{enumerate}
    \item $\bot \sqsubseteq \bot \sqsubseteq\ldots$, LUB = $\bot$
    \item $\sigma \sqsubseteq \sigma \sqsubseteq\ldots$, LUB = $\sigma$
    \item $\bot \sqsubseteq \bot \sqsubseteq\ldots\sqsubseteq \sigma
      \sqsubseteq \sigma \sqsubseteq\ldots$, LUB = $\sigma$
  \end{enumerate}
\end{itemize}

\section{Functions}

We are now ready to define an ordering relation on functions.  Functions
will be ordered using a \emph{pointwise ordering} on their results.
Given a CPO $E$, a domain $D$, $f \in D \to E$, and 
$g \in D \to E$:

\[ f \sqsubseteq_{D \to E} g \stackrel{def}{\iff} \forall x \in D
   . f(x) \subseteq_E g(x) \]

Note that we are defining a new partial order
over $D \to E$, and that this
CPO is pointed if $E$ is pointed, since $\bot_{D \to E} = \lambda
x \in D . \bot_E$.

As an example, consider two functions $\Z \to \Z_\bot$:
\begin{eqnarray*}
f & = & \lambda x \in \Z . \cond{x=0}{\bot}{x} \\
g & = & \lambda x \in \Z . x \\
\end{eqnarray*}

We conclude $f \sqsubseteq g$ because $f(x) \sqsubseteq g(x)$ for all $x$; 
in particular, $f(0) = \bot \sqsubseteq 1 = g(0)$.


If $E$ is a CPO, then the function space $D\to E$ is also a CPO.
We show that given a chain of functions $f_1 \sqleq f_2 \sqleq f_3 \ldots$,
the function $\lambda d\in D . \bigsqcup_{n\in\omega} f_n(d)$ is a least
upper bound for this chain. Consider any function $g$ that is an upper bound
for all the $f_n$. In that case, we have:

\begin{eqnarray*}
&&            \forall n\in\omega . \forall d\in D . f_n(d) \sqleq g(d) \\
&\iff& \forall d\in D . \forall n\in\omega . f_n(d) \sqleq g(d) \\
\end{eqnarray*}

Because the $f_n$ form a chain, so do the $f_n(d)$, and because $E$ is a
CPO,
it has a least upper bound that is necessarily less than the upper bound
$g(d)$:

\begin{eqnarray*}
&\Rightarrow& \forall d\in D . (\bigsqcup_{n\in\omega} f_n(d)) \sqleq g(d) \\
&\iff& \forall d\in D(\bigsqcup_{n\in\omega} f_n)(d) \sqleq g(d) \\
&\iff& \bigsqcup_{n\in\omega} f_n \sqleq g
\end{eqnarray*}

Therefore, $D\Rightarrow E$ is a CPO under the pointwise ordering.

\section{Back to \WHILE}

It's now time to unify our dual understanding of the denotation of {\WHILE}
as both a limit and a fixed point.

We previously defined the denotation of {\WHILE} as both:

\begin{eqnarray*}
\C{\while{b}{c}} & = & \nm{fix}(F)\\
 & = & \mbox{limit of\ } F^n(\bot)\\
\end{eqnarray*}

However, we did not know how to define the $\nm{fix}$ operator over the
range of $F$, nor did we have a definition for the least
fixed point of $F$ to take as its limit.  CPOs have given us
the machinery to handle these definitions now.

We assert that:

\[ \C{\while{b}{c}} = \bigsqcup_{n \in \omega} F^n(\bot) \]

As an example to give us confidence that this is the correct definition,
we see that:

\begin{eqnarray*}
\C{\while{\TRUE}{\SKIP}} & = & \bigsqcup_{n \in \omega} F^n(\bot)\\
 & = & \bot_{\Sigma \to \Sigma_\bot}\\
 & = & \lambda \sigma \in \Sigma . \bot \\
\end{eqnarray*}

As we begin to construct a proof that this denotation is correct, we
want to show that this limit, or LUB, is a least fixed point of $F$.
That is, we want to show that

\[ \bigsqcup_{n \in \omega} F^n(\bot) \]

is the least solution to

\[ x = F(x) \]

This will not be true for arbitrary $F$!  We need $F$ to be both
monotonic and continuous.

Consider a non-monotonic $F$:

\begin{eqnarray*}
F(x) & = & \IF\ x = \bot\ \THEN\ 1 \\
 & & \ELSE\ \IF\ x = 1\ \THEN\ \bot \\
 & & \ELSE\ \IF\ x = 0\ \THEN\ 0 \\
\end{eqnarray*}

Although 0 is clearly a fixed point of this $F$, $F^n(\bot)$
is not a chain (the elements cycle between $\bot$ and 1), and so
we cannot take the LUB of it.  Monotonicity would avoid this problem.

Monotonicity guarantees that the elements $F^n(\bot)$ are a chain
and hence that we can find a LUB. But it doesn't mean we have a fixed
point. Consider a monotonic but non-continuous $F$ defined over the
pointed CPO $(\mathbb{R} \cup \{-\infty,\infty\}, \leq)$:

\[ F(x) = \cond{x \lt 0}{\tan^{-1}(x)}{1} \]

\noindent
The least fixed point of this $F$ is 1.  However,

\begin{eqnarray*}
F^1(\bot) & = & \tan^{-1}(-\infty) = -\frac{\pi}{2}\\
F^2(\bot) & = & \tan^{-1}(-\frac{\pi}{2}) = -1 \\
F^2(\bot) & = & \tan^{-1}(-1) \approx -0.78 \\
\end{eqnarray*}

\noindent
For $x\lt 0$, $F(x) \gt x$ and $F(x) \lt 0$ :
$F^n(\bot)$ is a chain that
approaches 0 arbitrarily closely: its LUB is 0.
But $F(0) = 1$, so the LUB is not a fixed point! The least fixed
point of this monotonic function is actually $1 = F(1)$. The problem
with this function $F$ is that it is not continuous at $0$. In general,
we will look for some form of _continuity_ in $F$
for \nm{fix} to guarantee that the LUB formula gives us a (least) fixed point.

\section{Monotonicity and Continuity}
\paragraph{Definition:}
Let $(D,\sqleq)$ be a cpo, $F:D\to D$ a function. $F$
is \emph{monotonic} if
\[\forall x,y\in D\quad x\sqleq y\to F(x)\sqleq F(y).\]

\paragraph{Claim:}
If $(D,\sqleq,\perp)$ is a pointed cpo and $F:D\to D$ is
monotonic then the elements $F^n(\perp)$ form an increasing
chain in $D$:
\[\perp\sqleq F(\perp)\sqleq F^2(\perp)\sqleq\dots\]

\paragraph{Proof:}
Since $\perp$ is the least element of $D,$ we have
\[\perp\sqleq F(\perp).\]
Monotonicity of $F$ gives
\[\forall n\in\omega\quad F^n(\perp)\sqleq F^{n+1}(\perp)\Rightarrow
F^{n+1}(\perp)\sqleq F^{n+2}(\perp).\] The result
follows by induction.

\paragraph{}
Notice that if $F:D\to D$ is monotonic and $x_0\sqleq
x_1\sqleq x_2\sqleq\dots$ is a chain in $D,$ then
$F(x_0)\sqleq F(x_1)\sqleq
F(x_2)\sqleq\dots$ is also a chain in $D.$ This permits
the following definition.

\paragraph{Definition:}
Let $(D,\sqleq)$ be a cpo, $F:D\to D$ a monotonic function.
$F$ is \emph{continuous} if for every chain
\[x_0\sqleq x_1\sqleq x_2\sqleq\dots\]
in $D$, $F$ preserves the LUB operator:
\[\bigsqcup_{n\in\omega}F(x_n)\quad =
\quad F(\bigsqcup_{n\in\omega}x_n).\]

\section{The Fixed Point Theorem}

\paragraph{}
We will now show that the properties of monotonicity and
continuity allow us to compute the least fixed point as desired.

\paragraph{Claim:}
Let $(D, \sqleq)$ be a cpo, and let $F:D\to D$ be a
monotonic continuous function. Then $\bigsqcup_{n \in
\omega}F^{n}(\bot)$ is a fixed point of $F$.

\paragraph{Proof:}
By continuity of $F$,
\[F(\bigsqcup_{n \in \omega}F^{n}(\bot)) = \bigsqcup_{n \in
\omega}F(F^{n}(\bot))\] Applying $F$, \[= \bigsqcup_{n
\in \omega}F^{n+1}(\bot)\] Reindexing, \[ = \bigsqcup_{n =
1,2,...}F^{n}(\bot)\] By definition of $\bot$,
\[= \bot \: \sqcup \bigsqcup_{n =1,2,...}F^{n}(\bot)\]
And, finally, absorbing the join with $\bot$ into the big join, \[
= \bigsqcup_{n \in \omega}F^{n}(\bot)\]

\paragraph{}
We now know that monotonicity and continuity guarantee that
$\bigsqcup_{n \in \omega}F^{n}(\bot)$ is a fixed point of
$F$. We also want $\bigsqcup_{n \in \omega}F^{n}(\bot)$ to
be the \textit{least} fixed point of $F$.  To show this, we
must prove that $y=F(y) \Rightarrow \bigsqcup_{n \in
\omega}F^{n}(\bot) \sqleq y$. We can actually prove
something even stronger.

\paragraph{Definition:}
Let $(D, \sqleq)$ be a cpo, $F:D\to D$ a function. $x\in
D$ is a \emph{prefixed} point of $F$ if $F(x)\sqleq x.$

 \paragraph{}
Notice that every fixed point of $F$ is also a prefixed point.
As a consequence, if a fixed point of $F$ is the least
prefixed point of $F,$ it is also the least fixed point of
$F.$

\paragraph{Claim:}
Let $(D, \sqleq, \bot)$ be a pointed cpo. For any monotonic
continuous function, $F: D\to D, \: \bigsqcup_{n \in
\omega} F^{n}$ is the least prefixed point of $F$.

\paragraph{Proof:}
Suppose $y$ is a prefixed point of $F$.  By definition of
$\bot$,
\[\bot \sqleq y\]
Taking $F$ of both sides,
\[F(\bot)\sqleq F(y) \sqleq y\]
Inductively, for all $n \geq 0$,
\[F^{n}(\bot) \sqleq y\]
Because $y$ is an upper bound for all the $F^n(\bot)$, it must be
at least as large as their least upper bound:

\[ \bigsqcup_{n \in \omega}F^{n}(\bot) \sqleq y \]

We have now proven:
\paragraph{The Fixed Point Theorem:} Let $(D, \sqleq, \bot)$ be a pointed cpo. For any monotonic
continuous function, $F: D\to D, \: \bigsqcup_{n \in
\omega} F^{n}$ is the least fixed point of $F$.

\section{An instance of the FPT}

We have actually encountered an instance of the fixed-point
theorem before. Recall lecture 6, when we defined the set of all
elements derivable in some rule system to be the least fixed
point of the rule operator, $R$. Our proof in that case was an 
instantiation of the fixed point theorem on the CPO consisting of
all subsets of a set, ordered by set inclusion:

\[R = F\]

\[\emptyset = \bot\]

\[\bigcup = \bigsqcup\]

\[\subseteq = \sqleq\]

\noindent
The tricky part of the earlier proof corresponded to showing
that $R$ is a continuous operator, which was true because we only allow
inference rules with a finite number of premises.

\end{document}
