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