Source file pipe.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
type void
module type Monad = sig
type 'a t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end
module type S = sig
type 'a monad
type 'a thunk = unit -> 'a
type finalizer = (unit -> unit monad) option
type ('i, 'o, 'r) t =
| Has_output of 'o * ('i, 'o, 'r) t thunk * finalizer
| Needs_input of ('i option -> ('i, 'o, 'r) t)
| Done of 'r
| PipeM of ('i, 'o, 'r) t monad thunk
type 'a source = (void, 'a, unit) t
type 'a sink = ('a, void, unit) t
val return : 'r -> (_, _, 'r) t
val bind : ('i, 'o, 'a) t -> ('a -> ('i, 'o, 'b) t) -> ('i, 'o, 'b) t
module Monad_infix : sig
val ( >>= ) : ('i, 'o, 'a) t -> ('a -> ('i, 'o, 'b) t) -> ('i, 'o, 'b) t
end
val await : unit -> ('a, _, 'a option) t
val yield : 'o -> (_, 'o, unit) t
val compose : ('i, 'a, _) t -> ('a, 'o, 'r) t -> ('i, 'o, 'r) t
val ( $$ ) : ('i, 'a, _) t -> ('a, 'o, 'r) t -> ('i, 'o, 'r) t
val run : (void, void, 'r) t -> 'r monad
val bracket :
(unit -> 'a monad) ->
('a -> unit monad) ->
('a -> ('i, 'o, 'r) t) ->
('i, 'o, 'r) t
val fold : 'r -> ('i -> 'r -> 'r) -> ('i, void, 'r) t
val map : ('i -> 'o) -> ('i, 'o, unit) t
val mapi : (int -> 'i -> 'o) -> ('i, 'o, unit) t
val filter : ('i -> bool) -> ('i, 'i, unit) t
val filter_map : ('i -> 'o option) -> ('i, 'o, unit) t
val from_list : 'a list -> 'a source
val to_list : unit -> ('a, void, 'a list) t
val all : unit -> (('a, 'b) result,
void,
('a list, 'b) result) t
val loop : ('a -> 'b option -> 'a * 'c list) -> 'a -> ('b, 'c, unit) t
val loop' : ('a -> 'b option -> ('a * 'c list, 'd) result) -> 'a -> ('b, 'c, (unit, 'd) result) t
val drop : int -> ('a, 'a, unit) t
end
module Make(M : Monad) = struct
let ( >>= ) x f = M.bind x f
type 'a monad = 'a M.t
type 'a thunk = unit -> 'a
type finalizer = (unit -> unit M.t) option
type ('i, 'o, 'r) t =
| Has_output of 'o * ('i, 'o, 'r) t thunk * finalizer
| Needs_input of ('i option -> ('i, 'o, 'r) t)
| Done of 'r
| PipeM of ('i, 'o, 'r) t M.t thunk
type 'a source = (void, 'a, unit) t
type 'a sink = ('a, void, unit) t
let return x = Done x
let rec bind
= fun x f ->
match x with
| Has_output (o, next, cleanup) ->
Has_output (o, (fun () -> bind (next ()) f), cleanup)
| Needs_input on_input ->
Needs_input (fun i -> bind (on_input i) f)
| Done r -> f r
| PipeM m -> PipeM (fun () ->
m () >>= fun p ->
M.return (bind p f)
)
module Monad_infix = struct
let ( >>= ) x f = bind x f
end
let await () =
Needs_input (fun i -> Done i)
let yield x =
Has_output (x, (fun () -> Done ()), None)
let finalizer_compose f g =
match f, g with
| None, None -> None
| Some f, None -> Some f
| None, Some g -> Some g
| Some f, Some g -> Some (fun () -> f () >>= g)
let finalize = function
| None -> M.return ()
| Some f -> f ()
let compose p q =
let rec go_right final left = function
| Has_output (o, next, clean) ->
let next () = go_right final left (next ()) in
let clean = finalizer_compose clean final in
Has_output (o, next, clean)
| Needs_input f ->
go_left f final left
| Done r ->
PipeM (fun () ->
finalize final >>= fun () ->
M.return (Done r)
)
| PipeM m ->
PipeM (fun () ->
m () >>= fun p ->
M.return (
go_right final left p
)
)
and go_left next_right final = function
| Has_output (o, next, final') ->
go_right final' (next ()) (next_right (Some o))
| Needs_input f ->
Needs_input (fun i -> go_left next_right final (f i))
| Done r ->
go_right None (Done r) (next_right None)
| PipeM m ->
PipeM (fun () ->
m () >>= fun p ->
M.return (go_left next_right final p)
)
in
go_right None p q
let ( $$ ) = compose
let rec run = function
| Done r -> M.return r
| PipeM m -> m () >>= run
| Has_output _ -> assert false
| Needs_input _ -> assert false
let bracket alloc free f =
let rec add_clean_up f = function
| Done x ->
PipeM (fun () ->
f () >>= fun () ->
M.return (Done x)
)
| Has_output (o, next, final) ->
Has_output (o,
(fun () -> add_clean_up f (next ())),
finalizer_compose (Some f) final)
| Needs_input next ->
Needs_input (fun i -> add_clean_up f (next i))
| PipeM m ->
PipeM (fun () ->
m () >>= fun next ->
M.return (add_clean_up f next)
)
in
PipeM (fun () ->
alloc () >>= fun seed ->
M.return (add_clean_up (fun () -> free seed) (f seed))
)
let rec fold init f =
Needs_input (function
| None -> Done init
| Some i -> fold (f i init) f
)
let rec map f =
let open Monad_infix in
await () >>= function
| None -> return ()
| Some x ->
yield (f x) >>= fun () -> map f
let mapi f =
let open Monad_infix in
let rec aux i =
await () >>= function
| None -> return ()
| Some x ->
yield (f i x) >>= fun () -> aux (i + 1)
in
aux 0
let rec filter f =
let open Monad_infix in
await () >>= function
| None -> return ()
| Some x ->
if f x then yield x >>= fun () -> filter f
else filter f
let rec filter_map f =
let open Monad_infix in
await () >>= function
| None -> return ()
| Some x ->
match f x with
| None -> filter_map f
| Some y -> yield y >>= fun () -> filter_map f
let from_list l =
let open Monad_infix in
let rec loop = function
| [] -> Done ()
| h :: t ->
yield h >>= fun () ->
loop t
in
loop l
let to_list () =
let open Monad_infix in
let rec loop accu =
await () >>= function
| None -> Done (List.rev accu)
| Some x -> loop (x :: accu)
in
loop []
let all () =
let open Monad_infix in
let rec loop accu =
await () >>= function
| None -> Done (Ok (List.rev accu))
| Some (Ok x) -> loop (x :: accu)
| Some (Error _ as e) -> Done e
in
loop []
let rec loop f st =
let open Monad_infix in
await () >>= fun i ->
let (st', ys) = f st i in
let rec loop' ys =
match ys, i with
| [], None -> Done ()
| [], Some _ -> loop f st'
| h :: t, _ ->
yield h >>= fun () -> loop' t
in
loop' ys
let rec loop' f st =
let open Monad_infix in
await () >>= fun i ->
match f st i with
| Ok (st', ys) ->
let rec inner ys =
match ys, i with
| [], None -> Done (Ok ())
| [], Some _ -> loop' f st'
| h :: t, _ ->
yield h >>= fun () -> inner t
in
inner ys
| Error _ as e -> Done e
let drop n =
let open Monad_infix in
let rec loop k =
await () >>= function
| None -> return ()
| Some x ->
if k <= 0 then
yield x >>= fun () -> loop 0
else
loop (k - 1)
in
loop n
end