src/HOL/Number_Theory/Residues.thy
author haftmann
Wed, 08 Jul 2015 14:01:41 +0200
changeset 60688 01488b559910
parent 60528 190b4a7d8b87
child 62348 9a5f43dac883
permissions -rw-r--r--
avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
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
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
    11
imports UniqueFactorization MiscAlgebra
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    12
begin
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    13
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    14
subsection \<open>A locale for residue rings\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    15
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    16
definition residue_ring :: "int \<Rightarrow> int ring"
60528
wenzelm
parents: 60527
diff changeset
    17
where
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    18
  "residue_ring m =
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    19
    \<lparr>carrier = {0..m - 1},
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    20
     mult = \<lambda>x y. (x * y) mod m,
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    21
     one = 1,
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    22
     zero = 0,
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    23
     add = \<lambda>x y. (x + y) mod m\<rparr>"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    24
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    25
locale residues =
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    26
  fixes m :: int and R (structure)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    27
  assumes m_gt_one: "m > 1"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    28
  defines "R \<equiv> residue_ring m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    29
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    30
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    31
lemma abelian_group: "abelian_group R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    32
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    33
  apply (rule abelian_groupI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    34
  apply (unfold R_def residue_ring_def)
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
    35
  apply (auto simp add: mod_add_right_eq [symmetric] ac_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    36
  apply (case_tac "x = 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    37
  apply force
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    38
  apply (subgoal_tac "(x + (m - x)) mod m = 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    39
  apply (erule bexI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    40
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    41
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    42
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    43
lemma comm_monoid: "comm_monoid R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    44
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    45
  apply (unfold R_def residue_ring_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    46
  apply (rule comm_monoidI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    47
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    48
  apply (subgoal_tac "x * y mod m * z mod m = z * (x * y mod m) mod m")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    49
  apply (erule ssubst)
47163
248376f8881d remove redundant lemma
huffman
parents: 44872
diff changeset
    50
  apply (subst mod_mult_right_eq [symmetric])+
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
    51
  apply (simp_all only: ac_simps)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    52
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    53
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    54
lemma cring: "cring R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    55
  apply (rule cringI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    56
  apply (rule abelian_group)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    57
  apply (rule comm_monoid)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    58
  apply (unfold R_def residue_ring_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    59
  apply (subst mod_add_eq [symmetric])
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55352
diff changeset
    60
  apply (subst mult.commute)
47163
248376f8881d remove redundant lemma
huffman
parents: 44872
diff changeset
    61
  apply (subst mod_mult_right_eq [symmetric])
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35416
diff changeset
    62
  apply (simp add: field_simps)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    63
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    64
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    65
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    66
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    67
sublocale residues < cring
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    68
  by (rule cring)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    69
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    70
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    71
context residues
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    72
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    73
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    74
text \<open>
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    75
  These lemmas translate back and forth between internal and
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    76
  external concepts.
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    77
\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    78
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    79
lemma res_carrier_eq: "carrier R = {0..m - 1}"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    80
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    81
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    82
lemma res_add_eq: "x \<oplus> y = (x + y) mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    83
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    84
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    85
lemma res_mult_eq: "x \<otimes> y = (x * y) mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    86
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    87
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    88
lemma res_zero_eq: "\<zero> = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    89
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    90
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    91
lemma res_one_eq: "\<one> = 1"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    92
  unfolding R_def residue_ring_def units_of_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    93
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    94
lemma res_units_eq: "Units R = {x. 0 < x \<and> x < m \<and> coprime x m}"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    95
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    96
  apply (unfold Units_def R_def residue_ring_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    97
  apply auto
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
    98
  apply (subgoal_tac "x \<noteq> 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    99
  apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   100
  apply (metis invertible_coprime_int)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   101
  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
   102
  apply (auto simp add: cong_int_def mult.commute)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   103
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   104
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   105
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   106
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   107
  apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   108
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   109
  apply (rule the_equality)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   110
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   111
  apply (subst mod_add_right_eq [symmetric])
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   112
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   113
  apply (subst mod_add_left_eq [symmetric])
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   114
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   115
  apply (subgoal_tac "y mod m = - x mod m")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   116
  apply simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55352
diff changeset
   117
  apply (metis minus_add_cancel mod_mult_self1 mult.commute)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   118
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   119
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   120
lemma finite [iff]: "finite (carrier R)"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   121
  by (subst res_carrier_eq) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   122
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   123
lemma finite_Units [iff]: "finite (Units R)"
50027
7747a9f4c358 adjusting proofs as the set_comprehension_pointfree simproc breaks some existing proofs
bulwahn
parents: 47163
diff changeset
   124
  by (subst res_units_eq) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   125
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   126
text \<open>
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   127
  The function @{text "a \<mapsto> a mod m"} maps the integers to the
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   128
  residue classes. The following lemmas show that this mapping
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   129
  respects addition and multiplication on the integers.
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   130
\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   131
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   132
lemma mod_in_carrier [iff]: "a mod m \<in> carrier R"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   133
  unfolding res_carrier_eq
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   134
  using insert m_gt_one by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   135
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   136
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   137
  unfolding R_def residue_ring_def
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   138
  apply auto
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   139
  apply presburger
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   140
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   141
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   142
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   143
  unfolding R_def residue_ring_def
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   144
  by auto (metis mod_mult_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   145
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   146
lemma zero_cong: "\<zero> = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   147
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   148
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   149
lemma one_cong: "\<one> = 1 mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   150
  using m_gt_one unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   151
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   152
(* FIXME revise algebra library to use 1? *)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   153
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   154
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   155
  apply (induct n)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   156
  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
   157
  apply (metis mult.commute mult_cong)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   158
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   159
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   160
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   161
  by (metis mod_minus_eq res_neg_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   162
60528
wenzelm
parents: 60527
diff changeset
   163
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
   164
  by (induct set: finite) (auto simp: one_cong mult_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   165
60528
wenzelm
parents: 60527
diff changeset
   166
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
   167
  by (induct set: finite) (auto simp: zero_cong add_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   168
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   169
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
   170
  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
   171
  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
   172
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
   173
  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
   174
    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
   175
next
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   176
  case False
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   177
  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
   178
  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
   179
  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
   180
  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
   181
    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
   182
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   183
60528
wenzelm
parents: 60527
diff changeset
   184
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
   185
  unfolding cong_int_def by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   186
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   187
60528
wenzelm
parents: 60527
diff changeset
   188
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
   189
lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   190
    prod_cong sum_cong neg_cong res_eq_to_cong
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   191
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   192
text \<open>Other useful facts about the residue ring.\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   193
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   194
  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
   195
  apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
60528
wenzelm
parents: 60527
diff changeset
   196
    zero_neq_one zmod_zminus1_eq_if)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   197
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   198
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   199
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   200
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   201
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   202
subsection \<open>Prime residues\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   203
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   204
locale residues_prime =
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
   205
  fixes p and R (structure)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   206
  assumes p_prime [intro]: "prime p"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   207
  defines "R \<equiv> residue_ring p"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   208
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   209
sublocale residues_prime < residues p
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   210
  apply (unfold R_def residues_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   211
  using p_prime apply auto
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
   212
  apply (metis (full_types) int_1 of_nat_less_iff prime_gt_1_nat)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   213
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   214
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   215
context residues_prime
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   216
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   217
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   218
lemma is_field: "field R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   219
  apply (rule cring.field_intro2)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   220
  apply (rule cring)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   221
  apply (auto simp add: res_carrier_eq res_one_eq res_zero_eq res_units_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   222
  apply (rule classical)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   223
  apply (erule notE)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   224
  apply (subst gcd_commute_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   225
  apply (rule prime_imp_coprime_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   226
  apply (rule p_prime)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   227
  apply (rule notI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   228
  apply (frule zdvd_imp_le)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   229
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   230
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   231
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   232
lemma res_prime_units_eq: "Units R = {1..p - 1}"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   233
  apply (subst res_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   234
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   235
  apply (subst gcd_commute_int)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   236
  apply (auto simp add: p_prime prime_imp_coprime_int zdvd_not_zless)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   237
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   238
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   239
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   240
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   241
sublocale residues_prime < field
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   242
  by (rule is_field)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   243
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   244
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   245
section \<open>Test cases: Euler's theorem and Wilson's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   246
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   247
subsection \<open>Euler's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   248
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   249
text \<open>The definition of the phi function.\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   250
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   251
definition phi :: "int \<Rightarrow> nat"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   252
  where "phi m = card {x. 0 < x \<and> x < m \<and> gcd x m = 1}"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   253
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   254
lemma phi_def_nat: "phi m = card {x. 0 < x \<and> x < nat m \<and> gcd x (nat m) = 1}"
55261
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   255
  apply (simp add: phi_def)
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   256
  apply (rule bij_betw_same_card [of nat])
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   257
  apply (auto simp add: inj_on_def bij_betw_def image_def)
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   258
  apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   259
  apply (metis One_nat_def int_0 int_1 int_less_0_conv int_nat_eq nat_int
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   260
    transfer_int_nat_gcd(1) zless_int)
55261
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   261
  done
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   262
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>
55262
16724746ad89 fixed indentation
paulson <lp15@cam.ac.uk>
parents: 55261
diff changeset
   283
    by (simp add: prime_def)
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"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   289
  unfolding phi_def
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   290
(* Auto hangs here. Once again, where is the simplification rule
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   291
   1 \<equiv> Suc 0 coming from? *)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   292
  apply (auto simp add: card_eq_0_iff)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   293
(* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   294
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   295
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   296
lemma phi_one [simp]: "phi 1 = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   297
  by (auto simp add: phi_def card_eq_0_iff)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   298
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   299
lemma (in residues) phi_eq: "phi m = card (Units R)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   300
  by (simp add: phi_def res_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   301
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   302
lemma (in residues) euler_theorem1:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   303
  assumes a: "gcd a m = 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   304
  shows "[a^phi m = 1] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   305
proof -
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   306
  from a m_gt_one have [simp]: "a mod m \<in> Units R"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   307
    by (intro mod_in_res_units)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   308
  from phi_eq have "(a mod m) (^) (phi m) = (a mod m) (^) (card (Units R))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   309
    by simp
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   310
  also have "\<dots> = \<one>"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   311
    by (intro units_power_order_eq_one) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   312
  finally show ?thesis
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   313
    by (simp add: res_to_cong_simps)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   314
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   315
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   316
(* In fact, there is a two line proof!
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   317
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   318
lemma (in residues) euler_theorem1:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   319
  assumes a: "gcd a m = 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   320
  shows "[a^phi m = 1] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   321
proof -
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   322
  have "(a mod m) (^) (phi m) = \<one>"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   323
    by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   324
  then show ?thesis
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   325
    by (simp add: res_to_cong_simps)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   326
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   327
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   328
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   329
60528
wenzelm
parents: 60527
diff changeset
   330
text \<open>Outside the locale, we can relax the restriction @{text "m > 1"}.\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   331
lemma euler_theorem:
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   332
  assumes "m \<ge> 0"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   333
    and "gcd a m = 1"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   334
  shows "[a^phi m = 1] (mod m)"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   335
proof (cases "m = 0 | m = 1")
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   336
  case True
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   337
  then show ?thesis by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   338
next
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   339
  case False
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   340
  with assms show ?thesis
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   341
    by (intro residues.euler_theorem1, unfold residues_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   342
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   343
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   344
lemma (in residues_prime) phi_prime: "phi p = nat p - 1"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   345
  apply (subst phi_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   346
  apply (subst res_prime_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   347
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   348
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   349
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   350
lemma phi_prime: "prime p \<Longrightarrow> phi p = nat p - 1"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   351
  apply (rule residues_prime.phi_prime)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   352
  apply (erule residues_prime.intro)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   353
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   354
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   355
lemma fermat_theorem:
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   356
  fixes a :: int
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   357
  assumes "prime p"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   358
    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
   359
  shows "[a^(p - 1) = 1] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   360
proof -
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   361
  from assms have "[a ^ phi p = 1] (mod p)"
60688
01488b559910 avoid explicit definition of the relation of associated elements in a ring -- prefer explicit normalization instead
haftmann
parents: 60528
diff changeset
   362
    by (auto intro!: euler_theorem dest!: prime_imp_coprime_int simp add: ac_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   363
  also have "phi p = nat p - 1"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   364
    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
   365
  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
   366
    by (metis nat_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   367
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   368
55227
653de351d21c version of Fermat's Theorem for type nat
paulson <lp15@cam.ac.uk>
parents: 55172
diff changeset
   369
lemma fermat_theorem_nat:
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   370
  assumes "prime p" and "\<not> p dvd a"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   371
  shows "[a ^ (p - 1) = 1] (mod p)"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   372
  using fermat_theorem [of p a] assms
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   373
  by (metis int_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
   374
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   375
60526
fad653acf58f isabelle update_cartouches;
wenzelm
parents: 59730
diff changeset
   376
subsection \<open>Wilson's theorem\<close>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   377
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   378
lemma (in field) inv_pair_lemma: "x \<in> Units R \<Longrightarrow> y \<in> Units R \<Longrightarrow>
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   379
    {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
   380
  apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   381
  apply (metis Units_inv_inv)+
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   382
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   383
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   384
lemma (in residues_prime) wilson_theorem1:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   385
  assumes a: "p > 2"
59730
b7c394c7a619 The factorial function, "fact", now has type "nat => 'a"
paulson <lp15@cam.ac.uk>
parents: 59667
diff changeset
   386
  shows "[fact (p - 1) = (-1::int)] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   387
proof -
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   388
  let ?Inverse_Pairs = "{{x, inv x}| x. x \<in> Units R - {\<one>, \<ominus> \<one>}}"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   389
  have UR: "Units R = {\<one>, \<ominus> \<one>} \<union> \<Union>?Inverse_Pairs"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   390
    by auto
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   391
  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
   392
    apply (subst UR)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   393
    apply (subst finprod_Un_disjoint)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   394
    apply (auto intro: funcsetI)
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   395
    using inv_one apply auto[1]
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   396
    using inv_eq_neg_one_eq apply auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   397
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   398
  also have "(\<Otimes>i\<in>{\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   399
    apply (subst finprod_insert)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   400
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   401
    apply (frule one_eq_neg_one)
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   402
    using a apply force
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   403
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   404
  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
   405
    apply (subst finprod_Union_disjoint)
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   406
    apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   407
    apply (metis Units_inv_inv)+
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   408
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   409
  also have "\<dots> = \<one>"
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   410
    apply (rule finprod_one)
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   411
    apply auto
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   412
    apply (subst finprod_insert)
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   413
    apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   414
    apply (metis inv_eq_self)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   415
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   416
  finally have "(\<Otimes>i\<in>Units R. i) = \<ominus> \<one>"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   417
    by simp
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   418
  also have "(\<Otimes>i\<in>Units R. i) = (\<Otimes>i\<in>Units R. i mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   419
    apply (rule finprod_cong')
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   420
    apply auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   421
    apply (subst (asm) res_prime_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   422
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   423
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   424
  also have "\<dots> = (\<Prod>i\<in>Units R. i) mod p"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   425
    apply (rule prod_cong)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   426
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   427
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   428
  also have "\<dots> = fact (p - 1) mod p"
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
   429
    apply (subst fact_altdef_nat)
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
   430
    apply (insert assms)
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
   431
    apply (subst res_prime_units_eq)
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
   432
    apply (simp add: int_setprod zmod_int setprod_int_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   433
    done
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   434
  finally have "fact (p - 1) mod p = \<ominus> \<one>" .
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   435
  then show ?thesis
60528
wenzelm
parents: 60527
diff changeset
   436
    by (metis of_nat_fact Divides.transfer_int_nat_functions(2)
wenzelm
parents: 60527
diff changeset
   437
      cong_int_def res_neg_eq res_one_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   438
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   439
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   440
lemma wilson_theorem:
60527
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   441
  assumes "prime p"
eb431a5651fe tuned proofs;
wenzelm
parents: 60526
diff changeset
   442
  shows "[fact (p - 1) = - 1] (mod p)"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   443
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
   444
  case True
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   445
  then show ?thesis
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   446
    by (simp add: cong_int_def fact_altdef_nat)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   447
next
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   448
  case False
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   449
  then show ?thesis
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   450
    using assms prime_ge_2_nat
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   451
    by (metis residues_prime.wilson_theorem1 residues_prime.intro le_eq_less_or_eq)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   452
qed
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   453
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   454
end