]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/lib/cps.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / helena / src / lib / cps.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 let err _ = assert false
13
14 let start x = x
15
16 let id f x = f x
17
18 let rec list_sub_strict f l1 l2 = match l1, l2 with
19    | _, []              -> f l1
20    | _ :: tl1, _ :: tl2 -> list_sub_strict f tl1 tl2
21    | _                  -> assert false
22
23 let rec list_fold f map a = function
24    | []       -> f a
25    | hd :: tl -> list_fold f map (map a hd) tl
26
27 (* this is not tail recursive *)
28 let rec list_fold_left f map a = function
29    | []       -> f a
30    | hd :: tl -> 
31       let f a = list_fold_left f map a tl in
32       map f a hd
33
34 (* this is not tail recursive *)
35 let rec list_rev_map_append f map ~tail = function
36       | []       -> f tail        
37       | hd :: tl ->
38          let f hd = list_rev_map_append f map ~tail:(hd :: tail) tl in
39          map f hd
40
41 (* this is not tail recursive *)
42 let rec list_forall2 f map l1 l2 = match l1, l2 with
43    | [], []                 -> f true
44    | hd1 :: tl1, hd2 :: tl2 ->
45       let f b = if b then list_forall2 f map tl1 tl2 else f false in
46       map f hd1 hd2
47    | _                      -> f false
48
49 let list_rev_append f =
50    list_rev_map_append f (fun f t -> f t)
51
52 let list_rev_map =
53    list_rev_map_append ~tail:[]
54
55 let list_rev =
56    list_rev_append ~tail:[]
57
58 let list_iter f map l =
59    let map f () x = map f x in
60    list_fold_left f map () l
61
62 (* this is not tail recursive *)
63 let rec list_fold_left2 f map a l1 l2 = match l1, l2 with
64    | [], []                 -> f a
65    | hd1 :: tl1, hd2 :: tl2 -> 
66       let f a = list_fold_left2 f map a tl1 tl2 in
67       map f a hd1 hd2
68    | _                      -> assert false
69
70 let list_iter2 f map l1 l2 =
71    let map f () x1 x2 = map f x1 x2 in
72    list_fold_left2 f map () l1 l2
73
74 let rec list_fold_right f map l a = match l with
75    | []       -> f a
76    | hd :: tl -> list_fold_right (map f hd) map tl a
77
78 let rec list_fold_right2 f map l1 l2 a = match l1, l2 with
79    | [], []                 -> f a
80    | hd1 :: tl1, hd2 :: tl2 -> list_fold_right2 (map f hd1 hd2) map tl1 tl2 a
81    | _                      -> failwith "Cps.list_fold_right2"
82
83 let list_map f map l =
84    let map f hd a = 
85       let f hd = f (hd :: a) in map f hd
86    in
87    list_fold_right f map l []
88
89 let rec list_mem ?(eq=(=)) a = function
90    | []                   -> false
91    | hd :: _ when eq a hd -> true
92    | _ :: tl              -> list_mem ~eq a tl