src/HOL/Number_Theory/Residues.thy
author haftmann
Thu Apr 06 08:33:37 2017 +0200 (2017-04-06)
changeset 65416 f707dbcf11e3
parent 65066 c64d778a593a
child 65465 067210a08a22
permissions -rw-r--r--
more approproiate placement of theories MiscAlgebra and Multiplicate_Group
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
wenzelm@60526
     8
section \<open>Residue rings\<close>
nipkow@31719
     9
nipkow@31719
    10
theory Residues
haftmann@65416
    11
imports
haftmann@65416
    12
  Cong
haftmann@65416
    13
  "~~/src/HOL/Algebra/More_Group"
haftmann@65416
    14
  "~~/src/HOL/Algebra/More_Ring"
haftmann@65416
    15
  "~~/src/HOL/Algebra/More_Finite_Product"
haftmann@65416
    16
  "~~/src/HOL/Algebra/Multiplicative_Group"
nipkow@31719
    17
begin
nipkow@31719
    18
eberlm@64282
    19
definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool" where
eberlm@64282
    20
  "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))"
eberlm@64282
    21
eberlm@64282
    22
definition Legendre :: "int \<Rightarrow> int \<Rightarrow> int" where
eberlm@64282
    23
  "Legendre a p = (if ([a = 0] (mod p)) then 0
eberlm@64282
    24
    else if QuadRes p a then 1
eberlm@64282
    25
    else -1)"
eberlm@64282
    26
wenzelm@60527
    27
subsection \<open>A locale for residue rings\<close>
nipkow@31719
    28
wenzelm@60527
    29
definition residue_ring :: "int \<Rightarrow> int ring"
wenzelm@60528
    30
where
wenzelm@60527
    31
  "residue_ring m =
wenzelm@60527
    32
    \<lparr>carrier = {0..m - 1},
wenzelm@60527
    33
     mult = \<lambda>x y. (x * y) mod m,
wenzelm@60527
    34
     one = 1,
wenzelm@60527
    35
     zero = 0,
wenzelm@60527
    36
     add = \<lambda>x y. (x + y) mod m\<rparr>"
nipkow@31719
    37
nipkow@31719
    38
locale residues =
nipkow@31719
    39
  fixes m :: int and R (structure)
nipkow@31719
    40
  assumes m_gt_one: "m > 1"
wenzelm@60527
    41
  defines "R \<equiv> residue_ring m"
wenzelm@44872
    42
begin
nipkow@31719
    43
nipkow@31719
    44
lemma abelian_group: "abelian_group R"
lp15@65066
    45
proof -
lp15@65066
    46
  have "\<exists>y\<in>{0..m - 1}. (x + y) mod m = 0" if "0 \<le> x" "x < m" for x
lp15@65066
    47
  proof (cases "x = 0")
lp15@65066
    48
    case True
lp15@65066
    49
    with m_gt_one show ?thesis by simp
lp15@65066
    50
  next
lp15@65066
    51
    case False
lp15@65066
    52
    then have "(x + (m - x)) mod m = 0"
lp15@65066
    53
      by simp
lp15@65066
    54
    with m_gt_one that show ?thesis
lp15@65066
    55
      by (metis False atLeastAtMost_iff diff_ge_0_iff_ge diff_left_mono int_one_le_iff_zero_less less_le)
lp15@65066
    56
  qed
lp15@65066
    57
  with m_gt_one show ?thesis
lp15@65066
    58
    by (fastforce simp add: R_def residue_ring_def mod_add_right_eq ac_simps  intro!: abelian_groupI)
lp15@65066
    59
qed    
nipkow@31719
    60
nipkow@31719
    61
lemma comm_monoid: "comm_monoid R"
lp15@65066
    62
  unfolding R_def residue_ring_def
nipkow@31719
    63
  apply (rule comm_monoidI)
lp15@65066
    64
    using m_gt_one  apply auto
lp15@65066
    65
  apply (metis mod_mult_right_eq mult.assoc mult.commute)
lp15@65066
    66
  apply (metis mult.commute)
wenzelm@41541
    67
  done
nipkow@31719
    68
nipkow@31719
    69
lemma cring: "cring R"
lp15@65066
    70
  apply (intro cringI abelian_group comm_monoid)
lp15@65066
    71
  unfolding R_def residue_ring_def
lp15@65066
    72
  apply (auto simp add: comm_semiring_class.distrib mod_add_eq mod_mult_left_eq)
wenzelm@41541
    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
wenzelm@41541
    81
context residues
wenzelm@41541
    82
begin
nipkow@31719
    83
wenzelm@60527
    84
text \<open>
wenzelm@60527
    85
  These lemmas translate back and forth between internal and
wenzelm@60527
    86
  external concepts.
wenzelm@60527
    87
\<close>
nipkow@31719
    88
nipkow@31719
    89
lemma res_carrier_eq: "carrier R = {0..m - 1}"
wenzelm@44872
    90
  unfolding R_def residue_ring_def by auto
nipkow@31719
    91
nipkow@31719
    92
lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
wenzelm@44872
    93
  unfolding R_def residue_ring_def by auto
nipkow@31719
    94
nipkow@31719
    95
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
wenzelm@44872
    96
  unfolding R_def residue_ring_def by auto
nipkow@31719
    97
nipkow@31719
    98
lemma res_zero_eq: "\<zero> = 0"
wenzelm@44872
    99
  unfolding R_def residue_ring_def by auto
nipkow@31719
   100
nipkow@31719
   101
lemma res_one_eq: "\<one> = 1"
wenzelm@44872
   102
  unfolding R_def residue_ring_def units_of_def by auto
nipkow@31719
   103
wenzelm@60527
   104
lemma res_units_eq: "Units R = {x. 0 < x \<and> x < m \<and> coprime x m}"
lp15@65066
   105
  using m_gt_one
lp15@65066
   106
  unfolding Units_def R_def residue_ring_def
nipkow@31719
   107
  apply auto
wenzelm@60527
   108
  apply (subgoal_tac "x \<noteq> 0")
nipkow@31719
   109
  apply auto
lp15@55352
   110
  apply (metis invertible_coprime_int)
nipkow@31952
   111
  apply (subst (asm) coprime_iff_invertible'_int)
haftmann@57512
   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"
lp15@65066
   116
  using m_gt_one unfolding R_def a_inv_def m_inv_def residue_ring_def
lp15@65066
   117
  apply simp
nipkow@31719
   118
  apply (rule the_equality)
lp15@65066
   119
  apply (simp add: mod_add_right_eq)
lp15@65066
   120
  apply (simp add: add.commute mod_add_right_eq)
lp15@65066
   121
  apply (metis add.right_neutral minus_add_cancel mod_add_right_eq mod_pos_pos_trivial)
wenzelm@41541
   122
  done
nipkow@31719
   123
wenzelm@44872
   124
lemma finite [iff]: "finite (carrier R)"
haftmann@65416
   125
  by (simp add: res_carrier_eq)
nipkow@31719
   126
wenzelm@44872
   127
lemma finite_Units [iff]: "finite (Units R)"
haftmann@65416
   128
  by (simp add: finite_ring_finite_units)
nipkow@31719
   129
wenzelm@60527
   130
text \<open>
wenzelm@63167
   131
  The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
wenzelm@60527
   132
  residue classes. The following lemmas show that this mapping
wenzelm@60527
   133
  respects addition and multiplication on the integers.
wenzelm@60527
   134
\<close>
nipkow@31719
   135
wenzelm@60527
   136
lemma mod_in_carrier [iff]: "a mod m \<in> carrier R"
wenzelm@60527
   137
  unfolding res_carrier_eq
wenzelm@60527
   138
  using insert m_gt_one by auto
nipkow@31719
   139
nipkow@31719
   140
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
wenzelm@44872
   141
  unfolding R_def residue_ring_def
haftmann@64593
   142
  by (auto simp add: mod_simps)
nipkow@31719
   143
nipkow@31719
   144
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
lp15@55352
   145
  unfolding R_def residue_ring_def
haftmann@64593
   146
  by (auto simp add: mod_simps)
nipkow@31719
   147
nipkow@31719
   148
lemma zero_cong: "\<zero> = 0"
wenzelm@44872
   149
  unfolding R_def residue_ring_def by auto
nipkow@31719
   150
nipkow@31719
   151
lemma one_cong: "\<one> = 1 mod m"
wenzelm@44872
   152
  using m_gt_one unfolding R_def residue_ring_def by auto
nipkow@31719
   153
wenzelm@60527
   154
(* FIXME revise algebra library to use 1? *)
nipkow@31719
   155
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
lp15@65066
   156
  using m_gt_one
nipkow@31719
   157
  apply (induct n)
wenzelm@41541
   158
  apply (auto simp add: nat_pow_def one_cong)
haftmann@57512
   159
  apply (metis mult.commute mult_cong)
wenzelm@41541
   160
  done
nipkow@31719
   161
nipkow@31719
   162
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
lp15@55352
   163
  by (metis mod_minus_eq res_neg_eq)
nipkow@31719
   164
wenzelm@60528
   165
lemma (in residues) prod_cong: "finite A \<Longrightarrow> (\<Otimes>i\<in>A. (f i) mod m) = (\<Prod>i\<in>A. f i) mod m"
lp15@55352
   166
  by (induct set: finite) (auto simp: one_cong mult_cong)
nipkow@31719
   167
wenzelm@60528
   168
lemma (in residues) sum_cong: "finite A \<Longrightarrow> (\<Oplus>i\<in>A. (f i) mod m) = (\<Sum>i\<in>A. f i) mod m"
lp15@55352
   169
  by (induct set: finite) (auto simp: zero_cong add_cong)
nipkow@31719
   170
haftmann@60688
   171
lemma mod_in_res_units [simp]:
haftmann@60688
   172
  assumes "1 < m" and "coprime a m"
haftmann@60688
   173
  shows "a mod m \<in> Units R"
haftmann@60688
   174
proof (cases "a mod m = 0")
haftmann@60688
   175
  case True with assms show ?thesis
haftmann@60688
   176
    by (auto simp add: res_units_eq gcd_red_int [symmetric])
haftmann@60688
   177
next
haftmann@60688
   178
  case False
haftmann@60688
   179
  from assms have "0 < m" by simp
haftmann@60688
   180
  with pos_mod_sign [of m a] have "0 \<le> a mod m" .
haftmann@60688
   181
  with False have "0 < a mod m" by simp
haftmann@60688
   182
  with assms show ?thesis
haftmann@60688
   183
    by (auto simp add: res_units_eq gcd_red_int [symmetric] ac_simps)
haftmann@60688
   184
qed
nipkow@31719
   185
wenzelm@60528
   186
lemma res_eq_to_cong: "(a mod m) = (b mod m) \<longleftrightarrow> [a = b] (mod m)"
nipkow@31719
   187
  unfolding cong_int_def by auto
nipkow@31719
   188
nipkow@31719
   189
wenzelm@60528
   190
text \<open>Simplifying with these will translate a ring equation in R to a congruence.\<close>
nipkow@31719
   191
lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
nipkow@31719
   192
    prod_cong sum_cong neg_cong res_eq_to_cong
nipkow@31719
   193
wenzelm@60527
   194
text \<open>Other useful facts about the residue ring.\<close>
nipkow@31719
   195
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
nipkow@31719
   196
  apply (simp add: res_one_eq res_neg_eq)
haftmann@57512
   197
  apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
wenzelm@60528
   198
    zero_neq_one zmod_zminus1_eq_if)
wenzelm@41541
   199
  done
nipkow@31719
   200
nipkow@31719
   201
end
nipkow@31719
   202
nipkow@31719
   203
wenzelm@60527
   204
subsection \<open>Prime residues\<close>
nipkow@31719
   205
nipkow@31719
   206
locale residues_prime =
eberlm@63534
   207
  fixes p :: nat and R (structure)
nipkow@31719
   208
  assumes p_prime [intro]: "prime p"
eberlm@63534
   209
  defines "R \<equiv> residue_ring (int p)"
nipkow@31719
   210
nipkow@31719
   211
sublocale residues_prime < residues p
lp15@65066
   212
  unfolding R_def residues_def
nipkow@31719
   213
  using p_prime apply auto
haftmann@62348
   214
  apply (metis (full_types) of_nat_1 of_nat_less_iff prime_gt_1_nat)
wenzelm@41541
   215
  done
nipkow@31719
   216
wenzelm@44872
   217
context residues_prime
wenzelm@44872
   218
begin
nipkow@31719
   219
nipkow@31719
   220
lemma is_field: "field R"
lp15@65066
   221
proof -
lp15@65066
   222
  have "\<And>x. \<lbrakk>gcd x (int p) \<noteq> 1; 0 \<le> x; x < int p\<rbrakk> \<Longrightarrow> x = 0"
lp15@65066
   223
    by (metis dual_order.order_iff_strict gcd.commute less_le_not_le p_prime prime_imp_coprime prime_nat_int_transfer zdvd_imp_le)
lp15@65066
   224
  then show ?thesis
lp15@65066
   225
  apply (intro cring.field_intro2 cring)
wenzelm@44872
   226
  apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq)
lp15@65066
   227
    done
lp15@65066
   228
qed
nipkow@31719
   229
nipkow@31719
   230
lemma res_prime_units_eq: "Units R = {1..p - 1}"
nipkow@31719
   231
  apply (subst res_units_eq)
nipkow@31719
   232
  apply auto
haftmann@62348
   233
  apply (subst gcd.commute)
lp15@55352
   234
  apply (auto simp add: p_prime prime_imp_coprime_int zdvd_not_zless)
wenzelm@41541
   235
  done
nipkow@31719
   236
nipkow@31719
   237
end
nipkow@31719
   238
nipkow@31719
   239
sublocale residues_prime < field
nipkow@31719
   240
  by (rule is_field)
nipkow@31719
   241
nipkow@31719
   242
wenzelm@60527
   243
section \<open>Test cases: Euler's theorem and Wilson's theorem\<close>
nipkow@31719
   244
wenzelm@60527
   245
subsection \<open>Euler's theorem\<close>
nipkow@31719
   246
lp15@65066
   247
text \<open>The definition of the totient function.\<close>
nipkow@31719
   248
wenzelm@60527
   249
definition phi :: "int \<Rightarrow> nat"
lp15@65066
   250
  where "phi m = card {x. 0 < x \<and> x < m \<and> coprime x m}"
nipkow@31719
   251
lp15@65066
   252
lemma phi_def_nat: "phi m = card {x. 0 < x \<and> x < nat m \<and> coprime x (nat m)}"
lp15@65066
   253
  unfolding phi_def
lp15@65066
   254
proof (rule bij_betw_same_card [of nat])
lp15@65066
   255
  show "bij_betw nat {x. 0 < x \<and> x < m \<and> coprime x m} {x. 0 < x \<and> x < nat m \<and> coprime x (nat m)}"
lp15@65066
   256
    apply (auto simp add: inj_on_def bij_betw_def image_def)
lp15@65066
   257
     apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
lp15@65066
   258
    apply (metis One_nat_def of_nat_0 of_nat_1 of_nat_less_0_iff int_nat_eq nat_int
lp15@65066
   259
        transfer_int_nat_gcd(1) of_nat_less_iff)
lp15@65066
   260
    done
lp15@65066
   261
qed
lp15@65066
   262
  
lp15@55261
   263
lemma prime_phi:
wenzelm@60527
   264
  assumes "2 \<le> p" "phi p = p - 1"
wenzelm@60527
   265
  shows "prime p"
lp15@55261
   266
proof -
wenzelm@60528
   267
  have *: "{x. 0 < x \<and> x < p \<and> coprime x p} = {1..p - 1}"
lp15@55261
   268
    using assms unfolding phi_def_nat
lp15@55261
   269
    by (intro card_seteq) fastforce+
wenzelm@60528
   270
  have False if **: "1 < x" "x < p" and "x dvd p" for x :: nat
wenzelm@60527
   271
  proof -
wenzelm@60528
   272
    from * have cop: "x \<in> {1..p - 1} \<Longrightarrow> coprime x p"
wenzelm@60528
   273
      by blast
lp15@59667
   274
    have "coprime x p"
lp15@55261
   275
      apply (rule cop)
wenzelm@60528
   276
      using ** apply auto
lp15@55261
   277
      done
wenzelm@60527
   278
    with \<open>x dvd p\<close> \<open>1 < x\<close> show ?thesis
wenzelm@60527
   279
      by auto
wenzelm@60527
   280
  qed
lp15@59667
   281
  then show ?thesis
wenzelm@60526
   282
    using \<open>2 \<le> p\<close>
eberlm@63633
   283
    by (simp add: prime_nat_iff)
lp15@59667
   284
       (metis One_nat_def dvd_pos_nat nat_dvd_not_less nat_neq_iff not_gr0
lp15@55352
   285
              not_numeral_le_zero one_dvd)
lp15@55261
   286
qed
lp15@55261
   287
nipkow@31719
   288
lemma phi_zero [simp]: "phi 0 = 0"
lp15@65066
   289
  unfolding phi_def by (auto simp add: card_eq_0_iff)
nipkow@31719
   290
nipkow@31719
   291
lemma phi_one [simp]: "phi 1 = 0"
wenzelm@44872
   292
  by (auto simp add: phi_def card_eq_0_iff)
nipkow@31719
   293
haftmann@65416
   294
lemma phi_leq: "phi x \<le> nat x - 1"
haftmann@65416
   295
proof -
haftmann@65416
   296
  have "phi x \<le> card {1..x - 1}"
haftmann@65416
   297
    unfolding phi_def by (rule card_mono) auto
haftmann@65416
   298
  then show ?thesis by simp
haftmann@65416
   299
qed
haftmann@65416
   300
haftmann@65416
   301
lemma phi_nonzero:
haftmann@65416
   302
  "phi x > 0" if "2 \<le> x"
haftmann@65416
   303
proof -
haftmann@65416
   304
  have "finite {y. 0 < y \<and> y < x}"
haftmann@65416
   305
    by simp
haftmann@65416
   306
  then have "finite {y. 0 < y \<and> y < x \<and> coprime y x}"
haftmann@65416
   307
    by (auto intro: rev_finite_subset)
haftmann@65416
   308
  moreover have "1 \<in> {y. 0 < y \<and> y < x \<and> coprime y x}"
haftmann@65416
   309
    using that by simp
haftmann@65416
   310
  ultimately have "card {y. 0 < y \<and> y < x \<and> coprime y x} \<noteq> 0"
haftmann@65416
   311
    by auto
haftmann@65416
   312
  then show ?thesis
haftmann@65416
   313
    by (simp add: phi_def)
haftmann@65416
   314
qed
haftmann@65416
   315
wenzelm@60527
   316
lemma (in residues) phi_eq: "phi m = card (Units R)"
nipkow@31719
   317
  by (simp add: phi_def res_units_eq)
nipkow@31719
   318
wenzelm@44872
   319
lemma (in residues) euler_theorem1:
nipkow@31719
   320
  assumes a: "gcd a m = 1"
nipkow@31719
   321
  shows "[a^phi m = 1] (mod m)"
nipkow@31719
   322
proof -
lp15@65066
   323
  have "a ^ phi m mod m = 1 mod m"
lp15@65066
   324
    by (metis assms finite_Units m_gt_one mod_in_res_units one_cong phi_eq pow_cong units_power_order_eq_one)
lp15@65066
   325
  then show ?thesis
lp15@65066
   326
    using res_eq_to_cong by blast
nipkow@31719
   327
qed
nipkow@31719
   328
wenzelm@63167
   329
text \<open>Outside the locale, we can relax the restriction \<open>m > 1\<close>.\<close>
nipkow@31719
   330
lemma euler_theorem:
wenzelm@60527
   331
  assumes "m \<ge> 0"
wenzelm@60527
   332
    and "gcd a m = 1"
nipkow@31719
   333
  shows "[a^phi m = 1] (mod m)"
wenzelm@60527
   334
proof (cases "m = 0 | m = 1")
wenzelm@60527
   335
  case True
wenzelm@44872
   336
  then show ?thesis by auto
nipkow@31719
   337
next
wenzelm@60527
   338
  case False
wenzelm@41541
   339
  with assms show ?thesis
nipkow@31719
   340
    by (intro residues.euler_theorem1, unfold residues_def, auto)
nipkow@31719
   341
qed
nipkow@31719
   342
wenzelm@60527
   343
lemma (in residues_prime) phi_prime: "phi p = nat p - 1"
lp15@65066
   344
  by (simp add: residues.phi_eq residues_axioms residues_prime.res_prime_units_eq residues_prime_axioms)
nipkow@31719
   345
eberlm@63534
   346
lemma phi_prime: "prime (int p) \<Longrightarrow> phi p = nat p - 1"
lp15@65066
   347
  by (simp add: residues_prime.intro residues_prime.phi_prime)
nipkow@31719
   348
nipkow@31719
   349
lemma fermat_theorem:
wenzelm@60527
   350
  fixes a :: int
eberlm@63534
   351
  assumes "prime (int p)"
wenzelm@60527
   352
    and "\<not> p dvd a"
lp15@55242
   353
  shows "[a^(p - 1) = 1] (mod p)"
nipkow@31719
   354
proof -
wenzelm@60527
   355
  from assms have "[a ^ phi p = 1] (mod p)"
eberlm@63534
   356
    by (auto intro!: euler_theorem intro!: prime_imp_coprime_int[of p]
eberlm@63534
   357
             simp: gcd.commute[of _ "int p"])
nipkow@31719
   358
  also have "phi p = nat p - 1"
wenzelm@60527
   359
    by (rule phi_prime) (rule assms)
lp15@55242
   360
  finally show ?thesis
lp15@59667
   361
    by (metis nat_int)
nipkow@31719
   362
qed
nipkow@31719
   363
lp15@55227
   364
lemma fermat_theorem_nat:
eberlm@63534
   365
  assumes "prime (int p)" and "\<not> p dvd a"
wenzelm@60527
   366
  shows "[a ^ (p - 1) = 1] (mod p)"
wenzelm@60527
   367
  using fermat_theorem [of p a] assms
haftmann@62348
   368
  by (metis of_nat_1 of_nat_power transfer_int_nat_cong zdvd_int)
lp15@55227
   369
nipkow@31719
   370
wenzelm@60526
   371
subsection \<open>Wilson's theorem\<close>
nipkow@31719
   372
wenzelm@60527
   373
lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow>
wenzelm@60527
   374
    {x, inv x} \<noteq> {y, inv y} \<Longrightarrow> {x, inv x} \<inter> {y, inv y} = {}"
nipkow@31719
   375
  apply auto
lp15@55352
   376
  apply (metis Units_inv_inv)+
wenzelm@41541
   377
  done
nipkow@31719
   378
nipkow@31719
   379
lemma (in residues_prime) wilson_theorem1:
nipkow@31719
   380
  assumes a: "p > 2"
lp15@59730
   381
  shows "[fact (p - 1) = (-1::int)] (mod p)"
nipkow@31719
   382
proof -
wenzelm@60527
   383
  let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}"
wenzelm@60527
   384
  have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs"
nipkow@31719
   385
    by auto
wenzelm@60527
   386
  have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i\<in>\<Union>?Inverse_Pairs. i)"
nipkow@31732
   387
    apply (subst UR)
nipkow@31719
   388
    apply (subst finprod_Un_disjoint)
lp15@55352
   389
    apply (auto intro: funcsetI)
wenzelm@60527
   390
    using inv_one apply auto[1]
wenzelm@60527
   391
    using inv_eq_neg_one_eq apply auto
nipkow@31719
   392
    done
wenzelm@60527
   393
  also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
nipkow@31719
   394
    apply (subst finprod_insert)
nipkow@31719
   395
    apply auto
nipkow@31719
   396
    apply (frule one_eq_neg_one)
wenzelm@60527
   397
    using a apply force
nipkow@31719
   398
    done
wenzelm@60527
   399
  also have "(\<Otimes>i\<in>(\<Union>?Inverse_Pairs). i) = (\<Otimes>A\<in>?Inverse_Pairs. (\<Otimes>y\<in>A. y))"
wenzelm@60527
   400
    apply (subst finprod_Union_disjoint)
wenzelm@60527
   401
    apply auto
lp15@55352
   402
    apply (metis Units_inv_inv)+
nipkow@31719
   403
    done
nipkow@31719
   404
  also have "\<dots> = \<one>"
wenzelm@60527
   405
    apply (rule finprod_one)
wenzelm@60527
   406
    apply auto
wenzelm@60527
   407
    apply (subst finprod_insert)
wenzelm@60527
   408
    apply auto
lp15@55352
   409
    apply (metis inv_eq_self)
nipkow@31719
   410
    done
wenzelm@60527
   411
  finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>"
nipkow@31719
   412
    by simp
wenzelm@60527
   413
  also have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>Units R. i mod p)"
lp15@65066
   414
    by (rule finprod_cong') (auto simp: res_units_eq)
wenzelm@60527
   415
  also have "\<dots> = (\<Prod>i\<in>Units R. i) mod p"
lp15@65066
   416
    by (rule prod_cong) auto
nipkow@31719
   417
  also have "\<dots> = fact (p - 1) mod p"
nipkow@64272
   418
    apply (simp add: fact_prod)
lp15@65066
   419
    using assms
lp15@55242
   420
    apply (subst res_prime_units_eq)
nipkow@64272
   421
    apply (simp add: int_prod zmod_int prod_int_eq)
nipkow@31719
   422
    done
wenzelm@60527
   423
  finally have "fact (p - 1) mod p = \<ominus> \<one>" .
wenzelm@60527
   424
  then show ?thesis
wenzelm@60528
   425
    by (metis of_nat_fact Divides.transfer_int_nat_functions(2)
wenzelm@60528
   426
      cong_int_def res_neg_eq res_one_eq)
nipkow@31719
   427
qed
nipkow@31719
   428
lp15@55352
   429
lemma wilson_theorem:
wenzelm@60527
   430
  assumes "prime p"
wenzelm@60527
   431
  shows "[fact (p - 1) = - 1] (mod p)"
lp15@55352
   432
proof (cases "p = 2")
lp15@59667
   433
  case True
lp15@55352
   434
  then show ?thesis
nipkow@64272
   435
    by (simp add: cong_int_def fact_prod)
lp15@55352
   436
next
lp15@55352
   437
  case False
lp15@55352
   438
  then show ?thesis
lp15@55352
   439
    using assms prime_ge_2_nat
lp15@55352
   440
    by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
lp15@55352
   441
qed
nipkow@31719
   442
haftmann@65416
   443
text {*
haftmann@65416
   444
  This result can be transferred to the multiplicative group of
haftmann@65416
   445
  $\mathbb{Z}/p\mathbb{Z}$ for $p$ prime. *}
haftmann@65416
   446
haftmann@65416
   447
lemma mod_nat_int_pow_eq:
haftmann@65416
   448
  fixes n :: nat and p a :: int
haftmann@65416
   449
  assumes "a \<ge> 0" "p \<ge> 0"
haftmann@65416
   450
  shows "(nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)"
haftmann@65416
   451
  using assms
haftmann@65416
   452
  by (simp add: int_one_le_iff_zero_less nat_mod_distrib order_less_imp_le nat_power_eq[symmetric])
haftmann@65416
   453
haftmann@65416
   454
theorem residue_prime_mult_group_has_gen :
haftmann@65416
   455
 fixes p :: nat
haftmann@65416
   456
 assumes prime_p : "prime p"
haftmann@65416
   457
 shows "\<exists>a \<in> {1 .. p - 1}. {1 .. p - 1} = {a^i mod p|i . i \<in> UNIV}"
haftmann@65416
   458
proof -
haftmann@65416
   459
  have "p\<ge>2" using prime_gt_1_nat[OF prime_p] by simp
haftmann@65416
   460
  interpret R:residues_prime "p" "residue_ring p" unfolding residues_prime_def
haftmann@65416
   461
    by (simp add: prime_p)
haftmann@65416
   462
  have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} =  {1 .. int p - 1}"
haftmann@65416
   463
    by (auto simp add: R.zero_cong R.res_carrier_eq)
haftmann@65416
   464
  obtain a where a:"a \<in> {1 .. int p - 1}"
haftmann@65416
   465
         and a_gen:"{1 .. int p - 1} = {a(^)\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}"
haftmann@65416
   466
    apply atomize_elim using field.finite_field_mult_group_has_gen[OF R.is_field]
haftmann@65416
   467
    by (auto simp add: car[symmetric] carrier_mult_of)
haftmann@65416
   468
  { fix x fix i :: nat assume x: "x \<in> {1 .. int p - 1}"
haftmann@65416
   469
    hence "x (^)\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)" using R.pow_cong[of x i] by auto}
haftmann@65416
   470
  note * = this
haftmann@65416
   471
  have **:"nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R")
haftmann@65416
   472
  proof
haftmann@65416
   473
    { fix n assume n: "n \<in> ?L"
haftmann@65416
   474
      then have "n \<in> ?R" using `p\<ge>2` by force
haftmann@65416
   475
    } thus "?L \<subseteq> ?R" by blast
haftmann@65416
   476
    { fix n assume n: "n \<in> ?R"
haftmann@65416
   477
      then have "n \<in> ?L" using `p\<ge>2` Set_Interval.transfer_nat_int_set_functions(2) by fastforce
haftmann@65416
   478
    } thus "?R \<subseteq> ?L" by blast
haftmann@65416
   479
  qed
haftmann@65416
   480
  have "nat ` {a^i mod (int p) | i::nat. i \<in> UNIV} = {nat a^i mod p | i . i \<in> UNIV}" (is "?L = ?R")
haftmann@65416
   481
  proof
haftmann@65416
   482
    { fix x assume x: "x \<in> ?L"
haftmann@65416
   483
      then obtain i where i:"x = nat (a^i mod (int p))" by blast
haftmann@65416
   484
      hence "x = nat a ^ i mod p" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
haftmann@65416
   485
      hence "x \<in> ?R" using i by blast
haftmann@65416
   486
    } thus "?L \<subseteq> ?R" by blast
haftmann@65416
   487
    { fix x assume x: "x \<in> ?R"
haftmann@65416
   488
      then obtain i where i:"x = nat a^i mod p" by blast
haftmann@65416
   489
      hence "x \<in> ?L" using mod_nat_int_pow_eq[of a "int p" i] a `p\<ge>2` by auto
haftmann@65416
   490
    } thus "?R \<subseteq> ?L" by blast
haftmann@65416
   491
  qed
haftmann@65416
   492
  hence "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}"
haftmann@65416
   493
    using * a a_gen ** by presburger
haftmann@65416
   494
  moreover
haftmann@65416
   495
  have "nat a \<in> {1 .. p - 1}" using a by force
haftmann@65416
   496
  ultimately show ?thesis ..
haftmann@65416
   497
qed
haftmann@65416
   498
nipkow@31719
   499
end