src/HOL/NumberTheory/IntPrimes.thy
author obua
Mon Apr 10 16:00:34 2006 +0200 (2006-04-10)
changeset 19404 9bf2cdc9e8e8
parent 18369 694ea14ab4f2
child 19670 2e4a143c73c5
permissions -rw-r--r--
Moved stuff from Ring_and_Field to Matrix
     1 (*  Title:      HOL/NumberTheory/IntPrimes.thy
     2     ID:         $Id$
     3     Author:     Thomas M. Rasmussen
     4     Copyright   2000  University of Cambridge
     5 *)
     6 
     7 header {* Divisibility and prime numbers (on integers) *}
     8 
     9 theory IntPrimes imports Primes begin
    10 
    11 text {*
    12   The @{text dvd} relation, GCD, Euclid's extended algorithm, primes,
    13   congruences (all on the Integers).  Comparable to theory @{text
    14   Primes}, but @{text dvd} is included here as it is not present in
    15   main HOL.  Also includes extended GCD and congruences not present in
    16   @{text Primes}.
    17 *}
    18 
    19 
    20 subsection {* Definitions *}
    21 
    22 consts
    23   xzgcda :: "int * int * int * int * int * int * int * int => int * int * int"
    24 
    25 recdef xzgcda
    26   "measure ((\<lambda>(m, n, r', r, s', s, t', t). nat r)
    27     :: int * int * int * int *int * int * int * int => nat)"
    28   "xzgcda (m, n, r', r, s', s, t', t) =
    29 	(if r \<le> 0 then (r', s', t')
    30 	 else xzgcda (m, n, r, r' mod r, 
    31 		      s, s' - (r' div r) * s, 
    32 		      t, t' - (r' div r) * t))"
    33 
    34 constdefs
    35   zgcd :: "int * int => int"
    36   "zgcd == \<lambda>(x,y). int (gcd (nat (abs x), nat (abs y)))"
    37 
    38   zprime :: "int \<Rightarrow> bool"
    39   "zprime p == 1 < p \<and> (\<forall>m. 0 <= m & m dvd p --> m = 1 \<or> m = p)"
    40 
    41   xzgcd :: "int => int => int * int * int"
    42   "xzgcd m n == xzgcda (m, n, m, n, 1, 0, 0, 1)"
    43 
    44   zcong :: "int => int => int => bool"    ("(1[_ = _] '(mod _'))")
    45   "[a = b] (mod m) == m dvd (a - b)"
    46 
    47 
    48 
    49 text {* \medskip @{term gcd} lemmas *}
    50 
    51 lemma gcd_add1_eq: "gcd (m + k, k) = gcd (m + k, m)"
    52   by (simp add: gcd_commute)
    53 
    54 lemma gcd_diff2: "m \<le> n ==> gcd (n, n - m) = gcd (n, m)"
    55   apply (subgoal_tac "n = m + (n - m)")
    56    apply (erule ssubst, rule gcd_add1_eq, simp)
    57   done
    58 
    59 
    60 subsection {* Euclid's Algorithm and GCD *}
    61 
    62 lemma zgcd_0 [simp]: "zgcd (m, 0) = abs m"
    63   by (simp add: zgcd_def abs_if)
    64 
    65 lemma zgcd_0_left [simp]: "zgcd (0, m) = abs m"
    66   by (simp add: zgcd_def abs_if)
    67 
    68 lemma zgcd_zminus [simp]: "zgcd (-m, n) = zgcd (m, n)"
    69   by (simp add: zgcd_def)
    70 
    71 lemma zgcd_zminus2 [simp]: "zgcd (m, -n) = zgcd (m, n)"
    72   by (simp add: zgcd_def)
    73 
    74 lemma zgcd_non_0: "0 < n ==> zgcd (m, n) = zgcd (n, m mod n)"
    75   apply (frule_tac b = n and a = m in pos_mod_sign)
    76   apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
    77   apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
    78   apply (frule_tac a = m in pos_mod_bound)
    79   apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
    80   done
    81 
    82 lemma zgcd_eq: "zgcd (m, n) = zgcd (n, m mod n)"
    83   apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
    84   apply (auto simp add: linorder_neq_iff zgcd_non_0)
    85   apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
    86   done
    87 
    88 lemma zgcd_1 [simp]: "zgcd (m, 1) = 1"
    89   by (simp add: zgcd_def abs_if)
    90 
    91 lemma zgcd_0_1_iff [simp]: "(zgcd (0, m) = 1) = (abs m = 1)"
    92   by (simp add: zgcd_def abs_if)
    93 
    94 lemma zgcd_zdvd1 [iff]: "zgcd (m, n) dvd m"
    95   by (simp add: zgcd_def abs_if int_dvd_iff)
    96 
    97 lemma zgcd_zdvd2 [iff]: "zgcd (m, n) dvd n"
    98   by (simp add: zgcd_def abs_if int_dvd_iff)
    99 
   100 lemma zgcd_greatest_iff: "k dvd zgcd (m, n) = (k dvd m \<and> k dvd n)"
   101   by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
   102 
   103 lemma zgcd_commute: "zgcd (m, n) = zgcd (n, m)"
   104   by (simp add: zgcd_def gcd_commute)
   105 
   106 lemma zgcd_1_left [simp]: "zgcd (1, m) = 1"
   107   by (simp add: zgcd_def gcd_1_left)
   108 
   109 lemma zgcd_assoc: "zgcd (zgcd (k, m), n) = zgcd (k, zgcd (m, n))"
   110   by (simp add: zgcd_def gcd_assoc)
   111 
   112 lemma zgcd_left_commute: "zgcd (k, zgcd (m, n)) = zgcd (m, zgcd (k, n))"
   113   apply (rule zgcd_commute [THEN trans])
   114   apply (rule zgcd_assoc [THEN trans])
   115   apply (rule zgcd_commute [THEN arg_cong])
   116   done
   117 
   118 lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
   119   -- {* addition is an AC-operator *}
   120 
   121 lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd (m, n) = zgcd (k * m, k * n)"
   122   by (simp del: minus_mult_right [symmetric]
   123       add: minus_mult_right nat_mult_distrib zgcd_def abs_if
   124           mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
   125 
   126 lemma zgcd_zmult_distrib2_abs: "zgcd (k * m, k * n) = abs k * zgcd (m, n)"
   127   by (simp add: abs_if zgcd_zmult_distrib2)
   128 
   129 lemma zgcd_self [simp]: "0 \<le> m ==> zgcd (m, m) = m"
   130   by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
   131 
   132 lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd (k, k * n) = k"
   133   by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
   134 
   135 lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n, k) = k"
   136   by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
   137 
   138 lemma zrelprime_zdvd_zmult_aux:
   139      "zgcd (n, k) = 1 ==> k dvd m * n ==> 0 \<le> m ==> k dvd m"
   140   apply (subgoal_tac "m = zgcd (m * n, m * k)")
   141    apply (erule ssubst, rule zgcd_greatest_iff [THEN iffD2])
   142    apply (simp_all add: zgcd_zmult_distrib2 [symmetric] zero_le_mult_iff)
   143   done
   144 
   145 lemma zrelprime_zdvd_zmult: "zgcd (n, k) = 1 ==> k dvd m * n ==> k dvd m"
   146   apply (case_tac "0 \<le> m")
   147    apply (blast intro: zrelprime_zdvd_zmult_aux)
   148   apply (subgoal_tac "k dvd -m")
   149    apply (rule_tac [2] zrelprime_zdvd_zmult_aux, auto)
   150   done
   151 
   152 lemma zgcd_geq_zero: "0 <= zgcd(x,y)"
   153   by (auto simp add: zgcd_def)
   154 
   155 text{*This is merely a sanity check on zprime, since the previous version
   156       denoted the empty set.*}
   157 lemma "zprime 2"
   158   apply (auto simp add: zprime_def) 
   159   apply (frule zdvd_imp_le, simp) 
   160   apply (auto simp add: order_le_less dvd_def) 
   161   done
   162 
   163 lemma zprime_imp_zrelprime:
   164     "zprime p ==> \<not> p dvd n ==> zgcd (n, p) = 1"
   165   apply (auto simp add: zprime_def)
   166   apply (drule_tac x = "zgcd(n, p)" in allE)
   167   apply (auto simp add: zgcd_zdvd2 [of n p] zgcd_geq_zero)
   168   apply (insert zgcd_zdvd1 [of n p], auto)
   169   done
   170 
   171 lemma zless_zprime_imp_zrelprime:
   172     "zprime p ==> 0 < n ==> n < p ==> zgcd (n, p) = 1"
   173   apply (erule zprime_imp_zrelprime)
   174   apply (erule zdvd_not_zless, assumption)
   175   done
   176 
   177 lemma zprime_zdvd_zmult:
   178     "0 \<le> (m::int) ==> zprime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
   179   apply safe
   180   apply (rule zrelprime_zdvd_zmult)
   181    apply (rule zprime_imp_zrelprime, auto)
   182   done
   183 
   184 lemma zgcd_zadd_zmult [simp]: "zgcd (m + n * k, n) = zgcd (m, n)"
   185   apply (rule zgcd_eq [THEN trans])
   186   apply (simp add: zmod_zadd1_eq)
   187   apply (rule zgcd_eq [symmetric])
   188   done
   189 
   190 lemma zgcd_zdvd_zgcd_zmult: "zgcd (m, n) dvd zgcd (k * m, n)"
   191   apply (simp add: zgcd_greatest_iff)
   192   apply (blast intro: zdvd_trans)
   193   done
   194 
   195 lemma zgcd_zmult_zdvd_zgcd:
   196     "zgcd (k, n) = 1 ==> zgcd (k * m, n) dvd zgcd (m, n)"
   197   apply (simp add: zgcd_greatest_iff)
   198   apply (rule_tac n = k in zrelprime_zdvd_zmult)
   199    prefer 2
   200    apply (simp add: zmult_commute)
   201   apply (subgoal_tac "zgcd (k, zgcd (k * m, n)) = zgcd (k * m, zgcd (k, n))")
   202    apply simp
   203   apply (simp (no_asm) add: zgcd_ac)
   204   done
   205 
   206 lemma zgcd_zmult_cancel: "zgcd (k, n) = 1 ==> zgcd (k * m, n) = zgcd (m, n)"
   207   by (simp add: zgcd_def nat_abs_mult_distrib gcd_mult_cancel)
   208 
   209 lemma zgcd_zgcd_zmult:
   210     "zgcd (k, m) = 1 ==> zgcd (n, m) = 1 ==> zgcd (k * n, m) = 1"
   211   by (simp add: zgcd_zmult_cancel)
   212 
   213 lemma zdvd_iff_zgcd: "0 < m ==> (m dvd n) = (zgcd (n, m) = m)"
   214   apply safe
   215    apply (rule_tac [2] n = "zgcd (n, m)" in zdvd_trans)
   216     apply (rule_tac [3] zgcd_zdvd1, simp_all)
   217   apply (unfold dvd_def, auto)
   218   done
   219 
   220 
   221 subsection {* Congruences *}
   222 
   223 lemma zcong_1 [simp]: "[a = b] (mod 1)"
   224   by (unfold zcong_def, auto)
   225 
   226 lemma zcong_refl [simp]: "[k = k] (mod m)"
   227   by (unfold zcong_def, auto)
   228 
   229 lemma zcong_sym: "[a = b] (mod m) = [b = a] (mod m)"
   230   apply (unfold zcong_def dvd_def, auto)
   231    apply (rule_tac [!] x = "-k" in exI, auto)
   232   done
   233 
   234 lemma zcong_zadd:
   235     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a + c = b + d] (mod m)"
   236   apply (unfold zcong_def)
   237   apply (rule_tac s = "(a - b) + (c - d)" in subst)
   238    apply (rule_tac [2] zdvd_zadd, auto)
   239   done
   240 
   241 lemma zcong_zdiff:
   242     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a - c = b - d] (mod m)"
   243   apply (unfold zcong_def)
   244   apply (rule_tac s = "(a - b) - (c - d)" in subst)
   245    apply (rule_tac [2] zdvd_zdiff, auto)
   246   done
   247 
   248 lemma zcong_trans:
   249     "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
   250   apply (unfold zcong_def dvd_def, auto)
   251   apply (rule_tac x = "k + ka" in exI)
   252   apply (simp add: zadd_ac zadd_zmult_distrib2)
   253   done
   254 
   255 lemma zcong_zmult:
   256     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
   257   apply (rule_tac b = "b * c" in zcong_trans)
   258    apply (unfold zcong_def)
   259    apply (rule_tac s = "c * (a - b)" in subst)
   260     apply (rule_tac [3] s = "b * (c - d)" in subst)
   261      prefer 4
   262      apply (blast intro: zdvd_zmult)
   263     prefer 2
   264     apply (blast intro: zdvd_zmult)
   265    apply (simp_all add: zdiff_zmult_distrib2 zmult_commute)
   266   done
   267 
   268 lemma zcong_scalar: "[a = b] (mod m) ==> [a * k = b * k] (mod m)"
   269   by (rule zcong_zmult, simp_all)
   270 
   271 lemma zcong_scalar2: "[a = b] (mod m) ==> [k * a = k * b] (mod m)"
   272   by (rule zcong_zmult, simp_all)
   273 
   274 lemma zcong_zmult_self: "[a * m = b * m] (mod m)"
   275   apply (unfold zcong_def)
   276   apply (rule zdvd_zdiff, simp_all)
   277   done
   278 
   279 lemma zcong_square:
   280    "[| zprime p;  0 < a;  [a * a = 1] (mod p)|]
   281     ==> [a = 1] (mod p) \<or> [a = p - 1] (mod p)"
   282   apply (unfold zcong_def)
   283   apply (rule zprime_zdvd_zmult)
   284     apply (rule_tac [3] s = "a * a - 1 + p * (1 - a)" in subst)
   285      prefer 4
   286      apply (simp add: zdvd_reduce)
   287     apply (simp_all add: zdiff_zmult_distrib zmult_commute zdiff_zmult_distrib2)
   288   done
   289 
   290 lemma zcong_cancel:
   291   "0 \<le> m ==>
   292     zgcd (k, m) = 1 ==> [a * k = b * k] (mod m) = [a = b] (mod m)"
   293   apply safe
   294    prefer 2
   295    apply (blast intro: zcong_scalar)
   296   apply (case_tac "b < a")
   297    prefer 2
   298    apply (subst zcong_sym)
   299    apply (unfold zcong_def)
   300    apply (rule_tac [!] zrelprime_zdvd_zmult)
   301      apply (simp_all add: zdiff_zmult_distrib)
   302   apply (subgoal_tac "m dvd (-(a * k - b * k))")
   303    apply simp
   304   apply (subst zdvd_zminus_iff, assumption)
   305   done
   306 
   307 lemma zcong_cancel2:
   308   "0 \<le> m ==>
   309     zgcd (k, m) = 1 ==> [k * a = k * b] (mod m) = [a = b] (mod m)"
   310   by (simp add: zmult_commute zcong_cancel)
   311 
   312 lemma zcong_zgcd_zmult_zmod:
   313   "[a = b] (mod m) ==> [a = b] (mod n) ==> zgcd (m, n) = 1
   314     ==> [a = b] (mod m * n)"
   315   apply (unfold zcong_def dvd_def, auto)
   316   apply (subgoal_tac "m dvd n * ka")
   317    apply (subgoal_tac "m dvd ka")
   318     apply (case_tac [2] "0 \<le> ka")
   319      prefer 3
   320      apply (subst zdvd_zminus_iff [symmetric])
   321      apply (rule_tac n = n in zrelprime_zdvd_zmult)
   322       apply (simp add: zgcd_commute)
   323      apply (simp add: zmult_commute zdvd_zminus_iff)
   324     prefer 2
   325     apply (rule_tac n = n in zrelprime_zdvd_zmult)
   326      apply (simp add: zgcd_commute)
   327     apply (simp add: zmult_commute)
   328    apply (auto simp add: dvd_def)
   329   done
   330 
   331 lemma zcong_zless_imp_eq:
   332   "0 \<le> a ==>
   333     a < m ==> 0 \<le> b ==> b < m ==> [a = b] (mod m) ==> a = b"
   334   apply (unfold zcong_def dvd_def, auto)
   335   apply (drule_tac f = "\<lambda>z. z mod m" in arg_cong)
   336   apply (cut_tac x = a and y = b in linorder_less_linear, auto)
   337    apply (subgoal_tac [2] "(a - b) mod m = a - b")
   338     apply (rule_tac [3] mod_pos_pos_trivial, auto)
   339   apply (subgoal_tac "(m + (a - b)) mod m = m + (a - b)")
   340    apply (rule_tac [2] mod_pos_pos_trivial, auto)
   341   done
   342 
   343 lemma zcong_square_zless:
   344   "zprime p ==> 0 < a ==> a < p ==>
   345     [a * a = 1] (mod p) ==> a = 1 \<or> a = p - 1"
   346   apply (cut_tac p = p and a = a in zcong_square)
   347      apply (simp add: zprime_def)
   348     apply (auto intro: zcong_zless_imp_eq)
   349   done
   350 
   351 lemma zcong_not:
   352     "0 < a ==> a < m ==> 0 < b ==> b < a ==> \<not> [a = b] (mod m)"
   353   apply (unfold zcong_def)
   354   apply (rule zdvd_not_zless, auto)
   355   done
   356 
   357 lemma zcong_zless_0:
   358     "0 \<le> a ==> a < m ==> [a = 0] (mod m) ==> a = 0"
   359   apply (unfold zcong_def dvd_def, auto)
   360   apply (subgoal_tac "0 < m")
   361    apply (simp add: zero_le_mult_iff)
   362    apply (subgoal_tac "m * k < m * 1")
   363     apply (drule mult_less_cancel_left [THEN iffD1])
   364     apply (auto simp add: linorder_neq_iff)
   365   done
   366 
   367 lemma zcong_zless_unique:
   368     "0 < m ==> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
   369   apply auto
   370    apply (subgoal_tac [2] "[b = y] (mod m)")
   371     apply (case_tac [2] "b = 0")
   372      apply (case_tac [3] "y = 0")
   373       apply (auto intro: zcong_trans zcong_zless_0 zcong_zless_imp_eq order_less_le
   374         simp add: zcong_sym)
   375   apply (unfold zcong_def dvd_def)
   376   apply (rule_tac x = "a mod m" in exI, auto)
   377   apply (rule_tac x = "-(a div m)" in exI)
   378   apply (simp add: diff_eq_eq eq_diff_eq add_commute)
   379   done
   380 
   381 lemma zcong_iff_lin: "([a = b] (mod m)) = (\<exists>k. b = a + m * k)"
   382   apply (unfold zcong_def dvd_def, auto)
   383    apply (rule_tac [!] x = "-k" in exI, auto)
   384   done
   385 
   386 lemma zgcd_zcong_zgcd:
   387   "0 < m ==>
   388     zgcd (a, m) = 1 ==> [a = b] (mod m) ==> zgcd (b, m) = 1"
   389   by (auto simp add: zcong_iff_lin)
   390 
   391 lemma zcong_zmod_aux:
   392      "a - b = (m::int) * (a div m - b div m) + (a mod m - b mod m)"
   393   by(simp add: zdiff_zmult_distrib2 add_diff_eq eq_diff_eq add_ac)
   394 
   395 lemma zcong_zmod: "[a = b] (mod m) = [a mod m = b mod m] (mod m)"
   396   apply (unfold zcong_def)
   397   apply (rule_tac t = "a - b" in ssubst)
   398   apply (rule_tac m = m in zcong_zmod_aux)
   399   apply (rule trans)
   400    apply (rule_tac [2] k = m and m = "a div m - b div m" in zdvd_reduce)
   401   apply (simp add: zadd_commute)
   402   done
   403 
   404 lemma zcong_zmod_eq: "0 < m ==> [a = b] (mod m) = (a mod m = b mod m)"
   405   apply auto
   406    apply (rule_tac m = m in zcong_zless_imp_eq)
   407        prefer 5
   408        apply (subst zcong_zmod [symmetric], simp_all)
   409   apply (unfold zcong_def dvd_def)
   410   apply (rule_tac x = "a div m - b div m" in exI)
   411   apply (rule_tac m1 = m in zcong_zmod_aux [THEN trans], auto)
   412   done
   413 
   414 lemma zcong_zminus [iff]: "[a = b] (mod -m) = [a = b] (mod m)"
   415   by (auto simp add: zcong_def)
   416 
   417 lemma zcong_zero [iff]: "[a = b] (mod 0) = (a = b)"
   418   by (auto simp add: zcong_def)
   419 
   420 lemma "[a = b] (mod m) = (a mod m = b mod m)"
   421   apply (case_tac "m = 0", simp add: DIVISION_BY_ZERO)
   422   apply (simp add: linorder_neq_iff)
   423   apply (erule disjE)  
   424    prefer 2 apply (simp add: zcong_zmod_eq)
   425   txt{*Remainding case: @{term "m<0"}*}
   426   apply (rule_tac t = m in zminus_zminus [THEN subst])
   427   apply (subst zcong_zminus)
   428   apply (subst zcong_zmod_eq, arith)
   429   apply (frule neg_mod_bound [of _ a], frule neg_mod_bound [of _ b]) 
   430   apply (simp add: zmod_zminus2_eq_if del: neg_mod_bound)
   431   done
   432 
   433 subsection {* Modulo *}
   434 
   435 lemma zmod_zdvd_zmod:
   436     "0 < (m::int) ==> m dvd b ==> (a mod b mod m) = (a mod m)"
   437   apply (unfold dvd_def, auto)
   438   apply (subst zcong_zmod_eq [symmetric])
   439    prefer 2
   440    apply (subst zcong_iff_lin)
   441    apply (rule_tac x = "k * (a div (m * k))" in exI)
   442    apply (simp add:zmult_assoc [symmetric], assumption)
   443   done
   444 
   445 
   446 subsection {* Extended GCD *}
   447 
   448 declare xzgcda.simps [simp del]
   449 
   450 lemma xzgcd_correct_aux1:
   451   "zgcd (r', r) = k --> 0 < r -->
   452     (\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn))"
   453   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   454     z = s and aa = t' and ab = t in xzgcda.induct)
   455   apply (subst zgcd_eq)
   456   apply (subst xzgcda.simps, auto)
   457   apply (case_tac "r' mod r = 0")
   458    prefer 2
   459    apply (frule_tac a = "r'" in pos_mod_sign, auto)
   460   apply (rule exI)
   461   apply (rule exI)
   462   apply (subst xzgcda.simps, auto)
   463   done
   464 
   465 lemma xzgcd_correct_aux2:
   466   "(\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn)) --> 0 < r -->
   467     zgcd (r', r) = k"
   468   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   469     z = s and aa = t' and ab = t in xzgcda.induct)
   470   apply (subst zgcd_eq)
   471   apply (subst xzgcda.simps)
   472   apply (auto simp add: linorder_not_le)
   473   apply (case_tac "r' mod r = 0")
   474    prefer 2
   475    apply (frule_tac a = "r'" in pos_mod_sign, auto)
   476   apply (erule_tac P = "xzgcda ?u = ?v" in rev_mp)
   477   apply (subst xzgcda.simps, auto)
   478   done
   479 
   480 lemma xzgcd_correct:
   481     "0 < n ==> (zgcd (m, n) = k) = (\<exists>s t. xzgcd m n = (k, s, t))"
   482   apply (unfold xzgcd_def)
   483   apply (rule iffI)
   484    apply (rule_tac [2] xzgcd_correct_aux2 [THEN mp, THEN mp])
   485     apply (rule xzgcd_correct_aux1 [THEN mp, THEN mp], auto)
   486   done
   487 
   488 
   489 text {* \medskip @{term xzgcd} linear *}
   490 
   491 lemma xzgcda_linear_aux1:
   492   "(a - r * b) * m + (c - r * d) * (n::int) =
   493    (a * m + c * n) - r * (b * m + d * n)"
   494   by (simp add: zdiff_zmult_distrib zadd_zmult_distrib2 zmult_assoc)
   495 
   496 lemma xzgcda_linear_aux2:
   497   "r' = s' * m + t' * n ==> r = s * m + t * n
   498     ==> (r' mod r) = (s' - (r' div r) * s) * m + (t' - (r' div r) * t) * (n::int)"
   499   apply (rule trans)
   500    apply (rule_tac [2] xzgcda_linear_aux1 [symmetric])
   501   apply (simp add: eq_diff_eq mult_commute)
   502   done
   503 
   504 lemma order_le_neq_implies_less: "(x::'a::order) \<le> y ==> x \<noteq> y ==> x < y"
   505   by (rule iffD2 [OF order_less_le conjI])
   506 
   507 lemma xzgcda_linear [rule_format]:
   508   "0 < r --> xzgcda (m, n, r', r, s', s, t', t) = (rn, sn, tn) -->
   509     r' = s' * m + t' * n -->  r = s * m + t * n --> rn = sn * m + tn * n"
   510   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   511     z = s and aa = t' and ab = t in xzgcda.induct)
   512   apply (subst xzgcda.simps)
   513   apply (simp (no_asm))
   514   apply (rule impI)+
   515   apply (case_tac "r' mod r = 0")
   516    apply (simp add: xzgcda.simps, clarify)
   517   apply (subgoal_tac "0 < r' mod r")
   518    apply (rule_tac [2] order_le_neq_implies_less)
   519    apply (rule_tac [2] pos_mod_sign)
   520     apply (cut_tac m = m and n = n and r' = r' and r = r and s' = s' and
   521       s = s and t' = t' and t = t in xzgcda_linear_aux2, auto)
   522   done
   523 
   524 lemma xzgcd_linear:
   525     "0 < n ==> xzgcd m n = (r, s, t) ==> r = s * m + t * n"
   526   apply (unfold xzgcd_def)
   527   apply (erule xzgcda_linear, assumption, auto)
   528   done
   529 
   530 lemma zgcd_ex_linear:
   531     "0 < n ==> zgcd (m, n) = k ==> (\<exists>s t. k = s * m + t * n)"
   532   apply (simp add: xzgcd_correct, safe)
   533   apply (rule exI)+
   534   apply (erule xzgcd_linear, auto)
   535   done
   536 
   537 lemma zcong_lineq_ex:
   538     "0 < n ==> zgcd (a, n) = 1 ==> \<exists>x. [a * x = 1] (mod n)"
   539   apply (cut_tac m = a and n = n and k = 1 in zgcd_ex_linear, safe)
   540   apply (rule_tac x = s in exI)
   541   apply (rule_tac b = "s * a + t * n" in zcong_trans)
   542    prefer 2
   543    apply simp
   544   apply (unfold zcong_def)
   545   apply (simp (no_asm) add: zmult_commute zdvd_zminus_iff)
   546   done
   547 
   548 lemma zcong_lineq_unique:
   549   "0 < n ==>
   550     zgcd (a, n) = 1 ==> \<exists>!x. 0 \<le> x \<and> x < n \<and> [a * x = b] (mod n)"
   551   apply auto
   552    apply (rule_tac [2] zcong_zless_imp_eq)
   553        apply (tactic {* stac (thm "zcong_cancel2" RS sym) 6 *})
   554          apply (rule_tac [8] zcong_trans)
   555           apply (simp_all (no_asm_simp))
   556    prefer 2
   557    apply (simp add: zcong_sym)
   558   apply (cut_tac a = a and n = n in zcong_lineq_ex, auto)
   559   apply (rule_tac x = "x * b mod n" in exI, safe)
   560     apply (simp_all (no_asm_simp))
   561   apply (subst zcong_zmod)
   562   apply (subst zmod_zmult1_eq [symmetric])
   563   apply (subst zcong_zmod [symmetric])
   564   apply (subgoal_tac "[a * x * b = 1 * b] (mod n)")
   565    apply (rule_tac [2] zcong_zmult)
   566     apply (simp_all add: zmult_assoc)
   567   done
   568 
   569 end