-
Notifications
You must be signed in to change notification settings - Fork 0
/
odist_red.ml
313 lines (274 loc) · 7.48 KB
/
odist_red.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
open Odist_fold
open Odist_infix
open Odist_util
module Stream = Odist_stream
let and_reducer = monoid true (&&) |> with_maximum_check not
let or_reducer = monoid false (||) |> with_maximum_check id
let forall p = map p >> reduce and_reducer
let exists p = map p >> reduce or_reducer
let max_reducer compare =
let max a b = if compare a b >= 0 then a else b
in opt_monoid max
let min_reducer compare =
let min a b = if compare a b >= 0 then a else b
in opt_monoid min
let first =
let add a b = match a with
| None -> Some b
| _ -> a
in
let merge a b = match a with
| None -> b
| _ -> a
in
{
monoid = {
empty = (fun () -> None);
add = add;
merge = merge;
maximum = Some (function None -> false | _ -> true);
items = (fun o -> Stream.of_option o);
};
inject = add;
result = id;
}
let last =
let add a b = Some b in
let merge a b = match b with
| None -> a
| _ -> b
in
{
monoid = {
empty = (fun () -> None);
add = add;
merge = merge;
maximum = None;
items = Stream.of_option;
};
inject = add;
result = id;
}
(* FIXME: how to avoid taking more than n items. *)
let taking n reducer =
let counter = monoid 0 (+) |> mapping (fun x -> 1) in
pair_reducer counter reducer |> returning snd |> with_maximum_check (fun (c,_) -> c >= n)
let partition p true_red false_red =
let inject (ts,fs) x = if p x then (true_red.inject ts x, fs) else (ts, false_red.inject fs x) in
let pair_result (ts,fs) = (true_red.result ts, false_red.result fs) in
{
monoid = pair_monoid true_red.monoid false_red.monoid;
inject = inject;
result = pair_result;
}
let to_string_buffer size =
reducer_of_monoid {
empty = (fun () -> Buffer.create size);
add = (fun buf str -> Buffer.add_string buf str; buf);
merge = (fun buf str -> Buffer.add_buffer buf str; buf);
maximum = None;
items = Buffer.contents >> Stream.of_single;
}
let to_string = to_string_buffer 16 |> returning Buffer.contents
let to_list =
let add xs x = x::xs in
{
monoid = {
empty = (fun () -> []);
add = add;
merge = (fun xs ys -> ys @ xs);
maximum = None;
items = (fun xs -> List.rev xs |> Stream.of_list);
};
inject = add;
result = (fun xs -> List.rev xs);
}
let to_bag =
let add xs x = x::xs in
{
monoid = {
empty = (fun () -> []);
add = add;
merge = (fun xs ys -> List.rev_append xs ys);
maximum = None;
items = Stream.of_list;
};
inject = add;
result = id;
}
let list_zipper red =
let merge f z xs ys =
let rec loop acc xs ys = match xs,ys with
| _, [] -> List.rev_append acc xs
| [], y::ys -> loop ((f (z ()) y)::acc) xs ys
| x::xs, y::ys -> loop ((f x y)::acc) xs ys
in
loop [] xs ys
in
{
monoid = {
empty = (fun () -> []);
add = merge red.monoid.add red.monoid.empty;
merge = merge red.monoid.merge red.monoid.empty;
maximum = option_lift List.for_all red.monoid.maximum;
items = Stream.of_single;
};
inject = merge red.inject red.monoid.empty;
result = List.map red.result;
}
module type SET = sig
include Set.S
val union_reducer : (elt, t, elt, t) red
val items: t -> elt col
end
module SetRed(S: Set.S) = struct
include S
let to_sfoldable xs =
Stream.Stream {
Stream.sfold = (fun red acc -> S.fold (fun x xs -> red xs x) xs acc);
}
let union_reducer =
reducer_of_monoid {
empty = const S.empty;
add = (fun xs x -> S.add x xs);
merge = S.union;
maximum = None;
items = to_sfoldable;
}
let items xs = Seqcol (to_sfoldable xs)
end
module MakeSetRed(E: Set.OrderedType) = SetRed(Set.Make(E))
module type MAP = sig
include Map.S
val grouping_with: ('a,'b,'b,'b) red -> (key * 'a, 'b t, key * 'b, 'b t) red
val grouping_by: ('a -> key) -> ('a,'b,'b,'b) red -> ('a, 'b t, key * 'b, 'b t) red
val grouping: (key,'b,'b,'b) red -> (key, 'b t, key * 'b, 'b t) red
val pairs: 'a t -> (key * 'a) col
end
module MapRed(M: Map.S) = struct
include M
let to_sfoldable kvs =
Stream.Stream {
Stream.sfold = (fun red acc -> M.fold (fun k v acc -> red acc (k,v)) kvs acc);
}
let grouping_with value_reducer =
let m_reducer = value_reducer.monoid in
let get m k = try M.find k m with Not_found -> m_reducer.empty () in
let value_inserter inject m (k,v) = let v' = get m k in let v'' = inject v' v in M.add k v'' m in
let value_merger k oa ob = match (oa,ob) with
| (None, _) -> ob
| (_, None) -> oa
| (Some a, Some b) -> Some (m_reducer.merge a b)
in
{
monoid = {
empty = (fun () -> M.empty);
add = value_inserter m_reducer.add;
merge = M.merge value_merger;
maximum = None;
items = to_sfoldable;
};
inject = value_inserter value_reducer.inject;
result = id;
}
let grouping_by k reducer = mapping (fun x -> (k x,x)) (grouping_with reducer)
let grouping reducer = mapping (fun x -> (x,x)) (grouping_with reducer)
let pairs kvs = Seqcol (to_sfoldable kvs);
end
module MakeMapRed(E: Map.OrderedType) = MapRed(Map.Make(E))
let stream_of_array_i items a =
let fold red =
let n = Array.length a in
let get = Array.get a >> items in
let red_i i s x = red s (i,x) in
let rec loop i s =
if i = n then s
else
let s' = Stream.fold (red_i i) s (get i) in
loop (i+1) s'
in loop 0
in
Stream.Stream { Stream.sfold = fold }
let array_reducer red =
let monoid = red.monoid in
let empty_item i = monoid.empty () in
let empty () = Array.init 0 empty_item in
let extend a length =
let missing = length - (Array.length a) in
if missing > 0
then Array.append a (Array.init missing empty_item)
else a
in
let dispatch add a (i,x) =
let a = extend a (i+1) in
let x = add (Array.get a i) x in
Array.set a i x; a
in
let merge a b =
let update i x =
let x = monoid.merge x (Array.get b i)
in Array.set a i x
in
let a = extend a (Array.length b) in
Array.iteri update a; a
in
{
monoid = {
empty = empty;
add = dispatch monoid.add;
merge = merge;
maximum = None; (* TODO *)
items = stream_of_array_i monoid.items
};
inject = dispatch red.inject;
result = Array.map red.result;
}
module type NUM = sig
type t
val zero: t
val one: t
val add: t -> t -> t
val mul: t -> t -> t
end
module type NUMRED = sig
include NUM
val sum: (t, t, t, t) red
val product: (t, t, t, t) red
val count: ('a, t, t, t) red
val square_sum: (t, t, t, t) red
end
module NumRed(N: NUM) = struct
include N
let sum = monoid N.zero N.add
let product = monoid N.one N.mul |> with_maximum N.zero
let count = { sum with inject = (fun c _ -> N.add c N.one) }
let square_sum = mapping (fun x -> N.mul x x) sum
end
module CamlInt = struct
type t = int
let zero = 0
let one = 1
let add = (+)
let mul = ( * )
end
module Int = NumRed(CamlInt)
module CamlFloat = struct
type t = float
let zero = 0.0
let one = 1.0
let add = (+.)
let mul = ( *. )
end
module Float = NumRed(CamlFloat)
module NumBigInt = struct
type t = Big_int.big_int
let zero = Big_int.zero_big_int
let one = Big_int.unit_big_int
let add = Big_int.add_big_int
let mul = Big_int.mult_big_int
end
module BigInt = NumRed(NumBigInt)
module Int32 = NumRed(Int32)
module Int64 = NumRed(Int64)
module Nativeint = NumRed(Nativeint)
module Complex = NumRed(Complex)