| 1 |
= NumericLiteral |
| 2 |
|
| 3 |
Numeric literals in <<StandardML#,Standard ML>> can be written in either |
| 4 |
decimal or hexadecimal notation. Sometimes it can be convenient to |
| 5 |
write numbers down in other bases. Fortunately, using <<Fold#>>, it is |
| 6 |
possible to define a concise syntax for numeric literals that allows |
| 7 |
one to write numeric constants in any base and of various types |
| 8 |
(`int`, `IntInf.int`, `word`, and more). |
| 9 |
|
| 10 |
We will define constants `I`, `II`, `W`, and ``` so |
| 11 |
that, for example, |
| 12 |
[source,sml] |
| 13 |
---- |
| 14 |
I 10 `1`2`3 $ |
| 15 |
---- |
| 16 |
denotes `123:int` in base 10, while |
| 17 |
[source,sml] |
| 18 |
---- |
| 19 |
II 8 `2`3 $ |
| 20 |
---- |
| 21 |
denotes `19:IntInf.int` in base 8, and |
| 22 |
[source,sml] |
| 23 |
---- |
| 24 |
W 2 `1`1`0`1 $ |
| 25 |
---- |
| 26 |
denotes `0w13: word`. |
| 27 |
|
| 28 |
Here is the code. |
| 29 |
|
| 30 |
[source,sml] |
| 31 |
---- |
| 32 |
structure Num = |
| 33 |
struct |
| 34 |
fun make (op *, op +, i2x) iBase = |
| 35 |
let |
| 36 |
val xBase = i2x iBase |
| 37 |
in |
| 38 |
Fold.fold |
| 39 |
((i2x 0, |
| 40 |
fn (i, x) => |
| 41 |
if 0 <= i andalso i < iBase then |
| 42 |
x * xBase + i2x i |
| 43 |
else |
| 44 |
raise Fail (concat |
| 45 |
["Num: ", Int.toString i, |
| 46 |
" is not a valid\ |
| 47 |
\ digit in base ", |
| 48 |
Int.toString iBase])), |
| 49 |
fst) |
| 50 |
end |
| 51 |
|
| 52 |
fun I ? = make (op *, op +, id) ? |
| 53 |
fun II ? = make (op *, op +, IntInf.fromInt) ? |
| 54 |
fun W ? = make (op *, op +, Word.fromInt) ? |
| 55 |
|
| 56 |
fun ` ? = Fold.step1 (fn (i, (x, step)) => |
| 57 |
(step (i, x), step)) ? |
| 58 |
|
| 59 |
val a = 10 |
| 60 |
val b = 11 |
| 61 |
val c = 12 |
| 62 |
val d = 13 |
| 63 |
val e = 14 |
| 64 |
val f = 15 |
| 65 |
end |
| 66 |
---- |
| 67 |
where |
| 68 |
[source,sml] |
| 69 |
---- |
| 70 |
fun fst (x, _) = x |
| 71 |
---- |
| 72 |
|
| 73 |
The idea is for the fold to start with zero and to construct the |
| 74 |
result one digit at a time, with each stepper multiplying the previous |
| 75 |
result by the base and adding the next digit. The code is abstracted |
| 76 |
in two different ways for extra generality. First, the `make` |
| 77 |
function abstracts over the various primitive operations (addition, |
| 78 |
multiplication, etc) that are needed to construct a number. This |
| 79 |
allows the same code to be shared for constants `I`, `II`, `W` used to |
| 80 |
write down the various numeric types. It also allows users to add new |
| 81 |
constants for additional numeric types, by supplying the necessary |
| 82 |
arguments to make. |
| 83 |
|
| 84 |
Second, the step function, `{backtick}`, is abstracted over the actual |
| 85 |
construction operation, which is created by make, and passed along the |
| 86 |
fold. This allows the same constant, `{backtick}`, to be used for all |
| 87 |
numeric types. The alternative approach, having a different step |
| 88 |
function for each numeric type, would be more painful to use. |
| 89 |
|
| 90 |
On the surface, it appears that the code checks the digits dynamically |
| 91 |
to ensure they are valid for the base. However, MLton will simplify |
| 92 |
everything away at compile time, leaving just the final numeric |
| 93 |
constant. |