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