\documentclass[11pt]{article}
\usepackage{ifthen}
\usepackage{alltt}
% \usepackage{indentfirst}
\usepackage{latexsym}
\usepackage[pdftex]{graphicx}
\usepackage{gdefs}
% \usepackage{logdef}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{rotating}
\usepackage{longtable}
\usepackage{hhline}
\usepackage{refs}
\usepackage{graph}
\usepackage{changebar}
\usepackage{array}
\usepackage{wrapfig}
\usepackage[small,compact]{titlesec}

\usepackage{tweaklist}
% this sets the spacings in the itemize environment
\renewcommand{\itemhook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

% this sets the spacings in the enumerate environment
\renewcommand{\enumhook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

% this sets the spacings in the description environment
\renewcommand{\descripthook}{\setlength{\topsep}{0pt}%
  \setlength{\itemsep}{0pt}%
  \setlength{\parsep}{0ex}%
}

\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0.0in}
\setlength{\headsep}{0.0in}
\setlength{\textwidth}{6.5in}
\setlength{\oddsidemargin}{0.0in}
\setlength{\evensidemargin}{0.0in}
\setlength{\textheight}{9.0in}

\setcounter{topnumber}{5}
\def\topfraction{0.99}
\def\textfraction{0.05}
\def\floatpagefraction{0.9}
\setcounter{bottomnumber}{5}
\def\bottomfraction{0.99}
\setcounter{totalnumber}{10}
\def\dbltopfraction{0.99}
\def\dblfloatpagefraction{0.8}
\setcounter{dbltopnumber}{5}

\setlength{\marginparwidth}{.95in}
\let\oldmarginpar\marginpar
\renewcommand\marginpar[1]{\-\oldmarginpar[\raggedleft\footnotesize #1]%
{\raggedright\footnotesize #1}}

\let\oldthebibliography=\thebibliography
  \let\endoldthebibliography=\endthebibliography
  \renewenvironment{thebibliography}[1]{%
    \begin{oldthebibliography}{#1}%
      \setlength{\parskip}{0ex}%
      \setlength{\itemsep}{0ex}%
  }%
  {%
    \end{oldthebibliography}%
  }
\newcommand{\pf}{\textit{pf}}
\newcommand{\spf}{\textit{spf}}
\newcommand{\apf}{\textit{apf}}
\newcommand{\SM}{\textit{SM}}
\newcommand{\SSSD}{\textit{SSSD}}
\newcommand{\ESSSD}{\textit{ESSSD}}
\newcommand{\EQ}{\textit{EQ}}
\newcommand{\CS}{\textit{CS}}
\newcommand{\SCS}{\textit{SCS}}
\newcommand{\ECS}{\textit{ECS}}
\newcommand{\EAS}{\textit{EAS}}
\newcommand{\EFAS}{\textit{EFAS}}
\newcommand{\PT}{\textit{PT}}
\newcommand{\APS}{\textit{APS}}
\DeclareMathOperator{\ulambda}{\underline{\lambda}}
\DeclareMathOperator{\op}{\textrm{op}}
\DeclareMathOperator{\extend}{\otimes}
\DeclareMathOperator{\combine}{\oplus}
\DeclareMathOperator*{\bigcombine}{\bigoplus}
\DeclareMathOperator*{\Min}{\textrm{\large{Min}}}
\newcommand{\bbN}{\mathbb{N}}
\newcommand{\bbZ}{\mathbb{Z}}
\newcommand{\len}{\textrm{len}}
\newcommand{\weight}{\textrm{weight}}
\newcommand{\pathWeight}{\textrm{pathWeight}}
\newcommand{\Nodes}{\textrm{Nodes}}
\newcommand{\Edges}{\textrm{Edges}}
\newcommand{\Paths}{\textrm{Paths}}
\newcommand{\Store}{\textrm{Store}}
\newcommand{\AStore}{\textrm{AStore}}
\newcommand{\Var}{\textrm{Var}}
\newcommand{\Val}{\textrm{Val}}

\title{Formulating the Collecting Semantics and Abstract Semantics \\
       as Path Problems \\
       CS701
}

\author{Thomas Reps \\ \small[Based on notes taken by Morgan Zhang on October 13, 2015]}
\date{}

\begin{document}           % End of preamble and beginning of text.

\maketitle
\begin{abstract}
This lecture discusses standard semantics, collecting semantics, and
abstract semantics for a flowchart language and the control flow
graphs (CFGs) that represent programs in the language.
\end{abstract}


\section{Two Formulations of Path Problems}
\label{Se:TwoFormulationsOfPathProblems}

We start our discussion by reviewing the two formulations of
the Single Source Shortest Distance ($\SSSD$) problem.\footnote{
  See also the notes from the previous lecture.
}
Similar path problems, each with a pair of formulations, will be used
shortly to define the standard semantics, collecting semantics, and
abstract semantics of a flowchart language---and in particular, the
semantics of the control flow graphs (CFGs) that represent programs in
the language.

\subsection{Declarative Formulation}

Let $p = e_1,e_2, \ldots, e_k$ be a path from $s$ to $n$, and let $l(e_i)$ be the
length of the edge $e_i$.
Then the length of $p$, denoted by $\len(p)$, is defined as
\[
  \len(p) \eqdef l(e_1) + l(e_2) + \ldots + l(e_k),
\]
and the set of $\SSSD$ values is defined as follows:
\begin{equation}
  \label{Eq:SSSD}
  \SSSD[s,n] \eqdef \Min_{p\in \Paths(s,n)} \len(p).
\end{equation}

The $\SSSD$ problem is an example of a \emph{path problem}.
In general, a path problem is formulated in terms of two
binary operators, \emph{extend} (denoted by $\extend$),
which is used to define the cost of a path (also known as the
length or weight of the path), and \emph{combine}
(denoted by $\combine$), which is used to
combine information at a node from two or more incoming paths.
In the case of the $\SSSD$ problem, $\extend$ is +, and $\combine$
is $\min$.\footnote{
  More precisely, $\extend$ is an infix operator for the function
  $\lambda x.\lambda y.(x + y)$, and $\combine$ is an infix operator
  for the function $\lambda x.\lambda y. \min(x, y)$.
}
In general, a path problem $Q$ is defined in terms of a \emph{weight domain}
$(W,\extend,\combine)$ and a graph $G = (\Nodes,\Edges,s)$, where $s$ denotes
the start node, along with a function $\weight: \Edges \rightarrow W$ that
assigns a weight from $W$ to each edge in $G$.
Then the path-weight for a path $p = e_1,e_2, \ldots, e_k$ in $G$ is
defined as follows:
\[
  \pathWeight(p) \eqdef \weight(e_1) \extend \weight(e_2) \extend \ldots \extend \weight(e_k).
\]
Finally, we define the $Q$ value for $n$ (with respect to start node $s$)
as follows:
\begin{equation}
  \label{Eq:PathProblemDeclarative}
  Q[s,n] \eqdef \bigcombine_{p\in \Paths(s,n)} \pathWeight(p).
\end{equation}

\subsection{Equational Formulation}

The \emph{equational formulation} of a path problem adopts the local viewpoint
depicted below

\begin{center}
  \includegraphics[width=11.0cm]{figures/esssd.eps}
\end{center}

\noindent
and sets up the following collection of equations:
\begin{equation}
  \label{Eq:ESSSD}
  \ESSSD[s,n] =
  \begin{cases}
    0 & \textrm{if}~n = s \\
    {\displaystyle \Min_{\B{m,n} \in \Edges}} \ESSSD[s,m] + l(m,n) & \textrm{otherwise}
\end{cases}
\end{equation}

\begin{The}
For all $n$, $\ESSSD[s,n] = \SSSD[s,n]$.
\end{The}

This theorem can either be proved directly (hint: use induction on the length of paths
considered in computing $\Paths(s,n)$ in in \eqref{SSSD}),
or as an instance of a more general theorem about path problems.
The equational version of a path problem $Q$, which we will denote by $\EQ$,
is the analog of \eqref{ESSSD};
it is defined using the following set of equations:
\begin{equation}
  \label{Eq:EQ}
  \EQ[s,n] =
  \begin{cases}
    0 & \textrm{if}~n = s \\
    {\displaystyle \bigcombine_{\B{m,n} \in \Edges}} \EQ[s,m] + \weight(m,n) & \textrm{otherwise}
\end{cases}
\end{equation}
Note that the weight domain $W$ is typically a partially ordered set.
When the orientation of $W$ is the one typically used in the
abstract-interpretation community, we seek the \emph{least} solution to \eqref{EQ}.
When the orientation of $W$ is the one typically used in the
dataflow-analysis community, we seek the \emph{greatest}
solution to \eqref{EQ}.
However, by duality, the orientation does not matter for the purpose
of stating the following theorem:

\begin{The}
If $\extend$ distributes over $\combine$, then for all $n$, $\EQ[s,n] = Q[s,n]$.
\end{The}

Again, the theorem can be proved using induction on the length of paths
considered in computing $\Paths(s,n)$ in
\eqref{PathProblemDeclarative}.

\section{Standard and Collecting Semantics}

\begin{figure*}
\begin{center}
  \includegraphics[width=14.0cm]{figures/cfg.eps}
\end{center}
\vspace*{-2.0ex}
\caption{Example control-flow graph.}
\label{Fi:CFG}
\end{figure*}

We will consider the CFG example from the previous lecture (see \figref{CFG}).
We first define some notation that we will be using in subsequent
sections.
\[
  \Store \eqdef (\Var \rightarrow (\mathbb{Z}\cup \{?\}))_\bot
\]
The function $M$ assigns a $\Store$-transformation function to each
edge in $G$:
\[
  M: \Edges \rightarrow (\Store\rightarrow \Store)
\]
  \begin{equation*}
    M(\B{m,n}) \eqdef
    \begin{cases}
      \ulambda\sigma.\sigma[x\leftarrow E\semp{e}\sigma]  & \text{if $m$'s label is \texttt{x:=e}} \\
      \ulambda\sigma.B\semp{b}\sigma ? s : \bot  & \text{if $m$'s label is ``\texttt{if} $b$'' and $\B{m,n}$ is the True branch} \\
      \ulambda\sigma.B\semp{b}\sigma ? \bot : s  & \text{if $m$'s label is ``\texttt{if} $b$'' and $\B{m,n}$ is the False branch}
    \end{cases}
  \end{equation*}
  
\subsection{Standard Semantics}

Let $p = e_1, e_2, \ldots ,e_k$ be a path from s to n;
then $p$'s \emph{path transfer function} is
\[
  \pf_p = M(e_k)\circ \ldots \circ M(e_2)\circ M(e_1) = M(e_1)\extend M(e_2) \extend \ldots \extend M(e_k),
\]
where $\extend$ is the reverse of function composition.

\subsection{Collecting Semantics}

\subsubsection{Declarative Formulation}

Given an initial $\Store$ and a path $p$ to node $n$, the standard
semantics produces a $\Store$ at $n$, but abstract interpretation is
concerned with determining information about \emph{sets} of $\Store$s.
To formalize such sets, we define the \emph{collecting semantics}.
The collecting semantics at node $n$, which we will denote by
$\CS[s,n]$, is defined to be the set of all $\Store$s that can
possibly arise at $n$ during \emph{some} execution of the program
(starting from start node $s$).
\begin{equation}
  \label{Eq:CS}
  \CS[s,n] = \{ \pf_p(\sigma) \mid p \in \Paths(s,n), \sigma \in \Store \}.
\end{equation}
Note that the condition ``$\sigma \in \Store$'' means that \emph{any}
$\Store$ can be fed in at the beginning of execution.

The collecting semantics deliberately loses information about
\begin{itemize}
  \item
    \emph{whether} two $\Store$s, $\sigma_1,\sigma_2 \in \CS[s,n]$,
    \emph{can both arise} during the execution of the program on some
    initial store $\sigma_0 \in \Store$
  \item
    the \emph{order} in which two $\Store$s, $\sigma_1,\sigma_2 \in \CS[s,n]$ arise at $n$,
    if $\sigma_1$ and $\sigma_2$ can both arise during the execution
    of the program on some initial store $\sigma_0 \in \Store$.
\end{itemize}

\eqref{CS} is a start;
however, we have not succeeded in formulating the collecting semantics
as a path problem in the sense of \sectref{TwoFormulationsOfPathProblems}:
\eqref{CS} uses set comprehension rather than ``combine-over-all-paths.''
Instead of \eqref{CS}, we want to express $\CS$ so that it has the
following form:
\begin{equation}
  \label{Eq:form}
  \CS[s,n] = \bigcombine_{p \in \Paths(s,n)} F(p),
\end{equation}
where $F$ is some function on path $p$ that uses $\extend$.

Let us define some notation similar to that used for defining the
standard semantics.
$\SM$ is $M$ lifted to sets.
\[
  \SM: \Edges \rightarrow (\mathcal{P}(\Store)\rightarrow \mathcal{P}(\Store)),
\]
where $\mathcal{P}(\Store)$ denotes the powerset of $\Store$.
The function $\SM$ assigns to each edge in $G$
a function that transforms a set of $\Store$s to
a set of $\Store$s:
\[
  \SM(\B{m,n}) \eqdef \lambda S.(\{M(\B{m,n})(\sigma) \mid \sigma \in S\} - \{\bot\})
\]
(We subtract out $\{ \bot \}$ in the right-hand side expression
because $\bot$ is not a proper $\Store$.)
Then $p$'s \emph{setwise path transfer function} is 
\[
  \spf_p \eqdef \SM(e_k) \circ \ldots \circ \SM(e_2) \circ \SM(e_1) = \SM(e_1) \extend \SM(e_2) \extend  \ldots \extend \SM(e_k),
\]
where $\circ$ denotes function composition, and $\extend$ is the reversal of $\circ$.
Combine ($\combine$) is just $\cup$.
Then we define the \emph{setwise collecting semantics} as follows:
\begin{equation}
  \label{Eq:SCS}
  \SCS[s,n] \eqdef \bigcup_{p\in \Paths(s,n)} \spf_p(\Store).
\end{equation}
In contrast to \eqref{CS}, \eqref{SCS} has the form that we desire (cf.\ \eqref{form}).

\subsubsection{Equational Formulation}

We now turn to an equational formulation of the collecting semantics, which we will denote
by $\ECS[s,n]$.
$\ECS: \Nodes \rightarrow \mathcal{P}(\Store)$, where
\begin{equation}
  \label{Eq:ECS}
  \ECS[s,n] \eqdef
  \begin{cases}
    \Store & \text{if $n = s$} \\
    {\displaystyle \bigcup_{\B{m,n}\in \Edges}} \SM(\B{m,n})(\ECS[s,m]) & \text{otherwise}
  \end{cases}
\end{equation}

\begin{figure*}
\begin{center}
  \includegraphics[width=\textwidth]{figures/collecting-semantics-example.eps}
\end{center}
\vspace*{-3.0ex}
\caption{Table of the values that arise when \eqref{ECS}, instantiated for the
  CFG shown in \figref{CFG}, is solved by the method of successive approximations.
}
\vspace*{1.0ex}
\label{Fi:CollectingSemanticsExample}
\end{figure*}

The result of computing $\ECS$ is shown in \figref{CollectingSemanticsExample}.
Let's work through one entry of the table from \figref{CollectingSemanticsExample}.
Suppose that we are computing $\ECS$ for node 4 at iteration 5:
\[
  \ECS(4) = \SM(\B{3,4})(\ECS(3)) \cup \SM(\B{5,4})(\ECS(5))
\]
From the previous iteration, we know that $\ECS(3)=\{(1??)\}$ and $\ECS(5)=\{(11?)\}$. \\
Then
\begin{flalign*}
  \SM(\B{3,4})(\ECS(3)) &= \SM(\B{3,4})(\{1??\}) = \{M(\B{3,4})(\sigma) \mid \sigma\in \{(1??)\}\}-\{\bot\}&&\\ 
  &=\{M(\B{3,4})((1??))\} = \{\lambda s.s[b\leftarrow 1]((1??))\}&&\\
  &=\{(11?)\} 
\end{flalign*}
Similarly, we have $\SM(\B{5,4})(ECS(5)) = \{(21?)\}$.
Thus, at iteration 5, we obtain
$
  \ECS(4) = \{(11?)\} \cup \{(21?)\} = \{(11?)(21?)\}.
$

\section{Abstract Semantics (where \# comes in)}

So far we have been defining variants of a program's concrete semantics.
We are now going to define the program's abstract semantics.
We again start by introducing appropriate notation.
The items of notation defined below correspond to those
used for discussing the program's concrete semantics.

The function $\A{M}$ assigns to each edge in $G$
a function that transforms an $\AStore$ to an
$\AStore$:
\[
  \A{M}: \Edges \rightarrow (\AStore \rightarrow \AStore).
\]
$\AStore$ stands for ``abstract Store.''
An abstract store in $\AStore$ denotes a set of concrete stores.
In many problems, $\AStore$ has the following type, for
an appropriate space of abstract values $\A{\Val}$.
\[
  \AStore: \Var \rightarrow \A{\Val}.
\]
Each $\A{v} \in \A{\Val}$ represents a set of concrete values.

We assume that we have in hand sound abstract-interpretation functions
for our language's Boolean expressions (denoted by $\A{B}\semp{\cdot}$)
and arithmetic expressions (denoted by $\A{E}\semp{\cdot}$).
(One way to obtain such functions is by the method of syntax-directed
described in \S2.5.1 of the Oct.\ 6, 2015 lecture.)
If node $m$'s label is \texttt{x:=e}, we define $\A{M}(\B{m,n})$
as follows:
\begin{equation*}
  \A{M}(\B{m,n}) = \lambda a.
  \begin{cases}
    \bot & \text{if}~a = \bot \\
    a[x\leftarrow \A{E}\semp{e}(a)] & \text{otherwise}
  \end{cases}
\end{equation*}
If $m$'s label is ``\texttt{if} $b$'' and edge $\B{m,n}$ is the True branch,
we define $\A{M}(\B{m,n})$ as follows:
\begin{equation*}
  \A{M}(\B{m,n}) = \lambda a.
  \begin{cases}
    \bot & \text{if a=}\bot \\
    a    & \text{if }\A{B}\semp{b}(a)=True \\
    \bot & \text{if }\A{B}\semp{b}(a)=False \\
    a    & \text{if }\A{B}\semp{b}(a)=\top \text{ (don't know T or F; prepare both ways )}
  \end{cases}
\end{equation*}
If $m$'s label is ``\texttt{if} $b$'' and edge $\B{m,n}$ is the False branch,
$\A{M}(\B{m,n})$ is defined similarly.

\subsection{Declarative Formulation}

The \emph{abstract path transfer} function for a path $p = e_1,e_2, \ldots, e_k$ is
defined as follows:
\[
  \apf_p \eqdef \A{M}(e_k) \circ \ldots \circ \A{M}(e_2)\circ \A{M}(e_1)
    = \A{M}(e_1)\extend \A{M}(e_2) \extend \ldots \extend \A{M}(e_k),
\]
where $\extend$ is the reverse of function composition.
The \emph{abstract path semantics}, denoted by $\APS[s,n]$, is defined as follows:
\[
  \APS[s,n] \eqdef \bigcombine_{p\in \Paths(s,n)} \apf_p(\AStore)
\]
where $\bigcombine$ is the combine operator of the abstract domain.

\subsection{Equational Formulation}

Finally, we define the \emph{equational abstract semantics},
denoted by $\EAS[s,n]$, as follows:
\begin{equation}
  \label{Eq:EAS}
  \EAS[s,n] \eqdef
  \begin{cases}
    \top                                           & \text{if $n = s$} \\
    {\displaystyle \bigcombine_{\B{m,n}\in \Edges}} \A{M}(\B{m,n})(\EAS[s,m]) & \text{otherwise}
  \end{cases}
\end{equation}
where $\top$ is the value of $\AStore$ that represents all stores in $\Store$.

\begin{figure*}
\begin{center}
  \includegraphics[width=\textwidth]{figures/abstract-semantics-example.eps}
\end{center}
\vspace*{-3.0ex}
\caption{Table of the values that arise when \eqref{EAS}, instantiated for the
  CFG shown in \figref{CFG}, is solved by the method of successive approximations
  (together with the $\ECS$ values from \figref{CollectingSemanticsExample}):
}
\vspace*{1.0ex}
\label{Fi:AbstractSemanticsExample}
\end{figure*}

The result of computing $\EAS$ for the CFG from \figref{CFG}, together
with the previous $\ECS$ values from
\figref{CollectingSemanticsExample}, is shown in
\figref{AbstractSemanticsExample}.

{\small
  \bibliography{cs701}
  \bibliographystyle{plain}
}

\end{document}
