/usr/share/doc/mlton/guide/NumericLiteral is in mlton-doc 20100608-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | <!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>
<a href = "TitleIndex">Index</a>
</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: 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) =>
<B><FONT COLOR="#A020F0">if</FONT></B> <B><FONT COLOR="#5F9EA0">0</FONT></B> <= i <B><FONT COLOR="#A020F0">andalso</FONT></B> i < 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">"Num: "</FONT></B>, Int.toString i,
<B><FONT COLOR="#BC8F8F">" is not a valid\
\ digit in base "</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)) =>
(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>
|