Import Upstream version 20180207
[hcoop/debian/mlton.git] / mlton / backend / scale.fun
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2004-2007 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8functor Scale (S: SCALE_STRUCTS): SCALE =
9struct
10
11open S
12
13datatype t = One | Two | Four | Eight
14
15val toString =
16 fn One => "1"
17 | Two => "2"
18 | Four => "4"
19 | Eight => "8"
20
21val layout = Layout.str o toString
22
23val fromInt: int -> t option =
24 fn 1 => SOME One
25 | 2 => SOME Two
26 | 4 => SOME Four
27 | 8 => SOME Eight
28 | _ => NONE
29
30val fromBytes: Bytes.t -> t option =
31 fromInt o Bytes.toInt
32
33val toInt: t -> int =
34 fn One => 1
35 | Two => 2
36 | Four => 4
37 | Eight => 8
38
39end