\input{preamble}

\title{Resource Analysis: Problem Set 8}% \\ Type Inference and Unification}
\setcounter{section}{8}

\begin{document}
\maketitle

\begin{center}
  \noindent
  \large{Due before 1:30pm on Monday, April 11}
\end{center}

\subsection{(20 Points) Resource-Polymorphic Recursion}
\label{one}

In this problem we are interested in the number of cons operations. We
use a metric $M$
with $\cCons = 1$ and $M^K = 0$ for all $K \neq \text{cons}$.

Consider the following OCaml functions. 

\begin{lstlisting}
let rec rev_append (l1,l2) =
  match l1 with
  | [] -> l2
  | x::xs -> rev_append (xs, x::l2)

let rec skip l =
  match l with
    | [] -> []
    | x1::xs ->
      match xs with
	| [] -> []
	| x2::xs -> x2::skip(xs)

let rec f1 l =
  match l with
    | [] -> []
    | x::xs ->
	  let l' = skip l in
	  rev_append (f1 l', l')
\end{lstlisting}

\begin{enumerate}[a)]
\item Give linear resource-annotated types for the functions
  \code{rev\_append}, \code{skip}, and \code{f1}.
\item Provide annotated type derivations for the functions \code{skip}
  and \code{f1} that justify your types. Transform the functions to
  share-let-normal form for the type derivation.
\item Argue informally why the type inference algorithm that we
  discussed in class cannot derive a resource-annotated type for
  \code{f1}.
\item Now give a resource-annotated type derivation for \code{f2},
  which is defined below. Why can the type inference algorithm derive
  this bound?
\begin{lstlisting}
let rec f2 l =
  match l with
    | [] -> []
    | x::xs ->
	  let l' = skip l in
	  rev_append (l', f2 l')
\end{lstlisting}
\item Finally, consider again the original program in which we replace
  \code{skip} with the following implementation.
\begin{lstlisting}
let rec skip l =
  match l with
    | [] -> []
    | x1::xs ->
      match xs with
	| [] -> [x1]
	| x2::xs -> x2::skip(xs)
\end{lstlisting}
  Explain informally why your type derivation from (b) does not work
  for the new variant of \code{skip}. Can you informally derive a
  bound for \code{f1} with the new variant of \code{skip}?

\end{enumerate}




\subsection{(16 Points) Non-Negative Polynomials}

The potential functions of \emph{univariate polynomial amortized
  resource analysis} are a generalization of non-negative linear
combinations of binomial coefficients (binomials)
$$
\mathcal{B} = \left\{ \left.  \lambda \, n . \; \sum_{i=0,\ldots,k} q_i \binom{n}{i}   \;\; \right| \;\; k \in N,  q_i \in \Qplus \right\}
$$
Recall that $\binom{n}{0} = 1$ for every $n \in \N$.

For a function $f : \N \to \Qplus$,
the \emph{discrete derivative} $\Delta f : \N \to \Qplus$
is defined by
$$
(\Delta f)(n) = f(n+1) - f(n) \; .
$$
As usual, we define $\Delta^0 f = f$ and $\Delta^k f = \Delta (\Delta^{k{-}1} f)$ if $k>0$.

The set of polynomials is defined as
$$
\mathcal{P} = \left\{ \left.  \lambda \, n . \; \sum_{i=0,\ldots,k} q_i n^i   \;\; \right| \;\; k \in N,  q_i \in \Q \right\}
$$

We call a function $f : \N \to \Q$ \emph{hereditary non-negative} if $\Delta^i f \geq 0$ for all $i \in \N$.


\begin{enumerate}[a)]
\item Prove that if $f \in \mathcal{B}$
  and $k \in N$
  then $\Delta^k f \in \mathcal{B} $.
  Note that it follows that $\mathcal{B}$
  is a set of hereditary non-negative polynomials.

\item Show that $\mathcal{B}$
  is the largest set of hereditary non-negative polynomials:
  $\mathcal{P} \cap \{f \mid \forall i \; \Delta^i f \geq 0 \} =
  \mathcal{B}$. 

  Hint: If $p : \N \to \Q$
  is a hereditary non-negative polynomial then
  $p(n) = \sum_i q_i \binom{n}{i}$ where $q_i = (\Delta^i p)(0)$.

\item Let $\mathcal{C}$
  be a set of non-negative polynomials that is closed under discrete
  differentiation, that is,
  $p \in \mathcal{C} \implies \Delta p \in \mathcal{C}$.
  Show that $\mathcal{C} \subseteq \mathcal{B}$.
\end{enumerate}

\subsection{(12 Points) Resource Aware ML}

Resource Aware ML (RAML) is an implementation of \emph{multivariate
polynomial amortized resource analysis} for OCaml. A web interface for RAML is available at
\begin{center}
   \url{http://raml.co} 
\end{center}

\begin{enumerate}
\item Use the web interface of RAML to derive evaluation-step bounds
  on the functions defined in Problem~\ref{one} and report the derived
  bounds.
\item Use the template in the file \code{search.raml} and the
  functional queue from Problem~2.3 to implement a breadth-first
  search and a depth-first search in RAML. Derive and report
  evaluation-step bounds using the web interface.
\end{enumerate}

\input{linear_types}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% mode: flyspell
%%% TeX-master: t
%%% End:
