]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/lib/cps.ml
Additional contribs.
[helm.git] / helm / software / lambda-delta / 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 (* this is not tail recursive *)
24 let rec list_fold_left f map a = function
25    | []       -> f a
26    | hd :: tl -> 
27       let f a = list_fold_left f map a tl in
28       map f a hd
29
30 (* this is not tail recursive *)
31 let rec list_rev_map_append f map ~tail = function
32       | []       -> f tail        
33       | hd :: tl ->
34          let f hd = list_rev_map_append f map ~tail:(hd :: tail) tl in
35          map f hd
36
37 (* this is not tail recursive *)
38 let rec list_forall2 f map l1 l2 = match l1, l2 with
39    | [], []                 -> f true
40    | hd1 :: tl1, hd2 :: tl2 ->
41       let f b = if b then list_forall2 f map tl1 tl2 else f false in
42       map f hd1 hd2
43    | _                      -> f false
44
45 let list_rev_append f =
46    list_rev_map_append f (fun f t -> f t)
47
48 let list_rev_map =
49    list_rev_map_append ~tail:[]
50
51 let list_rev =
52    list_rev_append ~tail:[]
53
54 let list_iter f map l =
55    let map f () x = map f x in
56    list_fold_left f map () l
57
58 (* this is not tail recursive *)
59 let rec list_fold_left2 f map a l1 l2 = match l1, l2 with
60    | [], []                 -> f a
61    | hd1 :: tl1, hd2 :: tl2 -> 
62       let f a = list_fold_left2 f map a tl1 tl2 in
63       map f a hd1 hd2
64    | _                      -> assert false
65
66 let list_iter2 f map l1 l2 =
67    let map f () x1 x2 = map f x1 x2 in
68    list_fold_left2 f map () l1 l2
69
70 let rec list_fold_right f map l a = match l with
71    | []       -> f a
72    | hd :: tl -> list_fold_right (map f hd) map tl a
73
74 let list_map f map l =
75    let map f hd a = 
76       let f hd = f (hd :: a) in map f hd
77    in
78    list_fold_right f map l []
79
80 let rec list_mem ?(eq=(=)) a = function
81    | []                   -> false
82    | hd :: _ when eq a hd -> true
83    | _ :: tl              -> list_mem ~eq a tl