src/HOL/Library/positivstellensatz.ML
author wenzelm
Thu Jul 09 22:01:41 2009 +0200 (2009-07-09)
changeset 31971 8c1b845ed105
parent 31120 fc654c95c29e
child 32402 5731300da417
permissions -rw-r--r--
renamed functor TableFun to Table, and GraphFun to Graph;
chaieb@31120
     1
(* Title:      Library/positivstellensatz
chaieb@31120
     2
   Author:     Amine Chaieb, University of Cambridge
chaieb@31120
     3
   Description: A generic arithmetic prover based on Positivstellensatz certificates --- 
chaieb@31120
     4
    also implements Fourrier-Motzkin elimination as a special case Fourrier-Motzkin elimination.
chaieb@31120
     5
*)
chaieb@31120
     6
chaieb@31120
     7
(* A functor for finite mappings based on Tables *)
chaieb@31120
     8
signature FUNC = 
chaieb@31120
     9
sig
chaieb@31120
    10
 type 'a T
chaieb@31120
    11
 type key
chaieb@31120
    12
 val apply : 'a T -> key -> 'a
chaieb@31120
    13
 val applyd :'a T -> (key -> 'a) -> key -> 'a
chaieb@31120
    14
 val combine : ('a -> 'a -> 'a) -> ('a -> bool) -> 'a T -> 'a T -> 'a T
chaieb@31120
    15
 val defined : 'a T -> key -> bool
chaieb@31120
    16
 val dom : 'a T -> key list
chaieb@31120
    17
 val fold : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b
chaieb@31120
    18
 val fold_rev : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b
chaieb@31120
    19
 val graph : 'a T -> (key * 'a) list
chaieb@31120
    20
 val is_undefined : 'a T -> bool
chaieb@31120
    21
 val mapf : ('a -> 'b) -> 'a T -> 'b T
chaieb@31120
    22
 val tryapplyd : 'a T -> key -> 'a -> 'a
chaieb@31120
    23
 val undefine :  key -> 'a T -> 'a T
chaieb@31120
    24
 val undefined : 'a T
chaieb@31120
    25
 val update : key * 'a -> 'a T -> 'a T
chaieb@31120
    26
 val updatep : (key * 'a -> bool) -> key * 'a -> 'a T -> 'a T
chaieb@31120
    27
 val choose : 'a T -> key * 'a
chaieb@31120
    28
 val onefunc : key * 'a -> 'a T
chaieb@31120
    29
 val get_first: (key*'a -> 'a option) -> 'a T -> 'a option
chaieb@31120
    30
end;
chaieb@31120
    31
chaieb@31120
    32
functor FuncFun(Key: KEY) : FUNC=
chaieb@31120
    33
struct
chaieb@31120
    34
chaieb@31120
    35
type key = Key.key;
wenzelm@31971
    36
structure Tab = Table(Key);
chaieb@31120
    37
type 'a T = 'a Tab.table;
chaieb@31120
    38
chaieb@31120
    39
val undefined = Tab.empty;
chaieb@31120
    40
val is_undefined = Tab.is_empty;
chaieb@31120
    41
val mapf = Tab.map;
chaieb@31120
    42
val fold = Tab.fold;
chaieb@31120
    43
val fold_rev = Tab.fold_rev;
chaieb@31120
    44
val graph = Tab.dest;
chaieb@31120
    45
fun dom a = sort Key.ord (Tab.keys a);
chaieb@31120
    46
fun applyd f d x = case Tab.lookup f x of 
chaieb@31120
    47
   SOME y => y
chaieb@31120
    48
 | NONE => d x;
chaieb@31120
    49
chaieb@31120
    50
fun apply f x = applyd f (fn _ => raise Tab.UNDEF x) x;
chaieb@31120
    51
fun tryapplyd f a d = applyd f (K d) a;
chaieb@31120
    52
val defined = Tab.defined;
chaieb@31120
    53
fun undefine x t = (Tab.delete x t handle UNDEF => t);
chaieb@31120
    54
val update = Tab.update;
chaieb@31120
    55
fun updatep p (k,v) t = if p (k, v) then t else update (k,v) t
chaieb@31120
    56
fun combine f z a b = 
chaieb@31120
    57
 let
chaieb@31120
    58
  fun h (k,v) t = case Tab.lookup t k of
chaieb@31120
    59
     NONE => Tab.update (k,v) t
chaieb@31120
    60
   | SOME v' => let val w = f v v'
chaieb@31120
    61
     in if z w then Tab.delete k t else Tab.update (k,w) t end;
chaieb@31120
    62
  in Tab.fold h a b end;
chaieb@31120
    63
chaieb@31120
    64
fun choose f = case Tab.min_key f of 
chaieb@31120
    65
   SOME k => (k,valOf (Tab.lookup f k))
chaieb@31120
    66
 | NONE => error "FuncFun.choose : Completely undefined function"
chaieb@31120
    67
chaieb@31120
    68
fun onefunc kv = update kv undefined
chaieb@31120
    69
chaieb@31120
    70
local
chaieb@31120
    71
fun  find f (k,v) NONE = f (k,v)
chaieb@31120
    72
   | find f (k,v) r = r
chaieb@31120
    73
in
chaieb@31120
    74
fun get_first f t = fold (find f) t NONE
chaieb@31120
    75
end
chaieb@31120
    76
end;
chaieb@31120
    77
chaieb@31120
    78
structure Intfunc = FuncFun(type key = int val ord = int_ord);
chaieb@31120
    79
structure Symfunc = FuncFun(type key = string val ord = fast_string_ord);
chaieb@31120
    80
structure Termfunc = FuncFun(type key = term val ord = TermOrd.fast_term_ord);
chaieb@31120
    81
structure Ctermfunc = FuncFun(type key = cterm val ord = (fn (s,t) => TermOrd.fast_term_ord(term_of s, term_of t)));
chaieb@31120
    82
chaieb@31120
    83
structure Ratfunc = FuncFun(type key = Rat.rat val ord = Rat.ord);
chaieb@31120
    84
    (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*)
chaieb@31120
    85
structure Conv2 = 
chaieb@31120
    86
struct
chaieb@31120
    87
 open Conv
chaieb@31120
    88
fun instantiate_cterm' ty tms = Drule.cterm_rule (Drule.instantiate' ty tms)
chaieb@31120
    89
fun is_comb t = case (term_of t) of _$_ => true | _ => false;
chaieb@31120
    90
fun is_abs t = case (term_of t) of Abs _ => true | _ => false;
chaieb@31120
    91
chaieb@31120
    92
fun end_itlist f l =
chaieb@31120
    93
 case l of 
chaieb@31120
    94
   []     => error "end_itlist"
chaieb@31120
    95
 | [x]    => x
chaieb@31120
    96
 | (h::t) => f h (end_itlist f t);
chaieb@31120
    97
chaieb@31120
    98
 fun absc cv ct = case term_of ct of 
chaieb@31120
    99
 Abs (v,_, _) => 
chaieb@31120
   100
  let val (x,t) = Thm.dest_abs (SOME v) ct
chaieb@31120
   101
  in Thm.abstract_rule ((fst o dest_Free o term_of) x) x (cv t)
chaieb@31120
   102
  end
chaieb@31120
   103
 | _ => all_conv ct;
chaieb@31120
   104
chaieb@31120
   105
fun cache_conv conv =
chaieb@31120
   106
 let 
chaieb@31120
   107
  val tab = ref Termtab.empty
chaieb@31120
   108
  fun cconv t =  
chaieb@31120
   109
    case Termtab.lookup (!tab) (term_of t) of
chaieb@31120
   110
     SOME th => th
chaieb@31120
   111
   | NONE => let val th = conv t
chaieb@31120
   112
             in ((tab := Termtab.insert Thm.eq_thm (term_of t, th) (!tab)); th) end
chaieb@31120
   113
 in cconv end;
chaieb@31120
   114
fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct'))
chaieb@31120
   115
  handle CTERM _ => false;
chaieb@31120
   116
chaieb@31120
   117
local
chaieb@31120
   118
 fun thenqc conv1 conv2 tm =
chaieb@31120
   119
   case try conv1 tm of
chaieb@31120
   120
    SOME th1 => (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
chaieb@31120
   121
  | NONE => conv2 tm
chaieb@31120
   122
chaieb@31120
   123
 fun thencqc conv1 conv2 tm =
chaieb@31120
   124
    let val th1 = conv1 tm 
chaieb@31120
   125
    in (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
chaieb@31120
   126
    end
chaieb@31120
   127
 fun comb_qconv conv tm =
chaieb@31120
   128
   let val (l,r) = Thm.dest_comb tm 
chaieb@31120
   129
   in (case try conv l of 
chaieb@31120
   130
        SOME th1 => (case try conv r of SOME th2 => Thm.combination th1 th2 
chaieb@31120
   131
                                      | NONE => Drule.fun_cong_rule th1 r)
chaieb@31120
   132
      | NONE => Drule.arg_cong_rule l (conv r))
chaieb@31120
   133
   end
chaieb@31120
   134
 fun repeatqc conv tm = thencqc conv (repeatqc conv) tm 
chaieb@31120
   135
 fun sub_qconv conv tm =  if is_abs tm then absc conv tm else comb_qconv conv tm 
chaieb@31120
   136
 fun once_depth_qconv conv tm =
chaieb@31120
   137
      (conv else_conv (sub_qconv (once_depth_qconv conv))) tm
chaieb@31120
   138
 fun depth_qconv conv tm =
chaieb@31120
   139
    thenqc (sub_qconv (depth_qconv conv))
chaieb@31120
   140
           (repeatqc conv) tm
chaieb@31120
   141
 fun redepth_qconv conv tm =
chaieb@31120
   142
    thenqc (sub_qconv (redepth_qconv conv))
chaieb@31120
   143
           (thencqc conv (redepth_qconv conv)) tm
chaieb@31120
   144
 fun top_depth_qconv conv tm =
chaieb@31120
   145
    thenqc (repeatqc conv)
chaieb@31120
   146
           (thencqc (sub_qconv (top_depth_qconv conv))
chaieb@31120
   147
                    (thencqc conv (top_depth_qconv conv))) tm
chaieb@31120
   148
 fun top_sweep_qconv conv tm =
chaieb@31120
   149
    thenqc (repeatqc conv)
chaieb@31120
   150
           (sub_qconv (top_sweep_qconv conv)) tm
chaieb@31120
   151
in 
chaieb@31120
   152
val (once_depth_conv, depth_conv, rdepth_conv, top_depth_conv, top_sweep_conv) = 
chaieb@31120
   153
  (fn c => try_conv (once_depth_qconv c),
chaieb@31120
   154
   fn c => try_conv (depth_qconv c),
chaieb@31120
   155
   fn c => try_conv (redepth_qconv c),
chaieb@31120
   156
   fn c => try_conv (top_depth_qconv c),
chaieb@31120
   157
   fn c => try_conv (top_sweep_qconv c));
chaieb@31120
   158
end;
chaieb@31120
   159
end;
chaieb@31120
   160
chaieb@31120
   161
chaieb@31120
   162
    (* Some useful derived rules *)
chaieb@31120
   163
fun deduct_antisym_rule tha thb = 
chaieb@31120
   164
    equal_intr (implies_intr (cprop_of thb) tha) 
chaieb@31120
   165
     (implies_intr (cprop_of tha) thb);
chaieb@31120
   166
chaieb@31120
   167
fun prove_hyp tha thb = 
chaieb@31120
   168
  if exists (curry op aconv (concl_of tha)) (#hyps (rep_thm thb)) 
chaieb@31120
   169
  then equal_elim (symmetric (deduct_antisym_rule tha thb)) tha else thb;
chaieb@31120
   170
chaieb@31120
   171
chaieb@31120
   172
chaieb@31120
   173
signature REAL_ARITH = 
chaieb@31120
   174
sig
chaieb@31120
   175
  datatype positivstellensatz =
chaieb@31120
   176
   Axiom_eq of int
chaieb@31120
   177
 | Axiom_le of int
chaieb@31120
   178
 | Axiom_lt of int
chaieb@31120
   179
 | Rational_eq of Rat.rat
chaieb@31120
   180
 | Rational_le of Rat.rat
chaieb@31120
   181
 | Rational_lt of Rat.rat
chaieb@31120
   182
 | Square of cterm
chaieb@31120
   183
 | Eqmul of cterm * positivstellensatz
chaieb@31120
   184
 | Sum of positivstellensatz * positivstellensatz
chaieb@31120
   185
 | Product of positivstellensatz * positivstellensatz;
chaieb@31120
   186
chaieb@31120
   187
val gen_gen_real_arith :
chaieb@31120
   188
  Proof.context -> (Rat.rat -> Thm.cterm) * conv * conv * conv * 
chaieb@31120
   189
   conv * conv * conv * conv * conv * conv * 
chaieb@31120
   190
    ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@31120
   191
        thm list * thm list * thm list -> thm) -> conv
chaieb@31120
   192
val real_linear_prover : 
chaieb@31120
   193
  (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@31120
   194
   thm list * thm list * thm list -> thm
chaieb@31120
   195
chaieb@31120
   196
val gen_real_arith : Proof.context ->
chaieb@31120
   197
   (Rat.rat -> cterm) * conv * conv * conv * conv * conv * conv * conv *
chaieb@31120
   198
   ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@31120
   199
       thm list * thm list * thm list -> thm) -> conv
chaieb@31120
   200
val gen_prover_real_arith : Proof.context ->
chaieb@31120
   201
   ((thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@31120
   202
     thm list * thm list * thm list -> thm) -> conv
chaieb@31120
   203
val real_arith : Proof.context -> conv
chaieb@31120
   204
end
chaieb@31120
   205
chaieb@31120
   206
structure RealArith (* : REAL_ARITH *)=
chaieb@31120
   207
struct
chaieb@31120
   208
chaieb@31120
   209
 open Conv Thm;;
chaieb@31120
   210
(* ------------------------------------------------------------------------- *)
chaieb@31120
   211
(* Data structure for Positivstellensatz refutations.                        *)
chaieb@31120
   212
(* ------------------------------------------------------------------------- *)
chaieb@31120
   213
chaieb@31120
   214
datatype positivstellensatz =
chaieb@31120
   215
   Axiom_eq of int
chaieb@31120
   216
 | Axiom_le of int
chaieb@31120
   217
 | Axiom_lt of int
chaieb@31120
   218
 | Rational_eq of Rat.rat
chaieb@31120
   219
 | Rational_le of Rat.rat
chaieb@31120
   220
 | Rational_lt of Rat.rat
chaieb@31120
   221
 | Square of cterm
chaieb@31120
   222
 | Eqmul of cterm * positivstellensatz
chaieb@31120
   223
 | Sum of positivstellensatz * positivstellensatz
chaieb@31120
   224
 | Product of positivstellensatz * positivstellensatz;
chaieb@31120
   225
         (* Theorems used in the procedure *)
chaieb@31120
   226
chaieb@31120
   227
chaieb@31120
   228
val my_eqs = ref ([] : thm list);
chaieb@31120
   229
val my_les = ref ([] : thm list);
chaieb@31120
   230
val my_lts = ref ([] : thm list);
chaieb@31120
   231
val my_proof = ref (Axiom_eq 0);
chaieb@31120
   232
val my_context = ref @{context};
chaieb@31120
   233
chaieb@31120
   234
val my_mk_numeric = ref ((K @{cterm True}) :Rat.rat -> cterm);
chaieb@31120
   235
val my_numeric_eq_conv = ref no_conv;
chaieb@31120
   236
val my_numeric_ge_conv = ref no_conv;
chaieb@31120
   237
val my_numeric_gt_conv = ref no_conv;
chaieb@31120
   238
val my_poly_conv = ref no_conv;
chaieb@31120
   239
val my_poly_neg_conv = ref no_conv;
chaieb@31120
   240
val my_poly_add_conv = ref no_conv;
chaieb@31120
   241
val my_poly_mul_conv = ref no_conv;
chaieb@31120
   242
chaieb@31120
   243
fun conjunctions th = case try Conjunction.elim th of
chaieb@31120
   244
   SOME (th1,th2) => (conjunctions th1) @ conjunctions th2
chaieb@31120
   245
 | NONE => [th];
chaieb@31120
   246
chaieb@31120
   247
val pth = @{lemma "(((x::real) < y) == (y - x > 0)) &&& ((x <= y) == (y - x >= 0)) 
chaieb@31120
   248
     &&& ((x = y) == (x - y = 0)) &&& ((~(x < y)) == (x - y >= 0)) &&& ((~(x <= y)) == (x - y > 0))
chaieb@31120
   249
     &&& ((~(x = y)) == (x - y > 0 | -(x - y) > 0))"
chaieb@31120
   250
  by (atomize (full), auto simp add: less_diff_eq le_diff_eq not_less)} |> 
chaieb@31120
   251
conjunctions;
chaieb@31120
   252
chaieb@31120
   253
val pth_final = @{lemma "(~p ==> False) ==> p" by blast}
chaieb@31120
   254
val pth_add = 
chaieb@31120
   255
 @{lemma "(x = (0::real) ==> y = 0 ==> x + y = 0 ) &&& ( x = 0 ==> y >= 0 ==> x + y >= 0) 
chaieb@31120
   256
    &&& (x = 0 ==> y > 0 ==> x + y > 0) &&& (x >= 0 ==> y = 0 ==> x + y >= 0) 
chaieb@31120
   257
    &&& (x >= 0 ==> y >= 0 ==> x + y >= 0) &&& (x >= 0 ==> y > 0 ==> x + y > 0) 
chaieb@31120
   258
    &&& (x > 0 ==> y = 0 ==> x + y > 0) &&& (x > 0 ==> y >= 0 ==> x + y > 0) 
chaieb@31120
   259
    &&& (x > 0 ==> y > 0 ==> x + y > 0)"  by simp_all} |> conjunctions ;
chaieb@31120
   260
chaieb@31120
   261
val pth_mul = 
chaieb@31120
   262
  @{lemma "(x = (0::real) ==> y = 0 ==> x * y = 0) &&& (x = 0 ==> y >= 0 ==> x * y = 0) &&& 
chaieb@31120
   263
           (x = 0 ==> y > 0 ==> x * y = 0) &&& (x >= 0 ==> y = 0 ==> x * y = 0) &&& 
chaieb@31120
   264
           (x >= 0 ==> y >= 0 ==> x * y >= 0 ) &&& ( x >= 0 ==> y > 0 ==> x * y >= 0 ) &&&
chaieb@31120
   265
           (x > 0 ==>  y = 0 ==> x * y = 0 ) &&& ( x > 0 ==> y >= 0 ==> x * y >= 0 ) &&&
chaieb@31120
   266
           (x > 0 ==>  y > 0 ==> x * y > 0)"
chaieb@31120
   267
  by (auto intro: mult_mono[where a="0::real" and b="x" and d="y" and c="0", simplified]
chaieb@31120
   268
    mult_strict_mono[where b="x" and d="y" and a="0" and c="0", simplified])} |> conjunctions;
chaieb@31120
   269
chaieb@31120
   270
val pth_emul = @{lemma "y = (0::real) ==> x * y = 0"  by simp};
chaieb@31120
   271
val pth_square = @{lemma "x * x >= (0::real)"  by simp};
chaieb@31120
   272
chaieb@31120
   273
val weak_dnf_simps = List.take (simp_thms, 34) 
chaieb@31120
   274
    @ 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+};
chaieb@31120
   275
chaieb@31120
   276
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+}
chaieb@31120
   277
chaieb@31120
   278
val choice_iff = @{lemma "(ALL x. EX y. P x y) = (EX f. ALL x. P x (f x))" by metis};
chaieb@31120
   279
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)});
chaieb@31120
   280
chaieb@31120
   281
val real_abs_thms1 = conjunctions @{lemma
chaieb@31120
   282
  "((-1 * abs(x::real) >= r) = (-1 * x >= r & 1 * x >= r)) &&&
chaieb@31120
   283
  ((-1 * abs(x) + a >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
chaieb@31120
   284
  ((a + -1 * abs(x) >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
chaieb@31120
   285
  ((a + -1 * abs(x) + b >= r) = (a + -1 * x + b >= r & a + 1 * x + b >= r)) &&&
chaieb@31120
   286
  ((a + b + -1 * abs(x) >= r) = (a + b + -1 * x >= r & a + b + 1 * x >= r)) &&&
chaieb@31120
   287
  ((a + b + -1 * abs(x) + c >= r) = (a + b + -1 * x + c >= r & a + b + 1 * x + c >= r)) &&&
chaieb@31120
   288
  ((-1 * max x y >= r) = (-1 * x >= r & -1 * y >= r)) &&&
chaieb@31120
   289
  ((-1 * max x y + a >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
chaieb@31120
   290
  ((a + -1 * max x y >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
chaieb@31120
   291
  ((a + -1 * max x y + b >= r) = (a + -1 * x + b >= r & a + -1 * y  + b >= r)) &&&
chaieb@31120
   292
  ((a + b + -1 * max x y >= r) = (a + b + -1 * x >= r & a + b + -1 * y >= r)) &&&
chaieb@31120
   293
  ((a + b + -1 * max x y + c >= r) = (a + b + -1 * x + c >= r & a + b + -1 * y  + c >= r)) &&&
chaieb@31120
   294
  ((1 * min x y >= r) = (1 * x >= r & 1 * y >= r)) &&&
chaieb@31120
   295
  ((1 * min x y + a >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
chaieb@31120
   296
  ((a + 1 * min x y >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
chaieb@31120
   297
  ((a + 1 * min x y + b >= r) = (a + 1 * x + b >= r & a + 1 * y  + b >= r) )&&&
chaieb@31120
   298
  ((a + b + 1 * min x y >= r) = (a + b + 1 * x >= r & a + b + 1 * y >= r)) &&&
chaieb@31120
   299
  ((a + b + 1 * min x y + c >= r) = (a + b + 1 * x + c >= r & a + b + 1 * y  + c >= r)) &&&
chaieb@31120
   300
  ((min x y >= r) = (x >= r &  y >= r)) &&&
chaieb@31120
   301
  ((min x y + a >= r) = (a + x >= r & a + y >= r)) &&&
chaieb@31120
   302
  ((a + min x y >= r) = (a + x >= r & a + y >= r)) &&&
chaieb@31120
   303
  ((a + min x y + b >= r) = (a + x + b >= r & a + y  + b >= r)) &&&
chaieb@31120
   304
  ((a + b + min x y >= r) = (a + b + x >= r & a + b + y >= r) )&&&
chaieb@31120
   305
  ((a + b + min x y + c >= r) = (a + b + x + c >= r & a + b + y + c >= r)) &&&
chaieb@31120
   306
  ((-1 * abs(x) > r) = (-1 * x > r & 1 * x > r)) &&&
chaieb@31120
   307
  ((-1 * abs(x) + a > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
chaieb@31120
   308
  ((a + -1 * abs(x) > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
chaieb@31120
   309
  ((a + -1 * abs(x) + b > r) = (a + -1 * x + b > r & a + 1 * x + b > r)) &&&
chaieb@31120
   310
  ((a + b + -1 * abs(x) > r) = (a + b + -1 * x > r & a + b + 1 * x > r)) &&&
chaieb@31120
   311
  ((a + b + -1 * abs(x) + c > r) = (a + b + -1 * x + c > r & a + b + 1 * x + c > r)) &&&
chaieb@31120
   312
  ((-1 * max x y > r) = ((-1 * x > r) & -1 * y > r)) &&&
chaieb@31120
   313
  ((-1 * max x y + a > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
chaieb@31120
   314
  ((a + -1 * max x y > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
chaieb@31120
   315
  ((a + -1 * max x y + b > r) = (a + -1 * x + b > r & a + -1 * y  + b > r)) &&&
chaieb@31120
   316
  ((a + b + -1 * max x y > r) = (a + b + -1 * x > r & a + b + -1 * y > r)) &&&
chaieb@31120
   317
  ((a + b + -1 * max x y + c > r) = (a + b + -1 * x + c > r & a + b + -1 * y  + c > r)) &&&
chaieb@31120
   318
  ((min x y > r) = (x > r &  y > r)) &&&
chaieb@31120
   319
  ((min x y + a > r) = (a + x > r & a + y > r)) &&&
chaieb@31120
   320
  ((a + min x y > r) = (a + x > r & a + y > r)) &&&
chaieb@31120
   321
  ((a + min x y + b > r) = (a + x + b > r & a + y  + b > r)) &&&
chaieb@31120
   322
  ((a + b + min x y > r) = (a + b + x > r & a + b + y > r)) &&&
chaieb@31120
   323
  ((a + b + min x y + c > r) = (a + b + x + c > r & a + b + y + c > r))"
chaieb@31120
   324
  by auto};
chaieb@31120
   325
chaieb@31120
   326
val abs_split' = @{lemma "P (abs (x::'a::ordered_idom)) == (x >= 0 & P x | x < 0 & P (-x))"
chaieb@31120
   327
  by (atomize (full)) (auto split add: abs_split)};
chaieb@31120
   328
chaieb@31120
   329
val max_split = @{lemma "P (max x y) == ((x::'a::linorder) <= y & P y | x > y & P x)"
chaieb@31120
   330
  by (atomize (full)) (cases "x <= y", auto simp add: max_def)};
chaieb@31120
   331
chaieb@31120
   332
val min_split = @{lemma "P (min x y) == ((x::'a::linorder) <= y & P x | x > y & P y)"
chaieb@31120
   333
  by (atomize (full)) (cases "x <= y", auto simp add: min_def)};
chaieb@31120
   334
chaieb@31120
   335
chaieb@31120
   336
         (* Miscalineous *)
chaieb@31120
   337
fun literals_conv bops uops cv = 
chaieb@31120
   338
 let fun h t =
chaieb@31120
   339
  case (term_of t) of 
chaieb@31120
   340
   b$_$_ => if member (op aconv) bops b then binop_conv h t else cv t
chaieb@31120
   341
 | u$_ => if member (op aconv) uops u then arg_conv h t else cv t
chaieb@31120
   342
 | _ => cv t
chaieb@31120
   343
 in h end;
chaieb@31120
   344
chaieb@31120
   345
fun cterm_of_rat x = 
chaieb@31120
   346
let val (a, b) = Rat.quotient_of_rat x
chaieb@31120
   347
in 
chaieb@31120
   348
 if b = 1 then Numeral.mk_cnumber @{ctyp "real"} a
chaieb@31120
   349
  else Thm.capply (Thm.capply @{cterm "op / :: real => _"} 
chaieb@31120
   350
                   (Numeral.mk_cnumber @{ctyp "real"} a))
chaieb@31120
   351
        (Numeral.mk_cnumber @{ctyp "real"} b)
chaieb@31120
   352
end;
chaieb@31120
   353
chaieb@31120
   354
  fun dest_ratconst t = case term_of t of
chaieb@31120
   355
   Const(@{const_name divide}, _)$a$b => Rat.rat_of_quotient(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
chaieb@31120
   356
 | _ => Rat.rat_of_int (HOLogic.dest_number (term_of t) |> snd)
chaieb@31120
   357
 fun is_ratconst t = can dest_ratconst t
chaieb@31120
   358
chaieb@31120
   359
fun find_term p t = if p t then t else 
chaieb@31120
   360
 case t of
chaieb@31120
   361
  a$b => (find_term p a handle TERM _ => find_term p b)
chaieb@31120
   362
 | Abs (_,_,t') => find_term p t'
chaieb@31120
   363
 | _ => raise TERM ("find_term",[t]);
chaieb@31120
   364
chaieb@31120
   365
fun find_cterm p t = if p t then t else 
chaieb@31120
   366
 case term_of t of
chaieb@31120
   367
  a$b => (find_cterm p (Thm.dest_fun t) handle CTERM _ => find_cterm p (Thm.dest_arg t))
chaieb@31120
   368
 | Abs (_,_,t') => find_cterm p (Thm.dest_abs NONE t |> snd)
chaieb@31120
   369
 | _ => raise CTERM ("find_cterm",[t]);
chaieb@31120
   370
chaieb@31120
   371
chaieb@31120
   372
    (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*)
chaieb@31120
   373
fun instantiate_cterm' ty tms = Drule.cterm_rule (Drule.instantiate' ty tms)
chaieb@31120
   374
fun is_comb t = case (term_of t) of _$_ => true | _ => false;
chaieb@31120
   375
chaieb@31120
   376
fun cache_conv conv =
chaieb@31120
   377
 let 
chaieb@31120
   378
  val tab = ref Termtab.empty
chaieb@31120
   379
  fun cconv t =  
chaieb@31120
   380
    case Termtab.lookup (!tab) (term_of t) of
chaieb@31120
   381
     SOME th => th
chaieb@31120
   382
   | NONE => let val th = conv t
chaieb@31120
   383
             in ((tab := Termtab.insert Thm.eq_thm (term_of t, th) (!tab)); th) end
chaieb@31120
   384
 in cconv end;
chaieb@31120
   385
fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct'))
chaieb@31120
   386
  handle CTERM _ => false;
chaieb@31120
   387
chaieb@31120
   388
    (* A general real arithmetic prover *)
chaieb@31120
   389
chaieb@31120
   390
fun gen_gen_real_arith ctxt (mk_numeric,
chaieb@31120
   391
       numeric_eq_conv,numeric_ge_conv,numeric_gt_conv,
chaieb@31120
   392
       poly_conv,poly_neg_conv,poly_add_conv,poly_mul_conv,
chaieb@31120
   393
       absconv1,absconv2,prover) = 
chaieb@31120
   394
let
chaieb@31120
   395
 open Conv Thm;
chaieb@31120
   396
 val _ = my_context := ctxt 
chaieb@31120
   397
 val _ = (my_mk_numeric := mk_numeric ; my_numeric_eq_conv := numeric_eq_conv ; 
chaieb@31120
   398
          my_numeric_ge_conv := numeric_ge_conv; my_numeric_gt_conv := numeric_gt_conv ;
chaieb@31120
   399
          my_poly_conv := poly_conv; my_poly_neg_conv := poly_neg_conv; 
chaieb@31120
   400
          my_poly_add_conv := poly_add_conv; my_poly_mul_conv := poly_mul_conv)
chaieb@31120
   401
 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}]
chaieb@31120
   402
 val prenex_ss = HOL_basic_ss addsimps prenex_simps
chaieb@31120
   403
 val skolemize_ss = HOL_basic_ss addsimps [choice_iff]
chaieb@31120
   404
 val presimp_conv = Simplifier.rewrite (Simplifier.context ctxt pre_ss)
chaieb@31120
   405
 val prenex_conv = Simplifier.rewrite (Simplifier.context ctxt prenex_ss)
chaieb@31120
   406
 val skolemize_conv = Simplifier.rewrite (Simplifier.context ctxt skolemize_ss)
chaieb@31120
   407
 val weak_dnf_ss = HOL_basic_ss addsimps weak_dnf_simps
chaieb@31120
   408
 val weak_dnf_conv = Simplifier.rewrite (Simplifier.context ctxt weak_dnf_ss)
chaieb@31120
   409
 fun eqT_elim th = equal_elim (symmetric th) @{thm TrueI}
chaieb@31120
   410
 fun oprconv cv ct = 
chaieb@31120
   411
  let val g = Thm.dest_fun2 ct
chaieb@31120
   412
  in if g aconvc @{cterm "op <= :: real => _"} 
chaieb@31120
   413
       orelse g aconvc @{cterm "op < :: real => _"} 
chaieb@31120
   414
     then arg_conv cv ct else arg1_conv cv ct
chaieb@31120
   415
  end
chaieb@31120
   416
chaieb@31120
   417
 fun real_ineq_conv th ct =
chaieb@31120
   418
  let
chaieb@31120
   419
   val th' = (instantiate (match (lhs_of th, ct)) th 
chaieb@31120
   420
      handle MATCH => raise CTERM ("real_ineq_conv", [ct]))
chaieb@31120
   421
  in transitive th' (oprconv poly_conv (Thm.rhs_of th'))
chaieb@31120
   422
  end 
chaieb@31120
   423
  val [real_lt_conv, real_le_conv, real_eq_conv,
chaieb@31120
   424
       real_not_lt_conv, real_not_le_conv, _] =
chaieb@31120
   425
       map real_ineq_conv pth
chaieb@31120
   426
  fun match_mp_rule ths ths' = 
chaieb@31120
   427
   let
chaieb@31120
   428
     fun f ths ths' = case ths of [] => raise THM("match_mp_rule",0,ths)
chaieb@31120
   429
      | th::ths => (ths' MRS th handle THM _ => f ths ths')
chaieb@31120
   430
   in f ths ths' end
chaieb@31120
   431
  fun mul_rule th th' = fconv_rule (arg_conv (oprconv poly_mul_conv))
chaieb@31120
   432
         (match_mp_rule pth_mul [th, th'])
chaieb@31120
   433
  fun add_rule th th' = fconv_rule (arg_conv (oprconv poly_add_conv))
chaieb@31120
   434
         (match_mp_rule pth_add [th, th'])
chaieb@31120
   435
  fun emul_rule ct th = fconv_rule (arg_conv (oprconv poly_mul_conv)) 
chaieb@31120
   436
       (instantiate' [] [SOME ct] (th RS pth_emul)) 
chaieb@31120
   437
  fun square_rule t = fconv_rule (arg_conv (oprconv poly_conv))
chaieb@31120
   438
       (instantiate' [] [SOME t] pth_square)
chaieb@31120
   439
chaieb@31120
   440
  fun hol_of_positivstellensatz(eqs,les,lts) proof =
chaieb@31120
   441
   let 
chaieb@31120
   442
    val _ = (my_eqs := eqs ; my_les := les ; my_lts := lts ; my_proof := proof)
chaieb@31120
   443
    fun translate prf = case prf of
chaieb@31120
   444
        Axiom_eq n => nth eqs n
chaieb@31120
   445
      | Axiom_le n => nth les n
chaieb@31120
   446
      | Axiom_lt n => nth lts n
chaieb@31120
   447
      | Rational_eq x => eqT_elim(numeric_eq_conv(capply @{cterm Trueprop} 
chaieb@31120
   448
                          (capply (capply @{cterm "op =::real => _"} (mk_numeric x)) 
chaieb@31120
   449
                               @{cterm "0::real"})))
chaieb@31120
   450
      | Rational_le x => eqT_elim(numeric_ge_conv(capply @{cterm Trueprop} 
chaieb@31120
   451
                          (capply (capply @{cterm "op <=::real => _"} 
chaieb@31120
   452
                                     @{cterm "0::real"}) (mk_numeric x))))
chaieb@31120
   453
      | Rational_lt x => eqT_elim(numeric_gt_conv(capply @{cterm Trueprop} 
chaieb@31120
   454
                      (capply (capply @{cterm "op <::real => _"} @{cterm "0::real"})
chaieb@31120
   455
                        (mk_numeric x))))
chaieb@31120
   456
      | Square t => square_rule t
chaieb@31120
   457
      | Eqmul(t,p) => emul_rule t (translate p)
chaieb@31120
   458
      | Sum(p1,p2) => add_rule (translate p1) (translate p2)
chaieb@31120
   459
      | Product(p1,p2) => mul_rule (translate p1) (translate p2)
chaieb@31120
   460
   in fconv_rule (first_conv [numeric_ge_conv, numeric_gt_conv, numeric_eq_conv, all_conv]) 
chaieb@31120
   461
          (translate proof)
chaieb@31120
   462
   end
chaieb@31120
   463
  
chaieb@31120
   464
  val init_conv = presimp_conv then_conv
chaieb@31120
   465
      nnf_conv then_conv skolemize_conv then_conv prenex_conv then_conv
chaieb@31120
   466
      weak_dnf_conv
chaieb@31120
   467
chaieb@31120
   468
  val concl = dest_arg o cprop_of
chaieb@31120
   469
  fun is_binop opr ct = (dest_fun2 ct aconvc opr handle CTERM _ => false)
chaieb@31120
   470
  val is_req = is_binop @{cterm "op =:: real => _"}
chaieb@31120
   471
  val is_ge = is_binop @{cterm "op <=:: real => _"}
chaieb@31120
   472
  val is_gt = is_binop @{cterm "op <:: real => _"}
chaieb@31120
   473
  val is_conj = is_binop @{cterm "op &"}
chaieb@31120
   474
  val is_disj = is_binop @{cterm "op |"}
chaieb@31120
   475
  fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
chaieb@31120
   476
  fun disj_cases th th1 th2 = 
chaieb@31120
   477
   let val (p,q) = dest_binop (concl th)
chaieb@31120
   478
       val c = concl th1
chaieb@31120
   479
       val _ = if c aconvc (concl th2) then () else error "disj_cases : conclusions not alpha convertible"
chaieb@31120
   480
   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)
chaieb@31120
   481
   end
chaieb@31120
   482
 fun overall dun ths = case ths of
chaieb@31120
   483
  [] =>
chaieb@31120
   484
   let 
chaieb@31120
   485
    val (eq,ne) = List.partition (is_req o concl) dun
chaieb@31120
   486
     val (le,nl) = List.partition (is_ge o concl) ne
chaieb@31120
   487
     val lt = filter (is_gt o concl) nl 
chaieb@31120
   488
    in prover hol_of_positivstellensatz (eq,le,lt) end
chaieb@31120
   489
 | th::oths =>
chaieb@31120
   490
   let 
chaieb@31120
   491
    val ct = concl th 
chaieb@31120
   492
   in 
chaieb@31120
   493
    if is_conj ct  then
chaieb@31120
   494
     let 
chaieb@31120
   495
      val (th1,th2) = conj_pair th in
chaieb@31120
   496
      overall dun (th1::th2::oths) end
chaieb@31120
   497
    else if is_disj ct then
chaieb@31120
   498
      let 
chaieb@31120
   499
       val th1 = overall dun (assume (capply @{cterm Trueprop} (dest_arg1 ct))::oths)
chaieb@31120
   500
       val th2 = overall dun (assume (capply @{cterm Trueprop} (dest_arg ct))::oths)
chaieb@31120
   501
      in disj_cases th th1 th2 end
chaieb@31120
   502
   else overall (th::dun) oths
chaieb@31120
   503
  end
chaieb@31120
   504
  fun dest_binary b ct = if is_binop b ct then dest_binop ct 
chaieb@31120
   505
                         else raise CTERM ("dest_binary",[b,ct])
chaieb@31120
   506
  val dest_eq = dest_binary @{cterm "op = :: real => _"}
chaieb@31120
   507
  val neq_th = nth pth 5
chaieb@31120
   508
  fun real_not_eq_conv ct = 
chaieb@31120
   509
   let 
chaieb@31120
   510
    val (l,r) = dest_eq (dest_arg ct)
chaieb@31120
   511
    val th = instantiate ([],[(@{cpat "?x::real"},l),(@{cpat "?y::real"},r)]) neq_th
chaieb@31120
   512
    val th_p = poly_conv(dest_arg(dest_arg1(rhs_of th)))
chaieb@31120
   513
    val th_x = Drule.arg_cong_rule @{cterm "uminus :: real => _"} th_p
chaieb@31120
   514
    val th_n = fconv_rule (arg_conv poly_neg_conv) th_x
chaieb@31120
   515
    val th' = Drule.binop_cong_rule @{cterm "op |"} 
chaieb@31120
   516
     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_p)
chaieb@31120
   517
     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_n)
chaieb@31120
   518
    in transitive th th' 
chaieb@31120
   519
  end
chaieb@31120
   520
 fun equal_implies_1_rule PQ = 
chaieb@31120
   521
  let 
chaieb@31120
   522
   val P = lhs_of PQ
chaieb@31120
   523
  in implies_intr P (equal_elim PQ (assume P))
chaieb@31120
   524
  end
chaieb@31120
   525
 (* FIXME!!! Copied from groebner.ml *)
chaieb@31120
   526
 val strip_exists =
chaieb@31120
   527
  let fun h (acc, t) =
chaieb@31120
   528
   case (term_of t) of
chaieb@31120
   529
    Const("Ex",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
chaieb@31120
   530
  | _ => (acc,t)
chaieb@31120
   531
  in fn t => h ([],t)
chaieb@31120
   532
  end
chaieb@31120
   533
  fun name_of x = case term_of x of
chaieb@31120
   534
   Free(s,_) => s
chaieb@31120
   535
 | Var ((s,_),_) => s
chaieb@31120
   536
 | _ => "x"
chaieb@31120
   537
chaieb@31120
   538
  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)
chaieb@31120
   539
chaieb@31120
   540
  val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec));
chaieb@31120
   541
chaieb@31120
   542
 fun ext T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat Ex}
chaieb@31120
   543
 fun mk_ex v t = Thm.capply (ext (ctyp_of_term v)) (Thm.cabs v t)
chaieb@31120
   544
chaieb@31120
   545
 fun choose v th th' = case concl_of th of 
chaieb@31120
   546
   @{term Trueprop} $ (Const("Ex",_)$_) => 
chaieb@31120
   547
    let
chaieb@31120
   548
     val p = (funpow 2 Thm.dest_arg o cprop_of) th
chaieb@31120
   549
     val T = (hd o Thm.dest_ctyp o ctyp_of_term) p
chaieb@31120
   550
     val th0 = fconv_rule (Thm.beta_conversion true)
chaieb@31120
   551
         (instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o cprop_of) th'] exE)
chaieb@31120
   552
     val pv = (Thm.rhs_of o Thm.beta_conversion true) 
chaieb@31120
   553
           (Thm.capply @{cterm Trueprop} (Thm.capply p v))
chaieb@31120
   554
     val th1 = forall_intr v (implies_intr pv th')
chaieb@31120
   555
    in implies_elim (implies_elim th0 th) th1  end
chaieb@31120
   556
 | _ => raise THM ("choose",0,[th, th'])
chaieb@31120
   557
chaieb@31120
   558
  fun simple_choose v th = 
chaieb@31120
   559
     choose v (assume ((Thm.capply @{cterm Trueprop} o mk_ex v) ((Thm.dest_arg o hd o #hyps o Thm.crep_thm) th))) th
chaieb@31120
   560
chaieb@31120
   561
 val strip_forall =
chaieb@31120
   562
  let fun h (acc, t) =
chaieb@31120
   563
   case (term_of t) of
chaieb@31120
   564
    Const("All",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
chaieb@31120
   565
  | _ => (acc,t)
chaieb@31120
   566
  in fn t => h ([],t)
chaieb@31120
   567
  end
chaieb@31120
   568
chaieb@31120
   569
 fun f ct =
chaieb@31120
   570
  let 
chaieb@31120
   571
   val nnf_norm_conv' = 
chaieb@31120
   572
     nnf_conv then_conv 
chaieb@31120
   573
     literals_conv [@{term "op &"}, @{term "op |"}] [] 
chaieb@31120
   574
     (cache_conv 
chaieb@31120
   575
       (first_conv [real_lt_conv, real_le_conv, 
chaieb@31120
   576
                    real_eq_conv, real_not_lt_conv, 
chaieb@31120
   577
                    real_not_le_conv, real_not_eq_conv, all_conv]))
chaieb@31120
   578
  fun absremover ct = (literals_conv [@{term "op &"}, @{term "op |"}] [] 
chaieb@31120
   579
                  (try_conv (absconv1 then_conv binop_conv (arg_conv poly_conv))) then_conv 
chaieb@31120
   580
        try_conv (absconv2 then_conv nnf_norm_conv' then_conv binop_conv absremover)) ct
chaieb@31120
   581
  val nct = capply @{cterm Trueprop} (capply @{cterm "Not"} ct)
chaieb@31120
   582
  val th0 = (init_conv then_conv arg_conv nnf_norm_conv') nct
chaieb@31120
   583
  val tm0 = dest_arg (rhs_of th0)
chaieb@31120
   584
  val th = if tm0 aconvc @{cterm False} then equal_implies_1_rule th0 else
chaieb@31120
   585
   let 
chaieb@31120
   586
    val (evs,bod) = strip_exists tm0
chaieb@31120
   587
    val (avs,ibod) = strip_forall bod
chaieb@31120
   588
    val th1 = Drule.arg_cong_rule @{cterm Trueprop} (fold mk_forall avs (absremover ibod))
chaieb@31120
   589
    val th2 = overall [] [specl avs (assume (rhs_of th1))]
chaieb@31120
   590
    val th3 = fold simple_choose evs (prove_hyp (equal_elim th1 (assume (capply @{cterm Trueprop} bod))) th2)
chaieb@31120
   591
   in  Drule.implies_intr_hyps (prove_hyp (equal_elim th0 (assume nct)) th3)
chaieb@31120
   592
   end
chaieb@31120
   593
  in implies_elim (instantiate' [] [SOME ct] pth_final) th
chaieb@31120
   594
 end
chaieb@31120
   595
in f
chaieb@31120
   596
end;
chaieb@31120
   597
chaieb@31120
   598
(* A linear arithmetic prover *)
chaieb@31120
   599
local
chaieb@31120
   600
  val linear_add = Ctermfunc.combine (curry op +/) (fn z => z =/ Rat.zero)
chaieb@31120
   601
  fun linear_cmul c = Ctermfunc.mapf (fn x => c */ x)
chaieb@31120
   602
  val one_tm = @{cterm "1::real"}
chaieb@31120
   603
  fun contradictory p (e,_) = ((Ctermfunc.is_undefined e) andalso not(p Rat.zero)) orelse
chaieb@31120
   604
     ((gen_eq_set (op aconvc) (Ctermfunc.dom e, [one_tm])) andalso not(p(Ctermfunc.apply e one_tm)))
chaieb@31120
   605
chaieb@31120
   606
  fun linear_ineqs vars (les,lts) = 
chaieb@31120
   607
   case find_first (contradictory (fn x => x >/ Rat.zero)) lts of
chaieb@31120
   608
    SOME r => r
chaieb@31120
   609
  | NONE => 
chaieb@31120
   610
   (case find_first (contradictory (fn x => x >/ Rat.zero)) les of
chaieb@31120
   611
     SOME r => r
chaieb@31120
   612
   | NONE => 
chaieb@31120
   613
     if null vars then error "linear_ineqs: no contradiction" else
chaieb@31120
   614
     let 
chaieb@31120
   615
      val ineqs = les @ lts
chaieb@31120
   616
      fun blowup v =
chaieb@31120
   617
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) ineqs) +
chaieb@31120
   618
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) ineqs) *
chaieb@31120
   619
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero </ Rat.zero) ineqs)
chaieb@31120
   620
      val  v = fst(hd(sort (fn ((_,i),(_,j)) => int_ord (i,j))
chaieb@31120
   621
                 (map (fn v => (v,blowup v)) vars)))
chaieb@31120
   622
      fun addup (e1,p1) (e2,p2) acc =
chaieb@31120
   623
       let 
chaieb@31120
   624
        val c1 = Ctermfunc.tryapplyd e1 v Rat.zero 
chaieb@31120
   625
        val c2 = Ctermfunc.tryapplyd e2 v Rat.zero
chaieb@31120
   626
       in if c1 */ c2 >=/ Rat.zero then acc else
chaieb@31120
   627
        let 
chaieb@31120
   628
         val e1' = linear_cmul (Rat.abs c2) e1
chaieb@31120
   629
         val e2' = linear_cmul (Rat.abs c1) e2
chaieb@31120
   630
         val p1' = Product(Rational_lt(Rat.abs c2),p1)
chaieb@31120
   631
         val p2' = Product(Rational_lt(Rat.abs c1),p2)
chaieb@31120
   632
        in (linear_add e1' e2',Sum(p1',p2'))::acc
chaieb@31120
   633
        end
chaieb@31120
   634
       end
chaieb@31120
   635
      val (les0,les1) = 
chaieb@31120
   636
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) les
chaieb@31120
   637
      val (lts0,lts1) = 
chaieb@31120
   638
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) lts
chaieb@31120
   639
      val (lesp,lesn) = 
chaieb@31120
   640
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) les1
chaieb@31120
   641
      val (ltsp,ltsn) = 
chaieb@31120
   642
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) lts1
chaieb@31120
   643
      val les' = fold_rev (fn ep1 => fold_rev (addup ep1) lesp) lesn les0
chaieb@31120
   644
      val lts' = fold_rev (fn ep1 => fold_rev (addup ep1) (lesp@ltsp)) ltsn
chaieb@31120
   645
                      (fold_rev (fn ep1 => fold_rev (addup ep1) (lesn@ltsn)) ltsp lts0)
chaieb@31120
   646
     in linear_ineqs (remove (op aconvc) v vars) (les',lts')
chaieb@31120
   647
     end)
chaieb@31120
   648
chaieb@31120
   649
  fun linear_eqs(eqs,les,lts) = 
chaieb@31120
   650
   case find_first (contradictory (fn x => x =/ Rat.zero)) eqs of
chaieb@31120
   651
    SOME r => r
chaieb@31120
   652
  | NONE => (case eqs of 
chaieb@31120
   653
    [] => 
chaieb@31120
   654
     let val vars = remove (op aconvc) one_tm 
chaieb@31120
   655
           (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom o fst) (les@lts) []) 
chaieb@31120
   656
     in linear_ineqs vars (les,lts) end
chaieb@31120
   657
   | (e,p)::es => 
chaieb@31120
   658
     if Ctermfunc.is_undefined e then linear_eqs (es,les,lts) else
chaieb@31120
   659
     let 
chaieb@31120
   660
      val (x,c) = Ctermfunc.choose (Ctermfunc.undefine one_tm e)
chaieb@31120
   661
      fun xform (inp as (t,q)) =
chaieb@31120
   662
       let val d = Ctermfunc.tryapplyd t x Rat.zero in
chaieb@31120
   663
        if d =/ Rat.zero then inp else
chaieb@31120
   664
        let 
chaieb@31120
   665
         val k = (Rat.neg d) */ Rat.abs c // c
chaieb@31120
   666
         val e' = linear_cmul k e
chaieb@31120
   667
         val t' = linear_cmul (Rat.abs c) t
chaieb@31120
   668
         val p' = Eqmul(cterm_of_rat k,p)
chaieb@31120
   669
         val q' = Product(Rational_lt(Rat.abs c),q) 
chaieb@31120
   670
        in (linear_add e' t',Sum(p',q')) 
chaieb@31120
   671
        end 
chaieb@31120
   672
      end
chaieb@31120
   673
     in linear_eqs(map xform es,map xform les,map xform lts)
chaieb@31120
   674
     end)
chaieb@31120
   675
chaieb@31120
   676
  fun linear_prover (eq,le,lt) = 
chaieb@31120
   677
   let 
chaieb@31120
   678
    val eqs = map2 (fn p => fn n => (p,Axiom_eq n)) eq (0 upto (length eq - 1))
chaieb@31120
   679
    val les = map2 (fn p => fn n => (p,Axiom_le n)) le (0 upto (length le - 1))
chaieb@31120
   680
    val lts = map2 (fn p => fn n => (p,Axiom_lt n)) lt (0 upto (length lt - 1))
chaieb@31120
   681
   in linear_eqs(eqs,les,lts)
chaieb@31120
   682
   end 
chaieb@31120
   683
  
chaieb@31120
   684
  fun lin_of_hol ct = 
chaieb@31120
   685
   if ct aconvc @{cterm "0::real"} then Ctermfunc.undefined
chaieb@31120
   686
   else if not (is_comb ct) then Ctermfunc.onefunc (ct, Rat.one)
chaieb@31120
   687
   else if is_ratconst ct then Ctermfunc.onefunc (one_tm, dest_ratconst ct)
chaieb@31120
   688
   else
chaieb@31120
   689
    let val (lop,r) = Thm.dest_comb ct 
chaieb@31120
   690
    in if not (is_comb lop) then Ctermfunc.onefunc (ct, Rat.one)
chaieb@31120
   691
       else
chaieb@31120
   692
        let val (opr,l) = Thm.dest_comb lop 
chaieb@31120
   693
        in if opr aconvc @{cterm "op + :: real =>_"} 
chaieb@31120
   694
           then linear_add (lin_of_hol l) (lin_of_hol r)
chaieb@31120
   695
           else if opr aconvc @{cterm "op * :: real =>_"} 
chaieb@31120
   696
                   andalso is_ratconst l then Ctermfunc.onefunc (r, dest_ratconst l)
chaieb@31120
   697
           else Ctermfunc.onefunc (ct, Rat.one)
chaieb@31120
   698
        end
chaieb@31120
   699
    end
chaieb@31120
   700
chaieb@31120
   701
  fun is_alien ct = case term_of ct of 
chaieb@31120
   702
   Const(@{const_name "real"}, _)$ n => 
chaieb@31120
   703
     if can HOLogic.dest_number n then false else true
chaieb@31120
   704
  | _ => false
chaieb@31120
   705
 open Thm
chaieb@31120
   706
in 
chaieb@31120
   707
fun real_linear_prover translator (eq,le,lt) = 
chaieb@31120
   708
 let 
chaieb@31120
   709
  val lhs = lin_of_hol o dest_arg1 o dest_arg o cprop_of
chaieb@31120
   710
  val rhs = lin_of_hol o dest_arg o dest_arg o cprop_of
chaieb@31120
   711
  val eq_pols = map lhs eq
chaieb@31120
   712
  val le_pols = map rhs le
chaieb@31120
   713
  val lt_pols = map rhs lt 
chaieb@31120
   714
  val aliens =  filter is_alien
chaieb@31120
   715
      (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom) 
chaieb@31120
   716
          (eq_pols @ le_pols @ lt_pols) [])
chaieb@31120
   717
  val le_pols' = le_pols @ map (fn v => Ctermfunc.onefunc (v,Rat.one)) aliens
chaieb@31120
   718
  val (_,proof) = linear_prover (eq_pols,le_pols',lt_pols)
chaieb@31120
   719
  val le' = le @ map (fn a => instantiate' [] [SOME (dest_arg a)] @{thm real_of_nat_ge_zero}) aliens 
chaieb@31120
   720
 in (translator (eq,le',lt) proof) : thm
chaieb@31120
   721
 end
chaieb@31120
   722
end;
chaieb@31120
   723
chaieb@31120
   724
(* A less general generic arithmetic prover dealing with abs,max and min*)
chaieb@31120
   725
chaieb@31120
   726
local
chaieb@31120
   727
 val absmaxmin_elim_ss1 = HOL_basic_ss addsimps real_abs_thms1
chaieb@31120
   728
 fun absmaxmin_elim_conv1 ctxt = 
chaieb@31120
   729
    Simplifier.rewrite (Simplifier.context ctxt absmaxmin_elim_ss1)
chaieb@31120
   730
chaieb@31120
   731
 val absmaxmin_elim_conv2 =
chaieb@31120
   732
  let 
chaieb@31120
   733
   val pth_abs = instantiate' [SOME @{ctyp real}] [] abs_split'
chaieb@31120
   734
   val pth_max = instantiate' [SOME @{ctyp real}] [] max_split
chaieb@31120
   735
   val pth_min = instantiate' [SOME @{ctyp real}] [] min_split
chaieb@31120
   736
   val abs_tm = @{cterm "abs :: real => _"}
chaieb@31120
   737
   val p_tm = @{cpat "?P :: real => bool"}
chaieb@31120
   738
   val x_tm = @{cpat "?x :: real"}
chaieb@31120
   739
   val y_tm = @{cpat "?y::real"}
chaieb@31120
   740
   val is_max = is_binop @{cterm "max :: real => _"}
chaieb@31120
   741
   val is_min = is_binop @{cterm "min :: real => _"} 
chaieb@31120
   742
   fun is_abs t = is_comb t andalso dest_fun t aconvc abs_tm
chaieb@31120
   743
   fun eliminate_construct p c tm =
chaieb@31120
   744
    let 
chaieb@31120
   745
     val t = find_cterm p tm
chaieb@31120
   746
     val th0 = (symmetric o beta_conversion false) (capply (cabs t tm) t)
chaieb@31120
   747
     val (p,ax) = (dest_comb o rhs_of) th0
chaieb@31120
   748
    in fconv_rule(arg_conv(binop_conv (arg_conv (beta_conversion false))))
chaieb@31120
   749
               (transitive th0 (c p ax))
chaieb@31120
   750
   end
chaieb@31120
   751
chaieb@31120
   752
   val elim_abs = eliminate_construct is_abs
chaieb@31120
   753
    (fn p => fn ax => 
chaieb@31120
   754
       instantiate ([], [(p_tm,p), (x_tm, dest_arg ax)]) pth_abs)
chaieb@31120
   755
   val elim_max = eliminate_construct is_max
chaieb@31120
   756
    (fn p => fn ax => 
chaieb@31120
   757
      let val (ax,y) = dest_comb ax 
chaieb@31120
   758
      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
chaieb@31120
   759
      pth_max end)
chaieb@31120
   760
   val elim_min = eliminate_construct is_min
chaieb@31120
   761
    (fn p => fn ax => 
chaieb@31120
   762
      let val (ax,y) = dest_comb ax 
chaieb@31120
   763
      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
chaieb@31120
   764
      pth_min end)
chaieb@31120
   765
   in first_conv [elim_abs, elim_max, elim_min, all_conv]
chaieb@31120
   766
  end;
chaieb@31120
   767
in fun gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,prover) =
chaieb@31120
   768
        gen_gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,
chaieb@31120
   769
                       absmaxmin_elim_conv1 ctxt,absmaxmin_elim_conv2,prover)
chaieb@31120
   770
end;
chaieb@31120
   771
chaieb@31120
   772
(* An instance for reals*) 
chaieb@31120
   773
chaieb@31120
   774
fun gen_prover_real_arith ctxt prover = 
chaieb@31120
   775
 let
chaieb@31120
   776
  fun simple_cterm_ord t u = TermOrd.term_ord (term_of t, term_of u) = LESS
chaieb@31120
   777
  val {add,mul,neg,pow,sub,main} = 
chaieb@31120
   778
     Normalizer.semiring_normalizers_ord_wrapper ctxt
chaieb@31120
   779
      (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) 
chaieb@31120
   780
     simple_cterm_ord
chaieb@31120
   781
in gen_real_arith ctxt
chaieb@31120
   782
   (cterm_of_rat, field_comp_conv, field_comp_conv,field_comp_conv,
chaieb@31120
   783
    main,neg,add,mul, prover)
chaieb@31120
   784
end;
chaieb@31120
   785
chaieb@31120
   786
fun real_arith ctxt = gen_prover_real_arith ctxt real_linear_prover;
chaieb@31120
   787
end