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