In computability theory, a primitive recursive function is, roughly speaking, a function that can be computed by a computer program whose loops are all "for" loops (that is, an upper bound of the number of iterations of every loop is fixed before entering the loop). Primitive recursive functions form a strict subset of those general recursive functions that are also total functions.
The importance of primitive recursive functions lies in the fact that most computable functions that are studied in number theory (and more generally in mathematics) are primitive recursive. For example, addition and division, the factorial and exponential function, and the function which returns the nth prime are all primitive recursive.[1] In fact, for showing that a computable function is primitive recursive, it suffices to show that its time complexity is bounded above by a primitive recursive function of the input size.[2] It is hence not particularly easy to devise a computable function that is not primitive recursive; some examples are shown in section below.
The set of primitive recursive functions is known as PR in computational complexity theory.
A primitive recursive function takes a fixed number of arguments, each a natural number (nonnegative integer:), and returns a natural number. If it takes n arguments it is called n-ary.
The basic primitive recursive functions are given by these axioms:
More complex primitive recursive functions can be obtained by applying the operations given by these axioms:
The primitive recursive functions are the basic functions and those obtained from the basic functions by applying these operations a finite number of times.
A definition of the 2-ary function
Add
\rho
\begin{array}{rcll} 0+y&=&y&and\\ S(x)+y&=&S(x+y)&.\\ \end{array}
\rho(g,h)
g=
1 | |
P | |
1 |
Add(0,y)=g(y)=y
h=S\circ
3 | |
P | |
2 |
Add(S(x),y)=h(x,Add(x,y),y)=(S\circ
3)(x,Add(x,y),y) | |
P | |
2 |
=S(Add(x,y))
Add=
1,S | |
\rho(P | |
1 |
\circ
3) | |
P | |
2 |
\begin{array}{lll} &Add(1,7)\\ =&
1,S | |
\rho(P | |
1 |
\circ
3) | |
P | |
2 |
(S(0),7)&byDef.Add,S\\ =&(S\circ
3)(0,Add(0,7),7) | |
P | |
2 |
&bycase\rho(g,h) (S(...),...)\\ =&S(Add(0,7))&byDef.\circ,
3 | |
P | |
2 |
\\ =&S(
1,S | |
\rho(P | |
1 |
\circ
3) | |
P | |
2 |
(0,7) )&byDef.Add\\ =&
1(7)) | |
S(P | |
1 |
&bycase\rho(g,h) (0,...)\\ =&S(7)&byDef.
1 | |
P | |
1 |
\\ =&8&byDef.S.\ \end{array}
Given
Add
Add\circ
1) | |
(P | |
1 |
(Add\circ
1))(x) | |
(P | |
1 |
=Add(x,x)=x+x
In a similar way as addition, multiplication can be defined by
Mul=
1,Add | |
\rho(C | |
0 |
3)) | |
\circ(P | |
3 |
\begin{array}{lll} &Mul(0,y)\\ =&
1,Add | |
\rho(C | |
0 |
3)) | |
\circ(P | |
3 |
(0,y)&byDef.Mul\\ =&
1(y) | |
C | |
0 |
&bycase\rho(g,h) (0,...)\\ =&0&byDef.
1 | |
C | |
0 |
.\\ \end{array}
\begin{array}{lll} &Mul(S(x),y)\\ =&
1,Add | |
\rho(C | |
0 |
3)) | |
\circ(P | |
3 |
(S(x),y)&byDef.Mul\\ =&(Add
3)) | |
\circ(P | |
3 |
(x,Mul(x,y),y)&bycase\rho(g,h) (S(...),...)\\ =&Add(Mul(x,y),y)&byDef.\circ,
3, | |
P | |
2 |
3 | |
P | |
3 |
\\ =&Mul(x,y)+y&bypropertyofAdd.\\ \end{array}
The predecessor function acts as the "opposite" of the successor function and is recursively defined by the rules
Pred(0)=0
Pred(S(n))=n
Pred=
0, | |
\rho(C | |
0 |
2) | |
P | |
1 |
\begin{array}{lll} &Pred(8)\\ =&
0, | |
\rho(C | |
0 |
2) | |
P | |
1 |
(S(7))&byDef.Pred,S\\ =&
2(7,Pred(7)) | |
P | |
1 |
&bycase\rho(g,h) (S(...),...)\\ =&7&byDef.
2 | |
P | |
1 |
.\\ \end{array}
The limited subtraction function (also called "monus", and denoted "
\stackrel.-
\begin{array}{rcll} y\stackrel.-0&=&y&and\\ y\stackrel.-S(x)&=&Pred(y\stackrel.-x)&.\\ \end{array}
RSub(y,x)=x\stackrel.-y
RSub=
1, | |
\rho(P | |
1 |
Pred\circ
3) | |
P | |
2 |
Sub=RSub\circ
2) | |
(P | |
1 |
\begin{array}{lll} &Sub(8,1)\\ =&(RSub\circ
2)) | |
(P | |
1 |
(8,1)&byDef.Sub\\ =&RSub(1,8)&byDef.\circ,
2, | |
P | |
2 |
2 | |
P | |
1 |
\\ =&
1, | |
\rho(P | |
1 |
Pred\circ
3) | |
P | |
2 |
(S(0),8)&byDef.RSub,S\\ =&(Pred\circ
3) | |
P | |
2 |
(0,RSub(0,8),8)&bycase\rho(g,h) (S(...),...)\\ =&Pred(RSub(0,8))&byDef.\circ,
3 | |
P | |
2 |
\\ =&Pred(
1, | |
\rho(P | |
1 |
Pred\circ
3) | |
P | |
2 |
(0,8) )&byDef.RSub\\ =&
1(8)) | |
Pred(P | |
1 |
&bycase\rho(g,h) (0,...)\\ =&Pred(8)&byDef.
1 | |
P | |
1 |
\\ =&7&bypropertyofPred.\\ \end{array}
In some settings it is natural to consider primitive recursive functions that take as inputs tuples that mix numbers with truth values (that is
t
f
t
1
f
0
A
1
0
A
As an example for a primitive recursive predicate, the 1-ary function
IsZero
IsZero(x)=1
x=0
IsZero(x)=0
IsZero=
2) | |
\rho(C | |
0 |
IsZero(0)=
2)(0) | |
\rho(C | |
0 |
=
0(0) | |
C | |
1 |
=1
IsZero(8)=
2)(S(7)) | |
\rho(C | |
0 |
=
2(7,IsZero(7)) | |
C | |
0 |
=0
Using the property
x\leqy\iffx\stackrel.-y=0
Leq
Leq=IsZero\circSub
Leq(x,y)=1
x\leqy
Leq(x,y)=0
\begin{array}{lll} &Leq(8,3)\\ =&IsZero(Sub(8,3))&byDef.Leq\\ =&IsZero(5)&bypropertyofSub\\ =&0&bypropertyofIsZero\\ \end{array}
Once a definition of
Leq
Geq=Leq\circ
2) | |
(P | |
1 |
Geq(x,y)=Leq(y,x)
x\geqy
The 3-ary if-then-else operator known from programming languages can be defined by
it{If}=
4) | |
\rho(P | |
3 |
x
\begin{array}{lll} &it{If}(S(x),y,z)\\ =&
4) | |
\rho(P | |
3 |
(S(x),y,z)&byDef.it{If}\\ =&
4(x,it{If}(x,y,z),y,z)& | |
P | |
3 |
bycase\rho(S(...),...)\\ =&y&byDef.
4 | |
P | |
3 |
\\ \end{array}
\begin{array}{lll} &it{If}(0,y,z)\\ =&
4) | |
\rho(P | |
3 |
(0,y,z)&byDef.it{If}\\ =&
2(y,z) | |
P | |
2 |
&bycase\rho(0,...)\\ =&z&byDef.
2 | |
P | |
2 |
.\\ \end{array}
it{If}(x,y,z)
y
x
z
Based on the
it{If}
And=it{If}\circ
2) | |
(P | |
0 |
And(x,y)=it{If}(x,y,0)
And(x,y)
x
y
x
y
Similarly,
Or=it{If}\circ
2) | |
(P | |
2 |
Not=it{If}\circ
1) | |
(P | |
1 |
Or(x,y)=it{If}(x,1,y)
Not(x)=it{If}(x,0,1)
Using the above functions
Leq
Geq
And
Eq=And\circ(Leq,Geq)
Eq(x,y)=And(Leq(x,y),Geq(x,y))
x
y
Similarly, the definition
Lt=Not\circGeq
Gt=Not\circLeq
Exponentiation and primality testing are primitive recursive. Given primitive recursive functions
e
f
g
h
g
e\leqf
h
By using Gödel numberings, the primitive recursive functions can be extended to operate on other objects such as integers and rational numbers. If integers are encoded by Gödel numbers in a standard way, the arithmetic operations including addition, subtraction, and multiplication are all primitive recursive. Similarly, if the rationals are represented by Gödel numbers then the field operations are all primitive recursive.
The following examples and definitions are from Kleene (1952) pp. 223–231. Many appear with proofs. Most also appear with similar names, either as proofs or as examples, in Boolos-Burgess-Jeffrey 2002 pp. 63–70; they add the logarithm lo(x, y) or lg(x, y) depending on the exact derivation.
In the following the mark " ' ", e.g. a', is the primitive mark meaning "the successor of", usually thought of as " +1", e.g. a +1 =def a'. The functions 16–20 and #G are of particular interest with respect to converting primitive recursive predicates to, and extracting them from, their "arithmetical" form expressed as Gödel numbers.
In the following, the abbreviation x =def x1, ... xn; subscripts may be applied if the meaning requires.
φ(x) =
φ(y,x) = χ(y, COURSE-φ(y; x2, ... xn), x2, ... xn then φ is primitive recursive in χ. The value COURSE-φ(y; x2 to n) of the course-of-values function encodes the sequence of values φ(0,x2 to n), ..., φ(y-1,x2 to n) of the original function.
The broader class of partial recursive functions is defined by introducing an unbounded search operator. The use of this operator may result in a partial function, that is, a relation with at most one value for each argument, but does not necessarily have any value for any argument (see domain). An equivalent definition states that a partial recursive function is one that can be computed by a Turing machine. A total recursive function is a partial recursive function that is defined for every input.
Every primitive recursive function is total recursive, but not all total recursive functions are primitive recursive. The Ackermann function A(m,n) is a well-known example of a total recursive function (in fact, provable total), that is not primitive recursive. There is a characterization of the primitive recursive functions as a subset of the total recursive functions using the Ackermann function. This characterization states that a function is primitive recursive if and only if there is a natural number m such that the function can be computed by a Turing machine that always halts within A(m,n) or fewer steps, where n is the sum of the arguments of the primitive recursive function.[4]
An important property of the primitive recursive functions is that they are a recursively enumerable subset of the set of all total recursive functions (which is not itself recursively enumerable). This means that there is a single computable function f(m,n) that enumerates the primitive recursive functions, namely:
f can be explicitly constructed by iteratively repeating all possible ways of creating primitive recursive functions. Thus, it is provably total. One can use a diagonalization argument to show that f is not recursive primitive in itself: had it been such, so would be h(n) = f(n,n)+1. But if this equals some primitive recursive function, there is an m such that h(n) = f(m,n) for all n, and then h(m) = f(m,m), leading to contradiction.
However, the set of primitive recursive functions is not the largest recursively enumerable subset of the set of all total recursive functions. For example, the set of provably total functions (in Peano arithmetic) is also recursively enumerable, as one can enumerate all the proofs of the theory. While all primitive recursive functions are provably total, the converse is not true.
Primitive recursive functions tend to correspond very closely with our intuition of what a computable function must be. Certainly the initial functions are intuitively computable (in their very simplicity), and the two operations by which one can create new primitive recursive functions are also very straightforward. However, the set of primitive recursive functions does not include every possible total computable function—this can be seen with a variant of Cantor's diagonal argument. This argument provides a total computable function that is not primitive recursive. A sketch of the proof is as follows:
This argument can be applied to any class of computable (total) functions that can be enumerated in this way, as explained in the article Machine that always halts. Note however that the partial computable functions (those that need not be defined for all arguments) can be explicitly enumerated, for instance by enumerating Turing machine encodings.
Other examples of total recursive but not primitive recursive functions are known:
Instead of
k | |
C | |
n |
0 | |
C | |
0 |
The 1-place predecessor function is primitive recursive, see section
\begin{array}{lcl} f(0,x1,\ldots,xk)&=&g(x1,\ldots,xk)&and\\ f(S(y),x1,\ldots,xk)&=&h(S(y),f(y,x1,\ldots,xk),x1,\ldots,xk) \end{array}
Weakening this even further by using functions
h
y
S(y)
h
\begin{array}{lcll}f(0,x1,\ldots,xk)&=&g(x1,\ldots,xk)&rm{and}\ f(S(y),x1,\ldots,xk)&=&h(f(y,x1,\ldots,xk),x1,\ldots,xk)\end{array}
The class of iterative functions is defined the same way as the class of primitive recursive functions except with this weaker rule. These are conjectured to be a proper subset of the primitive recursive functions.
Some additional forms of recursion also define functions that are in factprimitive recursive. Definitions in these forms may be easier to find ormore natural for reading or writing. Course-of-values recursion defines primitive recursive functions. Some forms of mutual recursion also define primitive recursive functions.
The functions that can be programmed in the LOOP programming language are exactly the primitive recursive functions. This gives a different characterization of the power of these functions. The main limitation of the LOOP language, compared to a Turing-complete language, is that in the LOOP language the number of times that each loop will run is specified before the loop begins to run.
An example of a primitive recursive programming language is one that contains basic arithmetic operators (e.g. + and −, or ADD and SUBTRACT), conditionals and comparison (IF-THEN, EQUALS, LESS-THAN), and bounded loops, such as the basic for loop, where there is a known or calculable upper bound to all loops (FOR i FROM 1 TO n, with neither i nor n modifiable by the loop body). No control structures of greater generality, such as while loops or IF-THEN plus GOTO, are admitted in a primitive recursive language.
The LOOP language, introduced in a 1967 paper by Albert R. Meyer and Dennis M. Ritchie,[5] is such a language. Its computing power coincides with the primitive recursive functions. A variant of the LOOP language is Douglas Hofstadter's BlooP in Gödel, Escher, Bach. Adding unbounded loops (WHILE, GOTO) makes the language general recursive and Turing-complete, as are all real-world computer programming languages.
The definition of primitive recursive functions implies that their computation halts on every input (after a finite number of steps). On the other hand, the halting problem is undecidable for general recursive functions.
The primitive recursive functions are closely related to mathematical finitism, and are used in several contexts in mathematical logic where a particularly constructive system is desired. Primitive recursive arithmetic (PRA), a formal axiom system for the natural numbers and the primitive recursive functions on them, is often used for this purpose.
PRA is much weaker than Peano arithmetic, which is not a finitistic system. Nevertheless, many results in number theory and in proof theory can be proved in PRA. For example, Gödel's incompleteness theorem can be formalized into PRA, giving the following theorem:
If T is a theory of arithmetic satisfying certain hypotheses, with Gödel sentence GT, then PRA proves the implication Con(T)→GT.Similarly, many of the syntactic results in proof theory can be proved in PRA, which implies that there are primitive recursive functions that carry out the corresponding syntactic transformations of proofs.
In proof theory and set theory, there is an interest in finitistic consistency proofs, that is, consistency proofs that themselves are finitistically acceptable. Such a proof establishes that the consistency of a theory T implies the consistency of a theory S by producing a primitive recursive function that can transform any proof of an inconsistency from S into a proof of an inconsistency from T. One sufficient condition for a consistency proof to be finitistic is the ability to formalize it in PRA. For example, many consistency results in set theory that are obtained by forcing can be recast as syntactic proofs that can be formalized in PRA.
Recursive definitions had been used more or less formally in mathematics before, but the construction of primitive recursion is traced back to Richard Dedekind's theorem 126 of his Was sind und was sollen die Zahlen? (1888). This work was the first to give a proof that a certain recursive construction defines a unique function.[6] [7] [8]
Primitive recursive arithmetic was first proposed by Thoralf Skolem[9] in 1923.
The current terminology was coined by Rózsa Péter (1934) after Ackermann had proved in 1928 that the function which today is named after him was not primitive recursive, an event which prompted the need to rename what until then were simply called recursive functions.[7] [8]