doc-src/TutorialI/Recdef/document/simplification.tex
author wenzelm
Sun, 21 Oct 2001 19:49:29 +0200
changeset 11866 fbd097aec213
parent 11706 885e053ae664
child 12473 f41e477576b9
permissions -rw-r--r--
updated;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     1
%
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     2
\begin{isabellebody}%
9924
3370f6aa3200 updated;
wenzelm
parents: 9834
diff changeset
     3
\def\isabellecontext{simplification}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
     4
\isamarkupfalse%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     5
%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     6
\begin{isamarkuptext}%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
     7
Once we have proved all the termination conditions, the \isacommand{recdef} 
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
     8
recursion equations become simplification rules, just as with
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     9
\isacommand{primrec}. In most cases this works fine, but there is a subtle
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    10
problem that must be mentioned: simplification may not
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    11
terminate because of automatic splitting of \isa{if}.
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    12
\index{*if expressions!splitting of}
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    13
Let us look at an example:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    14
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    15
\isamarkuptrue%
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9924
diff changeset
    16
\isacommand{consts}\ gcd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    17
\isamarkupfalse%
9674
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    18
\isacommand{recdef}\ gcd\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    19
\ \ {\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}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    20
%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    21
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    22
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    23
According to the measure function, the second argument should decrease with
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    24
each recursive call. The resulting termination condition
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    25
\begin{isabelle}%
11706
885e053ae664 *** empty log message ***
wenzelm
parents: 11458
diff changeset
    26
\ \ \ \ \ n\ {\isasymnoteq}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}{\isacharprime}a{\isacharparenright}\ {\isasymLongrightarrow}\ m\ mod\ n\ {\isacharless}\ n%
9924
3370f6aa3200 updated;
wenzelm
parents: 9834
diff changeset
    27
\end{isabelle}
10878
b254d5ad6dd4 auto update
paulson
parents: 10795
diff changeset
    28
is proved automatically because it is already present as a lemma in
b254d5ad6dd4 auto update
paulson
parents: 10795
diff changeset
    29
HOL\@.  Thus the recursion equation becomes a simplification
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    30
rule. Of course the equation is nonterminating if we are allowed to unfold
10171
59d6633835fa *** empty log message ***
nipkow
parents: 9933
diff changeset
    31
the recursive call inside the \isa{else} branch, which is why programming
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    32
languages and our simplifier don't do that. Unfortunately the simplifier does
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    33
something else that leads to the same problem: it splits 
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    34
each \isa{if}-expression unless its
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    35
condition simplifies to \isa{True} or \isa{False}.  For
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    36
example, simplification reduces
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    37
\begin{isabelle}%
9834
109b11c4e77e *** empty log message ***
nipkow
parents: 9792
diff changeset
    38
\ \ \ \ \ gcd\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ k%
9924
3370f6aa3200 updated;
wenzelm
parents: 9834
diff changeset
    39
\end{isabelle}
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    40
in one step to
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    41
\begin{isabelle}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    42
\ \ \ \ \ {\isacharparenleft}if\ n\ {\isacharequal}\ {\isadigit{0}}\ then\ m\ else\ gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ k%
9924
3370f6aa3200 updated;
wenzelm
parents: 9834
diff changeset
    43
\end{isabelle}
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    44
where the condition cannot be reduced further, and splitting leads to
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    45
\begin{isabelle}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    46
\ \ \ \ \ {\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
3370f6aa3200 updated;
wenzelm
parents: 9834
diff changeset
    47
\end{isabelle}
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9754
diff changeset
    48
Since the recursive call \isa{gcd\ {\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}} is no longer protected by
9541
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    49
an \isa{if}, it is unfolded again, which leads to an infinite chain of
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    50
simplification steps. Fortunately, this problem can be avoided in many
d17c0b34d5c8 *** empty log message ***
nipkow
parents: 9458
diff changeset
    51
different ways.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    52
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    53
The most radical solution is to disable the offending theorem
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    54
\isa{split{\isacharunderscore}if},
11215
b44ad7e4c4d2 *** empty log message ***
nipkow
parents: 10878
diff changeset
    55
as shown in \S\ref{sec:AutoCaseSplits}.  However, we do not recommend this
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    56
approach: you will often have to invoke the rule explicitly when
11215
b44ad7e4c4d2 *** empty log message ***
nipkow
parents: 10878
diff changeset
    57
\isa{if} is involved.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    58
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    59
If possible, the definition should be given by pattern matching on the left
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    60
rather than \isa{if} on the right. In the case of \isa{gcd} the
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    61
following alternative definition suggests itself:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    62
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    63
\isamarkuptrue%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    64
\isacommand{consts}\ gcd{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    65
\isamarkupfalse%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    66
\isacommand{recdef}\ gcd{\isadigit{1}}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    67
\ \ {\isachardoublequote}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    68
\ \ {\isachardoublequote}gcd{\isadigit{1}}\ {\isacharparenleft}m{\isacharcomma}\ n{\isacharparenright}\ {\isacharequal}\ gcd{\isadigit{1}}{\isacharparenleft}n{\isacharcomma}\ m\ mod\ n{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    69
%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    70
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    71
\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    72
The order of equations is important: it hides the side condition
11706
885e053ae664 *** empty log message ***
wenzelm
parents: 11458
diff changeset
    73
\isa{n\ {\isasymnoteq}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}{\isacharprime}a{\isacharparenright}}.  Unfortunately, in general the case distinction
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    74
may not be expressible by pattern matching.
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    75
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    76
A simple alternative is to replace \isa{if} by \isa{case}, 
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    77
which is also available for \isa{bool} and is not split automatically:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    78
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    79
\isamarkuptrue%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    80
\isacommand{consts}\ gcd{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat{\isasymtimes}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    81
\isamarkupfalse%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    82
\isacommand{recdef}\ gcd{\isadigit{2}}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}n{\isacharparenright}{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    83
\ \ {\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}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    84
%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    85
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    86
\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    87
This is probably the neatest solution next to pattern matching, and it is
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
    88
always available.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    89
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    90
A final alternative is to replace the offending simplification rules by
10795
9e888d60d3e5 minor edits to Chapters 1-3
paulson
parents: 10187
diff changeset
    91
derived conditional ones. For \isa{gcd} it means we have to prove
9e888d60d3e5 minor edits to Chapters 1-3
paulson
parents: 10187
diff changeset
    92
these lemmas:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    93
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    94
\isamarkuptrue%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
    95
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\ {\isachardoublequote}gcd\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ m{\isachardoublequote}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    96
\isamarkupfalse%
10171
59d6633835fa *** empty log message ***
nipkow
parents: 9933
diff changeset
    97
\isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
    98
\isamarkupfalse%
10171
59d6633835fa *** empty log message ***
nipkow
parents: 9933
diff changeset
    99
\isacommand{done}\isanewline
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
   100
\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   101
\isamarkupfalse%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 10171
diff changeset
   102
\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
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   103
\isamarkupfalse%
10171
59d6633835fa *** empty log message ***
nipkow
parents: 9933
diff changeset
   104
\isacommand{apply}{\isacharparenleft}simp{\isacharparenright}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   105
\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   106
\isacommand{done}\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   107
%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   108
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   109
\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11215
diff changeset
   110
Simplification terminates for these proofs because the condition of the \isa{if} simplifies to \isa{True} or \isa{False}.
10795
9e888d60d3e5 minor edits to Chapters 1-3
paulson
parents: 10187
diff changeset
   111
Now we can disable the original simplification rule:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
   112
\end{isamarkuptext}%
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   113
\isamarkuptrue%
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9924
diff changeset
   114
\isacommand{declare}\ gcd{\isachardot}simps\ {\isacharbrackleft}simp\ del{\isacharbrackright}\isanewline
11866
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   115
\isamarkupfalse%
fbd097aec213 updated;
wenzelm
parents: 11706
diff changeset
   116
\isamarkupfalse%
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
   117
\end{isabellebody}%
9145
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   118
%%% Local Variables:
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   119
%%% mode: latex
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   120
%%% TeX-master: "root"
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
   121
%%% End: