src/HOL/Library/Polynomial.thy
changeset 29987 391dcbd7e4dd
parent 29980 17ddfd0c3506
child 30072 4eecd8b9b6cf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Library/Polynomial.thy	Wed Feb 18 20:14:45 2009 -0800
     1.3 @@ -0,0 +1,1441 @@
     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 Main
    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.25 +by (simp add: expand_poly_eq)
    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.74 +lemma leading_coeff_neq_0:
    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.96 +  by (cases "p = 0", simp, simp add: leading_coeff_neq_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 +syntax
   1.107 +  "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
   1.108 +
   1.109 +translations
   1.110 +  "[:x, xs:]" == "CONST pCons x [:xs:]"
   1.111 +  "[:x:]" == "CONST pCons x 0"
   1.112 +
   1.113 +lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
   1.114 +  unfolding Poly_def by (auto split: nat.split)
   1.115 +
   1.116 +lemma coeff_pCons:
   1.117 +  "coeff (pCons a p) = nat_case a (coeff p)"
   1.118 +  unfolding pCons_def
   1.119 +  by (simp add: Abs_poly_inverse Poly_nat_case coeff)
   1.120 +
   1.121 +lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
   1.122 +  by (simp add: coeff_pCons)
   1.123 +
   1.124 +lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
   1.125 +  by (simp add: coeff_pCons)
   1.126 +
   1.127 +lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
   1.128 +by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
   1.129 +
   1.130 +lemma degree_pCons_eq:
   1.131 +  "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
   1.132 +apply (rule order_antisym [OF degree_pCons_le])
   1.133 +apply (rule le_degree, simp)
   1.134 +done
   1.135 +
   1.136 +lemma degree_pCons_0: "degree (pCons a 0) = 0"
   1.137 +apply (rule order_antisym [OF _ le0])
   1.138 +apply (rule degree_le, simp add: coeff_pCons split: nat.split)
   1.139 +done
   1.140 +
   1.141 +lemma degree_pCons_eq_if [simp]:
   1.142 +  "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
   1.143 +apply (cases "p = 0", simp_all)
   1.144 +apply (rule order_antisym [OF _ le0])
   1.145 +apply (rule degree_le, simp add: coeff_pCons split: nat.split)
   1.146 +apply (rule order_antisym [OF degree_pCons_le])
   1.147 +apply (rule le_degree, simp)
   1.148 +done
   1.149 +
   1.150 +lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
   1.151 +by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.152 +
   1.153 +lemma pCons_eq_iff [simp]:
   1.154 +  "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
   1.155 +proof (safe)
   1.156 +  assume "pCons a p = pCons b q"
   1.157 +  then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
   1.158 +  then show "a = b" by simp
   1.159 +next
   1.160 +  assume "pCons a p = pCons b q"
   1.161 +  then have "\<forall>n. coeff (pCons a p) (Suc n) =
   1.162 +                 coeff (pCons b q) (Suc n)" by simp
   1.163 +  then show "p = q" by (simp add: expand_poly_eq)
   1.164 +qed
   1.165 +
   1.166 +lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
   1.167 +  using pCons_eq_iff [of a p 0 0] by simp
   1.168 +
   1.169 +lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
   1.170 +  unfolding Poly_def
   1.171 +  by (clarify, rule_tac x=n in exI, simp)
   1.172 +
   1.173 +lemma pCons_cases [cases type: poly]:
   1.174 +  obtains (pCons) a q where "p = pCons a q"
   1.175 +proof
   1.176 +  show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
   1.177 +    by (rule poly_ext)
   1.178 +       (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
   1.179 +             split: nat.split)
   1.180 +qed
   1.181 +
   1.182 +lemma pCons_induct [case_names 0 pCons, induct type: poly]:
   1.183 +  assumes zero: "P 0"
   1.184 +  assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
   1.185 +  shows "P p"
   1.186 +proof (induct p rule: measure_induct_rule [where f=degree])
   1.187 +  case (less p)
   1.188 +  obtain a q where "p = pCons a q" by (rule pCons_cases)
   1.189 +  have "P q"
   1.190 +  proof (cases "q = 0")
   1.191 +    case True
   1.192 +    then show "P q" by (simp add: zero)
   1.193 +  next
   1.194 +    case False
   1.195 +    then have "degree (pCons a q) = Suc (degree q)"
   1.196 +      by (rule degree_pCons_eq)
   1.197 +    then have "degree q < degree p"
   1.198 +      using `p = pCons a q` by simp
   1.199 +    then show "P q"
   1.200 +      by (rule less.hyps)
   1.201 +  qed
   1.202 +  then have "P (pCons a q)"
   1.203 +    by (rule pCons)
   1.204 +  then show ?case
   1.205 +    using `p = pCons a q` by simp
   1.206 +qed
   1.207 +
   1.208 +
   1.209 +subsection {* Recursion combinator for polynomials *}
   1.210 +
   1.211 +function
   1.212 +  poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b"
   1.213 +where
   1.214 +  poly_rec_pCons_eq_if [simp del, code del]:
   1.215 +    "poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)"
   1.216 +by (case_tac x, rename_tac q, case_tac q, auto)
   1.217 +
   1.218 +termination poly_rec
   1.219 +by (relation "measure (degree \<circ> snd \<circ> snd)", simp)
   1.220 +   (simp add: degree_pCons_eq)
   1.221 +
   1.222 +lemma poly_rec_0:
   1.223 +  "f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z"
   1.224 +  using poly_rec_pCons_eq_if [of z f 0 0] by simp
   1.225 +
   1.226 +lemma poly_rec_pCons:
   1.227 +  "f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)"
   1.228 +  by (simp add: poly_rec_pCons_eq_if poly_rec_0)
   1.229 +
   1.230 +
   1.231 +subsection {* Monomials *}
   1.232 +
   1.233 +definition
   1.234 +  monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
   1.235 +  "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
   1.236 +
   1.237 +lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
   1.238 +  unfolding monom_def
   1.239 +  by (subst Abs_poly_inverse, auto simp add: Poly_def)
   1.240 +
   1.241 +lemma monom_0: "monom a 0 = pCons a 0"
   1.242 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.243 +
   1.244 +lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
   1.245 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.246 +
   1.247 +lemma monom_eq_0 [simp]: "monom 0 n = 0"
   1.248 +  by (rule poly_ext) simp
   1.249 +
   1.250 +lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
   1.251 +  by (simp add: expand_poly_eq)
   1.252 +
   1.253 +lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
   1.254 +  by (simp add: expand_poly_eq)
   1.255 +
   1.256 +lemma degree_monom_le: "degree (monom a n) \<le> n"
   1.257 +  by (rule degree_le, simp)
   1.258 +
   1.259 +lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
   1.260 +  apply (rule order_antisym [OF degree_monom_le])
   1.261 +  apply (rule le_degree, simp)
   1.262 +  done
   1.263 +
   1.264 +
   1.265 +subsection {* Addition and subtraction *}
   1.266 +
   1.267 +instantiation poly :: (comm_monoid_add) comm_monoid_add
   1.268 +begin
   1.269 +
   1.270 +definition
   1.271 +  plus_poly_def [code del]:
   1.272 +    "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
   1.273 +
   1.274 +lemma Poly_add:
   1.275 +  fixes f g :: "nat \<Rightarrow> 'a"
   1.276 +  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
   1.277 +  unfolding Poly_def
   1.278 +  apply (clarify, rename_tac m n)
   1.279 +  apply (rule_tac x="max m n" in exI, simp)
   1.280 +  done
   1.281 +
   1.282 +lemma coeff_add [simp]:
   1.283 +  "coeff (p + q) n = coeff p n + coeff q n"
   1.284 +  unfolding plus_poly_def
   1.285 +  by (simp add: Abs_poly_inverse coeff Poly_add)
   1.286 +
   1.287 +instance proof
   1.288 +  fix p q r :: "'a poly"
   1.289 +  show "(p + q) + r = p + (q + r)"
   1.290 +    by (simp add: expand_poly_eq add_assoc)
   1.291 +  show "p + q = q + p"
   1.292 +    by (simp add: expand_poly_eq add_commute)
   1.293 +  show "0 + p = p"
   1.294 +    by (simp add: expand_poly_eq)
   1.295 +qed
   1.296 +
   1.297 +end
   1.298 +
   1.299 +instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
   1.300 +proof
   1.301 +  fix p q r :: "'a poly"
   1.302 +  assume "p + q = p + r" thus "q = r"
   1.303 +    by (simp add: expand_poly_eq)
   1.304 +qed
   1.305 +
   1.306 +instantiation poly :: (ab_group_add) ab_group_add
   1.307 +begin
   1.308 +
   1.309 +definition
   1.310 +  uminus_poly_def [code del]:
   1.311 +    "- p = Abs_poly (\<lambda>n. - coeff p n)"
   1.312 +
   1.313 +definition
   1.314 +  minus_poly_def [code del]:
   1.315 +    "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
   1.316 +
   1.317 +lemma Poly_minus:
   1.318 +  fixes f :: "nat \<Rightarrow> 'a"
   1.319 +  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
   1.320 +  unfolding Poly_def by simp
   1.321 +
   1.322 +lemma Poly_diff:
   1.323 +  fixes f g :: "nat \<Rightarrow> 'a"
   1.324 +  shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
   1.325 +  unfolding diff_minus by (simp add: Poly_add Poly_minus)
   1.326 +
   1.327 +lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
   1.328 +  unfolding uminus_poly_def
   1.329 +  by (simp add: Abs_poly_inverse coeff Poly_minus)
   1.330 +
   1.331 +lemma coeff_diff [simp]:
   1.332 +  "coeff (p - q) n = coeff p n - coeff q n"
   1.333 +  unfolding minus_poly_def
   1.334 +  by (simp add: Abs_poly_inverse coeff Poly_diff)
   1.335 +
   1.336 +instance proof
   1.337 +  fix p q :: "'a poly"
   1.338 +  show "- p + p = 0"
   1.339 +    by (simp add: expand_poly_eq)
   1.340 +  show "p - q = p + - q"
   1.341 +    by (simp add: expand_poly_eq diff_minus)
   1.342 +qed
   1.343 +
   1.344 +end
   1.345 +
   1.346 +lemma add_pCons [simp]:
   1.347 +  "pCons a p + pCons b q = pCons (a + b) (p + q)"
   1.348 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.349 +
   1.350 +lemma minus_pCons [simp]:
   1.351 +  "- pCons a p = pCons (- a) (- p)"
   1.352 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.353 +
   1.354 +lemma diff_pCons [simp]:
   1.355 +  "pCons a p - pCons b q = pCons (a - b) (p - q)"
   1.356 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.357 +
   1.358 +lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
   1.359 +  by (rule degree_le, auto simp add: coeff_eq_0)
   1.360 +
   1.361 +lemma degree_add_le:
   1.362 +  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
   1.363 +  by (auto intro: order_trans degree_add_le_max)
   1.364 +
   1.365 +lemma degree_add_less:
   1.366 +  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
   1.367 +  by (auto intro: le_less_trans degree_add_le_max)
   1.368 +
   1.369 +lemma degree_add_eq_right:
   1.370 +  "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
   1.371 +  apply (cases "q = 0", simp)
   1.372 +  apply (rule order_antisym)
   1.373 +  apply (simp add: degree_add_le)
   1.374 +  apply (rule le_degree)
   1.375 +  apply (simp add: coeff_eq_0)
   1.376 +  done
   1.377 +
   1.378 +lemma degree_add_eq_left:
   1.379 +  "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
   1.380 +  using degree_add_eq_right [of q p]
   1.381 +  by (simp add: add_commute)
   1.382 +
   1.383 +lemma degree_minus [simp]: "degree (- p) = degree p"
   1.384 +  unfolding degree_def by simp
   1.385 +
   1.386 +lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
   1.387 +  using degree_add_le [where p=p and q="-q"]
   1.388 +  by (simp add: diff_minus)
   1.389 +
   1.390 +lemma degree_diff_le:
   1.391 +  "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
   1.392 +  by (simp add: diff_minus degree_add_le)
   1.393 +
   1.394 +lemma degree_diff_less:
   1.395 +  "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
   1.396 +  by (simp add: diff_minus degree_add_less)
   1.397 +
   1.398 +lemma add_monom: "monom a n + monom b n = monom (a + b) n"
   1.399 +  by (rule poly_ext) simp
   1.400 +
   1.401 +lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
   1.402 +  by (rule poly_ext) simp
   1.403 +
   1.404 +lemma minus_monom: "- monom a n = monom (-a) n"
   1.405 +  by (rule poly_ext) simp
   1.406 +
   1.407 +lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
   1.408 +  by (cases "finite A", induct set: finite, simp_all)
   1.409 +
   1.410 +lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
   1.411 +  by (rule poly_ext) (simp add: coeff_setsum)
   1.412 +
   1.413 +
   1.414 +subsection {* Multiplication by a constant *}
   1.415 +
   1.416 +definition
   1.417 +  smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
   1.418 +  "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
   1.419 +
   1.420 +lemma Poly_smult:
   1.421 +  fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
   1.422 +  shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
   1.423 +  unfolding Poly_def
   1.424 +  by (clarify, rule_tac x=n in exI, simp)
   1.425 +
   1.426 +lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
   1.427 +  unfolding smult_def
   1.428 +  by (simp add: Abs_poly_inverse Poly_smult coeff)
   1.429 +
   1.430 +lemma degree_smult_le: "degree (smult a p) \<le> degree p"
   1.431 +  by (rule degree_le, simp add: coeff_eq_0)
   1.432 +
   1.433 +lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
   1.434 +  by (rule poly_ext, simp add: mult_assoc)
   1.435 +
   1.436 +lemma smult_0_right [simp]: "smult a 0 = 0"
   1.437 +  by (rule poly_ext, simp)
   1.438 +
   1.439 +lemma smult_0_left [simp]: "smult 0 p = 0"
   1.440 +  by (rule poly_ext, simp)
   1.441 +
   1.442 +lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
   1.443 +  by (rule poly_ext, simp)
   1.444 +
   1.445 +lemma smult_add_right:
   1.446 +  "smult a (p + q) = smult a p + smult a q"
   1.447 +  by (rule poly_ext, simp add: algebra_simps)
   1.448 +
   1.449 +lemma smult_add_left:
   1.450 +  "smult (a + b) p = smult a p + smult b p"
   1.451 +  by (rule poly_ext, simp add: algebra_simps)
   1.452 +
   1.453 +lemma smult_minus_right [simp]:
   1.454 +  "smult (a::'a::comm_ring) (- p) = - smult a p"
   1.455 +  by (rule poly_ext, simp)
   1.456 +
   1.457 +lemma smult_minus_left [simp]:
   1.458 +  "smult (- a::'a::comm_ring) p = - smult a p"
   1.459 +  by (rule poly_ext, simp)
   1.460 +
   1.461 +lemma smult_diff_right:
   1.462 +  "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
   1.463 +  by (rule poly_ext, simp add: algebra_simps)
   1.464 +
   1.465 +lemma smult_diff_left:
   1.466 +  "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
   1.467 +  by (rule poly_ext, simp add: algebra_simps)
   1.468 +
   1.469 +lemmas smult_distribs =
   1.470 +  smult_add_left smult_add_right
   1.471 +  smult_diff_left smult_diff_right
   1.472 +
   1.473 +lemma smult_pCons [simp]:
   1.474 +  "smult a (pCons b p) = pCons (a * b) (smult a p)"
   1.475 +  by (rule poly_ext, simp add: coeff_pCons split: nat.split)
   1.476 +
   1.477 +lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
   1.478 +  by (induct n, simp add: monom_0, simp add: monom_Suc)
   1.479 +
   1.480 +lemma degree_smult_eq [simp]:
   1.481 +  fixes a :: "'a::idom"
   1.482 +  shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
   1.483 +  by (cases "a = 0", simp, simp add: degree_def)
   1.484 +
   1.485 +lemma smult_eq_0_iff [simp]:
   1.486 +  fixes a :: "'a::idom"
   1.487 +  shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
   1.488 +  by (simp add: expand_poly_eq)
   1.489 +
   1.490 +
   1.491 +subsection {* Multiplication of polynomials *}
   1.492 +
   1.493 +text {* TODO: move to SetInterval.thy *}
   1.494 +lemma setsum_atMost_Suc_shift:
   1.495 +  fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
   1.496 +  shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
   1.497 +proof (induct n)
   1.498 +  case 0 show ?case by simp
   1.499 +next
   1.500 +  case (Suc n) note IH = this
   1.501 +  have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
   1.502 +    by (rule setsum_atMost_Suc)
   1.503 +  also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
   1.504 +    by (rule IH)
   1.505 +  also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
   1.506 +             f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
   1.507 +    by (rule add_assoc)
   1.508 +  also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
   1.509 +    by (rule setsum_atMost_Suc [symmetric])
   1.510 +  finally show ?case .
   1.511 +qed
   1.512 +
   1.513 +instantiation poly :: (comm_semiring_0) comm_semiring_0
   1.514 +begin
   1.515 +
   1.516 +definition
   1.517 +  times_poly_def [code del]:
   1.518 +    "p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"
   1.519 +
   1.520 +lemma mult_poly_0_left: "(0::'a poly) * q = 0"
   1.521 +  unfolding times_poly_def by (simp add: poly_rec_0)
   1.522 +
   1.523 +lemma mult_pCons_left [simp]:
   1.524 +  "pCons a p * q = smult a q + pCons 0 (p * q)"
   1.525 +  unfolding times_poly_def by (simp add: poly_rec_pCons)
   1.526 +
   1.527 +lemma mult_poly_0_right: "p * (0::'a poly) = 0"
   1.528 +  by (induct p, simp add: mult_poly_0_left, simp)
   1.529 +
   1.530 +lemma mult_pCons_right [simp]:
   1.531 +  "p * pCons a q = smult a p + pCons 0 (p * q)"
   1.532 +  by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)
   1.533 +
   1.534 +lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
   1.535 +
   1.536 +lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
   1.537 +  by (induct p, simp add: mult_poly_0, simp add: smult_add_right)
   1.538 +
   1.539 +lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
   1.540 +  by (induct q, simp add: mult_poly_0, simp add: smult_add_right)
   1.541 +
   1.542 +lemma mult_poly_add_left:
   1.543 +  fixes p q r :: "'a poly"
   1.544 +  shows "(p + q) * r = p * r + q * r"
   1.545 +  by (induct r, simp add: mult_poly_0,
   1.546 +                simp add: smult_distribs algebra_simps)
   1.547 +
   1.548 +instance proof
   1.549 +  fix p q r :: "'a poly"
   1.550 +  show 0: "0 * p = 0"
   1.551 +    by (rule mult_poly_0_left)
   1.552 +  show "p * 0 = 0"
   1.553 +    by (rule mult_poly_0_right)
   1.554 +  show "(p + q) * r = p * r + q * r"
   1.555 +    by (rule mult_poly_add_left)
   1.556 +  show "(p * q) * r = p * (q * r)"
   1.557 +    by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
   1.558 +  show "p * q = q * p"
   1.559 +    by (induct p, simp add: mult_poly_0, simp)
   1.560 +qed
   1.561 +
   1.562 +end
   1.563 +
   1.564 +instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
   1.565 +
   1.566 +lemma coeff_mult:
   1.567 +  "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
   1.568 +proof (induct p arbitrary: n)
   1.569 +  case 0 show ?case by simp
   1.570 +next
   1.571 +  case (pCons a p n) thus ?case
   1.572 +    by (cases n, simp, simp add: setsum_atMost_Suc_shift
   1.573 +                            del: setsum_atMost_Suc)
   1.574 +qed
   1.575 +
   1.576 +lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
   1.577 +apply (rule degree_le)
   1.578 +apply (induct p)
   1.579 +apply simp
   1.580 +apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
   1.581 +done
   1.582 +
   1.583 +lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
   1.584 +  by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
   1.585 +
   1.586 +
   1.587 +subsection {* The unit polynomial and exponentiation *}
   1.588 +
   1.589 +instantiation poly :: (comm_semiring_1) comm_semiring_1
   1.590 +begin
   1.591 +
   1.592 +definition
   1.593 +  one_poly_def:
   1.594 +    "1 = pCons 1 0"
   1.595 +
   1.596 +instance proof
   1.597 +  fix p :: "'a poly" show "1 * p = p"
   1.598 +    unfolding one_poly_def
   1.599 +    by simp
   1.600 +next
   1.601 +  show "0 \<noteq> (1::'a poly)"
   1.602 +    unfolding one_poly_def by simp
   1.603 +qed
   1.604 +
   1.605 +end
   1.606 +
   1.607 +instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
   1.608 +
   1.609 +lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
   1.610 +  unfolding one_poly_def
   1.611 +  by (simp add: coeff_pCons split: nat.split)
   1.612 +
   1.613 +lemma degree_1 [simp]: "degree 1 = 0"
   1.614 +  unfolding one_poly_def
   1.615 +  by (rule degree_pCons_0)
   1.616 +
   1.617 +text {* Lemmas about divisibility *}
   1.618 +
   1.619 +lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
   1.620 +proof -
   1.621 +  assume "p dvd q"
   1.622 +  then obtain k where "q = p * k" ..
   1.623 +  then have "smult a q = p * smult a k" by simp
   1.624 +  then show "p dvd smult a q" ..
   1.625 +qed
   1.626 +
   1.627 +lemma dvd_smult_cancel:
   1.628 +  fixes a :: "'a::field"
   1.629 +  shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
   1.630 +  by (drule dvd_smult [where a="inverse a"]) simp
   1.631 +
   1.632 +lemma dvd_smult_iff:
   1.633 +  fixes a :: "'a::field"
   1.634 +  shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
   1.635 +  by (safe elim!: dvd_smult dvd_smult_cancel)
   1.636 +
   1.637 +instantiation poly :: (comm_semiring_1) recpower
   1.638 +begin
   1.639 +
   1.640 +primrec power_poly where
   1.641 +  power_poly_0: "(p::'a poly) ^ 0 = 1"
   1.642 +| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n"
   1.643 +
   1.644 +instance
   1.645 +  by default simp_all
   1.646 +
   1.647 +end
   1.648 +
   1.649 +lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
   1.650 +by (induct n, simp, auto intro: order_trans degree_mult_le)
   1.651 +
   1.652 +instance poly :: (comm_ring) comm_ring ..
   1.653 +
   1.654 +instance poly :: (comm_ring_1) comm_ring_1 ..
   1.655 +
   1.656 +instantiation poly :: (comm_ring_1) number_ring
   1.657 +begin
   1.658 +
   1.659 +definition
   1.660 +  "number_of k = (of_int k :: 'a poly)"
   1.661 +
   1.662 +instance
   1.663 +  by default (rule number_of_poly_def)
   1.664 +
   1.665 +end
   1.666 +
   1.667 +
   1.668 +subsection {* Polynomials form an integral domain *}
   1.669 +
   1.670 +lemma coeff_mult_degree_sum:
   1.671 +  "coeff (p * q) (degree p + degree q) =
   1.672 +   coeff p (degree p) * coeff q (degree q)"
   1.673 +  by (induct p, simp, simp add: coeff_eq_0)
   1.674 +
   1.675 +instance poly :: (idom) idom
   1.676 +proof
   1.677 +  fix p q :: "'a poly"
   1.678 +  assume "p \<noteq> 0" and "q \<noteq> 0"
   1.679 +  have "coeff (p * q) (degree p + degree q) =
   1.680 +        coeff p (degree p) * coeff q (degree q)"
   1.681 +    by (rule coeff_mult_degree_sum)
   1.682 +  also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
   1.683 +    using `p \<noteq> 0` and `q \<noteq> 0` by simp
   1.684 +  finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
   1.685 +  thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
   1.686 +qed
   1.687 +
   1.688 +lemma degree_mult_eq:
   1.689 +  fixes p q :: "'a::idom poly"
   1.690 +  shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
   1.691 +apply (rule order_antisym [OF degree_mult_le le_degree])
   1.692 +apply (simp add: coeff_mult_degree_sum)
   1.693 +done
   1.694 +
   1.695 +lemma dvd_imp_degree_le:
   1.696 +  fixes p q :: "'a::idom poly"
   1.697 +  shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
   1.698 +  by (erule dvdE, simp add: degree_mult_eq)
   1.699 +
   1.700 +
   1.701 +subsection {* Polynomials form an ordered integral domain *}
   1.702 +
   1.703 +definition
   1.704 +  pos_poly :: "'a::ordered_idom poly \<Rightarrow> bool"
   1.705 +where
   1.706 +  "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
   1.707 +
   1.708 +lemma pos_poly_pCons:
   1.709 +  "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
   1.710 +  unfolding pos_poly_def by simp
   1.711 +
   1.712 +lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
   1.713 +  unfolding pos_poly_def by simp
   1.714 +
   1.715 +lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
   1.716 +  apply (induct p arbitrary: q, simp)
   1.717 +  apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
   1.718 +  done
   1.719 +
   1.720 +lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
   1.721 +  unfolding pos_poly_def
   1.722 +  apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
   1.723 +  apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
   1.724 +  apply auto
   1.725 +  done
   1.726 +
   1.727 +lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
   1.728 +by (induct p) (auto simp add: pos_poly_pCons)
   1.729 +
   1.730 +instantiation poly :: (ordered_idom) ordered_idom
   1.731 +begin
   1.732 +
   1.733 +definition
   1.734 +  [code del]:
   1.735 +    "x < y \<longleftrightarrow> pos_poly (y - x)"
   1.736 +
   1.737 +definition
   1.738 +  [code del]:
   1.739 +    "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
   1.740 +
   1.741 +definition
   1.742 +  [code del]:
   1.743 +    "abs (x::'a poly) = (if x < 0 then - x else x)"
   1.744 +
   1.745 +definition
   1.746 +  [code del]:
   1.747 +    "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
   1.748 +
   1.749 +instance proof
   1.750 +  fix x y :: "'a poly"
   1.751 +  show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   1.752 +    unfolding less_eq_poly_def less_poly_def
   1.753 +    apply safe
   1.754 +    apply simp
   1.755 +    apply (drule (1) pos_poly_add)
   1.756 +    apply simp
   1.757 +    done
   1.758 +next
   1.759 +  fix x :: "'a poly" show "x \<le> x"
   1.760 +    unfolding less_eq_poly_def by simp
   1.761 +next
   1.762 +  fix x y z :: "'a poly"
   1.763 +  assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
   1.764 +    unfolding less_eq_poly_def
   1.765 +    apply safe
   1.766 +    apply (drule (1) pos_poly_add)
   1.767 +    apply (simp add: algebra_simps)
   1.768 +    done
   1.769 +next
   1.770 +  fix x y :: "'a poly"
   1.771 +  assume "x \<le> y" and "y \<le> x" thus "x = y"
   1.772 +    unfolding less_eq_poly_def
   1.773 +    apply safe
   1.774 +    apply (drule (1) pos_poly_add)
   1.775 +    apply simp
   1.776 +    done
   1.777 +next
   1.778 +  fix x y z :: "'a poly"
   1.779 +  assume "x \<le> y" thus "z + x \<le> z + y"
   1.780 +    unfolding less_eq_poly_def
   1.781 +    apply safe
   1.782 +    apply (simp add: algebra_simps)
   1.783 +    done
   1.784 +next
   1.785 +  fix x y :: "'a poly"
   1.786 +  show "x \<le> y \<or> y \<le> x"
   1.787 +    unfolding less_eq_poly_def
   1.788 +    using pos_poly_total [of "x - y"]
   1.789 +    by auto
   1.790 +next
   1.791 +  fix x y z :: "'a poly"
   1.792 +  assume "x < y" and "0 < z"
   1.793 +  thus "z * x < z * y"
   1.794 +    unfolding less_poly_def
   1.795 +    by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
   1.796 +next
   1.797 +  fix x :: "'a poly"
   1.798 +  show "\<bar>x\<bar> = (if x < 0 then - x else x)"
   1.799 +    by (rule abs_poly_def)
   1.800 +next
   1.801 +  fix x :: "'a poly"
   1.802 +  show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
   1.803 +    by (rule sgn_poly_def)
   1.804 +qed
   1.805 +
   1.806 +end
   1.807 +
   1.808 +text {* TODO: Simplification rules for comparisons *}
   1.809 +
   1.810 +
   1.811 +subsection {* Long division of polynomials *}
   1.812 +
   1.813 +definition
   1.814 +  pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
   1.815 +where
   1.816 +  [code del]:
   1.817 +  "pdivmod_rel x y q r \<longleftrightarrow>
   1.818 +    x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
   1.819 +
   1.820 +lemma pdivmod_rel_0:
   1.821 +  "pdivmod_rel 0 y 0 0"
   1.822 +  unfolding pdivmod_rel_def by simp
   1.823 +
   1.824 +lemma pdivmod_rel_by_0:
   1.825 +  "pdivmod_rel x 0 0 x"
   1.826 +  unfolding pdivmod_rel_def by simp
   1.827 +
   1.828 +lemma eq_zero_or_degree_less:
   1.829 +  assumes "degree p \<le> n" and "coeff p n = 0"
   1.830 +  shows "p = 0 \<or> degree p < n"
   1.831 +proof (cases n)
   1.832 +  case 0
   1.833 +  with `degree p \<le> n` and `coeff p n = 0`
   1.834 +  have "coeff p (degree p) = 0" by simp
   1.835 +  then have "p = 0" by simp
   1.836 +  then show ?thesis ..
   1.837 +next
   1.838 +  case (Suc m)
   1.839 +  have "\<forall>i>n. coeff p i = 0"
   1.840 +    using `degree p \<le> n` by (simp add: coeff_eq_0)
   1.841 +  then have "\<forall>i\<ge>n. coeff p i = 0"
   1.842 +    using `coeff p n = 0` by (simp add: le_less)
   1.843 +  then have "\<forall>i>m. coeff p i = 0"
   1.844 +    using `n = Suc m` by (simp add: less_eq_Suc_le)
   1.845 +  then have "degree p \<le> m"
   1.846 +    by (rule degree_le)
   1.847 +  then have "degree p < n"
   1.848 +    using `n = Suc m` by (simp add: less_Suc_eq_le)
   1.849 +  then show ?thesis ..
   1.850 +qed
   1.851 +
   1.852 +lemma pdivmod_rel_pCons:
   1.853 +  assumes rel: "pdivmod_rel x y q r"
   1.854 +  assumes y: "y \<noteq> 0"
   1.855 +  assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
   1.856 +  shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
   1.857 +    (is "pdivmod_rel ?x y ?q ?r")
   1.858 +proof -
   1.859 +  have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
   1.860 +    using assms unfolding pdivmod_rel_def by simp_all
   1.861 +
   1.862 +  have 1: "?x = ?q * y + ?r"
   1.863 +    using b x by simp
   1.864 +
   1.865 +  have 2: "?r = 0 \<or> degree ?r < degree y"
   1.866 +  proof (rule eq_zero_or_degree_less)
   1.867 +    show "degree ?r \<le> degree y"
   1.868 +    proof (rule degree_diff_le)
   1.869 +      show "degree (pCons a r) \<le> degree y"
   1.870 +        using r by auto
   1.871 +      show "degree (smult b y) \<le> degree y"
   1.872 +        by (rule degree_smult_le)
   1.873 +    qed
   1.874 +  next
   1.875 +    show "coeff ?r (degree y) = 0"
   1.876 +      using `y \<noteq> 0` unfolding b by simp
   1.877 +  qed
   1.878 +
   1.879 +  from 1 2 show ?thesis
   1.880 +    unfolding pdivmod_rel_def
   1.881 +    using `y \<noteq> 0` by simp
   1.882 +qed
   1.883 +
   1.884 +lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
   1.885 +apply (cases "y = 0")
   1.886 +apply (fast intro!: pdivmod_rel_by_0)
   1.887 +apply (induct x)
   1.888 +apply (fast intro!: pdivmod_rel_0)
   1.889 +apply (fast intro!: pdivmod_rel_pCons)
   1.890 +done
   1.891 +
   1.892 +lemma pdivmod_rel_unique:
   1.893 +  assumes 1: "pdivmod_rel x y q1 r1"
   1.894 +  assumes 2: "pdivmod_rel x y q2 r2"
   1.895 +  shows "q1 = q2 \<and> r1 = r2"
   1.896 +proof (cases "y = 0")
   1.897 +  assume "y = 0" with assms show ?thesis
   1.898 +    by (simp add: pdivmod_rel_def)
   1.899 +next
   1.900 +  assume [simp]: "y \<noteq> 0"
   1.901 +  from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
   1.902 +    unfolding pdivmod_rel_def by simp_all
   1.903 +  from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
   1.904 +    unfolding pdivmod_rel_def by simp_all
   1.905 +  from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
   1.906 +    by (simp add: algebra_simps)
   1.907 +  from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
   1.908 +    by (auto intro: degree_diff_less)
   1.909 +
   1.910 +  show "q1 = q2 \<and> r1 = r2"
   1.911 +  proof (rule ccontr)
   1.912 +    assume "\<not> (q1 = q2 \<and> r1 = r2)"
   1.913 +    with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
   1.914 +    with r3 have "degree (r2 - r1) < degree y" by simp
   1.915 +    also have "degree y \<le> degree (q1 - q2) + degree y" by simp
   1.916 +    also have "\<dots> = degree ((q1 - q2) * y)"
   1.917 +      using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
   1.918 +    also have "\<dots> = degree (r2 - r1)"
   1.919 +      using q3 by simp
   1.920 +    finally have "degree (r2 - r1) < degree (r2 - r1)" .
   1.921 +    then show "False" by simp
   1.922 +  qed
   1.923 +qed
   1.924 +
   1.925 +lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
   1.926 +by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
   1.927 +
   1.928 +lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
   1.929 +by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
   1.930 +
   1.931 +lemmas pdivmod_rel_unique_div =
   1.932 +  pdivmod_rel_unique [THEN conjunct1, standard]
   1.933 +
   1.934 +lemmas pdivmod_rel_unique_mod =
   1.935 +  pdivmod_rel_unique [THEN conjunct2, standard]
   1.936 +
   1.937 +instantiation poly :: (field) ring_div
   1.938 +begin
   1.939 +
   1.940 +definition div_poly where
   1.941 +  [code del]: "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
   1.942 +
   1.943 +definition mod_poly where
   1.944 +  [code del]: "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
   1.945 +
   1.946 +lemma div_poly_eq:
   1.947 +  "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
   1.948 +unfolding div_poly_def
   1.949 +by (fast elim: pdivmod_rel_unique_div)
   1.950 +
   1.951 +lemma mod_poly_eq:
   1.952 +  "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
   1.953 +unfolding mod_poly_def
   1.954 +by (fast elim: pdivmod_rel_unique_mod)
   1.955 +
   1.956 +lemma pdivmod_rel:
   1.957 +  "pdivmod_rel x y (x div y) (x mod y)"
   1.958 +proof -
   1.959 +  from pdivmod_rel_exists
   1.960 +    obtain q r where "pdivmod_rel x y q r" by fast
   1.961 +  thus ?thesis
   1.962 +    by (simp add: div_poly_eq mod_poly_eq)
   1.963 +qed
   1.964 +
   1.965 +instance proof
   1.966 +  fix x y :: "'a poly"
   1.967 +  show "x div y * y + x mod y = x"
   1.968 +    using pdivmod_rel [of x y]
   1.969 +    by (simp add: pdivmod_rel_def)
   1.970 +next
   1.971 +  fix x :: "'a poly"
   1.972 +  have "pdivmod_rel x 0 0 x"
   1.973 +    by (rule pdivmod_rel_by_0)
   1.974 +  thus "x div 0 = 0"
   1.975 +    by (rule div_poly_eq)
   1.976 +next
   1.977 +  fix y :: "'a poly"
   1.978 +  have "pdivmod_rel 0 y 0 0"
   1.979 +    by (rule pdivmod_rel_0)
   1.980 +  thus "0 div y = 0"
   1.981 +    by (rule div_poly_eq)
   1.982 +next
   1.983 +  fix x y z :: "'a poly"
   1.984 +  assume "y \<noteq> 0"
   1.985 +  hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
   1.986 +    using pdivmod_rel [of x y]
   1.987 +    by (simp add: pdivmod_rel_def left_distrib)
   1.988 +  thus "(x + z * y) div y = z + x div y"
   1.989 +    by (rule div_poly_eq)
   1.990 +qed
   1.991 +
   1.992 +end
   1.993 +
   1.994 +lemma degree_mod_less:
   1.995 +  "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
   1.996 +  using pdivmod_rel [of x y]
   1.997 +  unfolding pdivmod_rel_def by simp
   1.998 +
   1.999 +lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
  1.1000 +proof -
  1.1001 +  assume "degree x < degree y"
  1.1002 +  hence "pdivmod_rel x y 0 x"
  1.1003 +    by (simp add: pdivmod_rel_def)
  1.1004 +  thus "x div y = 0" by (rule div_poly_eq)
  1.1005 +qed
  1.1006 +
  1.1007 +lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
  1.1008 +proof -
  1.1009 +  assume "degree x < degree y"
  1.1010 +  hence "pdivmod_rel x y 0 x"
  1.1011 +    by (simp add: pdivmod_rel_def)
  1.1012 +  thus "x mod y = x" by (rule mod_poly_eq)
  1.1013 +qed
  1.1014 +
  1.1015 +lemma pdivmod_rel_smult_left:
  1.1016 +  "pdivmod_rel x y q r
  1.1017 +    \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
  1.1018 +  unfolding pdivmod_rel_def by (simp add: smult_add_right)
  1.1019 +
  1.1020 +lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
  1.1021 +  by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
  1.1022 +
  1.1023 +lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
  1.1024 +  by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
  1.1025 +
  1.1026 +lemma pdivmod_rel_smult_right:
  1.1027 +  "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
  1.1028 +    \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
  1.1029 +  unfolding pdivmod_rel_def by simp
  1.1030 +
  1.1031 +lemma div_smult_right:
  1.1032 +  "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
  1.1033 +  by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
  1.1034 +
  1.1035 +lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
  1.1036 +  by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
  1.1037 +
  1.1038 +lemma pdivmod_rel_mult:
  1.1039 +  "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
  1.1040 +    \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
  1.1041 +apply (cases "z = 0", simp add: pdivmod_rel_def)
  1.1042 +apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
  1.1043 +apply (cases "r = 0")
  1.1044 +apply (cases "r' = 0")
  1.1045 +apply (simp add: pdivmod_rel_def)
  1.1046 +apply (simp add: pdivmod_rel_def ring_simps degree_mult_eq)
  1.1047 +apply (cases "r' = 0")
  1.1048 +apply (simp add: pdivmod_rel_def degree_mult_eq)
  1.1049 +apply (simp add: pdivmod_rel_def ring_simps)
  1.1050 +apply (simp add: degree_mult_eq degree_add_less)
  1.1051 +done
  1.1052 +
  1.1053 +lemma poly_div_mult_right:
  1.1054 +  fixes x y z :: "'a::field poly"
  1.1055 +  shows "x div (y * z) = (x div y) div z"
  1.1056 +  by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
  1.1057 +
  1.1058 +lemma poly_mod_mult_right:
  1.1059 +  fixes x y z :: "'a::field poly"
  1.1060 +  shows "x mod (y * z) = y * (x div y mod z) + x mod y"
  1.1061 +  by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
  1.1062 +
  1.1063 +lemma mod_pCons:
  1.1064 +  fixes a and x
  1.1065 +  assumes y: "y \<noteq> 0"
  1.1066 +  defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
  1.1067 +  shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
  1.1068 +unfolding b
  1.1069 +apply (rule mod_poly_eq)
  1.1070 +apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
  1.1071 +done
  1.1072 +
  1.1073 +
  1.1074 +subsection {* Evaluation of polynomials *}
  1.1075 +
  1.1076 +definition
  1.1077 +  poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where
  1.1078 +  "poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"
  1.1079 +
  1.1080 +lemma poly_0 [simp]: "poly 0 x = 0"
  1.1081 +  unfolding poly_def by (simp add: poly_rec_0)
  1.1082 +
  1.1083 +lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
  1.1084 +  unfolding poly_def by (simp add: poly_rec_pCons)
  1.1085 +
  1.1086 +lemma poly_1 [simp]: "poly 1 x = 1"
  1.1087 +  unfolding one_poly_def by simp
  1.1088 +
  1.1089 +lemma poly_monom:
  1.1090 +  fixes a x :: "'a::{comm_semiring_1,recpower}"
  1.1091 +  shows "poly (monom a n) x = a * x ^ n"
  1.1092 +  by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
  1.1093 +
  1.1094 +lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
  1.1095 +  apply (induct p arbitrary: q, simp)
  1.1096 +  apply (case_tac q, simp, simp add: algebra_simps)
  1.1097 +  done
  1.1098 +
  1.1099 +lemma poly_minus [simp]:
  1.1100 +  fixes x :: "'a::comm_ring"
  1.1101 +  shows "poly (- p) x = - poly p x"
  1.1102 +  by (induct p, simp_all)
  1.1103 +
  1.1104 +lemma poly_diff [simp]:
  1.1105 +  fixes x :: "'a::comm_ring"
  1.1106 +  shows "poly (p - q) x = poly p x - poly q x"
  1.1107 +  by (simp add: diff_minus)
  1.1108 +
  1.1109 +lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
  1.1110 +  by (cases "finite A", induct set: finite, simp_all)
  1.1111 +
  1.1112 +lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
  1.1113 +  by (induct p, simp, simp add: algebra_simps)
  1.1114 +
  1.1115 +lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
  1.1116 +  by (induct p, simp_all, simp add: algebra_simps)
  1.1117 +
  1.1118 +lemma poly_power [simp]:
  1.1119 +  fixes p :: "'a::{comm_semiring_1,recpower} poly"
  1.1120 +  shows "poly (p ^ n) x = poly p x ^ n"
  1.1121 +  by (induct n, simp, simp add: power_Suc)
  1.1122 +
  1.1123 +
  1.1124 +subsection {* Synthetic division *}
  1.1125 +
  1.1126 +text {*
  1.1127 +  Synthetic division is simply division by the
  1.1128 +  linear polynomial @{term "x - c"}.
  1.1129 +*}
  1.1130 +
  1.1131 +definition
  1.1132 +  synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
  1.1133 +where [code del]:
  1.1134 +  "synthetic_divmod p c =
  1.1135 +    poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"
  1.1136 +
  1.1137 +definition
  1.1138 +  synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
  1.1139 +where
  1.1140 +  "synthetic_div p c = fst (synthetic_divmod p c)"
  1.1141 +
  1.1142 +lemma synthetic_divmod_0 [simp]:
  1.1143 +  "synthetic_divmod 0 c = (0, 0)"
  1.1144 +  unfolding synthetic_divmod_def
  1.1145 +  by (simp add: poly_rec_0)
  1.1146 +
  1.1147 +lemma synthetic_divmod_pCons [simp]:
  1.1148 +  "synthetic_divmod (pCons a p) c =
  1.1149 +    (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
  1.1150 +  unfolding synthetic_divmod_def
  1.1151 +  by (simp add: poly_rec_pCons)
  1.1152 +
  1.1153 +lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
  1.1154 +  by (induct p, simp, simp add: split_def)
  1.1155 +
  1.1156 +lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
  1.1157 +  unfolding synthetic_div_def by simp
  1.1158 +
  1.1159 +lemma synthetic_div_pCons [simp]:
  1.1160 +  "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
  1.1161 +  unfolding synthetic_div_def
  1.1162 +  by (simp add: split_def snd_synthetic_divmod)
  1.1163 +
  1.1164 +lemma synthetic_div_eq_0_iff:
  1.1165 +  "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
  1.1166 +  by (induct p, simp, case_tac p, simp)
  1.1167 +
  1.1168 +lemma degree_synthetic_div:
  1.1169 +  "degree (synthetic_div p c) = degree p - 1"
  1.1170 +  by (induct p, simp, simp add: synthetic_div_eq_0_iff)
  1.1171 +
  1.1172 +lemma synthetic_div_correct:
  1.1173 +  "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
  1.1174 +  by (induct p) simp_all
  1.1175 +
  1.1176 +lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
  1.1177 +by (induct p arbitrary: a) simp_all
  1.1178 +
  1.1179 +lemma synthetic_div_unique:
  1.1180 +  "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
  1.1181 +apply (induct p arbitrary: q r)
  1.1182 +apply (simp, frule synthetic_div_unique_lemma, simp)
  1.1183 +apply (case_tac q, force)
  1.1184 +done
  1.1185 +
  1.1186 +lemma synthetic_div_correct':
  1.1187 +  fixes c :: "'a::comm_ring_1"
  1.1188 +  shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
  1.1189 +  using synthetic_div_correct [of p c]
  1.1190 +  by (simp add: algebra_simps)
  1.1191 +
  1.1192 +lemma poly_eq_0_iff_dvd:
  1.1193 +  fixes c :: "'a::idom"
  1.1194 +  shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
  1.1195 +proof
  1.1196 +  assume "poly p c = 0"
  1.1197 +  with synthetic_div_correct' [of c p]
  1.1198 +  have "p = [:-c, 1:] * synthetic_div p c" by simp
  1.1199 +  then show "[:-c, 1:] dvd p" ..
  1.1200 +next
  1.1201 +  assume "[:-c, 1:] dvd p"
  1.1202 +  then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
  1.1203 +  then show "poly p c = 0" by simp
  1.1204 +qed
  1.1205 +
  1.1206 +lemma dvd_iff_poly_eq_0:
  1.1207 +  fixes c :: "'a::idom"
  1.1208 +  shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
  1.1209 +  by (simp add: poly_eq_0_iff_dvd)
  1.1210 +
  1.1211 +lemma poly_roots_finite:
  1.1212 +  fixes p :: "'a::idom poly"
  1.1213 +  shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
  1.1214 +proof (induct n \<equiv> "degree p" arbitrary: p)
  1.1215 +  case (0 p)
  1.1216 +  then obtain a where "a \<noteq> 0" and "p = [:a:]"
  1.1217 +    by (cases p, simp split: if_splits)
  1.1218 +  then show "finite {x. poly p x = 0}" by simp
  1.1219 +next
  1.1220 +  case (Suc n p)
  1.1221 +  show "finite {x. poly p x = 0}"
  1.1222 +  proof (cases "\<exists>x. poly p x = 0")
  1.1223 +    case False
  1.1224 +    then show "finite {x. poly p x = 0}" by simp
  1.1225 +  next
  1.1226 +    case True
  1.1227 +    then obtain a where "poly p a = 0" ..
  1.1228 +    then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
  1.1229 +    then obtain k where k: "p = [:-a, 1:] * k" ..
  1.1230 +    with `p \<noteq> 0` have "k \<noteq> 0" by auto
  1.1231 +    with k have "degree p = Suc (degree k)"
  1.1232 +      by (simp add: degree_mult_eq del: mult_pCons_left)
  1.1233 +    with `Suc n = degree p` have "n = degree k" by simp
  1.1234 +    with `k \<noteq> 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps)
  1.1235 +    then have "finite (insert a {x. poly k x = 0})" by simp
  1.1236 +    then show "finite {x. poly p x = 0}"
  1.1237 +      by (simp add: k uminus_add_conv_diff Collect_disj_eq
  1.1238 +               del: mult_pCons_left)
  1.1239 +  qed
  1.1240 +qed
  1.1241 +
  1.1242 +lemma poly_zero:
  1.1243 +  fixes p :: "'a::{idom,ring_char_0} poly"
  1.1244 +  shows "poly p = poly 0 \<longleftrightarrow> p = 0"
  1.1245 +apply (cases "p = 0", simp_all)
  1.1246 +apply (drule poly_roots_finite)
  1.1247 +apply (auto simp add: infinite_UNIV_char_0)
  1.1248 +done
  1.1249 +
  1.1250 +lemma poly_eq_iff:
  1.1251 +  fixes p q :: "'a::{idom,ring_char_0} poly"
  1.1252 +  shows "poly p = poly q \<longleftrightarrow> p = q"
  1.1253 +  using poly_zero [of "p - q"]
  1.1254 +  by (simp add: expand_fun_eq)
  1.1255 +
  1.1256 +
  1.1257 +subsection {* Composition of polynomials *}
  1.1258 +
  1.1259 +definition
  1.1260 +  pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
  1.1261 +where
  1.1262 +  "pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"
  1.1263 +
  1.1264 +lemma pcompose_0 [simp]: "pcompose 0 q = 0"
  1.1265 +  unfolding pcompose_def by (simp add: poly_rec_0)
  1.1266 +
  1.1267 +lemma pcompose_pCons:
  1.1268 +  "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
  1.1269 +  unfolding pcompose_def by (simp add: poly_rec_pCons)
  1.1270 +
  1.1271 +lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
  1.1272 +  by (induct p) (simp_all add: pcompose_pCons)
  1.1273 +
  1.1274 +lemma degree_pcompose_le:
  1.1275 +  "degree (pcompose p q) \<le> degree p * degree q"
  1.1276 +apply (induct p, simp)
  1.1277 +apply (simp add: pcompose_pCons, clarify)
  1.1278 +apply (rule degree_add_le, simp)
  1.1279 +apply (rule order_trans [OF degree_mult_le], simp)
  1.1280 +done
  1.1281 +
  1.1282 +
  1.1283 +subsection {* Order of polynomial roots *}
  1.1284 +
  1.1285 +definition
  1.1286 +  order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
  1.1287 +where
  1.1288 +  [code del]:
  1.1289 +  "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
  1.1290 +
  1.1291 +lemma coeff_linear_power:
  1.1292 +  fixes a :: "'a::comm_semiring_1"
  1.1293 +  shows "coeff ([:a, 1:] ^ n) n = 1"
  1.1294 +apply (induct n, simp_all)
  1.1295 +apply (subst coeff_eq_0)
  1.1296 +apply (auto intro: le_less_trans degree_power_le)
  1.1297 +done
  1.1298 +
  1.1299 +lemma degree_linear_power:
  1.1300 +  fixes a :: "'a::comm_semiring_1"
  1.1301 +  shows "degree ([:a, 1:] ^ n) = n"
  1.1302 +apply (rule order_antisym)
  1.1303 +apply (rule ord_le_eq_trans [OF degree_power_le], simp)
  1.1304 +apply (rule le_degree, simp add: coeff_linear_power)
  1.1305 +done
  1.1306 +
  1.1307 +lemma order_1: "[:-a, 1:] ^ order a p dvd p"
  1.1308 +apply (cases "p = 0", simp)
  1.1309 +apply (cases "order a p", simp)
  1.1310 +apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
  1.1311 +apply (drule not_less_Least, simp)
  1.1312 +apply (fold order_def, simp)
  1.1313 +done
  1.1314 +
  1.1315 +lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
  1.1316 +unfolding order_def
  1.1317 +apply (rule LeastI_ex)
  1.1318 +apply (rule_tac x="degree p" in exI)
  1.1319 +apply (rule notI)
  1.1320 +apply (drule (1) dvd_imp_degree_le)
  1.1321 +apply (simp only: degree_linear_power)
  1.1322 +done
  1.1323 +
  1.1324 +lemma order:
  1.1325 +  "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
  1.1326 +by (rule conjI [OF order_1 order_2])
  1.1327 +
  1.1328 +lemma order_degree:
  1.1329 +  assumes p: "p \<noteq> 0"
  1.1330 +  shows "order a p \<le> degree p"
  1.1331 +proof -
  1.1332 +  have "order a p = degree ([:-a, 1:] ^ order a p)"
  1.1333 +    by (simp only: degree_linear_power)
  1.1334 +  also have "\<dots> \<le> degree p"
  1.1335 +    using order_1 p by (rule dvd_imp_degree_le)
  1.1336 +  finally show ?thesis .
  1.1337 +qed
  1.1338 +
  1.1339 +lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
  1.1340 +apply (cases "p = 0", simp_all)
  1.1341 +apply (rule iffI)
  1.1342 +apply (rule ccontr, simp)
  1.1343 +apply (frule order_2 [where a=a], simp)
  1.1344 +apply (simp add: poly_eq_0_iff_dvd)
  1.1345 +apply (simp add: poly_eq_0_iff_dvd)
  1.1346 +apply (simp only: order_def)
  1.1347 +apply (drule not_less_Least, simp)
  1.1348 +done
  1.1349 +
  1.1350 +
  1.1351 +subsection {* Configuration of the code generator *}
  1.1352 +
  1.1353 +code_datatype "0::'a::zero poly" pCons
  1.1354 +
  1.1355 +declare pCons_0_0 [code post]
  1.1356 +
  1.1357 +instantiation poly :: ("{zero,eq}") eq
  1.1358 +begin
  1.1359 +
  1.1360 +definition [code del]:
  1.1361 +  "eq_class.eq (p::'a poly) q \<longleftrightarrow> p = q"
  1.1362 +
  1.1363 +instance
  1.1364 +  by default (rule eq_poly_def)
  1.1365 +
  1.1366 +end
  1.1367 +
  1.1368 +lemma eq_poly_code [code]:
  1.1369 +  "eq_class.eq (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
  1.1370 +  "eq_class.eq (0::_ poly) (pCons b q) \<longleftrightarrow> eq_class.eq 0 b \<and> eq_class.eq 0 q"
  1.1371 +  "eq_class.eq (pCons a p) (0::_ poly) \<longleftrightarrow> eq_class.eq a 0 \<and> eq_class.eq p 0"
  1.1372 +  "eq_class.eq (pCons a p) (pCons b q) \<longleftrightarrow> eq_class.eq a b \<and> eq_class.eq p q"
  1.1373 +unfolding eq by simp_all
  1.1374 +
  1.1375 +lemmas coeff_code [code] =
  1.1376 +  coeff_0 coeff_pCons_0 coeff_pCons_Suc
  1.1377 +
  1.1378 +lemmas degree_code [code] =
  1.1379 +  degree_0 degree_pCons_eq_if
  1.1380 +
  1.1381 +lemmas monom_poly_code [code] =
  1.1382 +  monom_0 monom_Suc
  1.1383 +
  1.1384 +lemma add_poly_code [code]:
  1.1385 +  "0 + q = (q :: _ poly)"
  1.1386 +  "p + 0 = (p :: _ poly)"
  1.1387 +  "pCons a p + pCons b q = pCons (a + b) (p + q)"
  1.1388 +by simp_all
  1.1389 +
  1.1390 +lemma minus_poly_code [code]:
  1.1391 +  "- 0 = (0 :: _ poly)"
  1.1392 +  "- pCons a p = pCons (- a) (- p)"
  1.1393 +by simp_all
  1.1394 +
  1.1395 +lemma diff_poly_code [code]:
  1.1396 +  "0 - q = (- q :: _ poly)"
  1.1397 +  "p - 0 = (p :: _ poly)"
  1.1398 +  "pCons a p - pCons b q = pCons (a - b) (p - q)"
  1.1399 +by simp_all
  1.1400 +
  1.1401 +lemmas smult_poly_code [code] =
  1.1402 +  smult_0_right smult_pCons
  1.1403 +
  1.1404 +lemma mult_poly_code [code]:
  1.1405 +  "0 * q = (0 :: _ poly)"
  1.1406 +  "pCons a p * q = smult a q + pCons 0 (p * q)"
  1.1407 +by simp_all
  1.1408 +
  1.1409 +lemmas poly_code [code] =
  1.1410 +  poly_0 poly_pCons
  1.1411 +
  1.1412 +lemmas synthetic_divmod_code [code] =
  1.1413 +  synthetic_divmod_0 synthetic_divmod_pCons
  1.1414 +
  1.1415 +text {* code generator setup for div and mod *}
  1.1416 +
  1.1417 +definition
  1.1418 +  pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
  1.1419 +where
  1.1420 +  [code del]: "pdivmod x y = (x div y, x mod y)"
  1.1421 +
  1.1422 +lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
  1.1423 +  unfolding pdivmod_def by simp
  1.1424 +
  1.1425 +lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
  1.1426 +  unfolding pdivmod_def by simp
  1.1427 +
  1.1428 +lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
  1.1429 +  unfolding pdivmod_def by simp
  1.1430 +
  1.1431 +lemma pdivmod_pCons [code]:
  1.1432 +  "pdivmod (pCons a x) y =
  1.1433 +    (if y = 0 then (0, pCons a x) else
  1.1434 +      (let (q, r) = pdivmod x y;
  1.1435 +           b = coeff (pCons a r) (degree y) / coeff y (degree y)
  1.1436 +        in (pCons b q, pCons a r - smult b y)))"
  1.1437 +apply (simp add: pdivmod_def Let_def, safe)
  1.1438 +apply (rule div_poly_eq)
  1.1439 +apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
  1.1440 +apply (rule mod_poly_eq)
  1.1441 +apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
  1.1442 +done
  1.1443 +
  1.1444 +end