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