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