src/HOL/Computational_Algebra/Polynomial_Factorial.thy
 author haftmann Sun Oct 08 22:28:22 2017 +0200 (19 months ago) changeset 66817 0b12755ccbb2 parent 66808 1907167b6038 child 66838 17989f6bc7b2 permissions -rw-r--r--
euclidean rings need no normalization
```     1 (*  Title:      HOL/Computational_Algebra/Polynomial_Factorial.thy
```
```     2     Author:     Manuel Eberl
```
```     3 *)
```
```     4
```
```     5 section \<open>Polynomials, fractions and rings\<close>
```
```     6
```
```     7 theory Polynomial_Factorial
```
```     8 imports
```
```     9   Complex_Main
```
```    10   Polynomial
```
```    11   Normalized_Fraction
```
```    12 begin
```
```    13
```
```    14 subsection \<open>Lifting elements into the field of fractions\<close>
```
```    15
```
```    16 definition to_fract :: "'a :: idom \<Rightarrow> 'a fract"
```
```    17   where "to_fract x = Fract x 1"
```
```    18   \<comment> \<open>FIXME: more idiomatic name, abbreviation\<close>
```
```    19
```
```    20 lemma to_fract_0 [simp]: "to_fract 0 = 0"
```
```    21   by (simp add: to_fract_def eq_fract Zero_fract_def)
```
```    22
```
```    23 lemma to_fract_1 [simp]: "to_fract 1 = 1"
```
```    24   by (simp add: to_fract_def eq_fract One_fract_def)
```
```    25
```
```    26 lemma to_fract_add [simp]: "to_fract (x + y) = to_fract x + to_fract y"
```
```    27   by (simp add: to_fract_def)
```
```    28
```
```    29 lemma to_fract_diff [simp]: "to_fract (x - y) = to_fract x - to_fract y"
```
```    30   by (simp add: to_fract_def)
```
```    31
```
```    32 lemma to_fract_uminus [simp]: "to_fract (-x) = -to_fract x"
```
```    33   by (simp add: to_fract_def)
```
```    34
```
```    35 lemma to_fract_mult [simp]: "to_fract (x * y) = to_fract x * to_fract y"
```
```    36   by (simp add: to_fract_def)
```
```    37
```
```    38 lemma to_fract_eq_iff [simp]: "to_fract x = to_fract y \<longleftrightarrow> x = y"
```
```    39   by (simp add: to_fract_def eq_fract)
```
```    40
```
```    41 lemma to_fract_eq_0_iff [simp]: "to_fract x = 0 \<longleftrightarrow> x = 0"
```
```    42   by (simp add: to_fract_def Zero_fract_def eq_fract)
```
```    43
```
```    44 lemma snd_quot_of_fract_nonzero [simp]: "snd (quot_of_fract x) \<noteq> 0"
```
```    45   by transfer simp
```
```    46
```
```    47 lemma Fract_quot_of_fract [simp]: "Fract (fst (quot_of_fract x)) (snd (quot_of_fract x)) = x"
```
```    48   by transfer (simp del: fractrel_iff, subst fractrel_normalize_quot_left, simp)
```
```    49
```
```    50 lemma to_fract_quot_of_fract:
```
```    51   assumes "snd (quot_of_fract x) = 1"
```
```    52   shows   "to_fract (fst (quot_of_fract x)) = x"
```
```    53 proof -
```
```    54   have "x = Fract (fst (quot_of_fract x)) (snd (quot_of_fract x))" by simp
```
```    55   also note assms
```
```    56   finally show ?thesis by (simp add: to_fract_def)
```
```    57 qed
```
```    58
```
```    59 lemma snd_quot_of_fract_Fract_whole:
```
```    60   assumes "y dvd x"
```
```    61   shows   "snd (quot_of_fract (Fract x y)) = 1"
```
```    62   using assms by transfer (auto simp: normalize_quot_def Let_def gcd_proj2_if_dvd)
```
```    63
```
```    64 lemma Fract_conv_to_fract: "Fract a b = to_fract a / to_fract b"
```
```    65   by (simp add: to_fract_def)
```
```    66
```
```    67 lemma quot_of_fract_to_fract [simp]: "quot_of_fract (to_fract x) = (x, 1)"
```
```    68   unfolding to_fract_def by transfer (simp add: normalize_quot_def)
```
```    69
```
```    70 lemma fst_quot_of_fract_eq_0_iff [simp]: "fst (quot_of_fract x) = 0 \<longleftrightarrow> x = 0"
```
```    71   by transfer simp
```
```    72
```
```    73 lemma snd_quot_of_fract_to_fract [simp]: "snd (quot_of_fract (to_fract x)) = 1"
```
```    74   unfolding to_fract_def by (rule snd_quot_of_fract_Fract_whole) simp_all
```
```    75
```
```    76 lemma coprime_quot_of_fract:
```
```    77   "coprime (fst (quot_of_fract x)) (snd (quot_of_fract x))"
```
```    78   by transfer (simp add: coprime_normalize_quot)
```
```    79
```
```    80 lemma unit_factor_snd_quot_of_fract: "unit_factor (snd (quot_of_fract x)) = 1"
```
```    81   using quot_of_fract_in_normalized_fracts[of x]
```
```    82   by (simp add: normalized_fracts_def case_prod_unfold)
```
```    83
```
```    84 lemma unit_factor_1_imp_normalized: "unit_factor x = 1 \<Longrightarrow> normalize x = x"
```
```    85   by (subst (2) normalize_mult_unit_factor [symmetric, of x])
```
```    86      (simp del: normalize_mult_unit_factor)
```
```    87
```
```    88 lemma normalize_snd_quot_of_fract: "normalize (snd (quot_of_fract x)) = snd (quot_of_fract x)"
```
```    89   by (intro unit_factor_1_imp_normalized unit_factor_snd_quot_of_fract)
```
```    90
```
```    91
```
```    92 subsection \<open>Lifting polynomial coefficients to the field of fractions\<close>
```
```    93
```
```    94 abbreviation (input) fract_poly
```
```    95   where "fract_poly \<equiv> map_poly to_fract"
```
```    96
```
```    97 abbreviation (input) unfract_poly
```
```    98   where "unfract_poly \<equiv> map_poly (fst \<circ> quot_of_fract)"
```
```    99
```
```   100 lemma fract_poly_smult [simp]: "fract_poly (smult c p) = smult (to_fract c) (fract_poly p)"
```
```   101   by (simp add: smult_conv_map_poly map_poly_map_poly o_def)
```
```   102
```
```   103 lemma fract_poly_0 [simp]: "fract_poly 0 = 0"
```
```   104   by (simp add: poly_eqI coeff_map_poly)
```
```   105
```
```   106 lemma fract_poly_1 [simp]: "fract_poly 1 = 1"
```
```   107   by (simp add: map_poly_pCons)
```
```   108
```
```   109 lemma fract_poly_add [simp]:
```
```   110   "fract_poly (p + q) = fract_poly p + fract_poly q"
```
```   111   by (intro poly_eqI) (simp_all add: coeff_map_poly)
```
```   112
```
```   113 lemma fract_poly_diff [simp]:
```
```   114   "fract_poly (p - q) = fract_poly p - fract_poly q"
```
```   115   by (intro poly_eqI) (simp_all add: coeff_map_poly)
```
```   116
```
```   117 lemma to_fract_sum [simp]: "to_fract (sum f A) = sum (\<lambda>x. to_fract (f x)) A"
```
```   118   by (cases "finite A", induction A rule: finite_induct) simp_all
```
```   119
```
```   120 lemma fract_poly_mult [simp]:
```
```   121   "fract_poly (p * q) = fract_poly p * fract_poly q"
```
```   122   by (intro poly_eqI) (simp_all add: coeff_map_poly coeff_mult)
```
```   123
```
```   124 lemma fract_poly_eq_iff [simp]: "fract_poly p = fract_poly q \<longleftrightarrow> p = q"
```
```   125   by (auto simp: poly_eq_iff coeff_map_poly)
```
```   126
```
```   127 lemma fract_poly_eq_0_iff [simp]: "fract_poly p = 0 \<longleftrightarrow> p = 0"
```
```   128   using fract_poly_eq_iff[of p 0] by (simp del: fract_poly_eq_iff)
```
```   129
```
```   130 lemma fract_poly_dvd: "p dvd q \<Longrightarrow> fract_poly p dvd fract_poly q"
```
```   131   by (auto elim!: dvdE)
```
```   132
```
```   133 lemma prod_mset_fract_poly:
```
```   134   "(\<Prod>x\<in>#A. map_poly to_fract (f x)) = fract_poly (prod_mset (image_mset f A))"
```
```   135   by (induct A) (simp_all add: ac_simps)
```
```   136
```
```   137 lemma is_unit_fract_poly_iff:
```
```   138   "p dvd 1 \<longleftrightarrow> fract_poly p dvd 1 \<and> content p = 1"
```
```   139 proof safe
```
```   140   assume A: "p dvd 1"
```
```   141   with fract_poly_dvd [of p 1] show "is_unit (fract_poly p)"
```
```   142     by simp
```
```   143   from A show "content p = 1"
```
```   144     by (auto simp: is_unit_poly_iff normalize_1_iff)
```
```   145 next
```
```   146   assume A: "fract_poly p dvd 1" and B: "content p = 1"
```
```   147   from A obtain c where c: "fract_poly p = [:c:]" by (auto simp: is_unit_poly_iff)
```
```   148   {
```
```   149     fix n :: nat assume "n > 0"
```
```   150     have "to_fract (coeff p n) = coeff (fract_poly p) n" by (simp add: coeff_map_poly)
```
```   151     also note c
```
```   152     also from \<open>n > 0\<close> have "coeff [:c:] n = 0" by (simp add: coeff_pCons split: nat.splits)
```
```   153     finally have "coeff p n = 0" by simp
```
```   154   }
```
```   155   hence "degree p \<le> 0" by (intro degree_le) simp_all
```
```   156   with B show "p dvd 1" by (auto simp: is_unit_poly_iff normalize_1_iff elim!: degree_eq_zeroE)
```
```   157 qed
```
```   158
```
```   159 lemma fract_poly_is_unit: "p dvd 1 \<Longrightarrow> fract_poly p dvd 1"
```
```   160   using fract_poly_dvd[of p 1] by simp
```
```   161
```
```   162 lemma fract_poly_smult_eqE:
```
```   163   fixes c :: "'a :: {idom_divide,ring_gcd} fract"
```
```   164   assumes "fract_poly p = smult c (fract_poly q)"
```
```   165   obtains a b
```
```   166     where "c = to_fract b / to_fract a" "smult a p = smult b q" "coprime a b" "normalize a = a"
```
```   167 proof -
```
```   168   define a b where "a = fst (quot_of_fract c)" and "b = snd (quot_of_fract c)"
```
```   169   have "smult (to_fract a) (fract_poly q) = smult (to_fract b) (fract_poly p)"
```
```   170     by (subst smult_eq_iff) (simp_all add: a_def b_def Fract_conv_to_fract [symmetric] assms)
```
```   171   hence "fract_poly (smult a q) = fract_poly (smult b p)" by (simp del: fract_poly_eq_iff)
```
```   172   hence "smult b p = smult a q" by (simp only: fract_poly_eq_iff)
```
```   173   moreover have "c = to_fract a / to_fract b" "coprime b a" "normalize b = b"
```
```   174     by (simp_all add: a_def b_def coprime_quot_of_fract gcd.commute
```
```   175           normalize_snd_quot_of_fract Fract_conv_to_fract [symmetric])
```
```   176   ultimately show ?thesis by (intro that[of a b])
```
```   177 qed
```
```   178
```
```   179
```
```   180 subsection \<open>Fractional content\<close>
```
```   181
```
```   182 abbreviation (input) Lcm_coeff_denoms
```
```   183     :: "'a :: {semiring_Gcd,idom_divide,ring_gcd} fract poly \<Rightarrow> 'a"
```
```   184   where "Lcm_coeff_denoms p \<equiv> Lcm (snd ` quot_of_fract ` set (coeffs p))"
```
```   185
```
```   186 definition fract_content ::
```
```   187       "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly \<Rightarrow> 'a fract" where
```
```   188   "fract_content p =
```
```   189      (let d = Lcm_coeff_denoms p in Fract (content (unfract_poly (smult (to_fract d) p))) d)"
```
```   190
```
```   191 definition primitive_part_fract ::
```
```   192       "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly \<Rightarrow> 'a poly" where
```
```   193   "primitive_part_fract p =
```
```   194      primitive_part (unfract_poly (smult (to_fract (Lcm_coeff_denoms p)) p))"
```
```   195
```
```   196 lemma primitive_part_fract_0 [simp]: "primitive_part_fract 0 = 0"
```
```   197   by (simp add: primitive_part_fract_def)
```
```   198
```
```   199 lemma fract_content_eq_0_iff [simp]:
```
```   200   "fract_content p = 0 \<longleftrightarrow> p = 0"
```
```   201   unfolding fract_content_def Let_def Zero_fract_def
```
```   202   by (subst eq_fract) (auto simp: Lcm_0_iff map_poly_eq_0_iff)
```
```   203
```
```   204 lemma content_primitive_part_fract [simp]: "p \<noteq> 0 \<Longrightarrow> content (primitive_part_fract p) = 1"
```
```   205   unfolding primitive_part_fract_def
```
```   206   by (rule content_primitive_part)
```
```   207      (auto simp: primitive_part_fract_def map_poly_eq_0_iff Lcm_0_iff)
```
```   208
```
```   209 lemma content_times_primitive_part_fract:
```
```   210   "smult (fract_content p) (fract_poly (primitive_part_fract p)) = p"
```
```   211 proof -
```
```   212   define p' where "p' = unfract_poly (smult (to_fract (Lcm_coeff_denoms p)) p)"
```
```   213   have "fract_poly p' =
```
```   214           map_poly (to_fract \<circ> fst \<circ> quot_of_fract) (smult (to_fract (Lcm_coeff_denoms p)) p)"
```
```   215     unfolding primitive_part_fract_def p'_def
```
```   216     by (subst map_poly_map_poly) (simp_all add: o_assoc)
```
```   217   also have "\<dots> = smult (to_fract (Lcm_coeff_denoms p)) p"
```
```   218   proof (intro map_poly_idI, unfold o_apply)
```
```   219     fix c assume "c \<in> set (coeffs (smult (to_fract (Lcm_coeff_denoms p)) p))"
```
```   220     then obtain c' where c: "c' \<in> set (coeffs p)" "c = to_fract (Lcm_coeff_denoms p) * c'"
```
```   221       by (auto simp add: Lcm_0_iff coeffs_smult split: if_splits)
```
```   222     note c(2)
```
```   223     also have "c' = Fract (fst (quot_of_fract c')) (snd (quot_of_fract c'))"
```
```   224       by simp
```
```   225     also have "to_fract (Lcm_coeff_denoms p) * \<dots> =
```
```   226                  Fract (Lcm_coeff_denoms p * fst (quot_of_fract c')) (snd (quot_of_fract c'))"
```
```   227       unfolding to_fract_def by (subst mult_fract) simp_all
```
```   228     also have "snd (quot_of_fract \<dots>) = 1"
```
```   229       by (intro snd_quot_of_fract_Fract_whole dvd_mult2 dvd_Lcm) (insert c(1), auto)
```
```   230     finally show "to_fract (fst (quot_of_fract c)) = c"
```
```   231       by (rule to_fract_quot_of_fract)
```
```   232   qed
```
```   233   also have "p' = smult (content p') (primitive_part p')"
```
```   234     by (rule content_times_primitive_part [symmetric])
```
```   235   also have "primitive_part p' = primitive_part_fract p"
```
```   236     by (simp add: primitive_part_fract_def p'_def)
```
```   237   also have "fract_poly (smult (content p') (primitive_part_fract p)) =
```
```   238                smult (to_fract (content p')) (fract_poly (primitive_part_fract p))" by simp
```
```   239   finally have "smult (to_fract (content p')) (fract_poly (primitive_part_fract p)) =
```
```   240                       smult (to_fract (Lcm_coeff_denoms p)) p" .
```
```   241   thus ?thesis
```
```   242     by (subst (asm) smult_eq_iff)
```
```   243        (auto simp add: Let_def p'_def Fract_conv_to_fract field_simps Lcm_0_iff fract_content_def)
```
```   244 qed
```
```   245
```
```   246 lemma fract_content_fract_poly [simp]: "fract_content (fract_poly p) = to_fract (content p)"
```
```   247 proof -
```
```   248   have "Lcm_coeff_denoms (fract_poly p) = 1"
```
```   249     by (auto simp: set_coeffs_map_poly)
```
```   250   hence "fract_content (fract_poly p) =
```
```   251            to_fract (content (map_poly (fst \<circ> quot_of_fract \<circ> to_fract) p))"
```
```   252     by (simp add: fract_content_def to_fract_def fract_collapse map_poly_map_poly del: Lcm_1_iff)
```
```   253   also have "map_poly (fst \<circ> quot_of_fract \<circ> to_fract) p = p"
```
```   254     by (intro map_poly_idI) simp_all
```
```   255   finally show ?thesis .
```
```   256 qed
```
```   257
```
```   258 lemma content_decompose_fract:
```
```   259   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly"
```
```   260   obtains c p' where "p = smult c (map_poly to_fract p')" "content p' = 1"
```
```   261 proof (cases "p = 0")
```
```   262   case True
```
```   263   hence "p = smult 0 (map_poly to_fract 1)" "content 1 = 1" by simp_all
```
```   264   thus ?thesis ..
```
```   265 next
```
```   266   case False
```
```   267   thus ?thesis
```
```   268     by (rule that[OF content_times_primitive_part_fract [symmetric] content_primitive_part_fract])
```
```   269 qed
```
```   270
```
```   271
```
```   272 subsection \<open>More properties of content and primitive part\<close>
```
```   273
```
```   274 lemma lift_prime_elem_poly:
```
```   275   assumes "prime_elem (c :: 'a :: semidom)"
```
```   276   shows   "prime_elem [:c:]"
```
```   277 proof (rule prime_elemI)
```
```   278   fix a b assume *: "[:c:] dvd a * b"
```
```   279   from * have dvd: "c dvd coeff (a * b) n" for n
```
```   280     by (subst (asm) const_poly_dvd_iff) blast
```
```   281   {
```
```   282     define m where "m = (GREATEST m. \<not>c dvd coeff b m)"
```
```   283     assume "\<not>[:c:] dvd b"
```
```   284     hence A: "\<exists>i. \<not>c dvd coeff b i" by (subst (asm) const_poly_dvd_iff) blast
```
```   285     have B: "\<forall>i. \<not>c dvd coeff b i \<longrightarrow> i \<le> degree b"
```
```   286       by (auto intro: le_degree)
```
```   287     have coeff_m: "\<not>c dvd coeff b m" unfolding m_def by (rule GreatestI_ex_nat[OF A B])
```
```   288     have "i \<le> m" if "\<not>c dvd coeff b i" for i
```
```   289       unfolding m_def by (rule Greatest_le_nat[OF that B])
```
```   290     hence dvd_b: "c dvd coeff b i" if "i > m" for i using that by force
```
```   291
```
```   292     have "c dvd coeff a i" for i
```
```   293     proof (induction i rule: nat_descend_induct[of "degree a"])
```
```   294       case (base i)
```
```   295       thus ?case by (simp add: coeff_eq_0)
```
```   296     next
```
```   297       case (descend i)
```
```   298       let ?A = "{..i+m} - {i}"
```
```   299       have "c dvd coeff (a * b) (i + m)" by (rule dvd)
```
```   300       also have "coeff (a * b) (i + m) = (\<Sum>k\<le>i + m. coeff a k * coeff b (i + m - k))"
```
```   301         by (simp add: coeff_mult)
```
```   302       also have "{..i+m} = insert i ?A" by auto
```
```   303       also have "(\<Sum>k\<in>\<dots>. coeff a k * coeff b (i + m - k)) =
```
```   304                    coeff a i * coeff b m + (\<Sum>k\<in>?A. coeff a k * coeff b (i + m - k))"
```
```   305         (is "_ = _ + ?S")
```
```   306         by (subst sum.insert) simp_all
```
```   307       finally have eq: "c dvd coeff a i * coeff b m + ?S" .
```
```   308       moreover have "c dvd ?S"
```
```   309       proof (rule dvd_sum)
```
```   310         fix k assume k: "k \<in> {..i+m} - {i}"
```
```   311         show "c dvd coeff a k * coeff b (i + m - k)"
```
```   312         proof (cases "k < i")
```
```   313           case False
```
```   314           with k have "c dvd coeff a k" by (intro descend.IH) simp
```
```   315           thus ?thesis by simp
```
```   316         next
```
```   317           case True
```
```   318           hence "c dvd coeff b (i + m - k)" by (intro dvd_b) simp
```
```   319           thus ?thesis by simp
```
```   320         qed
```
```   321       qed
```
```   322       ultimately have "c dvd coeff a i * coeff b m"
```
```   323         by (simp add: dvd_add_left_iff)
```
```   324       with assms coeff_m show "c dvd coeff a i"
```
```   325         by (simp add: prime_elem_dvd_mult_iff)
```
```   326     qed
```
```   327     hence "[:c:] dvd a" by (subst const_poly_dvd_iff) blast
```
```   328   }
```
```   329   then show "[:c:] dvd a \<or> [:c:] dvd b" by blast
```
```   330 next
```
```   331   from assms show "[:c:] \<noteq> 0" and "\<not> [:c:] dvd 1"
```
```   332     by (simp_all add: prime_elem_def is_unit_const_poly_iff)
```
```   333 qed
```
```   334
```
```   335 lemma prime_elem_const_poly_iff:
```
```   336   fixes c :: "'a :: semidom"
```
```   337   shows   "prime_elem [:c:] \<longleftrightarrow> prime_elem c"
```
```   338 proof
```
```   339   assume A: "prime_elem [:c:]"
```
```   340   show "prime_elem c"
```
```   341   proof (rule prime_elemI)
```
```   342     fix a b assume "c dvd a * b"
```
```   343     hence "[:c:] dvd [:a:] * [:b:]" by (simp add: mult_ac)
```
```   344     from A and this have "[:c:] dvd [:a:] \<or> [:c:] dvd [:b:]" by (rule prime_elem_dvd_multD)
```
```   345     thus "c dvd a \<or> c dvd b" by simp
```
```   346   qed (insert A, auto simp: prime_elem_def is_unit_poly_iff)
```
```   347 qed (auto intro: lift_prime_elem_poly)
```
```   348
```
```   349 context
```
```   350 begin
```
```   351
```
```   352 private lemma content_1_mult:
```
```   353   fixes f g :: "'a :: {semiring_Gcd,factorial_semiring} poly"
```
```   354   assumes "content f = 1" "content g = 1"
```
```   355   shows   "content (f * g) = 1"
```
```   356 proof (cases "f * g = 0")
```
```   357   case False
```
```   358   from assms have "f \<noteq> 0" "g \<noteq> 0" by auto
```
```   359
```
```   360   hence "f * g \<noteq> 0" by auto
```
```   361   {
```
```   362     assume "\<not>is_unit (content (f * g))"
```
```   363     with False have "\<exists>p. p dvd content (f * g) \<and> prime p"
```
```   364       by (intro prime_divisor_exists) simp_all
```
```   365     then obtain p where "p dvd content (f * g)" "prime p" by blast
```
```   366     from \<open>p dvd content (f * g)\<close> have "[:p:] dvd f * g"
```
```   367       by (simp add: const_poly_dvd_iff_dvd_content)
```
```   368     moreover from \<open>prime p\<close> have "prime_elem [:p:]" by (simp add: lift_prime_elem_poly)
```
```   369     ultimately have "[:p:] dvd f \<or> [:p:] dvd g"
```
```   370       by (simp add: prime_elem_dvd_mult_iff)
```
```   371     with assms have "is_unit p" by (simp add: const_poly_dvd_iff_dvd_content)
```
```   372     with \<open>prime p\<close> have False by simp
```
```   373   }
```
```   374   hence "is_unit (content (f * g))" by blast
```
```   375   hence "normalize (content (f * g)) = 1" by (simp add: is_unit_normalize del: normalize_content)
```
```   376   thus ?thesis by simp
```
```   377 qed (insert assms, auto)
```
```   378
```
```   379 lemma content_mult:
```
```   380   fixes p q :: "'a :: {factorial_semiring, semiring_Gcd} poly"
```
```   381   shows "content (p * q) = content p * content q"
```
```   382 proof -
```
```   383   from content_decompose[of p] guess p' . note p = this
```
```   384   from content_decompose[of q] guess q' . note q = this
```
```   385   have "content (p * q) = content p * content q * content (p' * q')"
```
```   386     by (subst p, subst q) (simp add: mult_ac normalize_mult)
```
```   387   also from p q have "content (p' * q') = 1" by (intro content_1_mult)
```
```   388   finally show ?thesis by simp
```
```   389 qed
```
```   390
```
```   391 lemma fract_poly_dvdD:
```
```   392   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
```
```   393   assumes "fract_poly p dvd fract_poly q" "content p = 1"
```
```   394   shows   "p dvd q"
```
```   395 proof -
```
```   396   from assms(1) obtain r where r: "fract_poly q = fract_poly p * r" by (erule dvdE)
```
```   397   from content_decompose_fract[of r] guess c r' . note r' = this
```
```   398   from r r' have eq: "fract_poly q = smult c (fract_poly (p * r'))" by simp
```
```   399   from fract_poly_smult_eqE[OF this] guess a b . note ab = this
```
```   400   have "content (smult a q) = content (smult b (p * r'))" by (simp only: ab(2))
```
```   401   hence eq': "normalize b = a * content q" by (simp add: assms content_mult r' ab(4))
```
```   402   have "1 = gcd a (normalize b)" by (simp add: ab)
```
```   403   also note eq'
```
```   404   also have "gcd a (a * content q) = a" by (simp add: gcd_proj1_if_dvd ab(4))
```
```   405   finally have [simp]: "a = 1" by simp
```
```   406   from eq ab have "q = p * ([:b:] * r')" by simp
```
```   407   thus ?thesis by (rule dvdI)
```
```   408 qed
```
```   409
```
```   410 end
```
```   411
```
```   412
```
```   413 subsection \<open>Polynomials over a field are a Euclidean ring\<close>
```
```   414
```
```   415 context
```
```   416 begin
```
```   417
```
```   418 interpretation field_poly:
```
```   419   normalization_euclidean_semiring where zero = "0 :: 'a :: field poly"
```
```   420     and one = 1 and plus = plus and minus = minus
```
```   421     and times = times
```
```   422     and normalize = "\<lambda>p. smult (inverse (lead_coeff p)) p"
```
```   423     and unit_factor = "\<lambda>p. [:lead_coeff p:]"
```
```   424     and euclidean_size = "\<lambda>p. if p = 0 then 0 else 2 ^ degree p"
```
```   425     and divide = divide and modulo = modulo
```
```   426   rewrites "dvd.dvd (times :: 'a poly \<Rightarrow> _) = Rings.dvd"
```
```   427     and "comm_monoid_mult.prod_mset times 1 = prod_mset"
```
```   428     and "comm_semiring_1.irreducible times 1 0 = irreducible"
```
```   429     and "comm_semiring_1.prime_elem times 1 0 = prime_elem"
```
```   430 proof -
```
```   431   show "dvd.dvd (times :: 'a poly \<Rightarrow> _) = Rings.dvd"
```
```   432     by (simp add: dvd_dict)
```
```   433   show "comm_monoid_mult.prod_mset times 1 = prod_mset"
```
```   434     by (simp add: prod_mset_dict)
```
```   435   show "comm_semiring_1.irreducible times 1 0 = irreducible"
```
```   436     by (simp add: irreducible_dict)
```
```   437   show "comm_semiring_1.prime_elem times 1 0 = prime_elem"
```
```   438     by (simp add: prime_elem_dict)
```
```   439   show "class.normalization_euclidean_semiring divide plus minus (0 :: 'a poly) times 1
```
```   440     modulo (\<lambda>p. if p = 0 then 0 else 2 ^ degree p)
```
```   441     (\<lambda>p. [:lead_coeff p:]) (\<lambda>p. smult (inverse (lead_coeff p)) p)"
```
```   442   proof (standard, fold dvd_dict)
```
```   443     fix p :: "'a poly"
```
```   444     show "[:lead_coeff p:] * smult (inverse (lead_coeff p)) p = p"
```
```   445       by (cases "p = 0") simp_all
```
```   446   next
```
```   447     fix p :: "'a poly" assume "is_unit p"
```
```   448     then show "[:lead_coeff p:] = p"
```
```   449       by (elim is_unit_polyE) (auto simp: monom_0 one_poly_def field_simps)
```
```   450   next
```
```   451     fix p :: "'a poly" assume "p \<noteq> 0"
```
```   452     then show "is_unit [:lead_coeff p:]"
```
```   453       by (simp add: is_unit_pCons_iff)
```
```   454   qed (auto simp: lead_coeff_mult Rings.div_mult_mod_eq intro!: degree_mod_less' degree_mult_right_le)
```
```   455 qed
```
```   456
```
```   457 lemma field_poly_irreducible_imp_prime:
```
```   458   "prime_elem p" if "irreducible p" for p :: "'a :: field poly"
```
```   459   using that by (fact field_poly.irreducible_imp_prime_elem)
```
```   460
```
```   461 lemma field_poly_prod_mset_prime_factorization:
```
```   462   "prod_mset (field_poly.prime_factorization p) = smult (inverse (lead_coeff p)) p"
```
```   463   if "p \<noteq> 0" for p :: "'a :: field poly"
```
```   464   using that by (fact field_poly.prod_mset_prime_factorization)
```
```   465
```
```   466 lemma field_poly_in_prime_factorization_imp_prime:
```
```   467   "prime_elem p" if "p \<in># field_poly.prime_factorization x"
```
```   468   for p :: "'a :: field poly"
```
```   469   by (rule field_poly.prime_imp_prime_elem, rule field_poly.in_prime_factors_imp_prime)
```
```   470     (fact that)
```
```   471
```
```   472
```
```   473 subsection \<open>Primality and irreducibility in polynomial rings\<close>
```
```   474
```
```   475 lemma nonconst_poly_irreducible_iff:
```
```   476   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
```
```   477   assumes "degree p \<noteq> 0"
```
```   478   shows   "irreducible p \<longleftrightarrow> irreducible (fract_poly p) \<and> content p = 1"
```
```   479 proof safe
```
```   480   assume p: "irreducible p"
```
```   481
```
```   482   from content_decompose[of p] guess p' . note p' = this
```
```   483   hence "p = [:content p:] * p'" by simp
```
```   484   from p this have "[:content p:] dvd 1 \<or> p' dvd 1" by (rule irreducibleD)
```
```   485   moreover have "\<not>p' dvd 1"
```
```   486   proof
```
```   487     assume "p' dvd 1"
```
```   488     hence "degree p = 0" by (subst p') (auto simp: is_unit_poly_iff)
```
```   489     with assms show False by contradiction
```
```   490   qed
```
```   491   ultimately show [simp]: "content p = 1" by (simp add: is_unit_const_poly_iff)
```
```   492
```
```   493   show "irreducible (map_poly to_fract p)"
```
```   494   proof (rule irreducibleI)
```
```   495     have "fract_poly p = 0 \<longleftrightarrow> p = 0" by (intro map_poly_eq_0_iff) auto
```
```   496     with assms show "map_poly to_fract p \<noteq> 0" by auto
```
```   497   next
```
```   498     show "\<not>is_unit (fract_poly p)"
```
```   499     proof
```
```   500       assume "is_unit (map_poly to_fract p)"
```
```   501       hence "degree (map_poly to_fract p) = 0"
```
```   502         by (auto simp: is_unit_poly_iff)
```
```   503       hence "degree p = 0" by (simp add: degree_map_poly)
```
```   504       with assms show False by contradiction
```
```   505    qed
```
```   506  next
```
```   507    fix q r assume qr: "fract_poly p = q * r"
```
```   508    from content_decompose_fract[of q] guess cg q' . note q = this
```
```   509    from content_decompose_fract[of r] guess cr r' . note r = this
```
```   510    from qr q r p have nz: "cg \<noteq> 0" "cr \<noteq> 0" by auto
```
```   511    from qr have eq: "fract_poly p = smult (cr * cg) (fract_poly (q' * r'))"
```
```   512      by (simp add: q r)
```
```   513    from fract_poly_smult_eqE[OF this] guess a b . note ab = this
```
```   514    hence "content (smult a p) = content (smult b (q' * r'))" by (simp only:)
```
```   515    with ab(4) have a: "a = normalize b" by (simp add: content_mult q r)
```
```   516    hence "normalize b = gcd a b" by simp
```
```   517    also from ab(3) have "\<dots> = 1" .
```
```   518    finally have "a = 1" "is_unit b" by (simp_all add: a normalize_1_iff)
```
```   519
```
```   520    note eq
```
```   521    also from ab(1) \<open>a = 1\<close> have "cr * cg = to_fract b" by simp
```
```   522    also have "smult \<dots> (fract_poly (q' * r')) = fract_poly (smult b (q' * r'))" by simp
```
```   523    finally have "p = ([:b:] * q') * r'" by (simp del: fract_poly_smult)
```
```   524    from p and this have "([:b:] * q') dvd 1 \<or> r' dvd 1" by (rule irreducibleD)
```
```   525    hence "q' dvd 1 \<or> r' dvd 1" by (auto dest: dvd_mult_right simp del: mult_pCons_left)
```
```   526    hence "fract_poly q' dvd 1 \<or> fract_poly r' dvd 1" by (auto simp: fract_poly_is_unit)
```
```   527    with q r show "is_unit q \<or> is_unit r"
```
```   528      by (auto simp add: is_unit_smult_iff dvd_field_iff nz)
```
```   529  qed
```
```   530
```
```   531 next
```
```   532
```
```   533   assume irred: "irreducible (fract_poly p)" and primitive: "content p = 1"
```
```   534   show "irreducible p"
```
```   535   proof (rule irreducibleI)
```
```   536     from irred show "p \<noteq> 0" by auto
```
```   537   next
```
```   538     from irred show "\<not>p dvd 1"
```
```   539       by (auto simp: irreducible_def dest: fract_poly_is_unit)
```
```   540   next
```
```   541     fix q r assume qr: "p = q * r"
```
```   542     hence "fract_poly p = fract_poly q * fract_poly r" by simp
```
```   543     from irred and this have "fract_poly q dvd 1 \<or> fract_poly r dvd 1"
```
```   544       by (rule irreducibleD)
```
```   545     with primitive qr show "q dvd 1 \<or> r dvd 1"
```
```   546       by (auto simp:  content_prod_eq_1_iff is_unit_fract_poly_iff)
```
```   547   qed
```
```   548 qed
```
```   549
```
```   550 private lemma irreducible_imp_prime_poly:
```
```   551   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
```
```   552   assumes "irreducible p"
```
```   553   shows   "prime_elem p"
```
```   554 proof (cases "degree p = 0")
```
```   555   case True
```
```   556   with assms show ?thesis
```
```   557     by (auto simp: prime_elem_const_poly_iff irreducible_const_poly_iff
```
```   558              intro!: irreducible_imp_prime_elem elim!: degree_eq_zeroE)
```
```   559 next
```
```   560   case False
```
```   561   from assms False have irred: "irreducible (fract_poly p)" and primitive: "content p = 1"
```
```   562     by (simp_all add: nonconst_poly_irreducible_iff)
```
```   563   from irred have prime: "prime_elem (fract_poly p)" by (rule field_poly_irreducible_imp_prime)
```
```   564   show ?thesis
```
```   565   proof (rule prime_elemI)
```
```   566     fix q r assume "p dvd q * r"
```
```   567     hence "fract_poly p dvd fract_poly (q * r)" by (rule fract_poly_dvd)
```
```   568     hence "fract_poly p dvd fract_poly q * fract_poly r" by simp
```
```   569     from prime and this have "fract_poly p dvd fract_poly q \<or> fract_poly p dvd fract_poly r"
```
```   570       by (rule prime_elem_dvd_multD)
```
```   571     with primitive show "p dvd q \<or> p dvd r" by (auto dest: fract_poly_dvdD)
```
```   572   qed (insert assms, auto simp: irreducible_def)
```
```   573 qed
```
```   574
```
```   575 lemma degree_primitive_part_fract [simp]:
```
```   576   "degree (primitive_part_fract p) = degree p"
```
```   577 proof -
```
```   578   have "p = smult (fract_content p) (fract_poly (primitive_part_fract p))"
```
```   579     by (simp add: content_times_primitive_part_fract)
```
```   580   also have "degree \<dots> = degree (primitive_part_fract p)"
```
```   581     by (auto simp: degree_map_poly)
```
```   582   finally show ?thesis ..
```
```   583 qed
```
```   584
```
```   585 lemma irreducible_primitive_part_fract:
```
```   586   fixes p :: "'a :: {idom_divide, ring_gcd, factorial_semiring, semiring_Gcd} fract poly"
```
```   587   assumes "irreducible p"
```
```   588   shows   "irreducible (primitive_part_fract p)"
```
```   589 proof -
```
```   590   from assms have deg: "degree (primitive_part_fract p) \<noteq> 0"
```
```   591     by (intro notI)
```
```   592        (auto elim!: degree_eq_zeroE simp: irreducible_def is_unit_poly_iff dvd_field_iff)
```
```   593   hence [simp]: "p \<noteq> 0" by auto
```
```   594
```
```   595   note \<open>irreducible p\<close>
```
```   596   also have "p = [:fract_content p:] * fract_poly (primitive_part_fract p)"
```
```   597     by (simp add: content_times_primitive_part_fract)
```
```   598   also have "irreducible \<dots> \<longleftrightarrow> irreducible (fract_poly (primitive_part_fract p))"
```
```   599     by (intro irreducible_mult_unit_left) (simp_all add: is_unit_poly_iff dvd_field_iff)
```
```   600   finally show ?thesis using deg
```
```   601     by (simp add: nonconst_poly_irreducible_iff)
```
```   602 qed
```
```   603
```
```   604 lemma prime_elem_primitive_part_fract:
```
```   605   fixes p :: "'a :: {idom_divide, ring_gcd, factorial_semiring, semiring_Gcd} fract poly"
```
```   606   shows "irreducible p \<Longrightarrow> prime_elem (primitive_part_fract p)"
```
```   607   by (intro irreducible_imp_prime_poly irreducible_primitive_part_fract)
```
```   608
```
```   609 lemma irreducible_linear_field_poly:
```
```   610   fixes a b :: "'a::field"
```
```   611   assumes "b \<noteq> 0"
```
```   612   shows "irreducible [:a,b:]"
```
```   613 proof (rule irreducibleI)
```
```   614   fix p q assume pq: "[:a,b:] = p * q"
```
```   615   also from pq assms have "degree \<dots> = degree p + degree q"
```
```   616     by (intro degree_mult_eq) auto
```
```   617   finally have "degree p = 0 \<or> degree q = 0" using assms by auto
```
```   618   with assms pq show "is_unit p \<or> is_unit q"
```
```   619     by (auto simp: is_unit_const_poly_iff dvd_field_iff elim!: degree_eq_zeroE)
```
```   620 qed (insert assms, auto simp: is_unit_poly_iff)
```
```   621
```
```   622 lemma prime_elem_linear_field_poly:
```
```   623   "(b :: 'a :: field) \<noteq> 0 \<Longrightarrow> prime_elem [:a,b:]"
```
```   624   by (rule field_poly_irreducible_imp_prime, rule irreducible_linear_field_poly)
```
```   625
```
```   626 lemma irreducible_linear_poly:
```
```   627   fixes a b :: "'a::{idom_divide,ring_gcd,factorial_semiring,semiring_Gcd}"
```
```   628   shows "b \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> irreducible [:a,b:]"
```
```   629   by (auto intro!: irreducible_linear_field_poly
```
```   630            simp:   nonconst_poly_irreducible_iff content_def map_poly_pCons)
```
```   631
```
```   632 lemma prime_elem_linear_poly:
```
```   633   fixes a b :: "'a::{idom_divide,ring_gcd,factorial_semiring,semiring_Gcd}"
```
```   634   shows "b \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> prime_elem [:a,b:]"
```
```   635   by (rule irreducible_imp_prime_poly, rule irreducible_linear_poly)
```
```   636
```
```   637
```
```   638 subsection \<open>Prime factorisation of polynomials\<close>
```
```   639
```
```   640 private lemma poly_prime_factorization_exists_content_1:
```
```   641   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
```
```   642   assumes "p \<noteq> 0" "content p = 1"
```
```   643   shows   "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize p"
```
```   644 proof -
```
```   645   let ?P = "field_poly.prime_factorization (fract_poly p)"
```
```   646   define c where "c = prod_mset (image_mset fract_content ?P)"
```
```   647   define c' where "c' = c * to_fract (lead_coeff p)"
```
```   648   define e where "e = prod_mset (image_mset primitive_part_fract ?P)"
```
```   649   define A where "A = image_mset (normalize \<circ> primitive_part_fract) ?P"
```
```   650   have "content e = (\<Prod>x\<in>#field_poly.prime_factorization (map_poly to_fract p).
```
```   651                       content (primitive_part_fract x))"
```
```   652     by (simp add: e_def content_prod_mset multiset.map_comp o_def)
```
```   653   also have "image_mset (\<lambda>x. content (primitive_part_fract x)) ?P = image_mset (\<lambda>_. 1) ?P"
```
```   654     by (intro image_mset_cong content_primitive_part_fract) auto
```
```   655   finally have content_e: "content e = 1"
```
```   656     by simp
```
```   657
```
```   658   from \<open>p \<noteq> 0\<close> have "fract_poly p = [:lead_coeff (fract_poly p):] *
```
```   659     smult (inverse (lead_coeff (fract_poly p))) (fract_poly p)"
```
```   660     by simp
```
```   661   also have "[:lead_coeff (fract_poly p):] = [:to_fract (lead_coeff p):]"
```
```   662     by (simp add: monom_0 degree_map_poly coeff_map_poly)
```
```   663   also from assms have "smult (inverse (lead_coeff (fract_poly p))) (fract_poly p) = prod_mset ?P"
```
```   664     by (subst field_poly_prod_mset_prime_factorization) simp_all
```
```   665   also have "\<dots> = prod_mset (image_mset id ?P)" by simp
```
```   666   also have "image_mset id ?P =
```
```   667                image_mset (\<lambda>x. [:fract_content x:] * fract_poly (primitive_part_fract x)) ?P"
```
```   668     by (intro image_mset_cong) (auto simp: content_times_primitive_part_fract)
```
```   669   also have "prod_mset \<dots> = smult c (fract_poly e)"
```
```   670     by (subst prod_mset.distrib) (simp_all add: prod_mset_fract_poly prod_mset_const_poly c_def e_def)
```
```   671   also have "[:to_fract (lead_coeff p):] * \<dots> = smult c' (fract_poly e)"
```
```   672     by (simp add: c'_def)
```
```   673   finally have eq: "fract_poly p = smult c' (fract_poly e)" .
```
```   674   also obtain b where b: "c' = to_fract b" "is_unit b"
```
```   675   proof -
```
```   676     from fract_poly_smult_eqE[OF eq] guess a b . note ab = this
```
```   677     from ab(2) have "content (smult a p) = content (smult b e)" by (simp only: )
```
```   678     with assms content_e have "a = normalize b" by (simp add: ab(4))
```
```   679     with ab have ab': "a = 1" "is_unit b" by (simp_all add: normalize_1_iff)
```
```   680     with ab ab' have "c' = to_fract b" by auto
```
```   681     from this and \<open>is_unit b\<close> show ?thesis by (rule that)
```
```   682   qed
```
```   683   hence "smult c' (fract_poly e) = fract_poly (smult b e)" by simp
```
```   684   finally have "p = smult b e" by (simp only: fract_poly_eq_iff)
```
```   685   hence "p = [:b:] * e" by simp
```
```   686   with b have "normalize p = normalize e"
```
```   687     by (simp only: normalize_mult) (simp add: is_unit_normalize is_unit_poly_iff)
```
```   688   also have "normalize e = prod_mset A"
```
```   689     by (simp add: multiset.map_comp e_def A_def normalize_prod_mset)
```
```   690   finally have "prod_mset A = normalize p" ..
```
```   691
```
```   692   have "prime_elem p" if "p \<in># A" for p
```
```   693     using that by (auto simp: A_def prime_elem_primitive_part_fract prime_elem_imp_irreducible
```
```   694                         dest!: field_poly_in_prime_factorization_imp_prime )
```
```   695   from this and \<open>prod_mset A = normalize p\<close> show ?thesis
```
```   696     by (intro exI[of _ A]) blast
```
```   697 qed
```
```   698
```
```   699 lemma poly_prime_factorization_exists:
```
```   700   fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
```
```   701   assumes "p \<noteq> 0"
```
```   702   shows   "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize p"
```
```   703 proof -
```
```   704   define B where "B = image_mset (\<lambda>x. [:x:]) (prime_factorization (content p))"
```
```   705   have "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize (primitive_part p)"
```
```   706     by (rule poly_prime_factorization_exists_content_1) (insert assms, simp_all)
```
```   707   then guess A by (elim exE conjE) note A = this
```
```   708   moreover from assms have "prod_mset B = [:content p:]"
```
```   709     by (simp add: B_def prod_mset_const_poly prod_mset_prime_factorization)
```
```   710   moreover have "\<forall>p. p \<in># B \<longrightarrow> prime_elem p"
```
```   711     by (auto simp: B_def intro!: lift_prime_elem_poly dest: in_prime_factors_imp_prime)
```
```   712   ultimately show ?thesis by (intro exI[of _ "B + A"]) auto
```
```   713 qed
```
```   714
```
```   715 end
```
```   716
```
```   717
```
```   718 subsection \<open>Typeclass instances\<close>
```
```   719
```
```   720 instance poly :: (factorial_ring_gcd) factorial_semiring
```
```   721   by standard (rule poly_prime_factorization_exists)
```
```   722
```
```   723 instantiation poly :: (factorial_ring_gcd) factorial_ring_gcd
```
```   724 begin
```
```   725
```
```   726 definition gcd_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
```
```   727   [code del]: "gcd_poly = gcd_factorial"
```
```   728
```
```   729 definition lcm_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
```
```   730   [code del]: "lcm_poly = lcm_factorial"
```
```   731
```
```   732 definition Gcd_poly :: "'a poly set \<Rightarrow> 'a poly" where
```
```   733  [code del]: "Gcd_poly = Gcd_factorial"
```
```   734
```
```   735 definition Lcm_poly :: "'a poly set \<Rightarrow> 'a poly" where
```
```   736  [code del]: "Lcm_poly = Lcm_factorial"
```
```   737
```
```   738 instance by standard (simp_all add: gcd_poly_def lcm_poly_def Gcd_poly_def Lcm_poly_def)
```
```   739
```
```   740 end
```
```   741
```
```   742 instantiation poly :: ("{field,factorial_ring_gcd}") "{unique_euclidean_ring, normalization_euclidean_semiring}"
```
```   743 begin
```
```   744
```
```   745 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
```
```   746   where "euclidean_size_poly p = (if p = 0 then 0 else 2 ^ degree p)"
```
```   747
```
```   748 definition uniqueness_constraint_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
```
```   749   where [simp]: "uniqueness_constraint_poly = top"
```
```   750
```
```   751 instance proof
```
```   752   show "(q * p + r) div p = q" if "p \<noteq> 0"
```
```   753     and "euclidean_size r < euclidean_size p" for q p r :: "'a poly"
```
```   754   proof -
```
```   755     from \<open>p \<noteq> 0\<close> eucl_rel_poly [of r p] have "eucl_rel_poly (r + q * p) p (q + r div p, r mod p)"
```
```   756       by (simp add: eucl_rel_poly_iff distrib_right)
```
```   757     then have "(r + q * p) div p = q + r div p"
```
```   758       by (rule div_poly_eq)
```
```   759     with that show ?thesis
```
```   760       by (cases "r = 0") (simp_all add: euclidean_size_poly_def div_poly_less ac_simps)
```
```   761   qed
```
```   762 qed (auto simp: euclidean_size_poly_def Rings.div_mult_mod_eq div_poly_less degree_mult_eq intro!: degree_mod_less' degree_mult_right_le
```
```   763     split: if_splits)
```
```   764
```
```   765 end
```
```   766
```
```   767 instance poly :: ("{field, normalization_euclidean_semiring, factorial_ring_gcd}") euclidean_ring_gcd
```
```   768   by (rule euclidean_ring_gcd_class.intro, rule factorial_euclidean_semiring_gcdI) standard
```
```   769
```
```   770
```
```   771 subsection \<open>Polynomial GCD\<close>
```
```   772
```
```   773 lemma gcd_poly_decompose:
```
```   774   fixes p q :: "'a :: factorial_ring_gcd poly"
```
```   775   shows "gcd p q =
```
```   776            smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q))"
```
```   777 proof (rule sym, rule gcdI)
```
```   778   have "[:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q) dvd
```
```   779           [:content p:] * primitive_part p" by (intro mult_dvd_mono) simp_all
```
```   780   thus "smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q)) dvd p"
```
```   781     by simp
```
```   782 next
```
```   783   have "[:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q) dvd
```
```   784           [:content q:] * primitive_part q" by (intro mult_dvd_mono) simp_all
```
```   785   thus "smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q)) dvd q"
```
```   786     by simp
```
```   787 next
```
```   788   fix d assume "d dvd p" "d dvd q"
```
```   789   hence "[:content d:] * primitive_part d dvd
```
```   790            [:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q)"
```
```   791     by (intro mult_dvd_mono) auto
```
```   792   thus "d dvd smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q))"
```
```   793     by simp
```
```   794 qed (auto simp: normalize_smult)
```
```   795
```
```   796
```
```   797 lemma gcd_poly_pseudo_mod:
```
```   798   fixes p q :: "'a :: factorial_ring_gcd poly"
```
```   799   assumes nz: "q \<noteq> 0" and prim: "content p = 1" "content q = 1"
```
```   800   shows   "gcd p q = gcd q (primitive_part (pseudo_mod p q))"
```
```   801 proof -
```
```   802   define r s where "r = fst (pseudo_divmod p q)" and "s = snd (pseudo_divmod p q)"
```
```   803   define a where "a = [:coeff q (degree q) ^ (Suc (degree p) - degree q):]"
```
```   804   have [simp]: "primitive_part a = unit_factor a"
```
```   805     by (simp add: a_def unit_factor_poly_def unit_factor_power monom_0)
```
```   806   from nz have [simp]: "a \<noteq> 0" by (auto simp: a_def)
```
```   807
```
```   808   have rs: "pseudo_divmod p q = (r, s)" by (simp add: r_def s_def)
```
```   809   have "gcd (q * r + s) q = gcd q s"
```
```   810     using gcd_add_mult[of q r s] by (simp add: gcd.commute add_ac mult_ac)
```
```   811   with pseudo_divmod(1)[OF nz rs]
```
```   812     have "gcd (p * a) q = gcd q s" by (simp add: a_def)
```
```   813   also from prim have "gcd (p * a) q = gcd p q"
```
```   814     by (subst gcd_poly_decompose)
```
```   815        (auto simp: primitive_part_mult gcd_mult_unit1 primitive_part_prim
```
```   816              simp del: mult_pCons_right )
```
```   817   also from prim have "gcd q s = gcd q (primitive_part s)"
```
```   818     by (subst gcd_poly_decompose) (simp_all add: primitive_part_prim)
```
```   819   also have "s = pseudo_mod p q" by (simp add: s_def pseudo_mod_def)
```
```   820   finally show ?thesis .
```
```   821 qed
```
```   822
```
```   823 lemma degree_pseudo_mod_less:
```
```   824   assumes "q \<noteq> 0" "pseudo_mod p q \<noteq> 0"
```
```   825   shows   "degree (pseudo_mod p q) < degree q"
```
```   826   using pseudo_mod(2)[of q p] assms by auto
```
```   827
```
```   828 function gcd_poly_code_aux :: "'a :: factorial_ring_gcd poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
```
```   829   "gcd_poly_code_aux p q =
```
```   830      (if q = 0 then normalize p else gcd_poly_code_aux q (primitive_part (pseudo_mod p q)))"
```
```   831 by auto
```
```   832 termination
```
```   833   by (relation "measure ((\<lambda>p. if p = 0 then 0 else Suc (degree p)) \<circ> snd)")
```
```   834      (auto simp: degree_pseudo_mod_less)
```
```   835
```
```   836 declare gcd_poly_code_aux.simps [simp del]
```
```   837
```
```   838 lemma gcd_poly_code_aux_correct:
```
```   839   assumes "content p = 1" "q = 0 \<or> content q = 1"
```
```   840   shows   "gcd_poly_code_aux p q = gcd p q"
```
```   841   using assms
```
```   842 proof (induction p q rule: gcd_poly_code_aux.induct)
```
```   843   case (1 p q)
```
```   844   show ?case
```
```   845   proof (cases "q = 0")
```
```   846     case True
```
```   847     thus ?thesis by (subst gcd_poly_code_aux.simps) auto
```
```   848   next
```
```   849     case False
```
```   850     hence "gcd_poly_code_aux p q = gcd_poly_code_aux q (primitive_part (pseudo_mod p q))"
```
```   851       by (subst gcd_poly_code_aux.simps) simp_all
```
```   852     also from "1.prems" False
```
```   853       have "primitive_part (pseudo_mod p q) = 0 \<or>
```
```   854               content (primitive_part (pseudo_mod p q)) = 1"
```
```   855       by (cases "pseudo_mod p q = 0") auto
```
```   856     with "1.prems" False
```
```   857       have "gcd_poly_code_aux q (primitive_part (pseudo_mod p q)) =
```
```   858               gcd q (primitive_part (pseudo_mod p q))"
```
```   859       by (intro 1) simp_all
```
```   860     also from "1.prems" False
```
```   861       have "\<dots> = gcd p q" by (intro gcd_poly_pseudo_mod [symmetric]) auto
```
```   862     finally show ?thesis .
```
```   863   qed
```
```   864 qed
```
```   865
```
```   866 definition gcd_poly_code
```
```   867     :: "'a :: factorial_ring_gcd poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly"
```
```   868   where "gcd_poly_code p q =
```
```   869            (if p = 0 then normalize q else if q = 0 then normalize p else
```
```   870               smult (gcd (content p) (content q))
```
```   871                 (gcd_poly_code_aux (primitive_part p) (primitive_part q)))"
```
```   872
```
```   873 lemma gcd_poly_code [code]: "gcd p q = gcd_poly_code p q"
```
```   874   by (simp add: gcd_poly_code_def gcd_poly_code_aux_correct gcd_poly_decompose [symmetric])
```
```   875
```
```   876 lemma lcm_poly_code [code]:
```
```   877   fixes p q :: "'a :: factorial_ring_gcd poly"
```
```   878   shows "lcm p q = normalize (p * q) div gcd p q"
```
```   879   by (fact lcm_gcd)
```
```   880
```
```   881 lemmas Gcd_poly_set_eq_fold [code] = Gcd_set_eq_fold [where ?'a = "'a :: factorial_ring_gcd poly"]
```
```   882 lemmas Lcm_poly_set_eq_fold [code] = Lcm_set_eq_fold [where ?'a = "'a :: factorial_ring_gcd poly"]
```
```   883
```
```   884 text \<open>Example:
```
```   885   @{lemma "Lcm {[:1, 2, 3:], [:2, 3, 4:]} = [:[:2:], [:7:], [:16:], [:17:], [:12 :: int:]:]" by eval}
```
```   886 \<close>
```
```   887
```
```   888 end
```