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