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