10879
|
1 |
% $Id$
|
|
2 |
\section{The Set of Even Numbers}
|
|
3 |
|
|
4 |
The set of even numbers can be inductively defined as the least set
|
11129
|
5 |
containing 0 and closed under the operation $+2$. Obviously,
|
10879
|
6 |
\emph{even} can also be expressed using the divides relation (\isa{dvd}).
|
|
7 |
We shall prove below that the two formulations coincide. On the way we
|
|
8 |
shall examine the primary means of reasoning about inductively defined
|
|
9 |
sets: rule induction.
|
|
10 |
|
|
11 |
\subsection{Making an Inductive Definition}
|
|
12 |
|
|
13 |
Using \isacommand{consts}, we declare the constant \isa{even} to be a set
|
|
14 |
of natural numbers. The \isacommand{inductive} declaration gives it the
|
|
15 |
desired properties.
|
|
16 |
\begin{isabelle}
|
|
17 |
\isacommand{consts}\ even\ ::\ "nat\ set"\isanewline
|
|
18 |
\isacommand{inductive}\ even\isanewline
|
|
19 |
\isakeyword{intros}\isanewline
|
|
20 |
zero[intro!]:\ "0\ \isasymin \ even"\isanewline
|
|
21 |
step[intro!]:\ "n\ \isasymin \ even\ \isasymLongrightarrow \ (Suc\ (Suc\
|
|
22 |
n))\ \isasymin \ even"
|
|
23 |
\end{isabelle}
|
|
24 |
|
|
25 |
An inductive definition consists of introduction rules. The first one
|
|
26 |
above states that 0 is even; the second states that if $n$ is even, then so
|
|
27 |
is
|
|
28 |
$n+2$. Given this declaration, Isabelle generates a fixed point definition
|
|
29 |
for \isa{even} and proves theorems about it. These theorems include the
|
|
30 |
introduction rules specified in the declaration, an elimination rule for case
|
|
31 |
analysis and an induction rule. We can refer to these theorems by
|
|
32 |
automatically-generated names. Here are two examples:
|
|
33 |
%
|
|
34 |
\begin{isabelle}
|
|
35 |
0\ \isasymin \ even
|
|
36 |
\rulename{even.zero}
|
|
37 |
\par\smallskip
|
|
38 |
n\ \isasymin \ even\ \isasymLongrightarrow \ Suc\ (Suc\ n)\ \isasymin \
|
|
39 |
even%
|
|
40 |
\rulename{even.step}
|
|
41 |
\end{isabelle}
|
|
42 |
|
|
43 |
The introduction rules can be given attributes. Here both rules are
|
|
44 |
specified as \isa{intro!}, directing the classical reasoner to
|
|
45 |
apply them aggressively. Obviously, regarding 0 as even is safe. The
|
|
46 |
\isa{step} rule is also safe because $n+2$ is even if and only if $n$ is
|
|
47 |
even. We prove this equivalence later.
|
|
48 |
|
|
49 |
\subsection{Using Introduction Rules}
|
|
50 |
|
|
51 |
Our first lemma states that numbers of the form $2\times k$ are even.
|
|
52 |
Introduction rules are used to show that specific values belong to the
|
|
53 |
inductive set. Such proofs typically involve
|
|
54 |
induction, perhaps over some other inductive set.
|
|
55 |
\begin{isabelle}
|
|
56 |
\isacommand{lemma}\ two_times_even[intro!]:\ "\#2*k\ \isasymin \ even"
|
|
57 |
\isanewline
|
|
58 |
\isacommand{apply}\ (induct\ "k")\isanewline
|
|
59 |
\ \isacommand{apply}\ auto\isanewline
|
|
60 |
\isacommand{done}
|
|
61 |
\end{isabelle}
|
|
62 |
%
|
|
63 |
The first step is induction on the natural number \isa{k}, which leaves
|
|
64 |
two subgoals:
|
|
65 |
\begin{isabelle}
|
|
66 |
\ 1.\ \#2\ *\ 0\ \isasymin \ even\isanewline
|
|
67 |
\ 2.\ \isasymAnd n.\ \#2\ *\ n\ \isasymin \ even\ \isasymLongrightarrow \ \#2\ *\ Suc\ n\ \isasymin \ even
|
|
68 |
\end{isabelle}
|
|
69 |
%
|
|
70 |
Here \isa{auto} simplifies both subgoals so that they match the introduction
|
|
71 |
rules, which are then applied automatically.
|
|
72 |
|
|
73 |
Our ultimate goal is to prove the equivalence between the traditional
|
|
74 |
definition of \isa{even} (using the divides relation) and our inductive
|
|
75 |
definition. One direction of this equivalence is immediate by the lemma
|
11129
|
76 |
just proved, whose \isa{intro!} attribute ensures it is applied automatically.
|
10879
|
77 |
\begin{isabelle}
|
|
78 |
\isacommand{lemma}\ dvd_imp_even:\ "\#2\ dvd\ n\ \isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
|
79 |
\isacommand{by}\ (auto\ simp\ add:\ dvd_def)
|
|
80 |
\end{isabelle}
|
|
81 |
|
|
82 |
\subsection{Rule Induction}
|
|
83 |
\label{sec:rule-induction}
|
|
84 |
|
|
85 |
From the definition of the set
|
|
86 |
\isa{even}, Isabelle has
|
|
87 |
generated an induction rule:
|
|
88 |
\begin{isabelle}
|
|
89 |
\isasymlbrakk xa\ \isasymin \ even;\isanewline
|
|
90 |
\ P\ 0;\isanewline
|
|
91 |
\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ P\ n\isasymrbrakk \
|
|
92 |
\isasymLongrightarrow \ P\ (Suc\ (Suc\ n))\isasymrbrakk\isanewline
|
|
93 |
\ \isasymLongrightarrow \ P\ xa%
|
|
94 |
\rulename{even.induct}
|
|
95 |
\end{isabelle}
|
|
96 |
A property \isa{P} holds for every even number provided it
|
|
97 |
holds for~\isa{0} and is closed under the operation
|
11129
|
98 |
\isa{Suc(Suc \(\cdot\))}. Then \isa{P} is closed under the introduction
|
10879
|
99 |
rules for \isa{even}, which is the least set closed under those rules.
|
|
100 |
This type of inductive argument is called \textbf{rule induction}.
|
|
101 |
|
|
102 |
Apart from the double application of \isa{Suc}, the induction rule above
|
|
103 |
resembles the familiar mathematical induction, which indeed is an instance
|
|
104 |
of rule induction; the natural numbers can be defined inductively to be
|
|
105 |
the least set containing \isa{0} and closed under~\isa{Suc}.
|
|
106 |
|
|
107 |
Induction is the usual way of proving a property of the elements of an
|
|
108 |
inductively defined set. Let us prove that all members of the set
|
|
109 |
\isa{even} are multiples of two.
|
|
110 |
\begin{isabelle}
|
|
111 |
\isacommand{lemma}\ even_imp_dvd:\ "n\ \isasymin \ even\ \isasymLongrightarrow \ \#2\ dvd\ n"
|
|
112 |
\end{isabelle}
|
|
113 |
%
|
|
114 |
We begin by applying induction. Note that \isa{even.induct} has the form
|
|
115 |
of an elimination rule, so we use the method \isa{erule}. We get two
|
|
116 |
subgoals:
|
|
117 |
\begin{isabelle}
|
|
118 |
\isacommand{apply}\ (erule\ even.induct)
|
|
119 |
\isanewline\isanewline
|
|
120 |
\ 1.\ \#2\ dvd\ 0\isanewline
|
|
121 |
\ 2.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ \#2\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ \#2\ dvd\ Suc\ (Suc\ n)
|
|
122 |
\end{isabelle}
|
|
123 |
%
|
|
124 |
We unfold the definition of \isa{dvd} in both subgoals, proving the first
|
|
125 |
one and simplifying the second:
|
|
126 |
\begin{isabelle}
|
|
127 |
\isacommand{apply}\ (simp_all\ add:\ dvd_def)
|
|
128 |
\isanewline\isanewline
|
|
129 |
\ 1.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ \isasymexists k.\
|
|
130 |
n\ =\ \#2\ *\ k\isasymrbrakk \ \isasymLongrightarrow \ \isasymexists k.\
|
|
131 |
Suc\ (Suc\ n)\ =\ \#2\ *\ k
|
|
132 |
\end{isabelle}
|
|
133 |
%
|
|
134 |
The next command eliminates the existential quantifier from the assumption
|
|
135 |
and replaces \isa{n} by \isa{\#2\ *\ k}.
|
|
136 |
\begin{isabelle}
|
|
137 |
\isacommand{apply}\ clarify
|
|
138 |
\isanewline\isanewline
|
|
139 |
\ 1.\ \isasymAnd n\ k.\ \#2\ *\ k\ \isasymin \ even\ \isasymLongrightarrow \ \isasymexists ka.\ Suc\ (Suc\ (\#2\ *\ k))\ =\ \#2\ *\ ka%
|
|
140 |
\end{isabelle}
|
|
141 |
%
|
|
142 |
To conclude, we tell Isabelle that the desired value is
|
|
143 |
\isa{Suc\ k}. With this hint, the subgoal falls to \isa{simp}.
|
|
144 |
\begin{isabelle}
|
11156
|
145 |
\isacommand{apply}\ (rule_tac\ x\ =\ "Suc\ k"\ \isakeyword{in}\ exI, simp)
|
10879
|
146 |
\end{isabelle}
|
|
147 |
|
|
148 |
|
|
149 |
\medskip
|
|
150 |
Combining the previous two results yields our objective, the
|
|
151 |
equivalence relating \isa{even} and \isa{dvd}.
|
|
152 |
%
|
|
153 |
%we don't want [iff]: discuss?
|
|
154 |
\begin{isabelle}
|
|
155 |
\isacommand{theorem}\ even_iff_dvd:\ "(n\ \isasymin \ even)\ =\ (\#2\ dvd\ n)"\isanewline
|
|
156 |
\isacommand{by}\ (blast\ intro:\ dvd_imp_even\ even_imp_dvd)
|
|
157 |
\end{isabelle}
|
|
158 |
|
|
159 |
|
|
160 |
\subsection{Generalization and Rule Induction}
|
|
161 |
\label{sec:gen-rule-induction}
|
|
162 |
|
|
163 |
Before applying induction, we typically must generalize
|
|
164 |
the induction formula. With rule induction, the required generalization
|
|
165 |
can be hard to find and sometimes requires a complete reformulation of the
|
11156
|
166 |
problem. In this example, our first attempt uses the obvious statement of
|
|
167 |
the result. It fails:
|
10879
|
168 |
%
|
|
169 |
\begin{isabelle}
|
|
170 |
\isacommand{lemma}\ "Suc\ (Suc\ n)\ \isasymin \ even\
|
|
171 |
\isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
|
172 |
\isacommand{apply}\ (erule\ even.induct)\isanewline
|
|
173 |
\isacommand{oops}
|
|
174 |
\end{isabelle}
|
|
175 |
%
|
|
176 |
Rule induction finds no occurrences of \isa{Suc(Suc\ n)} in the
|
|
177 |
conclusion, which it therefore leaves unchanged. (Look at
|
|
178 |
\isa{even.induct} to see why this happens.) We have these subgoals:
|
|
179 |
\begin{isabelle}
|
|
180 |
\ 1.\ n\ \isasymin \ even\isanewline
|
|
181 |
\ 2.\ \isasymAnd na.\ \isasymlbrakk na\ \isasymin \ even;\ n\ \isasymin \ even\isasymrbrakk \ \isasymLongrightarrow \ n\ \isasymin \ even%
|
|
182 |
\end{isabelle}
|
|
183 |
The first one is hopeless. Rule inductions involving
|
|
184 |
non-trivial terms usually fail. How to deal with such situations
|
|
185 |
in general is described in {\S}\ref{sec:ind-var-in-prems} below.
|
|
186 |
In the current case the solution is easy because
|
|
187 |
we have the necessary inverse, subtraction:
|
|
188 |
\begin{isabelle}
|
|
189 |
\isacommand{lemma}\ even_imp_even_minus_2:\ "n\ \isasymin \ even\ \isasymLongrightarrow \ n-\#2\ \isasymin \ even"\isanewline
|
|
190 |
\isacommand{apply}\ (erule\ even.induct)\isanewline
|
|
191 |
\ \isacommand{apply}\ auto\isanewline
|
|
192 |
\isacommand{done}
|
|
193 |
\end{isabelle}
|
|
194 |
%
|
|
195 |
This lemma is trivially inductive. Here are the subgoals:
|
|
196 |
\begin{isabelle}
|
|
197 |
\ 1.\ 0\ -\ \#2\ \isasymin \ even\isanewline
|
|
198 |
\ 2.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ n\ -\ \#2\ \isasymin \ even\isasymrbrakk \ \isasymLongrightarrow \ Suc\ (Suc\ n)\ -\ \#2\ \isasymin \ even%
|
|
199 |
\end{isabelle}
|
|
200 |
The first is trivial because \isa{0\ -\ \#2} simplifies to \isa{0}, which is
|
|
201 |
even. The second is trivial too: \isa{Suc\ (Suc\ n)\ -\ \#2} simplifies to
|
|
202 |
\isa{n}, matching the assumption.
|
|
203 |
|
|
204 |
\medskip
|
|
205 |
Using our lemma, we can easily prove the result we originally wanted:
|
|
206 |
\begin{isabelle}
|
|
207 |
\isacommand{lemma}\ Suc_Suc_even_imp_even:\ "Suc\ (Suc\ n)\ \isasymin \ even\ \isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
11156
|
208 |
\isacommand{by}\ (drule\ even_imp_even_minus_2, simp)
|
10879
|
209 |
\end{isabelle}
|
|
210 |
|
|
211 |
We have just proved the converse of the introduction rule \isa{even.step}.
|
|
212 |
This suggests proving the following equivalence. We give it the \isa{iff}
|
|
213 |
attribute because of its obvious value for simplification.
|
|
214 |
\begin{isabelle}
|
|
215 |
\isacommand{lemma}\ [iff]:\ "((Suc\ (Suc\ n))\ \isasymin \ even)\ =\ (n\
|
|
216 |
\isasymin \ even)"\isanewline
|
|
217 |
\isacommand{by}\ (blast\ dest:\ Suc_Suc_even_imp_even)
|
|
218 |
\end{isabelle}
|
|
219 |
|
|
220 |
The even numbers example has shown how inductive definitions can be used.
|
|
221 |
Later examples will show that they are actually worth using.
|