@misc{models-of-computation-primitive-recursion,
title = {{Primitive recursive functions}},
howpublished = {Lesson 5 of \emph{Models of computation}, Computability Theory course notes},
url = {/courses/models-of-computation/primitive-recursion},
note = {Unreleased working version}
}
To cite a single statement, use the # link that appears next to its heading.
Lesson 5 of 13
Primitive recursive functions
Definition of the primitive recursive functions and predicates, and their closure under definition by cases, bounded sums and products, bounded quantification and bounded minimization.
Machines describe computations step by step. A different approach, due to Dedekind, Skolem, Gödel, Herbrand and Kleene, describes computable functions by the way they are defined: one starts from a few functions whose computability is evident, and closes them under operations which evidently preserve computability. In this lesson we study the functions obtained using composition and recursion on one argument. They form a large class of total computable functions, and the tools developed here are used throughout the course.
The class of primitive recursive functions is the smallest class of functions Nk→N (k≥1) containing the basic functions and closed under composition and primitive recursion.
Equivalently, a function is primitive recursive if it has a derivation: a finite sequence of functions f1,…,fn, each of which is a basic function or is obtained from earlier functions of the sequence by composition or primitive recursion, the last one being the function in question.
By induction on derivations. Basic functions are total and computable. If g1,…,gm and h are, so is their composition: compute the values gi(x), then h on them. If g and h are, then f(x,y) is computed by the loop: v←g(x); for z=0,…,y−1, v←h(x,z,v); return v. This loop is executed exactly y times, so it terminates.
Derivations are finite objects, so there are countably many primitive recursive functions, and their derivations can be effectively listed. This remark is the starting point of the proof, in the lesson The Ackermann function, that not every total computable function is primitive recursive.
The primitive recursive functions are closed under explicit transformations: if f:Nm→N is primitive recursive and i1,…,im∈{1,…,k}, then g(x1,…,xk)=f(xi1,…,xim) is primitive recursive. In particular one may permute variables, identify them, and add dummy variables. The constant functions cak(x)=a are primitive recursive.
χ<(x,y)=sg(y−˙x), χ≤(x,y)=χ<(x,y+1), χ=(x,y)=sg(∣x−y∣), and χ==1−˙χ=. For the connectives, χ¬P=1−˙χP, χP∧Q=χP⋅χQ, χP∨Q=sg(χP+χQ), and P→Q is ¬P∨Q.
Let P1,…,Pn⊆Nk be primitive recursive predicates such that every x satisfies exactly one of them, and let g1,…,gn:Nk→N be primitive recursive. Then the function f defined by f(x)=gi(x) if Pi(x) is primitive recursive.
For v<y, the product ∏u≤vsg(χP(x,u)) equals 1 if no u≤v satisfies P(x,u), and 0 otherwise. Hence
f(x,y)=v<y∑u≤v∏sg(χP(x,u))
counts the v<y that are smaller than every witness. If the least witness z0<y exists, these are exactly the v<z0, so the sum is z0; otherwise all v<y are counted and the sum is y. By Proposition 5.9, f is primitive recursive.
5.6 Arithmetic functions and primes
With bounded minimization at hand, most functions of elementary number theory are seen to be primitive recursive.
Quotient and remainder.⌊x/y⌋=μq<x+1((q+1)⋅y>x) for y≥1 (and 0 for y=0, by cases), and xmody=x−˙y⋅⌊x/y⌋.
Divisibility.y∣x⟺∃q≤x(q⋅y=x).
Primality.Prime(x)⟺x≥2∧∀y<x(y∣x→y≤1).
Integer square root.⌊x⌋=μr<x+1((r+1)2>x).
Cantor pairing.⟨x,y⟩=⌊(x+y)(x+y+1)/2⌋+y, and its inverses π0(z)=μx<z+1∃y≤z⟨x,y⟩=z and similarly for π1.
Euclid’s argument shows that pn!+1 has a prime factor larger than pn, so pn+1≤pn!+1. Hence
p0=2,pn+1=μz<pn!+2(z>pn∧Prime(z)).
This is a primitive recursion with h(n,v)=μz<v!+2(z>v∧Prime(z)), which is primitive recursive by Proposition 5.12.
Finally, every x≥1 is uniquely a product ∏ipiei. The exponent of pi in x is
(x)i=μe<x¬(pie+1∣x)for x≥1,(0)i=0,
which is primitive recursive. These exponents are the basis of the coding of finite sequences in the next lesson.
5.7 Exercises
Exercise 5.14★.
Show that gcd(x,y) and lcm(x,y) are primitive recursive.
Solution
gcd(x,y)=μd<x+y+1(d∣x∧d∣y∧∀e≤x+y(e∣x∧e∣y→e≤d)), which is correct for (x,y)=(0,0); set gcd(0,0)=0 by cases. For x,y≥1, lcm(x,y)=⌊xy/gcd(x,y)⌋, and lcm(x,y)=0 if x=0 or y=0.
Exercise 5.15★.
Show that the primitive recursive functions are closed under simultaneous recursion: if g1,g2,h1,h2 are primitive recursive, so are the functions f1,f2 defined by fi(x,0)=gi(x) and fi(x,y+1)=hi(x,y,f1(x,y),f2(x,y)). Deduce that the Fibonacci sequence is primitive recursive.
Solution
Let F(x,y)=⟨f1(x,y),f2(x,y)⟩. Then F(x,0)=⟨g1(x),g2(x)⟩ and F(x,y+1)=⟨h1(x,y,π0(F),π1(F)),h2(x,y,π0(F),π1(F))⟩ where F stands for F(x,y). This is a primitive recursion, and fi=πi−1∘F. For Fibonacci, take f1(y)=fib(y), f2(y)=fib(y+1), with f1(y+1)=f2(y) and f2(y+1)=f1(y)+f2(y).
Exercise 5.16★★.
Show that if f:N→N is primitive recursive and strictly increasing, then its range is a primitive recursive set. Is it true for every primitive recursive f?
Solution
If f is strictly increasing, then f(z)≥z, so y∈imf⟺∃z≤yf(z)=y, a bounded quantification. Without monotonicity this fails in general: there are primitive recursive functions whose range is not even computable. Fix a coding of Turing machines by integers in which every integer codes a machine, and a code a of a machine which halts on input a. Let f(⟨e,t⟩)=e if the machine of code e halts on input e within t steps, and f(⟨e,t⟩)=a otherwise. Step-bounded simulation is primitive recursive (see the lesson Equivalence of the models), so f is primitive recursive, and its range is the halting set {e:the machine of code e halts on input e}, which is not computable (see the lesson The halting problem).
5.8 Notes and references
Definitions by recursion go back to Dedekind (1888) and Skolem (1923). Gödel used the primitive recursive functions — he called them recursive — to arithmetize syntax in his incompleteness theorem; the modern name is due to Kleene. Rózsa Péter’s monograph is the classical reference on the subject.
K. Gödel, Über formal unentscheidbare Sätze der Principia Mathematica und verwandter Systeme I, Monatshefte für Mathematik und Physik 38 (1931), 173–198.
R. Péter, Rekursive Funktionen, Akadémiai Kiadó, 1951.
P. Odifreddi, Classical Recursion Theory, North-Holland, 1989, chapter I.