| 
9722
 | 
     1  | 
%
  | 
| 
 | 
     2  | 
\begin{isabellebody}%
 | 
| 
9924
 | 
     3  | 
\def\isabellecontext{termination}%
 | 
| 
17056
 | 
     4  | 
%
  | 
| 
 | 
     5  | 
\isadelimtheory
  | 
| 
 | 
     6  | 
%
  | 
| 
 | 
     7  | 
\endisadelimtheory
  | 
| 
 | 
     8  | 
%
  | 
| 
 | 
     9  | 
\isatagtheory
  | 
| 
 | 
    10  | 
%
  | 
| 
 | 
    11  | 
\endisatagtheory
  | 
| 
 | 
    12  | 
{\isafoldtheory}%
 | 
| 
 | 
    13  | 
%
  | 
| 
 | 
    14  | 
\isadelimtheory
  | 
| 
 | 
    15  | 
%
  | 
| 
 | 
    16  | 
\endisadelimtheory
  | 
| 
8749
 | 
    17  | 
%
  | 
| 
 | 
    18  | 
\begin{isamarkuptext}%
 | 
| 
11309
 | 
    19  | 
When a function~$f$ is defined via \isacommand{recdef}, Isabelle tries to prove
 | 
| 
 | 
    20  | 
its termination with the help of the user-supplied measure.  Each of the examples
  | 
| 
 | 
    21  | 
above is simple enough that Isabelle can automatically prove that the
  | 
| 
 | 
    22  | 
argument's measure decreases in each recursive call. As a result,
  | 
| 
9792
 | 
    23  | 
$f$\isa{{\isachardot}simps} will contain the defining equations (or variants derived
 | 
| 
 | 
    24  | 
from them) as theorems. For example, look (via \isacommand{thm}) at
 | 
| 
10187
 | 
    25  | 
\isa{sep{\isachardot}simps} and \isa{sep{\isadigit{1}}{\isachardot}simps} to see that they define
 | 
| 
9792
 | 
    26  | 
the same function. What is more, those equations are automatically declared as
  | 
| 
8749
 | 
    27  | 
simplification rules.
  | 
| 
 | 
    28  | 
  | 
| 
11458
 | 
    29  | 
Isabelle may fail to prove the termination condition for some
  | 
| 
12489
 | 
    30  | 
recursive call.  Let us try to define Quicksort:%
  | 
| 
11636
 | 
    31  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    32  | 
\isamarkuptrue%
  | 
| 
 | 
    33  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    34  | 
\ qs\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ list\ {\isasymRightarrow}\ nat\ list{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    35  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    36  | 
\ qs\ {\isachardoublequoteopen}measure\ length{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    37  | 
\ {\isachardoublequoteopen}qs\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    38  | 
\ {\isachardoublequoteopen}qs{\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ qs{\isacharparenleft}filter\ {\isacharparenleft}{\isasymlambda}y{\isachardot}\ y{\isasymle}x{\isacharparenright}\ xs{\isacharparenright}\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isacharat}\ qs{\isacharparenleft}filter\ {\isacharparenleft}{\isasymlambda}y{\isachardot}\ x{\isacharless}y{\isacharparenright}\ xs{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
11636
 | 
    39  | 
\begin{isamarkuptext}%
 | 
| 
12489
 | 
    40  | 
\noindent where \isa{filter} is predefined and \isa{filter\ P\ xs}
 | 
| 
 | 
    41  | 
is the list of elements of \isa{xs} satisfying \isa{P}.
 | 
| 
 | 
    42  | 
This definition of \isa{qs} fails, and Isabelle prints an error message
 | 
| 
 | 
    43  | 
showing you what it was unable to prove:
  | 
| 
 | 
    44  | 
\begin{isabelle}%
 | 
| 
 | 
    45  | 
\ \ \ \ \ length\ {\isacharparenleft}filter\ {\isachardot}{\isachardot}{\isachardot}\ xs{\isacharparenright}\ {\isacharless}\ Suc\ {\isacharparenleft}length\ xs{\isacharparenright}%
 | 
| 
 | 
    46  | 
\end{isabelle}
 | 
| 
 | 
    47  | 
We can either prove this as a separate lemma, or try to figure out which
  | 
| 
 | 
    48  | 
existing lemmas may help. We opt for the second alternative. The theory of
  | 
| 
 | 
    49  | 
lists contains the simplification rule \isa{length\ {\isacharparenleft}filter\ P\ xs{\isacharparenright}\ {\isasymle}\ length\ xs},
 | 
| 
15270
 | 
    50  | 
which is what we need, provided we turn \mbox{\isa{{\isacharless}\ Suc}}
 | 
| 
12489
 | 
    51  | 
into
  | 
| 
15270
 | 
    52  | 
\isa{{\isasymle}} so that the rule applies. Lemma
 | 
| 
12489
 | 
    53  | 
\isa{less{\isacharunderscore}Suc{\isacharunderscore}eq{\isacharunderscore}le} does just that: \isa{{\isacharparenleft}m\ {\isacharless}\ Suc\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}m\ {\isasymle}\ n{\isacharparenright}}.
 | 
| 
 | 
    54  | 
  | 
| 
 | 
    55  | 
Now we retry the above definition but supply the lemma(s) just found (or
  | 
| 
 | 
    56  | 
proved). Because \isacommand{recdef}'s termination prover involves
 | 
| 
 | 
    57  | 
simplification, we include in our second attempt a hint: the
  | 
| 
 | 
    58  | 
\attrdx{recdef_simp} attribute says to use \isa{less{\isacharunderscore}Suc{\isacharunderscore}eq{\isacharunderscore}le} as a
 | 
| 
13111
 | 
    59  | 
simplification rule.\cmmdx{hints}%
 | 
| 
11636
 | 
    60  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    61  | 
\isamarkuptrue%
  | 
| 
 | 
    62  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    63  | 
\ qs\ {\isachardoublequoteopen}measure\ length{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    64  | 
\ {\isachardoublequoteopen}qs\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    65  | 
\ {\isachardoublequoteopen}qs{\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ qs{\isacharparenleft}filter\ {\isacharparenleft}{\isasymlambda}y{\isachardot}\ y{\isasymle}x{\isacharparenright}\ xs{\isacharparenright}\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}\ {\isacharat}\ qs{\isacharparenleft}filter\ {\isacharparenleft}{\isasymlambda}y{\isachardot}\ x{\isacharless}y{\isacharparenright}\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
17181
 | 
    66  | 
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}simp{\isacharcolon}\ less{\isacharunderscore}Suc{\isacharunderscore}eq{\isacharunderscore}le{\isacharparenright}%
 | 
| 
11636
 | 
    67  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    68  | 
\noindent
  | 
| 
12489
 | 
    69  | 
This time everything works fine. Now \isa{qs{\isachardot}simps} contains precisely
 | 
| 
 | 
    70  | 
the stated recursion equations for \isa{qs} and they have become
 | 
| 
 | 
    71  | 
simplification rules.
  | 
| 
 | 
    72  | 
Thus we can automatically prove results such as this one:%
  | 
| 
11636
 | 
    73  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    74  | 
\isamarkuptrue%
  | 
| 
 | 
    75  | 
\isacommand{theorem}\isamarkupfalse%
 | 
| 
 | 
    76  | 
\ {\isachardoublequoteopen}qs{\isacharbrackleft}{\isadigit{2}}{\isacharcomma}{\isadigit{3}}{\isacharcomma}{\isadigit{0}}{\isacharbrackright}\ {\isacharequal}\ qs{\isacharbrackleft}{\isadigit{3}}{\isacharcomma}{\isadigit{0}}{\isacharcomma}{\isadigit{2}}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
| 
17056
 | 
    77  | 
%
  | 
| 
 | 
    78  | 
\isadelimproof
  | 
| 
 | 
    79  | 
%
  | 
| 
 | 
    80  | 
\endisadelimproof
  | 
| 
 | 
    81  | 
%
  | 
| 
 | 
    82  | 
\isatagproof
  | 
| 
17175
 | 
    83  | 
\isacommand{apply}\isamarkupfalse%
 | 
| 
 | 
    84  | 
{\isacharparenleft}simp{\isacharparenright}\isanewline
 | 
| 
 | 
    85  | 
\isacommand{done}\isamarkupfalse%
 | 
| 
 | 
    86  | 
%
  | 
| 
17056
 | 
    87  | 
\endisatagproof
  | 
| 
 | 
    88  | 
{\isafoldproof}%
 | 
| 
 | 
    89  | 
%
  | 
| 
 | 
    90  | 
\isadelimproof
  | 
| 
 | 
    91  | 
%
  | 
| 
 | 
    92  | 
\endisadelimproof
  | 
| 
11866
 | 
    93  | 
%
  | 
| 
11636
 | 
    94  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    95  | 
\noindent
  | 
| 
 | 
    96  | 
More exciting theorems require induction, which is discussed below.
  | 
| 
 | 
    97  | 
  | 
| 
12489
 | 
    98  | 
If the termination proof requires a lemma that is of general use, you can
  | 
| 
11636
 | 
    99  | 
turn it permanently into a simplification rule, in which case the above
  | 
| 
12489
 | 
   100  | 
\isacommand{hint} is not necessary. But in the case of
 | 
| 
 | 
   101  | 
\isa{less{\isacharunderscore}Suc{\isacharunderscore}eq{\isacharunderscore}le} this would be of dubious value.%
 | 
| 
11636
 | 
   102  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
   103  | 
\isamarkuptrue%
  | 
| 
17056
 | 
   104  | 
%
  | 
| 
 | 
   105  | 
\isadelimtheory
  | 
| 
 | 
   106  | 
%
  | 
| 
 | 
   107  | 
\endisadelimtheory
  | 
| 
 | 
   108  | 
%
  | 
| 
 | 
   109  | 
\isatagtheory
  | 
| 
 | 
   110  | 
%
  | 
| 
 | 
   111  | 
\endisatagtheory
  | 
| 
 | 
   112  | 
{\isafoldtheory}%
 | 
| 
 | 
   113  | 
%
  | 
| 
 | 
   114  | 
\isadelimtheory
  | 
| 
 | 
   115  | 
%
  | 
| 
 | 
   116  | 
\endisadelimtheory
  | 
| 
11636
 | 
   117  | 
\end{isabellebody}%
 | 
| 
9145
 | 
   118  | 
%%% Local Variables:
  | 
| 
 | 
   119  | 
%%% mode: latex
  | 
| 
 | 
   120  | 
%%% TeX-master: "root"
  | 
| 
 | 
   121  | 
%%% End:
  |