src/HOL/Old_Number_Theory/Int2.thy
author wenzelm
Fri Aug 06 12:37:00 2010 +0200 (2010-08-06)
changeset 38159 e9b4835a54ee
parent 32479 521cc9bf2958
child 41541 1fa4725c4656
permissions -rw-r--r--
modernized specifications;
tuned headers;
wenzelm@38159
     1
(*  Title:      HOL/Old_Number_Theory/Int2.thy
paulson@13871
     2
    Authors:    Jeremy Avigad, David Gray, and Adam Kramer
paulson@13871
     3
*)
paulson@13871
     4
paulson@13871
     5
header {*Integers: Divisibility and Congruences*}
paulson@13871
     6
haftmann@27556
     7
theory Int2
haftmann@27556
     8
imports Finite2 WilsonRuss
haftmann@27556
     9
begin
paulson@13871
    10
wenzelm@38159
    11
definition MultInv :: "int => int => int"
wenzelm@38159
    12
  where "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)"
nipkow@30042
    19
  by (induct n) (auto simp add: dvd_mult2 [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
nipkow@30042
    43
  apply (force simp del:dvd_mult)
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)"
nipkow@30042
    87
  by (auto simp add: zcong_def)
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@25675
   127
  by (metis zcong_not zcong_sym zless_linear)
paulson@13871
   128
wenzelm@19670
   129
lemma zcong_neg_1_impl_ne_1: "[| 2 < p; [x = -1] (mod p) |] ==>
wenzelm@18369
   130
    ~([x = 1] (mod p))"
wenzelm@18369
   131
proof
paulson@13871
   132
  assume "2 < p" and "[x = 1] (mod p)" and "[x = -1] (mod p)"
wenzelm@18369
   133
  then have "[1 = -1] (mod p)"
paulson@13871
   134
    apply (auto simp add: zcong_sym)
paulson@13871
   135
    apply (drule zcong_trans, auto)
wenzelm@18369
   136
    done
wenzelm@18369
   137
  then have "[1 + 1 = -1 + 1] (mod p)"
paulson@13871
   138
    by (simp only: zcong_shift)
wenzelm@18369
   139
  then have "[2 = 0] (mod p)"
paulson@13871
   140
    by auto
wenzelm@18369
   141
  then have "p dvd 2"
paulson@13871
   142
    by (auto simp add: dvd_def zcong_def)
wenzelm@18369
   143
  with prems show False
paulson@13871
   144
    by (auto simp add: zdvd_not_zless)
wenzelm@18369
   145
qed
paulson@13871
   146
wenzelm@18369
   147
lemma zcong_zero_equiv_div: "[a = 0] (mod m) = (m dvd a)"
paulson@13871
   148
  by (auto simp add: zcong_def)
paulson@13871
   149
wenzelm@19670
   150
lemma zcong_zprime_prod_zero: "[| zprime p; 0 < a |] ==>
wenzelm@19670
   151
    [a * b = 0] (mod p) ==> [a = 0] (mod p) | [b = 0] (mod p)"
paulson@13871
   152
  by (auto simp add: zcong_zero_equiv_div zprime_zdvd_zmult)
paulson@13871
   153
nipkow@16663
   154
lemma zcong_zprime_prod_zero_contra: "[| zprime p; 0 < a |] ==>
wenzelm@18369
   155
  ~[a = 0](mod p) & ~[b = 0](mod p) ==> ~[a * b = 0] (mod p)"
wenzelm@19670
   156
  apply auto
paulson@13871
   157
  apply (frule_tac a = a and b = b and p = p in zcong_zprime_prod_zero)
wenzelm@18369
   158
  apply auto
wenzelm@18369
   159
  done
paulson@13871
   160
wenzelm@19670
   161
lemma zcong_not_zero: "[| 0 < x; x < m |] ==> ~[x = 0] (mod m)"
paulson@13871
   162
  by (auto simp add: zcong_zero_equiv_div zdvd_not_zless)
paulson@13871
   163
wenzelm@18369
   164
lemma zcong_zero: "[| 0 \<le> x; x < m; [x = 0](mod m) |] ==> x = 0"
paulson@13871
   165
  apply (drule order_le_imp_less_or_eq, auto)
wenzelm@18369
   166
  apply (frule_tac m = m in zcong_not_zero)
wenzelm@18369
   167
  apply auto
wenzelm@18369
   168
  done
paulson@13871
   169
haftmann@27556
   170
lemma all_relprime_prod_relprime: "[| finite A; \<forall>x \<in> A. zgcd x y = 1 |]
haftmann@27556
   171
    ==> zgcd (setprod id A) y = 1"
berghofe@22274
   172
  by (induct set: finite) (auto simp add: zgcd_zgcd_zmult)
paulson@13871
   173
paulson@13871
   174
wenzelm@19670
   175
subsection {* Some properties of MultInv *}
wenzelm@19670
   176
wenzelm@19670
   177
lemma MultInv_prop1: "[| 2 < p; [x = y] (mod p) |] ==>
wenzelm@18369
   178
    [(MultInv p x) = (MultInv p y)] (mod p)"
paulson@13871
   179
  by (auto simp add: MultInv_def zcong_zpower)
paulson@13871
   180
wenzelm@19670
   181
lemma MultInv_prop2: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   182
  [(x * (MultInv p x)) = 1] (mod p)"
wenzelm@18369
   183
proof (simp add: MultInv_def zcong_eq_zdvd_prop)
wenzelm@18369
   184
  assume "2 < p" and "zprime p" and "~ p dvd x"
wenzelm@18369
   185
  have "x * x ^ nat (p - 2) = x ^ (nat (p - 2) + 1)"
paulson@13871
   186
    by auto
wenzelm@18369
   187
  also from prems have "nat (p - 2) + 1 = nat (p - 2 + 1)"
webertj@20217
   188
    by (simp only: nat_add_distrib)
paulson@13871
   189
  also have "p - 2 + 1 = p - 1" by arith
wenzelm@18369
   190
  finally have "[x * x ^ nat (p - 2) = x ^ nat (p - 1)] (mod p)"
paulson@13871
   191
    by (rule ssubst, auto)
wenzelm@18369
   192
  also from prems have "[x ^ nat (p - 1) = 1] (mod p)"
wenzelm@19670
   193
    by (auto simp add: Little_Fermat)
wenzelm@18369
   194
  finally (zcong_trans) show "[x * x ^ nat (p - 2) = 1] (mod p)" .
wenzelm@18369
   195
qed
paulson@13871
   196
wenzelm@19670
   197
lemma MultInv_prop2a: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   198
    [(MultInv p x) * x = 1] (mod p)"
paulson@13871
   199
  by (auto simp add: MultInv_prop2 zmult_ac)
paulson@13871
   200
wenzelm@18369
   201
lemma aux_1: "2 < p ==> ((nat p) - 2) = (nat (p - 2))"
paulson@13871
   202
  by (simp add: nat_diff_distrib)
paulson@13871
   203
wenzelm@18369
   204
lemma aux_2: "2 < p ==> 0 < nat (p - 2)"
paulson@13871
   205
  by auto
paulson@13871
   206
wenzelm@19670
   207
lemma MultInv_prop3: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   208
    ~([MultInv p x = 0](mod p))"
paulson@13871
   209
  apply (auto simp add: MultInv_def zcong_eq_zdvd_prop aux_1)
paulson@13871
   210
  apply (drule aux_2)
paulson@13871
   211
  apply (drule zpower_zdvd_prop2, auto)
wenzelm@18369
   212
  done
paulson@13871
   213
wenzelm@19670
   214
lemma aux__1: "[| 2 < p; zprime p; ~([x = 0](mod p))|] ==>
wenzelm@19670
   215
    [(MultInv p (MultInv p x)) = (x * (MultInv p x) *
wenzelm@18369
   216
      (MultInv p (MultInv p x)))] (mod p)"
paulson@13871
   217
  apply (drule MultInv_prop2, auto)
wenzelm@18369
   218
  apply (drule_tac k = "MultInv p (MultInv p x)" in zcong_scalar, auto)
paulson@13871
   219
  apply (auto simp add: zcong_sym)
wenzelm@18369
   220
  done
paulson@13871
   221
nipkow@16663
   222
lemma aux__2: "[| 2 < p; zprime p; ~([x = 0](mod p))|] ==>
wenzelm@18369
   223
    [(x * (MultInv p x) * (MultInv p (MultInv p x))) = x] (mod p)"
paulson@13871
   224
  apply (frule MultInv_prop3, auto)
paulson@13871
   225
  apply (insert MultInv_prop2 [of p "MultInv p x"], auto)
paulson@13871
   226
  apply (drule MultInv_prop2, auto)
paulson@13871
   227
  apply (drule_tac k = x in zcong_scalar2, auto)
paulson@13871
   228
  apply (auto simp add: zmult_ac)
wenzelm@18369
   229
  done
paulson@13871
   230
wenzelm@19670
   231
lemma MultInv_prop4: "[| 2 < p; zprime p; ~([x = 0](mod p)) |] ==>
wenzelm@18369
   232
    [(MultInv p (MultInv p x)) = x] (mod p)"
paulson@13871
   233
  apply (frule aux__1, auto)
paulson@13871
   234
  apply (drule aux__2, auto)
paulson@13871
   235
  apply (drule zcong_trans, auto)
wenzelm@18369
   236
  done
paulson@13871
   237
wenzelm@19670
   238
lemma MultInv_prop5: "[| 2 < p; zprime p; ~([x = 0](mod p));
wenzelm@19670
   239
    ~([y = 0](mod p)); [(MultInv p x) = (MultInv p y)] (mod p) |] ==>
wenzelm@18369
   240
    [x = y] (mod p)"
wenzelm@19670
   241
  apply (drule_tac a = "MultInv p x" and b = "MultInv p y" and
paulson@13871
   242
    m = p and k = x in zcong_scalar)
paulson@13871
   243
  apply (insert MultInv_prop2 [of p x], simp)
paulson@13871
   244
  apply (auto simp only: zcong_sym [of "MultInv p x * x"])
paulson@13871
   245
  apply (auto simp add:  zmult_ac)
paulson@13871
   246
  apply (drule zcong_trans, auto)
paulson@13871
   247
  apply (drule_tac a = "x * MultInv p y" and k = y in zcong_scalar, auto)
paulson@13871
   248
  apply (insert MultInv_prop2a [of p y], auto simp add: zmult_ac)
paulson@13871
   249
  apply (insert zcong_zmult_prop2 [of "y * MultInv p y" 1 p y x])
paulson@13871
   250
  apply (auto simp add: zcong_sym)
wenzelm@18369
   251
  done
paulson@13871
   252
wenzelm@19670
   253
lemma MultInv_zcong_prop1: "[| 2 < p; [j = k] (mod p) |] ==>
wenzelm@18369
   254
    [a * MultInv p j = a * MultInv p k] (mod p)"
paulson@13871
   255
  by (drule MultInv_prop1, auto simp add: zcong_scalar2)
paulson@13871
   256
wenzelm@19670
   257
lemma aux___1: "[j = a * MultInv p k] (mod p) ==>
wenzelm@18369
   258
    [j * k = a * MultInv p k * k] (mod p)"
paulson@13871
   259
  by (auto simp add: zcong_scalar)
paulson@13871
   260
wenzelm@19670
   261
lemma aux___2: "[|2 < p; zprime p; ~([k = 0](mod p));
wenzelm@18369
   262
    [j * k = a * MultInv p k * k] (mod p) |] ==> [j * k = a] (mod p)"
wenzelm@19670
   263
  apply (insert MultInv_prop2a [of p k] zcong_zmult_prop2
paulson@13871
   264
    [of "MultInv p k * k" 1 p "j * k" a])
paulson@13871
   265
  apply (auto simp add: zmult_ac)
wenzelm@18369
   266
  done
paulson@13871
   267
wenzelm@19670
   268
lemma aux___3: "[j * k = a] (mod p) ==> [(MultInv p j) * j * k =
wenzelm@18369
   269
     (MultInv p j) * a] (mod p)"
paulson@13871
   270
  by (auto simp add: zmult_assoc zcong_scalar2)
paulson@13871
   271
wenzelm@19670
   272
lemma aux___4: "[|2 < p; zprime p; ~([j = 0](mod p));
paulson@13871
   273
    [(MultInv p j) * j * k = (MultInv p j) * a] (mod p) |]
wenzelm@18369
   274
       ==> [k = a * (MultInv p j)] (mod p)"
wenzelm@19670
   275
  apply (insert MultInv_prop2a [of p j] zcong_zmult_prop1
paulson@13871
   276
    [of "MultInv p j * j" 1 p "MultInv p j * a" k])
paulson@13871
   277
  apply (auto simp add: zmult_ac zcong_sym)
wenzelm@18369
   278
  done
paulson@13871
   279
wenzelm@19670
   280
lemma MultInv_zcong_prop2: "[| 2 < p; zprime p; ~([k = 0](mod p));
wenzelm@19670
   281
    ~([j = 0](mod p)); [j = a * MultInv p k] (mod p) |] ==>
wenzelm@18369
   282
    [k = a * MultInv p j] (mod p)"
paulson@13871
   283
  apply (drule aux___1)
paulson@13871
   284
  apply (frule aux___2, auto)
paulson@13871
   285
  by (drule aux___3, drule aux___4, auto)
paulson@13871
   286
wenzelm@19670
   287
lemma MultInv_zcong_prop3: "[| 2 < p; zprime p; ~([a = 0](mod p));
paulson@13871
   288
    ~([k = 0](mod p)); ~([j = 0](mod p));
wenzelm@19670
   289
    [a * MultInv p j = a * MultInv p k] (mod p) |] ==>
wenzelm@18369
   290
      [j = k] (mod p)"
paulson@13871
   291
  apply (auto simp add: zcong_eq_zdvd_prop [of a p])
paulson@13871
   292
  apply (frule zprime_imp_zrelprime, auto)
paulson@13871
   293
  apply (insert zcong_cancel2 [of p a "MultInv p j" "MultInv p k"], auto)
paulson@13871
   294
  apply (drule MultInv_prop5, auto)
wenzelm@18369
   295
  done
paulson@13871
   296
paulson@13871
   297
end