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