src/HOL/Library/Primes.thy
author wenzelm
Sat, 09 Jun 2001 14:18:19 +0200
changeset 11368 9c1995c73383
parent 11363 a548865b1b6a
child 11369 2c4bb701546a
permissions -rw-r--r--
tuned Primes theory;
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
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
     7
header {*
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
     8
  \title{The Greatest Common Divisor and Euclid's algorithm}
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
     9
  \author{Christophe Tabacznyj and Lawrence C Paulson} *}
11363
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
theory Primes = Main:
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    56
lemma gcd_1 [simp]: "gcd (m, 1) = 1"
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    65
lemma gcd_dvd_both: "gcd (m, n) dvd m \<and> gcd (m, n) dvd n"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    66
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    67
   apply (simp_all add: gcd_non_0)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    68
  apply (blast dest: dvd_mod_imp_dvd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    69
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    70
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    71
lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1, standard]
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    72
lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2, standard]
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    73
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    74
lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)"
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    75
proof
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    76
  have "gcd (m, n) dvd m \<and> gcd (m, n) dvd n" by simp
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    77
  also assume "gcd (m, n) = 0"
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    78
  finally have "0 dvd m \<and> 0 dvd n" .
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    79
  thus "m = 0 \<and> n = 0" by (simp add: dvd_0_left)
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    80
next
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    81
  assume "m = 0 \<and> n = 0"
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    82
  thus "gcd (m, n) = 0" by simp
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    83
qed
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
    84
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    85
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    86
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    87
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    88
  naturals, if @{term k} divides @{term m} and @{term k} divides
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    89
  @{term n} then @{term k} divides @{term "gcd (m, n)"}.
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    92
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
    93
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    94
   apply (simp_all add: gcd_non_0 dvd_mod)
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
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
    98
  apply (blast intro!: gcd_greatest intro: dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
    99
  done
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   102
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   103
  \medskip Function gcd yields the Greatest Common Divisor.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   104
*}
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: "is_gcd (gcd (m, n)) m n"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   107
  apply (simp add: is_gcd_def gcd_greatest)
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
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   111
  \medskip Uniqueness of GCDs.
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   114
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
   115
  apply (simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   116
  apply (blast intro: dvd_anti_sym)
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 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
   120
  apply (auto simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   121
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   122
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   123
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   124
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   125
  \medskip Commutativity
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   126
*}
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 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
   129
  apply (auto simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   130
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   131
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   132
lemma gcd_commute: "gcd (m, n) = gcd (n, m)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   133
  apply (rule is_gcd_unique)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   134
   apply (rule is_gcd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   135
  apply (subst is_gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   136
  apply (simp add: is_gcd)
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   139
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
   140
  apply (rule is_gcd_unique)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   141
   apply (rule is_gcd)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   142
  apply (simp add: is_gcd_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   143
  apply (blast intro: dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   144
  done
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_0_left [simp]: "gcd (0, m) = m"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   147
  apply (simp add: gcd_commute [of 0])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   148
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   149
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   150
lemma gcd_1_left [simp]: "gcd (1, m) = 1"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   151
  apply (simp add: gcd_commute [of 1])
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   155
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   156
  \medskip Multiplication laws
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
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   159
lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)"
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   160
    -- {* \cite[page 27]{davenport92} *}
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   161
  apply (induct m n rule: gcd_induct)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   162
   apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   163
  apply (case_tac "k = 0")
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   164
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   165
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   166
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   167
lemma gcd_mult [simp]: "gcd (k, k * n) = k"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   168
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
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 gcd_self [simp]: "gcd (k, k) = k"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   172
  apply (rule gcd_mult [of k 1, simplified])
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 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
   176
  apply (insert gcd_mult_distrib2 [of m k n])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   177
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   178
  apply (erule_tac t = m in ssubst)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   179
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   180
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   181
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   182
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
   183
  apply (blast intro: relprime_dvd_mult dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   184
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   185
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   186
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
   187
  apply (auto simp add: prime_def)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   188
  apply (drule_tac x = "gcd (p, n)" in spec)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   189
  apply auto
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   190
  apply (insert gcd_dvd2 [of p n])
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   191
  apply simp
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   192
  done
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
text {*
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   195
  This theorem leads immediately to a proof of the uniqueness of
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   196
  factorization.  If @{term p} divides a product of primes then it is
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   197
  one of those primes.
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   198
*}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   199
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   200
lemma prime_dvd_mult: "p \<in> prime ==> p dvd m * n ==> p dvd m \<or> p dvd n"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   201
  apply (blast intro: relprime_dvd_mult prime_imp_relprime)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   202
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   203
11368
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   204
lemma prime_dvd_square: "p \<in> prime ==> p dvd m^2 ==> p dvd m"
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   205
  apply (auto dest: prime_dvd_mult)
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   206
  done
9c1995c73383 tuned Primes theory;
wenzelm
parents: 11363
diff changeset
   207
11363
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   208
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   209
text {* \medskip Addition laws *}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   210
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   211
lemma gcd_add1 [simp]: "gcd (m + n, n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   212
  apply (case_tac "n = 0")
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   213
   apply (simp_all add: gcd_non_0)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   214
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   215
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   216
lemma gcd_add2 [simp]: "gcd (m, m + n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   217
  apply (rule gcd_commute [THEN trans])
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 (simp add: gcd_add1)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   220
  apply (rule gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   221
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   222
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   223
lemma gcd_add2' [simp]: "gcd (m, n + m) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   224
  apply (subst add_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   225
  apply (rule gcd_add2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   226
  done
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
lemma gcd_add_mult: "gcd (m, k * m + n) = gcd (m, n)"
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   229
  apply (induct k)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   230
   apply (simp_all add: gcd_add2 add_assoc)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   231
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   232
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
text {* \medskip More multiplication laws *}
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   235
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   236
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
   237
  apply (rule dvd_anti_sym)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   238
   apply (rule gcd_greatest)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   239
    apply (rule_tac n = k in relprime_dvd_mult)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   240
     apply (simp add: gcd_assoc)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   241
     apply (simp add: gcd_commute)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   242
    apply (simp_all add: mult_commute gcd_dvd1 gcd_dvd2)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   243
  apply (blast intro: gcd_dvd1 dvd_trans)
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   244
  done
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   245
a548865b1b6a moved Primes.thy from NumberTheory to Library
paulson
parents:
diff changeset
   246
end