Source file 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
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 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

  include Make (struct
      type nonrec 'a t = 'a t

      let bind a ~f = f a
      let return a = a
      let map = `Custom (fun a ~f -> f a)
    end)
end