Download recursive function

Document related concepts
no text concepts found
Transcript
CD5560
FABER
Formal Languages, Automata
and Models of Computation
Lecture 14
Mälardalen University
2006
1
Rices sats
Om  är en mängd av Turing-accepterbara
språk som innehåller något men inte alla
sådana språk, så kan ingen TM avgöra för
ett godtyckligt Turing-accepterbart språk L
om L tillhör  eller ej.
(Varje icke-trivial egenskap av Turingaccepterbara språk är oavgörbar.)
2
Exempel
Avgörbart? Motivera!
a) ”Är L(M) oändlig?” Givet att M är en godtycklig DFA.
b) ”Är L(M) oändlig?” Givet att M är en godtycklig TM.
Svar
a) AVGÖRBART! Man behöver bara kontrollera om M innehåller
någon slinga på väg till acceptans, vilket kan göras i ändligt många
steg. Se Sallings bok uppgift 7.2.
b) OAVGÖRBART! Följer av Rices sats, om man väljer  som
mängden av alla oändliga Turingaccepterbara språk, eftersom
denna mängd är icketrivial.
3
Recursion
In computer programming, recursion is
related to performing computations in a
loop.
4
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.
5
"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
6
Recursion can be seen as concept of welldefined self-reference.
We use recursion frequently. Consider, for
example, the following hypothetical
“definition of a Jew”. I found it on web, as a
joke.
“Somebody is a Jew if she is Abraham's wife
Sarah, or if his or her mother is a Jew.”
(My digression: I wonder what about Abraham?)
7
So if I want to know if I am a Jew, I look at
this definition. I'm not Sarah, so I need
to know whether my mother is a Jew.
How do I know about my mother? I look at
the definition again. She isn't Sarah
either, so I ask about her mother.
I keep going back through the generations
- recursively.
8
Self-referential definitions can be dangerous
if we're not careful to avoid circularity.
The definition ''A rose is a rose'‘* just doesn't
cut it.
This is why our definition of recursion
includes the word well-defined.
*Know Gertrude Stein? '' A rose is a rose is a rose''
9
Yet another recursive definition: an
immigrant…
We can write 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 some authors (Rudbeckius) Adam and Eve were Swedish.]
10
Functions
From math classes, we have seen many
ways of defining and combining numerical
functions.
– Inverse
– Composition
– Derivatives
– Iteration
f-1
f◦g
f´(x), f´´(x), …
f1(x), f2(x), f3(x), f4(x), …
–…
11
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 cumbersome!
As alternative, look at a more intuitive
definition of functions.
12
Notation
For brevity, limit to functions on natural numbers
N = {0,1,2,…}
Notation will also use n-tuples of numbers
(m1, …, mn)
13
Natural Numbers
Start with standard recursive definition of
natural numbers (remember Peano?):
A natural number is either
• 0, or
• successor(n), where n is a
natural number.
14
What is a recurrence?
A recurrence is a well-defined
mathematical function written in terms
of itself.
It is a mathematical function defined
recursively.
15
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.)
16
F(0) = 1 F(1) = 1 (base cases)
F(n) = F(n - 1) + F(n - 2)
F is called a recurrence, since it is defined in
terms of itself evaluated at other values.
17
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.)
18
Computable Function
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 also be coded
using a combination of for- and while-loops.
19
Primitive Recursive Function
A function which can be implemented
using only for-loops.
Total Function
A function defined for all possible input
values.
20
An example function
2
Domain
3
f ( n)  n  1
f (3)  10
Range
10
21
We need a set of basic functions.
We need a way to define functions.
22
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
23
Building functions
Composition
f ( x, y)  h( g1( x, y), g2 ( x, y))
24
Composition, Generally
Given
g1 : Nk  N
...
gm : Nk  N
Create h : Nk  N
f : Nm  N
h(n1,…,nk) = f(g1(n1,…,nk), …, gm(n1,…,nk))
h = f ◦ (g1,…,gm)
Alternate notation.
25
Primitive Recursion “Template”
f ( x,0)  g1( x)
f ( x, y  1)  h( g2 ( x, y), f ( x, y))
N.B. For primitive recursive functions
recursion in only one argument.
26
Any function built from
the basic primitive recursive functions
is called Primitive Recursive Function.
27
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
28
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))
29
Basic Primitive Successor function
x
0 1 2 
x

succ( x ) 1 2 3  x  1 
...
30
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..
31
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
32
A Primitive Recursive Function add ( x, y )
add ( x, 0)  x
(projection)
add ( x, y  1)  succ(add ( x, y ))
(successor function)
33
Example
add (3, 2)  succ ( add (3,1))
 succ ( succ ( add (3, 0)))
 succ ( succ (3))
 succ ( 4)
5
34
Example
add (3, 2)  (add (3,1))  1
 (add (3, 0)  1)  1
 (3  1)  1
 4 1
5
35
Basic Primitive Predecessor function
x
0 1 2 
x

pred ( x ) 0 0 1  x  1 
...
36
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.
37
Subtraction
sub( y , x )  y  x
sub( y,0)  y
sub( y, succ( x))  pred ( sub( y, x))
( y  ( x  1)  ( y  x )  1)
38
Example
sub(3, 2)  pred ( sub(3,1))
 pred ( pred ( sub (3, 0)))
 pred ( pred (3))
 pred (2)
1
39
A Primitive Recursive Function mult ( x, y)
mult ( x,0)  0
mult ( x, y  1)  add ( x, mult ( x, y))
( x ( y  1)  ( xy )  x )
40
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
41
A Primitive Recursive Function exp ( x, y )
exp (0, x)  1
exp ( x  1, y)  mult (exp ( x, y), y)
(y
x 1
 y  y)
x
42
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
43
Primitive Recursion: 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.
44
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 
45
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 )))
46
More Recursive Predicates...
equal ( x, y)  and (non(less( x, y)), non(less( y, x)))
non _ equal ( x, y )  non(equal ( x, y ))
47
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.
48
Example
Another version of prime(n)
prime(n) = n2  i<n, (i1  mod(n,i) > 0)
mod(m,n) = if n>0 then (min i im, div(m,n)n+i=m) else 0
div(m,n)
= if n>0 then (min i im, (i+1)n>m) else 0
49
Function if
if ( x, y, z ) 
z if x  0
y if x  0
50
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
51
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
52
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 )))
53
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
54
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 ))
55
Recursive Predicate divisible
divisible( x, d )  zero ?( remain( x, d ))
56
Recursive Predicate
divisible( x, d )  zero ?( remind ( x, d ))
non _ equal ( x, y )  non(equal ( x, y ))
57
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.
58
Theorem
There is a function that
is not primitive recursive.
Proof
Enumerate the primitive recursive functions
f1, f 2 , f3 , 
59
Define function
g (i )  fi (i )  1
g differs from every fi
g is not primitive recursive
END OF PROOF
60
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
61
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.
62
  Recursive Functions
y( g ( x, y))  smallest y such that g ( x, y)  0
Ackermann’s function is a
  Recursive Function
63
  Recursive Functions
Primitive recursive functions
64
Primitive Recursion: Extended Example
A polynomial function:
f(x,y) = 3x7+ xy – 7y2.
Needs following building blocks:
– constants
– addition
– multiplication
– exponentiation
– subtraction
65
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)
66
Subtraction
sub(m,n) = m-n
sub(0,n)
= 0
= zero(n)
sub(m+1,n) = succ(sub(m,n))
Exponentiation:
exp(m,n) = nm
exp(0,n)
exp(m+1,n)
= 1
= one(n)
= mult(exp(m,n),n)
67
Primitive Recursion: Extended Example
f(x,y) = (3x7+ xy) - 7y2
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))
68
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.
69
Partial Recursive
A function is partial recursive 
it can be defined by the previous constructions.
A function is recursive 
it is partial recursive and total.
70
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
71
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.
72
More 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).
73
Factorials
n! n  (n  1)  1))
fact (0)  1
fact ( x  1)  mult ( fact ( x), ( x  1))
74
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))
75
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.
76
square?(5)  h(5,0)
h(5,0)  if (less(mult (0,0),5), h(5,1), equal(5, mult (0,0))
...
etc
77
Midterm Exam 3
Restriction-Free Languages
Place: the LAMBDA examination hall
Time: on Tuesday 2004-05-30, 10:15-12:00
It is OPEN BOOK.
(This means you are allowed to bring in one book
of your choice.)
It will cover Turing Machines/Restriction-free
Languages).
You will have the two hours to do the test.
78