doc-src/TutorialI/Recdef/document/simplification.tex
author nipkow
Tue, 29 Aug 2000 15:43:29 +0200
changeset 9722 a5f86aed785b
parent 9721 7e51c9f3d5a0
child 9754 a123a64cadeb
permissions -rw-r--r--
*** empty log message ***
nipkow@9722
     1
%
nipkow@9722
     2
\begin{isabellebody}%
nipkow@8749
     3
%
nipkow@8749
     4
\begin{isamarkuptext}%
nipkow@8749
     5
Once we have succeeded in proving all termination conditions, the recursion
nipkow@8749
     6
equations become simplification rules, just as with
nipkow@8749
     7
\isacommand{primrec}. In most cases this works fine, but there is a subtle
nipkow@8749
     8
problem that must be mentioned: simplification may not
nipkow@8749
     9
terminate because of automatic splitting of \isa{if}.
nipkow@8749
    10
Let us look at an example:%
nipkow@8749
    11
\end{isamarkuptext}%
wenzelm@9674
    12
\isacommand{consts}\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
wenzelm@9674
    13
\isacommand{recdef}\ gcd\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9674
    14
\ \ {\isachardoublequote}gcd\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ n{\isacharequal}\isadigit{0}\ then\ m\ else\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    15
\begin{isamarkuptext}%
nipkow@8749
    16
\noindent
nipkow@8749
    17
According to the measure function, the second argument should decrease with
nipkow@9541
    18
each recursive call. The resulting termination condition
nipkow@9541
    19
\begin{quote}
nipkow@9541
    20
nipkow@9541
    21
\begin{isabelle}%
wenzelm@9674
    22
\mbox{n}\ {\isasymnoteq}\ \isadigit{0}\ {\isasymLongrightarrow}\ \mbox{m}\ mod\ \mbox{n}\ {\isacharless}\ \mbox{n}
nipkow@9541
    23
\end{isabelle}%
nipkow@9541
    24
nipkow@9541
    25
\end{quote}
nipkow@8749
    26
is provded automatically because it is already present as a lemma in the
nipkow@8749
    27
arithmetic library. Thus the recursion equation becomes a simplification
nipkow@8749
    28
rule. Of course the equation is nonterminating if we are allowed to unfold
nipkow@8749
    29
the recursive call inside the \isa{else} branch, which is why programming
nipkow@8749
    30
languages and our simplifier don't do that. Unfortunately the simplifier does
nipkow@8749
    31
something else which leads to the same problem: it splits \isa{if}s if the
nipkow@8749
    32
condition simplifies to neither \isa{True} nor \isa{False}. For
nipkow@9541
    33
example, simplification reduces
nipkow@9541
    34
\begin{quote}
nipkow@9541
    35
nipkow@9541
    36
\begin{isabelle}%
wenzelm@9674
    37
gcd\ {\isacharparenleft}\mbox{m}{\isacharcomma}\ \mbox{n}{\isacharparenright}\ {\isacharequal}\ \mbox{k}
nipkow@9541
    38
\end{isabelle}%
nipkow@9541
    39
nipkow@9541
    40
\end{quote}
nipkow@9541
    41
in one step to
nipkow@9541
    42
\begin{quote}
nipkow@9541
    43
nipkow@9541
    44
\begin{isabelle}%
wenzelm@9674
    45
{\isacharparenleft}if\ \mbox{n}\ {\isacharequal}\ \isadigit{0}\ then\ \mbox{m}\ else\ gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ \mbox{k}
nipkow@9541
    46
\end{isabelle}%
nipkow@9541
    47
nipkow@9541
    48
\end{quote}
nipkow@9541
    49
where the condition cannot be reduced further, and splitting leads to
nipkow@9541
    50
\begin{quote}
nipkow@9541
    51
nipkow@9541
    52
\begin{isabelle}%
wenzelm@9674
    53
{\isacharparenleft}\mbox{n}\ {\isacharequal}\ \isadigit{0}\ {\isasymlongrightarrow}\ \mbox{m}\ {\isacharequal}\ \mbox{k}{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}\mbox{n}\ {\isasymnoteq}\ \isadigit{0}\ {\isasymlongrightarrow}\ gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}\ {\isacharequal}\ \mbox{k}{\isacharparenright}
nipkow@9541
    54
\end{isabelle}%
nipkow@9541
    55
nipkow@9541
    56
\end{quote}
wenzelm@9674
    57
Since the recursive call \isa{gcd\ {\isacharparenleft}\mbox{n}{\isacharcomma}\ \mbox{m}\ mod\ \mbox{n}{\isacharparenright}} is no longer protected by
nipkow@9541
    58
an \isa{if}, it is unfolded again, which leads to an infinite chain of
nipkow@9541
    59
simplification steps. Fortunately, this problem can be avoided in many
nipkow@9541
    60
different ways.
nipkow@8749
    61
nipkow@8771
    62
The most radical solution is to disable the offending
nipkow@8749
    63
\isa{split_if} as shown in the section on case splits in
nipkow@8749
    64
\S\ref{sec:SimpFeatures}.
nipkow@8749
    65
However, we do not recommend this because it means you will often have to
nipkow@8749
    66
invoke the rule explicitly when \isa{if} is involved.
nipkow@8749
    67
nipkow@8749
    68
If possible, the definition should be given by pattern matching on the left
nipkow@8749
    69
rather than \isa{if} on the right. In the case of \isa{gcd} the
nipkow@8749
    70
following alternative definition suggests itself:%
nipkow@8749
    71
\end{isamarkuptext}%
wenzelm@9674
    72
\isacommand{consts}\ gcd\isadigit{1}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
wenzelm@9674
    73
\isacommand{recdef}\ gcd\isadigit{1}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9674
    74
\ \ {\isachardoublequote}gcd\isadigit{1}\ {\isacharparenleft}m{\isacharcomma}\ \isadigit{0}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
wenzelm@9674
    75
\ \ {\isachardoublequote}gcd\isadigit{1}\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd\isadigit{1}{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    76
\begin{isamarkuptext}%
nipkow@8749
    77
\noindent
nipkow@8749
    78
Note that the order of equations is important and hides the side condition
nipkow@8749
    79
\isa{n \isasymnoteq\ 0}. Unfortunately, in general the case distinction
nipkow@8749
    80
may not be expressible by pattern matching.
nipkow@8749
    81
nipkow@8749
    82
A very simple alternative is to replace \isa{if} by \isa{case}, which
nipkow@8749
    83
is also available for \isa{bool} but is not split automatically:%
nipkow@8749
    84
\end{isamarkuptext}%
wenzelm@9674
    85
\isacommand{consts}\ gcd\isadigit{2}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isacharasterisk}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
wenzelm@9674
    86
\isacommand{recdef}\ gcd\isadigit{2}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9674
    87
\ \ {\isachardoublequote}gcd\isadigit{2}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}case\ n{\isacharequal}\isadigit{0}\ of\ True\ {\isasymRightarrow}\ m\ {\isacharbar}\ False\ {\isasymRightarrow}\ gcd\isadigit{2}{\isacharparenleft}n{\isacharcomma}m\ mod\ n{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
nipkow@8749
    88
\begin{isamarkuptext}%
nipkow@8749
    89
\noindent
nipkow@8749
    90
In fact, this is probably the neatest solution next to pattern matching.
nipkow@8749
    91
nipkow@8749
    92
A final alternative is to replace the offending simplification rules by
nipkow@8749
    93
derived conditional ones. For \isa{gcd} it means we have to prove%
nipkow@8749
    94
\end{isamarkuptext}%
wenzelm@9674
    95
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}gcd\ {\isacharparenleft}m{\isacharcomma}\ \isadigit{0}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
wenzelm@9674
    96
\isacommand{by}{\isacharparenleft}simp{\isacharparenright}\isanewline
wenzelm@9674
    97
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}n\ {\isasymnoteq}\ \isadigit{0}\ {\isasymLongrightarrow}\ gcd{\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@9674
    98
\isacommand{by}{\isacharparenleft}simp{\isacharparenright}%
nipkow@8749
    99
\begin{isamarkuptext}%
nipkow@8749
   100
\noindent
nipkow@8749
   101
after which we can disable the original simplification rule:%
nipkow@8749
   102
\end{isamarkuptext}%
wenzelm@9674
   103
\isacommand{lemmas}\ {\isacharbrackleft}simp\ del{\isacharbrackright}\ {\isacharequal}\ gcd{\isachardot}simps\isanewline
nipkow@9722
   104
\end{isabellebody}%
wenzelm@9145
   105
%%% Local Variables:
wenzelm@9145
   106
%%% mode: latex
wenzelm@9145
   107
%%% TeX-master: "root"
wenzelm@9145
   108
%%% End: