src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Sat Jun 27 20:20:34 2015 +0200 (2015-06-27)
changeset 60598 78ca5674c66a
parent 60582 d694f217ee41
child 60599 f8bb070dc98b
permissions -rw-r--r--
rings follow immediately their corresponding semirings
haftmann@58023
     1
(* Author: Manuel Eberl *)
haftmann@58023
     2
wenzelm@60526
     3
section \<open>Abstract euclidean algorithm\<close>
haftmann@58023
     4
haftmann@58023
     5
theory Euclidean_Algorithm
haftmann@60571
     6
imports Complex_Main "~~/src/HOL/Library/Polynomial"
haftmann@58023
     7
begin
haftmann@60433
     8
  
wenzelm@60526
     9
text \<open>
haftmann@58023
    10
  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
haftmann@58023
    11
  implemented. It must provide:
haftmann@58023
    12
  \begin{itemize}
haftmann@58023
    13
  \item division with remainder
haftmann@58023
    14
  \item a size function such that @{term "size (a mod b) < size b"} 
haftmann@58023
    15
        for any @{term "b \<noteq> 0"}
haftmann@60438
    16
  \item a normalization factor such that two associated numbers are equal iff 
haftmann@60438
    17
        they are the same when divd by their normalization factors.
haftmann@58023
    18
  \end{itemize}
haftmann@58023
    19
  The existence of these functions makes it possible to derive gcd and lcm functions 
haftmann@58023
    20
  for any Euclidean semiring.
wenzelm@60526
    21
\<close> 
haftmann@58023
    22
class euclidean_semiring = semiring_div + 
haftmann@58023
    23
  fixes euclidean_size :: "'a \<Rightarrow> nat"
haftmann@60438
    24
  fixes normalization_factor :: "'a \<Rightarrow> 'a"
haftmann@60569
    25
  assumes mod_size_less: 
haftmann@60569
    26
    "b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
haftmann@58023
    27
  assumes size_mult_mono:
haftmann@58023
    28
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"
haftmann@60438
    29
  assumes normalization_factor_is_unit [intro,simp]: 
haftmann@60438
    30
    "a \<noteq> 0 \<Longrightarrow> is_unit (normalization_factor a)"
haftmann@60438
    31
  assumes normalization_factor_mult: "normalization_factor (a * b) = 
haftmann@60438
    32
    normalization_factor a * normalization_factor b"
haftmann@60438
    33
  assumes normalization_factor_unit: "is_unit a \<Longrightarrow> normalization_factor a = a"
haftmann@60438
    34
  assumes normalization_factor_0 [simp]: "normalization_factor 0 = 0"
haftmann@58023
    35
begin
haftmann@58023
    36
haftmann@60438
    37
lemma normalization_factor_dvd [simp]:
haftmann@60438
    38
  "a \<noteq> 0 \<Longrightarrow> normalization_factor a dvd b"
haftmann@58023
    39
  by (rule unit_imp_dvd, simp)
haftmann@58023
    40
    
haftmann@60438
    41
lemma normalization_factor_1 [simp]:
haftmann@60438
    42
  "normalization_factor 1 = 1"
haftmann@60438
    43
  by (simp add: normalization_factor_unit)
haftmann@58023
    44
haftmann@60438
    45
lemma normalization_factor_0_iff [simp]:
haftmann@60438
    46
  "normalization_factor a = 0 \<longleftrightarrow> a = 0"
haftmann@58023
    47
proof
haftmann@60438
    48
  assume "normalization_factor a = 0"
haftmann@60438
    49
  hence "\<not> is_unit (normalization_factor a)"
haftmann@60433
    50
    by simp
haftmann@60433
    51
  then show "a = 0" by auto
haftmann@60433
    52
qed simp
haftmann@58023
    53
haftmann@60438
    54
lemma normalization_factor_pow:
haftmann@60438
    55
  "normalization_factor (a ^ n) = normalization_factor a ^ n"
haftmann@60438
    56
  by (induct n) (simp_all add: normalization_factor_mult power_Suc2)
haftmann@58023
    57
haftmann@60438
    58
lemma normalization_correct [simp]:
haftmann@60438
    59
  "normalization_factor (a div normalization_factor a) = (if a = 0 then 0 else 1)"
haftmann@60430
    60
proof (cases "a = 0", simp)
haftmann@60430
    61
  assume "a \<noteq> 0"
haftmann@60438
    62
  let ?nf = "normalization_factor"
wenzelm@60526
    63
  from normalization_factor_is_unit[OF \<open>a \<noteq> 0\<close>] have "?nf a \<noteq> 0"
haftmann@60433
    64
    by auto
haftmann@60430
    65
  have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)" 
haftmann@60438
    66
    by (simp add: normalization_factor_mult)
wenzelm@60526
    67
  also have "a div ?nf a * ?nf a = a" using \<open>a \<noteq> 0\<close>
haftmann@59009
    68
    by simp
wenzelm@60526
    69
  also have "?nf (?nf a) = ?nf a" using \<open>a \<noteq> 0\<close> 
haftmann@60438
    70
    normalization_factor_is_unit normalization_factor_unit by simp
haftmann@60438
    71
  finally have "normalization_factor (a div normalization_factor a) = 1"  
wenzelm@60526
    72
    using \<open>?nf a \<noteq> 0\<close> by (metis div_mult_self2_is_id div_self)
wenzelm@60526
    73
  with \<open>a \<noteq> 0\<close> show ?thesis by simp
haftmann@58023
    74
qed
haftmann@58023
    75
haftmann@60438
    76
lemma normalization_0_iff [simp]:
haftmann@60438
    77
  "a div normalization_factor a = 0 \<longleftrightarrow> a = 0"
haftmann@60430
    78
  by (cases "a = 0", simp, subst unit_eq_div1, blast, simp)
haftmann@58023
    79
haftmann@60438
    80
lemma mult_div_normalization [simp]:
haftmann@60438
    81
  "b * (1 div normalization_factor a) = b div normalization_factor a"
haftmann@60433
    82
  by (cases "a = 0") simp_all
haftmann@60433
    83
haftmann@58023
    84
lemma associated_iff_normed_eq:
haftmann@60438
    85
  "associated a b \<longleftrightarrow> a div normalization_factor a = b div normalization_factor b"
haftmann@60438
    86
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalization_0_iff, rule iffI)
haftmann@60438
    87
  let ?nf = normalization_factor
haftmann@58023
    88
  assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"
haftmann@58023
    89
  hence "a = b * (?nf a div ?nf b)"
haftmann@58023
    90
    apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)
haftmann@58023
    91
    apply (subst div_mult_swap, simp, simp)
haftmann@58023
    92
    done
wenzelm@60526
    93
  with \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> have "\<exists>c. is_unit c \<and> a = c * b"
haftmann@58023
    94
    by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)
haftmann@60436
    95
  then obtain c where "is_unit c" and "a = c * b" by blast
haftmann@60436
    96
  then show "associated a b" by (rule is_unit_associatedI) 
haftmann@58023
    97
next
haftmann@60438
    98
  let ?nf = normalization_factor
haftmann@58023
    99
  assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"
haftmann@60436
   100
  then obtain c where "is_unit c" and "a = c * b" by (blast elim: associated_is_unitE)
haftmann@58023
   101
  then show "a div ?nf a = b div ?nf b"
wenzelm@60526
   102
    apply (simp only: \<open>a = c * b\<close> normalization_factor_mult normalization_factor_unit)
haftmann@58023
   103
    apply (rule div_mult_mult1, force)
haftmann@58023
   104
    done
haftmann@58023
   105
  qed
haftmann@58023
   106
haftmann@58023
   107
lemma normed_associated_imp_eq:
haftmann@60438
   108
  "associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b"
haftmann@58023
   109
  by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
haftmann@60569
   110
haftmann@60569
   111
lemma normed_dvd [iff]:
haftmann@60569
   112
  "a div normalization_factor a dvd a"
haftmann@60569
   113
proof (cases "a = 0")
haftmann@60569
   114
  case True then show ?thesis by simp
haftmann@60569
   115
next
haftmann@60569
   116
  case False
haftmann@60569
   117
  then have "a = a div normalization_factor a * normalization_factor a"
haftmann@60569
   118
    by (auto intro: unit_div_mult_self)
haftmann@60569
   119
  then show ?thesis ..
haftmann@60569
   120
qed
haftmann@60569
   121
haftmann@60569
   122
lemma dvd_normed [iff]:
haftmann@60569
   123
  "a dvd a div normalization_factor a"
haftmann@60569
   124
proof (cases "a = 0")
haftmann@60569
   125
  case True then show ?thesis by simp
haftmann@60569
   126
next
haftmann@60569
   127
  case False
haftmann@60569
   128
  then have "a div normalization_factor a = a * (1 div normalization_factor a)"
haftmann@60569
   129
    by (auto intro: unit_mult_div_div)
haftmann@60569
   130
  then show ?thesis ..
haftmann@60569
   131
qed
haftmann@60569
   132
haftmann@60569
   133
lemma associated_normed:
haftmann@60569
   134
  "associated (a div normalization_factor a) a"
haftmann@60569
   135
  by (rule associatedI) simp_all
haftmann@60569
   136
haftmann@60569
   137
lemma normalization_factor_dvd' [simp]:
haftmann@60569
   138
  "normalization_factor a dvd a"
haftmann@60569
   139
  by (cases "a = 0", simp_all)
haftmann@60569
   140
haftmann@60438
   141
lemmas normalization_factor_dvd_iff [simp] =
haftmann@60438
   142
  unit_dvd_iff [OF normalization_factor_is_unit]
haftmann@58023
   143
haftmann@58023
   144
lemma euclidean_division:
haftmann@58023
   145
  fixes a :: 'a and b :: 'a
haftmann@60569
   146
  assumes "b \<noteq> 0" and "\<not> b dvd a"
haftmann@58023
   147
  obtains s and t where "a = s * b + t" 
haftmann@58023
   148
    and "euclidean_size t < euclidean_size b"
haftmann@58023
   149
proof -
haftmann@60569
   150
  from div_mod_equality [of a b 0] 
haftmann@58023
   151
     have "a = a div b * b + a mod b" by simp
haftmann@60569
   152
  with that and assms show ?thesis by (auto simp add: mod_size_less)
haftmann@58023
   153
qed
haftmann@58023
   154
haftmann@58023
   155
lemma dvd_euclidean_size_eq_imp_dvd:
haftmann@58023
   156
  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
haftmann@58023
   157
  shows "a dvd b"
haftmann@60569
   158
proof (rule ccontr)
haftmann@60569
   159
  assume "\<not> a dvd b"
haftmann@60569
   160
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
haftmann@58023
   161
  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
haftmann@58023
   162
  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
wenzelm@60526
   163
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
wenzelm@60526
   164
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
haftmann@58023
   165
      using size_mult_mono by force
haftmann@60569
   166
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
haftmann@60569
   167
  have "euclidean_size (b mod a) < euclidean_size a"
haftmann@58023
   168
      using mod_size_less by blast
haftmann@58023
   169
  ultimately show False using size_eq by simp
haftmann@58023
   170
qed
haftmann@58023
   171
haftmann@58023
   172
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   173
where
haftmann@60569
   174
  "gcd_eucl a b = (if b = 0 then a div normalization_factor a
haftmann@60569
   175
    else if b dvd a then b div normalization_factor b
haftmann@60569
   176
    else gcd_eucl b (a mod b))"
haftmann@60572
   177
  by pat_completeness simp
haftmann@60569
   178
termination
haftmann@60569
   179
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@58023
   180
haftmann@58023
   181
declare gcd_eucl.simps [simp del]
haftmann@58023
   182
haftmann@60569
   183
lemma gcd_eucl_induct [case_names zero mod]:
haftmann@60569
   184
  assumes H1: "\<And>b. P b 0"
haftmann@60569
   185
  and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
haftmann@60569
   186
  shows "P a b"
haftmann@58023
   187
proof (induct a b rule: gcd_eucl.induct)
haftmann@60569
   188
  case ("1" a b)
haftmann@60569
   189
  show ?case
haftmann@60569
   190
  proof (cases "b = 0")
haftmann@60569
   191
    case True then show "P a b" by simp (rule H1)
haftmann@60569
   192
  next
haftmann@60569
   193
    case False
haftmann@60569
   194
    have "P b (a mod b)"
haftmann@60569
   195
    proof (cases "b dvd a")
haftmann@60569
   196
      case False with \<open>b \<noteq> 0\<close> show "P b (a mod b)"
haftmann@60569
   197
        by (rule "1.hyps")
haftmann@60569
   198
    next
haftmann@60569
   199
      case True then have "a mod b = 0"
haftmann@60569
   200
        by (simp add: mod_eq_0_iff_dvd)
haftmann@60569
   201
      then show "P b (a mod b)" by simp (rule H1)
haftmann@60569
   202
    qed
haftmann@60569
   203
    with \<open>b \<noteq> 0\<close> show "P a b"
haftmann@60569
   204
      by (blast intro: H2)
haftmann@60569
   205
  qed
haftmann@58023
   206
qed
haftmann@58023
   207
haftmann@58023
   208
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
haftmann@58023
   209
where
haftmann@60438
   210
  "lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))"
haftmann@58023
   211
haftmann@60572
   212
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
haftmann@60572
   213
  Somewhat complicated definition of Lcm that has the advantage of working
haftmann@60572
   214
  for infinite sets as well\<close>
haftmann@58023
   215
where
haftmann@60430
   216
  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
haftmann@60430
   217
     let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
haftmann@60430
   218
       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
haftmann@60438
   219
       in l div normalization_factor l
haftmann@58023
   220
      else 0)"
haftmann@58023
   221
haftmann@58023
   222
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
haftmann@58023
   223
where
haftmann@58023
   224
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
   225
haftmann@60572
   226
lemma gcd_eucl_0:
haftmann@60572
   227
  "gcd_eucl a 0 = a div normalization_factor a"
haftmann@60572
   228
  by (simp add: gcd_eucl.simps [of a 0])
haftmann@60572
   229
haftmann@60572
   230
lemma gcd_eucl_0_left:
haftmann@60572
   231
  "gcd_eucl 0 a = a div normalization_factor a"
haftmann@60572
   232
  by (simp add: gcd_eucl.simps [of 0 a])
haftmann@60572
   233
haftmann@60572
   234
lemma gcd_eucl_non_0:
haftmann@60572
   235
  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
haftmann@60572
   236
  by (cases "b dvd a")
haftmann@60572
   237
    (simp_all add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
haftmann@60572
   238
haftmann@60572
   239
lemma gcd_eucl_code [code]:
haftmann@60572
   240
  "gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))"
haftmann@60572
   241
  by (auto simp add: gcd_eucl_non_0 gcd_eucl_0 gcd_eucl_0_left) 
haftmann@60572
   242
haftmann@58023
   243
end
haftmann@58023
   244
haftmann@60598
   245
class euclidean_ring = euclidean_semiring + idom
haftmann@60598
   246
begin
haftmann@60598
   247
haftmann@60598
   248
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
haftmann@60598
   249
  "euclid_ext a b = 
haftmann@60598
   250
     (if b = 0 then 
haftmann@60598
   251
        let c = 1 div normalization_factor a in (c, 0, a * c)
haftmann@60598
   252
      else if b dvd a then
haftmann@60598
   253
        let c = 1 div normalization_factor b in (0, c, b * c)
haftmann@60598
   254
      else
haftmann@60598
   255
        case euclid_ext b (a mod b) of
haftmann@60598
   256
            (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60598
   257
  by pat_completeness simp
haftmann@60598
   258
termination
haftmann@60598
   259
  by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
haftmann@60598
   260
haftmann@60598
   261
declare euclid_ext.simps [simp del]
haftmann@60598
   262
haftmann@60598
   263
lemma euclid_ext_0: 
haftmann@60598
   264
  "euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)"
haftmann@60598
   265
  by (simp add: euclid_ext.simps [of a 0])
haftmann@60598
   266
haftmann@60598
   267
lemma euclid_ext_left_0: 
haftmann@60598
   268
  "euclid_ext 0 a = (0, 1 div normalization_factor a, a div normalization_factor a)"
haftmann@60598
   269
  by (simp add: euclid_ext.simps [of 0 a])
haftmann@60598
   270
haftmann@60598
   271
lemma euclid_ext_non_0: 
haftmann@60598
   272
  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
haftmann@60598
   273
    (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
haftmann@60598
   274
  by (cases "b dvd a")
haftmann@60598
   275
    (simp_all add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   276
haftmann@60598
   277
lemma euclid_ext_code [code]:
haftmann@60598
   278
  "euclid_ext a b = (if b = 0 then (1 div normalization_factor a, 0, a div normalization_factor a)
haftmann@60598
   279
    else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
haftmann@60598
   280
  by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
haftmann@60598
   281
haftmann@60598
   282
lemma euclid_ext_correct:
haftmann@60598
   283
  "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
haftmann@60598
   284
proof (induct a b rule: gcd_eucl_induct)
haftmann@60598
   285
  case (zero a) then show ?case
haftmann@60598
   286
    by (simp add: euclid_ext_0 ac_simps)
haftmann@60598
   287
next
haftmann@60598
   288
  case (mod a b)
haftmann@60598
   289
  obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
haftmann@60598
   290
    by (cases "euclid_ext b (a mod b)") blast
haftmann@60598
   291
  with mod have "c = s * b + t * (a mod b)" by simp
haftmann@60598
   292
  also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
haftmann@60598
   293
    by (simp add: algebra_simps) 
haftmann@60598
   294
  also have "(a div b) * b + a mod b = a" using mod_div_equality .
haftmann@60598
   295
  finally show ?case
haftmann@60598
   296
    by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
haftmann@60598
   297
qed
haftmann@60598
   298
haftmann@60598
   299
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@60598
   300
where
haftmann@60598
   301
  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
haftmann@60598
   302
haftmann@60598
   303
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div normalization_factor a, 0)" 
haftmann@60598
   304
  by (simp add: euclid_ext'_def euclid_ext_0)
haftmann@60598
   305
haftmann@60598
   306
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div normalization_factor a)" 
haftmann@60598
   307
  by (simp add: euclid_ext'_def euclid_ext_left_0)
haftmann@60598
   308
  
haftmann@60598
   309
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
haftmann@60598
   310
  fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
haftmann@60598
   311
  by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
haftmann@60598
   312
haftmann@60598
   313
end
haftmann@60598
   314
haftmann@58023
   315
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
haftmann@58023
   316
  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
haftmann@58023
   317
  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
haftmann@58023
   318
begin
haftmann@58023
   319
haftmann@58023
   320
lemma gcd_0_left:
haftmann@60438
   321
  "gcd 0 a = a div normalization_factor a"
haftmann@60572
   322
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
haftmann@58023
   323
haftmann@58023
   324
lemma gcd_0:
haftmann@60438
   325
  "gcd a 0 = a div normalization_factor a"
haftmann@60572
   326
  unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
haftmann@58023
   327
haftmann@58023
   328
lemma gcd_non_0:
haftmann@60430
   329
  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
haftmann@60572
   330
  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
haftmann@58023
   331
haftmann@60430
   332
lemma gcd_dvd1 [iff]: "gcd a b dvd a"
haftmann@60430
   333
  and gcd_dvd2 [iff]: "gcd a b dvd b"
haftmann@60569
   334
  by (induct a b rule: gcd_eucl_induct)
haftmann@60569
   335
    (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
haftmann@60569
   336
    
haftmann@58023
   337
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
haftmann@58023
   338
  by (rule dvd_trans, assumption, rule gcd_dvd1)
haftmann@58023
   339
haftmann@58023
   340
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
haftmann@58023
   341
  by (rule dvd_trans, assumption, rule gcd_dvd2)
haftmann@58023
   342
haftmann@58023
   343
lemma gcd_greatest:
haftmann@60430
   344
  fixes k a b :: 'a
haftmann@60430
   345
  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
haftmann@60569
   346
proof (induct a b rule: gcd_eucl_induct)
haftmann@60569
   347
  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
haftmann@60569
   348
next
haftmann@60569
   349
  case (mod a b)
haftmann@60569
   350
  then show ?case
haftmann@60569
   351
    by (simp add: gcd_non_0 dvd_mod_iff)
haftmann@58023
   352
qed
haftmann@58023
   353
haftmann@58023
   354
lemma dvd_gcd_iff:
haftmann@60430
   355
  "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
haftmann@58023
   356
  by (blast intro!: gcd_greatest intro: dvd_trans)
haftmann@58023
   357
haftmann@58023
   358
lemmas gcd_greatest_iff = dvd_gcd_iff
haftmann@58023
   359
haftmann@58023
   360
lemma gcd_zero [simp]:
haftmann@60430
   361
  "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
haftmann@58023
   362
  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
haftmann@58023
   363
haftmann@60438
   364
lemma normalization_factor_gcd [simp]:
haftmann@60438
   365
  "normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
haftmann@60569
   366
  by (induct a b rule: gcd_eucl_induct)
haftmann@60569
   367
    (auto simp add: gcd_0 gcd_non_0)
haftmann@58023
   368
haftmann@58023
   369
lemma gcdI:
haftmann@60430
   370
  "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k)
haftmann@60438
   371
    \<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b"
haftmann@58023
   372
  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
haftmann@58023
   373
haftmann@58023
   374
sublocale gcd!: abel_semigroup gcd
haftmann@58023
   375
proof
haftmann@60430
   376
  fix a b c 
haftmann@60430
   377
  show "gcd (gcd a b) c = gcd a (gcd b c)"
haftmann@58023
   378
  proof (rule gcdI)
haftmann@60430
   379
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
haftmann@60430
   380
    then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
haftmann@60430
   381
    have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
haftmann@60430
   382
    hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
haftmann@60430
   383
    moreover have "gcd (gcd a b) c dvd c" by simp
haftmann@60430
   384
    ultimately show "gcd (gcd a b) c dvd gcd b c"
haftmann@58023
   385
      by (rule gcd_greatest)
haftmann@60438
   386
    show "normalization_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
haftmann@58023
   387
      by auto
haftmann@60430
   388
    fix l assume "l dvd a" and "l dvd gcd b c"
haftmann@58023
   389
    with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
haftmann@60430
   390
      have "l dvd b" and "l dvd c" by blast+
wenzelm@60526
   391
    with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
haftmann@58023
   392
      by (intro gcd_greatest)
haftmann@58023
   393
  qed
haftmann@58023
   394
next
haftmann@60430
   395
  fix a b
haftmann@60430
   396
  show "gcd a b = gcd b a"
haftmann@58023
   397
    by (rule gcdI) (simp_all add: gcd_greatest)
haftmann@58023
   398
qed
haftmann@58023
   399
haftmann@58023
   400
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
haftmann@60438
   401
    normalization_factor d = (if d = 0 then 0 else 1) \<and>
haftmann@58023
   402
    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
haftmann@58023
   403
  by (rule, auto intro: gcdI simp: gcd_greatest)
haftmann@58023
   404
haftmann@58023
   405
lemma gcd_dvd_prod: "gcd a b dvd k * b"
haftmann@58023
   406
  using mult_dvd_mono [of 1] by auto
haftmann@58023
   407
haftmann@60430
   408
lemma gcd_1_left [simp]: "gcd 1 a = 1"
haftmann@58023
   409
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   410
haftmann@60430
   411
lemma gcd_1 [simp]: "gcd a 1 = 1"
haftmann@58023
   412
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   413
haftmann@58023
   414
lemma gcd_proj2_if_dvd: 
haftmann@60438
   415
  "b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b"
haftmann@60430
   416
  by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
haftmann@58023
   417
haftmann@58023
   418
lemma gcd_proj1_if_dvd: 
haftmann@60438
   419
  "a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a"
haftmann@58023
   420
  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
haftmann@58023
   421
haftmann@60438
   422
lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n"
haftmann@58023
   423
proof
haftmann@60438
   424
  assume A: "gcd m n = m div normalization_factor m"
haftmann@58023
   425
  show "m dvd n"
haftmann@58023
   426
  proof (cases "m = 0")
haftmann@58023
   427
    assume [simp]: "m \<noteq> 0"
haftmann@60438
   428
    from A have B: "m = gcd m n * normalization_factor m"
haftmann@58023
   429
      by (simp add: unit_eq_div2)
haftmann@58023
   430
    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
haftmann@58023
   431
  qed (insert A, simp)
haftmann@58023
   432
next
haftmann@58023
   433
  assume "m dvd n"
haftmann@60438
   434
  then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd)
haftmann@58023
   435
qed
haftmann@58023
   436
  
haftmann@60438
   437
lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m"
haftmann@58023
   438
  by (subst gcd.commute, simp add: gcd_proj1_iff)
haftmann@58023
   439
haftmann@58023
   440
lemma gcd_mod1 [simp]:
haftmann@60430
   441
  "gcd (a mod b) b = gcd a b"
haftmann@58023
   442
  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   443
haftmann@58023
   444
lemma gcd_mod2 [simp]:
haftmann@60430
   445
  "gcd a (b mod a) = gcd a b"
haftmann@58023
   446
  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
haftmann@58023
   447
         
haftmann@58023
   448
lemma gcd_mult_distrib': 
haftmann@60569
   449
  "c div normalization_factor c * gcd a b = gcd (c * a) (c * b)"
haftmann@60569
   450
proof (cases "c = 0")
haftmann@60569
   451
  case True then show ?thesis by (simp_all add: gcd_0)
haftmann@60569
   452
next
haftmann@60569
   453
  case False then have [simp]: "is_unit (normalization_factor c)" by simp
haftmann@60569
   454
  show ?thesis
haftmann@60569
   455
  proof (induct a b rule: gcd_eucl_induct)
haftmann@60569
   456
    case (zero a) show ?case
haftmann@60569
   457
    proof (cases "a = 0")
haftmann@60569
   458
      case True then show ?thesis by (simp add: gcd_0)
haftmann@60569
   459
    next
haftmann@60569
   460
      case False then have "is_unit (normalization_factor a)" by simp
haftmann@60569
   461
      then show ?thesis
haftmann@60569
   462
        by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq)
haftmann@60569
   463
    qed
haftmann@60569
   464
    case (mod a b)
haftmann@60569
   465
    then show ?case by (simp add: mult_mod_right gcd.commute)
haftmann@58023
   466
  qed
haftmann@58023
   467
qed
haftmann@58023
   468
haftmann@58023
   469
lemma gcd_mult_distrib:
haftmann@60438
   470
  "k * gcd a b = gcd (k*a) (k*b) * normalization_factor k"
haftmann@58023
   471
proof-
haftmann@60438
   472
  let ?nf = "normalization_factor"
haftmann@58023
   473
  from gcd_mult_distrib' 
haftmann@60430
   474
    have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" ..
haftmann@60430
   475
  also have "... = k * gcd a b div ?nf k"
haftmann@60438
   476
    by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)
haftmann@58023
   477
  finally show ?thesis
haftmann@59009
   478
    by simp
haftmann@58023
   479
qed
haftmann@58023
   480
haftmann@58023
   481
lemma euclidean_size_gcd_le1 [simp]:
haftmann@58023
   482
  assumes "a \<noteq> 0"
haftmann@58023
   483
  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
haftmann@58023
   484
proof -
haftmann@58023
   485
   have "gcd a b dvd a" by (rule gcd_dvd1)
haftmann@58023
   486
   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
wenzelm@60526
   487
   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
haftmann@58023
   488
qed
haftmann@58023
   489
haftmann@58023
   490
lemma euclidean_size_gcd_le2 [simp]:
haftmann@58023
   491
  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
haftmann@58023
   492
  by (subst gcd.commute, rule euclidean_size_gcd_le1)
haftmann@58023
   493
haftmann@58023
   494
lemma euclidean_size_gcd_less1:
haftmann@58023
   495
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
   496
  shows "euclidean_size (gcd a b) < euclidean_size a"
haftmann@58023
   497
proof (rule ccontr)
haftmann@58023
   498
  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
wenzelm@60526
   499
  with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
haftmann@58023
   500
    by (intro le_antisym, simp_all)
haftmann@58023
   501
  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
haftmann@58023
   502
  hence "a dvd b" using dvd_gcd_D2 by blast
wenzelm@60526
   503
  with \<open>\<not>a dvd b\<close> show False by contradiction
haftmann@58023
   504
qed
haftmann@58023
   505
haftmann@58023
   506
lemma euclidean_size_gcd_less2:
haftmann@58023
   507
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
   508
  shows "euclidean_size (gcd a b) < euclidean_size b"
haftmann@58023
   509
  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
haftmann@58023
   510
haftmann@60430
   511
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
haftmann@58023
   512
  apply (rule gcdI)
haftmann@58023
   513
  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
haftmann@58023
   514
  apply (rule gcd_dvd2)
haftmann@58023
   515
  apply (rule gcd_greatest, simp add: unit_simps, assumption)
haftmann@60438
   516
  apply (subst normalization_factor_gcd, simp add: gcd_0)
haftmann@58023
   517
  done
haftmann@58023
   518
haftmann@60430
   519
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
haftmann@58023
   520
  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
haftmann@58023
   521
haftmann@60430
   522
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
haftmann@60433
   523
  by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
haftmann@58023
   524
haftmann@60430
   525
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
haftmann@60433
   526
  by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
haftmann@58023
   527
haftmann@60438
   528
lemma gcd_idem: "gcd a a = a div normalization_factor a"
haftmann@60430
   529
  by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
haftmann@58023
   530
haftmann@60430
   531
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
haftmann@58023
   532
  apply (rule gcdI)
haftmann@58023
   533
  apply (simp add: ac_simps)
haftmann@58023
   534
  apply (rule gcd_dvd2)
haftmann@58023
   535
  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
haftmann@59009
   536
  apply simp
haftmann@58023
   537
  done
haftmann@58023
   538
haftmann@60430
   539
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
haftmann@58023
   540
  apply (rule gcdI)
haftmann@58023
   541
  apply simp
haftmann@58023
   542
  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
haftmann@58023
   543
  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
haftmann@59009
   544
  apply simp
haftmann@58023
   545
  done
haftmann@58023
   546
haftmann@58023
   547
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
haftmann@58023
   548
proof
haftmann@58023
   549
  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
haftmann@58023
   550
    by (simp add: fun_eq_iff ac_simps)
haftmann@58023
   551
next
haftmann@58023
   552
  fix a show "gcd a \<circ> gcd a = gcd a"
haftmann@58023
   553
    by (simp add: fun_eq_iff gcd_left_idem)
haftmann@58023
   554
qed
haftmann@58023
   555
haftmann@58023
   556
lemma coprime_dvd_mult:
haftmann@60430
   557
  assumes "gcd c b = 1" and "c dvd a * b"
haftmann@60430
   558
  shows "c dvd a"
haftmann@58023
   559
proof -
haftmann@60438
   560
  let ?nf = "normalization_factor"
haftmann@60430
   561
  from assms gcd_mult_distrib [of a c b] 
haftmann@60430
   562
    have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
wenzelm@60526
   563
  from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
haftmann@58023
   564
qed
haftmann@58023
   565
haftmann@58023
   566
lemma coprime_dvd_mult_iff:
haftmann@60430
   567
  "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
haftmann@58023
   568
  by (rule, rule coprime_dvd_mult, simp_all)
haftmann@58023
   569
haftmann@58023
   570
lemma gcd_dvd_antisym:
haftmann@58023
   571
  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
haftmann@58023
   572
proof (rule gcdI)
haftmann@58023
   573
  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
haftmann@58023
   574
  have "gcd c d dvd c" by simp
haftmann@58023
   575
  with A show "gcd a b dvd c" by (rule dvd_trans)
haftmann@58023
   576
  have "gcd c d dvd d" by simp
haftmann@58023
   577
  with A show "gcd a b dvd d" by (rule dvd_trans)
haftmann@60438
   578
  show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
haftmann@59009
   579
    by simp
haftmann@58023
   580
  fix l assume "l dvd c" and "l dvd d"
haftmann@58023
   581
  hence "l dvd gcd c d" by (rule gcd_greatest)
haftmann@58023
   582
  from this and B show "l dvd gcd a b" by (rule dvd_trans)
haftmann@58023
   583
qed
haftmann@58023
   584
haftmann@58023
   585
lemma gcd_mult_cancel:
haftmann@58023
   586
  assumes "gcd k n = 1"
haftmann@58023
   587
  shows "gcd (k * m) n = gcd m n"
haftmann@58023
   588
proof (rule gcd_dvd_antisym)
haftmann@58023
   589
  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
wenzelm@60526
   590
  also note \<open>gcd k n = 1\<close>
haftmann@58023
   591
  finally have "gcd (gcd (k * m) n) k = 1" by simp
haftmann@58023
   592
  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
haftmann@58023
   593
  moreover have "gcd (k * m) n dvd n" by simp
haftmann@58023
   594
  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
haftmann@58023
   595
  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
haftmann@58023
   596
  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
haftmann@58023
   597
qed
haftmann@58023
   598
haftmann@58023
   599
lemma coprime_crossproduct:
haftmann@58023
   600
  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
haftmann@58023
   601
  shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@58023
   602
proof
haftmann@58023
   603
  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
haftmann@58023
   604
next
haftmann@58023
   605
  assume ?lhs
wenzelm@60526
   606
  from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
haftmann@58023
   607
  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
wenzelm@60526
   608
  moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
haftmann@58023
   609
  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
wenzelm@60526
   610
  moreover from \<open>?lhs\<close> have "c dvd d * b" 
haftmann@59009
   611
    unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   612
  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
wenzelm@60526
   613
  moreover from \<open>?lhs\<close> have "d dvd c * a"
haftmann@59009
   614
    unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
haftmann@58023
   615
  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
haftmann@58023
   616
  ultimately show ?rhs unfolding associated_def by simp
haftmann@58023
   617
qed
haftmann@58023
   618
haftmann@58023
   619
lemma gcd_add1 [simp]:
haftmann@58023
   620
  "gcd (m + n) n = gcd m n"
haftmann@58023
   621
  by (cases "n = 0", simp_all add: gcd_non_0)
haftmann@58023
   622
haftmann@58023
   623
lemma gcd_add2 [simp]:
haftmann@58023
   624
  "gcd m (m + n) = gcd m n"
haftmann@58023
   625
  using gcd_add1 [of n m] by (simp add: ac_simps)
haftmann@58023
   626
haftmann@60572
   627
lemma gcd_add_mult:
haftmann@60572
   628
  "gcd m (k * m + n) = gcd m n"
haftmann@60572
   629
proof -
haftmann@60572
   630
  have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
haftmann@60572
   631
    by (fact gcd_mod2)
haftmann@60572
   632
  then show ?thesis by simp 
haftmann@60572
   633
qed
haftmann@58023
   634
haftmann@60430
   635
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
haftmann@58023
   636
  by (rule sym, rule gcdI, simp_all)
haftmann@58023
   637
haftmann@58023
   638
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
haftmann@59061
   639
  by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
haftmann@58023
   640
haftmann@58023
   641
lemma div_gcd_coprime:
haftmann@58023
   642
  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
haftmann@58023
   643
  defines [simp]: "d \<equiv> gcd a b"
haftmann@58023
   644
  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
haftmann@58023
   645
  shows "gcd a' b' = 1"
haftmann@58023
   646
proof (rule coprimeI)
haftmann@58023
   647
  fix l assume "l dvd a'" "l dvd b'"
haftmann@58023
   648
  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
haftmann@59009
   649
  moreover have "a = a' * d" "b = b' * d" by simp_all
haftmann@58023
   650
  ultimately have "a = (l * d) * s" "b = (l * d) * t"
haftmann@59009
   651
    by (simp_all only: ac_simps)
haftmann@58023
   652
  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
haftmann@58023
   653
  hence "l*d dvd d" by (simp add: gcd_greatest)
haftmann@59009
   654
  then obtain u where "d = l * d * u" ..
haftmann@59009
   655
  then have "d * (l * u) = d" by (simp add: ac_simps)
haftmann@59009
   656
  moreover from nz have "d \<noteq> 0" by simp
haftmann@59009
   657
  with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
haftmann@59009
   658
  ultimately have "1 = l * u"
wenzelm@60526
   659
    using \<open>d \<noteq> 0\<close> by simp
haftmann@59009
   660
  then show "l dvd 1" ..
haftmann@58023
   661
qed
haftmann@58023
   662
haftmann@58023
   663
lemma coprime_mult: 
haftmann@58023
   664
  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
haftmann@58023
   665
  shows "gcd d (a * b) = 1"
haftmann@58023
   666
  apply (subst gcd.commute)
haftmann@58023
   667
  using da apply (subst gcd_mult_cancel)
haftmann@58023
   668
  apply (subst gcd.commute, assumption)
haftmann@58023
   669
  apply (subst gcd.commute, rule db)
haftmann@58023
   670
  done
haftmann@58023
   671
haftmann@58023
   672
lemma coprime_lmult:
haftmann@58023
   673
  assumes dab: "gcd d (a * b) = 1" 
haftmann@58023
   674
  shows "gcd d a = 1"
haftmann@58023
   675
proof (rule coprimeI)
haftmann@58023
   676
  fix l assume "l dvd d" and "l dvd a"
haftmann@58023
   677
  hence "l dvd a * b" by simp
wenzelm@60526
   678
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   679
qed
haftmann@58023
   680
haftmann@58023
   681
lemma coprime_rmult:
haftmann@58023
   682
  assumes dab: "gcd d (a * b) = 1"
haftmann@58023
   683
  shows "gcd d b = 1"
haftmann@58023
   684
proof (rule coprimeI)
haftmann@58023
   685
  fix l assume "l dvd d" and "l dvd b"
haftmann@58023
   686
  hence "l dvd a * b" by simp
wenzelm@60526
   687
  with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
haftmann@58023
   688
qed
haftmann@58023
   689
haftmann@58023
   690
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
haftmann@58023
   691
  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
haftmann@58023
   692
haftmann@58023
   693
lemma gcd_coprime:
haftmann@60430
   694
  assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
haftmann@58023
   695
  shows "gcd a' b' = 1"
haftmann@58023
   696
proof -
haftmann@60430
   697
  from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
haftmann@58023
   698
  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
haftmann@58023
   699
  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
haftmann@58023
   700
  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
haftmann@58023
   701
  finally show ?thesis .
haftmann@58023
   702
qed
haftmann@58023
   703
haftmann@58023
   704
lemma coprime_power:
haftmann@58023
   705
  assumes "0 < n"
haftmann@58023
   706
  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
haftmann@58023
   707
using assms proof (induct n)
haftmann@58023
   708
  case (Suc n) then show ?case
haftmann@58023
   709
    by (cases n) (simp_all add: coprime_mul_eq)
haftmann@58023
   710
qed simp
haftmann@58023
   711
haftmann@58023
   712
lemma gcd_coprime_exists:
haftmann@58023
   713
  assumes nz: "gcd a b \<noteq> 0"
haftmann@58023
   714
  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
haftmann@58023
   715
  apply (rule_tac x = "a div gcd a b" in exI)
haftmann@58023
   716
  apply (rule_tac x = "b div gcd a b" in exI)
haftmann@59009
   717
  apply (insert nz, auto intro: div_gcd_coprime)
haftmann@58023
   718
  done
haftmann@58023
   719
haftmann@58023
   720
lemma coprime_exp:
haftmann@58023
   721
  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
haftmann@58023
   722
  by (induct n, simp_all add: coprime_mult)
haftmann@58023
   723
haftmann@58023
   724
lemma coprime_exp2 [intro]:
haftmann@58023
   725
  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
haftmann@58023
   726
  apply (rule coprime_exp)
haftmann@58023
   727
  apply (subst gcd.commute)
haftmann@58023
   728
  apply (rule coprime_exp)
haftmann@58023
   729
  apply (subst gcd.commute)
haftmann@58023
   730
  apply assumption
haftmann@58023
   731
  done
haftmann@58023
   732
haftmann@58023
   733
lemma gcd_exp:
haftmann@58023
   734
  "gcd (a^n) (b^n) = (gcd a b) ^ n"
haftmann@58023
   735
proof (cases "a = 0 \<and> b = 0")
haftmann@58023
   736
  assume "a = 0 \<and> b = 0"
haftmann@58023
   737
  then show ?thesis by (cases n, simp_all add: gcd_0_left)
haftmann@58023
   738
next
haftmann@58023
   739
  assume A: "\<not>(a = 0 \<and> b = 0)"
haftmann@58023
   740
  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
haftmann@58023
   741
    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
haftmann@58023
   742
  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
haftmann@58023
   743
  also note gcd_mult_distrib
haftmann@60438
   744
  also have "normalization_factor ((gcd a b)^n) = 1"
haftmann@60438
   745
    by (simp add: normalization_factor_pow A)
haftmann@58023
   746
  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
haftmann@58023
   747
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   748
  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
haftmann@58023
   749
    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
haftmann@58023
   750
  finally show ?thesis by simp
haftmann@58023
   751
qed
haftmann@58023
   752
haftmann@58023
   753
lemma coprime_common_divisor: 
haftmann@60430
   754
  "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
haftmann@60430
   755
  apply (subgoal_tac "a dvd gcd a b")
haftmann@59061
   756
  apply simp
haftmann@58023
   757
  apply (erule (1) gcd_greatest)
haftmann@58023
   758
  done
haftmann@58023
   759
haftmann@58023
   760
lemma division_decomp: 
haftmann@58023
   761
  assumes dc: "a dvd b * c"
haftmann@58023
   762
  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
haftmann@58023
   763
proof (cases "gcd a b = 0")
haftmann@58023
   764
  assume "gcd a b = 0"
haftmann@59009
   765
  hence "a = 0 \<and> b = 0" by simp
haftmann@58023
   766
  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
haftmann@58023
   767
  then show ?thesis by blast
haftmann@58023
   768
next
haftmann@58023
   769
  let ?d = "gcd a b"
haftmann@58023
   770
  assume "?d \<noteq> 0"
haftmann@58023
   771
  from gcd_coprime_exists[OF this]
haftmann@58023
   772
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   773
    by blast
haftmann@58023
   774
  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
haftmann@58023
   775
  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
haftmann@58023
   776
  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
haftmann@58023
   777
  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
wenzelm@60526
   778
  with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
haftmann@58023
   779
  with coprime_dvd_mult[OF ab'(3)] 
haftmann@58023
   780
    have "a' dvd c" by (subst (asm) ac_simps, blast)
haftmann@58023
   781
  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
haftmann@58023
   782
  then show ?thesis by blast
haftmann@58023
   783
qed
haftmann@58023
   784
haftmann@60433
   785
lemma pow_divs_pow:
haftmann@58023
   786
  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
haftmann@58023
   787
  shows "a dvd b"
haftmann@58023
   788
proof (cases "gcd a b = 0")
haftmann@58023
   789
  assume "gcd a b = 0"
haftmann@59009
   790
  then show ?thesis by simp
haftmann@58023
   791
next
haftmann@58023
   792
  let ?d = "gcd a b"
haftmann@58023
   793
  assume "?d \<noteq> 0"
haftmann@58023
   794
  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
wenzelm@60526
   795
  from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
wenzelm@60526
   796
  from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
haftmann@58023
   797
    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
haftmann@58023
   798
    by blast
haftmann@58023
   799
  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
haftmann@58023
   800
    by (simp add: ab'(1,2)[symmetric])
haftmann@58023
   801
  hence "?d^n * a'^n dvd ?d^n * b'^n"
haftmann@58023
   802
    by (simp only: power_mult_distrib ac_simps)
haftmann@59009
   803
  with zn have "a'^n dvd b'^n" by simp
haftmann@58023
   804
  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
haftmann@58023
   805
  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
haftmann@58023
   806
  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
haftmann@58023
   807
    have "a' dvd b'" by (subst (asm) ac_simps, blast)
haftmann@58023
   808
  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
haftmann@58023
   809
  with ab'(1,2) show ?thesis by simp
haftmann@58023
   810
qed
haftmann@58023
   811
haftmann@60433
   812
lemma pow_divs_eq [simp]:
haftmann@58023
   813
  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
haftmann@60433
   814
  by (auto intro: pow_divs_pow dvd_power_same)
haftmann@58023
   815
haftmann@60433
   816
lemma divs_mult:
haftmann@58023
   817
  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
haftmann@58023
   818
  shows "m * n dvd r"
haftmann@58023
   819
proof -
haftmann@58023
   820
  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
haftmann@58023
   821
    unfolding dvd_def by blast
haftmann@58023
   822
  from mr n' have "m dvd n'*n" by (simp add: ac_simps)
haftmann@58023
   823
  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
haftmann@58023
   824
  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
haftmann@58023
   825
  with n' have "r = m * n * k" by (simp add: mult_ac)
haftmann@58023
   826
  then show ?thesis unfolding dvd_def by blast
haftmann@58023
   827
qed
haftmann@58023
   828
haftmann@58023
   829
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
haftmann@58023
   830
  by (subst add_commute, simp)
haftmann@58023
   831
haftmann@58023
   832
lemma setprod_coprime [rule_format]:
haftmann@60430
   833
  "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
haftmann@58023
   834
  apply (cases "finite A")
haftmann@58023
   835
  apply (induct set: finite)
haftmann@58023
   836
  apply (auto simp add: gcd_mult_cancel)
haftmann@58023
   837
  done
haftmann@58023
   838
haftmann@58023
   839
lemma coprime_divisors: 
haftmann@58023
   840
  assumes "d dvd a" "e dvd b" "gcd a b = 1"
haftmann@58023
   841
  shows "gcd d e = 1" 
haftmann@58023
   842
proof -
haftmann@58023
   843
  from assms obtain k l where "a = d * k" "b = e * l"
haftmann@58023
   844
    unfolding dvd_def by blast
haftmann@58023
   845
  with assms have "gcd (d * k) (e * l) = 1" by simp
haftmann@58023
   846
  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
haftmann@58023
   847
  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
haftmann@58023
   848
  finally have "gcd e d = 1" by (rule coprime_lmult)
haftmann@58023
   849
  then show ?thesis by (simp add: ac_simps)
haftmann@58023
   850
qed
haftmann@58023
   851
haftmann@58023
   852
lemma invertible_coprime:
haftmann@60430
   853
  assumes "a * b mod m = 1"
haftmann@60430
   854
  shows "coprime a m"
haftmann@59009
   855
proof -
haftmann@60430
   856
  from assms have "coprime m (a * b mod m)"
haftmann@59009
   857
    by simp
haftmann@60430
   858
  then have "coprime m (a * b)"
haftmann@59009
   859
    by simp
haftmann@60430
   860
  then have "coprime m a"
haftmann@59009
   861
    by (rule coprime_lmult)
haftmann@59009
   862
  then show ?thesis
haftmann@59009
   863
    by (simp add: ac_simps)
haftmann@59009
   864
qed
haftmann@58023
   865
haftmann@58023
   866
lemma lcm_gcd:
haftmann@60438
   867
  "lcm a b = a * b div (gcd a b * normalization_factor (a*b))"
haftmann@58023
   868
  by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
haftmann@58023
   869
haftmann@58023
   870
lemma lcm_gcd_prod:
haftmann@60438
   871
  "lcm a b * gcd a b = a * b div normalization_factor (a*b)"
haftmann@58023
   872
proof (cases "a * b = 0")
haftmann@60438
   873
  let ?nf = normalization_factor
haftmann@58023
   874
  assume "a * b \<noteq> 0"
haftmann@58953
   875
  hence "gcd a b \<noteq> 0" by simp
haftmann@58023
   876
  from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
haftmann@58023
   877
    by (simp add: mult_ac)
wenzelm@60526
   878
  also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)"
haftmann@60432
   879
    by (simp add: div_mult_swap mult.commute)
haftmann@58023
   880
  finally show ?thesis .
haftmann@58953
   881
qed (auto simp add: lcm_gcd)
haftmann@58023
   882
haftmann@58023
   883
lemma lcm_dvd1 [iff]:
haftmann@60430
   884
  "a dvd lcm a b"
haftmann@60430
   885
proof (cases "a*b = 0")
haftmann@60430
   886
  assume "a * b \<noteq> 0"
haftmann@60430
   887
  hence "gcd a b \<noteq> 0" by simp
haftmann@60438
   888
  let ?c = "1 div normalization_factor (a * b)"
wenzelm@60526
   889
  from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp
haftmann@60430
   890
  from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
haftmann@60432
   891
    by (simp add: div_mult_swap unit_div_commute)
haftmann@60430
   892
  hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
wenzelm@60526
   893
  with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
haftmann@58023
   894
    by (subst (asm) div_mult_self2_is_id, simp_all)
haftmann@60430
   895
  also have "... = a * (?c * b div gcd a b)"
haftmann@58023
   896
    by (metis div_mult_swap gcd_dvd2 mult_assoc)
haftmann@58023
   897
  finally show ?thesis by (rule dvdI)
haftmann@58953
   898
qed (auto simp add: lcm_gcd)
haftmann@58023
   899
haftmann@58023
   900
lemma lcm_least:
haftmann@58023
   901
  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
haftmann@58023
   902
proof (cases "k = 0")
haftmann@60438
   903
  let ?nf = normalization_factor
haftmann@58023
   904
  assume "k \<noteq> 0"
haftmann@58023
   905
  hence "is_unit (?nf k)" by simp
haftmann@58023
   906
  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
haftmann@58023
   907
  assume A: "a dvd k" "b dvd k"
wenzelm@60526
   908
  hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
haftmann@58023
   909
  from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
haftmann@58023
   910
    unfolding dvd_def by blast
wenzelm@60526
   911
  with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
haftmann@58953
   912
    by auto (drule sym [of 0], simp)
haftmann@58023
   913
  hence "is_unit (?nf (r * s))" by simp
haftmann@58023
   914
  let ?c = "?nf k div ?nf (r*s)"
wenzelm@60526
   915
  from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
haftmann@58023
   916
  hence "?c \<noteq> 0" using not_is_unit_0 by fast 
haftmann@58023
   917
  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
haftmann@58953
   918
    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
haftmann@58023
   919
  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
wenzelm@60526
   920
    by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
wenzelm@60526
   921
  also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
haftmann@58023
   922
    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
haftmann@58023
   923
  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
haftmann@58023
   924
    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
haftmann@58023
   925
  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
haftmann@58023
   926
    by (simp add: algebra_simps)
wenzelm@60526
   927
  hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
haftmann@58023
   928
    by (metis div_mult_self2_is_id)
haftmann@58023
   929
  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
haftmann@58023
   930
    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') 
haftmann@58023
   931
  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
haftmann@58023
   932
    by (simp add: algebra_simps)
wenzelm@60526
   933
  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
haftmann@58023
   934
    by (metis mult.commute div_mult_self2_is_id)
wenzelm@60526
   935
  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
haftmann@58023
   936
    by (metis div_mult_self2_is_id mult_assoc) 
wenzelm@60526
   937
  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
haftmann@58023
   938
    by (simp add: unit_simps)
haftmann@58023
   939
  finally show ?thesis by (rule dvdI)
haftmann@58023
   940
qed simp
haftmann@58023
   941
haftmann@58023
   942
lemma lcm_zero:
haftmann@58023
   943
  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
haftmann@58023
   944
proof -
haftmann@60438
   945
  let ?nf = normalization_factor
haftmann@58023
   946
  {
haftmann@58023
   947
    assume "a \<noteq> 0" "b \<noteq> 0"
haftmann@58023
   948
    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
wenzelm@60526
   949
    moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
haftmann@58023
   950
    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
haftmann@58023
   951
  } moreover {
haftmann@58023
   952
    assume "a = 0 \<or> b = 0"
haftmann@58023
   953
    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
haftmann@58023
   954
  }
haftmann@58023
   955
  ultimately show ?thesis by blast
haftmann@58023
   956
qed
haftmann@58023
   957
haftmann@58023
   958
lemmas lcm_0_iff = lcm_zero
haftmann@58023
   959
haftmann@58023
   960
lemma gcd_lcm: 
haftmann@58023
   961
  assumes "lcm a b \<noteq> 0"
haftmann@60438
   962
  shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))"
haftmann@58023
   963
proof-
haftmann@59009
   964
  from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)
haftmann@60438
   965
  let ?c = "normalization_factor (a * b)"
wenzelm@60526
   966
  from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
haftmann@58023
   967
  hence "is_unit ?c" by simp
haftmann@58023
   968
  from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
wenzelm@60526
   969
    by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac)
wenzelm@60526
   970
  also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)"
wenzelm@60526
   971
    by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd')
haftmann@60433
   972
  finally show ?thesis .
haftmann@58023
   973
qed
haftmann@58023
   974
haftmann@60438
   975
lemma normalization_factor_lcm [simp]:
haftmann@60438
   976
  "normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
haftmann@58023
   977
proof (cases "a = 0 \<or> b = 0")
haftmann@58023
   978
  case True then show ?thesis
haftmann@58953
   979
    by (auto simp add: lcm_gcd) 
haftmann@58023
   980
next
haftmann@58023
   981
  case False
haftmann@60438
   982
  let ?nf = normalization_factor
haftmann@58023
   983
  from lcm_gcd_prod[of a b] 
haftmann@58023
   984
    have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
haftmann@60438
   985
    by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)
haftmann@58023
   986
  also have "... = (if a*b = 0 then 0 else 1)"
haftmann@58953
   987
    by simp
haftmann@58953
   988
  finally show ?thesis using False by simp
haftmann@58023
   989
qed
haftmann@58023
   990
haftmann@60430
   991
lemma lcm_dvd2 [iff]: "b dvd lcm a b"
haftmann@60430
   992
  using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
haftmann@58023
   993
haftmann@58023
   994
lemma lcmI:
haftmann@60430
   995
  "\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l;
haftmann@60438
   996
    normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b"
haftmann@58023
   997
  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
haftmann@58023
   998
haftmann@58023
   999
sublocale lcm!: abel_semigroup lcm
haftmann@58023
  1000
proof
haftmann@60430
  1001
  fix a b c
haftmann@60430
  1002
  show "lcm (lcm a b) c = lcm a (lcm b c)"
haftmann@58023
  1003
  proof (rule lcmI)
haftmann@60430
  1004
    have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
haftmann@60430
  1005
    then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
haftmann@58023
  1006
    
haftmann@60430
  1007
    have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
haftmann@60430
  1008
    hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
haftmann@60430
  1009
    moreover have "c dvd lcm (lcm a b) c" by simp
haftmann@60430
  1010
    ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
haftmann@58023
  1011
haftmann@60430
  1012
    fix l assume "a dvd l" and "lcm b c dvd l"
haftmann@60430
  1013
    have "b dvd lcm b c" by simp
wenzelm@60526
  1014
    from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
haftmann@60430
  1015
    have "c dvd lcm b c" by simp
wenzelm@60526
  1016
    from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
wenzelm@60526
  1017
    from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
wenzelm@60526
  1018
    from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
haftmann@58023
  1019
  qed (simp add: lcm_zero)
haftmann@58023
  1020
next
haftmann@60430
  1021
  fix a b
haftmann@60430
  1022
  show "lcm a b = lcm b a"
haftmann@58023
  1023
    by (simp add: lcm_gcd ac_simps)
haftmann@58023
  1024
qed
haftmann@58023
  1025
haftmann@58023
  1026
lemma dvd_lcm_D1:
haftmann@58023
  1027
  "lcm m n dvd k \<Longrightarrow> m dvd k"
haftmann@58023
  1028
  by (rule dvd_trans, rule lcm_dvd1, assumption)
haftmann@58023
  1029
haftmann@58023
  1030
lemma dvd_lcm_D2:
haftmann@58023
  1031
  "lcm m n dvd k \<Longrightarrow> n dvd k"
haftmann@58023
  1032
  by (rule dvd_trans, rule lcm_dvd2, assumption)
haftmann@58023
  1033
haftmann@58023
  1034
lemma gcd_dvd_lcm [simp]:
haftmann@58023
  1035
  "gcd a b dvd lcm a b"
haftmann@58023
  1036
  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
haftmann@58023
  1037
haftmann@58023
  1038
lemma lcm_1_iff:
haftmann@58023
  1039
  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@58023
  1040
proof
haftmann@58023
  1041
  assume "lcm a b = 1"
haftmann@59061
  1042
  then show "is_unit a \<and> is_unit b" by auto
haftmann@58023
  1043
next
haftmann@58023
  1044
  assume "is_unit a \<and> is_unit b"
haftmann@59061
  1045
  hence "a dvd 1" and "b dvd 1" by simp_all
haftmann@59061
  1046
  hence "is_unit (lcm a b)" by (rule lcm_least)
haftmann@60438
  1047
  hence "lcm a b = normalization_factor (lcm a b)"
haftmann@60438
  1048
    by (subst normalization_factor_unit, simp_all)
wenzelm@60526
  1049
  also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
haftmann@59061
  1050
    by auto
haftmann@58023
  1051
  finally show "lcm a b = 1" .
haftmann@58023
  1052
qed
haftmann@58023
  1053
haftmann@58023
  1054
lemma lcm_0_left [simp]:
haftmann@60430
  1055
  "lcm 0 a = 0"
haftmann@58023
  1056
  by (rule sym, rule lcmI, simp_all)
haftmann@58023
  1057
haftmann@58023
  1058
lemma lcm_0 [simp]:
haftmann@60430
  1059
  "lcm a 0 = 0"
haftmann@58023
  1060
  by (rule sym, rule lcmI, simp_all)
haftmann@58023
  1061
haftmann@58023
  1062
lemma lcm_unique:
haftmann@58023
  1063
  "a dvd d \<and> b dvd d \<and> 
haftmann@60438
  1064
  normalization_factor d = (if d = 0 then 0 else 1) \<and>
haftmann@58023
  1065
  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
haftmann@58023
  1066
  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
haftmann@58023
  1067
haftmann@58023
  1068
lemma dvd_lcm_I1 [simp]:
haftmann@58023
  1069
  "k dvd m \<Longrightarrow> k dvd lcm m n"
haftmann@58023
  1070
  by (metis lcm_dvd1 dvd_trans)
haftmann@58023
  1071
haftmann@58023
  1072
lemma dvd_lcm_I2 [simp]:
haftmann@58023
  1073
  "k dvd n \<Longrightarrow> k dvd lcm m n"
haftmann@58023
  1074
  by (metis lcm_dvd2 dvd_trans)
haftmann@58023
  1075
haftmann@58023
  1076
lemma lcm_1_left [simp]:
haftmann@60438
  1077
  "lcm 1 a = a div normalization_factor a"
haftmann@60430
  1078
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
haftmann@58023
  1079
haftmann@58023
  1080
lemma lcm_1_right [simp]:
haftmann@60438
  1081
  "lcm a 1 = a div normalization_factor a"
haftmann@60430
  1082
  using lcm_1_left [of a] by (simp add: ac_simps)
haftmann@58023
  1083
haftmann@58023
  1084
lemma lcm_coprime:
haftmann@60438
  1085
  "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)"
haftmann@58023
  1086
  by (subst lcm_gcd) simp
haftmann@58023
  1087
haftmann@58023
  1088
lemma lcm_proj1_if_dvd: 
haftmann@60438
  1089
  "b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a"
haftmann@60430
  1090
  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
haftmann@58023
  1091
haftmann@58023
  1092
lemma lcm_proj2_if_dvd: 
haftmann@60438
  1093
  "a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b"
haftmann@60430
  1094
  using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
haftmann@58023
  1095
haftmann@58023
  1096
lemma lcm_proj1_iff:
haftmann@60438
  1097
  "lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m"
haftmann@58023
  1098
proof
haftmann@60438
  1099
  assume A: "lcm m n = m div normalization_factor m"
haftmann@58023
  1100
  show "n dvd m"
haftmann@58023
  1101
  proof (cases "m = 0")
haftmann@58023
  1102
    assume [simp]: "m \<noteq> 0"
haftmann@60438
  1103
    from A have B: "m = lcm m n * normalization_factor m"
haftmann@58023
  1104
      by (simp add: unit_eq_div2)
haftmann@58023
  1105
    show ?thesis by (subst B, simp)
haftmann@58023
  1106
  qed simp
haftmann@58023
  1107
next
haftmann@58023
  1108
  assume "n dvd m"
haftmann@60438
  1109
  then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd)
haftmann@58023
  1110
qed
haftmann@58023
  1111
haftmann@58023
  1112
lemma lcm_proj2_iff:
haftmann@60438
  1113
  "lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n"
haftmann@58023
  1114
  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
haftmann@58023
  1115
haftmann@58023
  1116
lemma euclidean_size_lcm_le1: 
haftmann@58023
  1117
  assumes "a \<noteq> 0" and "b \<noteq> 0"
haftmann@58023
  1118
  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
haftmann@58023
  1119
proof -
haftmann@58023
  1120
  have "a dvd lcm a b" by (rule lcm_dvd1)
haftmann@58023
  1121
  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
wenzelm@60526
  1122
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
haftmann@58023
  1123
  then show ?thesis by (subst A, intro size_mult_mono)
haftmann@58023
  1124
qed
haftmann@58023
  1125
haftmann@58023
  1126
lemma euclidean_size_lcm_le2:
haftmann@58023
  1127
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
haftmann@58023
  1128
  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
haftmann@58023
  1129
haftmann@58023
  1130
lemma euclidean_size_lcm_less1:
haftmann@58023
  1131
  assumes "b \<noteq> 0" and "\<not>b dvd a"
haftmann@58023
  1132
  shows "euclidean_size a < euclidean_size (lcm a b)"
haftmann@58023
  1133
proof (rule ccontr)
haftmann@58023
  1134
  from assms have "a \<noteq> 0" by auto
haftmann@58023
  1135
  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
wenzelm@60526
  1136
  with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
haftmann@58023
  1137
    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
haftmann@58023
  1138
  with assms have "lcm a b dvd a" 
haftmann@58023
  1139
    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
haftmann@58023
  1140
  hence "b dvd a" by (rule dvd_lcm_D2)
wenzelm@60526
  1141
  with \<open>\<not>b dvd a\<close> show False by contradiction
haftmann@58023
  1142
qed
haftmann@58023
  1143
haftmann@58023
  1144
lemma euclidean_size_lcm_less2:
haftmann@58023
  1145
  assumes "a \<noteq> 0" and "\<not>a dvd b"
haftmann@58023
  1146
  shows "euclidean_size b < euclidean_size (lcm a b)"
haftmann@58023
  1147
  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
haftmann@58023
  1148
haftmann@58023
  1149
lemma lcm_mult_unit1:
haftmann@60430
  1150
  "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
haftmann@58023
  1151
  apply (rule lcmI)
haftmann@60430
  1152
  apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
haftmann@58023
  1153
  apply (rule lcm_dvd2)
haftmann@58023
  1154
  apply (rule lcm_least, simp add: unit_simps, assumption)
haftmann@60438
  1155
  apply (subst normalization_factor_lcm, simp add: lcm_zero)
haftmann@58023
  1156
  done
haftmann@58023
  1157
haftmann@58023
  1158
lemma lcm_mult_unit2:
haftmann@60430
  1159
  "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
haftmann@60430
  1160
  using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
haftmann@58023
  1161
haftmann@58023
  1162
lemma lcm_div_unit1:
haftmann@60430
  1163
  "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
haftmann@60433
  1164
  by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
haftmann@58023
  1165
haftmann@58023
  1166
lemma lcm_div_unit2:
haftmann@60430
  1167
  "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
haftmann@60433
  1168
  by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
haftmann@58023
  1169
haftmann@58023
  1170
lemma lcm_left_idem:
haftmann@60430
  1171
  "lcm a (lcm a b) = lcm a b"
haftmann@58023
  1172
  apply (rule lcmI)
haftmann@58023
  1173
  apply simp
haftmann@58023
  1174
  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
haftmann@58023
  1175
  apply (rule lcm_least, assumption)
haftmann@58023
  1176
  apply (erule (1) lcm_least)
haftmann@58023
  1177
  apply (auto simp: lcm_zero)
haftmann@58023
  1178
  done
haftmann@58023
  1179
haftmann@58023
  1180
lemma lcm_right_idem:
haftmann@60430
  1181
  "lcm (lcm a b) b = lcm a b"
haftmann@58023
  1182
  apply (rule lcmI)
haftmann@58023
  1183
  apply (subst lcm.assoc, rule lcm_dvd1)
haftmann@58023
  1184
  apply (rule lcm_dvd2)
haftmann@58023
  1185
  apply (rule lcm_least, erule (1) lcm_least, assumption)
haftmann@58023
  1186
  apply (auto simp: lcm_zero)
haftmann@58023
  1187
  done
haftmann@58023
  1188
haftmann@58023
  1189
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
haftmann@58023
  1190
proof
haftmann@58023
  1191
  fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
haftmann@58023
  1192
    by (simp add: fun_eq_iff ac_simps)
haftmann@58023
  1193
next
haftmann@58023
  1194
  fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
haftmann@58023
  1195
    by (intro ext, simp add: lcm_left_idem)
haftmann@58023
  1196
qed
haftmann@58023
  1197
haftmann@60430
  1198
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
haftmann@60430
  1199
  and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'"
haftmann@60438
  1200
  and normalization_factor_Lcm [simp]: 
haftmann@60438
  1201
          "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
haftmann@58023
  1202
proof -
haftmann@60430
  1203
  have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm A dvd l') \<and>
haftmann@60438
  1204
    normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
haftmann@60430
  1205
  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
haftmann@58023
  1206
    case False
haftmann@58023
  1207
    hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
haftmann@58023
  1208
    with False show ?thesis by auto
haftmann@58023
  1209
  next
haftmann@58023
  1210
    case True
haftmann@60430
  1211
    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast
haftmann@60430
  1212
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1213
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1214
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@58023
  1215
      apply (subst n_def)
haftmann@58023
  1216
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
haftmann@58023
  1217
      apply (rule exI[of _ l\<^sub>0])
haftmann@58023
  1218
      apply (simp add: l\<^sub>0_props)
haftmann@58023
  1219
      done
haftmann@60430
  1220
    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
haftmann@58023
  1221
      unfolding l_def by simp_all
haftmann@58023
  1222
    {
haftmann@60430
  1223
      fix l' assume "\<forall>a\<in>A. a dvd l'"
wenzelm@60526
  1224
      with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest)
wenzelm@60526
  1225
      moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
haftmann@60430
  1226
      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
haftmann@58023
  1227
        by (intro exI[of _ "gcd l l'"], auto)
haftmann@58023
  1228
      hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
haftmann@58023
  1229
      moreover have "euclidean_size (gcd l l') \<le> n"
haftmann@58023
  1230
      proof -
haftmann@58023
  1231
        have "gcd l l' dvd l" by simp
haftmann@58023
  1232
        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
wenzelm@60526
  1233
        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
haftmann@58023
  1234
        hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
haftmann@58023
  1235
          by (rule size_mult_mono)
wenzelm@60526
  1236
        also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
wenzelm@60526
  1237
        also note \<open>euclidean_size l = n\<close>
haftmann@58023
  1238
        finally show "euclidean_size (gcd l l') \<le> n" .
haftmann@58023
  1239
      qed
haftmann@58023
  1240
      ultimately have "euclidean_size l = euclidean_size (gcd l l')" 
wenzelm@60526
  1241
        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
wenzelm@60526
  1242
      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
haftmann@58023
  1243
      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
haftmann@58023
  1244
    }
haftmann@58023
  1245
wenzelm@60526
  1246
    with \<open>(\<forall>a\<in>A. a dvd l)\<close> and normalization_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>
haftmann@60438
  1247
      have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and> 
haftmann@60438
  1248
        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and>
haftmann@60438
  1249
        normalization_factor (l div normalization_factor l) = 
haftmann@60438
  1250
        (if l div normalization_factor l = 0 then 0 else 1)"
haftmann@58023
  1251
      by (auto simp: unit_simps)
haftmann@60438
  1252
    also from True have "l div normalization_factor l = Lcm A"
haftmann@58023
  1253
      by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
haftmann@58023
  1254
    finally show ?thesis .
haftmann@58023
  1255
  qed
haftmann@58023
  1256
  note A = this
haftmann@58023
  1257
haftmann@60430
  1258
  {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
haftmann@60430
  1259
  {fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast}
haftmann@60438
  1260
  from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
haftmann@58023
  1261
qed
haftmann@58023
  1262
    
haftmann@58023
  1263
lemma LcmI:
haftmann@60430
  1264
  "(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>
haftmann@60438
  1265
      normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
haftmann@58023
  1266
  by (intro normed_associated_imp_eq)
haftmann@58023
  1267
    (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
haftmann@58023
  1268
haftmann@58023
  1269
lemma Lcm_subset:
haftmann@58023
  1270
  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
haftmann@58023
  1271
  by (blast intro: Lcm_dvd dvd_Lcm)
haftmann@58023
  1272
haftmann@58023
  1273
lemma Lcm_Un:
haftmann@58023
  1274
  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
haftmann@58023
  1275
  apply (rule lcmI)
haftmann@58023
  1276
  apply (blast intro: Lcm_subset)
haftmann@58023
  1277
  apply (blast intro: Lcm_subset)
haftmann@58023
  1278
  apply (intro Lcm_dvd ballI, elim UnE)
haftmann@58023
  1279
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
  1280
  apply (rule dvd_trans, erule dvd_Lcm, assumption)
haftmann@58023
  1281
  apply simp
haftmann@58023
  1282
  done
haftmann@58023
  1283
haftmann@58023
  1284
lemma Lcm_1_iff:
haftmann@60430
  1285
  "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
haftmann@58023
  1286
proof
haftmann@58023
  1287
  assume "Lcm A = 1"
haftmann@60430
  1288
  then show "\<forall>a\<in>A. is_unit a" by auto
haftmann@58023
  1289
qed (rule LcmI [symmetric], auto)
haftmann@58023
  1290
haftmann@58023
  1291
lemma Lcm_no_units:
haftmann@60430
  1292
  "Lcm A = Lcm (A - {a. is_unit a})"
haftmann@58023
  1293
proof -
haftmann@60430
  1294
  have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
haftmann@60430
  1295
  hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
haftmann@58023
  1296
    by (simp add: Lcm_Un[symmetric])
haftmann@60430
  1297
  also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
haftmann@58023
  1298
  finally show ?thesis by simp
haftmann@58023
  1299
qed
haftmann@58023
  1300
haftmann@58023
  1301
lemma Lcm_empty [simp]:
haftmann@58023
  1302
  "Lcm {} = 1"
haftmann@58023
  1303
  by (simp add: Lcm_1_iff)
haftmann@58023
  1304
haftmann@58023
  1305
lemma Lcm_eq_0 [simp]:
haftmann@58023
  1306
  "0 \<in> A \<Longrightarrow> Lcm A = 0"
haftmann@58023
  1307
  by (drule dvd_Lcm) simp
haftmann@58023
  1308
haftmann@58023
  1309
lemma Lcm0_iff':
haftmann@60430
  1310
  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
  1311
proof
haftmann@58023
  1312
  assume "Lcm A = 0"
haftmann@60430
  1313
  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
haftmann@58023
  1314
  proof
haftmann@60430
  1315
    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
haftmann@60430
  1316
    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast
haftmann@60430
  1317
    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1318
    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@60430
  1319
    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
haftmann@58023
  1320
      apply (subst n_def)
haftmann@58023
  1321
      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
haftmann@58023
  1322
      apply (rule exI[of _ l\<^sub>0])
haftmann@58023
  1323
      apply (simp add: l\<^sub>0_props)
haftmann@58023
  1324
      done
haftmann@58023
  1325
    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
haftmann@60438
  1326
    hence "l div normalization_factor l \<noteq> 0" by simp
haftmann@60438
  1327
    also from ex have "l div normalization_factor l = Lcm A"
haftmann@58023
  1328
       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
wenzelm@60526
  1329
    finally show False using \<open>Lcm A = 0\<close> by contradiction
haftmann@58023
  1330
  qed
haftmann@58023
  1331
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
  1332
haftmann@58023
  1333
lemma Lcm0_iff [simp]:
haftmann@58023
  1334
  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
haftmann@58023
  1335
proof -
haftmann@58023
  1336
  assume "finite A"
haftmann@58023
  1337
  have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
haftmann@58023
  1338
  moreover {
haftmann@58023
  1339
    assume "0 \<notin> A"
haftmann@58023
  1340
    hence "\<Prod>A \<noteq> 0" 
wenzelm@60526
  1341
      apply (induct rule: finite_induct[OF \<open>finite A\<close>]) 
haftmann@58023
  1342
      apply simp
haftmann@58023
  1343
      apply (subst setprod.insert, assumption, assumption)
haftmann@58023
  1344
      apply (rule no_zero_divisors)
haftmann@58023
  1345
      apply blast+
haftmann@58023
  1346
      done
wenzelm@60526
  1347
    moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
haftmann@60430
  1348
    ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
haftmann@58023
  1349
    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
haftmann@58023
  1350
  }
haftmann@58023
  1351
  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
haftmann@58023
  1352
qed
haftmann@58023
  1353
haftmann@58023
  1354
lemma Lcm_no_multiple:
haftmann@60430
  1355
  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
haftmann@58023
  1356
proof -
haftmann@60430
  1357
  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
haftmann@60430
  1358
  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
haftmann@58023
  1359
  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
haftmann@58023
  1360
qed
haftmann@58023
  1361
haftmann@58023
  1362
lemma Lcm_insert [simp]:
haftmann@58023
  1363
  "Lcm (insert a A) = lcm a (Lcm A)"
haftmann@58023
  1364
proof (rule lcmI)
haftmann@58023
  1365
  fix l assume "a dvd l" and "Lcm A dvd l"
haftmann@60430
  1366
  hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
wenzelm@60526
  1367
  with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
haftmann@58023
  1368
qed (auto intro: Lcm_dvd dvd_Lcm)
haftmann@58023
  1369
 
haftmann@58023
  1370
lemma Lcm_finite:
haftmann@58023
  1371
  assumes "finite A"
haftmann@58023
  1372
  shows "Lcm A = Finite_Set.fold lcm 1 A"
wenzelm@60526
  1373
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
  1374
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
haftmann@58023
  1375
haftmann@60431
  1376
lemma Lcm_set [code_unfold]:
haftmann@58023
  1377
  "Lcm (set xs) = fold lcm xs 1"
haftmann@58023
  1378
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
haftmann@58023
  1379
haftmann@58023
  1380
lemma Lcm_singleton [simp]:
haftmann@60438
  1381
  "Lcm {a} = a div normalization_factor a"
haftmann@58023
  1382
  by simp
haftmann@58023
  1383
haftmann@58023
  1384
lemma Lcm_2 [simp]:
haftmann@58023
  1385
  "Lcm {a,b} = lcm a b"
haftmann@58023
  1386
  by (simp only: Lcm_insert Lcm_empty lcm_1_right)
haftmann@58023
  1387
    (cases "b = 0", simp, rule lcm_div_unit2, simp)
haftmann@58023
  1388
haftmann@58023
  1389
lemma Lcm_coprime:
haftmann@58023
  1390
  assumes "finite A" and "A \<noteq> {}" 
haftmann@58023
  1391
  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
haftmann@60438
  1392
  shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
haftmann@58023
  1393
using assms proof (induct rule: finite_ne_induct)
haftmann@58023
  1394
  case (insert a A)
haftmann@58023
  1395
  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
haftmann@60438
  1396
  also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast
haftmann@58023
  1397
  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
haftmann@58023
  1398
  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
haftmann@60438
  1399
  with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))"
haftmann@58023
  1400
    by (simp add: lcm_coprime)
haftmann@58023
  1401
  finally show ?case .
haftmann@58023
  1402
qed simp
haftmann@58023
  1403
      
haftmann@58023
  1404
lemma Lcm_coprime':
haftmann@58023
  1405
  "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
haftmann@60438
  1406
    \<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
haftmann@58023
  1407
  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
haftmann@58023
  1408
haftmann@58023
  1409
lemma Gcd_Lcm:
haftmann@60430
  1410
  "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
haftmann@58023
  1411
  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
haftmann@58023
  1412
haftmann@60430
  1413
lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
haftmann@60430
  1414
  and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A"
haftmann@60438
  1415
  and normalization_factor_Gcd [simp]: 
haftmann@60438
  1416
    "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
haftmann@58023
  1417
proof -
haftmann@60430
  1418
  fix a assume "a \<in> A"
haftmann@60430
  1419
  hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast
haftmann@60430
  1420
  then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
haftmann@58023
  1421
next
haftmann@60430
  1422
  fix g' assume "\<forall>a\<in>A. g' dvd a"
haftmann@60430
  1423
  hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
haftmann@58023
  1424
  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
haftmann@58023
  1425
next
haftmann@60438
  1426
  show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
haftmann@59009
  1427
    by (simp add: Gcd_Lcm)
haftmann@58023
  1428
qed
haftmann@58023
  1429
haftmann@58023
  1430
lemma GcdI:
haftmann@60430
  1431
  "(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow>
haftmann@60438
  1432
    normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
haftmann@58023
  1433
  by (intro normed_associated_imp_eq)
haftmann@58023
  1434
    (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
haftmann@58023
  1435
haftmann@58023
  1436
lemma Lcm_Gcd:
haftmann@60430
  1437
  "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
haftmann@58023
  1438
  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
haftmann@58023
  1439
haftmann@58023
  1440
lemma Gcd_0_iff:
haftmann@58023
  1441
  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
haftmann@58023
  1442
  apply (rule iffI)
haftmann@58023
  1443
  apply (rule subsetI, drule Gcd_dvd, simp)
haftmann@58023
  1444
  apply (auto intro: GcdI[symmetric])
haftmann@58023
  1445
  done
haftmann@58023
  1446
haftmann@58023
  1447
lemma Gcd_empty [simp]:
haftmann@58023
  1448
  "Gcd {} = 0"
haftmann@58023
  1449
  by (simp add: Gcd_0_iff)
haftmann@58023
  1450
haftmann@58023
  1451
lemma Gcd_1:
haftmann@58023
  1452
  "1 \<in> A \<Longrightarrow> Gcd A = 1"
haftmann@58023
  1453
  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
haftmann@58023
  1454
haftmann@58023
  1455
lemma Gcd_insert [simp]:
haftmann@58023
  1456
  "Gcd (insert a A) = gcd a (Gcd A)"
haftmann@58023
  1457
proof (rule gcdI)
haftmann@58023
  1458
  fix l assume "l dvd a" and "l dvd Gcd A"
haftmann@60430
  1459
  hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
wenzelm@60526
  1460
  with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
haftmann@59009
  1461
qed auto
haftmann@58023
  1462
haftmann@58023
  1463
lemma Gcd_finite:
haftmann@58023
  1464
  assumes "finite A"
haftmann@58023
  1465
  shows "Gcd A = Finite_Set.fold gcd 0 A"
wenzelm@60526
  1466
  by (induct rule: finite.induct[OF \<open>finite A\<close>])
haftmann@58023
  1467
    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
haftmann@58023
  1468
haftmann@60431
  1469
lemma Gcd_set [code_unfold]:
haftmann@58023
  1470
  "Gcd (set xs) = fold gcd xs 0"
haftmann@58023
  1471
  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
haftmann@58023
  1472
haftmann@60438
  1473
lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a"
haftmann@58023
  1474
  by (simp add: gcd_0)
haftmann@58023
  1475
haftmann@58023
  1476
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
haftmann@58023
  1477
  by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
haftmann@58023
  1478
haftmann@60439
  1479
subclass semiring_gcd
haftmann@60439
  1480
  by unfold_locales (simp_all add: gcd_greatest_iff)
haftmann@60439
  1481
  
haftmann@58023
  1482
end
haftmann@58023
  1483
wenzelm@60526
  1484
text \<open>
haftmann@58023
  1485
  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
haftmann@58023
  1486
  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
wenzelm@60526
  1487
\<close>
haftmann@58023
  1488
haftmann@58023
  1489
class euclidean_ring_gcd = euclidean_semiring_gcd + idom
haftmann@58023
  1490
begin
haftmann@58023
  1491
haftmann@58023
  1492
subclass euclidean_ring ..
haftmann@58023
  1493
haftmann@60439
  1494
subclass ring_gcd ..
haftmann@60439
  1495
haftmann@60572
  1496
lemma euclid_ext_gcd [simp]:
haftmann@60572
  1497
  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
haftmann@60572
  1498
  by (induct a b rule: gcd_eucl_induct)
haftmann@60572
  1499
    (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
haftmann@60572
  1500
haftmann@60572
  1501
lemma euclid_ext_gcd' [simp]:
haftmann@60572
  1502
  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
haftmann@60572
  1503
  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
haftmann@60572
  1504
  
haftmann@60572
  1505
lemma euclid_ext'_correct:
haftmann@60572
  1506
  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
haftmann@60572
  1507
proof-
haftmann@60572
  1508
  obtain s t c where "euclid_ext a b = (s,t,c)"
haftmann@60572
  1509
    by (cases "euclid_ext a b", blast)
haftmann@60572
  1510
  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
haftmann@60572
  1511
    show ?thesis unfolding euclid_ext'_def by simp
haftmann@60572
  1512
qed
haftmann@60572
  1513
haftmann@60572
  1514
lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
haftmann@60572
  1515
  using euclid_ext'_correct by blast
haftmann@60572
  1516
haftmann@58023
  1517
lemma gcd_neg1 [simp]:
haftmann@60430
  1518
  "gcd (-a) b = gcd a b"
haftmann@59009
  1519
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1520
haftmann@58023
  1521
lemma gcd_neg2 [simp]:
haftmann@60430
  1522
  "gcd a (-b) = gcd a b"
haftmann@59009
  1523
  by (rule sym, rule gcdI, simp_all add: gcd_greatest)
haftmann@58023
  1524
haftmann@58023
  1525
lemma gcd_neg_numeral_1 [simp]:
haftmann@60430
  1526
  "gcd (- numeral n) a = gcd (numeral n) a"
haftmann@58023
  1527
  by (fact gcd_neg1)
haftmann@58023
  1528
haftmann@58023
  1529
lemma gcd_neg_numeral_2 [simp]:
haftmann@60430
  1530
  "gcd a (- numeral n) = gcd a (numeral n)"
haftmann@58023
  1531
  by (fact gcd_neg2)
haftmann@58023
  1532
haftmann@58023
  1533
lemma gcd_diff1: "gcd (m - n) n = gcd m n"
haftmann@58023
  1534
  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
haftmann@58023
  1535
haftmann@58023
  1536
lemma gcd_diff2: "gcd (n - m) n = gcd m n"
haftmann@58023
  1537
  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
haftmann@58023
  1538
haftmann@58023
  1539
lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
haftmann@58023
  1540
proof -
haftmann@58023
  1541
  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
haftmann@58023
  1542
  also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
haftmann@58023
  1543
  also have "\<dots> = 1" by (rule coprime_plus_one)
haftmann@58023
  1544
  finally show ?thesis .
haftmann@58023
  1545
qed
haftmann@58023
  1546
haftmann@60430
  1547
lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
haftmann@58023
  1548
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1549
haftmann@60430
  1550
lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
haftmann@58023
  1551
  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
haftmann@58023
  1552
haftmann@60430
  1553
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
haftmann@58023
  1554
  by (fact lcm_neg1)
haftmann@58023
  1555
haftmann@60430
  1556
lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
haftmann@58023
  1557
  by (fact lcm_neg2)
haftmann@58023
  1558
haftmann@60572
  1559
end
haftmann@58023
  1560
haftmann@58023
  1561
haftmann@60572
  1562
subsection \<open>Typical instances\<close>
haftmann@58023
  1563
haftmann@58023
  1564
instantiation nat :: euclidean_semiring
haftmann@58023
  1565
begin
haftmann@58023
  1566
haftmann@58023
  1567
definition [simp]:
haftmann@58023
  1568
  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
haftmann@58023
  1569
haftmann@58023
  1570
definition [simp]:
haftmann@60438
  1571
  "normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
haftmann@58023
  1572
haftmann@58023
  1573
instance proof
haftmann@59061
  1574
qed simp_all
haftmann@58023
  1575
haftmann@58023
  1576
end
haftmann@58023
  1577
haftmann@58023
  1578
instantiation int :: euclidean_ring
haftmann@58023
  1579
begin
haftmann@58023
  1580
haftmann@58023
  1581
definition [simp]:
haftmann@58023
  1582
  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
haftmann@58023
  1583
haftmann@58023
  1584
definition [simp]:
haftmann@60438
  1585
  "normalization_factor_int = (sgn :: int \<Rightarrow> int)"
haftmann@58023
  1586
wenzelm@60580
  1587
instance
wenzelm@60580
  1588
proof (default, goals)
wenzelm@60580
  1589
  case 2
wenzelm@60580
  1590
  then show ?case by (auto simp add: abs_mult nat_mult_distrib)
haftmann@58023
  1591
next
wenzelm@60580
  1592
  case 3
wenzelm@60580
  1593
  then show ?case by (simp add: zsgn_def)
haftmann@58023
  1594
next
wenzelm@60580
  1595
  case 5
wenzelm@60580
  1596
  then show ?case by (auto simp: zsgn_def)
haftmann@58023
  1597
next
wenzelm@60580
  1598
  case 6
wenzelm@60580
  1599
  then show ?case by (auto split: abs_split simp: zsgn_def)
haftmann@58023
  1600
qed (auto simp: sgn_times split: abs_split)
haftmann@58023
  1601
haftmann@58023
  1602
end
haftmann@58023
  1603
haftmann@60572
  1604
instantiation poly :: (field) euclidean_ring
haftmann@60571
  1605
begin
haftmann@60571
  1606
haftmann@60571
  1607
definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
haftmann@60571
  1608
  where "euclidean_size = (degree :: 'a poly \<Rightarrow> nat)"
haftmann@60571
  1609
haftmann@60571
  1610
definition normalization_factor_poly :: "'a poly \<Rightarrow> 'a poly"
haftmann@60571
  1611
  where "normalization_factor p = monom (coeff p (degree p)) 0"
haftmann@60571
  1612
haftmann@60571
  1613
instance
haftmann@60571
  1614
proof (default, unfold euclidean_size_poly_def normalization_factor_poly_def)
haftmann@60571
  1615
  fix p q :: "'a poly"
haftmann@60571
  1616
  assume "q \<noteq> 0" and "\<not> q dvd p"
haftmann@60571
  1617
  then show "degree (p mod q) < degree q"
haftmann@60571
  1618
    using degree_mod_less [of q p] by (simp add: mod_eq_0_iff_dvd)
haftmann@60571
  1619
next
haftmann@60571
  1620
  fix p q :: "'a poly"
haftmann@60571
  1621
  assume "q \<noteq> 0"
haftmann@60571
  1622
  from \<open>q \<noteq> 0\<close> show "degree p \<le> degree (p * q)"
haftmann@60571
  1623
    by (rule degree_mult_right_le)
haftmann@60571
  1624
  from \<open>q \<noteq> 0\<close> show "is_unit (monom (coeff q (degree q)) 0)"
haftmann@60571
  1625
    by (auto intro: is_unit_monom_0)
haftmann@60571
  1626
next
haftmann@60571
  1627
  fix p :: "'a poly"
haftmann@60571
  1628
  show "monom (coeff p (degree p)) 0 = p" if "is_unit p"
haftmann@60571
  1629
    using that by (fact is_unit_monom_trival)
haftmann@60571
  1630
next
haftmann@60571
  1631
  fix p q :: "'a poly"
haftmann@60571
  1632
  show "monom (coeff (p * q) (degree (p * q))) 0 =
haftmann@60571
  1633
    monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
haftmann@60571
  1634
    by (simp add: monom_0 coeff_degree_mult)
haftmann@60571
  1635
next
haftmann@60571
  1636
  show "monom (coeff 0 (degree 0)) 0 = 0"
haftmann@60571
  1637
    by simp
haftmann@60571
  1638
qed
haftmann@60571
  1639
haftmann@58023
  1640
end
haftmann@60571
  1641
haftmann@60571
  1642
end