author wenzelm Tue Sep 20 13:56:01 2005 +0200 (2005-09-20) changeset 17504 cc10c4b64b8e parent 17503 b053d5a89b6f child 17505 928bd7053d6a
tuned proofs;
 src/HOL/Commutative_Ring.thy file | annotate | diff | revisions
```     1.1 --- a/src/HOL/Commutative_Ring.thy	Tue Sep 20 13:33:27 2005 +0200
1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,310 +0,0 @@
1.4 -(*  ID:         \$Id\$
1.5 -    Author:     Bernhard Haeupler
1.6 -
1.7 -Proving equalities in commutative rings done "right" in Isabelle/HOL.
1.8 -*)
1.9 -
1.10 -header {* Proving equalities in commutative rings *}
1.11 -
1.12 -theory Commutative_Ring
1.13 -imports List
1.14 -uses ("Tools/comm_ring.ML")
1.15 -begin
1.16 -
1.17 -  (* Syntax of multivariate polynomials (pol) and polynomial expressions*)
1.18 -datatype 'a pol =
1.19 -  Pc 'a
1.20 -  | Pinj nat "'a pol"
1.21 -  | PX "'a pol" nat "'a pol"
1.22 -
1.23 -datatype 'a polex =
1.24 -  Pol "'a pol"
1.25 -  | Add "'a polex" "'a polex"
1.26 -  | Sub "'a polex" "'a polex"
1.27 -  | Mul "'a polex" "'a polex"
1.28 -  | Pow "'a polex" nat
1.29 -  | Neg "'a polex"
1.30 -
1.31 -  (* Interpretation functions for the shadow syntax *)
1.32 -consts
1.33 -  Ipol :: "('a::{comm_ring,recpower}) list \<Rightarrow> 'a pol \<Rightarrow> 'a"
1.34 -  Ipolex :: "('a::{comm_ring,recpower}) list \<Rightarrow> 'a polex \<Rightarrow> 'a"
1.35 -
1.36 -primrec
1.37 -  "Ipol l (Pc c) = c"
1.38 -  "Ipol l (Pinj i P) = Ipol (drop i l) P"
1.39 -  "Ipol l (PX P x Q) = (Ipol l P)*((hd l)^x) + (Ipol (drop 1 l) Q)"
1.40 -
1.41 -primrec
1.42 -  "Ipolex l (Pol P) = Ipol l P"
1.43 -  "Ipolex l (Add P Q) = (Ipolex l P) + (Ipolex l Q)"
1.44 -  "Ipolex l (Sub P Q) = (Ipolex l P) - (Ipolex l Q)"
1.45 -  "Ipolex l (Mul P Q) = (Ipolex l P) * (Ipolex l Q)"
1.46 -  "Ipolex l (Pow p n) = (Ipolex l p) ^ n"
1.47 -  "Ipolex l (Neg P) = -(Ipolex l P)"
1.48 -
1.49 -  (* Create polynomial normalized polynomials given normalized inputs *)
1.50 -constdefs mkPinj :: "nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol"
1.51 -  mkPinj_def: "mkPinj x P \<equiv> (case P of
1.52 -  (Pc c) \<Rightarrow> (Pc c) |
1.53 -  (Pinj y P) \<Rightarrow> Pinj (x+y) P |
1.54 -  (PX p1 y p2) \<Rightarrow> Pinj x P )"
1.55 -
1.56 -constdefs mkPX :: "('a::{comm_ring,recpower}) pol \<Rightarrow> nat \<Rightarrow> 'a pol \<Rightarrow> 'a pol"
1.57 -  mkPX_def: "mkPX P i Q == (case P of
1.58 -  (Pc c) \<Rightarrow> (if (c = 0) then (mkPinj 1 Q) else (PX P i Q)) |
1.59 -  (Pinj j R) \<Rightarrow> (PX P i Q) |
1.60 -  (PX P2 i2 Q2) \<Rightarrow> (if (Q2 = (Pc 0)) then (PX P2 (i+i2) Q) else (PX P i Q)) )"
1.61 -
1.62 -  (* Defining the basic ring operations on normalized polynomials *)
1.63 -consts
1.64 -add :: "(('a::{comm_ring,recpower}) pol) \<times> ('a pol) \<Rightarrow> 'a pol"
1.65 -mul :: "(('a::{comm_ring,recpower}) pol) \<times> ('a pol) \<Rightarrow> 'a pol"
1.66 -neg :: "('a::{comm_ring,recpower}) pol \<Rightarrow> 'a pol"
1.67 -sqr :: "('a::{comm_ring,recpower}) pol  \<Rightarrow> 'a pol"
1.68 -pow :: "('a::{comm_ring,recpower}) pol \<times> nat \<Rightarrow> 'a pol"
1.69 -
1.70 -
1.72 -recdef add "measure (\<lambda>(x, y). size x + size y)"
1.73 -  "add (Pc a, Pc b) = Pc (a+b)"
1.74 -  "add (Pc c, Pinj i P) = Pinj i (add (P, Pc c))"
1.75 -  "add (Pinj i P, Pc c) = Pinj i (add (P, Pc c))"
1.76 -  "add (Pc c, PX P i Q) = PX P i (add (Q, Pc c))"
1.77 -  "add (PX P i Q, Pc c) = PX P i (add (Q, Pc c))"
1.78 -  "add (Pinj x P, Pinj y Q) =
1.79 -  (if x=y then mkPinj x (add (P, Q))
1.80 -   else (if x>y then mkPinj y (add (Pinj (x-y) P, Q))
1.81 -         else mkPinj x (add (Pinj (y-x) Q, P)) ))"
1.82 -  "add (Pinj x P, PX Q y R) =
1.83 -  (if x=0 then add(P, PX Q y R)
1.84 -   else (if x=1 then PX Q y (add (R, P))
1.85 -         else PX Q y (add (R, Pinj (x - 1) P))))"
1.86 -  "add (PX P x R, Pinj y Q) =
1.87 -  (if y=0 then add(PX P x R, Q)
1.88 -   else (if y=1 then PX P x (add (R, Q))
1.89 -         else PX P x (add (R, Pinj (y - 1) Q))))"
1.90 -  "add (PX P1 x P2, PX Q1 y Q2) =
1.91 -  (if x=y then mkPX (add (P1, Q1)) x (add (P2, Q2))
1.92 -  else (if x>y then mkPX (add (PX P1 (x-y) (Pc 0), Q1)) y (add (P2,Q2))
1.93 -        else mkPX (add (PX Q1 (y-x) (Pc 0), P1)) x (add (P2,Q2)) ))"
1.94 -
1.95 -  (* Multiplication *)
1.96 -recdef mul "measure (\<lambda>(x, y). size x + size y)"
1.97 -  "mul (Pc a, Pc b) = Pc (a*b)"
1.98 -  "mul (Pc c, Pinj i P) = (if c=0 then Pc 0 else mkPinj i (mul (P, Pc c)))"
1.99 -  "mul (Pinj i P, Pc c) = (if c=0 then Pc 0 else mkPinj i (mul (P, Pc c)))"
1.100 -  "mul (Pc c, PX P i Q) =
1.101 -  (if c=0 then Pc 0 else mkPX (mul (P, Pc c)) i (mul (Q, Pc c)))"
1.102 -  "mul (PX P i Q, Pc c) =
1.103 -  (if c=0 then Pc 0 else mkPX (mul (P, Pc c)) i (mul (Q, Pc c)))"
1.104 -  "mul (Pinj x P, Pinj y Q) =
1.105 -  (if x=y then mkPinj x (mul (P, Q))
1.106 -   else (if x>y then mkPinj y (mul (Pinj (x-y) P, Q))
1.107 -         else mkPinj x (mul (Pinj (y-x) Q, P)) ))"
1.108 -  "mul (Pinj x P, PX Q y R) =
1.109 -  (if x=0 then mul(P, PX Q y R)
1.110 -   else (if x=1 then mkPX (mul (Pinj x P, Q)) y (mul (R, P))
1.111 -         else mkPX (mul (Pinj x P, Q)) y (mul (R, Pinj (x - 1) P))))"
1.112 -  "mul (PX P x R, Pinj y Q) =
1.113 -  (if y=0 then mul(PX P x R, Q)
1.114 -   else (if y=1 then mkPX (mul (Pinj y Q, P)) x (mul (R, Q))
1.115 -         else mkPX (mul (Pinj y Q, P)) x (mul (R, Pinj (y - 1) Q))))"
1.116 -  "mul (PX P1 x P2, PX Q1 y Q2) =
1.117 -  add (mkPX (mul (P1, Q1)) (x+y) (mul (P2, Q2)),
1.118 -  add (mkPX (mul (P1, mkPinj 1 Q2)) x (Pc 0), mkPX (mul (Q1, mkPinj 1 P2)) y (Pc 0)) )"
1.119 -(hints simp add: mkPinj_def split: pol.split)
1.120 -
1.121 -  (* Negation*)
1.122 -primrec
1.123 -  "neg (Pc c) = (Pc (-c))"
1.124 -  "neg (Pinj i P) = Pinj i (neg P)"
1.125 -  "neg (PX P x Q) = PX (neg P) x (neg Q)"
1.126 -
1.127 -  (* Substraction*)
1.128 -constdefs sub :: "(('a::{comm_ring,recpower}) pol) \<Rightarrow> ('a pol) \<Rightarrow> 'a pol"
1.129 -  "sub p q \<equiv> add (p,neg q)"
1.130 -
1.131 -  (* Square for Fast Exponentation *)
1.132 -primrec
1.133 -  "sqr (Pc c) = Pc (c*c)"
1.134 -  "sqr (Pinj i P) = mkPinj i (sqr P)"
1.135 -  "sqr (PX A x B) = add (mkPX (sqr A) (x+x) (sqr B), mkPX (mul (mul (Pc (1+1), A), mkPinj 1 B)) x (Pc 0))"
1.136 -
1.137 -  (* Fast Exponentation *)
1.138 -lemma pow_wf:"odd n \<longrightarrow> (n::nat) div 2 < n" by(cases n, auto)
1.139 -recdef pow "measure (\<lambda>(x, y). y)"
1.140 -  "pow (p, 0) = Pc 1"
1.141 -  "pow (p, n) = (if even n then (pow (sqr p, n div 2)) else mul (p, pow (sqr p, n div 2)))"
1.143 -
1.144 -lemma pow_if: "pow (p,n) = (if n=0 then Pc 1 else (if even n then (pow (sqr p, n div 2)) else mul (p, pow (sqr p, n div 2))))"
1.145 -by (cases n) simp_all
1.146 -
1.147 -(*
1.148 -lemma number_of_nat_B0: "(number_of (w BIT bit.B0) ::nat) = 2* (number_of w)"
1.149 -by simp
1.150 -
1.151 -lemma number_of_nat_even: "even (number_of (w BIT bit.B0)::nat)"
1.152 -by simp
1.153 -
1.154 -lemma pow_even : "pow (p, number_of(w BIT bit.B0)) = pow (sqr p, number_of w)"
1.155 -  ( is "pow(?p,?n) = pow (_,?n2)")
1.156 -proof-
1.157 -  have "even ?n" by simp
1.158 -  hence "pow (p, ?n) = pow (sqr p, ?n div 2)"
1.159 -    apply simp
1.160 -    apply (cases "IntDef.neg (number_of w)")
1.161 -    apply simp
1.162 -    done
1.163 -*)
1.164 -  (* Normalization of polynomial expressions *)
1.165 -
1.166 -consts norm :: "('a::{comm_ring,recpower}) polex \<Rightarrow> 'a pol"
1.167 -primrec
1.168 -  "norm (Pol P) = P"
1.169 -  "norm (Add P Q) = add (norm P, norm Q)"
1.170 -  "norm (Sub p q) = sub (norm p) (norm q)"
1.171 -  "norm (Mul P Q) = mul (norm P, norm Q)"
1.172 -  "norm (Pow p n) = pow (norm p, n)"
1.173 -  "norm (Neg P) = neg (norm P)"
1.174 -
1.175 -  (* mkPinj preserve semantics *)
1.176 -lemma mkPinj_ci: "ALL a l. Ipol l (mkPinj a B) = Ipol l (Pinj a B)"
1.177 -  by(induct B, auto simp add: mkPinj_def ring_eq_simps)
1.178 -
1.179 -  (* mkPX preserves semantics *)
1.180 -lemma mkPX_ci: "ALL b l. Ipol l (mkPX A b C) = Ipol l (PX A b C)"
1.182 -
1.183 -  (* Correctness theorems for the implemented operations *)
1.184 -  (* Negation *)
1.185 -lemma neg_ci: "ALL l. Ipol l (neg P) = -(Ipol l P)"
1.186 -  by(induct P, auto)
1.187 -
1.189 -lemma add_ci: "ALL l. Ipol l (add (P, Q)) = Ipol l P + Ipol l Q"
1.190 -proof(induct P Q rule: add.induct)
1.191 -  case (6 x P y Q)
1.192 -  have "x < y \<or> x = y \<or> x > y" by arith
1.193 -  moreover
1.194 -  { assume "x<y" hence "EX d. d+x=y" by arith
1.195 -    then obtain d where "d+x=y"..
1.196 -    with prems have ?case by (auto simp add: mkPinj_ci ring_eq_simps) }
1.197 -  moreover
1.198 -  { assume "x=y" with prems have ?case by (auto simp add: mkPinj_ci)}
1.199 -  moreover
1.200 -  { assume "x>y" hence "EX d. d+y=x" by arith
1.201 -    then obtain d where "d+y=x"..
1.202 -    with prems have ?case by (auto simp add: mkPinj_ci ring_eq_simps) }
1.203 -  ultimately show ?case by blast
1.204 -next
1.205 -  case (7 x P Q y R)
1.206 -  have "x=0 \<or> (x = 1) \<or> (x > 1)" by arith
1.207 -  moreover
1.208 -  { assume "x=0" with prems have ?case by auto }
1.209 -  moreover
1.210 -  { assume "x=1" with prems have ?case by (auto simp add: ring_eq_simps) }
1.211 -  moreover
1.212 -  { assume "x > 1" from prems have ?case by(cases x, auto) }
1.213 -  ultimately show ?case by blast
1.214 -next
1.215 -  case (8 P x R y Q)
1.216 -  have "(y = 0) \<or> (y = 1) \<or> (y > 1)" by arith
1.217 -  moreover
1.218 -  {assume "y=0" with prems have ?case by simp}
1.219 -  moreover
1.220 -  {assume "y=1" with prems have ?case by simp}
1.221 -  moreover
1.222 -  {assume "y>1" hence "EX d. d+1=y" by arith
1.223 -    then obtain d where "d+1=y" ..
1.224 -    with prems have ?case by auto }
1.225 -  ultimately show ?case by blast
1.226 -next
1.227 -  case (9 P1 x P2 Q1 y Q2)
1.228 -  have "y < x \<or> x = y \<or> x < y" by arith
1.229 -  moreover
1.230 -  {assume "y < x" hence "EX d. d+y=x" by arith
1.231 -    then obtain d where "d+y=x"..
1.232 -    with prems have ?case by (auto simp add: power_add mkPX_ci ring_eq_simps) }
1.233 -  moreover
1.234 -  {assume "x=y" with prems have ?case by(auto simp add: mkPX_ci ring_eq_simps) }
1.235 -  moreover
1.236 -  {assume "x<y" hence "EX d. d+x=y" by arith
1.237 -    then obtain d where "d+x=y" ..
1.238 -    with prems have ?case by (auto simp add: mkPX_ci power_add ring_eq_simps) }
1.239 -  ultimately show ?case by blast
1.240 -qed (auto simp add: ring_eq_simps)
1.241 -
1.242 -    (* Multiplication *)
1.243 -lemma mul_ci: "ALL l. Ipol l (mul (P, Q)) = Ipol l P * Ipol l Q"
1.245 -
1.246 -  (* Substraction *)
1.247 -lemma sub_ci: "\<forall> l. Ipol l (sub p q) = (Ipol l p) - (Ipol l q)"
1.249 -
1.250 -  (* Square *)
1.251 -lemma sqr_ci:"ALL ls. Ipol ls (sqr p) = Ipol ls p * Ipol ls p"
1.253 -
1.254 -
1.255 -  (* Power *)
1.256 -lemma even_pow:"even n \<longrightarrow> pow (p, n) = pow (sqr p, n div 2)" by(induct n,auto)
1.257 -lemma pow_ci: "ALL p. Ipol ls (pow (p, n)) = (Ipol ls p) ^ n"
1.258 -proof(induct n rule: nat_less_induct)
1.259 -  case (1 k)
1.260 -  have two:"2=Suc( Suc 0)" by simp
1.261 -  from prems show ?case
1.262 -  proof(cases k)
1.263 -    case (Suc l)
1.264 -    hence KL:"k=Suc l" by simp
1.265 -    have "even l \<or> odd l" by (simp)
1.266 -    moreover
1.267 -    {assume EL:"even l"
1.268 -      have "Suc l div 2 = l div 2" by (simp add: nat_number even_nat_plus_one_div_two[OF EL])
1.269 -      moreover
1.270 -      from KL have"l<k" by simp
1.271 -      with prems have "ALL p. Ipol ls (pow (p, l)) = Ipol ls p ^ l" by simp
1.272 -      moreover
1.273 -      note prems even_nat_plus_one_div_two[OF EL]
1.274 -      ultimately have ?thesis by (simp add: mul_ci power_Suc even_pow) }
1.275 -    moreover
1.276 -    {assume OL:"odd l"
1.277 -      with prems have "\<lbrakk>\<forall>m<Suc l. \<forall>p. Ipol ls (pow (p, m)) = Ipol ls p ^ m; k = Suc l; odd l\<rbrakk> \<Longrightarrow> \<forall>p. Ipol ls (sqr p) ^ (Suc l div 2) = Ipol ls p ^ Suc l"
1.278 -      proof(cases l)
1.279 -        case (Suc w)
1.280 -        from prems have EW:"even w" by simp
1.281 -        from two have two_times:"(2 * (w div 2))= w" by (simp only: even_nat_div_two_times_two[OF EW])
1.282 -        have A:"ALL p. (Ipol ls p * Ipol ls p) = (Ipol ls p) ^ (Suc (Suc 0))" by (simp add: power_Suc)
1.283 -        from A two[symmetric] have "ALL p.(Ipol ls p * Ipol ls p) = (Ipol ls p) ^ 2" by simp
1.284 -        with prems show ?thesis by (auto simp add: power_mult[symmetric, of _ 2 _] two_times mul_ci sqr_ci)
1.285 -      qed(simp)
1.286 -      with prems have ?thesis by simp }
1.287 -    ultimately show ?thesis by blast
1.288 -  qed(simp)
1.289 -qed
1.290 -
1.291 -  (* Normalization preserves semantics  *)
1.292 -lemma norm_ci:"Ipolex l Pe = Ipol l (norm Pe)"
1.294 -
1.295 -(* Reflection lemma: Key to the (incomplete) decision procedure *)
1.296 -lemma norm_eq:
1.297 -  assumes A:"norm P1  = norm P2"
1.298 -  shows "Ipolex l P1 = Ipolex l P2"
1.299 -proof -
1.300 -  from A have "Ipol l (norm P1) = Ipol l (norm P2)" by simp
1.301 -  thus ?thesis by(simp only: norm_ci)
1.302 -qed
1.303 -
1.304 -
1.305 -    (* Code generation *)
1.306 -(*
1.307 -Does not work, since no generic ring operations implementation is there
1.308 -generate_code ("ring.ML") test = "norm"*)
1.309 -
1.310 -use "Tools/comm_ring.ML"
1.311 -setup "CommRing.setup"
1.312 -
1.313 -end
```