Download recursive function

Document related concepts
no text concepts found
Transcript
CDT314
FABER
Formal Languages, Automata
and Models of Computation
Lecture 14
School of Innovation, Design and Engineering
Mälardalen University
2011
11
Content
Recursive Functions
Primitive Recursion
Ackermann function & -recursive functions
Relations Among Function Classes
2
Recursion
In computer programming, recursion is related to
performing computations in a loop.
3
Recursion in Problem Modelling
Reducing the complexity by
Breaking up computational sequences into its
simplest forms.
Synthesizing components into more complex objects
by replicating simple component sequences over and
over again.
4
"A reduction is a way of converting one problem into
another problem in such a way that a solution to the
second problem can be used to solve the first
problem."
Michael Sipser, Introduction to the Theory of Computation
5
Self-referential definitions can be dangerous if we're not
careful to avoid circularity.
This is why our definition of recursion includes the word
well-defined.
(Recursion can be seen as concept of well-defined selfreference.)
*Know Gertrude Stein? ''A rose is a rose is a rose''
6
Recursive definition: an immigrant…
We can write a pseudocode to determine whether
somebody is an immigrant:
FUNCTION isAnImmigrant(person):
IF person immigrated herself, THEN:
return true
ELSE:
return isAnImmigrant(person's parent)
END IF
This is a recursive function, since it uses itself to compute
its own value.
[According to Olaus Rudbeckius, Adam and Eve were Swedish.]
7
Functions
From math classes, we have seen many ways of
defining and combining numerical functions.
–Inverse
f 1
–Composition
f g
–Derivatives
f ( x), f ( x), f ( x), 
–Iteration
f 1( x), f 2 ( x), f 3 ( x), 
…
8
Functions
Look at what happens when we use only some of
these.
- How can we define standard interesting functions?
- How do these relate to e.g. TM computations?
We have seen TMs as functions.
They are awkward!
As alternative, look at a more intuitive definition of
functions.
9
Notation
For brevity, limit to functions on natural numbers
N  {0, 1, 2,}
Notation will also use n-tuples of numbers
(m1, , mn )
10
Natural Numbers
Start with standard recursive definition of natural
numbers (Peano axioms)
A natural number is either
0 or
successor(n), where n is a natural number.
11
What is a recurrence?
A recurrence is a well-defined mathematical function
written in terms of itself.
It is a mathematical function defined recursively.
12
Fibonacci sequence
1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...
The first two numbers of the sequence are both 1,
while each succeeding number is the sum of the two
numbers before it.
(We arrived at 55 as the tenth number, since it is the
sum of 21 and 34, the eighth and ninth numbers.)
13
F (0)  1
F (1)  1
F (n)  F (n  1)  F (n  2)
(base cases )
F is called a recurrence, since it is defined in terms of itself
evaluated at other values.
14
Recursion & Recurrence
A recursive process is one in which objects are
defined in terms of other objects of the same type.
Using some sort of recurrence relation*, the entire
class of objects can then be built up from a few initial
values and a small number of rules.
(*Recurrence is a mathematical function defined recursively.)
15
Computable Functions
Any computable function can be programmed using
while-loops (i.e., "while something is true, do
something else").
For-loops (which have a fixed iteration limit) are a
special case of while-loops.
(Computable functions could of course also be coded using a
combination of for- and while-loops. )
16
Primitive Recursive Function
A function which can be implemented using only forloops.
Total Function
A function defined for all possible input values.
17
An example function
2
Domain
3
f ( n)  n  1
f (3)  10
Range
10
18
We need a set of basic functions.
We need a way to define functions.
19
Basic Primitive Recursive Functions
Zero function:
zero ( x)  0
Successor function:
succ ( x)  x  1
Projection functions:
p1( x1, x2 )  x1
p2 ( x1, x2 )  x2
20
Building functions
Composition
f ( x, y)  h( g1( x, y), g2 ( x, y))
21
Composition, Generally
Given
g1 : N k  N
...
gm : N k  N
f : Nm  N







h: Nk  N
h(n1,, nk )  f ( g1(n1,, nk ),, gm (n1,, nk ))
h  f  ( g1,, gm )
22
Primitive Recursion “Template”
f ( x,0)  g1( x)
f ( x, y  1)  h( g2 ( x, y), f ( x, y))
For primitive recursive functions recursion is in only
one argument.
23
Any function built from
the basic primitive recursive functions
is called Primitive Recursive Function.
24
Basic Primitive Zero function
(a constant function)
zero ( x)  0
zero (succ ( x))  zero ( x)
Example
zero(3)  zero(2)  zero(1)  zero(0)  0
25
Basic Primitive Identity function
x
0 1 2  x 
identity( x) 0 1 2  x 
...
Recursive definition
identity(0)  0
identity(succ( x))  succ(ident( x))
26
Basic Primitive Successor function
x
0 1 2 
x

succ( x ) 1 2 3  x  1 
...
27
Using Basic Primitive Zero function
and a Successor function we can
construct Constant functions
one( x)  succ ( zero( x))
two( x)  succ (one ( x))
three( x)  succ (two ( x))
etc..
28
Example
three ( x )  succ (two ( x ))
 succ ( succ (one ( x )))
 succ ( succ ( succ ( zero ( x ))))
 succ ( succ ( succ (0)))
 succ ( succ (1))
 succ ( 2)
3
29
A Primitive Recursive Function add ( x, y )
add ( x, 0)  x
(projection)
add ( x, y  1)  succ(add ( x, y ))
(successor function)
30
Example
add (3, 2)  succ ( add (3,1))
 succ ( succ ( add (3, 0)))
 succ ( succ (3))
 succ ( 4)
5
31
Example
add (3, 2)  (add (3,1))  1
 (add (3, 0)  1)  1
 (3  1)  1
 4 1
5
32
Basic Primitive Predecessor function
x
0 1 2 
x

pred ( x ) 0 0 1  x  1 
...
33
Predecessor
pred ( x)  x  1
pred (0)  0
pred ( succ ( x ))  x
pred (0)  0
pred ( succ ( x ))  G ( x )
primitive recursive template
G(x)  identity( x)
Predecessor is a primitive recursive function with no direct
self-reference.
34
Subtraction
sub( y , x )  y  x
sub( y,0)  y
sub( y, succ( x))  pred ( sub( y, x))
( y  ( x  1)  ( y  x )  1)
35
Example
sub(3, 2)  pred ( sub(3,1))
 pred ( pred ( sub (3, 0)))
 pred ( pred (3))
 pred (2)
1
36
Multiplication
mult ( x, y)
mult ( x,0)  0
mult ( x, y  1)  add ( x, mult ( x, y))
( x ( y  1)  ( xy )  x )
37
Example
mult ( x,4)  add ( x, mult ( x,3))
 add ( x, add ( x, mult ( x,2)))
 add ( x, add ( x, add ( x, mult ( x,1))))
 add ( x, add ( x, add ( x, add ( x, mult ( x,0)))))
 add ( x, add ( x, add ( x, add ( x,0))))
 add ( x, add ( x, add ( x, x)))
 add ( x, add ( x,2 x))
 add ( x,3x)
 4x
38
A Primitive Recursive Function,exponentiation
exp ( x, y )
exp (0, x)  1
exp ( x  1, y)  mult (exp ( x, y), y)
(y
x 1
 y  y)
x
39
Example
exp ( 4, y )  mult ( exp (3, y ), y )
 mult ( mult ( exp ( 2, y ), y ), y )
 mult ( mult ( mult ( exp (1, y ), y ), y ), y )
 mult ( mult ( mult ( mult ( exp (0, y ), y ), y ), y ), y )
 mult ( mult ( mult ( mult (1, y ), y ), y ), y )
 mult ( mult ( mult ( y , y ), y ), y )
 mult ( mult ( y  y , y ), y )
 mult ( y  y  y ), y )
 y  y  y  y  y4
40
Primitive Recursion in Logic
A predicate (Boolean function) with output in the
set {0,1} which is interpreted as {yes, no}, can
be used to define standard functions.
–
–
–
–
–
Logical connectives
 , ,, , …
Numeric comparisons
=, < ,, …
Bounded existential quantification
in, f(i)
Bounded universal quantification
in, f(i)
Bounded minimization
min i in, f(i)
where result = 0 if f(i) never true within bounds.
41
Recursive Predicates
zero ? and non _ zero ?
x
0 1 2 3
zero ?( x )  sub(one( x ), x )
1 0 0 0
non _ zero ?  zero ?( zero ?( x )) 0 1 1 1 
42
More Recursive Predicates
and ( x , y )
or ( x , y )
less ( x, y )
non ( x )
returns
x 0 y 0
x 0 y 0
x y
x0
1
x 0 y 0
x 0 y 0
x y
x0
0
and ( x, y )  non( zero ?( mult ( x, y )))
or ( x, y )  non( zero ?( add ( x, y )))
less ( x, y )  non( zero ?( sub( x, y )))
43
More Recursive Predicates...
equal ( x, y)  and (non(less( x, y)), non(less( y, x)))
non _ equal ( x, y )  non(equal ( x, y ))
44
Example
Recursive predicates can combine into powerful
functions.
What does this compute?
? (n)  i  n, j  n, ((i  1  j  n)  ( j  1  i  n)  i  j  n)
Tests primality!
A prime number (or a prime) is a natural number greater than 1 that
has no positive divisors other than 1 and itself.
45
Function if
if ( x, y, z ) 
z if x  0
y if x  0
46
if (0, y, z )  z
if ( succ( x ), y, z )  y
if (0, y, z )  B( z )
if ( succ( x ), y, z )  G( y )
our construction
primitive recursive
template
f ( x,0)  g1( x)
f ( x, y  1)  h( g2 ( x, y), f ( x, y))
with B  G  identity
47
Division example: x/4
d 4
q  quotient
r  remainder
x
x  qd  r
quotient
remainder
0
0  04  0
0
0
1
1  0 4 1
0
1
2
2  04  2
0
2
3
3  04  3
0
3
4
4  1 4  0
1
0
5
5  1 4  1
1
1
6
6  1 4  2
1
2
7
7  1 4  3
1
3
8
8  24  0
2
0
48
Division as Primitive Recursion
remain ( x, d )  if (less ( x, d ),
x,
remain ( sub( x, d ), d ))
quot ( x, d )  if (less ( x, d ),
0,
succ( quot ( sub( x, d ), d )))
49
remain ( x, d )  if (less ( x, d ),
x,
remain ( sub( x, d ), d ))
x
x  qd  r
quotient
quot ( x, d )  if (less ( x, d ),
0,
succ( quot ( sub( x, d ), d )))
remainder
0
04  0
0
Division
example:
x/40
0
1
1  0 4 1
0
1
2
2  04  2
0
2
q  quotient
3
3  04  3
0
3
r  remainder
4
4  1 4  0
1
0
5
5  1 4  1
1
1
6
6  1 4  2
1
2
7
7  1 4  3
1
3
8
8  24  0
2
0
d 4
50
Division as Primitive Recursion
remain(0, d )  0
remain( succ( x ), d )  if (less ( succ( remain( x, d )), d ),
succ( remain( sub( x, d )),
0)
quot (0, d )  0
quot ( succ( x ), d )  add ( zero ?( remain( succ( x ), d )), quot ( x, d ))
51
Recursive Predicate divisible
divisible( x, d )  zero ?( remain( x, d ))
52
Recursive Predicate
divisible ( x, d )  zero?(remind ( x, d ))
non _ equal ( x, y)  non(equal ( x, y))
53
Theorem
The set of primitive recursive functions
is countable.
Proof
Each primitive recursive function
can be encoded as a string.
Enumerate all strings in proper order.
Check if a string is a function.
54
Theorem
There is a function that is not primitive recursive.
Proof (by Cantor diagonal argument)
Enumerate the primitive recursive functions
f1, f 2 , f3 , 
55
Define function
g (i )  fi (i )  1
g differs from every fi
g is not primitive recursive
END OF PROOF
56
A specific function that is not
primitive recursive:
Ackermann’s function:
A(0, y )  y  1
A( x,0)  A( x  1, 1)
A( x, y  1)  A( x  1, A( x, y ))
Grows very fast,
faster than any primitive recursive function
57
The Ackermann function is the simplest example of a
well defined total function which is computable but
not primitive recursive, providing a counterexample to
the belief in the early 1900s that every computable
function was also primitive recursive.
58
  Recursive Functions
y( g ( x, y))  smallest y such that g ( x, y)  0
Ackermann’s function is a
  Recursive Function
59
  Recursive Functions
Primitive recursive functions
60
Primitive Recursion: Extended Example
A polynomial function:
f ( x, y)  3x  xy  7 y
7
2
Needs following building blocks:
– constants
– addition
– multiplication
– exponentiation
– subtraction
61
Addition
add (m, n)  m  n
add(0,n)
add(m+1,n)
= n
= succ(add(m,n))
Multiplication:
mult (m, n)  m  n
mult(0,n)
mult(m+1,n)
= 0
= add(mult(m,n),n)
62
Subtraction
sub(m, n)  m  n
sub(0,n)
= 0
= zero(n)
sub(m+1,n) = succ(sub(m,n))
Exponentiation:
exp (m, n)  n
exp(0,n)
exp(m+1,n)
m
= 1
= one(n)
= mult(exp(m,n),n)
63
Primitive Recursion: Extended Example
f ( x, y)  3x  xy  7 y
7
2
f(x,y) = sub(add(f1(x,y),f2(x,y)),f3(x,y))
f = sub ◦ (add ◦ (f1,f2), f3)
f1(x,y) = mult(3,exp(7,x))
f1 = mult ◦ (three, exp ◦ (seven))
f2(x,y) = mult(x,y)
f2 = mult
f3(x,y) = mult(7,exp(2,y))
f3 = mult ◦ (seven, exp ◦ (two))
64
Primitive Recursion
All primitive recursive functions are total.
i.e., they are defined for all values.
Primitive recursion lack some interesting functions:
“True” subtraction
“True” division
Trigonometric functions
…
– when using natural numbers.
– undefined when divisor is 0.
– undefined for some values.
65
Partial Recursive vs. Recursive
A function is partial recursive 
it can be defined by the previous constructions.
A function is recursive 
it is partial recursive and total.
66
Example
Division:
div(m,n) = m  n
div(m,n) = minimum i such that
i  mn
in  m-(n-1)
in+n  m+1
(m+1) – (in+n)  0
(m+1)  (in+n) = 0
div(m,n) = min i, sub(succ(m),add(mult(i,n),n)) = 0
67
Relations Among Function Classes
Functions  TMs
partial recursive
= recognizable
recursive
= decidable
primitive
recursive
– Define TMs in terms of the
function formers.
– Straightforward, but long.
TMs  Functions
– Define functions where
subcomputations encode
TM behavior.
– Simple encoding scheme.
– Straightforward, but very
messy.
68
Additional Examples of Primitive Recursion
Even
1, if n is even
even(n) 
0,
otherwise
even(0)  1
even(k  1)  sub(1, even(k ))
A recursive function is a function that calls itself by using its own name within its function body.
69
Factorials
n! n  (n  1) 1
fact (0)  1
fact ( x  1)  mult ( fact ( x), ( x  1))
70
Is a number a square?
square( x)  mult ( x, x)
square?( x)  isSquare( x, x)
isSquare(0, y )  equal (0, y )
isSquare( succ( x), y )  or (equal ( square( succ( x)), y ), isSquare( x, y )
Forward recursion (-recursion)
square?(x)  h( x,0)
h( x, y)  if (less(mult ( y, y), x), h( x, succ( y)), equal( x, mult ( y, y))
71
square?(5)  isSquare(5,5)
isSquare(5,5)  or (equal ( square(5,5), isSquare(4,5)
 or (equal ( square(5,5), or (equal ( square(4,5), isSquare(3,5))))

..............................etc ............................................isSquare(2,5))
..............................etc ............................................isSquare(1,5))
..............................etc ...........................................isSquare(0,5))
isSquare(0,5))  0
5 is not a square of any natural number.
72
square?(5)  h(5,0)
h(5,0)  if (less(mult (0,0),5), h(5,1), equal(5, mult (0,0))
...
etc
73
Exam Examples
Midterm 3 example:
http://www.idt.mdh.se/kurser/cd5560/01_10/examination/DUGGOR/MIDTE
RM3-2010-Solution.pdf
Final exam example:
http://www.idt.mdh.se/kurser/cd5560/01_10/examination/examination/FINA
L-2010-03-18-sol.pdf
74
Related documents