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