src/HOL/Tools/Groebner_Basis/normalizer.ML
author chaieb
Mon Jun 11 18:28:16 2007 +0200 (2007-06-11)
changeset 23330 01c09922ce59
parent 23259 ccee01b8d1c5
child 23407 0e4452fcbeb8
permissions -rw-r--r--
Conversion for computation on constants now depends on the context
wenzelm@23252
     1
(*  Title:      HOL/Tools/Groebner_Basis/normalizer.ML
wenzelm@23252
     2
    ID:         $Id$
wenzelm@23252
     3
    Author:     Amine Chaieb, TU Muenchen
wenzelm@23252
     4
*)
wenzelm@23252
     5
wenzelm@23252
     6
signature NORMALIZER = 
wenzelm@23252
     7
sig
wenzelm@23259
     8
 val mk_cnumber : ctyp -> integer -> cterm
wenzelm@23259
     9
 val mk_cnumeral : integer -> cterm
wenzelm@23252
    10
 val semiring_normalize_conv : Proof.context -> Conv.conv
wenzelm@23252
    11
 val semiring_normalize_tac : Proof.context -> int -> tactic
chaieb@23330
    12
 val semiring_normalize_wrapper :  Proof.context -> NormalizerData.entry -> Conv.conv
wenzelm@23252
    13
 val semiring_normalizers_conv :
wenzelm@23252
    14
     cterm list -> cterm list * thm list -> cterm list * thm list ->
wenzelm@23252
    15
     (cterm -> bool) * Conv.conv * Conv.conv * Conv.conv -> (cterm -> Thm.cterm -> bool) ->
wenzelm@23252
    16
       {add: Conv.conv, mul: Conv.conv, neg: Conv.conv, main: Conv.conv, 
wenzelm@23252
    17
        pow: Conv.conv, sub: Conv.conv}
wenzelm@23252
    18
end
wenzelm@23252
    19
wenzelm@23252
    20
structure Normalizer: NORMALIZER = 
wenzelm@23252
    21
struct
wenzelm@23252
    22
open Misc;
wenzelm@23252
    23
wenzelm@23252
    24
local
wenzelm@23252
    25
 val pls_const = @{cterm "Numeral.Pls"}
wenzelm@23252
    26
   and min_const = @{cterm "Numeral.Min"}
wenzelm@23252
    27
   and bit_const = @{cterm "Numeral.Bit"}
wenzelm@23252
    28
   and zero = @{cpat "0"}
wenzelm@23252
    29
   and one = @{cpat "1"}
wenzelm@23252
    30
 fun mk_cbit 0 = @{cterm "Numeral.bit.B0"}
wenzelm@23252
    31
  | mk_cbit 1 = @{cterm "Numeral.bit.B1"}
wenzelm@23252
    32
  | mk_cbit _ = raise CTERM ("mk_cbit", []);
wenzelm@23252
    33
wenzelm@23252
    34
in
wenzelm@23252
    35
wenzelm@23252
    36
fun mk_cnumeral 0 = pls_const
wenzelm@23252
    37
  | mk_cnumeral ~1 = min_const
wenzelm@23252
    38
  | mk_cnumeral i =
wenzelm@23259
    39
      let val (q, r) = Integer.divmod i 2
wenzelm@23259
    40
      in Thm.capply (Thm.capply bit_const (mk_cnumeral q)) (mk_cbit (Integer.machine_int r)) end;
wenzelm@23252
    41
wenzelm@23252
    42
fun mk_cnumber cT = 
wenzelm@23252
    43
 let 
wenzelm@23252
    44
  val [nb_of, z, on] = 
wenzelm@23252
    45
    map (Drule.cterm_rule (instantiate' [SOME cT] [])) [@{cpat "number_of"}, zero, one]
wenzelm@23252
    46
  fun h 0 = z
wenzelm@23252
    47
    | h 1 = on
wenzelm@23252
    48
    | h x = Thm.capply nb_of (mk_cnumeral x)
wenzelm@23252
    49
 in h end;
wenzelm@23252
    50
end;
wenzelm@23252
    51
wenzelm@23252
    52
wenzelm@23252
    53
(* Very basic stuff for terms *)
wenzelm@23252
    54
val dest_numeral = term_of #> HOLogic.dest_number #> snd;
wenzelm@23252
    55
val is_numeral = can dest_numeral;
wenzelm@23252
    56
wenzelm@23252
    57
val numeral01_conv = Simplifier.rewrite
wenzelm@23252
    58
                         (HOL_basic_ss addsimps [numeral_1_eq_1, numeral_0_eq_0]);
wenzelm@23252
    59
val zero1_numeral_conv = 
wenzelm@23252
    60
 Simplifier.rewrite (HOL_basic_ss addsimps [numeral_1_eq_1 RS sym, numeral_0_eq_0 RS sym]);
wenzelm@23252
    61
val zerone_conv = fn cv => zero1_numeral_conv then_conv cv then_conv numeral01_conv;
wenzelm@23252
    62
val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"},
wenzelm@23252
    63
                @{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, 
wenzelm@23252
    64
                @{thm "less_nat_number_of"}];
wenzelm@23252
    65
val nat_add_conv = 
wenzelm@23252
    66
 zerone_conv 
wenzelm@23252
    67
  (Simplifier.rewrite 
wenzelm@23252
    68
    (HOL_basic_ss 
wenzelm@23252
    69
       addsimps arith_simps @ natarith @ rel_simps
wenzelm@23252
    70
             @ [if_False, if_True, add_0, add_Suc, add_number_of_left, Suc_eq_add_numeral_1]
wenzelm@23252
    71
             @ map (fn th => th RS sym) numerals));
wenzelm@23252
    72
wenzelm@23252
    73
val nat_mul_conv = nat_add_conv;
wenzelm@23252
    74
val zeron_tm = @{cterm "0::nat"};
wenzelm@23252
    75
val onen_tm  = @{cterm "1::nat"};
wenzelm@23252
    76
val true_tm = @{cterm "True"};
wenzelm@23252
    77
wenzelm@23252
    78
wenzelm@23252
    79
(* The main function! *)
wenzelm@23252
    80
fun semiring_normalizers_conv vars (sr_ops, sr_rules) (r_ops, r_rules)
wenzelm@23252
    81
  (is_semiring_constant, semiring_add_conv, semiring_mul_conv, semiring_pow_conv) =
wenzelm@23252
    82
let
wenzelm@23252
    83
wenzelm@23252
    84
val [pthm_02, pthm_03, pthm_04, pthm_05, pthm_07, pthm_08,
wenzelm@23252
    85
     pthm_09, pthm_10, pthm_11, pthm_12, pthm_13, pthm_14, pthm_15, pthm_16,
wenzelm@23252
    86
     pthm_17, pthm_18, pthm_19, pthm_21, pthm_22, pthm_23, pthm_24,
wenzelm@23252
    87
     pthm_25, pthm_26, pthm_27, pthm_28, pthm_29, pthm_30, pthm_31, pthm_32,
wenzelm@23252
    88
     pthm_33, pthm_34, pthm_35, pthm_36, pthm_37, pthm_38,pthm_39,pthm_40] = sr_rules;
wenzelm@23252
    89
wenzelm@23252
    90
val [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry] = vars;
wenzelm@23252
    91
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops;
wenzelm@23252
    92
val [add_tm, mul_tm, pow_tm] = map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat];
wenzelm@23252
    93
wenzelm@23252
    94
val dest_add = dest_binop add_tm
wenzelm@23252
    95
val dest_mul = dest_binop mul_tm
wenzelm@23252
    96
fun dest_pow tm =
wenzelm@23252
    97
 let val (l,r) = dest_binop pow_tm tm
wenzelm@23252
    98
 in if is_numeral r then (l,r) else raise CTERM ("dest_pow",[tm])
wenzelm@23252
    99
 end;
wenzelm@23252
   100
val is_add = is_binop add_tm
wenzelm@23252
   101
val is_mul = is_binop mul_tm
wenzelm@23252
   102
fun is_pow tm = is_binop pow_tm tm andalso is_numeral(Thm.dest_arg tm);
wenzelm@23252
   103
wenzelm@23252
   104
val (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub,cx',cy') =
wenzelm@23252
   105
  (case (r_ops, r_rules) of
wenzelm@23252
   106
    ([], []) => (TrueI, TrueI, true_tm, true_tm, (fn t => (t,t)), K false, true_tm, true_tm)
wenzelm@23252
   107
  | ([sub_pat, neg_pat], [neg_mul, sub_add]) =>
wenzelm@23252
   108
      let
wenzelm@23252
   109
        val sub_tm = Thm.dest_fun (Thm.dest_fun sub_pat)
wenzelm@23252
   110
        val neg_tm = Thm.dest_fun neg_pat
wenzelm@23252
   111
        val dest_sub = dest_binop sub_tm
wenzelm@23252
   112
        val is_sub = is_binop sub_tm
wenzelm@23252
   113
      in (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub, neg_mul |> concl |> Thm.dest_arg,
wenzelm@23252
   114
          sub_add |> concl |> Thm.dest_arg |> Thm.dest_arg)
wenzelm@23252
   115
      end);
wenzelm@23252
   116
in fn variable_order =>
wenzelm@23252
   117
 let
wenzelm@23252
   118
wenzelm@23252
   119
(* Conversion for "x^n * x^m", with either x^n = x and/or x^m = x possible.  *)
wenzelm@23252
   120
(* Also deals with "const * const", but both terms must involve powers of    *)
wenzelm@23252
   121
(* the same variable, or both be constants, or behaviour may be incorrect.   *)
wenzelm@23252
   122
wenzelm@23252
   123
 fun powvar_mul_conv tm =
wenzelm@23252
   124
  let
wenzelm@23252
   125
  val (l,r) = dest_mul tm
wenzelm@23252
   126
  in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   127
     then semiring_mul_conv tm
wenzelm@23252
   128
     else
wenzelm@23252
   129
      ((let
wenzelm@23252
   130
         val (lx,ln) = dest_pow l
wenzelm@23252
   131
        in
wenzelm@23252
   132
         ((let val (rx,rn) = dest_pow r
wenzelm@23252
   133
               val th1 = inst_thm [(cx,lx),(cp,ln),(cq,rn)] pthm_29
wenzelm@23252
   134
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   135
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   136
           handle CTERM _ =>
wenzelm@23252
   137
            (let val th1 = inst_thm [(cx,lx),(cq,ln)] pthm_31
wenzelm@23252
   138
                 val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   139
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)) end)
wenzelm@23252
   140
       handle CTERM _ =>
wenzelm@23252
   141
           ((let val (rx,rn) = dest_pow r
wenzelm@23252
   142
                val th1 = inst_thm [(cx,rx),(cq,rn)] pthm_30
wenzelm@23252
   143
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   144
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   145
           handle CTERM _ => inst_thm [(cx,l)] pthm_32
wenzelm@23252
   146
wenzelm@23252
   147
))
wenzelm@23252
   148
 end;
wenzelm@23252
   149
wenzelm@23252
   150
(* Remove "1 * m" from a monomial, and just leave m.                         *)
wenzelm@23252
   151
wenzelm@23252
   152
 fun monomial_deone th =
wenzelm@23252
   153
       (let val (l,r) = dest_mul(concl th) in
wenzelm@23252
   154
           if l aconvc one_tm
wenzelm@23252
   155
          then transitive th (inst_thm [(ca,r)] pthm_13)  else th end)
wenzelm@23252
   156
       handle CTERM _ => th;
wenzelm@23252
   157
wenzelm@23252
   158
(* Conversion for "(monomial)^n", where n is a numeral.                      *)
wenzelm@23252
   159
wenzelm@23252
   160
 val monomial_pow_conv =
wenzelm@23252
   161
  let
wenzelm@23252
   162
   fun monomial_pow tm bod ntm =
wenzelm@23252
   163
    if not(is_comb bod)
wenzelm@23252
   164
    then reflexive tm
wenzelm@23252
   165
    else
wenzelm@23252
   166
     if is_semiring_constant bod
wenzelm@23252
   167
     then semiring_pow_conv tm
wenzelm@23252
   168
     else
wenzelm@23252
   169
      let
wenzelm@23252
   170
      val (lopr,r) = Thm.dest_comb bod
wenzelm@23252
   171
      in if not(is_comb lopr)
wenzelm@23252
   172
         then reflexive tm
wenzelm@23252
   173
        else
wenzelm@23252
   174
          let
wenzelm@23252
   175
          val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   176
         in
wenzelm@23252
   177
           if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   178
          then
wenzelm@23252
   179
            let val th1 = inst_thm [(cx,l),(cp,r),(cq,ntm)] pthm_34
wenzelm@23252
   180
                val (l,r) = Thm.dest_comb(concl th1)
wenzelm@23252
   181
           in transitive th1 (Drule.arg_cong_rule l (nat_mul_conv r))
wenzelm@23252
   182
           end
wenzelm@23252
   183
           else
wenzelm@23252
   184
            if opr aconvc mul_tm
wenzelm@23252
   185
            then
wenzelm@23252
   186
             let
wenzelm@23252
   187
              val th1 = inst_thm [(cx,l),(cy,r),(cq,ntm)] pthm_33
wenzelm@23252
   188
             val (xy,z) = Thm.dest_comb(concl th1)
wenzelm@23252
   189
              val (x,y) = Thm.dest_comb xy
wenzelm@23252
   190
              val thl = monomial_pow y l ntm
wenzelm@23252
   191
              val thr = monomial_pow z r ntm
wenzelm@23252
   192
             in transitive th1 (combination (Drule.arg_cong_rule x thl) thr)
wenzelm@23252
   193
             end
wenzelm@23252
   194
             else reflexive tm
wenzelm@23252
   195
          end
wenzelm@23252
   196
      end
wenzelm@23252
   197
  in fn tm =>
wenzelm@23252
   198
   let
wenzelm@23252
   199
    val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   200
    val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   201
   in if not (opr aconvc pow_tm) orelse not(is_numeral r)
wenzelm@23252
   202
      then raise CTERM ("monomial_pow_conv", [tm])
wenzelm@23252
   203
      else if r aconvc zeron_tm
wenzelm@23252
   204
      then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   205
      else if r aconvc onen_tm
wenzelm@23252
   206
      then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   207
      else monomial_deone(monomial_pow tm l r)
wenzelm@23252
   208
   end
wenzelm@23252
   209
  end;
wenzelm@23252
   210
wenzelm@23252
   211
(* Multiplication of canonical monomials.                                    *)
wenzelm@23252
   212
 val monomial_mul_conv =
wenzelm@23252
   213
  let
wenzelm@23252
   214
   fun powvar tm =
wenzelm@23252
   215
    if is_semiring_constant tm then one_tm
wenzelm@23252
   216
    else
wenzelm@23252
   217
     ((let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   218
           val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   219
       in if opr aconvc pow_tm andalso is_numeral r then l 
wenzelm@23252
   220
          else raise CTERM ("monomial_mul_conv",[tm]) end)
wenzelm@23252
   221
     handle CTERM _ => tm)   (* FIXME !? *)
wenzelm@23252
   222
   fun  vorder x y =
wenzelm@23252
   223
    if x aconvc y then 0
wenzelm@23252
   224
    else
wenzelm@23252
   225
     if x aconvc one_tm then ~1
wenzelm@23252
   226
     else if y aconvc one_tm then 1
wenzelm@23252
   227
      else if variable_order x y then ~1 else 1
wenzelm@23252
   228
   fun monomial_mul tm l r =
wenzelm@23252
   229
    ((let val (lx,ly) = dest_mul l val vl = powvar lx
wenzelm@23252
   230
      in
wenzelm@23252
   231
      ((let
wenzelm@23252
   232
        val (rx,ry) = dest_mul r
wenzelm@23252
   233
         val vr = powvar rx
wenzelm@23252
   234
         val ord = vorder vl vr
wenzelm@23252
   235
        in
wenzelm@23252
   236
         if ord = 0
wenzelm@23252
   237
        then
wenzelm@23252
   238
          let
wenzelm@23252
   239
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] pthm_15
wenzelm@23252
   240
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   241
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   242
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   243
             val th3 = transitive th1 th2
wenzelm@23252
   244
              val  (tm5,tm6) = Thm.dest_comb(concl th3)
wenzelm@23252
   245
              val  (tm7,tm8) = Thm.dest_comb tm6
wenzelm@23252
   246
             val  th4 = monomial_mul tm6 (Thm.dest_arg tm7) tm8
wenzelm@23252
   247
         in  transitive th3 (Drule.arg_cong_rule tm5 th4)
wenzelm@23252
   248
         end
wenzelm@23252
   249
         else
wenzelm@23252
   250
          let val th0 = if ord < 0 then pthm_16 else pthm_17
wenzelm@23252
   251
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] th0
wenzelm@23252
   252
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   253
             val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   254
         in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   255
         end
wenzelm@23252
   256
        end)
wenzelm@23252
   257
       handle CTERM _ =>
wenzelm@23252
   258
        (let val vr = powvar r val ord = vorder vl vr
wenzelm@23252
   259
        in
wenzelm@23252
   260
          if ord = 0 then
wenzelm@23252
   261
           let
wenzelm@23252
   262
           val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_18
wenzelm@23252
   263
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   264
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   265
           val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   266
          in transitive th1 th2
wenzelm@23252
   267
          end
wenzelm@23252
   268
          else
wenzelm@23252
   269
          if ord < 0 then
wenzelm@23252
   270
            let val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_19
wenzelm@23252
   271
                val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   272
                val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   273
           in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   274
           end
wenzelm@23252
   275
           else inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   276
        end)) end)
wenzelm@23252
   277
     handle CTERM _ =>
wenzelm@23252
   278
      (let val vl = powvar l in
wenzelm@23252
   279
        ((let
wenzelm@23252
   280
          val (rx,ry) = dest_mul r
wenzelm@23252
   281
          val vr = powvar rx
wenzelm@23252
   282
           val ord = vorder vl vr
wenzelm@23252
   283
         in if ord = 0 then
wenzelm@23252
   284
              let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_21
wenzelm@23252
   285
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   286
                 val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   287
             in transitive th1 (Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2)
wenzelm@23252
   288
             end
wenzelm@23252
   289
             else if ord > 0 then
wenzelm@23252
   290
                 let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_22
wenzelm@23252
   291
                     val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   292
                    val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   293
                in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   294
                end
wenzelm@23252
   295
             else reflexive tm
wenzelm@23252
   296
         end)
wenzelm@23252
   297
        handle CTERM _ =>
wenzelm@23252
   298
          (let val vr = powvar r
wenzelm@23252
   299
               val  ord = vorder vl vr
wenzelm@23252
   300
          in if ord = 0 then powvar_mul_conv tm
wenzelm@23252
   301
              else if ord > 0 then inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   302
              else reflexive tm
wenzelm@23252
   303
          end)) end))
wenzelm@23252
   304
  in fn tm => let val (l,r) = dest_mul tm in monomial_deone(monomial_mul tm l r)
wenzelm@23252
   305
             end
wenzelm@23252
   306
  end;
wenzelm@23252
   307
(* Multiplication by monomial of a polynomial.                               *)
wenzelm@23252
   308
wenzelm@23252
   309
 val polynomial_monomial_mul_conv =
wenzelm@23252
   310
  let
wenzelm@23252
   311
   fun pmm_conv tm =
wenzelm@23252
   312
    let val (l,r) = dest_mul tm
wenzelm@23252
   313
    in
wenzelm@23252
   314
    ((let val (y,z) = dest_add r
wenzelm@23252
   315
          val th1 = inst_thm [(cx,l),(cy,y),(cz,z)] pthm_37
wenzelm@23252
   316
          val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   317
          val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   318
          val th2 = combination (Drule.arg_cong_rule tm3 (monomial_mul_conv tm4)) (pmm_conv tm2)
wenzelm@23252
   319
      in transitive th1 th2
wenzelm@23252
   320
      end)
wenzelm@23252
   321
     handle CTERM _ => monomial_mul_conv tm)
wenzelm@23252
   322
   end
wenzelm@23252
   323
 in pmm_conv
wenzelm@23252
   324
 end;
wenzelm@23252
   325
wenzelm@23252
   326
(* Addition of two monomials identical except for constant multiples.        *)
wenzelm@23252
   327
wenzelm@23252
   328
fun monomial_add_conv tm =
wenzelm@23252
   329
 let val (l,r) = dest_add tm
wenzelm@23252
   330
 in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   331
    then semiring_add_conv tm
wenzelm@23252
   332
    else
wenzelm@23252
   333
     let val th1 =
wenzelm@23252
   334
           if is_mul l andalso is_semiring_constant(Thm.dest_arg1 l)
wenzelm@23252
   335
           then if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) then
wenzelm@23252
   336
                    inst_thm [(ca,Thm.dest_arg1 l),(cm,Thm.dest_arg r), (cb,Thm.dest_arg1 r)] pthm_02
wenzelm@23252
   337
                else inst_thm [(ca,Thm.dest_arg1 l),(cm,r)] pthm_03
wenzelm@23252
   338
           else if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r)
wenzelm@23252
   339
           then inst_thm [(cm,l),(ca,Thm.dest_arg1 r)] pthm_04
wenzelm@23252
   340
           else inst_thm [(cm,r)] pthm_05
wenzelm@23252
   341
         val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   342
         val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   343
         val th2 = Drule.arg_cong_rule tm3 (semiring_add_conv tm4)
wenzelm@23252
   344
         val th3 = transitive th1 (Drule.fun_cong_rule th2 tm2)
wenzelm@23252
   345
         val tm5 = concl th3
wenzelm@23252
   346
      in
wenzelm@23252
   347
      if (Thm.dest_arg1 tm5) aconvc zero_tm
wenzelm@23252
   348
      then transitive th3 (inst_thm [(ca,Thm.dest_arg tm5)] pthm_11)
wenzelm@23252
   349
      else monomial_deone th3
wenzelm@23252
   350
     end
wenzelm@23252
   351
 end;
wenzelm@23252
   352
wenzelm@23252
   353
(* Ordering on monomials.                                                    *)
wenzelm@23252
   354
wenzelm@23252
   355
fun striplist dest =
wenzelm@23252
   356
 let fun strip x acc =
wenzelm@23252
   357
   ((let val (l,r) = dest x in
wenzelm@23252
   358
        strip l (strip r acc) end)
wenzelm@23252
   359
    handle CTERM _ => x::acc)    (* FIXME !? *)
wenzelm@23252
   360
 in fn x => strip x []
wenzelm@23252
   361
 end;
wenzelm@23252
   362
wenzelm@23252
   363
wenzelm@23252
   364
fun powervars tm =
wenzelm@23252
   365
 let val ptms = striplist dest_mul tm
wenzelm@23252
   366
 in if is_semiring_constant (hd ptms) then tl ptms else ptms
wenzelm@23252
   367
 end;
wenzelm@23252
   368
val num_0 = 0;
wenzelm@23252
   369
val num_1 = 1;
wenzelm@23252
   370
fun dest_varpow tm =
wenzelm@23252
   371
 ((let val (x,n) = dest_pow tm in (x,dest_numeral n) end)
wenzelm@23252
   372
   handle CTERM _ =>
wenzelm@23252
   373
   (tm,(if is_semiring_constant tm then num_0 else num_1)));
wenzelm@23252
   374
wenzelm@23252
   375
val morder =
wenzelm@23252
   376
 let fun lexorder l1 l2 =
wenzelm@23252
   377
  case (l1,l2) of
wenzelm@23252
   378
    ([],[]) => 0
wenzelm@23252
   379
  | (vps,[]) => ~1
wenzelm@23252
   380
  | ([],vps) => 1
wenzelm@23252
   381
  | (((x1,n1)::vs1),((x2,n2)::vs2)) =>
wenzelm@23252
   382
     if variable_order x1 x2 then 1
wenzelm@23252
   383
     else if variable_order x2 x1 then ~1
wenzelm@23252
   384
     else if n1 < n2 then ~1
wenzelm@23252
   385
     else if n2 < n1 then 1
wenzelm@23252
   386
     else lexorder vs1 vs2
wenzelm@23252
   387
 in fn tm1 => fn tm2 =>
wenzelm@23252
   388
  let val vdegs1 = map dest_varpow (powervars tm1)
wenzelm@23252
   389
      val vdegs2 = map dest_varpow (powervars tm2)
wenzelm@23252
   390
      val deg1 = fold_rev ((curry (op +)) o snd) vdegs1 num_0
wenzelm@23252
   391
      val deg2 = fold_rev ((curry (op +)) o snd) vdegs2 num_0
wenzelm@23252
   392
  in if deg1 < deg2 then ~1 else if deg1 > deg2 then 1
wenzelm@23252
   393
                            else lexorder vdegs1 vdegs2
wenzelm@23252
   394
  end
wenzelm@23252
   395
 end;
wenzelm@23252
   396
wenzelm@23252
   397
(* Addition of two polynomials.                                              *)
wenzelm@23252
   398
wenzelm@23252
   399
val polynomial_add_conv =
wenzelm@23252
   400
 let
wenzelm@23252
   401
 fun dezero_rule th =
wenzelm@23252
   402
  let
wenzelm@23252
   403
   val tm = concl th
wenzelm@23252
   404
  in
wenzelm@23252
   405
   if not(is_add tm) then th else
wenzelm@23252
   406
   let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   407
       val l = Thm.dest_arg lopr
wenzelm@23252
   408
   in
wenzelm@23252
   409
    if l aconvc zero_tm
wenzelm@23252
   410
    then transitive th (inst_thm [(ca,r)] pthm_07)   else
wenzelm@23252
   411
        if r aconvc zero_tm
wenzelm@23252
   412
        then transitive th (inst_thm [(ca,l)] pthm_08)  else th
wenzelm@23252
   413
   end
wenzelm@23252
   414
  end
wenzelm@23252
   415
 fun padd tm =
wenzelm@23252
   416
  let
wenzelm@23252
   417
   val (l,r) = dest_add tm
wenzelm@23252
   418
  in
wenzelm@23252
   419
   if l aconvc zero_tm then inst_thm [(ca,r)] pthm_07
wenzelm@23252
   420
   else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_08
wenzelm@23252
   421
   else
wenzelm@23252
   422
    if is_add l
wenzelm@23252
   423
    then
wenzelm@23252
   424
     let val (a,b) = dest_add l
wenzelm@23252
   425
     in
wenzelm@23252
   426
     if is_add r then
wenzelm@23252
   427
      let val (c,d) = dest_add r
wenzelm@23252
   428
          val ord = morder a c
wenzelm@23252
   429
      in
wenzelm@23252
   430
       if ord = 0 then
wenzelm@23252
   431
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,c),(cd,d)] pthm_23
wenzelm@23252
   432
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   433
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   434
            val th2 = Drule.arg_cong_rule tm3 (monomial_add_conv tm4)
wenzelm@23252
   435
        in dezero_rule (transitive th1 (combination th2 (padd tm2)))
wenzelm@23252
   436
        end
wenzelm@23252
   437
       else (* ord <> 0*)
wenzelm@23252
   438
        let val th1 =
wenzelm@23252
   439
                if ord > 0 then inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   440
                else inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   441
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   442
        in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   443
        end
wenzelm@23252
   444
      end
wenzelm@23252
   445
     else (* not (is_add r)*)
wenzelm@23252
   446
      let val ord = morder a r
wenzelm@23252
   447
      in
wenzelm@23252
   448
       if ord = 0 then
wenzelm@23252
   449
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_26
wenzelm@23252
   450
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   451
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   452
            val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   453
        in dezero_rule (transitive th1 th2)
wenzelm@23252
   454
        end
wenzelm@23252
   455
       else (* ord <> 0*)
wenzelm@23252
   456
        if ord > 0 then
wenzelm@23252
   457
          let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   458
              val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   459
          in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   460
          end
wenzelm@23252
   461
        else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   462
      end
wenzelm@23252
   463
    end
wenzelm@23252
   464
   else (* not (is_add l)*)
wenzelm@23252
   465
    if is_add r then
wenzelm@23252
   466
      let val (c,d) = dest_add r
wenzelm@23252
   467
          val  ord = morder l c
wenzelm@23252
   468
      in
wenzelm@23252
   469
       if ord = 0 then
wenzelm@23252
   470
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_28
wenzelm@23252
   471
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   472
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   473
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   474
         in dezero_rule (transitive th1 th2)
wenzelm@23252
   475
         end
wenzelm@23252
   476
       else
wenzelm@23252
   477
        if ord > 0 then reflexive tm
wenzelm@23252
   478
        else
wenzelm@23252
   479
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   480
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   481
         in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   482
         end
wenzelm@23252
   483
      end
wenzelm@23252
   484
    else
wenzelm@23252
   485
     let val ord = morder l r
wenzelm@23252
   486
     in
wenzelm@23252
   487
      if ord = 0 then monomial_add_conv tm
wenzelm@23252
   488
      else if ord > 0 then dezero_rule(reflexive tm)
wenzelm@23252
   489
      else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   490
     end
wenzelm@23252
   491
  end
wenzelm@23252
   492
 in padd
wenzelm@23252
   493
 end;
wenzelm@23252
   494
wenzelm@23252
   495
(* Multiplication of two polynomials.                                        *)
wenzelm@23252
   496
wenzelm@23252
   497
val polynomial_mul_conv =
wenzelm@23252
   498
 let
wenzelm@23252
   499
  fun pmul tm =
wenzelm@23252
   500
   let val (l,r) = dest_mul tm
wenzelm@23252
   501
   in
wenzelm@23252
   502
    if not(is_add l) then polynomial_monomial_mul_conv tm
wenzelm@23252
   503
    else
wenzelm@23252
   504
     if not(is_add r) then
wenzelm@23252
   505
      let val th1 = inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   506
      in transitive th1 (polynomial_monomial_mul_conv(concl th1))
wenzelm@23252
   507
      end
wenzelm@23252
   508
     else
wenzelm@23252
   509
       let val (a,b) = dest_add l
wenzelm@23252
   510
           val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_10
wenzelm@23252
   511
           val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   512
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   513
           val th2 = Drule.arg_cong_rule tm3 (polynomial_monomial_mul_conv tm4)
wenzelm@23252
   514
           val th3 = transitive th1 (combination th2 (pmul tm2))
wenzelm@23252
   515
       in transitive th3 (polynomial_add_conv (concl th3))
wenzelm@23252
   516
       end
wenzelm@23252
   517
   end
wenzelm@23252
   518
 in fn tm =>
wenzelm@23252
   519
   let val (l,r) = dest_mul tm
wenzelm@23252
   520
   in
wenzelm@23252
   521
    if l aconvc zero_tm then inst_thm [(ca,r)] pthm_11
wenzelm@23252
   522
    else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_12
wenzelm@23252
   523
    else if l aconvc one_tm then inst_thm [(ca,r)] pthm_13
wenzelm@23252
   524
    else if r aconvc one_tm then inst_thm [(ca,l)] pthm_14
wenzelm@23252
   525
    else pmul tm
wenzelm@23252
   526
   end
wenzelm@23252
   527
 end;
wenzelm@23252
   528
wenzelm@23252
   529
(* Power of polynomial (optimized for the monomial and trivial cases).       *)
wenzelm@23252
   530
wenzelm@23252
   531
val Succ = @{cterm "Suc"};
wenzelm@23252
   532
val num_conv = fn n =>
wenzelm@23252
   533
        nat_add_conv (Thm.capply (Succ) (mk_cnumber @{ctyp "nat"} ((dest_numeral n) - 1)))
wenzelm@23252
   534
                     |> Thm.symmetric;
wenzelm@23252
   535
wenzelm@23252
   536
wenzelm@23252
   537
val polynomial_pow_conv =
wenzelm@23252
   538
 let
wenzelm@23252
   539
  fun ppow tm =
wenzelm@23252
   540
    let val (l,n) = dest_pow tm
wenzelm@23252
   541
    in
wenzelm@23252
   542
     if n aconvc zeron_tm then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   543
     else if n aconvc onen_tm then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   544
     else
wenzelm@23252
   545
         let val th1 = num_conv n
wenzelm@23252
   546
             val th2 = inst_thm [(cx,l),(cq,Thm.dest_arg (concl th1))] pthm_38
wenzelm@23252
   547
             val (tm1,tm2) = Thm.dest_comb(concl th2)
wenzelm@23252
   548
             val th3 = transitive th2 (Drule.arg_cong_rule tm1 (ppow tm2))
wenzelm@23252
   549
             val th4 = transitive (Drule.arg_cong_rule (Thm.dest_fun tm) th1) th3
wenzelm@23252
   550
         in transitive th4 (polynomial_mul_conv (concl th4))
wenzelm@23252
   551
         end
wenzelm@23252
   552
    end
wenzelm@23252
   553
 in fn tm =>
wenzelm@23252
   554
       if is_add(Thm.dest_arg1 tm) then ppow tm else monomial_pow_conv tm
wenzelm@23252
   555
 end;
wenzelm@23252
   556
wenzelm@23252
   557
(* Negation.                                                                 *)
wenzelm@23252
   558
wenzelm@23252
   559
val polynomial_neg_conv =
wenzelm@23252
   560
 fn tm =>
wenzelm@23252
   561
   let val (l,r) = Thm.dest_comb tm in
wenzelm@23252
   562
        if not (l aconvc neg_tm) then raise CTERM ("polynomial_neg_conv",[tm]) else
wenzelm@23252
   563
        let val th1 = inst_thm [(cx',r)] neg_mul
wenzelm@23252
   564
            val th2 = transitive th1 (arg1_conv semiring_mul_conv (concl th1))
wenzelm@23252
   565
        in transitive th2 (polynomial_monomial_mul_conv (concl th2))
wenzelm@23252
   566
        end
wenzelm@23252
   567
   end;
wenzelm@23252
   568
wenzelm@23252
   569
wenzelm@23252
   570
(* Subtraction.                                                              *)
wenzelm@23252
   571
val polynomial_sub_conv = fn tm =>
wenzelm@23252
   572
  let val (l,r) = dest_sub tm
wenzelm@23252
   573
      val th1 = inst_thm [(cx',l),(cy',r)] sub_add
wenzelm@23252
   574
      val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   575
      val th2 = Drule.arg_cong_rule tm1 (polynomial_neg_conv tm2)
wenzelm@23252
   576
  in transitive th1 (transitive th2 (polynomial_add_conv (concl th2)))
wenzelm@23252
   577
  end;
wenzelm@23252
   578
wenzelm@23252
   579
(* Conversion from HOL term.                                                 *)
wenzelm@23252
   580
wenzelm@23252
   581
fun polynomial_conv tm =
wenzelm@23252
   582
 if not(is_comb tm) orelse is_semiring_constant tm
wenzelm@23252
   583
 then reflexive tm
wenzelm@23252
   584
 else
wenzelm@23252
   585
  let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   586
  in if lopr aconvc neg_tm then
wenzelm@23252
   587
       let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r)
wenzelm@23252
   588
       in transitive th1 (polynomial_neg_conv (concl th1))
wenzelm@23252
   589
       end
wenzelm@23252
   590
     else
wenzelm@23252
   591
       if not(is_comb lopr) then reflexive tm
wenzelm@23252
   592
       else
wenzelm@23252
   593
         let val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   594
         in if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   595
            then
wenzelm@23252
   596
              let val th1 = Drule.fun_cong_rule (Drule.arg_cong_rule opr (polynomial_conv l)) r
wenzelm@23252
   597
              in transitive th1 (polynomial_pow_conv (concl th1))
wenzelm@23252
   598
              end
wenzelm@23252
   599
            else
wenzelm@23252
   600
              if opr aconvc add_tm orelse opr aconvc mul_tm orelse opr aconvc sub_tm
wenzelm@23252
   601
              then
wenzelm@23252
   602
               let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) (polynomial_conv r)
wenzelm@23252
   603
                   val f = if opr aconvc add_tm then polynomial_add_conv
wenzelm@23252
   604
                      else if opr aconvc mul_tm then polynomial_mul_conv
wenzelm@23252
   605
                      else polynomial_sub_conv
wenzelm@23252
   606
               in transitive th1 (f (concl th1))
wenzelm@23252
   607
               end
wenzelm@23252
   608
              else reflexive tm
wenzelm@23252
   609
         end
wenzelm@23252
   610
  end;
wenzelm@23252
   611
 in
wenzelm@23252
   612
   {main = polynomial_conv,
wenzelm@23252
   613
    add = polynomial_add_conv,
wenzelm@23252
   614
    mul = polynomial_mul_conv,
wenzelm@23252
   615
    pow = polynomial_pow_conv,
wenzelm@23252
   616
    neg = polynomial_neg_conv,
wenzelm@23252
   617
    sub = polynomial_sub_conv}
wenzelm@23252
   618
 end
wenzelm@23252
   619
end;
wenzelm@23252
   620
wenzelm@23252
   621
val nat_arith = @{thms "nat_arith"};
wenzelm@23252
   622
val nat_exp_ss = HOL_basic_ss addsimps (nat_number @ nat_arith @ arith_simps @ rel_simps)
wenzelm@23252
   623
                              addsimps [Let_def, if_False, if_True, add_0, add_Suc];
wenzelm@23252
   624
chaieb@23330
   625
fun semiring_normalize_wrapper ctxt ({vars, semiring, ring, idom}, 
wenzelm@23252
   626
                                     {conv, dest_const, mk_const, is_const}) =
wenzelm@23252
   627
  let
wenzelm@23252
   628
    fun ord t u = Term.term_ord (term_of t, term_of u) = LESS
wenzelm@23252
   629
wenzelm@23252
   630
    val pow_conv =
wenzelm@23252
   631
      arg_conv (Simplifier.rewrite nat_exp_ss)
wenzelm@23252
   632
      then_conv Simplifier.rewrite
wenzelm@23252
   633
        (HOL_basic_ss addsimps [nth (snd semiring) 31, nth (snd semiring) 34])
chaieb@23330
   634
      then_conv conv ctxt
chaieb@23330
   635
    val dat = (is_const, conv ctxt, conv ctxt, pow_conv)
wenzelm@23252
   636
    val {main, ...} = semiring_normalizers_conv vars semiring ring dat ord
wenzelm@23252
   637
  in main end;
wenzelm@23252
   638
wenzelm@23252
   639
fun semiring_normalize_conv ctxt tm =
wenzelm@23252
   640
  (case NormalizerData.match ctxt tm of
wenzelm@23252
   641
    NONE => reflexive tm
chaieb@23330
   642
  | SOME res => semiring_normalize_wrapper ctxt res tm);
wenzelm@23252
   643
wenzelm@23252
   644
wenzelm@23252
   645
fun semiring_normalize_tac ctxt = SUBGOAL (fn (goal, i) =>
wenzelm@23252
   646
  rtac (semiring_normalize_conv ctxt
wenzelm@23252
   647
    (cterm_of (ProofContext.theory_of ctxt) (fst (Logic.dest_equals goal)))) i);
wenzelm@23252
   648
end;