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
(** 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 type Basic = sig
  type 'a t
  val return : 'a -> 'a t
  val apply : ('a -> 'b) t -> 'a t -> 'b t
  (** The following identities ought to hold for every Applicative (for some value of =):

      - identity:     [return Fn.id <*> t = t]
      - composition:  [return Fn.compose <*> tf <*> tg <*> tx = tf <*> (tg <*> tx)]
      - homomorphism: [return f <*> return x = return (f x)]
      - interchange:  [tf <*> return x = return (fun f -> f x) <*> tf]

      Note: <*> is the infix notation for apply. *)

  (** The [map] argument to [Applicative.Make] says how to implement the applicative's
      [map] function.  [`Define_using_apply] means to define [map t ~f = return f <*> t].
      [`Custom] overrides the default implementation, presumably with something more
      efficient.

      Some other functions returned by [Applicative.Make] are defined in terms of [map],
      so passing in a more efficient [map] will improve their efficiency as well. *)
  val map : [`Define_using_apply | `Custom of ('a t -> f:('a -> 'b) -> 'b t)]
end

module type Basic_using_map2 = sig
  type 'a t
  val return : 'a -> 'a t
  val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
  val map : [`Define_using_map2 | `Custom of ('a t -> f:('a -> 'b) -> 'b t)]
end

module type Applicative_infix = sig
  type 'a t

  val ( <*> ) : ('a -> 'b) t -> 'a t -> 'b t (** same as [apply] *)

  val ( <*  ) : 'a t -> unit t -> 'a t
  val (  *> ) : unit t -> 'a t -> 'a t

  val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t
end

module type For_let_syntax = sig
  type 'a t

  val return : 'a -> 'a t

  val map : 'a t -> f:('a -> 'b) -> 'b t

  val both : 'a t -> 'b t -> ('a * 'b) t

  include Applicative_infix with type 'a t := 'a t

end

module type S = sig

  include For_let_syntax

  val apply : ('a -> 'b) t -> 'a t -> 'b t

  val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t

  val map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t

  val all : 'a t list -> 'a list t

  val all_unit : unit t list -> unit t

  val all_ignore : unit t list -> unit t [@@deprecated "[since 2018-02] Use [all_unit]"]

  module Applicative_infix : Applicative_infix with type 'a t := 'a t
end

module type Let_syntax = sig
  type 'a t

  module Open_on_rhs_intf : sig
    module type S
  end

  module Let_syntax : sig

    val return : 'a -> 'a t
    include Applicative_infix with type 'a t := 'a t

    module Let_syntax : sig

      val return : 'a -> 'a t

      val map : 'a t -> f:('a -> 'b) -> 'b t

      val both : 'a t -> 'b t -> ('a * 'b) t

      module Open_on_rhs : Open_on_rhs_intf.S
    end
  end
end

(** Argument lists and associated N-ary map and apply functions. *)
module type Args = sig

  type 'a arg (** the underlying applicative *)

  (** ['f] is the type of a function that consumes the list of arguments and returns an
      ['r]. *)
  type ('f, 'r) t

  (** the empty argument list **)
  val nil : ('r, 'r) t

  (** prepend an argument *)
  val cons : 'a arg -> ('f, 'r) t -> ('a -> 'f, 'r) t

  (** infix operator for [cons] *)
  val (@>) : 'a arg -> ('f, 'r) t -> ('a -> 'f, 'r) t

  (** Transform argument values in some way.  For example, one can label a function
      argument like so:

      {[
        step ~f:(fun f x -> f ~foo:x) : ('a -> 'r1, 'r2) t -> (foo:'a -> 'r1, 'r2) t
      ]} *)
  val step : ('f1, 'r) t -> f:('f2 -> 'f1) -> ('f2, 'r) t

  (** The preferred way to factor out an [Args] sub-sequence:

      {[
        let args =
          Foo.Args.(
            bar "A"
            (* TODO: factor out the common baz qux sub-sequence *)
            @> baz "B"
            @> qux "C"
            @> zap "D"
            @> nil
          )
      ]}

      is to write a function that prepends the sub-sequence:

      {[
        let baz_qux remaining_args =
          Foo.Args.(
            baz "B"
            @> qux "C"
            @> remaining_args
          )
      ]}

      and splice it back into the original sequence using [@@] so that things line up
      nicely:

      {[
        let args =
          Foo.Args.(
            bar "A"
            @> baz_qux
            @@ zap "D"
            @> nil
          )
      ]} *)

  val mapN : f:'f -> ('f, 'r) t -> 'r arg

  val applyN : 'f arg -> ('f, 'r) t -> 'r arg

end
[@@deprecated "[since 2018-09] Use [ppx_let] instead."]

module type Basic2 = sig
  type ('a, 'e) t
  val return : 'a -> ('a, _) t
  val apply : ('a -> 'b, 'e) t -> ('a, 'e) t -> ('b, 'e) t
  val map : [`Define_using_apply | `Custom of (('a, 'e) t -> f:('a -> 'b) -> ('b, 'e) t)]
end

module type Basic2_using_map2 = sig
  type ('a, 'e) t
  val return : 'a -> ('a, _) t
  val map2 : ('a, 'e) t -> ('b, 'e) t -> f:('a -> 'b -> 'c) -> ('c, 'e) t
  val map : [`Define_using_map2 | `Custom of (('a, 'e) t -> f:('a -> 'b) -> ('b, 'e) t)]
end

module type S2 = sig
  type ('a, 'e) t

  val return : 'a -> ('a, _) t

  val apply : ('a -> 'b, 'e) t -> ('a, 'e) t -> ('b, 'e) t

  val map : ('a, 'e) t -> f:('a -> 'b) -> ('b, 'e) t

  val map2 : ('a, 'e) t -> ('b, 'e) t -> f:('a -> 'b -> 'c) -> ('c, 'e) t

  val map3
    :  ('a, 'e) t
    -> ('b, 'e) t
    -> ('c, 'e) t
    -> f:('a -> 'b -> 'c -> 'd)
    -> ('d, 'e) t

  val all : ('a, 'e) t list -> ('a list, 'e) t

  val all_unit : (unit, 'e) t list -> (unit, 'e) t

  val all_ignore : (unit, 'e) t list -> (unit, 'e) t
  [@@deprecated "[since 2018-02] Use [all_unit]"]

  val both : ('a, 'e) t -> ('b, 'e) t -> ('a * 'b, 'e) t

  module Applicative_infix : sig
    val ( <*> ) : ('a -> 'b, 'e) t -> ('a, 'e) t -> ('b, 'e) t
    val ( <*  ) : ('a, 'e) t -> (unit, 'e) t -> ('a, 'e) t
    val (  *> ) : (unit, 'e) t -> ('a, 'e) t -> ('a, 'e) t
    val ( >>| ) : ('a, 'e) t -> ('a -> 'b) -> ('b, 'e) t
  end

  include module type of Applicative_infix
end

module type Args2 = sig
  type ('a, 'e) arg

  type ('f, 'r, 'e) t

  val nil : ('r, 'r, _) t

  val cons : ('a, 'e) arg -> ('f, 'r, 'e) t -> ('a -> 'f, 'r, 'e) t
  val (@>) : ('a, 'e) arg -> ('f, 'r, 'e) t -> ('a -> 'f, 'r, 'e) t

  val step : ('f1, 'r, 'e) t -> f:('f2 -> 'f1) -> ('f2, 'r, 'e) t

  val mapN : f:'f -> ('f, 'r, 'e) t -> ('r, 'e) arg
  val applyN : ('f, 'e) arg -> ('f, 'r, 'e) t -> ('r, 'e) arg
end
[@@deprecated "[since 2018-09] Use [ppx_let] instead."]

module type Applicative = sig

  module type Applicative_infix = Applicative_infix
  module type Args              = Args
  [@@warning "-3"]
  [@@deprecated "[since 2018-09] Use [ppx_let] instead."]
  module type Args2             = Args2
  [@@warning "-3"]
  [@@deprecated "[since 2018-09] Use [ppx_let] instead."]
  module type Basic             = Basic
  module type Basic2            = Basic2
  module type Basic2_using_map2 = Basic2_using_map2
  module type Basic_using_map2  = Basic_using_map2
  module type Let_syntax        = Let_syntax
  module type S                 = S
  module type S2                = S2

  module Args_to_Args2 (X : Args) :
    Args2
    with type ('a, 'e) arg = 'a X.arg
    with type ('f, 'r, 'e) t = ('f, 'r) X.t
      [@@warning "-3"]

  module S2_to_S (X : S2) : S with type 'a t = ('a, unit) X.t

  module S_to_S2 (X : S) : S2 with type ('a, 'e) t = 'a X.t

  module Make  (X : Basic ) : S  with type  'a      t :=  'a      X.t
  module Make2 (X : Basic2) : S2 with type ('a, 'e) t := ('a, 'e) X.t

  module Make_let_syntax
      (X : For_let_syntax)
      (Intf : sig module type S end)
      (Impl : Intf.S)
    : Let_syntax with type 'a t := 'a X.t
      with module Open_on_rhs_intf := Intf

  module Make_using_map2  (X : Basic_using_map2 ) : S  with type  'a      t :=  'a      X.t
  module Make2_using_map2 (X : Basic2_using_map2) : S2 with type ('a, 'e) t := ('a, 'e) X.t

  module Make_args  (X : S ) : Args  with type  'a      arg :=  'a      X.t [@@warning "-3"]
  [@@deprecated "[since 2018-09] Use [ppx_let] instead."]
  module Make_args2 (X : S2) : Args2 with type ('a, 'e) arg := ('a, 'e) X.t [@@warning "-3"]
  [@@deprecated "[since 2018-09] Use [ppx_let] instead."]

  (** The following functors give a sense of what Applicatives one can define.

      Of these, [Of_monad] is likely the most useful.  The others are mostly didactic. *)

  (** Every monad is Applicative via:

      {[
        let apply mf mx =
          mf >>= fun f ->
          mx >>| fun x ->
          f x
      ]} *)
  module Of_monad (M : Monad.S)   : S with type 'a t := 'a M.t
  module Compose  (F : S) (G : S) : S with type 'a t =  'a F.t G.t
  module Pair     (F : S) (G : S) : S with type 'a t =  'a F.t * 'a G.t

end