src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Sun Nov 09 10:03:17 2014 +0100 (2014-11-09) changeset 58953 2e19b392d9e3 parent 58889 5b7a9633cfa8 child 59009 348561aa3869 permissions -rw-r--r--
self-contained simp rules for dvd on numerals
     1 (* Author: Manuel Eberl *)

     2

     3 section {* Abstract euclidean algorithm *}

     4

     5 theory Euclidean_Algorithm

     6 imports Complex_Main

     7 begin

     8

     9 lemma finite_int_set_iff_bounded_le:

    10   "finite (N::int set) = (\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m)"

    11 proof

    12   assume "finite (N::int set)"

    13   hence "finite (nat  abs  N)" by (intro finite_imageI)

    14   hence "\<exists>m. \<forall>n\<in>natabsN. n \<le> m" by (simp add: finite_nat_set_iff_bounded_le)

    15   then obtain m :: nat where "\<forall>n\<in>N. nat (abs n) \<le> nat (int m)" by auto

    16   then show "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m" by (intro exI[of _ "int m"]) (auto simp: nat_le_eq_zle)

    17 next

    18   assume "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m"

    19   then obtain m where "m \<ge> 0" and "\<forall>n\<in>N. abs n \<le> m" by blast

    20   hence "\<forall>n\<in>N. nat (abs n) \<le> nat m" by (auto simp: nat_le_eq_zle)

    21   hence "\<forall>n\<in>natabsN. n \<le> nat m" by (auto simp: nat_le_eq_zle)

    22   hence A: "finite ((nat \<circ> abs)N)" unfolding o_def

    23       by (subst finite_nat_set_iff_bounded_le) blast

    24   {

    25     assume "\<not>finite N"

    26     from pigeonhole_infinite[OF this A] obtain x

    27        where "x \<in> N" and B: "~finite {a\<in>N. nat (abs a) = nat (abs x)}"

    28        unfolding o_def by blast

    29     have "{a\<in>N. nat (abs a) = nat (abs x)} \<subseteq> {x, -x}" by auto

    30     hence "finite {a\<in>N. nat (abs a) = nat (abs x)}" by (rule finite_subset) simp

    31     with B have False by contradiction

    32   }

    33   then show "finite N" by blast

    34 qed

    35

    36 context semiring_div

    37 begin

    38

    39 lemma dvd_setprod [intro]:

    40   assumes "finite A" and "x \<in> A"

    41   shows "f x dvd setprod f A"

    42 proof

    43   from finite A have "setprod f (insert x (A - {x})) = f x * setprod f (A - {x})"

    44     by (intro setprod.insert) auto

    45   also from x \<in> A have "insert x (A - {x}) = A" by blast

    46   finally show "setprod f A = f x * setprod f (A - {x})" .

    47 qed

    48

    49 lemma dvd_mult_cancel_left:

    50   assumes "a \<noteq> 0" and "a * b dvd a * c"

    51   shows "b dvd c"

    52 proof-

    53   from assms(2) obtain k where "a * c = a * b * k" unfolding dvd_def by blast

    54   hence "c * a = b * k * a" by (simp add: ac_simps)

    55   hence "c * (a div a) = b * k * (a div a)" by (simp add: div_mult_swap)

    56   also from a \<noteq> 0 have "a div a = 1" by simp

    57   finally show ?thesis by simp

    58 qed

    59

    60 lemma dvd_mult_cancel_right:

    61   "a \<noteq> 0 \<Longrightarrow> b * a dvd c * a \<Longrightarrow> b dvd c"

    62   by (subst (asm) (1 2) ac_simps, rule dvd_mult_cancel_left)

    63

    64 lemma nonzero_pow_nonzero:

    65   "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"

    66   by (induct n) (simp_all add: no_zero_divisors)

    67

    68 lemma zero_pow_zero: "n \<noteq> 0 \<Longrightarrow> 0 ^ n = 0"

    69   by (cases n, simp_all)

    70

    71 lemma pow_zero_iff:

    72   "n \<noteq> 0 \<Longrightarrow> a^n = 0 \<longleftrightarrow> a = 0"

    73   using nonzero_pow_nonzero zero_pow_zero by auto

    74

    75 end

    76

    77 context semiring_div

    78 begin

    79

    80 definition ring_inv :: "'a \<Rightarrow> 'a"

    81 where

    82   "ring_inv x = 1 div x"

    83

    84 definition is_unit :: "'a \<Rightarrow> bool"

    85 where

    86   "is_unit x \<longleftrightarrow> x dvd 1"

    87

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

    89 where

    90   "associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"

    91

    92 lemma unit_prod [intro]:

    93   "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"

    94   unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)

    95

    96 lemma unit_ring_inv:

    97   "is_unit y \<Longrightarrow> x div y = x * ring_inv y"

    98   by (simp add: div_mult_swap ring_inv_def is_unit_def)

    99

   100 lemma unit_ring_inv_ring_inv [simp]:

   101   "is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"

   102   unfolding is_unit_def ring_inv_def

   103   by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)

   104

   105 lemma inv_imp_eq_ring_inv:

   106   "a * b = 1 \<Longrightarrow> ring_inv a = b"

   107   by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)

   108

   109 lemma ring_inv_is_inv1 [simp]:

   110   "is_unit a \<Longrightarrow> a * ring_inv a = 1"

   111   unfolding is_unit_def ring_inv_def by simp

   112

   113 lemma ring_inv_is_inv2 [simp]:

   114   "is_unit a \<Longrightarrow> ring_inv a * a = 1"

   115   by (simp add: ac_simps)

   116

   117 lemma unit_ring_inv_unit [simp, intro]:

   118   assumes "is_unit x"

   119   shows "is_unit (ring_inv x)"

   120 proof -

   121   from assms have "1 = ring_inv x * x" by simp

   122   then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)

   123 qed

   124

   125 lemma mult_unit_dvd_iff:

   126   "is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"

   127 proof

   128   assume "is_unit y" "x * y dvd z"

   129   then show "x dvd z" by (simp add: dvd_mult_left)

   130 next

   131   assume "is_unit y" "x dvd z"

   132   then obtain k where "z = x * k" unfolding dvd_def by blast

   133   with is_unit y have "z = (x * y) * (ring_inv y * k)"

   134       by (simp add: mult_ac)

   135   then show "x * y dvd z" by (rule dvdI)

   136 qed

   137

   138 lemma div_unit_dvd_iff:

   139   "is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"

   140   by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)

   141

   142 lemma dvd_mult_unit_iff:

   143   "is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"

   144 proof

   145   assume "is_unit y" and "x dvd z * y"

   146   have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp

   147   also from is_unit y have "y * ring_inv y = 1" by simp

   148   finally have "z * y dvd z" by simp

   149   with x dvd z * y show "x dvd z" by (rule dvd_trans)

   150 next

   151   assume "x dvd z"

   152   then show "x dvd z * y" by simp

   153 qed

   154

   155 lemma dvd_div_unit_iff:

   156   "is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"

   157   by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)

   158

   159 lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff

   160

   161 lemma unit_div [intro]:

   162   "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"

   163   by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)

   164

   165 lemma unit_div_mult_swap:

   166   "is_unit z \<Longrightarrow> x * (y div z) = x * y div z"

   167   by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)

   168

   169 lemma unit_div_commute:

   170   "is_unit y \<Longrightarrow> x div y * z = x * z div y"

   171   by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)

   172

   173 lemma unit_imp_dvd [dest]:

   174   "is_unit y \<Longrightarrow> y dvd x"

   175   by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)

   176

   177 lemma dvd_unit_imp_unit:

   178   "is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"

   179   by (unfold is_unit_def) (rule dvd_trans)

   180

   181 lemma ring_inv_0 [simp]:

   182   "ring_inv 0 = 0"

   183   unfolding ring_inv_def by simp

   184

   185 lemma unit_ring_inv'1:

   186   assumes "is_unit y"

   187   shows "x div (y * z) = x * ring_inv y div z"

   188 proof -

   189   from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"

   190     by simp

   191   also have "... = y * (x * ring_inv y) div (y * z)"

   192     by (simp only: mult_ac)

   193   also have "... = x * ring_inv y div z"

   194     by (cases "y = 0", simp, rule div_mult_mult1)

   195   finally show ?thesis .

   196 qed

   197

   198 lemma associated_comm:

   199   "associated x y \<Longrightarrow> associated y x"

   200   by (simp add: associated_def)

   201

   202 lemma associated_0 [simp]:

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

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

   205   unfolding associated_def by simp_all

   206

   207 lemma associated_unit:

   208   "is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"

   209   unfolding associated_def by (fast dest: dvd_unit_imp_unit)

   210

   211 lemma is_unit_1 [simp]:

   212   "is_unit 1"

   213   unfolding is_unit_def by simp

   214

   215 lemma not_is_unit_0 [simp]:

   216   "\<not> is_unit 0"

   217   unfolding is_unit_def by auto

   218

   219 lemma unit_mult_left_cancel:

   220   assumes "is_unit x"

   221   shows "(x * y) = (x * z) \<longleftrightarrow> y = z"

   222 proof -

   223   from assms have "x \<noteq> 0" by auto

   224   then show ?thesis by (metis div_mult_self1_is_id)

   225 qed

   226

   227 lemma unit_mult_right_cancel:

   228   "is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"

   229   by (simp add: ac_simps unit_mult_left_cancel)

   230

   231 lemma unit_div_cancel:

   232   "is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"

   233   apply (subst unit_ring_inv[of _ y], assumption)

   234   apply (subst unit_ring_inv[of _ z], assumption)

   235   apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)

   236   done

   237

   238 lemma unit_eq_div1:

   239   "is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"

   240   apply (subst unit_ring_inv, assumption)

   241   apply (subst unit_mult_right_cancel[symmetric], assumption)

   242   apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)

   243   done

   244

   245 lemma unit_eq_div2:

   246   "is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"

   247   by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)

   248

   249 lemma associated_iff_div_unit:

   250   "associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"

   251 proof

   252   assume "associated x y"

   253   show "\<exists>z. is_unit z \<and> x = z * y"

   254   proof (cases "x = 0")

   255     assume "x = 0"

   256     then show "\<exists>z. is_unit z \<and> x = z * y" using associated x y

   257         by (intro exI[of _ 1], simp add: associated_def)

   258   next

   259     assume [simp]: "x \<noteq> 0"

   260     hence [simp]: "x dvd y" "y dvd x" using associated x y

   261         unfolding associated_def by simp_all

   262     hence "1 = x div y * (y div x)"

   263       by (simp add: div_mult_swap dvd_div_mult_self)

   264     hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)

   265     moreover have "x = (x div y) * y" by (simp add: dvd_div_mult_self)

   266     ultimately show ?thesis by blast

   267   qed

   268 next

   269   assume "\<exists>z. is_unit z \<and> x = z * y"

   270   then obtain z where "is_unit z" and "x = z * y" by blast

   271   hence "y = x * ring_inv z" by (simp add: algebra_simps)

   272   hence "x dvd y" by simp

   273   moreover from x = z * y have "y dvd x" by simp

   274   ultimately show "associated x y" unfolding associated_def by simp

   275 qed

   276

   277 lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff

   278   dvd_div_unit_iff unit_div_mult_swap unit_div_commute

   279   unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel

   280   unit_eq_div1 unit_eq_div2

   281

   282 end

   283

   284 context ring_div

   285 begin

   286

   287 lemma is_unit_neg [simp]:

   288   "is_unit (- x) \<Longrightarrow> is_unit x"

   289   unfolding is_unit_def by simp

   290

   291 lemma is_unit_neg_1 [simp]:

   292   "is_unit (-1)"

   293   unfolding is_unit_def by simp

   294

   295 end

   296

   297 lemma is_unit_nat [simp]:

   298   "is_unit (x::nat) \<longleftrightarrow> x = 1"

   299   unfolding is_unit_def by simp

   300

   301 lemma is_unit_int:

   302   "is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = -1"

   303   unfolding is_unit_def by auto

   304

   305 text {*

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

   307   implemented. It must provide:

   308   \begin{itemize}

   309   \item division with remainder

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

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

   312   \item a normalisation factor such that two associated numbers are equal iff

   313         they are the same when divided by their normalisation factors.

   314   \end{itemize}

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

   316   for any Euclidean semiring.

   317 *}

   318 class euclidean_semiring = semiring_div +

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

   320   fixes normalisation_factor :: "'a \<Rightarrow> 'a"

   321   assumes mod_size_less [simp]:

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

   323   assumes size_mult_mono:

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

   325   assumes normalisation_factor_is_unit [intro,simp]:

   326     "a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"

   327   assumes normalisation_factor_mult: "normalisation_factor (a * b) =

   328     normalisation_factor a * normalisation_factor b"

   329   assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"

   330   assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"

   331 begin

   332

   333 lemma normalisation_factor_dvd [simp]:

   334   "a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"

   335   by (rule unit_imp_dvd, simp)

   336

   337 lemma normalisation_factor_1 [simp]:

   338   "normalisation_factor 1 = 1"

   339   by (simp add: normalisation_factor_unit)

   340

   341 lemma normalisation_factor_0_iff [simp]:

   342   "normalisation_factor x = 0 \<longleftrightarrow> x = 0"

   343 proof

   344   assume "normalisation_factor x = 0"

   345   hence "\<not> is_unit (normalisation_factor x)"

   346     by (metis not_is_unit_0)

   347   then show "x = 0" by force

   348 next

   349   assume "x = 0"

   350   then show "normalisation_factor x = 0" by simp

   351 qed

   352

   353 lemma normalisation_factor_pow:

   354   "normalisation_factor (x ^ n) = normalisation_factor x ^ n"

   355   by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)

   356

   357 lemma normalisation_correct [simp]:

   358   "normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"

   359 proof (cases "x = 0", simp)

   360   assume "x \<noteq> 0"

   361   let ?nf = "normalisation_factor"

   362   from normalisation_factor_is_unit[OF x \<noteq> 0] have "?nf x \<noteq> 0"

   363     by (metis not_is_unit_0)

   364   have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"

   365     by (simp add: normalisation_factor_mult)

   366   also have "x div ?nf x * ?nf x = x" using x \<noteq> 0

   367     by (simp add: dvd_div_mult_self)

   368   also have "?nf (?nf x) = ?nf x" using x \<noteq> 0

   369     normalisation_factor_is_unit normalisation_factor_unit by simp

   370   finally show ?thesis using x \<noteq> 0 and ?nf x \<noteq> 0

   371     by (metis div_mult_self2_is_id div_self)

   372 qed

   373

   374 lemma normalisation_0_iff [simp]:

   375   "x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"

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

   377

   378 lemma associated_iff_normed_eq:

   379   "associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"

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

   381   let ?nf = normalisation_factor

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

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

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

   385     apply (subst div_mult_swap, simp, simp)

   386     done

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

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

   389   with associated_iff_div_unit show "associated a b" by simp

   390 next

   391   let ?nf = normalisation_factor

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

   393   with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast

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

   395     apply (simp only: a = z * b normalisation_factor_mult normalisation_factor_unit)

   396     apply (rule div_mult_mult1, force)

   397     done

   398   qed

   399

   400 lemma normed_associated_imp_eq:

   401   "associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"

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

   403

   404 lemmas normalisation_factor_dvd_iff [simp] =

   405   unit_dvd_iff [OF normalisation_factor_is_unit]

   406

   407 lemma euclidean_division:

   408   fixes a :: 'a and b :: 'a

   409   assumes "b \<noteq> 0"

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

   411     and "euclidean_size t < euclidean_size b"

   412 proof -

   413   from div_mod_equality[of a b 0]

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

   415   with that and assms show ?thesis by force

   416 qed

   417

   418 lemma dvd_euclidean_size_eq_imp_dvd:

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

   420   shows "a dvd b"

   421 proof (subst dvd_eq_mod_eq_0, rule ccontr)

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

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

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

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

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

   427       using size_mult_mono by force

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

   429       using mod_size_less by blast

   430   ultimately show False using size_eq by simp

   431 qed

   432

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

   434 where

   435   "gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"

   436   by (pat_completeness, simp)

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

   438

   439 declare gcd_eucl.simps [simp del]

   440

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

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

   443   case ("1" m n)

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

   445 qed

   446

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

   448 where

   449   "lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"

   450

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

   452      for infinite sets as well *)

   453

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

   455 where

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

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

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

   459        in l div normalisation_factor l

   460       else 0)"

   461

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

   463 where

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

   465

   466 end

   467

   468 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   471 begin

   472

   473 lemma gcd_red:

   474   "gcd x y = gcd y (x mod y)"

   475   by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)

   476

   477 lemma gcd_non_0:

   478   "y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"

   479   by (rule gcd_red)

   480

   481 lemma gcd_0_left:

   482   "gcd 0 x = x div normalisation_factor x"

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

   484

   485 lemma gcd_0:

   486   "gcd x 0 = x div normalisation_factor x"

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

   488

   489 lemma gcd_dvd1 [iff]: "gcd x y dvd x"

   490   and gcd_dvd2 [iff]: "gcd x y dvd y"

   491 proof (induct x y rule: gcd_eucl.induct)

   492   fix x y :: 'a

   493   assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"

   494   assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"

   495

   496   have "gcd x y dvd x \<and> gcd x y dvd y"

   497   proof (cases "y = 0")

   498     case True

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

   500   next

   501     case False

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

   503   qed

   504   then show "gcd x y dvd x" "gcd x y dvd y" by simp_all

   505 qed

   506

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

   508   by (rule dvd_trans, assumption, rule gcd_dvd1)

   509

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

   511   by (rule dvd_trans, assumption, rule gcd_dvd2)

   512

   513 lemma gcd_greatest:

   514   fixes k x y :: 'a

   515   shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"

   516 proof (induct x y rule: gcd_eucl.induct)

   517   case (1 x y)

   518   show ?case

   519     proof (cases "y = 0")

   520       assume "y = 0"

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

   522     next

   523       assume "y \<noteq> 0"

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

   525     qed

   526 qed

   527

   528 lemma dvd_gcd_iff:

   529   "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"

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

   531

   532 lemmas gcd_greatest_iff = dvd_gcd_iff

   533

   534 lemma gcd_zero [simp]:

   535   "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"

   536   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   537

   538 lemma normalisation_factor_gcd [simp]:

   539   "normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")

   540 proof (induct x y rule: gcd_eucl.induct)

   541   fix x y :: 'a

   542   assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"

   543   then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)

   544 qed

   545

   546 lemma gcdI:

   547   "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)

   548     \<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"

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

   550

   551 sublocale gcd!: abel_semigroup gcd

   552 proof

   553   fix x y z

   554   show "gcd (gcd x y) z = gcd x (gcd y z)"

   555   proof (rule gcdI)

   556     have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all

   557     then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)

   558     have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all

   559     hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)

   560     moreover have "gcd (gcd x y) z dvd z" by simp

   561     ultimately show "gcd (gcd x y) z dvd gcd y z"

   562       by (rule gcd_greatest)

   563     show "normalisation_factor (gcd (gcd x y) z) =  (if gcd (gcd x y) z = 0 then 0 else 1)"

   564       by auto

   565     fix l assume "l dvd x" and "l dvd gcd y z"

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

   567       have "l dvd y" and "l dvd z" by blast+

   568     with l dvd x show "l dvd gcd (gcd x y) z"

   569       by (intro gcd_greatest)

   570   qed

   571 next

   572   fix x y

   573   show "gcd x y = gcd y x"

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

   575 qed

   576

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

   578     normalisation_factor d = (if d = 0 then 0 else 1) \<and>

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

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

   581

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

   583   using mult_dvd_mono [of 1] by auto

   584

   585 lemma gcd_1_left [simp]: "gcd 1 x = 1"

   586   by (rule sym, rule gcdI, simp_all)

   587

   588 lemma gcd_1 [simp]: "gcd x 1 = 1"

   589   by (rule sym, rule gcdI, simp_all)

   590

   591 lemma gcd_proj2_if_dvd:

   592   "y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"

   593   by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)

   594

   595 lemma gcd_proj1_if_dvd:

   596   "x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"

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

   598

   599 lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"

   600 proof

   601   assume A: "gcd m n = m div normalisation_factor m"

   602   show "m dvd n"

   603   proof (cases "m = 0")

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

   605     from A have B: "m = gcd m n * normalisation_factor m"

   606       by (simp add: unit_eq_div2)

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

   608   qed (insert A, simp)

   609 next

   610   assume "m dvd n"

   611   then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)

   612 qed

   613

   614 lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"

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

   616

   617 lemma gcd_mod1 [simp]:

   618   "gcd (x mod y) y = gcd x y"

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

   620

   621 lemma gcd_mod2 [simp]:

   622   "gcd x (y mod x) = gcd x y"

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

   624

   625 lemma normalisation_factor_dvd' [simp]:

   626   "normalisation_factor x dvd x"

   627   by (cases "x = 0", simp_all)

   628

   629 lemma gcd_mult_distrib':

   630   "k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"

   631 proof (induct x y rule: gcd_eucl.induct)

   632   case (1 x y)

   633   show ?case

   634   proof (cases "y = 0")

   635     case True

   636     then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)

   637   next

   638     case False

   639     hence "k div normalisation_factor k * gcd x y =  gcd (k * y) (k * (x mod y))"

   640       using 1 by (subst gcd_red, simp)

   641     also have "... = gcd (k * x) (k * y)"

   642       by (simp add: mult_mod_right gcd.commute)

   643     finally show ?thesis .

   644   qed

   645 qed

   646

   647 lemma gcd_mult_distrib:

   648   "k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"

   649 proof-

   650   let ?nf = "normalisation_factor"

   651   from gcd_mult_distrib'

   652     have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..

   653   also have "... = k * gcd x y div ?nf k"

   654     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)

   655   finally show ?thesis

   656     by (simp add: ac_simps dvd_mult_div_cancel)

   657 qed

   658

   659 lemma euclidean_size_gcd_le1 [simp]:

   660   assumes "a \<noteq> 0"

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

   662 proof -

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

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

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

   666 qed

   667

   668 lemma euclidean_size_gcd_le2 [simp]:

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

   670   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   671

   672 lemma euclidean_size_gcd_less1:

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

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

   675 proof (rule ccontr)

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

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

   678     by (intro le_antisym, simp_all)

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

   680   hence "a dvd b" using dvd_gcd_D2 by blast

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

   682 qed

   683

   684 lemma euclidean_size_gcd_less2:

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

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

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

   688

   689 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"

   690   apply (rule gcdI)

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

   692   apply (rule gcd_dvd2)

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

   694   apply (subst normalisation_factor_gcd, simp add: gcd_0)

   695   done

   696

   697 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"

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

   699

   700 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"

   701   by (simp add: unit_ring_inv gcd_mult_unit1)

   702

   703 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"

   704   by (simp add: unit_ring_inv gcd_mult_unit2)

   705

   706 lemma gcd_idem: "gcd x x = x div normalisation_factor x"

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

   708

   709 lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"

   710   apply (rule gcdI)

   711   apply (simp add: ac_simps)

   712   apply (rule gcd_dvd2)

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

   714   apply (simp add: gcd_zero)

   715   done

   716

   717 lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"

   718   apply (rule gcdI)

   719   apply simp

   720   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

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

   722   apply (simp add: gcd_zero)

   723   done

   724

   725 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   726 proof

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

   728     by (simp add: fun_eq_iff ac_simps)

   729 next

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

   731     by (simp add: fun_eq_iff gcd_left_idem)

   732 qed

   733

   734 lemma coprime_dvd_mult:

   735   assumes "gcd k n = 1" and "k dvd m * n"

   736   shows "k dvd m"

   737 proof -

   738   let ?nf = "normalisation_factor"

   739   from assms gcd_mult_distrib [of m k n]

   740     have A: "m = gcd (m * k) (m * n) * ?nf m" by simp

   741   from k dvd m * n show ?thesis by (subst A, simp_all add: gcd_greatest)

   742 qed

   743

   744 lemma coprime_dvd_mult_iff:

   745   "gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"

   746   by (rule, rule coprime_dvd_mult, simp_all)

   747

   748 lemma gcd_dvd_antisym:

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

   750 proof (rule gcdI)

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

   752   have "gcd c d dvd c" by simp

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

   754   have "gcd c d dvd d" by simp

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

   756   show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"

   757     by (simp add: gcd_zero)

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

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

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

   761 qed

   762

   763 lemma gcd_mult_cancel:

   764   assumes "gcd k n = 1"

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

   766 proof (rule gcd_dvd_antisym)

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

   768   also note gcd k n = 1

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

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

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

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

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

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

   775 qed

   776

   777 lemma coprime_crossproduct:

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

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

   780 proof

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

   782 next

   783   assume ?lhs

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

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

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

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

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

   789     unfolding associated_def by (metis dvd_mult_right ac_simps)

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

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

   792     unfolding associated_def by (metis dvd_mult_right ac_simps)

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

   794   ultimately show ?rhs unfolding associated_def by simp

   795 qed

   796

   797 lemma gcd_add1 [simp]:

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

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

   800

   801 lemma gcd_add2 [simp]:

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

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

   804

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

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

   807

   808 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"

   809   by (rule sym, rule gcdI, simp_all)

   810

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

   812   by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   813

   814 lemma div_gcd_coprime:

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

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

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

   818   shows "gcd a' b' = 1"

   819 proof (rule coprimeI)

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

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

   822   moreover have "a = a' * d" "b = b' * d" by (simp_all add: dvd_div_mult_self)

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

   824     by (metis ac_simps)+

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

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

   827   then obtain u where "u * l * d = d" unfolding dvd_def

   828     by (metis ac_simps mult_assoc)

   829   moreover from nz have "d \<noteq> 0" by (simp add: gcd_zero)

   830   ultimately have "u * l = 1"

   831     by (metis div_mult_self1_is_id div_self ac_simps)

   832   then show "l dvd 1" by force

   833 qed

   834

   835 lemma coprime_mult:

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

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

   838   apply (subst gcd.commute)

   839   using da apply (subst gcd_mult_cancel)

   840   apply (subst gcd.commute, assumption)

   841   apply (subst gcd.commute, rule db)

   842   done

   843

   844 lemma coprime_lmult:

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

   846   shows "gcd d a = 1"

   847 proof (rule coprimeI)

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

   849   hence "l dvd a * b" by simp

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

   851 qed

   852

   853 lemma coprime_rmult:

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

   855   shows "gcd d b = 1"

   856 proof (rule coprimeI)

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

   858   hence "l dvd a * b" by simp

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

   860 qed

   861

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

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

   864

   865 lemma gcd_coprime:

   866   assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"

   867   shows "gcd a' b' = 1"

   868 proof -

   869   from z have "a \<noteq> 0 \<or> b \<noteq> 0" by (simp add: gcd_zero)

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

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

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

   873   finally show ?thesis .

   874 qed

   875

   876 lemma coprime_power:

   877   assumes "0 < n"

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

   879 using assms proof (induct n)

   880   case (Suc n) then show ?case

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

   882 qed simp

   883

   884 lemma gcd_coprime_exists:

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

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

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

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

   889   apply (insert nz, auto simp add: dvd_div_mult gcd_0_left  gcd_zero intro: div_gcd_coprime)

   890   done

   891

   892 lemma coprime_exp:

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

   894   by (induct n, simp_all add: coprime_mult)

   895

   896 lemma coprime_exp2 [intro]:

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

   898   apply (rule coprime_exp)

   899   apply (subst gcd.commute)

   900   apply (rule coprime_exp)

   901   apply (subst gcd.commute)

   902   apply assumption

   903   done

   904

   905 lemma gcd_exp:

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

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

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

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

   910 next

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

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

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

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

   915   also note gcd_mult_distrib

   916   also have "normalisation_factor ((gcd a b)^n) = 1"

   917     by (simp add: normalisation_factor_pow A)

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

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

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

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

   922   finally show ?thesis by simp

   923 qed

   924

   925 lemma coprime_common_divisor:

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

   927   apply (subgoal_tac "x dvd gcd a b")

   928   apply (simp add: is_unit_def)

   929   apply (erule (1) gcd_greatest)

   930   done

   931

   932 lemma division_decomp:

   933   assumes dc: "a dvd b * c"

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

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

   936   assume "gcd a b = 0"

   937   hence "a = 0 \<and> b = 0" by (simp add: gcd_zero)

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

   939   then show ?thesis by blast

   940 next

   941   let ?d = "gcd a b"

   942   assume "?d \<noteq> 0"

   943   from gcd_coprime_exists[OF this]

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

   945     by blast

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

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

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

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

   950   with ?d \<noteq> 0 have "a' dvd b' * c" by (rule dvd_mult_cancel_left)

   951   with coprime_dvd_mult[OF ab'(3)]

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

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

   954   then show ?thesis by blast

   955 qed

   956

   957 lemma pow_divides_pow:

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

   959   shows "a dvd b"

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

   961   assume "gcd a b = 0"

   962   then show ?thesis by (simp add: gcd_zero)

   963 next

   964   let ?d = "gcd a b"

   965   assume "?d \<noteq> 0"

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

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

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

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

   970     by blast

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

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

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

   974     by (simp only: power_mult_distrib ac_simps)

   975   with zn have "a'^n dvd b'^n" by (rule dvd_mult_cancel_left)

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

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

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

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

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

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

   982 qed

   983

   984 lemma pow_divides_eq [simp]:

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

   986   by (auto intro: pow_divides_pow dvd_power_same)

   987

   988 lemma divides_mult:

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

   990   shows "m * n dvd r"

   991 proof -

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

   993     unfolding dvd_def by blast

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

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

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

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

   998   then show ?thesis unfolding dvd_def by blast

   999 qed

  1000

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

  1002   by (subst add_commute, simp)

  1003

  1004 lemma setprod_coprime [rule_format]:

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

  1006   apply (cases "finite A")

  1007   apply (induct set: finite)

  1008   apply (auto simp add: gcd_mult_cancel)

  1009   done

  1010

  1011 lemma coprime_divisors:

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

  1013   shows "gcd d e = 1"

  1014 proof -

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

  1016     unfolding dvd_def by blast

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

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

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

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

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

  1022 qed

  1023

  1024 lemma invertible_coprime:

  1025   "x * y mod m = 1 \<Longrightarrow> gcd x m = 1"

  1026   by (metis coprime_lmult gcd_1 ac_simps gcd_red)

  1027

  1028 lemma lcm_gcd:

  1029   "lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"

  1030   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

  1031

  1032 lemma lcm_gcd_prod:

  1033   "lcm a b * gcd a b = a * b div normalisation_factor (a*b)"

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

  1035   let ?nf = normalisation_factor

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

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

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

  1039     by (simp add: mult_ac)

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

  1041     by (simp_all add: unit_ring_inv'1 unit_ring_inv)

  1042   finally show ?thesis .

  1043 qed (auto simp add: lcm_gcd)

  1044

  1045 lemma lcm_dvd1 [iff]:

  1046   "x dvd lcm x y"

  1047 proof (cases "x*y = 0")

  1048   assume "x * y \<noteq> 0"

  1049   hence "gcd x y \<noteq> 0" by simp

  1050   let ?c = "ring_inv (normalisation_factor (x*y))"

  1051   from x * y \<noteq> 0 have [simp]: "is_unit (normalisation_factor (x*y))" by simp

  1052   from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"

  1053     by (simp add: mult_ac unit_ring_inv)

  1054   hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp

  1055   with gcd x y \<noteq> 0 have "lcm x y = x * ?c * y div gcd x y"

  1056     by (subst (asm) div_mult_self2_is_id, simp_all)

  1057   also have "... = x * (?c * y div gcd x y)"

  1058     by (metis div_mult_swap gcd_dvd2 mult_assoc)

  1059   finally show ?thesis by (rule dvdI)

  1060 qed (auto simp add: lcm_gcd)

  1061

  1062 lemma lcm_least:

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

  1064 proof (cases "k = 0")

  1065   let ?nf = normalisation_factor

  1066   assume "k \<noteq> 0"

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

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

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

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

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

  1072     unfolding dvd_def by blast

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  1088     by (simp add: algebra_simps)

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

  1090     by (metis div_mult_self2_is_id)

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

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

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

  1094     by (simp add: algebra_simps)

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

  1096     by (metis mult.commute div_mult_self2_is_id)

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

  1098     by (metis div_mult_self2_is_id mult_assoc)

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

  1100     by (simp add: unit_simps)

  1101   finally show ?thesis by (rule dvdI)

  1102 qed simp

  1103

  1104 lemma lcm_zero:

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

  1106 proof -

  1107   let ?nf = normalisation_factor

  1108   {

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

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

  1111     moreover from a \<noteq> 0 and b \<noteq> 0 have "gcd a b \<noteq> 0" by (simp add: gcd_zero)

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

  1113   } moreover {

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

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

  1116   }

  1117   ultimately show ?thesis by blast

  1118 qed

  1119

  1120 lemmas lcm_0_iff = lcm_zero

  1121

  1122 lemma gcd_lcm:

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

  1124   shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"

  1125 proof-

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

  1127   let ?c = "normalisation_factor (a*b)"

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

  1129   hence "is_unit ?c" by simp

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

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

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

  1133     by (simp only: unit_ring_inv'1 unit_ring_inv)

  1134   finally show ?thesis by (simp only: ac_simps)

  1135 qed

  1136

  1137 lemma normalisation_factor_lcm [simp]:

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

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

  1140   case True then show ?thesis

  1141     by (auto simp add: lcm_gcd)

  1142 next

  1143   case False

  1144   let ?nf = normalisation_factor

  1145   from lcm_gcd_prod[of a b]

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

  1147     by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)

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

  1149     by simp

  1150   finally show ?thesis using False by simp

  1151 qed

  1152

  1153 lemma lcm_dvd2 [iff]: "y dvd lcm x y"

  1154   using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)

  1155

  1156 lemma lcmI:

  1157   "\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;

  1158     normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"

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

  1160

  1161 sublocale lcm!: abel_semigroup lcm

  1162 proof

  1163   fix x y z

  1164   show "lcm (lcm x y) z = lcm x (lcm y z)"

  1165   proof (rule lcmI)

  1166     have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all

  1167     then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)

  1168

  1169     have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all

  1170     hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)

  1171     moreover have "z dvd lcm (lcm x y) z" by simp

  1172     ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)

  1173

  1174     fix l assume "x dvd l" and "lcm y z dvd l"

  1175     have "y dvd lcm y z" by simp

  1176     from this and lcm y z dvd l have "y dvd l" by (rule dvd_trans)

  1177     have "z dvd lcm y z" by simp

  1178     from this and lcm y z dvd l have "z dvd l" by (rule dvd_trans)

  1179     from x dvd l and y dvd l have "lcm x y dvd l" by (rule lcm_least)

  1180     from this and z dvd l show "lcm (lcm x y) z dvd l" by (rule lcm_least)

  1181   qed (simp add: lcm_zero)

  1182 next

  1183   fix x y

  1184   show "lcm x y = lcm y x"

  1185     by (simp add: lcm_gcd ac_simps)

  1186 qed

  1187

  1188 lemma dvd_lcm_D1:

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

  1190   by (rule dvd_trans, rule lcm_dvd1, assumption)

  1191

  1192 lemma dvd_lcm_D2:

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

  1194   by (rule dvd_trans, rule lcm_dvd2, assumption)

  1195

  1196 lemma gcd_dvd_lcm [simp]:

  1197   "gcd a b dvd lcm a b"

  1198   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

  1199

  1200 lemma lcm_1_iff:

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

  1202 proof

  1203   assume "lcm a b = 1"

  1204   then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto

  1205 next

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

  1207   hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all

  1208   hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)

  1209   hence "lcm a b = normalisation_factor (lcm a b)"

  1210     by (subst normalisation_factor_unit, simp_all)

  1211   also have "\<dots> = 1" using is_unit a \<and> is_unit b by (auto simp add: is_unit_def)

  1212   finally show "lcm a b = 1" .

  1213 qed

  1214

  1215 lemma lcm_0_left [simp]:

  1216   "lcm 0 x = 0"

  1217   by (rule sym, rule lcmI, simp_all)

  1218

  1219 lemma lcm_0 [simp]:

  1220   "lcm x 0 = 0"

  1221   by (rule sym, rule lcmI, simp_all)

  1222

  1223 lemma lcm_unique:

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

  1225   normalisation_factor d = (if d = 0 then 0 else 1) \<and>

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

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

  1228

  1229 lemma dvd_lcm_I1 [simp]:

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

  1231   by (metis lcm_dvd1 dvd_trans)

  1232

  1233 lemma dvd_lcm_I2 [simp]:

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

  1235   by (metis lcm_dvd2 dvd_trans)

  1236

  1237 lemma lcm_1_left [simp]:

  1238   "lcm 1 x = x div normalisation_factor x"

  1239   by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)

  1240

  1241 lemma lcm_1_right [simp]:

  1242   "lcm x 1 = x div normalisation_factor x"

  1243   by (simp add: ac_simps)

  1244

  1245 lemma lcm_coprime:

  1246   "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"

  1247   by (subst lcm_gcd) simp

  1248

  1249 lemma lcm_proj1_if_dvd:

  1250   "y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"

  1251   by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)

  1252

  1253 lemma lcm_proj2_if_dvd:

  1254   "x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"

  1255   using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)

  1256

  1257 lemma lcm_proj1_iff:

  1258   "lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"

  1259 proof

  1260   assume A: "lcm m n = m div normalisation_factor m"

  1261   show "n dvd m"

  1262   proof (cases "m = 0")

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

  1264     from A have B: "m = lcm m n * normalisation_factor m"

  1265       by (simp add: unit_eq_div2)

  1266     show ?thesis by (subst B, simp)

  1267   qed simp

  1268 next

  1269   assume "n dvd m"

  1270   then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)

  1271 qed

  1272

  1273 lemma lcm_proj2_iff:

  1274   "lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"

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

  1276

  1277 lemma euclidean_size_lcm_le1:

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

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

  1280 proof -

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

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

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

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

  1285 qed

  1286

  1287 lemma euclidean_size_lcm_le2:

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

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

  1290

  1291 lemma euclidean_size_lcm_less1:

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

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

  1294 proof (rule ccontr)

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

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

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

  1298     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

  1299   with assms have "lcm a b dvd a"

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

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

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

  1303 qed

  1304

  1305 lemma euclidean_size_lcm_less2:

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

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

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

  1309

  1310 lemma lcm_mult_unit1:

  1311   "is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"

  1312   apply (rule lcmI)

  1313   apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)

  1314   apply (rule lcm_dvd2)

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

  1316   apply (subst normalisation_factor_lcm, simp add: lcm_zero)

  1317   done

  1318

  1319 lemma lcm_mult_unit2:

  1320   "is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"

  1321   using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)

  1322

  1323 lemma lcm_div_unit1:

  1324   "is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"

  1325   by (simp add: unit_ring_inv lcm_mult_unit1)

  1326

  1327 lemma lcm_div_unit2:

  1328   "is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"

  1329   by (simp add: unit_ring_inv lcm_mult_unit2)

  1330

  1331 lemma lcm_left_idem:

  1332   "lcm p (lcm p q) = lcm p q"

  1333   apply (rule lcmI)

  1334   apply simp

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

  1336   apply (rule lcm_least, assumption)

  1337   apply (erule (1) lcm_least)

  1338   apply (auto simp: lcm_zero)

  1339   done

  1340

  1341 lemma lcm_right_idem:

  1342   "lcm (lcm p q) q = lcm p q"

  1343   apply (rule lcmI)

  1344   apply (subst lcm.assoc, rule lcm_dvd1)

  1345   apply (rule lcm_dvd2)

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

  1347   apply (auto simp: lcm_zero)

  1348   done

  1349

  1350 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

  1351 proof

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

  1353     by (simp add: fun_eq_iff ac_simps)

  1354 next

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

  1356     by (intro ext, simp add: lcm_left_idem)

  1357 qed

  1358

  1359 lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"

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

  1361   and normalisation_factor_Lcm [simp]:

  1362           "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"

  1363 proof -

  1364   have "(\<forall>x\<in>A. x dvd Lcm A) \<and> (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> Lcm A dvd l') \<and>

  1365     normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)

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

  1367     case False

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

  1369     with False show ?thesis by auto

  1370   next

  1371     case True

  1372     then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast

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

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

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

  1376       apply (subst n_def)

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

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

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

  1380       done

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

  1382       unfolding l_def by simp_all

  1383     {

  1384       fix l' assume "\<forall>x\<in>A. x dvd l'"

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

  1386       moreover from l \<noteq> 0 have "gcd l l' \<noteq> 0" by (simp add: gcd_zero)

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

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

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

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

  1391       proof -

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

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

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

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

  1396           by (rule size_mult_mono)

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

  1398         also note euclidean_size l = n

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

  1400       qed

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

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

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

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

  1405     }

  1406

  1407     with (\<forall>x\<in>A. x dvd l) and normalisation_factor_is_unit[OF l \<noteq> 0] and l \<noteq> 0

  1408       have "(\<forall>x\<in>A. x dvd l div normalisation_factor l) \<and>

  1409         (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> l div normalisation_factor l dvd l') \<and>

  1410         normalisation_factor (l div normalisation_factor l) =

  1411         (if l div normalisation_factor l = 0 then 0 else 1)"

  1412       by (auto simp: unit_simps)

  1413     also from True have "l div normalisation_factor l = Lcm A"

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

  1415     finally show ?thesis .

  1416   qed

  1417   note A = this

  1418

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

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

  1421   from A show "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast

  1422 qed

  1423

  1424 lemma LcmI:

  1425   "(\<And>x. x\<in>A \<Longrightarrow> x dvd l) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. x dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>

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

  1427   by (intro normed_associated_imp_eq)

  1428     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)

  1429

  1430 lemma Lcm_subset:

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

  1432   by (blast intro: Lcm_dvd dvd_Lcm)

  1433

  1434 lemma Lcm_Un:

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

  1436   apply (rule lcmI)

  1437   apply (blast intro: Lcm_subset)

  1438   apply (blast intro: Lcm_subset)

  1439   apply (intro Lcm_dvd ballI, elim UnE)

  1440   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1441   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1442   apply simp

  1443   done

  1444

  1445 lemma Lcm_1_iff:

  1446   "Lcm A = 1 \<longleftrightarrow> (\<forall>x\<in>A. is_unit x)"

  1447 proof

  1448   assume "Lcm A = 1"

  1449   then show "\<forall>x\<in>A. is_unit x" unfolding is_unit_def by auto

  1450 qed (rule LcmI [symmetric], auto)

  1451

  1452 lemma Lcm_no_units:

  1453   "Lcm A = Lcm (A - {x. is_unit x})"

  1454 proof -

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

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

  1457     by (simp add: Lcm_Un[symmetric])

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

  1459   finally show ?thesis by simp

  1460 qed

  1461

  1462 lemma Lcm_empty [simp]:

  1463   "Lcm {} = 1"

  1464   by (simp add: Lcm_1_iff)

  1465

  1466 lemma Lcm_eq_0 [simp]:

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

  1468   by (drule dvd_Lcm) simp

  1469

  1470 lemma Lcm0_iff':

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

  1472 proof

  1473   assume "Lcm A = 0"

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

  1475   proof

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

  1477     then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast

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

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

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

  1481       apply (subst n_def)

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

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

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

  1485       done

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

  1487     hence "l div normalisation_factor l \<noteq> 0" by simp

  1488     also from ex have "l div normalisation_factor l = Lcm A"

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

  1490     finally show False using Lcm A = 0 by contradiction

  1491   qed

  1492 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1493

  1494 lemma Lcm0_iff [simp]:

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

  1496 proof -

  1497   assume "finite A"

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

  1499   moreover {

  1500     assume "0 \<notin> A"

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

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

  1503       apply simp

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

  1505       apply (rule no_zero_divisors)

  1506       apply blast+

  1507       done

  1508     moreover from finite A have "\<forall>x\<in>A. x dvd \<Prod>A" by (intro ballI dvd_setprod)

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

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

  1511   }

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

  1513 qed

  1514

  1515 lemma Lcm_no_multiple:

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

  1517 proof -

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

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

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

  1521 qed

  1522

  1523 lemma Lcm_insert [simp]:

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

  1525 proof (rule lcmI)

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

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

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

  1529 qed (auto intro: Lcm_dvd dvd_Lcm)

  1530

  1531 lemma Lcm_finite:

  1532   assumes "finite A"

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

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

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

  1536

  1537 lemma Lcm_set [code, code_unfold]:

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

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

  1540

  1541 lemma Lcm_singleton [simp]:

  1542   "Lcm {a} = a div normalisation_factor a"

  1543   by simp

  1544

  1545 lemma Lcm_2 [simp]:

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

  1547   by (simp only: Lcm_insert Lcm_empty lcm_1_right)

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

  1549

  1550 lemma Lcm_coprime:

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

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

  1553   shows "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"

  1554 using assms proof (induct rule: finite_ne_induct)

  1555   case (insert a A)

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

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

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

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

  1560   with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalisation_factor (\<Prod>(insert a A))"

  1561     by (simp add: lcm_coprime)

  1562   finally show ?case .

  1563 qed simp

  1564

  1565 lemma Lcm_coprime':

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

  1567     \<Longrightarrow> Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"

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

  1569

  1570 lemma Gcd_Lcm:

  1571   "Gcd A = Lcm {d. \<forall>x\<in>A. d dvd x}"

  1572   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1573

  1574 lemma Gcd_dvd [simp]: "x \<in> A \<Longrightarrow> Gcd A dvd x"

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

  1576   and normalisation_factor_Gcd [simp]:

  1577     "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1578 proof -

  1579   fix x assume "x \<in> A"

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

  1581   then show "Gcd A dvd x" by (simp add: Gcd_Lcm)

  1582 next

  1583   fix g' assume "\<forall>x\<in>A. g' dvd x"

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

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

  1586 next

  1587   show "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1588     by (simp add: Gcd_Lcm normalisation_factor_Lcm)

  1589 qed

  1590

  1591 lemma GcdI:

  1592   "(\<And>x. x\<in>A \<Longrightarrow> l dvd x) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. l' dvd x) \<Longrightarrow> l' dvd l) \<Longrightarrow>

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

  1594   by (intro normed_associated_imp_eq)

  1595     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)

  1596

  1597 lemma Lcm_Gcd:

  1598   "Lcm A = Gcd {m. \<forall>x\<in>A. x dvd m}"

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

  1600

  1601 lemma Gcd_0_iff:

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

  1603   apply (rule iffI)

  1604   apply (rule subsetI, drule Gcd_dvd, simp)

  1605   apply (auto intro: GcdI[symmetric])

  1606   done

  1607

  1608 lemma Gcd_empty [simp]:

  1609   "Gcd {} = 0"

  1610   by (simp add: Gcd_0_iff)

  1611

  1612 lemma Gcd_1:

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

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

  1615

  1616 lemma Gcd_insert [simp]:

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

  1618 proof (rule gcdI)

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

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

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

  1622 qed (auto intro: Gcd_dvd dvd_Gcd simp: normalisation_factor_Gcd)

  1623

  1624 lemma Gcd_finite:

  1625   assumes "finite A"

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

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

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

  1629

  1630 lemma Gcd_set [code, code_unfold]:

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

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

  1633

  1634 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalisation_factor a"

  1635   by (simp add: gcd_0)

  1636

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

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

  1639

  1640 end

  1641

  1642 text {*

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

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

  1645 *}

  1646

  1647 class euclidean_ring = euclidean_semiring + idom

  1648

  1649 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1650 begin

  1651

  1652 subclass euclidean_ring ..

  1653

  1654 lemma gcd_neg1 [simp]:

  1655   "gcd (-x) y = gcd x y"

  1656   by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)

  1657

  1658 lemma gcd_neg2 [simp]:

  1659   "gcd x (-y) = gcd x y"

  1660   by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)

  1661

  1662 lemma gcd_neg_numeral_1 [simp]:

  1663   "gcd (- numeral n) x = gcd (numeral n) x"

  1664   by (fact gcd_neg1)

  1665

  1666 lemma gcd_neg_numeral_2 [simp]:

  1667   "gcd x (- numeral n) = gcd x (numeral n)"

  1668   by (fact gcd_neg2)

  1669

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

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

  1672

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

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

  1675

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

  1677 proof -

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

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

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

  1681   finally show ?thesis .

  1682 qed

  1683

  1684 lemma lcm_neg1 [simp]: "lcm (-x) y = lcm x y"

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

  1686

  1687 lemma lcm_neg2 [simp]: "lcm x (-y) = lcm x y"

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

  1689

  1690 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) x = lcm (numeral n) x"

  1691   by (fact lcm_neg1)

  1692

  1693 lemma lcm_neg_numeral_2 [simp]: "lcm x (- numeral n) = lcm x (numeral n)"

  1694   by (fact lcm_neg2)

  1695

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

  1697   "euclid_ext a b =

  1698      (if b = 0 then

  1699         let x = ring_inv (normalisation_factor a) in (x, 0, a * x)

  1700       else

  1701         case euclid_ext b (a mod b) of

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

  1703   by (pat_completeness, simp)

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

  1705

  1706 declare euclid_ext.simps [simp del]

  1707

  1708 lemma euclid_ext_0:

  1709   "euclid_ext a 0 = (ring_inv (normalisation_factor a), 0, a * ring_inv (normalisation_factor a))"

  1710   by (subst euclid_ext.simps, simp add: Let_def)

  1711

  1712 lemma euclid_ext_non_0:

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

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

  1715   by (subst euclid_ext.simps, simp)

  1716

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

  1718 where

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

  1720

  1721 lemma euclid_ext_gcd [simp]:

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

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

  1724   case (1 a b)

  1725   then show ?case

  1726   proof (cases "b = 0")

  1727     case True

  1728       then show ?thesis by (cases "a = 0")

  1729         (simp_all add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)

  1730     next

  1731     case False with 1 show ?thesis

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

  1733     qed

  1734 qed

  1735

  1736 lemma euclid_ext_gcd' [simp]:

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

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

  1739

  1740 lemma euclid_ext_correct:

  1741   "case euclid_ext x y of (s,t,c) \<Rightarrow> s*x + t*y = c"

  1742 proof (induct x y rule: euclid_ext.induct)

  1743   case (1 x y)

  1744   show ?case

  1745   proof (cases "y = 0")

  1746     case True

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

  1748   next

  1749     case False

  1750     obtain s t c where stc: "euclid_ext y (x mod y) = (s,t,c)"

  1751       by (cases "euclid_ext y (x mod y)", blast)

  1752     from 1 have "c = s * y + t * (x mod y)" by (simp add: stc False)

  1753     also have "... = t*((x div y)*y + x mod y) + (s - t * (x div y))*y"

  1754       by (simp add: algebra_simps)

  1755     also have "(x div y)*y + x mod y = x" using mod_div_equality .

  1756     finally show ?thesis

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

  1758     qed

  1759 qed

  1760

  1761 lemma euclid_ext'_correct:

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

  1763 proof-

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

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

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

  1767     show ?thesis unfolding euclid_ext'_def by simp

  1768 qed

  1769

  1770 lemma bezout: "\<exists>s t. s * x + t * y = gcd x y"

  1771   using euclid_ext'_correct by blast

  1772

  1773 lemma euclid_ext'_0 [simp]: "euclid_ext' x 0 = (ring_inv (normalisation_factor x), 0)"

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

  1775

  1776 lemma euclid_ext'_non_0: "y \<noteq> 0 \<Longrightarrow> euclid_ext' x y = (snd (euclid_ext' y (x mod y)),

  1777   fst (euclid_ext' y (x mod y)) - snd (euclid_ext' y (x mod y)) * (x div y))"

  1778   by (cases "euclid_ext y (x mod y)")

  1779     (simp add: euclid_ext'_def euclid_ext_non_0)

  1780

  1781 end

  1782

  1783 instantiation nat :: euclidean_semiring

  1784 begin

  1785

  1786 definition [simp]:

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

  1788

  1789 definition [simp]:

  1790   "normalisation_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"

  1791

  1792 instance proof

  1793 qed (simp_all add: is_unit_def)

  1794

  1795 end

  1796

  1797 instantiation int :: euclidean_ring

  1798 begin

  1799

  1800 definition [simp]:

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

  1802

  1803 definition [simp]:

  1804   "normalisation_factor_int = (sgn :: int \<Rightarrow> int)"

  1805

  1806 instance proof

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

  1808 next

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

  1810 next

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

  1812 next

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

  1814 qed (auto simp: sgn_times split: abs_split)

  1815

  1816 end

  1817

  1818 end

  1819
`