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