# Primitive recursive function

__: Function computable with bounded loops__

**Short description**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 can be determined 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 *n*th 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.^{[citation needed]} It is hence not particularly easy to devise a computable function that is *not* primitive recursive; some examples are shown in section § Limitations below.

The set of primitive recursive functions is known as PR in computational complexity theory.

## Definition

A primitive recursive function takes a fixed number of arguments, each a natural number (nonnegative integer: {0, 1, 2, ...}), 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:

*Constant functions Ckn*: For each natural number n and every k, the*k*-ary constant function, defined by [math]\displaystyle{ C_n^k(x_1,\ldots,x_k) \ \stackrel{\mathrm{def}}{=}\ n }[/math], is primitive recursive.*Successor function*: The 1-ary successor function*S*, which returns the successor of its argument (see Peano postulates), that is, [math]\displaystyle{ S(x) \ \stackrel{\mathrm{def}}{=}\ x + 1 }[/math], is primitive recursive.*Projection function*[math]\displaystyle{ P_i^k }[/math]: For all natural numbers [math]\displaystyle{ i, k }[/math] such that [math]\displaystyle{ 1\le i\le k }[/math], the*k*-ary function defined by [math]\displaystyle{ P_i^k(x_1,\ldots,x_k) \ \stackrel{\mathrm{def}}{=}\ x_i }[/math] is primitive recursive.

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

*Composition operator*[math]\displaystyle{ \circ\, }[/math] (also called the*substitution operator*): Given an*m*-ary function [math]\displaystyle{ h(x_1,\ldots,x_m)\, }[/math] and*m**k*-ary functions [math]\displaystyle{ g_1(x_1,\ldots,x_k),\ldots,g_m(x_1,\ldots, x_k) }[/math]: [math]\displaystyle{ h \circ (g_1, \ldots, g_m) \ \stackrel{\mathrm{def}}{=}\ f, \quad\text{where}\quad f(x_1,\ldots,x_k) = h(g_1(x_1,\ldots,x_k),\ldots,g_m(x_1,\ldots,x_k)). }[/math]*Primitive recursion operator*ρ: Given the*k*-ary function [math]\displaystyle{ g(x_1,\ldots,x_k)\, }[/math] and the (*k*+ 2)-ary function [math]\displaystyle{ h(y,z,x_1,\ldots,x_k)\, }[/math]:[math]\displaystyle{ \begin{align} \rho(g, h) &\ \stackrel{\mathrm{def}}{=}\ f, \quad\text{where the }(k+1)\text{-ary function } f \text{ is defined by}\\ f(0,x_1,\ldots,x_k) &= g(x_1,\ldots,x_k) \\ f(S(y),x_1,\ldots,x_k) &= h(y,f(y,x_1,\ldots,x_k),x_1,\ldots,x_k).\end{align} }[/math]

The function*Interpretation:**f*acts as a for-loop from 0 up to the value of its first argument. The rest of the arguments for*f*, denoted here with*x*_{1}, ...,*x*_{k}, are a set of initial conditions for the for-loop which may be used by it during calculations but which are immutable by it. The functions*g*and*h*on the right-hand side of the equations that define*f*represent the body of the loop, which performs calculations. The function*g*is used only once to perform initial calculations. Calculations for subsequent steps of the loop are performed by*h*. The first parameter of*h*is fed the "current" value of the for-loop's index. The second parameter of*h*is fed the result of the for-loop's previous calculations, from previous steps. The rest of the parameters for*h*are those immutable initial conditions for the for-loop mentioned earlier. They may be used by*h*to perform calculations but they will not themselves be altered by*h*.

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

## Examples

- [math]\displaystyle{ C_0^1 }[/math] is a 1-ary function which returns [math]\displaystyle{ 0 }[/math] for every input: [math]\displaystyle{ C_0^1(x) = 0 }[/math].
- [math]\displaystyle{ C_1^1 }[/math] is a 1-ary function which returns [math]\displaystyle{ 1 }[/math] for every input: [math]\displaystyle{ C_1^1(x) = 1 }[/math].
- [math]\displaystyle{ C_3^0 }[/math] is a 0-ary function, i.e. a constant: [math]\displaystyle{ C_3^0 = 3 }[/math].
- [math]\displaystyle{ P_1^1 }[/math] is the identity function on the natural numbers: [math]\displaystyle{ P_1^1(x) = x }[/math].
- [math]\displaystyle{ P_1^2 }[/math] and [math]\displaystyle{ P_2^2 }[/math] is the left and right projection on natural number pairs, respectively: [math]\displaystyle{ P_1^2(x,y) = x }[/math] and [math]\displaystyle{ P_2^2(x,y) = y }[/math].
- [math]\displaystyle{ S \circ S }[/math] is a 1-ary function that adds 2 to its input, [math]\displaystyle{ (S \circ S)(x) = x+2 }[/math].
- [math]\displaystyle{ S \circ C_0^1 }[/math] is a 1-ary function which returns 1 for every input: [math]\displaystyle{ (S \circ C_0^1)(x) = S(C_0^1(x)) = S(0) = 1 }[/math]. That is, [math]\displaystyle{ S \circ C_0^1 }[/math] and [math]\displaystyle{ C_1^1 }[/math] are the same function: [math]\displaystyle{ S \circ C_0^1 = C_1^1 }[/math]. In a similar way, every [math]\displaystyle{ C_n^1 }[/math] can be expressed as a composition of appropriately many [math]\displaystyle{ S }[/math] and [math]\displaystyle{ C_0^1 }[/math].

### Addition

A definition of the 2-ary function [math]\displaystyle{ Add }[/math], to compute the sum of its arguments, can be obtained using the primitive recursion operator [math]\displaystyle{ \rho }[/math]. To this end, the well-known equations

- [math]\displaystyle{ \begin{array}{rcll} 0+y & = & y & \text{ and} \\ S(x)+y & = & S(x+y) & . \\ \end{array} }[/math]

are "rephrased in primitive recursive function terminology": In the definition of [math]\displaystyle{ \rho(g,h) }[/math], the first equation suggests to choose [math]\displaystyle{ g = P_1^1 }[/math] to obtain [math]\displaystyle{ Add(0,y) = g(y) = y }[/math]; the second equation suggests to choose [math]\displaystyle{ h = S \circ P_2^3 }[/math] to obtain [math]\displaystyle{ Add(S(x),y) = h(x,Add(x,y),y) = (S \circ P_2^3)(x,Add(x,y),y) = S(Add(x,y)) }[/math]. Therefore, the addition function can be defined as [math]\displaystyle{ Add = \rho(P_1^1,S \circ P_2^3) }[/math]. As a computation example,

- [math]\displaystyle{ \begin{array}{lll} & Add(1,7) \\ = & \rho(P_1^1,S \circ P_2^3) \; (S(0),7) & \text{ by Def. } Add, S \\ = & (S \circ P_2^3)(0,Add(0,7),7) & \text{ by case } \rho(g,h) \; (S(...),...) \\ = & S(Add(0,7)) & \text{ by Def. } \circ, P_2^3 \\ = & S( \; \rho(P_1^1,S \circ P_2^3) \; (0,7) \; ) & \text{ by Def. } Add \\ = & S(P_1^1(7)) & \text{ by case } \rho(g,h) \; (0,...) \\ = & S(7) & \text{ by Def. } P_1^1 \\ = & 8 & \text{ by Def. } S . \\ \end{array} }[/math]

### Doubling

Given [math]\displaystyle{ Add }[/math], the 1-ary function [math]\displaystyle{ Add \circ (P_1^1,P_1^1) }[/math] doubles its argument, [math]\displaystyle{ (Add \circ (P_1^1,P_1^1))(x) = Add(x,x) = x+x }[/math].

### Multiplication

In a similar way as addition, multiplication can be defined by [math]\displaystyle{ Mul = \rho(C_0^1,Add \circ(P_2^3,P_3^3)) }[/math]. This reproduces the well-known multiplication equations:

- [math]\displaystyle{ \begin{array}{lll} & Mul(0,y) \\ = & \rho(C_0^1,Add \circ(P_2^3,P_3^3)) \; (0,y) & \text{ by Def. } Mul \\ = & C_0^1(y) & \text{ by case } \rho(g,h) \; (0,...)\\ = & 0 & \text{ by Def. } C_0^1 . \\ \end{array} }[/math]

and

- [math]\displaystyle{ \begin{array}{lll} & Mul(S(x),y) \\ = & \rho(C_0^1,Add \circ(P_2^3,P_3^3)) \; (S(x),y) & \text{ by Def. } Mul \\ = & (Add \circ(P_2^3,P_3^3)) \; (x,Mul(x,y),y) & \text{ by case } \rho(g,h) \; (S(...),...) \\ = & Add(Mul(x,y),y) & \text{ by Def. } \circ, P_2^3, P_3^3 \\ = & Mul(x,y)+y & \text{ by property of } Add . \\ \end{array} }[/math]

### Predecessor

The predecessor function acts as the "opposite" of the successor function and is recursively defined by the rules [math]\displaystyle{ Pred(0) = 0 }[/math] and [math]\displaystyle{ Pred(S(n)) = n }[/math]. A primitive recursive definition is [math]\displaystyle{ Pred = \rho(C_0^0, P_1^2) }[/math]. As a computation example,

- [math]\displaystyle{ \begin{array}{lll} & Pred(8) \\ = & \rho(C_0^0, P_1^2) \; (S(7)) & \text{ by Def. } Pred, S \\ = & P_1^2(7,Pred(7)) & \text{ by case } \rho(g,h) \; (S(...),...) \\ = & 7 & \text{ by Def. } P_1^2 . \\ \end{array} }[/math]

### Truncated subtraction

The limited subtraction function (also called "monus", and denoted "[math]\displaystyle{ \stackrel.- }[/math]") is definable from the predecessor function. It satisfies the equations

- [math]\displaystyle{ \begin{array}{rcll} y \stackrel.- 0 & = & y & \text{and} \\ y \stackrel.- S(x) & = & Pred(y \stackrel.- x) & . \\ \end{array} }[/math]

Since the recursion runs over the second argument, we begin with a primitive recursive definition of the reversed subtraction, [math]\displaystyle{ RSub(y,x) = x \stackrel.- y }[/math]. Its recursion then runs over the first argument, so its primitive recursive definition can be obtained, similar to addition, as [math]\displaystyle{ RSub = \rho(P_1^1, Pred \circ P_2^3) }[/math]. To get rid of the reversed argument order, then define [math]\displaystyle{ Sub = RSub \circ (P_2^2,P_1^2) }[/math]. As a computation example,

- [math]\displaystyle{ \begin{array}{lll} & Sub(8,1) \\ = & (RSub \circ (P_2^2,P_1^2)) \; (8,1) & \text{ by Def. } Sub \\ = & RSub(1,8) & \text{ by Def. } \circ, P_2^2, P_1^2 \\ = & \rho(P_1^1, Pred \circ P_2^3) \; (S(0),8) & \text{ by Def. } RSub, S \\ = & (Pred \circ P_2^3) \; (0,RSub(0,8),8) & \text{ by case } \rho(g,h) \; (S(...),...) \\ = & Pred(RSub(0,8)) & \text{ by Def. } \circ, P_2^3 \\ = & Pred( \; \rho(P_1^1, Pred \circ P_2^3) \; (0,8) \; ) & \text{ by Def. } RSub \\ = & Pred(P_1^1(8)) & \text{ by case } \rho(g,h) \; (0,...) \\ = & Pred(8) & \text{ by Def. } P_1^1 \\ = & 7 & \text{ by property of } Pred . \\ \end{array} }[/math]

### 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 (that is *t* for true and *f* for false), or that produce truth values as outputs.^{[2]} 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 always 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.

### Predicate "Is zero"

As an example for a primitive recursive predicate, the 1-ary function [math]\displaystyle{ IsZero }[/math] shall be defined such that [math]\displaystyle{ IsZero(x) = 1 }[/math] if [math]\displaystyle{ x = 0 }[/math], and [math]\displaystyle{ IsZero(x) = 0 }[/math], otherwise. This can be achieved by defining [math]\displaystyle{ IsZero = \rho(C_1^0,C_0^2) }[/math]. Then, [math]\displaystyle{ IsZero(0) = \rho(C_1^0,C_0^2)(0) = C_1^0(0) = 1 }[/math] and e.g. [math]\displaystyle{ IsZero(8) = \rho(C_1^0,C_0^2)(S(7)) = C_0^2(7,IsZero(7)) = 0 }[/math].

### Predicate "Less or equal"

Using the property [math]\displaystyle{ x \leq y \iff x \stackrel.- y = 0 }[/math], the 2-ary function [math]\displaystyle{ Leq }[/math] can be defined by [math]\displaystyle{ Leq = IsZero \circ Sub }[/math]. Then [math]\displaystyle{ Leq(x,y) = 1 }[/math] if [math]\displaystyle{ x \leq y }[/math], and [math]\displaystyle{ Leq(x,y) = 0 }[/math], otherwise. As a computation example,

- [math]\displaystyle{ \begin{array}{lll} & Leq(8,3) \\ = & IsZero(Sub(8,3)) & \text{ by Def. } Leq \\ = & IsZero(5) & \text{ by property of } Sub \\ = & 0 & \text{ by property of } IsZero \\ \end{array} }[/math]

### Predicate "Greater or equal"

Once a definition of [math]\displaystyle{ Leq }[/math] is obtained, the converse predicate can be defined as [math]\displaystyle{ Geq = Leq \circ (P_2^2,P_1^2) }[/math]. Then, [math]\displaystyle{ Geq(x,y) = Leq(y,x) }[/math] is true (more precisely: has value 1) if, and only if, [math]\displaystyle{ x \geq y }[/math].

### If-then-else

The 3-ary if-then-else operator known from programming languages can be defined by [math]\displaystyle{ \textit{If} = \rho(P_2^2,P_3^4) }[/math]. Then, for arbitrary [math]\displaystyle{ x }[/math],

- [math]\displaystyle{ \begin{array}{lll} & \textit{If}(S(x),y,z) \\ = & \rho(P_2^2,P_3^4) \; (S(x),y,z) & \text{ by Def. } \textit{If} \\ = & P_3^4(x,\textit{If}(x,y,z),y,z) & \text{ by case } \rho(S(...),...) \\ = & y & \text{ by Def. } P_3^4 \\ \end{array} }[/math]

and

- [math]\displaystyle{ \begin{array}{lll} & \textit{If}(0,y,z) \\ = & \rho(P_2^2,P_3^4) \; (0,y,z) & \text{ by Def. } \textit{If} \\ = & P_2^2(y,z) & \text{ by case } \rho(0,...) \\ = & z & \text{ by Def. } P_2^2 . \\ \end{array} }[/math].

That is, [math]\displaystyle{ \textit{If}(x,y,z) }[/math] returns the then-part, [math]\displaystyle{ y }[/math], if the if-part, [math]\displaystyle{ x }[/math], is true, and the else-part, [math]\displaystyle{ z }[/math], otherwise.

### Junctors

Based on the [math]\displaystyle{ \textit{If} }[/math] function, it is easy to define logical junctors. For example, defining [math]\displaystyle{ And = \textit{If} \circ (P_1^2,P_2^2,C_0^2) }[/math], one obtains [math]\displaystyle{ And(x,y) = \textit{If}(x,y,0) }[/math], that is, [math]\displaystyle{ And(x,y) }[/math] is true if, and only if, both [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math] are true (logical conjunction of [math]\displaystyle{ x }[/math] and [math]\displaystyle{ y }[/math]).

Similarly, [math]\displaystyle{ Or = \textit{If} \circ (P_1^2,C_1^2,P_2^2) }[/math] and [math]\displaystyle{ Not = \textit{If} \circ (P_1^1,C_0^1,C_1^1) }[/math] lead to appropriate definitions of disjunction and negation: [math]\displaystyle{ Or(x,y) = \textit{If}(x,1,y) }[/math] and [math]\displaystyle{ Not(x) = \textit{If}(x,0,1) }[/math].

### Equality predicate

Using the above functions [math]\displaystyle{ Leq }[/math], [math]\displaystyle{ Geq }[/math] and [math]\displaystyle{ And }[/math], the definition [math]\displaystyle{ Eq = And \circ (Leq, Geq) }[/math] implements the equality predicate. In fact, [math]\displaystyle{ Eq(x,y) = And( Leq(x,y) , Geq(x,y) ) }[/math] is true if, and only if, [math]\displaystyle{ x }[/math] equals [math]\displaystyle{ y }[/math].

Similarly, the definition [math]\displaystyle{ Lt = Not \circ Geq }[/math] implements the predicate "less-than", and [math]\displaystyle{ Gt = Not \circ Leq }[/math] implements "greater-than".

### Other operations on natural numbers

Exponentiation and primality testing are primitive recursive. 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.

### 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 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 ~sg( ) 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-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.

- Addition: a+b
- Multiplication: a×b
- Exponentiation: a
^{b} - Factorial a! : 0! = 1, a'! = a!×a'
- pred(a): (Predecessor or decrement): If a > 0 then a-1 else 0
- Proper subtraction a ∸ b: If a ≥ b then a-b else 0
- Minimum(a
_{1}, ... a_{n}) - Maximum(a
_{1}, ... a_{n}) - Absolute difference: | a-b | =
_{def}(a ∸ b) + (b ∸ a) - ~sg(a): NOT[signum(a)]: If a=0 then 1 else 0
- sg(a): signum(a): If a=0 then 0 else 1
- a | b: (a divides b): If b=k×a for some k then 0 else 1
- 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 reverse, namely to represent*true*by 1 and*false*by 0, which amounts to changing 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 of p_{i}in a: the unique x such that p_{i}^{x}|a & NOT(p_{i}^{x'}|a) - lh(a): the "length" or number of non-vanishing exponents in a
- lo(a, b): (logarithm of a to base b): If a, b > 1 then the greatest x such that b
^{x}| a else 0

*In the following, the abbreviation***x**=_{def}x_{1}, ... 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, COURSE-φ(y; x_{2}, ... x_{n}), x_{2}, ... x_{n}then φ is primitive recursive in χ. The value COURSE-φ(y;**x**_{2 to n}) of the course-of-values function encodes the sequence of values φ(0,**x**_{2 to n}), ..., φ(y-1,**x**_{2 to n}) of the original function.

- φ(y,

## Use in first-order Peano arithmetic

In first-order Peano arithmetic, there are infinitely many variables (0-ary symbols) but no k-ary non-logical symbols with k>0 other than S, +, *, and ≤. Thus in order to define primitive recursive functions one has to use the following trick by Gödel.

By using a Gödel numbering for sequences, for example Gödel's β function, any finite sequence of numbers can be encoded by a single number. Such a number can therefore represent the primitive recursive function until a given n.

Let *h* be a 1-ary primitive recursion function defined by:

- [math]\displaystyle{ h(0) = C }[/math]
- [math]\displaystyle{ h(n+1) = g(n,h(n)) }[/math]

where C is a constant and *g* is an already defined function.

Using Gödel's β function, for any sequence of natural numbers (k_{0}, k_{1}, ..., k_{n}), there are natural numbers b and c such that, for every i ≤ n, β(b, c, i) = k_{i}. We may thus use the following formula to define *h*; more precisely, *m*=*h*(*n*) is a shorthand for the following:

- [math]\displaystyle{ \exists b: \exists c: \beta(b, c, 0) = C \land \forall i: (i\lt n) \rightarrow (\beta(b, c, i+1) = g(i,\beta(b, c, i))) \land (m = \beta(b, c, n)) }[/math]

and the equating to *g*, being already defined, is in fact shorthand for some other already defined formula (as is β, whose formula is given here).

The generalization to any k-ary primitive recursion function is trivial.

## Elimination of parameters

The primitive recursion schema as given may be replaced by one which makes use of fewer parameters. Let [math]\displaystyle{ w }[/math] be an elementary pairing function, and [math]\displaystyle{ \pi_1,\pi_2 }[/math] be its projection functions for inversion.

Theorem: Any function constructible via the clauses of primitive recursion using the standard primitive recursion schema is constructible when the schema is replaced with the following.

- [math]\displaystyle{ f'(x,0) = g'(x) }[/math]
- [math]\displaystyle{ f'(x,y+1) = h'(f'(x,y)) }[/math]

This is proven by providing two intermediate schemata for primitive recursion, starting with a function defined via the standard schema, and translating the definition into terms of each intermediate schema and finally into terms of the above schema. The first intermediate schemata is as follows:

- [math]\displaystyle{ f_1(x,0) = g_1(x) }[/math]
- [math]\displaystyle{ f_1(x,y+1) = h_1(x,y,f_1(x,y)) }[/math]

Translation of the standard definition of a primitive recursive function to the intermediate schema is done inductively, where an elementary pairing function [math]\displaystyle{ w }[/math] is used to reinterpret the definition of a [math]\displaystyle{ k+1 }[/math]-ary primitive recursive function into a [math]\displaystyle{ k }[/math]-ary primitive recursive function, terminating the induction at [math]\displaystyle{ k=1 }[/math].

The second intermediate schema is as follows, with the [math]\displaystyle{ x }[/math] parameter eliminated.

- [math]\displaystyle{ f_2(x,0)=g_2(x) }[/math]
- [math]\displaystyle{ f_2(x,y+1)=h_2(y,f_2(x,y)) }[/math]

Translation to it is accomplished by pairing [math]\displaystyle{ x }[/math] and [math]\displaystyle{ f_1(x,y) }[/math] together to use one parameter for handling both, namely by setting [math]\displaystyle{ g_2(x)=w(x,g_1(x)) }[/math], [math]\displaystyle{ h_2(x,z)=w(\pi_1 z,h_1(\pi_1 z, x, \pi_2 z)) }[/math], and recovering [math]\displaystyle{ f_1(x,y) }[/math] from these paired images by taking [math]\displaystyle{ \pi_2(f_2(x,y)) }[/math].

Finally, translation of the intermediate schema into the parameter-eliminated schema is done with a similar pairing and unpairing of [math]\displaystyle{ y }[/math] and [math]\displaystyle{ f_2(x,y) }[/math]. Composing these three translations gives a definition in the original parameter-free schema.^{[3]}

This allows primitive recursion to be formalized in Peano arithmetic, due to its lack of extra *n*-ary function symbols.^{[citation needed]}

## 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 (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:

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

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

## 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:

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:

- 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.
- The Sudan function
- The Goodstein function

## Variants

### Constant functions

Instead of [math]\displaystyle{ C_n^k }[/math],
alternative definitions use just one 0-ary *zero function* [math]\displaystyle{ C_0^0 }[/math] as a primitive function that always returns zero, and built the constant functions from the zero function, the successor function and the composition operator.

### Weak primitive recursion

The 1-place predecessor function is primitive recursive, see section #Predecessor. Fischer, Fischer & Beigel^{[5]} removed the implicit predecessor from the recursion rule, replacing it by the weaker rule

- [math]\displaystyle{ \begin{array}{lcl} f (0, x_1, \ldots, x_k) & = & g (x_1, \ldots, x_k) & \text{and} \\ f (S(y), x_1, \ldots, x_k) & = & h (S(y), f (y, x_1, \ldots, x_k), x_1, \ldots, x_k) \end{array} }[/math]

They proved that the predecessor function still could be defined, and hence that "weak" primitive recursion also defines the primitive recursive functions.

### Iterative functions

Weakening this even further by using functions [math]\displaystyle{ h }[/math] of arity *k*+1, removing [math]\displaystyle{ y }[/math] and [math]\displaystyle{ S(y) }[/math] from the arguments of [math]\displaystyle{ h }[/math] completely, we get the *iteration rule*:

[math]\displaystyle{ \begin{array}{lcll} f(0,x_1,\ldots,x_k) & = & g(x_1,\ldots,x_k) &\textrm{and} \\ f(S(y),x_1,\ldots,x_k) & = & h(f(y,x_1,\ldots,x_k),x_1,\ldots,x_k) \end{array} }[/math]

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

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

### 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, 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,^{[6]} 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.

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

## History

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

Primitive recursive arithmetic was first proposed by Thoralf Skolem^{[10]} 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.^{[8]}^{[9]}

## See also

- Grzegorczyk hierarchy
- Recursion (computer science)
- Primitive recursive functional
- Double recursion
- Primitive recursive set function
- Primitive recursive ordinal function
- Tail call

## Notes

- ↑ Brainerd and Landweber, 1974
- ↑ Kleene [1952 pp. 226–227]
- ↑ H. E. Rose,
*Subrecursive Functions and Hierarchies*(1984), pp.33--34. Oxford Logic Guides: 9, Oxford University Press, ISBN 0-19-853189-3. - ↑ 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 Linz, Peter (2011),
*An Introduction to Formal Languages and Automata*, Jones & Bartlett Publishers, p. 332, ISBN 9781449615529, https://books.google.com/books?id=hsxDiWvVdBcC&pg=PA332. For the latter, see Moore, Cristopher; Mertens, Stephan (2011),*The Nature of Computation*, Oxford University Press, p. 287, ISBN 9780191620805, https://books.google.com/books?id=jnGKbpMV8xoC&pg=PA287 - ↑
^{5.0}^{5.1}Fischer, Fischer & Beigel 1989. - ↑ Meyer, Albert R.; Ritchie, Dennis M. (1967). "The complexity of loop programs". ACM '67: Proceedings of the 1967 22nd national conference. doi:10.1145/800196.806014.
- ↑ Peter Smith (2013).
*An Introduction to Gödel's Theorems*(2nd ed.). Cambridge University Press. pp. 98–99. ISBN 978-1-107-02284-3. - ↑
^{8.0}^{8.1}George Tourlakis (2003).*Lectures in Logic and Set Theory: Volume 1, Mathematical Logic*. Cambridge University Press. p. 129. ISBN 978-1-139-43942-8. - ↑
^{9.0}^{9.1}Rod Downey, ed (2014).*Turing's Legacy: Developments from Turing's Ideas in Logic*. Cambridge University Press. p. 474. ISBN 978-1-107-04348-0. - ↑ Thoralf Skolem (1923) "The foundations of elementary arithmetic" in Jean van Heijenoort, translator and ed. (1967)
*From Frege to Gödel: A Source Book in Mathematical Logic, 1879-1931*. Harvard Univ. Press: 302-33.

## References

- Brainerd, W.S., Landweber, L.H. (1974),
*Theory of Computation*, Wiley, ISBN 0-471-09585-0 - Fischer, Michael J.; Fischer, Robert P.; Beigel, Richard (November 1989). "Primitive Recursion without Implicit Predecessor".
*ACM SIGACT News***20**(4): 87–91. doi:10.1145/74074.74089. https://doi.org/10.1145/74074.74089. - 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 doi:10.2178/jsl/1230396909 JSTOR 275903221

Original source: https://en.wikipedia.org/wiki/Primitive recursive function.
Read more |