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