src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Wed, 08 Jul 2015 14:01:40 +0200
changeset 60687 33dbbcb6a8a3
parent 60686 ea5bc46c11e6
child 60688 01488b559910
permissions -rw-r--r--
eliminated some duplication
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
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   231
lemma unit_factor_gcd [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   232
  "unit_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
60569
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   233
  by (induct a b rule: gcd_eucl_induct)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   234
    (auto simp add: gcd_0 gcd_non_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   235
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   236
lemma gcdI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   237
  assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   238
    and "unit_factor c = (if c = 0 then 0 else 1)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   239
  shows "c = gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   240
  by (rule associated_eqI) (auto simp: assms associated_def intro: gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   241
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   242
sublocale gcd!: abel_semigroup gcd
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   243
proof
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   244
  fix a b c 
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   245
  show "gcd (gcd a b) c = gcd a (gcd b c)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   246
  proof (rule gcdI)
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   247
    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
   248
    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
   249
    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
   250
    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
   251
    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
   252
    ultimately show "gcd (gcd a b) c dvd gcd b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   253
      by (rule gcd_greatest)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   254
    show "unit_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   255
      by auto
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   256
    fix l assume "l dvd a" and "l dvd gcd b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   257
    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
   258
      have "l dvd b" and "l dvd c" by blast+
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   259
    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
   260
      by (intro gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   261
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   262
next
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   263
  fix a b
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   264
  show "gcd a b = gcd b a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   265
    by (rule gcdI) (simp_all add: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   266
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   267
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   268
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   269
    unit_factor d = (if d = 0 then 0 else 1) \<and>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   270
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   271
  by (rule, auto intro: gcdI simp: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   272
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   273
lemma gcd_dvd_prod: "gcd a b dvd k * b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   274
  using mult_dvd_mono [of 1] by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   275
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   276
lemma gcd_1_left [simp]: "gcd 1 a = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   277
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   278
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   279
lemma gcd_1 [simp]: "gcd a 1 = 1"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   280
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   281
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   282
lemma gcd_proj2_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   283
  "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
   284
  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
   285
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   286
lemma gcd_proj1_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   287
  "a dvd b \<Longrightarrow> gcd a b = normalize a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   288
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   289
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   290
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
   291
proof
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   292
  assume A: "gcd m n = normalize m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   293
  show "m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   294
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   295
    assume [simp]: "m \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   296
    from A have B: "m = gcd m n * unit_factor m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   297
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   298
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   299
  qed (insert A, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   300
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   301
  assume "m dvd n"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   302
  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
   303
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   304
  
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   305
lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   306
  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
   307
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   308
lemma gcd_mod1 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   309
  "gcd (a mod b) b = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   310
  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
   311
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   312
lemma gcd_mod2 [simp]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   313
  "gcd a (b mod a) = gcd a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   314
  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
   315
         
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   316
lemma gcd_mult_distrib': 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   317
  "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
   318
proof (cases "c = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   319
  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
   320
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   321
  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
   322
  show ?thesis
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   323
  proof (induct a b rule: gcd_eucl_induct)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   324
    case (zero a) show ?case
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   325
    proof (cases "a = 0")
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   326
      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
   327
    next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   328
      case False
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   329
      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
   330
    qed
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   331
    case (mod a b)
f2f1f6860959 generalized to definition from literature, which covers also polynomials
haftmann
parents: 60526
diff changeset
   332
    then show ?case by (simp add: mult_mod_right gcd.commute)
58023
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
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   335
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   336
lemma gcd_mult_distrib:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   337
  "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
   338
proof-
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   339
  have "normalize k * gcd a b = gcd (k * a) (k * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   340
    by (simp add: gcd_mult_distrib')
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   341
  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
   342
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   343
  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
   344
    by (simp only: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   345
  then show ?thesis
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   346
    by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   347
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   348
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   349
lemma euclidean_size_gcd_le1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   350
  assumes "a \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   351
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   352
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   353
   have "gcd a b dvd a" by (rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   354
   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
   355
   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
   356
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   357
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   358
lemma euclidean_size_gcd_le2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   359
  "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
   360
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   361
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   362
lemma euclidean_size_gcd_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   363
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   364
  shows "euclidean_size (gcd a b) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   365
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   366
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   367
  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
   368
    by (intro le_antisym, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   369
  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
   370
  hence "a dvd b" using dvd_gcd_D2 by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   371
  with \<open>\<not>a dvd b\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   372
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   373
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   374
lemma euclidean_size_gcd_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   375
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   376
  shows "euclidean_size (gcd a b) < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   377
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   378
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   379
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
   380
  apply (rule gcdI)
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_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   383
  apply (rule gcd_greatest, simp add: unit_simps, assumption)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   384
  apply (subst unit_factor_gcd, simp add: gcd_0)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   385
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   386
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   387
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
   388
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   389
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   390
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
   391
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   392
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   393
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
   394
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   395
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   396
lemma normalize_gcd_left [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   397
  "gcd (normalize a) b = gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   398
proof (cases "a = 0")
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   399
  case True then show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   400
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   401
next
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   402
  case False then have "is_unit (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
  moreover have "normalize a = a div unit_factor a"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   405
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   406
  ultimately show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   407
    by (simp only: gcd_div_unit1)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   408
qed
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   409
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   410
lemma normalize_gcd_right [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   411
  "gcd a (normalize b) = gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   412
  using normalize_gcd_left [of b a] by (simp add: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   413
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   414
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
   415
  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
   416
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   417
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
   418
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   419
  apply (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   420
  apply (rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   421
  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
   422
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   423
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   424
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   425
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
   426
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   427
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   428
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   429
  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
   430
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   431
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   432
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   433
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   434
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   435
  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
   436
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   437
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   438
  fix a show "gcd a \<circ> gcd a = gcd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   439
    by (simp add: fun_eq_iff gcd_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   440
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   441
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   442
lemma coprime_dvd_mult:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   443
  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
   444
  shows "c dvd a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   445
proof -
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   446
  let ?nf = "unit_factor"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   447
  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
   448
    have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   449
  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
   450
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   451
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   452
lemma coprime_dvd_mult_iff:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   453
  "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
   454
  by (rule, rule coprime_dvd_mult, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   455
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   456
lemma gcd_dvd_antisym:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   457
  "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
   458
proof (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   459
  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
   460
  have "gcd c d dvd c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   461
  with A show "gcd a b dvd c" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   462
  have "gcd c d dvd d" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   463
  with A show "gcd a b dvd d" by (rule dvd_trans)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   464
  show "unit_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   465
    by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   466
  fix l assume "l dvd c" and "l dvd d"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   467
  hence "l dvd gcd c d" by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   468
  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
   469
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   470
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   471
lemma gcd_mult_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   472
  assumes "gcd k n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   473
  shows "gcd (k * m) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   474
proof (rule gcd_dvd_antisym)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   475
  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
   476
  also note \<open>gcd k n = 1\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   477
  finally have "gcd (gcd (k * m) n) k = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   478
  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
   479
  moreover have "gcd (k * m) n dvd n" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   480
  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
   481
  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
   482
  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
   483
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   484
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   485
lemma coprime_crossproduct:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   486
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   487
  shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   488
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   489
  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   490
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   491
  assume ?lhs
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   492
  from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   493
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   494
  moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   495
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   496
  moreover from \<open>?lhs\<close> have "c dvd d * b" 
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   497
    unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   498
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   499
  moreover from \<open>?lhs\<close> have "d dvd c * a"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   500
    unfolding associated_def 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 "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   502
  ultimately show ?rhs unfolding associated_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   503
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   504
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   505
lemma gcd_add1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   506
  "gcd (m + n) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   507
  by (cases "n = 0", simp_all add: gcd_non_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   508
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   509
lemma gcd_add2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   510
  "gcd m (m + n) = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   511
  using gcd_add1 [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   512
60572
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   513
lemma gcd_add_mult:
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   514
  "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
   515
proof -
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   516
  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
   517
    by (fact gcd_mod2)
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   518
  then show ?thesis by simp 
718b1ba06429 streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents: 60571
diff changeset
   519
qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   520
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   521
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
   522
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   523
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   524
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
   525
  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
   526
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   527
lemma div_gcd_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   528
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   529
  defines [simp]: "d \<equiv> gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   530
  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
   531
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   532
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   533
  fix l assume "l dvd a'" "l dvd b'"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   534
  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
   535
  moreover have "a = a' * d" "b = b' * d" by simp_all
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   536
  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
   537
    by (simp_all only: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   538
  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
   539
  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
   540
  then obtain u where "d = l * d * u" ..
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   541
  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
   542
  moreover from nz have "d \<noteq> 0" by simp
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   543
  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
   544
  ultimately have "1 = l * u"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   545
    using \<open>d \<noteq> 0\<close> by simp
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   546
  then show "l dvd 1" ..
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   547
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   548
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   549
lemma coprime_mult: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   550
  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   551
  shows "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   552
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   553
  using da apply (subst gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   554
  apply (subst gcd.commute, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   555
  apply (subst gcd.commute, rule db)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   556
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   557
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   558
lemma coprime_lmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   559
  assumes dab: "gcd d (a * b) = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   560
  shows "gcd d a = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   561
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   562
  fix l assume "l dvd d" and "l dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   563
  hence "l dvd a * b" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   564
  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
   565
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   566
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   567
lemma coprime_rmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   568
  assumes dab: "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   569
  shows "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   570
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   571
  fix l assume "l dvd d" and "l dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   572
  hence "l dvd a * b" by simp
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   573
  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
   574
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   575
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   576
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
   577
  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
   578
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   579
lemma gcd_coprime:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   580
  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
   581
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   582
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   583
  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
   584
  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
   585
  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
   586
  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
   587
  finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   588
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   589
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   590
lemma coprime_power:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   591
  assumes "0 < n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   592
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   593
using assms proof (induct n)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   594
  case (Suc n) then show ?case
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   595
    by (cases n) (simp_all add: coprime_mul_eq)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   596
qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   597
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   598
lemma gcd_coprime_exists:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   599
  assumes nz: "gcd a b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   600
  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
   601
  apply (rule_tac x = "a div gcd a b" in exI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   602
  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
   603
  apply (insert nz, auto intro: div_gcd_coprime)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   604
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   605
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   606
lemma coprime_exp:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   607
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   608
  by (induct n, simp_all add: coprime_mult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   609
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   610
lemma coprime_exp2 [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   611
  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   612
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   613
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   614
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   615
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   616
  apply assumption
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   617
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   618
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   619
lemma gcd_exp:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   620
  "gcd (a^n) (b^n) = (gcd a b) ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   621
proof (cases "a = 0 \<and> b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   622
  assume "a = 0 \<and> b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   623
  then show ?thesis by (cases n, simp_all add: gcd_0_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   624
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   625
  assume A: "\<not>(a = 0 \<and> b = 0)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   626
  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   627
    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   628
  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   629
  also note gcd_mult_distrib
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   630
  also have "unit_factor ((gcd a b)^n) = 1"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   631
    by (simp add: unit_factor_power A)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   632
  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
   633
    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
   634
  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
   635
    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
   636
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   637
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   638
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   639
lemma coprime_common_divisor: 
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   640
  "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
   641
  apply (subgoal_tac "a dvd gcd a b")
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   642
  apply simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   643
  apply (erule (1) gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   644
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   645
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   646
lemma division_decomp: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   647
  assumes dc: "a dvd b * c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   648
  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
   649
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   650
  assume "gcd a b = 0"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   651
  hence "a = 0 \<and> b = 0" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   652
  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
   653
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   654
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   655
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   656
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   657
  from gcd_coprime_exists[OF this]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   658
    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
   659
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   660
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   661
  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
   662
  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
   663
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   664
  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
   665
  with coprime_dvd_mult[OF ab'(3)] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   666
    have "a' dvd c" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   667
  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
   668
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   669
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   670
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   671
lemma pow_divs_pow:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   672
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   673
  shows "a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   674
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   675
  assume "gcd a b = 0"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   676
  then show ?thesis by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   677
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   678
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   679
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   680
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   681
  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
   682
  from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   683
    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
   684
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   685
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   686
    by (simp add: ab'(1,2)[symmetric])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   687
  hence "?d^n * a'^n dvd ?d^n * b'^n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   688
    by (simp only: power_mult_distrib ac_simps)
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   689
  with zn have "a'^n dvd b'^n" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   690
  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
   691
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   692
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   693
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   694
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   695
  with ab'(1,2) show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   696
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   697
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   698
lemma pow_divs_eq [simp]:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   699
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   700
  by (auto intro: pow_divs_pow dvd_power_same)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   701
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   702
lemma divs_mult:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   703
  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
   704
  shows "m * n dvd r"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   705
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   706
  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
   707
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   708
  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
   709
  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
   710
  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
   711
  with n' have "r = m * n * k" by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   712
  then show ?thesis unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   713
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   714
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   715
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   716
  by (subst add_commute, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   717
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   718
lemma setprod_coprime [rule_format]:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   719
  "(\<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
   720
  apply (cases "finite A")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   721
  apply (induct set: finite)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   722
  apply (auto simp add: gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   723
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   724
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   725
lemma coprime_divisors: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   726
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   727
  shows "gcd d e = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   728
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   729
  from assms obtain k l where "a = d * k" "b = e * l"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   730
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   731
  with assms have "gcd (d * k) (e * l) = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   732
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   733
  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
   734
  finally have "gcd e d = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   735
  then show ?thesis by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   736
qed
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 invertible_coprime:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   739
  assumes "a * b mod m = 1"
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   740
  shows "coprime a m"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   741
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   742
  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
   743
    by simp
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   744
  then have "coprime m (a * b)"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   745
    by simp
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   746
  then have "coprime m a"
59009
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   747
    by (rule coprime_lmult)
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   748
  then show ?thesis
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   749
    by (simp add: ac_simps)
348561aa3869 generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents: 58953
diff changeset
   750
qed
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   751
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   752
lemma lcm_gcd:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   753
  "lcm a b = normalize (a * b) div gcd a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   754
  by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   755
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   756
subclass semiring_gcd
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   757
  apply standard
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   758
  using gcd_right_idem
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   759
  apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   760
  done
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   761
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   762
lemma lcm_gcd_prod:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   763
  "lcm a b * gcd a b = normalize (a * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   764
  by (simp add: lcm_gcd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   765
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   766
lemma lcm_zero:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   767
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   768
  by (fact lcm_eq_0_iff)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   769
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   770
lemmas lcm_0_iff = lcm_zero
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   771
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   772
lemma gcd_lcm: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   773
  assumes "lcm a b \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   774
  shows "gcd a b = normalize (a * b) div lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   775
proof -
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   776
  have "lcm a b * gcd a b = normalize (a * b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   777
    by (fact lcm_gcd_prod)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   778
  with assms show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   779
    by (metis nonzero_mult_divide_cancel_left)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   780
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   781
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   782
declare unit_factor_lcm [simp]
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   783
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   784
lemma lcmI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   785
  assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   786
    and "unit_factor c = (if c = 0 then 0 else 1)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   787
  shows "c = lcm a b"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   788
  by (rule associated_eqI) (auto simp: assms intro: associatedI lcm_least)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   789
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   790
sublocale lcm!: abel_semigroup lcm ..
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   791
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   792
lemma dvd_lcm_D1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   793
  "lcm m n dvd k \<Longrightarrow> m dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   794
  by (rule dvd_trans, rule lcm_dvd1, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   795
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   796
lemma dvd_lcm_D2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   797
  "lcm m n dvd k \<Longrightarrow> n dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   798
  by (rule dvd_trans, rule lcm_dvd2, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   799
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   800
lemma gcd_dvd_lcm [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   801
  "gcd a b dvd lcm a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   802
  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   803
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   804
lemma lcm_1_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   805
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   806
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   807
  assume "lcm a b = 1"
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   808
  then show "is_unit a \<and> is_unit b" by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   809
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   810
  assume "is_unit a \<and> is_unit b"
59061
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   811
  hence "a dvd 1" and "b dvd 1" by simp_all
67771d267ff2 prefer abbrev for is_unit
haftmann
parents: 59010
diff changeset
   812
  hence "is_unit (lcm a b)" by (rule lcm_least)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   813
  hence "lcm a b = unit_factor (lcm a b)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   814
    by (blast intro: sym is_unit_unit_factor)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   815
  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
   816
    by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   817
  finally show "lcm a b = 1" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   818
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   819
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   820
lemma lcm_0:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   821
  "lcm a 0 = 0"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
   822
  by (fact lcm_0_right)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   823
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   824
lemma lcm_unique:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   825
  "a dvd d \<and> b dvd d \<and> 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   826
  unit_factor d = (if d = 0 then 0 else 1) \<and>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   827
  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   828
  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   829
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   830
lemma dvd_lcm_I1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   831
  "k dvd m \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   832
  by (metis lcm_dvd1 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   833
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   834
lemma dvd_lcm_I2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   835
  "k dvd n \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   836
  by (metis lcm_dvd2 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   837
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   838
lemma lcm_coprime:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   839
  "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   840
  by (subst lcm_gcd) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   841
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   842
lemma lcm_proj1_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   843
  "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
   844
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   845
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   846
lemma lcm_proj2_if_dvd: 
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   847
  "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
   848
  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
   849
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   850
lemma lcm_proj1_iff:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   851
  "lcm m n = normalize m \<longleftrightarrow> n dvd m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   852
proof
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   853
  assume A: "lcm m n = normalize m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   854
  show "n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   855
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   856
    assume [simp]: "m \<noteq> 0"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   857
    from A have B: "m = lcm m n * unit_factor m"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   858
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   859
    show ?thesis by (subst B, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   860
  qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   861
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   862
  assume "n dvd m"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   863
  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
   864
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   865
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   866
lemma lcm_proj2_iff:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   867
  "lcm m n = normalize n \<longleftrightarrow> m dvd n"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   868
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   869
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   870
lemma euclidean_size_lcm_le1: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   871
  assumes "a \<noteq> 0" and "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   872
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   873
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   874
  have "a dvd lcm a b" by (rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   875
  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
   876
  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
   877
  then show ?thesis by (subst A, intro size_mult_mono)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   878
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   879
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   880
lemma euclidean_size_lcm_le2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   881
  "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
   882
  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
   883
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   884
lemma euclidean_size_lcm_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   885
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   886
  shows "euclidean_size a < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   887
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   888
  from assms have "a \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   889
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   890
  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
   891
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   892
  with assms have "lcm a b dvd a" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   893
    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
   894
  hence "b dvd a" by (rule dvd_lcm_D2)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   895
  with \<open>\<not>b dvd a\<close> show False by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   896
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   897
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   898
lemma euclidean_size_lcm_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   899
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   900
  shows "euclidean_size b < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   901
  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
   902
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   903
lemma lcm_mult_unit1:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   904
  "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   905
  apply (rule lcmI)
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   906
  apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   907
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   908
  apply (rule lcm_least, simp add: unit_simps, assumption)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   909
  apply (subst unit_factor_lcm, simp add: lcm_zero)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   910
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   911
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   912
lemma lcm_mult_unit2:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   913
  "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
   914
  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
   915
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   916
lemma lcm_div_unit1:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   917
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   918
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   919
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   920
lemma lcm_div_unit2:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   921
  "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
60433
720f210c5b1d tuned lemmas and proofs
haftmann
parents: 60432
diff changeset
   922
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   923
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   924
lemma normalize_lcm_left [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   925
  "lcm (normalize a) b = lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   926
proof (cases "a = 0")
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   927
  case True then show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   928
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   929
next
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   930
  case False then have "is_unit (unit_factor a)"
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
  moreover have "normalize a = a div unit_factor a"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   933
    by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   934
  ultimately show ?thesis
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   935
    by (simp only: lcm_div_unit1)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   936
qed
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   937
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   938
lemma normalize_lcm_right [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   939
  "lcm a (normalize b) = lcm a b"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   940
  using normalize_lcm_left [of b a] by (simp add: ac_simps)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   941
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   942
lemma lcm_left_idem:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   943
  "lcm a (lcm a b) = lcm a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   944
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   945
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   946
  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   947
  apply (rule lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   948
  apply (erule (1) lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   949
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   950
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   951
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   952
lemma lcm_right_idem:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   953
  "lcm (lcm a b) b = lcm a b"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   954
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   955
  apply (subst lcm.assoc, rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   956
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   957
  apply (rule lcm_least, erule (1) lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   958
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   959
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   960
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   961
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   962
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   963
  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
   964
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   965
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   966
  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
   967
    by (intro ext, simp add: lcm_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   968
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   969
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   970
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   971
  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
   972
  and unit_factor_Lcm [simp]: 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
   973
          "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
   974
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   975
  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
   976
    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
   977
  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
   978
    case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   979
    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
   980
    with False show ?thesis by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   981
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   982
    case True
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   983
    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
   984
    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
   985
    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
   986
    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
   987
      apply (subst n_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   988
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   989
      apply (rule exI[of _ l\<^sub>0])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   990
      apply (simp add: l\<^sub>0_props)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   991
      done
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   992
    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
   993
      unfolding l_def by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   994
    {
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
   995
      fix l' assume "\<forall>a\<in>A. a dvd l'"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
   996
      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
   997
      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
   998
      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
   999
        by (intro exI[of _ "gcd l l'"], auto)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1000
      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
  1001
      moreover have "euclidean_size (gcd l l') \<le> n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1002
      proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1003
        have "gcd l l' dvd l" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1004
        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1005
        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
  1006
        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
  1007
          by (rule size_mult_mono)
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1008
        also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1009
        also note \<open>euclidean_size l = n\<close>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1010
        finally show "euclidean_size (gcd l l') \<le> n" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1011
      qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1012
      ultimately have "euclidean_size l = euclidean_size (gcd l l')" 
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1013
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1014
      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1015
        using dvd_euclidean_size_eq_imp_dvd by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1016
      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1017
    }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1018
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1019
    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
  1020
      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1021
        (\<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
  1022
        unit_factor (normalize l) = 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1023
        (if normalize l = 0 then 0 else 1)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1024
      by (auto simp: unit_simps)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1025
    also from True have "normalize l = Lcm A"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1026
      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
  1027
    finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1028
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1029
  note A = this
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1030
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1031
  {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
  1032
  {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
  1033
  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
  1034
qed
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1035
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1036
lemma normalize_Lcm [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1037
  "normalize (Lcm A) = Lcm A"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1038
  by (cases "Lcm A = 0") (auto intro: associated_eqI)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1039
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1040
lemma LcmI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1041
  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"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1042
    and "unit_factor b = (if b = 0 then 0 else 1)" shows "b = Lcm A"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1043
  by (rule associated_eqI) (auto simp: assms associated_def intro: Lcm_least)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1044
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1045
lemma Lcm_subset:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1046
  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1047
  by (blast intro: Lcm_least dvd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1048
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1049
lemma Lcm_Un:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1050
  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1051
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1052
  apply (blast intro: Lcm_subset)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1053
  apply (blast intro: Lcm_subset)
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1054
  apply (intro Lcm_least ballI, elim UnE)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1055
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1056
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1057
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1058
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1059
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1060
lemma Lcm_1_iff:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1061
  "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1062
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1063
  assume "Lcm A = 1"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1064
  then show "\<forall>a\<in>A. is_unit a" by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1065
qed (rule LcmI [symmetric], auto)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1066
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1067
lemma Lcm_no_units:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1068
  "Lcm A = Lcm (A - {a. is_unit a})"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1069
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1070
  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
  1071
  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
  1072
    by (simp add: Lcm_Un [symmetric])
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1073
  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
  1074
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1075
qed
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_empty [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1078
  "Lcm {} = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1079
  by (simp add: Lcm_1_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1080
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1081
lemma Lcm_eq_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1082
  "0 \<in> A \<Longrightarrow> Lcm A = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1083
  by (drule dvd_Lcm) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1084
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1085
lemma Lcm0_iff':
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1086
  "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
  1087
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1088
  assume "Lcm A = 0"
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1089
  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
  1090
  proof
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1091
    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
  1092
    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
  1093
    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
  1094
    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
  1095
    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
  1096
      apply (subst n_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1097
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1098
      apply (rule exI[of _ l\<^sub>0])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1099
      apply (simp add: l\<^sub>0_props)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1100
      done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1101
    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
  1102
    hence "normalize l \<noteq> 0" by simp
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1103
    also from ex have "normalize l = Lcm A"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1104
       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
  1105
    finally show False using \<open>Lcm A = 0\<close> by contradiction
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1106
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1107
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1108
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1109
lemma Lcm0_iff [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1110
  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1111
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1112
  assume "finite A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1113
  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
  1114
  moreover {
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1115
    assume "0 \<notin> A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1116
    hence "\<Prod>A \<noteq> 0" 
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1117
      apply (induct rule: finite_induct[OF \<open>finite A\<close>]) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1118
      apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1119
      apply (subst setprod.insert, assumption, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1120
      apply (rule no_zero_divisors)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1121
      apply blast+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1122
      done
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1123
    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
  1124
    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
  1125
    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1126
  }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1127
  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1128
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1129
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1130
lemma Lcm_no_multiple:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1131
  "(\<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
  1132
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1133
  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
  1134
  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
  1135
  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
  1136
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1137
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1138
lemma Lcm_insert [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1139
  "Lcm (insert a A) = lcm a (Lcm A)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1140
proof (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1141
  fix l assume "a dvd l" and "Lcm A dvd l"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1142
  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
  1143
  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
  1144
qed (auto intro: Lcm_least dvd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1145
 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1146
lemma Lcm_finite:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1147
  assumes "finite A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1148
  shows "Lcm A = Finite_Set.fold lcm 1 A"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1149
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1150
    (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
  1151
60431
db9c67b760f1 dropped warnings by dropping ineffective code declarations
haftmann
parents: 60430
diff changeset
  1152
lemma Lcm_set [code_unfold]:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1153
  "Lcm (set xs) = fold lcm xs 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1154
  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
  1155
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1156
lemma Lcm_singleton [simp]:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1157
  "Lcm {a} = normalize a"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1158
  by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1159
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1160
lemma Lcm_2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1161
  "Lcm {a,b} = lcm a b"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1162
  by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1163
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1164
lemma Lcm_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1165
  assumes "finite A" and "A \<noteq> {}" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1166
  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
  1167
  shows "Lcm A = normalize (\<Prod>A)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1168
using assms proof (induct rule: finite_ne_induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1169
  case (insert a A)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1170
  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1171
  also from insert have "Lcm A = normalize (\<Prod>A)" by blast
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1172
  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
  1173
  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
  1174
  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
  1175
    by (simp add: lcm_coprime)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1176
  finally show ?case .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1177
qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1178
      
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1179
lemma Lcm_coprime':
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1180
  "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
  1181
    \<Longrightarrow> Lcm A = normalize (\<Prod>A)"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1182
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1183
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1184
lemma Gcd_Lcm:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1185
  "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1186
  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1187
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1188
lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1189
  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
  1190
  and unit_factor_Gcd [simp]: 
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1191
    "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
  1192
proof -
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1193
  fix a assume "a \<in> A"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1194
  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
  1195
  then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1196
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1197
  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
  1198
  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
  1199
  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1200
next
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1201
  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
  1202
    by (simp add: Gcd_Lcm)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1203
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1204
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1205
lemma normalize_Gcd [simp]:
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1206
  "normalize (Gcd A) = Gcd A"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1207
  by (cases "Gcd A = 0") (auto intro: associated_eqI)
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1208
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1209
subclass semiring_Gcd
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1210
  by standard (simp_all add: Gcd_greatest)
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1211
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1212
lemma GcdI:
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1213
  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"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1214
    and "unit_factor b = (if b = 0 then 0 else 1)"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1215
  shows "b = Gcd A"
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1216
  by (rule associated_eqI) (auto simp: assms associated_def intro: Gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1217
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1218
lemma Lcm_Gcd:
60430
ce559c850a27 standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents: 59061
diff changeset
  1219
  "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1220
  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1221
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1222
subclass semiring_Lcm
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1223
  by standard (simp add: Lcm_Gcd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1224
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1225
lemma Gcd_1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1226
  "1 \<in> A \<Longrightarrow> Gcd A = 1"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1227
  by (auto intro!: Gcd_eq_1_I)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1228
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1229
lemma Gcd_finite:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1230
  assumes "finite A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1231
  shows "Gcd A = Finite_Set.fold gcd 0 A"
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1232
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1233
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1234
60431
db9c67b760f1 dropped warnings by dropping ineffective code declarations
haftmann
parents: 60430
diff changeset
  1235
lemma Gcd_set [code_unfold]:
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1236
  "Gcd (set xs) = fold gcd xs 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1237
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1238
60634
e3b6e516608b separate (semi)ring with normalization
haftmann
parents: 60600
diff changeset
  1239
lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1240
  by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1241
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1242
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
60687
33dbbcb6a8a3 eliminated some duplication
haftmann
parents: 60686
diff changeset
  1243
  by simp
60686
ea5bc46c11e6 more algebraic properties for gcd/lcm
haftmann
parents: 60685
diff changeset
  1244
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1245
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1246
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 60517
diff changeset
  1247
text \<open>
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents: