src/HOL/Old_Number_Theory/EulerFermat.thy
author wenzelm
Sat Oct 17 14:43:18 2009 +0200 (2009-10-17)
changeset 32960 69916a850301
parent 32479 521cc9bf2958
child 35440 bdf8ad377877
permissions -rw-r--r--
eliminated hard tabulators, guessing at each author's individual tab-width;
tuned headers;
haftmann@32479
     1
(*  Author:     Thomas M. Rasmussen
wenzelm@11049
     2
    Copyright   2000  University of Cambridge
paulson@9508
     3
*)
paulson@9508
     4
wenzelm@11049
     5
header {* Fermat's Little Theorem extended to Euler's Totient function *}
wenzelm@11049
     6
haftmann@27556
     7
theory EulerFermat
haftmann@27556
     8
imports BijectionRel IntFact
haftmann@27556
     9
begin
wenzelm@11049
    10
wenzelm@11049
    11
text {*
wenzelm@11049
    12
  Fermat's Little Theorem extended to Euler's Totient function. More
wenzelm@11049
    13
  abstract approach than Boyer-Moore (which seems necessary to achieve
wenzelm@11049
    14
  the extended version).
wenzelm@11049
    15
*}
wenzelm@11049
    16
wenzelm@11049
    17
wenzelm@11049
    18
subsection {* Definitions and lemmas *}
paulson@9508
    19
berghofe@23755
    20
inductive_set
wenzelm@11049
    21
  RsetR :: "int => int set set"
berghofe@23755
    22
  for m :: int
berghofe@23755
    23
  where
wenzelm@11049
    24
    empty [simp]: "{} \<in> RsetR m"
haftmann@27556
    25
  | insert: "A \<in> RsetR m ==> zgcd a m = 1 ==>
wenzelm@11049
    26
      \<forall>a'. a' \<in> A --> \<not> zcong a a' m ==> insert a A \<in> RsetR m"
paulson@9508
    27
wenzelm@19670
    28
consts
wenzelm@19670
    29
  BnorRset :: "int * int => int set"
wenzelm@19670
    30
wenzelm@11049
    31
recdef BnorRset
wenzelm@11049
    32
  "measure ((\<lambda>(a, m). nat a) :: int * int => nat)"
wenzelm@11049
    33
  "BnorRset (a, m) =
paulson@11868
    34
   (if 0 < a then
paulson@11868
    35
    let na = BnorRset (a - 1, m)
haftmann@27556
    36
    in (if zgcd a m = 1 then insert a na else na)
wenzelm@11049
    37
    else {})"
paulson@9508
    38
wenzelm@19670
    39
definition
wenzelm@21404
    40
  norRRset :: "int => int set" where
wenzelm@19670
    41
  "norRRset m = BnorRset (m - 1, m)"
wenzelm@19670
    42
wenzelm@21404
    43
definition
wenzelm@21404
    44
  noXRRset :: "int => int => int set" where
wenzelm@19670
    45
  "noXRRset m x = (\<lambda>a. a * x) ` norRRset m"
wenzelm@19670
    46
wenzelm@21404
    47
definition
wenzelm@21404
    48
  phi :: "int => nat" where
wenzelm@19670
    49
  "phi m = card (norRRset m)"
wenzelm@19670
    50
wenzelm@21404
    51
definition
wenzelm@21404
    52
  is_RRset :: "int set => int => bool" where
wenzelm@19670
    53
  "is_RRset A m = (A \<in> RsetR m \<and> card A = phi m)"
wenzelm@19670
    54
wenzelm@21404
    55
definition
wenzelm@21404
    56
  RRset2norRR :: "int set => int => int => int" where
wenzelm@19670
    57
  "RRset2norRR A m a =
paulson@11868
    58
     (if 1 < m \<and> is_RRset A m \<and> a \<in> A then
wenzelm@11049
    59
        SOME b. zcong a b m \<and> b \<in> norRRset m
paulson@11868
    60
      else 0)"
wenzelm@11049
    61
wenzelm@21404
    62
definition
wenzelm@21404
    63
  zcongm :: "int => int => int => bool" where
wenzelm@19670
    64
  "zcongm m = (\<lambda>a b. zcong a b m)"
wenzelm@11049
    65
paulson@11868
    66
lemma abs_eq_1_iff [iff]: "(abs z = (1::int)) = (z = 1 \<or> z = -1)"
wenzelm@11049
    67
  -- {* LCP: not sure why this lemma is needed now *}
wenzelm@18369
    68
  by (auto simp add: abs_if)
wenzelm@11049
    69
wenzelm@11049
    70
wenzelm@11049
    71
text {* \medskip @{text norRRset} *}
wenzelm@11049
    72
wenzelm@11049
    73
declare BnorRset.simps [simp del]
wenzelm@11049
    74
wenzelm@11049
    75
lemma BnorRset_induct:
wenzelm@18369
    76
  assumes "!!a m. P {} a m"
wenzelm@18369
    77
    and "!!a m. 0 < (a::int) ==> P (BnorRset (a - 1, m::int)) (a - 1) m
wenzelm@18369
    78
      ==> P (BnorRset(a,m)) a m"
wenzelm@18369
    79
  shows "P (BnorRset(u,v)) u v"
wenzelm@18369
    80
  apply (rule BnorRset.induct)
wenzelm@18369
    81
  apply safe
wenzelm@18369
    82
   apply (case_tac [2] "0 < a")
wenzelm@18369
    83
    apply (rule_tac [2] prems)
wenzelm@18369
    84
     apply simp_all
wenzelm@18369
    85
   apply (simp_all add: BnorRset.simps prems)
wenzelm@11049
    86
  done
wenzelm@11049
    87
wenzelm@18369
    88
lemma Bnor_mem_zle [rule_format]: "b \<in> BnorRset (a, m) \<longrightarrow> b \<le> a"
wenzelm@11049
    89
  apply (induct a m rule: BnorRset_induct)
wenzelm@18369
    90
   apply simp
wenzelm@18369
    91
  apply (subst BnorRset.simps)
paulson@13833
    92
   apply (unfold Let_def, auto)
wenzelm@11049
    93
  done
wenzelm@11049
    94
wenzelm@11049
    95
lemma Bnor_mem_zle_swap: "a < b ==> b \<notin> BnorRset (a, m)"
wenzelm@18369
    96
  by (auto dest: Bnor_mem_zle)
wenzelm@11049
    97
paulson@11868
    98
lemma Bnor_mem_zg [rule_format]: "b \<in> BnorRset (a, m) --> 0 < b"
wenzelm@11049
    99
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   100
   prefer 2
wenzelm@11049
   101
   apply (subst BnorRset.simps)
paulson@13833
   102
   apply (unfold Let_def, auto)
wenzelm@11049
   103
  done
wenzelm@11049
   104
wenzelm@11049
   105
lemma Bnor_mem_if [rule_format]:
haftmann@27556
   106
    "zgcd b m = 1 --> 0 < b --> b \<le> a --> b \<in> BnorRset (a, m)"
paulson@13833
   107
  apply (induct a m rule: BnorRset.induct, auto)
wenzelm@11049
   108
   apply (subst BnorRset.simps)
wenzelm@11049
   109
   defer
wenzelm@11049
   110
   apply (subst BnorRset.simps)
paulson@13833
   111
   apply (unfold Let_def, auto)
wenzelm@11049
   112
  done
paulson@9508
   113
wenzelm@11049
   114
lemma Bnor_in_RsetR [rule_format]: "a < m --> BnorRset (a, m) \<in> RsetR m"
paulson@13833
   115
  apply (induct a m rule: BnorRset_induct, simp)
wenzelm@11049
   116
  apply (subst BnorRset.simps)
paulson@13833
   117
  apply (unfold Let_def, auto)
wenzelm@11049
   118
  apply (rule RsetR.insert)
wenzelm@11049
   119
    apply (rule_tac [3] allI)
wenzelm@11049
   120
    apply (rule_tac [3] impI)
wenzelm@11049
   121
    apply (rule_tac [3] zcong_not)
paulson@11868
   122
       apply (subgoal_tac [6] "a' \<le> a - 1")
wenzelm@11049
   123
        apply (rule_tac [7] Bnor_mem_zle)
paulson@13833
   124
        apply (rule_tac [5] Bnor_mem_zg, auto)
wenzelm@11049
   125
  done
wenzelm@11049
   126
wenzelm@11049
   127
lemma Bnor_fin: "finite (BnorRset (a, m))"
wenzelm@11049
   128
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   129
   prefer 2
wenzelm@11049
   130
   apply (subst BnorRset.simps)
paulson@13833
   131
   apply (unfold Let_def, auto)
wenzelm@11049
   132
  done
wenzelm@11049
   133
wenzelm@13524
   134
lemma norR_mem_unique_aux: "a \<le> b - 1 ==> a < (b::int)"
wenzelm@11049
   135
  apply auto
wenzelm@11049
   136
  done
paulson@9508
   137
wenzelm@11049
   138
lemma norR_mem_unique:
paulson@11868
   139
  "1 < m ==>
haftmann@27556
   140
    zgcd a m = 1 ==> \<exists>!b. [a = b] (mod m) \<and> b \<in> norRRset m"
wenzelm@11049
   141
  apply (unfold norRRset_def)
paulson@13833
   142
  apply (cut_tac a = a and m = m in zcong_zless_unique, auto)
wenzelm@11049
   143
   apply (rule_tac [2] m = m in zcong_zless_imp_eq)
wenzelm@11049
   144
       apply (auto intro: Bnor_mem_zle Bnor_mem_zg zcong_trans
wenzelm@32960
   145
         order_less_imp_le norR_mem_unique_aux simp add: zcong_sym)
ballarin@14174
   146
  apply (rule_tac x = b in exI, safe)
wenzelm@11049
   147
  apply (rule Bnor_mem_if)
paulson@11868
   148
    apply (case_tac [2] "b = 0")
wenzelm@11049
   149
     apply (auto intro: order_less_le [THEN iffD2])
wenzelm@11049
   150
   prefer 2
wenzelm@11049
   151
   apply (simp only: zcong_def)
haftmann@27556
   152
   apply (subgoal_tac "zgcd a m = m")
wenzelm@11049
   153
    prefer 2
wenzelm@11049
   154
    apply (subst zdvd_iff_zgcd [symmetric])
wenzelm@11049
   155
     apply (rule_tac [4] zgcd_zcong_zgcd)
nipkow@30042
   156
       apply (simp_all add: zcong_sym)
wenzelm@11049
   157
  done
wenzelm@11049
   158
wenzelm@11049
   159
wenzelm@11049
   160
text {* \medskip @{term noXRRset} *}
wenzelm@11049
   161
wenzelm@11049
   162
lemma RRset_gcd [rule_format]:
haftmann@27556
   163
    "is_RRset A m ==> a \<in> A --> zgcd a m = 1"
wenzelm@11049
   164
  apply (unfold is_RRset_def)
haftmann@27556
   165
  apply (rule RsetR.induct [where P="%A. a \<in> A --> zgcd a m = 1"], auto)
wenzelm@11049
   166
  done
wenzelm@11049
   167
wenzelm@11049
   168
lemma RsetR_zmult_mono:
wenzelm@11049
   169
  "A \<in> RsetR m ==>
haftmann@27556
   170
    0 < m ==> zgcd x m = 1 ==> (\<lambda>a. a * x) ` A \<in> RsetR m"
paulson@13833
   171
  apply (erule RsetR.induct, simp_all)
paulson@13833
   172
  apply (rule RsetR.insert, auto)
wenzelm@11049
   173
   apply (blast intro: zgcd_zgcd_zmult)
wenzelm@11049
   174
  apply (simp add: zcong_cancel)
wenzelm@11049
   175
  done
wenzelm@11049
   176
wenzelm@11049
   177
lemma card_nor_eq_noX:
paulson@11868
   178
  "0 < m ==>
haftmann@27556
   179
    zgcd x m = 1 ==> card (noXRRset m x) = card (norRRset m)"
wenzelm@11049
   180
  apply (unfold norRRset_def noXRRset_def)
wenzelm@11049
   181
  apply (rule card_image)
wenzelm@11049
   182
   apply (auto simp add: inj_on_def Bnor_fin)
wenzelm@11049
   183
  apply (simp add: BnorRset.simps)
wenzelm@11049
   184
  done
wenzelm@11049
   185
wenzelm@11049
   186
lemma noX_is_RRset:
haftmann@27556
   187
    "0 < m ==> zgcd x m = 1 ==> is_RRset (noXRRset m x) m"
wenzelm@11049
   188
  apply (unfold is_RRset_def phi_def)
wenzelm@11049
   189
  apply (auto simp add: card_nor_eq_noX)
wenzelm@11049
   190
  apply (unfold noXRRset_def norRRset_def)
wenzelm@11049
   191
  apply (rule RsetR_zmult_mono)
paulson@13833
   192
    apply (rule Bnor_in_RsetR, simp_all)
wenzelm@11049
   193
  done
paulson@9508
   194
wenzelm@11049
   195
lemma aux_some:
paulson@11868
   196
  "1 < m ==> is_RRset A m ==> a \<in> A
wenzelm@11049
   197
    ==> zcong a (SOME b. [a = b] (mod m) \<and> b \<in> norRRset m) m \<and>
wenzelm@11049
   198
      (SOME b. [a = b] (mod m) \<and> b \<in> norRRset m) \<in> norRRset m"
wenzelm@11049
   199
  apply (rule norR_mem_unique [THEN ex1_implies_ex, THEN someI_ex])
paulson@13833
   200
   apply (rule_tac [2] RRset_gcd, simp_all)
wenzelm@11049
   201
  done
wenzelm@11049
   202
wenzelm@11049
   203
lemma RRset2norRR_correct:
paulson@11868
   204
  "1 < m ==> is_RRset A m ==> a \<in> A ==>
wenzelm@11049
   205
    [a = RRset2norRR A m a] (mod m) \<and> RRset2norRR A m a \<in> norRRset m"
paulson@13833
   206
  apply (unfold RRset2norRR_def, simp)
paulson@13833
   207
  apply (rule aux_some, simp_all)
wenzelm@11049
   208
  done
wenzelm@11049
   209
wenzelm@11049
   210
lemmas RRset2norRR_correct1 =
wenzelm@11049
   211
  RRset2norRR_correct [THEN conjunct1, standard]
wenzelm@11049
   212
lemmas RRset2norRR_correct2 =
wenzelm@11049
   213
  RRset2norRR_correct [THEN conjunct2, standard]
wenzelm@11049
   214
wenzelm@11049
   215
lemma RsetR_fin: "A \<in> RsetR m ==> finite A"
wenzelm@18369
   216
  by (induct set: RsetR) auto
wenzelm@11049
   217
wenzelm@11049
   218
lemma RRset_zcong_eq [rule_format]:
paulson@11868
   219
  "1 < m ==>
wenzelm@11049
   220
    is_RRset A m ==> [a = b] (mod m) ==> a \<in> A --> b \<in> A --> a = b"
wenzelm@11049
   221
  apply (unfold is_RRset_def)
berghofe@26793
   222
  apply (rule RsetR.induct [where P="%A. a \<in> A --> b \<in> A --> a = b"])
wenzelm@11049
   223
    apply (auto simp add: zcong_sym)
wenzelm@11049
   224
  done
wenzelm@11049
   225
wenzelm@11049
   226
lemma aux:
wenzelm@11049
   227
  "P (SOME a. P a) ==> Q (SOME a. Q a) ==>
wenzelm@11049
   228
    (SOME a. P a) = (SOME a. Q a) ==> \<exists>a. P a \<and> Q a"
wenzelm@11049
   229
  apply auto
wenzelm@11049
   230
  done
wenzelm@11049
   231
wenzelm@11049
   232
lemma RRset2norRR_inj:
paulson@11868
   233
    "1 < m ==> is_RRset A m ==> inj_on (RRset2norRR A m) A"
paulson@13833
   234
  apply (unfold RRset2norRR_def inj_on_def, auto)
wenzelm@11049
   235
  apply (subgoal_tac "\<exists>b. ([x = b] (mod m) \<and> b \<in> norRRset m) \<and>
wenzelm@11049
   236
      ([y = b] (mod m) \<and> b \<in> norRRset m)")
wenzelm@11049
   237
   apply (rule_tac [2] aux)
wenzelm@11049
   238
     apply (rule_tac [3] aux_some)
wenzelm@11049
   239
       apply (rule_tac [2] aux_some)
paulson@13833
   240
         apply (rule RRset_zcong_eq, auto)
wenzelm@11049
   241
  apply (rule_tac b = b in zcong_trans)
wenzelm@11049
   242
   apply (simp_all add: zcong_sym)
wenzelm@11049
   243
  done
wenzelm@11049
   244
wenzelm@11049
   245
lemma RRset2norRR_eq_norR:
paulson@11868
   246
    "1 < m ==> is_RRset A m ==> RRset2norRR A m ` A = norRRset m"
wenzelm@11049
   247
  apply (rule card_seteq)
wenzelm@11049
   248
    prefer 3
wenzelm@11049
   249
    apply (subst card_image)
nipkow@15402
   250
      apply (rule_tac RRset2norRR_inj, auto)
nipkow@15402
   251
     apply (rule_tac [3] RRset2norRR_correct2, auto)
wenzelm@11049
   252
    apply (unfold is_RRset_def phi_def norRRset_def)
nipkow@15402
   253
    apply (auto simp add: Bnor_fin)
wenzelm@11049
   254
  done
wenzelm@11049
   255
wenzelm@11049
   256
wenzelm@13524
   257
lemma Bnor_prod_power_aux: "a \<notin> A ==> inj f ==> f a \<notin> f ` A"
paulson@13833
   258
by (unfold inj_on_def, auto)
paulson@9508
   259
wenzelm@11049
   260
lemma Bnor_prod_power [rule_format]:
nipkow@15392
   261
  "x \<noteq> 0 ==> a < m --> \<Prod>((\<lambda>a. a * x) ` BnorRset (a, m)) =
nipkow@15392
   262
      \<Prod>(BnorRset(a, m)) * x^card (BnorRset (a, m))"
wenzelm@11049
   263
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   264
   prefer 2
paulson@15481
   265
   apply (simplesubst BnorRset.simps)  --{*multiple redexes*}
paulson@13833
   266
   apply (unfold Let_def, auto)
wenzelm@11049
   267
  apply (simp add: Bnor_fin Bnor_mem_zle_swap)
wenzelm@11049
   268
  apply (subst setprod_insert)
wenzelm@13524
   269
    apply (rule_tac [2] Bnor_prod_power_aux)
wenzelm@11049
   270
     apply (unfold inj_on_def)
wenzelm@11049
   271
     apply (simp_all add: zmult_ac Bnor_fin finite_imageI
wenzelm@11049
   272
       Bnor_mem_zle_swap)
wenzelm@11049
   273
  done
wenzelm@11049
   274
wenzelm@11049
   275
wenzelm@11049
   276
subsection {* Fermat *}
wenzelm@11049
   277
wenzelm@11049
   278
lemma bijzcong_zcong_prod:
nipkow@15392
   279
    "(A, B) \<in> bijR (zcongm m) ==> [\<Prod>A = \<Prod>B] (mod m)"
wenzelm@11049
   280
  apply (unfold zcongm_def)
wenzelm@11049
   281
  apply (erule bijR.induct)
wenzelm@11049
   282
   apply (subgoal_tac [2] "a \<notin> A \<and> b \<notin> B \<and> finite A \<and> finite B")
wenzelm@11049
   283
    apply (auto intro: fin_bijRl fin_bijRr zcong_zmult)
wenzelm@11049
   284
  done
wenzelm@11049
   285
wenzelm@11049
   286
lemma Bnor_prod_zgcd [rule_format]:
haftmann@27556
   287
    "a < m --> zgcd (\<Prod>(BnorRset(a, m))) m = 1"
wenzelm@11049
   288
  apply (induct a m rule: BnorRset_induct)
wenzelm@11049
   289
   prefer 2
wenzelm@11049
   290
   apply (subst BnorRset.simps)
paulson@13833
   291
   apply (unfold Let_def, auto)
wenzelm@11049
   292
  apply (simp add: Bnor_fin Bnor_mem_zle_swap)
wenzelm@11049
   293
  apply (blast intro: zgcd_zgcd_zmult)
wenzelm@11049
   294
  done
paulson@9508
   295
wenzelm@11049
   296
theorem Euler_Fermat:
haftmann@27556
   297
    "0 < m ==> zgcd x m = 1 ==> [x^(phi m) = 1] (mod m)"
wenzelm@11049
   298
  apply (unfold norRRset_def phi_def)
paulson@11868
   299
  apply (case_tac "x = 0")
paulson@11868
   300
   apply (case_tac [2] "m = 1")
wenzelm@11049
   301
    apply (rule_tac [3] iffD1)
nipkow@15392
   302
     apply (rule_tac [3] k = "\<Prod>(BnorRset(m - 1, m))"
wenzelm@11049
   303
       in zcong_cancel2)
wenzelm@11049
   304
      prefer 5
wenzelm@11049
   305
      apply (subst Bnor_prod_power [symmetric])
paulson@13833
   306
        apply (rule_tac [7] Bnor_prod_zgcd, simp_all)
wenzelm@11049
   307
  apply (rule bijzcong_zcong_prod)
wenzelm@11049
   308
  apply (fold norRRset_def noXRRset_def)
wenzelm@11049
   309
  apply (subst RRset2norRR_eq_norR [symmetric])
paulson@13833
   310
    apply (rule_tac [3] inj_func_bijR, auto)
nipkow@13187
   311
     apply (unfold zcongm_def)
nipkow@13187
   312
     apply (rule_tac [2] RRset2norRR_correct1)
nipkow@13187
   313
       apply (rule_tac [5] RRset2norRR_inj)
nipkow@13187
   314
        apply (auto intro: order_less_le [THEN iffD2]
wenzelm@32960
   315
           simp add: noX_is_RRset)
wenzelm@11049
   316
  apply (unfold noXRRset_def norRRset_def)
wenzelm@11049
   317
  apply (rule finite_imageI)
wenzelm@11049
   318
  apply (rule Bnor_fin)
wenzelm@11049
   319
  done
wenzelm@11049
   320
nipkow@16733
   321
lemma Bnor_prime:
nipkow@16733
   322
  "\<lbrakk> zprime p; a < p \<rbrakk> \<Longrightarrow> card (BnorRset (a, p)) = nat a"
wenzelm@11049
   323
  apply (induct a p rule: BnorRset.induct)
wenzelm@11049
   324
  apply (subst BnorRset.simps)
nipkow@16733
   325
  apply (unfold Let_def, auto simp add:zless_zprime_imp_zrelprime)
paulson@13833
   326
  apply (subgoal_tac "finite (BnorRset (a - 1,m))")
paulson@13833
   327
   apply (subgoal_tac "a ~: BnorRset (a - 1,m)")
paulson@13833
   328
    apply (auto simp add: card_insert_disjoint Suc_nat_eq_nat_zadd1)
paulson@13833
   329
   apply (frule Bnor_mem_zle, arith)
paulson@13833
   330
  apply (frule Bnor_fin)
wenzelm@11049
   331
  done
wenzelm@11049
   332
nipkow@16663
   333
lemma phi_prime: "zprime p ==> phi p = nat (p - 1)"
wenzelm@11049
   334
  apply (unfold phi_def norRRset_def)
paulson@13833
   335
  apply (rule Bnor_prime, auto)
wenzelm@11049
   336
  done
wenzelm@11049
   337
wenzelm@11049
   338
theorem Little_Fermat:
nipkow@16663
   339
    "zprime p ==> \<not> p dvd x ==> [x^(nat (p - 1)) = 1] (mod p)"
wenzelm@11049
   340
  apply (subst phi_prime [symmetric])
wenzelm@11049
   341
   apply (rule_tac [2] Euler_Fermat)
wenzelm@11049
   342
    apply (erule_tac [3] zprime_imp_zrelprime)
paulson@13833
   343
    apply (unfold zprime_def, auto)
wenzelm@11049
   344
  done
paulson@9508
   345
paulson@9508
   346
end