src/HOL/Library/Polynomial_Factorial.thy
author haftmann
Wed Jan 04 21:28:29 2017 +0100 (2017-01-04)
changeset 64786 340db65fd2c1
parent 64784 5cb5e7ecb284
child 64794 6f7391f28197
permissions -rw-r--r--
reworked to provide auxiliary operations Euclidean_Algorithm.* to instantiate gcd etc. for euclidean rings
wenzelm@63764
     1
(*  Title:      HOL/Library/Polynomial_Factorial.thy
wenzelm@63764
     2
    Author:     Brian Huffman
wenzelm@63764
     3
    Author:     Clemens Ballarin
wenzelm@63764
     4
    Author:     Amine Chaieb
wenzelm@63764
     5
    Author:     Florian Haftmann
wenzelm@63764
     6
    Author:     Manuel Eberl
wenzelm@63764
     7
*)
wenzelm@63764
     8
eberlm@63498
     9
theory Polynomial_Factorial
eberlm@63498
    10
imports 
eberlm@63498
    11
  Complex_Main
eberlm@63498
    12
  "~~/src/HOL/Library/Polynomial"
eberlm@63500
    13
  "~~/src/HOL/Library/Normalized_Fraction"
haftmann@64591
    14
  "~~/src/HOL/Library/Field_as_Ring"
eberlm@63498
    15
begin
eberlm@63498
    16
haftmann@64591
    17
subsection \<open>Various facts about polynomials\<close>
eberlm@63498
    18
haftmann@64591
    19
lemma prod_mset_const_poly: "prod_mset (image_mset (\<lambda>x. [:f x:]) A) = [:prod_mset (image_mset f A):]"
haftmann@64591
    20
  by (induction A) (simp_all add: one_poly_def mult_ac)
eberlm@63498
    21
haftmann@64591
    22
lemma is_unit_smult_iff: "smult c p dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1"
haftmann@64591
    23
proof -
haftmann@64591
    24
  have "smult c p = [:c:] * p" by simp
haftmann@64591
    25
  also have "\<dots> dvd 1 \<longleftrightarrow> c dvd 1 \<and> p dvd 1"
haftmann@64591
    26
  proof safe
haftmann@64591
    27
    assume A: "[:c:] * p dvd 1"
haftmann@64591
    28
    thus "p dvd 1" by (rule dvd_mult_right)
haftmann@64591
    29
    from A obtain q where B: "1 = [:c:] * p * q" by (erule dvdE)
haftmann@64591
    30
    have "c dvd c * (coeff p 0 * coeff q 0)" by simp
haftmann@64591
    31
    also have "\<dots> = coeff ([:c:] * p * q) 0" by (simp add: mult.assoc coeff_mult)
haftmann@64591
    32
    also note B [symmetric]
haftmann@64591
    33
    finally show "c dvd 1" by simp
haftmann@64591
    34
  next
haftmann@64591
    35
    assume "c dvd 1" "p dvd 1"
haftmann@64591
    36
    from \<open>c dvd 1\<close> obtain d where "1 = c * d" by (erule dvdE)
haftmann@64591
    37
    hence "1 = [:c:] * [:d:]" by (simp add: one_poly_def mult_ac)
haftmann@64591
    38
    hence "[:c:] dvd 1" by (rule dvdI)
haftmann@64591
    39
    from mult_dvd_mono[OF this \<open>p dvd 1\<close>] show "[:c:] * p dvd 1" by simp
haftmann@64591
    40
  qed
haftmann@64591
    41
  finally show ?thesis .
haftmann@64591
    42
qed
eberlm@63498
    43
haftmann@64591
    44
lemma degree_mod_less': "b \<noteq> 0 \<Longrightarrow> a mod b \<noteq> 0 \<Longrightarrow> degree (a mod b) < degree b"
haftmann@64591
    45
  using degree_mod_less[of b a] by auto
haftmann@64591
    46
  
haftmann@64591
    47
lemma smult_eq_iff:
haftmann@64591
    48
  assumes "(b :: 'a :: field) \<noteq> 0"
haftmann@64591
    49
  shows   "smult a p = smult b q \<longleftrightarrow> smult (a / b) p = q"
haftmann@64591
    50
proof
haftmann@64591
    51
  assume "smult a p = smult b q"
haftmann@64591
    52
  also from assms have "smult (inverse b) \<dots> = q" by simp
haftmann@64591
    53
  finally show "smult (a / b) p = q" by (simp add: field_simps)
haftmann@64591
    54
qed (insert assms, auto)
eberlm@63498
    55
haftmann@64591
    56
lemma irreducible_const_poly_iff:
haftmann@64591
    57
  fixes c :: "'a :: {comm_semiring_1,semiring_no_zero_divisors}"
haftmann@64591
    58
  shows "irreducible [:c:] \<longleftrightarrow> irreducible c"
haftmann@64591
    59
proof
haftmann@64591
    60
  assume A: "irreducible c"
haftmann@64591
    61
  show "irreducible [:c:]"
haftmann@64591
    62
  proof (rule irreducibleI)
haftmann@64591
    63
    fix a b assume ab: "[:c:] = a * b"
haftmann@64591
    64
    hence "degree [:c:] = degree (a * b)" by (simp only: )
haftmann@64591
    65
    also from A ab have "a \<noteq> 0" "b \<noteq> 0" by auto
haftmann@64591
    66
    hence "degree (a * b) = degree a + degree b" by (simp add: degree_mult_eq)
haftmann@64591
    67
    finally have "degree a = 0" "degree b = 0" by auto
haftmann@64591
    68
    then obtain a' b' where ab': "a = [:a':]" "b = [:b':]" by (auto elim!: degree_eq_zeroE)
haftmann@64591
    69
    from ab have "coeff [:c:] 0 = coeff (a * b) 0" by (simp only: )
haftmann@64591
    70
    hence "c = a' * b'" by (simp add: ab' mult_ac)
haftmann@64591
    71
    from A and this have "a' dvd 1 \<or> b' dvd 1" by (rule irreducibleD)
haftmann@64591
    72
    with ab' show "a dvd 1 \<or> b dvd 1" by (auto simp: one_poly_def)
haftmann@64591
    73
  qed (insert A, auto simp: irreducible_def is_unit_poly_iff)
haftmann@64591
    74
next
haftmann@64591
    75
  assume A: "irreducible [:c:]"
haftmann@64591
    76
  show "irreducible c"
haftmann@64591
    77
  proof (rule irreducibleI)
haftmann@64591
    78
    fix a b assume ab: "c = a * b"
haftmann@64591
    79
    hence "[:c:] = [:a:] * [:b:]" by (simp add: mult_ac)
haftmann@64591
    80
    from A and this have "[:a:] dvd 1 \<or> [:b:] dvd 1" by (rule irreducibleD)
haftmann@64591
    81
    thus "a dvd 1 \<or> b dvd 1" by (simp add: one_poly_def)
haftmann@64591
    82
  qed (insert A, auto simp: irreducible_def one_poly_def)
haftmann@64591
    83
qed
eberlm@63498
    84
eberlm@63498
    85
eberlm@63498
    86
subsection \<open>Lifting elements into the field of fractions\<close>
eberlm@63498
    87
eberlm@63498
    88
definition to_fract :: "'a :: idom \<Rightarrow> 'a fract" where "to_fract x = Fract x 1"
haftmann@64591
    89
  -- \<open>FIXME: name \<open>of_idom\<close>, abbreviation\<close>
eberlm@63498
    90
eberlm@63498
    91
lemma to_fract_0 [simp]: "to_fract 0 = 0"
eberlm@63498
    92
  by (simp add: to_fract_def eq_fract Zero_fract_def)
eberlm@63498
    93
eberlm@63498
    94
lemma to_fract_1 [simp]: "to_fract 1 = 1"
eberlm@63498
    95
  by (simp add: to_fract_def eq_fract One_fract_def)
eberlm@63498
    96
eberlm@63498
    97
lemma to_fract_add [simp]: "to_fract (x + y) = to_fract x + to_fract y"
eberlm@63498
    98
  by (simp add: to_fract_def)
eberlm@63498
    99
eberlm@63498
   100
lemma to_fract_diff [simp]: "to_fract (x - y) = to_fract x - to_fract y"
eberlm@63498
   101
  by (simp add: to_fract_def)
eberlm@63498
   102
  
eberlm@63498
   103
lemma to_fract_uminus [simp]: "to_fract (-x) = -to_fract x"
eberlm@63498
   104
  by (simp add: to_fract_def)
eberlm@63498
   105
  
eberlm@63498
   106
lemma to_fract_mult [simp]: "to_fract (x * y) = to_fract x * to_fract y"
eberlm@63498
   107
  by (simp add: to_fract_def)
eberlm@63498
   108
eberlm@63498
   109
lemma to_fract_eq_iff [simp]: "to_fract x = to_fract y \<longleftrightarrow> x = y"
eberlm@63498
   110
  by (simp add: to_fract_def eq_fract)
eberlm@63498
   111
  
eberlm@63498
   112
lemma to_fract_eq_0_iff [simp]: "to_fract x = 0 \<longleftrightarrow> x = 0"
eberlm@63498
   113
  by (simp add: to_fract_def Zero_fract_def eq_fract)
eberlm@63498
   114
eberlm@63498
   115
lemma snd_quot_of_fract_nonzero [simp]: "snd (quot_of_fract x) \<noteq> 0"
eberlm@63498
   116
  by transfer simp
eberlm@63498
   117
eberlm@63498
   118
lemma Fract_quot_of_fract [simp]: "Fract (fst (quot_of_fract x)) (snd (quot_of_fract x)) = x"
eberlm@63498
   119
  by transfer (simp del: fractrel_iff, subst fractrel_normalize_quot_left, simp)
eberlm@63498
   120
eberlm@63498
   121
lemma to_fract_quot_of_fract:
eberlm@63498
   122
  assumes "snd (quot_of_fract x) = 1"
eberlm@63498
   123
  shows   "to_fract (fst (quot_of_fract x)) = x"
eberlm@63498
   124
proof -
eberlm@63498
   125
  have "x = Fract (fst (quot_of_fract x)) (snd (quot_of_fract x))" by simp
eberlm@63498
   126
  also note assms
eberlm@63498
   127
  finally show ?thesis by (simp add: to_fract_def)
eberlm@63498
   128
qed
eberlm@63498
   129
eberlm@63498
   130
lemma snd_quot_of_fract_Fract_whole:
eberlm@63498
   131
  assumes "y dvd x"
eberlm@63498
   132
  shows   "snd (quot_of_fract (Fract x y)) = 1"
eberlm@63498
   133
  using assms by transfer (auto simp: normalize_quot_def Let_def gcd_proj2_if_dvd)
eberlm@63498
   134
  
eberlm@63498
   135
lemma Fract_conv_to_fract: "Fract a b = to_fract a / to_fract b"
eberlm@63498
   136
  by (simp add: to_fract_def)
eberlm@63498
   137
eberlm@63498
   138
lemma quot_of_fract_to_fract [simp]: "quot_of_fract (to_fract x) = (x, 1)"
eberlm@63498
   139
  unfolding to_fract_def by transfer (simp add: normalize_quot_def)
eberlm@63498
   140
eberlm@63498
   141
lemma fst_quot_of_fract_eq_0_iff [simp]: "fst (quot_of_fract x) = 0 \<longleftrightarrow> x = 0"
eberlm@63498
   142
  by transfer simp
eberlm@63498
   143
 
eberlm@63498
   144
lemma snd_quot_of_fract_to_fract [simp]: "snd (quot_of_fract (to_fract x)) = 1"
eberlm@63498
   145
  unfolding to_fract_def by (rule snd_quot_of_fract_Fract_whole) simp_all
eberlm@63498
   146
eberlm@63498
   147
lemma coprime_quot_of_fract:
eberlm@63498
   148
  "coprime (fst (quot_of_fract x)) (snd (quot_of_fract x))"
eberlm@63498
   149
  by transfer (simp add: coprime_normalize_quot)
eberlm@63498
   150
eberlm@63498
   151
lemma unit_factor_snd_quot_of_fract: "unit_factor (snd (quot_of_fract x)) = 1"
eberlm@63498
   152
  using quot_of_fract_in_normalized_fracts[of x] 
eberlm@63498
   153
  by (simp add: normalized_fracts_def case_prod_unfold)  
eberlm@63498
   154
eberlm@63498
   155
lemma unit_factor_1_imp_normalized: "unit_factor x = 1 \<Longrightarrow> normalize x = x"
eberlm@63498
   156
  by (subst (2) normalize_mult_unit_factor [symmetric, of x])
eberlm@63498
   157
     (simp del: normalize_mult_unit_factor)
eberlm@63498
   158
  
eberlm@63498
   159
lemma normalize_snd_quot_of_fract: "normalize (snd (quot_of_fract x)) = snd (quot_of_fract x)"
eberlm@63498
   160
  by (intro unit_factor_1_imp_normalized unit_factor_snd_quot_of_fract)
eberlm@63498
   161
eberlm@63498
   162
eberlm@63498
   163
subsection \<open>Content and primitive part of a polynomial\<close>
eberlm@63498
   164
eberlm@63498
   165
definition content :: "('a :: semiring_Gcd poly) \<Rightarrow> 'a" where
eberlm@63498
   166
  "content p = Gcd (set (coeffs p))"
eberlm@63498
   167
eberlm@63498
   168
lemma content_0 [simp]: "content 0 = 0"
eberlm@63498
   169
  by (simp add: content_def)
eberlm@63498
   170
eberlm@63498
   171
lemma content_1 [simp]: "content 1 = 1"
eberlm@63498
   172
  by (simp add: content_def)
eberlm@63498
   173
eberlm@63498
   174
lemma content_const [simp]: "content [:c:] = normalize c"
eberlm@63498
   175
  by (simp add: content_def cCons_def)
eberlm@63498
   176
eberlm@63498
   177
lemma const_poly_dvd_iff_dvd_content:
eberlm@63498
   178
  fixes c :: "'a :: semiring_Gcd"
eberlm@63498
   179
  shows "[:c:] dvd p \<longleftrightarrow> c dvd content p"
eberlm@63498
   180
proof (cases "p = 0")
eberlm@63498
   181
  case [simp]: False
eberlm@63498
   182
  have "[:c:] dvd p \<longleftrightarrow> (\<forall>n. c dvd coeff p n)" by (rule const_poly_dvd_iff)
eberlm@63498
   183
  also have "\<dots> \<longleftrightarrow> (\<forall>a\<in>set (coeffs p). c dvd a)"
eberlm@63498
   184
  proof safe
eberlm@63498
   185
    fix n :: nat assume "\<forall>a\<in>set (coeffs p). c dvd a"
eberlm@63498
   186
    thus "c dvd coeff p n"
eberlm@63498
   187
      by (cases "n \<le> degree p") (auto simp: coeff_eq_0 coeffs_def split: if_splits)
eberlm@63498
   188
  qed (auto simp: coeffs_def simp del: upt_Suc split: if_splits)
eberlm@63498
   189
  also have "\<dots> \<longleftrightarrow> c dvd content p"
eberlm@63498
   190
    by (simp add: content_def dvd_Gcd_iff mult.commute [of "unit_factor x" for x]
eberlm@63498
   191
          dvd_mult_unit_iff lead_coeff_nonzero)
eberlm@63498
   192
  finally show ?thesis .
eberlm@63498
   193
qed simp_all
eberlm@63498
   194
eberlm@63498
   195
lemma content_dvd [simp]: "[:content p:] dvd p"
eberlm@63498
   196
  by (subst const_poly_dvd_iff_dvd_content) simp_all
eberlm@63498
   197
  
eberlm@63498
   198
lemma content_dvd_coeff [simp]: "content p dvd coeff p n"
eberlm@63498
   199
  by (cases "n \<le> degree p") 
eberlm@63498
   200
     (auto simp: content_def coeffs_def not_le coeff_eq_0 simp del: upt_Suc intro: Gcd_dvd)
eberlm@63498
   201
eberlm@63498
   202
lemma content_dvd_coeffs: "c \<in> set (coeffs p) \<Longrightarrow> content p dvd c"
eberlm@63498
   203
  by (simp add: content_def Gcd_dvd)
eberlm@63498
   204
eberlm@63498
   205
lemma normalize_content [simp]: "normalize (content p) = content p"
eberlm@63498
   206
  by (simp add: content_def)
eberlm@63498
   207
eberlm@63498
   208
lemma is_unit_content_iff [simp]: "is_unit (content p) \<longleftrightarrow> content p = 1"
eberlm@63498
   209
proof
eberlm@63498
   210
  assume "is_unit (content p)"
eberlm@63498
   211
  hence "normalize (content p) = 1" by (simp add: is_unit_normalize del: normalize_content)
eberlm@63498
   212
  thus "content p = 1" by simp
eberlm@63498
   213
qed auto
eberlm@63498
   214
eberlm@63498
   215
lemma content_smult [simp]: "content (smult c p) = normalize c * content p"
eberlm@63498
   216
  by (simp add: content_def coeffs_smult Gcd_mult)
eberlm@63498
   217
eberlm@63498
   218
lemma content_eq_zero_iff [simp]: "content p = 0 \<longleftrightarrow> p = 0"
eberlm@63498
   219
  by (auto simp: content_def simp: poly_eq_iff coeffs_def)
eberlm@63498
   220
eberlm@63498
   221
definition primitive_part :: "'a :: {semiring_Gcd,idom_divide} poly \<Rightarrow> 'a poly" where
eberlm@63498
   222
  "primitive_part p = (if p = 0 then 0 else map_poly (\<lambda>x. x div content p) p)"
eberlm@63498
   223
  
eberlm@63498
   224
lemma primitive_part_0 [simp]: "primitive_part 0 = 0"
eberlm@63498
   225
  by (simp add: primitive_part_def)
eberlm@63498
   226
eberlm@63498
   227
lemma content_times_primitive_part [simp]:
eberlm@63498
   228
  fixes p :: "'a :: {idom_divide, semiring_Gcd} poly"
eberlm@63498
   229
  shows "smult (content p) (primitive_part p) = p"
eberlm@63498
   230
proof (cases "p = 0")
eberlm@63498
   231
  case False
eberlm@63498
   232
  thus ?thesis
eberlm@63498
   233
  unfolding primitive_part_def
eberlm@63498
   234
  by (auto simp: smult_conv_map_poly map_poly_map_poly o_def content_dvd_coeffs 
eberlm@63498
   235
           intro: map_poly_idI)
eberlm@63498
   236
qed simp_all
eberlm@63498
   237
eberlm@63498
   238
lemma primitive_part_eq_0_iff [simp]: "primitive_part p = 0 \<longleftrightarrow> p = 0"
eberlm@63498
   239
proof (cases "p = 0")
eberlm@63498
   240
  case False
eberlm@63498
   241
  hence "primitive_part p = map_poly (\<lambda>x. x div content p) p"
eberlm@63498
   242
    by (simp add:  primitive_part_def)
eberlm@63498
   243
  also from False have "\<dots> = 0 \<longleftrightarrow> p = 0"
eberlm@63498
   244
    by (intro map_poly_eq_0_iff) (auto simp: dvd_div_eq_0_iff content_dvd_coeffs)
eberlm@63498
   245
  finally show ?thesis using False by simp
eberlm@63498
   246
qed simp
eberlm@63498
   247
eberlm@63498
   248
lemma content_primitive_part [simp]:
eberlm@63498
   249
  assumes "p \<noteq> 0"
eberlm@63498
   250
  shows   "content (primitive_part p) = 1"
eberlm@63498
   251
proof -
eberlm@63498
   252
  have "p = smult (content p) (primitive_part p)" by simp
eberlm@63498
   253
  also have "content \<dots> = content p * content (primitive_part p)" 
eberlm@63498
   254
    by (simp del: content_times_primitive_part)
eberlm@63498
   255
  finally show ?thesis using assms by simp
eberlm@63498
   256
qed
eberlm@63498
   257
eberlm@63498
   258
lemma content_decompose:
eberlm@63498
   259
  fixes p :: "'a :: semiring_Gcd poly"
eberlm@63498
   260
  obtains p' where "p = smult (content p) p'" "content p' = 1"
eberlm@63498
   261
proof (cases "p = 0")
eberlm@63498
   262
  case True
eberlm@63498
   263
  thus ?thesis by (intro that[of 1]) simp_all
eberlm@63498
   264
next
eberlm@63498
   265
  case False
eberlm@63498
   266
  from content_dvd[of p] obtain r where r: "p = [:content p:] * r" by (erule dvdE)
eberlm@63498
   267
  have "content p * 1 = content p * content r" by (subst r) simp
eberlm@63498
   268
  with False have "content r = 1" by (subst (asm) mult_left_cancel) simp_all
eberlm@63498
   269
  with r show ?thesis by (intro that[of r]) simp_all
eberlm@63498
   270
qed
eberlm@63498
   271
eberlm@63498
   272
lemma smult_content_normalize_primitive_part [simp]:
eberlm@63498
   273
  "smult (content p) (normalize (primitive_part p)) = normalize p"
eberlm@63498
   274
proof -
eberlm@63498
   275
  have "smult (content p) (normalize (primitive_part p)) = 
eberlm@63498
   276
          normalize ([:content p:] * primitive_part p)" 
eberlm@63498
   277
    by (subst normalize_mult) (simp_all add: normalize_const_poly)
eberlm@63498
   278
  also have "[:content p:] * primitive_part p = p" by simp
eberlm@63498
   279
  finally show ?thesis .
eberlm@63498
   280
qed
eberlm@63498
   281
eberlm@63498
   282
lemma content_dvd_contentI [intro]:
eberlm@63498
   283
  "p dvd q \<Longrightarrow> content p dvd content q"
eberlm@63498
   284
  using const_poly_dvd_iff_dvd_content content_dvd dvd_trans by blast
eberlm@63498
   285
  
eberlm@63498
   286
lemma primitive_part_const_poly [simp]: "primitive_part [:x:] = [:unit_factor x:]"
eberlm@63498
   287
  by (simp add: primitive_part_def map_poly_pCons)
eberlm@63498
   288
 
eberlm@63498
   289
lemma primitive_part_prim: "content p = 1 \<Longrightarrow> primitive_part p = p"
eberlm@63498
   290
  by (auto simp: primitive_part_def)
eberlm@63498
   291
  
eberlm@63498
   292
lemma degree_primitive_part [simp]: "degree (primitive_part p) = degree p"
eberlm@63498
   293
proof (cases "p = 0")
eberlm@63498
   294
  case False
eberlm@63498
   295
  have "p = smult (content p) (primitive_part p)" by simp
eberlm@63498
   296
  also from False have "degree \<dots> = degree (primitive_part p)"
eberlm@63498
   297
    by (subst degree_smult_eq) simp_all
eberlm@63498
   298
  finally show ?thesis ..
eberlm@63498
   299
qed simp_all
eberlm@63498
   300
eberlm@63498
   301
eberlm@63498
   302
subsection \<open>Lifting polynomial coefficients to the field of fractions\<close>
eberlm@63498
   303
eberlm@63498
   304
abbreviation (input) fract_poly 
eberlm@63498
   305
  where "fract_poly \<equiv> map_poly to_fract"
eberlm@63498
   306
eberlm@63498
   307
abbreviation (input) unfract_poly 
eberlm@63498
   308
  where "unfract_poly \<equiv> map_poly (fst \<circ> quot_of_fract)"
eberlm@63498
   309
  
eberlm@63498
   310
lemma fract_poly_smult [simp]: "fract_poly (smult c p) = smult (to_fract c) (fract_poly p)"
eberlm@63498
   311
  by (simp add: smult_conv_map_poly map_poly_map_poly o_def)
eberlm@63498
   312
eberlm@63498
   313
lemma fract_poly_0 [simp]: "fract_poly 0 = 0"
eberlm@63498
   314
  by (simp add: poly_eqI coeff_map_poly)
eberlm@63498
   315
eberlm@63498
   316
lemma fract_poly_1 [simp]: "fract_poly 1 = 1"
eberlm@63498
   317
  by (simp add: one_poly_def map_poly_pCons)
eberlm@63498
   318
eberlm@63498
   319
lemma fract_poly_add [simp]:
eberlm@63498
   320
  "fract_poly (p + q) = fract_poly p + fract_poly q"
eberlm@63498
   321
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
eberlm@63498
   322
eberlm@63498
   323
lemma fract_poly_diff [simp]:
eberlm@63498
   324
  "fract_poly (p - q) = fract_poly p - fract_poly q"
eberlm@63498
   325
  by (intro poly_eqI) (simp_all add: coeff_map_poly)
eberlm@63498
   326
nipkow@64267
   327
lemma to_fract_sum [simp]: "to_fract (sum f A) = sum (\<lambda>x. to_fract (f x)) A"
eberlm@63498
   328
  by (cases "finite A", induction A rule: finite_induct) simp_all 
eberlm@63498
   329
eberlm@63498
   330
lemma fract_poly_mult [simp]:
eberlm@63498
   331
  "fract_poly (p * q) = fract_poly p * fract_poly q"
eberlm@63498
   332
  by (intro poly_eqI) (simp_all add: coeff_map_poly coeff_mult)
eberlm@63498
   333
eberlm@63498
   334
lemma fract_poly_eq_iff [simp]: "fract_poly p = fract_poly q \<longleftrightarrow> p = q"
eberlm@63498
   335
  by (auto simp: poly_eq_iff coeff_map_poly)
eberlm@63498
   336
eberlm@63498
   337
lemma fract_poly_eq_0_iff [simp]: "fract_poly p = 0 \<longleftrightarrow> p = 0"
eberlm@63498
   338
  using fract_poly_eq_iff[of p 0] by (simp del: fract_poly_eq_iff)
eberlm@63498
   339
eberlm@63498
   340
lemma fract_poly_dvd: "p dvd q \<Longrightarrow> fract_poly p dvd fract_poly q"
eberlm@63498
   341
  by (auto elim!: dvdE)
eberlm@63498
   342
nipkow@63830
   343
lemma prod_mset_fract_poly: 
nipkow@63830
   344
  "prod_mset (image_mset (\<lambda>x. fract_poly (f x)) A) = fract_poly (prod_mset (image_mset f A))"
eberlm@63498
   345
  by (induction A) (simp_all add: mult_ac)
eberlm@63498
   346
  
eberlm@63498
   347
lemma is_unit_fract_poly_iff:
eberlm@63498
   348
  "p dvd 1 \<longleftrightarrow> fract_poly p dvd 1 \<and> content p = 1"
eberlm@63498
   349
proof safe
eberlm@63498
   350
  assume A: "p dvd 1"
eberlm@63498
   351
  with fract_poly_dvd[of p 1] show "is_unit (fract_poly p)" by simp
eberlm@63498
   352
  from A show "content p = 1"
eberlm@63498
   353
    by (auto simp: is_unit_poly_iff normalize_1_iff)
eberlm@63498
   354
next
eberlm@63498
   355
  assume A: "fract_poly p dvd 1" and B: "content p = 1"
eberlm@63498
   356
  from A obtain c where c: "fract_poly p = [:c:]" by (auto simp: is_unit_poly_iff)
eberlm@63498
   357
  {
eberlm@63498
   358
    fix n :: nat assume "n > 0"
eberlm@63498
   359
    have "to_fract (coeff p n) = coeff (fract_poly p) n" by (simp add: coeff_map_poly)
eberlm@63498
   360
    also note c
eberlm@63498
   361
    also from \<open>n > 0\<close> have "coeff [:c:] n = 0" by (simp add: coeff_pCons split: nat.splits)
eberlm@63498
   362
    finally have "coeff p n = 0" by simp
eberlm@63498
   363
  }
eberlm@63498
   364
  hence "degree p \<le> 0" by (intro degree_le) simp_all
eberlm@63498
   365
  with B show "p dvd 1" by (auto simp: is_unit_poly_iff normalize_1_iff elim!: degree_eq_zeroE)
eberlm@63498
   366
qed
eberlm@63498
   367
  
eberlm@63498
   368
lemma fract_poly_is_unit: "p dvd 1 \<Longrightarrow> fract_poly p dvd 1"
eberlm@63498
   369
  using fract_poly_dvd[of p 1] by simp
eberlm@63498
   370
eberlm@63498
   371
lemma fract_poly_smult_eqE:
eberlm@63498
   372
  fixes c :: "'a :: {idom_divide,ring_gcd} fract"
eberlm@63498
   373
  assumes "fract_poly p = smult c (fract_poly q)"
eberlm@63498
   374
  obtains a b 
eberlm@63498
   375
    where "c = to_fract b / to_fract a" "smult a p = smult b q" "coprime a b" "normalize a = a"
eberlm@63498
   376
proof -
eberlm@63498
   377
  define a b where "a = fst (quot_of_fract c)" and "b = snd (quot_of_fract c)"
eberlm@63498
   378
  have "smult (to_fract a) (fract_poly q) = smult (to_fract b) (fract_poly p)"
eberlm@63498
   379
    by (subst smult_eq_iff) (simp_all add: a_def b_def Fract_conv_to_fract [symmetric] assms)
eberlm@63498
   380
  hence "fract_poly (smult a q) = fract_poly (smult b p)" by (simp del: fract_poly_eq_iff)
eberlm@63498
   381
  hence "smult b p = smult a q" by (simp only: fract_poly_eq_iff)
eberlm@63498
   382
  moreover have "c = to_fract a / to_fract b" "coprime b a" "normalize b = b"
eberlm@63498
   383
    by (simp_all add: a_def b_def coprime_quot_of_fract gcd.commute
eberlm@63498
   384
          normalize_snd_quot_of_fract Fract_conv_to_fract [symmetric])
eberlm@63498
   385
  ultimately show ?thesis by (intro that[of a b])
eberlm@63498
   386
qed
eberlm@63498
   387
eberlm@63498
   388
eberlm@63498
   389
subsection \<open>Fractional content\<close>
eberlm@63498
   390
eberlm@63498
   391
abbreviation (input) Lcm_coeff_denoms 
eberlm@63498
   392
    :: "'a :: {semiring_Gcd,idom_divide,ring_gcd} fract poly \<Rightarrow> 'a"
eberlm@63498
   393
  where "Lcm_coeff_denoms p \<equiv> Lcm (snd ` quot_of_fract ` set (coeffs p))"
eberlm@63498
   394
  
eberlm@63498
   395
definition fract_content :: 
eberlm@63498
   396
      "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly \<Rightarrow> 'a fract" where
eberlm@63498
   397
  "fract_content p = 
eberlm@63498
   398
     (let d = Lcm_coeff_denoms p in Fract (content (unfract_poly (smult (to_fract d) p))) d)" 
eberlm@63498
   399
eberlm@63498
   400
definition primitive_part_fract :: 
eberlm@63498
   401
      "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly \<Rightarrow> 'a poly" where
eberlm@63498
   402
  "primitive_part_fract p = 
eberlm@63498
   403
     primitive_part (unfract_poly (smult (to_fract (Lcm_coeff_denoms p)) p))"
eberlm@63498
   404
eberlm@63498
   405
lemma primitive_part_fract_0 [simp]: "primitive_part_fract 0 = 0"
eberlm@63498
   406
  by (simp add: primitive_part_fract_def)
eberlm@63498
   407
eberlm@63498
   408
lemma fract_content_eq_0_iff [simp]:
eberlm@63498
   409
  "fract_content p = 0 \<longleftrightarrow> p = 0"
eberlm@63498
   410
  unfolding fract_content_def Let_def Zero_fract_def
eberlm@63498
   411
  by (subst eq_fract) (auto simp: Lcm_0_iff map_poly_eq_0_iff)
eberlm@63498
   412
eberlm@63498
   413
lemma content_primitive_part_fract [simp]: "p \<noteq> 0 \<Longrightarrow> content (primitive_part_fract p) = 1"
eberlm@63498
   414
  unfolding primitive_part_fract_def
eberlm@63498
   415
  by (rule content_primitive_part)
eberlm@63498
   416
     (auto simp: primitive_part_fract_def map_poly_eq_0_iff Lcm_0_iff)  
eberlm@63498
   417
eberlm@63498
   418
lemma content_times_primitive_part_fract:
eberlm@63498
   419
  "smult (fract_content p) (fract_poly (primitive_part_fract p)) = p"
eberlm@63498
   420
proof -
eberlm@63498
   421
  define p' where "p' = unfract_poly (smult (to_fract (Lcm_coeff_denoms p)) p)"
eberlm@63498
   422
  have "fract_poly p' = 
eberlm@63498
   423
          map_poly (to_fract \<circ> fst \<circ> quot_of_fract) (smult (to_fract (Lcm_coeff_denoms p)) p)"
eberlm@63498
   424
    unfolding primitive_part_fract_def p'_def 
eberlm@63498
   425
    by (subst map_poly_map_poly) (simp_all add: o_assoc)
eberlm@63498
   426
  also have "\<dots> = smult (to_fract (Lcm_coeff_denoms p)) p"
eberlm@63498
   427
  proof (intro map_poly_idI, unfold o_apply)
eberlm@63498
   428
    fix c assume "c \<in> set (coeffs (smult (to_fract (Lcm_coeff_denoms p)) p))"
eberlm@63498
   429
    then obtain c' where c: "c' \<in> set (coeffs p)" "c = to_fract (Lcm_coeff_denoms p) * c'"
eberlm@63498
   430
      by (auto simp add: Lcm_0_iff coeffs_smult split: if_splits)
eberlm@63498
   431
    note c(2)
eberlm@63498
   432
    also have "c' = Fract (fst (quot_of_fract c')) (snd (quot_of_fract c'))"
eberlm@63498
   433
      by simp
eberlm@63498
   434
    also have "to_fract (Lcm_coeff_denoms p) * \<dots> = 
eberlm@63498
   435
                 Fract (Lcm_coeff_denoms p * fst (quot_of_fract c')) (snd (quot_of_fract c'))"
eberlm@63498
   436
      unfolding to_fract_def by (subst mult_fract) simp_all
eberlm@63498
   437
    also have "snd (quot_of_fract \<dots>) = 1"
eberlm@63498
   438
      by (intro snd_quot_of_fract_Fract_whole dvd_mult2 dvd_Lcm) (insert c(1), auto)
eberlm@63498
   439
    finally show "to_fract (fst (quot_of_fract c)) = c"
eberlm@63498
   440
      by (rule to_fract_quot_of_fract)
eberlm@63498
   441
  qed
eberlm@63498
   442
  also have "p' = smult (content p') (primitive_part p')" 
eberlm@63498
   443
    by (rule content_times_primitive_part [symmetric])
eberlm@63498
   444
  also have "primitive_part p' = primitive_part_fract p"
eberlm@63498
   445
    by (simp add: primitive_part_fract_def p'_def)
eberlm@63498
   446
  also have "fract_poly (smult (content p') (primitive_part_fract p)) = 
eberlm@63498
   447
               smult (to_fract (content p')) (fract_poly (primitive_part_fract p))" by simp
eberlm@63498
   448
  finally have "smult (to_fract (content p')) (fract_poly (primitive_part_fract p)) =
eberlm@63498
   449
                      smult (to_fract (Lcm_coeff_denoms p)) p" .
eberlm@63498
   450
  thus ?thesis
eberlm@63498
   451
    by (subst (asm) smult_eq_iff)
eberlm@63498
   452
       (auto simp add: Let_def p'_def Fract_conv_to_fract field_simps Lcm_0_iff fract_content_def)
eberlm@63498
   453
qed
eberlm@63498
   454
eberlm@63498
   455
lemma fract_content_fract_poly [simp]: "fract_content (fract_poly p) = to_fract (content p)"
eberlm@63498
   456
proof -
eberlm@63498
   457
  have "Lcm_coeff_denoms (fract_poly p) = 1"
haftmann@63905
   458
    by (auto simp: set_coeffs_map_poly)
eberlm@63498
   459
  hence "fract_content (fract_poly p) = 
eberlm@63498
   460
           to_fract (content (map_poly (fst \<circ> quot_of_fract \<circ> to_fract) p))"
eberlm@63498
   461
    by (simp add: fract_content_def to_fract_def fract_collapse map_poly_map_poly del: Lcm_1_iff)
eberlm@63498
   462
  also have "map_poly (fst \<circ> quot_of_fract \<circ> to_fract) p = p"
eberlm@63498
   463
    by (intro map_poly_idI) simp_all
eberlm@63498
   464
  finally show ?thesis .
eberlm@63498
   465
qed
eberlm@63498
   466
eberlm@63498
   467
lemma content_decompose_fract:
eberlm@63498
   468
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} fract poly"
eberlm@63498
   469
  obtains c p' where "p = smult c (map_poly to_fract p')" "content p' = 1"
eberlm@63498
   470
proof (cases "p = 0")
eberlm@63498
   471
  case True
eberlm@63498
   472
  hence "p = smult 0 (map_poly to_fract 1)" "content 1 = 1" by simp_all
eberlm@63498
   473
  thus ?thesis ..
eberlm@63498
   474
next
eberlm@63498
   475
  case False
eberlm@63498
   476
  thus ?thesis
eberlm@63498
   477
    by (rule that[OF content_times_primitive_part_fract [symmetric] content_primitive_part_fract])
eberlm@63498
   478
qed
eberlm@63498
   479
eberlm@63498
   480
eberlm@63498
   481
subsection \<open>More properties of content and primitive part\<close>
eberlm@63498
   482
eberlm@63498
   483
lemma lift_prime_elem_poly:
eberlm@63633
   484
  assumes "prime_elem (c :: 'a :: semidom)"
eberlm@63633
   485
  shows   "prime_elem [:c:]"
eberlm@63633
   486
proof (rule prime_elemI)
eberlm@63498
   487
  fix a b assume *: "[:c:] dvd a * b"
eberlm@63498
   488
  from * have dvd: "c dvd coeff (a * b) n" for n
eberlm@63498
   489
    by (subst (asm) const_poly_dvd_iff) blast
eberlm@63498
   490
  {
eberlm@63498
   491
    define m where "m = (GREATEST m. \<not>c dvd coeff b m)"
eberlm@63498
   492
    assume "\<not>[:c:] dvd b"
eberlm@63498
   493
    hence A: "\<exists>i. \<not>c dvd coeff b i" by (subst (asm) const_poly_dvd_iff) blast
eberlm@63498
   494
    have B: "\<forall>i. \<not>c dvd coeff b i \<longrightarrow> i < Suc (degree b)"
eberlm@63498
   495
      by (auto intro: le_degree simp: less_Suc_eq_le)
eberlm@63498
   496
    have coeff_m: "\<not>c dvd coeff b m" unfolding m_def by (rule GreatestI_ex[OF A B])
eberlm@63498
   497
    have "i \<le> m" if "\<not>c dvd coeff b i" for i
eberlm@63498
   498
      unfolding m_def by (rule Greatest_le[OF that B])
eberlm@63498
   499
    hence dvd_b: "c dvd coeff b i" if "i > m" for i using that by force
eberlm@63498
   500
eberlm@63498
   501
    have "c dvd coeff a i" for i
eberlm@63498
   502
    proof (induction i rule: nat_descend_induct[of "degree a"])
eberlm@63498
   503
      case (base i)
eberlm@63498
   504
      thus ?case by (simp add: coeff_eq_0)
eberlm@63498
   505
    next
eberlm@63498
   506
      case (descend i)
eberlm@63498
   507
      let ?A = "{..i+m} - {i}"
eberlm@63498
   508
      have "c dvd coeff (a * b) (i + m)" by (rule dvd)
eberlm@63498
   509
      also have "coeff (a * b) (i + m) = (\<Sum>k\<le>i + m. coeff a k * coeff b (i + m - k))"
eberlm@63498
   510
        by (simp add: coeff_mult)
eberlm@63498
   511
      also have "{..i+m} = insert i ?A" by auto
eberlm@63498
   512
      also have "(\<Sum>k\<in>\<dots>. coeff a k * coeff b (i + m - k)) =
eberlm@63498
   513
                   coeff a i * coeff b m + (\<Sum>k\<in>?A. coeff a k * coeff b (i + m - k))"
eberlm@63498
   514
        (is "_ = _ + ?S")
nipkow@64267
   515
        by (subst sum.insert) simp_all
eberlm@63498
   516
      finally have eq: "c dvd coeff a i * coeff b m + ?S" .
eberlm@63498
   517
      moreover have "c dvd ?S"
nipkow@64267
   518
      proof (rule dvd_sum)
eberlm@63498
   519
        fix k assume k: "k \<in> {..i+m} - {i}"
eberlm@63498
   520
        show "c dvd coeff a k * coeff b (i + m - k)"
eberlm@63498
   521
        proof (cases "k < i")
eberlm@63498
   522
          case False
eberlm@63498
   523
          with k have "c dvd coeff a k" by (intro descend.IH) simp
eberlm@63498
   524
          thus ?thesis by simp
eberlm@63498
   525
        next
eberlm@63498
   526
          case True
eberlm@63498
   527
          hence "c dvd coeff b (i + m - k)" by (intro dvd_b) simp
eberlm@63498
   528
          thus ?thesis by simp
eberlm@63498
   529
        qed
eberlm@63498
   530
      qed
eberlm@63498
   531
      ultimately have "c dvd coeff a i * coeff b m"
eberlm@63498
   532
        by (simp add: dvd_add_left_iff)
eberlm@63498
   533
      with assms coeff_m show "c dvd coeff a i"
eberlm@63633
   534
        by (simp add: prime_elem_dvd_mult_iff)
eberlm@63498
   535
    qed
eberlm@63498
   536
    hence "[:c:] dvd a" by (subst const_poly_dvd_iff) blast
eberlm@63498
   537
  }
eberlm@63498
   538
  thus "[:c:] dvd a \<or> [:c:] dvd b" by blast
eberlm@63633
   539
qed (insert assms, simp_all add: prime_elem_def one_poly_def)
eberlm@63498
   540
eberlm@63498
   541
lemma prime_elem_const_poly_iff:
eberlm@63498
   542
  fixes c :: "'a :: semidom"
eberlm@63633
   543
  shows   "prime_elem [:c:] \<longleftrightarrow> prime_elem c"
eberlm@63498
   544
proof
eberlm@63633
   545
  assume A: "prime_elem [:c:]"
eberlm@63633
   546
  show "prime_elem c"
eberlm@63633
   547
  proof (rule prime_elemI)
eberlm@63498
   548
    fix a b assume "c dvd a * b"
eberlm@63498
   549
    hence "[:c:] dvd [:a:] * [:b:]" by (simp add: mult_ac)
eberlm@63633
   550
    from A and this have "[:c:] dvd [:a:] \<or> [:c:] dvd [:b:]" by (rule prime_elem_dvd_multD)
eberlm@63498
   551
    thus "c dvd a \<or> c dvd b" by simp
eberlm@63633
   552
  qed (insert A, auto simp: prime_elem_def is_unit_poly_iff)
eberlm@63498
   553
qed (auto intro: lift_prime_elem_poly)
eberlm@63498
   554
eberlm@63498
   555
context
eberlm@63498
   556
begin
eberlm@63498
   557
eberlm@63498
   558
private lemma content_1_mult:
eberlm@63498
   559
  fixes f g :: "'a :: {semiring_Gcd,factorial_semiring} poly"
eberlm@63498
   560
  assumes "content f = 1" "content g = 1"
eberlm@63498
   561
  shows   "content (f * g) = 1"
eberlm@63498
   562
proof (cases "f * g = 0")
eberlm@63498
   563
  case False
eberlm@63498
   564
  from assms have "f \<noteq> 0" "g \<noteq> 0" by auto
eberlm@63498
   565
eberlm@63498
   566
  hence "f * g \<noteq> 0" by auto
eberlm@63498
   567
  {
eberlm@63498
   568
    assume "\<not>is_unit (content (f * g))"
eberlm@63633
   569
    with False have "\<exists>p. p dvd content (f * g) \<and> prime p"
eberlm@63498
   570
      by (intro prime_divisor_exists) simp_all
eberlm@63633
   571
    then obtain p where "p dvd content (f * g)" "prime p" by blast
eberlm@63498
   572
    from \<open>p dvd content (f * g)\<close> have "[:p:] dvd f * g"
eberlm@63498
   573
      by (simp add: const_poly_dvd_iff_dvd_content)
eberlm@63633
   574
    moreover from \<open>prime p\<close> have "prime_elem [:p:]" by (simp add: lift_prime_elem_poly)
eberlm@63498
   575
    ultimately have "[:p:] dvd f \<or> [:p:] dvd g"
eberlm@63633
   576
      by (simp add: prime_elem_dvd_mult_iff)
eberlm@63498
   577
    with assms have "is_unit p" by (simp add: const_poly_dvd_iff_dvd_content)
eberlm@63633
   578
    with \<open>prime p\<close> have False by simp
eberlm@63498
   579
  }
eberlm@63498
   580
  hence "is_unit (content (f * g))" by blast
eberlm@63498
   581
  hence "normalize (content (f * g)) = 1" by (simp add: is_unit_normalize del: normalize_content)
eberlm@63498
   582
  thus ?thesis by simp
eberlm@63498
   583
qed (insert assms, auto)
eberlm@63498
   584
eberlm@63498
   585
lemma content_mult:
eberlm@63498
   586
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd} poly"
eberlm@63498
   587
  shows "content (p * q) = content p * content q"
eberlm@63498
   588
proof -
eberlm@63498
   589
  from content_decompose[of p] guess p' . note p = this
eberlm@63498
   590
  from content_decompose[of q] guess q' . note q = this
eberlm@63498
   591
  have "content (p * q) = content p * content q * content (p' * q')"
eberlm@63498
   592
    by (subst p, subst q) (simp add: mult_ac normalize_mult)
eberlm@63498
   593
  also from p q have "content (p' * q') = 1" by (intro content_1_mult)
eberlm@63498
   594
  finally show ?thesis by simp
eberlm@63498
   595
qed
eberlm@63498
   596
eberlm@63498
   597
lemma primitive_part_mult:
eberlm@63498
   598
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
eberlm@63498
   599
  shows "primitive_part (p * q) = primitive_part p * primitive_part q"
eberlm@63498
   600
proof -
eberlm@63498
   601
  have "primitive_part (p * q) = p * q div [:content (p * q):]"
eberlm@63498
   602
    by (simp add: primitive_part_def div_const_poly_conv_map_poly)
eberlm@63498
   603
  also have "\<dots> = (p div [:content p:]) * (q div [:content q:])"
eberlm@63498
   604
    by (subst div_mult_div_if_dvd) (simp_all add: content_mult mult_ac)
eberlm@63498
   605
  also have "\<dots> = primitive_part p * primitive_part q"
eberlm@63498
   606
    by (simp add: primitive_part_def div_const_poly_conv_map_poly)
eberlm@63498
   607
  finally show ?thesis .
eberlm@63498
   608
qed
eberlm@63498
   609
eberlm@63498
   610
lemma primitive_part_smult:
eberlm@63498
   611
  fixes p :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
eberlm@63498
   612
  shows "primitive_part (smult a p) = smult (unit_factor a) (primitive_part p)"
eberlm@63498
   613
proof -
eberlm@63498
   614
  have "smult a p = [:a:] * p" by simp
eberlm@63498
   615
  also have "primitive_part \<dots> = smult (unit_factor a) (primitive_part p)"
eberlm@63498
   616
    by (subst primitive_part_mult) simp_all
eberlm@63498
   617
  finally show ?thesis .
eberlm@63498
   618
qed  
eberlm@63498
   619
eberlm@63498
   620
lemma primitive_part_dvd_primitive_partI [intro]:
eberlm@63498
   621
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd, ring_gcd, idom_divide} poly"
eberlm@63498
   622
  shows "p dvd q \<Longrightarrow> primitive_part p dvd primitive_part q"
eberlm@63498
   623
  by (auto elim!: dvdE simp: primitive_part_mult)
eberlm@63498
   624
nipkow@63830
   625
lemma content_prod_mset: 
eberlm@63498
   626
  fixes A :: "'a :: {factorial_semiring, semiring_Gcd} poly multiset"
nipkow@63830
   627
  shows "content (prod_mset A) = prod_mset (image_mset content A)"
eberlm@63498
   628
  by (induction A) (simp_all add: content_mult mult_ac)
eberlm@63498
   629
eberlm@63498
   630
lemma fract_poly_dvdD:
eberlm@63498
   631
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
eberlm@63498
   632
  assumes "fract_poly p dvd fract_poly q" "content p = 1"
eberlm@63498
   633
  shows   "p dvd q"
eberlm@63498
   634
proof -
eberlm@63498
   635
  from assms(1) obtain r where r: "fract_poly q = fract_poly p * r" by (erule dvdE)
eberlm@63498
   636
  from content_decompose_fract[of r] guess c r' . note r' = this
eberlm@63498
   637
  from r r' have eq: "fract_poly q = smult c (fract_poly (p * r'))" by simp  
eberlm@63498
   638
  from fract_poly_smult_eqE[OF this] guess a b . note ab = this
eberlm@63498
   639
  have "content (smult a q) = content (smult b (p * r'))" by (simp only: ab(2))
eberlm@63498
   640
  hence eq': "normalize b = a * content q" by (simp add: assms content_mult r' ab(4))
eberlm@63498
   641
  have "1 = gcd a (normalize b)" by (simp add: ab)
eberlm@63498
   642
  also note eq'
eberlm@63498
   643
  also have "gcd a (a * content q) = a" by (simp add: gcd_proj1_if_dvd ab(4))
eberlm@63498
   644
  finally have [simp]: "a = 1" by simp
eberlm@63498
   645
  from eq ab have "q = p * ([:b:] * r')" by simp
eberlm@63498
   646
  thus ?thesis by (rule dvdI)
eberlm@63498
   647
qed
eberlm@63498
   648
eberlm@63498
   649
lemma content_prod_eq_1_iff: 
eberlm@63498
   650
  fixes p q :: "'a :: {factorial_semiring, semiring_Gcd} poly"
eberlm@63498
   651
  shows "content (p * q) = 1 \<longleftrightarrow> content p = 1 \<and> content q = 1"
eberlm@63498
   652
proof safe
eberlm@63498
   653
  assume A: "content (p * q) = 1"
eberlm@63498
   654
  {
eberlm@63498
   655
    fix p q :: "'a poly" assume "content p * content q = 1"
eberlm@63498
   656
    hence "1 = content p * content q" by simp
eberlm@63498
   657
    hence "content p dvd 1" by (rule dvdI)
eberlm@63498
   658
    hence "content p = 1" by simp
eberlm@63498
   659
  } note B = this
eberlm@63498
   660
  from A B[of p q] B [of q p] show "content p = 1" "content q = 1" 
eberlm@63498
   661
    by (simp_all add: content_mult mult_ac)
eberlm@63498
   662
qed (auto simp: content_mult)
eberlm@63498
   663
eberlm@63498
   664
end
eberlm@63498
   665
eberlm@63498
   666
eberlm@63498
   667
subsection \<open>Polynomials over a field are a Euclidean ring\<close>
eberlm@63498
   668
eberlm@63722
   669
definition unit_factor_field_poly :: "'a :: field poly \<Rightarrow> 'a poly" where
eberlm@63498
   670
  "unit_factor_field_poly p = [:lead_coeff p:]"
eberlm@63498
   671
eberlm@63722
   672
definition normalize_field_poly :: "'a :: field poly \<Rightarrow> 'a poly" where
eberlm@63498
   673
  "normalize_field_poly p = smult (inverse (lead_coeff p)) p"
eberlm@63498
   674
eberlm@63722
   675
definition euclidean_size_field_poly :: "'a :: field poly \<Rightarrow> nat" where
eberlm@63498
   676
  "euclidean_size_field_poly p = (if p = 0 then 0 else 2 ^ degree p)" 
eberlm@63498
   677
eberlm@63722
   678
lemma dvd_field_poly: "dvd.dvd (op * :: 'a :: field poly \<Rightarrow> _) = op dvd"
haftmann@64784
   679
  by (intro ext) (simp_all add: dvd.dvd_def dvd_def)
eberlm@63498
   680
eberlm@63498
   681
interpretation field_poly: 
haftmann@64784
   682
  unique_euclidean_ring where zero = "0 :: 'a :: field poly"
haftmann@64164
   683
    and one = 1 and plus = plus and uminus = uminus and minus = minus
haftmann@64164
   684
    and times = times
haftmann@64164
   685
    and normalize = normalize_field_poly and unit_factor = unit_factor_field_poly
haftmann@64164
   686
    and euclidean_size = euclidean_size_field_poly
haftmann@64784
   687
    and uniqueness_constraint = top
haftmann@64164
   688
    and divide = divide and modulo = modulo
eberlm@63498
   689
proof (standard, unfold dvd_field_poly)
eberlm@63498
   690
  fix p :: "'a poly"
eberlm@63498
   691
  show "unit_factor_field_poly p * normalize_field_poly p = p"
eberlm@63498
   692
    by (cases "p = 0") 
eberlm@63498
   693
       (simp_all add: unit_factor_field_poly_def normalize_field_poly_def lead_coeff_nonzero)
eberlm@63498
   694
next
eberlm@63498
   695
  fix p :: "'a poly" assume "is_unit p"
eberlm@63498
   696
  thus "normalize_field_poly p = 1"
eberlm@63498
   697
    by (elim is_unit_polyE) (auto simp: normalize_field_poly_def monom_0 one_poly_def field_simps)
eberlm@63498
   698
next
eberlm@63498
   699
  fix p :: "'a poly" assume "p \<noteq> 0"
eberlm@63498
   700
  thus "is_unit (unit_factor_field_poly p)"
eberlm@63498
   701
    by (simp add: unit_factor_field_poly_def lead_coeff_nonzero is_unit_pCons_iff)
haftmann@64784
   702
next
haftmann@64784
   703
  fix p q s :: "'a poly" assume "s \<noteq> 0"
haftmann@64784
   704
  moreover assume "euclidean_size_field_poly p < euclidean_size_field_poly q"
haftmann@64784
   705
  ultimately show "euclidean_size_field_poly (p * s) < euclidean_size_field_poly (q * s)"
haftmann@64784
   706
    by (auto simp add: euclidean_size_field_poly_def degree_mult_eq)
haftmann@64784
   707
next
haftmann@64784
   708
  fix p q r :: "'a poly" assume "p \<noteq> 0"
haftmann@64784
   709
  moreover assume "euclidean_size_field_poly r < euclidean_size_field_poly p"
haftmann@64784
   710
  ultimately show "(q * p + r) div p = q"
haftmann@64784
   711
    by (cases "r = 0")
haftmann@64784
   712
      (auto simp add: unit_factor_field_poly_def euclidean_size_field_poly_def div_poly_less)
eberlm@63498
   713
qed (auto simp: unit_factor_field_poly_def normalize_field_poly_def lead_coeff_mult 
haftmann@64242
   714
       euclidean_size_field_poly_def Rings.div_mult_mod_eq intro!: degree_mod_less' degree_mult_right_le)
eberlm@63498
   715
eberlm@63722
   716
lemma field_poly_irreducible_imp_prime:
eberlm@63498
   717
  assumes "irreducible (p :: 'a :: field poly)"
eberlm@63633
   718
  shows   "prime_elem p"
eberlm@63498
   719
proof -
eberlm@63498
   720
  have A: "class.comm_semiring_1 op * 1 op + (0 :: 'a poly)" ..
eberlm@63633
   721
  from field_poly.irreducible_imp_prime_elem[of p] assms
eberlm@63633
   722
    show ?thesis unfolding irreducible_def prime_elem_def dvd_field_poly
eberlm@63633
   723
      comm_semiring_1.irreducible_def[OF A] comm_semiring_1.prime_elem_def[OF A] by blast
eberlm@63498
   724
qed
eberlm@63498
   725
nipkow@63830
   726
lemma field_poly_prod_mset_prime_factorization:
eberlm@63498
   727
  assumes "(x :: 'a :: field poly) \<noteq> 0"
nipkow@63830
   728
  shows   "prod_mset (field_poly.prime_factorization x) = normalize_field_poly x"
eberlm@63498
   729
proof -
eberlm@63498
   730
  have A: "class.comm_monoid_mult op * (1 :: 'a poly)" ..
nipkow@63830
   731
  have "comm_monoid_mult.prod_mset op * (1 :: 'a poly) = prod_mset"
nipkow@63830
   732
    by (intro ext) (simp add: comm_monoid_mult.prod_mset_def[OF A] prod_mset_def)
nipkow@63830
   733
  with field_poly.prod_mset_prime_factorization[OF assms] show ?thesis by simp
eberlm@63498
   734
qed
eberlm@63498
   735
eberlm@63722
   736
lemma field_poly_in_prime_factorization_imp_prime:
eberlm@63498
   737
  assumes "(p :: 'a :: field poly) \<in># field_poly.prime_factorization x"
eberlm@63633
   738
  shows   "prime_elem p"
eberlm@63498
   739
proof -
eberlm@63498
   740
  have A: "class.comm_semiring_1 op * 1 op + (0 :: 'a poly)" ..
eberlm@63498
   741
  have B: "class.normalization_semidom op div op + op - (0 :: 'a poly) op * 1 
eberlm@63498
   742
             normalize_field_poly unit_factor_field_poly" ..
haftmann@63905
   743
  from field_poly.in_prime_factors_imp_prime [of p x] assms
eberlm@63633
   744
    show ?thesis unfolding prime_elem_def dvd_field_poly
eberlm@63633
   745
      comm_semiring_1.prime_elem_def[OF A] normalization_semidom.prime_def[OF B] by blast
eberlm@63498
   746
qed
eberlm@63498
   747
eberlm@63498
   748
eberlm@63498
   749
subsection \<open>Primality and irreducibility in polynomial rings\<close>
eberlm@63498
   750
eberlm@63498
   751
lemma nonconst_poly_irreducible_iff:
eberlm@63498
   752
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
eberlm@63498
   753
  assumes "degree p \<noteq> 0"
eberlm@63498
   754
  shows   "irreducible p \<longleftrightarrow> irreducible (fract_poly p) \<and> content p = 1"
eberlm@63498
   755
proof safe
eberlm@63498
   756
  assume p: "irreducible p"
eberlm@63498
   757
eberlm@63498
   758
  from content_decompose[of p] guess p' . note p' = this
eberlm@63498
   759
  hence "p = [:content p:] * p'" by simp
eberlm@63498
   760
  from p this have "[:content p:] dvd 1 \<or> p' dvd 1" by (rule irreducibleD)
eberlm@63498
   761
  moreover have "\<not>p' dvd 1"
eberlm@63498
   762
  proof
eberlm@63498
   763
    assume "p' dvd 1"
eberlm@63498
   764
    hence "degree p = 0" by (subst p') (auto simp: is_unit_poly_iff)
eberlm@63498
   765
    with assms show False by contradiction
eberlm@63498
   766
  qed
eberlm@63498
   767
  ultimately show [simp]: "content p = 1" by (simp add: is_unit_const_poly_iff)
eberlm@63498
   768
  
eberlm@63498
   769
  show "irreducible (map_poly to_fract p)"
eberlm@63498
   770
  proof (rule irreducibleI)
eberlm@63498
   771
    have "fract_poly p = 0 \<longleftrightarrow> p = 0" by (intro map_poly_eq_0_iff) auto
eberlm@63498
   772
    with assms show "map_poly to_fract p \<noteq> 0" by auto
eberlm@63498
   773
  next
eberlm@63498
   774
    show "\<not>is_unit (fract_poly p)"
eberlm@63498
   775
    proof
eberlm@63498
   776
      assume "is_unit (map_poly to_fract p)"
eberlm@63498
   777
      hence "degree (map_poly to_fract p) = 0"
eberlm@63498
   778
        by (auto simp: is_unit_poly_iff)
eberlm@63498
   779
      hence "degree p = 0" by (simp add: degree_map_poly)
eberlm@63498
   780
      with assms show False by contradiction
eberlm@63498
   781
   qed
eberlm@63498
   782
 next
eberlm@63498
   783
   fix q r assume qr: "fract_poly p = q * r"
eberlm@63498
   784
   from content_decompose_fract[of q] guess cg q' . note q = this
eberlm@63498
   785
   from content_decompose_fract[of r] guess cr r' . note r = this
eberlm@63498
   786
   from qr q r p have nz: "cg \<noteq> 0" "cr \<noteq> 0" by auto
eberlm@63498
   787
   from qr have eq: "fract_poly p = smult (cr * cg) (fract_poly (q' * r'))"
eberlm@63498
   788
     by (simp add: q r)
eberlm@63498
   789
   from fract_poly_smult_eqE[OF this] guess a b . note ab = this
eberlm@63498
   790
   hence "content (smult a p) = content (smult b (q' * r'))" by (simp only:)
eberlm@63498
   791
   with ab(4) have a: "a = normalize b" by (simp add: content_mult q r)
eberlm@63498
   792
   hence "normalize b = gcd a b" by simp
eberlm@63498
   793
   also from ab(3) have "\<dots> = 1" .
eberlm@63498
   794
   finally have "a = 1" "is_unit b" by (simp_all add: a normalize_1_iff)
eberlm@63498
   795
   
eberlm@63498
   796
   note eq
eberlm@63498
   797
   also from ab(1) \<open>a = 1\<close> have "cr * cg = to_fract b" by simp
eberlm@63498
   798
   also have "smult \<dots> (fract_poly (q' * r')) = fract_poly (smult b (q' * r'))" by simp
eberlm@63498
   799
   finally have "p = ([:b:] * q') * r'" by (simp del: fract_poly_smult)
eberlm@63498
   800
   from p and this have "([:b:] * q') dvd 1 \<or> r' dvd 1" by (rule irreducibleD)
eberlm@63498
   801
   hence "q' dvd 1 \<or> r' dvd 1" by (auto dest: dvd_mult_right simp del: mult_pCons_left)
eberlm@63498
   802
   hence "fract_poly q' dvd 1 \<or> fract_poly r' dvd 1" by (auto simp: fract_poly_is_unit)
eberlm@63498
   803
   with q r show "is_unit q \<or> is_unit r"
eberlm@63498
   804
     by (auto simp add: is_unit_smult_iff dvd_field_iff nz)
eberlm@63498
   805
 qed
eberlm@63498
   806
eberlm@63498
   807
next
eberlm@63498
   808
eberlm@63498
   809
  assume irred: "irreducible (fract_poly p)" and primitive: "content p = 1"
eberlm@63498
   810
  show "irreducible p"
eberlm@63498
   811
  proof (rule irreducibleI)
eberlm@63498
   812
    from irred show "p \<noteq> 0" by auto
eberlm@63498
   813
  next
eberlm@63498
   814
    from irred show "\<not>p dvd 1"
eberlm@63498
   815
      by (auto simp: irreducible_def dest: fract_poly_is_unit)
eberlm@63498
   816
  next
eberlm@63498
   817
    fix q r assume qr: "p = q * r"
eberlm@63498
   818
    hence "fract_poly p = fract_poly q * fract_poly r" by simp
eberlm@63498
   819
    from irred and this have "fract_poly q dvd 1 \<or> fract_poly r dvd 1" 
eberlm@63498
   820
      by (rule irreducibleD)
eberlm@63498
   821
    with primitive qr show "q dvd 1 \<or> r dvd 1"
eberlm@63498
   822
      by (auto simp:  content_prod_eq_1_iff is_unit_fract_poly_iff)
eberlm@63498
   823
  qed
eberlm@63498
   824
qed
eberlm@63498
   825
eberlm@63722
   826
context
eberlm@63722
   827
begin
eberlm@63722
   828
eberlm@63498
   829
private lemma irreducible_imp_prime_poly:
eberlm@63498
   830
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
eberlm@63498
   831
  assumes "irreducible p"
eberlm@63633
   832
  shows   "prime_elem p"
eberlm@63498
   833
proof (cases "degree p = 0")
eberlm@63498
   834
  case True
eberlm@63498
   835
  with assms show ?thesis
eberlm@63498
   836
    by (auto simp: prime_elem_const_poly_iff irreducible_const_poly_iff
eberlm@63633
   837
             intro!: irreducible_imp_prime_elem elim!: degree_eq_zeroE)
eberlm@63498
   838
next
eberlm@63498
   839
  case False
eberlm@63498
   840
  from assms False have irred: "irreducible (fract_poly p)" and primitive: "content p = 1"
eberlm@63498
   841
    by (simp_all add: nonconst_poly_irreducible_iff)
eberlm@63633
   842
  from irred have prime: "prime_elem (fract_poly p)" by (rule field_poly_irreducible_imp_prime)
eberlm@63498
   843
  show ?thesis
eberlm@63633
   844
  proof (rule prime_elemI)
eberlm@63498
   845
    fix q r assume "p dvd q * r"
eberlm@63498
   846
    hence "fract_poly p dvd fract_poly (q * r)" by (rule fract_poly_dvd)
eberlm@63498
   847
    hence "fract_poly p dvd fract_poly q * fract_poly r" by simp
eberlm@63498
   848
    from prime and this have "fract_poly p dvd fract_poly q \<or> fract_poly p dvd fract_poly r"
eberlm@63633
   849
      by (rule prime_elem_dvd_multD)
eberlm@63498
   850
    with primitive show "p dvd q \<or> p dvd r" by (auto dest: fract_poly_dvdD)
eberlm@63498
   851
  qed (insert assms, auto simp: irreducible_def)
eberlm@63498
   852
qed
eberlm@63498
   853
eberlm@63498
   854
eberlm@63498
   855
lemma degree_primitive_part_fract [simp]:
eberlm@63498
   856
  "degree (primitive_part_fract p) = degree p"
eberlm@63498
   857
proof -
eberlm@63498
   858
  have "p = smult (fract_content p) (fract_poly (primitive_part_fract p))"
eberlm@63498
   859
    by (simp add: content_times_primitive_part_fract)
eberlm@63498
   860
  also have "degree \<dots> = degree (primitive_part_fract p)"
eberlm@63498
   861
    by (auto simp: degree_map_poly)
eberlm@63498
   862
  finally show ?thesis ..
eberlm@63498
   863
qed
eberlm@63498
   864
eberlm@63498
   865
lemma irreducible_primitive_part_fract:
eberlm@63498
   866
  fixes p :: "'a :: {idom_divide, ring_gcd, factorial_semiring, semiring_Gcd} fract poly"
eberlm@63498
   867
  assumes "irreducible p"
eberlm@63498
   868
  shows   "irreducible (primitive_part_fract p)"
eberlm@63498
   869
proof -
eberlm@63498
   870
  from assms have deg: "degree (primitive_part_fract p) \<noteq> 0"
eberlm@63498
   871
    by (intro notI) 
eberlm@63498
   872
       (auto elim!: degree_eq_zeroE simp: irreducible_def is_unit_poly_iff dvd_field_iff)
eberlm@63498
   873
  hence [simp]: "p \<noteq> 0" by auto
eberlm@63498
   874
eberlm@63498
   875
  note \<open>irreducible p\<close>
eberlm@63498
   876
  also have "p = [:fract_content p:] * fract_poly (primitive_part_fract p)" 
eberlm@63498
   877
    by (simp add: content_times_primitive_part_fract)
eberlm@63498
   878
  also have "irreducible \<dots> \<longleftrightarrow> irreducible (fract_poly (primitive_part_fract p))"
eberlm@63498
   879
    by (intro irreducible_mult_unit_left) (simp_all add: is_unit_poly_iff dvd_field_iff)
eberlm@63498
   880
  finally show ?thesis using deg
eberlm@63498
   881
    by (simp add: nonconst_poly_irreducible_iff)
eberlm@63498
   882
qed
eberlm@63498
   883
eberlm@63633
   884
lemma prime_elem_primitive_part_fract:
eberlm@63498
   885
  fixes p :: "'a :: {idom_divide, ring_gcd, factorial_semiring, semiring_Gcd} fract poly"
eberlm@63633
   886
  shows "irreducible p \<Longrightarrow> prime_elem (primitive_part_fract p)"
eberlm@63498
   887
  by (intro irreducible_imp_prime_poly irreducible_primitive_part_fract)
eberlm@63498
   888
eberlm@63498
   889
lemma irreducible_linear_field_poly:
eberlm@63498
   890
  fixes a b :: "'a::field"
eberlm@63498
   891
  assumes "b \<noteq> 0"
eberlm@63498
   892
  shows "irreducible [:a,b:]"
eberlm@63498
   893
proof (rule irreducibleI)
eberlm@63498
   894
  fix p q assume pq: "[:a,b:] = p * q"
wenzelm@63539
   895
  also from pq assms have "degree \<dots> = degree p + degree q" 
eberlm@63498
   896
    by (intro degree_mult_eq) auto
eberlm@63498
   897
  finally have "degree p = 0 \<or> degree q = 0" using assms by auto
eberlm@63498
   898
  with assms pq show "is_unit p \<or> is_unit q"
eberlm@63498
   899
    by (auto simp: is_unit_const_poly_iff dvd_field_iff elim!: degree_eq_zeroE)
eberlm@63498
   900
qed (insert assms, auto simp: is_unit_poly_iff)
eberlm@63498
   901
eberlm@63633
   902
lemma prime_elem_linear_field_poly:
eberlm@63633
   903
  "(b :: 'a :: field) \<noteq> 0 \<Longrightarrow> prime_elem [:a,b:]"
eberlm@63498
   904
  by (rule field_poly_irreducible_imp_prime, rule irreducible_linear_field_poly)
eberlm@63498
   905
eberlm@63498
   906
lemma irreducible_linear_poly:
eberlm@63498
   907
  fixes a b :: "'a::{idom_divide,ring_gcd,factorial_semiring,semiring_Gcd}"
eberlm@63498
   908
  shows "b \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> irreducible [:a,b:]"
eberlm@63498
   909
  by (auto intro!: irreducible_linear_field_poly 
eberlm@63498
   910
           simp:   nonconst_poly_irreducible_iff content_def map_poly_pCons)
eberlm@63498
   911
eberlm@63633
   912
lemma prime_elem_linear_poly:
eberlm@63498
   913
  fixes a b :: "'a::{idom_divide,ring_gcd,factorial_semiring,semiring_Gcd}"
eberlm@63633
   914
  shows "b \<noteq> 0 \<Longrightarrow> coprime a b \<Longrightarrow> prime_elem [:a,b:]"
eberlm@63498
   915
  by (rule irreducible_imp_prime_poly, rule irreducible_linear_poly)
eberlm@63498
   916
eberlm@63722
   917
end
eberlm@63722
   918
haftmann@64591
   919
 
eberlm@63498
   920
subsection \<open>Prime factorisation of polynomials\<close>   
eberlm@63498
   921
eberlm@63722
   922
context
eberlm@63722
   923
begin 
eberlm@63722
   924
eberlm@63498
   925
private lemma poly_prime_factorization_exists_content_1:
eberlm@63498
   926
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
eberlm@63498
   927
  assumes "p \<noteq> 0" "content p = 1"
nipkow@63830
   928
  shows   "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize p"
eberlm@63498
   929
proof -
eberlm@63498
   930
  let ?P = "field_poly.prime_factorization (fract_poly p)"
nipkow@63830
   931
  define c where "c = prod_mset (image_mset fract_content ?P)"
eberlm@63498
   932
  define c' where "c' = c * to_fract (lead_coeff p)"
nipkow@63830
   933
  define e where "e = prod_mset (image_mset primitive_part_fract ?P)"
eberlm@63498
   934
  define A where "A = image_mset (normalize \<circ> primitive_part_fract) ?P"
eberlm@63498
   935
  have "content e = (\<Prod>x\<in>#field_poly.prime_factorization (map_poly to_fract p). 
eberlm@63498
   936
                      content (primitive_part_fract x))"
nipkow@63830
   937
    by (simp add: e_def content_prod_mset multiset.map_comp o_def)
eberlm@63498
   938
  also have "image_mset (\<lambda>x. content (primitive_part_fract x)) ?P = image_mset (\<lambda>_. 1) ?P"
eberlm@63498
   939
    by (intro image_mset_cong content_primitive_part_fract) auto
haftmann@64591
   940
  finally have content_e: "content e = 1"
haftmann@64591
   941
    by simp    
eberlm@63498
   942
  
eberlm@63498
   943
  have "fract_poly p = unit_factor_field_poly (fract_poly p) * 
eberlm@63498
   944
          normalize_field_poly (fract_poly p)" by simp
eberlm@63498
   945
  also have "unit_factor_field_poly (fract_poly p) = [:to_fract (lead_coeff p):]" 
eberlm@63498
   946
    by (simp add: unit_factor_field_poly_def lead_coeff_def monom_0 degree_map_poly coeff_map_poly)
nipkow@63830
   947
  also from assms have "normalize_field_poly (fract_poly p) = prod_mset ?P" 
nipkow@63830
   948
    by (subst field_poly_prod_mset_prime_factorization) simp_all
nipkow@63830
   949
  also have "\<dots> = prod_mset (image_mset id ?P)" by simp
eberlm@63498
   950
  also have "image_mset id ?P = 
eberlm@63498
   951
               image_mset (\<lambda>x. [:fract_content x:] * fract_poly (primitive_part_fract x)) ?P"
eberlm@63498
   952
    by (intro image_mset_cong) (auto simp: content_times_primitive_part_fract)
nipkow@63830
   953
  also have "prod_mset \<dots> = smult c (fract_poly e)"
haftmann@64591
   954
    by (subst prod_mset.distrib) (simp_all add: prod_mset_fract_poly prod_mset_const_poly c_def e_def)
eberlm@63498
   955
  also have "[:to_fract (lead_coeff p):] * \<dots> = smult c' (fract_poly e)"
eberlm@63498
   956
    by (simp add: c'_def)
eberlm@63498
   957
  finally have eq: "fract_poly p = smult c' (fract_poly e)" .
eberlm@63498
   958
  also obtain b where b: "c' = to_fract b" "is_unit b"
eberlm@63498
   959
  proof -
eberlm@63498
   960
    from fract_poly_smult_eqE[OF eq] guess a b . note ab = this
eberlm@63498
   961
    from ab(2) have "content (smult a p) = content (smult b e)" by (simp only: )
eberlm@63498
   962
    with assms content_e have "a = normalize b" by (simp add: ab(4))
eberlm@63498
   963
    with ab have ab': "a = 1" "is_unit b" by (simp_all add: normalize_1_iff)
eberlm@63498
   964
    with ab ab' have "c' = to_fract b" by auto
eberlm@63498
   965
    from this and \<open>is_unit b\<close> show ?thesis by (rule that)
eberlm@63498
   966
  qed
eberlm@63498
   967
  hence "smult c' (fract_poly e) = fract_poly (smult b e)" by simp
eberlm@63498
   968
  finally have "p = smult b e" by (simp only: fract_poly_eq_iff)
eberlm@63498
   969
  hence "p = [:b:] * e" by simp
eberlm@63498
   970
  with b have "normalize p = normalize e" 
eberlm@63498
   971
    by (simp only: normalize_mult) (simp add: is_unit_normalize is_unit_poly_iff)
nipkow@63830
   972
  also have "normalize e = prod_mset A"
nipkow@63830
   973
    by (simp add: multiset.map_comp e_def A_def normalize_prod_mset)
nipkow@63830
   974
  finally have "prod_mset A = normalize p" ..
eberlm@63498
   975
  
eberlm@63633
   976
  have "prime_elem p" if "p \<in># A" for p
eberlm@63633
   977
    using that by (auto simp: A_def prime_elem_primitive_part_fract prime_elem_imp_irreducible 
eberlm@63498
   978
                        dest!: field_poly_in_prime_factorization_imp_prime )
nipkow@63830
   979
  from this and \<open>prod_mset A = normalize p\<close> show ?thesis
eberlm@63498
   980
    by (intro exI[of _ A]) blast
eberlm@63498
   981
qed
eberlm@63498
   982
eberlm@63498
   983
lemma poly_prime_factorization_exists:
eberlm@63498
   984
  fixes p :: "'a :: {factorial_semiring,semiring_Gcd,ring_gcd,idom_divide} poly"
eberlm@63498
   985
  assumes "p \<noteq> 0"
nipkow@63830
   986
  shows   "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize p"
eberlm@63498
   987
proof -
eberlm@63498
   988
  define B where "B = image_mset (\<lambda>x. [:x:]) (prime_factorization (content p))"
nipkow@63830
   989
  have "\<exists>A. (\<forall>p. p \<in># A \<longrightarrow> prime_elem p) \<and> prod_mset A = normalize (primitive_part p)"
eberlm@63498
   990
    by (rule poly_prime_factorization_exists_content_1) (insert assms, simp_all)
eberlm@63498
   991
  then guess A by (elim exE conjE) note A = this
nipkow@63830
   992
  moreover from assms have "prod_mset B = [:content p:]"
nipkow@63830
   993
    by (simp add: B_def prod_mset_const_poly prod_mset_prime_factorization)
eberlm@63633
   994
  moreover have "\<forall>p. p \<in># B \<longrightarrow> prime_elem p"
haftmann@63905
   995
    by (auto simp: B_def intro!: lift_prime_elem_poly dest: in_prime_factors_imp_prime)
eberlm@63498
   996
  ultimately show ?thesis by (intro exI[of _ "B + A"]) auto
eberlm@63498
   997
qed
eberlm@63498
   998
eberlm@63498
   999
end
eberlm@63498
  1000
eberlm@63498
  1001
eberlm@63498
  1002
subsection \<open>Typeclass instances\<close>
eberlm@63498
  1003
eberlm@63498
  1004
instance poly :: (factorial_ring_gcd) factorial_semiring
eberlm@63498
  1005
  by standard (rule poly_prime_factorization_exists)  
eberlm@63498
  1006
eberlm@63498
  1007
instantiation poly :: (factorial_ring_gcd) factorial_ring_gcd
eberlm@63498
  1008
begin
eberlm@63498
  1009
eberlm@63498
  1010
definition gcd_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@63498
  1011
  [code del]: "gcd_poly = gcd_factorial"
eberlm@63498
  1012
eberlm@63498
  1013
definition lcm_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@63498
  1014
  [code del]: "lcm_poly = lcm_factorial"
eberlm@63498
  1015
  
eberlm@63498
  1016
definition Gcd_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@63498
  1017
 [code del]: "Gcd_poly = Gcd_factorial"
eberlm@63498
  1018
eberlm@63498
  1019
definition Lcm_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@63498
  1020
 [code del]: "Lcm_poly = Lcm_factorial"
eberlm@63498
  1021
 
eberlm@63498
  1022
instance by standard (simp_all add: gcd_poly_def lcm_poly_def Gcd_poly_def Lcm_poly_def)
eberlm@63498
  1023
eberlm@63498
  1024
end
eberlm@63498
  1025
haftmann@64784
  1026
instantiation poly :: ("{field,factorial_ring_gcd}") unique_euclidean_ring
eberlm@63498
  1027
begin
eberlm@63498
  1028
haftmann@64784
  1029
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
haftmann@64784
  1030
  where "euclidean_size_poly p = (if p = 0 then 0 else 2 ^ degree p)"
haftmann@64784
  1031
haftmann@64784
  1032
definition uniqueness_constraint_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> bool"
haftmann@64784
  1033
  where [simp]: "uniqueness_constraint_poly = top"
eberlm@63498
  1034
eberlm@63498
  1035
instance 
haftmann@64784
  1036
  by standard
haftmann@64784
  1037
   (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
haftmann@64784
  1038
    split: if_splits)
haftmann@64784
  1039
eberlm@63498
  1040
end
eberlm@63498
  1041
eberlm@63498
  1042
instance poly :: ("{field,factorial_ring_gcd}") euclidean_ring_gcd
haftmann@64786
  1043
  by (rule euclidean_ring_gcd_class.intro, rule factorial_euclidean_semiring_gcdI)
haftmann@64786
  1044
    standard
eberlm@63498
  1045
eberlm@63498
  1046
  
eberlm@63498
  1047
subsection \<open>Polynomial GCD\<close>
eberlm@63498
  1048
eberlm@63498
  1049
lemma gcd_poly_decompose:
eberlm@63498
  1050
  fixes p q :: "'a :: factorial_ring_gcd poly"
eberlm@63498
  1051
  shows "gcd p q = 
eberlm@63498
  1052
           smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q))"
eberlm@63498
  1053
proof (rule sym, rule gcdI)
eberlm@63498
  1054
  have "[:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q) dvd
eberlm@63498
  1055
          [:content p:] * primitive_part p" by (intro mult_dvd_mono) simp_all
eberlm@63498
  1056
  thus "smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q)) dvd p"
eberlm@63498
  1057
    by simp
eberlm@63498
  1058
next
eberlm@63498
  1059
  have "[:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q) dvd
eberlm@63498
  1060
          [:content q:] * primitive_part q" by (intro mult_dvd_mono) simp_all
eberlm@63498
  1061
  thus "smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q)) dvd q"
eberlm@63498
  1062
    by simp
eberlm@63498
  1063
next
eberlm@63498
  1064
  fix d assume "d dvd p" "d dvd q"
eberlm@63498
  1065
  hence "[:content d:] * primitive_part d dvd 
eberlm@63498
  1066
           [:gcd (content p) (content q):] * gcd (primitive_part p) (primitive_part q)"
eberlm@63498
  1067
    by (intro mult_dvd_mono) auto
eberlm@63498
  1068
  thus "d dvd smult (gcd (content p) (content q)) (gcd (primitive_part p) (primitive_part q))"
eberlm@63498
  1069
    by simp
eberlm@63498
  1070
qed (auto simp: normalize_smult)
eberlm@63498
  1071
  
eberlm@63498
  1072
eberlm@63498
  1073
lemma gcd_poly_pseudo_mod:
eberlm@63498
  1074
  fixes p q :: "'a :: factorial_ring_gcd poly"
eberlm@63498
  1075
  assumes nz: "q \<noteq> 0" and prim: "content p = 1" "content q = 1"
eberlm@63498
  1076
  shows   "gcd p q = gcd q (primitive_part (pseudo_mod p q))"
eberlm@63498
  1077
proof -
eberlm@63498
  1078
  define r s where "r = fst (pseudo_divmod p q)" and "s = snd (pseudo_divmod p q)"
eberlm@63498
  1079
  define a where "a = [:coeff q (degree q) ^ (Suc (degree p) - degree q):]"
eberlm@63498
  1080
  have [simp]: "primitive_part a = unit_factor a"
eberlm@63498
  1081
    by (simp add: a_def unit_factor_poly_def unit_factor_power monom_0)
eberlm@63498
  1082
  from nz have [simp]: "a \<noteq> 0" by (auto simp: a_def)
eberlm@63498
  1083
  
eberlm@63498
  1084
  have rs: "pseudo_divmod p q = (r, s)" by (simp add: r_def s_def)
eberlm@63498
  1085
  have "gcd (q * r + s) q = gcd q s"
eberlm@63498
  1086
    using gcd_add_mult[of q r s] by (simp add: gcd.commute add_ac mult_ac)
eberlm@63498
  1087
  with pseudo_divmod(1)[OF nz rs]
eberlm@63498
  1088
    have "gcd (p * a) q = gcd q s" by (simp add: a_def)
eberlm@63498
  1089
  also from prim have "gcd (p * a) q = gcd p q"
eberlm@63498
  1090
    by (subst gcd_poly_decompose)
eberlm@63498
  1091
       (auto simp: primitive_part_mult gcd_mult_unit1 primitive_part_prim 
eberlm@63498
  1092
             simp del: mult_pCons_right )
eberlm@63498
  1093
  also from prim have "gcd q s = gcd q (primitive_part s)"
eberlm@63498
  1094
    by (subst gcd_poly_decompose) (simp_all add: primitive_part_prim)
eberlm@63498
  1095
  also have "s = pseudo_mod p q" by (simp add: s_def pseudo_mod_def)
eberlm@63498
  1096
  finally show ?thesis .
eberlm@63498
  1097
qed
eberlm@63498
  1098
eberlm@63498
  1099
lemma degree_pseudo_mod_less:
eberlm@63498
  1100
  assumes "q \<noteq> 0" "pseudo_mod p q \<noteq> 0"
eberlm@63498
  1101
  shows   "degree (pseudo_mod p q) < degree q"
eberlm@63498
  1102
  using pseudo_mod(2)[of q p] assms by auto
eberlm@63498
  1103
eberlm@63498
  1104
function gcd_poly_code_aux :: "'a :: factorial_ring_gcd poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@63498
  1105
  "gcd_poly_code_aux p q = 
eberlm@63498
  1106
     (if q = 0 then normalize p else gcd_poly_code_aux q (primitive_part (pseudo_mod p q)))" 
eberlm@63498
  1107
by auto
eberlm@63498
  1108
termination
eberlm@63498
  1109
  by (relation "measure ((\<lambda>p. if p = 0 then 0 else Suc (degree p)) \<circ> snd)")
haftmann@64164
  1110
     (auto simp: degree_pseudo_mod_less)
eberlm@63498
  1111
eberlm@63498
  1112
declare gcd_poly_code_aux.simps [simp del]
eberlm@63498
  1113
eberlm@63498
  1114
lemma gcd_poly_code_aux_correct:
eberlm@63498
  1115
  assumes "content p = 1" "q = 0 \<or> content q = 1"
eberlm@63498
  1116
  shows   "gcd_poly_code_aux p q = gcd p q"
eberlm@63498
  1117
  using assms
eberlm@63498
  1118
proof (induction p q rule: gcd_poly_code_aux.induct)
eberlm@63498
  1119
  case (1 p q)
eberlm@63498
  1120
  show ?case
eberlm@63498
  1121
  proof (cases "q = 0")
eberlm@63498
  1122
    case True
eberlm@63498
  1123
    thus ?thesis by (subst gcd_poly_code_aux.simps) auto
eberlm@63498
  1124
  next
eberlm@63498
  1125
    case False
eberlm@63498
  1126
    hence "gcd_poly_code_aux p q = gcd_poly_code_aux q (primitive_part (pseudo_mod p q))"
eberlm@63498
  1127
      by (subst gcd_poly_code_aux.simps) simp_all
eberlm@63498
  1128
    also from "1.prems" False 
eberlm@63498
  1129
      have "primitive_part (pseudo_mod p q) = 0 \<or> 
eberlm@63498
  1130
              content (primitive_part (pseudo_mod p q)) = 1"
eberlm@63498
  1131
      by (cases "pseudo_mod p q = 0") auto
eberlm@63498
  1132
    with "1.prems" False 
eberlm@63498
  1133
      have "gcd_poly_code_aux q (primitive_part (pseudo_mod p q)) = 
eberlm@63498
  1134
              gcd q (primitive_part (pseudo_mod p q))"
eberlm@63498
  1135
      by (intro 1) simp_all
eberlm@63498
  1136
    also from "1.prems" False 
eberlm@63498
  1137
      have "\<dots> = gcd p q" by (intro gcd_poly_pseudo_mod [symmetric]) auto
eberlm@63498
  1138
    finally show ?thesis .
eberlm@63498
  1139
  qed
eberlm@63498
  1140
qed
eberlm@63498
  1141
eberlm@63498
  1142
definition gcd_poly_code 
eberlm@63498
  1143
    :: "'a :: factorial_ring_gcd poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" 
eberlm@63498
  1144
  where "gcd_poly_code p q = 
eberlm@63498
  1145
           (if p = 0 then normalize q else if q = 0 then normalize p else
eberlm@63498
  1146
              smult (gcd (content p) (content q)) 
eberlm@63498
  1147
                (gcd_poly_code_aux (primitive_part p) (primitive_part q)))"
eberlm@63498
  1148
haftmann@64591
  1149
lemma gcd_poly_code [code]: "gcd p q = gcd_poly_code p q"
haftmann@64591
  1150
  by (simp add: gcd_poly_code_def gcd_poly_code_aux_correct gcd_poly_decompose [symmetric])
haftmann@64591
  1151
eberlm@63498
  1152
lemma lcm_poly_code [code]: 
eberlm@63498
  1153
  fixes p q :: "'a :: factorial_ring_gcd poly"
eberlm@63498
  1154
  shows "lcm p q = normalize (p * q) div gcd p q"
haftmann@64591
  1155
  by (fact lcm_gcd)
eberlm@63498
  1156
eberlm@63498
  1157
declare Gcd_set
eberlm@63498
  1158
  [where ?'a = "'a :: factorial_ring_gcd poly", code]
eberlm@63498
  1159
eberlm@63498
  1160
declare Lcm_set
eberlm@63498
  1161
  [where ?'a = "'a :: factorial_ring_gcd poly", code]
haftmann@64591
  1162
haftmann@64591
  1163
text \<open>Example:
haftmann@64591
  1164
  @{lemma "Lcm {[:1, 2, 3:], [:2, 3, 4:]} = [:[:2:], [:7:], [:16:], [:17:], [:12 :: int:]:]" by eval}
haftmann@64591
  1165
\<close>
eberlm@63498
  1166
  
wenzelm@63764
  1167
end