src/HOL/Number_Theory/Euclidean_Algorithm.thy
author Manuel Eberl <eberlm@in.tum.de>
Fri Feb 26 18:33:01 2016 +0100 (2016-02-26)
changeset 62428 4d5fbec92bb1
parent 62425 d0936b500bf5
child 62429 25271ff79171
permissions -rw-r--r--
Fixed code equations for Gcd/Lcm
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@60685
     6
imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/Polynomial"
haftmann@58023
     7
begin
haftmann@60634
     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@58023
    16
  \end{itemize}
haftmann@58023
    17
  The existence of these functions makes it possible to derive gcd and lcm functions 
haftmann@58023
    18
  for any Euclidean semiring.
wenzelm@60526
    19
\<close> 
haftmann@60634
    20
class euclidean_semiring = semiring_div + normalization_semidom + 
haftmann@58023
    21
  fixes euclidean_size :: "'a \<Rightarrow> nat"
eberlm@62422
    22
  assumes size_0 [simp]: "euclidean_size 0 = 0"
haftmann@60569
    23
  assumes mod_size_less: 
haftmann@60600
    24
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
haftmann@58023
    25
  assumes size_mult_mono:
haftmann@60634
    26
    "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
haftmann@58023
    27
begin
haftmann@58023
    28
haftmann@58023
    29
lemma euclidean_division:
haftmann@58023
    30
  fixes a :: 'a and b :: 'a
haftmann@60600
    31
  assumes "b \<noteq> 0"
haftmann@58023
    32
  obtains s and t where "a = s * b + t" 
haftmann@58023
    33
    and "euclidean_size t < euclidean_size b"
haftmann@58023
    34
proof -
haftmann@60569
    35
  from div_mod_equality [of a b 0] 
haftmann@58023
    36
     have "a = a div b * b + a mod b" by simp
haftmann@60569
    37
  with that and assms show ?thesis by (auto simp add: mod_size_less)
haftmann@58023
    38
qed
haftmann@58023
    39
haftmann@58023
    40
lemma dvd_euclidean_size_eq_imp_dvd:
haftmann@58023
    41
  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
haftmann@58023
    42
  shows "a dvd b"
haftmann@60569
    43
proof (rule ccontr)
haftmann@60569
    44
  assume "\<not> a dvd b"
haftmann@60569
    45
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
haftmann@58023
    46
  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
haftmann@58023
    47
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
wenzelm@60526
    48
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
wenzelm@60526
    49
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
haftmann@58023
    50
      using size_mult_mono by force
haftmann@60569
    51
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
haftmann@60569
    52
  have "euclidean_size (b mod a) < euclidean_size a"
haftmann@58023
    53
      using mod_size_less by blast
haftmann@58023
    54
  ultimately show False using size_eq by simp
haftmann@58023
    55
qed
haftmann@58023
    56
haftmann@58023
    57
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
    58
where
haftmann@60634
    59
  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
haftmann@60572
    60
  by pat_completeness simp
haftmann@60569
    61
termination
haftmann@60569
    62
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
    63
haftmann@58023
    64
declare gcd_eucl.simps [simp del]
haftmann@58023
    65
haftmann@60569
    66
lemma gcd_eucl_induct [case_names zero mod]:
haftmann@60569
    67
  assumes H1: "\<And>b. P b 0"
haftmann@60569
    68
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
    69
  shows "P a b"
haftmann@58023
    70
proof (induct a b rule: gcd_eucl.induct)
haftmann@60569
    71
  case ("1" a b)
haftmann@60569
    72
  show ?case
haftmann@60569
    73
  proof (cases "b = 0")
haftmann@60569
    74
    case True then show "P a b" by simp (rule H1)
haftmann@60569
    75
  next
haftmann@60569
    76
    case False
haftmann@60600
    77
    then have "P b (a mod b)"
haftmann@60600
    78
      by (rule "1.hyps")
haftmann@60569
    79
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
    80
      by (blast intro: H2)
haftmann@60569
    81
  qed
haftmann@58023
    82
qed
haftmann@58023
    83
haftmann@58023
    84
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
    85
where
haftmann@60634
    86
  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
haftmann@58023
    87
haftmann@60572
    88
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
haftmann@60572
    89
  Somewhat complicated definition of Lcm that has the advantage of working
haftmann@60572
    90
  for infinite sets as well\<close>
haftmann@58023
    91
where
haftmann@60430
    92
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
    93
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
    94
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60634
    95
       in normalize l 
haftmann@58023
    96
      else 0)"
haftmann@58023
    97
haftmann@58023
    98
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
haftmann@58023
    99
where
haftmann@58023
   100
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
   101
eberlm@62428
   102
declare Lcm_eucl_def Gcd_eucl_def [code del]
eberlm@62428
   103
haftmann@60572
   104
lemma gcd_eucl_0:
haftmann@60634
   105
  "gcd_eucl a 0 = normalize a"
haftmann@60572
   106
  by (simp add: gcd_eucl.simps [of a 0])
haftmann@60572
   107
haftmann@60572
   108
lemma gcd_eucl_0_left:
haftmann@60634
   109
  "gcd_eucl 0 a = normalize a"
haftmann@60600
   110
  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
haftmann@60572
   111
haftmann@60572
   112
lemma gcd_eucl_non_0:
haftmann@60572
   113
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
haftmann@60600
   114
  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
haftmann@60572
   115
eberlm@62422
   116
lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
eberlm@62422
   117
  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
eberlm@62422
   118
  by (induct a b rule: gcd_eucl_induct)
eberlm@62422
   119
     (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   120
eberlm@62422
   121
lemma normalize_gcd_eucl [simp]:
eberlm@62422
   122
  "normalize (gcd_eucl a b) = gcd_eucl a b"
eberlm@62422
   123
  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
eberlm@62422
   124
     
eberlm@62422
   125
lemma gcd_eucl_greatest:
eberlm@62422
   126
  fixes k a b :: 'a
eberlm@62422
   127
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
eberlm@62422
   128
proof (induct a b rule: gcd_eucl_induct)
eberlm@62422
   129
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
eberlm@62422
   130
next
eberlm@62422
   131
  case (mod a b)
eberlm@62422
   132
  then show ?case
eberlm@62422
   133
    by (simp add: gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   134
qed
eberlm@62422
   135
eberlm@62422
   136
lemma eq_gcd_euclI:
eberlm@62422
   137
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
eberlm@62422
   138
  assumes "\<And>a b. gcd a b dvd a" "\<And>a b. gcd a b dvd b" "\<And>a b. normalize (gcd a b) = gcd a b"
eberlm@62422
   139
          "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
eberlm@62422
   140
  shows   "gcd = gcd_eucl"
eberlm@62422
   141
  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
eberlm@62422
   142
eberlm@62422
   143
lemma gcd_eucl_zero [simp]:
eberlm@62422
   144
  "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
eberlm@62422
   145
  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
eberlm@62422
   146
eberlm@62422
   147
  
eberlm@62422
   148
lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
eberlm@62422
   149
  and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
eberlm@62422
   150
  and unit_factor_Lcm_eucl [simp]: 
eberlm@62422
   151
          "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
eberlm@62422
   152
proof -
eberlm@62422
   153
  have "(\<forall>a\<in>A. a dvd Lcm_eucl A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm_eucl A dvd l') \<and>
eberlm@62422
   154
    unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
eberlm@62422
   155
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
eberlm@62422
   156
    case False
eberlm@62422
   157
    hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
eberlm@62422
   158
    with False show ?thesis by auto
eberlm@62422
   159
  next
eberlm@62422
   160
    case True
eberlm@62422
   161
    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
eberlm@62422
   162
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   163
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   164
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   165
      apply (subst n_def)
eberlm@62422
   166
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
eberlm@62422
   167
      apply (rule exI[of _ l\<^sub>0])
eberlm@62422
   168
      apply (simp add: l\<^sub>0_props)
eberlm@62422
   169
      done
eberlm@62422
   170
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
eberlm@62422
   171
      unfolding l_def by simp_all
eberlm@62422
   172
    {
eberlm@62422
   173
      fix l' assume "\<forall>a\<in>A. a dvd l'"
eberlm@62422
   174
      with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd_eucl l l'" by (auto intro: gcd_eucl_greatest)
eberlm@62422
   175
      moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
eberlm@62422
   176
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
eberlm@62422
   177
                          euclidean_size b = euclidean_size (gcd_eucl l l')"
eberlm@62422
   178
        by (intro exI[of _ "gcd_eucl l l'"], auto)
eberlm@62422
   179
      hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
eberlm@62422
   180
      moreover have "euclidean_size (gcd_eucl l l') \<le> n"
eberlm@62422
   181
      proof -
eberlm@62422
   182
        have "gcd_eucl l l' dvd l" by simp
eberlm@62422
   183
        then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
eberlm@62422
   184
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
eberlm@62422
   185
        hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
eberlm@62422
   186
          by (rule size_mult_mono)
eberlm@62422
   187
        also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
eberlm@62422
   188
        also note \<open>euclidean_size l = n\<close>
eberlm@62422
   189
        finally show "euclidean_size (gcd_eucl l l') \<le> n" .
eberlm@62422
   190
      qed
eberlm@62422
   191
      ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')" 
eberlm@62422
   192
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
eberlm@62422
   193
      from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
eberlm@62422
   194
        by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
eberlm@62422
   195
      hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
eberlm@62422
   196
    }
eberlm@62422
   197
eberlm@62422
   198
    with \<open>(\<forall>a\<in>A. a dvd l)\<close> and unit_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>
eberlm@62422
   199
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
eberlm@62422
   200
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
eberlm@62422
   201
        unit_factor (normalize l) = 
eberlm@62422
   202
        (if normalize l = 0 then 0 else 1)"
eberlm@62422
   203
      by (auto simp: unit_simps)
eberlm@62422
   204
    also from True have "normalize l = Lcm_eucl A"
eberlm@62422
   205
      by (simp add: Lcm_eucl_def Let_def n_def l_def)
eberlm@62422
   206
    finally show ?thesis .
eberlm@62422
   207
  qed
eberlm@62422
   208
  note A = this
eberlm@62422
   209
eberlm@62422
   210
  {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
eberlm@62422
   211
  {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}
eberlm@62422
   212
  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
eberlm@62422
   213
qed
eberlm@62422
   214
  
eberlm@62422
   215
lemma normalize_Lcm_eucl [simp]:
eberlm@62422
   216
  "normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   217
proof (cases "Lcm_eucl A = 0")
eberlm@62422
   218
  case True then show ?thesis by simp
eberlm@62422
   219
next
eberlm@62422
   220
  case False
eberlm@62422
   221
  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   222
    by (fact unit_factor_mult_normalize)
eberlm@62422
   223
  with False show ?thesis by simp
eberlm@62422
   224
qed
eberlm@62422
   225
eberlm@62422
   226
lemma eq_Lcm_euclI:
eberlm@62422
   227
  fixes lcm :: "'a set \<Rightarrow> 'a"
eberlm@62422
   228
  assumes "\<And>A a. a \<in> A \<Longrightarrow> a dvd lcm A" and "\<And>A c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> lcm A dvd c"
eberlm@62422
   229
          "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
eberlm@62422
   230
  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  
eberlm@62422
   231
haftmann@58023
   232
end
haftmann@58023
   233
haftmann@60598
   234
class euclidean_ring = euclidean_semiring + idom
haftmann@60598
   235
begin
haftmann@60598
   236
haftmann@60598
   237
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
haftmann@60598
   238
  "euclid_ext a b = 
haftmann@60598
   239
     (if b = 0 then 
haftmann@60634
   240
        (1 div unit_factor a, 0, normalize a)
haftmann@60598
   241
      else
haftmann@60598
   242
        case euclid_ext b (a mod b) of
haftmann@60598
   243
            (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60598
   244
  by pat_completeness simp
haftmann@60598
   245
termination
haftmann@60598
   246
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@60598
   247
haftmann@60598
   248
declare euclid_ext.simps [simp del]
haftmann@60598
   249
haftmann@60598
   250
lemma euclid_ext_0: 
haftmann@60634
   251
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
haftmann@60598
   252
  by (simp add: euclid_ext.simps [of a 0])
haftmann@60598
   253
haftmann@60598
   254
lemma euclid_ext_left_0: 
haftmann@60634
   255
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
haftmann@60600
   256
  by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
haftmann@60598
   257
haftmann@60598
   258
lemma euclid_ext_non_0: 
haftmann@60598
   259
  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
haftmann@60598
   260
    (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60600
   261
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   262
haftmann@60598
   263
lemma euclid_ext_code [code]:
haftmann@60634
   264
  "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)
haftmann@60598
   265
    else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
haftmann@60598
   266
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   267
haftmann@60598
   268
lemma euclid_ext_correct:
haftmann@60598
   269
  "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
haftmann@60598
   270
proof (induct a b rule: gcd_eucl_induct)
haftmann@60598
   271
  case (zero a) then show ?case
haftmann@60598
   272
    by (simp add: euclid_ext_0 ac_simps)
haftmann@60598
   273
next
haftmann@60598
   274
  case (mod a b)
haftmann@60598
   275
  obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
haftmann@60598
   276
    by (cases "euclid_ext b (a mod b)") blast
haftmann@60598
   277
  with mod have "c = s * b + t * (a mod b)" by simp
haftmann@60598
   278
  also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
haftmann@60598
   279
    by (simp add: algebra_simps) 
haftmann@60598
   280
  also have "(a div b) * b + a mod b = a" using mod_div_equality .
haftmann@60598
   281
  finally show ?case
haftmann@60598
   282
    by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
haftmann@60598
   283
qed
haftmann@60598
   284
haftmann@60598
   285
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@60598
   286
where
haftmann@60598
   287
  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
haftmann@60598
   288
haftmann@60634
   289
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
haftmann@60598
   290
  by (simp add: euclid_ext'_def euclid_ext_0)
haftmann@60598
   291
haftmann@60634
   292
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
haftmann@60598
   293
  by (simp add: euclid_ext'_def euclid_ext_left_0)
haftmann@60598
   294
  
haftmann@60598
   295
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
haftmann@60598
   296
  fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
haftmann@60598
   297
  by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
haftmann@60598
   298
haftmann@60598
   299
end
haftmann@60598
   300
haftmann@58023
   301
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@58023
   302
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
haftmann@58023
   303
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
haftmann@58023
   304
begin
haftmann@58023
   305
eberlm@62422
   306
subclass semiring_gcd
eberlm@62422
   307
  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
haftmann@58023
   308
eberlm@62422
   309
subclass semiring_Gcd
eberlm@62422
   310
  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
eberlm@62422
   311
  
haftmann@58023
   312
haftmann@58023
   313
lemma gcd_non_0:
haftmann@60430
   314
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
haftmann@60572
   315
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
haftmann@58023
   316
eberlm@62422
   317
lemmas gcd_0 = gcd_0_right
eberlm@62422
   318
lemmas dvd_gcd_iff = gcd_greatest_iff
haftmann@58023
   319
haftmann@58023
   320
lemmas gcd_greatest_iff = dvd_gcd_iff
haftmann@58023
   321
haftmann@58023
   322
lemma gcdI:
haftmann@60634
   323
  assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
haftmann@60688
   324
    and "normalize c = c"
haftmann@60634
   325
  shows "c = gcd a b"
haftmann@60688
   326
  by (rule associated_eqI) (auto simp: assms intro: gcd_greatest)
haftmann@58023
   327
haftmann@58023
   328
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
haftmann@60688
   329
    normalize d = d \<and>
haftmann@58023
   330
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
haftmann@60688
   331
  by rule (auto intro: gcdI simp: gcd_greatest)
haftmann@58023
   332
haftmann@58023
   333
lemma gcd_dvd_prod: "gcd a b dvd k * b"
haftmann@58023
   334
  using mult_dvd_mono [of 1] by auto
haftmann@58023
   335
haftmann@58023
   336
lemma gcd_proj2_if_dvd: 
haftmann@60634
   337
  "b dvd a \<Longrightarrow> gcd a b = normalize b"
eberlm@62422
   338
  by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0)
haftmann@58023
   339
haftmann@58023
   340
lemma gcd_proj1_if_dvd: 
haftmann@60634
   341
  "a dvd b \<Longrightarrow> gcd a b = normalize a"
haftmann@58023
   342
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
haftmann@58023
   343
haftmann@60634
   344
lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"
haftmann@58023
   345
proof
haftmann@60634
   346
  assume A: "gcd m n = normalize m"
haftmann@58023
   347
  show "m dvd n"
haftmann@58023
   348
  proof (cases "m = 0")
haftmann@58023
   349
    assume [simp]: "m \<noteq> 0"
haftmann@60634
   350
    from A have B: "m = gcd m n * unit_factor m"
haftmann@58023
   351
      by (simp add: unit_eq_div2)
haftmann@58023
   352
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
haftmann@58023
   353
  qed (insert A, simp)
haftmann@58023
   354
next
haftmann@58023
   355
  assume "m dvd n"
haftmann@60634
   356
  then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd)
haftmann@58023
   357
qed
haftmann@58023
   358
  
haftmann@60634
   359
lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
haftmann@60634
   360
  using gcd_proj1_iff [of n m] by (simp add: ac_simps)
haftmann@58023
   361
haftmann@58023
   362
lemma gcd_mod1 [simp]:
haftmann@60430
   363
  "gcd (a mod b) b = gcd a b"
haftmann@58023
   364
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   365
haftmann@58023
   366
lemma gcd_mod2 [simp]:
haftmann@60430
   367
  "gcd a (b mod a) = gcd a b"
haftmann@58023
   368
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   369
         
haftmann@58023
   370
lemma gcd_mult_distrib': 
haftmann@60634
   371
  "normalize c * gcd a b = gcd (c * a) (c * b)"
haftmann@60569
   372
proof (cases "c = 0")
eberlm@62422
   373
  case True then show ?thesis by simp_all
haftmann@60569
   374
next
haftmann@60634
   375
  case False then have [simp]: "is_unit (unit_factor c)" by simp
haftmann@60569
   376
  show ?thesis
haftmann@60569
   377
  proof (induct a b rule: gcd_eucl_induct)
haftmann@60569
   378
    case (zero a) show ?case
haftmann@60569
   379
    proof (cases "a = 0")
eberlm@62422
   380
      case True then show ?thesis by simp
haftmann@60569
   381
    next
haftmann@60634
   382
      case False
eberlm@62422
   383
      then show ?thesis by (simp add: normalize_mult)
haftmann@60569
   384
    qed
haftmann@60569
   385
    case (mod a b)
haftmann@60569
   386
    then show ?case by (simp add: mult_mod_right gcd.commute)
haftmann@58023
   387
  qed
haftmann@58023
   388
qed
haftmann@58023
   389
haftmann@58023
   390
lemma gcd_mult_distrib:
haftmann@60634
   391
  "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"
haftmann@58023
   392
proof-
haftmann@60634
   393
  have "normalize k * gcd a b = gcd (k * a) (k * b)"
haftmann@60634
   394
    by (simp add: gcd_mult_distrib')
haftmann@60634
   395
  then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"
haftmann@60634
   396
    by simp
haftmann@60634
   397
  then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"
haftmann@60634
   398
    by (simp only: ac_simps)
haftmann@60634
   399
  then show ?thesis
haftmann@59009
   400
    by simp
haftmann@58023
   401
qed
haftmann@58023
   402
haftmann@58023
   403
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   404
  assumes "a \<noteq> 0"
haftmann@58023
   405
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   406
proof -
haftmann@58023
   407
   have "gcd a b dvd a" by (rule gcd_dvd1)
haftmann@58023
   408
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
wenzelm@60526
   409
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
haftmann@58023
   410
qed
haftmann@58023
   411
haftmann@58023
   412
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   413
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   414
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   415
haftmann@58023
   416
lemma euclidean_size_gcd_less1:
haftmann@58023
   417
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   418
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   419
proof (rule ccontr)
haftmann@58023
   420
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
eberlm@62422
   421
  with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   422
    by (intro le_antisym, simp_all)
eberlm@62422
   423
  have "a dvd gcd a b"
eberlm@62422
   424
    by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
eberlm@62422
   425
  hence "a dvd b" using dvd_gcdD2 by blast
wenzelm@60526
   426
  with \<open>\<not>a dvd b\<close> show False by contradiction
haftmann@58023
   427
qed
haftmann@58023
   428
haftmann@58023
   429
lemma euclidean_size_gcd_less2:
haftmann@58023
   430
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   431
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   432
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   433
haftmann@60430
   434
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
haftmann@58023
   435
  apply (rule gcdI)
haftmann@60688
   436
  apply simp_all
haftmann@58023
   437
  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
haftmann@58023
   438
  done
haftmann@58023
   439
haftmann@60430
   440
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
haftmann@58023
   441
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
haftmann@58023
   442
haftmann@60430
   443
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
haftmann@60433
   444
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
haftmann@58023
   445
haftmann@60430
   446
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
haftmann@60433
   447
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
haftmann@58023
   448
haftmann@60634
   449
lemma normalize_gcd_left [simp]:
haftmann@60634
   450
  "gcd (normalize a) b = gcd a b"
haftmann@60634
   451
proof (cases "a = 0")
haftmann@60634
   452
  case True then show ?thesis
haftmann@60634
   453
    by simp
haftmann@60634
   454
next
haftmann@60634
   455
  case False then have "is_unit (unit_factor a)"
haftmann@60634
   456
    by simp
haftmann@60634
   457
  moreover have "normalize a = a div unit_factor a"
haftmann@60634
   458
    by simp
haftmann@60634
   459
  ultimately show ?thesis
haftmann@60634
   460
    by (simp only: gcd_div_unit1)
haftmann@60634
   461
qed
haftmann@60634
   462
haftmann@60634
   463
lemma normalize_gcd_right [simp]:
haftmann@60634
   464
  "gcd a (normalize b) = gcd a b"
haftmann@60634
   465
  using normalize_gcd_left [of b a] by (simp add: ac_simps)
haftmann@60634
   466
haftmann@60634
   467
lemma gcd_idem: "gcd a a = normalize a"
eberlm@62422
   468
  by (cases "a = 0") (simp, rule sym, rule gcdI, simp_all)
haftmann@58023
   469
haftmann@60430
   470
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
haftmann@58023
   471
  apply (rule gcdI)
haftmann@58023
   472
  apply (simp add: ac_simps)
haftmann@58023
   473
  apply (rule gcd_dvd2)
haftmann@58023
   474
  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
haftmann@59009
   475
  apply simp
haftmann@58023
   476
  done
haftmann@58023
   477
haftmann@60430
   478
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
haftmann@58023
   479
  apply (rule gcdI)
haftmann@58023
   480
  apply simp
haftmann@58023
   481
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
haftmann@58023
   482
  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
haftmann@59009
   483
  apply simp
haftmann@58023
   484
  done
haftmann@58023
   485
haftmann@58023
   486
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
haftmann@58023
   487
proof
haftmann@58023
   488
  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
haftmann@58023
   489
    by (simp add: fun_eq_iff ac_simps)
haftmann@58023
   490
next
haftmann@58023
   491
  fix a show "gcd a \<circ> gcd a = gcd a"
haftmann@58023
   492
    by (simp add: fun_eq_iff gcd_left_idem)
haftmann@58023
   493
qed
haftmann@58023
   494
haftmann@58023
   495
lemma gcd_dvd_antisym:
haftmann@58023
   496
  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
haftmann@58023
   497
proof (rule gcdI)
haftmann@58023
   498
  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
haftmann@58023
   499
  have "gcd c d dvd c" by simp
haftmann@58023
   500
  with A show "gcd a b dvd c" by (rule dvd_trans)
haftmann@58023
   501
  have "gcd c d dvd d" by simp
haftmann@58023
   502
  with A show "gcd a b dvd d" by (rule dvd_trans)
haftmann@60688
   503
  show "normalize (gcd a b) = gcd a b"
haftmann@59009
   504
    by simp
haftmann@58023
   505
  fix l assume "l dvd c" and "l dvd d"
haftmann@58023
   506
  hence "l dvd gcd c d" by (rule gcd_greatest)
haftmann@58023
   507
  from this and B show "l dvd gcd a b" by (rule dvd_trans)
haftmann@58023
   508
qed
haftmann@58023
   509
haftmann@58023
   510
lemma coprime_crossproduct:
haftmann@58023
   511
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
haftmann@60688
   512
  shows "normalize (a * c) = normalize (b * d) \<longleftrightarrow> normalize a  = normalize b \<and> normalize c = normalize d"
haftmann@60688
   513
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@58023
   514
proof
haftmann@60688
   515
  assume ?rhs
haftmann@60688
   516
  then have "a dvd b" "b dvd a" "c dvd d" "d dvd c" by (simp_all add: associated_iff_dvd)
haftmann@60688
   517
  then have "a * c dvd b * d" "b * d dvd a * c" by (fast intro: mult_dvd_mono)+
haftmann@60688
   518
  then show ?lhs by (simp add: associated_iff_dvd)
haftmann@58023
   519
next
haftmann@58023
   520
  assume ?lhs
haftmann@60688
   521
  then have dvd: "a * c dvd b * d" "b * d dvd a * c" by (simp_all add: associated_iff_dvd)
haftmann@60688
   522
  then have "a dvd b * d" by (metis dvd_mult_left) 
haftmann@58023
   523
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
haftmann@60688
   524
  moreover from dvd have "b dvd a * c" by (metis dvd_mult_left) 
haftmann@58023
   525
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
haftmann@60688
   526
  moreover from dvd have "c dvd d * b" 
haftmann@60688
   527
    by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   528
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
haftmann@60688
   529
  moreover from dvd have "d dvd c * a"
haftmann@60688
   530
    by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   531
  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
haftmann@60688
   532
  ultimately show ?rhs by (simp add: associated_iff_dvd)
haftmann@58023
   533
qed
haftmann@58023
   534
haftmann@58023
   535
lemma gcd_add1 [simp]:
haftmann@58023
   536
  "gcd (m + n) n = gcd m n"
haftmann@58023
   537
  by (cases "n = 0", simp_all add: gcd_non_0)
haftmann@58023
   538
haftmann@58023
   539
lemma gcd_add2 [simp]:
haftmann@58023
   540
  "gcd m (m + n) = gcd m n"
haftmann@58023
   541
  using gcd_add1 [of n m] by (simp add: ac_simps)
haftmann@58023
   542
haftmann@60572
   543
lemma gcd_add_mult:
haftmann@60572
   544
  "gcd m (k * m + n) = gcd m n"
haftmann@60572
   545
proof -
haftmann@60572
   546
  have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
haftmann@60572
   547
    by (fact gcd_mod2)
haftmann@60572
   548
  then show ?thesis by simp 
haftmann@60572
   549
qed
haftmann@58023
   550
haftmann@60430
   551
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
haftmann@58023
   552
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   553
haftmann@58023
   554
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
eberlm@62422
   555
  by (auto intro: coprimeI gcd_greatest dvd_gcdD1 dvd_gcdD2)
haftmann@58023
   556
haftmann@58023
   557
lemma div_gcd_coprime:
haftmann@58023
   558
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@58023
   559
  defines [simp]: "d \<equiv> gcd a b"
haftmann@58023
   560
  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
haftmann@58023
   561
  shows "gcd a' b' = 1"
haftmann@58023
   562
proof (rule coprimeI)
haftmann@58023
   563
  fix l assume "l dvd a'" "l dvd b'"
haftmann@58023
   564
  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
haftmann@59009
   565
  moreover have "a = a' * d" "b = b' * d" by simp_all
haftmann@58023
   566
  ultimately have "a = (l * d) * s" "b = (l * d) * t"
haftmann@59009
   567
    by (simp_all only: ac_simps)
haftmann@58023
   568
  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
haftmann@58023
   569
  hence "l*d dvd d" by (simp add: gcd_greatest)
haftmann@59009
   570
  then obtain u where "d = l * d * u" ..
haftmann@59009
   571
  then have "d * (l * u) = d" by (simp add: ac_simps)
haftmann@59009
   572
  moreover from nz have "d \<noteq> 0" by simp
haftmann@59009
   573
  with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
haftmann@59009
   574
  ultimately have "1 = l * u"
wenzelm@60526
   575
    using \<open>d \<noteq> 0\<close> by simp
haftmann@59009
   576
  then show "l dvd 1" ..
haftmann@58023
   577
qed
haftmann@58023
   578
haftmann@58023
   579
lemma coprime_lmult:
haftmann@58023
   580
  assumes dab: "gcd d (a * b) = 1" 
haftmann@58023
   581
  shows "gcd d a = 1"
haftmann@58023
   582
proof (rule coprimeI)
haftmann@58023
   583
  fix l assume "l dvd d" and "l dvd a"
haftmann@58023
   584
  hence "l dvd a * b" by simp
wenzelm@60526
   585
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   586
qed
haftmann@58023
   587
haftmann@58023
   588
lemma coprime_rmult:
haftmann@58023
   589
  assumes dab: "gcd d (a * b) = 1"
haftmann@58023
   590
  shows "gcd d b = 1"
haftmann@58023
   591
proof (rule coprimeI)
haftmann@58023
   592
  fix l assume "l dvd d" and "l dvd b"
haftmann@58023
   593
  hence "l dvd a * b" by simp
wenzelm@60526
   594
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   595
qed
haftmann@58023
   596
haftmann@58023
   597
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
haftmann@58023
   598
  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
haftmann@58023
   599
haftmann@58023
   600
lemma gcd_coprime:
haftmann@60430
   601
  assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
haftmann@58023
   602
  shows "gcd a' b' = 1"
haftmann@58023
   603
proof -
haftmann@60430
   604
  from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
haftmann@58023
   605
  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
haftmann@58023
   606
  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
haftmann@58023
   607
  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
haftmann@58023
   608
  finally show ?thesis .
haftmann@58023
   609
qed
haftmann@58023
   610
haftmann@58023
   611
lemma coprime_power:
haftmann@58023
   612
  assumes "0 < n"
haftmann@58023
   613
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
haftmann@58023
   614
using assms proof (induct n)
haftmann@58023
   615
  case (Suc n) then show ?case
haftmann@58023
   616
    by (cases n) (simp_all add: coprime_mul_eq)
haftmann@58023
   617
qed simp
haftmann@58023
   618
haftmann@58023
   619
lemma gcd_coprime_exists:
haftmann@58023
   620
  assumes nz: "gcd a b \<noteq> 0"
haftmann@58023
   621
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
haftmann@58023
   622
  apply (rule_tac x = "a div gcd a b" in exI)
haftmann@58023
   623
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@59009
   624
  apply (insert nz, auto intro: div_gcd_coprime)
haftmann@58023
   625
  done
haftmann@58023
   626
haftmann@58023
   627
lemma coprime_exp:
haftmann@58023
   628
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
haftmann@58023
   629
  by (induct n, simp_all add: coprime_mult)
haftmann@58023
   630
haftmann@58023
   631
lemma gcd_exp:
haftmann@60688
   632
  "gcd (a ^ n) (b ^ n) = gcd a b ^ n"
haftmann@58023
   633
proof (cases "a = 0 \<and> b = 0")
haftmann@60688
   634
  case True
haftmann@60688
   635
  then show ?thesis by (cases n) simp_all
haftmann@58023
   636
next
haftmann@60688
   637
  case False
haftmann@60688
   638
  then have "1 = gcd ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
eberlm@62422
   639
    using coprime_exp2[OF div_gcd_coprime[of a b], of n n, symmetric] by simp
haftmann@60688
   640
  then have "gcd a b ^ n = gcd a b ^ n * ..." by simp
haftmann@58023
   641
  also note gcd_mult_distrib
haftmann@60688
   642
  also have "unit_factor (gcd a b ^ n) = 1"
haftmann@60688
   643
    using False by (auto simp add: unit_factor_power unit_factor_gcd)
haftmann@58023
   644
  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
haftmann@58023
   645
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   646
  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
haftmann@58023
   647
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   648
  finally show ?thesis by simp
haftmann@58023
   649
qed
haftmann@58023
   650
haftmann@58023
   651
lemma coprime_common_divisor: 
haftmann@60430
   652
  "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
haftmann@60430
   653
  apply (subgoal_tac "a dvd gcd a b")
haftmann@59061
   654
  apply simp
haftmann@58023
   655
  apply (erule (1) gcd_greatest)
haftmann@58023
   656
  done
haftmann@58023
   657
haftmann@58023
   658
lemma division_decomp: 
haftmann@58023
   659
  assumes dc: "a dvd b * c"
haftmann@58023
   660
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
haftmann@58023
   661
proof (cases "gcd a b = 0")
haftmann@58023
   662
  assume "gcd a b = 0"
haftmann@59009
   663
  hence "a = 0 \<and> b = 0" by simp
haftmann@58023
   664
  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
haftmann@58023
   665
  then show ?thesis by blast
haftmann@58023
   666
next
haftmann@58023
   667
  let ?d = "gcd a b"
haftmann@58023
   668
  assume "?d \<noteq> 0"
haftmann@58023
   669
  from gcd_coprime_exists[OF this]
haftmann@58023
   670
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   671
    by blast
haftmann@58023
   672
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
haftmann@58023
   673
  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
haftmann@58023
   674
  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
haftmann@58023
   675
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
wenzelm@60526
   676
  with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
haftmann@58023
   677
  with coprime_dvd_mult[OF ab'(3)] 
haftmann@58023
   678
    have "a' dvd c" by (subst (asm) ac_simps, blast)
haftmann@58023
   679
  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
haftmann@58023
   680
  then show ?thesis by blast
haftmann@58023
   681
qed
haftmann@58023
   682
haftmann@60433
   683
lemma pow_divs_pow:
haftmann@58023
   684
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
haftmann@58023
   685
  shows "a dvd b"
haftmann@58023
   686
proof (cases "gcd a b = 0")
haftmann@58023
   687
  assume "gcd a b = 0"
haftmann@59009
   688
  then show ?thesis by simp
haftmann@58023
   689
next
haftmann@58023
   690
  let ?d = "gcd a b"
haftmann@58023
   691
  assume "?d \<noteq> 0"
haftmann@58023
   692
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
wenzelm@60526
   693
  from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
wenzelm@60526
   694
  from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
haftmann@58023
   695
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   696
    by blast
haftmann@58023
   697
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
haftmann@58023
   698
    by (simp add: ab'(1,2)[symmetric])
haftmann@58023
   699
  hence "?d^n * a'^n dvd ?d^n * b'^n"
haftmann@58023
   700
    by (simp only: power_mult_distrib ac_simps)
haftmann@59009
   701
  with zn have "a'^n dvd b'^n" by simp
haftmann@58023
   702
  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
haftmann@58023
   703
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
haftmann@58023
   704
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
haftmann@58023
   705
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
haftmann@58023
   706
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
haftmann@58023
   707
  with ab'(1,2) show ?thesis by simp
haftmann@58023
   708
qed
haftmann@58023
   709
haftmann@60433
   710
lemma pow_divs_eq [simp]:
haftmann@58023
   711
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
haftmann@60433
   712
  by (auto intro: pow_divs_pow dvd_power_same)
haftmann@58023
   713
eberlm@62422
   714
lemmas divs_mult = divides_mult
haftmann@58023
   715
haftmann@58023
   716
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
haftmann@58023
   717
  by (subst add_commute, simp)
haftmann@58023
   718
haftmann@58023
   719
lemma setprod_coprime [rule_format]:
haftmann@60430
   720
  "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
haftmann@58023
   721
  apply (cases "finite A")
haftmann@58023
   722
  apply (induct set: finite)
haftmann@58023
   723
  apply (auto simp add: gcd_mult_cancel)
haftmann@58023
   724
  done
eberlm@62422
   725
  
eberlm@62422
   726
lemma listprod_coprime:
eberlm@62422
   727
  "(\<And>x. x \<in> set xs \<Longrightarrow> coprime x y) \<Longrightarrow> coprime (listprod xs) y" 
eberlm@62422
   728
  by (induction xs) (simp_all add: gcd_mult_cancel)
haftmann@58023
   729
haftmann@58023
   730
lemma coprime_divisors: 
haftmann@58023
   731
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
haftmann@58023
   732
  shows "gcd d e = 1" 
haftmann@58023
   733
proof -
haftmann@58023
   734
  from assms obtain k l where "a = d * k" "b = e * l"
haftmann@58023
   735
    unfolding dvd_def by blast
haftmann@58023
   736
  with assms have "gcd (d * k) (e * l) = 1" by simp
haftmann@58023
   737
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
haftmann@58023
   738
  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
haftmann@58023
   739
  finally have "gcd e d = 1" by (rule coprime_lmult)
haftmann@58023
   740
  then show ?thesis by (simp add: ac_simps)
haftmann@58023
   741
qed
haftmann@58023
   742
haftmann@58023
   743
lemma invertible_coprime:
haftmann@60430
   744
  assumes "a * b mod m = 1"
haftmann@60430
   745
  shows "coprime a m"
haftmann@59009
   746
proof -
haftmann@60430
   747
  from assms have "coprime m (a * b mod m)"
haftmann@59009
   748
    by simp
haftmann@60430
   749
  then have "coprime m (a * b)"
haftmann@59009
   750
    by simp
haftmann@60430
   751
  then have "coprime m a"
haftmann@59009
   752
    by (rule coprime_lmult)
haftmann@59009
   753
  then show ?thesis
haftmann@59009
   754
    by (simp add: ac_simps)
haftmann@59009
   755
qed
haftmann@58023
   756
haftmann@58023
   757
lemma lcm_gcd_prod:
haftmann@60634
   758
  "lcm a b * gcd a b = normalize (a * b)"
haftmann@60634
   759
  by (simp add: lcm_gcd)
haftmann@58023
   760
haftmann@58023
   761
lemma lcm_zero:
haftmann@58023
   762
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
haftmann@60687
   763
  by (fact lcm_eq_0_iff)
haftmann@58023
   764
haftmann@58023
   765
lemmas lcm_0_iff = lcm_zero
haftmann@58023
   766
haftmann@58023
   767
lemma gcd_lcm: 
haftmann@58023
   768
  assumes "lcm a b \<noteq> 0"
haftmann@60634
   769
  shows "gcd a b = normalize (a * b) div lcm a b"
haftmann@60634
   770
proof -
haftmann@60634
   771
  have "lcm a b * gcd a b = normalize (a * b)"
haftmann@60634
   772
    by (fact lcm_gcd_prod)
haftmann@60634
   773
  with assms show ?thesis
haftmann@60634
   774
    by (metis nonzero_mult_divide_cancel_left)
haftmann@58023
   775
qed
haftmann@58023
   776
haftmann@60687
   777
declare unit_factor_lcm [simp]
haftmann@58023
   778
haftmann@58023
   779
lemma lcmI:
haftmann@60634
   780
  assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
haftmann@60688
   781
    and "normalize c = c"
haftmann@60634
   782
  shows "c = lcm a b"
haftmann@60688
   783
  by (rule associated_eqI) (auto simp: assms intro: lcm_least)
haftmann@58023
   784
haftmann@58023
   785
lemma gcd_dvd_lcm [simp]:
haftmann@58023
   786
  "gcd a b dvd lcm a b"
haftmann@60690
   787
  using gcd_dvd2 by (rule dvd_lcmI2)
haftmann@58023
   788
eberlm@62422
   789
lemmas lcm_0 = lcm_0_right
haftmann@58023
   790
haftmann@58023
   791
lemma lcm_unique:
haftmann@58023
   792
  "a dvd d \<and> b dvd d \<and> 
haftmann@60688
   793
  normalize d = d \<and>
haftmann@58023
   794
  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@60688
   795
  by rule (auto intro: lcmI simp: lcm_least lcm_zero)
haftmann@58023
   796
haftmann@58023
   797
lemma lcm_coprime:
haftmann@60634
   798
  "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
haftmann@58023
   799
  by (subst lcm_gcd) simp
haftmann@58023
   800
haftmann@58023
   801
lemma lcm_proj1_if_dvd: 
haftmann@60634
   802
  "b dvd a \<Longrightarrow> lcm a b = normalize a"
haftmann@60430
   803
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
haftmann@58023
   804
haftmann@58023
   805
lemma lcm_proj2_if_dvd: 
haftmann@60634
   806
  "a dvd b \<Longrightarrow> lcm a b = normalize b"
haftmann@60430
   807
  using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
haftmann@58023
   808
haftmann@58023
   809
lemma lcm_proj1_iff:
haftmann@60634
   810
  "lcm m n = normalize m \<longleftrightarrow> n dvd m"
haftmann@58023
   811
proof
haftmann@60634
   812
  assume A: "lcm m n = normalize m"
haftmann@58023
   813
  show "n dvd m"
haftmann@58023
   814
  proof (cases "m = 0")
haftmann@58023
   815
    assume [simp]: "m \<noteq> 0"
haftmann@60634
   816
    from A have B: "m = lcm m n * unit_factor m"
haftmann@58023
   817
      by (simp add: unit_eq_div2)
haftmann@58023
   818
    show ?thesis by (subst B, simp)
haftmann@58023
   819
  qed simp
haftmann@58023
   820
next
haftmann@58023
   821
  assume "n dvd m"
haftmann@60634
   822
  then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd)
haftmann@58023
   823
qed
haftmann@58023
   824
haftmann@58023
   825
lemma lcm_proj2_iff:
haftmann@60634
   826
  "lcm m n = normalize n \<longleftrightarrow> m dvd n"
haftmann@58023
   827
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
haftmann@58023
   828
haftmann@58023
   829
lemma euclidean_size_lcm_le1: 
haftmann@58023
   830
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
   831
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
   832
proof -
haftmann@60690
   833
  have "a dvd lcm a b" by (rule dvd_lcm1)
haftmann@60690
   834
  then obtain c where A: "lcm a b = a * c" ..
wenzelm@60526
   835
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
haftmann@58023
   836
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
   837
qed
haftmann@58023
   838
haftmann@58023
   839
lemma euclidean_size_lcm_le2:
haftmann@58023
   840
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
   841
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
   842
haftmann@58023
   843
lemma euclidean_size_lcm_less1:
haftmann@58023
   844
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   845
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
   846
proof (rule ccontr)
haftmann@58023
   847
  from assms have "a \<noteq> 0" by auto
haftmann@58023
   848
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
   849
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
   850
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
   851
  with assms have "lcm a b dvd a" 
haftmann@58023
   852
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
eberlm@62422
   853
  hence "b dvd a" by (rule lcm_dvdD2)
wenzelm@60526
   854
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
   855
qed
haftmann@58023
   856
haftmann@58023
   857
lemma euclidean_size_lcm_less2:
haftmann@58023
   858
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   859
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
   860
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
   861
haftmann@58023
   862
lemma lcm_mult_unit1:
haftmann@60430
   863
  "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
haftmann@60690
   864
  by (rule associated_eqI) (simp_all add: mult_unit_dvd_iff dvd_lcmI1)
haftmann@58023
   865
haftmann@58023
   866
lemma lcm_mult_unit2:
haftmann@60430
   867
  "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
haftmann@60430
   868
  using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
haftmann@58023
   869
haftmann@58023
   870
lemma lcm_div_unit1:
haftmann@60430
   871
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
haftmann@60433
   872
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
haftmann@58023
   873
haftmann@58023
   874
lemma lcm_div_unit2:
haftmann@60430
   875
  "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
haftmann@60433
   876
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
haftmann@58023
   877
haftmann@60634
   878
lemma normalize_lcm_left [simp]:
haftmann@60634
   879
  "lcm (normalize a) b = lcm a b"
haftmann@60634
   880
proof (cases "a = 0")
haftmann@60634
   881
  case True then show ?thesis
haftmann@60634
   882
    by simp
haftmann@60634
   883
next
haftmann@60634
   884
  case False then have "is_unit (unit_factor a)"
haftmann@60634
   885
    by simp
haftmann@60634
   886
  moreover have "normalize a = a div unit_factor a"
haftmann@60634
   887
    by simp
haftmann@60634
   888
  ultimately show ?thesis
haftmann@60634
   889
    by (simp only: lcm_div_unit1)
haftmann@60634
   890
qed
haftmann@60634
   891
haftmann@60634
   892
lemma normalize_lcm_right [simp]:
haftmann@60634
   893
  "lcm a (normalize b) = lcm a b"
haftmann@60634
   894
  using normalize_lcm_left [of b a] by (simp add: ac_simps)
haftmann@60634
   895
haftmann@58023
   896
lemma LcmI:
haftmann@60634
   897
  assumes "\<And>a. a \<in> A \<Longrightarrow> a dvd b" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> b dvd c"
haftmann@60688
   898
    and "normalize b = b" shows "b = Lcm A"
eberlm@62422
   899
  by (rule associated_eqI) (auto simp: assms dvd_Lcm intro: Lcm_least)
haftmann@58023
   900
haftmann@58023
   901
lemma Lcm_subset:
haftmann@58023
   902
  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
haftmann@60634
   903
  by (blast intro: Lcm_least dvd_Lcm)
haftmann@58023
   904
haftmann@58023
   905
lemma Lcm_Un:
haftmann@58023
   906
  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
haftmann@58023
   907
  apply (rule lcmI)
haftmann@58023
   908
  apply (blast intro: Lcm_subset)
haftmann@58023
   909
  apply (blast intro: Lcm_subset)
haftmann@60634
   910
  apply (intro Lcm_least ballI, elim UnE)
haftmann@58023
   911
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
   912
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
   913
  apply simp
haftmann@58023
   914
  done
haftmann@58023
   915
haftmann@58023
   916
lemma Lcm_no_units:
haftmann@60430
   917
  "Lcm A = Lcm (A - {a. is_unit a})"
haftmann@58023
   918
proof -
haftmann@60430
   919
  have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
haftmann@60430
   920
  hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
haftmann@60634
   921
    by (simp add: Lcm_Un [symmetric])
haftmann@60430
   922
  also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
haftmann@58023
   923
  finally show ?thesis by simp
haftmann@58023
   924
qed
haftmann@58023
   925
haftmann@62353
   926
lemma Lcm_0_iff':
haftmann@60430
   927
  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
   928
proof
haftmann@58023
   929
  assume "Lcm A = 0"
haftmann@60430
   930
  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
   931
  proof
haftmann@60430
   932
    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
haftmann@60430
   933
    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
   934
    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
   935
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
   936
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@58023
   937
      apply (subst n_def)
haftmann@58023
   938
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
haftmann@58023
   939
      apply (rule exI[of _ l\<^sub>0])
haftmann@58023
   940
      apply (simp add: l\<^sub>0_props)
haftmann@58023
   941
      done
haftmann@58023
   942
    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
haftmann@60634
   943
    hence "normalize l \<noteq> 0" by simp
haftmann@60634
   944
    also from ex have "normalize l = Lcm A"
haftmann@58023
   945
       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
wenzelm@60526
   946
    finally show False using \<open>Lcm A = 0\<close> by contradiction
haftmann@58023
   947
  qed
haftmann@58023
   948
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
   949
haftmann@58023
   950
lemma Lcm_no_multiple:
haftmann@60430
   951
  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
haftmann@58023
   952
proof -
haftmann@60430
   953
  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
haftmann@60430
   954
  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
haftmann@58023
   955
  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
   956
qed
haftmann@58023
   957
haftmann@58023
   958
lemma Lcm_finite:
haftmann@58023
   959
  assumes "finite A"
haftmann@58023
   960
  shows "Lcm A = Finite_Set.fold lcm 1 A"
wenzelm@60526
   961
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
   962
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
haftmann@58023
   963
eberlm@62428
   964
lemma Lcm_set:
eberlm@62428
   965
  "Lcm (set xs) = foldl lcm 1 xs"
eberlm@62428
   966
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite
eberlm@62428
   967
  by (simp add: foldl_conv_fold lcm.commute)
eberlm@62428
   968
eberlm@62428
   969
lemma Lcm_eucl_set [code]:
eberlm@62428
   970
  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
eberlm@62428
   971
  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
haftmann@58023
   972
haftmann@58023
   973
lemma Lcm_singleton [simp]:
haftmann@60634
   974
  "Lcm {a} = normalize a"
haftmann@58023
   975
  by simp
haftmann@58023
   976
haftmann@58023
   977
lemma Lcm_2 [simp]:
haftmann@58023
   978
  "Lcm {a,b} = lcm a b"
haftmann@60634
   979
  by simp
haftmann@58023
   980
haftmann@58023
   981
lemma Lcm_coprime:
haftmann@58023
   982
  assumes "finite A" and "A \<noteq> {}" 
haftmann@58023
   983
  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
haftmann@60634
   984
  shows "Lcm A = normalize (\<Prod>A)"
haftmann@58023
   985
using assms proof (induct rule: finite_ne_induct)
haftmann@58023
   986
  case (insert a A)
haftmann@58023
   987
  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
haftmann@60634
   988
  also from insert have "Lcm A = normalize (\<Prod>A)" by blast
haftmann@58023
   989
  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
haftmann@58023
   990
  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
haftmann@60634
   991
  with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"
haftmann@58023
   992
    by (simp add: lcm_coprime)
haftmann@58023
   993
  finally show ?case .
haftmann@58023
   994
qed simp
haftmann@58023
   995
      
haftmann@58023
   996
lemma Lcm_coprime':
haftmann@58023
   997
  "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
haftmann@60634
   998
    \<Longrightarrow> Lcm A = normalize (\<Prod>A)"
haftmann@58023
   999
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
haftmann@58023
  1000
eberlm@62422
  1001
lemma unit_factor_Gcd [simp]: "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
haftmann@58023
  1002
proof -
haftmann@60634
  1003
  show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
eberlm@62422
  1004
    by (simp add: Gcd_Lcm unit_factor_Lcm)
haftmann@58023
  1005
qed
haftmann@58023
  1006
haftmann@58023
  1007
lemma GcdI:
haftmann@60634
  1008
  assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
haftmann@60688
  1009
    and "normalize b = b"
haftmann@60634
  1010
  shows "b = Gcd A"
eberlm@62422
  1011
  by (rule associated_eqI) (auto simp: assms Gcd_dvd intro: Gcd_greatest)
haftmann@58023
  1012
haftmann@58023
  1013
lemma Gcd_1:
haftmann@58023
  1014
  "1 \<in> A \<Longrightarrow> Gcd A = 1"
haftmann@60687
  1015
  by (auto intro!: Gcd_eq_1_I)
haftmann@58023
  1016
haftmann@58023
  1017
lemma Gcd_finite:
haftmann@58023
  1018
  assumes "finite A"
haftmann@58023
  1019
  shows "Gcd A = Finite_Set.fold gcd 0 A"
wenzelm@60526
  1020
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
  1021
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
haftmann@58023
  1022
eberlm@62428
  1023
lemma Gcd_set:
eberlm@62428
  1024
  "Gcd (set xs) = foldl gcd 0 xs"
eberlm@62428
  1025
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite
eberlm@62428
  1026
  by (simp add: foldl_conv_fold gcd.commute)
eberlm@62428
  1027
eberlm@62428
  1028
lemma Gcd_eucl_set [code]:
eberlm@62428
  1029
  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
eberlm@62428
  1030
  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
haftmann@58023
  1031
haftmann@60634
  1032
lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
haftmann@60687
  1033
  by simp
haftmann@58023
  1034
haftmann@58023
  1035
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
haftmann@60687
  1036
  by simp
haftmann@60686
  1037
eberlm@62422
  1038
eberlm@62422
  1039
definition pairwise_coprime where
eberlm@62422
  1040
  "pairwise_coprime A = (\<forall>x y. x \<in> A \<and> y \<in> A \<and> x \<noteq> y \<longrightarrow> coprime x y)"
eberlm@62422
  1041
eberlm@62422
  1042
lemma pairwise_coprimeI [intro?]:
eberlm@62422
  1043
  "(\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> coprime x y) \<Longrightarrow> pairwise_coprime A"
eberlm@62422
  1044
  by (simp add: pairwise_coprime_def)
eberlm@62422
  1045
eberlm@62422
  1046
lemma pairwise_coprimeD:
eberlm@62422
  1047
  "pairwise_coprime A \<Longrightarrow> x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<noteq> y \<Longrightarrow> coprime x y"
eberlm@62422
  1048
  by (simp add: pairwise_coprime_def)
eberlm@62422
  1049
eberlm@62422
  1050
lemma pairwise_coprime_subset: "pairwise_coprime A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> pairwise_coprime B"
eberlm@62422
  1051
  by (force simp: pairwise_coprime_def)
eberlm@62422
  1052
haftmann@58023
  1053
end
haftmann@58023
  1054
wenzelm@60526
  1055
text \<open>
haftmann@58023
  1056
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
haftmann@58023
  1057
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
wenzelm@60526
  1058
\<close>
haftmann@58023
  1059
haftmann@58023
  1060
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
  1061
begin
haftmann@58023
  1062
haftmann@58023
  1063
subclass euclidean_ring ..
haftmann@60439
  1064
subclass ring_gcd ..
haftmann@60439
  1065
haftmann@60572
  1066
lemma euclid_ext_gcd [simp]:
haftmann@60572
  1067
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
haftmann@60572
  1068
  by (induct a b rule: gcd_eucl_induct)
haftmann@60686
  1069
    (simp_all add: euclid_ext_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
haftmann@60572
  1070
haftmann@60572
  1071
lemma euclid_ext_gcd' [simp]:
haftmann@60572
  1072
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
haftmann@60572
  1073
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
haftmann@60572
  1074
  
haftmann@60572
  1075
lemma euclid_ext'_correct:
haftmann@60572
  1076
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
haftmann@60572
  1077
proof-
haftmann@60572
  1078
  obtain s t c where "euclid_ext a b = (s,t,c)"
haftmann@60572
  1079
    by (cases "euclid_ext a b", blast)
haftmann@60572
  1080
  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
haftmann@60572
  1081
    show ?thesis unfolding euclid_ext'_def by simp
haftmann@60572
  1082
qed
haftmann@60572
  1083
haftmann@60572
  1084
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
haftmann@60572
  1085
  using euclid_ext'_correct by blast
haftmann@60572
  1086
haftmann@58023
  1087
lemma gcd_neg1 [simp]:
haftmann@60430
  1088
  "gcd (-a) b = gcd a b"
haftmann@59009
  1089
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1090
haftmann@58023
  1091
lemma gcd_neg2 [simp]:
haftmann@60430
  1092
  "gcd a (-b) = gcd a b"
haftmann@59009
  1093
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1094
haftmann@58023
  1095
lemma gcd_neg_numeral_1 [simp]:
haftmann@60430
  1096
  "gcd (- numeral n) a = gcd (numeral n) a"
haftmann@58023
  1097
  by (fact gcd_neg1)
haftmann@58023
  1098
haftmann@58023
  1099
lemma gcd_neg_numeral_2 [simp]:
haftmann@60430
  1100
  "gcd a (- numeral n) = gcd a (numeral n)"
haftmann@58023
  1101
  by (fact gcd_neg2)
haftmann@58023
  1102
haftmann@58023
  1103
lemma gcd_diff1: "gcd (m - n) n = gcd m n"
haftmann@58023
  1104
  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
haftmann@58023
  1105
haftmann@58023
  1106
lemma gcd_diff2: "gcd (n - m) n = gcd m n"
haftmann@58023
  1107
  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
haftmann@58023
  1108
haftmann@58023
  1109
lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
haftmann@58023
  1110
proof -
haftmann@58023
  1111
  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
haftmann@58023
  1112
  also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
haftmann@58023
  1113
  also have "\<dots> = 1" by (rule coprime_plus_one)
haftmann@58023
  1114
  finally show ?thesis .
haftmann@58023
  1115
qed
haftmann@58023
  1116
haftmann@60430
  1117
lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
haftmann@58023
  1118
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1119
haftmann@60430
  1120
lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
haftmann@58023
  1121
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1122
haftmann@60430
  1123
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
haftmann@58023
  1124
  by (fact lcm_neg1)
haftmann@58023
  1125
haftmann@60430
  1126
lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
haftmann@58023
  1127
  by (fact lcm_neg2)
haftmann@58023
  1128
haftmann@60572
  1129
end
haftmann@58023
  1130
haftmann@58023
  1131
haftmann@60572
  1132
subsection \<open>Typical instances\<close>
haftmann@58023
  1133
haftmann@58023
  1134
instantiation nat :: euclidean_semiring
haftmann@58023
  1135
begin
haftmann@58023
  1136
haftmann@58023
  1137
definition [simp]:
haftmann@58023
  1138
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
haftmann@58023
  1139
haftmann@58023
  1140
instance proof
haftmann@59061
  1141
qed simp_all
haftmann@58023
  1142
haftmann@58023
  1143
end
haftmann@58023
  1144
eberlm@62422
  1145
haftmann@58023
  1146
instantiation int :: euclidean_ring
haftmann@58023
  1147
begin
haftmann@58023
  1148
haftmann@58023
  1149
definition [simp]:
haftmann@58023
  1150
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
haftmann@58023
  1151
wenzelm@60580
  1152
instance
haftmann@60686
  1153
by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
haftmann@58023
  1154
haftmann@58023
  1155
end
haftmann@58023
  1156
eberlm@62422
  1157
haftmann@60572
  1158
instantiation poly :: (field) euclidean_ring
haftmann@60571
  1159
begin
haftmann@60571
  1160
haftmann@60571
  1161
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
eberlm@62422
  1162
  where "euclidean_size p = (if p = 0 then 0 else 2 ^ degree p)"
haftmann@60571
  1163
haftmann@60600
  1164
lemma euclidean_size_poly_0 [simp]:
haftmann@60600
  1165
  "euclidean_size (0::'a poly) = 0"
haftmann@60600
  1166
  by (simp add: euclidean_size_poly_def)
haftmann@60600
  1167
haftmann@60600
  1168
lemma euclidean_size_poly_not_0 [simp]:
eberlm@62422
  1169
  "p \<noteq> 0 \<Longrightarrow> euclidean_size p = 2 ^ degree p"
haftmann@60600
  1170
  by (simp add: euclidean_size_poly_def)
haftmann@60600
  1171
haftmann@60571
  1172
instance
haftmann@60600
  1173
proof
haftmann@60571
  1174
  fix p q :: "'a poly"
haftmann@60600
  1175
  assume "q \<noteq> 0"
haftmann@60600
  1176
  then have "p mod q = 0 \<or> degree (p mod q) < degree q"
haftmann@60600
  1177
    by (rule degree_mod_less [of q p])  
haftmann@60600
  1178
  with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
haftmann@60600
  1179
    by (cases "p mod q = 0") simp_all
haftmann@60571
  1180
next
haftmann@60571
  1181
  fix p q :: "'a poly"
haftmann@60571
  1182
  assume "q \<noteq> 0"
haftmann@60600
  1183
  from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
haftmann@60571
  1184
    by (rule degree_mult_right_le)
haftmann@60600
  1185
  with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
haftmann@60600
  1186
    by (cases "p = 0") simp_all
eberlm@62422
  1187
qed simp
haftmann@60571
  1188
haftmann@58023
  1189
end
haftmann@60571
  1190
eberlm@62422
  1191
eberlm@62422
  1192
instance nat :: euclidean_semiring_gcd
eberlm@62422
  1193
proof
eberlm@62422
  1194
  show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
  1195
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
  1196
  show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
  1197
    by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
eberlm@62422
  1198
qed
eberlm@62422
  1199
eberlm@62422
  1200
instance int :: euclidean_ring_gcd
eberlm@62422
  1201
proof
eberlm@62422
  1202
  show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
eberlm@62422
  1203
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
  1204
  show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
eberlm@62422
  1205
    by (intro ext, simp add: lcm_eucl_def lcm_altdef_int 
eberlm@62422
  1206
          semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
eberlm@62422
  1207
qed
eberlm@62422
  1208
eberlm@62422
  1209
eberlm@62422
  1210
instantiation poly :: (field) euclidean_ring_gcd
eberlm@62422
  1211
begin
eberlm@62422
  1212
eberlm@62422
  1213
definition gcd_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@62422
  1214
  "gcd_poly = gcd_eucl"
eberlm@62422
  1215
  
eberlm@62422
  1216
definition lcm_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@62422
  1217
  "lcm_poly = lcm_eucl"
eberlm@62422
  1218
  
eberlm@62422
  1219
definition Gcd_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@62422
  1220
  "Gcd_poly = Gcd_eucl"
eberlm@62422
  1221
  
eberlm@62422
  1222
definition Lcm_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@62422
  1223
  "Lcm_poly = Lcm_eucl"
eberlm@62422
  1224
eberlm@62422
  1225
instance by standard (simp_all only: gcd_poly_def lcm_poly_def Gcd_poly_def Lcm_poly_def)
eberlm@62422
  1226
end
haftmann@60687
  1227
eberlm@62425
  1228
lemma poly_gcd_monic:
eberlm@62425
  1229
  "lead_coeff (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)"
eberlm@62425
  1230
  using unit_factor_gcd[of x y]
eberlm@62425
  1231
  by (simp add: unit_factor_poly_def monom_0 one_poly_def lead_coeff_def split: if_split_asm)
eberlm@62425
  1232
eberlm@62425
  1233
lemma poly_dvd_antisym:
eberlm@62425
  1234
  fixes p q :: "'a::idom poly"
eberlm@62425
  1235
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
eberlm@62425
  1236
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
eberlm@62425
  1237
proof (cases "p = 0")
eberlm@62425
  1238
  case True with coeff show "p = q" by simp
eberlm@62425
  1239
next
eberlm@62425
  1240
  case False with coeff have "q \<noteq> 0" by auto
eberlm@62425
  1241
  have degree: "degree p = degree q"
eberlm@62425
  1242
    using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
eberlm@62425
  1243
    by (intro order_antisym dvd_imp_degree_le)
eberlm@62425
  1244
eberlm@62425
  1245
  from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
eberlm@62425
  1246
  with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
eberlm@62425
  1247
  with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
eberlm@62425
  1248
    by (simp add: degree_mult_eq)
eberlm@62425
  1249
  with coeff a show "p = q"
eberlm@62425
  1250
    by (cases a, auto split: if_splits)
eberlm@62425
  1251
qed
eberlm@62425
  1252
eberlm@62425
  1253
lemma poly_gcd_unique:
eberlm@62425
  1254
  fixes d x y :: "_ poly"
eberlm@62425
  1255
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
eberlm@62425
  1256
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
eberlm@62425
  1257
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
eberlm@62425
  1258
  shows "d = gcd x y"
eberlm@62425
  1259
  using assms by (intro gcdI) (auto simp: normalize_poly_def split: if_split_asm)
eberlm@62425
  1260
eberlm@62425
  1261
lemma poly_gcd_code [code]:
eberlm@62425
  1262
  "gcd x y = (if y = 0 then normalize x else gcd y (x mod (y :: _ poly)))"
eberlm@62425
  1263
  by (simp add: gcd_0 gcd_non_0)
eberlm@62425
  1264
haftmann@60571
  1265
end