src/HOL/Polynomial.thy
 changeset 29451 5f0cb3fa530d child 29453 de4f26f59135
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Polynomial.thy	Sun Jan 11 12:05:50 2009 -0800
1.3 @@ -0,0 +1,922 @@
1.4 +(*  Title:      HOL/Polynomial.thy
1.5 +    Author:     Brian Huffman
1.6 +                Based on an earlier development by Clemens Ballarin
1.7 +*)
1.8 +
1.9 +header {* Univariate Polynomials *}
1.10 +
1.11 +theory Polynomial
1.12 +imports Plain SetInterval
1.13 +begin
1.14 +
1.15 +subsection {* Definition of type @{text poly} *}
1.16 +
1.17 +typedef (Poly) 'a poly = "{f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
1.18 +  morphisms coeff Abs_poly
1.19 +  by auto
1.20 +
1.21 +lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
1.22 +by (simp add: coeff_inject [symmetric] expand_fun_eq)
1.23 +
1.24 +lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
1.26 +
1.27 +
1.28 +subsection {* Degree of a polynomial *}
1.29 +
1.30 +definition
1.31 +  degree :: "'a::zero poly \<Rightarrow> nat" where
1.32 +  "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
1.33 +
1.34 +lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
1.35 +proof -
1.36 +  have "coeff p \<in> Poly"
1.37 +    by (rule coeff)
1.38 +  hence "\<exists>n. \<forall>i>n. coeff p i = 0"
1.39 +    unfolding Poly_def by simp
1.40 +  hence "\<forall>i>degree p. coeff p i = 0"
1.41 +    unfolding degree_def by (rule LeastI_ex)
1.42 +  moreover assume "degree p < n"
1.43 +  ultimately show ?thesis by simp
1.44 +qed
1.45 +
1.46 +lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
1.47 +  by (erule contrapos_np, rule coeff_eq_0, simp)
1.48 +
1.49 +lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
1.50 +  unfolding degree_def by (erule Least_le)
1.51 +
1.52 +lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
1.53 +  unfolding degree_def by (drule not_less_Least, simp)
1.54 +
1.55 +
1.56 +subsection {* The zero polynomial *}
1.57 +
1.58 +instantiation poly :: (zero) zero
1.59 +begin
1.60 +
1.61 +definition
1.62 +  zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
1.63 +
1.64 +instance ..
1.65 +end
1.66 +
1.67 +lemma coeff_0 [simp]: "coeff 0 n = 0"
1.68 +  unfolding zero_poly_def
1.69 +  by (simp add: Abs_poly_inverse Poly_def)
1.70 +
1.71 +lemma degree_0 [simp]: "degree 0 = 0"
1.72 +  by (rule order_antisym [OF degree_le le0]) simp
1.73 +
1.75 +  assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
1.76 +proof (cases "degree p")
1.77 +  case 0
1.78 +  from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
1.79 +    by (simp add: expand_poly_eq)
1.80 +  then obtain n where "coeff p n \<noteq> 0" ..
1.81 +  hence "n \<le> degree p" by (rule le_degree)
1.82 +  with `coeff p n \<noteq> 0` and `degree p = 0`
1.83 +  show "coeff p (degree p) \<noteq> 0" by simp
1.84 +next
1.85 +  case (Suc n)
1.86 +  from `degree p = Suc n` have "n < degree p" by simp
1.87 +  hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
1.88 +  then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
1.89 +  from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
1.90 +  also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
1.91 +  finally have "degree p = i" .
1.92 +  with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
1.93 +qed
1.94 +
1.95 +lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
1.97 +
1.98 +
1.99 +subsection {* List-style constructor for polynomials *}
1.100 +
1.101 +definition
1.102 +  pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
1.103 +where
1.104 +  [code del]: "pCons a p = Abs_poly (nat_case a (coeff p))"
1.105 +
1.106 +lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
1.107 +  unfolding Poly_def by (auto split: nat.split)
1.108 +
1.109 +lemma coeff_pCons:
1.110 +  "coeff (pCons a p) = nat_case a (coeff p)"
1.111 +  unfolding pCons_def
1.112 +  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
1.113 +
1.114 +lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
1.115 +  by (simp add: coeff_pCons)
1.116 +
1.117 +lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
1.118 +  by (simp add: coeff_pCons)
1.119 +
1.120 +lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
1.121 +by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
1.122 +
1.123 +lemma degree_pCons_eq:
1.124 +  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
1.125 +apply (rule order_antisym [OF degree_pCons_le])
1.126 +apply (rule le_degree, simp)
1.127 +done
1.128 +
1.129 +lemma degree_pCons_0: "degree (pCons a 0) = 0"
1.130 +apply (rule order_antisym [OF _ le0])
1.131 +apply (rule degree_le, simp add: coeff_pCons split: nat.split)
1.132 +done
1.133 +
1.134 +lemma degree_pCons_eq_if:
1.135 +  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
1.136 +apply (cases "p = 0", simp_all)
1.137 +apply (rule order_antisym [OF _ le0])
1.138 +apply (rule degree_le, simp add: coeff_pCons split: nat.split)
1.139 +apply (rule order_antisym [OF degree_pCons_le])
1.140 +apply (rule le_degree, simp)
1.141 +done
1.142 +
1.143 +lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
1.144 +by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.145 +
1.146 +lemma pCons_eq_iff [simp]:
1.147 +  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
1.148 +proof (safe)
1.149 +  assume "pCons a p = pCons b q"
1.150 +  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
1.151 +  then show "a = b" by simp
1.152 +next
1.153 +  assume "pCons a p = pCons b q"
1.154 +  then have "\<forall>n. coeff (pCons a p) (Suc n) =
1.155 +                 coeff (pCons b q) (Suc n)" by simp
1.156 +  then show "p = q" by (simp add: expand_poly_eq)
1.157 +qed
1.158 +
1.159 +lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
1.160 +  using pCons_eq_iff [of a p 0 0] by simp
1.161 +
1.162 +lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
1.163 +  unfolding Poly_def
1.164 +  by (clarify, rule_tac x=n in exI, simp)
1.165 +
1.166 +lemma pCons_cases [cases type: poly]:
1.167 +  obtains (pCons) a q where "p = pCons a q"
1.168 +proof
1.169 +  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
1.170 +    by (rule poly_ext)
1.171 +       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
1.172 +             split: nat.split)
1.173 +qed
1.174 +
1.175 +lemma pCons_induct [case_names 0 pCons, induct type: poly]:
1.176 +  assumes zero: "P 0"
1.177 +  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
1.178 +  shows "P p"
1.179 +proof (induct p rule: measure_induct_rule [where f=degree])
1.180 +  case (less p)
1.181 +  obtain a q where "p = pCons a q" by (rule pCons_cases)
1.182 +  have "P q"
1.183 +  proof (cases "q = 0")
1.184 +    case True
1.185 +    then show "P q" by (simp add: zero)
1.186 +  next
1.187 +    case False
1.188 +    then have "degree (pCons a q) = Suc (degree q)"
1.189 +      by (rule degree_pCons_eq)
1.190 +    then have "degree q < degree p"
1.191 +      using `p = pCons a q` by simp
1.192 +    then show "P q"
1.193 +      by (rule less.hyps)
1.194 +  qed
1.195 +  then have "P (pCons a q)"
1.196 +    by (rule pCons)
1.197 +  then show ?case
1.198 +    using `p = pCons a q` by simp
1.199 +qed
1.200 +
1.201 +
1.202 +subsection {* Monomials *}
1.203 +
1.204 +definition
1.205 +  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
1.206 +  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
1.207 +
1.208 +lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
1.209 +  unfolding monom_def
1.210 +  by (subst Abs_poly_inverse, auto simp add: Poly_def)
1.211 +
1.212 +lemma monom_0: "monom a 0 = pCons a 0"
1.213 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.214 +
1.215 +lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
1.216 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.217 +
1.218 +lemma monom_eq_0 [simp]: "monom 0 n = 0"
1.219 +  by (rule poly_ext) simp
1.220 +
1.221 +lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
1.222 +  by (simp add: expand_poly_eq)
1.223 +
1.224 +lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
1.225 +  by (simp add: expand_poly_eq)
1.226 +
1.227 +lemma degree_monom_le: "degree (monom a n) \<le> n"
1.228 +  by (rule degree_le, simp)
1.229 +
1.230 +lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
1.231 +  apply (rule order_antisym [OF degree_monom_le])
1.232 +  apply (rule le_degree, simp)
1.233 +  done
1.234 +
1.235 +
1.236 +subsection {* Addition and subtraction *}
1.237 +
1.239 +begin
1.240 +
1.241 +definition
1.242 +  plus_poly_def [code del]:
1.243 +    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
1.244 +
1.246 +  fixes f g :: "nat \<Rightarrow> 'a"
1.247 +  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
1.248 +  unfolding Poly_def
1.249 +  apply (clarify, rename_tac m n)
1.250 +  apply (rule_tac x="max m n" in exI, simp)
1.251 +  done
1.252 +
1.254 +  "coeff (p + q) n = coeff p n + coeff q n"
1.255 +  unfolding plus_poly_def
1.257 +
1.258 +instance proof
1.259 +  fix p q r :: "'a poly"
1.260 +  show "(p + q) + r = p + (q + r)"
1.262 +  show "p + q = q + p"
1.264 +  show "0 + p = p"
1.265 +    by (simp add: expand_poly_eq)
1.266 +qed
1.267 +
1.268 +end
1.269 +
1.271 +begin
1.272 +
1.273 +definition
1.274 +  uminus_poly_def [code del]:
1.275 +    "- p = Abs_poly (\<lambda>n. - coeff p n)"
1.276 +
1.277 +definition
1.278 +  minus_poly_def [code del]:
1.279 +    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
1.280 +
1.281 +lemma Poly_minus:
1.282 +  fixes f :: "nat \<Rightarrow> 'a"
1.283 +  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
1.284 +  unfolding Poly_def by simp
1.285 +
1.286 +lemma Poly_diff:
1.287 +  fixes f g :: "nat \<Rightarrow> 'a"
1.288 +  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
1.290 +
1.291 +lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
1.292 +  unfolding uminus_poly_def
1.293 +  by (simp add: Abs_poly_inverse coeff Poly_minus)
1.294 +
1.295 +lemma coeff_diff [simp]:
1.296 +  "coeff (p - q) n = coeff p n - coeff q n"
1.297 +  unfolding minus_poly_def
1.298 +  by (simp add: Abs_poly_inverse coeff Poly_diff)
1.299 +
1.300 +instance proof
1.301 +  fix p q :: "'a poly"
1.302 +  show "- p + p = 0"
1.303 +    by (simp add: expand_poly_eq)
1.304 +  show "p - q = p + - q"
1.305 +    by (simp add: expand_poly_eq diff_minus)
1.306 +qed
1.307 +
1.308 +end
1.309 +
1.311 +  "pCons a p + pCons b q = pCons (a + b) (p + q)"
1.312 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.313 +
1.314 +lemma minus_pCons [simp]:
1.315 +  "- pCons a p = pCons (- a) (- p)"
1.316 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.317 +
1.318 +lemma diff_pCons [simp]:
1.319 +  "pCons a p - pCons b q = pCons (a - b) (p - q)"
1.320 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.321 +
1.322 +lemma degree_add_le: "degree (p + q) \<le> max (degree p) (degree q)"
1.323 +  by (rule degree_le, auto simp add: coeff_eq_0)
1.324 +
1.326 +  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
1.327 +  apply (cases "q = 0", simp)
1.328 +  apply (rule order_antisym)
1.329 +  apply (rule ord_le_eq_trans [OF degree_add_le])
1.330 +  apply simp
1.331 +  apply (rule le_degree)
1.332 +  apply (simp add: coeff_eq_0)
1.333 +  done
1.334 +
1.336 +  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
1.337 +  using degree_add_eq_right [of q p]
1.339 +
1.340 +lemma degree_minus [simp]: "degree (- p) = degree p"
1.341 +  unfolding degree_def by simp
1.342 +
1.343 +lemma degree_diff_le: "degree (p - q) \<le> max (degree p) (degree q)"
1.344 +  using degree_add_le [where p=p and q="-q"]
1.345 +  by (simp add: diff_minus)
1.346 +
1.347 +lemma add_monom: "monom a n + monom b n = monom (a + b) n"
1.348 +  by (rule poly_ext) simp
1.349 +
1.350 +lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
1.351 +  by (rule poly_ext) simp
1.352 +
1.353 +lemma minus_monom: "- monom a n = monom (-a) n"
1.354 +  by (rule poly_ext) simp
1.355 +
1.356 +lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
1.357 +  by (cases "finite A", induct set: finite, simp_all)
1.358 +
1.359 +lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
1.360 +  by (rule poly_ext) (simp add: coeff_setsum)
1.361 +
1.362 +
1.363 +subsection {* Multiplication by a constant *}
1.364 +
1.365 +definition
1.366 +  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
1.367 +  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
1.368 +
1.369 +lemma Poly_smult:
1.370 +  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
1.371 +  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
1.372 +  unfolding Poly_def
1.373 +  by (clarify, rule_tac x=n in exI, simp)
1.374 +
1.375 +lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
1.376 +  unfolding smult_def
1.377 +  by (simp add: Abs_poly_inverse Poly_smult coeff)
1.378 +
1.379 +lemma degree_smult_le: "degree (smult a p) \<le> degree p"
1.380 +  by (rule degree_le, simp add: coeff_eq_0)
1.381 +
1.382 +lemma smult_smult: "smult a (smult b p) = smult (a * b) p"
1.383 +  by (rule poly_ext, simp add: mult_assoc)
1.384 +
1.385 +lemma smult_0_right [simp]: "smult a 0 = 0"
1.386 +  by (rule poly_ext, simp)
1.387 +
1.388 +lemma smult_0_left [simp]: "smult 0 p = 0"
1.389 +  by (rule poly_ext, simp)
1.390 +
1.391 +lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
1.392 +  by (rule poly_ext, simp)
1.393 +
1.395 +  "smult a (p + q) = smult a p + smult a q"
1.396 +  by (rule poly_ext, simp add: ring_simps)
1.397 +
1.399 +  "smult (a + b) p = smult a p + smult b p"
1.400 +  by (rule poly_ext, simp add: ring_simps)
1.401 +
1.402 +lemma smult_minus_right:
1.403 +  "smult (a::'a::comm_ring) (- p) = - smult a p"
1.404 +  by (rule poly_ext, simp)
1.405 +
1.406 +lemma smult_minus_left:
1.407 +  "smult (- a::'a::comm_ring) p = - smult a p"
1.408 +  by (rule poly_ext, simp)
1.409 +
1.410 +lemma smult_diff_right:
1.411 +  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
1.412 +  by (rule poly_ext, simp add: ring_simps)
1.413 +
1.414 +lemma smult_diff_left:
1.415 +  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
1.416 +  by (rule poly_ext, simp add: ring_simps)
1.417 +
1.418 +lemma smult_pCons [simp]:
1.419 +  "smult a (pCons b p) = pCons (a * b) (smult a p)"
1.420 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
1.421 +
1.422 +lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
1.424 +
1.425 +
1.426 +subsection {* Multiplication of polynomials *}
1.427 +
1.428 +lemma Poly_mult_lemma:
1.429 +  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0" and m n :: nat
1.430 +  assumes "\<forall>i>m. f i = 0"
1.431 +  assumes "\<forall>j>n. g j = 0"
1.432 +  shows "\<forall>k>m+n. (\<Sum>i\<le>k. f i * g (k-i)) = 0"
1.433 +proof (clarify)
1.434 +  fix k :: nat
1.435 +  assume "m + n < k"
1.436 +  show "(\<Sum>i\<le>k. f i * g (k - i)) = 0"
1.437 +  proof (rule setsum_0' [rule_format])
1.438 +    fix i :: nat
1.439 +    assume "i \<in> {..k}" hence "i \<le> k" by simp
1.440 +    with `m + n < k` have "m < i \<or> n < k - i" by arith
1.441 +    thus "f i * g (k - i) = 0"
1.442 +      using prems by auto
1.443 +  qed
1.444 +qed
1.445 +
1.446 +lemma Poly_mult:
1.447 +  fixes f g :: "nat \<Rightarrow> 'a::comm_semiring_0"
1.448 +  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. \<Sum>i\<le>n. f i * g (n-i)) \<in> Poly"
1.449 +  unfolding Poly_def
1.450 +  by (safe, rule exI, rule Poly_mult_lemma)
1.451 +
1.452 +lemma poly_mult_assoc_lemma:
1.453 +  fixes k :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
1.454 +  shows "(\<Sum>j\<le>k. \<Sum>i\<le>j. f i (j - i) (n - j)) =
1.455 +         (\<Sum>j\<le>k. \<Sum>i\<le>k - j. f j i (n - j - i))"
1.456 +proof (induct k)
1.457 +  case 0 show ?case by simp
1.458 +next
1.459 +  case (Suc k) thus ?case
1.461 +             cong: strong_setsum_cong)
1.462 +qed
1.463 +
1.464 +lemma poly_mult_commute_lemma:
1.465 +  fixes n :: nat and f :: "nat \<Rightarrow> nat \<Rightarrow> 'a::comm_monoid_add"
1.466 +  shows "(\<Sum>i\<le>n. f i (n - i)) = (\<Sum>i\<le>n. f (n - i) i)"
1.467 +proof (rule setsum_reindex_cong)
1.468 +  show "inj_on (\<lambda>i. n - i) {..n}"
1.469 +    by (rule inj_onI) simp
1.470 +  show "{..n} = (\<lambda>i. n - i) ` {..n}"
1.471 +    by (auto, rule_tac x="n - x" in image_eqI, simp_all)
1.472 +next
1.473 +  fix i assume "i \<in> {..n}"
1.474 +  hence "n - (n - i) = i" by simp
1.475 +  thus "f (n - i) i = f (n - i) (n - (n - i))" by simp
1.476 +qed
1.477 +
1.478 +text {* TODO: move to appropriate theory *}
1.479 +lemma setsum_atMost_Suc_shift:
1.480 +  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
1.481 +  shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
1.482 +proof (induct n)
1.483 +  case 0 show ?case by simp
1.484 +next
1.485 +  case (Suc n) note IH = this
1.486 +  have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
1.487 +    by (rule setsum_atMost_Suc)
1.488 +  also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
1.489 +    by (rule IH)
1.490 +  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
1.491 +             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
1.493 +  also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
1.494 +    by (rule setsum_atMost_Suc [symmetric])
1.495 +  finally show ?case .
1.496 +qed
1.497 +
1.498 +instantiation poly :: (comm_semiring_0) comm_semiring_0
1.499 +begin
1.500 +
1.501 +definition
1.502 +  times_poly_def:
1.503 +    "p * q = Abs_poly (\<lambda>n. \<Sum>i\<le>n. coeff p i * coeff q (n-i))"
1.504 +
1.505 +lemma coeff_mult:
1.506 +  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
1.507 +  unfolding times_poly_def
1.508 +  by (simp add: Abs_poly_inverse coeff Poly_mult)
1.509 +
1.510 +instance proof
1.511 +  fix p q r :: "'a poly"
1.512 +  show 0: "0 * p = 0"
1.513 +    by (simp add: expand_poly_eq coeff_mult)
1.514 +  show "p * 0 = 0"
1.515 +    by (simp add: expand_poly_eq coeff_mult)
1.516 +  show "(p + q) * r = p * r + q * r"
1.518 +  show "(p * q) * r = p * (q * r)"
1.519 +  proof (rule poly_ext)
1.520 +    fix n :: nat
1.521 +    have "(\<Sum>j\<le>n. \<Sum>i\<le>j. coeff p i * coeff q (j - i) * coeff r (n - j)) =
1.522 +          (\<Sum>j\<le>n. \<Sum>i\<le>n - j. coeff p j * coeff q i * coeff r (n - j - i))"
1.523 +      by (rule poly_mult_assoc_lemma)
1.524 +    thus "coeff ((p * q) * r) n = coeff (p * (q * r)) n"
1.525 +      by (simp add: coeff_mult setsum_right_distrib
1.526 +                    setsum_left_distrib mult_assoc)
1.527 +  qed
1.528 +  show "p * q = q * p"
1.529 +  proof (rule poly_ext)
1.530 +    fix n :: nat
1.531 +    have "(\<Sum>i\<le>n. coeff p i * coeff q (n - i)) =
1.532 +          (\<Sum>i\<le>n. coeff p (n - i) * coeff q i)"
1.533 +      by (rule poly_mult_commute_lemma)
1.534 +    thus "coeff (p * q) n = coeff (q * p) n"
1.535 +      by (simp add: coeff_mult mult_commute)
1.536 +  qed
1.537 +qed
1.538 +
1.539 +end
1.540 +
1.541 +lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
1.542 +apply (rule degree_le, simp add: coeff_mult)
1.543 +apply (rule Poly_mult_lemma)
1.545 +done
1.546 +
1.547 +lemma mult_pCons_left [simp]:
1.548 +  "pCons a p * q = smult a q + pCons 0 (p * q)"
1.549 +apply (rule poly_ext)
1.550 +apply (case_tac n)
1.552 +apply (simp add: coeff_mult setsum_atMost_Suc_shift
1.553 +            del: setsum_atMost_Suc)
1.554 +done
1.555 +
1.556 +lemma mult_pCons_right [simp]:
1.557 +  "p * pCons a q = smult a p + pCons 0 (p * q)"
1.558 +  using mult_pCons_left [of a q p] by (simp add: mult_commute)
1.559 +
1.560 +lemma mult_smult_left: "smult a p * q = smult a (p * q)"
1.562 +
1.563 +lemma mult_smult_right: "p * smult a q = smult a (p * q)"
1.564 +  using mult_smult_left [of a q p] by (simp add: mult_commute)
1.565 +
1.566 +lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
1.567 +  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
1.568 +
1.569 +
1.570 +subsection {* The unit polynomial and exponentiation *}
1.571 +
1.572 +instantiation poly :: (comm_semiring_1) comm_semiring_1
1.573 +begin
1.574 +
1.575 +definition
1.576 +  one_poly_def:
1.577 +    "1 = pCons 1 0"
1.578 +
1.579 +instance proof
1.580 +  fix p :: "'a poly" show "1 * p = p"
1.581 +    unfolding one_poly_def
1.582 +    by simp
1.583 +next
1.584 +  show "0 \<noteq> (1::'a poly)"
1.585 +    unfolding one_poly_def by simp
1.586 +qed
1.587 +
1.588 +end
1.589 +
1.590 +lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
1.591 +  unfolding one_poly_def
1.592 +  by (simp add: coeff_pCons split: nat.split)
1.593 +
1.594 +lemma degree_1 [simp]: "degree 1 = 0"
1.595 +  unfolding one_poly_def
1.596 +  by (rule degree_pCons_0)
1.597 +
1.598 +instantiation poly :: (comm_semiring_1) recpower
1.599 +begin
1.600 +
1.601 +primrec power_poly where
1.602 +  power_poly_0: "(p::'a poly) ^ 0 = 1"
1.603 +| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
1.604 +
1.605 +instance
1.606 +  by default simp_all
1.607 +
1.608 +end
1.609 +
1.610 +instance poly :: (comm_ring) comm_ring ..
1.611 +
1.612 +instance poly :: (comm_ring_1) comm_ring_1 ..
1.613 +
1.614 +instantiation poly :: (comm_ring_1) number_ring
1.615 +begin
1.616 +
1.617 +definition
1.618 +  "number_of k = (of_int k :: 'a poly)"
1.619 +
1.620 +instance
1.621 +  by default (rule number_of_poly_def)
1.622 +
1.623 +end
1.624 +
1.625 +
1.626 +subsection {* Polynomials form an integral domain *}
1.627 +
1.628 +lemma coeff_mult_degree_sum:
1.629 +  "coeff (p * q) (degree p + degree q) =
1.630 +   coeff p (degree p) * coeff q (degree q)"
1.631 + apply (simp add: coeff_mult)
1.632 + apply (subst setsum_diff1' [where a="degree p"])
1.633 +   apply simp
1.634 +  apply simp
1.635 + apply (subst setsum_0' [rule_format])
1.636 +  apply clarsimp
1.637 +  apply (subgoal_tac "degree p < a \<or> degree q < degree p + degree q - a")
1.638 +   apply (force simp add: coeff_eq_0)
1.639 +  apply arith
1.640 + apply simp
1.641 +done
1.642 +
1.643 +instance poly :: (idom) idom
1.644 +proof
1.645 +  fix p q :: "'a poly"
1.646 +  assume "p \<noteq> 0" and "q \<noteq> 0"
1.647 +  have "coeff (p * q) (degree p + degree q) =
1.648 +        coeff p (degree p) * coeff q (degree q)"
1.649 +    by (rule coeff_mult_degree_sum)
1.650 +  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
1.651 +    using `p \<noteq> 0` and `q \<noteq> 0` by simp
1.652 +  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
1.653 +  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
1.654 +qed
1.655 +
1.656 +lemma degree_mult_eq:
1.657 +  fixes p q :: "'a::idom poly"
1.658 +  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
1.659 +apply (rule order_antisym [OF degree_mult_le le_degree])
1.661 +done
1.662 +
1.663 +lemma dvd_imp_degree_le:
1.664 +  fixes p q :: "'a::idom poly"
1.665 +  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
1.666 +  by (erule dvdE, simp add: degree_mult_eq)
1.667 +
1.668 +
1.669 +subsection {* Long division of polynomials *}
1.670 +
1.671 +definition
1.672 +  divmod_poly_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
1.673 +where
1.674 +  "divmod_poly_rel x y q r \<longleftrightarrow>
1.675 +    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
1.676 +
1.677 +lemma divmod_poly_rel_0:
1.678 +  "divmod_poly_rel 0 y 0 0"
1.679 +  unfolding divmod_poly_rel_def by simp
1.680 +
1.681 +lemma divmod_poly_rel_by_0:
1.682 +  "divmod_poly_rel x 0 0 x"
1.683 +  unfolding divmod_poly_rel_def by simp
1.684 +
1.685 +lemma eq_zero_or_degree_less:
1.686 +  assumes "degree p \<le> n" and "coeff p n = 0"
1.687 +  shows "p = 0 \<or> degree p < n"
1.688 +proof (cases n)
1.689 +  case 0
1.690 +  with `degree p \<le> n` and `coeff p n = 0`
1.691 +  have "coeff p (degree p) = 0" by simp
1.692 +  then have "p = 0" by simp
1.693 +  then show ?thesis ..
1.694 +next
1.695 +  case (Suc m)
1.696 +  have "\<forall>i>n. coeff p i = 0"
1.697 +    using `degree p \<le> n` by (simp add: coeff_eq_0)
1.698 +  then have "\<forall>i\<ge>n. coeff p i = 0"
1.699 +    using `coeff p n = 0` by (simp add: le_less)
1.700 +  then have "\<forall>i>m. coeff p i = 0"
1.701 +    using `n = Suc m` by (simp add: less_eq_Suc_le)
1.702 +  then have "degree p \<le> m"
1.703 +    by (rule degree_le)
1.704 +  then have "degree p < n"
1.705 +    using `n = Suc m` by (simp add: less_Suc_eq_le)
1.706 +  then show ?thesis ..
1.707 +qed
1.708 +
1.709 +lemma divmod_poly_rel_pCons:
1.710 +  assumes rel: "divmod_poly_rel x y q r"
1.711 +  assumes y: "y \<noteq> 0"
1.712 +  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
1.713 +  shows "divmod_poly_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
1.714 +    (is "divmod_poly_rel ?x y ?q ?r")
1.715 +proof -
1.716 +  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
1.717 +    using assms unfolding divmod_poly_rel_def by simp_all
1.718 +
1.719 +  have 1: "?x = ?q * y + ?r"
1.720 +    using b x by simp
1.721 +
1.722 +  have 2: "?r = 0 \<or> degree ?r < degree y"
1.723 +  proof (rule eq_zero_or_degree_less)
1.724 +    have "degree ?r \<le> max (degree (pCons a r)) (degree (smult b y))"
1.725 +      by (rule degree_diff_le)
1.726 +    also have "\<dots> \<le> degree y"
1.727 +    proof (rule min_max.le_supI)
1.728 +      show "degree (pCons a r) \<le> degree y"
1.729 +        using r by (auto simp add: degree_pCons_eq_if)
1.730 +      show "degree (smult b y) \<le> degree y"
1.731 +        by (rule degree_smult_le)
1.732 +    qed
1.733 +    finally show "degree ?r \<le> degree y" .
1.734 +  next
1.735 +    show "coeff ?r (degree y) = 0"
1.736 +      using `y \<noteq> 0` unfolding b by simp
1.737 +  qed
1.738 +
1.739 +  from 1 2 show ?thesis
1.740 +    unfolding divmod_poly_rel_def
1.741 +    using `y \<noteq> 0` by simp
1.742 +qed
1.743 +
1.744 +lemma divmod_poly_rel_exists: "\<exists>q r. divmod_poly_rel x y q r"
1.745 +apply (cases "y = 0")
1.746 +apply (fast intro!: divmod_poly_rel_by_0)
1.747 +apply (induct x)
1.748 +apply (fast intro!: divmod_poly_rel_0)
1.749 +apply (fast intro!: divmod_poly_rel_pCons)
1.750 +done
1.751 +
1.752 +lemma divmod_poly_rel_unique:
1.753 +  assumes 1: "divmod_poly_rel x y q1 r1"
1.754 +  assumes 2: "divmod_poly_rel x y q2 r2"
1.755 +  shows "q1 = q2 \<and> r1 = r2"
1.756 +proof (cases "y = 0")
1.757 +  assume "y = 0" with assms show ?thesis
1.758 +    by (simp add: divmod_poly_rel_def)
1.759 +next
1.760 +  assume [simp]: "y \<noteq> 0"
1.761 +  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
1.762 +    unfolding divmod_poly_rel_def by simp_all
1.763 +  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
1.764 +    unfolding divmod_poly_rel_def by simp_all
1.765 +  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
1.766 +    by (simp add: ring_simps)
1.767 +  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
1.768 +    by (auto intro: le_less_trans [OF degree_diff_le])
1.769 +
1.770 +  show "q1 = q2 \<and> r1 = r2"
1.771 +  proof (rule ccontr)
1.772 +    assume "\<not> (q1 = q2 \<and> r1 = r2)"
1.773 +    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
1.774 +    with r3 have "degree (r2 - r1) < degree y" by simp
1.775 +    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
1.776 +    also have "\<dots> = degree ((q1 - q2) * y)"
1.777 +      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
1.778 +    also have "\<dots> = degree (r2 - r1)"
1.779 +      using q3 by simp
1.780 +    finally have "degree (r2 - r1) < degree (r2 - r1)" .
1.781 +    then show "False" by simp
1.782 +  qed
1.783 +qed
1.784 +
1.785 +lemmas divmod_poly_rel_unique_div =
1.786 +  divmod_poly_rel_unique [THEN conjunct1, standard]
1.787 +
1.788 +lemmas divmod_poly_rel_unique_mod =
1.789 +  divmod_poly_rel_unique [THEN conjunct2, standard]
1.790 +
1.791 +instantiation poly :: (field) ring_div
1.792 +begin
1.793 +
1.794 +definition div_poly where
1.795 +  [code del]: "x div y = (THE q. \<exists>r. divmod_poly_rel x y q r)"
1.796 +
1.797 +definition mod_poly where
1.798 +  [code del]: "x mod y = (THE r. \<exists>q. divmod_poly_rel x y q r)"
1.799 +
1.800 +lemma div_poly_eq:
1.801 +  "divmod_poly_rel x y q r \<Longrightarrow> x div y = q"
1.802 +unfolding div_poly_def
1.803 +by (fast elim: divmod_poly_rel_unique_div)
1.804 +
1.805 +lemma mod_poly_eq:
1.806 +  "divmod_poly_rel x y q r \<Longrightarrow> x mod y = r"
1.807 +unfolding mod_poly_def
1.808 +by (fast elim: divmod_poly_rel_unique_mod)
1.809 +
1.810 +lemma divmod_poly_rel:
1.811 +  "divmod_poly_rel x y (x div y) (x mod y)"
1.812 +proof -
1.813 +  from divmod_poly_rel_exists
1.814 +    obtain q r where "divmod_poly_rel x y q r" by fast
1.815 +  thus ?thesis
1.816 +    by (simp add: div_poly_eq mod_poly_eq)
1.817 +qed
1.818 +
1.819 +instance proof
1.820 +  fix x y :: "'a poly"
1.821 +  show "x div y * y + x mod y = x"
1.822 +    using divmod_poly_rel [of x y]
1.823 +    by (simp add: divmod_poly_rel_def)
1.824 +next
1.825 +  fix x :: "'a poly"
1.826 +  have "divmod_poly_rel x 0 0 x"
1.827 +    by (rule divmod_poly_rel_by_0)
1.828 +  thus "x div 0 = 0"
1.829 +    by (rule div_poly_eq)
1.830 +next
1.831 +  fix y :: "'a poly"
1.832 +  have "divmod_poly_rel 0 y 0 0"
1.833 +    by (rule divmod_poly_rel_0)
1.834 +  thus "0 div y = 0"
1.835 +    by (rule div_poly_eq)
1.836 +next
1.837 +  fix x y z :: "'a poly"
1.838 +  assume "y \<noteq> 0"
1.839 +  hence "divmod_poly_rel (x + z * y) y (z + x div y) (x mod y)"
1.840 +    using divmod_poly_rel [of x y]
1.841 +    by (simp add: divmod_poly_rel_def left_distrib)
1.842 +  thus "(x + z * y) div y = z + x div y"
1.843 +    by (rule div_poly_eq)
1.844 +qed
1.845 +
1.846 +end
1.847 +
1.848 +lemma degree_mod_less:
1.849 +  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
1.850 +  using divmod_poly_rel [of x y]
1.851 +  unfolding divmod_poly_rel_def by simp
1.852 +
1.853 +lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
1.854 +proof -
1.855 +  assume "degree x < degree y"
1.856 +  hence "divmod_poly_rel x y 0 x"
1.857 +    by (simp add: divmod_poly_rel_def)
1.858 +  thus "x div y = 0" by (rule div_poly_eq)
1.859 +qed
1.860 +
1.861 +lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
1.862 +proof -
1.863 +  assume "degree x < degree y"
1.864 +  hence "divmod_poly_rel x y 0 x"
1.865 +    by (simp add: divmod_poly_rel_def)
1.866 +  thus "x mod y = x" by (rule mod_poly_eq)
1.867 +qed
1.868 +
1.869 +lemma mod_pCons:
1.870 +  fixes a and x
1.871 +  assumes y: "y \<noteq> 0"
1.872 +  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
1.873 +  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
1.874 +unfolding b
1.875 +apply (rule mod_poly_eq)
1.876 +apply (rule divmod_poly_rel_pCons [OF divmod_poly_rel y refl])
1.877 +done
1.878 +
1.879 +
1.880 +subsection {* Evaluation of polynomials *}
1.881 +
1.882 +definition
1.883 +  poly :: "'a::{comm_semiring_1,recpower} poly \<Rightarrow> 'a \<Rightarrow> 'a" where
1.884 +  "poly p = (\<lambda>x. \<Sum>n\<le>degree p. coeff p n * x ^ n)"
1.885 +
1.886 +lemma poly_0 [simp]: "poly 0 x = 0"
1.887 +  unfolding poly_def by simp
1.888 +
1.889 +lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
1.890 +  unfolding poly_def
1.891 +  by (simp add: degree_pCons_eq_if setsum_atMost_Suc_shift power_Suc
1.892 +                setsum_left_distrib setsum_right_distrib mult_ac
1.893 +           del: setsum_atMost_Suc)
1.894 +
1.895 +lemma poly_1 [simp]: "poly 1 x = 1"
1.896 +  unfolding one_poly_def by simp
1.897 +
1.898 +lemma poly_monom: "poly (monom a n) x = a * x ^ n"
1.899 +  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
1.900 +
1.901 +lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
1.902 +  apply (induct p arbitrary: q, simp)
1.903 +  apply (case_tac q, simp, simp add: ring_simps)
1.904 +  done
1.905 +
1.906 +lemma poly_minus [simp]:
1.907 +  fixes x :: "'a::{comm_ring_1,recpower}"
1.908 +  shows "poly (- p) x = - poly p x"
1.909 +  by (induct p, simp_all)
1.910 +
1.911 +lemma poly_diff [simp]:
1.912 +  fixes x :: "'a::{comm_ring_1,recpower}"
1.913 +  shows "poly (p - q) x = poly p x - poly q x"
1.914 +  by (simp add: diff_minus)
1.915 +
1.916 +lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
1.917 +  by (cases "finite A", induct set: finite, simp_all)
1.918 +
1.919 +lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
1.920 +  by (induct p, simp, simp add: ring_simps)
1.921 +
1.922 +lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
1.923 +  by (induct p, simp_all, simp add: ring_simps)
1.924 +
1.925 +end
```