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