src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Thu Jul 02 10:06:47 2015 +0200 (2015-07-02)
changeset 60634 e3b6e516608b
parent 60600 87fbfea0bd0a
child 60685 cb21b7022b00
permissions -rw-r--r--
separate (semi)ring with normalization
haftmann@58023
     1
(* Author: Manuel Eberl *)
haftmann@58023
     2
wenzelm@60526
     3
section \<open>Abstract euclidean algorithm\<close>
haftmann@58023
     4
haftmann@58023
     5
theory Euclidean_Algorithm
haftmann@60634
     6
imports Complex_Main "~~/src/HOL/Library/Polynomial" "~~/src/HOL/Number_Theory/Normalization_Semidom"
haftmann@60634
     7
begin
haftmann@60634
     8
haftmann@60634
     9
lemma is_unit_polyE:
haftmann@60634
    10
  assumes "is_unit p"
haftmann@60634
    11
  obtains a where "p = monom a 0" and "a \<noteq> 0"
haftmann@60634
    12
proof -
haftmann@60634
    13
  obtain a q where "p = pCons a q" by (cases p)
haftmann@60634
    14
  with assms have "p = [:a:]" and "a \<noteq> 0"
haftmann@60634
    15
    by (simp_all add: is_unit_pCons_iff)
haftmann@60634
    16
  with that show thesis by (simp add: monom_0)
haftmann@60634
    17
qed
haftmann@60634
    18
haftmann@60634
    19
instantiation poly :: (field) normalization_semidom
haftmann@58023
    20
begin
haftmann@60634
    21
haftmann@60634
    22
definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@60634
    23
  where "normalize_poly p = smult (1 / coeff p (degree p)) p"
haftmann@60634
    24
haftmann@60634
    25
definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@60634
    26
  where "unit_factor_poly p = monom (coeff p (degree p)) 0"
haftmann@60634
    27
haftmann@60634
    28
instance
haftmann@60634
    29
proof
haftmann@60634
    30
  fix p :: "'a poly"
haftmann@60634
    31
  show "unit_factor p * normalize p = p"
haftmann@60634
    32
    by (simp add: normalize_poly_def unit_factor_poly_def)
haftmann@60634
    33
      (simp only: mult_smult_left [symmetric] smult_monom, simp)
haftmann@60634
    34
next
haftmann@60634
    35
  show "normalize 0 = (0::'a poly)"
haftmann@60634
    36
    by (simp add: normalize_poly_def)
haftmann@60634
    37
next
haftmann@60634
    38
  show "unit_factor 0 = (0::'a poly)"
haftmann@60634
    39
    by (simp add: unit_factor_poly_def)
haftmann@60634
    40
next
haftmann@60634
    41
  fix p :: "'a poly"
haftmann@60634
    42
  assume "is_unit p"
haftmann@60634
    43
  then obtain a where "p = monom a 0" and "a \<noteq> 0"
haftmann@60634
    44
    by (rule is_unit_polyE)
haftmann@60634
    45
  then show "normalize p = 1"
haftmann@60634
    46
    by (auto simp add: normalize_poly_def smult_monom degree_monom_eq)
haftmann@60634
    47
next
haftmann@60634
    48
  fix p q :: "'a poly"
haftmann@60634
    49
  assume "q \<noteq> 0"
haftmann@60634
    50
  from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"
haftmann@60634
    51
    by (auto intro: is_unit_monom_0)
haftmann@60634
    52
  then show "is_unit (unit_factor q)"
haftmann@60634
    53
    by (simp add: unit_factor_poly_def)
haftmann@60634
    54
next
haftmann@60634
    55
  fix p q :: "'a poly"
haftmann@60634
    56
  have "monom (coeff (p * q) (degree (p * q))) 0 =
haftmann@60634
    57
    monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
haftmann@60634
    58
    by (simp add: monom_0 coeff_degree_mult)
haftmann@60634
    59
  then show "unit_factor (p * q) =
haftmann@60634
    60
    unit_factor p * unit_factor q"
haftmann@60634
    61
    by (simp add: unit_factor_poly_def)
haftmann@60634
    62
qed
haftmann@60634
    63
haftmann@60634
    64
end
haftmann@60634
    65
wenzelm@60526
    66
text \<open>
haftmann@58023
    67
  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
haftmann@58023
    68
  implemented. It must provide:
haftmann@58023
    69
  \begin{itemize}
haftmann@58023
    70
  \item division with remainder
haftmann@58023
    71
  \item a size function such that @{term "size (a mod b) < size b"} 
haftmann@58023
    72
        for any @{term "b \<noteq> 0"}
haftmann@58023
    73
  \end{itemize}
haftmann@58023
    74
  The existence of these functions makes it possible to derive gcd and lcm functions 
haftmann@58023
    75
  for any Euclidean semiring.
wenzelm@60526
    76
\<close> 
haftmann@60634
    77
class euclidean_semiring = semiring_div + normalization_semidom + 
haftmann@58023
    78
  fixes euclidean_size :: "'a \<Rightarrow> nat"
haftmann@60569
    79
  assumes mod_size_less: 
haftmann@60600
    80
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
haftmann@58023
    81
  assumes size_mult_mono:
haftmann@60634
    82
    "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
haftmann@58023
    83
begin
haftmann@58023
    84
haftmann@58023
    85
lemma euclidean_division:
haftmann@58023
    86
  fixes a :: 'a and b :: 'a
haftmann@60600
    87
  assumes "b \<noteq> 0"
haftmann@58023
    88
  obtains s and t where "a = s * b + t" 
haftmann@58023
    89
    and "euclidean_size t < euclidean_size b"
haftmann@58023
    90
proof -
haftmann@60569
    91
  from div_mod_equality [of a b 0] 
haftmann@58023
    92
     have "a = a div b * b + a mod b" by simp
haftmann@60569
    93
  with that and assms show ?thesis by (auto simp add: mod_size_less)
haftmann@58023
    94
qed
haftmann@58023
    95
haftmann@58023
    96
lemma dvd_euclidean_size_eq_imp_dvd:
haftmann@58023
    97
  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
haftmann@58023
    98
  shows "a dvd b"
haftmann@60569
    99
proof (rule ccontr)
haftmann@60569
   100
  assume "\<not> a dvd b"
haftmann@60569
   101
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
haftmann@58023
   102
  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
haftmann@58023
   103
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
wenzelm@60526
   104
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
wenzelm@60526
   105
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
haftmann@58023
   106
      using size_mult_mono by force
haftmann@60569
   107
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
haftmann@60569
   108
  have "euclidean_size (b mod a) < euclidean_size a"
haftmann@58023
   109
      using mod_size_less by blast
haftmann@58023
   110
  ultimately show False using size_eq by simp
haftmann@58023
   111
qed
haftmann@58023
   112
haftmann@58023
   113
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   114
where
haftmann@60634
   115
  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
haftmann@60572
   116
  by pat_completeness simp
haftmann@60569
   117
termination
haftmann@60569
   118
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
   119
haftmann@58023
   120
declare gcd_eucl.simps [simp del]
haftmann@58023
   121
haftmann@60569
   122
lemma gcd_eucl_induct [case_names zero mod]:
haftmann@60569
   123
  assumes H1: "\<And>b. P b 0"
haftmann@60569
   124
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
   125
  shows "P a b"
haftmann@58023
   126
proof (induct a b rule: gcd_eucl.induct)
haftmann@60569
   127
  case ("1" a b)
haftmann@60569
   128
  show ?case
haftmann@60569
   129
  proof (cases "b = 0")
haftmann@60569
   130
    case True then show "P a b" by simp (rule H1)
haftmann@60569
   131
  next
haftmann@60569
   132
    case False
haftmann@60600
   133
    then have "P b (a mod b)"
haftmann@60600
   134
      by (rule "1.hyps")
haftmann@60569
   135
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
   136
      by (blast intro: H2)
haftmann@60569
   137
  qed
haftmann@58023
   138
qed
haftmann@58023
   139
haftmann@58023
   140
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   141
where
haftmann@60634
   142
  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
haftmann@58023
   143
haftmann@60572
   144
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
haftmann@60572
   145
  Somewhat complicated definition of Lcm that has the advantage of working
haftmann@60572
   146
  for infinite sets as well\<close>
haftmann@58023
   147
where
haftmann@60430
   148
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
   149
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
   150
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60634
   151
       in normalize l 
haftmann@58023
   152
      else 0)"
haftmann@58023
   153
haftmann@58023
   154
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
haftmann@58023
   155
where
haftmann@58023
   156
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
   157
haftmann@60572
   158
lemma gcd_eucl_0:
haftmann@60634
   159
  "gcd_eucl a 0 = normalize a"
haftmann@60572
   160
  by (simp add: gcd_eucl.simps [of a 0])
haftmann@60572
   161
haftmann@60572
   162
lemma gcd_eucl_0_left:
haftmann@60634
   163
  "gcd_eucl 0 a = normalize a"
haftmann@60600
   164
  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
haftmann@60572
   165
haftmann@60572
   166
lemma gcd_eucl_non_0:
haftmann@60572
   167
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
haftmann@60600
   168
  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
haftmann@60572
   169
haftmann@58023
   170
end
haftmann@58023
   171
haftmann@60598
   172
class euclidean_ring = euclidean_semiring + idom
haftmann@60598
   173
begin
haftmann@60598
   174
haftmann@60598
   175
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
haftmann@60598
   176
  "euclid_ext a b = 
haftmann@60598
   177
     (if b = 0 then 
haftmann@60634
   178
        (1 div unit_factor a, 0, normalize a)
haftmann@60598
   179
      else
haftmann@60598
   180
        case euclid_ext b (a mod b) of
haftmann@60598
   181
            (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60598
   182
  by pat_completeness simp
haftmann@60598
   183
termination
haftmann@60598
   184
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@60598
   185
haftmann@60598
   186
declare euclid_ext.simps [simp del]
haftmann@60598
   187
haftmann@60598
   188
lemma euclid_ext_0: 
haftmann@60634
   189
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
haftmann@60598
   190
  by (simp add: euclid_ext.simps [of a 0])
haftmann@60598
   191
haftmann@60598
   192
lemma euclid_ext_left_0: 
haftmann@60634
   193
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
haftmann@60600
   194
  by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
haftmann@60598
   195
haftmann@60598
   196
lemma euclid_ext_non_0: 
haftmann@60598
   197
  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
haftmann@60598
   198
    (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60600
   199
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   200
haftmann@60598
   201
lemma euclid_ext_code [code]:
haftmann@60634
   202
  "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)
haftmann@60598
   203
    else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
haftmann@60598
   204
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   205
haftmann@60598
   206
lemma euclid_ext_correct:
haftmann@60598
   207
  "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
haftmann@60598
   208
proof (induct a b rule: gcd_eucl_induct)
haftmann@60598
   209
  case (zero a) then show ?case
haftmann@60598
   210
    by (simp add: euclid_ext_0 ac_simps)
haftmann@60598
   211
next
haftmann@60598
   212
  case (mod a b)
haftmann@60598
   213
  obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
haftmann@60598
   214
    by (cases "euclid_ext b (a mod b)") blast
haftmann@60598
   215
  with mod have "c = s * b + t * (a mod b)" by simp
haftmann@60598
   216
  also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
haftmann@60598
   217
    by (simp add: algebra_simps) 
haftmann@60598
   218
  also have "(a div b) * b + a mod b = a" using mod_div_equality .
haftmann@60598
   219
  finally show ?case
haftmann@60598
   220
    by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
haftmann@60598
   221
qed
haftmann@60598
   222
haftmann@60598
   223
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@60598
   224
where
haftmann@60598
   225
  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
haftmann@60598
   226
haftmann@60634
   227
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
haftmann@60598
   228
  by (simp add: euclid_ext'_def euclid_ext_0)
haftmann@60598
   229
haftmann@60634
   230
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
haftmann@60598
   231
  by (simp add: euclid_ext'_def euclid_ext_left_0)
haftmann@60598
   232
  
haftmann@60598
   233
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
haftmann@60598
   234
  fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
haftmann@60598
   235
  by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
haftmann@60598
   236
haftmann@60598
   237
end
haftmann@60598
   238
haftmann@58023
   239
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@58023
   240
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
haftmann@58023
   241
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
haftmann@58023
   242
begin
haftmann@58023
   243
haftmann@58023
   244
lemma gcd_0_left:
haftmann@60634
   245
  "gcd 0 a = normalize a"
haftmann@60572
   246
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
haftmann@58023
   247
haftmann@58023
   248
lemma gcd_0:
haftmann@60634
   249
  "gcd a 0 = normalize a"
haftmann@60572
   250
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
haftmann@58023
   251
haftmann@58023
   252
lemma gcd_non_0:
haftmann@60430
   253
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
haftmann@60572
   254
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
haftmann@58023
   255
haftmann@60430
   256
lemma gcd_dvd1 [iff]: "gcd a b dvd a"
haftmann@60430
   257
  and gcd_dvd2 [iff]: "gcd a b dvd b"
haftmann@60569
   258
  by (induct a b rule: gcd_eucl_induct)
haftmann@60569
   259
    (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
haftmann@60569
   260
    
haftmann@58023
   261
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
haftmann@58023
   262
  by (rule dvd_trans, assumption, rule gcd_dvd1)
haftmann@58023
   263
haftmann@58023
   264
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
haftmann@58023
   265
  by (rule dvd_trans, assumption, rule gcd_dvd2)
haftmann@58023
   266
haftmann@58023
   267
lemma gcd_greatest:
haftmann@60430
   268
  fixes k a b :: 'a
haftmann@60430
   269
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
haftmann@60569
   270
proof (induct a b rule: gcd_eucl_induct)
haftmann@60569
   271
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
haftmann@60569
   272
next
haftmann@60569
   273
  case (mod a b)
haftmann@60569
   274
  then show ?case
haftmann@60569
   275
    by (simp add: gcd_non_0 dvd_mod_iff)
haftmann@58023
   276
qed
haftmann@58023
   277
haftmann@58023
   278
lemma dvd_gcd_iff:
haftmann@60430
   279
  "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
haftmann@58023
   280
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@58023
   281
haftmann@58023
   282
lemmas gcd_greatest_iff = dvd_gcd_iff
haftmann@58023
   283
haftmann@58023
   284
lemma gcd_zero [simp]:
haftmann@60430
   285
  "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
haftmann@58023
   286
  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
haftmann@58023
   287
haftmann@60634
   288
lemma unit_factor_gcd [simp]:
haftmann@60634
   289
  "unit_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
haftmann@60569
   290
  by (induct a b rule: gcd_eucl_induct)
haftmann@60569
   291
    (auto simp add: gcd_0 gcd_non_0)
haftmann@58023
   292
haftmann@58023
   293
lemma gcdI:
haftmann@60634
   294
  assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
haftmann@60634
   295
    and "unit_factor c = (if c = 0 then 0 else 1)"
haftmann@60634
   296
  shows "c = gcd a b"
haftmann@60634
   297
  by (rule associated_eqI) (auto simp: assms associated_def intro: gcd_greatest)
haftmann@58023
   298
haftmann@58023
   299
sublocale gcd!: abel_semigroup gcd
haftmann@58023
   300
proof
haftmann@60430
   301
  fix a b c 
haftmann@60430
   302
  show "gcd (gcd a b) c = gcd a (gcd b c)"
haftmann@58023
   303
  proof (rule gcdI)
haftmann@60430
   304
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
haftmann@60430
   305
    then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
haftmann@60430
   306
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
haftmann@60430
   307
    hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
haftmann@60430
   308
    moreover have "gcd (gcd a b) c dvd c" by simp
haftmann@60430
   309
    ultimately show "gcd (gcd a b) c dvd gcd b c"
haftmann@58023
   310
      by (rule gcd_greatest)
haftmann@60634
   311
    show "unit_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
haftmann@58023
   312
      by auto
haftmann@60430
   313
    fix l assume "l dvd a" and "l dvd gcd b c"
haftmann@58023
   314
    with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
haftmann@60430
   315
      have "l dvd b" and "l dvd c" by blast+
wenzelm@60526
   316
    with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
haftmann@58023
   317
      by (intro gcd_greatest)
haftmann@58023
   318
  qed
haftmann@58023
   319
next
haftmann@60430
   320
  fix a b
haftmann@60430
   321
  show "gcd a b = gcd b a"
haftmann@58023
   322
    by (rule gcdI) (simp_all add: gcd_greatest)
haftmann@58023
   323
qed
haftmann@58023
   324
haftmann@58023
   325
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
haftmann@60634
   326
    unit_factor d = (if d = 0 then 0 else 1) \<and>
haftmann@58023
   327
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
haftmann@58023
   328
  by (rule, auto intro: gcdI simp: gcd_greatest)
haftmann@58023
   329
haftmann@58023
   330
lemma gcd_dvd_prod: "gcd a b dvd k * b"
haftmann@58023
   331
  using mult_dvd_mono [of 1] by auto
haftmann@58023
   332
haftmann@60430
   333
lemma gcd_1_left [simp]: "gcd 1 a = 1"
haftmann@58023
   334
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   335
haftmann@60430
   336
lemma gcd_1 [simp]: "gcd a 1 = 1"
haftmann@58023
   337
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   338
haftmann@58023
   339
lemma gcd_proj2_if_dvd: 
haftmann@60634
   340
  "b dvd a \<Longrightarrow> gcd a b = normalize b"
haftmann@60430
   341
  by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
haftmann@58023
   342
haftmann@58023
   343
lemma gcd_proj1_if_dvd: 
haftmann@60634
   344
  "a dvd b \<Longrightarrow> gcd a b = normalize a"
haftmann@58023
   345
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
haftmann@58023
   346
haftmann@60634
   347
lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"
haftmann@58023
   348
proof
haftmann@60634
   349
  assume A: "gcd m n = normalize m"
haftmann@58023
   350
  show "m dvd n"
haftmann@58023
   351
  proof (cases "m = 0")
haftmann@58023
   352
    assume [simp]: "m \<noteq> 0"
haftmann@60634
   353
    from A have B: "m = gcd m n * unit_factor m"
haftmann@58023
   354
      by (simp add: unit_eq_div2)
haftmann@58023
   355
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
haftmann@58023
   356
  qed (insert A, simp)
haftmann@58023
   357
next
haftmann@58023
   358
  assume "m dvd n"
haftmann@60634
   359
  then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd)
haftmann@58023
   360
qed
haftmann@58023
   361
  
haftmann@60634
   362
lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
haftmann@60634
   363
  using gcd_proj1_iff [of n m] by (simp add: ac_simps)
haftmann@58023
   364
haftmann@58023
   365
lemma gcd_mod1 [simp]:
haftmann@60430
   366
  "gcd (a mod b) b = gcd a b"
haftmann@58023
   367
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   368
haftmann@58023
   369
lemma gcd_mod2 [simp]:
haftmann@60430
   370
  "gcd a (b mod a) = gcd a b"
haftmann@58023
   371
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   372
         
haftmann@58023
   373
lemma gcd_mult_distrib': 
haftmann@60634
   374
  "normalize c * gcd a b = gcd (c * a) (c * b)"
haftmann@60569
   375
proof (cases "c = 0")
haftmann@60569
   376
  case True then show ?thesis by (simp_all add: gcd_0)
haftmann@60569
   377
next
haftmann@60634
   378
  case False then have [simp]: "is_unit (unit_factor c)" by simp
haftmann@60569
   379
  show ?thesis
haftmann@60569
   380
  proof (induct a b rule: gcd_eucl_induct)
haftmann@60569
   381
    case (zero a) show ?case
haftmann@60569
   382
    proof (cases "a = 0")
haftmann@60569
   383
      case True then show ?thesis by (simp add: gcd_0)
haftmann@60569
   384
    next
haftmann@60634
   385
      case False
haftmann@60634
   386
      then show ?thesis by (simp add: gcd_0 normalize_mult)
haftmann@60569
   387
    qed
haftmann@60569
   388
    case (mod a b)
haftmann@60569
   389
    then show ?case by (simp add: mult_mod_right gcd.commute)
haftmann@58023
   390
  qed
haftmann@58023
   391
qed
haftmann@58023
   392
haftmann@58023
   393
lemma gcd_mult_distrib:
haftmann@60634
   394
  "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"
haftmann@58023
   395
proof-
haftmann@60634
   396
  have "normalize k * gcd a b = gcd (k * a) (k * b)"
haftmann@60634
   397
    by (simp add: gcd_mult_distrib')
haftmann@60634
   398
  then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"
haftmann@60634
   399
    by simp
haftmann@60634
   400
  then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"
haftmann@60634
   401
    by (simp only: ac_simps)
haftmann@60634
   402
  then show ?thesis
haftmann@59009
   403
    by simp
haftmann@58023
   404
qed
haftmann@58023
   405
haftmann@58023
   406
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   407
  assumes "a \<noteq> 0"
haftmann@58023
   408
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   409
proof -
haftmann@58023
   410
   have "gcd a b dvd a" by (rule gcd_dvd1)
haftmann@58023
   411
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
wenzelm@60526
   412
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
haftmann@58023
   413
qed
haftmann@58023
   414
haftmann@58023
   415
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   416
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   417
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   418
haftmann@58023
   419
lemma euclidean_size_gcd_less1:
haftmann@58023
   420
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   421
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   422
proof (rule ccontr)
haftmann@58023
   423
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
wenzelm@60526
   424
  with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   425
    by (intro le_antisym, simp_all)
haftmann@58023
   426
  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
haftmann@58023
   427
  hence "a dvd b" using dvd_gcd_D2 by blast
wenzelm@60526
   428
  with \<open>\<not>a dvd b\<close> show False by contradiction
haftmann@58023
   429
qed
haftmann@58023
   430
haftmann@58023
   431
lemma euclidean_size_gcd_less2:
haftmann@58023
   432
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   433
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   434
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   435
haftmann@60430
   436
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
haftmann@58023
   437
  apply (rule gcdI)
haftmann@58023
   438
  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
haftmann@58023
   439
  apply (rule gcd_dvd2)
haftmann@58023
   440
  apply (rule gcd_greatest, simp add: unit_simps, assumption)
haftmann@60634
   441
  apply (subst unit_factor_gcd, simp add: gcd_0)
haftmann@58023
   442
  done
haftmann@58023
   443
haftmann@60430
   444
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
haftmann@58023
   445
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
haftmann@58023
   446
haftmann@60430
   447
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
haftmann@60433
   448
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
haftmann@58023
   449
haftmann@60430
   450
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
haftmann@60433
   451
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
haftmann@58023
   452
haftmann@60634
   453
lemma normalize_gcd_left [simp]:
haftmann@60634
   454
  "gcd (normalize a) b = gcd a b"
haftmann@60634
   455
proof (cases "a = 0")
haftmann@60634
   456
  case True then show ?thesis
haftmann@60634
   457
    by simp
haftmann@60634
   458
next
haftmann@60634
   459
  case False then have "is_unit (unit_factor a)"
haftmann@60634
   460
    by simp
haftmann@60634
   461
  moreover have "normalize a = a div unit_factor a"
haftmann@60634
   462
    by simp
haftmann@60634
   463
  ultimately show ?thesis
haftmann@60634
   464
    by (simp only: gcd_div_unit1)
haftmann@60634
   465
qed
haftmann@60634
   466
haftmann@60634
   467
lemma normalize_gcd_right [simp]:
haftmann@60634
   468
  "gcd a (normalize b) = gcd a b"
haftmann@60634
   469
  using normalize_gcd_left [of b a] by (simp add: ac_simps)
haftmann@60634
   470
haftmann@60634
   471
lemma gcd_idem: "gcd a a = normalize a"
haftmann@60430
   472
  by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
haftmann@58023
   473
haftmann@60430
   474
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
haftmann@58023
   475
  apply (rule gcdI)
haftmann@58023
   476
  apply (simp add: ac_simps)
haftmann@58023
   477
  apply (rule gcd_dvd2)
haftmann@58023
   478
  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
haftmann@59009
   479
  apply simp
haftmann@58023
   480
  done
haftmann@58023
   481
haftmann@60430
   482
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
haftmann@58023
   483
  apply (rule gcdI)
haftmann@58023
   484
  apply simp
haftmann@58023
   485
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
haftmann@58023
   486
  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
haftmann@59009
   487
  apply simp
haftmann@58023
   488
  done
haftmann@58023
   489
haftmann@58023
   490
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
haftmann@58023
   491
proof
haftmann@58023
   492
  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
haftmann@58023
   493
    by (simp add: fun_eq_iff ac_simps)
haftmann@58023
   494
next
haftmann@58023
   495
  fix a show "gcd a \<circ> gcd a = gcd a"
haftmann@58023
   496
    by (simp add: fun_eq_iff gcd_left_idem)
haftmann@58023
   497
qed
haftmann@58023
   498
haftmann@58023
   499
lemma coprime_dvd_mult:
haftmann@60430
   500
  assumes "gcd c b = 1" and "c dvd a * b"
haftmann@60430
   501
  shows "c dvd a"
haftmann@58023
   502
proof -
haftmann@60634
   503
  let ?nf = "unit_factor"
haftmann@60430
   504
  from assms gcd_mult_distrib [of a c b] 
haftmann@60430
   505
    have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
wenzelm@60526
   506
  from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
haftmann@58023
   507
qed
haftmann@58023
   508
haftmann@58023
   509
lemma coprime_dvd_mult_iff:
haftmann@60430
   510
  "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
haftmann@58023
   511
  by (rule, rule coprime_dvd_mult, simp_all)
haftmann@58023
   512
haftmann@58023
   513
lemma gcd_dvd_antisym:
haftmann@58023
   514
  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
haftmann@58023
   515
proof (rule gcdI)
haftmann@58023
   516
  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
haftmann@58023
   517
  have "gcd c d dvd c" by simp
haftmann@58023
   518
  with A show "gcd a b dvd c" by (rule dvd_trans)
haftmann@58023
   519
  have "gcd c d dvd d" by simp
haftmann@58023
   520
  with A show "gcd a b dvd d" by (rule dvd_trans)
haftmann@60634
   521
  show "unit_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
haftmann@59009
   522
    by simp
haftmann@58023
   523
  fix l assume "l dvd c" and "l dvd d"
haftmann@58023
   524
  hence "l dvd gcd c d" by (rule gcd_greatest)
haftmann@58023
   525
  from this and B show "l dvd gcd a b" by (rule dvd_trans)
haftmann@58023
   526
qed
haftmann@58023
   527
haftmann@58023
   528
lemma gcd_mult_cancel:
haftmann@58023
   529
  assumes "gcd k n = 1"
haftmann@58023
   530
  shows "gcd (k * m) n = gcd m n"
haftmann@58023
   531
proof (rule gcd_dvd_antisym)
haftmann@58023
   532
  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
wenzelm@60526
   533
  also note \<open>gcd k n = 1\<close>
haftmann@58023
   534
  finally have "gcd (gcd (k * m) n) k = 1" by simp
haftmann@58023
   535
  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
haftmann@58023
   536
  moreover have "gcd (k * m) n dvd n" by simp
haftmann@58023
   537
  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
haftmann@58023
   538
  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
haftmann@58023
   539
  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
haftmann@58023
   540
qed
haftmann@58023
   541
haftmann@58023
   542
lemma coprime_crossproduct:
haftmann@58023
   543
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
haftmann@58023
   544
  shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@58023
   545
proof
haftmann@58023
   546
  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
haftmann@58023
   547
next
haftmann@58023
   548
  assume ?lhs
wenzelm@60526
   549
  from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
haftmann@58023
   550
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
wenzelm@60526
   551
  moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
haftmann@58023
   552
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
wenzelm@60526
   553
  moreover from \<open>?lhs\<close> have "c dvd d * b" 
haftmann@59009
   554
    unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   555
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
wenzelm@60526
   556
  moreover from \<open>?lhs\<close> have "d dvd c * a"
haftmann@59009
   557
    unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   558
  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
haftmann@58023
   559
  ultimately show ?rhs unfolding associated_def by simp
haftmann@58023
   560
qed
haftmann@58023
   561
haftmann@58023
   562
lemma gcd_add1 [simp]:
haftmann@58023
   563
  "gcd (m + n) n = gcd m n"
haftmann@58023
   564
  by (cases "n = 0", simp_all add: gcd_non_0)
haftmann@58023
   565
haftmann@58023
   566
lemma gcd_add2 [simp]:
haftmann@58023
   567
  "gcd m (m + n) = gcd m n"
haftmann@58023
   568
  using gcd_add1 [of n m] by (simp add: ac_simps)
haftmann@58023
   569
haftmann@60572
   570
lemma gcd_add_mult:
haftmann@60572
   571
  "gcd m (k * m + n) = gcd m n"
haftmann@60572
   572
proof -
haftmann@60572
   573
  have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
haftmann@60572
   574
    by (fact gcd_mod2)
haftmann@60572
   575
  then show ?thesis by simp 
haftmann@60572
   576
qed
haftmann@58023
   577
haftmann@60430
   578
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
haftmann@58023
   579
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   580
haftmann@58023
   581
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
haftmann@59061
   582
  by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
haftmann@58023
   583
haftmann@58023
   584
lemma div_gcd_coprime:
haftmann@58023
   585
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@58023
   586
  defines [simp]: "d \<equiv> gcd a b"
haftmann@58023
   587
  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
haftmann@58023
   588
  shows "gcd a' b' = 1"
haftmann@58023
   589
proof (rule coprimeI)
haftmann@58023
   590
  fix l assume "l dvd a'" "l dvd b'"
haftmann@58023
   591
  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
haftmann@59009
   592
  moreover have "a = a' * d" "b = b' * d" by simp_all
haftmann@58023
   593
  ultimately have "a = (l * d) * s" "b = (l * d) * t"
haftmann@59009
   594
    by (simp_all only: ac_simps)
haftmann@58023
   595
  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
haftmann@58023
   596
  hence "l*d dvd d" by (simp add: gcd_greatest)
haftmann@59009
   597
  then obtain u where "d = l * d * u" ..
haftmann@59009
   598
  then have "d * (l * u) = d" by (simp add: ac_simps)
haftmann@59009
   599
  moreover from nz have "d \<noteq> 0" by simp
haftmann@59009
   600
  with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
haftmann@59009
   601
  ultimately have "1 = l * u"
wenzelm@60526
   602
    using \<open>d \<noteq> 0\<close> by simp
haftmann@59009
   603
  then show "l dvd 1" ..
haftmann@58023
   604
qed
haftmann@58023
   605
haftmann@58023
   606
lemma coprime_mult: 
haftmann@58023
   607
  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
haftmann@58023
   608
  shows "gcd d (a * b) = 1"
haftmann@58023
   609
  apply (subst gcd.commute)
haftmann@58023
   610
  using da apply (subst gcd_mult_cancel)
haftmann@58023
   611
  apply (subst gcd.commute, assumption)
haftmann@58023
   612
  apply (subst gcd.commute, rule db)
haftmann@58023
   613
  done
haftmann@58023
   614
haftmann@58023
   615
lemma coprime_lmult:
haftmann@58023
   616
  assumes dab: "gcd d (a * b) = 1" 
haftmann@58023
   617
  shows "gcd d a = 1"
haftmann@58023
   618
proof (rule coprimeI)
haftmann@58023
   619
  fix l assume "l dvd d" and "l dvd a"
haftmann@58023
   620
  hence "l dvd a * b" by simp
wenzelm@60526
   621
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   622
qed
haftmann@58023
   623
haftmann@58023
   624
lemma coprime_rmult:
haftmann@58023
   625
  assumes dab: "gcd d (a * b) = 1"
haftmann@58023
   626
  shows "gcd d b = 1"
haftmann@58023
   627
proof (rule coprimeI)
haftmann@58023
   628
  fix l assume "l dvd d" and "l dvd b"
haftmann@58023
   629
  hence "l dvd a * b" by simp
wenzelm@60526
   630
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   631
qed
haftmann@58023
   632
haftmann@58023
   633
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
haftmann@58023
   634
  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
haftmann@58023
   635
haftmann@58023
   636
lemma gcd_coprime:
haftmann@60430
   637
  assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
haftmann@58023
   638
  shows "gcd a' b' = 1"
haftmann@58023
   639
proof -
haftmann@60430
   640
  from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
haftmann@58023
   641
  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
haftmann@58023
   642
  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
haftmann@58023
   643
  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
haftmann@58023
   644
  finally show ?thesis .
haftmann@58023
   645
qed
haftmann@58023
   646
haftmann@58023
   647
lemma coprime_power:
haftmann@58023
   648
  assumes "0 < n"
haftmann@58023
   649
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
haftmann@58023
   650
using assms proof (induct n)
haftmann@58023
   651
  case (Suc n) then show ?case
haftmann@58023
   652
    by (cases n) (simp_all add: coprime_mul_eq)
haftmann@58023
   653
qed simp
haftmann@58023
   654
haftmann@58023
   655
lemma gcd_coprime_exists:
haftmann@58023
   656
  assumes nz: "gcd a b \<noteq> 0"
haftmann@58023
   657
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
haftmann@58023
   658
  apply (rule_tac x = "a div gcd a b" in exI)
haftmann@58023
   659
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@59009
   660
  apply (insert nz, auto intro: div_gcd_coprime)
haftmann@58023
   661
  done
haftmann@58023
   662
haftmann@58023
   663
lemma coprime_exp:
haftmann@58023
   664
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
haftmann@58023
   665
  by (induct n, simp_all add: coprime_mult)
haftmann@58023
   666
haftmann@58023
   667
lemma coprime_exp2 [intro]:
haftmann@58023
   668
  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
haftmann@58023
   669
  apply (rule coprime_exp)
haftmann@58023
   670
  apply (subst gcd.commute)
haftmann@58023
   671
  apply (rule coprime_exp)
haftmann@58023
   672
  apply (subst gcd.commute)
haftmann@58023
   673
  apply assumption
haftmann@58023
   674
  done
haftmann@58023
   675
haftmann@58023
   676
lemma gcd_exp:
haftmann@58023
   677
  "gcd (a^n) (b^n) = (gcd a b) ^ n"
haftmann@58023
   678
proof (cases "a = 0 \<and> b = 0")
haftmann@58023
   679
  assume "a = 0 \<and> b = 0"
haftmann@58023
   680
  then show ?thesis by (cases n, simp_all add: gcd_0_left)
haftmann@58023
   681
next
haftmann@58023
   682
  assume A: "\<not>(a = 0 \<and> b = 0)"
haftmann@58023
   683
  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
haftmann@58023
   684
    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
haftmann@58023
   685
  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
haftmann@58023
   686
  also note gcd_mult_distrib
haftmann@60634
   687
  also have "unit_factor ((gcd a b)^n) = 1"
haftmann@60634
   688
    by (simp add: unit_factor_power A)
haftmann@58023
   689
  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
haftmann@58023
   690
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   691
  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
haftmann@58023
   692
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   693
  finally show ?thesis by simp
haftmann@58023
   694
qed
haftmann@58023
   695
haftmann@58023
   696
lemma coprime_common_divisor: 
haftmann@60430
   697
  "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
haftmann@60430
   698
  apply (subgoal_tac "a dvd gcd a b")
haftmann@59061
   699
  apply simp
haftmann@58023
   700
  apply (erule (1) gcd_greatest)
haftmann@58023
   701
  done
haftmann@58023
   702
haftmann@58023
   703
lemma division_decomp: 
haftmann@58023
   704
  assumes dc: "a dvd b * c"
haftmann@58023
   705
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
haftmann@58023
   706
proof (cases "gcd a b = 0")
haftmann@58023
   707
  assume "gcd a b = 0"
haftmann@59009
   708
  hence "a = 0 \<and> b = 0" by simp
haftmann@58023
   709
  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
haftmann@58023
   710
  then show ?thesis by blast
haftmann@58023
   711
next
haftmann@58023
   712
  let ?d = "gcd a b"
haftmann@58023
   713
  assume "?d \<noteq> 0"
haftmann@58023
   714
  from gcd_coprime_exists[OF this]
haftmann@58023
   715
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   716
    by blast
haftmann@58023
   717
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
haftmann@58023
   718
  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
haftmann@58023
   719
  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
haftmann@58023
   720
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
wenzelm@60526
   721
  with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
haftmann@58023
   722
  with coprime_dvd_mult[OF ab'(3)] 
haftmann@58023
   723
    have "a' dvd c" by (subst (asm) ac_simps, blast)
haftmann@58023
   724
  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
haftmann@58023
   725
  then show ?thesis by blast
haftmann@58023
   726
qed
haftmann@58023
   727
haftmann@60433
   728
lemma pow_divs_pow:
haftmann@58023
   729
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
haftmann@58023
   730
  shows "a dvd b"
haftmann@58023
   731
proof (cases "gcd a b = 0")
haftmann@58023
   732
  assume "gcd a b = 0"
haftmann@59009
   733
  then show ?thesis by simp
haftmann@58023
   734
next
haftmann@58023
   735
  let ?d = "gcd a b"
haftmann@58023
   736
  assume "?d \<noteq> 0"
haftmann@58023
   737
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
wenzelm@60526
   738
  from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
wenzelm@60526
   739
  from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
haftmann@58023
   740
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   741
    by blast
haftmann@58023
   742
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
haftmann@58023
   743
    by (simp add: ab'(1,2)[symmetric])
haftmann@58023
   744
  hence "?d^n * a'^n dvd ?d^n * b'^n"
haftmann@58023
   745
    by (simp only: power_mult_distrib ac_simps)
haftmann@59009
   746
  with zn have "a'^n dvd b'^n" by simp
haftmann@58023
   747
  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
haftmann@58023
   748
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
haftmann@58023
   749
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
haftmann@58023
   750
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
haftmann@58023
   751
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
haftmann@58023
   752
  with ab'(1,2) show ?thesis by simp
haftmann@58023
   753
qed
haftmann@58023
   754
haftmann@60433
   755
lemma pow_divs_eq [simp]:
haftmann@58023
   756
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
haftmann@60433
   757
  by (auto intro: pow_divs_pow dvd_power_same)
haftmann@58023
   758
haftmann@60433
   759
lemma divs_mult:
haftmann@58023
   760
  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
haftmann@58023
   761
  shows "m * n dvd r"
haftmann@58023
   762
proof -
haftmann@58023
   763
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
haftmann@58023
   764
    unfolding dvd_def by blast
haftmann@58023
   765
  from mr n' have "m dvd n'*n" by (simp add: ac_simps)
haftmann@58023
   766
  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
haftmann@58023
   767
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
haftmann@58023
   768
  with n' have "r = m * n * k" by (simp add: mult_ac)
haftmann@58023
   769
  then show ?thesis unfolding dvd_def by blast
haftmann@58023
   770
qed
haftmann@58023
   771
haftmann@58023
   772
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
haftmann@58023
   773
  by (subst add_commute, simp)
haftmann@58023
   774
haftmann@58023
   775
lemma setprod_coprime [rule_format]:
haftmann@60430
   776
  "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
haftmann@58023
   777
  apply (cases "finite A")
haftmann@58023
   778
  apply (induct set: finite)
haftmann@58023
   779
  apply (auto simp add: gcd_mult_cancel)
haftmann@58023
   780
  done
haftmann@58023
   781
haftmann@58023
   782
lemma coprime_divisors: 
haftmann@58023
   783
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
haftmann@58023
   784
  shows "gcd d e = 1" 
haftmann@58023
   785
proof -
haftmann@58023
   786
  from assms obtain k l where "a = d * k" "b = e * l"
haftmann@58023
   787
    unfolding dvd_def by blast
haftmann@58023
   788
  with assms have "gcd (d * k) (e * l) = 1" by simp
haftmann@58023
   789
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
haftmann@58023
   790
  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
haftmann@58023
   791
  finally have "gcd e d = 1" by (rule coprime_lmult)
haftmann@58023
   792
  then show ?thesis by (simp add: ac_simps)
haftmann@58023
   793
qed
haftmann@58023
   794
haftmann@58023
   795
lemma invertible_coprime:
haftmann@60430
   796
  assumes "a * b mod m = 1"
haftmann@60430
   797
  shows "coprime a m"
haftmann@59009
   798
proof -
haftmann@60430
   799
  from assms have "coprime m (a * b mod m)"
haftmann@59009
   800
    by simp
haftmann@60430
   801
  then have "coprime m (a * b)"
haftmann@59009
   802
    by simp
haftmann@60430
   803
  then have "coprime m a"
haftmann@59009
   804
    by (rule coprime_lmult)
haftmann@59009
   805
  then show ?thesis
haftmann@59009
   806
    by (simp add: ac_simps)
haftmann@59009
   807
qed
haftmann@58023
   808
haftmann@58023
   809
lemma lcm_gcd:
haftmann@60634
   810
  "lcm a b = normalize (a * b) div gcd a b"
haftmann@60634
   811
  by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
haftmann@58023
   812
haftmann@58023
   813
lemma lcm_gcd_prod:
haftmann@60634
   814
  "lcm a b * gcd a b = normalize (a * b)"
haftmann@60634
   815
  by (simp add: lcm_gcd)
haftmann@58023
   816
haftmann@58023
   817
lemma lcm_dvd1 [iff]:
haftmann@60430
   818
  "a dvd lcm a b"
haftmann@60430
   819
proof (cases "a*b = 0")
haftmann@60430
   820
  assume "a * b \<noteq> 0"
haftmann@60430
   821
  hence "gcd a b \<noteq> 0" by simp
haftmann@60634
   822
  let ?c = "1 div unit_factor (a * b)"
haftmann@60634
   823
  from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (unit_factor (a * b))" by simp
haftmann@60430
   824
  from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
haftmann@60432
   825
    by (simp add: div_mult_swap unit_div_commute)
haftmann@60430
   826
  hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
wenzelm@60526
   827
  with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
haftmann@58023
   828
    by (subst (asm) div_mult_self2_is_id, simp_all)
haftmann@60430
   829
  also have "... = a * (?c * b div gcd a b)"
haftmann@58023
   830
    by (metis div_mult_swap gcd_dvd2 mult_assoc)
haftmann@58023
   831
  finally show ?thesis by (rule dvdI)
haftmann@58953
   832
qed (auto simp add: lcm_gcd)
haftmann@58023
   833
haftmann@58023
   834
lemma lcm_least:
haftmann@58023
   835
  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
haftmann@58023
   836
proof (cases "k = 0")
haftmann@60634
   837
  let ?nf = unit_factor
haftmann@58023
   838
  assume "k \<noteq> 0"
haftmann@58023
   839
  hence "is_unit (?nf k)" by simp
haftmann@58023
   840
  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
haftmann@58023
   841
  assume A: "a dvd k" "b dvd k"
wenzelm@60526
   842
  hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
haftmann@58023
   843
  from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
haftmann@58023
   844
    unfolding dvd_def by blast
wenzelm@60526
   845
  with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
haftmann@58953
   846
    by auto (drule sym [of 0], simp)
haftmann@58023
   847
  hence "is_unit (?nf (r * s))" by simp
haftmann@58023
   848
  let ?c = "?nf k div ?nf (r*s)"
wenzelm@60526
   849
  from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
haftmann@58023
   850
  hence "?c \<noteq> 0" using not_is_unit_0 by fast 
haftmann@58023
   851
  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
haftmann@58953
   852
    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
haftmann@58023
   853
  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
wenzelm@60526
   854
    by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
wenzelm@60526
   855
  also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
haftmann@58023
   856
    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
haftmann@58023
   857
  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
haftmann@58023
   858
    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
haftmann@58023
   859
  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
haftmann@58023
   860
    by (simp add: algebra_simps)
wenzelm@60526
   861
  hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
haftmann@58023
   862
    by (metis div_mult_self2_is_id)
haftmann@58023
   863
  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
haftmann@58023
   864
    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') 
haftmann@58023
   865
  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
haftmann@58023
   866
    by (simp add: algebra_simps)
wenzelm@60526
   867
  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
haftmann@58023
   868
    by (metis mult.commute div_mult_self2_is_id)
wenzelm@60526
   869
  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
haftmann@58023
   870
    by (metis div_mult_self2_is_id mult_assoc) 
wenzelm@60526
   871
  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
haftmann@58023
   872
    by (simp add: unit_simps)
haftmann@58023
   873
  finally show ?thesis by (rule dvdI)
haftmann@58023
   874
qed simp
haftmann@58023
   875
haftmann@58023
   876
lemma lcm_zero:
haftmann@58023
   877
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
haftmann@58023
   878
proof -
haftmann@60634
   879
  let ?nf = unit_factor
haftmann@58023
   880
  {
haftmann@58023
   881
    assume "a \<noteq> 0" "b \<noteq> 0"
haftmann@58023
   882
    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
wenzelm@60526
   883
    moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
haftmann@58023
   884
    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
haftmann@58023
   885
  } moreover {
haftmann@58023
   886
    assume "a = 0 \<or> b = 0"
haftmann@58023
   887
    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
haftmann@58023
   888
  }
haftmann@58023
   889
  ultimately show ?thesis by blast
haftmann@58023
   890
qed
haftmann@58023
   891
haftmann@58023
   892
lemmas lcm_0_iff = lcm_zero
haftmann@58023
   893
haftmann@58023
   894
lemma gcd_lcm: 
haftmann@58023
   895
  assumes "lcm a b \<noteq> 0"
haftmann@60634
   896
  shows "gcd a b = normalize (a * b) div lcm a b"
haftmann@60634
   897
proof -
haftmann@60634
   898
  have "lcm a b * gcd a b = normalize (a * b)"
haftmann@60634
   899
    by (fact lcm_gcd_prod)
haftmann@60634
   900
  with assms show ?thesis
haftmann@60634
   901
    by (metis nonzero_mult_divide_cancel_left)
haftmann@58023
   902
qed
haftmann@58023
   903
haftmann@60634
   904
lemma unit_factor_lcm [simp]:
haftmann@60634
   905
  "unit_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
haftmann@60634
   906
  by (simp add: dvd_unit_factor_div lcm_gcd)
haftmann@58023
   907
haftmann@60430
   908
lemma lcm_dvd2 [iff]: "b dvd lcm a b"
haftmann@60430
   909
  using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
haftmann@58023
   910
haftmann@58023
   911
lemma lcmI:
haftmann@60634
   912
  assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
haftmann@60634
   913
    and "unit_factor c = (if c = 0 then 0 else 1)"
haftmann@60634
   914
  shows "c = lcm a b"
haftmann@60634
   915
  by (rule associated_eqI) (auto simp: assms associated_def intro: lcm_least)
haftmann@58023
   916
haftmann@58023
   917
sublocale lcm!: abel_semigroup lcm
haftmann@58023
   918
proof
haftmann@60430
   919
  fix a b c
haftmann@60430
   920
  show "lcm (lcm a b) c = lcm a (lcm b c)"
haftmann@58023
   921
  proof (rule lcmI)
haftmann@60430
   922
    have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
haftmann@60430
   923
    then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
haftmann@58023
   924
    
haftmann@60430
   925
    have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
haftmann@60430
   926
    hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
haftmann@60430
   927
    moreover have "c dvd lcm (lcm a b) c" by simp
haftmann@60430
   928
    ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
haftmann@58023
   929
haftmann@60430
   930
    fix l assume "a dvd l" and "lcm b c dvd l"
haftmann@60430
   931
    have "b dvd lcm b c" by simp
wenzelm@60526
   932
    from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
haftmann@60430
   933
    have "c dvd lcm b c" by simp
wenzelm@60526
   934
    from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
wenzelm@60526
   935
    from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
wenzelm@60526
   936
    from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
haftmann@58023
   937
  qed (simp add: lcm_zero)
haftmann@58023
   938
next
haftmann@60430
   939
  fix a b
haftmann@60430
   940
  show "lcm a b = lcm b a"
haftmann@58023
   941
    by (simp add: lcm_gcd ac_simps)
haftmann@58023
   942
qed
haftmann@58023
   943
haftmann@58023
   944
lemma dvd_lcm_D1:
haftmann@58023
   945
  "lcm m n dvd k \<Longrightarrow> m dvd k"
haftmann@58023
   946
  by (rule dvd_trans, rule lcm_dvd1, assumption)
haftmann@58023
   947
haftmann@58023
   948
lemma dvd_lcm_D2:
haftmann@58023
   949
  "lcm m n dvd k \<Longrightarrow> n dvd k"
haftmann@58023
   950
  by (rule dvd_trans, rule lcm_dvd2, assumption)
haftmann@58023
   951
haftmann@58023
   952
lemma gcd_dvd_lcm [simp]:
haftmann@58023
   953
  "gcd a b dvd lcm a b"
haftmann@58023
   954
  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
haftmann@58023
   955
haftmann@58023
   956
lemma lcm_1_iff:
haftmann@58023
   957
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@58023
   958
proof
haftmann@58023
   959
  assume "lcm a b = 1"
haftmann@59061
   960
  then show "is_unit a \<and> is_unit b" by auto
haftmann@58023
   961
next
haftmann@58023
   962
  assume "is_unit a \<and> is_unit b"
haftmann@59061
   963
  hence "a dvd 1" and "b dvd 1" by simp_all
haftmann@59061
   964
  hence "is_unit (lcm a b)" by (rule lcm_least)
haftmann@60634
   965
  hence "lcm a b = unit_factor (lcm a b)"
haftmann@60634
   966
    by (blast intro: sym is_unit_unit_factor)
wenzelm@60526
   967
  also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
haftmann@59061
   968
    by auto
haftmann@58023
   969
  finally show "lcm a b = 1" .
haftmann@58023
   970
qed
haftmann@58023
   971
haftmann@58023
   972
lemma lcm_0_left [simp]:
haftmann@60430
   973
  "lcm 0 a = 0"
haftmann@58023
   974
  by (rule sym, rule lcmI, simp_all)
haftmann@58023
   975
haftmann@58023
   976
lemma lcm_0 [simp]:
haftmann@60430
   977
  "lcm a 0 = 0"
haftmann@58023
   978
  by (rule sym, rule lcmI, simp_all)
haftmann@58023
   979
haftmann@58023
   980
lemma lcm_unique:
haftmann@58023
   981
  "a dvd d \<and> b dvd d \<and> 
haftmann@60634
   982
  unit_factor d = (if d = 0 then 0 else 1) \<and>
haftmann@58023
   983
  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@58023
   984
  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
haftmann@58023
   985
haftmann@58023
   986
lemma dvd_lcm_I1 [simp]:
haftmann@58023
   987
  "k dvd m \<Longrightarrow> k dvd lcm m n"
haftmann@58023
   988
  by (metis lcm_dvd1 dvd_trans)
haftmann@58023
   989
haftmann@58023
   990
lemma dvd_lcm_I2 [simp]:
haftmann@58023
   991
  "k dvd n \<Longrightarrow> k dvd lcm m n"
haftmann@58023
   992
  by (metis lcm_dvd2 dvd_trans)
haftmann@58023
   993
haftmann@58023
   994
lemma lcm_1_left [simp]:
haftmann@60634
   995
  "lcm 1 a = normalize a"
haftmann@60430
   996
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
haftmann@58023
   997
haftmann@58023
   998
lemma lcm_1_right [simp]:
haftmann@60634
   999
  "lcm a 1 = normalize a"
haftmann@60430
  1000
  using lcm_1_left [of a] by (simp add: ac_simps)
haftmann@58023
  1001
haftmann@58023
  1002
lemma lcm_coprime:
haftmann@60634
  1003
  "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
haftmann@58023
  1004
  by (subst lcm_gcd) simp
haftmann@58023
  1005
haftmann@58023
  1006
lemma lcm_proj1_if_dvd: 
haftmann@60634
  1007
  "b dvd a \<Longrightarrow> lcm a b = normalize a"
haftmann@60430
  1008
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
haftmann@58023
  1009
haftmann@58023
  1010
lemma lcm_proj2_if_dvd: 
haftmann@60634
  1011
  "a dvd b \<Longrightarrow> lcm a b = normalize b"
haftmann@60430
  1012
  using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
haftmann@58023
  1013
haftmann@58023
  1014
lemma lcm_proj1_iff:
haftmann@60634
  1015
  "lcm m n = normalize m \<longleftrightarrow> n dvd m"
haftmann@58023
  1016
proof
haftmann@60634
  1017
  assume A: "lcm m n = normalize m"
haftmann@58023
  1018
  show "n dvd m"
haftmann@58023
  1019
  proof (cases "m = 0")
haftmann@58023
  1020
    assume [simp]: "m \<noteq> 0"
haftmann@60634
  1021
    from A have B: "m = lcm m n * unit_factor m"
haftmann@58023
  1022
      by (simp add: unit_eq_div2)
haftmann@58023
  1023
    show ?thesis by (subst B, simp)
haftmann@58023
  1024
  qed simp
haftmann@58023
  1025
next
haftmann@58023
  1026
  assume "n dvd m"
haftmann@60634
  1027
  then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd)
haftmann@58023
  1028
qed
haftmann@58023
  1029
haftmann@58023
  1030
lemma lcm_proj2_iff:
haftmann@60634
  1031
  "lcm m n = normalize n \<longleftrightarrow> m dvd n"
haftmann@58023
  1032
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
haftmann@58023
  1033
haftmann@58023
  1034
lemma euclidean_size_lcm_le1: 
haftmann@58023
  1035
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
  1036
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
  1037
proof -
haftmann@58023
  1038
  have "a dvd lcm a b" by (rule lcm_dvd1)
haftmann@58023
  1039
  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
wenzelm@60526
  1040
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
haftmann@58023
  1041
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
  1042
qed
haftmann@58023
  1043
haftmann@58023
  1044
lemma euclidean_size_lcm_le2:
haftmann@58023
  1045
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
  1046
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
  1047
haftmann@58023
  1048
lemma euclidean_size_lcm_less1:
haftmann@58023
  1049
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
  1050
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
  1051
proof (rule ccontr)
haftmann@58023
  1052
  from assms have "a \<noteq> 0" by auto
haftmann@58023
  1053
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
  1054
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
  1055
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
  1056
  with assms have "lcm a b dvd a" 
haftmann@58023
  1057
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
haftmann@58023
  1058
  hence "b dvd a" by (rule dvd_lcm_D2)
wenzelm@60526
  1059
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
  1060
qed
haftmann@58023
  1061
haftmann@58023
  1062
lemma euclidean_size_lcm_less2:
haftmann@58023
  1063
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
  1064
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
  1065
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
  1066
haftmann@58023
  1067
lemma lcm_mult_unit1:
haftmann@60430
  1068
  "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
haftmann@58023
  1069
  apply (rule lcmI)
haftmann@60430
  1070
  apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
haftmann@58023
  1071
  apply (rule lcm_dvd2)
haftmann@58023
  1072
  apply (rule lcm_least, simp add: unit_simps, assumption)
haftmann@60634
  1073
  apply (subst unit_factor_lcm, simp add: lcm_zero)
haftmann@58023
  1074
  done
haftmann@58023
  1075
haftmann@58023
  1076
lemma lcm_mult_unit2:
haftmann@60430
  1077
  "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
haftmann@60430
  1078
  using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
haftmann@58023
  1079
haftmann@58023
  1080
lemma lcm_div_unit1:
haftmann@60430
  1081
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
haftmann@60433
  1082
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
haftmann@58023
  1083
haftmann@58023
  1084
lemma lcm_div_unit2:
haftmann@60430
  1085
  "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
haftmann@60433
  1086
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
haftmann@58023
  1087
haftmann@60634
  1088
lemma normalize_lcm_left [simp]:
haftmann@60634
  1089
  "lcm (normalize a) b = lcm a b"
haftmann@60634
  1090
proof (cases "a = 0")
haftmann@60634
  1091
  case True then show ?thesis
haftmann@60634
  1092
    by simp
haftmann@60634
  1093
next
haftmann@60634
  1094
  case False then have "is_unit (unit_factor a)"
haftmann@60634
  1095
    by simp
haftmann@60634
  1096
  moreover have "normalize a = a div unit_factor a"
haftmann@60634
  1097
    by simp
haftmann@60634
  1098
  ultimately show ?thesis
haftmann@60634
  1099
    by (simp only: lcm_div_unit1)
haftmann@60634
  1100
qed
haftmann@60634
  1101
haftmann@60634
  1102
lemma normalize_lcm_right [simp]:
haftmann@60634
  1103
  "lcm a (normalize b) = lcm a b"
haftmann@60634
  1104
  using normalize_lcm_left [of b a] by (simp add: ac_simps)
haftmann@60634
  1105
haftmann@58023
  1106
lemma lcm_left_idem:
haftmann@60430
  1107
  "lcm a (lcm a b) = lcm a b"
haftmann@58023
  1108
  apply (rule lcmI)
haftmann@58023
  1109
  apply simp
haftmann@58023
  1110
  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
haftmann@58023
  1111
  apply (rule lcm_least, assumption)
haftmann@58023
  1112
  apply (erule (1) lcm_least)
haftmann@58023
  1113
  apply (auto simp: lcm_zero)
haftmann@58023
  1114
  done
haftmann@58023
  1115
haftmann@58023
  1116
lemma lcm_right_idem:
haftmann@60430
  1117
  "lcm (lcm a b) b = lcm a b"
haftmann@58023
  1118
  apply (rule lcmI)
haftmann@58023
  1119
  apply (subst lcm.assoc, rule lcm_dvd1)
haftmann@58023
  1120
  apply (rule lcm_dvd2)
haftmann@58023
  1121
  apply (rule lcm_least, erule (1) lcm_least, assumption)
haftmann@58023
  1122
  apply (auto simp: lcm_zero)
haftmann@58023
  1123
  done
haftmann@58023
  1124
haftmann@58023
  1125
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
haftmann@58023
  1126
proof
haftmann@58023
  1127
  fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
haftmann@58023
  1128
    by (simp add: fun_eq_iff ac_simps)
haftmann@58023
  1129
next
haftmann@58023
  1130
  fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
haftmann@58023
  1131
    by (intro ext, simp add: lcm_left_idem)
haftmann@58023
  1132
qed
haftmann@58023
  1133
haftmann@60430
  1134
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
haftmann@60634
  1135
  and Lcm_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b"
haftmann@60634
  1136
  and unit_factor_Lcm [simp]: 
haftmann@60634
  1137
          "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
haftmann@58023
  1138
proof -
haftmann@60430
  1139
  have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm A dvd l') \<and>
haftmann@60634
  1140
    unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
haftmann@60430
  1141
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
haftmann@58023
  1142
    case False
haftmann@58023
  1143
    hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
haftmann@58023
  1144
    with False show ?thesis by auto
haftmann@58023
  1145
  next
haftmann@58023
  1146
    case True
haftmann@60430
  1147
    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast
haftmann@60430
  1148
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1149
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1150
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@58023
  1151
      apply (subst n_def)
haftmann@58023
  1152
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
haftmann@58023
  1153
      apply (rule exI[of _ l\<^sub>0])
haftmann@58023
  1154
      apply (simp add: l\<^sub>0_props)
haftmann@58023
  1155
      done
haftmann@60430
  1156
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
haftmann@58023
  1157
      unfolding l_def by simp_all
haftmann@58023
  1158
    {
haftmann@60430
  1159
      fix l' assume "\<forall>a\<in>A. a dvd l'"
wenzelm@60526
  1160
      with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest)
wenzelm@60526
  1161
      moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
haftmann@60430
  1162
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
haftmann@58023
  1163
        by (intro exI[of _ "gcd l l'"], auto)
haftmann@58023
  1164
      hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
haftmann@58023
  1165
      moreover have "euclidean_size (gcd l l') \<le> n"
haftmann@58023
  1166
      proof -
haftmann@58023
  1167
        have "gcd l l' dvd l" by simp
haftmann@58023
  1168
        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
wenzelm@60526
  1169
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
haftmann@58023
  1170
        hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
haftmann@58023
  1171
          by (rule size_mult_mono)
wenzelm@60526
  1172
        also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
wenzelm@60526
  1173
        also note \<open>euclidean_size l = n\<close>
haftmann@58023
  1174
        finally show "euclidean_size (gcd l l') \<le> n" .
haftmann@58023
  1175
      qed
haftmann@58023
  1176
      ultimately have "euclidean_size l = euclidean_size (gcd l l')" 
wenzelm@60526
  1177
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
wenzelm@60526
  1178
      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
haftmann@58023
  1179
      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
haftmann@58023
  1180
    }
haftmann@58023
  1181
haftmann@60634
  1182
    with \<open>(\<forall>a\<in>A. a dvd l)\<close> and unit_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>
haftmann@60634
  1183
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
haftmann@60634
  1184
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
haftmann@60634
  1185
        unit_factor (normalize l) = 
haftmann@60634
  1186
        (if normalize l = 0 then 0 else 1)"
haftmann@58023
  1187
      by (auto simp: unit_simps)
haftmann@60634
  1188
    also from True have "normalize l = Lcm A"
haftmann@58023
  1189
      by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
haftmann@58023
  1190
    finally show ?thesis .
haftmann@58023
  1191
  qed
haftmann@58023
  1192
  note A = this
haftmann@58023
  1193
haftmann@60430
  1194
  {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
haftmann@60634
  1195
  {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm A dvd b" using A by blast}
haftmann@60634
  1196
  from A show "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
haftmann@58023
  1197
qed
haftmann@60634
  1198
haftmann@60634
  1199
lemma normalize_Lcm [simp]:
haftmann@60634
  1200
  "normalize (Lcm A) = Lcm A"
haftmann@60634
  1201
  by (cases "Lcm A = 0") (auto intro: associated_eqI)
haftmann@60634
  1202
haftmann@58023
  1203
lemma LcmI:
haftmann@60634
  1204
  assumes "\<And>a. a \<in> A \<Longrightarrow> a dvd b" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> b dvd c"
haftmann@60634
  1205
    and "unit_factor b = (if b = 0 then 0 else 1)" shows "b = Lcm A"
haftmann@60634
  1206
  by (rule associated_eqI) (auto simp: assms associated_def intro: Lcm_least)
haftmann@58023
  1207
haftmann@58023
  1208
lemma Lcm_subset:
haftmann@58023
  1209
  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
haftmann@60634
  1210
  by (blast intro: Lcm_least dvd_Lcm)
haftmann@58023
  1211
haftmann@58023
  1212
lemma Lcm_Un:
haftmann@58023
  1213
  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
haftmann@58023
  1214
  apply (rule lcmI)
haftmann@58023
  1215
  apply (blast intro: Lcm_subset)
haftmann@58023
  1216
  apply (blast intro: Lcm_subset)
haftmann@60634
  1217
  apply (intro Lcm_least ballI, elim UnE)
haftmann@58023
  1218
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
  1219
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
  1220
  apply simp
haftmann@58023
  1221
  done
haftmann@58023
  1222
haftmann@58023
  1223
lemma Lcm_1_iff:
haftmann@60430
  1224
  "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
haftmann@58023
  1225
proof
haftmann@58023
  1226
  assume "Lcm A = 1"
haftmann@60430
  1227
  then show "\<forall>a\<in>A. is_unit a" by auto
haftmann@58023
  1228
qed (rule LcmI [symmetric], auto)
haftmann@58023
  1229
haftmann@58023
  1230
lemma Lcm_no_units:
haftmann@60430
  1231
  "Lcm A = Lcm (A - {a. is_unit a})"
haftmann@58023
  1232
proof -
haftmann@60430
  1233
  have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
haftmann@60430
  1234
  hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
haftmann@60634
  1235
    by (simp add: Lcm_Un [symmetric])
haftmann@60430
  1236
  also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
haftmann@58023
  1237
  finally show ?thesis by simp
haftmann@58023
  1238
qed
haftmann@58023
  1239
haftmann@58023
  1240
lemma Lcm_empty [simp]:
haftmann@58023
  1241
  "Lcm {} = 1"
haftmann@58023
  1242
  by (simp add: Lcm_1_iff)
haftmann@58023
  1243
haftmann@58023
  1244
lemma Lcm_eq_0 [simp]:
haftmann@58023
  1245
  "0 \<in> A \<Longrightarrow> Lcm A = 0"
haftmann@58023
  1246
  by (drule dvd_Lcm) simp
haftmann@58023
  1247
haftmann@58023
  1248
lemma Lcm0_iff':
haftmann@60430
  1249
  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
  1250
proof
haftmann@58023
  1251
  assume "Lcm A = 0"
haftmann@60430
  1252
  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
  1253
  proof
haftmann@60430
  1254
    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
haftmann@60430
  1255
    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast
haftmann@60430
  1256
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1257
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1258
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@58023
  1259
      apply (subst n_def)
haftmann@58023
  1260
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
haftmann@58023
  1261
      apply (rule exI[of _ l\<^sub>0])
haftmann@58023
  1262
      apply (simp add: l\<^sub>0_props)
haftmann@58023
  1263
      done
haftmann@58023
  1264
    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
haftmann@60634
  1265
    hence "normalize l \<noteq> 0" by simp
haftmann@60634
  1266
    also from ex have "normalize l = Lcm A"
haftmann@58023
  1267
       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
wenzelm@60526
  1268
    finally show False using \<open>Lcm A = 0\<close> by contradiction
haftmann@58023
  1269
  qed
haftmann@58023
  1270
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
  1271
haftmann@58023
  1272
lemma Lcm0_iff [simp]:
haftmann@58023
  1273
  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
haftmann@58023
  1274
proof -
haftmann@58023
  1275
  assume "finite A"
haftmann@58023
  1276
  have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
haftmann@58023
  1277
  moreover {
haftmann@58023
  1278
    assume "0 \<notin> A"
haftmann@58023
  1279
    hence "\<Prod>A \<noteq> 0" 
wenzelm@60526
  1280
      apply (induct rule: finite_induct[OF \<open>finite A\<close>]) 
haftmann@58023
  1281
      apply simp
haftmann@58023
  1282
      apply (subst setprod.insert, assumption, assumption)
haftmann@58023
  1283
      apply (rule no_zero_divisors)
haftmann@58023
  1284
      apply blast+
haftmann@58023
  1285
      done
wenzelm@60526
  1286
    moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
haftmann@60430
  1287
    ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
haftmann@58023
  1288
    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
haftmann@58023
  1289
  }
haftmann@58023
  1290
  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
haftmann@58023
  1291
qed
haftmann@58023
  1292
haftmann@58023
  1293
lemma Lcm_no_multiple:
haftmann@60430
  1294
  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
haftmann@58023
  1295
proof -
haftmann@60430
  1296
  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
haftmann@60430
  1297
  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
haftmann@58023
  1298
  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
  1299
qed
haftmann@58023
  1300
haftmann@58023
  1301
lemma Lcm_insert [simp]:
haftmann@58023
  1302
  "Lcm (insert a A) = lcm a (Lcm A)"
haftmann@58023
  1303
proof (rule lcmI)
haftmann@58023
  1304
  fix l assume "a dvd l" and "Lcm A dvd l"
haftmann@60430
  1305
  hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
haftmann@60634
  1306
  with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_least)
haftmann@60634
  1307
qed (auto intro: Lcm_least dvd_Lcm)
haftmann@58023
  1308
 
haftmann@58023
  1309
lemma Lcm_finite:
haftmann@58023
  1310
  assumes "finite A"
haftmann@58023
  1311
  shows "Lcm A = Finite_Set.fold lcm 1 A"
wenzelm@60526
  1312
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
  1313
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
haftmann@58023
  1314
haftmann@60431
  1315
lemma Lcm_set [code_unfold]:
haftmann@58023
  1316
  "Lcm (set xs) = fold lcm xs 1"
haftmann@58023
  1317
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
haftmann@58023
  1318
haftmann@58023
  1319
lemma Lcm_singleton [simp]:
haftmann@60634
  1320
  "Lcm {a} = normalize a"
haftmann@58023
  1321
  by simp
haftmann@58023
  1322
haftmann@58023
  1323
lemma Lcm_2 [simp]:
haftmann@58023
  1324
  "Lcm {a,b} = lcm a b"
haftmann@60634
  1325
  by simp
haftmann@58023
  1326
haftmann@58023
  1327
lemma Lcm_coprime:
haftmann@58023
  1328
  assumes "finite A" and "A \<noteq> {}" 
haftmann@58023
  1329
  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
haftmann@60634
  1330
  shows "Lcm A = normalize (\<Prod>A)"
haftmann@58023
  1331
using assms proof (induct rule: finite_ne_induct)
haftmann@58023
  1332
  case (insert a A)
haftmann@58023
  1333
  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
haftmann@60634
  1334
  also from insert have "Lcm A = normalize (\<Prod>A)" by blast
haftmann@58023
  1335
  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
haftmann@58023
  1336
  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
haftmann@60634
  1337
  with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"
haftmann@58023
  1338
    by (simp add: lcm_coprime)
haftmann@58023
  1339
  finally show ?case .
haftmann@58023
  1340
qed simp
haftmann@58023
  1341
      
haftmann@58023
  1342
lemma Lcm_coprime':
haftmann@58023
  1343
  "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
haftmann@60634
  1344
    \<Longrightarrow> Lcm A = normalize (\<Prod>A)"
haftmann@58023
  1345
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
haftmann@58023
  1346
haftmann@58023
  1347
lemma Gcd_Lcm:
haftmann@60430
  1348
  "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
  1349
  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
haftmann@58023
  1350
haftmann@60430
  1351
lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
haftmann@60634
  1352
  and Gcd_greatest: "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A"
haftmann@60634
  1353
  and unit_factor_Gcd [simp]: 
haftmann@60634
  1354
    "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
haftmann@58023
  1355
proof -
haftmann@60430
  1356
  fix a assume "a \<in> A"
haftmann@60634
  1357
  hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_least) blast
haftmann@60430
  1358
  then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
haftmann@58023
  1359
next
haftmann@60634
  1360
  fix g' assume "\<And>a. a \<in> A \<Longrightarrow> g' dvd a"
haftmann@60430
  1361
  hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
haftmann@58023
  1362
  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
haftmann@58023
  1363
next
haftmann@60634
  1364
  show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
haftmann@59009
  1365
    by (simp add: Gcd_Lcm)
haftmann@58023
  1366
qed
haftmann@58023
  1367
haftmann@60634
  1368
lemma normalize_Gcd [simp]:
haftmann@60634
  1369
  "normalize (Gcd A) = Gcd A"
haftmann@60634
  1370
  by (cases "Gcd A = 0") (auto intro: associated_eqI)
haftmann@60634
  1371
haftmann@58023
  1372
lemma GcdI:
haftmann@60634
  1373
  assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
haftmann@60634
  1374
    and "unit_factor b = (if b = 0 then 0 else 1)"
haftmann@60634
  1375
  shows "b = Gcd A"
haftmann@60634
  1376
  by (rule associated_eqI) (auto simp: assms associated_def intro: Gcd_greatest)
haftmann@58023
  1377
haftmann@58023
  1378
lemma Lcm_Gcd:
haftmann@60430
  1379
  "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
haftmann@60634
  1380
  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest)
haftmann@58023
  1381
haftmann@58023
  1382
lemma Gcd_0_iff:
haftmann@58023
  1383
  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
haftmann@58023
  1384
  apply (rule iffI)
haftmann@58023
  1385
  apply (rule subsetI, drule Gcd_dvd, simp)
haftmann@58023
  1386
  apply (auto intro: GcdI[symmetric])
haftmann@58023
  1387
  done
haftmann@58023
  1388
haftmann@58023
  1389
lemma Gcd_empty [simp]:
haftmann@58023
  1390
  "Gcd {} = 0"
haftmann@58023
  1391
  by (simp add: Gcd_0_iff)
haftmann@58023
  1392
haftmann@58023
  1393
lemma Gcd_1:
haftmann@58023
  1394
  "1 \<in> A \<Longrightarrow> Gcd A = 1"
haftmann@58023
  1395
  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
haftmann@58023
  1396
haftmann@58023
  1397
lemma Gcd_insert [simp]:
haftmann@58023
  1398
  "Gcd (insert a A) = gcd a (Gcd A)"
haftmann@58023
  1399
proof (rule gcdI)
haftmann@58023
  1400
  fix l assume "l dvd a" and "l dvd Gcd A"
haftmann@60430
  1401
  hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
haftmann@60634
  1402
  with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd Gcd_greatest)
haftmann@60634
  1403
qed (auto intro: Gcd_greatest)
haftmann@58023
  1404
haftmann@58023
  1405
lemma Gcd_finite:
haftmann@58023
  1406
  assumes "finite A"
haftmann@58023
  1407
  shows "Gcd A = Finite_Set.fold gcd 0 A"
wenzelm@60526
  1408
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
  1409
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
haftmann@58023
  1410
haftmann@60431
  1411
lemma Gcd_set [code_unfold]:
haftmann@58023
  1412
  "Gcd (set xs) = fold gcd xs 0"
haftmann@58023
  1413
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
haftmann@58023
  1414
haftmann@60634
  1415
lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
haftmann@58023
  1416
  by (simp add: gcd_0)
haftmann@58023
  1417
haftmann@58023
  1418
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
haftmann@60634
  1419
  by (simp add: gcd_0)
haftmann@58023
  1420
haftmann@60439
  1421
subclass semiring_gcd
haftmann@60439
  1422
  by unfold_locales (simp_all add: gcd_greatest_iff)
haftmann@60439
  1423
  
haftmann@58023
  1424
end
haftmann@58023
  1425
wenzelm@60526
  1426
text \<open>
haftmann@58023
  1427
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
haftmann@58023
  1428
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
wenzelm@60526
  1429
\<close>
haftmann@58023
  1430
haftmann@58023
  1431
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
  1432
begin
haftmann@58023
  1433
haftmann@58023
  1434
subclass euclidean_ring ..
haftmann@58023
  1435
haftmann@60439
  1436
subclass ring_gcd ..
haftmann@60439
  1437
haftmann@60572
  1438
lemma euclid_ext_gcd [simp]:
haftmann@60572
  1439
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
haftmann@60572
  1440
  by (induct a b rule: gcd_eucl_induct)
haftmann@60572
  1441
    (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
haftmann@60572
  1442
haftmann@60572
  1443
lemma euclid_ext_gcd' [simp]:
haftmann@60572
  1444
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
haftmann@60572
  1445
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
haftmann@60572
  1446
  
haftmann@60572
  1447
lemma euclid_ext'_correct:
haftmann@60572
  1448
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
haftmann@60572
  1449
proof-
haftmann@60572
  1450
  obtain s t c where "euclid_ext a b = (s,t,c)"
haftmann@60572
  1451
    by (cases "euclid_ext a b", blast)
haftmann@60572
  1452
  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
haftmann@60572
  1453
    show ?thesis unfolding euclid_ext'_def by simp
haftmann@60572
  1454
qed
haftmann@60572
  1455
haftmann@60572
  1456
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
haftmann@60572
  1457
  using euclid_ext'_correct by blast
haftmann@60572
  1458
haftmann@58023
  1459
lemma gcd_neg1 [simp]:
haftmann@60430
  1460
  "gcd (-a) b = gcd a b"
haftmann@59009
  1461
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1462
haftmann@58023
  1463
lemma gcd_neg2 [simp]:
haftmann@60430
  1464
  "gcd a (-b) = gcd a b"
haftmann@59009
  1465
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1466
haftmann@58023
  1467
lemma gcd_neg_numeral_1 [simp]:
haftmann@60430
  1468
  "gcd (- numeral n) a = gcd (numeral n) a"
haftmann@58023
  1469
  by (fact gcd_neg1)
haftmann@58023
  1470
haftmann@58023
  1471
lemma gcd_neg_numeral_2 [simp]:
haftmann@60430
  1472
  "gcd a (- numeral n) = gcd a (numeral n)"
haftmann@58023
  1473
  by (fact gcd_neg2)
haftmann@58023
  1474
haftmann@58023
  1475
lemma gcd_diff1: "gcd (m - n) n = gcd m n"
haftmann@58023
  1476
  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
haftmann@58023
  1477
haftmann@58023
  1478
lemma gcd_diff2: "gcd (n - m) n = gcd m n"
haftmann@58023
  1479
  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
haftmann@58023
  1480
haftmann@58023
  1481
lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
haftmann@58023
  1482
proof -
haftmann@58023
  1483
  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
haftmann@58023
  1484
  also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
haftmann@58023
  1485
  also have "\<dots> = 1" by (rule coprime_plus_one)
haftmann@58023
  1486
  finally show ?thesis .
haftmann@58023
  1487
qed
haftmann@58023
  1488
haftmann@60430
  1489
lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
haftmann@58023
  1490
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1491
haftmann@60430
  1492
lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
haftmann@58023
  1493
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1494
haftmann@60430
  1495
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
haftmann@58023
  1496
  by (fact lcm_neg1)
haftmann@58023
  1497
haftmann@60430
  1498
lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
haftmann@58023
  1499
  by (fact lcm_neg2)
haftmann@58023
  1500
haftmann@60572
  1501
end
haftmann@58023
  1502
haftmann@58023
  1503
haftmann@60572
  1504
subsection \<open>Typical instances\<close>
haftmann@58023
  1505
haftmann@58023
  1506
instantiation nat :: euclidean_semiring
haftmann@58023
  1507
begin
haftmann@58023
  1508
haftmann@58023
  1509
definition [simp]:
haftmann@58023
  1510
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
haftmann@58023
  1511
haftmann@58023
  1512
definition [simp]:
haftmann@60634
  1513
  "unit_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
haftmann@58023
  1514
haftmann@58023
  1515
instance proof
haftmann@59061
  1516
qed simp_all
haftmann@58023
  1517
haftmann@58023
  1518
end
haftmann@58023
  1519
haftmann@58023
  1520
instantiation int :: euclidean_ring
haftmann@58023
  1521
begin
haftmann@58023
  1522
haftmann@58023
  1523
definition [simp]:
haftmann@58023
  1524
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
haftmann@58023
  1525
haftmann@58023
  1526
definition [simp]:
haftmann@60634
  1527
  "unit_factor_int = (sgn :: int \<Rightarrow> int)"
haftmann@58023
  1528
wenzelm@60580
  1529
instance
haftmann@60634
  1530
by standard (auto simp add: abs_mult nat_mult_distrib sgn_times split: abs_split)
haftmann@58023
  1531
haftmann@58023
  1532
end
haftmann@58023
  1533
haftmann@60572
  1534
instantiation poly :: (field) euclidean_ring
haftmann@60571
  1535
begin
haftmann@60571
  1536
haftmann@60571
  1537
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
haftmann@60600
  1538
  where "euclidean_size p = (if p = 0 then 0 else Suc (degree p))"
haftmann@60571
  1539
haftmann@60634
  1540
lemma euclidenan_size_poly_minus_one_degree [simp]:
haftmann@60634
  1541
  "euclidean_size p - 1 = degree p"
haftmann@60634
  1542
  by (simp add: euclidean_size_poly_def)
haftmann@60571
  1543
haftmann@60600
  1544
lemma euclidean_size_poly_0 [simp]:
haftmann@60600
  1545
  "euclidean_size (0::'a poly) = 0"
haftmann@60600
  1546
  by (simp add: euclidean_size_poly_def)
haftmann@60600
  1547
haftmann@60600
  1548
lemma euclidean_size_poly_not_0 [simp]:
haftmann@60600
  1549
  "p \<noteq> 0 \<Longrightarrow> euclidean_size p = Suc (degree p)"
haftmann@60600
  1550
  by (simp add: euclidean_size_poly_def)
haftmann@60600
  1551
haftmann@60571
  1552
instance
haftmann@60600
  1553
proof
haftmann@60571
  1554
  fix p q :: "'a poly"
haftmann@60600
  1555
  assume "q \<noteq> 0"
haftmann@60600
  1556
  then have "p mod q = 0 \<or> degree (p mod q) < degree q"
haftmann@60600
  1557
    by (rule degree_mod_less [of q p])  
haftmann@60600
  1558
  with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
haftmann@60600
  1559
    by (cases "p mod q = 0") simp_all
haftmann@60571
  1560
next
haftmann@60571
  1561
  fix p q :: "'a poly"
haftmann@60571
  1562
  assume "q \<noteq> 0"
haftmann@60600
  1563
  from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
haftmann@60571
  1564
    by (rule degree_mult_right_le)
haftmann@60600
  1565
  with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
haftmann@60600
  1566
    by (cases "p = 0") simp_all
haftmann@60571
  1567
qed
haftmann@60571
  1568
haftmann@58023
  1569
end
haftmann@60571
  1570
haftmann@60571
  1571
end