| 1 | <!DOCTYPE html>\r |
| 2 | <html lang="en">\r |
| 3 | <head>\r |
| 4 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\r |
| 5 | <meta name="generator" content="AsciiDoc 8.6.9">\r |
| 6 | <title>ScopeInference</title>\r |
| 7 | <link rel="stylesheet" href="./asciidoc.css" type="text/css">\r |
| 8 | <link rel="stylesheet" href="./pygments.css" type="text/css">\r |
| 9 | \r |
| 10 | \r |
| 11 | <script type="text/javascript" src="./asciidoc.js"></script>\r |
| 12 | <script type="text/javascript">\r |
| 13 | /*<![CDATA[*/\r |
| 14 | asciidoc.install();\r |
| 15 | /*]]>*/\r |
| 16 | </script>\r |
| 17 | <link rel="stylesheet" href="./mlton.css" type="text/css">\r |
| 18 | </head>\r |
| 19 | <body class="article">\r |
| 20 | <div id="banner">\r |
| 21 | <div id="banner-home">\r |
| 22 | <a href="./Home">MLton 20180207</a>\r |
| 23 | </div>\r |
| 24 | </div>\r |
| 25 | <div id="header">\r |
| 26 | <h1>ScopeInference</h1>\r |
| 27 | </div>\r |
| 28 | <div id="content">\r |
| 29 | <div id="preamble">\r |
| 30 | <div class="sectionbody">\r |
| 31 | <div class="paragraph"><p>Scope inference is an analysis/rewrite pass for the <a href="AST">AST</a>\r |
| 32 | <a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="Elaborate">Elaborate</a>.</p></div>\r |
| 33 | </div>\r |
| 34 | </div>\r |
| 35 | <div class="sect1">\r |
| 36 | <h2 id="_description">Description</h2>\r |
| 37 | <div class="sectionbody">\r |
| 38 | <div class="paragraph"><p>This pass adds free type variables to the <span class="monospaced">val</span> or <span class="monospaced">fun</span>\r |
| 39 | declaration where they are implicitly scoped.</p></div>\r |
| 40 | </div>\r |
| 41 | </div>\r |
| 42 | <div class="sect1">\r |
| 43 | <h2 id="_implementation">Implementation</h2>\r |
| 44 | <div class="sectionbody">\r |
| 45 | <div class="paragraph"><p><a href="https://github.com/MLton/mlton/blob/master/mlton/elaborate/scope.sig"><span class="monospaced">scope.sig</span></a>\r |
| 46 | <a href="https://github.com/MLton/mlton/blob/master/mlton/elaborate/scope.fun"><span class="monospaced">scope.fun</span></a></p></div>\r |
| 47 | </div>\r |
| 48 | </div>\r |
| 49 | <div class="sect1">\r |
| 50 | <h2 id="_details_and_notes">Details and Notes</h2>\r |
| 51 | <div class="sectionbody">\r |
| 52 | <div class="paragraph"><p>Scope inference determines for each type variable, the declaration\r |
| 53 | where it is bound. Scope inference is a direct implementation of the\r |
| 54 | specification given in section 4.6 of the\r |
| 55 | <a href="DefinitionOfStandardML"> Definition</a>. Recall that a free occurrence\r |
| 56 | of a type variable <span class="monospaced">'a</span> in a declaration <span class="monospaced">d</span> is <em>unguarded</em>\r |
| 57 | in <span class="monospaced">d</span> if <span class="monospaced">'a</span> is not part of a smaller declaration. A type\r |
| 58 | variable <span class="monospaced">'a</span> is implicitly scoped at <span class="monospaced">d</span> if <span class="monospaced">'a</span> is\r |
| 59 | unguarded in <span class="monospaced">d</span> and <span class="monospaced">'a</span> does not occur unguarded in any\r |
| 60 | declaration containing <span class="monospaced">d</span>.</p></div>\r |
| 61 | <div class="paragraph"><p>The first pass of scope inference walks down the tree and renames all\r |
| 62 | explicitly bound type variables in order to avoid name collisions. It\r |
| 63 | then walks up the tree and adds to each declaration the set of\r |
| 64 | unguarded type variables occurring in that declaration. At this\r |
| 65 | point, if declaration <span class="monospaced">d</span> contains an unguarded type variable\r |
| 66 | <span class="monospaced">'a</span> and the immediately containing declaration does not contain\r |
| 67 | <span class="monospaced">'a</span>, then <span class="monospaced">'a</span> is implicitly scoped at <span class="monospaced">d</span>. The final\r |
| 68 | pass walks down the tree leaving a <span class="monospaced">'a</span> at the a declaration where\r |
| 69 | it is scoped and removing it from all enclosed declarations.</p></div>\r |
| 70 | </div>\r |
| 71 | </div>\r |
| 72 | </div>\r |
| 73 | <div id="footnotes"><hr></div>\r |
| 74 | <div id="footer">\r |
| 75 | <div id="footer-text">\r |
| 76 | </div>\r |
| 77 | <div id="footer-badges">\r |
| 78 | </div>\r |
| 79 | </div>\r |
| 80 | </body>\r |
| 81 | </html>\r |