src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Mon Jan 09 18:53:06 2017 +0100 (2017-01-09)
changeset 64848 c50db2128048
parent 64786 340db65fd2c1
child 64850 fc9265882329
permissions -rw-r--r--
slightly generalized type class hierarchy concerning unit factors, to allow for lean polynomial normalization
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@64786
    12
subsection \<open>Generic construction of the (simple) euclidean algorithm\<close>
haftmann@64786
    13
  
haftmann@64784
    14
context euclidean_semiring
haftmann@64784
    15
begin
eberlm@63498
    16
haftmann@64786
    17
context
haftmann@64786
    18
begin
haftmann@64786
    19
haftmann@64786
    20
qualified function gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@64786
    21
  where "gcd a b = (if b = 0 then normalize a else gcd b (a mod b))"
haftmann@60572
    22
  by pat_completeness simp
haftmann@60569
    23
termination
haftmann@60569
    24
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
    25
haftmann@64786
    26
declare gcd.simps [simp del]
haftmann@58023
    27
haftmann@64786
    28
lemma eucl_induct [case_names zero mod]:
haftmann@60569
    29
  assumes H1: "\<And>b. P b 0"
haftmann@60569
    30
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
    31
  shows "P a b"
haftmann@64786
    32
proof (induct a b rule: gcd.induct)
haftmann@64786
    33
  case (1 a b)
haftmann@60569
    34
  show ?case
haftmann@60569
    35
  proof (cases "b = 0")
haftmann@60569
    36
    case True then show "P a b" by simp (rule H1)
haftmann@60569
    37
  next
haftmann@60569
    38
    case False
haftmann@60600
    39
    then have "P b (a mod b)"
haftmann@60600
    40
      by (rule "1.hyps")
haftmann@60569
    41
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
    42
      by (blast intro: H2)
haftmann@60569
    43
  qed
haftmann@58023
    44
qed
haftmann@64786
    45
  
haftmann@64786
    46
qualified lemma gcd_0:
haftmann@64786
    47
  "gcd a 0 = normalize a"
haftmann@64786
    48
  by (simp add: gcd.simps [of a 0])
haftmann@64786
    49
  
haftmann@64786
    50
qualified lemma gcd_mod:
haftmann@64786
    51
  "a \<noteq> 0 \<Longrightarrow> gcd a (b mod a) = gcd b a"
haftmann@64786
    52
  by (simp add: gcd.simps [of b 0] gcd.simps [of b a])
haftmann@58023
    53
haftmann@64786
    54
qualified definition lcm :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@64786
    55
  where "lcm a b = normalize (a * b) div gcd a b"
haftmann@64786
    56
haftmann@64786
    57
qualified definition Lcm :: "'a set \<Rightarrow> 'a" \<comment>
haftmann@64786
    58
    \<open>Somewhat complicated definition of Lcm that has the advantage of working
haftmann@64786
    59
    for infinite sets as well\<close>
haftmann@64786
    60
  where
haftmann@64786
    61
  [code del]: "Lcm A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
    62
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
    63
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60634
    64
       in normalize l 
haftmann@58023
    65
      else 0)"
haftmann@58023
    66
haftmann@64786
    67
qualified definition Gcd :: "'a set \<Rightarrow> 'a"
haftmann@64786
    68
  where [code del]: "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
eberlm@62428
    69
haftmann@64786
    70
end    
haftmann@60572
    71
haftmann@64786
    72
lemma semiring_gcd:
haftmann@64786
    73
  "class.semiring_gcd one zero times gcd lcm
haftmann@64848
    74
    divide plus minus unit_factor normalize"
haftmann@64786
    75
proof
haftmann@64786
    76
  show "gcd a b dvd a"
haftmann@64786
    77
    and "gcd a b dvd b" for a b
haftmann@64786
    78
    by (induct a b rule: eucl_induct)
haftmann@64786
    79
      (simp_all add: local.gcd_0 local.gcd_mod dvd_mod_iff)
eberlm@62422
    80
next
haftmann@64786
    81
  show "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> c dvd gcd a b" for a b c
haftmann@64786
    82
  proof (induct a b rule: eucl_induct)
haftmann@64786
    83
    case (zero a) from \<open>c dvd a\<close> show ?case
haftmann@64786
    84
      by (rule dvd_trans) (simp add: local.gcd_0)
eberlm@62422
    85
  next
haftmann@64786
    86
    case (mod a b)
haftmann@64786
    87
    then show ?case
haftmann@64786
    88
      by (simp add: local.gcd_mod dvd_mod_iff)
eberlm@62422
    89
  qed
eberlm@62422
    90
next
haftmann@64786
    91
  show "normalize (gcd a b) = gcd a b" for a b
haftmann@64786
    92
    by (induct a b rule: eucl_induct)
haftmann@64786
    93
      (simp_all add: local.gcd_0 local.gcd_mod)
haftmann@64786
    94
next
haftmann@64786
    95
  show "lcm a b = normalize (a * b) div gcd a b" for a b
haftmann@64786
    96
    by (fact local.lcm_def)
eberlm@63498
    97
qed
eberlm@63498
    98
haftmann@64786
    99
interpretation semiring_gcd one zero times gcd lcm
haftmann@64848
   100
  divide plus minus unit_factor normalize
haftmann@64786
   101
  by (fact semiring_gcd)
eberlm@63498
   102
  
haftmann@64786
   103
lemma semiring_Gcd:
haftmann@64786
   104
  "class.semiring_Gcd one zero times gcd lcm Gcd Lcm
haftmann@64848
   105
    divide plus minus unit_factor normalize"
haftmann@64786
   106
proof -
haftmann@64786
   107
  show ?thesis
haftmann@64786
   108
  proof
haftmann@64786
   109
    have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>b. (\<forall>a\<in>A. a dvd b) \<longrightarrow> Lcm A dvd b)" for A
haftmann@64786
   110
    proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
eberlm@63498
   111
      case False
haftmann@64786
   112
      then have "Lcm A = 0"
haftmann@64786
   113
        by (auto simp add: local.Lcm_def)
haftmann@64786
   114
      with False show ?thesis
haftmann@64786
   115
        by auto
eberlm@63498
   116
    next
eberlm@63498
   117
      case True
haftmann@64786
   118
      then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0" "\<forall>a\<in>A. a dvd l\<^sub>0" by blast
haftmann@64786
   119
      define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
haftmann@64786
   120
      define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
haftmann@64786
   121
      have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@64786
   122
        apply (subst n_def)
haftmann@64786
   123
        apply (rule LeastI [of _ "euclidean_size l\<^sub>0"])
haftmann@64786
   124
        apply (rule exI [of _ l\<^sub>0])
haftmann@64786
   125
        apply (simp add: l\<^sub>0_props)
haftmann@64786
   126
        done
haftmann@64786
   127
      from someI_ex [OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l"
haftmann@64786
   128
        and "euclidean_size l = n" 
haftmann@64786
   129
        unfolding l_def by simp_all
haftmann@64786
   130
      {
haftmann@64786
   131
        fix l' assume "\<forall>a\<in>A. a dvd l'"
haftmann@64786
   132
        with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'"
haftmann@64786
   133
          by (auto intro: gcd_greatest)
haftmann@64786
   134
        moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0"
haftmann@64786
   135
          by simp
haftmann@64786
   136
        ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
haftmann@64786
   137
          euclidean_size b = euclidean_size (gcd l l')"
haftmann@64786
   138
          by (intro exI [of _ "gcd l l'"], auto)
haftmann@64786
   139
        then have "euclidean_size (gcd l l') \<ge> n"
haftmann@64786
   140
          by (subst n_def) (rule Least_le)
haftmann@64786
   141
        moreover have "euclidean_size (gcd l l') \<le> n"
haftmann@64786
   142
        proof -
haftmann@64786
   143
          have "gcd l l' dvd l"
haftmann@64786
   144
            by simp
haftmann@64786
   145
          then obtain a where "l = gcd l l' * a" ..
haftmann@64786
   146
          with \<open>l \<noteq> 0\<close> have "a \<noteq> 0"
haftmann@64786
   147
            by auto
haftmann@64786
   148
          hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
haftmann@64786
   149
            by (rule size_mult_mono)
haftmann@64786
   150
          also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
haftmann@64786
   151
          also note \<open>euclidean_size l = n\<close>
haftmann@64786
   152
          finally show "euclidean_size (gcd l l') \<le> n" .
haftmann@64786
   153
        qed
haftmann@64786
   154
        ultimately have *: "euclidean_size l = euclidean_size (gcd l l')" 
haftmann@64786
   155
          by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
haftmann@64786
   156
        from \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"
haftmann@64786
   157
          by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
haftmann@64786
   158
        hence "l dvd l'" by (rule dvd_trans [OF _ gcd_dvd2])
haftmann@64786
   159
      }
haftmann@64786
   160
      with \<open>\<forall>a\<in>A. a dvd l\<close> and \<open>l \<noteq> 0\<close>
haftmann@64786
   161
        have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
haftmann@64786
   162
          (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l')"
haftmann@64786
   163
        by auto
haftmann@64786
   164
      also from True have "normalize l = Lcm A"
haftmann@64786
   165
        by (simp add: local.Lcm_def Let_def n_def l_def)
eberlm@63498
   166
      finally show ?thesis .
eberlm@63498
   167
    qed
haftmann@64786
   168
    then show dvd_Lcm: "a \<in> A \<Longrightarrow> a dvd Lcm A"
haftmann@64786
   169
      and Lcm_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b" for A and a b
haftmann@64786
   170
      by auto
haftmann@64786
   171
    show "a \<in> A \<Longrightarrow> Gcd A dvd a" for A and a
haftmann@64786
   172
      by (auto simp add: local.Gcd_def intro: Lcm_least)
haftmann@64786
   173
    show "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A" for A and b
haftmann@64786
   174
      by (auto simp add: local.Gcd_def intro: dvd_Lcm)
haftmann@64786
   175
    show [simp]: "normalize (Lcm A) = Lcm A" for A
haftmann@64786
   176
      by (simp add: local.Lcm_def)
haftmann@64786
   177
    show "normalize (Gcd A) = Gcd A" for A
haftmann@64786
   178
      by (simp add: local.Gcd_def)
eberlm@62442
   179
  qed
eberlm@62442
   180
qed
eberlm@62442
   181
haftmann@64786
   182
interpretation semiring_Gcd one zero times gcd lcm Gcd Lcm
haftmann@64848
   183
    divide plus minus unit_factor normalize
haftmann@64786
   184
  by (fact semiring_Gcd)
haftmann@60598
   185
haftmann@64786
   186
subclass factorial_semiring
haftmann@64786
   187
proof -
haftmann@64786
   188
  show "class.factorial_semiring divide plus minus zero times one
haftmann@64848
   189
     unit_factor normalize"
haftmann@64786
   190
  proof (standard, rule factorial_semiring_altI_aux) -- \<open>FIXME rule\<close>
haftmann@64786
   191
    fix x assume "x \<noteq> 0"
haftmann@64786
   192
    thus "finite {p. p dvd x \<and> normalize p = p}"
haftmann@64786
   193
    proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
haftmann@64786
   194
      case (less x)
haftmann@64786
   195
      show ?case
haftmann@64786
   196
      proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
haftmann@64786
   197
        case False
haftmann@64786
   198
        have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
haftmann@64786
   199
        proof
haftmann@64786
   200
          fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
haftmann@64786
   201
          with False have "is_unit p \<or> x dvd p" by blast
haftmann@64786
   202
          thus "p \<in> {1, normalize x}"
haftmann@64786
   203
          proof (elim disjE)
haftmann@64786
   204
            assume "is_unit p"
haftmann@64786
   205
            hence "normalize p = 1" by (simp add: is_unit_normalize)
haftmann@64786
   206
            with p show ?thesis by simp
haftmann@64786
   207
          next
haftmann@64786
   208
            assume "x dvd p"
haftmann@64786
   209
            with p have "normalize p = normalize x" by (intro associatedI) simp_all
haftmann@64786
   210
            with p show ?thesis by simp
haftmann@64786
   211
          qed
haftmann@64786
   212
        qed
haftmann@64786
   213
        moreover have "finite \<dots>" by simp
haftmann@64786
   214
        ultimately show ?thesis by (rule finite_subset)
haftmann@64786
   215
      next
haftmann@64786
   216
        case True
haftmann@64786
   217
        then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
haftmann@64786
   218
        define z where "z = x div y"
haftmann@64786
   219
        let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
haftmann@64786
   220
        from y have x: "x = y * z" by (simp add: z_def)
haftmann@64786
   221
        with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
haftmann@64786
   222
        have normalized_factors_product:
haftmann@64786
   223
          "{p. p dvd a * b \<and> normalize p = p} = 
haftmann@64786
   224
             (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})" for a b
haftmann@64786
   225
        proof safe
haftmann@64786
   226
          fix p assume p: "p dvd a * b" "normalize p = p"
haftmann@64786
   227
          from dvd_productE[OF p(1)] guess x y . note xy = this
haftmann@64786
   228
          define x' y' where "x' = normalize x" and "y' = normalize y"
haftmann@64786
   229
          have "p = x' * y'"
haftmann@64786
   230
            by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
haftmann@64786
   231
          moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b" 
haftmann@64786
   232
            by (simp_all add: x'_def y'_def)
haftmann@64786
   233
          ultimately show "p \<in> (\<lambda>(x, y). x * y) ` 
haftmann@64786
   234
            ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
haftmann@64786
   235
            by blast
haftmann@64786
   236
        qed (auto simp: normalize_mult mult_dvd_mono)
haftmann@64786
   237
        from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
haftmann@64786
   238
        have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
haftmann@64786
   239
          by (subst x) (rule normalized_factors_product)
haftmann@64786
   240
        also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
haftmann@64786
   241
          by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
haftmann@64786
   242
        hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
haftmann@64786
   243
          by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
haftmann@64786
   244
             (auto simp: x)
haftmann@64786
   245
        finally show ?thesis .
haftmann@64786
   246
      qed
haftmann@64786
   247
    qed
haftmann@64786
   248
  next
haftmann@64786
   249
    fix p
haftmann@64786
   250
    assume "irreducible p"
haftmann@64786
   251
    then show "prime_elem p"
haftmann@64786
   252
      by (rule irreducible_imp_prime_elem_gcd)
haftmann@64786
   253
  qed
haftmann@64786
   254
qed
haftmann@60598
   255
haftmann@64786
   256
lemma Gcd_eucl_set [code]:
haftmann@64786
   257
  "Gcd (set xs) = foldl gcd 0 xs"
haftmann@64786
   258
  by (fact local.Gcd_set)
haftmann@60598
   259
haftmann@64786
   260
lemma Lcm_eucl_set [code]:
haftmann@64786
   261
  "Lcm (set xs) = foldl lcm 1 xs"
haftmann@64786
   262
  by (fact local.Lcm_set)
haftmann@64786
   263
 
haftmann@60598
   264
end
haftmann@60598
   265
haftmann@64786
   266
hide_const (open) gcd lcm Gcd Lcm
haftmann@64786
   267
haftmann@64786
   268
lemma prime_elem_int_abs_iff [simp]:
haftmann@64786
   269
  fixes p :: int
haftmann@64786
   270
  shows "prime_elem \<bar>p\<bar> \<longleftrightarrow> prime_elem p"
haftmann@64786
   271
  using prime_elem_normalize_iff [of p] by simp
haftmann@64786
   272
  
haftmann@64786
   273
lemma prime_elem_int_minus_iff [simp]:
haftmann@64786
   274
  fixes p :: int
haftmann@64786
   275
  shows "prime_elem (- p) \<longleftrightarrow> prime_elem p"
haftmann@64786
   276
  using prime_elem_normalize_iff [of "- p"] by simp
haftmann@64786
   277
haftmann@64786
   278
lemma prime_int_iff:
haftmann@64786
   279
  fixes p :: int
haftmann@64786
   280
  shows "prime p \<longleftrightarrow> p > 0 \<and> prime_elem p"
haftmann@64786
   281
  by (auto simp add: prime_def dest: prime_elem_not_zeroI)
haftmann@64786
   282
  
haftmann@64786
   283
  
haftmann@64786
   284
subsection \<open>The (simple) euclidean algorithm as gcd computation\<close>
haftmann@64786
   285
  
haftmann@58023
   286
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@64786
   287
  assumes gcd_eucl: "Euclidean_Algorithm.gcd = GCD.gcd"
haftmann@64786
   288
    and lcm_eucl: "Euclidean_Algorithm.lcm = GCD.lcm"
haftmann@64786
   289
  assumes Gcd_eucl: "Euclidean_Algorithm.Gcd = GCD.Gcd"
haftmann@64786
   290
    and Lcm_eucl: "Euclidean_Algorithm.Lcm = GCD.Lcm"
haftmann@58023
   291
begin
haftmann@58023
   292
eberlm@62422
   293
subclass semiring_gcd
haftmann@64786
   294
  unfolding gcd_eucl [symmetric] lcm_eucl [symmetric]
haftmann@64786
   295
  by (fact semiring_gcd)
haftmann@58023
   296
eberlm@62422
   297
subclass semiring_Gcd
haftmann@64786
   298
  unfolding  gcd_eucl [symmetric] lcm_eucl [symmetric]
haftmann@64786
   299
    Gcd_eucl [symmetric] Lcm_eucl [symmetric]
haftmann@64786
   300
  by (fact semiring_Gcd)
eberlm@63498
   301
eberlm@63498
   302
subclass factorial_semiring_gcd
eberlm@63498
   303
proof
haftmann@64786
   304
  show "gcd a b = gcd_factorial a b" for a b
haftmann@64786
   305
    apply (rule sym)
haftmann@64786
   306
    apply (rule gcdI)
haftmann@64786
   307
       apply (fact gcd_lcm_factorial)+
haftmann@64786
   308
    done
haftmann@64786
   309
  then show "lcm a b = lcm_factorial a b" for a b
eberlm@63498
   310
    by (simp add: lcm_factorial_gcd_factorial lcm_gcd)
haftmann@64786
   311
  show "Gcd A = Gcd_factorial A" for A
haftmann@64786
   312
    apply (rule sym)
haftmann@64786
   313
    apply (rule GcdI)
haftmann@64786
   314
       apply (fact gcd_lcm_factorial)+
haftmann@64786
   315
    done
haftmann@64786
   316
  show "Lcm A = Lcm_factorial A" for A
haftmann@64786
   317
    apply (rule sym)
haftmann@64786
   318
    apply (rule LcmI)
haftmann@64786
   319
       apply (fact gcd_lcm_factorial)+
haftmann@64786
   320
    done
eberlm@63498
   321
qed
eberlm@63498
   322
haftmann@64786
   323
lemma gcd_mod_right [simp]:
haftmann@64786
   324
  "a \<noteq> 0 \<Longrightarrow> gcd a (b mod a) = gcd a b"
haftmann@64786
   325
  unfolding gcd.commute [of a b]
haftmann@64786
   326
  by (simp add: gcd_eucl [symmetric] local.gcd_mod)
haftmann@58023
   327
haftmann@64786
   328
lemma gcd_mod_left [simp]:
haftmann@64786
   329
  "b \<noteq> 0 \<Longrightarrow> gcd (a mod b) b = gcd a b"
haftmann@64786
   330
  by (drule gcd_mod_right [of _ a]) (simp add: gcd.commute)
haftmann@58023
   331
haftmann@58023
   332
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   333
  assumes "a \<noteq> 0"
haftmann@58023
   334
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   335
proof -
haftmann@64786
   336
  from gcd_dvd1 obtain c where A: "a = gcd a b * c" ..
haftmann@64786
   337
  with assms have "c \<noteq> 0"
haftmann@64786
   338
    by auto
haftmann@64786
   339
  moreover from this
haftmann@64786
   340
  have "euclidean_size (gcd a b) \<le> euclidean_size (gcd a b * c)"
haftmann@64786
   341
    by (rule size_mult_mono)
haftmann@64786
   342
  with A show ?thesis
haftmann@64786
   343
    by simp
haftmann@58023
   344
qed
haftmann@58023
   345
haftmann@58023
   346
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   347
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   348
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   349
haftmann@58023
   350
lemma euclidean_size_gcd_less1:
haftmann@64786
   351
  assumes "a \<noteq> 0" and "\<not> a dvd b"
haftmann@58023
   352
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   353
proof (rule ccontr)
haftmann@58023
   354
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
eberlm@62422
   355
  with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   356
    by (intro le_antisym, simp_all)
eberlm@62422
   357
  have "a dvd gcd a b"
eberlm@62422
   358
    by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
eberlm@62422
   359
  hence "a dvd b" using dvd_gcdD2 by blast
haftmann@64786
   360
  with \<open>\<not> a dvd b\<close> show False by contradiction
haftmann@58023
   361
qed
haftmann@58023
   362
haftmann@58023
   363
lemma euclidean_size_gcd_less2:
haftmann@64786
   364
  assumes "b \<noteq> 0" and "\<not> b dvd a"
haftmann@58023
   365
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   366
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   367
haftmann@58023
   368
lemma euclidean_size_lcm_le1: 
haftmann@58023
   369
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
   370
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
   371
proof -
haftmann@60690
   372
  have "a dvd lcm a b" by (rule dvd_lcm1)
haftmann@60690
   373
  then obtain c where A: "lcm a b = a * c" ..
eberlm@62429
   374
  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
   375
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
   376
qed
haftmann@58023
   377
haftmann@58023
   378
lemma euclidean_size_lcm_le2:
haftmann@58023
   379
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
   380
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
   381
haftmann@58023
   382
lemma euclidean_size_lcm_less1:
haftmann@64786
   383
  assumes "b \<noteq> 0" and "\<not> b dvd a"
haftmann@58023
   384
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
   385
proof (rule ccontr)
haftmann@58023
   386
  from assms have "a \<noteq> 0" by auto
haftmann@58023
   387
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
   388
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
   389
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
   390
  with assms have "lcm a b dvd a" 
eberlm@62429
   391
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
eberlm@62422
   392
  hence "b dvd a" by (rule lcm_dvdD2)
wenzelm@60526
   393
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
   394
qed
haftmann@58023
   395
haftmann@58023
   396
lemma euclidean_size_lcm_less2:
haftmann@64786
   397
  assumes "a \<noteq> 0" and "\<not> a dvd b"
haftmann@58023
   398
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
   399
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
   400
haftmann@58023
   401
end
haftmann@58023
   402
haftmann@64786
   403
lemma factorial_euclidean_semiring_gcdI:
haftmann@64786
   404
  "OFCLASS('a::{factorial_semiring_gcd, euclidean_semiring}, euclidean_semiring_gcd_class)"
haftmann@64786
   405
proof
haftmann@64786
   406
  interpret semiring_Gcd 1 0 times
haftmann@64786
   407
    Euclidean_Algorithm.gcd Euclidean_Algorithm.lcm
haftmann@64786
   408
    Euclidean_Algorithm.Gcd Euclidean_Algorithm.Lcm
haftmann@64848
   409
    divide plus minus unit_factor normalize
haftmann@64786
   410
    rewrites "dvd.dvd op * = Rings.dvd"
haftmann@64786
   411
    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
haftmann@64786
   412
  show [simp]: "Euclidean_Algorithm.gcd = (gcd :: 'a \<Rightarrow> _)"
haftmann@64786
   413
  proof (rule ext)+
haftmann@64786
   414
    fix a b :: 'a
haftmann@64786
   415
    show "Euclidean_Algorithm.gcd a b = gcd a b"
haftmann@64786
   416
    proof (induct a b rule: eucl_induct)
haftmann@64786
   417
      case zero
haftmann@64786
   418
      then show ?case
haftmann@64786
   419
        by simp
haftmann@64786
   420
    next
haftmann@64786
   421
      case (mod a b)
haftmann@64786
   422
      moreover have "gcd b (a mod b) = gcd b a"
haftmann@64786
   423
        using GCD.gcd_add_mult [of b "a div b" "a mod b", symmetric]
haftmann@64786
   424
          by (simp add: div_mult_mod_eq)
haftmann@64786
   425
      ultimately show ?case
haftmann@64786
   426
        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
haftmann@64786
   427
    qed
haftmann@64786
   428
  qed
haftmann@64786
   429
  show [simp]: "Euclidean_Algorithm.Lcm = (Lcm :: 'a set \<Rightarrow> _)"
haftmann@64786
   430
    by (auto intro!: Lcm_eqI GCD.dvd_Lcm GCD.Lcm_least)
haftmann@64786
   431
  show "Euclidean_Algorithm.lcm = (lcm :: 'a \<Rightarrow> _)"
haftmann@64786
   432
    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
haftmann@64786
   433
  show "Euclidean_Algorithm.Gcd = (Gcd :: 'a set \<Rightarrow> _)"
haftmann@64786
   434
    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
haftmann@64786
   435
qed
eberlm@63498
   436
haftmann@58023
   437
haftmann@64786
   438
subsection \<open>The extended euclidean algorithm\<close>
haftmann@64786
   439
  
haftmann@58023
   440
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
   441
begin
haftmann@58023
   442
haftmann@58023
   443
subclass euclidean_ring ..
haftmann@60439
   444
subclass ring_gcd ..
eberlm@63498
   445
subclass factorial_ring_gcd ..
haftmann@60439
   446
haftmann@64786
   447
function euclid_ext_aux :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> ('a \<times> 'a) \<times> 'a"
haftmann@64786
   448
  where "euclid_ext_aux s' s t' t r' r = (
haftmann@64786
   449
     if r = 0 then let c = 1 div unit_factor r' in ((s' * c, t' * c), normalize r')
haftmann@64786
   450
     else let q = r' div r
haftmann@64786
   451
          in euclid_ext_aux s (s' - q * s) t (t' - q * t) r (r' mod r))"
haftmann@64786
   452
  by auto
haftmann@64786
   453
termination
haftmann@64786
   454
  by (relation "measure (\<lambda>(_, _, _, _, _, b). euclidean_size b)")
haftmann@64786
   455
    (simp_all add: mod_size_less)
eberlm@62442
   456
haftmann@64786
   457
abbreviation (input) euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<times> 'a) \<times> 'a"
haftmann@64786
   458
  where "euclid_ext \<equiv> euclid_ext_aux 1 0 0 1"
haftmann@64786
   459
    
haftmann@64786
   460
lemma
haftmann@64786
   461
  assumes "gcd r' r = gcd a b"
haftmann@64786
   462
  assumes "s' * a + t' * b = r'"
haftmann@64786
   463
  assumes "s * a + t * b = r"
haftmann@64786
   464
  assumes "euclid_ext_aux s' s t' t r' r = ((x, y), c)"
haftmann@64786
   465
  shows euclid_ext_aux_eq_gcd: "c = gcd a b"
haftmann@64786
   466
    and euclid_ext_aux_bezout: "x * a + y * b = gcd a b"
haftmann@64786
   467
proof -
haftmann@64786
   468
  have "case euclid_ext_aux s' s t' t r' r of ((x, y), c) \<Rightarrow> 
haftmann@64786
   469
    x * a + y * b = c \<and> c = gcd a b" (is "?P (euclid_ext_aux s' s t' t r' r)")
haftmann@64786
   470
    using assms(1-3)
haftmann@64786
   471
  proof (induction s' s t' t r' r rule: euclid_ext_aux.induct)
haftmann@64786
   472
    case (1 s' s t' t r' r)
haftmann@64786
   473
    show ?case
haftmann@64786
   474
    proof (cases "r = 0")
haftmann@64786
   475
      case True
haftmann@64786
   476
      hence "euclid_ext_aux s' s t' t r' r = 
haftmann@64786
   477
               ((s' div unit_factor r', t' div unit_factor r'), normalize r')"
haftmann@64786
   478
        by (subst euclid_ext_aux.simps) (simp add: Let_def)
haftmann@64786
   479
      also have "?P \<dots>"
haftmann@64786
   480
      proof safe
haftmann@64786
   481
        have "s' div unit_factor r' * a + t' div unit_factor r' * b = 
haftmann@64786
   482
                (s' * a + t' * b) div unit_factor r'"
haftmann@64786
   483
          by (cases "r' = 0") (simp_all add: unit_div_commute)
haftmann@64786
   484
        also have "s' * a + t' * b = r'" by fact
haftmann@64786
   485
        also have "\<dots> div unit_factor r' = normalize r'" by simp
haftmann@64786
   486
        finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .
haftmann@64786
   487
      next
haftmann@64786
   488
        from "1.prems" True show "normalize r' = gcd a b"
haftmann@64786
   489
          by simp
haftmann@64786
   490
      qed
haftmann@64786
   491
      finally show ?thesis .
haftmann@64786
   492
    next
haftmann@64786
   493
      case False
haftmann@64786
   494
      hence "euclid_ext_aux s' s t' t r' r = 
haftmann@64786
   495
             euclid_ext_aux s (s' - r' div r * s) t (t' - r' div r * t) r (r' mod r)"
haftmann@64786
   496
        by (subst euclid_ext_aux.simps) (simp add: Let_def)
haftmann@64786
   497
      also from "1.prems" False have "?P \<dots>"
haftmann@64786
   498
      proof (intro "1.IH")
haftmann@64786
   499
        have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =
haftmann@64786
   500
              (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)
haftmann@64786
   501
        also have "s' * a + t' * b = r'" by fact
haftmann@64786
   502
        also have "s * a + t * b = r" by fact
haftmann@64786
   503
        also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]
haftmann@64786
   504
          by (simp add: algebra_simps)
haftmann@64786
   505
        finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .
haftmann@64786
   506
      qed (auto simp: gcd_mod_right algebra_simps minus_mod_eq_div_mult [symmetric] gcd.commute)
haftmann@64786
   507
      finally show ?thesis .
haftmann@64786
   508
    qed
haftmann@64786
   509
  qed
haftmann@64786
   510
  with assms(4) show "c = gcd a b" "x * a + y * b = gcd a b"
haftmann@64786
   511
    by simp_all
haftmann@64786
   512
qed
haftmann@60572
   513
haftmann@64786
   514
declare euclid_ext_aux.simps [simp del]
haftmann@64786
   515
haftmann@64786
   516
definition bezout_coefficients :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@64786
   517
  where [code]: "bezout_coefficients a b = fst (euclid_ext a b)"
haftmann@64786
   518
haftmann@64786
   519
lemma bezout_coefficients_0: 
haftmann@64786
   520
  "bezout_coefficients a 0 = (1 div unit_factor a, 0)"
haftmann@64786
   521
  by (simp add: bezout_coefficients_def euclid_ext_aux.simps)
haftmann@64786
   522
haftmann@64786
   523
lemma bezout_coefficients_left_0: 
haftmann@64786
   524
  "bezout_coefficients 0 a = (0, 1 div unit_factor a)"
haftmann@64786
   525
  by (simp add: bezout_coefficients_def euclid_ext_aux.simps)
haftmann@64786
   526
haftmann@64786
   527
lemma bezout_coefficients:
haftmann@64786
   528
  assumes "bezout_coefficients a b = (x, y)"
haftmann@64786
   529
  shows "x * a + y * b = gcd a b"
haftmann@64786
   530
  using assms by (simp add: bezout_coefficients_def
haftmann@64786
   531
    euclid_ext_aux_bezout [of a b a b 1 0 0 1 x y] prod_eq_iff)
haftmann@64786
   532
haftmann@64786
   533
lemma bezout_coefficients_fst_snd:
haftmann@64786
   534
  "fst (bezout_coefficients a b) * a + snd (bezout_coefficients a b) * b = gcd a b"
haftmann@64786
   535
  by (rule bezout_coefficients) simp
haftmann@64786
   536
haftmann@64786
   537
lemma euclid_ext_eq [simp]:
haftmann@64786
   538
  "euclid_ext a b = (bezout_coefficients a b, gcd a b)" (is "?p = ?q")
haftmann@64786
   539
proof
haftmann@64786
   540
  show "fst ?p = fst ?q"
haftmann@64786
   541
    by (simp add: bezout_coefficients_def)
haftmann@64786
   542
  have "snd (euclid_ext_aux 1 0 0 1 a b) = gcd a b"
haftmann@64786
   543
    by (rule euclid_ext_aux_eq_gcd [of a b a b 1 0 0 1])
haftmann@64786
   544
      (simp_all add: prod_eq_iff)
haftmann@64786
   545
  then show "snd ?p = snd ?q"
haftmann@64786
   546
    by simp
haftmann@64786
   547
qed
haftmann@64786
   548
haftmann@64786
   549
declare euclid_ext_eq [symmetric, code_unfold]
haftmann@60572
   550
haftmann@60572
   551
end
haftmann@58023
   552
haftmann@58023
   553
haftmann@60572
   554
subsection \<open>Typical instances\<close>
haftmann@58023
   555
eberlm@62422
   556
instance nat :: euclidean_semiring_gcd
eberlm@62422
   557
proof
haftmann@64786
   558
  interpret semiring_Gcd 1 0 times
haftmann@64786
   559
    "Euclidean_Algorithm.gcd" "Euclidean_Algorithm.lcm"
haftmann@64786
   560
    "Euclidean_Algorithm.Gcd" "Euclidean_Algorithm.Lcm"
haftmann@64848
   561
    divide plus minus unit_factor normalize
haftmann@64786
   562
    rewrites "dvd.dvd op * = Rings.dvd"
haftmann@64786
   563
    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
haftmann@64786
   564
  show [simp]: "(Euclidean_Algorithm.gcd :: nat \<Rightarrow> _) = gcd"
haftmann@64786
   565
  proof (rule ext)+
haftmann@64786
   566
    fix m n :: nat
haftmann@64786
   567
    show "Euclidean_Algorithm.gcd m n = gcd m n"
haftmann@64786
   568
    proof (induct m n rule: eucl_induct)
haftmann@64786
   569
      case zero
haftmann@64786
   570
      then show ?case
haftmann@64786
   571
        by simp
haftmann@64786
   572
    next
haftmann@64786
   573
      case (mod m n)
haftmann@64786
   574
      then have "gcd n (m mod n) = gcd n m"
haftmann@64786
   575
        using gcd_nat.simps [of m n] by (simp add: ac_simps)
haftmann@64786
   576
      with mod show ?case
haftmann@64786
   577
        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
haftmann@64786
   578
    qed
haftmann@64786
   579
  qed
haftmann@64786
   580
  show [simp]: "(Euclidean_Algorithm.Lcm :: nat set \<Rightarrow> _) = Lcm"
haftmann@64786
   581
    by (auto intro!: ext Lcm_eqI)
haftmann@64786
   582
  show "(Euclidean_Algorithm.lcm :: nat \<Rightarrow> _) = lcm"
haftmann@64786
   583
    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
haftmann@64786
   584
  show "(Euclidean_Algorithm.Gcd :: nat set \<Rightarrow> _) = Gcd"
haftmann@64786
   585
    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
eberlm@62422
   586
qed
eberlm@62422
   587
eberlm@62422
   588
instance int :: euclidean_ring_gcd
eberlm@62422
   589
proof
haftmann@64786
   590
  interpret semiring_Gcd 1 0 times
haftmann@64786
   591
    "Euclidean_Algorithm.gcd" "Euclidean_Algorithm.lcm"
haftmann@64786
   592
    "Euclidean_Algorithm.Gcd" "Euclidean_Algorithm.Lcm"
haftmann@64848
   593
    divide plus minus unit_factor normalize
haftmann@64786
   594
    rewrites "dvd.dvd op * = Rings.dvd"
haftmann@64786
   595
    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
haftmann@64786
   596
  show [simp]: "(Euclidean_Algorithm.gcd :: int \<Rightarrow> _) = gcd"
haftmann@64786
   597
  proof (rule ext)+
haftmann@64786
   598
    fix k l :: int
haftmann@64786
   599
    show "Euclidean_Algorithm.gcd k l = gcd k l"
haftmann@64786
   600
    proof (induct k l rule: eucl_induct)
haftmann@64786
   601
      case zero
haftmann@64786
   602
      then show ?case
haftmann@64786
   603
        by simp
haftmann@64786
   604
    next
haftmann@64786
   605
      case (mod k l)
haftmann@64786
   606
      have "gcd l (k mod l) = gcd l k"
haftmann@64786
   607
      proof (cases l "0::int" rule: linorder_cases)
haftmann@64786
   608
        case less
haftmann@64786
   609
        then show ?thesis
haftmann@64786
   610
          using gcd_non_0_int [of "- l" "- k"] by (simp add: ac_simps)
haftmann@64786
   611
      next
haftmann@64786
   612
        case equal
haftmann@64786
   613
        with mod show ?thesis
haftmann@64786
   614
          by simp
haftmann@64786
   615
      next
haftmann@64786
   616
        case greater
haftmann@64786
   617
        then show ?thesis
haftmann@64786
   618
          using gcd_non_0_int [of l k] by (simp add: ac_simps)
haftmann@64786
   619
      qed
haftmann@64786
   620
      with mod show ?case
haftmann@64786
   621
        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
haftmann@64786
   622
    qed
haftmann@64786
   623
  qed
haftmann@64786
   624
  show [simp]: "(Euclidean_Algorithm.Lcm :: int set \<Rightarrow> _) = Lcm"
haftmann@64786
   625
    by (auto intro!: ext Lcm_eqI)
haftmann@64786
   626
  show "(Euclidean_Algorithm.lcm :: int \<Rightarrow> _) = lcm"
haftmann@64786
   627
    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
haftmann@64786
   628
  show "(Euclidean_Algorithm.Gcd :: int set \<Rightarrow> _) = Gcd"
haftmann@64786
   629
    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
eberlm@62422
   630
qed
eberlm@62422
   631
haftmann@63924
   632
end