src/HOL/Algebra/IntRing.thy
author wenzelm
Wed Mar 05 21:51:30 2014 +0100 (2014-03-05)
changeset 55926 3ef14caf5637
parent 55242 413ec965f95d
child 55991 3fa6e6c81788
permissions -rw-r--r--
more symbols;
     1 (*  Title:      HOL/Algebra/IntRing.thy
     2     Author:     Stephan Hohe, TU Muenchen
     3     Author:     Clemens Ballarin
     4 *)
     5 
     6 theory IntRing
     7 imports QuotRing Lattice Int "~~/src/HOL/Number_Theory/Primes"
     8 begin
     9 
    10 section {* The Ring of Integers *}
    11 
    12 subsection {* Some properties of @{typ int} *}
    13 
    14 lemma dvds_eq_abseq:
    15   "(l dvd k \<and> k dvd l) = (abs l = abs (k::int))"
    16 apply rule
    17  apply (simp add: zdvd_antisym_abs)
    18 apply (simp add: dvd_if_abs_eq)
    19 done
    20 
    21 
    22 subsection {* @{text "\<Z>"}: The Set of Integers as Algebraic Structure *}
    23 
    24 abbreviation
    25   int_ring :: "int ring" ("\<Z>") where
    26   "int_ring == \<lparr>carrier = UNIV, mult = op *, one = 1, zero = 0, add = op +\<rparr>"
    27 
    28 lemma int_Zcarr [intro!, simp]:
    29   "k \<in> carrier \<Z>"
    30   by simp
    31 
    32 lemma int_is_cring:
    33   "cring \<Z>"
    34 apply (rule cringI)
    35   apply (rule abelian_groupI, simp_all)
    36   defer 1
    37   apply (rule comm_monoidI, simp_all)
    38  apply (rule distrib_right)
    39 apply (fast intro: left_minus)
    40 done
    41 
    42 (*
    43 lemma int_is_domain:
    44   "domain \<Z>"
    45 apply (intro domain.intro domain_axioms.intro)
    46   apply (rule int_is_cring)
    47  apply (unfold int_ring_def, simp+)
    48 done
    49 *)
    50 
    51 
    52 subsection {* Interpretations *}
    53 
    54 text {* Since definitions of derived operations are global, their
    55   interpretation needs to be done as early as possible --- that is,
    56   with as few assumptions as possible. *}
    57 
    58 interpretation int: monoid \<Z>
    59   where "carrier \<Z> = UNIV"
    60     and "mult \<Z> x y = x * y"
    61     and "one \<Z> = 1"
    62     and "pow \<Z> x n = x^n"
    63 proof -
    64   -- "Specification"
    65   show "monoid \<Z>" by default auto
    66   then interpret int: monoid \<Z> .
    67 
    68   -- "Carrier"
    69   show "carrier \<Z> = UNIV" by simp
    70 
    71   -- "Operations"
    72   { fix x y show "mult \<Z> x y = x * y" by simp }
    73   note mult = this
    74   show one: "one \<Z> = 1" by simp
    75   show "pow \<Z> x n = x^n" by (induct n) simp_all
    76 qed
    77 
    78 interpretation int: comm_monoid \<Z>
    79   where "finprod \<Z> f A = (if finite A then setprod f A else undefined)"
    80 proof -
    81   -- "Specification"
    82   show "comm_monoid \<Z>" by default auto
    83   then interpret int: comm_monoid \<Z> .
    84 
    85   -- "Operations"
    86   { fix x y have "mult \<Z> x y = x * y" by simp }
    87   note mult = this
    88   have one: "one \<Z> = 1" by simp
    89   show "finprod \<Z> f A = (if finite A then setprod f A else undefined)"
    90   proof (cases "finite A")
    91     case True then show ?thesis proof induct
    92       case empty show ?case by (simp add: one)
    93     next
    94       case insert then show ?case by (simp add: Pi_def mult)
    95     qed
    96   next
    97     case False then show ?thesis by (simp add: finprod_def)
    98   qed
    99 qed
   100 
   101 interpretation int: abelian_monoid \<Z>
   102   where int_carrier_eq: "carrier \<Z> = UNIV"
   103     and int_zero_eq: "zero \<Z> = 0"
   104     and int_add_eq: "add \<Z> x y = x + y"
   105     and int_finsum_eq: "finsum \<Z> f A = (if finite A then setsum f A else undefined)"
   106 proof -
   107   -- "Specification"
   108   show "abelian_monoid \<Z>" by default auto
   109   then interpret int: abelian_monoid \<Z> .
   110 
   111   -- "Carrier"
   112   show "carrier \<Z> = UNIV" by simp
   113 
   114   -- "Operations"
   115   { fix x y show "add \<Z> x y = x + y" by simp }
   116   note add = this
   117   show zero: "zero \<Z> = 0" by simp
   118   show "finsum \<Z> f A = (if finite A then setsum f A else undefined)"
   119   proof (cases "finite A")
   120     case True then show ?thesis proof induct
   121       case empty show ?case by (simp add: zero)
   122     next
   123       case insert then show ?case by (simp add: Pi_def add)
   124     qed
   125   next
   126     case False then show ?thesis by (simp add: finsum_def finprod_def)
   127   qed
   128 qed
   129 
   130 interpretation int: abelian_group \<Z>
   131   (* The equations from the interpretation of abelian_monoid need to be repeated.
   132      Since the morphisms through which the abelian structures are interpreted are
   133      not the identity, the equations of these interpretations are not inherited. *)
   134   (* FIXME *)
   135   where "carrier \<Z> = UNIV"
   136     and "zero \<Z> = 0"
   137     and "add \<Z> x y = x + y"
   138     and "finsum \<Z> f A = (if finite A then setsum f A else undefined)"
   139     and int_a_inv_eq: "a_inv \<Z> x = - x"
   140     and int_a_minus_eq: "a_minus \<Z> x y = x - y"
   141 proof -
   142   -- "Specification"
   143   show "abelian_group \<Z>"
   144   proof (rule abelian_groupI)
   145     show "!!x. x \<in> carrier \<Z> ==> EX y : carrier \<Z>. y \<oplus>\<^bsub>\<Z>\<^esub> x = \<zero>\<^bsub>\<Z>\<^esub>"
   146       by simp arith
   147   qed auto
   148   then interpret int: abelian_group \<Z> .
   149   -- "Operations"
   150   { fix x y have "add \<Z> x y = x + y" by simp }
   151   note add = this
   152   have zero: "zero \<Z> = 0" by simp
   153   { fix x
   154     have "add \<Z> (-x) x = zero \<Z>" by (simp add: add zero)
   155     then show "a_inv \<Z> x = - x" by (simp add: int.minus_equality) }
   156   note a_inv = this
   157   show "a_minus \<Z> x y = x - y" by (simp add: int.minus_eq add a_inv)
   158 qed (simp add: int_carrier_eq int_zero_eq int_add_eq int_finsum_eq)+
   159 
   160 interpretation int: "domain" \<Z>
   161   where "carrier \<Z> = UNIV"
   162     and "zero \<Z> = 0"
   163     and "add \<Z> x y = x + y"
   164     and "finsum \<Z> f A = (if finite A then setsum f A else undefined)"
   165     and "a_inv \<Z> x = - x"
   166     and "a_minus \<Z> x y = x - y"
   167 proof -
   168   show "domain \<Z>" by unfold_locales (auto simp: distrib_right distrib_left)
   169 qed (simp
   170     add: int_carrier_eq int_zero_eq int_add_eq int_finsum_eq int_a_inv_eq int_a_minus_eq)+
   171 
   172 
   173 text {* Removal of occurrences of @{term UNIV} in interpretation result
   174   --- experimental. *}
   175 
   176 lemma UNIV:
   177   "x \<in> UNIV = True"
   178   "A \<subseteq> UNIV = True"
   179   "(ALL x : UNIV. P x) = (ALL x. P x)"
   180   "(EX x : UNIV. P x) = (EX x. P x)"
   181   "(True --> Q) = Q"
   182   "(True ==> PROP R) == PROP R"
   183   by simp_all
   184 
   185 interpretation int (* FIXME [unfolded UNIV] *) :
   186   partial_order "\<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr>"
   187   where "carrier \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> = UNIV"
   188     and "le \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = (x \<le> y)"
   189     and "lless \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = (x < y)"
   190 proof -
   191   show "partial_order \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr>"
   192     by default simp_all
   193   show "carrier \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> = UNIV"
   194     by simp
   195   show "le \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = (x \<le> y)"
   196     by simp
   197   show "lless \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = (x < y)"
   198     by (simp add: lless_def) auto
   199 qed
   200 
   201 interpretation int (* FIXME [unfolded UNIV] *) :
   202   lattice "\<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr>"
   203   where "join \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = max x y"
   204     and "meet \<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr> x y = min x y"
   205 proof -
   206   let ?Z = "\<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr>"
   207   show "lattice ?Z"
   208     apply unfold_locales
   209     apply (simp add: least_def Upper_def)
   210     apply arith
   211     apply (simp add: greatest_def Lower_def)
   212     apply arith
   213     done
   214   then interpret int: lattice "?Z" .
   215   show "join ?Z x y = max x y"
   216     apply (rule int.joinI)
   217     apply (simp_all add: least_def Upper_def)
   218     apply arith
   219     done
   220   show "meet ?Z x y = min x y"
   221     apply (rule int.meetI)
   222     apply (simp_all add: greatest_def Lower_def)
   223     apply arith
   224     done
   225 qed
   226 
   227 interpretation int (* [unfolded UNIV] *) :
   228   total_order "\<lparr>carrier = UNIV::int set, eq = op =, le = op \<le>\<rparr>"
   229   by default clarsimp
   230 
   231 
   232 subsection {* Generated Ideals of @{text "\<Z>"} *}
   233 
   234 lemma int_Idl:
   235   "Idl\<^bsub>\<Z>\<^esub> {a} = {x * a | x. True}"
   236   apply (subst int.cgenideal_eq_genideal[symmetric]) apply simp
   237   apply (simp add: cgenideal_def)
   238   done
   239 
   240 lemma multiples_principalideal:
   241   "principalideal {x * a | x. True } \<Z>"
   242 by (metis UNIV_I int.cgenideal_eq_genideal int.cgenideal_is_principalideal int_Idl)
   243 
   244 lemma prime_primeideal:
   245   assumes prime: "prime p"
   246   shows "primeideal (Idl\<^bsub>\<Z>\<^esub> {p}) \<Z>"
   247 apply (rule primeidealI)
   248    apply (rule int.genideal_ideal, simp)
   249   apply (rule int_is_cring)
   250  apply (simp add: int.cgenideal_eq_genideal[symmetric] cgenideal_def)
   251  apply clarsimp defer 1
   252  apply (simp add: int.cgenideal_eq_genideal[symmetric] cgenideal_def)
   253  apply (elim exE)
   254 proof -
   255   fix a b x
   256   assume "a * b = x * int p"
   257   hence "p dvd a * b" by simp
   258   hence "p dvd a | p dvd b"
   259     by (metis prime prime_dvd_mult_eq_int)
   260   thus "(\<exists>x. a = x * int p) \<or> (\<exists>x. b = x * int p)"
   261     by (metis dvd_def mult_commute)
   262 next
   263   assume "UNIV = {uu. EX x. uu = x * int p}"
   264   then obtain x where "1 = x * int p" by best
   265   hence "\<bar>int p * x\<bar> = 1" by (simp add: mult_commute)
   266   then show False
   267     by (metis abs_of_nat int_1 of_nat_eq_iff abs_zmult_eq_1 one_not_prime_nat prime)
   268 qed
   269 
   270 
   271 subsection {* Ideals and Divisibility *}
   272 
   273 lemma int_Idl_subset_ideal:
   274   "Idl\<^bsub>\<Z>\<^esub> {k} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {l} = (k \<in> Idl\<^bsub>\<Z>\<^esub> {l})"
   275 by (rule int.Idl_subset_ideal', simp+)
   276 
   277 lemma Idl_subset_eq_dvd:
   278   "(Idl\<^bsub>\<Z>\<^esub> {k} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {l}) = (l dvd k)"
   279 apply (subst int_Idl_subset_ideal, subst int_Idl, simp)
   280 apply (rule, clarify)
   281 apply (simp add: dvd_def)
   282 apply (simp add: dvd_def mult_ac)
   283 done
   284 
   285 lemma dvds_eq_Idl:
   286   "(l dvd k \<and> k dvd l) = (Idl\<^bsub>\<Z>\<^esub> {k} = Idl\<^bsub>\<Z>\<^esub> {l})"
   287 proof -
   288   have a: "l dvd k = (Idl\<^bsub>\<Z>\<^esub> {k} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {l})" by (rule Idl_subset_eq_dvd[symmetric])
   289   have b: "k dvd l = (Idl\<^bsub>\<Z>\<^esub> {l} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {k})" by (rule Idl_subset_eq_dvd[symmetric])
   290 
   291   have "(l dvd k \<and> k dvd l) = ((Idl\<^bsub>\<Z>\<^esub> {k} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {l}) \<and> (Idl\<^bsub>\<Z>\<^esub> {l} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {k}))"
   292   by (subst a, subst b, simp)
   293   also have "((Idl\<^bsub>\<Z>\<^esub> {k} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {l}) \<and> (Idl\<^bsub>\<Z>\<^esub> {l} \<subseteq> Idl\<^bsub>\<Z>\<^esub> {k})) = (Idl\<^bsub>\<Z>\<^esub> {k} = Idl\<^bsub>\<Z>\<^esub> {l})" by (rule, fast+)
   294   finally
   295     show ?thesis .
   296 qed
   297 
   298 lemma Idl_eq_abs:
   299   "(Idl\<^bsub>\<Z>\<^esub> {k} = Idl\<^bsub>\<Z>\<^esub> {l}) = (abs l = abs k)"
   300 apply (subst dvds_eq_abseq[symmetric])
   301 apply (rule dvds_eq_Idl[symmetric])
   302 done
   303 
   304 
   305 subsection {* Ideals and the Modulus *}
   306 
   307 definition
   308   ZMod :: "int => int => int set"
   309   where "ZMod k r = (Idl\<^bsub>\<Z>\<^esub> {k}) +>\<^bsub>\<Z>\<^esub> r"
   310 
   311 lemmas ZMod_defs =
   312   ZMod_def genideal_def
   313 
   314 lemma rcos_zfact:
   315   assumes kIl: "k \<in> ZMod l r"
   316   shows "EX x. k = x * l + r"
   317 proof -
   318   from kIl[unfolded ZMod_def]
   319       have "\<exists>xl\<in>Idl\<^bsub>\<Z>\<^esub> {l}. k = xl + r" by (simp add: a_r_coset_defs)
   320   then obtain xl
   321       where xl: "xl \<in> Idl\<^bsub>\<Z>\<^esub> {l}"
   322       and k: "k = xl + r"
   323       by auto
   324   from xl obtain x
   325       where "xl = x * l"
   326       by (simp add: int_Idl, fast)
   327   from k and this
   328       have "k = x * l + r" by simp
   329   thus "\<exists>x. k = x * l + r" ..
   330 qed
   331 
   332 lemma ZMod_imp_zmod:
   333   assumes zmods: "ZMod m a = ZMod m b"
   334   shows "a mod m = b mod m"
   335 proof -
   336   interpret ideal "Idl\<^bsub>\<Z>\<^esub> {m}" \<Z> by (rule int.genideal_ideal, fast)
   337   from zmods
   338       have "b \<in> ZMod m a"
   339       unfolding ZMod_def
   340       by (simp add: a_repr_independenceD)
   341   then
   342       have "EX x. b = x * m + a" by (rule rcos_zfact)
   343   then obtain x
   344       where "b = x * m + a"
   345       by fast
   346 
   347   hence "b mod m = (x * m + a) mod m" by simp
   348   also
   349       have "\<dots> = ((x * m) mod m) + (a mod m)" by (simp add: mod_add_eq)
   350   also
   351       have "\<dots> = a mod m" by simp
   352   finally
   353       have "b mod m = a mod m" .
   354   thus "a mod m = b mod m" ..
   355 qed
   356 
   357 lemma ZMod_mod:
   358   shows "ZMod m a = ZMod m (a mod m)"
   359 proof -
   360   interpret ideal "Idl\<^bsub>\<Z>\<^esub> {m}" \<Z> by (rule int.genideal_ideal, fast)
   361   show ?thesis
   362       unfolding ZMod_def
   363   apply (rule a_repr_independence'[symmetric])
   364   apply (simp add: int_Idl a_r_coset_defs)
   365   proof -
   366     have "a = m * (a div m) + (a mod m)" by (simp add: zmod_zdiv_equality)
   367     hence "a = (a div m) * m + (a mod m)" by simp
   368     thus "\<exists>h. (\<exists>x. h = x * m) \<and> a = h + a mod m" by fast
   369   qed simp
   370 qed
   371 
   372 lemma zmod_imp_ZMod:
   373   assumes modeq: "a mod m = b mod m"
   374   shows "ZMod m a = ZMod m b"
   375 proof -
   376   have "ZMod m a = ZMod m (a mod m)" by (rule ZMod_mod)
   377   also have "\<dots> = ZMod m (b mod m)" by (simp add: modeq[symmetric])
   378   also have "\<dots> = ZMod m b" by (rule ZMod_mod[symmetric])
   379   finally show ?thesis .
   380 qed
   381 
   382 corollary ZMod_eq_mod:
   383   shows "(ZMod m a = ZMod m b) = (a mod m = b mod m)"
   384 by (rule, erule ZMod_imp_zmod, erule zmod_imp_ZMod)
   385 
   386 
   387 subsection {* Factorization *}
   388 
   389 definition
   390   ZFact :: "int \<Rightarrow> int set ring"
   391   where "ZFact k = \<Z> Quot (Idl\<^bsub>\<Z>\<^esub> {k})"
   392 
   393 lemmas ZFact_defs = ZFact_def FactRing_def
   394 
   395 lemma ZFact_is_cring:
   396   shows "cring (ZFact k)"
   397 apply (unfold ZFact_def)
   398 apply (rule ideal.quotient_is_cring)
   399  apply (intro ring.genideal_ideal)
   400   apply (simp add: cring.axioms[OF int_is_cring] ring.intro)
   401  apply simp
   402 apply (rule int_is_cring)
   403 done
   404 
   405 lemma ZFact_zero:
   406   "carrier (ZFact 0) = (\<Union>a. {{a}})"
   407 apply (insert int.genideal_zero)
   408 apply (simp add: ZFact_defs A_RCOSETS_defs r_coset_def ring_record_simps)
   409 done
   410 
   411 lemma ZFact_one:
   412   "carrier (ZFact 1) = {UNIV}"
   413 apply (simp only: ZFact_defs A_RCOSETS_defs r_coset_def ring_record_simps)
   414 apply (subst int.genideal_one)
   415 apply (rule, rule, clarsimp)
   416  apply (rule, rule, clarsimp)
   417  apply (rule, clarsimp, arith)
   418 apply (rule, clarsimp)
   419 apply (rule exI[of _ "0"], clarsimp)
   420 done
   421 
   422 lemma ZFact_prime_is_domain:
   423   assumes pprime: "prime p"
   424   shows "domain (ZFact p)"
   425 apply (unfold ZFact_def)
   426 apply (rule primeideal.quotient_is_domain)
   427 apply (rule prime_primeideal[OF pprime])
   428 done
   429 
   430 end