src/HOL/NumberTheory/IntPrimes.thy
author nipkow
Tue Oct 08 08:20:17 2002 +0200 (2002-10-08)
changeset 13630 a013a9dd370f
parent 13601 fd3e3d6b37b2
child 13788 fd03c4ab89d4
permissions -rw-r--r--
Got rid of rotates because of new simplifier
wenzelm@11049
     1
(*  Title:      HOL/NumberTheory/IntPrimes.thy
paulson@9508
     2
    ID:         $Id$
wenzelm@11049
     3
    Author:     Thomas M. Rasmussen
wenzelm@11049
     4
    Copyright   2000  University of Cambridge
paulson@9508
     5
*)
paulson@9508
     6
wenzelm@11049
     7
header {* Divisibility and prime numbers (on integers) *}
wenzelm@11049
     8
wenzelm@11049
     9
theory IntPrimes = Primes:
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
  xzgcd :: "int => int => int * int * int"
wenzelm@11049
    25
  zprime :: "int set"
wenzelm@11049
    26
  zcong :: "int => int => int => bool"    ("(1[_ = _] '(mod _'))")
wenzelm@11049
    27
wenzelm@11049
    28
recdef xzgcda
wenzelm@11049
    29
  "measure ((\<lambda>(m, n, r', r, s', s, t', t). nat r)
wenzelm@11049
    30
    :: int * int * int * int *int * int * int * int => nat)"
wenzelm@11049
    31
  "xzgcda (m, n, r', r, s', s, t', t) =
paulson@11868
    32
    (if r \<le> 0 then (r', s', t')
wenzelm@11049
    33
     else xzgcda (m, n, r, r' mod r, s, s' - (r' div r) * s, t, t' - (r' div r) * t))"
wenzelm@11049
    34
  (hints simp: pos_mod_bound)
paulson@9508
    35
paulson@9943
    36
constdefs
wenzelm@11049
    37
  zgcd :: "int * int => int"
wenzelm@11049
    38
  "zgcd == \<lambda>(x,y). int (gcd (nat (abs x), nat (abs y)))"
paulson@9943
    39
paulson@9508
    40
defs
paulson@11868
    41
  xzgcd_def: "xzgcd m n == xzgcda (m, n, m, n, 1, 0, 0, 1)"
paulson@11868
    42
  zprime_def: "zprime == {p. 1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p)}"
wenzelm@11049
    43
  zcong_def: "[a = b] (mod m) == m dvd (a - b)"
wenzelm@11049
    44
wenzelm@11049
    45
wenzelm@11049
    46
lemma zabs_eq_iff:
paulson@11868
    47
    "(abs (z::int) = w) = (z = w \<and> 0 <= z \<or> z = -w \<and> z < 0)"
wenzelm@11049
    48
  apply (auto simp add: zabs_def)
wenzelm@11049
    49
  done
wenzelm@11049
    50
wenzelm@11049
    51
wenzelm@11049
    52
text {* \medskip @{term gcd} lemmas *}
wenzelm@11049
    53
wenzelm@11049
    54
lemma gcd_add1_eq: "gcd (m + k, k) = gcd (m + k, m)"
wenzelm@11049
    55
  apply (simp add: gcd_commute)
wenzelm@11049
    56
  done
wenzelm@11049
    57
wenzelm@11049
    58
lemma gcd_diff2: "m \<le> n ==> gcd (n, n - m) = gcd (n, m)"
wenzelm@11049
    59
  apply (subgoal_tac "n = m + (n - m)")
wenzelm@11049
    60
   apply (erule ssubst, rule gcd_add1_eq)
wenzelm@11049
    61
  apply simp
wenzelm@11049
    62
  done
wenzelm@11049
    63
wenzelm@11049
    64
wenzelm@11049
    65
subsection {* Divides relation *}
wenzelm@11049
    66
paulson@11868
    67
lemma zdvd_0_right [iff]: "(m::int) dvd 0"
wenzelm@11049
    68
  apply (unfold dvd_def)
wenzelm@11049
    69
  apply (blast intro: zmult_0_right [symmetric])
wenzelm@11049
    70
  done
wenzelm@11049
    71
paulson@11868
    72
lemma zdvd_0_left [iff]: "(0 dvd (m::int)) = (m = 0)"
wenzelm@11049
    73
  apply (unfold dvd_def)
wenzelm@11049
    74
  apply auto
wenzelm@11049
    75
  done
wenzelm@11049
    76
paulson@11868
    77
lemma zdvd_1_left [iff]: "1 dvd (m::int)"
wenzelm@11049
    78
  apply (unfold dvd_def)
wenzelm@11049
    79
  apply simp
wenzelm@11049
    80
  done
wenzelm@11049
    81
wenzelm@11049
    82
lemma zdvd_refl [simp]: "m dvd (m::int)"
wenzelm@11049
    83
  apply (unfold dvd_def)
wenzelm@11049
    84
  apply (blast intro: zmult_1_right [symmetric])
wenzelm@11049
    85
  done
wenzelm@11049
    86
wenzelm@11049
    87
lemma zdvd_trans: "m dvd n ==> n dvd k ==> m dvd (k::int)"
wenzelm@11049
    88
  apply (unfold dvd_def)
wenzelm@11049
    89
  apply (blast intro: zmult_assoc)
wenzelm@11049
    90
  done
wenzelm@11049
    91
wenzelm@11049
    92
lemma zdvd_zminus_iff: "(m dvd -n) = (m dvd (n::int))"
wenzelm@11049
    93
  apply (unfold dvd_def)
wenzelm@11049
    94
  apply auto
wenzelm@11049
    95
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
    96
  apply auto
wenzelm@11049
    97
  done
wenzelm@11049
    98
wenzelm@11049
    99
lemma zdvd_zminus2_iff: "(-m dvd n) = (m dvd (n::int))"
wenzelm@11049
   100
  apply (unfold dvd_def)
wenzelm@11049
   101
  apply auto
wenzelm@11049
   102
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
   103
  apply auto
wenzelm@11049
   104
  done
wenzelm@11049
   105
wenzelm@11049
   106
lemma zdvd_anti_sym:
paulson@11868
   107
    "0 < m ==> 0 < n ==> m dvd n ==> n dvd m ==> m = (n::int)"
wenzelm@11049
   108
  apply (unfold dvd_def)
wenzelm@11049
   109
  apply auto
wenzelm@11049
   110
  apply (simp add: zmult_assoc zmult_eq_self_iff int_0_less_mult_iff zmult_eq_1_iff)
wenzelm@11049
   111
  done
wenzelm@11049
   112
wenzelm@11049
   113
lemma zdvd_zadd: "k dvd m ==> k dvd n ==> k dvd (m + n :: int)"
wenzelm@11049
   114
  apply (unfold dvd_def)
wenzelm@11049
   115
  apply (blast intro: zadd_zmult_distrib2 [symmetric])
wenzelm@11049
   116
  done
wenzelm@11049
   117
wenzelm@11049
   118
lemma zdvd_zdiff: "k dvd m ==> k dvd n ==> k dvd (m - n :: int)"
wenzelm@11049
   119
  apply (unfold dvd_def)
wenzelm@11049
   120
  apply (blast intro: zdiff_zmult_distrib2 [symmetric])
wenzelm@11049
   121
  done
wenzelm@11049
   122
wenzelm@11049
   123
lemma zdvd_zdiffD: "k dvd m - n ==> k dvd n ==> k dvd (m::int)"
wenzelm@11049
   124
  apply (subgoal_tac "m = n + (m - n)")
wenzelm@11049
   125
   apply (erule ssubst)
wenzelm@11049
   126
   apply (blast intro: zdvd_zadd)
wenzelm@11049
   127
  apply simp
wenzelm@11049
   128
  done
wenzelm@11049
   129
wenzelm@11049
   130
lemma zdvd_zmult: "k dvd (n::int) ==> k dvd m * n"
wenzelm@11049
   131
  apply (unfold dvd_def)
wenzelm@11049
   132
  apply (blast intro: zmult_left_commute)
wenzelm@11049
   133
  done
wenzelm@11049
   134
wenzelm@11049
   135
lemma zdvd_zmult2: "k dvd (m::int) ==> k dvd m * n"
wenzelm@11049
   136
  apply (subst zmult_commute)
wenzelm@11049
   137
  apply (erule zdvd_zmult)
wenzelm@11049
   138
  done
wenzelm@11049
   139
wenzelm@11049
   140
lemma [iff]: "(k::int) dvd m * k"
wenzelm@11049
   141
  apply (rule zdvd_zmult)
wenzelm@11049
   142
  apply (rule zdvd_refl)
wenzelm@11049
   143
  done
wenzelm@11049
   144
wenzelm@11049
   145
lemma [iff]: "(k::int) dvd k * m"
wenzelm@11049
   146
  apply (rule zdvd_zmult2)
wenzelm@11049
   147
  apply (rule zdvd_refl)
wenzelm@11049
   148
  done
wenzelm@11049
   149
wenzelm@11049
   150
lemma zdvd_zmultD2: "j * k dvd n ==> j dvd (n::int)"
wenzelm@11049
   151
  apply (unfold dvd_def)
wenzelm@11049
   152
  apply (simp add: zmult_assoc)
wenzelm@11049
   153
  apply blast
wenzelm@11049
   154
  done
wenzelm@11049
   155
wenzelm@11049
   156
lemma zdvd_zmultD: "j * k dvd n ==> k dvd (n::int)"
wenzelm@11049
   157
  apply (rule zdvd_zmultD2)
wenzelm@11049
   158
  apply (subst zmult_commute)
wenzelm@11049
   159
  apply assumption
wenzelm@11049
   160
  done
wenzelm@11049
   161
wenzelm@11049
   162
lemma zdvd_zmult_mono: "i dvd m ==> j dvd (n::int) ==> i * j dvd m * n"
wenzelm@11049
   163
  apply (unfold dvd_def)
wenzelm@11049
   164
  apply clarify
wenzelm@11049
   165
  apply (rule_tac x = "k * ka" in exI)
wenzelm@11049
   166
  apply (simp add: zmult_ac)
wenzelm@11049
   167
  done
wenzelm@11049
   168
wenzelm@11049
   169
lemma zdvd_reduce: "(k dvd n + k * m) = (k dvd (n::int))"
wenzelm@11049
   170
  apply (rule iffI)
wenzelm@11049
   171
   apply (erule_tac [2] zdvd_zadd)
wenzelm@11049
   172
   apply (subgoal_tac "n = (n + k * m) - k * m")
wenzelm@11049
   173
    apply (erule ssubst)
wenzelm@11049
   174
    apply (erule zdvd_zdiff)
wenzelm@11049
   175
    apply simp_all
wenzelm@11049
   176
  done
wenzelm@11049
   177
wenzelm@11049
   178
lemma zdvd_zmod: "f dvd m ==> f dvd (n::int) ==> f dvd m mod n"
wenzelm@11049
   179
  apply (unfold dvd_def)
wenzelm@11049
   180
  apply (auto simp add: zmod_zmult_zmult1)
wenzelm@11049
   181
  done
wenzelm@11049
   182
wenzelm@11049
   183
lemma zdvd_zmod_imp_zdvd: "k dvd m mod n ==> k dvd n ==> k dvd (m::int)"
wenzelm@11049
   184
  apply (subgoal_tac "k dvd n * (m div n) + m mod n")
wenzelm@11049
   185
   apply (simp add: zmod_zdiv_equality [symmetric])
nipkow@13517
   186
  apply (simp only: zdvd_zadd zdvd_zmult2)
wenzelm@11049
   187
  done
wenzelm@11049
   188
paulson@11868
   189
lemma zdvd_iff_zmod_eq_0: "(k dvd n) = (n mod (k::int) = 0)"
wenzelm@11049
   190
  apply (unfold dvd_def)
wenzelm@11049
   191
  apply auto
wenzelm@11049
   192
  done
wenzelm@11049
   193
paulson@11868
   194
lemma zdvd_not_zless: "0 < m ==> m < n ==> \<not> n dvd (m::int)"
wenzelm@11049
   195
  apply (unfold dvd_def)
wenzelm@11049
   196
  apply auto
paulson@11868
   197
  apply (subgoal_tac "0 < n")
wenzelm@11049
   198
   prefer 2
wenzelm@11049
   199
   apply (blast intro: zless_trans)
wenzelm@11049
   200
  apply (simp add: int_0_less_mult_iff)
paulson@11868
   201
  apply (subgoal_tac "n * k < n * 1")
wenzelm@11049
   202
   apply (drule zmult_zless_cancel1 [THEN iffD1])
wenzelm@11049
   203
   apply auto
wenzelm@11049
   204
  done
wenzelm@11049
   205
wenzelm@11049
   206
lemma int_dvd_iff: "(int m dvd z) = (m dvd nat (abs z))"
wenzelm@11049
   207
  apply (auto simp add: dvd_def nat_abs_mult_distrib)
wenzelm@11049
   208
  apply (auto simp add: nat_eq_iff zabs_eq_iff)
wenzelm@11049
   209
   apply (rule_tac [2] x = "-(int k)" in exI)
wenzelm@11049
   210
  apply (auto simp add: zmult_int [symmetric])
wenzelm@11049
   211
  done
wenzelm@11049
   212
wenzelm@11049
   213
lemma dvd_int_iff: "(z dvd int m) = (nat (abs z) dvd m)"
wenzelm@11049
   214
  apply (auto simp add: dvd_def zabs_def zmult_int [symmetric])
wenzelm@11049
   215
    apply (rule_tac [3] x = "nat k" in exI)
wenzelm@11049
   216
    apply (rule_tac [2] x = "-(int k)" in exI)
wenzelm@11049
   217
    apply (rule_tac x = "nat (-k)" in exI)
wenzelm@11049
   218
    apply (cut_tac [3] k = m in int_less_0_conv)
wenzelm@11049
   219
    apply (cut_tac k = m in int_less_0_conv)
wenzelm@11049
   220
    apply (auto simp add: int_0_le_mult_iff zmult_less_0_iff
wenzelm@11049
   221
      nat_mult_distrib [symmetric] nat_eq_iff2)
wenzelm@11049
   222
  done
wenzelm@11049
   223
paulson@11868
   224
lemma nat_dvd_iff: "(nat z dvd m) = (if 0 \<le> z then (z dvd int m) else m = 0)"
wenzelm@11049
   225
  apply (auto simp add: dvd_def zmult_int [symmetric])
wenzelm@11049
   226
  apply (rule_tac x = "nat k" in exI)
wenzelm@11049
   227
  apply (cut_tac k = m in int_less_0_conv)
wenzelm@11049
   228
  apply (auto simp add: int_0_le_mult_iff zmult_less_0_iff
wenzelm@11049
   229
    nat_mult_distrib [symmetric] nat_eq_iff2)
wenzelm@11049
   230
  done
wenzelm@11049
   231
wenzelm@11049
   232
lemma zminus_dvd_iff [iff]: "(-z dvd w) = (z dvd (w::int))"
wenzelm@11049
   233
  apply (auto simp add: dvd_def)
wenzelm@11049
   234
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
   235
   apply auto
wenzelm@11049
   236
  done
wenzelm@11049
   237
wenzelm@11049
   238
lemma dvd_zminus_iff [iff]: "(z dvd -w) = (z dvd (w::int))"
wenzelm@11049
   239
  apply (auto simp add: dvd_def)
wenzelm@11049
   240
   apply (drule zminus_equation [THEN iffD1])
wenzelm@11049
   241
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
   242
   apply auto
wenzelm@11049
   243
  done
wenzelm@11049
   244
wenzelm@11049
   245
wenzelm@11049
   246
subsection {* Euclid's Algorithm and GCD *}
wenzelm@11049
   247
paulson@11868
   248
lemma zgcd_0 [simp]: "zgcd (m, 0) = abs m"
wenzelm@11049
   249
  apply (simp add: zgcd_def zabs_def)
wenzelm@11049
   250
  done
wenzelm@11049
   251
paulson@11868
   252
lemma zgcd_0_left [simp]: "zgcd (0, m) = abs m"
wenzelm@11049
   253
  apply (simp add: zgcd_def zabs_def)
wenzelm@11049
   254
  done
wenzelm@11049
   255
wenzelm@11049
   256
lemma zgcd_zminus [simp]: "zgcd (-m, n) = zgcd (m, n)"
wenzelm@11049
   257
  apply (simp add: zgcd_def)
wenzelm@11049
   258
  done
wenzelm@11049
   259
wenzelm@11049
   260
lemma zgcd_zminus2 [simp]: "zgcd (m, -n) = zgcd (m, n)"
wenzelm@11049
   261
  apply (simp add: zgcd_def)
wenzelm@11049
   262
  done
wenzelm@11049
   263
paulson@11868
   264
lemma zgcd_non_0: "0 < n ==> zgcd (m, n) = zgcd (n, m mod n)"
wenzelm@11049
   265
  apply (frule_tac b = n and a = m in pos_mod_sign)
wenzelm@11049
   266
  apply (simp add: zgcd_def zabs_def nat_mod_distrib)
wenzelm@11049
   267
  apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
wenzelm@11049
   268
  apply (frule_tac a = m in pos_mod_bound)
nipkow@13187
   269
  apply (simp add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
nipkow@13187
   270
  apply (simp add: gcd_non_0 nat_mod_distrib [symmetric])
wenzelm@11049
   271
  done
wenzelm@11049
   272
wenzelm@11049
   273
lemma zgcd_eq: "zgcd (m, n) = zgcd (n, m mod n)"
paulson@13183
   274
  apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
wenzelm@11049
   275
  apply (auto simp add: linorder_neq_iff zgcd_non_0)
wenzelm@11049
   276
  apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0)
wenzelm@11049
   277
   apply auto
wenzelm@11049
   278
  done
wenzelm@11049
   279
paulson@11868
   280
lemma zgcd_1 [simp]: "zgcd (m, 1) = 1"
wenzelm@11049
   281
  apply (simp add: zgcd_def zabs_def)
wenzelm@11049
   282
  done
wenzelm@11049
   283
paulson@11868
   284
lemma zgcd_0_1_iff [simp]: "(zgcd (0, m) = 1) = (abs m = 1)"
wenzelm@11049
   285
  apply (simp add: zgcd_def zabs_def)
wenzelm@11049
   286
  done
wenzelm@11049
   287
wenzelm@11049
   288
lemma zgcd_zdvd1 [iff]: "zgcd (m, n) dvd m"
wenzelm@11049
   289
  apply (simp add: zgcd_def zabs_def int_dvd_iff)
wenzelm@11049
   290
  done
wenzelm@11049
   291
wenzelm@11049
   292
lemma zgcd_zdvd2 [iff]: "zgcd (m, n) dvd n"
wenzelm@11049
   293
  apply (simp add: zgcd_def zabs_def int_dvd_iff)
wenzelm@11049
   294
  done
wenzelm@11049
   295
wenzelm@11049
   296
lemma zgcd_greatest_iff: "k dvd zgcd (m, n) = (k dvd m \<and> k dvd n)"
wenzelm@11049
   297
  apply (simp add: zgcd_def zabs_def int_dvd_iff dvd_int_iff nat_dvd_iff)
wenzelm@11049
   298
  done
wenzelm@11049
   299
wenzelm@11049
   300
lemma zgcd_commute: "zgcd (m, n) = zgcd (n, m)"
wenzelm@11049
   301
  apply (simp add: zgcd_def gcd_commute)
wenzelm@11049
   302
  done
wenzelm@11049
   303
paulson@11868
   304
lemma zgcd_1_left [simp]: "zgcd (1, m) = 1"
wenzelm@11049
   305
  apply (simp add: zgcd_def gcd_1_left)
wenzelm@11049
   306
  done
wenzelm@11049
   307
wenzelm@11049
   308
lemma zgcd_assoc: "zgcd (zgcd (k, m), n) = zgcd (k, zgcd (m, n))"
wenzelm@11049
   309
  apply (simp add: zgcd_def gcd_assoc)
wenzelm@11049
   310
  done
wenzelm@11049
   311
wenzelm@11049
   312
lemma zgcd_left_commute: "zgcd (k, zgcd (m, n)) = zgcd (m, zgcd (k, n))"
wenzelm@11049
   313
  apply (rule zgcd_commute [THEN trans])
wenzelm@11049
   314
  apply (rule zgcd_assoc [THEN trans])
wenzelm@11049
   315
  apply (rule zgcd_commute [THEN arg_cong])
wenzelm@11049
   316
  done
wenzelm@11049
   317
wenzelm@11049
   318
lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
wenzelm@11049
   319
  -- {* addition is an AC-operator *}
wenzelm@11049
   320
paulson@11868
   321
lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd (m, n) = zgcd (k * m, k * n)"
wenzelm@11049
   322
  apply (simp del: zmult_zminus_right
wenzelm@11049
   323
    add: zmult_zminus_right [symmetric] nat_mult_distrib zgcd_def zabs_def
wenzelm@11049
   324
    zmult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
wenzelm@11049
   325
  done
wenzelm@11049
   326
wenzelm@11049
   327
lemma zgcd_zmult_distrib2_abs: "zgcd (k * m, k * n) = abs k * zgcd (m, n)"
wenzelm@11049
   328
  apply (simp add: zabs_def zgcd_zmult_distrib2)
wenzelm@11049
   329
  done
wenzelm@11049
   330
paulson@11868
   331
lemma zgcd_self [simp]: "0 \<le> m ==> zgcd (m, m) = m"
paulson@11868
   332
  apply (cut_tac k = m and m = "1" and n = "1" in zgcd_zmult_distrib2)
wenzelm@11049
   333
   apply simp_all
wenzelm@11049
   334
  done
wenzelm@11049
   335
paulson@11868
   336
lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd (k, k * n) = k"
paulson@11868
   337
  apply (cut_tac k = k and m = "1" and n = n in zgcd_zmult_distrib2)
wenzelm@11049
   338
   apply simp_all
wenzelm@11049
   339
  done
wenzelm@11049
   340
paulson@11868
   341
lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n, k) = k"
paulson@11868
   342
  apply (cut_tac k = k and m = n and n = "1" in zgcd_zmult_distrib2)
wenzelm@11049
   343
   apply simp_all
wenzelm@11049
   344
  done
wenzelm@11049
   345
wenzelm@13524
   346
lemma zrelprime_zdvd_zmult_aux: "zgcd (n, k) = 1 ==> k dvd m * n ==> 0 \<le> m ==> k dvd m"
wenzelm@11049
   347
  apply (subgoal_tac "m = zgcd (m * n, m * k)")
wenzelm@11049
   348
   apply (erule ssubst, rule zgcd_greatest_iff [THEN iffD2])
wenzelm@11049
   349
   apply (simp_all add: zgcd_zmult_distrib2 [symmetric] int_0_le_mult_iff)
wenzelm@11049
   350
  done
wenzelm@11049
   351
paulson@11868
   352
lemma zrelprime_zdvd_zmult: "zgcd (n, k) = 1 ==> k dvd m * n ==> k dvd m"
paulson@11868
   353
  apply (case_tac "0 \<le> m")
wenzelm@13524
   354
   apply (blast intro: zrelprime_zdvd_zmult_aux)
wenzelm@11049
   355
  apply (subgoal_tac "k dvd -m")
wenzelm@13524
   356
   apply (rule_tac [2] zrelprime_zdvd_zmult_aux)
wenzelm@11049
   357
     apply auto
wenzelm@11049
   358
  done
wenzelm@11049
   359
wenzelm@11049
   360
lemma zprime_imp_zrelprime:
paulson@11868
   361
    "p \<in> zprime ==> \<not> p dvd n ==> zgcd (n, p) = 1"
wenzelm@11049
   362
  apply (unfold zprime_def)
wenzelm@11049
   363
  apply auto
wenzelm@11049
   364
  done
wenzelm@11049
   365
wenzelm@11049
   366
lemma zless_zprime_imp_zrelprime:
paulson@11868
   367
    "p \<in> zprime ==> 0 < n ==> n < p ==> zgcd (n, p) = 1"
wenzelm@11049
   368
  apply (erule zprime_imp_zrelprime)
wenzelm@11049
   369
  apply (erule zdvd_not_zless)
wenzelm@11049
   370
  apply assumption
wenzelm@11049
   371
  done
wenzelm@11049
   372
wenzelm@11049
   373
lemma zprime_zdvd_zmult:
paulson@11868
   374
    "0 \<le> (m::int) ==> p \<in> zprime ==> p dvd m * n ==> p dvd m \<or> p dvd n"
wenzelm@11049
   375
  apply safe
wenzelm@11049
   376
  apply (rule zrelprime_zdvd_zmult)
wenzelm@11049
   377
   apply (rule zprime_imp_zrelprime)
wenzelm@11049
   378
    apply auto
wenzelm@11049
   379
  done
wenzelm@11049
   380
wenzelm@11049
   381
lemma zgcd_zadd_zmult [simp]: "zgcd (m + n * k, n) = zgcd (m, n)"
wenzelm@11049
   382
  apply (rule zgcd_eq [THEN trans])
wenzelm@11049
   383
  apply (simp add: zmod_zadd1_eq)
wenzelm@11049
   384
  apply (rule zgcd_eq [symmetric])
wenzelm@11049
   385
  done
wenzelm@11049
   386
wenzelm@11049
   387
lemma zgcd_zdvd_zgcd_zmult: "zgcd (m, n) dvd zgcd (k * m, n)"
wenzelm@11049
   388
  apply (simp add: zgcd_greatest_iff)
wenzelm@11049
   389
  apply (blast intro: zdvd_trans)
wenzelm@11049
   390
  done
wenzelm@11049
   391
wenzelm@11049
   392
lemma zgcd_zmult_zdvd_zgcd:
paulson@11868
   393
    "zgcd (k, n) = 1 ==> zgcd (k * m, n) dvd zgcd (m, n)"
wenzelm@11049
   394
  apply (simp add: zgcd_greatest_iff)
wenzelm@11049
   395
  apply (rule_tac n = k in zrelprime_zdvd_zmult)
wenzelm@11049
   396
   prefer 2
wenzelm@11049
   397
   apply (simp add: zmult_commute)
wenzelm@11049
   398
  apply (subgoal_tac "zgcd (k, zgcd (k * m, n)) = zgcd (k * m, zgcd (k, n))")
wenzelm@11049
   399
   apply simp
wenzelm@11049
   400
  apply (simp (no_asm) add: zgcd_ac)
wenzelm@11049
   401
  done
wenzelm@11049
   402
paulson@11868
   403
lemma zgcd_zmult_cancel: "zgcd (k, n) = 1 ==> zgcd (k * m, n) = zgcd (m, n)"
wenzelm@11049
   404
  apply (simp add: zgcd_def nat_abs_mult_distrib gcd_mult_cancel)
wenzelm@11049
   405
  done
wenzelm@11049
   406
wenzelm@11049
   407
lemma zgcd_zgcd_zmult:
paulson@11868
   408
    "zgcd (k, m) = 1 ==> zgcd (n, m) = 1 ==> zgcd (k * n, m) = 1"
wenzelm@11049
   409
  apply (simp (no_asm_simp) add: zgcd_zmult_cancel)
wenzelm@11049
   410
  done
wenzelm@11049
   411
paulson@11868
   412
lemma zdvd_iff_zgcd: "0 < m ==> (m dvd n) = (zgcd (n, m) = m)"
wenzelm@11049
   413
  apply safe
wenzelm@11049
   414
   apply (rule_tac [2] n = "zgcd (n, m)" in zdvd_trans)
wenzelm@11049
   415
    apply (rule_tac [3] zgcd_zdvd1)
wenzelm@11049
   416
   apply simp_all
wenzelm@11049
   417
  apply (unfold dvd_def)
wenzelm@11049
   418
  apply auto
wenzelm@11049
   419
  done
wenzelm@11049
   420
wenzelm@11049
   421
wenzelm@11049
   422
subsection {* Congruences *}
wenzelm@11049
   423
paulson@11868
   424
lemma zcong_1 [simp]: "[a = b] (mod 1)"
wenzelm@11049
   425
  apply (unfold zcong_def)
wenzelm@11049
   426
  apply auto
wenzelm@11049
   427
  done
wenzelm@11049
   428
wenzelm@11049
   429
lemma zcong_refl [simp]: "[k = k] (mod m)"
wenzelm@11049
   430
  apply (unfold zcong_def)
wenzelm@11049
   431
  apply auto
wenzelm@11049
   432
  done
paulson@9508
   433
wenzelm@11049
   434
lemma zcong_sym: "[a = b] (mod m) = [b = a] (mod m)"
wenzelm@11049
   435
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   436
  apply auto
wenzelm@11049
   437
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
   438
   apply auto
wenzelm@11049
   439
  done
wenzelm@11049
   440
wenzelm@11049
   441
lemma zcong_zadd:
wenzelm@11049
   442
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a + c = b + d] (mod m)"
wenzelm@11049
   443
  apply (unfold zcong_def)
wenzelm@11049
   444
  apply (rule_tac s = "(a - b) + (c - d)" in subst)
wenzelm@11049
   445
   apply (rule_tac [2] zdvd_zadd)
wenzelm@11049
   446
    apply auto
wenzelm@11049
   447
  done
wenzelm@11049
   448
wenzelm@11049
   449
lemma zcong_zdiff:
wenzelm@11049
   450
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a - c = b - d] (mod m)"
wenzelm@11049
   451
  apply (unfold zcong_def)
wenzelm@11049
   452
  apply (rule_tac s = "(a - b) - (c - d)" in subst)
wenzelm@11049
   453
   apply (rule_tac [2] zdvd_zdiff)
wenzelm@11049
   454
    apply auto
wenzelm@11049
   455
  done
wenzelm@11049
   456
wenzelm@11049
   457
lemma zcong_trans:
wenzelm@11049
   458
    "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
wenzelm@11049
   459
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   460
  apply auto
wenzelm@11049
   461
  apply (rule_tac x = "k + ka" in exI)
wenzelm@11049
   462
  apply (simp add: zadd_ac zadd_zmult_distrib2)
wenzelm@11049
   463
  done
wenzelm@11049
   464
wenzelm@11049
   465
lemma zcong_zmult:
wenzelm@11049
   466
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
wenzelm@11049
   467
  apply (rule_tac b = "b * c" in zcong_trans)
wenzelm@11049
   468
   apply (unfold zcong_def)
wenzelm@11049
   469
   apply (rule_tac s = "c * (a - b)" in subst)
wenzelm@11049
   470
    apply (rule_tac [3] s = "b * (c - d)" in subst)
wenzelm@11049
   471
     prefer 4
wenzelm@11049
   472
     apply (blast intro: zdvd_zmult)
wenzelm@11049
   473
    prefer 2
wenzelm@11049
   474
    apply (blast intro: zdvd_zmult)
wenzelm@11049
   475
   apply (simp_all add: zdiff_zmult_distrib2 zmult_commute)
wenzelm@11049
   476
  done
wenzelm@11049
   477
wenzelm@11049
   478
lemma zcong_scalar: "[a = b] (mod m) ==> [a * k = b * k] (mod m)"
wenzelm@11049
   479
  apply (rule zcong_zmult)
wenzelm@11049
   480
  apply simp_all
wenzelm@11049
   481
  done
wenzelm@11049
   482
wenzelm@11049
   483
lemma zcong_scalar2: "[a = b] (mod m) ==> [k * a = k * b] (mod m)"
wenzelm@11049
   484
  apply (rule zcong_zmult)
wenzelm@11049
   485
  apply simp_all
wenzelm@11049
   486
  done
wenzelm@11049
   487
wenzelm@11049
   488
lemma zcong_zmult_self: "[a * m = b * m] (mod m)"
wenzelm@11049
   489
  apply (unfold zcong_def)
wenzelm@11049
   490
  apply (rule zdvd_zdiff)
wenzelm@11049
   491
   apply simp_all
wenzelm@11049
   492
  done
wenzelm@11049
   493
wenzelm@11049
   494
lemma zcong_square:
paulson@11868
   495
  "p \<in> zprime ==> 0 < a ==> [a * a = 1] (mod p)
paulson@11868
   496
    ==> [a = 1] (mod p) \<or> [a = p - 1] (mod p)"
wenzelm@11049
   497
  apply (unfold zcong_def)
wenzelm@11049
   498
  apply (rule zprime_zdvd_zmult)
paulson@11868
   499
    apply (rule_tac [3] s = "a * a - 1 + p * (1 - a)" in subst)
wenzelm@11049
   500
     prefer 4
wenzelm@11049
   501
     apply (simp add: zdvd_reduce)
wenzelm@11049
   502
    apply (simp_all add: zdiff_zmult_distrib zmult_commute zdiff_zmult_distrib2)
wenzelm@11049
   503
  done
wenzelm@11049
   504
wenzelm@11049
   505
lemma zcong_cancel:
paulson@11868
   506
  "0 \<le> m ==>
paulson@11868
   507
    zgcd (k, m) = 1 ==> [a * k = b * k] (mod m) = [a = b] (mod m)"
wenzelm@11049
   508
  apply safe
wenzelm@11049
   509
   prefer 2
wenzelm@11049
   510
   apply (blast intro: zcong_scalar)
wenzelm@11049
   511
  apply (case_tac "b < a")
wenzelm@11049
   512
   prefer 2
wenzelm@11049
   513
   apply (subst zcong_sym)
wenzelm@11049
   514
   apply (unfold zcong_def)
wenzelm@11049
   515
   apply (rule_tac [!] zrelprime_zdvd_zmult)
wenzelm@11049
   516
     apply (simp_all add: zdiff_zmult_distrib)
wenzelm@11049
   517
  apply (subgoal_tac "m dvd (-(a * k - b * k))")
wenzelm@11049
   518
   apply (simp add: zminus_zdiff_eq)
wenzelm@11049
   519
  apply (subst zdvd_zminus_iff)
wenzelm@11049
   520
  apply assumption
wenzelm@11049
   521
  done
wenzelm@11049
   522
wenzelm@11049
   523
lemma zcong_cancel2:
paulson@11868
   524
  "0 \<le> m ==>
paulson@11868
   525
    zgcd (k, m) = 1 ==> [k * a = k * b] (mod m) = [a = b] (mod m)"
wenzelm@11049
   526
  apply (simp add: zmult_commute zcong_cancel)
wenzelm@11049
   527
  done
wenzelm@11049
   528
wenzelm@11049
   529
lemma zcong_zgcd_zmult_zmod:
paulson@11868
   530
  "[a = b] (mod m) ==> [a = b] (mod n) ==> zgcd (m, n) = 1
wenzelm@11049
   531
    ==> [a = b] (mod m * n)"
wenzelm@11049
   532
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   533
  apply auto
wenzelm@11049
   534
  apply (subgoal_tac "m dvd n * ka")
wenzelm@11049
   535
   apply (subgoal_tac "m dvd ka")
paulson@11868
   536
    apply (case_tac [2] "0 \<le> ka")
wenzelm@11049
   537
     prefer 3
wenzelm@11049
   538
     apply (subst zdvd_zminus_iff [symmetric])
wenzelm@11049
   539
     apply (rule_tac n = n in zrelprime_zdvd_zmult)
wenzelm@11049
   540
      apply (simp add: zgcd_commute)
wenzelm@11049
   541
     apply (simp add: zmult_commute zdvd_zminus_iff)
wenzelm@11049
   542
    prefer 2
wenzelm@11049
   543
    apply (rule_tac n = n in zrelprime_zdvd_zmult)
wenzelm@11049
   544
     apply (simp add: zgcd_commute)
wenzelm@11049
   545
    apply (simp add: zmult_commute)
wenzelm@11049
   546
   apply (auto simp add: dvd_def)
wenzelm@11049
   547
  done
wenzelm@11049
   548
wenzelm@11049
   549
lemma zcong_zless_imp_eq:
paulson@11868
   550
  "0 \<le> a ==>
paulson@11868
   551
    a < m ==> 0 \<le> b ==> b < m ==> [a = b] (mod m) ==> a = b"
wenzelm@11049
   552
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   553
  apply auto
wenzelm@11049
   554
  apply (drule_tac f = "\<lambda>z. z mod m" in arg_cong)
wenzelm@11049
   555
  apply (cut_tac z = a and w = b in zless_linear)
wenzelm@11049
   556
  apply auto
wenzelm@11049
   557
   apply (subgoal_tac [2] "(a - b) mod m = a - b")
wenzelm@11049
   558
    apply (rule_tac [3] mod_pos_pos_trivial)
wenzelm@11049
   559
     apply auto
wenzelm@11049
   560
  apply (subgoal_tac "(m + (a - b)) mod m = m + (a - b)")
wenzelm@11049
   561
   apply (rule_tac [2] mod_pos_pos_trivial)
wenzelm@11049
   562
    apply auto
wenzelm@11049
   563
  done
wenzelm@11049
   564
wenzelm@11049
   565
lemma zcong_square_zless:
paulson@11868
   566
  "p \<in> zprime ==> 0 < a ==> a < p ==>
paulson@11868
   567
    [a * a = 1] (mod p) ==> a = 1 \<or> a = p - 1"
wenzelm@11049
   568
  apply (cut_tac p = p and a = a in zcong_square)
wenzelm@11049
   569
     apply (simp add: zprime_def)
wenzelm@11049
   570
    apply (auto intro: zcong_zless_imp_eq)
wenzelm@11049
   571
  done
wenzelm@11049
   572
wenzelm@11049
   573
lemma zcong_not:
paulson@11868
   574
    "0 < a ==> a < m ==> 0 < b ==> b < a ==> \<not> [a = b] (mod m)"
wenzelm@11049
   575
  apply (unfold zcong_def)
wenzelm@11049
   576
  apply (rule zdvd_not_zless)
wenzelm@11049
   577
   apply auto
wenzelm@11049
   578
  done
wenzelm@11049
   579
wenzelm@11049
   580
lemma zcong_zless_0:
paulson@11868
   581
    "0 \<le> a ==> a < m ==> [a = 0] (mod m) ==> a = 0"
wenzelm@11049
   582
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   583
  apply auto
paulson@11868
   584
  apply (subgoal_tac "0 < m")
wenzelm@11049
   585
   apply (simp add: int_0_le_mult_iff)
paulson@11868
   586
   apply (subgoal_tac "m * k < m * 1")
wenzelm@11049
   587
    apply (drule zmult_zless_cancel1 [THEN iffD1])
wenzelm@11049
   588
    apply (auto simp add: linorder_neq_iff)
wenzelm@11049
   589
  done
wenzelm@11049
   590
wenzelm@11049
   591
lemma zcong_zless_unique:
paulson@11868
   592
    "0 < m ==> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
wenzelm@11049
   593
  apply auto
wenzelm@11049
   594
   apply (subgoal_tac [2] "[b = y] (mod m)")
paulson@11868
   595
    apply (case_tac [2] "b = 0")
paulson@11868
   596
     apply (case_tac [3] "y = 0")
wenzelm@11049
   597
      apply (auto intro: zcong_trans zcong_zless_0 zcong_zless_imp_eq order_less_le
wenzelm@11049
   598
        simp add: zcong_sym)
wenzelm@11049
   599
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   600
  apply (rule_tac x = "a mod m" in exI)
wenzelm@11049
   601
  apply (auto simp add: pos_mod_sign pos_mod_bound)
wenzelm@11049
   602
  apply (rule_tac x = "-(a div m)" in exI)
nipkow@13517
   603
  apply (simp add:zdiff_eq_eq eq_zdiff_eq zadd_commute)
wenzelm@11049
   604
  done
wenzelm@11049
   605
wenzelm@11049
   606
lemma zcong_iff_lin: "([a = b] (mod m)) = (\<exists>k. b = a + m * k)"
wenzelm@11049
   607
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   608
  apply auto
wenzelm@11049
   609
   apply (rule_tac [!] x = "-k" in exI)
wenzelm@11049
   610
   apply auto
wenzelm@11049
   611
  done
wenzelm@11049
   612
wenzelm@11049
   613
lemma zgcd_zcong_zgcd:
paulson@11868
   614
  "0 < m ==>
paulson@11868
   615
    zgcd (a, m) = 1 ==> [a = b] (mod m) ==> zgcd (b, m) = 1"
wenzelm@11049
   616
  apply (auto simp add: zcong_iff_lin)
wenzelm@11049
   617
  done
wenzelm@11049
   618
wenzelm@13524
   619
lemma zcong_zmod_aux: "a - b = (m::int) * (a div m - b div m) + (a mod m - b mod m)"
nipkow@13517
   620
by(simp add: zdiff_zmult_distrib2 zadd_zdiff_eq eq_zdiff_eq zadd_ac)
nipkow@13517
   621
wenzelm@11049
   622
lemma zcong_zmod: "[a = b] (mod m) = [a mod m = b mod m] (mod m)"
wenzelm@11049
   623
  apply (unfold zcong_def)
wenzelm@11049
   624
  apply (rule_tac t = "a - b" in ssubst)
wenzelm@13524
   625
  apply (rule_tac "m" = "m" in zcong_zmod_aux)
wenzelm@11049
   626
  apply (rule trans)
wenzelm@11049
   627
   apply (rule_tac [2] k = m and m = "a div m - b div m" in zdvd_reduce)
wenzelm@11049
   628
  apply (simp add: zadd_commute)
wenzelm@11049
   629
  done
wenzelm@11049
   630
paulson@11868
   631
lemma zcong_zmod_eq: "0 < m ==> [a = b] (mod m) = (a mod m = b mod m)"
wenzelm@11049
   632
  apply auto
wenzelm@11049
   633
   apply (rule_tac m = m in zcong_zless_imp_eq)
wenzelm@11049
   634
       prefer 5
wenzelm@11049
   635
       apply (subst zcong_zmod [symmetric])
wenzelm@11049
   636
       apply (simp_all add: pos_mod_bound pos_mod_sign)
wenzelm@11049
   637
  apply (unfold zcong_def dvd_def)
wenzelm@11049
   638
  apply (rule_tac x = "a div m - b div m" in exI)
wenzelm@13524
   639
  apply (rule_tac m1 = m in zcong_zmod_aux [THEN trans])
wenzelm@11049
   640
  apply auto
wenzelm@11049
   641
  done
wenzelm@11049
   642
wenzelm@11049
   643
lemma zcong_zminus [iff]: "[a = b] (mod -m) = [a = b] (mod m)"
wenzelm@11049
   644
  apply (auto simp add: zcong_def)
wenzelm@11049
   645
  done
wenzelm@11049
   646
paulson@11868
   647
lemma zcong_zero [iff]: "[a = b] (mod 0) = (a = b)"
wenzelm@11049
   648
  apply (auto simp add: zcong_def)
wenzelm@11049
   649
  done
wenzelm@11049
   650
wenzelm@11049
   651
lemma "[a = b] (mod m) = (a mod m = b mod m)"
paulson@13183
   652
  apply (case_tac "m = 0", simp add: DIVISION_BY_ZERO)
paulson@13193
   653
  apply (simp add: linorder_neq_iff)
paulson@13193
   654
  apply (erule disjE)  
paulson@13193
   655
   prefer 2 apply (simp add: zcong_zmod_eq)
paulson@13193
   656
  txt{*Remainding case: @{term "m<0"}*}
wenzelm@11049
   657
  apply (rule_tac t = m in zminus_zminus [THEN subst])
wenzelm@11049
   658
  apply (subst zcong_zminus)
wenzelm@11049
   659
  apply (subst zcong_zmod_eq)
wenzelm@11049
   660
   apply arith
paulson@13193
   661
  apply (frule neg_mod_bound [of _ a], frule neg_mod_bound [of _ b]) 
paulson@13193
   662
  apply (simp add: zmod_zminus2_eq_if)
paulson@13193
   663
  done
wenzelm@11049
   664
wenzelm@11049
   665
subsection {* Modulo *}
wenzelm@11049
   666
wenzelm@11049
   667
lemma zmod_zdvd_zmod:
paulson@11868
   668
    "0 < (m::int) ==> m dvd b ==> (a mod b mod m) = (a mod m)"
wenzelm@11049
   669
  apply (unfold dvd_def)
wenzelm@11049
   670
  apply auto
wenzelm@11049
   671
  apply (subst zcong_zmod_eq [symmetric])
wenzelm@11049
   672
   prefer 2
wenzelm@11049
   673
   apply (subst zcong_iff_lin)
wenzelm@11049
   674
   apply (rule_tac x = "k * (a div (m * k))" in exI)
nipkow@13517
   675
   apply(simp add:zmult_assoc [symmetric])
wenzelm@11049
   676
  apply assumption
wenzelm@11049
   677
  done
wenzelm@11049
   678
wenzelm@11049
   679
wenzelm@11049
   680
subsection {* Extended GCD *}
wenzelm@11049
   681
wenzelm@11049
   682
declare xzgcda.simps [simp del]
wenzelm@11049
   683
wenzelm@13524
   684
lemma xzgcd_correct_aux1:
paulson@11868
   685
  "zgcd (r', r) = k --> 0 < r -->
wenzelm@11049
   686
    (\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn))"
wenzelm@11049
   687
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   688
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   689
  apply (subst zgcd_eq)
wenzelm@11049
   690
  apply (subst xzgcda.simps)
wenzelm@11049
   691
  apply auto
paulson@11868
   692
  apply (case_tac "r' mod r = 0")
wenzelm@11049
   693
   prefer 2
wenzelm@11049
   694
   apply (frule_tac a = "r'" in pos_mod_sign)
wenzelm@11049
   695
   apply auto
wenzelm@11049
   696
  apply (rule exI)
wenzelm@11049
   697
  apply (rule exI)
wenzelm@11049
   698
  apply (subst xzgcda.simps)
wenzelm@11049
   699
  apply auto
wenzelm@11049
   700
  apply (simp add: zabs_def)
wenzelm@11049
   701
  done
wenzelm@11049
   702
wenzelm@13524
   703
lemma xzgcd_correct_aux2:
paulson@11868
   704
  "(\<exists>sn tn. xzgcda (m, n, r', r, s', s, t', t) = (k, sn, tn)) --> 0 < r -->
wenzelm@11049
   705
    zgcd (r', r) = k"
wenzelm@11049
   706
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   707
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   708
  apply (subst zgcd_eq)
wenzelm@11049
   709
  apply (subst xzgcda.simps)
wenzelm@11049
   710
  apply (auto simp add: linorder_not_le)
paulson@11868
   711
  apply (case_tac "r' mod r = 0")
wenzelm@11049
   712
   prefer 2
wenzelm@11049
   713
   apply (frule_tac a = "r'" in pos_mod_sign)
wenzelm@11049
   714
   apply auto
wenzelm@11049
   715
  apply (erule_tac P = "xzgcda ?u = ?v" in rev_mp)
wenzelm@11049
   716
  apply (subst xzgcda.simps)
wenzelm@11049
   717
  apply auto
wenzelm@11049
   718
  apply (simp add: zabs_def)
wenzelm@11049
   719
  done
wenzelm@11049
   720
wenzelm@11049
   721
lemma xzgcd_correct:
paulson@11868
   722
    "0 < n ==> (zgcd (m, n) = k) = (\<exists>s t. xzgcd m n = (k, s, t))"
wenzelm@11049
   723
  apply (unfold xzgcd_def)
wenzelm@11049
   724
  apply (rule iffI)
wenzelm@13524
   725
   apply (rule_tac [2] xzgcd_correct_aux2 [THEN mp, THEN mp])
wenzelm@13524
   726
    apply (rule xzgcd_correct_aux1 [THEN mp, THEN mp])
wenzelm@11049
   727
     apply auto
wenzelm@11049
   728
  done
wenzelm@11049
   729
wenzelm@11049
   730
wenzelm@11049
   731
text {* \medskip @{term xzgcd} linear *}
wenzelm@11049
   732
wenzelm@13524
   733
lemma xzgcda_linear_aux1:
wenzelm@11049
   734
  "(a - r * b) * m + (c - r * d) * (n::int) =
wenzelm@11049
   735
    (a * m + c * n) - r * (b * m + d * n)"
wenzelm@11049
   736
  apply (simp add: zdiff_zmult_distrib zadd_zmult_distrib2 zmult_assoc)
wenzelm@11049
   737
  done
wenzelm@11049
   738
wenzelm@13524
   739
lemma xzgcda_linear_aux2:
wenzelm@11049
   740
  "r' = s' * m + t' * n ==> r = s * m + t * n
wenzelm@11049
   741
    ==> (r' mod r) = (s' - (r' div r) * s) * m + (t' - (r' div r) * t) * (n::int)"
wenzelm@11049
   742
  apply (rule trans)
wenzelm@13524
   743
   apply (rule_tac [2] xzgcda_linear_aux1 [symmetric])
nipkow@13517
   744
  apply (simp add: eq_zdiff_eq zmult_commute)
wenzelm@11049
   745
  done
wenzelm@11049
   746
wenzelm@11049
   747
lemma order_le_neq_implies_less: "(x::'a::order) \<le> y ==> x \<noteq> y ==> x < y"
wenzelm@11049
   748
  by (rule iffD2 [OF order_less_le conjI])
wenzelm@11049
   749
wenzelm@11049
   750
lemma xzgcda_linear [rule_format]:
paulson@11868
   751
  "0 < r --> xzgcda (m, n, r', r, s', s, t', t) = (rn, sn, tn) -->
wenzelm@11049
   752
    r' = s' * m + t' * n -->  r = s * m + t * n --> rn = sn * m + tn * n"
wenzelm@11049
   753
  apply (rule_tac u = m and v = n and w = r' and x = r and y = s' and
wenzelm@11049
   754
    z = s and aa = t' and ab = t in xzgcda.induct)
wenzelm@11049
   755
  apply (subst xzgcda.simps)
wenzelm@11049
   756
  apply (simp (no_asm))
wenzelm@11049
   757
  apply (rule impI)+
paulson@11868
   758
  apply (case_tac "r' mod r = 0")
wenzelm@11049
   759
   apply (simp add: xzgcda.simps)
wenzelm@11049
   760
   apply clarify
paulson@11868
   761
  apply (subgoal_tac "0 < r' mod r")
wenzelm@11049
   762
   apply (rule_tac [2] order_le_neq_implies_less)
wenzelm@11049
   763
   apply (rule_tac [2] pos_mod_sign)
wenzelm@11049
   764
    apply (cut_tac m = m and n = n and r' = r' and r = r and s' = s' and
wenzelm@13524
   765
      s = s and t' = t' and t = t in xzgcda_linear_aux2)
wenzelm@11049
   766
      apply auto
wenzelm@11049
   767
  done
wenzelm@11049
   768
wenzelm@11049
   769
lemma xzgcd_linear:
paulson@11868
   770
    "0 < n ==> xzgcd m n = (r, s, t) ==> r = s * m + t * n"
wenzelm@11049
   771
  apply (unfold xzgcd_def)
wenzelm@11049
   772
  apply (erule xzgcda_linear)
wenzelm@11049
   773
    apply assumption
wenzelm@11049
   774
   apply auto
wenzelm@11049
   775
  done
wenzelm@11049
   776
wenzelm@11049
   777
lemma zgcd_ex_linear:
paulson@11868
   778
    "0 < n ==> zgcd (m, n) = k ==> (\<exists>s t. k = s * m + t * n)"
wenzelm@11049
   779
  apply (simp add: xzgcd_correct)
wenzelm@11049
   780
  apply safe
wenzelm@11049
   781
  apply (rule exI)+
wenzelm@11049
   782
  apply (erule xzgcd_linear)
wenzelm@11049
   783
  apply auto
wenzelm@11049
   784
  done
wenzelm@11049
   785
wenzelm@11049
   786
lemma zcong_lineq_ex:
paulson@11868
   787
    "0 < n ==> zgcd (a, n) = 1 ==> \<exists>x. [a * x = 1] (mod n)"
paulson@11868
   788
  apply (cut_tac m = a and n = n and k = "1" in zgcd_ex_linear)
wenzelm@11049
   789
    apply safe
wenzelm@11049
   790
  apply (rule_tac x = s in exI)
wenzelm@11049
   791
  apply (rule_tac b = "s * a + t * n" in zcong_trans)
wenzelm@11049
   792
   prefer 2
wenzelm@11049
   793
   apply simp
wenzelm@11049
   794
  apply (unfold zcong_def)
wenzelm@11049
   795
  apply (simp (no_asm) add: zmult_commute zdvd_zminus_iff)
wenzelm@11049
   796
  done
wenzelm@11049
   797
wenzelm@11049
   798
lemma zcong_lineq_unique:
paulson@11868
   799
  "0 < n ==>
paulson@11868
   800
    zgcd (a, n) = 1 ==> \<exists>!x. 0 \<le> x \<and> x < n \<and> [a * x = b] (mod n)"
wenzelm@11049
   801
  apply auto
wenzelm@11049
   802
   apply (rule_tac [2] zcong_zless_imp_eq)
wenzelm@11049
   803
       apply (tactic {* stac (thm "zcong_cancel2" RS sym) 6 *})
wenzelm@11049
   804
         apply (rule_tac [8] zcong_trans)
wenzelm@11049
   805
          apply (simp_all (no_asm_simp))
wenzelm@11049
   806
   prefer 2
wenzelm@11049
   807
   apply (simp add: zcong_sym)
wenzelm@11049
   808
  apply (cut_tac a = a and n = n in zcong_lineq_ex)
wenzelm@11049
   809
    apply auto
wenzelm@11049
   810
  apply (rule_tac x = "x * b mod n" in exI)
wenzelm@11049
   811
  apply safe
wenzelm@11049
   812
    apply (simp_all (no_asm_simp) add: pos_mod_bound pos_mod_sign)
wenzelm@11049
   813
  apply (subst zcong_zmod)
wenzelm@11049
   814
  apply (subst zmod_zmult1_eq [symmetric])
wenzelm@11049
   815
  apply (subst zcong_zmod [symmetric])
paulson@11868
   816
  apply (subgoal_tac "[a * x * b = 1 * b] (mod n)")
wenzelm@11049
   817
   apply (rule_tac [2] zcong_zmult)
wenzelm@11049
   818
    apply (simp_all add: zmult_assoc)
wenzelm@11049
   819
  done
paulson@9508
   820
paulson@9508
   821
end