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