<!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>Closure</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>Closure</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>A closure is a data structure that is the run-time representation of a
function.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_typical_implementation">Typical Implementation</h2>
<div class="sectionbody">
<div class="paragraph"><p>In a typical implementation, a closure consists of a <em>code pointer</em>
(indicating what the function does) and an <em>environment</em> containing
the values of the free variables of the function.  For example, in the
expression</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>the closure for <span class="monospaced">fn y =&gt; x + y</span> contains a pointer to a piece of code
that knows to take its argument and add the value of <span class="monospaced">x</span> to it, plus
the environment recording the value of <span class="monospaced">x</span> as <span class="monospaced">5</span>.</p></div>
<div class="paragraph"><p>To call a function, the code pointer is extracted and jumped to,
passing in some agreed upon location the environment and the argument.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_mlton_8217_s_implementation">MLton&#8217;s Implementation</h2>
<div class="sectionbody">
<div class="paragraph"><p>MLton does not implement closures traditionally.  Instead, based on
whole-program higher-order control-flow analysis, MLton represents a
function as an element of a sum type, where the variant indicates
which function it is and carries the free variables as arguments.  See
<a href="ClosureConvert">ClosureConvert</a> and <a href="References#CejtinEtAl00">CejtinEtAl00</a> for details.</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>
