<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.23">
<title>Closure</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./rouge-github.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230
</a>
</h2>
</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">
<pre class="rouge highlight"><code data-lang="sml"><span class="kr">let</span>
   <span class="kr">val</span> <span class="nv">x</span> <span class="p">=</span> <span class="mi">5</span>
<span class="kr">in</span>
   <span class="kr">fn</span> <span class="n">y</span> <span class="p">=&gt;</span> <span class="n">x</span> <span class="n">+</span> <span class="n">y</span>
<span class="kr">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>the closure for <code>fn y =&gt; x + y</code> contains a pointer to a piece of code
that knows to take its argument and add the value of <code>x</code> to it, plus
the environment recording the value of <code>x</code> as <code>5</code>.</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="_mltons_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="mlton-footer">
<div id="mlton-footer-text">
<div>
Last updated Thu Oct 21 15:53:06 2021 -0400 by Matthew Fluet.
<a href="https://github.com/MLton/mlton/commits/master/doc/guide/src/Closure.adoc">Log</a>
<a href="https://github.com/MLton/mlton/edit/master/doc/guide/src/Closure.adoc">Edit</a>
</div>
</div>
</body>
</html>