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