src/HOL/Number_Theory/Residues.thy
author haftmann
Sat Jul 05 11:01:53 2014 +0200 (2014-07-05)
changeset 57514 bdc2c6b40bf2
parent 57512 cc97b347b301
child 58889 5b7a9633cfa8
permissions -rw-r--r--
prefer ac_simps collections over separate name bindings for add and mult
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)
haftmann@57514
    43
  apply (auto simp add: mod_add_right_eq [symmetric] ac_simps)
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])+
haftmann@57514
    59
  apply (simp_all only: ac_simps)
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])
haftmann@57512
    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
lp15@55352
   106
  apply (metis invertible_coprime_int)
nipkow@31952
   107
  apply (subst (asm) coprime_iff_invertible'_int)
haftmann@57512
   108
  apply (auto simp add: cong_int_def mult.commute)
wenzelm@41541
   109
  done
nipkow@31719
   110
nipkow@31719
   111
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
nipkow@31719
   112
  apply (insert m_gt_one)
nipkow@31719
   113
  apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
nipkow@31719
   114
  apply auto
nipkow@31719
   115
  apply (rule the_equality)
nipkow@31719
   116
  apply auto
nipkow@31719
   117
  apply (subst mod_add_right_eq [symmetric])
nipkow@31719
   118
  apply auto
nipkow@31719
   119
  apply (subst mod_add_left_eq [symmetric])
nipkow@31719
   120
  apply auto
nipkow@31719
   121
  apply (subgoal_tac "y mod m = - x mod m")
nipkow@31719
   122
  apply simp
haftmann@57512
   123
  apply (metis minus_add_cancel mod_mult_self1 mult.commute)
wenzelm@41541
   124
  done
nipkow@31719
   125
wenzelm@44872
   126
lemma finite [iff]: "finite (carrier R)"
nipkow@31719
   127
  by (subst res_carrier_eq, auto)
nipkow@31719
   128
wenzelm@44872
   129
lemma finite_Units [iff]: "finite (Units R)"
bulwahn@50027
   130
  by (subst res_units_eq) auto
nipkow@31719
   131
wenzelm@44872
   132
(* The function a -> a mod m maps the integers to the
wenzelm@44872
   133
   residue classes. The following lemmas show that this mapping
nipkow@31719
   134
   respects addition and multiplication on the integers. *)
nipkow@31719
   135
nipkow@31719
   136
lemma mod_in_carrier [iff]: "a mod m : carrier R"
nipkow@31719
   137
  apply (unfold res_carrier_eq)
nipkow@31719
   138
  apply (insert m_gt_one, auto)
wenzelm@41541
   139
  done
nipkow@31719
   140
nipkow@31719
   141
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
wenzelm@44872
   142
  unfolding R_def residue_ring_def
wenzelm@44872
   143
  apply auto
wenzelm@44872
   144
  apply presburger
wenzelm@44872
   145
  done
nipkow@31719
   146
nipkow@31719
   147
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
lp15@55352
   148
  unfolding R_def residue_ring_def
lp15@55352
   149
  by auto (metis mod_mult_eq)
nipkow@31719
   150
nipkow@31719
   151
lemma zero_cong: "\<zero> = 0"
wenzelm@44872
   152
  unfolding R_def residue_ring_def by auto
nipkow@31719
   153
nipkow@31719
   154
lemma one_cong: "\<one> = 1 mod m"
wenzelm@44872
   155
  using m_gt_one unfolding R_def residue_ring_def by auto
nipkow@31719
   156
nipkow@31719
   157
(* revise algebra library to use 1? *)
nipkow@31719
   158
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
nipkow@31719
   159
  apply (insert m_gt_one)
nipkow@31719
   160
  apply (induct n)
wenzelm@41541
   161
  apply (auto simp add: nat_pow_def one_cong)
haftmann@57512
   162
  apply (metis mult.commute mult_cong)
wenzelm@41541
   163
  done
nipkow@31719
   164
nipkow@31719
   165
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
lp15@55352
   166
  by (metis mod_minus_eq res_neg_eq)
nipkow@31719
   167
wenzelm@44872
   168
lemma (in residues) prod_cong:
wenzelm@44872
   169
    "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
lp15@55352
   170
  by (induct set: finite) (auto simp: one_cong mult_cong)
nipkow@31719
   171
nipkow@31719
   172
lemma (in residues) sum_cong:
wenzelm@44872
   173
    "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
lp15@55352
   174
  by (induct set: finite) (auto simp: zero_cong add_cong)
nipkow@31719
   175
wenzelm@44872
   176
lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow>
nipkow@31719
   177
    a mod m : Units R"
nipkow@31719
   178
  apply (subst res_units_eq, auto)
nipkow@31719
   179
  apply (insert pos_mod_sign [of m a])
nipkow@31719
   180
  apply (subgoal_tac "a mod m ~= 0")
nipkow@31719
   181
  apply arith
nipkow@31719
   182
  apply auto
lp15@55352
   183
  apply (metis gcd_int.commute gcd_red_int)
wenzelm@41541
   184
  done
nipkow@31719
   185
wenzelm@44872
   186
lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))"
nipkow@31719
   187
  unfolding cong_int_def by auto
nipkow@31719
   188
wenzelm@44872
   189
(* Simplifying with these will translate a ring equation in R to a
nipkow@31719
   190
   congruence. *)
nipkow@31719
   191
nipkow@31719
   192
lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
nipkow@31719
   193
    prod_cong sum_cong neg_cong res_eq_to_cong
nipkow@31719
   194
nipkow@31719
   195
(* Other useful facts about the residue ring *)
nipkow@31719
   196
nipkow@31719
   197
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
nipkow@31719
   198
  apply (simp add: res_one_eq res_neg_eq)
haftmann@57512
   199
  apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
lp15@55352
   200
            zero_neq_one zmod_zminus1_eq_if)
wenzelm@41541
   201
  done
nipkow@31719
   202
nipkow@31719
   203
end
nipkow@31719
   204
nipkow@31719
   205
nipkow@31719
   206
(* prime residues *)
nipkow@31719
   207
nipkow@31719
   208
locale residues_prime =
lp15@55242
   209
  fixes p and R (structure)
nipkow@31719
   210
  assumes p_prime [intro]: "prime p"
nipkow@31719
   211
  defines "R == residue_ring p"
nipkow@31719
   212
nipkow@31719
   213
sublocale residues_prime < residues p
nipkow@31719
   214
  apply (unfold R_def residues_def)
nipkow@31719
   215
  using p_prime apply auto
lp15@55242
   216
  apply (metis (full_types) int_1 of_nat_less_iff prime_gt_1_nat)
wenzelm@41541
   217
  done
nipkow@31719
   218
wenzelm@44872
   219
context residues_prime
wenzelm@44872
   220
begin
nipkow@31719
   221
nipkow@31719
   222
lemma is_field: "field R"
nipkow@31719
   223
  apply (rule cring.field_intro2)
nipkow@31719
   224
  apply (rule cring)
wenzelm@44872
   225
  apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq)
nipkow@31719
   226
  apply (rule classical)
nipkow@31719
   227
  apply (erule notE)
nipkow@31952
   228
  apply (subst gcd_commute_int)
nipkow@31952
   229
  apply (rule prime_imp_coprime_int)
nipkow@31719
   230
  apply (rule p_prime)
nipkow@31719
   231
  apply (rule notI)
nipkow@31719
   232
  apply (frule zdvd_imp_le)
nipkow@31719
   233
  apply auto
wenzelm@41541
   234
  done
nipkow@31719
   235
nipkow@31719
   236
lemma res_prime_units_eq: "Units R = {1..p - 1}"
nipkow@31719
   237
  apply (subst res_units_eq)
nipkow@31719
   238
  apply auto
nipkow@31952
   239
  apply (subst gcd_commute_int)
lp15@55352
   240
  apply (auto simp add: p_prime prime_imp_coprime_int zdvd_not_zless)
wenzelm@41541
   241
  done
nipkow@31719
   242
nipkow@31719
   243
end
nipkow@31719
   244
nipkow@31719
   245
sublocale residues_prime < field
nipkow@31719
   246
  by (rule is_field)
nipkow@31719
   247
nipkow@31719
   248
nipkow@31719
   249
(*
nipkow@31719
   250
  Test cases: Euler's theorem and Wilson's theorem.
nipkow@31719
   251
*)
nipkow@31719
   252
nipkow@31719
   253
nipkow@31719
   254
subsection{* Euler's theorem *}
nipkow@31719
   255
nipkow@31719
   256
(* the definition of the phi function *)
nipkow@31719
   257
wenzelm@44872
   258
definition phi :: "int => nat"
wenzelm@44872
   259
  where "phi m = card({ x. 0 < x & x < m & gcd x m = 1})"
nipkow@31719
   260
lp15@55261
   261
lemma phi_def_nat: "phi m = card({ x. 0 < x & x < nat m & gcd x (nat m) = 1})"
lp15@55261
   262
  apply (simp add: phi_def)
lp15@55261
   263
  apply (rule bij_betw_same_card [of nat])
lp15@55261
   264
  apply (auto simp add: inj_on_def bij_betw_def image_def)
lp15@55261
   265
  apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
lp15@55261
   266
  apply (metis One_nat_def int_0 int_1 int_less_0_conv int_nat_eq nat_int transfer_int_nat_gcd(1) zless_int)
lp15@55261
   267
  done
lp15@55261
   268
lp15@55261
   269
lemma prime_phi:
lp15@55261
   270
  assumes  "2 \<le> p" "phi p = p - 1" shows "prime p"
lp15@55261
   271
proof -
lp15@55261
   272
  have "{x. 0 < x \<and> x < p \<and> coprime x p} = {1..p - 1}"
lp15@55261
   273
    using assms unfolding phi_def_nat
lp15@55261
   274
    by (intro card_seteq) fastforce+
lp15@55261
   275
  then have cop: "\<And>x. x \<in> {1::nat..p - 1} \<Longrightarrow> coprime x p"
lp15@55261
   276
    by blast
lp15@55261
   277
  { fix x::nat assume *: "1 < x" "x < p" and "x dvd p"
lp15@55261
   278
    have "coprime x p" 
lp15@55261
   279
      apply (rule cop)
lp15@55261
   280
      using * apply auto
lp15@55261
   281
      done
lp15@55261
   282
    with `x dvd p` `1 < x` have "False" by auto }
lp15@55261
   283
  then show ?thesis 
lp15@55262
   284
    using `2 \<le> p` 
lp15@55262
   285
    by (simp add: prime_def)
lp15@55352
   286
       (metis One_nat_def dvd_pos_nat nat_dvd_not_less nat_neq_iff not_gr0 
lp15@55352
   287
              not_numeral_le_zero one_dvd)
lp15@55261
   288
qed
lp15@55261
   289
nipkow@31719
   290
lemma phi_zero [simp]: "phi 0 = 0"
nipkow@31719
   291
  apply (subst phi_def)
wenzelm@44872
   292
(* Auto hangs here. Once again, where is the simplification rule
nipkow@31719
   293
   1 == Suc 0 coming from? *)
nipkow@31719
   294
  apply (auto simp add: card_eq_0_iff)
nipkow@31719
   295
(* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
wenzelm@41541
   296
  done
nipkow@31719
   297
nipkow@31719
   298
lemma phi_one [simp]: "phi 1 = 0"
wenzelm@44872
   299
  by (auto simp add: phi_def card_eq_0_iff)
nipkow@31719
   300
nipkow@31719
   301
lemma (in residues) phi_eq: "phi m = card(Units R)"
nipkow@31719
   302
  by (simp add: phi_def res_units_eq)
nipkow@31719
   303
wenzelm@44872
   304
lemma (in residues) euler_theorem1:
nipkow@31719
   305
  assumes a: "gcd a m = 1"
nipkow@31719
   306
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   307
proof -
nipkow@31719
   308
  from a m_gt_one have [simp]: "a mod m : Units R"
nipkow@31719
   309
    by (intro mod_in_res_units)
nipkow@31719
   310
  from phi_eq have "(a mod m) (^) (phi m) = (a mod m) (^) (card (Units R))"
nipkow@31719
   311
    by simp
wenzelm@44872
   312
  also have "\<dots> = \<one>"
nipkow@31719
   313
    by (intro units_power_order_eq_one, auto)
nipkow@31719
   314
  finally show ?thesis
nipkow@31719
   315
    by (simp add: res_to_cong_simps)
nipkow@31719
   316
qed
nipkow@31719
   317
nipkow@31719
   318
(* In fact, there is a two line proof!
nipkow@31719
   319
wenzelm@44872
   320
lemma (in residues) euler_theorem1:
nipkow@31719
   321
  assumes a: "gcd a m = 1"
nipkow@31719
   322
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   323
proof -
nipkow@31719
   324
  have "(a mod m) (^) (phi m) = \<one>"
nipkow@31719
   325
    by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
wenzelm@44872
   326
  then show ?thesis
nipkow@31719
   327
    by (simp add: res_to_cong_simps)
nipkow@31719
   328
qed
nipkow@31719
   329
nipkow@31719
   330
*)
nipkow@31719
   331
nipkow@31719
   332
(* outside the locale, we can relax the restriction m > 1 *)
nipkow@31719
   333
nipkow@31719
   334
lemma euler_theorem:
nipkow@31719
   335
  assumes "m >= 0" and "gcd a m = 1"
nipkow@31719
   336
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   337
proof (cases)
nipkow@31719
   338
  assume "m = 0 | m = 1"
wenzelm@44872
   339
  then show ?thesis by auto
nipkow@31719
   340
next
nipkow@31719
   341
  assume "~(m = 0 | m = 1)"
wenzelm@41541
   342
  with assms show ?thesis
nipkow@31719
   343
    by (intro residues.euler_theorem1, unfold residues_def, auto)
nipkow@31719
   344
qed
nipkow@31719
   345
nipkow@31719
   346
lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
nipkow@31719
   347
  apply (subst phi_eq)
nipkow@31719
   348
  apply (subst res_prime_units_eq)
nipkow@31719
   349
  apply auto
wenzelm@41541
   350
  done
nipkow@31719
   351
nipkow@31719
   352
lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
nipkow@31719
   353
  apply (rule residues_prime.phi_prime)
nipkow@31719
   354
  apply (erule residues_prime.intro)
wenzelm@41541
   355
  done
nipkow@31719
   356
nipkow@31719
   357
lemma fermat_theorem:
lp15@55242
   358
  fixes a::int
nipkow@31719
   359
  assumes "prime p" and "~ (p dvd a)"
lp15@55242
   360
  shows "[a^(p - 1) = 1] (mod p)"
nipkow@31719
   361
proof -
wenzelm@41541
   362
  from assms have "[a^phi p = 1] (mod p)"
nipkow@31719
   363
    apply (intro euler_theorem)
lp15@55242
   364
    apply (metis of_nat_0_le_iff)
lp15@55242
   365
    apply (metis gcd_int.commute prime_imp_coprime_int)
nipkow@31719
   366
    done
nipkow@31719
   367
  also have "phi p = nat p - 1"
wenzelm@41541
   368
    by (rule phi_prime, rule assms)
lp15@55242
   369
  finally show ?thesis
lp15@55242
   370
    by (metis nat_int) 
nipkow@31719
   371
qed
nipkow@31719
   372
lp15@55227
   373
lemma fermat_theorem_nat:
lp15@55227
   374
  assumes "prime p" and "~ (p dvd a)"
lp15@55227
   375
  shows "[a^(p - 1) = 1] (mod p)"
lp15@55227
   376
using fermat_theorem [of p a] assms
lp15@55242
   377
by (metis int_1 of_nat_power transfer_int_nat_cong zdvd_int)
lp15@55227
   378
nipkow@31719
   379
nipkow@31719
   380
subsection {* Wilson's theorem *}
nipkow@31719
   381
wenzelm@44872
   382
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
wenzelm@44872
   383
    {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}"
nipkow@31719
   384
  apply auto
lp15@55352
   385
  apply (metis Units_inv_inv)+
wenzelm@41541
   386
  done
nipkow@31719
   387
nipkow@31719
   388
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   389
  assumes a: "p > 2"
nipkow@31719
   390
  shows "[fact (p - 1) = - 1] (mod p)"
nipkow@31719
   391
proof -
wenzelm@44872
   392
  let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}"
nipkow@31732
   393
  have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
nipkow@31719
   394
    by auto
wenzelm@44872
   395
  have "(\<Otimes>i: Units R. i) =
nipkow@31719
   396
    (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
nipkow@31732
   397
    apply (subst UR)
nipkow@31719
   398
    apply (subst finprod_Un_disjoint)
lp15@55352
   399
    apply (auto intro: funcsetI)
lp15@55352
   400
    apply (metis Units_inv_inv inv_one inv_neg_one)+
nipkow@31719
   401
    done
nipkow@31719
   402
  also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   403
    apply (subst finprod_insert)
nipkow@31719
   404
    apply auto
nipkow@31719
   405
    apply (frule one_eq_neg_one)
nipkow@31719
   406
    apply (insert a, force)
nipkow@31719
   407
    done
wenzelm@44872
   408
  also have "(\<Otimes>i:(Union ?InversePairs). i) =
wenzelm@41541
   409
      (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
lp15@55352
   410
    apply (subst finprod_Union_disjoint, auto)
lp15@55352
   411
    apply (metis Units_inv_inv)+
nipkow@31719
   412
    done
nipkow@31719
   413
  also have "\<dots> = \<one>"
lp15@55352
   414
    apply (rule finprod_one, auto)
lp15@55352
   415
    apply (subst finprod_insert, auto)
lp15@55352
   416
    apply (metis inv_eq_self)
nipkow@31719
   417
    done
nipkow@31719
   418
  finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
nipkow@31719
   419
    by simp
nipkow@31719
   420
  also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
nipkow@31719
   421
    apply (rule finprod_cong')
nipkow@31732
   422
    apply (auto)
nipkow@31719
   423
    apply (subst (asm) res_prime_units_eq)
nipkow@31719
   424
    apply auto
nipkow@31719
   425
    done
nipkow@31719
   426
  also have "\<dots> = (PROD i: Units R. i) mod p"
nipkow@31719
   427
    apply (rule prod_cong)
nipkow@31719
   428
    apply auto
nipkow@31719
   429
    done
nipkow@31719
   430
  also have "\<dots> = fact (p - 1) mod p"
lp15@55242
   431
    apply (subst fact_altdef_nat)
lp15@55242
   432
    apply (insert assms)
lp15@55242
   433
    apply (subst res_prime_units_eq)
lp15@55242
   434
    apply (simp add: int_setprod zmod_int setprod_int_eq)
nipkow@31719
   435
    done
nipkow@31719
   436
  finally have "fact (p - 1) mod p = \<ominus> \<one>".
lp15@55242
   437
  then show ?thesis
lp15@55242
   438
    by (metis Divides.transfer_int_nat_functions(2) cong_int_def res_neg_eq res_one_eq)
nipkow@31719
   439
qed
nipkow@31719
   440
lp15@55352
   441
lemma wilson_theorem:
lp15@55352
   442
  assumes "prime p" shows "[fact (p - 1) = - 1] (mod p)"
lp15@55352
   443
proof (cases "p = 2")
lp15@55352
   444
  case True 
lp15@55352
   445
  then show ?thesis
lp15@55352
   446
    by (simp add: cong_int_def fact_altdef_nat)
lp15@55352
   447
next
lp15@55352
   448
  case False
lp15@55352
   449
  then show ?thesis
lp15@55352
   450
    using assms prime_ge_2_nat
lp15@55352
   451
    by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
lp15@55352
   452
qed
nipkow@31719
   453
nipkow@31719
   454
end