doc-src/TutorialI/Recdef/termination.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 19593 c52a4360a41d
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
nipkow@8745
     1
(*<*)
wenzelm@19593
     2
theory "termination" imports examples begin
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
nipkow@12489
    17
recursive call.  Let us try to define Quicksort:*}
nipkow@8745
    18
nipkow@12489
    19
consts qs :: "nat list \<Rightarrow> nat list"
nipkow@12489
    20
recdef(*<*)(permissive)(*>*) qs "measure length"
nipkow@12489
    21
 "qs [] = []"
nipkow@12489
    22
 "qs(x#xs) = qs(filter (\<lambda>y. y\<le>x) xs) @ [x] @ qs(filter (\<lambda>y. x<y) xs)"
nipkow@8745
    23
nipkow@12489
    24
text{*\noindent where @{term filter} is predefined and @{term"filter P xs"}
nipkow@12489
    25
is the list of elements of @{term xs} satisfying @{term P}.
nipkow@12489
    26
This definition of @{term qs} fails, and Isabelle prints an error message
nipkow@12489
    27
showing you what it was unable to prove:
nipkow@12489
    28
@{text[display]"length (filter ... xs) < Suc (length xs)"}
nipkow@12489
    29
We can either prove this as a separate lemma, or try to figure out which
nipkow@12489
    30
existing lemmas may help. We opt for the second alternative. The theory of
paulson@15270
    31
lists contains the simplification rule @{thm length_filter_le[no_vars]},
paulson@15270
    32
which is what we need, provided we turn \mbox{@{text"< Suc"}}
nipkow@12489
    33
into
paulson@15270
    34
@{text"\<le>"} so that the rule applies. Lemma
nipkow@12489
    35
@{thm[source]less_Suc_eq_le} does just that: @{thm less_Suc_eq_le[no_vars]}.
nipkow@8745
    36
nipkow@12489
    37
Now we retry the above definition but supply the lemma(s) just found (or
nipkow@12489
    38
proved). Because \isacommand{recdef}'s termination prover involves
nipkow@12489
    39
simplification, we include in our second attempt a hint: the
nipkow@12489
    40
\attrdx{recdef_simp} attribute says to use @{thm[source]less_Suc_eq_le} as a
nipkow@13111
    41
simplification rule.\cmmdx{hints}  *}
nipkow@8745
    42
nipkow@12489
    43
(*<*)global consts qs :: "nat list \<Rightarrow> nat list" (*>*)
nipkow@12489
    44
recdef qs "measure length"
nipkow@12489
    45
 "qs [] = []"
nipkow@12489
    46
 "qs(x#xs) = qs(filter (\<lambda>y. y\<le>x) xs) @ [x] @ qs(filter (\<lambda>y. x<y) xs)"
nipkow@12489
    47
(hints recdef_simp: less_Suc_eq_le)
nipkow@12489
    48
(*<*)local(*>*)
nipkow@12489
    49
text{*\noindent
nipkow@12489
    50
This time everything works fine. Now @{thm[source]qs.simps} contains precisely
nipkow@12489
    51
the stated recursion equations for @{text qs} and they have become
nipkow@12489
    52
simplification rules.
nipkow@12489
    53
Thus we can automatically prove results such as this one:
nipkow@8745
    54
*}
nipkow@8745
    55
nipkow@12489
    56
theorem "qs[2,3,0] = qs[3,0,2]"
wenzelm@11626
    57
apply(simp)
nipkow@10171
    58
done
nipkow@8745
    59
nipkow@8745
    60
text{*\noindent
nipkow@8745
    61
More exciting theorems require induction, which is discussed below.
nipkow@8745
    62
nipkow@12489
    63
If the termination proof requires a lemma that is of general use, you can
nipkow@9933
    64
turn it permanently into a simplification rule, in which case the above
nipkow@12489
    65
\isacommand{hint} is not necessary. But in the case of
nipkow@12489
    66
@{thm[source]less_Suc_eq_le} this would be of dubious value.
nipkow@8745
    67
*}
nipkow@8745
    68
(*<*)
nipkow@8745
    69
end
nipkow@8745
    70
(*>*)