doc-src/TutorialI/Advanced/WFrec.thy
author Walther Neuper <neuper@ist.tugraz.at>
Thu, 12 Aug 2010 15:03:34 +0200
branchisac-from-Isabelle2009-2
changeset 37913 20e3616b2d9c
parent 20217 25b068a99d2b
permissions -rw-r--r--
prepare reactivation of isac-update-Isa09-2
wenzelm@17914
     1
(*<*)theory WFrec imports Main begin(*>*)
nipkow@10187
     2
nipkow@10187
     3
text{*\noindent
nipkow@11161
     4
So far, all recursive definitions were shown to terminate via measure
paulson@11494
     5
functions. Sometimes this can be inconvenient or
nipkow@10187
     6
impossible. Fortunately, \isacommand{recdef} supports much more
nipkow@10187
     7
general definitions. For example, termination of Ackermann's function
nipkow@10654
     8
can be shown by means of the \rmindex{lexicographic product} @{text"<*lex*>"}:
nipkow@10187
     9
*}
nipkow@10187
    10
wenzelm@11626
    11
consts ack :: "nat\<times>nat \<Rightarrow> nat"
nipkow@10187
    12
recdef ack "measure(\<lambda>m. m) <*lex*> measure(\<lambda>n. n)"
nipkow@10187
    13
  "ack(0,n)         = Suc n"
nipkow@10187
    14
  "ack(Suc m,0)     = ack(m, 1)"
wenzelm@11626
    15
  "ack(Suc m,Suc n) = ack(m,ack(Suc m,n))"
nipkow@10187
    16
nipkow@10187
    17
text{*\noindent
nipkow@10187
    18
The lexicographic product decreases if either its first component
nipkow@10187
    19
decreases (as in the second equation and in the outer call in the
nipkow@10187
    20
third equation) or its first component stays the same and the second
nipkow@10187
    21
component decreases (as in the inner call in the third equation).
nipkow@10187
    22
nipkow@10187
    23
In general, \isacommand{recdef} supports termination proofs based on
nipkow@10396
    24
arbitrary well-founded relations as introduced in \S\ref{sec:Well-founded}.
nipkow@10396
    25
This is called \textbf{well-founded
paulson@11494
    26
recursion}\indexbold{recursion!well-founded}.  A function definition
paulson@11494
    27
is total if and only if the set of 
paulson@11494
    28
all pairs $(r,l)$, where $l$ is the argument on the
nipkow@10396
    29
left-hand side of an equation and $r$ the argument of some recursive call on
nipkow@10396
    30
the corresponding right-hand side, induces a well-founded relation.  For a
nipkow@10396
    31
systematic account of termination proofs via well-founded relations see, for
paulson@10885
    32
example, Baader and Nipkow~\cite{Baader-Nipkow}.
nipkow@10187
    33
paulson@11494
    34
Each \isacommand{recdef} definition should be accompanied (after the function's
paulson@11494
    35
name) by a well-founded relation on the function's argument type.  
paulson@11494
    36
Isabelle/HOL formalizes some of the most important
nipkow@10396
    37
constructions of well-founded relations (see \S\ref{sec:Well-founded}). For
paulson@11494
    38
example, @{term"measure f"} is always well-founded.   The lexicographic
nipkow@10396
    39
product of two well-founded relations is again well-founded, which we relied
nipkow@10396
    40
on when defining Ackermann's function above.
nipkow@11308
    41
Of course the lexicographic product can also be iterated:
nipkow@10189
    42
*}
nipkow@10187
    43
nipkow@10189
    44
consts contrived :: "nat \<times> nat \<times> nat \<Rightarrow> nat"
nipkow@10189
    45
recdef contrived
nipkow@10189
    46
  "measure(\<lambda>i. i) <*lex*> measure(\<lambda>j. j) <*lex*> measure(\<lambda>k. k)"
nipkow@10189
    47
"contrived(i,j,Suc k) = contrived(i,j,k)"
nipkow@10189
    48
"contrived(i,Suc j,0) = contrived(i,j,j)"
nipkow@10189
    49
"contrived(Suc i,0,0) = contrived(i,i,i)"
nipkow@10189
    50
"contrived(0,0,0)     = 0"
nipkow@10189
    51
nipkow@10189
    52
text{*
nipkow@10396
    53
Lexicographic products of measure functions already go a long
paulson@10885
    54
way. Furthermore, you may embed a type in an
nipkow@10396
    55
existing well-founded relation via the inverse image construction @{term
nipkow@10396
    56
inv_image}. All these constructions are known to \isacommand{recdef}. Thus you
paulson@10241
    57
will never have to prove well-foundedness of any relation composed
nipkow@10189
    58
solely of these building blocks. But of course the proof of
paulson@11494
    59
termination of your function definition --- that the arguments
paulson@11494
    60
decrease with every recursive call --- may still require you to provide
nipkow@10189
    61
additional lemmas.
nipkow@10189
    62
paulson@10841
    63
It is also possible to use your own well-founded relations with
paulson@10841
    64
\isacommand{recdef}.  For example, the greater-than relation can be made
paulson@10841
    65
well-founded by cutting it off at a certain point.  Here is an example
paulson@10841
    66
of a recursive function that calls itself with increasing values up to ten:
nipkow@10187
    67
*}
nipkow@10189
    68
nipkow@10189
    69
consts f :: "nat \<Rightarrow> nat"
wenzelm@11705
    70
recdef (*<*)(permissive)(*>*)f "{(i,j). j<i \<and> i \<le> (10::nat)}"
wenzelm@11705
    71
"f i = (if 10 \<le> i then 0 else i * f(Suc i))"
nipkow@10189
    72
nipkow@10396
    73
text{*\noindent
paulson@10841
    74
Since \isacommand{recdef} is not prepared for the relation supplied above,
paulson@10841
    75
Isabelle rejects the definition.  We should first have proved that
paulson@10841
    76
our relation was well-founded:
nipkow@10189
    77
*}
nipkow@10189
    78
paulson@10841
    79
lemma wf_greater: "wf {(i,j). j<i \<and> i \<le> (N::nat)}"
paulson@10841
    80
nipkow@11196
    81
txt{*\noindent
paulson@10841
    82
The proof is by showing that our relation is a subset of another well-founded
paulson@11494
    83
relation: one given by a measure function.\index{*wf_subset (theorem)}
wenzelm@11626
    84
*}
paulson@10841
    85
wenzelm@11626
    86
apply (rule wf_subset [of "measure (\<lambda>k::nat. N-k)"], blast)
paulson@10841
    87
paulson@10841
    88
txt{*
paulson@10841
    89
@{subgoals[display,indent=0,margin=65]}
paulson@10841
    90
paulson@10841
    91
\noindent
paulson@10841
    92
The inclusion remains to be proved. After unfolding some definitions, 
webertj@20217
    93
we are left with simple arithmetic that is dispatched automatically.
wenzelm@11626
    94
*}
paulson@10841
    95
webertj@20217
    96
by (clarify, simp add: measure_def inv_image_def)
nipkow@10189
    97
nipkow@10189
    98
text{*\noindent
paulson@10841
    99
paulson@11429
   100
Armed with this lemma, we use the \attrdx{recdef_wf} attribute to attach a
nipkow@13111
   101
crucial hint\cmmdx{hints} to our definition:
nipkow@10189
   102
*}
nipkow@10189
   103
(*<*)
nipkow@10189
   104
consts g :: "nat \<Rightarrow> nat"
wenzelm@11705
   105
recdef g "{(i,j). j<i \<and> i \<le> (10::nat)}"
wenzelm@11705
   106
"g i = (if 10 \<le> i then 0 else i * g(Suc i))"
nipkow@10189
   107
(*>*)
wenzelm@11626
   108
(hints recdef_wf: wf_greater)
paulson@10841
   109
paulson@10841
   110
text{*\noindent
wenzelm@11705
   111
Alternatively, we could have given @{text "measure (\<lambda>k::nat. 10-k)"} for the
paulson@10841
   112
well-founded relation in our \isacommand{recdef}.  However, the arithmetic
paulson@10841
   113
goal in the lemma above would have arisen instead in the \isacommand{recdef}
paulson@10841
   114
termination proof, where we have less control.  A tailor-made termination
paulson@10841
   115
relation makes even more sense when it can be used in several function
paulson@10841
   116
declarations.
paulson@10841
   117
*}
paulson@10841
   118
nipkow@10396
   119
(*<*)end(*>*)