src/HOL/Number_Theory/Residues.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 03 00:22:48 2014 +0000 (2014-02-03)
changeset 55262 16724746ad89
parent 55261 ad3604df6bc6
child 55352 1d2852dfc4a7
permissions -rw-r--r--
fixed indentation
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
lp15@55261
   292
lemma phi_def_nat: "phi m = card({ x. 0 < x & x < nat m & gcd x (nat m) = 1})"
lp15@55261
   293
  apply (simp add: phi_def)
lp15@55261
   294
  apply (rule bij_betw_same_card [of nat])
lp15@55261
   295
  apply (auto simp add: inj_on_def bij_betw_def image_def)
lp15@55261
   296
  apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
lp15@55261
   297
  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
   298
  done
lp15@55261
   299
lp15@55261
   300
lemma prime_phi:
lp15@55261
   301
  assumes  "2 \<le> p" "phi p = p - 1" shows "prime p"
lp15@55261
   302
proof -
lp15@55261
   303
  have "{x. 0 < x \<and> x < p \<and> coprime x p} = {1..p - 1}"
lp15@55261
   304
    using assms unfolding phi_def_nat
lp15@55261
   305
    by (intro card_seteq) fastforce+
lp15@55261
   306
  then have cop: "\<And>x. x \<in> {1::nat..p - 1} \<Longrightarrow> coprime x p"
lp15@55261
   307
    by blast
lp15@55261
   308
  { fix x::nat assume *: "1 < x" "x < p" and "x dvd p"
lp15@55261
   309
    have "coprime x p" 
lp15@55261
   310
      apply (rule cop)
lp15@55261
   311
      using * apply auto
lp15@55261
   312
      done
lp15@55261
   313
    with `x dvd p` `1 < x` have "False" by auto }
lp15@55261
   314
  then show ?thesis 
lp15@55262
   315
    using `2 \<le> p` 
lp15@55262
   316
    by (simp add: prime_def)
lp15@55262
   317
       (metis One_nat_def dvd_pos_nat nat_dvd_not_less nat_neq_iff not_gr0 not_numeral_le_zero one_dvd)
lp15@55261
   318
qed
lp15@55261
   319
nipkow@31719
   320
lemma phi_zero [simp]: "phi 0 = 0"
nipkow@31719
   321
  apply (subst phi_def)
wenzelm@44872
   322
(* Auto hangs here. Once again, where is the simplification rule
nipkow@31719
   323
   1 == Suc 0 coming from? *)
nipkow@31719
   324
  apply (auto simp add: card_eq_0_iff)
nipkow@31719
   325
(* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
wenzelm@41541
   326
  done
nipkow@31719
   327
nipkow@31719
   328
lemma phi_one [simp]: "phi 1 = 0"
wenzelm@44872
   329
  by (auto simp add: phi_def card_eq_0_iff)
nipkow@31719
   330
nipkow@31719
   331
lemma (in residues) phi_eq: "phi m = card(Units R)"
nipkow@31719
   332
  by (simp add: phi_def res_units_eq)
nipkow@31719
   333
wenzelm@44872
   334
lemma (in residues) euler_theorem1:
nipkow@31719
   335
  assumes a: "gcd a m = 1"
nipkow@31719
   336
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   337
proof -
nipkow@31719
   338
  from a m_gt_one have [simp]: "a mod m : Units R"
nipkow@31719
   339
    by (intro mod_in_res_units)
nipkow@31719
   340
  from phi_eq have "(a mod m) (^) (phi m) = (a mod m) (^) (card (Units R))"
nipkow@31719
   341
    by simp
wenzelm@44872
   342
  also have "\<dots> = \<one>"
nipkow@31719
   343
    by (intro units_power_order_eq_one, auto)
nipkow@31719
   344
  finally show ?thesis
nipkow@31719
   345
    by (simp add: res_to_cong_simps)
nipkow@31719
   346
qed
nipkow@31719
   347
nipkow@31719
   348
(* In fact, there is a two line proof!
nipkow@31719
   349
wenzelm@44872
   350
lemma (in residues) euler_theorem1:
nipkow@31719
   351
  assumes a: "gcd a m = 1"
nipkow@31719
   352
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   353
proof -
nipkow@31719
   354
  have "(a mod m) (^) (phi m) = \<one>"
nipkow@31719
   355
    by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
wenzelm@44872
   356
  then show ?thesis
nipkow@31719
   357
    by (simp add: res_to_cong_simps)
nipkow@31719
   358
qed
nipkow@31719
   359
nipkow@31719
   360
*)
nipkow@31719
   361
nipkow@31719
   362
(* outside the locale, we can relax the restriction m > 1 *)
nipkow@31719
   363
nipkow@31719
   364
lemma euler_theorem:
nipkow@31719
   365
  assumes "m >= 0" and "gcd a m = 1"
nipkow@31719
   366
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   367
proof (cases)
nipkow@31719
   368
  assume "m = 0 | m = 1"
wenzelm@44872
   369
  then show ?thesis by auto
nipkow@31719
   370
next
nipkow@31719
   371
  assume "~(m = 0 | m = 1)"
wenzelm@41541
   372
  with assms show ?thesis
nipkow@31719
   373
    by (intro residues.euler_theorem1, unfold residues_def, auto)
nipkow@31719
   374
qed
nipkow@31719
   375
nipkow@31719
   376
lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
nipkow@31719
   377
  apply (subst phi_eq)
nipkow@31719
   378
  apply (subst res_prime_units_eq)
nipkow@31719
   379
  apply auto
wenzelm@41541
   380
  done
nipkow@31719
   381
nipkow@31719
   382
lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
nipkow@31719
   383
  apply (rule residues_prime.phi_prime)
nipkow@31719
   384
  apply (erule residues_prime.intro)
wenzelm@41541
   385
  done
nipkow@31719
   386
nipkow@31719
   387
lemma fermat_theorem:
lp15@55242
   388
  fixes a::int
nipkow@31719
   389
  assumes "prime p" and "~ (p dvd a)"
lp15@55242
   390
  shows "[a^(p - 1) = 1] (mod p)"
nipkow@31719
   391
proof -
wenzelm@41541
   392
  from assms have "[a^phi p = 1] (mod p)"
nipkow@31719
   393
    apply (intro euler_theorem)
lp15@55242
   394
    apply (metis of_nat_0_le_iff)
lp15@55242
   395
    apply (metis gcd_int.commute prime_imp_coprime_int)
nipkow@31719
   396
    done
nipkow@31719
   397
  also have "phi p = nat p - 1"
wenzelm@41541
   398
    by (rule phi_prime, rule assms)
lp15@55242
   399
  finally show ?thesis
lp15@55242
   400
    by (metis nat_int) 
nipkow@31719
   401
qed
nipkow@31719
   402
lp15@55227
   403
lemma fermat_theorem_nat:
lp15@55227
   404
  assumes "prime p" and "~ (p dvd a)"
lp15@55227
   405
  shows "[a^(p - 1) = 1] (mod p)"
lp15@55227
   406
using fermat_theorem [of p a] assms
lp15@55242
   407
by (metis int_1 of_nat_power transfer_int_nat_cong zdvd_int)
lp15@55227
   408
nipkow@31719
   409
nipkow@31719
   410
subsection {* Wilson's theorem *}
nipkow@31719
   411
wenzelm@44872
   412
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
wenzelm@44872
   413
    {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}"
nipkow@31719
   414
  apply auto
nipkow@31719
   415
  apply (erule notE)
nipkow@31719
   416
  apply (erule inv_eq_imp_eq)
nipkow@31719
   417
  apply auto
nipkow@31719
   418
  apply (erule notE)
nipkow@31719
   419
  apply (erule inv_eq_imp_eq)
nipkow@31719
   420
  apply auto
wenzelm@41541
   421
  done
nipkow@31719
   422
nipkow@31719
   423
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   424
  assumes a: "p > 2"
nipkow@31719
   425
  shows "[fact (p - 1) = - 1] (mod p)"
nipkow@31719
   426
proof -
wenzelm@44872
   427
  let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}"
nipkow@31732
   428
  have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
nipkow@31719
   429
    by auto
wenzelm@44872
   430
  have "(\<Otimes>i: Units R. i) =
nipkow@31719
   431
    (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
nipkow@31732
   432
    apply (subst UR)
nipkow@31719
   433
    apply (subst finprod_Un_disjoint)
nipkow@31732
   434
    apply (auto intro:funcsetI)
nipkow@31719
   435
    apply (drule sym, subst (asm) inv_eq_one_eq)
nipkow@31719
   436
    apply auto
nipkow@31719
   437
    apply (drule sym, subst (asm) inv_eq_neg_one_eq)
nipkow@31719
   438
    apply auto
nipkow@31719
   439
    done
nipkow@31719
   440
  also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   441
    apply (subst finprod_insert)
nipkow@31719
   442
    apply auto
nipkow@31719
   443
    apply (frule one_eq_neg_one)
nipkow@31719
   444
    apply (insert a, force)
nipkow@31719
   445
    done
wenzelm@44872
   446
  also have "(\<Otimes>i:(Union ?InversePairs). i) =
wenzelm@41541
   447
      (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
nipkow@31719
   448
    apply (subst finprod_Union_disjoint)
nipkow@31719
   449
    apply force
nipkow@31719
   450
    apply force
nipkow@31719
   451
    apply clarify
nipkow@31719
   452
    apply (rule inv_pair_lemma)
nipkow@31719
   453
    apply auto
nipkow@31719
   454
    done
nipkow@31719
   455
  also have "\<dots> = \<one>"
nipkow@31719
   456
    apply (rule finprod_one)
nipkow@31719
   457
    apply auto
nipkow@31719
   458
    apply (subst finprod_insert)
nipkow@31719
   459
    apply auto
nipkow@31719
   460
    apply (frule inv_eq_self)
nipkow@31732
   461
    apply (auto)
nipkow@31719
   462
    done
nipkow@31719
   463
  finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
nipkow@31719
   464
    by simp
nipkow@31719
   465
  also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
nipkow@31719
   466
    apply (rule finprod_cong')
nipkow@31732
   467
    apply (auto)
nipkow@31719
   468
    apply (subst (asm) res_prime_units_eq)
nipkow@31719
   469
    apply auto
nipkow@31719
   470
    done
nipkow@31719
   471
  also have "\<dots> = (PROD i: Units R. i) mod p"
nipkow@31719
   472
    apply (rule prod_cong)
nipkow@31719
   473
    apply auto
nipkow@31719
   474
    done
nipkow@31719
   475
  also have "\<dots> = fact (p - 1) mod p"
lp15@55242
   476
    apply (subst fact_altdef_nat)
lp15@55242
   477
    apply (insert assms)
lp15@55242
   478
    apply (subst res_prime_units_eq)
lp15@55242
   479
    apply (simp add: int_setprod zmod_int setprod_int_eq)
nipkow@31719
   480
    done
nipkow@31719
   481
  finally have "fact (p - 1) mod p = \<ominus> \<one>".
lp15@55242
   482
  then show ?thesis
lp15@55242
   483
    by (metis Divides.transfer_int_nat_functions(2) cong_int_def res_neg_eq res_one_eq)
nipkow@31719
   484
qed
nipkow@31719
   485
lp15@55242
   486
lemma wilson_theorem: "prime p \<Longrightarrow> [fact (p - 1) = - 1] (mod p)"
lp15@55242
   487
  apply (frule prime_gt_1_nat)
nipkow@31719
   488
  apply (case_tac "p = 2")
lp15@55242
   489
  apply (subst fact_altdef_nat, simp)
nipkow@31719
   490
  apply (subst cong_int_def)
nipkow@31719
   491
  apply simp
nipkow@31719
   492
  apply (rule residues_prime.wilson_theorem1)
nipkow@31719
   493
  apply (rule residues_prime.intro)
nipkow@31719
   494
  apply auto
wenzelm@44872
   495
  done
nipkow@31719
   496
nipkow@31719
   497
end