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