Source file standard.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
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*                 Simon Cruanes                                          *)
(*                                                                        *)
(*   Copyright 2017 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(** Monadic traversors over {!Stdlib.Seq}.

    This [Make] functor produces functions to traverse {!Stdlib.Seq}
    sequences, when the function provided by the user is within a monad.

    The [Make] functor does not export a new type: it only exports
    traversors for the existing type exported by the Stdlib. If you need tighter
    integration between your monad and the sequence, checkout
    {!Seqes.Monadic}.

    The buld of the documentation is located in the {!Seqes.Sigs1} module.
    Familiarity with the {!Stdlib.Seq} module is also assumed.
 *)
module Make1(Mon: Sigs1.MONAD1)
: Sigs1.SEQMON1TRAVERSORS
    with type 'a mon := 'a Mon.t
    with type 'a callermon := 'a Mon.t
    with type 'a t := 'a Stdlib.Seq.t
= struct

  let return = Mon.return
  let ( let* ) = Mon.bind

  let rec fold_left f acc seq =
    let n = seq () in
    match n with
    | Stdlib.Seq.Nil -> return acc
    | Cons (x, next) ->
        let* acc = f acc x in
        fold_left f acc next

  let rec iter f seq =
    let n = seq () in
    match n with
    | Stdlib.Seq.Nil -> return ()
    | Cons (x, next) ->
        let* () = f x in
        iter f next

  let rec iteri_aux f i xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return ()
    | Cons (x, xs) ->
        let* () = f i x in
        iteri_aux f (i+1) xs
  let[@inline] iteri f xs = iteri_aux f 0 xs

  let rec fold_lefti_aux f accu i xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return accu
    | Cons (x, xs) ->
        let* accu = f accu i x in
        fold_lefti_aux f accu (i+1) xs
  let[@inline] fold_lefti f accu xs = fold_lefti_aux f accu 0 xs

  let rec for_all p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return true
    | Cons (x, xs) ->
        let* b = p x in
        if b then
          for_all p xs
        else
          return false

  let rec exists p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return false
    | Cons (x, xs) ->
        let* b = p x in
        if b then
          return true
        else
          exists p xs

  let rec find p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return None
    | Cons (x, xs) ->
        let* b = p x in
        if b then return (Some x) else find p xs

  let rec find_map f xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return None
    | Cons (x, xs) ->
        let* o = f x in
        match o with
        | None ->
            find_map f xs
        | Some _ as result ->
            return result

  let rec iter2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return ()
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return ()
        | Cons (y, ys) ->
            let* () = f x y in
            iter2 f xs ys

  let rec fold_left2 f accu xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return accu
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return accu
        | Cons (y, ys) ->
            let* accu = f accu x y in
            fold_left2 f accu xs ys

  let rec for_all2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return true
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return true
        | Cons (y, ys) ->
            let* b = f x y in
            if b then
              for_all2 f xs ys
            else
              return false

  let rec exists2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return false
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return false
        | Cons (y, ys) ->
            let* b = f x y in
            if b then
              return true
            else
              exists2 f xs ys

end


module Make2(Mon: Sigs2.MONAD2)
: Sigs2.SEQMON2TRAVERSORS
    with type ('a, 'e) mon := ('a, 'e) Mon.t
    with type ('a, 'e) callermon := ('a, 'e) Mon.t
    with type ('a, 'e) t := 'a Stdlib.Seq.t
= struct

  let return = Mon.return
  let ( let* ) = Mon.bind

  let rec fold_left f acc seq =
    let n = seq () in
    match n with
    | Stdlib.Seq.Nil -> return acc
    | Cons (x, next) ->
        let* acc = f acc x in
        fold_left f acc next

  let rec iter f seq =
    let n = seq () in
    match n with
    | Stdlib.Seq.Nil -> return ()
    | Cons (x, next) ->
        let* () = f x in
        iter f next

  let rec iteri_aux f i xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return ()
    | Cons (x, xs) ->
        let* () = f i x in
        iteri_aux f (i+1) xs
  let[@inline] iteri f xs = iteri_aux f 0 xs

  let rec fold_lefti_aux f accu i xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return accu
    | Cons (x, xs) ->
        let* accu = f accu i x in
        fold_lefti_aux f accu (i+1) xs
  let[@inline] fold_lefti f accu xs = fold_lefti_aux f accu 0 xs

  let rec for_all p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return true
    | Cons (x, xs) ->
        let* b = p x in
        if b then
          for_all p xs
        else
          return false

  let rec exists p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return false
    | Cons (x, xs) ->
        let* b = p x in
        if b then
          return true
        else
          exists p xs

  let rec find p xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return None
    | Cons (x, xs) ->
        let* b = p x in
        if b then return (Some x) else find p xs

  let rec find_map f xs =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return None
    | Cons (x, xs) ->
        let* o = f x in
        match o with
        | None ->
            find_map f xs
        | Some _ as result ->
            return result

  let rec iter2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return ()
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return ()
        | Cons (y, ys) ->
            let* () = f x y in
            iter2 f xs ys

  let rec fold_left2 f accu xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return accu
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return accu
        | Cons (y, ys) ->
            let* accu = f accu x y in
            fold_left2 f accu xs ys

  let rec for_all2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return true
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return true
        | Cons (y, ys) ->
            let* b = f x y in
            if b then
              for_all2 f xs ys
            else
              return false

  let rec exists2 f xs ys =
    let n = xs () in
    match n with
    | Stdlib.Seq.Nil ->
        return false
    | Cons (x, xs) ->
        let n = ys () in
        match n with
        | Stdlib.Seq.Nil ->
            return false
        | Cons (y, ys) ->
            let* b = f x y in
            if b then
              return true
            else
              exists2 f xs ys

end