src/HOL/Groebner_Basis.thy
 changeset 23252 67268bb40b21 child 23258 9062e98fdab1
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Groebner_Basis.thy	Tue Jun 05 16:26:04 2007 +0200
1.3 @@ -0,0 +1,416 @@
1.4 +(*  Title:      HOL/Groebner_Basis.thy
1.5 +    ID:         \$Id\$
1.6 +    Author:     Amine Chaieb, TU Muenchen
1.7 +*)
1.8 +
1.9 +header {* Semiring normalization and Groebner Bases *}
1.10 +
1.11 +theory Groebner_Basis
1.12 +imports NatBin
1.13 +uses
1.14 +  "Tools/Groebner_Basis/misc.ML"
1.15 +  "Tools/Groebner_Basis/normalizer_data.ML"
1.16 +  ("Tools/Groebner_Basis/normalizer.ML")
1.17 +begin
1.18 +
1.19 +subsection {* Semiring normalization *}
1.20 +
1.21 +setup NormalizerData.setup
1.22 +
1.23 +
1.24 +locale semiring =
1.25 +  fixes add mul pwr r0 r1
1.28 +    and mul_a:"mul x (mul y z) = mul (mul x y) z" and mul_c:"mul x y = mul y x"
1.29 +    and mul_1:"mul r1 x = x" and  mul_0:"mul r0 x = r0"
1.30 +    and mul_d:"mul x (add y z) = add (mul x y) (mul x z)"
1.31 +    and pwr_0:"pwr x 0 = r1" and pwr_Suc:"pwr x (Suc n) = mul x (pwr x n)"
1.32 +begin
1.33 +
1.34 +lemma mul_pwr:"mul (pwr x p) (pwr x q) = pwr x (p + q)"
1.35 +proof (induct p)
1.36 +  case 0
1.37 +  then show ?case by (auto simp add: pwr_0 mul_1)
1.38 +next
1.39 +  case Suc
1.40 +  from this [symmetric] show ?case
1.41 +    by (auto simp add: pwr_Suc mul_1 mul_a)
1.42 +qed
1.43 +
1.44 +lemma pwr_mul: "pwr (mul x y) q = mul (pwr x q) (pwr y q)"
1.45 +proof (induct q arbitrary: x y, auto simp add:pwr_0 pwr_Suc mul_1)
1.46 +  fix q x y
1.47 +  assume "\<And>x y. pwr (mul x y) q = mul (pwr x q) (pwr y q)"
1.48 +  have "mul (mul x y) (mul (pwr x q) (pwr y q)) = mul x (mul y (mul (pwr x q) (pwr y q)))"
1.49 +    by (simp add: mul_a)
1.50 +  also have "\<dots> = (mul (mul y (mul (pwr y q) (pwr x q))) x)" by (simp add: mul_c)
1.51 +  also have "\<dots> = (mul (mul y (pwr y q)) (mul (pwr x q) x))" by (simp add: mul_a)
1.52 +  finally show "mul (mul x y) (mul (pwr x q) (pwr y q)) =
1.53 +    mul (mul x (pwr x q)) (mul y (pwr y q))" by (simp add: mul_c)
1.54 +qed
1.55 +
1.56 +lemma pwr_pwr: "pwr (pwr x p) q = pwr x (p * q)"
1.57 +proof (induct p arbitrary: q)
1.58 +  case 0
1.59 +  show ?case using pwr_Suc mul_1 pwr_0 by (induct q) auto
1.60 +next
1.61 +  case Suc
1.62 +  thus ?case by (auto simp add: mul_pwr [symmetric] pwr_mul pwr_Suc)
1.63 +qed
1.64 +
1.65 +
1.66 +subsubsection {* Declaring the abstract theory *}
1.67 +
1.68 +lemma semiring_ops:
1.69 +  includes meta_term_syntax
1.70 +  shows "TERM (add x y)" and "TERM (mul x y)" and "TERM (pwr x n)"
1.71 +    and "TERM r0" and "TERM r1"
1.72 +  by rule+
1.73 +
1.74 +lemma semiring_rules:
1.75 +  "add (mul a m) (mul b m) = mul (add a b) m"
1.76 +  "add (mul a m) m = mul (add a r1) m"
1.77 +  "add m (mul a m) = mul (add a r1) m"
1.79 +  "add r0 a = a"
1.80 +  "add a r0 = a"
1.81 +  "mul a b = mul b a"
1.82 +  "mul (add a b) c = add (mul a c) (mul b c)"
1.83 +  "mul r0 a = r0"
1.84 +  "mul a r0 = r0"
1.85 +  "mul r1 a = a"
1.86 +  "mul a r1 = a"
1.87 +  "mul (mul lx ly) (mul rx ry) = mul (mul lx rx) (mul ly ry)"
1.88 +  "mul (mul lx ly) (mul rx ry) = mul lx (mul ly (mul rx ry))"
1.89 +  "mul (mul lx ly) (mul rx ry) = mul rx (mul (mul lx ly) ry)"
1.90 +  "mul (mul lx ly) rx = mul (mul lx rx) ly"
1.91 +  "mul (mul lx ly) rx = mul lx (mul ly rx)"
1.92 +  "mul lx (mul rx ry) = mul (mul lx rx) ry"
1.93 +  "mul lx (mul rx ry) = mul rx (mul lx ry)"
1.100 +  "mul (pwr x p) (pwr x q) = pwr x (p + q)"
1.101 +  "mul x (pwr x q) = pwr x (Suc q)"
1.102 +  "mul (pwr x q) x = pwr x (Suc q)"
1.103 +  "mul x x = pwr x 2"
1.104 +  "pwr (mul x y) q = mul (pwr x q) (pwr y q)"
1.105 +  "pwr (pwr x p) q = pwr x (p * q)"
1.106 +  "pwr x 0 = r1"
1.107 +  "pwr x 1 = x"
1.108 +  "mul x (add y z) = add (mul x y) (mul x z)"
1.109 +  "pwr x (Suc q) = mul x (pwr x q)"
1.110 +  "pwr x (2*n) = mul (pwr x n) (pwr x n)"
1.111 +  "pwr x (Suc (2*n)) = mul x (mul (pwr x n) (pwr x n))"
1.112 +proof -
1.113 +  show "add (mul a m) (mul b m) = mul (add a b) m" using mul_d mul_c by simp
1.114 +next show"add (mul a m) m = mul (add a r1) m" using mul_d mul_c mul_1 by simp
1.115 +next show "add m (mul a m) = mul (add a r1) m" using mul_c mul_d mul_1 add_c by simp
1.116 +next show "add m m = mul (add r1 r1) m" using mul_c mul_d mul_1 by simp
1.117 +next show "add r0 a = a" using add_0 by simp
1.119 +next show "mul a b = mul b a" using mul_c by simp
1.120 +next show "mul (add a b) c = add (mul a c) (mul b c)" using mul_c mul_d by simp
1.121 +next show "mul r0 a = r0" using mul_0 by simp
1.122 +next show "mul a r0 = r0" using mul_0 mul_c by simp
1.123 +next show "mul r1 a = a" using mul_1 by simp
1.124 +next show "mul a r1 = a" using mul_1 mul_c by simp
1.125 +next show "mul (mul lx ly) (mul rx ry) = mul (mul lx rx) (mul ly ry)"
1.126 +    using mul_c mul_a by simp
1.127 +next show "mul (mul lx ly) (mul rx ry) = mul lx (mul ly (mul rx ry))"
1.128 +    using mul_a by simp
1.129 +next
1.130 +  have "mul (mul lx ly) (mul rx ry) = mul (mul rx ry) (mul lx ly)" by (rule mul_c)
1.131 +  also have "\<dots> = mul rx (mul ry (mul lx ly))" using mul_a by simp
1.132 +  finally
1.133 +  show "mul (mul lx ly) (mul rx ry) = mul rx (mul (mul lx ly) ry)"
1.134 +    using mul_c by simp
1.135 +next show "mul (mul lx ly) rx = mul (mul lx rx) ly" using mul_c mul_a by simp
1.136 +next
1.137 +  show "mul (mul lx ly) rx = mul lx (mul ly rx)" by (simp add: mul_a)
1.138 +next show "mul lx (mul rx ry) = mul (mul lx rx) ry" by (simp add: mul_a )
1.139 +next show "mul lx (mul rx ry) = mul rx (mul lx ry)" by (simp add: mul_a,simp add: mul_c)
1.148 +next show "mul (pwr x p) (pwr x q) = pwr x (p + q)" by (rule mul_pwr)
1.149 +next show "mul x (pwr x q) = pwr x (Suc q)" using pwr_Suc by simp
1.150 +next show "mul (pwr x q) x = pwr x (Suc q)" using pwr_Suc mul_c by simp
1.151 +next show "mul x x = pwr x 2" by (simp add: nat_number pwr_Suc pwr_0 mul_1 mul_c)
1.152 +next show "pwr (mul x y) q = mul (pwr x q) (pwr y q)" by (rule pwr_mul)
1.153 +next show "pwr (pwr x p) q = pwr x (p * q)" by (rule pwr_pwr)
1.154 +next show "pwr x 0 = r1" using pwr_0 .
1.155 +next show "pwr x 1 = x" by (simp add: nat_number pwr_Suc pwr_0 mul_1 mul_c)
1.156 +next show "mul x (add y z) = add (mul x y) (mul x z)" using mul_d by simp
1.157 +next show "pwr x (Suc q) = mul x (pwr x q)" using pwr_Suc by simp
1.158 +next show "pwr x (2 * n) = mul (pwr x n) (pwr x n)" by (simp add: nat_number mul_pwr)
1.159 +next show "pwr x (Suc (2 * n)) = mul x (mul (pwr x n) (pwr x n))"
1.160 +    by (simp add: nat_number pwr_Suc mul_pwr)
1.161 +qed
1.162 +
1.163 +
1.164 +lemma "axioms" [normalizer
1.165 +    semiring ops: semiring_ops
1.166 +    semiring rules: semiring_rules]:
1.167 +  "semiring add mul pwr r0 r1" .
1.168 +
1.169 +end
1.170 +
1.171 +interpretation class_semiring: semiring
1.172 +    ["op +" "op *" "op ^" "0::'a::{comm_semiring_1, recpower}" "1"]
1.173 +  by unfold_locales (auto simp add: ring_eq_simps power_Suc)
1.174 +
1.175 +lemmas nat_arith =
1.176 +  add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
1.177 +
1.178 +lemma not_iszero_Numeral1: "\<not> iszero (Numeral1::'a::number_ring)"
1.179 +  by (simp add: numeral_1_eq_1)
1.180 +lemmas comp_arith = Let_def arith_simps nat_arith rel_simps if_False
1.183 +  numeral_0_eq_0[symmetric] numerals[symmetric] not_iszero_1
1.184 +  iszero_number_of_1 iszero_number_of_0 nonzero_number_of_Min
1.185 +  iszero_number_of_Pls iszero_0 not_iszero_Numeral1
1.186 +
1.187 +lemmas semiring_norm = comp_arith
1.188 +
1.189 +ML {*
1.190 +  fun numeral_is_const ct =
1.191 +    can HOLogic.dest_number (Thm.term_of ct);
1.192 +
1.193 +  val numeral_conv =
1.194 +    Conv.then_conv (Simplifier.rewrite (HOL_basic_ss addsimps @{thms semiring_norm}),
1.196 +  [@{thm numeral_1_eq_1},@{thm numeral_0_eq_0}] @ @{thms numerals(1-2)}));
1.197 +*}
1.198 +
1.199 +ML {*
1.200 +  fun int_of_rat x =
1.201 +    (case Rat.quotient_of_rat x of (i, 1) => i
1.202 +    | _ => error "int_of_rat: bad int")
1.203 +*}
1.204 +
1.205 +declaration {*
1.206 +  NormalizerData.funs @{thm class_semiring.axioms}
1.207 +   {is_const = fn phi => numeral_is_const,
1.208 +    dest_const = fn phi => fn ct =>
1.209 +      Rat.rat_of_int (snd
1.210 +        (HOLogic.dest_number (Thm.term_of ct)
1.211 +          handle TERM _ => error "ring_dest_const")),
1.212 +    mk_const = fn phi => fn cT => fn x =>
1.213 +      Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
1.214 +    conv = fn phi => numeral_conv}
1.215 +*}
1.216 +
1.217 +
1.218 +locale ring = semiring +
1.219 +  fixes sub :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
1.220 +    and neg :: "'a \<Rightarrow> 'a"
1.221 +  assumes neg_mul: "neg x = mul (neg r1) x"
1.222 +    and sub_add: "sub x y = add x (neg y)"
1.223 +begin
1.224 +
1.225 +lemma ring_ops:
1.226 +  includes meta_term_syntax
1.227 +  shows "TERM (sub x y)" and "TERM (neg x)" .
1.228 +
1.229 +lemmas ring_rules = neg_mul sub_add
1.230 +
1.231 +lemma "axioms" [normalizer
1.232 +  semiring ops: semiring_ops
1.233 +  semiring rules: semiring_rules
1.234 +  ring ops: ring_ops
1.235 +  ring rules: ring_rules]:
1.236 +  "ring add mul pwr r0 r1 sub neg" .
1.237 +
1.238 +end
1.239 +
1.240 +
1.241 +interpretation class_ring: ring ["op +" "op *" "op ^"
1.242 +    "0::'a::{comm_semiring_1,recpower,number_ring}" 1 "op -" "uminus"]
1.243 +  by unfold_locales simp_all
1.244 +
1.245 +
1.246 +declaration {*
1.247 +  NormalizerData.funs @{thm class_ring.axioms}
1.248 +   {is_const = fn phi => numeral_is_const,
1.249 +    dest_const = fn phi => fn ct =>
1.250 +      Rat.rat_of_int (snd
1.251 +        (HOLogic.dest_number (Thm.term_of ct)
1.252 +          handle TERM _ => error "ring_dest_const")),
1.253 +    mk_const = fn phi => fn cT => fn x =>
1.254 +      Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
1.255 +    conv = fn phi => numeral_conv}
1.256 +*}
1.257 +
1.258 +use "Tools/Groebner_Basis/normalizer.ML"
1.259 +
1.260 +method_setup sring_norm = {*
1.261 +  Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' (Normalizer.semiring_normalize_tac ctxt))
1.262 +*} "Semiring_normalizer"
1.263 +
1.264 +
1.265 +subsection {* Gr�bner Bases *}
1.266 +
1.267 +locale semiringb = semiring +
1.269 +  and add_mul_solve: "add (mul w y) (mul x z) =
1.270 +    add (mul w z) (mul x y) \<longleftrightarrow> w = x \<or> y = z"
1.271 +begin
1.272 +
1.273 +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)"
1.274 +proof-
1.275 +  have "a \<noteq> b \<and> c \<noteq> d \<longleftrightarrow> \<not> (a = b \<or> c = d)" by simp
1.276 +  also have "\<dots> \<longleftrightarrow> add (mul a c) (mul b d) \<noteq> add (mul a d) (mul b c)"
1.277 +    using add_mul_solve by blast
1.278 +  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)"
1.279 +    by simp
1.280 +qed
1.281 +
1.282 +lemma add_scale_eq_noteq: "\<lbrakk>r \<noteq> r0 ; (a = b) \<and> ~(c = d)\<rbrakk>
1.283 +  \<Longrightarrow> add a (mul r c) \<noteq> add b (mul r d)"
1.284 +proof(clarify)
1.285 +  assume nz: "r\<noteq> r0" and cnd: "c\<noteq>d"
1.286 +    and eq: "add b (mul r c) = add b (mul r d)"
1.287 +  hence "mul r c = mul r d" using cnd add_cancel by simp
1.288 +  hence "add (mul r0 d) (mul r c) = add (mul r0 c) (mul r d)"
1.289 +    using mul_0 add_cancel by simp
1.290 +  thus "False" using add_mul_solve nz cnd by simp
1.291 +qed
1.292 +
1.293 +declare "axioms" [normalizer del]
1.294 +
1.295 +lemma "axioms" [normalizer
1.296 +  semiring ops: semiring_ops
1.297 +  semiring rules: semiring_rules
1.298 +  idom rules: noteq_reduce add_scale_eq_noteq]:
1.299 +  "semiringb add mul pwr r0 r1" .
1.300 +
1.301 +end
1.302 +
1.303 +locale ringb = semiringb + ring
1.304 +begin
1.305 +
1.306 +declare "axioms" [normalizer del]
1.307 +
1.308 +lemma "axioms" [normalizer
1.309 +  semiring ops: semiring_ops
1.310 +  semiring rules: semiring_rules
1.311 +  ring ops: ring_ops
1.312 +  ring rules: ring_rules
1.313 +  idom rules: noteq_reduce add_scale_eq_noteq]:
1.314 +  "ringb add mul pwr r0 r1 sub neg" .
1.315 +
1.316 +end
1.317 +
1.318 +lemma no_zero_divirors_neq0:
1.319 +  assumes az: "(a::'a::no_zero_divisors) \<noteq> 0"
1.320 +    and ab: "a*b = 0" shows "b = 0"
1.321 +proof -
1.322 +  { assume bz: "b \<noteq> 0"
1.323 +    from no_zero_divisors [OF az bz] ab have False by blast }
1.324 +  thus "b = 0" by blast
1.325 +qed
1.326 +
1.327 +interpretation class_ringb: ringb
1.328 +  ["op +" "op *" "op ^" "0::'a::{idom,recpower,number_ring}" "1" "op -" "uminus"]
1.329 +proof(unfold_locales, simp add: ring_eq_simps power_Suc, auto)
1.330 +  fix w x y z ::"'a::{idom,recpower,number_ring}"
1.331 +  assume p: "w * y + x * z = w * z + x * y" and ynz: "y \<noteq> z"
1.332 +  hence ynz': "y - z \<noteq> 0" by simp
1.333 +  from p have "w * y + x* z - w*z - x*y = 0" by simp
1.334 +  hence "w* (y - z) - x * (y - z) = 0" by (simp add: ring_eq_simps)
1.335 +  hence "(y - z) * (w - x) = 0" by (simp add: ring_eq_simps)
1.336 +  with  no_zero_divirors_neq0 [OF ynz']
1.337 +  have "w - x = 0" by blast
1.338 +  thus "w = x"  by simp
1.339 +qed
1.340 +
1.341 +
1.342 +declaration {*
1.343 +  NormalizerData.funs @{thm class_ringb.axioms}
1.344 +   {is_const = fn phi => numeral_is_const,
1.345 +    dest_const = fn phi => fn ct =>
1.346 +      Rat.rat_of_int (snd
1.347 +        (HOLogic.dest_number (Thm.term_of ct)
1.348 +          handle TERM _ => error "ring_dest_const")),
1.349 +    mk_const = fn phi => fn cT => fn x =>
1.350 +      Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
1.351 +    conv = fn phi => numeral_conv}
1.352 +*}
1.353 +
1.354 +
1.355 +interpretation natgb: semiringb
1.356 +  ["op +" "op *" "op ^" "0::nat" "1"]
1.357 +proof (unfold_locales, simp add: ring_eq_simps power_Suc)
1.358 +  fix w x y z ::"nat"
1.359 +  { assume p: "w * y + x * z = w * z + x * y" and ynz: "y \<noteq> z"
1.360 +    hence "y < z \<or> y > z" by arith
1.361 +    moreover {
1.362 +      assume lt:"y <z" hence "\<exists>k. z = y + k \<and> k > 0" by (rule_tac x="z - y" in exI, auto)
1.363 +      then obtain k where kp: "k>0" and yz:"z = y + k" by blast
1.364 +      from p have "(w * y + x *y) + x*k = (w * y + x*y) + w*k" by (simp add: yz ring_eq_simps)
1.365 +      hence "x*k = w*k" by simp
1.366 +      hence "w = x" using kp by (simp add: mult_cancel2) }
1.367 +    moreover {
1.368 +      assume lt: "y >z" hence "\<exists>k. y = z + k \<and> k>0" by (rule_tac x="y - z" in exI, auto)
1.369 +      then obtain k where kp: "k>0" and yz:"y = z + k" by blast
1.370 +      from p have "(w * z + x *z) + w*k = (w * z + x*z) + x*k" by (simp add: yz ring_eq_simps)
1.371 +      hence "w*k = x*k" by simp
1.372 +      hence "w = x" using kp by (simp add: mult_cancel2)}
1.373 +    ultimately have "w=x" by blast }
1.374 +  thus "(w * y + x * z = w * z + x * y) = (w = x \<or> y = z)" by auto
1.375 +qed
1.376 +
1.377 +declaration {*
1.378 +  NormalizerData.funs @{thm natgb.axioms}
1.379 +   {is_const = fn phi => numeral_is_const,
1.380 +    dest_const = fn phi => fn ct =>
1.381 +      Rat.rat_of_int (snd
1.382 +        (HOLogic.dest_number (Thm.term_of ct)
1.383 +          handle TERM _ => error "ring_dest_const")),
1.384 +    mk_const = fn phi => fn cT => fn x =>
1.385 +      Thm.cterm_of (Thm.theory_of_ctyp cT) (HOLogic.mk_number (typ_of cT) (int_of_rat x)),
1.386 +    conv = fn phi => numeral_conv}
1.387 +*}
1.388 +
1.389 +
1.390 +lemmas bool_simps =  simp_thms(1-34)
1.391 +lemma dnf:
1.392 +    "(P & (Q | R)) = ((P&Q) | (P&R))" "((Q | R) & P) = ((Q&P) | (R&P))"
1.393 +    "(P \<and> Q) = (Q \<and> P)" "(P \<or> Q) = (Q \<or> P)"
1.394 +  by blast+
1.395 +
1.396 +lemmas weak_dnf_simps = dnf bool_simps
1.397 +
1.398 +lemma nnf_simps:
1.399 +    "(\<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)"
1.400 +    "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not> \<not>(P)) = P"
1.401 +  by blast+
1.402 +
1.403 +lemma PFalse:
1.404 +    "P \<equiv> False \<Longrightarrow> \<not> P"
1.405 +    "\<not> P \<Longrightarrow> (P \<equiv> False)"
1.406 +  by auto
1.407 +
1.408 +use "Tools/Groebner_Basis/groebner.ML"
1.409 +
1.410 +ML {*
1.411 +  fun algebra_tac ctxt i = ObjectLogic.full_atomize_tac i THEN (fn st =>
1.412 +  rtac (Groebner.ring_conv ctxt (Thm.dest_arg (nth (cprems_of st) (i - 1)))) i st);
1.413 +*}
1.414 +
1.415 +method_setup algebra = {*
1.416 +  Method.ctxt_args (Method.SIMPLE_METHOD' o algebra_tac)
1.417 +*} ""
1.418 +
1.419 +end
```