]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/util.ml
Code for critical eat reimplemented
[fireball-separation.git] / ocaml / util.ml
1 (* Function composition *)
2 let (++) f g x = f (g x);;
3
4 let findi p =
5  let rec aux n = function
6  | [] -> raise Not_found
7  | x::_ when p x -> n, x
8  | _::xs -> aux (n+1) xs
9  in aux 0
10 ;;
11
12 let find_alli p =
13  let rec aux n = function
14  | [] -> []
15  | x::xs when p x -> (n,x)::aux (n+1) xs
16  | _::xs -> aux (n+1) xs
17  in aux 0
18 ;;
19
20 let option_map f = function
21  | None -> None
22  | Some x -> Some (f x)
23 ;;
24
25 let option_get = function
26  | Some x -> x
27  | None -> failwith "option_get: None"
28 ;;
29
30 let rec find_opt f =
31  function
32     [] -> None
33   | x::xs ->
34      match f x with
35         None -> find_opt f xs
36       | Some _ as res -> res
37
38 let rec index_of ?(eq=(=)) x =
39  function
40     [] -> raise Not_found
41   | y::_ when eq x y -> 0
42   | _::l -> 1 + index_of ~eq x l
43
44 let index_of_opt ?eq l t =
45  try
46   Some (index_of ?eq t l)
47  with
48   Not_found -> None
49 ;;
50
51 let rec filter_map f =
52  function
53     [] -> []
54   | hd::tl ->
55      match f hd with
56         None -> filter_map f tl
57       | Some x -> x::filter_map f tl
58 ;;
59
60 (* the input must be sorted *)
61 let rec first_duplicate =
62  function
63     []
64   | [_] -> None
65   | x::y::_ when x=y -> Some x
66   | _::tl -> first_duplicate tl
67
68 (* the input must be sorted
69    output: list of non duplicates; list of duplicates *)
70 let rec split_duplicates =
71  function
72     [] -> [],[]
73   | [x] -> [x],[]
74   | x::(y::_ as tl) ->
75      let nondup,dup = split_duplicates tl in
76      if x = y then
77       List.filter ((<>) x) nondup, x::dup
78      else
79       x::nondup,dup
80
81 (* Non c'e' nella vecchia versione di OCaml :( *)
82 let uniq ?(compare=compare) =
83   let rec aux = function
84   | [] -> []
85   | [_] as ts -> ts
86   | t1 :: (t2 :: _ as ts) ->
87     if compare t1 t2 = 0 then aux ts else t1 :: (aux ts)
88   in aux
89
90 let sort_uniq ?(compare=compare) l = uniq ~compare (List.sort compare l)
91
92 let rec list_cut = function
93   | 0, lst -> [], lst
94   | n, x::xs -> let a, b = list_cut (n-1,xs) in x::a, b
95   | _ -> assert false
96 ;;
97
98 let concat_map f l = List.concat (List.map f l);;
99
100 let rec take n =
101  function
102  | [] -> assert (n = 0); []
103  | _ when n = 0 -> []
104  | x::xs -> x::(take (n-1) xs)
105 ;;
106
107 module Vars =
108 struct
109
110 let string_of_var v =
111   if v > 25
112      then "`" ^ string_of_int v
113      else String.make 1 (char_of_int (v + int_of_char 'a'))
114 ;;
115
116 let var_of_string s =
117  if String.length s <> 1 then (
118    if s.[0] = '`' then int_of_string (String.sub s 1 (-1 + String.length s)) else assert false
119  ) else int_of_char s.[0] - int_of_char 'a'
120
121 let print_name l n =
122  if n = -1
123   then "*"
124   else if n >= List.length l then "x" ^ string_of_int (List.length l - n - 1) else List.nth l n
125
126 end