src/HOL/Old_Number_Theory/IntPrimes.thy
author haftmann
Sun, 16 Oct 2016 09:31:06 +0200
changeset 64246 15d1ee6e847b
parent 64240 eabf80376aab
permissions -rw-r--r--
eliminated irregular aliasses
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
     1
(*  Title:      HOL/Old_Number_Theory/IntPrimes.thy
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
     2
    Author:     Thomas M. Rasmussen
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
     3
    Copyright   2000  University of Cambridge
9508
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
     4
*)
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
     5
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
     6
section \<open>Divisibility and prime numbers (on integers)\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
     7
25596
ad9e3594f3f3 tuned header
haftmann
parents: 24759
diff changeset
     8
theory IntPrimes
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
     9
imports Primes
25596
ad9e3594f3f3 tuned header
haftmann
parents: 24759
diff changeset
    10
begin
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    11
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    12
text \<open>
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 61424
diff changeset
    13
  The \<open>dvd\<close> relation, GCD, Euclid's extended algorithm, primes,
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 61424
diff changeset
    14
  congruences (all on the Integers).  Comparable to theory \<open>Primes\<close>, but \<open>dvd\<close> is included here as it is not present in
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    15
  main HOL.  Also includes extended GCD and congruences not present in
63167
0909deb8059b isabelle update_cartouches -c -t;
wenzelm
parents: 61424
diff changeset
    16
  \<open>Primes\<close>.
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    17
\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    18
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    19
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    20
subsection \<open>Definitions\<close>
9508
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
    21
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    22
fun xzgcda :: "int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> int => (int * int * int)"
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
    23
where
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
    24
  "xzgcda m n r' r s' s t' t =
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 32479
diff changeset
    25
        (if r \<le> 0 then (r', s', t')
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
    26
         else xzgcda m n r (r' mod r) 
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
    27
                      s (s' - (r' div r) * s) 
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
    28
                      t (t' - (r' div r) * t))"
9508
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
    29
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    30
definition zprime :: "int \<Rightarrow> bool"
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    31
  where "zprime p = (1 < p \<and> (\<forall>m. 0 <= m & m dvd p --> m = 1 \<or> m = p))"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    32
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    33
definition xzgcd :: "int => int => int * int * int"
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    34
  where "xzgcd m n = xzgcda m n m n 1 0 0 1"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    35
38159
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    36
definition zcong :: "int => int => int => bool"  ("(1[_ = _] '(mod _'))")
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    37
  where "[a = b] (mod m) = (m dvd (a - b))"
e9b4835a54ee modernized specifications;
wenzelm
parents: 35440
diff changeset
    38
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    39
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    40
subsection \<open>Euclid's Algorithm and GCD\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    41
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    42
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    43
lemma zrelprime_zdvd_zmult_aux:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    44
     "zgcd n k = 1 ==> k dvd m * n ==> 0 \<le> m ==> k dvd m"
44766
d4d33a4d7548 avoid using legacy theorem names
huffman
parents: 41541
diff changeset
    45
    by (metis abs_of_nonneg dvd_triv_right zgcd_greatest_iff zgcd_zmult_distrib2_abs mult_1_right)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    46
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    47
lemma zrelprime_zdvd_zmult: "zgcd n k = 1 ==> k dvd m * n ==> k dvd m"
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
    48
  apply (case_tac "0 \<le> m")
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
    49
   apply (blast intro: zrelprime_zdvd_zmult_aux)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    50
  apply (subgoal_tac "k dvd -m")
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    51
   apply (rule_tac [2] zrelprime_zdvd_zmult_aux, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    52
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    53
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    54
lemma zgcd_geq_zero: "0 <= zgcd x y"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    55
  by (auto simp add: zgcd_def)
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    56
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    57
text\<open>This is merely a sanity check on zprime, since the previous version
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
    58
      denoted the empty set.\<close>
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 16417
diff changeset
    59
lemma "zprime 2"
13837
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
    60
  apply (auto simp add: zprime_def) 
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
    61
  apply (frule zdvd_imp_le, simp) 
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
    62
  apply (auto simp add: order_le_less dvd_def) 
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
    63
  done
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
    64
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    65
lemma zprime_imp_zrelprime:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    66
    "zprime p ==> \<not> p dvd n ==> zgcd n p = 1"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    67
  apply (auto simp add: zprime_def)
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
    68
  apply (metis zgcd_geq_zero zgcd_zdvd1 zgcd_zdvd2)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    69
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    70
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    71
lemma zless_zprime_imp_zrelprime:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    72
    "zprime p ==> 0 < n ==> n < p ==> zgcd n p = 1"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    73
  apply (erule zprime_imp_zrelprime)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
    74
  apply (erule zdvd_not_zless, assumption)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    75
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    76
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    77
lemma zprime_zdvd_zmult:
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 16417
diff changeset
    78
    "0 \<le> (m::int) ==> zprime p ==> p dvd m * n ==> p dvd m \<or> p dvd n"
27569
c8419e8a2d83 Simple theorems about zgcd moved to GCD.thy
chaieb
parents: 27556
diff changeset
    79
  by (metis zgcd_zdvd1 zgcd_zdvd2 zgcd_pos zprime_def zrelprime_dvd_mult)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    80
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    81
lemma zgcd_zadd_zmult [simp]: "zgcd (m + n * k) n = zgcd m n"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    82
  apply (rule zgcd_eq [THEN trans])
29948
cdf12a1cb963 Cleaned up IntDiv and removed subsumed lemmas.
nipkow
parents: 29925
diff changeset
    83
  apply (simp add: mod_add_eq)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    84
  apply (rule zgcd_eq [symmetric])
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    85
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    86
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    87
lemma zgcd_zdvd_zgcd_zmult: "zgcd m n dvd zgcd (k * m) n"
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
    88
by (simp add: zgcd_greatest_iff)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    89
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    90
lemma zgcd_zmult_zdvd_zgcd:
27569
c8419e8a2d83 Simple theorems about zgcd moved to GCD.thy
chaieb
parents: 27556
diff changeset
    91
    "zgcd k n = 1 ==> zgcd (k * m) n dvd zgcd m n"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    92
  apply (simp add: zgcd_greatest_iff)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    93
  apply (rule_tac n = k in zrelprime_zdvd_zmult)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    94
   prefer 2
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
    95
   apply (simp add: mult.commute)
23839
d9fa0f457d9a tidying using metis
paulson
parents: 21404
diff changeset
    96
  apply (metis zgcd_1 zgcd_commute zgcd_left_commute)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    97
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
    98
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
    99
lemma zgcd_zmult_cancel: "zgcd k n = 1 ==> zgcd (k * m) n = zgcd m n"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   100
  by (simp add: zgcd_def nat_abs_mult_distrib gcd_mult_cancel)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   101
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   102
lemma zgcd_zgcd_zmult:
27569
c8419e8a2d83 Simple theorems about zgcd moved to GCD.thy
chaieb
parents: 27556
diff changeset
   103
    "zgcd k m = 1 ==> zgcd n m = 1 ==> zgcd (k * n) m = 1"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   104
  by (simp add: zgcd_zmult_cancel)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   105
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   106
lemma zdvd_iff_zgcd: "0 < m ==> m dvd n \<longleftrightarrow> zgcd n m = m"
47162
9d7d919b9fd8 remove redundant lemma
huffman
parents: 44766
diff changeset
   107
  by (metis abs_of_pos dvd_mult_div_cancel zgcd_0 zgcd_commute zgcd_geq_zero zgcd_zdvd2 zgcd_zmult_eq_self)
23839
d9fa0f457d9a tidying using metis
paulson
parents: 21404
diff changeset
   108
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   109
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   110
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   111
subsection \<open>Congruences\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   112
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   113
lemma zcong_1 [simp]: "[a = b] (mod 1)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   114
  by (unfold zcong_def, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   115
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   116
lemma zcong_refl [simp]: "[k = k] (mod m)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   117
  by (unfold zcong_def, auto)
9508
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
   118
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   119
lemma zcong_sym: "[a = b] (mod m) = [b = a] (mod m)"
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   120
  unfolding zcong_def minus_diff_eq [of a, symmetric] dvd_minus_iff ..
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   121
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   122
lemma zcong_zadd:
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   123
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a + c = b + d] (mod m)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   124
  apply (unfold zcong_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   125
  apply (rule_tac s = "(a - b) + (c - d)" in subst)
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   126
   apply (rule_tac [2] dvd_add, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   127
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   128
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   129
lemma zcong_zdiff:
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   130
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a - c = b - d] (mod m)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   131
  apply (unfold zcong_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   132
  apply (rule_tac s = "(a - b) - (c - d)" in subst)
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   133
   apply (rule_tac [2] dvd_diff, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   134
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   135
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   136
lemma zcong_trans:
29925
17d1e32ef867 dvd and setprod lemmas
nipkow
parents: 29667
diff changeset
   137
  "[a = b] (mod m) ==> [b = c] (mod m) ==> [a = c] (mod m)"
17d1e32ef867 dvd and setprod lemmas
nipkow
parents: 29667
diff changeset
   138
unfolding zcong_def by (auto elim!: dvdE simp add: algebra_simps)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   139
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   140
lemma zcong_zmult:
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   141
    "[a = b] (mod m) ==> [c = d] (mod m) ==> [a * c = b * d] (mod m)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   142
  apply (rule_tac b = "b * c" in zcong_trans)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   143
   apply (unfold zcong_def)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   144
  apply (metis right_diff_distrib dvd_mult mult.commute)
44766
d4d33a4d7548 avoid using legacy theorem names
huffman
parents: 41541
diff changeset
   145
  apply (metis right_diff_distrib dvd_mult)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   146
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   147
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   148
lemma zcong_scalar: "[a = b] (mod m) ==> [a * k = b * k] (mod m)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   149
  by (rule zcong_zmult, simp_all)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   150
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   151
lemma zcong_scalar2: "[a = b] (mod m) ==> [k * a = k * b] (mod m)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   152
  by (rule zcong_zmult, simp_all)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   153
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   154
lemma zcong_zmult_self: "[a * m = b * m] (mod m)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   155
  apply (unfold zcong_def)
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   156
  apply (rule dvd_diff, simp_all)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   157
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   158
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   159
lemma zcong_square:
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 16417
diff changeset
   160
   "[| zprime p;  0 < a;  [a * a = 1] (mod p)|]
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   161
    ==> [a = 1] (mod p) \<or> [a = p - 1] (mod p)"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   162
  apply (unfold zcong_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   163
  apply (rule zprime_zdvd_zmult)
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   164
    apply (rule_tac [3] s = "a * a - 1 + p * (1 - a)" in subst)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   165
     prefer 4
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   166
     apply (simp add: zdvd_reduce)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   167
    apply (simp_all add: left_diff_distrib mult.commute right_diff_distrib)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   168
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   169
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   170
lemma zcong_cancel:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   171
  "0 \<le> m ==>
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   172
    zgcd k m = 1 ==> [a * k = b * k] (mod m) = [a = b] (mod m)"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   173
  apply safe
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   174
   prefer 2
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   175
   apply (blast intro: zcong_scalar)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   176
  apply (case_tac "b < a")
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   177
   prefer 2
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   178
   apply (subst zcong_sym)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   179
   apply (unfold zcong_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   180
   apply (rule_tac [!] zrelprime_zdvd_zmult)
44766
d4d33a4d7548 avoid using legacy theorem names
huffman
parents: 41541
diff changeset
   181
     apply (simp_all add: left_diff_distrib)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   182
  apply (subgoal_tac "m dvd (-(a * k - b * k))")
14271
8ed6989228bb Simplification of the development of Integers
paulson
parents: 14174
diff changeset
   183
   apply simp
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   184
  apply (subst dvd_minus_iff, assumption)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   185
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   186
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   187
lemma zcong_cancel2:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   188
  "0 \<le> m ==>
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   189
    zgcd k m = 1 ==> [k * a = k * b] (mod m) = [a = b] (mod m)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   190
  by (simp add: mult.commute zcong_cancel)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   191
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   192
lemma zcong_zgcd_zmult_zmod:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   193
  "[a = b] (mod m) ==> [a = b] (mod n) ==> zgcd m n = 1
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   194
    ==> [a = b] (mod m * n)"
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27569
diff changeset
   195
  apply (auto simp add: zcong_def dvd_def)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   196
  apply (subgoal_tac "m dvd n * ka")
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   197
   apply (subgoal_tac "m dvd ka")
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   198
    apply (case_tac [2] "0 \<le> ka")
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   199
  apply (metis dvd_mult_div_cancel dvd_refl dvd_mult_left mult.commute zrelprime_zdvd_zmult)
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   200
  apply (metis abs_dvd_iff abs_of_nonneg add_0 zgcd_0_left zgcd_commute zgcd_zadd_zmult zgcd_zdvd_zgcd_zmult zgcd_zmult_distrib2_abs mult_1_right mult.commute)
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   201
  apply (metis mult_le_0_iff  zdvd_mono zdvd_mult_cancel dvd_triv_left zero_le_mult_iff order_antisym linorder_linear order_refl mult.commute zrelprime_zdvd_zmult)
30042
31039ee583fa Removed subsumed lemmas
nipkow
parents: 30034
diff changeset
   202
  apply (metis dvd_triv_left)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   203
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   204
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   205
lemma zcong_zless_imp_eq:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   206
  "0 \<le> a ==>
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   207
    a < m ==> 0 \<le> b ==> b < m ==> [a = b] (mod m) ==> a = b"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   208
  apply (unfold zcong_def dvd_def, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   209
  apply (drule_tac f = "\<lambda>z. z mod m" in arg_cong)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   210
  apply (metis diff_add_cancel mod_pos_pos_trivial add_0 add.commute zmod_eq_0_iff mod_add_right_eq)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   211
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   212
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   213
lemma zcong_square_zless:
16663
13e9c402308b prime is a predicate now.
nipkow
parents: 16417
diff changeset
   214
  "zprime p ==> 0 < a ==> a < p ==>
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   215
    [a * a = 1] (mod p) ==> a = 1 \<or> a = p - 1"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   216
  apply (cut_tac p = p and a = a in zcong_square)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   217
     apply (simp add: zprime_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   218
    apply (auto intro: zcong_zless_imp_eq)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   219
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   220
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   221
lemma zcong_not:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   222
    "0 < a ==> a < m ==> 0 < b ==> b < a ==> \<not> [a = b] (mod m)"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   223
  apply (unfold zcong_def)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   224
  apply (rule zdvd_not_zless, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   225
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   226
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   227
lemma zcong_zless_0:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   228
    "0 \<le> a ==> a < m ==> [a = 0] (mod m) ==> a = 0"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   229
  apply (unfold zcong_def dvd_def, auto)
64240
eabf80376aab more standardized names
haftmann
parents: 63167
diff changeset
   230
  apply (metis div_pos_pos_trivial linorder_not_less nonzero_mult_div_cancel_left)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   231
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   232
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   233
lemma zcong_zless_unique:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   234
    "0 < m ==> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   235
  apply auto
23839
d9fa0f457d9a tidying using metis
paulson
parents: 21404
diff changeset
   236
   prefer 2 apply (metis zcong_sym zcong_trans zcong_zless_imp_eq)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   237
  apply (unfold zcong_def dvd_def)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   238
  apply (rule_tac x = "a mod m" in exI, auto)
64246
15d1ee6e847b eliminated irregular aliasses
haftmann
parents: 64240
diff changeset
   239
  apply (metis minus_mod_eq_mult_div [symmetric])
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   240
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   241
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   242
lemma zcong_iff_lin: "([a = b] (mod m)) = (\<exists>k. b = a + m * k)"
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27569
diff changeset
   243
  unfolding zcong_def
29667
53103fc8ffa3 Replaced group_ and ring_simps by algebra_simps;
nipkow
parents: 29412
diff changeset
   244
  apply (auto elim!: dvdE simp add: algebra_simps)
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 27569
diff changeset
   245
  apply (rule_tac x = "-k" in exI) apply simp
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   246
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   247
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   248
lemma zgcd_zcong_zgcd:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   249
  "0 < m ==>
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   250
    zgcd a m = 1 ==> [a = b] (mod m) ==> zgcd b m = 1"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   251
  by (auto simp add: zcong_iff_lin)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   252
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   253
lemma zcong_zmod_aux:
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   254
     "a - b = (m::int) * (a div m - b div m) + (a mod m - b mod m)"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
   255
  by(simp add: right_diff_distrib add_diff_eq eq_diff_eq ac_simps)
13517
42efec18f5b2 Added div+mod cancelling simproc
nipkow
parents: 13193
diff changeset
   256
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   257
lemma zcong_zmod: "[a = b] (mod m) = [a mod m = b mod m] (mod m)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   258
  apply (unfold zcong_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   259
  apply (rule_tac t = "a - b" in ssubst)
14174
f3cafd2929d5 Methods rule_tac etc support static (Isar) contexts.
ballarin
parents: 13837
diff changeset
   260
  apply (rule_tac m = m in zcong_zmod_aux)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   261
  apply (rule trans)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   262
   apply (rule_tac [2] k = m and m = "a div m - b div m" in zdvd_reduce)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   263
  apply (simp add: add.commute)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   264
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   265
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   266
lemma zcong_zmod_eq: "0 < m ==> [a = b] (mod m) = (a mod m = b mod m)"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   267
  apply auto
23839
d9fa0f457d9a tidying using metis
paulson
parents: 21404
diff changeset
   268
  apply (metis pos_mod_conj zcong_zless_imp_eq zcong_zmod)
d9fa0f457d9a tidying using metis
paulson
parents: 21404
diff changeset
   269
  apply (metis zcong_refl zcong_zmod)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   270
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   271
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   272
lemma zcong_zminus [iff]: "[a = b] (mod -m) = [a = b] (mod m)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   273
  by (auto simp add: zcong_def)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   274
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   275
lemma zcong_zero [iff]: "[a = b] (mod 0) = (a = b)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   276
  by (auto simp add: zcong_def)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   277
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   278
lemma "[a = b] (mod m) = (a mod m = b mod m)"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 39159
diff changeset
   279
  apply (cases "m = 0", simp)
13193
d5234c261813 finished an incomplete proof
paulson
parents: 13187
diff changeset
   280
  apply (simp add: linorder_neq_iff)
d5234c261813 finished an incomplete proof
paulson
parents: 13187
diff changeset
   281
  apply (erule disjE)  
d5234c261813 finished an incomplete proof
paulson
parents: 13187
diff changeset
   282
   prefer 2 apply (simp add: zcong_zmod_eq)
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   283
  txt\<open>Remainding case: @{term "m<0"}\<close>
44766
d4d33a4d7548 avoid using legacy theorem names
huffman
parents: 41541
diff changeset
   284
  apply (rule_tac t = m in minus_minus [THEN subst])
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   285
  apply (subst zcong_zminus)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   286
  apply (subst zcong_zmod_eq, arith)
13193
d5234c261813 finished an incomplete proof
paulson
parents: 13187
diff changeset
   287
  apply (frule neg_mod_bound [of _ a], frule neg_mod_bound [of _ b]) 
13788
fd03c4ab89d4 pos/neg_mod_sign/bound are now simp rules.
nipkow
parents: 13630
diff changeset
   288
  apply (simp add: zmod_zminus2_eq_if del: neg_mod_bound)
13193
d5234c261813 finished an incomplete proof
paulson
parents: 13187
diff changeset
   289
  done
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   290
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   291
subsection \<open>Modulo\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   292
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   293
lemma zmod_zdvd_zmod:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   294
    "0 < (m::int) ==> m dvd b ==> (a mod b mod m) = (a mod m)"
30034
60f64f112174 removed redundant thms
nipkow
parents: 29948
diff changeset
   295
  by (rule mod_mod_cancel) 
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   296
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   297
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   298
subsection \<open>Extended GCD\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   299
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   300
declare xzgcda.simps [simp del]
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   301
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   302
lemma xzgcd_correct_aux1:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   303
  "zgcd r' r = k --> 0 < r -->
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   304
    (\<exists>sn tn. xzgcda m n r' r s' s t' t = (k, sn, tn))"
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   305
  apply (induct m n r' r s' s t' t rule: xzgcda.induct)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   306
  apply (subst zgcd_eq)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   307
  apply (subst xzgcda.simps, auto)
24759
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   308
  apply (case_tac "r' mod r = 0")
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   309
   prefer 2
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   310
   apply (frule_tac a = "r'" in pos_mod_sign, auto)
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   311
  apply (rule exI)
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   312
  apply (rule exI)
b448f94b1c88 fixed metis proof (Why did it stop working?);
wenzelm
parents: 24181
diff changeset
   313
  apply (subst xzgcda.simps, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   314
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   315
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   316
lemma xzgcd_correct_aux2:
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   317
  "(\<exists>sn tn. xzgcda m n r' r s' s t' t = (k, sn, tn)) --> 0 < r -->
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   318
    zgcd r' r = k"
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   319
  apply (induct m n r' r s' s t' t rule: xzgcda.induct)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   320
  apply (subst zgcd_eq)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   321
  apply (subst xzgcda.simps)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   322
  apply (auto simp add: linorder_not_le)
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   323
  apply (case_tac "r' mod r = 0")
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   324
   prefer 2
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   325
   apply (frule_tac a = "r'" in pos_mod_sign, auto)
61424
c3658c18b7bc prod_case as canonical name for product type eliminator
haftmann
parents: 61382
diff changeset
   326
  apply (metis prod.inject xzgcda.simps order_refl)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   327
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   328
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   329
lemma xzgcd_correct:
27569
c8419e8a2d83 Simple theorems about zgcd moved to GCD.thy
chaieb
parents: 27556
diff changeset
   330
    "0 < n ==> (zgcd m n = k) = (\<exists>s t. xzgcd m n = (k, s, t))"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   331
  apply (unfold xzgcd_def)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   332
  apply (rule iffI)
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   333
   apply (rule_tac [2] xzgcd_correct_aux2 [THEN mp, THEN mp])
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   334
    apply (rule xzgcd_correct_aux1 [THEN mp, THEN mp], auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   335
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   336
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   337
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   338
text \<open>\medskip @{term xzgcd} linear\<close>
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   339
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   340
lemma xzgcda_linear_aux1:
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   341
  "(a - r * b) * m + (c - r * d) * (n::int) =
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   342
   (a * m + c * n) - r * (b * m + d * n)"
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   343
  by (simp add: left_diff_distrib distrib_left mult.assoc)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   344
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   345
lemma xzgcda_linear_aux2:
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   346
  "r' = s' * m + t' * n ==> r = s * m + t * n
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   347
    ==> (r' mod r) = (s' - (r' div r) * s) * m + (t' - (r' div r) * t) * (n::int)"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   348
  apply (rule trans)
13524
604d0f3622d6 *** empty log message ***
wenzelm
parents: 13517
diff changeset
   349
   apply (rule_tac [2] xzgcda_linear_aux1 [symmetric])
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   350
  apply (simp add: eq_diff_eq mult.commute)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   351
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   352
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   353
lemma order_le_neq_implies_less: "(x::'a::order) \<le> y ==> x \<noteq> y ==> x < y"
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   354
  by (rule iffD2 [OF order_less_le conjI])
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   355
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   356
lemma xzgcda_linear [rule_format]:
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   357
  "0 < r --> xzgcda m n r' r s' s t' t = (rn, sn, tn) -->
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   358
    r' = s' * m + t' * n -->  r = s * m + t * n --> rn = sn * m + tn * n"
35440
bdf8ad377877 killed more recdefs
krauss
parents: 33657
diff changeset
   359
  apply (induct m n r' r s' s t' t rule: xzgcda.induct)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   360
  apply (subst xzgcda.simps)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   361
  apply (simp (no_asm))
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   362
  apply (rule impI)+
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   363
  apply (case_tac "r' mod r = 0")
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   364
   apply (simp add: xzgcda.simps, clarify)
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   365
  apply (subgoal_tac "0 < r' mod r")
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   366
   apply (rule_tac [2] order_le_neq_implies_less)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   367
   apply (rule_tac [2] pos_mod_sign)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   368
    apply (cut_tac m = m and n = n and r' = r' and r = r and s' = s' and
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   369
      s = s and t' = t' and t = t in xzgcda_linear_aux2, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   370
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   371
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   372
lemma xzgcd_linear:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   373
    "0 < n ==> xzgcd m n = (r, s, t) ==> r = s * m + t * n"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   374
  apply (unfold xzgcd_def)
13837
8dd150d36c65 Reorganized, moving many results about the integer dvd relation from IntPrimes
paulson
parents: 13833
diff changeset
   375
  apply (erule xzgcda_linear, assumption, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   376
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   377
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   378
lemma zgcd_ex_linear:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   379
    "0 < n ==> zgcd m n = k ==> (\<exists>s t. k = s * m + t * n)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   380
  apply (simp add: xzgcd_correct, safe)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   381
  apply (rule exI)+
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   382
  apply (erule xzgcd_linear, auto)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   383
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   384
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   385
lemma zcong_lineq_ex:
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   386
    "0 < n ==> zgcd a n = 1 ==> \<exists>x. [a * x = 1] (mod n)"
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   387
  apply (cut_tac m = a and n = n and k = 1 in zgcd_ex_linear, safe)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   388
  apply (rule_tac x = s in exI)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   389
  apply (rule_tac b = "s * a + t * n" in zcong_trans)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   390
   prefer 2
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   391
   apply simp
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   392
  apply (unfold zcong_def)
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   393
  apply (simp (no_asm) add: mult.commute)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   394
  done
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   395
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   396
lemma zcong_lineq_unique:
11868
56db9f3a6b3e Numerals now work for the integers: the binary numerals for 0 and 1 rewrite
paulson
parents: 11701
diff changeset
   397
  "0 < n ==>
27556
292098f2efdf unified curried gcd, lcm, zgcd, zlcm
haftmann
parents: 27368
diff changeset
   398
    zgcd a n = 1 ==> \<exists>!x. 0 \<le> x \<and> x < n \<and> [a * x = b] (mod n)"
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   399
  apply auto
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   400
   apply (rule_tac [2] zcong_zless_imp_eq)
61382
efac889fccbc isabelle update_cartouches;
wenzelm
parents: 59498
diff changeset
   401
       apply (tactic \<open>stac @{context} (@{thm zcong_cancel2} RS sym) 6\<close>)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   402
         apply (rule_tac [8] zcong_trans)
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   403
          apply (simp_all (no_asm_simp))
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   404
   prefer 2
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   405
   apply (simp add: zcong_sym)
13833
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   406
  apply (cut_tac a = a and n = n in zcong_lineq_ex, auto)
f8dcb1d9ea68 zprime_def fixes by Jeremy Avigad
paulson
parents: 13788
diff changeset
   407
  apply (rule_tac x = "x * b mod n" in exI, safe)
13788
fd03c4ab89d4 pos/neg_mod_sign/bound are now simp rules.
nipkow
parents: 13630
diff changeset
   408
    apply (simp_all (no_asm_simp))
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 49962
diff changeset
   409
  apply (metis zcong_scalar zcong_zmod mod_mult_right_eq mult_1 mult.assoc)
11049
7eef34adb852 HOL-NumberTheory: converted to new-style format and proper document setup;
wenzelm
parents: 10147
diff changeset
   410
  done
9508
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
   411
4d01dbf6ded7 Chinese Remainder Theorem, Wilsons Theorem, etc., by T M Masmussen
paulson
parents:
diff changeset
   412
end