-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathmonad.ml
312 lines (241 loc) · 6.65 KB
/
monad.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
open! Import
module List = List0
include Monad_intf
module type Basic_general = sig
type ('a, 'i, 'j, 'd, 'e) t
val bind
: ('a, 'i, 'j, 'd, 'e) t
-> f:('a -> ('b, 'j, 'k, 'd, 'e) t)
-> ('b, 'i, 'k, 'd, 'e) t
val map
: [ `Define_using_bind
| `Custom of ('a, 'i, 'j, 'd, 'e) t -> f:('a -> 'b) -> ('b, 'i, 'j, 'd, 'e) t
]
val return : 'a -> ('a, 'i, 'i, 'd, 'e) t
end
module Make_general (M : Basic_general) = struct
let bind = M.bind
let return = M.return
let map_via_bind ma ~f = M.bind ma ~f:(fun a -> M.return (f a))
let map =
match M.map with
| `Define_using_bind -> map_via_bind
| `Custom x -> x
;;
module Monad_infix = struct
let ( >>= ) t f = bind t ~f
let ( >>| ) t f = map t ~f
end
include Monad_infix
module Let_syntax = struct
let return = return
include Monad_infix
module Let_syntax = struct
let return = return
let bind = bind
let map = map
let both a b = a >>= fun a -> b >>| fun b -> a, b
module Open_on_rhs = struct end
end
end
let join t = t >>= fun t' -> t'
let ignore_m t = map t ~f:(fun _ -> ())
let all =
let rec loop vs = function
| [] -> return (List.rev vs)
| t :: ts -> t >>= fun v -> loop (v :: vs) ts
in
fun ts -> loop [] ts
;;
let rec all_unit = function
| [] -> return ()
| t :: ts -> t >>= fun () -> all_unit ts
;;
end
module Make_indexed (M : Basic_indexed) :
S_indexed with type ('a, 'i, 'j) t := ('a, 'i, 'j) M.t = Make_general (struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'i, 'j) M.t
end)
module Make3 (M : Basic3) : S3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) M.t =
Make_general (struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd, 'e) M.t
end)
module Make2 (M : Basic2) : S2 with type ('a, 'd) t := ('a, 'd) M.t = Make_general (struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd) M.t
end)
module Make (M : Basic) : S with type 'a t := 'a M.t = Make_general (struct
include M
type ('a, 'i, 'j, 'd, 'e) t = 'a M.t
end)
module Make2_local (M : Basic2_local) = struct
let bind = M.bind
let return = M.return
let map_via_bind ma ~f =
let res = M.bind ma ~f:(fun a -> M.return (f a)) in
res
;;
let map =
match M.map with
| `Define_using_bind -> map_via_bind
| `Custom x -> x
;;
module Monad_infix = struct
let ( >>= ) t f = bind t ~f
let ( >>| ) t f = map t ~f
end
include Monad_infix
module Let_syntax = struct
let return = return
include Monad_infix
module Let_syntax = struct
let return = return
let bind = bind
let map = map
let both a b =
let res =
bind a ~f:(fun a ->
let res = map b ~f:(fun b -> a, b) in
res)
in
res
;;
module Open_on_rhs = struct end
end
end
let join t = t >>= Fn.id
let ignore_m t =
let res = map t ~f:(fun _ -> ()) in
res
;;
let all =
let rec loop vs = function
| [] -> return (List.rev vs)
| t :: ts -> t >>= fun v -> loop (v :: vs) ts
in
fun ts -> loop [] ts
;;
let rec all_unit = function
| [] -> return ()
| t :: ts -> t >>= fun () -> all_unit ts
;;
end
module Make_local (M : Basic_local) : S_local with type 'a t := 'a M.t =
Make2_local (struct
include M
type ('a, 'e) t = 'a M.t
end)
module Of_monad_general
(Monad : sig
type ('a, 'i, 'j, 'd, 'e) t
val bind
: ('a, 'i, 'j, 'd, 'e) t
-> f:('a -> ('b, 'j, 'k, 'd, 'e) t)
-> ('b, 'i, 'k, 'd, 'e) t
val map : ('a, 'i, 'j, 'd, 'e) t -> f:('a -> 'b) -> ('b, 'i, 'j, 'd, 'e) t
val return : 'a -> ('a, 'i, 'i, 'd, 'e) t
end)
(M : sig
type ('a, 'i, 'j, 'd, 'e) t
val to_monad : ('a, 'i, 'j, 'd, 'e) t -> ('a, 'i, 'j, 'd, 'e) Monad.t
val of_monad : ('a, 'i, 'j, 'd, 'e) Monad.t -> ('a, 'i, 'j, 'd, 'e) t
end) =
Make_general (struct
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'i, 'j, 'd, 'e) M.t
let return a = M.of_monad (Monad.return a)
let bind t ~f = M.of_monad (Monad.bind (M.to_monad t) ~f:(fun a -> M.to_monad (f a)))
let map = `Custom (fun t ~f -> M.of_monad (Monad.map (M.to_monad t) ~f))
end)
module Of_monad_indexed
(Monad : S_indexed)
(M : sig
type ('a, 'i, 'j) t
val to_monad : ('a, 'i, 'j) t -> ('a, 'i, 'j) Monad.t
val of_monad : ('a, 'i, 'j) Monad.t -> ('a, 'i, 'j) t
end) =
Of_monad_general
(struct
include Monad
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'i, 'j) Monad.t
end)
(struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'i, 'j) M.t
end)
module Of_monad3
(Monad : S3)
(M : sig
type ('a, 'b, 'c) t
val to_monad : ('a, 'b, 'c) t -> ('a, 'b, 'c) Monad.t
val of_monad : ('a, 'b, 'c) Monad.t -> ('a, 'b, 'c) t
end) =
Of_monad_general
(struct
include Monad
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd, 'e) Monad.t
end)
(struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd, 'e) M.t
end)
module Of_monad2
(Monad : S2)
(M : sig
type ('a, 'b) t
val to_monad : ('a, 'b) t -> ('a, 'b) Monad.t
val of_monad : ('a, 'b) Monad.t -> ('a, 'b) t
end) =
Of_monad_general
(struct
include Monad
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd) Monad.t
end)
(struct
include M
type ('a, 'i, 'j, 'd, 'e) t = ('a, 'd) M.t
end)
module Of_monad
(Monad : S)
(M : sig
type 'a t
val to_monad : 'a t -> 'a Monad.t
val of_monad : 'a Monad.t -> 'a t
end) =
Of_monad_general
(struct
include Monad
type ('a, 'i, 'j, 'd, 'e) t = 'a Monad.t
end)
(struct
include M
type ('a, 'i, 'j, 'd, 'e) t = 'a M.t
end)
module Ident = struct
type 'a t = 'a
let[@inline] bind a ~f = (f [@inlined hint]) a
let[@inline] map a ~f = (f [@inlined hint]) a
external return : ('a[@local_opt]) -> ('a[@local_opt]) = "%identity"
module Monad_infix = struct
let[@inline] ( >>| ) a f = map a ~f
let[@inline] ( >>= ) a f = bind a ~f
end
include Monad_infix
module Let_syntax = struct
let return = return
include Monad_infix
module Let_syntax = struct
let return = return
let bind = bind
let map = map
let[@inline] both a b = a, b
module Open_on_rhs = struct end
end
let return = return
end
external join : ('a[@local_opt]) -> ('a[@local_opt]) = "%identity"
external ignore_m : (_[@local_opt]) -> unit = "%ignore"
external all_unit : (unit list[@local_opt]) -> unit = "%ignore"
external all : ('a list[@local_opt]) -> ('a list[@local_opt]) = "%identity"
end