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