src/HOL/Analysis/normarith.ML
author wenzelm
Sat Jan 05 17:24:33 2019 +0100 (4 months ago)
changeset 69597 ff784d5a5bfb
parent 69064 5840724b1d71
permissions -rw-r--r--
isabelle update -u control_cartouches;
hoelzl@63627
     1
(*  Title:      HOL/Analysis/normarith.ML
wenzelm@36938
     2
    Author:     Amine Chaieb, University of Cambridge
wenzelm@36938
     3
wenzelm@36938
     4
Simple decision procedure for linear problems in Euclidean space.
chaieb@31118
     5
*)
chaieb@29841
     6
wenzelm@36938
     7
signature NORM_ARITH =
chaieb@29841
     8
sig
chaieb@29841
     9
 val norm_arith : Proof.context -> conv
chaieb@29841
    10
 val norm_arith_tac : Proof.context -> int -> tactic
chaieb@29841
    11
end
chaieb@29841
    12
wenzelm@36938
    13
structure NormArith : NORM_ARITH =
chaieb@29841
    14
struct
chaieb@29841
    15
Philipp@32832
    16
 open Conv;
chaieb@29841
    17
 val bool_eq = op = : bool *bool -> bool
wenzelm@59582
    18
  fun dest_ratconst t = case Thm.term_of t of
wenzelm@69597
    19
   Const(\<^const_name>\<open>divide\<close>, _)$a$b => Rat.make(HOLogic.dest_number a |> snd, HOLogic.dest_number b |> snd)
wenzelm@69597
    20
 | Const(\<^const_name>\<open>inverse\<close>, _)$a => Rat.make(1, HOLogic.dest_number a |> snd)
wenzelm@63201
    21
 | _ => Rat.of_int (HOLogic.dest_number (Thm.term_of t) |> snd)
chaieb@29841
    22
 fun is_ratconst t = can dest_ratconst t
wenzelm@59582
    23
 fun augment_norm b t acc = case Thm.term_of t of
wenzelm@69597
    24
     Const(\<^const_name>\<open>norm\<close>, _) $ _ => insert (eq_pair bool_eq (op aconvc)) (b,Thm.dest_arg t) acc
chaieb@29841
    25
   | _ => acc
wenzelm@59582
    26
 fun find_normedterms t acc = case Thm.term_of t of
wenzelm@69597
    27
    \<^term>\<open>(+) :: real => _\<close>$_$_ =>
Philipp@32832
    28
            find_normedterms (Thm.dest_arg1 t) (find_normedterms (Thm.dest_arg t) acc)
wenzelm@69597
    29
      | \<^term>\<open>(*) :: real => _\<close>$_$_ =>
Philipp@32832
    30
            if not (is_ratconst (Thm.dest_arg1 t)) then acc else
wenzelm@63205
    31
            augment_norm (dest_ratconst (Thm.dest_arg1 t) >= @0)
Philipp@32832
    32
                      (Thm.dest_arg t) acc
wenzelm@36938
    33
      | _ => augment_norm true t acc
chaieb@29841
    34
wenzelm@63211
    35
 val cterm_lincomb_neg = FuncUtil.Ctermfunc.map (K ~)
wenzelm@36938
    36
 fun cterm_lincomb_cmul c t =
wenzelm@63205
    37
    if c = @0 then FuncUtil.Ctermfunc.empty else FuncUtil.Ctermfunc.map (fn _ => fn x => x * c) t
wenzelm@63205
    38
 fun cterm_lincomb_add l r = FuncUtil.Ctermfunc.combine (curry op +) (fn x => x = @0) l r
chaieb@29841
    39
 fun cterm_lincomb_sub l r = cterm_lincomb_add l (cterm_lincomb_neg r)
Philipp@32832
    40
 fun cterm_lincomb_eq l r = FuncUtil.Ctermfunc.is_empty (cterm_lincomb_sub l r)
chaieb@29841
    41
huffman@44454
    42
(*
wenzelm@63211
    43
 val int_lincomb_neg = FuncUtil.Intfunc.map (K ~)
huffman@44454
    44
*)
wenzelm@36938
    45
 fun int_lincomb_cmul c t =
wenzelm@63205
    46
    if c = @0 then FuncUtil.Intfunc.empty else FuncUtil.Intfunc.map (fn _ => fn x => x * c) t
wenzelm@63205
    47
 fun int_lincomb_add l r = FuncUtil.Intfunc.combine (curry op +) (fn x => x = @0) l r
huffman@44454
    48
(*
chaieb@29841
    49
 fun int_lincomb_sub l r = int_lincomb_add l (int_lincomb_neg r)
Philipp@32832
    50
 fun int_lincomb_eq l r = FuncUtil.Intfunc.is_empty (int_lincomb_sub l r)
huffman@44454
    51
*)
chaieb@29841
    52
wenzelm@59582
    53
fun vector_lincomb t = case Thm.term_of t of
wenzelm@69597
    54
   Const(\<^const_name>\<open>plus\<close>, _) $ _ $ _ =>
Philipp@32832
    55
    cterm_lincomb_add (vector_lincomb (Thm.dest_arg1 t)) (vector_lincomb (Thm.dest_arg t))
wenzelm@69597
    56
 | Const(\<^const_name>\<open>minus\<close>, _) $ _ $ _ =>
Philipp@32832
    57
    cterm_lincomb_sub (vector_lincomb (Thm.dest_arg1 t)) (vector_lincomb (Thm.dest_arg t))
wenzelm@69597
    58
 | Const(\<^const_name>\<open>scaleR\<close>, _)$_$_ =>
Philipp@32832
    59
    cterm_lincomb_cmul (dest_ratconst (Thm.dest_arg1 t)) (vector_lincomb (Thm.dest_arg t))
wenzelm@69597
    60
 | Const(\<^const_name>\<open>uminus\<close>, _)$_ =>
Philipp@32832
    61
     cterm_lincomb_neg (vector_lincomb (Thm.dest_arg t))
huffman@31445
    62
(* FIXME: how should we handle numerals?
wenzelm@36938
    63
 | Const(@ {const_name vec},_)$_ =>
wenzelm@36938
    64
   let
wenzelm@36938
    65
     val b = ((snd o HOLogic.dest_number o term_of o Thm.dest_arg) t = 0
chaieb@29841
    66
               handle TERM _=> false)
wenzelm@63205
    67
   in if b then FuncUtil.Ctermfunc.onefunc (t,@1)
Philipp@32832
    68
      else FuncUtil.Ctermfunc.empty
chaieb@29841
    69
   end
huffman@31445
    70
*)
wenzelm@63205
    71
 | _ => FuncUtil.Ctermfunc.onefunc (t,@1)
chaieb@29841
    72
chaieb@29841
    73
 fun vector_lincombs ts =
wenzelm@36938
    74
  fold_rev
chaieb@29841
    75
   (fn t => fn fns => case AList.lookup (op aconvc) fns t of
wenzelm@36938
    76
     NONE =>
wenzelm@36938
    77
       let val f = vector_lincomb t
chaieb@29841
    78
       in case find_first (fn (_,f') => cterm_lincomb_eq f f') fns of
chaieb@29841
    79
           SOME (_,f') => (t,f') :: fns
wenzelm@36938
    80
         | NONE => (t,f) :: fns
chaieb@29841
    81
       end
chaieb@29841
    82
   | SOME _ => fns) ts []
chaieb@29841
    83
wenzelm@59582
    84
fun replacenegnorms cv t = case Thm.term_of t of
wenzelm@69597
    85
  \<^term>\<open>(+) :: real => _\<close>$_$_ => binop_conv (replacenegnorms cv) t
wenzelm@69597
    86
| \<^term>\<open>(*) :: real => _\<close>$_$_ =>
wenzelm@63205
    87
    if dest_ratconst (Thm.dest_arg1 t) < @0 then arg_conv cv t else Thm.reflexive t
wenzelm@36945
    88
| _ => Thm.reflexive t
huffman@44454
    89
(*
wenzelm@36938
    90
fun flip v eq =
wenzelm@36938
    91
  if FuncUtil.Ctermfunc.defined eq v
wenzelm@63211
    92
  then FuncUtil.Ctermfunc.update (v, ~ (FuncUtil.Ctermfunc.apply eq v)) eq else eq
huffman@44454
    93
*)
wenzelm@36938
    94
fun allsubsets s = case s of
chaieb@29841
    95
  [] => [[]]
chaieb@29841
    96
|(a::t) => let val res = allsubsets t in
chaieb@29841
    97
               map (cons a) res @ res end
chaieb@29841
    98
fun evaluate env lin =
wenzelm@63198
    99
 FuncUtil.Intfunc.fold (fn (x,c) => fn s => s + c * (FuncUtil.Intfunc.apply env x))
wenzelm@63205
   100
   lin @0
chaieb@29841
   101
chaieb@29841
   102
fun solve (vs,eqs) = case (vs,eqs) of
wenzelm@63205
   103
  ([],[]) => SOME (FuncUtil.Intfunc.onefunc (0,@1))
wenzelm@36938
   104
 |(_,eq::oeqs) =>
Philipp@32832
   105
   (case filter (member (op =) vs) (FuncUtil.Intfunc.dom eq) of (*FIXME use find_first here*)
chaieb@29841
   106
     [] => NONE
wenzelm@36938
   107
    | v::_ =>
wenzelm@36938
   108
       if FuncUtil.Intfunc.defined eq v
wenzelm@36938
   109
       then
wenzelm@36938
   110
        let
Philipp@32832
   111
         val c = FuncUtil.Intfunc.apply eq v
wenzelm@63211
   112
         val vdef = int_lincomb_cmul (~ (Rat.inv c)) eq
wenzelm@36938
   113
         fun eliminate eqn = if not (FuncUtil.Intfunc.defined eqn v) then eqn
Philipp@32832
   114
                             else int_lincomb_add (int_lincomb_cmul (FuncUtil.Intfunc.apply eqn v) vdef) eqn
haftmann@33040
   115
        in (case solve (remove (op =) v vs, map eliminate oeqs) of
chaieb@29841
   116
            NONE => NONE
Philipp@32832
   117
          | SOME soln => SOME (FuncUtil.Intfunc.update (v, evaluate soln (FuncUtil.Intfunc.delete_safe v vdef)) soln))
chaieb@29841
   118
        end
chaieb@29841
   119
       else NONE)
chaieb@29841
   120
chaieb@29841
   121
fun combinations k l = if k = 0 then [[]] else
wenzelm@36938
   122
 case l of
chaieb@29841
   123
  [] => []
chaieb@29841
   124
| h::t => map (cons h) (combinations (k - 1) t) @ combinations k t
chaieb@29841
   125
chaieb@29841
   126
fun vertices vs eqs =
wenzelm@36938
   127
 let
chaieb@29841
   128
  fun vertex cmb = case solve(vs,cmb) of
chaieb@29841
   129
    NONE => NONE
wenzelm@63205
   130
   | SOME soln => SOME (map (fn v => FuncUtil.Intfunc.tryapplyd soln v @0) vs)
chaieb@29841
   131
  val rawvs = map_filter vertex (combinations (length vs) eqs)
wenzelm@63205
   132
  val unset = filter (forall (fn c => c >= @0)) rawvs
wenzelm@63198
   133
 in fold_rev (insert (eq_list op =)) unset []
wenzelm@36938
   134
 end
chaieb@29841
   135
wenzelm@63198
   136
val subsumes = eq_list (fn (x, y) => Rat.abs x <= Rat.abs y)
chaieb@29841
   137
chaieb@29841
   138
fun subsume todo dun = case todo of
chaieb@29841
   139
 [] => dun
wenzelm@36938
   140
|v::ovs =>
wenzelm@40718
   141
   let val dun' = if exists (fn w => subsumes (w, v)) dun then dun
wenzelm@40718
   142
                  else v:: filter (fn w => not (subsumes (v, w))) dun
wenzelm@36938
   143
   in subsume ovs dun'
chaieb@29841
   144
   end;
chaieb@29841
   145
chaieb@29841
   146
fun match_mp PQ P = P RS PQ;
chaieb@29841
   147
wenzelm@36938
   148
fun cterm_of_rat x =
wenzelm@63201
   149
let val (a, b) = Rat.dest x
wenzelm@36938
   150
in
wenzelm@69597
   151
 if b = 1 then Numeral.mk_cnumber \<^ctyp>\<open>real\<close> a
wenzelm@69597
   152
  else Thm.apply (Thm.apply \<^cterm>\<open>(/) :: real => _\<close>
wenzelm@69597
   153
                   (Numeral.mk_cnumber \<^ctyp>\<open>real\<close> a))
wenzelm@69597
   154
        (Numeral.mk_cnumber \<^ctyp>\<open>real\<close> b)
chaieb@29841
   155
end;
chaieb@29841
   156
wenzelm@60801
   157
fun norm_cmul_rule c th = Thm.instantiate' [] [SOME (cterm_of_rat c)] (th RS @{thm norm_cmul_rule_thm});
chaieb@29841
   158
chaieb@29841
   159
fun norm_add_rule th1 th2 = [th1, th2] MRS @{thm norm_add_rule_thm};
chaieb@29841
   160
chaieb@29841
   161
  (* I think here the static context should be sufficient!! *)
wenzelm@36938
   162
fun inequality_canon_rule ctxt =
wenzelm@36938
   163
 let
chaieb@29841
   164
  (* FIXME : Should be computed statically!! *)
wenzelm@36938
   165
  val real_poly_conv =
haftmann@36753
   166
    Semiring_Normalizer.semiring_normalize_wrapper ctxt
wenzelm@69597
   167
     (the (Semiring_Normalizer.match ctxt \<^cterm>\<open>(0::real) + 1\<close>))
wenzelm@51717
   168
 in
wenzelm@51717
   169
  fconv_rule (arg_conv ((rewr_conv @{thm ge_iff_diff_ge_0}) then_conv
wenzelm@51717
   170
    arg_conv (Numeral_Simprocs.field_comp_conv ctxt then_conv real_poly_conv)))
chaieb@29841
   171
end;
chaieb@29841
   172
chaieb@29841
   173
 val apply_pth1 = rewr_conv @{thm pth_1};
chaieb@29841
   174
 val apply_pth2 = rewr_conv @{thm pth_2};
chaieb@29841
   175
 val apply_pth3 = rewr_conv @{thm pth_3};
chaieb@29841
   176
 val apply_pth4 = rewrs_conv @{thms pth_4};
chaieb@29841
   177
 val apply_pth5 = rewr_conv @{thm pth_5};
chaieb@29841
   178
 val apply_pth6 = rewr_conv @{thm pth_6};
chaieb@29841
   179
 val apply_pth7 = rewrs_conv @{thms pth_7};
wenzelm@51717
   180
 fun apply_pth8 ctxt =
wenzelm@51717
   181
  rewr_conv @{thm pth_8} then_conv
wenzelm@51717
   182
  arg1_conv (Numeral_Simprocs.field_comp_conv ctxt) then_conv
wenzelm@51717
   183
  (try_conv (rewr_conv (mk_meta_eq @{thm scaleR_zero_left})));
wenzelm@51717
   184
 fun apply_pth9 ctxt =
wenzelm@51717
   185
  rewrs_conv @{thms pth_9} then_conv
wenzelm@51717
   186
  arg1_conv (arg1_conv (Numeral_Simprocs.field_comp_conv ctxt));
chaieb@29841
   187
 val apply_ptha = rewr_conv @{thm pth_a};
chaieb@29841
   188
 val apply_pthb = rewrs_conv @{thms pth_b};
chaieb@29841
   189
 val apply_pthc = rewrs_conv @{thms pth_c};
chaieb@29841
   190
 val apply_pthd = try_conv (rewr_conv @{thm pth_d});
chaieb@29841
   191
wenzelm@36938
   192
fun headvector t = case t of
wenzelm@69597
   193
  Const(\<^const_name>\<open>plus\<close>, _)$
wenzelm@69597
   194
   (Const(\<^const_name>\<open>scaleR\<close>, _)$_$v)$_ => v
wenzelm@69597
   195
 | Const(\<^const_name>\<open>scaleR\<close>, _)$_$v => v
chaieb@29841
   196
 | _ => error "headvector: non-canonical term"
chaieb@29841
   197
wenzelm@51717
   198
fun vector_cmul_conv ctxt ct =
wenzelm@51717
   199
   ((apply_pth5 then_conv arg1_conv (Numeral_Simprocs.field_comp_conv ctxt)) else_conv
wenzelm@51717
   200
    (apply_pth6 then_conv binop_conv (vector_cmul_conv ctxt))) ct
chaieb@29841
   201
wenzelm@51717
   202
fun vector_add_conv ctxt ct = apply_pth7 ct
wenzelm@36938
   203
 handle CTERM _ =>
wenzelm@51717
   204
  (apply_pth8 ctxt ct
wenzelm@36938
   205
   handle CTERM _ =>
wenzelm@59582
   206
    (case Thm.term_of ct of
wenzelm@69597
   207
     Const(\<^const_name>\<open>plus\<close>,_)$lt$rt =>
wenzelm@36938
   208
      let
wenzelm@36938
   209
       val l = headvector lt
chaieb@29841
   210
       val r = headvector rt
wenzelm@35408
   211
      in (case Term_Ord.fast_term_ord (l,r) of
wenzelm@51717
   212
         LESS => (apply_pthb then_conv arg_conv (vector_add_conv ctxt)
chaieb@29841
   213
                  then_conv apply_pthd) ct
wenzelm@51717
   214
        | GREATER => (apply_pthc then_conv arg_conv (vector_add_conv ctxt)
wenzelm@36938
   215
                     then_conv apply_pthd) ct
wenzelm@51717
   216
        | EQUAL => (apply_pth9 ctxt then_conv
wenzelm@51717
   217
                ((apply_ptha then_conv (vector_add_conv ctxt)) else_conv
wenzelm@51717
   218
              arg_conv (vector_add_conv ctxt) then_conv apply_pthd)) ct)
chaieb@29841
   219
      end
wenzelm@36945
   220
     | _ => Thm.reflexive ct))
chaieb@29841
   221
wenzelm@59582
   222
fun vector_canon_conv ctxt ct = case Thm.term_of ct of
wenzelm@69597
   223
 Const(\<^const_name>\<open>plus\<close>,_)$_$_ =>
wenzelm@36938
   224
  let
chaieb@29841
   225
   val ((p,l),r) = Thm.dest_comb ct |>> Thm.dest_comb
wenzelm@51717
   226
   val lth = vector_canon_conv ctxt l
wenzelm@51717
   227
   val rth = vector_canon_conv ctxt r
wenzelm@36938
   228
   val th = Drule.binop_cong_rule p lth rth
wenzelm@51717
   229
  in fconv_rule (arg_conv (vector_add_conv ctxt)) th end
chaieb@29841
   230
wenzelm@69597
   231
| Const(\<^const_name>\<open>scaleR\<close>, _)$_$_ =>
wenzelm@36938
   232
  let
chaieb@29841
   233
   val (p,r) = Thm.dest_comb ct
wenzelm@51717
   234
   val rth = Drule.arg_cong_rule p (vector_canon_conv ctxt r)
wenzelm@51717
   235
  in fconv_rule (arg_conv (apply_pth4 else_conv (vector_cmul_conv ctxt))) rth
chaieb@29841
   236
  end
chaieb@29841
   237
wenzelm@69597
   238
| Const(\<^const_name>\<open>minus\<close>,_)$_$_ => (apply_pth2 then_conv (vector_canon_conv ctxt)) ct
chaieb@29841
   239
wenzelm@69597
   240
| Const(\<^const_name>\<open>uminus\<close>,_)$_ => (apply_pth3 then_conv (vector_canon_conv ctxt)) ct
chaieb@29841
   241
huffman@31445
   242
(* FIXME
wenzelm@36938
   243
| Const(@{const_name vec},_)$n =>
chaieb@29841
   244
  let val n = Thm.dest_arg ct
wenzelm@63205
   245
  in if is_ratconst n andalso not (dest_ratconst n =/ @0)
wenzelm@36945
   246
     then Thm.reflexive ct else apply_pth1 ct
chaieb@29841
   247
  end
huffman@31445
   248
*)
chaieb@29841
   249
| _ => apply_pth1 ct
chaieb@29841
   250
wenzelm@59582
   251
fun norm_canon_conv ctxt ct = case Thm.term_of ct of
wenzelm@69597
   252
  Const(\<^const_name>\<open>norm\<close>,_)$_ => arg_conv (vector_canon_conv ctxt) ct
chaieb@29841
   253
 | _ => raise CTERM ("norm_canon_conv", [ct])
chaieb@29841
   254
wenzelm@36938
   255
fun int_flip v eq =
wenzelm@36938
   256
  if FuncUtil.Intfunc.defined eq v
wenzelm@63211
   257
  then FuncUtil.Intfunc.update (v, ~ (FuncUtil.Intfunc.apply eq v)) eq else eq;
chaieb@29841
   258
chaieb@29841
   259
local
huffman@31445
   260
 val pth_zero = @{thm norm_zero}
wenzelm@60642
   261
 val tv_n =
wenzelm@60642
   262
  (dest_TVar o Thm.typ_of_cterm o Thm.dest_arg o Thm.dest_arg1 o Thm.dest_arg o Thm.cprop_of)
wenzelm@60642
   263
    pth_zero
wenzelm@59582
   264
 val concl = Thm.dest_arg o Thm.cprop_of
wenzelm@36938
   265
 fun real_vector_combo_prover ctxt translator (nubs,ges,gts) =
wenzelm@36938
   266
  let
chaieb@29841
   267
   (* FIXME: Should be computed statically!!*)
wenzelm@36938
   268
   val real_poly_conv =
haftmann@36753
   269
      Semiring_Normalizer.semiring_normalize_wrapper ctxt
wenzelm@69597
   270
       (the (Semiring_Normalizer.match ctxt \<^cterm>\<open>(0::real) + 1\<close>))
Philipp@32832
   271
   val sources = map (Thm.dest_arg o Thm.dest_arg1 o concl) nubs
wenzelm@36938
   272
   val rawdests = fold_rev (find_normedterms o Thm.dest_arg o concl) (ges @ gts) []
wenzelm@36938
   273
   val _ = if not (forall fst rawdests) then error "real_vector_combo_prover: Sanity check"
chaieb@29841
   274
           else ()
chaieb@29841
   275
   val dests = distinct (op aconvc) (map snd rawdests)
chaieb@29841
   276
   val srcfuns = map vector_lincomb sources
wenzelm@36938
   277
   val destfuns = map vector_lincomb dests
haftmann@33042
   278
   val vvs = fold_rev (union (op aconvc) o FuncUtil.Ctermfunc.dom) (srcfuns @ destfuns) []
chaieb@29841
   279
   val n = length srcfuns
chaieb@29841
   280
   val nvs = 1 upto n
chaieb@29841
   281
   val srccombs = srcfuns ~~ nvs
chaieb@29841
   282
   fun consider d =
wenzelm@36938
   283
    let
chaieb@29841
   284
     fun coefficients x =
wenzelm@36938
   285
      let
wenzelm@63211
   286
       val inp =
wenzelm@63211
   287
        if FuncUtil.Ctermfunc.defined d x
wenzelm@63211
   288
        then FuncUtil.Intfunc.onefunc (0, ~ (FuncUtil.Ctermfunc.apply d x))
wenzelm@63211
   289
        else FuncUtil.Intfunc.empty
wenzelm@36938
   290
      in fold_rev (fn (f,v) => fn g => if FuncUtil.Ctermfunc.defined f x then FuncUtil.Intfunc.update (v, FuncUtil.Ctermfunc.apply f x) g else g) srccombs inp
chaieb@29841
   291
      end
chaieb@29841
   292
     val equations = map coefficients vvs
wenzelm@63205
   293
     val inequalities = map (fn n => FuncUtil.Intfunc.onefunc (n,@1)) nvs
chaieb@29841
   294
     fun plausiblevertices f =
wenzelm@36938
   295
      let
chaieb@29841
   296
       val flippedequations = map (fold_rev int_flip f) equations
chaieb@29841
   297
       val constraints = flippedequations @ inequalities
chaieb@29841
   298
       val rawverts = vertices nvs constraints
chaieb@29841
   299
       fun check_solution v =
wenzelm@36938
   300
        let
wenzelm@63205
   301
          val f = fold_rev FuncUtil.Intfunc.update (nvs ~~ v) (FuncUtil.Intfunc.onefunc (0, @1))
wenzelm@63205
   302
        in forall (fn e => evaluate f e = @0) flippedequations
chaieb@29841
   303
        end
chaieb@29841
   304
       val goodverts = filter check_solution rawverts
wenzelm@36938
   305
       val signfixups = map (fn n => if member (op =) f n then ~1 else 1) nvs
wenzelm@63201
   306
      in map (map2 (fn s => fn c => Rat.of_int s * c) signfixups) goodverts
chaieb@29841
   307
      end
wenzelm@36938
   308
     val allverts = fold_rev append (map plausiblevertices (allsubsets nvs)) []
chaieb@29841
   309
    in subsume allverts []
chaieb@29841
   310
    end
chaieb@29841
   311
   fun compute_ineq v =
wenzelm@36938
   312
    let
wenzelm@63205
   313
     val ths = map_filter (fn (v,t) => if v = @0 then NONE
chaieb@29841
   314
                                     else SOME(norm_cmul_rule v t))
wenzelm@36938
   315
                            (v ~~ nubs)
boehmes@32402
   316
     fun end_itlist f xs = split_last xs |> uncurry (fold_rev f)
chaieb@29841
   317
    in inequality_canon_rule ctxt (end_itlist norm_add_rule ths)
chaieb@29841
   318
    end
chaieb@29841
   319
   val ges' = map_filter (try compute_ineq) (fold_rev (append o consider) destfuns []) @
chaieb@29841
   320
                 map (inequality_canon_rule ctxt) nubs @ ges
chaieb@29841
   321
   val zerodests = filter
Philipp@32832
   322
        (fn t => null (FuncUtil.Ctermfunc.dom (vector_lincomb t))) (map snd rawdests)
chaieb@29841
   323
Philipp@32645
   324
  in fst (RealArith.real_linear_prover translator
wenzelm@59586
   325
        (map (fn t => Drule.instantiate_normalize ([(tv_n, Thm.ctyp_of_cterm t)],[]) pth_zero)
chaieb@29841
   326
            zerodests,
wenzelm@51717
   327
        map (fconv_rule (try_conv (Conv.top_sweep_conv (K (norm_canon_conv ctxt)) ctxt) then_conv
chaieb@29841
   328
                       arg_conv (arg_conv real_poly_conv))) ges',
wenzelm@51717
   329
        map (fconv_rule (try_conv (Conv.top_sweep_conv (K (norm_canon_conv ctxt)) ctxt) then_conv
Philipp@32645
   330
                       arg_conv (arg_conv real_poly_conv))) gts))
chaieb@29841
   331
  end
chaieb@29841
   332
in val real_vector_combo_prover = real_vector_combo_prover
chaieb@29841
   333
end;
chaieb@29841
   334
chaieb@29841
   335
local
chaieb@29841
   336
 val pth = @{thm norm_imp_pos_and_ge}
chaieb@29841
   337
 val norm_mp = match_mp pth
wenzelm@59582
   338
 val concl = Thm.dest_arg o Thm.cprop_of
chaieb@29841
   339
 fun conjunct1 th = th RS @{thm conjunct1}
chaieb@29841
   340
 fun conjunct2 th = th RS @{thm conjunct2}
wenzelm@36938
   341
fun real_vector_ineq_prover ctxt translator (ges,gts) =
wenzelm@36938
   342
 let
chaieb@29841
   343
(*   val _ = error "real_vector_ineq_prover: pause" *)
Philipp@32832
   344
  val ntms = fold_rev find_normedterms (map (Thm.dest_arg o concl) (ges @ gts)) []
chaieb@29841
   345
  val lctab = vector_lincombs (map snd (filter (not o fst) ntms))
chaieb@29841
   346
  val (fxns, ctxt') = Variable.variant_fixes (replicate (length lctab) "x") ctxt
wenzelm@60801
   347
  fun instantiate_cterm' ty tms = Drule.cterm_rule (Thm.instantiate' ty tms)
wenzelm@61075
   348
  fun mk_norm t =
wenzelm@61075
   349
    let val T = Thm.typ_of_cterm t
wenzelm@69597
   350
    in Thm.apply (Thm.cterm_of ctxt' (Const (\<^const_name>\<open>norm\<close>, T --> \<^typ>\<open>real\<close>))) t end
wenzelm@61075
   351
  fun mk_equals l r =
wenzelm@61075
   352
    let
wenzelm@61075
   353
      val T = Thm.typ_of_cterm l
wenzelm@69597
   354
      val eq = Thm.cterm_of ctxt (Const (\<^const_name>\<open>Pure.eq\<close>, T --> T --> propT))
wenzelm@61075
   355
    in Thm.apply (Thm.apply eq l) r end
wenzelm@69597
   356
  val asl = map2 (fn (t,_) => fn n => Thm.assume (mk_equals (mk_norm t) (Thm.cterm_of ctxt' (Free(n,\<^typ>\<open>real\<close>))))) lctab fxns
chaieb@29841
   357
  val replace_conv = try_conv (rewrs_conv asl)
chaieb@29841
   358
  val replace_rule = fconv_rule (funpow 2 arg_conv (replacenegnorms replace_conv))
chaieb@29841
   359
  val ges' =
chaieb@29841
   360
       fold_rev (fn th => fn ths => conjunct1(norm_mp th)::ths)
chaieb@29841
   361
              asl (map replace_rule ges)
chaieb@29841
   362
  val gts' = map replace_rule gts
chaieb@29841
   363
  val nubs = map (conjunct2 o norm_mp) asl
chaieb@29841
   364
  val th1 = real_vector_combo_prover ctxt' translator (nubs,ges',gts')
wenzelm@60949
   365
  val shs = filter (member (fn (t,th) => t aconvc Thm.cprop_of th) asl) (Thm.chyps_of th1)
wenzelm@36945
   366
  val th11 = hd (Variable.export ctxt' ctxt [fold Thm.implies_intr shs th1])
Philipp@32832
   367
  val cps = map (swap o Thm.dest_equals) (cprems_of th11)
wenzelm@60642
   368
  val th12 = Drule.instantiate_normalize ([], map (apfst (dest_Var o Thm.term_of)) cps) th11
wenzelm@36945
   369
  val th13 = fold Thm.elim_implies (map (Thm.reflexive o snd) cps) th12;
chaieb@29841
   370
 in hd (Variable.export ctxt' ctxt [th13])
wenzelm@36938
   371
 end
chaieb@29841
   372
in val real_vector_ineq_prover = real_vector_ineq_prover
chaieb@29841
   373
end;
chaieb@29841
   374
chaieb@29841
   375
local
chaieb@29841
   376
 val rawrule = fconv_rule (arg_conv (rewr_conv @{thm real_eq_0_iff_le_ge_0}))
chaieb@29841
   377
 fun conj_pair th = (th RS @{thm conjunct1}, th RS @{thm conjunct2})
chaieb@29841
   378
  (* FIXME: Lookup in the context every time!!! Fix this !!!*)
chaieb@29841
   379
 fun splitequation ctxt th acc =
wenzelm@36938
   380
  let
chaieb@29841
   381
   val real_poly_neg_conv = #neg
haftmann@36753
   382
       (Semiring_Normalizer.semiring_normalizers_ord_wrapper ctxt
wenzelm@69597
   383
        (the (Semiring_Normalizer.match ctxt \<^cterm>\<open>(0::real) + 1\<close>)) Thm.term_ord)
chaieb@29841
   384
   val (th1,th2) = conj_pair(rawrule th)
wenzelm@51717
   385
  in th1::fconv_rule (arg_conv (arg_conv (real_poly_neg_conv ctxt))) th2::acc
chaieb@29841
   386
  end
Philipp@32645
   387
in fun real_vector_prover ctxt _ translator (eqs,ges,gts) =
Philipp@32645
   388
     (real_vector_ineq_prover ctxt translator
Philipp@32645
   389
         (fold_rev (splitequation ctxt) eqs ges,gts), RealArith.Trivial)
chaieb@29841
   390
end;
chaieb@29841
   391
wenzelm@36938
   392
  fun init_conv ctxt =
wenzelm@51717
   393
   Simplifier.rewrite (put_simpset HOL_basic_ss ctxt
wenzelm@51717
   394
    addsimps ([(*@{thm vec_0}, @{thm vec_1},*) @{thm dist_norm}, @{thm right_minus},
wenzelm@51717
   395
      @{thm diff_self}, @{thm norm_zero}] @ @{thms arithmetic_simps} @ @{thms norm_pths}))
wenzelm@51717
   396
   then_conv Numeral_Simprocs.field_comp_conv ctxt
wenzelm@51717
   397
   then_conv nnf_conv ctxt
chaieb@29841
   398
Philipp@32645
   399
 fun pure ctxt = fst o RealArith.gen_prover_real_arith ctxt (real_vector_prover ctxt);
wenzelm@36938
   400
 fun norm_arith ctxt ct =
wenzelm@36938
   401
  let
wenzelm@59582
   402
   val ctxt' = Variable.declare_term (Thm.term_of ct) ctxt
chaieb@29841
   403
   val th = init_conv ctxt' ct
wenzelm@69597
   404
  in Thm.equal_elim (Drule.arg_cong_rule \<^cterm>\<open>Trueprop\<close> (Thm.symmetric th))
Philipp@32832
   405
                (pure ctxt' (Thm.rhs_of th))
chaieb@29841
   406
 end
chaieb@29841
   407
wenzelm@36938
   408
 fun norm_arith_tac ctxt =
wenzelm@42793
   409
   clarify_tac (put_claset HOL_cs ctxt) THEN'
wenzelm@54742
   410
   Object_Logic.full_atomize_tac ctxt THEN'
wenzelm@60754
   411
   CSUBGOAL ( fn (p,i) => resolve_tac ctxt [norm_arith ctxt (Thm.dest_arg p )] i);
chaieb@29841
   412
huffman@31445
   413
end;