Define the "oracle" type-system

This commit is contained in:
regnat 2017-05-22 16:24:42 +02:00
parent a78a2a3d60
commit 15c49c3cfc
5 changed files with 84 additions and 153 deletions

View File

@ -109,6 +109,7 @@
\newcommand{\tinfer}{\vdash^\Uparrow}
\newcommand{\tcheck}{\vdash^\Downarrow}
\newcommand{\tIC}{\vdash^\delta}
\newcommand{\toracle}{\vdash^\Updownarrow}
\newcommand{\onerec}{\{ \textbf{..} \}}
\DeclareCollectionInstance{plainmath}{xfrac}{mathdefault}{math}
{%

View File

@ -47,7 +47,12 @@ We also present a compilation from nix to nix-light.
\input{nix-light/typing/bidir.tex}
\section{Soundness of the type-system}
\input{soundness/main}
\input{soundness/intro}
\subsection{The oracle type-system}
\input{soundness/oracle}
\subsection{Soundness of the oracle type-system}
\section{Compilation}

14
soundness/intro.tex Normal file
View File

@ -0,0 +1,14 @@
The \emph{subject reduction} property does not hold for our type system.
Indeed, the checking part makes uses of annotations which are lost during
reduction, and designing a reduction system which keeps those annotations would
be non-trivial at all and would require the reduction system to be deeply
dependant on the structure of the types, which we want to avoid.
But we can imagine an extension of our type-system that would be able to guess
the type of any expression, and would just have to check it (some kind of
``oracle'' type-system).
In this section, we present such a type-system, and proove that subject
reduction and progress both hold for it.
As both the ``infer'' and ``check'' type-systems are restrictions of the oracle
type-system (\emph{i.e.} every term well-typed in one of those is well-typed in
the oracle), we get as corollary the soundness of those.

View File

@ -1,152 +0,0 @@
We have the two classical results of \emph{Subject reduction} and
\emph{Progress} which entail type soundess for the non-gradual part of the
language.
Note that because we defined two typing judgements, both result will be stated
twice.
We first prove the following lemmas:
\begin{lemma}\label{lemma:inferCheck}
Let $e$ be an expression, $$ a typing environment and $$ and $'$ two
(possibly gradual) types with $ \subtypeG '$.
If $ \tinfer e : $ then $ \tcheck e : '$.
\end{lemma}
\begin{proof}
\todo{}
\end{proof}
\begin{lemma}[Substitution]\label{lemma:substitution}
Let $e$ and $e'$ be expressions, $x$ be a variable, $$ and $'$ two types,
$$ a typing environment and $\delta$ be $\Uparrow$ or $\Downarrow$.
If $; x : ' \tIC e : $ and $ \tinfer e' : '$ then $ \tIC
\subst{x}{e'}{e} : $
\end{lemma}
\begin{proof}
By induction on the typing derivation of $; x : ' \tIC e : $. We
replace every Ivar rule introducing $ ; x : ' \tinfer x : '$ by a
derivation of $ \tinfer e' : '$, and every Cvar rule introducing $ ; x
: ' \tcheck x : ''$ (with $ ' \subtypeG '')$ by a derivation of $
\tcheck e' : ''$ (which exists because of Lemma~\ref{lemma:inferCheck}).
This builds a new derivation of $ \tinfer \subst{x}{e'}{e} : $.
\end{proof}
\begin{theorem}[Subject reduction $\Uparrow$]\label{thm:subj-reduction-infer}
For any pair $e, e'$ of terms (of nix-light), if $ \tinfer e : t$ and $e
\rightarrow e'$, then $ \tinfer e' : t$.
\end{theorem}
\begin{proof}
We consider an expression $e$ such that $ \tinfer e : t$.
We prove by induction on the derivation of $ \tinfer e : t$ that $\forall
e', (e \rightarrow e') \Rightarrow ( \tinfer e' : t)$.
Let's consider the various possibilities for the last rule of the derivation
$ \tinfer e : t$.
\begin{description}
\item[IVar,IConst,IAbs,ICons] The expression $e$ is a value and can't be
reduced, so the property holds.
\item[IApp]
$\inferrule{%
\tinfer e_1 : t_1 \\ \tinfer e_2 : t_2 \\
t_1 \subtypeG \zero \rightarrow \one \\
t_2 \subtypeG \dom(t_1)
}{%
\tinfer e_1~e_2 : t_1 \circ t_2
}$
The expression $e$ has then the form $e_1~e_2$ with $ \tinfer e_1
: t_1$ and $ \tinfer e_2 : t_2$ (and $t = t_1 \circ t_2$).
It can be reduced in three different ways (depending of the form of $e_1$
and $e_2$):
\begin{itemize}
\item If $e_1$ is a value $ r. e_0$, then the only possible reduction
is by applying the $\beta$-reduction rule, so the only choice for
$e'$ is $\subst{x}{e_2}{e_0}$ (where $x = \var(r)$).
Moreover, a case analysis on the different typing rules shows that
the last rule of the derivation of $ \tinfer r . e_0 : t_1$ can
only be the Iabs rule:
\[
\inferrule{%
x:t_x \vdash r:t_x \\ ; x:t_x \tinfer e_0:s
}{%
\tinfer r.e_0 : t_x \rightarrow s
}
\]
So $t_1$ has the form $t_x \rightarrow s$. This means that $t_2$ is a
subtype of $t_x = \dom(t_x \rightarrow s)$ and that $t'$ is equal to
$s = (t_x \rightarrow s) \circ t_2$.
Moreover, as $; x:t_x \tinfer e_0:s$ and $ \tinfer e_2 : t_2$
with $t_2 \subtypeG t_x$, the Lemma~\ref{lemma:substitution} allows
us to conclude that $ \tinfer e' : t'$.
\item If $e_1$ is a value $ p.e_0$ (with $p$ not in the form $x$ or
$x:t$), and $e_2$ is a value, then we can use the same reasoning.
\item In the other cases, we can reduce either $e_1$, either $e_2$.
If we reduce $e_1$ to $e'_1$, we get a new expression $e' =
e'_1~e_2$, and $e'_1$ satisfies $ \tinfer e'_1 : t_1$ (by induction
hypothesis). By re-applying the Iapp rule, we get $ \tinfer
e'_1~e_2 : t$, thus $ \tinfer e' : t$.
The same holds if we reduce $e_2$.
\end{itemize}
\item[Let]
$\inferrule{%
; x_1 : \any \tinfer e_1 : t_1\\
; x_1 : t_1 \tinfer e_2 : t
}{%
\tinfer \text{let } e_1 = x_1 \text{ in } e_2 : t
}$
The expression $e$ has the form ``let $e_1 = x_1$ in $e_2$''.
It reduces (and may only reduce to) $e' = \subst{x_1}{e_1}{e_2}$.
By applying the substitiution lemma, we can deduce that $e'$ has type
$t$.
\item[LetAnnot]
The subject reduction property do not hold for the \textbf{LetAnnot}
case for now.~\todo{Make this hold}
\item[ITcase]
$\inferrule{%
\tinfer e_0 : t_0 \\
t_0 \not\subtype s \Rightarrow ; x : t_0 \wedge \lnot s \tinfer e_2 : t_2 \\
t_0 \not\subtype \lnot s \Rightarrow ; x : t_0 \wedge s \tinfer e_1 : t_1 \\
}{%
\tinfer (x = e_0 \in s) ? e_1 : e_2 : t_1 \vee t_2
}$
If $e_0$ is not a value, then the only possible reduction for $e$ is to
reduce it to an expression $e'_0$ which will have a type $t'_0 \subtype
t_0$ under the context $$ (by induction hypothesis).
By re-applying the \textbf{ITcase} rule, we obtain a new type $t'$ for
the expression $(x = e'_0 \in s) ? e_1 : e_2$, which is a subtype of
$t$.
If $e_0$ is a value $v$, then the only possible reductions are if
$\tinfer v : s$ or $\tinfer v : \lnot s$\todo{Current typing rules
do not allow asserting that this is always true, which will b a deal
broker for Progress, see~\cite{Fri08} for a way to enforce this}.
Assume the first one. The expression $e$ then reduces to
$\subst{x}{v}{e_1}$, and (by application of the substitution Lemma), we
know that $ \tinfer \subst{x}{v}{e_1} : t_1$. As $t_1 \subtype t$
(because $t = t_1 \vee t_2$), we get the expected result.
By symmetry, this also holds if $\tinfer v : \lnot s$.
\end{description}
\end{proof}
\begin{theorem}[Subject reduction $\Downarrow$]\label{thm:subj-reduction-check}
For any pair $e, e'$ of terms (of nix-light), if $ \tcheck e : t$ and $e
\rightarrow e'$, then $ \tcheck e' : t$.
\end{theorem}
\begin{proof}
The proof is similar to the one of~\autoref{thm:subj-reduction-infer}.
\end{proof}

63
soundness/oracle.tex Normal file
View File

@ -0,0 +1,63 @@
The type-system is defined in~\autoref{fig::oracle-type-system}.
\begin{figure}
\begin{mathpar}
\and\inferrule{ (x) \subtype t }{; \toracle x:t}(OVar)
\and\inferrule{ \Bt(c) \subtype t }{ \toracle c:t}(OConst)
\and\inferrule{%
\toracle e_2 : s \\
\toracle e_1 : s \rightarrow t
}{%
\toracle e_1~e_2 : t
}
(OApp)
\and\inferrule{%
t \subtype \zero \rightarrow \one \\
\forall s_1 \rightarrow s_2 \in \A(t), \\
' \dashv p:s_1 \\ ;' \toracle e : s_2
}{%
\toracle p.e : t
}(OAbs)
\and
\inferrule{%
\forall i \in \discrete{1}{n},
; x_1 : t_i \toracle e_i : t_i \\
; x_1 : t_1 \toracle e : t
}{%
\Gamma \toracle \text{let } x_1 = e_1 \text{ in } e : t
}
(OLet)
\and
\inferrule{%
\forall i \in \discrete{1}{n},
; x_1 : t_1; \ldots; x_n : t_n \toracle e_i : t_i \\
; x_1 : t_1; \cdots; x_n : t_n \toracle e : t
}{%
\Gamma \toracle \text{let } x_1 : t_1 = e_1; \ldots{}; x_n : t_n = e_n
\text{ in } e : t
}
(OLetAnnot)
\and\inferrule{%
\toracle e : t \\
t \not\subtype t \Rightarrow ; x : t \wedge \lnot t \toracle e_2 : s \\
t \not\subtype \lnot t \Rightarrow ; x : t \wedge t \toracle e_1 : s \\
}{%
\toracle (x = e \in t) ? e_1 : e_2 : s
}(OTcase)
\and\inferrule{%
\toracle e_1 : t_1 \\ \toracle e_2 : t_2 \\
t_2 \subtype \cons(\any, \any) \\
\cons(t_1, t_2) \subtype t
}{%
\toracle \cons(e_1, e_2) : t
}(OCons)
\end{mathpar}
\caption{The oracle type-system}\label{fig::oracle-type-system}
\end{figure}