<!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>MLtonFinalizable</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>MLtonFinalizable</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">signature</span><span class="w"> </span><span class="n">MLTON_FINALIZABLE</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">addFinalizer</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">finalizeBefore</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">new</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">touch</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">withValue</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>A <em>finalizable</em> value is a container to which finalizers can be
attached.  A container holds a value, which is reachable as long as
the container itself is reachable.  A <em>finalizer</em> is a function that
runs at some point after garbage collection determines that the
container to which it is attached has become
<a href="Reachability">unreachable</a>.  A finalizer is treated like a signal
handler, in that it runs asynchronously in a separate thread, with
signals blocked, and will not interrupt a critical section (see
<a href="MLtonThread">MLtonThread</a>).</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">addFinalizer (v, f)</span>
</p>
<div class="paragraph"><p>adds <span class="monospaced">f</span> as a finalizer to <span class="monospaced">v</span>.  This means that sometime after the
last call to <span class="monospaced">withValue</span> on <span class="monospaced">v</span> completes and <span class="monospaced">v</span> becomes unreachable,
<span class="monospaced">f</span> will be called with the value of <span class="monospaced">v</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">finalizeBefore (v1, v2)</span>
</p>
<div class="paragraph"><p>ensures that <span class="monospaced">v1</span> will be finalized before <span class="monospaced">v2</span>.  A cycle of values
<span class="monospaced">v</span> = <span class="monospaced">v1</span>, &#8230;, <span class="monospaced">vn</span> = <span class="monospaced">v</span> with <span class="monospaced">finalizeBefore (vi, vi+1)</span> will
result in none of the <span class="monospaced">vi</span> being finalized.</p></div>
</li>
<li>
<p>
<span class="monospaced">new x</span>
</p>
<div class="paragraph"><p>creates a new finalizable value, <span class="monospaced">v</span>, with value <span class="monospaced">x</span>.  The finalizers
of <span class="monospaced">v</span> will run sometime after the last call to <span class="monospaced">withValue</span> on <span class="monospaced">v</span>
when the garbage collector determines that <span class="monospaced">v</span> is unreachable.</p></div>
</li>
<li>
<p>
<span class="monospaced">touch v</span>
</p>
<div class="paragraph"><p>ensures that <span class="monospaced">v</span>'s finalizers will not run before the call to <span class="monospaced">touch</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">withValue (v, f)</span>
</p>
<div class="paragraph"><p>returns the result of applying <span class="monospaced">f</span> to the value of <span class="monospaced">v</span> and ensures
that <span class="monospaced">v</span>'s finalizers will not run before <span class="monospaced">f</span> completes.  The call to
<span class="monospaced">f</span> is a nontail call.</p></div>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>Suppose that <span class="monospaced">finalizable.sml</span> contains the following:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">signature</span><span class="w"> </span><span class="n">CLIST</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">cons</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">sing</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">sum</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">functor</span><span class="w"> </span><span class="n">CList</span><span class="w"> </span><span class="p">(</span><span class="k">structure</span><span class="w"> </span><span class="n">F</span><span class="p">:</span><span class="w"> </span><span class="n">MLTON_FINALIZABLE</span><span class="w"></span>
<span class="w">               </span><span class="k">structure</span><span class="w"> </span><span class="n">P</span><span class="p">:</span><span class="w"> </span><span class="n">MLTON_POINTER</span><span class="w"></span>
<span class="w">               </span><span class="k">structure</span><span class="w"> </span><span class="n">Prim</span><span class="p">:</span><span class="w"></span>
<span class="w">                  </span><span class="k">sig</span><span class="w"></span>
<span class="w">                     </span><span class="k">val</span><span class="w"> </span><span class="n">cons</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">                     </span><span class="k">val</span><span class="w"> </span><span class="n">free</span><span class="p">:</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">                     </span><span class="k">val</span><span class="w"> </span><span class="n">sing</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
<span class="w">                     </span><span class="k">val</span><span class="w"> </span><span class="n">sum</span><span class="p">:</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="w"></span>
<span class="w">                  </span><span class="k">end</span><span class="p">):</span><span class="w"> </span><span class="n">CLIST</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">type</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">t</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">F</span><span class="p">.</span><span class="n">withValue</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">w&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">          </span><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">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="n">Prim</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">w&#39;</span><span class="p">))</span><span class="w"></span>
<span class="w">             </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">addFinalizer</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">Prim</span><span class="p">.</span><span class="n">free</span><span class="p">)</span><span class="w"></span>
<span class="w">             </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">finalizeBefore</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="k">in</span><span class="w"></span>
<span class="w">             </span><span class="n">c</span><span class="w"></span>
<span class="w">          </span><span class="k">end</span><span class="p">)</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">sing</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><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">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">new</span><span class="w"> </span><span class="p">(</span><span class="n">Prim</span><span class="p">.</span><span class="n">sing</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">addFinalizer</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">Prim</span><span class="p">.</span><span class="n">free</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="n">c</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">sum</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="p">.</span><span class="n">withValue</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">Prim</span><span class="p">.</span><span class="n">sum</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">functor</span><span class="w"> </span><span class="n">Test</span><span class="w"> </span><span class="p">(</span><span class="k">structure</span><span class="w"> </span><span class="n">CList</span><span class="p">:</span><span class="w"> </span><span class="n">CLIST</span><span class="w"></span>
<span class="w">              </span><span class="k">structure</span><span class="w"> </span><span class="n">MLton</span><span class="p">:</span><span class="w"> </span><span class="k">sig</span><span class="w"></span>
<span class="w">                                  </span><span class="k">structure</span><span class="w"> </span><span class="n">GC</span><span class="p">:</span><span class="w"></span>
<span class="w">                                     </span><span class="k">sig</span><span class="w"></span>
<span class="w">                                        </span><span class="k">val</span><span class="w"> </span><span class="n">collect</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">                                     </span><span class="k">end</span><span class="w"></span>
<span class="w">                               </span><span class="k">end</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="k">if</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="w">            </span><span class="k">then</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">         </span><span class="k">else</span><span class="w"></span>
<span class="w">            </span><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">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">tabulate</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"></span>
<span class="w">               </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">sub</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">sub</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">in</span><span class="w"></span>
<span class="w">               </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">sing</span><span class="w"> </span><span class="mi">2</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="p">.</span><span class="n">cons</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">l</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">GC</span><span class="p">.</span><span class="n">collect</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mi">100</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="s">&quot;listSum(l) = &quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">                             </span><span class="n">Int</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="p">(</span><span class="n">CList</span><span class="p">.</span><span class="n">sum</span><span class="w"> </span><span class="n">l</span><span class="p">),</span><span class="w"></span>
<span class="w">                             </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">])</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">GC</span><span class="p">.</span><span class="n">collect</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mi">100</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">CList</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">CList</span><span class="w"> </span><span class="p">(</span><span class="k">structure</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Finalizable</span><span class="w"></span>
<span class="w">          </span><span class="k">structure</span><span class="w"> </span><span class="n">P</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Pointer</span><span class="w"></span>
<span class="w">          </span><span class="k">structure</span><span class="w"> </span><span class="n">Prim</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">             </span><span class="k">struct</span><span class="w"></span>
<span class="w">                </span><span class="k">val</span><span class="w"> </span><span class="n">cons</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;listCons&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="k">val</span><span class="w"> </span><span class="n">free</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;listFree&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="k">val</span><span class="w"> </span><span class="n">sing</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;listSing&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="k">val</span><span class="w"> </span><span class="n">sum</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_</span><span class="n">import</span><span class="w"> </span><span class="s">&quot;listSum&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">P</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="p">;</span><span class="w"></span>
<span class="w">             </span><span class="k">end</span><span class="p">)</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">S</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Test</span><span class="w"> </span><span class="p">(</span><span class="k">structure</span><span class="w"> </span><span class="n">CList</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CList</span><span class="w"></span>
<span class="w">                    </span><span class="k">structure</span><span class="w"> </span><span class="n">MLton</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Suppose that <span class="monospaced">cons.c</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="cp">#include &lt;stdio.h&gt;</span>

<span class="k">typedef</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">uint</span><span class="p">;</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">Cons</span> <span class="p">{</span>
        <span class="k">struct</span> <span class="n">Cons</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
        <span class="kt">int</span> <span class="n">value</span><span class="p">;</span>
<span class="p">}</span> <span class="o">*</span><span class="n">Cons</span><span class="p">;</span>

<span class="n">Cons</span> <span class="nf">listCons</span> <span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="n">Cons</span> <span class="n">c</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">Cons</span> <span class="n">res</span><span class="p">;</span>

        <span class="n">res</span> <span class="o">=</span> <span class="p">(</span><span class="n">Cons</span><span class="p">)</span> <span class="n">malloc</span> <span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="o">*</span><span class="n">res</span><span class="p">));</span>
        <span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;0x%08x = listCons (%d)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">uint</span><span class="p">)</span><span class="n">res</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
        <span class="n">res</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">c</span><span class="p">;</span>
        <span class="n">res</span><span class="o">-&gt;</span><span class="n">value</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span>
        <span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="p">}</span>

<span class="n">Cons</span> <span class="nf">listSing</span> <span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">Cons</span> <span class="n">res</span><span class="p">;</span>

        <span class="n">res</span> <span class="o">=</span> <span class="p">(</span><span class="n">Cons</span><span class="p">)</span> <span class="n">malloc</span> <span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="o">*</span><span class="n">res</span><span class="p">));</span>
        <span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;0x%08x = listSing (%d)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">uint</span><span class="p">)</span><span class="n">res</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
        <span class="n">res</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
        <span class="n">res</span><span class="o">-&gt;</span><span class="n">value</span> <span class="o">=</span> <span class="n">n</span><span class="p">;</span>
        <span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">void</span> <span class="nf">listFree</span> <span class="p">(</span><span class="n">Cons</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;listFree (0x%08x)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">uint</span><span class="p">)</span><span class="n">p</span><span class="p">);</span>
        <span class="n">free</span> <span class="p">(</span><span class="n">p</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">listSum</span> <span class="p">(</span><span class="n">Cons</span> <span class="n">c</span><span class="p">)</span> <span class="p">{</span>
        <span class="kt">int</span> <span class="n">res</span><span class="p">;</span>

        <span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&quot;listSum</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
        <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
        <span class="k">for</span> <span class="p">(;</span> <span class="n">c</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">;</span> <span class="n">c</span> <span class="o">=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">)</span>
                <span class="n">res</span> <span class="o">+=</span> <span class="n">c</span><span class="o">-&gt;</span><span class="n">value</span><span class="p">;</span>
        <span class="k">return</span> <span class="n">res</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></div></div>
<div class="paragraph"><p>We can compile these to create an executable with</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -default-ann 'allowFFI true' finalizable.sml cons.c</pre>
</div></div>
<div class="paragraph"><p>Running this executable will create output like the following.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% finalizable
0x08072890 = listSing (2)
0x080728a0 = listCons (2)
0x080728b0 = listCons (2)
0x080728c0 = listCons (2)
0x080728d0 = listCons (2)
0x080728e0 = listCons (2)
0x080728f0 = listCons (2)
listSum
listSum(l) = 14
listFree (0x080728f0)
listFree (0x080728e0)
listFree (0x080728d0)
listFree (0x080728c0)
listFree (0x080728b0)
listFree (0x080728a0)
listFree (0x08072890)</pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_synchronous_finalizers">Synchronous Finalizers</h2>
<div class="sectionbody">
<div class="paragraph"><p>Finalizers in MLton are asynchronous.  That is, they run at an
unspecified time, interrupting the user program.  It is also possible,
and sometimes useful, to have synchronous finalizers, where the user
program explicitly decides when to run enabled finalizers.  We have
considered this in MLton, and it seems possible, but there are some
unresolved design issues.  See the thread at</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="http://www.mlton.org/pipermail/mlton/2004-September/016570.html">http://www.mlton.org/pipermail/mlton/2004-September/016570.html</a>
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="References#Boehm03">Boehm03</a>
</p>
</li>
</ul></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>
