Source file batBounded.ml
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
module O = BatOrd
exception Invalid_bounds
type 'a bound_t = [ `o of 'a | `c of 'a | `u]
type ('a, 'b) bounding_f = bounds:('a bound_t * 'a bound_t) -> 'a -> 'b
let ret_some x = Some x
let ret_none _ = None
let const a _ = a
external identity : 'a -> 'a = "%identity"
let bounding_of_ord ~default_low ~default_high conv ord =
fun ~(bounds : 'a bound_t * 'a bound_t) ->
match bounds with
| `c l, `c u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _ -> default_low
| _, O.Gt -> default_high
| O.Eq, _
| _, O.Eq
| O.Gt, _ -> conv x
end
| `u, `c u -> begin
fun x ->
match ord x u with
| O.Gt -> default_high
| O.Eq
| O.Lt -> conv x
end
| `c l, `u -> begin
fun x ->
match ord x l with
| O.Lt -> default_low
| O.Gt
| O.Eq -> conv x
end
| `u, `u -> conv
| `o l, `o u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _
| O.Eq, _ -> default_low
| _, O.Gt
| _, O.Eq -> default_high
| O.Gt, _ -> conv x
end
| `u, `o u -> begin
fun x ->
match ord x u with
| O.Gt
| O.Eq -> default_high
| O.Lt -> conv x
end
| `o l, `u -> begin
fun x ->
match ord x l with
| O.Lt
| O.Eq -> default_low
| O.Gt -> conv x
end
| `c l, `o u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _ -> default_low
| _, O.Gt
| _, O.Eq -> default_high
| O.Eq, _
| O.Gt, _ -> conv x
end
| `o l, `c u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _
| O.Eq, _ -> default_low
| _, O.Gt -> default_high
| _, O.Eq
| O.Gt, _ -> conv x
end
let bounding_of_ord_chain ~low ~high conv ord =
fun ~(bounds : 'a bound_t * 'a bound_t) ->
match bounds with
| `c l, `c u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _ -> low x
| _, O.Gt -> high x
| O.Eq, _
| _, O.Eq
| O.Gt, _ -> conv x
end
| `u, `c u -> begin
fun x ->
match ord x u with
| O.Gt -> high x
| O.Eq
| O.Lt -> conv x
end
| `c l, `u -> begin
fun x ->
match ord x l with
| O.Lt -> low x
| O.Gt
| O.Eq -> conv x
end
| `o l, `o u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _
| O.Eq, _ -> low x
| _, O.Gt
| _, O.Eq -> high x
| O.Gt, _ -> conv x
end
| `u, `o u -> begin
fun x ->
match ord x u with
| O.Gt
| O.Eq -> high x
| O.Lt -> conv x
end
| `o l, `u -> begin
fun x ->
match ord x l with
| O.Lt
| O.Eq -> low x
| O.Gt -> conv x
end
| `c l, `o u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _ -> low x
| _, O.Gt
| _, O.Eq -> high x
| O.Eq, _
| O.Gt, _ -> conv x
end
| `o l, `c u -> begin
if ord l u = O.Gt then raise Invalid_bounds;
fun x ->
match ord x l, ord x u with
| O.Lt, _
| O.Eq, _ -> low x
| _, O.Gt -> high x
| _, O.Eq
| O.Gt, _ -> conv x
end
| `u, `u -> conv
let saturate_of_ord ~(bounds : 'a bound_t * 'a bound_t) ord =
match bounds with
| `o l, `o h
| `c l, `c h
| `o l, `c h
| `c l, `o h ->
bounding_of_ord_chain
~low:(const l) ~high:(const h) identity ord ~bounds
| `u, `o h
| `u, `c h ->
bounding_of_ord_chain ~low:identity ~high:(const h) identity ord ~bounds
| `o l, `u
| `c l, `u ->
bounding_of_ord_chain ~low:(const l) ~high:identity identity ord ~bounds
| `u, `u ->
bounding_of_ord_chain ~low:identity ~high:identity identity ord ~bounds
let opt_of_ord ~(bounds : 'a bound_t * 'a bound_t) ord =
bounding_of_ord_chain ~low:ret_none ~high:ret_none ret_some ord ~bounds
module type BoundedType = sig
type base_t
type t
val bounds : base_t bound_t * base_t bound_t
val bounded : (base_t, t) bounding_f
val base_of_t : t -> base_t option
val base_of_t_exn : t -> base_t
end
module type BoundedNumericType = sig
include BoundedType
module Infix : BatNumber.Infix with type bat__infix_t := base_t
end
module type S = sig
type base_u
type u
type t = private u
val bounds : base_u bound_t * base_u bound_t
val make : base_u -> t
external extract : t -> base_u) -> t -> t option
val map2 : (base_u -> base_u -> base_u) -> t -> t -> t option
val map_exn : (base_u -> base_u) -> t -> t
val map2_exn : (base_u -> base_u -> base_u) -> t -> t -> t
end
module type NumericSig = sig
include S
val ( + ) : t -> base_u -> t
val ( - ) : t -> base_u -> t
val ( * ) : t -> base_u -> t
val ( / ) : t -> base_u -> t
val ( +: ) : t -> t -> t
val ( -: ) : t -> t -> t
val ( *: ) : t -> t -> t
val ( /: ) : t -> t -> t
end
module Make(M : BoundedType) : (
S with type base_u = M.base_t with type u = M.t with type t = private M.t
) = struct
include M
type base_u = base_t
type u = t
let make = bounded ~bounds
external extract : t -> Option.map make (BatOption.map f (base_of_t x))
let map2 f x y =
match base_of_t x, base_of_t y with
| Some bx, Some by ->
Some (make (f bx by))
| None, Some _
| Some _, None
| None, None ->
None
let map_exn f x =
make (f (base_of_t_exn x))
let map2_exn f x y =
let bx = base_of_t_exn x in
let by = base_of_t_exn y in
make (f bx by)
end
module MakeNumeric(M : BoundedNumericType) = struct
include Make(M)
module I = M.Infix
let ( + ) a b = map_exn (I.( + ) b) a
let ( - ) a b = map_exn (I.( - ) b) a
let ( * ) a b = map_exn (I.( * ) b) a
let ( / ) a b = map_exn (I.( / ) b) a
let ( +: ) = map2_exn I.( + )
let ( -: ) = map2_exn I.( - )
let ( *: ) = map2_exn I.( * )
let ( /: ) = map2_exn I.( / )
end