typing: introduce typing judgements for patterns

This commit is contained in:
regnat 2017-05-11 07:35:39 +02:00
parent 51468d4d15
commit 2bd9a4ac58
2 changed files with 74 additions and 19 deletions

View File

@ -89,8 +89,12 @@
\newcommand{}{\ensuremath{\tau}}
\newcommand{\σ}{\sigma}
\DeclareMathOperator\any{\textsc{Any}}
\DeclareMathOperator\grad{\star}
\DeclareMathOperator\Int{Int}
\DeclareMathOperator\Bool{Bool}
\newcommand{}{\lambda}
\newcommand{\recleq}{\sqsubsetleq}
\newcommand{\discrete}[2]{\left\{ #1, .., #2 \right\}}
\newcommand{\pref}[1]{\ref{#1} at page~\pageref{#1}}

View File

@ -2,23 +2,31 @@
\subsubsection{Patterns}
The typing of patterns is inspired by~\cite{Fri04}: To every pattern $p$, we
associate a type $\Lbag p \Rbag$. The exact definition of $\Lbag \_ \Rbag$ is
given in figure~\pref{typing::pattern-accept}.
In order to type patterns, we need to introduce a new form of typing judgement.
The judgement $ \dashv p:$ means that when applied against a type $$, the
pattern $p$ will enrich the environment with the constraints $$.
The intuition behind this operator is that $\matchType{p}$ represents the type
of the values that will be accepted by the pattern. So for example,
$\matchType{x}$ is equal to $\any$ as the pattern $x$ matches any value.
Likewise, $\matchType{\{ x, y ? 1 \}}$ is $\{ x = \any; y = \any \vee \undef
\}$.
For example, we got $x:\Int \dashv x:\Int$, which reads ``If we apply a term
of type $\Int$ to the pattern $x$, then the environment on the
right of the pattern will be enriched with the constraint $x:\Int$''
We also define the matching $\ofTypeP{p}{}$ of a type $$ against a pattern
$p$ (in figure~\pref{typing::pattern-ty-match}).
This represents the typing constraints that we can deduce from the applications
of a type $$ to the pattern $p$. It is used to convert type annotations into
concrete typing environments. So for example, $\ofTypeP{\{ x, y \}}{\{ x = ;
y = \σ \}}$ is equal to the typing constraints $x : $ and $y : \σ$, which may
be used to type the body of the corresponding function.
Likewise, the following statement holds.
\[x:\Int; y: \Bool \dashv \left\{ x; y ? \text{true}; \right\} : \{ x = \Int; y =? \Bool \}\]
This means that when if we match a term of type $\left\{ x =\Int; y =? \Bool
\right\}$ against the pattern $\left\{ x; y ? \text{true}; \right\}$, then the
environment on the right side of the pattern will be enriched with the
constraints $x : \Int$ and $y : \Bool$.
As the symbol ``$\dashv$'' suggests, this typing judgement is the converse of
the classical typing judgement $ \vdash e : $ for expressions: instead of
stating that under the hypothesis $$, the expression $e$ has type $$, we
state that if the pattern $p$ has type $$, then in produces the environment
$$.
The typing rules for this statement are given by the
figure~\pref{typing::patterns::typing-rules}.
Maybe~\todo{Find out wether this is true} this enjoys principal typing.
\subsubsection{Typecase}
@ -32,10 +40,10 @@ an expression such as $(x := e \in \bm{{Int}}) ? x + 1 : x$, with $\vdash e :
\any$ wouldn't typecheck, as $x+1$ isn't well typed without any further
constraint on the type of $x$.
A more interesting typing rule would state that if $; x: \wedge t \vdash e_1:
_1$ and $; x: \wedge \lnot t \vdash e_2: _2$ (where $$ is a type of
$e$ under the hypothesis $$), then the whole expression has type $_1 \vee
_2$.
A more interesting typing rule would state that if $; x: \wedge t \vdash
e_1: _1$ and $; x: \wedge \lnot t \vdash e_2: _2$ (where $$ is a type
of $e$ under the hypothesis $$), then the whole expression has type $_1
\vee _2$.
With this rule, the expression $(x := e \in \bm{{Int}}) ? x + 1 : x$ is
well-typed (provided that $e$ is).
@ -43,6 +51,49 @@ The typing rules are given by the
figures~\pref{typing::lambda-calculus},~\pref{typing::records}
and~\pref{typing::operators}.
\begin{figure}
\begin{mathpar}
\inferrule{~}{x: \dashv x:}
\and\inferrule{~}{\dashv \text{nil} : \text{nil}}
\and\inferrule{ \dashv p:}{ \dashv (p:):}
\and\inferrule{%
\dashv p : \\ \mathcal{B}(c) \subtype
}{%
\dashv p ? c :
}
\and\inferrule{%
\forall i \in \discrete{1}{n}, _i \dashv r_i : _i \\
\forall i, j \in \discrete{1}{n}, \text{Vars}(_i) \cap \text{Vars}(_j) = \varnothing \\
\forall i, j \in \discrete{1}{n}, x_i \neq x_j
}{%
_1; \cdots; _n \dashv \left\{ x_1 = r_1; \cdots; x_n = r_n; \right\}
: \left\{ x_1 = _1; \cdots; x_n = _n; \right\}
}
\and\inferrule{%
\forall i \in \discrete{1}{n}, _i \dashv r_i : _i \\
\forall i, j \in \discrete{1}{n}, \text{Vars}(_i) \cap \text{Vars}(_j) = \varnothing \\
\forall i, j \in \discrete{1}{n}, x_i \neq x_j
}{%
_1; \cdots; _n \dashv \left\{ x_1 = r_1; \cdots; x_n = r_n; \ldots \right\}
: \left\{ x_1 = _1; \cdots; x_n = _n; \ldots \right\}
}
\and\inferrule{%
_1 \dashv p_1 : _1 \\
_2 \dashv p_2 : _2 \\
\text{Vars}(_1) \cap \text{Vars}(_2) = \varnothing
}{%
_1; _2 \dashv \text{Cons}(p_1, p_2) : \text{Cons}(_1, _2)
}
\and\inferrule{%
\dashv p : \\
x \notin
}{%
; x: \dashv p@x :
}
\end{mathpar}
\caption{Typing rules for the patterns\label{typing::patterns::typing-rules}}
\end{figure}
\begin{figure}
\begin{center}
\begin{tabular}{rl}