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