| 1 |
(* Run hol_test *) |
| 2 |
|
| 3 |
needs "Formal_ineqs/arith/arith_nat.hl";; |
| 4 |
needs "new_arith/nat_arith.hl";; |
| 5 |
needs "Formal_ineqs/arith/arith_float.hl";; |
| 6 |
|
| 7 |
#load "nums.cma";; |
| 8 |
#load "unix.cma";; |
| 9 |
|
| 10 |
(* Creates all missing directories *) |
| 11 |
let rec create_dirs path = |
| 12 |
let dirname = Filename.dirname path in |
| 13 |
if Sys.file_exists dirname then |
| 14 |
if not (Sys.is_directory dirname) then |
| 15 |
failwith (Printf.sprintf "%s exists and it is not a directory" dirname) |
| 16 |
else |
| 17 |
() |
| 18 |
else |
| 19 |
let _ = create_dirs dirname in |
| 20 |
let _ = Unix.mkdir dirname 0o777 in |
| 21 |
();; |
| 22 |
|
| 23 |
(* Reads all lines from a file *) |
| 24 |
let read_file fname = |
| 25 |
let ic = open_in fname in |
| 26 |
let rec read acc = |
| 27 |
try |
| 28 |
let line = input_line ic in |
| 29 |
read (line :: acc) |
| 30 |
with End_of_file -> acc in |
| 31 |
let lines = read [] in |
| 32 |
let _ = close_in ic in |
| 33 |
rev lines;; |
| 34 |
|
| 35 |
(* Writes all lines to a file *) |
| 36 |
let write_file path buf = |
| 37 |
let _ = create_dirs path in |
| 38 |
let oc = open_out path in |
| 39 |
let rec write = function |
| 40 |
| [] -> () |
| 41 |
| line :: rest -> |
| 42 |
let _ = output_string oc (line ^ "\n") in |
| 43 |
write rest in |
| 44 |
let _ = write buf in |
| 45 |
let _ = close_out oc in |
| 46 |
();; |
| 47 |
|
| 48 |
(* Splits a string into two substrings separated by a given character. *) |
| 49 |
(* The separator is not included in returned substrings. *) |
| 50 |
let split_at str ch = |
| 51 |
let n = String.length str in |
| 52 |
let i = try String.index str ch with Not_found -> -1 in |
| 53 |
if i < 0 || i >= n then |
| 54 |
str, "" |
| 55 |
else |
| 56 |
String.sub str 0 i, |
| 57 |
String.sub str (i + 1) (n - i - 1);; |
| 58 |
|
| 59 |
(* Returns n first elements of a list *) |
| 60 |
let rec take n = function |
| 61 |
| [] -> [] |
| 62 |
| h :: t -> |
| 63 |
if n <= 0 then [] else h :: take (n - 1) t;; |
| 64 |
|
| 65 |
(* Removes n first elements from a list *) |
| 66 |
let rec drop n = function |
| 67 |
| [] -> [] |
| 68 |
| (h :: t) as s -> |
| 69 |
if n <= 0 then s else drop (n - 1) t;; |
| 70 |
|
| 71 |
let uncurry3 f (a, b, c) = f a b c;; |
| 72 |
|
| 73 |
let mk_nums = map Num.num_of_string;; |
| 74 |
|
| 75 |
(* Combines consecutive elements into pairs *) |
| 76 |
let rec data_to_pairs = function |
| 77 |
| [] -> [] |
| 78 |
| [h] -> [(h, h)] |
| 79 |
| (h1 :: h2 :: t) -> (h1, h2) :: data_to_pairs t;; |
| 80 |
|
| 81 |
let create_binop op pairs = |
| 82 |
map (uncurry (mk_binop op)) pairs;; |
| 83 |
|
| 84 |
let create_op op data = |
| 85 |
map (curry mk_comb op) data;; |
| 86 |
|
| 87 |
(* Runs a test. *) |
| 88 |
(* n: number of repetitions *) |
| 89 |
(* data: input data *) |
| 90 |
(* f: test function *) |
| 91 |
(* result: if true, then results of f on the data are returned *) |
| 92 |
let run_tests_raw = |
| 93 |
let run data f = |
| 94 |
let start = Unix.gettimeofday() in |
| 95 |
let result = map f data in |
| 96 |
let finish = Unix.gettimeofday() in |
| 97 |
finish -. start, result |
| 98 |
in |
| 99 |
let rec repeat n data f ((ts, r) as acc) = |
| 100 |
if n <= 0 then acc else |
| 101 |
let time, result = run data f in |
| 102 |
repeat (n - 1) data f (time :: ts, result) |
| 103 |
in |
| 104 |
let mean n s = |
| 105 |
let sum = itlist (+.) s 0.0 in |
| 106 |
sum /. (float_of_int n) |
| 107 |
in |
| 108 |
let var n s = |
| 109 |
let m = mean n s in |
| 110 |
let m2 = mean n (map (fun x -> x *. x) s) in |
| 111 |
m2 -. m *. m |
| 112 |
in |
| 113 |
fun n data ?(result = true) f -> |
| 114 |
let time, r = repeat n data f ([], []) in |
| 115 |
let r = if result then r else [] in |
| 116 |
let total = itlist (+.) time 0.0 and |
| 117 |
m = mean n time and |
| 118 |
std = float_sqrt (var n time) in |
| 119 |
(m, std, total), r;; |
| 120 |
|
| 121 |
let load_data data_file pre mk post = |
| 122 |
let strs = read_file data_file in |
| 123 |
post (map mk (pre strs));; |
| 124 |
|
| 125 |
(* Runs a test. *) |
| 126 |
(* n: number of repetitions *) |
| 127 |
(* data_file: input data file *) |
| 128 |
(* pre: function which is applied to the raw data first *) |
| 129 |
(* mk: function which is applied to all elements of the data *) |
| 130 |
(* post: function which is applied to the transformed data *) |
| 131 |
(* f: test function *) |
| 132 |
(* result: if true, then results of f on the data are returned *) |
| 133 |
let run_tests n data_file pre mk post ?(result = true) f = |
| 134 |
run_tests_raw n (load_data data_file pre mk post) ~result:result f;; |
| 135 |
|
| 136 |
(*********************************************************) |
| 137 |
(* Data conversions *) |
| 138 |
(*********************************************************) |
| 139 |
|
| 140 |
let mk_float_num m e = |
| 141 |
let m, s = if Num.sign_num m < 0 then Num.minus_num m, "T" else m, "F" in |
| 142 |
let m_tm = rand (Arith_nat.mk_numeral_array m) in |
| 143 |
let e_tm = rand (Arith_nat.mk_small_numeral_array (e + Float_theory.min_exp)) in |
| 144 |
Arith_float.make_float s m_tm e_tm;; |
| 145 |
|
| 146 |
let decode_float str = |
| 147 |
let m_str, e_str = split_at str ',' in |
| 148 |
mk_float_num (Num.num_of_string m_str) (int_of_string e_str);; |
| 149 |
|
| 150 |
let convert_float_binop_data str = |
| 151 |
let p_str, str = split_at str ':' in |
| 152 |
let f1_str, f2_str = split_at str ';' in |
| 153 |
int_of_string p_str, decode_float f1_str, decode_float f2_str;; |
| 154 |
|
| 155 |
(*********************************************************) |
| 156 |
(* Test operations *) |
| 157 |
(*********************************************************) |
| 158 |
|
| 159 |
let show_result = ref true;; |
| 160 |
|
| 161 |
let ntest1 n data op f = |
| 162 |
run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) |
| 163 |
(create_op op) ~result:!show_result f;; |
| 164 |
|
| 165 |
let ntest2 n data op f = |
| 166 |
run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) |
| 167 |
(create_binop op o data_to_pairs) ~result:!show_result f;; |
| 168 |
|
| 169 |
let ntest1_new n data f = |
| 170 |
run_tests n data mk_nums Nat_arith.mk_nat |
| 171 |
I ~result:!show_result f;; |
| 172 |
|
| 173 |
let ntest2_new n data f = |
| 174 |
run_tests n data mk_nums Nat_arith.mk_nat data_to_pairs |
| 175 |
~result:!show_result (uncurry f);; |
| 176 |
|
| 177 |
let ftest2 n data f = |
| 178 |
run_tests n data I convert_float_binop_data I |
| 179 |
~result:!show_result (uncurry3 f);; |
| 180 |
|
| 181 |
let ftest2_raw n data f = |
| 182 |
run_tests_raw n data |
| 183 |
~result:!show_result (uncurry3 f);; |
| 184 |
|
| 185 |
|