| 9722 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
| 9924 |      3 | \def\isabellecontext{simplification}%
 | 
| 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}%
 | 
| 11458 |     19 | Once we have proved all the termination conditions, the \isacommand{recdef} 
 | 
|  |     20 | recursion equations become simplification rules, just as with
 | 
| 8749 |     21 | \isacommand{primrec}. In most cases this works fine, but there is a subtle
 | 
|  |     22 | problem that must be mentioned: simplification may not
 | 
|  |     23 | terminate because of automatic splitting of \isa{if}.
 | 
| 11458 |     24 | \index{*if expressions!splitting of}
 | 
| 8749 |     25 | Let us look at an example:%
 | 
|  |     26 | \end{isamarkuptext}%
 | 
| 17175 |     27 | \isamarkuptrue%
 | 
|  |     28 | \isacommand{consts}\isamarkupfalse%
 | 
|  |     29 | \ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
|  |     30 | \isacommand{recdef}\isamarkupfalse%
 | 
|  |     31 | \ gcd\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |     32 | \ \ {\isachardoublequoteopen}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}{\isachardoublequoteclose}%
 | 
| 8749 |     33 | \begin{isamarkuptext}%
 | 
|  |     34 | \noindent
 | 
|  |     35 | According to the measure function, the second argument should decrease with
 | 
| 9541 |     36 | each recursive call. The resulting termination condition
 | 
|  |     37 | \begin{isabelle}%
 | 
| 12473 |     38 | \ \ \ \ \ n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ m\ mod\ n\ {\isacharless}\ n%
 | 
| 9924 |     39 | \end{isabelle}
 | 
| 10878 |     40 | is proved automatically because it is already present as a lemma in
 | 
|  |     41 | HOL\@.  Thus the recursion equation becomes a simplification
 | 
| 8749 |     42 | rule. Of course the equation is nonterminating if we are allowed to unfold
 | 
| 10171 |     43 | the recursive call inside the \isa{else} branch, which is why programming
 | 
| 8749 |     44 | languages and our simplifier don't do that. Unfortunately the simplifier does
 | 
| 11458 |     45 | something else that leads to the same problem: it splits 
 | 
|  |     46 | each \isa{if}-expression unless its
 | 
|  |     47 | condition simplifies to \isa{True} or \isa{False}.  For
 | 
| 9541 |     48 | example, simplification reduces
 | 
|  |     49 | \begin{isabelle}%
 | 
| 21261 |     50 | \ \ \ \ \ gcd\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ k%
 | 
| 9924 |     51 | \end{isabelle}
 | 
| 9541 |     52 | in one step to
 | 
|  |     53 | \begin{isabelle}%
 | 
| 21261 |     54 | \ \ \ \ \ {\isacharparenleft}if\ n\ {\isacharequal}\ {\isadigit{0}}\ then\ m\ else\ gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ k%
 | 
| 9924 |     55 | \end{isabelle}
 | 
| 9541 |     56 | where the condition cannot be reduced further, and splitting leads to
 | 
|  |     57 | \begin{isabelle}%
 | 
| 21261 |     58 | \ \ \ \ \ {\isacharparenleft}n\ {\isacharequal}\ {\isadigit{0}}\ {\isasymlongrightarrow}\ m\ {\isacharequal}\ k{\isacharparenright}\ {\isasymand}\ {\isacharparenleft}n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymlongrightarrow}\ gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}\ {\isacharequal}\ k{\isacharparenright}%
 | 
| 9924 |     59 | \end{isabelle}
 | 
| 21261 |     60 | Since the recursive call \isa{gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}} is no longer protected by
 | 
| 9541 |     61 | an \isa{if}, it is unfolded again, which leads to an infinite chain of
 | 
|  |     62 | simplification steps. Fortunately, this problem can be avoided in many
 | 
|  |     63 | different ways.
 | 
| 8749 |     64 | 
 | 
| 11458 |     65 | The most radical solution is to disable the offending theorem
 | 
|  |     66 | \isa{split{\isacharunderscore}if},
 | 
| 11215 |     67 | as shown in \S\ref{sec:AutoCaseSplits}.  However, we do not recommend this
 | 
| 11458 |     68 | approach: you will often have to invoke the rule explicitly when
 | 
| 11215 |     69 | \isa{if} is involved.
 | 
| 8749 |     70 | 
 | 
|  |     71 | If possible, the definition should be given by pattern matching on the left
 | 
| 21261 |     72 | rather than \isa{if} on the right. In the case of \isa{gcd} the
 | 
| 8749 |     73 | following alternative definition suggests itself:%
 | 
|  |     74 | \end{isamarkuptext}%
 | 
| 17175 |     75 | \isamarkuptrue%
 | 
|  |     76 | \isacommand{consts}\isamarkupfalse%
 | 
|  |     77 | \ gcd{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
|  |     78 | \isacommand{recdef}\isamarkupfalse%
 | 
|  |     79 | \ gcd{\isadigit{1}}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |     80 | \ \ {\isachardoublequoteopen}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
 | 
|  |     81 | \ \ {\isachardoublequoteopen}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isadigit{1}}{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 8749 |     82 | \begin{isamarkuptext}%
 | 
|  |     83 | \noindent
 | 
| 11458 |     84 | The order of equations is important: it hides the side condition
 | 
| 12473 |     85 | \isa{n\ {\isasymnoteq}\ {\isadigit{0}}}.  Unfortunately, in general the case distinction
 | 
| 8749 |     86 | may not be expressible by pattern matching.
 | 
|  |     87 | 
 | 
| 11458 |     88 | A simple alternative is to replace \isa{if} by \isa{case}, 
 | 
|  |     89 | which is also available for \isa{bool} and is not split automatically:%
 | 
| 8749 |     90 | \end{isamarkuptext}%
 | 
| 17175 |     91 | \isamarkuptrue%
 | 
|  |     92 | \isacommand{consts}\isamarkupfalse%
 | 
|  |     93 | \ gcd{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
|  |     94 | \isacommand{recdef}\isamarkupfalse%
 | 
|  |     95 | \ gcd{\isadigit{2}}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
|  |     96 | \ \ {\isachardoublequoteopen}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}{\isachardoublequoteclose}%
 | 
| 8749 |     97 | \begin{isamarkuptext}%
 | 
|  |     98 | \noindent
 | 
| 11458 |     99 | This is probably the neatest solution next to pattern matching, and it is
 | 
|  |    100 | always available.
 | 
| 8749 |    101 | 
 | 
|  |    102 | A final alternative is to replace the offending simplification rules by
 | 
| 21261 |    103 | derived conditional ones. For \isa{gcd} it means we have to prove
 | 
| 10795 |    104 | these lemmas:%
 | 
| 8749 |    105 | \end{isamarkuptext}%
 | 
| 17175 |    106 | \isamarkuptrue%
 | 
|  |    107 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |    108 | \ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}gcd\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
 | 
| 17056 |    109 | %
 | 
|  |    110 | \isadelimproof
 | 
|  |    111 | %
 | 
|  |    112 | \endisadelimproof
 | 
|  |    113 | %
 | 
|  |    114 | \isatagproof
 | 
| 17175 |    115 | \isacommand{apply}\isamarkupfalse%
 | 
|  |    116 | {\isacharparenleft}simp{\isacharparenright}\isanewline
 | 
|  |    117 | \isacommand{done}\isamarkupfalse%
 | 
|  |    118 | %
 | 
| 17056 |    119 | \endisatagproof
 | 
|  |    120 | {\isafoldproof}%
 | 
|  |    121 | %
 | 
|  |    122 | \isadelimproof
 | 
|  |    123 | \isanewline
 | 
|  |    124 | %
 | 
|  |    125 | \endisadelimproof
 | 
| 15481 |    126 | \isanewline
 | 
| 17175 |    127 | \isacommand{lemma}\isamarkupfalse%
 | 
|  |    128 | \ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}n\ {\isasymnoteq}\ {\isadigit{0}}\ {\isasymLongrightarrow}\ gcd{\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 17056 |    129 | %
 | 
|  |    130 | \isadelimproof
 | 
|  |    131 | %
 | 
|  |    132 | \endisadelimproof
 | 
|  |    133 | %
 | 
|  |    134 | \isatagproof
 | 
| 17175 |    135 | \isacommand{apply}\isamarkupfalse%
 | 
|  |    136 | {\isacharparenleft}simp{\isacharparenright}\isanewline
 | 
|  |    137 | \isacommand{done}\isamarkupfalse%
 | 
|  |    138 | %
 | 
| 17056 |    139 | \endisatagproof
 | 
|  |    140 | {\isafoldproof}%
 | 
|  |    141 | %
 | 
|  |    142 | \isadelimproof
 | 
|  |    143 | %
 | 
|  |    144 | \endisadelimproof
 | 
| 11866 |    145 | %
 | 
| 8749 |    146 | \begin{isamarkuptext}%
 | 
|  |    147 | \noindent
 | 
| 11458 |    148 | Simplification terminates for these proofs because the condition of the \isa{if} simplifies to \isa{True} or \isa{False}.
 | 
| 10795 |    149 | Now we can disable the original simplification rule:%
 | 
| 8749 |    150 | \end{isamarkuptext}%
 | 
| 17175 |    151 | \isamarkuptrue%
 | 
|  |    152 | \isacommand{declare}\isamarkupfalse%
 | 
|  |    153 | \ gcd{\isachardot}simps\ {\isacharbrackleft}simp\ del{\isacharbrackright}\isanewline
 | 
| 17056 |    154 | %
 | 
|  |    155 | \isadelimtheory
 | 
|  |    156 | %
 | 
|  |    157 | \endisadelimtheory
 | 
|  |    158 | %
 | 
|  |    159 | \isatagtheory
 | 
|  |    160 | %
 | 
|  |    161 | \endisatagtheory
 | 
|  |    162 | {\isafoldtheory}%
 | 
|  |    163 | %
 | 
|  |    164 | \isadelimtheory
 | 
|  |    165 | %
 | 
|  |    166 | \endisadelimtheory
 | 
| 9722 |    167 | \end{isabellebody}%
 | 
| 9145 |    168 | %%% Local Variables:
 | 
|  |    169 | %%% mode: latex
 | 
|  |    170 | %%% TeX-master: "root"
 | 
|  |    171 | %%% End:
 |