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