src/HOL/Library/normarith.ML
author chaieb
Mon Feb 09 16:43:52 2009 +0000 (2009-02-09)
changeset 29841 86d94bb79226
child 29843 4bb780545478
permissions -rw-r--r--
A generic decision procedure for linear rea arithmetic and normed vector spaces
chaieb@29841
     1
(* A functor for finite mappings based on Tables *)
chaieb@29841
     2
signature FUNC = 
chaieb@29841
     3
sig
chaieb@29841
     4
 type 'a T
chaieb@29841
     5
 type key
chaieb@29841
     6
 val apply : 'a T -> key -> 'a
chaieb@29841
     7
 val applyd :'a T -> (key -> 'a) -> key -> 'a
chaieb@29841
     8
 val combine : ('a -> 'a -> 'a) -> ('a -> bool) -> 'a T -> 'a T -> 'a T
chaieb@29841
     9
 val defined : 'a T -> key -> bool
chaieb@29841
    10
 val dom : 'a T -> key list
chaieb@29841
    11
 val fold : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b
chaieb@29841
    12
 val graph : 'a T -> (key * 'a) list
chaieb@29841
    13
 val is_undefined : 'a T -> bool
chaieb@29841
    14
 val mapf : ('a -> 'b) -> 'a T -> 'b T
chaieb@29841
    15
 val tryapplyd : 'a T -> key -> 'a -> 'a
chaieb@29841
    16
 val undefine :  key -> 'a T -> 'a T
chaieb@29841
    17
 val undefined : 'a T
chaieb@29841
    18
 val update : key * 'a -> 'a T -> 'a T
chaieb@29841
    19
 val updatep : (key * 'a -> bool) -> key * 'a -> 'a T -> 'a T
chaieb@29841
    20
 val choose : 'a T -> key * 'a
chaieb@29841
    21
 val onefunc : key * 'a -> 'a T
chaieb@29841
    22
 val get_first: (key*'a -> 'a option) -> 'a T -> 'a option
chaieb@29841
    23
 val fns: 
chaieb@29841
    24
   {key_ord: key*key -> order,
chaieb@29841
    25
    apply : 'a T -> key -> 'a,
chaieb@29841
    26
    applyd :'a T -> (key -> 'a) -> key -> 'a,
chaieb@29841
    27
    combine : ('a -> 'a -> 'a) -> ('a -> bool) -> 'a T -> 'a T -> 'a T,
chaieb@29841
    28
    defined : 'a T -> key -> bool,
chaieb@29841
    29
    dom : 'a T -> key list,
chaieb@29841
    30
    fold : (key * 'a -> 'b -> 'b) -> 'a T -> 'b -> 'b,
chaieb@29841
    31
    graph : 'a T -> (key * 'a) list,
chaieb@29841
    32
    is_undefined : 'a T -> bool,
chaieb@29841
    33
    mapf : ('a -> 'b) -> 'a T -> 'b T,
chaieb@29841
    34
    tryapplyd : 'a T -> key -> 'a -> 'a,
chaieb@29841
    35
    undefine :  key -> 'a T -> 'a T,
chaieb@29841
    36
    undefined : 'a T,
chaieb@29841
    37
    update : key * 'a -> 'a T -> 'a T,
chaieb@29841
    38
    updatep : (key * 'a -> bool) -> key * 'a -> 'a T -> 'a T,
chaieb@29841
    39
    choose : 'a T -> key * 'a,
chaieb@29841
    40
    onefunc : key * 'a -> 'a T,
chaieb@29841
    41
    get_first: (key*'a -> 'a option) -> 'a T -> 'a option}
chaieb@29841
    42
end;
chaieb@29841
    43
chaieb@29841
    44
functor FuncFun(Key: KEY) : FUNC=
chaieb@29841
    45
struct
chaieb@29841
    46
chaieb@29841
    47
type key = Key.key;
chaieb@29841
    48
structure Tab = TableFun(Key);
chaieb@29841
    49
type 'a T = 'a Tab.table;
chaieb@29841
    50
chaieb@29841
    51
val undefined = Tab.empty;
chaieb@29841
    52
val is_undefined = Tab.is_empty;
chaieb@29841
    53
val mapf = Tab.map;
chaieb@29841
    54
val fold = Tab.fold;
chaieb@29841
    55
val graph = Tab.dest;
chaieb@29841
    56
val dom = Tab.keys;
chaieb@29841
    57
fun applyd f d x = case Tab.lookup f x of 
chaieb@29841
    58
   SOME y => y
chaieb@29841
    59
 | NONE => d x;
chaieb@29841
    60
chaieb@29841
    61
fun apply f x = applyd f (fn _ => raise Tab.UNDEF x) x;
chaieb@29841
    62
fun tryapplyd f a d = applyd f (K d) a;
chaieb@29841
    63
val defined = Tab.defined;
chaieb@29841
    64
fun undefine x t = (Tab.delete x t handle UNDEF => t);
chaieb@29841
    65
val update = Tab.update;
chaieb@29841
    66
fun updatep p (k,v) t = if p (k, v) then t else update (k,v) t
chaieb@29841
    67
fun combine f z a b = 
chaieb@29841
    68
 let
chaieb@29841
    69
  fun h (k,v) t = case Tab.lookup t k of
chaieb@29841
    70
     NONE => Tab.update (k,v) t
chaieb@29841
    71
   | SOME v' => let val w = f v v'
chaieb@29841
    72
     in if z w then Tab.delete k t else Tab.update (k,w) t end;
chaieb@29841
    73
  in Tab.fold h a b end;
chaieb@29841
    74
chaieb@29841
    75
fun choose f = case Tab.max_key f of 
chaieb@29841
    76
   SOME k => (k,valOf (Tab.lookup f k))
chaieb@29841
    77
 | NONE => error "FuncFun.choose : Completely undefined function"
chaieb@29841
    78
chaieb@29841
    79
fun onefunc kv = update kv undefined
chaieb@29841
    80
chaieb@29841
    81
local
chaieb@29841
    82
fun  find f (k,v) NONE = f (k,v)
chaieb@29841
    83
   | find f (k,v) r = r
chaieb@29841
    84
in
chaieb@29841
    85
fun get_first f t = fold (find f) t NONE
chaieb@29841
    86
end
chaieb@29841
    87
chaieb@29841
    88
val fns = 
chaieb@29841
    89
   {key_ord = Key.ord,
chaieb@29841
    90
    apply = apply,
chaieb@29841
    91
    applyd = applyd,
chaieb@29841
    92
    combine = combine,
chaieb@29841
    93
    defined = defined,
chaieb@29841
    94
    dom = dom,
chaieb@29841
    95
    fold = fold,
chaieb@29841
    96
    graph = graph,
chaieb@29841
    97
    is_undefined = is_undefined,
chaieb@29841
    98
    mapf = mapf,
chaieb@29841
    99
    tryapplyd = tryapplyd,
chaieb@29841
   100
    undefine = undefine,
chaieb@29841
   101
    undefined = undefined,
chaieb@29841
   102
    update = update,
chaieb@29841
   103
    updatep = updatep,
chaieb@29841
   104
    choose = choose,
chaieb@29841
   105
    onefunc = onefunc,
chaieb@29841
   106
    get_first = get_first}
chaieb@29841
   107
chaieb@29841
   108
end;
chaieb@29841
   109
chaieb@29841
   110
structure Intfunc = FuncFun(type key = int val ord = int_ord);
chaieb@29841
   111
structure Symfunc = FuncFun(type key = string val ord = fast_string_ord);
chaieb@29841
   112
structure Termfunc = FuncFun(type key = term val ord = TermOrd.fast_term_ord);
chaieb@29841
   113
structure Ctermfunc = FuncFun(type key = cterm val ord = (fn (s,t) => TermOrd.fast_term_ord(term_of s, term_of t)));
chaieb@29841
   114
structure Ratfunc = FuncFun(type key = Rat.rat val ord = Rat.ord);
chaieb@29841
   115
chaieb@29841
   116
    (* Some conversions-related stuff which has been forbidden entrance into Pure/conv.ML*)
chaieb@29841
   117
structure Conv2 = 
chaieb@29841
   118
struct
chaieb@29841
   119
 open Conv
chaieb@29841
   120
fun instantiate_cterm' ty tms = Drule.cterm_rule (Drule.instantiate' ty tms)
chaieb@29841
   121
fun is_comb t = case (term_of t) of _$_ => true | _ => false;
chaieb@29841
   122
fun is_abs t = case (term_of t) of Abs _ => true | _ => false;
chaieb@29841
   123
chaieb@29841
   124
fun end_itlist f l =
chaieb@29841
   125
 case l of 
chaieb@29841
   126
   []     => error "end_itlist"
chaieb@29841
   127
 | [x]    => x
chaieb@29841
   128
 | (h::t) => f h (end_itlist f t);
chaieb@29841
   129
chaieb@29841
   130
 fun absc cv ct = case term_of ct of 
chaieb@29841
   131
 Abs (v,_, _) => 
chaieb@29841
   132
  let val (x,t) = Thm.dest_abs (SOME v) ct
chaieb@29841
   133
  in Thm.abstract_rule ((fst o dest_Free o term_of) x) x (cv t)
chaieb@29841
   134
  end
chaieb@29841
   135
 | _ => all_conv ct;
chaieb@29841
   136
chaieb@29841
   137
fun cache_conv conv =
chaieb@29841
   138
 let 
chaieb@29841
   139
  val tab = ref Termtab.empty
chaieb@29841
   140
  fun cconv t =  
chaieb@29841
   141
    case Termtab.lookup (!tab) (term_of t) of
chaieb@29841
   142
     SOME th => th
chaieb@29841
   143
   | NONE => let val th = conv t
chaieb@29841
   144
             in ((tab := Termtab.insert Thm.eq_thm (term_of t, th) (!tab)); th) end
chaieb@29841
   145
 in cconv end;
chaieb@29841
   146
fun is_binop ct ct' = ct aconvc (Thm.dest_fun (Thm.dest_fun ct'))
chaieb@29841
   147
  handle CTERM _ => false;
chaieb@29841
   148
chaieb@29841
   149
local
chaieb@29841
   150
 fun thenqc conv1 conv2 tm =
chaieb@29841
   151
   case try conv1 tm of
chaieb@29841
   152
    SOME th1 => (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
chaieb@29841
   153
  | NONE => conv2 tm
chaieb@29841
   154
chaieb@29841
   155
 fun thencqc conv1 conv2 tm =
chaieb@29841
   156
    let val th1 = conv1 tm 
chaieb@29841
   157
    in (case try conv2 (Thm.rhs_of th1) of SOME th2 => Thm.transitive th1 th2 | NONE => th1)
chaieb@29841
   158
    end
chaieb@29841
   159
 fun comb_qconv conv tm =
chaieb@29841
   160
   let val (l,r) = Thm.dest_comb tm 
chaieb@29841
   161
   in (case try conv l of 
chaieb@29841
   162
        SOME th1 => (case try conv r of SOME th2 => Thm.combination th1 th2 
chaieb@29841
   163
                                      | NONE => Drule.fun_cong_rule th1 r)
chaieb@29841
   164
      | NONE => Drule.arg_cong_rule l (conv r))
chaieb@29841
   165
   end
chaieb@29841
   166
 fun repeatqc conv tm = thencqc conv (repeatqc conv) tm 
chaieb@29841
   167
 fun sub_qconv conv tm =  if is_abs tm then absc conv tm else comb_qconv conv tm 
chaieb@29841
   168
 fun once_depth_qconv conv tm =
chaieb@29841
   169
      (conv else_conv (sub_qconv (once_depth_qconv conv))) tm
chaieb@29841
   170
 fun depth_qconv conv tm =
chaieb@29841
   171
    thenqc (sub_qconv (depth_qconv conv))
chaieb@29841
   172
           (repeatqc conv) tm
chaieb@29841
   173
 fun redepth_qconv conv tm =
chaieb@29841
   174
    thenqc (sub_qconv (redepth_qconv conv))
chaieb@29841
   175
           (thencqc conv (redepth_qconv conv)) tm
chaieb@29841
   176
 fun top_depth_qconv conv tm =
chaieb@29841
   177
    thenqc (repeatqc conv)
chaieb@29841
   178
           (thencqc (sub_qconv (top_depth_qconv conv))
chaieb@29841
   179
                    (thencqc conv (top_depth_qconv conv))) tm
chaieb@29841
   180
 fun top_sweep_qconv conv tm =
chaieb@29841
   181
    thenqc (repeatqc conv)
chaieb@29841
   182
           (sub_qconv (top_sweep_qconv conv)) tm
chaieb@29841
   183
in 
chaieb@29841
   184
val (once_depth_conv, depth_conv, rdepth_conv, top_depth_conv, top_sweep_conv) = 
chaieb@29841
   185
  (fn c => try_conv (once_depth_qconv c),
chaieb@29841
   186
   fn c => try_conv (depth_qconv c),
chaieb@29841
   187
   fn c => try_conv (redepth_qconv c),
chaieb@29841
   188
   fn c => try_conv (top_depth_qconv c),
chaieb@29841
   189
   fn c => try_conv (top_sweep_qconv c));
chaieb@29841
   190
end;
chaieb@29841
   191
end;
chaieb@29841
   192
chaieb@29841
   193
chaieb@29841
   194
    (* Some useful derived rules *)
chaieb@29841
   195
fun deduct_antisym_rule tha thb = 
chaieb@29841
   196
    equal_intr (implies_intr (cprop_of thb) tha) 
chaieb@29841
   197
     (implies_intr (cprop_of tha) thb);
chaieb@29841
   198
chaieb@29841
   199
fun prove_hyp tha thb = 
chaieb@29841
   200
  if exists (curry op aconv (concl_of tha)) (#hyps (rep_thm thb)) 
chaieb@29841
   201
  then equal_elim (symmetric (deduct_antisym_rule tha thb)) tha else thb;
chaieb@29841
   202
chaieb@29841
   203
chaieb@29841
   204
chaieb@29841
   205
signature REAL_ARITH = 
chaieb@29841
   206
sig
chaieb@29841
   207
  datatype positivstellensatz =
chaieb@29841
   208
   Axiom_eq of int
chaieb@29841
   209
 | Axiom_le of int
chaieb@29841
   210
 | Axiom_lt of int
chaieb@29841
   211
 | Rational_eq of Rat.rat
chaieb@29841
   212
 | Rational_le of Rat.rat
chaieb@29841
   213
 | Rational_lt of Rat.rat
chaieb@29841
   214
 | Square of cterm
chaieb@29841
   215
 | Eqmul of cterm * positivstellensatz
chaieb@29841
   216
 | Sum of positivstellensatz * positivstellensatz
chaieb@29841
   217
 | Product of positivstellensatz * positivstellensatz;
chaieb@29841
   218
chaieb@29841
   219
val gen_gen_real_arith :
chaieb@29841
   220
  Proof.context -> (Rat.rat -> Thm.cterm) * conv * conv * conv * 
chaieb@29841
   221
   conv * conv * conv * conv * conv * conv * 
chaieb@29841
   222
    ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@29841
   223
        thm list * thm list * thm list -> thm) -> conv
chaieb@29841
   224
val real_linear_prover : 
chaieb@29841
   225
  (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@29841
   226
   thm list * thm list * thm list -> thm
chaieb@29841
   227
chaieb@29841
   228
val gen_real_arith : Proof.context ->
chaieb@29841
   229
   (Rat.rat -> cterm) * conv * conv * conv * conv * conv * conv * conv *
chaieb@29841
   230
   ( (thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@29841
   231
       thm list * thm list * thm list -> thm) -> conv
chaieb@29841
   232
val gen_prover_real_arith : Proof.context ->
chaieb@29841
   233
   ((thm list * thm list * thm list -> positivstellensatz -> thm) ->
chaieb@29841
   234
     thm list * thm list * thm list -> thm) -> conv
chaieb@29841
   235
val real_arith : Proof.context -> conv
chaieb@29841
   236
end
chaieb@29841
   237
chaieb@29841
   238
structure RealArith (* : REAL_ARITH *)=
chaieb@29841
   239
struct
chaieb@29841
   240
chaieb@29841
   241
 open Conv Thm Conv2;;
chaieb@29841
   242
(* ------------------------------------------------------------------------- *)
chaieb@29841
   243
(* Data structure for Positivstellensatz refutations.                        *)
chaieb@29841
   244
(* ------------------------------------------------------------------------- *)
chaieb@29841
   245
chaieb@29841
   246
datatype positivstellensatz =
chaieb@29841
   247
   Axiom_eq of int
chaieb@29841
   248
 | Axiom_le of int
chaieb@29841
   249
 | Axiom_lt of int
chaieb@29841
   250
 | Rational_eq of Rat.rat
chaieb@29841
   251
 | Rational_le of Rat.rat
chaieb@29841
   252
 | Rational_lt of Rat.rat
chaieb@29841
   253
 | Square of cterm
chaieb@29841
   254
 | Eqmul of cterm * positivstellensatz
chaieb@29841
   255
 | Sum of positivstellensatz * positivstellensatz
chaieb@29841
   256
 | Product of positivstellensatz * positivstellensatz;
chaieb@29841
   257
         (* Theorems used in the procedure *)
chaieb@29841
   258
chaieb@29841
   259
fun conjunctions th = case try Conjunction.elim th of
chaieb@29841
   260
   SOME (th1,th2) => (conjunctions th1) @ conjunctions th2
chaieb@29841
   261
 | NONE => [th];
chaieb@29841
   262
chaieb@29841
   263
val pth = @{lemma "(((x::real) < y) == (y - x > 0)) &&& ((x <= y) == (y - x >= 0)) 
chaieb@29841
   264
     &&& ((x = y) == (x - y = 0)) &&& ((~(x < y)) == (x - y >= 0)) &&& ((~(x <= y)) == (x - y > 0))
chaieb@29841
   265
     &&& ((~(x = y)) == (x - y > 0 | -(x - y) > 0))"
chaieb@29841
   266
  by (atomize (full), auto simp add: less_diff_eq le_diff_eq not_less)} |> 
chaieb@29841
   267
conjunctions;
chaieb@29841
   268
chaieb@29841
   269
val pth_final = @{lemma "(~p ==> False) ==> p" by blast}
chaieb@29841
   270
val pth_add = 
chaieb@29841
   271
 @{lemma "(x = (0::real) ==> y = 0 ==> x + y = 0 ) &&& ( x = 0 ==> y >= 0 ==> x + y >= 0) 
chaieb@29841
   272
    &&& (x = 0 ==> y > 0 ==> x + y > 0) &&& (x >= 0 ==> y = 0 ==> x + y >= 0) 
chaieb@29841
   273
    &&& (x >= 0 ==> y >= 0 ==> x + y >= 0) &&& (x >= 0 ==> y > 0 ==> x + y > 0) 
chaieb@29841
   274
    &&& (x > 0 ==> y = 0 ==> x + y > 0) &&& (x > 0 ==> y >= 0 ==> x + y > 0) 
chaieb@29841
   275
    &&& (x > 0 ==> y > 0 ==> x + y > 0)"  by simp_all} |> conjunctions ;
chaieb@29841
   276
chaieb@29841
   277
val pth_mul = 
chaieb@29841
   278
  @{lemma "(x = (0::real) ==> y = 0 ==> x * y = 0) &&& (x = 0 ==> y >= 0 ==> x * y = 0) &&& 
chaieb@29841
   279
           (x = 0 ==> y > 0 ==> x * y = 0) &&& (x >= 0 ==> y = 0 ==> x * y = 0) &&& 
chaieb@29841
   280
           (x >= 0 ==> y >= 0 ==> x * y >= 0 ) &&& ( x >= 0 ==> y > 0 ==> x * y >= 0 ) &&&
chaieb@29841
   281
           (x > 0 ==>  y = 0 ==> x * y = 0 ) &&& ( x > 0 ==> y >= 0 ==> x * y >= 0 ) &&&
chaieb@29841
   282
           (x > 0 ==>  y > 0 ==> x * y > 0)"
chaieb@29841
   283
  by (auto intro: mult_mono[where a="0::real" and b="x" and d="y" and c="0", simplified]
chaieb@29841
   284
    mult_strict_mono[where b="x" and d="y" and a="0" and c="0", simplified])} |> conjunctions;
chaieb@29841
   285
chaieb@29841
   286
val pth_emul = @{lemma "y = (0::real) ==> x * y = 0"  by simp};
chaieb@29841
   287
val pth_square = @{lemma "x * x >= (0::real)"  by simp};
chaieb@29841
   288
chaieb@29841
   289
val weak_dnf_simps = List.take (simp_thms, 34) 
chaieb@29841
   290
    @ 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@29841
   291
chaieb@29841
   292
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@29841
   293
chaieb@29841
   294
val choice_iff = @{lemma "(ALL x. EX y. P x y) = (EX f. ALL x. P x (f x))" by metis};
chaieb@29841
   295
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@29841
   296
chaieb@29841
   297
val real_abs_thms1 = conjunctions @{lemma
chaieb@29841
   298
  "((-1 * abs(x::real) >= r) = (-1 * x >= r & 1 * x >= r)) &&&
chaieb@29841
   299
  ((-1 * abs(x) + a >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
chaieb@29841
   300
  ((a + -1 * abs(x) >= r) = (a + -1 * x >= r & a + 1 * x >= r)) &&&
chaieb@29841
   301
  ((a + -1 * abs(x) + b >= r) = (a + -1 * x + b >= r & a + 1 * x + b >= r)) &&&
chaieb@29841
   302
  ((a + b + -1 * abs(x) >= r) = (a + b + -1 * x >= r & a + b + 1 * x >= r)) &&&
chaieb@29841
   303
  ((a + b + -1 * abs(x) + c >= r) = (a + b + -1 * x + c >= r & a + b + 1 * x + c >= r)) &&&
chaieb@29841
   304
  ((-1 * max x y >= r) = (-1 * x >= r & -1 * y >= r)) &&&
chaieb@29841
   305
  ((-1 * max x y + a >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
chaieb@29841
   306
  ((a + -1 * max x y >= r) = (a + -1 * x >= r & a + -1 * y >= r)) &&&
chaieb@29841
   307
  ((a + -1 * max x y + b >= r) = (a + -1 * x + b >= r & a + -1 * y  + b >= r)) &&&
chaieb@29841
   308
  ((a + b + -1 * max x y >= r) = (a + b + -1 * x >= r & a + b + -1 * y >= r)) &&&
chaieb@29841
   309
  ((a + b + -1 * max x y + c >= r) = (a + b + -1 * x + c >= r & a + b + -1 * y  + c >= r)) &&&
chaieb@29841
   310
  ((1 * min x y >= r) = (1 * x >= r & 1 * y >= r)) &&&
chaieb@29841
   311
  ((1 * min x y + a >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
chaieb@29841
   312
  ((a + 1 * min x y >= r) = (a + 1 * x >= r & a + 1 * y >= r)) &&&
chaieb@29841
   313
  ((a + 1 * min x y + b >= r) = (a + 1 * x + b >= r & a + 1 * y  + b >= r) )&&&
chaieb@29841
   314
  ((a + b + 1 * min x y >= r) = (a + b + 1 * x >= r & a + b + 1 * y >= r)) &&&
chaieb@29841
   315
  ((a + b + 1 * min x y + c >= r) = (a + b + 1 * x + c >= r & a + b + 1 * y  + c >= r)) &&&
chaieb@29841
   316
  ((min x y >= r) = (x >= r &  y >= r)) &&&
chaieb@29841
   317
  ((min x y + a >= r) = (a + x >= r & a + y >= r)) &&&
chaieb@29841
   318
  ((a + min x y >= r) = (a + x >= r & a + y >= r)) &&&
chaieb@29841
   319
  ((a + min x y + b >= r) = (a + x + b >= r & a + y  + b >= r)) &&&
chaieb@29841
   320
  ((a + b + min x y >= r) = (a + b + x >= r & a + b + y >= r) )&&&
chaieb@29841
   321
  ((a + b + min x y + c >= r) = (a + b + x + c >= r & a + b + y + c >= r)) &&&
chaieb@29841
   322
  ((-1 * abs(x) > r) = (-1 * x > r & 1 * x > r)) &&&
chaieb@29841
   323
  ((-1 * abs(x) + a > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
chaieb@29841
   324
  ((a + -1 * abs(x) > r) = (a + -1 * x > r & a + 1 * x > r)) &&&
chaieb@29841
   325
  ((a + -1 * abs(x) + b > r) = (a + -1 * x + b > r & a + 1 * x + b > r)) &&&
chaieb@29841
   326
  ((a + b + -1 * abs(x) > r) = (a + b + -1 * x > r & a + b + 1 * x > r)) &&&
chaieb@29841
   327
  ((a + b + -1 * abs(x) + c > r) = (a + b + -1 * x + c > r & a + b + 1 * x + c > r)) &&&
chaieb@29841
   328
  ((-1 * max x y > r) = ((-1 * x > r) & -1 * y > r)) &&&
chaieb@29841
   329
  ((-1 * max x y + a > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
chaieb@29841
   330
  ((a + -1 * max x y > r) = (a + -1 * x > r & a + -1 * y > r)) &&&
chaieb@29841
   331
  ((a + -1 * max x y + b > r) = (a + -1 * x + b > r & a + -1 * y  + b > r)) &&&
chaieb@29841
   332
  ((a + b + -1 * max x y > r) = (a + b + -1 * x > r & a + b + -1 * y > r)) &&&
chaieb@29841
   333
  ((a + b + -1 * max x y + c > r) = (a + b + -1 * x + c > r & a + b + -1 * y  + c > r)) &&&
chaieb@29841
   334
  ((min x y > r) = (x > r &  y > r)) &&&
chaieb@29841
   335
  ((min x y + a > r) = (a + x > r & a + y > r)) &&&
chaieb@29841
   336
  ((a + min x y > r) = (a + x > r & a + y > r)) &&&
chaieb@29841
   337
  ((a + min x y + b > r) = (a + x + b > r & a + y  + b > r)) &&&
chaieb@29841
   338
  ((a + b + min x y > r) = (a + b + x > r & a + b + y > r)) &&&
chaieb@29841
   339
  ((a + b + min x y + c > r) = (a + b + x + c > r & a + b + y + c > r))"
chaieb@29841
   340
  by auto};
chaieb@29841
   341
chaieb@29841
   342
val abs_split' = @{lemma "P (abs (x::'a::ordered_idom)) == (x >= 0 & P x | x < 0 & P (-x))"
chaieb@29841
   343
  by (atomize (full)) (auto split add: abs_split)};
chaieb@29841
   344
chaieb@29841
   345
val max_split = @{lemma "P (max x y) == ((x::'a::linorder) <= y & P y | x > y & P x)"
chaieb@29841
   346
  by (atomize (full)) (cases "x <= y", auto simp add: max_def)};
chaieb@29841
   347
chaieb@29841
   348
val min_split = @{lemma "P (min x y) == ((x::'a::linorder) <= y & P x | x > y & P y)"
chaieb@29841
   349
  by (atomize (full)) (cases "x <= y", auto simp add: min_def)};
chaieb@29841
   350
chaieb@29841
   351
chaieb@29841
   352
         (* Miscalineous *)
chaieb@29841
   353
fun literals_conv bops uops cv = 
chaieb@29841
   354
 let fun h t =
chaieb@29841
   355
  case (term_of t) of 
chaieb@29841
   356
   b$_$_ => if member (op aconv) bops b then binop_conv h t else cv t
chaieb@29841
   357
 | u$_ => if member (op aconv) uops u then arg_conv h t else cv t
chaieb@29841
   358
 | _ => cv t
chaieb@29841
   359
 in h end;
chaieb@29841
   360
chaieb@29841
   361
fun cterm_of_rat x = 
chaieb@29841
   362
let val (a, b) = Rat.quotient_of_rat x
chaieb@29841
   363
in 
chaieb@29841
   364
 if b = 1 then Numeral.mk_cnumber @{ctyp "real"} a
chaieb@29841
   365
  else Thm.capply (Thm.capply @{cterm "op / :: real => _"} 
chaieb@29841
   366
                   (Numeral.mk_cnumber @{ctyp "real"} a))
chaieb@29841
   367
        (Numeral.mk_cnumber @{ctyp "real"} b)
chaieb@29841
   368
end;
chaieb@29841
   369
chaieb@29841
   370
  fun dest_ratconst t = case term_of t of
chaieb@29841
   371
   Const(@{const_name divide}, _)$a$b => Rat.rat_of_quotient(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
chaieb@29841
   372
 | _ => Rat.rat_of_int (HOLogic.dest_number (term_of t) |> snd)
chaieb@29841
   373
 fun is_ratconst t = can dest_ratconst t
chaieb@29841
   374
chaieb@29841
   375
fun find_term p t = if p t then t else 
chaieb@29841
   376
 case t of
chaieb@29841
   377
  a$b => (find_term p a handle TERM _ => find_term p b)
chaieb@29841
   378
 | Abs (_,_,t') => find_term p t'
chaieb@29841
   379
 | _ => raise TERM ("find_term",[t]);
chaieb@29841
   380
chaieb@29841
   381
fun find_cterm p t = if p t then t else 
chaieb@29841
   382
 case term_of t of
chaieb@29841
   383
  a$b => (find_cterm p (Thm.dest_fun t) handle CTERM _ => find_cterm p (Thm.dest_arg t))
chaieb@29841
   384
 | Abs (_,_,t') => find_cterm p (Thm.dest_abs NONE t |> snd)
chaieb@29841
   385
 | _ => raise CTERM ("find_cterm",[t]);
chaieb@29841
   386
chaieb@29841
   387
chaieb@29841
   388
    (* A general real arithmetic prover *)
chaieb@29841
   389
chaieb@29841
   390
fun gen_gen_real_arith ctxt (mk_numeric,
chaieb@29841
   391
       numeric_eq_conv,numeric_ge_conv,numeric_gt_conv,
chaieb@29841
   392
       poly_conv,poly_neg_conv,poly_add_conv,poly_mul_conv,
chaieb@29841
   393
       absconv1,absconv2,prover) = 
chaieb@29841
   394
let
chaieb@29841
   395
 open Conv Thm; 
chaieb@29841
   396
 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@29841
   397
 val prenex_ss = HOL_basic_ss addsimps prenex_simps
chaieb@29841
   398
 val skolemize_ss = HOL_basic_ss addsimps [choice_iff]
chaieb@29841
   399
 val presimp_conv = Simplifier.rewrite (Simplifier.context ctxt pre_ss)
chaieb@29841
   400
 val prenex_conv = Simplifier.rewrite (Simplifier.context ctxt prenex_ss)
chaieb@29841
   401
 val skolemize_conv = Simplifier.rewrite (Simplifier.context ctxt skolemize_ss)
chaieb@29841
   402
 val weak_dnf_ss = HOL_basic_ss addsimps weak_dnf_simps
chaieb@29841
   403
 val weak_dnf_conv = Simplifier.rewrite (Simplifier.context ctxt weak_dnf_ss)
chaieb@29841
   404
 fun eqT_elim th = equal_elim (symmetric th) @{thm TrueI}
chaieb@29841
   405
 fun oprconv cv ct = 
chaieb@29841
   406
  let val g = Thm.dest_fun2 ct
chaieb@29841
   407
  in if g aconvc @{cterm "op <= :: real => _"} 
chaieb@29841
   408
       orelse g aconvc @{cterm "op < :: real => _"} 
chaieb@29841
   409
     then arg_conv cv ct else arg1_conv cv ct
chaieb@29841
   410
  end
chaieb@29841
   411
chaieb@29841
   412
 fun real_ineq_conv th ct =
chaieb@29841
   413
  let
chaieb@29841
   414
   val th' = (instantiate (match (lhs_of th, ct)) th 
chaieb@29841
   415
      handle MATCH => raise CTERM ("real_ineq_conv", [ct]))
chaieb@29841
   416
  in transitive th' (oprconv poly_conv (Thm.rhs_of th'))
chaieb@29841
   417
  end 
chaieb@29841
   418
  val [real_lt_conv, real_le_conv, real_eq_conv,
chaieb@29841
   419
       real_not_lt_conv, real_not_le_conv, _] =
chaieb@29841
   420
       map real_ineq_conv pth
chaieb@29841
   421
  fun match_mp_rule ths ths' = 
chaieb@29841
   422
   let
chaieb@29841
   423
     fun f ths ths' = case ths of [] => raise THM("match_mp_rule",0,ths)
chaieb@29841
   424
      | th::ths => (ths' MRS th handle THM _ => f ths ths')
chaieb@29841
   425
   in f ths ths' end
chaieb@29841
   426
  fun mul_rule th th' = fconv_rule (arg_conv (oprconv poly_mul_conv))
chaieb@29841
   427
         (match_mp_rule pth_mul [th, th'])
chaieb@29841
   428
  fun add_rule th th' = fconv_rule (arg_conv (oprconv poly_add_conv))
chaieb@29841
   429
         (match_mp_rule pth_add [th, th'])
chaieb@29841
   430
  fun emul_rule ct th = fconv_rule (arg_conv (oprconv poly_mul_conv)) 
chaieb@29841
   431
       (instantiate' [] [SOME ct] (th RS pth_emul)) 
chaieb@29841
   432
  fun square_rule t = fconv_rule (arg_conv (oprconv poly_mul_conv))
chaieb@29841
   433
       (instantiate' [] [SOME t] pth_square)
chaieb@29841
   434
chaieb@29841
   435
  fun hol_of_positivstellensatz(eqs,les,lts) =
chaieb@29841
   436
   let 
chaieb@29841
   437
    fun translate prf = case prf of
chaieb@29841
   438
        Axiom_eq n => nth eqs n
chaieb@29841
   439
      | Axiom_le n => nth les n
chaieb@29841
   440
      | Axiom_lt n => nth lts n
chaieb@29841
   441
      | Rational_eq x => eqT_elim(numeric_eq_conv(capply @{cterm Trueprop} 
chaieb@29841
   442
                          (capply (capply @{cterm "op =::real => _"} (mk_numeric x)) 
chaieb@29841
   443
                               @{cterm "0::real"})))
chaieb@29841
   444
      | Rational_le x => eqT_elim(numeric_ge_conv(capply @{cterm Trueprop} 
chaieb@29841
   445
                          (capply (capply @{cterm "op <=::real => _"} 
chaieb@29841
   446
                                     @{cterm "0::real"}) (mk_numeric x))))
chaieb@29841
   447
      | Rational_lt x => eqT_elim(numeric_gt_conv(capply @{cterm Trueprop} 
chaieb@29841
   448
                      (capply (capply @{cterm "op <::real => _"} @{cterm "0::real"})
chaieb@29841
   449
                        (mk_numeric x))))
chaieb@29841
   450
      | Square t => square_rule t
chaieb@29841
   451
      | Eqmul(t,p) => emul_rule t (translate p)
chaieb@29841
   452
      | Sum(p1,p2) => add_rule (translate p1) (translate p2)
chaieb@29841
   453
      | Product(p1,p2) => mul_rule (translate p1) (translate p2)
chaieb@29841
   454
   in fn prf => 
chaieb@29841
   455
      fconv_rule (first_conv [numeric_ge_conv, numeric_gt_conv, numeric_eq_conv, all_conv]) 
chaieb@29841
   456
          (translate prf)
chaieb@29841
   457
   end
chaieb@29841
   458
  
chaieb@29841
   459
  val init_conv = presimp_conv then_conv
chaieb@29841
   460
      nnf_conv then_conv skolemize_conv then_conv prenex_conv then_conv
chaieb@29841
   461
      weak_dnf_conv
chaieb@29841
   462
chaieb@29841
   463
  val concl = dest_arg o cprop_of
chaieb@29841
   464
  fun is_binop opr ct = (dest_fun2 ct aconvc opr handle CTERM _ => false)
chaieb@29841
   465
  val is_req = is_binop @{cterm "op =:: real => _"}
chaieb@29841
   466
  val is_ge = is_binop @{cterm "op <=:: real => _"}
chaieb@29841
   467
  val is_gt = is_binop @{cterm "op <:: real => _"}
chaieb@29841
   468
  val is_conj = is_binop @{cterm "op &"}
chaieb@29841
   469
  val is_disj = is_binop @{cterm "op |"}
chaieb@29841
   470
  fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
chaieb@29841
   471
  fun disj_cases th th1 th2 = 
chaieb@29841
   472
   let val (p,q) = dest_binop (concl th)
chaieb@29841
   473
       val c = concl th1
chaieb@29841
   474
       val _ = if c aconvc (concl th2) then () else error "disj_cases : conclusions not alpha convertible"
chaieb@29841
   475
   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@29841
   476
   end
chaieb@29841
   477
 fun overall dun ths = case ths of
chaieb@29841
   478
  [] =>
chaieb@29841
   479
   let 
chaieb@29841
   480
    val (eq,ne) = List.partition (is_req o concl) dun
chaieb@29841
   481
     val (le,nl) = List.partition (is_ge o concl) ne
chaieb@29841
   482
     val lt = filter (is_gt o concl) nl 
chaieb@29841
   483
    in prover hol_of_positivstellensatz (eq,le,lt) end
chaieb@29841
   484
 | th::oths =>
chaieb@29841
   485
   let 
chaieb@29841
   486
    val ct = concl th 
chaieb@29841
   487
   in 
chaieb@29841
   488
    if is_conj ct  then
chaieb@29841
   489
     let 
chaieb@29841
   490
      val (th1,th2) = conj_pair th in
chaieb@29841
   491
      overall dun (th1::th2::oths) end
chaieb@29841
   492
    else if is_disj ct then
chaieb@29841
   493
      let 
chaieb@29841
   494
       val th1 = overall dun (assume (capply @{cterm Trueprop} (dest_arg1 ct))::oths)
chaieb@29841
   495
       val th2 = overall dun (assume (capply @{cterm Trueprop} (dest_arg ct))::oths)
chaieb@29841
   496
      in disj_cases th th1 th2 end
chaieb@29841
   497
   else overall (th::dun) oths
chaieb@29841
   498
  end
chaieb@29841
   499
  fun dest_binary b ct = if is_binop b ct then dest_binop ct 
chaieb@29841
   500
                         else raise CTERM ("dest_binary",[b,ct])
chaieb@29841
   501
  val dest_eq = dest_binary @{cterm "op = :: real => _"}
chaieb@29841
   502
  val neq_th = nth pth 5
chaieb@29841
   503
  fun real_not_eq_conv ct = 
chaieb@29841
   504
   let 
chaieb@29841
   505
    val (l,r) = dest_eq (dest_arg ct)
chaieb@29841
   506
    val th = instantiate ([],[(@{cpat "?x::real"},l),(@{cpat "?y::real"},r)]) neq_th
chaieb@29841
   507
    val th_p = poly_conv(dest_arg(dest_arg1(Thm.rhs_of th)))
chaieb@29841
   508
    val th_x = Drule.arg_cong_rule @{cterm "uminus :: real => _"} th_p
chaieb@29841
   509
    val th_n = fconv_rule (arg_conv poly_neg_conv) th_x
chaieb@29841
   510
    val th' = Drule.binop_cong_rule @{cterm "op |"} 
chaieb@29841
   511
     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_p)
chaieb@29841
   512
     (Drule.arg_cong_rule (capply @{cterm "op <::real=>_"} @{cterm "0::real"}) th_n)
chaieb@29841
   513
    in transitive th th' 
chaieb@29841
   514
  end
chaieb@29841
   515
 fun equal_implies_1_rule PQ = 
chaieb@29841
   516
  let 
chaieb@29841
   517
   val P = lhs_of PQ
chaieb@29841
   518
  in implies_intr P (equal_elim PQ (assume P))
chaieb@29841
   519
  end
chaieb@29841
   520
 (* FIXME!!! Copied from groebner.ml *)
chaieb@29841
   521
 val strip_exists =
chaieb@29841
   522
  let fun h (acc, t) =
chaieb@29841
   523
   case (term_of t) of
chaieb@29841
   524
    Const("Ex",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
chaieb@29841
   525
  | _ => (acc,t)
chaieb@29841
   526
  in fn t => h ([],t)
chaieb@29841
   527
  end
chaieb@29841
   528
  fun name_of x = case term_of x of
chaieb@29841
   529
   Free(s,_) => s
chaieb@29841
   530
 | Var ((s,_),_) => s
chaieb@29841
   531
 | _ => "x"
chaieb@29841
   532
chaieb@29841
   533
  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@29841
   534
chaieb@29841
   535
  val specl = fold_rev (fn x => fn th => instantiate' [] [SOME x] (th RS spec));
chaieb@29841
   536
chaieb@29841
   537
 fun ext T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat Ex}
chaieb@29841
   538
 fun mk_ex v t = Thm.capply (ext (ctyp_of_term v)) (Thm.cabs v t)
chaieb@29841
   539
chaieb@29841
   540
 fun choose v th th' = case concl_of th of 
chaieb@29841
   541
   @{term Trueprop} $ (Const("Ex",_)$_) => 
chaieb@29841
   542
    let
chaieb@29841
   543
     val p = (funpow 2 Thm.dest_arg o cprop_of) th
chaieb@29841
   544
     val T = (hd o Thm.dest_ctyp o ctyp_of_term) p
chaieb@29841
   545
     val th0 = fconv_rule (Thm.beta_conversion true)
chaieb@29841
   546
         (instantiate' [SOME T] [SOME p, (SOME o Thm.dest_arg o cprop_of) th'] exE)
chaieb@29841
   547
     val pv = (Thm.rhs_of o Thm.beta_conversion true) 
chaieb@29841
   548
           (Thm.capply @{cterm Trueprop} (Thm.capply p v))
chaieb@29841
   549
     val th1 = forall_intr v (implies_intr pv th')
chaieb@29841
   550
    in implies_elim (implies_elim th0 th) th1  end
chaieb@29841
   551
 | _ => raise THM ("choose",0,[th, th'])
chaieb@29841
   552
chaieb@29841
   553
  fun simple_choose v th = 
chaieb@29841
   554
     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@29841
   555
chaieb@29841
   556
 val strip_forall =
chaieb@29841
   557
  let fun h (acc, t) =
chaieb@29841
   558
   case (term_of t) of
chaieb@29841
   559
    Const("All",_)$Abs(x,T,p) => h (dest_abs NONE (dest_arg t) |>> (fn v => v::acc))
chaieb@29841
   560
  | _ => (acc,t)
chaieb@29841
   561
  in fn t => h ([],t)
chaieb@29841
   562
  end
chaieb@29841
   563
chaieb@29841
   564
 fun f ct =
chaieb@29841
   565
  let 
chaieb@29841
   566
   val nnf_norm_conv' = 
chaieb@29841
   567
     nnf_conv then_conv 
chaieb@29841
   568
     literals_conv [@{term "op &"}, @{term "op |"}] [] 
chaieb@29841
   569
     (cache_conv 
chaieb@29841
   570
       (first_conv [real_lt_conv, real_le_conv, 
chaieb@29841
   571
                    real_eq_conv, real_not_lt_conv, 
chaieb@29841
   572
                    real_not_le_conv, real_not_eq_conv, all_conv]))
chaieb@29841
   573
  fun absremover ct = (literals_conv [@{term "op &"}, @{term "op |"}] [] 
chaieb@29841
   574
                  (try_conv (absconv1 then_conv binop_conv (arg_conv poly_conv))) then_conv 
chaieb@29841
   575
        try_conv (absconv2 then_conv nnf_norm_conv' then_conv binop_conv absremover)) ct
chaieb@29841
   576
  val nct = capply @{cterm Trueprop} (capply @{cterm "Not"} ct)
chaieb@29841
   577
  val th0 = (init_conv then_conv arg_conv nnf_norm_conv') nct
chaieb@29841
   578
  val tm0 = dest_arg (Thm.rhs_of th0)
chaieb@29841
   579
  val th = if tm0 aconvc @{cterm False} then equal_implies_1_rule th0 else
chaieb@29841
   580
   let 
chaieb@29841
   581
    val (evs,bod) = strip_exists tm0
chaieb@29841
   582
    val (avs,ibod) = strip_forall bod
chaieb@29841
   583
    val th1 = Drule.arg_cong_rule @{cterm Trueprop} (fold mk_forall avs (absremover ibod))
chaieb@29841
   584
    val th2 = overall [] [specl avs (assume (Thm.rhs_of th1))]
chaieb@29841
   585
    val th3 = fold simple_choose evs (prove_hyp (equal_elim th1 (assume (capply @{cterm Trueprop} bod))) th2)
chaieb@29841
   586
   in  Drule.implies_intr_hyps (prove_hyp (equal_elim th0 (assume nct)) th3)
chaieb@29841
   587
   end
chaieb@29841
   588
  in implies_elim (instantiate' [] [SOME ct] pth_final) th
chaieb@29841
   589
 end
chaieb@29841
   590
in f
chaieb@29841
   591
end;
chaieb@29841
   592
chaieb@29841
   593
(* A linear arithmetic prover *)
chaieb@29841
   594
local
chaieb@29841
   595
  val linear_add = Ctermfunc.combine (curry op +/) (fn z => z =/ Rat.zero)
chaieb@29841
   596
  fun linear_cmul c = Ctermfunc.mapf (fn x => c */ x)
chaieb@29841
   597
  val one_tm = @{cterm "1::real"}
chaieb@29841
   598
  fun contradictory p (e,_) = ((Ctermfunc.is_undefined e) andalso not(p Rat.zero)) orelse
chaieb@29841
   599
     ((gen_eq_set (op aconvc) (Ctermfunc.dom e, [one_tm])) andalso not(p(Ctermfunc.apply e one_tm)))
chaieb@29841
   600
chaieb@29841
   601
  fun linear_ineqs vars (les,lts) = 
chaieb@29841
   602
   case find_first (contradictory (fn x => x >/ Rat.zero)) lts of
chaieb@29841
   603
    SOME r => r
chaieb@29841
   604
  | NONE => 
chaieb@29841
   605
   (case find_first (contradictory (fn x => x >/ Rat.zero)) les of
chaieb@29841
   606
     SOME r => r
chaieb@29841
   607
   | NONE => 
chaieb@29841
   608
     if null vars then error "linear_ineqs: no contradiction" else
chaieb@29841
   609
     let 
chaieb@29841
   610
      val ineqs = les @ lts
chaieb@29841
   611
      fun blowup v =
chaieb@29841
   612
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) ineqs) +
chaieb@29841
   613
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) ineqs) *
chaieb@29841
   614
       length(filter (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero </ Rat.zero) ineqs)
chaieb@29841
   615
      val  v = fst(hd(sort (fn ((_,i),(_,j)) => int_ord (i,j))
chaieb@29841
   616
                 (map (fn v => (v,blowup v)) vars)))
chaieb@29841
   617
      fun addup (e1,p1) (e2,p2) acc =
chaieb@29841
   618
       let 
chaieb@29841
   619
        val c1 = Ctermfunc.tryapplyd e1 v Rat.zero 
chaieb@29841
   620
        val c2 = Ctermfunc.tryapplyd e2 v Rat.zero
chaieb@29841
   621
       in if c1 */ c2 >=/ Rat.zero then acc else
chaieb@29841
   622
        let 
chaieb@29841
   623
         val e1' = linear_cmul (Rat.abs c2) e1
chaieb@29841
   624
         val e2' = linear_cmul (Rat.abs c1) e2
chaieb@29841
   625
         val p1' = Product(Rational_lt(Rat.abs c2),p1)
chaieb@29841
   626
         val p2' = Product(Rational_lt(Rat.abs c1),p2)
chaieb@29841
   627
        in (linear_add e1' e2',Sum(p1',p2'))::acc
chaieb@29841
   628
        end
chaieb@29841
   629
       end
chaieb@29841
   630
      val (les0,les1) = 
chaieb@29841
   631
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) les
chaieb@29841
   632
      val (lts0,lts1) = 
chaieb@29841
   633
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero =/ Rat.zero) lts
chaieb@29841
   634
      val (lesp,lesn) = 
chaieb@29841
   635
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) les1
chaieb@29841
   636
      val (ltsp,ltsn) = 
chaieb@29841
   637
         List.partition (fn (e,_) => Ctermfunc.tryapplyd e v Rat.zero >/ Rat.zero) lts1
chaieb@29841
   638
      val les' = fold_rev (fn ep1 => fold_rev (addup ep1) lesp) lesn les0
chaieb@29841
   639
      val lts' = fold_rev (fn ep1 => fold_rev (addup ep1) (lesp@ltsp)) ltsn
chaieb@29841
   640
                      (fold_rev (fn ep1 => fold_rev (addup ep1) (lesn@ltsn)) ltsp lts0)
chaieb@29841
   641
     in linear_ineqs (remove (op aconvc) v vars) (les',lts')
chaieb@29841
   642
     end)
chaieb@29841
   643
chaieb@29841
   644
  fun linear_eqs(eqs,les,lts) = 
chaieb@29841
   645
   case find_first (contradictory (fn x => x =/ Rat.zero)) eqs of
chaieb@29841
   646
    SOME r => r
chaieb@29841
   647
  | NONE => (case eqs of 
chaieb@29841
   648
    [] => 
chaieb@29841
   649
     let val vars = remove (op aconvc) one_tm 
chaieb@29841
   650
           (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom o fst) (les@lts) []) 
chaieb@29841
   651
     in linear_ineqs vars (les,lts) end
chaieb@29841
   652
   | (e,p)::es => 
chaieb@29841
   653
     if Ctermfunc.is_undefined e then linear_eqs (es,les,lts) else
chaieb@29841
   654
     let 
chaieb@29841
   655
      val (x,c) = Ctermfunc.choose (Ctermfunc.undefine one_tm e)
chaieb@29841
   656
      fun xform (inp as (t,q)) =
chaieb@29841
   657
       let val d = Ctermfunc.tryapplyd t x Rat.zero in
chaieb@29841
   658
        if d =/ Rat.zero then inp else
chaieb@29841
   659
        let 
chaieb@29841
   660
         val k = (Rat.neg d) */ Rat.abs c // c
chaieb@29841
   661
         val e' = linear_cmul k e
chaieb@29841
   662
         val t' = linear_cmul (Rat.abs c) t
chaieb@29841
   663
         val p' = Eqmul(cterm_of_rat k,p)
chaieb@29841
   664
         val q' = Product(Rational_lt(Rat.abs c),q) 
chaieb@29841
   665
        in (linear_add e' t',Sum(p',q')) 
chaieb@29841
   666
        end 
chaieb@29841
   667
      end
chaieb@29841
   668
     in linear_eqs(map xform es,map xform les,map xform lts)
chaieb@29841
   669
     end)
chaieb@29841
   670
chaieb@29841
   671
  fun linear_prover (eq,le,lt) = 
chaieb@29841
   672
   let 
chaieb@29841
   673
    val eqs = map2 (fn p => fn n => (p,Axiom_eq n)) eq (0 upto (length eq - 1))
chaieb@29841
   674
    val les = map2 (fn p => fn n => (p,Axiom_le n)) le (0 upto (length le - 1))
chaieb@29841
   675
    val lts = map2 (fn p => fn n => (p,Axiom_lt n)) lt (0 upto (length lt - 1))
chaieb@29841
   676
   in linear_eqs(eqs,les,lts)
chaieb@29841
   677
   end 
chaieb@29841
   678
  
chaieb@29841
   679
  fun lin_of_hol ct = 
chaieb@29841
   680
   if ct aconvc @{cterm "0::real"} then Ctermfunc.undefined
chaieb@29841
   681
   else if not (is_comb ct) then Ctermfunc.onefunc (ct, Rat.one)
chaieb@29841
   682
   else if is_ratconst ct then Ctermfunc.onefunc (one_tm, dest_ratconst ct)
chaieb@29841
   683
   else
chaieb@29841
   684
    let val (lop,r) = Thm.dest_comb ct 
chaieb@29841
   685
    in if not (is_comb lop) then Ctermfunc.onefunc (ct, Rat.one)
chaieb@29841
   686
       else
chaieb@29841
   687
        let val (opr,l) = Thm.dest_comb lop 
chaieb@29841
   688
        in if opr aconvc @{cterm "op + :: real =>_"} 
chaieb@29841
   689
           then linear_add (lin_of_hol l) (lin_of_hol r)
chaieb@29841
   690
           else if opr aconvc @{cterm "op * :: real =>_"} 
chaieb@29841
   691
                   andalso is_ratconst l then Ctermfunc.onefunc (r, dest_ratconst l)
chaieb@29841
   692
           else Ctermfunc.onefunc (ct, Rat.one)
chaieb@29841
   693
        end
chaieb@29841
   694
    end
chaieb@29841
   695
chaieb@29841
   696
  fun is_alien ct = case term_of ct of 
chaieb@29841
   697
   Const(@{const_name "real"}, _)$ n => 
chaieb@29841
   698
     if can HOLogic.dest_number n then false else true
chaieb@29841
   699
  | _ => false
chaieb@29841
   700
 open Thm
chaieb@29841
   701
in 
chaieb@29841
   702
fun real_linear_prover translator (eq,le,lt) = 
chaieb@29841
   703
 let 
chaieb@29841
   704
  val lhs = lin_of_hol o dest_arg1 o dest_arg o cprop_of
chaieb@29841
   705
  val rhs = lin_of_hol o dest_arg o dest_arg o cprop_of
chaieb@29841
   706
  val eq_pols = map lhs eq
chaieb@29841
   707
  val le_pols = map rhs le
chaieb@29841
   708
  val lt_pols = map rhs lt 
chaieb@29841
   709
  val aliens =  filter is_alien
chaieb@29841
   710
      (fold_rev (curry (gen_union (op aconvc)) o Ctermfunc.dom) 
chaieb@29841
   711
          (eq_pols @ le_pols @ lt_pols) [])
chaieb@29841
   712
  val le_pols' = le_pols @ map (fn v => Ctermfunc.onefunc (v,Rat.one)) aliens
chaieb@29841
   713
  val (_,proof) = linear_prover (eq_pols,le_pols',lt_pols)
chaieb@29841
   714
  val le' = le @ map (fn a => instantiate' [] [SOME (dest_arg a)] @{thm real_of_nat_ge_zero}) aliens 
chaieb@29841
   715
 in (translator (eq,le',lt) proof) : thm
chaieb@29841
   716
 end
chaieb@29841
   717
end;
chaieb@29841
   718
chaieb@29841
   719
(* A less general generic arithmetic prover dealing with abs,max and min*)
chaieb@29841
   720
chaieb@29841
   721
local
chaieb@29841
   722
 val absmaxmin_elim_ss1 = HOL_basic_ss addsimps real_abs_thms1
chaieb@29841
   723
 fun absmaxmin_elim_conv1 ctxt = 
chaieb@29841
   724
    Simplifier.rewrite (Simplifier.context ctxt absmaxmin_elim_ss1)
chaieb@29841
   725
chaieb@29841
   726
 val absmaxmin_elim_conv2 =
chaieb@29841
   727
  let 
chaieb@29841
   728
   val pth_abs = instantiate' [SOME @{ctyp real}] [] abs_split'
chaieb@29841
   729
   val pth_max = instantiate' [SOME @{ctyp real}] [] max_split
chaieb@29841
   730
   val pth_min = instantiate' [SOME @{ctyp real}] [] min_split
chaieb@29841
   731
   val abs_tm = @{cterm "abs :: real => _"}
chaieb@29841
   732
   val p_tm = @{cpat "?P :: real => bool"}
chaieb@29841
   733
   val x_tm = @{cpat "?x :: real"}
chaieb@29841
   734
   val y_tm = @{cpat "?y::real"}
chaieb@29841
   735
   val is_max = is_binop @{cterm "max :: real => _"}
chaieb@29841
   736
   val is_min = is_binop @{cterm "min :: real => _"} 
chaieb@29841
   737
   fun is_abs t = is_comb t andalso dest_fun t aconvc abs_tm
chaieb@29841
   738
   fun eliminate_construct p c tm =
chaieb@29841
   739
    let 
chaieb@29841
   740
     val t = find_cterm p tm
chaieb@29841
   741
     val th0 = (symmetric o beta_conversion false) (capply (cabs t tm) t)
chaieb@29841
   742
     val (p,ax) = (dest_comb o Thm.rhs_of) th0
chaieb@29841
   743
    in fconv_rule(arg_conv(binop_conv (arg_conv (beta_conversion false))))
chaieb@29841
   744
               (transitive th0 (c p ax))
chaieb@29841
   745
   end
chaieb@29841
   746
chaieb@29841
   747
   val elim_abs = eliminate_construct is_abs
chaieb@29841
   748
    (fn p => fn ax => 
chaieb@29841
   749
       instantiate ([], [(p_tm,p), (x_tm, dest_arg ax)]) pth_abs)
chaieb@29841
   750
   val elim_max = eliminate_construct is_max
chaieb@29841
   751
    (fn p => fn ax => 
chaieb@29841
   752
      let val (ax,y) = dest_comb ax 
chaieb@29841
   753
      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
chaieb@29841
   754
      pth_max end)
chaieb@29841
   755
   val elim_min = eliminate_construct is_min
chaieb@29841
   756
    (fn p => fn ax => 
chaieb@29841
   757
      let val (ax,y) = dest_comb ax 
chaieb@29841
   758
      in  instantiate ([], [(p_tm,p), (x_tm, dest_arg ax), (y_tm,y)]) 
chaieb@29841
   759
      pth_min end)
chaieb@29841
   760
   in first_conv [elim_abs, elim_max, elim_min, all_conv]
chaieb@29841
   761
  end;
chaieb@29841
   762
in fun gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,prover) =
chaieb@29841
   763
        gen_gen_real_arith ctxt (mkconst,eq,ge,gt,norm,neg,add,mul,
chaieb@29841
   764
                       absmaxmin_elim_conv1 ctxt,absmaxmin_elim_conv2,prover)
chaieb@29841
   765
end;
chaieb@29841
   766
chaieb@29841
   767
(* An instance for reals*) 
chaieb@29841
   768
chaieb@29841
   769
fun gen_prover_real_arith ctxt prover = 
chaieb@29841
   770
 let
chaieb@29841
   771
  fun simple_cterm_ord t u = TermOrd.term_ord (term_of t, term_of u) = LESS
chaieb@29841
   772
  val {add,mul,neg,pow,sub,main} = 
chaieb@29841
   773
     Normalizer.semiring_normalizers_ord_wrapper ctxt
chaieb@29841
   774
      (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) 
chaieb@29841
   775
     simple_cterm_ord
chaieb@29841
   776
in gen_real_arith ctxt
chaieb@29841
   777
   (cterm_of_rat, field_comp_conv, field_comp_conv,field_comp_conv,
chaieb@29841
   778
    main,neg,add,mul, prover)
chaieb@29841
   779
end;
chaieb@29841
   780
chaieb@29841
   781
fun real_arith ctxt = gen_prover_real_arith ctxt real_linear_prover;
chaieb@29841
   782
end
chaieb@29841
   783
chaieb@29841
   784
  (* Now the norm procedure for euclidean spaces *)
chaieb@29841
   785
chaieb@29841
   786
chaieb@29841
   787
signature NORM_ARITH = 
chaieb@29841
   788
sig
chaieb@29841
   789
 val norm_arith : Proof.context -> conv
chaieb@29841
   790
 val norm_arith_tac : Proof.context -> int -> tactic
chaieb@29841
   791
end
chaieb@29841
   792
chaieb@29841
   793
structure NormArith : NORM_ARITH = 
chaieb@29841
   794
struct
chaieb@29841
   795
chaieb@29841
   796
 open Conv Thm Conv2;
chaieb@29841
   797
 val bool_eq = op = : bool *bool -> bool
chaieb@29841
   798
 fun dest_ratconst t = case term_of t of
chaieb@29841
   799
   Const(@{const_name divide}, _)$a$b => Rat.rat_of_quotient(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
chaieb@29841
   800
 | _ => Rat.rat_of_int (HOLogic.dest_number (term_of t) |> snd)
chaieb@29841
   801
 fun is_ratconst t = can dest_ratconst t
chaieb@29841
   802
 fun augment_norm b t acc = case term_of t of 
chaieb@29841
   803
     Const(@{const_name norm}, _) $ _ => insert (eq_pair bool_eq (op aconvc)) (b,dest_arg t) acc
chaieb@29841
   804
   | _ => acc
chaieb@29841
   805
 fun find_normedterms t acc = case term_of t of
chaieb@29841
   806
    @{term "op + :: real => _"}$_$_ =>
chaieb@29841
   807
            find_normedterms (dest_arg1 t) (find_normedterms (dest_arg t) acc)
chaieb@29841
   808
      | @{term "op * :: real => _"}$_$n =>
chaieb@29841
   809
            if not (is_ratconst (dest_arg1 t)) then acc else
chaieb@29841
   810
            augment_norm (dest_ratconst (dest_arg1 t) >=/ Rat.zero) 
chaieb@29841
   811
                      (dest_arg t) acc
chaieb@29841
   812
      | _ => augment_norm true t acc 
chaieb@29841
   813
chaieb@29841
   814
 val cterm_lincomb_neg = Ctermfunc.mapf Rat.neg
chaieb@29841
   815
 fun cterm_lincomb_cmul c t = 
chaieb@29841
   816
    if c =/ Rat.zero then Ctermfunc.undefined else Ctermfunc.mapf (fn x => x */ c) t
chaieb@29841
   817
 fun cterm_lincomb_add l r = Ctermfunc.combine (curry op +/) (fn x => x =/ Rat.zero) l r
chaieb@29841
   818
 fun cterm_lincomb_sub l r = cterm_lincomb_add l (cterm_lincomb_neg r)
chaieb@29841
   819
 fun cterm_lincomb_eq l r = Ctermfunc.is_undefined (cterm_lincomb_sub l r)
chaieb@29841
   820
chaieb@29841
   821
 val int_lincomb_neg = Intfunc.mapf Rat.neg
chaieb@29841
   822
 fun int_lincomb_cmul c t = 
chaieb@29841
   823
    if c =/ Rat.zero then Intfunc.undefined else Intfunc.mapf (fn x => x */ c) t
chaieb@29841
   824
 fun int_lincomb_add l r = Intfunc.combine (curry op +/) (fn x => x =/ Rat.zero) l r
chaieb@29841
   825
 fun int_lincomb_sub l r = int_lincomb_add l (int_lincomb_neg r)
chaieb@29841
   826
 fun int_lincomb_eq l r = Intfunc.is_undefined (int_lincomb_sub l r)
chaieb@29841
   827
chaieb@29841
   828
fun vector_lincomb t = case term_of t of 
chaieb@29841
   829
   Const(@{const_name plus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_])) $ _ $ _ =>
chaieb@29841
   830
    cterm_lincomb_add (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
chaieb@29841
   831
 | Const(@{const_name minus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_])) $ _ $ _ =>
chaieb@29841
   832
    cterm_lincomb_sub (vector_lincomb (dest_arg1 t)) (vector_lincomb (dest_arg t))
chaieb@29841
   833
 | Const(@{const_name vector_scalar_mult},Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$_$_ =>
chaieb@29841
   834
    cterm_lincomb_cmul (dest_ratconst (dest_arg1 t)) (vector_lincomb (dest_arg t))
chaieb@29841
   835
 | Const(@{const_name uminus},Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$_ =>
chaieb@29841
   836
     cterm_lincomb_neg (vector_lincomb (dest_arg t))
chaieb@29841
   837
 | Const(@{const_name vec},_)$_ => 
chaieb@29841
   838
   let 
chaieb@29841
   839
     val b = ((snd o HOLogic.dest_number o term_of o dest_arg) t = 0 
chaieb@29841
   840
               handle TERM _=> false)
chaieb@29841
   841
   in if b then Ctermfunc.onefunc (t,Rat.one)
chaieb@29841
   842
      else Ctermfunc.undefined
chaieb@29841
   843
   end
chaieb@29841
   844
 | _ => Ctermfunc.onefunc (t,Rat.one)
chaieb@29841
   845
chaieb@29841
   846
 fun vector_lincombs ts =
chaieb@29841
   847
  fold_rev 
chaieb@29841
   848
   (fn t => fn fns => case AList.lookup (op aconvc) fns t of
chaieb@29841
   849
     NONE => 
chaieb@29841
   850
       let val f = vector_lincomb t 
chaieb@29841
   851
       in case find_first (fn (_,f') => cterm_lincomb_eq f f') fns of
chaieb@29841
   852
           SOME (_,f') => (t,f') :: fns
chaieb@29841
   853
         | NONE => (t,f) :: fns 
chaieb@29841
   854
       end
chaieb@29841
   855
   | SOME _ => fns) ts []
chaieb@29841
   856
chaieb@29841
   857
fun replacenegnorms cv t = case term_of t of 
chaieb@29841
   858
  @{term "op + :: real => _"}$_$_ => binop_conv (replacenegnorms cv) t
chaieb@29841
   859
| @{term "op * :: real => _"}$_$_ => 
chaieb@29841
   860
    if dest_ratconst (dest_arg1 t) </ Rat.zero then arg_conv cv t else reflexive t
chaieb@29841
   861
| _ => reflexive t
chaieb@29841
   862
fun flip v eq = 
chaieb@29841
   863
  if Ctermfunc.defined eq v 
chaieb@29841
   864
  then Ctermfunc.update (v, Rat.neg (Ctermfunc.apply eq v)) eq else eq
chaieb@29841
   865
fun allsubsets s = case s of 
chaieb@29841
   866
  [] => [[]]
chaieb@29841
   867
|(a::t) => let val res = allsubsets t in
chaieb@29841
   868
               map (cons a) res @ res end
chaieb@29841
   869
fun evaluate env lin =
chaieb@29841
   870
 Intfunc.fold (fn (x,c) => fn s => s +/ c */ (Intfunc.apply env x)) 
chaieb@29841
   871
   lin Rat.zero
chaieb@29841
   872
chaieb@29841
   873
fun solve (vs,eqs) = case (vs,eqs) of
chaieb@29841
   874
  ([],[]) => SOME (Intfunc.onefunc (0,Rat.one))
chaieb@29841
   875
 |(_,eq::oeqs) => 
chaieb@29841
   876
   (case vs inter (Intfunc.dom eq) of
chaieb@29841
   877
     [] => NONE
chaieb@29841
   878
    | v::_ => 
chaieb@29841
   879
       if Intfunc.defined eq v 
chaieb@29841
   880
       then 
chaieb@29841
   881
        let 
chaieb@29841
   882
         val c = Intfunc.apply eq v
chaieb@29841
   883
         val vdef = int_lincomb_cmul (Rat.neg (Rat.inv c)) eq
chaieb@29841
   884
         fun eliminate eqn = if not (Intfunc.defined eqn v) then eqn 
chaieb@29841
   885
                             else int_lincomb_add (int_lincomb_cmul (Intfunc.apply eqn v) vdef) eqn
chaieb@29841
   886
        in (case solve (vs \ v,map eliminate oeqs) of
chaieb@29841
   887
            NONE => NONE
chaieb@29841
   888
          | SOME soln => SOME (Intfunc.update (v, evaluate soln (Intfunc.undefine v vdef)) soln))
chaieb@29841
   889
        end
chaieb@29841
   890
       else NONE)
chaieb@29841
   891
chaieb@29841
   892
fun combinations k l = if k = 0 then [[]] else
chaieb@29841
   893
 case l of 
chaieb@29841
   894
  [] => []
chaieb@29841
   895
| h::t => map (cons h) (combinations (k - 1) t) @ combinations k t
chaieb@29841
   896
chaieb@29841
   897
chaieb@29841
   898
fun forall2 p l1 l2 = case (l1,l2) of 
chaieb@29841
   899
   ([],[]) => true
chaieb@29841
   900
 | (h1::t1,h2::t2) => p h1 h2 andalso forall2 p t1 t2
chaieb@29841
   901
 | _ => false;
chaieb@29841
   902
chaieb@29841
   903
chaieb@29841
   904
fun vertices vs eqs =
chaieb@29841
   905
 let 
chaieb@29841
   906
  fun vertex cmb = case solve(vs,cmb) of
chaieb@29841
   907
    NONE => NONE
chaieb@29841
   908
   | SOME soln => SOME (map (fn v => Intfunc.tryapplyd soln v Rat.zero) vs)
chaieb@29841
   909
  val rawvs = map_filter vertex (combinations (length vs) eqs)
chaieb@29841
   910
  val unset = filter (forall (fn c => c >=/ Rat.zero)) rawvs 
chaieb@29841
   911
 in fold_rev (insert (uncurry (forall2 (curry op =/)))) unset [] 
chaieb@29841
   912
 end 
chaieb@29841
   913
chaieb@29841
   914
fun subsumes l m = forall2 (fn x => fn y => Rat.abs x <=/ Rat.abs y) l m 
chaieb@29841
   915
chaieb@29841
   916
fun subsume todo dun = case todo of
chaieb@29841
   917
 [] => dun
chaieb@29841
   918
|v::ovs => 
chaieb@29841
   919
   let val dun' = if exists (fn w => subsumes w v) dun then dun
chaieb@29841
   920
                  else v::(filter (fn w => not(subsumes v w)) dun) 
chaieb@29841
   921
   in subsume ovs dun' 
chaieb@29841
   922
   end;
chaieb@29841
   923
chaieb@29841
   924
fun match_mp PQ P = P RS PQ;
chaieb@29841
   925
chaieb@29841
   926
fun cterm_of_rat x = 
chaieb@29841
   927
let val (a, b) = Rat.quotient_of_rat x
chaieb@29841
   928
in 
chaieb@29841
   929
 if b = 1 then Numeral.mk_cnumber @{ctyp "real"} a
chaieb@29841
   930
  else Thm.capply (Thm.capply @{cterm "op / :: real => _"} 
chaieb@29841
   931
                   (Numeral.mk_cnumber @{ctyp "real"} a))
chaieb@29841
   932
        (Numeral.mk_cnumber @{ctyp "real"} b)
chaieb@29841
   933
end;
chaieb@29841
   934
chaieb@29841
   935
fun norm_cmul_rule c th = instantiate' [] [SOME (cterm_of_rat c)] (th RS @{thm norm_cmul_rule_thm});
chaieb@29841
   936
chaieb@29841
   937
fun norm_add_rule th1 th2 = [th1, th2] MRS @{thm norm_add_rule_thm};
chaieb@29841
   938
chaieb@29841
   939
  (* I think here the static context should be sufficient!! *)
chaieb@29841
   940
fun inequality_canon_rule ctxt = 
chaieb@29841
   941
 let 
chaieb@29841
   942
  (* FIXME : Should be computed statically!! *)
chaieb@29841
   943
  val real_poly_conv = 
chaieb@29841
   944
    Normalizer.semiring_normalize_wrapper ctxt
chaieb@29841
   945
     (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"}))
chaieb@29841
   946
 in fconv_rule (arg_conv ((rewr_conv @{thm ge_iff_diff_ge_0}) then_conv arg_conv (field_comp_conv then_conv real_poly_conv)))
chaieb@29841
   947
end;
chaieb@29841
   948
chaieb@29841
   949
 fun absc cv ct = case term_of ct of 
chaieb@29841
   950
 Abs (v,_, _) => 
chaieb@29841
   951
  let val (x,t) = Thm.dest_abs (SOME v) ct
chaieb@29841
   952
  in Thm.abstract_rule ((fst o dest_Free o term_of) x) x (cv t)
chaieb@29841
   953
  end
chaieb@29841
   954
 | _ => all_conv ct;
chaieb@29841
   955
chaieb@29841
   956
fun sub_conv cv ct = (comb_conv cv else_conv absc cv) ct;
chaieb@29841
   957
fun botc1 conv ct = 
chaieb@29841
   958
  ((sub_conv (botc1 conv)) then_conv (conv else_conv all_conv)) ct;
chaieb@29841
   959
chaieb@29841
   960
 fun rewrs_conv eqs ct = first_conv (map rewr_conv eqs) ct;
chaieb@29841
   961
 val apply_pth1 = rewr_conv @{thm pth_1};
chaieb@29841
   962
 val apply_pth2 = rewr_conv @{thm pth_2};
chaieb@29841
   963
 val apply_pth3 = rewr_conv @{thm pth_3};
chaieb@29841
   964
 val apply_pth4 = rewrs_conv @{thms pth_4};
chaieb@29841
   965
 val apply_pth5 = rewr_conv @{thm pth_5};
chaieb@29841
   966
 val apply_pth6 = rewr_conv @{thm pth_6};
chaieb@29841
   967
 val apply_pth7 = rewrs_conv @{thms pth_7};
chaieb@29841
   968
 val apply_pth8 = rewr_conv @{thm pth_8} then_conv arg1_conv field_comp_conv then_conv (try_conv (rewr_conv (mk_meta_eq @{thm vector_smult_lzero})));
chaieb@29841
   969
 val apply_pth9 = rewrs_conv @{thms pth_9} then_conv arg1_conv (arg1_conv field_comp_conv);
chaieb@29841
   970
 val apply_ptha = rewr_conv @{thm pth_a};
chaieb@29841
   971
 val apply_pthb = rewrs_conv @{thms pth_b};
chaieb@29841
   972
 val apply_pthc = rewrs_conv @{thms pth_c};
chaieb@29841
   973
 val apply_pthd = try_conv (rewr_conv @{thm pth_d});
chaieb@29841
   974
chaieb@29841
   975
fun headvector t = case t of 
chaieb@29841
   976
  Const(@{const_name plus}, Type("fun",[Type("Finite_Cartesian_Product.^",_),_]))$
chaieb@29841
   977
   (Const(@{const_name vector_scalar_mult}, _)$l$v)$r => v
chaieb@29841
   978
 | Const(@{const_name vector_scalar_mult}, _)$l$v => v
chaieb@29841
   979
 | _ => error "headvector: non-canonical term"
chaieb@29841
   980
chaieb@29841
   981
fun vector_cmul_conv ct =
chaieb@29841
   982
   ((apply_pth5 then_conv arg1_conv field_comp_conv) else_conv
chaieb@29841
   983
    (apply_pth6 then_conv binop_conv vector_cmul_conv)) ct
chaieb@29841
   984
chaieb@29841
   985
fun vector_add_conv ct = apply_pth7 ct 
chaieb@29841
   986
 handle CTERM _ => 
chaieb@29841
   987
  (apply_pth8 ct 
chaieb@29841
   988
   handle CTERM _ => 
chaieb@29841
   989
    (case term_of ct of 
chaieb@29841
   990
     Const(@{const_name plus},_)$lt$rt =>
chaieb@29841
   991
      let 
chaieb@29841
   992
       val l = headvector lt 
chaieb@29841
   993
       val r = headvector rt
chaieb@29841
   994
      in (case TermOrd.fast_term_ord (l,r) of
chaieb@29841
   995
         LESS => (apply_pthb then_conv arg_conv vector_add_conv 
chaieb@29841
   996
                  then_conv apply_pthd) ct
chaieb@29841
   997
        | GREATER => (apply_pthc then_conv arg_conv vector_add_conv 
chaieb@29841
   998
                     then_conv apply_pthd) ct 
chaieb@29841
   999
        | EQUAL => (apply_pth9 then_conv 
chaieb@29841
  1000
                ((apply_ptha then_conv vector_add_conv) else_conv 
chaieb@29841
  1001
              arg_conv vector_add_conv then_conv apply_pthd)) ct)
chaieb@29841
  1002
      end
chaieb@29841
  1003
     | _ => reflexive ct))
chaieb@29841
  1004
chaieb@29841
  1005
fun vector_canon_conv ct = case term_of ct of
chaieb@29841
  1006
 Const(@{const_name plus},_)$_$_ =>
chaieb@29841
  1007
  let 
chaieb@29841
  1008
   val ((p,l),r) = Thm.dest_comb ct |>> Thm.dest_comb
chaieb@29841
  1009
   val lth = vector_canon_conv l 
chaieb@29841
  1010
   val rth = vector_canon_conv r
chaieb@29841
  1011
   val th = Drule.binop_cong_rule p lth rth 
chaieb@29841
  1012
  in fconv_rule (arg_conv vector_add_conv) th end
chaieb@29841
  1013
chaieb@29841
  1014
| Const(@{const_name vector_scalar_mult}, _)$_$_ =>
chaieb@29841
  1015
  let 
chaieb@29841
  1016
   val (p,r) = Thm.dest_comb ct
chaieb@29841
  1017
   val rth = Drule.arg_cong_rule p (vector_canon_conv r) 
chaieb@29841
  1018
  in fconv_rule (arg_conv (apply_pth4 else_conv vector_cmul_conv)) rth
chaieb@29841
  1019
  end
chaieb@29841
  1020
chaieb@29841
  1021
| Const(@{const_name minus},_)$_$_ => (apply_pth2 then_conv vector_canon_conv) ct
chaieb@29841
  1022
chaieb@29841
  1023
| Const(@{const_name uminus},_)$_ => (apply_pth3 then_conv vector_canon_conv) ct
chaieb@29841
  1024
chaieb@29841
  1025
| Const(@{const_name vec},_)$n => 
chaieb@29841
  1026
  let val n = Thm.dest_arg ct
chaieb@29841
  1027
  in if is_ratconst n andalso not (dest_ratconst n =/ Rat.zero) 
chaieb@29841
  1028
     then reflexive ct else apply_pth1 ct
chaieb@29841
  1029
  end
chaieb@29841
  1030
chaieb@29841
  1031
| _ => apply_pth1 ct
chaieb@29841
  1032
chaieb@29841
  1033
fun norm_canon_conv ct = case term_of ct of
chaieb@29841
  1034
  Const(@{const_name norm},_)$_ => arg_conv vector_canon_conv ct
chaieb@29841
  1035
 | _ => raise CTERM ("norm_canon_conv", [ct])
chaieb@29841
  1036
chaieb@29841
  1037
fun fold_rev2 f [] [] z = z
chaieb@29841
  1038
 | fold_rev2 f (x::xs) (y::ys) z = f x y (fold_rev2 f xs ys z)
chaieb@29841
  1039
 | fold_rev2 f _ _ _ = raise UnequalLengths;
chaieb@29841
  1040
chaieb@29841
  1041
fun int_flip v eq = 
chaieb@29841
  1042
  if Intfunc.defined eq v 
chaieb@29841
  1043
  then Intfunc.update (v, Rat.neg (Intfunc.apply eq v)) eq else eq;
chaieb@29841
  1044
chaieb@29841
  1045
local
chaieb@29841
  1046
 val pth_zero = @{thm "Vectors.norm_0"}
chaieb@29841
  1047
 val tv_n = (hd o tl o dest_ctyp o ctyp_of_term o dest_arg o dest_arg1 o dest_arg o cprop_of)
chaieb@29841
  1048
             pth_zero
chaieb@29841
  1049
 val concl = dest_arg o cprop_of 
chaieb@29841
  1050
 fun real_vector_combo_prover ctxt translator (nubs,ges,gts) = 
chaieb@29841
  1051
  let 
chaieb@29841
  1052
   (* FIXME: Should be computed statically!!*)
chaieb@29841
  1053
   val real_poly_conv = 
chaieb@29841
  1054
      Normalizer.semiring_normalize_wrapper ctxt
chaieb@29841
  1055
       (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"}))
chaieb@29841
  1056
   val sources = map (dest_arg o dest_arg1 o concl) nubs
chaieb@29841
  1057
   val rawdests = fold_rev (find_normedterms o dest_arg o concl) (ges @ gts) [] 
chaieb@29841
  1058
   val _ = if not (forall fst rawdests) then error "real_vector_combo_prover: Sanity check" 
chaieb@29841
  1059
           else ()
chaieb@29841
  1060
   val dests = distinct (op aconvc) (map snd rawdests)
chaieb@29841
  1061
   val srcfuns = map vector_lincomb sources
chaieb@29841
  1062
   val destfuns = map vector_lincomb dests 
chaieb@29841
  1063
   val vvs = fold_rev (curry (gen_union op aconvc) o Ctermfunc.dom) (srcfuns @ destfuns) []
chaieb@29841
  1064
   val n = length srcfuns
chaieb@29841
  1065
   val nvs = 1 upto n
chaieb@29841
  1066
   val srccombs = srcfuns ~~ nvs
chaieb@29841
  1067
   fun consider d =
chaieb@29841
  1068
    let 
chaieb@29841
  1069
     fun coefficients x =
chaieb@29841
  1070
      let 
chaieb@29841
  1071
       val inp = if Ctermfunc.defined d x then Intfunc.onefunc (0, Rat.neg(Ctermfunc.apply d x))
chaieb@29841
  1072
                      else Intfunc.undefined 
chaieb@29841
  1073
      in fold_rev (fn (f,v) => fn g => if Ctermfunc.defined f x then Intfunc.update (v, Ctermfunc.apply f x) g else g) srccombs inp 
chaieb@29841
  1074
      end
chaieb@29841
  1075
     val equations = map coefficients vvs
chaieb@29841
  1076
     val inequalities = map (fn n => Intfunc.onefunc (n,Rat.one)) nvs
chaieb@29841
  1077
     fun plausiblevertices f =
chaieb@29841
  1078
      let 
chaieb@29841
  1079
       val flippedequations = map (fold_rev int_flip f) equations
chaieb@29841
  1080
       val constraints = flippedequations @ inequalities
chaieb@29841
  1081
       val rawverts = vertices nvs constraints
chaieb@29841
  1082
       fun check_solution v =
chaieb@29841
  1083
        let 
chaieb@29841
  1084
          val f = fold_rev2 (curry Intfunc.update) nvs v (Intfunc.onefunc (0, Rat.one))
chaieb@29841
  1085
        in forall (fn e => evaluate f e =/ Rat.zero) flippedequations
chaieb@29841
  1086
        end
chaieb@29841
  1087
       val goodverts = filter check_solution rawverts
chaieb@29841
  1088
       val signfixups = map (fn n => if n mem_int  f then ~1 else 1) nvs 
chaieb@29841
  1089
      in map (map2 (fn s => fn c => Rat.rat_of_int s */ c) signfixups) goodverts
chaieb@29841
  1090
      end
chaieb@29841
  1091
     val allverts = fold_rev append (map plausiblevertices (allsubsets nvs)) [] 
chaieb@29841
  1092
    in subsume allverts []
chaieb@29841
  1093
    end
chaieb@29841
  1094
   fun compute_ineq v =
chaieb@29841
  1095
    let 
chaieb@29841
  1096
     val ths = map_filter (fn (v,t) => if v =/ Rat.zero then NONE 
chaieb@29841
  1097
                                     else SOME(norm_cmul_rule v t))
chaieb@29841
  1098
                            (v ~~ nubs) 
chaieb@29841
  1099
    in inequality_canon_rule ctxt (end_itlist norm_add_rule ths)
chaieb@29841
  1100
    end
chaieb@29841
  1101
   val ges' = map_filter (try compute_ineq) (fold_rev (append o consider) destfuns []) @
chaieb@29841
  1102
                 map (inequality_canon_rule ctxt) nubs @ ges
chaieb@29841
  1103
   val zerodests = filter
chaieb@29841
  1104
        (fn t => null (Ctermfunc.dom (vector_lincomb t))) (map snd rawdests)
chaieb@29841
  1105
chaieb@29841
  1106
  in RealArith.real_linear_prover translator
chaieb@29841
  1107
        (map (fn t => instantiate ([(tv_n,(hd o tl o dest_ctyp o ctyp_of_term) t)],[]) pth_zero)
chaieb@29841
  1108
            zerodests,
chaieb@29841
  1109
        map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv
chaieb@29841
  1110
                       arg_conv (arg_conv real_poly_conv))) ges',
chaieb@29841
  1111
        map (fconv_rule (once_depth_conv (norm_canon_conv) then_conv 
chaieb@29841
  1112
                       arg_conv (arg_conv real_poly_conv))) gts)
chaieb@29841
  1113
  end
chaieb@29841
  1114
in val real_vector_combo_prover = real_vector_combo_prover
chaieb@29841
  1115
end;
chaieb@29841
  1116
chaieb@29841
  1117
local
chaieb@29841
  1118
 val pth = @{thm norm_imp_pos_and_ge}
chaieb@29841
  1119
 val norm_mp = match_mp pth
chaieb@29841
  1120
 val concl = dest_arg o cprop_of
chaieb@29841
  1121
 fun conjunct1 th = th RS @{thm conjunct1}
chaieb@29841
  1122
 fun conjunct2 th = th RS @{thm conjunct2}
chaieb@29841
  1123
 fun C f x y = f y x
chaieb@29841
  1124
fun real_vector_ineq_prover ctxt translator (ges,gts) = 
chaieb@29841
  1125
 let 
chaieb@29841
  1126
(*   val _ = error "real_vector_ineq_prover: pause" *)
chaieb@29841
  1127
  val ntms = fold_rev find_normedterms (map (dest_arg o concl) (ges @ gts)) []
chaieb@29841
  1128
  val lctab = vector_lincombs (map snd (filter (not o fst) ntms))
chaieb@29841
  1129
  val (fxns, ctxt') = Variable.variant_fixes (replicate (length lctab) "x") ctxt
chaieb@29841
  1130
  fun mk_norm t = capply (instantiate_cterm' [SOME (ctyp_of_term t)] [] @{cpat "norm :: (?'a :: norm) => real"}) t
chaieb@29841
  1131
  fun mk_equals l r = capply (capply (instantiate_cterm' [SOME (ctyp_of_term l)] [] @{cpat "op == :: ?'a =>_"}) l) r
chaieb@29841
  1132
  val asl = map2 (fn (t,_) => fn n => assume (mk_equals (mk_norm t) (cterm_of (ProofContext.theory_of ctxt') (Free(n,@{typ real}))))) lctab fxns
chaieb@29841
  1133
  val replace_conv = try_conv (rewrs_conv asl)
chaieb@29841
  1134
  val replace_rule = fconv_rule (funpow 2 arg_conv (replacenegnorms replace_conv))
chaieb@29841
  1135
  val ges' =
chaieb@29841
  1136
       fold_rev (fn th => fn ths => conjunct1(norm_mp th)::ths)
chaieb@29841
  1137
              asl (map replace_rule ges)
chaieb@29841
  1138
  val gts' = map replace_rule gts
chaieb@29841
  1139
  val nubs = map (conjunct2 o norm_mp) asl
chaieb@29841
  1140
  val th1 = real_vector_combo_prover ctxt' translator (nubs,ges',gts')
chaieb@29841
  1141
  val shs = filter (member (fn (t,th) => t aconvc cprop_of th) asl) (#hyps (crep_thm th1)) 
chaieb@29841
  1142
  val th11 = hd (Variable.export ctxt' ctxt [fold implies_intr shs th1])
chaieb@29841
  1143
  val cps = map (swap o dest_equals) (cprems_of th11)
chaieb@29841
  1144
  val th12 = instantiate ([], cps) th11
chaieb@29841
  1145
  val th13 = fold (C implies_elim) (map (reflexive o snd) cps) th12;
chaieb@29841
  1146
 in hd (Variable.export ctxt' ctxt [th13])
chaieb@29841
  1147
 end 
chaieb@29841
  1148
in val real_vector_ineq_prover = real_vector_ineq_prover
chaieb@29841
  1149
end;
chaieb@29841
  1150
chaieb@29841
  1151
local
chaieb@29841
  1152
 val rawrule = fconv_rule (arg_conv (rewr_conv @{thm real_eq_0_iff_le_ge_0}))
chaieb@29841
  1153
 fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
chaieb@29841
  1154
 fun simple_cterm_ord t u = TermOrd.term_ord (term_of t, term_of u) = LESS;
chaieb@29841
  1155
  (* FIXME: Lookup in the context every time!!! Fix this !!!*)
chaieb@29841
  1156
 fun splitequation ctxt th acc =
chaieb@29841
  1157
  let 
chaieb@29841
  1158
   val real_poly_neg_conv = #neg
chaieb@29841
  1159
       (Normalizer.semiring_normalizers_ord_wrapper ctxt
chaieb@29841
  1160
        (valOf (NormalizerData.match ctxt @{cterm "(0::real) + 1"})) simple_cterm_ord)
chaieb@29841
  1161
   val (th1,th2) = conj_pair(rawrule th)
chaieb@29841
  1162
  in th1::fconv_rule (arg_conv (arg_conv real_poly_neg_conv)) th2::acc
chaieb@29841
  1163
  end
chaieb@29841
  1164
in fun real_vector_prover ctxt translator (eqs,ges,gts) =
chaieb@29841
  1165
     real_vector_ineq_prover ctxt translator
chaieb@29841
  1166
         (fold_rev (splitequation ctxt) eqs ges,gts)
chaieb@29841
  1167
end;
chaieb@29841
  1168
chaieb@29841
  1169
  fun init_conv ctxt = 
chaieb@29841
  1170
   Simplifier.rewrite (Simplifier.context ctxt 
chaieb@29841
  1171
     (HOL_basic_ss addsimps ([@{thm vec_0}, @{thm vec_1}, @{thm dist_def}, @{thm diff_0_right}, @{thm right_minus}, @{thm diff_self}, @{thm norm_0}] @ @{thms arithmetic_simps} @ @{thms norm_pths})))
chaieb@29841
  1172
   then_conv field_comp_conv 
chaieb@29841
  1173
   then_conv nnf_conv
chaieb@29841
  1174
chaieb@29841
  1175
 fun pure ctxt = RealArith.gen_prover_real_arith ctxt (real_vector_prover ctxt);
chaieb@29841
  1176
 fun norm_arith ctxt ct = 
chaieb@29841
  1177
  let 
chaieb@29841
  1178
   val ctxt' = Variable.declare_term (term_of ct) ctxt
chaieb@29841
  1179
   val th = init_conv ctxt' ct
chaieb@29841
  1180
  in equal_elim (Drule.arg_cong_rule @{cterm Trueprop} (symmetric th)) 
chaieb@29841
  1181
                (pure ctxt' (rhs_of th))
chaieb@29841
  1182
 end
chaieb@29841
  1183
chaieb@29841
  1184
 fun norm_arith_tac ctxt = 
chaieb@29841
  1185
   clarify_tac HOL_cs THEN'
chaieb@29841
  1186
   ObjectLogic.full_atomize_tac THEN'
chaieb@29841
  1187
   CSUBGOAL ( fn (p,i) => rtac (norm_arith ctxt (Thm.dest_arg p )) i);
chaieb@29841
  1188
chaieb@29841
  1189
end;