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