/
order.opa
202 lines (167 loc) · 6.38 KB
/
order.opa
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Copyright © 2011, 2012 MLstate
This file is part of Opa.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Ordering elements.
*
* Functions from this module are used to define maps, sets, etc.
*
* @author David Rajchenbach-Teller, Raja Boujbel 2010
*/
/**
* {1 About this module}
*
* A total order on elements of a given type.
*
* Total orders are characterized both by the type of elements they can order
* and by an additional type label which may be used to ensure that two
* orderings on elements of the same type cannot be confused.
*
* For instance, it is possible to define three totally incompatible orderings on strings,
* one which differentiates between uppercase and lowercase ([String.order]),
* one which mixes uppercase and lowercase ([String.order_ci]) and one which attempts to
* interpret numbers embedded in texts ([String.order_natural]). If we define
* a set to which we add elements "hello 1", "Hello 1" and "Hello 01", three
* behaviors are possible: if the set has been defined with [String.order],
* the result will contain 3 elements; if the set has been defined with [String.order_ci],
* the result will contain 2 elements; if the set has been defined with [String.order_natural],
* the result will contain only 1 element.
*
* To ensure that the three orderings cannot be confused, hence resulting in unspecified
* set contents, the sets defined from these distinct orders have different types, respectively
* [set(string, {String.order})], [set(string, {String.order_ci})] and [set(string, {String.order_natural})].
*
* @param 'item The type of elements compared
* @param 'label A label used to differentiate incompatible orderings
* on elements of the same type.
*
* {1 Where should I start ?}
*
* {1 What if I need more?}
*/
/**
* {1 Types defined in this module}
*/
@abstract type order('item, 'label) = ('item, 'item -> Order.ordering)
@abstract type Order.default = void
@opacapi
type Order.comparison = Order.ordering / {neq}
type Order.ordering = {lt} / {gt} / {eq}
/**
* {1 Interface}
*/
Order = {{
string_of_comparison(x:Order.comparison) =
match x with
| {lt} -> "lt"
| {eq} -> "eq"
| {gt} -> "gt"
| {neq}-> "neq"
string_of_ordering(x:Order.ordering) =
string_of_comparison(x <: Order.comparison)
/**
* Define an order from a comparison function.
*
* @param compare A function used to compare two elements of the same type.
* @return An order on elements of a given type. You should coerce this with an ordering label to ensure
* safety of data structures built from this order.
*/
make_unsafe(cmp: ('a, 'a -> Order.comparison)) =
//TODO: Using @typeof, reject ['a] for which the comparison does not create a total order
sanitized(a,b) = match cmp(a,b) with
| {lt} -> {lt}
| {gt} -> {gt}
| {eq} -> {eq}
| {neq}-> error("[Order.compare] These two elements cannot be compared")
sanitized : order('a, 'b)
make(cmp: ('a, 'a -> Order.ordering)) =
cmp : order('a, 'b)
make_by(f: 'a -> 'b, o: order('b, 'c)) : order('a, 'd) =
(x, y -> compare(f(x), f(y), o))
reverse(order: order('item, 'label)):order('item, 'other_label) =
reversed(x, y) = match order(x,y) with
| {gt} -> {lt}
| {lt} -> {gt}
| {eq} -> {eq}
make(reversed)
product(order1:order('item1,'label1),order2:order('item2,'label2)) : order(('item1,'item2),('label1,'label2)) =
aux((l1,l2),(r1,r2)) =
match compare(l1,r1,order1) with
| {eq} -> compare(l2,r2,order2)
| c -> c
make(aux)
/**
* The default ordering on items of any type.
*
* Do not attempt to use this ordering on items which contain functions
*/
default =
@nonexpansive(make_unsafe(@toplevel.compare)) : order('a, Order.default)
/**
* Compare two elements using a given order.
*/
ordering(x:'a, y:'a, order:order('a, 'b)): Order.ordering =
order(x,y)
is_smaller(x:'a, y:'a, order:order('a, 'b)): bool =
match compare(x,y,order) with
| {lt} -> true
| _ -> false
is_smallereq(x:'a, y:'a, order:order('a, 'b)): bool =
match compare(x,y,order) with
| {lt} | {eq} -> true
| _ -> false
is_greater(x:'a, y:'a, order:order('a, 'b)): bool =
match compare(x,y,order) with
| {gt} -> true
| _ -> false
is_greatereq(x:'a, y:'a, order:order('a, 'b)): bool =
match compare(x,y,order) with
| {gt} | {eq } -> true
| _ -> false
/**
* Checks the equality of two elements using a given order
*/
equals(x:'item,y:'item,order:order('a,'order)): bool =
match order(x,y) with {eq} -> {true} | _ -> {false}
/**
* Checks the inequality of two elements using a given order
*/
not_equal(x:'item,y:'item,order:order('a,'order)) =
match order(x,y) with {eq} -> {false} | _ -> {true}
compose(order1:order('a, 'b), order2:order('a, 'b)) : order('a, 'b) =
comp(x, y) =
match order1(x,y) with
| {eq} -> order2(x, y)
| r -> r
comp
of_int(i) : Order.ordering =
match i with
| -1 -> {lt}
| 0 -> {eq}
| 1 -> {gt}
| _ -> @fail
to_int(o:Order.ordering) =
match o with
| {lt} -> -1
| {eq} -> 0
| {gt} -> 1
make_from_low_level_compare(cmp: ('a, 'a -> int)): order('a, 'b) =
(
order(x,y) = of_int(cmp(x,y))
order
)
/*
BEGIN: Transitional block -- progressively remove these functions
*/
//@deprecated({use="equals"})
equal = equals
//@deprecated({use="ordering"})
compare = ordering//Doesn't match our naming conventions ([compare] vs. [ordering])
/*
END: Transitional block
*/
}}