Initial commit. Applying LipICS style to wildcard paper
This commit is contained in:
commit
8f3d49d70f
319
TIforWildFJ.tex
Normal file
319
TIforWildFJ.tex
Normal file
@ -0,0 +1,319 @@
|
|||||||
|
|
||||||
|
\documentclass[a4paper,UKenglish,cleveref, autoref, thm-restate]{lipics-v2021}
|
||||||
|
%This is a template for producing LIPIcs articles.
|
||||||
|
%See lipics-v2021-authors-guidelines.pdf for further information.
|
||||||
|
%for A4 paper format use option "a4paper", for US-letter use option "letterpaper"
|
||||||
|
%for british hyphenation rules use option "UKenglish", for american hyphenation rules use option "USenglish"
|
||||||
|
%for section-numbered lemmas etc., use "numberwithinsect"
|
||||||
|
%for enabling cleveref support, use "cleveref"
|
||||||
|
%for enabling autoref support, use "autoref"
|
||||||
|
%for anonymousing the authors (e.g. for double-blind review), add "anonymous"
|
||||||
|
%for enabling thm-restate support, use "thm-restate"
|
||||||
|
%for enabling a two-column layout for the author/affilation part (only applicable for > 6 authors), use "authorcolumns"
|
||||||
|
%for producing a PDF according the PDF/A standard, add "pdfa"
|
||||||
|
|
||||||
|
%\pdfoutput=1 %uncomment to ensure pdflatex processing (mandatatory e.g. to submit to arXiv)
|
||||||
|
%\hideLIPIcs %uncomment to remove references to LIPIcs series (logo, DOI, ...), e.g. when preparing a pre-final version to be uploaded to arXiv or another public repository
|
||||||
|
|
||||||
|
%\graphicspath{{./graphics/}}%helpful if your graphic files are in another directory
|
||||||
|
|
||||||
|
\usepackage[T1]{fontenc}
|
||||||
|
|
||||||
|
\usepackage{cite}
|
||||||
|
\usepackage[disable]{todonotes} % [disable]
|
||||||
|
\usepackage[utf8]{inputenc}
|
||||||
|
\usepackage{hyperref}
|
||||||
|
\usepackage{amsmath}
|
||||||
|
\usepackage{amssymb}
|
||||||
|
\usepackage{subcaption}
|
||||||
|
\usepackage{prftree}
|
||||||
|
\usepackage{tabularx}
|
||||||
|
\usepackage{multicol}
|
||||||
|
\usepackage{nicematrix}
|
||||||
|
\usepackage{tikz}
|
||||||
|
\usepackage{soul}
|
||||||
|
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
|
||||||
|
\usepackage{cancel}
|
||||||
|
\usepackage{tcolorbox}
|
||||||
|
|
||||||
|
\input{prolog}
|
||||||
|
|
||||||
|
\bibliographystyle{plainurl}% the mandatory bibstyle
|
||||||
|
|
||||||
|
\title{Global Type Inference for Featherweight Java with Wildcards} %TODO Please add
|
||||||
|
|
||||||
|
%\titlerunning{Dummy short title} %TODO optional, please use if title is longer than one line
|
||||||
|
|
||||||
|
\author{Andreas Stadelmeier}{DHBW Stuttgart, Campus Horb, Germany}{a.stadelmeier@hb.dhbw-stuttgart.de}{}{}%TODO mandatory, please use full name; only 1 author per \author macro; first two parameters are mandatory, other parameters can be empty. Please provide at least the name of the affiliation and the country. The full address is optional. Use additional curly braces to indicate the correct name splitting when the last name consists of multiple name parts.
|
||||||
|
|
||||||
|
\author{Martin Plümicke}{DHBW Stuttgart, Campus Horb, Germany}{pl@dhbw.de}{}{}
|
||||||
|
|
||||||
|
\author{Peter Thiemann}{Universität Freiburg, Institut für Informatik, Germany}{thiemann@informatik.uni-freiburg.de}{}{}
|
||||||
|
|
||||||
|
\authorrunning{A. Stadelmeier and M. Plümicke and P. Thiemann} %TODO mandatory. First: Use abbreviated first/middle names. Second (only in severe cases): Use first author plus 'et al.'
|
||||||
|
|
||||||
|
\Copyright{Andreas Stadelmeier and Martin Plümicke and Peter Thiemann} %TODO mandatory, please use full first names. LIPIcs license is "CC-BY"; http://creativecommons.org/licenses/by/3.0/
|
||||||
|
|
||||||
|
\ccsdesc[500]{Software and its engineering~Language features}
|
||||||
|
%\ccsdesc[300]{Software and its engineering~Syntax}
|
||||||
|
|
||||||
|
\keywords{type inference, Java, subtyping, generics} %TODO mandatory; please add comma-separated list of keywords
|
||||||
|
|
||||||
|
\category{} %optional, e.g. invited paper
|
||||||
|
|
||||||
|
\relatedversion{} %optional, e.g. full version hosted on arXiv, HAL, or other respository/website
|
||||||
|
%\relatedversiondetails[linktext={opt. text shown instead of the URL}, cite=DBLP:books/mk/GrayR93]{Classification (e.g. Full Version, Extended Version, Previous Version}{URL to related version} %linktext and cite are optional
|
||||||
|
|
||||||
|
%\supplement{}%optional, e.g. related research data, source code, ... hosted on a repository like zenodo, figshare, GitHub, ...
|
||||||
|
%\supplementdetails[linktext={opt. text shown instead of the URL}, cite=DBLP:books/mk/GrayR93, subcategory={Description, Subcategory}, swhid={Software Heritage Identifier}]{General Classification (e.g. Software, Dataset, Model, ...)}{URL to related version} %linktext, cite, and subcategory are optional
|
||||||
|
|
||||||
|
%\funding{(Optional) general funding statement \dots}%optional, to capture a funding statement, which applies to all authors. Please enter author specific funding statements as fifth argument of the \author macro.
|
||||||
|
|
||||||
|
%\acknowledgements{I want to thank \dots}%optional
|
||||||
|
|
||||||
|
%\nolinenumbers %uncomment to disable line numbering
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
%Editor-only macros:: begin (do not touch as author)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
\EventEditors{John Q. Open and Joan R. Access}
|
||||||
|
\EventNoEds{2}
|
||||||
|
\EventLongTitle{42nd Conference on Very Important Topics (CVIT 2016)}
|
||||||
|
\EventShortTitle{CVIT 2016}
|
||||||
|
\EventAcronym{CVIT}
|
||||||
|
\EventYear{2016}
|
||||||
|
\EventDate{December 24--27, 2016}
|
||||||
|
\EventLocation{Little Whinging, United Kingdom}
|
||||||
|
\EventLogo{}
|
||||||
|
\SeriesVolume{42}
|
||||||
|
\ArticleNo{23}
|
||||||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
\begin{document}
|
||||||
|
|
||||||
|
\maketitle
|
||||||
|
|
||||||
|
%TODO mandatory: add short abstract of the document
|
||||||
|
\begin{abstract}
|
||||||
|
TODO: Abstract
|
||||||
|
\end{abstract}
|
||||||
|
|
||||||
|
\input{introduction}
|
||||||
|
|
||||||
|
%\input{tRules}
|
||||||
|
|
||||||
|
%\input{tiRules}
|
||||||
|
|
||||||
|
\input{constraints}
|
||||||
|
|
||||||
|
\section{Capture Conversion}
|
||||||
|
The input to our type inference algorithm does not contain let statements.
|
||||||
|
Those are added after computing a type solution.
|
||||||
|
Let statements act as capture conversion and only have to be applied in method calls involving wildcard types.
|
||||||
|
|
||||||
|
\begin{figure}
|
||||||
|
\begin{minipage}{0.45\textwidth}
|
||||||
|
\begin{lstlisting}[style=fgj]
|
||||||
|
<X> List<X> clone(List<X> l);
|
||||||
|
example(p){
|
||||||
|
return clone(p);
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{minipage}%
|
||||||
|
\hfill
|
||||||
|
\begin{minipage}{0.5\textwidth}
|
||||||
|
\begin{lstlisting}[style=tfgj]
|
||||||
|
(*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) example((*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) p){
|
||||||
|
return let x : (*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) = p in
|
||||||
|
clone(x) : (*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*);
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
\end{minipage}
|
||||||
|
\caption{Type inference adding capture conversion}\label{fig:addingLetExample}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
Figure \ref{fig:addingLetExample} shows a let statement getting added to the typed output.
|
||||||
|
The method \texttt{clone} cannot be called with the type $\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$.
|
||||||
|
After a capture conversion \texttt{x} has the type $\exptype{List}{\rwildcard{X}}$ with $\rwildcard{X}$ being a free variable.
|
||||||
|
Afterwards we have to find a supertype of $\exptype{List}{\rwildcard{X}}$, which does not contain free variables
|
||||||
|
($\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$ in this case).
|
||||||
|
|
||||||
|
During the constraint generation step most types are not known yet and are represented by a type placeholder.
|
||||||
|
During a methodcall like the one in the \texttt{example} method in figure \ref{fig:ccExample} the type of the parameter \texttt{p}
|
||||||
|
is not known yet.
|
||||||
|
The type \texttt{List<?>} would be one possibility as a parameter type for \texttt{p}.
|
||||||
|
To make wildcards work for our type inference algorithm \unify{} has to apply capture conversions if necessary.
|
||||||
|
|
||||||
|
The type placeholder $\tv{r}$ is the return type of the \texttt{example} method.
|
||||||
|
One possible type solution is $\tv{p} \doteq \tv{r} \doteq \wctype{\rwildcard{X}}{List}{\rwildcard{X}}$,
|
||||||
|
which leads to:
|
||||||
|
\begin{verbatim}
|
||||||
|
List<?> example(List<?> p){
|
||||||
|
return clone(p);
|
||||||
|
}
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
But by substituting $\tv{p} \doteq \wctype{\rwildcard{X}}{List}{\rwildcard{X}}$ in the constraint
|
||||||
|
$\tv{p} \lessdot \exptype{List}{\wtv{x}}$ leads to
|
||||||
|
$\wctype{\rwildcard{X}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{x}}$.
|
||||||
|
|
||||||
|
To make this typing possible we have to introduce a capture conversion via a let statement:
|
||||||
|
$\texttt{return}\ (\texttt{let}\ \texttt{x} : \wctype{\rwildcard{X}}{List}{\rwildcard{X}} = \texttt{p}\ \texttt{in} \
|
||||||
|
\texttt{clone}\generics{\rwildcard{X}}(x) : \wctype{\rwildcard{X}}{List}{\rwildcard{X}})$
|
||||||
|
|
||||||
|
Inside the let statement the variable \texttt{x} has the type $\exptype{List}{\rwildcard{X}}$
|
||||||
|
|
||||||
|
$
|
||||||
|
\begin{array}[c]{l}
|
||||||
|
\wildcardEnv \vdash C \cup \set{\wtv{a} \doteq \rwildcard{G}}\\
|
||||||
|
\hline
|
||||||
|
[\type{G}/\wtv{a}]\wildcardEnv \vdash [\type{G}/\wtv{a}]C \cup \set{\tv{a} \doteq \type{G}}
|
||||||
|
\end{array}
|
||||||
|
$
|
||||||
|
|
||||||
|
This spawns additional problems.
|
||||||
|
%TODO
|
||||||
|
%we need the constraint set to be a list
|
||||||
|
%not on every constraint CC is allowed. The unify algorithm does not know the context in which a constraint was generated
|
||||||
|
%free type variables cannot leave the scope of the method call
|
||||||
|
|
||||||
|
\begin{figure}
|
||||||
|
\begin{minipage}{0.45\textwidth}
|
||||||
|
\begin{verbatim}
|
||||||
|
<X> List<X> clone(List<X> l){...}
|
||||||
|
|
||||||
|
example(p){
|
||||||
|
return clone(p);
|
||||||
|
}
|
||||||
|
\end{verbatim}
|
||||||
|
\end{minipage}%
|
||||||
|
\hfill
|
||||||
|
\begin{minipage}{0.35\textwidth}
|
||||||
|
\begin{constraintset}
|
||||||
|
\textbf{Constraints:}\\
|
||||||
|
$
|
||||||
|
\tv{p} \lessdot \exptype{List}{\wtv{x}}, \\
|
||||||
|
\tv{p} \lessdot \tv{r}, \\
|
||||||
|
\tv{p} \lessdot \type{Object},
|
||||||
|
\tv{r} \lessdot \type{Object}
|
||||||
|
$
|
||||||
|
\end{constraintset}
|
||||||
|
\end{minipage}
|
||||||
|
|
||||||
|
\caption{Type inference example}\label{fig:ccExample}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
In addition with free variables this leads to unwanted behaviour.
|
||||||
|
Take the constraint
|
||||||
|
$\wctype{\rwildcard{X}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{a}}$ for example.
|
||||||
|
After a capture conversion from $\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$ to $\exptype{List}{\rwildcard{Y}}$ and a substitution $\wtv{a} \doteq \rwildcard{Y}$
|
||||||
|
we get
|
||||||
|
$\exptype{List}{\rwildcard{Y}} \lessdot \exptype{List}{\rwildcard{Y}}$.
|
||||||
|
Which is correct if we apply capture conversion to the left side:
|
||||||
|
$\exptype{List}{\rwildcard{X}} <: \exptype{List}{\rwildcard{X}}$
|
||||||
|
If the input constraints did not intend for this constraint to undergo a capture conversion then \unify{} would produce an invalid
|
||||||
|
type solution due to:
|
||||||
|
$\wctype{\rwildcard{X}}{List}{\rwildcard{X}} \nless: \exptype{List}{\rwildcard{X}}$
|
||||||
|
The reason for this is the \texttt{S-Exists} rule's premise
|
||||||
|
$\text{dom}(\Delta') \cap \text{fv}(\exptype{List}{\rwildcard{X}}) = \emptyset$.
|
||||||
|
|
||||||
|
Additionally free variables are not allowed to leave the scope of a capture conversion
|
||||||
|
introduced by a let statement.
|
||||||
|
%TODO we combat both of this with wildcard type placeholders (? flag)
|
||||||
|
|
||||||
|
Type placeholders which are not flagged as possible free variables ($\wtv{a}$) can never hold a free variable or a type containing free variables.
|
||||||
|
Constraint generation places these standard place holders at method return types and parameter types.
|
||||||
|
\begin{lstlisting}[style=fgj]
|
||||||
|
<X> List<X> clone(List<X> l);
|
||||||
|
(*@$\red{\tv{r}}$@*) example((*@$\red{\tv{p}}$@*) p){
|
||||||
|
return clone(p);
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
This prevents type solutions that contain free variables in parameter and return types.
|
||||||
|
When calling a method which already has a type annotation we have to consider adding a capture conversion in form of a let statement.
|
||||||
|
The constraint $\tv{p} \lessdot \exptype{List}{\wtv{x}}$ signals the \unify{} algorithm that here a capture conversion is possible.
|
||||||
|
$\sigma(\tv{p}) = \wctype{\rwildcard{X}}{List}{\rwildcard{X}}, \sigma(\tv{r}) = \wctype{\rwildcard{X}}{List}{\rwildcard{X}}, $ is a possible solution.
|
||||||
|
But only when adding a capture conversion:
|
||||||
|
\begin{lstlisting}[style=fgj]
|
||||||
|
<X> List<X> clone(List<X> l);
|
||||||
|
(*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) example((*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) p){
|
||||||
|
return let x : (*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*) = p in clone(x) : (*@$\wctype{\rwildcard{X}}{List}{\rwildcard{X}}$@*);
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
\unify{}'s type solution for constraints involving free variables only holds under special circumstances.
|
||||||
|
Constraint $\tv{p} \lessdot \exptype{List}{\wtv{x}}$ only holds when a capture conversion is applied to the left side:
|
||||||
|
$\Delta, \Delta' \vdash CC(\sigma(\tv{p})) <: \sigma(\exptype{List}{\wtv{x}})$
|
||||||
|
and there is a an environment $\Delta'$ holding all type variables used inside the subtype relation.
|
||||||
|
This is done by packing the method call inside a let statement, which performs a capture conversion on all expressions used as parameters.
|
||||||
|
%TODO: Explain (do soundness and TYPE algorithm first)
|
||||||
|
|
||||||
|
A type solution of the \unify{} algorithm only guarantees correct subtyping for constraints not containing free variables.
|
||||||
|
Constraints like $\wctype{\rwildcard{X}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{a}}$ only guarantee
|
||||||
|
$\Delta, \Delta' \vdash \sigma(\wctype{\rwildcard{X}}{List}{\rwildcard{X}}) <: \sigma(\exptype{List}{\wtv{a}})$ when
|
||||||
|
adding a $\Delta'$ environment and applying a capture conversion on the left side.
|
||||||
|
In this case the type solution would be $\tv{a} \to \rwildcard{X}$ leading to:
|
||||||
|
$\Delta, \set{\rwildcard{X}} \vdash \exptype{List}{\rwildcard{X}} <: \exptype{List}{\rwildcard{X}}$
|
||||||
|
|
||||||
|
This is the reason input constraints containing free variables cannot be stored in a set.
|
||||||
|
$\wtv{a} \lessdot \wtv{b}$ is not the same as $\wtv{a} \lessdot \wtv{b}$.
|
||||||
|
Both constraints will end up the same after a substitution for both placeholders $\tv{a}$ and $\tv{b}$.
|
||||||
|
But afterwards a capture conversion is applied, which can generate different types on the left sides.
|
||||||
|
\begin{itemize}
|
||||||
|
\item $\text{CC}(\wctype{\rwildcard{X}}{List}{\rwildcard{X}}) \implies \exptype{List}{\rwildcard{Y}}$
|
||||||
|
\item $\text{CC}(\wctype{\rwildcard{X}}{List}{\rwildcard{X}}) \implies \exptype{List}{\rwildcard{Z}}$
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
|
||||||
|
Also the subtype relation is not symmetric for types involving free type variables.
|
||||||
|
$\type{T} \lessdot \type{S}$ and $\type{S} \lessdot \type{T}$ doesnt mean $\type{T} = \type{S}$, because we apply a capture conversion on every constraint.
|
||||||
|
Only for constraints without free variables symmetry is given.
|
||||||
|
|
||||||
|
% Can untyped methods also get a capture conversion? NO!
|
||||||
|
|
||||||
|
%TODO: Explain why capture conversion is needed (also in respect to martins algorithm)
|
||||||
|
|
||||||
|
\input{Unify}
|
||||||
|
|
||||||
|
\section{Limitations}
|
||||||
|
|
||||||
|
This example does not work:
|
||||||
|
|
||||||
|
\begin{minipage}{0.35\textwidth}
|
||||||
|
\begin{verbatim}
|
||||||
|
class Example{
|
||||||
|
<A> Pair<A,A> make(List<A> l){...}
|
||||||
|
<A> bool compare(Pair<A,A> p){...}
|
||||||
|
|
||||||
|
bool test(List<?> l){
|
||||||
|
return compare(make(l));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\end{verbatim}
|
||||||
|
\end{minipage}%
|
||||||
|
\hfill
|
||||||
|
\begin{minipage}{0.55\textwidth}
|
||||||
|
\begin{constraintset}
|
||||||
|
\textbf{Constraints:}\\
|
||||||
|
$
|
||||||
|
\wctype{\wildcard{A}{\type{Object}}{\bot}}{List}{\rwildcard{A}} \lessdot \exptype{List}{\wtv{x}}, \\
|
||||||
|
\exptype{Pair}{\wtv{x},\wtv{x}} \lessdot \tv{r}, \\
|
||||||
|
\tv{r} \lessdot \exptype{Pair}{\tv{z}, \tv{z}}%,\\
|
||||||
|
%\tv{y} \lessdot \tv{m}
|
||||||
|
$\\
|
||||||
|
\end{constraintset}
|
||||||
|
\end{minipage}
|
||||||
|
|
||||||
|
|
||||||
|
\input{conclusion}
|
||||||
|
|
||||||
|
\include{soundness}
|
||||||
|
%\include{termination}
|
||||||
|
|
||||||
|
\bibliography{martin}
|
||||||
|
|
||||||
|
\appendix
|
||||||
|
%\include{examples}
|
||||||
|
%\input{exampleWildcardParameter}
|
||||||
|
%\input{commonPatternsProof}
|
||||||
|
%\input{appendix}
|
||||||
|
\end{document}
|
35
conclusion.tex
Normal file
35
conclusion.tex
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
\section{Conclusion and Further Work}
|
||||||
|
% we solved the problems given in the introduction (see examples TODO give names to examples)
|
||||||
|
The problems introduced in the openening \ref{challenges} can be solved via our \unify{} algorithm (see examples \ref{example1} and \ref{example2}).
|
||||||
|
As you can see by the given examples our type inference algorithm can calculate
|
||||||
|
type solutions for programs involving wildcards.
|
||||||
|
|
||||||
|
Going further we try to proof soundness and completeness for \unify{}.
|
||||||
|
|
||||||
|
|
||||||
|
% The tricks are:
|
||||||
|
% \begin{itemize}
|
||||||
|
% \item Erasing wildcards by setting $\type{U} \doteq \type{L}$
|
||||||
|
% \end{itemize}
|
||||||
|
|
||||||
|
% Improvements:
|
||||||
|
% \begin{itemize}
|
||||||
|
% \item Straightforward algorithm. Less data types used. Only two sort of constraints.
|
||||||
|
% \end{itemize}
|
||||||
|
|
||||||
|
% \subsection{Problems}
|
||||||
|
% The algorithm is lacking completeness.
|
||||||
|
|
||||||
|
% \begin{verbatim}
|
||||||
|
% <A> void m(List<A> a){}
|
||||||
|
|
||||||
|
% m2(l){
|
||||||
|
% l.add("String");
|
||||||
|
% }
|
||||||
|
% \end{verbatim}
|
||||||
|
|
||||||
|
%COnstraints:
|
||||||
|
%l <. List<a>
|
||||||
|
%String <. a
|
||||||
|
|
||||||
|
|
337
constraints.tex
Normal file
337
constraints.tex
Normal file
@ -0,0 +1,337 @@
|
|||||||
|
|
||||||
|
\section{Constraint generation}
|
||||||
|
Our type inference algorithm is split into two parts.
|
||||||
|
A constraint generation step \textbf{TYPE} and a \unify{} step.
|
||||||
|
|
||||||
|
Method names are not unique.
|
||||||
|
It is possible to define the same method in multiple classes.
|
||||||
|
The \TYPE{} algorithm accounts for that by generating Or-Constraints.
|
||||||
|
This can lead to multiple possible solutions.
|
||||||
|
|
||||||
|
%\subsection{Well-Formedness}
|
||||||
|
|
||||||
|
|
||||||
|
%Why do we need a constraint generation step?
|
||||||
|
%% The problem is NP-Hard
|
||||||
|
%% a method call, does not know which type it will produce
|
||||||
|
%% depending on its type the
|
||||||
|
|
||||||
|
%NO equals constraints during the constraint generation step!
|
||||||
|
\begin{figure}[tp]
|
||||||
|
\begin{align*}
|
||||||
|
% Type
|
||||||
|
\type{T}, \type{U} &::= \tv{a} \mid \wtv{a} \mid \mv{X} \mid {\wcNtype{\Delta}{N}} && \text{types and type placeholders}\\
|
||||||
|
\type{N} &::= \exptype{C}{\il{T}} && \text{class type (with type variables)} \\
|
||||||
|
% Constraints
|
||||||
|
\simpleCons &::= \type{T} \lessdot \type{U} && \text{subtype constraint}\\
|
||||||
|
\orCons{} &::= \set{\set{\overline{\simpleCons_1}}, \ldots, \set{\overline{\simpleCons_n}}} && \text{or-constraint}\\
|
||||||
|
\constraint &::= \simpleCons \mid \orCons && \text{constraint}\\
|
||||||
|
\consSet &::= \set{\constraints} && \text{constraint set}\\
|
||||||
|
% Method assumptions:
|
||||||
|
\methodAssumption &::= \exptype{C}{\ol{X} \triangleleft \ol{N}}.\texttt{m} : \exptype{}{\ol{Y}
|
||||||
|
\triangleleft \ol{P}}\ \ol{\type{T}} \to \type{T} &&
|
||||||
|
\text{method
|
||||||
|
type assumption}\\
|
||||||
|
\localVarAssumption &::= \texttt{x} : \itype{T} && \text{parameter
|
||||||
|
assumption}\\
|
||||||
|
\mtypeEnvironment & ::= \overline{\methodAssumption} &
|
||||||
|
& \text{method type environment} \\
|
||||||
|
\typeAssumptionsSymbol &::= ({\mtypeEnvironment} ; \overline{\localVarAssumption})
|
||||||
|
\end{align*}
|
||||||
|
\caption{Syntax of constraints and type assumptions.}
|
||||||
|
\label{fig:syntax-constraints}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
\begin{figure}[tp]
|
||||||
|
\begin{gather*}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\fjtype & ({\mtypeEnvironment}, \mathtt{class } \ \exptype{C}{\ol{X} \triangleleft \ol{N}} \ \mathtt{ extends } \ \mathtt{N \{ \overline{T} \ \overline{f}; \, K \, \overline{M} \}}) =\\
|
||||||
|
& \begin{array}{ll@{}l}
|
||||||
|
\textbf{let} & \forall \texttt{m} \in \ol{M}: \tv{a}_\texttt{m}, \ol{\tv{a}_m} \ \text{fresh} \\
|
||||||
|
& \ol{\methodAssumption} = \begin{array}[t]{l}
|
||||||
|
\set{ \mv{m'} : (\exptype{C}{\ol{X} \triangleleft \ol{N}} \to \ol{\tv{a}} \to \tv{a}) \mid
|
||||||
|
\mv{m'} \in \ol{M} \setminus \set{\mv{m}}, \, \tv{a}\, \ol{\tv{a}}\ \text{fresh} } \\
|
||||||
|
\ \cup \, \set{\mv{m} : (\exptype{C}{\ol{X} \triangleleft \ol{N}} \to \ol{\tv{a}_m} \to \tv{a}_\mv{m})}
|
||||||
|
\end{array}
|
||||||
|
\\
|
||||||
|
& C_m = \typeExpr(\mtypeEnvironment \cup \set{\mv{this} :
|
||||||
|
\exptype{C}{\ol{X}} , \, \ol{x} : \ol{\tv{a}_m} }, \texttt{e}, \tv{a}_\texttt{m}) \\
|
||||||
|
\textbf{in}
|
||||||
|
& { ( \mtypeEnvironment \cup \ol{\methodAssumption}, \,
|
||||||
|
\bigcup_{\texttt{m} \in \ol{M}} C_m )
|
||||||
|
}
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{gather*}
|
||||||
|
\caption{Constraint generation for classes}
|
||||||
|
\label{fig:constraints-for-classes}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
% \textbf{Method Assumptions}
|
||||||
|
|
||||||
|
% %$\Pi$ is a set of method assumptions used by the $\fjtype{}$ algorithm.
|
||||||
|
|
||||||
|
% % \begin{verbatim}
|
||||||
|
% % class Example<X> {
|
||||||
|
% % <Y> Y m(Example<Y> p){ ... }
|
||||||
|
% % }
|
||||||
|
% % \end{verbatim}
|
||||||
|
|
||||||
|
% In Featherweight Java a method type is bound to a specific class.
|
||||||
|
% The class \texttt{Example} shown above contains one method \texttt{m}:
|
||||||
|
|
||||||
|
% \begin{displaymath}
|
||||||
|
% \textit{mtype}({\texttt{m}, \exptype{Example}{\type{X}}}) = \generics{\type{Y}} \ \exptype{Example}{\type{Y}} \to \type{Y}
|
||||||
|
% \end{displaymath}
|
||||||
|
|
||||||
|
% $\Pi$ is a set of method assumptions used by the $\fjtype{}$ algorithm.
|
||||||
|
% It's a map of method types to method names.
|
||||||
|
% Every method name has a set of possible types,
|
||||||
|
% because there could be more than one method with the same name in a program consisting out of multiple classes.
|
||||||
|
% To simplify the syntax of method assumptions, we add the inheriting class type to the parameter list:
|
||||||
|
|
||||||
|
% \begin{displaymath}
|
||||||
|
% \Pi = \set{ \texttt{m} : \generics{\type{X}, \type{Y}} \ (\exptype{Example}{\type{X}}, \exptype{Example}{\type{Y}}) \to \type{Y}}
|
||||||
|
% \end{displaymath}
|
||||||
|
|
||||||
|
% \begin{verbatim}
|
||||||
|
% class Example<X> { }
|
||||||
|
|
||||||
|
% <X, Y> Y m(Example<X> this, Example<Y> p){ ... }
|
||||||
|
% \end{verbatim}
|
||||||
|
|
||||||
|
The constraint generation step is the same as the one for regular Generic Featherweight Java.
|
||||||
|
Wildcard types are not used during the constraint generation step and have no influence on it.
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} &({\mtypeEnvironment} , \texttt{e}.\texttt{f}, \tv{a}) = \\
|
||||||
|
& \begin{array}{ll}
|
||||||
|
\textbf{let}
|
||||||
|
& \tv{r} \ \text{fresh} \\
|
||||||
|
& \consSet_R = \typeExpr({\mtypeEnvironment}, \texttt{e}, \tv{r})\\
|
||||||
|
& \constraint = \begin{array}[t]{@{}l@{}l}
|
||||||
|
\orCons\set{
|
||||||
|
\set{ &
|
||||||
|
\tv{r} \lessdot \exptype{C}{\ol{\wtv{a}}} ,
|
||||||
|
[\overline{\wtv{a}}/\ol{X}]\type{T} \lessdot \tv{a} , \ol{\tv{a}} \lessdot [\overline{\tv{a}}/\ol{X}]\ol{N}
|
||||||
|
} \\
|
||||||
|
& \quad \mid \mv{T}\ \mv{f} \in \texttt{class}\ \exptype{C}{\ol{X} \triangleleft \ol{N}} \set{ \ol{T\ f}; \ldots}
|
||||||
|
, \, \overline{\wtv{a}} \text{ fresh}
|
||||||
|
}\end{array}\\
|
||||||
|
{\mathbf{in}} & {
|
||||||
|
\consSet_R \cup \set{\constraint}}
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
The set of method assumptions returned by the \textit{mtypes} function is used to generate the constraints for a method call expression:
|
||||||
|
|
||||||
|
There are two kinds of method calls.
|
||||||
|
The ones to already typed methods and calls to untyped methods.
|
||||||
|
|
||||||
|
%Soundness: TODO
|
||||||
|
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} & ({\mtypeEnvironment} , \texttt{e}.\mathtt{m}(\overline{\texttt{e}}), \tv{a} ) = \\
|
||||||
|
& \begin{array}{ll}
|
||||||
|
\textbf{let}
|
||||||
|
& \tv{r}, \ol{\tv{r}} \text{ fresh} \\
|
||||||
|
& \consSet_R = \typeExpr(({\mtypeEnvironment} ;
|
||||||
|
\overline{\localVarAssumption}), \texttt{e}, \tv{r})\\
|
||||||
|
& \overline{\consSet} = \typeExpr({\mtypeEnvironment}, \ol{e}, \ol{\tv{r}}) \\
|
||||||
|
& \begin{array}{@{}l@{}l}
|
||||||
|
\constraint = \orCons\set{ &
|
||||||
|
\begin{array}[t]{l}
|
||||||
|
\{ \tv{r} \lessdot \exptype{C}{\ol{\wtv{a}}},
|
||||||
|
\overline{\tv{r}} \lessdot \ol{T},
|
||||||
|
\type{T} \lessdot \tv{a},
|
||||||
|
\overline{\wtv{a}} \lessdot \ol{N} \}
|
||||||
|
\end{array}\\
|
||||||
|
& \ |\
|
||||||
|
(\exptype{C}{\ol{X} \triangleleft \ol{N}}.\texttt{m} : \overline{\type{T}} \to \type{T}) \in
|
||||||
|
{\mtypeEnvironment}
|
||||||
|
, \, \overline{\wtv{a}} \text{ fresh} }
|
||||||
|
\end{array}\\
|
||||||
|
\mathbf{in} & \consSet_R \cup \overline{\consSet} \cup \constraint
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} & ({\mtypeEnvironment} , \texttt{e}.\mathtt{m}(\overline{\texttt{e}}), \tv{a} ) = \\
|
||||||
|
& \begin{array}{ll}
|
||||||
|
\textbf{let}
|
||||||
|
& \tv{r}, \ol{\tv{r}} \text{ fresh} \\
|
||||||
|
& \consSet_R = \typeExpr(({\mtypeEnvironment} ;
|
||||||
|
\overline{\localVarAssumption}), \texttt{e}, \tv{r})\\
|
||||||
|
& \overline{\consSet} = \typeExpr({\mtypeEnvironment}, \ol{e}, \ol{\tv{r}}) \\
|
||||||
|
& \begin{array}{@{}l@{}l}
|
||||||
|
\constraint = \orCons\set{ &
|
||||||
|
\begin{array}[t]{l}
|
||||||
|
[\overline{\wtv{a}}/\ol{Y}] [\overline{\wtv{b}}/\ol{X}] \{ \tv{r} \lessdot \exptype{C}{\ol{X}},
|
||||||
|
\overline{\tv{r}} \lessdot \ol{T},
|
||||||
|
\type{T} \lessdot \tv{a},
|
||||||
|
\overline{\wtv{a}} \lessdot \ol{N},
|
||||||
|
\overline{\wtv{b}} \lessdot \ol{N'} \}
|
||||||
|
\end{array}\\
|
||||||
|
& \ |\
|
||||||
|
(\exptype{C}{\ol{X} \triangleleft \ol{N}}.\texttt{m} : \generics{\ol{Y} \triangleleft \ol{N}}\overline{\type{T}} \to \type{T}) \in
|
||||||
|
{\mtypeEnvironment}
|
||||||
|
, \, \overline{\wtv{a}} \text{ fresh}, \, \overline{\wtv{b}} \text{ fresh} }
|
||||||
|
\end{array}\\
|
||||||
|
\mathbf{in} & \consSet_R \cup \overline{\consSet} \cup \constraint
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
\\[1em]
|
||||||
|
\noindent
|
||||||
|
\textbf{Example:}
|
||||||
|
\begin{verbatim}
|
||||||
|
class Class1{
|
||||||
|
<X> X m(List<X> lx, List<? extends X> lt){ ... }
|
||||||
|
List<? extends String> wGet(){ ... }
|
||||||
|
List<String> get() { ... }
|
||||||
|
}
|
||||||
|
|
||||||
|
class Class2{
|
||||||
|
example(c1){
|
||||||
|
return c1.m(c1.get(), c1.wGet());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\end{verbatim}
|
||||||
|
%This example comes with predefined type annotations.
|
||||||
|
We assume the class \texttt{Class1} has already been processed by our type inference algorithm,
|
||||||
|
which has lead to the given type annotations for \texttt{Class1}.
|
||||||
|
Now we call the $\fjtype{}$ function with the class \texttt{Class2} and the method assumptions for the preceeding class:
|
||||||
|
\begin{displaymath}
|
||||||
|
\mtypeEnvironment = \left\{\begin{array}{l}
|
||||||
|
\type{Class1}.\texttt{m}: \generics{\type{X} \triangleleft \type{Object}} \
|
||||||
|
(\exptype{List}{\type{X}}, \, \wctype{\wildcard{A}{\type{X}}{\bot}}{List}{\wildcard{A}{\type{X}}{\bot}}) \to \type{X}, \\
|
||||||
|
\type{Class1}.\texttt{wGet}: () \to \wctype{\wildcard{A}{\type{Object}}{\type{String}}}{List}{\wildcard{A}{\type{Object}}{\type{String}}}, \\
|
||||||
|
\type{Class1}.\texttt{get}: () \to \exptype{List}{\type{String}}
|
||||||
|
\end{array} \right\}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
The result of the $\typeExpr{}$ function is the constraint set
|
||||||
|
\begin{displaymath}
|
||||||
|
C = \left\{ \begin{array}{l}
|
||||||
|
\tv{c1} \lessdot \type{Class1}, \\
|
||||||
|
\tv{p1} \lessdot \exptype{List}{\wtv{x}}, \\
|
||||||
|
\exptype{List}{\type{String}} \lessdot \tv{p1}, \\
|
||||||
|
\tv{p2} \lessdot \wctype{\wildcard{A}{\wtv{x}}{\bot}}{List}{\rwildcard{A}}, \\
|
||||||
|
\wctype{\wildcard{A}{\type{String}}{\bot}}{List}{\rwildcard{A}} \lessdot \tv{p2}
|
||||||
|
\end{array} \right\}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
|
||||||
|
The first parameter of a method assumption is the receiver type $\type{T}_r$.
|
||||||
|
\texttt{Class1} for this example.
|
||||||
|
Therefore the $(\tv{c1} \lessdot \type{Class1})$ constraint.
|
||||||
|
The type variable $\tv{c1}$ is assigned to the parameter \texttt{c1} of the \texttt{example} method.
|
||||||
|
|
||||||
|
or a simplified version:
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
C = \left\{ \begin{array}{l}
|
||||||
|
\tv{c1} \lessdot \type{Class1}, \\
|
||||||
|
\exptype{List}{\type{String}}
|
||||||
|
\lessdot \exptype{List}{\wtv{x}}, \\
|
||||||
|
\wctype{\wildcard{A}{\type{String}}{\bot}}{List}{\rwildcard{A}}
|
||||||
|
\lessdot \wctype{\wildcard{A}{\wtv{x}}{\bot}}{List}{\rwildcard{A}}
|
||||||
|
\end{array} \right\}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
|
||||||
|
$\wtv{x}$ is a type variable we use for the generic $\type{X}$. It is flagged as a free type variable.
|
||||||
|
%TODO: Do an example where wildcards are inserted and we need capture conversion
|
||||||
|
%\unify{} returns the solution $(\sigma = \set{ \tv{x} \to \type{String} })$
|
||||||
|
|
||||||
|
% \\[1em]
|
||||||
|
% \noindent
|
||||||
|
% \textbf{Example:}
|
||||||
|
% \begin{verbatim}
|
||||||
|
% class Class1 {
|
||||||
|
% <X> Pair<X, X> make(List<X> l){ ... }
|
||||||
|
% <Y> boolean compare(Pair<Y,Y> p) { ... }
|
||||||
|
|
||||||
|
% example(l){
|
||||||
|
% return compare(make(l));
|
||||||
|
% }
|
||||||
|
% }
|
||||||
|
% \end{verbatim}
|
||||||
|
|
||||||
|
% The method call \texttt{make(l)} generates the constraints
|
||||||
|
% \begin{displaymath}
|
||||||
|
% \tv{l} \lessdot \exptype{List}{\tv{x}}, \exptype{Pair}{\tv{x}, \tv{x}} \lessdot \tv{m}
|
||||||
|
% \end{displaymath}
|
||||||
|
% with $\tv{l}$ being the type placeholder for the variable \texttt{l}
|
||||||
|
% and $\tv{m}$ is the type variable for the return type of the method call.
|
||||||
|
% $\tv{m}$ is then used as the parameter for the \texttt{compare} method:
|
||||||
|
% \begin{displaymath}
|
||||||
|
% \tv{m} \lessdot \exptype{Pair}{\tv{y}, \tv{y}}
|
||||||
|
% \end{displaymath}
|
||||||
|
% Note the conversion of the generic parameters \texttt{X} and \texttt{Y} to type variables $\tv{x}$ and $\tv{y}$.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% Step 3 of the \unify{} algorithm has to look for every possible supertype of $\exptype{Pair}{\tv{x}, \tv{x}}$,
|
||||||
|
% when processing the $\exptype{Pair}{\tv{x}, \tv{x}} \lessdot \tv{m}$ constraint.
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} &({\mtypeEnvironment} , e_1 \elvis{} e_2, \tv{a}) = \\
|
||||||
|
& \begin{array}{ll}
|
||||||
|
\textbf{let}
|
||||||
|
& \tv{r}_1, \tv{r}_2 \ \text{fresh} \\
|
||||||
|
& \consSet_1 = \typeExpr({\mtypeEnvironment}, e_1, \tv{r}_2)\\
|
||||||
|
& \consSet_2 = \typeExpr({\mtypeEnvironment}, e_2, \tv{r}_2)\\
|
||||||
|
{\mathbf{in}} & {
|
||||||
|
\consSet_1 \cup \consSet_2 \cup
|
||||||
|
\set{\tv{r}_1 \lessdot \tv{a}, \tv{r}_2 \lessdot \tv{a}}}
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
%We could skip wfresh here:
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} &({\mtypeEnvironment} , x, \tv{a}) =
|
||||||
|
\mtypeEnvironment(x)
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\begin{array}{@{}l@{}l}
|
||||||
|
\typeExpr{} &({\mtypeEnvironment} , \texttt{new}\ \type{C}(\overline{e}), \tv{a}) = \\
|
||||||
|
& \begin{array}{ll}
|
||||||
|
\textbf{let}
|
||||||
|
& \ol{\tv{r}} \ \text{fresh} \\
|
||||||
|
& \overline{\consSet} = \typeExpr({\mtypeEnvironment}, \overline{e}, \ol{\tv{r}}) \\
|
||||||
|
& C = \set{\ol{\tv{r}} \lessdot [\ol{\tv{a}}/\ol{X}]\ol{T}, \ol{\tv{a}} \lessdot \ol{N} \mid \texttt{class}\ \exptype{C}{\ol{X} \triangleleft \ol{N}} \set{ \ol{T\ f}; \ldots}} \\
|
||||||
|
{\mathbf{in}} & {
|
||||||
|
\overline{\consSet} \cup
|
||||||
|
\set{\tv{a} \doteq \exptype{C}{\ol{a}}}}
|
||||||
|
\end{array}
|
||||||
|
\end{array}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
% Problem:
|
||||||
|
% <X, A extends List<X>> void t2(List<A> l){}
|
||||||
|
|
||||||
|
% void test(List<List<?>> l){
|
||||||
|
% t2(l);
|
||||||
|
% }
|
||||||
|
% Problem:
|
||||||
|
% List<Y.List<Y>> <. List<a>, a <. List<x>
|
||||||
|
% Y.List<Y> =. a
|
||||||
|
% Z.List<Z> <. List<x>
|
||||||
|
|
||||||
|
% These constraints should fail!
|
||||||
|
|
||||||
|
\section{Result Generation}
|
||||||
|
If \unify{} returns atleast one type solution $(\Delta, \sigma)$
|
||||||
|
the last step of the type inference algorithm is to generate a typed class.
|
222
introduction.tex
Normal file
222
introduction.tex
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
|
||||||
|
\section{Type Inference for Java}
|
||||||
|
%The goal is to find a correct typing for a given Java program.
|
||||||
|
Type inference for Java has many use cases and could be used to help programmers by inserting correct types for them,
|
||||||
|
Finding better type solutions for already typed Java programs (for example more generical ones),
|
||||||
|
or allowing to write typeless Java code which is then type infered and thereby type checked by our algorithm.
|
||||||
|
The algorithm proposed in this paper can determine a correct typing for the untyped Java source code example shown in figure \ref{fig:intro-example-typeless}.
|
||||||
|
Our algorithm is also capable of finding solutions involving wildcards as shown in figure \ref{fig:intro-example-typed}.
|
||||||
|
|
||||||
|
This paper extends a type inference algorithm for Featherweight Java (\cite{TIforFGJ}) by adding wildcards.
|
||||||
|
The last step to create a type inference algorithm compatible to the Java type system.
|
||||||
|
The algorithm is a modified version of the \unify{} algorithm presented in \cite{plue09_1}.
|
||||||
|
|
||||||
|
%TODO
|
||||||
|
% The goal is to proof soundness in respect to the type rules introduced by \cite{aModelForJavaWithWildcards}
|
||||||
|
% and \cite{WildcardsNeedWitnessProtection}.
|
||||||
|
|
||||||
|
\begin{figure}[tp]
|
||||||
|
\begin{subfigure}[t]{0.49\linewidth}
|
||||||
|
\begin{lstlisting}[style=fgj]
|
||||||
|
genList() {
|
||||||
|
if( ... ) {
|
||||||
|
return new List<String>();
|
||||||
|
} else {
|
||||||
|
return new List<Integer>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
\caption{Java method with missing return type}
|
||||||
|
\label{fig:intro-example-typeless}
|
||||||
|
\end{subfigure}
|
||||||
|
~
|
||||||
|
\begin{subfigure}[t]{0.49\linewidth}
|
||||||
|
\begin{lstlisting}[style=tfgj]
|
||||||
|
List<?> genList() {
|
||||||
|
if( ... ) {
|
||||||
|
return new List<String>();
|
||||||
|
} else {
|
||||||
|
return new List<Integer>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
\end{lstlisting}
|
||||||
|
\caption{Correct type}
|
||||||
|
\label{fig:intro-example-typed}
|
||||||
|
\end{subfigure}
|
||||||
|
%\caption{Example code}
|
||||||
|
%\label{fig:intro-example-code}
|
||||||
|
\end{figure}
|
||||||
|
|
||||||
|
% \subsection{Wildcards}
|
||||||
|
% Java subtyping involving generics is invariant.
|
||||||
|
% For example \texttt{List<String>} is not a subtype of \texttt{List<Object>}.
|
||||||
|
% %Wildcards introduce variance by allowing \texttt{List<String>} to be a subtype of \texttt{List<?>}.
|
||||||
|
|
||||||
|
% \texttt{List<Object>} is not a valid return type for the method \texttt{genList}.
|
||||||
|
% The type inference algorithm has to find the correct type involving wildcards (\texttt{List<?>}).
|
||||||
|
|
||||||
|
\subsection{Java Wildcards}
|
||||||
|
Wildcards add variance to Java type parameters.
|
||||||
|
In Java a \texttt{List<String>} is not a subtype of \texttt{List<Object>}
|
||||||
|
even though it seems intuitive with \texttt{String} being a subtype of \texttt{Object}.
|
||||||
|
Here wildcards come into play.
|
||||||
|
|
||||||
|
$\exptype{List}{String} <: \wctype{\wildcard{X}{\bot}{\type{Object}}}{List}{\rwildcard{X}}$
|
||||||
|
means \texttt{List<String>} is a subtype of \texttt{List<? extend Object>}.
|
||||||
|
The syntax used here allows for wildcard parameters to have a name, an uppper and lower bound at the same time,
|
||||||
|
and a type they are bound to.
|
||||||
|
In this case the name is $\rwildcard{X}$ and it's bound to the the type \texttt{List}.
|
||||||
|
% Additionally they can hold a upper or lower bound restriction like \texttt{List<? super String>}.
|
||||||
|
% Our representation of this type is: $\wctype{\wildcard{X}{\type{String}}{\type{Object}}}{List}{\rwildcard{X}}$
|
||||||
|
% Every wildcard has a name ($\rwildcard{X}$ in this case) and an upper and lower bound (respectively \texttt{Object} and \texttt{String}).
|
||||||
|
|
||||||
|
|
||||||
|
\subsection{Constraints}
|
||||||
|
|
||||||
|
Constraints consist of normal types and type variables:
|
||||||
|
|
||||||
|
$\exptype{List}{String} \lessdot \tv{a}, \exptype{List}{Integer} \lessdot \tv{a}$
|
||||||
|
|
||||||
|
\noindent
|
||||||
|
Those two constraints imply that we have to find a type replacement for type variable $\tv{a}$,
|
||||||
|
which is a supertype of $\exptype{List}{String}$ aswell as $\exptype{List}{Integer}$.
|
||||||
|
This paper describes a \unify{} algorithm to solve these constraints and calculate a type solution $\sigma$.
|
||||||
|
For the example above a correct solution would be $\sigma(\tv{a}) = \wctype{\rwildcard{X}}{List}{\rwildcard{X}}$.
|
||||||
|
|
||||||
|
\subsection{Challenges}\label{challenges}
|
||||||
|
The introduction of wildcards adds additional challenges.
|
||||||
|
% we cannot replace every type variable with a wildcard
|
||||||
|
Type variables can also be used as type parameters, for example
|
||||||
|
$\exptype{List}{String} \lessdot \exptype{List}{\tv{a}}$.
|
||||||
|
A problem arises when replacing type variables with wildcards.
|
||||||
|
|
||||||
|
% Wildcards are not reflexive.
|
||||||
|
% ( on the equals property ), every wildcard has to be capture converted when leaving its scope
|
||||||
|
|
||||||
|
% do not substitute free type variables
|
||||||
|
|
||||||
|
Lets have a look at two examples:
|
||||||
|
\begin{itemize}
|
||||||
|
\item \begin{example} \label{intro-example1}
|
||||||
|
The first one is a valid Java program.
|
||||||
|
The type \texttt{List<? super String>} is \textit{capture converted} to a fresh type variable $\rwildcard{X}$
|
||||||
|
which is used as the generic method parameter \texttt{A}.
|
||||||
|
|
||||||
|
Java uses capture conversion to replace the generic \texttt{A} by a capture converted version of the
|
||||||
|
\texttt{? super String} wildcard.
|
||||||
|
Knowing that the type \texttt{String} is a subtype of any type the wildcard \texttt{? super String} can inherit
|
||||||
|
it is safe to pass \texttt{"String"} for the first parameter of the function.
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
<A> List<A> add(A a, List<A> la) {}
|
||||||
|
|
||||||
|
List<? super String> list = ...;
|
||||||
|
add("String", list);
|
||||||
|
\end{verbatim}
|
||||||
|
The constraints representing this code snippet are:
|
||||||
|
|
||||||
|
\begin{displaymath}
|
||||||
|
\type{String} \lessdot \wtv{a},\,
|
||||||
|
\wctype{\wildcard{X}{\type{Object}}{\type{String}}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{a}}
|
||||||
|
\end{displaymath}
|
||||||
|
|
||||||
|
Here $\sigma(\tv{a}) = \rwildcard{X}$ is a valid solution.
|
||||||
|
\end{example}
|
||||||
|
|
||||||
|
\item \begin{example}\label{intro-example2}
|
||||||
|
This example displays an incorrect Java program.
|
||||||
|
The method call to \texttt{concat} with two wildcard lists is unsound.
|
||||||
|
Each list could be of a different kind and therefore the \texttt{concat} cannot succeed.
|
||||||
|
\begin{verbatim}
|
||||||
|
<A> List<A> concat(List<A> l1, List<A> l2) {}
|
||||||
|
|
||||||
|
List<?> list = ... ;
|
||||||
|
concat(list, list);
|
||||||
|
\end{verbatim}
|
||||||
|
The constraints for this example are:
|
||||||
|
|
||||||
|
$\wctype{\wildcard{X}{\type{Object}}{\bot}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{a}}, \\
|
||||||
|
\wctype{\wildcard{X}{\type{Object}}{\bot}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\wtv{a}}$
|
||||||
|
%$\exptype{List}{?} \lessdot \exptype{List}{\tv{a}},
|
||||||
|
%\exptype{List}{?} \lessdot \exptype{List}{\tv{a}}$
|
||||||
|
|
||||||
|
Remember that the given constraints cannot have a valid solution.
|
||||||
|
%This is due to the fact that the wildcard variables cannot leave their scope.
|
||||||
|
In this example the \unify{} algorithm should not replace $\tv{a}$ with the captured wildcard $\rwildcard{X}$.
|
||||||
|
\end{example}
|
||||||
|
|
||||||
|
\end{itemize}
|
||||||
|
|
||||||
|
The \unify{} algorithm only sees the constraints with no information about the program they originated from.
|
||||||
|
The main challenge was to find an algorithm which computes $\sigma(\tv{a}) = \rwildcard{X}$ for example \ref{intro-example1} but not for example \ref{intro-example2}.
|
||||||
|
% Solution: A type variable can only take one type and not a wildcard type.
|
||||||
|
% A wildcard type is only treated like a wildcard while his definition is in scope (during the reduce rule)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% \subsection{Capture Conversion}
|
||||||
|
% \begin{verbatim}
|
||||||
|
% <A> List<A> add(A a, List<A> la) {}
|
||||||
|
|
||||||
|
% List<? super String> list = ...;
|
||||||
|
|
||||||
|
% add("String", list);
|
||||||
|
% \end{verbatim}
|
||||||
|
% The constraints representig this code snippet are:
|
||||||
|
% $\type{String} \lessdot \tv{a},
|
||||||
|
% \wctype{\wildcard{X}{\type{Object}}{\type{String}}}{List}{\rwildcard{X}} \lessdot \exptype{List}{\tv{a}}$
|
||||||
|
% Under the hood the typechecker has to find a replacement for the generic method parameter \texttt{A}.
|
||||||
|
|
||||||
|
% Java allows a method \texttt{<A> List<A> add(A a, List<A> la)} to be called with
|
||||||
|
% \texttt{String} and \texttt{List<? super String>}.
|
||||||
|
% A naive approach would be to treat wildcards like any other type and replace \texttt{A}
|
||||||
|
% with \texttt{? super String}.
|
||||||
|
% Generating the method type \texttt{List<? super String> add(? super String a, List<? super String> la)}.
|
||||||
|
|
||||||
|
% This does not work for a method like
|
||||||
|
% \texttt{<A> void merge(List<A> l1, List<A> l2)}.
|
||||||
|
% It is crucial for this method to be called with two lists of the same type.
|
||||||
|
% Substituting a wildcard for the generic \texttt{A} leads to
|
||||||
|
% \texttt{void merge(List<?> l1, List<?> l2)},
|
||||||
|
% which is unsound.
|
||||||
|
|
||||||
|
% Capture conversion utilizes the fact that instantiated classes always have an actual type.
|
||||||
|
% \texttt{new List<String>()} and \texttt{new List<Object>()} are
|
||||||
|
% valid instances.
|
||||||
|
% \texttt{new List<? super String>()} is not.
|
||||||
|
% Every type $\wcNtype{\Delta}{N}$ contains an underlying instance of a type $\type{N}$.
|
||||||
|
% Knowing this fact allows to make additional assumptions.
|
||||||
|
|
||||||
|
% \wildFJ{} type rules allow for generic parameters to be replaced by wildcard types.
|
||||||
|
% This is possible because wildcards are split into two parts.
|
||||||
|
% Their declaration at the environment part $\Delta$ of a type $\wcNtype{\Delta}{N}$
|
||||||
|
% and their actual use in the body of the type $\type{N}$.
|
||||||
|
% Replacing the generic \texttt{A} in the type \texttt{List<A>}
|
||||||
|
% by a wildcard $\type{X}$ will not generate the type \texttt{List<?>}.
|
||||||
|
% \texttt{List<?>} is the equivalent of $\wctype{\wildcard{X}{\type{Object}}{\bot}}{List}{\type{X}}$.
|
||||||
|
% The generated type here is $\exptype{List}{\type{X}}$, which has different subtype properties.
|
||||||
|
% There is no information about what the type $\exptype{List}{\type{X}}$ actually is.
|
||||||
|
% It has itself as a subtype and for example a type like $\exptype{ArrayList}{\type{X}}$.
|
||||||
|
|
||||||
|
% A method call for example only works with values, which are correct instances of classes.
|
||||||
|
% Capture conversion automatically converts wildcard type to a concrete class type,
|
||||||
|
% which then can be used as a parameter for a method call.
|
||||||
|
|
||||||
|
% In \wildFJ{} this is done via let statements.
|
||||||
|
|
||||||
|
% Written in FJ syntax: $\type{N}$ is a initialized type and
|
||||||
|
% $\wcNtype{\Delta}{N}$ is a type containing wildcards $\Delta$.
|
||||||
|
|
||||||
|
% It is crucial for the wildcard names to never be used twice.
|
||||||
|
% Otherwise the members of a list with type $\wctype{\type{X}}{List}{\type{X}}$ and
|
||||||
|
% a list with type $\wctype{\type{X}}{List}{\type{X}}$ would be the same. $\type{X}$ in fact.
|
||||||
|
|
||||||
|
% The let statement adds wildcards to the environment.
|
||||||
|
% Those wildcards are not allowed to escape the scope of the let statement.
|
||||||
|
% Which is a problem for our type inference algorithm.
|
||||||
|
|
||||||
|
% The capture converted version of the type $\wctype{\rwildcard{X}}{Box}{\rwildcard{X}}$ is
|
||||||
|
% $\exptype{Box}{\rwildcard{X}}$.
|
||||||
|
% The captured type is only used as an intermediate type during a method call.
|
||||||
|
|
BIN
lipics-logo-bw.pdf
Normal file
BIN
lipics-logo-bw.pdf
Normal file
Binary file not shown.
1260
lipics-v2021.cls
Normal file
1260
lipics-v2021.cls
Normal file
File diff suppressed because it is too large
Load Diff
310
martin.bib
Normal file
310
martin.bib
Normal file
@ -0,0 +1,310 @@
|
|||||||
|
@article{10.1145/3409006,
|
||||||
|
author = {Parreaux, Lionel},
|
||||||
|
title = {The Simple Essence of Algebraic Subtyping: Principal Type Inference with Subtyping Made Easy (Functional Pearl)},
|
||||||
|
year = {2020},
|
||||||
|
issue_date = {August 2020},
|
||||||
|
publisher = {Association for Computing Machinery},
|
||||||
|
address = {New York, NY, USA},
|
||||||
|
volume = {4},
|
||||||
|
number = {ICFP},
|
||||||
|
url = {https://doi.org/10.1145/3409006},
|
||||||
|
doi = {10.1145/3409006},
|
||||||
|
abstract = {MLsub extends traditional Hindley-Milner type inference with subtyping while preserving compact principal types, an exciting new development. However, its specification in terms of biunification is difficult to understand, relying on the new concepts of bisubstitution and polar types, and making use of advanced notions from abstract algebra. In this paper, we show that these are in fact not essential to understanding the mechanisms at play in MLsub. We propose an alternative algorithm called Simple-sub, which can be implemented efficiently in under 500 lines of code (including parsing, simplification, and pretty-printing), looks more familiar, and is easier to understand. We present an experimental evaluation of Simple-sub against MLsub on a million randomly-generated well-scoped expressions, showing that the two systems agree. The mutable automaton-based implementation of MLsub is quite far from its algebraic specification, leaving a lot of space for errors; in fact, our evaluation uncovered several bugs in it. We sketch more straightforward soundness and completeness arguments for Simple-sub, based on a syntactic specification of the type system. This paper is meant to be light in formalism, rich in insights, and easy to consume for prospective designers of new type systems and programming languages. In particular, no abstract algebra is inflicted on readers.},
|
||||||
|
journal = {Proc. ACM Program. Lang.},
|
||||||
|
month = {aug},
|
||||||
|
articleno = {124},
|
||||||
|
numpages = {28},
|
||||||
|
keywords = {subtyping, type inference, principal types}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{PT98,
|
||||||
|
author = {Pierce, Benjamin C. and Turner, David N.},
|
||||||
|
title = {Local type inference},
|
||||||
|
booktitle = {Proceedings of the 25th ACM SIGPLAN-SIGACT symposium on Principles of programming languages},
|
||||||
|
series = {POPL '98},
|
||||||
|
year = {1998},
|
||||||
|
location = {San Diego, California, United States},
|
||||||
|
pages = {252--265}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{OZZ01,
|
||||||
|
author = "Martin Odersky and Matthias Zenger and Christoph Zenger",
|
||||||
|
title = "Colored local type inference",
|
||||||
|
journal = "Proc. 28th ACM Symposium on Principles of Programming Languages",
|
||||||
|
volume = "36",
|
||||||
|
number = "3",
|
||||||
|
pages = "41--53",
|
||||||
|
year = "2001",
|
||||||
|
xnote = "citeseer.ist.psu.edu/article/odersky01colored.html" }
|
||||||
|
|
||||||
|
|
||||||
|
@InProceedings{plue09_1,
|
||||||
|
author = {Martin Pl{\"u}micke},
|
||||||
|
title = {Java type unification with wildcards},
|
||||||
|
booktitle = {17th International Conference, INAP 2007, and 21st Workshop on Logic Programming, WLP 2007, W\"urzburg, Germany, October 4-6, 2007, Revised Selected Papers},
|
||||||
|
year = {2009},
|
||||||
|
editor = {Dietmar Seipel and Michael Hanus and Armin Wolf},
|
||||||
|
Volume = {5437},
|
||||||
|
publisher = {Springer-Verlag Heidelberg},
|
||||||
|
pages = {223--240},
|
||||||
|
SERIES = {Lecture Notes in Artificial Intelligence}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{DM82,
|
||||||
|
author={Luis Damas and Robin Milner},
|
||||||
|
title={Principal type-schemes for functional programs},
|
||||||
|
journal={Proc. 9th Symposium on Principles of Programming Languages},
|
||||||
|
year={1982}
|
||||||
|
}
|
||||||
|
|
||||||
|
@article{Rob65,
|
||||||
|
author={J. A. Robinson},
|
||||||
|
title={A Machine-Oriented Logic Based on the Resolution Principle},
|
||||||
|
journal={Journal of ACM},
|
||||||
|
volume={12(1)},
|
||||||
|
pages={23-41},
|
||||||
|
month=Jan,
|
||||||
|
year={1965}}
|
||||||
|
|
||||||
|
@article{MM82,
|
||||||
|
author={A. Martelli and U. Montanari},
|
||||||
|
title={An Efficient Unification Algorithm},
|
||||||
|
journal={ACM Transactions on Programming Languages and Systems},
|
||||||
|
volume={4},
|
||||||
|
pages={258-282},
|
||||||
|
year={1982}}
|
||||||
|
|
||||||
|
@InProceedings{Plue07_3,
|
||||||
|
author = {Martin Pl{\"u}micke},
|
||||||
|
title = {Typeless {P}rogramming in \textsf{{J}ava 5.0} with {W}ildcards},
|
||||||
|
booktitle = {5th {I}nternational {C}onference on {P}rinciples and {P}ractices of {P}rogramming in {J}ava},
|
||||||
|
pages = {73--82},
|
||||||
|
year = {2007},
|
||||||
|
editor = {Vasco Amaral and Lu\'is Veiga and Lu\'is Marcelino and H. Conrad Cunningham},
|
||||||
|
series = {ACM International Conference Proceeding Series},
|
||||||
|
volume = {272},
|
||||||
|
month = {September}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{plue15_2,
|
||||||
|
author = {Martin Pl{\"{u}}micke},
|
||||||
|
title = {More Type Inference in {J}ava 8},
|
||||||
|
booktitle = {Perspectives of System Informatics - 9th International Ershov Informatics
|
||||||
|
Conference, {PSI} 2014, St. Petersburg, Russia, June 24-27, 2014.
|
||||||
|
Revised Selected Papers},
|
||||||
|
editor = {Andrei Voronkov and Irina Virbitskaite},
|
||||||
|
volume = {8974},
|
||||||
|
series = {Lecture Notes in Computer Science},
|
||||||
|
pages = {248--256},
|
||||||
|
year = {2015},
|
||||||
|
publisher = {Springer}
|
||||||
|
}
|
||||||
|
|
||||||
|
@phdthesis{GS89,
|
||||||
|
author = "Gert Smolka",
|
||||||
|
title = "Logic Programming over Polymorphically Order-Sorted Types",
|
||||||
|
school = {Department Informatik, University of Kaisers\-lautern},
|
||||||
|
address = {Kaiserslautern, Germany},
|
||||||
|
month = may,
|
||||||
|
year = 1989
|
||||||
|
}
|
||||||
|
|
||||||
|
@Article{MH91,
|
||||||
|
author = "Michael Hanus",
|
||||||
|
title = "Parametric order-sorted types in logic programming",
|
||||||
|
journal = "Proc. TAPSOFT 1991",
|
||||||
|
year = "1991",
|
||||||
|
volume = "LNCS",
|
||||||
|
number = "394",
|
||||||
|
pages = "181--200"
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{CB95,
|
||||||
|
author = "Christoph Beierle",
|
||||||
|
title = "Type Inferencing for Polymorphic Order-Sorted Logic Programs",
|
||||||
|
booktitle = "International Conference on Logic Programming",
|
||||||
|
pages = "765-779",
|
||||||
|
year = "1995",
|
||||||
|
OPturl = "citeseer.ist.psu.edu/beierle95type.html" }
|
||||||
|
|
||||||
|
@incollection{HiTo92,
|
||||||
|
author = {Patricia M. Hill and Rodney W. Topor},
|
||||||
|
editor = {Frank Pfenning},
|
||||||
|
title = {A {S}emantics for {T}yped {L}ogic {P}rograms},
|
||||||
|
booktitle = {Types in Logic Programming},
|
||||||
|
year = {1992},
|
||||||
|
pages = {1-62},
|
||||||
|
publisher = {MIT Press},
|
||||||
|
bibsource = {DBLP, http://dblp.uni-trier.de}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@inproceedings{TEP05,
|
||||||
|
author = "Torgersen, Mads and Ernst, Erik and
|
||||||
|
Hansen, Christian Plesner",
|
||||||
|
title = "Wild {F}{J}",
|
||||||
|
booktitle = "Proceedings of FOOL 12",
|
||||||
|
year = 2005,
|
||||||
|
editor = "Wadler, Philip",
|
||||||
|
address = "Long Beach, California, USA",
|
||||||
|
month = Jan,
|
||||||
|
organization = "ACM",
|
||||||
|
publisher = "School of Informatics, University of
|
||||||
|
Edinburgh",
|
||||||
|
anote = "Electronic publication, at the URL given
|
||||||
|
below",
|
||||||
|
abstract = "This paper presents a formalization of
|
||||||
|
wildcards, which is one of the new features of
|
||||||
|
the Java programming language in version
|
||||||
|
JDK5.0. Wildcards help alleviating the
|
||||||
|
impedance mismatch between generics, or
|
||||||
|
parametric polymorphism, and traditional
|
||||||
|
object-oriented subtype polymorphism. They do
|
||||||
|
this by quantifying over parameterized types
|
||||||
|
with different type arguments. Wildcards take
|
||||||
|
inspiration from several sources including
|
||||||
|
use-site variance, and they could be considered
|
||||||
|
as a way to introduce a syntactically
|
||||||
|
light-weight kind of existential types into a
|
||||||
|
main-stream language. This formalization
|
||||||
|
describes the mechanism, in particular the
|
||||||
|
wildcard capture process where the existential
|
||||||
|
nature of wildcards becomes evident.",
|
||||||
|
url = "http://homepages.inf.ed.ac.uk/wadler/fool/",
|
||||||
|
annote = "wild-fj.pdf"
|
||||||
|
}
|
||||||
|
|
||||||
|
@Article{BBDGV18,
|
||||||
|
author = {Lorenzo Bettini and Viviana Bono and Mariangiola Dezani-Ciancaglini and Paola Giannini and Betti, Venneri},
|
||||||
|
title = {Java \& Lambda: A Featherweight Story},
|
||||||
|
journal = {Logical Methods in Computer Science},
|
||||||
|
year = {2018},
|
||||||
|
OPTkey = {},
|
||||||
|
volume = {14(3:17)},
|
||||||
|
OPTnumber = {},
|
||||||
|
pages = {1--24},
|
||||||
|
OPTmonth = {},
|
||||||
|
OPTnote = {},
|
||||||
|
OPTannote = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{plue17_2,
|
||||||
|
author = {Pl\"{u}micke, Martin and Stadelmeier, Andreas},
|
||||||
|
title = {Introducing {S}cala-like Function Types into {J}ava-{TX}},
|
||||||
|
booktitle = {Proceedings of the 14th International Conference on Managed Languages and Runtimes},
|
||||||
|
series = {ManLang 2017},
|
||||||
|
year = {2017},
|
||||||
|
isbn = {978-1-4503-5340-3},
|
||||||
|
location = {Prague, Czech Republic},
|
||||||
|
pages = {23--34},
|
||||||
|
numpages = {12},
|
||||||
|
url_ = {http://doi.acm.org/10.1145/3132190.3132203},
|
||||||
|
doi = {10.1145/3132190.3132203},
|
||||||
|
acmid = {3132203},
|
||||||
|
publisher = {ACM},
|
||||||
|
address = {New York, NY, USA},
|
||||||
|
keywords = {Java, function types, lambda expressions, type inference},
|
||||||
|
}
|
||||||
|
|
||||||
|
@InProceedings{Plue04_1,
|
||||||
|
author = {Martin Pl{\"u}micke},
|
||||||
|
title = {Type {U}nification in \textsf{Generic--Java}},
|
||||||
|
booktitle = {Proceedings of 18th {I}nternational {W}orkshop on {U}nification ({U}{N}{I}{F}'04)},
|
||||||
|
year = {2004},
|
||||||
|
address = {Cork},
|
||||||
|
editor = {Michael Kohlhase},
|
||||||
|
month = {July},
|
||||||
|
OPturl = {http://www.lsv.ens-cachan.fr/unif/}
|
||||||
|
}
|
||||||
|
|
||||||
|
@INPROCEEDINGS{AZ04,
|
||||||
|
author = {Davide Ancona and Elena Zucca},
|
||||||
|
title = {Principal typings for {J}ava-like languages},
|
||||||
|
booktitle = {In ACM Symp. on Principles of Programming Languages 2004},
|
||||||
|
year = {2004},
|
||||||
|
pages = {306--317},
|
||||||
|
publisher = {ACM Press}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{plue16_1,
|
||||||
|
author = {Martin Pl{\"{u}}micke},
|
||||||
|
title = {Structural Type Inference in Java-like Languages},
|
||||||
|
booktitle = {Gemeinsamer Tagungsband der Workshops der Tagung Software Engineering
|
||||||
|
2016 {(SE} 2016), Wien, 23.-26. Februar 2016.},
|
||||||
|
pages = {109--113},
|
||||||
|
year = {2016},
|
||||||
|
Optcrossref = {DBLP:conf/se/2016w},
|
||||||
|
url = {http://ceur-ws.org/Vol-1559/paper09.pdf},
|
||||||
|
timestamp = {Mon, 07 Mar 2016 13:17:33 +0100},
|
||||||
|
biburl = {http://dblp.uni-trier.de/rec/bib/conf/se/Plumicke16},
|
||||||
|
bibsource = {dblp computer science bibliography, http://dblp.org}
|
||||||
|
}
|
||||||
|
|
||||||
|
@inproceedings{ADDZ05,
|
||||||
|
author = {Ancona, Davide and Damiani, Ferruccio and Drossopoulou, Sophia and Zucca, Elena},
|
||||||
|
title = {Polymorphic Bytecode: Compositional Compilation for {J}ava-like Languages},
|
||||||
|
booktitle = {Proceedings of the 32nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages},
|
||||||
|
series = {POPL '05},
|
||||||
|
year = {2005},
|
||||||
|
isbn = {1-58113-830-X},
|
||||||
|
location = {Long Beach, California, USA},
|
||||||
|
pages = {26--37},
|
||||||
|
numpages = {12},
|
||||||
|
url = {http://doi.acm.org/10.1145/1040305.1040308},
|
||||||
|
doi = {10.1145/1040305.1040308},
|
||||||
|
acmid = {1040308},
|
||||||
|
publisher = {ACM},
|
||||||
|
address = {New York, NY, USA},
|
||||||
|
keywords = {compositional analysis, type systems},
|
||||||
|
}
|
||||||
|
|
||||||
|
@InProceedings{aModelForJavaWithWildcards,
|
||||||
|
author="Cameron, Nicholas
|
||||||
|
and Drossopoulou, Sophia
|
||||||
|
and Ernst, Erik",
|
||||||
|
editor="Vitek, Jan",
|
||||||
|
title="A Model for Java with Wildcards",
|
||||||
|
booktitle="ECOOP 2008 -- Object-Oriented Programming",
|
||||||
|
year="2008",
|
||||||
|
publisher="Springer Berlin Heidelberg",
|
||||||
|
address="Berlin, Heidelberg",
|
||||||
|
pages="2--26",
|
||||||
|
abstract="Wildcards are a complex and subtle part of the Java type system, present since version 5.0. Although there have been various formalisations and partial type soundness results concerning wildcards, to the best of our knowledge, no system that includes all the key aspects of Java wildcards has been proven type sound. This paper establishes that Java wildcards are type sound. We describe a new formal model based on explicit existential types whose pack and unpack operations are handled implicitly, and prove it type sound. Moreover, we specify a translation from a subset of Java to our formal model, and discuss how several interesting aspects of the Java type system are handled.",
|
||||||
|
isbn="978-3-540-70592-5"
|
||||||
|
}
|
||||||
|
@article{WildcardsNeedWitnessProtection,
|
||||||
|
author = {Bierhoff, Kevin},
|
||||||
|
title = {Wildcards Need Witness Protection},
|
||||||
|
year = {2022},
|
||||||
|
issue_date = {October 2022},
|
||||||
|
publisher = {Association for Computing Machinery},
|
||||||
|
address = {New York, NY, USA},
|
||||||
|
volume = {6},
|
||||||
|
number = {OOPSLA2},
|
||||||
|
url = {https://doi.org/10.1145/3563301},
|
||||||
|
doi = {10.1145/3563301},
|
||||||
|
abstract = {In this paper, we show that the unsoundness discovered by Amin and Tate (2016) in Java’s wildcards is avoidable, even in the absence of a nullness-aware type system. The key insight of this paper is that soundness in type systems that implicitly introduce existential types through subtyping hinges on still making sure there are suitable witness types when introducing existentially quantified type variables. To show that this approach is viable, this paper formalizes a core calculus and proves it sound. We used a static analysis based on our approach to look for potential issues in a vast corpus of Java code and found none (with 1 false positive). This confirms both that Java's unsoundness has minimal practical consequence, and that our approach can avoid it entirely with minimal false positives.},
|
||||||
|
journal = {Proc. ACM Program. Lang.},
|
||||||
|
month = {oct},
|
||||||
|
articleno = {138},
|
||||||
|
numpages = {22},
|
||||||
|
keywords = {Null, Java Wildcards, Existential Types}
|
||||||
|
}
|
||||||
|
@InProceedings{TIforFGJ,
|
||||||
|
author = {Stadelmeier, Andreas and Pl\"{u}micke, Martin and Thiemann, Peter},
|
||||||
|
title = {{Global Type Inference for Featherweight Generic Java}},
|
||||||
|
booktitle = {36th European Conference on Object-Oriented Programming (ECOOP 2022)},
|
||||||
|
pages = {28:1--28:27},
|
||||||
|
series = {Leibniz International Proceedings in Informatics (LIPIcs)},
|
||||||
|
ISBN = {978-3-95977-225-9},
|
||||||
|
ISSN = {1868-8969},
|
||||||
|
year = {2022},
|
||||||
|
volume = {222},
|
||||||
|
editor = {Ali, Karim and Vitek, Jan},
|
||||||
|
publisher = {Schloss Dagstuhl -- Leibniz-Zentrum f{\"u}r Informatik},
|
||||||
|
address = {Dagstuhl, Germany},
|
||||||
|
URL = {https://drops.dagstuhl.de/opus/volltexte/2022/16256},
|
||||||
|
URN = {urn:nbn:de:0030-drops-162560},
|
||||||
|
doi = {10.4230/LIPIcs.ECOOP.2022.28},
|
||||||
|
annote = {Keywords: type inference, Java, subtyping, generics}
|
||||||
|
}
|
206
prolog.tex
Executable file
206
prolog.tex
Executable file
@ -0,0 +1,206 @@
|
|||||||
|
\usepackage{xspace}
|
||||||
|
\usepackage{color,ulem}
|
||||||
|
\usepackage{listings}
|
||||||
|
\lstset{language=Java,
|
||||||
|
showspaces=false,
|
||||||
|
showtabs=false,
|
||||||
|
breaklines=true,
|
||||||
|
showstringspaces=false,
|
||||||
|
breakatwhitespace=true,
|
||||||
|
basicstyle=\ttfamily\fontsize{8}{9.6}\selectfont, %\footnotesize
|
||||||
|
escapeinside={(*@}{@*)},
|
||||||
|
captionpos=b,
|
||||||
|
}
|
||||||
|
\lstdefinestyle{fgj}{backgroundcolor=\color{lime!20}}
|
||||||
|
\lstdefinestyle{tfgj}{backgroundcolor=\color{lightgray!20}}
|
||||||
|
|
||||||
|
|
||||||
|
\newcommand\mv[1]{{\tt #1}}
|
||||||
|
\newcommand{\ol}[1]{\overline{\tt #1}}
|
||||||
|
\newcommand{\exptype}[2]{\mathtt{#1 \texttt{<} #2 \texttt{>} }}
|
||||||
|
\newcommand\ddfrac[2]{\frac{\displaystyle #1}{\displaystyle #2}}
|
||||||
|
|
||||||
|
\newcommand{\sarray}[2]{\begin{array}[t]{#1} #2 \end{array}}
|
||||||
|
|
||||||
|
%\newcommand{\olsub}{\textrm{$\, \leq^\ast \,$}\ }
|
||||||
|
\newcommand{\olsub}{\textrm{$<:$}\ }
|
||||||
|
|
||||||
|
\newcommand{\sub}\olsub%{\mbox{$<$}}
|
||||||
|
|
||||||
|
\newcommand{\set}[1]{\{ #1 \} }
|
||||||
|
|
||||||
|
\definecolor{red}{rgb}{1,0,0}
|
||||||
|
\newcommand{\red}[1]{\textcolor{red}{#1}}
|
||||||
|
|
||||||
|
\newcommand{\commentarystar}[1]{\red{\({}^*\)}\marginpar[\tiny
|
||||||
|
\red{\({}^*\)#1}]{\tiny \red{\({}^*\)#1}}}
|
||||||
|
\newcommand{\commentary}[1]{\marginpar[\tiny
|
||||||
|
\red{#1}]{\tiny \red{#1}}}
|
||||||
|
\newcommand{\commentarymark}[1]{\color{red} #1\ensuremath{^*}\color{black}}
|
||||||
|
\newcommand{\commentarymargintext}[2]{\color{red} #1$^*$
|
||||||
|
\color{black}\marginpar[\tiny \red{\({}^*\)#2}]{\tiny \red{\({}^*\)#2}}}
|
||||||
|
\newcommand{\commentaryintext}[2]{\color{red} #1\textrm{$^*${\tiny #2}}\color{black}}
|
||||||
|
\newcommand{\commentarymath}[2]{\color{red} #1^*\color{black}\)\marginpar[\tiny \red{\({}^*\)#2}]{\tiny \red{\({}^*\)#2}}\(}
|
||||||
|
\newcommand{\replaced}[2]{\erased{#1}\inserted{#2}}
|
||||||
|
\newcommand{\erased}[1]{\commentary{\sout{#1}}}
|
||||||
|
\newcommand{\inserted}[1]{\color{red}#1\color{black}\xspace}
|
||||||
|
|
||||||
|
\newcommand\Erase[1]{|#1|}
|
||||||
|
\newcommand\Angle[1]{\langle#1\rangle}
|
||||||
|
|
||||||
|
\newcommand\TFGJ{FGJ-GT\xspace}
|
||||||
|
\newcommand\FGJGT{FGJ-GT\xspace}
|
||||||
|
\newcommand\FGJType{\textbf{FGJType} }
|
||||||
|
|
||||||
|
\newcommand\TVX{\mv X}
|
||||||
|
\newcommand\TVY{\mv Y}
|
||||||
|
\newcommand\TVZ{\mv Z}
|
||||||
|
\newcommand\TVW{\mv W}
|
||||||
|
|
||||||
|
\newcommand\CL[1]{\textit{Cl}$_{#1}$}
|
||||||
|
\newcommand\subconstr{\lessdot}
|
||||||
|
\newcommand\eqconstr{\doteq}
|
||||||
|
\newcommand\subeq{\mathbin{\texttt{<:}}}
|
||||||
|
\newcommand\extends{\ensuremath{\triangleleft}}
|
||||||
|
|
||||||
|
\newcommand\rulename[1]{\textup{\textsc{(#1)}}}
|
||||||
|
\newcommand{\generalizeRule}{General}
|
||||||
|
|
||||||
|
%%% FJ-IT Type rules
|
||||||
|
\newcommand{\environmentvdash}{\Pi;\Delta;\Gamma \vdash}
|
||||||
|
|
||||||
|
\newcommand{\fjtypeinference}{\textbf{FJTypeInference}}
|
||||||
|
|
||||||
|
%%% Commands for FGJTYPE algorithm
|
||||||
|
\newcommand{\tv}[1]{\mathit{ #1 }}
|
||||||
|
%\newcommand{\fjtypeInsert}{\textbf{FJTypeInsert}}
|
||||||
|
\newcommand{\unifyGenerics}{\ensuremath{\Delta}}% {\ensuremath{\overline{\type{G}\triangleleft \type{H}}}}
|
||||||
|
\newcommand{\fjtype}{\textbf{FJType}}
|
||||||
|
\newcommand{\unify}{\textbf{Unify}}
|
||||||
|
\newcommand{\typeMethod}{\textbf{TYPEMethod}}
|
||||||
|
\newcommand{\typeExpr}{\textbf{TYPEExpr}}
|
||||||
|
\newcommand{\constraint}{\ensuremath{\mathit{c}}}%{\ensuremath{\mathtt{C}}}
|
||||||
|
\newcommand{\consSet}{C}%{\ensuremath{\overline{\mathtt{C}}}}
|
||||||
|
\newcommand{\orCons}{\textit{oc}}%{\ensuremath{\textbf{C}_{||}}}
|
||||||
|
\newcommand{\simpleCons}{\textit{sc}}
|
||||||
|
\newcommand{\overridesFunc}{\textit{overrides}}
|
||||||
|
\newcommand{\typeAssumptionsSymbol}{\ensuremath{\Theta}}
|
||||||
|
\newcommand{\typeAssumptions}{\ensuremath{(\mv{\Pi} ; \overline{\localVarAssumption})}}%{\ensuremath{(\overline{\methodAssumption} ; \overline{\fieldAssumption}; \overline{\localVarAssumption})}}
|
||||||
|
\newcommand{\constraints}{\ensuremath{\mathit{\overline{c}}}}
|
||||||
|
\newcommand{\itype}[1]{\ensuremath{\mathit{#1}}}
|
||||||
|
\newcommand{\il}[1]{\ensuremath{\overline{\itype{#1}}}}
|
||||||
|
\newcommand{\type}[1]{\mathtt{#1}}
|
||||||
|
\newcommand{\anyType}[1]{\mathit{#1}}
|
||||||
|
\newcommand{\gType}[1]{\texttt{#1}}
|
||||||
|
\newcommand{\mtypeEnvironment}{\ensuremath{\Pi}}
|
||||||
|
\newcommand{\methodAssumption}{\ensuremath{\mathtt{\lambda}}}
|
||||||
|
\newcommand{\localVarAssumption}{\ensuremath{\mathtt{\eta}}}
|
||||||
|
\newcommand{\expandLB}{\textit{expandLB}}
|
||||||
|
|
||||||
|
\def\exptypett#1#2{\texttt{#1}\textrm{{\tt <#2}}\textrm{{\tt >}}\xspace}
|
||||||
|
\def\exp#1#2{#1(\,#2\,)\xspace}
|
||||||
|
\newcommand{\ldo}{, \ldots , }
|
||||||
|
|
||||||
|
% WILDCARD specific:
|
||||||
|
\newcommand{\xtype}[2]{\ensuremath{\exists #1 . \type{#2}}}
|
||||||
|
\newcommand{\xtypevar}[1]{\ensuremath{\exists \emptyset . \type{#1}}}
|
||||||
|
|
||||||
|
\newcommand{\wtypestore}[3]{\ensuremath{#1 = \wtype{#2}{#3}}}
|
||||||
|
%\newcommand{\wtype}[2]{\ensuremath{[#1\ #2]}}
|
||||||
|
\newcommand{\wtv}[1]{\ensuremath{\tv{#1}_?}}
|
||||||
|
\newcommand{\wcstore}{\ensuremath{\Delta}}
|
||||||
|
|
||||||
|
%\newcommand{\rwildcard}[1]{\ensuremath{\mathtt{#1}_?}}
|
||||||
|
\newcommand{\rwildcard}[1]{\ensuremath{\mathtt{#1}}}
|
||||||
|
\newcommand{\wildcardEnv}{\ensuremath{\mathbb{W}}}
|
||||||
|
\newcommand{\wildcard}[3]{\ensuremath{\mathtt{#1 : [#3 .. #2]}}}
|
||||||
|
\newcommand{\cwildcard}[3]{\ensuremath{\mathtt{\overleftarrow{#1}^{#2}_{#3}}}}
|
||||||
|
|
||||||
|
\newcommand{\generics}[1]{\ensuremath{\mathtt{\texttt{<} {#1} \texttt{>} }}}
|
||||||
|
|
||||||
|
\newcommand{\lessdotCC}{\ensuremath{\lessdot^{\texttt{c}}}}
|
||||||
|
|
||||||
|
\newcommand{\ccDecaptureRule}{\rulename{CC-Decapture}}
|
||||||
|
\newcommand{\ccInvalidate}{\rulename{CC-Invalidate}}
|
||||||
|
|
||||||
|
\newcommand{\ruleWCEquals}[0]{\rulename{WC-Equals}}
|
||||||
|
\newcommand{\ruleReduceWC}[0]{\rulename{Reduce}}
|
||||||
|
\newcommand{\ruleReduce}[0]{\rulename{Reduce}}
|
||||||
|
\newcommand{\ruleSubstWC}[0]{\rulename{Subst-WC}}
|
||||||
|
\newcommand{\subclass}{\mathtt{\sqsubset}\mkern-5mu :}
|
||||||
|
|
||||||
|
\newcommand{\TameFJ}{\text{TameFJ}}
|
||||||
|
\newcommand{\TamedFJ}{\textbf{TamedFJ}}
|
||||||
|
\newcommand{\TYPE}{\textbf{TYPE}}
|
||||||
|
|
||||||
|
\newcommand{\vdashI}[0]{\ \ . \kern-0.8em \vdash }
|
||||||
|
|
||||||
|
|
||||||
|
\newcommand{\wildcardTV}[1]{\type{#1}_?}
|
||||||
|
\newcommand{\replaceWC}[2]{\ensuremath{[#1/#2]_?}}
|
||||||
|
\newcommand{\replaceAdapt}[2]{\ensuremath{[#1/#2]_\uparrow}}
|
||||||
|
|
||||||
|
\newcommand{\wcEnv}[3]{#1 : [#2\ ..\ #3]}
|
||||||
|
\newcommand{\fresh}[1]{\textit{fresh}(#1)}
|
||||||
|
|
||||||
|
|
||||||
|
\newcommand{\typerule}[1]{\small\textsc{#1}}
|
||||||
|
\newcommand{\letstmt}[2]{\texttt{let}\ #1 \ \texttt{in} \ #2}
|
||||||
|
\newcommand{\elvis}[0]{\ensuremath{\ {?}{:}\ }}
|
||||||
|
|
||||||
|
\newcommand{\wildFJ}[0]{\textbf{WildFJ}}
|
||||||
|
|
||||||
|
%\newcommand{\wfresh}[0]{\textit{wfresh}}
|
||||||
|
|
||||||
|
%COLORS:
|
||||||
|
\definecolor{change}{RGB}{201,201,255}
|
||||||
|
\definecolor{subst}{RGB}{241,203,255}
|
||||||
|
\definecolor{addition}{RGB}{225,247,213}
|
||||||
|
\definecolor{highlight}{RGB}{255,189,189}
|
||||||
|
\definecolor{frame}{HTML}{634b7d}
|
||||||
|
%\definecolor{gray}{RGB}{120, 120, 120}
|
||||||
|
%\definecolor{frame}{RGB}{241, 246, 249}
|
||||||
|
|
||||||
|
\tcbset{colback=white,colframe=frame,fonttitle=\bfseries,coltitle=white}
|
||||||
|
|
||||||
|
\newcommand{\deduction}[2]{
|
||||||
|
\begin{array}[c]{l}
|
||||||
|
#1 \\
|
||||||
|
\hline
|
||||||
|
\vspace*{-0.4cm}\\
|
||||||
|
#2
|
||||||
|
\end{array}
|
||||||
|
}
|
||||||
|
|
||||||
|
\newcommand{\nextdeduction}[1]
|
||||||
|
{
|
||||||
|
\begin{array}{@{}l@{}r@{}}
|
||||||
|
\begin{array}[c]{l}
|
||||||
|
\hline
|
||||||
|
\vspace*{-0.4cm}\\
|
||||||
|
#1
|
||||||
|
\end{array} % \rulename{#3}
|
||||||
|
\end{array}
|
||||||
|
}
|
||||||
|
|
||||||
|
\newcommand{\subst}[2]{
|
||||||
|
\left[ #1 / #2 \right]
|
||||||
|
}
|
||||||
|
|
||||||
|
\newenvironment{constraintset}
|
||||||
|
{
|
||||||
|
\begin{tcolorbox}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
\end{tcolorbox}
|
||||||
|
}
|
||||||
|
|
||||||
|
\newcommand{\wcNtype}[2]{#1 .\ntype{#2}}
|
||||||
|
\newcommand{\wctype}[3]{#1 .\exptype{#2}{#3}}
|
||||||
|
\newcommand{\wtype}[1]{\mathit{#1}}
|
||||||
|
\newcommand{\ntype}[1]{\mathtt{#1}}
|
||||||
|
|
||||||
|
%%% Local Variables:
|
||||||
|
%%% mode: latex
|
||||||
|
%%% TeX-master: "TIforGFJ"
|
||||||
|
%%% End:
|
191
soundness.tex
Normal file
191
soundness.tex
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
\section{Soundness}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% %This lemma can be used to proof Normalize rule!
|
||||||
|
% \begin{lemma}\label{lemma:wildcardReplacement}
|
||||||
|
% Wildcards with the same upper and lower bound can be replaced by their bounds without breaking subtype relations.
|
||||||
|
% \begin{description}
|
||||||
|
% \item[If] $\Delta \cup \set{\wildcard{X}{\type{U}}{\type{L}}} \vdash \type{T} <: \type{S}$
|
||||||
|
% \item[and] $\type{U} = \type{L}$ and $\text{fv}(\type{U}) = \emptyset$
|
||||||
|
% \item[Then] $\Delta \vdash [\type{U}/\type{X}]\type{T} <: [\type{U}/\type{X}]\type{S}$
|
||||||
|
% \end{description}
|
||||||
|
% \end{lemma}
|
||||||
|
% \textit{Proof:} %TODO
|
||||||
|
% %By structural induction over the subtype relation
|
||||||
|
% %S-Refl: by assumption L <: L implies [\type{U}/\type{X}]L
|
||||||
|
|
||||||
|
% \begin{lemma}\label{lemma:noAdditionalFV}
|
||||||
|
% Type solution $\sigma$ does not add additional free variables.
|
||||||
|
% \begin{description}
|
||||||
|
% \item[If] $(\sigma, \Delta) = \unify{}( \overline{ \type{S} \lessdot \type{T} } \cup C)$
|
||||||
|
% \item[and] $fv(\overline{ \type{S} }) = \emptyset$, $fv(\overline{ \type{T} }) = \emptyset$
|
||||||
|
% \item[Then] $fv(\sigma(\overline{ \type{S} })) = \emptyset, fv(\sigma(\overline{ \type{T} })) = \emptyset$
|
||||||
|
% \end{description}
|
||||||
|
% \end{lemma}
|
||||||
|
|
||||||
|
% \begin{lemma}
|
||||||
|
% % https://math.libretexts.org/Bookshelves/Mathematical_Logic_and_Proof/Book%3A_Mathematical_Reasoning__Writing_and_Proof_(Sundstrom)/07%3A_Equivalence_Relations/7.03%3A_Equivalence_Classes
|
||||||
|
% $\doteq$ is an equivalence relation on a constraint set $C$.
|
||||||
|
|
||||||
|
% \begin{description}
|
||||||
|
% \item[If] $\Delta \vdash \overline{\type{T} <: \type{S}}$ and $\type{T} \doteq \type{S}$
|
||||||
|
% \item[Then] $\Delta \vdash [\type{T}/\type{S}](\overline{\type{T} <: \type{S}})$
|
||||||
|
% \end{description}
|
||||||
|
% \end{lemma}
|
||||||
|
|
||||||
|
\begin{lemma}
|
||||||
|
The \unify{} algorithm only produces correct output for constraints not containing free variables.
|
||||||
|
|
||||||
|
\begin{description}
|
||||||
|
\item[If] $(\sigma, \Delta) = \unify{}( \overline{ \type{S} \lessdot \type{T} } \cup \overline{ \type{S'} \lessdot \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 $\Delta'$ with:
|
||||||
|
$\Delta \vdash \overline{\sigma(\type{S}) <: \sigma(\type{T})}$
|
||||||
|
and $\Delta, \Delta' \vdash \overline{\text{CC}(\sigma(\type{S'})) <: \sigma(\type{T'})}$
|
||||||
|
% and $\sigma(\type{T'}) = \sigma(\type{T'})$.
|
||||||
|
|
||||||
|
The function $\text{CC}$ is given as $\text{CC}(\wcNtype{\Delta}{N}) = \type{N}$
|
||||||
|
\end{description}
|
||||||
|
\end{lemma}
|
||||||
|
|
||||||
|
\textit{Proof:}
|
||||||
|
%(we are going backwards over the algorithm)
|
||||||
|
%first we have to determine the \Delta'' -> it's only the wildcards which are free in N
|
||||||
|
% during this proof we can use Delta'' as we like
|
||||||
|
|
||||||
|
For every step in the \unify{} algorithm:
|
||||||
|
Assuming the unifier $\sigma$ is correct for a constraint set $C'$, the unifier is also correct for the
|
||||||
|
constraint set $C$ before the transformation.
|
||||||
|
\begin{description}
|
||||||
|
\item[Assumption:] $\unify{}(C) = (\Delta, \sigma)$, with $\Delta \vdash \sigma(C)$
|
||||||
|
\item[Induction step:] For every case $C'$ which can be transformed to $C$ we have to show $\Delta \vdash \sigma(C')$
|
||||||
|
\end{description}
|
||||||
|
|
||||||
|
\unify{} terminates with $C = \emptyset$ for which the preposition holds:
|
||||||
|
$\Delta \vdash \sigma(\emptyset)$
|
||||||
|
|
||||||
|
We now show that for every transformation of a constraint set $C$ to a constraint set $C'$
|
||||||
|
the preposition holds for $C$ using the assumption that it holds for $C'$ :
|
||||||
|
$\Delta \vdash \sigma(C') \implies \Delta \vdash \sigma(C)$
|
||||||
|
|
||||||
|
\begin{description}
|
||||||
|
\item[AddDelta] $C$ is not changed
|
||||||
|
\item[GenDelta] by definition, S-Var-Left, and S-Trans %The generated type variable is unique due to the solved form property. %and the solved form property (no $\tv{a}$ in $C$)
|
||||||
|
\item[GenSigma] by definition.
|
||||||
|
% holds for $\set{\tv{a} \doteq \type{G}}$ by definition.
|
||||||
|
% Holds for $C$ by assumption and because $\tv{a} \notin C$ by solved form definition ($[\type{G}/\tv{a}]C = C$).
|
||||||
|
\item[Ground] Assumption and S-Bot
|
||||||
|
\item[Sub-Elim] Assumption and S-Refl
|
||||||
|
\item[Force] by assumption and $\rwildcard{X} = \type{U}$ %TODO: step 5 should remove all X^T_T with T (make wildcards with same upper and lower bounds to normal types)
|
||||||
|
\item[Raise] Assumption, S-Trans
|
||||||
|
\item[Settle] Assumption, S-Trans
|
||||||
|
\item[Super] S-Extends ($\vdash \wctype{\Delta}{C}{\ol{T}} <: \wctype{\Delta}{D}{[\ol{T}/\ol{X}]\ol{N}}$), S-Trans
|
||||||
|
\item[\generalizeRule{}] by Assumption, because $C \subset C'$
|
||||||
|
\item[Adapt] Assumption, S-Extends, S-Trans
|
||||||
|
\item[Adopt] Assumption, because $C \subset C'$
|
||||||
|
%\item[Capture, Reduce] are always applied together. We have to destinct between two cases:
|
||||||
|
\item[Capture]
|
||||||
|
If $\text{fv}(\wctype{\Delta}{C}{\ol{T}}) = \emptyset$ the preposition holds by Assumption and S-Exists.
|
||||||
|
If $\text{fv}(\wctype{\Delta}{C}{\ol{T}}) \neq \emptyset$ we have to show $\Delta' \vdash \text{CC}(\sigma(\wctype{\overline{\wildcard{B}{\type{U'}}{\type{L'}}}}{C}{\ol{S}})) <: \sigma(\wctype{\Delta}{C}{\ol{T}})$,
|
||||||
|
which holds by assumption with $\Delta'$ chosen in a way that $\text{fv}(\exptype{C}{\ol{S}}) \subseteq \Delta'$. The variables $\ol{C}$ in $\ol{S}$ can be renamed to $\ol{B}$, because $\ol{C}$ are fresh.
|
||||||
|
%If $\text{fv}(\wctype{\Delta}{C}{\ol{T}}) \neq \emptyset$ we have to show $\Delta \vdash \sigma(\exptype{C}{\ol{S}}) <: \sigma(\wctype{\Delta}{C}{\ol{T}})$.
|
||||||
|
%$\Delta \vdash \sigma([\ol{C}/\ol{B}]\exptype{C}{\ol{S}}) <: \sigma(\wctype{\Delta}{C}{\ol{T}})$ holds by assumption and
|
||||||
|
%the variables $\ol{B}$ in $\ol{S}$ can be renamed to $\ol{C}$, because $\ol{C} \notin \ol{S}$ ($\ol{C}$ are fresh).
|
||||||
|
%The assumption implies $\text{fv}(\ol{S}) \subseteq \text{dom}(\Delta \cup \set{\overline{\wildcard{C}{\type{U'}}{\type{L'}}}})$
|
||||||
|
%, which implies $\text{fv}(\ol{S}) \subseteq \text{dom}(\Delta \cup \set{\overline{\wildcard{B}{\type{U'}}{\type{L'}}}})$.
|
||||||
|
|
||||||
|
%We are doing a capture conversion. If $\type{T}$ does not contain free variables, this does not affect the subtype relation.
|
||||||
|
\item[Reduce] %Assumption and S-Exists.
|
||||||
|
If $\text{fv}(\exptype{C}{\ol{S}}) = \emptyset$ the preposition holds by Assumption and S-Exists.
|
||||||
|
If $\text{fv}(\exptype{C}{\ol{S}}) \neq \emptyset$ there exists a $\Delta'$ with
|
||||||
|
$\Delta' \vdash \text{CC}(\sigma(\exptype{C}{\ol{S}})) <: \sigma(\wctype{\overline{\wildcard{A}{\type{U}}{\type{L}}}}{C}{\ol{T}})$.
|
||||||
|
\item[Match] Assumption, S-Trans
|
||||||
|
\item[Trim] Assumption and S-Exists
|
||||||
|
\item[Remove] $C$ is not changed
|
||||||
|
\item[Circle] S-Refl
|
||||||
|
\item[Swap] by definition
|
||||||
|
\item[Erase] S-Refl
|
||||||
|
\item[Equals] by definition
|
||||||
|
% \item[Reduce]
|
||||||
|
% The renaming from $\rwildcard{C}$ to $\rwildcard{B}$ is not a problem. It's allowed to rename wildcards inside a type.
|
||||||
|
% Removing $\rwildcard{C}$ from the environment does not change anything because it was freshly generated and is used nowhere.
|
||||||
|
|
||||||
|
% The rest follows directly from S-Exists.
|
||||||
|
% We can say: $\text{fv}(\wctype{\overline{\wildcard{A}{\type{U}}{\type{L}}}}{C}{\ol{T}}) = \emptyset$,
|
||||||
|
% because the input to the \unify{} algorithm has no free type variables and we never substitute a type with free type variables
|
||||||
|
% and none of the other steps of the algorithm generates a $\lessdot$ constraint containing free type variables on the right side. %TODO: proof
|
||||||
|
|
||||||
|
% $\text{fv}(\ol{T}) \subseteq \text{dom}(\wildcardEnv \cup \overline{\wildcard{B}{\type{U'}}{\type{L'}}})$
|
||||||
|
% TODO: The capture conversion has to be when substituting a $\wtv{a}$ variable. Then we have to rename!
|
||||||
|
% %Lets first try it without the capture conversion. And involve the wtvs in the second step
|
||||||
|
% % The algorithm works by never substituting wildcards
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% \unify{} cannot guarantee the premise $\text{dom}(\Delta, \Delta') \cap \text{fv}(\wcNtype{\overline{\wildcard{X}{\type{U}}{\type{L}}}}{N}) = \emptyset$.
|
||||||
|
% We loosen the soundness requirements and allow a arbitrary environment $\Delta''$ to be added to the right side of the subtype relation.
|
||||||
|
% This is still sufficient to proof soundness for the whole algorithm.
|
||||||
|
% We show that the need for the additional environment $\Delta''$ can be satisfied by a let statement.
|
||||||
|
|
||||||
|
% \begin{itemize}
|
||||||
|
% \item $\Delta, \Delta' \vdash [\ol{T}/\ol{X}]\ol{L} <: \ol{T}$: S-Exists
|
||||||
|
% \item $\Delta, \Delta' \vdash \ol{T} <: [\ol{T}/\ol{X}]\ol{U}$: S-Exists
|
||||||
|
% \item $\textit{fv}(\ol{T}) \subseteq \text{dom}{\Delta, \Delta'}$: $\wildcardEnv$ holds all variables %TODO: Proof
|
||||||
|
% \end{itemize}
|
||||||
|
\item[Normalize] Assumption and lemma 5 \emph{substitution preserves subtyping}.%\ref{lemma:wildcardReplacement}. (Or Lemma 5 from the wildcard paper. \emph{substitution preserves subtyping})
|
||||||
|
% The GenSigma step replaces both sides of $\rwildcard{A} \doteq \rwildcard{B}$ with the upper bound $\type{U}$.
|
||||||
|
% This works for every constraint, whether it contains free variables or not.
|
||||||
|
% It does not add to free variables of constraints because the upper bound does not contain any.
|
||||||
|
The GenSigma and Gen Delta steps remove Wildcards which have the same upper and lower bound.
|
||||||
|
$\rwildcard{A},\rwildcard{B} \notin \sigma(C)$
|
||||||
|
|
||||||
|
% sigma(T) = sigma(U) we have to show that T = U means \Delta \vdash [T/U]C \implies \Delta \vdash [U/T]C
|
||||||
|
% the constraints L <. U, U <. L lead to L =. U
|
||||||
|
%If L is List<X> with X being free wildcard
|
||||||
|
%then U <. L will fail if U is type variable
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
% this is because bounds never contain free variables (is that true?)
|
||||||
|
|
||||||
|
%This type contains free variables when A is replaced by an CC wildcard
|
||||||
|
|
||||||
|
%This must fail:
|
||||||
|
\begin{verbatim}
|
||||||
|
<A> A m(List<? extends List<A>> l, A)
|
||||||
|
|
||||||
|
m(List<List<? super String>> l, "hi")
|
||||||
|
\end{verbatim}
|
||||||
|
%This fails because of Equals rule (TODO: proof)
|
||||||
|
|
||||||
|
\item[Tame] same reasoning as Normalize
|
||||||
|
\item[Bot] S-Bot
|
||||||
|
\item[Pit] S-Bot
|
||||||
|
\item[Upper] S-Trans and S-VarLeft
|
||||||
|
\item[Lower] S-Trans and S-VerRight
|
||||||
|
\item[Subst-WC] by S-Refl
|
||||||
|
\item[Subst]
|
||||||
|
$\sigma(C \cup \set{\tv{a} \doteq \type{T}}) = \sigma([\type{T}/\tv{a}]C \cup \set{\tv{a} \doteq \type{T}})$
|
||||||
|
and
|
||||||
|
$\sigma(\wildcardEnv) = \sigma([\type{T}/\tv{a}]\wildcardEnv)$
|
||||||
|
\item[Subst-WC]
|
||||||
|
%Proof by Lemma 5 \emph{Type substitution preserves subtyping} from \cite{WildcardsNeedWitnessProtection}.
|
||||||
|
Same as Subst
|
||||||
|
\end{description}
|
||||||
|
|
||||||
|
\subsection{Type Inference Soundness}
|
||||||
|
The type solution is a correct one in respect to the type rules.
|
||||||
|
|
||||||
|
Problem:
|
||||||
|
The capture conversion and let statements.
|
||||||
|
Constraint a <. b => CC(X.C<X>) <: C<X>
|
||||||
|
Why is X the only type used
|
||||||
|
|
||||||
|
$\type{T} \lessdot \type{S}$:
|
||||||
|
If the left part of the constraint has no free variables the solution $\sigma(\type{T})$ will have neither.
|
||||||
|
This means that all used type variables on both sides are bound in the global environment or the environment of the left side
|
||||||
|
%X.C<X> <: C<X>
|
||||||
|
%C<X> = C<X> (both sides must be the same)
|
||||||
|
%the left side has no free variables!
|
Loading…
Reference in New Issue
Block a user