Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / bounded-order.fun
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006, 2008 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(* BoundedOrder *)
9(*------------------------------------------------------------------*)
10
11functor BoundedOrder(O: ORDER): BOUNDED_ORDER =
12struct
13
14structure O = O
15structure R = Relation
16
17datatype t =
18 Min
19 | Max
20 | Inject of O.t
21
22val smallest = Min
23val largest = Max
24val inject = Inject
25
26val project =
27 fn Inject x => x
28 | _ => Error.bug "BoundedOrder.project"
29
30val compare =
31 fn (Min, Min) => R.EQUAL
32 | (Min, _) => R.LESS
33 | (Max, Max) => R.EQUAL
34 | (Max, _) => R.GREATER
35 | (Inject _, Min) => R.GREATER
36 | (Inject _, Max) => R.LESS
37 | (Inject x, Inject y) => O.compare(x, y)
38
39val {equals, <, <=, >, >=, min, max} = R.compare compare
40
41local open Layout
42in fun layout x =
43 case x of
44 Min => str "Min"
45 | Max => str "Max"
46 | Inject x => O.layout x
47end
48
49end