doc-src/TutorialI/Advanced/document/WFrec.tex
author nipkow
Wed, 11 Oct 2000 13:15:04 +0200
changeset 10189 865918597b63
parent 10187 0376cccd9118
child 10190 871772d38b30
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     1
%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     2
\begin{isabellebody}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     3
\def\isabellecontext{WFrec}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     4
%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     5
\begin{isamarkuptext}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     6
\noindent
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     7
So far, all recursive definitions where shown to terminate via measure
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     8
functions. Sometimes this can be quite inconvenient or even
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
     9
impossible. Fortunately, \isacommand{recdef} supports much more
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    10
general definitions. For example, termination of Ackermann's function
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    11
can be shown by means of the lexicographic product \isa{{\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}}:%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    12
\end{isamarkuptext}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    13
\isacommand{consts}\ ack\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    14
\isacommand{recdef}\ ack\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}m{\isachardot}\ m{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    15
\ \ {\isachardoublequote}ack{\isacharparenleft}{\isadigit{0}}{\isacharcomma}n{\isacharparenright}\ \ \ \ \ \ \ \ \ {\isacharequal}\ Suc\ n{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    16
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    17
\ \ {\isachardoublequote}ack{\isacharparenleft}Suc\ m{\isacharcomma}Suc\ n{\isacharparenright}\ {\isacharequal}\ ack{\isacharparenleft}m{\isacharcomma}ack{\isacharparenleft}Suc\ m{\isacharcomma}n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    18
\begin{isamarkuptext}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    19
\noindent
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    20
The lexicographic product decreases if either its first component
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    21
decreases (as in the second equation and in the outer call in the
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    22
third equation) or its first component stays the same and the second
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    23
component decreases (as in the inner call in the third equation).
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    24
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    25
In general, \isacommand{recdef} supports termination proofs based on
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    26
arbitrary \emph{wellfounded relations}, i.e.\ \emph{wellfounded
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    27
recursion}\indexbold{recursion!wellfounded}\index{wellfounded
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    28
recursion|see{recursion, wellfounded}}.  A relation $<$ is
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    29
\bfindex{wellfounded} if it has no infinite descending chain $\cdots <
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    30
a@2 < a@1 < a@0$. Clearly, a function definition is total iff the set
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    31
of all pairs $(r,l)$, where $l$ is the argument on the left-hand side
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    32
of an equation and $r$ the argument of some recursive call on the
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    33
corresponding right-hand side, induces a wellfounded relation.  For a
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    34
systematic account of termination proofs via wellfounded relations
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    35
see, for example, \cite{Baader-Nipkow}. The HOL library formalizes
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    36
some of the theory of wellfounded relations. For example
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    37
\isa{wf\ r}\index{*wf|bold} means that relation \isa{r{\isasymColon}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ set} is
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    38
wellfounded.
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    39
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    40
Each \isacommand{recdef} definition should be accompanied (after the
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    41
name of the function) by a wellfounded relation on the argument type
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    42
of the function. For example, \isa{measure} is defined by
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    43
\begin{isabelle}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    44
\ \ \ \ \ measure\ f\ {\isasymequiv}\ {\isacharbraceleft}{\isacharparenleft}y{\isacharcomma}\ x{\isacharparenright}{\isachardot}\ f\ y\ {\isacharless}\ f\ x{\isacharbraceright}%
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    45
\end{isabelle}
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    46
and it has been proved that \isa{measure\ f} is always wellfounded.
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    47
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    48
In addition to \isa{measure}, the library provides
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    49
a number of further constructions for obtaining wellfounded relations.
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    50
Above we have already met \isa{{\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}} of type
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    51
\begin{isabelle}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    52
\ \ \ \ \ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}b\ {\isasymtimes}\ {\isacharprime}b{\isacharparenright}set\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b{\isacharparenright}\ {\isasymtimes}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b{\isacharparenright}{\isacharparenright}set{\isachardoublequote}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    53
\end{isabelle}
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    54
Of course the lexicographic product can also be interated, as in the following
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    55
function definition:%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    56
\end{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    57
\isacommand{consts}\ contrived\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymtimes}\ nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    58
\isacommand{recdef}\ contrived\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    59
\ \ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}i{\isachardot}\ i{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}j{\isachardot}\ j{\isacharparenright}\ {\isacharless}{\isacharasterisk}lex{\isacharasterisk}{\isachargreater}\ measure{\isacharparenleft}{\isasymlambda}k{\isachardot}\ k{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    60
{\isachardoublequote}contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}Suc\ k{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}k{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    61
{\isachardoublequote}contrived{\isacharparenleft}i{\isacharcomma}Suc\ j{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}j{\isacharcomma}j{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    62
{\isachardoublequote}contrived{\isacharparenleft}Suc\ i{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ contrived{\isacharparenleft}i{\isacharcomma}i{\isacharcomma}i{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    63
{\isachardoublequote}contrived{\isacharparenleft}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ {\isadigit{0}}{\isachardoublequote}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    64
\begin{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    65
Lexicographic products of measure functions already go a long way. A
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    66
further useful construction is the embedding of some type in an
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    67
existing wellfounded relation via the inverse image of a function:
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    68
\begin{isabelle}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    69
\ \ \ \ \ inv{\isacharunderscore}image\ {\isacharparenleft}r{\isasymColon}{\isacharparenleft}{\isacharprime}b\ {\isasymtimes}\ {\isacharprime}b{\isacharparenright}\ set{\isacharparenright}\ {\isacharparenleft}f{\isasymColon}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b{\isacharparenright}\ {\isasymequiv}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    70
\ \ \ \ \ {\isacharbraceleft}{\isacharparenleft}x{\isasymColon}{\isacharprime}a{\isacharcomma}\ y{\isasymColon}{\isacharprime}a{\isacharparenright}{\isachardot}\ {\isacharparenleft}f\ x{\isacharcomma}\ f\ y{\isacharparenright}\ {\isasymin}\ r{\isacharbraceright}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    71
\end{isabelle}
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    72
\begin{sloppypar}
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    73
\noindent
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    74
For example, \isa{measure} is actually defined as \isa{inv{\isacharunderscore}mage\ less{\isacharunderscore}than}, where
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    75
\isa{less{\isacharunderscore}than} of type \isa{{\isacharparenleft}nat\ {\isasymtimes}\ nat{\isacharparenright}\ set} is the less-than relation on type \isa{nat}
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    76
(as opposed to \isa{op\ {\isacharless}}, which is of type \isa{{\isacharbrackleft}nat{\isacharcomma}\ nat{\isacharbrackright}\ {\isasymRightarrow}\ bool}).
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    77
\end{sloppypar}
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    78
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    79
%Finally there is also {finite_psubset} the proper subset relation on finite sets
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    80
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    81
All the above constructions are known to \isacommand{recdef}. Thus you
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    82
will never have to prove wellfoundedness of any relation composed
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    83
solely of these building blocks. But of course the proof of
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    84
termination of your function definition, i.e.\ that the arguments
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    85
decrease with every recursive call, may still require you to provide
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    86
additional lemmas.
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    87
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    88
It is also possible to use your own wellfounded relations with \isacommand{recdef}.
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    89
Here is a simplistic example:%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
    90
\end{isamarkuptext}%
10189
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    91
\isacommand{consts}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    92
\isacommand{recdef}\ f\ {\isachardoublequote}id{\isacharparenleft}less{\isacharunderscore}than{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    93
{\isachardoublequote}f\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    94
{\isachardoublequote}f\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ f\ n{\isachardoublequote}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    95
\begin{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    96
Since \isacommand{recdef} is not prepared for \isa{id}, the identity
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    97
function, this leads to the complaint that it could not prove
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    98
\isa{wf\ {\isacharparenleft}id\ less{\isacharunderscore}than{\isacharparenright}}, the wellfoundedness of \isa{id\ less{\isacharunderscore}than}. We should first have proved that \isa{id} preserves wellfoundedness%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
    99
\end{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   100
\isacommand{lemma}\ wf{\isacharunderscore}id{\isacharcolon}\ {\isachardoublequote}wf\ r\ {\isasymLongrightarrow}\ wf{\isacharparenleft}id\ r{\isacharparenright}{\isachardoublequote}\isanewline
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   101
\isacommand{by}\ simp%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   102
\begin{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   103
\noindent
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   104
and should have added the following hint to our above definition:%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   105
\end{isamarkuptext}%
865918597b63 *** empty log message ***
nipkow
parents: 10187
diff changeset
   106
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}wf\ add{\isacharcolon}\ wf{\isacharunderscore}id{\isacharparenright}\end{isabellebody}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   107
%%% Local Variables:
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   108
%%% mode: latex
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   109
%%% TeX-master: "root"
0376cccd9118 *** empty log message ***
nipkow
parents:
diff changeset
   110
%%% End: