doc-src/TutorialI/Rules/Primes.thy
author paulson
Mon, 23 Oct 2000 18:55:00 +0200
changeset 10304 a372910d82d6
parent 10300 b247e62520ec
child 10341 6eb91805a012
permissions -rw-r--r--
part of set-up
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     1
(* EXTRACT from HOL/ex/Primes.thy*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     2
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     3
theory Primes = Main:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     4
consts
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
     5
  gcd     :: "nat*nat \<Rightarrow> nat"               (*Euclid's algorithm *)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     6
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
     7
recdef gcd "measure ((\<lambda>(m,n).n) ::nat*nat \<Rightarrow> nat)"
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     8
    "gcd (m, n) = (if n=0 then m else gcd(n, m mod n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
     9
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    10
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    11
ML "Pretty.setmargin 64"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    12
ML "IsarOutput.indent := 5"  (*that is, Doc/TutorialI/settings.ML*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    13
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    14
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    15
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    16
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    17
@{thm[display]"dvd_def"}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    18
\rulename{dvd_def}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    19
\end{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    20
*};
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
(*** Euclid's Algorithm ***)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    24
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    25
lemma gcd_0 [simp]: "gcd(m,0) = m"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    26
  apply (simp);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    27
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    28
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    29
lemma gcd_non_0 [simp]: "0<n \<Longrightarrow> gcd(m,n) = gcd (n, m mod n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    30
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    31
  done;
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    32
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    33
declare gcd.simps [simp del];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    34
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    35
(*gcd(m,n) divides m and n.  The conjunctions don't seem provable separately*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    36
lemma gcd_dvd_both: "(gcd(m,n) dvd m) \<and> (gcd(m,n) dvd n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    37
  apply (induct_tac m n rule: gcd.induct)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    38
  apply (case_tac "n=0")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    39
  apply (simp_all)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    40
  apply (blast dest: dvd_mod_imp_dvd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    41
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    42
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    43
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    44
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    45
@{thm[display] dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    46
\rulename{dvd_mod_imp_dvd}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    47
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    48
@{thm[display] dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    49
\rulename{dvd_trans}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    50
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    51
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    52
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    53
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    54
goal\ (lemma\ gcd_dvd_both):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    55
gcd\ (m,\ n)\ dvd\ m\ \isasymand \ gcd\ (m,\ n)\ dvd\ n\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    56
\ 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
    57
\ \ \ \ \ \ \ \ \ \ \isasymLongrightarrow \ gcd\ (n,\ m\ mod\ n)\ dvd\ m
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    58
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    59
*};
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
lemmas gcd_dvd1 [iff] = gcd_dvd_both [THEN conjunct1]
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    63
lemmas gcd_dvd2 [iff] = gcd_dvd_both [THEN conjunct2];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    64
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    65
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    66
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    67
\begin{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    68
@{thm[display] gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    69
\rulename{gcd_dvd1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    70
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    71
@{thm[display] gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    72
\rulename{gcd_dvd2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    73
\end{quote}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    74
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    75
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    76
(*Maximality: for all m,n,k naturals, 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    77
                if k divides m and k divides n then k divides gcd(m,n)*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    78
lemma gcd_greatest [rule_format]:
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    79
      "(k dvd m) \<longrightarrow> (k dvd n) \<longrightarrow> k dvd gcd(m,n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    80
  apply (induct_tac m n rule: gcd.induct)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    81
  apply (case_tac "n=0")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    82
  apply (simp_all add: dvd_mod);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    83
  done;
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    84
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    85
theorem gcd_greatest_iff [iff]: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    86
        "k dvd gcd(m,n) = (k dvd m \<and> k dvd n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    87
  apply (blast intro!: gcd_greatest intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    88
  done;
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    89
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    90
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    91
constdefs
10300
b247e62520ec X-symbol
paulson
parents: 10295
diff changeset
    92
  is_gcd  :: "[nat,nat,nat] \<Rightarrow> bool"        (*gcd as a relation*)
10295
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    93
    "is_gcd p m n == p dvd m  \<and>  p dvd n  \<and>
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    94
                     (ALL d. d dvd m \<and> d dvd n \<longrightarrow> d dvd p)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    95
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    96
(*Function gcd yields the Greatest Common Divisor*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    97
lemma is_gcd: "is_gcd (gcd(m,n)) m n"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    98
  apply (simp add: is_gcd_def gcd_greatest);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
    99
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   100
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   101
(*uniqueness of GCDs*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   102
lemma is_gcd_unique: "\<lbrakk> is_gcd m a b; is_gcd n a b \<rbrakk> \<Longrightarrow> m=n"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   103
  apply (simp add: is_gcd_def);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   104
  apply (blast intro: dvd_anti_sym)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   105
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   106
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   107
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   108
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   109
@{thm[display] dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   110
\rulename{dvd_anti_sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   111
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   112
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   113
proof\ (prove):\ step\ 1\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   114
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   115
goal\ (lemma\ is_gcd_unique):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   116
\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
   117
\ 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
   118
\ \ \ \ \ \ \ 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
   119
\ \ \ \ \isasymLongrightarrow \ m\ =\ n
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   120
\end{isabelle}
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
lemma gcd_assoc: "gcd(gcd(k,m),n) = gcd(k,gcd(m,n))"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   124
  apply (rule is_gcd_unique)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   125
  apply (rule is_gcd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   126
  apply (simp add: is_gcd_def);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   127
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   128
  done 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   129
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   130
text{*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   131
\begin{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   132
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   133
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   134
goal\ (lemma\ gcd_assoc):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   135
gcd\ (gcd\ (k,\ m),\ n)\ =\ gcd\ (k,\ gcd\ (m,\ n))\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   136
\ 1.\ gcd\ (k,\ gcd\ (m,\ n))\ dvd\ k\ \isasymand \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   137
\ \ \ \ 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
   138
\end{isabelle}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   139
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   140
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   141
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   142
lemma gcd_dvd_gcd_mult: "gcd(m,n) dvd gcd(k*m, n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   143
  apply (blast intro: dvd_trans);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   144
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   145
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   146
(*This is half of the proof (by dvd_anti_sym) of*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   147
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
   148
  oops
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   149
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   150
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   151
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   152
text{*\noindent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   153
Forward proof material: of, OF, THEN, simplify.
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
text{*\noindent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   157
SKIP most developments...
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   158
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   159
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   160
(** Commutativity **)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   161
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   162
lemma is_gcd_commute: "is_gcd k m n = is_gcd k n m"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   163
  apply (auto simp add: is_gcd_def);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   164
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   165
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   166
lemma gcd_commute: "gcd(m,n) = gcd(n,m)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   167
  apply (rule is_gcd_unique)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   168
  apply (rule is_gcd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   169
  apply (subst is_gcd_commute)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   170
  apply (simp add: is_gcd)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   171
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   172
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   173
lemma gcd_1 [simp]: "gcd(m,1) = 1"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   174
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   175
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   176
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   177
lemma gcd_1_left [simp]: "gcd(1,m) = 1"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   178
  apply (simp add: gcd_commute [of 1])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   179
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   180
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   181
text{*\noindent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   182
as far as HERE.
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   183
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   184
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   185
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   186
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   187
@{thm[display] gcd_1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   188
\rulename{gcd_1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   189
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   190
@{thm[display] gcd_1_left}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   191
\rulename{gcd_1_left}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   192
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   193
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   194
text{*\noindent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   195
SKIP THIS PROOF
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   196
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   197
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   198
lemma gcd_mult_distrib2: "k * gcd(m,n) = gcd(k*m, k*n)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   199
  apply (induct_tac m n rule: gcd.induct)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   200
  apply (case_tac "n=0")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   201
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   202
  apply (case_tac "k=0")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   203
  apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   204
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   205
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   206
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   207
@{thm[display] gcd_mult_distrib2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   208
\rulename{gcd_mult_distrib2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   209
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   210
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   211
text{*\noindent
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   212
of, simplified
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   213
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   214
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   215
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   216
lemmas gcd_mult_0 = gcd_mult_distrib2 [of k 1];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   217
lemmas gcd_mult_1 = gcd_mult_0 [simplified];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   218
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   219
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   220
@{thm[display] gcd_mult_distrib2 [of _ 1]}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   221
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   222
@{thm[display] gcd_mult_0}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   223
\rulename{gcd_mult_0}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   224
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   225
@{thm[display] gcd_mult_1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   226
\rulename{gcd_mult_1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   227
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   228
@{thm[display] sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   229
\rulename{sym}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   230
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   231
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   232
lemmas gcd_mult = gcd_mult_1 [THEN sym];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   233
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   234
lemmas gcd_mult = gcd_mult_distrib2 [of k 1, simplified, THEN sym];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   235
      (*better in one step!*)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   236
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   237
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   238
more legible
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   239
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   240
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   241
lemma gcd_mult [simp]: "gcd(k, k*n) = k"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   242
  apply (rule gcd_mult_distrib2 [of k 1, simplified, THEN sym])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   243
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   244
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   245
lemmas gcd_self = gcd_mult [of k 1, simplified];
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   246
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   247
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   248
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   249
Rules handy with THEN
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   250
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   251
@{thm[display] iffD1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   252
\rulename{iffD1}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   253
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   254
@{thm[display] iffD2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   255
\rulename{iffD2}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   256
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   257
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   258
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   259
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   260
again: more legible
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   261
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   262
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   263
lemma gcd_self [simp]: "gcd(k,k) = k"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   264
  apply (rule gcd_mult [of k 1, simplified])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   265
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   266
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   267
lemma relprime_dvd_mult: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   268
      "\<lbrakk> gcd(k,n)=1; k dvd (m*n) \<rbrakk> \<Longrightarrow> k dvd m";
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   269
  apply (insert gcd_mult_distrib2 [of m k n])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   270
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   271
  apply (erule_tac t="m" in ssubst);
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   272
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   273
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   274
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   275
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   276
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   277
Another example of "insert"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   278
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   279
@{thm[display] mod_div_equality}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   280
\rulename{mod_div_equality}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   281
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   282
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   283
lemma div_mult_self_is_m: 
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   284
      "0<n \<Longrightarrow> (m*n) div n = (m::nat)"
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   285
  apply (insert mod_div_equality [of "m*n" n])
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   286
  apply (simp)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   287
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   288
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   289
lemma relprime_dvd_mult_iff: "gcd(k,n)=1 \<Longrightarrow> k dvd (m*n) = k dvd m";
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   290
  apply (blast intro: relprime_dvd_mult dvd_trans)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   291
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   292
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   293
lemma relprime_20_81: "gcd(#20,#81) = 1";
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   294
  apply (simp add: gcd.simps)
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   295
  done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   296
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   297
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   298
text {*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   299
Examples of 'OF'
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   300
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   301
@{thm[display] relprime_dvd_mult}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   302
\rulename{relprime_dvd_mult}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   303
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   304
@{thm[display] relprime_dvd_mult [OF relprime_20_81]}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   305
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   306
@{thm[display] dvd_refl}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   307
\rulename{dvd_refl}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   308
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   309
@{thm[display] dvd_add}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   310
\rulename{dvd_add}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   311
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   312
@{thm[display] dvd_add [OF dvd_refl dvd_refl]}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   313
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   314
@{thm[display] dvd_add [OF _ dvd_refl]}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   315
*};
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   316
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   317
lemma "\<lbrakk>(z::int) < #37; #66 < #2*z; z*z \<noteq> #1225; Q(#34); Q(#36)\<rbrakk> \<Longrightarrow> Q(z)";
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   318
apply (subgoal_tac "z = #34 \<or> z = #36")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   319
apply blast
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   320
apply (subgoal_tac "z \<noteq> #35")
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   321
apply arith
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   322
apply force
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   323
done
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   324
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   325
text
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   326
{*
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   327
proof\ (prove):\ step\ 1\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   328
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   329
goal\ (lemma):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   330
\isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk \ \isasymLongrightarrow \ Q\ z\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   331
\ 1.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36;\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   332
\ \ \ \ \ \ \ z\ =\ \#34\ \isasymor \ z\ =\ \#36\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   333
\ \ \ \ \isasymLongrightarrow \ Q\ z\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   334
\ 2.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   335
\ \ \ \ \isasymLongrightarrow \ z\ =\ \#34\ \isasymor \ z\ =\ \#36
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   336
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   337
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   338
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   339
proof\ (prove):\ step\ 3\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   340
\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   341
goal\ (lemma):\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   342
\isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk \ \isasymLongrightarrow \ Q\ z\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   343
\ 1.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36;\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   344
\ \ \ \ \ \ \ z\ \isasymnoteq \ \#35\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   345
\ \ \ \ \isasymLongrightarrow \ z\ =\ \#34\ \isasymor \ z\ =\ \#36\isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   346
\ 2.\ \isasymlbrakk z\ <\ \#37;\ \#66\ <\ \#2\ *\ z;\ z\ *\ z\ \isasymnoteq \ \#1225;\ Q\ \#34;\ Q\ \#36\isasymrbrakk \isanewline
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   347
\ \ \ \ \isasymLongrightarrow \ z\ \isasymnoteq \ \#35
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   348
*}
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   349
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   350
8eb12693cead the Rules chapter and theories
paulson
parents:
diff changeset
   351
end