|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- Academia (pandoc HTML5 template) |
| 3 | + designer: soimort |
| 4 | + last updated: 2016-05-07 |
| 5 | + last adapted: 2017-09-25 --> |
| 6 | +<html> |
| 7 | + <head> |
| 8 | + <meta charset="utf-8"> |
| 9 | + <meta name="generator" content="pandoc"> |
| 10 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> |
| 11 | + <meta name="author" content="Mort Yao"> |
| 12 | + <meta name="dcterms.date" content="2018-02-08"> |
| 13 | + <title>Subtyping</title> |
| 14 | + <link rel="canonical" href="https://wiki.soimort.org/plt/subtyping"> |
| 15 | + <style type="text/css">code { white-space: pre; }</style> |
| 16 | + <link rel="stylesheet" href="//cdn.soimort.org/normalize/5.0.0/normalize.min.css"> |
| 17 | + <link rel="stylesheet" href="//cdn.soimort.org/mathsvg/latest/mathsvg.min.css"> |
| 18 | + <link rel="stylesheet" href="//cdn.soimort.org/fonts/latest/Latin-Modern-Roman.css"> |
| 19 | + <link rel="stylesheet" href="//cdn.soimort.org/fonts/latest/Latin-Modern-Mono.css"> |
| 20 | + <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> |
| 21 | + <link rel="stylesheet" href="/__/css/style.css"> |
| 22 | + <link rel="stylesheet" href="/__/css/pygments.css"> |
| 23 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script> |
| 24 | + <!--[if lt IE 9]> |
| 25 | + <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script> |
| 26 | + <![endif]--> |
| 27 | + <script src="//cdn.soimort.org/jk/20160504/jk.min.js"></script> |
| 28 | + <script src="//cdn.soimort.org/mathsvg/latest/mathsvg.min.js"></script> |
| 29 | + <script src="/__/js/jk-minibar.js"></script> |
| 30 | + <link rel="icon" href="/favicon.png"> |
| 31 | + <link rel="apple-touch-icon" href="/favicon.png"> |
| 32 | + </head> |
| 33 | + <body> |
| 34 | +<p hidden> |
| 35 | +<span class="math display">\[\def\textsc#1{\textsf{#1}}\]</span> |
| 36 | +</p> |
| 37 | + <main><article> |
| 38 | + <header> |
| 39 | + <h1 class="title">Subtyping</h1> |
| 40 | + <!-- h3 class="date">2018-02-08</h3 --> |
| 41 | + </header> |
| 42 | + <div id="content"> |
| 43 | +<p><em>Subtyping</em> is a form of type polymorphism, represented as a preorder on types: <span class="math display">\[\tau' \preceq \tau\]</span> we say that <span class="math inline">\(\tau'\)</span> is a <em>subtype</em> of <span class="math inline">\(\tau\)</span>, and that <span class="math inline">\(\tau\)</span> is a <em>supertype</em> of <span class="math inline">\(\tau'\)</span>. (Note that <span class="math inline">\(\preceq\)</span> need not be a partial order, as no antisymmetry is assumed, i.e., it may be the case that both <span class="math inline">\(\tau' \preceq \tau\)</span> and <span class="math inline">\(\tau \preceq \tau'\)</span> but not <span class="math inline">\(\tau' = \tau\)</span>.)</p> |
| 44 | +<p>As a preorder, the subtyping relation satisfies reflexivity: <span class="math display">\[\frac{}{ |
| 45 | +\tau \preceq \tau |
| 46 | +}\qquad\textsc{ST-Refl}\]</span> and transitivity: <span class="math display">\[\frac{ |
| 47 | +\tau' \preceq \tau'' \qquad |
| 48 | +\tau'' \preceq \tau |
| 49 | +}{ |
| 50 | +\tau' \preceq \tau |
| 51 | +}\qquad\textsc{ST-Trans}\]</span></p> |
| 52 | +<p>On function types, the following rule applies: <span class="math display">\[\frac{ |
| 53 | +\tau_1 \preceq \tau_1' \qquad |
| 54 | +\tau_2' \preceq \tau_2 |
| 55 | +}{ |
| 56 | +\tau_1' \to \tau_2' \preceq \tau_1 \to \tau_2 |
| 57 | +}\qquad\textsc{ST-Fun}\]</span> The <span class="math inline">\(\to\)</span> type constructor is said to be <em>contravariant</em> in the argument type and <em>covariant</em> in the return type.</p> |
| 58 | +<p>The typing rule of <em>subsumption</em> applies: <span class="math display">\[\frac{ |
| 59 | +\Gamma \vdash t : \tau' |
| 60 | +\qquad \tau' \preceq \tau |
| 61 | +}{ |
| 62 | +\Gamma \vdash t : \tau |
| 63 | +}\qquad\textsc{T-Sub}\]</span></p> |
| 64 | +<p><strong>Nominal vs. structural subtyping.</strong> <em>Nominal subtyping</em> requires an explicit declaration of the subtype/supertype relation. In contrast, in <em>structural subtyping</em>, the structure of two types determines the subtype/supertype relation.</p> |
| 65 | +<p><strong>Inclusive vs. coercive subtyping.</strong> Given <span class="math inline">\(\tau' \preceq \tau\)</span>. In an <em>inclusive</em> implementation of subtyping, a term <span class="math inline">\(t' : \tau'\)</span> is just the same value (of type <span class="math inline">\(\tau\)</span>) <span class="math inline">\(t' : \tau\)</span>. In a <em>coercive</em> implementation of subtyping, a term <span class="math inline">\(t' : \tau'\)</span> must be converted to another value (of type <span class="math inline">\(\tau\)</span>) <span class="math inline">\(t : \tau\)</span>, using an implicit type conversion function <span class="math inline">\(coerce: \tau' \to \tau\)</span> from subtype to supertype (e.g., from an integer to a floating-point number).</p> |
| 66 | +<p><strong>Mutable references.</strong> Write-only references (i.e., <em>sinks</em>) are contravariant, and read-only references (i.e., <em>sources</em>) are covariant. Mutable references which act as both sources and sinks are invariant.</p> |
| 67 | +<p><strong>Behavioral subtyping in OOP.</strong> In the scope of object-oriented programming where side effects are allowed, the notion of <em>behavioral subtyping</em> and the <em>Liskov substitution principle</em> apply, which are stronger than the above (sub)typing rules. Behavioral subtyping requires that subtypes preserve all invariants guaranteed by the supertypes in some contract; this problem is generally undecidable thus cannot be guaranteed by a type checker.</p> |
| 68 | +<p><strong>Subtyping and FP.</strong></p> |
| 69 | +<p>Conventionally, functional languages like ML and Haskell provide no direct support for subtype polymorphism. Subtyping raises difficulty for type inference; moreover, it is problematic in the presence of mutability. See:</p> |
| 70 | +<ul> |
| 71 | +<li><a href="https://redd.it/423o0c" class="uri">https://redd.it/423o0c</a></li> |
| 72 | +</ul> |
| 73 | +<p>Standard ML’s parametric polymorphism may be used to encode the subtyping hierarchy. See:</p> |
| 74 | +<ul> |
| 75 | +<li><a href="http://mlton.org/ObjectOrientedProgramming" class="uri">http://mlton.org/ObjectOrientedProgramming</a></li> |
| 76 | +<li>Matthew Fluet and Riccardo Pucella, “Phantom Types and Subtyping.” [<a href="https://www.cs.rit.edu/~mtf/research/phantom-subtyping/jfp06/jfp06.pdf">PDF</a>]</li> |
| 77 | +</ul> |
| 78 | +<p>Type inference has been implemented on MLsub, a superset of the core ML language. See:</p> |
| 79 | +<ul> |
| 80 | +<li>Stephen Dolan and Alan Mycroft, “Polymorphism, Subtyping, and Type Inference in MLsub.” [<a href="https://www.cl.cam.ac.uk/~sd601/papers/mlsub-preprint.pdf">PDF</a>] [<a href="https://github.com/stedolan/mlsub">Code</a>]</li> |
| 81 | +</ul> |
| 82 | + </div> |
| 83 | + <footer> |
| 84 | + <!-- TO BE MODIFIED BY NEED --> |
| 85 | + <a title="Keyboard shortcut: q" |
| 86 | + href=".."> |
| 87 | + <i class="fa fa-angle-double-left" aria-hidden="true"></i> |
| 88 | + <code>Parent</code> |
| 89 | + </a> | |
| 90 | + <a class="raw" accesskey="r" |
| 91 | + title="Keyboard shortcut: R" |
| 92 | + href="https://wiki.soimort.org/plt/subtyping/src.md"> |
| 93 | + <i class="fa fa-code" aria-hidden="true"></i> |
| 94 | + <code>Raw</code> |
| 95 | + </a> | |
| 96 | + <a class="history" accesskey="h" |
| 97 | + title="Keyboard shortcut: H" |
| 98 | + href="https://github.com/soimort/wiki/commits/gh-pages/plt/subtyping/src.md"> |
| 99 | + <i class="fa fa-history" aria-hidden="true"></i> |
| 100 | + <code>History</code> |
| 101 | + </a> | |
| 102 | + <a class="edit" accesskey="e" |
| 103 | + title="Keyboard shortcut: E" |
| 104 | + href="https://github.com/soimort/wiki/edit/gh-pages/plt/subtyping/src.md"> |
| 105 | + <i class="fa fa-code-fork" aria-hidden="true"></i> |
| 106 | + <code>Edit</code> |
| 107 | + </a> | |
| 108 | + <a title="Keyboard shortcut: p" |
| 109 | + href="javascript:window.print();"> |
| 110 | + <i class="fa fa-print" aria-hidden="true"></i> |
| 111 | + <code>Print</code> |
| 112 | + </a> | |
| 113 | + <a title="Keyboard shortcut: ." |
| 114 | + href="https://wiki.soimort.org/plt/subtyping"> |
| 115 | + <i class="fa fa-anchor" aria-hidden="true"></i> |
| 116 | + <code>Permalink</code> |
| 117 | + </a> | |
| 118 | + Last updated: <span id="update-time">2018-02-08</span> |
| 119 | + <address class="author">Mort Yao</address> |
| 120 | + </footer> |
| 121 | + </article></main> |
| 122 | + </body> |
| 123 | +</html> |
0 commit comments