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
|
11173
|
27 |
is~$n+2$. Given this declaration, Isabelle generates a fixed point
|
11201
|
28 |
definition for \isa{even} and proves theorems about it,
|
|
29 |
thus following the definitional approach (see \S\ref{sec:definitional}).
|
|
30 |
These theorems
|
11173
|
31 |
include the introduction rules specified in the declaration, an elimination
|
|
32 |
rule for case analysis and an induction rule. We can refer to these
|
|
33 |
theorems by automatically-generated names. Here are two examples:
|
10879
|
34 |
%
|
|
35 |
\begin{isabelle}
|
|
36 |
0\ \isasymin \ even
|
|
37 |
\rulename{even.zero}
|
|
38 |
\par\smallskip
|
|
39 |
n\ \isasymin \ even\ \isasymLongrightarrow \ Suc\ (Suc\ n)\ \isasymin \
|
|
40 |
even%
|
|
41 |
\rulename{even.step}
|
|
42 |
\end{isabelle}
|
|
43 |
|
|
44 |
The introduction rules can be given attributes. Here both rules are
|
|
45 |
specified as \isa{intro!}, directing the classical reasoner to
|
|
46 |
apply them aggressively. Obviously, regarding 0 as even is safe. The
|
|
47 |
\isa{step} rule is also safe because $n+2$ is even if and only if $n$ is
|
|
48 |
even. We prove this equivalence later.
|
|
49 |
|
|
50 |
\subsection{Using Introduction Rules}
|
|
51 |
|
|
52 |
Our first lemma states that numbers of the form $2\times k$ are even.
|
|
53 |
Introduction rules are used to show that specific values belong to the
|
|
54 |
inductive set. Such proofs typically involve
|
|
55 |
induction, perhaps over some other inductive set.
|
|
56 |
\begin{isabelle}
|
|
57 |
\isacommand{lemma}\ two_times_even[intro!]:\ "\#2*k\ \isasymin \ even"
|
|
58 |
\isanewline
|
|
59 |
\isacommand{apply}\ (induct\ "k")\isanewline
|
|
60 |
\ \isacommand{apply}\ auto\isanewline
|
|
61 |
\isacommand{done}
|
|
62 |
\end{isabelle}
|
|
63 |
%
|
|
64 |
The first step is induction on the natural number \isa{k}, which leaves
|
|
65 |
two subgoals:
|
|
66 |
\begin{isabelle}
|
|
67 |
\ 1.\ \#2\ *\ 0\ \isasymin \ even\isanewline
|
|
68 |
\ 2.\ \isasymAnd n.\ \#2\ *\ n\ \isasymin \ even\ \isasymLongrightarrow \ \#2\ *\ Suc\ n\ \isasymin \ even
|
|
69 |
\end{isabelle}
|
|
70 |
%
|
|
71 |
Here \isa{auto} simplifies both subgoals so that they match the introduction
|
|
72 |
rules, which are then applied automatically.
|
|
73 |
|
|
74 |
Our ultimate goal is to prove the equivalence between the traditional
|
|
75 |
definition of \isa{even} (using the divides relation) and our inductive
|
|
76 |
definition. One direction of this equivalence is immediate by the lemma
|
11129
|
77 |
just proved, whose \isa{intro!} attribute ensures it is applied automatically.
|
10879
|
78 |
\begin{isabelle}
|
|
79 |
\isacommand{lemma}\ dvd_imp_even:\ "\#2\ dvd\ n\ \isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
|
80 |
\isacommand{by}\ (auto\ simp\ add:\ dvd_def)
|
|
81 |
\end{isabelle}
|
|
82 |
|
|
83 |
\subsection{Rule Induction}
|
|
84 |
\label{sec:rule-induction}
|
|
85 |
|
|
86 |
From the definition of the set
|
|
87 |
\isa{even}, Isabelle has
|
|
88 |
generated an induction rule:
|
|
89 |
\begin{isabelle}
|
|
90 |
\isasymlbrakk xa\ \isasymin \ even;\isanewline
|
|
91 |
\ P\ 0;\isanewline
|
|
92 |
\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ P\ n\isasymrbrakk \
|
|
93 |
\isasymLongrightarrow \ P\ (Suc\ (Suc\ n))\isasymrbrakk\isanewline
|
|
94 |
\ \isasymLongrightarrow \ P\ xa%
|
|
95 |
\rulename{even.induct}
|
|
96 |
\end{isabelle}
|
|
97 |
A property \isa{P} holds for every even number provided it
|
|
98 |
holds for~\isa{0} and is closed under the operation
|
11129
|
99 |
\isa{Suc(Suc \(\cdot\))}. Then \isa{P} is closed under the introduction
|
10879
|
100 |
rules for \isa{even}, which is the least set closed under those rules.
|
|
101 |
This type of inductive argument is called \textbf{rule induction}.
|
|
102 |
|
|
103 |
Apart from the double application of \isa{Suc}, the induction rule above
|
|
104 |
resembles the familiar mathematical induction, which indeed is an instance
|
|
105 |
of rule induction; the natural numbers can be defined inductively to be
|
|
106 |
the least set containing \isa{0} and closed under~\isa{Suc}.
|
|
107 |
|
|
108 |
Induction is the usual way of proving a property of the elements of an
|
|
109 |
inductively defined set. Let us prove that all members of the set
|
|
110 |
\isa{even} are multiples of two.
|
|
111 |
\begin{isabelle}
|
|
112 |
\isacommand{lemma}\ even_imp_dvd:\ "n\ \isasymin \ even\ \isasymLongrightarrow \ \#2\ dvd\ n"
|
|
113 |
\end{isabelle}
|
|
114 |
%
|
|
115 |
We begin by applying induction. Note that \isa{even.induct} has the form
|
|
116 |
of an elimination rule, so we use the method \isa{erule}. We get two
|
|
117 |
subgoals:
|
|
118 |
\begin{isabelle}
|
|
119 |
\isacommand{apply}\ (erule\ even.induct)
|
|
120 |
\isanewline\isanewline
|
|
121 |
\ 1.\ \#2\ dvd\ 0\isanewline
|
|
122 |
\ 2.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ \#2\ dvd\ n\isasymrbrakk \ \isasymLongrightarrow \ \#2\ dvd\ Suc\ (Suc\ n)
|
|
123 |
\end{isabelle}
|
|
124 |
%
|
|
125 |
We unfold the definition of \isa{dvd} in both subgoals, proving the first
|
|
126 |
one and simplifying the second:
|
|
127 |
\begin{isabelle}
|
|
128 |
\isacommand{apply}\ (simp_all\ add:\ dvd_def)
|
|
129 |
\isanewline\isanewline
|
|
130 |
\ 1.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ \isasymexists k.\
|
|
131 |
n\ =\ \#2\ *\ k\isasymrbrakk \ \isasymLongrightarrow \ \isasymexists k.\
|
|
132 |
Suc\ (Suc\ n)\ =\ \#2\ *\ k
|
|
133 |
\end{isabelle}
|
|
134 |
%
|
|
135 |
The next command eliminates the existential quantifier from the assumption
|
|
136 |
and replaces \isa{n} by \isa{\#2\ *\ k}.
|
|
137 |
\begin{isabelle}
|
|
138 |
\isacommand{apply}\ clarify
|
|
139 |
\isanewline\isanewline
|
|
140 |
\ 1.\ \isasymAnd n\ k.\ \#2\ *\ k\ \isasymin \ even\ \isasymLongrightarrow \ \isasymexists ka.\ Suc\ (Suc\ (\#2\ *\ k))\ =\ \#2\ *\ ka%
|
|
141 |
\end{isabelle}
|
|
142 |
%
|
|
143 |
To conclude, we tell Isabelle that the desired value is
|
|
144 |
\isa{Suc\ k}. With this hint, the subgoal falls to \isa{simp}.
|
|
145 |
\begin{isabelle}
|
11156
|
146 |
\isacommand{apply}\ (rule_tac\ x\ =\ "Suc\ k"\ \isakeyword{in}\ exI, simp)
|
10879
|
147 |
\end{isabelle}
|
|
148 |
|
|
149 |
|
|
150 |
\medskip
|
|
151 |
Combining the previous two results yields our objective, the
|
|
152 |
equivalence relating \isa{even} and \isa{dvd}.
|
|
153 |
%
|
|
154 |
%we don't want [iff]: discuss?
|
|
155 |
\begin{isabelle}
|
|
156 |
\isacommand{theorem}\ even_iff_dvd:\ "(n\ \isasymin \ even)\ =\ (\#2\ dvd\ n)"\isanewline
|
|
157 |
\isacommand{by}\ (blast\ intro:\ dvd_imp_even\ even_imp_dvd)
|
|
158 |
\end{isabelle}
|
|
159 |
|
|
160 |
|
|
161 |
\subsection{Generalization and Rule Induction}
|
|
162 |
\label{sec:gen-rule-induction}
|
|
163 |
|
|
164 |
Before applying induction, we typically must generalize
|
|
165 |
the induction formula. With rule induction, the required generalization
|
|
166 |
can be hard to find and sometimes requires a complete reformulation of the
|
11156
|
167 |
problem. In this example, our first attempt uses the obvious statement of
|
|
168 |
the result. It fails:
|
10879
|
169 |
%
|
|
170 |
\begin{isabelle}
|
|
171 |
\isacommand{lemma}\ "Suc\ (Suc\ n)\ \isasymin \ even\
|
|
172 |
\isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
|
173 |
\isacommand{apply}\ (erule\ even.induct)\isanewline
|
|
174 |
\isacommand{oops}
|
|
175 |
\end{isabelle}
|
|
176 |
%
|
|
177 |
Rule induction finds no occurrences of \isa{Suc(Suc\ n)} in the
|
|
178 |
conclusion, which it therefore leaves unchanged. (Look at
|
|
179 |
\isa{even.induct} to see why this happens.) We have these subgoals:
|
|
180 |
\begin{isabelle}
|
|
181 |
\ 1.\ n\ \isasymin \ even\isanewline
|
|
182 |
\ 2.\ \isasymAnd na.\ \isasymlbrakk na\ \isasymin \ even;\ n\ \isasymin \ even\isasymrbrakk \ \isasymLongrightarrow \ n\ \isasymin \ even%
|
|
183 |
\end{isabelle}
|
|
184 |
The first one is hopeless. Rule inductions involving
|
|
185 |
non-trivial terms usually fail. How to deal with such situations
|
|
186 |
in general is described in {\S}\ref{sec:ind-var-in-prems} below.
|
|
187 |
In the current case the solution is easy because
|
|
188 |
we have the necessary inverse, subtraction:
|
|
189 |
\begin{isabelle}
|
|
190 |
\isacommand{lemma}\ even_imp_even_minus_2:\ "n\ \isasymin \ even\ \isasymLongrightarrow \ n-\#2\ \isasymin \ even"\isanewline
|
|
191 |
\isacommand{apply}\ (erule\ even.induct)\isanewline
|
|
192 |
\ \isacommand{apply}\ auto\isanewline
|
|
193 |
\isacommand{done}
|
|
194 |
\end{isabelle}
|
|
195 |
%
|
|
196 |
This lemma is trivially inductive. Here are the subgoals:
|
|
197 |
\begin{isabelle}
|
|
198 |
\ 1.\ 0\ -\ \#2\ \isasymin \ even\isanewline
|
|
199 |
\ 2.\ \isasymAnd n.\ \isasymlbrakk n\ \isasymin \ even;\ n\ -\ \#2\ \isasymin \ even\isasymrbrakk \ \isasymLongrightarrow \ Suc\ (Suc\ n)\ -\ \#2\ \isasymin \ even%
|
|
200 |
\end{isabelle}
|
|
201 |
The first is trivial because \isa{0\ -\ \#2} simplifies to \isa{0}, which is
|
|
202 |
even. The second is trivial too: \isa{Suc\ (Suc\ n)\ -\ \#2} simplifies to
|
|
203 |
\isa{n}, matching the assumption.
|
|
204 |
|
|
205 |
\medskip
|
|
206 |
Using our lemma, we can easily prove the result we originally wanted:
|
|
207 |
\begin{isabelle}
|
|
208 |
\isacommand{lemma}\ Suc_Suc_even_imp_even:\ "Suc\ (Suc\ n)\ \isasymin \ even\ \isasymLongrightarrow \ n\ \isasymin \ even"\isanewline
|
11156
|
209 |
\isacommand{by}\ (drule\ even_imp_even_minus_2, simp)
|
10879
|
210 |
\end{isabelle}
|
|
211 |
|
|
212 |
We have just proved the converse of the introduction rule \isa{even.step}.
|
|
213 |
This suggests proving the following equivalence. We give it the \isa{iff}
|
|
214 |
attribute because of its obvious value for simplification.
|
|
215 |
\begin{isabelle}
|
|
216 |
\isacommand{lemma}\ [iff]:\ "((Suc\ (Suc\ n))\ \isasymin \ even)\ =\ (n\
|
|
217 |
\isasymin \ even)"\isanewline
|
|
218 |
\isacommand{by}\ (blast\ dest:\ Suc_Suc_even_imp_even)
|
|
219 |
\end{isabelle}
|
|
220 |
|
11173
|
221 |
|
|
222 |
\subsection{Rule Inversion}\label{sec:rule-inversion}
|
|
223 |
|
|
224 |
Case analysis on an inductive definition is called \textbf{rule inversion}.
|
|
225 |
It is frequently used in proofs about operational semantics. It can be
|
|
226 |
highly effective when it is applied automatically. Let us look at how rule
|
|
227 |
inversion is done in Isabelle/HOL\@.
|
|
228 |
|
|
229 |
Recall that \isa{even} is the minimal set closed under these two rules:
|
|
230 |
\begin{isabelle}
|
|
231 |
0\ \isasymin \ even\isanewline
|
|
232 |
n\ \isasymin \ even\ \isasymLongrightarrow \ Suc\ (Suc\ n)\ \isasymin
|
|
233 |
\ even
|
|
234 |
\end{isabelle}
|
|
235 |
Minimality means that \isa{even} contains only the elements that these
|
|
236 |
rules force it to contain. If we are told that \isa{a}
|
|
237 |
belongs to
|
|
238 |
\isa{even} then there are only two possibilities. Either \isa{a} is \isa{0}
|
|
239 |
or else \isa{a} has the form \isa{Suc(Suc~n)}, for some suitable \isa{n}
|
|
240 |
that belongs to
|
|
241 |
\isa{even}. That is the gist of the \isa{cases} rule, which Isabelle proves
|
|
242 |
for us when it accepts an inductive definition:
|
|
243 |
\begin{isabelle}
|
|
244 |
\isasymlbrakk a\ \isasymin \ even;\isanewline
|
|
245 |
\ a\ =\ 0\ \isasymLongrightarrow \ P;\isanewline
|
|
246 |
\ \isasymAnd n.\ \isasymlbrakk a\ =\ Suc(Suc\ n);\ n\ \isasymin \
|
|
247 |
even\isasymrbrakk \ \isasymLongrightarrow \ P\isasymrbrakk \
|
|
248 |
\isasymLongrightarrow \ P
|
|
249 |
\rulename{even.cases}
|
|
250 |
\end{isabelle}
|
|
251 |
|
|
252 |
This general rule is less useful than instances of it for
|
|
253 |
specific patterns. For example, if \isa{a} has the form
|
|
254 |
\isa{Suc(Suc~n)} then the first case becomes irrelevant, while the second
|
|
255 |
case tells us that \isa{n} belongs to \isa{even}. Isabelle will generate
|
|
256 |
this instance for us:
|
|
257 |
\begin{isabelle}
|
|
258 |
\isacommand{inductive\_cases}\ Suc_Suc_cases\ [elim!]:
|
|
259 |
\ "Suc(Suc\ n)\ \isasymin \ even"
|
|
260 |
\end{isabelle}
|
|
261 |
The \isacommand{inductive\_cases} command generates an instance of the
|
|
262 |
\isa{cases} rule for the supplied pattern and gives it the supplied name:
|
|
263 |
%
|
|
264 |
\begin{isabelle}
|
|
265 |
\isasymlbrakk Suc(Suc\ n)\ \isasymin \ even;\ n\ \isasymin \ even\
|
|
266 |
\isasymLongrightarrow \ P\isasymrbrakk \ \isasymLongrightarrow \ P%
|
|
267 |
\rulename{Suc_Suc_cases}
|
|
268 |
\end{isabelle}
|
|
269 |
%
|
|
270 |
Applying this as an elimination rule yields one case where \isa{even.cases}
|
|
271 |
would yield two. Rule inversion works well when the conclusions of the
|
|
272 |
introduction rules involve datatype constructors like \isa{Suc} and \isa{\#}
|
|
273 |
(list ``cons''); freeness reasoning discards all but one or two cases.
|
|
274 |
|
|
275 |
In the \isacommand{inductive\_cases} command we supplied an
|
|
276 |
attribute, \isa{elim!}, indicating that this elimination rule can be applied
|
|
277 |
aggressively. The original
|
|
278 |
\isa{cases} rule would loop if used in that manner because the
|
|
279 |
pattern~\isa{a} matches everything.
|
|
280 |
|
|
281 |
The rule \isa{Suc_Suc_cases} is equivalent to the following implication:
|
|
282 |
\begin{isabelle}
|
|
283 |
Suc (Suc\ n)\ \isasymin \ even\ \isasymLongrightarrow \ n\ \isasymin \
|
|
284 |
even
|
|
285 |
\end{isabelle}
|
|
286 |
%
|
|
287 |
Just above we devoted some effort to reaching precisely
|
|
288 |
this result. Yet we could have obtained it by a one-line declaration,
|
|
289 |
dispensing with the lemma \isa{even_imp_even_minus_2}.
|
|
290 |
This example also justifies the terminology
|
|
291 |
\textbf{rule inversion}: the new rule inverts the introduction rule
|
|
292 |
\isa{even.step}.
|
|
293 |
|
|
294 |
For one-off applications of rule inversion, use the \isa{ind_cases} method.
|
|
295 |
Here is an example:
|
|
296 |
\begin{isabelle}
|
|
297 |
\isacommand{apply}\ (ind_cases\ "Suc(Suc\ n)\ \isasymin \ even")
|
|
298 |
\end{isabelle}
|
|
299 |
The specified instance of the \isa{cases} rule is generated, then applied
|
|
300 |
as an elimination rule.
|
|
301 |
|
|
302 |
To summarize, every inductive definition produces a \isa{cases} rule. The
|
|
303 |
\isacommand{inductive\_cases} command stores an instance of the \isa{cases}
|
|
304 |
rule for a given pattern. Within a proof, the \isa{ind_cases} method
|
|
305 |
applies an instance of the \isa{cases}
|
|
306 |
rule.
|
|
307 |
|
|
308 |
The even numbers example has shown how inductive definitions can be
|
|
309 |
used. Later examples will show that they are actually worth using.
|