src/HOL/Number_Theory/Residues.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 44872 a98ef45122f3
child 47163 248376f8881d
permissions -rw-r--r--
Quotient_Info stores only relation maps
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
(*
wenzelm@44872
    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
wenzelm@44872
    25
  "residue_ring m == (|
wenzelm@44872
    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
wenzelm@44872
    37
context residues
wenzelm@44872
    38
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)
wenzelm@41541
    44
  apply (auto simp add: mod_add_right_eq [symmetric] add_ac)
nipkow@31719
    45
  apply (case_tac "x = 0")
nipkow@31719
    46
  apply force
nipkow@31719
    47
  apply (subgoal_tac "(x + (m - x)) mod m = 0")
nipkow@31719
    48
  apply (erule bexI)
nipkow@31719
    49
  apply auto
wenzelm@41541
    50
  done
nipkow@31719
    51
nipkow@31719
    52
lemma comm_monoid: "comm_monoid R"
nipkow@31719
    53
  apply (insert m_gt_one)
nipkow@31719
    54
  apply (unfold R_def residue_ring_def)
nipkow@31719
    55
  apply (rule comm_monoidI)
nipkow@31719
    56
  apply auto
nipkow@31719
    57
  apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
nipkow@31719
    58
  apply (erule ssubst)
nipkow@31719
    59
  apply (subst zmod_zmult1_eq [symmetric])+
nipkow@31719
    60
  apply (simp_all only: mult_ac)
wenzelm@41541
    61
  done
nipkow@31719
    62
nipkow@31719
    63
lemma cring: "cring R"
nipkow@31719
    64
  apply (rule cringI)
nipkow@31719
    65
  apply (rule abelian_group)
nipkow@31719
    66
  apply (rule comm_monoid)
nipkow@31719
    67
  apply (unfold R_def residue_ring_def, auto)
nipkow@31719
    68
  apply (subst mod_add_eq [symmetric])
nipkow@31719
    69
  apply (subst mult_commute)
nipkow@31719
    70
  apply (subst zmod_zmult1_eq [symmetric])
haftmann@36350
    71
  apply (simp add: field_simps)
wenzelm@41541
    72
  done
nipkow@31719
    73
nipkow@31719
    74
end
nipkow@31719
    75
nipkow@31719
    76
sublocale residues < cring
nipkow@31719
    77
  by (rule cring)
nipkow@31719
    78
nipkow@31719
    79
wenzelm@41541
    80
context residues
wenzelm@41541
    81
begin
nipkow@31719
    82
wenzelm@44872
    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}"
wenzelm@44872
    87
  unfolding R_def residue_ring_def by auto
nipkow@31719
    88
nipkow@31719
    89
lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
wenzelm@44872
    90
  unfolding R_def residue_ring_def by auto
nipkow@31719
    91
nipkow@31719
    92
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
wenzelm@44872
    93
  unfolding R_def residue_ring_def by auto
nipkow@31719
    94
nipkow@31719
    95
lemma res_zero_eq: "\<zero> = 0"
wenzelm@44872
    96
  unfolding R_def residue_ring_def by auto
nipkow@31719
    97
nipkow@31719
    98
lemma res_one_eq: "\<one> = 1"
wenzelm@44872
    99
  unfolding R_def residue_ring_def units_of_def by 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)
wenzelm@41541
   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
wenzelm@41541
   129
  done
nipkow@31719
   130
wenzelm@44872
   131
lemma finite [iff]: "finite (carrier R)"
nipkow@31719
   132
  by (subst res_carrier_eq, auto)
nipkow@31719
   133
wenzelm@44872
   134
lemma finite_Units [iff]: "finite (Units R)"
nipkow@31719
   135
  by (subst res_units_eq, auto)
nipkow@31719
   136
wenzelm@44872
   137
(* The function a -> a mod m maps the integers to the
wenzelm@44872
   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)
wenzelm@41541
   144
  done
nipkow@31719
   145
nipkow@31719
   146
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
wenzelm@44872
   147
  unfolding R_def residue_ring_def
wenzelm@44872
   148
  apply auto
wenzelm@44872
   149
  apply presburger
wenzelm@44872
   150
  done
nipkow@31719
   151
nipkow@31719
   152
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
nipkow@31719
   153
  apply (unfold R_def residue_ring_def, auto)
nipkow@31719
   154
  apply (subst zmod_zmult1_eq [symmetric])
nipkow@31719
   155
  apply (subst mult_commute)
nipkow@31719
   156
  apply (subst zmod_zmult1_eq [symmetric])
nipkow@31719
   157
  apply (subst mult_commute)
nipkow@31719
   158
  apply auto
wenzelm@41541
   159
  done
nipkow@31719
   160
nipkow@31719
   161
lemma zero_cong: "\<zero> = 0"
wenzelm@44872
   162
  unfolding R_def residue_ring_def by auto
nipkow@31719
   163
nipkow@31719
   164
lemma one_cong: "\<one> = 1 mod m"
wenzelm@44872
   165
  using m_gt_one unfolding R_def residue_ring_def by auto
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)
wenzelm@41541
   171
  apply (auto simp add: nat_pow_def one_cong)
nipkow@31719
   172
  apply (subst mult_commute)
nipkow@31719
   173
  apply (rule mult_cong)
wenzelm@41541
   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
wenzelm@41541
   183
  done
nipkow@31719
   184
wenzelm@44872
   185
lemma (in residues) prod_cong:
wenzelm@44872
   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)
wenzelm@41541
   189
  done
nipkow@31719
   190
nipkow@31719
   191
lemma (in residues) sum_cong:
wenzelm@44872
   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)
wenzelm@41541
   195
  done
nipkow@31719
   196
wenzelm@44872
   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)
wenzelm@41541
   206
  done
nipkow@31719
   207
wenzelm@44872
   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
wenzelm@44872
   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
wenzelm@41541
   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
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:
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
wenzelm@44872
   378
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
wenzelm@44872
   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 -
wenzelm@44872
   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
wenzelm@44872
   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
wenzelm@44872
   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>".
wenzelm@44872
   447
  then show ?thesis by (simp add: res_to_cong_simps)
nipkow@31719
   448
qed
nipkow@31719
   449
nipkow@31719
   450
lemma wilson_theorem: "prime (p::int) \<Longrightarrow> [fact (p - 1) = - 1] (mod p)"
nipkow@31952
   451
  apply (frule prime_gt_1_int)
nipkow@31719
   452
  apply (case_tac "p = 2")
nipkow@31952
   453
  apply (subst fact_altdef_int, simp)
nipkow@31719
   454
  apply (subst cong_int_def)
nipkow@31719
   455
  apply simp
nipkow@31719
   456
  apply (rule residues_prime.wilson_theorem1)
nipkow@31719
   457
  apply (rule residues_prime.intro)
nipkow@31719
   458
  apply auto
wenzelm@44872
   459
  done
nipkow@31719
   460
nipkow@31719
   461
end