src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Sun, 09 Nov 2014 10:03:18 +0100
changeset 58954 18750e86d5b8
parent 58953 2e19b392d9e3
child 59009 348561aa3869
permissions -rw-r--r--
reverted 1ebf0a1f12a4 after successful re-tuning of simp rules for divisibility
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
58889
5b7a9633cfa8 modernized header uniformly as section;
wenzelm
parents: 58023
diff changeset
     3
section {* Abstract euclidean algorithm *}
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
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     6
imports Complex_Main
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     7
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     8
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
     9
lemma finite_int_set_iff_bounded_le:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    10
  "finite (N::int set) = (\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    11
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    12
  assume "finite (N::int set)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    13
  hence "finite (nat ` abs ` N)" by (intro finite_imageI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    14
  hence "\<exists>m. \<forall>n\<in>nat`abs`N. n \<le> m" by (simp add: finite_nat_set_iff_bounded_le)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    15
  then obtain m :: nat where "\<forall>n\<in>N. nat (abs n) \<le> nat (int m)" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    16
  then show "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m" by (intro exI[of _ "int m"]) (auto simp: nat_le_eq_zle)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    17
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    18
  assume "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    19
  then obtain m where "m \<ge> 0" and "\<forall>n\<in>N. abs n \<le> m" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    20
  hence "\<forall>n\<in>N. nat (abs n) \<le> nat m" by (auto simp: nat_le_eq_zle)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    21
  hence "\<forall>n\<in>nat`abs`N. n \<le> nat m" by (auto simp: nat_le_eq_zle)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    22
  hence A: "finite ((nat \<circ> abs)`N)" unfolding o_def 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    23
      by (subst finite_nat_set_iff_bounded_le) blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    24
  {
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    25
    assume "\<not>finite N"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    26
    from pigeonhole_infinite[OF this A] obtain x 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    27
       where "x \<in> N" and B: "~finite {a\<in>N. nat (abs a) = nat (abs x)}" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    28
       unfolding o_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    29
    have "{a\<in>N. nat (abs a) = nat (abs x)} \<subseteq> {x, -x}" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    30
    hence "finite {a\<in>N. nat (abs a) = nat (abs x)}" by (rule finite_subset) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    31
    with B have False by contradiction
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    32
  }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    33
  then show "finite N" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    34
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    35
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    36
context semiring_div
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    37
begin
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_setprod [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    40
  assumes "finite A" and "x \<in> A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    41
  shows "f x dvd setprod f A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    42
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    43
  from `finite A` have "setprod f (insert x (A - {x})) = f x * setprod f (A - {x})"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    44
    by (intro setprod.insert) auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    45
  also from `x \<in> A` have "insert x (A - {x}) = A" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    46
  finally show "setprod f A = f x * setprod f (A - {x})" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    47
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    48
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    49
lemma dvd_mult_cancel_left:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    50
  assumes "a \<noteq> 0" and "a * b dvd a * c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    51
  shows "b dvd c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    52
proof-
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    53
  from assms(2) obtain k where "a * c = a * b * k" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    54
  hence "c * a = b * k * a" by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    55
  hence "c * (a div a) = b * k * (a div a)" by (simp add: div_mult_swap)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    56
  also from `a \<noteq> 0` have "a div a = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    57
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    58
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    59
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    60
lemma dvd_mult_cancel_right:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    61
  "a \<noteq> 0 \<Longrightarrow> b * a dvd c * a \<Longrightarrow> b dvd c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    62
  by (subst (asm) (1 2) ac_simps, rule dvd_mult_cancel_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    63
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    64
lemma nonzero_pow_nonzero:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    65
  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    66
  by (induct n) (simp_all add: no_zero_divisors)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    67
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    68
lemma zero_pow_zero: "n \<noteq> 0 \<Longrightarrow> 0 ^ n = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    69
  by (cases n, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    70
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    71
lemma pow_zero_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    72
  "n \<noteq> 0 \<Longrightarrow> a^n = 0 \<longleftrightarrow> a = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    73
  using nonzero_pow_nonzero zero_pow_zero by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    74
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    75
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    76
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    77
context semiring_div
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    78
begin 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    79
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    80
definition ring_inv :: "'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    81
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    82
  "ring_inv x = 1 div x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    83
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    84
definition is_unit :: "'a \<Rightarrow> bool"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    85
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    86
  "is_unit x \<longleftrightarrow> x dvd 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    87
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    88
definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    89
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    90
  "associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    91
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    92
lemma unit_prod [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    93
  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    94
  unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    95
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    96
lemma unit_ring_inv:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    97
  "is_unit y \<Longrightarrow> x div y = x * ring_inv y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    98
  by (simp add: div_mult_swap ring_inv_def is_unit_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
    99
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   100
lemma unit_ring_inv_ring_inv [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   101
  "is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   102
  unfolding is_unit_def ring_inv_def
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   103
  by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   104
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   105
lemma inv_imp_eq_ring_inv:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   106
  "a * b = 1 \<Longrightarrow> ring_inv a = b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   107
  by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   108
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   109
lemma ring_inv_is_inv1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   110
  "is_unit a \<Longrightarrow> a * ring_inv a = 1"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
   111
  unfolding is_unit_def ring_inv_def by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   112
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   113
lemma ring_inv_is_inv2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   114
  "is_unit a \<Longrightarrow> ring_inv a * a = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   115
  by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   116
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   117
lemma unit_ring_inv_unit [simp, intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   118
  assumes "is_unit x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   119
  shows "is_unit (ring_inv x)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   120
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   121
  from assms have "1 = ring_inv x * x" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   122
  then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   123
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   124
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   125
lemma mult_unit_dvd_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   126
  "is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   127
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   128
  assume "is_unit y" "x * y dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   129
  then show "x dvd z" by (simp add: dvd_mult_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   130
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   131
  assume "is_unit y" "x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   132
  then obtain k where "z = x * k" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   133
  with `is_unit y` have "z = (x * y) * (ring_inv y * k)" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   134
      by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   135
  then show "x * y dvd z" by (rule dvdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   136
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   137
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   138
lemma div_unit_dvd_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   139
  "is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   140
  by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   141
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   142
lemma dvd_mult_unit_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   143
  "is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   144
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   145
  assume "is_unit y" and "x dvd z * y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   146
  have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   147
  also from `is_unit y` have "y * ring_inv y = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   148
  finally have "z * y dvd z" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   149
  with `x dvd z * y` show "x dvd z" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   150
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   151
  assume "x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   152
  then show "x dvd z * y" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   153
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   154
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   155
lemma dvd_div_unit_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   156
  "is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   157
  by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   158
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   159
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   160
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   161
lemma unit_div [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   162
  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   163
  by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   164
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   165
lemma unit_div_mult_swap:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   166
  "is_unit z \<Longrightarrow> x * (y div z) = x * y div z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   167
  by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   168
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   169
lemma unit_div_commute:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   170
  "is_unit y \<Longrightarrow> x div y * z = x * z div y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   171
  by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   172
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   173
lemma unit_imp_dvd [dest]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   174
  "is_unit y \<Longrightarrow> y dvd x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   175
  by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   176
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   177
lemma dvd_unit_imp_unit:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   178
  "is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   179
  by (unfold is_unit_def) (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   180
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   181
lemma ring_inv_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   182
  "ring_inv 0 = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   183
  unfolding ring_inv_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   184
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   185
lemma unit_ring_inv'1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   186
  assumes "is_unit y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   187
  shows "x div (y * z) = x * ring_inv y div z" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   188
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   189
  from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   190
    by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   191
  also have "... = y * (x * ring_inv y) div (y * z)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   192
    by (simp only: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   193
  also have "... = x * ring_inv y div z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   194
    by (cases "y = 0", simp, rule div_mult_mult1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   195
  finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   196
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   197
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   198
lemma associated_comm:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   199
  "associated x y \<Longrightarrow> associated y x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   200
  by (simp add: associated_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   201
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   202
lemma associated_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   203
  "associated 0 b \<longleftrightarrow> b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   204
  "associated a 0 \<longleftrightarrow> a = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   205
  unfolding associated_def by simp_all
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 associated_unit:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   208
  "is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   209
  unfolding associated_def by (fast dest: dvd_unit_imp_unit)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   210
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   211
lemma is_unit_1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   212
  "is_unit 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   213
  unfolding is_unit_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   214
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   215
lemma not_is_unit_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   216
  "\<not> is_unit 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   217
  unfolding is_unit_def by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   218
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   219
lemma unit_mult_left_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   220
  assumes "is_unit x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   221
  shows "(x * y) = (x * z) \<longleftrightarrow> y = z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   222
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   223
  from assms have "x \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   224
  then show ?thesis by (metis div_mult_self1_is_id)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   225
qed
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 unit_mult_right_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   228
  "is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   229
  by (simp add: ac_simps unit_mult_left_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   230
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   231
lemma unit_div_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   232
  "is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   233
  apply (subst unit_ring_inv[of _ y], assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   234
  apply (subst unit_ring_inv[of _ z], assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   235
  apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   236
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   237
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   238
lemma unit_eq_div1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   239
  "is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   240
  apply (subst unit_ring_inv, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   241
  apply (subst unit_mult_right_cancel[symmetric], assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   242
  apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   243
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   244
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   245
lemma unit_eq_div2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   246
  "is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   247
  by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   248
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   249
lemma associated_iff_div_unit:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   250
  "associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   251
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   252
  assume "associated x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   253
  show "\<exists>z. is_unit z \<and> x = z * y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   254
  proof (cases "x = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   255
    assume "x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   256
    then show "\<exists>z. is_unit z \<and> x = z * y" using `associated x y`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   257
        by (intro exI[of _ 1], simp add: associated_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   258
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   259
    assume [simp]: "x \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   260
    hence [simp]: "x dvd y" "y dvd x" using `associated x y`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   261
        unfolding associated_def by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   262
    hence "1 = x div y * (y div x)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   263
      by (simp add: div_mult_swap dvd_div_mult_self)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   264
    hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   265
    moreover have "x = (x div y) * y" by (simp add: dvd_div_mult_self)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   266
    ultimately show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   267
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   268
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   269
  assume "\<exists>z. is_unit z \<and> x = z * y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   270
  then obtain z where "is_unit z" and "x = z * y" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   271
  hence "y = x * ring_inv z" by (simp add: algebra_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   272
  hence "x dvd y" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   273
  moreover from `x = z * y` have "y dvd x" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   274
  ultimately show "associated x y" unfolding associated_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   275
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   276
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   277
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   278
  dvd_div_unit_iff unit_div_mult_swap unit_div_commute
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   279
  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   280
  unit_eq_div1 unit_eq_div2
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
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   283
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   284
context ring_div
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   285
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   286
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   287
lemma is_unit_neg [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   288
  "is_unit (- x) \<Longrightarrow> is_unit x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   289
  unfolding is_unit_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   290
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   291
lemma is_unit_neg_1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   292
  "is_unit (-1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   293
  unfolding is_unit_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   294
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   295
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   296
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   297
lemma is_unit_nat [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   298
  "is_unit (x::nat) \<longleftrightarrow> x = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   299
  unfolding is_unit_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   300
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   301
lemma is_unit_int:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   302
  "is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = -1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   303
  unfolding is_unit_def by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   304
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   305
text {*
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   306
  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
   307
  implemented. It must provide:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   308
  \begin{itemize}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   309
  \item division with remainder
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   310
  \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
   311
        for any @{term "b \<noteq> 0"}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   312
  \item a normalisation factor such that two associated numbers are equal iff 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   313
        they are the same when divided by their normalisation factors.
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   314
  \end{itemize}
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   315
  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
   316
  for any Euclidean semiring.
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   317
*} 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   318
class euclidean_semiring = semiring_div + 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   319
  fixes euclidean_size :: "'a \<Rightarrow> nat"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   320
  fixes normalisation_factor :: "'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   321
  assumes mod_size_less [simp]: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   322
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   323
  assumes size_mult_mono:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   324
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   325
  assumes normalisation_factor_is_unit [intro,simp]: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   326
    "a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   327
  assumes normalisation_factor_mult: "normalisation_factor (a * b) = 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   328
    normalisation_factor a * normalisation_factor b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   329
  assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   330
  assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   331
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   332
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   333
lemma normalisation_factor_dvd [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   334
  "a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   335
  by (rule unit_imp_dvd, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   336
    
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   337
lemma normalisation_factor_1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   338
  "normalisation_factor 1 = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   339
  by (simp add: normalisation_factor_unit)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   340
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   341
lemma normalisation_factor_0_iff [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   342
  "normalisation_factor x = 0 \<longleftrightarrow> x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   343
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   344
  assume "normalisation_factor x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   345
  hence "\<not> is_unit (normalisation_factor x)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   346
    by (metis not_is_unit_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   347
  then show "x = 0" by force
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   348
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   349
  assume "x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   350
  then show "normalisation_factor x = 0" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   351
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   352
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   353
lemma normalisation_factor_pow:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   354
  "normalisation_factor (x ^ n) = normalisation_factor x ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   355
  by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   356
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   357
lemma normalisation_correct [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   358
  "normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   359
proof (cases "x = 0", simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   360
  assume "x \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   361
  let ?nf = "normalisation_factor"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   362
  from normalisation_factor_is_unit[OF `x \<noteq> 0`] have "?nf x \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   363
    by (metis not_is_unit_0) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   364
  have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   365
    by (simp add: normalisation_factor_mult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   366
  also have "x div ?nf x * ?nf x = x" using `x \<noteq> 0`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   367
    by (simp add: dvd_div_mult_self)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   368
  also have "?nf (?nf x) = ?nf x" using `x \<noteq> 0` 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   369
    normalisation_factor_is_unit normalisation_factor_unit by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   370
  finally show ?thesis using `x \<noteq> 0` and `?nf x \<noteq> 0` 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   371
    by (metis div_mult_self2_is_id div_self)
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 normalisation_0_iff [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   375
  "x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   376
  by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   377
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   378
lemma associated_iff_normed_eq:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   379
  "associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   380
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   381
  let ?nf = normalisation_factor
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   382
  assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   383
  hence "a = b * (?nf a div ?nf b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   384
    apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   385
    apply (subst div_mult_swap, simp, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   386
    done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   387
  with `a \<noteq> 0` `b \<noteq> 0` have "\<exists>z. is_unit z \<and> a = z * b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   388
    by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   389
  with associated_iff_div_unit show "associated a b" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   390
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   391
  let ?nf = normalisation_factor
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   392
  assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   393
  with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   394
  then show "a div ?nf a = b div ?nf b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   395
    apply (simp only: `a = z * b` normalisation_factor_mult normalisation_factor_unit)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   396
    apply (rule div_mult_mult1, force)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   397
    done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   398
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   399
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   400
lemma normed_associated_imp_eq:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   401
  "associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   402
  by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   403
    
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   404
lemmas normalisation_factor_dvd_iff [simp] =
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   405
  unit_dvd_iff [OF normalisation_factor_is_unit]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   406
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   407
lemma euclidean_division:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   408
  fixes a :: 'a and b :: 'a
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   409
  assumes "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   410
  obtains s and t where "a = s * b + t" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   411
    and "euclidean_size t < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   412
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   413
  from div_mod_equality[of a b 0] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   414
     have "a = a div b * b + a mod b" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   415
  with that and assms show ?thesis by force
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   416
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   417
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   418
lemma dvd_euclidean_size_eq_imp_dvd:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   419
  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
   420
  shows "a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   421
proof (subst dvd_eq_mod_eq_0, rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   422
  assume "b mod a \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   423
  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
   424
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   425
    with `b mod a \<noteq> 0` have "c \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   426
  with `b mod a = b * c` have "euclidean_size (b mod a) \<ge> euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   427
      using size_mult_mono by force
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   428
  moreover from `a \<noteq> 0` have "euclidean_size (b mod a) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   429
      using mod_size_less by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   430
  ultimately show False using size_eq by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   431
qed
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
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   434
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   435
  "gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   436
  by (pat_completeness, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   437
termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   438
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   439
declare gcd_eucl.simps [simp del]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   440
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   441
lemma gcd_induct: "\<lbrakk>\<And>b. P b 0; \<And>a b. 0 \<noteq> b \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   442
proof (induct a b rule: gcd_eucl.induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   443
  case ("1" m n)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   444
    then show ?case by (cases "n = 0") auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   445
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   446
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   447
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   448
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   449
  "lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   450
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   451
  (* Somewhat complicated definition of Lcm that has the advantage of working
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   452
     for infinite sets as well *)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   453
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   454
definition Lcm_eucl :: "'a set \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   455
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   456
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   457
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l =
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   458
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   459
       in l div normalisation_factor l
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   460
      else 0)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   461
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   462
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   463
where
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   464
  "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
   465
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   466
end
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   467
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   468
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   469
  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
   470
  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
   471
begin
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   472
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   473
lemma gcd_red:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   474
  "gcd x y = gcd y (x mod y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   475
  by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   476
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   477
lemma gcd_non_0:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   478
  "y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   479
  by (rule gcd_red)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   480
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   481
lemma gcd_0_left:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   482
  "gcd 0 x = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   483
   by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)
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 gcd_0:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   486
  "gcd x 0 = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   487
  by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   488
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   489
lemma gcd_dvd1 [iff]: "gcd x y dvd x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   490
  and gcd_dvd2 [iff]: "gcd x y dvd y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   491
proof (induct x y rule: gcd_eucl.induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   492
  fix x y :: 'a
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   493
  assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   494
  assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   495
  
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   496
  have "gcd x y dvd x \<and> gcd x y dvd y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   497
  proof (cases "y = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   498
    case True
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   499
      then show ?thesis by (cases "x = 0", simp_all add: gcd_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   500
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   501
    case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   502
      with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
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
  then show "gcd x y dvd x" "gcd x y dvd y" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   505
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   506
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   507
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
   508
  by (rule dvd_trans, assumption, rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   509
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   510
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
   511
  by (rule dvd_trans, assumption, rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   512
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   513
lemma gcd_greatest:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   514
  fixes k x y :: 'a
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   515
  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   516
proof (induct x y rule: gcd_eucl.induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   517
  case (1 x y)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   518
  show ?case
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   519
    proof (cases "y = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   520
      assume "y = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   521
      with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   522
    next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   523
      assume "y \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   524
      with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   525
    qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   526
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   527
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   528
lemma dvd_gcd_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   529
  "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   530
  by (blast intro!: gcd_greatest intro: dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   531
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   532
lemmas gcd_greatest_iff = dvd_gcd_iff
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   533
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   534
lemma gcd_zero [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   535
  "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   536
  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
   537
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   538
lemma normalisation_factor_gcd [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   539
  "normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   540
proof (induct x y rule: gcd_eucl.induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   541
  fix x y :: 'a
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   542
  assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   543
  then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   544
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   545
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   546
lemma gcdI:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   547
  "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   548
    \<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   549
  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   550
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   551
sublocale gcd!: abel_semigroup gcd
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   552
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   553
  fix x y z 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   554
  show "gcd (gcd x y) z = gcd x (gcd y z)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   555
  proof (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   556
    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   557
    then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   558
    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   559
    hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   560
    moreover have "gcd (gcd x y) z dvd z" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   561
    ultimately show "gcd (gcd x y) z dvd gcd y z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   562
      by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   563
    show "normalisation_factor (gcd (gcd x y) z) =  (if gcd (gcd x y) z = 0 then 0 else 1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   564
      by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   565
    fix l assume "l dvd x" and "l dvd gcd y z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   566
    with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   567
      have "l dvd y" and "l dvd z" by blast+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   568
    with `l dvd x` show "l dvd gcd (gcd x y) z"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   569
      by (intro gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   570
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   571
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   572
  fix x y
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   573
  show "gcd x y = gcd y x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   574
    by (rule gcdI) (simp_all add: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   575
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   576
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   577
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   578
    normalisation_factor d = (if d = 0 then 0 else 1) \<and>
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   579
    (\<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
   580
  by (rule, auto intro: gcdI simp: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   581
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   582
lemma gcd_dvd_prod: "gcd a b dvd k * b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   583
  using mult_dvd_mono [of 1] by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   584
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   585
lemma gcd_1_left [simp]: "gcd 1 x = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   586
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   587
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   588
lemma gcd_1 [simp]: "gcd x 1 = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   589
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   590
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   591
lemma gcd_proj2_if_dvd: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   592
  "y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   593
  by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   594
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   595
lemma gcd_proj1_if_dvd: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   596
  "x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   597
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   598
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   599
lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   600
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   601
  assume A: "gcd m n = m div normalisation_factor m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   602
  show "m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   603
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   604
    assume [simp]: "m \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   605
    from A have B: "m = gcd m n * normalisation_factor m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   606
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   607
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   608
  qed (insert A, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   609
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   610
  assume "m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   611
  then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   612
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   613
  
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   614
lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   615
  by (subst gcd.commute, simp add: gcd_proj1_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   616
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   617
lemma gcd_mod1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   618
  "gcd (x mod y) y = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   619
  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
   620
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   621
lemma gcd_mod2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   622
  "gcd x (y mod x) = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   623
  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
   624
         
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   625
lemma normalisation_factor_dvd' [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   626
  "normalisation_factor x dvd x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   627
  by (cases "x = 0", simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   628
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   629
lemma gcd_mult_distrib': 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   630
  "k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   631
proof (induct x y rule: gcd_eucl.induct)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   632
  case (1 x y)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   633
  show ?case
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   634
  proof (cases "y = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   635
    case True
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   636
    then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   637
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   638
    case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   639
    hence "k div normalisation_factor k * gcd x y =  gcd (k * y) (k * (x mod y))" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   640
      using 1 by (subst gcd_red, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   641
    also have "... = gcd (k * x) (k * y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   642
      by (simp add: mult_mod_right gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   643
    finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   644
  qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   645
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   646
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   647
lemma gcd_mult_distrib:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   648
  "k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   649
proof-
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   650
  let ?nf = "normalisation_factor"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   651
  from gcd_mult_distrib' 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   652
    have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   653
  also have "... = k * gcd x y div ?nf k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   654
    by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   655
  finally show ?thesis
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   656
    by (simp add: ac_simps dvd_mult_div_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   657
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   658
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   659
lemma euclidean_size_gcd_le1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   660
  assumes "a \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   661
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   662
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   663
   have "gcd a b dvd a" by (rule gcd_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   664
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   665
   with `a \<noteq> 0` show ?thesis by (subst (2) A, intro size_mult_mono) auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   666
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   667
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   668
lemma euclidean_size_gcd_le2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   669
  "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
   670
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   671
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   672
lemma euclidean_size_gcd_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   673
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   674
  shows "euclidean_size (gcd a b) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   675
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   676
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   677
  with `a \<noteq> 0` have "euclidean_size (gcd a b) = euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   678
    by (intro le_antisym, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   679
  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
   680
  hence "a dvd b" using dvd_gcd_D2 by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   681
  with `\<not>a dvd b` show False by contradiction
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   682
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   683
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   684
lemma euclidean_size_gcd_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   685
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   686
  shows "euclidean_size (gcd a b) < euclidean_size b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   687
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   688
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   689
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   690
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   691
  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   692
  apply (rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   693
  apply (rule gcd_greatest, simp add: unit_simps, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   694
  apply (subst normalisation_factor_gcd, simp add: gcd_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   695
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   696
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   697
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   698
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   699
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   700
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   701
  by (simp add: unit_ring_inv gcd_mult_unit1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   702
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   703
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   704
  by (simp add: unit_ring_inv gcd_mult_unit2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   705
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   706
lemma gcd_idem: "gcd x x = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   707
  by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   708
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   709
lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   710
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   711
  apply (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   712
  apply (rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   713
  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   714
  apply (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   715
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   716
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   717
lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   718
  apply (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   719
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   720
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   721
  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   722
  apply (simp add: gcd_zero)
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 comp_fun_idem_gcd: "comp_fun_idem gcd"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   726
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   727
  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
   728
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   729
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   730
  fix a show "gcd a \<circ> gcd a = gcd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   731
    by (simp add: fun_eq_iff gcd_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   732
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   733
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   734
lemma coprime_dvd_mult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   735
  assumes "gcd k n = 1" and "k dvd m * n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   736
  shows "k dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   737
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   738
  let ?nf = "normalisation_factor"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   739
  from assms gcd_mult_distrib [of m k n] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   740
    have A: "m = gcd (m * k) (m * n) * ?nf m" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   741
  from `k dvd m * n` show ?thesis by (subst A, simp_all add: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   742
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   743
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   744
lemma coprime_dvd_mult_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   745
  "gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   746
  by (rule, rule coprime_dvd_mult, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   747
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   748
lemma gcd_dvd_antisym:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   749
  "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
   750
proof (rule gcdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   751
  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
   752
  have "gcd c d dvd c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   753
  with A show "gcd a b dvd c" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   754
  have "gcd c d dvd d" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   755
  with A show "gcd a b dvd d" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   756
  show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   757
    by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   758
  fix l assume "l dvd c" and "l dvd d"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   759
  hence "l dvd gcd c d" by (rule gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   760
  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
   761
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   762
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   763
lemma gcd_mult_cancel:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   764
  assumes "gcd k n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   765
  shows "gcd (k * m) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   766
proof (rule gcd_dvd_antisym)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   767
  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   768
  also note `gcd k n = 1`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   769
  finally have "gcd (gcd (k * m) n) k = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   770
  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
   771
  moreover have "gcd (k * m) n dvd n" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   772
  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
   773
  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
   774
  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
   775
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   776
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   777
lemma coprime_crossproduct:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   778
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   779
  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
   780
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   781
  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
   782
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   783
  assume ?lhs
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   784
  from `?lhs` have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   785
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   786
  moreover from `?lhs` have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   787
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   788
  moreover from `?lhs` have "c dvd d * b" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   789
    unfolding associated_def by (metis dvd_mult_right ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   790
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   791
  moreover from `?lhs` have "d dvd c * a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   792
    unfolding associated_def by (metis dvd_mult_right ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   793
  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
   794
  ultimately show ?rhs unfolding associated_def by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   795
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   796
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   797
lemma gcd_add1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   798
  "gcd (m + n) n = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   799
  by (cases "n = 0", simp_all add: gcd_non_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   800
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   801
lemma gcd_add2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   802
  "gcd m (m + n) = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   803
  using gcd_add1 [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   804
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   805
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   806
  by (subst gcd.commute, subst gcd_red, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   807
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   808
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   809
  by (rule sym, rule gcdI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   810
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   811
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   812
  by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   813
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   814
lemma div_gcd_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   815
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   816
  defines [simp]: "d \<equiv> gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   817
  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
   818
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   819
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   820
  fix l assume "l dvd a'" "l dvd b'"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   821
  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   822
  moreover have "a = a' * d" "b = b' * d" by (simp_all add: dvd_div_mult_self)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   823
  ultimately have "a = (l * d) * s" "b = (l * d) * t"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   824
    by (metis ac_simps)+
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   825
  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
   826
  hence "l*d dvd d" by (simp add: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   827
  then obtain u where "u * l * d = d" unfolding dvd_def
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   828
    by (metis ac_simps mult_assoc)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   829
  moreover from nz have "d \<noteq> 0" by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   830
  ultimately have "u * l = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   831
    by (metis div_mult_self1_is_id div_self ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   832
  then show "l dvd 1" by force
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   833
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   834
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   835
lemma coprime_mult: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   836
  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   837
  shows "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   838
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   839
  using da apply (subst gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   840
  apply (subst gcd.commute, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   841
  apply (subst gcd.commute, rule db)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   842
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   843
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   844
lemma coprime_lmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   845
  assumes dab: "gcd d (a * b) = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   846
  shows "gcd d a = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   847
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   848
  fix l assume "l dvd d" and "l dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   849
  hence "l dvd a * b" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   850
  with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   851
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   852
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   853
lemma coprime_rmult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   854
  assumes dab: "gcd d (a * b) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   855
  shows "gcd d b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   856
proof (rule coprimeI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   857
  fix l assume "l dvd d" and "l dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   858
  hence "l dvd a * b" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   859
  with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   860
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   861
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   862
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
   863
  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
   864
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   865
lemma gcd_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   866
  assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   867
  shows "gcd a' b' = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   868
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   869
  from z have "a \<noteq> 0 \<or> b \<noteq> 0" by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   870
  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
   871
  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
   872
  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
   873
  finally show ?thesis .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   874
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   875
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   876
lemma coprime_power:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   877
  assumes "0 < n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   878
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   879
using assms proof (induct n)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   880
  case (Suc n) then show ?case
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   881
    by (cases n) (simp_all add: coprime_mul_eq)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   882
qed simp
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 gcd_coprime_exists:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   885
  assumes nz: "gcd a b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   886
  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
   887
  apply (rule_tac x = "a div gcd a b" in exI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   888
  apply (rule_tac x = "b div gcd a b" in exI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   889
  apply (insert nz, auto simp add: dvd_div_mult gcd_0_left  gcd_zero intro: div_gcd_coprime)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   890
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   891
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   892
lemma coprime_exp:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   893
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   894
  by (induct n, simp_all add: coprime_mult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   895
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   896
lemma coprime_exp2 [intro]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   897
  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   898
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   899
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   900
  apply (rule coprime_exp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   901
  apply (subst gcd.commute)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   902
  apply assumption
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   903
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   904
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   905
lemma gcd_exp:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   906
  "gcd (a^n) (b^n) = (gcd a b) ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   907
proof (cases "a = 0 \<and> b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   908
  assume "a = 0 \<and> b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   909
  then show ?thesis by (cases n, simp_all add: gcd_0_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   910
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   911
  assume A: "\<not>(a = 0 \<and> b = 0)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   912
  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
   913
    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   914
  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   915
  also note gcd_mult_distrib
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   916
  also have "normalisation_factor ((gcd a b)^n) = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   917
    by (simp add: normalisation_factor_pow A)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   918
  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
   919
    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
   920
  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
   921
    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
   922
  finally show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   923
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   924
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   925
lemma coprime_common_divisor: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   926
  "gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   927
  apply (subgoal_tac "x dvd gcd a b")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   928
  apply (simp add: is_unit_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   929
  apply (erule (1) gcd_greatest)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   930
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   931
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   932
lemma division_decomp: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   933
  assumes dc: "a dvd b * c"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   934
  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
   935
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   936
  assume "gcd a b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   937
  hence "a = 0 \<and> b = 0" by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   938
  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
   939
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   940
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   941
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   942
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   943
  from gcd_coprime_exists[OF this]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   944
    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
   945
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   946
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   947
  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
   948
  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
   949
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   950
  with `?d \<noteq> 0` have "a' dvd b' * c" by (rule dvd_mult_cancel_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   951
  with coprime_dvd_mult[OF ab'(3)] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   952
    have "a' dvd c" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   953
  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
   954
  then show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   955
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   956
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   957
lemma pow_divides_pow:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   958
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   959
  shows "a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   960
proof (cases "gcd a b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   961
  assume "gcd a b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   962
  then show ?thesis by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   963
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   964
  let ?d = "gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   965
  assume "?d \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   966
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   967
  from `?d \<noteq> 0` have zn: "?d ^ n \<noteq> 0" by (rule nonzero_pow_nonzero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   968
  from gcd_coprime_exists[OF `?d \<noteq> 0`]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   969
    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
   970
    by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   971
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   972
    by (simp add: ab'(1,2)[symmetric])
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   973
  hence "?d^n * a'^n dvd ?d^n * b'^n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   974
    by (simp only: power_mult_distrib ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   975
  with zn have "a'^n dvd b'^n" by (rule dvd_mult_cancel_left)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   976
  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
   977
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   978
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   979
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   980
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   981
  with ab'(1,2) show ?thesis by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   982
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   983
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   984
lemma pow_divides_eq [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   985
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   986
  by (auto intro: pow_divides_pow dvd_power_same)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   987
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   988
lemma divides_mult:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   989
  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
   990
  shows "m * n dvd r"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   991
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   992
  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
   993
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   994
  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
   995
  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
   996
  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
   997
  with n' have "r = m * n * k" by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   998
  then show ?thesis unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
   999
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1000
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1001
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1002
  by (subst add_commute, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1003
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1004
lemma setprod_coprime [rule_format]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1005
  "(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1006
  apply (cases "finite A")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1007
  apply (induct set: finite)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1008
  apply (auto simp add: gcd_mult_cancel)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1009
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1010
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1011
lemma coprime_divisors: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1012
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1013
  shows "gcd d e = 1" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1014
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1015
  from assms obtain k l where "a = d * k" "b = e * l"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1016
    unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1017
  with assms have "gcd (d * k) (e * l) = 1" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1018
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1019
  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
  1020
  finally have "gcd e d = 1" by (rule coprime_lmult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1021
  then show ?thesis by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1022
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1024
lemma invertible_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1025
  "x * y mod m = 1 \<Longrightarrow> gcd x m = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1026
  by (metis coprime_lmult gcd_1 ac_simps gcd_red)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1027
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1028
lemma lcm_gcd:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1029
  "lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1030
  by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1031
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1032
lemma lcm_gcd_prod:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1033
  "lcm a b * gcd a b = a * b div normalisation_factor (a*b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1034
proof (cases "a * b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1035
  let ?nf = normalisation_factor
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1036
  assume "a * b \<noteq> 0"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1037
  hence "gcd a b \<noteq> 0" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1038
  from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1039
    by (simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1040
  also from `a * b \<noteq> 0` have "... = a * b div ?nf (a*b)" 
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1041
    by (simp_all add: unit_ring_inv'1 unit_ring_inv)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1042
  finally show ?thesis .
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1043
qed (auto simp add: lcm_gcd)
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_dvd1 [iff]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1046
  "x dvd lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1047
proof (cases "x*y = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1048
  assume "x * y \<noteq> 0"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1049
  hence "gcd x y \<noteq> 0" by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1050
  let ?c = "ring_inv (normalisation_factor (x*y))"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1051
  from `x * y \<noteq> 0` have [simp]: "is_unit (normalisation_factor (x*y))" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1052
  from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1053
    by (simp add: mult_ac unit_ring_inv)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1054
  hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1055
  with `gcd x y \<noteq> 0` have "lcm x y = x * ?c * y div gcd x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1056
    by (subst (asm) div_mult_self2_is_id, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1057
  also have "... = x * (?c * y div gcd x y)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1058
    by (metis div_mult_swap gcd_dvd2 mult_assoc)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1059
  finally show ?thesis by (rule dvdI)
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1060
qed (auto simp add: lcm_gcd)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1061
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1062
lemma lcm_least:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1063
  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1064
proof (cases "k = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1065
  let ?nf = normalisation_factor
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1066
  assume "k \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1067
  hence "is_unit (?nf k)" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1068
  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1069
  assume A: "a dvd k" "b dvd k"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1070
  hence "gcd a b \<noteq> 0" using `k \<noteq> 0` by auto
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1071
  from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1072
    unfolding dvd_def by blast
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1073
  with `k \<noteq> 0` have "r * s \<noteq> 0"
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1074
    by auto (drule sym [of 0], simp)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1075
  hence "is_unit (?nf (r * s))" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1076
  let ?c = "?nf k div ?nf (r*s)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1077
  from `is_unit (?nf k)` and `is_unit (?nf (r * s))` have "is_unit ?c" by (rule unit_div)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1078
  hence "?c \<noteq> 0" using not_is_unit_0 by fast 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1079
  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1080
    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1081
  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1082
    by (subst (3) `k = a * r`, subst (3) `k = b * s`, simp add: algebra_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1083
  also have "... = ?c * r*s * k * gcd a b" using `r * s \<noteq> 0`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1084
    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1085
  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1086
    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1087
  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1088
    by (simp add: algebra_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1089
  hence "?c * k * gcd a b = a * b * gcd s r" using `r * s \<noteq> 0`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1090
    by (metis div_mult_self2_is_id)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1091
  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1092
    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1093
  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1094
    by (simp add: algebra_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1095
  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using `gcd a b \<noteq> 0`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1096
    by (metis mult.commute div_mult_self2_is_id)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1097
  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using `?c \<noteq> 0`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1098
    by (metis div_mult_self2_is_id mult_assoc) 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1099
  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using `is_unit ?c`
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1100
    by (simp add: unit_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1101
  finally show ?thesis by (rule dvdI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1102
qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1103
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1104
lemma lcm_zero:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1105
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1106
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1107
  let ?nf = normalisation_factor
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
    assume "a \<noteq> 0" "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1110
    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1111
    moreover from `a \<noteq> 0` and `b \<noteq> 0` have "gcd a b \<noteq> 0" by (simp add: gcd_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1112
    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1113
  } moreover {
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1114
    assume "a = 0 \<or> b = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1115
    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1116
  }
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1117
  ultimately show ?thesis by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1118
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1119
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1120
lemmas lcm_0_iff = lcm_zero
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1121
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1122
lemma gcd_lcm: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1123
  assumes "lcm a b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1124
  shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1125
proof-
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1126
  from assms have "gcd a b \<noteq> 0" by (simp add: gcd_zero lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1127
  let ?c = "normalisation_factor (a*b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1128
  from `lcm a b \<noteq> 0` have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1129
  hence "is_unit ?c" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1130
  from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1131
    by (subst (2) div_mult_self2_is_id[OF `lcm a b \<noteq> 0`, symmetric], simp add: mult_ac)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1132
  also from `is_unit ?c` have "... = a * b div (?c * lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1133
    by (simp only: unit_ring_inv'1 unit_ring_inv)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1134
  finally show ?thesis by (simp only: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1135
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1136
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1137
lemma normalisation_factor_lcm [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1138
  "normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1139
proof (cases "a = 0 \<or> b = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1140
  case True then show ?thesis
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1141
    by (auto simp add: lcm_gcd) 
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1142
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1143
  case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1144
  let ?nf = normalisation_factor
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1145
  from lcm_gcd_prod[of a b] 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1146
    have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1147
    by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1148
  also have "... = (if a*b = 0 then 0 else 1)"
58953
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1149
    by simp
2e19b392d9e3 self-contained simp rules for dvd on numerals
haftmann
parents: 58889
diff changeset
  1150
  finally show ?thesis using False by simp
58023
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1151
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1152
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1153
lemma lcm_dvd2 [iff]: "y dvd lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1154
  using lcm_dvd1 [of y x] by (simp add: lcm_gcd 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 lcmI:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1157
  "\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1158
    normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1159
  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1160
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1161
sublocale lcm!: abel_semigroup lcm
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1162
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1163
  fix x y z
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1164
  show "lcm (lcm x y) z = lcm x (lcm y z)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1165
  proof (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1166
    have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1167
    then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1168
    
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1169
    have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1170
    hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1171
    moreover have "z dvd lcm (lcm x y) z" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1172
    ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1173
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1174
    fix l assume "x dvd l" and "lcm y z dvd l"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1175
    have "y dvd lcm y z" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1176
    from this and `lcm y z dvd l` have "y dvd l" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1177
    have "z dvd lcm y z" by simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1178
    from this and `lcm y z dvd l` have "z dvd l" by (rule dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1179
    from `x dvd l` and `y dvd l` have "lcm x y dvd l" by (rule lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1180
    from this and `z dvd l` show "lcm (lcm x y) z dvd l" by (rule lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1181
  qed (simp add: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1182
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1183
  fix x y
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1184
  show "lcm x y = lcm y x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1185
    by (simp add: lcm_gcd ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1186
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1187
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1188
lemma dvd_lcm_D1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1189
  "lcm m n dvd k \<Longrightarrow> m dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1190
  by (rule dvd_trans, rule lcm_dvd1, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1191
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1192
lemma dvd_lcm_D2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1193
  "lcm m n dvd k \<Longrightarrow> n dvd k"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1194
  by (rule dvd_trans, rule lcm_dvd2, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1195
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1196
lemma gcd_dvd_lcm [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1197
  "gcd a b dvd lcm a b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1198
  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1199
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1200
lemma lcm_1_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1201
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1202
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1203
  assume "lcm a b = 1"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1204
  then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1205
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1206
  assume "is_unit a \<and> is_unit b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1207
  hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1208
  hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1209
  hence "lcm a b = normalisation_factor (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1210
    by (subst normalisation_factor_unit, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1211
  also have "\<dots> = 1" using `is_unit a \<and> is_unit b` by (auto simp add: is_unit_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1212
  finally show "lcm a b = 1" .
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1213
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1214
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1215
lemma lcm_0_left [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1216
  "lcm 0 x = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1217
  by (rule sym, rule lcmI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1218
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1219
lemma lcm_0 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1220
  "lcm x 0 = 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1221
  by (rule sym, rule lcmI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1222
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1223
lemma lcm_unique:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1224
  "a dvd d \<and> b dvd d \<and> 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1225
  normalisation_factor d = (if d = 0 then 0 else 1) \<and>
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1226
  (\<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
  1227
  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
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 dvd_lcm_I1 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1230
  "k dvd m \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1231
  by (metis lcm_dvd1 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1232
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1233
lemma dvd_lcm_I2 [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1234
  "k dvd n \<Longrightarrow> k dvd lcm m n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1235
  by (metis lcm_dvd2 dvd_trans)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1236
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1237
lemma lcm_1_left [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1238
  "lcm 1 x = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1239
  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1240
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1241
lemma lcm_1_right [simp]:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1242
  "lcm x 1 = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1243
  by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1244
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1245
lemma lcm_coprime:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1246
  "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1247
  by (subst lcm_gcd) simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1248
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1249
lemma lcm_proj1_if_dvd: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1250
  "y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1251
  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1252
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1253
lemma lcm_proj2_if_dvd: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1254
  "x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1255
  using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1256
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1257
lemma lcm_proj1_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1258
  "lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1259
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1260
  assume A: "lcm m n = m div normalisation_factor m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1261
  show "n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1262
  proof (cases "m = 0")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1263
    assume [simp]: "m \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1264
    from A have B: "m = lcm m n * normalisation_factor m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1265
      by (simp add: unit_eq_div2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1266
    show ?thesis by (subst B, simp)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1267
  qed simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1268
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1269
  assume "n dvd m"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1270
  then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1271
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1272
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1273
lemma lcm_proj2_iff:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1274
  "lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1275
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1276
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1277
lemma euclidean_size_lcm_le1: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1278
  assumes "a \<noteq> 0" and "b \<noteq> 0"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1279
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1280
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1281
  have "a dvd lcm a b" by (rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1282
  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1283
  with `a \<noteq> 0` and `b \<noteq> 0` have "c \<noteq> 0" by (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1284
  then show ?thesis by (subst A, intro size_mult_mono)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1285
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1286
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1287
lemma euclidean_size_lcm_le2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1288
  "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
  1289
  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
  1290
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1291
lemma euclidean_size_lcm_less1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1292
  assumes "b \<noteq> 0" and "\<not>b dvd a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1293
  shows "euclidean_size a < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1294
proof (rule ccontr)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1295
  from assms have "a \<noteq> 0" by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1296
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1297
  with `a \<noteq> 0` and `b \<noteq> 0` have "euclidean_size (lcm a b) = euclidean_size a"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1298
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1299
  with assms have "lcm a b dvd a" 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1300
    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
  1301
  hence "b dvd a" by (rule dvd_lcm_D2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1302
  with `\<not>b dvd a` show False by contradiction
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1303
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1304
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1305
lemma euclidean_size_lcm_less2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1306
  assumes "a \<noteq> 0" and "\<not>a dvd b"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1307
  shows "euclidean_size b < euclidean_size (lcm a b)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1308
  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
  1309
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1310
lemma lcm_mult_unit1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1311
  "is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1312
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1313
  apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1314
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1315
  apply (rule lcm_least, simp add: unit_simps, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1316
  apply (subst normalisation_factor_lcm, simp add: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1317
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1318
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1319
lemma lcm_mult_unit2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1320
  "is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1321
  using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1322
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1323
lemma lcm_div_unit1:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1324
  "is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1325
  by (simp add: unit_ring_inv lcm_mult_unit1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1326
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1327
lemma lcm_div_unit2:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1328
  "is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1329
  by (simp add: unit_ring_inv lcm_mult_unit2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1330
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1331
lemma lcm_left_idem:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1332
  "lcm p (lcm p q) = lcm p q"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1333
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1334
  apply simp
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1335
  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1336
  apply (rule lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1337
  apply (erule (1) lcm_least)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1338
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1339
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1340
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1341
lemma lcm_right_idem:
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1342
  "lcm (lcm p q) q = lcm p q"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1343
  apply (rule lcmI)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1344
  apply (subst lcm.assoc, rule lcm_dvd1)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1345
  apply (rule lcm_dvd2)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1346
  apply (rule lcm_least, erule (1) lcm_least, assumption)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1347
  apply (auto simp: lcm_zero)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1348
  done
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1349
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1350
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1351
proof
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1352
  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
  1353
    by (simp add: fun_eq_iff ac_simps)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1354
next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1355
  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
  1356
    by (intro ext, simp add: lcm_left_idem)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1357
qed
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1358
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1359
lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1360
  and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1361
  and normalisation_factor_Lcm [simp]: 
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1362
          "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1363
proof -
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1364
  have "(\<forall>x\<in>A. x dvd Lcm A) \<and> (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> Lcm A dvd l') \<and>
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1365
    normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1366
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>x\<in>A. x dvd l)")
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1367
    case False
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1368
    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
  1369
    with False show ?thesis by auto
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1370
  next
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1371
    case True
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1372
    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1373
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1374
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1375
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
diff changeset
  1376
      apply (subst n_def)
62826b36ac5e generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents: