Source file applicative.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
open! Import
include Applicative_intf
module List = List0
(** This module serves mostly as a partial check that [S2] and [S] are in sync, but
actually calling it is occasionally useful. *)
module S_to_S2 (X : S) : S2 with type ('a, 'e) t = 'a X.t = struct
include X
type ('a, 'e) t = 'a X.t
end
module S2_to_S (T : T.T) (X : S2) : S with type 'a t = ('a, T.t) X.t = struct
include X
type 'a t = ('a, T.t) X.t
end
module S2_to_S3 (X : S2) : S3 with type ('a, 'd, 'e) t = ('a, 'd) X.t = struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end
module S3_to_S2 (T : T.T) (X : S3) : S2 with type ('a, 'd) t = ('a, 'd, T.t) X.t = struct
include X
type ('a, 'd) t = ('a, 'd, T.t) X.t
end
module S3_to_S (T1 : T.T) (T2 : T.T) (X : S3) : S with type 'a t = ('a, T1.t, T2.t) X.t =
struct
include X
type 'a t = ('a, T1.t, T2.t) X.t
end
module Make3 (X : Basic3) : S3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t = struct
include X
let ( <*> ) = apply
let derived_map t ~f = return f <*> t
let map =
match X.map with
| `Define_using_apply -> derived_map
| `Custom x -> x
;;
let ( >>| ) t f = map t ~f
let map2 ta tb ~f = map ~f ta <*> tb
let map3 ta tb tc ~f = map ~f ta <*> tb <*> tc
let all ts = List.fold_right ts ~init:(return []) ~f:(map2 ~f:(fun x xs -> x :: xs))
let both ta tb = map2 ta tb ~f:(fun a b -> a, b)
let ( *> ) u v = return (fun () y -> y) <*> u <*> v
let ( <* ) u v = return (fun x () -> x) <*> u <*> v
let all_unit ts = List.fold ts ~init:(return ()) ~f:( *> )
module Applicative_infix = struct
let ( <*> ) = ( <*> )
let ( *> ) = ( *> )
let ( <* ) = ( <* )
let ( >>| ) = ( >>| )
end
end
module Make2 (X : Basic2) : S2 with type ('a, 'e) t := ('a, 'e) X.t = Make3 (struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end)
module Make (X : Basic) : S with type 'a t := 'a X.t = Make2 (struct
include X
type ('a, 'e) t = 'a X.t
end)
module Make_let_syntax3
(X : For_let_syntax3) (Intf : sig
module type S
end)
(Impl : Intf.S) =
struct
module Let_syntax = struct
include X
module Let_syntax = struct
include X
module Open_on_rhs = Impl
end
end
end
module Make_let_syntax2
(X : For_let_syntax2) (Intf : sig
module type S
end)
(Impl : Intf.S) =
Make_let_syntax3
(struct
include X
type ('a, 'd, _) t = ('a, 'd) X.t
end)
(Intf)
(Impl)
module Make_let_syntax
(X : For_let_syntax) (Intf : sig
module type S
end)
(Impl : Intf.S) =
Make_let_syntax2
(struct
include X
type ('a, _) t = 'a X.t
end)
(Intf)
(Impl)
(** This functor closely resembles [Make3], and indeed it could be implemented
much shorter in terms of [Make3]. However, we implement it by hand so that
the resulting functions are more efficient, e.g. using [map2] directly instead of
defining [apply] in terms of it and then [map2] in terms of that. For most
applicatives this does not matter, but for some (such as Bonsai.Value.t), it has a
larger impact. *)
module Make3_using_map2 (X : Basic3_using_map2) :
S3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t = struct
include X
let apply tf ta = map2 tf ta ~f:(fun f a -> f a)
let ( <*> ) = apply
let derived_map t ~f = return f <*> t
let map =
match X.map with
| `Define_using_map2 -> derived_map
| `Custom x -> x
;;
let ( >>| ) t f = map t ~f
let both ta tb = map2 ta tb ~f:(fun a b -> a, b)
let map3 ta tb tc ~f = map2 (map2 ta tb ~f) tc ~f:(fun fab c -> fab c)
let all ts = List.fold_right ts ~init:(return []) ~f:(map2 ~f:(fun x xs -> x :: xs))
let ( *> ) u v = map2 u v ~f:(fun () y -> y)
let ( <* ) u v = map2 u v ~f:(fun x () -> x)
let all_unit ts = List.fold ts ~init:(return ()) ~f:( *> )
module Applicative_infix = struct
let ( <*> ) = ( <*> )
let ( *> ) = ( *> )
let ( <* ) = ( <* )
let ( >>| ) = ( >>| )
end
end
module Make2_using_map2 (X : Basic2_using_map2) :
S2 with type ('a, 'e) t := ('a, 'e) X.t = Make3_using_map2 (struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end)
module Make_using_map2 (X : Basic_using_map2) : S with type 'a t := 'a X.t =
Make2_using_map2 (struct
include X
type ('a, 'e) t = 'a X.t
end)
module Make3_using_map2_local (X : Basic3_using_map2_local) :
S3_local with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t = struct
include X
let apply tf ta = map2 tf ta ~f:(fun f a -> f a)
let ( <*> ) = apply
let derived_map t ~f = map2 ~f:(fun () -> f) (return ()) t [@nontail]
let map =
match X.map with
| `Define_using_map2 -> derived_map
| `Custom map -> map
;;
let ( >>| ) t f = map t ~f
let both ta tb = map2 ta tb ~f:(fun a b -> a, b)
let map3 ta tb tc ~f =
let res = map2 (both ta tb) tc ~f:(fun (a, b) c -> f a b c) in
res
;;
let all ts = List.fold_right ts ~init:(return []) ~f:(map2 ~f:(fun x xs -> x :: xs))
let ( *> ) u v = map2 u v ~f:(fun () y -> y)
let ( <* ) u v = map2 u v ~f:(fun x () -> x)
let all_unit ts = List.fold ts ~init:(return ()) ~f:( *> )
module Applicative_infix = struct
let ( <*> ) = ( <*> )
let ( *> ) = ( *> )
let ( <* ) = ( <* )
let ( >>| ) = ( >>| )
end
end
module Make2_using_map2_local (X : Basic2_using_map2_local) :
S2_local with type ('a, 'e) t := ('a, 'e) X.t = Make3_using_map2_local (struct
include X
type ('a, 'd, 'e) t = ('a, 'd) X.t
end)
module Make_using_map2_local (X : Basic_using_map2_local) :
S_local with type 'a t := 'a X.t = Make2_using_map2_local (struct
include X
type ('a, 'e) t = 'a X.t
end)
module Of_monad2 (M : Monad.S2) : S2 with type ('a, 'e) t := ('a, 'e) M.t = Make2 (struct
type ('a, 'e) t = ('a, 'e) M.t
let return = M.return
let apply mf mx = M.bind mf ~f:(fun f -> M.map mx ~f)
let map = `Custom M.map
end)
module Of_monad (M : Monad.S) : S with type 'a t := 'a M.t = Of_monad2 (struct
include M
type ('a, _) t = 'a M.t
end)
module Compose (F : S) (G : S) : S with type 'a t = 'a F.t G.t = struct
type 'a t = 'a F.t G.t
include Make (struct
type nonrec 'a t = 'a t
let return a = G.return (F.return a)
let apply tf tx = G.apply (G.map ~f:F.apply tf) tx
let custom_map t ~f = G.map ~f:(F.map ~f) t
let map = `Custom custom_map
end)
end
module Pair (F : S) (G : S) : S with type 'a t = 'a F.t * 'a G.t = struct
type 'a t = 'a F.t * 'a G.t
include Make (struct
type nonrec 'a t = 'a t
let return a = F.return a, G.return a
let apply tf tx = F.apply (fst tf) (fst tx), G.apply (snd tf) (snd tx)
let custom_map t ~f = F.map ~f (fst t), G.map ~f (snd t)
let map = `Custom custom_map
end)
end