\input{preamble}

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

\begin{document}
\maketitle

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

\subsection{Univariate Polynomial Amortized Analysis (12 Points)}

Recall that the type rules of univariate polynomial amortized resource
analysis are similar to the rules for the linear type system. The main
difference are the rules for pattern matching and constructing
lists. All rules can be found in \pref{fig:trulesUni}.

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

let rec partition ((y : int),l) =
  match l with
  | [] -> ([],[])
  | x::xs ->
     let (cs,bs) = partition (y,xs) in
     if y<x then
       (cs,x::bs)
     else
       (x::cs,bs)
	
let rec quicksort l =
  match l with
  | [] -> []
  | x::xs ->
     let (ys,zs) = partition (x,xs) in
     append (quicksort ys, x :: (quicksort zs))
\end{lstlisting}

Consider the implementation of quick sort that is given above. We are
again interested in the number of cons operations that are performed
during an evaluation, that is, we use a metric $M$
with $\cCons = 1$ and $M^K = 0$ for all $K \neq \text{cons}$.

\begin{enumerate}[a)]
\item Provide resource annotated types for the functions
  \code{append}, \code{partition}, and \code{quicksort}.
\item Give a type derivation for \code{quicksort}. (You don't have to
  derive the types for \code{partition} and \code{append}.)
\end{enumerate}

\subsection{Multivariate Resource Polynomials (18 Points)}

Recall that multivariate resource polynomials $\res(A)$ for values of
type $A$ are non-negative linear combinations of base polynomials
$\atoms(A)$ of type $A$. Base polynomials are defined as follows.
\begin{eqnarray*}
  \atoms(A) &=& \{ \lambda a \, . \, 1 \} \text{ if } A \text{ is an atomic type} \\
  \atoms(A_1 * A_2) &=& \{ \lambda (a_1,a_2) \,. \, p_1(a_1) \cdot p_2(a_2) \mid p_i \in \atoms(A_i) \} \\
  \atoms(L(A)) &=&  \big\{ \lambda [a_1,\ldots,a_n] \, . \,
      \sum_{1 \leq j_1 < \cdots < j_k \leq n} \, \prod_{1 \leq i \leq k} p_i(a_{j_i}) \mid k \in \N, p_i \in \atoms(A) \big\}
\end{eqnarray*}
To assign a unique name to each base polynomial we define the
\emph{index set} $\ind(A)$ to denote resource polynomials for a given
data type $A$.  Interestingly, but as I find coincidentally, $\ind(A)$
is essentially the meaning of $A$ with every atomic type replaced by
\unit.
\begin{eqnarray*}
  \ind(A) & = & \{\unitelement\} \text{ if } A \in \{\boolT\} \cup \tvars \\
  \ind(A_1 * A_2) & = & \{ (i_1,i_2) \mid i_1 \in \ind(A_1) \text{ and }
  i_2\in \ind(A_2) \} \\
  \ind(\li{B}) & = & \{ [i_1, \ldots, i_k] \mid k \geq 0 , i_j \in \ind(B) \}
\end{eqnarray*}
For each $i\in\ind(A)$, I define a base polynomial
$p_i\in\atoms(A)$ as follows: 
%
If $A \in \{\int,\bool,\unit\}$ then
$$p_\unitelement(a)  = 1 \, .$$
%
If $A = (A_1*A_2)$ is a pair type and $a = (a_1,a_2)$ then
$$p_{(i_1,i_2)}(a)  = p_{i_1}(a_1) \cdot p_{i_2}(a_2) \,.$$
%
If $A = \li{B}$ is a list and $\elts(a) = [a_1,\ldots,a_n]$ then
$$p_{[i_1,\ldots,i_m]}(a) = \sum_{1
    \leq j_1 < \cdots < j_m \leq n} p_{i_1}(a_{j_1}) \cdots
p_{i_m}(a_{j_m}) \, .$$


A \emph{type annotation} for a data type
$A$ is defined to be a family
%
$$Q_A = (q_i)_{i \in \ind(A)} \text{ with } q_i \in \Qplus \; .$$
%
An \emph{annotated data type} is a pair $(A,Q_A)$ of a data type $A$
and a type annotation $Q_A$.

Let $a \in \sem{A}$ be a value of type $A$.  Then the type annotation
$Q_A$ defines the \emph{potential}
%
$$\Phi(a{:}(A,Q_A)) = \sum_{i \in \ind(A)} q_i \cdot p_i(a) \; .$$ 
%

Prove the following statements.
\begin{enumerate}[a)]
\item If $p,p'\in\res(A)$ then $p+p'\in\res(A)$ and
  $p\cdot p'\in\res(A)$.
\item For every $p\in\res(A*A)$ there exists $p'\in\res(A)$ with
  $p'(a)=p(a,a)$ for all $a\in\sem A$.
\item  Let $a \in \sem{A}$ and $\ell \in \sem{\li{A}}$ be a list. Let
  furthermore $k\geq 0$ and let $i_0,\dots,i_k\in\ind(A)$ indexes for
  type $A$.  Then we have
  \begin{eqnarray*}
    p_{[i_0,i_1,\dots,i_k]}([]) &=& 0 \\
  p_{[i_0,i_1,\ldots,i_k]}(a \cons \ell) &=& p_{i_0}(a)\cdot p_{[i_1,\ldots, i_k]}(\ell) + p_{[i_0,i_1,\ldots, i_k]}(\ell)\; .
  \end{eqnarray*}
\end{enumerate}

\subsection{Solving Recurrence Relations with RAML}

Recall the recurrence relations from Problem~1.3.
\begin{enumerate}[a)]
\item $T(0) = 10$ and  $T(n) = T(n - 1) + 12n + 3$ for $n>0$

\item $T(m,n) = 0$ for $n<4 $and $T(m,n) = T(m,n-4) + 12 (m + \binom{m}{2})$ for $n\geq 4$

\item $T_1(0) = T_2(0) = 0$, $T_1(n) \geq T_2(n-1) + 20(n-1)$, and $T_2(n) \geq T_1(n-1) + 8(n-1)$ for $n>0$

\item $T(0) = T(1) = 0$, $T(2) = 1$, and $T(n) = T(\lceil n/2 \rceil) + T(\lfloor n/2 \rfloor) + n  - 1 $ for $n>0$
\end{enumerate}
Use RAML and the built-in tick metric to automatically find solutions
these recurrence relations.\\

\noindent
{\bf Hint:} The recurrence $T(0) = 10$ and $T(n) = T(n - 1) + 3$ for
$n>0$ can be encoded by the following program.
\begin{lstlisting}
let rec t l =
  match l with
  | [] -> Raml.tick(10.0)
  | x::xs -> let _ = Raml.tick(3.0) in t xs
\end{lstlisting}


\include{uni_types}
\end{document}

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