src/HOL/NumberTheory/Int2.thy
author haftmann
Wed Sep 26 20:27:55 2007 +0200 (2007-09-26)
changeset 24728 e2b3a1065676
parent 23315 df3a7e9ebadb
child 25675 2488fc510178
permissions -rw-r--r--
moved Finite_Set before Datatype
paulson@13871
     1
(*  Title:      HOL/Quadratic_Reciprocity/Gauss.thy
kleing@14981
     2
    ID:         $Id$
paulson@13871
     3
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871
     4
*)
paulson@13871
     5
paulson@13871
     6
header {*Integers: Divisibility and Congruences*}
paulson@13871
     7
wenzelm@18369
     8
theory Int2 imports Finite2 WilsonRuss begin
paulson@13871
     9
wenzelm@19670
    10
definition
wenzelm@21404
    11
  MultInv :: "int => int => int" where
wenzelm@19670
    12
  "MultInv p x = x ^ nat (p - 2)"
paulson@13871
    13
paulson@13871
    14
wenzelm@19670
    15
subsection {* Useful lemmas about dvd and powers *}
paulson@13871
    16
wenzelm@18369
    17
lemma zpower_zdvd_prop1:
wenzelm@18369
    18
  "0 < n \<Longrightarrow> p dvd y \<Longrightarrow> p dvd ((y::int) ^ n)"
wenzelm@18369
    19
  by (induct n) (auto simp add: zdvd_zmult zdvd_zmult2 [of p y])
paulson@13871
    20
wenzelm@18369
    21
lemma zdvd_bounds: "n dvd m ==> m \<le> (0::int) | n \<le> m"
wenzelm@18369
    22
proof -
wenzelm@18369
    23
  assume "n dvd m"
wenzelm@18369
    24
  then have "~(0 < m & m < n)"
wenzelm@18369
    25
    using zdvd_not_zless [of m n] by auto
paulson@13871
    26
  then show ?thesis by auto
wenzelm@18369
    27
qed
paulson@13871
    28
wenzelm@19670
    29
lemma zprime_zdvd_zmult_better: "[| zprime p;  p dvd (m * n) |] ==>
wenzelm@18369
    30
    (p dvd m) | (p dvd n)"
wenzelm@18369
    31
  apply (cases "0 \<le> m")
paulson@13871
    32
  apply (simp add: zprime_zdvd_zmult)
wenzelm@18369
    33
  apply (insert zprime_zdvd_zmult [of "-m" p n])
wenzelm@18369
    34
  apply auto
wenzelm@18369
    35
  done
paulson@13871
    36
wenzelm@18369
    37
lemma zpower_zdvd_prop2:
wenzelm@18369
    38
    "zprime p \<Longrightarrow> p dvd ((y::int) ^ n) \<Longrightarrow> 0 < n \<Longrightarrow> p dvd y"
wenzelm@18369
    39
  apply (induct n)
wenzelm@18369
    40
   apply simp
wenzelm@18369
    41
  apply (frule zprime_zdvd_zmult_better)
wenzelm@18369
    42
   apply simp
wenzelm@18369
    43
  apply force
wenzelm@18369
    44
  done
paulson@13871
    45
wenzelm@18369
    46
lemma div_prop1: "[| 0 < z; (x::int) < y * z |] ==> x div z < y"
wenzelm@18369
    47
proof -
chaieb@23315
    48
  assume "0 < z" then have modth: "x mod z \<ge> 0" by simp
chaieb@23315
    49
  have "(x div z) * z \<le> (x div z) * z" by simp
chaieb@23315
    50
  then have "(x div z) * z \<le> (x div z) * z + x mod z" using modth by arith 
chaieb@23315
    51
  also have "\<dots> = x"
wenzelm@18369
    52
    by (auto simp add: zmod_zdiv_equality [symmetric] zmult_ac)
wenzelm@18369
    53
  also assume  "x < y * z"
wenzelm@18369
    54
  finally show ?thesis
paulson@14387
    55
    by (auto simp add: prems mult_less_cancel_right, insert prems, arith)
wenzelm@18369
    56
qed
paulson@13871
    57
wenzelm@18369
    58
lemma div_prop2: "[| 0 < z; (x::int) < (y * z) + z |] ==> x div z \<le> y"
wenzelm@18369
    59
proof -
wenzelm@18369
    60
  assume "0 < z" and "x < (y * z) + z"
paulson@13871
    61
  then have "x < (y + 1) * z" by (auto simp add: int_distrib)
wenzelm@18369
    62
  then have "x div z < y + 1"
wenzelm@18369
    63
    apply -
wenzelm@18369
    64
    apply (rule_tac y = "y + 1" in div_prop1)
wenzelm@18369
    65
    apply (auto simp add: prems)
wenzelm@18369
    66
    done
paulson@13871
    67
  then show ?thesis by auto
wenzelm@18369
    68
qed
paulson@13871
    69
wenzelm@18369
    70
lemma zdiv_leq_prop: "[| 0 < y |] ==> y * (x div y) \<le> (x::int)"
wenzelm@18369
    71
proof-
wenzelm@18369
    72
  assume "0 < y"
paulson@13871
    73
  from zmod_zdiv_equality have "x = y * (x div y) + x mod y" by auto
wenzelm@18369
    74
  moreover have "0 \<le> x mod y"
paulson@13871
    75
    by (auto simp add: prems pos_mod_sign)
wenzelm@18369
    76
  ultimately show ?thesis
paulson@13871
    77
    by arith
wenzelm@18369
    78
qed
paulson@13871
    79
wenzelm@19670
    80
wenzelm@19670
    81
subsection {* Useful properties of congruences *}
paulson@13871
    82
wenzelm@18369
    83
lemma zcong_eq_zdvd_prop: "[x = 0](mod p) = (p dvd x)"
paulson@13871
    84
  by (auto simp add: zcong_def)
paulson@13871
    85
wenzelm@18369
    86
lemma zcong_id: "[m = 0] (mod m)"
paulson@13871
    87
  by (auto simp add: zcong_def zdvd_0_right)
paulson@13871
    88
wenzelm@18369
    89
lemma zcong_shift: "[a = b] (mod m) ==> [a + c = b + c] (mod m)"
paulson@13871
    90
  by (auto simp add: zcong_refl zcong_zadd)
paulson@13871
    91
wenzelm@18369
    92
lemma zcong_zpower: "[x = y](mod m) ==> [x^z = y^z](mod m)"
wenzelm@18369
    93
  by (induct z) (auto simp add: zcong_zmult)
paulson@13871
    94
wenzelm@19670
    95
lemma zcong_eq_trans: "[| [a = b](mod m); b = c; [c = d](mod m) |] ==>
wenzelm@18369
    96
    [a = d](mod m)"
wenzelm@18369
    97
  apply (erule zcong_trans)
wenzelm@18369
    98
  apply simp
wenzelm@18369
    99
  done
paulson@13871
   100
wenzelm@18369
   101
lemma aux1: "a - b = (c::int) ==> a = c + b"
paulson@13871
   102
  by auto
paulson@13871
   103
wenzelm@19670
   104
lemma zcong_zmult_prop1: "[a = b](mod m) ==> ([c = a * d](mod m) =
wenzelm@18369
   105
    [c = b * d] (mod m))"
paulson@13871
   106
  apply (auto simp add: zcong_def dvd_def)
paulson@13871
   107
  apply (rule_tac x = "ka + k * d" in exI)
wenzelm@18369
   108
  apply (drule aux1)+
paulson@13871
   109
  apply (auto simp add: int_distrib)
paulson@13871
   110
  apply (rule_tac x = "ka - k * d" in exI)
wenzelm@18369
   111
  apply (drule aux1)+
paulson@13871
   112
  apply (auto simp add: int_distrib)
wenzelm@18369
   113
  done
paulson@13871
   114
wenzelm@19670
   115
lemma zcong_zmult_prop2: "[a = b](mod m) ==>
wenzelm@18369
   116
    ([c = d * a](mod m) = [c = d * b] (mod m))"
paulson@13871
   117
  by (auto simp add: zmult_ac zcong_zmult_prop1)
paulson@13871
   118
wenzelm@19670
   119
lemma zcong_zmult_prop3: "[| zprime p; ~[x = 0] (mod p);
wenzelm@18369
   120
    ~[y = 0] (mod p) |] ==> ~[x * y = 0] (mod p)"
paulson@13871
   121
  apply (auto simp add: zcong_def)
paulson@13871
   122
  apply (drule zprime_zdvd_zmult_better, auto)
wenzelm@18369
   123
  done
paulson@13871
   124
wenzelm@19670
   125
lemma zcong_less_eq: "[| 0 < x; 0 < y; 0 < m; [x = y] (mod m);
wenzelm@18369
   126
    x < m; y < m |] ==> x = y"
paulson@13871
   127
  apply (simp add: zcong_zmod_eq)
wenzelm@18369
   128
  apply (subgoal_tac "(x mod m) = x")
wenzelm@18369
   129
  apply (subgoal_tac "(y mod m) = y")
paulson@13871
   130
  apply simp
paulson@13871
   131
  apply (rule_tac [1-2] mod_pos_pos_trivial)
wenzelm@18369
   132
  apply auto
wenzelm@18369
   133
  done
paulson@13871
   134
wenzelm@19670
   135
lemma zcong_neg_1_impl_ne_1: "[| 2 < p; [x = -1] (mod p) |] ==>
wenzelm@18369
   136
    ~([x = 1] (mod p))"
wenzelm@18369
   137
proof
paulson@13871
   138
  assume "2 < p" and "[x = 1] (mod p)" and "[x = -1] (mod p)"
wenzelm@18369
   139
  then have "[1 = -1] (mod p)"
paulson@13871
   140
    apply (auto simp add: zcong_sym)
paulson@13871
   141
    apply (drule zcong_trans, auto)
wenzelm@18369
   142
    done
wenzelm@18369
   143
  then have "[1 + 1 = -1 + 1] (mod p)"
paulson@13871
   144
    by (simp only: zcong_shift)
wenzelm@18369
   145
  then have "[2 = 0] (mod p)"
paulson@13871
   146
    by auto
wenzelm@18369
   147
  then have "p dvd 2"
paulson@13871
   148
    by (auto simp add: dvd_def zcong_def)
wenzelm@18369
   149
  with prems show False
paulson@13871
   150
    by (auto simp add: zdvd_not_zless)
wenzelm@18369
   151
qed
paulson@13871
   152
wenzelm@18369
   153
lemma zcong_zero_equiv_div: "[a = 0] (mod m) = (m dvd a)"
paulson@13871
   154
  by (auto simp add: zcong_def)
paulson@13871
   155
wenzelm@19670
   156
lemma zcong_zprime_prod_zero: "[| zprime p; 0 < a |] ==>
wenzelm@19670
   157
    [a * b = 0] (mod p) ==> [a = 0] (mod p) | [b = 0] (mod p)"
paulson@13871
   158
  by (auto simp add: zcong_zero_equiv_div zprime_zdvd_zmult)
paulson@13871
   159
nipkow@16663
   160
lemma zcong_zprime_prod_zero_contra: "[| zprime p; 0 < a |] ==>
wenzelm@18369
   161
  ~[a = 0](mod p) & ~[b = 0](mod p) ==> ~[a * b = 0] (mod p)"
wenzelm@19670
   162
  apply auto
paulson@13871
   163
  apply (frule_tac a = a and b = b and p = p in zcong_zprime_prod_zero)
wenzelm@18369
   164
  apply auto
wenzelm@18369
   165
  done
paulson@13871
   166
wenzelm@19670
   167
lemma zcong_not_zero: "[| 0 < x; x < m |] ==> ~[x = 0] (mod m)"
paulson@13871
   168
  by (auto simp add: zcong_zero_equiv_div zdvd_not_zless)
paulson@13871
   169
wenzelm@18369
   170
lemma zcong_zero: "[| 0 \<le> x; x < m; [x = 0](mod m) |] ==> x = 0"
paulson@13871
   171
  apply (drule order_le_imp_less_or_eq, auto)
wenzelm@18369
   172
  apply (frule_tac m = m in zcong_not_zero)
wenzelm@18369
   173
  apply auto
wenzelm@18369
   174
  done
paulson@13871
   175
paulson@13871
   176
lemma all_relprime_prod_relprime: "[| finite A; \<forall>x \<in> A. (zgcd(x,y) = 1) |]
wenzelm@18369
   177
    ==> zgcd (setprod id A,y) = 1"
berghofe@22274
   178
  by (induct set: finite) (auto simp add: zgcd_zgcd_zmult)
paulson@13871
   179
paulson@13871
   180
wenzelm@19670
   181
subsection {* Some properties of MultInv *}
wenzelm@19670
   182
wenzelm@19670
   183
lemma MultInv_prop1: "[| 2 < p; [x = y] (mod p) |] ==>
wenzelm@18369
   184
    [(MultInv p x) = (MultInv p y)] (mod p)"
paulson@13871
   185
  by (auto simp add: MultInv_def zcong_zpower)
paulson@13871
   186
wenzelm@19670
   187
lemma MultInv_prop2: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   188
  [(x * (MultInv p x)) = 1] (mod p)"
wenzelm@18369
   189
proof (simp add: MultInv_def zcong_eq_zdvd_prop)
wenzelm@18369
   190
  assume "2 < p" and "zprime p" and "~ p dvd x"
wenzelm@18369
   191
  have "x * x ^ nat (p - 2) = x ^ (nat (p - 2) + 1)"
paulson@13871
   192
    by auto
wenzelm@18369
   193
  also from prems have "nat (p - 2) + 1 = nat (p - 2 + 1)"
webertj@20217
   194
    by (simp only: nat_add_distrib)
paulson@13871
   195
  also have "p - 2 + 1 = p - 1" by arith
wenzelm@18369
   196
  finally have "[x * x ^ nat (p - 2) = x ^ nat (p - 1)] (mod p)"
paulson@13871
   197
    by (rule ssubst, auto)
wenzelm@18369
   198
  also from prems have "[x ^ nat (p - 1) = 1] (mod p)"
wenzelm@19670
   199
    by (auto simp add: Little_Fermat)
wenzelm@18369
   200
  finally (zcong_trans) show "[x * x ^ nat (p - 2) = 1] (mod p)" .
wenzelm@18369
   201
qed
paulson@13871
   202
wenzelm@19670
   203
lemma MultInv_prop2a: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   204
    [(MultInv p x) * x = 1] (mod p)"
paulson@13871
   205
  by (auto simp add: MultInv_prop2 zmult_ac)
paulson@13871
   206
wenzelm@18369
   207
lemma aux_1: "2 < p ==> ((nat p) - 2) = (nat (p - 2))"
paulson@13871
   208
  by (simp add: nat_diff_distrib)
paulson@13871
   209
wenzelm@18369
   210
lemma aux_2: "2 < p ==> 0 < nat (p - 2)"
paulson@13871
   211
  by auto
paulson@13871
   212
wenzelm@19670
   213
lemma MultInv_prop3: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   214
    ~([MultInv p x = 0](mod p))"
paulson@13871
   215
  apply (auto simp add: MultInv_def zcong_eq_zdvd_prop aux_1)
paulson@13871
   216
  apply (drule aux_2)
paulson@13871
   217
  apply (drule zpower_zdvd_prop2, auto)
wenzelm@18369
   218
  done
paulson@13871
   219
wenzelm@19670
   220
lemma aux__1: "[| 2 < p; zprime p; ~([x = 0](mod p))|] ==>
wenzelm@19670
   221
    [(MultInv p (MultInv p x)) = (x * (MultInv p x) *
wenzelm@18369
   222
      (MultInv p (MultInv p x)))] (mod p)"
paulson@13871
   223
  apply (drule MultInv_prop2, auto)
wenzelm@18369
   224
  apply (drule_tac k = "MultInv p (MultInv p x)" in zcong_scalar, auto)
paulson@13871
   225
  apply (auto simp add: zcong_sym)
wenzelm@18369
   226
  done
paulson@13871
   227
nipkow@16663
   228
lemma aux__2: "[| 2 < p; zprime p; ~([x = 0](mod p))|] ==>
wenzelm@18369
   229
    [(x * (MultInv p x) * (MultInv p (MultInv p x))) = x] (mod p)"
paulson@13871
   230
  apply (frule MultInv_prop3, auto)
paulson@13871
   231
  apply (insert MultInv_prop2 [of p "MultInv p x"], auto)
paulson@13871
   232
  apply (drule MultInv_prop2, auto)
paulson@13871
   233
  apply (drule_tac k = x in zcong_scalar2, auto)
paulson@13871
   234
  apply (auto simp add: zmult_ac)
wenzelm@18369
   235
  done
paulson@13871
   236
wenzelm@19670
   237
lemma MultInv_prop4: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   238
    [(MultInv p (MultInv p x)) = x] (mod p)"
paulson@13871
   239
  apply (frule aux__1, auto)
paulson@13871
   240
  apply (drule aux__2, auto)
paulson@13871
   241
  apply (drule zcong_trans, auto)
wenzelm@18369
   242
  done
paulson@13871
   243
wenzelm@19670
   244
lemma MultInv_prop5: "[| 2 < p; zprime p; ~([x = 0](mod p));
wenzelm@19670
   245
    ~([y = 0](mod p)); [(MultInv p x) = (MultInv p y)] (mod p) |] ==>
wenzelm@18369
   246
    [x = y] (mod p)"
wenzelm@19670
   247
  apply (drule_tac a = "MultInv p x" and b = "MultInv p y" and
paulson@13871
   248
    m = p and k = x in zcong_scalar)
paulson@13871
   249
  apply (insert MultInv_prop2 [of p x], simp)
paulson@13871
   250
  apply (auto simp only: zcong_sym [of "MultInv p x * x"])
paulson@13871
   251
  apply (auto simp add:  zmult_ac)
paulson@13871
   252
  apply (drule zcong_trans, auto)
paulson@13871
   253
  apply (drule_tac a = "x * MultInv p y" and k = y in zcong_scalar, auto)
paulson@13871
   254
  apply (insert MultInv_prop2a [of p y], auto simp add: zmult_ac)
paulson@13871
   255
  apply (insert zcong_zmult_prop2 [of "y * MultInv p y" 1 p y x])
paulson@13871
   256
  apply (auto simp add: zcong_sym)
wenzelm@18369
   257
  done
paulson@13871
   258
wenzelm@19670
   259
lemma MultInv_zcong_prop1: "[| 2 < p; [j = k] (mod p) |] ==>
wenzelm@18369
   260
    [a * MultInv p j = a * MultInv p k] (mod p)"
paulson@13871
   261
  by (drule MultInv_prop1, auto simp add: zcong_scalar2)
paulson@13871
   262
wenzelm@19670
   263
lemma aux___1: "[j = a * MultInv p k] (mod p) ==>
wenzelm@18369
   264
    [j * k = a * MultInv p k * k] (mod p)"
paulson@13871
   265
  by (auto simp add: zcong_scalar)
paulson@13871
   266
wenzelm@19670
   267
lemma aux___2: "[|2 < p; zprime p; ~([k = 0](mod p));
wenzelm@18369
   268
    [j * k = a * MultInv p k * k] (mod p) |] ==> [j * k = a] (mod p)"
wenzelm@19670
   269
  apply (insert MultInv_prop2a [of p k] zcong_zmult_prop2
paulson@13871
   270
    [of "MultInv p k * k" 1 p "j * k" a])
paulson@13871
   271
  apply (auto simp add: zmult_ac)
wenzelm@18369
   272
  done
paulson@13871
   273
wenzelm@19670
   274
lemma aux___3: "[j * k = a] (mod p) ==> [(MultInv p j) * j * k =
wenzelm@18369
   275
     (MultInv p j) * a] (mod p)"
paulson@13871
   276
  by (auto simp add: zmult_assoc zcong_scalar2)
paulson@13871
   277
wenzelm@19670
   278
lemma aux___4: "[|2 < p; zprime p; ~([j = 0](mod p));
paulson@13871
   279
    [(MultInv p j) * j * k = (MultInv p j) * a] (mod p) |]
wenzelm@18369
   280
       ==> [k = a * (MultInv p j)] (mod p)"
wenzelm@19670
   281
  apply (insert MultInv_prop2a [of p j] zcong_zmult_prop1
paulson@13871
   282
    [of "MultInv p j * j" 1 p "MultInv p j * a" k])
paulson@13871
   283
  apply (auto simp add: zmult_ac zcong_sym)
wenzelm@18369
   284
  done
paulson@13871
   285
wenzelm@19670
   286
lemma MultInv_zcong_prop2: "[| 2 < p; zprime p; ~([k = 0](mod p));
wenzelm@19670
   287
    ~([j = 0](mod p)); [j = a * MultInv p k] (mod p) |] ==>
wenzelm@18369
   288
    [k = a * MultInv p j] (mod p)"
paulson@13871
   289
  apply (drule aux___1)
paulson@13871
   290
  apply (frule aux___2, auto)
paulson@13871
   291
  by (drule aux___3, drule aux___4, auto)
paulson@13871
   292
wenzelm@19670
   293
lemma MultInv_zcong_prop3: "[| 2 < p; zprime p; ~([a = 0](mod p));
paulson@13871
   294
    ~([k = 0](mod p)); ~([j = 0](mod p));
wenzelm@19670
   295
    [a * MultInv p j = a * MultInv p k] (mod p) |] ==>
wenzelm@18369
   296
      [j = k] (mod p)"
paulson@13871
   297
  apply (auto simp add: zcong_eq_zdvd_prop [of a p])
paulson@13871
   298
  apply (frule zprime_imp_zrelprime, auto)
paulson@13871
   299
  apply (insert zcong_cancel2 [of p a "MultInv p j" "MultInv p k"], auto)
paulson@13871
   300
  apply (drule MultInv_prop5, auto)
wenzelm@18369
   301
  done
paulson@13871
   302
paulson@13871
   303
end