comparison doc/manual.tex @ 655:42ca2ab55f91

Revise manual, through static semantics
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Mar 2009 11:18:54 -0400
parents a0f2b070af01
children 3be5e6f01c32
comparison
equal deleted inserted replaced
654:a0f2b070af01 655:42ca2ab55f91
209 \\ 209 \\
210 &&& c \; c & \textrm{type-level function application} \\ 210 &&& c \; c & \textrm{type-level function application} \\
211 &&& \lambda x \; :: \; \kappa \Rightarrow c & \textrm{type-level function abstraction} \\ 211 &&& \lambda x \; :: \; \kappa \Rightarrow c & \textrm{type-level function abstraction} \\
212 \\ 212 \\
213 &&& X \Longrightarrow c & \textrm{type-level kind-polymorphic function abstraction} \\ 213 &&& X \Longrightarrow c & \textrm{type-level kind-polymorphic function abstraction} \\
214 &&& c [\kappa] & \textrm{type-level kind-polymorphic function application} \\
214 \\ 215 \\
215 &&& () & \textrm{type-level unit} \\ 216 &&& () & \textrm{type-level unit} \\
216 &&& \#X & \textrm{field name} \\ 217 &&& \#X & \textrm{field name} \\
217 \\ 218 \\
218 &&& [(c = c)^*] & \textrm{known-length type-level record} \\ 219 &&& [(c = c)^*] & \textrm{known-length type-level record} \\
228 &&& (c) & \textrm{explicit precedence} \\ 229 &&& (c) & \textrm{explicit precedence} \\
229 \\ 230 \\
230 \textrm{Qualified uncapitalized variables} & \hat{x} &::=& x & \textrm{not from a module} \\ 231 \textrm{Qualified uncapitalized variables} & \hat{x} &::=& x & \textrm{not from a module} \\
231 &&& M.x & \textrm{projection from a module} \\ 232 &&& M.x & \textrm{projection from a module} \\
232 \end{array}$$ 233 \end{array}$$
234
235 We include both abstraction and application for kind polymorphism, but applications are only inferred internally; they may not be written explicitly in source programs.
233 236
234 Modules of the module system are described by \emph{signatures}. 237 Modules of the module system are described by \emph{signatures}.
235 $$\begin{array}{rrcll} 238 $$\begin{array}{rrcll}
236 \textrm{Signatures} & S &::=& \mt{sig} \; s^* \; \mt{end} & \textrm{constant} \\ 239 \textrm{Signatures} & S &::=& \mt{sig} \; s^* \; \mt{end} & \textrm{constant} \\
237 &&& X & \textrm{variable} \\ 240 &&& X & \textrm{variable} \\
279 \\ 282 \\
280 &&& e \; e & \textrm{function application} \\ 283 &&& e \; e & \textrm{function application} \\
281 &&& \lambda x : \tau \Rightarrow e & \textrm{function abstraction} \\ 284 &&& \lambda x : \tau \Rightarrow e & \textrm{function abstraction} \\
282 &&& e [c] & \textrm{polymorphic function application} \\ 285 &&& e [c] & \textrm{polymorphic function application} \\
283 &&& \lambda x \; ? \; \kappa \Rightarrow e & \textrm{polymorphic function abstraction} \\ 286 &&& \lambda x \; ? \; \kappa \Rightarrow e & \textrm{polymorphic function abstraction} \\
287 &&& e [\kappa] & \textrm{kind-polymorphic function application} \\
284 &&& X \Longrightarrow e & \textrm{kind-polymorphic function abstraction} \\ 288 &&& X \Longrightarrow e & \textrm{kind-polymorphic function abstraction} \\
285 \\ 289 \\
286 &&& \{(c = e,)^*\} & \textrm{known-length record} \\ 290 &&& \{(c = e,)^*\} & \textrm{known-length record} \\
287 &&& e.c & \textrm{record field projection} \\ 291 &&& e.c & \textrm{record field projection} \\
288 &&& e \rc e & \textrm{record concatenation} \\ 292 &&& e \rc e & \textrm{record concatenation} \\
300 &&& (e) & \textrm{explicit precedence} \\ 304 &&& (e) & \textrm{explicit precedence} \\
301 \\ 305 \\
302 \textrm{Local declarations} & ed &::=& \cd{val} \; x : \tau = e & \textrm{non-recursive value} \\ 306 \textrm{Local declarations} & ed &::=& \cd{val} \; x : \tau = e & \textrm{non-recursive value} \\
303 &&& \cd{val} \; \cd{rec} \; (x : \tau = e \; \cd{and})^+ & \textrm{mutually-recursive values} \\ 307 &&& \cd{val} \; \cd{rec} \; (x : \tau = e \; \cd{and})^+ & \textrm{mutually-recursive values} \\
304 \end{array}$$ 308 \end{array}$$
309
310 As with constructors, we include both abstraction and application for kind polymorphism, but applications are only inferred internally.
305 311
306 \emph{Declarations} primarily bring new symbols into context. 312 \emph{Declarations} primarily bring new symbols into context.
307 $$\begin{array}{rrcll} 313 $$\begin{array}{rrcll}
308 \textrm{Declarations} & d &::=& \mt{con} \; x :: \kappa = c & \textrm{constructor synonym} \\ 314 \textrm{Declarations} & d &::=& \mt{con} \; x :: \kappa = c & \textrm{constructor synonym} \\
309 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\ 315 &&& \mt{datatype} \; x \; x^* = dc\mid^+ & \textrm{algebraic datatype definition} \\
372 378
373 In this section, we give a declarative presentation of Ur's typing rules and related judgments. Inference is the subject of the next section; here, we assume that an oracle has filled in all wildcards with concrete values. 379 In this section, we give a declarative presentation of Ur's typing rules and related judgments. Inference is the subject of the next section; here, we assume that an oracle has filled in all wildcards with concrete values.
374 380
375 Since there is significant mutual recursion among the judgments, we introduce them all before beginning to give rules. We use the same variety of contexts throughout this section, implicitly introducing new sorts of context entries as needed. 381 Since there is significant mutual recursion among the judgments, we introduce them all before beginning to give rules. We use the same variety of contexts throughout this section, implicitly introducing new sorts of context entries as needed.
376 \begin{itemize} 382 \begin{itemize}
383 \item $\Gamma \vdash \kappa$ expresses kind well-formedness.
377 \item $\Gamma \vdash c :: \kappa$ assigns a kind to a constructor in a context. 384 \item $\Gamma \vdash c :: \kappa$ assigns a kind to a constructor in a context.
378 \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. 385 \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.
379 \item $\Gamma \vdash c \hookrightarrow C$ proves that record constructor $c$ decomposes into set $C$ of field names and record constructors. 386 \item $\Gamma \vdash c \hookrightarrow C$ proves that record constructor $c$ decomposes into set $C$ of field names and record constructors.
380 \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. 387 \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.
381 \item $\Gamma \vdash e : \tau$ is a standard typing judgment. 388 \item $\Gamma \vdash e : \tau$ is a standard typing judgment.
386 \item $\Gamma \vdash M : S$ is the module signature checking judgment. 393 \item $\Gamma \vdash M : S$ is the module signature checking judgment.
387 \item $\mt{proj}(M, \overline{s}, V)$ is a partial function for projecting a signature item from $\overline{s}$, given the module $M$ that we project from. $V$ may be $\mt{con} \; x$, $\mt{datatype} \; 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 from $\overline{s}$. 394 \item $\mt{proj}(M, \overline{s}, V)$ is a partial function for projecting a signature item from $\overline{s}$, given the module $M$ that we project from. $V$ may be $\mt{con} \; x$, $\mt{datatype} \; 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 from $\overline{s}$.
388 \item $\mt{selfify}(M, \overline{s})$ adds information to signature items $\overline{s}$ to reflect the fact that we are concerned with the particular module $M$. This function is overloaded to work over individual signature items as well. 395 \item $\mt{selfify}(M, \overline{s})$ adds information to signature items $\overline{s}$ to reflect the fact that we are concerned with the particular module $M$. This function is overloaded to work over individual signature items as well.
389 \end{itemize} 396 \end{itemize}
390 397
398
399 \subsection{Kind Well-Formedness}
400
401 $$\infer{\Gamma \vdash \mt{Type}}{}
402 \quad \infer{\Gamma \vdash \mt{Unit}}{}
403 \quad \infer{\Gamma \vdash \mt{Name}}{}
404 \quad \infer{\Gamma \vdash \kappa_1 \to \kappa_2}{
405 \Gamma \vdash \kappa_1
406 & \Gamma \vdash \kappa_2
407 }
408 \quad \infer{\Gamma \vdash \{\kappa\}}{
409 \Gamma \vdash \kappa
410 }
411 \quad \infer{\Gamma \vdash (\kappa_1 \times \ldots \times \kappa_n)}{
412 \forall i: \Gamma \vdash \kappa_i
413 }$$
414
415 $$\infer{\Gamma \vdash X}{
416 X \in \Gamma
417 }
418 \quad \infer{\Gamma \vdash X \longrightarrow \kappa}{
419 \Gamma, X \vdash \kappa
420 }$$
421
391 \subsection{Kinding} 422 \subsection{Kinding}
423
424 We write $[X \mapsto \kappa_1]\kappa_2$ for capture-avoiding substitution of $\kappa_1$ for $X$ in $\kappa_2$.
392 425
393 $$\infer{\Gamma \vdash (c) :: \kappa :: \kappa}{ 426 $$\infer{\Gamma \vdash (c) :: \kappa :: \kappa}{
394 \Gamma \vdash c :: \kappa 427 \Gamma \vdash c :: \kappa
395 } 428 }
396 \quad \infer{\Gamma \vdash x :: \kappa}{ 429 \quad \infer{\Gamma \vdash x :: \kappa}{
414 & \Gamma \vdash \tau_2 :: \mt{Type} 447 & \Gamma \vdash \tau_2 :: \mt{Type}
415 } 448 }
416 \quad \infer{\Gamma \vdash x \; ? \: \kappa \to \tau :: \mt{Type}}{ 449 \quad \infer{\Gamma \vdash x \; ? \: \kappa \to \tau :: \mt{Type}}{
417 \Gamma, x :: \kappa \vdash \tau :: \mt{Type} 450 \Gamma, x :: \kappa \vdash \tau :: \mt{Type}
418 } 451 }
452 \quad \infer{\Gamma \vdash X \longrightarrow \tau :: \mt{Type}}{
453 \Gamma, X \vdash \tau :: \mt{Type}
454 }
419 \quad \infer{\Gamma \vdash \$c :: \mt{Type}}{ 455 \quad \infer{\Gamma \vdash \$c :: \mt{Type}}{
420 \Gamma \vdash c :: \{\mt{Type}\} 456 \Gamma \vdash c :: \{\mt{Type}\}
421 }$$ 457 }$$
422 458
423 $$\infer{\Gamma \vdash c_1 \; c_2 :: \kappa_2}{ 459 $$\infer{\Gamma \vdash c_1 \; c_2 :: \kappa_2}{
424 \Gamma \vdash c_1 :: \kappa_1 \to \kappa_2 460 \Gamma \vdash c_1 :: \kappa_1 \to \kappa_2
425 & \Gamma \vdash c_2 :: \kappa_1 461 & \Gamma \vdash c_2 :: \kappa_1
426 } 462 }
427 \quad \infer{\Gamma \vdash \lambda x \; :: \; \kappa_1 \Rightarrow c :: \kappa_1 \to \kappa_2}{ 463 \quad \infer{\Gamma \vdash \lambda x \; :: \; \kappa_1 \Rightarrow c :: \kappa_1 \to \kappa_2}{
428 \Gamma, x :: \kappa_1 \vdash c :: \kappa_2 464 \Gamma, x :: \kappa_1 \vdash c :: \kappa_2
465 }$$
466
467 $$\infer{\Gamma \vdash c[\kappa'] :: [X \mapsto \kappa']\kappa}{
468 \Gamma \vdash c :: X \to \kappa
469 & \Gamma \vdash \kappa'
470 }
471 \quad \infer{\Gamma \vdash X \Longrightarrow c :: X \to \kappa}{
472 \Gamma, X \vdash c :: \kappa
429 }$$ 473 }$$
430 474
431 $$\infer{\Gamma \vdash () :: \mt{Unit}}{} 475 $$\infer{\Gamma \vdash () :: \mt{Unit}}{}
432 \quad \infer{\Gamma \vdash \#X :: \mt{Name}}{}$$ 476 \quad \infer{\Gamma \vdash \#X :: \mt{Name}}{}$$
433 477
440 \Gamma \vdash c_1 :: \{\kappa\} 484 \Gamma \vdash c_1 :: \{\kappa\}
441 & \Gamma \vdash c_2 :: \{\kappa\} 485 & \Gamma \vdash c_2 :: \{\kappa\}
442 & \Gamma \vdash c_1 \sim c_2 486 & \Gamma \vdash c_1 \sim c_2
443 }$$ 487 }$$
444 488
445 $$\infer{\Gamma \vdash \mt{fold} :: ((\mt{Name} \to \kappa_1 \to \kappa_2 \to \kappa_2) \to \kappa_2 \to \{\kappa_1\} \to \kappa_2}{}$$ 489 $$\infer{\Gamma \vdash \mt{map} :: (\kappa_1 \to \kappa_2) \to \{\kappa_1\} \to \{\kappa_2\}}{}$$
446 490
447 $$\infer{\Gamma \vdash (\overline c) :: (\kappa_1 \times \ldots \times \kappa_n)}{ 491 $$\infer{\Gamma \vdash (\overline c) :: (\kappa_1 \times \ldots \times \kappa_n)}{
448 \forall i: \Gamma \vdash c_i :: \kappa_i 492 \forall i: \Gamma \vdash c_i :: \kappa_i
449 } 493 }
450 \quad \infer{\Gamma \vdash c.i :: \kappa_i}{ 494 \quad \infer{\Gamma \vdash c.i :: \kappa_i}{
451 \Gamma \vdash c :: (\kappa_1 \times \ldots \times \kappa_n) 495 \Gamma \vdash c :: (\kappa_1 \times \ldots \times \kappa_n)
452 }$$ 496 }$$
453 497
454 $$\infer{\Gamma \vdash \lambda [c_1 \sim c_2] \Rightarrow c :: \kappa}{ 498 $$\infer{\Gamma \vdash \lambda [c_1 \sim c_2] \Rightarrow \tau :: \mt{Type}}{
455 \Gamma \vdash c_1 :: \{\kappa'\} 499 \Gamma \vdash c_1 :: \{\kappa\}
456 & \Gamma \vdash c_2 :: \{\kappa'\} 500 & \Gamma \vdash c_2 :: \{\kappa'\}
457 & \Gamma, c_1 \sim c_2 \vdash c :: \kappa 501 & \Gamma, c_1 \sim c_2 \vdash \tau :: \mt{Type}
458 }$$ 502 }$$
459 503
460 \subsection{Record Disjointness} 504 \subsection{Record Disjointness}
461
462 We will use a keyword $\mt{map}$ as a shorthand, such that, for $f$ of kind $\kappa \to \kappa'$, $\mt{map} \; f$ stands for $\mt{fold} \; (\lambda (x_1 :: \mt{Name}) (x_2 :: \kappa) (x_3 :: \{\kappa'\}) \Rightarrow [x_1 = f \; x_2] \rc x_3) \; []$.
463 505
464 $$\infer{\Gamma \vdash c_1 \sim c_2}{ 506 $$\infer{\Gamma \vdash c_1 \sim c_2}{
465 \Gamma \vdash c_1 \hookrightarrow C_1 507 \Gamma \vdash c_1 \hookrightarrow C_1
466 & \Gamma \vdash c_2 \hookrightarrow C_2 508 & \Gamma \vdash c_2 \hookrightarrow C_2
467 & \forall c'_1 \in C_1, c'_2 \in C_2: \Gamma \vdash c'_1 \sim c'_2 509 & \forall c'_1 \in C_1, c'_2 \in C_2: \Gamma \vdash c'_1 \sim c'_2
492 \Gamma \vdash c \hookrightarrow C 534 \Gamma \vdash c \hookrightarrow C
493 }$$ 535 }$$
494 536
495 \subsection{\label{definitional}Definitional Equality} 537 \subsection{\label{definitional}Definitional Equality}
496 538
497 We use $\mathcal C$ to stand for a one-hole context that, when filled, yields a constructor. The notation $\mathcal C[c]$ plugs $c$ into $\mathcal C$. We omit the standard definition of one-hole contexts. We write $[x \mapsto c_1]c_2$ for capture-avoiding substitution of $c_1$ for $x$ in $c_2$. 539 We use $\mathcal C$ to stand for a one-hole context that, when filled, yields a constructor. The notation $\mathcal C[c]$ plugs $c$ into $\mathcal C$. We omit the standard definition of one-hole contexts. We write $[x \mapsto c_1]c_2$ for capture-avoiding substitution of $c_1$ for $x$ in $c_2$, with analogous notation for substituting a kind in a constructor.
498 540
499 $$\infer{\Gamma \vdash c \equiv c}{} 541 $$\infer{\Gamma \vdash c \equiv c}{}
500 \quad \infer{\Gamma \vdash c_1 \equiv c_2}{ 542 \quad \infer{\Gamma \vdash c_1 \equiv c_2}{
501 \Gamma \vdash c_2 \equiv c_1 543 \Gamma \vdash c_2 \equiv c_1
502 } 544 }
516 & \mt{proj}(M, \overline{s}, \mt{con} \; x) = (\kappa, c) 558 & \mt{proj}(M, \overline{s}, \mt{con} \; x) = (\kappa, c)
517 } 559 }
518 \quad \infer{\Gamma \vdash (\overline c).i \equiv c_i}{}$$ 560 \quad \infer{\Gamma \vdash (\overline c).i \equiv c_i}{}$$
519 561
520 $$\infer{\Gamma \vdash (\lambda x :: \kappa \Rightarrow c) \; c' \equiv [x \mapsto c'] c}{} 562 $$\infer{\Gamma \vdash (\lambda x :: \kappa \Rightarrow c) \; c' \equiv [x \mapsto c'] c}{}
521 \quad \infer{\Gamma \vdash c_1 \rc c_2 \equiv c_2 \rc c_1}{} 563 \quad \infer{\Gamma \vdash (X \Longrightarrow c) [\kappa] \equiv [X \mapsto \kappa] c}{}$$
564
565 $$\infer{\Gamma \vdash c_1 \rc c_2 \equiv c_2 \rc c_1}{}
522 \quad \infer{\Gamma \vdash c_1 \rc (c_2 \rc c_3) \equiv (c_1 \rc c_2) \rc c_3}{}$$ 566 \quad \infer{\Gamma \vdash c_1 \rc (c_2 \rc c_3) \equiv (c_1 \rc c_2) \rc c_3}{}$$
523 567
524 $$\infer{\Gamma \vdash [] \rc c \equiv c}{} 568 $$\infer{\Gamma \vdash [] \rc c \equiv c}{}
525 \quad \infer{\Gamma \vdash [\overline{c_1 = c'_1}] \rc [\overline{c_2 = c'_2}] \equiv [\overline{c_1 = c'_1}, \overline{c_2 = c'_2}]}{}$$ 569 \quad \infer{\Gamma \vdash [\overline{c_1 = c'_1}] \rc [\overline{c_2 = c'_2}] \equiv [\overline{c_1 = c'_1}, \overline{c_2 = c'_2}]}{}$$
526 570
527 $$\infer{\Gamma \vdash \lambda [c_1 \sim c_2] \Rightarrow c \equiv c}{ 571 $$\infer{\Gamma \vdash \mt{map} \; f \; [] \equiv []}{}
528 \Gamma \vdash c_1 \sim c_2 572 \quad \infer{\Gamma \vdash \mt{map} \; f \; ([c_1 = c_2] \rc c) \equiv [c_1 = f \; c_2] \rc \mt{map} \; f \; c}{}$$
529 }
530 \quad \infer{\Gamma \vdash \mt{fold} \; f \; i \; [] \equiv i}{}
531 \quad \infer{\Gamma \vdash \mt{fold} \; f \; i \; ([c_1 = c_2] \rc c) \equiv f \; c_1 \; c_2 \; (\mt{fold} \; f \; i \; c)}{}$$
532 573
533 $$\infer{\Gamma \vdash \mt{map} \; (\lambda x \Rightarrow x) \; c \equiv c}{} 574 $$\infer{\Gamma \vdash \mt{map} \; (\lambda x \Rightarrow x) \; c \equiv c}{}
534 \quad \infer{\Gamma \vdash \mt{fold} \; f \; i \; (\mt{map} \; f' \; c) 575 \quad \infer{\Gamma \vdash \mt{map} \; f \; (\mt{map} \; f' \; c)
535 \equiv \mt{fold} \; (\lambda (x_1 :: \mt{Name}) (x_2 :: \kappa) \Rightarrow f \; x_1 \; (f' \; x_2)) \; i \; c}{}$$ 576 \equiv \mt{map} \; (\lambda x \Rightarrow f \; (f' \; x)) \; c}{}$$
536 577
537 $$\infer{\Gamma \vdash \mt{map} \; f \; (c_1 \rc c_2) \equiv \mt{map} \; f \; c_1 \rc \mt{map} \; f \; c_2}{}$$ 578 $$\infer{\Gamma \vdash \mt{map} \; f \; (c_1 \rc c_2) \equiv \mt{map} \; f \; c_1 \rc \mt{map} \; f \; c_2}{}$$
538 579
539 \subsection{Expression Typing} 580 \subsection{Expression Typing}
540 581
578 \Gamma \vdash e : x :: \kappa \to \tau 619 \Gamma \vdash e : x :: \kappa \to \tau
579 & \Gamma \vdash c :: \kappa 620 & \Gamma \vdash c :: \kappa
580 } 621 }
581 \quad \infer{\Gamma \vdash \lambda x \; ? \; \kappa \Rightarrow e : x \; ? \; \kappa \to \tau}{ 622 \quad \infer{\Gamma \vdash \lambda x \; ? \; \kappa \Rightarrow e : x \; ? \; \kappa \to \tau}{
582 \Gamma, x :: \kappa \vdash e : \tau 623 \Gamma, x :: \kappa \vdash e : \tau
624 }$$
625
626 $$\infer{\Gamma \vdash e [\kappa] : [X \mapsto \kappa]\tau}{
627 \Gamma \vdash e : X \longrightarrow \tau
628 & \Gamma \vdash \kappa
629 }
630 \quad \infer{\Gamma \vdash X \Longrightarrow e : X \longrightarrow \tau}{
631 \Gamma, X \vdash e : \tau
583 }$$ 632 }$$
584 633
585 $$\infer{\Gamma \vdash \{\overline{c = e}\} : \{\overline{c : \tau}\}}{ 634 $$\infer{\Gamma \vdash \{\overline{c = e}\} : \{\overline{c : \tau}\}}{
586 \forall i: \Gamma \vdash c_i :: \mt{Name} 635 \forall i: \Gamma \vdash c_i :: \mt{Name}
587 & \Gamma \vdash e_i : \tau_i 636 & \Gamma \vdash e_i : \tau_i
601 } 650 }
602 \quad \infer{\Gamma \vdash e \rcutM c : \$c'}{ 651 \quad \infer{\Gamma \vdash e \rcutM c : \$c'}{
603 \Gamma \vdash e : \$(c \rc c') 652 \Gamma \vdash e : \$(c \rc c')
604 }$$ 653 }$$
605 654
606 $$\infer{\Gamma \vdash \mt{fold} : \begin{array}{c}
607 x_1 :: (\{\kappa\} \to \tau)
608 \to (x_2 :: \mt{Name} \to x_3 :: \kappa \to x_4 :: \{\kappa\} \to \lambda [[x_2] \sim x_4]
609 \Rightarrow x_1 \; x_4 \to x_1 \; ([x_2 = x_3] \rc x_4)) \\
610 \to x_1 \; [] \to x_5 :: \{\kappa\} \to x_1 \; x_5
611 \end{array}}{}$$
612
613 $$\infer{\Gamma \vdash \mt{let} \; \overline{ed} \; \mt{in} \; e \; \mt{end} : \tau}{ 655 $$\infer{\Gamma \vdash \mt{let} \; \overline{ed} \; \mt{in} \; e \; \mt{end} : \tau}{
614 \Gamma \vdash \overline{ed} \leadsto \Gamma' 656 \Gamma \vdash \overline{ed} \leadsto \Gamma'
615 & \Gamma' \vdash e : \tau 657 & \Gamma' \vdash e : \tau
616 } 658 }
617 \quad \infer{\Gamma \vdash \mt{case} \; e \; \mt{of} \; \overline{p \Rightarrow e} : \tau}{ 659 \quad \infer{\Gamma \vdash \mt{case} \; e \; \mt{of} \; \overline{p \Rightarrow e} : \tau}{
619 & \Gamma_i \vdash e_i : \tau 661 & \Gamma_i \vdash e_i : \tau
620 }$$ 662 }$$
621 663
622 $$\infer{\Gamma \vdash \lambda [c_1 \sim c_2] \Rightarrow e : \lambda [c_1 \sim c_2] \Rightarrow \tau}{ 664 $$\infer{\Gamma \vdash \lambda [c_1 \sim c_2] \Rightarrow e : \lambda [c_1 \sim c_2] \Rightarrow \tau}{
623 \Gamma \vdash c_1 :: \{\kappa\} 665 \Gamma \vdash c_1 :: \{\kappa\}
624 & \Gamma \vdash c_2 :: \{\kappa\} 666 & \Gamma \vdash c_2 :: \{\kappa'\}
625 & \Gamma, c_1 \sim c_2 \vdash e : \tau 667 & \Gamma, c_1 \sim c_2 \vdash e : \tau
626 }$$ 668 }$$
627 669
628 \subsection{Pattern Typing} 670 \subsection{Pattern Typing}
629 671
663 705
664 \subsection{Declaration Typing} 706 \subsection{Declaration Typing}
665 707
666 We use an auxiliary judgment $\overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma'$, expressing the enrichment of $\Gamma$ with the types of the datatype constructors $\overline{dc}$, when they are known to belong to datatype $x$ with type parameters $\overline{y}$. 708 We use an auxiliary judgment $\overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma'$, expressing the enrichment of $\Gamma$ with the types of the datatype constructors $\overline{dc}$, when they are known to belong to datatype $x$ with type parameters $\overline{y}$.
667 709
668 This is the first judgment where we deal with type classes, for the $\mt{class}$ declaration form. We will omit their special handling in this formal specification. Section \ref{typeclasses} gives an informal description of how type classes influence type inference. 710 This is the first judgment where we deal with constructor classes, for the $\mt{class}$ declaration form. We will omit their special handling in this formal specification. Section \ref{typeclasses} gives an informal description of how constructor classes influence type inference.
669 711
670 We presuppose the existence of a function $\mathcal O$, where $\mathcal O(M, \overline{s})$ implements the $\mt{open}$ declaration by producing a context with the appropriate entry for each available component of module $M$ with signature items $\overline{s}$. Where possible, $\mathcal O$ uses ``transparent'' entries (e.g., an abstract type $M.x$ is mapped to $x :: \mt{Type} = M.x$), so that the relationship with $M$ is maintained. A related function $\mathcal O_c$ builds a context containing the disjointness constraints found in $\overline s$. 712 We presuppose the existence of a function $\mathcal O$, where $\mathcal O(M, \overline{s})$ implements the $\mt{open}$ declaration by producing a context with the appropriate entry for each available component of module $M$ with signature items $\overline{s}$. Where possible, $\mathcal O$ uses ``transparent'' entries (e.g., an abstract type $M.x$ is mapped to $x :: \mt{Type} = M.x$), so that the relationship with $M$ is maintained. A related function $\mathcal O_c$ builds a context containing the disjointness constraints found in $\overline s$.
671 We write $\kappa_1^n \to \kappa$ as a shorthand, where $\kappa_1^0 \to \kappa = \kappa$ and $\kappa_1^{n+1} \to \kappa_2 = \kappa_1 \to (\kappa_1^n \to \kappa_2)$. We write $\mt{len}(\overline{y})$ for the length of vector $\overline{y}$ of variables. 713 We write $\kappa_1^n \to \kappa$ as a shorthand, where $\kappa_1^0 \to \kappa = \kappa$ and $\kappa_1^{n+1} \to \kappa_2 = \kappa_1 \to (\kappa_1^n \to \kappa_2)$. We write $\mt{len}(\overline{y})$ for the length of vector $\overline{y}$ of variables.
672 714
673 $$\infer{\Gamma \vdash \cdot \leadsto \Gamma}{} 715 $$\infer{\Gamma \vdash \cdot \leadsto \Gamma}{}
730 772
731 $$\infer{\Gamma \vdash \mt{cookie} \; x : \tau \leadsto \Gamma, x : \mt{Basis}.\mt{http\_cookie} \; \tau}{ 773 $$\infer{\Gamma \vdash \mt{cookie} \; x : \tau \leadsto \Gamma, x : \mt{Basis}.\mt{http\_cookie} \; \tau}{
732 \Gamma \vdash \tau :: \mt{Type} 774 \Gamma \vdash \tau :: \mt{Type}
733 }$$ 775 }$$
734 776
735 $$\infer{\Gamma \vdash \mt{class} \; x = c \leadsto \Gamma, x :: \mt{Type} \to \mt{Type} = c}{ 777 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa \to \mt{Type} = c}{
736 \Gamma \vdash c :: \mt{Type} \to \mt{Type} 778 \Gamma \vdash c :: \kappa \to \mt{Type}
737 }$$ 779 }$$
738 780
739 $$\infer{\overline{y}; x; \Gamma \vdash \cdot \leadsto \Gamma}{} 781 $$\infer{\overline{y}; x; \Gamma \vdash \cdot \leadsto \Gamma}{}
740 \quad \infer{\overline{y}; x; \Gamma \vdash X \mid \overline{dc} \leadsto \Gamma', X : \overline{y ::: \mt{Type}} \to x \; \overline{y}}{ 782 \quad \infer{\overline{y}; x; \Gamma \vdash X \mid \overline{dc} \leadsto \Gamma', X : \overline{y ::: \mt{Type}} \to x \; \overline{y}}{
741 \overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma' 783 \overline{y}; x; \Gamma \vdash \overline{dc} \leadsto \Gamma'
787 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leadsto \Gamma, c_1 \sim c_2}{ 829 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leadsto \Gamma, c_1 \sim c_2}{
788 \Gamma \vdash c_1 :: \{\kappa\} 830 \Gamma \vdash c_1 :: \{\kappa\}
789 & \Gamma \vdash c_2 :: \{\kappa\} 831 & \Gamma \vdash c_2 :: \{\kappa\}
790 }$$ 832 }$$
791 833
792 $$\infer{\Gamma \vdash \mt{class} \; x = c \leadsto \Gamma, x :: \mt{Type} \to \mt{Type} = c}{ 834 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leadsto \Gamma, x :: \kappa \to \mt{Type} = c}{
793 \Gamma \vdash c :: \mt{Type} \to \mt{Type} 835 \Gamma \vdash c :: \kappa \to \mt{Type}
794 } 836 }
795 \quad \infer{\Gamma \vdash \mt{class} \; x \leadsto \Gamma, x :: \mt{Type} \to \mt{Type}}{}$$ 837 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa \leadsto \Gamma, x :: \kappa \to \mt{Type}}{}$$
796 838
797 \subsection{Signature Compatibility} 839 \subsection{Signature Compatibility}
798 840
799 To simplify the judgments in this section, we assume that all signatures are alpha-varied as necessary to avoid including multiple bindings for the same identifier. This is in addition to the usual alpha-variation of locally-bound variables. 841 To simplify the judgments in this section, we assume that all signatures are alpha-varied as necessary to avoid including multiple bindings for the same identifier. This is in addition to the usual alpha-variation of locally-bound variables.
800 842
850 $$\infer{\Gamma \vdash \mt{datatype} \; x = \mt{datatype} \; M.z \leq \mt{con} \; x :: \mt{Type}^{\mt{len}(y)} \to \mt{Type}}{ 892 $$\infer{\Gamma \vdash \mt{datatype} \; x = \mt{datatype} \; M.z \leq \mt{con} \; x :: \mt{Type}^{\mt{len}(y)} \to \mt{Type}}{
851 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end} 893 \Gamma \vdash M : \mt{sig} \; \overline{s} \; \mt{end}
852 & \mt{proj}(M, \overline{s}, \mt{datatype} \; z) = (\overline{y}, \overline{dc}) 894 & \mt{proj}(M, \overline{s}, \mt{datatype} \; z) = (\overline{y}, \overline{dc})
853 }$$ 895 }$$
854 896
855 $$\infer{\Gamma \vdash \mt{class} \; x \leq \mt{con} \; x :: \mt{Type} \to \mt{Type}}{} 897 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa \leq \mt{con} \; x :: \kappa \to \mt{Type}}{}
856 \quad \infer{\Gamma \vdash \mt{class} \; x = c \leq \mt{con} \; x :: \mt{Type} \to \mt{Type}}{}$$ 898 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leq \mt{con} \; x :: \kappa \to \mt{Type}}{}$$
857 899
858 $$\infer{\Gamma \vdash \mt{con} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \mt{\kappa} = c_2}{ 900 $$\infer{\Gamma \vdash \mt{con} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \mt{\kappa} = c_2}{
859 \Gamma \vdash c_1 \equiv c_2 901 \Gamma \vdash c_1 \equiv c_2
860 } 902 }
861 \quad \infer{\Gamma \vdash \mt{class} \; x = c_1 \leq \mt{con} \; x :: \mt{Type} \to \mt{Type} = c_2}{ 903 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c_1 \leq \mt{con} \; x :: \kappa \to \mt{Type} = c_2}{
862 \Gamma \vdash c_1 \equiv c_2 904 \Gamma \vdash c_1 \equiv c_2
863 }$$ 905 }$$
864 906
865 $$\infer{\Gamma \vdash \mt{datatype} \; x \; \overline{y} = \overline{dc} \leq \mt{datatype} \; x \; \overline{y} = \overline{dc'}}{ 907 $$\infer{\Gamma \vdash \mt{datatype} \; x \; \overline{y} = \overline{dc} \leq \mt{datatype} \; x \; \overline{y} = \overline{dc'}}{
866 \Gamma, \overline{y :: \mt{Type}} \vdash \overline{dc} \leq \overline{dc'} 908 \Gamma, \overline{y :: \mt{Type}} \vdash \overline{dc} \leq \overline{dc'}
899 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leq \mt{constraint} \; c'_1 \sim c'_2}{ 941 $$\infer{\Gamma \vdash \mt{constraint} \; c_1 \sim c_2 \leq \mt{constraint} \; c'_1 \sim c'_2}{
900 \Gamma \vdash c_1 \equiv c'_1 942 \Gamma \vdash c_1 \equiv c'_1
901 & \Gamma \vdash c_2 \equiv c'_2 943 & \Gamma \vdash c_2 \equiv c'_2
902 }$$ 944 }$$
903 945
904 $$\infer{\Gamma \vdash \mt{class} \; x \leq \mt{class} \; x}{} 946 $$\infer{\Gamma \vdash \mt{class} \; x :: \kappa \leq \mt{class} \; x :: \kappa}{}
905 \quad \infer{\Gamma \vdash \mt{class} \; x = c \leq \mt{class} \; x}{} 947 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c \leq \mt{class} \; x :: \kappa}{}
906 \quad \infer{\Gamma \vdash \mt{class} \; x = c_1 \leq \mt{class} \; x = c_2}{ 948 \quad \infer{\Gamma \vdash \mt{class} \; x :: \kappa = c_1 \leq \mt{class} \; x :: \kappa = c_2}{
907 \Gamma \vdash c_1 \equiv c_2 949 \Gamma \vdash c_1 \equiv c_2
908 }$$ 950 }$$
909 951
910 \subsection{Module Typing} 952 \subsection{Module Typing}
911 953
952 \mt{sigOf}(\mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\ 994 \mt{sigOf}(\mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\
953 \mt{sigOf}(\mt{open} \; \mt{constraints} \; M) &=& \cdot \\ 995 \mt{sigOf}(\mt{open} \; \mt{constraints} \; M) &=& \cdot \\
954 \mt{sigOf}(\mt{table} \; x : c) &=& \mt{table} \; x : c \\ 996 \mt{sigOf}(\mt{table} \; x : c) &=& \mt{table} \; x : c \\
955 \mt{sigOf}(\mt{sequence} \; x) &=& \mt{sequence} \; x \\ 997 \mt{sigOf}(\mt{sequence} \; x) &=& \mt{sequence} \; x \\
956 \mt{sigOf}(\mt{cookie} \; x : \tau) &=& \mt{cookie} \; x : \tau \\ 998 \mt{sigOf}(\mt{cookie} \; x : \tau) &=& \mt{cookie} \; x : \tau \\
957 \mt{sigOf}(\mt{class} \; x = c) &=& \mt{class} \; x = c \\ 999 \mt{sigOf}(\mt{class} \; x :: \kappa = c) &=& \mt{class} \; x :: \kappa = c \\
958 \end{eqnarray*} 1000 \end{eqnarray*}
959 \begin{eqnarray*} 1001 \begin{eqnarray*}
960 \mt{selfify}(M, \cdot) &=& \cdot \\ 1002 \mt{selfify}(M, \cdot) &=& \cdot \\
961 \mt{selfify}(M, s \; \overline{s'}) &=& \mt{selfify}(M, s) \; \mt{selfify}(M, \overline{s'}) \\ 1003 \mt{selfify}(M, s \; \overline{s'}) &=& \mt{selfify}(M, s) \; \mt{selfify}(M, \overline{s'}) \\
962 \\ 1004 \\
967 \mt{selfify}(M, \mt{val} \; x : \tau) &=& \mt{val} \; x : \tau \\ 1009 \mt{selfify}(M, \mt{val} \; x : \tau) &=& \mt{val} \; x : \tau \\
968 \mt{selfify}(M, \mt{structure} \; X : S) &=& \mt{structure} \; X : \mt{selfify}(M.X, \overline{s}) \textrm{ (where $\Gamma \vdash S \equiv \mt{sig} \; \overline{s} \; \mt{end}$)} \\ 1010 \mt{selfify}(M, \mt{structure} \; X : S) &=& \mt{structure} \; X : \mt{selfify}(M.X, \overline{s}) \textrm{ (where $\Gamma \vdash S \equiv \mt{sig} \; \overline{s} \; \mt{end}$)} \\
969 \mt{selfify}(M, \mt{signature} \; X = S) &=& \mt{signature} \; X = S \\ 1011 \mt{selfify}(M, \mt{signature} \; X = S) &=& \mt{signature} \; X = S \\
970 \mt{selfify}(M, \mt{include} \; S) &=& \mt{include} \; S \\ 1012 \mt{selfify}(M, \mt{include} \; S) &=& \mt{include} \; S \\
971 \mt{selfify}(M, \mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\ 1013 \mt{selfify}(M, \mt{constraint} \; c_1 \sim c_2) &=& \mt{constraint} \; c_1 \sim c_2 \\
972 \mt{selfify}(M, \mt{class} \; x) &=& \mt{class} \; x = M.x \\ 1014 \mt{selfify}(M, \mt{class} \; x :: \kappa) &=& \mt{class} \; x :: \kappa = M.x \\
973 \mt{selfify}(M, \mt{class} \; x = c) &=& \mt{class} \; x = c \\ 1015 \mt{selfify}(M, \mt{class} \; x :: \kappa = c) &=& \mt{class} \; x :: \kappa = c \\
974 \end{eqnarray*} 1016 \end{eqnarray*}
975 1017
976 \subsection{Module Projection} 1018 \subsection{Module Projection}
977 1019
978 \begin{eqnarray*} 1020 \begin{eqnarray*}
979 \mt{proj}(M, \mt{con} \; x :: \kappa \; \overline{s}, \mt{con} \; x) &=& \kappa \\ 1021 \mt{proj}(M, \mt{con} \; x :: \kappa \; \overline{s}, \mt{con} \; x) &=& \kappa \\
980 \mt{proj}(M, \mt{con} \; x :: \kappa = c \; \overline{s}, \mt{con} \; x) &=& (\kappa, c) \\ 1022 \mt{proj}(M, \mt{con} \; x :: \kappa = c \; \overline{s}, \mt{con} \; x) &=& (\kappa, c) \\
981 \mt{proj}(M, \mt{datatype} \; x \; \overline{y} = \overline{dc} \; \overline{s}, \mt{con} \; x) &=& \mt{Type}^{\mt{len}(\overline{y})} \to \mt{Type} \\ 1023 \mt{proj}(M, \mt{datatype} \; x \; \overline{y} = \overline{dc} \; \overline{s}, \mt{con} \; x) &=& \mt{Type}^{\mt{len}(\overline{y})} \to \mt{Type} \\
982 \mt{proj}(M, \mt{datatype} \; x = \mt{datatype} \; M'.z \; \overline{s}, \mt{con} \; x) &=& (\mt{Type}^{\mt{len}(\overline{y})} \to \mt{Type}, M'.z) \textrm{ (where $\Gamma \vdash M' : \mt{sig} \; \overline{s'} \; \mt{end}$} \\ 1024 \mt{proj}(M, \mt{datatype} \; x = \mt{datatype} \; M'.z \; \overline{s}, \mt{con} \; x) &=& (\mt{Type}^{\mt{len}(\overline{y})} \to \mt{Type}, M'.z) \textrm{ (where $\Gamma \vdash M' : \mt{sig} \; \overline{s'} \; \mt{end}$} \\
983 && \textrm{and $\mt{proj}(M', \overline{s'}, \mt{datatype} \; z) = (\overline{y}, \overline{dc})$)} \\ 1025 && \textrm{and $\mt{proj}(M', \overline{s'}, \mt{datatype} \; z) = (\overline{y}, \overline{dc})$)} \\
984 \mt{proj}(M, \mt{class} \; x \; \overline{s}, \mt{con} \; x) &=& \mt{Type} \to \mt{Type} \\ 1026 \mt{proj}(M, \mt{class} \; x :: \kappa \; \overline{s}, \mt{con} \; x) &=& \kappa \to \mt{Type} \\
985 \mt{proj}(M, \mt{class} \; x = c \; \overline{s}, \mt{con} \; x) &=& (\mt{Type} \to \mt{Type}, c) \\ 1027 \mt{proj}(M, \mt{class} \; x :: \kappa = c \; \overline{s}, \mt{con} \; x) &=& (\kappa \to \mt{Type}, c) \\
986 \\ 1028 \\
987 \mt{proj}(M, \mt{datatype} \; x \; \overline{y} = \overline{dc} \; \overline{s}, \mt{datatype} \; x) &=& (\overline{y}, \overline{dc}) \\ 1029 \mt{proj}(M, \mt{datatype} \; x \; \overline{y} = \overline{dc} \; \overline{s}, \mt{datatype} \; x) &=& (\overline{y}, \overline{dc}) \\
988 \mt{proj}(M, \mt{datatype} \; x = \mt{datatype} \; M'.z \; \overline{s}, \mt{con} \; x) &=& \mt{proj}(M', \overline{s'}, \mt{datatype} \; z) \textrm{ (where $\Gamma \vdash M' : \mt{sig} \; \overline{s'} \; \mt{end}$)} \\ 1030 \mt{proj}(M, \mt{datatype} \; x = \mt{datatype} \; M'.z \; \overline{s}, \mt{con} \; x) &=& \mt{proj}(M', \overline{s'}, \mt{datatype} \; z) \textrm{ (where $\Gamma \vdash M' : \mt{sig} \; \overline{s'} \; \mt{end}$)} \\
989 \\ 1031 \\
990 \mt{proj}(M, \mt{val} \; x : \tau \; \overline{s}, \mt{val} \; x) &=& \tau \\ 1032 \mt{proj}(M, \mt{val} \; x : \tau \; \overline{s}, \mt{val} \; x) &=& \tau \\
1006 \mt{proj}(M, \mt{val} \; x : \tau \; \overline{s}, V) &=& \mt{proj}(M, \overline{s}, V) \\ 1048 \mt{proj}(M, \mt{val} \; x : \tau \; \overline{s}, V) &=& \mt{proj}(M, \overline{s}, V) \\
1007 \mt{proj}(M, \mt{structure} \; X : S \; \overline{s}, V) &=& [X \mapsto M.X]\mt{proj}(M, \overline{s}, V) \\ 1049 \mt{proj}(M, \mt{structure} \; X : S \; \overline{s}, V) &=& [X \mapsto M.X]\mt{proj}(M, \overline{s}, V) \\
1008 \mt{proj}(M, \mt{signature} \; X = S \; \overline{s}, V) &=& [X \mapsto M.X]\mt{proj}(M, \overline{s}, V) \\ 1050 \mt{proj}(M, \mt{signature} \; X = S \; \overline{s}, V) &=& [X \mapsto M.X]\mt{proj}(M, \overline{s}, V) \\
1009 \mt{proj}(M, \mt{include} \; S \; \overline{s}, V) &=& \mt{proj}(M, \overline{s'} \; \overline{s}, V) \textrm{ (where $\Gamma \vdash S \equiv \mt{sig} \; \overline{s'} \; \mt{end}$)} \\ 1051 \mt{proj}(M, \mt{include} \; S \; \overline{s}, V) &=& \mt{proj}(M, \overline{s'} \; \overline{s}, V) \textrm{ (where $\Gamma \vdash S \equiv \mt{sig} \; \overline{s'} \; \mt{end}$)} \\
1010 \mt{proj}(M, \mt{constraint} \; c_1 \sim c_2 \; \overline{s}, V) &=& \mt{proj}(M, \overline{s}, V) \\ 1052 \mt{proj}(M, \mt{constraint} \; c_1 \sim c_2 \; \overline{s}, V) &=& \mt{proj}(M, \overline{s}, V) \\
1011 \mt{proj}(M, \mt{class} \; x \; \overline{s}, V) &=& [x \mapsto M.x]\mt{proj}(M, \overline{s}, V) \\ 1053 \mt{proj}(M, \mt{class} \; x :: \kappa \; \overline{s}, V) &=& [x \mapsto M.x]\mt{proj}(M, \overline{s}, V) \\
1012 \mt{proj}(M, \mt{class} \; x = c \; \overline{s}, V) &=& [x \mapsto M.x]\mt{proj}(M, \overline{s}, V) \\ 1054 \mt{proj}(M, \mt{class} \; x :: \kappa = c \; \overline{s}, V) &=& [x \mapsto M.x]\mt{proj}(M, \overline{s}, V) \\
1013 \end{eqnarray*} 1055 \end{eqnarray*}
1014 1056
1015 1057
1016 \section{Type Inference} 1058 \section{Type Inference}
1017 1059