src/HOL/Tools/Groebner_Basis/normalizer.ML
author haftmann
Thu May 06 23:11:57 2010 +0200 (2010-05-06)
changeset 36720 41da7025e59c
parent 36718 30cdc863a4f8
child 36731 08cd7eccb043
child 36751 7f1da69cacb3
permissions -rw-r--r--
proper sublocales; no free-floating ML sections
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@36711
    13
  val add: {semiring: cterm list * thm list, ring: cterm list * thm list,
haftmann@36711
    14
    field: cterm list * thm list, idom: thm list, ideal: thm list} -> 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@36720
    19
  val semiring_funs: thm -> declaration
haftmann@36720
    20
  val field_funs: thm -> declaration
haftmann@36700
    21
haftmann@36711
    22
  val semiring_normalize_conv: Proof.context -> conv
haftmann@36711
    23
  val semiring_normalize_ord_conv: Proof.context -> (cterm -> cterm -> bool) -> conv
haftmann@36711
    24
  val semiring_normalize_wrapper: Proof.context -> entry -> conv
haftmann@36711
    25
  val semiring_normalize_ord_wrapper: Proof.context -> entry
haftmann@36711
    26
    -> (cterm -> cterm -> bool) -> conv
haftmann@36711
    27
  val semiring_normalizers_conv: cterm list -> cterm list * thm list
haftmann@36711
    28
    -> cterm list * thm list -> cterm list * thm list ->
haftmann@36700
    29
      (cterm -> bool) * conv * conv * conv -> (cterm -> cterm -> bool) ->
haftmann@36700
    30
        {add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv}
haftmann@36711
    31
  val semiring_normalizers_ord_wrapper:  Proof.context -> entry ->
haftmann@36711
    32
    (cterm -> cterm -> bool) ->
chaieb@27222
    33
      {add: conv, mul: conv, neg: conv, main: conv, pow: conv, sub: conv}
haftmann@36720
    34
  val field_comp_conv: conv
haftmann@36700
    35
haftmann@36700
    36
  val setup: theory -> theory
wenzelm@23252
    37
end
wenzelm@23252
    38
wenzelm@23252
    39
structure Normalizer: NORMALIZER = 
wenzelm@23252
    40
struct
wenzelm@23559
    41
haftmann@36720
    42
(** some conversion **)
haftmann@36720
    43
haftmann@36720
    44
local
haftmann@36720
    45
 val zr = @{cpat "0"}
haftmann@36720
    46
 val zT = ctyp_of_term zr
haftmann@36720
    47
 val geq = @{cpat "op ="}
haftmann@36720
    48
 val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
haftmann@36720
    49
 val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
haftmann@36720
    50
 val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
haftmann@36720
    51
 val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
haftmann@36720
    52
haftmann@36720
    53
 fun prove_nz ss T t =
haftmann@36720
    54
    let
haftmann@36720
    55
      val z = instantiate_cterm ([(zT,T)],[]) zr
haftmann@36720
    56
      val eq = instantiate_cterm ([(eqT,T)],[]) geq
haftmann@36720
    57
      val th = Simplifier.rewrite (ss addsimps @{thms simp_thms})
haftmann@36720
    58
           (Thm.capply @{cterm "Trueprop"} (Thm.capply @{cterm "Not"}
haftmann@36720
    59
                  (Thm.capply (Thm.capply eq t) z)))
haftmann@36720
    60
    in equal_elim (symmetric th) TrueI
haftmann@36720
    61
    end
haftmann@36720
    62
haftmann@36720
    63
 fun proc phi ss ct =
haftmann@36720
    64
  let
haftmann@36720
    65
    val ((x,y),(w,z)) =
haftmann@36720
    66
         (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
haftmann@36720
    67
    val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
haftmann@36720
    68
    val T = ctyp_of_term x
haftmann@36720
    69
    val [y_nz, z_nz] = map (prove_nz ss T) [y, z]
haftmann@36720
    70
    val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
haftmann@36720
    71
  in SOME (implies_elim (implies_elim th y_nz) z_nz)
haftmann@36720
    72
  end
haftmann@36720
    73
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36720
    74
haftmann@36720
    75
 fun proc2 phi ss ct =
haftmann@36720
    76
  let
haftmann@36720
    77
    val (l,r) = Thm.dest_binop ct
haftmann@36720
    78
    val T = ctyp_of_term l
haftmann@36720
    79
  in (case (term_of l, term_of r) of
haftmann@36720
    80
      (Const(@{const_name Rings.divide},_)$_$_, _) =>
haftmann@36720
    81
        let val (x,y) = Thm.dest_binop l val z = r
haftmann@36720
    82
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
haftmann@36720
    83
            val ynz = prove_nz ss T y
haftmann@36720
    84
        in SOME (implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
haftmann@36720
    85
        end
haftmann@36720
    86
     | (_, Const (@{const_name Rings.divide},_)$_$_) =>
haftmann@36720
    87
        let val (x,y) = Thm.dest_binop r val z = l
haftmann@36720
    88
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
haftmann@36720
    89
            val ynz = prove_nz ss T y
haftmann@36720
    90
        in SOME (implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
haftmann@36720
    91
        end
haftmann@36720
    92
     | _ => NONE)
haftmann@36720
    93
  end
haftmann@36720
    94
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36720
    95
haftmann@36720
    96
 fun is_number (Const(@{const_name Rings.divide},_)$a$b) = is_number a andalso is_number b
haftmann@36720
    97
   | is_number t = can HOLogic.dest_number t
haftmann@36720
    98
haftmann@36720
    99
 val is_number = is_number o term_of
haftmann@36720
   100
haftmann@36720
   101
 fun proc3 phi ss ct =
haftmann@36720
   102
  (case term_of ct of
haftmann@36720
   103
    Const(@{const_name Orderings.less},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
haftmann@36720
   104
      let
haftmann@36720
   105
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36720
   106
        val _ = map is_number [a,b,c]
haftmann@36720
   107
        val T = ctyp_of_term c
haftmann@36720
   108
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
haftmann@36720
   109
      in SOME (mk_meta_eq th) end
haftmann@36720
   110
  | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
haftmann@36720
   111
      let
haftmann@36720
   112
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36720
   113
        val _ = map is_number [a,b,c]
haftmann@36720
   114
        val T = ctyp_of_term c
haftmann@36720
   115
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
haftmann@36720
   116
      in SOME (mk_meta_eq th) end
haftmann@36720
   117
  | Const("op =",_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
haftmann@36720
   118
      let
haftmann@36720
   119
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36720
   120
        val _ = map is_number [a,b,c]
haftmann@36720
   121
        val T = ctyp_of_term c
haftmann@36720
   122
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
haftmann@36720
   123
      in SOME (mk_meta_eq th) end
haftmann@36720
   124
  | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
haftmann@36720
   125
    let
haftmann@36720
   126
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36720
   127
        val _ = map is_number [a,b,c]
haftmann@36720
   128
        val T = ctyp_of_term c
haftmann@36720
   129
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
haftmann@36720
   130
      in SOME (mk_meta_eq th) end
haftmann@36720
   131
  | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
haftmann@36720
   132
    let
haftmann@36720
   133
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36720
   134
        val _ = map is_number [a,b,c]
haftmann@36720
   135
        val T = ctyp_of_term c
haftmann@36720
   136
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
haftmann@36720
   137
      in SOME (mk_meta_eq th) end
haftmann@36720
   138
  | Const("op =",_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
haftmann@36720
   139
    let
haftmann@36720
   140
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36720
   141
        val _ = map is_number [a,b,c]
haftmann@36720
   142
        val T = ctyp_of_term c
haftmann@36720
   143
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
haftmann@36720
   144
      in SOME (mk_meta_eq th) end
haftmann@36720
   145
  | _ => NONE)
haftmann@36720
   146
  handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE
haftmann@36720
   147
haftmann@36720
   148
val add_frac_frac_simproc =
haftmann@36720
   149
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + (?w::?'a::field)/?z"}],
haftmann@36720
   150
                     name = "add_frac_frac_simproc",
haftmann@36720
   151
                     proc = proc, identifier = []}
haftmann@36720
   152
haftmann@36720
   153
val add_frac_num_simproc =
haftmann@36720
   154
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + ?z"}, @{cpat "?z + (?x::?'a::field)/?y"}],
haftmann@36720
   155
                     name = "add_frac_num_simproc",
haftmann@36720
   156
                     proc = proc2, identifier = []}
haftmann@36720
   157
haftmann@36720
   158
val ord_frac_simproc =
haftmann@36720
   159
  make_simproc
haftmann@36720
   160
    {lhss = [@{cpat "(?a::(?'a::{field, ord}))/?b < ?c"},
haftmann@36720
   161
             @{cpat "(?a::(?'a::{field, ord}))/?b <= ?c"},
haftmann@36720
   162
             @{cpat "?c < (?a::(?'a::{field, ord}))/?b"},
haftmann@36720
   163
             @{cpat "?c <= (?a::(?'a::{field, ord}))/?b"},
haftmann@36720
   164
             @{cpat "?c = ((?a::(?'a::{field, ord}))/?b)"},
haftmann@36720
   165
             @{cpat "((?a::(?'a::{field, ord}))/ ?b) = ?c"}],
haftmann@36720
   166
             name = "ord_frac_simproc", proc = proc3, identifier = []}
haftmann@36720
   167
haftmann@36720
   168
val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
haftmann@36720
   169
           @{thm "divide_Numeral1"},
haftmann@36720
   170
           @{thm "divide_zero"}, @{thm "divide_Numeral0"},
haftmann@36720
   171
           @{thm "divide_divide_eq_left"}, 
haftmann@36720
   172
           @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
haftmann@36720
   173
           @{thm "times_divide_times_eq"},
haftmann@36720
   174
           @{thm "divide_divide_eq_right"},
haftmann@36720
   175
           @{thm "diff_def"}, @{thm "minus_divide_left"},
haftmann@36720
   176
           @{thm "Numeral1_eq1_nat"}, @{thm "add_divide_distrib"} RS sym,
haftmann@36720
   177
           @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
haftmann@36720
   178
           Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
haftmann@36720
   179
           (@{thm field_divide_inverse} RS sym)]
haftmann@36720
   180
haftmann@36720
   181
in
haftmann@36720
   182
haftmann@36720
   183
val field_comp_conv = (Simplifier.rewrite
haftmann@36720
   184
(HOL_basic_ss addsimps @{thms "semiring_norm"}
haftmann@36720
   185
              addsimps ths addsimps @{thms simp_thms}
haftmann@36720
   186
              addsimprocs Numeral_Simprocs.field_cancel_numeral_factors
haftmann@36720
   187
               addsimprocs [add_frac_frac_simproc, add_frac_num_simproc,
haftmann@36720
   188
                            ord_frac_simproc]
haftmann@36720
   189
                addcongs [@{thm "if_weak_cong"}]))
haftmann@36720
   190
then_conv (Simplifier.rewrite (HOL_basic_ss addsimps
haftmann@36720
   191
  [@{thm numeral_1_eq_1},@{thm numeral_0_eq_0}] @ @{thms numerals(1-2)}))
haftmann@36720
   192
haftmann@36720
   193
end
haftmann@36720
   194
haftmann@36720
   195
haftmann@36708
   196
(** data **)
haftmann@36700
   197
haftmann@36700
   198
type entry =
haftmann@36700
   199
 {vars: cterm list,
haftmann@36700
   200
  semiring: cterm list * thm list,
haftmann@36700
   201
  ring: cterm list * thm list,
haftmann@36700
   202
  field: cterm list * thm list,
haftmann@36700
   203
  idom: thm list,
haftmann@36700
   204
  ideal: thm list} *
haftmann@36700
   205
 {is_const: cterm -> bool,
haftmann@36700
   206
  dest_const: cterm -> Rat.rat,
haftmann@36700
   207
  mk_const: ctyp -> Rat.rat -> cterm,
haftmann@36700
   208
  conv: Proof.context -> cterm -> thm};
haftmann@36700
   209
haftmann@36700
   210
structure Data = Generic_Data
haftmann@36700
   211
(
haftmann@36705
   212
  type T = (thm * entry) list;
haftmann@36705
   213
  val empty = [];
haftmann@36700
   214
  val extend = I;
haftmann@36718
   215
  val merge = AList.merge Thm.eq_thm (K true);
haftmann@36700
   216
);
haftmann@36700
   217
haftmann@36705
   218
val get = Data.get o Context.Proof;
haftmann@36700
   219
haftmann@36700
   220
fun match ctxt tm =
haftmann@36700
   221
  let
haftmann@36700
   222
    fun match_inst
haftmann@36700
   223
        ({vars, semiring = (sr_ops, sr_rules), 
haftmann@36700
   224
          ring = (r_ops, r_rules), field = (f_ops, f_rules), idom, ideal},
haftmann@36700
   225
         fns as {is_const, dest_const, mk_const, conv}) pat =
haftmann@36700
   226
       let
haftmann@36700
   227
        fun h instT =
haftmann@36700
   228
          let
haftmann@36700
   229
            val substT = Thm.instantiate (instT, []);
haftmann@36700
   230
            val substT_cterm = Drule.cterm_rule substT;
haftmann@36700
   231
haftmann@36700
   232
            val vars' = map substT_cterm vars;
haftmann@36700
   233
            val semiring' = (map substT_cterm sr_ops, map substT sr_rules);
haftmann@36700
   234
            val ring' = (map substT_cterm r_ops, map substT r_rules);
haftmann@36700
   235
            val field' = (map substT_cterm f_ops, map substT f_rules);
haftmann@36700
   236
            val idom' = map substT idom;
haftmann@36700
   237
            val ideal' = map substT ideal;
haftmann@36700
   238
haftmann@36700
   239
            val result = ({vars = vars', semiring = semiring', 
haftmann@36700
   240
                           ring = ring', field = field', idom = idom', ideal = ideal'}, fns);
haftmann@36700
   241
          in SOME result end
haftmann@36700
   242
      in (case try Thm.match (pat, tm) of
haftmann@36700
   243
           NONE => NONE
haftmann@36700
   244
         | SOME (instT, _) => h instT)
haftmann@36700
   245
      end;
haftmann@36700
   246
haftmann@36700
   247
    fun match_struct (_,
haftmann@36700
   248
        entry as ({semiring = (sr_ops, _), ring = (r_ops, _), field = (f_ops, _), ...}, _): entry) =
haftmann@36700
   249
      get_first (match_inst entry) (sr_ops @ r_ops @ f_ops);
haftmann@36704
   250
  in get_first match_struct (get ctxt) end;
haftmann@36700
   251
haftmann@36700
   252
haftmann@36700
   253
(* logical content *)
haftmann@36700
   254
haftmann@36700
   255
val semiringN = "semiring";
haftmann@36700
   256
val ringN = "ring";
haftmann@36700
   257
val idomN = "idom";
haftmann@36700
   258
val idealN = "ideal";
haftmann@36700
   259
val fieldN = "field";
haftmann@36700
   260
haftmann@36700
   261
fun undefined _ = raise Match;
haftmann@36700
   262
haftmann@36706
   263
val del = Thm.declaration_attribute (Data.map o AList.delete Thm.eq_thm);
haftmann@36700
   264
haftmann@36700
   265
fun add {semiring = (sr_ops, sr_rules), ring = (r_ops, r_rules), 
haftmann@36700
   266
         field = (f_ops, f_rules), idom, ideal} =
haftmann@36700
   267
  Thm.declaration_attribute (fn key => fn context => context |> Data.map
haftmann@36700
   268
    let
haftmann@36700
   269
      val ctxt = Context.proof_of context;
haftmann@36700
   270
haftmann@36700
   271
      fun check kind name xs n =
haftmann@36700
   272
        null xs orelse length xs = n orelse
haftmann@36700
   273
        error ("Expected " ^ string_of_int n ^ " " ^ kind ^ " for " ^ name);
haftmann@36700
   274
      val check_ops = check "operations";
haftmann@36700
   275
      val check_rules = check "rules";
haftmann@36700
   276
haftmann@36700
   277
      val _ =
haftmann@36700
   278
        check_ops semiringN sr_ops 5 andalso
haftmann@36700
   279
        check_rules semiringN sr_rules 37 andalso
haftmann@36700
   280
        check_ops ringN r_ops 2 andalso
haftmann@36700
   281
        check_rules ringN r_rules 2 andalso
haftmann@36700
   282
        check_ops fieldN f_ops 2 andalso
haftmann@36700
   283
        check_rules fieldN f_rules 2 andalso
haftmann@36700
   284
        check_rules idomN idom 2;
haftmann@36700
   285
haftmann@36700
   286
      val mk_meta = Local_Defs.meta_rewrite_rule ctxt;
haftmann@36700
   287
      val sr_rules' = map mk_meta sr_rules;
haftmann@36700
   288
      val r_rules' = map mk_meta r_rules;
haftmann@36700
   289
      val f_rules' = map mk_meta f_rules;
haftmann@36700
   290
haftmann@36700
   291
      fun rule i = nth sr_rules' (i - 1);
haftmann@36700
   292
haftmann@36700
   293
      val (cx, cy) = Thm.dest_binop (hd sr_ops);
haftmann@36700
   294
      val cz = rule 34 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
haftmann@36700
   295
      val cn = rule 36 |> Thm.rhs_of |> Thm.dest_arg |> Thm.dest_arg;
haftmann@36700
   296
      val ((clx, crx), (cly, cry)) =
haftmann@36700
   297
        rule 13 |> Thm.rhs_of |> Thm.dest_binop |> pairself Thm.dest_binop;
haftmann@36700
   298
      val ((ca, cb), (cc, cd)) =
haftmann@36700
   299
        rule 20 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_binop;
haftmann@36700
   300
      val cm = rule 1 |> Thm.rhs_of |> Thm.dest_arg;
haftmann@36700
   301
      val (cp, cq) = rule 26 |> Thm.lhs_of |> Thm.dest_binop |> pairself Thm.dest_arg;
haftmann@36700
   302
haftmann@36700
   303
      val vars = [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry];
haftmann@36700
   304
      val semiring = (sr_ops, sr_rules');
haftmann@36700
   305
      val ring = (r_ops, r_rules');
haftmann@36700
   306
      val field = (f_ops, f_rules');
haftmann@36700
   307
      val ideal' = map (symmetric o mk_meta) ideal
haftmann@36700
   308
    in
haftmann@36706
   309
      AList.delete Thm.eq_thm key #>
haftmann@36705
   310
      cons (key, ({vars = vars, semiring = semiring, 
haftmann@36700
   311
                          ring = ring, field = field, idom = idom, ideal = ideal'},
haftmann@36700
   312
             {is_const = undefined, dest_const = undefined, mk_const = undefined,
haftmann@36705
   313
             conv = undefined}))
haftmann@36700
   314
    end);
haftmann@36700
   315
haftmann@36700
   316
haftmann@36700
   317
(* extra-logical functions *)
haftmann@36700
   318
haftmann@36700
   319
fun funs raw_key {is_const, dest_const, mk_const, conv} phi = 
haftmann@36705
   320
 Data.map (fn data =>
haftmann@36700
   321
  let
haftmann@36700
   322
    val key = Morphism.thm phi raw_key;
haftmann@36706
   323
    val _ = AList.defined Thm.eq_thm data key orelse
haftmann@36700
   324
      raise THM ("No data entry for structure key", 0, [key]);
haftmann@36700
   325
    val fns = {is_const = is_const phi, dest_const = dest_const phi,
haftmann@36700
   326
      mk_const = mk_const phi, conv = conv phi};
haftmann@36706
   327
  in AList.map_entry Thm.eq_thm key (apsnd (K fns)) data end);
haftmann@36700
   328
haftmann@36720
   329
fun semiring_funs key = funs key
haftmann@36720
   330
   {is_const = fn phi => can HOLogic.dest_number o Thm.term_of,
haftmann@36720
   331
    dest_const = fn phi => fn ct =>
haftmann@36720
   332
      Rat.rat_of_int (snd
haftmann@36720
   333
        (HOLogic.dest_number (Thm.term_of ct)
haftmann@36720
   334
          handle TERM _ => error "ring_dest_const")),
haftmann@36720
   335
    mk_const = fn phi => fn cT => fn x => Numeral.mk_cnumber cT
haftmann@36720
   336
      (case Rat.quotient_of_rat x of (i, 1) => i | _ => error "int_of_rat: bad int"),
haftmann@36720
   337
    conv = fn phi => fn _ => Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm})
haftmann@36720
   338
      then_conv Simplifier.rewrite (HOL_basic_ss addsimps
haftmann@36720
   339
        (@{thms numeral_1_eq_1} @ @{thms numeral_0_eq_0} @ @{thms numerals(1-2)}))};
haftmann@36720
   340
haftmann@36720
   341
fun field_funs key =
haftmann@36720
   342
  let
haftmann@36720
   343
    fun numeral_is_const ct =
haftmann@36720
   344
      case term_of ct of
haftmann@36720
   345
       Const (@{const_name Rings.divide},_) $ a $ b =>
haftmann@36720
   346
         can HOLogic.dest_number a andalso can HOLogic.dest_number b
haftmann@36720
   347
     | Const (@{const_name Rings.inverse},_)$t => can HOLogic.dest_number t
haftmann@36720
   348
     | t => can HOLogic.dest_number t
haftmann@36720
   349
    fun dest_const ct = ((case term_of ct of
haftmann@36720
   350
       Const (@{const_name Rings.divide},_) $ a $ b=>
haftmann@36720
   351
        Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
haftmann@36720
   352
     | Const (@{const_name Rings.inverse},_)$t => 
haftmann@36720
   353
                   Rat.inv (Rat.rat_of_int (snd (HOLogic.dest_number t)))
haftmann@36720
   354
     | t => Rat.rat_of_int (snd (HOLogic.dest_number t))) 
haftmann@36720
   355
       handle TERM _ => error "ring_dest_const")
haftmann@36720
   356
    fun mk_const phi cT x =
haftmann@36720
   357
      let val (a, b) = Rat.quotient_of_rat x
haftmann@36720
   358
      in if b = 1 then Numeral.mk_cnumber cT a
haftmann@36720
   359
        else Thm.capply
haftmann@36720
   360
             (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
haftmann@36720
   361
                         (Numeral.mk_cnumber cT a))
haftmann@36720
   362
             (Numeral.mk_cnumber cT b)
haftmann@36720
   363
      end
haftmann@36720
   364
  in funs key
haftmann@36720
   365
     {is_const = K numeral_is_const,
haftmann@36720
   366
      dest_const = K dest_const,
haftmann@36720
   367
      mk_const = mk_const,
haftmann@36720
   368
      conv = K (K field_comp_conv)}
haftmann@36720
   369
  end;
haftmann@36720
   370
haftmann@36720
   371
haftmann@36700
   372
haftmann@36710
   373
(** auxiliary **)
chaieb@25253
   374
chaieb@25253
   375
fun is_comb ct =
chaieb@25253
   376
  (case Thm.term_of ct of
chaieb@25253
   377
    _ $ _ => true
chaieb@25253
   378
  | _ => false);
chaieb@25253
   379
chaieb@25253
   380
val concl = Thm.cprop_of #> Thm.dest_arg;
chaieb@25253
   381
chaieb@25253
   382
fun is_binop ct ct' =
chaieb@25253
   383
  (case Thm.term_of ct' of
chaieb@25253
   384
    c $ _ $ _ => term_of ct aconv c
chaieb@25253
   385
  | _ => false);
chaieb@25253
   386
chaieb@25253
   387
fun dest_binop ct ct' =
chaieb@25253
   388
  if is_binop ct ct' then Thm.dest_binop ct'
chaieb@25253
   389
  else raise CTERM ("dest_binop: bad binop", [ct, ct'])
chaieb@25253
   390
chaieb@25253
   391
fun inst_thm inst = Thm.instantiate ([], inst);
chaieb@25253
   392
wenzelm@23252
   393
val dest_numeral = term_of #> HOLogic.dest_number #> snd;
wenzelm@23252
   394
val is_numeral = can dest_numeral;
wenzelm@23252
   395
wenzelm@23252
   396
val numeral01_conv = Simplifier.rewrite
haftmann@25481
   397
                         (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}, @{thm numeral_0_eq_0}]);
wenzelm@23252
   398
val zero1_numeral_conv = 
haftmann@25481
   399
 Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1} RS sym, @{thm numeral_0_eq_0} RS sym]);
wenzelm@23580
   400
fun zerone_conv cv = zero1_numeral_conv then_conv cv then_conv numeral01_conv;
wenzelm@23252
   401
val natarith = [@{thm "add_nat_number_of"}, @{thm "diff_nat_number_of"},
wenzelm@23252
   402
                @{thm "mult_nat_number_of"}, @{thm "eq_nat_number_of"}, 
wenzelm@23252
   403
                @{thm "less_nat_number_of"}];
haftmann@36700
   404
wenzelm@23252
   405
val nat_add_conv = 
wenzelm@23252
   406
 zerone_conv 
wenzelm@23252
   407
  (Simplifier.rewrite 
wenzelm@23252
   408
    (HOL_basic_ss 
haftmann@25481
   409
       addsimps @{thms arith_simps} @ natarith @ @{thms rel_simps}
wenzelm@35410
   410
             @ [@{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc},
nipkow@31790
   411
                 @{thm add_number_of_left}, @{thm Suc_eq_plus1}]
haftmann@25481
   412
             @ map (fn th => th RS sym) @{thms numerals}));
wenzelm@23252
   413
wenzelm@23252
   414
val zeron_tm = @{cterm "0::nat"};
wenzelm@23252
   415
val onen_tm  = @{cterm "1::nat"};
wenzelm@23252
   416
val true_tm = @{cterm "True"};
wenzelm@23252
   417
wenzelm@23252
   418
haftmann@36710
   419
(** normalizing conversions **)
haftmann@36710
   420
haftmann@36710
   421
(* core conversion *)
haftmann@36710
   422
chaieb@30866
   423
fun semiring_normalizers_conv vars (sr_ops, sr_rules) (r_ops, r_rules) (f_ops, f_rules)
wenzelm@23252
   424
  (is_semiring_constant, semiring_add_conv, semiring_mul_conv, semiring_pow_conv) =
wenzelm@23252
   425
let
wenzelm@23252
   426
wenzelm@23252
   427
val [pthm_02, pthm_03, pthm_04, pthm_05, pthm_07, pthm_08,
wenzelm@23252
   428
     pthm_09, pthm_10, pthm_11, pthm_12, pthm_13, pthm_14, pthm_15, pthm_16,
wenzelm@23252
   429
     pthm_17, pthm_18, pthm_19, pthm_21, pthm_22, pthm_23, pthm_24,
wenzelm@23252
   430
     pthm_25, pthm_26, pthm_27, pthm_28, pthm_29, pthm_30, pthm_31, pthm_32,
wenzelm@23252
   431
     pthm_33, pthm_34, pthm_35, pthm_36, pthm_37, pthm_38,pthm_39,pthm_40] = sr_rules;
wenzelm@23252
   432
wenzelm@23252
   433
val [ca, cb, cc, cd, cm, cn, cp, cq, cx, cy, cz, clx, crx, cly, cry] = vars;
wenzelm@23252
   434
val [add_pat, mul_pat, pow_pat, zero_tm, one_tm] = sr_ops;
wenzelm@23252
   435
val [add_tm, mul_tm, pow_tm] = map (Thm.dest_fun o Thm.dest_fun) [add_pat, mul_pat, pow_pat];
wenzelm@23252
   436
wenzelm@23252
   437
val dest_add = dest_binop add_tm
wenzelm@23252
   438
val dest_mul = dest_binop mul_tm
wenzelm@23252
   439
fun dest_pow tm =
wenzelm@23252
   440
 let val (l,r) = dest_binop pow_tm tm
wenzelm@23252
   441
 in if is_numeral r then (l,r) else raise CTERM ("dest_pow",[tm])
wenzelm@23252
   442
 end;
wenzelm@23252
   443
val is_add = is_binop add_tm
wenzelm@23252
   444
val is_mul = is_binop mul_tm
wenzelm@23252
   445
fun is_pow tm = is_binop pow_tm tm andalso is_numeral(Thm.dest_arg tm);
wenzelm@23252
   446
wenzelm@23252
   447
val (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub,cx',cy') =
wenzelm@23252
   448
  (case (r_ops, r_rules) of
chaieb@30866
   449
    ([sub_pat, neg_pat], [neg_mul, sub_add]) =>
wenzelm@23252
   450
      let
wenzelm@23252
   451
        val sub_tm = Thm.dest_fun (Thm.dest_fun sub_pat)
wenzelm@23252
   452
        val neg_tm = Thm.dest_fun neg_pat
wenzelm@23252
   453
        val dest_sub = dest_binop sub_tm
wenzelm@23252
   454
        val is_sub = is_binop sub_tm
wenzelm@23252
   455
      in (neg_mul,sub_add,sub_tm,neg_tm,dest_sub,is_sub, neg_mul |> concl |> Thm.dest_arg,
wenzelm@23252
   456
          sub_add |> concl |> Thm.dest_arg |> Thm.dest_arg)
chaieb@30866
   457
      end
chaieb@30866
   458
    | _ => (TrueI, TrueI, true_tm, true_tm, (fn t => (t,t)), K false, true_tm, true_tm));
chaieb@30866
   459
chaieb@30866
   460
val (divide_inverse, inverse_divide, divide_tm, inverse_tm, is_divide) = 
chaieb@30866
   461
  (case (f_ops, f_rules) of 
chaieb@30866
   462
   ([divide_pat, inverse_pat], [div_inv, inv_div]) => 
chaieb@30866
   463
     let val div_tm = funpow 2 Thm.dest_fun divide_pat
chaieb@30866
   464
         val inv_tm = Thm.dest_fun inverse_pat
chaieb@30866
   465
     in (div_inv, inv_div, div_tm, inv_tm, is_binop div_tm)
chaieb@30866
   466
     end
chaieb@30866
   467
   | _ => (TrueI, TrueI, true_tm, true_tm, K false));
chaieb@30866
   468
wenzelm@23252
   469
in fn variable_order =>
wenzelm@23252
   470
 let
wenzelm@23252
   471
wenzelm@23252
   472
(* Conversion for "x^n * x^m", with either x^n = x and/or x^m = x possible.  *)
wenzelm@23252
   473
(* Also deals with "const * const", but both terms must involve powers of    *)
wenzelm@23252
   474
(* the same variable, or both be constants, or behaviour may be incorrect.   *)
wenzelm@23252
   475
wenzelm@23252
   476
 fun powvar_mul_conv tm =
wenzelm@23252
   477
  let
wenzelm@23252
   478
  val (l,r) = dest_mul tm
wenzelm@23252
   479
  in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   480
     then semiring_mul_conv tm
wenzelm@23252
   481
     else
wenzelm@23252
   482
      ((let
wenzelm@23252
   483
         val (lx,ln) = dest_pow l
wenzelm@23252
   484
        in
wenzelm@23252
   485
         ((let val (rx,rn) = dest_pow r
wenzelm@23252
   486
               val th1 = inst_thm [(cx,lx),(cp,ln),(cq,rn)] pthm_29
wenzelm@23252
   487
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   488
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   489
           handle CTERM _ =>
wenzelm@23252
   490
            (let val th1 = inst_thm [(cx,lx),(cq,ln)] pthm_31
wenzelm@23252
   491
                 val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   492
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)) end)
wenzelm@23252
   493
       handle CTERM _ =>
wenzelm@23252
   494
           ((let val (rx,rn) = dest_pow r
wenzelm@23252
   495
                val th1 = inst_thm [(cx,rx),(cq,rn)] pthm_30
wenzelm@23252
   496
                val (tm1,tm2) = Thm.dest_comb(concl th1) in
wenzelm@23252
   497
               transitive th1 (Drule.arg_cong_rule tm1 (nat_add_conv tm2)) end)
wenzelm@23252
   498
           handle CTERM _ => inst_thm [(cx,l)] pthm_32
wenzelm@23252
   499
wenzelm@23252
   500
))
wenzelm@23252
   501
 end;
wenzelm@23252
   502
wenzelm@23252
   503
(* Remove "1 * m" from a monomial, and just leave m.                         *)
wenzelm@23252
   504
wenzelm@23252
   505
 fun monomial_deone th =
wenzelm@23252
   506
       (let val (l,r) = dest_mul(concl th) in
wenzelm@23252
   507
           if l aconvc one_tm
wenzelm@23252
   508
          then transitive th (inst_thm [(ca,r)] pthm_13)  else th end)
wenzelm@23252
   509
       handle CTERM _ => th;
wenzelm@23252
   510
wenzelm@23252
   511
(* Conversion for "(monomial)^n", where n is a numeral.                      *)
wenzelm@23252
   512
wenzelm@23252
   513
 val monomial_pow_conv =
wenzelm@23252
   514
  let
wenzelm@23252
   515
   fun monomial_pow tm bod ntm =
wenzelm@23252
   516
    if not(is_comb bod)
wenzelm@23252
   517
    then reflexive tm
wenzelm@23252
   518
    else
wenzelm@23252
   519
     if is_semiring_constant bod
wenzelm@23252
   520
     then semiring_pow_conv tm
wenzelm@23252
   521
     else
wenzelm@23252
   522
      let
wenzelm@23252
   523
      val (lopr,r) = Thm.dest_comb bod
wenzelm@23252
   524
      in if not(is_comb lopr)
wenzelm@23252
   525
         then reflexive tm
wenzelm@23252
   526
        else
wenzelm@23252
   527
          let
wenzelm@23252
   528
          val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   529
         in
wenzelm@23252
   530
           if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   531
          then
wenzelm@23252
   532
            let val th1 = inst_thm [(cx,l),(cp,r),(cq,ntm)] pthm_34
wenzelm@23252
   533
                val (l,r) = Thm.dest_comb(concl th1)
haftmann@36700
   534
           in transitive th1 (Drule.arg_cong_rule l (nat_add_conv r))
wenzelm@23252
   535
           end
wenzelm@23252
   536
           else
wenzelm@23252
   537
            if opr aconvc mul_tm
wenzelm@23252
   538
            then
wenzelm@23252
   539
             let
wenzelm@23252
   540
              val th1 = inst_thm [(cx,l),(cy,r),(cq,ntm)] pthm_33
wenzelm@23252
   541
             val (xy,z) = Thm.dest_comb(concl th1)
wenzelm@23252
   542
              val (x,y) = Thm.dest_comb xy
wenzelm@23252
   543
              val thl = monomial_pow y l ntm
wenzelm@23252
   544
              val thr = monomial_pow z r ntm
wenzelm@23252
   545
             in transitive th1 (combination (Drule.arg_cong_rule x thl) thr)
wenzelm@23252
   546
             end
wenzelm@23252
   547
             else reflexive tm
wenzelm@23252
   548
          end
wenzelm@23252
   549
      end
wenzelm@23252
   550
  in fn tm =>
wenzelm@23252
   551
   let
wenzelm@23252
   552
    val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   553
    val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   554
   in if not (opr aconvc pow_tm) orelse not(is_numeral r)
wenzelm@23252
   555
      then raise CTERM ("monomial_pow_conv", [tm])
wenzelm@23252
   556
      else if r aconvc zeron_tm
wenzelm@23252
   557
      then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   558
      else if r aconvc onen_tm
wenzelm@23252
   559
      then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   560
      else monomial_deone(monomial_pow tm l r)
wenzelm@23252
   561
   end
wenzelm@23252
   562
  end;
wenzelm@23252
   563
wenzelm@23252
   564
(* Multiplication of canonical monomials.                                    *)
wenzelm@23252
   565
 val monomial_mul_conv =
wenzelm@23252
   566
  let
wenzelm@23252
   567
   fun powvar tm =
wenzelm@23252
   568
    if is_semiring_constant tm then one_tm
wenzelm@23252
   569
    else
wenzelm@23252
   570
     ((let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   571
           val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   572
       in if opr aconvc pow_tm andalso is_numeral r then l 
wenzelm@23252
   573
          else raise CTERM ("monomial_mul_conv",[tm]) end)
wenzelm@23252
   574
     handle CTERM _ => tm)   (* FIXME !? *)
wenzelm@23252
   575
   fun  vorder x y =
wenzelm@23252
   576
    if x aconvc y then 0
wenzelm@23252
   577
    else
wenzelm@23252
   578
     if x aconvc one_tm then ~1
wenzelm@23252
   579
     else if y aconvc one_tm then 1
wenzelm@23252
   580
      else if variable_order x y then ~1 else 1
wenzelm@23252
   581
   fun monomial_mul tm l r =
wenzelm@23252
   582
    ((let val (lx,ly) = dest_mul l val vl = powvar lx
wenzelm@23252
   583
      in
wenzelm@23252
   584
      ((let
wenzelm@23252
   585
        val (rx,ry) = dest_mul r
wenzelm@23252
   586
         val vr = powvar rx
wenzelm@23252
   587
         val ord = vorder vl vr
wenzelm@23252
   588
        in
wenzelm@23252
   589
         if ord = 0
wenzelm@23252
   590
        then
wenzelm@23252
   591
          let
wenzelm@23252
   592
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] pthm_15
wenzelm@23252
   593
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   594
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   595
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   596
             val th3 = transitive th1 th2
wenzelm@23252
   597
              val  (tm5,tm6) = Thm.dest_comb(concl th3)
wenzelm@23252
   598
              val  (tm7,tm8) = Thm.dest_comb tm6
wenzelm@23252
   599
             val  th4 = monomial_mul tm6 (Thm.dest_arg tm7) tm8
wenzelm@23252
   600
         in  transitive th3 (Drule.arg_cong_rule tm5 th4)
wenzelm@23252
   601
         end
wenzelm@23252
   602
         else
wenzelm@23252
   603
          let val th0 = if ord < 0 then pthm_16 else pthm_17
wenzelm@23252
   604
             val th1 = inst_thm [(clx,lx),(cly,ly),(crx,rx),(cry,ry)] th0
wenzelm@23252
   605
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   606
             val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   607
         in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   608
         end
wenzelm@23252
   609
        end)
wenzelm@23252
   610
       handle CTERM _ =>
wenzelm@23252
   611
        (let val vr = powvar r val ord = vorder vl vr
wenzelm@23252
   612
        in
wenzelm@23252
   613
          if ord = 0 then
wenzelm@23252
   614
           let
wenzelm@23252
   615
           val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_18
wenzelm@23252
   616
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   617
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   618
           val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2
wenzelm@23252
   619
          in transitive th1 th2
wenzelm@23252
   620
          end
wenzelm@23252
   621
          else
wenzelm@23252
   622
          if ord < 0 then
wenzelm@23252
   623
            let val th1 = inst_thm [(clx,lx),(cly,ly),(crx,r)] pthm_19
wenzelm@23252
   624
                val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   625
                val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   626
           in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   627
           end
wenzelm@23252
   628
           else inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   629
        end)) end)
wenzelm@23252
   630
     handle CTERM _ =>
wenzelm@23252
   631
      (let val vl = powvar l in
wenzelm@23252
   632
        ((let
wenzelm@23252
   633
          val (rx,ry) = dest_mul r
wenzelm@23252
   634
          val vr = powvar rx
wenzelm@23252
   635
           val ord = vorder vl vr
wenzelm@23252
   636
         in if ord = 0 then
wenzelm@23252
   637
              let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_21
wenzelm@23252
   638
                 val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   639
                 val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   640
             in transitive th1 (Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (powvar_mul_conv tm4)) tm2)
wenzelm@23252
   641
             end
wenzelm@23252
   642
             else if ord > 0 then
wenzelm@23252
   643
                 let val th1 = inst_thm [(clx,l),(crx,rx),(cry,ry)] pthm_22
wenzelm@23252
   644
                     val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   645
                    val (tm3,tm4) = Thm.dest_comb tm2
wenzelm@23252
   646
                in transitive th1 (Drule.arg_cong_rule tm1 (monomial_mul tm2 (Thm.dest_arg tm3) tm4))
wenzelm@23252
   647
                end
wenzelm@23252
   648
             else reflexive tm
wenzelm@23252
   649
         end)
wenzelm@23252
   650
        handle CTERM _ =>
wenzelm@23252
   651
          (let val vr = powvar r
wenzelm@23252
   652
               val  ord = vorder vl vr
wenzelm@23252
   653
          in if ord = 0 then powvar_mul_conv tm
wenzelm@23252
   654
              else if ord > 0 then inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   655
              else reflexive tm
wenzelm@23252
   656
          end)) end))
wenzelm@23252
   657
  in fn tm => let val (l,r) = dest_mul tm in monomial_deone(monomial_mul tm l r)
wenzelm@23252
   658
             end
wenzelm@23252
   659
  end;
wenzelm@23252
   660
(* Multiplication by monomial of a polynomial.                               *)
wenzelm@23252
   661
wenzelm@23252
   662
 val polynomial_monomial_mul_conv =
wenzelm@23252
   663
  let
wenzelm@23252
   664
   fun pmm_conv tm =
wenzelm@23252
   665
    let val (l,r) = dest_mul tm
wenzelm@23252
   666
    in
wenzelm@23252
   667
    ((let val (y,z) = dest_add r
wenzelm@23252
   668
          val th1 = inst_thm [(cx,l),(cy,y),(cz,z)] pthm_37
wenzelm@23252
   669
          val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   670
          val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   671
          val th2 = combination (Drule.arg_cong_rule tm3 (monomial_mul_conv tm4)) (pmm_conv tm2)
wenzelm@23252
   672
      in transitive th1 th2
wenzelm@23252
   673
      end)
wenzelm@23252
   674
     handle CTERM _ => monomial_mul_conv tm)
wenzelm@23252
   675
   end
wenzelm@23252
   676
 in pmm_conv
wenzelm@23252
   677
 end;
wenzelm@23252
   678
wenzelm@23252
   679
(* Addition of two monomials identical except for constant multiples.        *)
wenzelm@23252
   680
wenzelm@23252
   681
fun monomial_add_conv tm =
wenzelm@23252
   682
 let val (l,r) = dest_add tm
wenzelm@23252
   683
 in if is_semiring_constant l andalso is_semiring_constant r
wenzelm@23252
   684
    then semiring_add_conv tm
wenzelm@23252
   685
    else
wenzelm@23252
   686
     let val th1 =
wenzelm@23252
   687
           if is_mul l andalso is_semiring_constant(Thm.dest_arg1 l)
wenzelm@23252
   688
           then if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r) then
wenzelm@23252
   689
                    inst_thm [(ca,Thm.dest_arg1 l),(cm,Thm.dest_arg r), (cb,Thm.dest_arg1 r)] pthm_02
wenzelm@23252
   690
                else inst_thm [(ca,Thm.dest_arg1 l),(cm,r)] pthm_03
wenzelm@23252
   691
           else if is_mul r andalso is_semiring_constant(Thm.dest_arg1 r)
wenzelm@23252
   692
           then inst_thm [(cm,l),(ca,Thm.dest_arg1 r)] pthm_04
wenzelm@23252
   693
           else inst_thm [(cm,r)] pthm_05
wenzelm@23252
   694
         val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   695
         val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   696
         val th2 = Drule.arg_cong_rule tm3 (semiring_add_conv tm4)
wenzelm@23252
   697
         val th3 = transitive th1 (Drule.fun_cong_rule th2 tm2)
wenzelm@23252
   698
         val tm5 = concl th3
wenzelm@23252
   699
      in
wenzelm@23252
   700
      if (Thm.dest_arg1 tm5) aconvc zero_tm
wenzelm@23252
   701
      then transitive th3 (inst_thm [(ca,Thm.dest_arg tm5)] pthm_11)
wenzelm@23252
   702
      else monomial_deone th3
wenzelm@23252
   703
     end
wenzelm@23252
   704
 end;
wenzelm@23252
   705
wenzelm@23252
   706
(* Ordering on monomials.                                                    *)
wenzelm@23252
   707
wenzelm@23252
   708
fun striplist dest =
wenzelm@23252
   709
 let fun strip x acc =
wenzelm@23252
   710
   ((let val (l,r) = dest x in
wenzelm@23252
   711
        strip l (strip r acc) end)
wenzelm@23252
   712
    handle CTERM _ => x::acc)    (* FIXME !? *)
wenzelm@23252
   713
 in fn x => strip x []
wenzelm@23252
   714
 end;
wenzelm@23252
   715
wenzelm@23252
   716
wenzelm@23252
   717
fun powervars tm =
wenzelm@23252
   718
 let val ptms = striplist dest_mul tm
wenzelm@23252
   719
 in if is_semiring_constant (hd ptms) then tl ptms else ptms
wenzelm@23252
   720
 end;
wenzelm@23252
   721
val num_0 = 0;
wenzelm@23252
   722
val num_1 = 1;
wenzelm@23252
   723
fun dest_varpow tm =
wenzelm@23252
   724
 ((let val (x,n) = dest_pow tm in (x,dest_numeral n) end)
wenzelm@23252
   725
   handle CTERM _ =>
wenzelm@23252
   726
   (tm,(if is_semiring_constant tm then num_0 else num_1)));
wenzelm@23252
   727
wenzelm@23252
   728
val morder =
wenzelm@23252
   729
 let fun lexorder l1 l2 =
wenzelm@23252
   730
  case (l1,l2) of
wenzelm@23252
   731
    ([],[]) => 0
wenzelm@23252
   732
  | (vps,[]) => ~1
wenzelm@23252
   733
  | ([],vps) => 1
wenzelm@23252
   734
  | (((x1,n1)::vs1),((x2,n2)::vs2)) =>
wenzelm@23252
   735
     if variable_order x1 x2 then 1
wenzelm@23252
   736
     else if variable_order x2 x1 then ~1
wenzelm@23252
   737
     else if n1 < n2 then ~1
wenzelm@23252
   738
     else if n2 < n1 then 1
wenzelm@23252
   739
     else lexorder vs1 vs2
wenzelm@23252
   740
 in fn tm1 => fn tm2 =>
wenzelm@23252
   741
  let val vdegs1 = map dest_varpow (powervars tm1)
wenzelm@23252
   742
      val vdegs2 = map dest_varpow (powervars tm2)
wenzelm@33002
   743
      val deg1 = fold (Integer.add o snd) vdegs1 num_0
wenzelm@33002
   744
      val deg2 = fold (Integer.add o snd) vdegs2 num_0
wenzelm@23252
   745
  in if deg1 < deg2 then ~1 else if deg1 > deg2 then 1
wenzelm@23252
   746
                            else lexorder vdegs1 vdegs2
wenzelm@23252
   747
  end
wenzelm@23252
   748
 end;
wenzelm@23252
   749
wenzelm@23252
   750
(* Addition of two polynomials.                                              *)
wenzelm@23252
   751
wenzelm@23252
   752
val polynomial_add_conv =
wenzelm@23252
   753
 let
wenzelm@23252
   754
 fun dezero_rule th =
wenzelm@23252
   755
  let
wenzelm@23252
   756
   val tm = concl th
wenzelm@23252
   757
  in
wenzelm@23252
   758
   if not(is_add tm) then th else
wenzelm@23252
   759
   let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   760
       val l = Thm.dest_arg lopr
wenzelm@23252
   761
   in
wenzelm@23252
   762
    if l aconvc zero_tm
wenzelm@23252
   763
    then transitive th (inst_thm [(ca,r)] pthm_07)   else
wenzelm@23252
   764
        if r aconvc zero_tm
wenzelm@23252
   765
        then transitive th (inst_thm [(ca,l)] pthm_08)  else th
wenzelm@23252
   766
   end
wenzelm@23252
   767
  end
wenzelm@23252
   768
 fun padd tm =
wenzelm@23252
   769
  let
wenzelm@23252
   770
   val (l,r) = dest_add tm
wenzelm@23252
   771
  in
wenzelm@23252
   772
   if l aconvc zero_tm then inst_thm [(ca,r)] pthm_07
wenzelm@23252
   773
   else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_08
wenzelm@23252
   774
   else
wenzelm@23252
   775
    if is_add l
wenzelm@23252
   776
    then
wenzelm@23252
   777
     let val (a,b) = dest_add l
wenzelm@23252
   778
     in
wenzelm@23252
   779
     if is_add r then
wenzelm@23252
   780
      let val (c,d) = dest_add r
wenzelm@23252
   781
          val ord = morder a c
wenzelm@23252
   782
      in
wenzelm@23252
   783
       if ord = 0 then
wenzelm@23252
   784
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,c),(cd,d)] pthm_23
wenzelm@23252
   785
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   786
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   787
            val th2 = Drule.arg_cong_rule tm3 (monomial_add_conv tm4)
wenzelm@23252
   788
        in dezero_rule (transitive th1 (combination th2 (padd tm2)))
wenzelm@23252
   789
        end
wenzelm@23252
   790
       else (* ord <> 0*)
wenzelm@23252
   791
        let val th1 =
wenzelm@23252
   792
                if ord > 0 then inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   793
                else inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   794
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   795
        in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   796
        end
wenzelm@23252
   797
      end
wenzelm@23252
   798
     else (* not (is_add r)*)
wenzelm@23252
   799
      let val ord = morder a r
wenzelm@23252
   800
      in
wenzelm@23252
   801
       if ord = 0 then
wenzelm@23252
   802
        let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_26
wenzelm@23252
   803
            val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   804
            val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   805
            val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   806
        in dezero_rule (transitive th1 th2)
wenzelm@23252
   807
        end
wenzelm@23252
   808
       else (* ord <> 0*)
wenzelm@23252
   809
        if ord > 0 then
wenzelm@23252
   810
          let val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_24
wenzelm@23252
   811
              val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   812
          in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   813
          end
wenzelm@23252
   814
        else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   815
      end
wenzelm@23252
   816
    end
wenzelm@23252
   817
   else (* not (is_add l)*)
wenzelm@23252
   818
    if is_add r then
wenzelm@23252
   819
      let val (c,d) = dest_add r
wenzelm@23252
   820
          val  ord = morder l c
wenzelm@23252
   821
      in
wenzelm@23252
   822
       if ord = 0 then
wenzelm@23252
   823
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_28
wenzelm@23252
   824
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   825
             val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   826
             val th2 = Drule.fun_cong_rule (Drule.arg_cong_rule tm3 (monomial_add_conv tm4)) tm2
wenzelm@23252
   827
         in dezero_rule (transitive th1 th2)
wenzelm@23252
   828
         end
wenzelm@23252
   829
       else
wenzelm@23252
   830
        if ord > 0 then reflexive tm
wenzelm@23252
   831
        else
wenzelm@23252
   832
         let val th1 = inst_thm [(ca,l),(cc,c),(cd,d)] pthm_25
wenzelm@23252
   833
             val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   834
         in dezero_rule (transitive th1 (Drule.arg_cong_rule tm1 (padd tm2)))
wenzelm@23252
   835
         end
wenzelm@23252
   836
      end
wenzelm@23252
   837
    else
wenzelm@23252
   838
     let val ord = morder l r
wenzelm@23252
   839
     in
wenzelm@23252
   840
      if ord = 0 then monomial_add_conv tm
wenzelm@23252
   841
      else if ord > 0 then dezero_rule(reflexive tm)
wenzelm@23252
   842
      else dezero_rule (inst_thm [(ca,l),(cc,r)] pthm_27)
wenzelm@23252
   843
     end
wenzelm@23252
   844
  end
wenzelm@23252
   845
 in padd
wenzelm@23252
   846
 end;
wenzelm@23252
   847
wenzelm@23252
   848
(* Multiplication of two polynomials.                                        *)
wenzelm@23252
   849
wenzelm@23252
   850
val polynomial_mul_conv =
wenzelm@23252
   851
 let
wenzelm@23252
   852
  fun pmul tm =
wenzelm@23252
   853
   let val (l,r) = dest_mul tm
wenzelm@23252
   854
   in
wenzelm@23252
   855
    if not(is_add l) then polynomial_monomial_mul_conv tm
wenzelm@23252
   856
    else
wenzelm@23252
   857
     if not(is_add r) then
wenzelm@23252
   858
      let val th1 = inst_thm [(ca,l),(cb,r)] pthm_09
wenzelm@23252
   859
      in transitive th1 (polynomial_monomial_mul_conv(concl th1))
wenzelm@23252
   860
      end
wenzelm@23252
   861
     else
wenzelm@23252
   862
       let val (a,b) = dest_add l
wenzelm@23252
   863
           val th1 = inst_thm [(ca,a),(cb,b),(cc,r)] pthm_10
wenzelm@23252
   864
           val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   865
           val (tm3,tm4) = Thm.dest_comb tm1
wenzelm@23252
   866
           val th2 = Drule.arg_cong_rule tm3 (polynomial_monomial_mul_conv tm4)
wenzelm@23252
   867
           val th3 = transitive th1 (combination th2 (pmul tm2))
wenzelm@23252
   868
       in transitive th3 (polynomial_add_conv (concl th3))
wenzelm@23252
   869
       end
wenzelm@23252
   870
   end
wenzelm@23252
   871
 in fn tm =>
wenzelm@23252
   872
   let val (l,r) = dest_mul tm
wenzelm@23252
   873
   in
wenzelm@23252
   874
    if l aconvc zero_tm then inst_thm [(ca,r)] pthm_11
wenzelm@23252
   875
    else if r aconvc zero_tm then inst_thm [(ca,l)] pthm_12
wenzelm@23252
   876
    else if l aconvc one_tm then inst_thm [(ca,r)] pthm_13
wenzelm@23252
   877
    else if r aconvc one_tm then inst_thm [(ca,l)] pthm_14
wenzelm@23252
   878
    else pmul tm
wenzelm@23252
   879
   end
wenzelm@23252
   880
 end;
wenzelm@23252
   881
wenzelm@23252
   882
(* Power of polynomial (optimized for the monomial and trivial cases).       *)
wenzelm@23252
   883
wenzelm@23580
   884
fun num_conv n =
wenzelm@23580
   885
  nat_add_conv (Thm.capply @{cterm Suc} (Numeral.mk_cnumber @{ctyp nat} (dest_numeral n - 1)))
wenzelm@23580
   886
  |> Thm.symmetric;
wenzelm@23252
   887
wenzelm@23252
   888
wenzelm@23252
   889
val polynomial_pow_conv =
wenzelm@23252
   890
 let
wenzelm@23252
   891
  fun ppow tm =
wenzelm@23252
   892
    let val (l,n) = dest_pow tm
wenzelm@23252
   893
    in
wenzelm@23252
   894
     if n aconvc zeron_tm then inst_thm [(cx,l)] pthm_35
wenzelm@23252
   895
     else if n aconvc onen_tm then inst_thm [(cx,l)] pthm_36
wenzelm@23252
   896
     else
wenzelm@23252
   897
         let val th1 = num_conv n
wenzelm@23252
   898
             val th2 = inst_thm [(cx,l),(cq,Thm.dest_arg (concl th1))] pthm_38
wenzelm@23252
   899
             val (tm1,tm2) = Thm.dest_comb(concl th2)
wenzelm@23252
   900
             val th3 = transitive th2 (Drule.arg_cong_rule tm1 (ppow tm2))
wenzelm@23252
   901
             val th4 = transitive (Drule.arg_cong_rule (Thm.dest_fun tm) th1) th3
wenzelm@23252
   902
         in transitive th4 (polynomial_mul_conv (concl th4))
wenzelm@23252
   903
         end
wenzelm@23252
   904
    end
wenzelm@23252
   905
 in fn tm =>
wenzelm@23252
   906
       if is_add(Thm.dest_arg1 tm) then ppow tm else monomial_pow_conv tm
wenzelm@23252
   907
 end;
wenzelm@23252
   908
wenzelm@23252
   909
(* Negation.                                                                 *)
wenzelm@23252
   910
wenzelm@23580
   911
fun polynomial_neg_conv tm =
wenzelm@23252
   912
   let val (l,r) = Thm.dest_comb tm in
wenzelm@23252
   913
        if not (l aconvc neg_tm) then raise CTERM ("polynomial_neg_conv",[tm]) else
wenzelm@23252
   914
        let val th1 = inst_thm [(cx',r)] neg_mul
haftmann@36709
   915
            val th2 = transitive th1 (Conv.arg1_conv semiring_mul_conv (concl th1))
wenzelm@23252
   916
        in transitive th2 (polynomial_monomial_mul_conv (concl th2))
wenzelm@23252
   917
        end
wenzelm@23252
   918
   end;
wenzelm@23252
   919
wenzelm@23252
   920
wenzelm@23252
   921
(* Subtraction.                                                              *)
wenzelm@23580
   922
fun polynomial_sub_conv tm =
wenzelm@23252
   923
  let val (l,r) = dest_sub tm
wenzelm@23252
   924
      val th1 = inst_thm [(cx',l),(cy',r)] sub_add
wenzelm@23252
   925
      val (tm1,tm2) = Thm.dest_comb(concl th1)
wenzelm@23252
   926
      val th2 = Drule.arg_cong_rule tm1 (polynomial_neg_conv tm2)
wenzelm@23252
   927
  in transitive th1 (transitive th2 (polynomial_add_conv (concl th2)))
wenzelm@23252
   928
  end;
wenzelm@23252
   929
wenzelm@23252
   930
(* Conversion from HOL term.                                                 *)
wenzelm@23252
   931
wenzelm@23252
   932
fun polynomial_conv tm =
chaieb@23407
   933
 if is_semiring_constant tm then semiring_add_conv tm
chaieb@23407
   934
 else if not(is_comb tm) then reflexive tm
wenzelm@23252
   935
 else
wenzelm@23252
   936
  let val (lopr,r) = Thm.dest_comb tm
wenzelm@23252
   937
  in if lopr aconvc neg_tm then
wenzelm@23252
   938
       let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r)
wenzelm@23252
   939
       in transitive th1 (polynomial_neg_conv (concl th1))
wenzelm@23252
   940
       end
chaieb@30866
   941
     else if lopr aconvc inverse_tm then
chaieb@30866
   942
       let val th1 = Drule.arg_cong_rule lopr (polynomial_conv r)
chaieb@30866
   943
       in transitive th1 (semiring_mul_conv (concl th1))
chaieb@30866
   944
       end
wenzelm@23252
   945
     else
wenzelm@23252
   946
       if not(is_comb lopr) then reflexive tm
wenzelm@23252
   947
       else
wenzelm@23252
   948
         let val (opr,l) = Thm.dest_comb lopr
wenzelm@23252
   949
         in if opr aconvc pow_tm andalso is_numeral r
wenzelm@23252
   950
            then
wenzelm@23252
   951
              let val th1 = Drule.fun_cong_rule (Drule.arg_cong_rule opr (polynomial_conv l)) r
wenzelm@23252
   952
              in transitive th1 (polynomial_pow_conv (concl th1))
wenzelm@23252
   953
              end
chaieb@30866
   954
         else if opr aconvc divide_tm 
chaieb@30866
   955
            then
chaieb@30866
   956
              let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) 
chaieb@30866
   957
                                        (polynomial_conv r)
haftmann@36709
   958
                  val th2 = (Conv.rewr_conv divide_inverse then_conv polynomial_mul_conv)
chaieb@30866
   959
                              (Thm.rhs_of th1)
chaieb@30866
   960
              in transitive th1 th2
chaieb@30866
   961
              end
wenzelm@23252
   962
            else
wenzelm@23252
   963
              if opr aconvc add_tm orelse opr aconvc mul_tm orelse opr aconvc sub_tm
wenzelm@23252
   964
              then
wenzelm@23252
   965
               let val th1 = combination (Drule.arg_cong_rule opr (polynomial_conv l)) (polynomial_conv r)
wenzelm@23252
   966
                   val f = if opr aconvc add_tm then polynomial_add_conv
wenzelm@23252
   967
                      else if opr aconvc mul_tm then polynomial_mul_conv
wenzelm@23252
   968
                      else polynomial_sub_conv
wenzelm@23252
   969
               in transitive th1 (f (concl th1))
wenzelm@23252
   970
               end
wenzelm@23252
   971
              else reflexive tm
wenzelm@23252
   972
         end
wenzelm@23252
   973
  end;
wenzelm@23252
   974
 in
wenzelm@23252
   975
   {main = polynomial_conv,
wenzelm@23252
   976
    add = polynomial_add_conv,
wenzelm@23252
   977
    mul = polynomial_mul_conv,
wenzelm@23252
   978
    pow = polynomial_pow_conv,
wenzelm@23252
   979
    neg = polynomial_neg_conv,
wenzelm@23252
   980
    sub = polynomial_sub_conv}
wenzelm@23252
   981
 end
wenzelm@23252
   982
end;
wenzelm@23252
   983
wenzelm@35410
   984
val nat_exp_ss =
wenzelm@35410
   985
  HOL_basic_ss addsimps (@{thms nat_number} @ @{thms nat_arith} @ @{thms arith_simps} @ @{thms rel_simps})
wenzelm@35410
   986
    addsimps [@{thm Let_def}, @{thm if_False}, @{thm if_True}, @{thm Nat.add_0}, @{thm add_Suc}];
wenzelm@23252
   987
wenzelm@35408
   988
fun simple_cterm_ord t u = Term_Ord.term_ord (term_of t, term_of u) = LESS;
chaieb@27222
   989
haftmann@36710
   990
haftmann@36710
   991
(* various normalizing conversions *)
haftmann@36710
   992
chaieb@30866
   993
fun semiring_normalizers_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, 
chaieb@23407
   994
                                     {conv, dest_const, mk_const, is_const}) ord =
wenzelm@23252
   995
  let
wenzelm@23252
   996
    val pow_conv =
haftmann@36709
   997
      Conv.arg_conv (Simplifier.rewrite nat_exp_ss)
wenzelm@23252
   998
      then_conv Simplifier.rewrite
wenzelm@23252
   999
        (HOL_basic_ss addsimps [nth (snd semiring) 31, nth (snd semiring) 34])
chaieb@23330
  1000
      then_conv conv ctxt
chaieb@23330
  1001
    val dat = (is_const, conv ctxt, conv ctxt, pow_conv)
chaieb@30866
  1002
  in semiring_normalizers_conv vars semiring ring field dat ord end;
chaieb@27222
  1003
chaieb@30866
  1004
fun semiring_normalize_ord_wrapper ctxt ({vars, semiring, ring, field, idom, ideal}, {conv, dest_const, mk_const, is_const}) ord =
chaieb@30866
  1005
 #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
  1006
chaieb@23407
  1007
fun semiring_normalize_wrapper ctxt data = 
chaieb@23407
  1008
  semiring_normalize_ord_wrapper ctxt data simple_cterm_ord;
chaieb@23407
  1009
chaieb@23407
  1010
fun semiring_normalize_ord_conv ctxt ord tm =
haftmann@36700
  1011
  (case match ctxt tm of
wenzelm@23252
  1012
    NONE => reflexive tm
chaieb@23407
  1013
  | SOME res => semiring_normalize_ord_wrapper ctxt res ord tm);
chaieb@23407
  1014
 
chaieb@23407
  1015
fun semiring_normalize_conv ctxt = semiring_normalize_ord_conv ctxt simple_cterm_ord;
wenzelm@23252
  1016
haftmann@36708
  1017
haftmann@36708
  1018
(** Isar setup **)
haftmann@36708
  1019
haftmann@36708
  1020
local
haftmann@36708
  1021
haftmann@36708
  1022
fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ();
haftmann@36708
  1023
fun keyword2 k1 k2 = Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.colon) >> K ();
haftmann@36708
  1024
fun keyword3 k1 k2 k3 =
haftmann@36708
  1025
  Scan.lift (Args.$$$ k1 -- Args.$$$ k2 -- Args.$$$ k3 -- Args.colon) >> K ();
haftmann@36708
  1026
haftmann@36708
  1027
val opsN = "ops";
haftmann@36708
  1028
val rulesN = "rules";
haftmann@36708
  1029
haftmann@36708
  1030
val normN = "norm";
haftmann@36708
  1031
val constN = "const";
haftmann@36708
  1032
val delN = "del";
haftmann@36700
  1033
haftmann@36708
  1034
val any_keyword =
haftmann@36708
  1035
  keyword2 semiringN opsN || keyword2 semiringN rulesN ||
haftmann@36708
  1036
  keyword2 ringN opsN || keyword2 ringN rulesN ||
haftmann@36708
  1037
  keyword2 fieldN opsN || keyword2 fieldN rulesN ||
haftmann@36708
  1038
  keyword2 idomN rulesN || keyword2 idealN rulesN;
haftmann@36708
  1039
haftmann@36708
  1040
val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
haftmann@36708
  1041
val terms = thms >> map Drule.dest_term;
haftmann@36708
  1042
haftmann@36708
  1043
fun optional scan = Scan.optional scan [];
haftmann@36708
  1044
haftmann@36708
  1045
in
haftmann@36708
  1046
haftmann@36708
  1047
val setup =
haftmann@36708
  1048
  Attrib.setup @{binding normalizer}
haftmann@36708
  1049
    (Scan.lift (Args.$$$ delN >> K del) ||
haftmann@36708
  1050
      ((keyword2 semiringN opsN |-- terms) --
haftmann@36708
  1051
       (keyword2 semiringN rulesN |-- thms)) --
haftmann@36708
  1052
      (optional (keyword2 ringN opsN |-- terms) --
haftmann@36708
  1053
       optional (keyword2 ringN rulesN |-- thms)) --
haftmann@36708
  1054
      (optional (keyword2 fieldN opsN |-- terms) --
haftmann@36708
  1055
       optional (keyword2 fieldN rulesN |-- thms)) --
haftmann@36708
  1056
      optional (keyword2 idomN rulesN |-- thms) --
haftmann@36708
  1057
      optional (keyword2 idealN rulesN |-- thms)
haftmann@36708
  1058
      >> (fn ((((sr, r), f), id), idl) => 
haftmann@36708
  1059
             add {semiring = sr, ring = r, field = f, idom = id, ideal = idl}))
haftmann@36708
  1060
    "semiring normalizer data";
haftmann@36700
  1061
wenzelm@23252
  1062
end;
haftmann@36708
  1063
haftmann@36708
  1064
end;