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