Explain let scoping in unify
This commit is contained in:
parent
5718c42e28
commit
079bb914e4
57
unify.tex
57
unify.tex
@ -115,14 +115,47 @@ The vital part are the \rulename{Subst} and \rulename{Normalize} rules.
|
|||||||
They assert that a normal type placeholder is never replaced by a type containing free variables.
|
They assert that a normal type placeholder is never replaced by a type containing free variables.
|
||||||
\rulename{Normalize} replaces Wildcard placeholders with normal placeholders right before they get substituted by \rulename{Subst}.
|
\rulename{Normalize} replaces Wildcard placeholders with normal placeholders right before they get substituted by \rulename{Subst}.
|
||||||
The idea is to keep the possibility of replacing a wildcard placeholder with a free variable as long as possible.
|
The idea is to keep the possibility of replacing a wildcard placeholder with a free variable as long as possible.
|
||||||
As soon as they appear in a $\ntv{a} \doteq \type{T}$ constraint they can no longer be replaced by free variables.
|
As soon as they appear in a $\ntv{a} \doteq \type{T}$ constraint they have to be replaced with normal placeholders.
|
||||||
|
|
||||||
A type solution for a normal type placeholder will never contain free variables.
|
A type solution for a normal type placeholder will never contain free variables.
|
||||||
This is needed to guarantee well-formed type solutions and also keep free variables inside their scope.
|
This is needed to guarantee well-formed type solutions and also keep free variables inside their scope.
|
||||||
|
|
||||||
|
\begin{example}{Free variables must not leave the scope of the surrounding \texttt{let} statement}
|
||||||
|
|
||||||
|
\noindent
|
||||||
|
\begin{minipage}{0.40\textwidth}
|
||||||
\begin{lstlisting}
|
\begin{lstlisting}
|
||||||
let y = { let x = v in v.get() } in y.get()
|
m(l) = let v = l in v.get()
|
||||||
\end{lstlisting} %TODO: explain: here y has to be a type without free variables.
|
\end{lstlisting}
|
||||||
|
\end{minipage}%
|
||||||
|
\hfill
|
||||||
|
\begin{minipage}{0.59\textwidth}
|
||||||
|
\begin{constraintset}
|
||||||
|
$\tv{l} \lessdot \tv{v}, \tv{v} \lessdotCC \exptype{List}{\wtv{x}},
|
||||||
|
\wtv{x} \lessdot \tv{r}$
|
||||||
|
\end{constraintset}
|
||||||
|
\end{minipage}
|
||||||
|
Lets assume the variables \texttt{l} and \texttt{v}
|
||||||
|
get the type $\wctype{\wildcard{X}{\type{Object}}{\type{String}}}{List}{\rwildcard{X}}$ assigned to them.
|
||||||
|
After application of the \rulename{Capture} and \rulename{SubstWC} rules the constraint set looks like this:
|
||||||
|
|
||||||
|
$\begin{array}[c]{l}
|
||||||
|
\wctype{\wildcard{X}{\type{Object}}{\type{String}}}{List}{\rwildcard{X}} \lessdotCC \exptype{List}{\wtv{x}}, \wtv{x} \lessdot \ntv{r}
|
||||||
|
\\
|
||||||
|
\hline
|
||||||
|
\vspace*{-0.4cm}\\
|
||||||
|
\wildcard{X}{\type{Object}}{\type{String}} \vdash \wtv{x} \doteq \rwildcard{X}, \rwildcard{X} \lessdot \ntv{r}
|
||||||
|
\end{array}
|
||||||
|
$
|
||||||
|
|
||||||
|
Replacing $\tv{r}$ with $\rwildcard{X}$ would solve the constraint set but lead to the method type
|
||||||
|
|
||||||
|
\texttt{X m(List<? super String> l)} which makes no sense.
|
||||||
|
The normal type placeholder $\ntv{r}$ has to be replaced by a type without free type variables
|
||||||
|
($\ntv{r} \doteq \type{Object}$) leading to
|
||||||
|
|
||||||
|
\texttt{Object m(List<? super String> l)}.
|
||||||
|
\end{example}
|
||||||
|
|
||||||
\subsection{Algorithm}
|
\subsection{Algorithm}
|
||||||
|
|
||||||
@ -438,18 +471,14 @@ C \cup [\type{U}/\type{A}]\set{\ntv{a} \doteq \type{T}, \type{L} \doteq \type{U}
|
|||||||
There are n different rules to deal with $\type{N} \lessdot \type{N}$ constraints.
|
There are n different rules to deal with $\type{N} \lessdot \type{N}$ constraints.
|
||||||
Prepare, Capture, Reduce, Trim, Clear, Exclude, Adapt
|
Prepare, Capture, Reduce, Trim, Clear, Exclude, Adapt
|
||||||
|
|
||||||
|
% % TODO:
|
||||||
|
% a <c C<X>
|
||||||
|
% -------------
|
||||||
|
% a <. X.C<X>, X.C<X> <c C<X>
|
||||||
|
|
||||||
a <c C<x?>
|
% a <. C<X>
|
||||||
x? =. X
|
% ---------
|
||||||
a <c C<X>
|
% a <. C<U>, U = L
|
||||||
|
|
||||||
a <c C<X>
|
|
||||||
-------------
|
|
||||||
a <. X.C<X>, X.C<X> <c C<X>
|
|
||||||
|
|
||||||
a <. C<X>
|
|
||||||
---------
|
|
||||||
a <. C<U>, U = L
|
|
||||||
|
|
||||||
%The capture constraints are preserved when applying the \rulename{Upper} rule.
|
%The capture constraints are preserved when applying the \rulename{Upper} rule.
|
||||||
% This is legal: a T <c S constraint indicates a let-statement can be inserted. Therefore there must exist a type T' with T <. T' <c S
|
% This is legal: a T <c S constraint indicates a let-statement can be inserted. Therefore there must exist a type T' with T <. T' <c S
|
||||||
|
Loading…
Reference in New Issue
Block a user