In mathematics and computer science, a recursive definition, or inductive definition, is used to define the elements in a set in terms of other elements in the set (Aczel 1977:740ff). Some examples of recursively definable objects include factorials, natural numbers, Fibonacci numbers, and the Cantor ternary set.
A recursive definition of a function defines values of the function for some inputs in terms of the values of the same function for other (usually smaller) inputs. For example, the factorial function Template:Math is defined by the rules
- <math>\begin{align}
& 0! = 1. \\ & (n+1)! = (n+1) \cdot n!. \end{align}</math>
This definition is valid for each natural number Template:Mvar, because the recursion eventually reaches the base case of 0. The definition may also be thought of as giving a procedure for computing the value of the function Template:Math, starting from Template:Math and proceeding onwards with Template:Math etc.
The recursion theorem states that such a definition indeed defines a function that is unique. The proof uses mathematical induction.<ref>Template:Cite journal</ref>
An inductive definition of a set describes the elements in a set in terms of other elements in the set. For example, one definition of the set Template:Tmath of natural numbers is:
- 0 is in Template:Tmath
- If an element n is in Template:Tmath then Template:Math is in Template:Tmath
- Template:Tmath is the smallest set satisfying (1) and (2).
There are many sets that satisfy (1) and (2) – for example, the set Template:Math satisfies the definition. However, condition (3) specifies the set of natural numbers by removing the sets with extraneous members.
Properties of recursively defined functions and sets can often be proved by an induction principle that follows the recursive definition. For example, the definition of the natural numbers presented here directly implies the principle of mathematical induction for natural numbers: if a property holds of the natural number 0 (or 1), and the property holds of Template:Math whenever it holds of Template:Mvar, then the property holds of all natural numbers (Aczel 1977:742).
Form of recursive definitionsEdit
Most recursive definitions have two foundations: a base case (basis) and an inductive clause.
The difference between a circular definition and a recursive definition is that a recursive definition must always have base cases, cases that satisfy the definition without being defined in terms of the definition itself, and that all other instances in the inductive clauses must be "smaller" in some sense (i.e., closer to those base cases that terminate the recursion) — a rule also known as "recur only with a simpler case".<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
In contrast, a circular definition may have no base case, and even may define the value of a function in terms of that value itself — rather than on other values of the function. Such a situation would lead to an infinite regress.
That recursive definitions are valid – meaning that a recursive definition identifies a unique function – is a theorem of set theory known as the recursion theorem, the proof of which is non-trivial.<ref>For a proof of Recursion Theorem, see On Mathematical Induction (1960) by Leon Henkin.</ref> Where the domain of the function is the natural numbers, sufficient conditions for the definition to be valid are that the value of Template:Math (i.e., base case) is given, and that for Template:Math, an algorithm is given for determining Template:Math in terms of Template:Mvar, <math>f(0), f(1), \dots, f(n-1)</math> (i.e., inductive clause).
More generally, recursive definitions of functions can be made whenever the domain is a well-ordered set, using the principle of transfinite recursion. The formal criteria for what constitutes a valid recursive definition are more complex for the general case. An outline of the general proof and the criteria can be found in James Munkres' Topology. However, a specific case (domain is restricted to the positive integers instead of any well-ordered set) of the general recursive definition will be given below.<ref name=Munkres>Template:Cite book</ref>
Principle of recursive definitionEdit
Let Template:Mvar be a set and let Template:Math be an element of Template:Mvar. If Template:Mvar is a function which assigns to each function Template:Mvar mapping a nonempty section of the positive integers into Template:Mvar, an element of Template:Mvar, then there exists a unique function <math>h : \Z_+ \to A</math> such that
- <math>\begin{align}
h(1) &= a_0 \\ h(i) &= \rho \left(h|_{\{1,2,\ldots,i-1\}}\right) \text{ for } i>1. \end{align}</math>
Examples of recursive definitionsEdit
Elementary functionsEdit
Addition is defined recursively based on counting as
- <math>\begin{align}
& 0 + a = a, \\ & (1+n) + a = 1 + (n+a). \end{align}</math> Multiplication is defined recursively as
- <math>\begin{align}
& 0 \cdot a = 0, \\ & (1+n) \cdot a = a + n \cdot a. \end{align}</math> Exponentiation is defined recursively as
- <math>\begin{align}
& a^0 = 1, \\ & a^{1+n} = a \cdot a^n. \end{align}</math> Binomial coefficients can be defined recursively as
- <math>\begin{align}
& \binom{a}{0} = 1, \\ & \binom{1+a}{1+n} = \frac{(1+a)\binom{a}{n}}{1+n}. \end{align}</math>
Prime numbersEdit
The set of prime numbers can be defined as the unique set of positive integers satisfying
- 2 is a prime number,
- any other positive integer is a prime number if and only if it is not divisible by any prime number smaller than itself.
The primality of the integer 2 is the base case; checking the primality of any larger integer Template:Mvar by this definition requires knowing the primality of every integer between 2 and Template:Mvar, which is well defined by this definition. That last point can be proved by induction on Template:Mvar, for which it is essential that the second clause says "if and only if"; if it had just said "if", the primality of, for instance, the number 4 would not be clear, and the further application of the second clause would be impossible.
Non-negative even numbersEdit
The even numbers can be defined as consisting of
- 0 is in the set Template:Mvar of non-negative evens (basis clause),
- For any element Template:Mvar in the set Template:Mvar, Template:Math is in Template:Mvar (inductive clause),
- Nothing is in Template:Mvar unless it is obtained from the basis and inductive clauses (extremal clause).
Well formed formulaEdit
The notion of a well-formed formula (wff) in propositional logic is defined recursively as the smallest set satisfying the three rules:
- Template:Math is a wff if Template:Math is a propositional variable.
- Template:Math is a wff if Template:Math is a wff.
- Template:Math is a wff if Template:Math and Template:Math are wffs and • is one of the logical connectives ∨, ∧, →, or ↔.
The definition can be used to determine whether any particular string of symbols is a wff:
- Template:Math is a wff, because the propositional variables Template:Math and Template:Math are wffs and Template:Math is a logical connective.
- Template:Math is a wff, because Template:Math is a wff.
- Template:Math is a wff, because Template:Math and Template:Math are wffs and Template:Math is a logical connective.
Recursive definitions as logic programsEdit
Template:See also Logic programs can be understood as sets of recursive definitions.<ref>Denecker, M., Ternovska, E.: A logic of nonmonotone inductive definitions. ACM Trans. Comput. Log. 9(2), 14:1–14:52 (2008)</ref><ref>Warren, D.S. and Denecker, M., 2023. A better logical semantics for prolog. In Prolog: The Next 50 Years (pp. 82-92). Cham: Springer Nature Switzerland.</ref> For example, the recursive definition of even number can be written as the logic program:
<syntaxhighlight lang="prolog"> even(0). even(s(s(X))) :- even(X). </syntaxhighlight>
Here <syntaxhighlight lang="prolog" inline>:-</syntaxhighlight> represents if, and <syntaxhighlight lang="prolog" inline>s(X)</syntaxhighlight> represents the successor of <syntaxhighlight lang="prolog" inline>X</syntaxhighlight>, namely <syntaxhighlight lang="prolog" inline>X+1</syntaxhighlight>, as in Peano arithmetic.
The logic programming language Prolog uses backward reasoning to solve goals and answer queries. For example, given the query <syntaxhighlight lang="prolog" inline>?- even(s(s(0)))</syntaxhighlight> it produces the answer <syntaxhighlight lang="prolog" inline>true</syntaxhighlight>. Given the query <syntaxhighlight lang="prolog" inline>?- even(s(0))</syntaxhighlight> it produces the answer <syntaxhighlight lang="prolog" inline>false</syntaxhighlight>.
The program can be used not only to check whether a query is true, but also to generate answers that are true. For example:
<syntaxhighlight lang="prolog"> ?- even(X). X = 0 X = s(s(0)) X = s(s(s(s(0)))) X = s(s(s(s(s(s(0)))))) ..... </syntaxhighlight>
Logic programs significantly extend recursive definitions by including the use of negative conditions, implemented by negation as failure, as in the definition:
<syntaxhighlight lang="prolog"> even(0). even(s(X)) :- not(even(X)). </syntaxhighlight>
See alsoEdit
- Definition
- Logic programming
- Mathematical induction
- Recursive data types
- Recursion
- Recursion (computer science)
- Structural induction