src/HOL/Number_Theory/Euclidean_Algorithm.thy
author Manuel Eberl <eberlm@in.tum.de>
Sun Feb 28 12:05:52 2016 +0100 (2016-02-28)
changeset 62442 26e4be6a680f
parent 62429 25271ff79171
child 62457 a3c7bd201da7
permissions -rw-r--r--
More efficient Extended Euclidean Algorithm
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@62429
     6
imports "~~/src/HOL/GCD" "~~/src/HOL/Library/Polynomial"
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
haftmann@58023
    57
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
    58
where
haftmann@60634
    59
  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
haftmann@60572
    60
  by pat_completeness simp
haftmann@60569
    61
termination
haftmann@60569
    62
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
    63
haftmann@58023
    64
declare gcd_eucl.simps [simp del]
haftmann@58023
    65
haftmann@60569
    66
lemma gcd_eucl_induct [case_names zero mod]:
haftmann@60569
    67
  assumes H1: "\<And>b. P b 0"
haftmann@60569
    68
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
    69
  shows "P a b"
haftmann@58023
    70
proof (induct a b rule: gcd_eucl.induct)
haftmann@60569
    71
  case ("1" a b)
haftmann@60569
    72
  show ?case
haftmann@60569
    73
  proof (cases "b = 0")
haftmann@60569
    74
    case True then show "P a b" by simp (rule H1)
haftmann@60569
    75
  next
haftmann@60569
    76
    case False
haftmann@60600
    77
    then have "P b (a mod b)"
haftmann@60600
    78
      by (rule "1.hyps")
haftmann@60569
    79
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
    80
      by (blast intro: H2)
haftmann@60569
    81
  qed
haftmann@58023
    82
qed
haftmann@58023
    83
haftmann@58023
    84
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
    85
where
haftmann@60634
    86
  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
haftmann@58023
    87
haftmann@60572
    88
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
haftmann@60572
    89
  Somewhat complicated definition of Lcm that has the advantage of working
haftmann@60572
    90
  for infinite sets as well\<close>
haftmann@58023
    91
where
haftmann@60430
    92
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
    93
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
    94
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60634
    95
       in normalize l 
haftmann@58023
    96
      else 0)"
haftmann@58023
    97
haftmann@58023
    98
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
haftmann@58023
    99
where
haftmann@58023
   100
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
   101
eberlm@62428
   102
declare Lcm_eucl_def Gcd_eucl_def [code del]
eberlm@62428
   103
haftmann@60572
   104
lemma gcd_eucl_0:
haftmann@60634
   105
  "gcd_eucl a 0 = normalize a"
haftmann@60572
   106
  by (simp add: gcd_eucl.simps [of a 0])
haftmann@60572
   107
haftmann@60572
   108
lemma gcd_eucl_0_left:
haftmann@60634
   109
  "gcd_eucl 0 a = normalize a"
haftmann@60600
   110
  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
haftmann@60572
   111
haftmann@60572
   112
lemma gcd_eucl_non_0:
haftmann@60572
   113
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
haftmann@60600
   114
  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
haftmann@60572
   115
eberlm@62422
   116
lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
eberlm@62422
   117
  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
eberlm@62422
   118
  by (induct a b rule: gcd_eucl_induct)
eberlm@62422
   119
     (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   120
eberlm@62422
   121
lemma normalize_gcd_eucl [simp]:
eberlm@62422
   122
  "normalize (gcd_eucl a b) = gcd_eucl a b"
eberlm@62422
   123
  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
eberlm@62422
   124
     
eberlm@62422
   125
lemma gcd_eucl_greatest:
eberlm@62422
   126
  fixes k a b :: 'a
eberlm@62422
   127
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
eberlm@62422
   128
proof (induct a b rule: gcd_eucl_induct)
eberlm@62422
   129
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
eberlm@62422
   130
next
eberlm@62422
   131
  case (mod a b)
eberlm@62422
   132
  then show ?case
eberlm@62422
   133
    by (simp add: gcd_eucl_non_0 dvd_mod_iff)
eberlm@62422
   134
qed
eberlm@62422
   135
eberlm@62422
   136
lemma eq_gcd_euclI:
eberlm@62422
   137
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
eberlm@62422
   138
  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
   139
          "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
eberlm@62422
   140
  shows   "gcd = gcd_eucl"
eberlm@62422
   141
  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
eberlm@62422
   142
eberlm@62422
   143
lemma gcd_eucl_zero [simp]:
eberlm@62422
   144
  "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
eberlm@62422
   145
  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
eberlm@62422
   146
eberlm@62422
   147
  
eberlm@62422
   148
lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
eberlm@62422
   149
  and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
eberlm@62422
   150
  and unit_factor_Lcm_eucl [simp]: 
eberlm@62422
   151
          "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
eberlm@62422
   152
proof -
eberlm@62422
   153
  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
   154
    unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
eberlm@62422
   155
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
eberlm@62422
   156
    case False
eberlm@62422
   157
    hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
eberlm@62422
   158
    with False show ?thesis by auto
eberlm@62422
   159
  next
eberlm@62422
   160
    case True
eberlm@62422
   161
    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
eberlm@62422
   162
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   163
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   164
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
eberlm@62422
   165
      apply (subst n_def)
eberlm@62422
   166
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
eberlm@62422
   167
      apply (rule exI[of _ l\<^sub>0])
eberlm@62422
   168
      apply (simp add: l\<^sub>0_props)
eberlm@62422
   169
      done
eberlm@62422
   170
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
eberlm@62422
   171
      unfolding l_def by simp_all
eberlm@62422
   172
    {
eberlm@62422
   173
      fix l' assume "\<forall>a\<in>A. a dvd l'"
eberlm@62422
   174
      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
   175
      moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
eberlm@62422
   176
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
eberlm@62422
   177
                          euclidean_size b = euclidean_size (gcd_eucl l l')"
eberlm@62422
   178
        by (intro exI[of _ "gcd_eucl l l'"], auto)
eberlm@62422
   179
      hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
eberlm@62422
   180
      moreover have "euclidean_size (gcd_eucl l l') \<le> n"
eberlm@62422
   181
      proof -
eberlm@62422
   182
        have "gcd_eucl l l' dvd l" by simp
eberlm@62422
   183
        then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
eberlm@62422
   184
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
eberlm@62422
   185
        hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
eberlm@62422
   186
          by (rule size_mult_mono)
eberlm@62422
   187
        also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
eberlm@62422
   188
        also note \<open>euclidean_size l = n\<close>
eberlm@62422
   189
        finally show "euclidean_size (gcd_eucl l l') \<le> n" .
eberlm@62422
   190
      qed
eberlm@62422
   191
      ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')" 
eberlm@62422
   192
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
eberlm@62422
   193
      from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
eberlm@62422
   194
        by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
eberlm@62422
   195
      hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
eberlm@62422
   196
    }
eberlm@62422
   197
eberlm@62422
   198
    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
   199
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
eberlm@62422
   200
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
eberlm@62422
   201
        unit_factor (normalize l) = 
eberlm@62422
   202
        (if normalize l = 0 then 0 else 1)"
eberlm@62422
   203
      by (auto simp: unit_simps)
eberlm@62422
   204
    also from True have "normalize l = Lcm_eucl A"
eberlm@62422
   205
      by (simp add: Lcm_eucl_def Let_def n_def l_def)
eberlm@62422
   206
    finally show ?thesis .
eberlm@62422
   207
  qed
eberlm@62422
   208
  note A = this
eberlm@62422
   209
eberlm@62422
   210
  {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
eberlm@62422
   211
  {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
   212
  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
eberlm@62422
   213
qed
eberlm@62422
   214
  
eberlm@62422
   215
lemma normalize_Lcm_eucl [simp]:
eberlm@62422
   216
  "normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   217
proof (cases "Lcm_eucl A = 0")
eberlm@62422
   218
  case True then show ?thesis by simp
eberlm@62422
   219
next
eberlm@62422
   220
  case False
eberlm@62422
   221
  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
eberlm@62422
   222
    by (fact unit_factor_mult_normalize)
eberlm@62422
   223
  with False show ?thesis by simp
eberlm@62422
   224
qed
eberlm@62422
   225
eberlm@62422
   226
lemma eq_Lcm_euclI:
eberlm@62422
   227
  fixes lcm :: "'a set \<Rightarrow> 'a"
eberlm@62422
   228
  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
   229
          "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
eberlm@62422
   230
  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  
eberlm@62422
   231
haftmann@58023
   232
end
haftmann@58023
   233
haftmann@60598
   234
class euclidean_ring = euclidean_semiring + idom
haftmann@60598
   235
begin
haftmann@60598
   236
eberlm@62442
   237
function euclid_ext_aux :: "'a \<Rightarrow> _" where
eberlm@62442
   238
  "euclid_ext_aux r' r s' s t' t = (
eberlm@62442
   239
     if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')
eberlm@62442
   240
     else let q = r' div r
eberlm@62442
   241
          in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"
eberlm@62442
   242
by auto
eberlm@62442
   243
termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)
eberlm@62442
   244
eberlm@62442
   245
declare euclid_ext_aux.simps [simp del]
haftmann@60598
   246
eberlm@62442
   247
lemma euclid_ext_aux_correct:
eberlm@62442
   248
  assumes "gcd_eucl r' r = gcd_eucl x y"
eberlm@62442
   249
  assumes "s' * x + t' * y = r'"
eberlm@62442
   250
  assumes "s * x + t * y = r"
eberlm@62442
   251
  shows   "case euclid_ext_aux r' r s' s t' t of (a,b,c) \<Rightarrow>
eberlm@62442
   252
             a * x + b * y = c \<and> c = gcd_eucl x y" (is "?P (euclid_ext_aux r' r s' s t' t)")
eberlm@62442
   253
using assms
eberlm@62442
   254
proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)
eberlm@62442
   255
  case (1 r' r s' s t' t)
eberlm@62442
   256
  show ?case
eberlm@62442
   257
  proof (cases "r = 0")
eberlm@62442
   258
    case True
eberlm@62442
   259
    hence "euclid_ext_aux r' r s' s t' t = 
eberlm@62442
   260
             (s' div unit_factor r', t' div unit_factor r', normalize r')"
eberlm@62442
   261
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
eberlm@62442
   262
    also have "?P \<dots>"
eberlm@62442
   263
    proof safe
eberlm@62442
   264
      have "s' div unit_factor r' * x + t' div unit_factor r' * y = 
eberlm@62442
   265
                (s' * x + t' * y) div unit_factor r'"
eberlm@62442
   266
        by (cases "r' = 0") (simp_all add: unit_div_commute)
eberlm@62442
   267
      also have "s' * x + t' * y = r'" by fact
eberlm@62442
   268
      also have "\<dots> div unit_factor r' = normalize r'" by simp
eberlm@62442
   269
      finally show "s' div unit_factor r' * x + t' div unit_factor r' * y = normalize r'" .
eberlm@62442
   270
    next
eberlm@62442
   271
      from "1.prems" True show "normalize r' = gcd_eucl x y" by (simp add: gcd_eucl_0)
eberlm@62442
   272
    qed
eberlm@62442
   273
    finally show ?thesis .
eberlm@62442
   274
  next
eberlm@62442
   275
    case False
eberlm@62442
   276
    hence "euclid_ext_aux r' r s' s t' t = 
eberlm@62442
   277
             euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"
eberlm@62442
   278
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
eberlm@62442
   279
    also from "1.prems" False have "?P \<dots>"
eberlm@62442
   280
    proof (intro "1.IH")
eberlm@62442
   281
      have "(s' - r' div r * s) * x + (t' - r' div r * t) * y =
eberlm@62442
   282
              (s' * x + t' * y) - r' div r * (s * x + t * y)" by (simp add: algebra_simps)
eberlm@62442
   283
      also have "s' * x + t' * y = r'" by fact
eberlm@62442
   284
      also have "s * x + t * y = r" by fact
eberlm@62442
   285
      also have "r' - r' div r * r = r' mod r" using mod_div_equality[of r' r]
eberlm@62442
   286
        by (simp add: algebra_simps)
eberlm@62442
   287
      finally show "(s' - r' div r * s) * x + (t' - r' div r * t) * y = r' mod r" .
eberlm@62442
   288
    qed (auto simp: gcd_eucl_non_0 algebra_simps div_mod_equality')
eberlm@62442
   289
    finally show ?thesis .
eberlm@62442
   290
  qed
eberlm@62442
   291
qed
eberlm@62442
   292
eberlm@62442
   293
definition euclid_ext where
eberlm@62442
   294
  "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"
haftmann@60598
   295
haftmann@60598
   296
lemma euclid_ext_0: 
haftmann@60634
   297
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
eberlm@62442
   298
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
haftmann@60598
   299
haftmann@60598
   300
lemma euclid_ext_left_0: 
haftmann@60634
   301
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
eberlm@62442
   302
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
haftmann@60598
   303
eberlm@62442
   304
lemma euclid_ext_correct':
eberlm@62442
   305
  "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd_eucl x y"
eberlm@62442
   306
  unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all
haftmann@60598
   307
eberlm@62442
   308
definition euclid_ext' where
eberlm@62442
   309
  "euclid_ext' x y = (case euclid_ext x y of (a, b, _) \<Rightarrow> (a, b))"
haftmann@60598
   310
eberlm@62442
   311
lemma euclid_ext'_correct':
eberlm@62442
   312
  "case euclid_ext' x y of (a,b) \<Rightarrow> a * x + b * y = gcd_eucl x y"
eberlm@62442
   313
  using euclid_ext_correct'[of x y] by (simp add: case_prod_unfold euclid_ext'_def)
haftmann@60598
   314
haftmann@60634
   315
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
haftmann@60598
   316
  by (simp add: euclid_ext'_def euclid_ext_0)
haftmann@60598
   317
haftmann@60634
   318
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
haftmann@60598
   319
  by (simp add: euclid_ext'_def euclid_ext_left_0)
haftmann@60598
   320
haftmann@60598
   321
end
haftmann@60598
   322
haftmann@58023
   323
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@58023
   324
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
haftmann@58023
   325
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
haftmann@58023
   326
begin
haftmann@58023
   327
eberlm@62422
   328
subclass semiring_gcd
eberlm@62422
   329
  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
haftmann@58023
   330
eberlm@62422
   331
subclass semiring_Gcd
eberlm@62422
   332
  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
eberlm@62422
   333
  
haftmann@58023
   334
lemma gcd_non_0:
haftmann@60430
   335
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
haftmann@60572
   336
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
haftmann@58023
   337
eberlm@62422
   338
lemmas gcd_0 = gcd_0_right
eberlm@62422
   339
lemmas dvd_gcd_iff = gcd_greatest_iff
haftmann@58023
   340
lemmas gcd_greatest_iff = dvd_gcd_iff
haftmann@58023
   341
haftmann@58023
   342
lemma gcd_mod1 [simp]:
haftmann@60430
   343
  "gcd (a mod b) b = gcd a b"
haftmann@58023
   344
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   345
haftmann@58023
   346
lemma gcd_mod2 [simp]:
haftmann@60430
   347
  "gcd a (b mod a) = gcd a b"
haftmann@58023
   348
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   349
         
haftmann@58023
   350
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   351
  assumes "a \<noteq> 0"
haftmann@58023
   352
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   353
proof -
haftmann@58023
   354
   have "gcd a b dvd a" by (rule gcd_dvd1)
haftmann@58023
   355
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
wenzelm@60526
   356
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
haftmann@58023
   357
qed
haftmann@58023
   358
haftmann@58023
   359
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   360
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   361
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   362
haftmann@58023
   363
lemma euclidean_size_gcd_less1:
haftmann@58023
   364
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   365
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   366
proof (rule ccontr)
haftmann@58023
   367
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
eberlm@62422
   368
  with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   369
    by (intro le_antisym, simp_all)
eberlm@62422
   370
  have "a dvd gcd a b"
eberlm@62422
   371
    by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
eberlm@62422
   372
  hence "a dvd b" using dvd_gcdD2 by blast
wenzelm@60526
   373
  with \<open>\<not>a dvd b\<close> show False by contradiction
haftmann@58023
   374
qed
haftmann@58023
   375
haftmann@58023
   376
lemma euclidean_size_gcd_less2:
haftmann@58023
   377
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   378
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   379
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   380
haftmann@58023
   381
lemma euclidean_size_lcm_le1: 
haftmann@58023
   382
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
   383
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
   384
proof -
haftmann@60690
   385
  have "a dvd lcm a b" by (rule dvd_lcm1)
haftmann@60690
   386
  then obtain c where A: "lcm a b = a * c" ..
eberlm@62429
   387
  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
   388
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
   389
qed
haftmann@58023
   390
haftmann@58023
   391
lemma euclidean_size_lcm_le2:
haftmann@58023
   392
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
   393
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
   394
haftmann@58023
   395
lemma euclidean_size_lcm_less1:
haftmann@58023
   396
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   397
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
   398
proof (rule ccontr)
haftmann@58023
   399
  from assms have "a \<noteq> 0" by auto
haftmann@58023
   400
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
   401
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
   402
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
   403
  with assms have "lcm a b dvd a" 
eberlm@62429
   404
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
eberlm@62422
   405
  hence "b dvd a" by (rule lcm_dvdD2)
wenzelm@60526
   406
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
   407
qed
haftmann@58023
   408
haftmann@58023
   409
lemma euclidean_size_lcm_less2:
haftmann@58023
   410
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   411
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
   412
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
   413
eberlm@62428
   414
lemma Lcm_eucl_set [code]:
eberlm@62428
   415
  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
eberlm@62428
   416
  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
haftmann@58023
   417
eberlm@62428
   418
lemma Gcd_eucl_set [code]:
eberlm@62428
   419
  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
eberlm@62428
   420
  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
haftmann@58023
   421
haftmann@58023
   422
end
haftmann@58023
   423
wenzelm@60526
   424
text \<open>
haftmann@58023
   425
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
haftmann@58023
   426
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
wenzelm@60526
   427
\<close>
haftmann@58023
   428
haftmann@58023
   429
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
   430
begin
haftmann@58023
   431
haftmann@58023
   432
subclass euclidean_ring ..
haftmann@60439
   433
subclass ring_gcd ..
haftmann@60439
   434
haftmann@60572
   435
lemma euclid_ext_gcd [simp]:
haftmann@60572
   436
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
eberlm@62442
   437
  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)
haftmann@60572
   438
haftmann@60572
   439
lemma euclid_ext_gcd' [simp]:
haftmann@60572
   440
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
haftmann@60572
   441
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
eberlm@62442
   442
eberlm@62442
   443
lemma euclid_ext_correct:
eberlm@62442
   444
  "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd x y"
eberlm@62442
   445
  using euclid_ext_correct'[of x y]
eberlm@62442
   446
  by (simp add: gcd_gcd_eucl case_prod_unfold)
haftmann@60572
   447
  
haftmann@60572
   448
lemma euclid_ext'_correct:
haftmann@60572
   449
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
eberlm@62442
   450
  using euclid_ext_correct'[of a b]
eberlm@62442
   451
  by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)
haftmann@60572
   452
haftmann@60572
   453
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
haftmann@60572
   454
  using euclid_ext'_correct by blast
haftmann@60572
   455
haftmann@60572
   456
end
haftmann@58023
   457
haftmann@58023
   458
haftmann@60572
   459
subsection \<open>Typical instances\<close>
haftmann@58023
   460
haftmann@58023
   461
instantiation nat :: euclidean_semiring
haftmann@58023
   462
begin
haftmann@58023
   463
haftmann@58023
   464
definition [simp]:
haftmann@58023
   465
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
haftmann@58023
   466
haftmann@58023
   467
instance proof
haftmann@59061
   468
qed simp_all
haftmann@58023
   469
haftmann@58023
   470
end
haftmann@58023
   471
eberlm@62422
   472
haftmann@58023
   473
instantiation int :: euclidean_ring
haftmann@58023
   474
begin
haftmann@58023
   475
haftmann@58023
   476
definition [simp]:
haftmann@58023
   477
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
haftmann@58023
   478
wenzelm@60580
   479
instance
haftmann@60686
   480
by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
haftmann@58023
   481
haftmann@58023
   482
end
haftmann@58023
   483
eberlm@62422
   484
haftmann@60572
   485
instantiation poly :: (field) euclidean_ring
haftmann@60571
   486
begin
haftmann@60571
   487
haftmann@60571
   488
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
eberlm@62422
   489
  where "euclidean_size p = (if p = 0 then 0 else 2 ^ degree p)"
haftmann@60571
   490
haftmann@60600
   491
lemma euclidean_size_poly_0 [simp]:
haftmann@60600
   492
  "euclidean_size (0::'a poly) = 0"
haftmann@60600
   493
  by (simp add: euclidean_size_poly_def)
haftmann@60600
   494
haftmann@60600
   495
lemma euclidean_size_poly_not_0 [simp]:
eberlm@62422
   496
  "p \<noteq> 0 \<Longrightarrow> euclidean_size p = 2 ^ degree p"
haftmann@60600
   497
  by (simp add: euclidean_size_poly_def)
haftmann@60600
   498
haftmann@60571
   499
instance
haftmann@60600
   500
proof
haftmann@60571
   501
  fix p q :: "'a poly"
haftmann@60600
   502
  assume "q \<noteq> 0"
haftmann@60600
   503
  then have "p mod q = 0 \<or> degree (p mod q) < degree q"
haftmann@60600
   504
    by (rule degree_mod_less [of q p])  
haftmann@60600
   505
  with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
haftmann@60600
   506
    by (cases "p mod q = 0") simp_all
haftmann@60571
   507
next
haftmann@60571
   508
  fix p q :: "'a poly"
haftmann@60571
   509
  assume "q \<noteq> 0"
haftmann@60600
   510
  from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
haftmann@60571
   511
    by (rule degree_mult_right_le)
haftmann@60600
   512
  with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
haftmann@60600
   513
    by (cases "p = 0") simp_all
eberlm@62422
   514
qed simp
haftmann@60571
   515
haftmann@58023
   516
end
haftmann@60571
   517
eberlm@62422
   518
eberlm@62422
   519
instance nat :: euclidean_semiring_gcd
eberlm@62422
   520
proof
eberlm@62422
   521
  show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
   522
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
   523
  show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
eberlm@62422
   524
    by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
eberlm@62422
   525
qed
eberlm@62422
   526
eberlm@62422
   527
instance int :: euclidean_ring_gcd
eberlm@62422
   528
proof
eberlm@62422
   529
  show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
eberlm@62422
   530
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
eberlm@62422
   531
  show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
eberlm@62422
   532
    by (intro ext, simp add: lcm_eucl_def lcm_altdef_int 
eberlm@62422
   533
          semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
eberlm@62422
   534
qed
eberlm@62422
   535
eberlm@62422
   536
eberlm@62422
   537
instantiation poly :: (field) euclidean_ring_gcd
eberlm@62422
   538
begin
eberlm@62422
   539
eberlm@62422
   540
definition gcd_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@62422
   541
  "gcd_poly = gcd_eucl"
eberlm@62422
   542
  
eberlm@62422
   543
definition lcm_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
eberlm@62422
   544
  "lcm_poly = lcm_eucl"
eberlm@62422
   545
  
eberlm@62422
   546
definition Gcd_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@62422
   547
  "Gcd_poly = Gcd_eucl"
eberlm@62422
   548
  
eberlm@62422
   549
definition Lcm_poly :: "'a poly set \<Rightarrow> 'a poly" where
eberlm@62422
   550
  "Lcm_poly = Lcm_eucl"
eberlm@62422
   551
eberlm@62422
   552
instance by standard (simp_all only: gcd_poly_def lcm_poly_def Gcd_poly_def Lcm_poly_def)
eberlm@62422
   553
end
haftmann@60687
   554
eberlm@62425
   555
lemma poly_gcd_monic:
eberlm@62425
   556
  "lead_coeff (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)"
eberlm@62425
   557
  using unit_factor_gcd[of x y]
eberlm@62425
   558
  by (simp add: unit_factor_poly_def monom_0 one_poly_def lead_coeff_def split: if_split_asm)
eberlm@62425
   559
eberlm@62425
   560
lemma poly_dvd_antisym:
eberlm@62425
   561
  fixes p q :: "'a::idom poly"
eberlm@62425
   562
  assumes coeff: "coeff p (degree p) = coeff q (degree q)"
eberlm@62425
   563
  assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
eberlm@62425
   564
proof (cases "p = 0")
eberlm@62425
   565
  case True with coeff show "p = q" by simp
eberlm@62425
   566
next
eberlm@62425
   567
  case False with coeff have "q \<noteq> 0" by auto
eberlm@62425
   568
  have degree: "degree p = degree q"
eberlm@62425
   569
    using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
eberlm@62425
   570
    by (intro order_antisym dvd_imp_degree_le)
eberlm@62425
   571
eberlm@62425
   572
  from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
eberlm@62425
   573
  with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
eberlm@62425
   574
  with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
eberlm@62425
   575
    by (simp add: degree_mult_eq)
eberlm@62425
   576
  with coeff a show "p = q"
eberlm@62425
   577
    by (cases a, auto split: if_splits)
eberlm@62425
   578
qed
eberlm@62425
   579
eberlm@62425
   580
lemma poly_gcd_unique:
eberlm@62425
   581
  fixes d x y :: "_ poly"
eberlm@62425
   582
  assumes dvd1: "d dvd x" and dvd2: "d dvd y"
eberlm@62425
   583
    and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
eberlm@62425
   584
    and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
eberlm@62425
   585
  shows "d = gcd x y"
eberlm@62425
   586
  using assms by (intro gcdI) (auto simp: normalize_poly_def split: if_split_asm)
eberlm@62425
   587
eberlm@62425
   588
lemma poly_gcd_code [code]:
eberlm@62425
   589
  "gcd x y = (if y = 0 then normalize x else gcd y (x mod (y :: _ poly)))"
eberlm@62425
   590
  by (simp add: gcd_0 gcd_non_0)
eberlm@62425
   591
haftmann@60571
   592
end