src/HOL/Number_Theory/Cong.thy
author paulson <lp15@cam.ac.uk>
Sun, 02 Feb 2014 19:15:25 +0000
changeset 55242 413ec965f95d
parent 55161 8eb891539804
child 55321 eadea363deb6
permissions -rw-r--r--
Number_Theory: prime is no longer overloaded, but only for nat. Automatic coercion to int enabled.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41959
b460124855b8 tuned headers;
wenzelm
parents: 41541
diff changeset
     1
(*  Title:      HOL/Number_Theory/Cong.thy
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     2
    Authors:    Christophe Tabacznyj, Lawrence C. Paulson, Amine Chaieb,
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     3
                Thomas M. Rasmussen, Jeremy Avigad
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     4
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     5
Defines congruence (notation: [x = y] (mod z)) for natural numbers and
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     6
integers.
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     7
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     8
This file combines and revises a number of prior developments.
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
     9
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    10
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    11
and Lawrence C. Paulson, based on \cite{davenport92}. They introduced
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    12
gcd, lcm, and prime for the natural numbers.
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    13
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    14
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    15
extended gcd, lcm, primes to the integers. Amine Chaieb provided
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    16
another extension of the notions to the integers, and added a number
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    17
of results to "Primes" and "GCD".
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    18
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    19
The original theory, "IntPrimes", by Thomas M. Rasmussen, defined and
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    20
developed the congruence relations on the integers. The notion was
33718
06e9aff51d17 Fixed a typo in a comment.
webertj
parents: 32479
diff changeset
    21
extended to the natural numbers by Chaieb. Jeremy Avigad combined
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    22
these, revised and tidied them, made the development uniform for the
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    23
natural numbers and the integers, and added a number of new theorems.
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    24
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    25
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    26
header {* Congruence *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    27
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    28
theory Cong
37293
2c9ed7478e6e avoid duplicate import
haftmann
parents: 36350
diff changeset
    29
imports Primes
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    30
begin
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    31
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    32
subsection {* Turn off @{text One_nat_def} *}
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    33
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    34
lemma power_eq_one_eq_nat [simp]: "((x::nat)^m = 1) = (m = 0 | x = 1)"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    35
  by (induct m) auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    36
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    37
declare mod_pos_pos_trivial [simp]
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    38
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    39
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    40
subsection {* Main definitions *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    41
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    42
class cong =
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    43
  fixes cong :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" ("(1[_ = _] '(mod _'))")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    44
begin
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    45
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    46
abbreviation notcong :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool"  ("(1[_ \<noteq> _] '(mod _'))")
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    47
  where "notcong x y m \<equiv> \<not> cong x y m"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    48
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    49
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    50
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    51
(* definitions for the natural numbers *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    52
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    53
instantiation nat :: cong
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    54
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    55
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    56
definition cong_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    57
  where "cong_nat x y m = ((x mod m) = (y mod m))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    58
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    59
instance ..
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    60
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    61
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    62
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    63
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    64
(* definitions for the integers *)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    65
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    66
instantiation int :: cong
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    67
begin
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    68
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    69
definition cong_int :: "int \<Rightarrow> int \<Rightarrow> int \<Rightarrow> bool"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    70
  where "cong_int x y m = ((x mod m) = (y mod m))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    71
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    72
instance ..
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    73
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    74
end
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    75
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    76
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    77
subsection {* Set up Transfer *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    78
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    79
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    80
lemma transfer_nat_int_cong:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    81
  "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> m >= 0 \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    82
    ([(nat x) = (nat y)] (mod (nat m))) = ([x = y] (mod m))"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    83
  unfolding cong_int_def cong_nat_def
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
    84
  by (metis Divides.transfer_int_nat_functions(2) nat_0_le nat_mod_distrib)
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
    85
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    86
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    87
declare transfer_morphism_nat_int[transfer add return:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    88
    transfer_nat_int_cong]
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    89
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    90
lemma transfer_int_nat_cong:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    91
  "[(int x) = (int y)] (mod (int m)) = [x = y] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    92
  apply (auto simp add: cong_int_def cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    93
  apply (auto simp add: zmod_int [symmetric])
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    94
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    95
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
    96
declare transfer_morphism_int_nat[transfer add return:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    97
    transfer_int_nat_cong]
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    98
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
    99
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   100
subsection {* Congruence *}
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   101
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   102
(* was zcong_0, etc. *)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   103
lemma cong_0_nat [simp, presburger]: "([(a::nat) = b] (mod 0)) = (a = b)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   104
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   105
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   106
lemma cong_0_int [simp, presburger]: "([(a::int) = b] (mod 0)) = (a = b)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   107
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   108
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   109
lemma cong_1_nat [simp, presburger]: "[(a::nat) = b] (mod 1)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   110
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   111
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   112
lemma cong_Suc_0_nat [simp, presburger]: "[(a::nat) = b] (mod Suc 0)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   113
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   114
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   115
lemma cong_1_int [simp, presburger]: "[(a::int) = b] (mod 1)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   116
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   117
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   118
lemma cong_refl_nat [simp]: "[(k::nat) = k] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   119
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   120
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   121
lemma cong_refl_int [simp]: "[(k::int) = k] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   122
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   123
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   124
lemma cong_sym_nat: "[(a::nat) = b] (mod m) \<Longrightarrow> [b = a] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   125
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   126
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   127
lemma cong_sym_int: "[(a::int) = b] (mod m) \<Longrightarrow> [b = a] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   128
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   129
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   130
lemma cong_sym_eq_nat: "[(a::nat) = b] (mod m) = [b = a] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   131
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   132
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   133
lemma cong_sym_eq_int: "[(a::int) = b] (mod m) = [b = a] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   134
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   135
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   136
lemma cong_trans_nat [trans]:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   137
    "[(a::nat) = b] (mod m) \<Longrightarrow> [b = c] (mod m) \<Longrightarrow> [a = c] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   138
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   139
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   140
lemma cong_trans_int [trans]:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   141
    "[(a::int) = b] (mod m) \<Longrightarrow> [b = c] (mod m) \<Longrightarrow> [a = c] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   142
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   143
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   144
lemma cong_add_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   145
    "[(a::nat) = b] (mod m) \<Longrightarrow> [c = d] (mod m) \<Longrightarrow> [a + c = b + d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   146
  unfolding cong_nat_def  by (metis mod_add_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   147
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   148
lemma cong_add_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   149
    "[(a::int) = b] (mod m) \<Longrightarrow> [c = d] (mod m) \<Longrightarrow> [a + c = b + d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   150
  unfolding cong_int_def  by (metis mod_add_cong)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   151
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   152
lemma cong_diff_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   153
    "[(a::int) = b] (mod m) \<Longrightarrow> [c = d] (mod m) \<Longrightarrow> [a - c = b - d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   154
  unfolding cong_int_def  by (metis mod_diff_cong) 
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   155
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   156
lemma cong_diff_aux_int:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   157
  "(a::int) >= c \<Longrightarrow> b >= d \<Longrightarrow> [(a::int) = b] (mod m) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   158
      [c = d] (mod m) \<Longrightarrow> [tsub a c = tsub b d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   159
  by (metis cong_diff_int tsub_eq)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   160
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   161
lemma cong_diff_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   162
  assumes "(a::nat) >= c" and "b >= d" and "[a = b] (mod m)" and
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   163
    "[c = d] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   164
  shows "[a - c = b - d] (mod m)"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   165
  using assms by (rule cong_diff_aux_int [transferred]);
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   166
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   167
lemma cong_mult_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   168
    "[(a::nat) = b] (mod m) \<Longrightarrow> [c = d] (mod m) \<Longrightarrow> [a * c = b * d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   169
  unfolding cong_nat_def  by (metis mod_mult_cong) 
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   170
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   171
lemma cong_mult_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   172
    "[(a::int) = b] (mod m) \<Longrightarrow> [c = d] (mod m) \<Longrightarrow> [a * c = b * d] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   173
  unfolding cong_int_def  by (metis mod_mult_cong) 
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   174
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   175
lemma cong_exp_nat: "[(x::nat) = y] (mod n) \<Longrightarrow> [x^k = y^k] (mod n)"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   176
  by (induct k) (auto simp add: cong_mult_nat)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   177
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   178
lemma cong_exp_int: "[(x::int) = y] (mod n) \<Longrightarrow> [x^k = y^k] (mod n)"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   179
  by (induct k) (auto simp add: cong_mult_int)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   180
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   181
lemma cong_setsum_nat [rule_format]:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   182
    "(ALL x: A. [((f x)::nat) = g x] (mod m)) \<longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   183
      [(SUM x:A. f x) = (SUM x:A. g x)] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   184
  apply (cases "finite A")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   185
  apply (induct set: finite)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   186
  apply (auto intro: cong_add_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   187
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   188
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   189
lemma cong_setsum_int [rule_format]:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   190
    "(ALL x: A. [((f x)::int) = g x] (mod m)) \<longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   191
      [(SUM x:A. f x) = (SUM x:A. g x)] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   192
  apply (cases "finite A")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   193
  apply (induct set: finite)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   194
  apply (auto intro: cong_add_int)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   195
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   196
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   197
lemma cong_setprod_nat [rule_format]:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   198
    "(ALL x: A. [((f x)::nat) = g x] (mod m)) \<longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   199
      [(PROD x:A. f x) = (PROD x:A. g x)] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   200
  apply (cases "finite A")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   201
  apply (induct set: finite)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   202
  apply (auto intro: cong_mult_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   203
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   204
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   205
lemma cong_setprod_int [rule_format]:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   206
    "(ALL x: A. [((f x)::int) = g x] (mod m)) \<longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   207
      [(PROD x:A. f x) = (PROD x:A. g x)] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   208
  apply (cases "finite A")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   209
  apply (induct set: finite)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   210
  apply (auto intro: cong_mult_int)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   211
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   212
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   213
lemma cong_scalar_nat: "[(a::nat)= b] (mod m) \<Longrightarrow> [a * k = b * k] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   214
  by (rule cong_mult_nat) simp_all
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   215
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   216
lemma cong_scalar_int: "[(a::int)= b] (mod m) \<Longrightarrow> [a * k = b * k] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   217
  by (rule cong_mult_int) simp_all
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   218
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   219
lemma cong_scalar2_nat: "[(a::nat)= b] (mod m) \<Longrightarrow> [k * a = k * b] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   220
  by (rule cong_mult_nat) simp_all
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   221
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   222
lemma cong_scalar2_int: "[(a::int)= b] (mod m) \<Longrightarrow> [k * a = k * b] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   223
  by (rule cong_mult_int) simp_all
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   224
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   225
lemma cong_mult_self_nat: "[(a::nat) * m = 0] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   226
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   227
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   228
lemma cong_mult_self_int: "[(a::int) * m = 0] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   229
  unfolding cong_int_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   230
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   231
lemma cong_eq_diff_cong_0_int: "[(a::int) = b] (mod m) = [a - b = 0] (mod m)"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   232
  by (metis cong_add_int cong_diff_int cong_refl_int diff_add_cancel diff_self)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   233
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   234
lemma cong_eq_diff_cong_0_aux_int: "a >= b \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   235
    [(a::int) = b] (mod m) = [tsub a b = 0] (mod m)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   236
  by (subst tsub_eq, assumption, rule cong_eq_diff_cong_0_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   237
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   238
lemma cong_eq_diff_cong_0_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   239
  assumes "(a::nat) >= b"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   240
  shows "[a = b] (mod m) = [a - b = 0] (mod m)"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   241
  using assms by (rule cong_eq_diff_cong_0_aux_int [transferred])
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   242
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   243
lemma cong_diff_cong_0'_nat:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   244
  "[(x::nat) = y] (mod n) \<longleftrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   245
    (if x <= y then [y - x = 0] (mod n) else [x - y = 0] (mod n))"
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   246
  by (metis cong_eq_diff_cong_0_nat cong_sym_nat nat_le_linear)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   247
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   248
lemma cong_altdef_nat: "(a::nat) >= b \<Longrightarrow> [a = b] (mod m) = (m dvd (a - b))"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   249
  apply (subst cong_eq_diff_cong_0_nat, assumption)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   250
  apply (unfold cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   251
  apply (simp add: dvd_eq_mod_eq_0 [symmetric])
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   252
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   253
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   254
lemma cong_altdef_int: "[(a::int) = b] (mod m) = (m dvd (a - b))"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   255
  apply (subst cong_eq_diff_cong_0_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   256
  apply (unfold cong_int_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   257
  apply (simp add: dvd_eq_mod_eq_0 [symmetric])
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   258
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   259
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   260
lemma cong_abs_int: "[(x::int) = y] (mod abs m) = [x = y] (mod m)"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   261
  by (simp add: cong_altdef_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   262
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   263
lemma cong_square_int:
55242
413ec965f95d Number_Theory: prime is no longer overloaded, but only for nat. Automatic coercion to int enabled.
paulson <lp15@cam.ac.uk>
parents: 55161
diff changeset
   264
  fixes a::int
413ec965f95d Number_Theory: prime is no longer overloaded, but only for nat. Automatic coercion to int enabled.
paulson <lp15@cam.ac.uk>
parents: 55161
diff changeset
   265
  shows "\<lbrakk> prime p; 0 < a; [a * a = 1] (mod p) \<rbrakk>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   266
    \<Longrightarrow> [a = 1] (mod p) \<or> [a = - 1] (mod p)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   267
  apply (simp only: cong_altdef_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   268
  apply (subst prime_dvd_mult_eq_int [symmetric], assumption)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   269
  apply (auto simp add: field_simps)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   270
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   271
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   272
lemma cong_mult_rcancel_int:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   273
    "coprime k (m::int) \<Longrightarrow> [a * k = b * k] (mod m) = [a = b] (mod m)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   274
  apply (subst (1 2) cong_altdef_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   275
  apply (subst left_diff_distrib [symmetric])
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   276
  apply (rule coprime_dvd_mult_iff_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   277
  apply (subst gcd_commute_int, assumption)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   278
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   279
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   280
lemma cong_mult_rcancel_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   281
  assumes  "coprime k (m::nat)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   282
  shows "[a * k = b * k] (mod m) = [a = b] (mod m)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   283
  apply (rule cong_mult_rcancel_int [transferred])
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   284
  using assms apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   285
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   286
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   287
lemma cong_mult_lcancel_nat:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   288
    "coprime k (m::nat) \<Longrightarrow> [k * a = k * b ] (mod m) = [a = b] (mod m)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   289
  by (simp add: mult_commute cong_mult_rcancel_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   290
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   291
lemma cong_mult_lcancel_int:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   292
    "coprime k (m::int) \<Longrightarrow> [k * a = k * b] (mod m) = [a = b] (mod m)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   293
  by (simp add: mult_commute cong_mult_rcancel_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   294
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   295
(* was zcong_zgcd_zmult_zmod *)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   296
lemma coprime_cong_mult_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   297
  "[(a::int) = b] (mod m) \<Longrightarrow> [a = b] (mod n) \<Longrightarrow> coprime m n
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   298
    \<Longrightarrow> [a = b] (mod m * n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   299
  apply (simp only: cong_altdef_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   300
  apply (erule (2) divides_mult_int)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   301
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   302
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   303
lemma coprime_cong_mult_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   304
  assumes "[(a::nat) = b] (mod m)" and "[a = b] (mod n)" and "coprime m n"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   305
  shows "[a = b] (mod m * n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   306
  apply (rule coprime_cong_mult_int [transferred])
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   307
  using assms apply auto
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   308
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   309
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   310
lemma cong_less_imp_eq_nat: "0 \<le> (a::nat) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   311
    a < m \<Longrightarrow> 0 \<le> b \<Longrightarrow> b < m \<Longrightarrow> [a = b] (mod m) \<Longrightarrow> a = b"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   312
  by (auto simp add: cong_nat_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   313
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   314
lemma cong_less_imp_eq_int: "0 \<le> (a::int) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   315
    a < m \<Longrightarrow> 0 \<le> b \<Longrightarrow> b < m \<Longrightarrow> [a = b] (mod m) \<Longrightarrow> a = b"
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   316
  by (auto simp add: cong_int_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   317
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   318
lemma cong_less_unique_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   319
    "0 < (m::nat) \<Longrightarrow> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   320
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   321
  apply (rule_tac x = "a mod m" in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   322
  apply (unfold cong_nat_def, auto)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   323
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   324
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   325
lemma cong_less_unique_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   326
    "0 < (m::int) \<Longrightarrow> (\<exists>!b. 0 \<le> b \<and> b < m \<and> [a = b] (mod m))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   327
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   328
  apply (rule_tac x = "a mod m" in exI)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   329
  apply (unfold cong_int_def, auto)
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   330
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   331
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   332
lemma cong_iff_lin_int: "([(a::int) = b] (mod m)) = (\<exists>k. b = a + m * k)"
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   333
  apply (auto simp add: cong_altdef_int dvd_def field_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   334
  apply (rule_tac [!] x = "-k" in exI, auto)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   335
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   336
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   337
lemma cong_iff_lin_nat: "([(a::nat) = b] (mod m)) =
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   338
    (\<exists>k1 k2. b + k1 * m = a + k2 * m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   339
  apply (rule iffI)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   340
  apply (cases "b <= a")
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   341
  apply (subst (asm) cong_altdef_nat, assumption)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   342
  apply (unfold dvd_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   343
  apply (rule_tac x = k in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   344
  apply (rule_tac x = 0 in exI)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   345
  apply (auto simp add: field_simps)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   346
  apply (subst (asm) cong_sym_eq_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   347
  apply (subst (asm) cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   348
  apply force
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   349
  apply (unfold dvd_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   350
  apply (rule_tac x = 0 in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   351
  apply (rule_tac x = k in exI)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   352
  apply (auto simp add: field_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   353
  apply (unfold cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   354
  apply (subgoal_tac "a mod m = (a + k2 * m) mod m")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   355
  apply (erule ssubst)back
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   356
  apply (erule subst)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   357
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   358
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   359
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   360
lemma cong_gcd_eq_int: "[(a::int) = b] (mod m) \<Longrightarrow> gcd a m = gcd b m"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   361
  apply (subst (asm) cong_iff_lin_int, auto)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   362
  apply (subst add_commute)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   363
  apply (subst (2) gcd_commute_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   364
  apply (subst mult_commute)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   365
  apply (subst gcd_add_mult_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   366
  apply (rule gcd_commute_int)
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   367
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   368
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   369
lemma cong_gcd_eq_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   370
  assumes "[(a::nat) = b] (mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   371
  shows "gcd a m = gcd b m"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   372
  apply (rule cong_gcd_eq_int [transferred])
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   373
  using assms apply auto
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   374
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   375
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   376
lemma cong_imp_coprime_nat: "[(a::nat) = b] (mod m) \<Longrightarrow> coprime a m \<Longrightarrow> coprime b m"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   377
  by (auto simp add: cong_gcd_eq_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   378
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   379
lemma cong_imp_coprime_int: "[(a::int) = b] (mod m) \<Longrightarrow> coprime a m \<Longrightarrow> coprime b m"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   380
  by (auto simp add: cong_gcd_eq_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   381
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   382
lemma cong_cong_mod_nat: "[(a::nat) = b] (mod m) = [a mod m = b mod m] (mod m)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   383
  by (auto simp add: cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   384
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   385
lemma cong_cong_mod_int: "[(a::int) = b] (mod m) = [a mod m = b mod m] (mod m)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   386
  by (auto simp add: cong_int_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   387
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   388
lemma cong_minus_int [iff]: "[(a::int) = b] (mod -m) = [a = b] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   389
  apply (subst (1 2) cong_altdef_int)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   390
  apply auto
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   391
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   392
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   393
(*
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   394
lemma mod_dvd_mod_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   395
    "0 < (m::int) \<Longrightarrow> m dvd b \<Longrightarrow> (a mod b mod m) = (a mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   396
  apply (unfold dvd_def, auto)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   397
  apply (rule mod_mod_cancel)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   398
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   399
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   400
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   401
lemma mod_dvd_mod:
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   402
  assumes "0 < (m::nat)" and "m dvd b"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   403
  shows "(a mod b mod m) = (a mod m)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   404
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   405
  apply (rule mod_dvd_mod_int [transferred])
41541
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   406
  using assms apply auto
1fa4725c4656 eliminated global prems;
wenzelm
parents: 37293
diff changeset
   407
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   408
*)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   409
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   410
lemma cong_add_lcancel_nat:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   411
    "[(a::nat) + x = a + y] (mod n) \<longleftrightarrow> [x = y] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   412
  by (simp add: cong_iff_lin_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   413
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   414
lemma cong_add_lcancel_int:
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   415
    "[(a::int) + x = a + y] (mod n) \<longleftrightarrow> [x = y] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   416
  by (simp add: cong_iff_lin_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   417
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   418
lemma cong_add_rcancel_nat: "[(x::nat) + a = y + a] (mod n) \<longleftrightarrow> [x = y] (mod n)"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   419
  by (simp add: cong_iff_lin_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   420
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   421
lemma cong_add_rcancel_int: "[(x::int) + a = y + a] (mod n) \<longleftrightarrow> [x = y] (mod n)"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   422
  by (simp add: cong_iff_lin_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   423
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   424
lemma cong_add_lcancel_0_nat: "[(a::nat) + x = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   425
  by (simp add: cong_iff_lin_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   426
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   427
lemma cong_add_lcancel_0_int: "[(a::int) + x = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   428
  by (simp add: cong_iff_lin_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   429
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   430
lemma cong_add_rcancel_0_nat: "[x + (a::nat) = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   431
  by (simp add: cong_iff_lin_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   432
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   433
lemma cong_add_rcancel_0_int: "[x + (a::int) = a] (mod n) \<longleftrightarrow> [x = 0] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   434
  by (simp add: cong_iff_lin_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   435
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   436
lemma cong_dvd_modulus_nat: "[(x::nat) = y] (mod m) \<Longrightarrow> n dvd m \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   437
    [x = y] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   438
  apply (auto simp add: cong_iff_lin_nat dvd_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   439
  apply (rule_tac x="k1 * k" in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   440
  apply (rule_tac x="k2 * k" in exI)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   441
  apply (simp add: field_simps)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   442
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   443
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   444
lemma cong_dvd_modulus_int: "[(x::int) = y] (mod m) \<Longrightarrow> n dvd m \<Longrightarrow> [x = y] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   445
  by (auto simp add: cong_altdef_int dvd_def)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   446
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   447
lemma cong_dvd_eq_nat: "[(x::nat) = y] (mod n) \<Longrightarrow> n dvd x \<longleftrightarrow> n dvd y"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   448
  unfolding cong_nat_def by (auto simp add: dvd_eq_mod_eq_0)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   449
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   450
lemma cong_dvd_eq_int: "[(x::int) = y] (mod n) \<Longrightarrow> n dvd x \<longleftrightarrow> n dvd y"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   451
  unfolding cong_int_def by (auto simp add: dvd_eq_mod_eq_0)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   452
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   453
lemma cong_mod_nat: "(n::nat) ~= 0 \<Longrightarrow> [a mod n = a] (mod n)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   454
  by (simp add: cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   455
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   456
lemma cong_mod_int: "(n::int) ~= 0 \<Longrightarrow> [a mod n = a] (mod n)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   457
  by (simp add: cong_int_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   458
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   459
lemma mod_mult_cong_nat: "(a::nat) ~= 0 \<Longrightarrow> b ~= 0
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   460
    \<Longrightarrow> [x mod (a * b) = y] (mod a) \<longleftrightarrow> [x = y] (mod a)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   461
  by (simp add: cong_nat_def mod_mult2_eq  mod_add_left_eq)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   462
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   463
lemma neg_cong_int: "([(a::int) = b] (mod m)) = ([-a = -b] (mod m))"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   464
  apply (simp add: cong_altdef_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   465
  apply (subst dvd_minus_iff [symmetric])
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   466
  apply (simp add: field_simps)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   467
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   468
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   469
lemma cong_modulus_neg_int: "([(a::int) = b] (mod m)) = ([a = b] (mod -m))"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   470
  by (auto simp add: cong_altdef_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   471
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   472
lemma mod_mult_cong_int: "(a::int) ~= 0 \<Longrightarrow> b ~= 0
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   473
    \<Longrightarrow> [x mod (a * b) = y] (mod a) \<longleftrightarrow> [x = y] (mod a)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   474
  apply (cases "b > 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   475
  apply (simp add: cong_int_def mod_mod_cancel mod_add_left_eq)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   476
  apply (subst (1 2) cong_modulus_neg_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   477
  apply (unfold cong_int_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   478
  apply (subgoal_tac "a * b = (-a * -b)")
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   479
  apply (erule ssubst)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   480
  apply (subst zmod_zmult2_eq)
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 47163
diff changeset
   481
  apply (auto simp add: mod_add_left_eq mod_minus_right div_minus_right)
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 47163
diff changeset
   482
  apply (metis mod_diff_left_eq mod_diff_right_eq mod_mult_self1_is_0 semiring_numeral_div_class.diff_zero)+
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   483
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   484
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   485
lemma cong_to_1_nat: "([(a::nat) = 1] (mod n)) \<Longrightarrow> (n dvd (a - 1))"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   486
  apply (cases "a = 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   487
  apply force
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   488
  apply (subst (asm) cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   489
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   490
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   491
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   492
lemma cong_0_1_nat': "[(0::nat) = Suc 0] (mod n) = (n = Suc 0)"
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   493
  unfolding cong_nat_def by auto
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   494
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   495
lemma cong_0_1_nat: "[(0::nat) = 1] (mod n) = (n = 1)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   496
  unfolding cong_nat_def by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   497
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   498
lemma cong_0_1_int: "[(0::int) = 1] (mod n) = ((n = 1) | (n = -1))"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   499
  unfolding cong_int_def by (auto simp add: zmult_eq_1_iff)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   500
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   501
lemma cong_to_1'_nat: "[(a::nat) = 1] (mod n) \<longleftrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   502
    a = 0 \<and> n = 1 \<or> (\<exists>m. a = 1 + m * n)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   503
  apply (cases "n = 1")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   504
  apply auto [1]
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   505
  apply (drule_tac x = "a - 1" in spec)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   506
  apply force
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   507
  apply (cases "a = 0")
55130
70db8d380d62 Restored Suc rather than +1, and using Library/Binimial
paulson <lp15@cam.ac.uk>
parents: 54489
diff changeset
   508
  apply (auto simp add: cong_0_1_nat') [1]
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   509
  apply (rule iffI)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   510
  apply (drule cong_to_1_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   511
  apply (unfold dvd_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   512
  apply auto [1]
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   513
  apply (rule_tac x = k in exI)
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   514
  apply (auto simp add: field_simps) [1]
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   515
  apply (subst cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   516
  apply (auto simp add: dvd_def)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   517
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   518
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   519
lemma cong_le_nat: "(y::nat) <= x \<Longrightarrow> [x = y] (mod n) \<longleftrightarrow> (\<exists>q. x = q * n + y)"
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   520
  apply (subst cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   521
  apply assumption
36350
bc7982c54e37 dropped group_simps, ring_simps, field_eq_simps
haftmann
parents: 35644
diff changeset
   522
  apply (unfold dvd_def, auto simp add: field_simps)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   523
  apply (rule_tac x = k in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   524
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   525
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   526
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   527
lemma cong_solve_nat: "(a::nat) \<noteq> 0 \<Longrightarrow> EX x. [a * x = gcd a n] (mod n)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   528
  apply (cases "n = 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   529
  apply force
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   530
  apply (frule bezout_nat [of a n], auto)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   531
  apply (rule exI, erule ssubst)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   532
  apply (rule cong_trans_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   533
  apply (rule cong_add_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   534
  apply (subst mult_commute)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   535
  apply (rule cong_mult_self_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   536
  prefer 2
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   537
  apply simp
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   538
  apply (rule cong_refl_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   539
  apply (rule cong_refl_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   540
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   541
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   542
lemma cong_solve_int: "(a::int) \<noteq> 0 \<Longrightarrow> EX x. [a * x = gcd a n] (mod n)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   543
  apply (cases "n = 0")
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   544
  apply (cases "a \<ge> 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   545
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   546
  apply (rule_tac x = "-1" in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   547
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   548
  apply (insert bezout_int [of a n], auto)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   549
  apply (rule exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   550
  apply (erule subst)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   551
  apply (rule cong_trans_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   552
  prefer 2
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   553
  apply (rule cong_add_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   554
  apply (rule cong_refl_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   555
  apply (rule cong_sym_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   556
  apply (rule cong_mult_self_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   557
  apply simp
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   558
  apply (subst mult_commute)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   559
  apply (rule cong_refl_int)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   560
  done
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   561
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   562
lemma cong_solve_dvd_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   563
  assumes a: "(a::nat) \<noteq> 0" and b: "gcd a n dvd d"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   564
  shows "EX x. [a * x = d] (mod n)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   565
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   566
  from cong_solve_nat [OF a] obtain x where "[a * x = gcd a n](mod n)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   567
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   568
  then have "[(d div gcd a n) * (a * x) = (d div gcd a n) * gcd a n] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   569
    by (elim cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   570
  also from b have "(d div gcd a n) * gcd a n = d"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   571
    by (rule dvd_div_mult_self)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   572
  also have "(d div gcd a n) * (a * x) = a * (d div gcd a n * x)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   573
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   574
  finally show ?thesis
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   575
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   576
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   577
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   578
lemma cong_solve_dvd_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   579
  assumes a: "(a::int) \<noteq> 0" and b: "gcd a n dvd d"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   580
  shows "EX x. [a * x = d] (mod n)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   581
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   582
  from cong_solve_int [OF a] obtain x where "[a * x = gcd a n](mod n)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   583
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   584
  then have "[(d div gcd a n) * (a * x) = (d div gcd a n) * gcd a n] (mod n)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   585
    by (elim cong_scalar2_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   586
  also from b have "(d div gcd a n) * gcd a n = d"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   587
    by (rule dvd_div_mult_self)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   588
  also have "(d div gcd a n) * (a * x) = a * (d div gcd a n * x)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   589
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   590
  finally show ?thesis
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   591
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   592
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   593
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   594
lemma cong_solve_coprime_nat: "coprime (a::nat) n \<Longrightarrow> EX x. [a * x = 1] (mod n)"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   595
  apply (cases "a = 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   596
  apply force
55161
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   597
  apply (metis cong_solve_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   598
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   599
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   600
lemma cong_solve_coprime_int: "coprime (a::int) n \<Longrightarrow> EX x. [a * x = 1] (mod n)"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   601
  apply (cases "a = 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   602
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   603
  apply (cases "n \<ge> 0")
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   604
  apply auto
55161
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   605
  apply (metis cong_solve_int)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   606
  done
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   607
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   608
lemma coprime_iff_invertible_nat: "m > 0 \<Longrightarrow> coprime a m = (EX x. [a * x = Suc 0] (mod m))"
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   609
  apply (auto intro: cong_solve_coprime_nat)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   610
  apply (metis cong_Suc_0_nat cong_solve_nat gcd_nat.left_neutral)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   611
  apply (metis One_nat_def cong_gcd_eq_nat coprime_lmult_nat 
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   612
      gcd_lcm_complete_lattice_nat.inf_bot_right gcd_nat.commute)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   613
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   614
55161
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   615
lemma coprime_iff_invertible_int: "m > (0::int) \<Longrightarrow> coprime a m = (EX x. [a * x = 1] (mod m))"
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   616
  apply (auto intro: cong_solve_coprime_int)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   617
  apply (metis cong_int_def coprime_mul_eq_int gcd_1_int gcd_int.commute gcd_red_int)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   618
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   619
55161
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   620
lemma coprime_iff_invertible'_nat: "m > 0 \<Longrightarrow> coprime a m =
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   621
    (EX x. 0 \<le> x & x < m & [a * x = Suc 0] (mod m))"
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   622
  apply (subst coprime_iff_invertible_nat)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   623
  apply auto
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   624
  apply (auto simp add: cong_nat_def)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   625
  apply (rule_tac x = "x mod m" in exI)
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   626
  apply (metis mod_less_divisor mod_mult_right_eq)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   627
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   628
55161
8eb891539804 minor adjustments
paulson <lp15@cam.ac.uk>
parents: 55130
diff changeset
   629
lemma coprime_iff_invertible'_int: "m > (0::int) \<Longrightarrow> coprime a m =
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   630
    (EX x. 0 <= x & x < m & [a * x = 1] (mod m))"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   631
  apply (subst coprime_iff_invertible_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   632
  apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   633
  apply (auto simp add: cong_int_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   634
  apply (rule_tac x = "x mod m" in exI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   635
  apply (auto simp add: mod_mult_right_eq [symmetric])
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   636
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   637
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   638
lemma cong_cong_lcm_nat: "[(x::nat) = y] (mod a) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   639
    [x = y] (mod b) \<Longrightarrow> [x = y] (mod lcm a b)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   640
  apply (cases "y \<le> x")
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   641
  apply (auto simp add: cong_altdef_nat lcm_least_nat) [1]
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   642
  apply (rule cong_sym_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   643
  apply (subst (asm) (1 2) cong_sym_eq_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   644
  apply (auto simp add: cong_altdef_nat lcm_least_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   645
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   646
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   647
lemma cong_cong_lcm_int: "[(x::int) = y] (mod a) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   648
    [x = y] (mod b) \<Longrightarrow> [x = y] (mod lcm a b)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   649
  by (auto simp add: cong_altdef_int lcm_least_int) [1]
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   650
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   651
lemma cong_cong_coprime_nat: "coprime a b \<Longrightarrow> [(x::nat) = y] (mod a) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   652
    [x = y] (mod b) \<Longrightarrow> [x = y] (mod a * b)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   653
  apply (frule (1) cong_cong_lcm_nat)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   654
  back
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   655
  apply (simp add: lcm_nat_def)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   656
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   657
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   658
lemma cong_cong_coprime_int: "coprime a b \<Longrightarrow> [(x::int) = y] (mod a) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   659
    [x = y] (mod b) \<Longrightarrow> [x = y] (mod a * b)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   660
  apply (frule (1) cong_cong_lcm_int)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   661
  back
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   662
  apply (simp add: lcm_altdef_int cong_abs_int abs_mult [symmetric])
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   663
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   664
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   665
lemma cong_cong_setprod_coprime_nat [rule_format]: "finite A \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   666
    (ALL i:A. (ALL j:A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))) \<longrightarrow>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   667
    (ALL i:A. [(x::nat) = y] (mod m i)) \<longrightarrow>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   668
      [x = y] (mod (PROD i:A. m i))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   669
  apply (induct set: finite)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   670
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   671
  apply (rule cong_cong_coprime_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   672
  apply (subst gcd_commute_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   673
  apply (rule setprod_coprime_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   674
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   675
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   676
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   677
lemma cong_cong_setprod_coprime_int [rule_format]: "finite A \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   678
    (ALL i:A. (ALL j:A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))) \<longrightarrow>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   679
    (ALL i:A. [(x::int) = y] (mod m i)) \<longrightarrow>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   680
      [x = y] (mod (PROD i:A. m i))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   681
  apply (induct set: finite)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   682
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   683
  apply (rule cong_cong_coprime_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   684
  apply (subst gcd_commute_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   685
  apply (rule setprod_coprime_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   686
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   687
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   688
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   689
lemma binary_chinese_remainder_aux_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   690
  assumes a: "coprime (m1::nat) m2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   691
  shows "EX b1 b2. [b1 = 1] (mod m1) \<and> [b1 = 0] (mod m2) \<and>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   692
    [b2 = 0] (mod m1) \<and> [b2 = 1] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   693
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   694
  from cong_solve_coprime_nat [OF a] obtain x1 where one: "[m1 * x1 = 1] (mod m2)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   695
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   696
  from a have b: "coprime m2 m1"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   697
    by (subst gcd_commute_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   698
  from cong_solve_coprime_nat [OF b] obtain x2 where two: "[m2 * x2 = 1] (mod m1)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   699
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   700
  have "[m1 * x1 = 0] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   701
    by (subst mult_commute, rule cong_mult_self_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   702
  moreover have "[m2 * x2 = 0] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   703
    by (subst mult_commute, rule cong_mult_self_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   704
  moreover note one two
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   705
  ultimately show ?thesis by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   706
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   707
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   708
lemma binary_chinese_remainder_aux_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   709
  assumes a: "coprime (m1::int) m2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   710
  shows "EX b1 b2. [b1 = 1] (mod m1) \<and> [b1 = 0] (mod m2) \<and>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   711
    [b2 = 0] (mod m1) \<and> [b2 = 1] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   712
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   713
  from cong_solve_coprime_int [OF a] obtain x1 where one: "[m1 * x1 = 1] (mod m2)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   714
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   715
  from a have b: "coprime m2 m1"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   716
    by (subst gcd_commute_int)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   717
  from cong_solve_coprime_int [OF b] obtain x2 where two: "[m2 * x2 = 1] (mod m1)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   718
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   719
  have "[m1 * x1 = 0] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   720
    by (subst mult_commute, rule cong_mult_self_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   721
  moreover have "[m2 * x2 = 0] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   722
    by (subst mult_commute, rule cong_mult_self_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   723
  moreover note one two
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   724
  ultimately show ?thesis by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   725
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   726
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   727
lemma binary_chinese_remainder_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   728
  assumes a: "coprime (m1::nat) m2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   729
  shows "EX x. [x = u1] (mod m1) \<and> [x = u2] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   730
proof -
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   731
  from binary_chinese_remainder_aux_nat [OF a] obtain b1 b2
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   732
      where "[b1 = 1] (mod m1)" and "[b1 = 0] (mod m2)" and
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   733
            "[b2 = 0] (mod m1)" and "[b2 = 1] (mod m2)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   734
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   735
  let ?x = "u1 * b1 + u2 * b2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   736
  have "[?x = u1 * 1 + u2 * 0] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   737
    apply (rule cong_add_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   738
    apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   739
    apply (rule `[b1 = 1] (mod m1)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   740
    apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   741
    apply (rule `[b2 = 0] (mod m1)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   742
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   743
  then have "[?x = u1] (mod m1)" by simp
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   744
  have "[?x = u1 * 0 + u2 * 1] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   745
    apply (rule cong_add_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   746
    apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   747
    apply (rule `[b1 = 0] (mod m2)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   748
    apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   749
    apply (rule `[b2 = 1] (mod m2)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   750
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   751
  then have "[?x = u2] (mod m2)" by simp
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   752
  with `[?x = u1] (mod m1)` show ?thesis by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   753
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   754
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   755
lemma binary_chinese_remainder_int:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   756
  assumes a: "coprime (m1::int) m2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   757
  shows "EX x. [x = u1] (mod m1) \<and> [x = u2] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   758
proof -
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   759
  from binary_chinese_remainder_aux_int [OF a] obtain b1 b2
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   760
    where "[b1 = 1] (mod m1)" and "[b1 = 0] (mod m2)" and
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   761
          "[b2 = 0] (mod m1)" and "[b2 = 1] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   762
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   763
  let ?x = "u1 * b1 + u2 * b2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   764
  have "[?x = u1 * 1 + u2 * 0] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   765
    apply (rule cong_add_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   766
    apply (rule cong_scalar2_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   767
    apply (rule `[b1 = 1] (mod m1)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   768
    apply (rule cong_scalar2_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   769
    apply (rule `[b2 = 0] (mod m1)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   770
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   771
  then have "[?x = u1] (mod m1)" by simp
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   772
  have "[?x = u1 * 0 + u2 * 1] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   773
    apply (rule cong_add_int)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   774
    apply (rule cong_scalar2_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   775
    apply (rule `[b1 = 0] (mod m2)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   776
    apply (rule cong_scalar2_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   777
    apply (rule `[b2 = 1] (mod m2)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   778
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   779
  then have "[?x = u2] (mod m2)" by simp
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   780
  with `[?x = u1] (mod m1)` show ?thesis by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   781
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   782
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   783
lemma cong_modulus_mult_nat: "[(x::nat) = y] (mod m * n) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   784
    [x = y] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   785
  apply (cases "y \<le> x")
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   786
  apply (simp add: cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   787
  apply (erule dvd_mult_left)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   788
  apply (rule cong_sym_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   789
  apply (subst (asm) cong_sym_eq_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   790
  apply (simp add: cong_altdef_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   791
  apply (erule dvd_mult_left)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   792
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   793
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   794
lemma cong_modulus_mult_int: "[(x::int) = y] (mod m * n) \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   795
    [x = y] (mod m)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   796
  apply (simp add: cong_altdef_int)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   797
  apply (erule dvd_mult_left)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   798
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   799
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   800
lemma cong_less_modulus_unique_nat:
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   801
    "[(x::nat) = y] (mod m) \<Longrightarrow> x < m \<Longrightarrow> y < m \<Longrightarrow> x = y"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   802
  by (simp add: cong_nat_def)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   803
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   804
lemma binary_chinese_remainder_unique_nat:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   805
  assumes a: "coprime (m1::nat) m2"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   806
    and nz: "m1 \<noteq> 0" "m2 \<noteq> 0"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   807
  shows "EX! x. x < m1 * m2 \<and> [x = u1] (mod m1) \<and> [x = u2] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   808
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   809
  from binary_chinese_remainder_nat [OF a] obtain y where
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   810
      "[y = u1] (mod m1)" and "[y = u2] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   811
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   812
  let ?x = "y mod (m1 * m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   813
  from nz have less: "?x < m1 * m2"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   814
    by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   815
  have one: "[?x = u1] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   816
    apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   817
    prefer 2
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   818
    apply (rule `[y = u1] (mod m1)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   819
    apply (rule cong_modulus_mult_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   820
    apply (rule cong_mod_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   821
    using nz apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   822
    done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   823
  have two: "[?x = u2] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   824
    apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   825
    prefer 2
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   826
    apply (rule `[y = u2] (mod m2)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   827
    apply (subst mult_commute)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   828
    apply (rule cong_modulus_mult_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   829
    apply (rule cong_mod_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   830
    using nz apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   831
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   832
  have "ALL z. z < m1 * m2 \<and> [z = u1] (mod m1) \<and> [z = u2] (mod m2) \<longrightarrow> z = ?x"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   833
  proof clarify
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   834
    fix z
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   835
    assume "z < m1 * m2"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   836
    assume "[z = u1] (mod m1)" and  "[z = u2] (mod m2)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   837
    have "[?x = z] (mod m1)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   838
      apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   839
      apply (rule `[?x = u1] (mod m1)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   840
      apply (rule cong_sym_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   841
      apply (rule `[z = u1] (mod m1)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   842
      done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   843
    moreover have "[?x = z] (mod m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   844
      apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   845
      apply (rule `[?x = u2] (mod m2)`)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   846
      apply (rule cong_sym_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   847
      apply (rule `[z = u2] (mod m2)`)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   848
      done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   849
    ultimately have "[?x = z] (mod m1 * m2)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   850
      by (auto intro: coprime_cong_mult_nat a)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   851
    with `z < m1 * m2` `?x < m1 * m2` show "z = ?x"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   852
      apply (intro cong_less_modulus_unique_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   853
      apply (auto, erule cong_sym_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   854
      done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   855
  qed
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   856
  with less one two show ?thesis by auto
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   857
 qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   858
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   859
lemma chinese_remainder_aux_nat:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   860
  fixes A :: "'a set"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   861
    and m :: "'a \<Rightarrow> nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   862
  assumes fin: "finite A"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   863
    and cop: "ALL i : A. (ALL j : A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   864
  shows "EX b. (ALL i : A. [b i = 1] (mod m i) \<and> [b i = 0] (mod (PROD j : A - {i}. m j)))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   865
proof (rule finite_set_choice, rule fin, rule ballI)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   866
  fix i
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   867
  assume "i : A"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   868
  with cop have "coprime (PROD j : A - {i}. m j) (m i)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   869
    by (intro setprod_coprime_nat, auto)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   870
  then have "EX x. [(PROD j : A - {i}. m j) * x = 1] (mod m i)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   871
    by (elim cong_solve_coprime_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   872
  then obtain x where "[(PROD j : A - {i}. m j) * x = 1] (mod m i)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   873
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   874
  moreover have "[(PROD j : A - {i}. m j) * x = 0]
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   875
    (mod (PROD j : A - {i}. m j))"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   876
    by (subst mult_commute, rule cong_mult_self_nat)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   877
  ultimately show "\<exists>a. [a = 1] (mod m i) \<and> [a = 0]
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   878
      (mod setprod m (A - {i}))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   879
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   880
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   881
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   882
lemma chinese_remainder_nat:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   883
  fixes A :: "'a set"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   884
    and m :: "'a \<Rightarrow> nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   885
    and u :: "'a \<Rightarrow> nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   886
  assumes fin: "finite A"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   887
    and cop: "ALL i:A. (ALL j : A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   888
  shows "EX x. (ALL i:A. [x = u i] (mod m i))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   889
proof -
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   890
  from chinese_remainder_aux_nat [OF fin cop] obtain b where
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   891
    bprop: "ALL i:A. [b i = 1] (mod m i) \<and>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   892
      [b i = 0] (mod (PROD j : A - {i}. m j))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   893
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   894
  let ?x = "SUM i:A. (u i) * (b i)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   895
  show "?thesis"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   896
  proof (rule exI, clarify)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   897
    fix i
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   898
    assume a: "i : A"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   899
    show "[?x = u i] (mod m i)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   900
    proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   901
      from fin a have "?x = (SUM j:{i}. u j * b j) +
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   902
          (SUM j:A-{i}. u j * b j)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   903
        by (subst setsum_Un_disjoint [symmetric], auto intro: setsum_cong)
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   904
      then have "[?x = u i * b i + (SUM j:A-{i}. u j * b j)] (mod m i)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   905
        by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   906
      also have "[u i * b i + (SUM j:A-{i}. u j * b j) =
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   907
                  u i * 1 + (SUM j:A-{i}. u j * 0)] (mod m i)"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   908
        apply (rule cong_add_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   909
        apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   910
        using bprop a apply blast
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   911
        apply (rule cong_setsum_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   912
        apply (rule cong_scalar2_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   913
        using bprop apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   914
        apply (rule cong_dvd_modulus_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   915
        apply (drule (1) bspec)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   916
        apply (erule conjE)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   917
        apply assumption
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   918
        apply (rule dvd_setprod)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   919
        using fin a apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   920
        done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   921
      finally show ?thesis
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   922
        by simp
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   923
    qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   924
  qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   925
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   926
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   927
lemma coprime_cong_prod_nat [rule_format]: "finite A \<Longrightarrow>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   928
    (ALL i: A. (ALL j: A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))) \<longrightarrow>
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   929
      (ALL i: A. [(x::nat) = y] (mod m i)) \<longrightarrow>
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   930
         [x = y] (mod (PROD i:A. m i))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   931
  apply (induct set: finite)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   932
  apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   933
  apply (erule (1) coprime_cong_mult_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   934
  apply (subst gcd_commute_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   935
  apply (rule setprod_coprime_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   936
  apply auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   937
  done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   938
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   939
lemma chinese_remainder_unique_nat:
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   940
  fixes A :: "'a set"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   941
    and m :: "'a \<Rightarrow> nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   942
    and u :: "'a \<Rightarrow> nat"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   943
  assumes fin: "finite A"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   944
    and nz: "ALL i:A. m i \<noteq> 0"
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   945
    and cop: "ALL i:A. (ALL j : A. i \<noteq> j \<longrightarrow> coprime (m i) (m j))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   946
  shows "EX! x. x < (PROD i:A. m i) \<and> (ALL i:A. [x = u i] (mod m i))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   947
proof -
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   948
  from chinese_remainder_nat [OF fin cop]
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   949
  obtain y where one: "(ALL i:A. [y = u i] (mod m i))"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   950
    by blast
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   951
  let ?x = "y mod (PROD i:A. m i)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   952
  from fin nz have prodnz: "(PROD i:A. m i) \<noteq> 0"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   953
    by auto
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   954
  then have less: "?x < (PROD i:A. m i)"
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   955
    by auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   956
  have cong: "ALL i:A. [?x = u i] (mod m i)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   957
    apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   958
    apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   959
    prefer 2
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   960
    using one apply auto
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   961
    apply (rule cong_dvd_modulus_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   962
    apply (rule cong_mod_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   963
    using prodnz apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   964
    apply (rule dvd_setprod)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   965
    apply (rule fin)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   966
    apply assumption
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   967
    done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   968
  have unique: "ALL z. z < (PROD i:A. m i) \<and>
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   969
      (ALL i:A. [z = u i] (mod m i)) \<longrightarrow> z = ?x"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   970
  proof (clarify)
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   971
    fix z
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   972
    assume zless: "z < (PROD i:A. m i)"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   973
    assume zcong: "(ALL i:A. [z = u i] (mod m i))"
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   974
    have "ALL i:A. [?x = z] (mod m i)"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   975
      apply clarify
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   976
      apply (rule cong_trans_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   977
      using cong apply (erule bspec)
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   978
      apply (rule cong_sym_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   979
      using zcong apply auto
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   980
      done
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   981
    with fin cop have "[?x = z] (mod (PROD i:A. m i))"
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   982
      apply (intro coprime_cong_prod_nat)
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   983
      apply auto
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   984
      done
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   985
    with zless less show "z = ?x"
31952
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   986
      apply (intro cong_less_modulus_unique_nat)
40501bb2d57c renamed lemmas: nat_xyz/int_xyz -> xyz_nat/xyz_int
nipkow
parents: 31792
diff changeset
   987
      apply (auto, erule cong_sym_nat)
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   988
      done
44872
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   989
  qed
a98ef45122f3 misc tuning;
wenzelm
parents: 41959
diff changeset
   990
  from less cong unique show ?thesis by blast
31719
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   991
qed
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   992
29f5b20e8ee8 Added NewNumberTheory by Jeremy Avigad
nipkow
parents:
diff changeset
   993
end