Source

ocaml-llists / lazy_list.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
(* Lazy list implementation
 *
 * Author: Vadim Shender
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)


type 'a node_t =
  Nil
| Cons of 'a * 'a t
and  'a t = 'a node_t lazy_t


external follow_forward : Obj.t -> 'a = "caml_lazy_follow_forward"
external make_forward : 'a -> 'a lazy_t = "caml_lazy_make_forward"


let lazy_apply f l =
  lazy (f (Lazy.force l))


let fold_left ?limit f init lst =
  let rec fold_left_inf accu rest =
    match Lazy.force rest with
      Nil         -> accu
    | Cons (h, t) -> fold_left_inf (f accu h) t
  and     fold_left_fin accu rest = function
      0 -> accu
    | n ->
        match Lazy.force rest with
          Nil         -> accu
        | Cons (h, t) -> fold_left_fin (f accu h) t (n - 1)

  in
  match limit with
    None   -> fold_left_inf init lst
  | Some n -> if n >= 0 then fold_left_fin init lst n
              else           invalid_arg "Lazy_list.fold_left"

let fold_right ?limit f lst init =
  let rec fold_right_inf rest =
    match Lazy.force rest with
      Nil         -> init
    | Cons (h, t) -> f h (fold_right_inf t)
  and     fold_right_fin rest = function
      0 -> init
    | n ->
        match Lazy.force rest with
          Nil         -> init
        | Cons (h, t) -> f h (fold_right_fin t (n - 1))
  in
  match limit with
    None   -> fold_right_inf lst
  | Some n -> if n >= 0 then fold_right_fin lst n
              else           invalid_arg "Lazy_list.fold_right"


let empty = lazy Nil

let singleton x = lazy (Cons (x, empty))


let cons h t =
  lazy (Cons (h, t))


let init ?limit ?(starti=0) f =
  let rec init_inf i =
    lazy (Cons (f i, init_inf (i + 1)) )
  and     init_fin i n =
    lazy ( if n = 0 then Nil
           else          Cons (f i, init_fin (i + 1) (n - 1)) )
  in
  match limit with
    None   -> init_inf starti
  | Some n -> if n >= 0 then init_fin starti n else invalid_arg "Lazy_list.init"

let make ?limit x =
  let rec make_inf () =
    lazy (Cons (x, make_inf ()))
  and     make_fin n =
    lazy ( if n = 0 then Nil
           else          Cons (x, make_fin (n - 1)) )
  in
  match limit with
    None   -> make_inf ()
  | Some n -> if n >= 0 then make_fin n else invalid_arg "Lazy_list.make"

let interval ?(step=1) a b =
  let cmp = if step > 0 then ( > ) else ( < ) in
  let rec interval' a =
    lazy ( if cmp a b then Nil
           else       Cons (a, interval' (a + step)) )
  in interval' a

let rec interval_inf ?(step=1) a =
  lazy ( Cons (a, interval_inf (a + step) ~step:step) )


let length ?limit lst =
  fold_left ?limit (fun n _ -> n + 1) 0 lst


let hd lst =
  match Lazy.force lst with
    Nil         -> failwith "Lazy_list.hd"
  | Cons (h, t) -> h

let tl lst =
  match Lazy.force lst with
    Nil         -> failwith "Lazy_list.tl"
  | Cons (h, t) -> t

let nth lst n =
  let rec loop rest n =
    match Lazy.force rest with
      Nil         -> failwith "Lazy_list.nth"
    | Cons (h, t) ->
        if n = 0 then h
        else          loop t (n - 1)
  in if n >= 0 then loop lst n else invalid_arg "Lazy_list.nth"


let rec append lst1 lst2 =
  let append' = function
      Nil         -> Lazy.force lst2
    | Cons (h, t) -> Cons (h, append t lst2)
  in lazy_apply append' lst1

let concat lstlst =
  let rec concat_aux lst1 llrest =
    let concat' llrest = function
        Nil         ->
          ( match Lazy.force llrest with
              Nil                    -> Nil
            | Cons (lstnext, llrest) -> Lazy.force (concat_aux lstnext llrest) )
      | Cons (h, t) -> Cons (h, concat_aux t llrest)
    in lazy_apply (concat' llrest) lst1
  in
  match Lazy.force lstlst with
    Nil         -> empty
  | Cons (h, t) -> concat_aux h t

let flatten = concat


let iter ?limit f lst =
  let rec iter_inf lst =
    match Lazy.force lst with
      Nil         -> ()
    | Cons (h, t) -> let () = f h in iter_inf t
  and     iter_fin lst = function
      0 -> ()
    | n ->
        match Lazy.force lst with
          Nil         -> ()
        | Cons (h, t) -> let () = f h in iter_fin t (n - 1)
  in
  match limit with
    None   -> iter_inf lst
  | Some n -> if n >= 0 then iter_fin lst n else invalid_arg "Lazy_list.iter"

let iteri ?limit ?(starti=0) f lst =
  let rec iteri_inf lst i =
    match Lazy.force lst with
      Nil         -> ()
    | Cons (h, t) -> let () = f i h in iteri_inf t (i + 1)
  and     iteri_fin lst i = function
      0 -> ()
    | n ->
        match Lazy.force lst with
          Nil         -> ()
        | Cons (h, t) -> let () = f i h in iteri_fin t (i + 1) (n - 1)
  in
  match limit with
    None   -> iteri_inf lst starti
  | Some n -> if n >= 0 then iteri_fin lst starti n
              else           invalid_arg "Lazy_list.iteri"

let rec map f lst =
  let map' = function
      Nil         -> Nil
    | Cons (h, t) -> Cons (f h, map f t)
  in lazy_apply map' lst

let rec mapi ?(starti=0) f lst =
  let mapi' i = function
      Nil         -> Nil
    | Cons (h, t) -> Cons (f i h, mapi ~starti:(i + 1) f t)
  in lazy_apply (mapi' starti) lst


let iter2 ?limit f lst1 lst2 =
  let rec iter2_inf lst1 lst2 =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> ()
    | Cons (h1, t1), Cons (h2, t2) -> let () = f h1 h2 in iter2_inf t1 t2
    | _                            -> invalid_arg "Lazy_list.iter2"
  and     iter2_fin lst1 lst2 = function
      0 -> ()
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> ()
        | Cons (h1, t1), Cons (h2, t2) -> let () = f h1 h2 in iter2_fin t1 t2 (n - 1)
        | _                            -> invalid_arg "Lazy_list.iter2"
  in
  match limit with
    None   -> iter2_inf lst1 lst2
  | Some n -> if n >= 0 then iter2_fin lst1 lst2 n else invalid_arg "Lazy_list.iter2"

let iteri2 ?limit ?(starti=0) f lst1 lst2 =
  let rec iteri2_inf lst1 lst2 i =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> ()
    | Cons (h1, t1), Cons (h2, t2) -> let () = f i h1 h2 in iteri2_inf t1 t2 (i + 1)
    | _                            -> invalid_arg "Lazy_list.iteri2"
  and     iteri2_fin lst1 lst2 i = function
      0 -> ()
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> ()
        | Cons (h1, t1), Cons (h2, t2) -> let () = f i h1 h2 in iteri2_fin t1 t2 (i + 1) (n - 1)
        | _                            -> invalid_arg "Lazy_list.iteri2"
  in
  match limit with
    None   -> iteri2_inf lst1 lst2 starti
  | Some n -> if n >= 0 then iteri2_fin lst1 lst2 starti n
              else           invalid_arg "Lazy_list.iteri2"

let rec map2 f lst1 lst2 =
  let map2' lst1_unwrapped =
    match lst1_unwrapped, Lazy.force lst2 with
      Nil, Nil                     -> Nil
    | Cons (h1, t1), Cons (h2, t2) -> Cons (f h1 h2, map2 f t1 t2)
    | _                            -> invalid_arg "Lazy_list.map2"
  in lazy_apply map2' lst1

let rec mapi2 ?(starti=0) f lst1 lst2 =
  let mapi2' i lst1_unwrapped =
    match lst1_unwrapped, Lazy.force lst2 with
      Nil, Nil                     -> Nil
    | Cons (h1, t1), Cons (h2, t2) -> Cons (f i h1 h2, mapi2 ~starti:(i + 1) f t1 t2)
    | _                            -> invalid_arg "Lazy_list.mapi2"
  in lazy_apply (mapi2' starti) lst1

let fold_left2 ?limit f init lst1 lst2 =
  let rec fold_left2_inf accu lst1 lst2 =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> accu
    | Cons (h1, t1), Cons (h2, t2) -> fold_left2_inf (f accu h1 h2) t1 t2
    | _                            -> invalid_arg "Lazy_list.fold_left2"
  and     fold_left2_fin accu lst1 lst2 = function
      0 -> accu
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> accu
        | Cons (h1, t1), Cons (h2, t2) -> fold_left2_fin (f accu h1 h2) t1 t2 (n - 1)
        | _                            -> invalid_arg "Lazy_list.fold_left2"
  in
  match limit with
    None   -> fold_left2_inf init lst1 lst2
  | Some n -> if n >= 0 then fold_left2_fin init lst1 lst2 n
              else           invalid_arg "Lazy_list.fold_left2"

let rec fold_right2 ?limit f lst1 lst2 init =
  let rec fold_right2_inf lst1 lst2 =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> init
    | Cons (h1, t1), Cons (h2, t2) -> f h1 h2 (fold_right2_inf t1 t2)
    | _                            -> invalid_arg "Lazy_list.fold_right2"
  and     fold_right2_fin lst1 lst2 = function
      0 -> init
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> init
        | Cons (h1, t1), Cons (h2, t2) -> f h1 h2 (fold_right2_fin t1 t2 (n - 1))
        | _                            -> invalid_arg "Lazy_list.fold_right2"
  in
  match limit with
    None   -> fold_right2_inf lst1 lst2
  | Some n -> if n >= 0 then fold_right2_fin lst1 lst2 n
              else           invalid_arg "Lazy_list.fold_right2"


let for_all ?limit p lst =
  let rec for_all_inf lst =
    match Lazy.force lst with
      Nil         -> true
    | Cons (h, t) -> p h && for_all_inf t
  and     for_all_fin lst = function
      0 -> true
    | n ->
        match Lazy.force lst with
          Nil         -> true
        | Cons (h, t) -> p h && for_all_fin t (n - 1)
  in
  match limit with
    None   -> for_all_inf lst
  | Some n -> if n >= 0 then for_all_fin lst n else invalid_arg "Lazy_list.for_all"

let exists ?limit p lst =
  let rec exists_inf lst =
    match Lazy.force lst with
      Nil         -> false
    | Cons (h, t) -> p h || exists_inf t
  and     exists_fin lst = function
      0 -> false
    | n ->
        match Lazy.force lst with
          Nil         -> false
        | Cons (h, t) -> p h || exists_fin t (n - 1)
  in
  match limit with
    None   -> exists_inf lst
  | Some n -> if n >= 0 then exists_fin lst n else invalid_arg "Lazy_list.exists"

let for_all2 ?limit p lst1 lst2 =
  let rec for_all2_inf lst1 lst2 =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> true
    | Cons (h1, t1), Cons (h2, t2) -> p h1 h2 && for_all2_inf t1 t2
    | _                            -> invalid_arg "Lazy_list.for_all2"
  and     for_all2_fin lst1 lst2 = function
      0 -> true
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> true
        | Cons (h1, t1), Cons (h2, t2) -> p h1 h2 && for_all2_fin t1 t2 (n - 1)
        | _                            -> invalid_arg "Lazy_list.for_all2"
  in
  match limit with
    None   -> for_all2_inf lst1 lst2
  | Some n -> if n >= 0 then for_all2_fin lst1 lst2 n
              else           invalid_arg "Lazy_list.for_all2"

let exists2 ?limit p lst1 lst2 =
  let rec exists2_inf lst1 lst2 =
    match Lazy.force lst1, Lazy.force lst2 with
      Nil, Nil                     -> false
    | Cons (h1, t1), Cons (h2, t2) -> p h1 h2 || exists2_inf t1 t2
    | _                            -> invalid_arg "Lazy_list.exists2"
  and     exists2_fin lst1 lst2 = function
      0 -> false
    | n ->
        match Lazy.force lst1, Lazy.force lst2 with
          Nil, Nil                     -> false
        | Cons (h1, t1), Cons (h2, t2) -> p h1 h2 || exists2_fin t1 t2 (n - 1)
        | _                            -> invalid_arg "Lazy_list.exists2"
  in
  match limit with
    None   -> exists2_inf lst1 lst2
  | Some n -> if n >= 0 then exists2_fin lst1 lst2 n
              else           invalid_arg "Lazy_list.exists2"

let mem ?limit x lst =
  try exists ?limit ((=) x) lst with
    Invalid_argument _ -> invalid_arg "Lazy_list.mem"

let memq ?limit x lst =
  try exists ?limit ((==) x) lst with
    Invalid_argument _ -> invalid_arg "Lazy_list.memq"


let find ?limit p lst =
  let rec find_inf lst =
    match Lazy.force lst with
      Nil         -> raise Not_found
    | Cons (h, t) -> if p h then h else find_inf t
  and     find_fin lst = function
      0 -> raise Not_found
    | n ->
        match Lazy.force lst with
          Nil         -> raise Not_found
        | Cons (h, t) -> if p h then h else find_fin t (n - 1)
  in
  match limit with
    None   -> find_inf lst
  | Some n -> if n >= 0 then find_fin lst n else invalid_arg "Lazy_list.find"

let findi ?limit ?(starti=0) p lst =
  let rec findi_inf lst i =
    match Lazy.force lst with
      Nil         -> raise Not_found
    | Cons (h, t) -> if p i h then i, h else findi_inf t (i + 1)
  and     findi_fin lst i = function
      0 -> raise Not_found
    | n ->
        match Lazy.force lst with
          Nil         -> raise Not_found
        | Cons (h, t) -> if p i h then i, h else findi_fin t (i + 1) (n - 1)
  in
  match limit with
    None   -> findi_inf lst starti
  | Some n -> if n >= 0 then findi_fin lst starti n
              else           invalid_arg "Lazy_list.findi"

let rec find_all p lst =
  let rec find_all' = function
      Nil                  -> Nil
    | Cons (h, t) when p h -> Cons (h, find_all p t)
    | Cons (_, t)          -> find_all' (Lazy.force t)
  in lazy_apply find_all' lst

let filter = find_all

let partition p lst =
  find_all p lst, find_all (fun x -> not (p x)) lst


let split lst =
  let rec fst lst =
    let fst' = function 
        Nil              -> Nil
      | Cons ((h, _), t) -> Cons (h, fst t)
    in lazy_apply fst' lst
  and     snd lst =
    let snd' = function
        Nil              -> Nil
      | Cons ((_, h), t) -> Cons (h, snd t)
    in lazy_apply snd' lst
  in fst lst, snd lst

let rec combine lst1 lst2 =
  let combine' lst1_unwrapped =
    match lst1_unwrapped, Lazy.force lst2 with
      Nil, Nil                     -> Nil
    | Cons (h1, t1), Cons (h2, t2) -> Cons ((h1, h2), combine t1 t2)
    | _                            -> invalid_arg "Lazy_list.combine"
  in lazy_apply combine' lst1
            
        
let split_nth n lst =
  let rec take n lst =
    let take' = function
        Nil         -> invalid_arg "Lazy_list.split_nth"
      | Cons (h, t) -> Cons (h, take (n - 1) t)
    in if n = 0 then empty else lazy_apply take' lst
  and     drop n lst =
    let drop' = function
        Nil         -> invalid_arg "Lazy_list.split_nth"
      | Cons (_, t) -> Lazy.force (drop (n - 1) t)
    in if n = 0 then lst else lazy_apply drop' lst
  in
  if n >= 0 then take n lst, drop n lst else invalid_arg "Lazy_list.split_nth"

let rec take n lst =
  let take' = function
      Nil         -> Nil
    | Cons (h, t) -> Cons (h, take (n - 1) t)
  in if n > 0 then lazy_apply take' lst else empty

let rec drop n lst =
  let drop' = function
      Nil         -> Nil
    | Cons (_, t) -> Lazy.force (drop (n - 1) t)
  in if n > 0 then lazy_apply drop' lst else lst

let rec takewhile p lst =
  let takewhile' = function
      Nil         -> Nil
    | Cons (h, t) -> if p h then Cons (h, takewhile p t) else Nil
  in lazy_apply takewhile' lst

let rec dropwhile p lst =
  let dropwhile' = function
      Nil              -> Nil
    | Cons (h, t) as c -> if p h then Lazy.force (dropwhile p t) else c
  in lazy_apply dropwhile' lst


let rec of_list lst = 
  lazy ( match lst with
           []     -> Nil
         | h :: t -> Cons (h, of_list t) )

let to_list ?limit lst =
  try fold_right ?limit (fun x l -> x :: l) lst [] with
    Invalid_argument "Lazy_list.fold_right" -> invalid_arg "Lazy_list.to_list"


let rec of_stream str =
  lazy ( try let x = Stream.next str in Cons (x, of_stream str) with
           Stream.Failure -> Nil )

let rec to_stream lst =
  [< 'hd lst; to_stream (tl lst) >]


let rec of_channel ic =
  lazy ( try let x = input_byte ic in Cons (x, of_channel ic) with
           End_of_file -> Nil )

let to_channel ?limit lst oc =
  try iter ?limit (fun x -> output_byte oc x) lst with
    Invalid_argument "Lazy_list.iter" -> invalid_arg "Lazy_list.to_channel"