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