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