\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{geometry}
\usepackage{epsfig}
\usepackage{alg}
\usepackage{alltt}
\usepackage{stmaryrd}

\geometry{letterpaper, width=6.5in, height=9in, nohead}

\newtheorem{define}{Definition}
\newtheorem{propose}{Proposition}
\newtheorem{claim}{Claim}

\newcommand{\Be}[1]{\mathcal{B}_e \llbracket #1 \rrbracket}
\newcommand{\Bv}[1]{\mathcal{B}_v \llbracket #1 \rrbracket}

\begin{document}

\title{Equations for Binding-Time Analysis}
% \date{}

\maketitle

\section{A Problem with the Binding-Time Analysis in Jones, Gomard, and Sestoft \cite[\S5.2]{Book:JGS93}}

This document gives a modified set of equations for binding-time analysis
that aims to overcome certain problems with the equations given
by Jones, Gomard, and Sestoft \cite[\S5.2]{Book:JGS93}.  In particular,
their equations apparently do not account for a certain kind of
congruence issue that arises when a function that always has
static arguments calls a function that can have dynamic arguments.

Consider the following example:
\begin{alltt}
(
  (define (goal x y z)
    (if (= x 0)
        z
        (call auxiliary y x)
  ))
  (define (auxiliary x y)
    (call goal (- y 1) 2 x)
  )
)
\end{alltt}
where the input binding-time pattern is $(S,S,D)$.
The division given by the binding-time-analysis equations from \cite[\S5.2]{Book:JGS93}
is $[\mathtt{goal} \mapsto (S,S,D), \mathtt{auxiliary} \mapsto (S,S)]$.
Thus, the annotated program is
\begin{alltt}
(
 (define (goal (x y) (z))
   (ifs (=s x 0)
        z
        (lift (calls auxiliary (y x) ()))
 ))
 (define (auxiliary (x y) ())
   (calld goal ((-s y 1) 2) ((lift x)))
 )
)
\end{alltt}

The specializer calls
\begin{alltt}
 reduce[(ifs (=s x 0) z (lift (calls auxiliary (y x) ()))), names, values]
\end{alltt}
which spawns a call
\begin{alltt}
 reduce[(lift (calls auxiliary (y x) ())), names, values]
\end{alltt}
where the lift case of reduce is defined by
\begin{alltt}
 (lift e) => list[quote, reduce[e, names, values]]
\end{alltt}
Consequently, a quoted expression is introduced whose body is the result of
\begin{alltt}
  reduce[(calls auxiliary (y x) ()), names, values]
\end{alltt}
The \texttt{calls} case of reduce spawns a call to reduce on the body of function \texttt{auxiliary}.
However, because the body of \texttt{auxiliary} in the annotated program is
\begin{alltt}
  (calld goal ((-s y 1) 2) ((lift x)))
\end{alltt}
the result is a residuated call on a variant of \texttt{goal}.
For example, if the value of \texttt{y} is 5, the overall quoted
expression that would be created is
\begin{alltt}
  (quote (call goal-4:2 2))
\end{alltt}
When the specialized program is evaluated on some dynamic input,
if the interpreter encounters the above quoted expression, it would yield
the call expression (as an s-expression) instead of evaluating
the call expression.

The problem is that function \texttt{auxiliary} is always called
with static arguments (in this case \texttt{(call auxiliary y x)}),
which in annotated form is \texttt{(calls auxiliary (y x) ())});
however, the body of \texttt{auxiliary} has a call on a function
that has dynamic arguments, namely \texttt{goal}
(i.e., \texttt{(call goal (- y 1) 2 x)}, which in annotated form
is \texttt{(calld goal ((-s y 1) 2) ((lift x)))}).

There is a kind of congruence mismatch in this example: the (static)
context expects a pure value to be produced by the specializer,
whereas the presence of the dynamic operator \texttt{calld} causes a
residual expression to be produced.

\section{An Alternative Set of Binding-Time-Analysis Equations}

To address the problem described above, we now give a modified set of
equations for binding-time analysis below.
First, we redefine the types:

$$
\begin{array}{ccccc}
t  & \in & \mathrm{BindingTime} & = & \{S,D\} \\
\tau & \in & \mathrm{BTEnv} & = & \mathrm{BindingTime}^* \times \mathrm{BindingTime} \\
\tt{div} & \in & \mathrm{Monodivision} & = & \mathrm{FuncName} \to \mathrm{BTEnv}
\end{array}
$$

The main difference is the BTEnv type, which will now be a pair.  The
first element of the pair is a list of binding times, which correspond
to the binding times of the formals of a function; the second element
is the binding time of the function's result.  Because we
are using monovariant divisions, the environment for the formals should
be greater than or equal to the binding times for all calls made to the
function, and the return binding time for each call made to the
function should be greater than or equal to the function's result
binding time.

Next, we redefine the less-than-or-equal-to operators for binding
times and binding-time environments.
For binding times $t$ and $r$ we have

$$
t \sqsubseteq r \textrm{ iff } t=S \textrm{ or } t=r.
$$

For environments $\tau=((t_1,\ldots,t_a),t)$ and $\rho=((r_1,\ldots,r_a),r)$ we have
$$
\tau \sqsubseteq \rho \textrm{ iff } t_i \sqsubseteq r_i \textrm{ for all } i=1,\ldots,a \textrm{ and } t \sqsubseteq r.
$$

Then, the least-upper-bound operations for these types are as follows.
For binding times $t$ and $r$ we have:
$$
t \sqcup r = \left\{
\begin{array}{cc}
  t & \textrm{ if } r \sqsubseteq t \\
  r & \textrm{ otherwise}
\end{array}\right.
$$

For environments $\tau$ and $\rho$ as above we have
$$
\tau \sqcup \rho = ((s_1, \ldots, s_a), s),
$$
where $s_i = t_i \sqcup r_i$ and $s = t \sqcup r$.

Now we define the new analysis functions.
The function $\Be{e}~\mathtt{div}~\mathtt{h}$ gives the binding time of
expression $e$ (an expression that occurrs in function $\mathtt{h}$)
with respect to division $\mathtt{div}$.
\[
  \Be{\mathtt{e}} : \mathrm{Monodivision} \to \mathrm{FuncName} \to \mathrm{BindingTime}
\]
The main change we make with respect to the definition of Jones et al.\ \cite[\S5.2]{Book:JGS93}
is in the \texttt{call} rule:
a call is dynamic if any of its actual parameters is dynamic or the
binding time for the function result is dynamic.

$$
\begin{array}{lcl}
  \Be{\mathtt{c}}~\mathtt{div}~\mathtt{h} & = & S \\
  \Be{\mathtt{x}_j}~\mathtt{div}~\mathtt{h} & = & t_j \text{ where } \mathtt{div}~\mathtt{h} = ((t_1,\ldots,t_a),t) \\
  \Be{\mathtt{(if \; e_1 \; e_2 \; e_3)}}~\mathtt{div}~\mathtt{h} & = & \Be{\mathtt{e_1}}~\mathtt{div}~\mathtt{h} \sqcup \Be{\mathtt{e_2}}~\mathtt{div}~\mathtt{h} \sqcup \Be{\mathtt{e_3}}~\mathtt{div}~\mathtt{h} \\
  \Be{\mathtt{(call \; f \; e_1 \; \ldots \; e_a)}}~\mathtt{div}~\mathtt{h} & = & (\bigsqcup_{j=1}^a \Be{\mathtt{e_j}}~\mathtt{div}~\mathtt{h}) \sqcup t \text{ where } (\mathtt{div} \; \mathtt{f}) = (\_,t) \\
  \Be{\mathtt{(op \; e_1 \; \ldots \; e_a)}}~\mathtt{div}~\mathtt{h} & = & \bigsqcup_{j=1}^a \Be{\mathtt{e_j}}~\mathtt{div}~\mathtt{h}
\end{array}
$$

Next, we define $\Bv{\mathtt{e}}~\mathtt{div}~\mathtt{h}~\mathtt{g}$ which collects the binding-time
descriptions for function $\mathtt{g}$ in expression $\mathtt{e}$, where $\mathtt{e}$ occurs in $\mathtt{h}$.
\[
  \Bv{\mathtt{e}} : \mathrm{Monodivision} \to \mathrm{FuncName} \to \mathrm{FuncName} \to \mathrm{BTEnv}
\]
Again, the main change we make is in the \texttt{call} rule. The
notation $(\bigsqcup u)$ is shorthand for $\bigsqcup_{j=1}^a u_j$, where
$u = (u_1,\ldots,u_a)$.
$(p \uparrow 2)$ selects the second element of a pair $p$.

$$
\begin{array}{lcl}
  \Bv{\mathtt{c}}~\mathtt{div}~\mathtt{h}~\mathtt{g} & = & ((S,\ldots,S),S) \\
  \Bv{\mathtt{v}}~\mathtt{div}~\mathtt{h}~\mathtt{g} & = & ((S,\ldots,S),S) \\
  \Bv{\mathtt{(if \; e_1 \; e_2 \; e_3)}}~\mathtt{div}~\mathtt{h}~\mathtt{g} & = & 
             \Bv{\mathtt{e_1}}~\mathtt{div}~\mathtt{h}~\mathtt{g} \sqcup \Bv{\mathtt{e_2}}~\mathtt{div}~\mathtt{h}~\mathtt{g} \sqcup \Bv{\mathtt{e_3}}~\mathtt{div}~\mathtt{h}~\mathtt{g} \\
  \Bv{\mathtt{(call \; f \; e_1 \; \ldots \; e_a)}}~\mathtt{div}~\mathtt{h}~\mathtt{g} & = &
     \left\{\begin{array}{ll}
       (t, s)                                                         & \text{ if } \mathtt{f \neq g} \\
       (t,s) \sqcup (u, ((\mathtt{div}~g)\uparrow 2) \sqcup (\bigsqcup u)) & \text{ if } \mathtt{f = g}
     \end{array}\right. \\
  {} & {} & \text{where } \left\{\begin{array}{lcl}
                            (t,s) & = & \bigsqcup_{j=1}^a \Bv{\mathtt{e_j}}~\mathtt{div}~\mathtt{h}~\mathtt{g} \\
                            u     & = & (\Be{\mathtt{e_1}}~\mathtt{div}~\mathtt{h}, \ldots, \Be{\mathtt{e_a}}~\mathtt{div}~\mathtt{h})
                          \end{array}\right. \\
  \Bv{\mathtt{(op \; e_1 \; \ldots \; e_a)}} & = & \bigsqcup_{j=1}^a \Bv{e_j}~\mathtt{div}~\mathtt{h}~\mathtt{g}
\end{array}
$$

Finally, we define the equation for $\mathtt{div}$ as follows:
\[
  \mathtt{div}~\mathtt{g} =
       \left\{\begin{array}{ll}
         (\bigsqcup_{i=1}^n \Bv{\mathtt{e_{f_i}}}~\mathtt{div}~\mathtt{f}_i~\mathtt{g}) \sqcup (\text{\tt goal-args},D) & \text{if } \mathtt{g} \text{ is the goal function} \\
         (\bigsqcup_{i=1}^n \Bv{\mathtt{e_{f_i}}}~\mathtt{div}~\mathtt{f}_i~\mathtt{g}) \sqcup ((S,\ldots,S), \Be{\mathtt{e}_\mathtt{g}}~\mathtt{div}~\mathtt{g}) & \text{otherwise}
       \end{array}\right.
\]
where $\mathtt{e_f}$ denotes the body of function $\mathtt{e_f}$ and
$\text{\tt goal-args}$ denotes the binding-time pattern of the goal function's
static arguments.
The terms of the form $((S,\ldots,S), \Be{\mathtt{e}_\mathtt{g}}~\mathtt{div}~\mathtt{g})$
for each non-goal function $\mathtt{g}$ allow the binding time of $\mathtt{g}$'s
return value to account for the binding time of the body of $\mathtt{g}$.

Note that if in the least-fixed-point solution for $\mathtt{div}$ we have
$\mathtt{div}~\mathtt{goal} \sqsupset (\text{\tt goal-args},D)$, then some of the
arguments of the goal function that are intended to be static will have
to be supplied as dynamic arguments.

\bibliography{cs701}
\bibliographystyle{plain}

\end{document}
