You are on page 1of 29

Primitive Recursive Functions

(Chapter 3)

1
Preliminaries: partial and total functions
 The domain of a partial function on set A contains the subset of A.
 The domain of a total function on set A contains the entire set A.

 A partial function f is called partially computable if there is


some program that computes it. Another term for such
functions partial recursive.

 Similarly, a function f is called computable if it is both total


and partially computable. Another term for such function is
recursive.
2
Composition
 Let f : A → B and g : B → C
 Composition of f and g can then be expressed as:
g ͦ f:A→C
(g ͦ f)(x) = g(f(x))
h(x) = g(f(x))

 NB: In general composition is not commutative:


( g ͦ f )(x) ≠ ( f ͦ g )(x) 3
Composition
 Definition: Let g be a function containing k
variables and f1 ... fk be functions of n variables, so
the composition of g and f is defined as:
h(0) = k Base step
h( x1, ... , xn) = g( f1(x1 ,..., xn), … , fk(x1 ,..., xn) ) Inductive step

Example: h(x , y) = g( f1(x , y), f2(x , y), f3(x , y) )


 h is obtained from g and f1... fk by composition.
 If g and f1...fk are (partially) computable, then h is
(partially) computable. (Proof by construction) 4
Recursion
 From programming experience we know that recursion refers
to a function calling upon itself within its own definition.

 Definition: Let g be a function containing k variables then


h is obtained through recursion as follows:
h(x1 , … , xn) = g( … , h(x1 , … , xn) )
Example: x + y
f( x , 0 ) = x (1)

f(x , y+1 ) = f( x , y ) + 1 (2)

Input: f ( 3, 2 ) => f ( 3 , 1 ) + 1 => ( f ( 3 , 0 ) + 1 ) + 1 => ( 3 + 1 ) + 1 => 5


PRC: Initial functions
 Primitive Recursively Closed (PRC) class of functions.
 Initial functions:
s(x) = x + 1
n(x) = 0
ui (x1 , … , xn) = xi

 Example of a projection function: u2 ( x1 , x2 , x3 , x4 , x5 ) = x2

 Definition: A class of total functions C is called PRC² class if:


 The initial functions belong to C.
 Function obtained from functions belonging to C by either
composition or recursion belongs to C. 6
PRC: primitive recursive functions
 There exists a class of computable functions that is a
PRC class.
 Definition: Function is considered primitive
recursive if it can be obtained from initial
functions and through finite number of
composition and recursion steps.
 Theorem: A function is primitive recursive iff it
belongs to the PRC class. (see proof in chapter 3)
 Corollary: Every primitive recursive function is 7

computable.
Primitive recursive functions: sum
 We have already seen the addition function, which can
be rewritten in LRR as follows:

sum( x, succ(y) ) => succ( sum( x , y)) ;


sum( x , 0 ) => x ;

Example: sum(succ(0),succ(succ(succ(0)))) => succ(sum(succ(0),succ(succ(0)))) =>


succ(succ(sum(succ(0),succ(0)))) => succ(succ(succ(sum(succ(0),0) =>
succ(succ(succ(succ(0))) => succ(succ(succ(1))) => succ(succ(2)) => succ(3) => 4

NB: To prove that a function is primitive recursive you need show that it
can be obtained from the initial functions using only concatenation and 8
recursion.
Primitive recursive functions: multiplication
h( x , 0 ) = 0
h( x , y + 1) = h( x , y ) + x
 In LRR this can be written as:
mult(x,0) => 0 ;
mult(x,succ(y)) => sum(mult(x,y),x) ;

 What would happen on the following input?


mult(succ(succ(0)),succ(succ(0)))
9
Primitive recursive functions: factorial
0! = 1
( x + 1 ) ! = x ! * s( x )

 LRR implementation would be as follows:


fact(0) => succ(null(0)) ;
fact(succ(x)) => mult(fact(x),succ(x)) ;

Output for the following? fact(succ(succ(null(0))))

10
Primitive recursive functions:
power and predecessor
Power function In LRR the power function can
be expressed as follows:
x0 = 1
x y+1 = x y * x pow(x,0) => succ(null(0)) ;
pow(x,succ(y)) => mult(pow(x,y),x) ;

Predecessor
In LRR the predecessor is as follows:
function
pred(1) => 0 ;
p (0) = 0
p(t+1)=t pred(succ(x)) => x ;

11
Primitive recursive functions:
∸, | x – y | and α
dotsub(x,x) => 0 ;
x∸0=x dotsub(x,succ(y)) => pred(dotsub(x,y)) ;

x ∸ ( t + 1) = p( x ∸ t ) What would be the output?


 dotsub(succ(succ(succ(0))),succ(0))

|x–y|=(x∸y)+(y∸x) abs(x,y) => sum(dotsub(x,y),dotsub(y,x)) ;

α(x) = 1 ∸ x α(x) => dotsub(1,x) ;

1 if x0 Output for the following?


 ( x)    a(succ(succ(0)))
12
0 otherwise  a(null(0))
Primitive recursive functions
x+y f( x , 0 ) = x
f( x , y + 1 ) = f( x , y ) + 1
x*y h( x , 0 ) = 0
h( x , y + 1 ) = h( x , y ) + x
x! 0! = 1
( x + 1 )! = x! * s(x)
x^y x^0 = 1
x^( y + 1 ) = x^y * x
p(x) p( 0 ) = 0
p( x + 1 ) = x
x∸y x∸0=x
if x ≥ y then x ∸ y = x – y; else x ∸ y = 0 x ∸ ( t + 1) = p( x ∸ t )

|x–y| |x–y|=(x∸y)+(y∸x)

α(x) α(x) = 1 ∸ x 13
Bounded quantifiers
 Theorem: Let C be a PRC class. If f( t , x1 , … ,
xn) belongs to C then so do the functions
y

g( y , x1 , ... , xn ) =  f( t , x1 , …, xn )
t 0 y

g( y , x1 , ... , xn ) =  f( t , x1 , …, xn )
t 0

14
Bounded quantifiers
 Theorem: Let C be a PRC class. If f( t , x1 , … ,
xn) belongs to C then so do the functions
y

g( y , x1 , ... , xn ) =  f( t , x1 , …, xn )
t 0
y

g( y , x1 , ... , xn ) =  f( t , x1 , …, xn )
t 0

 Theorem: If the predicate P( t, x1 , … , xn ) belongs to some


PRC class C, then so do the predicates:
(t)≤y P(t, x1, … , xn )
(∃t)≤y P(t, x1, … , xn )
15
Primitive recursive predicates
x=y d( x , y ) = α( | x – y | )

x≤y α(x∸y)

~P α( P )

P&Q P*Q

PvQ ~ ( ~P & ~Q )

y|x y | x = (∃t)≤x { y * t = x }

Prime(x) Prime(x) = x > 1 & (t)≤x { t = 1 v t = x v ~( t | x ) }

Exercises for Chapter 3: page 62 Questions 3,4 and 5. Fibonacci function


16
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u

u=0 t=0

17
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

18
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.

19
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).

20
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).

t < t0 :  P t, x1,..., xn  = 1  True


t  t0 :  P t, x1,..., xn  = 0  False

21
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 fort<t 0
,where P (t 0, x1, . . . ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).
  Pt, x .., xn  = 1if u < t0
u

t < t0 :  P t, x1,..., xn  = 1  True


1,.
t =0

t  t0 :  P t, x1,..., xn  = 0  False


  Pt, x .., xn  = 0if u  t0
u

1,.
t =0

22
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).
  Pt, x .., xn  = 1if u < t0
u

t < t0 :  P t, x1,..., xn  = 1  True


1,.
t =0 g (y,x 1, . . . ,xn )= ∑ 1= 1=t 0 for u<t 0
t  t0 :  P t, x1,..., xn  = 0  False
  Pt, x .., xn  = 0if u  t0
u

1,.
t =0

23
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1,. .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).
  Pt, x .., xn  = 1if u < t0
u

t < t0 :  P t, x1,..., xn  = 1  True


1,.
t= 0 g (y,x 1, . . . ,xn )= ∑ 1= 1=t 0 for u<t 0
t  t0 :  P t, x1,..., xn  = 0  False
  Pt, x .., xn  = 0if u  t0
u

1,.
t= 0

 g( y , x1 , ... , xn ) produces the least value for which P is true. Finally the
definition for bounded minimalization can be given as:
min
t y
Pt, x1,. .., xn = g  y, x1,. .., xn  if t t  y Pt, x1,. .., xn 
min
t y
Pt, x1,. .., xn = 0otherwise

24
Bounded minimalization
Let P(t, x1, … ,xn) be in some PRC class C and we can define a function g as follows:

g t, x1,..., xn  =  Pt, x1,..., xn 


y u
P (t,x 1, . . . ,xn )= 0 for t<t 0
,where P (t 0, x 1, . .. ,x n )= 1
u=0 t=0

 Function g also belongs to C as it is attained from composition of primitive


recursive functions.
 t0 is the least value for for which the predicate P is true (1).
  Pt, x .., xn  = 1if u < t0
u

t < t0 :  P t, x1,..., xn  = 1  True


1,.
t= 0 g (y,x 1, . . . ,xn )= ∑ 1= 1=t 0 for u<t 0
t  t0 :  P t, x1,..., xn  = 0  False
  Pt, x .., xn  = 0if u  t0
u

1,.
t= 0

 g( y , x1 , ... , xn ) produces the least value for which P is true. Finally the
definition for bounded minimalization can be given as:
min
t y
Pt, x1,. .., xn = g  y, x1,. .., xn  if t t  y Pt, x1,. .., xn 
min
t y
Pt, x1,. .., xn = 0otherwise

 Theorem: If P(t,x1, … ,xn) belongs to some PRC class C and there is function g 25
that does the bounded minimalization for P, then f belongs to C.
Unbounded minimalization
min P ( x1,. .. , x n , y )
y

 Definition: y is the least value for which predicate P is true if it exists.


If there is no value of y for which P is true, the unbounded
minimalization is undefined.

26
Unbounded minimalization
min P ( x1,. .. , x n , y )
y

 Definition: y is the least value for which predicate P is true if it exists.


If there is no value of y for which P is true, the unbounded
minimalization is undefined.
 We can then define this as a non-total function in the following way:
x− y= min [ y+ z= x ]
z

27
Unbounded minimalization
min P ( x1,. .. , x n , y )
y

 Definition: y is the least value for which predicate P is true if it exists.


If there is no value of y for which P is true, the unbounded
minimalization is undefined.
 We can then define this as a non-total function in the following way:
x− y= min [ y+ z= x ]
z

 Theorem: If P(x1, … , xn, y) is a computable predicate and if


g ( x1,. .. , xn )= min P (x1,. .. , x n , y)
y

then g is a partially computable function.


28
(Proof by construction)
Additional primitive recursive functions
 [ x / y ] , the whole part of the division i.e. [10/4]=2

x / y= mt inx t +1* y > x

 R(x,y) , remainder of the division of x by y.

Rx, y  = x -  y * x / y

 pn , nth prime number i.e p1=2 , p2=3 etc.


p0 = 0,
pn+ 1= min [ Prime(t)& t> pn ] 29
t < p n! + 1

You might also like