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