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