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