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