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