Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > d07d7ab417d79053e7e0155c99e1a1c8 > files > 2354

mlton-20100608-3.fc15.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>NumericLiteral - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      NumericLiteral
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
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 (<tt>int</tt>, <tt>IntInf.int</tt>, <tt>word</tt>, and more). <p>
We will define constants <tt>I</tt>, <tt>II</tt>, <tt>W</tt>, and <tt>`</tt> so that, for example, 
</p>

<pre class=code>
I <B><FONT COLOR="#5F9EA0">10</FONT></B> `<B><FONT COLOR="#5F9EA0">1</FONT></B>`<B><FONT COLOR="#5F9EA0">2</FONT></B>`<B><FONT COLOR="#5F9EA0">3</FONT></B> $
</PRE>
<p>
 
</p>
<p>
denotes <tt>123:int</tt> in base 10, while 
</p>

<pre class=code>
II <B><FONT COLOR="#5F9EA0">8</FONT></B> `<B><FONT COLOR="#5F9EA0">2</FONT></B>`<B><FONT COLOR="#5F9EA0">3</FONT></B> $
</PRE>
<p>
 
</p>
<p>
denotes <tt>19:IntInf.int</tt> in base 8, and 
</p>

<pre class=code>
W <B><FONT COLOR="#5F9EA0">2</FONT></B> `<B><FONT COLOR="#5F9EA0">1</FONT></B>`<B><FONT COLOR="#5F9EA0">1</FONT></B>`<B><FONT COLOR="#5F9EA0">0</FONT></B>`<B><FONT COLOR="#5F9EA0">1</FONT></B> $
</PRE>
<p>
 
</p>
<p>
denotes <tt>0w13:&nbsp;word</tt>. 
</p>
<p>
Here is the code. 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> Num =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> make (<B><FONT COLOR="#A020F0">op</FONT></B> *, <B><FONT COLOR="#A020F0">op</FONT></B> +, i2x) iBase =
          <B><FONT COLOR="#A020F0">let</FONT></B>
             <B><FONT COLOR="#A020F0">val</FONT></B> xBase = i2x iBase
          <B><FONT COLOR="#A020F0">in</FONT></B>
             Fold.fold
                ((i2x <B><FONT COLOR="#5F9EA0">0</FONT></B>,
                  <B><FONT COLOR="#A020F0">fn</FONT></B> (i, x) =&gt;
                     <B><FONT COLOR="#A020F0">if</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> &lt;= i <B><FONT COLOR="#A020F0">andalso</FONT></B> i &lt; iBase <B><FONT COLOR="#A020F0">then</FONT></B>
                        x * xBase + i2x i
                     <B><FONT COLOR="#A020F0">else</FONT></B>
                        <B><FONT COLOR="#A020F0">raise</FONT></B> Fail (concat
                                       [<B><FONT COLOR="#BC8F8F">&quot;Num: &quot;</FONT></B>, Int.toString i,
                                        <B><FONT COLOR="#BC8F8F">&quot; is not a valid\
                                        \ digit in base &quot;</FONT></B>,
                                        Int.toString iBase])),
                 fst)
          <B><FONT COLOR="#A020F0">end</FONT></B>

      <B><FONT COLOR="#A020F0">fun</FONT></B> I  ? = make (<B><FONT COLOR="#A020F0">op</FONT></B> *, <B><FONT COLOR="#A020F0">op</FONT></B> +, id) ?
      <B><FONT COLOR="#A020F0">fun</FONT></B> II ? = make (<B><FONT COLOR="#A020F0">op</FONT></B> *, <B><FONT COLOR="#A020F0">op</FONT></B> +, IntInf.fromInt) ?
      <B><FONT COLOR="#A020F0">fun</FONT></B> W  ? = make (<B><FONT COLOR="#A020F0">op</FONT></B> *, <B><FONT COLOR="#A020F0">op</FONT></B> +, Word.fromInt) ?

      <B><FONT COLOR="#A020F0">fun</FONT></B> ` ? = Fold.step1 (<B><FONT COLOR="#A020F0">fn</FONT></B> (i, (x, step)) =&gt;
                               (step (i, x), step)) ?

      <B><FONT COLOR="#A020F0">val</FONT></B> a = <B><FONT COLOR="#5F9EA0">10</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> b = <B><FONT COLOR="#5F9EA0">11</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> c = <B><FONT COLOR="#5F9EA0">12</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> d = <B><FONT COLOR="#5F9EA0">13</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> e = <B><FONT COLOR="#5F9EA0">14</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> f = <B><FONT COLOR="#5F9EA0">15</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
where 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> fst (x, _) = x
</PRE>
<p>
 
</p>
<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 <tt>make</tt> 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 <tt>I</tt>, <tt>II</tt>, <tt>W</tt> 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>
<p>
Second, the step function, <tt>`</tt>, is abstracted over the actual construction operation, which is created by make, and passed along the fold.  This allows the same constant, <tt>`</tt>, 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>
<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>



<p>
<hr>
Last edited on 2006-05-28 08:52:54 by <span title="cs181143070.pp.htv.fi"><a href="VesaKarvonen">VesaKarvonen</a></span>.
</body></html>