src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Wed Jul 08 14:01:41 2015 +0200 (2015-07-08) changeset 60688 01488b559910 parent 60687 33dbbcb6a8a3 child 60690 a9e45c9588c3 permissions -rw-r--r--
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
     1 (* Author: Manuel Eberl *)

     2

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

     4

     5 theory Euclidean_Algorithm

     6 imports Main "~~/src/HOL/GCD" "~~/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   \end{itemize}

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

    18   for any Euclidean semiring.

    19 \<close>

    20 class euclidean_semiring = semiring_div + normalization_semidom +

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

    22   assumes mod_size_less:

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

    24   assumes size_mult_mono:

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

    26 begin

    27

    28 lemma euclidean_division:

    29   fixes a :: 'a and b :: 'a

    30   assumes "b \<noteq> 0"

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

    32     and "euclidean_size t < euclidean_size b"

    33 proof -

    34   from div_mod_equality [of a b 0]

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

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

    37 qed

    38

    39 lemma dvd_euclidean_size_eq_imp_dvd:

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

    41   shows "a dvd b"

    42 proof (rule ccontr)

    43   assume "\<not> a dvd b"

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

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

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

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

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

    49       using size_mult_mono by force

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

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

    52       using mod_size_less by blast

    53   ultimately show False using size_eq by simp

    54 qed

    55

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

    57 where

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

    59   by pat_completeness simp

    60 termination

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

    62

    63 declare gcd_eucl.simps [simp del]

    64

    65 lemma gcd_eucl_induct [case_names zero mod]:

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

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

    68   shows "P a b"

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

    70   case ("1" a b)

    71   show ?case

    72   proof (cases "b = 0")

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

    74   next

    75     case False

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

    77       by (rule "1.hyps")

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

    79       by (blast intro: H2)

    80   qed

    81 qed

    82

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

    84 where

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

    86

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

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

    89   for infinite sets as well\<close>

    90 where

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

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

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

    94        in normalize l

    95       else 0)"

    96

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

    98 where

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

   100

   101 lemma gcd_eucl_0:

   102   "gcd_eucl a 0 = normalize a"

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

   104

   105 lemma gcd_eucl_0_left:

   106   "gcd_eucl 0 a = normalize a"

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

   108

   109 lemma gcd_eucl_non_0:

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

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

   112

   113 end

   114

   115 class euclidean_ring = euclidean_semiring + idom

   116 begin

   117

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

   119   "euclid_ext a b =

   120      (if b = 0 then

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

   122       else

   123         case euclid_ext b (a mod b) of

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

   125   by pat_completeness simp

   126 termination

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

   128

   129 declare euclid_ext.simps [simp del]

   130

   131 lemma euclid_ext_0:

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

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

   134

   135 lemma euclid_ext_left_0:

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

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

   138

   139 lemma euclid_ext_non_0:

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

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

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

   143

   144 lemma euclid_ext_code [code]:

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

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

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

   148

   149 lemma euclid_ext_correct:

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

   151 proof (induct a b rule: gcd_eucl_induct)

   152   case (zero a) then show ?case

   153     by (simp add: euclid_ext_0 ac_simps)

   154 next

   155   case (mod a b)

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

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

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

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

   160     by (simp add: algebra_simps)

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

   162   finally show ?case

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

   164 qed

   165

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

   167 where

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

   169

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

   171   by (simp add: euclid_ext'_def euclid_ext_0)

   172

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

   174   by (simp add: euclid_ext'_def euclid_ext_left_0)

   175

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

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

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

   179

   180 end

   181

   182 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +

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

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

   185 begin

   186

   187 lemma gcd_0_left:

   188   "gcd 0 a = normalize a"

   189   unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)

   190

   191 lemma gcd_0:

   192   "gcd a 0 = normalize a"

   193   unfolding gcd_gcd_eucl by (fact gcd_eucl_0)

   194

   195 lemma gcd_non_0:

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

   197   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)

   198

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

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

   201   by (induct a b rule: gcd_eucl_induct)

   202     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)

   203

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

   205   by (rule dvd_trans, assumption, rule gcd_dvd1)

   206

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

   208   by (rule dvd_trans, assumption, rule gcd_dvd2)

   209

   210 lemma gcd_greatest:

   211   fixes k a b :: 'a

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

   213 proof (induct a b rule: gcd_eucl_induct)

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

   215 next

   216   case (mod a b)

   217   then show ?case

   218     by (simp add: gcd_non_0 dvd_mod_iff)

   219 qed

   220

   221 lemma dvd_gcd_iff:

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

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

   224

   225 lemmas gcd_greatest_iff = dvd_gcd_iff

   226

   227 lemma gcd_zero [simp]:

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

   229   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+

   230

   231 lemma normalize_gcd [simp]:

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

   233   by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_0 gcd_non_0)

   234

   235 lemma gcdI:

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

   237     and "normalize c = c"

   238   shows "c = gcd a b"

   239   by (rule associated_eqI) (auto simp: assms intro: gcd_greatest)

   240

   241 sublocale gcd!: abel_semigroup gcd

   242 proof

   243   fix a b c

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

   245   proof (rule gcdI)

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

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

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

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

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

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

   252       by (rule gcd_greatest)

   253     show "normalize (gcd (gcd a b) c) = gcd (gcd a b) c"

   254       by auto

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

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

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

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

   259       by (intro gcd_greatest)

   260   qed

   261 next

   262   fix a b

   263   show "gcd a b = gcd b a"

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

   265 qed

   266

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

   268     normalize d = d \<and>

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

   270   by rule (auto intro: gcdI simp: gcd_greatest)

   271

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

   273   using mult_dvd_mono [of 1] by auto

   274

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

   276   by (rule sym, rule gcdI, simp_all)

   277

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

   279   by (rule sym, rule gcdI, simp_all)

   280

   281 lemma gcd_proj2_if_dvd:

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

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

   284

   285 lemma gcd_proj1_if_dvd:

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

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

   288

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

   290 proof

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

   292   show "m dvd n"

   293   proof (cases "m = 0")

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

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

   296       by (simp add: unit_eq_div2)

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

   298   qed (insert A, simp)

   299 next

   300   assume "m dvd n"

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

   302 qed

   303

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

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

   306

   307 lemma gcd_mod1 [simp]:

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

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

   310

   311 lemma gcd_mod2 [simp]:

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

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

   314

   315 lemma gcd_mult_distrib':

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

   317 proof (cases "c = 0")

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

   319 next

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

   321   show ?thesis

   322   proof (induct a b rule: gcd_eucl_induct)

   323     case (zero a) show ?case

   324     proof (cases "a = 0")

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

   326     next

   327       case False

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

   329     qed

   330     case (mod a b)

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

   332   qed

   333 qed

   334

   335 lemma gcd_mult_distrib:

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

   337 proof-

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

   339     by (simp add: gcd_mult_distrib')

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

   341     by simp

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

   343     by (simp only: ac_simps)

   344   then show ?thesis

   345     by simp

   346 qed

   347

   348 lemma euclidean_size_gcd_le1 [simp]:

   349   assumes "a \<noteq> 0"

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

   351 proof -

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

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

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

   355 qed

   356

   357 lemma euclidean_size_gcd_le2 [simp]:

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

   359   by (subst gcd.commute, rule euclidean_size_gcd_le1)

   360

   361 lemma euclidean_size_gcd_less1:

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

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

   364 proof (rule ccontr)

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

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

   367     by (intro le_antisym, simp_all)

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

   369   hence "a dvd b" using dvd_gcd_D2 by blast

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

   371 qed

   372

   373 lemma euclidean_size_gcd_less2:

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

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

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

   377

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

   379   apply (rule gcdI)

   380   apply simp_all

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

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

   383   done

   384

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

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

   387

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

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

   390

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

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

   393

   394 lemma normalize_gcd_left [simp]:

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

   396 proof (cases "a = 0")

   397   case True then show ?thesis

   398     by simp

   399 next

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

   401     by simp

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

   403     by simp

   404   ultimately show ?thesis

   405     by (simp only: gcd_div_unit1)

   406 qed

   407

   408 lemma normalize_gcd_right [simp]:

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

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

   411

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

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

   414

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

   416   apply (rule gcdI)

   417   apply (simp add: ac_simps)

   418   apply (rule gcd_dvd2)

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

   420   apply simp

   421   done

   422

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

   424   apply (rule gcdI)

   425   apply simp

   426   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)

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

   428   apply simp

   429   done

   430

   431 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"

   432 proof

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

   434     by (simp add: fun_eq_iff ac_simps)

   435 next

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

   437     by (simp add: fun_eq_iff gcd_left_idem)

   438 qed

   439

   440 lemma coprime_dvd_mult:

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

   442   shows "c dvd a"

   443 proof -

   444   let ?nf = "unit_factor"

   445   from assms gcd_mult_distrib [of a c b]

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

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

   448 qed

   449

   450 lemma coprime_dvd_mult_iff:

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

   452   by (rule, rule coprime_dvd_mult, simp_all)

   453

   454 lemma gcd_dvd_antisym:

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

   456 proof (rule gcdI)

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

   458   have "gcd c d dvd c" by simp

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

   460   have "gcd c d dvd d" by simp

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

   462   show "normalize (gcd a b) = gcd a b"

   463     by simp

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

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

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

   467 qed

   468

   469 lemma gcd_mult_cancel:

   470   assumes "gcd k n = 1"

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

   472 proof (rule gcd_dvd_antisym)

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

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

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

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

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

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

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

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

   481 qed

   482

   483 lemma coprime_crossproduct:

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

   485   shows "normalize (a * c) = normalize (b * d) \<longleftrightarrow> normalize a  = normalize b \<and> normalize c = normalize d"

   486     (is "?lhs \<longleftrightarrow> ?rhs")

   487 proof

   488   assume ?rhs

   489   then have "a dvd b" "b dvd a" "c dvd d" "d dvd c" by (simp_all add: associated_iff_dvd)

   490   then have "a * c dvd b * d" "b * d dvd a * c" by (fast intro: mult_dvd_mono)+

   491   then show ?lhs by (simp add: associated_iff_dvd)

   492 next

   493   assume ?lhs

   494   then have dvd: "a * c dvd b * d" "b * d dvd a * c" by (simp_all add: associated_iff_dvd)

   495   then have "a dvd b * d" by (metis dvd_mult_left)

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

   497   moreover from dvd have "b dvd a * c" by (metis dvd_mult_left)

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

   499   moreover from dvd have "c dvd d * b"

   500     by (auto dest: dvd_mult_right simp add: ac_simps)

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

   502   moreover from dvd have "d dvd c * a"

   503     by (auto dest: dvd_mult_right simp add: ac_simps)

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

   505   ultimately show ?rhs by (simp add: associated_iff_dvd)

   506 qed

   507

   508 lemma gcd_add1 [simp]:

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

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

   511

   512 lemma gcd_add2 [simp]:

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

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

   515

   516 lemma gcd_add_mult:

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

   518 proof -

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

   520     by (fact gcd_mod2)

   521   then show ?thesis by simp

   522 qed

   523

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

   525   by (rule sym, rule gcdI, simp_all)

   526

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

   528   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)

   529

   530 lemma div_gcd_coprime:

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

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

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

   534   shows "gcd a' b' = 1"

   535 proof (rule coprimeI)

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

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

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

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

   540     by (simp_all only: ac_simps)

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

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

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

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

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

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

   547   ultimately have "1 = l * u"

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

   549   then show "l dvd 1" ..

   550 qed

   551

   552 lemma coprime_mult:

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

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

   555   apply (subst gcd.commute)

   556   using da apply (subst gcd_mult_cancel)

   557   apply (subst gcd.commute, assumption)

   558   apply (subst gcd.commute, rule db)

   559   done

   560

   561 lemma coprime_lmult:

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

   563   shows "gcd d a = 1"

   564 proof (rule coprimeI)

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

   566   hence "l dvd a * b" by simp

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

   568 qed

   569

   570 lemma coprime_rmult:

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

   572   shows "gcd d b = 1"

   573 proof (rule coprimeI)

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

   575   hence "l dvd a * b" by simp

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

   577 qed

   578

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

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

   581

   582 lemma gcd_coprime:

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

   584   shows "gcd a' b' = 1"

   585 proof -

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

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

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

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

   590   finally show ?thesis .

   591 qed

   592

   593 lemma coprime_power:

   594   assumes "0 < n"

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

   596 using assms proof (induct n)

   597   case (Suc n) then show ?case

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

   599 qed simp

   600

   601 lemma gcd_coprime_exists:

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

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

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

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

   606   apply (insert nz, auto intro: div_gcd_coprime)

   607   done

   608

   609 lemma coprime_exp:

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

   611   by (induct n, simp_all add: coprime_mult)

   612

   613 lemma coprime_exp2 [intro]:

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

   615   apply (rule coprime_exp)

   616   apply (subst gcd.commute)

   617   apply (rule coprime_exp)

   618   apply (subst gcd.commute)

   619   apply assumption

   620   done

   621

   622 lemma lcm_gcd:

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

   624   by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)

   625

   626 subclass semiring_gcd

   627   apply standard

   628   using gcd_right_idem

   629   apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)

   630   done

   631

   632 lemma gcd_exp:

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

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

   635   case True

   636   then show ?thesis by (cases n) simp_all

   637 next

   638   case False

   639   then have "1 = gcd ((a div gcd a b) ^ n) ((b div gcd a b) ^ n)"

   640     using div_gcd_coprime by (subst sym) (auto simp: div_gcd_coprime)

   641   then have "gcd a b ^ n = gcd a b ^ n * ..." by simp

   642   also note gcd_mult_distrib

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

   644     using False by (auto simp add: unit_factor_power unit_factor_gcd)

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

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

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

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

   649   finally show ?thesis by simp

   650 qed

   651

   652 lemma coprime_common_divisor:

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

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

   655   apply simp

   656   apply (erule (1) gcd_greatest)

   657   done

   658

   659 lemma division_decomp:

   660   assumes dc: "a dvd b * c"

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

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

   663   assume "gcd a b = 0"

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

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

   666   then show ?thesis by blast

   667 next

   668   let ?d = "gcd a b"

   669   assume "?d \<noteq> 0"

   670   from gcd_coprime_exists[OF this]

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

   672     by blast

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

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

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

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

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

   678   with coprime_dvd_mult[OF ab'(3)]

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

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

   681   then show ?thesis by blast

   682 qed

   683

   684 lemma pow_divs_pow:

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

   686   shows "a dvd b"

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

   688   assume "gcd a b = 0"

   689   then show ?thesis by simp

   690 next

   691   let ?d = "gcd a b"

   692   assume "?d \<noteq> 0"

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

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

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

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

   697     by blast

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

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

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

   701     by (simp only: power_mult_distrib ac_simps)

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

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

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

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

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

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

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

   709 qed

   710

   711 lemma pow_divs_eq [simp]:

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

   713   by (auto intro: pow_divs_pow dvd_power_same)

   714

   715 lemma divs_mult:

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

   717   shows "m * n dvd r"

   718 proof -

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

   720     unfolding dvd_def by blast

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

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

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

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

   725   then show ?thesis unfolding dvd_def by blast

   726 qed

   727

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

   729   by (subst add_commute, simp)

   730

   731 lemma setprod_coprime [rule_format]:

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

   733   apply (cases "finite A")

   734   apply (induct set: finite)

   735   apply (auto simp add: gcd_mult_cancel)

   736   done

   737

   738 lemma coprime_divisors:

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

   740   shows "gcd d e = 1"

   741 proof -

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

   743     unfolding dvd_def by blast

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

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

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

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

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

   749 qed

   750

   751 lemma invertible_coprime:

   752   assumes "a * b mod m = 1"

   753   shows "coprime a m"

   754 proof -

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

   756     by simp

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

   758     by simp

   759   then have "coprime m a"

   760     by (rule coprime_lmult)

   761   then show ?thesis

   762     by (simp add: ac_simps)

   763 qed

   764

   765 lemma lcm_gcd_prod:

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

   767   by (simp add: lcm_gcd)

   768

   769 lemma lcm_zero:

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

   771   by (fact lcm_eq_0_iff)

   772

   773 lemmas lcm_0_iff = lcm_zero

   774

   775 lemma gcd_lcm:

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

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

   778 proof -

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

   780     by (fact lcm_gcd_prod)

   781   with assms show ?thesis

   782     by (metis nonzero_mult_divide_cancel_left)

   783 qed

   784

   785 declare unit_factor_lcm [simp]

   786

   787 lemma lcmI:

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

   789     and "normalize c = c"

   790   shows "c = lcm a b"

   791   by (rule associated_eqI) (auto simp: assms intro: lcm_least)

   792

   793 sublocale lcm!: abel_semigroup lcm ..

   794

   795 lemma dvd_lcm_D1:

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

   797   by (rule dvd_trans, rule lcm_dvd1, assumption)

   798

   799 lemma dvd_lcm_D2:

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

   801   by (rule dvd_trans, rule lcm_dvd2, assumption)

   802

   803 lemma gcd_dvd_lcm [simp]:

   804   "gcd a b dvd lcm a b"

   805   by (metis dvd_trans gcd_dvd2 lcm_dvd2)

   806

   807 lemma lcm_1_iff:

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

   809 proof

   810   assume "lcm a b = 1"

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

   812 next

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

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

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

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

   817     by (blast intro: sym is_unit_unit_factor)

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

   819     by auto

   820   finally show "lcm a b = 1" .

   821 qed

   822

   823 lemma lcm_0:

   824   "lcm a 0 = 0"

   825   by (fact lcm_0_right)

   826

   827 lemma lcm_unique:

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

   829   normalize d = d \<and>

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

   831   by rule (auto intro: lcmI simp: lcm_least lcm_zero)

   832

   833 lemma dvd_lcm_I1 [simp]:

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

   835   by (metis lcm_dvd1 dvd_trans)

   836

   837 lemma dvd_lcm_I2 [simp]:

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

   839   by (metis lcm_dvd2 dvd_trans)

   840

   841 lemma lcm_coprime:

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

   843   by (subst lcm_gcd) simp

   844

   845 lemma lcm_proj1_if_dvd:

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

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

   848

   849 lemma lcm_proj2_if_dvd:

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

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

   852

   853 lemma lcm_proj1_iff:

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

   855 proof

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

   857   show "n dvd m"

   858   proof (cases "m = 0")

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

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

   861       by (simp add: unit_eq_div2)

   862     show ?thesis by (subst B, simp)

   863   qed simp

   864 next

   865   assume "n dvd m"

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

   867 qed

   868

   869 lemma lcm_proj2_iff:

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

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

   872

   873 lemma euclidean_size_lcm_le1:

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

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

   876 proof -

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

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

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

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

   881 qed

   882

   883 lemma euclidean_size_lcm_le2:

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

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

   886

   887 lemma euclidean_size_lcm_less1:

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

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

   890 proof (rule ccontr)

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

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

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

   894     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)

   895   with assms have "lcm a b dvd a"

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

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

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

   899 qed

   900

   901 lemma euclidean_size_lcm_less2:

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

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

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

   905

   906 lemma lcm_mult_unit1:

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

   908   apply (rule lcmI)

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

   910   apply (rule lcm_dvd2)

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

   912   apply simp

   913   done

   914

   915 lemma lcm_mult_unit2:

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

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

   918

   919 lemma lcm_div_unit1:

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

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

   922

   923 lemma lcm_div_unit2:

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

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

   926

   927 lemma normalize_lcm_left [simp]:

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

   929 proof (cases "a = 0")

   930   case True then show ?thesis

   931     by simp

   932 next

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

   934     by simp

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

   936     by simp

   937   ultimately show ?thesis

   938     by (simp only: lcm_div_unit1)

   939 qed

   940

   941 lemma normalize_lcm_right [simp]:

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

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

   944

   945 lemma lcm_left_idem:

   946   "lcm a (lcm a b) = lcm a b"

   947   apply (rule lcmI)

   948   apply simp

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

   950   apply (rule lcm_least, assumption)

   951   apply (erule (1) lcm_least)

   952   apply (auto simp: lcm_zero)

   953   done

   954

   955 lemma lcm_right_idem:

   956   "lcm (lcm a b) b = lcm a b"

   957   apply (rule lcmI)

   958   apply (subst lcm.assoc, rule lcm_dvd1)

   959   apply (rule lcm_dvd2)

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

   961   apply (auto simp: lcm_zero)

   962   done

   963

   964 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"

   965 proof

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

   967     by (simp add: fun_eq_iff ac_simps)

   968 next

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

   970     by (intro ext, simp add: lcm_left_idem)

   971 qed

   972

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

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

   975   and unit_factor_Lcm [simp]:

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

   977 proof -

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

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

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

   981     case False

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

   983     with False show ?thesis by auto

   984   next

   985     case True

   986     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

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

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

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

   990       apply (subst n_def)

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

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

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

   994       done

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

   996       unfolding l_def by simp_all

   997     {

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

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

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

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

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

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

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

  1005       proof -

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

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

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

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

  1010           by (rule size_mult_mono)

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

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

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

  1014       qed

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

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

  1017       with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"

  1018         using dvd_euclidean_size_eq_imp_dvd by auto

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

  1020     }

  1021

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

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

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

  1025         unit_factor (normalize l) =

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

  1027       by (auto simp: unit_simps)

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

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

  1030     finally show ?thesis .

  1031   qed

  1032   note A = this

  1033

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

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

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

  1037 qed

  1038

  1039 lemma normalize_Lcm [simp]:

  1040   "normalize (Lcm A) = Lcm A"

  1041 proof (cases "Lcm A = 0")

  1042   case True then show ?thesis by simp

  1043 next

  1044   case False

  1045   have "unit_factor (Lcm A) * normalize (Lcm A) = Lcm A"

  1046     by (fact unit_factor_mult_normalize)

  1047   with False show ?thesis by simp

  1048 qed

  1049

  1050 lemma LcmI:

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

  1052     and "normalize b = b" shows "b = Lcm A"

  1053   by (rule associated_eqI) (auto simp: assms intro: Lcm_least)

  1054

  1055 lemma Lcm_subset:

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

  1057   by (blast intro: Lcm_least dvd_Lcm)

  1058

  1059 lemma Lcm_Un:

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

  1061   apply (rule lcmI)

  1062   apply (blast intro: Lcm_subset)

  1063   apply (blast intro: Lcm_subset)

  1064   apply (intro Lcm_least ballI, elim UnE)

  1065   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1066   apply (rule dvd_trans, erule dvd_Lcm, assumption)

  1067   apply simp

  1068   done

  1069

  1070 lemma Lcm_1_iff:

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

  1072 proof

  1073   assume "Lcm A = 1"

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

  1075 qed (rule LcmI [symmetric], auto)

  1076

  1077 lemma Lcm_no_units:

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

  1079 proof -

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

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

  1082     by (simp add: Lcm_Un [symmetric])

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

  1084   finally show ?thesis by simp

  1085 qed

  1086

  1087 lemma Lcm_empty [simp]:

  1088   "Lcm {} = 1"

  1089   by (simp add: Lcm_1_iff)

  1090

  1091 lemma Lcm_eq_0 [simp]:

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

  1093   by (drule dvd_Lcm) simp

  1094

  1095 lemma Lcm0_iff':

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

  1097 proof

  1098   assume "Lcm A = 0"

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

  1100   proof

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

  1102     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

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

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

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

  1106       apply (subst n_def)

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

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

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

  1110       done

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

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

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

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

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

  1116   qed

  1117 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)

  1118

  1119 lemma Lcm0_iff [simp]:

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

  1121 proof -

  1122   assume "finite A"

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

  1124   moreover {

  1125     assume "0 \<notin> A"

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

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

  1128       apply simp

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

  1130       apply (rule no_zero_divisors)

  1131       apply blast+

  1132       done

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

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

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

  1136   }

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

  1138 qed

  1139

  1140 lemma Lcm_no_multiple:

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

  1142 proof -

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

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

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

  1146 qed

  1147

  1148 lemma Lcm_insert [simp]:

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

  1150 proof (rule lcmI)

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

  1152   then have "\<forall>a\<in>A. a dvd l" by (auto intro: dvd_trans [of _ "Lcm A" l])

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

  1154 qed (auto intro: Lcm_least dvd_Lcm)

  1155

  1156 lemma Lcm_finite:

  1157   assumes "finite A"

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

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

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

  1161

  1162 lemma Lcm_set [code_unfold]:

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

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

  1165

  1166 lemma Lcm_singleton [simp]:

  1167   "Lcm {a} = normalize a"

  1168   by simp

  1169

  1170 lemma Lcm_2 [simp]:

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

  1172   by simp

  1173

  1174 lemma Lcm_coprime:

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

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

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

  1178 using assms proof (induct rule: finite_ne_induct)

  1179   case (insert a A)

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

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

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

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

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

  1185     by (simp add: lcm_coprime)

  1186   finally show ?case .

  1187 qed simp

  1188

  1189 lemma Lcm_coprime':

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

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

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

  1193

  1194 lemma Gcd_Lcm:

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

  1196   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)

  1197

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

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

  1200   and unit_factor_Gcd [simp]:

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

  1202 proof -

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

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

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

  1206 next

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

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

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

  1210 next

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

  1212     by (simp add: Gcd_Lcm)

  1213 qed

  1214

  1215 lemma normalize_Gcd [simp]:

  1216   "normalize (Gcd A) = Gcd A"

  1217 proof (cases "Gcd A = 0")

  1218   case True then show ?thesis by simp

  1219 next

  1220   case False

  1221   have "unit_factor (Gcd A) * normalize (Gcd A) = Gcd A"

  1222     by (fact unit_factor_mult_normalize)

  1223   with False show ?thesis by simp

  1224 qed

  1225

  1226 subclass semiring_Gcd

  1227   by standard (simp_all add: Gcd_greatest)

  1228

  1229 lemma GcdI:

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

  1231     and "normalize b = b"

  1232   shows "b = Gcd A"

  1233   by (rule associated_eqI) (auto simp: assms intro: Gcd_greatest)

  1234

  1235 lemma Lcm_Gcd:

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

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

  1238

  1239 subclass semiring_Lcm

  1240   by standard (simp add: Lcm_Gcd)

  1241

  1242 lemma Gcd_1:

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

  1244   by (auto intro!: Gcd_eq_1_I)

  1245

  1246 lemma Gcd_finite:

  1247   assumes "finite A"

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

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

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

  1251

  1252 lemma Gcd_set [code_unfold]:

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

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

  1255

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

  1257   by simp

  1258

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

  1260   by simp

  1261

  1262 end

  1263

  1264 text \<open>

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

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

  1267 \<close>

  1268

  1269 class euclidean_ring_gcd = euclidean_semiring_gcd + idom

  1270 begin

  1271

  1272 subclass euclidean_ring ..

  1273

  1274 subclass ring_gcd ..

  1275

  1276 lemma euclid_ext_gcd [simp]:

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

  1278   by (induct a b rule: gcd_eucl_induct)

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

  1280

  1281 lemma euclid_ext_gcd' [simp]:

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

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

  1284

  1285 lemma euclid_ext'_correct:

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

  1287 proof-

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

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

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

  1291     show ?thesis unfolding euclid_ext'_def by simp

  1292 qed

  1293

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

  1295   using euclid_ext'_correct by blast

  1296

  1297 lemma gcd_neg1 [simp]:

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

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

  1300

  1301 lemma gcd_neg2 [simp]:

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

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

  1304

  1305 lemma gcd_neg_numeral_1 [simp]:

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

  1307   by (fact gcd_neg1)

  1308

  1309 lemma gcd_neg_numeral_2 [simp]:

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

  1311   by (fact gcd_neg2)

  1312

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

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

  1315

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

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

  1318

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

  1320 proof -

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

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

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

  1324   finally show ?thesis .

  1325 qed

  1326

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

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

  1329

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

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

  1332

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

  1334   by (fact lcm_neg1)

  1335

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

  1337   by (fact lcm_neg2)

  1338

  1339 end

  1340

  1341

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

  1343

  1344 instantiation nat :: euclidean_semiring

  1345 begin

  1346

  1347 definition [simp]:

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

  1349

  1350 instance proof

  1351 qed simp_all

  1352

  1353 end

  1354

  1355 instantiation int :: euclidean_ring

  1356 begin

  1357

  1358 definition [simp]:

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

  1360

  1361 instance

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

  1363

  1364 end

  1365

  1366 instantiation poly :: (field) euclidean_ring

  1367 begin

  1368

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

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

  1371

  1372 lemma euclidenan_size_poly_minus_one_degree [simp]:

  1373   "euclidean_size p - 1 = degree p"

  1374   by (simp add: euclidean_size_poly_def)

  1375

  1376 lemma euclidean_size_poly_0 [simp]:

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

  1378   by (simp add: euclidean_size_poly_def)

  1379

  1380 lemma euclidean_size_poly_not_0 [simp]:

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

  1382   by (simp add: euclidean_size_poly_def)

  1383

  1384 instance

  1385 proof

  1386   fix p q :: "'a poly"

  1387   assume "q \<noteq> 0"

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

  1389     by (rule degree_mod_less [of q p])

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

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

  1392 next

  1393   fix p q :: "'a poly"

  1394   assume "q \<noteq> 0"

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

  1396     by (rule degree_mult_right_le)

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

  1398     by (cases "p = 0") simp_all

  1399 qed

  1400

  1401 end

  1402

  1403 (*instance nat :: euclidean_semiring_gcd

  1404 proof (standard, auto intro!: ext)

  1405   fix m n :: nat

  1406   show *: "gcd m n = gcd_eucl m n"

  1407   proof (induct m n rule: gcd_eucl_induct)

  1408     case zero then show ?case by (simp add: gcd_eucl_0)

  1409   next

  1410     case (mod m n)

  1411     with gcd_eucl_non_0 [of n m, symmetric]

  1412     show ?case by (simp add: gcd_non_0_nat)

  1413   qed

  1414   show "lcm m n = lcm_eucl m n"

  1415     by (simp add: lcm_eucl_def lcm_gcd * [symmetric] lcm_nat_def)

  1416 qed*)

  1417

  1418 end