In the previous section we outlined the main ideas of our approach to occurrence typing. However, devil is in the details. So the formalization we give in Section~\ref{sec:language} is not so smooth as we just outlined: we must introduce several auxiliary definitions to handle some corner cases. This section presents by tiny examples the main technical difficulties we had to overcome and the definitions we introduced to handle them. As such it provides a kind of road-map to the technicalities of Section~\ref{sec:language}.
These rules are quite standard and do not need any particular explanation. Just notice that we used a classic subsumption rule to embed subtyping in the type system.
Let us now focus on the unconventional aspects of our system. starting
from the simplest ones. The first one is that, as explained in
Section~\ref{sec:challenges}, our assumptions are about
expressions. Therefore the type environments of our rules, ranged over
by $\Gamma$ map \emph{expressions}---rather than just variables---into
types. This explains why the classic typing rule for variables is replace by the more general \Rule{Env} rule below:\beppe{Changed the name of Occ into Env since it seems more appropriate}
\begin{mathpar}
\Infer[Env]
{}
{\Gamma\vdash e: \Gamma(e) }
{ e\in\dom\Gamma}
\qquad
\Infer[Intersect]
{\Gamma\vdash e:t_1\\\Gamma\vdash e:t_2 }
{\Gamma\vdash e: t_1 \wedge t_2 }
{}
\end{mathpar}
The rule is coupled with the classic intersection introduction rule
which allow us to deduce for a complex expression the intersection of
the types recorded by the occurrence typing analysis in the
environment $\Gamma$ with the static type deduced for the same
expression by using the other typing rules. This same intersection
rule is also used to infer the second unconventional aspect of our
system, that is the fact that $\lambda$ abstractions can have negated
arrow types as long as this negation does not make their type empty:
Clearly the expression above is well typed, but the rule \Rule{Abs+}
is not enough to type it. In particular, according to \Rule{Abs+} we
have to prove that under the hypothesis that $x:\Int$ the expression
$\tcase{x}{\Int}{x+1}{\textsf{true}}$, that is that under the
hypothesis that x has type $\Int\wedge\Int$ (we apply occurrence
typing) then $x+1$ had type \Int (which is ok) and that under the
hypothesis that $x$ has type $\Int\setminus\Int$, that is $\Empty$
(once more we apply occurrence typing), \textsf{true} is of type \Int
(which is not ok). The problem is that we are trying to type the
second case of a type case even if we now that there is no chance that
it will be selected. The fact that it is never selected is witnessed
by the fact that there is a type hypothesis with the $\Empty$ type. To
avoid this problem (and type the term above) we add the rule
\Rule{Efq} (\emph{ex falso quodlibet}) that allows to deduce any type
for an expression that will never be selected, that is for an
expression whose type environment has an empty assumption:
\begin{mathpar}
\Infer[Efq]
{}
{\Gamma, (e:\Empty) \vdash e': t }
{}
\end{mathpar}
Once more, this kind of deduction was already present in~\cite{Frisch2008} to type full fledged overloaded functions, though it was embedded in the typing rule for the type-case. Here we need the more general \Rule{Efq} rule, to ensure the property of subject reduction.\beppe{Example?}
Finally the core of our type system is given by the following rule for the type-case.\beppe{Changed to If to Case}
\begin{mathpar}
\Infer[Case]
{\Gamma\vdash e:t_0\\
%t_0\not\leq \neg t \Rightarrow
\Gamma\evdash + e t \Gamma^+ \\\Gamma^+ \vdash e_1:t'\\
%t_0\not\leq t \Rightarrow
\Gamma\evdash - e t \Gamma^- \\\Gamma^- \vdash e_2:t'}