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