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