src/HOL/Number_Theory/Residues.thy
author paulson <lp15@cam.ac.uk>
Thu, 24 Aug 2023 21:40:24 +0100
changeset 78522 918a9ed06898
parent 75963 884dbbc8e1b3
child 80084 173548e4d5d0
permissions -rw-r--r--
some refinements in Algebra and Number_Theory
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
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 66305
diff changeset
    13
  "HOL-Algebra.Multiplicative_Group"
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
    14
  Totient
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    15
begin
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    16
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    17
definition QuadRes :: "int \<Rightarrow> int \<Rightarrow> bool"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    18
  where "QuadRes p a = (\<exists>y. ([y^2 = a] (mod p)))"
64282
261d42f0bfac Removed Old_Number_Theory; all theories ported (thanks to Jaime Mendizabal Roche)
eberlm <eberlm@in.tum.de>
parents: 64272
diff changeset
    19
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    20
definition Legendre :: "int \<Rightarrow> int \<Rightarrow> int"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    21
  where "Legendre a p =
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    22
    (if ([a = 0] (mod p)) then 0
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    23
     else if QuadRes p a then 1
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    24
     else -1)"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    25
64282
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"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    30
  where
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    31
    "residue_ring m =
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    32
      \<lparr>carrier = {0..m - 1},
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    33
       monoid.mult = \<lambda>x y. (x * y) mod m,
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    34
       one = 1,
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
    35
       zero = 0,
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
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"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
    41
  defines R_m_def: "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
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
    58
    by (fastforce simp add: R_m_def residue_ring_def mod_add_right_eq ac_simps  intro!: abelian_groupI)
65899
ab7d8c999531 removed junk;
wenzelm
parents: 65726
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"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    62
proof -
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    63
  have "\<And>x y z. \<lbrakk>x \<in> carrier R; y \<in> carrier R; z \<in> carrier R\<rbrakk> \<Longrightarrow> x \<otimes> y \<otimes> z = x \<otimes> (y \<otimes> z)"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    64
    "\<And>x y. \<lbrakk>x \<in> carrier R; y \<in> carrier R\<rbrakk> \<Longrightarrow> x \<otimes> y = y \<otimes> x"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    65
    unfolding R_m_def residue_ring_def
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    66
    by (simp_all add: algebra_simps mod_mult_right_eq)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    67
  then show ?thesis
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    68
    unfolding R_m_def residue_ring_def
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    69
    by unfold_locales (use m_gt_one in simp_all)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    70
qed
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    71
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    72
interpretation comm_monoid R
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
    73
  using comm_monoid by blast
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    74
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    75
lemma cring: "cring R"
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
    76
  apply (intro cringI abelian_group comm_monoid)
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
    77
  unfolding R_m_def residue_ring_def
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
    78
  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
    79
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    80
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    81
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    82
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    83
sublocale residues < cring
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    84
  by (rule cring)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    85
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    86
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    87
context residues
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    88
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    89
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    90
text \<open>
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    91
  These lemmas translate back and forth between internal and
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    92
  external concepts.
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    93
\<close>
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_carrier_eq: "carrier R = {0..m - 1}"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
    96
  by (auto simp: R_m_def residue_ring_def)
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_add_eq: "x \<oplus> y = (x + y) mod m"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
    99
  by (auto simp: R_m_def residue_ring_def)
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_mult_eq: "x \<otimes> y = (x * y) mod m"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   102
  by (auto simp: R_m_def residue_ring_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   103
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   104
lemma res_zero_eq: "\<zero> = 0"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   105
  by (auto simp: R_m_def residue_ring_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   106
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   107
lemma res_one_eq: "\<one> = 1"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   108
  by (auto simp: R_m_def residue_ring_def units_of_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   109
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   110
lemma res_units_eq: "Units R = {x. 0 < x \<and> x < m \<and> coprime x m}" (is "_ = ?rhs")
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   111
proof
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   112
  show "Units R \<subseteq> ?rhs"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   113
    using zero_less_mult_iff invertible_coprime 
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   114
    by (fastforce simp: Units_def R_m_def residue_ring_def)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   115
next
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   116
  show "?rhs \<subseteq> Units R"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   117
    unfolding Units_def R_m_def residue_ring_def 
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   118
    by (force simp add: cong_def coprime_iff_invertible'_int mult.commute)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   119
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   120
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   121
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   122
proof -
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   123
  have "\<ominus> x = (THE y. 0 \<le> y \<and> y < m \<and> (x + y) mod m = 0 \<and> (y + x) mod m = 0)"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   124
    by (simp add: R_m_def a_inv_def m_inv_def residue_ring_def)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   125
  also have "\<dots> = (- x) mod m"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   126
  proof -
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   127
    have "\<And>y. 0 \<le> y \<and> y < m \<and> (x + y) mod m = 0 \<and> (y + x) mod m = 0 \<Longrightarrow>
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   128
         y = - x mod m"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   129
      by (metis minus_add_cancel mod_add_eq plus_int_code(1) zmod_trivial_iff)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   130
    then show ?thesis
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   131
      by (intro the_equality) (use m_gt_one  in \<open>simp add: add.commute mod_add_right_eq\<close>)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   132
  qed
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   133
  finally show ?thesis .
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   134
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   135
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   136
lemma finite [iff]: "finite (carrier R)"
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   137
  by (simp add: res_carrier_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   138
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   139
lemma finite_Units [iff]: "finite (Units R)"
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   140
  by (simp add: finite_ring_finite_units)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   141
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   142
text \<open>
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 62348
diff changeset
   143
  The function \<open>a \<mapsto> a mod m\<close> maps the integers to the
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   144
  residue classes. The following lemmas show that this mapping
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   145
  respects addition and multiplication on the integers.
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   146
\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   147
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   148
lemma mod_in_carrier [iff]: "a mod m \<in> carrier R"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   149
  unfolding res_carrier_eq
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   150
  using insert m_gt_one by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   151
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   152
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   153
  by (auto simp: R_m_def residue_ring_def mod_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   154
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   155
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   156
  by (auto simp: R_m_def residue_ring_def mod_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   157
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   158
lemma zero_cong: "\<zero> = 0"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   159
  by (auto simp: R_m_def residue_ring_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   160
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   161
lemma one_cong: "\<one> = 1 mod m"
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   162
  using m_gt_one by (auto simp: R_m_def residue_ring_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   163
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   164
(* FIXME revise algebra library to use 1? *)
67341
df79ef3b3a41 Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents: 67091
diff changeset
   165
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
   166
  using m_gt_one
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   167
proof (induct n)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   168
  case 0
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   169
  then show ?case
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   170
    by (simp add: one_cong) 
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   171
next
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   172
  case (Suc n)
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   173
  then show ?case
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   174
    by (simp add: mult_cong power_commutes) 
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   175
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   176
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   177
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   178
  by (metis mod_minus_eq res_neg_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   179
60528
wenzelm
parents: 60527
diff changeset
   180
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
   181
  by (induct set: finite) (auto simp: one_cong mult_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   182
60528
wenzelm
parents: 60527
diff changeset
   183
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
   184
  by (induct set: finite) (auto simp: zero_cong add_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   185
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   186
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
   187
  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
   188
  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
   189
proof (cases "a mod m = 0")
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   190
  case True
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   191
  with assms show ?thesis
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   192
    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
   193
next
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   194
  case False
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   195
  from assms have "0 < m" by simp
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   196
  then have "0 \<le> a mod m" by (rule pos_mod_sign [of m a])
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   197
  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
   198
  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
   199
    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
   200
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   201
60528
wenzelm
parents: 60527
diff changeset
   202
lemma res_eq_to_cong: "(a mod m) = (b mod m) \<longleftrightarrow> [a = b] (mod m)"
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66837
diff changeset
   203
  by (auto simp: cong_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   204
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   205
60528
wenzelm
parents: 60527
diff changeset
   206
text \<open>Simplifying with these will translate a ring equation in R to a congruence.\<close>
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   207
lemmas res_to_cong_simps =
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   208
  add_cong mult_cong pow_cong one_cong
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   209
  prod_cong sum_cong neg_cong res_eq_to_cong
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   210
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   211
text \<open>Other useful facts about the residue ring.\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   212
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   213
  using one_cong res_neg_eq res_one_eq zmod_zminus1_eq_if by fastforce
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   214
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   215
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   216
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   217
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   218
subsection \<open>Prime residues\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   219
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   220
locale residues_prime =
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63417
diff changeset
   221
  fixes p :: nat and R (structure)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   222
  assumes p_prime [intro]: "prime p"
63534
523b488b15c9 Overhaul of prime/multiplicity/prime_factors
eberlm <eberlm@in.tum.de>
parents: 63417
diff changeset
   223
  defines "R \<equiv> residue_ring (int p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   224
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   225
sublocale residues_prime < residues p
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   226
  unfolding R_def residues_def
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   227
  by (auto simp: p_prime prime_gt_1_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   228
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   229
context residues_prime
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   230
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   231
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   232
lemma p_coprime_left:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   233
  "coprime p a \<longleftrightarrow> \<not> p dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   234
  using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   235
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   236
lemma p_coprime_right:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   237
  "coprime a p  \<longleftrightarrow> \<not> p dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   238
  using p_coprime_left [of a] by (simp add: ac_simps)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   239
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   240
lemma p_coprime_left_int:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   241
  "coprime (int p) a \<longleftrightarrow> \<not> int p dvd a"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   242
  using p_prime by (auto intro: prime_imp_coprime dest: coprime_common_divisor)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   243
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   244
lemma p_coprime_right_int:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   245
  "coprime a (int p) \<longleftrightarrow> \<not> int p dvd a"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   246
  using coprime_commute p_coprime_left_int by blast
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   247
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   248
lemma is_field: "field R"
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   249
proof -
66837
6ba663ff2b1c tuned proofs
haftmann
parents: 66817
diff changeset
   250
  have "0 < x \<Longrightarrow> x < int p \<Longrightarrow> coprime (int p) x" for x
6ba663ff2b1c tuned proofs
haftmann
parents: 66817
diff changeset
   251
    by (rule prime_imp_coprime) (auto simp add: zdvd_not_zless)
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   252
  then show ?thesis
66837
6ba663ff2b1c tuned proofs
haftmann
parents: 66817
diff changeset
   253
    by (intro cring.field_intro2 cring)
6ba663ff2b1c tuned proofs
haftmann
parents: 66817
diff changeset
   254
      (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq ac_simps)
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   255
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   256
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   257
lemma res_prime_units_eq: "Units R = {1..p - 1}"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   258
  by (auto simp add: res_units_eq p_coprime_right_int zdvd_not_zless)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   259
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   260
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   261
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   262
sublocale residues_prime < field
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   263
  by (rule is_field)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   264
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   265
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   266
section \<open>Test cases: Euler's theorem and Wilson's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   267
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   268
subsection \<open>Euler's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   269
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   270
lemma (in residues) totatives_eq:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   271
  "totatives (nat m) = nat ` Units R"
55261
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   272
proof -
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   273
  from m_gt_one have "\<bar>m\<bar> > 1"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   274
    by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   275
  then have "totatives (nat \<bar>m\<bar>) = nat ` abs ` Units R"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   276
    by (auto simp add: totatives_def res_units_eq image_iff le_less)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   277
      (use m_gt_one zless_nat_eq_int_zless in force)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   278
  moreover have "\<bar>m\<bar> = m" "abs ` Units R = Units R"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   279
    using m_gt_one by (auto simp add: res_units_eq image_iff)
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   280
  ultimately show ?thesis
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   281
    by simp
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   282
qed
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   283
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   284
lemma (in residues) totient_eq:
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   285
  "totient (nat m) = card (Units R)"
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   286
proof  -
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   287
  have *: "inj_on nat (Units R)"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   288
    by (rule inj_onI) (auto simp add: res_units_eq)
67051
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   289
  then show ?thesis
e7e54a0b9197 dedicated definition for coprimality
haftmann
parents: 66954
diff changeset
   290
    by (simp add: totient_def totatives_eq card_image)
55261
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   291
qed
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   292
75963
884dbbc8e1b3 avoid duplicate fact error on global_interpretation of residues
haftmann
parents: 71252
diff changeset
   293
lemma (in residues_prime) prime_totient_eq: "totient p = p - 1"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   294
  using p_prime totient_prime by blast
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   295
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   296
lemma (in residues) euler_theorem:
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   297
  assumes "coprime a m"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   298
  shows "[a ^ totient (nat m) = 1] (mod m)"
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   299
proof -
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   300
  have "a ^ totient (nat m) mod m = 1 mod m"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   301
    by (metis assms finite_Units m_gt_one mod_in_res_units one_cong totient_eq pow_cong units_power_order_eq_one)
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   302
  then show ?thesis
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   303
    using res_eq_to_cong by blast
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   304
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   305
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   306
lemma euler_theorem:
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   307
  fixes a m :: nat
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   308
  assumes "coprime a m"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   309
  shows "[a ^ totient m = 1] (mod m)"
67091
1393c2340eec more symbols;
wenzelm
parents: 67051
diff changeset
   310
proof (cases "m = 0 \<or> m = 1")
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   311
  case True
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   312
  then show ?thesis by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   313
next
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   314
  case False
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   315
  with assms show ?thesis
66954
0230af0f3c59 removed ancient nat-int transfer
haftmann
parents: 66888
diff changeset
   316
    using residues.euler_theorem [of "int m" "int a"] cong_int_iff
0230af0f3c59 removed ancient nat-int transfer
haftmann
parents: 66888
diff changeset
   317
    by (auto simp add: residues_def gcd_int_def) fastforce
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   318
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   319
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   320
lemma fermat_theorem:
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   321
  fixes p a :: nat
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   322
  assumes "prime p" and "\<not> p dvd a"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   323
  shows "[a ^ (p - 1) = 1] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   324
proof -
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   325
  from assms prime_imp_coprime [of p a] have "coprime a p"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   326
    by (auto simp add: ac_simps)
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   327
  then have "[a ^ totient p = 1] (mod p)"
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   328
     by (rule euler_theorem)
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   329
  also have "totient p = p - 1"
65726
f5d64d094efe More material on totient function
eberlm <eberlm@in.tum.de>
parents: 65465
diff changeset
   330
    by (rule totient_prime) (rule assms)
65465
067210a08a22 more fundamental euler's totient function on nat rather than int;
haftmann
parents: 65416
diff changeset
   331
  finally show ?thesis .
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   332
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   333
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   334
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   335
subsection \<open>Wilson's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   336
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   337
lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow>
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   338
    {x, inv x} \<noteq> {y, inv y} \<Longrightarrow> {x, inv x} \<inter> {y, inv y} = {}"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   339
  by auto
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   340
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   341
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   342
lemma (in residues_prime) wilson_theorem1:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   343
  assumes a: "p > 2"
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59667
diff changeset
   344
  shows "[fact (p - 1) = (-1::int)] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   345
proof -
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   346
  let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   347
  have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   348
    by auto
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   349
  have 11: "\<one> \<noteq> \<ominus> \<one>"
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   350
    using a one_eq_neg_one by force
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   351
  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
   352
    apply (subst UR)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   353
    apply (subst finprod_Un_disjoint)
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   354
    using inv_one inv_eq_neg_one_eq apply (auto intro!: funcsetI)+
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   355
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   356
  also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   357
    by (simp add: 11)
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   358
  also have "(\<Otimes>i\<in>(\<Union>?Inverse_Pairs). i) = (\<Otimes>A\<in>?Inverse_Pairs. (\<Otimes>y\<in>A. y))"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   359
    by (rule finprod_Union_disjoint) (auto simp: pairwise_def disjnt_def dest!: inv_eq_imp_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   360
  also have "\<dots> = \<one>"
68447
0beb927eed89 Adjusting Number_Theory for new Algebra
paulson <lp15@cam.ac.uk>
parents: 67341
diff changeset
   361
    apply (rule finprod_one_eqI)
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   362
    apply clarsimp
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   363
    apply (subst finprod_insert)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   364
        apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   365
    apply (metis inv_eq_self)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   366
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   367
  finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   368
    by simp
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   369
  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
   370
    by (rule finprod_cong') (auto simp: res_units_eq)
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   371
  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
   372
    by (rule prod_cong) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   373
  also have "\<dots> = fact (p - 1) mod p"
65066
c64d778a593a tidied some messy proofs
paulson <lp15@cam.ac.uk>
parents: 64593
diff changeset
   374
    using assms
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   375
    by (simp add: res_prime_units_eq int_prod zmod_int prod_int_eq fact_prod)
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   376
  finally have "fact (p - 1) mod p = \<ominus> \<one>" .
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   377
  then show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66837
diff changeset
   378
    by (simp add: cong_def res_neg_eq res_one_eq zmod_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   379
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   380
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   381
lemma wilson_theorem:
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   382
  assumes "prime p"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   383
  shows "[fact (p - 1) = - 1] (mod p)"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   384
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
   385
  case True
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   386
  then show ?thesis
66888
930abfdf8727 algebraic foundation for congruences
haftmann
parents: 66837
diff changeset
   387
    by (simp add: cong_def fact_prod)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   388
next
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   389
  case False
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   390
  then show ?thesis
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   391
    using assms prime_ge_2_nat
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   392
    by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   393
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   394
66304
cde6ceffcbc7 isabelle update_cartouches -c -t;
wenzelm
parents: 65899
diff changeset
   395
text \<open>
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   396
  This result can be transferred to the multiplicative group of
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   397
  \<open>\<int>/p\<int>\<close> for \<open>p\<close> prime.\<close>
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   398
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   399
lemma mod_nat_int_pow_eq:
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   400
  fixes n :: nat and p a :: int
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   401
  shows "a \<ge> 0 \<Longrightarrow> p \<ge> 0 \<Longrightarrow> (nat a ^ n) mod (nat p) = nat ((a ^ n) mod p)"
78522
918a9ed06898 some refinements in Algebra and Number_Theory
paulson <lp15@cam.ac.uk>
parents: 75963
diff changeset
   402
  by (simp add: nat_mod_as_int)
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   403
69785
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   404
theorem residue_prime_mult_group_has_gen:
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   405
 fixes p :: nat
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   406
 assumes prime_p : "prime p"
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   407
 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
   408
proof -
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   409
  have "p \<ge> 2"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   410
    using prime_gt_1_nat[OF prime_p] by simp
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   411
  interpret R: residues_prime p "residue_ring p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   412
    by (simp add: residues_prime_def prime_p)
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   413
  have car: "carrier (residue_ring (int p)) - {\<zero>\<^bsub>residue_ring (int p)\<^esub>} = {1 .. int p - 1}"
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   414
    by (auto simp add: R.zero_cong R.res_carrier_eq)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   415
67341
df79ef3b3a41 Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents: 67091
diff changeset
   416
  have "x [^]\<^bsub>residue_ring (int p)\<^esub> i = x ^ i mod (int p)"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   417
    if "x \<in> {1 .. int p - 1}" for x and i :: nat
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   418
    using that R.pow_cong[of x i] by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   419
  moreover
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   420
  obtain a where a: "a \<in> {1 .. int p - 1}"
67341
df79ef3b3a41 Renamed (^) to [^] in preparation of the move from "op X" to (X)
nipkow
parents: 67091
diff changeset
   421
    and a_gen: "{1 .. int p - 1} = {a[^]\<^bsub>residue_ring (int p)\<^esub>i|i::nat . i \<in> UNIV}"
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   422
    using field.finite_field_mult_group_has_gen[OF R.is_field]
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   423
    by (auto simp add: car[symmetric] carrier_mult_of)
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   424
  moreover
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   425
  have "nat ` {1 .. int p - 1} = {1 .. p - 1}" (is "?L = ?R")
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   426
  proof
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   427
    have "n \<in> ?R" if "n \<in> ?L" for n
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   428
      using that \<open>p\<ge>2\<close> by force
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   429
    then show "?L \<subseteq> ?R" by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   430
    have "n \<in> ?L" if "n \<in> ?R" for n
66837
6ba663ff2b1c tuned proofs
haftmann
parents: 66817
diff changeset
   431
      using that \<open>p\<ge>2\<close> by (auto intro: rev_image_eqI [of "int n"])
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   432
    then show "?R \<subseteq> ?L" by blast
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   433
  qed
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   434
  moreover
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   435
  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
   436
  proof
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   437
    have "x \<in> ?R" if "x \<in> ?L" for x
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   438
    proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   439
      from that obtain i where i: "x = nat (a^i mod (int p))"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   440
        by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   441
      then have "x = nat a ^ i mod p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   442
        using mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   443
      with i show ?thesis by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   444
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   445
    then show "?L \<subseteq> ?R" by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   446
    have "x \<in> ?L" if "x \<in> ?R" for x
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   447
    proof -
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   448
      from that obtain i where i: "x = nat a^i mod p"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   449
        by blast
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   450
      with mod_nat_int_pow_eq[of a "int p" i] a \<open>p\<ge>2\<close> show ?thesis
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   451
        by auto
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   452
    qed
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   453
    then show "?R \<subseteq> ?L" by blast
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   454
  qed
66305
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   455
  ultimately have "{1 .. p - 1} = {nat a^i mod p | i. i \<in> UNIV}"
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   456
    by presburger
7454317f883c misc tuning and modernization;
wenzelm
parents: 66304
diff changeset
   457
  moreover from a have "nat a \<in> {1 .. p - 1}" by force
65416
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   458
  ultimately show ?thesis ..
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   459
qed
f707dbcf11e3 more approproiate placement of theories MiscAlgebra and Multiplicate_Group
haftmann
parents: 65066
diff changeset
   460
69785
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   461
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   462
subsection \<open>Upper bound for the number of $n$-th roots\<close>
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   463
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   464
lemma roots_mod_prime_bound:
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   465
  fixes n c p :: nat
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   466
  assumes "prime p" "n > 0"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   467
  defines "A \<equiv> {x\<in>{..<p}. [x ^ n = c] (mod p)}"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   468
  shows   "card A \<le> n"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   469
proof -
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   470
  define R where "R = residue_ring (int p)"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   471
  from assms(1) interpret residues_prime p R
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   472
    by unfold_locales (simp_all add: R_def)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   473
  interpret R: UP_domain R "UP R" by (unfold_locales)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   474
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   475
  let ?f = "UnivPoly.monom (UP R) \<one>\<^bsub>R\<^esub> n \<ominus>\<^bsub>(UP R)\<^esub> UnivPoly.monom (UP R) (int (c mod p)) 0"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   476
  have in_carrier: "int (c mod p) \<in> carrier R"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   477
    using prime_gt_1_nat[OF assms(1)] by (simp add: R_def residue_ring_def)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   478
  
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   479
  have "deg R ?f = n"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   480
    using assms in_carrier by (simp add: R.deg_minus_eq)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   481
  hence f_not_zero: "?f \<noteq> \<zero>\<^bsub>UP R\<^esub>" using assms by (auto simp add : R.deg_nzero_nzero)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   482
  have roots_bound: "finite {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>} \<and>
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   483
                     card {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>} \<le> deg R ?f"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   484
                    using finite in_carrier by (intro R.roots_bound[OF _ f_not_zero]) simp
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   485
  have subs: "{x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} \<subseteq>
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   486
                {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>}"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   487
    using in_carrier by (auto simp: R.evalRR_simps)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   488
  then have "card {x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} \<le>
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   489
               card {a \<in> carrier R. UnivPoly.eval R R id a ?f = \<zero>\<^bsub>R\<^esub>}"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   490
    using finite by (intro card_mono) auto
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   491
  also have "\<dots> \<le> n"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   492
    using \<open>deg R ?f = n\<close> roots_bound by linarith
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   493
  also {
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   494
    fix x assume "x \<in> carrier R"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   495
    hence "x [^]\<^bsub>R\<^esub> n = (x ^ n) mod (int p)"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   496
      by (subst pow_cong [symmetric]) (auto simp: R_def residue_ring_def)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   497
  }
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   498
  hence "{x \<in> carrier R. x [^]\<^bsub>R\<^esub> n = int (c mod p)} = {x \<in> carrier R. [x ^ n = int c] (mod p)}"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   499
    by (fastforce simp: cong_def zmod_int)
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   500
  also have "bij_betw int A {x \<in> carrier R. [x ^ n = int c] (mod p)}"
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   501
    by (rule bij_betwI[of int _ _ nat])
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   502
       (use cong_int_iff in \<open>force simp: R_def residue_ring_def A_def\<close>)+
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   503
  from bij_betw_same_card[OF this] have "card {x \<in> carrier R. [x ^ n = int c] (mod p)} = card A" ..
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   504
  finally show ?thesis .
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   505
qed
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   506
9e326f6f8a24 More material for HOL-Number_Theory: ord, Carmichael's function, primitive roots
Manuel Eberl <eberlm@in.tum.de>
parents: 68458
diff changeset
   507
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   508
end