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