\input{preamble}

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

\begin{document}
\maketitle

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

\subsection{(8 Points) Resource Monoid}

Recall the definition of the resource monoid
$\monoid = (\Qplus \times \Qplus, \cdot)$ where
$$(q,q')\cdot(p,p') = \left\{ 
    \begin{array}{ll}
      (q + p - q', \hspace{0.4em} p') & \text{if } q' \leq p \\
      (q, \hspace{0.4em} p' + q'-p) & \text{if } q' > p
    \end{array}
  \right.
$$
Let $(q,q') = (r,r') \cdot (s,s')$. Prove the following statements.
%
\begin{enumerate}
  \item $q \geq r$ \text{ and } $q-q' = r - r' + s - s'$
  \item If $(p,p') = (\bar r,r') \cdot (s,s')$ and $\bar r \geq r$
    then $p \geq q$ and $p' = q'$
  \item If $(p,p') = (r,r') \cdot (\bar s,s')$ and $\bar s \geq s$
    then $p \geq q$ and $p' \leq q'$
  \item $(r,r') \cdot ((s,s') \cdot (t,t')) = ((r,r') \cdot (s,s')) \cdot (t,t')$
    % \item If $(p,p') = K \cdot (r,r')$ then $p-p' = r - r' + K$.
    % \item If $(p,p') = (t,t') \cdot (s,s')$ and $t - p'_1 \geq
    %   r - q'_1$ then $p - p' \geq q - q'$
    % \item If $(p,p') = (r,r') \cdot (p_2,p_2')$ and $p_2 - p'_2 \geq
    %   s - q'_2$ then $p - p' \geq q - q'$
\end{enumerate}

\subsection{(12 Points) Reasoning with the Cost Semantics}

Consider the metric $M_\text{app}$ that counts the number of function
applications, that is,
$$
\begin{array}{llll}
  M_\text{app}(\mathsf{app})& = & 1 \\
  M_\text{app}(K)& = & 0 & \text{if } K \neq \mathsf{app}
\end{array}
$$
Consider the function \code{omega}$: (X \to X) \to Y$ that is defined as follows.
\begin{lstlisting}
  let rec omega = fun x -> omega x in omega (fun x -> x)
\end{lstlisting}
Let $e_\text{omega}$ be the above expression. 

\begin{enumerate}[a)]
\item Prove that $\Nil; \heap \pdash{M} e_\text{omega} \bigsb{(n,0)}$ for every
$n \in \N$ and every heap $\heap$.
\item Prove that $\Nil; \heap \;\;\; \not\!\!\!\!\!\!\!\!\!\pdash{M} e_\text{omega} \bigs (\ell,\heap')$ for any
$\ell$ and $\heap'$.

\end{enumerate}


\subsection{(18 Points) Resource-Based Type Safety}

We will now use our effect-based cost semantics to show that
\emph{well-typed programs don't go wrong}: In a well-formed
environment, a well-typed expression will either evaluate to a value
of the right type or can make an infinite number of steps.

First, recall the definition of a \emph{well-typed} environment. We
write $\heap\vDash \ell \,{:}\,A\,$ to indicate that there exists a,
necessarily unique, semantic value $a\in\sem{A}$ so that
$\heap \vDash v \mapsto a \,{:}\, A\,$.
%
An environment $\env$ and a heap $\heap$ are \emph{well-formed} with
respect to a context $\Gamma$ if
$\heap\vDash \env(x)\,{:}\,\Gamma(x)\,$ holds for every
$x \in \dom{\Gamma}$.  We then write $\heap \vDash \env : \Gamma$.

The judgement $\heap \vDash v \mapsto a \,{:}\, A$ is defined by the
following rules. Recall that the rules have to be interpreted
coinductively.
%
\begin{mathpar}
 \Rule{V:TVar}
      { X \in \typeVars
      \\ \ell \in \dom{\heap}
      }
      {\heap \vDash \ell \mapsto \ell : X}

 % \Rule{V:Bool}
 %      { \heap(\ell) = b
 %      \\ b \in \{\trueS,\falseS\}
 %      }
 %      {\heap \vDash \ell \mapsto b : \boolT }

 \Rule{V:Nil}
      { 
      }
      {\heap \vDash \Null \mapsto [] : \liT{T}}

 \Rule{V:Cons}
      { \heap(\ell) = (\ell_1,\ell_2)
      \\ \heap \vDash \ell_1 \mapsto a_1
      \\ \heap \vDash \ell_2 \mapsto (a_2,\ldots,a_n) : \liT{T}
      }
      {\heap \vDash \ell \mapsto [a_1,\ldots,a_n] : \liT{T}
      }

 \Rule{V:Fun}
      { \heap(\ell) = \closure{\lambda x . e} {\env}
      \\ \exists \, \Gamma \, . \;  \heap \vDash \env : \Gamma \, 
         \land \, \Gamma \vdash \lambda x . e \tmono T_1 \to T_2
      }
      {\heap \vDash \ell \mapsto \closure{\lambda x . e} {\env} : \tstack \to T}

 % \Rule{V:Pair}
 %      {\heap(\ell) = (\ell_1,\ell_2)
 %      \\ \heap \vDash \ell_1 \mapsto a_1 : T_1
 %      \\ \heap \vDash \ell_2 \mapsto a_2 : T_2
 %      }
 %      {\heap \vDash \ell \mapsto (a_1,a_2) :  T_1 * T_2}
\end{mathpar}
%
In this problem assume that $\Me$ is the \emph{steps metric},
which counts the number of evaluation steps. We then have $\Me^K = 1$
for all constants $K$.

Prove the following theorem.  It is sufficient if you prove the
theorem for expressions of the form
$$
\begin{array}{llll}
  e &::=& x & x \\
  && \absS{x}{e} & \word{fun } x \to e\\
  && \appS{e_1}{e_2} & e_1\ e_2\\
  && \letS{e_1}{x}{e_2} & \word{let } x = e_1 \word{ in } e_2\\
  % && \trueS & \word{true}\\
  % && \falseS & \word{false}\\
  % && \ifS{e}{e_1}{e_2} & \word{if } e \word{ then } e_1 \word{ else } e_2
  % && \pairS{e_1}{e_2} & (e_1,e_2)\\
  % && \matchPS{e}{x_1}{x_2}{e'}
  %    & \word{let } (x_1,x_2) = e' \word{ in } e\\
  % && \nilS & \word{[]}\\
  % && \consS{e_1}{e_2} & e_1 \mathop{::} e_2\\
  % && \matchLS{e}{e_1}{x_1}{x_2}{e_2} 
  %    & \word{match } e \word{ with } \mid [] \to e_1 \mid x_1\mathop{::}x_2 \to e_2\\
  && \recS{f}{x}{e_f}{e} 
     & \word{let} \word{ rec } f x = e_f \word{ in } e
\end{array}
$$
\begin{theorem}[Type Safety]
  Let $\heap \vDash \env : \Gamma$, $\Gamma \tmono e : T$, and let $\Me$
  be the steps metric. Then
  \begin{itemize}
  \item there is an
  $n \in \N$ such that
  $\env; \heap \pdash{\Me} e \bigs (\ell,\heap') \mid (n,0)$,
  $\heap' \vDash \env : \Gamma$, and $\heap' \vDash \ell:T$
\item or $\env; \heap \pdash{\Me} e \bigsb{(m,0)}$ for every $m \in N$
\end{itemize}
\end{theorem}
A consequence of the theorem is that resource bounds on the number of
evaluation steps prove termination.

\paragraph{Hint:} The following lemma can be proved by induction on $n$.
\begin{lemma}
  Let $\heap \vDash \env : \Gamma$ and $\Gamma \tmono e : T$. If
  $\env; \heap \pdash{\Me} e \bigsb{(n,0)}$ then
  $\env; \heap \pdash{\Me} e \bigsb{(n+1,0)}$ or
  $\env; \heap \pdash{\Me} e \bigs (\ell,\heap') \mid (n+1,0)$ for a
  location $ell$ and an heap $\heap'$.
\end{lemma}


\begin{figure*}
\fbox{\parbox{\textwidth}{
$\env; \heap \pdash{M} e \bigse{(\ell,\heap')}{{(q,q')}}$ \hspace{1.5em}
In environment $\env$ and heap $\heap$, expression $e$ evaluates to $(\ell, \heap')$,\\
\mbox{} \hspace{7.8em} the watermark resource usage is $q$ and $q'$ resources are available afterwards.}}
\begin{center}
  \def \MathparLineskip {\lineskip=0.45cm}
  \begin{mathpar}
    \Rule{Ee:Var}
    {
    }
    {\env; \heap \pdash{M} x \bigse{(\ell,\heap)}{\cVar} }

    \Rule{Ee:Abs}
    {\heap' = \heap, \ell \mapsto \closure{\lambda x . e}{\env}
    }
    {\env; \heap \pdash{M} \absS{x}{e} \bigse{(\ell,\heap')}{\cAbs} }

    \Rule{Ee:App}
    {  \env; \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)}
    \\ \heap(\ell_1) = \closure{\lambda x. e}{\env'}
    \\ \env; \heap_1 \pdash{M} e_2 \bigse{(\ell_2,\heap_2)}{(q_2,q_3)} 
    \\ \env'[x \mapsto \ell_2]; \heap_2 \pdash{M} e \bigse{(\ell,\heap')}{(q_3,q_4)} 
    }
    {\env; \heap \pdash{M} \appS{e_1}{e_2} \bigse{(\ell,\heap')}
    {\cApp{}{\cdot}(q_0,q_1){\cdot}(q_2,q_3){\cdot}(q_3,q_4)} 
    }

    \Rule{Ee:Let}
    {  \env, \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \env[x\mapsto \ell_1], \heap_1 \pdash{M} e_2 \bigse{(\ell,\heap')}{(q_2,q_3)} 
    }
    {\env; \heap \pdash{M} \letS{e_1}{x}{e_2} \bigse{(\ell,\heap')}
    {\cLet{}{\cdot}(q_0,q_1){\cdot}(q_2,q_3)} 
    }

    \Rule{Ee:Nil}
    { \heap' = \heap, \ell \mapsto \Null
    }
    {\env; \heap \pdash{M} \nilS \bigse {(\ell,\heap')}{\cNil}
    }

    \Rule{Ee:Cons}
    {  \env; \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \env; \heap_1 \pdash{M} e_2 \bigse{(\ell_2,\heap_2)}{(q_2,q_3)}
    \\ \heap' = \heap_2, \ell \mapsto (\ell_1, \ell_2)
    }
    {\env; \heap \pdash{M} \consS{e_1}{e_2} \bigse{(\ell,\heap')}
     {\cCons}{\cdot}{(q_0,q_1)}{\cdot}{(q_2,q_3)}
    }

    \Rule{Ee:MatL1}
    { \env; \heap \pdash{M} e \bigse{(\ell,\heap')}{(q_0,q_1)} 
    \\ \heap'(\ell) = \Null
    \\ \env; \heap' \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_2,q_3)} 
    }
    {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}  
     \bigse{(\ell_1,\heap_1)}{\cMatL1{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ee:MatL2}
    { \env; \heap \pdash{M} e \bigse{(\ell,\heap')}{(q_0,q_1)} 
    \\ \heap'(\ell) = (\ell_1,\ell_2)
    \\ \env[x_1 \mapsto \ell_1, x_2 \mapsto \ell_2]; \heap' \pdash{M} e_2 \bigse{(\ell,\heap')}{(q_2,q_3)}
    }
    {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}
     \bigse{(\ell_1,\heap_1)}{\cMatL2{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ee:Rec}
    { \env' = \env[f \mapsto \ell_f]
    \\ \heap' = \heap,\ell_f \mapsto \closure{\lambda x . e_f}{\env'}
    \\ \env'; \heap' \pdash{M} e \bigse{(\ell',\heap'')}{(q,q')}
    }
    {\env; \heap \pdash{M} \recS{f}{x}{e_f}{e} \bigse{(\ell',\heap'')}{\cRec{\cdot}(q,q')} }
  \end{mathpar}
\end{center}
  \caption{Rules of the effect-based cost semantics.}
  \label{fig:costsem2}
\end{figure*}


\begin{figure*}[th]
\fbox{\parbox{\textwidth}{
$\env; \heap \pdash{M} e \bigsb{(q,q')}$ \hspace{1.2em}
After evaluating expression $e$ in environment $\env$ and heap $\heap$ for several\\
\mbox{}\hspace{11em} steps, the watermark resource usage is $q$ and $q'$ resources are available.}}
\begin{center}
  \def \MathparLineskip {\lineskip=0.45cm}
  \begin{mathpar}
    \Rule{Ep:Abort}
    {
    }
    {\env; \heap \pdash{M} e \bigsb{0} }

    \Rule{Ep:App1}
    {  \env; \heap \pdash{M} e_1 \bigsb{(q_0,q_1)} 
    }
    {\env; \heap \pdash{M} \appS{e_1}{e_2} \bigsb{\cApp{}{\cdot}(q_0,q_1)}
    }

    \Rule{Ep:App2}
    {  \env; \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \env; \heap_1 \pdash{M} e_2 \bigsb{(q_2,q_3)} 
    }
    {\env; \heap \pdash{M} \appS{e_1}{e_2} \bigsb{\cApp{}{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ep:App3}
    {  \env; \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \heap(\ell_1) = \closure{\lambda x. e}{\env'}
    \\ \env; \heap_1 \pdash{M} e_2 \bigse{(\ell_2,\heap_2)}{(q_2,q_3)}
    \\ \env'[x \mapsto \ell_2]; \heap_2 \pdash{M} e \bigsb{(q_3,q_4)} 
    }
    {\env; \heap \pdash{M} \appS{e_1}{e_2} \bigsb
    {\cApp{}{\cdot}(q_0,q_1){\cdot}(q_2,q_3){\cdot}(q_3,q_4)}
    }

    \Rule{Ep:Let1}
    {  \env, \heap \pdash{M} e_1 \bigsb{(q_0,q_1)}
    }
    {\env; \heap \pdash{M} \letS{e_1}{x}{e_2} \bigsb
    {\cLet{}{\cdot}(q_0,q_1)} 
    }

    \Rule{Ep:Let2}
    {  \env, \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \env[x\mapsto \ell_1], \heap_1 \pdash{M} e_2 \bigsb{(q_2,q_3)} 
    }
    {\env; \heap \pdash{M} \letS{e_1}{x}{e_2} \bigsb
    {\cLet{}{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ep:Cons1}
    {  \env; \heap \pdash{M} e_1 \bigsb{(q_0,q_1)} 
    }
    {\env; \heap \pdash{M} \consS{e_1}{e_2} \bigsb
     {\cCons}{\cdot}{(q_0,q_1)}
    }

    \Rule{Ep:Cons2}
    {  \env; \heap \pdash{M} e_1 \bigse{(\ell_1,\heap_1)}{(q_0,q_1)} 
    \\ \env; \heap_1 \pdash{M} e_2 \bigsb{(q_2,q_3)}
    }
    {\env; \heap \pdash{M} \consS{e_1}{e_2} \bigsb
     {\cCons}{\cdot}{(q_0,q_1)}{\cdot}{(q_2,q_3)}
    }

    \Rule{Ep:MatL0}
    { \env; \heap \pdash{M} e \bigsb{(q_0,q_1)} 
    }
    {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}  
     \bigsb{\cMatL1{\cdot}(q_0,q_1)}
    }

    \Rule{Ep:MatL1}
    { \env; \heap \pdash{M} e \bigse{(\ell,\heap')}{(q_0,q_1)} 
    \\ \heap'(\ell) = \Null
    \\ \env; \heap' \pdash{M} e_1 \bigsb{(q_2,q_3)} 
    }
    {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}  
     \bigsb{\cMatL1{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ep:MatL2}
    { \env; \heap \pdash{M} e \bigse{(\ell,\heap')}{(q_0,q_1)} 
    \\ \heap'(\ell) = (\ell_1,\ell_2)
    \\ \env[x_1 \mapsto \ell_1, x_2 \mapsto \ell_2]; \heap' \pdash{M} e_2 \bigsb{(q_2,q_3)}
    }
    {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}
     \bigsb{\cMatL2{\cdot}(q_0,q_1){\cdot}(q_2,q_3)}
    }

    \Rule{Ep:Rec}
    { \env' = \env[f \mapsto \ell_f]
    \\ \heap' = \heap,\ell_f \mapsto \closure{\lambda x . e_f}{\env'}
    \\ \env'; \heap' \pdash{M} e \bigsb{(q,q')}
    }
    {\env; \heap \pdash{M} \recS{f}{x}{e_f}{e} \bigsb{\cRec{\cdot}(q,q')} }
  \end{mathpar}
\end{center}
  \caption{Rules of the partial effect-based cost semantics.}
  \label{fig:costsem3}
\end{figure*}


\end{document}

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