(* Run hol_test *) needs "Formal_ineqs/arith/arith_nat.hl";; needs "new_arith/nat_arith.hl";; #load "nums.cma";; #load "unix.cma";; (* Creates all missing directories *) let rec create_dirs path = let dirname = Filename.dirname path in if Sys.file_exists dirname then if not (Sys.is_directory dirname) then failwith (Printf.sprintf "%s exists and it is not a directory" dirname) else () else let _ = create_dirs dirname in let _ = Unix.mkdir dirname 0o777 in ();; (* Reads all lines from a file *) let read_file fname = let ic = open_in fname in let rec read acc = try let line = input_line ic in read (line :: acc) with End_of_file -> acc in let lines = read [] in let _ = close_in ic in rev lines;; (* Writes all lines to a file *) let write_file path buf = let _ = create_dirs path in let oc = open_out path in let rec write = function | [] -> () | line :: rest -> let _ = output_string oc (line ^ "\n") in write rest in let _ = write buf in let _ = close_out oc in ();; let rec take n = function | [] -> [] | h :: t -> if n <= 0 then [] else h :: take (n - 1) t;; let rec drop n = function | [] -> [] | (h :: t) as s -> if n <= 0 then s else drop (n - 1) t;; let mk_nums = map Num.num_of_string;; (* Combines consecutive elements into pairs *) let rec data_to_pairs = function | [] -> [] | [h] -> [(h, h)] | (h1 :: h2 :: t) -> (h1, h2) :: data_to_pairs t;; let create_binop op pairs = map (uncurry (mk_binop op)) pairs;; let create_op op data = map (curry mk_comb op) data;; (* Runs a test. *) (* n: number of repetitions *) (* data_file: input data file *) (* pre: function which is applied to the raw data first *) (* mk: function which is applied to all elements of the data *) (* post: function which is applied to the transformed data *) (* f: test function *) (* result: if true, then results of f on the data are returned *) let run_tests = let run data f = let start = Unix.gettimeofday() in let result = map f data in let finish = Unix.gettimeofday() in finish -. start, result in let rec repeat n data f ((ts, r) as acc) = if n <= 0 then acc else let time, result = run data f in repeat (n - 1) data f (time :: ts, result) in let mean n s = let sum = itlist (+.) s 0.0 in sum /. (float_of_int n) in let var n s = let m = mean n s in let m2 = mean n (map (fun x -> x *. x) s) in m2 -. m *. m in fun n data_file pre mk post ?(result = true) f -> let strs = read_file data_file in let data = post (map mk (pre strs)) in let time, r = repeat n data f ([], []) in let r = if result then r else [] in let total = itlist (+.) time 0.0 and m = mean n time and std = sqrt (var n time) in (m, std, total), r;; let show_result = ref true;; let arith_test1d n data op f = run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) (create_op op) ~result:!show_result f;; let arith_test2d n data op f = run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) (create_binop op o data_to_pairs) ~result:!show_result f;; let new_arith_test1d n data f = run_tests n data mk_nums Nat_arith.mk_nat I ~result:!show_result f;; let new_arith_test2d n data f = run_tests n data mk_nums Nat_arith.mk_nat data_to_pairs ~result:!show_result (uncurry f);; (***************************) show_result := true;; let rep = 1;; show_result := false;; let rep = 10;; arith_test1d rep "data/nat15.txt" `SUC` Arith_nat.raw_suc_conv_hash;; new_arith_test1d rep "data/nat15.txt" Nat_arith.nsuc;; (* Addition *) (* 100: 2.189 +/- 0.053 *) arith_test2d rep "data/nat10.txt" `(+):num->num->num` Arith_nat.raw_add_conv_hash;; (* 100: 2.041 +/- 0.024 *) new_arith_test2d rep "data/nat10.txt" Nat_arith.nadd;; (* Subtraction *) (* 100: 3.482 +/- 0.043 *) arith_test2d rep "data/nat10.txt" `(-):num->num->num` Arith_nat.raw_sub_hash_conv;; (* 100: 3.217 +/- 0.065 *) new_arith_test2d rep "data/nat10.txt" Nat_arith.nsub;; (* Multiplication *) (* 100: 36.875 +/- 0.184 *) arith_test2d rep "data/nat10.txt" `( * ):num->num->num` Arith_nat.raw_mul_conv_hash;; (* 100: 35.060 +/- 0.432 *) new_arith_test2d rep "data/nat10.txt" Nat_arith.nmul;; (* Division *) (* 100: +/- *) arith_test2d rep "data/nat10_small.txt" `DIV:num->num->num` Arith_nat.raw_div_hash_conv;; (* 100: +/- *) new_arith_test2d rep "data/nat10_small.txt" Nat_arith.ndiv;; show_result := true;; let data = read_file "data/nat8.txt";; let mk_arith_term = Arith_nat.mk_numeral_array;; let mk_new_arith_term = Nat_arith.mk_nat;;