%%% This is the scribe notes template for CS611
%%% There are several comments preceded by CS611: and boxed in %%%%'s
%%% which indicate where macros should be altered to set up the header
%%% for the paper.  Your Notes should go at the comment SCRIBE NOTES GO HERE!.

%%% In the various .sty files that accompany this .tex file you will
%%% find LaTeX macros that make it easier to typeset inference rules
%%% and programming language constructs.  You must make sure that the
%%% file proof.sty is in a path searched by LaTeX when you try to
%%% use this file.  Take a look to see what macros are available--it
%%% will save you time and make the notes look better.  Feel free to
%%% extend the set of macros--post them to the newsgroup and contact
%%% the course staff if you come up with some good ones so they can be
%%% added to the template.

%%% This template includes examples of how to use some of the macros
%%% to give you an idea of how they work.  (Delete the examples when
%%% you do your scribing.)

\documentclass{article}
\usepackage{611-lecture}
\usepackage{amsmath,amssymb,amsthm,amsfonts,latexsym}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: Please fill in these macros as appropriate:
\lecture{34} %% Lecture number
\title{Type Inference for Partial Types}   %% Title of lecture
%\author{Asif-ul Haque, Xin Zheng}  %% name of scribe
\date{29 November 2006}    %% Date of lecture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% See 611.sty for a variety of macros that will be helpful in
% typesetting the lecture
%
% quotes can be used for typewriter font
% <> can be used for angle brackets

\renewcommand\emptyset\varnothing
\newcommand{\inL}{\ensuremath{\mathsf{inL}}}
\newcommand{\inR}{\ensuremath{\mathsf{inR}}}
\newcommand\caseof[3]{\mathsf{case}~{#1}~\mathsf{of}~{#2}~|~{#3}}

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}

\newcommand{\Z}{\mathbb{Z}}
\newcommand{\group}[1]{\left\langle{#1}\right\rangle}

%\theoremstyle{definition}
%\newtheorem*{defn}{Definition}
\newcommand{\nondet}{\left[\!\kern1pt\right]}
\renewcommand\phi\varphi
\renewcommand\wp[2]{\mathsf{wp}~{#1}~{#2}}
\newcommand\wlp[2]{\mathsf{wlp}~{#1}~{#2}}
\renewcommand\({\begin{eqnarray*}}
\renewcommand\){\end{eqnarray*}}

\newcommand\LOOKUP[2]{\mathrm{LOOKUP}~{#1}~{#2}}
\newcommand\UPDATE[3]{\mathrm{UPDATE}~{#1}~{#2}~{#3}}
\newcommand\MALLOC[2]{\mathrm{MALLOC}~{#1}~{#2}}
\newcommand\EMPTY{\mathrm{EMPTY\mbox{-}STORE}}
\renewcommand\dom[1]{\mathrm{dom}\,{#1}}
\newcommand\p[2]{\langle{#1},\,{#2}\rangle}
\newcommand\bigcdot{\mathrel{\raisebox{1pt}{$\scriptscriptstyle\bullet$}}}
\newcommand\holed[1]{[\,#1\,]}
\newcommand\hole{\holed\bigcdot}
\newcommand\context[1]{E\kern1pt\holed{#1}}
\newcommand\contextHole{\context\bigcdot}
\newcommand\goesto[2]{\underset{#2}{\overset{#1}\to}}
\newcommand\ifthenelse[3]{\mathsf{if\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\ifpthenelse[3]{\mathsf{ifp\ }#1\mathsf{\ then\ }#2\mathsf{\ else\ }#3}
\newcommand\whiledo[2]{\mathsf{while\ }#1\mathsf{\ do\ }#2}
\newcommand\letin[3]{\mathsf{let\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\letrec[5]{\mathsf{letrec\ }#1 = #2\mathsf{\ and\ \ldots\ and\ }#3 = #4\mathsf{\ in\ }#5}
\newcommand\letrecone[3]{\mathsf{letrec\ }#1 = #2\mathsf{\ in\ }#3}
\newcommand\true{\ensuremath{\mathsf{true}}}
\newcommand\false{\ensuremath{\mathsf{false}}}
\newcommand\error{\ensuremath{\mathsf{error}}}
\newcommand\pca[3]{\{#1\}\kern1pt{#2}\kern1pt\{#3\}}
\newcommand\states{\Set{St}}
\newcommand\rtc{^{\textstyle *}}
\newcommand\sat\vDash
\newcommand\force\vdash

\newcommand\hyphen{\mbox{-}}
\newcommand\lookup[2]{\nm{LOOKUP}~#1~\mquote{#2}}
\newcommand\update[3]{\nm{UPDATE}~#1~\mquote{#2}~#3}
\newcommand\SBk[1]{\SB{#1}k}
\newcommand\fix[1]{\mathsf{fix}\,{#1}}

\newlength\reasonwidth
\setlength\reasonwidth{3cm}
\newcommand\reasoning[1]{\def\longest{#1}\settowidth{\reasonwidth}{$\displaystyle\longest$}\addtolength{\reasonwidth}{5mm}}
\newcommand\reason[2]{\makebox[\reasonwidth][l]{$\displaystyle{#1}$}\mbox{#2}}

\renewcommand\inj[1]{\mathsf{in}_{#1}}
\newcommand\proj[1]{\pi_{#1}}
\newcommand{\dlt}{\sqsubseteq}
\newcommand\floor[1]{\lfloor{#1}\rfloor}
\newcommand\cf[1]{[\kern1pt{#1}\kern1pt]}
\newcommand\SBpr[1]{\SB{#1}\,\phi\,\rho}
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}

\newcommand\judge[3]{{#1}\force{#2}:{#3}}
\newcommand\Gjudge[2]{\judge\Gamma{#1}{#2}}%
\newcommand\forceUSN{\mathrel{\makebox[2pt][l]{$\force$}\raisebox{-3pt}[0pt][0pt]{\tiny{\textit{USN}}}}}
\newcommand\judgeUSN[3]{{#1}\forceUSN{#2}:{#3}}
\renewcommand\C[3]{\Tr C{#1}\kern1pt{#2}\kern1pt{#3}}
\renewcommand\Cr[1]{\C{#1}\Gamma\rho}
\newcommand\Irred[1]{\ensuremath{\mathrm{Irred}(#1)}}

\newcommand\seq[3]{#1_{#2},\ldots,#1_{#3}}
\newcommand\subseteqttwo[5]{\subseteqt{#1}{#2}{#3,\,#4/#5}}
\newcommand\subseteqtlist[5]{\subseteqt{#1}{#2}{#3,\ldots,#4/#5}}
\newcommand\Unify[1]{\mathrm{Unify}(#1)}
\newcommand\fa[2]{\forall{#1}\kern1pt.\kern1pt{#2}}%
\newcommand\Judge[4]{\judge{#1;\,#2}{#3}{#4}}
%\renewcommand\infer[2]{\ensuremath{\dfrac{#2}{#1}}}%
\renewcommand\hat\widehat

\newenvironment{proofof}[1]{\addtolength{\topsep}{1mm}\begin{trivlist}\item[]\hspace{\parindent}{\em Proof of #1.}}{\qed\end{trivlist}}

\newcommand\lamcal{$\lambda$-calculus}
\newcommand\mut[2]{\mu#1.\,#2}
\renewcommand\star{^{\textstyle *}}

\newsavebox{\fterm}
\newsavebox{\gterm}
\savebox{\fterm}(40,20){
\begin{picture}(40,20)(-20,-20)
\put(0,0){\makebox(0,0){$f$}}
\put(-5,-5){\line(-1,-1){10}}
\put(5,-5){\line(1,-1){10}}
\end{picture}
}

\savebox{\gterm}(0,20){
\begin{picture}(0,20)(0,-20)
\thicklines
\put(0,0){\makebox(0,0){$g$}}
\put(0,-5){\line(0,-1){10}}
\end{picture}
}

\def\dead{(\;)}
\def\eps{\epsilon}
\def\edge#1#2#3{#1\stackrel{#2}{\to}#3}
\def\trans#1{\stackrel{#1}{\to}}
\def\sem#1{\mbox{$[\hspace{-0.15em}[$}#1\mbox{$]\hspace{-0.15em}]$}}
%\def\TS{{\mathcal{T}}}
%\def\FS{{\mathcal{F}}}
%\def\RS{{\mathcal{R}}}
\newcommand\DA[1]{_{#1}}
\def\lAC{\leq_{\rm AC}}
\def\lFIN{\leq_{\rm FIN}}

\begin{document}
\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CS611: SCRIBE NOTES GO HERE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\parindent=0pt%
\parskip=1em%

\newcommand\MM{M}
\newcommand\NN{N}
\newcommand\LL{\mathcal{L}}

\section{Introduction}

In this lecture we study the inference problem for simple types
in the presence of subtyping and recursive types.  For simplicity,
we restrict our attention to a simple equirecursive
type system consist only of the function
space constructor $\to$ and a universal type $\top$.  This system
is known in the literature as _partial types_.

Finite partial types were originally introduced by Thatte in
1988 as a means of typing certain objects that are not typable
with simple types, such as heterogeneous lists and persistent data.

In this lecture we will outline an approach that gives an
$O(n^3)$ algorithm for type inference.  As in the previous lecture,
the algorithm is automata-theoretic.  It constructs a certain finite
automaton with $O(n^2)$
states representing a canonical solution to a given set of type constraints.
The canonical
solution to the system is just the regular language accepted by the
automaton, where we represent types as binary trees and binary trees
as prefix-closed sets of strings over a two-letter alphabet.
The construction works equally well for finite or recursive types.

The canonical solution always exists, but it may not be finite;
however, since it is contained in all other solutions, we can check
for the existence of a finite solution by checking whether the
canonical solution is finite.  Thus the typability question reduces
essentially to the finiteness problem for regular sets.

It is known that the type inference problem
for partial types is P-hard; thus it is P-complete.  It can also be
shown that every $\lambda$-term with a partial type is strongly
normalizing and that every $\lambda$-term in
normal form has a partial type.

This result is quite long, so for brevity we will omit proofs.

\subsection{Partial Types}
\label{sec:partialtypes}

Formally, partial types comprise a partially ordered set $(T,\leq)$,
where $T$ is the set of well-formed terms over the constant symbol
$\top$ and the binary type constructor $\to$, and $\leq$ is the
partial order defined inductively as follows:
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item $\tau\leq \top$ for any $\tau$;
\item $\sigma\to \tau\leq \sigma'\to \tau'$ if and only if $\sigma'\leq \sigma$ and $\tau\leq \tau'$.
\end{enumerate}
Intuitively, the type constructor $\to$ represents the usual function
space constructor, and $\top$ is a _universal type_ that
includes every other type.  The partial order $\leq$ can be thought of
as _type inclusion_ or _coercion_; that is, $\sigma\leq \tau$ if it
is possible to cast type $\sigma$ to type $\tau$.

Clause (ii) in the definition of $\leq$ models the fact that a
function with domain $\sigma$ and range $\tau$ can be coerced to a function
with domain $\sigma'$ and range $\tau'$ provided $\sigma'$ can be coerced to $\sigma$
and $\tau$ can be coerced to $\tau'$; thus the subtype order on functions
is covariant in the range and contravariant in the domain.  That
$\leq$ is contravariant in the domain is the main
source of difficulty in type inference in the presence of subtyping.

\subsection{Typing Rules}

If $e$ is a $\lambda$-term, $\tau$ is a partial type, and $\Gamma$ is a type
environment, that is, a partial function assigning types to variables,
then the judgement $\Gamma \vdash e:\tau\label j$
means that $e$ has the partial type $\tau$ in the environment $\Gamma$.
This holds when the judgement is derivable using the rules
\begin{gather}
\Gamma \vdash x:\Gamma(x)\label{ir1}\\[1ex]
\dfrac{\Gamma[\sigma/x] \vdash e:\tau}{\Gamma \vdash \lambda x.e:\sigma\to \tau}\label{ir2}\\[1ex]
\dfrac{\Gamma \vdash e:\sigma\to \tau ~~~ \Gamma \vdash e':\sigma}{\Gamma \vdash e~e':\tau} \label{ir3}\\[1ex]
\dfrac{\Gamma \vdash e:\sigma ~~~ \sigma \leq \tau}{\Gamma \vdash e:\tau}\label{ir4}
\end{gather}
The first three rules are the usual rules for simple types and the
last rule is the rule of _subsumption_.

More $\lambda$-terms are typable with partial types than with simple
types.  For example, the term $\lambda f.(fK(fI))$, where $K = \lambda
x.(\lambda y.x)$ and $I = \lambda z.z$, has partial type
$(\top\to\top\to\top)\to\top$, but no simple type.

\section{From Rules to Constraints}
\label{RtoC}

Given a $\lambda$-term $e$, the type inference question can be
rephrased in terms of solving a system of type constraints.  Assume
that $e$ has been $\alpha$-converted so that all bound variables are
distinct.  Let $X$ be the set of $\lambda$-variables $x$ occurring in
$e$, and let $Y$ be a set of variables disjoint from $X$ consisting of
one variable $\sem d$ for each occurrence of a subterm $d$ of $e$.
(The notation $\sem d$ is ambiguous because there may be more than one
occurrence of $d$ in $e$.  However, it will always be clear from
context which occurrence is meant.)  We generate the following system
of inequalities over $X\cup Y$:
\begin{itemize}
\item
for every occurrence in $e$ of a subterm of the form $\lambda x.d$, the
inequality
\(
x\to\sem d &\leq& \sem{\lambda x.d};
\)
\item
for every occurrence in $e$ of a subterm of the form $d~c$, the inequality
\(
\sem d &\leq& \sem c\to\sem{d~c};
\)
\item
for every occurrence in $e$ of a $\lambda$-variable $x$, the
inequality
\(
x &\leq& \sem x.
\)
\end{itemize}

Denote by $C(e)$ the system of constraints generated from $e$ in this fashion.

Let $\Gamma$ be a type environment assigning a type to each
$\lambda$-variable occurring freely in $e$.  If $\Delta$ is a function
assigning a type to each variable in $X\cup Y$, we say that $\Delta$ _extends_ $\Gamma$ if $\Gamma$ and $\Delta$ agree on the domain of $\Gamma$.

The following theorem asserts that the solutions of $C(e)$ over $T$
correspond exactly to the possible type annotations of $e$.

\begin{theorem}
\label{annot}
The judgement $\Gamma\vdash e : \tau$ is derivable if and only if there exists
a solution $\Delta$ of $C(e)$ extending $\Gamma$ such that $\Delta(\sem e) = \tau$.  In
particular, if $e$ is closed, then $e$ is typable with type $\tau$ if and
only if there exists a solution $\Delta$ of $C(e)$ such that $\Delta(\sem e) = \tau$.
\end{theorem}

\section{From Types to Trees}
\label{TtoT}

In the previous lecture, we represented types as labeled trees,
but here the picture is even simpler:
a node is either an internal node, in which case its label must be
$\to$, or it is a leaf, in which case its label must be $\top$.
Thus we can dispense with the labeling altogether and represent
types as binary trees, or subsets $\sigma\subseteq\{L,R\}\star$ that are
\begin{itemize}
\item nonempty,
\item closed under prefix, and
\item binary, in the sense that for all $x$,
$x R\in\sigma$ iff $x L\in\sigma$.
\end{itemize}

As before, the _parity_ of a string $x\in\{L,R\}\star$, denoted $\pi x$, is the number mod 2 of $L$'s in $x$.  A string $x$ is _even_ (respectively, _odd_) if $\pi x = 0$ (respectively, 1).
A tree is _finite_ if it is finite as a set of strings.
A _path_ in a tree $\sigma$ is a maximal subset of $\sigma$ linearly
ordered by the prefix relation.  By K\"onig's Lemma, a tree is finite
iff it has no infinite paths.  An element $x\in\sigma$ is a _leaf_ of $\sigma$ if it is
not a proper prefix of any other element of $\sigma$.

For trees $\sigma,\tau$ and $x\in\{L,R\}\star$, define
\begin{align*}
\sigma\cdot\tau &\definedas
\{\eps\} \cup \{Lx \mid x\in\sigma\} \cup \{Ry \mid y\in\tau\} &
\sigma\DA x &\definedas \{y \mid xy\in\sigma\}.
\end{align*}
Thus $\sigma\cdot\tau$ is the tree with left
subtree $\sigma$ and right subtree $\tau$, and $\sigma\DA x$ is
the subtree of $\sigma$ at position $x$ if $x\in\sigma$.

The partial order on types defined in Section \ref{sec:partialtypes}
can now be reformulated.
For $\sigma,\tau\in T$, define $\sigma\leq\tau$ if both of the
following conditions hold for any $x$:
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item if $x$ is an even leaf of $\sigma$, then $x R\not\in\tau$;
\item if $x$ is an odd leaf of $\tau$, then $x R\not\in\sigma$.
\end{enumerate}

\medskip

\begin{lemma}
\label{po}
The relation $\leq$ is a partial order on trees, and agrees with the
order $\leq$ on finite types defined in Section \ref{sec:partialtypes}.
\end{lemma}

\section{From Constraints to Graphs}
\label{CtoG}

Instead of systems of type constraints involving type variables, we
consider a more general notion of a _constraint graph_.

A _constraint graph_ is a directed graph $G = (S,L,R,\leq)$
consisting of a set of nodes $S$ and three sets of directed edges $L,
R, \leq$.  We write $\edge s L t$ to indicate that the pair $(s,t)$ is
in the edge set $L$, and similarly $\edge s R t$, $\edge s {\leq} t$.
A constraint graph must satisfy the properties:
\begin{itemize}
\item
any node has at most one outgoing $L$ edge and at most one outgoing
$R$ edge;
\item
a node has an outgoing $L$ edge if and only if it has an outgoing $R$
edge.
\end{itemize}
A _solution_ for $G$ is any map $h:S\to T$ such that
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item if $\edge u L v$ and $\edge u R w$, then $h(u) = h(v)\cdot h(w)$;
\item if $\edge u {\leq} v$, then $h(u)\leq h(v)$.
\end{enumerate}
The solution $h$ is _finite_ if $h(s)$ is a finite set for all
$s$.

A system of type constraints as described in \S\ref{RtoC} gives rise
to a constraint graph by associating a unique node with every
subexpression occurring in the system of constraints, defining $L$ and
$R$ edges from an occurrence of an expression to its left and right
subexpressions, and defining $\leq$ edges for the inequalities.

A constraint graph is _closed_ if the edge relation $\leq$ is
reflexive, transitive, and closed under the following two rules which
say that the dashed edges exist whenever the solid ones do:
$$\begin{array}{c@{~~~~~~~~}c}
\setlength{\unitlength}{0.0125in}
\begin{picture}(158,95)(135,518)
\multiput(240,540)(-7.77778,0.00000){14}{\line(-1, 0){  3.889}}
\put(135,540){\vector(-1, 0){0}}
\multiput(180,555)(7.77778,0.00000){14}{\line( 1, 0){  3.889}}
\put(285,555){\vector( 1, 0){0}}
\put(165,600){\vector( 1, 0){105}}
\put(270,600){\vector( 1,-3){ 15}}
\put(270,600){\vector(-1,-2){ 30}}
\put(165,600){\vector( 1,-3){ 15}}
\put(165,600){\vector(-1,-2){ 30}}
\put (186,528) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\geq$}}}
\put (219,560) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\leq$}}}
\put (210,605) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\leq$}}}
\put (283,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$R$}}}
\put (244,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$L$}}}
\put (177,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$R$}}}
\put (138,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$L$}}}
\end{picture}
&
\setlength{\unitlength}{0.0125in}
\begin{picture}(158,95)(135,518)
\put(240,540){\vector(-1, 0){105}}
\put(180,555){\vector( 1, 0){105}}
\multiput(165,600)(7.77778,0.00000){14}{\line( 1, 0){  3.889}}
\put(270,600){\vector( 1, 0){0}}
\put(270,600){\vector( 1,-3){ 15}}
\put(270,600){\vector(-1,-2){ 30}}
\put(165,600){\vector( 1,-3){ 15}}
\put(165,600){\vector(-1,-2){ 30}}
\put (186,528) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\geq$}}}
\put (219,560) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\leq$}}}
\put (210,605) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$\leq$}}}
\put (283,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$R$}}}
\put (244,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$L$}}}
\put (177,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$R$}}}
\put (138,570) {\makebox(0,0) [lb] {\raisebox{0pt}[0pt][0pt]{$L$}}}
\end{picture}
\end{array}$$
Note that these two rules resemble the two implications of
Lemma~\ref{po}(iii).  The _closure_ of a constraint graph $G$ is
the smallest closed graph containing $G$ as a subgraph.

\begin{lemma}
A constraint graph and its closure have the same set of solutions.
\end{lemma}

\section{From Graphs to Automata}
\label{GtoA}

In this section we define an automaton $\MM$ that will be used to characterize the
canonical solution of a given constraint graph $G$.

Let a closed constraint graph $G = (S,L,R,\leq)$ be given.  The
automaton $\MM$ is defined as follows.  The input alphabet of $\MM$ is
$\{L,R\}$.  The states of $\MM$ are $S^2\cup S^1\cup S^0$.  States in
$S^2$ are written $(s,t)$, those in $S^1$ are written $(s)$, and the
unique state in $S^0$ is written $\dead$.  The transitions are defined
as follows.
\(
\begin{array}{ll}
\edge{(u,v)}{\eps}{(u,v')} & \mbox{if $\edge v{\leq}{v'}$ in $G$}\\
\edge{(u,v)}{\eps}{(u',v)} & \mbox{if $\edge{u'}{\leq}u$ in $G$}\\
\edge{(u,v)}R{(u',v')} & \mbox{if $\edge u R {u'}$ and $\edge v R {v'}$ in $G$}\\
\edge{(u,v)}L{(v',u')} & \mbox{if $\edge u L {u'}$ and $\edge v L {v'}$ in $G$}\\
\edge{(u,v)}{\eps}{(v)} & \mbox{always}\\
\edge{(v)}{\eps}{(v')} & \mbox{if $\edge v{\leq}{v'}$ in $G$}\\
\edge{(v)}R{(v')} & \mbox{if $\edge v R {v'}$ in $G$}\\
\edge{(v)}L{\dead} & \mbox{if $\edge v L {v'}$ in $G$}
\end{array}
\)
If $p$ and $q$ are states of $\MM$ and $\alpha\in\{L,R\}\star$, we
write $\edge p {\alpha} q$ if the automaton can move from state $p$ to
state $q$ under input $\alpha$, including possible
$\eps$-transitions.

The automaton $\MM_s$ is the automaton $\MM$ with start state $(s,s)$.
All states are accept states; thus the language accepted by $\MM_s$ is
the set of strings $\alpha$ for which there exists a state $p$ such
that $\edge{(s,s)}{\alpha}p$.  We denote this language by $\LL(s)$.

Informally, we can think of the automaton $\MM_s$ as follows.  We
start with two pebbles, one green and one red, on the node $s$ of the
constraint graph $G$.  We can move the green pebble forward along a
$\leq$ edge at any time, and we can move the red pebble backward along
a $\leq$ edge at any time.  We can move both pebbles simultaneously
along $R$ edges leading out of the nodes they occupy.  We can also
move them simultaneously along outgoing $L$ edges, but in the latter
case we switch their colors.  At any time, we can elect to remove the
red pebble; thereafter, we can move the green pebble forward along
$\leq$ or $R$ edges as often as we like, and forward along an $L$ edge
once, at which point the pebble must be removed.  The sequence of
$L$'s and $R$'s that were seen gives a string in $\LL(s)$, and all
strings in $\LL(s)$ are obtained in this way.

The intuition motivating the definition of $\MM$ is that we want to
identify the conditions that require a path to exist in any solution.
Thus $\LL(s)$ is the set of $\alpha$ that _must_ be there; this
intuition formalized in Lemma~\ref{must}. Once
we identify this set, we can show that it is a solution itself.

\begin{lemma}
\label{must}
If $h:S\to T$ is any solution and $(s,s)\trans{\alpha}p$, then
$\alpha\in h(s)$.  Moreover,
\begin{enumerate}
\renewcommand\labelenumi{(\roman{enumi})}
\item if $p=(u,v)$ then $h(u)\leq h(s)\DA\alpha\leq h(v)$;
\item if $p=(v)$ then $h(s)\DA\alpha\leq h(v)$.
\end{enumerate}
\end{lemma}

\section{Main Result}

The following theorem says that $\LL(s)$ gives the canonical solution of $G$.

\begin{theorem}
\label{main}
The sets $\LL(s)$ are trees, and the function $\LL:S\to T$ is a
solution of $G$.  Moreover, if $h:S\to T$ is any other solution,
then $\LL(s)\subseteq h(s)$ for any $s$.
\end{theorem}

As observed, recursive types are just regular trees.  The
canonical solution we have constructed, although possibly infinite, is
a regular tree.  Thus we can also treat the type inference problem for
recursive types.  Specifically, given
a $\lambda$-term, we construct the corresponding constraint graph and
automaton $\MM$.  Every subterm corresponds to a node $s$ in the
constraint graph, and its setwise-minimal type annotation
is represented by the language $\LL(s)$. 

\section{An Algorithm}

By Theorem \ref{main}, there exists a finite solution if and only if
the canonical solution is finite.  To determine this, we need only
check whether any $\LL(s)$ contains an infinite path.  We first form
the constraint graph, then close it; this gives a graph with $n$
vertices and $O(n^2)$ edges.  This can be done in time $O(n^3)$.  We
then form the automaton $\MM$, which has $n^2+n+1$ states but only
$O(n^3)$ transitions, at most $O(n)$ from each state.  We then check
for a cycle with at least one non-$\eps$ transition reachable from
some $(s,s)$.  This can be done in linear time in the size of the
graph using depth-first search.  The entire algorithm requires time
$O(n^3)$.

Thus the type inference problem for partial types with or without recursive
types is solvable in time $O(n^3)$.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

