Remove comments (cleanup). Add Clear and Exclude rules. Change Unify Soundness premise

This commit is contained in:
Andreas Stadelmeier 2024-03-25 19:12:35 +01:00
parent f2002ea833
commit e40693a7de
3 changed files with 59 additions and 229 deletions

View File

@ -144,11 +144,11 @@ For the example above a correct solution would be $\sigma(\tv{a}) = \wctype{\rwi
\typeExpr{} &({\mtypeEnvironment} , \texttt{let}\ \expr{x} = \expr{e}_1 \ \texttt{in} \ \expr{e}_2) = \\ \typeExpr{} &({\mtypeEnvironment} , \texttt{let}\ \expr{x} = \expr{e}_1 \ \texttt{in} \ \expr{e}_2) = \\
& \begin{array}{ll} & \begin{array}{ll}
\textbf{let} \textbf{let}
& \tv{e} \ \text{fresh} \\ & \tv{e}, \tv{x} \ \text{fresh} \\
& \consSet_R = \typeExpr({\mtypeEnvironment}, \expr{e}_1, \tv{e})\\ & \consSet_R = \typeExpr({\mtypeEnvironment}, \expr{e}_1, \tv{e})\\
& \constraint = & \constraint =
\set{ \set{
\tv{e} \lessdotCC \tv{x} \tv{e} \lessdot \tv{x}
}\\ }\\
{\mathbf{in}} & { {\mathbf{in}} & {
\consSet_R \cup \set{\constraint}} \cup \typeExpr(\mtypeEnvironment \cup \set{\expr{x} : \tv{x}}) \consSet_R \cup \set{\constraint}} \cup \typeExpr(\mtypeEnvironment \cup \set{\expr{x} : \tv{x}})
@ -190,7 +190,7 @@ The ones to already typed methods and calls to untyped methods.
\textbf{let} \textbf{let}
& \tv{r}, \ol{\tv{r}} \text{ fresh} \\ & \tv{r}, \ol{\tv{r}} \text{ fresh} \\
& \constraint = [\overline{\wtv{b}}/\ol{Y}]\set{ & \constraint = [\overline{\wtv{b}}/\ol{Y}]\set{
\ol{S} \lessdot \ol{T}, \type{T} \lessdot \tv{a}, \ol{S} \lessdotCC \ol{T}, \type{T} \lessdot \tv{a},
\ol{Y} \lessdot \ol{N} }\\ \ol{Y} \lessdot \ol{N} }\\
\mathbf{in} & (\consSet_R \cup \overline{\consSet} \cup \constraint, \type{T}) \\ \mathbf{in} & (\consSet_R \cup \overline{\consSet} \cup \constraint, \type{T}) \\
& \mathbf{where}\ \begin{array}[t]{l} & \mathbf{where}\ \begin{array}[t]{l}

View File

@ -329,18 +329,17 @@ If there is a solution for a constraint set $C$, then there is also a solution f
% does this really work. We have to show that the algorithm never gets stuck as long as there is a solution % does this really work. We have to show that the algorithm never gets stuck as long as there is a solution
% maybe there are substitutions with types containing free variables that make additional solutions possible. Check! % maybe there are substitutions with types containing free variables that make additional solutions possible. Check!
\begin{lemma}\label{lemma:unifySoundness} \begin{lemma}{\unify{} Soundness:}\label{lemma:unifySoundness}
The \unify{} algorithm only produces correct output. \unify{}'s type solutions are correct respective to the subtyping rules defined in figure \ref{fig:subtyping}.
\begin{description} \begin{description}
\item[If] $(\sigma, \Delta) = \unify{}( \Delta', \, \overline{ \type{S} \lessdot \type{T} } \cup \overline{ \type{S'} \lessdotCC \type{T'} } )$ %\cup \overline{ \type{S} \doteq \type{S'} })$ \item[If] $(\sigma, \Delta) = \unify{}( \Delta', \, \overline{ \type{S} \lessdot \type{T} } \cup \overline{ \type{S'} \lessdotCC \type{T'} } )$ %\cup \overline{ \type{S} \doteq \type{S'} })$
%\item[and] $fv(\overline{ \type{S} }) = \emptyset$, $fv(\overline{ \type{T} }) = \emptyset$ \item[Then] there exists a substitution $\sigma'$ and a set of types $\overline{\wcNtype{\Delta}{N}}$ with:
\item[Then] there exists a $\sigma'$ with: \begin{itemize}
$\sigma \subseteq \sigma'$ and \item $\sigma \subseteq \sigma'$
$\Delta, \Delta' \vdash \overline{\sigma'(\type{S}) <: \sigma'(\type{T})}$ \item $\Delta, \Delta' \vdash \overline{\sigma'(\type{S}) <: \sigma'(\type{T})}$
and $\Delta, \Delta', \overline{\Delta} \vdash \overline{\type{N} <: \sigma'(\type{T'})}$ where $\overline{\sigma(\type{S'}) = \wcNtype{\Delta}{N}}$, \item $\Delta, \Delta' \vdash \overline{\sigma'(\type{S'}) <: \wcNtype{\Delta}{N}}$
otherwise $\Delta, \Delta' \vdash \overline{\sigma'(\type{S'}) <: \sigma'(\type{T'})}$ \item $\Delta, \Delta', \overline{\Delta} \vdash \overline{\type{N} <: \sigma'(\type{T'})}$
% and $\sigma(\type{T'}) = \sigma(\type{T'})$. \end{itemize}
\end{description} \end{description}
\end{lemma} \end{lemma}

263
unify.tex
View File

@ -259,11 +259,8 @@ $
\end{figure} \end{figure}
The capture constraints are preserved when applying the \rulename{Upper} rule. %The capture constraints are preserved when applying the \rulename{Upper} rule.
%because \texttt{let} statements like % 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
%let x : X = v in
%can be transformed to
%let x : U = v in
\begin{figure} \begin{figure}
\begin{center} \begin{center}
@ -439,6 +436,11 @@ The capture constraints are preserved when applying the \rulename{Upper} rule.
\wildcardEnv \vdash C \cup \set{\type{N} \doteq \rwildcard{A}}\\ \wildcardEnv \vdash C \cup \set{\type{N} \doteq \rwildcard{A}}\\
\hline \hline
\wildcardEnv \vdash C \cup \set{\rwildcard{A} \doteq \type{N}} \wildcardEnv \vdash C \cup \set{\rwildcard{A} \doteq \type{N}}
\end{array} \quad
\begin{array}[c]{l}
\wildcardEnv \vdash C \cup \set{\ntv{a} \doteq \rwildcard{A}}\\
\hline
\wildcardEnv \vdash C \cup \set{\rwildcard{A} \doteq \ntv{a}}
\end{array}$ \end{array}$
\end{tabular}} \end{tabular}}
\end{center} \end{center}
@ -583,6 +585,46 @@ Their upper and lower bounds are fresh type variables.
\end{array} \end{array}
$ $
\\\\ \\\\
\rulename{Clear}
&
$
\begin{array}[c]{@{}ll}
\begin{array}[c]{l}
\wildcardEnv \cup \set{\wildcard{A}{\type{U}}{\type{L}}} \vdash
C \cup \, \set{ \wctype{\Delta}{C}{\ol{S}} \lessdot \type{T} } \\
\hline
\vspace*{-0.4cm}\\
\subst{\type{U}}{\rwildcard{A}}\wildcardEnv \vdash
[\type{U}/\rwildcard{A}]C \cup \, [\type{U}/\rwildcard{A}]\set{ \wctype{\Delta}{C}{\ol{S}} \lessdot \type{T}, \type{U} \doteq \type{L} } \\
\end{array}
%\quad \ol{Y} = \textit{fresh}(\ol{X})
\quad \begin{array}[c]{l}
\Delta \neq \emptyset\\
\rwildcard{A} \in \text{fv}(\type{T})
\end{array}
\end{array}
$
\\\\
\rulename{Exclude}
&
$
\begin{array}[c]{@{}ll}
\begin{array}[c]{l}
\wildcardEnv \vdash
C \cup \, \set{ \wctype{\Delta}{C}{\ol{S}} \lessdot \type{T} } \\
\hline
\vspace*{-0.4cm}\\
\subst{\tv{a}}{\wtv{a}}\wildcardEnv \vdash
[\tv{a}/\wtv{a}]C \cup \, [\tv{a}/\wtv{a}]\set{ \wctype{\Delta}{C}{\ol{S}} \lessdot \type{T}, \type{U} \doteq \type{L} } \\
\end{array}
%\quad \ol{Y} = \textit{fresh}(\ol{X})
\quad \begin{array}[c]{l}
\Delta \neq \emptyset\\
\wtv{a} \in \text{fv}(\type{T}), \tv{a} \ \text{fresh}
\end{array}
\end{array}
$
\\\\
\rulename{Adopt} \rulename{Adopt}
& $ & $
\begin{array}[c]{@{}ll} \begin{array}[c]{@{}ll}
@ -1002,137 +1044,6 @@ This builds a search tree over multiple possible solutions.
\label{fig:step2-rules} \label{fig:step2-rules}
\end{figure} \end{figure}
% \begin{figure}
% \begin{center}
% \fbox{
% \begin{tabular}[t]{l@{~}l}
% \rulename{Same}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \vdash
% C \cup \type{G} \lessdot \tv{a}\\
% \hline
% \wildcardEnv \vdash
% C \cup \set{
% \tv{a} \doteq \type{G}
% }
% \end{array} \quad \begin{array}[c]{l}
% \text{fv}(\type{G}) \in \Delta_{in}
% \end{array}
% $
% \\\\
% \rulename{\generalizeRule}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \vdash C \cup \wctype{\Delta}{C}{\ol{T}} \lessdot \tv{a}\\
% \hline
% \wildcardEnv \vdash C \cup \set{\wctype{\Delta}{C}{\ol{T}} \lessdot \tv{a},
% \tv{a} \doteq \wctype{\overline{\wildcard{X}{\tv{u}}{\tv{l}}}}{C}{\overline{\rwildcard{X}}},
% %\overline{\tv{l} \lessdot \tv{u}}, % not needed, due to subst and reduce rule which are used afterwards
% \overline{\tv{u} \lessdot \type{S}}
% }
% \end{array} \quad \begin{array}[c]{l}
% \texttt{class} \ \exptype{C}{\ol{X \triangleleft \type{S}}} \triangleleft \exptype{D}{\ol{N}} \\
% \text{fresh}\ \overline{\wildcard{X}{\tv{u}}{\tv{l}}}
% \end{array}
% $
% \\\\
% \rulename{Subst-X}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}} \vdash
% C \cup \rwildcard{X} \lessdot \tv{a}\\
% \hline
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}} \vdash
% C \cup \set{
% \tv{a} \doteq \rwildcard{X}
% }
% \end{array} \quad \begin{array}[c]{l}
% \rwildcard{X} \in \Delta_{in}
% \end{array}
% $
% \\\\
% \rulename{Gen-X}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}} \vdash
% C \cup \rwildcard{X} \lessdot \tv{a}\\
% \hline
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}} \vdash
% C \cup \set{
% \type{U} \lessdot \tv{a}
% }
% \end{array}
% $
% \\\\
% \rulename{Super}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \vdash C \cup \wctype{\Delta}{C}{\ol{T}} \lessdot \tv{a}\\
% \hline
% \wildcardEnv \vdash C \cup \set{ \wctype{\Delta'}{D}{[\ol{T}/\ol{X}]\ol{N}} \lessdot \tv{a} }
% %\set{\wctype{\ol{\wtype{W}}}{D}{[\ol{X}/\ol{Y}]\ol{Z}} \lessdot \tv{a}}
% \end{array} \quad
% \begin{array}{l}
% \texttt{class} \ \exptype{C}{\ol{X}} \triangleleft \exptype{D}{\ol{N}} \\
% \ol{X} \notin \wildcardEnv \cup \Delta,\, \Delta' = \Delta \cap \text{fv}([\ol{T}/\ol{X}]\ol{N})
% \end{array}
% $
% \\\\
% \rulename{Settle}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \vdash C \cup \set{ \tv{a} \lessdot \type{N},
% \tv{a} \lessdot \tv{b}}
% \\
% \hline
% \wildcardEnv \vdash C \cup \set{ \tv{a} \lessdot \tv{b}, \tv{b} \lessdot \type{N} }
% \end{array}
% $
% \\\\
% \rulename{Raise}
% & $
% \begin{array}[c]{l}
% \wildcardEnv \vdash C \cup \set{ \tv{a} \lessdot \type{N},
% \tv{a} \lessdot \tv{b}}
% \\
% \hline
% \wildcardEnv \vdash C \cup \set{\tv{a} \lessdot \type{N}, \type{N} \lessdot \tv{b} }
% \end{array}
% $
% \\\\
% \end{tabular}}
% \end{center}
% \caption{Step 2 branching: Multiple rules can be applied to the same constraint}
% \label{fig:step2-rules}
% \end{figure}
%For every $\type{T} \lessdot \tv{a}$ constraint, the unify algorithm has to consider every possible supertype of $\type{T}$.
%For every $\type{N}$ with $\type{T} \leq \type{N}$: ($\texttt{class} \ \exptype{T}{\ol{Y} \triangleleft \ol{N}} \triangleleft \type{N}$)
%There are two different ways of handling a $\type{T} \lessdot \tv{a}$ constraint:
%TODO: why the \generalizeRule is basically the Same rule for regular type placeholders
%where is the mistake in the old unify algorithm?
%when working with equality the problems arise! Free variables should not escape their scope
% Replacing regular type placeholders causes problems related to method calls and capture conversion.
% <X> List<X> same(List<X> a, List<X> b){}
% This program has no correct type. the same method requires
% \begin{lstlisting}
% List<?> f;
% List<?> problem(){
% return same(problem(), problem()) ?: f;
% }
% \end{lstlisting}
% \begin{constraints}
% r <. List<x?>
% r <. List<x?>
% X.List<X> <. r
% \end{constraints}
% %TODO
\unify{} generates wildcard types for constraints of the form $\type{N} \lessdot \tv{a}$ with the \rulename{\generalizeRule} rule. \unify{} generates wildcard types for constraints of the form $\type{N} \lessdot \tv{a}$ with the \rulename{\generalizeRule} rule.
Otherwise only the wildcards already defined in the input constraints will be included in the result. Otherwise only the wildcards already defined in the input constraints will be included in the result.
\rulename{\generalizeRule} attempts to give $\tv{a}$ a more general type by replacing only the type parameters with fresh wildcards. \rulename{\generalizeRule} attempts to give $\tv{a}$ a more general type by replacing only the type parameters with fresh wildcards.
@ -1145,15 +1056,6 @@ need to be handled in a similiar fashion.
The type variable $\tv{b}$ could either be a sub or a supertype of the type $\type{N}$. The type variable $\tv{b}$ could either be a sub or a supertype of the type $\type{N}$.
We have to consider both possibilities. We have to consider both possibilities.
\\[1em] \\[1em]
% The specification of the \unify{} algorithm has only two rules generating $\doteq$-Constraints
% , \rulename{Reduce} and \rulename{Ground}.
% $\doteq$-Constraints and the accompaning substitutions are dangerous respective to the soundness of the algorithm.
% For the soundness proof of the \unify{} algorithm we have to show every generation of equals constraints
% and the subsequent application of the \rulename{subst} rule is correct.
% We try to use them as sparsely as possible to simplify the soundness proof.
% You can notice this at \rulename{Equals} or \rulename{Force}:
% Instead of setting $\type{U} \doteq \type{L}$, we say
% $\type{U} \lessdot \type{L}, \type{L} \lessdot \type{U}$.
\noindent \noindent
\textbf{Step 3} \textbf{Step 3}
@ -1164,23 +1066,6 @@ If $C$ does not contain any wildcard variables the algorithm proceeds with step
\begin{center} \begin{center}
\fbox{ \fbox{
\begin{tabular}[t]{l@{~}l} \begin{tabular}[t]{l@{~}l}
% \rulename{Remove} % kann beim Subst step gemacht werden und ist nur nötig wenn ein a =. T constraint mit wtv(T) > 1 entsteht
% & $
% \begin{array}[c]{@{}ll}
% \begin{array}[c]{l}
% \wildcardEnv \vdash C \\
% % \cup \, \set{ \wtv{a} \lessdot \type{T} }\\
% \hline
% \vspace*{-0.4cm}\\
% \subst{\tv{a}}{\wtv{a}}\wildcardEnv \vdash [\tv{a}/\wtv{a}]C
% \end{array}
% &\begin{array}[c]{l}
% \wtv{a} \in C \\
% \tv{a} \ \text{fresh}
% \end{array}
% \end{array}
% $
% \\\\
\rulename{Remove-Cons} & $ \rulename{Remove-Cons} & $
\begin{array}[c]{l} \begin{array}[c]{l}
\wildcardEnv \vdash C \cup \set{\type{S} \lessdotCC \type{T} } \\ \wildcardEnv \vdash C \cup \set{\type{S} \lessdotCC \type{T} } \\
@ -1192,23 +1077,6 @@ If $C$ does not contain any wildcard variables the algorithm proceeds with step
\end{tabular}} \end{tabular}}
\end{center} \end{center}
% \textbf{Step 4:}
% If there are constraints of the form $(\tv{a} \lessdot \tv{b})$ remaining in the constraint set then
% apply the \rulename{Sub-Elim} rule and start over with step 1.
% Otherwise continue to step 5.
% \begin{center}
% \fbox{
% \begin{tabular}[t]{l@{~}l}
% \rulename{SubElim}
% & $\begin{array}[c]{l}
% \wildcardEnv \vdash C \cup \set{\tv{a} \lessdot \tv{b}}\\
% \hline
% [\tv{a}/\tv{b}]\wildcardEnv \vdash [\tv{a}/\tv{b}]C \cup \set{ \tv{b} \doteq \tv{a} }
% \end{array}
% $
% \end{tabular}}
% \end{center}
\noindent \noindent
\textbf{Step 4:} \textbf{Step 4:}
We apply the rules in figure \ref{fig:cleanup-rules} exhaustively and proceed with step 6. We apply the rules in figure \ref{fig:cleanup-rules} exhaustively and proceed with step 6.
@ -1242,43 +1110,6 @@ C \cup \set{ \tv{a} \doteq \bot }
%\text{length}( \overline{\tv{a} \lessdot \type{T}} ) > 1 %\text{length}( \overline{\tv{a} \lessdot \type{T}} ) > 1
\end{array} \end{array}
\end{array}$ \end{array}$
% \\\\ % The force rule is unnecessary because every type placeholder has an upper bound Object (a <. Object) The match rule eliminates those wildcards
% \rulename{Force} &$
% \begin{array}[c]{@{}ll}
% \begin{array}[c]{l}
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}}
% \vdash C \cup \, \set{
% \tv{a} \lessdot \type{N} } \\
% \hline
% \vspace*{-0.4cm}\\
% \wildcardEnv
% \vdash
% C \cup \, \set{ \tv{a} \lessdot [\type{U}/\rwildcard{X}]\type{N},
% \type{U} \doteq \type{L} }
% \end{array}
% &\begin{array}[c]{l}
% \type{X} \in \text{fv}(\type{N}) \\
% %\Delta' = \Delta \cup \set{\wildcard{X}{\type{U}}{\type{L}}}
% \end{array}
% \end{array}$
% \\\\
% \rulename{FlatOut} &$
% \begin{array}[c]{@{}ll}
% \begin{array}[c]{l}
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}}
% \vdash C \cup \, \set{
% \tv{a} \lessdot \wcNtype{\Delta}{N} } \\
% \hline
% \vspace*{-0.4cm}\\
% \wildcardEnv \cup \set{\wildcard{X}{\type{U}}{\type{L}}}
% \vdash
% C
% \end{array}
% &\begin{array}[c]{l}
% \type{X} \in \text{fv}(\wcNtype{\Delta}{N}) \\
% \tv{a} \notin C , \, \tv{a} \notin \wildcardEnv, \tv{a} \notin \sigma
% \end{array}
% \end{array}$
\end{tabular}} \end{tabular}}
\end{center} \end{center}
\caption{Cleanup rules}\label{fig:cleanup-rules} \caption{Cleanup rules}\label{fig:cleanup-rules}