comparison doc/manual.tex @ 533:419f51b1e68d

Typing
author Adam Chlipala <adamc@hcoop.net>
date Sat, 29 Nov 2008 10:34:56 -0500
parents 23718a4b23d7
children 65c253a9ca92
comparison
equal deleted inserted replaced
532:23718a4b23d7 533:419f51b1e68d
199 199
200 In many contexts where record fields are expected, like in a projection $e.c$, a constant field may be written as simply $X$, rather than $\#X$. 200 In many contexts where record fields are expected, like in a projection $e.c$, a constant field may be written as simply $X$, rather than $\#X$.
201 201
202 A record type may be written $\{(c = c,)^*\}$, which elaborates to $\$[(c = c,)^*]$. 202 A record type may be written $\{(c = c,)^*\}$, which elaborates to $\$[(c = c,)^*]$.
203 203
204 The notation $[c_1, \ldots, c_n]$ is shorthand for $[c_1 = (), \ldots, c_n = ()]$.
205
204 A tuple type $(\tau_1, \ldots, \tau_n)$ expands to a record type $\{1 = \tau_1, \ldots, n = \tau_n\}$, with natural numbers as field names. A tuple pattern $(p_1, \ldots, p_n)$ expands to a rigid record pattern $\{1 = p_1, \ldots, n = p_n\}$. Positive natural numbers may be used in most places where field names would be allowed. 206 A tuple type $(\tau_1, \ldots, \tau_n)$ expands to a record type $\{1 = \tau_1, \ldots, n = \tau_n\}$, with natural numbers as field names. A tuple pattern $(p_1, \ldots, p_n)$ expands to a rigid record pattern $\{1 = p_1, \ldots, n = p_n\}$. Positive natural numbers may be used in most places where field names would be allowed.
205 207
206 In general, several adjacent $\lambda$ forms may be combined into one, and kind and type annotations may be omitted, in which case they are implicitly included as wildcards. More formally, for constructor-level abstractions, we can define a new non-terminal $b ::= x \mid (x :: \kappa) \mid [c \sim c]$ and allow composite abstractions of the form $\lambda b^+ \Rightarrow c$, elaborating into the obvious sequence of one core $\lambda$ per element of $b^+$. 208 In general, several adjacent $\lambda$ forms may be combined into one, and kind and type annotations may be omitted, in which case they are implicitly included as wildcards. More formally, for constructor-level abstractions, we can define a new non-terminal $b ::= x \mid (x :: \kappa) \mid [c \sim c]$ and allow composite abstractions of the form $\lambda b^+ \Rightarrow c$, elaborating into the obvious sequence of one core $\lambda$ per element of $b^+$.
207 209
208 For any signature item or declaration that defines some entity to be equal to $A$ with classification annotation $B$ (e.g., $\mt{val} \; x : B = A$), $B$ and the preceding colon (or similar punctuation) may be omitted, in which case it is filled in as a wildcard. 210 For any signature item or declaration that defines some entity to be equal to $A$ with classification annotation $B$ (e.g., $\mt{val} \; x : B = A$), $B$ and the preceding colon (or similar punctuation) may be omitted, in which case it is filled in as a wildcard.
239 \item $\Gamma \vdash c :: \kappa$ assigns a kind to a constructor in a context. 241 \item $\Gamma \vdash c :: \kappa$ assigns a kind to a constructor in a context.
240 \item $\Gamma \vdash c \sim c$ proves the disjointness of two record constructors; that is, that they share no field names. We overload the judgment to apply to pairs of field names as well. 242 \item $\Gamma \vdash c \sim c$ proves the disjointness of two record constructors; that is, that they share no field names. We overload the judgment to apply to pairs of field names as well.
241 \item $\Gamma \vdash c \hookrightarrow C$ proves that record constructor $c$ decomposes into set $C$ of field names and record constructors. 243 \item $\Gamma \vdash c \hookrightarrow C$ proves that record constructor $c$ decomposes into set $C$ of field names and record constructors.
242 \item $\Gamma \vdash c \equiv c$ proves the computational equivalence of two constructors. This is often called a \emph{definitional equality} in the world of type theory. 244 \item $\Gamma \vdash c \equiv c$ proves the computational equivalence of two constructors. This is often called a \emph{definitional equality} in the world of type theory.
243 \item $\Gamma \vdash e : \tau$ is a standard typing judgment. 245 \item $\Gamma \vdash e : \tau$ is a standard typing judgment.
246 \item $\Gamma \vdash p \leadsto \Gamma, \tau$ combines typing of patterns with calculation of which new variables they bind.
247 \item $\Gamma \vdash d \leadsto \Gamma$ expresses how a declaration modifies a context. We overload this judgment to apply to sequences of declarations.
244 \item $\Gamma \vdash M : S$ is the module signature checking judgment. 248 \item $\Gamma \vdash M : S$ is the module signature checking judgment.
245 \item $\mt{proj}(M, S, V)$ is a partial function for projecting a signature item from a signature $S$, given the module $M$ that we project from. $V$ may be $\mt{con} \; x$, $\mt{val} \; x$, $\mt{signature} \; X$, or $\mt{structure} \; X$. The parameter $M$ is needed because the projected signature item may refer to other items of $S$. 249 \item $\mt{proj}(M, S, V)$ is a partial function for projecting a signature item from a signature $S$, given the module $M$ that we project from. $V$ may be $\mt{con} \; x$, $\mt{val} \; x$, $\mt{signature} \; X$, or $\mt{structure} \; X$. The parameter $M$ is needed because the projected signature item may refer to other items of $S$.
246 \end{itemize} 250 \end{itemize}
247 251
248 \subsection{Kinding} 252 \subsection{Kinding}
391 \quad \infer{\Gamma \vdash \mt{fold} \; f \; i \; (\mt{map} \; f' \; c) 395 \quad \infer{\Gamma \vdash \mt{fold} \; f \; i \; (\mt{map} \; f' \; c)
392 \equiv \mt{fold} \; (\lambda (x_1 :: \mt{Name}) (x_2 :: \kappa) \Rightarrow f \; x_1 \; (f' \; x_2)) \; i \; c}{}$$ 396 \equiv \mt{fold} \; (\lambda (x_1 :: \mt{Name}) (x_2 :: \kappa) \Rightarrow f \; x_1 \; (f' \; x_2)) \; i \; c}{}$$
393 397
394 $$\infer{\Gamma \vdash \mt{map} \; f \; (c_1 \rc c_2) \equiv \mt{map} \; f \; c_1 \rc \mt{map} \; f \; c_2}{}$$ 398 $$\infer{\Gamma \vdash \mt{map} \; f \; (c_1 \rc c_2) \equiv \mt{map} \; f \; c_1 \rc \mt{map} \; f \; c_2}{}$$
395 399
400 \subsection{Typing}
401
402 We assume the existence of a function $T$ assigning types to literal constants. It maps integer constants to $\mt{Basis}.\mt{int}$, float constants to $\mt{Basis}.\mt{float}$, and string constants to $\mt{Basis}.\mt{string}$.
403
404 We also refer to a function $\mathcal I$, such that $\mathcal I(\tau)$ ``uses an oracle'' to instantiate all constructor function arguments at the beginning of $\tau$ that are marked implicit; i.e., replace $x_1 ::: \kappa_1 \to \ldots \to x_n ::: \kappa_n \to \tau$ with $[x_1 \mapsto c_1]\ldots[x_n \mapsto c_n]\tau$, where the $c_i$s are inferred and $\tau$ does not start like $x ::: \kappa \to \tau'$.
405
406 $$\infer{\Gamma \vdash e : \tau : \tau}{
407 \Gamma \vdash e : \tau
408 }
409 \quad \infer{\Gamma \vdash e : \tau}{
410 \Gamma \vdash e : \tau'
411 & \Gamma \vdash \tau' \equiv \tau
412 }
413 \quad \infer{\Gamma \vdash \ell : T(\ell)}{}$$
414
415 $$\infer{\Gamma \vdash x : \mathcal I(\tau)}{
416 x : \tau \in \Gamma
417 }
418 \quad \infer{\Gamma \vdash M.x : \mathcal I(\tau)}{
419 \Gamma \vdash M : S
420 & \mt{proj}(M, S, \mt{val} \; x) = \tau
421 }
422 \quad \infer{\Gamma \vdash X : \mathcal I(\tau)}{
423 X : \tau \in \Gamma
424 }
425 \quad \infer{\Gamma \vdash M.X : \mathcal I(\tau)}{
426 \Gamma \vdash M : S
427 & \mt{proj}(M, S, \mt{val} \; X) = \tau
428 }$$
429
430 $$\infer{\Gamma \vdash e_1 \; e_2 : \tau_2}{
431 \Gamma \vdash e_1 : \tau_1 \to \tau_2
432 & \Gamma \vdash e_2 : \tau_1
433 }
434 \quad \infer{\Gamma \vdash \lambda x : \tau_1 \Rightarrow e : \tau_1 \to \tau_2}{
435 \Gamma, x : \tau_1 \vdash e : \tau_2
436 }$$
437
438 $$\infer{\Gamma \vdash e [c] : [x \mapsto c]\tau}{
439 \Gamma \vdash e : x :: \kappa \to \tau
440 & \Gamma \vdash c :: \kappa
441 }
442 \quad \infer{\Gamma \vdash \lambda x \; ? \; \kappa \Rightarrow e : x \; ? \; \kappa \to \tau}{
443 \Gamma, x :: \kappa \vdash e : \tau
444 }$$
445
446 $$\infer{\Gamma \vdash \{\overline{c = e}\} : \{\overline{c : \tau}\}}{
447 \forall i: \Gamma \vdash c_i :: \mt{Name}
448 & \Gamma \vdash e_i : \tau_i
449 & \forall i \neq j: \Gamma \vdash c_i \sim c_j
450 }
451 \quad \infer{\Gamma \vdash e.c : \tau}{
452 \Gamma \vdash e : \$([c = \tau] \rc c')
453 }
454 \quad \infer{\Gamma \vdash e_1 \rc e_2 : \$(c_1 \rc c_2)}{
455 \Gamma \vdash e_1 : \$c_1
456 & \Gamma \vdash e_2 : \$c_2
457 }$$
458
459 $$\infer{\Gamma \vdash e \rcut c : \$c'}{
460 \Gamma \vdash e : \$([c = \tau] \rc c')
461 }
462 \quad \infer{\Gamma \vdash e \rcutM c : \$c'}{
463 \Gamma \vdash e : \$(c \rc c')
464 }$$
465
466 $$\infer{\Gamma \vdash \mt{fold} : \begin{array}{c}
467 x_1 :: (\{\kappa\} \to \tau)
468 \to (x_2 :: \mt{Name} \to x_3 :: \kappa \to x_4 :: \{\kappa\} \to \lambda [[x_2] \sim x_4]
469 \Rightarrow x_1 \; x_4 \to x_1 \; ([x_2 = x_3] \rc x_4)) \\
470 \to x_1 \; [] \to x_5 :: \{\kappa\} \to x_1 \; x_5
471 \end{array}}{}$$
472
473 $$\infer{\Gamma \vdash \mt{let} \; \overline{ed} \; \mt{in} \; e \; \mt{end} : \tau}{
474 \Gamma \vdash \overline{ed} \leadsto \Gamma'
475 & \Gamma' \vdash e : \tau
476 }
477 \quad \infer{\Gamma \vdash \mt{case} \; e \; \mt{of} \; \overline{p \Rightarrow e} : \tau}{
478 \forall i: \Gamma \vdash p_i \leadsto \Gamma_i, \tau'
479 & \Gamma_i \vdash e_i : \tau
480 }$$
481
482 $$\infer{\Gamma \vdash [c_1 \sim c_2] \Rightarrow e : [c_1 \sim c_2] \Rightarrow \tau}{
483 \Gamma \vdash c_1 :: \{\kappa\}
484 & \Gamma \vdash c_2 :: \{\kappa\}
485 & \Gamma, c_1 \sim c_2 \vdash e : \tau
486 }$$
487
396 \end{document} 488 \end{document}