Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / localhost / Closure
CommitLineData
7f918cf1
CE
1<!DOCTYPE html>\r
2<html lang="en">\r
3<head>\r
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\r
5<meta name="generator" content="AsciiDoc 8.6.9">\r
6<title>Closure</title>\r
7<link rel="stylesheet" href="./asciidoc.css" type="text/css">\r
8<link rel="stylesheet" href="./pygments.css" type="text/css">\r
9\r
10\r
11<script type="text/javascript" src="./asciidoc.js"></script>\r
12<script type="text/javascript">\r
13/*<![CDATA[*/\r
14asciidoc.install();\r
15/*]]>*/\r
16</script>\r
17<link rel="stylesheet" href="./mlton.css" type="text/css">\r
18</head>\r
19<body class="article">\r
20<div id="banner">\r
21<div id="banner-home">\r
22<a href="./Home">MLton 20180207</a>\r
23</div>\r
24</div>\r
25<div id="header">\r
26<h1>Closure</h1>\r
27</div>\r
28<div id="content">\r
29<div id="preamble">\r
30<div class="sectionbody">\r
31<div class="paragraph"><p>A closure is a data structure that is the run-time representation of a\r
32function.</p></div>\r
33</div>\r
34</div>\r
35<div class="sect1">\r
36<h2 id="_typical_implementation">Typical Implementation</h2>\r
37<div class="sectionbody">\r
38<div class="paragraph"><p>In a typical implementation, a closure consists of a <em>code pointer</em>\r
39(indicating what the function does) and an <em>environment</em> containing\r
40the values of the free variables of the function. For example, in the\r
41expression</p></div>\r
42<div class="listingblock">\r
43<div class="content"><div class="highlight"><pre><span class="k">let</span><span class="w"></span>\r
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>\r
45<span class="k">in</span><span class="w"></span>\r
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>\r
47<span class="k">end</span><span class="w"></span>\r
48</pre></div></div></div>\r
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\r
50that knows to take its argument and add the value of <span class="monospaced">x</span> to it, plus\r
51the environment recording the value of <span class="monospaced">x</span> as <span class="monospaced">5</span>.</p></div>\r
52<div class="paragraph"><p>To call a function, the code pointer is extracted and jumped to,\r
53passing in some agreed upon location the environment and the argument.</p></div>\r
54</div>\r
55</div>\r
56<div class="sect1">\r
57<h2 id="_mlton_8217_s_implementation">MLton&#8217;s Implementation</h2>\r
58<div class="sectionbody">\r
59<div class="paragraph"><p>MLton does not implement closures traditionally. Instead, based on\r
60whole-program higher-order control-flow analysis, MLton represents a\r
61function as an element of a sum type, where the variant indicates\r
62which function it is and carries the free variables as arguments. See\r
63<a href="ClosureConvert">ClosureConvert</a> and <a href="References#CejtinEtAl00">CejtinEtAl00</a> for details.</p></div>\r
64</div>\r
65</div>\r
66</div>\r
67<div id="footnotes"><hr></div>\r
68<div id="footer">\r
69<div id="footer-text">\r
70</div>\r
71<div id="footer-badges">\r
72</div>\r
73</div>\r
74</body>\r
75</html>\r