src/HOL/Library/GCD.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 21263 de65ce2bfb32
child 22027 e4a08629c4bd
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
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@21263
    24
definition
wenzelm@21404
    25
  is_gcd :: "nat => nat => nat => bool" where -- {* @{term gcd} as a relation *}
wenzelm@21263
    26
  "is_gcd p m n = (p dvd m \<and> p dvd n \<and>
wenzelm@21263
    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@21263
    41
  by simp
wenzelm@21256
    42
wenzelm@21256
    43
lemma gcd_non_0: "0 < n ==> gcd (m, n) = gcd (n, m mod n)"
wenzelm@21263
    44
  by simp
wenzelm@21256
    45
wenzelm@21256
    46
declare gcd.simps [simp del]
wenzelm@21256
    47
wenzelm@21256
    48
lemma gcd_1 [simp]: "gcd (m, Suc 0) = 1"
wenzelm@21263
    49
  by (simp add: gcd_non_0)
wenzelm@21256
    50
wenzelm@21256
    51
text {*
wenzelm@21256
    52
  \medskip @{term "gcd (m, n)"} divides @{text m} and @{text n}.  The
wenzelm@21256
    53
  conjunctions don't seem provable separately.
wenzelm@21256
    54
*}
wenzelm@21256
    55
wenzelm@21256
    56
lemma gcd_dvd1 [iff]: "gcd (m, n) dvd m"
wenzelm@21256
    57
  and gcd_dvd2 [iff]: "gcd (m, n) dvd n"
wenzelm@21256
    58
  apply (induct m n rule: gcd_induct)
wenzelm@21263
    59
     apply (simp_all add: gcd_non_0)
wenzelm@21256
    60
  apply (blast dest: dvd_mod_imp_dvd)
wenzelm@21256
    61
  done
wenzelm@21256
    62
wenzelm@21256
    63
text {*
wenzelm@21256
    64
  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
wenzelm@21256
    65
  naturals, if @{term k} divides @{term m} and @{term k} divides
wenzelm@21256
    66
  @{term n} then @{term k} divides @{term "gcd (m, n)"}.
wenzelm@21256
    67
*}
wenzelm@21256
    68
wenzelm@21256
    69
lemma gcd_greatest: "k dvd m ==> k dvd n ==> k dvd gcd (m, n)"
wenzelm@21263
    70
  by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
wenzelm@21256
    71
wenzelm@21256
    72
lemma gcd_greatest_iff [iff]: "(k dvd gcd (m, n)) = (k dvd m \<and> k dvd n)"
wenzelm@21263
    73
  by (blast intro!: gcd_greatest intro: dvd_trans)
wenzelm@21256
    74
wenzelm@21256
    75
lemma gcd_zero: "(gcd (m, n) = 0) = (m = 0 \<and> n = 0)"
wenzelm@21263
    76
  by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
wenzelm@21256
    77
wenzelm@21256
    78
wenzelm@21256
    79
text {*
wenzelm@21256
    80
  \medskip Function gcd yields the Greatest Common Divisor.
wenzelm@21256
    81
*}
wenzelm@21256
    82
wenzelm@21256
    83
lemma is_gcd: "is_gcd (gcd (m, n)) m n"
wenzelm@21256
    84
  apply (simp add: is_gcd_def gcd_greatest)
wenzelm@21256
    85
  done
wenzelm@21256
    86
wenzelm@21256
    87
text {*
wenzelm@21256
    88
  \medskip Uniqueness of GCDs.
wenzelm@21256
    89
*}
wenzelm@21256
    90
wenzelm@21256
    91
lemma is_gcd_unique: "is_gcd m a b ==> is_gcd n a b ==> m = n"
wenzelm@21256
    92
  apply (simp add: is_gcd_def)
wenzelm@21256
    93
  apply (blast intro: dvd_anti_sym)
wenzelm@21256
    94
  done
wenzelm@21256
    95
wenzelm@21256
    96
lemma is_gcd_dvd: "is_gcd m a b ==> k dvd a ==> k dvd b ==> k dvd m"
wenzelm@21256
    97
  apply (auto simp add: is_gcd_def)
wenzelm@21256
    98
  done
wenzelm@21256
    99
wenzelm@21256
   100
wenzelm@21256
   101
text {*
wenzelm@21256
   102
  \medskip Commutativity
wenzelm@21256
   103
*}
wenzelm@21256
   104
wenzelm@21256
   105
lemma is_gcd_commute: "is_gcd k m n = is_gcd k n m"
wenzelm@21256
   106
  apply (auto simp add: is_gcd_def)
wenzelm@21256
   107
  done
wenzelm@21256
   108
wenzelm@21256
   109
lemma gcd_commute: "gcd (m, n) = gcd (n, m)"
wenzelm@21256
   110
  apply (rule is_gcd_unique)
wenzelm@21256
   111
   apply (rule is_gcd)
wenzelm@21256
   112
  apply (subst is_gcd_commute)
wenzelm@21256
   113
  apply (simp add: is_gcd)
wenzelm@21256
   114
  done
wenzelm@21256
   115
wenzelm@21256
   116
lemma gcd_assoc: "gcd (gcd (k, m), n) = gcd (k, gcd (m, n))"
wenzelm@21256
   117
  apply (rule is_gcd_unique)
wenzelm@21256
   118
   apply (rule is_gcd)
wenzelm@21256
   119
  apply (simp add: is_gcd_def)
wenzelm@21256
   120
  apply (blast intro: dvd_trans)
wenzelm@21256
   121
  done
wenzelm@21256
   122
wenzelm@21256
   123
lemma gcd_0_left [simp]: "gcd (0, m) = m"
wenzelm@21256
   124
  apply (simp add: gcd_commute [of 0])
wenzelm@21256
   125
  done
wenzelm@21256
   126
wenzelm@21256
   127
lemma gcd_1_left [simp]: "gcd (Suc 0, m) = 1"
wenzelm@21256
   128
  apply (simp add: gcd_commute [of "Suc 0"])
wenzelm@21256
   129
  done
wenzelm@21256
   130
wenzelm@21256
   131
wenzelm@21256
   132
text {*
wenzelm@21256
   133
  \medskip Multiplication laws
wenzelm@21256
   134
*}
wenzelm@21256
   135
wenzelm@21256
   136
lemma gcd_mult_distrib2: "k * gcd (m, n) = gcd (k * m, k * n)"
wenzelm@21256
   137
    -- {* \cite[page 27]{davenport92} *}
wenzelm@21256
   138
  apply (induct m n rule: gcd_induct)
wenzelm@21256
   139
   apply simp
wenzelm@21256
   140
  apply (case_tac "k = 0")
wenzelm@21256
   141
   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
wenzelm@21256
   142
  done
wenzelm@21256
   143
wenzelm@21256
   144
lemma gcd_mult [simp]: "gcd (k, k * n) = k"
wenzelm@21256
   145
  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
wenzelm@21256
   146
  done
wenzelm@21256
   147
wenzelm@21256
   148
lemma gcd_self [simp]: "gcd (k, k) = k"
wenzelm@21256
   149
  apply (rule gcd_mult [of k 1, simplified])
wenzelm@21256
   150
  done
wenzelm@21256
   151
wenzelm@21256
   152
lemma relprime_dvd_mult: "gcd (k, n) = 1 ==> k dvd m * n ==> k dvd m"
wenzelm@21256
   153
  apply (insert gcd_mult_distrib2 [of m k n])
wenzelm@21256
   154
  apply simp
wenzelm@21256
   155
  apply (erule_tac t = m in ssubst)
wenzelm@21256
   156
  apply simp
wenzelm@21256
   157
  done
wenzelm@21256
   158
wenzelm@21256
   159
lemma relprime_dvd_mult_iff: "gcd (k, n) = 1 ==> (k dvd m * n) = (k dvd m)"
wenzelm@21256
   160
  apply (blast intro: relprime_dvd_mult dvd_trans)
wenzelm@21256
   161
  done
wenzelm@21256
   162
wenzelm@21256
   163
lemma gcd_mult_cancel: "gcd (k, n) = 1 ==> gcd (k * m, n) = gcd (m, n)"
wenzelm@21256
   164
  apply (rule dvd_anti_sym)
wenzelm@21256
   165
   apply (rule gcd_greatest)
wenzelm@21256
   166
    apply (rule_tac n = k in relprime_dvd_mult)
wenzelm@21256
   167
     apply (simp add: gcd_assoc)
wenzelm@21256
   168
     apply (simp add: gcd_commute)
wenzelm@21256
   169
    apply (simp_all add: mult_commute)
wenzelm@21256
   170
  apply (blast intro: dvd_trans)
wenzelm@21256
   171
  done
wenzelm@21256
   172
wenzelm@21256
   173
wenzelm@21256
   174
text {* \medskip Addition laws *}
wenzelm@21256
   175
wenzelm@21256
   176
lemma gcd_add1 [simp]: "gcd (m + n, n) = gcd (m, n)"
wenzelm@21256
   177
  apply (case_tac "n = 0")
wenzelm@21256
   178
   apply (simp_all add: gcd_non_0)
wenzelm@21256
   179
  done
wenzelm@21256
   180
wenzelm@21256
   181
lemma gcd_add2 [simp]: "gcd (m, m + n) = gcd (m, n)"
wenzelm@21256
   182
proof -
wenzelm@21256
   183
  have "gcd (m, m + n) = gcd (m + n, m)" by (rule gcd_commute) 
wenzelm@21256
   184
  also have "... = gcd (n + m, m)" by (simp add: add_commute)
wenzelm@21256
   185
  also have "... = gcd (n, m)" by simp
wenzelm@21256
   186
  also have  "... = gcd (m, n)" by (rule gcd_commute) 
wenzelm@21256
   187
  finally show ?thesis .
wenzelm@21256
   188
qed
wenzelm@21256
   189
wenzelm@21256
   190
lemma gcd_add2' [simp]: "gcd (m, n + m) = gcd (m, n)"
wenzelm@21256
   191
  apply (subst add_commute)
wenzelm@21256
   192
  apply (rule gcd_add2)
wenzelm@21256
   193
  done
wenzelm@21256
   194
wenzelm@21256
   195
lemma gcd_add_mult: "gcd (m, k * m + n) = gcd (m, n)"
wenzelm@21263
   196
  by (induct k) (simp_all add: add_assoc)
wenzelm@21256
   197
wenzelm@21256
   198
end