src/HOL/Number_Theory/Euclidean_Algorithm.thy
author haftmann
Thu Jun 25 15:01:43 2015 +0200 (2015-06-25)
changeset 60572 718b1ba06429
parent 60571 c9fdf2080447
child 60582 d694f217ee41
permissions -rw-r--r--
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
     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 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
   213   Somewhat complicated definition of Lcm that has the advantage of working
   214   for infinite sets as well\<close>
   215 where
   216   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
   217      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
   218        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
   219        in l div normalization_factor l
   220       else 0)"
   221 
   222 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
   223 where
   224   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
   225 
   226 lemma gcd_eucl_0:
   227   "gcd_eucl a 0 = a div normalization_factor a"
   228   by (simp add: gcd_eucl.simps [of a 0])
   229 
   230 lemma gcd_eucl_0_left:
   231   "gcd_eucl 0 a = a div normalization_factor a"
   232   by (simp add: gcd_eucl.simps [of 0 a])
   233 
   234 lemma gcd_eucl_non_0:
   235   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
   236   by (cases "b dvd a")
   237     (simp_all add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
   238 
   239 lemma gcd_eucl_code [code]:
   240   "gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))"
   241   by (auto simp add: gcd_eucl_non_0 gcd_eucl_0 gcd_eucl_0_left) 
   242 
   243 end
   244 
   245 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
   246   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
   247   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
   248 begin
   249 
   250 lemma gcd_0_left:
   251   "gcd 0 a = a div normalization_factor a"
   252   unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
   253 
   254 lemma gcd_0:
   255   "gcd a 0 = a div normalization_factor a"
   256   unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
   257 
   258 lemma gcd_non_0:
   259   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
   260   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
   261 
   262 lemma gcd_dvd1 [iff]: "gcd a b dvd a"
   263   and gcd_dvd2 [iff]: "gcd a b dvd b"
   264   by (induct a b rule: gcd_eucl_induct)
   265     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
   266     
   267 lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
   268   by (rule dvd_trans, assumption, rule gcd_dvd1)
   269 
   270 lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
   271   by (rule dvd_trans, assumption, rule gcd_dvd2)
   272 
   273 lemma gcd_greatest:
   274   fixes k a b :: 'a
   275   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
   276 proof (induct a b rule: gcd_eucl_induct)
   277   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
   278 next
   279   case (mod a b)
   280   then show ?case
   281     by (simp add: gcd_non_0 dvd_mod_iff)
   282 qed
   283 
   284 lemma dvd_gcd_iff:
   285   "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
   286   by (blast intro!: gcd_greatest intro: dvd_trans)
   287 
   288 lemmas gcd_greatest_iff = dvd_gcd_iff
   289 
   290 lemma gcd_zero [simp]:
   291   "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
   292   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
   293 
   294 lemma normalization_factor_gcd [simp]:
   295   "normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
   296   by (induct a b rule: gcd_eucl_induct)
   297     (auto simp add: gcd_0 gcd_non_0)
   298 
   299 lemma gcdI:
   300   "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k)
   301     \<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b"
   302   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
   303 
   304 sublocale gcd!: abel_semigroup gcd
   305 proof
   306   fix a b c 
   307   show "gcd (gcd a b) c = gcd a (gcd b c)"
   308   proof (rule gcdI)
   309     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
   310     then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
   311     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
   312     hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
   313     moreover have "gcd (gcd a b) c dvd c" by simp
   314     ultimately show "gcd (gcd a b) c dvd gcd b c"
   315       by (rule gcd_greatest)
   316     show "normalization_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
   317       by auto
   318     fix l assume "l dvd a" and "l dvd gcd b c"
   319     with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
   320       have "l dvd b" and "l dvd c" by blast+
   321     with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
   322       by (intro gcd_greatest)
   323   qed
   324 next
   325   fix a b
   326   show "gcd a b = gcd b a"
   327     by (rule gcdI) (simp_all add: gcd_greatest)
   328 qed
   329 
   330 lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 
   331     normalization_factor d = (if d = 0 then 0 else 1) \<and>
   332     (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
   333   by (rule, auto intro: gcdI simp: gcd_greatest)
   334 
   335 lemma gcd_dvd_prod: "gcd a b dvd k * b"
   336   using mult_dvd_mono [of 1] by auto
   337 
   338 lemma gcd_1_left [simp]: "gcd 1 a = 1"
   339   by (rule sym, rule gcdI, simp_all)
   340 
   341 lemma gcd_1 [simp]: "gcd a 1 = 1"
   342   by (rule sym, rule gcdI, simp_all)
   343 
   344 lemma gcd_proj2_if_dvd: 
   345   "b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b"
   346   by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
   347 
   348 lemma gcd_proj1_if_dvd: 
   349   "a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a"
   350   by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
   351 
   352 lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n"
   353 proof
   354   assume A: "gcd m n = m div normalization_factor m"
   355   show "m dvd n"
   356   proof (cases "m = 0")
   357     assume [simp]: "m \<noteq> 0"
   358     from A have B: "m = gcd m n * normalization_factor m"
   359       by (simp add: unit_eq_div2)
   360     show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
   361   qed (insert A, simp)
   362 next
   363   assume "m dvd n"
   364   then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd)
   365 qed
   366   
   367 lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m"
   368   by (subst gcd.commute, simp add: gcd_proj1_iff)
   369 
   370 lemma gcd_mod1 [simp]:
   371   "gcd (a mod b) b = gcd a b"
   372   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
   373 
   374 lemma gcd_mod2 [simp]:
   375   "gcd a (b mod a) = gcd a b"
   376   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
   377          
   378 lemma gcd_mult_distrib': 
   379   "c div normalization_factor c * gcd a b = gcd (c * a) (c * b)"
   380 proof (cases "c = 0")
   381   case True then show ?thesis by (simp_all add: gcd_0)
   382 next
   383   case False then have [simp]: "is_unit (normalization_factor c)" by simp
   384   show ?thesis
   385   proof (induct a b rule: gcd_eucl_induct)
   386     case (zero a) show ?case
   387     proof (cases "a = 0")
   388       case True then show ?thesis by (simp add: gcd_0)
   389     next
   390       case False then have "is_unit (normalization_factor a)" by simp
   391       then show ?thesis
   392         by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq)
   393     qed
   394     case (mod a b)
   395     then show ?case by (simp add: mult_mod_right gcd.commute)
   396   qed
   397 qed
   398 
   399 lemma gcd_mult_distrib:
   400   "k * gcd a b = gcd (k*a) (k*b) * normalization_factor k"
   401 proof-
   402   let ?nf = "normalization_factor"
   403   from gcd_mult_distrib' 
   404     have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" ..
   405   also have "... = k * gcd a b div ?nf k"
   406     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)
   407   finally show ?thesis
   408     by simp
   409 qed
   410 
   411 lemma euclidean_size_gcd_le1 [simp]:
   412   assumes "a \<noteq> 0"
   413   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
   414 proof -
   415    have "gcd a b dvd a" by (rule gcd_dvd1)
   416    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
   417    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
   418 qed
   419 
   420 lemma euclidean_size_gcd_le2 [simp]:
   421   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
   422   by (subst gcd.commute, rule euclidean_size_gcd_le1)
   423 
   424 lemma euclidean_size_gcd_less1:
   425   assumes "a \<noteq> 0" and "\<not>a dvd b"
   426   shows "euclidean_size (gcd a b) < euclidean_size a"
   427 proof (rule ccontr)
   428   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
   429   with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
   430     by (intro le_antisym, simp_all)
   431   with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
   432   hence "a dvd b" using dvd_gcd_D2 by blast
   433   with \<open>\<not>a dvd b\<close> show False by contradiction
   434 qed
   435 
   436 lemma euclidean_size_gcd_less2:
   437   assumes "b \<noteq> 0" and "\<not>b dvd a"
   438   shows "euclidean_size (gcd a b) < euclidean_size b"
   439   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
   440 
   441 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
   442   apply (rule gcdI)
   443   apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
   444   apply (rule gcd_dvd2)
   445   apply (rule gcd_greatest, simp add: unit_simps, assumption)
   446   apply (subst normalization_factor_gcd, simp add: gcd_0)
   447   done
   448 
   449 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
   450   by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
   451 
   452 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
   453   by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
   454 
   455 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
   456   by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
   457 
   458 lemma gcd_idem: "gcd a a = a div normalization_factor a"
   459   by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
   460 
   461 lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
   462   apply (rule gcdI)
   463   apply (simp add: ac_simps)
   464   apply (rule gcd_dvd2)
   465   apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
   466   apply simp
   467   done
   468 
   469 lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
   470   apply (rule gcdI)
   471   apply simp
   472   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
   473   apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
   474   apply simp
   475   done
   476 
   477 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
   478 proof
   479   fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
   480     by (simp add: fun_eq_iff ac_simps)
   481 next
   482   fix a show "gcd a \<circ> gcd a = gcd a"
   483     by (simp add: fun_eq_iff gcd_left_idem)
   484 qed
   485 
   486 lemma coprime_dvd_mult:
   487   assumes "gcd c b = 1" and "c dvd a * b"
   488   shows "c dvd a"
   489 proof -
   490   let ?nf = "normalization_factor"
   491   from assms gcd_mult_distrib [of a c b] 
   492     have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
   493   from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
   494 qed
   495 
   496 lemma coprime_dvd_mult_iff:
   497   "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
   498   by (rule, rule coprime_dvd_mult, simp_all)
   499 
   500 lemma gcd_dvd_antisym:
   501   "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
   502 proof (rule gcdI)
   503   assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
   504   have "gcd c d dvd c" by simp
   505   with A show "gcd a b dvd c" by (rule dvd_trans)
   506   have "gcd c d dvd d" by simp
   507   with A show "gcd a b dvd d" by (rule dvd_trans)
   508   show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
   509     by simp
   510   fix l assume "l dvd c" and "l dvd d"
   511   hence "l dvd gcd c d" by (rule gcd_greatest)
   512   from this and B show "l dvd gcd a b" by (rule dvd_trans)
   513 qed
   514 
   515 lemma gcd_mult_cancel:
   516   assumes "gcd k n = 1"
   517   shows "gcd (k * m) n = gcd m n"
   518 proof (rule gcd_dvd_antisym)
   519   have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
   520   also note \<open>gcd k n = 1\<close>
   521   finally have "gcd (gcd (k * m) n) k = 1" by simp
   522   hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
   523   moreover have "gcd (k * m) n dvd n" by simp
   524   ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
   525   have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
   526   then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
   527 qed
   528 
   529 lemma coprime_crossproduct:
   530   assumes [simp]: "gcd a d = 1" "gcd b c = 1"
   531   shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
   532 proof
   533   assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
   534 next
   535   assume ?lhs
   536   from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
   537   hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
   538   moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
   539   hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
   540   moreover from \<open>?lhs\<close> have "c dvd d * b" 
   541     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
   542   hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
   543   moreover from \<open>?lhs\<close> have "d dvd c * a"
   544     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
   545   hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
   546   ultimately show ?rhs unfolding associated_def by simp
   547 qed
   548 
   549 lemma gcd_add1 [simp]:
   550   "gcd (m + n) n = gcd m n"
   551   by (cases "n = 0", simp_all add: gcd_non_0)
   552 
   553 lemma gcd_add2 [simp]:
   554   "gcd m (m + n) = gcd m n"
   555   using gcd_add1 [of n m] by (simp add: ac_simps)
   556 
   557 lemma gcd_add_mult:
   558   "gcd m (k * m + n) = gcd m n"
   559 proof -
   560   have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
   561     by (fact gcd_mod2)
   562   then show ?thesis by simp 
   563 qed
   564 
   565 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
   566   by (rule sym, rule gcdI, simp_all)
   567 
   568 lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
   569   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
   570 
   571 lemma div_gcd_coprime:
   572   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   573   defines [simp]: "d \<equiv> gcd a b"
   574   defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
   575   shows "gcd a' b' = 1"
   576 proof (rule coprimeI)
   577   fix l assume "l dvd a'" "l dvd b'"
   578   then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
   579   moreover have "a = a' * d" "b = b' * d" by simp_all
   580   ultimately have "a = (l * d) * s" "b = (l * d) * t"
   581     by (simp_all only: ac_simps)
   582   hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
   583   hence "l*d dvd d" by (simp add: gcd_greatest)
   584   then obtain u where "d = l * d * u" ..
   585   then have "d * (l * u) = d" by (simp add: ac_simps)
   586   moreover from nz have "d \<noteq> 0" by simp
   587   with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
   588   ultimately have "1 = l * u"
   589     using \<open>d \<noteq> 0\<close> by simp
   590   then show "l dvd 1" ..
   591 qed
   592 
   593 lemma coprime_mult: 
   594   assumes da: "gcd d a = 1" and db: "gcd d b = 1"
   595   shows "gcd d (a * b) = 1"
   596   apply (subst gcd.commute)
   597   using da apply (subst gcd_mult_cancel)
   598   apply (subst gcd.commute, assumption)
   599   apply (subst gcd.commute, rule db)
   600   done
   601 
   602 lemma coprime_lmult:
   603   assumes dab: "gcd d (a * b) = 1" 
   604   shows "gcd d a = 1"
   605 proof (rule coprimeI)
   606   fix l assume "l dvd d" and "l dvd a"
   607   hence "l dvd a * b" by simp
   608   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
   609 qed
   610 
   611 lemma coprime_rmult:
   612   assumes dab: "gcd d (a * b) = 1"
   613   shows "gcd d b = 1"
   614 proof (rule coprimeI)
   615   fix l assume "l dvd d" and "l dvd b"
   616   hence "l dvd a * b" by simp
   617   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
   618 qed
   619 
   620 lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
   621   using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
   622 
   623 lemma gcd_coprime:
   624   assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
   625   shows "gcd a' b' = 1"
   626 proof -
   627   from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
   628   with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
   629   also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
   630   also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
   631   finally show ?thesis .
   632 qed
   633 
   634 lemma coprime_power:
   635   assumes "0 < n"
   636   shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
   637 using assms proof (induct n)
   638   case (Suc n) then show ?case
   639     by (cases n) (simp_all add: coprime_mul_eq)
   640 qed simp
   641 
   642 lemma gcd_coprime_exists:
   643   assumes nz: "gcd a b \<noteq> 0"
   644   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
   645   apply (rule_tac x = "a div gcd a b" in exI)
   646   apply (rule_tac x = "b div gcd a b" in exI)
   647   apply (insert nz, auto intro: div_gcd_coprime)
   648   done
   649 
   650 lemma coprime_exp:
   651   "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
   652   by (induct n, simp_all add: coprime_mult)
   653 
   654 lemma coprime_exp2 [intro]:
   655   "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
   656   apply (rule coprime_exp)
   657   apply (subst gcd.commute)
   658   apply (rule coprime_exp)
   659   apply (subst gcd.commute)
   660   apply assumption
   661   done
   662 
   663 lemma gcd_exp:
   664   "gcd (a^n) (b^n) = (gcd a b) ^ n"
   665 proof (cases "a = 0 \<and> b = 0")
   666   assume "a = 0 \<and> b = 0"
   667   then show ?thesis by (cases n, simp_all add: gcd_0_left)
   668 next
   669   assume A: "\<not>(a = 0 \<and> b = 0)"
   670   hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
   671     using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
   672   hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
   673   also note gcd_mult_distrib
   674   also have "normalization_factor ((gcd a b)^n) = 1"
   675     by (simp add: normalization_factor_pow A)
   676   also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
   677     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
   678   also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
   679     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
   680   finally show ?thesis by simp
   681 qed
   682 
   683 lemma coprime_common_divisor: 
   684   "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
   685   apply (subgoal_tac "a dvd gcd a b")
   686   apply simp
   687   apply (erule (1) gcd_greatest)
   688   done
   689 
   690 lemma division_decomp: 
   691   assumes dc: "a dvd b * c"
   692   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
   693 proof (cases "gcd a b = 0")
   694   assume "gcd a b = 0"
   695   hence "a = 0 \<and> b = 0" by simp
   696   hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
   697   then show ?thesis by blast
   698 next
   699   let ?d = "gcd a b"
   700   assume "?d \<noteq> 0"
   701   from gcd_coprime_exists[OF this]
   702     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
   703     by blast
   704   from ab'(1) have "a' dvd a" unfolding dvd_def by blast
   705   with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
   706   from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
   707   hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
   708   with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
   709   with coprime_dvd_mult[OF ab'(3)] 
   710     have "a' dvd c" by (subst (asm) ac_simps, blast)
   711   with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
   712   then show ?thesis by blast
   713 qed
   714 
   715 lemma pow_divs_pow:
   716   assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
   717   shows "a dvd b"
   718 proof (cases "gcd a b = 0")
   719   assume "gcd a b = 0"
   720   then show ?thesis by simp
   721 next
   722   let ?d = "gcd a b"
   723   assume "?d \<noteq> 0"
   724   from n obtain m where m: "n = Suc m" by (cases n, simp_all)
   725   from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
   726   from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
   727     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
   728     by blast
   729   from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
   730     by (simp add: ab'(1,2)[symmetric])
   731   hence "?d^n * a'^n dvd ?d^n * b'^n"
   732     by (simp only: power_mult_distrib ac_simps)
   733   with zn have "a'^n dvd b'^n" by simp
   734   hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
   735   hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
   736   with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
   737     have "a' dvd b'" by (subst (asm) ac_simps, blast)
   738   hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
   739   with ab'(1,2) show ?thesis by simp
   740 qed
   741 
   742 lemma pow_divs_eq [simp]:
   743   "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
   744   by (auto intro: pow_divs_pow dvd_power_same)
   745 
   746 lemma divs_mult:
   747   assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
   748   shows "m * n dvd r"
   749 proof -
   750   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
   751     unfolding dvd_def by blast
   752   from mr n' have "m dvd n'*n" by (simp add: ac_simps)
   753   hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
   754   then obtain k where k: "n' = m*k" unfolding dvd_def by blast
   755   with n' have "r = m * n * k" by (simp add: mult_ac)
   756   then show ?thesis unfolding dvd_def by blast
   757 qed
   758 
   759 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
   760   by (subst add_commute, simp)
   761 
   762 lemma setprod_coprime [rule_format]:
   763   "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
   764   apply (cases "finite A")
   765   apply (induct set: finite)
   766   apply (auto simp add: gcd_mult_cancel)
   767   done
   768 
   769 lemma coprime_divisors: 
   770   assumes "d dvd a" "e dvd b" "gcd a b = 1"
   771   shows "gcd d e = 1" 
   772 proof -
   773   from assms obtain k l where "a = d * k" "b = e * l"
   774     unfolding dvd_def by blast
   775   with assms have "gcd (d * k) (e * l) = 1" by simp
   776   hence "gcd (d * k) e = 1" by (rule coprime_lmult)
   777   also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
   778   finally have "gcd e d = 1" by (rule coprime_lmult)
   779   then show ?thesis by (simp add: ac_simps)
   780 qed
   781 
   782 lemma invertible_coprime:
   783   assumes "a * b mod m = 1"
   784   shows "coprime a m"
   785 proof -
   786   from assms have "coprime m (a * b mod m)"
   787     by simp
   788   then have "coprime m (a * b)"
   789     by simp
   790   then have "coprime m a"
   791     by (rule coprime_lmult)
   792   then show ?thesis
   793     by (simp add: ac_simps)
   794 qed
   795 
   796 lemma lcm_gcd:
   797   "lcm a b = a * b div (gcd a b * normalization_factor (a*b))"
   798   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
   799 
   800 lemma lcm_gcd_prod:
   801   "lcm a b * gcd a b = a * b div normalization_factor (a*b)"
   802 proof (cases "a * b = 0")
   803   let ?nf = normalization_factor
   804   assume "a * b \<noteq> 0"
   805   hence "gcd a b \<noteq> 0" by simp
   806   from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
   807     by (simp add: mult_ac)
   808   also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)"
   809     by (simp add: div_mult_swap mult.commute)
   810   finally show ?thesis .
   811 qed (auto simp add: lcm_gcd)
   812 
   813 lemma lcm_dvd1 [iff]:
   814   "a dvd lcm a b"
   815 proof (cases "a*b = 0")
   816   assume "a * b \<noteq> 0"
   817   hence "gcd a b \<noteq> 0" by simp
   818   let ?c = "1 div normalization_factor (a * b)"
   819   from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp
   820   from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
   821     by (simp add: div_mult_swap unit_div_commute)
   822   hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
   823   with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
   824     by (subst (asm) div_mult_self2_is_id, simp_all)
   825   also have "... = a * (?c * b div gcd a b)"
   826     by (metis div_mult_swap gcd_dvd2 mult_assoc)
   827   finally show ?thesis by (rule dvdI)
   828 qed (auto simp add: lcm_gcd)
   829 
   830 lemma lcm_least:
   831   "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
   832 proof (cases "k = 0")
   833   let ?nf = normalization_factor
   834   assume "k \<noteq> 0"
   835   hence "is_unit (?nf k)" by simp
   836   hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
   837   assume A: "a dvd k" "b dvd k"
   838   hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
   839   from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
   840     unfolding dvd_def by blast
   841   with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
   842     by auto (drule sym [of 0], simp)
   843   hence "is_unit (?nf (r * s))" by simp
   844   let ?c = "?nf k div ?nf (r*s)"
   845   from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
   846   hence "?c \<noteq> 0" using not_is_unit_0 by fast 
   847   from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
   848     by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
   849   also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
   850     by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
   851   also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
   852     by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
   853   finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
   854     by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
   855   hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
   856     by (simp add: algebra_simps)
   857   hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
   858     by (metis div_mult_self2_is_id)
   859   also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
   860     by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') 
   861   also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
   862     by (simp add: algebra_simps)
   863   finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
   864     by (metis mult.commute div_mult_self2_is_id)
   865   hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
   866     by (metis div_mult_self2_is_id mult_assoc) 
   867   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
   868     by (simp add: unit_simps)
   869   finally show ?thesis by (rule dvdI)
   870 qed simp
   871 
   872 lemma lcm_zero:
   873   "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
   874 proof -
   875   let ?nf = normalization_factor
   876   {
   877     assume "a \<noteq> 0" "b \<noteq> 0"
   878     hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
   879     moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
   880     ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
   881   } moreover {
   882     assume "a = 0 \<or> b = 0"
   883     hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
   884   }
   885   ultimately show ?thesis by blast
   886 qed
   887 
   888 lemmas lcm_0_iff = lcm_zero
   889 
   890 lemma gcd_lcm: 
   891   assumes "lcm a b \<noteq> 0"
   892   shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))"
   893 proof-
   894   from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)
   895   let ?c = "normalization_factor (a * b)"
   896   from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
   897   hence "is_unit ?c" by simp
   898   from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
   899     by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac)
   900   also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)"
   901     by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd')
   902   finally show ?thesis .
   903 qed
   904 
   905 lemma normalization_factor_lcm [simp]:
   906   "normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
   907 proof (cases "a = 0 \<or> b = 0")
   908   case True then show ?thesis
   909     by (auto simp add: lcm_gcd) 
   910 next
   911   case False
   912   let ?nf = normalization_factor
   913   from lcm_gcd_prod[of a b] 
   914     have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
   915     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)
   916   also have "... = (if a*b = 0 then 0 else 1)"
   917     by simp
   918   finally show ?thesis using False by simp
   919 qed
   920 
   921 lemma lcm_dvd2 [iff]: "b dvd lcm a b"
   922   using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
   923 
   924 lemma lcmI:
   925   "\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l;
   926     normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b"
   927   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
   928 
   929 sublocale lcm!: abel_semigroup lcm
   930 proof
   931   fix a b c
   932   show "lcm (lcm a b) c = lcm a (lcm b c)"
   933   proof (rule lcmI)
   934     have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
   935     then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
   936     
   937     have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
   938     hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
   939     moreover have "c dvd lcm (lcm a b) c" by simp
   940     ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
   941 
   942     fix l assume "a dvd l" and "lcm b c dvd l"
   943     have "b dvd lcm b c" by simp
   944     from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
   945     have "c dvd lcm b c" by simp
   946     from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
   947     from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
   948     from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
   949   qed (simp add: lcm_zero)
   950 next
   951   fix a b
   952   show "lcm a b = lcm b a"
   953     by (simp add: lcm_gcd ac_simps)
   954 qed
   955 
   956 lemma dvd_lcm_D1:
   957   "lcm m n dvd k \<Longrightarrow> m dvd k"
   958   by (rule dvd_trans, rule lcm_dvd1, assumption)
   959 
   960 lemma dvd_lcm_D2:
   961   "lcm m n dvd k \<Longrightarrow> n dvd k"
   962   by (rule dvd_trans, rule lcm_dvd2, assumption)
   963 
   964 lemma gcd_dvd_lcm [simp]:
   965   "gcd a b dvd lcm a b"
   966   by (metis dvd_trans gcd_dvd2 lcm_dvd2)
   967 
   968 lemma lcm_1_iff:
   969   "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
   970 proof
   971   assume "lcm a b = 1"
   972   then show "is_unit a \<and> is_unit b" by auto
   973 next
   974   assume "is_unit a \<and> is_unit b"
   975   hence "a dvd 1" and "b dvd 1" by simp_all
   976   hence "is_unit (lcm a b)" by (rule lcm_least)
   977   hence "lcm a b = normalization_factor (lcm a b)"
   978     by (subst normalization_factor_unit, simp_all)
   979   also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
   980     by auto
   981   finally show "lcm a b = 1" .
   982 qed
   983 
   984 lemma lcm_0_left [simp]:
   985   "lcm 0 a = 0"
   986   by (rule sym, rule lcmI, simp_all)
   987 
   988 lemma lcm_0 [simp]:
   989   "lcm a 0 = 0"
   990   by (rule sym, rule lcmI, simp_all)
   991 
   992 lemma lcm_unique:
   993   "a dvd d \<and> b dvd d \<and> 
   994   normalization_factor d = (if d = 0 then 0 else 1) \<and>
   995   (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
   996   by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
   997 
   998 lemma dvd_lcm_I1 [simp]:
   999   "k dvd m \<Longrightarrow> k dvd lcm m n"
  1000   by (metis lcm_dvd1 dvd_trans)
  1001 
  1002 lemma dvd_lcm_I2 [simp]:
  1003   "k dvd n \<Longrightarrow> k dvd lcm m n"
  1004   by (metis lcm_dvd2 dvd_trans)
  1005 
  1006 lemma lcm_1_left [simp]:
  1007   "lcm 1 a = a div normalization_factor a"
  1008   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
  1009 
  1010 lemma lcm_1_right [simp]:
  1011   "lcm a 1 = a div normalization_factor a"
  1012   using lcm_1_left [of a] by (simp add: ac_simps)
  1013 
  1014 lemma lcm_coprime:
  1015   "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)"
  1016   by (subst lcm_gcd) simp
  1017 
  1018 lemma lcm_proj1_if_dvd: 
  1019   "b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a"
  1020   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
  1021 
  1022 lemma lcm_proj2_if_dvd: 
  1023   "a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b"
  1024   using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
  1025 
  1026 lemma lcm_proj1_iff:
  1027   "lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m"
  1028 proof
  1029   assume A: "lcm m n = m div normalization_factor m"
  1030   show "n dvd m"
  1031   proof (cases "m = 0")
  1032     assume [simp]: "m \<noteq> 0"
  1033     from A have B: "m = lcm m n * normalization_factor m"
  1034       by (simp add: unit_eq_div2)
  1035     show ?thesis by (subst B, simp)
  1036   qed simp
  1037 next
  1038   assume "n dvd m"
  1039   then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd)
  1040 qed
  1041 
  1042 lemma lcm_proj2_iff:
  1043   "lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n"
  1044   using lcm_proj1_iff [of n m] by (simp add: ac_simps)
  1045 
  1046 lemma euclidean_size_lcm_le1: 
  1047   assumes "a \<noteq> 0" and "b \<noteq> 0"
  1048   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
  1049 proof -
  1050   have "a dvd lcm a b" by (rule lcm_dvd1)
  1051   then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
  1052   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
  1053   then show ?thesis by (subst A, intro size_mult_mono)
  1054 qed
  1055 
  1056 lemma euclidean_size_lcm_le2:
  1057   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
  1058   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
  1059 
  1060 lemma euclidean_size_lcm_less1:
  1061   assumes "b \<noteq> 0" and "\<not>b dvd a"
  1062   shows "euclidean_size a < euclidean_size (lcm a b)"
  1063 proof (rule ccontr)
  1064   from assms have "a \<noteq> 0" by auto
  1065   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
  1066   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
  1067     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
  1068   with assms have "lcm a b dvd a" 
  1069     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
  1070   hence "b dvd a" by (rule dvd_lcm_D2)
  1071   with \<open>\<not>b dvd a\<close> show False by contradiction
  1072 qed
  1073 
  1074 lemma euclidean_size_lcm_less2:
  1075   assumes "a \<noteq> 0" and "\<not>a dvd b"
  1076   shows "euclidean_size b < euclidean_size (lcm a b)"
  1077   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
  1078 
  1079 lemma lcm_mult_unit1:
  1080   "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
  1081   apply (rule lcmI)
  1082   apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
  1083   apply (rule lcm_dvd2)
  1084   apply (rule lcm_least, simp add: unit_simps, assumption)
  1085   apply (subst normalization_factor_lcm, simp add: lcm_zero)
  1086   done
  1087 
  1088 lemma lcm_mult_unit2:
  1089   "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
  1090   using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
  1091 
  1092 lemma lcm_div_unit1:
  1093   "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
  1094   by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
  1095 
  1096 lemma lcm_div_unit2:
  1097   "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
  1098   by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
  1099 
  1100 lemma lcm_left_idem:
  1101   "lcm a (lcm a b) = lcm a b"
  1102   apply (rule lcmI)
  1103   apply simp
  1104   apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
  1105   apply (rule lcm_least, assumption)
  1106   apply (erule (1) lcm_least)
  1107   apply (auto simp: lcm_zero)
  1108   done
  1109 
  1110 lemma lcm_right_idem:
  1111   "lcm (lcm a b) b = lcm a b"
  1112   apply (rule lcmI)
  1113   apply (subst lcm.assoc, rule lcm_dvd1)
  1114   apply (rule lcm_dvd2)
  1115   apply (rule lcm_least, erule (1) lcm_least, assumption)
  1116   apply (auto simp: lcm_zero)
  1117   done
  1118 
  1119 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
  1120 proof
  1121   fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
  1122     by (simp add: fun_eq_iff ac_simps)
  1123 next
  1124   fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
  1125     by (intro ext, simp add: lcm_left_idem)
  1126 qed
  1127 
  1128 lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
  1129   and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'"
  1130   and normalization_factor_Lcm [simp]: 
  1131           "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
  1132 proof -
  1133   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>
  1134     normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
  1135   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
  1136     case False
  1137     hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
  1138     with False show ?thesis by auto
  1139   next
  1140     case True
  1141     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
  1142     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1143     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1144     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1145       apply (subst n_def)
  1146       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
  1147       apply (rule exI[of _ l\<^sub>0])
  1148       apply (simp add: l\<^sub>0_props)
  1149       done
  1150     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
  1151       unfolding l_def by simp_all
  1152     {
  1153       fix l' assume "\<forall>a\<in>A. a dvd l'"
  1154       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)
  1155       moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
  1156       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
  1157         by (intro exI[of _ "gcd l l'"], auto)
  1158       hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
  1159       moreover have "euclidean_size (gcd l l') \<le> n"
  1160       proof -
  1161         have "gcd l l' dvd l" by simp
  1162         then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
  1163         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
  1164         hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
  1165           by (rule size_mult_mono)
  1166         also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
  1167         also note \<open>euclidean_size l = n\<close>
  1168         finally show "euclidean_size (gcd l l') \<le> n" .
  1169       qed
  1170       ultimately have "euclidean_size l = euclidean_size (gcd l l')" 
  1171         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
  1172       with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
  1173       hence "l dvd l'" by (blast dest: dvd_gcd_D2)
  1174     }
  1175 
  1176     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>
  1177       have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and> 
  1178         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and>
  1179         normalization_factor (l div normalization_factor l) = 
  1180         (if l div normalization_factor l = 0 then 0 else 1)"
  1181       by (auto simp: unit_simps)
  1182     also from True have "l div normalization_factor l = Lcm A"
  1183       by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
  1184     finally show ?thesis .
  1185   qed
  1186   note A = this
  1187 
  1188   {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
  1189   {fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast}
  1190   from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
  1191 qed
  1192     
  1193 lemma LcmI:
  1194   "(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>
  1195       normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
  1196   by (intro normed_associated_imp_eq)
  1197     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
  1198 
  1199 lemma Lcm_subset:
  1200   "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
  1201   by (blast intro: Lcm_dvd dvd_Lcm)
  1202 
  1203 lemma Lcm_Un:
  1204   "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
  1205   apply (rule lcmI)
  1206   apply (blast intro: Lcm_subset)
  1207   apply (blast intro: Lcm_subset)
  1208   apply (intro Lcm_dvd ballI, elim UnE)
  1209   apply (rule dvd_trans, erule dvd_Lcm, assumption)
  1210   apply (rule dvd_trans, erule dvd_Lcm, assumption)
  1211   apply simp
  1212   done
  1213 
  1214 lemma Lcm_1_iff:
  1215   "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
  1216 proof
  1217   assume "Lcm A = 1"
  1218   then show "\<forall>a\<in>A. is_unit a" by auto
  1219 qed (rule LcmI [symmetric], auto)
  1220 
  1221 lemma Lcm_no_units:
  1222   "Lcm A = Lcm (A - {a. is_unit a})"
  1223 proof -
  1224   have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
  1225   hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
  1226     by (simp add: Lcm_Un[symmetric])
  1227   also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
  1228   finally show ?thesis by simp
  1229 qed
  1230 
  1231 lemma Lcm_empty [simp]:
  1232   "Lcm {} = 1"
  1233   by (simp add: Lcm_1_iff)
  1234 
  1235 lemma Lcm_eq_0 [simp]:
  1236   "0 \<in> A \<Longrightarrow> Lcm A = 0"
  1237   by (drule dvd_Lcm) simp
  1238 
  1239 lemma Lcm0_iff':
  1240   "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
  1241 proof
  1242   assume "Lcm A = 0"
  1243   show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
  1244   proof
  1245     assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
  1246     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
  1247     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1248     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1249     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
  1250       apply (subst n_def)
  1251       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
  1252       apply (rule exI[of _ l\<^sub>0])
  1253       apply (simp add: l\<^sub>0_props)
  1254       done
  1255     from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
  1256     hence "l div normalization_factor l \<noteq> 0" by simp
  1257     also from ex have "l div normalization_factor l = Lcm A"
  1258        by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
  1259     finally show False using \<open>Lcm A = 0\<close> by contradiction
  1260   qed
  1261 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
  1262 
  1263 lemma Lcm0_iff [simp]:
  1264   "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
  1265 proof -
  1266   assume "finite A"
  1267   have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
  1268   moreover {
  1269     assume "0 \<notin> A"
  1270     hence "\<Prod>A \<noteq> 0" 
  1271       apply (induct rule: finite_induct[OF \<open>finite A\<close>]) 
  1272       apply simp
  1273       apply (subst setprod.insert, assumption, assumption)
  1274       apply (rule no_zero_divisors)
  1275       apply blast+
  1276       done
  1277     moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
  1278     ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
  1279     with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
  1280   }
  1281   ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
  1282 qed
  1283 
  1284 lemma Lcm_no_multiple:
  1285   "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
  1286 proof -
  1287   assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
  1288   hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
  1289   then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
  1290 qed
  1291 
  1292 lemma Lcm_insert [simp]:
  1293   "Lcm (insert a A) = lcm a (Lcm A)"
  1294 proof (rule lcmI)
  1295   fix l assume "a dvd l" and "Lcm A dvd l"
  1296   hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
  1297   with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
  1298 qed (auto intro: Lcm_dvd dvd_Lcm)
  1299  
  1300 lemma Lcm_finite:
  1301   assumes "finite A"
  1302   shows "Lcm A = Finite_Set.fold lcm 1 A"
  1303   by (induct rule: finite.induct[OF \<open>finite A\<close>])
  1304     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
  1305 
  1306 lemma Lcm_set [code_unfold]:
  1307   "Lcm (set xs) = fold lcm xs 1"
  1308   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
  1309 
  1310 lemma Lcm_singleton [simp]:
  1311   "Lcm {a} = a div normalization_factor a"
  1312   by simp
  1313 
  1314 lemma Lcm_2 [simp]:
  1315   "Lcm {a,b} = lcm a b"
  1316   by (simp only: Lcm_insert Lcm_empty lcm_1_right)
  1317     (cases "b = 0", simp, rule lcm_div_unit2, simp)
  1318 
  1319 lemma Lcm_coprime:
  1320   assumes "finite A" and "A \<noteq> {}" 
  1321   assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
  1322   shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
  1323 using assms proof (induct rule: finite_ne_induct)
  1324   case (insert a A)
  1325   have "Lcm (insert a A) = lcm a (Lcm A)" by simp
  1326   also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast
  1327   also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
  1328   also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
  1329   with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))"
  1330     by (simp add: lcm_coprime)
  1331   finally show ?case .
  1332 qed simp
  1333       
  1334 lemma Lcm_coprime':
  1335   "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
  1336     \<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
  1337   by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
  1338 
  1339 lemma Gcd_Lcm:
  1340   "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
  1341   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
  1342 
  1343 lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
  1344   and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A"
  1345   and normalization_factor_Gcd [simp]: 
  1346     "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
  1347 proof -
  1348   fix a assume "a \<in> A"
  1349   hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast
  1350   then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
  1351 next
  1352   fix g' assume "\<forall>a\<in>A. g' dvd a"
  1353   hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
  1354   then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
  1355 next
  1356   show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
  1357     by (simp add: Gcd_Lcm)
  1358 qed
  1359 
  1360 lemma GcdI:
  1361   "(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow>
  1362     normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
  1363   by (intro normed_associated_imp_eq)
  1364     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
  1365 
  1366 lemma Lcm_Gcd:
  1367   "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
  1368   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
  1369 
  1370 lemma Gcd_0_iff:
  1371   "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
  1372   apply (rule iffI)
  1373   apply (rule subsetI, drule Gcd_dvd, simp)
  1374   apply (auto intro: GcdI[symmetric])
  1375   done
  1376 
  1377 lemma Gcd_empty [simp]:
  1378   "Gcd {} = 0"
  1379   by (simp add: Gcd_0_iff)
  1380 
  1381 lemma Gcd_1:
  1382   "1 \<in> A \<Longrightarrow> Gcd A = 1"
  1383   by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
  1384 
  1385 lemma Gcd_insert [simp]:
  1386   "Gcd (insert a A) = gcd a (Gcd A)"
  1387 proof (rule gcdI)
  1388   fix l assume "l dvd a" and "l dvd Gcd A"
  1389   hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
  1390   with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
  1391 qed auto
  1392 
  1393 lemma Gcd_finite:
  1394   assumes "finite A"
  1395   shows "Gcd A = Finite_Set.fold gcd 0 A"
  1396   by (induct rule: finite.induct[OF \<open>finite A\<close>])
  1397     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
  1398 
  1399 lemma Gcd_set [code_unfold]:
  1400   "Gcd (set xs) = fold gcd xs 0"
  1401   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
  1402 
  1403 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a"
  1404   by (simp add: gcd_0)
  1405 
  1406 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
  1407   by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
  1408 
  1409 subclass semiring_gcd
  1410   by unfold_locales (simp_all add: gcd_greatest_iff)
  1411   
  1412 end
  1413 
  1414 text \<open>
  1415   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
  1416   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
  1417 \<close>
  1418 
  1419 class euclidean_ring = euclidean_semiring + idom
  1420 begin
  1421 
  1422 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
  1423   "euclid_ext a b = 
  1424      (if b = 0 then 
  1425         let c = 1 div normalization_factor a in (c, 0, a * c)
  1426       else if b dvd a then
  1427         let c = 1 div normalization_factor b in (0, c, b * c)
  1428       else
  1429         case euclid_ext b (a mod b) of
  1430             (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
  1431   by pat_completeness simp
  1432 termination
  1433   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
  1434 
  1435 declare euclid_ext.simps [simp del]
  1436 
  1437 lemma euclid_ext_0: 
  1438   "euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)"
  1439   by (simp add: euclid_ext.simps [of a 0])
  1440 
  1441 lemma euclid_ext_left_0: 
  1442   "euclid_ext 0 a = (0, 1 div normalization_factor a, a div normalization_factor a)"
  1443   by (simp add: euclid_ext.simps [of 0 a])
  1444 
  1445 lemma euclid_ext_non_0: 
  1446   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
  1447     (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
  1448   by (cases "b dvd a")
  1449     (simp_all add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
  1450 
  1451 lemma euclid_ext_code [code]:
  1452   "euclid_ext a b = (if b = 0 then (1 div normalization_factor a, 0, a div normalization_factor a)
  1453     else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
  1454   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
  1455 
  1456 lemma euclid_ext_correct:
  1457   "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
  1458 proof (induct a b rule: gcd_eucl_induct)
  1459   case (zero a) then show ?case
  1460     by (simp add: euclid_ext_0 ac_simps)
  1461 next
  1462   case (mod a b)
  1463   obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
  1464     by (cases "euclid_ext b (a mod b)") blast
  1465   with mod have "c = s * b + t * (a mod b)" by simp
  1466   also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
  1467     by (simp add: algebra_simps) 
  1468   also have "(a div b) * b + a mod b = a" using mod_div_equality .
  1469   finally show ?case
  1470     by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
  1471 qed
  1472 
  1473 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
  1474 where
  1475   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
  1476 
  1477 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div normalization_factor a, 0)" 
  1478   by (simp add: euclid_ext'_def euclid_ext_0)
  1479 
  1480 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div normalization_factor a)" 
  1481   by (simp add: euclid_ext'_def euclid_ext_left_0)
  1482   
  1483 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
  1484   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
  1485   by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
  1486 
  1487 end
  1488 
  1489 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
  1490 begin
  1491 
  1492 subclass euclidean_ring ..
  1493 
  1494 subclass ring_gcd ..
  1495 
  1496 lemma euclid_ext_gcd [simp]:
  1497   "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
  1498   by (induct a b rule: gcd_eucl_induct)
  1499     (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
  1500 
  1501 lemma euclid_ext_gcd' [simp]:
  1502   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
  1503   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
  1504   
  1505 lemma euclid_ext'_correct:
  1506   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
  1507 proof-
  1508   obtain s t c where "euclid_ext a b = (s,t,c)"
  1509     by (cases "euclid_ext a b", blast)
  1510   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
  1511     show ?thesis unfolding euclid_ext'_def by simp
  1512 qed
  1513 
  1514 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
  1515   using euclid_ext'_correct by blast
  1516 
  1517 lemma gcd_neg1 [simp]:
  1518   "gcd (-a) b = gcd a b"
  1519   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
  1520 
  1521 lemma gcd_neg2 [simp]:
  1522   "gcd a (-b) = gcd a b"
  1523   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
  1524 
  1525 lemma gcd_neg_numeral_1 [simp]:
  1526   "gcd (- numeral n) a = gcd (numeral n) a"
  1527   by (fact gcd_neg1)
  1528 
  1529 lemma gcd_neg_numeral_2 [simp]:
  1530   "gcd a (- numeral n) = gcd a (numeral n)"
  1531   by (fact gcd_neg2)
  1532 
  1533 lemma gcd_diff1: "gcd (m - n) n = gcd m n"
  1534   by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
  1535 
  1536 lemma gcd_diff2: "gcd (n - m) n = gcd m n"
  1537   by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
  1538 
  1539 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
  1540 proof -
  1541   have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
  1542   also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
  1543   also have "\<dots> = 1" by (rule coprime_plus_one)
  1544   finally show ?thesis .
  1545 qed
  1546 
  1547 lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
  1548   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
  1549 
  1550 lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
  1551   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
  1552 
  1553 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
  1554   by (fact lcm_neg1)
  1555 
  1556 lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
  1557   by (fact lcm_neg2)
  1558 
  1559 end
  1560 
  1561 
  1562 subsection \<open>Typical instances\<close>
  1563 
  1564 instantiation nat :: euclidean_semiring
  1565 begin
  1566 
  1567 definition [simp]:
  1568   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
  1569 
  1570 definition [simp]:
  1571   "normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
  1572 
  1573 instance proof
  1574 qed simp_all
  1575 
  1576 end
  1577 
  1578 instantiation int :: euclidean_ring
  1579 begin
  1580 
  1581 definition [simp]:
  1582   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
  1583 
  1584 definition [simp]:
  1585   "normalization_factor_int = (sgn :: int \<Rightarrow> int)"
  1586 
  1587 instance proof
  1588   case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)
  1589 next
  1590   case goal3 then show ?case by (simp add: zsgn_def)
  1591 next
  1592   case goal5 then show ?case by (auto simp: zsgn_def)
  1593 next
  1594   case goal6 then show ?case by (auto split: abs_split simp: zsgn_def)
  1595 qed (auto simp: sgn_times split: abs_split)
  1596 
  1597 end
  1598 
  1599 instantiation poly :: (field) euclidean_ring
  1600 begin
  1601 
  1602 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
  1603   where "euclidean_size = (degree :: 'a poly \<Rightarrow> nat)"
  1604 
  1605 definition normalization_factor_poly :: "'a poly \<Rightarrow> 'a poly"
  1606   where "normalization_factor p = monom (coeff p (degree p)) 0"
  1607 
  1608 instance
  1609 proof (default, unfold euclidean_size_poly_def normalization_factor_poly_def)
  1610   fix p q :: "'a poly"
  1611   assume "q \<noteq> 0" and "\<not> q dvd p"
  1612   then show "degree (p mod q) < degree q"
  1613     using degree_mod_less [of q p] by (simp add: mod_eq_0_iff_dvd)
  1614 next
  1615   fix p q :: "'a poly"
  1616   assume "q \<noteq> 0"
  1617   from \<open>q \<noteq> 0\<close> show "degree p \<le> degree (p * q)"
  1618     by (rule degree_mult_right_le)
  1619   from \<open>q \<noteq> 0\<close> show "is_unit (monom (coeff q (degree q)) 0)"
  1620     by (auto intro: is_unit_monom_0)
  1621 next
  1622   fix p :: "'a poly"
  1623   show "monom (coeff p (degree p)) 0 = p" if "is_unit p"
  1624     using that by (fact is_unit_monom_trival)
  1625 next
  1626   fix p q :: "'a poly"
  1627   show "monom (coeff (p * q) (degree (p * q))) 0 =
  1628     monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
  1629     by (simp add: monom_0 coeff_degree_mult)
  1630 next
  1631   show "monom (coeff 0 (degree 0)) 0 = 0"
  1632     by simp
  1633 qed
  1634 
  1635 end
  1636 
  1637 end