src/HOL/Old_Number_Theory/IntPrimes.thy
author haftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963 977b94b64905
parent 33657 a4179bf442d1
child 35440 bdf8ad377877
permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann@32479
     1
(*  Author:     Thomas M. Rasmussen
wenzelm@11049
     2
    Copyright   2000  University of Cambridge
paulson@9508
     3
*)
paulson@9508
     4
wenzelm@11049
     5
header {* Divisibility and prime numbers (on integers) *}
wenzelm@11049
     6
haftmann@25596
     7
theory IntPrimes
haftmann@27368
     8
imports Main Primes
haftmann@25596
     9
begin
wenzelm@11049
    10
wenzelm@11049
    11
text {*
wenzelm@11049
    12
  The @{text dvd} relation, GCD, Euclid's extended algorithm, primes,
wenzelm@11049
    13
  congruences (all on the Integers).  Comparable to theory @{text
wenzelm@11049
    14
  Primes}, but @{text dvd} is included here as it is not present in
wenzelm@11049
    15
  main HOL.  Also includes extended GCD and congruences not present in
wenzelm@11049
    16
  @{text Primes}.
wenzelm@11049
    17
*}
wenzelm@11049
    18
wenzelm@11049
    19
wenzelm@11049
    20
subsection {* Definitions *}
paulson@9508
    21
paulson@9508
    22
consts
wenzelm@11049
    23
  xzgcda :: "int * int * int * int * int * int * int * int => int * int * int"
wenzelm@11049
    24
wenzelm@11049
    25
recdef xzgcda
wenzelm@11049
    26
  "measure ((\<lambda>(m, n, r', r, s', s, t', t). nat r)
wenzelm@11049
    27
    :: int * int * int * int *int * int * int * int => nat)"
wenzelm@11049
    28
  "xzgcda (m, n, r', r, s', s, t', t) =
wenzelm@32960
    29
        (if r \<le> 0 then (r', s', t')
wenzelm@32960
    30
         else xzgcda (m, n, r, r' mod r, 
wenzelm@32960
    31
                      s, s' - (r' div r) * s, 
wenzelm@32960
    32
                      t, t' - (r' div r) * t))"
paulson@9508
    33
wenzelm@19670
    34
definition
wenzelm@21404
    35
  zprime :: "int \<Rightarrow> bool" where
wenzelm@19670
    36
  "zprime p = (1 < p \<and> (\<forall>m. 0 <= m & m dvd p --> m = 1 \<or> m = p))"
paulson@13833
    37
wenzelm@21404
    38
definition
wenzelm@21404
    39
  xzgcd :: "int => int => int * int * int" where
wenzelm@19670
    40
  "xzgcd m n = xzgcda (m, n, m, n, 1, 0, 0, 1)"
paulson@13833
    41
wenzelm@21404
    42
definition
wenzelm@21404
    43
  zcong :: "int => int => int => bool"  ("(1[_ = _] '(mod _'))") where
wenzelm@19670
    44
  "[a = b] (mod m) = (m dvd (a - b))"
wenzelm@11049
    45
wenzelm@11049
    46
subsection {* Euclid's Algorithm and GCD *}
wenzelm@11049
    47
wenzelm@11049
    48
paulson@13833
    49
lemma zrelprime_zdvd_zmult_aux:
haftmann@27556
    50
     "zgcd n k = 1 ==> k dvd m * n ==> 0 \<le> m ==> k dvd m"
nipkow@30042
    51
    by (metis abs_of_nonneg dvd_triv_right zgcd_greatest_iff zgcd_zmult_distrib2_abs zmult_1_right)
wenzelm@11049
    52
haftmann@27556
    53
lemma zrelprime_zdvd_zmult: "zgcd n k = 1 ==> k dvd m * n ==> k dvd m"
paulson@11868
    54
  apply (case_tac "0 \<le> m")
wenzelm@13524
    55
   apply (blast intro: zrelprime_zdvd_zmult_aux)
wenzelm@11049
    56
  apply (subgoal_tac "k dvd -m")
paulson@13833
    57
   apply (rule_tac [2] zrelprime_zdvd_zmult_aux, auto)
wenzelm@11049
    58
  done
wenzelm@11049
    59
haftmann@27556
    60
lemma zgcd_geq_zero: "0 <= zgcd x y"
paulson@13833
    61
  by (auto simp add: zgcd_def)
paulson@13833
    62
paulson@13837
    63
text{*This is merely a sanity check on zprime, since the previous version
paulson@13837
    64
      denoted the empty set.*}
nipkow@16663
    65
lemma "zprime 2"
paulson@13837
    66
  apply (auto simp add: zprime_def) 
paulson@13837
    67
  apply (frule zdvd_imp_le, simp) 
paulson@13837
    68
  apply (auto simp add: order_le_less dvd_def) 
paulson@13837
    69
  done
paulson@13837
    70
wenzelm@11049
    71
lemma zprime_imp_zrelprime:
haftmann@27556
    72
    "zprime p ==> \<not> p dvd n ==> zgcd n p = 1"
paulson@13833
    73
  apply (auto simp add: zprime_def)
nipkow@30042
    74
  apply (metis zgcd_geq_zero zgcd_zdvd1 zgcd_zdvd2)
wenzelm@11049
    75
  done
wenzelm@11049
    76
wenzelm@11049
    77
lemma zless_zprime_imp_zrelprime:
haftmann@27556
    78
    "zprime p ==> 0 < n ==> n < p ==> zgcd n p = 1"
wenzelm@11049
    79
  apply (erule zprime_imp_zrelprime)
paulson@13833
    80
  apply (erule zdvd_not_zless, assumption)
wenzelm@11049
    81
  done
wenzelm@11049
    82
wenzelm@11049
    83
lemma zprime_zdvd_zmult:
nipkow@16663
    84
    "0 \<le> (m::int) ==> zprime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
chaieb@27569
    85
  by (metis zgcd_zdvd1 zgcd_zdvd2 zgcd_pos zprime_def zrelprime_dvd_mult)
wenzelm@11049
    86
haftmann@27556
    87
lemma zgcd_zadd_zmult [simp]: "zgcd (m + n * k) n = zgcd m n"
wenzelm@11049
    88
  apply (rule zgcd_eq [THEN trans])
nipkow@29948
    89
  apply (simp add: mod_add_eq)
wenzelm@11049
    90
  apply (rule zgcd_eq [symmetric])
wenzelm@11049
    91
  done
wenzelm@11049
    92
haftmann@27556
    93
lemma zgcd_zdvd_zgcd_zmult: "zgcd m n dvd zgcd (k * m) n"
nipkow@30042
    94
by (simp add: zgcd_greatest_iff)
wenzelm@11049
    95
wenzelm@11049
    96
lemma zgcd_zmult_zdvd_zgcd:
chaieb@27569
    97
    "zgcd k n = 1 ==> zgcd (k * m) n dvd zgcd m n"
wenzelm@11049
    98
  apply (simp add: zgcd_greatest_iff)
wenzelm@11049
    99
  apply (rule_tac n = k in zrelprime_zdvd_zmult)
wenzelm@11049
   100
   prefer 2
wenzelm@11049
   101
   apply (simp add: zmult_commute)
paulson@23839
   102
  apply (metis zgcd_1 zgcd_commute zgcd_left_commute)
wenzelm@11049
   103
  done
wenzelm@11049
   104
haftmann@27556
   105
lemma zgcd_zmult_cancel: "zgcd k n = 1 ==> zgcd (k * m) n = zgcd m n"
paulson@13833
   106
  by (simp add: zgcd_def nat_abs_mult_distrib gcd_mult_cancel)
wenzelm@11049
   107
wenzelm@11049
   108
lemma zgcd_zgcd_zmult:
chaieb@27569
   109
    "zgcd k m = 1 ==> zgcd n m = 1 ==> zgcd (k * n) m = 1"
paulson@13833
   110
  by (simp add: zgcd_zmult_cancel)
wenzelm@11049
   111
haftmann@27556
   112
lemma zdvd_iff_zgcd: "0 < m ==> m dvd n \<longleftrightarrow> zgcd n m = m"
paulson@23839
   113
  by (metis abs_of_pos zdvd_mult_div_cancel zgcd_0 zgcd_commute zgcd_geq_zero zgcd_zdvd2 zgcd_zmult_eq_self)
paulson@23839
   114
wenzelm@11049
   115
wenzelm@11049
   116
wenzelm@11049
   117
subsection {* Congruences *}
wenzelm@11049
   118
paulson@11868
   119
lemma zcong_1 [simp]: "[a = b] (mod 1)"
paulson@13833
   120
  by (unfold zcong_def, auto)
wenzelm@11049
   121
wenzelm@11049
   122
lemma zcong_refl [simp]: "[k = k] (mod m)"
paulson@13833
   123
  by (unfold zcong_def, auto)
paulson@9508
   124
wenzelm@11049
   125
lemma zcong_sym: "[a = b] (mod m) = [b = a] (mod m)"
nipkow@30042
   126
  unfolding zcong_def minus_diff_eq [of a, symmetric] dvd_minus_iff ..
wenzelm@11049
   127
wenzelm@11049
   128
lemma zcong_zadd:
wenzelm@11049
   129
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a + c = b + d] (mod m)"
wenzelm@11049
   130
  apply (unfold zcong_def)
wenzelm@11049
   131
  apply (rule_tac s = "(a - b) + (c - d)" in subst)
nipkow@30042
   132
   apply (rule_tac [2] dvd_add, auto)
wenzelm@11049
   133
  done
wenzelm@11049
   134
wenzelm@11049
   135
lemma zcong_zdiff:
wenzelm@11049
   136
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a - c = b - d] (mod m)"
wenzelm@11049
   137
  apply (unfold zcong_def)
wenzelm@11049
   138
  apply (rule_tac s = "(a - b) - (c - d)" in subst)
nipkow@30042
   139
   apply (rule_tac [2] dvd_diff, auto)
wenzelm@11049
   140
  done
wenzelm@11049
   141
wenzelm@11049
   142
lemma zcong_trans:
nipkow@29925
   143
  "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
nipkow@29925
   144
unfolding zcong_def by (auto elim!: dvdE simp add: algebra_simps)
wenzelm@11049
   145
wenzelm@11049
   146
lemma zcong_zmult:
wenzelm@11049
   147
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
wenzelm@11049
   148
  apply (rule_tac b = "b * c" in zcong_trans)
wenzelm@11049
   149
   apply (unfold zcong_def)
nipkow@30042
   150
  apply (metis zdiff_zmult_distrib2 dvd_mult zmult_commute)
nipkow@30042
   151
  apply (metis zdiff_zmult_distrib2 dvd_mult)
wenzelm@11049
   152
  done
wenzelm@11049
   153
wenzelm@11049
   154
lemma zcong_scalar: "[a = b] (mod m) ==> [a * k = b * k] (mod m)"
paulson@13833
   155
  by (rule zcong_zmult, simp_all)
wenzelm@11049
   156
wenzelm@11049
   157
lemma zcong_scalar2: "[a = b] (mod m) ==> [k * a = k * b] (mod m)"
paulson@13833
   158
  by (rule zcong_zmult, simp_all)
wenzelm@11049
   159
wenzelm@11049
   160
lemma zcong_zmult_self: "[a * m = b * m] (mod m)"
wenzelm@11049
   161
  apply (unfold zcong_def)
nipkow@30042
   162
  apply (rule dvd_diff, simp_all)
wenzelm@11049
   163
  done
wenzelm@11049
   164
wenzelm@11049
   165
lemma zcong_square:
nipkow@16663
   166
   "[| zprime p;  0 < a;  [a * a = 1] (mod p)|]
paulson@11868
   167
    ==> [a = 1] (mod p) \<or> [a = p - 1] (mod p)"
wenzelm@11049
   168
  apply (unfold zcong_def)
wenzelm@11049
   169
  apply (rule zprime_zdvd_zmult)
paulson@11868
   170
    apply (rule_tac [3] s = "a * a - 1 + p * (1 - a)" in subst)
wenzelm@11049
   171
     prefer 4
wenzelm@11049
   172
     apply (simp add: zdvd_reduce)
wenzelm@11049
   173
    apply (simp_all add: zdiff_zmult_distrib zmult_commute zdiff_zmult_distrib2)
wenzelm@11049
   174
  done
wenzelm@11049
   175
wenzelm@11049
   176
lemma zcong_cancel:
paulson@11868
   177
  "0 \<le> m ==>
haftmann@27556
   178
    zgcd k m = 1 ==> [a * k = b * k] (mod m) = [a = b] (mod m)"
wenzelm@11049
   179
  apply safe
wenzelm@11049
   180
   prefer 2
wenzelm@11049
   181
   apply (blast intro: zcong_scalar)
wenzelm@11049
   182
  apply (case_tac "b < a")
wenzelm@11049
   183
   prefer 2
wenzelm@11049
   184
   apply (subst zcong_sym)
wenzelm@11049
   185
   apply (unfold zcong_def)
wenzelm@11049
   186
   apply (rule_tac [!] zrelprime_zdvd_zmult)
wenzelm@11049
   187
     apply (simp_all add: zdiff_zmult_distrib)
wenzelm@11049
   188
  apply (subgoal_tac "m dvd (-(a * k - b * k))")
paulson@14271
   189
   apply simp
nipkow@30042
   190
  apply (subst dvd_minus_iff, assumption)
wenzelm@11049
   191
  done
wenzelm@11049
   192
wenzelm@11049
   193
lemma zcong_cancel2:
paulson@11868
   194
  "0 \<le> m ==>
haftmann@27556
   195
    zgcd k m = 1 ==> [k * a = k * b] (mod m) = [a = b] (mod m)"
paulson@13833
   196
  by (simp add: zmult_commute zcong_cancel)
wenzelm@11049
   197
wenzelm@11049
   198
lemma zcong_zgcd_zmult_zmod:
haftmann@27556
   199
  "[a = b] (mod m) ==> [a = b] (mod n) ==> zgcd m n = 1
wenzelm@11049
   200
    ==> [a = b] (mod m * n)"
haftmann@27651
   201
  apply (auto simp add: zcong_def dvd_def)
wenzelm@11049
   202
  apply (subgoal_tac "m dvd n * ka")
wenzelm@11049
   203
   apply (subgoal_tac "m dvd ka")
paulson@11868
   204
    apply (case_tac [2] "0 \<le> ka")
nipkow@30042
   205
  apply (metis zdvd_mult_div_cancel dvd_refl dvd_mult_left zmult_commute zrelprime_zdvd_zmult)
nipkow@30042
   206
  apply (metis abs_dvd_iff 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)
nipkow@33657
   207
  apply (metis mult_le_0_iff  zdvd_mono zdvd_mult_cancel dvd_triv_left zero_le_mult_iff zle_antisym zle_linear zle_refl zmult_commute zrelprime_zdvd_zmult)
nipkow@30042
   208
  apply (metis dvd_triv_left)
wenzelm@11049
   209
  done
wenzelm@11049
   210
wenzelm@11049
   211
lemma zcong_zless_imp_eq:
paulson@11868
   212
  "0 \<le> a ==>
paulson@11868
   213
    a < m ==> 0 \<le> b ==> b < m ==> [a = b] (mod m) ==> a = b"
paulson@13833
   214
  apply (unfold zcong_def dvd_def, auto)
wenzelm@11049
   215
  apply (drule_tac f = "\<lambda>z. z mod m" in arg_cong)
nipkow@30034
   216
  apply (metis diff_add_cancel mod_pos_pos_trivial zadd_0 zadd_commute zmod_eq_0_iff mod_add_right_eq)
wenzelm@11049
   217
  done
wenzelm@11049
   218
wenzelm@11049
   219
lemma zcong_square_zless:
nipkow@16663
   220
  "zprime p ==> 0 < a ==> a < p ==>
paulson@11868
   221
    [a * a = 1] (mod p) ==> a = 1 \<or> a = p - 1"
wenzelm@11049
   222
  apply (cut_tac p = p and a = a in zcong_square)
wenzelm@11049
   223
     apply (simp add: zprime_def)
wenzelm@11049
   224
    apply (auto intro: zcong_zless_imp_eq)
wenzelm@11049
   225
  done
wenzelm@11049
   226
wenzelm@11049
   227
lemma zcong_not:
paulson@11868
   228
    "0 < a ==> a < m ==> 0 < b ==> b < a ==> \<not> [a = b] (mod m)"
wenzelm@11049
   229
  apply (unfold zcong_def)
paulson@13833
   230
  apply (rule zdvd_not_zless, auto)
wenzelm@11049
   231
  done
wenzelm@11049
   232
wenzelm@11049
   233
lemma zcong_zless_0:
paulson@11868
   234
    "0 \<le> a ==> a < m ==> [a = 0] (mod m) ==> a = 0"
paulson@13833
   235
  apply (unfold zcong_def dvd_def, auto)
nipkow@30042
   236
  apply (metis div_pos_pos_trivial linorder_not_less div_mult_self1_is_id)
wenzelm@11049
   237
  done
wenzelm@11049
   238
wenzelm@11049
   239
lemma zcong_zless_unique:
paulson@11868
   240
    "0 < m ==> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
wenzelm@11049
   241
  apply auto
paulson@23839
   242
   prefer 2 apply (metis zcong_sym zcong_trans zcong_zless_imp_eq)
wenzelm@11049
   243
  apply (unfold zcong_def dvd_def)
paulson@13833
   244
  apply (rule_tac x = "a mod m" in exI, auto)
paulson@23839
   245
  apply (metis zmult_div_cancel)
wenzelm@11049
   246
  done
wenzelm@11049
   247
wenzelm@11049
   248
lemma zcong_iff_lin: "([a = b] (mod m)) = (\<exists>k. b = a + m * k)"
haftmann@27651
   249
  unfolding zcong_def
nipkow@29667
   250
  apply (auto elim!: dvdE simp add: algebra_simps)
haftmann@27651
   251
  apply (rule_tac x = "-k" in exI) apply simp
wenzelm@11049
   252
  done
wenzelm@11049
   253
wenzelm@11049
   254
lemma zgcd_zcong_zgcd:
paulson@11868
   255
  "0 < m ==>
haftmann@27556
   256
    zgcd a m = 1 ==> [a = b] (mod m) ==> zgcd b m = 1"
paulson@13833
   257
  by (auto simp add: zcong_iff_lin)
wenzelm@11049
   258
paulson@13833
   259
lemma zcong_zmod_aux:
paulson@13833
   260
     "a - b = (m::int) * (a div m - b div m) + (a mod m - b mod m)"
paulson@14271
   261
  by(simp add: zdiff_zmult_distrib2 add_diff_eq eq_diff_eq add_ac)
nipkow@13517
   262
wenzelm@11049
   263
lemma zcong_zmod: "[a = b] (mod m) = [a mod m = b mod m] (mod m)"
wenzelm@11049
   264
  apply (unfold zcong_def)
wenzelm@11049
   265
  apply (rule_tac t = "a - b" in ssubst)
ballarin@14174
   266
  apply (rule_tac m = m in zcong_zmod_aux)
wenzelm@11049
   267
  apply (rule trans)
wenzelm@11049
   268
   apply (rule_tac [2] k = m and m = "a div m - b div m" in zdvd_reduce)
wenzelm@11049
   269
  apply (simp add: zadd_commute)
wenzelm@11049
   270
  done
wenzelm@11049
   271
paulson@11868
   272
lemma zcong_zmod_eq: "0 < m ==> [a = b] (mod m) = (a mod m = b mod m)"
wenzelm@11049
   273
  apply auto
paulson@23839
   274
  apply (metis pos_mod_conj zcong_zless_imp_eq zcong_zmod)
paulson@23839
   275
  apply (metis zcong_refl zcong_zmod)
wenzelm@11049
   276
  done
wenzelm@11049
   277
wenzelm@11049
   278
lemma zcong_zminus [iff]: "[a = b] (mod -m) = [a = b] (mod m)"
paulson@13833
   279
  by (auto simp add: zcong_def)
wenzelm@11049
   280
paulson@11868
   281
lemma zcong_zero [iff]: "[a = b] (mod 0) = (a = b)"
paulson@13833
   282
  by (auto simp add: zcong_def)
wenzelm@11049
   283
wenzelm@11049
   284
lemma "[a = b] (mod m) = (a mod m = b mod m)"
paulson@13183
   285
  apply (case_tac "m = 0", simp add: DIVISION_BY_ZERO)
paulson@13193
   286
  apply (simp add: linorder_neq_iff)
paulson@13193
   287
  apply (erule disjE)  
paulson@13193
   288
   prefer 2 apply (simp add: zcong_zmod_eq)
paulson@13193
   289
  txt{*Remainding case: @{term "m<0"}*}
wenzelm@11049
   290
  apply (rule_tac t = m in zminus_zminus [THEN subst])
wenzelm@11049
   291
  apply (subst zcong_zminus)
paulson@13833
   292
  apply (subst zcong_zmod_eq, arith)
paulson@13193
   293
  apply (frule neg_mod_bound [of _ a], frule neg_mod_bound [of _ b]) 
nipkow@13788
   294
  apply (simp add: zmod_zminus2_eq_if del: neg_mod_bound)
paulson@13193
   295
  done
wenzelm@11049
   296
wenzelm@11049
   297
subsection {* Modulo *}
wenzelm@11049
   298
wenzelm@11049
   299
lemma zmod_zdvd_zmod:
paulson@11868
   300
    "0 < (m::int) ==> m dvd b ==> (a mod b mod m) = (a mod m)"
nipkow@30034
   301
  by (rule mod_mod_cancel) 
wenzelm@11049
   302
wenzelm@11049
   303
wenzelm@11049
   304
subsection {* Extended GCD *}
wenzelm@11049
   305
wenzelm@11049
   306
declare xzgcda.simps [simp del]
wenzelm@11049
   307
wenzelm@13524
   308
lemma xzgcd_correct_aux1:
haftmann@27556
   309
  "zgcd r' r = k --> 0 < r -->
wenzelm@11049
   310
    (\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn))"
wenzelm@11049
   311
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   312
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   313
  apply (subst zgcd_eq)
paulson@13833
   314
  apply (subst xzgcda.simps, auto)
wenzelm@24759
   315
  apply (case_tac "r' mod r = 0")
wenzelm@24759
   316
   prefer 2
wenzelm@24759
   317
   apply (frule_tac a = "r'" in pos_mod_sign, auto)
wenzelm@24759
   318
  apply (rule exI)
wenzelm@24759
   319
  apply (rule exI)
wenzelm@24759
   320
  apply (subst xzgcda.simps, auto)
wenzelm@11049
   321
  done
wenzelm@11049
   322
wenzelm@13524
   323
lemma xzgcd_correct_aux2:
paulson@11868
   324
  "(\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn)) --> 0 < r -->
haftmann@27556
   325
    zgcd r' r = k"
wenzelm@11049
   326
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   327
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   328
  apply (subst zgcd_eq)
wenzelm@11049
   329
  apply (subst xzgcda.simps)
wenzelm@11049
   330
  apply (auto simp add: linorder_not_le)
paulson@11868
   331
  apply (case_tac "r' mod r = 0")
wenzelm@11049
   332
   prefer 2
paulson@13833
   333
   apply (frule_tac a = "r'" in pos_mod_sign, auto)
paulson@23839
   334
  apply (metis Pair_eq simps zle_refl)
wenzelm@11049
   335
  done
wenzelm@11049
   336
wenzelm@11049
   337
lemma xzgcd_correct:
chaieb@27569
   338
    "0 < n ==> (zgcd m n = k) = (\<exists>s t. xzgcd m n = (k, s, t))"
wenzelm@11049
   339
  apply (unfold xzgcd_def)
wenzelm@11049
   340
  apply (rule iffI)
wenzelm@13524
   341
   apply (rule_tac [2] xzgcd_correct_aux2 [THEN mp, THEN mp])
paulson@13833
   342
    apply (rule xzgcd_correct_aux1 [THEN mp, THEN mp], auto)
wenzelm@11049
   343
  done
wenzelm@11049
   344
wenzelm@11049
   345
wenzelm@11049
   346
text {* \medskip @{term xzgcd} linear *}
wenzelm@11049
   347
wenzelm@13524
   348
lemma xzgcda_linear_aux1:
wenzelm@11049
   349
  "(a - r * b) * m + (c - r * d) * (n::int) =
paulson@13833
   350
   (a * m + c * n) - r * (b * m + d * n)"
paulson@13833
   351
  by (simp add: zdiff_zmult_distrib zadd_zmult_distrib2 zmult_assoc)
wenzelm@11049
   352
wenzelm@13524
   353
lemma xzgcda_linear_aux2:
wenzelm@11049
   354
  "r' = s' * m + t' * n ==> r = s * m + t * n
wenzelm@11049
   355
    ==> (r' mod r) = (s' - (r' div r) * s) * m + (t' - (r' div r) * t) * (n::int)"
wenzelm@11049
   356
  apply (rule trans)
wenzelm@13524
   357
   apply (rule_tac [2] xzgcda_linear_aux1 [symmetric])
paulson@14271
   358
  apply (simp add: eq_diff_eq mult_commute)
wenzelm@11049
   359
  done
wenzelm@11049
   360
wenzelm@11049
   361
lemma order_le_neq_implies_less: "(x::'a::order) \<le> y ==> x \<noteq> y ==> x < y"
wenzelm@11049
   362
  by (rule iffD2 [OF order_less_le conjI])
wenzelm@11049
   363
wenzelm@11049
   364
lemma xzgcda_linear [rule_format]:
paulson@11868
   365
  "0 < r --> xzgcda (m, n, r', r, s', s, t', t) = (rn, sn, tn) -->
wenzelm@11049
   366
    r' = s' * m + t' * n -->  r = s * m + t * n --> rn = sn * m + tn * n"
wenzelm@11049
   367
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   368
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   369
  apply (subst xzgcda.simps)
wenzelm@11049
   370
  apply (simp (no_asm))
wenzelm@11049
   371
  apply (rule impI)+
paulson@11868
   372
  apply (case_tac "r' mod r = 0")
paulson@13833
   373
   apply (simp add: xzgcda.simps, clarify)
paulson@11868
   374
  apply (subgoal_tac "0 < r' mod r")
wenzelm@11049
   375
   apply (rule_tac [2] order_le_neq_implies_less)
wenzelm@11049
   376
   apply (rule_tac [2] pos_mod_sign)
wenzelm@11049
   377
    apply (cut_tac m = m and n = n and r' = r' and r = r and s' = s' and
paulson@13833
   378
      s = s and t' = t' and t = t in xzgcda_linear_aux2, auto)
wenzelm@11049
   379
  done
wenzelm@11049
   380
wenzelm@11049
   381
lemma xzgcd_linear:
paulson@11868
   382
    "0 < n ==> xzgcd m n = (r, s, t) ==> r = s * m + t * n"
wenzelm@11049
   383
  apply (unfold xzgcd_def)
paulson@13837
   384
  apply (erule xzgcda_linear, assumption, auto)
wenzelm@11049
   385
  done
wenzelm@11049
   386
wenzelm@11049
   387
lemma zgcd_ex_linear:
haftmann@27556
   388
    "0 < n ==> zgcd m n = k ==> (\<exists>s t. k = s * m + t * n)"
paulson@13833
   389
  apply (simp add: xzgcd_correct, safe)
wenzelm@11049
   390
  apply (rule exI)+
paulson@13833
   391
  apply (erule xzgcd_linear, auto)
wenzelm@11049
   392
  done
wenzelm@11049
   393
wenzelm@11049
   394
lemma zcong_lineq_ex:
haftmann@27556
   395
    "0 < n ==> zgcd a n = 1 ==> \<exists>x. [a * x = 1] (mod n)"
paulson@13833
   396
  apply (cut_tac m = a and n = n and k = 1 in zgcd_ex_linear, safe)
wenzelm@11049
   397
  apply (rule_tac x = s in exI)
wenzelm@11049
   398
  apply (rule_tac b = "s * a + t * n" in zcong_trans)
wenzelm@11049
   399
   prefer 2
wenzelm@11049
   400
   apply simp
wenzelm@11049
   401
  apply (unfold zcong_def)
nipkow@30042
   402
  apply (simp (no_asm) add: zmult_commute)
wenzelm@11049
   403
  done
wenzelm@11049
   404
wenzelm@11049
   405
lemma zcong_lineq_unique:
paulson@11868
   406
  "0 < n ==>
haftmann@27556
   407
    zgcd a n = 1 ==> \<exists>!x. 0 \<le> x \<and> x < n \<and> [a * x = b] (mod n)"
wenzelm@11049
   408
  apply auto
wenzelm@11049
   409
   apply (rule_tac [2] zcong_zless_imp_eq)
wenzelm@11049
   410
       apply (tactic {* stac (thm "zcong_cancel2" RS sym) 6 *})
wenzelm@11049
   411
         apply (rule_tac [8] zcong_trans)
wenzelm@11049
   412
          apply (simp_all (no_asm_simp))
wenzelm@11049
   413
   prefer 2
wenzelm@11049
   414
   apply (simp add: zcong_sym)
paulson@13833
   415
  apply (cut_tac a = a and n = n in zcong_lineq_ex, auto)
paulson@13833
   416
  apply (rule_tac x = "x * b mod n" in exI, safe)
nipkow@13788
   417
    apply (simp_all (no_asm_simp))
paulson@23839
   418
  apply (metis zcong_scalar zcong_zmod zmod_zmult1_eq zmult_1 zmult_assoc)
wenzelm@11049
   419
  done
paulson@9508
   420
paulson@9508
   421
end