src/HOL/Library/GCD.thy
author wenzelm
Wed Nov 08 23:11:13 2006 +0100 (2006-11-08)
changeset 21256 47195501ecf7
child 21263 de65ce2bfb32
permissions -rw-r--r--
moved theories Parity, GCD, Binomial to Library;
wenzelm@21256
     1
(*  Title:      HOL/GCD.thy
wenzelm@21256
     2
    ID:         $Id$
wenzelm@21256
     3
    Author:     Christophe Tabacznyj and Lawrence C Paulson
wenzelm@21256
     4
    Copyright   1996  University of Cambridge
wenzelm@21256
     5
*)
wenzelm@21256
     6
wenzelm@21256
     7
header {* The Greatest Common Divisor *}
wenzelm@21256
     8
wenzelm@21256
     9
theory GCD
wenzelm@21256
    10
imports Main
wenzelm@21256
    11
begin
wenzelm@21256
    12
wenzelm@21256
    13
text {*
wenzelm@21256
    14
  See \cite{davenport92}.
wenzelm@21256
    15
  \bigskip
wenzelm@21256
    16
*}
wenzelm@21256
    17
wenzelm@21256
    18
consts
wenzelm@21256
    19
  gcd  :: "nat \<times> nat => nat"  -- {* Euclid's algorithm *}
wenzelm@21256
    20
wenzelm@21256
    21
recdef gcd  "measure ((\<lambda>(m, n). n) :: nat \<times> nat => nat)"
wenzelm@21256
    22
  "gcd (m, n) = (if n = 0 then m else gcd (n, m mod n))"
wenzelm@21256
    23
wenzelm@21256
    24
constdefs
wenzelm@21256
    25
  is_gcd :: "nat => nat => nat => bool"  -- {* @{term gcd} as a relation *}
wenzelm@21256
    26
  "is_gcd p m n == p dvd m \<and> p dvd n \<and>
wenzelm@21256
    27
    (\<forall>d. d dvd m \<and> d dvd n --> d dvd p)"
wenzelm@21256
    28
wenzelm@21256
    29
wenzelm@21256
    30
lemma gcd_induct:
wenzelm@21256
    31
  "(!!m. P m 0) ==>
wenzelm@21256
    32
    (!!m n. 0 < n ==> P n (m mod n) ==> P m n)
wenzelm@21256
    33
  ==> P (m::nat) (n::nat)"
wenzelm@21256
    34
  apply (induct m n rule: gcd.induct)
wenzelm@21256
    35
  apply (case_tac "n = 0")
wenzelm@21256
    36
   apply simp_all
wenzelm@21256
    37
  done
wenzelm@21256
    38
wenzelm@21256
    39
wenzelm@21256
    40
lemma gcd_0 [simp]: "gcd (m, 0) = m"
wenzelm@21256
    41
  apply simp
wenzelm@21256
    42
  done
wenzelm@21256
    43
wenzelm@21256
    44
lemma gcd_non_0: "0 < n ==> gcd (m, n) = gcd (n, m mod n)"
wenzelm@21256
    45
  apply simp
wenzelm@21256
    46
  done
wenzelm@21256
    47
wenzelm@21256
    48
declare gcd.simps [simp del]
wenzelm@21256
    49
wenzelm@21256
    50
lemma gcd_1 [simp]: "gcd (m, Suc 0) = 1"
wenzelm@21256
    51
  apply (simp add: gcd_non_0)
wenzelm@21256
    52
  done
wenzelm@21256
    53
wenzelm@21256
    54
text {*
wenzelm@21256
    55
  \medskip @{term "gcd (m, n)"} divides @{text m} and @{text n}.  The
wenzelm@21256
    56
  conjunctions don't seem provable separately.
wenzelm@21256
    57
*}
wenzelm@21256
    58
wenzelm@21256
    59
lemma gcd_dvd1 [iff]: "gcd (m, n) dvd m"
wenzelm@21256
    60
  and gcd_dvd2 [iff]: "gcd (m, n) dvd n"
wenzelm@21256
    61
  apply (induct m n rule: gcd_induct)
wenzelm@21256
    62
   apply (simp_all add: gcd_non_0)
wenzelm@21256
    63
  apply (blast dest: dvd_mod_imp_dvd)
wenzelm@21256
    64
  done
wenzelm@21256
    65
wenzelm@21256
    66
text {*
wenzelm@21256
    67
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
wenzelm@21256
    68
  naturals, if @{term k} divides @{term m} and @{term k} divides
wenzelm@21256
    69
  @{term n} then @{term k} divides @{term "gcd (m, n)"}.
wenzelm@21256
    70
*}
wenzelm@21256
    71
wenzelm@21256
    72
lemma gcd_greatest: "k dvd m ==> k dvd n ==> k dvd gcd (m, n)"
wenzelm@21256
    73
  apply (induct m n rule: gcd_induct)
wenzelm@21256
    74
   apply (simp_all add: gcd_non_0 dvd_mod)
wenzelm@21256
    75
  done
wenzelm@21256
    76
wenzelm@21256
    77
lemma gcd_greatest_iff [iff]: "(k dvd gcd (m, n)) = (k dvd m \<and> k dvd n)"
wenzelm@21256
    78
  apply (blast intro!: gcd_greatest intro: dvd_trans)
wenzelm@21256
    79
  done
wenzelm@21256
    80
wenzelm@21256
    81
lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)"
wenzelm@21256
    82
  by (simp only: dvd_0_left_iff [THEN sym] gcd_greatest_iff)
wenzelm@21256
    83
wenzelm@21256
    84
wenzelm@21256
    85
text {*
wenzelm@21256
    86
  \medskip Function gcd yields the Greatest Common Divisor.
wenzelm@21256
    87
*}
wenzelm@21256
    88
wenzelm@21256
    89
lemma is_gcd: "is_gcd (gcd (m, n)) m n"
wenzelm@21256
    90
  apply (simp add: is_gcd_def gcd_greatest)
wenzelm@21256
    91
  done
wenzelm@21256
    92
wenzelm@21256
    93
text {*
wenzelm@21256
    94
  \medskip Uniqueness of GCDs.
wenzelm@21256
    95
*}
wenzelm@21256
    96
wenzelm@21256
    97
lemma is_gcd_unique: "is_gcd m a b ==> is_gcd n a b ==> m = n"
wenzelm@21256
    98
  apply (simp add: is_gcd_def)
wenzelm@21256
    99
  apply (blast intro: dvd_anti_sym)
wenzelm@21256
   100
  done
wenzelm@21256
   101
wenzelm@21256
   102
lemma is_gcd_dvd: "is_gcd m a b ==> k dvd a ==> k dvd b ==> k dvd m"
wenzelm@21256
   103
  apply (auto simp add: is_gcd_def)
wenzelm@21256
   104
  done
wenzelm@21256
   105
wenzelm@21256
   106
wenzelm@21256
   107
text {*
wenzelm@21256
   108
  \medskip Commutativity
wenzelm@21256
   109
*}
wenzelm@21256
   110
wenzelm@21256
   111
lemma is_gcd_commute: "is_gcd k m n = is_gcd k n m"
wenzelm@21256
   112
  apply (auto simp add: is_gcd_def)
wenzelm@21256
   113
  done
wenzelm@21256
   114
wenzelm@21256
   115
lemma gcd_commute: "gcd (m, n) = gcd (n, m)"
wenzelm@21256
   116
  apply (rule is_gcd_unique)
wenzelm@21256
   117
   apply (rule is_gcd)
wenzelm@21256
   118
  apply (subst is_gcd_commute)
wenzelm@21256
   119
  apply (simp add: is_gcd)
wenzelm@21256
   120
  done
wenzelm@21256
   121
wenzelm@21256
   122
lemma gcd_assoc: "gcd (gcd (k, m), n) = gcd (k, gcd (m, n))"
wenzelm@21256
   123
  apply (rule is_gcd_unique)
wenzelm@21256
   124
   apply (rule is_gcd)
wenzelm@21256
   125
  apply (simp add: is_gcd_def)
wenzelm@21256
   126
  apply (blast intro: dvd_trans)
wenzelm@21256
   127
  done
wenzelm@21256
   128
wenzelm@21256
   129
lemma gcd_0_left [simp]: "gcd (0, m) = m"
wenzelm@21256
   130
  apply (simp add: gcd_commute [of 0])
wenzelm@21256
   131
  done
wenzelm@21256
   132
wenzelm@21256
   133
lemma gcd_1_left [simp]: "gcd (Suc 0, m) = 1"
wenzelm@21256
   134
  apply (simp add: gcd_commute [of "Suc 0"])
wenzelm@21256
   135
  done
wenzelm@21256
   136
wenzelm@21256
   137
wenzelm@21256
   138
text {*
wenzelm@21256
   139
  \medskip Multiplication laws
wenzelm@21256
   140
*}
wenzelm@21256
   141
wenzelm@21256
   142
lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)"
wenzelm@21256
   143
    -- {* \cite[page 27]{davenport92} *}
wenzelm@21256
   144
  apply (induct m n rule: gcd_induct)
wenzelm@21256
   145
   apply simp
wenzelm@21256
   146
  apply (case_tac "k = 0")
wenzelm@21256
   147
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
wenzelm@21256
   148
  done
wenzelm@21256
   149
wenzelm@21256
   150
lemma gcd_mult [simp]: "gcd (k, k * n) = k"
wenzelm@21256
   151
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
wenzelm@21256
   152
  done
wenzelm@21256
   153
wenzelm@21256
   154
lemma gcd_self [simp]: "gcd (k, k) = k"
wenzelm@21256
   155
  apply (rule gcd_mult [of k 1, simplified])
wenzelm@21256
   156
  done
wenzelm@21256
   157
wenzelm@21256
   158
lemma relprime_dvd_mult: "gcd (k, n) = 1 ==> k dvd m * n ==> k dvd m"
wenzelm@21256
   159
  apply (insert gcd_mult_distrib2 [of m k n])
wenzelm@21256
   160
  apply simp
wenzelm@21256
   161
  apply (erule_tac t = m in ssubst)
wenzelm@21256
   162
  apply simp
wenzelm@21256
   163
  done
wenzelm@21256
   164
wenzelm@21256
   165
lemma relprime_dvd_mult_iff: "gcd (k, n) = 1 ==> (k dvd m * n) = (k dvd m)"
wenzelm@21256
   166
  apply (blast intro: relprime_dvd_mult dvd_trans)
wenzelm@21256
   167
  done
wenzelm@21256
   168
wenzelm@21256
   169
lemma gcd_mult_cancel: "gcd (k, n) = 1 ==> gcd (k * m, n) = gcd (m, n)"
wenzelm@21256
   170
  apply (rule dvd_anti_sym)
wenzelm@21256
   171
   apply (rule gcd_greatest)
wenzelm@21256
   172
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   173
     apply (simp add: gcd_assoc)
wenzelm@21256
   174
     apply (simp add: gcd_commute)
wenzelm@21256
   175
    apply (simp_all add: mult_commute)
wenzelm@21256
   176
  apply (blast intro: dvd_trans)
wenzelm@21256
   177
  done
wenzelm@21256
   178
wenzelm@21256
   179
wenzelm@21256
   180
text {* \medskip Addition laws *}
wenzelm@21256
   181
wenzelm@21256
   182
lemma gcd_add1 [simp]: "gcd (m + n, n) = gcd (m, n)"
wenzelm@21256
   183
  apply (case_tac "n = 0")
wenzelm@21256
   184
   apply (simp_all add: gcd_non_0)
wenzelm@21256
   185
  done
wenzelm@21256
   186
wenzelm@21256
   187
lemma gcd_add2 [simp]: "gcd (m, m + n) = gcd (m, n)"
wenzelm@21256
   188
proof -
wenzelm@21256
   189
  have "gcd (m, m + n) = gcd (m + n, m)" by (rule gcd_commute) 
wenzelm@21256
   190
  also have "... = gcd (n + m, m)" by (simp add: add_commute)
wenzelm@21256
   191
  also have "... = gcd (n, m)" by simp
wenzelm@21256
   192
  also have  "... = gcd (m, n)" by (rule gcd_commute) 
wenzelm@21256
   193
  finally show ?thesis .
wenzelm@21256
   194
qed
wenzelm@21256
   195
wenzelm@21256
   196
lemma gcd_add2' [simp]: "gcd (m, n + m) = gcd (m, n)"
wenzelm@21256
   197
  apply (subst add_commute)
wenzelm@21256
   198
  apply (rule gcd_add2)
wenzelm@21256
   199
  done
wenzelm@21256
   200
wenzelm@21256
   201
lemma gcd_add_mult: "gcd (m, k * m + n) = gcd (m, n)"
wenzelm@21256
   202
  apply (induct k)
wenzelm@21256
   203
   apply (simp_all add: add_assoc)
wenzelm@21256
   204
  done
wenzelm@21256
   205
wenzelm@21256
   206
end