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