src/HOL/Number_Theory/Residues.thy
author paulson <lp15@cam.ac.uk>
Sun Feb 02 19:15:25 2014 +0000 (2014-02-02)
changeset 55242 413ec965f95d
parent 55227 653de351d21c
child 55261 ad3604df6bc6
permissions -rw-r--r--
Number_Theory: prime is no longer overloaded, but only for nat. Automatic coercion to int enabled.
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
(*
wenzelm@44872
    18
nipkow@31719
    19
  A locale for residue rings
nipkow@31719
    20
nipkow@31719
    21
*)
nipkow@31719
    22
haftmann@35416
    23
definition residue_ring :: "int => int ring" where
wenzelm@44872
    24
  "residue_ring m == (|
wenzelm@44872
    25
    carrier =       {0..m - 1},
nipkow@31719
    26
    mult =          (%x y. (x * y) mod m),
nipkow@31719
    27
    one =           1,
nipkow@31719
    28
    zero =          0,
nipkow@31719
    29
    add =           (%x y. (x + y) mod m) |)"
nipkow@31719
    30
nipkow@31719
    31
locale residues =
nipkow@31719
    32
  fixes m :: int and R (structure)
nipkow@31719
    33
  assumes m_gt_one: "m > 1"
nipkow@31719
    34
  defines "R == residue_ring m"
nipkow@31719
    35
wenzelm@44872
    36
context residues
wenzelm@44872
    37
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)
huffman@47163
    58
  apply (subst mod_mult_right_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)
huffman@47163
    69
  apply (subst mod_mult_right_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
wenzelm@44872
    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}"
wenzelm@44872
    86
  unfolding R_def residue_ring_def by auto
nipkow@31719
    87
nipkow@31719
    88
lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
wenzelm@44872
    89
  unfolding R_def residue_ring_def by auto
nipkow@31719
    90
nipkow@31719
    91
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
wenzelm@44872
    92
  unfolding R_def residue_ring_def by auto
nipkow@31719
    93
nipkow@31719
    94
lemma res_zero_eq: "\<zero> = 0"
wenzelm@44872
    95
  unfolding R_def residue_ring_def by auto
nipkow@31719
    96
nipkow@31719
    97
lemma res_one_eq: "\<one> = 1"
wenzelm@44872
    98
  unfolding R_def residue_ring_def units_of_def by 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)
lp15@55161
   110
  apply arith
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
wenzelm@44872
   130
lemma finite [iff]: "finite (carrier R)"
nipkow@31719
   131
  by (subst res_carrier_eq, auto)
nipkow@31719
   132
wenzelm@44872
   133
lemma finite_Units [iff]: "finite (Units R)"
bulwahn@50027
   134
  by (subst res_units_eq) auto
nipkow@31719
   135
wenzelm@44872
   136
(* The function a -> a mod m maps the integers to the
wenzelm@44872
   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"
wenzelm@44872
   146
  unfolding R_def residue_ring_def
wenzelm@44872
   147
  apply auto
wenzelm@44872
   148
  apply presburger
wenzelm@44872
   149
  done
nipkow@31719
   150
nipkow@31719
   151
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
nipkow@31719
   152
  apply (unfold R_def residue_ring_def, auto)
huffman@47163
   153
  apply (subst mod_mult_right_eq [symmetric])
nipkow@31719
   154
  apply (subst mult_commute)
huffman@47163
   155
  apply (subst mod_mult_right_eq [symmetric])
nipkow@31719
   156
  apply (subst mult_commute)
nipkow@31719
   157
  apply auto
wenzelm@41541
   158
  done
nipkow@31719
   159
nipkow@31719
   160
lemma zero_cong: "\<zero> = 0"
wenzelm@44872
   161
  unfolding R_def residue_ring_def by auto
nipkow@31719
   162
nipkow@31719
   163
lemma one_cong: "\<one> = 1 mod m"
wenzelm@44872
   164
  using m_gt_one unfolding R_def residue_ring_def by auto
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
wenzelm@44872
   184
lemma (in residues) prod_cong:
wenzelm@44872
   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:
wenzelm@44872
   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
wenzelm@44872
   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
wenzelm@44872
   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
wenzelm@44872
   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 =
lp15@55242
   237
  fixes p 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
lp15@55242
   244
  apply (metis (full_types) int_1 of_nat_less_iff prime_gt_1_nat)
wenzelm@41541
   245
  done
nipkow@31719
   246
wenzelm@44872
   247
context residues_prime
wenzelm@44872
   248
begin
nipkow@31719
   249
nipkow@31719
   250
lemma is_field: "field R"
nipkow@31719
   251
  apply (rule cring.field_intro2)
nipkow@31719
   252
  apply (rule cring)
wenzelm@44872
   253
  apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq 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
wenzelm@41541
   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
wenzelm@41541
   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
wenzelm@44872
   289
definition phi :: "int => nat"
wenzelm@44872
   290
  where "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)
wenzelm@44872
   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? *)
wenzelm@41541
   298
  done
nipkow@31719
   299
nipkow@31719
   300
lemma phi_one [simp]: "phi 1 = 0"
wenzelm@44872
   301
  by (auto simp add: phi_def card_eq_0_iff)
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
wenzelm@44872
   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
wenzelm@44872
   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
wenzelm@44872
   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)
wenzelm@44872
   328
  then show ?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"
wenzelm@44872
   341
  then show ?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:
lp15@55242
   360
  fixes a::int
nipkow@31719
   361
  assumes "prime p" and "~ (p dvd a)"
lp15@55242
   362
  shows "[a^(p - 1) = 1] (mod p)"
nipkow@31719
   363
proof -
wenzelm@41541
   364
  from assms have "[a^phi p = 1] (mod p)"
nipkow@31719
   365
    apply (intro euler_theorem)
lp15@55242
   366
    apply (metis of_nat_0_le_iff)
lp15@55242
   367
    apply (metis gcd_int.commute prime_imp_coprime_int)
nipkow@31719
   368
    done
nipkow@31719
   369
  also have "phi p = nat p - 1"
wenzelm@41541
   370
    by (rule phi_prime, rule assms)
lp15@55242
   371
  finally show ?thesis
lp15@55242
   372
    by (metis nat_int) 
nipkow@31719
   373
qed
nipkow@31719
   374
lp15@55227
   375
lemma fermat_theorem_nat:
lp15@55227
   376
  assumes "prime p" and "~ (p dvd a)"
lp15@55227
   377
  shows "[a^(p - 1) = 1] (mod p)"
lp15@55227
   378
using fermat_theorem [of p a] assms
lp15@55242
   379
by (metis int_1 of_nat_power transfer_int_nat_cong zdvd_int)
lp15@55227
   380
nipkow@31719
   381
nipkow@31719
   382
subsection {* Wilson's theorem *}
nipkow@31719
   383
wenzelm@44872
   384
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
wenzelm@44872
   385
    {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}"
nipkow@31719
   386
  apply auto
nipkow@31719
   387
  apply (erule notE)
nipkow@31719
   388
  apply (erule inv_eq_imp_eq)
nipkow@31719
   389
  apply auto
nipkow@31719
   390
  apply (erule notE)
nipkow@31719
   391
  apply (erule inv_eq_imp_eq)
nipkow@31719
   392
  apply auto
wenzelm@41541
   393
  done
nipkow@31719
   394
nipkow@31719
   395
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   396
  assumes a: "p > 2"
nipkow@31719
   397
  shows "[fact (p - 1) = - 1] (mod p)"
nipkow@31719
   398
proof -
wenzelm@44872
   399
  let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}"
nipkow@31732
   400
  have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
nipkow@31719
   401
    by auto
wenzelm@44872
   402
  have "(\<Otimes>i: Units R. i) =
nipkow@31719
   403
    (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
nipkow@31732
   404
    apply (subst UR)
nipkow@31719
   405
    apply (subst finprod_Un_disjoint)
nipkow@31732
   406
    apply (auto intro:funcsetI)
nipkow@31719
   407
    apply (drule sym, subst (asm) inv_eq_one_eq)
nipkow@31719
   408
    apply auto
nipkow@31719
   409
    apply (drule sym, subst (asm) inv_eq_neg_one_eq)
nipkow@31719
   410
    apply auto
nipkow@31719
   411
    done
nipkow@31719
   412
  also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   413
    apply (subst finprod_insert)
nipkow@31719
   414
    apply auto
nipkow@31719
   415
    apply (frule one_eq_neg_one)
nipkow@31719
   416
    apply (insert a, force)
nipkow@31719
   417
    done
wenzelm@44872
   418
  also have "(\<Otimes>i:(Union ?InversePairs). i) =
wenzelm@41541
   419
      (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
nipkow@31719
   420
    apply (subst finprod_Union_disjoint)
nipkow@31719
   421
    apply force
nipkow@31719
   422
    apply force
nipkow@31719
   423
    apply clarify
nipkow@31719
   424
    apply (rule inv_pair_lemma)
nipkow@31719
   425
    apply auto
nipkow@31719
   426
    done
nipkow@31719
   427
  also have "\<dots> = \<one>"
nipkow@31719
   428
    apply (rule finprod_one)
nipkow@31719
   429
    apply auto
nipkow@31719
   430
    apply (subst finprod_insert)
nipkow@31719
   431
    apply auto
nipkow@31719
   432
    apply (frule inv_eq_self)
nipkow@31732
   433
    apply (auto)
nipkow@31719
   434
    done
nipkow@31719
   435
  finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
nipkow@31719
   436
    by simp
nipkow@31719
   437
  also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
nipkow@31719
   438
    apply (rule finprod_cong')
nipkow@31732
   439
    apply (auto)
nipkow@31719
   440
    apply (subst (asm) res_prime_units_eq)
nipkow@31719
   441
    apply auto
nipkow@31719
   442
    done
nipkow@31719
   443
  also have "\<dots> = (PROD i: Units R. i) mod p"
nipkow@31719
   444
    apply (rule prod_cong)
nipkow@31719
   445
    apply auto
nipkow@31719
   446
    done
nipkow@31719
   447
  also have "\<dots> = fact (p - 1) mod p"
lp15@55242
   448
    apply (subst fact_altdef_nat)
lp15@55242
   449
    apply (insert assms)
lp15@55242
   450
    apply (subst res_prime_units_eq)
lp15@55242
   451
    apply (simp add: int_setprod zmod_int setprod_int_eq)
nipkow@31719
   452
    done
nipkow@31719
   453
  finally have "fact (p - 1) mod p = \<ominus> \<one>".
lp15@55242
   454
  then show ?thesis
lp15@55242
   455
    by (metis Divides.transfer_int_nat_functions(2) cong_int_def res_neg_eq res_one_eq)
nipkow@31719
   456
qed
nipkow@31719
   457
lp15@55242
   458
lemma wilson_theorem: "prime p \<Longrightarrow> [fact (p - 1) = - 1] (mod p)"
lp15@55242
   459
  apply (frule prime_gt_1_nat)
nipkow@31719
   460
  apply (case_tac "p = 2")
lp15@55242
   461
  apply (subst fact_altdef_nat, simp)
nipkow@31719
   462
  apply (subst cong_int_def)
nipkow@31719
   463
  apply simp
nipkow@31719
   464
  apply (rule residues_prime.wilson_theorem1)
nipkow@31719
   465
  apply (rule residues_prime.intro)
nipkow@31719
   466
  apply auto
wenzelm@44872
   467
  done
nipkow@31719
   468
nipkow@31719
   469
end