src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Wed, 08 Jul 2015 14:01:41 +0200
changeset 60688 01488b559910
parent 60687 33dbbcb6a8a3
child 60690 a9e45c9588c3
permissions -rw-r--r--
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
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
60685
cb21b7022b00 moved normalization and unit_factor into Main HOL corpus
haftmann
parents: 60634
diff changeset
     6
imports Main "~~/src/HOL/GCD" "~~/src/HOL/Library/Polynomial"
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"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    22
  assumes mod_size_less: 
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    23
    "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
    24
  assumes size_mult_mono:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
    25
    "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
    26
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    27
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    28
lemma euclidean_division:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    29
  fixes a :: 'a and b :: 'a
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    30
  assumes "b \<noteq> 0"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    31
  obtains s and t where "a = s * b + t" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    32
    and "euclidean_size t < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    33
proof -
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    34
  from div_mod_equality [of a b 0] 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    35
     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
    36
  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
    37
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    38
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    39
lemma dvd_euclidean_size_eq_imp_dvd:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    40
  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
    41
  shows "a dvd b"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    42
proof (rule ccontr)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    43
  assume "\<not> a dvd b"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    44
  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
    45
  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
    46
  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
    47
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
    48
  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
    49
      using size_mult_mono by force
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    50
  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
    51
  have "euclidean_size (b mod a) < euclidean_size a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    52
      using mod_size_less by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    53
  ultimately show False using size_eq by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    54
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    55
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    56
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    57
where
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
    58
  "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
    59
  by pat_completeness simp
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    60
termination
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    61
  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
    62
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    63
declare gcd_eucl.simps [simp del]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    64
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    65
lemma gcd_eucl_induct [case_names zero mod]:
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    66
  assumes H1: "\<And>b. P b 0"
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    67
  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
    68
  shows "P a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    69
proof (induct a b rule: gcd_eucl.induct)
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    70
  case ("1" a b)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    71
  show ?case
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    72
  proof (cases "b = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    73
    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
    74
  next
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    75
    case False
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    76
    then have "P b (a mod b)"
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
    77
      by (rule "1.hyps")
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    78
    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
    79
      by (blast intro: H2)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
    80
  qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    81
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    82
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    83
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    84
where
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
    85
  "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
    86
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
    87
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
    88
  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
    89
  for infinite sets as well\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    90
where
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
    91
  "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
    92
     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
    93
       (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
    94
       in normalize l 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    95
      else 0)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    96
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    97
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    98
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    99
  "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
   100
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   101
lemma gcd_eucl_0:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   102
  "gcd_eucl a 0 = normalize a"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   103
  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
   104
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   105
lemma gcd_eucl_0_left:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   106
  "gcd_eucl 0 a = normalize a"
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   107
  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
   108
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   109
lemma gcd_eucl_non_0:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   110
  "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
   111
  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
   112
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   113
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   114
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   115
class euclidean_ring = euclidean_semiring + idom
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   116
begin
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   117
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   118
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   119
  "euclid_ext a b = 
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   120
     (if b = 0 then 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   121
        (1 div unit_factor a, 0, normalize a)
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   122
      else
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   123
        case euclid_ext b (a mod b) of
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   124
            (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   125
  by pat_completeness simp
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   126
termination
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   127
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   128
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   129
declare euclid_ext.simps [simp del]
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   130
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   131
lemma euclid_ext_0: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   132
  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   133
  by (simp add: euclid_ext.simps [of a 0])
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   134
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   135
lemma euclid_ext_left_0: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   136
  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   137
  by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   138
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   139
lemma euclid_ext_non_0: 
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   140
  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   141
    (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
60600
87fbfea0bd0a simplified termination criterion for euclidean algorithm (again)
haftmann
parents: 60599
diff changeset
   142
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   143
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   144
lemma euclid_ext_code [code]:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   145
  "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)
60598
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   146
    else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   147
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   148
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   149
lemma euclid_ext_correct:
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   150
  "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   151
proof (induct a b rule: gcd_eucl_induct)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   152
  case (zero a) then show ?case
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   153
    by (simp add: euclid_ext_0 ac_simps)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   154
next
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   155
  case (mod a b)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   156
  obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   157
    by (cases "euclid_ext b (a mod b)") blast
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   158
  with mod have "c = s * b + t * (a mod b)" by simp
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   159
  also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   160
    by (simp add: algebra_simps) 
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   161
  also have "(a div b) * b + a mod b = a" using mod_div_equality .
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   162
  finally show ?case
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   163
    by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   164
qed
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   165
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   166
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   167
where
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   168
  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   169
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   170
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
   171
  by (simp add: euclid_ext'_def euclid_ext_0)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   172
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   173
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
   174
  by (simp add: euclid_ext'_def euclid_ext_left_0)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   175
  
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   176
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   177
  fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   178
  by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   179
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   180
end
78ca5674c66a rings follow immediately their corresponding semirings
haftmann
parents: 60582
diff changeset
   181
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   182
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   183
  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
   184
  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
   185
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   186
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   187
lemma gcd_0_left:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   188
  "gcd 0 a = normalize a"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   189
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   190
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   191
lemma gcd_0:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   192
  "gcd a 0 = normalize a"
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   193
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   194
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   195
lemma gcd_non_0:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   196
  "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
   197
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   198
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   199
lemma gcd_dvd1 [iff]: "gcd a b dvd a"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   200
  and gcd_dvd2 [iff]: "gcd a b dvd b"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   201
  by (induct a b rule: gcd_eucl_induct)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   202
    (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   203
    
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   204
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   205
  by (rule dvd_trans, assumption, rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   206
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   207
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   208
  by (rule dvd_trans, assumption, rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   209
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   210
lemma gcd_greatest:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   211
  fixes k a b :: 'a
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   212
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   213
proof (induct a b rule: gcd_eucl_induct)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   214
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   215
next
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   216
  case (mod a b)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   217
  then show ?case
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   218
    by (simp add: gcd_non_0 dvd_mod_iff)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   219
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   220
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   221
lemma dvd_gcd_iff:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   222
  "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   223
  by (blast intro!: gcd_greatest intro: dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   224
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   225
lemmas gcd_greatest_iff = dvd_gcd_iff
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   226
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   227
lemma gcd_zero [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   228
  "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   229
  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   230
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   231
lemma normalize_gcd [simp]:
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   232
  "normalize (gcd a b) = gcd a b"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   233
  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_0 gcd_non_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   234
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   235
lemma gcdI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   236
  assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   237
    and "normalize c = c"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   238
  shows "c = gcd a b"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   239
  by (rule associated_eqI) (auto simp: assms intro: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   240
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   241
sublocale gcd!: abel_semigroup gcd
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   242
proof
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   243
  fix a b c 
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   244
  show "gcd (gcd a b) c = gcd a (gcd b c)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   245
  proof (rule gcdI)
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   246
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   247
    then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   248
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   249
    hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   250
    moreover have "gcd (gcd a b) c dvd c" by simp
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   251
    ultimately show "gcd (gcd a b) c dvd gcd b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   252
      by (rule gcd_greatest)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   253
    show "normalize (gcd (gcd a b) c) = gcd (gcd a b) c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   254
      by auto
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   255
    fix l assume "l dvd a" and "l dvd gcd b c"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   256
    with dvd_trans [OF _ gcd_dvd1] and dvd_trans [OF _ gcd_dvd2]
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   257
      have "l dvd b" and "l dvd c" by blast+
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   258
    with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   259
      by (intro gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   260
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   261
next
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   262
  fix a b
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   263
  show "gcd a b = gcd b a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   264
    by (rule gcdI) (simp_all add: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   265
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   266
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   267
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   268
    normalize d = d \<and>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   269
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   270
  by rule (auto intro: gcdI simp: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   271
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   272
lemma gcd_dvd_prod: "gcd a b dvd k * b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   273
  using mult_dvd_mono [of 1] by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   274
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   275
lemma gcd_1_left [simp]: "gcd 1 a = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   276
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   277
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   278
lemma gcd_1 [simp]: "gcd a 1 = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   279
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   280
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   281
lemma gcd_proj2_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   282
  "b dvd a \<Longrightarrow> gcd a b = normalize b"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   283
  by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   284
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   285
lemma gcd_proj1_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   286
  "a dvd b \<Longrightarrow> gcd a b = normalize a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   287
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   288
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   289
lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   290
proof
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   291
  assume A: "gcd m n = normalize m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   292
  show "m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   293
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   294
    assume [simp]: "m \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   295
    from A have B: "m = gcd m n * unit_factor m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   296
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   297
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   298
  qed (insert A, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   299
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   300
  assume "m dvd n"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   301
  then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   302
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   303
  
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   304
lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   305
  using gcd_proj1_iff [of n m] by (simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   306
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   307
lemma gcd_mod1 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   308
  "gcd (a mod b) b = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   309
  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
   310
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   311
lemma gcd_mod2 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   312
  "gcd a (b mod a) = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   313
  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
   314
         
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   315
lemma gcd_mult_distrib': 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   316
  "normalize c * gcd a b = gcd (c * a) (c * b)"
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   317
proof (cases "c = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   318
  case True then show ?thesis by (simp_all add: gcd_0)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   319
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   320
  case False then have [simp]: "is_unit (unit_factor c)" by simp
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   321
  show ?thesis
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   322
  proof (induct a b rule: gcd_eucl_induct)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   323
    case (zero a) show ?case
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   324
    proof (cases "a = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   325
      case True then show ?thesis by (simp add: gcd_0)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   326
    next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   327
      case False
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   328
      then show ?thesis by (simp add: gcd_0 normalize_mult)
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   329
    qed
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   330
    case (mod a b)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   331
    then show ?case by (simp add: mult_mod_right gcd.commute)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   332
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   333
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   334
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   335
lemma gcd_mult_distrib:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   336
  "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   337
proof-
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   338
  have "normalize k * gcd a b = gcd (k * a) (k * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   339
    by (simp add: gcd_mult_distrib')
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   340
  then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   341
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   342
  then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   343
    by (simp only: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   344
  then show ?thesis
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   345
    by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   346
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   347
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   348
lemma euclidean_size_gcd_le1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   349
  assumes "a \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   350
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   351
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   352
   have "gcd a b dvd a" by (rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   353
   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
   354
   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
   355
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   356
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   357
lemma euclidean_size_gcd_le2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   358
  "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
   359
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   360
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   361
lemma euclidean_size_gcd_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   362
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   363
  shows "euclidean_size (gcd a b) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   364
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   365
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   366
  with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   367
    by (intro le_antisym, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   368
  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   369
  hence "a dvd b" using dvd_gcd_D2 by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   370
  with \<open>\<not>a dvd b\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   371
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   372
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   373
lemma euclidean_size_gcd_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   374
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   375
  shows "euclidean_size (gcd a b) < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   376
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   377
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   378
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   379
  apply (rule gcdI)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   380
  apply simp_all
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   381
  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   382
  apply (rule gcd_greatest, simp add: unit_simps, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   383
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   384
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   385
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   386
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   387
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   388
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   389
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   390
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   391
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   392
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   393
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   394
lemma normalize_gcd_left [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   395
  "gcd (normalize a) b = gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   396
proof (cases "a = 0")
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   397
  case True then show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   398
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   399
next
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   400
  case False then have "is_unit (unit_factor a)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   401
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   402
  moreover have "normalize a = a div unit_factor a"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   403
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   404
  ultimately show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   405
    by (simp only: gcd_div_unit1)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   406
qed
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   407
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   408
lemma normalize_gcd_right [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   409
  "gcd a (normalize b) = gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   410
  using normalize_gcd_left [of b a] by (simp add: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   411
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   412
lemma gcd_idem: "gcd a a = normalize a"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   413
  by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   414
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   415
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   416
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   417
  apply (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   418
  apply (rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   419
  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   420
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   421
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   422
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   423
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   424
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   425
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   426
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   427
  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   428
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   429
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   430
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   431
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   432
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   433
  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   434
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   435
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   436
  fix a show "gcd a \<circ> gcd a = gcd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   437
    by (simp add: fun_eq_iff gcd_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   438
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   439
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   440
lemma coprime_dvd_mult:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   441
  assumes "gcd c b = 1" and "c dvd a * b"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   442
  shows "c dvd a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   443
proof -
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   444
  let ?nf = "unit_factor"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   445
  from assms gcd_mult_distrib [of a c b] 
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   446
    have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   447
  from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   448
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   449
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   450
lemma coprime_dvd_mult_iff:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   451
  "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   452
  by (rule, rule coprime_dvd_mult, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   453
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   454
lemma gcd_dvd_antisym:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   455
  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   456
proof (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   457
  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   458
  have "gcd c d dvd c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   459
  with A show "gcd a b dvd c" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   460
  have "gcd c d dvd d" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   461
  with A show "gcd a b dvd d" by (rule dvd_trans)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   462
  show "normalize (gcd a b) = gcd a b"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   463
    by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   464
  fix l assume "l dvd c" and "l dvd d"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   465
  hence "l dvd gcd c d" by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   466
  from this and B show "l dvd gcd a b" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   467
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   468
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   469
lemma gcd_mult_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   470
  assumes "gcd k n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   471
  shows "gcd (k * m) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   472
proof (rule gcd_dvd_antisym)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   473
  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   474
  also note \<open>gcd k n = 1\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   475
  finally have "gcd (gcd (k * m) n) k = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   476
  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   477
  moreover have "gcd (k * m) n dvd n" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   478
  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   479
  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   480
  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   481
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   482
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   483
lemma coprime_crossproduct:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   484
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   485
  shows "normalize (a * c) = normalize (b * d) \<longleftrightarrow> normalize a  = normalize b \<and> normalize c = normalize d"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   486
    (is "?lhs \<longleftrightarrow> ?rhs")
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   487
proof
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   488
  assume ?rhs
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   489
  then have "a dvd b" "b dvd a" "c dvd d" "d dvd c" by (simp_all add: associated_iff_dvd)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   490
  then have "a * c dvd b * d" "b * d dvd a * c" by (fast intro: mult_dvd_mono)+
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   491
  then show ?lhs by (simp add: associated_iff_dvd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   492
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   493
  assume ?lhs
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   494
  then have dvd: "a * c dvd b * d" "b * d dvd a * c" by (simp_all add: associated_iff_dvd)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   495
  then have "a dvd b * d" by (metis dvd_mult_left) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   496
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   497
  moreover from dvd have "b dvd a * c" by (metis dvd_mult_left) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   498
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   499
  moreover from dvd have "c dvd d * b" 
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   500
    by (auto dest: dvd_mult_right simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   501
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   502
  moreover from dvd have "d dvd c * a"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   503
    by (auto dest: dvd_mult_right simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   504
  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   505
  ultimately show ?rhs by (simp add: associated_iff_dvd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   506
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   507
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   508
lemma gcd_add1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   509
  "gcd (m + n) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   510
  by (cases "n = 0", simp_all add: gcd_non_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   511
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   512
lemma gcd_add2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   513
  "gcd m (m + n) = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   514
  using gcd_add1 [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   515
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   516
lemma gcd_add_mult:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   517
  "gcd m (k * m + n) = gcd m n"
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   518
proof -
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   519
  have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   520
    by (fact gcd_mod2)
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   521
  then show ?thesis by simp 
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   522
qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   523
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   524
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   525
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   526
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   527
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   528
  by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   529
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   530
lemma div_gcd_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   531
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   532
  defines [simp]: "d \<equiv> gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   533
  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   534
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   535
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   536
  fix l assume "l dvd a'" "l dvd b'"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   537
  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   538
  moreover have "a = a' * d" "b = b' * d" by simp_all
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   539
  ultimately have "a = (l * d) * s" "b = (l * d) * t"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   540
    by (simp_all only: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   541
  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   542
  hence "l*d dvd d" by (simp add: gcd_greatest)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   543
  then obtain u where "d = l * d * u" ..
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   544
  then have "d * (l * u) = d" by (simp add: ac_simps)
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   545
  moreover from nz have "d \<noteq> 0" by simp
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   546
  with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   547
  ultimately have "1 = l * u"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   548
    using \<open>d \<noteq> 0\<close> by simp
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   549
  then show "l dvd 1" ..
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   550
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   551
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   552
lemma coprime_mult: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   553
  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   554
  shows "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   555
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   556
  using da apply (subst gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   557
  apply (subst gcd.commute, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   558
  apply (subst gcd.commute, rule db)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   559
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   560
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   561
lemma coprime_lmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   562
  assumes dab: "gcd d (a * b) = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   563
  shows "gcd d a = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   564
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   565
  fix l assume "l dvd d" and "l dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   566
  hence "l dvd a * b" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   567
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   568
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   569
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   570
lemma coprime_rmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   571
  assumes dab: "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   572
  shows "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   573
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   574
  fix l assume "l dvd d" and "l dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   575
  hence "l dvd a * b" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   576
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   577
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   578
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   579
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   580
  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   581
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   582
lemma gcd_coprime:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   583
  assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   584
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   585
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   586
  from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   587
  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   588
  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   589
  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   590
  finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   591
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   592
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   593
lemma coprime_power:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   594
  assumes "0 < n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   595
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   596
using assms proof (induct n)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   597
  case (Suc n) then show ?case
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   598
    by (cases n) (simp_all add: coprime_mul_eq)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   599
qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   600
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   601
lemma gcd_coprime_exists:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   602
  assumes nz: "gcd a b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   603
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   604
  apply (rule_tac x = "a div gcd a b" in exI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   605
  apply (rule_tac x = "b div gcd a b" in exI)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   606
  apply (insert nz, auto intro: div_gcd_coprime)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   607
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   608
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   609
lemma coprime_exp:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   610
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   611
  by (induct n, simp_all add: coprime_mult)
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 coprime_exp2 [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   614
  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   615
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   616
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   617
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   618
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   619
  apply assumption
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   620
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   621
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   622
lemma lcm_gcd:
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   623
  "lcm a b = normalize (a * b) div gcd a b"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   624
  by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   625
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   626
subclass semiring_gcd
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   627
  apply standard
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   628
  using gcd_right_idem
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   629
  apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   630
  done
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   631
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   632
lemma gcd_exp:
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   633
  "gcd (a ^ n) (b ^ n) = gcd a b ^ n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   634
proof (cases "a = 0 \<and> b = 0")
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   635
  case True
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   636
  then show ?thesis by (cases n) simp_all
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   637
next
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   638
  case False
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   639
  then have "1 = gcd ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   640
    using div_gcd_coprime by (subst sym) (auto simp: div_gcd_coprime)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   641
  then have "gcd a b ^ n = gcd a b ^ n * ..." by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   642
  also note gcd_mult_distrib
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   643
  also have "unit_factor (gcd a b ^ n) = 1"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   644
    using False by (auto simp add: unit_factor_power unit_factor_gcd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   645
  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   646
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   647
  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   648
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   649
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   650
qed
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
lemma coprime_common_divisor: 
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   653
  "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   654
  apply (subgoal_tac "a dvd gcd a b")
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   655
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   656
  apply (erule (1) gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   657
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   658
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   659
lemma division_decomp: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   660
  assumes dc: "a dvd b * c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   661
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   662
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   663
  assume "gcd a b = 0"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   664
  hence "a = 0 \<and> b = 0" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   665
  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   666
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   667
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   668
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   669
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   670
  from gcd_coprime_exists[OF this]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   671
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   672
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   673
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   674
  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   675
  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   676
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   677
  with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   678
  with coprime_dvd_mult[OF ab'(3)] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   679
    have "a' dvd c" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   680
  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   681
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   682
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   683
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   684
lemma pow_divs_pow:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   685
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   686
  shows "a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   687
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   688
  assume "gcd a b = 0"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   689
  then show ?thesis by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   690
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   691
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   692
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   693
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   694
  from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   695
  from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   696
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   697
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   698
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   699
    by (simp add: ab'(1,2)[symmetric])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   700
  hence "?d^n * a'^n dvd ?d^n * b'^n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   701
    by (simp only: power_mult_distrib ac_simps)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   702
  with zn have "a'^n dvd b'^n" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   703
  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   704
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   705
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   706
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   707
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   708
  with ab'(1,2) show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   709
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   710
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   711
lemma pow_divs_eq [simp]:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   712
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   713
  by (auto intro: pow_divs_pow dvd_power_same)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   714
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   715
lemma divs_mult:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   716
  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   717
  shows "m * n dvd r"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   718
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   719
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   720
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   721
  from mr n' have "m dvd n'*n" by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   722
  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   723
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   724
  with n' have "r = m * n * k" by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   725
  then show ?thesis unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   726
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   727
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   728
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   729
  by (subst add_commute, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   730
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   731
lemma setprod_coprime [rule_format]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   732
  "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   733
  apply (cases "finite A")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   734
  apply (induct set: finite)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   735
  apply (auto simp add: gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   736
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   737
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   738
lemma coprime_divisors: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   739
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   740
  shows "gcd d e = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   741
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   742
  from assms obtain k l where "a = d * k" "b = e * l"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   743
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   744
  with assms have "gcd (d * k) (e * l) = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   745
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   746
  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   747
  finally have "gcd e d = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   748
  then show ?thesis by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   749
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   750
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   751
lemma invertible_coprime:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   752
  assumes "a * b mod m = 1"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   753
  shows "coprime a m"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   754
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   755
  from assms have "coprime m (a * b mod m)"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   756
    by simp
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   757
  then have "coprime m (a * b)"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   758
    by simp
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   759
  then have "coprime m a"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   760
    by (rule coprime_lmult)
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   761
  then show ?thesis
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   762
    by (simp add: ac_simps)
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   763
qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   764
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   765
lemma lcm_gcd_prod:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   766
  "lcm a b * gcd a b = normalize (a * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   767
  by (simp add: lcm_gcd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   768
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   769
lemma lcm_zero:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   770
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   771
  by (fact lcm_eq_0_iff)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   772
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   773
lemmas lcm_0_iff = lcm_zero
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   774
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   775
lemma gcd_lcm: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   776
  assumes "lcm a b \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   777
  shows "gcd a b = normalize (a * b) div lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   778
proof -
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   779
  have "lcm a b * gcd a b = normalize (a * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   780
    by (fact lcm_gcd_prod)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   781
  with assms show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   782
    by (metis nonzero_mult_divide_cancel_left)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   783
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   784
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   785
declare unit_factor_lcm [simp]
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   786
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   787
lemma lcmI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   788
  assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   789
    and "normalize c = c"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   790
  shows "c = lcm a b"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   791
  by (rule associated_eqI) (auto simp: assms intro: lcm_least)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   792
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   793
sublocale lcm!: abel_semigroup lcm ..
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   794
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   795
lemma dvd_lcm_D1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   796
  "lcm m n dvd k \<Longrightarrow> m dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   797
  by (rule dvd_trans, rule lcm_dvd1, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   798
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   799
lemma dvd_lcm_D2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   800
  "lcm m n dvd k \<Longrightarrow> n dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   801
  by (rule dvd_trans, rule lcm_dvd2, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   802
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   803
lemma gcd_dvd_lcm [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   804
  "gcd a b dvd lcm a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   805
  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   806
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   807
lemma lcm_1_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   808
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   809
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   810
  assume "lcm a b = 1"
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   811
  then show "is_unit a \<and> is_unit b" by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   812
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   813
  assume "is_unit a \<and> is_unit b"
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   814
  hence "a dvd 1" and "b dvd 1" by simp_all
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   815
  hence "is_unit (lcm a b)" by (rule lcm_least)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   816
  hence "lcm a b = unit_factor (lcm a b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   817
    by (blast intro: sym is_unit_unit_factor)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   818
  also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   819
    by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   820
  finally show "lcm a b = 1" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   821
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   822
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   823
lemma lcm_0:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   824
  "lcm a 0 = 0"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   825
  by (fact lcm_0_right)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   826
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   827
lemma lcm_unique:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   828
  "a dvd d \<and> b dvd d \<and> 
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   829
  normalize d = d \<and>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   830
  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   831
  by rule (auto intro: lcmI simp: lcm_least lcm_zero)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   832
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   833
lemma dvd_lcm_I1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   834
  "k dvd m \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   835
  by (metis lcm_dvd1 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   836
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   837
lemma dvd_lcm_I2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   838
  "k dvd n \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   839
  by (metis lcm_dvd2 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   840
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   841
lemma lcm_coprime:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   842
  "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   843
  by (subst lcm_gcd) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   844
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   845
lemma lcm_proj1_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   846
  "b dvd a \<Longrightarrow> lcm a b = normalize a"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   847
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   848
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   849
lemma lcm_proj2_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   850
  "a dvd b \<Longrightarrow> lcm a b = normalize b"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   851
  using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   852
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   853
lemma lcm_proj1_iff:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   854
  "lcm m n = normalize m \<longleftrightarrow> n dvd m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   855
proof
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   856
  assume A: "lcm m n = normalize m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   857
  show "n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   858
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   859
    assume [simp]: "m \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   860
    from A have B: "m = lcm m n * unit_factor m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   861
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   862
    show ?thesis by (subst B, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   863
  qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   864
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   865
  assume "n dvd m"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   866
  then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   867
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   868
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   869
lemma lcm_proj2_iff:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   870
  "lcm m n = normalize n \<longleftrightarrow> m dvd n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   871
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   872
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   873
lemma euclidean_size_lcm_le1: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   874
  assumes "a \<noteq> 0" and "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   875
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   876
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   877
  have "a dvd lcm a b" by (rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   878
  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   879
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   880
  then show ?thesis by (subst A, intro size_mult_mono)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   881
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   882
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   883
lemma euclidean_size_lcm_le2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   884
  "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
   885
  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
   886
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   887
lemma euclidean_size_lcm_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   888
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   889
  shows "euclidean_size a < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   890
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   891
  from assms have "a \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   892
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   893
  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
   894
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   895
  with assms have "lcm a b dvd a" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   896
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   897
  hence "b dvd a" by (rule dvd_lcm_D2)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   898
  with \<open>\<not>b dvd a\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   899
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   900
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   901
lemma euclidean_size_lcm_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   902
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   903
  shows "euclidean_size b < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   904
  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
   905
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   906
lemma lcm_mult_unit1:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   907
  "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   908
  apply (rule lcmI)
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   909
  apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   910
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   911
  apply (rule lcm_least, simp add: unit_simps, assumption)
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
   912
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   913
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   914
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   915
lemma lcm_mult_unit2:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   916
  "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   917
  using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   918
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   919
lemma lcm_div_unit1:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   920
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   921
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   922
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   923
lemma lcm_div_unit2:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   924
  "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   925
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   926
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   927
lemma normalize_lcm_left [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   928
  "lcm (normalize a) b = lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   929
proof (cases "a = 0")
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   930
  case True then show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   931
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   932
next
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   933
  case False then have "is_unit (unit_factor a)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   934
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   935
  moreover have "normalize a = a div unit_factor a"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   936
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   937
  ultimately show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   938
    by (simp only: lcm_div_unit1)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   939
qed
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   940
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   941
lemma normalize_lcm_right [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   942
  "lcm a (normalize b) = lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   943
  using normalize_lcm_left [of b a] by (simp add: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   944
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   945
lemma lcm_left_idem:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   946
  "lcm a (lcm a b) = lcm a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   947
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   948
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   949
  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   950
  apply (rule lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   951
  apply (erule (1) lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   952
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   953
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   954
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   955
lemma lcm_right_idem:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   956
  "lcm (lcm a b) b = lcm a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   957
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   958
  apply (subst lcm.assoc, rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   959
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   960
  apply (rule lcm_least, erule (1) lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   961
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   962
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   963
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   964
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   965
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   966
  fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   967
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   968
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   969
  fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   970
    by (intro ext, simp add: lcm_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   971
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   972
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   973
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   974
  and Lcm_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   975
  and unit_factor_Lcm [simp]: 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   976
          "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   977
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   978
  have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm A dvd l') \<and>
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   979
    unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   980
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   981
    case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   982
    hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   983
    with False show ?thesis by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   984
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   985
    case True
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   986
    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
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   987
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   988
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   989
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   990
      apply (subst n_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   991
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   992
      apply (rule exI[of _ l\<^sub>0])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   993
      apply (simp add: l\<^sub>0_props)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   994
      done
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   995
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   996
      unfolding l_def by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   997
    {
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   998
      fix l' assume "\<forall>a\<in>A. a dvd l'"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   999
      with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest)
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1000
      moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1001
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1002
        by (intro exI[of _ "gcd l l'"], auto)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1003
      hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1004
      moreover have "euclidean_size (gcd l l') \<le> n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1005
      proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1006
        have "gcd l l' dvd l" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1007
        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1008
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1009
        hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1010
          by (rule size_mult_mono)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1011
        also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1012
        also note \<open>euclidean_size l = n\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1013
        finally show "euclidean_size (gcd l l') \<le> n" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1014
      qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1015
      ultimately have "euclidean_size l = euclidean_size (gcd l l')" 
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1016
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1017
      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1018
        using dvd_euclidean_size_eq_imp_dvd by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1019
      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1020
    }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1021
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1022
    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>
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1023
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1024
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1025
        unit_factor (normalize l) = 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1026
        (if normalize l = 0 then 0 else 1)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1027
      by (auto simp: unit_simps)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1028
    also from True have "normalize l = Lcm A"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1029
      by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1030
    finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1031
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1032
  note A = this
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1033
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1034
  {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1035
  {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm A dvd b" using A by blast}
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1036
  from A show "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1037
qed
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1038
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1039
lemma normalize_Lcm [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1040
  "normalize (Lcm A) = Lcm A"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1041
proof (cases "Lcm A = 0")
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1042
  case True then show ?thesis by simp
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1043
next
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1044
  case False
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1045
  have "unit_factor (Lcm A) * normalize (Lcm A) = Lcm A"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1046
    by (fact unit_factor_mult_normalize)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1047
  with False show ?thesis by simp
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1048
qed
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1049
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1050
lemma LcmI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1051
  assumes "\<And>a. a \<in> A \<Longrightarrow> a dvd b" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> b dvd c"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1052
    and "normalize b = b" shows "b = Lcm A"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1053
  by (rule associated_eqI) (auto simp: assms intro: Lcm_least)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1054
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1055
lemma Lcm_subset:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1056
  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1057
  by (blast intro: Lcm_least dvd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1058
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1059
lemma Lcm_Un:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1060
  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1061
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1062
  apply (blast intro: Lcm_subset)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1063
  apply (blast intro: Lcm_subset)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1064
  apply (intro Lcm_least ballI, elim UnE)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1065
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1066
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1067
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1068
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1069
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1070
lemma Lcm_1_iff:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1071
  "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1072
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1073
  assume "Lcm A = 1"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1074
  then show "\<forall>a\<in>A. is_unit a" by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1075
qed (rule LcmI [symmetric], auto)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1076
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1077
lemma Lcm_no_units:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1078
  "Lcm A = Lcm (A - {a. is_unit a})"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1079
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1080
  have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1081
  hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1082
    by (simp add: Lcm_Un [symmetric])
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1083
  also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1084
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1085
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1086
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1087
lemma Lcm_empty [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1088
  "Lcm {} = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1089
  by (simp add: Lcm_1_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1090
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1091
lemma Lcm_eq_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1092
  "0 \<in> A \<Longrightarrow> Lcm A = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1093
  by (drule dvd_Lcm) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1094
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1095
lemma Lcm0_iff':
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1096
  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1097
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1098
  assume "Lcm A = 0"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1099
  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1100
  proof
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1101
    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1102
    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
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1103
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1104
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1105
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1106
      apply (subst n_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1107
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1108
      apply (rule exI[of _ l\<^sub>0])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1109
      apply (simp add: l\<^sub>0_props)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1110
      done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1111
    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1112
    hence "normalize l \<noteq> 0" by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1113
    also from ex have "normalize l = Lcm A"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1114
       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1115
    finally show False using \<open>Lcm A = 0\<close> by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1116
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1117
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1118
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1119
lemma Lcm0_iff [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1120
  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1121
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1122
  assume "finite A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1123
  have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1124
  moreover {
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1125
    assume "0 \<notin> A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1126
    hence "\<Prod>A \<noteq> 0" 
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1127
      apply (induct rule: finite_induct[OF \<open>finite A\<close>]) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1128
      apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1129
      apply (subst setprod.insert, assumption, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1130
      apply (rule no_zero_divisors)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1131
      apply blast+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1132
      done
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1133
    moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1134
    ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1135
    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1136
  }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1137
  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1138
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1139
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1140
lemma Lcm_no_multiple:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1141
  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1142
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1143
  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1144
  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1145
  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1146
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1147
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1148
lemma Lcm_insert [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1149
  "Lcm (insert a A) = lcm a (Lcm A)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1150
proof (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1151
  fix l assume "a dvd l" and "Lcm A dvd l"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1152
  then have "\<forall>a\<in>A. a dvd l" by (auto intro: dvd_trans [of _ "Lcm A" l])
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1153
  with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_least)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1154
qed (auto intro: Lcm_least dvd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1155
 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1156
lemma Lcm_finite:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1157
  assumes "finite A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1158
  shows "Lcm A = Finite_Set.fold lcm 1 A"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1159
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1160
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1161
60431
db9c67b760f1 dropped warnings by dropping ineffective code declarations
haftmann
parents: 60430
diff changeset
  1162
lemma Lcm_set [code_unfold]:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1163
  "Lcm (set xs) = fold lcm xs 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1164
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1165
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1166
lemma Lcm_singleton [simp]:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1167
  "Lcm {a} = normalize a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1168
  by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1169
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1170
lemma Lcm_2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1171
  "Lcm {a,b} = lcm a b"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1172
  by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1173
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1174
lemma Lcm_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1175
  assumes "finite A" and "A \<noteq> {}" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1176
  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1177
  shows "Lcm A = normalize (\<Prod>A)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1178
using assms proof (induct rule: finite_ne_induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1179
  case (insert a A)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1180
  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1181
  also from insert have "Lcm A = normalize (\<Prod>A)" by blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1182
  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1183
  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1184
  with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1185
    by (simp add: lcm_coprime)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1186
  finally show ?case .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1187
qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1188
      
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1189
lemma Lcm_coprime':
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1190
  "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1191
    \<Longrightarrow> Lcm A = normalize (\<Prod>A)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1192
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1193
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1194
lemma Gcd_Lcm:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1195
  "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1196
  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1197
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1198
lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1199
  and Gcd_greatest: "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1200
  and unit_factor_Gcd [simp]: 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1201
    "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1202
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1203
  fix a assume "a \<in> A"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1204
  hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_least) blast
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1205
  then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1206
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1207
  fix g' assume "\<And>a. a \<in> A \<Longrightarrow> g' dvd a"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1208
  hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1209
  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1210
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1211
  show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
  1212
    by (simp add: Gcd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1213
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1214
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1215
lemma normalize_Gcd [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1216
  "normalize (Gcd A) = Gcd A"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1217
proof (cases "Gcd A = 0")
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1218
  case True then show ?thesis by simp
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1219
next
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1220
  case False
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1221
  have "unit_factor (Gcd A) * normalize (Gcd A) = Gcd A"
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1222
    by (fact unit_factor_mult_normalize)
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1223
  with False show ?thesis by simp
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1224
qed
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1225
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1226
subclass semiring_Gcd
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1227
  by standard (simp_all add: Gcd_greatest)
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1228
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1229
lemma GcdI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1230
  assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1231
    and "normalize b = b"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1232
  shows "b = Gcd A"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60687
diff changeset
  1233
  by (rule associated_eqI) (auto simp: assms intro: Gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1234
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1235
lemma Lcm_Gcd:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1236
  "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: