package seqes

  1. Overview
  2. Docs

Source file monadic2.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*                 Simon Cruanes                                          *)
(*                                                                        *)
(*   Copyright 2017 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*                 Raphaël Proust                                         *)
(*                                                                        *)
(*   Copyright 2022 Nomadic Labs                                          *)
(*   Copyright 2025 Raphaël Proust                                        *)
(*                                                                        *)
(*   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.          *)
(*                                                                        *)
(**************************************************************************)

(* This implementation is a refactoring and functorisation of the original code
   present in the OCaml {!Stdlib.Seq} module. *)

module type S2 = sig
  type ('a, 'e) mon
  type ('a, 'e) t = unit -> (('a, 'e) node, 'e) mon
  and ('a, 'e) node =
    | Nil
    | Cons of 'a * ('a, 'e) t

  include Sigs2.SEQMON2ALL
    with type ('a, 'e) mon := ('a, 'e) mon
    with type ('a, 'e) t := ('a, 'e) t

  module M
  : Sigs2.SEQMON2TRANSFORMERS
    with type ('a, 'e) mon := ('a, 'e) mon
    with type ('a, 'e) callermon := ('a, 'e) mon
    with type ('a, 'e) t := ('a, 'e) t

  module Make
    (Alt : Sigs2.MONAD2)
    (Glue : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) Alt.t
      with type ('a, 'e) f := ('a, 'e) mon
      with type ('a, 'e) ret := ('a, 'e) mon)
  : Sigs2.SEQMON2TRANSFORMERS
      with type ('a, 'e) mon := ('a, 'e) mon
      with type ('a, 'e) callermon := ('a, 'e) Alt.t
      with type ('a, 'e) t := ('a, 'e) t

  module MakeTraversors
    (Alt : Sigs2.MONAD2)
    (Ret : Sigs2.MONAD2)
    (GlueAlt : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) Alt.t
      with type ('a, 'e) f := ('a, 'e) Ret.t
      with type ('a, 'e) ret := ('a, 'e) Ret.t)
    (GlueMon : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) mon
      with type ('a, 'e) f := ('a, 'e) Ret.t
      with type ('a, 'e) ret := ('a, 'e) Ret.t)
  : Sigs2.SEQMON2TRAVERSORS
      with type ('a, 'e) mon := ('a, 'e) Ret.t
      with type ('a, 'e) callermon := ('a, 'e) Alt.t
      with type ('a, 'e) t := ('a, 'e) t
end

module Make2
  (Mon: Sigs2.MONAD2)
: S2
   with type ('a, 'e) mon := ('a, 'e) Mon.t
= struct

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

  type ('a, 'e) t = unit -> (('a, 'e) node, 'e) Mon.t
  and ('a, 'e) node =
    | Nil
    | Cons of 'a * ('a, 'e) t

  let is_empty xs =
    let* n = xs () in
    match n with
    | Nil ->
        return true
    | Cons (_, _) ->
        return false

  let uncons xs =
    let* n = xs () in
    match n with
    | Cons (x, xs) ->
        return (Some (x, xs))
    | Nil ->
        return None

  let rec length_aux accu xs =
    let* n = xs () in
    match n with
    | Nil ->
        return accu
    | Cons (_, xs) ->
        length_aux (accu + 1) xs
  let[@inline] length xs = length_aux 0 xs

  let empty () = return Nil

  let cons x next () = return (Cons (x, next))

  let rec append seq1 seq2 () =
    let* n = seq1 () in
    match n with
    | Nil -> seq2()
    | Cons (x, next) -> return (Cons (x, append next seq2))

  let rec repeat x () =
    return (Cons (x, repeat x))

  let rec cycle_nonempty xs () =
    append xs (cycle_nonempty xs) ()

  let cycle xs () =
    let* n = xs () in
    match n with
    | Nil ->
        return Nil
    | Cons (x, xs') ->
        return (Cons (x, append xs' (cycle_nonempty xs)))

  let rec take_aux n xs =
    if n = 0 then
      empty
    else
      fun () ->
        let* node = xs () in
        match node with
        | Nil ->
            return Nil
        | Cons (x, xs) ->
            return (Cons (x, take_aux (n-1) xs))
  let take n xs =
    if n < 0 then invalid_arg "Seq.take";
    take_aux n xs

  let rec force_drop n xs =
    let* node = xs () in
    match node with
    | Nil ->
        return Nil
    | Cons (_, xs) ->
        let n = n - 1 in
        if n = 0 then
          xs()
        else
          force_drop n xs

  let drop n xs =
    if n < 0 then invalid_arg "Seq.drop"
    else if n = 0 then
      xs
    else
      fun () ->
        force_drop n xs

  let rec memoize xs =
    Suspension.memoize (fun () ->
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          return (Cons (x, memoize xs))
    )

  let rec once xs =
    Suspension.once (fun () ->
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          return (Cons (x, once xs))
    )

  let rec map_fst xys () =
    let* n = xys () in
    match n with
    | Nil ->
        return Nil
    | Cons ((x, _), xys) ->
        return (Cons (x, map_fst xys))

  let rec map_snd xys () =
    let* n = xys () in
    match n with
    | Nil ->
        return Nil
    | Cons ((_, y), xys) ->
        return (Cons (y, map_snd xys))

  let unzip xys =
    map_fst xys, map_snd xys

  let split =
    unzip

  let rec filter_map_beeg f seq () =
    let* n = seq () in
    match n with
    | Nil -> return Nil
    | Cons (x, next) ->
        let* o = f x in
        match o with
        | None -> filter_map_beeg f next ()
        | Some y ->  return (Cons (y, filter_map_beeg f next))

  let peel xss =
    unzip (filter_map_beeg uncons xss)

  let rec transpose xss () =
    let heads, tails = peel xss in
    let* b = is_empty heads in
    if b then begin
      return Nil
    end
    else
      return (Cons (heads, transpose tails))

  let rec concat seq () =
    let* n = seq () in
    match n with
    | Nil -> return Nil
    | Cons (x, next) ->
       append x (concat next) ()

  let rec zip xs ys () =
    let* n = xs () in
    match n with
    | Nil ->
        return Nil
    | Cons (x, xs) ->
        let* n = ys () in
        match n with
        | Nil ->
            return Nil
        | Cons (y, ys) ->
            return (Cons ((x, y), zip xs ys))

  let rec interleave xs ys () =
    let* n = xs () in
    match n with
    | Nil ->
        ys()
    | Cons (x, xs) ->
        return (Cons (x, interleave ys xs))

  let to_dispenser xs =
    let s = ref xs in
    fun () ->
      let* n = (!s) () in
      match n with
      | Nil ->
          return None
      | Cons (x, xs) ->
          s := xs;
          return (Some x)

  let rec ints i () =
    return (Cons (i, ints (i + 1)))

  let rec of_seq s () =
    match s () with
    | Seq.Nil ->  return Nil
    | Seq.Cons (x, s) -> return (Cons (x, of_seq s))

  module MakeTraversors
    (Alt : Sigs2.MONAD2)
    (Ret : Sigs2.MONAD2)
    (GlueAlt : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) Alt.t
      with type ('a, 'e) f := ('a, 'e) Ret.t
      with type ('a, 'e) ret := ('a, 'e) Ret.t)
    (GlueMon : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) Mon.t
      with type ('a, 'e) f := ('a, 'e) Ret.t
      with type ('a, 'e) ret := ('a, 'e) Ret.t)
  : Sigs2.SEQMON2TRAVERSORS
      with type ('a, 'e) mon := ('a, 'e) Ret.t
      with type ('a, 'e) callermon := ('a, 'e) Alt.t
      with type ('a, 'e) t := ('a, 'e) t
  = struct

    let return = Ret.return
    let ( let* ) = GlueMon.bind
    let ( let** ) = GlueAlt.bind

    let rec equal eq xs ys =
      (* TODO: support ( and* ) with Beeg.join *)
      let* xs = xs () in
      let* ys = ys () in
      match xs, ys with
      | Nil, Nil ->
          return true
      | Cons (x, xs), Cons (y, ys) ->
          let** e = eq x y in
          if e then
            equal eq xs ys
          else
            return false
      | Nil, Cons (_, _)
      | Cons (_, _), Nil ->
          return false

    let rec compare cmp xs ys =
      let* xs = xs () in
      let* ys = ys () in
      match xs, ys with
      | Nil, Nil ->
          return 0
      | Cons (x, xs), Cons (y, ys) ->
          let** c = cmp x y in
          if c <> 0 then return c else compare cmp xs ys
      | Nil, Cons (_, _) ->
          return (-1)
      | Cons (_, _), Nil ->
          return (+1)

    let rec fold_left f acc seq =
      let* n = seq () in
      match n with
      | 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
      | 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
      | 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
      | 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
      | 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
      | 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
      | 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
      | 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
      | Nil ->
          return ()
      | Cons (x, xs) ->
          let* n = ys () in
          match n with
          | 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
      | Nil ->
          return accu
      | Cons (x, xs) ->
          let* n = ys () in
          match n with
          | 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
      | Nil ->
          return true
      | Cons (x, xs) ->
          let* n = ys () in
          match n with
          | 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
      | Nil ->
          return false
      | Cons (x, xs) ->
          let* n = ys () in
          match n with
          | Nil ->
              return false
          | Cons (y, ys) ->
              let** b = f x y in
              if b then
                return true
              else
                exists2 f xs ys

  end

  module Make
    (Alt : Sigs2.MONAD2)
    (Glue : Sigs2.GLUE2
      with type ('a, 'e) x := ('a, 'e) Alt.t
      with type ('a, 'e) f := ('a, 'e) Mon.t
      with type ('a, 'e) ret := ('a, 'e) Mon.t)
  : Sigs2.SEQMON2TRANSFORMERS
      with type ('a, 'e) mon := ('a, 'e) Mon.t
      with type ('a, 'e) callermon := ('a, 'e) Alt.t
      with type ('a, 'e) t := ('a, 'e) t
  = struct

    include MakeTraversors (Alt)(Mon)(Glue)(Mon)

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

    (* we define return at the end of the file to avoid shadowing the monad *)

    let rec map f seq () =
      let* n = seq () in
      match n with
      | Nil -> return Nil
      | Cons (x, next) ->
          let** y = f x in
          return (Cons (y, map f next))

    let rec flat_map f seq () =
      let* n = seq () in
      match n with
      | Nil -> return Nil
      | Cons (x, next) ->
          let** y = f x in
        append y (flat_map f next) ()

    let concat_map = flat_map

    let rec filter_map f seq () =
      let* n = seq () in
      match n with
      | Nil -> return Nil
      | Cons (x, next) ->
          let** o = f x in
          match o with
          | None -> filter_map f next ()
          | Some y ->  return (Cons (y, filter_map f next))

    let rec filter f seq () =
      let* n = seq () in
      match n with
      | Nil -> return Nil
      | Cons (x, next) ->
          let** b = f x in
          if b
          then return (Cons (x, filter f next))
          else filter f next ()

    let rec unfold f u () =
      let** o = f u in
      match o with
      | None -> return Nil
      | Some (x, u') -> return (Cons (x, unfold f u'))

    (* [init_aux f i j] is the sequence [f i, ..., f (j-1)]. *)

    let rec init_aux f i j () =
      if i < j then begin
        let** fi = f i in
        return (Cons (fi, init_aux f (i + 1) j))
      end
      else
        return Nil

    let init n f =
      if n < 0 then
        invalid_arg "Seq.init"
      else
        init_aux f 0 n

    let rec forever f () =
      let** x = f () in
      return (Cons (x, forever f))

    let rec iterate1 f x () =
      let** y = f x in
      return (Cons (y, iterate1 f y))

    let iterate f x =
      cons x (iterate1 f x)

    let rec mapi_aux f i xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let** y = f i x in
          return (Cons (y, mapi_aux f (i+1) xs))

    let[@inline] mapi f xs = mapi_aux f 0 xs

    let rec tail_scan f s xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let** s = f s x in
          return (Cons (s, tail_scan f s xs))

    let scan f s xs =
      cons s (tail_scan f s xs)

    let rec sorted_merge1l cmp x xs ys () =
      let* n = ys () in
      match n with
      | Nil ->
          return (Cons (x, xs))
      | Cons (y, ys) ->
          sorted_merge1 cmp x xs y ys

    and sorted_merge1r cmp xs y ys () =
      let* n = xs () in
      match n with
      | Nil ->
          return (Cons (y, ys))
      | Cons (x, xs) ->
          sorted_merge1 cmp x xs y ys

    and sorted_merge1 cmp x xs y ys =
      let** c = cmp x y in
      if c <= 0 then
        return (Cons (x, sorted_merge1r cmp xs y ys))
      else
        return (Cons (y, sorted_merge1l cmp x xs ys))

    let sorted_merge cmp xs ys () =
      let* xs = xs () in
      let* ys = ys () in
      match xs, ys with
        | Nil, Nil ->
            return Nil
        | Nil, c
        | c, Nil ->
            return c
        | Cons (x, xs), Cons (y, ys) ->
            sorted_merge1 cmp x xs y ys

    let rec take_while p xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let** b = p x in
          if b then return (Cons (x, take_while p xs)) else return Nil

    let rec drop_while p xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) as node ->
          let** b = p x in
          if b then drop_while p xs () else return node

    let rec group eq xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          return (Cons (cons x (take_while (eq x) xs), group eq (drop_while (eq x) xs)))

    let rec map2 f xs ys () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let* n = ys () in
          match n with
          | Nil ->
              return Nil
          | Cons (y, ys) ->
              let** z = f x y in
              return (Cons (z, map2 f xs ys))


    let rec filter_map_find_left_map f xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let** fx = f x in
          match fx with
          | Either.Left y ->
              return (Cons (y, filter_map_find_left_map f xs))
          | Either.Right _ ->
              filter_map_find_left_map f xs ()

    let rec filter_map_find_right_map f xs () =
      let* n = xs () in
      match n with
      | Nil ->
          return Nil
      | Cons (x, xs) ->
          let** fx = f x in
          match fx with
          | Either.Left _ ->
              filter_map_find_right_map f xs ()
          | Either.Right z ->
              return (Cons (z, filter_map_find_right_map f xs))

    let partition_map f xs =
      filter_map_find_left_map f xs,
      filter_map_find_right_map f xs

    let partition p xs =
      filter p xs, filter (fun x -> Alt.bind (p x) (fun b -> Alt.return (not b))) xs

    let rec diagonals remainders xss () =
      let* n = xss () in
      match n with
      | Cons (xs, xss) ->
          begin
            let* n = xs () in
            match n with
          | Cons (x, xs) ->
              let heads, tails = peel remainders in
              return (Cons (cons x heads, diagonals (cons xs tails) xss))
          | Nil ->
              let heads, tails = peel remainders in
              return (Cons (heads, diagonals tails xss))
          end
      | Nil ->
          transpose remainders ()

    let diagonals xss =
      diagonals empty xss

    let map_product f xs ys =
      concat (diagonals (
        map
          (fun x ->
            Alt.return (fun () ->
            map
              (fun y -> f x y)
              ys
              ()
            )
          )
          xs
      ))

    let of_dispenser it =
      let rec c () =
        let** o = it () in
        match o with
        | None ->
            return Nil
        | Some x ->
            return (Cons (x, c))
      in
      c

  end

  include Make (Identity2) (Identity2)

  module M
  : Sigs2.SEQMON2TRANSFORMERS
      with type ('a, 'e) mon := ('a, 'e) Mon.t
      with type ('a, 'e) callermon := ('a, 'e) Mon.t
      with type ('a, 'e) t := ('a, 'e) t
  = Make(Mon)(Mon)

  (* defined here to avoid duplicating the code of [map_product] defined by
     [MakeTraversors(..)]'s inclusion *)
  let product xs ys =
    map_product (fun x y -> (x, y)) xs ys

  let return x () = return (Cons (x, empty))
end
OCaml

Innovation. Community. Security.