src/HOL/NumberTheory/IntPrimes.thy
author haftmann
Wed Sep 26 20:27:55 2007 +0200 (2007-09-26)
changeset 24728 e2b3a1065676
parent 24181 102ebceaa495
child 24759 b448f94b1c88
permissions -rw-r--r--
moved Finite_Set before Datatype
     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 definition
    35   zgcd :: "int * int => int" where
    36   "zgcd = (\<lambda>(x,y). int (gcd (nat (abs x), nat (abs y))))"
    37 
    38 definition
    39   zprime :: "int \<Rightarrow> bool" where
    40   "zprime p = (1 < p \<and> (\<forall>m. 0 <= m & m dvd p --> m = 1 \<or> m = p))"
    41 
    42 definition
    43   xzgcd :: "int => int => int * int * int" where
    44   "xzgcd m n = xzgcda (m, n, m, n, 1, 0, 0, 1)"
    45 
    46 definition
    47   zcong :: "int => int => int => bool"  ("(1[_ = _] '(mod _'))") where
    48   "[a = b] (mod m) = (m dvd (a - b))"
    49 
    50 
    51 
    52 text {* \medskip @{term gcd} lemmas *}
    53 
    54 lemma gcd_add1_eq: "gcd (m + k, k) = gcd (m + k, m)"
    55   by (simp add: gcd_commute)
    56 
    57 lemma gcd_diff2: "m \<le> n ==> gcd (n, n - m) = gcd (n, m)"
    58   apply (subgoal_tac "n = m + (n - m)")
    59    apply (erule ssubst, rule gcd_add1_eq, simp)
    60   done
    61 
    62 
    63 subsection {* Euclid's Algorithm and GCD *}
    64 
    65 lemma zgcd_0 [simp]: "zgcd (m, 0) = abs m"
    66   by (simp add: zgcd_def abs_if)
    67 
    68 lemma zgcd_0_left [simp]: "zgcd (0, m) = abs m"
    69   by (simp add: zgcd_def abs_if)
    70 
    71 lemma zgcd_zminus [simp]: "zgcd (-m, n) = zgcd (m, n)"
    72   by (simp add: zgcd_def)
    73 
    74 lemma zgcd_zminus2 [simp]: "zgcd (m, -n) = zgcd (m, n)"
    75   by (simp add: zgcd_def)
    76 
    77 lemma zgcd_non_0: "0 < n ==> zgcd (m, n) = zgcd (n, m mod n)"
    78   apply (frule_tac b = n and a = m in pos_mod_sign)
    79   apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
    80   apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
    81   apply (frule_tac a = m in pos_mod_bound)
    82   apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
    83   done
    84 
    85 lemma zgcd_eq: "zgcd (m, n) = zgcd (n, m mod n)"
    86   apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
    87   apply (auto simp add: linorder_neq_iff zgcd_non_0)
    88   apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
    89   done
    90 
    91 lemma zgcd_1 [simp]: "zgcd (m, 1) = 1"
    92   by (simp add: zgcd_def abs_if)
    93 
    94 lemma zgcd_0_1_iff [simp]: "(zgcd (0, m) = 1) = (abs m = 1)"
    95   by (simp add: zgcd_def abs_if)
    96 
    97 lemma zgcd_zdvd1 [iff]: "zgcd (m, n) dvd m"
    98   by (simp add: zgcd_def abs_if int_dvd_iff)
    99 
   100 lemma zgcd_zdvd2 [iff]: "zgcd (m, n) dvd n"
   101   by (simp add: zgcd_def abs_if int_dvd_iff)
   102 
   103 lemma zgcd_greatest_iff: "k dvd zgcd (m, n) = (k dvd m \<and> k dvd n)"
   104   by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
   105 
   106 lemma zgcd_commute: "zgcd (m, n) = zgcd (n, m)"
   107   by (simp add: zgcd_def gcd_commute)
   108 
   109 lemma zgcd_1_left [simp]: "zgcd (1, m) = 1"
   110   by (simp add: zgcd_def gcd_1_left)
   111 
   112 lemma zgcd_assoc: "zgcd (zgcd (k, m), n) = zgcd (k, zgcd (m, n))"
   113   by (simp add: zgcd_def gcd_assoc)
   114 
   115 lemma zgcd_left_commute: "zgcd (k, zgcd (m, n)) = zgcd (m, zgcd (k, n))"
   116   apply (rule zgcd_commute [THEN trans])
   117   apply (rule zgcd_assoc [THEN trans])
   118   apply (rule zgcd_commute [THEN arg_cong])
   119   done
   120 
   121 lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
   122   -- {* addition is an AC-operator *}
   123 
   124 lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd (m, n) = zgcd (k * m, k * n)"
   125   by (simp del: minus_mult_right [symmetric]
   126       add: minus_mult_right nat_mult_distrib zgcd_def abs_if
   127           mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
   128 
   129 lemma zgcd_zmult_distrib2_abs: "zgcd (k * m, k * n) = abs k * zgcd (m, n)"
   130   by (simp add: abs_if zgcd_zmult_distrib2)
   131 
   132 lemma zgcd_self [simp]: "0 \<le> m ==> zgcd (m, m) = m"
   133   by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
   134 
   135 lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd (k, k * n) = k"
   136   by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
   137 
   138 lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n, k) = k"
   139   by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
   140 
   141 lemma zrelprime_zdvd_zmult_aux:
   142      "zgcd (n, k) = 1 ==> k dvd m * n ==> 0 \<le> m ==> k dvd m"
   143   by (metis abs_of_nonneg zdvd_triv_right zgcd_greatest_iff zgcd_zmult_distrib2_abs zmult_1_right)
   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 (metis zgcd_commute zgcd_geq_zero zgcd_zdvd1 zgcd_zdvd2)
   167   done
   168 
   169 lemma zless_zprime_imp_zrelprime:
   170     "zprime p ==> 0 < n ==> n < p ==> zgcd (n, p) = 1"
   171   apply (erule zprime_imp_zrelprime)
   172   apply (erule zdvd_not_zless, assumption)
   173   done
   174 
   175 lemma zprime_zdvd_zmult:
   176     "0 \<le> (m::int) ==> zprime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
   177   by (metis igcd_dvd1 igcd_dvd2 igcd_pos zprime_def zrelprime_dvd_mult)
   178 
   179 lemma zgcd_zadd_zmult [simp]: "zgcd (m + n * k, n) = zgcd (m, n)"
   180   apply (rule zgcd_eq [THEN trans])
   181   apply (simp add: zmod_zadd1_eq)
   182   apply (rule zgcd_eq [symmetric])
   183   done
   184 
   185 lemma zgcd_zdvd_zgcd_zmult: "zgcd (m, n) dvd zgcd (k * m, n)"
   186   apply (simp add: zgcd_greatest_iff)
   187   apply (blast intro: zdvd_trans)
   188   done
   189 
   190 lemma zgcd_zmult_zdvd_zgcd:
   191     "zgcd (k, n) = 1 ==> zgcd (k * m, n) dvd zgcd (m, n)"
   192   apply (simp add: zgcd_greatest_iff)
   193   apply (rule_tac n = k in zrelprime_zdvd_zmult)
   194    prefer 2
   195    apply (simp add: zmult_commute)
   196   apply (metis zgcd_1 zgcd_commute zgcd_left_commute)
   197   done
   198 
   199 lemma zgcd_zmult_cancel: "zgcd (k, n) = 1 ==> zgcd (k * m, n) = zgcd (m, n)"
   200   by (simp add: zgcd_def nat_abs_mult_distrib gcd_mult_cancel)
   201 
   202 lemma zgcd_zgcd_zmult:
   203     "zgcd (k, m) = 1 ==> zgcd (n, m) = 1 ==> zgcd (k * n, m) = 1"
   204   by (simp add: zgcd_zmult_cancel)
   205 
   206 lemma zdvd_iff_zgcd: "0 < m ==> (m dvd n) = (zgcd (n, m) = m)"
   207   by (metis abs_of_pos zdvd_mult_div_cancel zgcd_0 zgcd_commute zgcd_geq_zero zgcd_zdvd2 zgcd_zmult_eq_self)
   208 
   209 
   210 
   211 subsection {* Congruences *}
   212 
   213 lemma zcong_1 [simp]: "[a = b] (mod 1)"
   214   by (unfold zcong_def, auto)
   215 
   216 lemma zcong_refl [simp]: "[k = k] (mod m)"
   217   by (unfold zcong_def, auto)
   218 
   219 lemma zcong_sym: "[a = b] (mod m) = [b = a] (mod m)"
   220   apply (unfold zcong_def dvd_def, auto)
   221    apply (rule_tac [!] x = "-k" in exI, auto)
   222   done
   223 
   224 lemma zcong_zadd:
   225     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a + c = b + d] (mod m)"
   226   apply (unfold zcong_def)
   227   apply (rule_tac s = "(a - b) + (c - d)" in subst)
   228    apply (rule_tac [2] zdvd_zadd, auto)
   229   done
   230 
   231 lemma zcong_zdiff:
   232     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a - c = b - d] (mod m)"
   233   apply (unfold zcong_def)
   234   apply (rule_tac s = "(a - b) - (c - d)" in subst)
   235    apply (rule_tac [2] zdvd_zdiff, auto)
   236   done
   237 
   238 lemma zcong_trans:
   239     "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
   240   apply (unfold zcong_def dvd_def, auto)
   241   apply (rule_tac x = "k + ka" in exI)
   242   apply (simp add: zadd_ac zadd_zmult_distrib2)
   243   done
   244 
   245 lemma zcong_zmult:
   246     "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
   247   apply (rule_tac b = "b * c" in zcong_trans)
   248    apply (unfold zcong_def)
   249   apply (metis zdiff_zmult_distrib2 zdvd_zmult zmult_commute)
   250   apply (metis zdiff_zmult_distrib2 zdvd_zmult)
   251   done
   252 
   253 lemma zcong_scalar: "[a = b] (mod m) ==> [a * k = b * k] (mod m)"
   254   by (rule zcong_zmult, simp_all)
   255 
   256 lemma zcong_scalar2: "[a = b] (mod m) ==> [k * a = k * b] (mod m)"
   257   by (rule zcong_zmult, simp_all)
   258 
   259 lemma zcong_zmult_self: "[a * m = b * m] (mod m)"
   260   apply (unfold zcong_def)
   261   apply (rule zdvd_zdiff, simp_all)
   262   done
   263 
   264 lemma zcong_square:
   265    "[| zprime p;  0 < a;  [a * a = 1] (mod p)|]
   266     ==> [a = 1] (mod p) \<or> [a = p - 1] (mod p)"
   267   apply (unfold zcong_def)
   268   apply (rule zprime_zdvd_zmult)
   269     apply (rule_tac [3] s = "a * a - 1 + p * (1 - a)" in subst)
   270      prefer 4
   271      apply (simp add: zdvd_reduce)
   272     apply (simp_all add: zdiff_zmult_distrib zmult_commute zdiff_zmult_distrib2)
   273   done
   274 
   275 lemma zcong_cancel:
   276   "0 \<le> m ==>
   277     zgcd (k, m) = 1 ==> [a * k = b * k] (mod m) = [a = b] (mod m)"
   278   apply safe
   279    prefer 2
   280    apply (blast intro: zcong_scalar)
   281   apply (case_tac "b < a")
   282    prefer 2
   283    apply (subst zcong_sym)
   284    apply (unfold zcong_def)
   285    apply (rule_tac [!] zrelprime_zdvd_zmult)
   286      apply (simp_all add: zdiff_zmult_distrib)
   287   apply (subgoal_tac "m dvd (-(a * k - b * k))")
   288    apply simp
   289   apply (subst zdvd_zminus_iff, assumption)
   290   done
   291 
   292 lemma zcong_cancel2:
   293   "0 \<le> m ==>
   294     zgcd (k, m) = 1 ==> [k * a = k * b] (mod m) = [a = b] (mod m)"
   295   by (simp add: zmult_commute zcong_cancel)
   296 
   297 lemma zcong_zgcd_zmult_zmod:
   298   "[a = b] (mod m) ==> [a = b] (mod n) ==> zgcd (m, n) = 1
   299     ==> [a = b] (mod m * n)"
   300   apply (unfold zcong_def dvd_def, auto)
   301   apply (subgoal_tac "m dvd n * ka")
   302    apply (subgoal_tac "m dvd ka")
   303     apply (case_tac [2] "0 \<le> ka")
   304   apply (metis zdvd_mult_div_cancel zdvd_refl zdvd_zminus2_iff zdvd_zmultD2 zgcd_zminus zmult_commute zmult_zminus zrelprime_zdvd_zmult)
   305   apply (metis IntDiv.zdvd_abs1 abs_of_nonneg zadd_0 zgcd_0_left zgcd_commute zgcd_zadd_zmult zgcd_zdvd_zgcd_zmult zgcd_zmult_distrib2_abs zmult_1_right zmult_commute)
   306   apply (metis abs_eq_0 int_0_neq_1 mult_le_0_iff  zdvd_mono zdvd_mult_cancel zdvd_mult_cancel1 zdvd_refl zdvd_triv_left zdvd_zmult2 zero_le_mult_iff zgcd_greatest_iff zle_anti_sym zle_linear zle_refl zmult_commute zrelprime_zdvd_zmult)
   307   apply (metis zdvd_triv_left)
   308   done
   309 
   310 lemma zcong_zless_imp_eq:
   311   "0 \<le> a ==>
   312     a < m ==> 0 \<le> b ==> b < m ==> [a = b] (mod m) ==> a = b"
   313   apply (unfold zcong_def dvd_def, auto)
   314   apply (drule_tac f = "\<lambda>z. z mod m" in arg_cong)
   315   apply (metis diff_add_cancel mod_pos_pos_trivial zadd_0 zadd_commute zmod_eq_0_iff zmod_zadd_right_eq)
   316   done
   317 
   318 lemma zcong_square_zless:
   319   "zprime p ==> 0 < a ==> a < p ==>
   320     [a * a = 1] (mod p) ==> a = 1 \<or> a = p - 1"
   321   apply (cut_tac p = p and a = a in zcong_square)
   322      apply (simp add: zprime_def)
   323     apply (auto intro: zcong_zless_imp_eq)
   324   done
   325 
   326 lemma zcong_not:
   327     "0 < a ==> a < m ==> 0 < b ==> b < a ==> \<not> [a = b] (mod m)"
   328   apply (unfold zcong_def)
   329   apply (rule zdvd_not_zless, auto)
   330   done
   331 
   332 lemma zcong_zless_0:
   333     "0 \<le> a ==> a < m ==> [a = 0] (mod m) ==> a = 0"
   334   apply (unfold zcong_def dvd_def, auto)
   335   apply (metis div_pos_pos_trivial linorder_not_less zdiv_zmult_self2 zle_refl zle_trans)
   336   done
   337 
   338 lemma zcong_zless_unique:
   339     "0 < m ==> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
   340   apply auto
   341    prefer 2 apply (metis zcong_sym zcong_trans zcong_zless_imp_eq)
   342   apply (unfold zcong_def dvd_def)
   343   apply (rule_tac x = "a mod m" in exI, auto)
   344   apply (metis zmult_div_cancel)
   345   done
   346 
   347 lemma zcong_iff_lin: "([a = b] (mod m)) = (\<exists>k. b = a + m * k)"
   348   apply (unfold zcong_def dvd_def, auto)
   349    apply (rule_tac [!] x = "-k" in exI, auto)
   350   done
   351 
   352 lemma zgcd_zcong_zgcd:
   353   "0 < m ==>
   354     zgcd (a, m) = 1 ==> [a = b] (mod m) ==> zgcd (b, m) = 1"
   355   by (auto simp add: zcong_iff_lin)
   356 
   357 lemma zcong_zmod_aux:
   358      "a - b = (m::int) * (a div m - b div m) + (a mod m - b mod m)"
   359   by(simp add: zdiff_zmult_distrib2 add_diff_eq eq_diff_eq add_ac)
   360 
   361 lemma zcong_zmod: "[a = b] (mod m) = [a mod m = b mod m] (mod m)"
   362   apply (unfold zcong_def)
   363   apply (rule_tac t = "a - b" in ssubst)
   364   apply (rule_tac m = m in zcong_zmod_aux)
   365   apply (rule trans)
   366    apply (rule_tac [2] k = m and m = "a div m - b div m" in zdvd_reduce)
   367   apply (simp add: zadd_commute)
   368   done
   369 
   370 lemma zcong_zmod_eq: "0 < m ==> [a = b] (mod m) = (a mod m = b mod m)"
   371   apply auto
   372   apply (metis pos_mod_conj zcong_zless_imp_eq zcong_zmod)
   373   apply (metis zcong_refl zcong_zmod)
   374   done
   375 
   376 lemma zcong_zminus [iff]: "[a = b] (mod -m) = [a = b] (mod m)"
   377   by (auto simp add: zcong_def)
   378 
   379 lemma zcong_zero [iff]: "[a = b] (mod 0) = (a = b)"
   380   by (auto simp add: zcong_def)
   381 
   382 lemma "[a = b] (mod m) = (a mod m = b mod m)"
   383   apply (case_tac "m = 0", simp add: DIVISION_BY_ZERO)
   384   apply (simp add: linorder_neq_iff)
   385   apply (erule disjE)  
   386    prefer 2 apply (simp add: zcong_zmod_eq)
   387   txt{*Remainding case: @{term "m<0"}*}
   388   apply (rule_tac t = m in zminus_zminus [THEN subst])
   389   apply (subst zcong_zminus)
   390   apply (subst zcong_zmod_eq, arith)
   391   apply (frule neg_mod_bound [of _ a], frule neg_mod_bound [of _ b]) 
   392   apply (simp add: zmod_zminus2_eq_if del: neg_mod_bound)
   393   done
   394 
   395 subsection {* Modulo *}
   396 
   397 lemma zmod_zdvd_zmod:
   398     "0 < (m::int) ==> m dvd b ==> (a mod b mod m) = (a mod m)"
   399   apply (unfold dvd_def, auto)
   400   apply (subst zcong_zmod_eq [symmetric])
   401    prefer 2
   402    apply (subst zcong_iff_lin)
   403    apply (rule_tac x = "k * (a div (m * k))" in exI)
   404    apply (simp add:zmult_assoc [symmetric], assumption)
   405   done
   406 
   407 
   408 subsection {* Extended GCD *}
   409 
   410 declare xzgcda.simps [simp del]
   411 
   412 lemma xzgcd_correct_aux1:
   413   "zgcd (r', r) = k --> 0 < r -->
   414     (\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn))"
   415   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   416     z = s and aa = t' and ab = t in xzgcda.induct)
   417   apply (subst zgcd_eq)
   418   apply (subst xzgcda.simps, auto)
   419   apply (metis abs_of_pos pos_mod_conj simps zgcd_0 zgcd_eq zle_refl zless_le)
   420   done
   421 
   422 lemma xzgcd_correct_aux2:
   423   "(\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn)) --> 0 < r -->
   424     zgcd (r', r) = k"
   425   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   426     z = s and aa = t' and ab = t in xzgcda.induct)
   427   apply (subst zgcd_eq)
   428   apply (subst xzgcda.simps)
   429   apply (auto simp add: linorder_not_le)
   430   apply (case_tac "r' mod r = 0")
   431    prefer 2
   432    apply (frule_tac a = "r'" in pos_mod_sign, auto)
   433   apply (metis Pair_eq simps zle_refl)
   434   done
   435 
   436 lemma xzgcd_correct:
   437     "0 < n ==> (zgcd (m, n) = k) = (\<exists>s t. xzgcd m n = (k, s, t))"
   438   apply (unfold xzgcd_def)
   439   apply (rule iffI)
   440    apply (rule_tac [2] xzgcd_correct_aux2 [THEN mp, THEN mp])
   441     apply (rule xzgcd_correct_aux1 [THEN mp, THEN mp], auto)
   442   done
   443 
   444 
   445 text {* \medskip @{term xzgcd} linear *}
   446 
   447 lemma xzgcda_linear_aux1:
   448   "(a - r * b) * m + (c - r * d) * (n::int) =
   449    (a * m + c * n) - r * (b * m + d * n)"
   450   by (simp add: zdiff_zmult_distrib zadd_zmult_distrib2 zmult_assoc)
   451 
   452 lemma xzgcda_linear_aux2:
   453   "r' = s' * m + t' * n ==> r = s * m + t * n
   454     ==> (r' mod r) = (s' - (r' div r) * s) * m + (t' - (r' div r) * t) * (n::int)"
   455   apply (rule trans)
   456    apply (rule_tac [2] xzgcda_linear_aux1 [symmetric])
   457   apply (simp add: eq_diff_eq mult_commute)
   458   done
   459 
   460 lemma order_le_neq_implies_less: "(x::'a::order) \<le> y ==> x \<noteq> y ==> x < y"
   461   by (rule iffD2 [OF order_less_le conjI])
   462 
   463 lemma xzgcda_linear [rule_format]:
   464   "0 < r --> xzgcda (m, n, r', r, s', s, t', t) = (rn, sn, tn) -->
   465     r' = s' * m + t' * n -->  r = s * m + t * n --> rn = sn * m + tn * n"
   466   apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
   467     z = s and aa = t' and ab = t in xzgcda.induct)
   468   apply (subst xzgcda.simps)
   469   apply (simp (no_asm))
   470   apply (rule impI)+
   471   apply (case_tac "r' mod r = 0")
   472    apply (simp add: xzgcda.simps, clarify)
   473   apply (subgoal_tac "0 < r' mod r")
   474    apply (rule_tac [2] order_le_neq_implies_less)
   475    apply (rule_tac [2] pos_mod_sign)
   476     apply (cut_tac m = m and n = n and r' = r' and r = r and s' = s' and
   477       s = s and t' = t' and t = t in xzgcda_linear_aux2, auto)
   478   done
   479 
   480 lemma xzgcd_linear:
   481     "0 < n ==> xzgcd m n = (r, s, t) ==> r = s * m + t * n"
   482   apply (unfold xzgcd_def)
   483   apply (erule xzgcda_linear, assumption, auto)
   484   done
   485 
   486 lemma zgcd_ex_linear:
   487     "0 < n ==> zgcd (m, n) = k ==> (\<exists>s t. k = s * m + t * n)"
   488   apply (simp add: xzgcd_correct, safe)
   489   apply (rule exI)+
   490   apply (erule xzgcd_linear, auto)
   491   done
   492 
   493 lemma zcong_lineq_ex:
   494     "0 < n ==> zgcd (a, n) = 1 ==> \<exists>x. [a * x = 1] (mod n)"
   495   apply (cut_tac m = a and n = n and k = 1 in zgcd_ex_linear, safe)
   496   apply (rule_tac x = s in exI)
   497   apply (rule_tac b = "s * a + t * n" in zcong_trans)
   498    prefer 2
   499    apply simp
   500   apply (unfold zcong_def)
   501   apply (simp (no_asm) add: zmult_commute zdvd_zminus_iff)
   502   done
   503 
   504 lemma zcong_lineq_unique:
   505   "0 < n ==>
   506     zgcd (a, n) = 1 ==> \<exists>!x. 0 \<le> x \<and> x < n \<and> [a * x = b] (mod n)"
   507   apply auto
   508    apply (rule_tac [2] zcong_zless_imp_eq)
   509        apply (tactic {* stac (thm "zcong_cancel2" RS sym) 6 *})
   510          apply (rule_tac [8] zcong_trans)
   511           apply (simp_all (no_asm_simp))
   512    prefer 2
   513    apply (simp add: zcong_sym)
   514   apply (cut_tac a = a and n = n in zcong_lineq_ex, auto)
   515   apply (rule_tac x = "x * b mod n" in exI, safe)
   516     apply (simp_all (no_asm_simp))
   517   apply (metis zcong_scalar zcong_zmod zmod_zmult1_eq zmult_1 zmult_assoc)
   518   done
   519 
   520 end