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