src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Thu Jun 25 15:01:42 2015 +0200 (2015-06-25) changeset 60571 c9fdf2080447 parent 60569 f2f1f6860959 child 60572 718b1ba06429 permissions -rw-r--r--
euclidean algorithm on polynomials
     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"

     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   \item a normalization factor such that two associated numbers are equal iff

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

    18   \end{itemize}

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

    20   for any Euclidean semiring.

    21 \<close>

    22 class euclidean_semiring = semiring_div +

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

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

    25   assumes mod_size_less:

    26     "b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"

    27   assumes size_mult_mono:

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

    29   assumes normalization_factor_is_unit [intro,simp]:

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

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

    32     normalization_factor a * normalization_factor b"

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

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

    35 begin

    36

    37 lemma normalization_factor_dvd [simp]:

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

    39   by (rule unit_imp_dvd, simp)

    40

    41 lemma normalization_factor_1 [simp]:

    42   "normalization_factor 1 = 1"

    43   by (simp add: normalization_factor_unit)

    44

    45 lemma normalization_factor_0_iff [simp]:

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

    47 proof

    48   assume "normalization_factor a = 0"

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

    50     by simp

    51   then show "a = 0" by auto

    52 qed simp

    53

    54 lemma normalization_factor_pow:

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

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

    57

    58 lemma normalization_correct [simp]:

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

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

    61   assume "a \<noteq> 0"

    62   let ?nf = "normalization_factor"

    63   from normalization_factor_is_unit[OF \<open>a \<noteq> 0\<close>] have "?nf a \<noteq> 0"

    64     by auto

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

    66     by (simp add: normalization_factor_mult)

    67   also have "a div ?nf a * ?nf a = a" using \<open>a \<noteq> 0\<close>

    68     by simp

    69   also have "?nf (?nf a) = ?nf a" using \<open>a \<noteq> 0\<close>

    70     normalization_factor_is_unit normalization_factor_unit by simp

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

    72     using \<open>?nf a \<noteq> 0\<close> by (metis div_mult_self2_is_id div_self)

    73   with \<open>a \<noteq> 0\<close> show ?thesis by simp

    74 qed

    75

    76 lemma normalization_0_iff [simp]:

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

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

    79

    80 lemma mult_div_normalization [simp]:

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

    82   by (cases "a = 0") simp_all

    83

    84 lemma associated_iff_normed_eq:

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

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

    87   let ?nf = normalization_factor

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

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

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

    91     apply (subst div_mult_swap, simp, simp)

    92     done

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

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

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

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

    97 next

    98   let ?nf = normalization_factor

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

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

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

   102     apply (simp only: \<open>a = c * b\<close> normalization_factor_mult normalization_factor_unit)

   103     apply (rule div_mult_mult1, force)

   104     done

   105   qed

   106

   107 lemma normed_associated_imp_eq:

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

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

   110

   111 lemma normed_dvd [iff]:

   112   "a div normalization_factor a dvd a"

   113 proof (cases "a = 0")

   114   case True then show ?thesis by simp

   115 next

   116   case False

   117   then have "a = a div normalization_factor a * normalization_factor a"

   118     by (auto intro: unit_div_mult_self)

   119   then show ?thesis ..

   120 qed

   121

   122 lemma dvd_normed [iff]:

   123   "a dvd a div normalization_factor a"

   124 proof (cases "a = 0")

   125   case True then show ?thesis by simp

   126 next

   127   case False

   128   then have "a div normalization_factor a = a * (1 div normalization_factor a)"

   129     by (auto intro: unit_mult_div_div)

   130   then show ?thesis ..

   131 qed

   132

   133 lemma associated_normed:

   134   "associated (a div normalization_factor a) a"

   135   by (rule associatedI) simp_all

   136

   137 lemma normalization_factor_dvd' [simp]:

   138   "normalization_factor a dvd a"

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

   140

   141 lemmas normalization_factor_dvd_iff [simp] =

   142   unit_dvd_iff [OF normalization_factor_is_unit]

   143

   144 lemma euclidean_division:

   145   fixes a :: 'a and b :: 'a

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

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

   148     and "euclidean_size t < euclidean_size b"

   149 proof -

   150   from div_mod_equality [of a b 0]

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

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

   153 qed

   154

   155 lemma dvd_euclidean_size_eq_imp_dvd:

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

   157   shows "a dvd b"

   158 proof (rule ccontr)

   159   assume "\<not> a dvd b"

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

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

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

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

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

   165       using size_mult_mono by force

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

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

   168       using mod_size_less by blast

   169   ultimately show False using size_eq by simp

   170 qed

   171

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

   173 where

   174   "gcd_eucl a b = (if b = 0 then a div normalization_factor a

   175     else if b dvd a then b div normalization_factor b

   176     else gcd_eucl b (a mod b))"

   177   by (pat_completeness, simp)

   178 termination

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

   180

   181 declare gcd_eucl.simps [simp del]

   182

   183 lemma gcd_eucl_induct [case_names zero mod]:

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

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

   186   shows "P a b"

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

   188   case ("1" a b)

   189   show ?case

   190   proof (cases "b = 0")

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

   192   next

   193     case False

   194     have "P b (a mod b)"

   195     proof (cases "b dvd a")

   196       case False with \<open>b \<noteq> 0\<close> show "P b (a mod b)"

   197         by (rule "1.hyps")

   198     next

   199       case True then have "a mod b = 0"

   200         by (simp add: mod_eq_0_iff_dvd)

   201       then show "P b (a mod b)" by simp (rule H1)

   202     qed

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

   204       by (blast intro: H2)

   205   qed

   206 qed

   207

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

   209 where

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

   211

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

   213      for infinite sets as well *)

   214

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

   216 where

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

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

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

   220        in l div normalization_factor l

   221       else 0)"

   222

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

   224 where

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

   226

   227 end

   228

   229 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   232 begin

   233

   234 lemma gcd_red:

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

   236   by (cases "b dvd a")

   237     (auto simp add: gcd_gcd_eucl gcd_eucl.simps [of a b] gcd_eucl.simps [of 0 a] gcd_eucl.simps [of b 0])

   238

   239 lemma gcd_non_0:

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

   241   by (rule gcd_red)

   242

   243 lemma gcd_0_left:

   244   "gcd 0 a = a div normalization_factor a"

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

   246

   247 lemma gcd_0:

   248   "gcd a 0 = a div normalization_factor a"

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

   250

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

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

   253   by (induct a b rule: gcd_eucl_induct)

   254     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)

   255

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

   257   by (rule dvd_trans, assumption, rule gcd_dvd1)

   258

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

   260   by (rule dvd_trans, assumption, rule gcd_dvd2)

   261

   262 lemma gcd_greatest:

   263   fixes k a b :: 'a

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

   265 proof (induct a b rule: gcd_eucl_induct)

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

   267 next

   268   case (mod a b)

   269   then show ?case

   270     by (simp add: gcd_non_0 dvd_mod_iff)

   271 qed

   272

   273 lemma dvd_gcd_iff:

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

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

   276

   277 lemmas gcd_greatest_iff = dvd_gcd_iff

   278

   279 lemma gcd_zero [simp]:

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

   281   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   282

   283 lemma normalization_factor_gcd [simp]:

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

   285   by (induct a b rule: gcd_eucl_induct)

   286     (auto simp add: gcd_0 gcd_non_0)

   287

   288 lemma gcdI:

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

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

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

   292

   293 sublocale gcd!: abel_semigroup gcd

   294 proof

   295   fix a b c

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

   297   proof (rule gcdI)

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

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

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

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

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

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

   304       by (rule gcd_greatest)

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

   306       by auto

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

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

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

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

   311       by (intro gcd_greatest)

   312   qed

   313 next

   314   fix a b

   315   show "gcd a b = gcd b a"

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

   317 qed

   318

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

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

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

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

   323

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

   325   using mult_dvd_mono [of 1] by auto

   326

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

   328   by (rule sym, rule gcdI, simp_all)

   329

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

   331   by (rule sym, rule gcdI, simp_all)

   332

   333 lemma gcd_proj2_if_dvd:

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

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

   336

   337 lemma gcd_proj1_if_dvd:

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

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

   340

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

   342 proof

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

   344   show "m dvd n"

   345   proof (cases "m = 0")

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

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

   348       by (simp add: unit_eq_div2)

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

   350   qed (insert A, simp)

   351 next

   352   assume "m dvd n"

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

   354 qed

   355

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

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

   358

   359 lemma gcd_mod1 [simp]:

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

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

   362

   363 lemma gcd_mod2 [simp]:

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

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

   366

   367 lemma gcd_mult_distrib':

   368   "c div normalization_factor c * gcd a b = gcd (c * a) (c * b)"

   369 proof (cases "c = 0")

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

   371 next

   372   case False then have [simp]: "is_unit (normalization_factor c)" by simp

   373   show ?thesis

   374   proof (induct a b rule: gcd_eucl_induct)

   375     case (zero a) show ?case

   376     proof (cases "a = 0")

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

   378     next

   379       case False then have "is_unit (normalization_factor a)" by simp

   380       then show ?thesis

   381         by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq)

   382     qed

   383     case (mod a b)

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

   385   qed

   386 qed

   387

   388 lemma gcd_mult_distrib:

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

   390 proof-

   391   let ?nf = "normalization_factor"

   392   from gcd_mult_distrib'

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

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

   395     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)

   396   finally show ?thesis

   397     by simp

   398 qed

   399

   400 lemma euclidean_size_gcd_le1 [simp]:

   401   assumes "a \<noteq> 0"

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

   403 proof -

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

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

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

   407 qed

   408

   409 lemma euclidean_size_gcd_le2 [simp]:

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

   411   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   412

   413 lemma euclidean_size_gcd_less1:

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

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

   416 proof (rule ccontr)

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

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

   419     by (intro le_antisym, simp_all)

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

   421   hence "a dvd b" using dvd_gcd_D2 by blast

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

   423 qed

   424

   425 lemma euclidean_size_gcd_less2:

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

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

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

   429

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

   431   apply (rule gcdI)

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

   433   apply (rule gcd_dvd2)

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

   435   apply (subst normalization_factor_gcd, simp add: gcd_0)

   436   done

   437

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

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

   440

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

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

   443

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

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

   446

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

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

   449

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

   451   apply (rule gcdI)

   452   apply (simp add: ac_simps)

   453   apply (rule gcd_dvd2)

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

   455   apply simp

   456   done

   457

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

   459   apply (rule gcdI)

   460   apply simp

   461   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

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

   463   apply simp

   464   done

   465

   466 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   467 proof

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

   469     by (simp add: fun_eq_iff ac_simps)

   470 next

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

   472     by (simp add: fun_eq_iff gcd_left_idem)

   473 qed

   474

   475 lemma coprime_dvd_mult:

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

   477   shows "c dvd a"

   478 proof -

   479   let ?nf = "normalization_factor"

   480   from assms gcd_mult_distrib [of a c b]

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

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

   483 qed

   484

   485 lemma coprime_dvd_mult_iff:

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

   487   by (rule, rule coprime_dvd_mult, simp_all)

   488

   489 lemma gcd_dvd_antisym:

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

   491 proof (rule gcdI)

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

   493   have "gcd c d dvd c" by simp

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

   495   have "gcd c d dvd d" by simp

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

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

   498     by simp

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

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

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

   502 qed

   503

   504 lemma gcd_mult_cancel:

   505   assumes "gcd k n = 1"

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

   507 proof (rule gcd_dvd_antisym)

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

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

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

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

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

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

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

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

   516 qed

   517

   518 lemma coprime_crossproduct:

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

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

   521 proof

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

   523 next

   524   assume ?lhs

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

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

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

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

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

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

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

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

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

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

   535   ultimately show ?rhs unfolding associated_def by simp

   536 qed

   537

   538 lemma gcd_add1 [simp]:

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

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

   541

   542 lemma gcd_add2 [simp]:

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

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

   545

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

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

   548

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

   550   by (rule sym, rule gcdI, simp_all)

   551

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

   553   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   554

   555 lemma div_gcd_coprime:

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

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

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

   559   shows "gcd a' b' = 1"

   560 proof (rule coprimeI)

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

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

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

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

   565     by (simp_all only: ac_simps)

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

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

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

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

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

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

   572   ultimately have "1 = l * u"

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

   574   then show "l dvd 1" ..

   575 qed

   576

   577 lemma coprime_mult:

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

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

   580   apply (subst gcd.commute)

   581   using da apply (subst gcd_mult_cancel)

   582   apply (subst gcd.commute, assumption)

   583   apply (subst gcd.commute, rule db)

   584   done

   585

   586 lemma coprime_lmult:

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

   588   shows "gcd d a = 1"

   589 proof (rule coprimeI)

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

   591   hence "l dvd a * b" by simp

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

   593 qed

   594

   595 lemma coprime_rmult:

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

   597   shows "gcd d b = 1"

   598 proof (rule coprimeI)

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

   600   hence "l dvd a * b" by simp

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

   602 qed

   603

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

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

   606

   607 lemma gcd_coprime:

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

   609   shows "gcd a' b' = 1"

   610 proof -

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

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

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

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

   615   finally show ?thesis .

   616 qed

   617

   618 lemma coprime_power:

   619   assumes "0 < n"

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

   621 using assms proof (induct n)

   622   case (Suc n) then show ?case

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

   624 qed simp

   625

   626 lemma gcd_coprime_exists:

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

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

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

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

   631   apply (insert nz, auto intro: div_gcd_coprime)

   632   done

   633

   634 lemma coprime_exp:

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

   636   by (induct n, simp_all add: coprime_mult)

   637

   638 lemma coprime_exp2 [intro]:

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

   640   apply (rule coprime_exp)

   641   apply (subst gcd.commute)

   642   apply (rule coprime_exp)

   643   apply (subst gcd.commute)

   644   apply assumption

   645   done

   646

   647 lemma gcd_exp:

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

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

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

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

   652 next

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

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

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

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

   657   also note gcd_mult_distrib

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

   659     by (simp add: normalization_factor_pow A)

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

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

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

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

   664   finally show ?thesis by simp

   665 qed

   666

   667 lemma coprime_common_divisor:

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

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

   670   apply simp

   671   apply (erule (1) gcd_greatest)

   672   done

   673

   674 lemma division_decomp:

   675   assumes dc: "a dvd b * c"

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

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

   678   assume "gcd a b = 0"

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

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

   681   then show ?thesis by blast

   682 next

   683   let ?d = "gcd a b"

   684   assume "?d \<noteq> 0"

   685   from gcd_coprime_exists[OF this]

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

   687     by blast

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

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

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

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

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

   693   with coprime_dvd_mult[OF ab'(3)]

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

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

   696   then show ?thesis by blast

   697 qed

   698

   699 lemma pow_divs_pow:

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

   701   shows "a dvd b"

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

   703   assume "gcd a b = 0"

   704   then show ?thesis by simp

   705 next

   706   let ?d = "gcd a b"

   707   assume "?d \<noteq> 0"

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

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

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

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

   712     by blast

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

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

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

   716     by (simp only: power_mult_distrib ac_simps)

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

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

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

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

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

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

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

   724 qed

   725

   726 lemma pow_divs_eq [simp]:

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

   728   by (auto intro: pow_divs_pow dvd_power_same)

   729

   730 lemma divs_mult:

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

   732   shows "m * n dvd r"

   733 proof -

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

   735     unfolding dvd_def by blast

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

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

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

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

   740   then show ?thesis unfolding dvd_def by blast

   741 qed

   742

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

   744   by (subst add_commute, simp)

   745

   746 lemma setprod_coprime [rule_format]:

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

   748   apply (cases "finite A")

   749   apply (induct set: finite)

   750   apply (auto simp add: gcd_mult_cancel)

   751   done

   752

   753 lemma coprime_divisors:

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

   755   shows "gcd d e = 1"

   756 proof -

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

   758     unfolding dvd_def by blast

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

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

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

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

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

   764 qed

   765

   766 lemma invertible_coprime:

   767   assumes "a * b mod m = 1"

   768   shows "coprime a m"

   769 proof -

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

   771     by simp

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

   773     by simp

   774   then have "coprime m a"

   775     by (rule coprime_lmult)

   776   then show ?thesis

   777     by (simp add: ac_simps)

   778 qed

   779

   780 lemma lcm_gcd:

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

   782   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

   783

   784 lemma lcm_gcd_prod:

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

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

   787   let ?nf = normalization_factor

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

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

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

   791     by (simp add: mult_ac)

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

   793     by (simp add: div_mult_swap mult.commute)

   794   finally show ?thesis .

   795 qed (auto simp add: lcm_gcd)

   796

   797 lemma lcm_dvd1 [iff]:

   798   "a dvd lcm a b"

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

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

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

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

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

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

   805     by (simp add: div_mult_swap unit_div_commute)

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

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

   808     by (subst (asm) div_mult_self2_is_id, simp_all)

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

   810     by (metis div_mult_swap gcd_dvd2 mult_assoc)

   811   finally show ?thesis by (rule dvdI)

   812 qed (auto simp add: lcm_gcd)

   813

   814 lemma lcm_least:

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

   816 proof (cases "k = 0")

   817   let ?nf = normalization_factor

   818   assume "k \<noteq> 0"

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

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

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

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

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

   824     unfolding dvd_def by blast

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

   840     by (simp add: algebra_simps)

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

   842     by (metis div_mult_self2_is_id)

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

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

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

   846     by (simp add: algebra_simps)

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

   848     by (metis mult.commute div_mult_self2_is_id)

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

   850     by (metis div_mult_self2_is_id mult_assoc)

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

   852     by (simp add: unit_simps)

   853   finally show ?thesis by (rule dvdI)

   854 qed simp

   855

   856 lemma lcm_zero:

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

   858 proof -

   859   let ?nf = normalization_factor

   860   {

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

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

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

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

   865   } moreover {

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

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

   868   }

   869   ultimately show ?thesis by blast

   870 qed

   871

   872 lemmas lcm_0_iff = lcm_zero

   873

   874 lemma gcd_lcm:

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

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

   877 proof-

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

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

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

   881   hence "is_unit ?c" by simp

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

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

   884   also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)"

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

   886   finally show ?thesis .

   887 qed

   888

   889 lemma normalization_factor_lcm [simp]:

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

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

   892   case True then show ?thesis

   893     by (auto simp add: lcm_gcd)

   894 next

   895   case False

   896   let ?nf = normalization_factor

   897   from lcm_gcd_prod[of a b]

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

   899     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)

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

   901     by simp

   902   finally show ?thesis using False by simp

   903 qed

   904

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

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

   907

   908 lemma lcmI:

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

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

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

   912

   913 sublocale lcm!: abel_semigroup lcm

   914 proof

   915   fix a b c

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

   917   proof (rule lcmI)

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

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

   920

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

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

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

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

   925

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

   927     have "b dvd lcm b c" by simp

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

   929     have "c dvd lcm b c" by simp

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

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

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

   933   qed (simp add: lcm_zero)

   934 next

   935   fix a b

   936   show "lcm a b = lcm b a"

   937     by (simp add: lcm_gcd ac_simps)

   938 qed

   939

   940 lemma dvd_lcm_D1:

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

   942   by (rule dvd_trans, rule lcm_dvd1, assumption)

   943

   944 lemma dvd_lcm_D2:

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

   946   by (rule dvd_trans, rule lcm_dvd2, assumption)

   947

   948 lemma gcd_dvd_lcm [simp]:

   949   "gcd a b dvd lcm a b"

   950   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

   951

   952 lemma lcm_1_iff:

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

   954 proof

   955   assume "lcm a b = 1"

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

   957 next

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

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

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

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

   962     by (subst normalization_factor_unit, simp_all)

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

   964     by auto

   965   finally show "lcm a b = 1" .

   966 qed

   967

   968 lemma lcm_0_left [simp]:

   969   "lcm 0 a = 0"

   970   by (rule sym, rule lcmI, simp_all)

   971

   972 lemma lcm_0 [simp]:

   973   "lcm a 0 = 0"

   974   by (rule sym, rule lcmI, simp_all)

   975

   976 lemma lcm_unique:

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

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

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

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

   981

   982 lemma dvd_lcm_I1 [simp]:

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

   984   by (metis lcm_dvd1 dvd_trans)

   985

   986 lemma dvd_lcm_I2 [simp]:

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

   988   by (metis lcm_dvd2 dvd_trans)

   989

   990 lemma lcm_1_left [simp]:

   991   "lcm 1 a = a div normalization_factor a"

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

   993

   994 lemma lcm_1_right [simp]:

   995   "lcm a 1 = a div normalization_factor a"

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

   997

   998 lemma lcm_coprime:

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

  1000   by (subst lcm_gcd) simp

  1001

  1002 lemma lcm_proj1_if_dvd:

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

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

  1005

  1006 lemma lcm_proj2_if_dvd:

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

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

  1009

  1010 lemma lcm_proj1_iff:

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

  1012 proof

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

  1014   show "n dvd m"

  1015   proof (cases "m = 0")

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

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

  1018       by (simp add: unit_eq_div2)

  1019     show ?thesis by (subst B, simp)

  1020   qed simp

  1021 next

  1022   assume "n dvd m"

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

  1024 qed

  1025

  1026 lemma lcm_proj2_iff:

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

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

  1029

  1030 lemma euclidean_size_lcm_le1:

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

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

  1033 proof -

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

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

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

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

  1038 qed

  1039

  1040 lemma euclidean_size_lcm_le2:

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

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

  1043

  1044 lemma euclidean_size_lcm_less1:

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

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

  1047 proof (rule ccontr)

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

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

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

  1051     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

  1052   with assms have "lcm a b dvd a"

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

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

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

  1056 qed

  1057

  1058 lemma euclidean_size_lcm_less2:

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

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

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

  1062

  1063 lemma lcm_mult_unit1:

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

  1065   apply (rule lcmI)

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

  1067   apply (rule lcm_dvd2)

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

  1069   apply (subst normalization_factor_lcm, simp add: lcm_zero)

  1070   done

  1071

  1072 lemma lcm_mult_unit2:

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

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

  1075

  1076 lemma lcm_div_unit1:

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

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

  1079

  1080 lemma lcm_div_unit2:

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

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

  1083

  1084 lemma lcm_left_idem:

  1085   "lcm a (lcm a b) = lcm a b"

  1086   apply (rule lcmI)

  1087   apply simp

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

  1089   apply (rule lcm_least, assumption)

  1090   apply (erule (1) lcm_least)

  1091   apply (auto simp: lcm_zero)

  1092   done

  1093

  1094 lemma lcm_right_idem:

  1095   "lcm (lcm a b) b = lcm a b"

  1096   apply (rule lcmI)

  1097   apply (subst lcm.assoc, rule lcm_dvd1)

  1098   apply (rule lcm_dvd2)

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

  1100   apply (auto simp: lcm_zero)

  1101   done

  1102

  1103 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

  1104 proof

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

  1106     by (simp add: fun_eq_iff ac_simps)

  1107 next

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

  1109     by (intro ext, simp add: lcm_left_idem)

  1110 qed

  1111

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

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

  1114   and normalization_factor_Lcm [simp]:

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

  1116 proof -

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

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

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

  1120     case False

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

  1122     with False show ?thesis by auto

  1123   next

  1124     case True

  1125     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

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

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

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

  1129       apply (subst n_def)

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

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

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

  1133       done

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

  1135       unfolding l_def by simp_all

  1136     {

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

  1138       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)

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

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

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

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

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

  1144       proof -

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

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

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

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

  1149           by (rule size_mult_mono)

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

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

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

  1153       qed

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

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

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

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

  1158     }

  1159

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

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

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

  1163         normalization_factor (l div normalization_factor l) =

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

  1165       by (auto simp: unit_simps)

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

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

  1168     finally show ?thesis .

  1169   qed

  1170   note A = this

  1171

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

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

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

  1175 qed

  1176

  1177 lemma LcmI:

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

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

  1180   by (intro normed_associated_imp_eq)

  1181     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)

  1182

  1183 lemma Lcm_subset:

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

  1185   by (blast intro: Lcm_dvd dvd_Lcm)

  1186

  1187 lemma Lcm_Un:

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

  1189   apply (rule lcmI)

  1190   apply (blast intro: Lcm_subset)

  1191   apply (blast intro: Lcm_subset)

  1192   apply (intro Lcm_dvd ballI, elim UnE)

  1193   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1194   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1195   apply simp

  1196   done

  1197

  1198 lemma Lcm_1_iff:

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

  1200 proof

  1201   assume "Lcm A = 1"

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

  1203 qed (rule LcmI [symmetric], auto)

  1204

  1205 lemma Lcm_no_units:

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

  1207 proof -

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

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

  1210     by (simp add: Lcm_Un[symmetric])

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

  1212   finally show ?thesis by simp

  1213 qed

  1214

  1215 lemma Lcm_empty [simp]:

  1216   "Lcm {} = 1"

  1217   by (simp add: Lcm_1_iff)

  1218

  1219 lemma Lcm_eq_0 [simp]:

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

  1221   by (drule dvd_Lcm) simp

  1222

  1223 lemma Lcm0_iff':

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

  1225 proof

  1226   assume "Lcm A = 0"

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

  1228   proof

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

  1230     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

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

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

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

  1234       apply (subst n_def)

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

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

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

  1238       done

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

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

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

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

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

  1244   qed

  1245 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1246

  1247 lemma Lcm0_iff [simp]:

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

  1249 proof -

  1250   assume "finite A"

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

  1252   moreover {

  1253     assume "0 \<notin> A"

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

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

  1256       apply simp

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

  1258       apply (rule no_zero_divisors)

  1259       apply blast+

  1260       done

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

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

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

  1264   }

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

  1266 qed

  1267

  1268 lemma Lcm_no_multiple:

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

  1270 proof -

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

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

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

  1274 qed

  1275

  1276 lemma Lcm_insert [simp]:

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

  1278 proof (rule lcmI)

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

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

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

  1282 qed (auto intro: Lcm_dvd dvd_Lcm)

  1283

  1284 lemma Lcm_finite:

  1285   assumes "finite A"

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

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

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

  1289

  1290 lemma Lcm_set [code_unfold]:

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

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

  1293

  1294 lemma Lcm_singleton [simp]:

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

  1296   by simp

  1297

  1298 lemma Lcm_2 [simp]:

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

  1300   by (simp only: Lcm_insert Lcm_empty lcm_1_right)

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

  1302

  1303 lemma Lcm_coprime:

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

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

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

  1307 using assms proof (induct rule: finite_ne_induct)

  1308   case (insert a A)

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

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

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

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

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

  1314     by (simp add: lcm_coprime)

  1315   finally show ?case .

  1316 qed simp

  1317

  1318 lemma Lcm_coprime':

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

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

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

  1322

  1323 lemma Gcd_Lcm:

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

  1325   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1326

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

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

  1329   and normalization_factor_Gcd [simp]:

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

  1331 proof -

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

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

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

  1335 next

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

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

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

  1339 next

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

  1341     by (simp add: Gcd_Lcm)

  1342 qed

  1343

  1344 lemma GcdI:

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

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

  1347   by (intro normed_associated_imp_eq)

  1348     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)

  1349

  1350 lemma Lcm_Gcd:

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

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

  1353

  1354 lemma Gcd_0_iff:

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

  1356   apply (rule iffI)

  1357   apply (rule subsetI, drule Gcd_dvd, simp)

  1358   apply (auto intro: GcdI[symmetric])

  1359   done

  1360

  1361 lemma Gcd_empty [simp]:

  1362   "Gcd {} = 0"

  1363   by (simp add: Gcd_0_iff)

  1364

  1365 lemma Gcd_1:

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

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

  1368

  1369 lemma Gcd_insert [simp]:

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

  1371 proof (rule gcdI)

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

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

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

  1375 qed auto

  1376

  1377 lemma Gcd_finite:

  1378   assumes "finite A"

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

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

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

  1382

  1383 lemma Gcd_set [code_unfold]:

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

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

  1386

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

  1388   by (simp add: gcd_0)

  1389

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

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

  1392

  1393 subclass semiring_gcd

  1394   by unfold_locales (simp_all add: gcd_greatest_iff)

  1395

  1396 end

  1397

  1398 text \<open>

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

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

  1401 \<close>

  1402

  1403 class euclidean_ring = euclidean_semiring + idom

  1404

  1405 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1406 begin

  1407

  1408 subclass euclidean_ring ..

  1409

  1410 subclass ring_gcd ..

  1411

  1412 lemma gcd_neg1 [simp]:

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

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

  1415

  1416 lemma gcd_neg2 [simp]:

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

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

  1419

  1420 lemma gcd_neg_numeral_1 [simp]:

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

  1422   by (fact gcd_neg1)

  1423

  1424 lemma gcd_neg_numeral_2 [simp]:

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

  1426   by (fact gcd_neg2)

  1427

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

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

  1430

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

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

  1433

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

  1435 proof -

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

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

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

  1439   finally show ?thesis .

  1440 qed

  1441

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

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

  1444

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

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

  1447

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

  1449   by (fact lcm_neg1)

  1450

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

  1452   by (fact lcm_neg2)

  1453

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

  1455   "euclid_ext a b =

  1456      (if b = 0 then

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

  1458       else if b dvd a then

  1459         let c = 1 div normalization_factor b in (0, c, b * c)

  1460       else

  1461         case euclid_ext b (a mod b) of

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

  1463   by (pat_completeness, simp)

  1464   termination by (relation "measure (euclidean_size \<circ> snd)")

  1465     (simp_all add: mod_size_less)

  1466

  1467 declare euclid_ext.simps [simp del]

  1468

  1469 lemma euclid_ext_0:

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

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

  1472

  1473 lemma euclid_ext_non_0:

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

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

  1476   apply (subst euclid_ext.simps)

  1477   apply (auto simp add: split: if_splits)

  1478   apply (subst euclid_ext.simps)

  1479   apply (auto simp add: split: if_splits)

  1480   done

  1481

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

  1483 where

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

  1485

  1486 lemma euclid_ext_gcd [simp]:

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

  1488   by (induct a b rule: gcd_eucl_induct)

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

  1490

  1491 lemma euclid_ext_gcd' [simp]:

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

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

  1494

  1495 lemma euclid_ext_correct:

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

  1497 proof (induct a b rule: gcd_eucl_induct)

  1498   case (zero a) then show ?case

  1499     by (simp add: euclid_ext_0 ac_simps)

  1500 next

  1501   case (mod a b)

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

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

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

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

  1506     by (simp add: algebra_simps)

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

  1508   finally show ?case

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

  1510 qed

  1511

  1512 lemma euclid_ext'_correct:

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

  1514 proof-

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

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

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

  1518     show ?thesis unfolding euclid_ext'_def by simp

  1519 qed

  1520

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

  1522   using euclid_ext'_correct by blast

  1523

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

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

  1526

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

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

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

  1530     (simp add: euclid_ext'_def euclid_ext_non_0)

  1531

  1532 end

  1533

  1534 instantiation nat :: euclidean_semiring

  1535 begin

  1536

  1537 definition [simp]:

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

  1539

  1540 definition [simp]:

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

  1542

  1543 instance proof

  1544 qed simp_all

  1545

  1546 end

  1547

  1548 instantiation int :: euclidean_ring

  1549 begin

  1550

  1551 definition [simp]:

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

  1553

  1554 definition [simp]:

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

  1556

  1557 instance proof

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

  1559 next

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

  1561 next

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

  1563 next

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

  1565 qed (auto simp: sgn_times split: abs_split)

  1566

  1567 end

  1568

  1569 instantiation poly :: (field) euclidean_semiring

  1570 begin

  1571

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

  1573   where "euclidean_size = (degree :: 'a poly \<Rightarrow> nat)"

  1574

  1575 definition normalization_factor_poly :: "'a poly \<Rightarrow> 'a poly"

  1576   where "normalization_factor p = monom (coeff p (degree p)) 0"

  1577

  1578 instance

  1579 proof (default, unfold euclidean_size_poly_def normalization_factor_poly_def)

  1580   fix p q :: "'a poly"

  1581   assume "q \<noteq> 0" and "\<not> q dvd p"

  1582   then show "degree (p mod q) < degree q"

  1583     using degree_mod_less [of q p] by (simp add: mod_eq_0_iff_dvd)

  1584 next

  1585   fix p q :: "'a poly"

  1586   assume "q \<noteq> 0"

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

  1588     by (rule degree_mult_right_le)

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

  1590     by (auto intro: is_unit_monom_0)

  1591 next

  1592   fix p :: "'a poly"

  1593   show "monom (coeff p (degree p)) 0 = p" if "is_unit p"

  1594     using that by (fact is_unit_monom_trival)

  1595 next

  1596   fix p q :: "'a poly"

  1597   show "monom (coeff (p * q) (degree (p * q))) 0 =

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

  1599     by (simp add: monom_0 coeff_degree_mult)

  1600 next

  1601   show "monom (coeff 0 (degree 0)) 0 = 0"

  1602     by simp

  1603 qed

  1604

  1605 end

  1606

  1607 end