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