src/HOL/Number_Theory/Residues.thy
author wenzelm
Sun Mar 13 22:55:50 2011 +0100 (2011-03-13)
changeset 41959 b460124855b8
parent 41541 1fa4725c4656
child 44872 a98ef45122f3
permissions -rw-r--r--
tuned headers;
wenzelm@41959
     1
(*  Title:      HOL/Number_Theory/Residues.thy
nipkow@31719
     2
    Author:     Jeremy Avigad
nipkow@31719
     3
wenzelm@41541
     4
An algebraic treatment of residue rings, and resulting proofs of
wenzelm@41959
     5
Euler's theorem and Wilson's theorem.
nipkow@31719
     6
*)
nipkow@31719
     7
nipkow@31719
     8
header {* Residue rings *}
nipkow@31719
     9
nipkow@31719
    10
theory Residues
nipkow@31719
    11
imports
wenzelm@41541
    12
  UniqueFactorization
wenzelm@41541
    13
  Binomial
wenzelm@41541
    14
  MiscAlgebra
nipkow@31719
    15
begin
nipkow@31719
    16
nipkow@31719
    17
nipkow@31719
    18
(*
nipkow@31719
    19
 
nipkow@31719
    20
  A locale for residue rings
nipkow@31719
    21
nipkow@31719
    22
*)
nipkow@31719
    23
haftmann@35416
    24
definition residue_ring :: "int => int ring" where
nipkow@31719
    25
  "residue_ring m == (| 
nipkow@31719
    26
    carrier =       {0..m - 1}, 
nipkow@31719
    27
    mult =          (%x y. (x * y) mod m),
nipkow@31719
    28
    one =           1,
nipkow@31719
    29
    zero =          0,
nipkow@31719
    30
    add =           (%x y. (x + y) mod m) |)"
nipkow@31719
    31
nipkow@31719
    32
locale residues =
nipkow@31719
    33
  fixes m :: int and R (structure)
nipkow@31719
    34
  assumes m_gt_one: "m > 1"
nipkow@31719
    35
  defines "R == residue_ring m"
nipkow@31719
    36
nipkow@31719
    37
context residues begin
nipkow@31719
    38
nipkow@31719
    39
lemma abelian_group: "abelian_group R"
nipkow@31719
    40
  apply (insert m_gt_one)
nipkow@31719
    41
  apply (rule abelian_groupI)
nipkow@31719
    42
  apply (unfold R_def residue_ring_def)
wenzelm@41541
    43
  apply (auto simp add: mod_add_right_eq [symmetric] add_ac)
nipkow@31719
    44
  apply (case_tac "x = 0")
nipkow@31719
    45
  apply force
nipkow@31719
    46
  apply (subgoal_tac "(x + (m - x)) mod m = 0")
nipkow@31719
    47
  apply (erule bexI)
nipkow@31719
    48
  apply auto
wenzelm@41541
    49
  done
nipkow@31719
    50
nipkow@31719
    51
lemma comm_monoid: "comm_monoid R"
nipkow@31719
    52
  apply (insert m_gt_one)
nipkow@31719
    53
  apply (unfold R_def residue_ring_def)
nipkow@31719
    54
  apply (rule comm_monoidI)
nipkow@31719
    55
  apply auto
nipkow@31719
    56
  apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
nipkow@31719
    57
  apply (erule ssubst)
nipkow@31719
    58
  apply (subst zmod_zmult1_eq [symmetric])+
nipkow@31719
    59
  apply (simp_all only: mult_ac)
wenzelm@41541
    60
  done
nipkow@31719
    61
nipkow@31719
    62
lemma cring: "cring R"
nipkow@31719
    63
  apply (rule cringI)
nipkow@31719
    64
  apply (rule abelian_group)
nipkow@31719
    65
  apply (rule comm_monoid)
nipkow@31719
    66
  apply (unfold R_def residue_ring_def, auto)
nipkow@31719
    67
  apply (subst mod_add_eq [symmetric])
nipkow@31719
    68
  apply (subst mult_commute)
nipkow@31719
    69
  apply (subst zmod_zmult1_eq [symmetric])
haftmann@36350
    70
  apply (simp add: field_simps)
wenzelm@41541
    71
  done
nipkow@31719
    72
nipkow@31719
    73
end
nipkow@31719
    74
nipkow@31719
    75
sublocale residues < cring
nipkow@31719
    76
  by (rule cring)
nipkow@31719
    77
nipkow@31719
    78
wenzelm@41541
    79
context residues
wenzelm@41541
    80
begin
nipkow@31719
    81
nipkow@31719
    82
(* These lemmas translate back and forth between internal and 
nipkow@31719
    83
   external concepts *)
nipkow@31719
    84
nipkow@31719
    85
lemma res_carrier_eq: "carrier R = {0..m - 1}"
nipkow@31719
    86
  by (unfold R_def residue_ring_def, auto)
nipkow@31719
    87
nipkow@31719
    88
lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
nipkow@31719
    89
  by (unfold R_def residue_ring_def, auto)
nipkow@31719
    90
nipkow@31719
    91
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
nipkow@31719
    92
  by (unfold R_def residue_ring_def, auto)
nipkow@31719
    93
nipkow@31719
    94
lemma res_zero_eq: "\<zero> = 0"
nipkow@31719
    95
  by (unfold R_def residue_ring_def, auto)
nipkow@31719
    96
nipkow@31719
    97
lemma res_one_eq: "\<one> = 1"
wenzelm@41541
    98
  by (unfold R_def residue_ring_def units_of_def, auto)
nipkow@31719
    99
nipkow@31719
   100
lemma res_units_eq: "Units R = { x. 0 < x & x < m & coprime x m}"
nipkow@31719
   101
  apply (insert m_gt_one)
nipkow@31719
   102
  apply (unfold Units_def R_def residue_ring_def)
nipkow@31719
   103
  apply auto
nipkow@31719
   104
  apply (subgoal_tac "x ~= 0")
nipkow@31719
   105
  apply auto
nipkow@31952
   106
  apply (rule invertible_coprime_int)
nipkow@31719
   107
  apply (subgoal_tac "x ~= 0")
nipkow@31719
   108
  apply auto
nipkow@31952
   109
  apply (subst (asm) coprime_iff_invertible'_int)
nipkow@31719
   110
  apply (rule m_gt_one)
nipkow@31719
   111
  apply (auto simp add: cong_int_def mult_commute)
wenzelm@41541
   112
  done
nipkow@31719
   113
nipkow@31719
   114
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
nipkow@31719
   115
  apply (insert m_gt_one)
nipkow@31719
   116
  apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
nipkow@31719
   117
  apply auto
nipkow@31719
   118
  apply (rule the_equality)
nipkow@31719
   119
  apply auto
nipkow@31719
   120
  apply (subst mod_add_right_eq [symmetric])
nipkow@31719
   121
  apply auto
nipkow@31719
   122
  apply (subst mod_add_left_eq [symmetric])
nipkow@31719
   123
  apply auto
nipkow@31719
   124
  apply (subgoal_tac "y mod m = - x mod m")
nipkow@31719
   125
  apply simp
nipkow@31719
   126
  apply (subst zmod_eq_dvd_iff)
nipkow@31719
   127
  apply auto
wenzelm@41541
   128
  done
nipkow@31719
   129
nipkow@31719
   130
lemma finite [iff]: "finite(carrier R)"
nipkow@31719
   131
  by (subst res_carrier_eq, auto)
nipkow@31719
   132
nipkow@31719
   133
lemma finite_Units [iff]: "finite(Units R)"
nipkow@31719
   134
  by (subst res_units_eq, auto)
nipkow@31719
   135
nipkow@31719
   136
(* The function a -> a mod m maps the integers to the 
nipkow@31719
   137
   residue classes. The following lemmas show that this mapping 
nipkow@31719
   138
   respects addition and multiplication on the integers. *)
nipkow@31719
   139
nipkow@31719
   140
lemma mod_in_carrier [iff]: "a mod m : carrier R"
nipkow@31719
   141
  apply (unfold res_carrier_eq)
nipkow@31719
   142
  apply (insert m_gt_one, auto)
wenzelm@41541
   143
  done
nipkow@31719
   144
nipkow@31719
   145
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
nipkow@31719
   146
  by (unfold R_def residue_ring_def, auto, arith)
nipkow@31719
   147
nipkow@31719
   148
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
nipkow@31719
   149
  apply (unfold R_def residue_ring_def, auto)
nipkow@31719
   150
  apply (subst zmod_zmult1_eq [symmetric])
nipkow@31719
   151
  apply (subst mult_commute)
nipkow@31719
   152
  apply (subst zmod_zmult1_eq [symmetric])
nipkow@31719
   153
  apply (subst mult_commute)
nipkow@31719
   154
  apply auto
wenzelm@41541
   155
  done
nipkow@31719
   156
nipkow@31719
   157
lemma zero_cong: "\<zero> = 0"
nipkow@31719
   158
  apply (unfold R_def residue_ring_def, auto)
wenzelm@41541
   159
  done
nipkow@31719
   160
nipkow@31719
   161
lemma one_cong: "\<one> = 1 mod m"
nipkow@31719
   162
  apply (insert m_gt_one)
nipkow@31719
   163
  apply (unfold R_def residue_ring_def, auto)
wenzelm@41541
   164
  done
nipkow@31719
   165
nipkow@31719
   166
(* revise algebra library to use 1? *)
nipkow@31719
   167
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
nipkow@31719
   168
  apply (insert m_gt_one)
nipkow@31719
   169
  apply (induct n)
wenzelm@41541
   170
  apply (auto simp add: nat_pow_def one_cong)
nipkow@31719
   171
  apply (subst mult_commute)
nipkow@31719
   172
  apply (rule mult_cong)
wenzelm@41541
   173
  done
nipkow@31719
   174
nipkow@31719
   175
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
nipkow@31719
   176
  apply (rule sym)
nipkow@31719
   177
  apply (rule sum_zero_eq_neg)
nipkow@31719
   178
  apply auto
nipkow@31719
   179
  apply (subst add_cong)
nipkow@31719
   180
  apply (subst zero_cong)
nipkow@31719
   181
  apply auto
wenzelm@41541
   182
  done
nipkow@31719
   183
nipkow@31719
   184
lemma (in residues) prod_cong: 
nipkow@31719
   185
  "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
nipkow@31719
   186
  apply (induct set: finite)
nipkow@31727
   187
  apply (auto simp: one_cong mult_cong)
wenzelm@41541
   188
  done
nipkow@31719
   189
nipkow@31719
   190
lemma (in residues) sum_cong:
nipkow@31719
   191
  "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
nipkow@31719
   192
  apply (induct set: finite)
nipkow@31727
   193
  apply (auto simp: zero_cong add_cong)
wenzelm@41541
   194
  done
nipkow@31719
   195
nipkow@31719
   196
lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow> 
nipkow@31719
   197
    a mod m : Units R"
nipkow@31719
   198
  apply (subst res_units_eq, auto)
nipkow@31719
   199
  apply (insert pos_mod_sign [of m a])
nipkow@31719
   200
  apply (subgoal_tac "a mod m ~= 0")
nipkow@31719
   201
  apply arith
nipkow@31719
   202
  apply auto
nipkow@31952
   203
  apply (subst (asm) gcd_red_int)
nipkow@31952
   204
  apply (subst gcd_commute_int, assumption)
wenzelm@41541
   205
  done
nipkow@31719
   206
nipkow@31719
   207
lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))" 
nipkow@31719
   208
  unfolding cong_int_def by auto
nipkow@31719
   209
nipkow@31719
   210
(* Simplifying with these will translate a ring equation in R to a 
nipkow@31719
   211
   congruence. *)
nipkow@31719
   212
nipkow@31719
   213
lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
nipkow@31719
   214
    prod_cong sum_cong neg_cong res_eq_to_cong
nipkow@31719
   215
nipkow@31719
   216
(* Other useful facts about the residue ring *)
nipkow@31719
   217
nipkow@31719
   218
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
nipkow@31719
   219
  apply (simp add: res_one_eq res_neg_eq)
nipkow@31719
   220
  apply (insert m_gt_one)
nipkow@31719
   221
  apply (subgoal_tac "~(m > 2)")
nipkow@31719
   222
  apply arith
nipkow@31719
   223
  apply (rule notI)
nipkow@31719
   224
  apply (subgoal_tac "-1 mod m = m - 1")
nipkow@31719
   225
  apply force
nipkow@31719
   226
  apply (subst mod_add_self2 [symmetric])
nipkow@31719
   227
  apply (subst mod_pos_pos_trivial)
nipkow@31719
   228
  apply auto
wenzelm@41541
   229
  done
nipkow@31719
   230
nipkow@31719
   231
end
nipkow@31719
   232
nipkow@31719
   233
nipkow@31719
   234
(* prime residues *)
nipkow@31719
   235
nipkow@31719
   236
locale residues_prime =
nipkow@31719
   237
  fixes p :: int and R (structure)
nipkow@31719
   238
  assumes p_prime [intro]: "prime p"
nipkow@31719
   239
  defines "R == residue_ring p"
nipkow@31719
   240
nipkow@31719
   241
sublocale residues_prime < residues p
nipkow@31719
   242
  apply (unfold R_def residues_def)
nipkow@31719
   243
  using p_prime apply auto
wenzelm@41541
   244
  done
nipkow@31719
   245
nipkow@31719
   246
context residues_prime begin
nipkow@31719
   247
nipkow@31719
   248
lemma is_field: "field R"
nipkow@31719
   249
  apply (rule cring.field_intro2)
nipkow@31719
   250
  apply (rule cring)
nipkow@31719
   251
  apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq
nipkow@31719
   252
    res_units_eq)
nipkow@31719
   253
  apply (rule classical)
nipkow@31719
   254
  apply (erule notE)
nipkow@31952
   255
  apply (subst gcd_commute_int)
nipkow@31952
   256
  apply (rule prime_imp_coprime_int)
nipkow@31719
   257
  apply (rule p_prime)
nipkow@31719
   258
  apply (rule notI)
nipkow@31719
   259
  apply (frule zdvd_imp_le)
nipkow@31719
   260
  apply auto
wenzelm@41541
   261
  done
nipkow@31719
   262
nipkow@31719
   263
lemma res_prime_units_eq: "Units R = {1..p - 1}"
nipkow@31719
   264
  apply (subst res_units_eq)
nipkow@31719
   265
  apply auto
nipkow@31952
   266
  apply (subst gcd_commute_int)
nipkow@31952
   267
  apply (rule prime_imp_coprime_int)
nipkow@31719
   268
  apply (rule p_prime)
nipkow@31719
   269
  apply (rule zdvd_not_zless)
nipkow@31719
   270
  apply auto
wenzelm@41541
   271
  done
nipkow@31719
   272
nipkow@31719
   273
end
nipkow@31719
   274
nipkow@31719
   275
sublocale residues_prime < field
nipkow@31719
   276
  by (rule is_field)
nipkow@31719
   277
nipkow@31719
   278
nipkow@31719
   279
(*
nipkow@31719
   280
  Test cases: Euler's theorem and Wilson's theorem.
nipkow@31719
   281
*)
nipkow@31719
   282
nipkow@31719
   283
nipkow@31719
   284
subsection{* Euler's theorem *}
nipkow@31719
   285
nipkow@31719
   286
(* the definition of the phi function *)
nipkow@31719
   287
haftmann@35416
   288
definition phi :: "int => nat" where
nipkow@31719
   289
  "phi m == card({ x. 0 < x & x < m & gcd x m = 1})" 
nipkow@31719
   290
nipkow@31719
   291
lemma phi_zero [simp]: "phi 0 = 0"
nipkow@31719
   292
  apply (subst phi_def)
nipkow@31719
   293
(* Auto hangs here. Once again, where is the simplification rule 
nipkow@31719
   294
   1 == Suc 0 coming from? *)
nipkow@31719
   295
  apply (auto simp add: card_eq_0_iff)
nipkow@31719
   296
(* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
wenzelm@41541
   297
  done
nipkow@31719
   298
nipkow@31719
   299
lemma phi_one [simp]: "phi 1 = 0"
nipkow@31719
   300
  apply (auto simp add: phi_def card_eq_0_iff)
wenzelm@41541
   301
  done
nipkow@31719
   302
nipkow@31719
   303
lemma (in residues) phi_eq: "phi m = card(Units R)"
nipkow@31719
   304
  by (simp add: phi_def res_units_eq)
nipkow@31719
   305
nipkow@31719
   306
lemma (in residues) euler_theorem1: 
nipkow@31719
   307
  assumes a: "gcd a m = 1"
nipkow@31719
   308
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   309
proof -
nipkow@31719
   310
  from a m_gt_one have [simp]: "a mod m : Units R"
nipkow@31719
   311
    by (intro mod_in_res_units)
nipkow@31719
   312
  from phi_eq have "(a mod m) (^) (phi m) = (a mod m) (^) (card (Units R))"
nipkow@31719
   313
    by simp
nipkow@31719
   314
  also have "\<dots> = \<one>" 
nipkow@31719
   315
    by (intro units_power_order_eq_one, auto)
nipkow@31719
   316
  finally show ?thesis
nipkow@31719
   317
    by (simp add: res_to_cong_simps)
nipkow@31719
   318
qed
nipkow@31719
   319
nipkow@31719
   320
(* In fact, there is a two line proof!
nipkow@31719
   321
nipkow@31719
   322
lemma (in residues) euler_theorem1: 
nipkow@31719
   323
  assumes a: "gcd a m = 1"
nipkow@31719
   324
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   325
proof -
nipkow@31719
   326
  have "(a mod m) (^) (phi m) = \<one>"
nipkow@31719
   327
    by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
nipkow@31719
   328
  thus ?thesis
nipkow@31719
   329
    by (simp add: res_to_cong_simps)
nipkow@31719
   330
qed
nipkow@31719
   331
nipkow@31719
   332
*)
nipkow@31719
   333
nipkow@31719
   334
(* outside the locale, we can relax the restriction m > 1 *)
nipkow@31719
   335
nipkow@31719
   336
lemma euler_theorem:
nipkow@31719
   337
  assumes "m >= 0" and "gcd a m = 1"
nipkow@31719
   338
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   339
proof (cases)
nipkow@31719
   340
  assume "m = 0 | m = 1"
nipkow@31719
   341
  thus ?thesis by auto
nipkow@31719
   342
next
nipkow@31719
   343
  assume "~(m = 0 | m = 1)"
wenzelm@41541
   344
  with assms show ?thesis
nipkow@31719
   345
    by (intro residues.euler_theorem1, unfold residues_def, auto)
nipkow@31719
   346
qed
nipkow@31719
   347
nipkow@31719
   348
lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
nipkow@31719
   349
  apply (subst phi_eq)
nipkow@31719
   350
  apply (subst res_prime_units_eq)
nipkow@31719
   351
  apply auto
wenzelm@41541
   352
  done
nipkow@31719
   353
nipkow@31719
   354
lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
nipkow@31719
   355
  apply (rule residues_prime.phi_prime)
nipkow@31719
   356
  apply (erule residues_prime.intro)
wenzelm@41541
   357
  done
nipkow@31719
   358
nipkow@31719
   359
lemma fermat_theorem:
nipkow@31719
   360
  assumes "prime p" and "~ (p dvd a)"
nipkow@31719
   361
  shows "[a^(nat p - 1) = 1] (mod p)"
nipkow@31719
   362
proof -
wenzelm@41541
   363
  from assms have "[a^phi p = 1] (mod p)"
nipkow@31719
   364
    apply (intro euler_theorem)
nipkow@31719
   365
    (* auto should get this next part. matching across
nipkow@31719
   366
       substitutions is needed. *)
nipkow@31952
   367
    apply (frule prime_gt_1_int, arith)
nipkow@31952
   368
    apply (subst gcd_commute_int, erule prime_imp_coprime_int, assumption)
nipkow@31719
   369
    done
nipkow@31719
   370
  also have "phi p = nat p - 1"
wenzelm@41541
   371
    by (rule phi_prime, rule assms)
nipkow@31719
   372
  finally show ?thesis .
nipkow@31719
   373
qed
nipkow@31719
   374
nipkow@31719
   375
nipkow@31719
   376
subsection {* Wilson's theorem *}
nipkow@31719
   377
nipkow@31719
   378
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow> 
wenzelm@41541
   379
    {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}" 
nipkow@31719
   380
  apply auto
nipkow@31719
   381
  apply (erule notE)
nipkow@31719
   382
  apply (erule inv_eq_imp_eq)
nipkow@31719
   383
  apply auto
nipkow@31719
   384
  apply (erule notE)
nipkow@31719
   385
  apply (erule inv_eq_imp_eq)
nipkow@31719
   386
  apply auto
wenzelm@41541
   387
  done
nipkow@31719
   388
nipkow@31719
   389
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   390
  assumes a: "p > 2"
nipkow@31719
   391
  shows "[fact (p - 1) = - 1] (mod p)"
nipkow@31719
   392
proof -
nipkow@31719
   393
  let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}" 
nipkow@31732
   394
  have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
nipkow@31719
   395
    by auto
nipkow@31732
   396
  have "(\<Otimes>i: Units R. i) = 
nipkow@31719
   397
    (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
nipkow@31732
   398
    apply (subst UR)
nipkow@31719
   399
    apply (subst finprod_Un_disjoint)
nipkow@31732
   400
    apply (auto intro:funcsetI)
nipkow@31719
   401
    apply (drule sym, subst (asm) inv_eq_one_eq)
nipkow@31719
   402
    apply auto
nipkow@31719
   403
    apply (drule sym, subst (asm) inv_eq_neg_one_eq)
nipkow@31719
   404
    apply auto
nipkow@31719
   405
    done
nipkow@31719
   406
  also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   407
    apply (subst finprod_insert)
nipkow@31719
   408
    apply auto
nipkow@31719
   409
    apply (frule one_eq_neg_one)
nipkow@31719
   410
    apply (insert a, force)
nipkow@31719
   411
    done
nipkow@31719
   412
  also have "(\<Otimes>i:(Union ?InversePairs). i) = 
wenzelm@41541
   413
      (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
nipkow@31719
   414
    apply (subst finprod_Union_disjoint)
nipkow@31719
   415
    apply force
nipkow@31719
   416
    apply force
nipkow@31719
   417
    apply clarify
nipkow@31719
   418
    apply (rule inv_pair_lemma)
nipkow@31719
   419
    apply auto
nipkow@31719
   420
    done
nipkow@31719
   421
  also have "\<dots> = \<one>"
nipkow@31719
   422
    apply (rule finprod_one)
nipkow@31719
   423
    apply auto
nipkow@31719
   424
    apply (subst finprod_insert)
nipkow@31719
   425
    apply auto
nipkow@31719
   426
    apply (frule inv_eq_self)
nipkow@31732
   427
    apply (auto)
nipkow@31719
   428
    done
nipkow@31719
   429
  finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
nipkow@31719
   430
    by simp
nipkow@31719
   431
  also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
nipkow@31719
   432
    apply (rule finprod_cong')
nipkow@31732
   433
    apply (auto)
nipkow@31719
   434
    apply (subst (asm) res_prime_units_eq)
nipkow@31719
   435
    apply auto
nipkow@31719
   436
    done
nipkow@31719
   437
  also have "\<dots> = (PROD i: Units R. i) mod p"
nipkow@31719
   438
    apply (rule prod_cong)
nipkow@31719
   439
    apply auto
nipkow@31719
   440
    done
nipkow@31719
   441
  also have "\<dots> = fact (p - 1) mod p"
nipkow@31952
   442
    apply (subst fact_altdef_int)
wenzelm@41541
   443
    apply (insert assms, force)
nipkow@31719
   444
    apply (subst res_prime_units_eq, rule refl)
nipkow@31719
   445
    done
nipkow@31719
   446
  finally have "fact (p - 1) mod p = \<ominus> \<one>".
nipkow@31719
   447
  thus ?thesis
nipkow@31719
   448
    by (simp add: res_to_cong_simps)
nipkow@31719
   449
qed
nipkow@31719
   450
nipkow@31719
   451
lemma wilson_theorem: "prime (p::int) \<Longrightarrow> [fact (p - 1) = - 1] (mod p)"
nipkow@31952
   452
  apply (frule prime_gt_1_int)
nipkow@31719
   453
  apply (case_tac "p = 2")
nipkow@31952
   454
  apply (subst fact_altdef_int, simp)
nipkow@31719
   455
  apply (subst cong_int_def)
nipkow@31719
   456
  apply simp
nipkow@31719
   457
  apply (rule residues_prime.wilson_theorem1)
nipkow@31719
   458
  apply (rule residues_prime.intro)
nipkow@31719
   459
  apply auto
nipkow@31719
   460
done
nipkow@31719
   461
nipkow@31719
   462
nipkow@31719
   463
end