src/HOL/Groebner_Basis.thy
changeset 23573 d85a277f90fd
parent 23477 f4b83f03cac9
child 25250 b3a485b98963
equal deleted inserted replaced
23572:b3ce27bd211f 23573:d85a277f90fd
   183   iszero_number_of_Pls iszero_0 not_iszero_Numeral1
   183   iszero_number_of_Pls iszero_0 not_iszero_Numeral1
   184 
   184 
   185 lemmas semiring_norm = comp_arith
   185 lemmas semiring_norm = comp_arith
   186 
   186 
   187 ML {*
   187 ML {*
   188   fun numeral_is_const ct =
   188 local
   189     can HOLogic.dest_number (Thm.term_of ct);
   189 
   190 
   190 open Conv;
   191   val numeral_conv =
   191 
   192     Conv.then_conv (Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm}),
   192 fun numeral_is_const ct =
   193    Simplifier.rewrite (HOL_basic_ss addsimps
   193   can HOLogic.dest_number (Thm.term_of ct);
   194   [@{thm numeral_1_eq_1},@{thm numeral_0_eq_0}] @ @{thms numerals(1-2)}));
   194 
   195 *}
   195 fun int_of_rat x =
   196 
   196   (case Rat.quotient_of_rat x of (i, 1) => i
   197 ML {*
   197   | _ => error "int_of_rat: bad int");
   198   fun int_of_rat x =
   198 
   199     (case Rat.quotient_of_rat x of (i, 1) => i
   199 val numeral_conv =
   200     | _ => error "int_of_rat: bad int")
   200   Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm}) then_conv
   201 *}
   201   Simplifier.rewrite (HOL_basic_ss addsimps
   202 
   202     (@{thms numeral_1_eq_1} @ @{thms numeral_0_eq_0} @ @{thms numerals(1-2)}));
   203 declaration {*
   203 
   204   NormalizerData.funs @{thm class_semiring.axioms}
   204 in
       
   205 
       
   206 fun normalizer_funs key =
       
   207   NormalizerData.funs key
   205    {is_const = fn phi => numeral_is_const,
   208    {is_const = fn phi => numeral_is_const,
   206     dest_const = fn phi => fn ct =>
   209     dest_const = fn phi => fn ct =>
   207       Rat.rat_of_int (snd
   210       Rat.rat_of_int (snd
   208         (HOLogic.dest_number (Thm.term_of ct)
   211         (HOLogic.dest_number (Thm.term_of ct)
   209           handle TERM _ => error "ring_dest_const")),
   212           handle TERM _ => error "ring_dest_const")),
   210     mk_const = fn phi => fn cT => fn x =>
   213     mk_const = fn phi => fn cT => fn x => Numeral.mk_cnumber cT (int_of_rat x),
   211       Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
       
   212     conv = fn phi => K numeral_conv}
   214     conv = fn phi => K numeral_conv}
       
   215 
       
   216 end
   213 *}
   217 *}
       
   218 
       
   219 declaration {* normalizer_funs @{thm class_semiring.axioms} *}
   214 
   220 
   215 
   221 
   216 locale gb_ring = gb_semiring +
   222 locale gb_ring = gb_semiring +
   217   fixes sub :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   223   fixes sub :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   218     and neg :: "'a \<Rightarrow> 'a"
   224     and neg :: "'a \<Rightarrow> 'a"
   239 interpretation class_ring: gb_ring ["op +" "op *" "op ^"
   245 interpretation class_ring: gb_ring ["op +" "op *" "op ^"
   240     "0::'a::{comm_semiring_1,recpower,number_ring}" 1 "op -" "uminus"]
   246     "0::'a::{comm_semiring_1,recpower,number_ring}" 1 "op -" "uminus"]
   241   by unfold_locales simp_all
   247   by unfold_locales simp_all
   242 
   248 
   243 
   249 
   244 declaration {*
   250 declaration {* normalizer_funs @{thm class_ring.axioms} *}
   245   NormalizerData.funs @{thm class_ring.axioms}
       
   246    {is_const = fn phi => numeral_is_const,
       
   247     dest_const = fn phi => fn ct =>
       
   248       Rat.rat_of_int (snd
       
   249         (HOLogic.dest_number (Thm.term_of ct)
       
   250           handle TERM _ => error "ring_dest_const")),
       
   251     mk_const = fn phi => fn cT => fn x =>
       
   252       Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
       
   253     conv = fn phi => K numeral_conv}
       
   254 *}
       
   255 
   251 
   256 use "Tools/Groebner_Basis/normalizer.ML"
   252 use "Tools/Groebner_Basis/normalizer.ML"
   257 
   253 
   258 method_setup sring_norm = {*
   254 method_setup sring_norm = {*
   259   Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' (Normalizer.semiring_normalize_tac ctxt))
   255   Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' (Normalizer.semiring_normalize_tac ctxt))
   352   have "w - x = 0" by blast
   348   have "w - x = 0" by blast
   353   thus "w = x"  by simp
   349   thus "w = x"  by simp
   354 qed
   350 qed
   355 
   351 
   356 
   352 
   357 declaration {*
   353 declaration {* normalizer_funs @{thm class_ringb.axioms} *}
   358   NormalizerData.funs @{thm class_ringb.axioms}
       
   359    {is_const = fn phi => numeral_is_const,
       
   360     dest_const = fn phi => fn ct =>
       
   361       Rat.rat_of_int (snd
       
   362         (HOLogic.dest_number (Thm.term_of ct)
       
   363           handle TERM _ => error "ring_dest_const")),
       
   364     mk_const = fn phi => fn cT => fn x =>
       
   365       Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
       
   366     conv = fn phi => K numeral_conv}
       
   367 *}
       
   368 
   354 
   369 interpretation natgb: semiringb
   355 interpretation natgb: semiringb
   370   ["op +" "op *" "op ^" "0::nat" "1"]
   356   ["op +" "op *" "op ^" "0::nat" "1"]
   371 proof (unfold_locales, simp add: ring_simps power_Suc)
   357 proof (unfold_locales, simp add: ring_simps power_Suc)
   372   fix w x y z ::"nat"
   358   fix w x y z ::"nat"
   386       hence "w = x" using kp by (simp add: mult_cancel2)}
   372       hence "w = x" using kp by (simp add: mult_cancel2)}
   387     ultimately have "w=x" by blast }
   373     ultimately have "w=x" by blast }
   388   thus "(w * y + x * z = w * z + x * y) = (w = x \<or> y = z)" by auto
   374   thus "(w * y + x * z = w * z + x * y) = (w = x \<or> y = z)" by auto
   389 qed
   375 qed
   390 
   376 
   391 declaration {*
   377 declaration {* normalizer_funs @{thm natgb.axioms} *}
   392   NormalizerData.funs @{thm natgb.axioms}
       
   393    {is_const = fn phi => numeral_is_const,
       
   394     dest_const = fn phi => fn ct =>
       
   395       Rat.rat_of_int (snd
       
   396         (HOLogic.dest_number (Thm.term_of ct)
       
   397           handle TERM _ => error "ring_dest_const")),
       
   398     mk_const = fn phi => fn cT => fn x =>
       
   399       Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
       
   400     conv = fn phi => K numeral_conv}
       
   401 *}
       
   402 
   378 
   403 locale fieldgb = ringb + gb_field
   379 locale fieldgb = ringb + gb_field
   404 begin
   380 begin
   405 
   381 
   406 declare "axioms" [normalizer del]
   382 declare "axioms" [normalizer del]
   446 in
   422 in
   447 fn src => Method.syntax 
   423 fn src => Method.syntax 
   448     ((Scan.optional (keyword addN |-- thms) []) -- 
   424     ((Scan.optional (keyword addN |-- thms) []) -- 
   449     (Scan.optional (keyword delN |-- thms) [])) src 
   425     (Scan.optional (keyword delN |-- thms) [])) src 
   450  #> (fn ((add_ths, del_ths), ctxt) => 
   426  #> (fn ((add_ths, del_ths), ctxt) => 
   451        Method.SIMPLE_METHOD' (Groebner.algebra_tac add_ths del_ths ctxt))
   427        Method.SIMPLE_METHOD' (Groebner.ring_tac add_ths del_ths ctxt))
   452 end
   428 end
   453 *} "solve polynomial equations over (semi)rings using Groebner bases"
   429 *} "solve polynomial equations over (semi)rings using Groebner bases"
   454 
   430 
   455 end
   431 end