number)$\to$number)} so that the cast is not inserted when the

function is applied to a number. To make this deduction we need a

separate deduction system to collect the type information generated by

the occurrence typing technique we developed in the previous section

(we define a separate deduction system only for presentation purposes:

in reality this should be merged with the type deduction system in

order to avoid several passes on the parse tree).

\beppe{Side remark:

notice that the type \code{(number$\to$number)$\wedge$((?\textbackslash

number)$\to$number)} and \code{((?$\vee$number)$\to$number)} are

equivalent types for subtyping (are they?) but they yield different compilations and,

thus, different results. This is not nice}.

------------------

Finally to take into account gradual typing we need a little bit of

extra work, but just in this last typing rule, all the rest remains

unchanged.

Let $\tau$ be a gradual type. Define $\tau^*$ as the type obtained from $\tau$ by replacing all covariant occurrences of ? by \Any\ and all contravariant ones by \Empty. Now modify the rule above as follows:

In reality, we want to consider the largest $I$ such that $\{s_i\alt i\in I\}\subseteq\psi(x)$ and the typing does not fail (and $I$ must not be empty). We exclude those that fail because they correspond to branches whose typing requires the presence of gradual types (that we have excluded by considering $\tau$ instead of $\tau$. Notice that if we do not take the maximum, then we obtain a supertype, so the rule is sound but not algorithmic (unless we specify maximum).

The idea behind this rule is the following. We have the function $\lambda x:\tau.e$, where $\tau$ is graudal. Instead of trying to deduce from $\Gamma, x:\tau\vdash e: \tau'$ a generic type $\tau\to\tau'$, we use the occurence typing analysis we developped to collect the set $\psi(x)$ of the (static) types the varible $x$ is checked against in the body of the function. To do that we assign the variable $x$ the type $\tau^*$, that is, the greatest type which is a concretization of $\tau$ and deduce $\psi(x)$. Then, for each type in $\psi(x)$ we try to type the body of the function under the hypothesis that $x$ has that type. If we suceed, we keep the arrow type we just deduced otherwise we discard the typing. Finally, we type the body a last time under the hypothesis that $x$ has the type $\tau$ minus all the types for which the typechecking suceeded (these are the types in $I$), and take the intersection of all these types.

\beppe{I seriously wonder whether the extra arrow is necessary or we can prove that it is already covered by the other arrows but without considering the case $\tau^*$. Plus j'y pense et plus je suis convaincu que la première regle va bien aussi pour le cas graduel, problème il me fait des intersections avec des types graduels}

Note that every time the function is applied to an argument of type $s_i$ no cast is required.

To see how this work, consider again our case study for the case $\tau$ equal to \code{?}. The analysis would yield $\psi(x)=\{\Int,\Real\setminus\Int\}$ for the branch \code{(x $\in$ Int) ? succ x : sqrt x} (as before), but yield $\psi(x)=\{\Any\setminus\Real\}$ for the branch \code{$\neg$x}. Therefore, according to the rule above the function would be checked for the input types, $\Int$, $\Real\setminus\Int$, $\Any\setminus\Real$, and $\code{?}\setminus(\Int\vee(\Real\setminus\Int)$. The typing would fail for the case $\Any\setminus\Real$ leaving just the relevant cases.