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