\input{preamble}

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

\begin{document}
\maketitle

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

\subsection{(12 Points) Solved Forms and MGUs}

Recall the following definitions from the lecture.

\begin{definition}
  A type substitution $\tsubst$ is a most general unifier (MGU) of a
  set of type constraints $\consts$ if $\tsubst \in \unifs(\consts)$
  and for every $\rho \in \unifs(\consts)$ there exists a type
  substitution $\tau$ such that $\rho = \tau \circ \tsubst$.
\end{definition}
 
\begin{definition}
  An equation $\teq{X}{T} \in \consts$ is in \emph{solved form} in a
  constraints set $\consts$ if $X$ is a variable that does not occur
  anywhere else in $\consts$; in particular $X \not\in \vars{T}$. A
  set of constraints $\consts$ is in solved form if every equation in
  $\consts$ is in solved form in $\consts$.
\end{definition}
%
Let $\consts = \{\teq{X_1,T_1}, \ldots, \teq{X_n,T_n}\}$ be a set of
type constraints in solved form and let
$\tsubst_{\consts} = \{X_1 \mapsto T_1, \ldots, X_n \mapsto
T_n\}$. Prove the following statements.


\begin{enumerate}[a)]
\item $\tsubst_{\consts}$ is a MGU of $\consts$.
\item For every unifier $\rho \in \unifs(C)$ we have
  $\widehat{\rho} \circ \sigma_{\consts} = \rho$.
\end{enumerate}

\subsection{(18 Points) Let Normal Form}

Consider the subset of expressions that is given by the following
subset of syntactic forms.

$$
\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}
$$
Recall the corresponding rules from the resource-safety-based cost
semantics.

  \def \MathparLineskip {\lineskip=0.45cm}
  \begin{mathpar}
 \small
    \Rule{Es:Var}
    {  \env(x) = \ell
    \\ q = q' + \cVar
    }
    {\env; \heap \pdash{M} x \bigss{q}{q'} (\ell,\heap)}

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

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

    \Rule{Es:Let}
    {  q = q_0 + \cLet{}
    \\ \env, \heap \pdash{M} e_1 \bigss{q_0}{q_1} (\ell_1,\heap_1)
    \\ \env[x\mapsto \ell_1], \heap_1 \pdash{M} e_2 \bigss{q_1}{q'} (\ell,\heap')
    }
    {\env; \heap \pdash{M} \letS{e_1}{x}{e_2} \bigss{q}{q'} (\ell,\heap')}

    \Rule{Es:Bconst}
    { b \in \{\trueS,\falseS\}
    \\ q = q' + \cB
    \\ \heap'= \heap, \ell \mapsto b
    }
    {\env; \heap \pdash{M} b \bigss{q}{q'} (\heap', \ell)}

    \Rule{Es:Cond1}
    { q = p + \cCond1
    \\\env; \heap \pdash{M} e \bigss{p}{p'} (\ell,\heap')
    \\ \heap'(\ell) {=} \trueS
    \\ \env; \heap' \pdash{M} e_1 \bigss{p'}{q'} (\ell_1,\heap_1)
    }
    {\env; \heap \pdash{M} \ifS{e}{e_1}{e_2} \bigss{q}{q'} (\ell_1,\heap_1)}

    \Rule{Es:Cond2}
    { q = p {+} \cCond2
    \\\env; \heap \pdash{M} e \bigss{p}{p'} (\ell,\heap')
    \\ \heap'(\ell) {=} \falseS
    \\ \env; \heap' \pdash{M} e_2 \bigss{p'}{q} (\ell_2,\heap_2)
    }
    {\env; \heap \pdash{M} \ifS{e}{e_1}{e_2} \bigss{q}{q'} (\ell_2,\heap_2)}
    % \Rule{Es:Nil}
    % { q = q' + \cNil
    % \\ \heap' = \heap, \ell \mapsto \Null
    % }
    % {\env; \heap \pdash{M} \nilS \bigss{q}{q'} (\ell,\heap')}

    % \Rule{Es:Cons}
    % { q = p + \cCons
    % \\ \env; \heap \pdash{M} e_1 \bigss{p}{p'} (\ell_1,\heap_1)
    % \\ \env; \heap_1 \pdash{M} e_2 \bigss{p'}{q'} (\ell_2,\heap_2)
    % \\ \heap_2 = \heap, \ell \mapsto (\ell_1, \ell_2)
    % }
    % {\env; \heap \pdash{M} \consS{e_1}{e_2} \bigss{q}{q'} (\ell,\heap')}

    % \Rule{Es:MatL1}
    % { q = p + \cMatL1
    % \\ \env; \heap \pdash{M} e \bigss{p}{p'} (\ell,\heap')
    % \\ \heap'(\ell) = \Null
    % \\ \env; \heap' \pdash{M} e_1 \bigss{p'}{q'} (\ell_1,\heap_1)
    % }
    % {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}  \bigss{q}{q'} (\ell_1,\heap_1)}

    % \Rule{Es:MatL2}
    % { q = p + \cMatL2
    % \\ \env; \heap \pdash{M} e \bigss{p}{p'} (\ell,\heap')
    % \\ \heap'(\ell) = (\ell_1,\ell_2)
    % \\ \env[x_1 \mapsto \ell_1, x_2 \mapsto \ell_2]; \heap' \pdash{M} e_2 \bigs{p'}{q'} (\ell,\heap')
    % }
    % {\env; \heap \pdash{M} \matchLS{e}{e_1}{x_1}{x_2}{e_2}  \bigss{q}{q'} (\ell_1,\heap_1)}

    % \Rule{Es:Rec}
    % { q = p + \cRec
    % \\\env' = \env[f \mapsto \ell_f]
    % \\ \heap' = \heap,\ell_f \mapsto \closure{\lambda x . e_f}{\env'}
    % \\ \env'; \heap' \pdash{M} e \bigs{p}{q'} (\ell',\heap'')
    % }
    % {\env; \heap \pdash{M} \recS{f}{x}{e_f}{e} \bigss{q}{q'} (\ell',\heap'')}
\end{mathpar}

When defining more advanced type systems, it is often handy to work
with a simpler set of expressions in which the evaluation order is
exclusively defined by let expressions. To this end we use variables
instead of expressions in syntactic forms whenever this is possible
without restricting the expressivity of the language.
%
We define expressions in \emph{let normal form} as follows.
$$
\begin{array}{llll}
  \bar e &::=& x & x \\
  && \text{free}(x) & \text{free}(x) \\
  && \absS{x}{\bar e} & \word{fun } x \to \bar e\\
  && \appS{x_1}{x_1} & x_1\ x_2\\
  && \letS{\bar e_1}{x}{\bar e_2} & \word{let } x = \bar e_1 \word{ in } \bar e_2\\
  && \letFS{\bar e_1}{x}{\bar e_2} & \word{free let } x = \bar e_1 \word{ in } \bar e_2\\
  && \trueS & \word{true}\\
  && \falseS & \word{false}\\
  && \ifS{x}{\bar e_1}{\bar e_2} & \word{if } x \word{ then } \bar e_1 \word{ else } \bar e_2\\
  && \pairS{x_1}{x_2} & (x_1,x_2)\\
  && \matchPS{x}{x_1}{x_2}{\bar e}
     & \word{let } (x_1,x_2) = x \word{ in } \bar e\\
  && \nilS & \word{[]}\\
  && \consS{x_1}{x_2} & x_1 \mathop{::} x_2\\
  && \matchLS{x}{\bar e_1}{x_1}{x_2}{\bar e_2} 
     & \word{match } x \word{ with } \mid [] \to \bar e_1 \mid x_1\mathop{::}x_2 \to \bar e_2\\
  && \recS{f}{x}{\bar e_f}{\bar e} 
     & \word{let} \word{ rec } f x = \bar e_f \word{ in } \bar e
\end{array}
$$
The expressions in let normal form contain a new syntactic form
$\letFS{\bar e_1}{x}{\bar e_2}$
for let expressions. These \emph{free let expressions} are similar to
ordinary let expressions except that they do not influence the
resource usage. The corresponding evaluation rule is defined as
follows.
%
\begin{mathpar}
  \Rule{Es:FLet} { \env, \heap \pdash{M} e_1
    \bigss{q}{p} (\ell_1,\heap_1) \\ \env[x\mapsto \ell_1],
    \heap_1 \pdash{M} e_2 \bigss{p}{q'} (\ell,\heap') } {\env; \heap
    \pdash{M} \letFS{e_1}{x}{e_2} \bigss{q}{q'} (\ell,\heap')}

    \Rule{Es:FVar}
    {  \env(x) = \ell
    }
    {\env; \heap \pdash{M} \text{free}(x) \bigss{q}{q} (\ell,\heap)}
\end{mathpar}
%
Prove the following theorem.
\begin{theorem}
  Let $\Gamma \tmono e : T$. Then
  there exists an expression $\bar e$ in let normal form such that
  $\Gamma \tmono \bar e : T$  and
  $$
  \env; \heap \pdash{M} e \bigss{q}{q'} (\ell,\heap') \iff \env; \heap \pdash{M} \bar e \bigss{q}{q'} (\ell,\heap') \; .
  $$
  for every resource metric $M$.
  Moreover, the expression $\bar e$ can be efficiently computed from $e$.
\end{theorem}
Note that the expression $e$ in the theorem is defined by the subset
of syntactic forms defined in this problem. The theorem does not hold
if $e$ contains lambda expressions since the translation to let normal
form would affect function closures $\closure{\lambda x . e}{\env}$
that are stored on the heap. So in general, two evaluation judgements
would not result in the same heap $\heap'$. To prove the theorem for
expressions with lambda abstraction we would have to define an
equivalence relation for heaps that would be preserved during the
evaluation of $e$ and $\bar e$ under equivalent heaps.


\end{document}

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