<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>Utilities</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 20201002</a>
</div>
</div>
<div id="header">
<h1>Utilities</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>This page is a collection of basic utilities used in the examples on
various pages.  See</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="InfixingOperators">InfixingOperators</a>, and
</p>
</li>
<li>
<p>
<a href="ProductType">ProductType</a>
</p>
</li>
</ul></div>
<div class="paragraph"><p>for longer discussions on some of these utilities.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="cm">(* Operator precedence table *)</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">8</span><span class="w">  </span><span class="n">*</span><span class="w"> </span><span class="n">/</span><span class="w"> </span><span class="n">div</span><span class="w"> </span><span class="n">mod</span><span class="w">        </span><span class="cm">(* +1 from Basis Library *)</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">7</span><span class="w">  </span><span class="n">+</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="n">^</span><span class="w">              </span><span class="cm">(* +1 from Basis Library *)</span><span class="w"></span>
<span class="k">infixr</span><span class="w">  </span><span class="mi">6</span><span class="w">  </span><span class="n">::</span><span class="w"> </span><span class="n">@</span><span class="w">               </span><span class="cm">(* +1 from Basis Library *)</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">5</span><span class="w">  </span><span class="p">=</span><span class="w"> </span><span class="n">&lt;&gt;</span><span class="w"> </span><span class="n">&gt;</span><span class="w"> </span><span class="n">&gt;=</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">&lt;=</span><span class="w">     </span><span class="cm">(* +1 from Basis Library *)</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">4</span><span class="w">  </span><span class="n">&lt;\</span><span class="w"> </span><span class="n">\&gt;</span><span class="w"></span>
<span class="k">infixr</span><span class="w">  </span><span class="mi">4</span><span class="w">  </span><span class="n">&lt;/</span><span class="w"> </span><span class="n">/&gt;</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">3</span><span class="w">  </span><span class="n">o</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">2</span><span class="w">  </span><span class="n">&gt;|</span><span class="w"></span>
<span class="k">infixr</span><span class="w">  </span><span class="mi">2</span><span class="w">  </span><span class="n">|&lt;</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">1</span><span class="w">  </span><span class="n">:=</span><span class="w">                 </span><span class="cm">(* -2 from Basis Library *)</span><span class="w"></span>
<span class="k">infix</span><span class="w">   </span><span class="mi">0</span><span class="w">  </span><span class="n">before</span><span class="w"> </span><span class="n">&amp;</span><span class="w"></span>

<span class="cm">(* Some basic combinators *)</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">const</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">cross</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="n">g</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">curry</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">fail</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">e</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">x</span><span class="w"></span>

<span class="cm">(* Product type *)</span><span class="w"></span>
<span class="k">datatype</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">product</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;a</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="cm">(* Sum type *)</span><span class="w"></span>
<span class="k">datatype</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">sum</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">INL</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">INR</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"></span>

<span class="cm">(* Some type shorthands *)</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">uop</span><span class="w"> </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="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">fix</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">uop</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="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">thunk</span><span class="w"> </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">&#39;a</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">effect</span><span class="w"> </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">unit</span><span class="w"></span>
<span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">emb</span><span class="w"> </span><span class="p">=</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="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;b</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"></span>

<span class="cm">(* Infixing, sectioning, and application operators *)</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">&lt;\</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</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">f</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</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">\&gt;</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">y</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">/&gt;</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">&lt;/</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">f</span><span class="w"> </span><span class="n">x</span><span class="w"></span>

<span class="cm">(* Piping operators *)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="k">op</span><span class="n">&gt;|</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">op</span><span class="n">&lt;/</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="k">op</span><span class="n">|&lt;</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">op</span><span class="n">\&gt;</span><span class="w"></span>
</pre></div></div></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>
