Source file applicative_intf.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
(** Applicatives model computations in which values computed by subcomputations cannot
affect what subsequent computations will take place.
Relative to monads, this restriction takes power away from the user of the interface
and gives it to the implementation. In particular, because the structure of the entire
computation is known, one can augment its definition with some description of that
structure.
For more information, see:
{v
Applicative Programming with Effects.
Conor McBride and Ross Paterson.
Journal of Functional Programming 18:1 (2008), pages 1-13.
http://staff.city.ac.uk/~ross/papers/Applicative.pdf
v} *)
open! Import
module Definitions = struct
[%%template
[@@@kind.default k = (value, value_or_null mod maybe_null)]
[@@@mode.default m = (global, local), p = (nonportable, portable)]
(** Module types below provide both global and local versions. In OxCaml, the latter
accept local [~f] closures.
Both [local] and [portable] versions are designed with a strict data structure in
mind. The types won't make sense for Applicatives whose evaluation is delayed (e.g.
most monads, Command.Param, or anything like a database query applicative). *)
(** Applicative operations. An applicative abstracts the notion of computations whose
results can be combined. An ['a t] represents a computation returning ['a].
This module type subsumes the other [S*_kernel] module types below. It is extended
with infix operators in [S*] below. *)
module type S3_kernel = sig
(** Applicative type. ['a] is the result type. ['p] and ['q] are extra parameters
unchanged by applicative operations. *)
type ('a : k, 'p, 'q) t : k
(** Convert a value to a [t]. *)
val return : ('a : k) 'p 'q. 'a @ p -> ('a, 'p, 'q) t @ p
(** Applies the functions in one [t] to the values in another. Well-behaved
applicatives satisfy these "laws", using [<*>] as infix [apply]:
- [return Fn.id <*> t] is equivalent to [t]
- [return Fn.compose <*> tf <*> tg <*> tx] is equivalent to [tf <*> (tg <*> tx)]
- [return f <*> return x] is equivalent to [return (f x)]
- [tf <*> return x] is equivalent to [return (fun f -> f x) <*> tf] *)
val apply
: ('a : k) ('b : k) 'p 'q.
('a @ p -> 'b @ p, 'p, 'q) t @ p -> ('a, 'p, 'q) t @ p -> ('b, 'p, 'q) t @ p
(** Combines values in two [t]s as tuples. Using [<*>] as infix [apply], equivalent to
[return (fun a b -> a, b) <*> ta <*> tb]. *)
val both
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t @ p -> ('b, 'p, 'q) t @ p -> ('a * 'b, 'p, 'q) t @ p
(** Transforms the contents of a [t]. Using [<*>] as infix [apply], equivalent to
[return f <*> t]. *)
val map
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t @ p -> f:('a @ p -> 'b @ p) @ m -> ('b, 'p, 'q) t @ p
(** Combines the contents of two [t]s. Using [<*>] as infix [apply], equivalent to
[return f <*> ta <*> tb]. *)
val map2
: ('a : k) ('b : k) ('c : k) 'p 'q.
('a, 'p, 'q) t @ p
-> ('b, 'p, 'q) t @ p
-> f:('a @ p -> 'b @ p -> 'c @ p) @ m
-> ('c, 'p, 'q) t @ p
(** Combines the contents of three [t]s. Using [<*>] as infix [apply], equivalent to
[return f <*> ta <*> tb <*> tc]. *)
val map3
: ('a : k) ('b : k) ('c : k) ('d : k) 'p 'q.
('a, 'p, 'q) t @ p
-> ('b, 'p, 'q) t @ p
-> ('c, 'p, 'q) t @ p
-> f:('a @ p -> 'b @ p -> 'c @ p -> 'd @ p) @ m
-> ('d, 'p, 'q) t @ p
(** Combines a list of [t]. *)
val all : ('a : k) 'p 'q. ('a, 'p, 'q) t list @ p -> ('a list, 'p, 'q) t @ p
(** Combines a list of [t] whose contents are unimportant. *)
val all_unit : 'p 'q. (unit, 'p, 'q) t list @ p -> (unit, 'p, 'q) t @ p
end
(** Applicative operations for applicatives with two type parameters. *)
module type S2_kernel = sig
type ('a : k, 'p) t : k
include S3_kernel [@kind k] [@mode m p] with type ('a : k, 'p, _) t := ('a, 'p) t
end
(** Applicative operations for applicatives with one type parameter. *)
module type S_kernel = sig
type ('a : k) t : k
include S3_kernel [@kind k] [@mode m p] with type ('a : k, _, _) t := 'a t
end
(** Infix operators. This module type subsumes the other [Index*] types below. *)
module type Applicative_infix3 = sig
type ('a : k, 'p, 'q) t : k
val ( <*> )
: ('a : k) ('b : k) 'p 'q.
('a @ p -> 'b @ p, 'p, 'q) t @ p -> ('a, 'p, 'q) t @ p -> ('b, 'p, 'q) t @ p
val ( <* )
: ('a : k) 'p 'q.
('a, 'p, 'q) t @ p -> (unit, 'p, 'q) t @ p -> ('a, 'p, 'q) t @ p
val ( *> )
: ('a : k) 'p 'q.
(unit, 'p, 'q) t @ p -> ('a, 'p, 'q) t @ p -> ('a, 'p, 'q) t @ p
val ( >>| )
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t @ p -> ('a @ p -> 'b @ p) @ m -> ('b, 'p, 'q) t @ p
end
(** Infix operators for applicatives with two type parameters. *)
module type Applicative_infix2 = sig
type ('a : k, 'p) t : k
include
Applicative_infix3 [@kind k] [@mode m p] with type ('a : k, 'p, _) t := ('a, 'p) t
end
(** Infix operators for applicatives with one type parameter. *)
module type Applicative_infix = sig
type ('a : k) t : k
include Applicative_infix3 [@kind k] [@mode m p] with type ('a : k, _, _) t := 'a t
end
(** Complete applicative interface. Extends [_kernel] with infix operators.
This module type subsumes the other [S*] types below. *)
module type S3 = sig
type ('a : k, 'p, 'q) t : k
include S3_kernel [@kind k] [@mode m p] with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) t
include
Applicative_infix3
[@kind k] [@mode m p]
with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) t
module Applicative_infix :
Applicative_infix3
[@kind k] [@mode m p]
with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) t
end
(** Complete applicative interface with two type parameters. *)
module type S2 = sig
type ('a : k, 'p) t : k
include S3 [@kind k] [@mode m p] with type ('a : k, 'p, _) t := ('a, 'p) t
end
(** Complete applicative interface with one type parameter. *)
module type S = sig
type ('a : k) t : k
include S3 [@kind k] [@mode m p] with type ('a : k, _, _) t := 'a t
end
(** Supports [let%map] syntax. See [ppx_let] documentation.
This module type subsumes the other [Let_syntax*] module types below. *)
module type Let_syntax3 = sig
type ('a : k, 'p, 'q) t : k
module Open_on_rhs_intf : sig
module type S
end
module Let_syntax : sig
val return : ('a : k) 'p 'q. 'a -> ('a : k, 'p, 'q) t
include
Applicative_infix3
[@kind k] [@mode m]
with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) t
module Let_syntax : sig
val return : ('a : k) 'p 'q. 'a -> ('a, 'p, 'q) t
val map
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t -> f:('a -> 'b) @ m -> ('b, 'p, 'q) t
val both
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t -> ('b, 'p, 'q) t -> ('a * 'b, 'p, 'q) t
module Open_on_rhs : Open_on_rhs_intf.S
end
end
end
(** Supports [let%map] syntax for applicatives with two type parameters. *)
module type Let_syntax2 = sig
type ('a : k, 'p) t : k
include Let_syntax3 [@kind k] [@mode m] with type ('a : k, 'p, _) t := ('a, 'p) t
end
(** Supports [let%map] syntax for applicatives with one type parameter. *)
module type Let_syntax = sig
type ('a : k) t : k
include Let_syntax3 [@kind k] [@mode m] with type ('a : k, _, _) t := 'a t
end
(** [Lazy_applicative] is an applicative whose structure may be computed on-demand,
instead of being constructed up-front. This is useful when implementing traversals
over large data structures, where otherwise we have to pay O(n) up-front cost both
in time and in memory. *)
module type Lazy_applicative = sig
include S [@kind k] [@mode m]
val of_thunk : ('a : k). (unit -> 'a t) -> 'a t
end
(** Argument to [Make_let_syntax3].
This module type subsumes the other [For_let_syntax*] types below. *)
module type For_let_syntax3 = sig
type ('a : k, 'p, 'q) t : k
val return : ('a : k) 'p 'q. 'a -> ('a, 'p, 'q) t
val map
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t -> f:('a -> 'b) @ m -> ('b, 'p, 'q) t
val both
: ('a : k) ('b : k) 'p 'q.
('a, 'p, 'q) t -> ('b, 'p, 'q) t -> ('a * 'b, 'p, 'q) t
include
Applicative_infix3
[@kind k] [@mode m]
with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) t
end
(** Argument to [Make_let_syntax2]. *)
module type For_let_syntax2 = sig
type ('a : k, 'p) t : k
include For_let_syntax3 [@kind k] [@mode m] with type ('a : k, 'p, _) t := ('a, 'p) t
end
(** Argument to [Make_let_syntax]. *)
module type For_let_syntax = sig
type ('a : k) t : k
include For_let_syntax3 [@kind k] [@mode m] with type ('a : k, _, _) t := 'a t
end
(** Argument to [Make3_using_map2].
This module type subsumes the other [Make*_using_map2] types below. *)
module type Basic3_using_map2 = sig
type ('a : k, 'p, 'q) t : k
val return : ('a : k) 'p 'q. 'a -> ('a, 'p, 'q) t
val map2
: ('a : k) ('b : k) ('c : k) 'p 'q.
('a, 'p, 'q) t -> ('b, 'p, 'q) t -> f:('a -> 'b -> 'c) @ m -> ('c, 'p, 'q) t
(** [`Define_using_map2] defines [map] as [map2 (return ()) t ~f:(fun () x -> x)].
[`Custom] overrides the default.
Some functions returned by [Make*_using_map2] are defined in terms of [map].
Providing an efficient [map] improves them as well. *)
val map
: ('a : k) ('b : k) 'p 'q.
[ `Define_using_map2
| `Custom of ('a, 'p, 'q) t -> f:('a -> 'b) @ m -> ('b, 'p, 'q) t
]
end
(** Argument to [Make_using_map2]. *)
module type Basic_using_map2 = sig
type ('a : k) t : k
include Basic3_using_map2 [@kind k] [@mode m] with type ('a : k, _, _) t := 'a t
end
(** Argument to [Make2_using_map2]. *)
module type Basic2_using_map2 = sig
type ('a : k, 'p) t : k
include
Basic3_using_map2 [@kind k] [@mode m] with type ('a : k, 'p, _) t := ('a, 'p) t
end]
[%%template
[@@@kind.default k = (value, value_or_null mod maybe_null)]
module type Basic3 = sig
type ('a : k, 'p, 'q) t : k
val return : ('a : k) 'p 'q. 'a -> ('a, 'p, 'q) t
val apply
: ('a : k) ('b : k) 'p 'q.
('a -> 'b, 'p, 'q) t -> ('a, 'p, 'q) t -> ('b, 'p, 'q) t
(** [`Define_using_map2] defines [map] as [apply (return f) t]. [`Custom] overrides
the default. Some functions returned by [Make*] are defined in terms of [map].
Providing an efficient [map] improves them as well. *)
val map
: ('a : k) ('b : k) 'p 'q.
[ `Define_using_apply
| `Custom of ('a, 'p, 'q) t -> f:('a -> 'b) -> ('b, 'p, 'q) t
]
end
(** Argument to [Make]. *)
module type Basic = sig
type ('a : k) t : k
include Basic3 [@kind k] with type ('a : k, _, _) t := 'a t
end
(** Argument to [Make2]. *)
module type Basic2 = sig
type ('a : k, 'p) t : k
include Basic3 [@kind k] with type ('a : k, 'p, _) t := ('a, 'p) t
end]
end
module type Applicative = sig @@ portable
include module type of struct
include Definitions
end
[%%template:
(** The identity applicative. Useful as an argument to functors that require a monad, to
produce a non-applicative result. *)
module%template [@mode p = (portable, nonportable)] Ident :
S
[@kind value_or_null mod maybe_null] [@mode local p]
with type ('a : value_or_null) t = 'a
[@@@kind.default k = (value, value_or_null mod maybe_null)]
(** Produces a monad with one type parameter. *)
module%template.portable Make (X : Basic [@kind k]) :
S [@kind k] with type ('a : k) t := 'a X.t
(** Produces a monad with two type parameters. *)
module%template.portable Make2 (X : Basic2 [@kind k]) :
S2 [@kind k] with type ('a : k, 'p) t := ('a, 'p) X.t
(** Produces a monad with three type parameters. *)
module%template.portable Make3 (X : Basic3 [@kind k]) :
S3 [@kind k] with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) X.t
[@@@mode.default m = (global, local)]
(** Produces a monad with one type parameter. *)
module%template.portable Make_using_map2 (X : Basic_using_map2 [@kind k] [@mode m]) :
S [@kind k] [@mode m] with type ('a : k) t := 'a X.t
(** Produces a monad with two type parameters. *)
module%template.portable Make2_using_map2 (X : Basic2_using_map2 [@kind k] [@mode m]) :
S2 [@kind k] [@mode m] with type ('a : k, 'p) t := ('a, 'p) X.t
(** Produces a monad with three type parameters. *)
module%template.portable Make3_using_map2 (X : Basic3_using_map2 [@kind k] [@mode m]) :
S3 [@kind k] [@mode m] with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) X.t
(** Converts a monad with one type parameter to an applicative. *)
module%template.portable Of_monad (M : Monad.S [@kind k] [@mode m]) :
S [@kind k] [@mode m] with type ('a : k) t := 'a M.t
(** Converts a monad with two type parameters to an applicative. *)
module%template.portable Of_monad2 (M : Monad.S2 [@kind k] [@mode m]) :
S2 [@kind k] [@mode m] with type ('a : k, 'p) t := ('a, 'p) M.t
(** Converts a monad with three type parameters to an applicative. *)
module%template.portable Of_monad3 (M : Monad.S3 [@kind k] [@mode m]) :
S3 [@kind k] [@mode m] with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) M.t
(** Composes two applicatives, one nested inside the other. *)
module%template.portable Compose
(Inner : S
[@kind k] [@mode m])
(Outer : S
[@kind k] [@mode m]) : S [@kind k] [@mode m] with type ('a : k) t = 'a Inner.t Outer.t
(** Combines two applicatives as a pair. *)
module%template.portable Pair
(Fst : S
[@kind k] [@mode m])
(Snd : S
[@kind k] [@mode m]) :
S [@kind k] [@mode m] with type ('a : k) t = 'a Fst.t * 'a Snd.t
[@@@modality.default p = (nonportable, portable)]
(** Constructs a [Let_syntax] module for an applicative with one type parameter. *)
module Make_let_syntax
(X : sig
@@ p
include For_let_syntax [@kind k] [@mode m]
end)
(Intf : sig
module type S
end)
(Impl : Intf.S) : sig @@ p
include Let_syntax [@kind k] [@mode m]
end
with type ('a : k) t := 'a X.t
with module Open_on_rhs_intf := Intf
(** Constructs a [Let_syntax] module for an applicative with two type parameters. *)
module Make_let_syntax2
(X : sig
@@ p
include For_let_syntax2 [@kind k] [@mode m]
end)
(Intf : sig
module type S
end)
(Impl : Intf.S) : sig @@ p
include Let_syntax2 [@kind k] [@mode m]
end
with type ('a : k, 'p) t := ('a, 'p) X.t
with module Open_on_rhs_intf := Intf
(** Constructs a [Let_syntax] module for an applicative with three type parameters. *)
module Make_let_syntax3
(X : sig
@@ p
include For_let_syntax3 [@kind k] [@mode m]
end)
(Intf : sig
module type S
end)
(Impl : Intf.S) : sig @@ p
include Let_syntax3 [@kind k] [@mode m]
end
with type ('a : k, 'p, 'q) t := ('a, 'p, 'q) X.t
with module Open_on_rhs_intf := Intf]
end