src/HOL/Tools/Groebner_Basis/normalizer.ML
author haftmann
Thu May 06 16:53:35 2010 +0200 (2010-05-06)
changeset 36705 4a7709f041a8
parent 36704 9dd2fe596ace
child 36706 b6a47c7d6125
permissions -rw-r--r--
removed former algebra presimpset entirely
wenzelm@23252
     1
(*  Title:      HOL/Tools/Groebner_Basis/normalizer.ML
wenzelm@23252
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@36700
     3
haftmann@36700
     4
Normalization of expressions in semirings.
wenzelm@23252
     5
*)
wenzelm@23252
     6
wenzelm@23252
     7
signature NORMALIZER = 
wenzelm@23252
     8
sig
haftmann@36700
     9
  type entry
haftmann@36703
    10
  val get: Proof.context -> (thm * entry) list
haftmann@36700
    11
  val match: Proof.context -> cterm -> entry option
haftmann@36700
    12
  val del: attribute
haftmann@36700
    13
  val add: {semiring: cterm list * thm list, ring: cterm list * thm list, field: cterm list * thm list, idom: thm list, ideal: thm list}
haftmann@36700
    14
    -> attribute
haftmann@36700
    15
  val funs: thm -> {is_const: morphism -> cterm -> bool,
haftmann@36700
    16
    dest_const: morphism -> cterm -> Rat.rat,
haftmann@36700
    17
    mk_const: morphism -> ctyp -> Rat.rat -> cterm,
haftmann@36700
    18
    conv: morphism -> Proof.context -> cterm -> thm} -> declaration
haftmann@36700
    19
haftmann@36700
    20
  val semiring_normalize_conv : Proof.context -> conv
haftmann@36700
    21
  val semiring_normalize_ord_conv : Proof.context -> (cterm -> cterm -> bool) -> conv
haftmann@36700
    22
  val semiring_normalize_wrapper :  Proof.context -> entry -> conv
haftmann@36700
    23
  val semiring_normalize_ord_wrapper :  Proof.context -> entry ->
haftmann@36700
    24
    (cterm -> cterm -> bool) -> conv
haftmann@36700
    25
  val semiring_normalizers_conv :
haftmann@36700
    26
    cterm list -> cterm list * thm list -> cterm list * thm list -> cterm list * thm list ->
haftmann@36700
    27
      (cterm -> bool) * conv * conv * conv -> (cterm -> cterm -> bool) ->
haftmann@36700
    28
        {add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv}
haftmann@36700
    29
  val semiring_normalizers_ord_wrapper :  
haftmann@36700
    30
    Proof.context -> entry -> (cterm -> cterm -> bool) ->
chaieb@27222
    31
      {add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv}
haftmann@36700
    32
haftmann@36700
    33
  val setup: theory -> theory
wenzelm@23252
    34
end
wenzelm@23252
    35
wenzelm@23252
    36
structure Normalizer: NORMALIZER = 
wenzelm@23252
    37
struct
wenzelm@23559
    38
haftmann@36700
    39
(* data *)
haftmann@36700
    40
haftmann@36700
    41
type entry =
haftmann@36700
    42
 {vars: cterm list,
haftmann@36700
    43
  semiring: cterm list * thm list,
haftmann@36700
    44
  ring: cterm list * thm list,
haftmann@36700
    45
  field: cterm list * thm list,
haftmann@36700
    46
  idom: thm list,
haftmann@36700
    47
  ideal: thm list} *
haftmann@36700
    48
 {is_const: cterm -> bool,
haftmann@36700
    49
  dest_const: cterm -> Rat.rat,
haftmann@36700
    50
  mk_const: ctyp -> Rat.rat -> cterm,
haftmann@36700
    51
  conv: Proof.context -> cterm -> thm};
haftmann@36700
    52
haftmann@36700
    53
val eq_key = Thm.eq_thm;
haftmann@36700
    54
fun eq_data arg = eq_fst eq_key arg;
haftmann@36700
    55
haftmann@36700
    56
structure Data = Generic_Data
haftmann@36700
    57
(
haftmann@36705
    58
  type T = (thm * entry) list;
haftmann@36705
    59
  val empty = [];
haftmann@36700
    60
  val extend = I;
haftmann@36705
    61
  val merge = AList.merge eq_key (K true);
haftmann@36700
    62
);
haftmann@36700
    63
haftmann@36705
    64
val get = Data.get o Context.Proof;
haftmann@36700
    65
haftmann@36700
    66
haftmann@36700
    67
(* match data *)
haftmann@36700
    68
haftmann@36700
    69
fun match ctxt tm =
haftmann@36700
    70
  let
haftmann@36700
    71
    fun match_inst
haftmann@36700
    72
        ({vars, semiring = (sr_ops, sr_rules), 
haftmann@36700
    73
          ring = (r_ops, r_rules), field = (f_ops, f_rules), idom, ideal},
haftmann@36700
    74
         fns as {is_const, dest_const, mk_const, conv}) pat =
haftmann@36700
    75
       let
haftmann@36700
    76
        fun h instT =
haftmann@36700
    77
          let
haftmann@36700
    78
            val substT = Thm.instantiate (instT, []);
haftmann@36700
    79
            val substT_cterm = Drule.cterm_rule substT;
haftmann@36700
    80
haftmann@36700
    81
            val vars' = map substT_cterm vars;
haftmann@36700
    82
            val semiring' = (map substT_cterm sr_ops, map substT sr_rules);
haftmann@36700
    83
            val ring' = (map substT_cterm r_ops, map substT r_rules);
haftmann@36700
    84
            val field' = (map substT_cterm f_ops, map substT f_rules);
haftmann@36700
    85
            val idom' = map substT idom;
haftmann@36700
    86
            val ideal' = map substT ideal;
haftmann@36700
    87
haftmann@36700
    88
            val result = ({vars = vars', semiring = semiring', 
haftmann@36700
    89
                           ring = ring', field = field', idom = idom', ideal = ideal'}, fns);
haftmann@36700
    90
          in SOME result end
haftmann@36700
    91
      in (case try Thm.match (pat, tm) of
haftmann@36700
    92
           NONE => NONE
haftmann@36700
    93
         | SOME (instT, _) => h instT)
haftmann@36700
    94
      end;
haftmann@36700
    95
haftmann@36700
    96
    fun match_struct (_,
haftmann@36700
    97
        entry as ({semiring = (sr_ops, _), ring = (r_ops, _), field = (f_ops, _), ...}, _): entry) =
haftmann@36700
    98
      get_first (match_inst entry) (sr_ops @ r_ops @ f_ops);
haftmann@36704
    99
  in get_first match_struct (get ctxt) end;
haftmann@36700
   100
haftmann@36700
   101
haftmann@36700
   102
(* logical content *)
haftmann@36700
   103
haftmann@36700
   104
val semiringN = "semiring";
haftmann@36700
   105
val ringN = "ring";
haftmann@36700
   106
val idomN = "idom";
haftmann@36700
   107
val idealN = "ideal";
haftmann@36700
   108
val fieldN = "field";
haftmann@36700
   109
haftmann@36700
   110
fun undefined _ = raise Match;
haftmann@36700
   111
haftmann@36705
   112
fun del_data key = remove eq_data (key, []);
haftmann@36700
   113
haftmann@36700
   114
val del = Thm.declaration_attribute (Data.map o del_data);
haftmann@36700
   115
haftmann@36700
   116
fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), 
haftmann@36700
   117
         field = (f_ops, f_rules), idom, ideal} =
haftmann@36700
   118
  Thm.declaration_attribute (fn key => fn context => context |> Data.map
haftmann@36700
   119
    let
haftmann@36700
   120
      val ctxt = Context.proof_of context;
haftmann@36700
   121
haftmann@36700
   122
      fun check kind name xs n =
haftmann@36700
   123
        null xs orelse length xs = n orelse
haftmann@36700
   124
        error ("Expected " ^ string_of_int n ^ " " ^ kind ^ " for " ^ name);
haftmann@36700
   125
      val check_ops = check "operations";
haftmann@36700
   126
      val check_rules = check "rules";
haftmann@36700
   127
haftmann@36700
   128
      val _ =
haftmann@36700
   129
        check_ops semiringN sr_ops 5 andalso
haftmann@36700
   130
        check_rules semiringN sr_rules 37 andalso
haftmann@36700
   131
        check_ops ringN r_ops 2 andalso
haftmann@36700
   132
        check_rules ringN r_rules 2 andalso
haftmann@36700
   133
        check_ops fieldN f_ops 2 andalso
haftmann@36700
   134
        check_rules fieldN f_rules 2 andalso
haftmann@36700
   135
        check_rules idomN idom 2;
haftmann@36700
   136
haftmann@36700
   137
      val mk_meta = Local_Defs.meta_rewrite_rule ctxt;
haftmann@36700
   138
      val sr_rules' = map mk_meta sr_rules;
haftmann@36700
   139
      val r_rules' = map mk_meta r_rules;
haftmann@36700
   140
      val f_rules' = map mk_meta f_rules;
haftmann@36700
   141
haftmann@36700
   142
      fun rule i = nth sr_rules' (i - 1);
haftmann@36700
   143
haftmann@36700
   144
      val (cx, cy) = Thm.dest_binop (hd sr_ops);
haftmann@36700
   145
      val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
haftmann@36700
   146
      val cn = rule 36 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
haftmann@36700
   147
      val ((clx, crx), (cly, cry)) =
haftmann@36700
   148
        rule 13 |> Thm.rhs_of |> Thm.dest_binop |> pairself Thm.dest_binop;
haftmann@36700
   149
      val ((ca, cb), (cc, cd)) =
haftmann@36700
   150
        rule 20 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_binop;
haftmann@36700
   151
      val cm = rule 1 |> Thm.rhs_of |> Thm.dest_arg;
haftmann@36700
   152
      val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg;
haftmann@36700
   153
haftmann@36700
   154
      val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry];
haftmann@36700
   155
      val semiring = (sr_ops, sr_rules');
haftmann@36700
   156
      val ring = (r_ops, r_rules');
haftmann@36700
   157
      val field = (f_ops, f_rules');
haftmann@36700
   158
      val ideal' = map (symmetric o mk_meta) ideal
haftmann@36700
   159
    in
haftmann@36700
   160
      del_data key #>
haftmann@36705
   161
      cons (key, ({vars = vars, semiring = semiring, 
haftmann@36700
   162
                          ring = ring, field = field, idom = idom, ideal = ideal'},
haftmann@36700
   163
             {is_const = undefined, dest_const = undefined, mk_const = undefined,
haftmann@36705
   164
             conv = undefined}))
haftmann@36700
   165
    end);
haftmann@36700
   166
haftmann@36700
   167
haftmann@36700
   168
(* extra-logical functions *)
haftmann@36700
   169
haftmann@36700
   170
fun funs raw_key {is_const, dest_const, mk_const, conv} phi = 
haftmann@36705
   171
 Data.map (fn data =>
haftmann@36700
   172
  let
haftmann@36700
   173
    val key = Morphism.thm phi raw_key;
haftmann@36700
   174
    val _ = AList.defined eq_key data key orelse
haftmann@36700
   175
      raise THM ("No data entry for structure key", 0, [key]);
haftmann@36700
   176
    val fns = {is_const = is_const phi, dest_const = dest_const phi,
haftmann@36700
   177
      mk_const = mk_const phi, conv = conv phi};
haftmann@36700
   178
  in AList.map_entry eq_key key (apsnd (K fns)) data end);
haftmann@36700
   179
haftmann@36700
   180
haftmann@36700
   181
(* concrete syntax *)
haftmann@36700
   182
haftmann@36700
   183
local
haftmann@36700
   184
haftmann@36700
   185
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
haftmann@36700
   186
fun keyword2 k1 k2 = Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.colon) >> K ();
haftmann@36700
   187
fun keyword3 k1 k2 k3 =
haftmann@36700
   188
  Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.$$$ k3 -- Args.colon) >> K ();
haftmann@36700
   189
haftmann@36700
   190
val opsN = "ops";
haftmann@36700
   191
val rulesN = "rules";
haftmann@36700
   192
haftmann@36700
   193
val normN = "norm";
haftmann@36700
   194
val constN = "const";
haftmann@36700
   195
val delN = "del";
haftmann@36700
   196
haftmann@36700
   197
val any_keyword =
haftmann@36700
   198
  keyword2 semiringN opsN || keyword2 semiringN rulesN ||
haftmann@36700
   199
  keyword2 ringN opsN || keyword2 ringN rulesN ||
haftmann@36700
   200
  keyword2 fieldN opsN || keyword2 fieldN rulesN ||
haftmann@36700
   201
  keyword2 idomN rulesN || keyword2 idealN rulesN;
haftmann@36700
   202
haftmann@36700
   203
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@36700
   204
val terms = thms >> map Drule.dest_term;
haftmann@36700
   205
haftmann@36700
   206
fun optional scan = Scan.optional scan [];
haftmann@36700
   207
haftmann@36700
   208
in
haftmann@36700
   209
haftmann@36700
   210
val normalizer_setup =
haftmann@36700
   211
  Attrib.setup @{binding normalizer}
haftmann@36700
   212
    (Scan.lift (Args.$$$ delN >> K del) ||
haftmann@36700
   213
      ((keyword2 semiringN opsN |-- terms) --
haftmann@36700
   214
       (keyword2 semiringN rulesN |-- thms)) --
haftmann@36700
   215
      (optional (keyword2 ringN opsN |-- terms) --
haftmann@36700
   216
       optional (keyword2 ringN rulesN |-- thms)) --
haftmann@36700
   217
      (optional (keyword2 fieldN opsN |-- terms) --
haftmann@36700
   218
       optional (keyword2 fieldN rulesN |-- thms)) --
haftmann@36700
   219
      optional (keyword2 idomN rulesN |-- thms) --
haftmann@36700
   220
      optional (keyword2 idealN rulesN |-- thms)
haftmann@36700
   221
      >> (fn ((((sr, r), f), id), idl) => 
haftmann@36700
   222
             add {semiring = sr, ring = r, field = f, idom = id, ideal = idl}))
haftmann@36700
   223
    "semiring normalizer data";
haftmann@36700
   224
haftmann@36700
   225
end;
haftmann@36700
   226
chaieb@25253
   227
open Conv;
wenzelm@23252
   228
wenzelm@23252
   229
(* Very basic stuff for terms *)
chaieb@25253
   230
chaieb@25253
   231
fun is_comb ct =
chaieb@25253
   232
  (case Thm.term_of ct of
chaieb@25253
   233
    _ $ _ => true
chaieb@25253
   234
  | _ => false);
chaieb@25253
   235
chaieb@25253
   236
val concl = Thm.cprop_of #> Thm.dest_arg;
chaieb@25253
   237
chaieb@25253
   238
fun is_binop ct ct' =
chaieb@25253
   239
  (case Thm.term_of ct' of
chaieb@25253
   240
    c $ _ $ _ => term_of ct aconv c
chaieb@25253
   241
  | _ => false);
chaieb@25253
   242
chaieb@25253
   243
fun dest_binop ct ct' =
chaieb@25253
   244
  if is_binop ct ct' then Thm.dest_binop ct'
chaieb@25253
   245
  else raise CTERM ("dest_binop: bad binop", [ct, ct'])
chaieb@25253
   246
chaieb@25253
   247
fun inst_thm inst = Thm.instantiate ([], inst);
chaieb@25253
   248
wenzelm@23252
   249
val dest_numeral = term_of #> HOLogic.dest_number #> snd;
wenzelm@23252
   250
val is_numeral = can dest_numeral;
wenzelm@23252
   251
wenzelm@23252
   252
val numeral01_conv = Simplifier.rewrite
haftmann@25481
   253
                         (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}, @{thm numeral_0_eq_0}]);
wenzelm@23252
   254
val zero1_numeral_conv = 
haftmann@25481
   255
 Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1} RS sym, @{thm numeral_0_eq_0} RS sym]);
wenzelm@23580
   256
fun zerone_conv cv = zero1_numeral_conv then_conv cv then_conv numeral01_conv;
wenzelm@23252
   257
val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"},
wenzelm@23252
   258
                @{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, 
wenzelm@23252
   259
                @{thm "less_nat_number_of"}];
haftmann@36700
   260
wenzelm@23252
   261
val nat_add_conv = 
wenzelm@23252
   262
 zerone_conv 
wenzelm@23252
   263
  (Simplifier.rewrite 
wenzelm@23252
   264
    (HOL_basic_ss 
haftmann@25481
   265
       addsimps @{thms arith_simps} @ natarith @ @{thms rel_simps}
wenzelm@35410
   266
             @ [@{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc},
nipkow@31790
   267
                 @{thm add_number_of_left}, @{thm Suc_eq_plus1}]
haftmann@25481
   268
             @ map (fn th => th RS sym) @{thms numerals}));
wenzelm@23252
   269
wenzelm@23252
   270
val zeron_tm = @{cterm "0::nat"};
wenzelm@23252
   271
val onen_tm  = @{cterm "1::nat"};
wenzelm@23252
   272
val true_tm = @{cterm "True"};
wenzelm@23252
   273
wenzelm@23252
   274
wenzelm@23252
   275
(* The main function! *)
chaieb@30866
   276
fun semiring_normalizers_conv vars (sr_ops, sr_rules) (r_ops, r_rules) (f_ops, f_rules)
wenzelm@23252
   277
  (is_semiring_constant, semiring_add_conv, semiring_mul_conv, semiring_pow_conv) =
wenzelm@23252
   278
let
wenzelm@23252
   279
wenzelm@23252
   280
val [pthm_02, pthm_03, pthm_04, pthm_05, pthm_07, pthm_08,
wenzelm@23252
   281
     pthm_09, pthm_10, pthm_11, pthm_12, pthm_13, pthm_14, pthm_15, pthm_16,
wenzelm@23252
   282
     pthm_17, pthm_18, pthm_19, pthm_21, pthm_22, pthm_23, pthm_24,
wenzelm@23252
   283
     pthm_25, pthm_26, pthm_27, pthm_28, pthm_29, pthm_30, pthm_31, pthm_32,
wenzelm@23252
   284
     pthm_33, pthm_34, pthm_35, pthm_36, pthm_37, pthm_38,pthm_39,pthm_40] = sr_rules;
wenzelm@23252
   285
wenzelm@23252
   286
val [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry] = vars;
wenzelm@23252
   287
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops;
wenzelm@23252
   288
val [add_tm, mul_tm, pow_tm] = map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat];
wenzelm@23252
   289
wenzelm@23252
   290
val dest_add = dest_binop add_tm
wenzelm@23252
   291
val dest_mul = dest_binop mul_tm
wenzelm@23252
   292
fun dest_pow tm =
wenzelm@23252
   293
 let val (l,r) = dest_binop pow_tm tm
wenzelm@23252
   294
 in if is_numeral r then (l,r) else raise CTERM ("dest_pow",[tm])
wenzelm@23252
   295
 end;
wenzelm@23252
   296
val is_add = is_binop add_tm
wenzelm@23252
   297
val is_mul = is_binop mul_tm
wenzelm@23252
   298
fun is_pow tm = is_binop pow_tm tm andalso is_numeral(Thm.dest_arg tm);
wenzelm@23252
   299
wenzelm@23252
   300
val (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub,cx',cy') =
wenzelm@23252
   301
  (case (r_ops, r_rules) of
chaieb@30866
   302
    ([sub_pat, neg_pat], [neg_mul, sub_add]) =>
wenzelm@23252
   303
      let
wenzelm@23252
   304
        val sub_tm = Thm.dest_fun (Thm.dest_fun sub_pat)
wenzelm@23252
   305
        val neg_tm = Thm.dest_fun neg_pat
wenzelm@23252
   306
        val dest_sub = dest_binop sub_tm
wenzelm@23252
   307
        val is_sub = is_binop sub_tm
wenzelm@23252
   308
      in (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub, neg_mul |> concl |> Thm.dest_arg,
wenzelm@23252
   309
          sub_add |> concl |> Thm.dest_arg |> Thm.dest_arg)
chaieb@30866
   310
      end
chaieb@30866
   311
    | _ => (TrueI, TrueI, true_tm, true_tm, (fn t => (t,t)), K false, true_tm, true_tm));
chaieb@30866
   312
chaieb@30866
   313
val (divide_inverse, inverse_divide, divide_tm, inverse_tm, is_divide) = 
chaieb@30866
   314
  (case (f_ops, f_rules) of 
chaieb@30866
   315
   ([divide_pat, inverse_pat], [div_inv, inv_div]) => 
chaieb@30866
   316
     let val div_tm = funpow 2 Thm.dest_fun divide_pat
chaieb@30866
   317
         val inv_tm = Thm.dest_fun inverse_pat
chaieb@30866
   318
     in (div_inv, inv_div, div_tm, inv_tm, is_binop div_tm)
chaieb@30866
   319
     end
chaieb@30866
   320
   | _ => (TrueI, TrueI, true_tm, true_tm, K false));
chaieb@30866
   321
wenzelm@23252
   322
in fn variable_order =>
wenzelm@23252
   323
 let
wenzelm@23252
   324
wenzelm@23252
   325
(* Conversion for "x^n * x^m", with either x^n = x and/or x^m = x possible.  *)
wenzelm@23252
   326
(* Also deals with "const * const", but both terms must involve powers of    *)
wenzelm@23252
   327
(* the same variable, or both be constants, or behaviour may be incorrect.   *)
wenzelm@23252
   328
wenzelm@23252
   329
 fun powvar_mul_conv tm =
wenzelm@23252
   330
  let
wenzelm@23252
   331
  val (l,r) = dest_mul tm
wenzelm@23252
   332
  in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   333
     then semiring_mul_conv tm
wenzelm@23252
   334
     else
wenzelm@23252
   335
      ((let
wenzelm@23252
   336
         val (lx,ln) = dest_pow l
wenzelm@23252
   337
        in
wenzelm@23252
   338
         ((let val (rx,rn) = dest_pow r
wenzelm@23252
   339
               val th1 = inst_thm [(cx,lx),(cp,ln),(cq,rn)] pthm_29
wenzelm@23252
   340
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   341
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   342
           handle CTERM _ =>
wenzelm@23252
   343
            (let val th1 = inst_thm [(cx,lx),(cq,ln)] pthm_31
wenzelm@23252
   344
                 val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   345
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)) end)
wenzelm@23252
   346
       handle CTERM _ =>
wenzelm@23252
   347
           ((let val (rx,rn) = dest_pow r
wenzelm@23252
   348
                val th1 = inst_thm [(cx,rx),(cq,rn)] pthm_30
wenzelm@23252
   349
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   350
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   351
           handle CTERM _ => inst_thm [(cx,l)] pthm_32
wenzelm@23252
   352
wenzelm@23252
   353
))
wenzelm@23252
   354
 end;
wenzelm@23252
   355
wenzelm@23252
   356
(* Remove "1 * m" from a monomial, and just leave m.                         *)
wenzelm@23252
   357
wenzelm@23252
   358
 fun monomial_deone th =
wenzelm@23252
   359
       (let val (l,r) = dest_mul(concl th) in
wenzelm@23252
   360
           if l aconvc one_tm
wenzelm@23252
   361
          then transitive th (inst_thm [(ca,r)] pthm_13)  else th end)
wenzelm@23252
   362
       handle CTERM _ => th;
wenzelm@23252
   363
wenzelm@23252
   364
(* Conversion for "(monomial)^n", where n is a numeral.                      *)
wenzelm@23252
   365
wenzelm@23252
   366
 val monomial_pow_conv =
wenzelm@23252
   367
  let
wenzelm@23252
   368
   fun monomial_pow tm bod ntm =
wenzelm@23252
   369
    if not(is_comb bod)
wenzelm@23252
   370
    then reflexive tm
wenzelm@23252
   371
    else
wenzelm@23252
   372
     if is_semiring_constant bod
wenzelm@23252
   373
     then semiring_pow_conv tm
wenzelm@23252
   374
     else
wenzelm@23252
   375
      let
wenzelm@23252
   376
      val (lopr,r) = Thm.dest_comb bod
wenzelm@23252
   377
      in if not(is_comb lopr)
wenzelm@23252
   378
         then reflexive tm
wenzelm@23252
   379
        else
wenzelm@23252
   380
          let
wenzelm@23252
   381
          val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   382
         in
wenzelm@23252
   383
           if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   384
          then
wenzelm@23252
   385
            let val th1 = inst_thm [(cx,l),(cp,r),(cq,ntm)] pthm_34
wenzelm@23252
   386
                val (l,r) = Thm.dest_comb(concl th1)
haftmann@36700
   387
           in transitive th1 (Drule.arg_cong_rule l (nat_add_conv r))
wenzelm@23252
   388
           end
wenzelm@23252
   389
           else
wenzelm@23252
   390
            if opr aconvc mul_tm
wenzelm@23252
   391
            then
wenzelm@23252
   392
             let
wenzelm@23252
   393
              val th1 = inst_thm [(cx,l),(cy,r),(cq,ntm)] pthm_33
wenzelm@23252
   394
             val (xy,z) = Thm.dest_comb(concl th1)
wenzelm@23252
   395
              val (x,y) = Thm.dest_comb xy
wenzelm@23252
   396
              val thl = monomial_pow y l ntm
wenzelm@23252
   397
              val thr = monomial_pow z r ntm
wenzelm@23252
   398
             in transitive th1 (combination (Drule.arg_cong_rule x thl) thr)
wenzelm@23252
   399
             end
wenzelm@23252
   400
             else reflexive tm
wenzelm@23252
   401
          end
wenzelm@23252
   402
      end
wenzelm@23252
   403
  in fn tm =>
wenzelm@23252
   404
   let
wenzelm@23252
   405
    val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   406
    val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   407
   in if not (opr aconvc pow_tm) orelse not(is_numeral r)
wenzelm@23252
   408
      then raise CTERM ("monomial_pow_conv", [tm])
wenzelm@23252
   409
      else if r aconvc zeron_tm
wenzelm@23252
   410
      then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   411
      else if r aconvc onen_tm
wenzelm@23252
   412
      then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   413
      else monomial_deone(monomial_pow tm l r)
wenzelm@23252
   414
   end
wenzelm@23252
   415
  end;
wenzelm@23252
   416
wenzelm@23252
   417
(* Multiplication of canonical monomials.                                    *)
wenzelm@23252
   418
 val monomial_mul_conv =
wenzelm@23252
   419
  let
wenzelm@23252
   420
   fun powvar tm =
wenzelm@23252
   421
    if is_semiring_constant tm then one_tm
wenzelm@23252
   422
    else
wenzelm@23252
   423
     ((let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   424
           val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   425
       in if opr aconvc pow_tm andalso is_numeral r then l 
wenzelm@23252
   426
          else raise CTERM ("monomial_mul_conv",[tm]) end)
wenzelm@23252
   427
     handle CTERM _ => tm)   (* FIXME !? *)
wenzelm@23252
   428
   fun  vorder x y =
wenzelm@23252
   429
    if x aconvc y then 0
wenzelm@23252
   430
    else
wenzelm@23252
   431
     if x aconvc one_tm then ~1
wenzelm@23252
   432
     else if y aconvc one_tm then 1
wenzelm@23252
   433
      else if variable_order x y then ~1 else 1
wenzelm@23252
   434
   fun monomial_mul tm l r =
wenzelm@23252
   435
    ((let val (lx,ly) = dest_mul l val vl = powvar lx
wenzelm@23252
   436
      in
wenzelm@23252
   437
      ((let
wenzelm@23252
   438
        val (rx,ry) = dest_mul r
wenzelm@23252
   439
         val vr = powvar rx
wenzelm@23252
   440
         val ord = vorder vl vr
wenzelm@23252
   441
        in
wenzelm@23252
   442
         if ord = 0
wenzelm@23252
   443
        then
wenzelm@23252
   444
          let
wenzelm@23252
   445
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] pthm_15
wenzelm@23252
   446
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   447
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   448
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   449
             val th3 = transitive th1 th2
wenzelm@23252
   450
              val  (tm5,tm6) = Thm.dest_comb(concl th3)
wenzelm@23252
   451
              val  (tm7,tm8) = Thm.dest_comb tm6
wenzelm@23252
   452
             val  th4 = monomial_mul tm6 (Thm.dest_arg tm7) tm8
wenzelm@23252
   453
         in  transitive th3 (Drule.arg_cong_rule tm5 th4)
wenzelm@23252
   454
         end
wenzelm@23252
   455
         else
wenzelm@23252
   456
          let val th0 = if ord < 0 then pthm_16 else pthm_17
wenzelm@23252
   457
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] th0
wenzelm@23252
   458
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   459
             val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   460
         in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   461
         end
wenzelm@23252
   462
        end)
wenzelm@23252
   463
       handle CTERM _ =>
wenzelm@23252
   464
        (let val vr = powvar r val ord = vorder vl vr
wenzelm@23252
   465
        in
wenzelm@23252
   466
          if ord = 0 then
wenzelm@23252
   467
           let
wenzelm@23252
   468
           val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_18
wenzelm@23252
   469
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   470
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   471
           val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   472
          in transitive th1 th2
wenzelm@23252
   473
          end
wenzelm@23252
   474
          else
wenzelm@23252
   475
          if ord < 0 then
wenzelm@23252
   476
            let val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_19
wenzelm@23252
   477
                val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   478
                val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   479
           in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   480
           end
wenzelm@23252
   481
           else inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   482
        end)) end)
wenzelm@23252
   483
     handle CTERM _ =>
wenzelm@23252
   484
      (let val vl = powvar l in
wenzelm@23252
   485
        ((let
wenzelm@23252
   486
          val (rx,ry) = dest_mul r
wenzelm@23252
   487
          val vr = powvar rx
wenzelm@23252
   488
           val ord = vorder vl vr
wenzelm@23252
   489
         in if ord = 0 then
wenzelm@23252
   490
              let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_21
wenzelm@23252
   491
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   492
                 val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   493
             in transitive th1 (Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2)
wenzelm@23252
   494
             end
wenzelm@23252
   495
             else if ord > 0 then
wenzelm@23252
   496
                 let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_22
wenzelm@23252
   497
                     val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   498
                    val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   499
                in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   500
                end
wenzelm@23252
   501
             else reflexive tm
wenzelm@23252
   502
         end)
wenzelm@23252
   503
        handle CTERM _ =>
wenzelm@23252
   504
          (let val vr = powvar r
wenzelm@23252
   505
               val  ord = vorder vl vr
wenzelm@23252
   506
          in if ord = 0 then powvar_mul_conv tm
wenzelm@23252
   507
              else if ord > 0 then inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   508
              else reflexive tm
wenzelm@23252
   509
          end)) end))
wenzelm@23252
   510
  in fn tm => let val (l,r) = dest_mul tm in monomial_deone(monomial_mul tm l r)
wenzelm@23252
   511
             end
wenzelm@23252
   512
  end;
wenzelm@23252
   513
(* Multiplication by monomial of a polynomial.                               *)
wenzelm@23252
   514
wenzelm@23252
   515
 val polynomial_monomial_mul_conv =
wenzelm@23252
   516
  let
wenzelm@23252
   517
   fun pmm_conv tm =
wenzelm@23252
   518
    let val (l,r) = dest_mul tm
wenzelm@23252
   519
    in
wenzelm@23252
   520
    ((let val (y,z) = dest_add r
wenzelm@23252
   521
          val th1 = inst_thm [(cx,l),(cy,y),(cz,z)] pthm_37
wenzelm@23252
   522
          val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   523
          val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   524
          val th2 = combination (Drule.arg_cong_rule tm3 (monomial_mul_conv tm4)) (pmm_conv tm2)
wenzelm@23252
   525
      in transitive th1 th2
wenzelm@23252
   526
      end)
wenzelm@23252
   527
     handle CTERM _ => monomial_mul_conv tm)
wenzelm@23252
   528
   end
wenzelm@23252
   529
 in pmm_conv
wenzelm@23252
   530
 end;
wenzelm@23252
   531
wenzelm@23252
   532
(* Addition of two monomials identical except for constant multiples.        *)
wenzelm@23252
   533
wenzelm@23252
   534
fun monomial_add_conv tm =
wenzelm@23252
   535
 let val (l,r) = dest_add tm
wenzelm@23252
   536
 in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   537
    then semiring_add_conv tm
wenzelm@23252
   538
    else
wenzelm@23252
   539
     let val th1 =
wenzelm@23252
   540
           if is_mul l andalso is_semiring_constant(Thm.dest_arg1 l)
wenzelm@23252
   541
           then if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) then
wenzelm@23252
   542
                    inst_thm [(ca,Thm.dest_arg1 l),(cm,Thm.dest_arg r), (cb,Thm.dest_arg1 r)] pthm_02
wenzelm@23252
   543
                else inst_thm [(ca,Thm.dest_arg1 l),(cm,r)] pthm_03
wenzelm@23252
   544
           else if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r)
wenzelm@23252
   545
           then inst_thm [(cm,l),(ca,Thm.dest_arg1 r)] pthm_04
wenzelm@23252
   546
           else inst_thm [(cm,r)] pthm_05
wenzelm@23252
   547
         val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   548
         val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   549
         val th2 = Drule.arg_cong_rule tm3 (semiring_add_conv tm4)
wenzelm@23252
   550
         val th3 = transitive th1 (Drule.fun_cong_rule th2 tm2)
wenzelm@23252
   551
         val tm5 = concl th3
wenzelm@23252
   552
      in
wenzelm@23252
   553
      if (Thm.dest_arg1 tm5) aconvc zero_tm
wenzelm@23252
   554
      then transitive th3 (inst_thm [(ca,Thm.dest_arg tm5)] pthm_11)
wenzelm@23252
   555
      else monomial_deone th3
wenzelm@23252
   556
     end
wenzelm@23252
   557
 end;
wenzelm@23252
   558
wenzelm@23252
   559
(* Ordering on monomials.                                                    *)
wenzelm@23252
   560
wenzelm@23252
   561
fun striplist dest =
wenzelm@23252
   562
 let fun strip x acc =
wenzelm@23252
   563
   ((let val (l,r) = dest x in
wenzelm@23252
   564
        strip l (strip r acc) end)
wenzelm@23252
   565
    handle CTERM _ => x::acc)    (* FIXME !? *)
wenzelm@23252
   566
 in fn x => strip x []
wenzelm@23252
   567
 end;
wenzelm@23252
   568
wenzelm@23252
   569
wenzelm@23252
   570
fun powervars tm =
wenzelm@23252
   571
 let val ptms = striplist dest_mul tm
wenzelm@23252
   572
 in if is_semiring_constant (hd ptms) then tl ptms else ptms
wenzelm@23252
   573
 end;
wenzelm@23252
   574
val num_0 = 0;
wenzelm@23252
   575
val num_1 = 1;
wenzelm@23252
   576
fun dest_varpow tm =
wenzelm@23252
   577
 ((let val (x,n) = dest_pow tm in (x,dest_numeral n) end)
wenzelm@23252
   578
   handle CTERM _ =>
wenzelm@23252
   579
   (tm,(if is_semiring_constant tm then num_0 else num_1)));
wenzelm@23252
   580
wenzelm@23252
   581
val morder =
wenzelm@23252
   582
 let fun lexorder l1 l2 =
wenzelm@23252
   583
  case (l1,l2) of
wenzelm@23252
   584
    ([],[]) => 0
wenzelm@23252
   585
  | (vps,[]) => ~1
wenzelm@23252
   586
  | ([],vps) => 1
wenzelm@23252
   587
  | (((x1,n1)::vs1),((x2,n2)::vs2)) =>
wenzelm@23252
   588
     if variable_order x1 x2 then 1
wenzelm@23252
   589
     else if variable_order x2 x1 then ~1
wenzelm@23252
   590
     else if n1 < n2 then ~1
wenzelm@23252
   591
     else if n2 < n1 then 1
wenzelm@23252
   592
     else lexorder vs1 vs2
wenzelm@23252
   593
 in fn tm1 => fn tm2 =>
wenzelm@23252
   594
  let val vdegs1 = map dest_varpow (powervars tm1)
wenzelm@23252
   595
      val vdegs2 = map dest_varpow (powervars tm2)
wenzelm@33002
   596
      val deg1 = fold (Integer.add o snd) vdegs1 num_0
wenzelm@33002
   597
      val deg2 = fold (Integer.add o snd) vdegs2 num_0
wenzelm@23252
   598
  in if deg1 < deg2 then ~1 else if deg1 > deg2 then 1
wenzelm@23252
   599
                            else lexorder vdegs1 vdegs2
wenzelm@23252
   600
  end
wenzelm@23252
   601
 end;
wenzelm@23252
   602
wenzelm@23252
   603
(* Addition of two polynomials.                                              *)
wenzelm@23252
   604
wenzelm@23252
   605
val polynomial_add_conv =
wenzelm@23252
   606
 let
wenzelm@23252
   607
 fun dezero_rule th =
wenzelm@23252
   608
  let
wenzelm@23252
   609
   val tm = concl th
wenzelm@23252
   610
  in
wenzelm@23252
   611
   if not(is_add tm) then th else
wenzelm@23252
   612
   let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   613
       val l = Thm.dest_arg lopr
wenzelm@23252
   614
   in
wenzelm@23252
   615
    if l aconvc zero_tm
wenzelm@23252
   616
    then transitive th (inst_thm [(ca,r)] pthm_07)   else
wenzelm@23252
   617
        if r aconvc zero_tm
wenzelm@23252
   618
        then transitive th (inst_thm [(ca,l)] pthm_08)  else th
wenzelm@23252
   619
   end
wenzelm@23252
   620
  end
wenzelm@23252
   621
 fun padd tm =
wenzelm@23252
   622
  let
wenzelm@23252
   623
   val (l,r) = dest_add tm
wenzelm@23252
   624
  in
wenzelm@23252
   625
   if l aconvc zero_tm then inst_thm [(ca,r)] pthm_07
wenzelm@23252
   626
   else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_08
wenzelm@23252
   627
   else
wenzelm@23252
   628
    if is_add l
wenzelm@23252
   629
    then
wenzelm@23252
   630
     let val (a,b) = dest_add l
wenzelm@23252
   631
     in
wenzelm@23252
   632
     if is_add r then
wenzelm@23252
   633
      let val (c,d) = dest_add r
wenzelm@23252
   634
          val ord = morder a c
wenzelm@23252
   635
      in
wenzelm@23252
   636
       if ord = 0 then
wenzelm@23252
   637
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,c),(cd,d)] pthm_23
wenzelm@23252
   638
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   639
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   640
            val th2 = Drule.arg_cong_rule tm3 (monomial_add_conv tm4)
wenzelm@23252
   641
        in dezero_rule (transitive th1 (combination th2 (padd tm2)))
wenzelm@23252
   642
        end
wenzelm@23252
   643
       else (* ord <> 0*)
wenzelm@23252
   644
        let val th1 =
wenzelm@23252
   645
                if ord > 0 then inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   646
                else inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   647
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   648
        in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   649
        end
wenzelm@23252
   650
      end
wenzelm@23252
   651
     else (* not (is_add r)*)
wenzelm@23252
   652
      let val ord = morder a r
wenzelm@23252
   653
      in
wenzelm@23252
   654
       if ord = 0 then
wenzelm@23252
   655
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_26
wenzelm@23252
   656
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   657
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   658
            val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   659
        in dezero_rule (transitive th1 th2)
wenzelm@23252
   660
        end
wenzelm@23252
   661
       else (* ord <> 0*)
wenzelm@23252
   662
        if ord > 0 then
wenzelm@23252
   663
          let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   664
              val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   665
          in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   666
          end
wenzelm@23252
   667
        else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   668
      end
wenzelm@23252
   669
    end
wenzelm@23252
   670
   else (* not (is_add l)*)
wenzelm@23252
   671
    if is_add r then
wenzelm@23252
   672
      let val (c,d) = dest_add r
wenzelm@23252
   673
          val  ord = morder l c
wenzelm@23252
   674
      in
wenzelm@23252
   675
       if ord = 0 then
wenzelm@23252
   676
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_28
wenzelm@23252
   677
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   678
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   679
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   680
         in dezero_rule (transitive th1 th2)
wenzelm@23252
   681
         end
wenzelm@23252
   682
       else
wenzelm@23252
   683
        if ord > 0 then reflexive tm
wenzelm@23252
   684
        else
wenzelm@23252
   685
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   686
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   687
         in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   688
         end
wenzelm@23252
   689
      end
wenzelm@23252
   690
    else
wenzelm@23252
   691
     let val ord = morder l r
wenzelm@23252
   692
     in
wenzelm@23252
   693
      if ord = 0 then monomial_add_conv tm
wenzelm@23252
   694
      else if ord > 0 then dezero_rule(reflexive tm)
wenzelm@23252
   695
      else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   696
     end
wenzelm@23252
   697
  end
wenzelm@23252
   698
 in padd
wenzelm@23252
   699
 end;
wenzelm@23252
   700
wenzelm@23252
   701
(* Multiplication of two polynomials.                                        *)
wenzelm@23252
   702
wenzelm@23252
   703
val polynomial_mul_conv =
wenzelm@23252
   704
 let
wenzelm@23252
   705
  fun pmul tm =
wenzelm@23252
   706
   let val (l,r) = dest_mul tm
wenzelm@23252
   707
   in
wenzelm@23252
   708
    if not(is_add l) then polynomial_monomial_mul_conv tm
wenzelm@23252
   709
    else
wenzelm@23252
   710
     if not(is_add r) then
wenzelm@23252
   711
      let val th1 = inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   712
      in transitive th1 (polynomial_monomial_mul_conv(concl th1))
wenzelm@23252
   713
      end
wenzelm@23252
   714
     else
wenzelm@23252
   715
       let val (a,b) = dest_add l
wenzelm@23252
   716
           val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_10
wenzelm@23252
   717
           val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   718
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   719
           val th2 = Drule.arg_cong_rule tm3 (polynomial_monomial_mul_conv tm4)
wenzelm@23252
   720
           val th3 = transitive th1 (combination th2 (pmul tm2))
wenzelm@23252
   721
       in transitive th3 (polynomial_add_conv (concl th3))
wenzelm@23252
   722
       end
wenzelm@23252
   723
   end
wenzelm@23252
   724
 in fn tm =>
wenzelm@23252
   725
   let val (l,r) = dest_mul tm
wenzelm@23252
   726
   in
wenzelm@23252
   727
    if l aconvc zero_tm then inst_thm [(ca,r)] pthm_11
wenzelm@23252
   728
    else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_12
wenzelm@23252
   729
    else if l aconvc one_tm then inst_thm [(ca,r)] pthm_13
wenzelm@23252
   730
    else if r aconvc one_tm then inst_thm [(ca,l)] pthm_14
wenzelm@23252
   731
    else pmul tm
wenzelm@23252
   732
   end
wenzelm@23252
   733
 end;
wenzelm@23252
   734
wenzelm@23252
   735
(* Power of polynomial (optimized for the monomial and trivial cases).       *)
wenzelm@23252
   736
wenzelm@23580
   737
fun num_conv n =
wenzelm@23580
   738
  nat_add_conv (Thm.capply @{cterm Suc} (Numeral.mk_cnumber @{ctyp nat} (dest_numeral n - 1)))
wenzelm@23580
   739
  |> Thm.symmetric;
wenzelm@23252
   740
wenzelm@23252
   741
wenzelm@23252
   742
val polynomial_pow_conv =
wenzelm@23252
   743
 let
wenzelm@23252
   744
  fun ppow tm =
wenzelm@23252
   745
    let val (l,n) = dest_pow tm
wenzelm@23252
   746
    in
wenzelm@23252
   747
     if n aconvc zeron_tm then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   748
     else if n aconvc onen_tm then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   749
     else
wenzelm@23252
   750
         let val th1 = num_conv n
wenzelm@23252
   751
             val th2 = inst_thm [(cx,l),(cq,Thm.dest_arg (concl th1))] pthm_38
wenzelm@23252
   752
             val (tm1,tm2) = Thm.dest_comb(concl th2)
wenzelm@23252
   753
             val th3 = transitive th2 (Drule.arg_cong_rule tm1 (ppow tm2))
wenzelm@23252
   754
             val th4 = transitive (Drule.arg_cong_rule (Thm.dest_fun tm) th1) th3
wenzelm@23252
   755
         in transitive th4 (polynomial_mul_conv (concl th4))
wenzelm@23252
   756
         end
wenzelm@23252
   757
    end
wenzelm@23252
   758
 in fn tm =>
wenzelm@23252
   759
       if is_add(Thm.dest_arg1 tm) then ppow tm else monomial_pow_conv tm
wenzelm@23252
   760
 end;
wenzelm@23252
   761
wenzelm@23252
   762
(* Negation.                                                                 *)
wenzelm@23252
   763
wenzelm@23580
   764
fun polynomial_neg_conv tm =
wenzelm@23252
   765
   let val (l,r) = Thm.dest_comb tm in
wenzelm@23252
   766
        if not (l aconvc neg_tm) then raise CTERM ("polynomial_neg_conv",[tm]) else
wenzelm@23252
   767
        let val th1 = inst_thm [(cx',r)] neg_mul
wenzelm@23252
   768
            val th2 = transitive th1 (arg1_conv semiring_mul_conv (concl th1))
wenzelm@23252
   769
        in transitive th2 (polynomial_monomial_mul_conv (concl th2))
wenzelm@23252
   770
        end
wenzelm@23252
   771
   end;
wenzelm@23252
   772
wenzelm@23252
   773
wenzelm@23252
   774
(* Subtraction.                                                              *)
wenzelm@23580
   775
fun polynomial_sub_conv tm =
wenzelm@23252
   776
  let val (l,r) = dest_sub tm
wenzelm@23252
   777
      val th1 = inst_thm [(cx',l),(cy',r)] sub_add
wenzelm@23252
   778
      val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   779
      val th2 = Drule.arg_cong_rule tm1 (polynomial_neg_conv tm2)
wenzelm@23252
   780
  in transitive th1 (transitive th2 (polynomial_add_conv (concl th2)))
wenzelm@23252
   781
  end;
wenzelm@23252
   782
wenzelm@23252
   783
(* Conversion from HOL term.                                                 *)
wenzelm@23252
   784
wenzelm@23252
   785
fun polynomial_conv tm =
chaieb@23407
   786
 if is_semiring_constant tm then semiring_add_conv tm
chaieb@23407
   787
 else if not(is_comb tm) then reflexive tm
wenzelm@23252
   788
 else
wenzelm@23252
   789
  let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   790
  in if lopr aconvc neg_tm then
wenzelm@23252
   791
       let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r)
wenzelm@23252
   792
       in transitive th1 (polynomial_neg_conv (concl th1))
wenzelm@23252
   793
       end
chaieb@30866
   794
     else if lopr aconvc inverse_tm then
chaieb@30866
   795
       let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r)
chaieb@30866
   796
       in transitive th1 (semiring_mul_conv (concl th1))
chaieb@30866
   797
       end
wenzelm@23252
   798
     else
wenzelm@23252
   799
       if not(is_comb lopr) then reflexive tm
wenzelm@23252
   800
       else
wenzelm@23252
   801
         let val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   802
         in if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   803
            then
wenzelm@23252
   804
              let val th1 = Drule.fun_cong_rule (Drule.arg_cong_rule opr (polynomial_conv l)) r
wenzelm@23252
   805
              in transitive th1 (polynomial_pow_conv (concl th1))
wenzelm@23252
   806
              end
chaieb@30866
   807
         else if opr aconvc divide_tm 
chaieb@30866
   808
            then
chaieb@30866
   809
              let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) 
chaieb@30866
   810
                                        (polynomial_conv r)
chaieb@30866
   811
                  val th2 = (rewr_conv divide_inverse then_conv polynomial_mul_conv) 
chaieb@30866
   812
                              (Thm.rhs_of th1)
chaieb@30866
   813
              in transitive th1 th2
chaieb@30866
   814
              end
wenzelm@23252
   815
            else
wenzelm@23252
   816
              if opr aconvc add_tm orelse opr aconvc mul_tm orelse opr aconvc sub_tm
wenzelm@23252
   817
              then
wenzelm@23252
   818
               let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) (polynomial_conv r)
wenzelm@23252
   819
                   val f = if opr aconvc add_tm then polynomial_add_conv
wenzelm@23252
   820
                      else if opr aconvc mul_tm then polynomial_mul_conv
wenzelm@23252
   821
                      else polynomial_sub_conv
wenzelm@23252
   822
               in transitive th1 (f (concl th1))
wenzelm@23252
   823
               end
wenzelm@23252
   824
              else reflexive tm
wenzelm@23252
   825
         end
wenzelm@23252
   826
  end;
wenzelm@23252
   827
 in
wenzelm@23252
   828
   {main = polynomial_conv,
wenzelm@23252
   829
    add = polynomial_add_conv,
wenzelm@23252
   830
    mul = polynomial_mul_conv,
wenzelm@23252
   831
    pow = polynomial_pow_conv,
wenzelm@23252
   832
    neg = polynomial_neg_conv,
wenzelm@23252
   833
    sub = polynomial_sub_conv}
wenzelm@23252
   834
 end
wenzelm@23252
   835
end;
wenzelm@23252
   836
wenzelm@35410
   837
val nat_exp_ss =
wenzelm@35410
   838
  HOL_basic_ss addsimps (@{thms nat_number} @ @{thms nat_arith} @ @{thms arith_simps} @ @{thms rel_simps})
wenzelm@35410
   839
    addsimps [@{thm Let_def}, @{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc}];
wenzelm@23252
   840
wenzelm@35408
   841
fun simple_cterm_ord t u = Term_Ord.term_ord (term_of t, term_of u) = LESS;
chaieb@27222
   842
chaieb@30866
   843
fun semiring_normalizers_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, 
chaieb@23407
   844
                                     {conv, dest_const, mk_const, is_const}) ord =
wenzelm@23252
   845
  let
wenzelm@23252
   846
    val pow_conv =
wenzelm@23252
   847
      arg_conv (Simplifier.rewrite nat_exp_ss)
wenzelm@23252
   848
      then_conv Simplifier.rewrite
wenzelm@23252
   849
        (HOL_basic_ss addsimps [nth (snd semiring) 31, nth (snd semiring) 34])
chaieb@23330
   850
      then_conv conv ctxt
chaieb@23330
   851
    val dat = (is_const, conv ctxt, conv ctxt, pow_conv)
chaieb@30866
   852
  in semiring_normalizers_conv vars semiring ring field dat ord end;
chaieb@27222
   853
chaieb@30866
   854
fun semiring_normalize_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, {conv, dest_const, mk_const, is_const}) ord =
chaieb@30866
   855
 #main (semiring_normalizers_ord_wrapper ctxt ({vars = vars, semiring = semiring, ring = ring, field = field, idom = idom, ideal = ideal},{conv = conv, dest_const = dest_const, mk_const = mk_const, is_const = is_const}) ord);
wenzelm@23252
   856
chaieb@23407
   857
fun semiring_normalize_wrapper ctxt data = 
chaieb@23407
   858
  semiring_normalize_ord_wrapper ctxt data simple_cterm_ord;
chaieb@23407
   859
chaieb@23407
   860
fun semiring_normalize_ord_conv ctxt ord tm =
haftmann@36700
   861
  (case match ctxt tm of
wenzelm@23252
   862
    NONE => reflexive tm
chaieb@23407
   863
  | SOME res => semiring_normalize_ord_wrapper ctxt res ord tm);
chaieb@23407
   864
 
chaieb@23407
   865
fun semiring_normalize_conv ctxt = semiring_normalize_ord_conv ctxt simple_cterm_ord;
wenzelm@23252
   866
haftmann@36700
   867
(* theory setup *)
haftmann@36700
   868
haftmann@36702
   869
val setup = normalizer_setup
haftmann@36700
   870
wenzelm@23252
   871
end;