src/HOL/Library/positivstellensatz.ML
changeset 31120 fc654c95c29e
child 31971 8c1b845ed105
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/positivstellensatz.ML	Tue May 12 17:32:50 2009 +0100
     1.3 @@ -0,0 +1,787 @@
     1.4 +(* Title:      Library/positivstellensatz
     1.5 +   Author:     Amine Chaieb, University of Cambridge
     1.6 +   Description: A generic arithmetic prover based on Positivstellensatz certificates --- 
     1.7 +    also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination.
     1.8 +*)
     1.9 +
    1.10 +(* A functor for finite mappings based on Tables *)
    1.11 +signature FUNC = 
    1.12 +sig
    1.13 + type 'a T
    1.14 + type key
    1.15 + val apply : 'a T -> key -> 'a
    1.16 + val applyd :'a T -> (key -> 'a) -> key -> 'a
    1.17 + val combine : ('a -> 'a -> 'a) -> ('a -> bool) -> 'a T -> 'a T -> 'a T
    1.18 + val defined : 'a T -> key -> bool
    1.19 + val dom : 'a T -> key list
    1.20 + val fold : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b
    1.21 + val fold_rev : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b
    1.22 + val graph : 'a T -> (key * 'a) list
    1.23 + val is_undefined : 'a T -> bool
    1.24 + val mapf : ('a -> 'b) -> 'a T -> 'b T
    1.25 + val tryapplyd : 'a T -> key -> 'a -> 'a
    1.26 + val undefine :  key -> 'a T -> 'a T
    1.27 + val undefined : 'a T
    1.28 + val update : key * 'a -> 'a T -> 'a T
    1.29 + val updatep : (key * 'a -> bool) -> key * 'a -> 'a T -> 'a T
    1.30 + val choose : 'a T -> key * 'a
    1.31 + val onefunc : key * 'a -> 'a T
    1.32 + val get_first: (key*'a -> 'a option) -> 'a T -> 'a option
    1.33 +end;
    1.34 +
    1.35 +functor FuncFun(Key: KEY) : FUNC=
    1.36 +struct
    1.37 +
    1.38 +type key = Key.key;
    1.39 +structure Tab = TableFun(Key);
    1.40 +type 'a T = 'a Tab.table;
    1.41 +
    1.42 +val undefined = Tab.empty;
    1.43 +val is_undefined = Tab.is_empty;
    1.44 +val mapf = Tab.map;
    1.45 +val fold = Tab.fold;
    1.46 +val fold_rev = Tab.fold_rev;
    1.47 +val graph = Tab.dest;
    1.48 +fun dom a = sort Key.ord (Tab.keys a);
    1.49 +fun applyd f d x = case Tab.lookup f x of 
    1.50 +   SOME y => y
    1.51 + | NONE => d x;
    1.52 +
    1.53 +fun apply f x = applyd f (fn _ => raise Tab.UNDEF x) x;
    1.54 +fun tryapplyd f a d = applyd f (K d) a;
    1.55 +val defined = Tab.defined;
    1.56 +fun undefine x t = (Tab.delete x t handle UNDEF => t);
    1.57 +val update = Tab.update;
    1.58 +fun updatep p (k,v) t = if p (k, v) then t else update (k,v) t
    1.59 +fun combine f z a b = 
    1.60 + let
    1.61 +  fun h (k,v) t = case Tab.lookup t k of
    1.62 +     NONE => Tab.update (k,v) t
    1.63 +   | SOME v' => let val w = f v v'
    1.64 +     in if z w then Tab.delete k t else Tab.update (k,w) t end;
    1.65 +  in Tab.fold h a b end;
    1.66 +
    1.67 +fun choose f = case Tab.min_key f of 
    1.68 +   SOME k => (k,valOf (Tab.lookup f k))
    1.69 + | NONE => error "FuncFun.choose : Completely undefined function"
    1.70 +
    1.71 +fun onefunc kv = update kv undefined
    1.72 +
    1.73 +local
    1.74 +fun  find f (k,v) NONE = f (k,v)
    1.75 +   | find f (k,v) r = r
    1.76 +in
    1.77 +fun get_first f t = fold (find f) t NONE
    1.78 +end
    1.79 +end;
    1.80 +
    1.81 +structure Intfunc = FuncFun(type key = int val ord = int_ord);
    1.82 +structure Symfunc = FuncFun(type key = string val ord = fast_string_ord);
    1.83 +structure Termfunc = FuncFun(type key = term val ord = TermOrd.fast_term_ord);
    1.84 +structure Ctermfunc = FuncFun(type key = cterm val ord = (fn (s,t) => TermOrd.fast_term_ord(term_of s, term_of t)));
    1.85 +
    1.86 +structure Ratfunc = FuncFun(type key = Rat.rat val ord = Rat.ord);
    1.87 +    (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*)
    1.88 +structure Conv2 = 
    1.89 +struct
    1.90 + open Conv
    1.91 +fun instantiate_cterm' ty tms = Drule.cterm_rule (Drule.instantiate' ty tms)
    1.92 +fun is_comb t = case (term_of t) of _$_ => true | _ => false;
    1.93 +fun is_abs t = case (term_of t) of Abs _ => true | _ => false;
    1.94 +
    1.95 +fun end_itlist f l =
    1.96 + case l of 
    1.97 +   []     => error "end_itlist"
    1.98 + | [x]    => x
    1.99 + | (h::t) => f h (end_itlist f t);
   1.100 +
   1.101 + fun absc cv ct = case term_of ct of 
   1.102 + Abs (v,_, _) => 
   1.103 +  let val (x,t) = Thm.dest_abs (SOME v) ct
   1.104 +  in Thm.abstract_rule ((fst o dest_Free o term_of) x) x (cv t)
   1.105 +  end
   1.106 + | _ => all_conv ct;
   1.107 +
   1.108 +fun cache_conv conv =
   1.109 + let 
   1.110 +  val tab = ref Termtab.empty
   1.111 +  fun cconv t =  
   1.112 +    case Termtab.lookup (!tab) (term_of t) of
   1.113 +     SOME th => th
   1.114 +   | NONE => let val th = conv t
   1.115 +             in ((tab := Termtab.insert Thm.eq_thm (term_of t, th) (!tab)); th) end
   1.116 + in cconv end;
   1.117 +fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct'))
   1.118 +  handle CTERM _ => false;
   1.119 +
   1.120 +local
   1.121 + fun thenqc conv1 conv2 tm =
   1.122 +   case try conv1 tm of
   1.123 +    SOME th1 => (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
   1.124 +  | NONE => conv2 tm
   1.125 +
   1.126 + fun thencqc conv1 conv2 tm =
   1.127 +    let val th1 = conv1 tm 
   1.128 +    in (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
   1.129 +    end
   1.130 + fun comb_qconv conv tm =
   1.131 +   let val (l,r) = Thm.dest_comb tm 
   1.132 +   in (case try conv l of 
   1.133 +        SOME th1 => (case try conv r of SOME th2 => Thm.combination th1 th2 
   1.134 +                                      | NONE => Drule.fun_cong_rule th1 r)
   1.135 +      | NONE => Drule.arg_cong_rule l (conv r))
   1.136 +   end
   1.137 + fun repeatqc conv tm = thencqc conv (repeatqc conv) tm 
   1.138 + fun sub_qconv conv tm =  if is_abs tm then absc conv tm else comb_qconv conv tm 
   1.139 + fun once_depth_qconv conv tm =
   1.140 +      (conv else_conv (sub_qconv (once_depth_qconv conv))) tm
   1.141 + fun depth_qconv conv tm =
   1.142 +    thenqc (sub_qconv (depth_qconv conv))
   1.143 +           (repeatqc conv) tm
   1.144 + fun redepth_qconv conv tm =
   1.145 +    thenqc (sub_qconv (redepth_qconv conv))
   1.146 +           (thencqc conv (redepth_qconv conv)) tm
   1.147 + fun top_depth_qconv conv tm =
   1.148 +    thenqc (repeatqc conv)
   1.149 +           (thencqc (sub_qconv (top_depth_qconv conv))
   1.150 +                    (thencqc conv (top_depth_qconv conv))) tm
   1.151 + fun top_sweep_qconv conv tm =
   1.152 +    thenqc (repeatqc conv)
   1.153 +           (sub_qconv (top_sweep_qconv conv)) tm
   1.154 +in 
   1.155 +val (once_depth_conv, depth_conv, rdepth_conv, top_depth_conv, top_sweep_conv) = 
   1.156 +  (fn c => try_conv (once_depth_qconv c),
   1.157 +   fn c => try_conv (depth_qconv c),
   1.158 +   fn c => try_conv (redepth_qconv c),
   1.159 +   fn c => try_conv (top_depth_qconv c),
   1.160 +   fn c => try_conv (top_sweep_qconv c));
   1.161 +end;
   1.162 +end;
   1.163 +
   1.164 +
   1.165 +    (* Some useful derived rules *)
   1.166 +fun deduct_antisym_rule tha thb = 
   1.167 +    equal_intr (implies_intr (cprop_of thb) tha) 
   1.168 +     (implies_intr (cprop_of tha) thb);
   1.169 +
   1.170 +fun prove_hyp tha thb = 
   1.171 +  if exists (curry op aconv (concl_of tha)) (#hyps (rep_thm thb)) 
   1.172 +  then equal_elim (symmetric (deduct_antisym_rule tha thb)) tha else thb;
   1.173 +
   1.174 +
   1.175 +
   1.176 +signature REAL_ARITH = 
   1.177 +sig
   1.178 +  datatype positivstellensatz =
   1.179 +   Axiom_eq of int
   1.180 + | Axiom_le of int
   1.181 + | Axiom_lt of int
   1.182 + | Rational_eq of Rat.rat
   1.183 + | Rational_le of Rat.rat
   1.184 + | Rational_lt of Rat.rat
   1.185 + | Square of cterm
   1.186 + | Eqmul of cterm * positivstellensatz
   1.187 + | Sum of positivstellensatz * positivstellensatz
   1.188 + | Product of positivstellensatz * positivstellensatz;
   1.189 +
   1.190 +val gen_gen_real_arith :
   1.191 +  Proof.context -> (Rat.rat -> Thm.cterm) * conv * conv * conv * 
   1.192 +   conv * conv * conv * conv * conv * conv * 
   1.193 +    ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
   1.194 +        thm list * thm list * thm list -> thm) -> conv
   1.195 +val real_linear_prover : 
   1.196 +  (thm list * thm list * thm list -> positivstellensatz -> thm) ->
   1.197 +   thm list * thm list * thm list -> thm
   1.198 +
   1.199 +val gen_real_arith : Proof.context ->
   1.200 +   (Rat.rat -> cterm) * conv * conv * conv * conv * conv * conv * conv *
   1.201 +   ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
   1.202 +       thm list * thm list * thm list -> thm) -> conv
   1.203 +val gen_prover_real_arith : Proof.context ->
   1.204 +   ((thm list * thm list * thm list -> positivstellensatz -> thm) ->
   1.205 +     thm list * thm list * thm list -> thm) -> conv
   1.206 +val real_arith : Proof.context -> conv
   1.207 +end
   1.208 +
   1.209 +structure RealArith (* : REAL_ARITH *)=
   1.210 +struct
   1.211 +
   1.212 + open Conv Thm;;
   1.213 +(* ------------------------------------------------------------------------- *)
   1.214 +(* Data structure for Positivstellensatz refutations.                        *)
   1.215 +(* ------------------------------------------------------------------------- *)
   1.216 +
   1.217 +datatype positivstellensatz =
   1.218 +   Axiom_eq of int
   1.219 + | Axiom_le of int
   1.220 + | Axiom_lt of int
   1.221 + | Rational_eq of Rat.rat
   1.222 + | Rational_le of Rat.rat
   1.223 + | Rational_lt of Rat.rat
   1.224 + | Square of cterm
   1.225 + | Eqmul of cterm * positivstellensatz
   1.226 + | Sum of positivstellensatz * positivstellensatz
   1.227 + | Product of positivstellensatz * positivstellensatz;
   1.228 +         (* Theorems used in the procedure *)
   1.229 +
   1.230 +
   1.231 +val my_eqs = ref ([] : thm list);
   1.232 +val my_les = ref ([] : thm list);
   1.233 +val my_lts = ref ([] : thm list);
   1.234 +val my_proof = ref (Axiom_eq 0);
   1.235 +val my_context = ref @{context};
   1.236 +
   1.237 +val my_mk_numeric = ref ((K @{cterm True}) :Rat.rat -> cterm);
   1.238 +val my_numeric_eq_conv = ref no_conv;
   1.239 +val my_numeric_ge_conv = ref no_conv;
   1.240 +val my_numeric_gt_conv = ref no_conv;
   1.241 +val my_poly_conv = ref no_conv;
   1.242 +val my_poly_neg_conv = ref no_conv;
   1.243 +val my_poly_add_conv = ref no_conv;
   1.244 +val my_poly_mul_conv = ref no_conv;
   1.245 +
   1.246 +fun conjunctions th = case try Conjunction.elim th of
   1.247 +   SOME (th1,th2) => (conjunctions th1) @ conjunctions th2
   1.248 + | NONE => [th];
   1.249 +
   1.250 +val pth = @{lemma "(((x::real) < y) == (y - x > 0)) &&& ((x <= y) == (y - x >= 0)) 
   1.251 +     &&& ((x = y) == (x - y = 0)) &&& ((~(x < y)) == (x - y >= 0)) &&& ((~(x <= y)) == (x - y > 0))
   1.252 +     &&& ((~(x = y)) == (x - y > 0 | -(x - y) > 0))"
   1.253 +  by (atomize (full), auto simp add: less_diff_eq le_diff_eq not_less)} |> 
   1.254 +conjunctions;
   1.255 +
   1.256 +val pth_final = @{lemma "(~p ==> False) ==> p" by blast}
   1.257 +val pth_add = 
   1.258 + @{lemma "(x = (0::real) ==> y = 0 ==> x + y = 0 ) &&& ( x = 0 ==> y >= 0 ==> x + y >= 0) 
   1.259 +    &&& (x = 0 ==> y > 0 ==> x + y > 0) &&& (x >= 0 ==> y = 0 ==> x + y >= 0) 
   1.260 +    &&& (x >= 0 ==> y >= 0 ==> x + y >= 0) &&& (x >= 0 ==> y > 0 ==> x + y > 0) 
   1.261 +    &&& (x > 0 ==> y = 0 ==> x + y > 0) &&& (x > 0 ==> y >= 0 ==> x + y > 0) 
   1.262 +    &&& (x > 0 ==> y > 0 ==> x + y > 0)"  by simp_all} |> conjunctions ;
   1.263 +
   1.264 +val pth_mul = 
   1.265 +  @{lemma "(x = (0::real) ==> y = 0 ==> x * y = 0) &&& (x = 0 ==> y >= 0 ==> x * y = 0) &&& 
   1.266 +           (x = 0 ==> y > 0 ==> x * y = 0) &&& (x >= 0 ==> y = 0 ==> x * y = 0) &&& 
   1.267 +           (x >= 0 ==> y >= 0 ==> x * y >= 0 ) &&& ( x >= 0 ==> y > 0 ==> x * y >= 0 ) &&&
   1.268 +           (x > 0 ==>  y = 0 ==> x * y = 0 ) &&& ( x > 0 ==> y >= 0 ==> x * y >= 0 ) &&&
   1.269 +           (x > 0 ==>  y > 0 ==> x * y > 0)"
   1.270 +  by (auto intro: mult_mono[where a="0::real" and b="x" and d="y" and c="0", simplified]
   1.271 +    mult_strict_mono[where b="x" and d="y" and a="0" and c="0", simplified])} |> conjunctions;
   1.272 +
   1.273 +val pth_emul = @{lemma "y = (0::real) ==> x * y = 0"  by simp};
   1.274 +val pth_square = @{lemma "x * x >= (0::real)"  by simp};
   1.275 +
   1.276 +val weak_dnf_simps = List.take (simp_thms, 34) 
   1.277 +    @ conjunctions @{lemma "((P & (Q | R)) = ((P&Q) | (P&R))) &&& ((Q | R) & P) = ((Q&P) | (R&P)) &&& (P & Q) = (Q & P) &&& ((P | Q) = (Q | P))" by blast+};
   1.278 +
   1.279 +val nnfD_simps = conjunctions @{lemma "((~(P & Q)) = (~P | ~Q)) &&& ((~(P | Q)) = (~P & ~Q) ) &&& ((P --> Q) = (~P | Q) ) &&& ((P = Q) = ((P & Q) | (~P & ~ Q))) &&& ((~(P = Q)) = ((P & ~ Q) | (~P & Q)) ) &&& ((~ ~(P)) = P)" by blast+}
   1.280 +
   1.281 +val choice_iff = @{lemma "(ALL x. EX y. P x y) = (EX f. ALL x. P x (f x))" by metis};
   1.282 +val prenex_simps = map (fn th => th RS sym) ([@{thm "all_conj_distrib"}, @{thm "ex_disj_distrib"}] @ @{thms "all_simps"(1-4)} @ @{thms "ex_simps"(1-4)});
   1.283 +
   1.284 +val real_abs_thms1 = conjunctions @{lemma
   1.285 +  "((-1 * abs(x::real) >= r) = (-1 * x >= r & 1 * x >= r)) &&&
   1.286 +  ((-1 * abs(x) + a >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
   1.287 +  ((a + -1 * abs(x) >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
   1.288 +  ((a + -1 * abs(x) + b >= r) = (a + -1 * x + b >= r & a + 1 * x + b >= r)) &&&
   1.289 +  ((a + b + -1 * abs(x) >= r) = (a + b + -1 * x >= r & a + b + 1 * x >= r)) &&&
   1.290 +  ((a + b + -1 * abs(x) + c >= r) = (a + b + -1 * x + c >= r & a + b + 1 * x + c >= r)) &&&
   1.291 +  ((-1 * max x y >= r) = (-1 * x >= r & -1 * y >= r)) &&&
   1.292 +  ((-1 * max x y + a >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
   1.293 +  ((a + -1 * max x y >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
   1.294 +  ((a + -1 * max x y + b >= r) = (a + -1 * x + b >= r & a + -1 * y  + b >= r)) &&&
   1.295 +  ((a + b + -1 * max x y >= r) = (a + b + -1 * x >= r & a + b + -1 * y >= r)) &&&
   1.296 +  ((a + b + -1 * max x y + c >= r) = (a + b + -1 * x + c >= r & a + b + -1 * y  + c >= r)) &&&
   1.297 +  ((1 * min x y >= r) = (1 * x >= r & 1 * y >= r)) &&&
   1.298 +  ((1 * min x y + a >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
   1.299 +  ((a + 1 * min x y >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
   1.300 +  ((a + 1 * min x y + b >= r) = (a + 1 * x + b >= r & a + 1 * y  + b >= r) )&&&
   1.301 +  ((a + b + 1 * min x y >= r) = (a + b + 1 * x >= r & a + b + 1 * y >= r)) &&&
   1.302 +  ((a + b + 1 * min x y + c >= r) = (a + b + 1 * x + c >= r & a + b + 1 * y  + c >= r)) &&&
   1.303 +  ((min x y >= r) = (x >= r &  y >= r)) &&&
   1.304 +  ((min x y + a >= r) = (a + x >= r & a + y >= r)) &&&
   1.305 +  ((a + min x y >= r) = (a + x >= r & a + y >= r)) &&&
   1.306 +  ((a + min x y + b >= r) = (a + x + b >= r & a + y  + b >= r)) &&&
   1.307 +  ((a + b + min x y >= r) = (a + b + x >= r & a + b + y >= r) )&&&
   1.308 +  ((a + b + min x y + c >= r) = (a + b + x + c >= r & a + b + y + c >= r)) &&&
   1.309 +  ((-1 * abs(x) > r) = (-1 * x > r & 1 * x > r)) &&&
   1.310 +  ((-1 * abs(x) + a > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
   1.311 +  ((a + -1 * abs(x) > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
   1.312 +  ((a + -1 * abs(x) + b > r) = (a + -1 * x + b > r & a + 1 * x + b > r)) &&&
   1.313 +  ((a + b + -1 * abs(x) > r) = (a + b + -1 * x > r & a + b + 1 * x > r)) &&&
   1.314 +  ((a + b + -1 * abs(x) + c > r) = (a + b + -1 * x + c > r & a + b + 1 * x + c > r)) &&&
   1.315 +  ((-1 * max x y > r) = ((-1 * x > r) & -1 * y > r)) &&&
   1.316 +  ((-1 * max x y + a > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
   1.317 +  ((a + -1 * max x y > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
   1.318 +  ((a + -1 * max x y + b > r) = (a + -1 * x + b > r & a + -1 * y  + b > r)) &&&
   1.319 +  ((a + b + -1 * max x y > r) = (a + b + -1 * x > r & a + b + -1 * y > r)) &&&
   1.320 +  ((a + b + -1 * max x y + c > r) = (a + b + -1 * x + c > r & a + b + -1 * y  + c > r)) &&&
   1.321 +  ((min x y > r) = (x > r &  y > r)) &&&
   1.322 +  ((min x y + a > r) = (a + x > r & a + y > r)) &&&
   1.323 +  ((a + min x y > r) = (a + x > r & a + y > r)) &&&
   1.324 +  ((a + min x y + b > r) = (a + x + b > r & a + y  + b > r)) &&&
   1.325 +  ((a + b + min x y > r) = (a + b + x > r & a + b + y > r)) &&&
   1.326 +  ((a + b + min x y + c > r) = (a + b + x + c > r & a + b + y + c > r))"
   1.327 +  by auto};
   1.328 +
   1.329 +val abs_split' = @{lemma "P (abs (x::'a::ordered_idom)) == (x >= 0 & P x | x < 0 & P (-x))"
   1.330 +  by (atomize (full)) (auto split add: abs_split)};
   1.331 +
   1.332 +val max_split = @{lemma "P (max x y) == ((x::'a::linorder) <= y & P y | x > y & P x)"
   1.333 +  by (atomize (full)) (cases "x <= y", auto simp add: max_def)};
   1.334 +
   1.335 +val min_split = @{lemma "P (min x y) == ((x::'a::linorder) <= y & P x | x > y & P y)"
   1.336 +  by (atomize (full)) (cases "x <= y", auto simp add: min_def)};
   1.337 +
   1.338 +
   1.339 +         (* Miscalineous *)
   1.340 +fun literals_conv bops uops cv = 
   1.341 + let fun h t =
   1.342 +  case (term_of t) of 
   1.343 +   b$_$_ => if member (op aconv) bops b then binop_conv h t else cv t
   1.344 + | u$_ => if member (op aconv) uops u then arg_conv h t else cv t
   1.345 + | _ => cv t
   1.346 + in h end;
   1.347 +
   1.348 +fun cterm_of_rat x = 
   1.349 +let val (a, b) = Rat.quotient_of_rat x
   1.350 +in 
   1.351 + if b = 1 then Numeral.mk_cnumber @{ctyp "real"} a
   1.352 +  else Thm.capply (Thm.capply @{cterm "op / :: real => _"} 
   1.353 +                   (Numeral.mk_cnumber @{ctyp "real"} a))
   1.354 +        (Numeral.mk_cnumber @{ctyp "real"} b)
   1.355 +end;
   1.356 +
   1.357 +  fun dest_ratconst t = case term_of t of
   1.358 +   Const(@{const_name divide}, _)$a$b => Rat.rat_of_quotient(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
   1.359 + | _ => Rat.rat_of_int (HOLogic.dest_number (term_of t) |> snd)
   1.360 + fun is_ratconst t = can dest_ratconst t
   1.361 +
   1.362 +fun find_term p t = if p t then t else 
   1.363 + case t of
   1.364 +  a$b => (find_term p a handle TERM _ => find_term p b)
   1.365 + | Abs (_,_,t') => find_term p t'
   1.366 + | _ => raise TERM ("find_term",[t]);
   1.367 +
   1.368 +fun find_cterm p t = if p t then t else 
   1.369 + case term_of t of
   1.370 +  a$b => (find_cterm p (Thm.dest_fun t) handle CTERM _ => find_cterm p (Thm.dest_arg t))
   1.371 + | Abs (_,_,t') => find_cterm p (Thm.dest_abs NONE t |> snd)
   1.372 + | _ => raise CTERM ("find_cterm",[t]);
   1.373 +
   1.374 +
   1.375 +    (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*)
   1.376 +fun instantiate_cterm' ty tms = Drule.cterm_rule (Drule.instantiate' ty tms)
   1.377 +fun is_comb t = case (term_of t) of _$_ => true | _ => false;
   1.378 +
   1.379 +fun cache_conv conv =
   1.380 + let 
   1.381 +  val tab = ref Termtab.empty
   1.382 +  fun cconv t =  
   1.383 +    case Termtab.lookup (!tab) (term_of t) of
   1.384 +     SOME th => th
   1.385 +   | NONE => let val th = conv t
   1.386 +             in ((tab := Termtab.insert Thm.eq_thm (term_of t, th) (!tab)); th) end
   1.387 + in cconv end;
   1.388 +fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct'))
   1.389 +  handle CTERM _ => false;
   1.390 +
   1.391 +    (* A general real arithmetic prover *)
   1.392 +
   1.393 +fun gen_gen_real_arith ctxt (mk_numeric,
   1.394 +       numeric_eq_conv,numeric_ge_conv,numeric_gt_conv,
   1.395 +       poly_conv,poly_neg_conv,poly_add_conv,poly_mul_conv,
   1.396 +       absconv1,absconv2,prover) = 
   1.397 +let
   1.398 + open Conv Thm;
   1.399 + val _ = my_context := ctxt 
   1.400 + val _ = (my_mk_numeric := mk_numeric ; my_numeric_eq_conv := numeric_eq_conv ; 
   1.401 +          my_numeric_ge_conv := numeric_ge_conv; my_numeric_gt_conv := numeric_gt_conv ;
   1.402 +          my_poly_conv := poly_conv; my_poly_neg_conv := poly_neg_conv; 
   1.403 +          my_poly_add_conv := poly_add_conv; my_poly_mul_conv := poly_mul_conv)
   1.404 + val pre_ss = HOL_basic_ss addsimps simp_thms@ ex_simps@ all_simps@[@{thm not_all},@{thm not_ex},ex_disj_distrib, all_conj_distrib, @{thm if_bool_eq_disj}]
   1.405 + val prenex_ss = HOL_basic_ss addsimps prenex_simps
   1.406 + val skolemize_ss = HOL_basic_ss addsimps [choice_iff]
   1.407 + val presimp_conv = Simplifier.rewrite (Simplifier.context ctxt pre_ss)
   1.408 + val prenex_conv = Simplifier.rewrite (Simplifier.context ctxt prenex_ss)
   1.409 + val skolemize_conv = Simplifier.rewrite (Simplifier.context ctxt skolemize_ss)
   1.410 + val weak_dnf_ss = HOL_basic_ss addsimps weak_dnf_simps
   1.411 + val weak_dnf_conv = Simplifier.rewrite (Simplifier.context ctxt weak_dnf_ss)
   1.412 + fun eqT_elim th = equal_elim (symmetric th) @{thm TrueI}
   1.413 + fun oprconv cv ct = 
   1.414 +  let val g = Thm.dest_fun2 ct
   1.415 +  in if g aconvc @{cterm "op <= :: real => _"} 
   1.416 +       orelse g aconvc @{cterm "op < :: real => _"} 
   1.417 +     then arg_conv cv ct else arg1_conv cv ct
   1.418 +  end
   1.419 +
   1.420 + fun real_ineq_conv th ct =
   1.421 +  let
   1.422 +   val th' = (instantiate (match (lhs_of th, ct)) th 
   1.423 +      handle MATCH => raise CTERM ("real_ineq_conv", [ct]))
   1.424 +  in transitive th' (oprconv poly_conv (Thm.rhs_of th'))
   1.425 +  end 
   1.426 +  val [real_lt_conv, real_le_conv, real_eq_conv,
   1.427 +       real_not_lt_conv, real_not_le_conv, _] =
   1.428 +       map real_ineq_conv pth
   1.429 +  fun match_mp_rule ths ths' = 
   1.430 +   let
   1.431 +     fun f ths ths' = case ths of [] => raise THM("match_mp_rule",0,ths)
   1.432 +      | th::ths => (ths' MRS th handle THM _ => f ths ths')
   1.433 +   in f ths ths' end
   1.434 +  fun mul_rule th th' = fconv_rule (arg_conv (oprconv poly_mul_conv))
   1.435 +         (match_mp_rule pth_mul [th, th'])
   1.436 +  fun add_rule th th' = fconv_rule (arg_conv (oprconv poly_add_conv))
   1.437 +         (match_mp_rule pth_add [th, th'])
   1.438 +  fun emul_rule ct th = fconv_rule (arg_conv (oprconv poly_mul_conv)) 
   1.439 +       (instantiate' [] [SOME ct] (th RS pth_emul)) 
   1.440 +  fun square_rule t = fconv_rule (arg_conv (oprconv poly_conv))
   1.441 +       (instantiate' [] [SOME t] pth_square)
   1.442 +
   1.443 +  fun hol_of_positivstellensatz(eqs,les,lts) proof =
   1.444 +   let 
   1.445 +    val _ = (my_eqs := eqs ; my_les := les ; my_lts := lts ; my_proof := proof)
   1.446 +    fun translate prf = case prf of
   1.447 +        Axiom_eq n => nth eqs n
   1.448 +      | Axiom_le n => nth les n
   1.449 +      | Axiom_lt n => nth lts n
   1.450 +      | Rational_eq x => eqT_elim(numeric_eq_conv(capply @{cterm Trueprop} 
   1.451 +                          (capply (capply @{cterm "op =::real => _"} (mk_numeric x)) 
   1.452 +                               @{cterm "0::real"})))
   1.453 +      | Rational_le x => eqT_elim(numeric_ge_conv(capply @{cterm Trueprop} 
   1.454 +                          (capply (capply @{cterm "op <=::real => _"} 
   1.455 +                                     @{cterm "0::real"}) (mk_numeric x))))
   1.456 +      | Rational_lt x => eqT_elim(numeric_gt_conv(capply @{cterm Trueprop} 
   1.457 +                      (capply (capply @{cterm "op <::real => _"} @{cterm "0::real"})
   1.458 +                        (mk_numeric x))))
   1.459 +      | Square t => square_rule t
   1.460 +      | Eqmul(t,p) => emul_rule t (translate p)
   1.461 +      | Sum(p1,p2) => add_rule (translate p1) (translate p2)
   1.462 +      | Product(p1,p2) => mul_rule (translate p1) (translate p2)
   1.463 +   in fconv_rule (first_conv [numeric_ge_conv, numeric_gt_conv, numeric_eq_conv, all_conv]) 
   1.464 +          (translate proof)
   1.465 +   end
   1.466 +  
   1.467 +  val init_conv = presimp_conv then_conv
   1.468 +      nnf_conv then_conv skolemize_conv then_conv prenex_conv then_conv
   1.469 +      weak_dnf_conv
   1.470 +
   1.471 +  val concl = dest_arg o cprop_of
   1.472 +  fun is_binop opr ct = (dest_fun2 ct aconvc opr handle CTERM _ => false)
   1.473 +  val is_req = is_binop @{cterm "op =:: real => _"}
   1.474 +  val is_ge = is_binop @{cterm "op <=:: real => _"}
   1.475 +  val is_gt = is_binop @{cterm "op <:: real => _"}
   1.476 +  val is_conj = is_binop @{cterm "op &"}
   1.477 +  val is_disj = is_binop @{cterm "op |"}
   1.478 +  fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
   1.479 +  fun disj_cases th th1 th2 = 
   1.480 +   let val (p,q) = dest_binop (concl th)
   1.481 +       val c = concl th1
   1.482 +       val _ = if c aconvc (concl th2) then () else error "disj_cases : conclusions not alpha convertible"
   1.483 +   in implies_elim (implies_elim (implies_elim (instantiate' [] (map SOME [p,q,c]) @{thm disjE}) th) (implies_intr (capply @{cterm Trueprop} p) th1)) (implies_intr (capply @{cterm Trueprop} q) th2)
   1.484 +   end
   1.485 + fun overall dun ths = case ths of
   1.486 +  [] =>
   1.487 +   let 
   1.488 +    val (eq,ne) = List.partition (is_req o concl) dun
   1.489 +     val (le,nl) = List.partition (is_ge o concl) ne
   1.490 +     val lt = filter (is_gt o concl) nl 
   1.491 +    in prover hol_of_positivstellensatz (eq,le,lt) end
   1.492 + | th::oths =>
   1.493 +   let 
   1.494 +    val ct = concl th 
   1.495 +   in 
   1.496 +    if is_conj ct  then
   1.497 +     let 
   1.498 +      val (th1,th2) = conj_pair th in
   1.499 +      overall dun (th1::th2::oths) end
   1.500 +    else if is_disj ct then
   1.501 +      let 
   1.502 +       val th1 = overall dun (assume (capply @{cterm Trueprop} (dest_arg1 ct))::oths)
   1.503 +       val th2 = overall dun (assume (capply @{cterm Trueprop} (dest_arg ct))::oths)
   1.504 +      in disj_cases th th1 th2 end
   1.505 +   else overall (th::dun) oths
   1.506 +  end
   1.507 +  fun dest_binary b ct = if is_binop b ct then dest_binop ct 
   1.508 +                         else raise CTERM ("dest_binary",[b,ct])
   1.509 +  val dest_eq = dest_binary @{cterm "op = :: real => _"}
   1.510 +  val neq_th = nth pth 5
   1.511 +  fun real_not_eq_conv ct = 
   1.512 +   let 
   1.513 +    val (l,r) = dest_eq (dest_arg ct)
   1.514 +    val th = instantiate ([],[(@{cpat "?x::real"},l),(@{cpat "?y::real"},r)]) neq_th
   1.515 +    val th_p = poly_conv(dest_arg(dest_arg1(rhs_of th)))
   1.516 +    val th_x = Drule.arg_cong_rule @{cterm "uminus :: real => _"} th_p
   1.517 +    val th_n = fconv_rule (arg_conv poly_neg_conv) th_x
   1.518 +    val th' = Drule.binop_cong_rule @{cterm "op |"} 
   1.519 +     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_p)
   1.520 +     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_n)
   1.521 +    in transitive th th' 
   1.522 +  end
   1.523 + fun equal_implies_1_rule PQ = 
   1.524 +  let 
   1.525 +   val P = lhs_of PQ
   1.526 +  in implies_intr P (equal_elim PQ (assume P))
   1.527 +  end
   1.528 + (* FIXME!!! Copied from groebner.ml *)
   1.529 + val strip_exists =
   1.530 +  let fun h (acc, t) =
   1.531 +   case (term_of t) of
   1.532 +    Const("Ex",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
   1.533 +  | _ => (acc,t)
   1.534 +  in fn t => h ([],t)
   1.535 +  end
   1.536 +  fun name_of x = case term_of x of
   1.537 +   Free(s,_) => s
   1.538 + | Var ((s,_),_) => s
   1.539 + | _ => "x"
   1.540 +
   1.541 +  fun mk_forall x th = Drule.arg_cong_rule (instantiate_cterm' [SOME (ctyp_of_term x)] [] @{cpat "All :: (?'a => bool) => _" }) (abstract_rule (name_of x) x th)
   1.542 +
   1.543 +  val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec));
   1.544 +
   1.545 + fun ext T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat Ex}
   1.546 + fun mk_ex v t = Thm.capply (ext (ctyp_of_term v)) (Thm.cabs v t)
   1.547 +
   1.548 + fun choose v th th' = case concl_of th of 
   1.549 +   @{term Trueprop} $ (Const("Ex",_)$_) => 
   1.550 +    let
   1.551 +     val p = (funpow 2 Thm.dest_arg o cprop_of) th
   1.552 +     val T = (hd o Thm.dest_ctyp o ctyp_of_term) p
   1.553 +     val th0 = fconv_rule (Thm.beta_conversion true)
   1.554 +         (instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o cprop_of) th'] exE)
   1.555 +     val pv = (Thm.rhs_of o Thm.beta_conversion true) 
   1.556 +           (Thm.capply @{cterm Trueprop} (Thm.capply p v))
   1.557 +     val th1 = forall_intr v (implies_intr pv th')
   1.558 +    in implies_elim (implies_elim th0 th) th1  end
   1.559 + | _ => raise THM ("choose",0,[th, th'])
   1.560 +
   1.561 +  fun simple_choose v th = 
   1.562 +     choose v (assume ((Thm.capply @{cterm Trueprop} o mk_ex v) ((Thm.dest_arg o hd o #hyps o Thm.crep_thm) th))) th
   1.563 +
   1.564 + val strip_forall =
   1.565 +  let fun h (acc, t) =
   1.566 +   case (term_of t) of
   1.567 +    Const("All",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
   1.568 +  | _ => (acc,t)
   1.569 +  in fn t => h ([],t)
   1.570 +  end
   1.571 +
   1.572 + fun f ct =
   1.573 +  let 
   1.574 +   val nnf_norm_conv' = 
   1.575 +     nnf_conv then_conv 
   1.576 +     literals_conv [@{term "op &"}, @{term "op |"}] [] 
   1.577 +     (cache_conv 
   1.578 +       (first_conv [real_lt_conv, real_le_conv, 
   1.579 +                    real_eq_conv, real_not_lt_conv, 
   1.580 +                    real_not_le_conv, real_not_eq_conv, all_conv]))
   1.581 +  fun absremover ct = (literals_conv [@{term "op &"}, @{term "op |"}] [] 
   1.582 +                  (try_conv (absconv1 then_conv binop_conv (arg_conv poly_conv))) then_conv 
   1.583 +        try_conv (absconv2 then_conv nnf_norm_conv' then_conv binop_conv absremover)) ct
   1.584 +  val nct = capply @{cterm Trueprop} (capply @{cterm "Not"} ct)
   1.585 +  val th0 = (init_conv then_conv arg_conv nnf_norm_conv') nct
   1.586 +  val tm0 = dest_arg (rhs_of th0)
   1.587 +  val th = if tm0 aconvc @{cterm False} then equal_implies_1_rule th0 else
   1.588 +   let 
   1.589 +    val (evs,bod) = strip_exists tm0
   1.590 +    val (avs,ibod) = strip_forall bod
   1.591 +    val th1 = Drule.arg_cong_rule @{cterm Trueprop} (fold mk_forall avs (absremover ibod))
   1.592 +    val th2 = overall [] [specl avs (assume (rhs_of th1))]
   1.593 +    val th3 = fold simple_choose evs (prove_hyp (equal_elim th1 (assume (capply @{cterm Trueprop} bod))) th2)
   1.594 +   in  Drule.implies_intr_hyps (prove_hyp (equal_elim th0 (assume nct)) th3)
   1.595 +   end
   1.596 +  in implies_elim (instantiate' [] [SOME ct] pth_final) th
   1.597 + end
   1.598 +in f
   1.599 +end;
   1.600 +
   1.601 +(* A linear arithmetic prover *)
   1.602 +local
   1.603 +  val linear_add = Ctermfunc.combine (curry op +/) (fn z => z =/ Rat.zero)
   1.604 +  fun linear_cmul c = Ctermfunc.mapf (fn x => c */ x)
   1.605 +  val one_tm = @{cterm "1::real"}
   1.606 +  fun contradictory p (e,_) = ((Ctermfunc.is_undefined e) andalso not(p Rat.zero)) orelse
   1.607 +     ((gen_eq_set (op aconvc) (Ctermfunc.dom e, [one_tm])) andalso not(p(Ctermfunc.apply e one_tm)))
   1.608 +
   1.609 +  fun linear_ineqs vars (les,lts) = 
   1.610 +   case find_first (contradictory (fn x => x >/ Rat.zero)) lts of
   1.611 +    SOME r => r
   1.612 +  | NONE => 
   1.613 +   (case find_first (contradictory (fn x => x >/ Rat.zero)) les of
   1.614 +     SOME r => r
   1.615 +   | NONE => 
   1.616 +     if null vars then error "linear_ineqs: no contradiction" else
   1.617 +     let 
   1.618 +      val ineqs = les @ lts
   1.619 +      fun blowup v =
   1.620 +       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) ineqs) +
   1.621 +       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) ineqs) *
   1.622 +       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero </ Rat.zero) ineqs)
   1.623 +      val  v = fst(hd(sort (fn ((_,i),(_,j)) => int_ord (i,j))
   1.624 +                 (map (fn v => (v,blowup v)) vars)))
   1.625 +      fun addup (e1,p1) (e2,p2) acc =
   1.626 +       let 
   1.627 +        val c1 = Ctermfunc.tryapplyd e1 v Rat.zero 
   1.628 +        val c2 = Ctermfunc.tryapplyd e2 v Rat.zero
   1.629 +       in if c1 */ c2 >=/ Rat.zero then acc else
   1.630 +        let 
   1.631 +         val e1' = linear_cmul (Rat.abs c2) e1
   1.632 +         val e2' = linear_cmul (Rat.abs c1) e2
   1.633 +         val p1' = Product(Rational_lt(Rat.abs c2),p1)
   1.634 +         val p2' = Product(Rational_lt(Rat.abs c1),p2)
   1.635 +        in (linear_add e1' e2',Sum(p1',p2'))::acc
   1.636 +        end
   1.637 +       end
   1.638 +      val (les0,les1) = 
   1.639 +         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) les
   1.640 +      val (lts0,lts1) = 
   1.641 +         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) lts
   1.642 +      val (lesp,lesn) = 
   1.643 +         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) les1
   1.644 +      val (ltsp,ltsn) = 
   1.645 +         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) lts1
   1.646 +      val les' = fold_rev (fn ep1 => fold_rev (addup ep1) lesp) lesn les0
   1.647 +      val lts' = fold_rev (fn ep1 => fold_rev (addup ep1) (lesp@ltsp)) ltsn
   1.648 +                      (fold_rev (fn ep1 => fold_rev (addup ep1) (lesn@ltsn)) ltsp lts0)
   1.649 +     in linear_ineqs (remove (op aconvc) v vars) (les',lts')
   1.650 +     end)
   1.651 +
   1.652 +  fun linear_eqs(eqs,les,lts) = 
   1.653 +   case find_first (contradictory (fn x => x =/ Rat.zero)) eqs of
   1.654 +    SOME r => r
   1.655 +  | NONE => (case eqs of 
   1.656 +    [] => 
   1.657 +     let val vars = remove (op aconvc) one_tm 
   1.658 +           (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom o fst) (les@lts) []) 
   1.659 +     in linear_ineqs vars (les,lts) end
   1.660 +   | (e,p)::es => 
   1.661 +     if Ctermfunc.is_undefined e then linear_eqs (es,les,lts) else
   1.662 +     let 
   1.663 +      val (x,c) = Ctermfunc.choose (Ctermfunc.undefine one_tm e)
   1.664 +      fun xform (inp as (t,q)) =
   1.665 +       let val d = Ctermfunc.tryapplyd t x Rat.zero in
   1.666 +        if d =/ Rat.zero then inp else
   1.667 +        let 
   1.668 +         val k = (Rat.neg d) */ Rat.abs c // c
   1.669 +         val e' = linear_cmul k e
   1.670 +         val t' = linear_cmul (Rat.abs c) t
   1.671 +         val p' = Eqmul(cterm_of_rat k,p)
   1.672 +         val q' = Product(Rational_lt(Rat.abs c),q) 
   1.673 +        in (linear_add e' t',Sum(p',q')) 
   1.674 +        end 
   1.675 +      end
   1.676 +     in linear_eqs(map xform es,map xform les,map xform lts)
   1.677 +     end)
   1.678 +
   1.679 +  fun linear_prover (eq,le,lt) = 
   1.680 +   let 
   1.681 +    val eqs = map2 (fn p => fn n => (p,Axiom_eq n)) eq (0 upto (length eq - 1))
   1.682 +    val les = map2 (fn p => fn n => (p,Axiom_le n)) le (0 upto (length le - 1))
   1.683 +    val lts = map2 (fn p => fn n => (p,Axiom_lt n)) lt (0 upto (length lt - 1))
   1.684 +   in linear_eqs(eqs,les,lts)
   1.685 +   end 
   1.686 +  
   1.687 +  fun lin_of_hol ct = 
   1.688 +   if ct aconvc @{cterm "0::real"} then Ctermfunc.undefined
   1.689 +   else if not (is_comb ct) then Ctermfunc.onefunc (ct, Rat.one)
   1.690 +   else if is_ratconst ct then Ctermfunc.onefunc (one_tm, dest_ratconst ct)
   1.691 +   else
   1.692 +    let val (lop,r) = Thm.dest_comb ct 
   1.693 +    in if not (is_comb lop) then Ctermfunc.onefunc (ct, Rat.one)
   1.694 +       else
   1.695 +        let val (opr,l) = Thm.dest_comb lop 
   1.696 +        in if opr aconvc @{cterm "op + :: real =>_"} 
   1.697 +           then linear_add (lin_of_hol l) (lin_of_hol r)
   1.698 +           else if opr aconvc @{cterm "op * :: real =>_"} 
   1.699 +                   andalso is_ratconst l then Ctermfunc.onefunc (r, dest_ratconst l)
   1.700 +           else Ctermfunc.onefunc (ct, Rat.one)
   1.701 +        end
   1.702 +    end
   1.703 +
   1.704 +  fun is_alien ct = case term_of ct of 
   1.705 +   Const(@{const_name "real"}, _)$ n => 
   1.706 +     if can HOLogic.dest_number n then false else true
   1.707 +  | _ => false
   1.708 + open Thm
   1.709 +in 
   1.710 +fun real_linear_prover translator (eq,le,lt) = 
   1.711 + let 
   1.712 +  val lhs = lin_of_hol o dest_arg1 o dest_arg o cprop_of
   1.713 +  val rhs = lin_of_hol o dest_arg o dest_arg o cprop_of
   1.714 +  val eq_pols = map lhs eq
   1.715 +  val le_pols = map rhs le
   1.716 +  val lt_pols = map rhs lt 
   1.717 +  val aliens =  filter is_alien
   1.718 +      (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom) 
   1.719 +          (eq_pols @ le_pols @ lt_pols) [])
   1.720 +  val le_pols' = le_pols @ map (fn v => Ctermfunc.onefunc (v,Rat.one)) aliens
   1.721 +  val (_,proof) = linear_prover (eq_pols,le_pols',lt_pols)
   1.722 +  val le' = le @ map (fn a => instantiate' [] [SOME (dest_arg a)] @{thm real_of_nat_ge_zero}) aliens 
   1.723 + in (translator (eq,le',lt) proof) : thm
   1.724 + end
   1.725 +end;
   1.726 +
   1.727 +(* A less general generic arithmetic prover dealing with abs,max and min*)
   1.728 +
   1.729 +local
   1.730 + val absmaxmin_elim_ss1 = HOL_basic_ss addsimps real_abs_thms1
   1.731 + fun absmaxmin_elim_conv1 ctxt = 
   1.732 +    Simplifier.rewrite (Simplifier.context ctxt absmaxmin_elim_ss1)
   1.733 +
   1.734 + val absmaxmin_elim_conv2 =
   1.735 +  let 
   1.736 +   val pth_abs = instantiate' [SOME @{ctyp real}] [] abs_split'
   1.737 +   val pth_max = instantiate' [SOME @{ctyp real}] [] max_split
   1.738 +   val pth_min = instantiate' [SOME @{ctyp real}] [] min_split
   1.739 +   val abs_tm = @{cterm "abs :: real => _"}
   1.740 +   val p_tm = @{cpat "?P :: real => bool"}
   1.741 +   val x_tm = @{cpat "?x :: real"}
   1.742 +   val y_tm = @{cpat "?y::real"}
   1.743 +   val is_max = is_binop @{cterm "max :: real => _"}
   1.744 +   val is_min = is_binop @{cterm "min :: real => _"} 
   1.745 +   fun is_abs t = is_comb t andalso dest_fun t aconvc abs_tm
   1.746 +   fun eliminate_construct p c tm =
   1.747 +    let 
   1.748 +     val t = find_cterm p tm
   1.749 +     val th0 = (symmetric o beta_conversion false) (capply (cabs t tm) t)
   1.750 +     val (p,ax) = (dest_comb o rhs_of) th0
   1.751 +    in fconv_rule(arg_conv(binop_conv (arg_conv (beta_conversion false))))
   1.752 +               (transitive th0 (c p ax))
   1.753 +   end
   1.754 +
   1.755 +   val elim_abs = eliminate_construct is_abs
   1.756 +    (fn p => fn ax => 
   1.757 +       instantiate ([], [(p_tm,p), (x_tm, dest_arg ax)]) pth_abs)
   1.758 +   val elim_max = eliminate_construct is_max
   1.759 +    (fn p => fn ax => 
   1.760 +      let val (ax,y) = dest_comb ax 
   1.761 +      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
   1.762 +      pth_max end)
   1.763 +   val elim_min = eliminate_construct is_min
   1.764 +    (fn p => fn ax => 
   1.765 +      let val (ax,y) = dest_comb ax 
   1.766 +      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
   1.767 +      pth_min end)
   1.768 +   in first_conv [elim_abs, elim_max, elim_min, all_conv]
   1.769 +  end;
   1.770 +in fun gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,prover) =
   1.771 +        gen_gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,
   1.772 +                       absmaxmin_elim_conv1 ctxt,absmaxmin_elim_conv2,prover)
   1.773 +end;
   1.774 +
   1.775 +(* An instance for reals*) 
   1.776 +
   1.777 +fun gen_prover_real_arith ctxt prover = 
   1.778 + let
   1.779 +  fun simple_cterm_ord t u = TermOrd.term_ord (term_of t, term_of u) = LESS
   1.780 +  val {add,mul,neg,pow,sub,main} = 
   1.781 +     Normalizer.semiring_normalizers_ord_wrapper ctxt
   1.782 +      (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) 
   1.783 +     simple_cterm_ord
   1.784 +in gen_real_arith ctxt
   1.785 +   (cterm_of_rat, field_comp_conv, field_comp_conv,field_comp_conv,
   1.786 +    main,neg,add,mul, prover)
   1.787 +end;
   1.788 +
   1.789 +fun real_arith ctxt = gen_prover_real_arith ctxt real_linear_prover;
   1.790 +end