src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Sun Oct 16 09:31:05 2016 +0200 (2016-10-16)
changeset 64242 93c6f0da5c70
parent 64240 eabf80376aab
child 64243 aee949f6642d
permissions -rw-r--r--
more standardized theorem names for facts involving the div and mod identity
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
eberlm@63498
     6
imports "~~/src/HOL/GCD" Factorial_Ring
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@64164
    20
class euclidean_semiring = semiring_modulo + 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@64240
    29
lemma mod_0 [simp]: "0 mod a = 0"
haftmann@64242
    30
  using div_mult_mod_eq [of 0 a] by simp
haftmann@64164
    31
haftmann@64164
    32
lemma dvd_mod_iff: 
haftmann@64164
    33
  assumes "k dvd n"
haftmann@64164
    34
  shows   "(k dvd m mod n) = (k dvd m)"
haftmann@64164
    35
proof -
haftmann@64164
    36
  from assms have "(k dvd m mod n) \<longleftrightarrow> (k dvd ((m div n) * n + m mod n))" 
haftmann@64164
    37
    by (simp add: dvd_add_right_iff)
haftmann@64164
    38
  also have "(m div n) * n + m mod n = m"
haftmann@64242
    39
    using div_mult_mod_eq [of m n] by simp
haftmann@64164
    40
  finally show ?thesis .
haftmann@64164
    41
qed
haftmann@64164
    42
haftmann@64164
    43
lemma mod_0_imp_dvd: 
haftmann@64164
    44
  assumes "a mod b = 0"
haftmann@64164
    45
  shows   "b dvd a"
haftmann@64164
    46
proof -
haftmann@64164
    47
  have "b dvd ((a div b) * b)" by simp
haftmann@64164
    48
  also have "(a div b) * b = a"
haftmann@64242
    49
    using div_mult_mod_eq [of a b] by (simp add: assms)
haftmann@64164
    50
  finally show ?thesis .
haftmann@64164
    51
qed
haftmann@64164
    52
haftmann@63947
    53
lemma euclidean_size_normalize [simp]:
haftmann@63947
    54
  "euclidean_size (normalize a) = euclidean_size a"
haftmann@63947
    55
proof (cases "a = 0")
haftmann@63947
    56
  case True
haftmann@63947
    57
  then show ?thesis
haftmann@63947
    58
    by simp
haftmann@63947
    59
next
haftmann@63947
    60
  case [simp]: False
haftmann@63947
    61
  have "euclidean_size (normalize a) \<le> euclidean_size (normalize a * unit_factor a)"
haftmann@63947
    62
    by (rule size_mult_mono) simp
haftmann@63947
    63
  moreover have "euclidean_size a \<le> euclidean_size (a * (1 div unit_factor a))"
haftmann@63947
    64
    by (rule size_mult_mono) simp
haftmann@63947
    65
  ultimately show ?thesis
haftmann@63947
    66
    by simp
haftmann@63947
    67
qed
haftmann@63947
    68
haftmann@58023
    69
lemma euclidean_division:
haftmann@58023
    70
  fixes a :: 'a and b :: 'a
haftmann@60600
    71
  assumes "b \<noteq> 0"
haftmann@58023
    72
  obtains s and t where "a = s * b + t" 
haftmann@58023
    73
    and "euclidean_size t < euclidean_size b"
haftmann@58023
    74
proof -
haftmann@64242
    75
  from div_mult_mod_eq [of a b] 
haftmann@58023
    76
     have "a = a div b * b + a mod b" by simp
haftmann@60569
    77
  with that and assms show ?thesis by (auto simp add: mod_size_less)
haftmann@58023
    78
qed
haftmann@58023
    79
haftmann@58023
    80
lemma dvd_euclidean_size_eq_imp_dvd:
haftmann@58023
    81
  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
haftmann@58023
    82
  shows "a dvd b"
haftmann@60569
    83
proof (rule ccontr)
haftmann@60569
    84
  assume "\<not> a dvd b"
haftmann@64163
    85
  hence "b mod a \<noteq> 0" using mod_0_imp_dvd[of b a] by blast
haftmann@60569
    86
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
haftmann@64164
    87
  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
haftmann@58023
    88
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
wenzelm@60526
    89
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
wenzelm@60526
    90
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
haftmann@58023
    91
      using size_mult_mono by force
haftmann@60569
    92
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
haftmann@60569
    93
  have "euclidean_size (b mod a) < euclidean_size a"
haftmann@58023
    94
      using mod_size_less by blast
haftmann@58023
    95
  ultimately show False using size_eq by simp
haftmann@58023
    96
qed
haftmann@58023
    97
eberlm@63498
    98
lemma size_mult_mono': "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (b * a)"
eberlm@63498
    99
  by (subst mult.commute) (rule size_mult_mono)
eberlm@63498
   100
eberlm@63498
   101
lemma euclidean_size_times_unit:
eberlm@63498
   102
  assumes "is_unit a"
eberlm@63498
   103
  shows   "euclidean_size (a * b) = euclidean_size b"
eberlm@63498
   104
proof (rule antisym)
eberlm@63498
   105
  from assms have [simp]: "a \<noteq> 0" by auto
eberlm@63498
   106
  thus "euclidean_size (a * b) \<ge> euclidean_size b" by (rule size_mult_mono')
eberlm@63498
   107
  from assms have "is_unit (1 div a)" by simp
eberlm@63498
   108
  hence "1 div a \<noteq> 0" by (intro notI) simp_all
eberlm@63498
   109
  hence "euclidean_size (a * b) \<le> euclidean_size ((1 div a) * (a * b))"
eberlm@63498
   110
    by (rule size_mult_mono')
eberlm@63498
   111
  also from assms have "(1 div a) * (a * b) = b"
eberlm@63498
   112
    by (simp add: algebra_simps unit_div_mult_swap)
eberlm@63498
   113
  finally show "euclidean_size (a * b) \<le> euclidean_size b" .
eberlm@63498
   114
qed
eberlm@63498
   115
haftmann@64177
   116
lemma euclidean_size_unit: "is_unit a \<Longrightarrow> euclidean_size a = euclidean_size 1"
haftmann@64177
   117
  using euclidean_size_times_unit[of a 1] by simp
eberlm@63498
   118
eberlm@63498
   119
lemma unit_iff_euclidean_size: 
haftmann@64177
   120
  "is_unit a \<longleftrightarrow> euclidean_size a = euclidean_size 1 \<and> a \<noteq> 0"
eberlm@63498
   121
proof safe
haftmann@64177
   122
  assume A: "a \<noteq> 0" and B: "euclidean_size a = euclidean_size 1"
haftmann@64177
   123
  show "is_unit a" by (rule dvd_euclidean_size_eq_imp_dvd[OF A _ B]) simp_all
eberlm@63498
   124
qed (auto intro: euclidean_size_unit)
eberlm@63498
   125
eberlm@63498
   126
lemma euclidean_size_times_nonunit:
eberlm@63498
   127
  assumes "a \<noteq> 0" "b \<noteq> 0" "\<not>is_unit a"
eberlm@63498
   128
  shows   "euclidean_size b < euclidean_size (a * b)"
eberlm@63498
   129
proof (rule ccontr)
eberlm@63498
   130
  assume "\<not>euclidean_size b < euclidean_size (a * b)"
eberlm@63498
   131
  with size_mult_mono'[OF assms(1), of b] 
eberlm@63498
   132
    have eq: "euclidean_size (a * b) = euclidean_size b" by simp
eberlm@63498
   133
  have "a * b dvd b"
eberlm@63498
   134
    by (rule dvd_euclidean_size_eq_imp_dvd[OF _ _ eq]) (insert assms, simp_all)
eberlm@63498
   135
  hence "a * b dvd 1 * b" by simp
eberlm@63498
   136
  with \<open>b \<noteq> 0\<close> have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff)
eberlm@63498
   137
  with assms(3) show False by contradiction
eberlm@63498
   138
qed
eberlm@63498
   139
eberlm@63498
   140
lemma dvd_imp_size_le:
haftmann@64177
   141
  assumes "a dvd b" "b \<noteq> 0" 
haftmann@64177
   142
  shows   "euclidean_size a \<le> euclidean_size b"
eberlm@63498
   143
  using assms by (auto elim!: dvdE simp: size_mult_mono)
eberlm@63498
   144
eberlm@63498
   145
lemma dvd_proper_imp_size_less:
haftmann@64177
   146
  assumes "a dvd b" "\<not>b dvd a" "b \<noteq> 0" 
haftmann@64177
   147
  shows   "euclidean_size a < euclidean_size b"
eberlm@63498
   148
proof -
haftmann@64177
   149
  from assms(1) obtain c where "b = a * c" by (erule dvdE)
haftmann@64177
   150
  hence z: "b = c * a" by (simp add: mult.commute)
haftmann@64177
   151
  from z assms have "\<not>is_unit c" by (auto simp: mult.commute mult_unit_dvd_iff)
eberlm@63498
   152
  with z assms show ?thesis
eberlm@63498
   153
    by (auto intro!: euclidean_size_times_nonunit simp: )
eberlm@63498
   154
qed
eberlm@63498
   155
haftmann@58023
   156
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   157
where
haftmann@60634
   158
  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
haftmann@60572
   159
  by pat_completeness simp
haftmann@60569
   160
termination
haftmann@60569
   161
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
   162
haftmann@58023
   163
declare gcd_eucl.simps [simp del]
haftmann@58023
   164
haftmann@60569
   165
lemma gcd_eucl_induct [case_names zero mod]:
haftmann@60569
   166
  assumes H1: "\<And>b. P b 0"
haftmann@60569
   167
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
   168
  shows "P a b"
haftmann@58023
   169
proof (induct a b rule: gcd_eucl.induct)
haftmann@60569
   170
  case ("1" a b)
haftmann@60569
   171
  show ?case
haftmann@60569
   172
  proof (cases "b = 0")
haftmann@60569
   173
    case True then show "P a b" by simp (rule H1)
haftmann@60569
   174
  next
haftmann@60569
   175
    case False
haftmann@60600
   176
    then have "P b (a mod b)"
haftmann@60600
   177
      by (rule "1.hyps")
haftmann@60569
   178
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
   179
      by (blast intro: H2)
haftmann@60569
   180
  qed
haftmann@58023
   181
qed
haftmann@58023
   182
haftmann@58023
   183
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   184
where
haftmann@60634
   185
  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
haftmann@58023
   186
wenzelm@63167
   187
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" \<comment> \<open>
haftmann@60572
   188
  Somewhat complicated definition of Lcm that has the advantage of working
haftmann@60572
   189
  for infinite sets as well\<close>
haftmann@58023
   190
where
haftmann@60430
   191
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
   192
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
   193
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60634
   194
       in normalize l 
haftmann@58023
   195
      else 0)"
haftmann@58023
   196
haftmann@58023
   197
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
haftmann@58023
   198
where
haftmann@58023
   199
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
   200
eberlm@62428
   201
declare Lcm_eucl_def Gcd_eucl_def [code del]
eberlm@62428
   202
haftmann@60572
   203
lemma gcd_eucl_0:
haftmann@60634
   204
  "gcd_eucl a 0 = normalize a"
haftmann@60572
   205
  by (simp add: gcd_eucl.simps [of a 0])
haftmann@60572
   206
haftmann@60572
   207
lemma gcd_eucl_0_left:
haftmann@60634
   208
  "gcd_eucl 0 a = normalize a"
haftmann@60600
   209
  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
haftmann@60572
   210
haftmann@60572
   211
lemma gcd_eucl_non_0:
haftmann@60572
   212
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
haftmann@60600
   213
  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
haftmann@60572
   214
eberlm@62422
   215
lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
eberlm@62422
   216
  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
eberlm@62422
   217
  by (induct a b rule: gcd_eucl_induct)
eberlm@62422
   218
     (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   219
eberlm@62422
   220
lemma normalize_gcd_eucl [simp]:
eberlm@62422
   221
  "normalize (gcd_eucl a b) = gcd_eucl a b"
eberlm@62422
   222
  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
eberlm@62422
   223
     
eberlm@62422
   224
lemma gcd_eucl_greatest:
eberlm@62422
   225
  fixes k a b :: 'a
eberlm@62422
   226
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
eberlm@62422
   227
proof (induct a b rule: gcd_eucl_induct)
eberlm@62422
   228
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
eberlm@62422
   229
next
eberlm@62422
   230
  case (mod a b)
eberlm@62422
   231
  then show ?case
eberlm@62422
   232
    by (simp add: gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   233
qed
eberlm@62422
   234
eberlm@63498
   235
lemma gcd_euclI:
eberlm@63498
   236
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
eberlm@63498
   237
  assumes "d dvd a" "d dvd b" "normalize d = d"
eberlm@63498
   238
          "\<And>k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd d"
eberlm@63498
   239
  shows   "gcd_eucl a b = d"
eberlm@63498
   240
  by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
eberlm@63498
   241
eberlm@62422
   242
lemma eq_gcd_euclI:
eberlm@62422
   243
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
eberlm@62422
   244
  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
   245
          "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
eberlm@62422
   246
  shows   "gcd = gcd_eucl"
eberlm@62422
   247
  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
eberlm@62422
   248
eberlm@62422
   249
lemma gcd_eucl_zero [simp]:
eberlm@62422
   250
  "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
eberlm@62422
   251
  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
eberlm@62422
   252
eberlm@62422
   253
  
eberlm@62422
   254
lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
eberlm@62422
   255
  and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
eberlm@62422
   256
  and unit_factor_Lcm_eucl [simp]: 
eberlm@62422
   257
          "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
eberlm@62422
   258
proof -
eberlm@62422
   259
  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
   260
    unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
eberlm@62422
   261
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
eberlm@62422
   262
    case False
eberlm@62422
   263
    hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
eberlm@62422
   264
    with False show ?thesis by auto
eberlm@62422
   265
  next
eberlm@62422
   266
    case True
eberlm@62422
   267
    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
wenzelm@63040
   268
    define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
wenzelm@63040
   269
    define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
eberlm@62422
   270
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   271
      apply (subst n_def)
eberlm@62422
   272
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
eberlm@62422
   273
      apply (rule exI[of _ l\<^sub>0])
eberlm@62422
   274
      apply (simp add: l\<^sub>0_props)
eberlm@62422
   275
      done
eberlm@62422
   276
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
eberlm@62422
   277
      unfolding l_def by simp_all
eberlm@62422
   278
    {
eberlm@62422
   279
      fix l' assume "\<forall>a\<in>A. a dvd l'"
eberlm@62422
   280
      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
   281
      moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
eberlm@62422
   282
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
eberlm@62422
   283
                          euclidean_size b = euclidean_size (gcd_eucl l l')"
eberlm@62422
   284
        by (intro exI[of _ "gcd_eucl l l'"], auto)
eberlm@62422
   285
      hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
eberlm@62422
   286
      moreover have "euclidean_size (gcd_eucl l l') \<le> n"
eberlm@62422
   287
      proof -
eberlm@62422
   288
        have "gcd_eucl l l' dvd l" by simp
eberlm@62422
   289
        then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
eberlm@62422
   290
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
eberlm@62422
   291
        hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
eberlm@62422
   292
          by (rule size_mult_mono)
eberlm@62422
   293
        also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
eberlm@62422
   294
        also note \<open>euclidean_size l = n\<close>
eberlm@62422
   295
        finally show "euclidean_size (gcd_eucl l l') \<le> n" .
eberlm@62422
   296
      qed
eberlm@62422
   297
      ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')" 
eberlm@62422
   298
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
eberlm@62422
   299
      from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
eberlm@62422
   300
        by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
eberlm@62422
   301
      hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
eberlm@62422
   302
    }
eberlm@62422
   303
eberlm@62422
   304
    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
   305
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
eberlm@62422
   306
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
eberlm@62422
   307
        unit_factor (normalize l) = 
eberlm@62422
   308
        (if normalize l = 0 then 0 else 1)"
eberlm@62422
   309
      by (auto simp: unit_simps)
eberlm@62422
   310
    also from True have "normalize l = Lcm_eucl A"
eberlm@62422
   311
      by (simp add: Lcm_eucl_def Let_def n_def l_def)
eberlm@62422
   312
    finally show ?thesis .
eberlm@62422
   313
  qed
eberlm@62422
   314
  note A = this
eberlm@62422
   315
eberlm@62422
   316
  {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
eberlm@62422
   317
  {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
   318
  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
eberlm@62422
   319
qed
eberlm@63498
   320
eberlm@62422
   321
lemma normalize_Lcm_eucl [simp]:
eberlm@62422
   322
  "normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   323
proof (cases "Lcm_eucl A = 0")
eberlm@62422
   324
  case True then show ?thesis by simp
eberlm@62422
   325
next
eberlm@62422
   326
  case False
eberlm@62422
   327
  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   328
    by (fact unit_factor_mult_normalize)
eberlm@62422
   329
  with False show ?thesis by simp
eberlm@62422
   330
qed
eberlm@62422
   331
eberlm@62422
   332
lemma eq_Lcm_euclI:
eberlm@62422
   333
  fixes lcm :: "'a set \<Rightarrow> 'a"
eberlm@62422
   334
  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
   335
          "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
eberlm@62422
   336
  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  
eberlm@62422
   337
haftmann@64177
   338
lemma Gcd_eucl_dvd: "a \<in> A \<Longrightarrow> Gcd_eucl A dvd a"
eberlm@63498
   339
  unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)
eberlm@63498
   340
eberlm@63498
   341
lemma Gcd_eucl_greatest: "(\<And>x. x \<in> A \<Longrightarrow> d dvd x) \<Longrightarrow> d dvd Gcd_eucl A"
eberlm@63498
   342
  unfolding Gcd_eucl_def by auto
eberlm@63498
   343
eberlm@63498
   344
lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"
eberlm@63498
   345
  by (simp add: Gcd_eucl_def)
eberlm@63498
   346
eberlm@63498
   347
lemma Lcm_euclI:
eberlm@63498
   348
  assumes "\<And>x. x \<in> A \<Longrightarrow> x dvd d" "\<And>d'. (\<And>x. x \<in> A \<Longrightarrow> x dvd d') \<Longrightarrow> d dvd d'" "normalize d = d"
eberlm@63498
   349
  shows   "Lcm_eucl A = d"
eberlm@63498
   350
proof -
eberlm@63498
   351
  have "normalize (Lcm_eucl A) = normalize d"
eberlm@63498
   352
    by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)
eberlm@63498
   353
  thus ?thesis by (simp add: assms)
eberlm@63498
   354
qed
eberlm@63498
   355
eberlm@63498
   356
lemma Gcd_euclI:
eberlm@63498
   357
  assumes "\<And>x. x \<in> A \<Longrightarrow> d dvd x" "\<And>d'. (\<And>x. x \<in> A \<Longrightarrow> d' dvd x) \<Longrightarrow> d' dvd d" "normalize d = d"
eberlm@63498
   358
  shows   "Gcd_eucl A = d"
eberlm@63498
   359
proof -
eberlm@63498
   360
  have "normalize (Gcd_eucl A) = normalize d"
eberlm@63498
   361
    by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)
eberlm@63498
   362
  thus ?thesis by (simp add: assms)
eberlm@63498
   363
qed
eberlm@63498
   364
  
eberlm@63498
   365
lemmas lcm_gcd_eucl_facts = 
eberlm@63498
   366
  gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def
eberlm@63498
   367
  Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl
eberlm@63498
   368
  dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl
eberlm@63498
   369
eberlm@63498
   370
lemma normalized_factors_product:
eberlm@63498
   371
  "{p. p dvd a * b \<and> normalize p = p} = 
eberlm@63498
   372
     (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
eberlm@63498
   373
proof safe
eberlm@63498
   374
  fix p assume p: "p dvd a * b" "normalize p = p"
eberlm@63498
   375
  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
eberlm@63498
   376
    by standard (rule lcm_gcd_eucl_facts; assumption)+
eberlm@63498
   377
  from dvd_productE[OF p(1)] guess x y . note xy = this
eberlm@63498
   378
  define x' y' where "x' = normalize x" and "y' = normalize y"
eberlm@63498
   379
  have "p = x' * y'"
eberlm@63498
   380
    by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
eberlm@63498
   381
  moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b" 
eberlm@63498
   382
    by (simp_all add: x'_def y'_def)
eberlm@63498
   383
  ultimately show "p \<in> (\<lambda>(x, y). x * y) ` 
eberlm@63498
   384
                     ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
eberlm@63498
   385
    by blast
eberlm@63498
   386
qed (auto simp: normalize_mult mult_dvd_mono)
eberlm@63498
   387
eberlm@63498
   388
eberlm@63498
   389
subclass factorial_semiring
eberlm@63498
   390
proof (standard, rule factorial_semiring_altI_aux)
eberlm@63498
   391
  fix x assume "x \<noteq> 0"
eberlm@63498
   392
  thus "finite {p. p dvd x \<and> normalize p = p}"
eberlm@63498
   393
  proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
eberlm@63498
   394
    case (less x)
eberlm@63498
   395
    show ?case
eberlm@63498
   396
    proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
eberlm@63498
   397
      case False
eberlm@63498
   398
      have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
eberlm@63498
   399
      proof
eberlm@63498
   400
        fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
eberlm@63498
   401
        with False have "is_unit p \<or> x dvd p" by blast
eberlm@63498
   402
        thus "p \<in> {1, normalize x}"
eberlm@63498
   403
        proof (elim disjE)
eberlm@63498
   404
          assume "is_unit p"
eberlm@63498
   405
          hence "normalize p = 1" by (simp add: is_unit_normalize)
eberlm@63498
   406
          with p show ?thesis by simp
eberlm@63498
   407
        next
eberlm@63498
   408
          assume "x dvd p"
eberlm@63498
   409
          with p have "normalize p = normalize x" by (intro associatedI) simp_all
eberlm@63498
   410
          with p show ?thesis by simp
eberlm@63498
   411
        qed
eberlm@63498
   412
      qed
eberlm@63498
   413
      moreover have "finite \<dots>" by simp
eberlm@63498
   414
      ultimately show ?thesis by (rule finite_subset)
eberlm@63498
   415
      
eberlm@63498
   416
    next
eberlm@63498
   417
      case True
eberlm@63498
   418
      then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
eberlm@63498
   419
      define z where "z = x div y"
eberlm@63498
   420
      let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
eberlm@63498
   421
      from y have x: "x = y * z" by (simp add: z_def)
eberlm@63498
   422
      with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
eberlm@63498
   423
      from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
eberlm@63498
   424
      have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
eberlm@63498
   425
        by (subst x) (rule normalized_factors_product)
eberlm@63498
   426
      also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
eberlm@63498
   427
        by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
eberlm@63498
   428
      hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
eberlm@63498
   429
        by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
eberlm@63498
   430
           (auto simp: x)
eberlm@63498
   431
      finally show ?thesis .
eberlm@63498
   432
    qed
eberlm@63498
   433
  qed
eberlm@63498
   434
next
eberlm@63498
   435
  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
eberlm@63498
   436
    by standard (rule lcm_gcd_eucl_facts; assumption)+
eberlm@63498
   437
  fix p assume p: "irreducible p"
eberlm@63633
   438
  thus "prime_elem p" by (rule irreducible_imp_prime_elem_gcd)
eberlm@63498
   439
qed
eberlm@63498
   440
eberlm@63498
   441
lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"
eberlm@63498
   442
  by (intro ext gcd_euclI gcd_lcm_factorial)
eberlm@63498
   443
eberlm@63498
   444
lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"
eberlm@63498
   445
  by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)
eberlm@63498
   446
eberlm@63498
   447
lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"
eberlm@63498
   448
  by (intro ext Gcd_euclI gcd_lcm_factorial)
eberlm@63498
   449
eberlm@63498
   450
lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"
eberlm@63498
   451
  by (intro ext Lcm_euclI gcd_lcm_factorial)
eberlm@63498
   452
eberlm@63498
   453
lemmas eucl_eq_factorial = 
eberlm@63498
   454
  gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial 
eberlm@63498
   455
  Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial
eberlm@63498
   456
  
haftmann@58023
   457
end
haftmann@58023
   458
haftmann@60598
   459
class euclidean_ring = euclidean_semiring + idom
haftmann@60598
   460
begin
haftmann@60598
   461
eberlm@62442
   462
function euclid_ext_aux :: "'a \<Rightarrow> _" where
eberlm@62442
   463
  "euclid_ext_aux r' r s' s t' t = (
eberlm@62442
   464
     if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')
eberlm@62442
   465
     else let q = r' div r
eberlm@62442
   466
          in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"
eberlm@62442
   467
by auto
eberlm@62442
   468
termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)
eberlm@62442
   469
eberlm@62442
   470
declare euclid_ext_aux.simps [simp del]
haftmann@60598
   471
eberlm@62442
   472
lemma euclid_ext_aux_correct:
haftmann@64177
   473
  assumes "gcd_eucl r' r = gcd_eucl a b"
haftmann@64177
   474
  assumes "s' * a + t' * b = r'"
haftmann@64177
   475
  assumes "s * a + t * b = r"
haftmann@64177
   476
  shows   "case euclid_ext_aux r' r s' s t' t of (x,y,c) \<Rightarrow>
haftmann@64177
   477
             x * a + y * b = c \<and> c = gcd_eucl a b" (is "?P (euclid_ext_aux r' r s' s t' t)")
eberlm@62442
   478
using assms
eberlm@62442
   479
proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)
eberlm@62442
   480
  case (1 r' r s' s t' t)
eberlm@62442
   481
  show ?case
eberlm@62442
   482
  proof (cases "r = 0")
eberlm@62442
   483
    case True
eberlm@62442
   484
    hence "euclid_ext_aux r' r s' s t' t = 
eberlm@62442
   485
             (s' div unit_factor r', t' div unit_factor r', normalize r')"
eberlm@62442
   486
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
eberlm@62442
   487
    also have "?P \<dots>"
eberlm@62442
   488
    proof safe
haftmann@64177
   489
      have "s' div unit_factor r' * a + t' div unit_factor r' * b = 
haftmann@64177
   490
                (s' * a + t' * b) div unit_factor r'"
eberlm@62442
   491
        by (cases "r' = 0") (simp_all add: unit_div_commute)
haftmann@64177
   492
      also have "s' * a + t' * b = r'" by fact
eberlm@62442
   493
      also have "\<dots> div unit_factor r' = normalize r'" by simp
haftmann@64177
   494
      finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .
eberlm@62442
   495
    next
haftmann@64177
   496
      from "1.prems" True show "normalize r' = gcd_eucl a b" by (simp add: gcd_eucl_0)
eberlm@62442
   497
    qed
eberlm@62442
   498
    finally show ?thesis .
eberlm@62442
   499
  next
eberlm@62442
   500
    case False
eberlm@62442
   501
    hence "euclid_ext_aux r' r s' s t' t = 
eberlm@62442
   502
             euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"
eberlm@62442
   503
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
eberlm@62442
   504
    also from "1.prems" False have "?P \<dots>"
eberlm@62442
   505
    proof (intro "1.IH")
haftmann@64177
   506
      have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =
haftmann@64177
   507
              (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)
haftmann@64177
   508
      also have "s' * a + t' * b = r'" by fact
haftmann@64177
   509
      also have "s * a + t * b = r" by fact
haftmann@64242
   510
      also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]
eberlm@62442
   511
        by (simp add: algebra_simps)
haftmann@64177
   512
      finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .
eberlm@62442
   513
    qed (auto simp: gcd_eucl_non_0 algebra_simps div_mod_equality')
eberlm@62442
   514
    finally show ?thesis .
eberlm@62442
   515
  qed
eberlm@62442
   516
qed
eberlm@62442
   517
eberlm@62442
   518
definition euclid_ext where
eberlm@62442
   519
  "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"
haftmann@60598
   520
haftmann@60598
   521
lemma euclid_ext_0: 
haftmann@60634
   522
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
eberlm@62442
   523
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
haftmann@60598
   524
haftmann@60598
   525
lemma euclid_ext_left_0: 
haftmann@60634
   526
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
eberlm@62442
   527
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
haftmann@60598
   528
eberlm@62442
   529
lemma euclid_ext_correct':
haftmann@64177
   530
  "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd_eucl a b"
eberlm@62442
   531
  unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all
haftmann@60598
   532
eberlm@62457
   533
lemma euclid_ext_gcd_eucl:
haftmann@64177
   534
  "(case euclid_ext a b of (x,y,c) \<Rightarrow> c) = gcd_eucl a b"
haftmann@64177
   535
  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold)
eberlm@62457
   536
eberlm@62442
   537
definition euclid_ext' where
haftmann@64177
   538
  "euclid_ext' a b = (case euclid_ext a b of (x, y, _) \<Rightarrow> (x, y))"
haftmann@60598
   539
eberlm@62442
   540
lemma euclid_ext'_correct':
haftmann@64177
   541
  "case euclid_ext' a b of (x,y) \<Rightarrow> x * a + y * b = gcd_eucl a b"
haftmann@64177
   542
  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold euclid_ext'_def)
haftmann@60598
   543
haftmann@60634
   544
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
haftmann@60598
   545
  by (simp add: euclid_ext'_def euclid_ext_0)
haftmann@60598
   546
haftmann@60634
   547
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
haftmann@60598
   548
  by (simp add: euclid_ext'_def euclid_ext_left_0)
haftmann@60598
   549
haftmann@60598
   550
end
haftmann@60598
   551
haftmann@58023
   552
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@58023
   553
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
haftmann@58023
   554
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
haftmann@58023
   555
begin
haftmann@58023
   556
eberlm@62422
   557
subclass semiring_gcd
eberlm@62422
   558
  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
haftmann@58023
   559
eberlm@62422
   560
subclass semiring_Gcd
eberlm@62422
   561
  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
eberlm@63498
   562
eberlm@63498
   563
subclass factorial_semiring_gcd
eberlm@63498
   564
proof
eberlm@63498
   565
  fix a b
eberlm@63498
   566
  show "gcd a b = gcd_factorial a b"
eberlm@63498
   567
    by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+
eberlm@63498
   568
  thus "lcm a b = lcm_factorial a b"
eberlm@63498
   569
    by (simp add: lcm_factorial_gcd_factorial lcm_gcd)
eberlm@63498
   570
next
eberlm@63498
   571
  fix A 
eberlm@63498
   572
  show "Gcd A = Gcd_factorial A"
eberlm@63498
   573
    by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+
eberlm@63498
   574
  show "Lcm A = Lcm_factorial A"
eberlm@63498
   575
    by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+
eberlm@63498
   576
qed
eberlm@63498
   577
haftmann@58023
   578
lemma gcd_non_0:
haftmann@60430
   579
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
haftmann@60572
   580
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
haftmann@58023
   581
eberlm@62422
   582
lemmas gcd_0 = gcd_0_right
eberlm@62422
   583
lemmas dvd_gcd_iff = gcd_greatest_iff
haftmann@58023
   584
lemmas gcd_greatest_iff = dvd_gcd_iff
haftmann@58023
   585
haftmann@58023
   586
lemma gcd_mod1 [simp]:
haftmann@60430
   587
  "gcd (a mod b) b = gcd a b"
haftmann@58023
   588
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   589
haftmann@58023
   590
lemma gcd_mod2 [simp]:
haftmann@60430
   591
  "gcd a (b mod a) = gcd a b"
haftmann@58023
   592
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   593
         
haftmann@58023
   594
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   595
  assumes "a \<noteq> 0"
haftmann@58023
   596
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   597
proof -
haftmann@58023
   598
   have "gcd a b dvd a" by (rule gcd_dvd1)
haftmann@58023
   599
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
wenzelm@60526
   600
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
haftmann@58023
   601
qed
haftmann@58023
   602
haftmann@58023
   603
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   604
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   605
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   606
haftmann@58023
   607
lemma euclidean_size_gcd_less1:
haftmann@58023
   608
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   609
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   610
proof (rule ccontr)
haftmann@58023
   611
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
eberlm@62422
   612
  with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   613
    by (intro le_antisym, simp_all)
eberlm@62422
   614
  have "a dvd gcd a b"
eberlm@62422
   615
    by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
eberlm@62422
   616
  hence "a dvd b" using dvd_gcdD2 by blast
wenzelm@60526
   617
  with \<open>\<not>a dvd b\<close> show False by contradiction
haftmann@58023
   618
qed
haftmann@58023
   619
haftmann@58023
   620
lemma euclidean_size_gcd_less2:
haftmann@58023
   621
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   622
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   623
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   624
haftmann@58023
   625
lemma euclidean_size_lcm_le1: 
haftmann@58023
   626
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
   627
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
   628
proof -
haftmann@60690
   629
  have "a dvd lcm a b" by (rule dvd_lcm1)
haftmann@60690
   630
  then obtain c where A: "lcm a b = a * c" ..
eberlm@62429
   631
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_eq_0_iff)
haftmann@58023
   632
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
   633
qed
haftmann@58023
   634
haftmann@58023
   635
lemma euclidean_size_lcm_le2:
haftmann@58023
   636
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
   637
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
   638
haftmann@58023
   639
lemma euclidean_size_lcm_less1:
haftmann@58023
   640
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   641
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
   642
proof (rule ccontr)
haftmann@58023
   643
  from assms have "a \<noteq> 0" by auto
haftmann@58023
   644
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
   645
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
   646
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
   647
  with assms have "lcm a b dvd a" 
eberlm@62429
   648
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
eberlm@62422
   649
  hence "b dvd a" by (rule lcm_dvdD2)
wenzelm@60526
   650
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
   651
qed
haftmann@58023
   652
haftmann@58023
   653
lemma euclidean_size_lcm_less2:
haftmann@58023
   654
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   655
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
   656
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
   657
eberlm@62428
   658
lemma Lcm_eucl_set [code]:
eberlm@62428
   659
  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
eberlm@62428
   660
  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
haftmann@58023
   661
eberlm@62428
   662
lemma Gcd_eucl_set [code]:
eberlm@62428
   663
  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
eberlm@62428
   664
  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
haftmann@58023
   665
haftmann@58023
   666
end
haftmann@58023
   667
eberlm@63498
   668
wenzelm@60526
   669
text \<open>
haftmann@58023
   670
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
haftmann@58023
   671
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
wenzelm@60526
   672
\<close>
haftmann@58023
   673
haftmann@58023
   674
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
   675
begin
haftmann@58023
   676
haftmann@58023
   677
subclass euclidean_ring ..
haftmann@60439
   678
subclass ring_gcd ..
eberlm@63498
   679
subclass factorial_ring_gcd ..
haftmann@60439
   680
haftmann@60572
   681
lemma euclid_ext_gcd [simp]:
haftmann@60572
   682
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
eberlm@62442
   683
  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)
haftmann@60572
   684
haftmann@60572
   685
lemma euclid_ext_gcd' [simp]:
haftmann@60572
   686
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
haftmann@60572
   687
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
eberlm@62442
   688
eberlm@62442
   689
lemma euclid_ext_correct:
haftmann@64177
   690
  "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd a b"
haftmann@64177
   691
  using euclid_ext_correct'[of a b]
eberlm@62442
   692
  by (simp add: gcd_gcd_eucl case_prod_unfold)
haftmann@60572
   693
  
haftmann@60572
   694
lemma euclid_ext'_correct:
haftmann@60572
   695
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
eberlm@62442
   696
  using euclid_ext_correct'[of a b]
eberlm@62442
   697
  by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)
haftmann@60572
   698
haftmann@60572
   699
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
haftmann@60572
   700
  using euclid_ext'_correct by blast
haftmann@60572
   701
haftmann@60572
   702
end
haftmann@58023
   703
haftmann@58023
   704
haftmann@60572
   705
subsection \<open>Typical instances\<close>
haftmann@58023
   706
haftmann@58023
   707
instantiation nat :: euclidean_semiring
haftmann@58023
   708
begin
haftmann@58023
   709
haftmann@58023
   710
definition [simp]:
haftmann@58023
   711
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
haftmann@58023
   712
eberlm@63498
   713
instance by standard simp_all
haftmann@58023
   714
haftmann@58023
   715
end
haftmann@58023
   716
eberlm@62422
   717
haftmann@58023
   718
instantiation int :: euclidean_ring
haftmann@58023
   719
begin
haftmann@58023
   720
haftmann@58023
   721
definition [simp]:
haftmann@58023
   722
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
haftmann@58023
   723
eberlm@63498
   724
instance by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
haftmann@58023
   725
haftmann@58023
   726
end
haftmann@58023
   727
eberlm@62422
   728
instance nat :: euclidean_semiring_gcd
eberlm@62422
   729
proof
eberlm@62422
   730
  show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
   731
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
   732
  show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
   733
    by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
eberlm@62422
   734
qed
eberlm@62422
   735
eberlm@62422
   736
instance int :: euclidean_ring_gcd
eberlm@62422
   737
proof
eberlm@62422
   738
  show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
eberlm@62422
   739
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
   740
  show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
eberlm@62422
   741
    by (intro ext, simp add: lcm_eucl_def lcm_altdef_int 
eberlm@62422
   742
          semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
eberlm@62422
   743
qed
eberlm@62422
   744
haftmann@63924
   745
end