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