src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Fri Jun 12 21:53:05 2015 +0200 (2015-06-12) changeset 60438 e1c345094813 parent 60437 63edc650cf67 child 60439 b765e08f8bc0 permissions -rw-r--r--
slight preference for American English
     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 mult_cancel_right [simp]:

    13   "a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b"

    14 proof (cases "c = 0")

    15   case True then show ?thesis by simp

    16 next

    17   case False

    18   { assume "a * c = b * c"

    19     then have "a * c div c = b * c div c"

    20       by simp

    21     with False have "a = b"

    22       by simp

    23   } then show ?thesis by auto

    24 qed

    25

    26 lemma mult_cancel_left [simp]:

    27   "c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b"

    28   using mult_cancel_right [of a c b] by (simp add: ac_simps)

    29

    30 end

    31

    32 context semidom_divide

    33 begin

    34

    35 lemma div_self [simp]:

    36   assumes "a \<noteq> 0"

    37   shows "a div a = 1"

    38   using assms nonzero_mult_divide_cancel_left [of a 1] by simp

    39

    40 lemma dvd_div_mult_self [simp]:

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

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

    43

    44 lemma dvd_mult_div_cancel [simp]:

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

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

    47

    48 lemma div_mult_swap:

    49   assumes "c dvd b"

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

    51 proof (cases "c = 0")

    52   case True then show ?thesis by simp

    53 next

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

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

    56     by simp

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

    58 qed

    59

    60 lemma dvd_div_mult:

    61   assumes "c dvd b"

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

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

    64

    65

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

    67

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

    69 where

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

    71

    72 lemma not_is_unit_0 [simp]:

    73   "\<not> is_unit 0"

    74   by simp

    75

    76 lemma unit_imp_dvd [dest]:

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

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

    79

    80 lemma unit_dvdE:

    81   assumes "is_unit a"

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

    83 proof -

    84   from assms have "a dvd b" by auto

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

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

    87   ultimately show thesis using that by blast

    88 qed

    89

    90 lemma dvd_unit_imp_unit:

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

    92   by (rule dvd_trans)

    93

    94 lemma unit_div_1_unit [simp, intro]:

    95   assumes "is_unit a"

    96   shows "is_unit (1 div a)"

    97 proof -

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

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

   100 qed

   101

   102 lemma is_unitE [elim?]:

   103   assumes "is_unit a"

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

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

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

   107 proof (rule that)

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

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

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

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

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

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

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

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

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

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

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

   119 qed

   120

   121 lemma unit_prod [intro]:

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

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

   124

   125 lemma unit_div [intro]:

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

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

   128

   129 lemma mult_unit_dvd_iff:

   130   assumes "is_unit b"

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

   132 proof

   133   assume "a * b dvd c"

   134   with assms show "a dvd c"

   135     by (simp add: dvd_mult_left)

   136 next

   137   assume "a dvd c"

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

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

   140     by (simp add: mult_ac)

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

   142 qed

   143

   144 lemma dvd_mult_unit_iff:

   145   assumes "is_unit b"

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

   147 proof

   148   assume "a dvd c * b"

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

   150     by (subst mult_assoc [symmetric]) simp

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

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

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

   154 next

   155   assume "a dvd c"

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

   157 qed

   158

   159 lemma div_unit_dvd_iff:

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

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

   162

   163 lemma dvd_div_unit_iff:

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

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

   166

   167 lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff

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

   169

   170 lemma unit_mult_div_div [simp]:

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

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

   173

   174 lemma unit_div_mult_self [simp]:

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

   176   by (rule dvd_div_mult_self) auto

   177

   178 lemma unit_div_1_div_1 [simp]:

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

   180   by (erule is_unitE) simp

   181

   182 lemma unit_div_mult_swap:

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

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

   185

   186 lemma unit_div_commute:

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

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

   189

   190 lemma unit_eq_div1:

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

   192   by (auto elim: is_unitE)

   193

   194 lemma unit_eq_div2:

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

   196   using unit_eq_div1 [of b c a] by auto

   197

   198 lemma unit_mult_left_cancel:

   199   assumes "is_unit a"

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

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

   202

   203 lemma unit_mult_right_cancel:

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

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

   206

   207 lemma unit_div_cancel:

   208   assumes "is_unit a"

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

   210 proof -

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

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

   213     by (rule unit_mult_right_cancel)

   214   with assms show ?thesis by simp

   215 qed

   216

   217

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

   219

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

   221 where

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

   223

   224 lemma associatedI:

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

   226   by (simp add: associated_def)

   227

   228 lemma associatedD1:

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

   230   by (simp add: associated_def)

   231

   232 lemma associatedD2:

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

   234   by (simp add: associated_def)

   235

   236 lemma associated_refl [simp]:

   237   "associated a a"

   238   by (auto intro: associatedI)

   239

   240 lemma associated_sym:

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

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

   243

   244 lemma associated_trans:

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

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

   247

   248 lemma equivp_associated:

   249   "equivp associated"

   250 proof (rule equivpI)

   251   show "reflp associated"

   252     by (rule reflpI) simp

   253   show "symp associated"

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

   255   show "transp associated"

   256     by (rule transpI) (fact associated_trans)

   257 qed

   258

   259 lemma associated_0 [simp]:

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

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

   262   by (auto dest: associatedD1 associatedD2)

   263

   264 lemma associated_unit:

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

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

   267

   268 lemma is_unit_associatedI:

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

   270   shows "associated a b"

   271 proof (rule associatedI)

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

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

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

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

   276   then show "a dvd b" ..

   277 qed

   278

   279 lemma associated_is_unitE:

   280   assumes "associated a b"

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

   282 proof (cases "b = 0")

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

   284   with that show thesis .

   285 next

   286   case False

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

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

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

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

   291   then have "is_unit c" by auto

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

   293 qed

   294

   295 lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff

   296   dvd_div_unit_iff unit_div_mult_swap unit_div_commute

   297   unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel

   298   unit_eq_div1 unit_eq_div2

   299

   300 end

   301

   302 lemma is_unit_int:

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

   304   by auto

   305

   306

   307 text {*

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

   309   implemented. It must provide:

   310   \begin{itemize}

   311   \item division with remainder

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

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

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

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

   316   \end{itemize}

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

   318   for any Euclidean semiring.

   319 *}

   320 class euclidean_semiring = semiring_div +

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

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

   323   assumes mod_size_less [simp]:

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

   325   assumes size_mult_mono:

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

   327   assumes normalization_factor_is_unit [intro,simp]:

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

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

   330     normalization_factor a * normalization_factor b"

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

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

   333 begin

   334

   335 lemma normalization_factor_dvd [simp]:

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

   337   by (rule unit_imp_dvd, simp)

   338

   339 lemma normalization_factor_1 [simp]:

   340   "normalization_factor 1 = 1"

   341   by (simp add: normalization_factor_unit)

   342

   343 lemma normalization_factor_0_iff [simp]:

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

   345 proof

   346   assume "normalization_factor a = 0"

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

   348     by simp

   349   then show "a = 0" by auto

   350 qed simp

   351

   352 lemma normalization_factor_pow:

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

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

   355

   356 lemma normalization_correct [simp]:

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

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

   359   assume "a \<noteq> 0"

   360   let ?nf = "normalization_factor"

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

   362     by auto

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

   364     by (simp add: normalization_factor_mult)

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

   366     by simp

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

   368     normalization_factor_is_unit normalization_factor_unit by simp

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

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

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

   372 qed

   373

   374 lemma normalization_0_iff [simp]:

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

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

   377

   378 lemma mult_div_normalization [simp]:

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

   380   by (cases "a = 0") simp_all

   381

   382 lemma associated_iff_normed_eq:

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

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

   385   let ?nf = normalization_factor

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

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

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

   389     apply (subst div_mult_swap, simp, simp)

   390     done

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

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

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

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

   395 next

   396   let ?nf = normalization_factor

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

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

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

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

   401     apply (rule div_mult_mult1, force)

   402     done

   403   qed

   404

   405 lemma normed_associated_imp_eq:

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

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

   408

   409 lemmas normalization_factor_dvd_iff [simp] =

   410   unit_dvd_iff [OF normalization_factor_is_unit]

   411

   412 lemma euclidean_division:

   413   fixes a :: 'a and b :: 'a

   414   assumes "b \<noteq> 0"

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

   416     and "euclidean_size t < euclidean_size b"

   417 proof -

   418   from div_mod_equality[of a b 0]

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

   420   with that and assms show ?thesis by force

   421 qed

   422

   423 lemma dvd_euclidean_size_eq_imp_dvd:

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

   425   shows "a dvd b"

   426 proof (subst dvd_eq_mod_eq_0, rule ccontr)

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

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

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

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

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

   432       using size_mult_mono by force

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

   434       using mod_size_less by blast

   435   ultimately show False using size_eq by simp

   436 qed

   437

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

   439 where

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

   441   by (pat_completeness, simp)

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

   443

   444 declare gcd_eucl.simps [simp del]

   445

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

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

   448   case ("1" m n)

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

   450 qed

   451

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

   453 where

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

   455

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

   457      for infinite sets as well *)

   458

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

   460 where

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

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

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

   464        in l div normalization_factor l

   465       else 0)"

   466

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

   468 where

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

   470

   471 end

   472

   473 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   476 begin

   477

   478 lemma gcd_red:

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

   480   by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)

   481

   482 lemma gcd_non_0:

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

   484   by (rule gcd_red)

   485

   486 lemma gcd_0_left:

   487   "gcd 0 a = a div normalization_factor a"

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

   489

   490 lemma gcd_0:

   491   "gcd a 0 = a div normalization_factor a"

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

   493

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

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

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

   497   fix a b :: 'a

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

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

   500

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

   502   proof (cases "b = 0")

   503     case True

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

   505   next

   506     case False

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

   508   qed

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

   510 qed

   511

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

   513   by (rule dvd_trans, assumption, rule gcd_dvd1)

   514

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

   516   by (rule dvd_trans, assumption, rule gcd_dvd2)

   517

   518 lemma gcd_greatest:

   519   fixes k a b :: 'a

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

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

   522   case (1 a b)

   523   show ?case

   524     proof (cases "b = 0")

   525       assume "b = 0"

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

   527     next

   528       assume "b \<noteq> 0"

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

   530     qed

   531 qed

   532

   533 lemma dvd_gcd_iff:

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

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

   536

   537 lemmas gcd_greatest_iff = dvd_gcd_iff

   538

   539 lemma gcd_zero [simp]:

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

   541   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   542

   543 lemma normalization_factor_gcd [simp]:

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

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

   546   fix a b :: 'a

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

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

   549 qed

   550

   551 lemma gcdI:

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

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

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

   555

   556 sublocale gcd!: abel_semigroup gcd

   557 proof

   558   fix a b c

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

   560   proof (rule gcdI)

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

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

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

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

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

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

   567       by (rule gcd_greatest)

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

   569       by auto

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

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

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

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

   574       by (intro gcd_greatest)

   575   qed

   576 next

   577   fix a b

   578   show "gcd a b = gcd b a"

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

   580 qed

   581

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

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

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

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

   586

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

   588   using mult_dvd_mono [of 1] by auto

   589

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

   591   by (rule sym, rule gcdI, simp_all)

   592

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

   594   by (rule sym, rule gcdI, simp_all)

   595

   596 lemma gcd_proj2_if_dvd:

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

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

   599

   600 lemma gcd_proj1_if_dvd:

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

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

   603

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

   605 proof

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

   607   show "m dvd n"

   608   proof (cases "m = 0")

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

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

   611       by (simp add: unit_eq_div2)

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

   613   qed (insert A, simp)

   614 next

   615   assume "m dvd n"

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

   617 qed

   618

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

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

   621

   622 lemma gcd_mod1 [simp]:

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

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

   625

   626 lemma gcd_mod2 [simp]:

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

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

   629

   630 lemma normalization_factor_dvd' [simp]:

   631   "normalization_factor a dvd a"

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

   633

   634 lemma gcd_mult_distrib':

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

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

   637   case (1 a b)

   638   show ?case

   639   proof (cases "b = 0")

   640     case True

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

   642   next

   643     case False

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

   645       using 1 by (subst gcd_red, simp)

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

   647       by (simp add: mult_mod_right gcd.commute)

   648     finally show ?thesis .

   649   qed

   650 qed

   651

   652 lemma gcd_mult_distrib:

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

   654 proof-

   655   let ?nf = "normalization_factor"

   656   from gcd_mult_distrib'

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

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

   659     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)

   660   finally show ?thesis

   661     by simp

   662 qed

   663

   664 lemma euclidean_size_gcd_le1 [simp]:

   665   assumes "a \<noteq> 0"

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

   667 proof -

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

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

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

   671 qed

   672

   673 lemma euclidean_size_gcd_le2 [simp]:

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

   675   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   676

   677 lemma euclidean_size_gcd_less1:

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

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

   680 proof (rule ccontr)

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

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

   683     by (intro le_antisym, simp_all)

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

   685   hence "a dvd b" using dvd_gcd_D2 by blast

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

   687 qed

   688

   689 lemma euclidean_size_gcd_less2:

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

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

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

   693

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

   695   apply (rule gcdI)

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

   697   apply (rule gcd_dvd2)

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

   699   apply (subst normalization_factor_gcd, simp add: gcd_0)

   700   done

   701

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

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

   704

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

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

   707

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

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

   710

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

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

   713

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

   715   apply (rule gcdI)

   716   apply (simp add: ac_simps)

   717   apply (rule gcd_dvd2)

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

   719   apply simp

   720   done

   721

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

   723   apply (rule gcdI)

   724   apply simp

   725   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

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

   727   apply simp

   728   done

   729

   730 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   731 proof

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

   733     by (simp add: fun_eq_iff ac_simps)

   734 next

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

   736     by (simp add: fun_eq_iff gcd_left_idem)

   737 qed

   738

   739 lemma coprime_dvd_mult:

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

   741   shows "c dvd a"

   742 proof -

   743   let ?nf = "normalization_factor"

   744   from assms gcd_mult_distrib [of a c b]

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

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

   747 qed

   748

   749 lemma coprime_dvd_mult_iff:

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

   751   by (rule, rule coprime_dvd_mult, simp_all)

   752

   753 lemma gcd_dvd_antisym:

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

   755 proof (rule gcdI)

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

   757   have "gcd c d dvd c" by simp

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

   759   have "gcd c d dvd d" by simp

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

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

   762     by simp

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

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

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

   766 qed

   767

   768 lemma gcd_mult_cancel:

   769   assumes "gcd k n = 1"

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

   771 proof (rule gcd_dvd_antisym)

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

   773   also note gcd k n = 1

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

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

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

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

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

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

   780 qed

   781

   782 lemma coprime_crossproduct:

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

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

   785 proof

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

   787 next

   788   assume ?lhs

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

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

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

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

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

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

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

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

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

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

   799   ultimately show ?rhs unfolding associated_def by simp

   800 qed

   801

   802 lemma gcd_add1 [simp]:

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

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

   805

   806 lemma gcd_add2 [simp]:

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

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

   809

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

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

   812

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

   814   by (rule sym, rule gcdI, simp_all)

   815

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

   817   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   818

   819 lemma div_gcd_coprime:

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

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

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

   823   shows "gcd a' b' = 1"

   824 proof (rule coprimeI)

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

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

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

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

   829     by (simp_all only: ac_simps)

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

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

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

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

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

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

   836   ultimately have "1 = l * u"

   837     using d \<noteq> 0 by simp

   838   then show "l dvd 1" ..

   839 qed

   840

   841 lemma coprime_mult:

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

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

   844   apply (subst gcd.commute)

   845   using da apply (subst gcd_mult_cancel)

   846   apply (subst gcd.commute, assumption)

   847   apply (subst gcd.commute, rule db)

   848   done

   849

   850 lemma coprime_lmult:

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

   852   shows "gcd d a = 1"

   853 proof (rule coprimeI)

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

   855   hence "l dvd a * b" by simp

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

   857 qed

   858

   859 lemma coprime_rmult:

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

   861   shows "gcd d b = 1"

   862 proof (rule coprimeI)

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

   864   hence "l dvd a * b" by simp

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

   866 qed

   867

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

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

   870

   871 lemma gcd_coprime:

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

   873   shows "gcd a' b' = 1"

   874 proof -

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

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

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

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

   879   finally show ?thesis .

   880 qed

   881

   882 lemma coprime_power:

   883   assumes "0 < n"

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

   885 using assms proof (induct n)

   886   case (Suc n) then show ?case

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

   888 qed simp

   889

   890 lemma gcd_coprime_exists:

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

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

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

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

   895   apply (insert nz, auto intro: div_gcd_coprime)

   896   done

   897

   898 lemma coprime_exp:

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

   900   by (induct n, simp_all add: coprime_mult)

   901

   902 lemma coprime_exp2 [intro]:

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

   904   apply (rule coprime_exp)

   905   apply (subst gcd.commute)

   906   apply (rule coprime_exp)

   907   apply (subst gcd.commute)

   908   apply assumption

   909   done

   910

   911 lemma gcd_exp:

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

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

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

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

   916 next

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

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

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

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

   921   also note gcd_mult_distrib

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

   923     by (simp add: normalization_factor_pow A)

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

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

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

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

   928   finally show ?thesis by simp

   929 qed

   930

   931 lemma coprime_common_divisor:

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

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

   934   apply simp

   935   apply (erule (1) gcd_greatest)

   936   done

   937

   938 lemma division_decomp:

   939   assumes dc: "a dvd b * c"

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

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

   942   assume "gcd a b = 0"

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

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

   945   then show ?thesis by blast

   946 next

   947   let ?d = "gcd a b"

   948   assume "?d \<noteq> 0"

   949   from gcd_coprime_exists[OF this]

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

   951     by blast

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

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

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

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

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

   957   with coprime_dvd_mult[OF ab'(3)]

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

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

   960   then show ?thesis by blast

   961 qed

   962

   963 lemma pow_divs_pow:

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

   965   shows "a dvd b"

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

   967   assume "gcd a b = 0"

   968   then show ?thesis by simp

   969 next

   970   let ?d = "gcd a b"

   971   assume "?d \<noteq> 0"

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

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

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

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

   976     by blast

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

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

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

   980     by (simp only: power_mult_distrib ac_simps)

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

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

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

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

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

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

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

   988 qed

   989

   990 lemma pow_divs_eq [simp]:

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

   992   by (auto intro: pow_divs_pow dvd_power_same)

   993

   994 lemma divs_mult:

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

   996   shows "m * n dvd r"

   997 proof -

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

   999     unfolding dvd_def by blast

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

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

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

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

  1004   then show ?thesis unfolding dvd_def by blast

  1005 qed

  1006

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

  1008   by (subst add_commute, simp)

  1009

  1010 lemma setprod_coprime [rule_format]:

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

  1012   apply (cases "finite A")

  1013   apply (induct set: finite)

  1014   apply (auto simp add: gcd_mult_cancel)

  1015   done

  1016

  1017 lemma coprime_divisors:

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

  1019   shows "gcd d e = 1"

  1020 proof -

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

  1022     unfolding dvd_def by blast

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

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

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

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

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

  1028 qed

  1029

  1030 lemma invertible_coprime:

  1031   assumes "a * b mod m = 1"

  1032   shows "coprime a m"

  1033 proof -

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

  1035     by simp

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

  1037     by simp

  1038   then have "coprime m a"

  1039     by (rule coprime_lmult)

  1040   then show ?thesis

  1041     by (simp add: ac_simps)

  1042 qed

  1043

  1044 lemma lcm_gcd:

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

  1046   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

  1047

  1048 lemma lcm_gcd_prod:

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

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

  1051   let ?nf = normalization_factor

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

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

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

  1055     by (simp add: mult_ac)

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

  1057     by (simp add: div_mult_swap mult.commute)

  1058   finally show ?thesis .

  1059 qed (auto simp add: lcm_gcd)

  1060

  1061 lemma lcm_dvd1 [iff]:

  1062   "a dvd lcm a b"

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

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

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

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

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

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

  1069     by (simp add: div_mult_swap unit_div_commute)

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

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

  1072     by (subst (asm) div_mult_self2_is_id, simp_all)

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

  1074     by (metis div_mult_swap gcd_dvd2 mult_assoc)

  1075   finally show ?thesis by (rule dvdI)

  1076 qed (auto simp add: lcm_gcd)

  1077

  1078 lemma lcm_least:

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

  1080 proof (cases "k = 0")

  1081   let ?nf = normalization_factor

  1082   assume "k \<noteq> 0"

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

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

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

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

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

  1088     unfolding dvd_def by blast

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  1104     by (simp add: algebra_simps)

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

  1106     by (metis div_mult_self2_is_id)

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

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

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

  1110     by (simp add: algebra_simps)

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

  1112     by (metis mult.commute div_mult_self2_is_id)

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

  1114     by (metis div_mult_self2_is_id mult_assoc)

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

  1116     by (simp add: unit_simps)

  1117   finally show ?thesis by (rule dvdI)

  1118 qed simp

  1119

  1120 lemma lcm_zero:

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

  1122 proof -

  1123   let ?nf = normalization_factor

  1124   {

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

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

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

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

  1129   } moreover {

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

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

  1132   }

  1133   ultimately show ?thesis by blast

  1134 qed

  1135

  1136 lemmas lcm_0_iff = lcm_zero

  1137

  1138 lemma gcd_lcm:

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

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

  1141 proof-

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

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

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

  1145   hence "is_unit ?c" by simp

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

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

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

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

  1150   finally show ?thesis .

  1151 qed

  1152

  1153 lemma normalization_factor_lcm [simp]:

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

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

  1156   case True then show ?thesis

  1157     by (auto simp add: lcm_gcd)

  1158 next

  1159   case False

  1160   let ?nf = normalization_factor

  1161   from lcm_gcd_prod[of a b]

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

  1163     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)

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

  1165     by simp

  1166   finally show ?thesis using False by simp

  1167 qed

  1168

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

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

  1171

  1172 lemma lcmI:

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

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

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

  1176

  1177 sublocale lcm!: abel_semigroup lcm

  1178 proof

  1179   fix a b c

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

  1181   proof (rule lcmI)

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

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

  1184

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

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

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

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

  1189

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

  1191     have "b dvd lcm b c" by simp

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

  1193     have "c dvd lcm b c" by simp

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

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

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

  1197   qed (simp add: lcm_zero)

  1198 next

  1199   fix a b

  1200   show "lcm a b = lcm b a"

  1201     by (simp add: lcm_gcd ac_simps)

  1202 qed

  1203

  1204 lemma dvd_lcm_D1:

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

  1206   by (rule dvd_trans, rule lcm_dvd1, assumption)

  1207

  1208 lemma dvd_lcm_D2:

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

  1210   by (rule dvd_trans, rule lcm_dvd2, assumption)

  1211

  1212 lemma gcd_dvd_lcm [simp]:

  1213   "gcd a b dvd lcm a b"

  1214   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

  1215

  1216 lemma lcm_1_iff:

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

  1218 proof

  1219   assume "lcm a b = 1"

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

  1221 next

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

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

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

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

  1226     by (subst normalization_factor_unit, simp_all)

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

  1228     by auto

  1229   finally show "lcm a b = 1" .

  1230 qed

  1231

  1232 lemma lcm_0_left [simp]:

  1233   "lcm 0 a = 0"

  1234   by (rule sym, rule lcmI, simp_all)

  1235

  1236 lemma lcm_0 [simp]:

  1237   "lcm a 0 = 0"

  1238   by (rule sym, rule lcmI, simp_all)

  1239

  1240 lemma lcm_unique:

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

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

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

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

  1245

  1246 lemma dvd_lcm_I1 [simp]:

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

  1248   by (metis lcm_dvd1 dvd_trans)

  1249

  1250 lemma dvd_lcm_I2 [simp]:

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

  1252   by (metis lcm_dvd2 dvd_trans)

  1253

  1254 lemma lcm_1_left [simp]:

  1255   "lcm 1 a = a div normalization_factor a"

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

  1257

  1258 lemma lcm_1_right [simp]:

  1259   "lcm a 1 = a div normalization_factor a"

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

  1261

  1262 lemma lcm_coprime:

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

  1264   by (subst lcm_gcd) simp

  1265

  1266 lemma lcm_proj1_if_dvd:

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

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

  1269

  1270 lemma lcm_proj2_if_dvd:

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

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

  1273

  1274 lemma lcm_proj1_iff:

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

  1276 proof

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

  1278   show "n dvd m"

  1279   proof (cases "m = 0")

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

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

  1282       by (simp add: unit_eq_div2)

  1283     show ?thesis by (subst B, simp)

  1284   qed simp

  1285 next

  1286   assume "n dvd m"

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

  1288 qed

  1289

  1290 lemma lcm_proj2_iff:

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

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

  1293

  1294 lemma euclidean_size_lcm_le1:

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

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

  1297 proof -

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

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

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

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

  1302 qed

  1303

  1304 lemma euclidean_size_lcm_le2:

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

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

  1307

  1308 lemma euclidean_size_lcm_less1:

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

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

  1311 proof (rule ccontr)

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

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

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

  1315     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

  1316   with assms have "lcm a b dvd a"

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

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

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

  1320 qed

  1321

  1322 lemma euclidean_size_lcm_less2:

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

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

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

  1326

  1327 lemma lcm_mult_unit1:

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

  1329   apply (rule lcmI)

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

  1331   apply (rule lcm_dvd2)

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

  1333   apply (subst normalization_factor_lcm, simp add: lcm_zero)

  1334   done

  1335

  1336 lemma lcm_mult_unit2:

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

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

  1339

  1340 lemma lcm_div_unit1:

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

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

  1343

  1344 lemma lcm_div_unit2:

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

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

  1347

  1348 lemma lcm_left_idem:

  1349   "lcm a (lcm a b) = lcm a b"

  1350   apply (rule lcmI)

  1351   apply simp

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

  1353   apply (rule lcm_least, assumption)

  1354   apply (erule (1) lcm_least)

  1355   apply (auto simp: lcm_zero)

  1356   done

  1357

  1358 lemma lcm_right_idem:

  1359   "lcm (lcm a b) b = lcm a b"

  1360   apply (rule lcmI)

  1361   apply (subst lcm.assoc, rule lcm_dvd1)

  1362   apply (rule lcm_dvd2)

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

  1364   apply (auto simp: lcm_zero)

  1365   done

  1366

  1367 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

  1368 proof

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

  1370     by (simp add: fun_eq_iff ac_simps)

  1371 next

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

  1373     by (intro ext, simp add: lcm_left_idem)

  1374 qed

  1375

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

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

  1378   and normalization_factor_Lcm [simp]:

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

  1380 proof -

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

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

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

  1384     case False

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

  1386     with False show ?thesis by auto

  1387   next

  1388     case True

  1389     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

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

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

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

  1393       apply (subst n_def)

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

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

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

  1397       done

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

  1399       unfolding l_def by simp_all

  1400     {

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

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

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

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

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

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

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

  1408       proof -

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

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

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

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

  1413           by (rule size_mult_mono)

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

  1415         also note euclidean_size l = n

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

  1417       qed

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

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

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

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

  1422     }

  1423

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

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

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

  1427         normalization_factor (l div normalization_factor l) =

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

  1429       by (auto simp: unit_simps)

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

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

  1432     finally show ?thesis .

  1433   qed

  1434   note A = this

  1435

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

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

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

  1439 qed

  1440

  1441 lemma LcmI:

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

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

  1444   by (intro normed_associated_imp_eq)

  1445     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)

  1446

  1447 lemma Lcm_subset:

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

  1449   by (blast intro: Lcm_dvd dvd_Lcm)

  1450

  1451 lemma Lcm_Un:

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

  1453   apply (rule lcmI)

  1454   apply (blast intro: Lcm_subset)

  1455   apply (blast intro: Lcm_subset)

  1456   apply (intro Lcm_dvd ballI, elim UnE)

  1457   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1458   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1459   apply simp

  1460   done

  1461

  1462 lemma Lcm_1_iff:

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

  1464 proof

  1465   assume "Lcm A = 1"

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

  1467 qed (rule LcmI [symmetric], auto)

  1468

  1469 lemma Lcm_no_units:

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

  1471 proof -

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

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

  1474     by (simp add: Lcm_Un[symmetric])

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

  1476   finally show ?thesis by simp

  1477 qed

  1478

  1479 lemma Lcm_empty [simp]:

  1480   "Lcm {} = 1"

  1481   by (simp add: Lcm_1_iff)

  1482

  1483 lemma Lcm_eq_0 [simp]:

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

  1485   by (drule dvd_Lcm) simp

  1486

  1487 lemma Lcm0_iff':

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

  1489 proof

  1490   assume "Lcm A = 0"

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

  1492   proof

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

  1494     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

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

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

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

  1498       apply (subst n_def)

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

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

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

  1502       done

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

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

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

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

  1507     finally show False using Lcm A = 0 by contradiction

  1508   qed

  1509 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1510

  1511 lemma Lcm0_iff [simp]:

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

  1513 proof -

  1514   assume "finite A"

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

  1516   moreover {

  1517     assume "0 \<notin> A"

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

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

  1520       apply simp

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

  1522       apply (rule no_zero_divisors)

  1523       apply blast+

  1524       done

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

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

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

  1528   }

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

  1530 qed

  1531

  1532 lemma Lcm_no_multiple:

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

  1534 proof -

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

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

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

  1538 qed

  1539

  1540 lemma Lcm_insert [simp]:

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

  1542 proof (rule lcmI)

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

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

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

  1546 qed (auto intro: Lcm_dvd dvd_Lcm)

  1547

  1548 lemma Lcm_finite:

  1549   assumes "finite A"

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

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

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

  1553

  1554 lemma Lcm_set [code_unfold]:

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

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

  1557

  1558 lemma Lcm_singleton [simp]:

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

  1560   by simp

  1561

  1562 lemma Lcm_2 [simp]:

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

  1564   by (simp only: Lcm_insert Lcm_empty lcm_1_right)

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

  1566

  1567 lemma Lcm_coprime:

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

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

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

  1571 using assms proof (induct rule: finite_ne_induct)

  1572   case (insert a A)

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

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

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

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

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

  1578     by (simp add: lcm_coprime)

  1579   finally show ?case .

  1580 qed simp

  1581

  1582 lemma Lcm_coprime':

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

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

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

  1586

  1587 lemma Gcd_Lcm:

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

  1589   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1590

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

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

  1593   and normalization_factor_Gcd [simp]:

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

  1595 proof -

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

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

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

  1599 next

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

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

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

  1603 next

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

  1605     by (simp add: Gcd_Lcm)

  1606 qed

  1607

  1608 lemma GcdI:

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

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

  1611   by (intro normed_associated_imp_eq)

  1612     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)

  1613

  1614 lemma Lcm_Gcd:

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

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

  1617

  1618 lemma Gcd_0_iff:

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

  1620   apply (rule iffI)

  1621   apply (rule subsetI, drule Gcd_dvd, simp)

  1622   apply (auto intro: GcdI[symmetric])

  1623   done

  1624

  1625 lemma Gcd_empty [simp]:

  1626   "Gcd {} = 0"

  1627   by (simp add: Gcd_0_iff)

  1628

  1629 lemma Gcd_1:

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

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

  1632

  1633 lemma Gcd_insert [simp]:

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

  1635 proof (rule gcdI)

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

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

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

  1639 qed auto

  1640

  1641 lemma Gcd_finite:

  1642   assumes "finite A"

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

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

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

  1646

  1647 lemma Gcd_set [code_unfold]:

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

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

  1650

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

  1652   by (simp add: gcd_0)

  1653

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

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

  1656

  1657 end

  1658

  1659 text {*

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

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

  1662 *}

  1663

  1664 class euclidean_ring = euclidean_semiring + idom

  1665

  1666 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1667 begin

  1668

  1669 subclass euclidean_ring ..

  1670

  1671 lemma gcd_neg1 [simp]:

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

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

  1674

  1675 lemma gcd_neg2 [simp]:

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

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

  1678

  1679 lemma gcd_neg_numeral_1 [simp]:

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

  1681   by (fact gcd_neg1)

  1682

  1683 lemma gcd_neg_numeral_2 [simp]:

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

  1685   by (fact gcd_neg2)

  1686

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

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

  1689

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

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

  1692

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

  1694 proof -

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

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

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

  1698   finally show ?thesis .

  1699 qed

  1700

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

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

  1703

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

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

  1706

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

  1708   by (fact lcm_neg1)

  1709

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

  1711   by (fact lcm_neg2)

  1712

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

  1714   "euclid_ext a b =

  1715      (if b = 0 then

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

  1717       else

  1718         case euclid_ext b (a mod b) of

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

  1720   by (pat_completeness, simp)

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

  1722

  1723 declare euclid_ext.simps [simp del]

  1724

  1725 lemma euclid_ext_0:

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

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

  1728

  1729 lemma euclid_ext_non_0:

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

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

  1732   by (subst euclid_ext.simps) simp

  1733

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

  1735 where

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

  1737

  1738 lemma euclid_ext_gcd [simp]:

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

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

  1741   case (1 a b)

  1742   then show ?case

  1743   proof (cases "b = 0")

  1744     case True

  1745       then show ?thesis by

  1746         (simp add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)

  1747     next

  1748     case False with 1 show ?thesis

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

  1750     qed

  1751 qed

  1752

  1753 lemma euclid_ext_gcd' [simp]:

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

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

  1756

  1757 lemma euclid_ext_correct:

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

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

  1760   case (1 a b)

  1761   show ?case

  1762   proof (cases "b = 0")

  1763     case True

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

  1765   next

  1766     case False

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

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

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

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

  1771       by (simp add: algebra_simps)

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

  1773     finally show ?thesis

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

  1775     qed

  1776 qed

  1777

  1778 lemma euclid_ext'_correct:

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

  1780 proof-

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

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

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

  1784     show ?thesis unfolding euclid_ext'_def by simp

  1785 qed

  1786

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

  1788   using euclid_ext'_correct by blast

  1789

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

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

  1792

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

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

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

  1796     (simp add: euclid_ext'_def euclid_ext_non_0)

  1797

  1798 end

  1799

  1800 instantiation nat :: euclidean_semiring

  1801 begin

  1802

  1803 definition [simp]:

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

  1805

  1806 definition [simp]:

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

  1808

  1809 instance proof

  1810 qed simp_all

  1811

  1812 end

  1813

  1814 instantiation int :: euclidean_ring

  1815 begin

  1816

  1817 definition [simp]:

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

  1819

  1820 definition [simp]:

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

  1822

  1823 instance proof

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

  1825 next

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

  1827 next

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

  1829 next

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

  1831 qed (auto simp: sgn_times split: abs_split)

  1832

  1833 end

  1834

  1835 end