doc-src/TutorialI/Recdef/Induction.thy
author nipkow
Wed, 19 Apr 2000 11:56:31 +0200
changeset 8745 13b32661dde4
child 8771 026f37a86ea7
permissions -rw-r--r--
I wonder which files i forgot.
nipkow@8745
     1
(*<*)
nipkow@8745
     2
theory Induction = examples + simplification:;
nipkow@8745
     3
(*>*)
nipkow@8745
     4
nipkow@8745
     5
text{*
nipkow@8745
     6
Assuming we have defined our function such that Isabelle could prove
nipkow@8745
     7
termination and that the recursion equations (or some suitable derived
nipkow@8745
     8
equations) are simplification rules, we might like to prove something about
nipkow@8745
     9
our function. Since the function is recursive, the natural proof principle is
nipkow@8745
    10
again induction. But this time the structural form of induction that comes
nipkow@8745
    11
with datatypes is unlikely to work well---otherwise we could have defined the
nipkow@8745
    12
function by \isacommand{primrec}. Therefore \isacommand{recdef} automatically
nipkow@8745
    13
proves a suitable induction rule $f$\isa{.induct} that follows the
nipkow@8745
    14
recursion pattern of the particular function $f$. We call this
nipkow@8745
    15
\textbf{recursion induction}. Roughly speaking, it
nipkow@8745
    16
requires you to prove for each \isacommand{recdef} equation that the property
nipkow@8745
    17
you are trying to establish holds for the left-hand side provided it holds
nipkow@8745
    18
for all recursive calls on the right-hand side. Here is a simple example:
nipkow@8745
    19
*}
nipkow@8745
    20
nipkow@8745
    21
lemma "map f (sep(x,xs)) = sep(f x, map f xs)";
nipkow@8745
    22
nipkow@8745
    23
txt{*\noindent
nipkow@8745
    24
involving the predefined \isa{map} functional on lists: \isa{map f xs}
nipkow@8745
    25
is the result of applying \isa{f} to all elements of \isa{xs}. We prove
nipkow@8745
    26
this lemma by recursion induction w.r.t. \isa{sep}:
nipkow@8745
    27
*}
nipkow@8745
    28
nipkow@8745
    29
apply(induct_tac x xs rule: sep.induct);
nipkow@8745
    30
nipkow@8745
    31
txt{*\noindent
nipkow@8745
    32
The resulting proof state has three subgoals corresponding to the three
nipkow@8745
    33
clauses for \isa{sep}:
nipkow@8745
    34
\begin{isabellepar}%
nipkow@8745
    35
~1.~{\isasymAnd}a.~map~f~(sep~(a,~[]))~=~sep~(f~a,~map~f~[])\isanewline
nipkow@8745
    36
~2.~{\isasymAnd}a~x.~map~f~(sep~(a,~[x]))~=~sep~(f~a,~map~f~[x])\isanewline
nipkow@8745
    37
~3.~{\isasymAnd}a~x~y~zs.\isanewline
nipkow@8745
    38
~~~~~~~map~f~(sep~(a,~y~\#~zs))~=~sep~(f~a,~map~f~(y~\#~zs))~{\isasymLongrightarrow}\isanewline
nipkow@8745
    39
~~~~~~~map~f~(sep~(a,~x~\#~y~\#~zs))~=~sep~(f~a,~map~f~(x~\#~y~\#~zs))%
nipkow@8745
    40
\end{isabellepar}%
nipkow@8745
    41
The rest is pure simplification:
nipkow@8745
    42
*}
nipkow@8745
    43
nipkow@8745
    44
apply auto.;
nipkow@8745
    45
nipkow@8745
    46
text{*
nipkow@8745
    47
Try proving the above lemma by structural induction, and you find that you
nipkow@8745
    48
need an additional case distinction. What is worse, the names of variables
nipkow@8745
    49
are invented by Isabelle and have nothing to do with the names in the
nipkow@8745
    50
definition of \isa{sep}.
nipkow@8745
    51
nipkow@8745
    52
In general, the format of invoking recursion induction is
nipkow@8745
    53
\begin{ttbox}
nipkow@8745
    54
apply(induct_tac \(x@1 \dots x@n\) rule: \(f\).induct)
nipkow@8745
    55
\end{ttbox}\index{*induct_tac}%
nipkow@8745
    56
where $x@1~\dots~x@n$ is a list of free variables in the subgoal and $f$ the
nipkow@8745
    57
name of a function that takes an $n$-tuple. Usually the subgoal will
nipkow@8745
    58
contain the term $f~x@1~\dots~x@n$ but this need not be the case. The
nipkow@8745
    59
induction rules do not mention $f$ at all. For example \isa{sep.induct}
nipkow@8745
    60
\begin{isabellepar}%
nipkow@8745
    61
{\isasymlbrakk}~{\isasymAnd}a.~?P~a~[];\isanewline
nipkow@8745
    62
~~{\isasymAnd}a~x.~?P~a~[x];\isanewline
nipkow@8745
    63
~~{\isasymAnd}a~x~y~zs.~?P~a~(y~\#~zs)~{\isasymLongrightarrow}~?P~a~(x~\#~y~\#~zs){\isasymrbrakk}\isanewline
nipkow@8745
    64
{\isasymLongrightarrow}~?P~?u~?v%
nipkow@8745
    65
\end{isabellepar}%
nipkow@8745
    66
merely says that in order to prove a property \isa{?P} of \isa{?u} and
nipkow@8745
    67
\isa{?v} you need to prove it for the three cases where \isa{?v} is the
nipkow@8745
    68
empty list, the singleton list, and the list with at least two elements
nipkow@8745
    69
(in which case you may assume it holds for the tail of that list).
nipkow@8745
    70
*}
nipkow@8745
    71
nipkow@8745
    72
(*<*)
nipkow@8745
    73
end
nipkow@8745
    74
(*>*)