Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / localhost / Closure
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>Closure</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>Closure</h1>
27 </div>
28 <div id="content">
29 <div id="preamble">
30 <div class="sectionbody">
31 <div class="paragraph"><p>A closure is a data structure that is the run-time representation of a
32 function.</p></div>
33 </div>
34 </div>
35 <div class="sect1">
36 <h2 id="_typical_implementation">Typical Implementation</h2>
37 <div class="sectionbody">
38 <div class="paragraph"><p>In a typical implementation, a closure consists of a <em>code pointer</em>
39 (indicating what the function does) and an <em>environment</em> containing
40 the values of the free variables of the function. For example, in the
41 expression</p></div>
42 <div class="listingblock">
43 <div class="content"><div class="highlight"><pre><span class="k">let</span><span class="w"></span>
44 <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>
45 <span class="k">in</span><span class="w"></span>
46 <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>
47 <span class="k">end</span><span class="w"></span>
48 </pre></div></div></div>
49 <div class="paragraph"><p>the closure for <span class="monospaced">fn y =&gt; x + y</span> contains a pointer to a piece of code
50 that knows to take its argument and add the value of <span class="monospaced">x</span> to it, plus
51 the environment recording the value of <span class="monospaced">x</span> as <span class="monospaced">5</span>.</p></div>
52 <div class="paragraph"><p>To call a function, the code pointer is extracted and jumped to,
53 passing in some agreed upon location the environment and the argument.</p></div>
54 </div>
55 </div>
56 <div class="sect1">
57 <h2 id="_mlton_8217_s_implementation">MLton&#8217;s Implementation</h2>
58 <div class="sectionbody">
59 <div class="paragraph"><p>MLton does not implement closures traditionally. Instead, based on
60 whole-program higher-order control-flow analysis, MLton represents a
61 function as an element of a sum type, where the variant indicates
62 which function it is and carries the free variables as arguments. See
63 <a href="ClosureConvert">ClosureConvert</a> and <a href="References#CejtinEtAl00">CejtinEtAl00</a> for details.</p></div>
64 </div>
65 </div>
66 </div>
67 <div id="footnotes"><hr></div>
68 <div id="footer">
69 <div id="footer-text">
70 </div>
71 <div id="footer-badges">
72 </div>
73 </div>
74 </body>
75 </html>