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