<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>ScopeInference</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>ScopeInference</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Scope inference is an analysis/rewrite pass for the <a href="AST">AST</a>
<a href="IntermediateLanguage">IntermediateLanguage</a>, invoked from <a href="Elaborate">Elaborate</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_description">Description</h2>
<div class="sectionbody">
<div class="paragraph"><p>This pass adds free type variables to the <span class="monospaced">val</span> or <span class="monospaced">fun</span>
declaration where they are implicitly scoped.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="https://github.com/MLton/mlton/blob/master/mlton/elaborate/scope.sig"><span class="monospaced">scope.sig</span></a>
<a href="https://github.com/MLton/mlton/blob/master/mlton/elaborate/scope.fun"><span class="monospaced">scope.fun</span></a></p></div>
</div>
</div>
<div class="sect1">
<h2 id="_details_and_notes">Details and Notes</h2>
<div class="sectionbody">
<div class="paragraph"><p>Scope inference determines for each type variable, the declaration
where it is bound.  Scope inference is a direct implementation of the
specification given in section 4.6 of the
<a href="DefinitionOfStandardML"> Definition</a>.  Recall that a free occurrence
of a type variable <span class="monospaced">'a</span> in a declaration <span class="monospaced">d</span> is <em>unguarded</em>
in <span class="monospaced">d</span> if <span class="monospaced">'a</span> is not part of a smaller declaration.  A type
variable <span class="monospaced">'a</span> is implicitly scoped at <span class="monospaced">d</span> if <span class="monospaced">'a</span> is
unguarded in <span class="monospaced">d</span> and <span class="monospaced">'a</span> does not occur unguarded in any
declaration containing <span class="monospaced">d</span>.</p></div>
<div class="paragraph"><p>The first pass of scope inference walks down the tree and renames all
explicitly bound type variables in order to avoid name collisions.  It
then walks up the tree and adds to each declaration the set of
unguarded type variables occurring in that declaration.  At this
point, if declaration <span class="monospaced">d</span> contains an unguarded type variable
<span class="monospaced">'a</span> and the immediately containing declaration does not contain
<span class="monospaced">'a</span>, then <span class="monospaced">'a</span> is implicitly scoped at <span class="monospaced">d</span>.  The final
pass walks down the tree leaving a <span class="monospaced">'a</span> at the a declaration where
it is scoped and removing it from all enclosed declarations.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
