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