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