src/HOL/Number_Theory/Residues.thy
author haftmann
Sun, 09 Nov 2014 10:03:18 +0100
changeset 58954 18750e86d5b8
parent 58889 5b7a9633cfa8
child 59667 651ea265d568
permissions -rw-r--r--
reverted 1ebf0a1f12a4 after successful re-tuning of simp rules for divisibility
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
58889
5b7a9633cfa8 modernized header uniformly as section;
wenzelm
parents: 57514
diff changeset
     8
section {* Residue rings *}
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
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    11
imports
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    12
  UniqueFactorization
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    13
  Binomial
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    14
  MiscAlgebra
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    15
begin
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    16
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    17
(*
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    18
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    19
  A locale for residue rings
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    20
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    21
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    22
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32479
diff changeset
    23
definition residue_ring :: "int => int ring" where
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    24
  "residue_ring m == (|
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    25
    carrier =       {0..m - 1},
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    26
    mult =          (%x y. (x * y) mod m),
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    27
    one =           1,
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    28
    zero =          0,
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    29
    add =           (%x y. (x + y) mod m) |)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    30
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    31
locale residues =
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    32
  fixes m :: int and R (structure)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    33
  assumes m_gt_one: "m > 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    34
  defines "R == residue_ring m"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    35
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    36
context residues
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    37
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    38
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    39
lemma abelian_group: "abelian_group R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    40
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    41
  apply (rule abelian_groupI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    42
  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
    43
  apply (auto simp add: mod_add_right_eq [symmetric] ac_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    44
  apply (case_tac "x = 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    45
  apply force
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    46
  apply (subgoal_tac "(x + (m - x)) mod m = 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    47
  apply (erule bexI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    48
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    49
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    50
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    51
lemma comm_monoid: "comm_monoid R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    52
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    53
  apply (unfold R_def residue_ring_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    54
  apply (rule comm_monoidI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    55
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    56
  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
    57
  apply (erule ssubst)
47163
248376f8881d remove redundant lemma
huffman
parents: 44872
diff changeset
    58
  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
    59
  apply (simp_all only: ac_simps)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    60
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    61
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    62
lemma cring: "cring R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    63
  apply (rule cringI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    64
  apply (rule abelian_group)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    65
  apply (rule comm_monoid)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    66
  apply (unfold R_def residue_ring_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    67
  apply (subst mod_add_eq [symmetric])
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55352
diff changeset
    68
  apply (subst mult.commute)
47163
248376f8881d remove redundant lemma
huffman
parents: 44872
diff changeset
    69
  apply (subst mod_mult_right_eq [symmetric])
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35416
diff changeset
    70
  apply (simp add: field_simps)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    71
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    72
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    73
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    74
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    75
sublocale residues < cring
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    76
  by (rule cring)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    77
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    78
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    79
context residues
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
    80
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    81
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    82
(* These lemmas translate back and forth between internal and
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    83
   external concepts *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    84
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    85
lemma res_carrier_eq: "carrier R = {0..m - 1}"
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_add_eq: "x \<oplus> y = (x + y) mod m"
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_mult_eq: "x \<otimes> y = (x * y) mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    92
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    93
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    94
lemma res_zero_eq: "\<zero> = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    95
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    96
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    97
lemma res_one_eq: "\<one> = 1"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    98
  unfolding R_def residue_ring_def units_of_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    99
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   100
lemma res_units_eq: "Units R = { x. 0 < x & x < m & coprime x m}"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   101
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   102
  apply (unfold Units_def R_def residue_ring_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   103
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   104
  apply (subgoal_tac "x ~= 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   105
  apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   106
  apply (metis invertible_coprime_int)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   107
  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
   108
  apply (auto simp add: cong_int_def mult.commute)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   109
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   110
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   111
lemma res_neg_eq: "\<ominus> x = (- x) mod m"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   112
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   113
  apply (unfold R_def a_inv_def m_inv_def residue_ring_def)
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 (rule the_equality)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   116
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   117
  apply (subst mod_add_right_eq [symmetric])
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   118
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   119
  apply (subst mod_add_left_eq [symmetric])
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   120
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   121
  apply (subgoal_tac "y mod m = - x mod m")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   122
  apply simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 55352
diff changeset
   123
  apply (metis minus_add_cancel mod_mult_self1 mult.commute)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   124
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   125
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   126
lemma finite [iff]: "finite (carrier R)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   127
  by (subst res_carrier_eq, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   128
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   129
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
   130
  by (subst res_units_eq) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   131
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   132
(* The function a -> a mod m maps the integers to the
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   133
   residue classes. The following lemmas show that this mapping
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   134
   respects addition and multiplication on the integers. *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   135
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   136
lemma mod_in_carrier [iff]: "a mod m : carrier R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   137
  apply (unfold res_carrier_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   138
  apply (insert m_gt_one, auto)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   139
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   140
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   141
lemma add_cong: "(x mod m) \<oplus> (y mod m) = (x + y) mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   142
  unfolding R_def residue_ring_def
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   143
  apply auto
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   144
  apply presburger
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   145
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   146
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   147
lemma mult_cong: "(x mod m) \<otimes> (y mod m) = (x * y) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   148
  unfolding R_def residue_ring_def
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   149
  by auto (metis mod_mult_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   150
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   151
lemma zero_cong: "\<zero> = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   152
  unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   153
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   154
lemma one_cong: "\<one> = 1 mod m"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   155
  using m_gt_one unfolding R_def residue_ring_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   156
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   157
(* revise algebra library to use 1? *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   158
lemma pow_cong: "(x mod m) (^) n = x^n mod m"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   159
  apply (insert m_gt_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   160
  apply (induct n)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   161
  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
   162
  apply (metis mult.commute mult_cong)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   163
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   164
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   165
lemma neg_cong: "\<ominus> (x mod m) = (- x) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   166
  by (metis mod_minus_eq res_neg_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   167
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   168
lemma (in residues) prod_cong:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   169
    "finite A \<Longrightarrow> (\<Otimes> i:A. (f i) mod m) = (PROD i:A. f i) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   170
  by (induct set: finite) (auto simp: one_cong mult_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   171
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   172
lemma (in residues) sum_cong:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   173
    "finite A \<Longrightarrow> (\<Oplus> i:A. (f i) mod m) = (SUM i: A. f i) mod m"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   174
  by (induct set: finite) (auto simp: zero_cong add_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   175
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   176
lemma mod_in_res_units [simp]: "1 < m \<Longrightarrow> coprime a m \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   177
    a mod m : Units R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   178
  apply (subst res_units_eq, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   179
  apply (insert pos_mod_sign [of m a])
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   180
  apply (subgoal_tac "a mod m ~= 0")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   181
  apply arith
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   182
  apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   183
  apply (metis gcd_int.commute gcd_red_int)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   184
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   185
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   186
lemma res_eq_to_cong: "((a mod m) = (b mod m)) = [a = b] (mod (m::int))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   187
  unfolding cong_int_def by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   188
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   189
(* Simplifying with these will translate a ring equation in R to a
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   190
   congruence. *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   191
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   192
lemmas res_to_cong_simps = add_cong mult_cong pow_cong one_cong
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   193
    prod_cong sum_cong neg_cong res_eq_to_cong
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   194
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   195
(* Other useful facts about the residue ring *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   196
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   197
lemma one_eq_neg_one: "\<one> = \<ominus> \<one> \<Longrightarrow> m = 2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   198
  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
   199
  apply (metis add.commute add_diff_cancel mod_mod_trivial one_add_one uminus_add_conv_diff
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   200
            zero_neq_one zmod_zminus1_eq_if)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   201
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   202
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   203
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   204
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   205
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   206
(* prime residues *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   207
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   208
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
   209
  fixes p and R (structure)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   210
  assumes p_prime [intro]: "prime p"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   211
  defines "R == residue_ring p"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   212
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   213
sublocale residues_prime < residues p
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   214
  apply (unfold R_def residues_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   215
  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
   216
  apply (metis (full_types) int_1 of_nat_less_iff prime_gt_1_nat)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   217
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   218
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   219
context residues_prime
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   220
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   221
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   222
lemma is_field: "field R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   223
  apply (rule cring.field_intro2)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   224
  apply (rule cring)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   225
  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
   226
  apply (rule classical)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   227
  apply (erule notE)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   228
  apply (subst gcd_commute_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   229
  apply (rule prime_imp_coprime_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   230
  apply (rule p_prime)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   231
  apply (rule notI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   232
  apply (frule zdvd_imp_le)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   233
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   234
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   235
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   236
lemma res_prime_units_eq: "Units R = {1..p - 1}"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   237
  apply (subst res_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   238
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31798
diff changeset
   239
  apply (subst gcd_commute_int)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   240
  apply (auto simp add: p_prime prime_imp_coprime_int zdvd_not_zless)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   241
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   242
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   243
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   244
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   245
sublocale residues_prime < field
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   246
  by (rule is_field)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   247
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   248
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   249
(*
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   250
  Test cases: Euler's theorem and Wilson's theorem.
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   251
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   252
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   253
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   254
subsection{* Euler's theorem *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   255
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   256
(* the definition of the phi function *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   257
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   258
definition phi :: "int => nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   259
  where "phi m = card({ x. 0 < x & x < m & gcd x m = 1})"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   260
55261
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   261
lemma phi_def_nat: "phi m = card({ x. 0 < x & x < nat m & gcd x (nat m) = 1})"
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   262
  apply (simp add: phi_def)
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   263
  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
   264
  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
   265
  apply (metis dual_order.irrefl dual_order.strict_trans leI nat_1 transfer_nat_int_gcd(1))
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   266
  apply (metis One_nat_def int_0 int_1 int_less_0_conv int_nat_eq nat_int transfer_int_nat_gcd(1) zless_int)
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   267
  done
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   268
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   269
lemma prime_phi:
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   270
  assumes  "2 \<le> p" "phi p = p - 1" shows "prime p"
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   271
proof -
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   272
  have "{x. 0 < x \<and> x < p \<and> coprime x p} = {1..p - 1}"
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   273
    using assms unfolding phi_def_nat
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   274
    by (intro card_seteq) fastforce+
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   275
  then have cop: "\<And>x. x \<in> {1::nat..p - 1} \<Longrightarrow> coprime x p"
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   276
    by blast
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   277
  { fix x::nat assume *: "1 < x" "x < p" and "x dvd p"
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   278
    have "coprime x p" 
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   279
      apply (rule cop)
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   280
      using * apply auto
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   281
      done
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   282
    with `x dvd p` `1 < x` have "False" by auto }
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   283
  then show ?thesis 
55262
16724746ad89 fixed indentation
paulson <lp15@cam.ac.uk>
parents: 55261
diff changeset
   284
    using `2 \<le> p` 
16724746ad89 fixed indentation
paulson <lp15@cam.ac.uk>
parents: 55261
diff changeset
   285
    by (simp add: prime_def)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   286
       (metis One_nat_def dvd_pos_nat nat_dvd_not_less nat_neq_iff not_gr0 
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   287
              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
   288
qed
ad3604df6bc6 new lemmas involving phi from Lehmer AFP entry
paulson <lp15@cam.ac.uk>
parents: 55242
diff changeset
   289
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   290
lemma phi_zero [simp]: "phi 0 = 0"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   291
  apply (subst phi_def)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   292
(* Auto hangs here. Once again, where is the simplification rule
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   293
   1 == Suc 0 coming from? *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   294
  apply (auto simp add: card_eq_0_iff)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   295
(* Add card_eq_0_iff as a simp rule? delete card_empty_imp? *)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   296
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   297
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   298
lemma phi_one [simp]: "phi 1 = 0"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   299
  by (auto simp add: phi_def card_eq_0_iff)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   300
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   301
lemma (in residues) phi_eq: "phi m = card(Units R)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   302
  by (simp add: phi_def res_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   303
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   304
lemma (in residues) euler_theorem1:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   305
  assumes a: "gcd a m = 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   306
  shows "[a^phi m = 1] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   307
proof -
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   308
  from a m_gt_one have [simp]: "a mod m : Units R"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   309
    by (intro mod_in_res_units)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   310
  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
   311
    by simp
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   312
  also have "\<dots> = \<one>"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   313
    by (intro units_power_order_eq_one, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   314
  finally show ?thesis
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   315
    by (simp add: res_to_cong_simps)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   316
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   317
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   318
(* In fact, there is a two line proof!
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   319
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   320
lemma (in residues) euler_theorem1:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   321
  assumes a: "gcd a m = 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   322
  shows "[a^phi m = 1] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   323
proof -
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   324
  have "(a mod m) (^) (phi m) = \<one>"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   325
    by (simp add: phi_eq units_power_order_eq_one a m_gt_one)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   326
  then show ?thesis
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   327
    by (simp add: res_to_cong_simps)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   328
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   329
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   330
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   331
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   332
(* outside the locale, we can relax the restriction m > 1 *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   333
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   334
lemma euler_theorem:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   335
  assumes "m >= 0" and "gcd a m = 1"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   336
  shows "[a^phi m = 1] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   337
proof (cases)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   338
  assume "m = 0 | m = 1"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   339
  then show ?thesis by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   340
next
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   341
  assume "~(m = 0 | m = 1)"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   342
  with assms show ?thesis
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   343
    by (intro residues.euler_theorem1, unfold residues_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   344
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   345
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   346
lemma (in residues_prime) phi_prime: "phi p = (nat p - 1)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   347
  apply (subst phi_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   348
  apply (subst res_prime_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   349
  apply auto
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   350
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   351
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   352
lemma phi_prime: "prime p \<Longrightarrow> phi p = (nat p - 1)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   353
  apply (rule residues_prime.phi_prime)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   354
  apply (erule residues_prime.intro)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   355
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   356
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   357
lemma fermat_theorem:
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
   358
  fixes a::int
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   359
  assumes "prime p" and "~ (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
   360
  shows "[a^(p - 1) = 1] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   361
proof -
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   362
  from assms have "[a^phi p = 1] (mod p)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   363
    apply (intro euler_theorem)
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
   364
    apply (metis of_nat_0_le_iff)
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
    apply (metis gcd_int.commute prime_imp_coprime_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   366
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   367
  also have "phi p = nat p - 1"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   368
    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
   369
  finally show ?thesis
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
   370
    by (metis nat_int) 
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   371
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   372
55227
653de351d21c version of Fermat's Theorem for type nat
paulson <lp15@cam.ac.uk>
parents: 55172
diff changeset
   373
lemma fermat_theorem_nat:
653de351d21c version of Fermat's Theorem for type nat
paulson <lp15@cam.ac.uk>
parents: 55172
diff changeset
   374
  assumes "prime p" and "~ (p dvd a)"
653de351d21c version of Fermat's Theorem for type nat
paulson <lp15@cam.ac.uk>
parents: 55172
diff changeset
   375
  shows "[a^(p - 1) = 1] (mod p)"
653de351d21c version of Fermat's Theorem for type nat
paulson <lp15@cam.ac.uk>
parents: 55172
diff changeset
   376
using fermat_theorem [of p a] 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
   377
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
   378
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   379
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   380
subsection {* Wilson's theorem *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   381
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   382
lemma (in field) inv_pair_lemma: "x : Units R \<Longrightarrow> y : Units R \<Longrightarrow>
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   383
    {x, inv x} ~= {y, inv y} \<Longrightarrow> {x, inv x} Int {y, inv y} = {}"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   384
  apply auto
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   385
  apply (metis Units_inv_inv)+
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   386
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   387
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   388
lemma (in residues_prime) wilson_theorem1:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   389
  assumes a: "p > 2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   390
  shows "[fact (p - 1) = - 1] (mod p)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   391
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   392
  let ?InversePairs = "{ {x, inv x} | x. x : Units R - {\<one>, \<ominus> \<one>}}"
31732
052399f580cf fixed proof
nipkow
parents: 31727
diff changeset
   393
  have UR: "Units R = {\<one>, \<ominus> \<one>} Un (Union ?InversePairs)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   394
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   395
  have "(\<Otimes>i: Units R. i) =
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   396
    (\<Otimes>i: {\<one>, \<ominus> \<one>}. i) \<otimes> (\<Otimes>i: Union ?InversePairs. i)"
31732
052399f580cf fixed proof
nipkow
parents: 31727
diff changeset
   397
    apply (subst UR)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   398
    apply (subst finprod_Un_disjoint)
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   399
    apply (auto intro: funcsetI)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   400
    apply (metis Units_inv_inv inv_one inv_neg_one)+
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   401
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   402
  also have "(\<Otimes>i: {\<one>, \<ominus> \<one>}. i) = \<ominus> \<one>"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   403
    apply (subst finprod_insert)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   404
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   405
    apply (frule one_eq_neg_one)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   406
    apply (insert a, force)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   407
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   408
  also have "(\<Otimes>i:(Union ?InversePairs). i) =
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 36350
diff changeset
   409
      (\<Otimes>A: ?InversePairs. (\<Otimes>y:A. y))"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   410
    apply (subst finprod_Union_disjoint, auto)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   411
    apply (metis Units_inv_inv)+
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   412
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   413
  also have "\<dots> = \<one>"
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   414
    apply (rule finprod_one, auto)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   415
    apply (subst finprod_insert, auto)
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   416
    apply (metis inv_eq_self)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   417
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   418
  finally have "(\<Otimes>i: Units R. i) = \<ominus> \<one>"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   419
    by simp
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   420
  also have "(\<Otimes>i: Units R. i) = (\<Otimes>i: Units R. i mod p)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   421
    apply (rule finprod_cong')
31732
052399f580cf fixed proof
nipkow
parents: 31727
diff changeset
   422
    apply (auto)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   423
    apply (subst (asm) res_prime_units_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   424
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   425
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   426
  also have "\<dots> = (PROD i: Units R. i) mod p"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   427
    apply (rule prod_cong)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   428
    apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   429
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   430
  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
   431
    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
   432
    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
   433
    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
   434
    apply (simp add: int_setprod zmod_int setprod_int_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   435
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   436
  finally have "fact (p - 1) mod p = \<ominus> \<one>".
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
   437
  then show ?thesis
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
   438
    by (metis Divides.transfer_int_nat_functions(2) cong_int_def res_neg_eq res_one_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   439
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   440
55352
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   441
lemma wilson_theorem:
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   442
  assumes "prime p" shows "[fact (p - 1) = - 1] (mod p)"
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   443
proof (cases "p = 2")
paulson <lp15@cam.ac.uk>
parents: 55262
diff changeset
   444
  case True 
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