src/HOL/Library/Polynomial.thy
 author wenzelm Thu Nov 05 10:39:49 2015 +0100 (2015-11-05) changeset 61585 a9599d3d7610 parent 61260 e6f03fae14d5 child 61605 1bf7b186542e permissions -rw-r--r--
isabelle update_cartouches -c -t;
 wenzelm@41959 ` 1` ```(* Title: HOL/Library/Polynomial.thy ``` huffman@29451 ` 2` ``` Author: Brian Huffman ``` wenzelm@41959 ` 3` ``` Author: Clemens Ballarin ``` haftmann@52380 ` 4` ``` Author: Florian Haftmann ``` huffman@29451 ` 5` ```*) ``` huffman@29451 ` 6` wenzelm@60500 ` 7` ```section \Polynomials as type over a ring structure\ ``` huffman@29451 ` 8` huffman@29451 ` 9` ```theory Polynomial ``` haftmann@60685 ` 10` ```imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/More_List" "~~/src/HOL/Library/Infinite_Set" ``` huffman@29451 ` 11` ```begin ``` huffman@29451 ` 12` wenzelm@60500 ` 13` ```subsection \Auxiliary: operations for lists (later) representing coefficients\ ``` haftmann@52380 ` 14` haftmann@52380 ` 15` ```definition cCons :: "'a::zero \ 'a list \ 'a list" (infixr "##" 65) ``` haftmann@52380 ` 16` ```where ``` haftmann@52380 ` 17` ``` "x ## xs = (if xs = [] \ x = 0 then [] else x # xs)" ``` haftmann@52380 ` 18` haftmann@52380 ` 19` ```lemma cCons_0_Nil_eq [simp]: ``` haftmann@52380 ` 20` ``` "0 ## [] = []" ``` haftmann@52380 ` 21` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 22` haftmann@52380 ` 23` ```lemma cCons_Cons_eq [simp]: ``` haftmann@52380 ` 24` ``` "x ## y # ys = x # y # ys" ``` haftmann@52380 ` 25` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 26` haftmann@52380 ` 27` ```lemma cCons_append_Cons_eq [simp]: ``` haftmann@52380 ` 28` ``` "x ## xs @ y # ys = x # xs @ y # ys" ``` haftmann@52380 ` 29` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 30` haftmann@52380 ` 31` ```lemma cCons_not_0_eq [simp]: ``` haftmann@52380 ` 32` ``` "x \ 0 \ x ## xs = x # xs" ``` haftmann@52380 ` 33` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 34` haftmann@52380 ` 35` ```lemma strip_while_not_0_Cons_eq [simp]: ``` haftmann@52380 ` 36` ``` "strip_while (\x. x = 0) (x # xs) = x ## strip_while (\x. x = 0) xs" ``` haftmann@52380 ` 37` ```proof (cases "x = 0") ``` haftmann@52380 ` 38` ``` case False then show ?thesis by simp ``` haftmann@52380 ` 39` ```next ``` haftmann@52380 ` 40` ``` case True show ?thesis ``` haftmann@52380 ` 41` ``` proof (induct xs rule: rev_induct) ``` haftmann@52380 ` 42` ``` case Nil with True show ?case by simp ``` haftmann@52380 ` 43` ``` next ``` haftmann@52380 ` 44` ``` case (snoc y ys) then show ?case ``` haftmann@52380 ` 45` ``` by (cases "y = 0") (simp_all add: append_Cons [symmetric] del: append_Cons) ``` haftmann@52380 ` 46` ``` qed ``` haftmann@52380 ` 47` ```qed ``` haftmann@52380 ` 48` haftmann@52380 ` 49` ```lemma tl_cCons [simp]: ``` haftmann@52380 ` 50` ``` "tl (x ## xs) = xs" ``` haftmann@52380 ` 51` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 52` wenzelm@61585 ` 53` ```subsection \Definition of type \poly\\ ``` huffman@29451 ` 54` wenzelm@61260 ` 55` ```typedef (overloaded) 'a poly = "{f :: nat \ 'a::zero. \\<^sub>\ n. f n = 0}" ``` hoelzl@59983 ` 56` ``` morphisms coeff Abs_poly by (auto intro!: ALL_MOST) ``` huffman@29451 ` 57` haftmann@59487 ` 58` ```setup_lifting type_definition_poly ``` haftmann@52380 ` 59` haftmann@52380 ` 60` ```lemma poly_eq_iff: "p = q \ (\n. coeff p n = coeff q n)" ``` wenzelm@45694 ` 61` ``` by (simp add: coeff_inject [symmetric] fun_eq_iff) ``` huffman@29451 ` 62` haftmann@52380 ` 63` ```lemma poly_eqI: "(\n. coeff p n = coeff q n) \ p = q" ``` haftmann@52380 ` 64` ``` by (simp add: poly_eq_iff) ``` haftmann@52380 ` 65` hoelzl@59983 ` 66` ```lemma MOST_coeff_eq_0: "\\<^sub>\ n. coeff p n = 0" ``` haftmann@52380 ` 67` ``` using coeff [of p] by simp ``` huffman@29451 ` 68` huffman@29451 ` 69` wenzelm@60500 ` 70` ```subsection \Degree of a polynomial\ ``` huffman@29451 ` 71` haftmann@52380 ` 72` ```definition degree :: "'a::zero poly \ nat" ``` haftmann@52380 ` 73` ```where ``` huffman@29451 ` 74` ``` "degree p = (LEAST n. \i>n. coeff p i = 0)" ``` huffman@29451 ` 75` haftmann@52380 ` 76` ```lemma coeff_eq_0: ``` haftmann@52380 ` 77` ``` assumes "degree p < n" ``` haftmann@52380 ` 78` ``` shows "coeff p n = 0" ``` huffman@29451 ` 79` ```proof - ``` hoelzl@59983 ` 80` ``` have "\n. \i>n. coeff p i = 0" ``` hoelzl@59983 ` 81` ``` using MOST_coeff_eq_0 by (simp add: MOST_nat) ``` haftmann@52380 ` 82` ``` then have "\i>degree p. coeff p i = 0" ``` huffman@29451 ` 83` ``` unfolding degree_def by (rule LeastI_ex) ``` haftmann@52380 ` 84` ``` with assms show ?thesis by simp ``` huffman@29451 ` 85` ```qed ``` huffman@29451 ` 86` huffman@29451 ` 87` ```lemma le_degree: "coeff p n \ 0 \ n \ degree p" ``` huffman@29451 ` 88` ``` by (erule contrapos_np, rule coeff_eq_0, simp) ``` huffman@29451 ` 89` huffman@29451 ` 90` ```lemma degree_le: "\i>n. coeff p i = 0 \ degree p \ n" ``` huffman@29451 ` 91` ``` unfolding degree_def by (erule Least_le) ``` huffman@29451 ` 92` huffman@29451 ` 93` ```lemma less_degree_imp: "n < degree p \ \i>n. coeff p i \ 0" ``` huffman@29451 ` 94` ``` unfolding degree_def by (drule not_less_Least, simp) ``` huffman@29451 ` 95` huffman@29451 ` 96` wenzelm@60500 ` 97` ```subsection \The zero polynomial\ ``` huffman@29451 ` 98` huffman@29451 ` 99` ```instantiation poly :: (zero) zero ``` huffman@29451 ` 100` ```begin ``` huffman@29451 ` 101` haftmann@52380 ` 102` ```lift_definition zero_poly :: "'a poly" ``` hoelzl@59983 ` 103` ``` is "\_. 0" by (rule MOST_I) simp ``` huffman@29451 ` 104` huffman@29451 ` 105` ```instance .. ``` haftmann@52380 ` 106` huffman@29451 ` 107` ```end ``` huffman@29451 ` 108` haftmann@52380 ` 109` ```lemma coeff_0 [simp]: ``` haftmann@52380 ` 110` ``` "coeff 0 n = 0" ``` haftmann@52380 ` 111` ``` by transfer rule ``` huffman@29451 ` 112` haftmann@52380 ` 113` ```lemma degree_0 [simp]: ``` haftmann@52380 ` 114` ``` "degree 0 = 0" ``` huffman@29451 ` 115` ``` by (rule order_antisym [OF degree_le le0]) simp ``` huffman@29451 ` 116` huffman@29451 ` 117` ```lemma leading_coeff_neq_0: ``` haftmann@52380 ` 118` ``` assumes "p \ 0" ``` haftmann@52380 ` 119` ``` shows "coeff p (degree p) \ 0" ``` huffman@29451 ` 120` ```proof (cases "degree p") ``` huffman@29451 ` 121` ``` case 0 ``` wenzelm@60500 ` 122` ``` from \p \ 0\ have "\n. coeff p n \ 0" ``` haftmann@52380 ` 123` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 124` ``` then obtain n where "coeff p n \ 0" .. ``` huffman@29451 ` 125` ``` hence "n \ degree p" by (rule le_degree) ``` wenzelm@60500 ` 126` ``` with \coeff p n \ 0\ and \degree p = 0\ ``` huffman@29451 ` 127` ``` show "coeff p (degree p) \ 0" by simp ``` huffman@29451 ` 128` ```next ``` huffman@29451 ` 129` ``` case (Suc n) ``` wenzelm@60500 ` 130` ``` from \degree p = Suc n\ have "n < degree p" by simp ``` huffman@29451 ` 131` ``` hence "\i>n. coeff p i \ 0" by (rule less_degree_imp) ``` huffman@29451 ` 132` ``` then obtain i where "n < i" and "coeff p i \ 0" by fast ``` wenzelm@60500 ` 133` ``` from \degree p = Suc n\ and \n < i\ have "degree p \ i" by simp ``` wenzelm@60500 ` 134` ``` also from \coeff p i \ 0\ have "i \ degree p" by (rule le_degree) ``` huffman@29451 ` 135` ``` finally have "degree p = i" . ``` wenzelm@60500 ` 136` ``` with \coeff p i \ 0\ show "coeff p (degree p) \ 0" by simp ``` huffman@29451 ` 137` ```qed ``` huffman@29451 ` 138` haftmann@52380 ` 139` ```lemma leading_coeff_0_iff [simp]: ``` haftmann@52380 ` 140` ``` "coeff p (degree p) = 0 \ p = 0" ``` huffman@29451 ` 141` ``` by (cases "p = 0", simp, simp add: leading_coeff_neq_0) ``` huffman@29451 ` 142` huffman@29451 ` 143` wenzelm@60500 ` 144` ```subsection \List-style constructor for polynomials\ ``` huffman@29451 ` 145` haftmann@52380 ` 146` ```lift_definition pCons :: "'a::zero \ 'a poly \ 'a poly" ``` blanchet@55415 ` 147` ``` is "\a p. case_nat a (coeff p)" ``` hoelzl@59983 ` 148` ``` by (rule MOST_SucD) (simp add: MOST_coeff_eq_0) ``` huffman@29451 ` 149` haftmann@52380 ` 150` ```lemmas coeff_pCons = pCons.rep_eq ``` huffman@29455 ` 151` haftmann@52380 ` 152` ```lemma coeff_pCons_0 [simp]: ``` haftmann@52380 ` 153` ``` "coeff (pCons a p) 0 = a" ``` haftmann@52380 ` 154` ``` by transfer simp ``` huffman@29455 ` 155` haftmann@52380 ` 156` ```lemma coeff_pCons_Suc [simp]: ``` haftmann@52380 ` 157` ``` "coeff (pCons a p) (Suc n) = coeff p n" ``` huffman@29451 ` 158` ``` by (simp add: coeff_pCons) ``` huffman@29451 ` 159` haftmann@52380 ` 160` ```lemma degree_pCons_le: ``` haftmann@52380 ` 161` ``` "degree (pCons a p) \ Suc (degree p)" ``` haftmann@52380 ` 162` ``` by (rule degree_le) (simp add: coeff_eq_0 coeff_pCons split: nat.split) ``` huffman@29451 ` 163` huffman@29451 ` 164` ```lemma degree_pCons_eq: ``` huffman@29451 ` 165` ``` "p \ 0 \ degree (pCons a p) = Suc (degree p)" ``` haftmann@52380 ` 166` ``` apply (rule order_antisym [OF degree_pCons_le]) ``` haftmann@52380 ` 167` ``` apply (rule le_degree, simp) ``` haftmann@52380 ` 168` ``` done ``` huffman@29451 ` 169` haftmann@52380 ` 170` ```lemma degree_pCons_0: ``` haftmann@52380 ` 171` ``` "degree (pCons a 0) = 0" ``` haftmann@52380 ` 172` ``` apply (rule order_antisym [OF _ le0]) ``` haftmann@52380 ` 173` ``` apply (rule degree_le, simp add: coeff_pCons split: nat.split) ``` haftmann@52380 ` 174` ``` done ``` huffman@29451 ` 175` huffman@29460 ` 176` ```lemma degree_pCons_eq_if [simp]: ``` huffman@29451 ` 177` ``` "degree (pCons a p) = (if p = 0 then 0 else Suc (degree p))" ``` haftmann@52380 ` 178` ``` apply (cases "p = 0", simp_all) ``` haftmann@52380 ` 179` ``` apply (rule order_antisym [OF _ le0]) ``` haftmann@52380 ` 180` ``` apply (rule degree_le, simp add: coeff_pCons split: nat.split) ``` haftmann@52380 ` 181` ``` apply (rule order_antisym [OF degree_pCons_le]) ``` haftmann@52380 ` 182` ``` apply (rule le_degree, simp) ``` haftmann@52380 ` 183` ``` done ``` huffman@29451 ` 184` haftmann@52380 ` 185` ```lemma pCons_0_0 [simp]: ``` haftmann@52380 ` 186` ``` "pCons 0 0 = 0" ``` haftmann@52380 ` 187` ``` by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 188` huffman@29451 ` 189` ```lemma pCons_eq_iff [simp]: ``` huffman@29451 ` 190` ``` "pCons a p = pCons b q \ a = b \ p = q" ``` haftmann@52380 ` 191` ```proof safe ``` huffman@29451 ` 192` ``` assume "pCons a p = pCons b q" ``` huffman@29451 ` 193` ``` then have "coeff (pCons a p) 0 = coeff (pCons b q) 0" by simp ``` huffman@29451 ` 194` ``` then show "a = b" by simp ``` huffman@29451 ` 195` ```next ``` huffman@29451 ` 196` ``` assume "pCons a p = pCons b q" ``` huffman@29451 ` 197` ``` then have "\n. coeff (pCons a p) (Suc n) = ``` huffman@29451 ` 198` ``` coeff (pCons b q) (Suc n)" by simp ``` haftmann@52380 ` 199` ``` then show "p = q" by (simp add: poly_eq_iff) ``` huffman@29451 ` 200` ```qed ``` huffman@29451 ` 201` haftmann@52380 ` 202` ```lemma pCons_eq_0_iff [simp]: ``` haftmann@52380 ` 203` ``` "pCons a p = 0 \ a = 0 \ p = 0" ``` huffman@29451 ` 204` ``` using pCons_eq_iff [of a p 0 0] by simp ``` huffman@29451 ` 205` huffman@29451 ` 206` ```lemma pCons_cases [cases type: poly]: ``` huffman@29451 ` 207` ``` obtains (pCons) a q where "p = pCons a q" ``` huffman@29451 ` 208` ```proof ``` huffman@29451 ` 209` ``` show "p = pCons (coeff p 0) (Abs_poly (\n. coeff p (Suc n)))" ``` haftmann@52380 ` 210` ``` by transfer ``` hoelzl@59983 ` 211` ``` (simp_all add: MOST_inj[where f=Suc and P="\n. p n = 0" for p] fun_eq_iff Abs_poly_inverse ``` hoelzl@59983 ` 212` ``` split: nat.split) ``` huffman@29451 ` 213` ```qed ``` huffman@29451 ` 214` huffman@29451 ` 215` ```lemma pCons_induct [case_names 0 pCons, induct type: poly]: ``` huffman@29451 ` 216` ``` assumes zero: "P 0" ``` haftmann@54856 ` 217` ``` assumes pCons: "\a p. a \ 0 \ p \ 0 \ P p \ P (pCons a p)" ``` huffman@29451 ` 218` ``` shows "P p" ``` huffman@29451 ` 219` ```proof (induct p rule: measure_induct_rule [where f=degree]) ``` huffman@29451 ` 220` ``` case (less p) ``` huffman@29451 ` 221` ``` obtain a q where "p = pCons a q" by (rule pCons_cases) ``` huffman@29451 ` 222` ``` have "P q" ``` huffman@29451 ` 223` ``` proof (cases "q = 0") ``` huffman@29451 ` 224` ``` case True ``` huffman@29451 ` 225` ``` then show "P q" by (simp add: zero) ``` huffman@29451 ` 226` ``` next ``` huffman@29451 ` 227` ``` case False ``` huffman@29451 ` 228` ``` then have "degree (pCons a q) = Suc (degree q)" ``` huffman@29451 ` 229` ``` by (rule degree_pCons_eq) ``` huffman@29451 ` 230` ``` then have "degree q < degree p" ``` wenzelm@60500 ` 231` ``` using \p = pCons a q\ by simp ``` huffman@29451 ` 232` ``` then show "P q" ``` huffman@29451 ` 233` ``` by (rule less.hyps) ``` huffman@29451 ` 234` ``` qed ``` haftmann@54856 ` 235` ``` have "P (pCons a q)" ``` haftmann@54856 ` 236` ``` proof (cases "a \ 0 \ q \ 0") ``` haftmann@54856 ` 237` ``` case True ``` wenzelm@60500 ` 238` ``` with \P q\ show ?thesis by (auto intro: pCons) ``` haftmann@54856 ` 239` ``` next ``` haftmann@54856 ` 240` ``` case False ``` haftmann@54856 ` 241` ``` with zero show ?thesis by simp ``` haftmann@54856 ` 242` ``` qed ``` huffman@29451 ` 243` ``` then show ?case ``` wenzelm@60500 ` 244` ``` using \p = pCons a q\ by simp ``` huffman@29451 ` 245` ```qed ``` huffman@29451 ` 246` haftmann@60570 ` 247` ```lemma degree_eq_zeroE: ``` haftmann@60570 ` 248` ``` fixes p :: "'a::zero poly" ``` haftmann@60570 ` 249` ``` assumes "degree p = 0" ``` haftmann@60570 ` 250` ``` obtains a where "p = pCons a 0" ``` haftmann@60570 ` 251` ```proof - ``` haftmann@60570 ` 252` ``` obtain a q where p: "p = pCons a q" by (cases p) ``` haftmann@60570 ` 253` ``` with assms have "q = 0" by (cases "q = 0") simp_all ``` haftmann@60570 ` 254` ``` with p have "p = pCons a 0" by simp ``` haftmann@60570 ` 255` ``` with that show thesis . ``` haftmann@60570 ` 256` ```qed ``` haftmann@60570 ` 257` huffman@29451 ` 258` wenzelm@60500 ` 259` ```subsection \List-style syntax for polynomials\ ``` haftmann@52380 ` 260` haftmann@52380 ` 261` ```syntax ``` haftmann@52380 ` 262` ``` "_poly" :: "args \ 'a poly" ("[:(_):]") ``` haftmann@52380 ` 263` haftmann@52380 ` 264` ```translations ``` haftmann@52380 ` 265` ``` "[:x, xs:]" == "CONST pCons x [:xs:]" ``` haftmann@52380 ` 266` ``` "[:x:]" == "CONST pCons x 0" ``` haftmann@52380 ` 267` ``` "[:x:]" <= "CONST pCons x (_constrain 0 t)" ``` haftmann@52380 ` 268` haftmann@52380 ` 269` wenzelm@60500 ` 270` ```subsection \Representation of polynomials by lists of coefficients\ ``` haftmann@52380 ` 271` haftmann@52380 ` 272` ```primrec Poly :: "'a::zero list \ 'a poly" ``` haftmann@52380 ` 273` ```where ``` haftmann@54855 ` 274` ``` [code_post]: "Poly [] = 0" ``` haftmann@54855 ` 275` ```| [code_post]: "Poly (a # as) = pCons a (Poly as)" ``` haftmann@52380 ` 276` haftmann@52380 ` 277` ```lemma Poly_replicate_0 [simp]: ``` haftmann@52380 ` 278` ``` "Poly (replicate n 0) = 0" ``` haftmann@52380 ` 279` ``` by (induct n) simp_all ``` haftmann@52380 ` 280` haftmann@52380 ` 281` ```lemma Poly_eq_0: ``` haftmann@52380 ` 282` ``` "Poly as = 0 \ (\n. as = replicate n 0)" ``` haftmann@52380 ` 283` ``` by (induct as) (auto simp add: Cons_replicate_eq) ``` haftmann@52380 ` 284` haftmann@52380 ` 285` ```definition coeffs :: "'a poly \ 'a::zero list" ``` haftmann@52380 ` 286` ```where ``` haftmann@52380 ` 287` ``` "coeffs p = (if p = 0 then [] else map (\i. coeff p i) [0 ..< Suc (degree p)])" ``` haftmann@52380 ` 288` haftmann@52380 ` 289` ```lemma coeffs_eq_Nil [simp]: ``` haftmann@52380 ` 290` ``` "coeffs p = [] \ p = 0" ``` haftmann@52380 ` 291` ``` by (simp add: coeffs_def) ``` haftmann@52380 ` 292` haftmann@52380 ` 293` ```lemma not_0_coeffs_not_Nil: ``` haftmann@52380 ` 294` ``` "p \ 0 \ coeffs p \ []" ``` haftmann@52380 ` 295` ``` by simp ``` haftmann@52380 ` 296` haftmann@52380 ` 297` ```lemma coeffs_0_eq_Nil [simp]: ``` haftmann@52380 ` 298` ``` "coeffs 0 = []" ``` haftmann@52380 ` 299` ``` by simp ``` huffman@29454 ` 300` haftmann@52380 ` 301` ```lemma coeffs_pCons_eq_cCons [simp]: ``` haftmann@52380 ` 302` ``` "coeffs (pCons a p) = a ## coeffs p" ``` haftmann@52380 ` 303` ```proof - ``` haftmann@52380 ` 304` ``` { fix ms :: "nat list" and f :: "nat \ 'a" and x :: "'a" ``` haftmann@52380 ` 305` ``` assume "\m\set ms. m > 0" ``` blanchet@55415 ` 306` ``` then have "map (case_nat x f) ms = map f (map (\n. n - 1) ms)" ``` haftmann@58199 ` 307` ``` by (induct ms) (auto split: nat.split) ``` haftmann@58199 ` 308` ``` } ``` haftmann@52380 ` 309` ``` note * = this ``` haftmann@52380 ` 310` ``` show ?thesis ``` haftmann@60570 ` 311` ``` by (simp add: coeffs_def * upt_conv_Cons coeff_pCons map_decr_upt del: upt_Suc) ``` haftmann@52380 ` 312` ```qed ``` haftmann@52380 ` 313` haftmann@52380 ` 314` ```lemma not_0_cCons_eq [simp]: ``` haftmann@52380 ` 315` ``` "p \ 0 \ a ## coeffs p = a # coeffs p" ``` haftmann@52380 ` 316` ``` by (simp add: cCons_def) ``` haftmann@52380 ` 317` haftmann@52380 ` 318` ```lemma Poly_coeffs [simp, code abstype]: ``` haftmann@52380 ` 319` ``` "Poly (coeffs p) = p" ``` haftmann@54856 ` 320` ``` by (induct p) auto ``` haftmann@52380 ` 321` haftmann@52380 ` 322` ```lemma coeffs_Poly [simp]: ``` haftmann@52380 ` 323` ``` "coeffs (Poly as) = strip_while (HOL.eq 0) as" ``` haftmann@52380 ` 324` ```proof (induct as) ``` haftmann@52380 ` 325` ``` case Nil then show ?case by simp ``` haftmann@52380 ` 326` ```next ``` haftmann@52380 ` 327` ``` case (Cons a as) ``` haftmann@52380 ` 328` ``` have "(\n. as \ replicate n 0) \ (\a\set as. a \ 0)" ``` haftmann@52380 ` 329` ``` using replicate_length_same [of as 0] by (auto dest: sym [of _ as]) ``` haftmann@52380 ` 330` ``` with Cons show ?case by auto ``` haftmann@52380 ` 331` ```qed ``` haftmann@52380 ` 332` haftmann@52380 ` 333` ```lemma last_coeffs_not_0: ``` haftmann@52380 ` 334` ``` "p \ 0 \ last (coeffs p) \ 0" ``` haftmann@52380 ` 335` ``` by (induct p) (auto simp add: cCons_def) ``` haftmann@52380 ` 336` haftmann@52380 ` 337` ```lemma strip_while_coeffs [simp]: ``` haftmann@52380 ` 338` ``` "strip_while (HOL.eq 0) (coeffs p) = coeffs p" ``` haftmann@52380 ` 339` ``` by (cases "p = 0") (auto dest: last_coeffs_not_0 intro: strip_while_not_last) ``` haftmann@52380 ` 340` haftmann@52380 ` 341` ```lemma coeffs_eq_iff: ``` haftmann@52380 ` 342` ``` "p = q \ coeffs p = coeffs q" (is "?P \ ?Q") ``` haftmann@52380 ` 343` ```proof ``` haftmann@52380 ` 344` ``` assume ?P then show ?Q by simp ``` haftmann@52380 ` 345` ```next ``` haftmann@52380 ` 346` ``` assume ?Q ``` haftmann@52380 ` 347` ``` then have "Poly (coeffs p) = Poly (coeffs q)" by simp ``` haftmann@52380 ` 348` ``` then show ?P by simp ``` haftmann@52380 ` 349` ```qed ``` haftmann@52380 ` 350` haftmann@52380 ` 351` ```lemma coeff_Poly_eq: ``` haftmann@52380 ` 352` ``` "coeff (Poly xs) n = nth_default 0 xs n" ``` haftmann@52380 ` 353` ``` apply (induct xs arbitrary: n) apply simp_all ``` blanchet@55642 ` 354` ``` by (metis nat.case not0_implies_Suc nth_default_Cons_0 nth_default_Cons_Suc pCons.rep_eq) ``` huffman@29454 ` 355` haftmann@52380 ` 356` ```lemma nth_default_coeffs_eq: ``` haftmann@52380 ` 357` ``` "nth_default 0 (coeffs p) = coeff p" ``` haftmann@52380 ` 358` ``` by (simp add: fun_eq_iff coeff_Poly_eq [symmetric]) ``` haftmann@52380 ` 359` haftmann@52380 ` 360` ```lemma [code]: ``` haftmann@52380 ` 361` ``` "coeff p = nth_default 0 (coeffs p)" ``` haftmann@52380 ` 362` ``` by (simp add: nth_default_coeffs_eq) ``` haftmann@52380 ` 363` haftmann@52380 ` 364` ```lemma coeffs_eqI: ``` haftmann@52380 ` 365` ``` assumes coeff: "\n. coeff p n = nth_default 0 xs n" ``` haftmann@52380 ` 366` ``` assumes zero: "xs \ [] \ last xs \ 0" ``` haftmann@52380 ` 367` ``` shows "coeffs p = xs" ``` haftmann@52380 ` 368` ```proof - ``` haftmann@52380 ` 369` ``` from coeff have "p = Poly xs" by (simp add: poly_eq_iff coeff_Poly_eq) ``` haftmann@52380 ` 370` ``` with zero show ?thesis by simp (cases xs, simp_all) ``` haftmann@52380 ` 371` ```qed ``` haftmann@52380 ` 372` haftmann@52380 ` 373` ```lemma degree_eq_length_coeffs [code]: ``` haftmann@52380 ` 374` ``` "degree p = length (coeffs p) - 1" ``` haftmann@52380 ` 375` ``` by (simp add: coeffs_def) ``` haftmann@52380 ` 376` haftmann@52380 ` 377` ```lemma length_coeffs_degree: ``` haftmann@52380 ` 378` ``` "p \ 0 \ length (coeffs p) = Suc (degree p)" ``` haftmann@52380 ` 379` ``` by (induct p) (auto simp add: cCons_def) ``` haftmann@52380 ` 380` haftmann@52380 ` 381` ```lemma [code abstract]: ``` haftmann@52380 ` 382` ``` "coeffs 0 = []" ``` haftmann@52380 ` 383` ``` by (fact coeffs_0_eq_Nil) ``` haftmann@52380 ` 384` haftmann@52380 ` 385` ```lemma [code abstract]: ``` haftmann@52380 ` 386` ``` "coeffs (pCons a p) = a ## coeffs p" ``` haftmann@52380 ` 387` ``` by (fact coeffs_pCons_eq_cCons) ``` haftmann@52380 ` 388` haftmann@52380 ` 389` ```instantiation poly :: ("{zero, equal}") equal ``` haftmann@52380 ` 390` ```begin ``` haftmann@52380 ` 391` haftmann@52380 ` 392` ```definition ``` haftmann@52380 ` 393` ``` [code]: "HOL.equal (p::'a poly) q \ HOL.equal (coeffs p) (coeffs q)" ``` haftmann@52380 ` 394` wenzelm@60679 ` 395` ```instance ``` wenzelm@60679 ` 396` ``` by standard (simp add: equal equal_poly_def coeffs_eq_iff) ``` haftmann@52380 ` 397` haftmann@52380 ` 398` ```end ``` haftmann@52380 ` 399` wenzelm@60679 ` 400` ```lemma [code nbe]: "HOL.equal (p :: _ poly) p \ True" ``` haftmann@52380 ` 401` ``` by (fact equal_refl) ``` huffman@29454 ` 402` haftmann@52380 ` 403` ```definition is_zero :: "'a::zero poly \ bool" ``` haftmann@52380 ` 404` ```where ``` haftmann@52380 ` 405` ``` [code]: "is_zero p \ List.null (coeffs p)" ``` haftmann@52380 ` 406` haftmann@52380 ` 407` ```lemma is_zero_null [code_abbrev]: ``` haftmann@52380 ` 408` ``` "is_zero p \ p = 0" ``` haftmann@52380 ` 409` ``` by (simp add: is_zero_def null_def) ``` haftmann@52380 ` 410` haftmann@52380 ` 411` wenzelm@60500 ` 412` ```subsection \Fold combinator for polynomials\ ``` haftmann@52380 ` 413` haftmann@52380 ` 414` ```definition fold_coeffs :: "('a::zero \ 'b \ 'b) \ 'a poly \ 'b \ 'b" ``` haftmann@52380 ` 415` ```where ``` haftmann@52380 ` 416` ``` "fold_coeffs f p = foldr f (coeffs p)" ``` haftmann@52380 ` 417` haftmann@52380 ` 418` ```lemma fold_coeffs_0_eq [simp]: ``` haftmann@52380 ` 419` ``` "fold_coeffs f 0 = id" ``` haftmann@52380 ` 420` ``` by (simp add: fold_coeffs_def) ``` haftmann@52380 ` 421` haftmann@52380 ` 422` ```lemma fold_coeffs_pCons_eq [simp]: ``` haftmann@52380 ` 423` ``` "f 0 = id \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p" ``` haftmann@52380 ` 424` ``` by (simp add: fold_coeffs_def cCons_def fun_eq_iff) ``` huffman@29454 ` 425` haftmann@52380 ` 426` ```lemma fold_coeffs_pCons_0_0_eq [simp]: ``` haftmann@52380 ` 427` ``` "fold_coeffs f (pCons 0 0) = id" ``` haftmann@52380 ` 428` ``` by (simp add: fold_coeffs_def) ``` haftmann@52380 ` 429` haftmann@52380 ` 430` ```lemma fold_coeffs_pCons_coeff_not_0_eq [simp]: ``` haftmann@52380 ` 431` ``` "a \ 0 \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p" ``` haftmann@52380 ` 432` ``` by (simp add: fold_coeffs_def) ``` haftmann@52380 ` 433` haftmann@52380 ` 434` ```lemma fold_coeffs_pCons_not_0_0_eq [simp]: ``` haftmann@52380 ` 435` ``` "p \ 0 \ fold_coeffs f (pCons a p) = f a \ fold_coeffs f p" ``` haftmann@52380 ` 436` ``` by (simp add: fold_coeffs_def) ``` haftmann@52380 ` 437` haftmann@52380 ` 438` wenzelm@60500 ` 439` ```subsection \Canonical morphism on polynomials -- evaluation\ ``` haftmann@52380 ` 440` haftmann@52380 ` 441` ```definition poly :: "'a::comm_semiring_0 poly \ 'a \ 'a" ``` haftmann@52380 ` 442` ```where ``` wenzelm@61585 ` 443` ``` "poly p = fold_coeffs (\a f x. a + x * f x) p (\x. 0)" \ \The Horner Schema\ ``` haftmann@52380 ` 444` haftmann@52380 ` 445` ```lemma poly_0 [simp]: ``` haftmann@52380 ` 446` ``` "poly 0 x = 0" ``` haftmann@52380 ` 447` ``` by (simp add: poly_def) ``` haftmann@52380 ` 448` haftmann@52380 ` 449` ```lemma poly_pCons [simp]: ``` haftmann@52380 ` 450` ``` "poly (pCons a p) x = a + x * poly p x" ``` haftmann@52380 ` 451` ``` by (cases "p = 0 \ a = 0") (auto simp add: poly_def) ``` huffman@29454 ` 452` huffman@29454 ` 453` wenzelm@60500 ` 454` ```subsection \Monomials\ ``` huffman@29451 ` 455` haftmann@52380 ` 456` ```lift_definition monom :: "'a \ nat \ 'a::zero poly" ``` haftmann@52380 ` 457` ``` is "\a m n. if m = n then a else 0" ``` hoelzl@59983 ` 458` ``` by (simp add: MOST_iff_cofinite) ``` haftmann@52380 ` 459` haftmann@52380 ` 460` ```lemma coeff_monom [simp]: ``` haftmann@52380 ` 461` ``` "coeff (monom a m) n = (if m = n then a else 0)" ``` haftmann@52380 ` 462` ``` by transfer rule ``` huffman@29451 ` 463` haftmann@52380 ` 464` ```lemma monom_0: ``` haftmann@52380 ` 465` ``` "monom a 0 = pCons a 0" ``` haftmann@52380 ` 466` ``` by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 467` haftmann@52380 ` 468` ```lemma monom_Suc: ``` haftmann@52380 ` 469` ``` "monom a (Suc n) = pCons 0 (monom a n)" ``` haftmann@52380 ` 470` ``` by (rule poly_eqI) (simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 471` huffman@29451 ` 472` ```lemma monom_eq_0 [simp]: "monom 0 n = 0" ``` haftmann@52380 ` 473` ``` by (rule poly_eqI) simp ``` huffman@29451 ` 474` huffman@29451 ` 475` ```lemma monom_eq_0_iff [simp]: "monom a n = 0 \ a = 0" ``` haftmann@52380 ` 476` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 477` huffman@29451 ` 478` ```lemma monom_eq_iff [simp]: "monom a n = monom b n \ a = b" ``` haftmann@52380 ` 479` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 480` huffman@29451 ` 481` ```lemma degree_monom_le: "degree (monom a n) \ n" ``` huffman@29451 ` 482` ``` by (rule degree_le, simp) ``` huffman@29451 ` 483` huffman@29451 ` 484` ```lemma degree_monom_eq: "a \ 0 \ degree (monom a n) = n" ``` huffman@29451 ` 485` ``` apply (rule order_antisym [OF degree_monom_le]) ``` huffman@29451 ` 486` ``` apply (rule le_degree, simp) ``` huffman@29451 ` 487` ``` done ``` huffman@29451 ` 488` haftmann@52380 ` 489` ```lemma coeffs_monom [code abstract]: ``` haftmann@52380 ` 490` ``` "coeffs (monom a n) = (if a = 0 then [] else replicate n 0 @ [a])" ``` haftmann@52380 ` 491` ``` by (induct n) (simp_all add: monom_0 monom_Suc) ``` haftmann@52380 ` 492` haftmann@52380 ` 493` ```lemma fold_coeffs_monom [simp]: ``` haftmann@52380 ` 494` ``` "a \ 0 \ fold_coeffs f (monom a n) = f 0 ^^ n \ f a" ``` haftmann@52380 ` 495` ``` by (simp add: fold_coeffs_def coeffs_monom fun_eq_iff) ``` haftmann@52380 ` 496` haftmann@52380 ` 497` ```lemma poly_monom: ``` haftmann@52380 ` 498` ``` fixes a x :: "'a::{comm_semiring_1}" ``` haftmann@52380 ` 499` ``` shows "poly (monom a n) x = a * x ^ n" ``` haftmann@52380 ` 500` ``` by (cases "a = 0", simp_all) ``` haftmann@52380 ` 501` ``` (induct n, simp_all add: mult.left_commute poly_def) ``` haftmann@52380 ` 502` huffman@29451 ` 503` wenzelm@60500 ` 504` ```subsection \Addition and subtraction\ ``` huffman@29451 ` 505` huffman@29451 ` 506` ```instantiation poly :: (comm_monoid_add) comm_monoid_add ``` huffman@29451 ` 507` ```begin ``` huffman@29451 ` 508` haftmann@52380 ` 509` ```lift_definition plus_poly :: "'a poly \ 'a poly \ 'a poly" ``` haftmann@52380 ` 510` ``` is "\p q n. coeff p n + coeff q n" ``` hoelzl@60040 ` 511` ```proof - ``` wenzelm@60679 ` 512` ``` fix q p :: "'a poly" ``` wenzelm@60679 ` 513` ``` show "\\<^sub>\n. coeff p n + coeff q n = 0" ``` hoelzl@60040 ` 514` ``` using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp ``` haftmann@52380 ` 515` ```qed ``` huffman@29451 ` 516` wenzelm@60679 ` 517` ```lemma coeff_add [simp]: "coeff (p + q) n = coeff p n + coeff q n" ``` haftmann@52380 ` 518` ``` by (simp add: plus_poly.rep_eq) ``` huffman@29451 ` 519` wenzelm@60679 ` 520` ```instance ``` wenzelm@60679 ` 521` ```proof ``` huffman@29451 ` 522` ``` fix p q r :: "'a poly" ``` huffman@29451 ` 523` ``` show "(p + q) + r = p + (q + r)" ``` haftmann@57512 ` 524` ``` by (simp add: poly_eq_iff add.assoc) ``` huffman@29451 ` 525` ``` show "p + q = q + p" ``` haftmann@57512 ` 526` ``` by (simp add: poly_eq_iff add.commute) ``` huffman@29451 ` 527` ``` show "0 + p = p" ``` haftmann@52380 ` 528` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 529` ```qed ``` huffman@29451 ` 530` huffman@29451 ` 531` ```end ``` huffman@29451 ` 532` haftmann@59815 ` 533` ```instantiation poly :: (cancel_comm_monoid_add) cancel_comm_monoid_add ``` haftmann@59815 ` 534` ```begin ``` haftmann@59815 ` 535` haftmann@59815 ` 536` ```lift_definition minus_poly :: "'a poly \ 'a poly \ 'a poly" ``` haftmann@59815 ` 537` ``` is "\p q n. coeff p n - coeff q n" ``` hoelzl@60040 ` 538` ```proof - ``` wenzelm@60679 ` 539` ``` fix q p :: "'a poly" ``` wenzelm@60679 ` 540` ``` show "\\<^sub>\n. coeff p n - coeff q n = 0" ``` hoelzl@60040 ` 541` ``` using MOST_coeff_eq_0[of p] MOST_coeff_eq_0[of q] by eventually_elim simp ``` haftmann@59815 ` 542` ```qed ``` haftmann@59815 ` 543` wenzelm@60679 ` 544` ```lemma coeff_diff [simp]: "coeff (p - q) n = coeff p n - coeff q n" ``` haftmann@59815 ` 545` ``` by (simp add: minus_poly.rep_eq) ``` haftmann@59815 ` 546` wenzelm@60679 ` 547` ```instance ``` wenzelm@60679 ` 548` ```proof ``` huffman@29540 ` 549` ``` fix p q r :: "'a poly" ``` haftmann@59815 ` 550` ``` show "p + q - p = q" ``` haftmann@52380 ` 551` ``` by (simp add: poly_eq_iff) ``` haftmann@59815 ` 552` ``` show "p - q - r = p - (q + r)" ``` haftmann@59815 ` 553` ``` by (simp add: poly_eq_iff diff_diff_eq) ``` huffman@29540 ` 554` ```qed ``` huffman@29540 ` 555` haftmann@59815 ` 556` ```end ``` haftmann@59815 ` 557` huffman@29451 ` 558` ```instantiation poly :: (ab_group_add) ab_group_add ``` huffman@29451 ` 559` ```begin ``` huffman@29451 ` 560` haftmann@52380 ` 561` ```lift_definition uminus_poly :: "'a poly \ 'a poly" ``` haftmann@52380 ` 562` ``` is "\p n. - coeff p n" ``` hoelzl@60040 ` 563` ```proof - ``` wenzelm@60679 ` 564` ``` fix p :: "'a poly" ``` wenzelm@60679 ` 565` ``` show "\\<^sub>\n. - coeff p n = 0" ``` hoelzl@60040 ` 566` ``` using MOST_coeff_eq_0 by simp ``` haftmann@52380 ` 567` ```qed ``` huffman@29451 ` 568` huffman@29451 ` 569` ```lemma coeff_minus [simp]: "coeff (- p) n = - coeff p n" ``` haftmann@52380 ` 570` ``` by (simp add: uminus_poly.rep_eq) ``` huffman@29451 ` 571` wenzelm@60679 ` 572` ```instance ``` wenzelm@60679 ` 573` ```proof ``` huffman@29451 ` 574` ``` fix p q :: "'a poly" ``` huffman@29451 ` 575` ``` show "- p + p = 0" ``` haftmann@52380 ` 576` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 577` ``` show "p - q = p + - q" ``` haftmann@54230 ` 578` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 579` ```qed ``` huffman@29451 ` 580` huffman@29451 ` 581` ```end ``` huffman@29451 ` 582` huffman@29451 ` 583` ```lemma add_pCons [simp]: ``` huffman@29451 ` 584` ``` "pCons a p + pCons b q = pCons (a + b) (p + q)" ``` haftmann@52380 ` 585` ``` by (rule poly_eqI, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 586` huffman@29451 ` 587` ```lemma minus_pCons [simp]: ``` huffman@29451 ` 588` ``` "- pCons a p = pCons (- a) (- p)" ``` haftmann@52380 ` 589` ``` by (rule poly_eqI, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 590` huffman@29451 ` 591` ```lemma diff_pCons [simp]: ``` huffman@29451 ` 592` ``` "pCons a p - pCons b q = pCons (a - b) (p - q)" ``` haftmann@52380 ` 593` ``` by (rule poly_eqI, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 594` huffman@29539 ` 595` ```lemma degree_add_le_max: "degree (p + q) \ max (degree p) (degree q)" ``` huffman@29451 ` 596` ``` by (rule degree_le, auto simp add: coeff_eq_0) ``` huffman@29451 ` 597` huffman@29539 ` 598` ```lemma degree_add_le: ``` huffman@29539 ` 599` ``` "\degree p \ n; degree q \ n\ \ degree (p + q) \ n" ``` huffman@29539 ` 600` ``` by (auto intro: order_trans degree_add_le_max) ``` huffman@29539 ` 601` huffman@29453 ` 602` ```lemma degree_add_less: ``` huffman@29453 ` 603` ``` "\degree p < n; degree q < n\ \ degree (p + q) < n" ``` huffman@29539 ` 604` ``` by (auto intro: le_less_trans degree_add_le_max) ``` huffman@29453 ` 605` huffman@29451 ` 606` ```lemma degree_add_eq_right: ``` huffman@29451 ` 607` ``` "degree p < degree q \ degree (p + q) = degree q" ``` huffman@29451 ` 608` ``` apply (cases "q = 0", simp) ``` huffman@29451 ` 609` ``` apply (rule order_antisym) ``` huffman@29539 ` 610` ``` apply (simp add: degree_add_le) ``` huffman@29451 ` 611` ``` apply (rule le_degree) ``` huffman@29451 ` 612` ``` apply (simp add: coeff_eq_0) ``` huffman@29451 ` 613` ``` done ``` huffman@29451 ` 614` huffman@29451 ` 615` ```lemma degree_add_eq_left: ``` huffman@29451 ` 616` ``` "degree q < degree p \ degree (p + q) = degree p" ``` huffman@29451 ` 617` ``` using degree_add_eq_right [of q p] ``` haftmann@57512 ` 618` ``` by (simp add: add.commute) ``` huffman@29451 ` 619` haftmann@59815 ` 620` ```lemma degree_minus [simp]: ``` haftmann@59815 ` 621` ``` "degree (- p) = degree p" ``` huffman@29451 ` 622` ``` unfolding degree_def by simp ``` huffman@29451 ` 623` haftmann@59815 ` 624` ```lemma degree_diff_le_max: ``` haftmann@59815 ` 625` ``` fixes p q :: "'a :: ab_group_add poly" ``` haftmann@59815 ` 626` ``` shows "degree (p - q) \ max (degree p) (degree q)" ``` huffman@29451 ` 627` ``` using degree_add_le [where p=p and q="-q"] ``` haftmann@54230 ` 628` ``` by simp ``` huffman@29451 ` 629` huffman@29539 ` 630` ```lemma degree_diff_le: ``` haftmann@59815 ` 631` ``` fixes p q :: "'a :: ab_group_add poly" ``` haftmann@59815 ` 632` ``` assumes "degree p \ n" and "degree q \ n" ``` haftmann@59815 ` 633` ``` shows "degree (p - q) \ n" ``` haftmann@59815 ` 634` ``` using assms degree_add_le [of p n "- q"] by simp ``` huffman@29539 ` 635` huffman@29453 ` 636` ```lemma degree_diff_less: ``` haftmann@59815 ` 637` ``` fixes p q :: "'a :: ab_group_add poly" ``` haftmann@59815 ` 638` ``` assumes "degree p < n" and "degree q < n" ``` haftmann@59815 ` 639` ``` shows "degree (p - q) < n" ``` haftmann@59815 ` 640` ``` using assms degree_add_less [of p n "- q"] by simp ``` huffman@29453 ` 641` huffman@29451 ` 642` ```lemma add_monom: "monom a n + monom b n = monom (a + b) n" ``` haftmann@52380 ` 643` ``` by (rule poly_eqI) simp ``` huffman@29451 ` 644` huffman@29451 ` 645` ```lemma diff_monom: "monom a n - monom b n = monom (a - b) n" ``` haftmann@52380 ` 646` ``` by (rule poly_eqI) simp ``` huffman@29451 ` 647` huffman@29451 ` 648` ```lemma minus_monom: "- monom a n = monom (-a) n" ``` haftmann@52380 ` 649` ``` by (rule poly_eqI) simp ``` huffman@29451 ` 650` huffman@29451 ` 651` ```lemma coeff_setsum: "coeff (\x\A. p x) i = (\x\A. coeff (p x) i)" ``` huffman@29451 ` 652` ``` by (cases "finite A", induct set: finite, simp_all) ``` huffman@29451 ` 653` huffman@29451 ` 654` ```lemma monom_setsum: "monom (\x\A. a x) n = (\x\A. monom (a x) n)" ``` haftmann@52380 ` 655` ``` by (rule poly_eqI) (simp add: coeff_setsum) ``` haftmann@52380 ` 656` haftmann@52380 ` 657` ```fun plus_coeffs :: "'a::comm_monoid_add list \ 'a list \ 'a list" ``` haftmann@52380 ` 658` ```where ``` haftmann@52380 ` 659` ``` "plus_coeffs xs [] = xs" ``` haftmann@52380 ` 660` ```| "plus_coeffs [] ys = ys" ``` haftmann@52380 ` 661` ```| "plus_coeffs (x # xs) (y # ys) = (x + y) ## plus_coeffs xs ys" ``` haftmann@52380 ` 662` haftmann@52380 ` 663` ```lemma coeffs_plus_eq_plus_coeffs [code abstract]: ``` haftmann@52380 ` 664` ``` "coeffs (p + q) = plus_coeffs (coeffs p) (coeffs q)" ``` haftmann@52380 ` 665` ```proof - ``` haftmann@52380 ` 666` ``` { fix xs ys :: "'a list" and n ``` haftmann@52380 ` 667` ``` have "nth_default 0 (plus_coeffs xs ys) n = nth_default 0 xs n + nth_default 0 ys n" ``` haftmann@52380 ` 668` ``` proof (induct xs ys arbitrary: n rule: plus_coeffs.induct) ``` wenzelm@60679 ` 669` ``` case (3 x xs y ys n) ``` wenzelm@60679 ` 670` ``` then show ?case by (cases n) (auto simp add: cCons_def) ``` haftmann@52380 ` 671` ``` qed simp_all } ``` haftmann@52380 ` 672` ``` note * = this ``` haftmann@52380 ` 673` ``` { fix xs ys :: "'a list" ``` haftmann@52380 ` 674` ``` assume "xs \ [] \ last xs \ 0" and "ys \ [] \ last ys \ 0" ``` haftmann@52380 ` 675` ``` moreover assume "plus_coeffs xs ys \ []" ``` haftmann@52380 ` 676` ``` ultimately have "last (plus_coeffs xs ys) \ 0" ``` haftmann@52380 ` 677` ``` proof (induct xs ys rule: plus_coeffs.induct) ``` haftmann@52380 ` 678` ``` case (3 x xs y ys) then show ?case by (auto simp add: cCons_def) metis ``` haftmann@52380 ` 679` ``` qed simp_all } ``` haftmann@52380 ` 680` ``` note ** = this ``` haftmann@52380 ` 681` ``` show ?thesis ``` haftmann@52380 ` 682` ``` apply (rule coeffs_eqI) ``` haftmann@52380 ` 683` ``` apply (simp add: * nth_default_coeffs_eq) ``` haftmann@52380 ` 684` ``` apply (rule **) ``` haftmann@52380 ` 685` ``` apply (auto dest: last_coeffs_not_0) ``` haftmann@52380 ` 686` ``` done ``` haftmann@52380 ` 687` ```qed ``` haftmann@52380 ` 688` haftmann@52380 ` 689` ```lemma coeffs_uminus [code abstract]: ``` haftmann@52380 ` 690` ``` "coeffs (- p) = map (\a. - a) (coeffs p)" ``` haftmann@52380 ` 691` ``` by (rule coeffs_eqI) ``` haftmann@52380 ` 692` ``` (simp_all add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) ``` haftmann@52380 ` 693` haftmann@52380 ` 694` ```lemma [code]: ``` haftmann@52380 ` 695` ``` fixes p q :: "'a::ab_group_add poly" ``` haftmann@52380 ` 696` ``` shows "p - q = p + - q" ``` haftmann@59557 ` 697` ``` by (fact diff_conv_add_uminus) ``` haftmann@52380 ` 698` haftmann@52380 ` 699` ```lemma poly_add [simp]: "poly (p + q) x = poly p x + poly q x" ``` haftmann@52380 ` 700` ``` apply (induct p arbitrary: q, simp) ``` haftmann@52380 ` 701` ``` apply (case_tac q, simp, simp add: algebra_simps) ``` haftmann@52380 ` 702` ``` done ``` haftmann@52380 ` 703` haftmann@52380 ` 704` ```lemma poly_minus [simp]: ``` haftmann@52380 ` 705` ``` fixes x :: "'a::comm_ring" ``` haftmann@52380 ` 706` ``` shows "poly (- p) x = - poly p x" ``` haftmann@52380 ` 707` ``` by (induct p) simp_all ``` haftmann@52380 ` 708` haftmann@52380 ` 709` ```lemma poly_diff [simp]: ``` haftmann@52380 ` 710` ``` fixes x :: "'a::comm_ring" ``` haftmann@52380 ` 711` ``` shows "poly (p - q) x = poly p x - poly q x" ``` haftmann@54230 ` 712` ``` using poly_add [of p "- q" x] by simp ``` haftmann@52380 ` 713` haftmann@52380 ` 714` ```lemma poly_setsum: "poly (\k\A. p k) x = (\k\A. poly (p k) x)" ``` haftmann@52380 ` 715` ``` by (induct A rule: infinite_finite_induct) simp_all ``` huffman@29451 ` 716` huffman@29451 ` 717` wenzelm@60500 ` 718` ```subsection \Multiplication by a constant, polynomial multiplication and the unit polynomial\ ``` huffman@29451 ` 719` haftmann@52380 ` 720` ```lift_definition smult :: "'a::comm_semiring_0 \ 'a poly \ 'a poly" ``` haftmann@52380 ` 721` ``` is "\a p n. a * coeff p n" ``` hoelzl@60040 ` 722` ```proof - ``` hoelzl@60040 ` 723` ``` fix a :: 'a and p :: "'a poly" show "\\<^sub>\ i. a * coeff p i = 0" ``` hoelzl@60040 ` 724` ``` using MOST_coeff_eq_0[of p] by eventually_elim simp ``` haftmann@52380 ` 725` ```qed ``` huffman@29451 ` 726` haftmann@52380 ` 727` ```lemma coeff_smult [simp]: ``` haftmann@52380 ` 728` ``` "coeff (smult a p) n = a * coeff p n" ``` haftmann@52380 ` 729` ``` by (simp add: smult.rep_eq) ``` huffman@29451 ` 730` huffman@29451 ` 731` ```lemma degree_smult_le: "degree (smult a p) \ degree p" ``` huffman@29451 ` 732` ``` by (rule degree_le, simp add: coeff_eq_0) ``` huffman@29451 ` 733` huffman@29472 ` 734` ```lemma smult_smult [simp]: "smult a (smult b p) = smult (a * b) p" ``` haftmann@57512 ` 735` ``` by (rule poly_eqI, simp add: mult.assoc) ``` huffman@29451 ` 736` huffman@29451 ` 737` ```lemma smult_0_right [simp]: "smult a 0 = 0" ``` haftmann@52380 ` 738` ``` by (rule poly_eqI, simp) ``` huffman@29451 ` 739` huffman@29451 ` 740` ```lemma smult_0_left [simp]: "smult 0 p = 0" ``` haftmann@52380 ` 741` ``` by (rule poly_eqI, simp) ``` huffman@29451 ` 742` huffman@29451 ` 743` ```lemma smult_1_left [simp]: "smult (1::'a::comm_semiring_1) p = p" ``` haftmann@52380 ` 744` ``` by (rule poly_eqI, simp) ``` huffman@29451 ` 745` huffman@29451 ` 746` ```lemma smult_add_right: ``` huffman@29451 ` 747` ``` "smult a (p + q) = smult a p + smult a q" ``` haftmann@52380 ` 748` ``` by (rule poly_eqI, simp add: algebra_simps) ``` huffman@29451 ` 749` huffman@29451 ` 750` ```lemma smult_add_left: ``` huffman@29451 ` 751` ``` "smult (a + b) p = smult a p + smult b p" ``` haftmann@52380 ` 752` ``` by (rule poly_eqI, simp add: algebra_simps) ``` huffman@29451 ` 753` huffman@29457 ` 754` ```lemma smult_minus_right [simp]: ``` huffman@29451 ` 755` ``` "smult (a::'a::comm_ring) (- p) = - smult a p" ``` haftmann@52380 ` 756` ``` by (rule poly_eqI, simp) ``` huffman@29451 ` 757` huffman@29457 ` 758` ```lemma smult_minus_left [simp]: ``` huffman@29451 ` 759` ``` "smult (- a::'a::comm_ring) p = - smult a p" ``` haftmann@52380 ` 760` ``` by (rule poly_eqI, simp) ``` huffman@29451 ` 761` huffman@29451 ` 762` ```lemma smult_diff_right: ``` huffman@29451 ` 763` ``` "smult (a::'a::comm_ring) (p - q) = smult a p - smult a q" ``` haftmann@52380 ` 764` ``` by (rule poly_eqI, simp add: algebra_simps) ``` huffman@29451 ` 765` huffman@29451 ` 766` ```lemma smult_diff_left: ``` huffman@29451 ` 767` ``` "smult (a - b::'a::comm_ring) p = smult a p - smult b p" ``` haftmann@52380 ` 768` ``` by (rule poly_eqI, simp add: algebra_simps) ``` huffman@29451 ` 769` huffman@29472 ` 770` ```lemmas smult_distribs = ``` huffman@29472 ` 771` ``` smult_add_left smult_add_right ``` huffman@29472 ` 772` ``` smult_diff_left smult_diff_right ``` huffman@29472 ` 773` huffman@29451 ` 774` ```lemma smult_pCons [simp]: ``` huffman@29451 ` 775` ``` "smult a (pCons b p) = pCons (a * b) (smult a p)" ``` haftmann@52380 ` 776` ``` by (rule poly_eqI, simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 777` huffman@29451 ` 778` ```lemma smult_monom: "smult a (monom b n) = monom (a * b) n" ``` huffman@29451 ` 779` ``` by (induct n, simp add: monom_0, simp add: monom_Suc) ``` huffman@29451 ` 780` huffman@29659 ` 781` ```lemma degree_smult_eq [simp]: ``` huffman@29659 ` 782` ``` fixes a :: "'a::idom" ``` huffman@29659 ` 783` ``` shows "degree (smult a p) = (if a = 0 then 0 else degree p)" ``` huffman@29659 ` 784` ``` by (cases "a = 0", simp, simp add: degree_def) ``` huffman@29659 ` 785` huffman@29659 ` 786` ```lemma smult_eq_0_iff [simp]: ``` huffman@29659 ` 787` ``` fixes a :: "'a::idom" ``` huffman@29659 ` 788` ``` shows "smult a p = 0 \ a = 0 \ p = 0" ``` haftmann@52380 ` 789` ``` by (simp add: poly_eq_iff) ``` huffman@29451 ` 790` haftmann@52380 ` 791` ```lemma coeffs_smult [code abstract]: ``` haftmann@52380 ` 792` ``` fixes p :: "'a::idom poly" ``` haftmann@52380 ` 793` ``` shows "coeffs (smult a p) = (if a = 0 then [] else map (Groups.times a) (coeffs p))" ``` haftmann@52380 ` 794` ``` by (rule coeffs_eqI) ``` haftmann@52380 ` 795` ``` (auto simp add: not_0_coeffs_not_Nil last_map last_coeffs_not_0 nth_default_map_eq nth_default_coeffs_eq) ``` huffman@29451 ` 796` huffman@29451 ` 797` ```instantiation poly :: (comm_semiring_0) comm_semiring_0 ``` huffman@29451 ` 798` ```begin ``` huffman@29451 ` 799` huffman@29451 ` 800` ```definition ``` haftmann@52380 ` 801` ``` "p * q = fold_coeffs (\a p. smult a q + pCons 0 p) p 0" ``` huffman@29474 ` 802` huffman@29474 ` 803` ```lemma mult_poly_0_left: "(0::'a poly) * q = 0" ``` haftmann@52380 ` 804` ``` by (simp add: times_poly_def) ``` huffman@29474 ` 805` huffman@29474 ` 806` ```lemma mult_pCons_left [simp]: ``` huffman@29474 ` 807` ``` "pCons a p * q = smult a q + pCons 0 (p * q)" ``` haftmann@52380 ` 808` ``` by (cases "p = 0 \ a = 0") (auto simp add: times_poly_def) ``` huffman@29474 ` 809` huffman@29474 ` 810` ```lemma mult_poly_0_right: "p * (0::'a poly) = 0" ``` haftmann@52380 ` 811` ``` by (induct p) (simp add: mult_poly_0_left, simp) ``` huffman@29451 ` 812` huffman@29474 ` 813` ```lemma mult_pCons_right [simp]: ``` huffman@29474 ` 814` ``` "p * pCons a q = smult a p + pCons 0 (p * q)" ``` haftmann@52380 ` 815` ``` by (induct p) (simp add: mult_poly_0_left, simp add: algebra_simps) ``` huffman@29474 ` 816` huffman@29474 ` 817` ```lemmas mult_poly_0 = mult_poly_0_left mult_poly_0_right ``` huffman@29474 ` 818` haftmann@52380 ` 819` ```lemma mult_smult_left [simp]: ``` haftmann@52380 ` 820` ``` "smult a p * q = smult a (p * q)" ``` haftmann@52380 ` 821` ``` by (induct p) (simp add: mult_poly_0, simp add: smult_add_right) ``` huffman@29474 ` 822` haftmann@52380 ` 823` ```lemma mult_smult_right [simp]: ``` haftmann@52380 ` 824` ``` "p * smult a q = smult a (p * q)" ``` haftmann@52380 ` 825` ``` by (induct q) (simp add: mult_poly_0, simp add: smult_add_right) ``` huffman@29474 ` 826` huffman@29474 ` 827` ```lemma mult_poly_add_left: ``` huffman@29474 ` 828` ``` fixes p q r :: "'a poly" ``` huffman@29474 ` 829` ``` shows "(p + q) * r = p * r + q * r" ``` haftmann@52380 ` 830` ``` by (induct r) (simp add: mult_poly_0, simp add: smult_distribs algebra_simps) ``` huffman@29451 ` 831` wenzelm@60679 ` 832` ```instance ``` wenzelm@60679 ` 833` ```proof ``` huffman@29451 ` 834` ``` fix p q r :: "'a poly" ``` huffman@29451 ` 835` ``` show 0: "0 * p = 0" ``` huffman@29474 ` 836` ``` by (rule mult_poly_0_left) ``` huffman@29451 ` 837` ``` show "p * 0 = 0" ``` huffman@29474 ` 838` ``` by (rule mult_poly_0_right) ``` huffman@29451 ` 839` ``` show "(p + q) * r = p * r + q * r" ``` huffman@29474 ` 840` ``` by (rule mult_poly_add_left) ``` huffman@29451 ` 841` ``` show "(p * q) * r = p * (q * r)" ``` huffman@29474 ` 842` ``` by (induct p, simp add: mult_poly_0, simp add: mult_poly_add_left) ``` huffman@29451 ` 843` ``` show "p * q = q * p" ``` huffman@29474 ` 844` ``` by (induct p, simp add: mult_poly_0, simp) ``` huffman@29451 ` 845` ```qed ``` huffman@29451 ` 846` huffman@29451 ` 847` ```end ``` huffman@29451 ` 848` huffman@29540 ` 849` ```instance poly :: (comm_semiring_0_cancel) comm_semiring_0_cancel .. ``` huffman@29540 ` 850` huffman@29474 ` 851` ```lemma coeff_mult: ``` huffman@29474 ` 852` ``` "coeff (p * q) n = (\i\n. coeff p i * coeff q (n-i))" ``` huffman@29474 ` 853` ```proof (induct p arbitrary: n) ``` huffman@29474 ` 854` ``` case 0 show ?case by simp ``` huffman@29474 ` 855` ```next ``` huffman@29474 ` 856` ``` case (pCons a p n) thus ?case ``` huffman@29474 ` 857` ``` by (cases n, simp, simp add: setsum_atMost_Suc_shift ``` huffman@29474 ` 858` ``` del: setsum_atMost_Suc) ``` huffman@29474 ` 859` ```qed ``` huffman@29451 ` 860` huffman@29474 ` 861` ```lemma degree_mult_le: "degree (p * q) \ degree p + degree q" ``` huffman@29474 ` 862` ```apply (rule degree_le) ``` huffman@29474 ` 863` ```apply (induct p) ``` huffman@29474 ` 864` ```apply simp ``` huffman@29474 ` 865` ```apply (simp add: coeff_eq_0 coeff_pCons split: nat.split) ``` huffman@29451 ` 866` ```done ``` huffman@29451 ` 867` huffman@29451 ` 868` ```lemma mult_monom: "monom a m * monom b n = monom (a * b) (m + n)" ``` wenzelm@60679 ` 869` ``` by (induct m) (simp add: monom_0 smult_monom, simp add: monom_Suc) ``` huffman@29451 ` 870` huffman@29451 ` 871` ```instantiation poly :: (comm_semiring_1) comm_semiring_1 ``` huffman@29451 ` 872` ```begin ``` huffman@29451 ` 873` wenzelm@60679 ` 874` ```definition one_poly_def: "1 = pCons 1 0" ``` huffman@29451 ` 875` wenzelm@60679 ` 876` ```instance ``` wenzelm@60679 ` 877` ```proof ``` wenzelm@60679 ` 878` ``` show "1 * p = p" for p :: "'a poly" ``` haftmann@52380 ` 879` ``` unfolding one_poly_def by simp ``` huffman@29451 ` 880` ``` show "0 \ (1::'a poly)" ``` huffman@29451 ` 881` ``` unfolding one_poly_def by simp ``` huffman@29451 ` 882` ```qed ``` huffman@29451 ` 883` huffman@29451 ` 884` ```end ``` huffman@29451 ` 885` haftmann@52380 ` 886` ```instance poly :: (comm_ring) comm_ring .. ``` haftmann@52380 ` 887` haftmann@52380 ` 888` ```instance poly :: (comm_ring_1) comm_ring_1 .. ``` haftmann@52380 ` 889` huffman@29451 ` 890` ```lemma coeff_1 [simp]: "coeff 1 n = (if n = 0 then 1 else 0)" ``` huffman@29451 ` 891` ``` unfolding one_poly_def ``` huffman@29451 ` 892` ``` by (simp add: coeff_pCons split: nat.split) ``` huffman@29451 ` 893` haftmann@60570 ` 894` ```lemma monom_eq_1 [simp]: ``` haftmann@60570 ` 895` ``` "monom 1 0 = 1" ``` haftmann@60570 ` 896` ``` by (simp add: monom_0 one_poly_def) ``` haftmann@60570 ` 897` ``` ``` huffman@29451 ` 898` ```lemma degree_1 [simp]: "degree 1 = 0" ``` huffman@29451 ` 899` ``` unfolding one_poly_def ``` huffman@29451 ` 900` ``` by (rule degree_pCons_0) ``` huffman@29451 ` 901` haftmann@52380 ` 902` ```lemma coeffs_1_eq [simp, code abstract]: ``` haftmann@52380 ` 903` ``` "coeffs 1 = [1]" ``` haftmann@52380 ` 904` ``` by (simp add: one_poly_def) ``` haftmann@52380 ` 905` haftmann@52380 ` 906` ```lemma degree_power_le: ``` haftmann@52380 ` 907` ``` "degree (p ^ n) \ degree p * n" ``` haftmann@52380 ` 908` ``` by (induct n) (auto intro: order_trans degree_mult_le) ``` haftmann@52380 ` 909` haftmann@52380 ` 910` ```lemma poly_smult [simp]: ``` haftmann@52380 ` 911` ``` "poly (smult a p) x = a * poly p x" ``` haftmann@52380 ` 912` ``` by (induct p, simp, simp add: algebra_simps) ``` haftmann@52380 ` 913` haftmann@52380 ` 914` ```lemma poly_mult [simp]: ``` haftmann@52380 ` 915` ``` "poly (p * q) x = poly p x * poly q x" ``` haftmann@52380 ` 916` ``` by (induct p, simp_all, simp add: algebra_simps) ``` haftmann@52380 ` 917` haftmann@52380 ` 918` ```lemma poly_1 [simp]: ``` haftmann@52380 ` 919` ``` "poly 1 x = 1" ``` haftmann@52380 ` 920` ``` by (simp add: one_poly_def) ``` haftmann@52380 ` 921` haftmann@52380 ` 922` ```lemma poly_power [simp]: ``` haftmann@52380 ` 923` ``` fixes p :: "'a::{comm_semiring_1} poly" ``` haftmann@52380 ` 924` ``` shows "poly (p ^ n) x = poly p x ^ n" ``` haftmann@52380 ` 925` ``` by (induct n) simp_all ``` haftmann@52380 ` 926` haftmann@52380 ` 927` wenzelm@60500 ` 928` ```subsection \Lemmas about divisibility\ ``` huffman@29979 ` 929` huffman@29979 ` 930` ```lemma dvd_smult: "p dvd q \ p dvd smult a q" ``` huffman@29979 ` 931` ```proof - ``` huffman@29979 ` 932` ``` assume "p dvd q" ``` huffman@29979 ` 933` ``` then obtain k where "q = p * k" .. ``` huffman@29979 ` 934` ``` then have "smult a q = p * smult a k" by simp ``` huffman@29979 ` 935` ``` then show "p dvd smult a q" .. ``` huffman@29979 ` 936` ```qed ``` huffman@29979 ` 937` huffman@29979 ` 938` ```lemma dvd_smult_cancel: ``` huffman@29979 ` 939` ``` fixes a :: "'a::field" ``` huffman@29979 ` 940` ``` shows "p dvd smult a q \ a \ 0 \ p dvd q" ``` huffman@29979 ` 941` ``` by (drule dvd_smult [where a="inverse a"]) simp ``` huffman@29979 ` 942` huffman@29979 ` 943` ```lemma dvd_smult_iff: ``` huffman@29979 ` 944` ``` fixes a :: "'a::field" ``` huffman@29979 ` 945` ``` shows "a \ 0 \ p dvd smult a q \ p dvd q" ``` huffman@29979 ` 946` ``` by (safe elim!: dvd_smult dvd_smult_cancel) ``` huffman@29979 ` 947` huffman@31663 ` 948` ```lemma smult_dvd_cancel: ``` huffman@31663 ` 949` ``` "smult a p dvd q \ p dvd q" ``` huffman@31663 ` 950` ```proof - ``` huffman@31663 ` 951` ``` assume "smult a p dvd q" ``` huffman@31663 ` 952` ``` then obtain k where "q = smult a p * k" .. ``` huffman@31663 ` 953` ``` then have "q = p * smult a k" by simp ``` huffman@31663 ` 954` ``` then show "p dvd q" .. ``` huffman@31663 ` 955` ```qed ``` huffman@31663 ` 956` huffman@31663 ` 957` ```lemma smult_dvd: ``` huffman@31663 ` 958` ``` fixes a :: "'a::field" ``` huffman@31663 ` 959` ``` shows "p dvd q \ a \ 0 \ smult a p dvd q" ``` huffman@31663 ` 960` ``` by (rule smult_dvd_cancel [where a="inverse a"]) simp ``` huffman@31663 ` 961` huffman@31663 ` 962` ```lemma smult_dvd_iff: ``` huffman@31663 ` 963` ``` fixes a :: "'a::field" ``` huffman@31663 ` 964` ``` shows "smult a p dvd q \ (if a = 0 then q = 0 else p dvd q)" ``` huffman@31663 ` 965` ``` by (auto elim: smult_dvd smult_dvd_cancel) ``` huffman@31663 ` 966` huffman@29451 ` 967` wenzelm@60500 ` 968` ```subsection \Polynomials form an integral domain\ ``` huffman@29451 ` 969` huffman@29451 ` 970` ```lemma coeff_mult_degree_sum: ``` huffman@29451 ` 971` ``` "coeff (p * q) (degree p + degree q) = ``` huffman@29451 ` 972` ``` coeff p (degree p) * coeff q (degree q)" ``` huffman@29471 ` 973` ``` by (induct p, simp, simp add: coeff_eq_0) ``` huffman@29451 ` 974` huffman@29451 ` 975` ```instance poly :: (idom) idom ``` huffman@29451 ` 976` ```proof ``` huffman@29451 ` 977` ``` fix p q :: "'a poly" ``` huffman@29451 ` 978` ``` assume "p \ 0" and "q \ 0" ``` huffman@29451 ` 979` ``` have "coeff (p * q) (degree p + degree q) = ``` huffman@29451 ` 980` ``` coeff p (degree p) * coeff q (degree q)" ``` huffman@29451 ` 981` ``` by (rule coeff_mult_degree_sum) ``` huffman@29451 ` 982` ``` also have "coeff p (degree p) * coeff q (degree q) \ 0" ``` wenzelm@60500 ` 983` ``` using \p \ 0\ and \q \ 0\ by simp ``` huffman@29451 ` 984` ``` finally have "\n. coeff (p * q) n \ 0" .. ``` haftmann@52380 ` 985` ``` thus "p * q \ 0" by (simp add: poly_eq_iff) ``` huffman@29451 ` 986` ```qed ``` huffman@29451 ` 987` huffman@29451 ` 988` ```lemma degree_mult_eq: ``` huffman@29451 ` 989` ``` fixes p q :: "'a::idom poly" ``` huffman@29451 ` 990` ``` shows "\p \ 0; q \ 0\ \ degree (p * q) = degree p + degree q" ``` huffman@29451 ` 991` ```apply (rule order_antisym [OF degree_mult_le le_degree]) ``` huffman@29451 ` 992` ```apply (simp add: coeff_mult_degree_sum) ``` huffman@29451 ` 993` ```done ``` huffman@29451 ` 994` haftmann@60570 ` 995` ```lemma degree_mult_right_le: ``` haftmann@60570 ` 996` ``` fixes p q :: "'a::idom poly" ``` haftmann@60570 ` 997` ``` assumes "q \ 0" ``` haftmann@60570 ` 998` ``` shows "degree p \ degree (p * q)" ``` haftmann@60570 ` 999` ``` using assms by (cases "p = 0") (simp_all add: degree_mult_eq) ``` haftmann@60570 ` 1000` haftmann@60570 ` 1001` ```lemma coeff_degree_mult: ``` haftmann@60570 ` 1002` ``` fixes p q :: "'a::idom poly" ``` haftmann@60570 ` 1003` ``` shows "coeff (p * q) (degree (p * q)) = ``` haftmann@60570 ` 1004` ``` coeff q (degree q) * coeff p (degree p)" ``` haftmann@60570 ` 1005` ``` by (cases "p = 0 \ q = 0") (auto simp add: degree_mult_eq coeff_mult_degree_sum) ``` haftmann@60570 ` 1006` huffman@29451 ` 1007` ```lemma dvd_imp_degree_le: ``` huffman@29451 ` 1008` ``` fixes p q :: "'a::idom poly" ``` huffman@29451 ` 1009` ``` shows "\p dvd q; q \ 0\ \ degree p \ degree q" ``` huffman@29451 ` 1010` ``` by (erule dvdE, simp add: degree_mult_eq) ``` huffman@29451 ` 1011` huffman@29451 ` 1012` wenzelm@60500 ` 1013` ```subsection \Polynomials form an ordered integral domain\ ``` huffman@29878 ` 1014` haftmann@52380 ` 1015` ```definition pos_poly :: "'a::linordered_idom poly \ bool" ``` huffman@29878 ` 1016` ```where ``` huffman@29878 ` 1017` ``` "pos_poly p \ 0 < coeff p (degree p)" ``` huffman@29878 ` 1018` huffman@29878 ` 1019` ```lemma pos_poly_pCons: ``` huffman@29878 ` 1020` ``` "pos_poly (pCons a p) \ pos_poly p \ (p = 0 \ 0 < a)" ``` huffman@29878 ` 1021` ``` unfolding pos_poly_def by simp ``` huffman@29878 ` 1022` huffman@29878 ` 1023` ```lemma not_pos_poly_0 [simp]: "\ pos_poly 0" ``` huffman@29878 ` 1024` ``` unfolding pos_poly_def by simp ``` huffman@29878 ` 1025` huffman@29878 ` 1026` ```lemma pos_poly_add: "\pos_poly p; pos_poly q\ \ pos_poly (p + q)" ``` huffman@29878 ` 1027` ``` apply (induct p arbitrary: q, simp) ``` huffman@29878 ` 1028` ``` apply (case_tac q, force simp add: pos_poly_pCons add_pos_pos) ``` huffman@29878 ` 1029` ``` done ``` huffman@29878 ` 1030` huffman@29878 ` 1031` ```lemma pos_poly_mult: "\pos_poly p; pos_poly q\ \ pos_poly (p * q)" ``` huffman@29878 ` 1032` ``` unfolding pos_poly_def ``` huffman@29878 ` 1033` ``` apply (subgoal_tac "p \ 0 \ q \ 0") ``` nipkow@56544 ` 1034` ``` apply (simp add: degree_mult_eq coeff_mult_degree_sum) ``` huffman@29878 ` 1035` ``` apply auto ``` huffman@29878 ` 1036` ``` done ``` huffman@29878 ` 1037` huffman@29878 ` 1038` ```lemma pos_poly_total: "p = 0 \ pos_poly p \ pos_poly (- p)" ``` huffman@29878 ` 1039` ```by (induct p) (auto simp add: pos_poly_pCons) ``` huffman@29878 ` 1040` haftmann@52380 ` 1041` ```lemma last_coeffs_eq_coeff_degree: ``` haftmann@52380 ` 1042` ``` "p \ 0 \ last (coeffs p) = coeff p (degree p)" ``` haftmann@52380 ` 1043` ``` by (simp add: coeffs_def) ``` haftmann@52380 ` 1044` haftmann@52380 ` 1045` ```lemma pos_poly_coeffs [code]: ``` haftmann@52380 ` 1046` ``` "pos_poly p \ (let as = coeffs p in as \ [] \ last as > 0)" (is "?P \ ?Q") ``` haftmann@52380 ` 1047` ```proof ``` haftmann@52380 ` 1048` ``` assume ?Q then show ?P by (auto simp add: pos_poly_def last_coeffs_eq_coeff_degree) ``` haftmann@52380 ` 1049` ```next ``` haftmann@52380 ` 1050` ``` assume ?P then have *: "0 < coeff p (degree p)" by (simp add: pos_poly_def) ``` haftmann@52380 ` 1051` ``` then have "p \ 0" by auto ``` haftmann@52380 ` 1052` ``` with * show ?Q by (simp add: last_coeffs_eq_coeff_degree) ``` haftmann@52380 ` 1053` ```qed ``` haftmann@52380 ` 1054` haftmann@35028 ` 1055` ```instantiation poly :: (linordered_idom) linordered_idom ``` huffman@29878 ` 1056` ```begin ``` huffman@29878 ` 1057` huffman@29878 ` 1058` ```definition ``` haftmann@37765 ` 1059` ``` "x < y \ pos_poly (y - x)" ``` huffman@29878 ` 1060` huffman@29878 ` 1061` ```definition ``` haftmann@37765 ` 1062` ``` "x \ y \ x = y \ pos_poly (y - x)" ``` huffman@29878 ` 1063` huffman@29878 ` 1064` ```definition ``` haftmann@37765 ` 1065` ``` "abs (x::'a poly) = (if x < 0 then - x else x)" ``` huffman@29878 ` 1066` huffman@29878 ` 1067` ```definition ``` haftmann@37765 ` 1068` ``` "sgn (x::'a poly) = (if x = 0 then 0 else if 0 < x then 1 else - 1)" ``` huffman@29878 ` 1069` wenzelm@60679 ` 1070` ```instance ``` wenzelm@60679 ` 1071` ```proof ``` wenzelm@60679 ` 1072` ``` fix x y z :: "'a poly" ``` huffman@29878 ` 1073` ``` show "x < y \ x \ y \ \ y \ x" ``` huffman@29878 ` 1074` ``` unfolding less_eq_poly_def less_poly_def ``` huffman@29878 ` 1075` ``` apply safe ``` huffman@29878 ` 1076` ``` apply simp ``` huffman@29878 ` 1077` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 1078` ``` apply simp ``` huffman@29878 ` 1079` ``` done ``` wenzelm@60679 ` 1080` ``` show "x \ x" ``` huffman@29878 ` 1081` ``` unfolding less_eq_poly_def by simp ``` wenzelm@60679 ` 1082` ``` show "x \ y \ y \ z \ x \ z" ``` huffman@29878 ` 1083` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 1084` ``` apply safe ``` huffman@29878 ` 1085` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 1086` ``` apply (simp add: algebra_simps) ``` huffman@29878 ` 1087` ``` done ``` wenzelm@60679 ` 1088` ``` show "x \ y \ y \ x \ x = y" ``` huffman@29878 ` 1089` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 1090` ``` apply safe ``` huffman@29878 ` 1091` ``` apply (drule (1) pos_poly_add) ``` huffman@29878 ` 1092` ``` apply simp ``` huffman@29878 ` 1093` ``` done ``` wenzelm@60679 ` 1094` ``` show "x \ y \ z + x \ z + y" ``` huffman@29878 ` 1095` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 1096` ``` apply safe ``` huffman@29878 ` 1097` ``` apply (simp add: algebra_simps) ``` huffman@29878 ` 1098` ``` done ``` huffman@29878 ` 1099` ``` show "x \ y \ y \ x" ``` huffman@29878 ` 1100` ``` unfolding less_eq_poly_def ``` huffman@29878 ` 1101` ``` using pos_poly_total [of "x - y"] ``` huffman@29878 ` 1102` ``` by auto ``` wenzelm@60679 ` 1103` ``` show "x < y \ 0 < z \ z * x < z * y" ``` huffman@29878 ` 1104` ``` unfolding less_poly_def ``` huffman@29878 ` 1105` ``` by (simp add: right_diff_distrib [symmetric] pos_poly_mult) ``` huffman@29878 ` 1106` ``` show "\x\ = (if x < 0 then - x else x)" ``` huffman@29878 ` 1107` ``` by (rule abs_poly_def) ``` huffman@29878 ` 1108` ``` show "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)" ``` huffman@29878 ` 1109` ``` by (rule sgn_poly_def) ``` huffman@29878 ` 1110` ```qed ``` huffman@29878 ` 1111` huffman@29878 ` 1112` ```end ``` huffman@29878 ` 1113` wenzelm@60500 ` 1114` ```text \TODO: Simplification rules for comparisons\ ``` huffman@29878 ` 1115` huffman@29878 ` 1116` wenzelm@60500 ` 1117` ```subsection \Synthetic division and polynomial roots\ ``` haftmann@52380 ` 1118` wenzelm@60500 ` 1119` ```text \ ``` haftmann@52380 ` 1120` ``` Synthetic division is simply division by the linear polynomial @{term "x - c"}. ``` wenzelm@60500 ` 1121` ```\ ``` haftmann@52380 ` 1122` haftmann@52380 ` 1123` ```definition synthetic_divmod :: "'a::comm_semiring_0 poly \ 'a \ 'a poly \ 'a" ``` haftmann@52380 ` 1124` ```where ``` haftmann@52380 ` 1125` ``` "synthetic_divmod p c = fold_coeffs (\a (q, r). (pCons r q, a + c * r)) p (0, 0)" ``` haftmann@52380 ` 1126` haftmann@52380 ` 1127` ```definition synthetic_div :: "'a::comm_semiring_0 poly \ 'a \ 'a poly" ``` haftmann@52380 ` 1128` ```where ``` haftmann@52380 ` 1129` ``` "synthetic_div p c = fst (synthetic_divmod p c)" ``` haftmann@52380 ` 1130` haftmann@52380 ` 1131` ```lemma synthetic_divmod_0 [simp]: ``` haftmann@52380 ` 1132` ``` "synthetic_divmod 0 c = (0, 0)" ``` haftmann@52380 ` 1133` ``` by (simp add: synthetic_divmod_def) ``` haftmann@52380 ` 1134` haftmann@52380 ` 1135` ```lemma synthetic_divmod_pCons [simp]: ``` haftmann@52380 ` 1136` ``` "synthetic_divmod (pCons a p) c = (\(q, r). (pCons r q, a + c * r)) (synthetic_divmod p c)" ``` haftmann@52380 ` 1137` ``` by (cases "p = 0 \ a = 0") (auto simp add: synthetic_divmod_def) ``` haftmann@52380 ` 1138` haftmann@52380 ` 1139` ```lemma synthetic_div_0 [simp]: ``` haftmann@52380 ` 1140` ``` "synthetic_div 0 c = 0" ``` haftmann@52380 ` 1141` ``` unfolding synthetic_div_def by simp ``` haftmann@52380 ` 1142` haftmann@52380 ` 1143` ```lemma synthetic_div_unique_lemma: "smult c p = pCons a p \ p = 0" ``` haftmann@52380 ` 1144` ```by (induct p arbitrary: a) simp_all ``` haftmann@52380 ` 1145` haftmann@52380 ` 1146` ```lemma snd_synthetic_divmod: ``` haftmann@52380 ` 1147` ``` "snd (synthetic_divmod p c) = poly p c" ``` haftmann@52380 ` 1148` ``` by (induct p, simp, simp add: split_def) ``` haftmann@52380 ` 1149` haftmann@52380 ` 1150` ```lemma synthetic_div_pCons [simp]: ``` haftmann@52380 ` 1151` ``` "synthetic_div (pCons a p) c = pCons (poly p c) (synthetic_div p c)" ``` haftmann@52380 ` 1152` ``` unfolding synthetic_div_def ``` haftmann@52380 ` 1153` ``` by (simp add: split_def snd_synthetic_divmod) ``` haftmann@52380 ` 1154` haftmann@52380 ` 1155` ```lemma synthetic_div_eq_0_iff: ``` haftmann@52380 ` 1156` ``` "synthetic_div p c = 0 \ degree p = 0" ``` haftmann@52380 ` 1157` ``` by (induct p, simp, case_tac p, simp) ``` haftmann@52380 ` 1158` haftmann@52380 ` 1159` ```lemma degree_synthetic_div: ``` haftmann@52380 ` 1160` ``` "degree (synthetic_div p c) = degree p - 1" ``` haftmann@52380 ` 1161` ``` by (induct p, simp, simp add: synthetic_div_eq_0_iff) ``` haftmann@52380 ` 1162` haftmann@52380 ` 1163` ```lemma synthetic_div_correct: ``` haftmann@52380 ` 1164` ``` "p + smult c (synthetic_div p c) = pCons (poly p c) (synthetic_div p c)" ``` haftmann@52380 ` 1165` ``` by (induct p) simp_all ``` haftmann@52380 ` 1166` haftmann@52380 ` 1167` ```lemma synthetic_div_unique: ``` haftmann@52380 ` 1168` ``` "p + smult c q = pCons r q \ r = poly p c \ q = synthetic_div p c" ``` haftmann@52380 ` 1169` ```apply (induct p arbitrary: q r) ``` haftmann@52380 ` 1170` ```apply (simp, frule synthetic_div_unique_lemma, simp) ``` haftmann@52380 ` 1171` ```apply (case_tac q, force) ``` haftmann@52380 ` 1172` ```done ``` haftmann@52380 ` 1173` haftmann@52380 ` 1174` ```lemma synthetic_div_correct': ``` haftmann@52380 ` 1175` ``` fixes c :: "'a::comm_ring_1" ``` haftmann@52380 ` 1176` ``` shows "[:-c, 1:] * synthetic_div p c + [:poly p c:] = p" ``` haftmann@52380 ` 1177` ``` using synthetic_div_correct [of p c] ``` haftmann@52380 ` 1178` ``` by (simp add: algebra_simps) ``` haftmann@52380 ` 1179` haftmann@52380 ` 1180` ```lemma poly_eq_0_iff_dvd: ``` haftmann@52380 ` 1181` ``` fixes c :: "'a::idom" ``` haftmann@52380 ` 1182` ``` shows "poly p c = 0 \ [:-c, 1:] dvd p" ``` haftmann@52380 ` 1183` ```proof ``` haftmann@52380 ` 1184` ``` assume "poly p c = 0" ``` haftmann@52380 ` 1185` ``` with synthetic_div_correct' [of c p] ``` haftmann@52380 ` 1186` ``` have "p = [:-c, 1:] * synthetic_div p c" by simp ``` haftmann@52380 ` 1187` ``` then show "[:-c, 1:] dvd p" .. ``` haftmann@52380 ` 1188` ```next ``` haftmann@52380 ` 1189` ``` assume "[:-c, 1:] dvd p" ``` haftmann@52380 ` 1190` ``` then obtain k where "p = [:-c, 1:] * k" by (rule dvdE) ``` haftmann@52380 ` 1191` ``` then show "poly p c = 0" by simp ``` haftmann@52380 ` 1192` ```qed ``` haftmann@52380 ` 1193` haftmann@52380 ` 1194` ```lemma dvd_iff_poly_eq_0: ``` haftmann@52380 ` 1195` ``` fixes c :: "'a::idom" ``` haftmann@52380 ` 1196` ``` shows "[:c, 1:] dvd p \ poly p (-c) = 0" ``` haftmann@52380 ` 1197` ``` by (simp add: poly_eq_0_iff_dvd) ``` haftmann@52380 ` 1198` haftmann@52380 ` 1199` ```lemma poly_roots_finite: ``` haftmann@52380 ` 1200` ``` fixes p :: "'a::idom poly" ``` haftmann@52380 ` 1201` ``` shows "p \ 0 \ finite {x. poly p x = 0}" ``` haftmann@52380 ` 1202` ```proof (induct n \ "degree p" arbitrary: p) ``` haftmann@52380 ` 1203` ``` case (0 p) ``` haftmann@52380 ` 1204` ``` then obtain a where "a \ 0" and "p = [:a:]" ``` haftmann@52380 ` 1205` ``` by (cases p, simp split: if_splits) ``` haftmann@52380 ` 1206` ``` then show "finite {x. poly p x = 0}" by simp ``` haftmann@52380 ` 1207` ```next ``` haftmann@52380 ` 1208` ``` case (Suc n p) ``` haftmann@52380 ` 1209` ``` show "finite {x. poly p x = 0}" ``` haftmann@52380 ` 1210` ``` proof (cases "\x. poly p x = 0") ``` haftmann@52380 ` 1211` ``` case False ``` haftmann@52380 ` 1212` ``` then show "finite {x. poly p x = 0}" by simp ``` haftmann@52380 ` 1213` ``` next ``` haftmann@52380 ` 1214` ``` case True ``` haftmann@52380 ` 1215` ``` then obtain a where "poly p a = 0" .. ``` haftmann@52380 ` 1216` ``` then have "[:-a, 1:] dvd p" by (simp only: poly_eq_0_iff_dvd) ``` haftmann@52380 ` 1217` ``` then obtain k where k: "p = [:-a, 1:] * k" .. ``` wenzelm@60500 ` 1218` ``` with \p \ 0\ have "k \ 0" by auto ``` haftmann@52380 ` 1219` ``` with k have "degree p = Suc (degree k)" ``` haftmann@52380 ` 1220` ``` by (simp add: degree_mult_eq del: mult_pCons_left) ``` wenzelm@60500 ` 1221` ``` with \Suc n = degree p\ have "n = degree k" by simp ``` wenzelm@60500 ` 1222` ``` then have "finite {x. poly k x = 0}" using \k \ 0\ by (rule Suc.hyps) ``` haftmann@52380 ` 1223` ``` then have "finite (insert a {x. poly k x = 0})" by simp ``` haftmann@52380 ` 1224` ``` then show "finite {x. poly p x = 0}" ``` wenzelm@57862 ` 1225` ``` by (simp add: k Collect_disj_eq del: mult_pCons_left) ``` haftmann@52380 ` 1226` ``` qed ``` haftmann@52380 ` 1227` ```qed ``` haftmann@52380 ` 1228` haftmann@52380 ` 1229` ```lemma poly_eq_poly_eq_iff: ``` haftmann@52380 ` 1230` ``` fixes p q :: "'a::{idom,ring_char_0} poly" ``` haftmann@52380 ` 1231` ``` shows "poly p = poly q \ p = q" (is "?P \ ?Q") ``` haftmann@52380 ` 1232` ```proof ``` haftmann@52380 ` 1233` ``` assume ?Q then show ?P by simp ``` haftmann@52380 ` 1234` ```next ``` haftmann@52380 ` 1235` ``` { fix p :: "'a::{idom,ring_char_0} poly" ``` haftmann@52380 ` 1236` ``` have "poly p = poly 0 \ p = 0" ``` haftmann@52380 ` 1237` ``` apply (cases "p = 0", simp_all) ``` haftmann@52380 ` 1238` ``` apply (drule poly_roots_finite) ``` haftmann@52380 ` 1239` ``` apply (auto simp add: infinite_UNIV_char_0) ``` haftmann@52380 ` 1240` ``` done ``` haftmann@52380 ` 1241` ``` } note this [of "p - q"] ``` haftmann@52380 ` 1242` ``` moreover assume ?P ``` haftmann@52380 ` 1243` ``` ultimately show ?Q by auto ``` haftmann@52380 ` 1244` ```qed ``` haftmann@52380 ` 1245` haftmann@52380 ` 1246` ```lemma poly_all_0_iff_0: ``` haftmann@52380 ` 1247` ``` fixes p :: "'a::{ring_char_0, idom} poly" ``` haftmann@52380 ` 1248` ``` shows "(\x. poly p x = 0) \ p = 0" ``` haftmann@52380 ` 1249` ``` by (auto simp add: poly_eq_poly_eq_iff [symmetric]) ``` haftmann@52380 ` 1250` haftmann@52380 ` 1251` wenzelm@60500 ` 1252` ```subsection \Long division of polynomials\ ``` huffman@29451 ` 1253` haftmann@52380 ` 1254` ```definition pdivmod_rel :: "'a::field poly \ 'a poly \ 'a poly \ 'a poly \ bool" ``` huffman@29451 ` 1255` ```where ``` huffman@29537 ` 1256` ``` "pdivmod_rel x y q r \ ``` huffman@29451 ` 1257` ``` x = q * y + r \ (if y = 0 then q = 0 else r = 0 \ degree r < degree y)" ``` huffman@29451 ` 1258` huffman@29537 ` 1259` ```lemma pdivmod_rel_0: ``` huffman@29537 ` 1260` ``` "pdivmod_rel 0 y 0 0" ``` huffman@29537 ` 1261` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 1262` huffman@29537 ` 1263` ```lemma pdivmod_rel_by_0: ``` huffman@29537 ` 1264` ``` "pdivmod_rel x 0 0 x" ``` huffman@29537 ` 1265` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 1266` huffman@29451 ` 1267` ```lemma eq_zero_or_degree_less: ``` huffman@29451 ` 1268` ``` assumes "degree p \ n" and "coeff p n = 0" ``` huffman@29451 ` 1269` ``` shows "p = 0 \ degree p < n" ``` huffman@29451 ` 1270` ```proof (cases n) ``` huffman@29451 ` 1271` ``` case 0 ``` wenzelm@60500 ` 1272` ``` with \degree p \ n\ and \coeff p n = 0\ ``` huffman@29451 ` 1273` ``` have "coeff p (degree p) = 0" by simp ``` huffman@29451 ` 1274` ``` then have "p = 0" by simp ``` huffman@29451 ` 1275` ``` then show ?thesis .. ``` huffman@29451 ` 1276` ```next ``` huffman@29451 ` 1277` ``` case (Suc m) ``` huffman@29451 ` 1278` ``` have "\i>n. coeff p i = 0" ``` wenzelm@60500 ` 1279` ``` using \degree p \ n\ by (simp add: coeff_eq_0) ``` huffman@29451 ` 1280` ``` then have "\i\n. coeff p i = 0" ``` wenzelm@60500 ` 1281` ``` using \coeff p n = 0\ by (simp add: le_less) ``` huffman@29451 ` 1282` ``` then have "\i>m. coeff p i = 0" ``` wenzelm@60500 ` 1283` ``` using \n = Suc m\ by (simp add: less_eq_Suc_le) ``` huffman@29451 ` 1284` ``` then have "degree p \ m" ``` huffman@29451 ` 1285` ``` by (rule degree_le) ``` huffman@29451 ` 1286` ``` then have "degree p < n" ``` wenzelm@60500 ` 1287` ``` using \n = Suc m\ by (simp add: less_Suc_eq_le) ``` huffman@29451 ` 1288` ``` then show ?thesis .. ``` huffman@29451 ` 1289` ```qed ``` huffman@29451 ` 1290` huffman@29537 ` 1291` ```lemma pdivmod_rel_pCons: ``` huffman@29537 ` 1292` ``` assumes rel: "pdivmod_rel x y q r" ``` huffman@29451 ` 1293` ``` assumes y: "y \ 0" ``` huffman@29451 ` 1294` ``` assumes b: "b = coeff (pCons a r) (degree y) / coeff y (degree y)" ``` huffman@29537 ` 1295` ``` shows "pdivmod_rel (pCons a x) y (pCons b q) (pCons a r - smult b y)" ``` huffman@29537 ` 1296` ``` (is "pdivmod_rel ?x y ?q ?r") ``` huffman@29451 ` 1297` ```proof - ``` huffman@29451 ` 1298` ``` have x: "x = q * y + r" and r: "r = 0 \ degree r < degree y" ``` huffman@29537 ` 1299` ``` using assms unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 1300` huffman@29451 ` 1301` ``` have 1: "?x = ?q * y + ?r" ``` huffman@29451 ` 1302` ``` using b x by simp ``` huffman@29451 ` 1303` huffman@29451 ` 1304` ``` have 2: "?r = 0 \ degree ?r < degree y" ``` huffman@29451 ` 1305` ``` proof (rule eq_zero_or_degree_less) ``` huffman@29539 ` 1306` ``` show "degree ?r \ degree y" ``` huffman@29539 ` 1307` ``` proof (rule degree_diff_le) ``` huffman@29451 ` 1308` ``` show "degree (pCons a r) \ degree y" ``` huffman@29460 ` 1309` ``` using r by auto ``` huffman@29451 ` 1310` ``` show "degree (smult b y) \ degree y" ``` huffman@29451 ` 1311` ``` by (rule degree_smult_le) ``` huffman@29451 ` 1312` ``` qed ``` huffman@29451 ` 1313` ``` next ``` huffman@29451 ` 1314` ``` show "coeff ?r (degree y) = 0" ``` wenzelm@60500 ` 1315` ``` using \y \ 0\ unfolding b by simp ``` huffman@29451 ` 1316` ``` qed ``` huffman@29451 ` 1317` huffman@29451 ` 1318` ``` from 1 2 show ?thesis ``` huffman@29537 ` 1319` ``` unfolding pdivmod_rel_def ``` wenzelm@60500 ` 1320` ``` using \y \ 0\ by simp ``` huffman@29451 ` 1321` ```qed ``` huffman@29451 ` 1322` huffman@29537 ` 1323` ```lemma pdivmod_rel_exists: "\q r. pdivmod_rel x y q r" ``` huffman@29451 ` 1324` ```apply (cases "y = 0") ``` huffman@29537 ` 1325` ```apply (fast intro!: pdivmod_rel_by_0) ``` huffman@29451 ` 1326` ```apply (induct x) ``` huffman@29537 ` 1327` ```apply (fast intro!: pdivmod_rel_0) ``` huffman@29537 ` 1328` ```apply (fast intro!: pdivmod_rel_pCons) ``` huffman@29451 ` 1329` ```done ``` huffman@29451 ` 1330` huffman@29537 ` 1331` ```lemma pdivmod_rel_unique: ``` huffman@29537 ` 1332` ``` assumes 1: "pdivmod_rel x y q1 r1" ``` huffman@29537 ` 1333` ``` assumes 2: "pdivmod_rel x y q2 r2" ``` huffman@29451 ` 1334` ``` shows "q1 = q2 \ r1 = r2" ``` huffman@29451 ` 1335` ```proof (cases "y = 0") ``` huffman@29451 ` 1336` ``` assume "y = 0" with assms show ?thesis ``` huffman@29537 ` 1337` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 1338` ```next ``` huffman@29451 ` 1339` ``` assume [simp]: "y \ 0" ``` huffman@29451 ` 1340` ``` from 1 have q1: "x = q1 * y + r1" and r1: "r1 = 0 \ degree r1 < degree y" ``` huffman@29537 ` 1341` ``` unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 1342` ``` from 2 have q2: "x = q2 * y + r2" and r2: "r2 = 0 \ degree r2 < degree y" ``` huffman@29537 ` 1343` ``` unfolding pdivmod_rel_def by simp_all ``` huffman@29451 ` 1344` ``` from q1 q2 have q3: "(q1 - q2) * y = r2 - r1" ``` nipkow@29667 ` 1345` ``` by (simp add: algebra_simps) ``` huffman@29451 ` 1346` ``` from r1 r2 have r3: "(r2 - r1) = 0 \ degree (r2 - r1) < degree y" ``` huffman@29453 ` 1347` ``` by (auto intro: degree_diff_less) ``` huffman@29451 ` 1348` huffman@29451 ` 1349` ``` show "q1 = q2 \ r1 = r2" ``` huffman@29451 ` 1350` ``` proof (rule ccontr) ``` huffman@29451 ` 1351` ``` assume "\ (q1 = q2 \ r1 = r2)" ``` huffman@29451 ` 1352` ``` with q3 have "q1 \ q2" and "r1 \ r2" by auto ``` huffman@29451 ` 1353` ``` with r3 have "degree (r2 - r1) < degree y" by simp ``` huffman@29451 ` 1354` ``` also have "degree y \ degree (q1 - q2) + degree y" by simp ``` huffman@29451 ` 1355` ``` also have "\ = degree ((q1 - q2) * y)" ``` wenzelm@60500 ` 1356` ``` using \q1 \ q2\ by (simp add: degree_mult_eq) ``` huffman@29451 ` 1357` ``` also have "\ = degree (r2 - r1)" ``` huffman@29451 ` 1358` ``` using q3 by simp ``` huffman@29451 ` 1359` ``` finally have "degree (r2 - r1) < degree (r2 - r1)" . ``` huffman@29451 ` 1360` ``` then show "False" by simp ``` huffman@29451 ` 1361` ``` qed ``` huffman@29451 ` 1362` ```qed ``` huffman@29451 ` 1363` huffman@29660 ` 1364` ```lemma pdivmod_rel_0_iff: "pdivmod_rel 0 y q r \ q = 0 \ r = 0" ``` huffman@29660 ` 1365` ```by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_0) ``` huffman@29660 ` 1366` huffman@29660 ` 1367` ```lemma pdivmod_rel_by_0_iff: "pdivmod_rel x 0 q r \ q = 0 \ r = x" ``` huffman@29660 ` 1368` ```by (auto dest: pdivmod_rel_unique intro: pdivmod_rel_by_0) ``` huffman@29660 ` 1369` wenzelm@45605 ` 1370` ```lemmas pdivmod_rel_unique_div = pdivmod_rel_unique [THEN conjunct1] ``` huffman@29451 ` 1371` wenzelm@45605 ` 1372` ```lemmas pdivmod_rel_unique_mod = pdivmod_rel_unique [THEN conjunct2] ``` huffman@29451 ` 1373` huffman@29451 ` 1374` ```instantiation poly :: (field) ring_div ``` huffman@29451 ` 1375` ```begin ``` huffman@29451 ` 1376` haftmann@60352 ` 1377` ```definition divide_poly where ``` haftmann@60429 ` 1378` ``` div_poly_def: "x div y = (THE q. \r. pdivmod_rel x y q r)" ``` huffman@29451 ` 1379` huffman@29451 ` 1380` ```definition mod_poly where ``` haftmann@37765 ` 1381` ``` "x mod y = (THE r. \q. pdivmod_rel x y q r)" ``` huffman@29451 ` 1382` huffman@29451 ` 1383` ```lemma div_poly_eq: ``` haftmann@60429 ` 1384` ``` "pdivmod_rel x y q r \ x div y = q" ``` huffman@29451 ` 1385` ```unfolding div_poly_def ``` huffman@29537 ` 1386` ```by (fast elim: pdivmod_rel_unique_div) ``` huffman@29451 ` 1387` huffman@29451 ` 1388` ```lemma mod_poly_eq: ``` huffman@29537 ` 1389` ``` "pdivmod_rel x y q r \ x mod y = r" ``` huffman@29451 ` 1390` ```unfolding mod_poly_def ``` huffman@29537 ` 1391` ```by (fast elim: pdivmod_rel_unique_mod) ``` huffman@29451 ` 1392` huffman@29537 ` 1393` ```lemma pdivmod_rel: ``` haftmann@60429 ` 1394` ``` "pdivmod_rel x y (x div y) (x mod y)" ``` huffman@29451 ` 1395` ```proof - ``` huffman@29537 ` 1396` ``` from pdivmod_rel_exists ``` huffman@29537 ` 1397` ``` obtain q r where "pdivmod_rel x y q r" by fast ``` huffman@29451 ` 1398` ``` thus ?thesis ``` huffman@29451 ` 1399` ``` by (simp add: div_poly_eq mod_poly_eq) ``` huffman@29451 ` 1400` ```qed ``` huffman@29451 ` 1401` wenzelm@60679 ` 1402` ```instance ``` wenzelm@60679 ` 1403` ```proof ``` huffman@29451 ` 1404` ``` fix x y :: "'a poly" ``` haftmann@60429 ` 1405` ``` show "x div y * y + x mod y = x" ``` huffman@29537 ` 1406` ``` using pdivmod_rel [of x y] ``` huffman@29537 ` 1407` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 1408` ```next ``` huffman@29451 ` 1409` ``` fix x :: "'a poly" ``` huffman@29537 ` 1410` ``` have "pdivmod_rel x 0 0 x" ``` huffman@29537 ` 1411` ``` by (rule pdivmod_rel_by_0) ``` haftmann@60429 ` 1412` ``` thus "x div 0 = 0" ``` huffman@29451 ` 1413` ``` by (rule div_poly_eq) ``` huffman@29451 ` 1414` ```next ``` huffman@29451 ` 1415` ``` fix y :: "'a poly" ``` huffman@29537 ` 1416` ``` have "pdivmod_rel 0 y 0 0" ``` huffman@29537 ` 1417` ``` by (rule pdivmod_rel_0) ``` haftmann@60429 ` 1418` ``` thus "0 div y = 0" ``` huffman@29451 ` 1419` ``` by (rule div_poly_eq) ``` huffman@29451 ` 1420` ```next ``` huffman@29451 ` 1421` ``` fix x y z :: "'a poly" ``` huffman@29451 ` 1422` ``` assume "y \ 0" ``` haftmann@60429 ` 1423` ``` hence "pdivmod_rel (x + z * y) y (z + x div y) (x mod y)" ``` huffman@29537 ` 1424` ``` using pdivmod_rel [of x y] ``` webertj@49962 ` 1425` ``` by (simp add: pdivmod_rel_def distrib_right) ``` haftmann@60429 ` 1426` ``` thus "(x + z * y) div y = z + x div y" ``` huffman@29451 ` 1427` ``` by (rule div_poly_eq) ``` haftmann@30930 ` 1428` ```next ``` haftmann@30930 ` 1429` ``` fix x y z :: "'a poly" ``` haftmann@30930 ` 1430` ``` assume "x \ 0" ``` haftmann@60429 ` 1431` ``` show "(x * y) div (x * z) = y div z" ``` haftmann@30930 ` 1432` ``` proof (cases "y \ 0 \ z \ 0") ``` haftmann@30930 ` 1433` ``` have "\x::'a poly. pdivmod_rel x 0 0 x" ``` haftmann@30930 ` 1434` ``` by (rule pdivmod_rel_by_0) ``` haftmann@60429 ` 1435` ``` then have [simp]: "\x::'a poly. x div 0 = 0" ``` haftmann@30930 ` 1436` ``` by (rule div_poly_eq) ``` haftmann@30930 ` 1437` ``` have "\x::'a poly. pdivmod_rel 0 x 0 0" ``` haftmann@30930 ` 1438` ``` by (rule pdivmod_rel_0) ``` haftmann@60429 ` 1439` ``` then have [simp]: "\x::'a poly. 0 div x = 0" ``` haftmann@30930 ` 1440` ``` by (rule div_poly_eq) ``` haftmann@30930 ` 1441` ``` case False then show ?thesis by auto ``` haftmann@30930 ` 1442` ``` next ``` haftmann@30930 ` 1443` ``` case True then have "y \ 0" and "z \ 0" by auto ``` wenzelm@60500 ` 1444` ``` with \x \ 0\ ``` haftmann@30930 ` 1445` ``` have "\q r. pdivmod_rel y z q r \ pdivmod_rel (x * y) (x * z) q (x * r)" ``` haftmann@30930 ` 1446` ``` by (auto simp add: pdivmod_rel_def algebra_simps) ``` haftmann@30930 ` 1447` ``` (rule classical, simp add: degree_mult_eq) ``` haftmann@60429 ` 1448` ``` moreover from pdivmod_rel have "pdivmod_rel y z (y div z) (y mod z)" . ``` haftmann@60429 ` 1449` ``` ultimately have "pdivmod_rel (x * y) (x * z) (y div z) (x * (y mod z))" . ``` haftmann@30930 ` 1450` ``` then show ?thesis by (simp add: div_poly_eq) ``` haftmann@30930 ` 1451` ``` qed ``` huffman@29451 ` 1452` ```qed ``` huffman@29451 ` 1453` huffman@29451 ` 1454` ```end ``` huffman@29451 ` 1455` haftmann@60570 ` 1456` ```lemma is_unit_monom_0: ``` haftmann@60570 ` 1457` ``` fixes a :: "'a::field" ``` haftmann@60570 ` 1458` ``` assumes "a \ 0" ``` haftmann@60570 ` 1459` ``` shows "is_unit (monom a 0)" ``` haftmann@60570 ` 1460` ```proof ``` haftmann@60570 ` 1461` ``` from assms show "1 = monom a 0 * monom (1 / a) 0" ``` haftmann@60570 ` 1462` ``` by (simp add: mult_monom) ``` haftmann@60570 ` 1463` ```qed ``` haftmann@60570 ` 1464` haftmann@60570 ` 1465` ```lemma is_unit_triv: ``` haftmann@60570 ` 1466` ``` fixes a :: "'a::field" ``` haftmann@60570 ` 1467` ``` assumes "a \ 0" ``` haftmann@60570 ` 1468` ``` shows "is_unit [:a:]" ``` haftmann@60570 ` 1469` ``` using assms by (simp add: is_unit_monom_0 monom_0 [symmetric]) ``` haftmann@60570 ` 1470` haftmann@60570 ` 1471` ```lemma is_unit_iff_degree: ``` haftmann@60570 ` 1472` ``` assumes "p \ 0" ``` haftmann@60570 ` 1473` ``` shows "is_unit p \ degree p = 0" (is "?P \ ?Q") ``` haftmann@60570 ` 1474` ```proof ``` haftmann@60570 ` 1475` ``` assume ?Q ``` haftmann@60570 ` 1476` ``` then obtain a where "p = [:a:]" by (rule degree_eq_zeroE) ``` haftmann@60570 ` 1477` ``` with assms show ?P by (simp add: is_unit_triv) ``` haftmann@60570 ` 1478` ```next ``` haftmann@60570 ` 1479` ``` assume ?P ``` haftmann@60570 ` 1480` ``` then obtain q where "q \ 0" "p * q = 1" .. ``` haftmann@60570 ` 1481` ``` then have "degree (p * q) = degree 1" ``` haftmann@60570 ` 1482` ``` by simp ``` haftmann@60570 ` 1483` ``` with \p \ 0\ \q \ 0\ have "degree p + degree q = 0" ``` haftmann@60570 ` 1484` ``` by (simp add: degree_mult_eq) ``` haftmann@60570 ` 1485` ``` then show ?Q by simp ``` haftmann@60570 ` 1486` ```qed ``` haftmann@60570 ` 1487` haftmann@60570 ` 1488` ```lemma is_unit_pCons_iff: ``` haftmann@60570 ` 1489` ``` "is_unit (pCons a p) \ p = 0 \ a \ 0" (is "?P \ ?Q") ``` haftmann@60570 ` 1490` ``` by (cases "p = 0") (auto simp add: is_unit_triv is_unit_iff_degree) ``` haftmann@60570 ` 1491` haftmann@60570 ` 1492` ```lemma is_unit_monom_trival: ``` haftmann@60570 ` 1493` ``` fixes p :: "'a::field poly" ``` haftmann@60570 ` 1494` ``` assumes "is_unit p" ``` haftmann@60570 ` 1495` ``` shows "monom (coeff p (degree p)) 0 = p" ``` haftmann@60570 ` 1496` ``` using assms by (cases p) (simp_all add: monom_0 is_unit_pCons_iff) ``` haftmann@60570 ` 1497` haftmann@60685 ` 1498` ```lemma is_unit_polyE: ``` haftmann@60685 ` 1499` ``` assumes "is_unit p" ``` haftmann@60685 ` 1500` ``` obtains a where "p = monom a 0" and "a \ 0" ``` haftmann@60685 ` 1501` ```proof - ``` haftmann@60685 ` 1502` ``` obtain a q where "p = pCons a q" by (cases p) ``` haftmann@60685 ` 1503` ``` with assms have "p = [:a:]" and "a \ 0" ``` haftmann@60685 ` 1504` ``` by (simp_all add: is_unit_pCons_iff) ``` haftmann@60685 ` 1505` ``` with that show thesis by (simp add: monom_0) ``` haftmann@60685 ` 1506` ```qed ``` haftmann@60685 ` 1507` haftmann@60685 ` 1508` ```instantiation poly :: (field) normalization_semidom ``` haftmann@60685 ` 1509` ```begin ``` haftmann@60685 ` 1510` haftmann@60685 ` 1511` ```definition normalize_poly :: "'a poly \ 'a poly" ``` haftmann@60685 ` 1512` ``` where "normalize_poly p = smult (1 / coeff p (degree p)) p" ``` haftmann@60685 ` 1513` haftmann@60685 ` 1514` ```definition unit_factor_poly :: "'a poly \ 'a poly" ``` haftmann@60685 ` 1515` ``` where "unit_factor_poly p = monom (coeff p (degree p)) 0" ``` haftmann@60685 ` 1516` haftmann@60685 ` 1517` ```instance ``` haftmann@60685 ` 1518` ```proof ``` haftmann@60685 ` 1519` ``` fix p :: "'a poly" ``` haftmann@60685 ` 1520` ``` show "unit_factor p * normalize p = p" ``` haftmann@60685 ` 1521` ``` by (simp add: normalize_poly_def unit_factor_poly_def) ``` haftmann@60685 ` 1522` ``` (simp only: mult_smult_left [symmetric] smult_monom, simp) ``` haftmann@60685 ` 1523` ```next ``` haftmann@60685 ` 1524` ``` show "normalize 0 = (0::'a poly)" ``` haftmann@60685 ` 1525` ``` by (simp add: normalize_poly_def) ``` haftmann@60685 ` 1526` ```next ``` haftmann@60685 ` 1527` ``` show "unit_factor 0 = (0::'a poly)" ``` haftmann@60685 ` 1528` ``` by (simp add: unit_factor_poly_def) ``` haftmann@60685 ` 1529` ```next ``` haftmann@60685 ` 1530` ``` fix p :: "'a poly" ``` haftmann@60685 ` 1531` ``` assume "is_unit p" ``` haftmann@60685 ` 1532` ``` then obtain a where "p = monom a 0" and "a \ 0" ``` haftmann@60685 ` 1533` ``` by (rule is_unit_polyE) ``` haftmann@60685 ` 1534` ``` then show "normalize p = 1" ``` haftmann@60685 ` 1535` ``` by (auto simp add: normalize_poly_def smult_monom degree_monom_eq) ``` haftmann@60685 ` 1536` ```next ``` haftmann@60685 ` 1537` ``` fix p q :: "'a poly" ``` haftmann@60685 ` 1538` ``` assume "q \ 0" ``` haftmann@60685 ` 1539` ``` from \q \ 0\ have "is_unit (monom (coeff q (degree q)) 0)" ``` haftmann@60685 ` 1540` ``` by (auto intro: is_unit_monom_0) ``` haftmann@60685 ` 1541` ``` then show "is_unit (unit_factor q)" ``` haftmann@60685 ` 1542` ``` by (simp add: unit_factor_poly_def) ``` haftmann@60685 ` 1543` ```next ``` haftmann@60685 ` 1544` ``` fix p q :: "'a poly" ``` haftmann@60685 ` 1545` ``` have "monom (coeff (p * q) (degree (p * q))) 0 = ``` haftmann@60685 ` 1546` ``` monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0" ``` haftmann@60685 ` 1547` ``` by (simp add: monom_0 coeff_degree_mult) ``` haftmann@60685 ` 1548` ``` then show "unit_factor (p * q) = ``` haftmann@60685 ` 1549` ``` unit_factor p * unit_factor q" ``` haftmann@60685 ` 1550` ``` by (simp add: unit_factor_poly_def) ``` haftmann@60685 ` 1551` ```qed ``` haftmann@60685 ` 1552` haftmann@60685 ` 1553` ```end ``` haftmann@60685 ` 1554` huffman@29451 ` 1555` ```lemma degree_mod_less: ``` huffman@29451 ` 1556` ``` "y \ 0 \ x mod y = 0 \ degree (x mod y) < degree y" ``` huffman@29537 ` 1557` ``` using pdivmod_rel [of x y] ``` huffman@29537 ` 1558` ``` unfolding pdivmod_rel_def by simp ``` huffman@29451 ` 1559` huffman@29451 ` 1560` ```lemma div_poly_less: "degree x < degree y \ x div y = 0" ``` huffman@29451 ` 1561` ```proof - ``` huffman@29451 ` 1562` ``` assume "degree x < degree y" ``` huffman@29537 ` 1563` ``` hence "pdivmod_rel x y 0 x" ``` huffman@29537 ` 1564` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 1565` ``` thus "x div y = 0" by (rule div_poly_eq) ``` huffman@29451 ` 1566` ```qed ``` huffman@29451 ` 1567` huffman@29451 ` 1568` ```lemma mod_poly_less: "degree x < degree y \ x mod y = x" ``` huffman@29451 ` 1569` ```proof - ``` huffman@29451 ` 1570` ``` assume "degree x < degree y" ``` huffman@29537 ` 1571` ``` hence "pdivmod_rel x y 0 x" ``` huffman@29537 ` 1572` ``` by (simp add: pdivmod_rel_def) ``` huffman@29451 ` 1573` ``` thus "x mod y = x" by (rule mod_poly_eq) ``` huffman@29451 ` 1574` ```qed ``` huffman@29451 ` 1575` huffman@29659 ` 1576` ```lemma pdivmod_rel_smult_left: ``` huffman@29659 ` 1577` ``` "pdivmod_rel x y q r ``` huffman@29659 ` 1578` ``` \ pdivmod_rel (smult a x) y (smult a q) (smult a r)" ``` huffman@29659 ` 1579` ``` unfolding pdivmod_rel_def by (simp add: smult_add_right) ``` huffman@29659 ` 1580` huffman@29659 ` 1581` ```lemma div_smult_left: "(smult a x) div y = smult a (x div y)" ``` huffman@29659 ` 1582` ``` by (rule div_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) ``` huffman@29659 ` 1583` huffman@29659 ` 1584` ```lemma mod_smult_left: "(smult a x) mod y = smult a (x mod y)" ``` huffman@29659 ` 1585` ``` by (rule mod_poly_eq, rule pdivmod_rel_smult_left, rule pdivmod_rel) ``` huffman@29659 ` 1586` huffman@30072 ` 1587` ```lemma poly_div_minus_left [simp]: ``` huffman@30072 ` 1588` ``` fixes x y :: "'a::field poly" ``` huffman@30072 ` 1589` ``` shows "(- x) div y = - (x div y)" ``` haftmann@54489 ` 1590` ``` using div_smult_left [of "- 1::'a"] by simp ``` huffman@30072 ` 1591` huffman@30072 ` 1592` ```lemma poly_mod_minus_left [simp]: ``` huffman@30072 ` 1593` ``` fixes x y :: "'a::field poly" ``` huffman@30072 ` 1594` ``` shows "(- x) mod y = - (x mod y)" ``` haftmann@54489 ` 1595` ``` using mod_smult_left [of "- 1::'a"] by simp ``` huffman@30072 ` 1596` huffman@57482 ` 1597` ```lemma pdivmod_rel_add_left: ``` huffman@57482 ` 1598` ``` assumes "pdivmod_rel x y q r" ``` huffman@57482 ` 1599` ``` assumes "pdivmod_rel x' y q' r'" ``` huffman@57482 ` 1600` ``` shows "pdivmod_rel (x + x') y (q + q') (r + r')" ``` huffman@57482 ` 1601` ``` using assms unfolding pdivmod_rel_def ``` haftmann@59557 ` 1602` ``` by (auto simp add: algebra_simps degree_add_less) ``` huffman@57482 ` 1603` huffman@57482 ` 1604` ```lemma poly_div_add_left: ``` huffman@57482 ` 1605` ``` fixes x y z :: "'a::field poly" ``` huffman@57482 ` 1606` ``` shows "(x + y) div z = x div z + y div z" ``` huffman@57482 ` 1607` ``` using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] ``` huffman@57482 ` 1608` ``` by (rule div_poly_eq) ``` huffman@57482 ` 1609` huffman@57482 ` 1610` ```lemma poly_mod_add_left: ``` huffman@57482 ` 1611` ``` fixes x y z :: "'a::field poly" ``` huffman@57482 ` 1612` ``` shows "(x + y) mod z = x mod z + y mod z" ``` huffman@57482 ` 1613` ``` using pdivmod_rel_add_left [OF pdivmod_rel pdivmod_rel] ``` huffman@57482 ` 1614` ``` by (rule mod_poly_eq) ``` huffman@57482 ` 1615` huffman@57482 ` 1616` ```lemma poly_div_diff_left: ``` huffman@57482 ` 1617` ``` fixes x y z :: "'a::field poly" ``` huffman@57482 ` 1618` ``` shows "(x - y) div z = x div z - y div z" ``` huffman@57482 ` 1619` ``` by (simp only: diff_conv_add_uminus poly_div_add_left poly_div_minus_left) ``` huffman@57482 ` 1620` huffman@57482 ` 1621` ```lemma poly_mod_diff_left: ``` huffman@57482 ` 1622` ``` fixes x y z :: "'a::field poly" ``` huffman@57482 ` 1623` ``` shows "(x - y) mod z = x mod z - y mod z" ``` huffman@57482 ` 1624` ``` by (simp only: diff_conv_add_uminus poly_mod_add_left poly_mod_minus_left) ``` huffman@57482 ` 1625` huffman@29659 ` 1626` ```lemma pdivmod_rel_smult_right: ``` huffman@29659 ` 1627` ``` "\a \ 0; pdivmod_rel x y q r\ ``` huffman@29659 ` 1628` ``` \ pdivmod_rel x (smult a y) (smult (inverse a) q) r" ``` huffman@29659 ` 1629` ``` unfolding pdivmod_rel_def by simp ``` huffman@29659 ` 1630` huffman@29659 ` 1631` ```lemma div_smult_right: ``` huffman@29659 ` 1632` ``` "a \ 0 \ x div (smult a y) = smult (inverse a) (x div y)" ``` huffman@29659 ` 1633` ``` by (rule div_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) ``` huffman@29659 ` 1634` huffman@29659 ` 1635` ```lemma mod_smult_right: "a \ 0 \ x mod (smult a y) = x mod y" ``` huffman@29659 ` 1636` ``` by (rule mod_poly_eq, erule pdivmod_rel_smult_right, rule pdivmod_rel) ``` huffman@29659 ` 1637` huffman@30072 ` 1638` ```lemma poly_div_minus_right [simp]: ``` huffman@30072 ` 1639` ``` fixes x y :: "'a::field poly" ``` huffman@30072 ` 1640` ``` shows "x div (- y) = - (x div y)" ``` haftmann@54489 ` 1641` ``` using div_smult_right [of "- 1::'a"] by (simp add: nonzero_inverse_minus_eq) ``` huffman@30072 ` 1642` huffman@30072 ` 1643` ```lemma poly_mod_minus_right [simp]: ``` huffman@30072 ` 1644` ``` fixes x y :: "'a::field poly" ``` huffman@30072 ` 1645` ``` shows "x mod (- y) = x mod y" ``` haftmann@54489 ` 1646` ``` using mod_smult_right [of "- 1::'a"] by simp ``` huffman@30072 ` 1647` huffman@29660 ` 1648` ```lemma pdivmod_rel_mult: ``` huffman@29660 ` 1649` ``` "\pdivmod_rel x y q r; pdivmod_rel q z q' r'\ ``` huffman@29660 ` 1650` ``` \ pdivmod_rel x (y * z) q' (y * r' + r)" ``` huffman@29660 ` 1651` ```apply (cases "z = 0", simp add: pdivmod_rel_def) ``` huffman@29660 ` 1652` ```apply (cases "y = 0", simp add: pdivmod_rel_by_0_iff pdivmod_rel_0_iff) ``` huffman@29660 ` 1653` ```apply (cases "r = 0") ``` huffman@29660 ` 1654` ```apply (cases "r' = 0") ``` huffman@29660 ` 1655` ```apply (simp add: pdivmod_rel_def) ``` haftmann@36350 ` 1656` ```apply (simp add: pdivmod_rel_def field_simps degree_mult_eq) ``` huffman@29660 ` 1657` ```apply (cases "r' = 0") ``` huffman@29660 ` 1658` ```apply (simp add: pdivmod_rel_def degree_mult_eq) ``` haftmann@36350 ` 1659` ```apply (simp add: pdivmod_rel_def field_simps) ``` huffman@29660 ` 1660` ```apply (simp add: degree_mult_eq degree_add_less) ``` huffman@29660 ` 1661` ```done ``` huffman@29660 ` 1662` huffman@29660 ` 1663` ```lemma poly_div_mult_right: ``` huffman@29660 ` 1664` ``` fixes x y z :: "'a::field poly" ``` huffman@29660 ` 1665` ``` shows "x div (y * z) = (x div y) div z" ``` huffman@29660 ` 1666` ``` by (rule div_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) ``` huffman@29660 ` 1667` huffman@29660 ` 1668` ```lemma poly_mod_mult_right: ``` huffman@29660 ` 1669` ``` fixes x y z :: "'a::field poly" ``` huffman@29660 ` 1670` ``` shows "x mod (y * z) = y * (x div y mod z) + x mod y" ``` huffman@29660 ` 1671` ``` by (rule mod_poly_eq, rule pdivmod_rel_mult, (rule pdivmod_rel)+) ``` huffman@29660 ` 1672` huffman@29451 ` 1673` ```lemma mod_pCons: ``` huffman@29451 ` 1674` ``` fixes a and x ``` huffman@29451 ` 1675` ``` assumes y: "y \ 0" ``` huffman@29451 ` 1676` ``` defines b: "b \ coeff (pCons a (x mod y)) (degree y) / coeff y (degree y)" ``` huffman@29451 ` 1677` ``` shows "(pCons a x) mod y = (pCons a (x mod y) - smult b y)" ``` huffman@29451 ` 1678` ```unfolding b ``` huffman@29451 ` 1679` ```apply (rule mod_poly_eq) ``` huffman@29537 ` 1680` ```apply (rule pdivmod_rel_pCons [OF pdivmod_rel y refl]) ``` huffman@29451 ` 1681` ```done ``` huffman@29451 ` 1682` haftmann@52380 ` 1683` ```definition pdivmod :: "'a::field poly \ 'a poly \ 'a poly \ 'a poly" ``` haftmann@52380 ` 1684` ```where ``` haftmann@52380 ` 1685` ``` "pdivmod p q = (p div q, p mod q)" ``` huffman@31663 ` 1686` haftmann@52380 ` 1687` ```lemma div_poly_code [code]: ``` haftmann@52380 ` 1688` ``` "p div q = fst (pdivmod p q)" ``` haftmann@52380 ` 1689` ``` by (simp add: pdivmod_def) ``` huffman@31663 ` 1690` haftmann@52380 ` 1691` ```lemma mod_poly_code [code]: ``` haftmann@52380 ` 1692` ``` "p mod q = snd (pdivmod p q)" ``` haftmann@52380 ` 1693` ``` by (simp add: pdivmod_def) ``` huffman@31663 ` 1694` haftmann@52380 ` 1695` ```lemma pdivmod_0: ``` haftmann@52380 ` 1696` ``` "pdivmod 0 q = (0, 0)" ``` haftmann@52380 ` 1697` ``` by (simp add: pdivmod_def) ``` huffman@31663 ` 1698` haftmann@52380 ` 1699` ```lemma pdivmod_pCons: ``` haftmann@52380 ` 1700` ``` "pdivmod (pCons a p) q = ``` haftmann@52380 ` 1701` ``` (if q = 0 then (0, pCons a p) else ``` haftmann@52380 ` 1702` ``` (let (s, r) = pdivmod p q; ``` haftmann@52380 ` 1703` ``` b = coeff (pCons a r) (degree q) / coeff q (degree q) ``` haftmann@52380 ` 1704` ``` in (pCons b s, pCons a r - smult b q)))" ``` haftmann@52380 ` 1705` ``` apply (simp add: pdivmod_def Let_def, safe) ``` haftmann@52380 ` 1706` ``` apply (rule div_poly_eq) ``` haftmann@52380 ` 1707` ``` apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) ``` haftmann@52380 ` 1708` ``` apply (rule mod_poly_eq) ``` haftmann@52380 ` 1709` ``` apply (erule pdivmod_rel_pCons [OF pdivmod_rel _ refl]) ``` huffman@29451 ` 1710` ``` done ``` huffman@29451 ` 1711` haftmann@52380 ` 1712` ```lemma pdivmod_fold_coeffs [code]: ``` haftmann@52380 ` 1713` ``` "pdivmod p q = (if q = 0 then (0, p) ``` haftmann@52380 ` 1714` ``` else fold_coeffs (\a (s, r). ``` haftmann@52380 ` 1715` ``` let b = coeff (pCons a r) (degree q) / coeff q (degree q) ``` haftmann@52380 ` 1716` ``` in (pCons b s, pCons a r - smult b q) ``` haftmann@52380 ` 1717` ``` ) p (0, 0))" ``` haftmann@52380 ` 1718` ``` apply (cases "q = 0") ``` haftmann@52380 ` 1719` ``` apply (simp add: pdivmod_def) ``` haftmann@52380 ` 1720` ``` apply (rule sym) ``` haftmann@52380 ` 1721` ``` apply (induct p) ``` haftmann@52380 ` 1722` ``` apply (simp_all add: pdivmod_0 pdivmod_pCons) ``` haftmann@52380 ` 1723` ``` apply (case_tac "a = 0 \ p = 0") ``` haftmann@52380 ` 1724` ``` apply (auto simp add: pdivmod_def) ``` haftmann@52380 ` 1725` ``` done ``` huffman@29980 ` 1726` huffman@29980 ` 1727` wenzelm@60500 ` 1728` ```subsection \Order of polynomial roots\ ``` huffman@29977 ` 1729` haftmann@52380 ` 1730` ```definition order :: "'a::idom \ 'a poly \ nat" ``` huffman@29977 ` 1731` ```where ``` huffman@29977 ` 1732` ``` "order a p = (LEAST n. \ [:-a, 1:] ^ Suc n dvd p)" ``` huffman@29977 ` 1733` huffman@29977 ` 1734` ```lemma coeff_linear_power: ``` huffman@29979 ` 1735` ``` fixes a :: "'a::comm_semiring_1" ``` huffman@29977 ` 1736` ``` shows "coeff ([:a, 1:] ^ n) n = 1" ``` huffman@29977 ` 1737` ```apply (induct n, simp_all) ``` huffman@29977 ` 1738` ```apply (subst coeff_eq_0) ``` huffman@29977 ` 1739` ```apply (auto intro: le_less_trans degree_power_le) ``` huffman@29977 ` 1740` ```done ``` huffman@29977 ` 1741` huffman@29977 ` 1742` ```lemma degree_linear_power: ``` huffman@29979 ` 1743` ``` fixes a :: "'a::comm_semiring_1" ``` huffman@29977 ` 1744` ``` shows "degree ([:a, 1:] ^ n) = n" ``` huffman@29977 ` 1745` ```apply (rule order_antisym) ``` huffman@29977 ` 1746` ```apply (rule ord_le_eq_trans [OF degree_power_le], simp) ``` huffman@29977 ` 1747` ```apply (rule le_degree, simp add: coeff_linear_power) ``` huffman@29977 ` 1748` ```done ``` huffman@29977 ` 1749` huffman@29977 ` 1750` ```lemma order_1: "[:-a, 1:] ^ order a p dvd p" ``` huffman@29977 ` 1751` ```apply (cases "p = 0", simp) ``` huffman@29977 ` 1752` ```apply (cases "order a p", simp) ``` huffman@29977 ` 1753` ```apply (subgoal_tac "nat < (LEAST n. \ [:-a, 1:] ^ Suc n dvd p)") ``` huffman@29977 ` 1754` ```apply (drule not_less_Least, simp) ``` huffman@29977 ` 1755` ```apply (fold order_def, simp) ``` huffman@29977 ` 1756` ```done ``` huffman@29977 ` 1757` huffman@29977 ` 1758` ```lemma order_2: "p \ 0 \ \ [:-a, 1:] ^ Suc (order a p) dvd p" ``` huffman@29977 ` 1759` ```unfolding order_def ``` huffman@29977 ` 1760` ```apply (rule LeastI_ex) ``` huffman@29977 ` 1761` ```apply (rule_tac x="degree p" in exI) ``` huffman@29977 ` 1762` ```apply (rule notI) ``` huffman@29977 ` 1763` ```apply (drule (1) dvd_imp_degree_le) ``` huffman@29977 ` 1764` ```apply (simp only: degree_linear_power) ``` huffman@29977 ` 1765` ```done ``` huffman@29977 ` 1766` huffman@29977 ` 1767` ```lemma order: ``` huffman@29977 ` 1768` ``` "p \ 0 \ [:-a, 1:] ^ order a p dvd p \ \ [:-a, 1:] ^ Suc (order a p) dvd p" ``` huffman@29977 ` 1769` ```by (rule conjI [OF order_1 order_2]) ``` huffman@29977 ` 1770` huffman@29977 ` 1771` ```lemma order_degree: ``` huffman@29977 ` 1772` ``` assumes p: "p \ 0" ``` huffman@29977 ` 1773` ``` shows "order a p \ degree p" ``` huffman@29977 ` 1774` ```proof - ``` huffman@29977 ` 1775` ``` have "order a p = degree ([:-a, 1:] ^ order a p)" ``` huffman@29977 ` 1776` ``` by (simp only: degree_linear_power) ``` huffman@29977 ` 1777` ``` also have "\ \ degree p" ``` huffman@29977 ` 1778` ``` using order_1 p by (rule dvd_imp_degree_le) ``` huffman@29977 ` 1779` ``` finally show ?thesis . ``` huffman@29977 ` 1780` ```qed ``` huffman@29977 ` 1781` huffman@29977 ` 1782` ```lemma order_root: "poly p a = 0 \ p = 0 \ order a p \ 0" ``` huffman@29977 ` 1783` ```apply (cases "p = 0", simp_all) ``` huffman@29977 ` 1784` ```apply (rule iffI) ``` lp15@56383 ` 1785` ```apply (metis order_2 not_gr0 poly_eq_0_iff_dvd power_0 power_Suc_0 power_one_right) ``` lp15@56383 ` 1786` ```unfolding poly_eq_0_iff_dvd ``` lp15@56383 ` 1787` ```apply (metis dvd_power dvd_trans order_1) ``` huffman@29977 ` 1788` ```done ``` huffman@29977 ` 1789` huffman@29977 ` 1790` wenzelm@60500 ` 1791` ```subsection \GCD of polynomials\ ``` huffman@29478 ` 1792` haftmann@52380 ` 1793` ```instantiation poly :: (field) gcd ``` huffman@29478 ` 1794` ```begin ``` huffman@29478 ` 1795` haftmann@52380 ` 1796` ```function gcd_poly :: "'a::field poly \ 'a poly \ 'a poly" ``` haftmann@52380 ` 1797` ```where ``` haftmann@52380 ` 1798` ``` "gcd (x::'a poly) 0 = smult (inverse (coeff x (degree x))) x" ``` haftmann@52380 ` 1799` ```| "y \ 0 \ gcd (x::'a poly) y = gcd y (x mod y)" ``` haftmann@52380 ` 1800` ```by auto ``` huffman@29478 ` 1801` haftmann@52380 ` 1802` ```termination "gcd :: _ poly \ _" ``` haftmann@52380 ` 1803` ```by (relation "measure (\(x, y). if y = 0 then 0 else Suc (degree y))") ``` haftmann@52380 ` 1804` ``` (auto dest: degree_mod_less) ``` haftmann@52380 ` 1805` haftmann@52380 ` 1806` ```declare gcd_poly.simps [simp del] ``` haftmann@52380 ` 1807` haftmann@58513 ` 1808` ```definition lcm_poly :: "'a::field poly \ 'a poly \ 'a poly" ``` haftmann@58513 ` 1809` ```where ``` haftmann@58513 ` 1810` ``` "lcm_poly a b = a * b div smult (coeff a (degree a) * coeff b (degree b)) (gcd a b)" ``` haftmann@58513 ` 1811` haftmann@52380 ` 1812` ```instance .. ``` huffman@29478 ` 1813` huffman@29451 ` 1814` ```end ``` huffman@29478 ` 1815` haftmann@52380 ` 1816` ```lemma ``` haftmann@52380 ` 1817` ``` fixes x y :: "_ poly" ``` haftmann@52380 ` 1818` ``` shows poly_gcd_dvd1 [iff]: "gcd x y dvd x" ``` haftmann@52380 ` 1819` ``` and poly_gcd_dvd2 [iff]: "gcd x y dvd y" ``` haftmann@52380 ` 1820` ``` apply (induct x y rule: gcd_poly.induct) ``` haftmann@52380 ` 1821` ``` apply (simp_all add: gcd_poly.simps) ``` haftmann@52380 ` 1822` ``` apply (fastforce simp add: smult_dvd_iff dest: inverse_zero_imp_zero) ``` haftmann@52380 ` 1823` ``` apply (blast dest: dvd_mod_imp_dvd) ``` haftmann@52380 ` 1824` ``` done ``` haftmann@38857 ` 1825` haftmann@52380 ` 1826` ```lemma poly_gcd_greatest: ``` haftmann@52380 ` 1827` ``` fixes k x y :: "_ poly" ``` haftmann@52380 ` 1828` ``` shows "k dvd x \ k dvd y \ k dvd gcd x y" ``` haftmann@52380 ` 1829` ``` by (induct x y rule: gcd_poly.induct) ``` haftmann@52380 ` 1830` ``` (simp_all add: gcd_poly.simps dvd_mod dvd_smult) ``` huffman@29478 ` 1831` haftmann@52380 ` 1832` ```lemma dvd_poly_gcd_iff [iff]: ``` haftmann@52380 ` 1833` ``` fixes k x y :: "_ poly" ``` haftmann@52380 ` 1834` ``` shows "k dvd gcd x y \ k dvd x \ k dvd y" ``` haftmann@60686 ` 1835` ``` by (auto intro!: poly_gcd_greatest intro: dvd_trans [of _ "gcd x y"]) ``` huffman@29478 ` 1836` haftmann@52380 ` 1837` ```lemma poly_gcd_monic: ``` haftmann@52380 ` 1838` ``` fixes x y :: "_ poly" ``` haftmann@52380 ` 1839` ``` shows "coeff (gcd x y) (degree (gcd x y)) = ``` haftmann@52380 ` 1840` ``` (if x = 0 \ y = 0 then 0 else 1)" ``` haftmann@52380 ` 1841` ``` by (induct x y rule: gcd_poly.induct) ``` haftmann@52380 ` 1842` ``` (simp_all add: gcd_poly.simps nonzero_imp_inverse_nonzero) ``` huffman@29478 ` 1843` haftmann@52380 ` 1844` ```lemma poly_gcd_zero_iff [simp]: ``` haftmann@52380 ` 1845` ``` fixes x y :: "_ poly" ``` haftmann@52380 ` 1846` ``` shows "gcd x y = 0 \ x = 0 \ y = 0" ``` haftmann@52380 ` 1847` ``` by (simp only: dvd_0_left_iff [symmetric] dvd_poly_gcd_iff) ``` huffman@29478 ` 1848` haftmann@52380 ` 1849` ```lemma poly_gcd_0_0 [simp]: ``` haftmann@52380 ` 1850` ``` "gcd (0::_ poly) 0 = 0" ``` haftmann@52380 ` 1851` ``` by simp ``` huffman@29478 ` 1852` haftmann@52380 ` 1853` ```lemma poly_dvd_antisym: ``` haftmann@52380 ` 1854` ``` fixes p q :: "'a::idom poly" ``` haftmann@52380 ` 1855` ``` assumes coeff: "coeff p (degree p) = coeff q (degree q)" ``` haftmann@52380 ` 1856` ``` assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q" ``` haftmann@52380 ` 1857` ```proof (cases "p = 0") ``` haftmann@52380 ` 1858` ``` case True with coeff show "p = q" by simp ``` haftmann@52380 ` 1859` ```next ``` haftmann@52380 ` 1860` ``` case False with coeff have "q \ 0" by auto ``` haftmann@52380 ` 1861` ``` have degree: "degree p = degree q" ``` wenzelm@60500 ` 1862` ``` using \p dvd q\ \q dvd p\ \p \ 0\ \q \ 0\ ``` haftmann@52380 ` 1863` ``` by (intro order_antisym dvd_imp_degree_le) ``` huffman@29478 ` 1864` wenzelm@60500 ` 1865` ``` from \p dvd q\ obtain a where a: "q = p * a" .. ``` wenzelm@60500 ` 1866` ``` with \q \ 0\ have "a \ 0" by auto ``` wenzelm@60500 ` 1867` ``` with degree a \p \ 0\ have "degree a = 0" ``` haftmann@52380 ` 1868` ``` by (simp add: degree_mult_eq) ``` haftmann@52380 ` 1869` ``` with coeff a show "p = q" ``` haftmann@52380 ` 1870` ``` by (cases a, auto split: if_splits) ``` haftmann@52380 ` 1871` ```qed ``` huffman@29478 ` 1872` haftmann@52380 ` 1873` ```lemma poly_gcd_unique: ``` haftmann@52380 ` 1874` ``` fixes d x y :: "_ poly" ``` haftmann@52380 ` 1875` ``` assumes dvd1: "d dvd x" and dvd2: "d dvd y" ``` haftmann@52380 ` 1876` ``` and greatest: "\k. k dvd x \ k dvd y \ k dvd d" ``` haftmann@52380 ` 1877` ``` and monic: "coeff d (degree d) = (if x = 0 \ y = 0 then 0 else 1)" ``` haftmann@52380 ` 1878` ``` shows "gcd x y = d" ``` haftmann@52380 ` 1879` ```proof - ``` haftmann@52380 ` 1880` ``` have "coeff (gcd x y) (degree (gcd x y)) = coeff d (degree d)" ``` haftmann@52380 ` 1881` ``` by (simp_all add: poly_gcd_monic monic) ``` haftmann@52380 ` 1882` ``` moreover have "gcd x y dvd d" ``` haftmann@52380 ` 1883` ``` using poly_gcd_dvd1 poly_gcd_dvd2 by (rule greatest) ``` haftmann@52380 ` 1884` ``` moreover have "d dvd gcd x y" ``` haftmann@52380 ` 1885` ``` using dvd1 dvd2 by (rule poly_gcd_greatest) ``` haftmann@52380 ` 1886` ``` ultimately show ?thesis ``` haftmann@52380 ` 1887` ``` by (rule poly_dvd_antisym) ``` haftmann@52380 ` 1888` ```qed ``` huffman@29478 ` 1889` haftmann@52380 ` 1890` ```interpretation gcd_poly!: abel_semigroup "gcd :: _ poly \ _" ``` haftmann@52380 ` 1891` ```proof ``` haftmann@52380 ` 1892` ``` fix x y z :: "'a poly" ``` haftmann@52380 ` 1893` ``` show "gcd (gcd x y) z = gcd x (gcd y z)" ``` haftmann@52380 ` 1894` ``` by (rule poly_gcd_unique) (auto intro: dvd_trans simp add: poly_gcd_monic) ``` haftmann@52380 ` 1895` ``` show "gcd x y = gcd y x" ``` haftmann@52380 ` 1896` ``` by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) ``` haftmann@52380 ` 1897` ```qed ``` huffman@29478 ` 1898` haftmann@52380 ` 1899` ```lemmas poly_gcd_assoc = gcd_poly.assoc ``` haftmann@52380 ` 1900` ```lemmas poly_gcd_commute = gcd_poly.commute ``` haftmann@52380 ` 1901` ```lemmas poly_gcd_left_commute = gcd_poly.left_commute ``` huffman@29478 ` 1902` haftmann@52380 ` 1903` ```lemmas poly_gcd_ac = poly_gcd_assoc poly_gcd_commute poly_gcd_left_commute ``` haftmann@52380 ` 1904` haftmann@52380 ` 1905` ```lemma poly_gcd_1_left [simp]: "gcd 1 y = (1 :: _ poly)" ``` haftmann@52380 ` 1906` ```by (rule poly_gcd_unique) simp_all ``` huffman@29478 ` 1907` haftmann@52380 ` 1908` ```lemma poly_gcd_1_right [simp]: "gcd x 1 = (1 :: _ poly)" ``` haftmann@52380 ` 1909` ```by (rule poly_gcd_unique) simp_all ``` haftmann@52380 ` 1910` haftmann@52380 ` 1911` ```lemma poly_gcd_minus_left [simp]: "gcd (- x) y = gcd x (y :: _ poly)" ``` haftmann@52380 ` 1912` ```by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) ``` huffman@29478 ` 1913` haftmann@52380 ` 1914` ```lemma poly_gcd_minus_right [simp]: "gcd x (- y) = gcd x (y :: _ poly)" ``` haftmann@52380 ` 1915` ```by (rule poly_gcd_unique) (simp_all add: poly_gcd_monic) ``` huffman@29478 ` 1916` haftmann@52380 ` 1917` ```lemma poly_gcd_code [code]: ``` haftmann@52380 ` 1918` ``` "gcd x y = (if y = 0 then smult (inverse (coeff x (degree x))) x else gcd y (x mod (y :: _ poly)))" ``` haftmann@52380 ` 1919` ``` by (simp add: gcd_poly.simps) ``` haftmann@52380 ` 1920` haftmann@52380 ` 1921` wenzelm@60500 ` 1922` ```subsection \Composition of polynomials\ ``` huffman@29478 ` 1923` haftmann@52380 ` 1924` ```definition pcompose :: "'a::comm_semiring_0 poly \ 'a poly \ 'a poly" ``` haftmann@52380 ` 1925` ```where ``` haftmann@52380 ` 1926` ``` "pcompose p q = fold_coeffs (\a c. [:a:] + q * c) p 0" ``` haftmann@52380 ` 1927` haftmann@52380 ` 1928` ```lemma pcompose_0 [simp]: ``` haftmann@52380 ` 1929` ``` "pcompose 0 q = 0" ``` haftmann@52380 ` 1930` ``` by (simp add: pcompose_def) ``` haftmann@52380 ` 1931` haftmann@52380 ` 1932` ```lemma pcompose_pCons: ``` haftmann@52380 ` 1933` ``` "pcompose (pCons a p) q = [:a:] + q * pcompose p q" ``` haftmann@52380 ` 1934` ``` by (cases "p = 0 \ a = 0") (auto simp add: pcompose_def) ``` haftmann@52380 ` 1935` haftmann@52380 ` 1936` ```lemma poly_pcompose: ``` haftmann@52380 ` 1937` ``` "poly (pcompose p q) x = poly p (poly q x)" ``` haftmann@52380 ` 1938` ``` by (induct p) (simp_all add: pcompose_pCons) ``` haftmann@52380 ` 1939` haftmann@52380 ` 1940` ```lemma degree_pcompose_le: ``` haftmann@52380 ` 1941` ``` "degree (pcompose p q) \ degree p * degree q" ``` haftmann@52380 ` 1942` ```apply (induct p, simp) ``` haftmann@52380 ` 1943` ```apply (simp add: pcompose_pCons, clarify) ``` haftmann@52380 ` 1944` ```apply (rule degree_add_le, simp) ``` haftmann@52380 ` 1945` ```apply (rule order_trans [OF degree_mult_le], simp) ``` huffman@29478 ` 1946` ```done ``` huffman@29478 ` 1947` haftmann@52380 ` 1948` haftmann@52380 ` 1949` ```no_notation cCons (infixr "##" 65) ``` huffman@31663 ` 1950` huffman@29478 ` 1951` ```end ```