Source file BwdNoLabels.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
type 'a t = 'a BwdDef.bwd =
  | Emp
  | Snoc of 'a t * 'a

let length xs =
  let rec go acc =
    function
    | Emp -> acc
    | Snoc (xs, _) -> (go[@tailcall]) (acc+1) xs
  in
  go 0 xs

let compare_lengths xs ys =
  let rec go =
    function
    | Emp, Emp -> 0
    | Snoc _, Emp -> 1
    | Emp, Snoc _ -> -1
    | Snoc (xs, _), Snoc (ys, _) ->
      (go[@tailcall]) (xs, ys)
  in go (xs, ys)

let compare_length_with xs len =
  let rec go len =
    function
    | Emp -> Int.compare 0 len
    | Snoc (xs, _) ->
      (go[@tailcall]) (len-1) xs
  in go len xs

let is_empty = function Emp -> true | _ -> false

let snoc l x = Snoc (l, x)

let nth xs i =
  if i < 0 then
    invalid_arg "Bwd.nth"
  else
    let rec go =
      function
      | Emp, _ -> failwith "Bwd.nth"
      | Snoc (_, x), 0 -> x
      | Snoc (xs, _), i -> (go[@tailcall]) (xs, i - 1)
    in go (xs, i)

let nth_opt xs i =
  if i < 0 then
    invalid_arg "Bwd.nth_opt"
  else
    let rec go =
      function
      | Emp, _ -> None
      | Snoc (_, x), 0 -> Some x
      | Snoc (xs, _), i -> (go[@tailcall]) (xs, i - 1)
    in go (xs, i)

let init len f =
  if len < 0 then invalid_arg "Bwd.init"
  else
    let[@tail_mod_cons] rec go i =
      if i >= len then Emp
      else
        let v = f i in
        Snoc ((go[@tailcall]) (i+1), v)
    in
    go 0

let append xs ys =
  let rec go =
    function
    | xs, [] -> xs
    | xs, y :: ys ->
      (go[@tailcall]) (Snoc (xs, y), ys)
  in go (xs, ys)

let prepend xs ys =
  let rec go =
    function
    | Emp, ys -> ys
    | Snoc (xs, x), ys ->
      (go[@tailcall]) (xs, x :: ys)
  in go (xs, ys)

let equal eq xs ys =
  let rec go =
    function
    | Emp, Emp -> true
    | Snoc (xs, x), Snoc (ys, y) ->
      eq x y && (go[@tailcall]) (xs, ys)
    | _ -> false
  in go (xs, ys)

let compare cmp xs ys =
  let rec go =
    function
    | Emp, Emp -> 0
    | Emp, _ -> -1
    | _, Emp -> 1
    | Snoc (xs, x), Snoc (ys, y) ->
      let c = cmp x y in
      if c <> 0 then c else (go[@tailcall]) (xs, ys)
  in go (xs, ys)

let iter f =
  let rec go =
    function
    | Emp -> ()
    | Snoc (xs, x) ->
      f x; (go[@tailcall]) xs
  in go

let iteri f =
  let[@tail_mod_cons] rec go i =
    function
    | Emp -> ()
    | Snoc (xs, x) ->
      f i x; (go[@tailcall]) (i + 1) xs
  in go 0

let map f =
  let[@tail_mod_cons] rec go =
    function
    | Emp -> Emp
    | Snoc (xs, x) -> let y = f x in Snoc ((go[@tailcall]) xs, y)
  in go

let mapi f =
  let[@tail_mod_cons] rec go i =
    function
    | Emp -> Emp
    | Snoc (xs, x) -> let y = f i x in Snoc ((go[@tailcall]) (i + 1) xs, y)
  in
  go 0

let filter_map f =
  let[@tail_mod_cons] rec go =
    function
    | Emp -> Emp
    | Snoc (xs, x) ->
      match f x with
      | None -> go xs
      | Some fx -> Snoc ((go[@tailcall]) xs, fx)
  in go

let fold_left f init =
  let rec go =
    function
    | Emp -> init
    | Snoc (xs, x) ->
      f (go xs) x
  in go

let fold_right_map f xs init =
  let rec go init =
    function
    | Emp -> init, Emp
    | Snoc (xs, x) ->
      let init, y = f x init in
      let z, ys = go init xs in
      z, Snoc (ys, y)
  in go init xs

let fold_right f xs init =
  let rec go init =
    function
    | Emp -> init
    | Snoc (xs, x) ->
      let init = f x init in
      (go[@tailcall]) init xs
  in go init xs

let iter2 f xs ys =
  let rec go =
    function
    | Emp, Emp -> ()
    | Snoc (xs, x), Snoc (ys, y) -> f x y; go (xs, ys)
    | _ -> invalid_arg "Bwd.iter2"
  in go (xs, ys)

let map2 f xs ys =
  let[@tail_mod_cons] rec go =
    function
    | Emp, Emp -> Emp
    | Snoc (xs, x), Snoc (ys, y) ->
      let z = f x y in Snoc ((go[@tailcall]) (xs, ys), z)
    | _ -> invalid_arg "Bwd.map2"
  in go (xs, ys)

let fold_left2 f init xs ys =
  let rec go =
    function
    | Emp, Emp -> init
    | Snoc (xs, x), Snoc (ys, y) ->
      f (go (xs, ys)) x y
    | _ -> invalid_arg "Bwd.fold_left2"
  in go (xs, ys)

let fold_right2 f xs ys init =
  let rec go init =
    function
    | Emp, Emp -> init
    | Snoc (xs, x), Snoc (ys, y) ->
      let init = f x y init in
      (go[@tailcall]) init (xs, ys)
    | _ -> invalid_arg "Bwd.fold_right2"
  in
  go init (xs, ys)

let for_all f =
  let rec go =
    function
    | Emp -> true
    | Snoc (xs, x) ->
      f x && (go[@tailcall]) xs
  in go

let for_all2 f xs ys =
  let rec go =
    function
    | Emp, Emp -> true
    | Snoc (xs, x), Snoc (ys, y) ->
      f x y && (go[@tailcall]) (xs, ys)
    | _ -> invalid_arg "Bwd.for_all2"
  in go (xs, ys)

let exists f =
  let rec go =
    function
    | Emp -> false
    | Snoc (xs, x) ->
      f x || (go[@tailcall]) xs
  in go

let exists2 f xs ys =
  let rec go =
    function
    | Emp, Emp -> false
    | Snoc (xs, x), Snoc (ys, y) ->
      f x y || (go[@tailcall]) (xs, ys)
    | _ -> invalid_arg "Bwd.exists2"
  in go (xs, ys)

let mem a set =
  let rec go =
    function
    | Emp -> false
    | Snoc (xs, x) ->
      a = x || (go[@tailcall]) xs
  in go set

let memq a set =
  let rec go =
    function
    | Emp -> false
    | Snoc (xs, x) ->
      a == x || (go[@tailcall]) xs
  in go set

let find f =
  let rec go =
    function
    | Emp -> raise Not_found
    | Snoc (xs, x) ->
      if f x then x else go xs
  in go

let find_opt f =
  let rec go =
    function
    | Emp -> None
    | Snoc (xs, x) ->
      if f x then Some x else go xs
  in go

let find_index f =
  let rec go i =
    function
    | Emp -> None
    | Snoc (xs, x) ->
      if f x then Some i else go (i+1) xs
  in go 0

let find_map f =
  let rec go =
    function
    | Emp -> None
    | Snoc (xs, x) ->
      match f x with
      | Some _ as r -> r
      | None -> go xs
  in go

let find_mapi f =
  let rec go i =
    function
    | Emp -> None
    | Snoc (xs, x) ->
      match f i x with
      | Some _ as r -> r
      | None -> go (i+1) xs
  in go 0

let filter f =
  let[@tail_mod_cons] rec go =
    function
    | Emp -> Emp
    | Snoc (xs, x) ->
      if f x then
        Snoc ((go[@tailcall]) xs, x)
      else
        (go[@tailcall]) xs
  in go

let find_all f = filter f

let filteri f =
  let[@tail_mod_cons] rec go i =
    function
    | Emp -> Emp
    | Snoc (xs, x) ->
      if f i x then
        Snoc ((go[@tailcall]) (i + 1) xs, x)
      else
        (go[@tailcall]) (i + 1) xs
  in go 0

let partition f =
  let rec go =
    function
    | Emp -> Emp, Emp
    | Snoc (xs, x) ->
      if f x then
        let ys, zs = go xs in
        Snoc (ys, x), zs
      else
        let ys, zs = go xs in
        ys, Snoc (zs, x)
  in go

let partition_map f =
  let rec go =
    function
    | Emp -> Emp, Emp
    | Snoc (xs, x) ->
      match f x with
      | Either.Left y ->
        let ys, zs = go xs in
        Snoc (ys, y), zs
      | Either.Right z ->
        let ys, zs = go xs in
        ys, Snoc (zs, z)
  in go

let rec split =
  function
  | Emp -> Emp, Emp
  | Snoc (xys, (x, y)) ->
    let xs, ys = split xys in
    Snoc (xs, x), Snoc (ys, y)

let combine xs ys =
  let[@tail_mod_cons] rec go =
    function
    | Emp, Emp -> Emp
    | Snoc (xs, x), Snoc (ys, y) ->
      Snoc (go (xs, ys), (x, y))
    | _ -> invalid_arg "Bwd.combine"
  in go (xs, ys)

let to_list xs =
  prepend xs []

let of_list xs =
  append Emp xs

module Infix =
struct
  let (<:) = snoc
  let (<@) = append
  let (@>) = prepend
  let (#<) = snoc
  let (<><) = append
  let (<>>) = prepend
end

module Notation = Infix