Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / util / integral-comparisons.sml
1 (* Copyright (C) 1999-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
8 functor IntegralComparisons (type t
9 val < : t * t -> bool) =
10 struct
11 val < : t * t -> bool = <
12 fun <= (a, b) = not (< (b, a))
13 fun > (a, b) = < (b, a)
14 fun >= (a, b) = <= (b, a)
15
16 fun compare (i, j) =
17 if < (i, j) then LESS
18 else if < (j, i) then GREATER
19 else EQUAL
20 fun min (x, y) = if < (x, y) then x else y
21 fun max (x, y) = if < (x, y) then y else x
22 end
23 functor UnsignedIntegralComparisons (type int
24 type word
25 val idFromIntToWord : int -> word
26 val < : word * word -> bool) =
27 struct
28 local
29 fun ltu (i: int, i': int) = < (idFromIntToWord i, idFromIntToWord i')
30 structure S = IntegralComparisons (type t = int
31 val < = ltu)
32 in
33 val ltu = S.<
34 val leu = S.<=
35 val gtu = S.>
36 val geu = S.>=
37 end
38 end