src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Thu Jul 02 10:06:47 2015 +0200 (2015-07-02) changeset 60634 e3b6e516608b parent 60600 87fbfea0bd0a child 60685 cb21b7022b00 permissions -rw-r--r--
separate (semi)ring with normalization
     1 (* Author: Manuel Eberl *)

     2

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

     4

     5 theory Euclidean_Algorithm

     6 imports Complex_Main "~~/src/HOL/Library/Polynomial" "~~/src/HOL/Number_Theory/Normalization_Semidom"

     7 begin

     8

     9 lemma is_unit_polyE:

    10   assumes "is_unit p"

    11   obtains a where "p = monom a 0" and "a \<noteq> 0"

    12 proof -

    13   obtain a q where "p = pCons a q" by (cases p)

    14   with assms have "p = [:a:]" and "a \<noteq> 0"

    15     by (simp_all add: is_unit_pCons_iff)

    16   with that show thesis by (simp add: monom_0)

    17 qed

    18

    19 instantiation poly :: (field) normalization_semidom

    20 begin

    21

    22 definition normalize_poly :: "'a poly \<Rightarrow> 'a poly"

    23   where "normalize_poly p = smult (1 / coeff p (degree p)) p"

    24

    25 definition unit_factor_poly :: "'a poly \<Rightarrow> 'a poly"

    26   where "unit_factor_poly p = monom (coeff p (degree p)) 0"

    27

    28 instance

    29 proof

    30   fix p :: "'a poly"

    31   show "unit_factor p * normalize p = p"

    32     by (simp add: normalize_poly_def unit_factor_poly_def)

    33       (simp only: mult_smult_left [symmetric] smult_monom, simp)

    34 next

    35   show "normalize 0 = (0::'a poly)"

    36     by (simp add: normalize_poly_def)

    37 next

    38   show "unit_factor 0 = (0::'a poly)"

    39     by (simp add: unit_factor_poly_def)

    40 next

    41   fix p :: "'a poly"

    42   assume "is_unit p"

    43   then obtain a where "p = monom a 0" and "a \<noteq> 0"

    44     by (rule is_unit_polyE)

    45   then show "normalize p = 1"

    46     by (auto simp add: normalize_poly_def smult_monom degree_monom_eq)

    47 next

    48   fix p q :: "'a poly"

    49   assume "q \<noteq> 0"

    50   from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"

    51     by (auto intro: is_unit_monom_0)

    52   then show "is_unit (unit_factor q)"

    53     by (simp add: unit_factor_poly_def)

    54 next

    55   fix p q :: "'a poly"

    56   have "monom (coeff (p * q) (degree (p * q))) 0 =

    57     monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"

    58     by (simp add: monom_0 coeff_degree_mult)

    59   then show "unit_factor (p * q) =

    60     unit_factor p * unit_factor q"

    61     by (simp add: unit_factor_poly_def)

    62 qed

    63

    64 end

    65

    66 text \<open>

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

    68   implemented. It must provide:

    69   \begin{itemize}

    70   \item division with remainder

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

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

    73   \end{itemize}

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

    75   for any Euclidean semiring.

    76 \<close>

    77 class euclidean_semiring = semiring_div + normalization_semidom +

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

    79   assumes mod_size_less:

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

    81   assumes size_mult_mono:

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

    83 begin

    84

    85 lemma euclidean_division:

    86   fixes a :: 'a and b :: 'a

    87   assumes "b \<noteq> 0"

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

    89     and "euclidean_size t < euclidean_size b"

    90 proof -

    91   from div_mod_equality [of a b 0]

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

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

    94 qed

    95

    96 lemma dvd_euclidean_size_eq_imp_dvd:

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

    98   shows "a dvd b"

    99 proof (rule ccontr)

   100   assume "\<not> a dvd b"

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

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

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

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

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

   106       using size_mult_mono by force

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

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

   109       using mod_size_less by blast

   110   ultimately show False using size_eq by simp

   111 qed

   112

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

   114 where

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

   116   by pat_completeness simp

   117 termination

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

   119

   120 declare gcd_eucl.simps [simp del]

   121

   122 lemma gcd_eucl_induct [case_names zero mod]:

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

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

   125   shows "P a b"

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

   127   case ("1" a b)

   128   show ?case

   129   proof (cases "b = 0")

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

   131   next

   132     case False

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

   134       by (rule "1.hyps")

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

   136       by (blast intro: H2)

   137   qed

   138 qed

   139

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

   141 where

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

   143

   144 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>

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

   146   for infinite sets as well\<close>

   147 where

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

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

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

   151        in normalize l

   152       else 0)"

   153

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

   155 where

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

   157

   158 lemma gcd_eucl_0:

   159   "gcd_eucl a 0 = normalize a"

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

   161

   162 lemma gcd_eucl_0_left:

   163   "gcd_eucl 0 a = normalize a"

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

   165

   166 lemma gcd_eucl_non_0:

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

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

   169

   170 end

   171

   172 class euclidean_ring = euclidean_semiring + idom

   173 begin

   174

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

   176   "euclid_ext a b =

   177      (if b = 0 then

   178         (1 div unit_factor a, 0, normalize a)

   179       else

   180         case euclid_ext b (a mod b) of

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

   182   by pat_completeness simp

   183 termination

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

   185

   186 declare euclid_ext.simps [simp del]

   187

   188 lemma euclid_ext_0:

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

   190   by (simp add: euclid_ext.simps [of a 0])

   191

   192 lemma euclid_ext_left_0:

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

   194   by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])

   195

   196 lemma euclid_ext_non_0:

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

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

   199   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])

   200

   201 lemma euclid_ext_code [code]:

   202   "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)

   203     else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"

   204   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])

   205

   206 lemma euclid_ext_correct:

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

   208 proof (induct a b rule: gcd_eucl_induct)

   209   case (zero a) then show ?case

   210     by (simp add: euclid_ext_0 ac_simps)

   211 next

   212   case (mod a b)

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

   214     by (cases "euclid_ext b (a mod b)") blast

   215   with mod have "c = s * b + t * (a mod b)" by simp

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

   217     by (simp add: algebra_simps)

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

   219   finally show ?case

   220     by (subst euclid_ext.simps) (simp add: stc mod ac_simps)

   221 qed

   222

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

   224 where

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

   226

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

   228   by (simp add: euclid_ext'_def euclid_ext_0)

   229

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

   231   by (simp add: euclid_ext'_def euclid_ext_left_0)

   232

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

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

   235   by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)

   236

   237 end

   238

   239 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   242 begin

   243

   244 lemma gcd_0_left:

   245   "gcd 0 a = normalize a"

   246   unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)

   247

   248 lemma gcd_0:

   249   "gcd a 0 = normalize a"

   250   unfolding gcd_gcd_eucl by (fact gcd_eucl_0)

   251

   252 lemma gcd_non_0:

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

   254   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)

   255

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

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

   258   by (induct a b rule: gcd_eucl_induct)

   259     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)

   260

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

   262   by (rule dvd_trans, assumption, rule gcd_dvd1)

   263

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

   265   by (rule dvd_trans, assumption, rule gcd_dvd2)

   266

   267 lemma gcd_greatest:

   268   fixes k a b :: 'a

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

   270 proof (induct a b rule: gcd_eucl_induct)

   271   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)

   272 next

   273   case (mod a b)

   274   then show ?case

   275     by (simp add: gcd_non_0 dvd_mod_iff)

   276 qed

   277

   278 lemma dvd_gcd_iff:

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

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

   281

   282 lemmas gcd_greatest_iff = dvd_gcd_iff

   283

   284 lemma gcd_zero [simp]:

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

   286   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   287

   288 lemma unit_factor_gcd [simp]:

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

   290   by (induct a b rule: gcd_eucl_induct)

   291     (auto simp add: gcd_0 gcd_non_0)

   292

   293 lemma gcdI:

   294   assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"

   295     and "unit_factor c = (if c = 0 then 0 else 1)"

   296   shows "c = gcd a b"

   297   by (rule associated_eqI) (auto simp: assms associated_def intro: gcd_greatest)

   298

   299 sublocale gcd!: abel_semigroup gcd

   300 proof

   301   fix a b c

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

   303   proof (rule gcdI)

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

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

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

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

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

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

   310       by (rule gcd_greatest)

   311     show "unit_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"

   312       by auto

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

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

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

   316     with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"

   317       by (intro gcd_greatest)

   318   qed

   319 next

   320   fix a b

   321   show "gcd a b = gcd b a"

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

   323 qed

   324

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

   326     unit_factor d = (if d = 0 then 0 else 1) \<and>

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

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

   329

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

   331   using mult_dvd_mono [of 1] by auto

   332

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

   334   by (rule sym, rule gcdI, simp_all)

   335

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

   337   by (rule sym, rule gcdI, simp_all)

   338

   339 lemma gcd_proj2_if_dvd:

   340   "b dvd a \<Longrightarrow> gcd a b = normalize b"

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

   342

   343 lemma gcd_proj1_if_dvd:

   344   "a dvd b \<Longrightarrow> gcd a b = normalize a"

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

   346

   347 lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"

   348 proof

   349   assume A: "gcd m n = normalize m"

   350   show "m dvd n"

   351   proof (cases "m = 0")

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

   353     from A have B: "m = gcd m n * unit_factor m"

   354       by (simp add: unit_eq_div2)

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

   356   qed (insert A, simp)

   357 next

   358   assume "m dvd n"

   359   then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd)

   360 qed

   361

   362 lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"

   363   using gcd_proj1_iff [of n m] by (simp add: ac_simps)

   364

   365 lemma gcd_mod1 [simp]:

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

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

   368

   369 lemma gcd_mod2 [simp]:

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

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

   372

   373 lemma gcd_mult_distrib':

   374   "normalize c * gcd a b = gcd (c * a) (c * b)"

   375 proof (cases "c = 0")

   376   case True then show ?thesis by (simp_all add: gcd_0)

   377 next

   378   case False then have [simp]: "is_unit (unit_factor c)" by simp

   379   show ?thesis

   380   proof (induct a b rule: gcd_eucl_induct)

   381     case (zero a) show ?case

   382     proof (cases "a = 0")

   383       case True then show ?thesis by (simp add: gcd_0)

   384     next

   385       case False

   386       then show ?thesis by (simp add: gcd_0 normalize_mult)

   387     qed

   388     case (mod a b)

   389     then show ?case by (simp add: mult_mod_right gcd.commute)

   390   qed

   391 qed

   392

   393 lemma gcd_mult_distrib:

   394   "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"

   395 proof-

   396   have "normalize k * gcd a b = gcd (k * a) (k * b)"

   397     by (simp add: gcd_mult_distrib')

   398   then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"

   399     by simp

   400   then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"

   401     by (simp only: ac_simps)

   402   then show ?thesis

   403     by simp

   404 qed

   405

   406 lemma euclidean_size_gcd_le1 [simp]:

   407   assumes "a \<noteq> 0"

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

   409 proof -

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

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

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

   413 qed

   414

   415 lemma euclidean_size_gcd_le2 [simp]:

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

   417   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   418

   419 lemma euclidean_size_gcd_less1:

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

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

   422 proof (rule ccontr)

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

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

   425     by (intro le_antisym, simp_all)

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

   427   hence "a dvd b" using dvd_gcd_D2 by blast

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

   429 qed

   430

   431 lemma euclidean_size_gcd_less2:

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

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

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

   435

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

   437   apply (rule gcdI)

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

   439   apply (rule gcd_dvd2)

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

   441   apply (subst unit_factor_gcd, simp add: gcd_0)

   442   done

   443

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

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

   446

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

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

   449

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

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

   452

   453 lemma normalize_gcd_left [simp]:

   454   "gcd (normalize a) b = gcd a b"

   455 proof (cases "a = 0")

   456   case True then show ?thesis

   457     by simp

   458 next

   459   case False then have "is_unit (unit_factor a)"

   460     by simp

   461   moreover have "normalize a = a div unit_factor a"

   462     by simp

   463   ultimately show ?thesis

   464     by (simp only: gcd_div_unit1)

   465 qed

   466

   467 lemma normalize_gcd_right [simp]:

   468   "gcd a (normalize b) = gcd a b"

   469   using normalize_gcd_left [of b a] by (simp add: ac_simps)

   470

   471 lemma gcd_idem: "gcd a a = normalize a"

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

   473

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

   475   apply (rule gcdI)

   476   apply (simp add: ac_simps)

   477   apply (rule gcd_dvd2)

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

   479   apply simp

   480   done

   481

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

   483   apply (rule gcdI)

   484   apply simp

   485   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

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

   487   apply simp

   488   done

   489

   490 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   491 proof

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

   493     by (simp add: fun_eq_iff ac_simps)

   494 next

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

   496     by (simp add: fun_eq_iff gcd_left_idem)

   497 qed

   498

   499 lemma coprime_dvd_mult:

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

   501   shows "c dvd a"

   502 proof -

   503   let ?nf = "unit_factor"

   504   from assms gcd_mult_distrib [of a c b]

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

   506   from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)

   507 qed

   508

   509 lemma coprime_dvd_mult_iff:

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

   511   by (rule, rule coprime_dvd_mult, simp_all)

   512

   513 lemma gcd_dvd_antisym:

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

   515 proof (rule gcdI)

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

   517   have "gcd c d dvd c" by simp

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

   519   have "gcd c d dvd d" by simp

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

   521   show "unit_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"

   522     by simp

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

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

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

   526 qed

   527

   528 lemma gcd_mult_cancel:

   529   assumes "gcd k n = 1"

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

   531 proof (rule gcd_dvd_antisym)

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

   533   also note \<open>gcd k n = 1\<close>

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

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

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

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

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

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

   540 qed

   541

   542 lemma coprime_crossproduct:

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

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

   545 proof

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

   547 next

   548   assume ?lhs

   549   from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)

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

   551   moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)

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

   553   moreover from \<open>?lhs\<close> have "c dvd d * b"

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

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

   556   moreover from \<open>?lhs\<close> have "d dvd c * a"

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

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

   559   ultimately show ?rhs unfolding associated_def by simp

   560 qed

   561

   562 lemma gcd_add1 [simp]:

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

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

   565

   566 lemma gcd_add2 [simp]:

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

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

   569

   570 lemma gcd_add_mult:

   571   "gcd m (k * m + n) = gcd m n"

   572 proof -

   573   have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"

   574     by (fact gcd_mod2)

   575   then show ?thesis by simp

   576 qed

   577

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

   579   by (rule sym, rule gcdI, simp_all)

   580

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

   582   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   583

   584 lemma div_gcd_coprime:

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

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

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

   588   shows "gcd a' b' = 1"

   589 proof (rule coprimeI)

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

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

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

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

   594     by (simp_all only: ac_simps)

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

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

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

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

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

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

   601   ultimately have "1 = l * u"

   602     using \<open>d \<noteq> 0\<close> by simp

   603   then show "l dvd 1" ..

   604 qed

   605

   606 lemma coprime_mult:

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

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

   609   apply (subst gcd.commute)

   610   using da apply (subst gcd_mult_cancel)

   611   apply (subst gcd.commute, assumption)

   612   apply (subst gcd.commute, rule db)

   613   done

   614

   615 lemma coprime_lmult:

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

   617   shows "gcd d a = 1"

   618 proof (rule coprimeI)

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

   620   hence "l dvd a * b" by simp

   621   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)

   622 qed

   623

   624 lemma coprime_rmult:

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

   626   shows "gcd d b = 1"

   627 proof (rule coprimeI)

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

   629   hence "l dvd a * b" by simp

   630   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)

   631 qed

   632

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

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

   635

   636 lemma gcd_coprime:

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

   638   shows "gcd a' b' = 1"

   639 proof -

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

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

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

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

   644   finally show ?thesis .

   645 qed

   646

   647 lemma coprime_power:

   648   assumes "0 < n"

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

   650 using assms proof (induct n)

   651   case (Suc n) then show ?case

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

   653 qed simp

   654

   655 lemma gcd_coprime_exists:

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

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

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

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

   660   apply (insert nz, auto intro: div_gcd_coprime)

   661   done

   662

   663 lemma coprime_exp:

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

   665   by (induct n, simp_all add: coprime_mult)

   666

   667 lemma coprime_exp2 [intro]:

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

   669   apply (rule coprime_exp)

   670   apply (subst gcd.commute)

   671   apply (rule coprime_exp)

   672   apply (subst gcd.commute)

   673   apply assumption

   674   done

   675

   676 lemma gcd_exp:

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

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

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

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

   681 next

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

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

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

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

   686   also note gcd_mult_distrib

   687   also have "unit_factor ((gcd a b)^n) = 1"

   688     by (simp add: unit_factor_power A)

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

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

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

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

   693   finally show ?thesis by simp

   694 qed

   695

   696 lemma coprime_common_divisor:

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

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

   699   apply simp

   700   apply (erule (1) gcd_greatest)

   701   done

   702

   703 lemma division_decomp:

   704   assumes dc: "a dvd b * c"

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

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

   707   assume "gcd a b = 0"

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

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

   710   then show ?thesis by blast

   711 next

   712   let ?d = "gcd a b"

   713   assume "?d \<noteq> 0"

   714   from gcd_coprime_exists[OF this]

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

   716     by blast

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

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

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

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

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

   722   with coprime_dvd_mult[OF ab'(3)]

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

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

   725   then show ?thesis by blast

   726 qed

   727

   728 lemma pow_divs_pow:

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

   730   shows "a dvd b"

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

   732   assume "gcd a b = 0"

   733   then show ?thesis by simp

   734 next

   735   let ?d = "gcd a b"

   736   assume "?d \<noteq> 0"

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

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

   739   from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]

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

   741     by blast

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

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

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

   745     by (simp only: power_mult_distrib ac_simps)

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

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

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

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

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

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

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

   753 qed

   754

   755 lemma pow_divs_eq [simp]:

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

   757   by (auto intro: pow_divs_pow dvd_power_same)

   758

   759 lemma divs_mult:

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

   761   shows "m * n dvd r"

   762 proof -

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

   764     unfolding dvd_def by blast

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

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

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

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

   769   then show ?thesis unfolding dvd_def by blast

   770 qed

   771

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

   773   by (subst add_commute, simp)

   774

   775 lemma setprod_coprime [rule_format]:

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

   777   apply (cases "finite A")

   778   apply (induct set: finite)

   779   apply (auto simp add: gcd_mult_cancel)

   780   done

   781

   782 lemma coprime_divisors:

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

   784   shows "gcd d e = 1"

   785 proof -

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

   787     unfolding dvd_def by blast

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

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

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

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

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

   793 qed

   794

   795 lemma invertible_coprime:

   796   assumes "a * b mod m = 1"

   797   shows "coprime a m"

   798 proof -

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

   800     by simp

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

   802     by simp

   803   then have "coprime m a"

   804     by (rule coprime_lmult)

   805   then show ?thesis

   806     by (simp add: ac_simps)

   807 qed

   808

   809 lemma lcm_gcd:

   810   "lcm a b = normalize (a * b) div gcd a b"

   811   by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

   812

   813 lemma lcm_gcd_prod:

   814   "lcm a b * gcd a b = normalize (a * b)"

   815   by (simp add: lcm_gcd)

   816

   817 lemma lcm_dvd1 [iff]:

   818   "a dvd lcm a b"

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

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

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

   822   let ?c = "1 div unit_factor (a * b)"

   823   from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (unit_factor (a * b))" by simp

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

   825     by (simp add: div_mult_swap unit_div_commute)

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

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

   828     by (subst (asm) div_mult_self2_is_id, simp_all)

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

   830     by (metis div_mult_swap gcd_dvd2 mult_assoc)

   831   finally show ?thesis by (rule dvdI)

   832 qed (auto simp add: lcm_gcd)

   833

   834 lemma lcm_least:

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

   836 proof (cases "k = 0")

   837   let ?nf = unit_factor

   838   assume "k \<noteq> 0"

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

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

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

   842   hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto

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

   844     unfolding dvd_def by blast

   845   with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"

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

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

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

   849   from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)

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

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

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

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

   854     by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)

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

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

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

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

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

   860     by (simp add: algebra_simps)

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

   862     by (metis div_mult_self2_is_id)

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

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

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

   866     by (simp add: algebra_simps)

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

   868     by (metis mult.commute div_mult_self2_is_id)

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

   870     by (metis div_mult_self2_is_id mult_assoc)

   871   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>

   872     by (simp add: unit_simps)

   873   finally show ?thesis by (rule dvdI)

   874 qed simp

   875

   876 lemma lcm_zero:

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

   878 proof -

   879   let ?nf = unit_factor

   880   {

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

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

   883     moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp

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

   885   } moreover {

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

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

   888   }

   889   ultimately show ?thesis by blast

   890 qed

   891

   892 lemmas lcm_0_iff = lcm_zero

   893

   894 lemma gcd_lcm:

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

   896   shows "gcd a b = normalize (a * b) div lcm a b"

   897 proof -

   898   have "lcm a b * gcd a b = normalize (a * b)"

   899     by (fact lcm_gcd_prod)

   900   with assms show ?thesis

   901     by (metis nonzero_mult_divide_cancel_left)

   902 qed

   903

   904 lemma unit_factor_lcm [simp]:

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

   906   by (simp add: dvd_unit_factor_div lcm_gcd)

   907

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

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

   910

   911 lemma lcmI:

   912   assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"

   913     and "unit_factor c = (if c = 0 then 0 else 1)"

   914   shows "c = lcm a b"

   915   by (rule associated_eqI) (auto simp: assms associated_def intro: lcm_least)

   916

   917 sublocale lcm!: abel_semigroup lcm

   918 proof

   919   fix a b c

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

   921   proof (rule lcmI)

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

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

   924

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

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

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

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

   929

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

   931     have "b dvd lcm b c" by simp

   932     from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)

   933     have "c dvd lcm b c" by simp

   934     from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)

   935     from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)

   936     from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)

   937   qed (simp add: lcm_zero)

   938 next

   939   fix a b

   940   show "lcm a b = lcm b a"

   941     by (simp add: lcm_gcd ac_simps)

   942 qed

   943

   944 lemma dvd_lcm_D1:

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

   946   by (rule dvd_trans, rule lcm_dvd1, assumption)

   947

   948 lemma dvd_lcm_D2:

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

   950   by (rule dvd_trans, rule lcm_dvd2, assumption)

   951

   952 lemma gcd_dvd_lcm [simp]:

   953   "gcd a b dvd lcm a b"

   954   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

   955

   956 lemma lcm_1_iff:

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

   958 proof

   959   assume "lcm a b = 1"

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

   961 next

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

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

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

   965   hence "lcm a b = unit_factor (lcm a b)"

   966     by (blast intro: sym is_unit_unit_factor)

   967   also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>

   968     by auto

   969   finally show "lcm a b = 1" .

   970 qed

   971

   972 lemma lcm_0_left [simp]:

   973   "lcm 0 a = 0"

   974   by (rule sym, rule lcmI, simp_all)

   975

   976 lemma lcm_0 [simp]:

   977   "lcm a 0 = 0"

   978   by (rule sym, rule lcmI, simp_all)

   979

   980 lemma lcm_unique:

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

   982   unit_factor d = (if d = 0 then 0 else 1) \<and>

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

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

   985

   986 lemma dvd_lcm_I1 [simp]:

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

   988   by (metis lcm_dvd1 dvd_trans)

   989

   990 lemma dvd_lcm_I2 [simp]:

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

   992   by (metis lcm_dvd2 dvd_trans)

   993

   994 lemma lcm_1_left [simp]:

   995   "lcm 1 a = normalize a"

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

   997

   998 lemma lcm_1_right [simp]:

   999   "lcm a 1 = normalize a"

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

  1001

  1002 lemma lcm_coprime:

  1003   "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"

  1004   by (subst lcm_gcd) simp

  1005

  1006 lemma lcm_proj1_if_dvd:

  1007   "b dvd a \<Longrightarrow> lcm a b = normalize a"

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

  1009

  1010 lemma lcm_proj2_if_dvd:

  1011   "a dvd b \<Longrightarrow> lcm a b = normalize b"

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

  1013

  1014 lemma lcm_proj1_iff:

  1015   "lcm m n = normalize m \<longleftrightarrow> n dvd m"

  1016 proof

  1017   assume A: "lcm m n = normalize m"

  1018   show "n dvd m"

  1019   proof (cases "m = 0")

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

  1021     from A have B: "m = lcm m n * unit_factor m"

  1022       by (simp add: unit_eq_div2)

  1023     show ?thesis by (subst B, simp)

  1024   qed simp

  1025 next

  1026   assume "n dvd m"

  1027   then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd)

  1028 qed

  1029

  1030 lemma lcm_proj2_iff:

  1031   "lcm m n = normalize n \<longleftrightarrow> m dvd n"

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

  1033

  1034 lemma euclidean_size_lcm_le1:

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

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

  1037 proof -

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

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

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

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

  1042 qed

  1043

  1044 lemma euclidean_size_lcm_le2:

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

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

  1047

  1048 lemma euclidean_size_lcm_less1:

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

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

  1051 proof (rule ccontr)

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

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

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

  1055     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

  1056   with assms have "lcm a b dvd a"

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

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

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

  1060 qed

  1061

  1062 lemma euclidean_size_lcm_less2:

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

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

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

  1066

  1067 lemma lcm_mult_unit1:

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

  1069   apply (rule lcmI)

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

  1071   apply (rule lcm_dvd2)

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

  1073   apply (subst unit_factor_lcm, simp add: lcm_zero)

  1074   done

  1075

  1076 lemma lcm_mult_unit2:

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

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

  1079

  1080 lemma lcm_div_unit1:

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

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

  1083

  1084 lemma lcm_div_unit2:

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

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

  1087

  1088 lemma normalize_lcm_left [simp]:

  1089   "lcm (normalize a) b = lcm a b"

  1090 proof (cases "a = 0")

  1091   case True then show ?thesis

  1092     by simp

  1093 next

  1094   case False then have "is_unit (unit_factor a)"

  1095     by simp

  1096   moreover have "normalize a = a div unit_factor a"

  1097     by simp

  1098   ultimately show ?thesis

  1099     by (simp only: lcm_div_unit1)

  1100 qed

  1101

  1102 lemma normalize_lcm_right [simp]:

  1103   "lcm a (normalize b) = lcm a b"

  1104   using normalize_lcm_left [of b a] by (simp add: ac_simps)

  1105

  1106 lemma lcm_left_idem:

  1107   "lcm a (lcm a b) = lcm a b"

  1108   apply (rule lcmI)

  1109   apply simp

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

  1111   apply (rule lcm_least, assumption)

  1112   apply (erule (1) lcm_least)

  1113   apply (auto simp: lcm_zero)

  1114   done

  1115

  1116 lemma lcm_right_idem:

  1117   "lcm (lcm a b) b = lcm a b"

  1118   apply (rule lcmI)

  1119   apply (subst lcm.assoc, rule lcm_dvd1)

  1120   apply (rule lcm_dvd2)

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

  1122   apply (auto simp: lcm_zero)

  1123   done

  1124

  1125 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

  1126 proof

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

  1128     by (simp add: fun_eq_iff ac_simps)

  1129 next

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

  1131     by (intro ext, simp add: lcm_left_idem)

  1132 qed

  1133

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

  1135   and Lcm_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b"

  1136   and unit_factor_Lcm [simp]:

  1137           "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"

  1138 proof -

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

  1140     unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)

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

  1142     case False

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

  1144     with False show ?thesis by auto

  1145   next

  1146     case True

  1147     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

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

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

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

  1151       apply (subst n_def)

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

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

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

  1155       done

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

  1157       unfolding l_def by simp_all

  1158     {

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

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

  1161       moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp

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

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

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

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

  1166       proof -

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

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

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

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

  1171           by (rule size_mult_mono)

  1172         also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..

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

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

  1175       qed

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

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

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

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

  1180     }

  1181

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

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

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

  1185         unit_factor (normalize l) =

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

  1187       by (auto simp: unit_simps)

  1188     also from True have "normalize l = Lcm A"

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

  1190     finally show ?thesis .

  1191   qed

  1192   note A = this

  1193

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

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

  1196   from A show "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast

  1197 qed

  1198

  1199 lemma normalize_Lcm [simp]:

  1200   "normalize (Lcm A) = Lcm A"

  1201   by (cases "Lcm A = 0") (auto intro: associated_eqI)

  1202

  1203 lemma LcmI:

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

  1205     and "unit_factor b = (if b = 0 then 0 else 1)" shows "b = Lcm A"

  1206   by (rule associated_eqI) (auto simp: assms associated_def intro: Lcm_least)

  1207

  1208 lemma Lcm_subset:

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

  1210   by (blast intro: Lcm_least dvd_Lcm)

  1211

  1212 lemma Lcm_Un:

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

  1214   apply (rule lcmI)

  1215   apply (blast intro: Lcm_subset)

  1216   apply (blast intro: Lcm_subset)

  1217   apply (intro Lcm_least ballI, elim UnE)

  1218   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1219   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1220   apply simp

  1221   done

  1222

  1223 lemma Lcm_1_iff:

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

  1225 proof

  1226   assume "Lcm A = 1"

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

  1228 qed (rule LcmI [symmetric], auto)

  1229

  1230 lemma Lcm_no_units:

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

  1232 proof -

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

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

  1235     by (simp add: Lcm_Un [symmetric])

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

  1237   finally show ?thesis by simp

  1238 qed

  1239

  1240 lemma Lcm_empty [simp]:

  1241   "Lcm {} = 1"

  1242   by (simp add: Lcm_1_iff)

  1243

  1244 lemma Lcm_eq_0 [simp]:

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

  1246   by (drule dvd_Lcm) simp

  1247

  1248 lemma Lcm0_iff':

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

  1250 proof

  1251   assume "Lcm A = 0"

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

  1253   proof

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

  1255     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

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

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

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

  1259       apply (subst n_def)

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

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

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

  1263       done

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

  1265     hence "normalize l \<noteq> 0" by simp

  1266     also from ex have "normalize l = Lcm A"

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

  1268     finally show False using \<open>Lcm A = 0\<close> by contradiction

  1269   qed

  1270 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1271

  1272 lemma Lcm0_iff [simp]:

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

  1274 proof -

  1275   assume "finite A"

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

  1277   moreover {

  1278     assume "0 \<notin> A"

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

  1280       apply (induct rule: finite_induct[OF \<open>finite A\<close>])

  1281       apply simp

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

  1283       apply (rule no_zero_divisors)

  1284       apply blast+

  1285       done

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

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

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

  1289   }

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

  1291 qed

  1292

  1293 lemma Lcm_no_multiple:

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

  1295 proof -

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

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

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

  1299 qed

  1300

  1301 lemma Lcm_insert [simp]:

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

  1303 proof (rule lcmI)

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

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

  1306   with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_least)

  1307 qed (auto intro: Lcm_least dvd_Lcm)

  1308

  1309 lemma Lcm_finite:

  1310   assumes "finite A"

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

  1312   by (induct rule: finite.induct[OF \<open>finite A\<close>])

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

  1314

  1315 lemma Lcm_set [code_unfold]:

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

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

  1318

  1319 lemma Lcm_singleton [simp]:

  1320   "Lcm {a} = normalize a"

  1321   by simp

  1322

  1323 lemma Lcm_2 [simp]:

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

  1325   by simp

  1326

  1327 lemma Lcm_coprime:

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

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

  1330   shows "Lcm A = normalize (\<Prod>A)"

  1331 using assms proof (induct rule: finite_ne_induct)

  1332   case (insert a A)

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

  1334   also from insert have "Lcm A = normalize (\<Prod>A)" by blast

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

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

  1337   with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"

  1338     by (simp add: lcm_coprime)

  1339   finally show ?case .

  1340 qed simp

  1341

  1342 lemma Lcm_coprime':

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

  1344     \<Longrightarrow> Lcm A = normalize (\<Prod>A)"

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

  1346

  1347 lemma Gcd_Lcm:

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

  1349   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1350

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

  1352   and Gcd_greatest: "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A"

  1353   and unit_factor_Gcd [simp]:

  1354     "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1355 proof -

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

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

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

  1359 next

  1360   fix g' assume "\<And>a. a \<in> A \<Longrightarrow> g' dvd a"

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

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

  1363 next

  1364   show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"

  1365     by (simp add: Gcd_Lcm)

  1366 qed

  1367

  1368 lemma normalize_Gcd [simp]:

  1369   "normalize (Gcd A) = Gcd A"

  1370   by (cases "Gcd A = 0") (auto intro: associated_eqI)

  1371

  1372 lemma GcdI:

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

  1374     and "unit_factor b = (if b = 0 then 0 else 1)"

  1375   shows "b = Gcd A"

  1376   by (rule associated_eqI) (auto simp: assms associated_def intro: Gcd_greatest)

  1377

  1378 lemma Lcm_Gcd:

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

  1380   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest)

  1381

  1382 lemma Gcd_0_iff:

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

  1384   apply (rule iffI)

  1385   apply (rule subsetI, drule Gcd_dvd, simp)

  1386   apply (auto intro: GcdI[symmetric])

  1387   done

  1388

  1389 lemma Gcd_empty [simp]:

  1390   "Gcd {} = 0"

  1391   by (simp add: Gcd_0_iff)

  1392

  1393 lemma Gcd_1:

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

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

  1396

  1397 lemma Gcd_insert [simp]:

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

  1399 proof (rule gcdI)

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

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

  1402   with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd Gcd_greatest)

  1403 qed (auto intro: Gcd_greatest)

  1404

  1405 lemma Gcd_finite:

  1406   assumes "finite A"

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

  1408   by (induct rule: finite.induct[OF \<open>finite A\<close>])

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

  1410

  1411 lemma Gcd_set [code_unfold]:

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

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

  1414

  1415 lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"

  1416   by (simp add: gcd_0)

  1417

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

  1419   by (simp add: gcd_0)

  1420

  1421 subclass semiring_gcd

  1422   by unfold_locales (simp_all add: gcd_greatest_iff)

  1423

  1424 end

  1425

  1426 text \<open>

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

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

  1429 \<close>

  1430

  1431 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1432 begin

  1433

  1434 subclass euclidean_ring ..

  1435

  1436 subclass ring_gcd ..

  1437

  1438 lemma euclid_ext_gcd [simp]:

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

  1440   by (induct a b rule: gcd_eucl_induct)

  1441     (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)

  1442

  1443 lemma euclid_ext_gcd' [simp]:

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

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

  1446

  1447 lemma euclid_ext'_correct:

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

  1449 proof-

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

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

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

  1453     show ?thesis unfolding euclid_ext'_def by simp

  1454 qed

  1455

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

  1457   using euclid_ext'_correct by blast

  1458

  1459 lemma gcd_neg1 [simp]:

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

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

  1462

  1463 lemma gcd_neg2 [simp]:

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

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

  1466

  1467 lemma gcd_neg_numeral_1 [simp]:

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

  1469   by (fact gcd_neg1)

  1470

  1471 lemma gcd_neg_numeral_2 [simp]:

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

  1473   by (fact gcd_neg2)

  1474

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

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

  1477

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

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

  1480

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

  1482 proof -

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

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

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

  1486   finally show ?thesis .

  1487 qed

  1488

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

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

  1491

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

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

  1494

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

  1496   by (fact lcm_neg1)

  1497

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

  1499   by (fact lcm_neg2)

  1500

  1501 end

  1502

  1503

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

  1505

  1506 instantiation nat :: euclidean_semiring

  1507 begin

  1508

  1509 definition [simp]:

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

  1511

  1512 definition [simp]:

  1513   "unit_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"

  1514

  1515 instance proof

  1516 qed simp_all

  1517

  1518 end

  1519

  1520 instantiation int :: euclidean_ring

  1521 begin

  1522

  1523 definition [simp]:

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

  1525

  1526 definition [simp]:

  1527   "unit_factor_int = (sgn :: int \<Rightarrow> int)"

  1528

  1529 instance

  1530 by standard (auto simp add: abs_mult nat_mult_distrib sgn_times split: abs_split)

  1531

  1532 end

  1533

  1534 instantiation poly :: (field) euclidean_ring

  1535 begin

  1536

  1537 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"

  1538   where "euclidean_size p = (if p = 0 then 0 else Suc (degree p))"

  1539

  1540 lemma euclidenan_size_poly_minus_one_degree [simp]:

  1541   "euclidean_size p - 1 = degree p"

  1542   by (simp add: euclidean_size_poly_def)

  1543

  1544 lemma euclidean_size_poly_0 [simp]:

  1545   "euclidean_size (0::'a poly) = 0"

  1546   by (simp add: euclidean_size_poly_def)

  1547

  1548 lemma euclidean_size_poly_not_0 [simp]:

  1549   "p \<noteq> 0 \<Longrightarrow> euclidean_size p = Suc (degree p)"

  1550   by (simp add: euclidean_size_poly_def)

  1551

  1552 instance

  1553 proof

  1554   fix p q :: "'a poly"

  1555   assume "q \<noteq> 0"

  1556   then have "p mod q = 0 \<or> degree (p mod q) < degree q"

  1557     by (rule degree_mod_less [of q p])

  1558   with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"

  1559     by (cases "p mod q = 0") simp_all

  1560 next

  1561   fix p q :: "'a poly"

  1562   assume "q \<noteq> 0"

  1563   from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"

  1564     by (rule degree_mult_right_le)

  1565   with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"

  1566     by (cases "p = 0") simp_all

  1567 qed

  1568

  1569 end

  1570

  1571 end