src/HOL/Groebner_Basis.thy
author haftmann
Wed May 05 16:46:19 2010 +0200 (2010-05-05)
changeset 36698 45f1a487cd27
parent 36409 d323e7773aa8
child 36699 816da1023508
permissions -rw-r--r--
dropped unused file
     1 (*  Title:      HOL/Groebner_Basis.thy
     2     Author:     Amine Chaieb, TU Muenchen
     3 *)
     4 
     5 header {* Semiring normalization and Groebner Bases *}
     6 
     7 theory Groebner_Basis
     8 imports Numeral_Simprocs
     9 uses
    10   "Tools/Groebner_Basis/normalizer_data.ML"
    11   ("Tools/Groebner_Basis/normalizer.ML")
    12   ("Tools/Groebner_Basis/groebner.ML")
    13 begin
    14 
    15 subsection {* Semiring normalization *}
    16 
    17 setup NormalizerData.setup
    18 
    19 
    20 locale gb_semiring =
    21   fixes add mul pwr r0 r1
    22   assumes add_a:"(add x (add y z) = add (add x y) z)"
    23     and add_c: "add x y = add y x" and add_0:"add r0 x = x"
    24     and mul_a:"mul x (mul y z) = mul (mul x y) z" and mul_c:"mul x y = mul y x"
    25     and mul_1:"mul r1 x = x" and  mul_0:"mul r0 x = r0"
    26     and mul_d:"mul x (add y z) = add (mul x y) (mul x z)"
    27     and pwr_0:"pwr x 0 = r1" and pwr_Suc:"pwr x (Suc n) = mul x (pwr x n)"
    28 begin
    29 
    30 lemma mul_pwr:"mul (pwr x p) (pwr x q) = pwr x (p + q)"
    31 proof (induct p)
    32   case 0
    33   then show ?case by (auto simp add: pwr_0 mul_1)
    34 next
    35   case Suc
    36   from this [symmetric] show ?case
    37     by (auto simp add: pwr_Suc mul_1 mul_a)
    38 qed
    39 
    40 lemma pwr_mul: "pwr (mul x y) q = mul (pwr x q) (pwr y q)"
    41 proof (induct q arbitrary: x y, auto simp add:pwr_0 pwr_Suc mul_1)
    42   fix q x y
    43   assume "\<And>x y. pwr (mul x y) q = mul (pwr x q) (pwr y q)"
    44   have "mul (mul x y) (mul (pwr x q) (pwr y q)) = mul x (mul y (mul (pwr x q) (pwr y q)))"
    45     by (simp add: mul_a)
    46   also have "\<dots> = (mul (mul y (mul (pwr y q) (pwr x q))) x)" by (simp add: mul_c)
    47   also have "\<dots> = (mul (mul y (pwr y q)) (mul (pwr x q) x))" by (simp add: mul_a)
    48   finally show "mul (mul x y) (mul (pwr x q) (pwr y q)) =
    49     mul (mul x (pwr x q)) (mul y (pwr y q))" by (simp add: mul_c)
    50 qed
    51 
    52 lemma pwr_pwr: "pwr (pwr x p) q = pwr x (p * q)"
    53 proof (induct p arbitrary: q)
    54   case 0
    55   show ?case using pwr_Suc mul_1 pwr_0 by (induct q) auto
    56 next
    57   case Suc
    58   thus ?case by (auto simp add: mul_pwr [symmetric] pwr_mul pwr_Suc)
    59 qed
    60 
    61 
    62 subsubsection {* Declaring the abstract theory *}
    63 
    64 lemma semiring_ops:
    65   shows "TERM (add x y)" and "TERM (mul x y)" and "TERM (pwr x n)"
    66     and "TERM r0" and "TERM r1" .
    67 
    68 lemma semiring_rules:
    69   "add (mul a m) (mul b m) = mul (add a b) m"
    70   "add (mul a m) m = mul (add a r1) m"
    71   "add m (mul a m) = mul (add a r1) m"
    72   "add m m = mul (add r1 r1) m"
    73   "add r0 a = a"
    74   "add a r0 = a"
    75   "mul a b = mul b a"
    76   "mul (add a b) c = add (mul a c) (mul b c)"
    77   "mul r0 a = r0"
    78   "mul a r0 = r0"
    79   "mul r1 a = a"
    80   "mul a r1 = a"
    81   "mul (mul lx ly) (mul rx ry) = mul (mul lx rx) (mul ly ry)"
    82   "mul (mul lx ly) (mul rx ry) = mul lx (mul ly (mul rx ry))"
    83   "mul (mul lx ly) (mul rx ry) = mul rx (mul (mul lx ly) ry)"
    84   "mul (mul lx ly) rx = mul (mul lx rx) ly"
    85   "mul (mul lx ly) rx = mul lx (mul ly rx)"
    86   "mul lx (mul rx ry) = mul (mul lx rx) ry"
    87   "mul lx (mul rx ry) = mul rx (mul lx ry)"
    88   "add (add a b) (add c d) = add (add a c) (add b d)"
    89   "add (add a b) c = add a (add b c)"
    90   "add a (add c d) = add c (add a d)"
    91   "add (add a b) c = add (add a c) b"
    92   "add a c = add c a"
    93   "add a (add c d) = add (add a c) d"
    94   "mul (pwr x p) (pwr x q) = pwr x (p + q)"
    95   "mul x (pwr x q) = pwr x (Suc q)"
    96   "mul (pwr x q) x = pwr x (Suc q)"
    97   "mul x x = pwr x 2"
    98   "pwr (mul x y) q = mul (pwr x q) (pwr y q)"
    99   "pwr (pwr x p) q = pwr x (p * q)"
   100   "pwr x 0 = r1"
   101   "pwr x 1 = x"
   102   "mul x (add y z) = add (mul x y) (mul x z)"
   103   "pwr x (Suc q) = mul x (pwr x q)"
   104   "pwr x (2*n) = mul (pwr x n) (pwr x n)"
   105   "pwr x (Suc (2*n)) = mul x (mul (pwr x n) (pwr x n))"
   106 proof -
   107   show "add (mul a m) (mul b m) = mul (add a b) m" using mul_d mul_c by simp
   108 next show"add (mul a m) m = mul (add a r1) m" using mul_d mul_c mul_1 by simp
   109 next show "add m (mul a m) = mul (add a r1) m" using mul_c mul_d mul_1 add_c by simp
   110 next show "add m m = mul (add r1 r1) m" using mul_c mul_d mul_1 by simp
   111 next show "add r0 a = a" using add_0 by simp
   112 next show "add a r0 = a" using add_0 add_c by simp
   113 next show "mul a b = mul b a" using mul_c by simp
   114 next show "mul (add a b) c = add (mul a c) (mul b c)" using mul_c mul_d by simp
   115 next show "mul r0 a = r0" using mul_0 by simp
   116 next show "mul a r0 = r0" using mul_0 mul_c by simp
   117 next show "mul r1 a = a" using mul_1 by simp
   118 next show "mul a r1 = a" using mul_1 mul_c by simp
   119 next show "mul (mul lx ly) (mul rx ry) = mul (mul lx rx) (mul ly ry)"
   120     using mul_c mul_a by simp
   121 next show "mul (mul lx ly) (mul rx ry) = mul lx (mul ly (mul rx ry))"
   122     using mul_a by simp
   123 next
   124   have "mul (mul lx ly) (mul rx ry) = mul (mul rx ry) (mul lx ly)" by (rule mul_c)
   125   also have "\<dots> = mul rx (mul ry (mul lx ly))" using mul_a by simp
   126   finally
   127   show "mul (mul lx ly) (mul rx ry) = mul rx (mul (mul lx ly) ry)"
   128     using mul_c by simp
   129 next show "mul (mul lx ly) rx = mul (mul lx rx) ly" using mul_c mul_a by simp
   130 next
   131   show "mul (mul lx ly) rx = mul lx (mul ly rx)" by (simp add: mul_a)
   132 next show "mul lx (mul rx ry) = mul (mul lx rx) ry" by (simp add: mul_a )
   133 next show "mul lx (mul rx ry) = mul rx (mul lx ry)" by (simp add: mul_a,simp add: mul_c)
   134 next show "add (add a b) (add c d) = add (add a c) (add b d)"
   135     using add_c add_a by simp
   136 next show "add (add a b) c = add a (add b c)" using add_a by simp
   137 next show "add a (add c d) = add c (add a d)"
   138     apply (simp add: add_a) by (simp only: add_c)
   139 next show "add (add a b) c = add (add a c) b" using add_a add_c by simp
   140 next show "add a c = add c a" by (rule add_c)
   141 next show "add a (add c d) = add (add a c) d" using add_a by simp
   142 next show "mul (pwr x p) (pwr x q) = pwr x (p + q)" by (rule mul_pwr)
   143 next show "mul x (pwr x q) = pwr x (Suc q)" using pwr_Suc by simp
   144 next show "mul (pwr x q) x = pwr x (Suc q)" using pwr_Suc mul_c by simp
   145 next show "mul x x = pwr x 2" by (simp add: nat_number' pwr_Suc pwr_0 mul_1 mul_c)
   146 next show "pwr (mul x y) q = mul (pwr x q) (pwr y q)" by (rule pwr_mul)
   147 next show "pwr (pwr x p) q = pwr x (p * q)" by (rule pwr_pwr)
   148 next show "pwr x 0 = r1" using pwr_0 .
   149 next show "pwr x 1 = x" unfolding One_nat_def by (simp add: nat_number' pwr_Suc pwr_0 mul_1 mul_c)
   150 next show "mul x (add y z) = add (mul x y) (mul x z)" using mul_d by simp
   151 next show "pwr x (Suc q) = mul x (pwr x q)" using pwr_Suc by simp
   152 next show "pwr x (2 * n) = mul (pwr x n) (pwr x n)" by (simp add: nat_number' mul_pwr)
   153 next show "pwr x (Suc (2 * n)) = mul x (mul (pwr x n) (pwr x n))"
   154     by (simp add: nat_number' pwr_Suc mul_pwr)
   155 qed
   156 
   157 
   158 lemmas gb_semiring_axioms' =
   159   gb_semiring_axioms [normalizer
   160     semiring ops: semiring_ops
   161     semiring rules: semiring_rules]
   162 
   163 end
   164 
   165 interpretation class_semiring: gb_semiring
   166     "op +" "op *" "op ^" "0::'a::{comm_semiring_1}" "1"
   167   proof qed (auto simp add: algebra_simps)
   168 
   169 lemmas nat_arith =
   170   add_nat_number_of
   171   diff_nat_number_of
   172   mult_nat_number_of
   173   eq_nat_number_of
   174   less_nat_number_of
   175 
   176 lemma not_iszero_Numeral1: "\<not> iszero (Numeral1::'a::number_ring)"
   177   by simp
   178 
   179 lemmas comp_arith =
   180   Let_def arith_simps nat_arith rel_simps neg_simps if_False
   181   if_True add_0 add_Suc add_number_of_left mult_number_of_left
   182   numeral_1_eq_1[symmetric] Suc_eq_plus1
   183   numeral_0_eq_0[symmetric] numerals[symmetric]
   184   iszero_simps not_iszero_Numeral1
   185 
   186 lemmas semiring_norm = comp_arith
   187 
   188 ML {*
   189 local
   190 
   191 open Conv;
   192 
   193 fun numeral_is_const ct = can HOLogic.dest_number (Thm.term_of ct);
   194 
   195 fun int_of_rat x =
   196   (case Rat.quotient_of_rat x of (i, 1) => i
   197   | _ => error "int_of_rat: bad int");
   198 
   199 val numeral_conv =
   200   Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm}) then_conv
   201   Simplifier.rewrite (HOL_basic_ss addsimps
   202     (@{thms numeral_1_eq_1} @ @{thms numeral_0_eq_0} @ @{thms numerals(1-2)}));
   203 
   204 in
   205 
   206 fun normalizer_funs key =
   207   NormalizerData.funs key
   208    {is_const = fn phi => numeral_is_const,
   209     dest_const = fn phi => fn ct =>
   210       Rat.rat_of_int (snd
   211         (HOLogic.dest_number (Thm.term_of ct)
   212           handle TERM _ => error "ring_dest_const")),
   213     mk_const = fn phi => fn cT => fn x => Numeral.mk_cnumber cT (int_of_rat x),
   214     conv = fn phi => K numeral_conv}
   215 
   216 end
   217 *}
   218 
   219 declaration {* normalizer_funs @{thm class_semiring.gb_semiring_axioms'} *}
   220 
   221 
   222 locale gb_ring = gb_semiring +
   223   fixes sub :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   224     and neg :: "'a \<Rightarrow> 'a"
   225   assumes neg_mul: "neg x = mul (neg r1) x"
   226     and sub_add: "sub x y = add x (neg y)"
   227 begin
   228 
   229 lemma ring_ops: shows "TERM (sub x y)" and "TERM (neg x)" .
   230 
   231 lemmas ring_rules = neg_mul sub_add
   232 
   233 lemmas gb_ring_axioms' =
   234   gb_ring_axioms [normalizer
   235     semiring ops: semiring_ops
   236     semiring rules: semiring_rules
   237     ring ops: ring_ops
   238     ring rules: ring_rules]
   239 
   240 end
   241 
   242 
   243 interpretation class_ring: gb_ring "op +" "op *" "op ^"
   244     "0::'a::{comm_semiring_1,number_ring}" 1 "op -" "uminus"
   245   proof qed simp_all
   246 
   247 
   248 declaration {* normalizer_funs @{thm class_ring.gb_ring_axioms'} *}
   249 
   250 use "Tools/Groebner_Basis/normalizer.ML"
   251 
   252 
   253 method_setup sring_norm = {*
   254   Scan.succeed (SIMPLE_METHOD' o Normalizer.semiring_normalize_tac)
   255 *} "semiring normalizer"
   256 
   257 
   258 locale gb_field = gb_ring +
   259   fixes divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   260     and inverse:: "'a \<Rightarrow> 'a"
   261   assumes divide_inverse: "divide x y = mul x (inverse y)"
   262      and inverse_divide: "inverse x = divide r1 x"
   263 begin
   264 
   265 lemma field_ops: shows "TERM (divide x y)" and "TERM (inverse x)" .
   266 
   267 lemmas field_rules = divide_inverse inverse_divide
   268 
   269 lemmas gb_field_axioms' =
   270   gb_field_axioms [normalizer
   271     semiring ops: semiring_ops
   272     semiring rules: semiring_rules
   273     ring ops: ring_ops
   274     ring rules: ring_rules
   275     field ops: field_ops
   276     field rules: field_rules]
   277 
   278 end
   279 
   280 
   281 subsection {* Groebner Bases *}
   282 
   283 locale semiringb = gb_semiring +
   284   assumes add_cancel: "add (x::'a) y = add x z \<longleftrightarrow> y = z"
   285   and add_mul_solve: "add (mul w y) (mul x z) =
   286     add (mul w z) (mul x y) \<longleftrightarrow> w = x \<or> y = z"
   287 begin
   288 
   289 lemma noteq_reduce: "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> add (mul a c) (mul b d) \<noteq> add (mul a d) (mul b c)"
   290 proof-
   291   have "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> \<not> (a = b \<or> c = d)" by simp
   292   also have "\<dots> \<longleftrightarrow> add (mul a c) (mul b d) \<noteq> add (mul a d) (mul b c)"
   293     using add_mul_solve by blast
   294   finally show "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> add (mul a c) (mul b d) \<noteq> add (mul a d) (mul b c)"
   295     by simp
   296 qed
   297 
   298 lemma add_scale_eq_noteq: "\<lbrakk>r \<noteq> r0 ; (a = b) \<and> ~(c = d)\<rbrakk>
   299   \<Longrightarrow> add a (mul r c) \<noteq> add b (mul r d)"
   300 proof(clarify)
   301   assume nz: "r\<noteq> r0" and cnd: "c\<noteq>d"
   302     and eq: "add b (mul r c) = add b (mul r d)"
   303   hence "mul r c = mul r d" using cnd add_cancel by simp
   304   hence "add (mul r0 d) (mul r c) = add (mul r0 c) (mul r d)"
   305     using mul_0 add_cancel by simp
   306   thus "False" using add_mul_solve nz cnd by simp
   307 qed
   308 
   309 lemma add_r0_iff: " x = add x a \<longleftrightarrow> a = r0"
   310 proof-
   311   have "a = r0 \<longleftrightarrow> add x a = add x r0" by (simp add: add_cancel)
   312   thus "x = add x a \<longleftrightarrow> a = r0" by (auto simp add: add_c add_0)
   313 qed
   314 
   315 declare gb_semiring_axioms' [normalizer del]
   316 
   317 lemmas semiringb_axioms' = semiringb_axioms [normalizer
   318   semiring ops: semiring_ops
   319   semiring rules: semiring_rules
   320   idom rules: noteq_reduce add_scale_eq_noteq]
   321 
   322 end
   323 
   324 locale ringb = semiringb + gb_ring + 
   325   assumes subr0_iff: "sub x y = r0 \<longleftrightarrow> x = y"
   326 begin
   327 
   328 declare gb_ring_axioms' [normalizer del]
   329 
   330 lemmas ringb_axioms' = ringb_axioms [normalizer
   331   semiring ops: semiring_ops
   332   semiring rules: semiring_rules
   333   ring ops: ring_ops
   334   ring rules: ring_rules
   335   idom rules: noteq_reduce add_scale_eq_noteq
   336   ideal rules: subr0_iff add_r0_iff]
   337 
   338 end
   339 
   340 
   341 lemma no_zero_divirors_neq0:
   342   assumes az: "(a::'a::no_zero_divisors) \<noteq> 0"
   343     and ab: "a*b = 0" shows "b = 0"
   344 proof -
   345   { assume bz: "b \<noteq> 0"
   346     from no_zero_divisors [OF az bz] ab have False by blast }
   347   thus "b = 0" by blast
   348 qed
   349 
   350 interpretation class_ringb: ringb
   351   "op +" "op *" "op ^" "0::'a::{idom,number_ring}" "1" "op -" "uminus"
   352 proof(unfold_locales, simp add: algebra_simps, auto)
   353   fix w x y z ::"'a::{idom,number_ring}"
   354   assume p: "w * y + x * z = w * z + x * y" and ynz: "y \<noteq> z"
   355   hence ynz': "y - z \<noteq> 0" by simp
   356   from p have "w * y + x* z - w*z - x*y = 0" by simp
   357   hence "w* (y - z) - x * (y - z) = 0" by (simp add: algebra_simps)
   358   hence "(y - z) * (w - x) = 0" by (simp add: algebra_simps)
   359   with  no_zero_divirors_neq0 [OF ynz']
   360   have "w - x = 0" by blast
   361   thus "w = x"  by simp
   362 qed
   363 
   364 declaration {* normalizer_funs @{thm class_ringb.ringb_axioms'} *}
   365 
   366 interpretation natgb: semiringb
   367   "op +" "op *" "op ^" "0::nat" "1"
   368 proof (unfold_locales, simp add: algebra_simps)
   369   fix w x y z ::"nat"
   370   { assume p: "w * y + x * z = w * z + x * y" and ynz: "y \<noteq> z"
   371     hence "y < z \<or> y > z" by arith
   372     moreover {
   373       assume lt:"y <z" hence "\<exists>k. z = y + k \<and> k > 0" by (rule_tac x="z - y" in exI, auto)
   374       then obtain k where kp: "k>0" and yz:"z = y + k" by blast
   375       from p have "(w * y + x *y) + x*k = (w * y + x*y) + w*k" by (simp add: yz algebra_simps)
   376       hence "x*k = w*k" by simp
   377       hence "w = x" using kp by simp }
   378     moreover {
   379       assume lt: "y >z" hence "\<exists>k. y = z + k \<and> k>0" by (rule_tac x="y - z" in exI, auto)
   380       then obtain k where kp: "k>0" and yz:"y = z + k" by blast
   381       from p have "(w * z + x *z) + w*k = (w * z + x*z) + x*k" by (simp add: yz algebra_simps)
   382       hence "w*k = x*k" by simp
   383       hence "w = x" using kp by simp }
   384     ultimately have "w=x" by blast }
   385   thus "(w * y + x * z = w * z + x * y) = (w = x \<or> y = z)" by auto
   386 qed
   387 
   388 declaration {* normalizer_funs @{thm natgb.semiringb_axioms'} *}
   389 
   390 locale fieldgb = ringb + gb_field
   391 begin
   392 
   393 declare gb_field_axioms' [normalizer del]
   394 
   395 lemmas fieldgb_axioms' = fieldgb_axioms [normalizer
   396   semiring ops: semiring_ops
   397   semiring rules: semiring_rules
   398   ring ops: ring_ops
   399   ring rules: ring_rules
   400   field ops: field_ops
   401   field rules: field_rules
   402   idom rules: noteq_reduce add_scale_eq_noteq
   403   ideal rules: subr0_iff add_r0_iff]
   404 
   405 end
   406 
   407 
   408 lemmas bool_simps = simp_thms(1-34)
   409 lemma dnf:
   410     "(P & (Q | R)) = ((P&Q) | (P&R))" "((Q | R) & P) = ((Q&P) | (R&P))"
   411     "(P \<and> Q) = (Q \<and> P)" "(P \<or> Q) = (Q \<or> P)"
   412   by blast+
   413 
   414 lemmas weak_dnf_simps = dnf bool_simps
   415 
   416 lemma nnf_simps:
   417     "(\<not>(P \<and> Q)) = (\<not>P \<or> \<not>Q)" "(\<not>(P \<or> Q)) = (\<not>P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
   418     "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
   419   by blast+
   420 
   421 lemma PFalse:
   422     "P \<equiv> False \<Longrightarrow> \<not> P"
   423     "\<not> P \<Longrightarrow> (P \<equiv> False)"
   424   by auto
   425 use "Tools/Groebner_Basis/groebner.ML"
   426 
   427 method_setup algebra =
   428 {*
   429 let
   430  fun keyword k = Scan.lift (Args.$$$ k -- Args.colon) >> K ()
   431  val addN = "add"
   432  val delN = "del"
   433  val any_keyword = keyword addN || keyword delN
   434  val thms = Scan.repeat (Scan.unless any_keyword Attrib.multi_thm) >> flat;
   435 in
   436   ((Scan.optional (keyword addN |-- thms) []) -- 
   437    (Scan.optional (keyword delN |-- thms) [])) >>
   438   (fn (add_ths, del_ths) => fn ctxt =>
   439        SIMPLE_METHOD' (Groebner.algebra_tac add_ths del_ths ctxt))
   440 end
   441 *} "solve polynomial equations over (semi)rings and ideal membership problems using Groebner bases"
   442 declare dvd_def[algebra]
   443 declare dvd_eq_mod_eq_0[symmetric, algebra]
   444 declare mod_div_trivial[algebra]
   445 declare mod_mod_trivial[algebra]
   446 declare conjunct1[OF DIVISION_BY_ZERO, algebra]
   447 declare conjunct2[OF DIVISION_BY_ZERO, algebra]
   448 declare zmod_zdiv_equality[symmetric,algebra]
   449 declare zdiv_zmod_equality[symmetric, algebra]
   450 declare zdiv_zminus_zminus[algebra]
   451 declare zmod_zminus_zminus[algebra]
   452 declare zdiv_zminus2[algebra]
   453 declare zmod_zminus2[algebra]
   454 declare zdiv_zero[algebra]
   455 declare zmod_zero[algebra]
   456 declare mod_by_1[algebra]
   457 declare div_by_1[algebra]
   458 declare zmod_minus1_right[algebra]
   459 declare zdiv_minus1_right[algebra]
   460 declare mod_div_trivial[algebra]
   461 declare mod_mod_trivial[algebra]
   462 declare mod_mult_self2_is_0[algebra]
   463 declare mod_mult_self1_is_0[algebra]
   464 declare zmod_eq_0_iff[algebra]
   465 declare dvd_0_left_iff[algebra]
   466 declare zdvd1_eq[algebra]
   467 declare zmod_eq_dvd_iff[algebra]
   468 declare nat_mod_eq_iff[algebra]
   469 
   470 subsection{* Groebner Bases for fields *}
   471 
   472 interpretation class_fieldgb:
   473   fieldgb "op +" "op *" "op ^" "0::'a::{field,number_ring}" "1" "op -" "uminus" "op /" "inverse" apply (unfold_locales) by (simp_all add: divide_inverse)
   474 
   475 lemma divide_Numeral1: "(x::'a::{field, number_ring}) / Numeral1 = x" by simp
   476 lemma divide_Numeral0: "(x::'a::{field_inverse_zero, number_ring}) / Numeral0 = 0"
   477   by simp
   478 lemma mult_frac_frac: "((x::'a::field_inverse_zero) / y) * (z / w) = (x*z) / (y*w)"
   479   by simp
   480 lemma mult_frac_num: "((x::'a::field_inverse_zero) / y) * z  = (x*z) / y"
   481   by simp
   482 lemma mult_num_frac: "((x::'a::field_inverse_zero) / y) * z  = (x*z) / y"
   483   by simp
   484 
   485 lemma Numeral1_eq1_nat: "(1::nat) = Numeral1" by simp
   486 
   487 lemma add_frac_num: "y\<noteq> 0 \<Longrightarrow> (x::'a::field_inverse_zero) / y + z = (x + z*y) / y"
   488   by (simp add: add_divide_distrib)
   489 lemma add_num_frac: "y\<noteq> 0 \<Longrightarrow> z + (x::'a::field_inverse_zero) / y = (x + z*y) / y"
   490   by (simp add: add_divide_distrib)
   491 
   492 ML {*
   493 let open Conv
   494 in fconv_rule (arg_conv (arg1_conv (rewr_conv (mk_meta_eq @{thm mult_commute})))) (@{thm field_divide_inverse} RS sym)
   495 end
   496 *}
   497 
   498 ML{* 
   499 local
   500  val zr = @{cpat "0"}
   501  val zT = ctyp_of_term zr
   502  val geq = @{cpat "op ="}
   503  val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
   504  val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
   505  val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
   506  val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
   507 
   508  fun prove_nz ss T t =
   509     let
   510       val z = instantiate_cterm ([(zT,T)],[]) zr
   511       val eq = instantiate_cterm ([(eqT,T)],[]) geq
   512       val th = Simplifier.rewrite (ss addsimps @{thms simp_thms})
   513            (Thm.capply @{cterm "Trueprop"} (Thm.capply @{cterm "Not"}
   514                   (Thm.capply (Thm.capply eq t) z)))
   515     in equal_elim (symmetric th) TrueI
   516     end
   517 
   518  fun proc phi ss ct =
   519   let
   520     val ((x,y),(w,z)) =
   521          (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
   522     val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
   523     val T = ctyp_of_term x
   524     val [y_nz, z_nz] = map (prove_nz ss T) [y, z]
   525     val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
   526   in SOME (implies_elim (implies_elim th y_nz) z_nz)
   527   end
   528   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   529 
   530  fun proc2 phi ss ct =
   531   let
   532     val (l,r) = Thm.dest_binop ct
   533     val T = ctyp_of_term l
   534   in (case (term_of l, term_of r) of
   535       (Const(@{const_name Rings.divide},_)$_$_, _) =>
   536         let val (x,y) = Thm.dest_binop l val z = r
   537             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   538             val ynz = prove_nz ss T y
   539         in SOME (implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
   540         end
   541      | (_, Const (@{const_name Rings.divide},_)$_$_) =>
   542         let val (x,y) = Thm.dest_binop r val z = l
   543             val _ = map (HOLogic.dest_number o term_of) [x,y,z]
   544             val ynz = prove_nz ss T y
   545         in SOME (implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
   546         end
   547      | _ => NONE)
   548   end
   549   handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
   550 
   551  fun is_number (Const(@{const_name Rings.divide},_)$a$b) = is_number a andalso is_number b
   552    | is_number t = can HOLogic.dest_number t
   553 
   554  val is_number = is_number o term_of
   555 
   556  fun proc3 phi ss ct =
   557   (case term_of ct of
   558     Const(@{const_name Orderings.less},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
   559       let
   560         val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
   561         val _ = map is_number [a,b,c]
   562         val T = ctyp_of_term c
   563         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
   564       in SOME (mk_meta_eq th) end
   565   | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
   566       let
   567         val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
   568         val _ = map is_number [a,b,c]
   569         val T = ctyp_of_term c
   570         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
   571       in SOME (mk_meta_eq th) end
   572   | Const("op =",_)$(Const(@{const_name Rings.divide},_)$_$_)$_ =>
   573       let
   574         val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
   575         val _ = map is_number [a,b,c]
   576         val T = ctyp_of_term c
   577         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
   578       in SOME (mk_meta_eq th) end
   579   | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
   580     let
   581       val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
   582         val _ = map is_number [a,b,c]
   583         val T = ctyp_of_term c
   584         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
   585       in SOME (mk_meta_eq th) end
   586   | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
   587     let
   588       val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
   589         val _ = map is_number [a,b,c]
   590         val T = ctyp_of_term c
   591         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
   592       in SOME (mk_meta_eq th) end
   593   | Const("op =",_)$_$(Const(@{const_name Rings.divide},_)$_$_) =>
   594     let
   595       val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
   596         val _ = map is_number [a,b,c]
   597         val T = ctyp_of_term c
   598         val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
   599       in SOME (mk_meta_eq th) end
   600   | _ => NONE)
   601   handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE
   602 
   603 val add_frac_frac_simproc =
   604        make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + (?w::?'a::field)/?z"}],
   605                      name = "add_frac_frac_simproc",
   606                      proc = proc, identifier = []}
   607 
   608 val add_frac_num_simproc =
   609        make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + ?z"}, @{cpat "?z + (?x::?'a::field)/?y"}],
   610                      name = "add_frac_num_simproc",
   611                      proc = proc2, identifier = []}
   612 
   613 val ord_frac_simproc =
   614   make_simproc
   615     {lhss = [@{cpat "(?a::(?'a::{field, ord}))/?b < ?c"},
   616              @{cpat "(?a::(?'a::{field, ord}))/?b \<le> ?c"},
   617              @{cpat "?c < (?a::(?'a::{field, ord}))/?b"},
   618              @{cpat "?c \<le> (?a::(?'a::{field, ord}))/?b"},
   619              @{cpat "?c = ((?a::(?'a::{field, ord}))/?b)"},
   620              @{cpat "((?a::(?'a::{field, ord}))/ ?b) = ?c"}],
   621              name = "ord_frac_simproc", proc = proc3, identifier = []}
   622 
   623 local
   624 open Conv
   625 in
   626 
   627 val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
   628            @{thm "divide_Numeral1"},
   629            @{thm "divide_zero"}, @{thm "divide_Numeral0"},
   630            @{thm "divide_divide_eq_left"}, @{thm "mult_frac_frac"},
   631            @{thm "mult_num_frac"}, @{thm "mult_frac_num"},
   632            @{thm "mult_frac_frac"}, @{thm "times_divide_eq_right"},
   633            @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
   634            @{thm "diff_def"}, @{thm "minus_divide_left"},
   635            @{thm "Numeral1_eq1_nat"}, @{thm "add_divide_distrib"} RS sym,
   636            @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
   637            fconv_rule (arg_conv (arg1_conv (rewr_conv (mk_meta_eq @{thm mult_commute}))))   
   638            (@{thm field_divide_inverse} RS sym)]
   639 
   640 val comp_conv = (Simplifier.rewrite
   641 (HOL_basic_ss addsimps @{thms "Groebner_Basis.comp_arith"}
   642               addsimps ths addsimps @{thms simp_thms}
   643               addsimprocs Numeral_Simprocs.field_cancel_numeral_factors
   644                addsimprocs [add_frac_frac_simproc, add_frac_num_simproc,
   645                             ord_frac_simproc]
   646                 addcongs [@{thm "if_weak_cong"}]))
   647 then_conv (Simplifier.rewrite (HOL_basic_ss addsimps
   648   [@{thm numeral_1_eq_1},@{thm numeral_0_eq_0}] @ @{thms numerals(1-2)}))
   649 end
   650 
   651 fun numeral_is_const ct =
   652   case term_of ct of
   653    Const (@{const_name Rings.divide},_) $ a $ b =>
   654      can HOLogic.dest_number a andalso can HOLogic.dest_number b
   655  | Const (@{const_name Rings.inverse},_)$t => can HOLogic.dest_number t
   656  | t => can HOLogic.dest_number t
   657 
   658 fun dest_const ct = ((case term_of ct of
   659    Const (@{const_name Rings.divide},_) $ a $ b=>
   660     Rat.rat_of_quotient (snd (HOLogic.dest_number a), snd (HOLogic.dest_number b))
   661  | Const (@{const_name Rings.inverse},_)$t => 
   662                Rat.inv (Rat.rat_of_int (snd (HOLogic.dest_number t)))
   663  | t => Rat.rat_of_int (snd (HOLogic.dest_number t))) 
   664    handle TERM _ => error "ring_dest_const")
   665 
   666 fun mk_const phi cT x =
   667  let val (a, b) = Rat.quotient_of_rat x
   668  in if b = 1 then Numeral.mk_cnumber cT a
   669     else Thm.capply
   670          (Thm.capply (Drule.cterm_rule (instantiate' [SOME cT] []) @{cpat "op /"})
   671                      (Numeral.mk_cnumber cT a))
   672          (Numeral.mk_cnumber cT b)
   673   end
   674 
   675 in
   676  val field_comp_conv = comp_conv;
   677  val fieldgb_declaration = 
   678   NormalizerData.funs @{thm class_fieldgb.fieldgb_axioms'}
   679    {is_const = K numeral_is_const,
   680     dest_const = K dest_const,
   681     mk_const = mk_const,
   682     conv = K (K comp_conv)}
   683 end;
   684 *}
   685 
   686 declaration fieldgb_declaration
   687 
   688 end