1961 |
1961 |
1962 \index{recursion!primitive|)} |
1962 \index{recursion!primitive|)} |
1963 \index{*primrec|)} |
1963 \index{*primrec|)} |
1964 |
1964 |
1965 |
1965 |
1966 \subsection{Well-founded recursive functions} |
1966 \subsection{General recursive functions} |
1967 \label{sec:HOL:recdef} |
1967 \label{sec:HOL:recdef} |
1968 \index{recursion!general|(} |
1968 \index{recursion!general|(} |
1969 \index{*recdef|(} |
1969 \index{*recdef|(} |
1970 |
1970 |
1971 Well-founded recursion can express any function whose termination can be |
1971 Using \texttt{recdef}, you can declare functions involving nested recursion |
1972 proved by showing that each recursive call makes the argument smaller in a |
1972 and pattern-matching. Recursion need not involve datatypes and there are few |
1973 suitable sense. The recursion need not involve datatypes and there are few |
1973 syntactic restrictions. Termination is proved by showing that each recursive |
1974 syntactic restrictions. Nested recursion and pattern-matching are allowed. |
1974 call makes the argument smaller in a suitable sense, which you specify by |
|
1975 supplying a well-founded relation. |
1975 |
1976 |
1976 Here is a simple example, the Fibonacci function. The first line declares |
1977 Here is a simple example, the Fibonacci function. The first line declares |
1977 \texttt{fib} to be a constant. The well-founded relation is simply~$<$ (on |
1978 \texttt{fib} to be a constant. The well-founded relation is simply~$<$ (on |
1978 the natural numbers). Pattern-matching is used here: \texttt{1} is a |
1979 the natural numbers). Pattern-matching is used here: \texttt{1} is a |
1979 macro for \texttt{Suc~0}. |
1980 macro for \texttt{Suc~0}. |
1987 |
1988 |
1988 With \texttt{recdef}, function definitions may be incomplete, and patterns may |
1989 With \texttt{recdef}, function definitions may be incomplete, and patterns may |
1989 overlap, as in functional programming. The \texttt{recdef} package |
1990 overlap, as in functional programming. The \texttt{recdef} package |
1990 disambiguates overlapping patterns by taking the order of rules into account. |
1991 disambiguates overlapping patterns by taking the order of rules into account. |
1991 For missing patterns, the function is defined to return an arbitrary value. |
1992 For missing patterns, the function is defined to return an arbitrary value. |
|
1993 For example, here is a declaration of the list function \cdx{hd}: |
|
1994 \begin{ttbox} |
|
1995 consts hd :: 'a list => 'a |
|
1996 recdef hd "\{\}" |
|
1997 "hd (x#l) = x" |
|
1998 \end{ttbox} |
|
1999 Because this function is not recursive, we may supply the empty well-founded |
|
2000 relation, $\{\}$. |
1992 |
2001 |
1993 The well-founded relation defines a notion of ``smaller'' for the function's |
2002 The well-founded relation defines a notion of ``smaller'' for the function's |
1994 argument type. The relation $\prec$ is \textbf{well-founded} provided it |
2003 argument type. The relation $\prec$ is \textbf{well-founded} provided it |
1995 admits no infinitely decreasing chains |
2004 admits no infinitely decreasing chains |
1996 \[ \cdots\prec x@n\prec\cdots\prec x@1. \] |
2005 \[ \cdots\prec x@n\prec\cdots\prec x@1. \] |