src/HOL/Library/Polynomial.thy
 author bulwahn Tue Dec 20 17:40:18 2011 +0100 (2011-12-20) changeset 45928 874845660119 parent 45694 4a8743618257 child 46031 ac6bae9fdc2f permissions -rw-r--r--
adding quickcheck generators in some HOL-Library theories
```     1 (*  Title:      HOL/Library/Polynomial.thy
```
```     2     Author:     Brian Huffman
```
```     3     Author:     Clemens Ballarin
```
```     4 *)
```
```     5
```
```     6 header {* Univariate Polynomials *}
```
```     7
```
```     8 theory Polynomial
```
```     9 imports Main
```
```    10 begin
```
```    11
```
```    12 subsection {* Definition of type @{text poly} *}
```
```    13
```
```    14 definition "Poly = {f::nat \<Rightarrow> 'a::zero. \<exists>n. \<forall>i>n. f i = 0}"
```
```    15
```
```    16 typedef (open) 'a poly = "Poly :: (nat => 'a::zero) set"
```
```    17   morphisms coeff Abs_poly
```
```    18   unfolding Poly_def by auto
```
```    19
```
```    20 lemma expand_poly_eq: "p = q \<longleftrightarrow> (\<forall>n. coeff p n = coeff q n)"
```
```    21   by (simp add: coeff_inject [symmetric] fun_eq_iff)
```
```    22
```
```    23 lemma poly_ext: "(\<And>n. coeff p n = coeff q n) \<Longrightarrow> p = q"
```
```    24   by (simp add: expand_poly_eq)
```
```    25
```
```    26
```
```    27 subsection {* Degree of a polynomial *}
```
```    28
```
```    29 definition
```
```    30   degree :: "'a::zero poly \<Rightarrow> nat" where
```
```    31   "degree p = (LEAST n. \<forall>i>n. coeff p i = 0)"
```
```    32
```
```    33 lemma coeff_eq_0: "degree p < n \<Longrightarrow> coeff p n = 0"
```
```    34 proof -
```
```    35   have "coeff p \<in> Poly"
```
```    36     by (rule coeff)
```
```    37   hence "\<exists>n. \<forall>i>n. coeff p i = 0"
```
```    38     unfolding Poly_def by simp
```
```    39   hence "\<forall>i>degree p. coeff p i = 0"
```
```    40     unfolding degree_def by (rule LeastI_ex)
```
```    41   moreover assume "degree p < n"
```
```    42   ultimately show ?thesis by simp
```
```    43 qed
```
```    44
```
```    45 lemma le_degree: "coeff p n \<noteq> 0 \<Longrightarrow> n \<le> degree p"
```
```    46   by (erule contrapos_np, rule coeff_eq_0, simp)
```
```    47
```
```    48 lemma degree_le: "\<forall>i>n. coeff p i = 0 \<Longrightarrow> degree p \<le> n"
```
```    49   unfolding degree_def by (erule Least_le)
```
```    50
```
```    51 lemma less_degree_imp: "n < degree p \<Longrightarrow> \<exists>i>n. coeff p i \<noteq> 0"
```
```    52   unfolding degree_def by (drule not_less_Least, simp)
```
```    53
```
```    54
```
```    55 subsection {* The zero polynomial *}
```
```    56
```
```    57 instantiation poly :: (zero) zero
```
```    58 begin
```
```    59
```
```    60 definition
```
```    61   zero_poly_def: "0 = Abs_poly (\<lambda>n. 0)"
```
```    62
```
```    63 instance ..
```
```    64 end
```
```    65
```
```    66 lemma coeff_0 [simp]: "coeff 0 n = 0"
```
```    67   unfolding zero_poly_def
```
```    68   by (simp add: Abs_poly_inverse Poly_def)
```
```    69
```
```    70 lemma degree_0 [simp]: "degree 0 = 0"
```
```    71   by (rule order_antisym [OF degree_le le0]) simp
```
```    72
```
```    73 lemma leading_coeff_neq_0:
```
```    74   assumes "p \<noteq> 0" shows "coeff p (degree p) \<noteq> 0"
```
```    75 proof (cases "degree p")
```
```    76   case 0
```
```    77   from `p \<noteq> 0` have "\<exists>n. coeff p n \<noteq> 0"
```
```    78     by (simp add: expand_poly_eq)
```
```    79   then obtain n where "coeff p n \<noteq> 0" ..
```
```    80   hence "n \<le> degree p" by (rule le_degree)
```
```    81   with `coeff p n \<noteq> 0` and `degree p = 0`
```
```    82   show "coeff p (degree p) \<noteq> 0" by simp
```
```    83 next
```
```    84   case (Suc n)
```
```    85   from `degree p = Suc n` have "n < degree p" by simp
```
```    86   hence "\<exists>i>n. coeff p i \<noteq> 0" by (rule less_degree_imp)
```
```    87   then obtain i where "n < i" and "coeff p i \<noteq> 0" by fast
```
```    88   from `degree p = Suc n` and `n < i` have "degree p \<le> i" by simp
```
```    89   also from `coeff p i \<noteq> 0` have "i \<le> degree p" by (rule le_degree)
```
```    90   finally have "degree p = i" .
```
```    91   with `coeff p i \<noteq> 0` show "coeff p (degree p) \<noteq> 0" by simp
```
```    92 qed
```
```    93
```
```    94 lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \<longleftrightarrow> p = 0"
```
```    95   by (cases "p = 0", simp, simp add: leading_coeff_neq_0)
```
```    96
```
```    97
```
```    98 subsection {* List-style constructor for polynomials *}
```
```    99
```
```   100 definition
```
```   101   pCons :: "'a::zero \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
```
```   102 where
```
```   103   "pCons a p = Abs_poly (nat_case a (coeff p))"
```
```   104
```
```   105 syntax
```
```   106   "_poly" :: "args \<Rightarrow> 'a poly"  ("[:(_):]")
```
```   107
```
```   108 translations
```
```   109   "[:x, xs:]" == "CONST pCons x [:xs:]"
```
```   110   "[:x:]" == "CONST pCons x 0"
```
```   111   "[:x:]" <= "CONST pCons x (_constrain 0 t)"
```
```   112
```
```   113 lemma Poly_nat_case: "f \<in> Poly \<Longrightarrow> nat_case a f \<in> Poly"
```
```   114   unfolding Poly_def by (auto split: nat.split)
```
```   115
```
```   116 lemma coeff_pCons:
```
```   117   "coeff (pCons a p) = nat_case a (coeff p)"
```
```   118   unfolding pCons_def
```
```   119   by (simp add: Abs_poly_inverse Poly_nat_case coeff)
```
```   120
```
```   121 lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a"
```
```   122   by (simp add: coeff_pCons)
```
```   123
```
```   124 lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n"
```
```   125   by (simp add: coeff_pCons)
```
```   126
```
```   127 lemma degree_pCons_le: "degree (pCons a p) \<le> Suc (degree p)"
```
```   128 by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split)
```
```   129
```
```   130 lemma degree_pCons_eq:
```
```   131   "p \<noteq> 0 \<Longrightarrow> degree (pCons a p) = Suc (degree p)"
```
```   132 apply (rule order_antisym [OF degree_pCons_le])
```
```   133 apply (rule le_degree, simp)
```
```   134 done
```
```   135
```
```   136 lemma degree_pCons_0: "degree (pCons a 0) = 0"
```
```   137 apply (rule order_antisym [OF _ le0])
```
```   138 apply (rule degree_le, simp add: coeff_pCons split: nat.split)
```
```   139 done
```
```   140
```
```   141 lemma degree_pCons_eq_if [simp]:
```
```   142   "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))"
```
```   143 apply (cases "p = 0", simp_all)
```
```   144 apply (rule order_antisym [OF _ le0])
```
```   145 apply (rule degree_le, simp add: coeff_pCons split: nat.split)
```
```   146 apply (rule order_antisym [OF degree_pCons_le])
```
```   147 apply (rule le_degree, simp)
```
```   148 done
```
```   149
```
```   150 lemma pCons_0_0 [simp]: "pCons 0 0 = 0"
```
```   151 by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   152
```
```   153 lemma pCons_eq_iff [simp]:
```
```   154   "pCons a p = pCons b q \<longleftrightarrow> a = b \<and> p = q"
```
```   155 proof (safe)
```
```   156   assume "pCons a p = pCons b q"
```
```   157   then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp
```
```   158   then show "a = b" by simp
```
```   159 next
```
```   160   assume "pCons a p = pCons b q"
```
```   161   then have "\<forall>n. coeff (pCons a p) (Suc n) =
```
```   162                  coeff (pCons b q) (Suc n)" by simp
```
```   163   then show "p = q" by (simp add: expand_poly_eq)
```
```   164 qed
```
```   165
```
```   166 lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \<longleftrightarrow> a = 0 \<and> p = 0"
```
```   167   using pCons_eq_iff [of a p 0 0] by simp
```
```   168
```
```   169 lemma Poly_Suc: "f \<in> Poly \<Longrightarrow> (\<lambda>n. f (Suc n)) \<in> Poly"
```
```   170   unfolding Poly_def
```
```   171   by (clarify, rule_tac x=n in exI, simp)
```
```   172
```
```   173 lemma pCons_cases [cases type: poly]:
```
```   174   obtains (pCons) a q where "p = pCons a q"
```
```   175 proof
```
```   176   show "p = pCons (coeff p 0) (Abs_poly (\<lambda>n. coeff p (Suc n)))"
```
```   177     by (rule poly_ext)
```
```   178        (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons
```
```   179              split: nat.split)
```
```   180 qed
```
```   181
```
```   182 lemma pCons_induct [case_names 0 pCons, induct type: poly]:
```
```   183   assumes zero: "P 0"
```
```   184   assumes pCons: "\<And>a p. P p \<Longrightarrow> P (pCons a p)"
```
```   185   shows "P p"
```
```   186 proof (induct p rule: measure_induct_rule [where f=degree])
```
```   187   case (less p)
```
```   188   obtain a q where "p = pCons a q" by (rule pCons_cases)
```
```   189   have "P q"
```
```   190   proof (cases "q = 0")
```
```   191     case True
```
```   192     then show "P q" by (simp add: zero)
```
```   193   next
```
```   194     case False
```
```   195     then have "degree (pCons a q) = Suc (degree q)"
```
```   196       by (rule degree_pCons_eq)
```
```   197     then have "degree q < degree p"
```
```   198       using `p = pCons a q` by simp
```
```   199     then show "P q"
```
```   200       by (rule less.hyps)
```
```   201   qed
```
```   202   then have "P (pCons a q)"
```
```   203     by (rule pCons)
```
```   204   then show ?case
```
```   205     using `p = pCons a q` by simp
```
```   206 qed
```
```   207
```
```   208
```
```   209 subsection {* Recursion combinator for polynomials *}
```
```   210
```
```   211 function
```
```   212   poly_rec :: "'b \<Rightarrow> ('a::zero \<Rightarrow> 'a poly \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a poly \<Rightarrow> 'b"
```
```   213 where
```
```   214   poly_rec_pCons_eq_if [simp del]:
```
```   215     "poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)"
```
```   216 by (case_tac x, rename_tac q, case_tac q, auto)
```
```   217
```
```   218 termination poly_rec
```
```   219 by (relation "measure (degree \<circ> snd \<circ> snd)", simp)
```
```   220    (simp add: degree_pCons_eq)
```
```   221
```
```   222 lemma poly_rec_0:
```
```   223   "f 0 0 z = z \<Longrightarrow> poly_rec z f 0 = z"
```
```   224   using poly_rec_pCons_eq_if [of z f 0 0] by simp
```
```   225
```
```   226 lemma poly_rec_pCons:
```
```   227   "f 0 0 z = z \<Longrightarrow> poly_rec z f (pCons a p) = f a p (poly_rec z f p)"
```
```   228   by (simp add: poly_rec_pCons_eq_if poly_rec_0)
```
```   229
```
```   230
```
```   231 subsection {* Monomials *}
```
```   232
```
```   233 definition
```
```   234   monom :: "'a \<Rightarrow> nat \<Rightarrow> 'a::zero poly" where
```
```   235   "monom a m = Abs_poly (\<lambda>n. if m = n then a else 0)"
```
```   236
```
```   237 lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)"
```
```   238   unfolding monom_def
```
```   239   by (subst Abs_poly_inverse, auto simp add: Poly_def)
```
```   240
```
```   241 lemma monom_0: "monom a 0 = pCons a 0"
```
```   242   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   243
```
```   244 lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)"
```
```   245   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   246
```
```   247 lemma monom_eq_0 [simp]: "monom 0 n = 0"
```
```   248   by (rule poly_ext) simp
```
```   249
```
```   250 lemma monom_eq_0_iff [simp]: "monom a n = 0 \<longleftrightarrow> a = 0"
```
```   251   by (simp add: expand_poly_eq)
```
```   252
```
```   253 lemma monom_eq_iff [simp]: "monom a n = monom b n \<longleftrightarrow> a = b"
```
```   254   by (simp add: expand_poly_eq)
```
```   255
```
```   256 lemma degree_monom_le: "degree (monom a n) \<le> n"
```
```   257   by (rule degree_le, simp)
```
```   258
```
```   259 lemma degree_monom_eq: "a \<noteq> 0 \<Longrightarrow> degree (monom a n) = n"
```
```   260   apply (rule order_antisym [OF degree_monom_le])
```
```   261   apply (rule le_degree, simp)
```
```   262   done
```
```   263
```
```   264
```
```   265 subsection {* Addition and subtraction *}
```
```   266
```
```   267 instantiation poly :: (comm_monoid_add) comm_monoid_add
```
```   268 begin
```
```   269
```
```   270 definition
```
```   271   plus_poly_def:
```
```   272     "p + q = Abs_poly (\<lambda>n. coeff p n + coeff q n)"
```
```   273
```
```   274 lemma Poly_add:
```
```   275   fixes f g :: "nat \<Rightarrow> 'a"
```
```   276   shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n + g n) \<in> Poly"
```
```   277   unfolding Poly_def
```
```   278   apply (clarify, rename_tac m n)
```
```   279   apply (rule_tac x="max m n" in exI, simp)
```
```   280   done
```
```   281
```
```   282 lemma coeff_add [simp]:
```
```   283   "coeff (p + q) n = coeff p n + coeff q n"
```
```   284   unfolding plus_poly_def
```
```   285   by (simp add: Abs_poly_inverse coeff Poly_add)
```
```   286
```
```   287 instance proof
```
```   288   fix p q r :: "'a poly"
```
```   289   show "(p + q) + r = p + (q + r)"
```
```   290     by (simp add: expand_poly_eq add_assoc)
```
```   291   show "p + q = q + p"
```
```   292     by (simp add: expand_poly_eq add_commute)
```
```   293   show "0 + p = p"
```
```   294     by (simp add: expand_poly_eq)
```
```   295 qed
```
```   296
```
```   297 end
```
```   298
```
```   299 instance poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add
```
```   300 proof
```
```   301   fix p q r :: "'a poly"
```
```   302   assume "p + q = p + r" thus "q = r"
```
```   303     by (simp add: expand_poly_eq)
```
```   304 qed
```
```   305
```
```   306 instantiation poly :: (ab_group_add) ab_group_add
```
```   307 begin
```
```   308
```
```   309 definition
```
```   310   uminus_poly_def:
```
```   311     "- p = Abs_poly (\<lambda>n. - coeff p n)"
```
```   312
```
```   313 definition
```
```   314   minus_poly_def:
```
```   315     "p - q = Abs_poly (\<lambda>n. coeff p n - coeff q n)"
```
```   316
```
```   317 lemma Poly_minus:
```
```   318   fixes f :: "nat \<Rightarrow> 'a"
```
```   319   shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. - f n) \<in> Poly"
```
```   320   unfolding Poly_def by simp
```
```   321
```
```   322 lemma Poly_diff:
```
```   323   fixes f g :: "nat \<Rightarrow> 'a"
```
```   324   shows "\<lbrakk>f \<in> Poly; g \<in> Poly\<rbrakk> \<Longrightarrow> (\<lambda>n. f n - g n) \<in> Poly"
```
```   325   unfolding diff_minus by (simp add: Poly_add Poly_minus)
```
```   326
```
```   327 lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n"
```
```   328   unfolding uminus_poly_def
```
```   329   by (simp add: Abs_poly_inverse coeff Poly_minus)
```
```   330
```
```   331 lemma coeff_diff [simp]:
```
```   332   "coeff (p - q) n = coeff p n - coeff q n"
```
```   333   unfolding minus_poly_def
```
```   334   by (simp add: Abs_poly_inverse coeff Poly_diff)
```
```   335
```
```   336 instance proof
```
```   337   fix p q :: "'a poly"
```
```   338   show "- p + p = 0"
```
```   339     by (simp add: expand_poly_eq)
```
```   340   show "p - q = p + - q"
```
```   341     by (simp add: expand_poly_eq diff_minus)
```
```   342 qed
```
```   343
```
```   344 end
```
```   345
```
```   346 lemma add_pCons [simp]:
```
```   347   "pCons a p + pCons b q = pCons (a + b) (p + q)"
```
```   348   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   349
```
```   350 lemma minus_pCons [simp]:
```
```   351   "- pCons a p = pCons (- a) (- p)"
```
```   352   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   353
```
```   354 lemma diff_pCons [simp]:
```
```   355   "pCons a p - pCons b q = pCons (a - b) (p - q)"
```
```   356   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   357
```
```   358 lemma degree_add_le_max: "degree (p + q) \<le> max (degree p) (degree q)"
```
```   359   by (rule degree_le, auto simp add: coeff_eq_0)
```
```   360
```
```   361 lemma degree_add_le:
```
```   362   "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p + q) \<le> n"
```
```   363   by (auto intro: order_trans degree_add_le_max)
```
```   364
```
```   365 lemma degree_add_less:
```
```   366   "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p + q) < n"
```
```   367   by (auto intro: le_less_trans degree_add_le_max)
```
```   368
```
```   369 lemma degree_add_eq_right:
```
```   370   "degree p < degree q \<Longrightarrow> degree (p + q) = degree q"
```
```   371   apply (cases "q = 0", simp)
```
```   372   apply (rule order_antisym)
```
```   373   apply (simp add: degree_add_le)
```
```   374   apply (rule le_degree)
```
```   375   apply (simp add: coeff_eq_0)
```
```   376   done
```
```   377
```
```   378 lemma degree_add_eq_left:
```
```   379   "degree q < degree p \<Longrightarrow> degree (p + q) = degree p"
```
```   380   using degree_add_eq_right [of q p]
```
```   381   by (simp add: add_commute)
```
```   382
```
```   383 lemma degree_minus [simp]: "degree (- p) = degree p"
```
```   384   unfolding degree_def by simp
```
```   385
```
```   386 lemma degree_diff_le_max: "degree (p - q) \<le> max (degree p) (degree q)"
```
```   387   using degree_add_le [where p=p and q="-q"]
```
```   388   by (simp add: diff_minus)
```
```   389
```
```   390 lemma degree_diff_le:
```
```   391   "\<lbrakk>degree p \<le> n; degree q \<le> n\<rbrakk> \<Longrightarrow> degree (p - q) \<le> n"
```
```   392   by (simp add: diff_minus degree_add_le)
```
```   393
```
```   394 lemma degree_diff_less:
```
```   395   "\<lbrakk>degree p < n; degree q < n\<rbrakk> \<Longrightarrow> degree (p - q) < n"
```
```   396   by (simp add: diff_minus degree_add_less)
```
```   397
```
```   398 lemma add_monom: "monom a n + monom b n = monom (a + b) n"
```
```   399   by (rule poly_ext) simp
```
```   400
```
```   401 lemma diff_monom: "monom a n - monom b n = monom (a - b) n"
```
```   402   by (rule poly_ext) simp
```
```   403
```
```   404 lemma minus_monom: "- monom a n = monom (-a) n"
```
```   405   by (rule poly_ext) simp
```
```   406
```
```   407 lemma coeff_setsum: "coeff (\<Sum>x\<in>A. p x) i = (\<Sum>x\<in>A. coeff (p x) i)"
```
```   408   by (cases "finite A", induct set: finite, simp_all)
```
```   409
```
```   410 lemma monom_setsum: "monom (\<Sum>x\<in>A. a x) n = (\<Sum>x\<in>A. monom (a x) n)"
```
```   411   by (rule poly_ext) (simp add: coeff_setsum)
```
```   412
```
```   413
```
```   414 subsection {* Multiplication by a constant *}
```
```   415
```
```   416 definition
```
```   417   smult :: "'a::comm_semiring_0 \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
```
```   418   "smult a p = Abs_poly (\<lambda>n. a * coeff p n)"
```
```   419
```
```   420 lemma Poly_smult:
```
```   421   fixes f :: "nat \<Rightarrow> 'a::comm_semiring_0"
```
```   422   shows "f \<in> Poly \<Longrightarrow> (\<lambda>n. a * f n) \<in> Poly"
```
```   423   unfolding Poly_def
```
```   424   by (clarify, rule_tac x=n in exI, simp)
```
```   425
```
```   426 lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n"
```
```   427   unfolding smult_def
```
```   428   by (simp add: Abs_poly_inverse Poly_smult coeff)
```
```   429
```
```   430 lemma degree_smult_le: "degree (smult a p) \<le> degree p"
```
```   431   by (rule degree_le, simp add: coeff_eq_0)
```
```   432
```
```   433 lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p"
```
```   434   by (rule poly_ext, simp add: mult_assoc)
```
```   435
```
```   436 lemma smult_0_right [simp]: "smult a 0 = 0"
```
```   437   by (rule poly_ext, simp)
```
```   438
```
```   439 lemma smult_0_left [simp]: "smult 0 p = 0"
```
```   440   by (rule poly_ext, simp)
```
```   441
```
```   442 lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p"
```
```   443   by (rule poly_ext, simp)
```
```   444
```
```   445 lemma smult_add_right:
```
```   446   "smult a (p + q) = smult a p + smult a q"
```
```   447   by (rule poly_ext, simp add: algebra_simps)
```
```   448
```
```   449 lemma smult_add_left:
```
```   450   "smult (a + b) p = smult a p + smult b p"
```
```   451   by (rule poly_ext, simp add: algebra_simps)
```
```   452
```
```   453 lemma smult_minus_right [simp]:
```
```   454   "smult (a::'a::comm_ring) (- p) = - smult a p"
```
```   455   by (rule poly_ext, simp)
```
```   456
```
```   457 lemma smult_minus_left [simp]:
```
```   458   "smult (- a::'a::comm_ring) p = - smult a p"
```
```   459   by (rule poly_ext, simp)
```
```   460
```
```   461 lemma smult_diff_right:
```
```   462   "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q"
```
```   463   by (rule poly_ext, simp add: algebra_simps)
```
```   464
```
```   465 lemma smult_diff_left:
```
```   466   "smult (a - b::'a::comm_ring) p = smult a p - smult b p"
```
```   467   by (rule poly_ext, simp add: algebra_simps)
```
```   468
```
```   469 lemmas smult_distribs =
```
```   470   smult_add_left smult_add_right
```
```   471   smult_diff_left smult_diff_right
```
```   472
```
```   473 lemma smult_pCons [simp]:
```
```   474   "smult a (pCons b p) = pCons (a * b) (smult a p)"
```
```   475   by (rule poly_ext, simp add: coeff_pCons split: nat.split)
```
```   476
```
```   477 lemma smult_monom: "smult a (monom b n) = monom (a * b) n"
```
```   478   by (induct n, simp add: monom_0, simp add: monom_Suc)
```
```   479
```
```   480 lemma degree_smult_eq [simp]:
```
```   481   fixes a :: "'a::idom"
```
```   482   shows "degree (smult a p) = (if a = 0 then 0 else degree p)"
```
```   483   by (cases "a = 0", simp, simp add: degree_def)
```
```   484
```
```   485 lemma smult_eq_0_iff [simp]:
```
```   486   fixes a :: "'a::idom"
```
```   487   shows "smult a p = 0 \<longleftrightarrow> a = 0 \<or> p = 0"
```
```   488   by (simp add: expand_poly_eq)
```
```   489
```
```   490
```
```   491 subsection {* Multiplication of polynomials *}
```
```   492
```
```   493 text {* TODO: move to SetInterval.thy *}
```
```   494 lemma setsum_atMost_Suc_shift:
```
```   495   fixes f :: "nat \<Rightarrow> 'a::comm_monoid_add"
```
```   496   shows "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
```
```   497 proof (induct n)
```
```   498   case 0 show ?case by simp
```
```   499 next
```
```   500   case (Suc n) note IH = this
```
```   501   have "(\<Sum>i\<le>Suc (Suc n). f i) = (\<Sum>i\<le>Suc n. f i) + f (Suc (Suc n))"
```
```   502     by (rule setsum_atMost_Suc)
```
```   503   also have "(\<Sum>i\<le>Suc n. f i) = f 0 + (\<Sum>i\<le>n. f (Suc i))"
```
```   504     by (rule IH)
```
```   505   also have "f 0 + (\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) =
```
```   506              f 0 + ((\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)))"
```
```   507     by (rule add_assoc)
```
```   508   also have "(\<Sum>i\<le>n. f (Suc i)) + f (Suc (Suc n)) = (\<Sum>i\<le>Suc n. f (Suc i))"
```
```   509     by (rule setsum_atMost_Suc [symmetric])
```
```   510   finally show ?case .
```
```   511 qed
```
```   512
```
```   513 instantiation poly :: (comm_semiring_0) comm_semiring_0
```
```   514 begin
```
```   515
```
```   516 definition
```
```   517   times_poly_def:
```
```   518     "p * q = poly_rec 0 (\<lambda>a p pq. smult a q + pCons 0 pq) p"
```
```   519
```
```   520 lemma mult_poly_0_left: "(0::'a poly) * q = 0"
```
```   521   unfolding times_poly_def by (simp add: poly_rec_0)
```
```   522
```
```   523 lemma mult_pCons_left [simp]:
```
```   524   "pCons a p * q = smult a q + pCons 0 (p * q)"
```
```   525   unfolding times_poly_def by (simp add: poly_rec_pCons)
```
```   526
```
```   527 lemma mult_poly_0_right: "p * (0::'a poly) = 0"
```
```   528   by (induct p, simp add: mult_poly_0_left, simp)
```
```   529
```
```   530 lemma mult_pCons_right [simp]:
```
```   531   "p * pCons a q = smult a p + pCons 0 (p * q)"
```
```   532   by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps)
```
```   533
```
```   534 lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right
```
```   535
```
```   536 lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)"
```
```   537   by (induct p, simp add: mult_poly_0, simp add: smult_add_right)
```
```   538
```
```   539 lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)"
```
```   540   by (induct q, simp add: mult_poly_0, simp add: smult_add_right)
```
```   541
```
```   542 lemma mult_poly_add_left:
```
```   543   fixes p q r :: "'a poly"
```
```   544   shows "(p + q) * r = p * r + q * r"
```
```   545   by (induct r, simp add: mult_poly_0,
```
```   546                 simp add: smult_distribs algebra_simps)
```
```   547
```
```   548 instance proof
```
```   549   fix p q r :: "'a poly"
```
```   550   show 0: "0 * p = 0"
```
```   551     by (rule mult_poly_0_left)
```
```   552   show "p * 0 = 0"
```
```   553     by (rule mult_poly_0_right)
```
```   554   show "(p + q) * r = p * r + q * r"
```
```   555     by (rule mult_poly_add_left)
```
```   556   show "(p * q) * r = p * (q * r)"
```
```   557     by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left)
```
```   558   show "p * q = q * p"
```
```   559     by (induct p, simp add: mult_poly_0, simp)
```
```   560 qed
```
```   561
```
```   562 end
```
```   563
```
```   564 instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel ..
```
```   565
```
```   566 lemma coeff_mult:
```
```   567   "coeff (p * q) n = (\<Sum>i\<le>n. coeff p i * coeff q (n-i))"
```
```   568 proof (induct p arbitrary: n)
```
```   569   case 0 show ?case by simp
```
```   570 next
```
```   571   case (pCons a p n) thus ?case
```
```   572     by (cases n, simp, simp add: setsum_atMost_Suc_shift
```
```   573                             del: setsum_atMost_Suc)
```
```   574 qed
```
```   575
```
```   576 lemma degree_mult_le: "degree (p * q) \<le> degree p + degree q"
```
```   577 apply (rule degree_le)
```
```   578 apply (induct p)
```
```   579 apply simp
```
```   580 apply (simp add: coeff_eq_0 coeff_pCons split: nat.split)
```
```   581 done
```
```   582
```
```   583 lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)"
```
```   584   by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc)
```
```   585
```
```   586
```
```   587 subsection {* The unit polynomial and exponentiation *}
```
```   588
```
```   589 instantiation poly :: (comm_semiring_1) comm_semiring_1
```
```   590 begin
```
```   591
```
```   592 definition
```
```   593   one_poly_def:
```
```   594     "1 = pCons 1 0"
```
```   595
```
```   596 instance proof
```
```   597   fix p :: "'a poly" show "1 * p = p"
```
```   598     unfolding one_poly_def
```
```   599     by simp
```
```   600 next
```
```   601   show "0 \<noteq> (1::'a poly)"
```
```   602     unfolding one_poly_def by simp
```
```   603 qed
```
```   604
```
```   605 end
```
```   606
```
```   607 instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel ..
```
```   608
```
```   609 lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)"
```
```   610   unfolding one_poly_def
```
```   611   by (simp add: coeff_pCons split: nat.split)
```
```   612
```
```   613 lemma degree_1 [simp]: "degree 1 = 0"
```
```   614   unfolding one_poly_def
```
```   615   by (rule degree_pCons_0)
```
```   616
```
```   617 text {* Lemmas about divisibility *}
```
```   618
```
```   619 lemma dvd_smult: "p dvd q \<Longrightarrow> p dvd smult a q"
```
```   620 proof -
```
```   621   assume "p dvd q"
```
```   622   then obtain k where "q = p * k" ..
```
```   623   then have "smult a q = p * smult a k" by simp
```
```   624   then show "p dvd smult a q" ..
```
```   625 qed
```
```   626
```
```   627 lemma dvd_smult_cancel:
```
```   628   fixes a :: "'a::field"
```
```   629   shows "p dvd smult a q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> p dvd q"
```
```   630   by (drule dvd_smult [where a="inverse a"]) simp
```
```   631
```
```   632 lemma dvd_smult_iff:
```
```   633   fixes a :: "'a::field"
```
```   634   shows "a \<noteq> 0 \<Longrightarrow> p dvd smult a q \<longleftrightarrow> p dvd q"
```
```   635   by (safe elim!: dvd_smult dvd_smult_cancel)
```
```   636
```
```   637 lemma smult_dvd_cancel:
```
```   638   "smult a p dvd q \<Longrightarrow> p dvd q"
```
```   639 proof -
```
```   640   assume "smult a p dvd q"
```
```   641   then obtain k where "q = smult a p * k" ..
```
```   642   then have "q = p * smult a k" by simp
```
```   643   then show "p dvd q" ..
```
```   644 qed
```
```   645
```
```   646 lemma smult_dvd:
```
```   647   fixes a :: "'a::field"
```
```   648   shows "p dvd q \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> smult a p dvd q"
```
```   649   by (rule smult_dvd_cancel [where a="inverse a"]) simp
```
```   650
```
```   651 lemma smult_dvd_iff:
```
```   652   fixes a :: "'a::field"
```
```   653   shows "smult a p dvd q \<longleftrightarrow> (if a = 0 then q = 0 else p dvd q)"
```
```   654   by (auto elim: smult_dvd smult_dvd_cancel)
```
```   655
```
```   656 lemma degree_power_le: "degree (p ^ n) \<le> degree p * n"
```
```   657 by (induct n, simp, auto intro: order_trans degree_mult_le)
```
```   658
```
```   659 instance poly :: (comm_ring) comm_ring ..
```
```   660
```
```   661 instance poly :: (comm_ring_1) comm_ring_1 ..
```
```   662
```
```   663 instantiation poly :: (comm_ring_1) number_ring
```
```   664 begin
```
```   665
```
```   666 definition
```
```   667   "number_of k = (of_int k :: 'a poly)"
```
```   668
```
```   669 instance
```
```   670   by default (rule number_of_poly_def)
```
```   671
```
```   672 end
```
```   673
```
```   674
```
```   675 subsection {* Polynomials form an integral domain *}
```
```   676
```
```   677 lemma coeff_mult_degree_sum:
```
```   678   "coeff (p * q) (degree p + degree q) =
```
```   679    coeff p (degree p) * coeff q (degree q)"
```
```   680   by (induct p, simp, simp add: coeff_eq_0)
```
```   681
```
```   682 instance poly :: (idom) idom
```
```   683 proof
```
```   684   fix p q :: "'a poly"
```
```   685   assume "p \<noteq> 0" and "q \<noteq> 0"
```
```   686   have "coeff (p * q) (degree p + degree q) =
```
```   687         coeff p (degree p) * coeff q (degree q)"
```
```   688     by (rule coeff_mult_degree_sum)
```
```   689   also have "coeff p (degree p) * coeff q (degree q) \<noteq> 0"
```
```   690     using `p \<noteq> 0` and `q \<noteq> 0` by simp
```
```   691   finally have "\<exists>n. coeff (p * q) n \<noteq> 0" ..
```
```   692   thus "p * q \<noteq> 0" by (simp add: expand_poly_eq)
```
```   693 qed
```
```   694
```
```   695 lemma degree_mult_eq:
```
```   696   fixes p q :: "'a::idom poly"
```
```   697   shows "\<lbrakk>p \<noteq> 0; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree (p * q) = degree p + degree q"
```
```   698 apply (rule order_antisym [OF degree_mult_le le_degree])
```
```   699 apply (simp add: coeff_mult_degree_sum)
```
```   700 done
```
```   701
```
```   702 lemma dvd_imp_degree_le:
```
```   703   fixes p q :: "'a::idom poly"
```
```   704   shows "\<lbrakk>p dvd q; q \<noteq> 0\<rbrakk> \<Longrightarrow> degree p \<le> degree q"
```
```   705   by (erule dvdE, simp add: degree_mult_eq)
```
```   706
```
```   707
```
```   708 subsection {* Polynomials form an ordered integral domain *}
```
```   709
```
```   710 definition
```
```   711   pos_poly :: "'a::linordered_idom poly \<Rightarrow> bool"
```
```   712 where
```
```   713   "pos_poly p \<longleftrightarrow> 0 < coeff p (degree p)"
```
```   714
```
```   715 lemma pos_poly_pCons:
```
```   716   "pos_poly (pCons a p) \<longleftrightarrow> pos_poly p \<or> (p = 0 \<and> 0 < a)"
```
```   717   unfolding pos_poly_def by simp
```
```   718
```
```   719 lemma not_pos_poly_0 [simp]: "\<not> pos_poly 0"
```
```   720   unfolding pos_poly_def by simp
```
```   721
```
```   722 lemma pos_poly_add: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p + q)"
```
```   723   apply (induct p arbitrary: q, simp)
```
```   724   apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos)
```
```   725   done
```
```   726
```
```   727 lemma pos_poly_mult: "\<lbrakk>pos_poly p; pos_poly q\<rbrakk> \<Longrightarrow> pos_poly (p * q)"
```
```   728   unfolding pos_poly_def
```
```   729   apply (subgoal_tac "p \<noteq> 0 \<and> q \<noteq> 0")
```
```   730   apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos)
```
```   731   apply auto
```
```   732   done
```
```   733
```
```   734 lemma pos_poly_total: "p = 0 \<or> pos_poly p \<or> pos_poly (- p)"
```
```   735 by (induct p) (auto simp add: pos_poly_pCons)
```
```   736
```
```   737 instantiation poly :: (linordered_idom) linordered_idom
```
```   738 begin
```
```   739
```
```   740 definition
```
```   741   "x < y \<longleftrightarrow> pos_poly (y - x)"
```
```   742
```
```   743 definition
```
```   744   "x \<le> y \<longleftrightarrow> x = y \<or> pos_poly (y - x)"
```
```   745
```
```   746 definition
```
```   747   "abs (x::'a poly) = (if x < 0 then - x else x)"
```
```   748
```
```   749 definition
```
```   750   "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
```
```   751
```
```   752 instance proof
```
```   753   fix x y :: "'a poly"
```
```   754   show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
```
```   755     unfolding less_eq_poly_def less_poly_def
```
```   756     apply safe
```
```   757     apply simp
```
```   758     apply (drule (1) pos_poly_add)
```
```   759     apply simp
```
```   760     done
```
```   761 next
```
```   762   fix x :: "'a poly" show "x \<le> x"
```
```   763     unfolding less_eq_poly_def by simp
```
```   764 next
```
```   765   fix x y z :: "'a poly"
```
```   766   assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
```
```   767     unfolding less_eq_poly_def
```
```   768     apply safe
```
```   769     apply (drule (1) pos_poly_add)
```
```   770     apply (simp add: algebra_simps)
```
```   771     done
```
```   772 next
```
```   773   fix x y :: "'a poly"
```
```   774   assume "x \<le> y" and "y \<le> x" thus "x = y"
```
```   775     unfolding less_eq_poly_def
```
```   776     apply safe
```
```   777     apply (drule (1) pos_poly_add)
```
```   778     apply simp
```
```   779     done
```
```   780 next
```
```   781   fix x y z :: "'a poly"
```
```   782   assume "x \<le> y" thus "z + x \<le> z + y"
```
```   783     unfolding less_eq_poly_def
```
```   784     apply safe
```
```   785     apply (simp add: algebra_simps)
```
```   786     done
```
```   787 next
```
```   788   fix x y :: "'a poly"
```
```   789   show "x \<le> y \<or> y \<le> x"
```
```   790     unfolding less_eq_poly_def
```
```   791     using pos_poly_total [of "x - y"]
```
```   792     by auto
```
```   793 next
```
```   794   fix x y z :: "'a poly"
```
```   795   assume "x < y" and "0 < z"
```
```   796   thus "z * x < z * y"
```
```   797     unfolding less_poly_def
```
```   798     by (simp add: right_diff_distrib [symmetric] pos_poly_mult)
```
```   799 next
```
```   800   fix x :: "'a poly"
```
```   801   show "\<bar>x\<bar> = (if x < 0 then - x else x)"
```
```   802     by (rule abs_poly_def)
```
```   803 next
```
```   804   fix x :: "'a poly"
```
```   805   show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
```
```   806     by (rule sgn_poly_def)
```
```   807 qed
```
```   808
```
```   809 end
```
```   810
```
```   811 text {* TODO: Simplification rules for comparisons *}
```
```   812
```
```   813
```
```   814 subsection {* Long division of polynomials *}
```
```   815
```
```   816 definition
```
```   817   pdivmod_rel :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
```
```   818 where
```
```   819   "pdivmod_rel x y q r \<longleftrightarrow>
```
```   820     x = q * y + r \<and> (if y = 0 then q = 0 else r = 0 \<or> degree r < degree y)"
```
```   821
```
```   822 lemma pdivmod_rel_0:
```
```   823   "pdivmod_rel 0 y 0 0"
```
```   824   unfolding pdivmod_rel_def by simp
```
```   825
```
```   826 lemma pdivmod_rel_by_0:
```
```   827   "pdivmod_rel x 0 0 x"
```
```   828   unfolding pdivmod_rel_def by simp
```
```   829
```
```   830 lemma eq_zero_or_degree_less:
```
```   831   assumes "degree p \<le> n" and "coeff p n = 0"
```
```   832   shows "p = 0 \<or> degree p < n"
```
```   833 proof (cases n)
```
```   834   case 0
```
```   835   with `degree p \<le> n` and `coeff p n = 0`
```
```   836   have "coeff p (degree p) = 0" by simp
```
```   837   then have "p = 0" by simp
```
```   838   then show ?thesis ..
```
```   839 next
```
```   840   case (Suc m)
```
```   841   have "\<forall>i>n. coeff p i = 0"
```
```   842     using `degree p \<le> n` by (simp add: coeff_eq_0)
```
```   843   then have "\<forall>i\<ge>n. coeff p i = 0"
```
```   844     using `coeff p n = 0` by (simp add: le_less)
```
```   845   then have "\<forall>i>m. coeff p i = 0"
```
```   846     using `n = Suc m` by (simp add: less_eq_Suc_le)
```
```   847   then have "degree p \<le> m"
```
```   848     by (rule degree_le)
```
```   849   then have "degree p < n"
```
```   850     using `n = Suc m` by (simp add: less_Suc_eq_le)
```
```   851   then show ?thesis ..
```
```   852 qed
```
```   853
```
```   854 lemma pdivmod_rel_pCons:
```
```   855   assumes rel: "pdivmod_rel x y q r"
```
```   856   assumes y: "y \<noteq> 0"
```
```   857   assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)"
```
```   858   shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)"
```
```   859     (is "pdivmod_rel ?x y ?q ?r")
```
```   860 proof -
```
```   861   have x: "x = q * y + r" and r: "r = 0 \<or> degree r < degree y"
```
```   862     using assms unfolding pdivmod_rel_def by simp_all
```
```   863
```
```   864   have 1: "?x = ?q * y + ?r"
```
```   865     using b x by simp
```
```   866
```
```   867   have 2: "?r = 0 \<or> degree ?r < degree y"
```
```   868   proof (rule eq_zero_or_degree_less)
```
```   869     show "degree ?r \<le> degree y"
```
```   870     proof (rule degree_diff_le)
```
```   871       show "degree (pCons a r) \<le> degree y"
```
```   872         using r by auto
```
```   873       show "degree (smult b y) \<le> degree y"
```
```   874         by (rule degree_smult_le)
```
```   875     qed
```
```   876   next
```
```   877     show "coeff ?r (degree y) = 0"
```
```   878       using `y \<noteq> 0` unfolding b by simp
```
```   879   qed
```
```   880
```
```   881   from 1 2 show ?thesis
```
```   882     unfolding pdivmod_rel_def
```
```   883     using `y \<noteq> 0` by simp
```
```   884 qed
```
```   885
```
```   886 lemma pdivmod_rel_exists: "\<exists>q r. pdivmod_rel x y q r"
```
```   887 apply (cases "y = 0")
```
```   888 apply (fast intro!: pdivmod_rel_by_0)
```
```   889 apply (induct x)
```
```   890 apply (fast intro!: pdivmod_rel_0)
```
```   891 apply (fast intro!: pdivmod_rel_pCons)
```
```   892 done
```
```   893
```
```   894 lemma pdivmod_rel_unique:
```
```   895   assumes 1: "pdivmod_rel x y q1 r1"
```
```   896   assumes 2: "pdivmod_rel x y q2 r2"
```
```   897   shows "q1 = q2 \<and> r1 = r2"
```
```   898 proof (cases "y = 0")
```
```   899   assume "y = 0" with assms show ?thesis
```
```   900     by (simp add: pdivmod_rel_def)
```
```   901 next
```
```   902   assume [simp]: "y \<noteq> 0"
```
```   903   from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \<or> degree r1 < degree y"
```
```   904     unfolding pdivmod_rel_def by simp_all
```
```   905   from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \<or> degree r2 < degree y"
```
```   906     unfolding pdivmod_rel_def by simp_all
```
```   907   from q1 q2 have q3: "(q1 - q2) * y = r2 - r1"
```
```   908     by (simp add: algebra_simps)
```
```   909   from r1 r2 have r3: "(r2 - r1) = 0 \<or> degree (r2 - r1) < degree y"
```
```   910     by (auto intro: degree_diff_less)
```
```   911
```
```   912   show "q1 = q2 \<and> r1 = r2"
```
```   913   proof (rule ccontr)
```
```   914     assume "\<not> (q1 = q2 \<and> r1 = r2)"
```
```   915     with q3 have "q1 \<noteq> q2" and "r1 \<noteq> r2" by auto
```
```   916     with r3 have "degree (r2 - r1) < degree y" by simp
```
```   917     also have "degree y \<le> degree (q1 - q2) + degree y" by simp
```
```   918     also have "\<dots> = degree ((q1 - q2) * y)"
```
```   919       using `q1 \<noteq> q2` by (simp add: degree_mult_eq)
```
```   920     also have "\<dots> = degree (r2 - r1)"
```
```   921       using q3 by simp
```
```   922     finally have "degree (r2 - r1) < degree (r2 - r1)" .
```
```   923     then show "False" by simp
```
```   924   qed
```
```   925 qed
```
```   926
```
```   927 lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \<longleftrightarrow> q = 0 \<and> r = 0"
```
```   928 by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0)
```
```   929
```
```   930 lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \<longleftrightarrow> q = 0 \<and> r = x"
```
```   931 by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0)
```
```   932
```
```   933 lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1]
```
```   934
```
```   935 lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2]
```
```   936
```
```   937 instantiation poly :: (field) ring_div
```
```   938 begin
```
```   939
```
```   940 definition div_poly where
```
```   941   "x div y = (THE q. \<exists>r. pdivmod_rel x y q r)"
```
```   942
```
```   943 definition mod_poly where
```
```   944   "x mod y = (THE r. \<exists>q. pdivmod_rel x y q r)"
```
```   945
```
```   946 lemma div_poly_eq:
```
```   947   "pdivmod_rel x y q r \<Longrightarrow> x div y = q"
```
```   948 unfolding div_poly_def
```
```   949 by (fast elim: pdivmod_rel_unique_div)
```
```   950
```
```   951 lemma mod_poly_eq:
```
```   952   "pdivmod_rel x y q r \<Longrightarrow> x mod y = r"
```
```   953 unfolding mod_poly_def
```
```   954 by (fast elim: pdivmod_rel_unique_mod)
```
```   955
```
```   956 lemma pdivmod_rel:
```
```   957   "pdivmod_rel x y (x div y) (x mod y)"
```
```   958 proof -
```
```   959   from pdivmod_rel_exists
```
```   960     obtain q r where "pdivmod_rel x y q r" by fast
```
```   961   thus ?thesis
```
```   962     by (simp add: div_poly_eq mod_poly_eq)
```
```   963 qed
```
```   964
```
```   965 instance proof
```
```   966   fix x y :: "'a poly"
```
```   967   show "x div y * y + x mod y = x"
```
```   968     using pdivmod_rel [of x y]
```
```   969     by (simp add: pdivmod_rel_def)
```
```   970 next
```
```   971   fix x :: "'a poly"
```
```   972   have "pdivmod_rel x 0 0 x"
```
```   973     by (rule pdivmod_rel_by_0)
```
```   974   thus "x div 0 = 0"
```
```   975     by (rule div_poly_eq)
```
```   976 next
```
```   977   fix y :: "'a poly"
```
```   978   have "pdivmod_rel 0 y 0 0"
```
```   979     by (rule pdivmod_rel_0)
```
```   980   thus "0 div y = 0"
```
```   981     by (rule div_poly_eq)
```
```   982 next
```
```   983   fix x y z :: "'a poly"
```
```   984   assume "y \<noteq> 0"
```
```   985   hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)"
```
```   986     using pdivmod_rel [of x y]
```
```   987     by (simp add: pdivmod_rel_def left_distrib)
```
```   988   thus "(x + z * y) div y = z + x div y"
```
```   989     by (rule div_poly_eq)
```
```   990 next
```
```   991   fix x y z :: "'a poly"
```
```   992   assume "x \<noteq> 0"
```
```   993   show "(x * y) div (x * z) = y div z"
```
```   994   proof (cases "y \<noteq> 0 \<and> z \<noteq> 0")
```
```   995     have "\<And>x::'a poly. pdivmod_rel x 0 0 x"
```
```   996       by (rule pdivmod_rel_by_0)
```
```   997     then have [simp]: "\<And>x::'a poly. x div 0 = 0"
```
```   998       by (rule div_poly_eq)
```
```   999     have "\<And>x::'a poly. pdivmod_rel 0 x 0 0"
```
```  1000       by (rule pdivmod_rel_0)
```
```  1001     then have [simp]: "\<And>x::'a poly. 0 div x = 0"
```
```  1002       by (rule div_poly_eq)
```
```  1003     case False then show ?thesis by auto
```
```  1004   next
```
```  1005     case True then have "y \<noteq> 0" and "z \<noteq> 0" by auto
```
```  1006     with `x \<noteq> 0`
```
```  1007     have "\<And>q r. pdivmod_rel y z q r \<Longrightarrow> pdivmod_rel (x * y) (x * z) q (x * r)"
```
```  1008       by (auto simp add: pdivmod_rel_def algebra_simps)
```
```  1009         (rule classical, simp add: degree_mult_eq)
```
```  1010     moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" .
```
```  1011     ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" .
```
```  1012     then show ?thesis by (simp add: div_poly_eq)
```
```  1013   qed
```
```  1014 qed
```
```  1015
```
```  1016 end
```
```  1017
```
```  1018 lemma degree_mod_less:
```
```  1019   "y \<noteq> 0 \<Longrightarrow> x mod y = 0 \<or> degree (x mod y) < degree y"
```
```  1020   using pdivmod_rel [of x y]
```
```  1021   unfolding pdivmod_rel_def by simp
```
```  1022
```
```  1023 lemma div_poly_less: "degree x < degree y \<Longrightarrow> x div y = 0"
```
```  1024 proof -
```
```  1025   assume "degree x < degree y"
```
```  1026   hence "pdivmod_rel x y 0 x"
```
```  1027     by (simp add: pdivmod_rel_def)
```
```  1028   thus "x div y = 0" by (rule div_poly_eq)
```
```  1029 qed
```
```  1030
```
```  1031 lemma mod_poly_less: "degree x < degree y \<Longrightarrow> x mod y = x"
```
```  1032 proof -
```
```  1033   assume "degree x < degree y"
```
```  1034   hence "pdivmod_rel x y 0 x"
```
```  1035     by (simp add: pdivmod_rel_def)
```
```  1036   thus "x mod y = x" by (rule mod_poly_eq)
```
```  1037 qed
```
```  1038
```
```  1039 lemma pdivmod_rel_smult_left:
```
```  1040   "pdivmod_rel x y q r
```
```  1041     \<Longrightarrow> pdivmod_rel (smult a x) y (smult a q) (smult a r)"
```
```  1042   unfolding pdivmod_rel_def by (simp add: smult_add_right)
```
```  1043
```
```  1044 lemma div_smult_left: "(smult a x) div y = smult a (x div y)"
```
```  1045   by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
```
```  1046
```
```  1047 lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)"
```
```  1048   by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel)
```
```  1049
```
```  1050 lemma poly_div_minus_left [simp]:
```
```  1051   fixes x y :: "'a::field poly"
```
```  1052   shows "(- x) div y = - (x div y)"
```
```  1053   using div_smult_left [of "- 1::'a"] by simp
```
```  1054
```
```  1055 lemma poly_mod_minus_left [simp]:
```
```  1056   fixes x y :: "'a::field poly"
```
```  1057   shows "(- x) mod y = - (x mod y)"
```
```  1058   using mod_smult_left [of "- 1::'a"] by simp
```
```  1059
```
```  1060 lemma pdivmod_rel_smult_right:
```
```  1061   "\<lbrakk>a \<noteq> 0; pdivmod_rel x y q r\<rbrakk>
```
```  1062     \<Longrightarrow> pdivmod_rel x (smult a y) (smult (inverse a) q) r"
```
```  1063   unfolding pdivmod_rel_def by simp
```
```  1064
```
```  1065 lemma div_smult_right:
```
```  1066   "a \<noteq> 0 \<Longrightarrow> x div (smult a y) = smult (inverse a) (x div y)"
```
```  1067   by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
```
```  1068
```
```  1069 lemma mod_smult_right: "a \<noteq> 0 \<Longrightarrow> x mod (smult a y) = x mod y"
```
```  1070   by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel)
```
```  1071
```
```  1072 lemma poly_div_minus_right [simp]:
```
```  1073   fixes x y :: "'a::field poly"
```
```  1074   shows "x div (- y) = - (x div y)"
```
```  1075   using div_smult_right [of "- 1::'a"]
```
```  1076   by (simp add: nonzero_inverse_minus_eq)
```
```  1077
```
```  1078 lemma poly_mod_minus_right [simp]:
```
```  1079   fixes x y :: "'a::field poly"
```
```  1080   shows "x mod (- y) = x mod y"
```
```  1081   using mod_smult_right [of "- 1::'a"] by simp
```
```  1082
```
```  1083 lemma pdivmod_rel_mult:
```
```  1084   "\<lbrakk>pdivmod_rel x y q r; pdivmod_rel q z q' r'\<rbrakk>
```
```  1085     \<Longrightarrow> pdivmod_rel x (y * z) q' (y * r' + r)"
```
```  1086 apply (cases "z = 0", simp add: pdivmod_rel_def)
```
```  1087 apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff)
```
```  1088 apply (cases "r = 0")
```
```  1089 apply (cases "r' = 0")
```
```  1090 apply (simp add: pdivmod_rel_def)
```
```  1091 apply (simp add: pdivmod_rel_def field_simps degree_mult_eq)
```
```  1092 apply (cases "r' = 0")
```
```  1093 apply (simp add: pdivmod_rel_def degree_mult_eq)
```
```  1094 apply (simp add: pdivmod_rel_def field_simps)
```
```  1095 apply (simp add: degree_mult_eq degree_add_less)
```
```  1096 done
```
```  1097
```
```  1098 lemma poly_div_mult_right:
```
```  1099   fixes x y z :: "'a::field poly"
```
```  1100   shows "x div (y * z) = (x div y) div z"
```
```  1101   by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
```
```  1102
```
```  1103 lemma poly_mod_mult_right:
```
```  1104   fixes x y z :: "'a::field poly"
```
```  1105   shows "x mod (y * z) = y * (x div y mod z) + x mod y"
```
```  1106   by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+)
```
```  1107
```
```  1108 lemma mod_pCons:
```
```  1109   fixes a and x
```
```  1110   assumes y: "y \<noteq> 0"
```
```  1111   defines b: "b \<equiv> coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)"
```
```  1112   shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)"
```
```  1113 unfolding b
```
```  1114 apply (rule mod_poly_eq)
```
```  1115 apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl])
```
```  1116 done
```
```  1117
```
```  1118
```
```  1119 subsection {* GCD of polynomials *}
```
```  1120
```
```  1121 function
```
```  1122   poly_gcd :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
```
```  1123   "poly_gcd x 0 = smult (inverse (coeff x (degree x))) x"
```
```  1124 | "y \<noteq> 0 \<Longrightarrow> poly_gcd x y = poly_gcd y (x mod y)"
```
```  1125 by auto
```
```  1126
```
```  1127 termination poly_gcd
```
```  1128 by (relation "measure (\<lambda>(x, y). if y = 0 then 0 else Suc (degree y))")
```
```  1129    (auto dest: degree_mod_less)
```
```  1130
```
```  1131 declare poly_gcd.simps [simp del]
```
```  1132
```
```  1133 lemma poly_gcd_dvd1 [iff]: "poly_gcd x y dvd x"
```
```  1134   and poly_gcd_dvd2 [iff]: "poly_gcd x y dvd y"
```
```  1135   apply (induct x y rule: poly_gcd.induct)
```
```  1136   apply (simp_all add: poly_gcd.simps)
```
```  1137   apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero)
```
```  1138   apply (blast dest: dvd_mod_imp_dvd)
```
```  1139   done
```
```  1140
```
```  1141 lemma poly_gcd_greatest: "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd poly_gcd x y"
```
```  1142   by (induct x y rule: poly_gcd.induct)
```
```  1143      (simp_all add: poly_gcd.simps dvd_mod dvd_smult)
```
```  1144
```
```  1145 lemma dvd_poly_gcd_iff [iff]:
```
```  1146   "k dvd poly_gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
```
```  1147   by (blast intro!: poly_gcd_greatest intro: dvd_trans)
```
```  1148
```
```  1149 lemma poly_gcd_monic:
```
```  1150   "coeff (poly_gcd x y) (degree (poly_gcd x y)) =
```
```  1151     (if x = 0 \<and> y = 0 then 0 else 1)"
```
```  1152   by (induct x y rule: poly_gcd.induct)
```
```  1153      (simp_all add: poly_gcd.simps nonzero_imp_inverse_nonzero)
```
```  1154
```
```  1155 lemma poly_gcd_zero_iff [simp]:
```
```  1156   "poly_gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
```
```  1157   by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff)
```
```  1158
```
```  1159 lemma poly_gcd_0_0 [simp]: "poly_gcd 0 0 = 0"
```
```  1160   by simp
```
```  1161
```
```  1162 lemma poly_dvd_antisym:
```
```  1163   fixes p q :: "'a::idom poly"
```
```  1164   assumes coeff: "coeff p (degree p) = coeff q (degree q)"
```
```  1165   assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
```
```  1166 proof (cases "p = 0")
```
```  1167   case True with coeff show "p = q" by simp
```
```  1168 next
```
```  1169   case False with coeff have "q \<noteq> 0" by auto
```
```  1170   have degree: "degree p = degree q"
```
```  1171     using `p dvd q` `q dvd p` `p \<noteq> 0` `q \<noteq> 0`
```
```  1172     by (intro order_antisym dvd_imp_degree_le)
```
```  1173
```
```  1174   from `p dvd q` obtain a where a: "q = p * a" ..
```
```  1175   with `q \<noteq> 0` have "a \<noteq> 0" by auto
```
```  1176   with degree a `p \<noteq> 0` have "degree a = 0"
```
```  1177     by (simp add: degree_mult_eq)
```
```  1178   with coeff a show "p = q"
```
```  1179     by (cases a, auto split: if_splits)
```
```  1180 qed
```
```  1181
```
```  1182 lemma poly_gcd_unique:
```
```  1183   assumes dvd1: "d dvd x" and dvd2: "d dvd y"
```
```  1184     and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
```
```  1185     and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
```
```  1186   shows "poly_gcd x y = d"
```
```  1187 proof -
```
```  1188   have "coeff (poly_gcd x y) (degree (poly_gcd x y)) = coeff d (degree d)"
```
```  1189     by (simp_all add: poly_gcd_monic monic)
```
```  1190   moreover have "poly_gcd x y dvd d"
```
```  1191     using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest)
```
```  1192   moreover have "d dvd poly_gcd x y"
```
```  1193     using dvd1 dvd2 by (rule poly_gcd_greatest)
```
```  1194   ultimately show ?thesis
```
```  1195     by (rule poly_dvd_antisym)
```
```  1196 qed
```
```  1197
```
```  1198 interpretation poly_gcd: abel_semigroup poly_gcd
```
```  1199 proof
```
```  1200   fix x y z :: "'a poly"
```
```  1201   show "poly_gcd (poly_gcd x y) z = poly_gcd x (poly_gcd y z)"
```
```  1202     by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic)
```
```  1203   show "poly_gcd x y = poly_gcd y x"
```
```  1204     by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
```
```  1205 qed
```
```  1206
```
```  1207 lemmas poly_gcd_assoc = poly_gcd.assoc
```
```  1208 lemmas poly_gcd_commute = poly_gcd.commute
```
```  1209 lemmas poly_gcd_left_commute = poly_gcd.left_commute
```
```  1210
```
```  1211 lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute
```
```  1212
```
```  1213 lemma poly_gcd_1_left [simp]: "poly_gcd 1 y = 1"
```
```  1214 by (rule poly_gcd_unique) simp_all
```
```  1215
```
```  1216 lemma poly_gcd_1_right [simp]: "poly_gcd x 1 = 1"
```
```  1217 by (rule poly_gcd_unique) simp_all
```
```  1218
```
```  1219 lemma poly_gcd_minus_left [simp]: "poly_gcd (- x) y = poly_gcd x y"
```
```  1220 by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
```
```  1221
```
```  1222 lemma poly_gcd_minus_right [simp]: "poly_gcd x (- y) = poly_gcd x y"
```
```  1223 by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic)
```
```  1224
```
```  1225
```
```  1226 subsection {* Evaluation of polynomials *}
```
```  1227
```
```  1228 definition
```
```  1229   poly :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a" where
```
```  1230   "poly = poly_rec (\<lambda>x. 0) (\<lambda>a p f x. a + x * f x)"
```
```  1231
```
```  1232 lemma poly_0 [simp]: "poly 0 x = 0"
```
```  1233   unfolding poly_def by (simp add: poly_rec_0)
```
```  1234
```
```  1235 lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x"
```
```  1236   unfolding poly_def by (simp add: poly_rec_pCons)
```
```  1237
```
```  1238 lemma poly_1 [simp]: "poly 1 x = 1"
```
```  1239   unfolding one_poly_def by simp
```
```  1240
```
```  1241 lemma poly_monom:
```
```  1242   fixes a x :: "'a::{comm_semiring_1}"
```
```  1243   shows "poly (monom a n) x = a * x ^ n"
```
```  1244   by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac)
```
```  1245
```
```  1246 lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x"
```
```  1247   apply (induct p arbitrary: q, simp)
```
```  1248   apply (case_tac q, simp, simp add: algebra_simps)
```
```  1249   done
```
```  1250
```
```  1251 lemma poly_minus [simp]:
```
```  1252   fixes x :: "'a::comm_ring"
```
```  1253   shows "poly (- p) x = - poly p x"
```
```  1254   by (induct p, simp_all)
```
```  1255
```
```  1256 lemma poly_diff [simp]:
```
```  1257   fixes x :: "'a::comm_ring"
```
```  1258   shows "poly (p - q) x = poly p x - poly q x"
```
```  1259   by (simp add: diff_minus)
```
```  1260
```
```  1261 lemma poly_setsum: "poly (\<Sum>k\<in>A. p k) x = (\<Sum>k\<in>A. poly (p k) x)"
```
```  1262   by (cases "finite A", induct set: finite, simp_all)
```
```  1263
```
```  1264 lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x"
```
```  1265   by (induct p, simp, simp add: algebra_simps)
```
```  1266
```
```  1267 lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x"
```
```  1268   by (induct p, simp_all, simp add: algebra_simps)
```
```  1269
```
```  1270 lemma poly_power [simp]:
```
```  1271   fixes p :: "'a::{comm_semiring_1} poly"
```
```  1272   shows "poly (p ^ n) x = poly p x ^ n"
```
```  1273   by (induct n, simp, simp add: power_Suc)
```
```  1274
```
```  1275
```
```  1276 subsection {* Synthetic division *}
```
```  1277
```
```  1278 text {*
```
```  1279   Synthetic division is simply division by the
```
```  1280   linear polynomial @{term "x - c"}.
```
```  1281 *}
```
```  1282
```
```  1283 definition
```
```  1284   synthetic_divmod :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly \<times> 'a"
```
```  1285 where
```
```  1286   "synthetic_divmod p c =
```
```  1287     poly_rec (0, 0) (\<lambda>a p (q, r). (pCons r q, a + c * r)) p"
```
```  1288
```
```  1289 definition
```
```  1290   synthetic_div :: "'a::comm_semiring_0 poly \<Rightarrow> 'a \<Rightarrow> 'a poly"
```
```  1291 where
```
```  1292   "synthetic_div p c = fst (synthetic_divmod p c)"
```
```  1293
```
```  1294 lemma synthetic_divmod_0 [simp]:
```
```  1295   "synthetic_divmod 0 c = (0, 0)"
```
```  1296   unfolding synthetic_divmod_def
```
```  1297   by (simp add: poly_rec_0)
```
```  1298
```
```  1299 lemma synthetic_divmod_pCons [simp]:
```
```  1300   "synthetic_divmod (pCons a p) c =
```
```  1301     (\<lambda>(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)"
```
```  1302   unfolding synthetic_divmod_def
```
```  1303   by (simp add: poly_rec_pCons)
```
```  1304
```
```  1305 lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c"
```
```  1306   by (induct p, simp, simp add: split_def)
```
```  1307
```
```  1308 lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0"
```
```  1309   unfolding synthetic_div_def by simp
```
```  1310
```
```  1311 lemma synthetic_div_pCons [simp]:
```
```  1312   "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)"
```
```  1313   unfolding synthetic_div_def
```
```  1314   by (simp add: split_def snd_synthetic_divmod)
```
```  1315
```
```  1316 lemma synthetic_div_eq_0_iff:
```
```  1317   "synthetic_div p c = 0 \<longleftrightarrow> degree p = 0"
```
```  1318   by (induct p, simp, case_tac p, simp)
```
```  1319
```
```  1320 lemma degree_synthetic_div:
```
```  1321   "degree (synthetic_div p c) = degree p - 1"
```
```  1322   by (induct p, simp, simp add: synthetic_div_eq_0_iff)
```
```  1323
```
```  1324 lemma synthetic_div_correct:
```
```  1325   "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)"
```
```  1326   by (induct p) simp_all
```
```  1327
```
```  1328 lemma synthetic_div_unique_lemma: "smult c p = pCons a p \<Longrightarrow> p = 0"
```
```  1329 by (induct p arbitrary: a) simp_all
```
```  1330
```
```  1331 lemma synthetic_div_unique:
```
```  1332   "p + smult c q = pCons r q \<Longrightarrow> r = poly p c \<and> q = synthetic_div p c"
```
```  1333 apply (induct p arbitrary: q r)
```
```  1334 apply (simp, frule synthetic_div_unique_lemma, simp)
```
```  1335 apply (case_tac q, force)
```
```  1336 done
```
```  1337
```
```  1338 lemma synthetic_div_correct':
```
```  1339   fixes c :: "'a::comm_ring_1"
```
```  1340   shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p"
```
```  1341   using synthetic_div_correct [of p c]
```
```  1342   by (simp add: algebra_simps)
```
```  1343
```
```  1344 lemma poly_eq_0_iff_dvd:
```
```  1345   fixes c :: "'a::idom"
```
```  1346   shows "poly p c = 0 \<longleftrightarrow> [:-c, 1:] dvd p"
```
```  1347 proof
```
```  1348   assume "poly p c = 0"
```
```  1349   with synthetic_div_correct' [of c p]
```
```  1350   have "p = [:-c, 1:] * synthetic_div p c" by simp
```
```  1351   then show "[:-c, 1:] dvd p" ..
```
```  1352 next
```
```  1353   assume "[:-c, 1:] dvd p"
```
```  1354   then obtain k where "p = [:-c, 1:] * k" by (rule dvdE)
```
```  1355   then show "poly p c = 0" by simp
```
```  1356 qed
```
```  1357
```
```  1358 lemma dvd_iff_poly_eq_0:
```
```  1359   fixes c :: "'a::idom"
```
```  1360   shows "[:c, 1:] dvd p \<longleftrightarrow> poly p (-c) = 0"
```
```  1361   by (simp add: poly_eq_0_iff_dvd)
```
```  1362
```
```  1363 lemma poly_roots_finite:
```
```  1364   fixes p :: "'a::idom poly"
```
```  1365   shows "p \<noteq> 0 \<Longrightarrow> finite {x. poly p x = 0}"
```
```  1366 proof (induct n \<equiv> "degree p" arbitrary: p)
```
```  1367   case (0 p)
```
```  1368   then obtain a where "a \<noteq> 0" and "p = [:a:]"
```
```  1369     by (cases p, simp split: if_splits)
```
```  1370   then show "finite {x. poly p x = 0}" by simp
```
```  1371 next
```
```  1372   case (Suc n p)
```
```  1373   show "finite {x. poly p x = 0}"
```
```  1374   proof (cases "\<exists>x. poly p x = 0")
```
```  1375     case False
```
```  1376     then show "finite {x. poly p x = 0}" by simp
```
```  1377   next
```
```  1378     case True
```
```  1379     then obtain a where "poly p a = 0" ..
```
```  1380     then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd)
```
```  1381     then obtain k where k: "p = [:-a, 1:] * k" ..
```
```  1382     with `p \<noteq> 0` have "k \<noteq> 0" by auto
```
```  1383     with k have "degree p = Suc (degree k)"
```
```  1384       by (simp add: degree_mult_eq del: mult_pCons_left)
```
```  1385     with `Suc n = degree p` have "n = degree k" by simp
```
```  1386     then have "finite {x. poly k x = 0}" using `k \<noteq> 0` by (rule Suc.hyps)
```
```  1387     then have "finite (insert a {x. poly k x = 0})" by simp
```
```  1388     then show "finite {x. poly p x = 0}"
```
```  1389       by (simp add: k uminus_add_conv_diff Collect_disj_eq
```
```  1390                del: mult_pCons_left)
```
```  1391   qed
```
```  1392 qed
```
```  1393
```
```  1394 lemma poly_zero:
```
```  1395   fixes p :: "'a::{idom,ring_char_0} poly"
```
```  1396   shows "poly p = poly 0 \<longleftrightarrow> p = 0"
```
```  1397 apply (cases "p = 0", simp_all)
```
```  1398 apply (drule poly_roots_finite)
```
```  1399 apply (auto simp add: infinite_UNIV_char_0)
```
```  1400 done
```
```  1401
```
```  1402 lemma poly_eq_iff:
```
```  1403   fixes p q :: "'a::{idom,ring_char_0} poly"
```
```  1404   shows "poly p = poly q \<longleftrightarrow> p = q"
```
```  1405   using poly_zero [of "p - q"]
```
```  1406   by (simp add: fun_eq_iff)
```
```  1407
```
```  1408
```
```  1409 subsection {* Composition of polynomials *}
```
```  1410
```
```  1411 definition
```
```  1412   pcompose :: "'a::comm_semiring_0 poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
```
```  1413 where
```
```  1414   "pcompose p q = poly_rec 0 (\<lambda>a _ c. [:a:] + q * c) p"
```
```  1415
```
```  1416 lemma pcompose_0 [simp]: "pcompose 0 q = 0"
```
```  1417   unfolding pcompose_def by (simp add: poly_rec_0)
```
```  1418
```
```  1419 lemma pcompose_pCons:
```
```  1420   "pcompose (pCons a p) q = [:a:] + q * pcompose p q"
```
```  1421   unfolding pcompose_def by (simp add: poly_rec_pCons)
```
```  1422
```
```  1423 lemma poly_pcompose: "poly (pcompose p q) x = poly p (poly q x)"
```
```  1424   by (induct p) (simp_all add: pcompose_pCons)
```
```  1425
```
```  1426 lemma degree_pcompose_le:
```
```  1427   "degree (pcompose p q) \<le> degree p * degree q"
```
```  1428 apply (induct p, simp)
```
```  1429 apply (simp add: pcompose_pCons, clarify)
```
```  1430 apply (rule degree_add_le, simp)
```
```  1431 apply (rule order_trans [OF degree_mult_le], simp)
```
```  1432 done
```
```  1433
```
```  1434
```
```  1435 subsection {* Order of polynomial roots *}
```
```  1436
```
```  1437 definition
```
```  1438   order :: "'a::idom \<Rightarrow> 'a poly \<Rightarrow> nat"
```
```  1439 where
```
```  1440   "order a p = (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)"
```
```  1441
```
```  1442 lemma coeff_linear_power:
```
```  1443   fixes a :: "'a::comm_semiring_1"
```
```  1444   shows "coeff ([:a, 1:] ^ n) n = 1"
```
```  1445 apply (induct n, simp_all)
```
```  1446 apply (subst coeff_eq_0)
```
```  1447 apply (auto intro: le_less_trans degree_power_le)
```
```  1448 done
```
```  1449
```
```  1450 lemma degree_linear_power:
```
```  1451   fixes a :: "'a::comm_semiring_1"
```
```  1452   shows "degree ([:a, 1:] ^ n) = n"
```
```  1453 apply (rule order_antisym)
```
```  1454 apply (rule ord_le_eq_trans [OF degree_power_le], simp)
```
```  1455 apply (rule le_degree, simp add: coeff_linear_power)
```
```  1456 done
```
```  1457
```
```  1458 lemma order_1: "[:-a, 1:] ^ order a p dvd p"
```
```  1459 apply (cases "p = 0", simp)
```
```  1460 apply (cases "order a p", simp)
```
```  1461 apply (subgoal_tac "nat < (LEAST n. \<not> [:-a, 1:] ^ Suc n dvd p)")
```
```  1462 apply (drule not_less_Least, simp)
```
```  1463 apply (fold order_def, simp)
```
```  1464 done
```
```  1465
```
```  1466 lemma order_2: "p \<noteq> 0 \<Longrightarrow> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
```
```  1467 unfolding order_def
```
```  1468 apply (rule LeastI_ex)
```
```  1469 apply (rule_tac x="degree p" in exI)
```
```  1470 apply (rule notI)
```
```  1471 apply (drule (1) dvd_imp_degree_le)
```
```  1472 apply (simp only: degree_linear_power)
```
```  1473 done
```
```  1474
```
```  1475 lemma order:
```
```  1476   "p \<noteq> 0 \<Longrightarrow> [:-a, 1:] ^ order a p dvd p \<and> \<not> [:-a, 1:] ^ Suc (order a p) dvd p"
```
```  1477 by (rule conjI [OF order_1 order_2])
```
```  1478
```
```  1479 lemma order_degree:
```
```  1480   assumes p: "p \<noteq> 0"
```
```  1481   shows "order a p \<le> degree p"
```
```  1482 proof -
```
```  1483   have "order a p = degree ([:-a, 1:] ^ order a p)"
```
```  1484     by (simp only: degree_linear_power)
```
```  1485   also have "\<dots> \<le> degree p"
```
```  1486     using order_1 p by (rule dvd_imp_degree_le)
```
```  1487   finally show ?thesis .
```
```  1488 qed
```
```  1489
```
```  1490 lemma order_root: "poly p a = 0 \<longleftrightarrow> p = 0 \<or> order a p \<noteq> 0"
```
```  1491 apply (cases "p = 0", simp_all)
```
```  1492 apply (rule iffI)
```
```  1493 apply (rule ccontr, simp)
```
```  1494 apply (frule order_2 [where a=a], simp)
```
```  1495 apply (simp add: poly_eq_0_iff_dvd)
```
```  1496 apply (simp add: poly_eq_0_iff_dvd)
```
```  1497 apply (simp only: order_def)
```
```  1498 apply (drule not_less_Least, simp)
```
```  1499 done
```
```  1500
```
```  1501
```
```  1502 subsection {* Configuration of the code generator *}
```
```  1503
```
```  1504 code_datatype "0::'a::zero poly" pCons
```
```  1505
```
```  1506 quickcheck_generator poly constructors: "0::'a::zero poly", pCons
```
```  1507
```
```  1508 declare pCons_0_0 [code_post]
```
```  1509
```
```  1510 instantiation poly :: ("{zero, equal}") equal
```
```  1511 begin
```
```  1512
```
```  1513 definition
```
```  1514   "HOL.equal (p::'a poly) q \<longleftrightarrow> p = q"
```
```  1515
```
```  1516 instance proof
```
```  1517 qed (rule equal_poly_def)
```
```  1518
```
```  1519 end
```
```  1520
```
```  1521 lemma eq_poly_code [code]:
```
```  1522   "HOL.equal (0::_ poly) (0::_ poly) \<longleftrightarrow> True"
```
```  1523   "HOL.equal (0::_ poly) (pCons b q) \<longleftrightarrow> HOL.equal 0 b \<and> HOL.equal 0 q"
```
```  1524   "HOL.equal (pCons a p) (0::_ poly) \<longleftrightarrow> HOL.equal a 0 \<and> HOL.equal p 0"
```
```  1525   "HOL.equal (pCons a p) (pCons b q) \<longleftrightarrow> HOL.equal a b \<and> HOL.equal p q"
```
```  1526   by (simp_all add: equal)
```
```  1527
```
```  1528 lemma [code nbe]:
```
```  1529   "HOL.equal (p :: _ poly) p \<longleftrightarrow> True"
```
```  1530   by (fact equal_refl)
```
```  1531
```
```  1532 lemmas coeff_code [code] =
```
```  1533   coeff_0 coeff_pCons_0 coeff_pCons_Suc
```
```  1534
```
```  1535 lemmas degree_code [code] =
```
```  1536   degree_0 degree_pCons_eq_if
```
```  1537
```
```  1538 lemmas monom_poly_code [code] =
```
```  1539   monom_0 monom_Suc
```
```  1540
```
```  1541 lemma add_poly_code [code]:
```
```  1542   "0 + q = (q :: _ poly)"
```
```  1543   "p + 0 = (p :: _ poly)"
```
```  1544   "pCons a p + pCons b q = pCons (a + b) (p + q)"
```
```  1545 by simp_all
```
```  1546
```
```  1547 lemma minus_poly_code [code]:
```
```  1548   "- 0 = (0 :: _ poly)"
```
```  1549   "- pCons a p = pCons (- a) (- p)"
```
```  1550 by simp_all
```
```  1551
```
```  1552 lemma diff_poly_code [code]:
```
```  1553   "0 - q = (- q :: _ poly)"
```
```  1554   "p - 0 = (p :: _ poly)"
```
```  1555   "pCons a p - pCons b q = pCons (a - b) (p - q)"
```
```  1556 by simp_all
```
```  1557
```
```  1558 lemmas smult_poly_code [code] =
```
```  1559   smult_0_right smult_pCons
```
```  1560
```
```  1561 lemma mult_poly_code [code]:
```
```  1562   "0 * q = (0 :: _ poly)"
```
```  1563   "pCons a p * q = smult a q + pCons 0 (p * q)"
```
```  1564 by simp_all
```
```  1565
```
```  1566 lemmas poly_code [code] =
```
```  1567   poly_0 poly_pCons
```
```  1568
```
```  1569 lemmas synthetic_divmod_code [code] =
```
```  1570   synthetic_divmod_0 synthetic_divmod_pCons
```
```  1571
```
```  1572 text {* code generator setup for div and mod *}
```
```  1573
```
```  1574 definition
```
```  1575   pdivmod :: "'a::field poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly \<times> 'a poly"
```
```  1576 where
```
```  1577   "pdivmod x y = (x div y, x mod y)"
```
```  1578
```
```  1579 lemma div_poly_code [code]: "x div y = fst (pdivmod x y)"
```
```  1580   unfolding pdivmod_def by simp
```
```  1581
```
```  1582 lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)"
```
```  1583   unfolding pdivmod_def by simp
```
```  1584
```
```  1585 lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)"
```
```  1586   unfolding pdivmod_def by simp
```
```  1587
```
```  1588 lemma pdivmod_pCons [code]:
```
```  1589   "pdivmod (pCons a x) y =
```
```  1590     (if y = 0 then (0, pCons a x) else
```
```  1591       (let (q, r) = pdivmod x y;
```
```  1592            b = coeff (pCons a r) (degree y) / coeff y (degree y)
```
```  1593         in (pCons b q, pCons a r - smult b y)))"
```
```  1594 apply (simp add: pdivmod_def Let_def, safe)
```
```  1595 apply (rule div_poly_eq)
```
```  1596 apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
```
```  1597 apply (rule mod_poly_eq)
```
```  1598 apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl])
```
```  1599 done
```
```  1600
```
```  1601 lemma poly_gcd_code [code]:
```
```  1602   "poly_gcd x y =
```
```  1603     (if y = 0 then smult (inverse (coeff x (degree x))) x
```
```  1604               else poly_gcd y (x mod y))"
```
```  1605   by (simp add: poly_gcd.simps)
```
```  1606
```
```  1607 end
```