Explain let scoping in unify

This commit is contained in:
JanUlrich 2024-04-08 21:45:54 +02:00
parent 5718c42e28
commit 079bb914e4

View File

@ -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.
\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.
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.
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}
let y = { let x = v in v.get() } in y.get()
\end{lstlisting} %TODO: explain: here y has to be a type without free variables.
m(l) = let v = l in v.get()
\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}
@ -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.
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?>
x? =. X
a <c C<X>
a <c C<X>
-------------
a <. X.C<X>, X.C<X> <c C<X>
a <. C<X>
---------
a <. C<U>, U = L
% a <. C<X>
% ---------
% a <. C<U>, U = L
%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