doc-src/TutorialI/Recdef/termination.thy
author nipkow
Thu, 29 Nov 2001 21:12:37 +0100
changeset 12332 aea72a834c85
parent 11636 0bec857c9871
child 12473 f41e477576b9
permissions -rw-r--r--
*** empty log message ***
nipkow@8745
     1
(*<*)
nipkow@9792
     2
theory termination = examples:
nipkow@8745
     3
(*>*)
nipkow@8745
     4
nipkow@8745
     5
text{*
paulson@11309
     6
When a function~$f$ is defined via \isacommand{recdef}, Isabelle tries to prove
paulson@11309
     7
its termination with the help of the user-supplied measure.  Each of the examples
paulson@11309
     8
above is simple enough that Isabelle can automatically prove that the
paulson@11309
     9
argument's measure decreases in each recursive call. As a result,
nipkow@9792
    10
$f$@{text".simps"} will contain the defining equations (or variants derived
nipkow@9792
    11
from them) as theorems. For example, look (via \isacommand{thm}) at
nipkow@9792
    12
@{thm[source]sep.simps} and @{thm[source]sep1.simps} to see that they define
nipkow@9792
    13
the same function. What is more, those equations are automatically declared as
nipkow@8745
    14
simplification rules.
nipkow@8745
    15
paulson@11458
    16
Isabelle may fail to prove the termination condition for some
paulson@11458
    17
recursive call.  Let us try the following artificial function:*}
nipkow@8745
    18
wenzelm@11626
    19
consts f :: "nat\<times>nat \<Rightarrow> nat"
wenzelm@11636
    20
recdef (*<*)(permissive)(*>*)f "measure(\<lambda>(x,y). x-y)"
wenzelm@11626
    21
  "f(x,y) = (if x \<le> y then x else f(x,y+1))"
nipkow@8745
    22
nipkow@12332
    23
text{*\noindent This definition fails, and Isabelle prints an error message
nipkow@12332
    24
showing you what it was unable to prove. You will then have to prove it as a
nipkow@12332
    25
separate lemma before you attempt the definition of your function once
nipkow@12332
    26
more. In our case the required lemma is the obvious one: *}
nipkow@8745
    27
wenzelm@11626
    28
lemma termi_lem: "\<not> x \<le> y \<Longrightarrow> x - Suc y < x - y"
nipkow@8745
    29
nipkow@8745
    30
txt{*\noindent
paulson@11458
    31
It was not proved automatically because of the awkward behaviour of subtraction
paulson@11458
    32
on type @{typ"nat"}. This requires more arithmetic than is tried by default:
nipkow@8745
    33
*}
nipkow@8745
    34
wenzelm@11626
    35
apply(arith)
nipkow@10171
    36
done
nipkow@8745
    37
nipkow@8745
    38
text{*\noindent
nipkow@8771
    39
Because \isacommand{recdef}'s termination prover involves simplification,
paulson@11429
    40
we include in our second attempt a hint: the \attrdx{recdef_simp} attribute 
nipkow@12332
    41
says to use @{thm[source]termi_lem} as a simplification rule.  
nipkow@8745
    42
*}
nipkow@8745
    43
nipkow@12332
    44
(*<*)global consts f :: "nat\<times>nat \<Rightarrow> nat" (*>*)
nipkow@12332
    45
recdef f "measure(\<lambda>(x,y). x-y)"
nipkow@12332
    46
  "f(x,y) = (if x \<le> y then x else f(x,y+1))"
nipkow@9992
    47
(hints recdef_simp: termi_lem)
nipkow@12332
    48
(*<*)local(*>*)
nipkow@8745
    49
text{*\noindent
nipkow@12332
    50
This time everything works fine. Now @{thm[source]f.simps} contains precisely
nipkow@12332
    51
the stated recursion equation for @{term f}, which has been stored as a
paulson@11458
    52
simplification rule.  Thus we can automatically prove results such as this one:
nipkow@8745
    53
*}
nipkow@8745
    54
nipkow@12332
    55
theorem "f(1,0) = f(1,1)"
wenzelm@11626
    56
apply(simp)
nipkow@10171
    57
done
nipkow@8745
    58
nipkow@8745
    59
text{*\noindent
nipkow@8745
    60
More exciting theorems require induction, which is discussed below.
nipkow@8745
    61
nipkow@9933
    62
If the termination proof requires a new lemma that is of general use, you can
nipkow@9933
    63
turn it permanently into a simplification rule, in which case the above
nipkow@9933
    64
\isacommand{hint} is not necessary. But our @{thm[source]termi_lem} is not
nipkow@9933
    65
sufficiently general to warrant this distinction.
nipkow@8745
    66
*}
nipkow@8745
    67
(*<*)
nipkow@8745
    68
end
nipkow@8745
    69
(*>*)