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