src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Fri Jun 19 07:53:33 2015 +0200 (2015-06-19) changeset 60516 0826b7025d07 parent 60439 b765e08f8bc0 child 60517 f16e4fb20652 permissions -rw-r--r--
generalized some theorems about integral domains and moved to HOL theories
     1 (* Author: Manuel Eberl *)

     2

     3 section {* Abstract euclidean algorithm *}

     4

     5 theory Euclidean_Algorithm

     6 imports Complex_Main

     7 begin

     8

     9 context semidom_divide

    10 begin

    11

    12 lemma dvd_div_mult_self [simp]:

    13   "a dvd b \<Longrightarrow> b div a * a = b"

    14   by (cases "a = 0") (auto elim: dvdE simp add: ac_simps)

    15

    16 lemma dvd_mult_div_cancel [simp]:

    17   "a dvd b \<Longrightarrow> a * (b div a) = b"

    18   using dvd_div_mult_self [of a b] by (simp add: ac_simps)

    19

    20 lemma div_mult_swap:

    21   assumes "c dvd b"

    22   shows "a * (b div c) = (a * b) div c"

    23 proof (cases "c = 0")

    24   case True then show ?thesis by simp

    25 next

    26   case False from assms obtain d where "b = c * d" ..

    27   moreover from False have "a * divide (d * c) c = ((a * d) * c) div c"

    28     by simp

    29   ultimately show ?thesis by (simp add: ac_simps)

    30 qed

    31

    32 lemma dvd_div_mult:

    33   assumes "c dvd b"

    34   shows "b div c * a = (b * a) div c"

    35   using assms div_mult_swap [of c b a] by (simp add: ac_simps)

    36

    37

    38 text \<open>Units: invertible elements in a ring\<close>

    39

    40 abbreviation is_unit :: "'a \<Rightarrow> bool"

    41 where

    42   "is_unit a \<equiv> a dvd 1"

    43

    44 lemma not_is_unit_0 [simp]:

    45   "\<not> is_unit 0"

    46   by simp

    47

    48 lemma unit_imp_dvd [dest]:

    49   "is_unit b \<Longrightarrow> b dvd a"

    50   by (rule dvd_trans [of _ 1]) simp_all

    51

    52 lemma unit_dvdE:

    53   assumes "is_unit a"

    54   obtains c where "a \<noteq> 0" and "b = a * c"

    55 proof -

    56   from assms have "a dvd b" by auto

    57   then obtain c where "b = a * c" ..

    58   moreover from assms have "a \<noteq> 0" by auto

    59   ultimately show thesis using that by blast

    60 qed

    61

    62 lemma dvd_unit_imp_unit:

    63   "a dvd b \<Longrightarrow> is_unit b \<Longrightarrow> is_unit a"

    64   by (rule dvd_trans)

    65

    66 lemma unit_div_1_unit [simp, intro]:

    67   assumes "is_unit a"

    68   shows "is_unit (1 div a)"

    69 proof -

    70   from assms have "1 = 1 div a * a" by simp

    71   then show "is_unit (1 div a)" by (rule dvdI)

    72 qed

    73

    74 lemma is_unitE [elim?]:

    75   assumes "is_unit a"

    76   obtains b where "a \<noteq> 0" and "b \<noteq> 0"

    77     and "is_unit b" and "1 div a = b" and "1 div b = a"

    78     and "a * b = 1" and "c div a = c * b"

    79 proof (rule that)

    80   def b \<equiv> "1 div a"

    81   then show "1 div a = b" by simp

    82   from b_def is_unit a show "is_unit b" by simp

    83   from is_unit a and is_unit b show "a \<noteq> 0" and "b \<noteq> 0" by auto

    84   from b_def is_unit a show "a * b = 1" by simp

    85   then have "1 = a * b" ..

    86   with b_def b \<noteq> 0 show "1 div b = a" by simp

    87   from is_unit a have "a dvd c" ..

    88   then obtain d where "c = a * d" ..

    89   with a \<noteq> 0 a * b = 1 show "c div a = c * b"

    90     by (simp add: mult.assoc mult.left_commute [of a])

    91 qed

    92

    93 lemma unit_prod [intro]:

    94   "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a * b)"

    95   by (subst mult_1_left [of 1, symmetric]) (rule mult_dvd_mono)

    96

    97 lemma unit_div [intro]:

    98   "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a div b)"

    99   by (erule is_unitE [of b a]) (simp add: ac_simps unit_prod)

   100

   101 lemma mult_unit_dvd_iff:

   102   assumes "is_unit b"

   103   shows "a * b dvd c \<longleftrightarrow> a dvd c"

   104 proof

   105   assume "a * b dvd c"

   106   with assms show "a dvd c"

   107     by (simp add: dvd_mult_left)

   108 next

   109   assume "a dvd c"

   110   then obtain k where "c = a * k" ..

   111   with assms have "c = (a * b) * (1 div b * k)"

   112     by (simp add: mult_ac)

   113   then show "a * b dvd c" by (rule dvdI)

   114 qed

   115

   116 lemma dvd_mult_unit_iff:

   117   assumes "is_unit b"

   118   shows "a dvd c * b \<longleftrightarrow> a dvd c"

   119 proof

   120   assume "a dvd c * b"

   121   with assms have "c * b dvd c * (b * (1 div b))"

   122     by (subst mult_assoc [symmetric]) simp

   123   also from is_unit b have "b * (1 div b) = 1" by (rule is_unitE) simp

   124   finally have "c * b dvd c" by simp

   125   with a dvd c * b show "a dvd c" by (rule dvd_trans)

   126 next

   127   assume "a dvd c"

   128   then show "a dvd c * b" by simp

   129 qed

   130

   131 lemma div_unit_dvd_iff:

   132   "is_unit b \<Longrightarrow> a div b dvd c \<longleftrightarrow> a dvd c"

   133   by (erule is_unitE [of _ a]) (auto simp add: mult_unit_dvd_iff)

   134

   135 lemma dvd_div_unit_iff:

   136   "is_unit b \<Longrightarrow> a dvd c div b \<longleftrightarrow> a dvd c"

   137   by (erule is_unitE [of _ c]) (simp add: dvd_mult_unit_iff)

   138

   139 lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff

   140   dvd_mult_unit_iff dvd_div_unit_iff -- \<open>FIXME consider fact collection\<close>

   141

   142 lemma unit_mult_div_div [simp]:

   143   "is_unit a \<Longrightarrow> b * (1 div a) = b div a"

   144   by (erule is_unitE [of _ b]) simp

   145

   146 lemma unit_div_mult_self [simp]:

   147   "is_unit a \<Longrightarrow> b div a * a = b"

   148   by (rule dvd_div_mult_self) auto

   149

   150 lemma unit_div_1_div_1 [simp]:

   151   "is_unit a \<Longrightarrow> 1 div (1 div a) = a"

   152   by (erule is_unitE) simp

   153

   154 lemma unit_div_mult_swap:

   155   "is_unit c \<Longrightarrow> a * (b div c) = (a * b) div c"

   156   by (erule unit_dvdE [of _ b]) (simp add: mult.left_commute [of _ c])

   157

   158 lemma unit_div_commute:

   159   "is_unit b \<Longrightarrow> (a div b) * c = (a * c) div b"

   160   using unit_div_mult_swap [of b c a] by (simp add: ac_simps)

   161

   162 lemma unit_eq_div1:

   163   "is_unit b \<Longrightarrow> a div b = c \<longleftrightarrow> a = c * b"

   164   by (auto elim: is_unitE)

   165

   166 lemma unit_eq_div2:

   167   "is_unit b \<Longrightarrow> a = c div b \<longleftrightarrow> a * b = c"

   168   using unit_eq_div1 [of b c a] by auto

   169

   170 lemma unit_mult_left_cancel:

   171   assumes "is_unit a"

   172   shows "a * b = a * c \<longleftrightarrow> b = c" (is "?P \<longleftrightarrow> ?Q")

   173   using assms mult_cancel_left [of a b c] by auto

   174

   175 lemma unit_mult_right_cancel:

   176   "is_unit a \<Longrightarrow> b * a = c * a \<longleftrightarrow> b = c"

   177   using unit_mult_left_cancel [of a b c] by (auto simp add: ac_simps)

   178

   179 lemma unit_div_cancel:

   180   assumes "is_unit a"

   181   shows "b div a = c div a \<longleftrightarrow> b = c"

   182 proof -

   183   from assms have "is_unit (1 div a)" by simp

   184   then have "b * (1 div a) = c * (1 div a) \<longleftrightarrow> b = c"

   185     by (rule unit_mult_right_cancel)

   186   with assms show ?thesis by simp

   187 qed

   188

   189

   190 text \<open>Associated elements in a ring – an equivalence relation induced by the quasi-order divisibility \<close>

   191

   192 definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool"

   193 where

   194   "associated a b \<longleftrightarrow> a dvd b \<and> b dvd a"

   195

   196 lemma associatedI:

   197   "a dvd b \<Longrightarrow> b dvd a \<Longrightarrow> associated a b"

   198   by (simp add: associated_def)

   199

   200 lemma associatedD1:

   201   "associated a b \<Longrightarrow> a dvd b"

   202   by (simp add: associated_def)

   203

   204 lemma associatedD2:

   205   "associated a b \<Longrightarrow> b dvd a"

   206   by (simp add: associated_def)

   207

   208 lemma associated_refl [simp]:

   209   "associated a a"

   210   by (auto intro: associatedI)

   211

   212 lemma associated_sym:

   213   "associated b a \<longleftrightarrow> associated a b"

   214   by (auto intro: associatedI dest: associatedD1 associatedD2)

   215

   216 lemma associated_trans:

   217   "associated a b \<Longrightarrow> associated b c \<Longrightarrow> associated a c"

   218   by (auto intro: associatedI dvd_trans dest: associatedD1 associatedD2)

   219

   220 lemma equivp_associated:

   221   "equivp associated"

   222 proof (rule equivpI)

   223   show "reflp associated"

   224     by (rule reflpI) simp

   225   show "symp associated"

   226     by (rule sympI) (simp add: associated_sym)

   227   show "transp associated"

   228     by (rule transpI) (fact associated_trans)

   229 qed

   230

   231 lemma associated_0 [simp]:

   232   "associated 0 b \<longleftrightarrow> b = 0"

   233   "associated a 0 \<longleftrightarrow> a = 0"

   234   by (auto dest: associatedD1 associatedD2)

   235

   236 lemma associated_unit:

   237   "associated a b \<Longrightarrow> is_unit a \<Longrightarrow> is_unit b"

   238   using dvd_unit_imp_unit by (auto dest!: associatedD1 associatedD2)

   239

   240 lemma is_unit_associatedI:

   241   assumes "is_unit c" and "a = c * b"

   242   shows "associated a b"

   243 proof (rule associatedI)

   244   from a = c * b show "b dvd a" by auto

   245   from is_unit c obtain d where "c * d = 1" by (rule is_unitE)

   246   moreover from a = c * b have "d * a = d * (c * b)" by simp

   247   ultimately have "b = a * d" by (simp add: ac_simps)

   248   then show "a dvd b" ..

   249 qed

   250

   251 lemma associated_is_unitE:

   252   assumes "associated a b"

   253   obtains c where "is_unit c" and "a = c * b"

   254 proof (cases "b = 0")

   255   case True with assms have "is_unit 1" and "a = 1 * b" by simp_all

   256   with that show thesis .

   257 next

   258   case False

   259   from assms have "a dvd b" and "b dvd a" by (auto dest: associatedD1 associatedD2)

   260   then obtain c d where "b = a * d" and "a = b * c" by (blast elim: dvdE)

   261   then have "a = c * b" and "(c * d) * b = 1 * b" by (simp_all add: ac_simps)

   262   with False have "c * d = 1" using mult_cancel_right [of "c * d" b 1] by simp

   263   then have "is_unit c" by auto

   264   with a = c * b that show thesis by blast

   265 qed

   266

   267 lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff

   268   dvd_div_unit_iff unit_div_mult_swap unit_div_commute

   269   unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel

   270   unit_eq_div1 unit_eq_div2

   271

   272 end

   273

   274 lemma is_unit_int:

   275   "is_unit (k::int) \<longleftrightarrow> k = 1 \<or> k = - 1"

   276   by auto

   277

   278

   279 text {*

   280   A Euclidean semiring is a semiring upon which the Euclidean algorithm can be

   281   implemented. It must provide:

   282   \begin{itemize}

   283   \item division with remainder

   284   \item a size function such that @{term "size (a mod b) < size b"}

   285         for any @{term "b \<noteq> 0"}

   286   \item a normalization factor such that two associated numbers are equal iff

   287         they are the same when divd by their normalization factors.

   288   \end{itemize}

   289   The existence of these functions makes it possible to derive gcd and lcm functions

   290   for any Euclidean semiring.

   291 *}

   292 class euclidean_semiring = semiring_div +

   293   fixes euclidean_size :: "'a \<Rightarrow> nat"

   294   fixes normalization_factor :: "'a \<Rightarrow> 'a"

   295   assumes mod_size_less [simp]:

   296     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"

   297   assumes size_mult_mono:

   298     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"

   299   assumes normalization_factor_is_unit [intro,simp]:

   300     "a \<noteq> 0 \<Longrightarrow> is_unit (normalization_factor a)"

   301   assumes normalization_factor_mult: "normalization_factor (a * b) =

   302     normalization_factor a * normalization_factor b"

   303   assumes normalization_factor_unit: "is_unit a \<Longrightarrow> normalization_factor a = a"

   304   assumes normalization_factor_0 [simp]: "normalization_factor 0 = 0"

   305 begin

   306

   307 lemma normalization_factor_dvd [simp]:

   308   "a \<noteq> 0 \<Longrightarrow> normalization_factor a dvd b"

   309   by (rule unit_imp_dvd, simp)

   310

   311 lemma normalization_factor_1 [simp]:

   312   "normalization_factor 1 = 1"

   313   by (simp add: normalization_factor_unit)

   314

   315 lemma normalization_factor_0_iff [simp]:

   316   "normalization_factor a = 0 \<longleftrightarrow> a = 0"

   317 proof

   318   assume "normalization_factor a = 0"

   319   hence "\<not> is_unit (normalization_factor a)"

   320     by simp

   321   then show "a = 0" by auto

   322 qed simp

   323

   324 lemma normalization_factor_pow:

   325   "normalization_factor (a ^ n) = normalization_factor a ^ n"

   326   by (induct n) (simp_all add: normalization_factor_mult power_Suc2)

   327

   328 lemma normalization_correct [simp]:

   329   "normalization_factor (a div normalization_factor a) = (if a = 0 then 0 else 1)"

   330 proof (cases "a = 0", simp)

   331   assume "a \<noteq> 0"

   332   let ?nf = "normalization_factor"

   333   from normalization_factor_is_unit[OF a \<noteq> 0] have "?nf a \<noteq> 0"

   334     by auto

   335   have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)"

   336     by (simp add: normalization_factor_mult)

   337   also have "a div ?nf a * ?nf a = a" using a \<noteq> 0

   338     by simp

   339   also have "?nf (?nf a) = ?nf a" using a \<noteq> 0

   340     normalization_factor_is_unit normalization_factor_unit by simp

   341   finally have "normalization_factor (a div normalization_factor a) = 1"

   342     using ?nf a \<noteq> 0 by (metis div_mult_self2_is_id div_self)

   343   with a \<noteq> 0 show ?thesis by simp

   344 qed

   345

   346 lemma normalization_0_iff [simp]:

   347   "a div normalization_factor a = 0 \<longleftrightarrow> a = 0"

   348   by (cases "a = 0", simp, subst unit_eq_div1, blast, simp)

   349

   350 lemma mult_div_normalization [simp]:

   351   "b * (1 div normalization_factor a) = b div normalization_factor a"

   352   by (cases "a = 0") simp_all

   353

   354 lemma associated_iff_normed_eq:

   355   "associated a b \<longleftrightarrow> a div normalization_factor a = b div normalization_factor b"

   356 proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalization_0_iff, rule iffI)

   357   let ?nf = normalization_factor

   358   assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"

   359   hence "a = b * (?nf a div ?nf b)"

   360     apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)

   361     apply (subst div_mult_swap, simp, simp)

   362     done

   363   with a \<noteq> 0 b \<noteq> 0 have "\<exists>c. is_unit c \<and> a = c * b"

   364     by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)

   365   then obtain c where "is_unit c" and "a = c * b" by blast

   366   then show "associated a b" by (rule is_unit_associatedI)

   367 next

   368   let ?nf = normalization_factor

   369   assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"

   370   then obtain c where "is_unit c" and "a = c * b" by (blast elim: associated_is_unitE)

   371   then show "a div ?nf a = b div ?nf b"

   372     apply (simp only: a = c * b normalization_factor_mult normalization_factor_unit)

   373     apply (rule div_mult_mult1, force)

   374     done

   375   qed

   376

   377 lemma normed_associated_imp_eq:

   378   "associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b"

   379   by (simp add: associated_iff_normed_eq, elim disjE, simp_all)

   380

   381 lemmas normalization_factor_dvd_iff [simp] =

   382   unit_dvd_iff [OF normalization_factor_is_unit]

   383

   384 lemma euclidean_division:

   385   fixes a :: 'a and b :: 'a

   386   assumes "b \<noteq> 0"

   387   obtains s and t where "a = s * b + t"

   388     and "euclidean_size t < euclidean_size b"

   389 proof -

   390   from div_mod_equality[of a b 0]

   391      have "a = a div b * b + a mod b" by simp

   392   with that and assms show ?thesis by force

   393 qed

   394

   395 lemma dvd_euclidean_size_eq_imp_dvd:

   396   assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"

   397   shows "a dvd b"

   398 proof (subst dvd_eq_mod_eq_0, rule ccontr)

   399   assume "b mod a \<noteq> 0"

   400   from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)

   401   from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast

   402     with b mod a \<noteq> 0 have "c \<noteq> 0" by auto

   403   with b mod a = b * c have "euclidean_size (b mod a) \<ge> euclidean_size b"

   404       using size_mult_mono by force

   405   moreover from a \<noteq> 0 have "euclidean_size (b mod a) < euclidean_size a"

   406       using mod_size_less by blast

   407   ultimately show False using size_eq by simp

   408 qed

   409

   410 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"

   411 where

   412   "gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))"

   413   by (pat_completeness, simp)

   414 termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)

   415

   416 declare gcd_eucl.simps [simp del]

   417

   418 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"

   419 proof (induct a b rule: gcd_eucl.induct)

   420   case ("1" m n)

   421     then show ?case by (cases "n = 0") auto

   422 qed

   423

   424 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"

   425 where

   426   "lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))"

   427

   428   (* Somewhat complicated definition of Lcm that has the advantage of working

   429      for infinite sets as well *)

   430

   431 definition Lcm_eucl :: "'a set \<Rightarrow> 'a"

   432 where

   433   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then

   434      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =

   435        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)

   436        in l div normalization_factor l

   437       else 0)"

   438

   439 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"

   440 where

   441   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"

   442

   443 end

   444

   445 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

   446   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"

   447   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"

   448 begin

   449

   450 lemma gcd_red:

   451   "gcd a b = gcd b (a mod b)"

   452   by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)

   453

   454 lemma gcd_non_0:

   455   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"

   456   by (rule gcd_red)

   457

   458 lemma gcd_0_left:

   459   "gcd 0 a = a div normalization_factor a"

   460    by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)

   461

   462 lemma gcd_0:

   463   "gcd a 0 = a div normalization_factor a"

   464   by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)

   465

   466 lemma gcd_dvd1 [iff]: "gcd a b dvd a"

   467   and gcd_dvd2 [iff]: "gcd a b dvd b"

   468 proof (induct a b rule: gcd_eucl.induct)

   469   fix a b :: 'a

   470   assume IH1: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd b"

   471   assume IH2: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd (a mod b)"

   472

   473   have "gcd a b dvd a \<and> gcd a b dvd b"

   474   proof (cases "b = 0")

   475     case True

   476       then show ?thesis by (cases "a = 0", simp_all add: gcd_0)

   477   next

   478     case False

   479       with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)

   480   qed

   481   then show "gcd a b dvd a" "gcd a b dvd b" by simp_all

   482 qed

   483

   484 lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"

   485   by (rule dvd_trans, assumption, rule gcd_dvd1)

   486

   487 lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"

   488   by (rule dvd_trans, assumption, rule gcd_dvd2)

   489

   490 lemma gcd_greatest:

   491   fixes k a b :: 'a

   492   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"

   493 proof (induct a b rule: gcd_eucl.induct)

   494   case (1 a b)

   495   show ?case

   496     proof (cases "b = 0")

   497       assume "b = 0"

   498       with 1 show ?thesis by (cases "a = 0", simp_all add: gcd_0)

   499     next

   500       assume "b \<noteq> 0"

   501       with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)

   502     qed

   503 qed

   504

   505 lemma dvd_gcd_iff:

   506   "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"

   507   by (blast intro!: gcd_greatest intro: dvd_trans)

   508

   509 lemmas gcd_greatest_iff = dvd_gcd_iff

   510

   511 lemma gcd_zero [simp]:

   512   "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"

   513   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   514

   515 lemma normalization_factor_gcd [simp]:

   516   "normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")

   517 proof (induct a b rule: gcd_eucl.induct)

   518   fix a b :: 'a

   519   assume IH: "b \<noteq> 0 \<Longrightarrow> ?f b (a mod b) = ?g b (a mod b)"

   520   then show "?f a b = ?g a b" by (cases "b = 0", auto simp: gcd_non_0 gcd_0)

   521 qed

   522

   523 lemma gcdI:

   524   "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k)

   525     \<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b"

   526   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)

   527

   528 sublocale gcd!: abel_semigroup gcd

   529 proof

   530   fix a b c

   531   show "gcd (gcd a b) c = gcd a (gcd b c)"

   532   proof (rule gcdI)

   533     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all

   534     then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)

   535     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all

   536     hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)

   537     moreover have "gcd (gcd a b) c dvd c" by simp

   538     ultimately show "gcd (gcd a b) c dvd gcd b c"

   539       by (rule gcd_greatest)

   540     show "normalization_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"

   541       by auto

   542     fix l assume "l dvd a" and "l dvd gcd b c"

   543     with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]

   544       have "l dvd b" and "l dvd c" by blast+

   545     with l dvd a show "l dvd gcd (gcd a b) c"

   546       by (intro gcd_greatest)

   547   qed

   548 next

   549   fix a b

   550   show "gcd a b = gcd b a"

   551     by (rule gcdI) (simp_all add: gcd_greatest)

   552 qed

   553

   554 lemma gcd_unique: "d dvd a \<and> d dvd b \<and>

   555     normalization_factor d = (if d = 0 then 0 else 1) \<and>

   556     (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"

   557   by (rule, auto intro: gcdI simp: gcd_greatest)

   558

   559 lemma gcd_dvd_prod: "gcd a b dvd k * b"

   560   using mult_dvd_mono [of 1] by auto

   561

   562 lemma gcd_1_left [simp]: "gcd 1 a = 1"

   563   by (rule sym, rule gcdI, simp_all)

   564

   565 lemma gcd_1 [simp]: "gcd a 1 = 1"

   566   by (rule sym, rule gcdI, simp_all)

   567

   568 lemma gcd_proj2_if_dvd:

   569   "b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b"

   570   by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)

   571

   572 lemma gcd_proj1_if_dvd:

   573   "a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a"

   574   by (subst gcd.commute, simp add: gcd_proj2_if_dvd)

   575

   576 lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n"

   577 proof

   578   assume A: "gcd m n = m div normalization_factor m"

   579   show "m dvd n"

   580   proof (cases "m = 0")

   581     assume [simp]: "m \<noteq> 0"

   582     from A have B: "m = gcd m n * normalization_factor m"

   583       by (simp add: unit_eq_div2)

   584     show ?thesis by (subst B, simp add: mult_unit_dvd_iff)

   585   qed (insert A, simp)

   586 next

   587   assume "m dvd n"

   588   then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd)

   589 qed

   590

   591 lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m"

   592   by (subst gcd.commute, simp add: gcd_proj1_iff)

   593

   594 lemma gcd_mod1 [simp]:

   595   "gcd (a mod b) b = gcd a b"

   596   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)

   597

   598 lemma gcd_mod2 [simp]:

   599   "gcd a (b mod a) = gcd a b"

   600   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)

   601

   602 lemma normalization_factor_dvd' [simp]:

   603   "normalization_factor a dvd a"

   604   by (cases "a = 0", simp_all)

   605

   606 lemma gcd_mult_distrib':

   607   "k div normalization_factor k * gcd a b = gcd (k*a) (k*b)"

   608 proof (induct a b rule: gcd_eucl.induct)

   609   case (1 a b)

   610   show ?case

   611   proof (cases "b = 0")

   612     case True

   613     then show ?thesis by (simp add: normalization_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)

   614   next

   615     case False

   616     hence "k div normalization_factor k * gcd a b =  gcd (k * b) (k * (a mod b))"

   617       using 1 by (subst gcd_red, simp)

   618     also have "... = gcd (k * a) (k * b)"

   619       by (simp add: mult_mod_right gcd.commute)

   620     finally show ?thesis .

   621   qed

   622 qed

   623

   624 lemma gcd_mult_distrib:

   625   "k * gcd a b = gcd (k*a) (k*b) * normalization_factor k"

   626 proof-

   627   let ?nf = "normalization_factor"

   628   from gcd_mult_distrib'

   629     have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" ..

   630   also have "... = k * gcd a b div ?nf k"

   631     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)

   632   finally show ?thesis

   633     by simp

   634 qed

   635

   636 lemma euclidean_size_gcd_le1 [simp]:

   637   assumes "a \<noteq> 0"

   638   shows "euclidean_size (gcd a b) \<le> euclidean_size a"

   639 proof -

   640    have "gcd a b dvd a" by (rule gcd_dvd1)

   641    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast

   642    with a \<noteq> 0 show ?thesis by (subst (2) A, intro size_mult_mono) auto

   643 qed

   644

   645 lemma euclidean_size_gcd_le2 [simp]:

   646   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"

   647   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   648

   649 lemma euclidean_size_gcd_less1:

   650   assumes "a \<noteq> 0" and "\<not>a dvd b"

   651   shows "euclidean_size (gcd a b) < euclidean_size a"

   652 proof (rule ccontr)

   653   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"

   654   with a \<noteq> 0 have "euclidean_size (gcd a b) = euclidean_size a"

   655     by (intro le_antisym, simp_all)

   656   with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)

   657   hence "a dvd b" using dvd_gcd_D2 by blast

   658   with \<not>a dvd b show False by contradiction

   659 qed

   660

   661 lemma euclidean_size_gcd_less2:

   662   assumes "b \<noteq> 0" and "\<not>b dvd a"

   663   shows "euclidean_size (gcd a b) < euclidean_size b"

   664   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)

   665

   666 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"

   667   apply (rule gcdI)

   668   apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)

   669   apply (rule gcd_dvd2)

   670   apply (rule gcd_greatest, simp add: unit_simps, assumption)

   671   apply (subst normalization_factor_gcd, simp add: gcd_0)

   672   done

   673

   674 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"

   675   by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)

   676

   677 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"

   678   by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)

   679

   680 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"

   681   by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)

   682

   683 lemma gcd_idem: "gcd a a = a div normalization_factor a"

   684   by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)

   685

   686 lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"

   687   apply (rule gcdI)

   688   apply (simp add: ac_simps)

   689   apply (rule gcd_dvd2)

   690   apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)

   691   apply simp

   692   done

   693

   694 lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"

   695   apply (rule gcdI)

   696   apply simp

   697   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

   698   apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)

   699   apply simp

   700   done

   701

   702 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   703 proof

   704   fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"

   705     by (simp add: fun_eq_iff ac_simps)

   706 next

   707   fix a show "gcd a \<circ> gcd a = gcd a"

   708     by (simp add: fun_eq_iff gcd_left_idem)

   709 qed

   710

   711 lemma coprime_dvd_mult:

   712   assumes "gcd c b = 1" and "c dvd a * b"

   713   shows "c dvd a"

   714 proof -

   715   let ?nf = "normalization_factor"

   716   from assms gcd_mult_distrib [of a c b]

   717     have A: "a = gcd (a * c) (a * b) * ?nf a" by simp

   718   from c dvd a * b show ?thesis by (subst A, simp_all add: gcd_greatest)

   719 qed

   720

   721 lemma coprime_dvd_mult_iff:

   722   "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"

   723   by (rule, rule coprime_dvd_mult, simp_all)

   724

   725 lemma gcd_dvd_antisym:

   726   "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"

   727 proof (rule gcdI)

   728   assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"

   729   have "gcd c d dvd c" by simp

   730   with A show "gcd a b dvd c" by (rule dvd_trans)

   731   have "gcd c d dvd d" by simp

   732   with A show "gcd a b dvd d" by (rule dvd_trans)

   733   show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"

   734     by simp

   735   fix l assume "l dvd c" and "l dvd d"

   736   hence "l dvd gcd c d" by (rule gcd_greatest)

   737   from this and B show "l dvd gcd a b" by (rule dvd_trans)

   738 qed

   739

   740 lemma gcd_mult_cancel:

   741   assumes "gcd k n = 1"

   742   shows "gcd (k * m) n = gcd m n"

   743 proof (rule gcd_dvd_antisym)

   744   have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)

   745   also note gcd k n = 1

   746   finally have "gcd (gcd (k * m) n) k = 1" by simp

   747   hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)

   748   moreover have "gcd (k * m) n dvd n" by simp

   749   ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)

   750   have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all

   751   then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)

   752 qed

   753

   754 lemma coprime_crossproduct:

   755   assumes [simp]: "gcd a d = 1" "gcd b c = 1"

   756   shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")

   757 proof

   758   assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)

   759 next

   760   assume ?lhs

   761   from ?lhs have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)

   762   hence "a dvd b" by (simp add: coprime_dvd_mult_iff)

   763   moreover from ?lhs have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)

   764   hence "b dvd a" by (simp add: coprime_dvd_mult_iff)

   765   moreover from ?lhs have "c dvd d * b"

   766     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)

   767   hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)

   768   moreover from ?lhs have "d dvd c * a"

   769     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)

   770   hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)

   771   ultimately show ?rhs unfolding associated_def by simp

   772 qed

   773

   774 lemma gcd_add1 [simp]:

   775   "gcd (m + n) n = gcd m n"

   776   by (cases "n = 0", simp_all add: gcd_non_0)

   777

   778 lemma gcd_add2 [simp]:

   779   "gcd m (m + n) = gcd m n"

   780   using gcd_add1 [of n m] by (simp add: ac_simps)

   781

   782 lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"

   783   by (subst gcd.commute, subst gcd_red, simp)

   784

   785 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"

   786   by (rule sym, rule gcdI, simp_all)

   787

   788 lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"

   789   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   790

   791 lemma div_gcd_coprime:

   792   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"

   793   defines [simp]: "d \<equiv> gcd a b"

   794   defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"

   795   shows "gcd a' b' = 1"

   796 proof (rule coprimeI)

   797   fix l assume "l dvd a'" "l dvd b'"

   798   then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast

   799   moreover have "a = a' * d" "b = b' * d" by simp_all

   800   ultimately have "a = (l * d) * s" "b = (l * d) * t"

   801     by (simp_all only: ac_simps)

   802   hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)

   803   hence "l*d dvd d" by (simp add: gcd_greatest)

   804   then obtain u where "d = l * d * u" ..

   805   then have "d * (l * u) = d" by (simp add: ac_simps)

   806   moreover from nz have "d \<noteq> 0" by simp

   807   with div_mult_self1_is_id have "d * (l * u) div d = l * u" .

   808   ultimately have "1 = l * u"

   809     using d \<noteq> 0 by simp

   810   then show "l dvd 1" ..

   811 qed

   812

   813 lemma coprime_mult:

   814   assumes da: "gcd d a = 1" and db: "gcd d b = 1"

   815   shows "gcd d (a * b) = 1"

   816   apply (subst gcd.commute)

   817   using da apply (subst gcd_mult_cancel)

   818   apply (subst gcd.commute, assumption)

   819   apply (subst gcd.commute, rule db)

   820   done

   821

   822 lemma coprime_lmult:

   823   assumes dab: "gcd d (a * b) = 1"

   824   shows "gcd d a = 1"

   825 proof (rule coprimeI)

   826   fix l assume "l dvd d" and "l dvd a"

   827   hence "l dvd a * b" by simp

   828   with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)

   829 qed

   830

   831 lemma coprime_rmult:

   832   assumes dab: "gcd d (a * b) = 1"

   833   shows "gcd d b = 1"

   834 proof (rule coprimeI)

   835   fix l assume "l dvd d" and "l dvd b"

   836   hence "l dvd a * b" by simp

   837   with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)

   838 qed

   839

   840 lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"

   841   using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast

   842

   843 lemma gcd_coprime:

   844   assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"

   845   shows "gcd a' b' = 1"

   846 proof -

   847   from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp

   848   with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .

   849   also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+

   850   also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+

   851   finally show ?thesis .

   852 qed

   853

   854 lemma coprime_power:

   855   assumes "0 < n"

   856   shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"

   857 using assms proof (induct n)

   858   case (Suc n) then show ?case

   859     by (cases n) (simp_all add: coprime_mul_eq)

   860 qed simp

   861

   862 lemma gcd_coprime_exists:

   863   assumes nz: "gcd a b \<noteq> 0"

   864   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"

   865   apply (rule_tac x = "a div gcd a b" in exI)

   866   apply (rule_tac x = "b div gcd a b" in exI)

   867   apply (insert nz, auto intro: div_gcd_coprime)

   868   done

   869

   870 lemma coprime_exp:

   871   "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"

   872   by (induct n, simp_all add: coprime_mult)

   873

   874 lemma coprime_exp2 [intro]:

   875   "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"

   876   apply (rule coprime_exp)

   877   apply (subst gcd.commute)

   878   apply (rule coprime_exp)

   879   apply (subst gcd.commute)

   880   apply assumption

   881   done

   882

   883 lemma gcd_exp:

   884   "gcd (a^n) (b^n) = (gcd a b) ^ n"

   885 proof (cases "a = 0 \<and> b = 0")

   886   assume "a = 0 \<and> b = 0"

   887   then show ?thesis by (cases n, simp_all add: gcd_0_left)

   888 next

   889   assume A: "\<not>(a = 0 \<and> b = 0)"

   890   hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"

   891     using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)

   892   hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp

   893   also note gcd_mult_distrib

   894   also have "normalization_factor ((gcd a b)^n) = 1"

   895     by (simp add: normalization_factor_pow A)

   896   also have "(gcd a b)^n * (a div gcd a b)^n = a^n"

   897     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)

   898   also have "(gcd a b)^n * (b div gcd a b)^n = b^n"

   899     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)

   900   finally show ?thesis by simp

   901 qed

   902

   903 lemma coprime_common_divisor:

   904   "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"

   905   apply (subgoal_tac "a dvd gcd a b")

   906   apply simp

   907   apply (erule (1) gcd_greatest)

   908   done

   909

   910 lemma division_decomp:

   911   assumes dc: "a dvd b * c"

   912   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"

   913 proof (cases "gcd a b = 0")

   914   assume "gcd a b = 0"

   915   hence "a = 0 \<and> b = 0" by simp

   916   hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp

   917   then show ?thesis by blast

   918 next

   919   let ?d = "gcd a b"

   920   assume "?d \<noteq> 0"

   921   from gcd_coprime_exists[OF this]

   922     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"

   923     by blast

   924   from ab'(1) have "a' dvd a" unfolding dvd_def by blast

   925   with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp

   926   from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp

   927   hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)

   928   with ?d \<noteq> 0 have "a' dvd b' * c" by simp

   929   with coprime_dvd_mult[OF ab'(3)]

   930     have "a' dvd c" by (subst (asm) ac_simps, blast)

   931   with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)

   932   then show ?thesis by blast

   933 qed

   934

   935 lemma pow_divs_pow:

   936   assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"

   937   shows "a dvd b"

   938 proof (cases "gcd a b = 0")

   939   assume "gcd a b = 0"

   940   then show ?thesis by simp

   941 next

   942   let ?d = "gcd a b"

   943   assume "?d \<noteq> 0"

   944   from n obtain m where m: "n = Suc m" by (cases n, simp_all)

   945   from ?d \<noteq> 0 have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)

   946   from gcd_coprime_exists[OF ?d \<noteq> 0]

   947     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"

   948     by blast

   949   from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"

   950     by (simp add: ab'(1,2)[symmetric])

   951   hence "?d^n * a'^n dvd ?d^n * b'^n"

   952     by (simp only: power_mult_distrib ac_simps)

   953   with zn have "a'^n dvd b'^n" by simp

   954   hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)

   955   hence "a' dvd b'^m * b'" by (simp add: m ac_simps)

   956   with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]

   957     have "a' dvd b'" by (subst (asm) ac_simps, blast)

   958   hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)

   959   with ab'(1,2) show ?thesis by simp

   960 qed

   961

   962 lemma pow_divs_eq [simp]:

   963   "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"

   964   by (auto intro: pow_divs_pow dvd_power_same)

   965

   966 lemma divs_mult:

   967   assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"

   968   shows "m * n dvd r"

   969 proof -

   970   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"

   971     unfolding dvd_def by blast

   972   from mr n' have "m dvd n'*n" by (simp add: ac_simps)

   973   hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp

   974   then obtain k where k: "n' = m*k" unfolding dvd_def by blast

   975   with n' have "r = m * n * k" by (simp add: mult_ac)

   976   then show ?thesis unfolding dvd_def by blast

   977 qed

   978

   979 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"

   980   by (subst add_commute, simp)

   981

   982 lemma setprod_coprime [rule_format]:

   983   "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"

   984   apply (cases "finite A")

   985   apply (induct set: finite)

   986   apply (auto simp add: gcd_mult_cancel)

   987   done

   988

   989 lemma coprime_divisors:

   990   assumes "d dvd a" "e dvd b" "gcd a b = 1"

   991   shows "gcd d e = 1"

   992 proof -

   993   from assms obtain k l where "a = d * k" "b = e * l"

   994     unfolding dvd_def by blast

   995   with assms have "gcd (d * k) (e * l) = 1" by simp

   996   hence "gcd (d * k) e = 1" by (rule coprime_lmult)

   997   also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)

   998   finally have "gcd e d = 1" by (rule coprime_lmult)

   999   then show ?thesis by (simp add: ac_simps)

  1000 qed

  1001

  1002 lemma invertible_coprime:

  1003   assumes "a * b mod m = 1"

  1004   shows "coprime a m"

  1005 proof -

  1006   from assms have "coprime m (a * b mod m)"

  1007     by simp

  1008   then have "coprime m (a * b)"

  1009     by simp

  1010   then have "coprime m a"

  1011     by (rule coprime_lmult)

  1012   then show ?thesis

  1013     by (simp add: ac_simps)

  1014 qed

  1015

  1016 lemma lcm_gcd:

  1017   "lcm a b = a * b div (gcd a b * normalization_factor (a*b))"

  1018   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

  1019

  1020 lemma lcm_gcd_prod:

  1021   "lcm a b * gcd a b = a * b div normalization_factor (a*b)"

  1022 proof (cases "a * b = 0")

  1023   let ?nf = normalization_factor

  1024   assume "a * b \<noteq> 0"

  1025   hence "gcd a b \<noteq> 0" by simp

  1026   from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"

  1027     by (simp add: mult_ac)

  1028   also from a * b \<noteq> 0 have "... = a * b div ?nf (a*b)"

  1029     by (simp add: div_mult_swap mult.commute)

  1030   finally show ?thesis .

  1031 qed (auto simp add: lcm_gcd)

  1032

  1033 lemma lcm_dvd1 [iff]:

  1034   "a dvd lcm a b"

  1035 proof (cases "a*b = 0")

  1036   assume "a * b \<noteq> 0"

  1037   hence "gcd a b \<noteq> 0" by simp

  1038   let ?c = "1 div normalization_factor (a * b)"

  1039   from a * b \<noteq> 0 have [simp]: "is_unit (normalization_factor (a * b))" by simp

  1040   from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"

  1041     by (simp add: div_mult_swap unit_div_commute)

  1042   hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp

  1043   with gcd a b \<noteq> 0 have "lcm a b = a * ?c * b div gcd a b"

  1044     by (subst (asm) div_mult_self2_is_id, simp_all)

  1045   also have "... = a * (?c * b div gcd a b)"

  1046     by (metis div_mult_swap gcd_dvd2 mult_assoc)

  1047   finally show ?thesis by (rule dvdI)

  1048 qed (auto simp add: lcm_gcd)

  1049

  1050 lemma lcm_least:

  1051   "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"

  1052 proof (cases "k = 0")

  1053   let ?nf = normalization_factor

  1054   assume "k \<noteq> 0"

  1055   hence "is_unit (?nf k)" by simp

  1056   hence "?nf k \<noteq> 0" by (metis not_is_unit_0)

  1057   assume A: "a dvd k" "b dvd k"

  1058   hence "gcd a b \<noteq> 0" using k \<noteq> 0 by auto

  1059   from A obtain r s where ar: "k = a * r" and bs: "k = b * s"

  1060     unfolding dvd_def by blast

  1061   with k \<noteq> 0 have "r * s \<noteq> 0"

  1062     by auto (drule sym [of 0], simp)

  1063   hence "is_unit (?nf (r * s))" by simp

  1064   let ?c = "?nf k div ?nf (r*s)"

  1065   from is_unit (?nf k) and is_unit (?nf (r * s)) have "is_unit ?c" by (rule unit_div)

  1066   hence "?c \<noteq> 0" using not_is_unit_0 by fast

  1067   from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"

  1068     by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)

  1069   also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"

  1070     by (subst (3) k = a * r, subst (3) k = b * s, simp add: algebra_simps)

  1071   also have "... = ?c * r*s * k * gcd a b" using r * s \<noteq> 0

  1072     by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)

  1073   finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"

  1074     by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)

  1075   hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"

  1076     by (simp add: algebra_simps)

  1077   hence "?c * k * gcd a b = a * b * gcd s r" using r * s \<noteq> 0

  1078     by (metis div_mult_self2_is_id)

  1079   also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"

  1080     by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')

  1081   also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"

  1082     by (simp add: algebra_simps)

  1083   finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using gcd a b \<noteq> 0

  1084     by (metis mult.commute div_mult_self2_is_id)

  1085   hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using ?c \<noteq> 0

  1086     by (metis div_mult_self2_is_id mult_assoc)

  1087   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using is_unit ?c

  1088     by (simp add: unit_simps)

  1089   finally show ?thesis by (rule dvdI)

  1090 qed simp

  1091

  1092 lemma lcm_zero:

  1093   "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"

  1094 proof -

  1095   let ?nf = normalization_factor

  1096   {

  1097     assume "a \<noteq> 0" "b \<noteq> 0"

  1098     hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)

  1099     moreover from a \<noteq> 0 and b \<noteq> 0 have "gcd a b \<noteq> 0" by simp

  1100     ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)

  1101   } moreover {

  1102     assume "a = 0 \<or> b = 0"

  1103     hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)

  1104   }

  1105   ultimately show ?thesis by blast

  1106 qed

  1107

  1108 lemmas lcm_0_iff = lcm_zero

  1109

  1110 lemma gcd_lcm:

  1111   assumes "lcm a b \<noteq> 0"

  1112   shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))"

  1113 proof-

  1114   from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)

  1115   let ?c = "normalization_factor (a * b)"

  1116   from lcm a b \<noteq> 0 have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)

  1117   hence "is_unit ?c" by simp

  1118   from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"

  1119     by (subst (2) div_mult_self2_is_id[OF lcm a b \<noteq> 0, symmetric], simp add: mult_ac)

  1120   also from is_unit ?c have "... = a * b div (lcm a b * ?c)"

  1121     by (metis ?c \<noteq> 0 div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd')

  1122   finally show ?thesis .

  1123 qed

  1124

  1125 lemma normalization_factor_lcm [simp]:

  1126   "normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"

  1127 proof (cases "a = 0 \<or> b = 0")

  1128   case True then show ?thesis

  1129     by (auto simp add: lcm_gcd)

  1130 next

  1131   case False

  1132   let ?nf = normalization_factor

  1133   from lcm_gcd_prod[of a b]

  1134     have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"

  1135     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)

  1136   also have "... = (if a*b = 0 then 0 else 1)"

  1137     by simp

  1138   finally show ?thesis using False by simp

  1139 qed

  1140

  1141 lemma lcm_dvd2 [iff]: "b dvd lcm a b"

  1142   using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)

  1143

  1144 lemma lcmI:

  1145   "\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l;

  1146     normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b"

  1147   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)

  1148

  1149 sublocale lcm!: abel_semigroup lcm

  1150 proof

  1151   fix a b c

  1152   show "lcm (lcm a b) c = lcm a (lcm b c)"

  1153   proof (rule lcmI)

  1154     have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all

  1155     then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)

  1156

  1157     have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all

  1158     hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)

  1159     moreover have "c dvd lcm (lcm a b) c" by simp

  1160     ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)

  1161

  1162     fix l assume "a dvd l" and "lcm b c dvd l"

  1163     have "b dvd lcm b c" by simp

  1164     from this and lcm b c dvd l have "b dvd l" by (rule dvd_trans)

  1165     have "c dvd lcm b c" by simp

  1166     from this and lcm b c dvd l have "c dvd l" by (rule dvd_trans)

  1167     from a dvd l and b dvd l have "lcm a b dvd l" by (rule lcm_least)

  1168     from this and c dvd l show "lcm (lcm a b) c dvd l" by (rule lcm_least)

  1169   qed (simp add: lcm_zero)

  1170 next

  1171   fix a b

  1172   show "lcm a b = lcm b a"

  1173     by (simp add: lcm_gcd ac_simps)

  1174 qed

  1175

  1176 lemma dvd_lcm_D1:

  1177   "lcm m n dvd k \<Longrightarrow> m dvd k"

  1178   by (rule dvd_trans, rule lcm_dvd1, assumption)

  1179

  1180 lemma dvd_lcm_D2:

  1181   "lcm m n dvd k \<Longrightarrow> n dvd k"

  1182   by (rule dvd_trans, rule lcm_dvd2, assumption)

  1183

  1184 lemma gcd_dvd_lcm [simp]:

  1185   "gcd a b dvd lcm a b"

  1186   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

  1187

  1188 lemma lcm_1_iff:

  1189   "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"

  1190 proof

  1191   assume "lcm a b = 1"

  1192   then show "is_unit a \<and> is_unit b" by auto

  1193 next

  1194   assume "is_unit a \<and> is_unit b"

  1195   hence "a dvd 1" and "b dvd 1" by simp_all

  1196   hence "is_unit (lcm a b)" by (rule lcm_least)

  1197   hence "lcm a b = normalization_factor (lcm a b)"

  1198     by (subst normalization_factor_unit, simp_all)

  1199   also have "\<dots> = 1" using is_unit a \<and> is_unit b

  1200     by auto

  1201   finally show "lcm a b = 1" .

  1202 qed

  1203

  1204 lemma lcm_0_left [simp]:

  1205   "lcm 0 a = 0"

  1206   by (rule sym, rule lcmI, simp_all)

  1207

  1208 lemma lcm_0 [simp]:

  1209   "lcm a 0 = 0"

  1210   by (rule sym, rule lcmI, simp_all)

  1211

  1212 lemma lcm_unique:

  1213   "a dvd d \<and> b dvd d \<and>

  1214   normalization_factor d = (if d = 0 then 0 else 1) \<and>

  1215   (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"

  1216   by (rule, auto intro: lcmI simp: lcm_least lcm_zero)

  1217

  1218 lemma dvd_lcm_I1 [simp]:

  1219   "k dvd m \<Longrightarrow> k dvd lcm m n"

  1220   by (metis lcm_dvd1 dvd_trans)

  1221

  1222 lemma dvd_lcm_I2 [simp]:

  1223   "k dvd n \<Longrightarrow> k dvd lcm m n"

  1224   by (metis lcm_dvd2 dvd_trans)

  1225

  1226 lemma lcm_1_left [simp]:

  1227   "lcm 1 a = a div normalization_factor a"

  1228   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)

  1229

  1230 lemma lcm_1_right [simp]:

  1231   "lcm a 1 = a div normalization_factor a"

  1232   using lcm_1_left [of a] by (simp add: ac_simps)

  1233

  1234 lemma lcm_coprime:

  1235   "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)"

  1236   by (subst lcm_gcd) simp

  1237

  1238 lemma lcm_proj1_if_dvd:

  1239   "b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a"

  1240   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)

  1241

  1242 lemma lcm_proj2_if_dvd:

  1243   "a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b"

  1244   using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)

  1245

  1246 lemma lcm_proj1_iff:

  1247   "lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m"

  1248 proof

  1249   assume A: "lcm m n = m div normalization_factor m"

  1250   show "n dvd m"

  1251   proof (cases "m = 0")

  1252     assume [simp]: "m \<noteq> 0"

  1253     from A have B: "m = lcm m n * normalization_factor m"

  1254       by (simp add: unit_eq_div2)

  1255     show ?thesis by (subst B, simp)

  1256   qed simp

  1257 next

  1258   assume "n dvd m"

  1259   then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd)

  1260 qed

  1261

  1262 lemma lcm_proj2_iff:

  1263   "lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n"

  1264   using lcm_proj1_iff [of n m] by (simp add: ac_simps)

  1265

  1266 lemma euclidean_size_lcm_le1:

  1267   assumes "a \<noteq> 0" and "b \<noteq> 0"

  1268   shows "euclidean_size a \<le> euclidean_size (lcm a b)"

  1269 proof -

  1270   have "a dvd lcm a b" by (rule lcm_dvd1)

  1271   then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast

  1272   with a \<noteq> 0 and b \<noteq> 0 have "c \<noteq> 0" by (auto simp: lcm_zero)

  1273   then show ?thesis by (subst A, intro size_mult_mono)

  1274 qed

  1275

  1276 lemma euclidean_size_lcm_le2:

  1277   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"

  1278   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)

  1279

  1280 lemma euclidean_size_lcm_less1:

  1281   assumes "b \<noteq> 0" and "\<not>b dvd a"

  1282   shows "euclidean_size a < euclidean_size (lcm a b)"

  1283 proof (rule ccontr)

  1284   from assms have "a \<noteq> 0" by auto

  1285   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"

  1286   with a \<noteq> 0 and b \<noteq> 0 have "euclidean_size (lcm a b) = euclidean_size a"

  1287     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

  1288   with assms have "lcm a b dvd a"

  1289     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)

  1290   hence "b dvd a" by (rule dvd_lcm_D2)

  1291   with \<not>b dvd a show False by contradiction

  1292 qed

  1293

  1294 lemma euclidean_size_lcm_less2:

  1295   assumes "a \<noteq> 0" and "\<not>a dvd b"

  1296   shows "euclidean_size b < euclidean_size (lcm a b)"

  1297   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)

  1298

  1299 lemma lcm_mult_unit1:

  1300   "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"

  1301   apply (rule lcmI)

  1302   apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)

  1303   apply (rule lcm_dvd2)

  1304   apply (rule lcm_least, simp add: unit_simps, assumption)

  1305   apply (subst normalization_factor_lcm, simp add: lcm_zero)

  1306   done

  1307

  1308 lemma lcm_mult_unit2:

  1309   "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"

  1310   using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)

  1311

  1312 lemma lcm_div_unit1:

  1313   "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"

  1314   by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1)

  1315

  1316 lemma lcm_div_unit2:

  1317   "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"

  1318   by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)

  1319

  1320 lemma lcm_left_idem:

  1321   "lcm a (lcm a b) = lcm a b"

  1322   apply (rule lcmI)

  1323   apply simp

  1324   apply (subst lcm.assoc [symmetric], rule lcm_dvd2)

  1325   apply (rule lcm_least, assumption)

  1326   apply (erule (1) lcm_least)

  1327   apply (auto simp: lcm_zero)

  1328   done

  1329

  1330 lemma lcm_right_idem:

  1331   "lcm (lcm a b) b = lcm a b"

  1332   apply (rule lcmI)

  1333   apply (subst lcm.assoc, rule lcm_dvd1)

  1334   apply (rule lcm_dvd2)

  1335   apply (rule lcm_least, erule (1) lcm_least, assumption)

  1336   apply (auto simp: lcm_zero)

  1337   done

  1338

  1339 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

  1340 proof

  1341   fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"

  1342     by (simp add: fun_eq_iff ac_simps)

  1343 next

  1344   fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def

  1345     by (intro ext, simp add: lcm_left_idem)

  1346 qed

  1347

  1348 lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"

  1349   and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'"

  1350   and normalization_factor_Lcm [simp]:

  1351           "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"

  1352 proof -

  1353   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>

  1354     normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)

  1355   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")

  1356     case False

  1357     hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)

  1358     with False show ?thesis by auto

  1359   next

  1360     case True

  1361     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

  1362     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1363     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1364     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1365       apply (subst n_def)

  1366       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])

  1367       apply (rule exI[of _ l\<^sub>0])

  1368       apply (simp add: l\<^sub>0_props)

  1369       done

  1370     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n"

  1371       unfolding l_def by simp_all

  1372     {

  1373       fix l' assume "\<forall>a\<in>A. a dvd l'"

  1374       with \<forall>a\<in>A. a dvd l have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest)

  1375       moreover from l \<noteq> 0 have "gcd l l' \<noteq> 0" by simp

  1376       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"

  1377         by (intro exI[of _ "gcd l l'"], auto)

  1378       hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)

  1379       moreover have "euclidean_size (gcd l l') \<le> n"

  1380       proof -

  1381         have "gcd l l' dvd l" by simp

  1382         then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast

  1383         with l \<noteq> 0 have "a \<noteq> 0" by auto

  1384         hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"

  1385           by (rule size_mult_mono)

  1386         also have "gcd l l' * a = l" using l = gcd l l' * a ..

  1387         also note euclidean_size l = n

  1388         finally show "euclidean_size (gcd l l') \<le> n" .

  1389       qed

  1390       ultimately have "euclidean_size l = euclidean_size (gcd l l')"

  1391         by (intro le_antisym, simp_all add: euclidean_size l = n)

  1392       with l \<noteq> 0 have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)

  1393       hence "l dvd l'" by (blast dest: dvd_gcd_D2)

  1394     }

  1395

  1396     with (\<forall>a\<in>A. a dvd l) and normalization_factor_is_unit[OF l \<noteq> 0] and l \<noteq> 0

  1397       have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and>

  1398         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and>

  1399         normalization_factor (l div normalization_factor l) =

  1400         (if l div normalization_factor l = 0 then 0 else 1)"

  1401       by (auto simp: unit_simps)

  1402     also from True have "l div normalization_factor l = Lcm A"

  1403       by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)

  1404     finally show ?thesis .

  1405   qed

  1406   note A = this

  1407

  1408   {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}

  1409   {fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast}

  1410   from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast

  1411 qed

  1412

  1413 lemma LcmI:

  1414   "(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>

  1415       normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"

  1416   by (intro normed_associated_imp_eq)

  1417     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)

  1418

  1419 lemma Lcm_subset:

  1420   "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"

  1421   by (blast intro: Lcm_dvd dvd_Lcm)

  1422

  1423 lemma Lcm_Un:

  1424   "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"

  1425   apply (rule lcmI)

  1426   apply (blast intro: Lcm_subset)

  1427   apply (blast intro: Lcm_subset)

  1428   apply (intro Lcm_dvd ballI, elim UnE)

  1429   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1430   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1431   apply simp

  1432   done

  1433

  1434 lemma Lcm_1_iff:

  1435   "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"

  1436 proof

  1437   assume "Lcm A = 1"

  1438   then show "\<forall>a\<in>A. is_unit a" by auto

  1439 qed (rule LcmI [symmetric], auto)

  1440

  1441 lemma Lcm_no_units:

  1442   "Lcm A = Lcm (A - {a. is_unit a})"

  1443 proof -

  1444   have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast

  1445   hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"

  1446     by (simp add: Lcm_Un[symmetric])

  1447   also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)

  1448   finally show ?thesis by simp

  1449 qed

  1450

  1451 lemma Lcm_empty [simp]:

  1452   "Lcm {} = 1"

  1453   by (simp add: Lcm_1_iff)

  1454

  1455 lemma Lcm_eq_0 [simp]:

  1456   "0 \<in> A \<Longrightarrow> Lcm A = 0"

  1457   by (drule dvd_Lcm) simp

  1458

  1459 lemma Lcm0_iff':

  1460   "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"

  1461 proof

  1462   assume "Lcm A = 0"

  1463   show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"

  1464   proof

  1465     assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"

  1466     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

  1467     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1468     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1469     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"

  1470       apply (subst n_def)

  1471       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])

  1472       apply (rule exI[of _ l\<^sub>0])

  1473       apply (simp add: l\<^sub>0_props)

  1474       done

  1475     from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all

  1476     hence "l div normalization_factor l \<noteq> 0" by simp

  1477     also from ex have "l div normalization_factor l = Lcm A"

  1478        by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)

  1479     finally show False using Lcm A = 0 by contradiction

  1480   qed

  1481 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1482

  1483 lemma Lcm0_iff [simp]:

  1484   "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"

  1485 proof -

  1486   assume "finite A"

  1487   have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)

  1488   moreover {

  1489     assume "0 \<notin> A"

  1490     hence "\<Prod>A \<noteq> 0"

  1491       apply (induct rule: finite_induct[OF finite A])

  1492       apply simp

  1493       apply (subst setprod.insert, assumption, assumption)

  1494       apply (rule no_zero_divisors)

  1495       apply blast+

  1496       done

  1497     moreover from finite A have "\<forall>a\<in>A. a dvd \<Prod>A" by blast

  1498     ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast

  1499     with Lcm0_iff' have "Lcm A \<noteq> 0" by simp

  1500   }

  1501   ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast

  1502 qed

  1503

  1504 lemma Lcm_no_multiple:

  1505   "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"

  1506 proof -

  1507   assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"

  1508   hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast

  1509   then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1510 qed

  1511

  1512 lemma Lcm_insert [simp]:

  1513   "Lcm (insert a A) = lcm a (Lcm A)"

  1514 proof (rule lcmI)

  1515   fix l assume "a dvd l" and "Lcm A dvd l"

  1516   hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)

  1517   with a dvd l show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)

  1518 qed (auto intro: Lcm_dvd dvd_Lcm)

  1519

  1520 lemma Lcm_finite:

  1521   assumes "finite A"

  1522   shows "Lcm A = Finite_Set.fold lcm 1 A"

  1523   by (induct rule: finite.induct[OF finite A])

  1524     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])

  1525

  1526 lemma Lcm_set [code_unfold]:

  1527   "Lcm (set xs) = fold lcm xs 1"

  1528   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)

  1529

  1530 lemma Lcm_singleton [simp]:

  1531   "Lcm {a} = a div normalization_factor a"

  1532   by simp

  1533

  1534 lemma Lcm_2 [simp]:

  1535   "Lcm {a,b} = lcm a b"

  1536   by (simp only: Lcm_insert Lcm_empty lcm_1_right)

  1537     (cases "b = 0", simp, rule lcm_div_unit2, simp)

  1538

  1539 lemma Lcm_coprime:

  1540   assumes "finite A" and "A \<noteq> {}"

  1541   assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"

  1542   shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"

  1543 using assms proof (induct rule: finite_ne_induct)

  1544   case (insert a A)

  1545   have "Lcm (insert a A) = lcm a (Lcm A)" by simp

  1546   also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast

  1547   also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)

  1548   also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto

  1549   with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))"

  1550     by (simp add: lcm_coprime)

  1551   finally show ?case .

  1552 qed simp

  1553

  1554 lemma Lcm_coprime':

  1555   "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)

  1556     \<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"

  1557   by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)

  1558

  1559 lemma Gcd_Lcm:

  1560   "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"

  1561   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1562

  1563 lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"

  1564   and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A"

  1565   and normalization_factor_Gcd [simp]:

  1566     "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1567 proof -

  1568   fix a assume "a \<in> A"

  1569   hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast

  1570   then show "Gcd A dvd a" by (simp add: Gcd_Lcm)

  1571 next

  1572   fix g' assume "\<forall>a\<in>A. g' dvd a"

  1573   hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast

  1574   then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)

  1575 next

  1576   show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1577     by (simp add: Gcd_Lcm)

  1578 qed

  1579

  1580 lemma GcdI:

  1581   "(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow>

  1582     normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"

  1583   by (intro normed_associated_imp_eq)

  1584     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)

  1585

  1586 lemma Lcm_Gcd:

  1587   "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"

  1588   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)

  1589

  1590 lemma Gcd_0_iff:

  1591   "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"

  1592   apply (rule iffI)

  1593   apply (rule subsetI, drule Gcd_dvd, simp)

  1594   apply (auto intro: GcdI[symmetric])

  1595   done

  1596

  1597 lemma Gcd_empty [simp]:

  1598   "Gcd {} = 0"

  1599   by (simp add: Gcd_0_iff)

  1600

  1601 lemma Gcd_1:

  1602   "1 \<in> A \<Longrightarrow> Gcd A = 1"

  1603   by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)

  1604

  1605 lemma Gcd_insert [simp]:

  1606   "Gcd (insert a A) = gcd a (Gcd A)"

  1607 proof (rule gcdI)

  1608   fix l assume "l dvd a" and "l dvd Gcd A"

  1609   hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)

  1610   with l dvd a show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)

  1611 qed auto

  1612

  1613 lemma Gcd_finite:

  1614   assumes "finite A"

  1615   shows "Gcd A = Finite_Set.fold gcd 0 A"

  1616   by (induct rule: finite.induct[OF finite A])

  1617     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])

  1618

  1619 lemma Gcd_set [code_unfold]:

  1620   "Gcd (set xs) = fold gcd xs 0"

  1621   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)

  1622

  1623 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a"

  1624   by (simp add: gcd_0)

  1625

  1626 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"

  1627   by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)

  1628

  1629 subclass semiring_gcd

  1630   by unfold_locales (simp_all add: gcd_greatest_iff)

  1631

  1632 end

  1633

  1634 text {*

  1635   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a

  1636   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.

  1637 *}

  1638

  1639 class euclidean_ring = euclidean_semiring + idom

  1640

  1641 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1642 begin

  1643

  1644 subclass euclidean_ring ..

  1645

  1646 subclass ring_gcd ..

  1647

  1648 lemma gcd_neg1 [simp]:

  1649   "gcd (-a) b = gcd a b"

  1650   by (rule sym, rule gcdI, simp_all add: gcd_greatest)

  1651

  1652 lemma gcd_neg2 [simp]:

  1653   "gcd a (-b) = gcd a b"

  1654   by (rule sym, rule gcdI, simp_all add: gcd_greatest)

  1655

  1656 lemma gcd_neg_numeral_1 [simp]:

  1657   "gcd (- numeral n) a = gcd (numeral n) a"

  1658   by (fact gcd_neg1)

  1659

  1660 lemma gcd_neg_numeral_2 [simp]:

  1661   "gcd a (- numeral n) = gcd a (numeral n)"

  1662   by (fact gcd_neg2)

  1663

  1664 lemma gcd_diff1: "gcd (m - n) n = gcd m n"

  1665   by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)

  1666

  1667 lemma gcd_diff2: "gcd (n - m) n = gcd m n"

  1668   by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)

  1669

  1670 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"

  1671 proof -

  1672   have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)

  1673   also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp

  1674   also have "\<dots> = 1" by (rule coprime_plus_one)

  1675   finally show ?thesis .

  1676 qed

  1677

  1678 lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"

  1679   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)

  1680

  1681 lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"

  1682   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)

  1683

  1684 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"

  1685   by (fact lcm_neg1)

  1686

  1687 lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"

  1688   by (fact lcm_neg2)

  1689

  1690 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where

  1691   "euclid_ext a b =

  1692      (if b = 0 then

  1693         let c = 1 div normalization_factor a in (c, 0, a * c)

  1694       else

  1695         case euclid_ext b (a mod b) of

  1696             (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"

  1697   by (pat_completeness, simp)

  1698   termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)

  1699

  1700 declare euclid_ext.simps [simp del]

  1701

  1702 lemma euclid_ext_0:

  1703   "euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)"

  1704   by (subst euclid_ext.simps) (simp add: Let_def)

  1705

  1706 lemma euclid_ext_non_0:

  1707   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of

  1708     (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"

  1709   by (subst euclid_ext.simps) simp

  1710

  1711 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"

  1712 where

  1713   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"

  1714

  1715 lemma euclid_ext_gcd [simp]:

  1716   "(case euclid_ext a b of (_,_,t) \<Rightarrow> t) = gcd a b"

  1717 proof (induct a b rule: euclid_ext.induct)

  1718   case (1 a b)

  1719   then show ?case

  1720   proof (cases "b = 0")

  1721     case True

  1722       then show ?thesis by

  1723         (simp add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)

  1724     next

  1725     case False with 1 show ?thesis

  1726       by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)

  1727     qed

  1728 qed

  1729

  1730 lemma euclid_ext_gcd' [simp]:

  1731   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"

  1732   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)

  1733

  1734 lemma euclid_ext_correct:

  1735   "case euclid_ext a b of (s,t,c) \<Rightarrow> s*a + t*b = c"

  1736 proof (induct a b rule: euclid_ext.induct)

  1737   case (1 a b)

  1738   show ?case

  1739   proof (cases "b = 0")

  1740     case True

  1741     then show ?thesis by (simp add: euclid_ext_0 mult_ac)

  1742   next

  1743     case False

  1744     obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"

  1745       by (cases "euclid_ext b (a mod b)", blast)

  1746     from 1 have "c = s * b + t * (a mod b)" by (simp add: stc False)

  1747     also have "... = t*((a div b)*b + a mod b) + (s - t * (a div b))*b"

  1748       by (simp add: algebra_simps)

  1749     also have "(a div b)*b + a mod b = a" using mod_div_equality .

  1750     finally show ?thesis

  1751       by (subst euclid_ext.simps, simp add: False stc)

  1752     qed

  1753 qed

  1754

  1755 lemma euclid_ext'_correct:

  1756   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"

  1757 proof-

  1758   obtain s t c where "euclid_ext a b = (s,t,c)"

  1759     by (cases "euclid_ext a b", blast)

  1760   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]

  1761     show ?thesis unfolding euclid_ext'_def by simp

  1762 qed

  1763

  1764 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"

  1765   using euclid_ext'_correct by blast

  1766

  1767 lemma euclid_ext'_0 [simp]: "euclid_ext' a 0 = (1 div normalization_factor a, 0)"

  1768   by (simp add: bezw_def euclid_ext'_def euclid_ext_0)

  1769

  1770 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),

  1771   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"

  1772   by (cases "euclid_ext b (a mod b)")

  1773     (simp add: euclid_ext'_def euclid_ext_non_0)

  1774

  1775 end

  1776

  1777 instantiation nat :: euclidean_semiring

  1778 begin

  1779

  1780 definition [simp]:

  1781   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"

  1782

  1783 definition [simp]:

  1784   "normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"

  1785

  1786 instance proof

  1787 qed simp_all

  1788

  1789 end

  1790

  1791 instantiation int :: euclidean_ring

  1792 begin

  1793

  1794 definition [simp]:

  1795   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"

  1796

  1797 definition [simp]:

  1798   "normalization_factor_int = (sgn :: int \<Rightarrow> int)"

  1799

  1800 instance proof

  1801   case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)

  1802 next

  1803   case goal3 then show ?case by (simp add: zsgn_def)

  1804 next

  1805   case goal5 then show ?case by (auto simp: zsgn_def)

  1806 next

  1807   case goal6 then show ?case by (auto split: abs_split simp: zsgn_def)

  1808 qed (auto simp: sgn_times split: abs_split)

  1809

  1810 end

  1811

  1812 end