src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Mon Sep 26 07:56:54 2016 +0200 (2016-09-26) changeset 63947 559f0882d6a6 parent 63924 f91766530e13 child 64163 62c9e5c05928 permissions -rw-r--r--
more lemmas
     1 (* Author: Manuel Eberl *)

     2

     3 section \<open>Abstract euclidean algorithm\<close>

     4

     5 theory Euclidean_Algorithm

     6 imports "~~/src/HOL/GCD" Factorial_Ring

     7 begin

     8

     9 text \<open>

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

    11   implemented. It must provide:

    12   \begin{itemize}

    13   \item division with remainder

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

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

    16   \end{itemize}

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

    18   for any Euclidean semiring.

    19 \<close>

    20 class euclidean_semiring = semiring_div + normalization_semidom +

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

    22   assumes size_0 [simp]: "euclidean_size 0 = 0"

    23   assumes mod_size_less:

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

    25   assumes size_mult_mono:

    26     "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"

    27 begin

    28

    29 lemma euclidean_size_normalize [simp]:

    30   "euclidean_size (normalize a) = euclidean_size a"

    31 proof (cases "a = 0")

    32   case True

    33   then show ?thesis

    34     by simp

    35 next

    36   case [simp]: False

    37   have "euclidean_size (normalize a) \<le> euclidean_size (normalize a * unit_factor a)"

    38     by (rule size_mult_mono) simp

    39   moreover have "euclidean_size a \<le> euclidean_size (a * (1 div unit_factor a))"

    40     by (rule size_mult_mono) simp

    41   ultimately show ?thesis

    42     by simp

    43 qed

    44

    45 lemma euclidean_division:

    46   fixes a :: 'a and b :: 'a

    47   assumes "b \<noteq> 0"

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

    49     and "euclidean_size t < euclidean_size b"

    50 proof -

    51   from div_mod_equality [of a b 0]

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

    53   with that and assms show ?thesis by (auto simp add: mod_size_less)

    54 qed

    55

    56 lemma dvd_euclidean_size_eq_imp_dvd:

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

    58   shows "a dvd b"

    59 proof (rule ccontr)

    60   assume "\<not> a dvd b"

    61   then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)

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

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

    64     with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto

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

    66       using size_mult_mono by force

    67   moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>

    68   have "euclidean_size (b mod a) < euclidean_size a"

    69       using mod_size_less by blast

    70   ultimately show False using size_eq by simp

    71 qed

    72

    73 lemma size_mult_mono': "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (b * a)"

    74   by (subst mult.commute) (rule size_mult_mono)

    75

    76 lemma euclidean_size_times_unit:

    77   assumes "is_unit a"

    78   shows   "euclidean_size (a * b) = euclidean_size b"

    79 proof (rule antisym)

    80   from assms have [simp]: "a \<noteq> 0" by auto

    81   thus "euclidean_size (a * b) \<ge> euclidean_size b" by (rule size_mult_mono')

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

    83   hence "1 div a \<noteq> 0" by (intro notI) simp_all

    84   hence "euclidean_size (a * b) \<le> euclidean_size ((1 div a) * (a * b))"

    85     by (rule size_mult_mono')

    86   also from assms have "(1 div a) * (a * b) = b"

    87     by (simp add: algebra_simps unit_div_mult_swap)

    88   finally show "euclidean_size (a * b) \<le> euclidean_size b" .

    89 qed

    90

    91 lemma euclidean_size_unit: "is_unit x \<Longrightarrow> euclidean_size x = euclidean_size 1"

    92   using euclidean_size_times_unit[of x 1] by simp

    93

    94 lemma unit_iff_euclidean_size:

    95   "is_unit x \<longleftrightarrow> euclidean_size x = euclidean_size 1 \<and> x \<noteq> 0"

    96 proof safe

    97   assume A: "x \<noteq> 0" and B: "euclidean_size x = euclidean_size 1"

    98   show "is_unit x" by (rule dvd_euclidean_size_eq_imp_dvd[OF A _ B]) simp_all

    99 qed (auto intro: euclidean_size_unit)

   100

   101 lemma euclidean_size_times_nonunit:

   102   assumes "a \<noteq> 0" "b \<noteq> 0" "\<not>is_unit a"

   103   shows   "euclidean_size b < euclidean_size (a * b)"

   104 proof (rule ccontr)

   105   assume "\<not>euclidean_size b < euclidean_size (a * b)"

   106   with size_mult_mono'[OF assms(1), of b]

   107     have eq: "euclidean_size (a * b) = euclidean_size b" by simp

   108   have "a * b dvd b"

   109     by (rule dvd_euclidean_size_eq_imp_dvd[OF _ _ eq]) (insert assms, simp_all)

   110   hence "a * b dvd 1 * b" by simp

   111   with \<open>b \<noteq> 0\<close> have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff)

   112   with assms(3) show False by contradiction

   113 qed

   114

   115 lemma dvd_imp_size_le:

   116   assumes "x dvd y" "y \<noteq> 0"

   117   shows   "euclidean_size x \<le> euclidean_size y"

   118   using assms by (auto elim!: dvdE simp: size_mult_mono)

   119

   120 lemma dvd_proper_imp_size_less:

   121   assumes "x dvd y" "\<not>y dvd x" "y \<noteq> 0"

   122   shows   "euclidean_size x < euclidean_size y"

   123 proof -

   124   from assms(1) obtain z where "y = x * z" by (erule dvdE)

   125   hence z: "y = z * x" by (simp add: mult.commute)

   126   from z assms have "\<not>is_unit z" by (auto simp: mult.commute mult_unit_dvd_iff)

   127   with z assms show ?thesis

   128     by (auto intro!: euclidean_size_times_nonunit simp: )

   129 qed

   130

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

   132 where

   133   "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"

   134   by pat_completeness simp

   135 termination

   136   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)

   137

   138 declare gcd_eucl.simps [simp del]

   139

   140 lemma gcd_eucl_induct [case_names zero mod]:

   141   assumes H1: "\<And>b. P b 0"

   142   and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"

   143   shows "P a b"

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

   145   case ("1" a b)

   146   show ?case

   147   proof (cases "b = 0")

   148     case True then show "P a b" by simp (rule H1)

   149   next

   150     case False

   151     then have "P b (a mod b)"

   152       by (rule "1.hyps")

   153     with \<open>b \<noteq> 0\<close> show "P a b"

   154       by (blast intro: H2)

   155   qed

   156 qed

   157

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

   159 where

   160   "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"

   161

   162 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" \<comment> \<open>

   163   Somewhat complicated definition of Lcm that has the advantage of working

   164   for infinite sets as well\<close>

   165 where

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

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

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

   169        in normalize l

   170       else 0)"

   171

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

   173 where

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

   175

   176 declare Lcm_eucl_def Gcd_eucl_def [code del]

   177

   178 lemma gcd_eucl_0:

   179   "gcd_eucl a 0 = normalize a"

   180   by (simp add: gcd_eucl.simps [of a 0])

   181

   182 lemma gcd_eucl_0_left:

   183   "gcd_eucl 0 a = normalize a"

   184   by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])

   185

   186 lemma gcd_eucl_non_0:

   187   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"

   188   by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])

   189

   190 lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"

   191   and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"

   192   by (induct a b rule: gcd_eucl_induct)

   193      (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)

   194

   195 lemma normalize_gcd_eucl [simp]:

   196   "normalize (gcd_eucl a b) = gcd_eucl a b"

   197   by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)

   198

   199 lemma gcd_eucl_greatest:

   200   fixes k a b :: 'a

   201   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"

   202 proof (induct a b rule: gcd_eucl_induct)

   203   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)

   204 next

   205   case (mod a b)

   206   then show ?case

   207     by (simp add: gcd_eucl_non_0 dvd_mod_iff)

   208 qed

   209

   210 lemma gcd_euclI:

   211   fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"

   212   assumes "d dvd a" "d dvd b" "normalize d = d"

   213           "\<And>k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd d"

   214   shows   "gcd_eucl a b = d"

   215   by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)

   216

   217 lemma eq_gcd_euclI:

   218   fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"

   219   assumes "\<And>a b. gcd a b dvd a" "\<And>a b. gcd a b dvd b" "\<And>a b. normalize (gcd a b) = gcd a b"

   220           "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"

   221   shows   "gcd = gcd_eucl"

   222   by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)

   223

   224 lemma gcd_eucl_zero [simp]:

   225   "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"

   226   by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+

   227

   228

   229 lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"

   230   and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"

   231   and unit_factor_Lcm_eucl [simp]:

   232           "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"

   233 proof -

   234   have "(\<forall>a\<in>A. a dvd Lcm_eucl A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm_eucl A dvd l') \<and>

   235     unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)

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

   237     case False

   238     hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)

   239     with False show ?thesis by auto

   240   next

   241     case True

   242     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

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

   244     define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"

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

   246       apply (subst n_def)

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

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

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

   250       done

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

   252       unfolding l_def by simp_all

   253     {

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

   255       with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd_eucl l l'" by (auto intro: gcd_eucl_greatest)

   256       moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp

   257       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and>

   258                           euclidean_size b = euclidean_size (gcd_eucl l l')"

   259         by (intro exI[of _ "gcd_eucl l l'"], auto)

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

   261       moreover have "euclidean_size (gcd_eucl l l') \<le> n"

   262       proof -

   263         have "gcd_eucl l l' dvd l" by simp

   264         then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast

   265         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto

   266         hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"

   267           by (rule size_mult_mono)

   268         also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..

   269         also note \<open>euclidean_size l = n\<close>

   270         finally show "euclidean_size (gcd_eucl l l') \<le> n" .

   271       qed

   272       ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')"

   273         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)

   274       from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"

   275         by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)

   276       hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])

   277     }

   278

   279     with \<open>(\<forall>a\<in>A. a dvd l)\<close> and unit_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>

   280       have "(\<forall>a\<in>A. a dvd normalize l) \<and>

   281         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>

   282         unit_factor (normalize l) =

   283         (if normalize l = 0 then 0 else 1)"

   284       by (auto simp: unit_simps)

   285     also from True have "normalize l = Lcm_eucl A"

   286       by (simp add: Lcm_eucl_def Let_def n_def l_def)

   287     finally show ?thesis .

   288   qed

   289   note A = this

   290

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

   292   {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}

   293   from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast

   294 qed

   295

   296 lemma normalize_Lcm_eucl [simp]:

   297   "normalize (Lcm_eucl A) = Lcm_eucl A"

   298 proof (cases "Lcm_eucl A = 0")

   299   case True then show ?thesis by simp

   300 next

   301   case False

   302   have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"

   303     by (fact unit_factor_mult_normalize)

   304   with False show ?thesis by simp

   305 qed

   306

   307 lemma eq_Lcm_euclI:

   308   fixes lcm :: "'a set \<Rightarrow> 'a"

   309   assumes "\<And>A a. a \<in> A \<Longrightarrow> a dvd lcm A" and "\<And>A c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> lcm A dvd c"

   310           "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"

   311   by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)

   312

   313 lemma Gcd_eucl_dvd: "x \<in> A \<Longrightarrow> Gcd_eucl A dvd x"

   314   unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)

   315

   316 lemma Gcd_eucl_greatest: "(\<And>x. x \<in> A \<Longrightarrow> d dvd x) \<Longrightarrow> d dvd Gcd_eucl A"

   317   unfolding Gcd_eucl_def by auto

   318

   319 lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"

   320   by (simp add: Gcd_eucl_def)

   321

   322 lemma Lcm_euclI:

   323   assumes "\<And>x. x \<in> A \<Longrightarrow> x dvd d" "\<And>d'. (\<And>x. x \<in> A \<Longrightarrow> x dvd d') \<Longrightarrow> d dvd d'" "normalize d = d"

   324   shows   "Lcm_eucl A = d"

   325 proof -

   326   have "normalize (Lcm_eucl A) = normalize d"

   327     by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)

   328   thus ?thesis by (simp add: assms)

   329 qed

   330

   331 lemma Gcd_euclI:

   332   assumes "\<And>x. x \<in> A \<Longrightarrow> d dvd x" "\<And>d'. (\<And>x. x \<in> A \<Longrightarrow> d' dvd x) \<Longrightarrow> d' dvd d" "normalize d = d"

   333   shows   "Gcd_eucl A = d"

   334 proof -

   335   have "normalize (Gcd_eucl A) = normalize d"

   336     by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)

   337   thus ?thesis by (simp add: assms)

   338 qed

   339

   340 lemmas lcm_gcd_eucl_facts =

   341   gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def

   342   Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl

   343   dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl

   344

   345 lemma normalized_factors_product:

   346   "{p. p dvd a * b \<and> normalize p = p} =

   347      (\<lambda>(x,y). x * y)  ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"

   348 proof safe

   349   fix p assume p: "p dvd a * b" "normalize p = p"

   350   interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor

   351     by standard (rule lcm_gcd_eucl_facts; assumption)+

   352   from dvd_productE[OF p(1)] guess x y . note xy = this

   353   define x' y' where "x' = normalize x" and "y' = normalize y"

   354   have "p = x' * y'"

   355     by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)

   356   moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b"

   357     by (simp_all add: x'_def y'_def)

   358   ultimately show "p \<in> (\<lambda>(x, y). x * y) 

   359                      ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"

   360     by blast

   361 qed (auto simp: normalize_mult mult_dvd_mono)

   362

   363

   364 subclass factorial_semiring

   365 proof (standard, rule factorial_semiring_altI_aux)

   366   fix x assume "x \<noteq> 0"

   367   thus "finite {p. p dvd x \<and> normalize p = p}"

   368   proof (induction "euclidean_size x" arbitrary: x rule: less_induct)

   369     case (less x)

   370     show ?case

   371     proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")

   372       case False

   373       have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"

   374       proof

   375         fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"

   376         with False have "is_unit p \<or> x dvd p" by blast

   377         thus "p \<in> {1, normalize x}"

   378         proof (elim disjE)

   379           assume "is_unit p"

   380           hence "normalize p = 1" by (simp add: is_unit_normalize)

   381           with p show ?thesis by simp

   382         next

   383           assume "x dvd p"

   384           with p have "normalize p = normalize x" by (intro associatedI) simp_all

   385           with p show ?thesis by simp

   386         qed

   387       qed

   388       moreover have "finite \<dots>" by simp

   389       ultimately show ?thesis by (rule finite_subset)

   390

   391     next

   392       case True

   393       then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast

   394       define z where "z = x div y"

   395       let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"

   396       from y have x: "x = y * z" by (simp add: z_def)

   397       with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto

   398       from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)

   399       have "?fctrs x = (\<lambda>(p,p'). p * p')  (?fctrs y \<times> ?fctrs z)"

   400         by (subst x) (rule normalized_factors_product)

   401       also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"

   402         by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+

   403       hence "finite ((\<lambda>(p,p'). p * p')  (?fctrs y \<times> ?fctrs z))"

   404         by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)

   405            (auto simp: x)

   406       finally show ?thesis .

   407     qed

   408   qed

   409 next

   410   interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor

   411     by standard (rule lcm_gcd_eucl_facts; assumption)+

   412   fix p assume p: "irreducible p"

   413   thus "prime_elem p" by (rule irreducible_imp_prime_elem_gcd)

   414 qed

   415

   416 lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"

   417   by (intro ext gcd_euclI gcd_lcm_factorial)

   418

   419 lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"

   420   by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)

   421

   422 lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"

   423   by (intro ext Gcd_euclI gcd_lcm_factorial)

   424

   425 lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"

   426   by (intro ext Lcm_euclI gcd_lcm_factorial)

   427

   428 lemmas eucl_eq_factorial =

   429   gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial

   430   Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial

   431

   432 end

   433

   434 class euclidean_ring = euclidean_semiring + idom

   435 begin

   436

   437 subclass ring_div ..

   438

   439 function euclid_ext_aux :: "'a \<Rightarrow> _" where

   440   "euclid_ext_aux r' r s' s t' t = (

   441      if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')

   442      else let q = r' div r

   443           in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"

   444 by auto

   445 termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)

   446

   447 declare euclid_ext_aux.simps [simp del]

   448

   449 lemma euclid_ext_aux_correct:

   450   assumes "gcd_eucl r' r = gcd_eucl x y"

   451   assumes "s' * x + t' * y = r'"

   452   assumes "s * x + t * y = r"

   453   shows   "case euclid_ext_aux r' r s' s t' t of (a,b,c) \<Rightarrow>

   454              a * x + b * y = c \<and> c = gcd_eucl x y" (is "?P (euclid_ext_aux r' r s' s t' t)")

   455 using assms

   456 proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)

   457   case (1 r' r s' s t' t)

   458   show ?case

   459   proof (cases "r = 0")

   460     case True

   461     hence "euclid_ext_aux r' r s' s t' t =

   462              (s' div unit_factor r', t' div unit_factor r', normalize r')"

   463       by (subst euclid_ext_aux.simps) (simp add: Let_def)

   464     also have "?P \<dots>"

   465     proof safe

   466       have "s' div unit_factor r' * x + t' div unit_factor r' * y =

   467                 (s' * x + t' * y) div unit_factor r'"

   468         by (cases "r' = 0") (simp_all add: unit_div_commute)

   469       also have "s' * x + t' * y = r'" by fact

   470       also have "\<dots> div unit_factor r' = normalize r'" by simp

   471       finally show "s' div unit_factor r' * x + t' div unit_factor r' * y = normalize r'" .

   472     next

   473       from "1.prems" True show "normalize r' = gcd_eucl x y" by (simp add: gcd_eucl_0)

   474     qed

   475     finally show ?thesis .

   476   next

   477     case False

   478     hence "euclid_ext_aux r' r s' s t' t =

   479              euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"

   480       by (subst euclid_ext_aux.simps) (simp add: Let_def)

   481     also from "1.prems" False have "?P \<dots>"

   482     proof (intro "1.IH")

   483       have "(s' - r' div r * s) * x + (t' - r' div r * t) * y =

   484               (s' * x + t' * y) - r' div r * (s * x + t * y)" by (simp add: algebra_simps)

   485       also have "s' * x + t' * y = r'" by fact

   486       also have "s * x + t * y = r" by fact

   487       also have "r' - r' div r * r = r' mod r" using mod_div_equality[of r' r]

   488         by (simp add: algebra_simps)

   489       finally show "(s' - r' div r * s) * x + (t' - r' div r * t) * y = r' mod r" .

   490     qed (auto simp: gcd_eucl_non_0 algebra_simps div_mod_equality')

   491     finally show ?thesis .

   492   qed

   493 qed

   494

   495 definition euclid_ext where

   496   "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"

   497

   498 lemma euclid_ext_0:

   499   "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"

   500   by (simp add: euclid_ext_def euclid_ext_aux.simps)

   501

   502 lemma euclid_ext_left_0:

   503   "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"

   504   by (simp add: euclid_ext_def euclid_ext_aux.simps)

   505

   506 lemma euclid_ext_correct':

   507   "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd_eucl x y"

   508   unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all

   509

   510 lemma euclid_ext_gcd_eucl:

   511   "(case euclid_ext x y of (a,b,c) \<Rightarrow> c) = gcd_eucl x y"

   512   using euclid_ext_correct'[of x y] by (simp add: case_prod_unfold)

   513

   514 definition euclid_ext' where

   515   "euclid_ext' x y = (case euclid_ext x y of (a, b, _) \<Rightarrow> (a, b))"

   516

   517 lemma euclid_ext'_correct':

   518   "case euclid_ext' x y of (a,b) \<Rightarrow> a * x + b * y = gcd_eucl x y"

   519   using euclid_ext_correct'[of x y] by (simp add: case_prod_unfold euclid_ext'_def)

   520

   521 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)"

   522   by (simp add: euclid_ext'_def euclid_ext_0)

   523

   524 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)"

   525   by (simp add: euclid_ext'_def euclid_ext_left_0)

   526

   527 end

   528

   529 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   532 begin

   533

   534 subclass semiring_gcd

   535   by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)

   536

   537 subclass semiring_Gcd

   538   by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)

   539

   540 subclass factorial_semiring_gcd

   541 proof

   542   fix a b

   543   show "gcd a b = gcd_factorial a b"

   544     by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+

   545   thus "lcm a b = lcm_factorial a b"

   546     by (simp add: lcm_factorial_gcd_factorial lcm_gcd)

   547 next

   548   fix A

   549   show "Gcd A = Gcd_factorial A"

   550     by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+

   551   show "Lcm A = Lcm_factorial A"

   552     by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+

   553 qed

   554

   555 lemma gcd_non_0:

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

   557   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)

   558

   559 lemmas gcd_0 = gcd_0_right

   560 lemmas dvd_gcd_iff = gcd_greatest_iff

   561 lemmas gcd_greatest_iff = dvd_gcd_iff

   562

   563 lemma gcd_mod1 [simp]:

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

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

   566

   567 lemma gcd_mod2 [simp]:

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

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

   570

   571 lemma euclidean_size_gcd_le1 [simp]:

   572   assumes "a \<noteq> 0"

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

   574 proof -

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

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

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

   578 qed

   579

   580 lemma euclidean_size_gcd_le2 [simp]:

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

   582   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   583

   584 lemma euclidean_size_gcd_less1:

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

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

   587 proof (rule ccontr)

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

   589   with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"

   590     by (intro le_antisym, simp_all)

   591   have "a dvd gcd a b"

   592     by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)

   593   hence "a dvd b" using dvd_gcdD2 by blast

   594   with \<open>\<not>a dvd b\<close> show False by contradiction

   595 qed

   596

   597 lemma euclidean_size_gcd_less2:

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

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

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

   601

   602 lemma euclidean_size_lcm_le1:

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

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

   605 proof -

   606   have "a dvd lcm a b" by (rule dvd_lcm1)

   607   then obtain c where A: "lcm a b = a * c" ..

   608   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_eq_0_iff)

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

   610 qed

   611

   612 lemma euclidean_size_lcm_le2:

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

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

   615

   616 lemma euclidean_size_lcm_less1:

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

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

   619 proof (rule ccontr)

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

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

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

   623     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

   624   with assms have "lcm a b dvd a"

   625     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)

   626   hence "b dvd a" by (rule lcm_dvdD2)

   627   with \<open>\<not>b dvd a\<close> show False by contradiction

   628 qed

   629

   630 lemma euclidean_size_lcm_less2:

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

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

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

   634

   635 lemma Lcm_eucl_set [code]:

   636   "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"

   637   by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)

   638

   639 lemma Gcd_eucl_set [code]:

   640   "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"

   641   by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)

   642

   643 end

   644

   645

   646 text \<open>

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

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

   649 \<close>

   650

   651 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

   652 begin

   653

   654 subclass euclidean_ring ..

   655 subclass ring_gcd ..

   656 subclass factorial_ring_gcd ..

   657

   658 lemma euclid_ext_gcd [simp]:

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

   660   using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)

   661

   662 lemma euclid_ext_gcd' [simp]:

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

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

   665

   666 lemma euclid_ext_correct:

   667   "case euclid_ext x y of (a,b,c) \<Rightarrow> a * x + b * y = c \<and> c = gcd x y"

   668   using euclid_ext_correct'[of x y]

   669   by (simp add: gcd_gcd_eucl case_prod_unfold)

   670

   671 lemma euclid_ext'_correct:

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

   673   using euclid_ext_correct'[of a b]

   674   by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)

   675

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

   677   using euclid_ext'_correct by blast

   678

   679 end

   680

   681

   682 subsection \<open>Typical instances\<close>

   683

   684 instantiation nat :: euclidean_semiring

   685 begin

   686

   687 definition [simp]:

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

   689

   690 instance by standard simp_all

   691

   692 end

   693

   694

   695 instantiation int :: euclidean_ring

   696 begin

   697

   698 definition [simp]:

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

   700

   701 instance by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)

   702

   703 end

   704

   705 instance nat :: euclidean_semiring_gcd

   706 proof

   707   show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"

   708     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)

   709   show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"

   710     by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+

   711 qed

   712

   713 instance int :: euclidean_ring_gcd

   714 proof

   715   show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"

   716     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)

   717   show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"

   718     by (intro ext, simp add: lcm_eucl_def lcm_altdef_int

   719           semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+

   720 qed

   721

   722 end