doc-src/TutorialI/Rules/Primes.thy
author paulson
Wed, 10 Jan 2001 11:09:11 +0100
changeset 10846 623141a08705
parent 10795 9e888d60d3e5
child 10853 2c64c7991f7c
permissions -rw-r--r--
reformatting, and splitting the end of "Primes" to create "Forward"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10341
6eb91805a012 added the $Id:$ line
paulson
parents: 10300
diff changeset
     1
(* ID:         $Id$ *)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     2
(* EXTRACT from HOL/ex/Primes.thy*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     3
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
     4
(*Euclid's algorithm *)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     5
theory Primes = Main:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     6
consts
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
     7
  gcd     :: "nat*nat \<Rightarrow> nat"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     8
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
     9
recdef gcd "measure ((\<lambda>(m,n).n) ::nat*nat \<Rightarrow> nat)"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    10
    "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    11
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    12
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    13
ML "Pretty.setmargin 64"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    14
ML "IsarOutput.indent := 5"  (*that is, Doc/TutorialI/settings.ML*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    15
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    16
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    17
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    18
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    19
@{thm[display]"dvd_def"}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    20
\rulename{dvd_def}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    21
\end{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    22
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    23
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    24
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    25
(*** Euclid's Algorithm ***)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    26
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    27
lemma gcd_0 [simp]: "gcd(m,0) = m"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    28
apply (simp);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    29
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    30
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    31
lemma gcd_non_0 [simp]: "0<n \<Longrightarrow> gcd(m,n) = gcd (n, m mod n)"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    32
apply (simp)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    33
done;
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    34
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    35
declare gcd.simps [simp del];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    36
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    37
(*gcd(m,n) divides m and n.  The conjunctions don't seem provable separately*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    38
lemma gcd_dvd_both: "(gcd(m,n) dvd m) \<and> (gcd(m,n) dvd n)"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    39
apply (induct_tac m n rule: gcd.induct)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    40
apply (case_tac "n=0")
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    41
apply (simp_all)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    42
by (blast dest: dvd_mod_imp_dvd)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    43
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    44
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    45
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    46
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    47
@{thm[display] dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    48
\rulename{dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    49
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    50
@{thm[display] dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    51
\rulename{dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    52
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    53
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    54
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    55
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    56
goal\ (lemma\ gcd_dvd_both):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    57
gcd\ (m,\ n)\ dvd\ m\ \isasymand \ gcd\ (m,\ n)\ dvd\ n\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    58
\ 1.\ \isasymAnd m\ n.\ \isasymlbrakk 0\ <\ n;\ gcd\ (n,\ m\ mod\ n)\ dvd\ n\ \isasymand \ gcd\ (n,\ m\ mod\ n)\ dvd\ (m\ mod\ n)\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    59
\ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ gcd\ (n,\ m\ mod\ n)\ dvd\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    60
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    61
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    62
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    63
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    64
lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    65
lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    66
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    67
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    68
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    69
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    70
@{thm[display] gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    71
\rulename{gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    72
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    73
@{thm[display] gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    74
\rulename{gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    75
\end{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    76
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    77
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    78
(*Maximality: for all m,n,k naturals, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    79
                if k divides m and k divides n then k divides gcd(m,n)*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    80
lemma gcd_greatest [rule_format]:
10790
520dd8696927 *** empty log message ***
nipkow
parents: 10341
diff changeset
    81
      "k dvd m \<longrightarrow> k dvd n \<longrightarrow> k dvd gcd(m,n)"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    82
apply (induct_tac m n rule: gcd.induct)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    83
apply (case_tac "n=0")
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    84
apply (simp_all add: dvd_mod)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    85
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    86
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    87
theorem gcd_greatest_iff [iff]: 
10795
9e888d60d3e5 minor edits to Chapters 1-3
paulson
parents: 10790
diff changeset
    88
        "(k dvd gcd(m,n)) = (k dvd m \<and> k dvd n)"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    89
by (blast intro!: gcd_greatest intro: dvd_trans)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    90
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    91
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    92
(**** The material below was omitted from the book ****)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    93
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    94
constdefs
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
    95
  is_gcd  :: "[nat,nat,nat] \<Rightarrow> bool"        (*gcd as a relation*)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    96
    "is_gcd p m n == p dvd m  \<and>  p dvd n  \<and>
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    97
                     (ALL d. d dvd m \<and> d dvd n \<longrightarrow> d dvd p)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    98
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    99
(*Function gcd yields the Greatest Common Divisor*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   100
lemma is_gcd: "is_gcd (gcd(m,n)) m n"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   101
apply (simp add: is_gcd_def gcd_greatest);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   102
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   103
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   104
(*uniqueness of GCDs*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   105
lemma is_gcd_unique: "\<lbrakk> is_gcd m a b; is_gcd n a b \<rbrakk> \<Longrightarrow> m=n"
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   106
apply (simp add: is_gcd_def);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   107
apply (blast intro: dvd_anti_sym)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   108
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   109
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   110
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   111
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   112
@{thm[display] dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   113
\rulename{dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   114
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   115
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   116
proof\ (prove):\ step\ 1\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   117
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   118
goal\ (lemma\ is_gcd_unique):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   119
\isasymlbrakk is_gcd\ m\ a\ b;\ is_gcd\ n\ a\ b\isasymrbrakk \ \isasymLongrightarrow \ m\ =\ n\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   120
\ 1.\ \isasymlbrakk m\ dvd\ a\ \isasymand \ m\ dvd\ b\ \isasymand \ (\isasymforall d.\ d\ dvd\ a\ \isasymand \ d\ dvd\ b\ \isasymlongrightarrow \ d\ dvd\ m);\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   121
\ \ \ \ \ \ \ n\ dvd\ a\ \isasymand \ n\ dvd\ b\ \isasymand \ (\isasymforall d.\ d\ dvd\ a\ \isasymand \ d\ dvd\ b\ \isasymlongrightarrow \ d\ dvd\ n)\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   122
\ \ \ \ \isasymLongrightarrow \ m\ =\ n
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   123
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   124
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   125
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   126
lemma gcd_assoc: "gcd(gcd(k,m),n) = gcd(k,gcd(m,n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   127
  apply (rule is_gcd_unique)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   128
  apply (rule is_gcd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   129
  apply (simp add: is_gcd_def);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   130
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   131
  done 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   132
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   133
text{*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   134
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   135
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   136
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   137
goal\ (lemma\ gcd_assoc):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   138
gcd\ (gcd\ (k,\ m),\ n)\ =\ gcd\ (k,\ gcd\ (m,\ n))\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   139
\ 1.\ gcd\ (k,\ gcd\ (m,\ n))\ dvd\ k\ \isasymand \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   140
\ \ \ \ gcd\ (k,\ gcd\ (m,\ n))\ dvd\ m\ \isasymand \ gcd\ (k,\ gcd\ (m,\ n))\ dvd\ n
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   141
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   142
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   143
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   144
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   145
lemma gcd_dvd_gcd_mult: "gcd(m,n) dvd gcd(k*m, n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   146
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   147
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   148
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   149
(*This is half of the proof (by dvd_anti_sym) of*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   150
lemma gcd_mult_cancel: "gcd(k,n) = 1 \<Longrightarrow> gcd(k*m, n) = gcd(m,n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   151
  oops
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   152
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   153
end