| 1 |
(* Run hol_test *) |
| 2 |
|
| 3 |
needs "Formal_ineqs/arith/arith_nat.hl";; |
| 4 |
needs "new_arith/nat_arith.hl";; |
| 5 |
|
| 6 |
#load "nums.cma";; |
| 7 |
#load "unix.cma";; |
| 8 |
|
| 9 |
(* Creates all missing directories *) |
| 10 |
let rec create_dirs path = |
| 11 |
let dirname = Filename.dirname path in |
| 12 |
if Sys.file_exists dirname then |
| 13 |
if not (Sys.is_directory dirname) then |
| 14 |
failwith (Printf.sprintf "%s exists and it is not a directory" dirname) |
| 15 |
else |
| 16 |
() |
| 17 |
else |
| 18 |
let _ = create_dirs dirname in |
| 19 |
let _ = Unix.mkdir dirname 0o777 in |
| 20 |
();; |
| 21 |
|
| 22 |
(* Reads all lines from a file *) |
| 23 |
let read_file fname = |
| 24 |
let ic = open_in fname in |
| 25 |
let rec read acc = |
| 26 |
try |
| 27 |
let line = input_line ic in |
| 28 |
read (line :: acc) |
| 29 |
with End_of_file -> acc in |
| 30 |
let lines = read [] in |
| 31 |
let _ = close_in ic in |
| 32 |
rev lines;; |
| 33 |
|
| 34 |
(* Writes all lines to a file *) |
| 35 |
let write_file path buf = |
| 36 |
let _ = create_dirs path in |
| 37 |
let oc = open_out path in |
| 38 |
let rec write = function |
| 39 |
| [] -> () |
| 40 |
| line :: rest -> |
| 41 |
let _ = output_string oc (line ^ "\n") in |
| 42 |
write rest in |
| 43 |
let _ = write buf in |
| 44 |
let _ = close_out oc in |
| 45 |
();; |
| 46 |
|
| 47 |
let rec take n = function |
| 48 |
| [] -> [] |
| 49 |
| h :: t -> |
| 50 |
if n <= 0 then [] else h :: take (n - 1) t;; |
| 51 |
|
| 52 |
let rec drop n = function |
| 53 |
| [] -> [] |
| 54 |
| (h :: t) as s -> |
| 55 |
if n <= 0 then s else drop (n - 1) t;; |
| 56 |
|
| 57 |
let mk_nums = map Num.num_of_string;; |
| 58 |
|
| 59 |
(* Combines consecutive elements into pairs *) |
| 60 |
let rec data_to_pairs = function |
| 61 |
| [] -> [] |
| 62 |
| [h] -> [(h, h)] |
| 63 |
| (h1 :: h2 :: t) -> (h1, h2) :: data_to_pairs t;; |
| 64 |
|
| 65 |
let create_binop op pairs = |
| 66 |
map (uncurry (mk_binop op)) pairs;; |
| 67 |
|
| 68 |
let create_op op data = |
| 69 |
map (curry mk_comb op) data;; |
| 70 |
|
| 71 |
(* Runs a test. *) |
| 72 |
(* n: number of repetitions *) |
| 73 |
(* data_file: input data file *) |
| 74 |
(* pre: function which is applied to the raw data first *) |
| 75 |
(* mk: function which is applied to all elements of the data *) |
| 76 |
(* post: function which is applied to the transformed data *) |
| 77 |
(* f: test function *) |
| 78 |
(* result: if true, then results of f on the data are returned *) |
| 79 |
let run_tests = |
| 80 |
let run data f = |
| 81 |
let start = Unix.gettimeofday() in |
| 82 |
let result = map f data in |
| 83 |
let finish = Unix.gettimeofday() in |
| 84 |
finish -. start, result |
| 85 |
in |
| 86 |
let rec repeat n data f ((ts, r) as acc) = |
| 87 |
if n <= 0 then acc else |
| 88 |
let time, result = run data f in |
| 89 |
repeat (n - 1) data f (time :: ts, result) |
| 90 |
in |
| 91 |
let mean n s = |
| 92 |
let sum = itlist (+.) s 0.0 in |
| 93 |
sum /. (float_of_int n) |
| 94 |
in |
| 95 |
let var n s = |
| 96 |
let m = mean n s in |
| 97 |
let m2 = mean n (map (fun x -> x *. x) s) in |
| 98 |
m2 -. m *. m |
| 99 |
in |
| 100 |
fun n data_file pre mk post ?(result = true) f -> |
| 101 |
let strs = read_file data_file in |
| 102 |
let data = post (map mk (pre strs)) in |
| 103 |
let time, r = repeat n data f ([], []) in |
| 104 |
let r = if result then r else [] in |
| 105 |
let total = itlist (+.) time 0.0 and |
| 106 |
m = mean n time and |
| 107 |
std = sqrt (var n time) in |
| 108 |
(m, std, total), r;; |
| 109 |
|
| 110 |
let show_result = ref true;; |
| 111 |
|
| 112 |
let arith_test1d n data op f = |
| 113 |
run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) |
| 114 |
(create_op op) ~result:!show_result f;; |
| 115 |
|
| 116 |
let arith_test2d n data op f = |
| 117 |
run_tests n data mk_nums (rand o Arith_nat.mk_numeral_array) |
| 118 |
(create_binop op o data_to_pairs) ~result:!show_result f;; |
| 119 |
|
| 120 |
let new_arith_test1d n data f = |
| 121 |
run_tests n data mk_nums Nat_arith.mk_nat |
| 122 |
I ~result:!show_result f;; |
| 123 |
|
| 124 |
let new_arith_test2d n data f = |
| 125 |
run_tests n data mk_nums Nat_arith.mk_nat data_to_pairs |
| 126 |
~result:!show_result (uncurry f);; |
| 127 |
|
| 128 |
|
| 129 |
(***************************) |
| 130 |
|
| 131 |
show_result := true;; |
| 132 |
let rep = 1;; |
| 133 |
|
| 134 |
|
| 135 |
show_result := false;; |
| 136 |
let rep = 10;; |
| 137 |
|
| 138 |
|
| 139 |
arith_test1d rep "data/nat15.txt" |
| 140 |
`SUC` Arith_nat.raw_suc_conv_hash;; |
| 141 |
|
| 142 |
new_arith_test1d rep "data/nat15.txt" |
| 143 |
Nat_arith.nsuc;; |
| 144 |
|
| 145 |
|
| 146 |
(* Addition *) |
| 147 |
|
| 148 |
(* 100: 2.189 +/- 0.053 *) |
| 149 |
arith_test2d rep "data/nat10.txt" |
| 150 |
`(+):num->num->num` |
| 151 |
Arith_nat.raw_add_conv_hash;; |
| 152 |
|
| 153 |
(* 100: 2.041 +/- 0.024 *) |
| 154 |
new_arith_test2d rep "data/nat10.txt" |
| 155 |
Nat_arith.nadd;; |
| 156 |
|
| 157 |
|
| 158 |
(* Subtraction *) |
| 159 |
|
| 160 |
(* 100: 3.482 +/- 0.043 *) |
| 161 |
arith_test2d rep "data/nat10.txt" |
| 162 |
`(-):num->num->num` |
| 163 |
Arith_nat.raw_sub_hash_conv;; |
| 164 |
|
| 165 |
(* 100: 3.217 +/- 0.065 *) |
| 166 |
new_arith_test2d rep "data/nat10.txt" |
| 167 |
Nat_arith.nsub;; |
| 168 |
|
| 169 |
|
| 170 |
(* Multiplication *) |
| 171 |
|
| 172 |
(* 100: 36.875 +/- 0.184 *) |
| 173 |
arith_test2d rep "data/nat10.txt" |
| 174 |
`( * ):num->num->num` |
| 175 |
Arith_nat.raw_mul_conv_hash;; |
| 176 |
|
| 177 |
(* 100: 35.060 +/- 0.432 *) |
| 178 |
new_arith_test2d rep "data/nat10.txt" |
| 179 |
Nat_arith.nmul;; |
| 180 |
|
| 181 |
|
| 182 |
(* Division *) |
| 183 |
|
| 184 |
(* 100: +/- *) |
| 185 |
arith_test2d rep "data/nat10_small.txt" |
| 186 |
`DIV:num->num->num` |
| 187 |
Arith_nat.raw_div_hash_conv;; |
| 188 |
|
| 189 |
(* 100: +/- *) |
| 190 |
new_arith_test2d rep "data/nat10_small.txt" |
| 191 |
Nat_arith.ndiv;; |
| 192 |
|
| 193 |
show_result := true;; |
| 194 |
|
| 195 |
let data = read_file "data/nat8.txt";; |
| 196 |
|
| 197 |
let mk_arith_term = Arith_nat.mk_numeral_array;; |
| 198 |
|
| 199 |
let mk_new_arith_term = Nat_arith.mk_nat;; |
| 200 |
|