| 1 |
(* ========================================================================== *) |
| 2 |
(* Formal verification of nonlinear inequalities in HOL Light *) |
| 3 |
(* *) |
| 4 |
(* Copyright (c) 2012 Alexey Solovyev *) |
| 5 |
(* *) |
| 6 |
(* This file is distributed under the terms of the MIT licence *) |
| 7 |
(* ========================================================================== *) |
| 8 |
|
| 9 |
(* -------------------------------------------------------------------------- *) |
| 10 |
(* Miscellaneous functions *) |
| 11 |
(* -------------------------------------------------------------------------- *) |
| 12 |
|
| 13 |
#load "unix.cma";; |
| 14 |
|
| 15 |
module Misc_functions = struct |
| 16 |
|
| 17 |
exception Error of string * term list * thm list;; |
| 18 |
|
| 19 |
let error msg tms ths = raise (Error (msg, tms, ths));; |
| 20 |
|
| 21 |
let error_msg msg = raise (Error (msg, [], []));; |
| 22 |
|
| 23 |
let error_fmt str fmt = |
| 24 |
let msg = sprintf str fmt in |
| 25 |
error_msg msg;; |
| 26 |
|
| 27 |
(* A little faster version of PROVE_HYP *) |
| 28 |
let MY_PROVE_HYP hyp th = EQ_MP (DEDUCT_ANTISYM_RULE hyp th) hyp;; |
| 29 |
|
| 30 |
(* A faster version of BETA_RULE *) |
| 31 |
let MY_BETA_RULE th = |
| 32 |
let rec beta tm = |
| 33 |
let op, arg = dest_comb tm in |
| 34 |
if is_comb op then |
| 35 |
let op_th = AP_THM (beta op) arg in |
| 36 |
let beta_th = BETA_CONV (rand (concl op_th)) in |
| 37 |
TRANS op_th beta_th |
| 38 |
else |
| 39 |
BETA_CONV tm in |
| 40 |
EQ_MP (beta (concl th)) th;; |
| 41 |
|
| 42 |
(* Converts a list with two elements into a pair *) |
| 43 |
let pair_of_list = function |
| 44 |
| [a; b] -> a, b |
| 45 |
| _ -> failwith "pair_of_list: list does not contain exactly two element";; |
| 46 |
|
| 47 |
(* Applies f to arg n times and returns the total execution time *) |
| 48 |
let test n f arg = |
| 49 |
let start = Unix.gettimeofday() in |
| 50 |
let result = f arg in |
| 51 |
for i = 1 to n - 1 do |
| 52 |
let _ = f arg in () |
| 53 |
done; |
| 54 |
result, Unix.gettimeofday() -. start;; |
| 55 |
|
| 56 |
(* Do not use when msg is not a fixed string *) |
| 57 |
let assert_msg (cond, msg) = |
| 58 |
if cond then () else failwith msg;; |
| 59 |
|
| 60 |
(* Generates a power function for the given binary operation *) |
| 61 |
let gen_pow op id = |
| 62 |
let ( * ) = op in |
| 63 |
let rec pow n x = |
| 64 |
if n <= 0 then id |
| 65 |
else if n = 1 then x |
| 66 |
else if n land 1 = 1 then |
| 67 |
x * pow (n - 1) x |
| 68 |
else |
| 69 |
let t = pow (n lsr 1) x in |
| 70 |
t * t in |
| 71 |
pow;; |
| 72 |
|
| 73 |
let rec shape_list n list = |
| 74 |
if length list <= n then [list] |
| 75 |
else |
| 76 |
let l1, l2 = chop_list n list in |
| 77 |
l1 :: shape_list n l2;; |
| 78 |
|
| 79 |
(* map2 which works for lists of any size (no requirement |l1| = |l2|) *) |
| 80 |
let rec my_map2 f l1 l2 = |
| 81 |
match l1 with |
| 82 |
| [] -> [] |
| 83 |
| (h1::t1) -> |
| 84 |
(match l2 with |
| 85 |
| [] -> [] |
| 86 |
| (h2::t2) -> (f h1 h2) :: my_map2 f t1 t2);; |
| 87 |
|
| 88 |
end;; |