package seqes

  1. Overview
  2. Docs

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
(**************************************************************************)
(*                                                                        *)
(*                                 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.          *)
(*                                                                        *)
(**************************************************************************)


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 equal eq xs ys =
    (* TODO: support ( and* ) with Beeg.join *)
    let xs = xs () in
    let ys = ys () in
    match xs, ys with
    | Stdlib.Seq.Nil, Stdlib.Seq.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
    | Stdlib.Seq.Nil, Stdlib.Seq.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
    | 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 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
= Make2
  (struct
    type ('a, 'e) t = 'a Mon.t
    let return = Mon.return
    let bind = Mon.bind
  end)