doc-src/TutorialI/Rules/Primes.thy
author nipkow
Thu, 15 Mar 2001 15:05:51 +0100
changeset 11210 33300d16a63a
parent 11080 22855d091249
child 11234 6902638af59e
permissions -rw-r--r--
*** empty log message ***
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
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
     4
(*Euclid's algorithm 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
     5
  This material now appears AFTER that of Forward.thy *)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     6
theory Primes = Main:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     7
consts
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
     8
  gcd     :: "nat*nat \<Rightarrow> nat"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     9
10986
616bcfc7b848 simplified gcd
paulson
parents: 10853
diff changeset
    10
recdef gcd "measure snd"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    11
    "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    12
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    13
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    14
ML "Pretty.setmargin 64"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    15
ML "IsarOutput.indent := 5"  (*that is, Doc/TutorialI/settings.ML*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    16
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    17
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    18
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    19
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    20
@{thm[display]"dvd_def"}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    21
\rulename{dvd_def}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    22
\end{quote}
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
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    26
(*** Euclid's Algorithm ***)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    27
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    28
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
    29
apply (simp);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    30
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    31
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    32
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
    33
apply (simp)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    34
done;
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    35
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    36
declare gcd.simps [simp del];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    37
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    38
(*gcd(m,n) divides m and n.  The conjunctions don't seem provable separately*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    39
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
    40
apply (induct_tac m n rule: gcd.induct)
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    41
  --{* @{subgoals[display,indent=0,margin=65]} *}
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    42
apply (case_tac "n=0")
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    43
txt{*subgoals after the case tac
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    44
@{subgoals[display,indent=0,margin=65]}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    45
*};
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    46
apply (simp_all) 
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    47
  --{* @{subgoals[display,indent=0,margin=65]} *}
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    48
by (blast dest: dvd_mod_imp_dvd)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    49
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    50
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    51
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    52
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    53
@{thm[display] dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    54
\rulename{dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    55
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    56
@{thm[display] dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    57
\rulename{dvd_trans}
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    58
*}
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    59
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    60
lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    61
lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2];
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
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    65
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    66
@{thm[display] gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    67
\rulename{gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    68
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    69
@{thm[display] gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    70
\rulename{gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    71
\end{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    72
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    73
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    74
(*Maximality: for all m,n,k naturals, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    75
                if k divides m and k divides n then k divides gcd(m,n)*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    76
lemma gcd_greatest [rule_format]:
10790
520dd8696927 *** empty log message ***
nipkow
parents: 10341
diff changeset
    77
      "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
    78
apply (induct_tac m n rule: gcd.induct)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    79
apply (case_tac "n=0")
10853
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    80
txt{*subgoals after the case tac
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    81
@{subgoals[display,indent=0,margin=65]}
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    82
*};
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    83
apply (simp_all add: dvd_mod)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
    84
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    85
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    86
text {*
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    87
@{thm[display] dvd_mod}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    88
\rulename{dvd_mod}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    89
*}
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    90
10853
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    91
(*just checking the claim that case_tac "n" works too*)
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    92
lemma "k dvd m \<longrightarrow> k dvd n \<longrightarrow> k dvd gcd(m,n)"
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    93
apply (induct_tac m n rule: gcd.induct)
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    94
apply (case_tac "n")
11080
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    95
apply (simp_all add: dvd_mod)
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    96
done
22855d091249 various revisions in response to comments from Tobias
paulson
parents: 10986
diff changeset
    97
10853
2c64c7991f7c case_tac subgoals
paulson
parents: 10846
diff changeset
    98
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    99
theorem gcd_greatest_iff [iff]: 
10795
9e888d60d3e5 minor edits to Chapters 1-3
paulson
parents: 10790
diff changeset
   100
        "(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
   101
by (blast intro!: gcd_greatest intro: dvd_trans)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   102
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   103
10846
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   104
(**** The material below was omitted from the book ****)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   105
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   106
constdefs
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
   107
  is_gcd  :: "[nat,nat,nat] \<Rightarrow> bool"        (*gcd as a relation*)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   108
    "is_gcd p m n == p dvd m  \<and>  p dvd n  \<and>
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   109
                     (ALL d. d dvd m \<and> d dvd n \<longrightarrow> d dvd p)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   110
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   111
(*Function gcd yields the Greatest Common Divisor*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   112
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
   113
apply (simp add: is_gcd_def gcd_greatest);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   114
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   115
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   116
(*uniqueness of GCDs*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   117
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
   118
apply (simp add: is_gcd_def);
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   119
apply (blast intro: dvd_anti_sym)
623141a08705 reformatting, and splitting the end of "Primes" to create "Forward"
paulson
parents: 10795
diff changeset
   120
done
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   121
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   122
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   123
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   124
@{thm[display] dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   125
\rulename{dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   126
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   127
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   128
proof\ (prove):\ step\ 1\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   129
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   130
goal\ (lemma\ is_gcd_unique):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   131
\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
   132
\ 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
   133
\ \ \ \ \ \ \ 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
   134
\ \ \ \ \isasymLongrightarrow \ m\ =\ n
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   135
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   136
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   137
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   138
lemma gcd_assoc: "gcd(gcd(k,m),n) = gcd(k,gcd(m,n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   139
  apply (rule is_gcd_unique)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   140
  apply (rule is_gcd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   141
  apply (simp add: is_gcd_def);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   142
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   143
  done 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   144
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   145
text{*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   146
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   147
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   148
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   149
goal\ (lemma\ gcd_assoc):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   150
gcd\ (gcd\ (k,\ m),\ n)\ =\ gcd\ (k,\ gcd\ (m,\ n))\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   151
\ 1.\ gcd\ (k,\ gcd\ (m,\ n))\ dvd\ k\ \isasymand \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   152
\ \ \ \ 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
   153
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   154
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   155
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   156
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   157
lemma gcd_dvd_gcd_mult: "gcd(m,n) dvd gcd(k*m, n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   158
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   159
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   160
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   161
(*This is half of the proof (by dvd_anti_sym) of*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   162
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
   163
  oops
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   164
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   165
end