practical.tex 10.2 KB
Newer Older
Giuseppe Castagna's avatar
Giuseppe Castagna committed
1
We have implemented the algorithmic system $\vdashA$. Our
Giuseppe Castagna's avatar
Giuseppe Castagna committed
2
implementation is written in OCaml and uses CDuce as a library to
Giuseppe Castagna's avatar
Giuseppe Castagna committed
3
provide the semantic subtyping machinery. Besides the type-checking
Kim Nguyễn's avatar
Kim Nguyễn committed
4
algorithm defined on the base language, our implementation supports
Giuseppe Castagna's avatar
Giuseppe Castagna committed
5
6
7
record types (Section \ref{ssec:struct}) and the refinement of
function types (Section \ref{sec:refining} with the rule of
Appendix~\ref{app:optimize}). The implementation is rather crude and
Giuseppe Castagna's avatar
Giuseppe Castagna committed
8
consists of 2000 lines of OCaml code, including parsing, type-checking
Giuseppe Castagna's avatar
Giuseppe Castagna committed
9
10
11
of programs, and pretty printing of types. We demonstrate the output
of our type-checking implementation in Table~\ref{tab:implem} by
listing some examples none of which can be typed by current
Giuseppe Castagna's avatar
Giuseppe Castagna committed
12
13
systems (even though some systems such as Flow and TypeScript
can type some of these examples by adding explicit type annotations, the code 6,
Giuseppe Castagna's avatar
Giuseppe Castagna committed
14
7, 9, and 10 in Table~\ref{tab:implem}  and, even more,  the \code{and\_} and \code{xor\_} functions at the end of this
Giuseppe Castagna's avatar
Giuseppe Castagna committed
15
section are out of reach of current systems, even when using the right
Giuseppe Castagna's avatar
Giuseppe Castagna committed
16
explicit annotations). These and other examples can be tested in the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
17
\ifsubmission
18
19
anonymized
\else
Giuseppe Castagna's avatar
Giuseppe Castagna committed
20
\fi
21
22
online toplevel available at
\url{https://occtyping.github.io/}%
Kim Nguyễn's avatar
Kim Nguyễn committed
23
\input{code_table}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
24
In Table~\ref{tab:implem}, the second column gives a code fragment and the third
Kim Nguyễn's avatar
Kim Nguyễn committed
25
column the type deduced by our implementation. Code~1 is a
Giuseppe Castagna's avatar
Giuseppe Castagna committed
26
27
straightforward function similar to our introductory example \code{foo} in (\ref{foo},\ref{foo2}). Here the
programmer annotates the parameter of the function with a coarse type
28
$\Int\vee\Bool$. Our implementation first type-checks the body of the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
29
function under this assumption, but doing so it collects that the type of
Giuseppe Castagna's avatar
Giuseppe Castagna committed
30
31
32
$\texttt{x}$ is specialized to \Int{} in the ``then'' case and to \Bool{}
in the ``else'' case. The function is thus type-checked twice more
under each hypothesis for \texttt{x}, yielding the precise type
Giuseppe Castagna's avatar
Giuseppe Castagna committed
33
$(\Int\to\Int)\land(\Bool\to\Bool)$. Note that w.r.t.\  rule \Rule{AbsInf+} of Section~\ref{sec:refining}, our implementation improved the output of the computed
Giuseppe Castagna's avatar
Giuseppe Castagna committed
34
type. Indeed, using rule~[{\sc AbsInf}+] we would obtain the
Kim Nguyễn's avatar
Kim Nguyễn committed
35
36
type
$(\Int\to\Int)\land(\Bool\to\Bool)\land(\Bool\vee\Int\to\Bool\vee\Int)$
Giuseppe Castagna's avatar
Giuseppe Castagna committed
37
with a redundant arrow. Here we can see that, since we deduced
38
the first two arrows $(\Int\to\Int)\land(\Bool\to\Bool)$, and since
Giuseppe Castagna's avatar
Giuseppe Castagna committed
39
the union of their domain exactly covers the domain of the third arrow, then
40
the latter is not needed. Code~2 shows what happens when the argument
Giuseppe Castagna's avatar
Giuseppe Castagna committed
41
of the function is left unannotated (i.e., it is annotated by the top
Giuseppe Castagna's avatar
Giuseppe Castagna committed
42
type \Any, written ``\texttt{Any}'' in our implementation). Here
43
44
type-checking and refinement also work as expected, but the function
only type checks if all cases for \texttt{x} are covered (which means
Giuseppe Castagna's avatar
Giuseppe Castagna committed
45
46
that the function must handle the case of inputs that are neither in \Int{}
nor in \Bool).
47
48
49

The following examples paint a more interesting picture. First
(Code~3) it is
Giuseppe Castagna's avatar
typos    
Giuseppe Castagna committed
50
easy in our formalism to program type predicates such as those
Giuseppe Castagna's avatar
Giuseppe Castagna committed
51
hard-coded in the $\lambda_{\textit{TR}}$ language of \citet{THF10}. Such type
52
53
predicates, which return \texttt{true} if and only if their input has
a particular type, are just plain functions with an intersection
Giuseppe Castagna's avatar
typos    
Giuseppe Castagna committed
54
type inferred by the system of Section~\ref{sec:refining}. We next define Boolean connectives as overloaded
Giuseppe Castagna's avatar
Giuseppe Castagna committed
55
functions. The \texttt{not\_} connective (Code~4) just tests whether its
56
argument is the Boolean \texttt{true} by testing that it belongs to
57
58
59
60
61
62
63
64
65
the singleton type \True{} (the type whose only value is
\texttt{true}) returning \texttt{false} for it and \texttt{true} for
any other value (recall that $\neg\True$ is equivalent to
$\texttt{Any\textbackslash}\True$). It works on values of any type,
but we could restrict it to Boolean values by simply annotating the
parameter by \Bool{} (which in CDuce is syntactic sugar for
\True$\vee$\False) yielding the type
$(\True{\to}\False)\wedge(\False{\to}\True)$.
The \texttt{or\_} connective (Code~5) is straightforward as far as the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
66
67
code goes, but we see that the overloaded type precisely captures all
possible cases. Again we use a generalized version of the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
68
\texttt{or\_} connective that accepts and treats any value that is not
69
\texttt{true} as \texttt{false} and again, we could easily restrict the
70
71
domain to \Bool{} if desired.\\
\indent
Giuseppe Castagna's avatar
Giuseppe Castagna committed
72
To showcase the power of our type system, and in particular of
73
the ``$\worra{}{}$''
Giuseppe Castagna's avatar
Giuseppe Castagna committed
74
type operator, we define \texttt{and\_} (Code~6) using De Morgan's
75
Laws instead of
Giuseppe Castagna's avatar
Giuseppe Castagna committed
76
using a direct definition. Here the application of the outermost \texttt{not\_} operator is checked against type \True. This
77
allows the system to deduce that the whole \texttt{or\_} application
Giuseppe Castagna's avatar
typos    
Giuseppe Castagna committed
78
has type \False, which in turn leads to \texttt{not\_\;x} and
Victor Lanvin's avatar
Fixes    
Victor Lanvin committed
79
\texttt{not\_\;y} to have type $\lnot \True$ and therefore both \texttt{x}
80
81
82
83
84
85
and \texttt{y} to have type \True. The whole function is typed with
the most precise type (we present the type as printed by our
implementation, but the first arrow of the resulting type is
equivalent to
$(\True\to\lnot\True\to\False)\land(\True\to\True\to\True)$).

Giuseppe Castagna's avatar
Giuseppe Castagna committed
86
All these type predicates and Boolean connectives can be used together
87
88
89
to write complex type tests, as in Code~7. Here we define a function
\texttt{f} that takes two arguments \texttt{x} and \texttt{y}. If
\texttt{x} is an integer and \texttt{y} a Boolean, then it returns the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
90
91
integer \texttt{1}; if \texttt{x} is a character or
\texttt{y} is an integer, then it returns \texttt{2}; otherwise the
92
function returns \texttt{3}. Our system correctly deduces a (complex)
Giuseppe Castagna's avatar
Giuseppe Castagna committed
93
94
intersection type that covers all cases (plus several redundant arrow
types). That this type is as precise as possible can be shown by the fact that
95
when applying
Giuseppe Castagna's avatar
Giuseppe Castagna committed
96
97
\texttt{f} to arguments of the expected type, the \emph{type} deduced for the
whole expression is the singleton type \texttt{1}, or \texttt{2},
98
99
100
or \texttt{3}, depending on the type of the arguments.

Code~8 allows us to demonstrate the use and typing of record paths. We
Giuseppe Castagna's avatar
Giuseppe Castagna committed
101
model, using open records, the type of DOM objects that represent XML
102
or HTML documents. Such objects possess a common field
Giuseppe Castagna's avatar
Giuseppe Castagna committed
103
\texttt{nodeType} containing an integer constant denoting the kind of
104
the node (e.g., \p{1} for an element node, \p{3} for a text node, \ldots). Depending on the kind, the object will have
105
106
different fields and methods. It is common practice to perform a test
on the value of the \texttt{nodeType} field. In dynamic languages such
107
108
109
as JavaScript, the relevant field can directly be accessed
after having checked for the appropriate \texttt{nodeType}, whereas
in statically typed languages such as Java, a downward cast
110
from the generic \texttt{Node} type to the expected precise type of
Giuseppe Castagna's avatar
Giuseppe Castagna committed
111
the object is needed. We can see that using the extension presented in
112
113
114
115
116
117
118
Section~\ref{ssec:struct} we can deduce the correct type for
\texttt{x} in all cases. Of particular interest is the last case,
since we use a type case to check the emptiness of the list of child
nodes. This splits, at the type level, the case for the \Keyw{Element}
type depending on whether the content of the \texttt{childNodes} field
is the empty list or not.

Giuseppe Castagna's avatar
Giuseppe Castagna committed
119
Code~9 shows the usefulness of the rule \Rule{OverApp}.
120
Consider the definition of the \texttt{xor\_} operator.
121
122
Here the rule~[{\sc AbsInf}+] is not sufficient to precisely type the
function, and using only this rule would yield a type
Giuseppe Castagna's avatar
Giuseppe Castagna committed
123
124
125
 $\Any\to\Any\to\Bool$.
\iflongversion
Let us follow the behavior of the
126
127
 ``$\worra{}{}$'' operator. Here the whole \texttt{and\_} is requested
 to have type \True, which implies that \texttt{or\_ x y} must have
Giuseppe Castagna's avatar
Giuseppe Castagna committed
128
 type \True. This can always happen, whether \texttt{x} is \True{} or
129
130
 not (but then depends on the type of \texttt{y}). The ``$\worra{}{}$''
 operator correctly computes that the type for \texttt{x} in the
Giuseppe Castagna's avatar
Giuseppe Castagna committed
131
 ``\texttt{then}'' branch is $\True\vee\lnot\True\lor\True\simeq\Any$,
132
and a similar reasoning holds for \texttt{y}.
Giuseppe Castagna's avatar
Giuseppe Castagna committed
133
\fi%%%%%%%%%%%%%%
134
However, since \texttt{or\_} has type
Giuseppe Castagna's avatar
Giuseppe Castagna committed
135
136
137
138
139
%\\[.7mm]\centerline{%
$(\True\to\Any\to\True)\land(\Any\to\True\to\True)\land
   (\lnot\True\to\lnot\True\to\False)$
%}\\[.7mm]
then the rule \Rule{OverApp} applies and \True, \Any, and $\lnot\True$ become candidate types for
Giuseppe Castagna's avatar
Giuseppe Castagna committed
140
\texttt{x}, which allows us to deduce the precise type given in the table. Finally, thanks to rule \Rule{OverApp} it is not  necessary to use a type case to force refinement. As a consequence, we can define the functions \texttt{and\_} and \texttt{xor\_} more naturally as:
141
\begin{alltt}\color{darkblue}\morecompact
Giuseppe Castagna's avatar
Giuseppe Castagna committed
142
143
  let and_ = fun (x : Any) -> fun (y : Any) -> not_ (or_ (not_ x) (not_ y))
  let xor_ = fun (x : Any) -> fun (y : Any) -> and_ (or_ x y) (not_ (and_ x y))
Giuseppe Castagna's avatar
Giuseppe Castagna committed
144
145
\end{alltt}
for which the very same types as in Table~\ref{tab:implem} are deduced.
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
146

Giuseppe Castagna's avatar
Giuseppe Castagna committed
147
148
149
Last but not least Code~10 (corresponding to our introductory
example~\eqref{nest1}) illustrates the need for iterative refinement of
type environments, as defined in Section~\ref{sec:typenv}. As
150
explained, a single pass analysis would deduce
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
151
for {\tt x}
Giuseppe Castagna's avatar
Giuseppe Castagna committed
152
a type \Int{} from the {\tt f\;x} application and \Any{} from the {\tt g\;x}
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
153
application. Here by iterating a second time, the algorithm deduces
Giuseppe Castagna's avatar
empty    
Giuseppe Castagna committed
154
that {\tt x} has type $\Empty$ (i.e., $\textsf{Empty}$), that is that the first branch can never
Giuseppe Castagna's avatar
Giuseppe Castagna committed
155
be selected (and our implementation warns the user accordingly). In hindsight, the only way for a well-typed overloaded function to have
Giuseppe Castagna's avatar
empty    
Giuseppe Castagna committed
156
type $(\Int{\to}\Int)\land(\Any{\to}\Bool)$ is to diverge when the
Giuseppe Castagna's avatar
typo    
Giuseppe Castagna committed
157
argument is of type \Int: since this intersection type states that
Kim Nguyễn's avatar
.    
Kim Nguyễn committed
158
159
160
161
162
whenever the input is \Int, {\em both\/} branches can be selected,
yielding a result that is at the same time an integer and a Boolean.
This is precisely reflected by the case $\Int\to\Empty$ in the result.
Indeed our {\tt example10} function can be applied to an integer, but
at runtime the application of {\tt f ~x} will diverge.
163
164
165
166
167
168
169
170
171
172
173

Although these experiments are still preliminary, they show how the
combination of occurrence typing and set-theoretic types, together
with the type inference for overloaded function types presented in
Section~\ref{sec:refining} allows us to go beyond what languages like
TypeScript and Flow which can only infer single arrow types.
Our refining of overloaded
function is also future proof. Since it ``retypes'' functions
 using information gathered by the typing of occurrences in the body,
 its precision will improve when we further improve the
 our occurrence typing implementation.