<!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>NumericLiteral</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>NumericLiteral</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Numeric literals in <a href="StandardML">Standard ML</a> can be written in either
decimal or hexadecimal notation.  Sometimes it can be convenient to
write numbers down in other bases.  Fortunately, using <a href="Fold">Fold</a>, it is
possible to define a concise syntax for numeric literals that allows
one to write numeric constants in any base and of various types
(<span class="monospaced">int</span>, <span class="monospaced">IntInf.int</span>, <span class="monospaced">word</span>, and more).</p></div>
<div class="paragraph"><p>We will define constants <span class="monospaced">I</span>, <span class="monospaced">II</span>, <span class="monospaced">W</span>, and <span class="monospaced">`</span> so
that, for example,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">I</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="n">`</span><span class="mi">1</span><span class="n">`</span><span class="mi">2</span><span class="n">`</span><span class="mi">3</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>denotes <span class="monospaced">123:int</span> in base 10, while</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">II</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="n">`</span><span class="mi">2</span><span class="n">`</span><span class="mi">3</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>denotes <span class="monospaced">19:IntInf.int</span> in base 8, and</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">W</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="n">`</span><span class="mi">1</span><span class="n">`</span><span class="mi">1</span><span class="n">`</span><span class="mi">0</span><span class="n">`</span><span class="mi">1</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>denotes <span class="monospaced">0w13: word</span>.</p></div>
<div class="paragraph"><p>Here is the code.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">Num</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">make</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">*</span><span class="p">,</span><span class="w"> </span><span class="k">op</span><span class="w"> </span><span class="n">+</span><span class="p">,</span><span class="w"> </span><span class="n">i2x</span><span class="p">)</span><span class="w"> </span><span class="n">iBase</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">xBase</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">i2x</span><span class="w"> </span><span class="n">iBase</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">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"></span>
<span class="w">                </span><span class="p">((</span><span class="n">i2x</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"></span>
<span class="w">                  </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                     </span><span class="k">if</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">&lt;=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">andalso</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">iBase</span><span class="w"> </span><span class="k">then</span><span class="w"></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">xBase</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">i2x</span><span class="w"> </span><span class="n">i</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">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"></span>
<span class="w">                                       </span><span class="p">[</span><span class="s">&quot;Num: &quot;</span><span class="p">,</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="n">i</span><span class="p">,</span><span class="w"></span>
<span class="w">                                        </span><span class="s">&quot; is not a valid\</span>
<span class="s">                                        \ digit in base &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="n">iBase</span><span class="p">])),</span><span class="w"></span>
<span class="w">                 </span><span class="n">fst</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">fun</span><span class="w"> </span><span class="n">I</span><span class="w">  </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">make</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">*</span><span class="p">,</span><span class="w"> </span><span class="k">op</span><span class="w"> </span><span class="n">+</span><span class="p">,</span><span class="w"> </span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">II</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">make</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">*</span><span class="p">,</span><span class="w"> </span><span class="k">op</span><span class="w"> </span><span class="n">+</span><span class="p">,</span><span class="w"> </span><span class="n">IntInf</span><span class="p">.</span><span class="n">fromInt</span><span class="p">)</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">W</span><span class="w">  </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">make</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">*</span><span class="p">,</span><span class="w"> </span><span class="k">op</span><span class="w"> </span><span class="n">+</span><span class="p">,</span><span class="w"> </span><span class="n">Word</span><span class="p">.</span><span class="n">fromInt</span><span class="p">)</span><span class="w"> </span><span class="n">?</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">`</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">i</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">step</span><span class="p">))</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                               </span><span class="p">(</span><span class="n">step</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">),</span><span class="w"> </span><span class="n">step</span><span class="p">))</span><span class="w"> </span><span class="n">?</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="mi">10</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">11</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="mi">12</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">13</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">14</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">15</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>where</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">fun</span><span class="w"> </span><span class="n">fst</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</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>
</pre></div></div></div>
<div class="paragraph"><p>The idea is for the fold to start with zero and to construct the
result one digit at a time, with each stepper multiplying the previous
result by the base and adding the next digit.  The code is abstracted
in two different ways for extra generality.  First, the <span class="monospaced">make</span>
function abstracts over the various primitive operations (addition,
multiplication, etc) that are needed to construct a number.  This
allows the same code to be shared for constants <span class="monospaced">I</span>, <span class="monospaced">II</span>, <span class="monospaced">W</span> used to
write down the various numeric types.  It also allows users to add new
constants for additional numeric types, by supplying the necessary
arguments to make.</p></div>
<div class="paragraph"><p>Second, the step function, <span class="monospaced">&grave;</span>, is abstracted over the actual
construction operation, which is created by make, and passed along the
fold.  This allows the same constant, <span class="monospaced">&grave;</span>, to be used for all
numeric types.  The alternative approach, having a different step
function for each numeric type, would be more painful to use.</p></div>
<div class="paragraph"><p>On the surface, it appears that the code checks the digits dynamically
to ensure they are valid for the base.  However, MLton will simplify
everything away at compile time, leaving just the final numeric
constant.</p></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>
