src/HOL/Library/Primes.thy
author wenzelm
Thu, 06 May 2004 14:14:18 +0200
changeset 14706 71590b7733b7
parent 14353 79f9fbef9106
child 15131 c69542757a4d
permissions -rw-r--r--
tuned document;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
     1
(*  Title:      HOL/Library/Primes.thy
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     2
    ID:         $Id$
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     3
    Author:     Christophe Tabacznyj and Lawrence C Paulson
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     4
    Copyright   1996  University of Cambridge
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     5
*)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     6
14706
71590b7733b7 tuned document;
wenzelm
parents: 14353
diff changeset
     7
header {* The Greatest Common Divisor and Euclid's algorithm *}
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     8
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
     9
theory Primes = Main:
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    10
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    11
text {*
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    12
  See \cite{davenport92}.
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    13
  \bigskip
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    14
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    15
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    16
consts
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    17
  gcd  :: "nat \<times> nat => nat"  -- {* Euclid's algorithm *}
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    18
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    19
recdef gcd  "measure ((\<lambda>(m, n). n) :: nat \<times> nat => nat)"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    20
  "gcd (m, n) = (if n = 0 then m else gcd (n, m mod n))"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    21
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    22
constdefs
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    23
  is_gcd :: "nat => nat => nat => bool"  -- {* @{term gcd} as a relation *}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    24
  "is_gcd p m n == p dvd m \<and> p dvd n \<and>
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    25
    (\<forall>d. d dvd m \<and> d dvd n --> d dvd p)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    26
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    27
  coprime :: "nat => nat => bool"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    28
  "coprime m n == gcd (m, n) = 1"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    29
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    30
  prime :: "nat set"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    31
  "prime == {p. 1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p)}"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    32
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    33
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    34
lemma gcd_induct:
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    35
  "(!!m. P m 0) ==>
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    36
    (!!m n. 0 < n ==> P n (m mod n) ==> P m n)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    37
  ==> P (m::nat) (n::nat)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    38
  apply (induct m n rule: gcd.induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    39
  apply (case_tac "n = 0")
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    40
   apply simp_all
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    41
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    42
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    43
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    44
lemma gcd_0 [simp]: "gcd (m, 0) = m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    45
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    46
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    47
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    48
lemma gcd_non_0: "0 < n ==> gcd (m, n) = gcd (n, m mod n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    49
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    50
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    51
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    52
declare gcd.simps [simp del]
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    53
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
    54
lemma gcd_1 [simp]: "gcd (m, Suc 0) = 1"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    55
  apply (simp add: gcd_non_0)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    56
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    57
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    58
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    59
  \medskip @{term "gcd (m, n)"} divides @{text m} and @{text n}.  The
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    60
  conjunctions don't seem provable separately.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    61
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    62
12300
9fbce4042034 gcd_dvd1 and gcd_dvd2 proven simultaneously;
wenzelm
parents: 11701
diff changeset
    63
lemma gcd_dvd1 [iff]: "gcd (m, n) dvd m"
9fbce4042034 gcd_dvd1 and gcd_dvd2 proven simultaneously;
wenzelm
parents: 11701
diff changeset
    64
  and gcd_dvd2 [iff]: "gcd (m, n) dvd n"
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    65
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    66
   apply (simp_all add: gcd_non_0)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    67
  apply (blast dest: dvd_mod_imp_dvd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    68
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    69
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    70
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    71
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    72
  naturals, if @{term k} divides @{term m} and @{term k} divides
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    73
  @{term n} then @{term k} divides @{term "gcd (m, n)"}.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    74
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    75
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    76
lemma gcd_greatest: "k dvd m ==> k dvd n ==> k dvd gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    77
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    78
   apply (simp_all add: gcd_non_0 dvd_mod)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    79
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    80
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    81
lemma gcd_greatest_iff [iff]: "(k dvd gcd (m, n)) = (k dvd m \<and> k dvd n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    82
  apply (blast intro!: gcd_greatest intro: dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    83
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    84
11374
2badb9b2a8ec New proof of gcd_zero after a change to Divides.ML made the old one fail
paulson
parents: 11369
diff changeset
    85
lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)"
2badb9b2a8ec New proof of gcd_zero after a change to Divides.ML made the old one fail
paulson
parents: 11369
diff changeset
    86
  by (simp only: dvd_0_left_iff [THEN sym] gcd_greatest_iff)
2badb9b2a8ec New proof of gcd_zero after a change to Divides.ML made the old one fail
paulson
parents: 11369
diff changeset
    87
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    88
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    89
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    90
  \medskip Function gcd yields the Greatest Common Divisor.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    91
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    92
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    93
lemma is_gcd: "is_gcd (gcd (m, n)) m n"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    94
  apply (simp add: is_gcd_def gcd_greatest)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    95
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    96
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    97
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    98
  \medskip Uniqueness of GCDs.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    99
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   100
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   101
lemma is_gcd_unique: "is_gcd m a b ==> is_gcd n a b ==> m = n"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   102
  apply (simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   103
  apply (blast intro: dvd_anti_sym)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   104
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   105
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   106
lemma is_gcd_dvd: "is_gcd m a b ==> k dvd a ==> k dvd b ==> k dvd m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   107
  apply (auto simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   108
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   109
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   110
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   111
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   112
  \medskip Commutativity
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   113
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   114
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   115
lemma is_gcd_commute: "is_gcd k m n = is_gcd k n m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   116
  apply (auto simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   117
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   118
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   119
lemma gcd_commute: "gcd (m, n) = gcd (n, m)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   120
  apply (rule is_gcd_unique)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   121
   apply (rule is_gcd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   122
  apply (subst is_gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   123
  apply (simp add: is_gcd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   124
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   125
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   126
lemma gcd_assoc: "gcd (gcd (k, m), n) = gcd (k, gcd (m, n))"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   127
  apply (rule is_gcd_unique)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   128
   apply (rule is_gcd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   129
  apply (simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   130
  apply (blast intro: dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   131
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   132
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   133
lemma gcd_0_left [simp]: "gcd (0, m) = m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   134
  apply (simp add: gcd_commute [of 0])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   135
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   136
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   137
lemma gcd_1_left [simp]: "gcd (Suc 0, m) = 1"
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   138
  apply (simp add: gcd_commute [of "Suc 0"])
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   139
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   140
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   141
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   142
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   143
  \medskip Multiplication laws
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   144
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   145
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   146
lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)"
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   147
    -- {* \cite[page 27]{davenport92} *}
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   148
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   149
   apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   150
  apply (case_tac "k = 0")
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   151
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   152
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   153
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   154
lemma gcd_mult [simp]: "gcd (k, k * n) = k"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   155
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   156
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   157
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   158
lemma gcd_self [simp]: "gcd (k, k) = k"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   159
  apply (rule gcd_mult [of k 1, simplified])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   160
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   161
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   162
lemma relprime_dvd_mult: "gcd (k, n) = 1 ==> k dvd m * n ==> k dvd m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   163
  apply (insert gcd_mult_distrib2 [of m k n])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   164
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   165
  apply (erule_tac t = m in ssubst)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   166
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   167
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   168
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   169
lemma relprime_dvd_mult_iff: "gcd (k, n) = 1 ==> (k dvd m * n) = (k dvd m)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   170
  apply (blast intro: relprime_dvd_mult dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   171
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   172
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   173
lemma prime_imp_relprime: "p \<in> prime ==> \<not> p dvd n ==> gcd (p, n) = 1"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   174
  apply (auto simp add: prime_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   175
  apply (drule_tac x = "gcd (p, n)" in spec)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   176
  apply auto
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   177
  apply (insert gcd_dvd2 [of p n])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   178
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   179
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   180
13032
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   181
lemma two_is_prime: "2 \<in> prime"
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   182
  apply (auto simp add: prime_def)
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   183
  apply (case_tac m)
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   184
   apply (auto dest!: dvd_imp_le)
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   185
  done
1ec445c51931 added two_is_prime;
wenzelm
parents: 12739
diff changeset
   186
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   187
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   188
  This theorem leads immediately to a proof of the uniqueness of
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   189
  factorization.  If @{term p} divides a product of primes then it is
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   190
  one of those primes.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   191
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   192
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   193
lemma prime_dvd_mult: "p \<in> prime ==> p dvd m * n ==> p dvd m \<or> p dvd n"
12739
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
   194
  by (blast intro: relprime_dvd_mult prime_imp_relprime)
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   195
11701
3d51fbf81c17 sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents: 11464
diff changeset
   196
lemma prime_dvd_square: "p \<in> prime ==> p dvd m^Suc (Suc 0) ==> p dvd m"
12739
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
   197
  by (auto dest: prime_dvd_mult)
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
   198
1fce8f51034d prime_dvd_power_two;
wenzelm
parents: 12300
diff changeset
   199
lemma prime_dvd_power_two: "p \<in> prime ==> p dvd m\<twosuperior> ==> p dvd m"
14353
79f9fbef9106 Added lemmas to Ring_and_Field with slightly modified simplification rules
paulson
parents: 13187
diff changeset
   200
  by (rule prime_dvd_square) (simp_all add: power2_eq_square)
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   201
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   202
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   203
text {* \medskip Addition laws *}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   204
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   205
lemma gcd_add1 [simp]: "gcd (m + n, n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   206
  apply (case_tac "n = 0")
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   207
   apply (simp_all add: gcd_non_0)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   208
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   209
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   210
lemma gcd_add2 [simp]: "gcd (m, m + n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   211
  apply (rule gcd_commute [THEN trans])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   212
  apply (subst add_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   213
  apply (simp add: gcd_add1)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   214
  apply (rule gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   215
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   216
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   217
lemma gcd_add2' [simp]: "gcd (m, n + m) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   218
  apply (subst add_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   219
  apply (rule gcd_add2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   220
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   221
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   222
lemma gcd_add_mult: "gcd (m, k * m + n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   223
  apply (induct k)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   224
   apply (simp_all add: gcd_add2 add_assoc)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   225
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   226
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   227
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   228
text {* \medskip More multiplication laws *}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   229
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   230
lemma gcd_mult_cancel: "gcd (k, n) = 1 ==> gcd (k * m, n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   231
  apply (rule dvd_anti_sym)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   232
   apply (rule gcd_greatest)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   233
    apply (rule_tac n = k in relprime_dvd_mult)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   234
     apply (simp add: gcd_assoc)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   235
     apply (simp add: gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   236
    apply (simp_all add: mult_commute gcd_dvd1 gcd_dvd2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   237
  apply (blast intro: gcd_dvd1 dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   238
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   239
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   240
end