# Primitive recursive function

In computability theory, **primitive recursive functions** are a class of functions that are defined using primitive recursion and composition as central operations and are a strict subset of the total µ-recursive functions (µ-recursive functions are also called partial recursive). Primitive recursive functions form an important building block on the way to a full formalization of computability. These functions are also important in proof theory. The term was coined by Rózsa Péter.

Most of the functions normally studied in number theory are primitive recursive. For example: addition, division, factorial, exponential and the *n*th prime are all primitive recursive. So are many approximations to real-valued functions.^{[1]} In fact, it is difficult to devise a computable function that is *not* primitive recursive, although some are known (see the section on Limitations below).
The set of primitive recursive functions is known as PR in computational complexity theory.

Every primitive recursive function is a total recursive function.

## Contents

## Definition

The primitive recursive functions are among the number-theoretic functions, which are functions from the natural numbers (nonnegative integers) {0, 1, 2, ...} to the natural numbers. These functions take *n* arguments for some natural number *n* and are called *n*-ary.

The basic primitive recursive functions are given by these axioms:

**Constant function**: The 0-ary constant function 0 is primitive recursive.**Successor function**: The 1-ary successor function*S*, which returns the successor of its argument (see Peano postulates), is primitive recursive. That is,*S*(*k*) =*k*+ 1.**Projection function**: For every*n*≥1 and each*i*with 1≤*i*≤*n*, the*n*-ary projection function*P*_{i}^{n}, which returns its*i*-th argument, is primitive recursive.

More complex primitive recursive functions can be obtained by applying the operations given by these axioms:

**Composition**: Given*f*, a*k*-ary primitive recursive function, and*k**m*-ary primitive recursive functions*g*_{1},...,*g*_{k}, the composition of*f*with*g*_{1},...,*g*_{k}, i.e. the*m*-ary function is primitive recursive.**Primitive recursion**: Given*f*, a*k*-ary primitive recursive function, and*g*, a (*k*+2)-ary primitive recursive function, the (*k*+1)-ary function*h*is defined as the primitive recursion of*f*and*g*, i.e. the function*h*is primitive recursive when

The **primitive recursive** functions are the basic functions and those obtained from the basic functions by applying these operations a finite number of times.

### Role of the projection functions

The projection functions can be used to avoid the apparent rigidity in terms of the arity of the functions above; by using compositions with various projection functions, it is possible to pass a subset of the arguments of one function to another function. For example, if *g* and *h* are 2-ary primitive recursive functions then

is also primitive recursive. One formal definition using projection functions is

### Converting predicates to numeric functions

In some settings it is natural to consider primitive recursive functions that take as inputs tuples that mix numbers with truth values { t= true, f=false }, or that produce truth values as outputs (see Kleene [1952 pp. 226–227]). This can be accomplished by identifying the truth values with numbers in any fixed manner. For example, it is common to identify the truth value *t* with the number *1* and the truth value *f* with the number *0*. Once this identification has been made, the characteristic function of a set *A*, which literally returns *1* or *0*, can be viewed as a predicate that tells whether a number is in the set *A*. Such an identification of predicates with numeric functions will be assumed for the remainder of this article.

### Computer language definition

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). No control structures of greater generality, such as while loops or IF-THEN plus GOTO, are admitted in a primitive recursive language. Douglas Hofstadter's Bloop in Gödel, Escher, Bach is one such. Adding unbounded loops (WHILE, GOTO) makes the language partially recursive, or Turing-complete; Floop is such, as are almost all real-world computer languages.

Arbitrary computer programs, or Turing machines, cannot in general be analyzed to see if they halt or not (the halting problem). However, all primitive recursive functions halt. This is not a contradiction; primitive recursive programs are a non-arbitrary subset of all possible programs, constructed specifically to be analyzable.

## Examples

Most number-theoretic functions definable using recursion on a single variable are primitive recursive. Basic examples include the addition and "limited subtraction" functions.

### Addition

Intuitively, addition can be recursively defined with the rules:

- add(0,
*x*)=*x*, - add(
*n*+1,*x*)=add(*n*,*x*)+1.

To fit this into a strict primitive recursive definition, define:

- add(0,
*x*)=*P*_{1}^{1}(*x*) , - add(S(
*n*),*x*)=*S*(*P*_{2}^{3}(*n*,add(*n*,*x*),*x*)).

Here *P*_{2}^{3} is the projection function that takes 3 arguments and returns the second one.

*P*_{1}^{1} is simply the identity function; its inclusion is required by the definition of the primitive recursion operator above; it plays the role of *f*. The composition of *S* and *P*_{2}^{3}, which is primitive recursive, plays the role of *g*. The term S(*n*) refers to "the successor of *n*".

### Subtraction

Because primitive recursive functions use natural numbers rather than integers, and the natural numbers are not closed under subtraction, a **limited subtraction** function (also called "proper subtraction") is studied in this context. This limited subtraction function sub(*a*,*b*) [or *b* ∸ *a*] returns *b* - *a* if this is nonnegative and returns *0* otherwise.

The **predecessor function** acts as the opposite of the successor function and is recursively defined by the rules:

- pred(0)=0,
- pred(
*n*+1)=*n*.

These rules can be converted into a more formal definition by primitive recursion:

- pred(0)=0,
- pred(S(
*n*))=*P*_{1}^{2}(*n*, pred(*n*)).

The limited subtraction function is definable from the predecessor function in a manner analogous to the way addition is defined from successor:

- sub(0,
*x*)=*P*_{1}^{1}(*x*), - sub(S(
*n*),*x*)=pred(*P*_{2}^{3}(*n*,sub(*n*,*x*),*x*)).

Here sub(*a*,*b*) corresponds to *b*∸*a*; for the sake of simplicity, the order of the arguments has been switched from the "standard" definition to fit the requirements of primitive recursion. This could easily be rectified using composition with suitable projections.

Other primitive recursive functions include exponentiation and primality testing. Given primitive recursive functions *e*, *f*, *g*, and *h*, a function that returns the value of *g* when *e*≤*f* and the value of *h* otherwise is primitive recursive.

### Operations on integers and rational numbers

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.

## Relationship to recursive functions

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 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.^{[2]}

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*(*e*,*n*) such that:

- For every primitive recursive function
*g*, there is an*e*such that*g*(*n*) =*f*(*e*,*n*) for all*n*, and - For every
*e*, the function*h*(*n*) =*f*(*e*,*n*) is primitive recursive.

However, the primitive recursive functions are not the largest recursively enumerable set of total computable functions.

## Limitations

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:

- The primitive recursive functions of one argument (i.e., unary functions) can be computably enumerated. This enumeration uses the definitions of the primitive recursive functions (which are essentially just expressions with the composition and primitive recursion operations as operators and the basic primitive recursive functions as atoms), and can be assumed to contain every definition once, even though a same
*function*will occur many times on the list (since many definitions define the same function; indeed simply composing by the identity function generates infinitely many definitions of any one primitive recursive function). This means that the Template:Mvar-th definition of a primitive recursive function in this enumeration can be effectively determined from Template:Mvar. Indeed if one uses some Gödel numbering to encode definitions as numbers, then this Template:Mvar-th definition in the list is computed by a primitive recursive function of Template:Mvar. Let*f*_{n}denote the unary primitive recursive function given by this definition.

- Now define the "evaluator function" Template:Mvar with two arguments, by
*ev*(*i*,*j*) =*f*_{i}(*j*). Clearly*ev*is total and computable, since one can effectively determine the definition of*f*_{i}, and being a primitive recursive function*f*_{i}is itself total and computable, so*f*_{i}(*j*) is always defined and effectively computable. However a diagonal argument will show that the function Template:Mvar of two arguments is not primitive recursive.

- Suppose Template:Mvar were primitive recursive, then the unary function Template:Mvar defined by
*g*(*i*) = S(*ev*(*i*,*i*)) would also be primitive recursive, as it is defined by composition from the successor function and Template:Mvar. But then Template:Mvar occurs in the enumeration, so there is some number Template:Mvar such that*g*=*f*_{n}. But now*g*(*n*) = S(*ev*(*n*,*n*)) = S(*f*_{n}(*n*)) = S(*g*(*n*)) gives a contradiction.

This argument can be applied to any class of computable (total) functions that can be enumerated in this way, as explained in the article Machines that always halt. 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:

- The function that takes
*m*to Ackermann(*m*,*m*) is a unary total recursive function that is not primitive recursive. - The Paris–Harrington theorem involves a total recursive function that is not primitive recursive. Because this function is motivated by Ramsey theory, it is sometimes considered more "natural" than the Ackermann function.
- The Sudan function
- The Goodstein function

## Some common primitive recursive functions

- 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 #22 the logarithm lo(x, y) or lg(x, y) depending on the exact derivation.

In the following we observe that primitive recursive functions can be of four types:

*functions*for short: "number-theoretic functions" from { 0, 1, 2, ...} to { 0, 1, 2, ...},*predicates*: from { 0, 1, 2, ...} to truth values { t =true, f =false },*propositional connectives*: from truth values { t, f } to truth values { t, f },*representing functions*: from truth values { t, f } to { 0, 1, 2, ... }. Many times a predicate requires a representing function to convert the predicate's output { t, f } to { 0, 1 } (note the order "t" to "0" and "f" to "1" matches with ~(sig( )) defined below). By definition a function φ(**x**) is a "representing function" of the predicate P(**x**) if φ takes only values 0 and 1 and produces*0*when P is true".

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-21 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.

- Addition: a+b
- Multiplication: a×b
- Exponentiation: a
^{b,} - Factorial a! : 0! = 1, a'! = a!×a'
- pred(a): Decrement: "predecessor of a" defined as "If a> 0 then a-1 → a
_{new}else 0 → a." - Proper subtraction: a ∸ b defined as "If a ≥ b then a-b else 0."
- Minimum (a
_{1}, ... a_{n}) - Maximum (a
_{1}, ... a_{n}) - Absolute value: | a-b | =
_{defined}(a ∸ b) + (b ∸ a) - ~sg(a): NOT[signum(a)]: If a=0 then sg(a)=1 else if a>0 then sg(a)=0
- sg(a): signum(a): If a=0 then sg(a)=0 else if a>0 then sg(a)=1
- "b divides a" [ a | b ]: If the remainder ( a, b )=0 then [ a | b ] else b does not divide a "evenly"
- Remainder ( a, b ): the leftover if b does not divide a "evenly". Also called MOD(a, b)
- a = b: sg | a - b | (Kleene's convention was to represent
*true*by 0 and*false*by 1; presently, especially in computers, the most common convention is the converse, namely to represent*true*by 1 and*false*by 0, which amounts to change sg into ~sg here and in the next item) - a < b: sg( a' ∸ b )
- Pr(a): a is a prime number Pr(a) =
_{def}a>1 & NOT(Exists c)_{1<c<a}[ c|a ] - P
_{i}: the i+1-st prime number - (a)
_{i}: exponent a_{i}of p_{i}=_{def}μx [_{x<a}[p_{i}^{x}|a & NOT(p_{i}^{x'}|a ]; μx is the minimization operator described in #E below. - lh(a): the "length" or number of non-vanishing exponents in a
- a×b: given the expression of a and b as prime factors then a×b is the product's expression as prime factors
- lo(x, y): logarithm of x to the base y

*In the following, the abbreviation***x**=_{def}x_{i}, ... x_{n}; subscripts may be applied if the meaning requires.

- #A: A function φ definable explicitly from functions Ψ and constants q
_{1}, ... q_{n}is primitive recursive in Ψ. - #B: The finite sum Σ
_{y<z}ψ(**x**, y) and product Π_{y<z}ψ(**x**, y) are primitive recursive in ψ. - #C: A
*predicate*P obtained by substituting functions χ_{1},..., χ_{m}for the respective variables of a predicate Q is primitive recursive in χ_{1},..., χ_{m}, Q. - #D: The following
*predicates*are primitive recursive in Q and R:

- NOT_Q(
**x**) . - Q OR R: Q(
**x**) V R(**x**), - Q AND R: Q(
**x**) & R(**x**), - Q IMPLIES R: Q(
**x**) → R(**x**) - Q is equivalent to R: Q(
**x**) ≡ R(**x**)

- NOT_Q(

- #E: The following
*predicates*are primitive recursive in the*predicate*R:

- (Ey)
_{y<z}R(**x**, y) where (Ey)_{y<z}denotes "there exists at least one y that is less than z such that" - (y)
_{y<z}R(**x**, y) where (y)_{y<z}denotes "for all y less than z it is true that" - μy
_{y<z}R(**x**, y). The operator μy_{y<z}R(**x**, y) is a*bounded*form of the so-called minimization- or mu-operator: Defined as "the least value of y less than z such that R(**x**, y) is true; or z if there is no such value."

- (Ey)

- #F: Definition by cases: The function defined thus, where Q
_{1}, ..., Q_{m}are mutually exclusive*predicates*(or "ψ(**x**) shall have the value given by the first clause that applies), is primitive recursive in φ_{1}, ..., Q_{1}, ... Q_{m}:

- φ(
**x**) =- φ
_{1}(**x**) if Q_{1}(**x**) is true, - . . . . . . . . . . . . . . . . . . .
- φ
_{m}(**x**) if Q_{m}(**x**) is true - φ
_{m+1}(**x**) otherwise

- φ

- φ(

- #G: If φ satisfies the equation:

- φ(y,
**x**) = χ(y, NOT-φ(y; x_{2}, ... x_{n}), x_{2}, ... x_{n}then φ is primitive recursive in χ. 'So, in a sense the knowledge of the value NOT-φ(y;**x**_{2 to n}) of the course-of-values function is equivalent to the knowledge of the sequence of values φ(0,**x**_{2 to n}), ..., φ(y-1,**x**_{2 to n}) of the original function."

- φ(y,

## Additional primitive recursive forms

Some additional forms of recursion also define functions that are in fact primitive recursive. Definitions in these forms may be easier to find or more 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.

## Finitism and consistency results

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*G*_{T}, then PRA proves the implication Con(*T*)→*G*_{T}.

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.

## See also

- Course-of-values recursion
- Grzegorczyk hierarchy
- Machine that always halts
- Recursion (computer science)
- Primitive recursive functional
- Double recursion
- Primitive recursive set function
- Primitive recursive ordinal function

## References

- Brainerd, W.S., Landweber, L.H. (1974),
*Theory of Computation*, Wiley, ISBN 0-471-09585-0 - Robert I. Soare,
*Recursively Enumerable Sets and Degrees*, Springer-Verlag, 1987. ISBN 0-387-15299-7 - Stephen Kleene (1952)
*Introduction to Metamathematics*, North-Holland Publishing Company, New York, 11th reprint 1971: (2nd edition notes added on 6th reprint). In Chapter XI. General Recursive Functions §57 - George Boolos, John Burgess, Richard Jeffrey (2002),
*Computability and Logic: Fourth Edition*, Cambridge University Press, Cambridge, UK. Cf pp. 70–71. - Robert I. Soare 1995
*Computability and Recursion*http://www.people.cs.uchicago.edu/~soare/History/compute.pdf - Daniel Severin 2008,
*Unary primitive recursive functions*, J. Symbolic Logic Volume 73, Issue 4, pp. 1122–1138 arXiv projecteuclid

- ↑ Brainerd and Landweber, 1974
- ↑ This follows from the facts that the functions of this form are the most quickly growing primitive recursive functions, and that a function is primitive recursive if and only if its time complexity is bounded by a primitive recursive function. For the former, see {{#invoke:citation/CS1|citation |CitationClass=citation }}. For the latter, see {{#invoke:citation/CS1|citation |CitationClass=citation }}.