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