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