92 |
92 |
93 The following trivial proof illustrates this point. |
93 The following trivial proof illustrates this point. |
94 \begin{isabelle} |
94 \begin{isabelle} |
95 \isacommand{lemma}\ conj_rule:\ "{\isasymlbrakk}P;\ |
95 \isacommand{lemma}\ conj_rule:\ "{\isasymlbrakk}P;\ |
96 Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ |
96 Q\isasymrbrakk\ \isasymLongrightarrow\ P\ \isasymand\ |
97 (Q\ \isasymand\ P){"}\isanewline |
97 (Q\ \isasymand\ P)"\isanewline |
98 \isacommand{apply}\ (rule\ conjI)\isanewline |
98 \isacommand{apply}\ (rule\ conjI)\isanewline |
99 \ \isacommand{apply}\ assumption\isanewline |
99 \ \isacommand{apply}\ assumption\isanewline |
100 \isacommand{apply}\ (rule\ conjI)\isanewline |
100 \isacommand{apply}\ (rule\ conjI)\isanewline |
101 \ \isacommand{apply}\ assumption\isanewline |
101 \ \isacommand{apply}\ assumption\isanewline |
102 \isacommand{apply}\ assumption |
102 \isacommand{apply}\ assumption |
170 \end{isabelle} |
170 \end{isabelle} |
171 When we use this sort of elimination rule backwards, it produces |
171 When we use this sort of elimination rule backwards, it produces |
172 a case split. (We have this before, in proofs by induction.) The following proof |
172 a case split. (We have this before, in proofs by induction.) The following proof |
173 illustrates the use of disjunction elimination. |
173 illustrates the use of disjunction elimination. |
174 \begin{isabelle} |
174 \begin{isabelle} |
175 \isacommand{lemma}\ disj_swap:\ {"}P\ \isasymor\ Q\ |
175 \isacommand{lemma}\ disj_swap:\ "P\ \isasymor\ Q\ |
176 \isasymLongrightarrow\ Q\ \isasymor\ P"\isanewline |
176 \isasymLongrightarrow\ Q\ \isasymor\ P"\isanewline |
177 \isacommand{apply}\ (erule\ disjE)\isanewline |
177 \isacommand{apply}\ (erule\ disjE)\isanewline |
178 \ \isacommand{apply}\ (rule\ disjI2)\isanewline |
178 \ \isacommand{apply}\ (rule\ disjI2)\isanewline |
179 \ \isacommand{apply}\ assumption\isanewline |
179 \ \isacommand{apply}\ assumption\isanewline |
180 \isacommand{apply}\ (rule\ disjI1)\isanewline |
180 \isacommand{apply}\ (rule\ disjI1)\isanewline |
221 |
221 |
222 \section{Destruction rules: some examples} |
222 \section{Destruction rules: some examples} |
223 |
223 |
224 Now let us examine the analogous proof for conjunction. |
224 Now let us examine the analogous proof for conjunction. |
225 \begin{isabelle} |
225 \begin{isabelle} |
226 \isacommand{lemma}\ conj_swap:\ {"}P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P"\isanewline |
226 \isacommand{lemma}\ conj_swap:\ "P\ \isasymand\ Q\ \isasymLongrightarrow\ Q\ \isasymand\ P"\isanewline |
227 \isacommand{apply}\ (rule\ conjI)\isanewline |
227 \isacommand{apply}\ (rule\ conjI)\isanewline |
228 \ \isacommand{apply}\ (drule\ conjunct2)\isanewline |
228 \ \isacommand{apply}\ (drule\ conjunct2)\isanewline |
229 \ \isacommand{apply}\ assumption\isanewline |
229 \ \isacommand{apply}\ assumption\isanewline |
230 \isacommand{apply}\ (drule\ conjunct1)\isanewline |
230 \isacommand{apply}\ (drule\ conjunct1)\isanewline |
231 \isacommand{apply}\ assumption |
231 \isacommand{apply}\ assumption |
232 \end{isabelle} |
232 \end{isabelle} |
233 Recall that the conjunction elimination rules --- whose Isabelle names are |
233 Recall that the conjunction elimination rules --- whose Isabelle names are |
234 \isa{conjunct1} and \isa{conjunct2} --- simply return the first or second half |
234 \isa{conjunct1} and \isa{conjunct2} --- simply return the first or second half |
235 of a conjunction. Rules of this sort (where the conclusion is a subformula of a |
235 of a conjunction. Rules of this sort (where the conclusion is a subformula of a |
236 premise) are called \textbf{destruction} rules, by analogy with the destructor |
236 premise) are called \textbf{destruction} rules, by analogy with the destructor |
237 functions of functional pr§gramming.% |
237 functions of functional programming.% |
238 \footnote{This Isabelle terminology has no counterpart in standard logic texts, |
238 \footnote{This Isabelle terminology has no counterpart in standard logic texts, |
239 although the distinction between the two forms of elimination rule is well known. |
239 although the distinction between the two forms of elimination rule is well known. |
240 Girard \cite[page 74]{girard89}, for example, writes ``The elimination rules are very |
240 Girard \cite[page 74]{girard89}, for example, writes ``The elimination rules are very |
241 bad. What is catastrophic about them is the parasitic presence of a formula [$R$] |
241 bad. What is catastrophic about them is the parasitic presence of a formula [$R$] |
242 which has no structural link with the formula which is eliminated.''} |
242 which has no structural link with the formula which is eliminated.''} |
305 Here is a proof using the rules for implication. This |
305 Here is a proof using the rules for implication. This |
306 lemma performs a sort of uncurrying, replacing the two antecedents |
306 lemma performs a sort of uncurrying, replacing the two antecedents |
307 of a nested implication by a conjunction. |
307 of a nested implication by a conjunction. |
308 \begin{isabelle} |
308 \begin{isabelle} |
309 \isacommand{lemma}\ imp_uncurry:\ |
309 \isacommand{lemma}\ imp_uncurry:\ |
310 {"}P\ \isasymlongrightarrow\ (Q\ |
310 "P\ \isasymlongrightarrow\ (Q\ |
311 \isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\ |
311 \isasymlongrightarrow\ R)\ \isasymLongrightarrow\ P\ |
312 \isasymand\ Q\ \isasymlongrightarrow\ |
312 \isasymand\ Q\ \isasymlongrightarrow\ |
313 R"\isanewline |
313 R"\isanewline |
314 \isacommand{apply}\ (rule\ impI)\isanewline |
314 \isacommand{apply}\ (rule\ impI)\isanewline |
315 \isacommand{apply}\ (erule\ conjE)\isanewline |
315 \isacommand{apply}\ (erule\ conjE)\isanewline |
367 complicated induction formula, typically involving |
367 complicated induction formula, typically involving |
368 \isa{\isasymlongrightarrow} and \isa{\isasymforall}. From this lemma you |
368 \isa{\isasymlongrightarrow} and \isa{\isasymforall}. From this lemma you |
369 derive the desired theorem , typically involving |
369 derive the desired theorem , typically involving |
370 \isa{\isasymLongrightarrow}. We shall see an example below, |
370 \isa{\isasymLongrightarrow}. We shall see an example below, |
371 \S\ref{sec:proving-euclid}. |
371 \S\ref{sec:proving-euclid}. |
372 |
|
373 |
|
374 |
|
375 \remark{negation: notI, notE, ccontr, swap, contrapos?} |
|
376 |
372 |
377 |
373 |
378 \section{Unification and substitution}\label{sec:unification} |
374 \section{Unification and substitution}\label{sec:unification} |
379 |
375 |
380 As we have seen, Isabelle rules involve variables that begin with a |
376 As we have seen, Isabelle rules involve variables that begin with a |
780 variable \isa{P} can introduce a dependence upon~\isa{x}: that would be a |
776 variable \isa{P} can introduce a dependence upon~\isa{x}: that would be a |
781 bound variable capture. Here is the isabelle proof in full: |
777 bound variable capture. Here is the isabelle proof in full: |
782 \begin{isabelle} |
778 \begin{isabelle} |
783 \isacommand{lemma}\ "({\isasymforall}x.\ P\ |
779 \isacommand{lemma}\ "({\isasymforall}x.\ P\ |
784 \isasymlongrightarrow\ Q\ x)\ \isasymLongrightarrow\ P\ |
780 \isasymlongrightarrow\ Q\ x)\ \isasymLongrightarrow\ P\ |
785 \isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x){"}\isanewline |
781 \isasymlongrightarrow\ ({\isasymforall}x.\ Q\ x)"\isanewline |
786 \isacommand{apply}\ (rule\ impI)\isanewline |
782 \isacommand{apply}\ (rule\ impI)\isanewline |
787 \isacommand{apply}\ (rule\ allI)\isanewline |
783 \isacommand{apply}\ (rule\ allI)\isanewline |
788 \isacommand{apply}\ (drule\ spec)\isanewline |
784 \isacommand{apply}\ (drule\ spec)\isanewline |
789 \isacommand{apply}\ (drule\ mp)\isanewline |
785 \isacommand{apply}\ (drule\ mp)\isanewline |
790 \ \ \isacommand{apply}\ assumption\isanewline |
786 \ \ \isacommand{apply}\ assumption\isanewline |
909 existential destruction rule: |
905 existential destruction rule: |
910 \[ \infer{P[(\epsilon x. P) / \, x]}{\exists x.\,P} \] |
906 \[ \infer{P[(\epsilon x. P) / \, x]}{\exists x.\,P} \] |
911 This rule is seldom used, for it can cause exponential blow-up. The |
907 This rule is seldom used, for it can cause exponential blow-up. The |
912 main use of $\epsilon x. P(x)$ is in definitions when $P(x)$ characterizes $x$ |
908 main use of $\epsilon x. P(x)$ is in definitions when $P(x)$ characterizes $x$ |
913 uniquely. For instance, we can define the cardinality of a finite set~$A$ to be that |
909 uniquely. For instance, we can define the cardinality of a finite set~$A$ to be that |
914 $n$ such that $A$ is in one-to-one correspondance with $\{1,\ldots,n\}$. We can then |
910 $n$ such that $A$ is in one-to-one correspondence with $\{1,\ldots,n\}$. We can then |
915 prove that the cardinality of the empty set is zero (since $n=0$ satisfies the |
911 prove that the cardinality of the empty set is zero (since $n=0$ satisfies the |
916 description) and proceed to prove other facts.\remark{SOME theorems |
912 description) and proceed to prove other facts.\remark{SOME theorems |
917 and example} |
913 and example} |
918 |
914 |
919 \begin{exercise} |
915 \begin{exercise} |
1056 a fraction of a second. |
1052 a fraction of a second. |
1057 \begin{isabelle} |
1053 \begin{isabelle} |
1058 \isacommand{lemma}\ |
1054 \isacommand{lemma}\ |
1059 "(({\isasymexists}x.\ |
1055 "(({\isasymexists}x.\ |
1060 {\isasymforall}y.\ |
1056 {\isasymforall}y.\ |
1061 p(x){=}p(y){)}\ |
1057 p(x){=}p(y))\ |
1062 =\ |
1058 =\ |
1063 (({\isasymexists}x.\ |
1059 (({\isasymexists}x.\ |
1064 q(x){)}=({\isasymforall}y.\ |
1060 q(x))=({\isasymforall}y.\ |
1065 p(y){)}){)}\ |
1061 p(y))))\ |
1066 \ \ =\ \ \ \ \isanewline |
1062 \ \ =\ \ \ \ \isanewline |
1067 \ \ \ \ \ \ \ \ |
1063 \ \ \ \ \ \ \ \ |
1068 (({\isasymexists}x.\ |
1064 (({\isasymexists}x.\ |
1069 {\isasymforall}y.\ |
1065 {\isasymforall}y.\ |
1070 q(x){=}q(y){)}\ |
1066 q(x){=}q(y))\ |
1071 =\ |
1067 =\ |
1072 (({\isasymexists}x.\ |
1068 (({\isasymexists}x.\ |
1073 p(x){)}=({\isasymforall}y.\ |
1069 p(x))=({\isasymforall}y.\ |
1074 q(y){)}){)}"\isanewline |
1070 q(y))))"\isanewline |
1075 \isacommand{apply}\ blast\isanewline |
1071 \isacommand{apply}\ blast\isanewline |
1076 \isacommand{done} |
1072 \isacommand{done} |
1077 \end{isabelle} |
1073 \end{isabelle} |
1078 The next example is a logic problem composed by Lewis Carroll. |
1074 The next example is a logic problem composed by Lewis Carroll. |
1079 The {\isa{blast}} method finds it trivial. Moreover, it turns out |
1075 The {\isa{blast}} method finds it trivial. Moreover, it turns out |
1083 \begin{isabelle} |
1079 \begin{isabelle} |
1084 \isacommand{lemma}\ |
1080 \isacommand{lemma}\ |
1085 "({\isasymforall}x.\ |
1081 "({\isasymforall}x.\ |
1086 honest(x)\ \isasymand\ |
1082 honest(x)\ \isasymand\ |
1087 industrious(x)\ \isasymlongrightarrow\ |
1083 industrious(x)\ \isasymlongrightarrow\ |
1088 healthy(x){)}\ |
1084 healthy(x))\ |
1089 \isasymand\ \ \isanewline |
1085 \isasymand\ \ \isanewline |
1090 \ \ \ \ \ \ \ \ \isasymnot\ ({\isasymexists}x.\ |
1086 \ \ \ \ \ \ \ \ \isasymnot\ ({\isasymexists}x.\ |
1091 grocer(x)\ \isasymand\ |
1087 grocer(x)\ \isasymand\ |
1092 healthy(x){)}\ |
1088 healthy(x))\ |
1093 \isasymand\ \isanewline |
1089 \isasymand\ \isanewline |
1094 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1090 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1095 industrious(x)\ \isasymand\ |
1091 industrious(x)\ \isasymand\ |
1096 grocer(x)\ \isasymlongrightarrow\ |
1092 grocer(x)\ \isasymlongrightarrow\ |
1097 honest(x){)}\ |
1093 honest(x))\ |
1098 \isasymand\ \isanewline |
1094 \isasymand\ \isanewline |
1099 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1095 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1100 cyclist(x)\ \isasymlongrightarrow\ |
1096 cyclist(x)\ \isasymlongrightarrow\ |
1101 industrious(x){)}\ |
1097 industrious(x))\ |
1102 \isasymand\ \isanewline |
1098 \isasymand\ \isanewline |
1103 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1099 \ \ \ \ \ \ \ \ ({\isasymforall}x.\ |
1104 {\isasymnot}healthy(x)\ \isasymand\ |
1100 {\isasymnot}healthy(x)\ \isasymand\ |
1105 cyclist(x)\ \isasymlongrightarrow\ |
1101 cyclist(x)\ \isasymlongrightarrow\ |
1106 {\isasymnot}honest(x){)}\ |
1102 {\isasymnot}honest(x))\ |
1107 \ \isanewline |
1103 \ \isanewline |
1108 \ \ \ \ \ \ \ \ \isasymlongrightarrow\ |
1104 \ \ \ \ \ \ \ \ \isasymlongrightarrow\ |
1109 ({\isasymforall}x.\ |
1105 ({\isasymforall}x.\ |
1110 grocer(x)\ \isasymlongrightarrow\ |
1106 grocer(x)\ \isasymlongrightarrow\ |
1111 {\isasymnot}cyclist(x){)}"\isanewline |
1107 {\isasymnot}cyclist(x))"\isanewline |
1112 \isacommand{apply}\ blast\isanewline |
1108 \isacommand{apply}\ blast\isanewline |
1113 \isacommand{done} |
1109 \isacommand{done} |
1114 \end{isabelle} |
1110 \end{isabelle} |
1115 The {\isa{blast}} method is also effective for set theory, which is |
1111 The {\isa{blast}} method is also effective for set theory, which is |
1116 described in the next chapter. This formula below may look horrible, but |
1112 described in the next chapter. This formula below may look horrible, but |
1117 the \isa{blast} method proves it easily. |
1113 the \isa{blast} method proves it easily. |
1118 \begin{isabelle} |
1114 \begin{isabelle} |
1119 \isacommand{lemma}\ "({\isasymUnion}i{\isasymin}I.\ A(i){)}\ \isasyminter\ ({\isasymUnion}j{\isasymin}J.\ B(j){)}\ =\isanewline |
1115 \isacommand{lemma}\ "({\isasymUnion}i{\isasymin}I.\ A(i))\ \isasyminter\ ({\isasymUnion}j{\isasymin}J.\ B(j))\ =\isanewline |
1120 \ \ \ \ \ \ \ \ ({\isasymUnion}i{\isasymin}I.\ {\isasymUnion}j{\isasymin}J.\ A(i)\ \isasyminter\ B(j){)}"\isanewline |
1116 \ \ \ \ \ \ \ \ ({\isasymUnion}i{\isasymin}I.\ {\isasymUnion}j{\isasymin}J.\ A(i)\ \isasyminter\ B(j))"\isanewline |
1121 \isacommand{apply}\ blast\isanewline |
1117 \isacommand{apply}\ blast\isanewline |
1122 \isacommand{done} |
1118 \isacommand{done} |
1123 \end{isabelle} |
1119 \end{isabelle} |
1124 |
1120 |
1125 Few subgoals are couched purely in predicate logic and set theory. |
1121 Few subgoals are couched purely in predicate logic and set theory. |
1147 will result in a simpler goal. When stating this lemma, we include |
1143 will result in a simpler goal. When stating this lemma, we include |
1148 the {\isa{iff}} attribute. Once we have proved the lemma, Isabelle |
1144 the {\isa{iff}} attribute. Once we have proved the lemma, Isabelle |
1149 will make it known to the classical reasoner (and to the simplifier). |
1145 will make it known to the classical reasoner (and to the simplifier). |
1150 \begin{isabelle} |
1146 \begin{isabelle} |
1151 \isacommand{lemma}\ |
1147 \isacommand{lemma}\ |
1152 [iff]{:}\ |
1148 [iff]:\ |
1153 "(xs{\isacharat}ys\ =\ |
1149 "(xs{\isacharat}ys\ =\ |
1154 \isacharbrackleft{]})\ =\ |
1150 \isacharbrackleft{]})\ =\ |
1155 (xs=[]\ |
1151 (xs=[]\ |
1156 \isacharampersand\ |
1152 \isacharampersand\ |
1157 ys=[]{)}"\isanewline |
1153 ys=[])"\isanewline |
1158 \isacommand{apply}\ (induct_tac\ |
1154 \isacommand{apply}\ (induct_tac\ |
1159 xs)\isanewline |
1155 xs)\isanewline |
1160 \isacommand{apply}\ (simp_all) |
1156 \isacommand{apply}\ (simp_all) |
1161 \isanewline |
1157 \isanewline |
1162 \isacommand{done} |
1158 \isacommand{done} |
1190 |
1186 |
1191 A brief development will illustrate advanced use of |
1187 A brief development will illustrate advanced use of |
1192 \isa{blast}. In \S\ref{sec:recdef-simplification}, we declared the |
1188 \isa{blast}. In \S\ref{sec:recdef-simplification}, we declared the |
1193 recursive function {\isa{gcd}}: |
1189 recursive function {\isa{gcd}}: |
1194 \begin{isabelle} |
1190 \begin{isabelle} |
1195 \isacommand{consts}\ gcd\ :{:}\ {"}nat{\isacharasterisk}nat={\isachargreater}nat"\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \isanewline |
1191 \isacommand{consts}\ gcd\ ::\ "nat{\isacharasterisk}nat\ \isasymRightarrow\ nat"\ |
1196 \isacommand{recdef}\ gcd\ {"}measure\ ((\isasymlambda(m{,}n){.}n)\ :{:}nat{\isacharasterisk}nat={\isachargreater}nat){"}\isanewline |
1192 \ |
1197 \ \ \ \ {"}gcd\ (m,\ n)\ =\ (if\ n=0\ then\ m\ else\ gcd(n,\ m\ mod\ n){)}"% |
1193 \ |
|
1194 \ \ \ \ \ \ \ \ \ \ \ \ \isanewline |
|
1195 \isacommand{recdef}\ gcd\ "measure\ ((\isasymlambda(m,n).n)\ |
|
1196 ::nat{\isacharasterisk}nat\ \isasymRightarrow\ nat)"\isanewline |
|
1197 \ \ \ \ "gcd\ (m,n)\ =\ (if\ n=0\ then\ m\ else\ gcd(n,\ m\ mod\ n))" |
1198 \end{isabelle} |
1198 \end{isabelle} |
1199 Let us prove that it computes the greatest common |
1199 Let us prove that it computes the greatest common |
1200 divisor of its two arguments. |
1200 divisor of its two arguments. |
1201 % |
1201 % |
1202 %The declaration yields a recursion |
1202 %The declaration yields a recursion |
1203 %equation for {\isa{gcd}}. Simplifying with this equation can |
1203 %equation for {\isa{gcd}}. Simplifying with this equation can |
1204 %cause looping, expanding to ever-larger expressions of if-then-else |
1204 %cause looping, expanding to ever-larger expressions of if-then-else |
1205 %and {\isa{gcd}} calls. To prevent this, we prove separate simplification rules |
1205 %and {\isa{gcd}} calls. To prevent this, we prove separate simplification rules |
1206 %for $n=0$\ldots |
1206 %for $n=0$\ldots |
1207 %\begin{isabelle} |
1207 %\begin{isabelle} |
1208 %\isacommand{lemma}\ gcd_0\ [simp]{:}\ {"}gcd(m,0)\ =\ m"\isanewline |
1208 %\isacommand{lemma}\ gcd_0\ [simp]:\ "gcd(m,0)\ =\ m"\isanewline |
1209 %\isacommand{apply}\ (simp)\isanewline |
1209 %\isacommand{apply}\ (simp)\isanewline |
1210 %\isacommand{done} |
1210 %\isacommand{done} |
1211 %\end{isabelle} |
1211 %\end{isabelle} |
1212 %\ldots{} and for $n>0$: |
1212 %\ldots{} and for $n>0$: |
1213 %\begin{isabelle} |
1213 %\begin{isabelle} |
1214 %\isacommand{lemma}\ gcd_non_0:\ "0{\isacharless}n\ \isasymLongrightarrow\ gcd(m{,}n)\ =\ gcd\ (n,\ m\ mod\ n){"}\isanewline |
1214 %\isacommand{lemma}\ gcd_non_0:\ "0{\isacharless}n\ \isasymLongrightarrow\ gcd(m,n)\ =\ gcd\ (n,\ m\ mod\ n)"\isanewline |
1215 %\isacommand{apply}\ (simp)\isanewline |
1215 %\isacommand{apply}\ (simp)\isanewline |
1216 %\isacommand{done} |
1216 %\isacommand{done} |
1217 %\end{isabelle} |
1217 %\end{isabelle} |
1218 %This second rule is similar to the original equation but |
1218 %This second rule is similar to the original equation but |
1219 %does not loop because it is conditional. It can be applied only |
1219 %does not loop because it is conditional. It can be applied only |
1220 %when the second argument is known to be non-zero. |
1220 %when the second argument is known to be non-zero. |
1221 %Armed with our two new simplification rules, we now delete the |
1221 %Armed with our two new simplification rules, we now delete the |
1222 %original {\isa{gcd}} recursion equation. |
1222 %original {\isa{gcd}} recursion equation. |
1223 %\begin{isabelle} |
1223 %\begin{isabelle} |
1224 %\isacommand{declare}\ gcd{.}simps\ [simp\ del] |
1224 %\isacommand{declare}\ gcd.simps\ [simp\ del] |
1225 %\end{isabelle} |
1225 %\end{isabelle} |
1226 % |
1226 % |
1227 %Now we can prove some interesting facts about the {\isa{gcd}} function, |
1227 %Now we can prove some interesting facts about the {\isa{gcd}} function, |
1228 %for exampe, that it computes a common divisor of its arguments. |
1228 %for exampe, that it computes a common divisor of its arguments. |
1229 % |
1229 % |
1237 A simple induction proves the theorem. Here \isa{gcd.induct} refers to the |
1237 A simple induction proves the theorem. Here \isa{gcd.induct} refers to the |
1238 induction rule returned by \isa{recdef}. The proof relies on the simplification |
1238 induction rule returned by \isa{recdef}. The proof relies on the simplification |
1239 rules proved in \S\ref{sec:recdef-simplification}, since rewriting by the |
1239 rules proved in \S\ref{sec:recdef-simplification}, since rewriting by the |
1240 definition of \isa{gcd} can cause looping. |
1240 definition of \isa{gcd} can cause looping. |
1241 \begin{isabelle} |
1241 \begin{isabelle} |
1242 \isacommand{lemma}\ gcd_dvd_both:\ "(gcd(m{,}n)\ dvd\ m)\ \isasymand\ (gcd(m{,}n)\ dvd\ n){"}\isanewline |
1242 \isacommand{lemma}\ gcd_dvd_both:\ "(gcd(m,n)\ dvd\ m)\ \isasymand\ (gcd(m,n)\ dvd\ n)"\isanewline |
1243 \isacommand{apply}\ (induct_tac\ m\ n\ rule:\ gcd{.}induct)\isanewline |
1243 \isacommand{apply}\ (induct_tac\ m\ n\ rule:\ gcd.induct)\isanewline |
1244 \isacommand{apply}\ (case_tac\ "n=0"{)}\isanewline |
1244 \isacommand{apply}\ (case_tac\ "n=0")\isanewline |
1245 \isacommand{apply}\ (simp_all)\isanewline |
1245 \isacommand{apply}\ (simp_all)\isanewline |
1246 \isacommand{apply}\ (blast\ dest:\ dvd_mod_imp_dvd)\isanewline |
1246 \isacommand{apply}\ (blast\ dest:\ dvd_mod_imp_dvd)\isanewline |
1247 \isacommand{done}% |
1247 \isacommand{done}% |
1248 \end{isabelle} |
1248 \end{isabelle} |
1249 Notice that the induction formula |
1249 Notice that the induction formula |
1271 This theorem can be applied in various ways. As an introduction rule, it |
1271 This theorem can be applied in various ways. As an introduction rule, it |
1272 would cause backward chaining from the conclusion (namely |
1272 would cause backward chaining from the conclusion (namely |
1273 \isa{?k\ dvd\ ?m}) to the two premises, which |
1273 \isa{?k\ dvd\ ?m}) to the two premises, which |
1274 also involve the divides relation. This process does not look promising |
1274 also involve the divides relation. This process does not look promising |
1275 and could easily loop. More sensible is to apply the rule in the forward |
1275 and could easily loop. More sensible is to apply the rule in the forward |
1276 direction; each step would eliminate the \isa{mod} symboi from an |
1276 direction; each step would eliminate the \isa{mod} symbol from an |
1277 assumption, so the process must terminate. |
1277 assumption, so the process must terminate. |
1278 |
1278 |
1279 So the final proof step applies the \isa{blast} method. |
1279 So the final proof step applies the \isa{blast} method. |
1280 Attaching the {\isa{dest}} attribute to \isa{dvd_mod_imp_dvd} tells \isa{blast} |
1280 Attaching the {\isa{dest}} attribute to \isa{dvd_mod_imp_dvd} tells \isa{blast} |
1281 to use it as destruction rule: in the forward direction. |
1281 to use it as destruction rule: in the forward direction. |
1316 To complete the verification of the {\isa{gcd}} function, we must |
1316 To complete the verification of the {\isa{gcd}} function, we must |
1317 prove that it returns the greatest of all the common divisors |
1317 prove that it returns the greatest of all the common divisors |
1318 of its arguments. The proof is by induction and simplification. |
1318 of its arguments. The proof is by induction and simplification. |
1319 \begin{isabelle} |
1319 \begin{isabelle} |
1320 \isacommand{lemma}\ gcd_greatest\ |
1320 \isacommand{lemma}\ gcd_greatest\ |
1321 [rule_format]{:}\isanewline |
1321 [rule_format]:\isanewline |
1322 \ \ \ \ \ \ \ "(k\ dvd\ |
1322 \ \ \ \ \ \ \ "(k\ dvd\ |
1323 m)\ \isasymlongrightarrow\ (k\ dvd\ |
1323 m)\ \isasymlongrightarrow\ (k\ dvd\ |
1324 n)\ \isasymlongrightarrow\ k\ dvd\ |
1324 n)\ \isasymlongrightarrow\ k\ dvd\ |
1325 gcd(m{,}n)"\isanewline |
1325 gcd(m,n)"\isanewline |
1326 \isacommand{apply}\ (induct_tac\ m\ n\ |
1326 \isacommand{apply}\ (induct_tac\ m\ n\ |
1327 rule:\ gcd{.}induct)\isanewline |
1327 rule:\ gcd.induct)\isanewline |
1328 \isacommand{apply}\ (case_tac\ "n=0"{)}\isanewline |
1328 \isacommand{apply}\ (case_tac\ "n=0")\isanewline |
1329 \isacommand{apply}\ (simp_all\ add:\ gcd_non_0\ dvd_mod)\isanewline |
1329 \isacommand{apply}\ (simp_all\ add:\ gcd_non_0\ dvd_mod)\isanewline |
1330 \isacommand{done} |
1330 \isacommand{done} |
1331 \end{isabelle} |
1331 \end{isabelle} |
1332 % |
1332 % |
1333 Note that the theorem has been expressed using HOL implication, |
1333 Note that the theorem has been expressed using HOL implication, |
1340 |
1340 |
1341 The facts proved above can be summarized as a single logical |
1341 The facts proved above can be summarized as a single logical |
1342 equivalence. This step gives us a chance to see another application |
1342 equivalence. This step gives us a chance to see another application |
1343 of \isa{blast}, and it is worth doing for sound logical reasons. |
1343 of \isa{blast}, and it is worth doing for sound logical reasons. |
1344 \begin{isabelle} |
1344 \begin{isabelle} |
1345 \isacommand{theorem}\ gcd_greatest_iff\ [iff]{:}\isanewline |
1345 \isacommand{theorem}\ gcd_greatest_iff\ [iff]:\isanewline |
1346 \ \ \ \ \ \ \ \ \ "k\ dvd\ gcd(m{,}n)\ =\ (k\ dvd\ m\ \isasymand\ k\ dvd\ n)"\isanewline |
1346 \ \ \ \ \ \ \ \ \ "k\ dvd\ gcd(m,n)\ =\ (k\ dvd\ m\ \isasymand\ k\ dvd\ n)"\isanewline |
1347 \isacommand{apply}\ (blast\ intro!{:}\ gcd_greatest\ intro:\ dvd_trans)\isanewline |
1347 \isacommand{apply}\ (blast\ intro!:\ gcd_greatest\ intro:\ dvd_trans)\isanewline |
1348 \isacommand{done} |
1348 \isacommand{done} |
1349 \end{isabelle} |
1349 \end{isabelle} |
1350 This theorem concisely expresses the correctness of the {\isa{gcd}} |
1350 This theorem concisely expresses the correctness of the {\isa{gcd}} |
1351 function. |
1351 function. |
1352 We state it with the {\isa{iff}} attribute so that |
1352 We state it with the {\isa{iff}} attribute so that |
1493 |
1493 |
1494 Forward proof means deriving new facts from old ones. It is the |
1494 Forward proof means deriving new facts from old ones. It is the |
1495 most fundamental type of proof. Backward proof, by working from goals to |
1495 most fundamental type of proof. Backward proof, by working from goals to |
1496 subgoals, can help us find a difficult proof. But it is |
1496 subgoals, can help us find a difficult proof. But it is |
1497 not always the best way of presenting the proof so found. Forward |
1497 not always the best way of presenting the proof so found. Forward |
1498 proof is particuarly good for reasoning from the general |
1498 proof is particularly good for reasoning from the general |
1499 to the specific. For example, consider the following distributive law for |
1499 to the specific. For example, consider the following distributive law for |
1500 the |
1500 the |
1501 \isa{gcd} function: |
1501 \isa{gcd} function: |
1502 \[ k\times\gcd(m,n) = \gcd(k\times m,k\times n)\] |
1502 \[ k\times\gcd(m,n) = \gcd(k\times m,k\times n)\] |
1503 |
1503 |
1608 what is being proved? More legible |
1608 what is being proved? More legible |
1609 is to state the new lemma explicitly and to prove it using a single |
1609 is to state the new lemma explicitly and to prove it using a single |
1610 \isa{rule} method whose operand is expressed using forward reasoning: |
1610 \isa{rule} method whose operand is expressed using forward reasoning: |
1611 \begin{isabelle} |
1611 \begin{isabelle} |
1612 \isacommand{lemma}\ gcd_mult\ |
1612 \isacommand{lemma}\ gcd_mult\ |
1613 [simp]{:}\ |
1613 [simp]:\ |
1614 "gcd(k,\ |
1614 "gcd(k,\ |
1615 k{\isacharasterisk}n)\ =\ |
1615 k{\isacharasterisk}n)\ =\ |
1616 k"\isanewline |
1616 k"\isanewline |
1617 \isacommand{apply}\ (rule\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym]{)}\isanewline |
1617 \isacommand{apply}\ (rule\ gcd_mult_distrib2\ [of\ k\ 1,\ simplified,\ THEN\ sym])\isanewline |
1618 \isacommand{done} |
1618 \isacommand{done} |
1619 \end{isabelle} |
1619 \end{isabelle} |
1620 Compared with the previous proof of \isa{gcd_mult}, this |
1620 Compared with the previous proof of \isa{gcd_mult}, this |
1621 version shows the reader what has been proved. Also, it receives |
1621 version shows the reader what has been proved. Also, it receives |
1622 the usual Isabelle treatment. In particular, Isabelle generalizes over all |
1622 the usual Isabelle treatment. In particular, Isabelle generalizes over all |
1623 variables: the resulting theorem will have {\isa{?k}} instead of {\isa{k}}. |
1623 variables: the resulting theorem will have {\isa{?k}} instead of {\isa{k}}. |
1624 |
1624 |
1625 At the start of this section, we also saw a proof of $\gcd(k,k)=k$. Here |
1625 At the start of this section, we also saw a proof of $\gcd(k,k)=k$. Here |
1626 is the Isabelle version: |
1626 is the Isabelle version: |
1627 \begin{isabelle} |
1627 \begin{isabelle} |
1628 \isacommand{lemma}\ gcd_self\ |
1628 \isacommand{lemma}\ gcd_self\ [simp]:\ "gcd(k,k)\ =\ k"\isanewline |
1629 [simp]{:}\ |
1629 \isacommand{apply}\ (rule\ gcd_mult\ [of\ k\ 1,\ simplified])\isanewline |
1630 "gcd(k{,}k)\ |
|
1631 =\ k"\isanewline |
|
1632 \isacommand{apply}\ (rule\ gcd_mult\ [of\ k\ 1,\ simplified]{)}\isanewline |
|
1633 \isacommand{done} |
1630 \isacommand{done} |
1634 \end{isabelle} |
1631 \end{isabelle} |
1635 |
1632 |
1636 Recall that \isa{of} generates an instance of a rule by specifying |
1633 Recall that \isa{of} generates an instance of a rule by specifying |
1637 values for its variables. Analogous is \isa{OF}, which generates an |
1634 values for its variables. Analogous is \isa{OF}, which generates an |
1644 \end{isabelle} |
1641 \end{isabelle} |
1645 First, we |
1642 First, we |
1646 prove an instance of its first premise: |
1643 prove an instance of its first premise: |
1647 \begin{isabelle} |
1644 \begin{isabelle} |
1648 \isacommand{lemma}\ relprime_20_81:\ "gcd(\#20,\#81)\ =\ 1"\isanewline |
1645 \isacommand{lemma}\ relprime_20_81:\ "gcd(\#20,\#81)\ =\ 1"\isanewline |
1649 \isacommand{apply}\ (simp\ add:\ gcd{.}simps)\isanewline |
1646 \isacommand{apply}\ (simp\ add:\ gcd.simps)\isanewline |
1650 \isacommand{done}% |
1647 \isacommand{done}% |
1651 \end{isabelle} |
1648 \end{isabelle} |
1652 We have evaluated an application of the \isa{gcd} function by |
1649 We have evaluated an application of the \isa{gcd} function by |
1653 simplification. Expression evaluation is not guaranteed to terminate, and |
1650 simplification. Expression evaluation is not guaranteed to terminate, and |
1654 certainly is not efficient; Isabelle performs arithmetic operations by |
1651 certainly is not efficient; Isabelle performs arithmetic operations by |
1777 \end{isabelle} |
1774 \end{isabelle} |
1778 |
1775 |
1779 We refer to this law explicitly in the following proof: |
1776 We refer to this law explicitly in the following proof: |
1780 \begin{isabelle} |
1777 \begin{isabelle} |
1781 \isacommand{lemma}\ div_mult_self_is_m:\ \isanewline |
1778 \isacommand{lemma}\ div_mult_self_is_m:\ \isanewline |
1782 \ \ \ \ \ \ "0{\isacharless}n\ \isasymLongrightarrow\ (m{\isacharasterisk}n)\ div\ n\ =\ (m:{:}nat)"\isanewline |
1779 \ \ \ \ \ \ "0{\isacharless}n\ \isasymLongrightarrow\ (m{\isacharasterisk}n)\ div\ n\ =\ (m::nat)"\isanewline |
1783 \isacommand{apply}\ (insert\ mod_div_equality\ [of\ "m{\isacharasterisk}n"\ n]{)}\isanewline |
1780 \isacommand{apply}\ (insert\ mod_div_equality\ [of\ "m{\isacharasterisk}n"\ n])\isanewline |
1784 \isacommand{apply}\ (simp)\isanewline |
1781 \isacommand{apply}\ (simp)\isanewline |
1785 \isacommand{done} |
1782 \isacommand{done} |
1786 \end{isabelle} |
1783 \end{isabelle} |
1787 The first step inserts the law, specifying \isa{m*n} and |
1784 The first step inserts the law, specifying \isa{m*n} and |
1788 \isa{n} for its variables. Notice that nontrivial expressions must be |
1785 \isa{n} for its variables. Notice that non-trivial expressions must be |
1789 enclosed in quotation marks. Here is the resulting |
1786 enclosed in quotation marks. Here is the resulting |
1790 subgoal, with its new assumption: |
1787 subgoal, with its new assumption: |
1791 \begin{isabelle} |
1788 \begin{isabelle} |
1792 %0\ \isacharless\ n\ \isasymLongrightarrow\ (m\ |
1789 %0\ \isacharless\ n\ \isasymLongrightarrow\ (m\ |
1793 %\isacharasterisk\ n)\ div\ n\ =\ m\isanewline |
1790 %\isacharasterisk\ n)\ div\ n\ =\ m\isanewline |