9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{termination}%
|
11866
|
4 |
\isamarkupfalse%
|
8749
|
5 |
%
|
|
6 |
\begin{isamarkuptext}%
|
11309
|
7 |
When a function~$f$ is defined via \isacommand{recdef}, Isabelle tries to prove
|
|
8 |
its termination with the help of the user-supplied measure. Each of the examples
|
|
9 |
above is simple enough that Isabelle can automatically prove that the
|
|
10 |
argument's measure decreases in each recursive call. As a result,
|
9792
|
11 |
$f$\isa{{\isachardot}simps} will contain the defining equations (or variants derived
|
|
12 |
from them) as theorems. For example, look (via \isacommand{thm}) at
|
10187
|
13 |
\isa{sep{\isachardot}simps} and \isa{sep{\isadigit{1}}{\isachardot}simps} to see that they define
|
9792
|
14 |
the same function. What is more, those equations are automatically declared as
|
8749
|
15 |
simplification rules.
|
|
16 |
|
11458
|
17 |
Isabelle may fail to prove the termination condition for some
|
|
18 |
recursive call. Let us try the following artificial function:%
|
8749
|
19 |
\end{isamarkuptext}%
|
11866
|
20 |
\isamarkuptrue%
|
9933
|
21 |
\isacommand{consts}\ f\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
|
11866
|
22 |
\isamarkupfalse%
|
11636
|
23 |
\isacommand{recdef}\ f\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
24 |
\ \ {\isachardoublequote}f{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ x\ {\isasymle}\ y\ then\ x\ else\ f{\isacharparenleft}x{\isacharcomma}y{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
|
|
25 |
%
|
11636
|
26 |
\begin{isamarkuptext}%
|
12332
|
27 |
\noindent This definition fails, and Isabelle prints an error message
|
|
28 |
showing you what it was unable to prove. You will then have to prove it as a
|
|
29 |
separate lemma before you attempt the definition of your function once
|
|
30 |
more. In our case the required lemma is the obvious one:%
|
11636
|
31 |
\end{isamarkuptext}%
|
11866
|
32 |
\isamarkuptrue%
|
|
33 |
\isacommand{lemma}\ termi{\isacharunderscore}lem{\isacharcolon}\ {\isachardoublequote}{\isasymnot}\ x\ {\isasymle}\ y\ {\isasymLongrightarrow}\ x\ {\isacharminus}\ Suc\ y\ {\isacharless}\ x\ {\isacharminus}\ y{\isachardoublequote}\isamarkupfalse%
|
|
34 |
%
|
11636
|
35 |
\begin{isamarkuptxt}%
|
|
36 |
\noindent
|
|
37 |
It was not proved automatically because of the awkward behaviour of subtraction
|
|
38 |
on type \isa{nat}. This requires more arithmetic than is tried by default:%
|
|
39 |
\end{isamarkuptxt}%
|
11866
|
40 |
\isamarkuptrue%
|
11636
|
41 |
\isacommand{apply}{\isacharparenleft}arith{\isacharparenright}\isanewline
|
11866
|
42 |
\isamarkupfalse%
|
|
43 |
\isacommand{done}\isamarkupfalse%
|
|
44 |
%
|
11636
|
45 |
\begin{isamarkuptext}%
|
|
46 |
\noindent
|
|
47 |
Because \isacommand{recdef}'s termination prover involves simplification,
|
|
48 |
we include in our second attempt a hint: the \attrdx{recdef_simp} attribute
|
12332
|
49 |
says to use \isa{termi{\isacharunderscore}lem} as a simplification rule.%
|
11636
|
50 |
\end{isamarkuptext}%
|
11866
|
51 |
\isamarkuptrue%
|
12332
|
52 |
\isamarkupfalse%
|
11866
|
53 |
\isamarkupfalse%
|
12332
|
54 |
\isacommand{recdef}\ f\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}{\isachardot}\ x{\isacharminus}y{\isacharparenright}{\isachardoublequote}\isanewline
|
|
55 |
\ \ {\isachardoublequote}f{\isacharparenleft}x{\isacharcomma}y{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ x\ {\isasymle}\ y\ then\ x\ else\ f{\isacharparenleft}x{\isacharcomma}y{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
56 |
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}simp{\isacharcolon}\ termi{\isacharunderscore}lem{\isacharparenright}\isamarkupfalse%
|
12332
|
57 |
\isamarkupfalse%
|
11866
|
58 |
%
|
11636
|
59 |
\begin{isamarkuptext}%
|
|
60 |
\noindent
|
12332
|
61 |
This time everything works fine. Now \isa{f{\isachardot}simps} contains precisely
|
|
62 |
the stated recursion equation for \isa{{\isacharquery}{\isacharquery}{\isachardot}f}, which has been stored as a
|
11636
|
63 |
simplification rule. Thus we can automatically prove results such as this one:%
|
|
64 |
\end{isamarkuptext}%
|
11866
|
65 |
\isamarkuptrue%
|
12332
|
66 |
\isacommand{theorem}\ {\isachardoublequote}f{\isacharparenleft}{\isadigit{1}}{\isacharcomma}{\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ f{\isacharparenleft}{\isadigit{1}}{\isacharcomma}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
|
11866
|
67 |
\isamarkupfalse%
|
11636
|
68 |
\isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline
|
11866
|
69 |
\isamarkupfalse%
|
|
70 |
\isacommand{done}\isamarkupfalse%
|
|
71 |
%
|
11636
|
72 |
\begin{isamarkuptext}%
|
|
73 |
\noindent
|
|
74 |
More exciting theorems require induction, which is discussed below.
|
|
75 |
|
|
76 |
If the termination proof requires a new lemma that is of general use, you can
|
|
77 |
turn it permanently into a simplification rule, in which case the above
|
|
78 |
\isacommand{hint} is not necessary. But our \isa{termi{\isacharunderscore}lem} is not
|
12332
|
79 |
sufficiently general to warrant this distinction.%
|
11636
|
80 |
\end{isamarkuptext}%
|
11866
|
81 |
\isamarkuptrue%
|
|
82 |
\isamarkupfalse%
|
11636
|
83 |
\end{isabellebody}%
|
9145
|
84 |
%%% Local Variables:
|
|
85 |
%%% mode: latex
|
|
86 |
%%% TeX-master: "root"
|
|
87 |
%%% End:
|