src/HOL/Polynomial.thy
 author huffman Wed Feb 11 11:22:42 2009 -0800 (2009-02-11) changeset 29878 06efd6e731c6 parent 29668 33ba3faeaa0e child 29904 856f16a3b436 permissions -rw-r--r--
ordered_idom instance for polynomials
 huffman@29451 ` 1` ```(* Title: HOL/Polynomial.thy ``` huffman@29451 ` 2` ``` Author: Brian Huffman ``` huffman@29451 ` 3` ``` Based on an earlier development by Clemens Ballarin ``` huffman@29451 ` 4` ```*) ``` huffman@29451 ` 5` huffman@29451 ` 6` ```header {* Univariate Polynomials *} ``` huffman@29451 ` 7` huffman@29451 ` 8` ```theory Polynomial ``` haftmann@29654 ` 9` ```imports Plain SetInterval Main ``` huffman@29451 ` 10` ```begin ``` huffman@29451 ` 11` huffman@29451 ` 12` ```subsection {* Definition of type @{text poly} *} ``` huffman@29451 ` 13` huffman@29451 ` 14` ```typedef (Poly) 'a poly = "{f::nat \ 'a::zero. \n. \i>n. f i = 0}" ``` huffman@29451 ` 15` ``` morphisms coeff Abs_poly ``` huffman@29451 ` 16` ``` by auto ``` huffman@29451 ` 17` huffman@29451 ` 18` ```lemma expand_poly_eq: "p = q \ (\n. coeff p n = coeff q n)" ``` huffman@29451 ` 19` ```by (simp add: coeff_inject [symmetric] expand_fun_eq) ``` huffman@29451 ` 20` huffman@29451 ` 21` ```lemma poly_ext: "(\n. coeff p n = coeff q n) \ p = q" ``` huffman@29451 ` 22` ```by (simp add: expand_poly_eq) ``` huffman@29451 ` 23` huffman@29451 ` 24` huffman@29451 ` 25` ```subsection {* Degree of a polynomial *} ``` huffman@29451 ` 26` huffman@29451 ` 27` ```definition ``` huffman@29451 ` 28` ``` degree :: "'a::zero poly \ nat" where ``` huffman@29451 ` 29` ``` "degree p = (LEAST n. \i>n. coeff p i = 0)" ``` huffman@29451 ` 30` huffman@29451 ` 31` ```lemma coeff_eq_0: "degree p < n \ coeff p n = 0" ``` huffman@29451 ` 32` ```proof - ``` huffman@29451 ` 33` ``` have "coeff p \ Poly" ``` huffman@29451 ` 34` ``` by (rule coeff) ``` huffman@29451 ` 35` ``` hence "\n. \i>n. coeff p i = 0" ``` huffman@29451 ` 36` ``` unfolding Poly_def by simp ``` huffman@29451 ` 37` ``` hence "\i>degree p. coeff p i = 0" ``` huffman@29451 ` 38` ``` unfolding degree_def by (rule LeastI_ex) ``` huffman@29451 ` 39` ``` moreover assume "degree p < n" ``` huffman@29451 ` 40` ``` ultimately show ?thesis by simp ``` huffman@29451 ` 41` ```qed ``` huffman@29451 ` 42` huffman@29451 ` 43` ```lemma le_degree: "coeff p n \ 0 \ n \ degree p" ``` huffman@29451 ` 44` ``` by (erule contrapos_np, rule coeff_eq_0, simp) ``` huffman@29451 ` 45` huffman@29451 ` 46` ```lemma degree_le: "\i>n. coeff p i = 0 \ degree p \ n" ``` huffman@29451 ` 47` ``` unfolding degree_def by (erule Least_le) ``` huffman@29451 ` 48` huffman@29451 ` 49` ```lemma less_degree_imp: "n < degree p \ \i>n. coeff p i \ 0" ``` huffman@29451 ` 50` ``` unfolding degree_def by (drule not_less_Least, simp) ``` huffman@29451 ` 51` huffman@29451 ` 52` huffman@29451 ` 53` ```subsection {* The zero polynomial *} ``` huffman@29451 ` 54` huffman@29451 ` 55` ```instantiation poly :: (zero) zero ``` huffman@29451 ` 56` ```begin ``` huffman@29451 ` 57` huffman@29451 ` 58` ```definition ``` huffman@29451 ` 59` ``` zero_poly_def: "0 = Abs_poly (\n. 0)" ``` huffman@29451 ` 60` huffman@29451 ` 61` ```instance .. ``` huffman@29451 ` 62` ```end ``` huffman@29451 ` 63` huffman@29451 ` 64` ```lemma coeff_0 [simp]: "coeff 0 n = 0" ``` huffman@29451 ` 65` ``` unfolding zero_poly_def ``` huffman@29451 ` 66` ``` by (simp add: Abs_poly_inverse Poly_def) ``` huffman@29451 ` 67` huffman@29451 ` 68` ```lemma degree_0 [simp]: "degree 0 = 0" ``` huffman@29451 ` 69` ``` by (rule order_antisym [OF degree_le le0]) simp ``` huffman@29451 ` 70` huffman@29451 ` 71` ```lemma leading_coeff_neq_0: ``` huffman@29451 ` 72` ``` assumes "p \ 0" shows "coeff p (degree p) \ 0" ``` huffman@29451 ` 73` ```proof (cases "degree p") ``` huffman@29451 ` 74` ``` case 0 ``` huffman@29451 ` 75` ``` from `p \ 0` have "\n. coeff p n \ 0" ``` huffman@29451 ` 76` ``` by (simp add: expand_poly_eq) ``` huffman@29451 ` 77` ``` then obtain n where "coeff p n \ 0" .. ``` huffman@29451 ` 78` ``` hence "n \ degree p" by (rule le_degree) ``` huffman@29451 ` 79` ``` with `coeff p n \ 0` and `degree p = 0` ``` huffman@29451 ` 80` ``` show "coeff p (degree p) \ 0" by simp ``` huffman@29451 ` 81` ```next ``` huffman@29451 ` 82` ``` case (Suc n) ``` huffman@29451 ` 83` ``` from `degree p = Suc n` have "n < degree p" by simp ``` huffman@29451 ` 84` ``` hence "\i>n. coeff p i \ 0" by (rule less_degree_imp) ``` huffman@29451 ` 85` ``` then obtain i where "n < i" and "coeff p i \ 0" by fast ``` huffman@29451 ` 86` ``` from `degree p = Suc n` and `n < i` have "degree p \ i" by simp ``` huffman@29451 ` 87` ``` also from `coeff p i \ 0` have "i \ degree p" by (rule le_degree) ``` huffman@29451 ` 88` ``` finally have "degree p = i" . ``` huffman@29451 ` 89` ``` with `coeff p i \ 0` show "coeff p (degree p) \ 0" by simp ``` huffman@29451 ` 90` ```qed ``` huffman@29451 ` 91` huffman@29451 ` 92` ```lemma leading_coeff_0_iff [simp]: "coeff p (degree p) = 0 \ p = 0" ``` huffman@29451 ` 93` ``` by (cases "p = 0", simp, simp add: leading_coeff_neq_0) ``` huffman@29451 ` 94` huffman@29451 ` 95` huffman@29451 ` 96` ```subsection {* List-style constructor for polynomials *} ``` huffman@29451 ` 97` huffman@29451 ` 98` ```definition ``` huffman@29451 ` 99` ``` pCons :: "'a::zero \ 'a poly \ 'a poly" ``` huffman@29451 ` 100` ```where ``` huffman@29451 ` 101` ``` [code del]: "pCons a p = Abs_poly (nat_case a (coeff p))" ``` huffman@29451 ` 102` huffman@29455 ` 103` ```syntax ``` huffman@29455 ` 104` ``` "_poly" :: "args \ 'a poly" ("[:(_):]") ``` huffman@29455 ` 105` huffman@29455 ` 106` ```translations ``` huffman@29455 ` 107` ``` "[:x, xs:]" == "CONST pCons x [:xs:]" ``` huffman@29455 ` 108` ``` "[:x:]" == "CONST pCons x 0" ``` huffman@29455 ` 109` huffman@29451 ` 110` ```lemma Poly_nat_case: "f \ Poly \ nat_case a f \ Poly" ``` huffman@29451 ` 111` ``` unfolding Poly_def by (auto split: nat.split) ``` huffman@29451 ` 112` huffman@29451 ` 113` ```lemma coeff_pCons: ``` huffman@29451 ` 114` ``` "coeff (pCons a p) = nat_case a (coeff p)" ``` huffman@29451 ` 115` ``` unfolding pCons_def ``` huffman@29451 ` 116` ``` by (simp add: Abs_poly_inverse Poly_nat_case coeff) ``` huffman@29451 ` 117` huffman@29451 ` 118` ```lemma coeff_pCons_0 [simp]: "coeff (pCons a p) 0 = a" ``` huffman@29451 ` 119` ``` by (simp add: coeff_pCons) ``` huffman@29451 ` 120` huffman@29451 ` 121` ```lemma coeff_pCons_Suc [simp]: "coeff (pCons a p) (Suc n) = coeff p n" ``` huffman@29451 ` 122` ``` by (simp add: coeff_pCons) ``` huffman@29451 ` 123` huffman@29451 ` 124` ```lemma degree_pCons_le: "degree (pCons a p) \ Suc (degree p)" ``` huffman@29451 ` 125` ```by (rule degree_le, simp add: coeff_eq_0 coeff_pCons split: nat.split) ``` huffman@29451 ` 126` huffman@29451 ` 127` ```lemma degree_pCons_eq: ``` huffman@29451 ` 128` ``` "p \ 0 \ degree (pCons a p) = Suc (degree p)" ``` huffman@29451 ` 129` ```apply (rule order_antisym [OF degree_pCons_le]) ``` huffman@29451 ` 130` ```apply (rule le_degree, simp) ``` huffman@29451 ` 131` ```done ``` huffman@29451 ` 132` huffman@29451 ` 133` ```lemma degree_pCons_0: "degree (pCons a 0) = 0" ``` huffman@29451 ` 134` ```apply (rule order_antisym [OF _ le0]) ``` huffman@29451 ` 135` ```apply (rule degree_le, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 136` ```done ``` huffman@29451 ` 137` huffman@29460 ` 138` ```lemma degree_pCons_eq_if [simp]: ``` huffman@29451 ` 139` ``` "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" ``` huffman@29451 ` 140` ```apply (cases "p = 0", simp_all) ``` huffman@29451 ` 141` ```apply (rule order_antisym [OF _ le0]) ``` huffman@29451 ` 142` ```apply (rule degree_le, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 143` ```apply (rule order_antisym [OF degree_pCons_le]) ``` huffman@29451 ` 144` ```apply (rule le_degree, simp) ``` huffman@29451 ` 145` ```done ``` huffman@29451 ` 146` huffman@29451 ` 147` ```lemma pCons_0_0 [simp]: "pCons 0 0 = 0" ``` huffman@29451 ` 148` ```by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 149` huffman@29451 ` 150` ```lemma pCons_eq_iff [simp]: ``` huffman@29451 ` 151` ``` "pCons a p = pCons b q \ a = b \ p = q" ``` huffman@29451 ` 152` ```proof (safe) ``` huffman@29451 ` 153` ``` assume "pCons a p = pCons b q" ``` huffman@29451 ` 154` ``` then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp ``` huffman@29451 ` 155` ``` then show "a = b" by simp ``` huffman@29451 ` 156` ```next ``` huffman@29451 ` 157` ``` assume "pCons a p = pCons b q" ``` huffman@29451 ` 158` ``` then have "\n. coeff (pCons a p) (Suc n) = ``` huffman@29451 ` 159` ``` coeff (pCons b q) (Suc n)" by simp ``` huffman@29451 ` 160` ``` then show "p = q" by (simp add: expand_poly_eq) ``` huffman@29451 ` 161` ```qed ``` huffman@29451 ` 162` huffman@29451 ` 163` ```lemma pCons_eq_0_iff [simp]: "pCons a p = 0 \ a = 0 \ p = 0" ``` huffman@29451 ` 164` ``` using pCons_eq_iff [of a p 0 0] by simp ``` huffman@29451 ` 165` huffman@29451 ` 166` ```lemma Poly_Suc: "f \ Poly \ (\n. f (Suc n)) \ Poly" ``` huffman@29451 ` 167` ``` unfolding Poly_def ``` huffman@29451 ` 168` ``` by (clarify, rule_tac x=n in exI, simp) ``` huffman@29451 ` 169` huffman@29451 ` 170` ```lemma pCons_cases [cases type: poly]: ``` huffman@29451 ` 171` ``` obtains (pCons) a q where "p = pCons a q" ``` huffman@29451 ` 172` ```proof ``` huffman@29451 ` 173` ``` show "p = pCons (coeff p 0) (Abs_poly (\n. coeff p (Suc n)))" ``` huffman@29451 ` 174` ``` by (rule poly_ext) ``` huffman@29451 ` 175` ``` (simp add: Abs_poly_inverse Poly_Suc coeff coeff_pCons ``` huffman@29451 ` 176` ``` split: nat.split) ``` huffman@29451 ` 177` ```qed ``` huffman@29451 ` 178` huffman@29451 ` 179` ```lemma pCons_induct [case_names 0 pCons, induct type: poly]: ``` huffman@29451 ` 180` ``` assumes zero: "P 0" ``` huffman@29451 ` 181` ``` assumes pCons: "\a p. P p \ P (pCons a p)" ``` huffman@29451 ` 182` ``` shows "P p" ``` huffman@29451 ` 183` ```proof (induct p rule: measure_induct_rule [where f=degree]) ``` huffman@29451 ` 184` ``` case (less p) ``` huffman@29451 ` 185` ``` obtain a q where "p = pCons a q" by (rule pCons_cases) ``` huffman@29451 ` 186` ``` have "P q" ``` huffman@29451 ` 187` ``` proof (cases "q = 0") ``` huffman@29451 ` 188` ``` case True ``` huffman@29451 ` 189` ``` then show "P q" by (simp add: zero) ``` huffman@29451 ` 190` ``` next ``` huffman@29451 ` 191` ``` case False ``` huffman@29451 ` 192` ``` then have "degree (pCons a q) = Suc (degree q)" ``` huffman@29451 ` 193` ``` by (rule degree_pCons_eq) ``` huffman@29451 ` 194` ``` then have "degree q < degree p" ``` huffman@29451 ` 195` ``` using `p = pCons a q` by simp ``` huffman@29451 ` 196` ``` then show "P q" ``` huffman@29451 ` 197` ``` by (rule less.hyps) ``` huffman@29451 ` 198` ``` qed ``` huffman@29451 ` 199` ``` then have "P (pCons a q)" ``` huffman@29451 ` 200` ``` by (rule pCons) ``` huffman@29451 ` 201` ``` then show ?case ``` huffman@29451 ` 202` ``` using `p = pCons a q` by simp ``` huffman@29451 ` 203` ```qed ``` huffman@29451 ` 204` huffman@29451 ` 205` huffman@29454 ` 206` ```subsection {* Recursion combinator for polynomials *} ``` huffman@29454 ` 207` huffman@29454 ` 208` ```function ``` huffman@29454 ` 209` ``` poly_rec :: "'b \ ('a::zero \ 'a poly \ 'b \ 'b) \ 'a poly \ 'b" ``` huffman@29454 ` 210` ```where ``` huffman@29475 ` 211` ``` poly_rec_pCons_eq_if [simp del, code del]: ``` huffman@29454 ` 212` ``` "poly_rec z f (pCons a p) = f a p (if p = 0 then z else poly_rec z f p)" ``` huffman@29454 ` 213` ```by (case_tac x, rename_tac q, case_tac q, auto) ``` huffman@29454 ` 214` huffman@29454 ` 215` ```termination poly_rec ``` huffman@29454 ` 216` ```by (relation "measure (degree \ snd \ snd)", simp) ``` huffman@29454 ` 217` ``` (simp add: degree_pCons_eq) ``` huffman@29454 ` 218` huffman@29454 ` 219` ```lemma poly_rec_0: ``` huffman@29454 ` 220` ``` "f 0 0 z = z \ poly_rec z f 0 = z" ``` huffman@29454 ` 221` ``` using poly_rec_pCons_eq_if [of z f 0 0] by simp ``` huffman@29454 ` 222` huffman@29454 ` 223` ```lemma poly_rec_pCons: ``` huffman@29454 ` 224` ``` "f 0 0 z = z \ poly_rec z f (pCons a p) = f a p (poly_rec z f p)" ``` huffman@29454 ` 225` ``` by (simp add: poly_rec_pCons_eq_if poly_rec_0) ``` huffman@29454 ` 226` huffman@29454 ` 227` huffman@29451 ` 228` ```subsection {* Monomials *} ``` huffman@29451 ` 229` huffman@29451 ` 230` ```definition ``` huffman@29451 ` 231` ``` monom :: "'a \ nat \ 'a::zero poly" where ``` huffman@29451 ` 232` ``` "monom a m = Abs_poly (\n. if m = n then a else 0)" ``` huffman@29451 ` 233` huffman@29451 ` 234` ```lemma coeff_monom [simp]: "coeff (monom a m) n = (if m=n then a else 0)" ``` huffman@29451 ` 235` ``` unfolding monom_def ``` huffman@29451 ` 236` ``` by (subst Abs_poly_inverse, auto simp add: Poly_def) ``` huffman@29451 ` 237` huffman@29451 ` 238` ```lemma monom_0: "monom a 0 = pCons a 0" ``` huffman@29451 ` 239` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 240` huffman@29451 ` 241` ```lemma monom_Suc: "monom a (Suc n) = pCons 0 (monom a n)" ``` huffman@29451 ` 242` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 243` huffman@29451 ` 244` ```lemma monom_eq_0 [simp]: "monom 0 n = 0" ``` huffman@29451 ` 245` ``` by (rule poly_ext) simp ``` huffman@29451 ` 246` huffman@29451 ` 247` ```lemma monom_eq_0_iff [simp]: "monom a n = 0 \ a = 0" ``` huffman@29451 ` 248` ``` by (simp add: expand_poly_eq) ``` huffman@29451 ` 249` huffman@29451 ` 250` ```lemma monom_eq_iff [simp]: "monom a n = monom b n \ a = b" ``` huffman@29451 ` 251` ``` by (simp add: expand_poly_eq) ``` huffman@29451 ` 252` huffman@29451 ` 253` ```lemma degree_monom_le: "degree (monom a n) \ n" ``` huffman@29451 ` 254` ``` by (rule degree_le, simp) ``` huffman@29451 ` 255` huffman@29451 ` 256` ```lemma degree_monom_eq: "a \ 0 \ degree (monom a n) = n" ``` huffman@29451 ` 257` ``` apply (rule order_antisym [OF degree_monom_le]) ``` huffman@29451 ` 258` ``` apply (rule le_degree, simp) ``` huffman@29451 ` 259` ``` done ``` huffman@29451 ` 260` huffman@29451 ` 261` huffman@29451 ` 262` ```subsection {* Addition and subtraction *} ``` huffman@29451 ` 263` huffman@29451 ` 264` ```instantiation poly :: (comm_monoid_add) comm_monoid_add ``` huffman@29451 ` 265` ```begin ``` huffman@29451 ` 266` huffman@29451 ` 267` ```definition ``` huffman@29451 ` 268` ``` plus_poly_def [code del]: ``` huffman@29451 ` 269` ``` "p + q = Abs_poly (\n. coeff p n + coeff q n)" ``` huffman@29451 ` 270` huffman@29451 ` 271` ```lemma Poly_add: ``` huffman@29451 ` 272` ``` fixes f g :: "nat \ 'a" ``` huffman@29451 ` 273` ``` shows "\f \ Poly; g \ Poly\ \ (\n. f n + g n) \ Poly" ``` huffman@29451 ` 274` ``` unfolding Poly_def ``` huffman@29451 ` 275` ``` apply (clarify, rename_tac m n) ``` huffman@29451 ` 276` ``` apply (rule_tac x="max m n" in exI, simp) ``` huffman@29451 ` 277` ``` done ``` huffman@29451 ` 278` huffman@29451 ` 279` ```lemma coeff_add [simp]: ``` huffman@29451 ` 280` ``` "coeff (p + q) n = coeff p n + coeff q n" ``` huffman@29451 ` 281` ``` unfolding plus_poly_def ``` huffman@29451 ` 282` ``` by (simp add: Abs_poly_inverse coeff Poly_add) ``` huffman@29451 ` 283` huffman@29451 ` 284` ```instance proof ``` huffman@29451 ` 285` ``` fix p q r :: "'a poly" ``` huffman@29451 ` 286` ``` show "(p + q) + r = p + (q + r)" ``` huffman@29451 ` 287` ``` by (simp add: expand_poly_eq add_assoc) ``` huffman@29451 ` 288` ``` show "p + q = q + p" ``` huffman@29451 ` 289` ``` by (simp add: expand_poly_eq add_commute) ``` huffman@29451 ` 290` ``` show "0 + p = p" ``` huffman@29451 ` 291` ``` by (simp add: expand_poly_eq) ``` huffman@29451 ` 292` ```qed ``` huffman@29451 ` 293` huffman@29451 ` 294` ```end ``` huffman@29451 ` 295` huffman@29540 ` 296` ```instance poly :: ``` huffman@29540 ` 297` ``` ("{cancel_ab_semigroup_add,comm_monoid_add}") cancel_ab_semigroup_add ``` huffman@29540 ` 298` ```proof ``` huffman@29540 ` 299` ``` fix p q r :: "'a poly" ``` huffman@29540 ` 300` ``` assume "p + q = p + r" thus "q = r" ``` huffman@29540 ` 301` ``` by (simp add: expand_poly_eq) ``` huffman@29540 ` 302` ```qed ``` huffman@29540 ` 303` huffman@29451 ` 304` ```instantiation poly :: (ab_group_add) ab_group_add ``` huffman@29451 ` 305` ```begin ``` huffman@29451 ` 306` huffman@29451 ` 307` ```definition ``` huffman@29451 ` 308` ``` uminus_poly_def [code del]: ``` huffman@29451 ` 309` ``` "- p = Abs_poly (\n. - coeff p n)" ``` huffman@29451 ` 310` huffman@29451 ` 311` ```definition ``` huffman@29451 ` 312` ``` minus_poly_def [code del]: ``` huffman@29451 ` 313` ``` "p - q = Abs_poly (\n. coeff p n - coeff q n)" ``` huffman@29451 ` 314` huffman@29451 ` 315` ```lemma Poly_minus: ``` huffman@29451 ` 316` ``` fixes f :: "nat \ 'a" ``` huffman@29451 ` 317` ``` shows "f \ Poly \ (\n. - f n) \ Poly" ``` huffman@29451 ` 318` ``` unfolding Poly_def by simp ``` huffman@29451 ` 319` huffman@29451 ` 320` ```lemma Poly_diff: ``` huffman@29451 ` 321` ``` fixes f g :: "nat \ 'a" ``` huffman@29451 ` 322` ``` shows "\f \ Poly; g \ Poly\ \ (\n. f n - g n) \ Poly" ``` huffman@29451 ` 323` ``` unfolding diff_minus by (simp add: Poly_add Poly_minus) ``` huffman@29451 ` 324` huffman@29451 ` 325` ```lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" ``` huffman@29451 ` 326` ``` unfolding uminus_poly_def ``` huffman@29451 ` 327` ``` by (simp add: Abs_poly_inverse coeff Poly_minus) ``` huffman@29451 ` 328` huffman@29451 ` 329` ```lemma coeff_diff [simp]: ``` huffman@29451 ` 330` ``` "coeff (p - q) n = coeff p n - coeff q n" ``` huffman@29451 ` 331` ``` unfolding minus_poly_def ``` huffman@29451 ` 332` ``` by (simp add: Abs_poly_inverse coeff Poly_diff) ``` huffman@29451 ` 333` huffman@29451 ` 334` ```instance proof ``` huffman@29451 ` 335` ``` fix p q :: "'a poly" ``` huffman@29451 ` 336` ``` show "- p + p = 0" ``` huffman@29451 ` 337` ``` by (simp add: expand_poly_eq) ``` huffman@29451 ` 338` ``` show "p - q = p + - q" ``` huffman@29451 ` 339` ``` by (simp add: expand_poly_eq diff_minus) ``` huffman@29451 ` 340` ```qed ``` huffman@29451 ` 341` huffman@29451 ` 342` ```end ``` huffman@29451 ` 343` huffman@29451 ` 344` ```lemma add_pCons [simp]: ``` huffman@29451 ` 345` ``` "pCons a p + pCons b q = pCons (a + b) (p + q)" ``` huffman@29451 ` 346` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 347` huffman@29451 ` 348` ```lemma minus_pCons [simp]: ``` huffman@29451 ` 349` ``` "- pCons a p = pCons (- a) (- p)" ``` huffman@29451 ` 350` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 351` huffman@29451 ` 352` ```lemma diff_pCons [simp]: ``` huffman@29451 ` 353` ``` "pCons a p - pCons b q = pCons (a - b) (p - q)" ``` huffman@29451 ` 354` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 355` huffman@29539 ` 356` ```lemma degree_add_le_max: "degree (p + q) \ max (degree p) (degree q)" ``` huffman@29451 ` 357` ``` by (rule degree_le, auto simp add: coeff_eq_0) ``` huffman@29451 ` 358` huffman@29539 ` 359` ```lemma degree_add_le: ``` huffman@29539 ` 360` ``` "\degree p \ n; degree q \ n\ \ degree (p + q) \ n" ``` huffman@29539 ` 361` ``` by (auto intro: order_trans degree_add_le_max) ``` huffman@29539 ` 362` huffman@29453 ` 363` ```lemma degree_add_less: ``` huffman@29453 ` 364` ``` "\degree p < n; degree q < n\ \ degree (p + q) < n" ``` huffman@29539 ` 365` ``` by (auto intro: le_less_trans degree_add_le_max) ``` huffman@29453 ` 366` huffman@29451 ` 367` ```lemma degree_add_eq_right: ``` huffman@29451 ` 368` ``` "degree p < degree q \ degree (p + q) = degree q" ``` huffman@29451 ` 369` ``` apply (cases "q = 0", simp) ``` huffman@29451 ` 370` ``` apply (rule order_antisym) ``` huffman@29539 ` 371` ``` apply (simp add: degree_add_le) ``` huffman@29451 ` 372` ``` apply (rule le_degree) ``` huffman@29451 ` 373` ``` apply (simp add: coeff_eq_0) ``` huffman@29451 ` 374` ``` done ``` huffman@29451 ` 375` huffman@29451 ` 376` ```lemma degree_add_eq_left: ``` huffman@29451 ` 377` ``` "degree q < degree p \ degree (p + q) = degree p" ``` huffman@29451 ` 378` ``` using degree_add_eq_right [of q p] ``` huffman@29451 ` 379` ``` by (simp add: add_commute) ``` huffman@29451 ` 380` huffman@29451 ` 381` ```lemma degree_minus [simp]: "degree (- p) = degree p" ``` huffman@29451 ` 382` ``` unfolding degree_def by simp ``` huffman@29451 ` 383` huffman@29539 ` 384` ```lemma degree_diff_le_max: "degree (p - q) \ max (degree p) (degree q)" ``` huffman@29451 ` 385` ``` using degree_add_le [where p=p and q="-q"] ``` huffman@29451 ` 386` ``` by (simp add: diff_minus) ``` huffman@29451 ` 387` huffman@29539 ` 388` ```lemma degree_diff_le: ``` huffman@29539 ` 389` ``` "\degree p \ n; degree q \ n\ \ degree (p - q) \ n" ``` huffman@29539 ` 390` ``` by (simp add: diff_minus degree_add_le) ``` huffman@29539 ` 391` huffman@29453 ` 392` ```lemma degree_diff_less: ``` huffman@29453 ` 393` ``` "\degree p < n; degree q < n\ \ degree (p - q) < n" ``` huffman@29539 ` 394` ``` by (simp add: diff_minus degree_add_less) ``` huffman@29453 ` 395` huffman@29451 ` 396` ```lemma add_monom: "monom a n + monom b n = monom (a + b) n" ``` huffman@29451 ` 397` ``` by (rule poly_ext) simp ``` huffman@29451 ` 398` huffman@29451 ` 399` ```lemma diff_monom: "monom a n - monom b n = monom (a - b) n" ``` huffman@29451 ` 400` ``` by (rule poly_ext) simp ``` huffman@29451 ` 401` huffman@29451 ` 402` ```lemma minus_monom: "- monom a n = monom (-a) n" ``` huffman@29451 ` 403` ``` by (rule poly_ext) simp ``` huffman@29451 ` 404` huffman@29451 ` 405` ```lemma coeff_setsum: "coeff (\x\A. p x) i = (\x\A. coeff (p x) i)" ``` huffman@29451 ` 406` ``` by (cases "finite A", induct set: finite, simp_all) ``` huffman@29451 ` 407` huffman@29451 ` 408` ```lemma monom_setsum: "monom (\x\A. a x) n = (\x\A. monom (a x) n)" ``` huffman@29451 ` 409` ``` by (rule poly_ext) (simp add: coeff_setsum) ``` huffman@29451 ` 410` huffman@29451 ` 411` huffman@29451 ` 412` ```subsection {* Multiplication by a constant *} ``` huffman@29451 ` 413` huffman@29451 ` 414` ```definition ``` huffman@29451 ` 415` ``` smult :: "'a::comm_semiring_0 \ 'a poly \ 'a poly" where ``` huffman@29451 ` 416` ``` "smult a p = Abs_poly (\n. a * coeff p n)" ``` huffman@29451 ` 417` huffman@29451 ` 418` ```lemma Poly_smult: ``` huffman@29451 ` 419` ``` fixes f :: "nat \ 'a::comm_semiring_0" ``` huffman@29451 ` 420` ``` shows "f \ Poly \ (\n. a * f n) \ Poly" ``` huffman@29451 ` 421` ``` unfolding Poly_def ``` huffman@29451 ` 422` ``` by (clarify, rule_tac x=n in exI, simp) ``` huffman@29451 ` 423` huffman@29451 ` 424` ```lemma coeff_smult [simp]: "coeff (smult a p) n = a * coeff p n" ``` huffman@29451 ` 425` ``` unfolding smult_def ``` huffman@29451 ` 426` ``` by (simp add: Abs_poly_inverse Poly_smult coeff) ``` huffman@29451 ` 427` huffman@29451 ` 428` ```lemma degree_smult_le: "degree (smult a p) \ degree p" ``` huffman@29451 ` 429` ``` by (rule degree_le, simp add: coeff_eq_0) ``` huffman@29451 ` 430` huffman@29472 ` 431` ```lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" ``` huffman@29451 ` 432` ``` by (rule poly_ext, simp add: mult_assoc) ``` huffman@29451 ` 433` huffman@29451 ` 434` ```lemma smult_0_right [simp]: "smult a 0 = 0" ``` huffman@29451 ` 435` ``` by (rule poly_ext, simp) ``` huffman@29451 ` 436` huffman@29451 ` 437` ```lemma smult_0_left [simp]: "smult 0 p = 0" ``` huffman@29451 ` 438` ``` by (rule poly_ext, simp) ``` huffman@29451 ` 439` huffman@29451 ` 440` ```lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" ``` huffman@29451 ` 441` ``` by (rule poly_ext, simp) ``` huffman@29451 ` 442` huffman@29451 ` 443` ```lemma smult_add_right: ``` huffman@29451 ` 444` ``` "smult a (p + q) = smult a p + smult a q" ``` nipkow@29667 ` 445` ``` by (rule poly_ext, simp add: algebra_simps) ``` huffman@29451 ` 446` huffman@29451 ` 447` ```lemma smult_add_left: ``` huffman@29451 ` 448` ``` "smult (a + b) p = smult a p + smult b p" ``` nipkow@29667 ` 449` ``` by (rule poly_ext, simp add: algebra_simps) ``` huffman@29451 ` 450` huffman@29457 ` 451` ```lemma smult_minus_right [simp]: ``` huffman@29451 ` 452` ``` "smult (a::'a::comm_ring) (- p) = - smult a p" ``` huffman@29451 ` 453` ``` by (rule poly_ext, simp) ``` huffman@29451 ` 454` huffman@29457 ` 455` ```lemma smult_minus_left [simp]: ``` huffman@29451 ` 456` ``` "smult (- a::'a::comm_ring) p = - smult a p" ``` huffman@29451 ` 457` ``` by (rule poly_ext, simp) ``` huffman@29451 ` 458` huffman@29451 ` 459` ```lemma smult_diff_right: ``` huffman@29451 ` 460` ``` "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" ``` nipkow@29667 ` 461` ``` by (rule poly_ext, simp add: algebra_simps) ``` huffman@29451 ` 462` huffman@29451 ` 463` ```lemma smult_diff_left: ``` huffman@29451 ` 464` ``` "smult (a - b::'a::comm_ring) p = smult a p - smult b p" ``` nipkow@29667 ` 465` ``` by (rule poly_ext, simp add: algebra_simps) ``` huffman@29451 ` 466` huffman@29472 ` 467` ```lemmas smult_distribs = ``` huffman@29472 ` 468` ``` smult_add_left smult_add_right ``` huffman@29472 ` 469` ``` smult_diff_left smult_diff_right ``` huffman@29472 ` 470` huffman@29451 ` 471` ```lemma smult_pCons [simp]: ``` huffman@29451 ` 472` ``` "smult a (pCons b p) = pCons (a * b) (smult a p)" ``` huffman@29451 ` 473` ``` by (rule poly_ext, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 474` huffman@29451 ` 475` ```lemma smult_monom: "smult a (monom b n) = monom (a * b) n" ``` huffman@29451 ` 476` ``` by (induct n, simp add: monom_0, simp add: monom_Suc) ``` huffman@29451 ` 477` huffman@29659 ` 478` ```lemma degree_smult_eq [simp]: ``` huffman@29659 ` 479` ``` fixes a :: "'a::idom" ``` huffman@29659 ` 480` ``` shows "degree (smult a p) = (if a = 0 then 0 else degree p)" ``` huffman@29659 ` 481` ``` by (cases "a = 0", simp, simp add: degree_def) ``` huffman@29659 ` 482` huffman@29659 ` 483` ```lemma smult_eq_0_iff [simp]: ``` huffman@29659 ` 484` ``` fixes a :: "'a::idom" ``` huffman@29659 ` 485` ``` shows "smult a p = 0 \ a = 0 \ p = 0" ``` huffman@29659 ` 486` ``` by (simp add: expand_poly_eq) ``` huffman@29659 ` 487` huffman@29451 ` 488` huffman@29451 ` 489` ```subsection {* Multiplication of polynomials *} ``` huffman@29451 ` 490` huffman@29474 ` 491` ```text {* TODO: move to SetInterval.thy *} ``` huffman@29451 ` 492` ```lemma setsum_atMost_Suc_shift: ``` huffman@29451 ` 493` ``` fixes f :: "nat \ 'a::comm_monoid_add" ``` huffman@29451 ` 494` ``` shows "(\i\Suc n. f i) = f 0 + (\i\n. f (Suc i))" ``` huffman@29451 ` 495` ```proof (induct n) ``` huffman@29451 ` 496` ``` case 0 show ?case by simp ``` huffman@29451 ` 497` ```next ``` huffman@29451 ` 498` ``` case (Suc n) note IH = this ``` huffman@29451 ` 499` ``` have "(\i\Suc (Suc n). f i) = (\i\Suc n. f i) + f (Suc (Suc n))" ``` huffman@29451 ` 500` ``` by (rule setsum_atMost_Suc) ``` huffman@29451 ` 501` ``` also have "(\i\Suc n. f i) = f 0 + (\i\n. f (Suc i))" ``` huffman@29451 ` 502` ``` by (rule IH) ``` huffman@29451 ` 503` ``` also have "f 0 + (\i\n. f (Suc i)) + f (Suc (Suc n)) = ``` huffman@29451 ` 504` ``` f 0 + ((\i\n. f (Suc i)) + f (Suc (Suc n)))" ``` huffman@29451 ` 505` ``` by (rule add_assoc) ``` huffman@29451 ` 506` ``` also have "(\i\n. f (Suc i)) + f (Suc (Suc n)) = (\i\Suc n. f (Suc i))" ``` huffman@29451 ` 507` ``` by (rule setsum_atMost_Suc [symmetric]) ``` huffman@29451 ` 508` ``` finally show ?case . ``` huffman@29451 ` 509` ```qed ``` huffman@29451 ` 510` huffman@29451 ` 511` ```instantiation poly :: (comm_semiring_0) comm_semiring_0 ``` huffman@29451 ` 512` ```begin ``` huffman@29451 ` 513` huffman@29451 ` 514` ```definition ``` huffman@29475 ` 515` ``` times_poly_def [code del]: ``` huffman@29474 ` 516` ``` "p * q = poly_rec 0 (\a p pq. smult a q + pCons 0 pq) p" ``` huffman@29474 ` 517` huffman@29474 ` 518` ```lemma mult_poly_0_left: "(0::'a poly) * q = 0" ``` huffman@29474 ` 519` ``` unfolding times_poly_def by (simp add: poly_rec_0) ``` huffman@29474 ` 520` huffman@29474 ` 521` ```lemma mult_pCons_left [simp]: ``` huffman@29474 ` 522` ``` "pCons a p * q = smult a q + pCons 0 (p * q)" ``` huffman@29474 ` 523` ``` unfolding times_poly_def by (simp add: poly_rec_pCons) ``` huffman@29474 ` 524` huffman@29474 ` 525` ```lemma mult_poly_0_right: "p * (0::'a poly) = 0" ``` huffman@29474 ` 526` ``` by (induct p, simp add: mult_poly_0_left, simp) ``` huffman@29451 ` 527` huffman@29474 ` 528` ```lemma mult_pCons_right [simp]: ``` huffman@29474 ` 529` ``` "p * pCons a q = smult a p + pCons 0 (p * q)" ``` nipkow@29667 ` 530` ``` by (induct p, simp add: mult_poly_0_left, simp add: algebra_simps) ``` huffman@29474 ` 531` huffman@29474 ` 532` ```lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right ``` huffman@29474 ` 533` huffman@29474 ` 534` ```lemma mult_smult_left [simp]: "smult a p * q = smult a (p * q)" ``` huffman@29474 ` 535` ``` by (induct p, simp add: mult_poly_0, simp add: smult_add_right) ``` huffman@29474 ` 536` huffman@29474 ` 537` ```lemma mult_smult_right [simp]: "p * smult a q = smult a (p * q)" ``` huffman@29474 ` 538` ``` by (induct q, simp add: mult_poly_0, simp add: smult_add_right) ``` huffman@29474 ` 539` huffman@29474 ` 540` ```lemma mult_poly_add_left: ``` huffman@29474 ` 541` ``` fixes p q r :: "'a poly" ``` huffman@29474 ` 542` ``` shows "(p + q) * r = p * r + q * r" ``` huffman@29474 ` 543` ``` by (induct r, simp add: mult_poly_0, ``` nipkow@29667 ` 544` ``` simp add: smult_distribs algebra_simps) ``` huffman@29451 ` 545` huffman@29451 ` 546` ```instance proof ``` huffman@29451 ` 547` ``` fix p q r :: "'a poly" ``` huffman@29451 ` 548` ``` show 0: "0 * p = 0" ``` huffman@29474 ` 549` ``` by (rule mult_poly_0_left) ``` huffman@29451 ` 550` ``` show "p * 0 = 0" ``` huffman@29474 ` 551` ``` by (rule mult_poly_0_right) ``` huffman@29451 ` 552` ``` show "(p + q) * r = p * r + q * r" ``` huffman@29474 ` 553` ``` by (rule mult_poly_add_left) ``` huffman@29451 ` 554` ``` show "(p * q) * r = p * (q * r)" ``` huffman@29474 ` 555` ``` by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) ``` huffman@29451 ` 556` ``` show "p * q = q * p" ``` huffman@29474 ` 557` ``` by (induct p, simp add: mult_poly_0, simp) ``` huffman@29451 ` 558` ```qed ``` huffman@29451 ` 559` huffman@29451 ` 560` ```end ``` huffman@29451 ` 561` huffman@29540 ` 562` ```instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. ``` huffman@29540 ` 563` huffman@29474 ` 564` ```lemma coeff_mult: ``` huffman@29474 ` 565` ``` "coeff (p * q) n = (\i\n. coeff p i * coeff q (n-i))" ``` huffman@29474 ` 566` ```proof (induct p arbitrary: n) ``` huffman@29474 ` 567` ``` case 0 show ?case by simp ``` huffman@29474 ` 568` ```next ``` huffman@29474 ` 569` ``` case (pCons a p n) thus ?case ``` huffman@29474 ` 570` ``` by (cases n, simp, simp add: setsum_atMost_Suc_shift ``` huffman@29474 ` 571` ``` del: setsum_atMost_Suc) ``` huffman@29474 ` 572` ```qed ``` huffman@29451 ` 573` huffman@29474 ` 574` ```lemma degree_mult_le: "degree (p * q) \ degree p + degree q" ``` huffman@29474 ` 575` ```apply (rule degree_le) ``` huffman@29474 ` 576` ```apply (induct p) ``` huffman@29474 ` 577` ```apply simp ``` huffman@29474 ` 578` ```apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) ``` huffman@29451 ` 579` ```done ``` huffman@29451 ` 580` huffman@29451 ` 581` ```lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" ``` huffman@29451 ` 582` ``` by (induct m, simp add: monom_0 smult_monom, simp add: monom_Suc) ``` huffman@29451 ` 583` huffman@29451 ` 584` huffman@29451 ` 585` ```subsection {* The unit polynomial and exponentiation *} ``` huffman@29451 ` 586` huffman@29451 ` 587` ```instantiation poly :: (comm_semiring_1) comm_semiring_1 ``` huffman@29451 ` 588` ```begin ``` huffman@29451 ` 589` huffman@29451 ` 590` ```definition ``` huffman@29451 ` 591` ``` one_poly_def: ``` huffman@29451 ` 592` ``` "1 = pCons 1 0" ``` huffman@29451 ` 593` huffman@29451 ` 594` ```instance proof ``` huffman@29451 ` 595` ``` fix p :: "'a poly" show "1 * p = p" ``` huffman@29451 ` 596` ``` unfolding one_poly_def ``` huffman@29451 ` 597` ``` by simp ``` huffman@29451 ` 598` ```next ``` huffman@29451 ` 599` ``` show "0 \ (1::'a poly)" ``` huffman@29451 ` 600` ``` unfolding one_poly_def by simp ``` huffman@29451 ` 601` ```qed ``` huffman@29451 ` 602` huffman@29451 ` 603` ```end ``` huffman@29451 ` 604` huffman@29540 ` 605` ```instance poly :: (comm_semiring_1_cancel) comm_semiring_1_cancel .. ``` huffman@29540 ` 606` huffman@29451 ` 607` ```lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" ``` huffman@29451 ` 608` ``` unfolding one_poly_def ``` huffman@29451 ` 609` ``` by (simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 610` huffman@29451 ` 611` ```lemma degree_1 [simp]: "degree 1 = 0" ``` huffman@29451 ` 612` ``` unfolding one_poly_def ``` huffman@29451 ` 613` ``` by (rule degree_pCons_0) ``` huffman@29451 ` 614` huffman@29451 ` 615` ```instantiation poly :: (comm_semiring_1) recpower ``` huffman@29451 ` 616` ```begin ``` huffman@29451 ` 617` huffman@29451 ` 618` ```primrec power_poly where ``` huffman@29451 ` 619` ``` power_poly_0: "(p::'a poly) ^ 0 = 1" ``` huffman@29451 ` 620` ```| power_poly_Suc: "(p::'a poly) ^ (Suc n) = p * p ^ n" ``` huffman@29451 ` 621` huffman@29451 ` 622` ```instance ``` huffman@29451 ` 623` ``` by default simp_all ``` huffman@29451 ` 624` huffman@29451 ` 625` ```end ``` huffman@29451 ` 626` huffman@29451 ` 627` ```instance poly :: (comm_ring) comm_ring .. ``` huffman@29451 ` 628` huffman@29451 ` 629` ```instance poly :: (comm_ring_1) comm_ring_1 .. ``` huffman@29451 ` 630` huffman@29451 ` 631` ```instantiation poly :: (comm_ring_1) number_ring ``` huffman@29451 ` 632` ```begin ``` huffman@29451 ` 633` huffman@29451 ` 634` ```definition ``` huffman@29451 ` 635` ``` "number_of k = (of_int k :: 'a poly)" ``` huffman@29451 ` 636` huffman@29451 ` 637` ```instance ``` huffman@29451 ` 638` ``` by default (rule number_of_poly_def) ``` huffman@29451 ` 639` huffman@29451 ` 640` ```end ``` huffman@29451 ` 641` huffman@29451 ` 642` huffman@29451 ` 643` ```subsection {* Polynomials form an integral domain *} ``` huffman@29451 ` 644` huffman@29451 ` 645` ```lemma coeff_mult_degree_sum: ``` huffman@29451 ` 646` ``` "coeff (p * q) (degree p + degree q) = ``` huffman@29451 ` 647` ``` coeff p (degree p) * coeff q (degree q)" ``` huffman@29471 ` 648` ``` by (induct p, simp, simp add: coeff_eq_0) ``` huffman@29451 ` 649` huffman@29451 ` 650` ```instance poly :: (idom) idom ``` huffman@29451 ` 651` ```proof ``` huffman@29451 ` 652` ``` fix p q :: "'a poly" ``` huffman@29451 ` 653` ``` assume "p \ 0" and "q \ 0" ``` huffman@29451 ` 654` ``` have "coeff (p * q) (degree p + degree q) = ``` huffman@29451 ` 655` ``` coeff p (degree p) * coeff q (degree q)" ``` huffman@29451 ` 656` ``` by (rule coeff_mult_degree_sum) ``` huffman@29451 ` 657` ``` also have "coeff p (degree p) * coeff q (degree q) \ 0" ``` huffman@29451 ` 658` ``` using `p \ 0` and `q \ 0` by simp ``` huffman@29451 ` 659` ``` finally have "\n. coeff (p * q) n \ 0" .. ``` huffman@29451 ` 660` ``` thus "p * q \ 0" by (simp add: expand_poly_eq) ``` huffman@29451 ` 661` ```qed ``` huffman@29451 ` 662` huffman@29451 ` 663` ```lemma degree_mult_eq: ``` huffman@29451 ` 664` ``` fixes p q :: "'a::idom poly" ``` huffman@29451 ` 665` ``` shows "\p \ 0; q \ 0\ \ degree (p * q) = degree p + degree q" ``` huffman@29451 ` 666` ```apply (rule order_antisym [OF degree_mult_le le_degree]) ``` huffman@29451 ` 667` ```apply (simp add: coeff_mult_degree_sum) ``` huffman@29451 ` 668` ```done ``` huffman@29451 ` 669` huffman@29451 ` 670` ```lemma dvd_imp_degree_le: ``` huffman@29451 ` 671` ``` fixes p q :: "'a::idom poly" ``` huffman@29451 ` 672` ``` shows "\p dvd q; q \ 0\ \ degree p \ degree q" ``` huffman@29451 ` 673` ``` by (erule dvdE, simp add: degree_mult_eq) ``` huffman@29451 ` 674` huffman@29451 ` 675` huffman@29878 ` 676` ```subsection {* Polynomials form an ordered integral domain *} ``` huffman@29878 ` 677` huffman@29878 ` 678` ```definition ``` huffman@29878 ` 679` ``` pos_poly :: "'a::ordered_idom poly \ bool" ``` huffman@29878 ` 680` ```where ``` huffman@29878 ` 681` ``` "pos_poly p \ 0 < coeff p (degree p)" ``` huffman@29878 ` 682` huffman@29878 ` 683` ```lemma pos_poly_pCons: ``` huffman@29878 ` 684` ``` "pos_poly (pCons a p) \ pos_poly p \ (p = 0 \ 0 < a)" ``` huffman@29878 ` 685` ``` unfolding pos_poly_def by simp ``` huffman@29878 ` 686` huffman@29878 ` 687` ```lemma not_pos_poly_0 [simp]: "\ pos_poly 0" ``` huffman@29878 ` 688` ``` unfolding pos_poly_def by simp ``` huffman@29878 ` 689` huffman@29878 ` 690` ```lemma pos_poly_add: "\pos_poly p; pos_poly q\ \ pos_poly (p + q)" ``` huffman@29878 ` 691` ``` apply (induct p arbitrary: q, simp) ``` huffman@29878 ` 692` ``` apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos) ``` huffman@29878 ` 693` ``` done ``` huffman@29878 ` 694` huffman@29878 ` 695` ```lemma pos_poly_mult: "\pos_poly p; pos_poly q\ \ pos_poly (p * q)" ``` huffman@29878 ` 696` ``` unfolding pos_poly_def ``` huffman@29878 ` 697` ``` apply (subgoal_tac "p \ 0 \ q \ 0") ``` huffman@29878 ` 698` ``` apply (simp add: degree_mult_eq coeff_mult_degree_sum mult_pos_pos) ``` huffman@29878 ` 699` ``` apply auto ``` huffman@29878 ` 700` ``` done ``` huffman@29878 ` 701` huffman@29878 ` 702` ```lemma pos_poly_total: "p = 0 \ pos_poly p \ pos_poly (- p)" ``` huffman@29878 ` 703` ```by (induct p) (auto simp add: pos_poly_pCons) ``` huffman@29878 ` 704` huffman@29878 ` 705` ```instantiation poly :: (ordered_idom) ordered_idom ``` huffman@29878 ` 706` ```begin ``` huffman@29878 ` 707` huffman@29878 ` 708` ```definition ``` huffman@29878 ` 709` ``` [code del]: ``` huffman@29878 ` 710` ``` "x < y \ pos_poly (y - x)" ``` huffman@29878 ` 711` huffman@29878 ` 712` ```definition ``` huffman@29878 ` 713` ``` [code del]: ``` huffman@29878 ` 714` ``` "x \ y \ x = y \ pos_poly (y - x)" ``` huffman@29878 ` 715` huffman@29878 ` 716` ```definition ``` huffman@29878 ` 717` ``` [code del]: ``` huffman@29878 ` 718` ``` "abs (x::'a poly) = (if x < 0 then - x else x)" ``` huffman@29878 ` 719` huffman@29878 ` 720` ```definition ``` huffman@29878 ` 721` ``` [code del]: ``` huffman@29878 ` 722` ``` "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" ``` huffman@29878 ` 723` huffman@29878 ` 724` ```instance proof ``` huffman@29878 ` 725` ``` fix x y :: "'a poly" ``` huffman@29878 ` 726` ``` show "x < y \ x \ y \ \ y \ x" ``` huffman@29878 ` 727` ``` unfolding less_eq_poly_def less_poly_def ``` huffman@29878 ` 728` ``` apply safe ``` huffman@29878 ` 729` ``` apply simp ``` huffman@29878 ` 730` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 731` ``` apply simp ``` huffman@29878 ` 732` ``` done ``` huffman@29878 ` 733` ```next ``` huffman@29878 ` 734` ``` fix x :: "'a poly" show "x \ x" ``` huffman@29878 ` 735` ``` unfolding less_eq_poly_def by simp ``` huffman@29878 ` 736` ```next ``` huffman@29878 ` 737` ``` fix x y z :: "'a poly" ``` huffman@29878 ` 738` ``` assume "x \ y" and "y \ z" thus "x \ z" ``` huffman@29878 ` 739` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 740` ``` apply safe ``` huffman@29878 ` 741` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 742` ``` apply (simp add: algebra_simps) ``` huffman@29878 ` 743` ``` done ``` huffman@29878 ` 744` ```next ``` huffman@29878 ` 745` ``` fix x y :: "'a poly" ``` huffman@29878 ` 746` ``` assume "x \ y" and "y \ x" thus "x = y" ``` huffman@29878 ` 747` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 748` ``` apply safe ``` huffman@29878 ` 749` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 750` ``` apply simp ``` huffman@29878 ` 751` ``` done ``` huffman@29878 ` 752` ```next ``` huffman@29878 ` 753` ``` fix x y z :: "'a poly" ``` huffman@29878 ` 754` ``` assume "x \ y" thus "z + x \ z + y" ``` huffman@29878 ` 755` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 756` ``` apply safe ``` huffman@29878 ` 757` ``` apply (simp add: algebra_simps) ``` huffman@29878 ` 758` ``` done ``` huffman@29878 ` 759` ```next ``` huffman@29878 ` 760` ``` fix x y :: "'a poly" ``` huffman@29878 ` 761` ``` show "x \ y \ y \ x" ``` huffman@29878 ` 762` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 763` ``` using pos_poly_total [of "x - y"] ``` huffman@29878 ` 764` ``` by auto ``` huffman@29878 ` 765` ```next ``` huffman@29878 ` 766` ``` fix x y z :: "'a poly" ``` huffman@29878 ` 767` ``` assume "x < y" and "0 < z" ``` huffman@29878 ` 768` ``` thus "z * x < z * y" ``` huffman@29878 ` 769` ``` unfolding less_poly_def ``` huffman@29878 ` 770` ``` by (simp add: right_diff_distrib [symmetric] pos_poly_mult) ``` huffman@29878 ` 771` ```next ``` huffman@29878 ` 772` ``` fix x :: "'a poly" ``` huffman@29878 ` 773` ``` show "\x\ = (if x < 0 then - x else x)" ``` huffman@29878 ` 774` ``` by (rule abs_poly_def) ``` huffman@29878 ` 775` ```next ``` huffman@29878 ` 776` ``` fix x :: "'a poly" ``` huffman@29878 ` 777` ``` show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" ``` huffman@29878 ` 778` ``` by (rule sgn_poly_def) ``` huffman@29878 ` 779` ```qed ``` huffman@29878 ` 780` huffman@29878 ` 781` ```end ``` huffman@29878 ` 782` huffman@29878 ` 783` ```text {* TODO: Simplification rules for comparisons *} ``` huffman@29878 ` 784` huffman@29878 ` 785` huffman@29451 ` 786` ```subsection {* Long division of polynomials *} ``` huffman@29451 ` 787` huffman@29451 ` 788` ```definition ``` huffman@29537 ` 789` ``` pdivmod_rel :: "'a::field poly \ 'a poly \ 'a poly \ 'a poly \ bool" ``` huffman@29451 ` 790` ```where ``` huffman@29475 ` 791` ``` [code del]: ``` huffman@29537 ` 792` ``` "pdivmod_rel x y q r \ ``` huffman@29451 ` 793` ``` x = q * y + r \ (if y = 0 then q = 0 else r = 0 \ degree r < degree y)" ``` huffman@29451 ` 794` huffman@29537 ` 795` ```lemma pdivmod_rel_0: ``` huffman@29537 ` 796` ``` "pdivmod_rel 0 y 0 0" ``` huffman@29537 ` 797` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 798` huffman@29537 ` 799` ```lemma pdivmod_rel_by_0: ``` huffman@29537 ` 800` ``` "pdivmod_rel x 0 0 x" ``` huffman@29537 ` 801` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 802` huffman@29451 ` 803` ```lemma eq_zero_or_degree_less: ``` huffman@29451 ` 804` ``` assumes "degree p \ n" and "coeff p n = 0" ``` huffman@29451 ` 805` ``` shows "p = 0 \ degree p < n" ``` huffman@29451 ` 806` ```proof (cases n) ``` huffman@29451 ` 807` ``` case 0 ``` huffman@29451 ` 808` ``` with `degree p \ n` and `coeff p n = 0` ``` huffman@29451 ` 809` ``` have "coeff p (degree p) = 0" by simp ``` huffman@29451 ` 810` ``` then have "p = 0" by simp ``` huffman@29451 ` 811` ``` then show ?thesis .. ``` huffman@29451 ` 812` ```next ``` huffman@29451 ` 813` ``` case (Suc m) ``` huffman@29451 ` 814` ``` have "\i>n. coeff p i = 0" ``` huffman@29451 ` 815` ``` using `degree p \ n` by (simp add: coeff_eq_0) ``` huffman@29451 ` 816` ``` then have "\i\n. coeff p i = 0" ``` huffman@29451 ` 817` ``` using `coeff p n = 0` by (simp add: le_less) ``` huffman@29451 ` 818` ``` then have "\i>m. coeff p i = 0" ``` huffman@29451 ` 819` ``` using `n = Suc m` by (simp add: less_eq_Suc_le) ``` huffman@29451 ` 820` ``` then have "degree p \ m" ``` huffman@29451 ` 821` ``` by (rule degree_le) ``` huffman@29451 ` 822` ``` then have "degree p < n" ``` huffman@29451 ` 823` ``` using `n = Suc m` by (simp add: less_Suc_eq_le) ``` huffman@29451 ` 824` ``` then show ?thesis .. ``` huffman@29451 ` 825` ```qed ``` huffman@29451 ` 826` huffman@29537 ` 827` ```lemma pdivmod_rel_pCons: ``` huffman@29537 ` 828` ``` assumes rel: "pdivmod_rel x y q r" ``` huffman@29451 ` 829` ``` assumes y: "y \ 0" ``` huffman@29451 ` 830` ``` assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" ``` huffman@29537 ` 831` ``` shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" ``` huffman@29537 ` 832` ``` (is "pdivmod_rel ?x y ?q ?r") ``` huffman@29451 ` 833` ```proof - ``` huffman@29451 ` 834` ``` have x: "x = q * y + r" and r: "r = 0 \ degree r < degree y" ``` huffman@29537 ` 835` ``` using assms unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 836` huffman@29451 ` 837` ``` have 1: "?x = ?q * y + ?r" ``` huffman@29451 ` 838` ``` using b x by simp ``` huffman@29451 ` 839` huffman@29451 ` 840` ``` have 2: "?r = 0 \ degree ?r < degree y" ``` huffman@29451 ` 841` ``` proof (rule eq_zero_or_degree_less) ``` huffman@29539 ` 842` ``` show "degree ?r \ degree y" ``` huffman@29539 ` 843` ``` proof (rule degree_diff_le) ``` huffman@29451 ` 844` ``` show "degree (pCons a r) \ degree y" ``` huffman@29460 ` 845` ``` using r by auto ``` huffman@29451 ` 846` ``` show "degree (smult b y) \ degree y" ``` huffman@29451 ` 847` ``` by (rule degree_smult_le) ``` huffman@29451 ` 848` ``` qed ``` huffman@29451 ` 849` ``` next ``` huffman@29451 ` 850` ``` show "coeff ?r (degree y) = 0" ``` huffman@29451 ` 851` ``` using `y \ 0` unfolding b by simp ``` huffman@29451 ` 852` ``` qed ``` huffman@29451 ` 853` huffman@29451 ` 854` ``` from 1 2 show ?thesis ``` huffman@29537 ` 855` ``` unfolding pdivmod_rel_def ``` huffman@29451 ` 856` ``` using `y \ 0` by simp ``` huffman@29451 ` 857` ```qed ``` huffman@29451 ` 858` huffman@29537 ` 859` ```lemma pdivmod_rel_exists: "\q r. pdivmod_rel x y q r" ``` huffman@29451 ` 860` ```apply (cases "y = 0") ``` huffman@29537 ` 861` ```apply (fast intro!: pdivmod_rel_by_0) ``` huffman@29451 ` 862` ```apply (induct x) ``` huffman@29537 ` 863` ```apply (fast intro!: pdivmod_rel_0) ``` huffman@29537 ` 864` ```apply (fast intro!: pdivmod_rel_pCons) ``` huffman@29451 ` 865` ```done ``` huffman@29451 ` 866` huffman@29537 ` 867` ```lemma pdivmod_rel_unique: ``` huffman@29537 ` 868` ``` assumes 1: "pdivmod_rel x y q1 r1" ``` huffman@29537 ` 869` ``` assumes 2: "pdivmod_rel x y q2 r2" ``` huffman@29451 ` 870` ``` shows "q1 = q2 \ r1 = r2" ``` huffman@29451 ` 871` ```proof (cases "y = 0") ``` huffman@29451 ` 872` ``` assume "y = 0" with assms show ?thesis ``` huffman@29537 ` 873` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 874` ```next ``` huffman@29451 ` 875` ``` assume [simp]: "y \ 0" ``` huffman@29451 ` 876` ``` from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \ degree r1 < degree y" ``` huffman@29537 ` 877` ``` unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 878` ``` from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \ degree r2 < degree y" ``` huffman@29537 ` 879` ``` unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 880` ``` from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" ``` nipkow@29667 ` 881` ``` by (simp add: algebra_simps) ``` huffman@29451 ` 882` ``` from r1 r2 have r3: "(r2 - r1) = 0 \ degree (r2 - r1) < degree y" ``` huffman@29453 ` 883` ``` by (auto intro: degree_diff_less) ``` huffman@29451 ` 884` huffman@29451 ` 885` ``` show "q1 = q2 \ r1 = r2" ``` huffman@29451 ` 886` ``` proof (rule ccontr) ``` huffman@29451 ` 887` ``` assume "\ (q1 = q2 \ r1 = r2)" ``` huffman@29451 ` 888` ``` with q3 have "q1 \ q2" and "r1 \ r2" by auto ``` huffman@29451 ` 889` ``` with r3 have "degree (r2 - r1) < degree y" by simp ``` huffman@29451 ` 890` ``` also have "degree y \ degree (q1 - q2) + degree y" by simp ``` huffman@29451 ` 891` ``` also have "\ = degree ((q1 - q2) * y)" ``` huffman@29451 ` 892` ``` using `q1 \ q2` by (simp add: degree_mult_eq) ``` huffman@29451 ` 893` ``` also have "\ = degree (r2 - r1)" ``` huffman@29451 ` 894` ``` using q3 by simp ``` huffman@29451 ` 895` ``` finally have "degree (r2 - r1) < degree (r2 - r1)" . ``` huffman@29451 ` 896` ``` then show "False" by simp ``` huffman@29451 ` 897` ``` qed ``` huffman@29451 ` 898` ```qed ``` huffman@29451 ` 899` huffman@29660 ` 900` ```lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \ q = 0 \ r = 0" ``` huffman@29660 ` 901` ```by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0) ``` huffman@29660 ` 902` huffman@29660 ` 903` ```lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \ q = 0 \ r = x" ``` huffman@29660 ` 904` ```by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0) ``` huffman@29660 ` 905` huffman@29537 ` 906` ```lemmas pdivmod_rel_unique_div = ``` huffman@29537 ` 907` ``` pdivmod_rel_unique [THEN conjunct1, standard] ``` huffman@29451 ` 908` huffman@29537 ` 909` ```lemmas pdivmod_rel_unique_mod = ``` huffman@29537 ` 910` ``` pdivmod_rel_unique [THEN conjunct2, standard] ``` huffman@29451 ` 911` huffman@29451 ` 912` ```instantiation poly :: (field) ring_div ``` huffman@29451 ` 913` ```begin ``` huffman@29451 ` 914` huffman@29451 ` 915` ```definition div_poly where ``` huffman@29537 ` 916` ``` [code del]: "x div y = (THE q. \r. pdivmod_rel x y q r)" ``` huffman@29451 ` 917` huffman@29451 ` 918` ```definition mod_poly where ``` huffman@29537 ` 919` ``` [code del]: "x mod y = (THE r. \q. pdivmod_rel x y q r)" ``` huffman@29451 ` 920` huffman@29451 ` 921` ```lemma div_poly_eq: ``` huffman@29537 ` 922` ``` "pdivmod_rel x y q r \ x div y = q" ``` huffman@29451 ` 923` ```unfolding div_poly_def ``` huffman@29537 ` 924` ```by (fast elim: pdivmod_rel_unique_div) ``` huffman@29451 ` 925` huffman@29451 ` 926` ```lemma mod_poly_eq: ``` huffman@29537 ` 927` ``` "pdivmod_rel x y q r \ x mod y = r" ``` huffman@29451 ` 928` ```unfolding mod_poly_def ``` huffman@29537 ` 929` ```by (fast elim: pdivmod_rel_unique_mod) ``` huffman@29451 ` 930` huffman@29537 ` 931` ```lemma pdivmod_rel: ``` huffman@29537 ` 932` ``` "pdivmod_rel x y (x div y) (x mod y)" ``` huffman@29451 ` 933` ```proof - ``` huffman@29537 ` 934` ``` from pdivmod_rel_exists ``` huffman@29537 ` 935` ``` obtain q r where "pdivmod_rel x y q r" by fast ``` huffman@29451 ` 936` ``` thus ?thesis ``` huffman@29451 ` 937` ``` by (simp add: div_poly_eq mod_poly_eq) ``` huffman@29451 ` 938` ```qed ``` huffman@29451 ` 939` huffman@29451 ` 940` ```instance proof ``` huffman@29451 ` 941` ``` fix x y :: "'a poly" ``` huffman@29451 ` 942` ``` show "x div y * y + x mod y = x" ``` huffman@29537 ` 943` ``` using pdivmod_rel [of x y] ``` huffman@29537 ` 944` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 945` ```next ``` huffman@29451 ` 946` ``` fix x :: "'a poly" ``` huffman@29537 ` 947` ``` have "pdivmod_rel x 0 0 x" ``` huffman@29537 ` 948` ``` by (rule pdivmod_rel_by_0) ``` huffman@29451 ` 949` ``` thus "x div 0 = 0" ``` huffman@29451 ` 950` ``` by (rule div_poly_eq) ``` huffman@29451 ` 951` ```next ``` huffman@29451 ` 952` ``` fix y :: "'a poly" ``` huffman@29537 ` 953` ``` have "pdivmod_rel 0 y 0 0" ``` huffman@29537 ` 954` ``` by (rule pdivmod_rel_0) ``` huffman@29451 ` 955` ``` thus "0 div y = 0" ``` huffman@29451 ` 956` ``` by (rule div_poly_eq) ``` huffman@29451 ` 957` ```next ``` huffman@29451 ` 958` ``` fix x y z :: "'a poly" ``` huffman@29451 ` 959` ``` assume "y \ 0" ``` huffman@29537 ` 960` ``` hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" ``` huffman@29537 ` 961` ``` using pdivmod_rel [of x y] ``` huffman@29537 ` 962` ``` by (simp add: pdivmod_rel_def left_distrib) ``` huffman@29451 ` 963` ``` thus "(x + z * y) div y = z + x div y" ``` huffman@29451 ` 964` ``` by (rule div_poly_eq) ``` huffman@29451 ` 965` ```qed ``` huffman@29451 ` 966` huffman@29451 ` 967` ```end ``` huffman@29451 ` 968` huffman@29451 ` 969` ```lemma degree_mod_less: ``` huffman@29451 ` 970` ``` "y \ 0 \ x mod y = 0 \ degree (x mod y) < degree y" ``` huffman@29537 ` 971` ``` using pdivmod_rel [of x y] ``` huffman@29537 ` 972` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 973` huffman@29451 ` 974` ```lemma div_poly_less: "degree x < degree y \ x div y = 0" ``` huffman@29451 ` 975` ```proof - ``` huffman@29451 ` 976` ``` assume "degree x < degree y" ``` huffman@29537 ` 977` ``` hence "pdivmod_rel x y 0 x" ``` huffman@29537 ` 978` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 979` ``` thus "x div y = 0" by (rule div_poly_eq) ``` huffman@29451 ` 980` ```qed ``` huffman@29451 ` 981` huffman@29451 ` 982` ```lemma mod_poly_less: "degree x < degree y \ x mod y = x" ``` huffman@29451 ` 983` ```proof - ``` huffman@29451 ` 984` ``` assume "degree x < degree y" ``` huffman@29537 ` 985` ``` hence "pdivmod_rel x y 0 x" ``` huffman@29537 ` 986` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 987` ``` thus "x mod y = x" by (rule mod_poly_eq) ``` huffman@29451 ` 988` ```qed ``` huffman@29451 ` 989` huffman@29659 ` 990` ```lemma pdivmod_rel_smult_left: ``` huffman@29659 ` 991` ``` "pdivmod_rel x y q r ``` huffman@29659 ` 992` ``` \ pdivmod_rel (smult a x) y (smult a q) (smult a r)" ``` huffman@29659 ` 993` ``` unfolding pdivmod_rel_def by (simp add: smult_add_right) ``` huffman@29659 ` 994` huffman@29659 ` 995` ```lemma div_smult_left: "(smult a x) div y = smult a (x div y)" ``` huffman@29659 ` 996` ``` by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) ``` huffman@29659 ` 997` huffman@29659 ` 998` ```lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" ``` huffman@29659 ` 999` ``` by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) ``` huffman@29659 ` 1000` huffman@29659 ` 1001` ```lemma pdivmod_rel_smult_right: ``` huffman@29659 ` 1002` ``` "\a \ 0; pdivmod_rel x y q r\ ``` huffman@29659 ` 1003` ``` \ pdivmod_rel x (smult a y) (smult (inverse a) q) r" ``` huffman@29659 ` 1004` ``` unfolding pdivmod_rel_def by simp ``` huffman@29659 ` 1005` huffman@29659 ` 1006` ```lemma div_smult_right: ``` huffman@29659 ` 1007` ``` "a \ 0 \ x div (smult a y) = smult (inverse a) (x div y)" ``` huffman@29659 ` 1008` ``` by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) ``` huffman@29659 ` 1009` huffman@29659 ` 1010` ```lemma mod_smult_right: "a \ 0 \ x mod (smult a y) = x mod y" ``` huffman@29659 ` 1011` ``` by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) ``` huffman@29659 ` 1012` huffman@29660 ` 1013` ```lemma pdivmod_rel_mult: ``` huffman@29660 ` 1014` ``` "\pdivmod_rel x y q r; pdivmod_rel q z q' r'\ ``` huffman@29660 ` 1015` ``` \ pdivmod_rel x (y * z) q' (y * r' + r)" ``` huffman@29660 ` 1016` ```apply (cases "z = 0", simp add: pdivmod_rel_def) ``` huffman@29660 ` 1017` ```apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff) ``` huffman@29660 ` 1018` ```apply (cases "r = 0") ``` huffman@29660 ` 1019` ```apply (cases "r' = 0") ``` huffman@29660 ` 1020` ```apply (simp add: pdivmod_rel_def) ``` huffman@29660 ` 1021` ```apply (simp add: pdivmod_rel_def ring_simps degree_mult_eq) ``` huffman@29660 ` 1022` ```apply (cases "r' = 0") ``` huffman@29660 ` 1023` ```apply (simp add: pdivmod_rel_def degree_mult_eq) ``` huffman@29660 ` 1024` ```apply (simp add: pdivmod_rel_def ring_simps) ``` huffman@29660 ` 1025` ```apply (simp add: degree_mult_eq degree_add_less) ``` huffman@29660 ` 1026` ```done ``` huffman@29660 ` 1027` huffman@29660 ` 1028` ```lemma poly_div_mult_right: ``` huffman@29660 ` 1029` ``` fixes x y z :: "'a::field poly" ``` huffman@29660 ` 1030` ``` shows "x div (y * z) = (x div y) div z" ``` huffman@29660 ` 1031` ``` by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) ``` huffman@29660 ` 1032` huffman@29660 ` 1033` ```lemma poly_mod_mult_right: ``` huffman@29660 ` 1034` ``` fixes x y z :: "'a::field poly" ``` huffman@29660 ` 1035` ``` shows "x mod (y * z) = y * (x div y mod z) + x mod y" ``` huffman@29660 ` 1036` ``` by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) ``` huffman@29660 ` 1037` huffman@29451 ` 1038` ```lemma mod_pCons: ``` huffman@29451 ` 1039` ``` fixes a and x ``` huffman@29451 ` 1040` ``` assumes y: "y \ 0" ``` huffman@29451 ` 1041` ``` defines b: "b \ coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" ``` huffman@29451 ` 1042` ``` shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" ``` huffman@29451 ` 1043` ```unfolding b ``` huffman@29451 ` 1044` ```apply (rule mod_poly_eq) ``` huffman@29537 ` 1045` ```apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) ``` huffman@29451 ` 1046` ```done ``` huffman@29451 ` 1047` huffman@29451 ` 1048` huffman@29451 ` 1049` ```subsection {* Evaluation of polynomials *} ``` huffman@29451 ` 1050` huffman@29451 ` 1051` ```definition ``` huffman@29454 ` 1052` ``` poly :: "'a::comm_semiring_0 poly \ 'a \ 'a" where ``` huffman@29454 ` 1053` ``` "poly = poly_rec (\x. 0) (\a p f x. a + x * f x)" ``` huffman@29451 ` 1054` huffman@29451 ` 1055` ```lemma poly_0 [simp]: "poly 0 x = 0" ``` huffman@29454 ` 1056` ``` unfolding poly_def by (simp add: poly_rec_0) ``` huffman@29451 ` 1057` huffman@29451 ` 1058` ```lemma poly_pCons [simp]: "poly (pCons a p) x = a + x * poly p x" ``` huffman@29454 ` 1059` ``` unfolding poly_def by (simp add: poly_rec_pCons) ``` huffman@29451 ` 1060` huffman@29451 ` 1061` ```lemma poly_1 [simp]: "poly 1 x = 1" ``` huffman@29451 ` 1062` ``` unfolding one_poly_def by simp ``` huffman@29451 ` 1063` huffman@29454 ` 1064` ```lemma poly_monom: ``` huffman@29454 ` 1065` ``` fixes a x :: "'a::{comm_semiring_1,recpower}" ``` huffman@29454 ` 1066` ``` shows "poly (monom a n) x = a * x ^ n" ``` huffman@29451 ` 1067` ``` by (induct n, simp add: monom_0, simp add: monom_Suc power_Suc mult_ac) ``` huffman@29451 ` 1068` huffman@29451 ` 1069` ```lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" ``` huffman@29451 ` 1070` ``` apply (induct p arbitrary: q, simp) ``` nipkow@29667 ` 1071` ``` apply (case_tac q, simp, simp add: algebra_simps) ``` huffman@29451 ` 1072` ``` done ``` huffman@29451 ` 1073` huffman@29451 ` 1074` ```lemma poly_minus [simp]: ``` huffman@29454 ` 1075` ``` fixes x :: "'a::comm_ring" ``` huffman@29451 ` 1076` ``` shows "poly (- p) x = - poly p x" ``` huffman@29451 ` 1077` ``` by (induct p, simp_all) ``` huffman@29451 ` 1078` huffman@29451 ` 1079` ```lemma poly_diff [simp]: ``` huffman@29454 ` 1080` ``` fixes x :: "'a::comm_ring" ``` huffman@29451 ` 1081` ``` shows "poly (p - q) x = poly p x - poly q x" ``` huffman@29451 ` 1082` ``` by (simp add: diff_minus) ``` huffman@29451 ` 1083` huffman@29451 ` 1084` ```lemma poly_setsum: "poly (\k\A. p k) x = (\k\A. poly (p k) x)" ``` huffman@29451 ` 1085` ``` by (cases "finite A", induct set: finite, simp_all) ``` huffman@29451 ` 1086` huffman@29451 ` 1087` ```lemma poly_smult [simp]: "poly (smult a p) x = a * poly p x" ``` nipkow@29667 ` 1088` ``` by (induct p, simp, simp add: algebra_simps) ``` huffman@29451 ` 1089` huffman@29451 ` 1090` ```lemma poly_mult [simp]: "poly (p * q) x = poly p x * poly q x" ``` nipkow@29667 ` 1091` ``` by (induct p, simp_all, simp add: algebra_simps) ``` huffman@29451 ` 1092` huffman@29462 ` 1093` ```lemma poly_power [simp]: ``` huffman@29462 ` 1094` ``` fixes p :: "'a::{comm_semiring_1,recpower} poly" ``` huffman@29462 ` 1095` ``` shows "poly (p ^ n) x = poly p x ^ n" ``` huffman@29462 ` 1096` ``` by (induct n, simp, simp add: power_Suc) ``` huffman@29462 ` 1097` huffman@29456 ` 1098` huffman@29456 ` 1099` ```subsection {* Synthetic division *} ``` huffman@29456 ` 1100` huffman@29456 ` 1101` ```definition ``` huffman@29456 ` 1102` ``` synthetic_divmod :: "'a::comm_semiring_0 poly \ 'a \ 'a poly \ 'a" ``` huffman@29478 ` 1103` ```where [code del]: ``` huffman@29456 ` 1104` ``` "synthetic_divmod p c = ``` huffman@29456 ` 1105` ``` poly_rec (0, 0) (\a p (q, r). (pCons r q, a + c * r)) p" ``` huffman@29456 ` 1106` huffman@29456 ` 1107` ```definition ``` huffman@29456 ` 1108` ``` synthetic_div :: "'a::comm_semiring_0 poly \ 'a \ 'a poly" ``` huffman@29456 ` 1109` ```where ``` huffman@29456 ` 1110` ``` "synthetic_div p c = fst (synthetic_divmod p c)" ``` huffman@29456 ` 1111` huffman@29456 ` 1112` ```lemma synthetic_divmod_0 [simp]: ``` huffman@29456 ` 1113` ``` "synthetic_divmod 0 c = (0, 0)" ``` huffman@29456 ` 1114` ``` unfolding synthetic_divmod_def ``` huffman@29456 ` 1115` ``` by (simp add: poly_rec_0) ``` huffman@29456 ` 1116` huffman@29456 ` 1117` ```lemma synthetic_divmod_pCons [simp]: ``` huffman@29456 ` 1118` ``` "synthetic_divmod (pCons a p) c = ``` huffman@29456 ` 1119` ``` (\(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" ``` huffman@29456 ` 1120` ``` unfolding synthetic_divmod_def ``` huffman@29456 ` 1121` ``` by (simp add: poly_rec_pCons) ``` huffman@29456 ` 1122` huffman@29456 ` 1123` ```lemma snd_synthetic_divmod: "snd (synthetic_divmod p c) = poly p c" ``` huffman@29456 ` 1124` ``` by (induct p, simp, simp add: split_def) ``` huffman@29456 ` 1125` huffman@29456 ` 1126` ```lemma synthetic_div_0 [simp]: "synthetic_div 0 c = 0" ``` huffman@29456 ` 1127` ``` unfolding synthetic_div_def by simp ``` huffman@29456 ` 1128` huffman@29456 ` 1129` ```lemma synthetic_div_pCons [simp]: ``` huffman@29456 ` 1130` ``` "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" ``` huffman@29456 ` 1131` ``` unfolding synthetic_div_def ``` huffman@29456 ` 1132` ``` by (simp add: split_def snd_synthetic_divmod) ``` huffman@29456 ` 1133` huffman@29460 ` 1134` ```lemma synthetic_div_eq_0_iff: ``` huffman@29460 ` 1135` ``` "synthetic_div p c = 0 \ degree p = 0" ``` huffman@29460 ` 1136` ``` by (induct p, simp, case_tac p, simp) ``` huffman@29460 ` 1137` huffman@29460 ` 1138` ```lemma degree_synthetic_div: ``` huffman@29460 ` 1139` ``` "degree (synthetic_div p c) = degree p - 1" ``` huffman@29460 ` 1140` ``` by (induct p, simp, simp add: synthetic_div_eq_0_iff) ``` huffman@29460 ` 1141` huffman@29457 ` 1142` ```lemma synthetic_div_correct: ``` huffman@29456 ` 1143` ``` "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" ``` huffman@29456 ` 1144` ``` by (induct p) simp_all ``` huffman@29456 ` 1145` huffman@29457 ` 1146` ```lemma synthetic_div_unique_lemma: "smult c p = pCons a p \ p = 0" ``` huffman@29457 ` 1147` ```by (induct p arbitrary: a) simp_all ``` huffman@29457 ` 1148` huffman@29457 ` 1149` ```lemma synthetic_div_unique: ``` huffman@29457 ` 1150` ``` "p + smult c q = pCons r q \ r = poly p c \ q = synthetic_div p c" ``` huffman@29457 ` 1151` ```apply (induct p arbitrary: q r) ``` huffman@29457 ` 1152` ```apply (simp, frule synthetic_div_unique_lemma, simp) ``` huffman@29457 ` 1153` ```apply (case_tac q, force) ``` huffman@29457 ` 1154` ```done ``` huffman@29457 ` 1155` huffman@29457 ` 1156` ```lemma synthetic_div_correct': ``` huffman@29457 ` 1157` ``` fixes c :: "'a::comm_ring_1" ``` huffman@29457 ` 1158` ``` shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" ``` huffman@29457 ` 1159` ``` using synthetic_div_correct [of p c] ``` nipkow@29667 ` 1160` ``` by (simp add: algebra_simps) ``` huffman@29457 ` 1161` huffman@29460 ` 1162` ```lemma poly_eq_0_iff_dvd: ``` huffman@29460 ` 1163` ``` fixes c :: "'a::idom" ``` huffman@29460 ` 1164` ``` shows "poly p c = 0 \ [:-c, 1:] dvd p" ``` huffman@29460 ` 1165` ```proof ``` huffman@29460 ` 1166` ``` assume "poly p c = 0" ``` huffman@29460 ` 1167` ``` with synthetic_div_correct' [of c p] ``` huffman@29460 ` 1168` ``` have "p = [:-c, 1:] * synthetic_div p c" by simp ``` huffman@29460 ` 1169` ``` then show "[:-c, 1:] dvd p" .. ``` huffman@29460 ` 1170` ```next ``` huffman@29460 ` 1171` ``` assume "[:-c, 1:] dvd p" ``` huffman@29460 ` 1172` ``` then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) ``` huffman@29460 ` 1173` ``` then show "poly p c = 0" by simp ``` huffman@29460 ` 1174` ```qed ``` huffman@29460 ` 1175` huffman@29460 ` 1176` ```lemma dvd_iff_poly_eq_0: ``` huffman@29460 ` 1177` ``` fixes c :: "'a::idom" ``` huffman@29460 ` 1178` ``` shows "[:c, 1:] dvd p \ poly p (-c) = 0" ``` huffman@29460 ` 1179` ``` by (simp add: poly_eq_0_iff_dvd) ``` huffman@29460 ` 1180` huffman@29462 ` 1181` ```lemma poly_roots_finite: ``` huffman@29462 ` 1182` ``` fixes p :: "'a::idom poly" ``` huffman@29462 ` 1183` ``` shows "p \ 0 \ finite {x. poly p x = 0}" ``` huffman@29462 ` 1184` ```proof (induct n \ "degree p" arbitrary: p) ``` huffman@29462 ` 1185` ``` case (0 p) ``` huffman@29462 ` 1186` ``` then obtain a where "a \ 0" and "p = [:a:]" ``` huffman@29462 ` 1187` ``` by (cases p, simp split: if_splits) ``` huffman@29462 ` 1188` ``` then show "finite {x. poly p x = 0}" by simp ``` huffman@29462 ` 1189` ```next ``` huffman@29462 ` 1190` ``` case (Suc n p) ``` huffman@29462 ` 1191` ``` show "finite {x. poly p x = 0}" ``` huffman@29462 ` 1192` ``` proof (cases "\x. poly p x = 0") ``` huffman@29462 ` 1193` ``` case False ``` huffman@29462 ` 1194` ``` then show "finite {x. poly p x = 0}" by simp ``` huffman@29462 ` 1195` ``` next ``` huffman@29462 ` 1196` ``` case True ``` huffman@29462 ` 1197` ``` then obtain a where "poly p a = 0" .. ``` huffman@29462 ` 1198` ``` then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) ``` huffman@29462 ` 1199` ``` then obtain k where k: "p = [:-a, 1:] * k" .. ``` huffman@29462 ` 1200` ``` with `p \ 0` have "k \ 0" by auto ``` huffman@29462 ` 1201` ``` with k have "degree p = Suc (degree k)" ``` huffman@29462 ` 1202` ``` by (simp add: degree_mult_eq del: mult_pCons_left) ``` huffman@29462 ` 1203` ``` with `Suc n = degree p` have "n = degree k" by simp ``` huffman@29462 ` 1204` ``` with `k \ 0` have "finite {x. poly k x = 0}" by (rule Suc.hyps) ``` huffman@29462 ` 1205` ``` then have "finite (insert a {x. poly k x = 0})" by simp ``` huffman@29462 ` 1206` ``` then show "finite {x. poly p x = 0}" ``` huffman@29462 ` 1207` ``` by (simp add: k uminus_add_conv_diff Collect_disj_eq ``` huffman@29462 ` 1208` ``` del: mult_pCons_left) ``` huffman@29462 ` 1209` ``` qed ``` huffman@29462 ` 1210` ```qed ``` huffman@29462 ` 1211` huffman@29478 ` 1212` huffman@29478 ` 1213` ```subsection {* Configuration of the code generator *} ``` huffman@29478 ` 1214` huffman@29478 ` 1215` ```code_datatype "0::'a::zero poly" pCons ``` huffman@29478 ` 1216` huffman@29480 ` 1217` ```declare pCons_0_0 [code post] ``` huffman@29480 ` 1218` huffman@29478 ` 1219` ```instantiation poly :: ("{zero,eq}") eq ``` huffman@29478 ` 1220` ```begin ``` huffman@29478 ` 1221` huffman@29478 ` 1222` ```definition [code del]: ``` huffman@29478 ` 1223` ``` "eq_class.eq (p::'a poly) q \ p = q" ``` huffman@29478 ` 1224` huffman@29478 ` 1225` ```instance ``` huffman@29478 ` 1226` ``` by default (rule eq_poly_def) ``` huffman@29478 ` 1227` huffman@29451 ` 1228` ```end ``` huffman@29478 ` 1229` huffman@29478 ` 1230` ```lemma eq_poly_code [code]: ``` huffman@29478 ` 1231` ``` "eq_class.eq (0::_ poly) (0::_ poly) \ True" ``` huffman@29478 ` 1232` ``` "eq_class.eq (0::_ poly) (pCons b q) \ eq_class.eq 0 b \ eq_class.eq 0 q" ``` huffman@29478 ` 1233` ``` "eq_class.eq (pCons a p) (0::_ poly) \ eq_class.eq a 0 \ eq_class.eq p 0" ``` huffman@29478 ` 1234` ``` "eq_class.eq (pCons a p) (pCons b q) \ eq_class.eq a b \ eq_class.eq p q" ``` huffman@29478 ` 1235` ```unfolding eq by simp_all ``` huffman@29478 ` 1236` huffman@29478 ` 1237` ```lemmas coeff_code [code] = ``` huffman@29478 ` 1238` ``` coeff_0 coeff_pCons_0 coeff_pCons_Suc ``` huffman@29478 ` 1239` huffman@29478 ` 1240` ```lemmas degree_code [code] = ``` huffman@29478 ` 1241` ``` degree_0 degree_pCons_eq_if ``` huffman@29478 ` 1242` huffman@29478 ` 1243` ```lemmas monom_poly_code [code] = ``` huffman@29478 ` 1244` ``` monom_0 monom_Suc ``` huffman@29478 ` 1245` huffman@29478 ` 1246` ```lemma add_poly_code [code]: ``` huffman@29478 ` 1247` ``` "0 + q = (q :: _ poly)" ``` huffman@29478 ` 1248` ``` "p + 0 = (p :: _ poly)" ``` huffman@29478 ` 1249` ``` "pCons a p + pCons b q = pCons (a + b) (p + q)" ``` huffman@29478 ` 1250` ```by simp_all ``` huffman@29478 ` 1251` huffman@29478 ` 1252` ```lemma minus_poly_code [code]: ``` huffman@29478 ` 1253` ``` "- 0 = (0 :: _ poly)" ``` huffman@29478 ` 1254` ``` "- pCons a p = pCons (- a) (- p)" ``` huffman@29478 ` 1255` ```by simp_all ``` huffman@29478 ` 1256` huffman@29478 ` 1257` ```lemma diff_poly_code [code]: ``` huffman@29478 ` 1258` ``` "0 - q = (- q :: _ poly)" ``` huffman@29478 ` 1259` ``` "p - 0 = (p :: _ poly)" ``` huffman@29478 ` 1260` ``` "pCons a p - pCons b q = pCons (a - b) (p - q)" ``` huffman@29478 ` 1261` ```by simp_all ``` huffman@29478 ` 1262` huffman@29478 ` 1263` ```lemmas smult_poly_code [code] = ``` huffman@29478 ` 1264` ``` smult_0_right smult_pCons ``` huffman@29478 ` 1265` huffman@29478 ` 1266` ```lemma mult_poly_code [code]: ``` huffman@29478 ` 1267` ``` "0 * q = (0 :: _ poly)" ``` huffman@29478 ` 1268` ``` "pCons a p * q = smult a q + pCons 0 (p * q)" ``` huffman@29478 ` 1269` ```by simp_all ``` huffman@29478 ` 1270` huffman@29478 ` 1271` ```lemmas poly_code [code] = ``` huffman@29478 ` 1272` ``` poly_0 poly_pCons ``` huffman@29478 ` 1273` huffman@29478 ` 1274` ```lemmas synthetic_divmod_code [code] = ``` huffman@29478 ` 1275` ``` synthetic_divmod_0 synthetic_divmod_pCons ``` huffman@29478 ` 1276` huffman@29478 ` 1277` ```text {* code generator setup for div and mod *} ``` huffman@29478 ` 1278` huffman@29478 ` 1279` ```definition ``` huffman@29537 ` 1280` ``` pdivmod :: "'a::field poly \ 'a poly \ 'a poly \ 'a poly" ``` huffman@29478 ` 1281` ```where ``` huffman@29537 ` 1282` ``` [code del]: "pdivmod x y = (x div y, x mod y)" ``` huffman@29478 ` 1283` huffman@29537 ` 1284` ```lemma div_poly_code [code]: "x div y = fst (pdivmod x y)" ``` huffman@29537 ` 1285` ``` unfolding pdivmod_def by simp ``` huffman@29478 ` 1286` huffman@29537 ` 1287` ```lemma mod_poly_code [code]: "x mod y = snd (pdivmod x y)" ``` huffman@29537 ` 1288` ``` unfolding pdivmod_def by simp ``` huffman@29478 ` 1289` huffman@29537 ` 1290` ```lemma pdivmod_0 [code]: "pdivmod 0 y = (0, 0)" ``` huffman@29537 ` 1291` ``` unfolding pdivmod_def by simp ``` huffman@29478 ` 1292` huffman@29537 ` 1293` ```lemma pdivmod_pCons [code]: ``` huffman@29537 ` 1294` ``` "pdivmod (pCons a x) y = ``` huffman@29478 ` 1295` ``` (if y = 0 then (0, pCons a x) else ``` huffman@29537 ` 1296` ``` (let (q, r) = pdivmod x y; ``` huffman@29478 ` 1297` ``` b = coeff (pCons a r) (degree y) / coeff y (degree y) ``` huffman@29478 ` 1298` ``` in (pCons b q, pCons a r - smult b y)))" ``` huffman@29537 ` 1299` ```apply (simp add: pdivmod_def Let_def, safe) ``` huffman@29478 ` 1300` ```apply (rule div_poly_eq) ``` huffman@29537 ` 1301` ```apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) ``` huffman@29478 ` 1302` ```apply (rule mod_poly_eq) ``` huffman@29537 ` 1303` ```apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) ``` huffman@29478 ` 1304` ```done ``` huffman@29478 ` 1305` huffman@29478 ` 1306` ```end ```