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