1 (* Copyright (C) 1998-2008 Yoann Padioleau
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * version 2.1 as published by the Free Software Foundation, with the
6 * special exception on linking described in file license.txt.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
11 * license.txt for more details.
14 (*****************************************************************************)
16 (*****************************************************************************)
20 (* ---------------------------------------------------------------------- *)
21 (* Maybe could split common.ml and use include tricks as in ofullcommon.ml or
22 * Jane Street core lib. But then harder to bundle simple scripts like my
23 * make_full_linux_kernel.ml because would then need to pass all the files
24 * either to ocamlc or either to some #load. Also as the code of many
25 * functions depends on other functions from this common, it would
26 * be tedious to add those dependencies. Here simpler (have just the
27 * pb of the Prelude, but it's a small problem).
29 * pixel means code from Pascal Rigaux
30 * julia means code from Julia Lawall
32 (* ---------------------------------------------------------------------- *)
34 (*****************************************************************************)
36 (*****************************************************************************)
39 * - Pervasives, of course
49 * - =, <=, max min, abs, ...
50 * - List.rev, List.mem, List.partition,
51 * - List.fold*, List.concat, ...
52 * - Str.global_replace
55 * The Format library allows to hide passing an indent_level variable.
56 * You use as usual the print_string function except that there is
57 * this automatic indent_level variable handled for you (and maybe
58 * more services). src: julia in coccinelle unparse_cocci.
70 * Many functions were inspired by Haskell or Lisp librairies.
73 (*****************************************************************************)
75 (*****************************************************************************)
77 (* The following functions should be in their respective sections but
78 * because some functions in some sections use functions in other
79 * sections, and because I don't want to take care of the order of
80 * those sections, of those dependencies, I put the functions causing
81 * dependency problem here. C is better than caml on this with the
82 * ability to declare prototype, enabling some form of forward
89 exception UnixExit
of int
91 let rec (do_n
: int -> (unit -> unit) -> unit) = fun i f
->
92 if i
= 0 then () else (f
(); do_n
(i
-1) f
)
93 let rec (foldn
: ('a
-> int -> 'a
) -> 'a
-> int -> 'a
) = fun f acc i
->
94 if i
= 0 then acc
else foldn f
(f acc i
) (i
-1)
96 let sum_int = List.fold_left
(+) 0
98 (* could really call it 'for' :) *)
99 let fold_left_with_index f acc
=
100 let rec fold_lwi_aux acc n
= function
102 | x
::xs
-> fold_lwi_aux (f acc x n
) (n
+1) xs
103 in fold_lwi_aux acc
0
109 | (_
,[]) -> failwith
"drop: not enough"
110 | (n
,x
::xs
) -> drop (n
-1) xs
112 let rec enum_orig x n
= if x
= n
then [n
] else x
::enum_orig (x
+1) n
116 then failwith
(Printf.sprintf
"bad values in enum, expect %d <= %d" x n
);
117 let rec enum_aux acc x n
=
118 if x
= n
then n
::acc
else enum_aux (x
::acc
) (x
+1) n
120 List.rev
(enum_aux [] x n
)
125 | (_
,[]) -> failwith
"take: not enough"
126 | (n
,x
::xs
) -> x
::take (n
-1) xs
129 let last_n n l
= List.rev
(take n
(List.rev l
))
130 let last l
= List.hd
(last_n 1 l
)
133 let (list_of_string
: string -> char list
) = function
135 | s
-> (enum 0 ((String.length s
) - 1) +> List.map
(String.get s
))
137 let (lines
: string -> string list
) = fun s
->
138 let rec lines_aux = function
140 | [x
] -> if x
= "" then [] else [x
]
144 Str.split_delim
(Str.regexp
"\n") s
+> lines_aux
154 let debugger = ref false
156 let unwind_protect f cleanup
=
157 if !debugger then f
() else
159 with e
-> begin cleanup e
; raise e
end
161 let finalize f cleanup
=
162 if !debugger then f
() else
171 let command2 s
= ignore
(Sys.command s
)
174 let (matched
: int -> string -> string) = fun i s
->
175 Str.matched_group i s
177 let matched1 = fun s
-> matched
1 s
178 let matched2 = fun s
-> (matched
1 s
, matched
2 s
)
179 let matched3 = fun s
-> (matched
1 s
, matched
2 s
, matched
3 s
)
180 let matched4 = fun s
-> (matched
1 s
, matched
2 s
, matched
3 s
, matched
4 s
)
181 let matched5 = fun s
-> (matched
1 s
, matched
2 s
, matched
3 s
, matched
4 s
, matched
5 s
)
182 let matched6 = fun s
-> (matched
1 s
, matched
2 s
, matched
3 s
, matched
4 s
, matched
5 s
, matched
6 s
)
183 let matched7 = fun s
-> (matched
1 s
, matched
2 s
, matched
3 s
, matched
4 s
, matched
5 s
, matched
6 s
, matched
7 s
)
185 let (with_open_stringbuf
: (((string -> unit) * Buffer.t
) -> unit) -> string) =
187 let buf = Buffer.create
1000 in
188 let pr s
= Buffer.add_string
buf (s ^
"\n") in
193 let foldl1 p
= function x
::xs
-> List.fold_left p x xs
| _
-> failwith
"foldl1"
195 (*****************************************************************************)
196 (* Debugging/logging *)
197 (*****************************************************************************)
199 (* I used this in coccinelle where the huge logging of stuff ask for
200 * a more organized solution that use more visual indentation hints.
202 * todo? could maybe use log4j instead ? or use Format module more
206 let _tab_level_print = ref 0
210 let _prefix_pr = ref ""
213 _tab_level_print := !_tab_level_print + _tab_indent;
215 (fun () -> _tab_level_print := !_tab_level_print - _tab_indent;)
219 print_string
!_prefix_pr;
220 do_n
!_tab_level_print (fun () -> print_string
" ");
226 print_string
!_prefix_pr;
227 do_n
!_tab_level_print (fun () -> print_string
" ");
233 prerr_string
!_prefix_pr;
234 do_n
!_tab_level_print (fun () -> prerr_string
" ");
240 prerr_string
!_prefix_pr;
241 do_n
!_tab_level_print (fun () -> prerr_string
" ");
245 let pr_xxxxxxxxxxxxxxxxx () =
246 pr "-----------------------------------------------------------------------"
248 let pr2_xxxxxxxxxxxxxxxxx () =
249 pr2 "-----------------------------------------------------------------------"
252 let reset_pr_indent () =
253 _tab_level_print := 0
256 * let pr s = (print_string s; print_string "\n"; flush stdout)
257 * let pr2 s = (prerr_string s; prerr_string "\n"; flush stderr)
260 (* ---------------------------------------------------------------------- *)
262 (* I can not use the _xxx ref tech that I use for common_extra.ml here because
263 * ocaml don't like the polymorphism of Dumper mixed with refs.
265 * let (_dump_func : ('a -> string) ref) = ref
266 * (fun x -> failwith "no dump yet, have you included common_extra.cmo?")
267 * let (dump : 'a -> string) = fun x ->
270 * So I have included directly dumper.ml in common.ml. It's more practical
271 * when want to give script that use my common.ml, I just have to give
275 (* start of dumper.ml *)
277 (* Dump an OCaml value into a printable string.
278 * By Richard W.M. Jones (rich@annexia.org).
279 * dumper.ml 1.2 2005/02/06 12:38:21 rich Exp
286 string_of_int
(magic r
: int)
288 let rec get_fields acc
= function
290 | n
-> let n = n-1 in get_fields (field r
n :: acc
) n
294 if (magic r
: int) = 0 then true (* [] *)
297 let s = size r
and t
= tag r
in
298 if t
= 0 && s = 2 then is_list (field r
1) (* h :: t *)
304 else let h = field r
0 and t
= get_list (field r
1) in h :: t
307 (* XXX In future, print the address of value 'r'. Not possible in
308 * pure OCaml at the moment.
313 let s = size r
and t
= tag r
in
315 (* From the tag, determine the type of block. *)
316 if is_list r
then ( (* List. *)
317 let fields = get_list r
in
318 "[" ^
String.concat
"; " (List.map
dump fields) ^
"]"
320 else if t
= 0 then ( (* Tuple, array, record. *)
321 let fields = get_fields [] s in
322 "(" ^
String.concat
", " (List.map
dump fields) ^
")"
325 (* Note that [lazy_tag .. forward_tag] are < no_scan_tag. Not
326 * clear if very large constructed values could have the same
328 else if t
= lazy_tag
then opaque "lazy"
329 else if t
= closure_tag
then opaque "closure"
330 else if t
= object_tag
then ( (* Object. *)
331 let fields = get_fields [] s in
332 let clasz, id
, slots
=
333 match fields with h::h'
::t
-> h, h'
, t
| _
-> assert false in
334 (* No information on decoding the class (first field). So just print
335 * out the ID and the slots.
337 "Object #" ^
dump id ^
338 " (" ^
String.concat
", " (List.map
dump slots
) ^
")"
340 else if t
= infix_tag
then opaque "infix"
341 else if t
= forward_tag
then opaque "forward"
343 else if t
< no_scan_tag
then ( (* Constructed value. *)
344 let fields = get_fields [] s in
345 "Tag" ^ string_of_int t ^
346 " (" ^
String.concat
", " (List.map
dump fields) ^
")"
348 else if t
= string_tag
then (
349 "\"" ^
String.escaped
(magic r
: string) ^
"\""
351 else if t
= double_tag
then (
352 string_of_float
(magic r
: float)
354 else if t
= abstract_tag
then opaque "abstract"
355 else if t
= custom_tag
then opaque "custom"
356 else if t
= final_tag
then opaque "final"
357 else failwith
("dump: impossible tag (" ^ string_of_int t ^
")")
360 let dump v
= dump (repr v
)
362 (* end of dumper.ml *)
365 let (dump : 'a -> string) = fun x ->
370 (* ---------------------------------------------------------------------- *)
371 let pr2_gen x
= pr2 (dump x
)
375 (* ---------------------------------------------------------------------- *)
378 let _already_printed = Hashtbl.create
101
379 let disable_pr2_once = ref false
381 if !disable_pr2_once then pr2 s
383 if not
(Hashtbl.mem
_already_printed s)
385 Hashtbl.add
_already_printed s true;
390 (* ---------------------------------------------------------------------- *)
391 (* could also be in File section *)
393 let redirect_stdout_stderr file f
=
395 let chan = open_out file
in
396 let descr = Unix.descr_of_out_channel
chan in
398 let saveout = Unix.dup
Unix.stdout
in
399 let saveerr = Unix.dup
Unix.stderr
in
400 Unix.dup2
descr Unix.stdout
;
401 Unix.dup2
descr Unix.stderr
;
402 flush stdout
; flush stderr
;
404 flush stdout
; flush stderr
;
405 Unix.dup2
saveout Unix.stdout
;
406 Unix.dup2
saveerr Unix.stderr
;
410 let redirect_stdin file f
=
412 let chan = open_in file
in
413 let descr = Unix.descr_of_in_channel
chan in
415 let savein = Unix.dup
Unix.stdin
in
416 Unix.dup2
descr Unix.stdin
;
418 Unix.dup2
savein Unix.stdin
;
422 let redirect_stdin_opt optfile f
=
425 | Some infile
-> redirect_stdin infile f
429 (* ---------------------------------------------------------------------- *)
433 (* cf common.mli, fprintf, printf, eprintf, sprintf.
434 * also what is this ?
435 * val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a
436 * val kprintf : (string -> 'a) -> ('b, unit, string, 'a) format4 -> 'b
446 (* ---------------------------------------------------------------------- *)
448 let _chan = ref stderr
449 let start_log_file () =
450 let filename = (spf "/tmp/debugml%d:%d" (Unix.getuid
()) (Unix.getpid
())) in
451 pr2 (spf "now using %s for logging" filename);
452 _chan := open_out
filename
455 let dolog s = output_string
!_chan (s ^
"\n"); flush
!_chan
457 let verbose_level = ref 1
458 let log s = if !verbose_level >= 1 then dolog s
459 let log2 s = if !verbose_level >= 2 then dolog s
460 let log3 s = if !verbose_level >= 3 then dolog s
461 let log4 s = if !verbose_level >= 4 then dolog s
463 let if_log f
= if !verbose_level >= 1 then f
()
464 let if_log2 f
= if !verbose_level >= 2 then f
()
465 let if_log3 f
= if !verbose_level >= 3 then f
()
466 let if_log4 f
= if !verbose_level >= 4 then f
()
468 (* ---------------------------------------------------------------------- *)
470 let pause () = (pr2 "pause: type return"; ignore
(read_line
()))
472 (* src: from getopt from frish *)
473 let bip () = Printf.printf
"\007"; flush stdout
474 let wait () = Unix.sleep
1
476 (* was used by fix_caml *)
477 let _trace_var = ref 0
478 let add_var() = incr
_trace_var
479 let dec_var() = decr
_trace_var
480 let get_var() = !_trace_var
482 let (print_n
: int -> string -> unit) = fun i
s ->
483 do_n i
(fun () -> print_string
s)
484 let (printerr_n
: int -> string -> unit) = fun i
s ->
485 do_n i
(fun () -> prerr_string
s)
487 let _debug = ref true
488 let debugon () = _debug := true
489 let debugoff () = _debug := false
490 let debug f
= if !_debug then f
() else ()
495 * let debugger = ref false
499 (*****************************************************************************)
501 (*****************************************************************************)
504 command2("grep VmData /proc/" ^ string_of_int
(Unix.getpid
()) ^
"/status")
507 let stat = Gc.stat() in
508 let conv_mo x
= x
* 4 / 1000000 in
509 Printf.sprintf
"maximal = %d Mo\n" (conv_mo stat.Gc.top_heap_words
) ^
510 Printf.sprintf
"current = %d Mo\n" (conv_mo stat.Gc.heap_words
) ^
511 Printf.sprintf
"lives = %d Mo\n" (conv_mo stat.Gc.live_words
)
512 (* Printf.printf "fragments = %d Mo\n" (conv_mo stat.Gc.fragments); *)
515 "sys:" ^
(string_of_float
(Sys.time
())) ^
" seconds" ^
517 (let tm = Unix.time
() +> Unix.gmtime
in
518 tm.Unix.tm_min
+> string_of_int ^
" min:" ^
519 tm.Unix.tm_sec
+> string_of_int ^
".00 seconds")
527 let count1 () = incr
_count1
528 let count2 () = incr
_count2
529 let count3 () = incr
_count3
530 let count4 () = incr
_count4
531 let count5 () = incr
_count5
533 let profile_diagnostic_basic () =
535 "count1 = %d\ncount2 = %d\ncount3 = %d\ncount4 = %d\ncount5 = %d\n"
536 !_count1 !_count2 !_count3 !_count4 !_count5
541 (* let _ = Timing () in *)
543 (* let _ = Timing () in *)
546 (* ---------------------------------------------------------------------- *)
548 type prof
= PALL
| PNONE
| PSOME
of string list
549 let profile = ref PNONE
550 let show_trace_profile = ref false
552 let check_profile category
=
556 | PSOME l
-> List.mem category l
558 let _profile_table = ref (Hashtbl.create
100)
560 let adjust_profile_entry category difftime
=
561 let (xtime
, xcount
) =
562 (try Hashtbl.find
!_profile_table category
564 let xtime = ref 0.0 in
565 let xcount = ref 0 in
566 Hashtbl.add
!_profile_table category
(xtime, xcount);
569 xtime := !xtime +. difftime
;
570 xcount := !xcount + 1;
573 let profile_start category
= failwith
"todo"
574 let profile_end category
= failwith
"todo"
577 (* subtil: don't forget to give all argumens to f, otherwise partial app
578 * and will profile nothing.
580 let profile_code category f
=
581 if not
(check_profile category
)
584 if !show_trace_profile then pr2 (spf "p: %s" category
);
585 let t = Unix.gettimeofday
() in
588 with Timeout
-> None
, "*"
590 let category = prefix ^
category in (* add a '*' to indicate timeout func *)
591 let t'
= Unix.gettimeofday
() in
593 adjust_profile_entry category (t'
-. t);
596 | None
-> raise Timeout
601 let _is_in_exclusif = ref (None
: string option)
603 let profile_code_exclusif category f
=
604 if not
(check_profile category)
608 match !_is_in_exclusif with
610 failwith
(spf "profile_code_exclusif: %s but already in %s " category s);
612 _is_in_exclusif := (Some
category);
615 profile_code category f
618 _is_in_exclusif := None
623 let profile_code_inside_exclusif_ok category f
=
627 (* todo: also put % ? also add % to see if coherent numbers *)
628 let profile_diagnostic () =
629 if !profile = PNONE
then "" else
631 Hashtbl.fold
(fun k v acc
-> (k
,v
)::acc
) !_profile_table []
632 +> List.sort
(fun (k1
, (t1
,n1
)) (k2
, (t2
,n2
)) -> compare t2 t1
)
634 with_open_stringbuf
(fun (pr,_) ->
635 pr "---------------------";
636 pr "profiling result";
637 pr "---------------------";
638 xs +> List.iter
(fun (k
, (t,n)) ->
639 pr (sprintf
"%-40s : %10.3f sec %10d count" k
!t !n)
645 let report_if_take_time timethreshold
s f
=
646 let t = Unix.gettimeofday
() in
648 let t'
= Unix.gettimeofday
() in
649 if (t'
-. t > float_of_int timethreshold
)
650 then pr2 (sprintf
"NOTE: this code takes more than: %ds %s" timethreshold
s);
653 let profile_code2 category f
=
654 profile_code category (fun () ->
656 then pr2 ("starting: " ^
category);
657 let t = Unix.gettimeofday
() in
659 let t'
= Unix.gettimeofday
() in
661 then pr2 (spf "ending: %s, %fs" category (t'
-. t));
666 (*****************************************************************************)
668 (*****************************************************************************)
669 let example b
= assert b
671 let _ex1 = example (enum 1 4 = [1;2;3;4])
673 let assert_equal a b
=
675 then failwith
("assert_equal: those 2 values are not equal:\n\t" ^
676 (dump a
) ^
"\n\t" ^
(dump b
) ^
"\n")
678 let (example2
: string -> bool -> unit) = fun s b
->
679 try assert b
with x -> failwith
s
681 (*-------------------------------------------------------------------*)
682 let _list_bool = ref []
684 let (example3
: string -> bool -> unit) = fun s b
->
685 _list_bool := (s,b
)::(!_list_bool)
687 (* could introduce a fun () otherwise the calculus is made at compile time
688 * and this can be long. This would require to redefine test_all.
689 * let (example3: string -> (unit -> bool) -> unit) = fun s func ->
690 * _list_bool := (s,func):: (!_list_bool)
692 * I would like to do as a func that take 2 terms, and make an = over it
693 * avoid to add this ugly fun (), but pb of type, cant do that :(
697 let (test_all
: unit -> unit) = fun () ->
698 List.iter
(fun (s, b
) ->
699 Printf.printf
"%s: %s\n" s (if b
then "passed" else "failed")
702 let (test
: string -> unit) = fun s ->
703 Printf.printf
"%s: %s\n" s
704 (if (List.assoc
s (!_list_bool)) then "passed" else "failed")
706 let _ex = example3
"++" ([1;2]++[3;4;5] = [1;2;3;4;5])
708 (*-------------------------------------------------------------------*)
709 (* Regression testing *)
710 (*-------------------------------------------------------------------*)
712 (* cf end of file. It uses too many other common functions so I
713 * have put the code at the end of this file.
718 (* todo? take code from julien signoles in calendar-2.0.2/tests *)
721 (* Generic functions used in the tests. *)
723 val reset
: unit -> unit
724 val nb_ok
: unit -> int
725 val nb_bug
: unit -> int
726 val test
: bool -> string -> unit
727 val test_exn
: 'a
Lazy.t -> string -> unit
731 let ok () = incr
ok_ref
732 let nb_ok () = !ok_ref
735 let bug () = incr
bug_ref
736 let nb_bug () = !bug_ref
743 if x then ok () else begin Printf.printf
"%s\n" s; bug () end;;
747 ignore
(Lazy.force
x);
748 Printf.printf
"%s\n" s;
755 (*****************************************************************************)
756 (* Quickcheck like (sfl) *)
757 (*****************************************************************************)
759 (* Better than quickcheck, cos cant do a test_all_prop in haskell cos
760 * prop were functions, whereas here we have not prop_Unix x = ... but
763 * How to do without overloading ? objet ? can pass a generator as a
764 * parameter, mais lourd, prefer automatic inferring of the
765 * generator? But at the same time quickcheck does not do better cos
766 * we must explictly type the property. So between a
767 * prop_unit:: [Int] -> [Int] -> bool ...
768 * prop_unit x = reverse [x] == [x]
770 * let _ = laws "unit" (fun x -> reverse [x] = [x]) (listg intg)
771 * there is no real differences.
773 * Yes I define typeg generator but quickcheck too, he must define
774 * class instance. I emulate the context Gen a => Gen [a] by making
775 * listg take as a param a type generator. Moreover I have not the pb of
776 * monad. I can do random independently, so my code is more simple
777 * I think than the haskell code of quickcheck.
779 * update: apparently Jane Street have copied some of my code for their
780 * Ounit_util.ml and quichcheck.ml in their Core library :)
783 (*---------------------------------------------------------------------------*)
785 (*---------------------------------------------------------------------------*)
786 type 'a gen
= unit -> 'a
788 let (ig
: int gen
) = fun () ->
790 let (lg
: ('a gen
) -> ('a list
) gen
) = fun gen
() ->
791 foldn
(fun acc i
-> (gen
())::acc
) [] (Random.int 10)
792 let (pg
: ('a gen
) -> ('b gen
) -> ('a
* 'b
) gen
) = fun gen1 gen2
() ->
795 let (ng
: (string gen
)) = fun () ->
796 "a" ^
(string_of_int
(ig
()))
798 let (oneofl
: ('a list
) -> 'a gen
) = fun xs () ->
799 List.nth
xs (Random.int (List.length
xs))
800 (* let oneofl l = oneof (List.map always l) *)
802 let (oneof
: (('a gen
) list
) -> 'a gen
) = fun xs ->
803 List.nth
xs (Random.int (List.length
xs))
805 let (always
: 'a
-> 'a gen
) = fun e
() -> e
807 let (frequency
: ((int * ('a gen
)) list
) -> 'a gen
) = fun xs ->
808 let sums = sum_int (List.map fst
xs) in
809 let i = Random.int sums in
810 let rec freq_aux acc
= function
811 | (x,g
)::xs -> if i < acc
+x then g
else freq_aux (acc
+x) xs
812 | _ -> failwith
"frequency"
815 let frequencyl l
= frequency
(List.map
(fun (i,e
) -> (i,always e
)) l
)
818 let b = oneof [always true; always false] ()
819 let b = frequency [3, always true; 2, always false] ()
823 * let rec (lg: ('a gen) -> ('a list) gen) = fun gen -> oneofl [[]; lg gen ()]
825 * let rec (lg: ('a gen) -> ('a list) gen) = fun gen -> oneof [always []; lg gen]
827 * because caml is not as lazy as haskell :( fix the pb by introducing a size
828 * limit. take the bounds/size as parameter. morover this is needed for
831 * how make a bintreeg ?? we need recursion
833 * let rec (bintreeg: ('a gen) -> ('a bintree) gen) = fun gen () ->
835 * if n = 0 then (Leaf (gen ()))
836 * else frequencyl [1, Leaf (gen ()); 4, Branch ((aux (n / 2)), aux (n / 2))]
843 (*---------------------------------------------------------------------------*)
845 (*---------------------------------------------------------------------------*)
847 (* todo: a test_all_laws, better syntax (done already a little with ig in
848 * place of intg. En cas d'erreur, print the arg that not respect
850 * todo: with monitoring, as in haskell, laws = laws2, no need for 2 func,
853 * todo classify, collect, forall
857 (* return None when good, and Just the_problematic_case when bad *)
858 let (laws
: string -> ('a
-> bool) -> ('a gen
) -> 'a
option) = fun s func gen
->
859 let res = foldn
(fun acc
i -> let n = gen
() in (n, func
n)::acc
) [] 1000 in
860 let res = List.filter
(fun (x,b) -> not
b) res in
861 if res = [] then None
else Some
(fst
(List.hd
res))
863 let rec (statistic_number
: ('a list
) -> (int * 'a
) list
) = function
865 | x::xs -> let (splitg
, splitd
) = List.partition
(fun y
-> y
= x) xs in
866 (1+(List.length splitg
), x)::(statistic_number splitd
)
869 let (statistic
: ('a list
) -> (int * 'a
) list
) = fun xs ->
870 let stat_num = statistic_number
xs in
871 let totals = sum_int (List.map fst
stat_num) in
872 List.map
(fun (i, v
) -> ((i * 100) / totals), v
) stat_num
875 string -> ('a
-> (bool * '
b)) -> ('a gen
) ->
876 ('a
option * ((int * '
b) list
))) =
878 let res = foldn
(fun acc
i -> let n = gen
() in (n, func
n)::acc
) [] 1000 in
879 let stat = statistic
(List.map
(fun (x,(b,v
)) -> v
) res) in
880 let res = List.filter
(fun (x,(b,v
)) -> not
b) res in
881 if res = [] then (None
, stat) else (Some
(fst
(List.hd
res)), stat)
885 let b = laws "unit" (fun x -> reverse [x] = [x] )ig
886 let b = laws "app " (fun (xs,ys) -> reverse (xs++ys) = reverse ys++reverse xs)(pg (lg ig)(lg ig))
887 let b = laws "rev " (fun xs -> reverse (reverse xs) = xs )(lg ig)
888 let b = laws "appb" (fun (xs,ys) -> reverse (xs++ys) = reverse xs++reverse ys)(pg (lg ig)(lg ig))
889 let b = laws "max" (fun (x,y) -> x <= y ==> (max x y = y) )(pg ig ig)
891 let b = laws2 "max" (fun (x,y) -> ((x <= y ==> (max x y = y)), x <= y))(pg ig ig)
895 (* todo, do with coarbitrary ?? idea is that given a 'a, generate a 'b
896 * depending of 'a and gen 'b, that is modify gen 'b, what is important is
897 * that each time given the same 'a, we must get the same 'b !!!
901 let (fg: ('a gen) -> ('b gen) -> ('a -> 'b) gen) = fun gen1 gen2 () ->
902 let b = laws "funs" (fun (f,g,h) -> x <= y ==> (max x y = y) )(pg ig ig)
906 let one_of xs = List.nth xs (Random.int (List.length xs))
908 if empty xs then failwith "Take_one: empty list"
910 let i = Random.int (List.length xs) in
911 List.nth xs i, filter_index (fun j _ -> i <> j) xs
914 (*****************************************************************************)
916 (*****************************************************************************)
918 let get_value filename =
919 let chan = open_in
filename in
920 let x = input_value
chan in (* <=> Marshal.from_channel *)
923 let write_value valu
filename =
924 let chan = open_out
filename in
925 (output_value
chan valu
; (* <=> Marshal.to_channel *)
926 (* Marshal.to_channel chan valu [Marshal.Closures]; *)
929 let write_back func
filename =
930 write_value (func
(get_value filename)) filename
933 let read_value f
= get_value f
936 (*****************************************************************************)
938 (*****************************************************************************)
940 let counter () = (_counter := !_counter +1; !_counter)
942 let _counter2 = ref 0
943 let counter2 () = (_counter2 := !_counter2 +1; !_counter2)
945 let _counter3 = ref 0
946 let counter3 () = (_counter3 := !_counter3 +1; !_counter3)
950 (*****************************************************************************)
952 (*****************************************************************************)
953 (* To work with the macro system autogenerated string_of and print_ function
954 (kind of deriving a la haskell) *)
956 (* int, bool, char, float, ref ?, string *)
958 let string_of_string s = "\"" ^
s "\""
960 let string_of_list f
xs =
961 "[" ^
(xs +> List.map f
+> String.concat
";" ) ^
"]"
963 let string_of_unit () = "()"
965 let string_of_array f
xs =
966 "[|" ^
(xs +> Array.to_list
+> List.map f
+> String.concat
";") ^
"|]"
968 let string_of_option f
= function
970 | Some
x -> "Some " ^
(f
x)
975 let print_bool x = print_string
(if x then "True" else "False")
977 let print_option pr = function
978 | None
-> print_string
"None"
979 | Some
x -> print_string
"Some ("; pr x; print_string
")"
981 let print_list pr xs =
984 List.iter
(fun x -> pr x; print_string
",") xs;
989 let (string_of_list: char list -> string) =
990 List.fold_left (fun acc x -> acc^(Char.escaped x)) ""
994 let rec print_between between fn
= function
997 | x::xs -> fn
x; between
(); print_between between fn
xs
1002 let adjust_pp_with_indent f
=
1003 Format.open_box
!_tab_level_print;
1004 (*Format.force_newline();*)
1006 Format.close_box
();
1007 Format.print_newline
()
1009 let adjust_pp_with_indent_and_header s f
=
1010 Format.open_box
(!_tab_level_print + String.length
s);
1011 do_n
!_tab_level_print (fun () -> Format.print_string
" ");
1012 Format.print_string
s;
1014 Format.close_box
();
1015 Format.print_newline
()
1019 let pp_do_in_box f
= Format.open_box
1; f
(); Format.close_box
()
1020 let pp_do_in_zero_box f
= Format.open_box
0; f
(); Format.close_box
()
1025 Format.close_box
();
1028 let pp s = Format.print_string
s
1032 (* julia: convert something printed using format to print into a string *)
1033 let format_to_string f
=
1034 let o = open_out
"/tmp/out" in
1035 Format.set_formatter_out_channel
o;
1037 Format.print_flush
();
1038 Format.set_formatter_out_channel stdout
;
1040 let i = open_in
"/tmp/out" in
1041 let lines = ref [] in
1043 let cur = input_line
i in
1044 lines := cur :: !lines;
1046 (try loop() with End_of_file
-> ());
1048 String.concat
"\n" (List.rev
!lines)
1052 (*****************************************************************************)
1054 (*****************************************************************************)
1056 (* put your macro in macro.ml4, and you can test it interactivly as in lisp *)
1057 let macro_expand s =
1058 let c = open_out
"/tmp/ttttt.ml" in
1060 output_string
c s; close_out
c;
1061 command2 ("ocamlc -c -pp 'camlp4o pa_extend.cmo q_MLast.cmo -impl' " ^
1062 "-I +camlp4 -impl macro.ml4");
1063 command2 "camlp4o ./macro.cmo pr_o.cmo /tmp/ttttt.ml";
1064 command2 "rm -f /tmp/ttttt.ml";
1068 let t = macro_expand "{ x + y | (x,y) <- [(1,1);(2,2);(3,3)] and x>2 and y<3}"
1069 let x = { x + y | (x,y) <- [(1,1);(2,2);(3,3)] and x > 2 and y < 3}
1070 let t = macro_expand "{1 .. 10}"
1071 let x = {1 .. 10} +> List.map (fun i -> i)
1072 let t = macro_expand "[1;2] to append to [2;4]"
1073 let t = macro_expand "{x = 2; x = 3}"
1075 let t = macro_expand "type 'a bintree = Leaf of 'a | Branch of ('a bintree * 'a bintree)"
1080 (*****************************************************************************)
1081 (* Composition/Control *)
1082 (*****************************************************************************)
1084 (* I like the obj.func object notation. In OCaml cant use '.' so I use +>
1086 * update: it seems that F# agrees with me :) but they use |>
1090 * let (+>) o f = f o
1092 let (+!>) refo f
= refo
:= f
!refo
1094 * let ((@): 'a -> ('a -> 'b) -> 'b) = fun a b -> b a
1095 * let o f g x = f (g x)
1098 let ($
) f g
x = g
(f
x)
1099 let compose f g
x = f
(g
x)
1100 (* dont work :( let ( ° ) f g x = f(g(x)) *)
1102 (* trick to have something similar to the 1 `max` 4 haskell infix notation.
1103 by Keisuke Nakano on the caml mailing list.
1104 > let ( /* ) x y = y x
1105 > and ( */ ) x y = x y
1107 let ( <| ) x y = y x
1108 and ( |> ) x y = x y
1110 > Then we can make an infix operator <| f |> for a binary function f.
1113 let flip f
= fun a
b -> f
b a
1115 let curry f
x y
= f
(x,y
)
1116 let uncurry f
(a
,b) = f a
b
1120 let do_nothing () = ()
1122 let rec applyn n f
o = if n = 0 then o else applyn (n-1) f
(f
o)
1130 class ['a
] shared_variable_hook
(x:'a
) =
1132 val mutable data
= x
1133 val mutable registered
= []
1137 pr "refresh registered";
1138 registered
+> List.iter
(fun f
-> f
());
1141 method modify f
= self#set
(f self#get
)
1143 registered
<- f
:: registered
1146 (* src: from aop project. was called ptFix *)
1147 let rec fixpoint trans elem
=
1148 let image = trans elem
in
1150 then elem
(* point fixe *)
1151 else fixpoint trans
image
1153 (* le point fixe pour les objets. was called ptFixForObjetct *)
1154 let rec fixpoint_for_object trans elem
=
1155 let image = trans elem
in
1156 if (image#equal elem
) then elem
(* point fixe *)
1157 else fixpoint_for_object trans
image
1159 let (add_hook
: ('a
-> ('a
-> '
b) -> '
b) ref -> ('a
-> ('a
-> '
b) -> '
b) -> unit) =
1161 let oldvar = !var
in
1162 var
:= fun arg k
-> f arg
(fun x -> oldvar x k
)
1164 let (add_hook_action
: ('a
-> unit) -> ('a
-> unit) list
ref -> unit) =
1168 let (run_hooks_action
: 'a
-> ('a
-> unit) list
ref -> unit) =
1170 !hooks
+> List.iter
(fun f
-> try f obj
with _ -> ())
1173 type 'a mylazy
= (unit -> 'a
)
1176 let save_excursion reference f
=
1177 let old = !reference
in
1184 let memoized h k f
=
1185 try Hashtbl.find
h k
1194 let already = ref false in
1197 then begin already := true; f
x end
1200 (* cache_file, cf below *)
1202 let before_leaving f
x =
1206 (* finalize, cf prelude *)
1210 let rec y f
= fun x -> f
(y f
) x
1212 (*****************************************************************************)
1214 (*****************************************************************************)
1216 (* from http://en.wikipedia.org/wiki/File_locking
1218 * "When using file locks, care must be taken to ensure that operations
1219 * are atomic. When creating the lock, the process must verify that it
1220 * does not exist and then create it, but without allowing another
1221 * process the opportunity to create it in the meantime. Various
1222 * schemes are used to implement this, such as taking advantage of
1223 * system calls designed for this purpose (but such system calls are
1224 * not usually available to shell scripts) or by creating the lock file
1225 * under a temporary name and then attempting to move it into place."
1227 * => can't use 'if(not (file_exist xxx)) then create_file xxx' because
1228 * file_exist/create_file are not in atomic section (classic problem).
1232 * "O_EXCL When used with O_CREAT, if the file already exists it
1233 * is an error and the open() will fail. In this context, a
1234 * symbolic link exists, regardless of where it points to.
1235 * O_EXCL is broken on NFS file systems; programs which
1236 * rely on it for performing locking tasks will contain a
1237 * race condition. The solution for performing atomic file
1238 * locking using a lockfile is to create a unique file on
1239 * the same file system (e.g., incorporating host- name and
1240 * pid), use link(2) to make a link to the lockfile. If
1241 * link(2) returns 0, the lock is successful. Otherwise,
1242 * use stat(2) on the unique file to check if its link
1243 * count has increased to 2, in which case the lock is also
1248 exception FileAlreadyLocked
1250 (* Racy if lock file on NFS!!! But still racy with recent Linux ? *)
1251 let acquire_file_lock filename =
1252 pr2 ("Locking file: " ^
filename);
1254 let _fd = Unix.openfile
filename [Unix.O_CREAT
;Unix.O_EXCL
] 0o777
in
1256 with Unix.Unix_error
(e
, fm
, argm
) ->
1257 pr2 (spf "exn Unix_error: %s %s %s\n" (Unix.error_message e
) fm argm
);
1258 raise FileAlreadyLocked
1261 let release_file_lock filename =
1262 pr2 ("Releasing file: " ^
filename);
1263 Unix.unlink
filename;
1268 (*****************************************************************************)
1269 (* Error managment *)
1270 (*****************************************************************************)
1273 exception Impossible
1277 exception WrongFormat
of string
1279 (* old: let _TODO () = failwith "TODO", now via fix_caml with raise Todo *)
1281 let internal_error s = failwith
("internal error: "^
s)
1282 let error_cant_have x = internal_error ("cant have this case" ^
(dump x))
1283 let myassert cond
= if cond
then () else failwith
"assert error"
1287 (* before warning I was forced to do stuff like this:
1289 * let (fixed_int_to_posmap: fixed_int -> posmap) = fun fixed ->
1290 * let v = ((fix_to_i fixed) / (power 2 16)) in
1291 * let _ = Printf.printf "coord xy = %d\n" v in
1294 * The need for printf make me force to name stuff :(
1295 * How avoid ? use 'it' special keyword ?
1296 * In fact dont have to name it, use +> (fun v -> ...) so when want
1297 * erase debug just have to erase one line.
1299 let warning s v = (pr2 ("Warning: " ^
s ^
"; value = " ^
(dump v)); v)
1305 Printexc.to_string exn
1309 (* want or of merd, but cant cos cant put die ... in b (strict call) *)
1310 let (|||) a
b = try a
with _ -> b
1312 (* emacs/lisp inspiration, (vouillon does that too in unison I think) *)
1315 * let unwind_protect f cleanup = ...
1316 * let finalize f cleanup = ...
1320 (*****************************************************************************)
1322 (*****************************************************************************)
1324 let check_stack = ref true
1325 let check_stack_size limit
=
1326 if !check_stack then begin
1327 pr2 "checking stack size (do ulimit -s 50000 if problem)";
1331 else 1 + aux (i + 1)
1333 assert(aux 0 = limit
);
1337 let test_check_stack_size limit
=
1338 (* bytecode: 100000000 *)
1339 (* native: 10000000 *)
1340 check_stack_size (int_of_string limit
)
1343 (* only relevant in bytecode, in native the stacklimit is the os stacklimit
1344 * (adjustable by ulimit -s)
1346 let _init_gc_stack =
1347 Gc.set
{(Gc.get
()) with Gc.stack_limit
= 100 * 1024 * 1024}
1350 (* if process a big set of files then dont want get overflow in the middle
1351 * so for this we are ready to spend some extra time at the beginning that
1352 * could save far more later.
1354 let check_stack_nbfiles nbfiles
=
1356 then check_stack_size 10000000
1358 (*****************************************************************************)
1359 (* Arguments/options and command line (cocci and acomment) *)
1360 (*****************************************************************************)
1363 * Why define wrappers ? Arg not good enough ? Well the Arg.Rest is not that
1364 * good and I need a way sometimes to get a list of argument.
1366 * I could define maybe a new Arg.spec such as
1367 * | String_list of (string list -> unit), but the action may require
1368 * some flags to be set, so better to process this after all flags have
1369 * been set by parse_options. So have to split. Otherwise it would impose
1370 * an order of the options such as
1371 * -verbose_parsing -parse_c file1 file2. and I really like to use bash
1372 * history and add just at the end of my command a -profile for instance.
1375 * Why want a -action arg1 arg2 arg3 ? (which in turn requires this
1376 * convulated scheme ...) Why not use Arg.String action such as
1377 * "-parse_c", Arg.String (fun file -> ...) ?
1378 * I want something that looks like ocaml function but at the UNIX
1379 * command line level. So natural to have this scheme instead of
1380 * -taxo_file arg2 -sample_file arg3 -parse_c arg1.
1383 * Why not use the toplevel ? because to debug ocamldebug is far superior
1384 * to the toplevel (can go back, can go directly to a specific point, etc).
1385 * I want a kind of testing at cmdline level.
1388 * Why having variable flags ? Why use 'if !verbose_parsing then ...' ?
1389 * why not use strings and do stuff like the following
1390 * 'if (get_config "verbose_parsing") then ...'
1391 * Because I want to make the interface for flags easier for the code
1392 * that use it. The programmer should not be bothered wether this
1393 * flag is set via args cmd line or a config file, so I want to make it
1394 * as simple as possible, just use a global plain caml ref variable.
1396 * Same spirit a little for the action. Instead of having function such as
1397 * test_parsing_c, I could do it only via string. But I still prefer
1398 * to have plain caml test functions. Also it makes it easier to call
1399 * those functions from a toplevel for people who prefer the toplevel.
1402 * So have flag_spec and action_spec. And in flag have debug_xxx flags,
1403 * verbose_xxx flags and other flags.
1405 * I would like to not have to separate the -xxx actions spec from the
1406 * corresponding actions, but those actions may need more than one argument
1407 * and so have to wait for parse_options, which in turn need the options
1410 * Also I dont want to mix code with data structures, so it's better that the
1411 * options variable contain just a few stuff and have no side effects except
1412 * setting global variables.
1414 * Why not have a global variable such as Common.actions that
1415 * other modules modify ? No, I prefer to do less stuff behind programmer's
1416 * back so better to let the user merge the different options at call
1417 * site, but at least make it easier by providing shortcut for set of options.
1422 * todo? isn't unison or scott-mcpeak-lib-in-cil handles that kind of
1423 * stuff better ? That is the need to localize command line argument
1424 * while still being able to gathering them. Same for logging.
1425 * Similiar to the type prof = PALL | PNONE | PSOME of string list.
1426 * Same spirit of fine grain config in log4j ?
1428 * todo? how mercurial/cvs/git manage command line options ? because they
1429 * all have a kind of DSL around arguments with some common options,
1430 * specific options, conventions, etc.
1433 * todo? generate the corresponding noxxx options ?
1434 * todo? generate list of options and show their value ?
1436 * todo? make it possible to set this value via a config file ?
1441 type arg_spec_full
= Arg.key
* Arg.spec
* Arg.doc
1442 type cmdline_options
= arg_spec_full list
1444 (* the format is a list of triples:
1445 * (title of section * (optional) explanation of sections * options)
1447 type options_with_title
= string * string * arg_spec_full list
1448 type cmdline_sections
= options_with_title list
1451 (* ---------------------------------------------------------------------- *)
1453 (* now I use argv as I like at the call sites to show that
1454 * this function internally use argv.
1456 let parse_options options usage_msg argv
=
1457 let args = ref [] in
1459 Arg.parse_argv argv options
(fun file
-> args := file
::!args) usage_msg
;
1460 args := List.rev
!args;
1463 | Arg.Bad msg
-> eprintf
"%s" msg
; exit
2
1464 | Arg.Help msg
-> printf
"%s" msg
; exit
0
1470 let usage usage_msg options
=
1471 Arg.usage (Arg.align options
) usage_msg
1474 (* for coccinelle *)
1476 (* If you don't want the -help and --help that are appended by Arg.align *)
1478 Arg.align
xs +> List.rev
+> drop 2 +> List.rev
1481 let short_usage usage_msg ~short_opt
=
1482 usage usage_msg short_opt
1484 let long_usage usage_msg ~short_opt ~long_opt
=
1487 let all_options_with_title =
1488 (("main options", "", short_opt
)::long_opt
) in
1489 all_options_with_title +> List.iter
1490 (fun (title
, explanations
, xs) ->
1492 pr_xxxxxxxxxxxxxxxxx();
1493 if explanations
<> ""
1494 then begin pr explanations
; pr "" end;
1495 arg_align2 xs +> List.iter
(fun (key
,action
,s) ->
1503 (* copy paste of Arg.parse. Don't want the default -help msg *)
1504 let arg_parse2 l msg short_usage_fun
=
1505 let args = ref [] in
1506 let f = (fun file
-> args := file
::!args) in
1507 let l = Arg.align
l in
1509 Arg.parse_argv
Sys.argv
l f msg
;
1510 args := List.rev
!args;
1514 | Arg.Bad msg
-> (* eprintf "%s" msg; exit 2; *)
1515 let xs = lines msg
in
1516 (* take only head, it's where the error msg is *)
1519 raise
(UnixExit
(2))
1520 | Arg.Help msg
-> (* printf "%s" msg; exit 0; *)
1521 raise Impossible
(* -help is specified in speclist *)
1525 (* ---------------------------------------------------------------------- *)
1526 (* kind of unit testing framework, or toplevel like functionnality
1527 * at shell command line. I realize than in fact It follows a current trend
1528 * to have a main cmdline program where can then select different actions,
1529 * as in cvs/hg/git where do hg <action> <arguments>, and the shell even
1530 * use a curried syntax :)
1533 * Not-perfect-but-basic-feels-right: an action
1534 * spec looks like this:
1536 * let actions () = [
1537 * "-parse_taxo", " <file>",
1538 * Common.mk_action_1_arg test_parse_taxo;
1542 * Not-perfect-but-basic-feels-right because for such functionality we
1543 * need a way to transform a string into a caml function and pass arguments
1544 * and the preceding design does exactly that, even if then the
1545 * functions that use this design are not so convenient to use (there
1546 * are 2 places where we need to pass those data, in the options and in the
1549 * Also it's not too much intrusive. Still have an
1550 * action ref variable in the main.ml and can still use the previous
1551 * simpler way to do where the match args with in main.ml do the
1554 * Use like this at option place:
1555 * (Common.options_of_actions actionref (Test_parsing_c.actions())) ++
1556 * Use like this at dispatch action place:
1557 * | xs when List.mem !action (Common.action_list all_actions) ->
1558 * Common.do_action !action xs all_actions
1562 type flag_spec
= Arg.key
* Arg.spec
* Arg.doc
1563 type action_spec
= Arg.key
* Arg.doc
* action_func
1564 and action_func
= (string list
-> unit)
1566 type cmdline_actions
= action_spec list
1567 exception WrongNumberOfArguments
1569 let options_of_actions action_ref
actions =
1570 actions +> List.map
(fun (key
, doc
, _func
) ->
1571 (key
, (Arg.Unit
(fun () -> action_ref
:= key
)), doc
)
1574 let (action_list
: cmdline_actions
-> Arg.key list
) = fun xs ->
1575 List.map
(fun (a
,b,c) -> a
) xs
1577 let (do_action
: Arg.key
-> string list
(* args *) -> cmdline_actions
-> unit) =
1579 let assoc = xs +> List.map
(fun (a
,b,c) -> (a
,c)) in
1580 let action_func = List.assoc key
assoc in
1584 (* todo? if have a function with default argument ? would like a
1585 * mk_action_0_or_1_arg ?
1588 let mk_action_0_arg f =
1591 | _ -> raise WrongNumberOfArguments
1594 let mk_action_1_arg f =
1597 | _ -> raise WrongNumberOfArguments
1600 let mk_action_2_arg f =
1602 | [file1
;file2
] -> f file1 file2
1603 | _ -> raise WrongNumberOfArguments
1606 let mk_action_3_arg f =
1608 | [file1
;file2
;file3
] -> f file1 file2 file3
1609 | _ -> raise WrongNumberOfArguments
1612 let mk_action_n_arg f = f
1615 (*****************************************************************************)
1617 (*****************************************************************************)
1619 (* Using the generic (=) is tempting, but it backfires, so better avoid it *)
1621 (* To infer all the code that use an equal, and that should be
1622 * transformed, is not that easy, because (=) is used by many
1623 * functions, such as List.find, List.mem, and so on. So the strategy
1624 * is to turn what you were previously using into a function, because
1625 * (=) return an exception when applied to a function. Then you simply
1626 * use ocamldebug to infer where the code has to be transformed.
1629 (* src: caml mailing list ? *)
1630 let (=|=) : int -> int -> bool = (=)
1631 let (=<=) : char
-> char
-> bool = (=)
1632 let (=$
=) : string -> string -> bool = (=)
1633 let (=:=) : bool -> bool -> bool = (=)
1635 (* the evil generic (=). I define another symbol to more easily detect
1636 * it, cos the '=' sign is syntaxically overloaded in caml. It is also
1637 * used to define function.
1641 (* if really want to forbid to use '='
1652 (*###########################################################################*)
1653 (* And now basic types *)
1654 (*###########################################################################*)
1658 (*****************************************************************************)
1660 (*****************************************************************************)
1661 let (==>) b1 b2
= if b1
then b2
else true (* could use too => *)
1663 let (<=>) a
b = if a
= b then 0 else if a
< b then -1 else 1
1665 let xor a
b = not
(a
= b)
1668 (*****************************************************************************)
1670 (*****************************************************************************)
1672 let string_of_char c = String.make
1 c
1674 let is_single = String.contains
",;()[]{}_`"
1675 let is_symbol = String.contains
"!@#$%&*+./<=>?\\^|:-~"
1676 let is_space = String.contains
"\n\t "
1677 let cbetween min max
c =
1678 (int_of_char
c) <= (int_of_char max
) &&
1679 (int_of_char
c) >= (int_of_char min
)
1680 let is_upper = cbetween 'A' 'Z'
1681 let is_lower = cbetween 'a' 'z'
1682 let is_alpha c = is_upper c || is_lower c
1683 let is_digit = cbetween '
0' '
9'
1685 let string_of_chars cs
= cs
+> List.map
(String.make
1) +> String.concat
""
1689 (*****************************************************************************)
1691 (*****************************************************************************)
1693 (* since 3.08, div by 0 raise Div_by_rezo, and not anymore a hardware trap :)*)
1694 let (/!) x y = if y = 0 then (log "common.ml: div by 0"; 0) else x / y
1697 * let rec (do_n: int -> (unit -> unit) -> unit) = fun i f ->
1698 * if i = 0 then () else (f(); do_n (i-1) f)
1702 * let rec (foldn: ('a -> int -> 'a) -> 'a -> int -> 'a) = fun f acc i ->
1703 * if i = 0 then acc else foldn f (f acc i) (i-1)
1706 let sum_float = List.fold_left
(+.) 0.0
1707 let sum_int = List.fold_left
(+) 0
1709 let pi = 3.14159265358979323846
1714 let (deg_to_rad
: float -> float) = fun deg
->
1715 (deg
*. pi) /. 180.0
1717 let clampf = function
1718 | n when n < 0.0 -> 0.0
1719 | n when n > 1.0 -> 1.0
1722 let square x = x *. x
1724 let rec power x n = if n = 0 then 1 else x * power x (n-1)
1726 let between i min max
= i > min
&& i < max
1728 let (between_strict
: int -> int -> int -> bool) = fun a
b c ->
1732 let bitrange x p
= let v = power 2 p
in between x (-v) v
1735 let (prime1
: int -> int option) = fun x ->
1736 let rec prime1_aux n =
1739 if (x / n) * n = x then Some
n else prime1_aux (n-1)
1740 in if x = 1 then None
else if x < 0 then failwith
"negative" else prime1_aux (x-1)
1742 (* montant, better *)
1743 let (prime
: int -> int option) = fun x ->
1744 let rec prime_aux n =
1747 if (x / n) * n = x then Some
n else prime_aux (n+1)
1748 in if x = 1 then None
else if x < 0 then failwith
"negative" else prime_aux 2
1750 let sum xs = List.fold_left
(+) 0 xs
1751 let product = List.fold_left
( * ) 1
1755 let rec decompose x =
1760 | Some
n -> n::decompose (x / n)
1762 in assert (product (decompose x) = x); decompose x
1764 let mysquare x = x * x
1768 type compare
= Equal
| Inf
| Sup
1769 let (<=>) a
b = if a
= b then Equal
else if a
< b then Inf
else Sup
1770 let (<==>) a
b = if a
= b then 0 else if a
< b then -1 else 1
1775 let int_of_stringchar s =
1776 fold_left_with_index (fun acc e
i -> acc
+ (Char.code e
*(power 8 i))) 0 (List.rev
(list_of_string
s))
1778 let int_of_base s base
=
1779 fold_left_with_index (fun acc e
i ->
1780 let j = Char.code e
- Char.code '
0'
in
1781 if j >= base
then failwith
"not in good base"
1782 else acc
+ (j*(power base
i))
1784 0 (List.rev
(list_of_string
s))
1786 let int_of_stringbits s = int_of_base s 2
1787 let _ = example (int_of_stringbits "1011" = 1*8 + 1*2 + 1*1)
1789 let int_of_octal s = int_of_base s 8
1790 let _ = example (int_of_octal "017" = 15)
1792 (* let int_of_hex s = int_of_base s 16, NONONONO cos 'A' - '0' does not give 10 !! *)
1795 if String.length
s >= 2 && (String.get
s 0 = '
0'
) && is_digit (String.get
s 1)
1796 then int_of_octal s else int_of_string
s
1799 let (+=) ref v = ref := !ref + v
1800 let (-=) ref v = ref := !ref - v
1802 let pourcent x total
=
1804 let pourcent_float x total
=
1805 ((float_of_int
x) *. 100.0) /. (float_of_int total
)
1807 let pourcent_float_of_floats x total
=
1808 (x *. 100.0) /. total
1810 (*****************************************************************************)
1811 (* Numeric/overloading *)
1812 (*****************************************************************************)
1815 NumDict
of (('a
-> 'a
-> 'a
) *
1820 let add (NumDict
(a
, m
, d
, n)) = a
;;
1821 let mul (NumDict
(a
, m
, d
, n)) = m
;;
1822 let div (NumDict
(a
, m
, d
, n)) = d
;;
1823 let neg (NumDict
(a
, m
, d
, n)) = n;;
1825 let numd_int = NumDict
(( + ),( * ),( / ),( ~
- ));;
1826 let numd_float = NumDict
(( +. ),( *. ), ( /. ),( ~
-. ));;
1828 let ( * ) x y = mul dict
x y in
1829 let ( / ) x y = div dict
x y in
1830 let ( + ) x y = add dict
x y in
1831 (* Now you can define all sorts of things in terms of *, /, + *)
1832 let f num
= (num
* num
) / (num
+ num
) in
1837 module ArithFloatInfix
= struct
1849 let (+=) ref v = ref := !ref + v
1850 let (-=) ref v = ref := !ref - v
1856 (*****************************************************************************)
1858 (*****************************************************************************)
1860 type 'a pair
= 'a
* 'a
1861 type 'a triple
= 'a
* 'a
* 'a
1863 let fst3 (x,_,_) = x
1864 let snd3 (_,y,_) = y
1865 let thd3 (_,_,z
) = z
1867 let sndthd (a
,b,c) = (b,c)
1869 let map_fst f (x, y) = f x, y
1870 let map_snd f (x, y) = x, f y
1872 let pair f (x,y) = (f x, f y)
1874 (* for my ocamlbeautify script *)
1879 let swap (x,y) = (y,x)
1882 let tuple_of_list1 = function [a
] -> a
| _ -> failwith
"tuple_of_list1"
1883 let tuple_of_list2 = function [a
;b] -> a
,b | _ -> failwith
"tuple_of_list2"
1884 let tuple_of_list3 = function [a
;b;c] -> a
,b,c | _ -> failwith
"tuple_of_list3"
1885 let tuple_of_list4 = function [a
;b;c;d
] -> a
,b,c,d
| _ -> failwith
"tuple_of_list4"
1886 let tuple_of_list5 = function [a
;b;c;d
;e
] -> a
,b,c,d
,e
| _ -> failwith
"tuple_of_list5"
1887 let tuple_of_list6 = function [a
;b;c;d
;e
;f] -> a
,b,c,d
,e
,f | _ -> failwith
"tuple_of_list6"
1890 (*****************************************************************************)
1892 (*****************************************************************************)
1894 (* type 'a maybe = Just of 'a | None *)
1896 type ('a
,'
b) either
= Left
of 'a
| Right
of '
b
1897 type ('a
, '
b, '
c) either3
= Left3
of 'a
| Middle3
of '
b | Right3
of '
c
1901 | _ -> failwith
"just: pb"
1906 let fmap f = function
1908 | Some
x -> Some
(f x)
1909 let map_option = fmap
1911 let do_option f = function
1916 try Some
(f ()) with Not_found
-> None
1921 let some_or = function
1923 | Some e
-> fun _ -> e
1926 let partition_either f l =
1927 let rec part_either left right
= function
1928 | [] -> (List.rev left
, List.rev right
)
1931 | Left e
-> part_either (e
:: left
) right
l
1932 | Right e
-> part_either left
(e
:: right
) l) in
1937 let rec filter_some = function
1939 | None
:: l -> filter_some l
1940 | Some e
:: l -> e
:: filter_some l
1942 let map_filter f xs = xs +> List.map
f +> filter_some
1944 let rec find_some p
= function
1945 | [] -> raise Not_found
1949 | None
-> find_some p
l
1953 xs +> List.map f +> List.find (function Some x -> true | None -> false)
1954 +> (function Some x -> x | None -> raise Impossible)
1958 (*****************************************************************************)
1960 (*****************************************************************************)
1962 (* put before String section because String section use some =~ *)
1964 (* let gsubst = global_replace *)
1966 (* Different from Perl a little. Must match the entire way.
1967 * So "testBee" =~ "Bee" is wrong
1968 * but "testBee" =~ ".*Bee" is right
1969 * Can have the perl behavior if use Str.search_forward instead of
1973 let (==~
) s re
= Str.string_match re
s 0
1975 let _memo_compiled_regexp = Hashtbl.create
101
1976 let candidate_match_func s re
=
1977 (* old: Str.string_match (Str.regexp re) s 0 *)
1979 memoized _memo_compiled_regexp re
(fun () -> Str.regexp re
)
1981 Str.string_match
compile_re s 0
1983 let match_func s re
=
1984 profile_code "Common.=~" (fun () -> candidate_match_func s re
)
1993 let string_match_substring re
s =
1994 try let _i = Str.search_forward re
s 0 in true
1995 with Not_found
-> false
1997 let (regexp_match
: string -> string -> string) = fun s re
->
1999 Str.matched_group
1 s
2001 (* beurk, side effect code, but hey, it is convenient *)
2003 * let (matched: int -> string -> string) = fun i s ->
2004 * Str.matched_group i s
2006 * let matched1 = fun s -> matched 1 s
2007 * let matched2 = fun s -> (matched 1 s, matched 2 s)
2008 * let matched3 = fun s -> (matched 1 s, matched 2 s, matched 3 s)
2009 * let matched4 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s)
2010 * let matched5 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s)
2011 * let matched6 = fun s -> (matched 1 s, matched 2 s, matched 3 s, matched 4 s, matched 5 s, matched 6 s)
2016 let split sep
s = Str.split (Str.regexp sep
) s
2017 let _ = example (split "/" "" = [])
2018 let join sep
xs = String.concat sep
xs
2019 let _ = example (join "/" ["toto"; "titi"; "tata"] = "toto/titi/tata")
2021 let rec join str = function
2024 | x::xs -> x ^ str ^ (join str xs)
2028 let (split_list_regexp
: string -> string list
-> (string * string list
) list
) =
2030 let rec split_lr_aux (heading
, accu
) = function
2031 | [] -> [(heading
, List.rev accu
)]
2034 then (heading
, List.rev accu
)::split_lr_aux (x, []) xs
2035 else split_lr_aux (heading
, x::accu
) xs
2037 split_lr_aux ("__noheading__", []) xs
2038 +> (fun xs -> if (List.hd
xs) = ("__noheading__",[]) then List.tl
xs else xs)
2042 let regexp_alpha = Str.regexp
2043 "^[a-zA-Z_][A-Za-z_0-9]*$"
2046 let all_match re
s =
2047 let regexp = Str.regexp re
in
2049 let _ = Str.global_substitute
regexp (fun _s
->
2050 let substr = Str.matched_string
s in
2051 assert(substr ==~
regexp); (* @Effect: also use it's side effect *)
2052 let paren_matched = matched1 substr in
2053 push2 paren_matched res;
2058 let _ = example (all_match "\\(@[A-Za-z]+\\)" "ca va @Et toi @Comment"
2059 = ["@Et";"@Comment"])
2062 let global_replace_regexp re f_on_substr
s =
2063 let regexp = Str.regexp re
in
2064 Str.global_substitute
regexp (fun _wholestr
->
2066 let substr = Str.matched_string
s in
2071 let regexp_word_str =
2072 "\\([a-zA-Z_][A-Za-z_0-9]*\\)"
2073 let regexp_word = Str.regexp regexp_word_str
2075 let regular_words s =
2076 all_match regexp_word_str s
2078 let contain_regular_word s =
2079 let xs = regular_words s in
2084 (*****************************************************************************)
2086 (*****************************************************************************)
2088 let slength = String.length
2089 let concat = String.concat
2092 let i_to_s = string_of_int
2093 let s_to_i = int_of_string
2096 (* strings take space in memory. Better when can share the space used by
2098 let _shareds = Hashtbl.create
100
2099 let (shared_string
: string -> string) = fun s ->
2100 try Hashtbl.find
_shareds s
2101 with Not_found
-> (Hashtbl.add _shareds s s; s)
2105 | s -> String.sub
s 0 (String.length
s - 1)
2108 let chop_dirsymbol = function
2109 | s when s =~
"\\(.*\\)/$" -> matched1 s
2113 let (<!!>) s (i,j) =
2114 String.sub
s i (if j < 0 then String.length
s - i + j + 1 else j - i)
2115 (* let _ = example ( "tototati"<!!>(3,-2) = "otat" ) *)
2117 let (<!>) s i = String.get
s i
2120 let rec split_on_char c s =
2122 let sp = String.index
s c in
2123 String.sub
s 0 sp ::
2124 split_on_char c (String.sub
s (sp+1) (String.length
s - sp - 1))
2125 with Not_found
-> [s]
2128 let lowercase = String.lowercase
2130 let quote s = "\"" ^
s ^
"\""
2132 (* easier to have this to be passed as hof, because ocaml dont have
2133 * haskell "section" operators
2138 let is_blank_string s =
2139 s =~
"^\\([ \t]\\)*$"
2141 (* src: lablgtk2/examples/entrycompletion.ml *)
2142 let is_string_prefix s1 s2
=
2143 (String.length s1
<= String.length s2
) && (String.sub s2
0 (String.length s1
) = s1
)
2147 then Printf.sprintf
"%d %s" i s
2148 else Printf.sprintf
"%d %ss" i s
2150 let showCodeHex xs = List.iter
(fun i -> printf
"%02x" i) xs
2155 let ko = (i / 1024) mod 1024 in
2156 let mo = (i / 1024) / 1024 in
2158 then sprintf
"%dMo%dKo" mo ko
2159 else sprintf
"%dKo" ko
2163 let ko = i / 1024 in
2171 (* done in summer 2007 for julia
2172 * Reference: P216 of gusfeld book
2173 * For two strings S1 and S2, D(i,j) is defined to be the edit distance of S1[1..i] to S2[1..j]
2174 * So edit distance of S1 (of length n) and S2 (of length m) is D(n,m)
2176 * Dynamic programming technique
2178 * D(i,0) = i for all i (cos to go from S1[1..i] to 0 characteres of S2 you have to delete all characters from S1[1..i]
2179 * D(0,j) = j for all j (cos j characters must be inserted)
2181 * D(i,j) = min([D(i-1, j)+1, D(i, j - 1 + 1), D(i-1, j-1) + t(i,j)])
2182 * where t(i,j) is equal to 1 if S1(i) != S2(j) and 0 if equal
2183 * intuition = there is 4 possible action = deletion, insertion, substitution, or match
2186 * D(i,j) must be one of the three
2194 let matrix_distance s1 s2
=
2195 let n = (String.length s1
) in
2196 let m = (String.length s2
) in
2197 let mat = Array.make_matrix
(n+1) (m+1) 0 in
2199 if String.get s1
(i-1) = String.get s2
(j-1)
2203 let min3 a
b c = min
(min a
b) c in
2215 min3 (mat.(i).(j-1) + 1) (mat.(i-1).(j) + 1) (mat.(i-1).(j-1) + t i j)
2220 let edit_distance s1 s2
=
2221 (matrix_distance s1 s2
).(String.length s1
).(String.length s2
)
2224 let test = edit_distance "vintner" "writers"
2225 let _ = assert (edit_distance "winter" "winter" = 0)
2226 let _ = assert (edit_distance "vintner" "writers" = 5)
2229 (*****************************************************************************)
2231 (*****************************************************************************)
2233 let dirname = Filename.dirname
2234 let basename = Filename.basename
2236 type filename = string (* TODO could check that exist :) type sux *)
2237 type dirname = string (* TODO could check that exist :) type sux *)
2239 module BasicType
= struct
2240 type filename = string
2244 let (filesuffix
: filename -> string) = fun s ->
2245 (try regexp_match
s ".+\\.\\([a-zA-Z0-9_]+\\)$" with _ -> "NOEXT")
2246 let (fileprefix
: filename -> string) = fun s ->
2247 (try regexp_match
s "\\(.+\\)\\.\\([a-zA-Z0-9_]+\\)?$" with _ -> s)
2249 let _ = example (filesuffix
"toto.c" = "c")
2250 let _ = example (fileprefix
"toto.c" = "toto")
2253 assert (s = fileprefix s ^ filesuffix s)
2255 let withoutExtension s = global_replace (regexp "\\..*$") "" s
2256 let () = example "without"
2257 (withoutExtension "toto.s.toto" = "toto")
2260 let adjust_ext_if_needed filename ext
=
2261 if String.get ext
0 <> '
.'
2262 then failwith
"I need an extension such as .c not just c";
2264 if not
(filename =~
(".*\\" ^ ext
))
2270 let db_of_filename file
=
2271 dirname file
, basename file
2273 let filename_of_db (basedir
, file
) =
2274 Filename.concat basedir file
2278 let dbe_of_filename file
=
2279 (* raise Invalid_argument if no ext, so safe to use later the unsafe
2280 * fileprefix and filesuffix functions.
2282 ignore
(Filename.chop_extension file
);
2283 Filename.dirname file
,
2284 Filename.basename file
+> fileprefix
,
2285 Filename.basename file
+> filesuffix
2287 let filename_of_dbe (dir
, base
, ext
) =
2288 Filename.concat dir
(base ^
"." ^ ext
)
2291 let dbe_of_filename_safe file
=
2292 try Left
(dbe_of_filename file
)
2293 with Invalid_argument
_ ->
2294 Right
(Filename.dirname file
, Filename.basename file
)
2297 let dbe_of_filename_nodot file
=
2298 let (d
,b,e
) = dbe_of_filename file
in
2299 let d = if d = "." then "" else d in
2306 let replace_ext file oldext newext
=
2307 let (d,b,e
) = dbe_of_filename file
in
2309 filename_of_dbe (d,b,newext
)
2312 let normalize_path file
=
2313 let (dir
, filename) = Filename.dirname file
, Filename.basename file
in
2314 let xs = split "/" dir
in
2315 let rec aux acc
= function
2316 | [] -> List.rev acc
2320 | ".." -> aux (List.tl acc
) xs
2321 | x -> aux (x::acc
) xs
2324 let xs'
= aux [] xs in
2325 Filename.concat (join "/" xs'
) filename
2330 let relative_to_absolute s =
2331 if Filename.is_relative s
2334 let old = Sys.getcwd () in
2336 let current = Sys.getcwd () in
2343 let relative_to_absolute s =
2344 if Filename.is_relative
s
2345 then Sys.getcwd
() ^
"/" ^
s
2350 (* @Pre: prj_path must not contain regexp symbol *)
2351 let filename_without_leading_path prj_path
s =
2352 let prj_path = chop_dirsymbol prj_path in
2353 if s =~
("^" ^
prj_path ^
"/\\(.*\\)$")
2357 (spf "cant find filename_without_project_path: %s %s" prj_path s)
2360 (*****************************************************************************)
2362 (*****************************************************************************)
2371 (*****************************************************************************)
2373 (*****************************************************************************)
2376 | Jan
| Feb
| Mar
| Apr
| May
| Jun
2377 | Jul
| Aug
| Sep
| Oct
| Nov
| Dec
2378 type year
= Year
of int
2379 type day
= Day
of int
2380 type wday
= Sunday
| Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
2382 type date_dmy
= DMY
of day
* month
* year
2384 type hour
= Hour
of int
2385 type minute
= Min
of int
2386 type second
= Sec
of int
2388 type time_hms
= HMS
of hour
* minute
* second
2390 type full_date
= date_dmy
* time_hms
2394 type days
= Days
of int
2396 type time_dmy
= TimeDMY
of day
* month
* year
2399 type float_time
= float
2403 let check_date_dmy (DMY
(day
, month
, year
)) =
2406 let check_time_dmy (TimeDMY
(day
, month
, year
)) =
2409 let check_time_hms (HMS
(x,y,a
)) =
2414 (* ---------------------------------------------------------------------- *)
2417 let int_to_month i =
2418 assert (i <= 12 && i >= 1);
2447 | _ -> raise Impossible
2451 1 , Jan
, "Jan", "January", 31;
2452 2 , Feb
, "Feb", "February", 28;
2453 3 , Mar
, "Mar", "March", 31;
2454 4 , Apr
, "Apr", "April", 30;
2455 5 , May
, "May", "May", 31;
2456 6 , Jun
, "Jun", "June", 30;
2457 7 , Jul
, "Jul", "July", 31;
2458 8 , Aug
, "Aug", "August", 31;
2459 9 , Sep
, "Sep", "September", 30;
2460 10 , Oct
, "Oct", "October", 31;
2461 11 , Nov
, "Nov", "November", 30;
2462 12 , Dec
, "Dec", "December", 31;
2465 let week_day_info = [
2466 0 , Sunday
, "Sun" , "Dim" , "Sunday";
2467 1 , Monday
, "Mon" , "Lun" , "Monday";
2468 2 , Tuesday
, "Tue" , "Mar" , "Tuesday";
2469 3 , Wednesday
, "Wed" , "Mer" , "Wednesday";
2470 4 , Thursday
, "Thu" ,"Jeu" ,"Thursday";
2471 5 , Friday
, "Fri" , "Ven" , "Friday";
2472 6 , Saturday
, "Sat" ,"Sam" , "Saturday";
2476 month_info +> List.map
(fun (i,month
,monthstr
,mlong
,days
) -> i, month
)
2478 month_info +> List.map
(fun (i,month
,monthstr
,mlong
,days
) -> monthstr
, month
)
2479 let slong_to_month_h =
2480 month_info +> List.map
(fun (i,month
,monthstr
,mlong
,days
) -> mlong
, month
)
2482 month_info +> List.map
(fun (i,month
,monthstr
,mlong
,days
) -> month
, monthstr
)
2484 month_info +> List.map
(fun (i,month
,monthstr
,mlong
,days
) -> month
, i)
2487 week_day_info +> List.map
(fun (i,day
,dayen
,dayfr
,daylong
) -> i, day
)
2489 week_day_info +> List.map
(fun (i,day
,dayen
,dayfr
,daylong
) -> day
, dayen
)
2491 week_day_info +> List.map
(fun (i,day
,dayen
,dayfr
,daylong
) -> day
, dayfr
)
2493 let month_of_string s =
2494 List.assoc s s_to_month_h
2496 let month_of_string_long s =
2497 List.assoc s slong_to_month_h
2499 let string_of_month s =
2500 List.assoc s month_to_s_h
2502 let month_of_int i =
2503 List.assoc i i_to_month_h
2505 let int_of_month m =
2506 List.assoc m month_to_i_h
2510 List.assoc i i_to_wday_h
2512 let string_en_of_wday wday
=
2513 List.assoc wday
wday_to_en_h
2514 let string_fr_of_wday wday
=
2515 List.assoc wday
wday_to_fr_h
2517 (* ---------------------------------------------------------------------- *)
2519 let wday_str_of_int ~langage
i =
2520 let wday = wday_of_int i in
2522 | English
-> string_en_of_wday wday
2523 | Francais
-> string_fr_of_wday wday
2524 | Deutsch
-> raise Todo
2528 let string_of_date_dmy (DMY
(Day
n, month
, Year
y)) =
2529 (spf "%02d-%s-%d" n (string_of_month month
) y)
2532 let string_of_unix_time ?
(langage
=English
) tm =
2533 let y = tm.Unix.tm_year
+ 1900 in
2534 let mon = string_of_month (month_of_int (tm.Unix.tm_mon
+ 1)) in
2535 let d = tm.Unix.tm_mday
in
2536 let h = tm.Unix.tm_hour
in
2537 let min = tm.Unix.tm_min
in
2538 let s = tm.Unix.tm_sec
in
2540 let wday = wday_str_of_int ~langage
tm.Unix.tm_wday
in
2542 spf "%02d/%03s/%04d (%s) %02d:%02d:%02d" d mon y wday h min s
2544 (* ex: 21/Jul/2008 (Lun) 21:25:12 *)
2545 let unix_time_of_string s =
2547 ("\\([0-9][0-9]\\)/\\(...\\)/\\([0-9][0-9][0-9][0-9]\\) " ^
2548 "\\(.*\\) \\([0-9][0-9]\\):\\([0-9][0-9]\\):\\([0-9][0-9]\\)")
2550 let (sday
, smonth
, syear
, _sday
, shour
, smin
, ssec
) = matched7 s in
2552 let y = s_to_i syear
- 1900 in
2554 smonth
+> month_of_string +> int_of_month +> (fun i -> i -1)
2557 let tm = Unix.localtime
(Unix.time
()) in
2561 Unix.tm_mday
= s_to_i sday
;
2562 Unix.tm_hour
= s_to_i shour
;
2563 Unix.tm_min
= s_to_i smin
;
2564 Unix.tm_sec
= s_to_i ssec
;
2566 else failwith
("unix_time_of_string: " ^
s)
2570 let short_string_of_unix_time ?
(langage
=English
) tm =
2571 let y = tm.Unix.tm_year
+ 1900 in
2572 let mon = string_of_month (month_of_int (tm.Unix.tm_mon
+ 1)) in
2573 let d = tm.Unix.tm_mday
in
2574 let _h = tm.Unix.tm_hour
in
2575 let _min = tm.Unix.tm_min
in
2576 let _s = tm.Unix.tm_sec
in
2578 let wday = wday_str_of_int ~langage
tm.Unix.tm_wday
in
2580 spf "%02d/%03s/%04d (%s)" d mon y wday
2583 let string_of_unix_time_lfs time
=
2586 (int_to_month (time
.Unix.tm_mon
+ 1))
2587 (time
.Unix.tm_year
+ 1900)
2590 (* ---------------------------------------------------------------------- *)
2591 let string_of_floattime ?langage
i =
2592 let tm = Unix.localtime
i in
2593 string_of_unix_time ?langage
tm
2595 let short_string_of_floattime ?langage
i =
2596 let tm = Unix.localtime
i in
2597 short_string_of_unix_time ?langage
tm
2599 let floattime_of_string s =
2600 let tm = unix_time_of_string s in
2601 let (sec
,_tm
) = Unix.mktime
tm in
2605 (* ---------------------------------------------------------------------- *)
2606 let days_in_week_of_day day
=
2607 let tm = Unix.localtime day
in
2609 let wday = tm.Unix.tm_wday
in
2610 let wday = if wday = 0 then 6 else wday -1 in
2612 let mday = tm.Unix.tm_mday
in
2614 let start_d = mday - wday in
2615 let end_d = mday + (6 - wday) in
2617 enum start_d end_d +> List.map
(fun mday ->
2618 Unix.mktime
{tm with Unix.tm_mday
= mday} +> fst
2621 let first_day_in_week_of_day day
=
2622 List.hd
(days_in_week_of_day day
)
2624 let last_day_in_week_of_day day
=
2625 last (days_in_week_of_day day
)
2628 (* ---------------------------------------------------------------------- *)
2630 (* (modified) copy paste from ocamlcalendar/src/date.ml *)
2632 [| 0; 31; 59; 90; 120; 151; 181; 212; 243; 273; 304; 334(*; 365*) |]
2635 let rough_days_since_jesus (DMY
(Day nday
, month
, Year year
)) =
2638 (days_month.(int_of_month month
-1)) +
2645 let is_more_recent d1 d2
=
2646 let (Days n1
) = rough_days_since_jesus d1
in
2647 let (Days n2
) = rough_days_since_jesus d2
in
2652 if is_more_recent d1 d2
2657 if is_more_recent d1 d2
2662 let maximum_dmy ds
=
2665 let minimum_dmy ds
=
2670 let rough_days_between_dates d1 d2
=
2671 let (Days n1
) = rough_days_since_jesus d1
in
2672 let (Days n2
) = rough_days_since_jesus d2
in
2676 (rough_days_between_dates
2677 (DMY
(Day
7, Jan
, Year
1977))
2678 (DMY
(Day
13, Jan
, Year
1977)) = Days
6)
2680 (* because of rough days, it is a bit buggy, here it should return 1 *)
2682 let _ = assert_equal
2683 (rough_days_between_dates
2684 (DMY (Day 29, Feb, Year 1977))
2685 (DMY (Day 1, Mar , Year 1977)))
2690 (* from julia, in gitsort.ml *)
2694 [(1,31);(2,28);(3,31);(4,30);(5,31); (6,6);(7,7);(8,31);(9,30);(10,31);
2695 (11,30);(12,31);(0,31)]
2697 let normalize (year,month,day,hour,minute,second) =
2700 let (day,hour) = (day - 1,hour + 24) in
2703 let month = month - 1 in
2704 let day = List.assoc month antimonths in
2706 if month = 2 && year / 4 * 4 = year && not (year / 100 * 100 = year)
2710 then (year-1,12,day,hour,minute,second)
2711 else (year,month,day,hour,minute,second)
2712 else (year,month,day,hour,minute,second)
2713 else (year,month,day,hour,minute,second)
2718 let mk_date_dmy day month year
=
2719 let date = DMY
(Day
day, month_of_int month, Year year
) in
2720 (* check_date_dmy date *)
2724 (* ---------------------------------------------------------------------- *)
2725 (* conversion to unix.tm *)
2727 let dmy_to_unixtime (DMY
(Day
n, month, Year year
)) =
2729 Unix.tm_sec
= 0; (** Seconds 0..60 *)
2730 tm_min
= 0; (** Minutes 0..59 *)
2731 tm_hour
= 12; (** Hours 0..23 *)
2732 tm_mday
= n; (** Day of month 1..31 *)
2733 tm_mon
= (int_of_month month -1); (** Month of year 0..11 *)
2734 tm_year
= year
- 1900; (** Year - 1900 *)
2735 tm_wday
= 0; (** Day of week (Sunday is 0) *)
2736 tm_yday
= 0; (** Day of year 0..365 *)
2737 tm_isdst
= false; (** Daylight time savings in effect *)
2741 let unixtime_to_dmy tm =
2742 let n = tm.Unix.tm_mday
in
2743 let month = month_of_int (tm.Unix.tm_mon
+ 1) in
2744 let year = tm.Unix.tm_year
+ 1900 in
2746 DMY
(Day
n, month, Year
year)
2749 let unixtime_to_floattime tm =
2750 Unix.mktime
tm +> fst
2753 let sec_to_days sec
=
2754 let minfactor = 60 in
2755 let hourfactor = 60 * 60 in
2756 let dayfactor = 60 * 60 * 24 in
2758 let days = sec
/ dayfactor in
2759 let hours = (sec
mod dayfactor) / hourfactor in
2760 let mins = (sec
mod hourfactor) / minfactor in
2761 let sec = (sec mod 60) in
2762 (* old: Printf.sprintf "%d days, %d hours, %d minutes" days hours mins *)
2763 (if days > 0 then plural days "day" ^
" " else "") ^
2764 (if hours > 0 then plural hours "hour" ^
" " else "") ^
2765 (if mins > 0 then plural mins "min" ^
" " else "") ^
2768 let sec_to_hours sec =
2769 let minfactor = 60 in
2770 let hourfactor = 60 * 60 in
2772 let hours = sec / hourfactor in
2773 let mins = (sec mod hourfactor) / minfactor in
2774 let sec = (sec mod 60) in
2775 (* old: Printf.sprintf "%d days, %d hours, %d minutes" days hours mins *)
2776 (if hours > 0 then plural hours "hour" ^
" " else "") ^
2777 (if mins > 0 then plural mins "min" ^
" " else "") ^
2782 let test_date_1 () =
2783 let date = DMY
(Day
17, Sep
, Year
1991) in
2784 let float, tm = dmy_to_unixtime date in
2785 pr2 (spf "date: %.0f" float);
2789 (* src: ferre in logfun/.../date.ml *)
2791 let day_secs : float = 86400.
2793 let today : unit -> float = fun () -> (Unix.time
() )
2794 let yesterday : unit -> float = fun () -> (Unix.time
() -. day_secs)
2795 let tomorrow : unit -> float = fun () -> (Unix.time
() +. day_secs)
2797 let lastweek : unit -> float = fun () -> (Unix.time
() -. (7.0 *. day_secs))
2798 let lastmonth : unit -> float = fun () -> (Unix.time
() -. (30.0 *. day_secs))
2801 let week_before : float_time
-> float_time
= fun d ->
2802 (d -. (7.0 *. day_secs))
2803 let month_before : float_time
-> float_time
= fun d ->
2804 (d -. (30.0 *. day_secs))
2806 let week_after : float_time
-> float_time
= fun d ->
2807 (d +. (7.0 *. day_secs))
2811 (*****************************************************************************)
2812 (* Lines/words/strings *)
2813 (*****************************************************************************)
2816 * let (list_of_string: string -> char list) = fun s ->
2817 * (enum 0 ((String.length s) - 1) +> List.map (String.get s))
2820 let _ = example (list_of_string
"abcd" = ['a'
;'
b'
;'
c'
;'
d'
])
2823 let rec (list_of_stream: ('a Stream.t) -> 'a list) =
2825 | [< 'c ; stream >] -> c :: list_of_stream stream
2828 let (list_of_string: string -> char list) =
2829 Stream.of_string $ list_of_stream
2833 * let (lines: string -> string list) = fun s -> ...
2836 let (lines_with_nl
: string -> string list
) = fun s ->
2837 let rec lines_aux = function
2839 | [x] -> if x = "" then [] else [x ^
"\n"] (* old: [x] *)
2844 (time_func (fun () -> Str.split_delim
(Str.regexp "\n") s)) +> lines_aux
2846 (* in fact better make it return always complete lines, simplify *)
2847 (* Str.split, but lines "\n1\n2\n" dont return the \n and forget the first \n => split_delim better than split *)
2848 (* +> List.map (fun s -> s ^ "\n") but add an \n even at the end => lines_aux *)
2850 let chars = list_of_string s in
2851 chars +> List.fold_left (fun (acc, lines) char ->
2852 let newacc = acc ^ (String.make 1 char) in
2854 then ("", newacc::lines)
2855 else (newacc, lines)
2857 +> (fun (s, lines) -> List.rev (s::lines))
2860 (* CHECK: unlines (lines x) = x *)
2861 let (unlines
: string list
-> string) = fun s ->
2862 (String.concat "\n" s) ^
"\n"
2863 let (words
: string -> string list
) = fun s ->
2864 Str.split (Str.regexp "[ \t()\";]+") s
2865 let (unwords
: string list
-> string) = fun s ->
2868 let (split_space
: string -> string list
) = fun s ->
2869 Str.split (Str.regexp "[ \t\n]+") s
2874 lines s +> List.length
2875 let _ = example (nblines "" = 0)
2876 let _ = example (nblines "toto" = 1)
2877 let _ = example (nblines "toto\n" = 1)
2878 let _ = example (nblines "toto\ntata" = 2)
2879 let _ = example (nblines "toto\ntata\n" = 2)
2881 (*****************************************************************************)
2883 (*****************************************************************************)
2885 let chan = open_in file
in
2886 let rec cat_orig_aux () =
2888 (* cant do input_line chan::aux() cos ocaml eval from right to left ! *)
2889 let l = input_line
chan in
2890 l :: cat_orig_aux ()
2891 with End_of_file
-> [] in
2894 (* tail recursive efficient version *)
2896 let chan = open_in file
in
2897 let rec cat_aux acc
() =
2898 (* cant do input_line chan::aux() cos ocaml eval from right to left ! *)
2899 let (b, l) = try (true, input_line
chan) with End_of_file
-> (false, "") in
2901 then cat_aux (l::acc
) ()
2904 cat_aux [] () +> List.rev
+> (fun x -> close_in
chan; x)
2906 let interpolate str
=
2908 command2 ("printf \"%s\\n\" " ^ str ^
">/tmp/caml");
2912 (* could do a print_string but printf dont like print_string *)
2913 let echo s = printf
"%s" s; flush stdout
; s
2915 let usleep s = for i = 1 to s do () done
2917 let sleep_little () =
2920 (*ignore(Sys.command ("usleep " ^ !_sleep_time))*)
2924 * let command2 s = ignore(Sys.command s)
2928 let pid = Unix.fork
() in
2932 (* Unix.setsid(); *)
2933 Sys.set_signal
Sys.sigint
(Sys.Signal_handle
(fun _ ->
2935 Unix.kill
0 Sys.sigkill
;
2943 let process_output_to_list2 = fun command
->
2944 let chan = Unix.open_process_in command
in
2945 let res = ref ([] : string list
) in
2946 let rec process_otl_aux () =
2947 let e = input_line
chan in
2949 process_otl_aux() in
2950 try process_otl_aux ()
2952 let stat = Unix.close_process_in
chan in (List.rev
!res,stat)
2953 let cmd_to_list command
=
2954 let (l,_) = process_output_to_list2 command
in l
2955 let process_output_to_list = cmd_to_list
2956 let cmd_to_list_and_status = process_output_to_list2
2959 * let command2 s = ignore(Sys.command s)
2962 let command2_y_or_no cmd
=
2963 pr2 (cmd ^
" [y/n] ?");
2964 match read_line
() with
2965 | "y" | "yes" | "Y" -> command2 cmd
; true
2966 | "n" | "no" | "N" -> false
2967 | _ -> failwith
"answer by yes or no"
2972 let mkdir ?
(mode
=0o770
) file
=
2973 Unix.mkdir file mode
2975 let read_file_orig file
= cat file
+> unlines
2976 let read_file file
=
2977 let ic = open_in file
in
2978 let size = in_channel_length
ic in
2979 let buf = String.create
size in
2980 really_input
ic buf 0 size;
2985 let write_file ~file
s =
2986 let chan = open_out file
in
2987 (output_string
chan s; close_out
chan)
2990 (Unix.stat file
).Unix.st_size
2992 let filemtime file
=
2993 (Unix.stat file
).Unix.st_mtime
2995 (* opti? use wc -l ? *)
2996 let nblines_file file
=
2997 cat file
+> List.length
2999 let lfile_exists filename =
3001 (match (Unix.lstat
filename).Unix.st_kind
with
3002 | (Unix.S_REG
| Unix.S_LNK
) -> true
3005 with Unix.Unix_error
(Unix.ENOENT
, _, _) -> false
3007 let is_directory file
=
3008 (Unix.stat file
).Unix.st_kind
= Unix.S_DIR
3011 (* src: from chailloux et al book *)
3012 let capsule_unix f args =
3014 with Unix.Unix_error
(e, fm
, argm
) ->
3015 log (Printf.sprintf
"exn Unix_error: %s %s %s\n" (Unix.error_message
e) fm argm
)
3018 let (readdir_to_kind_list
: string -> Unix.file_kind
-> string list
) =
3022 +> List.filter
(fun s ->
3024 let stat = Unix.lstat
(path ^
"/" ^
s) in
3025 stat.Unix.st_kind
= kind
3027 pr2 ("EXN pb stating file: " ^
s);
3031 let (readdir_to_dir_list
: string -> string list
) = fun path
->
3032 readdir_to_kind_list path
Unix.S_DIR
3034 let (readdir_to_file_list
: string -> string list
) = fun path
->
3035 readdir_to_kind_list path
Unix.S_REG
3037 let (readdir_to_link_list
: string -> string list
) = fun path
->
3038 readdir_to_kind_list path
Unix.S_LNK
3041 let (readdir_to_dir_size_list
: string -> (string * int) list
) = fun path
->
3044 +> map_filter (fun s ->
3045 let stat = Unix.lstat
(path ^
"/" ^
s) in
3046 if stat.Unix.st_kind
= Unix.S_DIR
3047 then Some
(s, stat.Unix.st_size
)
3051 (* could be in control section too *)
3053 (* Why a use_cache argument ? because sometimes want disable it but dont
3054 * want put the cache_computation funcall in comment, so just easier to
3055 * pass this extra option.
3057 let cache_computation2 ?
(verbose
=false) ?
(use_cache
=true) file ext_cache
f =
3061 if not
(Sys.file_exists file
)
3062 then failwith
("can't find: " ^ file
);
3063 let file_cache = (file ^ ext_cache
) in
3064 if Sys.file_exists
file_cache &&
3065 filemtime file_cache >= filemtime file
3067 if verbose
then pr2 ("using cache: " ^
file_cache);
3068 get_value file_cache
3072 write_value res file_cache;
3076 let cache_computation ?verbose ?use_cache a
b c =
3077 profile_code "Common.cache_computation" (fun () ->
3078 cache_computation2 ?verbose ?use_cache a
b c)
3081 let cache_computation_robust2
3083 (need_no_changed_files
, need_no_changed_variables
) ext_depend
3085 if not
(Sys.file_exists file
)
3086 then failwith
("can't find: " ^ file
);
3088 let file_cache = (file ^ ext_cache
) in
3089 let dependencies_cache = (file ^ ext_depend
) in
3092 (* could do md5sum too *)
3093 ((file
::need_no_changed_files
) +> List.map
(fun f -> f, filemtime f),
3094 need_no_changed_variables
)
3097 if Sys.file_exists
dependencies_cache &&
3098 get_value dependencies_cache = dependencies
3099 then get_value file_cache
3101 pr2 ("cache computation recompute " ^ file
);
3103 write_value dependencies dependencies_cache;
3104 write_value res file_cache;
3108 let cache_computation_robust a
b c d e =
3109 profile_code "Common.cache_computation_robust" (fun () ->
3110 cache_computation_robust2 a
b c d e)
3115 (* dont forget that cmd_to_list call bash and so pattern may contain
3116 * '*' symbols that will be expanded, so can do glob "*.c"
3119 cmd_to_list ("ls -1 " ^ pattern
)
3122 (* update: have added the -type f, so normally need less the sanity_check_xxx
3124 let files_of_dir_or_files ext
xs =
3125 xs +> List.map
(fun x ->
3127 then cmd_to_list ("find " ^
x ^
" -noleaf -type f -name \"*." ^ext^
"\"")
3132 let files_of_dir_or_files_no_vcs ext
xs =
3133 xs +> List.map
(fun x ->
3137 ("find " ^
x ^
" -noleaf -type f -name \"*." ^ext^
"\"" ^
3138 "| grep -v /.hg/ |grep -v /CVS/ | grep -v /.git/ |grep -v /_darcs/"
3144 let files_of_dir_or_files_no_vcs_post_filter regex
xs =
3145 xs +> List.map
(fun x ->
3150 " -noleaf -type f | grep -v /.hg/ |grep -v /CVS/ | grep -v /.git/"
3152 +> List.filter
(fun s -> s =~ regex
)
3157 let sanity_check_files_and_adjust ext files
=
3158 let files = files +> List.filter
(fun file
->
3159 if not
(file
=~
(".*\\."^ext
))
3161 pr2 ("warning: seems not a ."^ext^
" file");
3165 if is_directory file
3167 pr2 (spf "warning: %s is a directory" file
);
3177 (* taken from mlfuse, the predecessor of ocamlfuse *)
3178 type rwx
= [`R
|`W
|`X
] list
3179 let file_perm_of : u
:rwx
-> g
:rwx
-> o:rwx
-> Unix.file_perm
=
3182 List.fold_left
(fun acc p
-> acc
lor ((function `R
-> 4 | `W
-> 2 | `X
-> 1) p
)) 0 l in
3184 ((to_oct u
) lsl 6) lor
3185 ((to_oct g
) lsl 3) lor
3194 let _ = Sys.getenv var
in true
3195 with Not_found
-> false
3197 (* emacs/lisp inspiration (eric cooper and yaron minsky use that too) *)
3198 let (with_open_outfile
: filename -> (((string -> unit) * out_channel
) -> 'a
) -> 'a
) =
3200 let chan = open_out file
in
3201 let pr s = output_string
chan s in
3202 unwind_protect (fun () ->
3203 let res = f (pr, chan) in
3206 (fun e -> close_out
chan)
3208 let (with_open_infile
: filename -> ((in_channel
) -> 'a
) -> 'a
) = fun file
f ->
3209 let chan = open_in file
in
3210 unwind_protect (fun () ->
3214 (fun e -> close_in
chan)
3217 let (with_open_outfile_append
: filename -> (((string -> unit) * out_channel
) -> 'a
) -> 'a
) =
3219 let chan = open_out_gen
[Open_creat
;Open_append
] 0o666 file
in
3220 let pr s = output_string
chan s in
3221 unwind_protect (fun () ->
3222 let res = f (pr, chan) in
3225 (fun e -> close_out
chan)
3232 (* it seems that the toplevel block such signals, even with this explicit
3234 * let _ = Unix.sigprocmask Unix.SIG_UNBLOCK [Sys.sigalrm]
3237 (* could be in Control section *)
3239 (* subtil: have to make sure that timeout is not intercepted before here, so
3240 * avoid exn handle such as try (...) with _ -> cos timeout will not bubble up
3241 * enough. In such case, add a case before such as
3242 * with Timeout -> raise Timeout | _ -> ...
3244 * question: can we have a signal and so exn when in a exn handler ?
3246 let timeout_function timeoutval
= fun f ->
3249 Sys.set_signal
Sys.sigalrm
(Sys.Signal_handle
(fun _ -> raise Timeout
));
3250 ignore
(Unix.alarm timeoutval
);
3252 ignore
(Unix.alarm
0);
3257 log "timeout (we abort)";
3261 (* subtil: important to disable the alarm before relaunching the exn,
3262 * otherwise the alarm is still running.
3264 * robust?: and if alarm launched after the log (...) ?
3265 * Maybe signals are disabled when process an exception handler ?
3268 ignore
(Unix.alarm
0);
3269 (* log ("exn while in transaction (we abort too, even if ...) = " ^
3270 Printexc.to_string e);
3272 log "exn while in timeout_function";
3276 let timeout_function_opt timeoutvalopt
f =
3277 match timeoutvalopt
with
3279 | Some
x -> timeout_function x f
3283 (* creation of tmp files, a la gcc *)
3285 let _temp_files_created = ref []
3287 (* ex: new_temp_file "cocci" ".c" will give "/tmp/cocci-3252-434465.c" *)
3288 let new_temp_file prefix suffix
=
3289 let processid = i_to_s (Unix.getpid
()) in
3290 let tmp_file = Filename.temp_file
(prefix ^
"-" ^
processid ^
"-") suffix
in
3291 push2 tmp_file _temp_files_created;
3295 let save_tmp_files = ref false
3296 let erase_temp_files () =
3297 if not
!save_tmp_files then begin
3298 !_temp_files_created +> List.iter
(fun s ->
3299 (* pr2 ("erasing: " ^ s); *)
3300 command2 ("rm -f " ^
s)
3302 _temp_files_created := []
3305 (* now in prelude: exception UnixExit of int *)
3306 let exn_to_real_unixexit f =
3308 with UnixExit
x -> exit
x
3316 (*****************************************************************************)
3318 (*****************************************************************************)
3321 let uncons l = (List.hd
l, List.tl
l)
3324 let safe_tl l = try List.tl
l with _ -> []
3332 | ([],_) -> failwith
"zip: not same length"
3333 | (_,[]) -> failwith
"zip: not same length"
3334 | (x::xs,y::ys
) -> (x,y)::zip xs ys
3336 let rec zip_safe xs ys
=
3340 | (x::xs,y::ys
) -> (x,y)::zip_safe xs ys
3343 List.fold_right
(fun e (xs, ys
) ->
3344 (fst e::xs), (snd e::ys
)) zs
([],[])
3348 * let rec take n xs =
3351 * | (_,[]) -> failwith "take: not enough"
3352 * | (n,x::xs) -> x::take (n-1) xs
3355 let rec take_safe n xs =
3359 | (n,x::xs) -> x::take_safe (n-1) xs
3361 let rec take_until p
= function
3363 | x::xs -> if p
x then [] else x::(take_until p
xs)
3365 let take_while p
= take_until (p $ not
)
3368 (* now in prelude: let rec drop n xs = ... *)
3369 let _ = example (drop 3 [1;2;3;4] = [4])
3371 let rec drop_while p
= function
3373 | x::xs -> if p
x then drop_while p
xs else x::xs
3376 let rec drop_until p
xs =
3377 drop_while (fun x -> not
(p
x)) xs
3378 let _ = example (drop_until (fun x -> x = 3) [1;2;3;4;5] = [3;4;5])
3381 let span p
xs = (take_while p
xs, drop_while p
xs)
3384 let rec (span: ('a
-> bool) -> 'a list
-> 'a list
* 'a list
) =
3389 let (l1
, l2
) = span p
xs in
3392 let _ = example ((span (fun x -> x <= 3) [1;2;3;4;1;2] = ([1;2;3],[4;1;2])))
3394 let rec groupBy eq
l =
3398 let (xs1
,xs2
) = List.partition
(fun x'
-> eq
x x'
) xs in
3399 (x::xs1
)::(groupBy eq xs2
)
3401 let (exclude_but_keep_attached
: ('a
-> bool) -> 'a list
-> ('a
* 'a list
) list
)=
3403 let rec aux_filter acc
= function
3404 | [] -> [] (* drop what was accumulated because nothing to attach to *)
3407 then aux_filter (x::acc
) xs
3408 else (x, List.rev acc
)::aux_filter [] xs
3412 (exclude_but_keep_attached
(fun x -> x = 3) [3;3;1;3;2;3;3;3] =
3413 [(1,[3;3]);(2,[3])])
3415 let (group_by_post
: ('a
-> bool) -> 'a list
-> ('a list
* 'a
) list
* 'a list
)=
3417 let rec aux_filter grouped_acc acc
= function
3419 List.rev grouped_acc
, List.rev acc
3423 aux_filter ((List.rev acc
,x)::grouped_acc
) [] xs
3425 aux_filter grouped_acc
(x::acc
) xs
3430 (group_by_post
(fun x -> x = 3) [1;1;3;2;3;4;5;3;6;6;6] =
3431 ([([1;1],3);([2],3);[4;5],3], [6;6;6]))
3435 let rec (split_when
: ('a
-> bool) -> 'a list
-> 'a list
* 'a
* 'a list
) =
3437 | [] -> raise Not_found
3442 let (l1
, a
, l2
) = split_when p
xs in
3444 let _ = example (split_when
(fun x -> x = 3) [1;2;3;4;1;2] = ([1;2],3,[4;1;2]))
3447 (* not so easy to come up with ... used in aComment for split_paragraph *)
3448 let rec split_gen_when_aux f acc
xs =
3455 (match f (x::xs) with
3457 split_gen_when_aux f (x::acc
) xs
3459 let before = List.rev acc
in
3461 then split_gen_when_aux f [] rest
3462 else before::split_gen_when_aux f [] rest
3464 (* could avoid introduce extra aux function by using ?(acc = []) *)
3465 let split_gen_when f xs =
3466 split_gen_when_aux f [] xs
3470 (* generate exception (Failure "tl") if there is no element satisfying p *)
3471 let rec (skip_until
: ('a list
-> bool) -> 'a list
-> 'a list
) = fun p
xs ->
3472 if p
xs then xs else skip_until p
(List.tl
xs)
3473 let _ = example (skip_until
(function 1::2::xs -> true | _ -> false) [1;3;4;1;2;4;5] = [1;2;4;5])
3475 let rec skipfirst e = function
3477 | e'
::l when e = e'
-> skipfirst e l
3482 * let rec enum x n = ...
3487 if xs = [] then [] (* enum 0 (-1) generate an exception *)
3488 else zip xs (enum 0 ((List.length
xs) -1))
3490 let index_list_and_total xs =
3491 let total = List.length
xs in
3492 if xs = [] then [] (* enum 0 (-1) generate an exception *)
3493 else zip xs (enum 0 ((List.length
xs) -1))
3494 +> List.map
(fun (a
,b) -> (a
,b,total))
3496 let index_list_1 xs =
3497 xs +> index_list +> List.map
(fun (x,i) -> x, i+1)
3499 let or_list = List.fold_left
(||) false
3500 let and_list = List.fold_left
(&&) true
3503 let sum = sum_int xs in
3504 (float_of_int
sum) /. (float_of_int
(List.length
xs))
3506 let snoc x xs = xs @ [x]
3507 let cons x xs = x::xs
3509 let head_middle_tail xs =
3513 let reversed = List.rev
(y::xs) in
3514 let tail = List.hd
reversed in
3515 let middle = List.rev
(List.tl
reversed) in
3517 | _ -> failwith
"head_middle_tail, too small list"
3519 let _ = assert_equal (head_middle_tail [1;2;3]) (1, [2], 3)
3520 let _ = assert_equal (head_middle_tail [1;3]) (1, [], 3)
3526 (* let (++) = (@), could do that, but if load many times the common, then pb *)
3527 (* let (++) l1 l2 = List.fold_right (fun x acc -> x::acc) l1 l2 *)
3530 let newxs = List.filter
(fun y -> y <> x) xs in
3531 assert (List.length
newxs = List.length
xs - 1);
3536 List.filter
(fun x -> not
(p
x)) xs
3541 let fold_k f lastk acc
xs =
3542 let rec fold_k_aux acc
= function
3545 f acc
x (fun acc
-> fold_k_aux acc
xs)
3550 let rec list_init = function
3551 | [] -> raise Not_found
3553 | x::y::xs -> x::(list_init (y::xs))
3555 let rec list_last = function
3556 | [] -> raise Not_found
3558 | x::y::xs -> list_last (y::xs)
3562 * let last_n n l = List.rev (take n (List.rev l))
3563 * let last l = List.hd (last_n 1 l)
3566 let rec join_gen a
= function
3569 | x::xs -> x::a
::(join_gen a
xs)
3572 (* todo: foldl, foldr (a more consistent foldr) *)
3575 let iter_index f l =
3576 let rec iter_ n = function
3578 | e::l -> f e n ; iter_ (n+1) l
3582 let rec map_ n = function
3584 | e::l -> f e n :: map_ (n+1) l
3589 let filter_index f l =
3590 let rec filt i = function
3592 | e::l -> if f i e then e :: filt (i+1) l else filt (i+1) l
3597 let do_withenv doit
f env
l =
3598 let r_env = ref env
in
3599 let l'
= doit
(fun e ->
3600 let e'
, env'
= f !r_env e in
3606 * let fold_left_with_index f acc = ...
3609 let map_withenv f env
e = do_withenv List.map
f env
e
3611 let rec collect_accu f accu
= function
3613 | e::l -> collect_accu f (List.rev_append
(f e) accu
) l
3615 let collect f l = List.rev
(collect_accu f [] l)
3617 (* cf also List.partition *)
3619 let rec fpartition p
l =
3620 let rec part yes no
= function
3621 | [] -> (List.rev yes
, List.rev no
)
3624 | None
-> part yes
(x :: no
) l
3625 | Some
v -> part (v :: yes
) no
l) in
3630 let rec removelast = function
3631 | [] -> failwith
"removelast"
3633 | e::l -> e :: removelast l
3635 let remove x = List.filter
(fun y -> y != x)
3636 let empty list
= list
= []
3639 let rec inits = function
3641 | e::l -> [] :: List.map
(fun l -> e::l) (inits l)
3643 let rec tails = function
3645 | (_::xs) as xxs
-> xxs
:: tails xs
3648 let reverse = List.rev
3652 let fold_left = List.fold_left
3653 let rev_map = List.rev_map
3656 let rec fold_right1 f = function
3657 | [] -> failwith
"fold_right1"
3659 | e::l -> f e (fold_right1 f l)
3661 let maximum l = foldl1 max
l
3662 let minimum l = foldl1 min l
3664 (* do a map tail recursive, and result is reversed, it is a tail recursive map => efficient *)
3665 let map_eff_rev = fun f l ->
3666 let rec map_eff_aux acc
=
3669 | x::xs -> map_eff_aux ((f x)::acc
) xs
3674 let rec loop acc
= function
3676 | x::xs -> loop ((f x)::acc
) xs in
3680 let rec (generate
: int -> 'a
-> 'a list
) = fun i el
->
3682 else el
::(generate
(i-1) el
)
3684 let rec uniq = function
3686 | e::l -> if List.mem
e l then uniq l else e :: uniq l
3688 let rec all_assoc e = function
3690 | (e'
,v) :: l when e=e'
-> v :: all_assoc e l
3691 | _ :: l -> all_assoc e l
3693 let prepare_want_all_assoc l =
3694 List.map
(fun n -> n, uniq (all_assoc n l)) (uniq (List.map
fst l))
3696 let rotate list
= List.tl list
++ [(List.hd list
)]
3698 let or_list = List.fold_left (||) false
3699 let and_list = List.fold_left (&&) true
3701 let rec (return_when
: ('a
-> '
b option) -> 'a list
-> '
b) = fun p
-> function
3702 | [] -> raise Not_found
3703 | x::xs -> (match p
x with None
-> return_when p
xs | Some
b -> b)
3705 let rec splitAt n xs =
3706 if n = 0 then ([],xs)
3710 | (x::xs) -> let (a
,b) = splitAt (n-1) xs in (x::a
, b)
3714 let rec pack_aux l i = function
3715 | [] -> failwith
"not on a boundary"
3716 | [x] -> if i = n then [l++[x]] else failwith
"not on a boundary"
3719 then (l++[x])::(pack_aux [] 1 xs)
3720 else pack_aux (l++[x]) (i+1) xs
3724 let min_with f = function
3725 | [] -> raise Not_found
3727 let rec min_with_ min_val min_elt
= function
3732 then min_with_ val_ e l
3733 else min_with_ min_val min_elt
l
3734 in min_with_ (f e) e l
3736 let two_mins_with f = function
3738 let rec min_with_ min_val min_elt min_val2 min_elt2
= function
3739 | [] -> min_elt
, min_elt2
3745 then min_with_ val_ e min_val min_elt
l
3746 else min_with_ min_val min_elt
val_ e l
3747 else min_with_ min_val min_elt min_val2 min_elt2
l
3751 if v1 < v2 then min_with_ v1 e1
v2 e2
l else min_with_ v2 e2
v1 e1
l
3752 | _ -> raise Not_found
3754 let grep_with_previous f = function
3757 let rec grep_with_previous_ previous
= function
3759 | e::l -> if f previous
e then e :: grep_with_previous_ e l else grep_with_previous_ previous
l
3760 in e :: grep_with_previous_ e l
3762 let iter_with_previous f = function
3765 let rec iter_with_previous_ previous
= function
3767 | e::l -> f previous
e ; iter_with_previous_ e l
3768 in iter_with_previous_ e l
3771 let iter_with_before_after f xs =
3772 let rec aux before_rev after
=
3777 aux (x::before_rev
) xs
3783 (* kind of cartesian product of x*x *)
3784 let rec (get_pair
: ('a list
) -> (('a
* 'a
) list
)) = function
3786 | x::xs -> (List.map
(fun y -> (x,y)) xs) ++ (get_pair
xs)
3789 (* retourne le rang dans une liste d'un element *)
3790 let rang elem liste
=
3791 let rec rang_rec elem accu
= function
3792 | [] -> raise Not_found
3793 | a
::l -> if a
= elem
then accu
3794 else rang_rec elem
(accu
+1) l in
3795 rang_rec elem
1 liste
3797 (* retourne vrai si une liste contient des doubles *)
3798 let rec doublon = function
3800 | a
::l -> if List.mem a
l then true
3803 let rec (insert_in
: 'a
-> 'a list
-> 'a list list
) = fun x -> function
3805 | y::ys
-> (x::y::ys
) :: (List.map
(fun xs -> y::xs) (insert_in
x ys
))
3806 (* insert_in 3 [1;2] = [[3; 1; 2]; [1; 3; 2]; [1; 2; 3]] *)
3808 let rec (permutation
: 'a list
-> 'a list list
) = function
3811 | x::xs -> List.flatten
(List.map
(insert_in
x) (permutation
xs))
3812 (* permutation [1;2;3] =
3813 * [[1; 2; 3]; [2; 1; 3]; [2; 3; 1]; [1; 3; 2]; [3; 1; 2]; [3; 2; 1]]
3817 let rec remove_elem_pos pos
xs =
3818 match (pos
, xs) with
3819 | _, [] -> failwith
"remove_elem_pos"
3821 | n, x::xs -> x::(remove_elem_pos (n-1) xs)
3823 let rec insert_elem_pos (e, pos
) xs =
3824 match (pos
, xs) with
3826 | n, x::xs -> x::(insert_elem_pos (e, (n-1)) xs)
3827 | n, [] -> failwith
"insert_elem_pos"
3829 let rec uncons_permut xs =
3830 let indexed = index_list xs in
3831 indexed +> List.map
(fun (x, pos
) -> (x, pos
), remove_elem_pos pos
xs)
3834 (uncons_permut ['a'
;'
b'
;'
c'
] =
3835 [('a'
, 0), ['
b'
;'
c'
];
3836 ('
b'
, 1), ['a'
;'
c'
];
3840 let rec uncons_permut_lazy xs =
3841 let indexed = index_list xs in
3842 indexed +> List.map
(fun (x, pos
) ->
3844 lazy (remove_elem_pos pos
xs)
3851 let rec map_flatten f l =
3852 let rec map_flatten_aux accu
= function
3854 | e :: l -> map_flatten_aux (List.rev (f e) ++ accu
) l
3855 in List.rev (map_flatten_aux [] l)
3858 let rec repeat e n =
3859 let rec repeat_aux acc
= function
3861 | n when n < 0 -> failwith
"repeat"
3862 | n -> repeat_aux (e::acc
) (n-1) in
3865 let rec map2 f = function
3867 | x::xs -> let r = f x in r::map2 f xs
3870 let rec map3_aux acc
= function
3872 | x::xs -> map3_aux (f x::acc
) xs in
3876 let tails2 xs = map rev (inits (rev xs))
3877 let res = tails2 [1;2;3;4]
3878 let res = tails [1;2;3;4]
3882 let pack_sorted same
xs =
3883 let rec pack_s_aux acc
xs =
3885 | ((cur,rest
),[]) -> cur::rest
3886 | ((cur,rest
), y::ys
) ->
3887 if same
(List.hd
cur) y then pack_s_aux (y::cur, rest
) ys
3888 else pack_s_aux ([y], cur::rest
) ys
3889 in pack_s_aux ([List.hd
xs],[]) (List.tl
xs) +> List.rev
3890 let test = pack_sorted (=) [1;1;1;2;2;3;4]
3893 let rec keep_best f =
3894 let rec partition e = function
3898 | None
-> let (e''
, l'
) = partition e l in e''
, e'
:: l'
3899 | Some
e''
-> partition e''
l
3903 let (e'
, l'
) = partition e l in
3904 e'
:: keep_best f l'
3906 let rec sorted_keep_best f = function
3911 | None
-> a
:: sorted_keep_best f (b :: l)
3912 | Some
e -> sorted_keep_best f (e :: l)
3916 let (cartesian_product
: 'a list
-> '
b list
-> ('a
* '
b) list
) = fun xs ys
->
3917 xs +> List.map
(fun x -> ys
+> List.map
(fun y -> (x,y)))
3920 let _ = assert_equal
3921 (cartesian_product
[1;2] ["3";"4";"5"])
3922 [1,"3";1,"4";1,"5"; 2,"3";2,"4";2,"5"]
3924 (*----------------------------------*)
3926 (* sur surEnsemble [p1;p2] [[p1;p2;p3] [p1;p2] ....] -> [[p1;p2;p3] ... *)
3927 (* mais pas p2;p3 *)
3929 let surEnsemble liste_el liste_liste_el
=
3931 (function liste_elbis
->
3932 List.for_all
(function el
-> List.mem el liste_elbis
) liste_el
3937 (*----------------------------------*)
3938 (* combinaison/product/.... (aop) *)
3939 (* 123 -> 123 12 13 23 1 2 3 *)
3940 let rec realCombinaison = function
3944 let res = realCombinaison l in
3945 let res2 = List.map
(function x -> a
::x) res in
3946 res2 ++ res ++ [[a
]]
3948 (* genere toutes les combinaisons possible de paire *)
3949 (* par exemple combinaison [1;2;4] -> [1, 2; 1, 4; 2, 4] *)
3950 let rec combinaison = function
3954 | a
::b::l -> (List.map
(function elem
-> (a
, elem
)) (b::l)) ++
3955 (combinaison (b::l))
3957 (*----------------------------------*)
3959 (* list of list(aop) *)
3960 (* insere elem dans la liste de liste (si elem est deja present dans une de *)
3961 (* ces listes, on ne fait rien *)
3962 let rec insere elem
= function
3965 if (List.mem elem a
) then a
::l
3966 else a
::(insere elem
l)
3968 let rec insereListeContenant lis el
= function
3971 if List.mem el a
then
3972 (List.append lis a
)::l
3973 else a
::(insereListeContenant lis el
l)
3975 (* fusionne les listes contenant et1 et et2 dans la liste de liste*)
3976 let rec fusionneListeContenant (et1
, et2
) = function
3977 | [] -> [[et1
; et2
]]
3979 (* si les deux sont deja dedans alors rien faire *)
3980 if List.mem et1 a
then
3981 if List.mem et2 a
then a
::l
3983 insereListeContenant a et2
l
3984 else if List.mem et2 a
then
3985 insereListeContenant a et1
l
3986 else a
::(fusionneListeContenant (et1
, et2
) l)
3988 (*****************************************************************************)
3990 (*****************************************************************************)
3992 let array_find_index f a
=
3993 let rec array_find_index_ i =
3994 if f a
.(i) then i else array_find_index_ (i+1)
3996 try array_find_index_ 0 with _ -> raise Not_found
3999 type 'a matrix
= 'a array array
4001 let map_matrix f mat =
4002 mat +> Array.map
(fun arr
-> arr
+> Array.map
f)
4005 (*****************************************************************************)
4007 (*****************************************************************************)
4009 module B_Array = Bigarray.Array2
4018 (* for the string_of auto generation of camlp4
4019 val b_array_string_of_t : 'a -> 'b -> string
4020 val bigarray_string_of_int16_unsigned_elt : 'a -> string
4021 val bigarray_string_of_c_layout : 'a -> string
4022 let b_array_string_of_t f a = "<>"
4023 let bigarray_string_of_int16_unsigned_elt a = "<>"
4024 let bigarray_string_of_c_layout a = "<>"
4029 (*****************************************************************************)
4030 (* Set. Have a look too at set*.mli *)
4031 (*****************************************************************************)
4032 type 'a set
= 'a list
4034 let (empty_set
: 'a set
) = []
4035 let (insert_set
: 'a
-> 'a set
-> 'a set
) = fun x xs ->
4037 then (* let _ = print_string "warning insert: already exist" in *)
4041 let (single_set
: 'a
-> 'a set
) = fun x -> insert_set
x empty_set
4042 let (set
: 'a list
-> 'a set
) = fun xs ->
4043 xs +> List.fold_left (flip insert_set
) empty_set
4045 let (exists_set
: ('a
-> bool) -> 'a set
-> bool) = List.exists
4046 let (forall_set
: ('a
-> bool) -> 'a set
-> bool) = List.for_all
4047 let (filter_set
: ('a
-> bool) -> 'a set
-> 'a set
) = List.filter
4048 let (fold_set
: ('a
-> '
b -> 'a
) -> 'a
-> '
b set
-> 'a
) = List.fold_left
4049 let (map_set
: ('a
-> '
b) -> 'a set
-> '
b set
) = List.map
4050 let (member_set
: 'a
-> 'a set
-> bool) = List.mem
4052 let find_set = List.find
4053 let sort_set = List.sort
4054 let iter_set = List.iter
4056 let (top_set
: 'a set
-> 'a
) = List.hd
4058 let (inter_set
: 'a set
-> 'a set
-> 'a set
) = fun s1 s2
->
4059 s1
+> fold_set
(fun acc
x -> if member_set
x s2
then insert_set
x acc
else acc
) empty_set
4060 let (union_set
: 'a set
-> 'a set
-> 'a set
) = fun s1 s2
->
4061 s2
+> fold_set
(fun acc
x -> if member_set
x s1
then acc
else insert_set
x acc
) s1
4062 let (minus_set
: 'a set
-> 'a set
-> 'a set
) = fun s1 s2
->
4063 s1
+> filter_set
(fun x -> not
(member_set
x s2
))
4066 let union_all l = List.fold_left union_set
[] l
4068 let big_union_set f xs = xs +> map_set
f +> fold_set union_set empty_set
4070 let (card_set
: 'a set
-> int) = List.length
4072 let (include_set
: 'a set
-> 'a set
-> bool) = fun s1 s2
->
4073 (s1
+> forall_set
(fun p
-> member_set p s2
))
4075 let equal_set s1 s2
= include_set s1 s2
&& include_set s2 s1
4077 let (include_set_strict
: 'a set
-> 'a set
-> bool) = fun s1 s2
->
4078 (card_set s1
< card_set s2
) && (include_set s1 s2
)
4080 let ($
*$
) = inter_set
4081 let ($
+$
) = union_set
4082 let ($
-$
) = minus_set
4083 let ($?$
) a
b = profile_code "$?$" (fun () -> member_set a
b)
4084 let ($
<$
) = include_set_strict
4085 let ($
<=$
) = include_set
4086 let ($
=$
) = equal_set
4088 (* as $+$ but do not check for memberness, allow to have set of func *)
4089 let ($
@$
) = fun a
b -> a
@ b
4091 let rec nub = function
4093 | x::xs -> if List.mem
x xs then nub xs else x::(nub xs)
4095 (*****************************************************************************)
4096 (* Set as normal list *)
4097 (*****************************************************************************)
4099 let (union: 'a list -> 'a list -> 'a list) = fun l1 l2 ->
4100 List.fold_left (fun acc x -> if List.mem x l1 then acc else x::acc) l1 l2
4102 let insert_normal x xs = union xs [x]
4104 (* retourne lis1 - lis2 *)
4105 let minus l1 l2
= List.filter
(fun x -> not
(List.mem
x l2
)) l1
4107 let inter l1 l2
= List.fold_left (fun acc
x -> if List.mem
x l2
then x::acc
else acc
) [] l1
4109 let union_list = List.fold_left union
[]
4112 List.fold_left (function acc
-> function el
-> union
[el
] acc
) [] lis
4115 let rec non_uniq = function
4117 | e::l -> if mem
e l then e :: non_uniq l else non_uniq l
4119 let rec inclu lis1 lis2
=
4120 List.for_all
(function el
-> List.mem el lis2
) lis1
4122 let equivalent lis1 lis2
=
4123 (inclu lis1 lis2
) && (inclu lis2 lis1
)
4128 (*****************************************************************************)
4129 (* Set as sorted list *)
4130 (*****************************************************************************)
4131 (* liste trie, cos we need to do intersection, and insertion (it is a set
4132 cos when introduce has, if we create a new has => must do a recurse_rep
4133 and another categ can have to this has => must do an union
4136 let rec insert x = function
4140 else (if x < y then x::y::ys else y::(insert x ys))
4142 (* same, suppose sorted list *)
4143 let rec intersect x y =
4148 if x = y then x::(intersect xs ys
)
4150 (if x < y then intersect xs (y::ys
)
4151 else intersect (x::xs) ys
4153 (* intersect [1;3;7] [2;3;4;7;8];; *)
4156 (*****************************************************************************)
4158 (*****************************************************************************)
4159 type ('a
,'
b) assoc = ('a
* '
b) list
4162 let (assoc_to_function
: ('a
, '
b) assoc -> ('a
-> '
b)) = fun xs ->
4163 xs +> List.fold_left (fun acc
(k
, v) ->
4165 if k
= k'
then v else acc k'
4166 )) (fun k
-> failwith
"no key in this assoc")
4168 let (assoc_to_function: ('a, 'b) assoc -> ('a -> 'b)) = fun xs ->
4169 fun k -> List.assoc k xs
4172 let (empty_assoc
: ('a
, '
b) assoc) = []
4173 let fold_assoc = List.fold_left
4174 let insert_assoc = fun x xs -> x::xs
4175 let map_assoc = List.map
4176 let filter_assoc = List.filter
4178 let assoc = List.assoc
4179 let keys xs = List.map
fst xs
4183 (* assert unique key ?*)
4184 let del_assoc key
xs = xs +> List.filter
(fun (k
,v) -> k
<> key
)
4185 let replace_assoc (key
, v) xs = insert_assoc (key
, v) (del_assoc key
xs)
4187 let apply_assoc key
f xs =
4188 let old = assoc key
xs in
4189 replace_assoc (key
, f old) xs
4191 let big_union_assoc f xs = xs +> map_assoc f +> fold_assoc union_set empty_set
4193 (* todo: pb normally can suppr fun l -> .... l but if do that, then strange type _a
4194 => assoc_map is strange too => equal dont work
4196 let (assoc_reverse
: (('a
* '
b) list
) -> (('
b * 'a
) list
)) = fun l ->
4197 List.map
(fun(x,y) -> (y,x)) l
4199 let (assoc_map
: (('a
* '
b) list
) -> (('a
* '
b) list
) -> (('a
* 'a
) list
)) =
4201 let (l1bis
, l2bis
) = (assoc_reverse l1
, assoc_reverse l2
) in
4202 List.map
(fun (x,y) -> (y, List.assoc x l2bis
)) l1bis
4204 let rec (lookup_list
: 'a
-> ('a
, '
b) assoc list
-> '
b) = fun el
-> function
4205 | [] -> raise Not_found
4206 | (xs::xxs
) -> try List.assoc el
xs with Not_found
-> lookup_list el xxs
4208 let (lookup_list2
: 'a
-> ('a
, '
b) assoc list
-> ('
b * int)) = fun el xxs
->
4209 let rec lookup_l_aux i = function
4210 | [] -> raise Not_found
4212 try let res = List.assoc el
xs in (res,i)
4213 with Not_found
-> lookup_l_aux (i+1) xxs
4214 in lookup_l_aux 0 xxs
4216 let _ = example (lookup_list2
"c" [["a",1;"b",2];["a",1;"b",3];["a",1;"c",7]] = (7,2))
4219 let assoc_option k
l =
4220 optionise (fun () -> List.assoc k
l)
4222 let assoc_with_err_msg k
l =
4225 pr2 (spf "pb assoc_with_err_msg: %s" (dump k
));
4228 (*****************************************************************************)
4229 (* Assoc int -> xxx with binary tree. Have a look too at Mapb.mli *)
4230 (*****************************************************************************)
4232 (* ex: type robot_list = robot_info IntMap.t *)
4233 module IntMap
= Map.Make
4236 let compare = compare
4238 let intmap_to_list m = IntMap.fold
(fun id v acc
-> (id, v) :: acc
) m []
4239 let intmap_string_of_t f a
= "<Not Yet>"
4241 module IntIntMap
= Map.Make
4244 let compare = compare
4247 let intintmap_to_list m = IntIntMap.fold
(fun id v acc
-> (id, v) :: acc
) m []
4248 let intintmap_string_of_t f a
= "<Not Yet>"
4251 (*****************************************************************************)
4253 (*****************************************************************************)
4255 (* il parait que better when choose a prime *)
4256 let hcreate () = Hashtbl.create
401
4257 let hadd (k
,v) h = Hashtbl.add h k
v
4258 let hmem k
h = Hashtbl.mem
h k
4259 let hfind k
h = Hashtbl.find
h k
4260 let hreplace (k
,v) h = Hashtbl.replace
h k
v
4261 let hiter = Hashtbl.iter
4262 let hfold = Hashtbl.fold
4263 let hremove k
h = Hashtbl.remove h k
4266 let hash_to_list h =
4267 Hashtbl.fold
(fun k
v acc
-> (k
,v)::acc
) h []
4268 +> List.sort
compare
4270 let hash_to_list_unsorted h =
4271 Hashtbl.fold
(fun k
v acc
-> (k
,v)::acc
) h []
4273 let hash_of_list xs =
4274 let h = Hashtbl.create
101 in
4276 xs +> List.iter
(fun (k
, v) -> Hashtbl.add h k
v);
4281 let h = Hashtbl.create
101 in
4282 Hashtbl.add h "toto" 1;
4283 Hashtbl.add h "toto" 1;
4284 assert(hash_to_list h = ["toto",1; "toto",1])
4287 let hfind_default key value_if_not_found
h =
4288 try Hashtbl.find
h key
4290 (Hashtbl.add h key
(value_if_not_found
()); Hashtbl.find
h key
)
4292 (* not as easy as Perl $h->{key}++; but still possible *)
4293 let hupdate_default key op value_if_not_found
h =
4294 let old = hfind_default key value_if_not_found
h in
4295 Hashtbl.replace
h key
(op
old)
4298 let hfind_option key
h =
4299 optionise (fun () -> Hashtbl.find
h key
)
4302 (* see below: let hkeys h = ... *)
4305 (*****************************************************************************)
4307 (*****************************************************************************)
4309 type 'a hashset
= ('a
, bool) Hashtbl.t
4312 let hash_hashset_add k
e h =
4313 match optionise (fun () -> Hashtbl.find
h k
) with
4314 | Some hset
-> Hashtbl.replace hset
e true
4316 let hset = Hashtbl.create
11 in
4318 Hashtbl.add h k
hset;
4319 Hashtbl.replace
hset e true;
4322 let hashset_to_set baseset
h =
4323 h +> hash_to_list +> List.map
fst +> (fun xs -> baseset#fromlist
xs)
4325 let hashset_to_list h = hash_to_list h +> List.map
fst
4327 let hashset_of_list xs =
4328 xs +> List.map
(fun x -> x, true) +> hash_of_list
4333 let hkey = Hashtbl.create
101 in
4334 h +> Hashtbl.iter
(fun k
v -> Hashtbl.replace
hkey k
true);
4335 hashset_to_list hkey
4339 let group_assoc_bykey_eff xs =
4340 let h = Hashtbl.create
101 in
4341 xs +> List.iter
(fun (k
, v) -> Hashtbl.add h k
v);
4342 let keys = hkeys h in
4343 keys +> List.map
(fun k
-> k
, Hashtbl.find_all
h k
)
4346 let test_group_assoc () =
4347 let xs = enum 0 10000 +> List.map
(fun i -> i_to_s i, i) in
4348 let xs = ("0", 2)::xs in
4349 (* let _ys = xs +> Common.groupBy (fun (a,resa) (b,resb) -> a =$= b) *)
4350 let ys = xs +> group_assoc_bykey_eff
4357 let diff_two_say_set_eff xs1 xs2
=
4358 let h1 = hashset_of_list xs1
in
4359 let h2 = hashset_of_list xs2
in
4361 let hcommon = Hashtbl.create
101 in
4362 let honly_in_h1 = Hashtbl.create
101 in
4363 let honly_in_h2 = Hashtbl.create
101 in
4365 h1 +> Hashtbl.iter
(fun k
_ ->
4367 then Hashtbl.replace
hcommon k
true
4368 else Hashtbl.add honly_in_h1 k
true
4370 h2 +> Hashtbl.iter
(fun k
_ ->
4372 then Hashtbl.replace
hcommon k
true
4373 else Hashtbl.add honly_in_h2 k
true
4375 hashset_to_list hcommon,
4376 hashset_to_list honly_in_h1,
4377 hashset_to_list honly_in_h2
4380 (*****************************************************************************)
4382 (*****************************************************************************)
4383 type 'a stack
= 'a list
4385 let (empty_stack
: 'a stack
) = []
4386 let (push: 'a
-> 'a stack
-> 'a stack
) = fun x xs -> x::xs
4387 let (top
: 'a stack
-> 'a
) = List.hd
4388 let (pop
: 'a stack
-> 'a stack
) = List.tl
4392 * let push2 v l = l := v :: !l
4396 let v = List.hd
!l in
4403 (*****************************************************************************)
4405 (*****************************************************************************)
4406 type 'a bintree
= Leaf
of 'a
| Branch
of ('a bintree
* 'a bintree
)
4409 (*****************************************************************************)
4411 (*****************************************************************************)
4413 (* no empty tree, must have one root at list *)
4414 type 'a tree
= Tree
of 'a
* ('a tree
) list
4416 let rec (tree_iter
: ('a
-> unit) -> 'a tree
-> unit) = fun f tree
->
4418 | Tree
(node
, xs) ->
4420 xs +> List.iter
(tree_iter
f)
4423 (*****************************************************************************)
4424 (* N-ary tree with updatable childrens *)
4425 (*****************************************************************************)
4427 (* Leaf can seem redundant, but sometimes want to directly see if
4428 * a children is a leaf without looking if the list is empty.
4430 type ('a
, '
b) treeref
=
4431 | NodeRef
of 'a
* ('a
, '
b) treeref list
ref
4434 let rec (treeref_node_iter
:
4435 (('a
* ('a
, '
b) treeref list
ref) -> unit) ->
4436 ('a
, '
b) treeref
-> unit) = fun f tree
->
4439 | NodeRef
(n, xs) ->
4441 !xs +> List.iter
(treeref_node_iter
f)
4444 let rec (treeref_node_iter_with_parents
:
4445 (('a
* ('a
, '
b) treeref list
ref) -> ('a list
) -> unit) ->
4446 ('a
, '
b) treeref
-> unit) = fun f tree
->
4447 let rec aux acc tree
=
4450 | NodeRef
(n, xs) ->
4452 !xs +> List.iter
(aux (n::acc
))
4457 let find_treeref f tree
=
4460 tree
+> treeref_node_iter
(fun (n, xs) ->
4462 then push2 (n, xs) res;
4465 | [n,xs] -> NodeRef
(n, xs)
4466 | [] -> raise Not_found
4467 | x::y::zs
-> failwith
"multi found"
4469 (*****************************************************************************)
4470 (* Graph. Have a look too at Ograph_*.mli *)
4471 (*****************************************************************************)
4472 (* todo: generalise to put in common (need 'edge (and 'c ?),
4473 * and take in param a display func, cos caml sux, no overloading of show :(
4474 * Simple impelemntation. Can do also matrix, or adjacent list, or pointer(ref)
4475 * todo: do some check (dont exist already, ...)
4478 type 'node graph
= ('node set
) * (('node
* 'node
) set
)
4480 let (add_node
: 'a
-> 'a graph
-> 'a graph
) = fun node
(nodes
, arcs
) ->
4483 let (del_node
: 'a
-> 'a graph
-> 'a graph
) = fun node
(nodes
, arcs
) ->
4484 (nodes $
-$ set
[node
], arcs
)
4485 (* could do more job:
4486 let _ = assert (successors node (nodes, arcs) = empty) in
4487 +> List.filter (fun (src, dst) -> dst != node))
4489 let (add_arc
: ('a
* 'a
) -> 'a graph
-> 'a graph
) = fun arc
(nodes
, arcs
) ->
4490 (nodes
, set
[arc
] $
+$ arcs
)
4492 let (del_arc
: ('a
* 'a
) -> 'a graph
-> 'a graph
) = fun arc
(nodes
, arcs
) ->
4493 (nodes
, arcs
+> List.filter
(fun a
-> not
(arc
= a
)))
4495 let (successors
: 'a
-> 'a graph
-> 'a set
) = fun x (nodes
, arcs
) ->
4496 arcs
+> List.filter
(fun (src
, dst
) -> src
= x) +> List.map
snd
4498 let (predecessors
: 'a
-> 'a graph
-> 'a set
) = fun x (nodes
, arcs
) ->
4499 arcs
+> List.filter
(fun (src
, dst
) -> dst
= x) +> List.map
fst
4501 let (nodes
: 'a graph
-> 'a set
) = fun (nodes
, arcs
) -> nodes
4504 let rec (fold_upward
: ('
b -> 'a
-> '
b) -> 'a set
-> '
b -> 'a graph
-> '
b) =
4505 fun f xs acc graph
->
4508 | x::xs -> (f acc
x)
4509 +> (fun newacc -> fold_upward
f (graph
+> predecessors
x) newacc graph
)
4510 +> (fun newacc -> fold_upward
f xs newacc graph
)
4511 (* TODO avoid already visited *)
4513 let empty_graph = ([], [])
4518 let (add_arcs_toward: int -> (int list) -> 'a graph -> 'a graph) = fun i xs ->
4520 (nodes, arcs) -> (nodes, (List.map (fun j -> (j,i) ) xs)++arcs)
4521 let (del_arcs_toward: int -> (int list) -> 'a graph -> 'a graph)= fun i xs g ->
4522 List.fold_left (fun acc el -> del_arc (el, i) acc) g xs
4523 let (add_arcs_from: int -> (int list) -> 'a graph -> 'a graph) = fun i xs ->
4525 (nodes, arcs) -> (nodes, (List.map (fun j -> (i,j) ) xs)++arcs)
4528 let (del_node: (int * 'node) -> 'node graph -> 'node graph) = fun node ->
4529 function (nodes, arcs) ->
4530 let newnodes = List.filter (fun a -> not (node = a)) nodes in
4531 if newnodes = nodes then (raise Not_found) else (newnodes, arcs)
4532 let (replace_node: int -> 'node -> 'node graph -> 'node graph) = fun i n ->
4533 function (nodes, arcs) ->
4534 let newnodes = List.filter (fun (j,_) -> not (i = j)) nodes in
4535 ((i,n)::newnodes, arcs)
4536 let (get_node: int -> 'node graph -> 'node) = fun i -> function
4537 (nodes, arcs) -> List.assoc i nodes
4539 let (get_free: 'a graph -> int) = function
4540 (nodes, arcs) -> (maximum (List.map fst nodes))+1
4541 (* require no cycle !!
4542 TODO if cycle check that we have already visited a node *)
4543 let rec (succ_all
: int -> 'a graph
-> (int list
)) = fun i -> function
4544 (nodes
, arcs
) as g
->
4545 let direct = succ
i g
in
4546 union
direct (union_list (List.map
(fun i -> succ_all
i g
) direct))
4547 let rec (pred_all
: int -> 'a graph
-> (int list
)) = fun i -> function
4548 (nodes
, arcs
) as g
->
4549 let direct = pred
i g
in
4550 union
direct (union_list (List.map
(fun i -> pred_all
i g
) direct))
4551 (* require that the nodes are different !! *)
4552 let rec (equal
: 'a graph
-> 'a graph
-> bool) = fun g1 g2
->
4553 let ((nodes1
, arcs1
),(nodes2
, arcs2
)) = (g1
,g2
) in
4555 (* do 2 things, check same length and to assoc *)
4556 let conv = assoc_map nodes1 nodes2
in
4557 List.for_all
(fun (i1
,i2
) ->
4558 List.mem
(List.assoc i1
conv, List.assoc i2
conv) arcs2
)
4560 && (List.length arcs1
= List.length arcs2
)
4561 (* could think that only forall is needed, but need check same lenth too*)
4564 let (display
: 'a graph
-> ('a
-> unit) -> unit) = fun g display_func
->
4565 let rec aux depth
i =
4567 print_int
i; print_string
"->"; display_func
(get_node
i g
);
4569 List.iter
(aux (depth
+2)) (succ
i g
)
4572 let (display_dot
: 'a graph
-> ('a
-> string) -> unit)= fun (nodes
,arcs
) func
->
4573 let file = open_out
"test.dot" in
4574 output_string
file "digraph misc {\n" ;
4575 List.iter
(fun (n, node
) ->
4576 output_int
file n; output_string
file " [label=\"";
4577 output_string
file (func node
); output_string
file " \"];\n"; ) nodes
;
4578 List.iter
(fun (i1
,i2
) -> output_int
file i1
; output_string
file " -> " ;
4579 output_int
file i2
; output_string
file " ;\n"; ) arcs
;
4580 output_string
file "}\n" ;
4582 let status = Unix.system
"viewdot test.dot" in
4584 (* todo: faire = graphe (int can change !!! => cant make simply =)
4585 reassign number first !!
4588 (* todo: mettre diff(modulo = !!) en rouge *)
4589 let (display_dot2
: 'a graph
-> 'a graph
-> ('a
-> string) -> unit) =
4590 fun (nodes1
, arcs1
) (nodes2
, arcs2
) func
->
4591 let file = open_out
"test.dot" in
4592 output_string
file "digraph misc {\n" ;
4593 output_string
file "rotate = 90;\n";
4594 List.iter
(fun (n, node
) ->
4595 output_string
file "100"; output_int
file n;
4596 output_string
file " [label=\"";
4597 output_string
file (func node
); output_string
file " \"];\n"; ) nodes1
;
4598 List.iter
(fun (n, node
) ->
4599 output_string
file "200"; output_int
file n;
4600 output_string
file " [label=\"";
4601 output_string
file (func node
); output_string
file " \"];\n"; ) nodes2
;
4602 List.iter
(fun (i1
,i2
) ->
4603 output_string
file "100"; output_int
file i1
; output_string
file " -> " ;
4604 output_string
file "100"; output_int
file i2
; output_string
file " ;\n";
4607 List.iter
(fun (i1
,i2
) ->
4608 output_string
file "200"; output_int
file i1
; output_string
file " -> " ;
4609 output_string
file "200"; output_int
file i2
; output_string
file " ;\n"; )
4611 (* output_string file "500 -> 1001; 500 -> 2001}\n" ; *)
4612 output_string
file "}\n" ;
4614 let status = Unix.system
"viewdot test.dot" in
4619 (*****************************************************************************)
4621 (*****************************************************************************)
4624 let map = List.map (* note: really really slow, use rev_map if possible *)
4625 let filter = List.filter
4626 let fold = List.fold_left
4627 let member = List.mem
4628 let iter = List.iter
4629 let find = List.find
4630 let exists = List.exists
4631 let forall = List.for_all
4632 let big_union f xs = xs +> map f +> fold union_set empty_set
4633 (* let empty = [] *)
4635 let sort = List.sort
4636 let length = List.length
4637 let null xs = match xs with [] -> true | _ -> false
4640 let is_singleton = fun xs -> List.length xs = 1
4642 (*****************************************************************************)
4643 (* Geometry (raytracer) *)
4644 (*****************************************************************************)
4646 type vector
= (float * float * float)
4648 type color
= vector
(* color(0-1) *)
4650 (* todo: factorise *)
4651 let (dotproduct
: vector
* vector
-> float) =
4652 fun ((x1
,y1
,z1
),(x2
,y2
,z2
)) -> (x1
*.x2
+. y1
*.y2
+. z1
*.z2
)
4653 let (vector_length
: vector
-> float) =
4654 fun (x,y,z
) -> sqrt
(square x +. square y +. square z
)
4655 let (minus_point
: point
* point
-> vector
) =
4656 fun ((x1
,y1
,z1
),(x2
,y2
,z2
)) -> ((x1
-. x2
),(y1
-. y2
),(z1
-. z2
))
4657 let (distance
: point
* point
-> float) =
4658 fun (x1
, x2
) -> vector_length
(minus_point
(x2
,x1
))
4659 let (normalise
: vector
-> vector
) =
4661 let len = vector_length
(x,y,z
) in (x /. len, y /. len, z
/. len)
4662 let (mult_coeff
: vector
-> float -> vector
) =
4663 fun (x,y,z
) c -> (x *. c, y *. c, z
*. c)
4664 let (add_vector
: vector
-> vector
-> vector
) =
4665 fun v1 v2 -> let ((x1
,y1
,z1
),(x2
,y2
,z2
)) = (v1,v2) in
4666 (x1
+.x2
, y1
+.y2
, z1
+.z2
)
4667 let (mult_vector
: vector
-> vector
-> vector
) =
4668 fun v1 v2 -> let ((x1
,y1
,z1
),(x2
,y2
,z2
)) = (v1,v2) in
4669 (x1
*.x2
, y1
*.y2
, z1
*.z2
)
4670 let sum_vector = List.fold_left add_vector
(0.0,0.0,0.0)
4672 (*****************************************************************************)
4673 (* Pics (raytracer) *)
4674 (*****************************************************************************)
4676 type pixel
= (int * int * int) (* RGB *)
4678 (* required pixel list in row major order, line after line *)
4679 let (write_ppm
: int -> int -> (pixel list
) -> string -> unit) = fun
4680 width height
xs filename ->
4681 let chan = open_out
filename in
4683 output_string
chan "P6\n";
4684 output_string
chan ((string_of_int width
) ^
"\n");
4685 output_string
chan ((string_of_int height
) ^
"\n");
4686 output_string
chan "255\n";
4687 List.iter (fun (r,g
,b) ->
4688 List.iter (fun byt
-> output_byte
chan byt
) [r;g
;b]
4693 let test_ppm1 () = write_ppm
100 100
4694 ((generate
(50*100) (1,45,100)) ++ (generate
(50*100) (1,1,100)))
4697 (*****************************************************************************)
4699 (*****************************************************************************)
4700 type diff
= Match
| BnotinA
| AnotinB
4702 let (diff
: (int -> int -> diff
-> unit)-> (string list
* string list
) -> unit)=
4704 let file1 = "/tmp/diff1-" ^
(string_of_int
(Unix.getuid
())) in
4705 let file2 = "/tmp/diff2-" ^
(string_of_int
(Unix.getuid
())) in
4706 let fileresult = "/tmp/diffresult-" ^
(string_of_int
(Unix.getuid
())) in
4707 write_file file1 (unwords
xs);
4708 write_file file2 (unwords
ys);
4710 ("diff --side-by-side -W 1 " ^
file1 ^
" " ^
file2 ^
" > " ^
fileresult);
4711 let res = cat fileresult in
4714 res +> List.iter (fun s ->
4716 | ("" | " ") -> f !a !b Match
; incr
a; incr
b;
4717 | ">" -> f !a !b BnotinA
; incr
b;
4718 | ("|" | "/" | "\\" ) ->
4719 f !a !b BnotinA
; f !a !b AnotinB
; incr
a; incr
b;
4720 | "<" -> f !a !b AnotinB
; incr
a;
4721 | _ -> raise Impossible
4726 ["0";"a";"b";"c";"d"; "f";"g";"h";"j";"q"; "z"]
4727 [ "a";"b";"c";"d";"e";"f";"g";"i";"j";"k";"r";"x";"y";"z"]
4728 (fun x y -> pr "match")
4729 (fun x y -> pr "a_not_in_b")
4730 (fun x y -> pr "b_not_in_a")
4733 let (diff2
: (int -> int -> diff
-> unit) -> (string * string) -> unit) =
4734 fun f (xstr
,ystr
) ->
4735 write_file "/tmp/diff1" xstr
;
4736 write_file "/tmp/diff2" ystr
;
4738 ("diff --side-by-side --left-column -W 1 " ^
4739 "/tmp/diff1 /tmp/diff2 > /tmp/diffresult");
4740 let res = cat "/tmp/diffresult" in
4743 res +> List.iter (fun s ->
4745 | "(" -> f !a !b Match
; incr
a; incr
b;
4746 | ">" -> f !a !b BnotinA
; incr
b;
4747 | "|" -> f !a !b BnotinA
; f !a !b AnotinB
; incr
a; incr
b;
4748 | "<" -> f !a !b AnotinB
; incr
a;
4749 | _ -> raise Impossible
4753 (*****************************************************************************)
4754 (* Parsers (aop-colcombet) *)
4755 (*****************************************************************************)
4757 let parserCommon lexbuf parserer lexer
=
4759 let result = parserer lexer lexbuf
in
4761 with Parsing.Parse_error
->
4762 print_string
"buf: "; print_string lexbuf
.Lexing.lex_buffer
;
4764 print_string
"current: "; print_int lexbuf
.Lexing.lex_curr_pos
;
4766 raise
Parsing.Parse_error
4769 (* marche pas ca neuneu *)
4771 let getDoubleParser parserer lexer string =
4772 let lexbuf1 = Lexing.from_string string in
4773 let chan = open_in string in
4774 let lexbuf2 = Lexing.from_channel chan in
4775 (parserCommon lexbuf1 parserer lexer , parserCommon lexbuf2 parserer lexer )
4778 let getDoubleParser parserer lexer
=
4781 let lexbuf1 = Lexing.from_string
string in
4782 parserCommon lexbuf1 parserer lexer
4785 let chan = open_in
string in
4786 let lexbuf2 = Lexing.from_channel
chan in
4787 parserCommon lexbuf2 parserer lexer
4791 (*****************************************************************************)
4792 (* parser combinators *)
4793 (*****************************************************************************)
4795 (* cf parser_combinators.ml
4797 * Could also use ocaml stream. but not backtrack and forced to do LL,
4798 * so combinators are better.
4803 (*****************************************************************************)
4804 (* Parser related (cocci) *)
4805 (*****************************************************************************)
4816 let fake_parse_info = {
4817 charpos
= -1; str
= "";
4818 line
= -1; column
= -1; file = "";
4821 let string_of_parse_info x =
4822 spf "%s at %s:%d:%d" x.str
x.file x.line
x.column
4823 let string_of_parse_info_bis x =
4824 spf "%s:%d:%d" x.file x.line
x.column
4827 let (info_from_charpos2
: int -> filename -> (int * int * string)) =
4828 fun charpos
filename ->
4830 (* Currently lexing.ml does not handle the line number position.
4831 * Even if there is some fields in the lexing structure, they are not
4832 * maintained by the lexing engine :( So the following code does not work:
4833 * let pos = Lexing.lexeme_end_p lexbuf in
4834 * sprintf "at file %s, line %d, char %d" pos.pos_fname pos.pos_lnum
4835 * (pos.pos_cnum - pos.pos_bol) in
4836 * Hence this function to overcome the previous limitation.
4838 let chan = open_in
filename in
4839 let linen = ref 0 in
4841 let rec charpos_to_pos_aux () =
4842 let s = (input_line
chan) in
4845 if (!posl + slength s > charpos
)
4848 (!linen, charpos
- !posl, s)
4851 posl := !posl + slength s;
4852 charpos_to_pos_aux ();
4855 let res = charpos_to_pos_aux () in
4859 let info_from_charpos a b =
4860 profile_code "Common.info_from_charpos" (fun () -> info_from_charpos2
a b)
4864 let (full_charpos_to_pos2
: filename -> (int * int) array
) = fun filename ->
4866 let arr = Array.create
(filesize filename + 2) (0,0) in
4868 let chan = open_in
filename in
4870 let charpos = ref 0 in
4873 let rec full_charpos_to_pos_aux () =
4875 let s = (input_line
chan) in
4878 (* '... +1 do' cos input_line dont return the trailing \n *)
4879 for i = 0 to (slength s - 1) + 1 do
4880 arr.(!charpos + i) <- (!line, i);
4882 charpos := !charpos + slength s + 1;
4883 full_charpos_to_pos_aux();
4886 for i = !charpos to Array.length arr - 1 do
4887 arr.(i) <- (!line, 0);
4892 full_charpos_to_pos_aux ();
4896 let full_charpos_to_pos a =
4897 profile_code "Common.full_charpos_to_pos" (fun () -> full_charpos_to_pos2
a)
4899 let test_charpos file =
4900 full_charpos_to_pos file +> dump +> pr2
4904 let complete_parse_info filename table
x =
4907 line = fst (table
.(x.charpos));
4908 column
= snd (table
.(x.charpos));
4911 (*---------------------------------------------------------------------------*)
4912 (* Decalage is here to handle stuff such as cpp which include file and who
4915 let (error_messagebis
: filename -> (string * int) -> int -> string)=
4916 fun filename (lexeme
, lexstart
) decalage
->
4918 let charpos = lexstart
+ decalage
in
4920 let (line, pos, linecontent
) = info_from_charpos charpos filename in
4921 sprintf
"File \"%s\", line %d, column %d, charpos = %d
4922 around = '%s', whole content = %s"
4923 filename line pos charpos tok (chop linecontent
)
4925 let error_message = fun filename (lexeme
, lexstart
) ->
4927 error_messagebis
filename (lexeme
, lexstart
) 0
4930 ("PB in Common.error_message, position " ^
i_to_s lexstart ^
4931 " given out of file:" ^
filename);
4936 let error_message_short = fun filename (lexeme
, lexstart
) ->
4938 let charpos = lexstart
in
4939 let (line, pos, linecontent
) = info_from_charpos charpos filename in
4940 sprintf
"File \"%s\", line %d" filename line
4944 ("PB in Common.error_message, position " ^
i_to_s lexstart ^
4945 " given out of file:" ^
filename);
4950 (*****************************************************************************)
4951 (* Regression testing bis (cocci) *)
4952 (*****************************************************************************)
4954 (* todo: keep also size of file, compute md5sum ? cos maybe the file
4957 * todo: could also compute the date, or some version info of the program,
4958 * can record the first date when was found a OK, the last date where
4959 * was ok, and then first date when found fail. So the
4960 * Common.Ok would have more information that would be passed
4961 * to the Common.Pb of date * date * date * string peut etre.
4963 * todo? maybe use plain text file instead of marshalling.
4966 type score_result
= Ok
| Pb
of string
4967 type score
= (string (* usually a filename *), score_result
) Hashtbl.t
4969 let empty_score () = (Hashtbl.create
101 : score
)
4973 let regression_testing newscore best_score_file
=
4975 pr2 ("regression file: "^ best_score_file
);
4976 let (bestscore
: score
) =
4977 if not
(Sys.file_exists best_score_file
)
4978 then write_value (empty_score()) best_score_file
;
4979 get_value best_score_file
4981 let newbestscore = empty_score () in
4984 (hash_to_list newscore
+> List.map fst)
4986 (hash_to_list bestscore
+> List.map fst)
4989 allres +> List.iter (fun res ->
4991 optionise (fun () -> Hashtbl.find newscore
res),
4992 optionise (fun () -> Hashtbl.find bestscore
res)
4994 | None
, None
-> raise Impossible
4996 Printf.printf
"new test file appeared: %s\n" res;
4997 Hashtbl.add newbestscore res x;
4999 Printf.printf
"old test file disappeared: %s\n" res;
5000 | Some newone
, Some bestone
->
5001 (match newone
, bestone
with
5003 Hashtbl.add newbestscore res Ok
5006 "PBBBBBBBB: a test file does not work anymore!!! : %s\n" res;
5007 Printf.printf
"Error : %s\n" x;
5008 Hashtbl.add newbestscore res Ok
5010 Printf.printf
"Great: a test file now works: %s\n" res;
5011 Hashtbl.add newbestscore res Ok
5013 Hashtbl.add newbestscore res (Pb
x);
5017 "Semipb: still error but not same error : %s\n" res;
5018 Printf.printf
"%s\n" (chop ("Old error: " ^
y));
5019 Printf.printf
"New error: %s\n" x;
5023 write_value newbestscore (best_score_file ^
".old");
5024 write_value newbestscore best_score_file
;
5025 flush stdout
; flush stderr
;
5028 let string_of_score_result v =
5031 | Pb
s -> "Pb: " ^
s
5033 let print_score score
=
5034 score
+> hash_to_list +> List.iter (fun (k
, v) ->
5035 pr2 (sprintf
"% s --> %s" k
(string_of_score_result v))
5037 pr2 "--------------------------------";
5039 pr2 "--------------------------------";
5040 let total = hash_to_list score
+> List.length in
5041 let good = hash_to_list score
+> List.filter
5042 (fun (s, v) -> v = Ok
) +> List.length
5044 pr2 (sprintf
"good = %d/%d" good total)
5047 (*****************************************************************************)
5048 (* Scope managment (cocci) *)
5049 (*****************************************************************************)
5051 (* could also make a function Common.make_scope_functions that return
5052 * the new_scope, del_scope, do_in_scope, add_env. Kind of functor :)
5055 type ('
a, '
b) scoped_env
= ('
a, '
b) assoc list
5058 let rec lookup_env f env =
5060 | [] -> raise Not_found
5061 | []::zs -> lookup_env f zs
5064 | None -> lookup_env f (xs::zs)
5067 let member_env_key k env =
5069 let _ = lookup_env (fun (k',v) -> if k = k' then Some v else None) env in
5071 with Not_found -> false
5075 let rec lookup_env k env
=
5077 | [] -> raise Not_found
5078 | []::zs
-> lookup_env k zs
5079 | ((k'
,v)::xs)::zs
->
5082 else lookup_env k
(xs::zs
)
5084 let member_env_key k env
=
5085 match optionise (fun () -> lookup_env k env
) with
5090 let new_scope scoped_env
= scoped_env
:= []::!scoped_env
5091 let del_scope scoped_env
= scoped_env
:= List.tl
!scoped_env
5093 let do_in_new_scope scoped_env
f =
5095 new_scope scoped_env
;
5097 del_scope scoped_env
;
5101 let add_in_scope scoped_env def
=
5102 let (current, older
) = uncons !scoped_env
in
5103 scoped_env
:= (def
::current)::older
5109 (* note that ocaml hashtbl store also old value of a binding when add
5110 * add a newbinding; that's why del_scope works
5113 type ('
a, '
b) scoped_h_env
= {
5114 scoped_h
: ('
a, '
b) Hashtbl.t;
5115 scoped_list
: ('
a, '
b) assoc list
;
5118 let empty_scoped_h_env () = {
5119 scoped_h
= Hashtbl.create
101;
5122 let clone_scoped_h_env x =
5123 { scoped_h
= Hashtbl.copy
x.scoped_h
;
5124 scoped_list
= x.scoped_list
;
5127 let rec lookup_h_env k env
=
5128 Hashtbl.find env
.scoped_h k
5130 let member_h_env_key k env
=
5131 match optionise (fun () -> lookup_h_env k env
) with
5136 let new_scope_h scoped_env
=
5137 scoped_env
:= {!scoped_env
with scoped_list
= []::!scoped_env
.scoped_list
}
5138 let del_scope_h scoped_env
=
5140 List.hd
!scoped_env
.scoped_list
+> List.iter (fun (k
, v) ->
5141 Hashtbl.remove !scoped_env
.scoped_h k
5143 scoped_env
:= {!scoped_env
with scoped_list
=
5144 List.tl
!scoped_env
.scoped_list
5148 let do_in_new_scope_h scoped_env
f =
5150 new_scope_h scoped_env
;
5152 del_scope_h scoped_env
;
5157 let add_in_scope scoped_env def =
5158 let (current, older) = uncons !scoped_env in
5159 scoped_env := (def::current)::older
5162 let add_in_scope_h x (k
,v) =
5164 Hashtbl.add !x.scoped_h k
v;
5165 x := { !x with scoped_list
=
5166 ((k
,v)::(List.hd
!x.scoped_list
))::(List.tl
!x.scoped_list
);
5170 (*****************************************************************************)
5172 (*****************************************************************************)
5174 (* let ansi_terminal = ref true *)
5176 let (_execute_and_show_progress_func
: (int (* length *) -> ((unit -> unit) -> unit) -> unit) ref)
5179 failwith
"no execute yet, have you included common_extra.cmo?"
5184 let execute_and_show_progress len f =
5185 !_execute_and_show_progress_func
len f
5188 (* now in common_extra.ml:
5189 * let execute_and_show_progress len f = ...
5192 (*****************************************************************************)
5194 (*****************************************************************************)
5196 let _init_random = Random.self_init
()
5198 let random_insert i l =
5199 let p = Random.int (length l +1)
5200 in let rec insert i p l =
5201 if (p = 0) then i::l else (hd l)::insert i (p-1) (tl l)
5204 let rec randomize_list = function
5206 | a::l -> random_insert a (randomize_list l)
5208 let random_list xs =
5209 List.nth xs (Random.int (length xs))
5211 (* todo_opti: use fisher/yates algorithm.
5212 * ref: http://en.wikipedia.org/wiki/Knuth_shuffle
5214 * public static void shuffle (int[] array)
5216 * Random rng = new Random ();
5217 * int n = array.length;
5220 * int k = rng.nextInt(n + 1); // 0 <= k <= n (!)
5221 * int temp = array[n];
5222 * array[n] = array[k];
5228 let randomize_list xs =
5229 let permut = permutation
xs in
5234 let random_subset_of_list num
xs =
5235 let array = Array.of_list
xs in
5236 let len = Array.length array in
5238 let h = Hashtbl.create
101 in
5239 let cnt = ref num
in
5241 let x = Random.int len in
5242 if not
(Hashtbl.mem
h (array.(x))) (* bugfix2: not just x :) *)
5244 Hashtbl.add h (array.(x)) true; (* bugfix1: not just x :) *)
5248 let objs = hash_to_list h +> List.map fst in
5253 (*****************************************************************************)
5254 (* Flags and actions *)
5255 (*****************************************************************************)
5257 (* I put it inside a func as it can help to give a chance to
5258 * change the globals before getting the options as some
5259 * options sometimes may want to show the default value.
5261 let cmdline_flags_devel () =
5263 "-debugger", Arg.Set
debugger ,
5264 " option to set if launched inside ocamldebug";
5265 "-profile", Arg.Unit
(fun () -> profile := PALL
),
5266 " gather timing information about important functions";
5268 let cmdline_flags_verbose () =
5270 "-verbose_level", Arg.Set_int
verbose_level,
5271 " <int> guess what";
5272 "-disable_pr2_once", Arg.Set
disable_pr2_once,
5273 " to print more messages";
5274 "-show_trace_profile", Arg.Set
show_trace_profile,
5278 let cmdline_flags_other () =
5280 "-nocheck_stack", Arg.Clear
check_stack,
5284 (* potentially other common options but not yet integrated:
5286 "-timeout", Arg.Set_int timeout,
5287 " <sec> interrupt LFS or buggy external plugins";
5289 (* can't be factorized because of the $ cvs stuff, we want the date
5290 * of the main.ml file, not common.ml
5292 "-version", Arg.Unit
(fun () ->
5293 pr2 "version: _dollar_Date: 2008/06/14 00:54:22 _dollar_";
5294 raise
(Common.UnixExit
0)
5298 "-shorthelp", Arg.Unit
(fun () ->
5299 !short_usage_func
();
5300 raise
(Common.UnixExit
0)
5302 " see short list of options";
5303 "-longhelp", Arg.Unit
(fun () ->
5305 raise
(Common.UnixExit
0)
5307 "-help", Arg.Unit
(fun () ->
5309 raise
(Common.UnixExit
0)
5312 "--help", Arg.Unit
(fun () ->
5314 raise
(Common.UnixExit
0)
5320 let cmdline_actions () =
5322 "-test_check_stack", " <limit>",
5323 mk_action_1_arg test_check_stack_size;
5327 (*****************************************************************************)
5329 (*****************************************************************************)
5330 (* stuff put here cos of of forward definition limitation of ocaml *)
5333 (* Infix trick, seen in jane street lib and harrop's code, and maybe in GMP *)
5334 module Infix
= struct
5341 let main_boilerplate f =
5342 if not
(!Sys.interactive
) then
5343 exn_to_real_unixexit (fun () ->
5345 Sys.set_signal
Sys.sigint
(Sys.Signal_handle
(fun _ ->
5346 pr2 "C-c intercepted, will do some cleaning before exiting";
5347 (* But if do some try ... with e -> and if do not reraise the exn,
5348 * the bubble never goes at top and so I cant really C-c.
5350 * A solution would be to not raise, but do the erase_temp_file in the
5351 * syshandler, here, and then exit.
5352 * The current solution is to not do some wild try ... with e
5353 * by having in the exn handler a case: UnixExit x -> raise ... | e ->
5355 Sys.set_signal
Sys.sigint
Sys.Signal_default
;
5356 raise
(UnixExit
(-1))
5359 (* The finalize below makes it tedious to go back to exn when use
5360 * 'back' in the debugger. Hence this special case. But the
5361 * Common.debugger will be set in main(), so too late, so
5362 * have to be quicker
5364 if Sys.argv
+> Array.to_list
+> List.exists (fun x -> x ="-debugger")
5365 then debugger := true;
5368 pp_do_in_zero_box (fun () ->
5369 f(); (* <---- here it is *)
5372 if !profile <> PNONE
5373 then pr2 (profile_diagnostic ());
5374 erase_temp_files ();
5377 (* let _ = if not !Sys.interactive then (main ()) *)
5380 (* based on code found in cameleon from maxence guesdon *)
5381 let md5sum_of_string s =
5382 let com = spf "echo %s | md5sum | cut -d\" \" -f 1"
5385 match cmd_to_list com with
5389 | _ -> failwith
"md5sum_of_string wrong output"
5392 (*****************************************************************************)
5394 (*****************************************************************************)
5396 let (generic_print
: '
a -> string -> string) = fun v typ
->
5397 write_value v "/tmp/generic_print";
5399 ("printf 'let (v:" ^ typ ^
")= Common.get_value \"/tmp/generic_print\" " ^
5401 " | calc.top > /tmp/result_generic_print");
5402 cat "/tmp/result_generic_print"
5403 +> drop_while (fun e -> not
(e =~
"^#.*")) +> tail
5406 if (s =~
".*= \\(.+\\)")
5408 else "error in generic_print, not good format:" ^
s)
5410 (* let main () = pr (generic_print [1;2;3;4] "int list") *)
5412 class ['
a] olist
(ys: '
a list
) =
5416 (* method fold f a = List.fold_left f a xs *)
5417 method fold : '
b. ('
b -> '
a -> '
b) -> '
b -> '
b =
5418 fun f accu
-> List.fold_left f accu
xs
5422 (* let _ = write_value ((new setb[])#add 1) "/tmp/test" *)
5423 let typing_sux_test () =
5424 let x = Obj.magic
[1;2;3] in
5425 let f1 xs = List.iter print_int
xs in
5426 let f2 xs = List.iter print_string
xs in
5429 (* let (test: 'a osetb -> 'a ocollection) = fun o -> (o :> 'a ocollection) *)
5430 (* let _ = test (new osetb (Setb.empty)) *)