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
open! Import
include Applicative_intf

(** 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
  type ('a, 'e) t = 'a X.t

  include (X : S with type 'a t := 'a X.t)
end

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

  include (X : S2 with type ('a, 'e) t := ('a, 'e) X.t)
end

module S2_to_S3 (X : S2) : S3 with type ('a, 'd, 'e) t = ('a, 'd) X.t = struct
  type ('a, 'd, 'e) t = ('a, 'd) X.t

  include (X : S2 with type ('a, 'd) t := ('a, 'd) X.t)
end

module S3_to_S2 (X : S3) : S2 with type ('a, 'd) t = ('a, 'd, unit) X.t = struct
  type ('a, 'd) t = ('a, 'd, unit) X.t

  include (X : S3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t)
end

(* These functors serve only to check that the signatures for various Foo and Foo2 module
   types don't drift apart over time.
*)
module Check_compatibility = struct
  module Applicative_infix_to_Applicative_infix2 (X : Applicative_infix) :
    Applicative_infix2 with type ('a, 'e) t = 'a X.t = struct
    type ('a, 'e) t = 'a X.t

    include (X : Applicative_infix with type 'a t := 'a X.t)
  end

  module Applicative_infix2_to_Applicative_infix (X : Applicative_infix2) :
    Applicative_infix with type 'a t = ('a, unit) X.t = struct
    type 'a t = ('a, unit) X.t

    include (X : Applicative_infix2 with type ('a, 'e) t := ('a, 'e) X.t)
  end

  module Applicative_infix2_to_Applicative_infix3 (X : Applicative_infix2) :
    Applicative_infix3 with type ('a, 'd, 'e) t = ('a, 'd) X.t = struct
    type ('a, 'd, 'e) t = ('a, 'd) X.t

    include (X : Applicative_infix2 with type ('a, 'd) t := ('a, 'd) X.t)
  end

  module Applicative_infix3_to_Applicative_infix2 (X : Applicative_infix3) :
    Applicative_infix2 with type ('a, 'd) t = ('a, 'd, unit) X.t = struct
    type ('a, 'd) t = ('a, 'd, unit) X.t

    include (X : Applicative_infix3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t)
  end

  module Let_syntax_to_Let_syntax2 (X : Let_syntax) :
    Let_syntax2 with type ('a, 'e) t = 'a X.t = struct
    type ('a, 'e) t = 'a X.t

    include (X : Let_syntax with type 'a t := 'a X.t)
  end

  module Let_syntax2_to_Let_syntax (X : Let_syntax2) :
    Let_syntax with type 'a t = ('a, unit) X.t = struct
    type 'a t = ('a, unit) X.t

    include (X : Let_syntax2 with type ('a, 'e) t := ('a, 'e) X.t)
  end

  module Let_syntax2_to_Let_syntax3 (X : Let_syntax2) :
    Let_syntax3 with type ('a, 'd, 'e) t = ('a, 'd) X.t = struct
    type ('a, 'd, 'e) t = ('a, 'd) X.t

    include (X : Let_syntax2 with type ('a, 'd) t := ('a, 'd) X.t)
  end

  module Let_syntax3_to_Let_syntax2 (X : Let_syntax3) :
    Let_syntax2 with type ('a, 'd) t = ('a, 'd, unit) X.t = struct
    type ('a, 'd) t = ('a, 'd, unit) X.t

    include (X : Let_syntax3 with type ('a, 'd, 'e) t := ('a, 'd, 'e) X.t)
  end
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
    type ('a, 'd, 'e) t = ('a, 'd) X.t

    include (X : Basic2 with type ('a, 'e) t := ('a, 'e) X.t)
  end)

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

    include (X : Basic with type 'a 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
      type ('a, 'd, _) t = ('a, 'd) X.t

      include (X : For_let_syntax2 with type ('a, 'e) t := ('a, 'e) 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
      type ('a, _) t = 'a X.t

      include (X : For_let_syntax with type 'a t := 'a X.t)
    end)
    (Intf)
    (Impl)

module Make3_using_map2 (X : Basic3_using_map2) = Make3 (struct
    include X

    let apply tf tx = map2 tf tx ~f:(fun f x -> f x)

    let map =
      match map with
      | `Custom map -> `Custom map
      | `Define_using_map2 -> `Define_using_apply
    ;;
  end)

module Make2_using_map2 (X : Basic2_using_map2) :
  S2 with type ('a, 'e) t := ('a, 'e) X.t = Make3_using_map2 (struct
    type ('a, 'd, 'e) t = ('a, 'd) X.t

    include (X : Basic2_using_map2 with type ('a, 'e) t := ('a, 'e) X.t)
  end)

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

    include (X : Basic_using_map2 with type 'a 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
    type ('a, _) t = 'a M.t

    include (M : Monad.S with 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