src/HOL/Number_Theory/Euclidean_Algorithm.thy
author eberlm <eberlm@in.tum.de>
Wed, 13 Jul 2016 15:46:52 +0200
changeset 63498 a3fe3250d05d
parent 63167 0909deb8059b
child 63633 2accfb71e33b
permissions -rw-r--r--
Reformed factorial rings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     1
(* Author: Manuel Eberl *)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     2
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
     3
section \<open>Abstract euclidean algorithm\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     4
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     5
theory Euclidean_Algorithm
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
     6
imports "~~/src/HOL/GCD" Factorial_Ring
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     7
begin
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
     8
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
     9
text \<open>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    10
  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    11
  implemented. It must provide:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    12
  \begin{itemize}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    13
  \item division with remainder
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    14
  \item a size function such that @{term "size (a mod b) < size b"} 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    15
        for any @{term "b \<noteq> 0"}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    16
  \end{itemize}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    17
  The existence of these functions makes it possible to derive gcd and lcm functions 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    18
  for any Euclidean semiring.
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
    19
\<close> 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
    20
class euclidean_semiring = semiring_div + normalization_semidom + 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    21
  fixes euclidean_size :: "'a \<Rightarrow> nat"
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
    22
  assumes size_0 [simp]: "euclidean_size 0 = 0"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    23
  assumes mod_size_less: 
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    24
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    25
  assumes size_mult_mono:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
    26
    "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    27
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    28
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    29
lemma euclidean_division:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    30
  fixes a :: 'a and b :: 'a
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    31
  assumes "b \<noteq> 0"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    32
  obtains s and t where "a = s * b + t" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    33
    and "euclidean_size t < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    34
proof -
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    35
  from div_mod_equality [of a b 0] 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    36
     have "a = a div b * b + a mod b" by simp
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    37
  with that and assms show ?thesis by (auto simp add: mod_size_less)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    38
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    39
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    40
lemma dvd_euclidean_size_eq_imp_dvd:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    41
  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    42
  shows "a dvd b"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    43
proof (rule ccontr)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    44
  assume "\<not> a dvd b"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    45
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    46
  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    47
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
    48
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
    49
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    50
      using size_mult_mono by force
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    51
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    52
  have "euclidean_size (b mod a) < euclidean_size a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    53
      using mod_size_less by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    54
  ultimately show False using size_eq by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    55
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    56
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    57
lemma size_mult_mono': "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (b * a)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    58
  by (subst mult.commute) (rule size_mult_mono)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    59
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    60
lemma euclidean_size_times_unit:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    61
  assumes "is_unit a"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    62
  shows   "euclidean_size (a * b) = euclidean_size b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    63
proof (rule antisym)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    64
  from assms have [simp]: "a \<noteq> 0" by auto
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    65
  thus "euclidean_size (a * b) \<ge> euclidean_size b" by (rule size_mult_mono')
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    66
  from assms have "is_unit (1 div a)" by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    67
  hence "1 div a \<noteq> 0" by (intro notI) simp_all
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    68
  hence "euclidean_size (a * b) \<le> euclidean_size ((1 div a) * (a * b))"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    69
    by (rule size_mult_mono')
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    70
  also from assms have "(1 div a) * (a * b) = b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    71
    by (simp add: algebra_simps unit_div_mult_swap)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    72
  finally show "euclidean_size (a * b) \<le> euclidean_size b" .
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    73
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    74
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    75
lemma euclidean_size_unit: "is_unit x \<Longrightarrow> euclidean_size x = euclidean_size 1"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    76
  using euclidean_size_times_unit[of x 1] by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    77
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    78
lemma unit_iff_euclidean_size: 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    79
  "is_unit x \<longleftrightarrow> euclidean_size x = euclidean_size 1 \<and> x \<noteq> 0"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    80
proof safe
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    81
  assume A: "x \<noteq> 0" and B: "euclidean_size x = euclidean_size 1"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    82
  show "is_unit x" by (rule dvd_euclidean_size_eq_imp_dvd[OF A _ B]) simp_all
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    83
qed (auto intro: euclidean_size_unit)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    84
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    85
lemma euclidean_size_times_nonunit:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    86
  assumes "a \<noteq> 0" "b \<noteq> 0" "\<not>is_unit a"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    87
  shows   "euclidean_size b < euclidean_size (a * b)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    88
proof (rule ccontr)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    89
  assume "\<not>euclidean_size b < euclidean_size (a * b)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    90
  with size_mult_mono'[OF assms(1), of b] 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    91
    have eq: "euclidean_size (a * b) = euclidean_size b" by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    92
  have "a * b dvd b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    93
    by (rule dvd_euclidean_size_eq_imp_dvd[OF _ _ eq]) (insert assms, simp_all)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    94
  hence "a * b dvd 1 * b" by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    95
  with \<open>b \<noteq> 0\<close> have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    96
  with assms(3) show False by contradiction
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    97
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    98
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
    99
lemma dvd_imp_size_le:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   100
  assumes "x dvd y" "y \<noteq> 0" 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   101
  shows   "euclidean_size x \<le> euclidean_size y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   102
  using assms by (auto elim!: dvdE simp: size_mult_mono)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   103
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   104
lemma dvd_proper_imp_size_less:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   105
  assumes "x dvd y" "\<not>y dvd x" "y \<noteq> 0" 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   106
  shows   "euclidean_size x < euclidean_size y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   107
proof -
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   108
  from assms(1) obtain z where "y = x * z" by (erule dvdE)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   109
  hence z: "y = z * x" by (simp add: mult.commute)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   110
  from z assms have "\<not>is_unit z" by (auto simp: mult.commute mult_unit_dvd_iff)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   111
  with z assms show ?thesis
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   112
    by (auto intro!: euclidean_size_times_nonunit simp: )
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   113
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   114
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   115
lemma irreducible_normalized_divisors:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   116
  assumes "irreducible x" "y dvd x" "normalize y = y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   117
  shows   "y = 1 \<or> y = normalize x"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   118
proof -
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   119
  from assms have "is_unit y \<or> x dvd y" by (auto simp: irreducible_altdef)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   120
  thus ?thesis
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   121
  proof (elim disjE)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   122
    assume "is_unit y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   123
    hence "normalize y = 1" by (simp add: is_unit_normalize)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   124
    with assms show ?thesis by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   125
  next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   126
    assume "x dvd y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   127
    with \<open>y dvd x\<close> have "normalize y = normalize x" by (rule associatedI)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   128
    with assms show ?thesis by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   129
  qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   130
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   131
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   132
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   133
where
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   134
  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   135
  by pat_completeness simp
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   136
termination
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   137
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   138
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   139
declare gcd_eucl.simps [simp del]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   140
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   141
lemma gcd_eucl_induct [case_names zero mod]:
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   142
  assumes H1: "\<And>b. P b 0"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   143
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   144
  shows "P a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   145
proof (induct a b rule: gcd_eucl.induct)
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   146
  case ("1" a b)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   147
  show ?case
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   148
  proof (cases "b = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   149
    case True then show "P a b" by simp (rule H1)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   150
  next
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   151
    case False
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   152
    then have "P b (a mod b)"
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   153
      by (rule "1.hyps")
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   154
    with \<open>b \<noteq> 0\<close> show "P a b"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   155
      by (blast intro: H2)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   156
  qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   157
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   158
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   159
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   160
where
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   161
  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   162
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 63040
diff changeset
   163
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" \<comment> \<open>
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   164
  Somewhat complicated definition of Lcm that has the advantage of working
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   165
  for infinite sets as well\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   166
where
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   167
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   168
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   169
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   170
       in normalize l 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   171
      else 0)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   172
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   173
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   174
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   175
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   176
62428
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   177
declare Lcm_eucl_def Gcd_eucl_def [code del]
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   178
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   179
lemma gcd_eucl_0:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   180
  "gcd_eucl a 0 = normalize a"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   181
  by (simp add: gcd_eucl.simps [of a 0])
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   182
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   183
lemma gcd_eucl_0_left:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   184
  "gcd_eucl 0 a = normalize a"
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   185
  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   186
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   187
lemma gcd_eucl_non_0:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   188
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   189
  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   190
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   191
lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   192
  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   193
  by (induct a b rule: gcd_eucl_induct)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   194
     (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   195
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   196
lemma normalize_gcd_eucl [simp]:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   197
  "normalize (gcd_eucl a b) = gcd_eucl a b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   198
  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   199
     
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   200
lemma gcd_eucl_greatest:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   201
  fixes k a b :: 'a
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   202
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   203
proof (induct a b rule: gcd_eucl_induct)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   204
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   205
next
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   206
  case (mod a b)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   207
  then show ?case
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   208
    by (simp add: gcd_eucl_non_0 dvd_mod_iff)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   209
qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   210
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   211
lemma gcd_euclI:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   212
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   213
  assumes "d dvd a" "d dvd b" "normalize d = d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   214
          "\<And>k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   215
  shows   "gcd_eucl a b = d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   216
  by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   217
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   218
lemma eq_gcd_euclI:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   219
  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   220
  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"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   221
          "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   222
  shows   "gcd = gcd_eucl"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   223
  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   224
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   225
lemma gcd_eucl_zero [simp]:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   226
  "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   227
  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   228
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   229
  
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   230
lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   231
  and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   232
  and unit_factor_Lcm_eucl [simp]: 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   233
          "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   234
proof -
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   235
  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>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   236
    unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   237
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   238
    case False
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   239
    hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   240
    with False show ?thesis by auto
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   241
  next
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   242
    case True
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   243
    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
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62457
diff changeset
   244
    define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62457
diff changeset
   245
    define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   246
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   247
      apply (subst n_def)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   248
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   249
      apply (rule exI[of _ l\<^sub>0])
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   250
      apply (simp add: l\<^sub>0_props)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   251
      done
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   252
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   253
      unfolding l_def by simp_all
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   254
    {
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   255
      fix l' assume "\<forall>a\<in>A. a dvd l'"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   256
      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)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   257
      moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   258
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   259
                          euclidean_size b = euclidean_size (gcd_eucl l l')"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   260
        by (intro exI[of _ "gcd_eucl l l'"], auto)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   261
      hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   262
      moreover have "euclidean_size (gcd_eucl l l') \<le> n"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   263
      proof -
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   264
        have "gcd_eucl l l' dvd l" by simp
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   265
        then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   266
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   267
        hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   268
          by (rule size_mult_mono)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   269
        also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   270
        also note \<open>euclidean_size l = n\<close>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   271
        finally show "euclidean_size (gcd_eucl l l') \<le> n" .
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   272
      qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   273
      ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')" 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   274
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   275
      from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   276
        by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   277
      hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   278
    }
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   279
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   280
    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>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   281
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   282
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   283
        unit_factor (normalize l) = 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   284
        (if normalize l = 0 then 0 else 1)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   285
      by (auto simp: unit_simps)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   286
    also from True have "normalize l = Lcm_eucl A"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   287
      by (simp add: Lcm_eucl_def Let_def n_def l_def)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   288
    finally show ?thesis .
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   289
  qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   290
  note A = this
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   291
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   292
  {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   293
  {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   294
  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   295
qed
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   296
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   297
lemma normalize_Lcm_eucl [simp]:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   298
  "normalize (Lcm_eucl A) = Lcm_eucl A"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   299
proof (cases "Lcm_eucl A = 0")
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   300
  case True then show ?thesis by simp
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   301
next
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   302
  case False
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   303
  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   304
    by (fact unit_factor_mult_normalize)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   305
  with False show ?thesis by simp
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   306
qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   307
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   308
lemma eq_Lcm_euclI:
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   309
  fixes lcm :: "'a set \<Rightarrow> 'a"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   310
  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"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   311
          "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   312
  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   313
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   314
lemma Gcd_eucl_dvd: "x \<in> A \<Longrightarrow> Gcd_eucl A dvd x"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   315
  unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   316
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   317
lemma Gcd_eucl_greatest: "(\<And>x. x \<in> A \<Longrightarrow> d dvd x) \<Longrightarrow> d dvd Gcd_eucl A"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   318
  unfolding Gcd_eucl_def by auto
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   319
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   320
lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   321
  by (simp add: Gcd_eucl_def)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   322
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   323
lemma Lcm_euclI:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   324
  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"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   325
  shows   "Lcm_eucl A = d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   326
proof -
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   327
  have "normalize (Lcm_eucl A) = normalize d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   328
    by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   329
  thus ?thesis by (simp add: assms)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   330
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   331
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   332
lemma Gcd_euclI:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   333
  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"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   334
  shows   "Gcd_eucl A = d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   335
proof -
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   336
  have "normalize (Gcd_eucl A) = normalize d"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   337
    by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   338
  thus ?thesis by (simp add: assms)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   339
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   340
  
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   341
lemmas lcm_gcd_eucl_facts = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   342
  gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   343
  Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   344
  dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   345
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   346
lemma normalized_factors_product:
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   347
  "{p. p dvd a * b \<and> normalize p = p} = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   348
     (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   349
proof safe
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   350
  fix p assume p: "p dvd a * b" "normalize p = p"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   351
  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   352
    by standard (rule lcm_gcd_eucl_facts; assumption)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   353
  from dvd_productE[OF p(1)] guess x y . note xy = this
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   354
  define x' y' where "x' = normalize x" and "y' = normalize y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   355
  have "p = x' * y'"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   356
    by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   357
  moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b" 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   358
    by (simp_all add: x'_def y'_def)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   359
  ultimately show "p \<in> (\<lambda>(x, y). x * y) ` 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   360
                     ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   361
    by blast
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   362
qed (auto simp: normalize_mult mult_dvd_mono)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   363
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   364
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   365
subclass factorial_semiring
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   366
proof (standard, rule factorial_semiring_altI_aux)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   367
  fix x assume "x \<noteq> 0"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   368
  thus "finite {p. p dvd x \<and> normalize p = p}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   369
  proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   370
    case (less x)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   371
    show ?case
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   372
    proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   373
      case False
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   374
      have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   375
      proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   376
        fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   377
        with False have "is_unit p \<or> x dvd p" by blast
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   378
        thus "p \<in> {1, normalize x}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   379
        proof (elim disjE)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   380
          assume "is_unit p"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   381
          hence "normalize p = 1" by (simp add: is_unit_normalize)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   382
          with p show ?thesis by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   383
        next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   384
          assume "x dvd p"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   385
          with p have "normalize p = normalize x" by (intro associatedI) simp_all
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   386
          with p show ?thesis by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   387
        qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   388
      qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   389
      moreover have "finite \<dots>" by simp
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   390
      ultimately show ?thesis by (rule finite_subset)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   391
      
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   392
    next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   393
      case True
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   394
      then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   395
      define z where "z = x div y"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   396
      let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   397
      from y have x: "x = y * z" by (simp add: z_def)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   398
      with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   399
      from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   400
      have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   401
        by (subst x) (rule normalized_factors_product)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   402
      also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   403
        by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   404
      hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   405
        by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   406
           (auto simp: x)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   407
      finally show ?thesis .
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   408
    qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   409
  qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   410
next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   411
  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   412
    by standard (rule lcm_gcd_eucl_facts; assumption)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   413
  fix p assume p: "irreducible p"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   414
  thus "is_prime_elem p" by (rule irreducible_imp_prime_gcd)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   415
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   416
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   417
lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   418
  by (intro ext gcd_euclI gcd_lcm_factorial)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   419
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   420
lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   421
  by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   422
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   423
lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   424
  by (intro ext Gcd_euclI gcd_lcm_factorial)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   425
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   426
lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   427
  by (intro ext Lcm_euclI gcd_lcm_factorial)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   428
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   429
lemmas eucl_eq_factorial = 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   430
  gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   431
  Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   432
  
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   433
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   434
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   435
class euclidean_ring = euclidean_semiring + idom
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   436
begin
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   437
62457
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   438
subclass ring_div ..
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   439
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   440
function euclid_ext_aux :: "'a \<Rightarrow> _" where
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   441
  "euclid_ext_aux r' r s' s t' t = (
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   442
     if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   443
     else let q = r' div r
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   444
          in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   445
by auto
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   446
termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   447
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   448
declare euclid_ext_aux.simps [simp del]
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   449
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   450
lemma euclid_ext_aux_correct:
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   451
  assumes "gcd_eucl r' r = gcd_eucl x y"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   452
  assumes "s' * x + t' * y = r'"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   453
  assumes "s * x + t * y = r"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   454
  shows   "case euclid_ext_aux r' r s' s t' t of (a,b,c) \<Rightarrow>
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   455
             a * x + b * y = c \<and> c = gcd_eucl x y" (is "?P (euclid_ext_aux r' r s' s t' t)")
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   456
using assms
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   457
proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   458
  case (1 r' r s' s t' t)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   459
  show ?case
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   460
  proof (cases "r = 0")
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   461
    case True
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   462
    hence "euclid_ext_aux r' r s' s t' t = 
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   463
             (s' div unit_factor r', t' div unit_factor r', normalize r')"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   464
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   465
    also have "?P \<dots>"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   466
    proof safe
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   467
      have "s' div unit_factor r' * x + t' div unit_factor r' * y = 
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   468
                (s' * x + t' * y) div unit_factor r'"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   469
        by (cases "r' = 0") (simp_all add: unit_div_commute)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   470
      also have "s' * x + t' * y = r'" by fact
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   471
      also have "\<dots> div unit_factor r' = normalize r'" by simp
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   472
      finally show "s' div unit_factor r' * x + t' div unit_factor r' * y = normalize r'" .
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   473
    next
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   474
      from "1.prems" True show "normalize r' = gcd_eucl x y" by (simp add: gcd_eucl_0)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   475
    qed
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   476
    finally show ?thesis .
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   477
  next
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   478
    case False
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   479
    hence "euclid_ext_aux r' r s' s t' t = 
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   480
             euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   481
      by (subst euclid_ext_aux.simps) (simp add: Let_def)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   482
    also from "1.prems" False have "?P \<dots>"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   483
    proof (intro "1.IH")
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   484
      have "(s' - r' div r * s) * x + (t' - r' div r * t) * y =
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   485
              (s' * x + t' * y) - r' div r * (s * x + t * y)" by (simp add: algebra_simps)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   486
      also have "s' * x + t' * y = r'" by fact
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   487
      also have "s * x + t * y = r" by fact
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   488
      also have "r' - r' div r * r = r' mod r" using mod_div_equality[of r' r]
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   489
        by (simp add: algebra_simps)
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   490
      finally show "(s' - r' div r * s) * x + (t' - r' div r * t) * y = r' mod r" .
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   491
    qed (auto simp: gcd_eucl_non_0 algebra_simps div_mod_equality')
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   492
    finally show ?thesis .
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   493
  qed
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   494
qed
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   495
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   496
definition euclid_ext where
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   497
  "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   498
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   499
lemma euclid_ext_0: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   500
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   501
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   502
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   503
lemma euclid_ext_left_0: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   504
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   505
  by (simp add: euclid_ext_def euclid_ext_aux.simps)
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   506
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   507
lemma euclid_ext_correct':
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   508
  "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd_eucl x y"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   509
  unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   510
62457
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   511
lemma euclid_ext_gcd_eucl:
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   512
  "(case euclid_ext x y of (a,b,c) \<Rightarrow> c) = gcd_eucl x y"
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   513
  using euclid_ext_correct'[of x y] by (simp add: case_prod_unfold)
a3c7bd201da7 Minor adjustments to euclidean rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62442
diff changeset
   514
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   515
definition euclid_ext' where
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   516
  "euclid_ext' x y = (case euclid_ext x y of (a, b, _) \<Rightarrow> (a, b))"
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   517
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   518
lemma euclid_ext'_correct':
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   519
  "case euclid_ext' x y of (a,b) \<Rightarrow> a * x + b * y = gcd_eucl x y"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   520
  using euclid_ext_correct'[of x y] by (simp add: case_prod_unfold euclid_ext'_def)
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   521
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   522
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   523
  by (simp add: euclid_ext'_def euclid_ext_0)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   524
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   525
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   526
  by (simp add: euclid_ext'_def euclid_ext_left_0)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   527
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   528
end
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   529
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   530
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   531
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   532
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   533
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   534
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   535
subclass semiring_gcd
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   536
  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   537
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   538
subclass semiring_Gcd
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   539
  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   540
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   541
subclass factorial_semiring_gcd
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   542
proof
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   543
  fix a b
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   544
  show "gcd a b = gcd_factorial a b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   545
    by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   546
  thus "lcm a b = lcm_factorial a b"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   547
    by (simp add: lcm_factorial_gcd_factorial lcm_gcd)
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   548
next
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   549
  fix A 
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   550
  show "Gcd A = Gcd_factorial A"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   551
    by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   552
  show "Lcm A = Lcm_factorial A"
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   553
    by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   554
qed
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   555
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   556
lemma gcd_non_0:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   557
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   558
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   559
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   560
lemmas gcd_0 = gcd_0_right
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   561
lemmas dvd_gcd_iff = gcd_greatest_iff
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   562
lemmas gcd_greatest_iff = dvd_gcd_iff
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   563
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   564
lemma gcd_mod1 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   565
  "gcd (a mod b) b = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   566
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   567
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   568
lemma gcd_mod2 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   569
  "gcd a (b mod a) = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   570
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   571
         
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   572
lemma euclidean_size_gcd_le1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   573
  assumes "a \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   574
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   575
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   576
   have "gcd a b dvd a" by (rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   577
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   578
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   579
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   580
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   581
lemma euclidean_size_gcd_le2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   582
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   583
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   584
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   585
lemma euclidean_size_gcd_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   586
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   587
  shows "euclidean_size (gcd a b) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   588
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   589
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   590
  with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   591
    by (intro le_antisym, simp_all)
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   592
  have "a dvd gcd a b"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   593
    by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   594
  hence "a dvd b" using dvd_gcdD2 by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   595
  with \<open>\<not>a dvd b\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   596
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   597
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   598
lemma euclidean_size_gcd_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   599
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   600
  shows "euclidean_size (gcd a b) < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   601
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   602
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   603
lemma euclidean_size_lcm_le1: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   604
  assumes "a \<noteq> 0" and "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   605
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   606
proof -
60690
a9e45c9588c3 tuned facts
haftmann
parents: 60688
diff changeset
   607
  have "a dvd lcm a b" by (rule dvd_lcm1)
a9e45c9588c3 tuned facts
haftmann
parents: 60688
diff changeset
   608
  then obtain c where A: "lcm a b = a * c" ..
62429
25271ff79171 Tuned Euclidean Rings/GCD rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62428
diff changeset
   609
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_eq_0_iff)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   610
  then show ?thesis by (subst A, intro size_mult_mono)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   611
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   612
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   613
lemma euclidean_size_lcm_le2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   614
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   615
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   616
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   617
lemma euclidean_size_lcm_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   618
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   619
  shows "euclidean_size a < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   620
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   621
  from assms have "a \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   622
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   623
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   624
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   625
  with assms have "lcm a b dvd a" 
62429
25271ff79171 Tuned Euclidean Rings/GCD rings
Manuel Eberl <eberlm@in.tum.de>
parents: 62428
diff changeset
   626
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   627
  hence "b dvd a" by (rule lcm_dvdD2)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   628
  with \<open>\<not>b dvd a\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   629
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   630
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   631
lemma euclidean_size_lcm_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   632
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   633
  shows "euclidean_size b < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   634
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   635
62428
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   636
lemma Lcm_eucl_set [code]:
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   637
  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   638
  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   639
62428
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   640
lemma Gcd_eucl_set [code]:
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   641
  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
4d5fbec92bb1 Fixed code equations for Gcd/Lcm
Manuel Eberl <eberlm@in.tum.de>
parents: 62425
diff changeset
   642
  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   643
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   644
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   645
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   646
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   647
text \<open>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   648
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   649
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   650
\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   651
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   652
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   653
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   654
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   655
subclass euclidean_ring ..
60439
b765e08f8bc0 proper subclass instances for existing gcd (semi)rings
haftmann
parents: 60438
diff changeset
   656
subclass ring_gcd ..
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   657
subclass factorial_ring_gcd ..
60439
b765e08f8bc0 proper subclass instances for existing gcd (semi)rings
haftmann
parents: 60438
diff changeset
   658
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   659
lemma euclid_ext_gcd [simp]:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   660
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   661
  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   662
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   663
lemma euclid_ext_gcd' [simp]:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   664
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   665
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   666
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   667
lemma euclid_ext_correct:
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   668
  "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd x y"
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   669
  using euclid_ext_correct'[of x y]
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   670
  by (simp add: gcd_gcd_eucl case_prod_unfold)
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   671
  
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   672
lemma euclid_ext'_correct:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   673
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
62442
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   674
  using euclid_ext_correct'[of a b]
26e4be6a680f More efficient Extended Euclidean Algorithm
Manuel Eberl <eberlm@in.tum.de>
parents: 62429
diff changeset
   675
  by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   676
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   677
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   678
  using euclid_ext'_correct by blast
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   679
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   680
end
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   681
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   682
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   683
subsection \<open>Typical instances\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   684
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   685
instantiation nat :: euclidean_semiring
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   686
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   687
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   688
definition [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   689
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   690
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   691
instance by standard simp_all
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   692
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   693
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   694
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   695
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   696
instantiation int :: euclidean_ring
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   697
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   698
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   699
definition [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   700
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   701
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   702
instance by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   703
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   704
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   705
62422
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   706
instance nat :: euclidean_semiring_gcd
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   707
proof
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   708
  show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   709
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   710
  show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   711
    by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   712
qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   713
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   714
instance int :: euclidean_ring_gcd
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   715
proof
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   716
  show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   717
    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   718
  show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   719
    by (intro ext, simp add: lcm_eucl_def lcm_altdef_int 
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   720
          semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   721
qed
4aa35fd6c152 Tuned Euclidean rings
eberlm
parents: 62353
diff changeset
   722
63498
a3fe3250d05d Reformed factorial rings
eberlm <eberlm@in.tum.de>
parents: 63167
diff changeset
   723
end