Download presentation
Presentation is loading. Please wait.
Published byMelvyn Fitzgerald Modified over 9 years ago
1
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 14 Mälardalen University 2010
2
2 Content Recursive Functions Primitive Recursion Ackermann function & -recursive functions Relations Among Function Classes
3
3 Recursion In computer programming, recursion is related to performing computations in a loop.
4
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
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
6 Recursion can be seen as concept of well- defined 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
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
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
9 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.] Yet another recursive definition: an immigrant…
10
10 Functions From math classes, we have seen many ways of defining and combining numerical functions. –Inversef -1 –Compositionf ◦ g –Derivativesf´(x), f´´(x), … –Iterationf 1 (x), f 2 (x), f 3 (x), f 4 (x), … –…
11
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
12 Notation For brevity, limit to functions on natural numbers N = {0,1,2,…} Notation will also use n-tuples of numbers (m 1, …, m n )
13
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
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
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
16 F is called a recurrence, since it is defined in terms of itself evaluated at other values. F(0) = 1F(1) = 1 (base cases) F(n) = F(n - 1) + F(n - 2)
17
17 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. Recursion & Recurrence (*Recurrence is a mathematical function defined recursively.)
18
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
19 Total Function A function defined for all possible input values. Primitive Recursive Function A function which can be implemented using only for-loops.
20
20 An example function DomainRange
21
21 We need a way to define functions. We need a set of basic functions.
22
22 Zero function: Successor function: Projection functions: Basic Primitive Recursive Functions
23
23 Building functions Composition
24
24 Composition, Generally Given g 1 : N k N. g m : N k N f : N m N h(n 1,…,n k ) = f(g 1 (n 1,…,n k ), …, g m (n 1,…,n k )) h = f ◦ (g 1,…,g m )Alternate notation. Create h : N k N
25
25 Primitive Recursion “Template” N.B. For primitive recursive functions recursion in only one argument.
26
26 Any function built from the basic primitive recursive functions is called Primitive Recursive Function.
27
27 Basic Primitive Zero function (a constant function) Example
28
28 Basic Primitive Identity function Recursive definition
29
29 Basic Primitive Successor function
30
30 Using Basic Primitive Zero function and a Successor function we can construct Constant functions etc..
31
31 Example
32
32 A Primitive Recursive Function (projection) (successor function)
33
33 Example
34
34 Example
35
35 Basic Primitive Predecessor function
36
36 Predecessor Predecessor is a primitive recursive function with no direct self-reference.
37
37 Subtraction
38
38 Example
39
39 A Primitive Recursive Function
40
40 Example
41
41 A Primitive Recursive Function
42
42 Example
43
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 minimizationmin i i n, f(i) where result = 0 if f(i) never true within bounds.
44
44 Recursive Predicates and
45
45 More Recursive Predicates
46
46 More Recursive Predicates...
47
47 Example Recursive predicates can combine into powerful functions. What does this compute? Tests primality. ???(n) = i n, j n, ((i=1 j=n) (j=1 i=n) i j n)
48
48 prime(n) = n 2 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 Example Another version of prime(n)
49
49 Function
50
50 our construction primitive recursive template
51
51 Division example: x/4
52
52 Division as Primitive Recursion
53
53 Division example: x/4
54
54 Division as Primitive Recursion
55
55 Recursive Predicate
56
56 Recursive Predicate
57
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
58 There is a function that is not primitive recursive. Proof Enumerate the primitive recursive functions Theorem
59
59 Define function differs from every is not primitive recursive END OF PROOF
60
60 A specific function that is not primitive recursive: Ackermann’s function: Grows very fast, faster than any primitive recursive function
61
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
62 Recursive Functions Ackermann’s function is a Recursive Function
63
63 Primitive recursive functions Recursive Functions
64
64 Primitive Recursion: Extended Example Needs following building blocks: –constants –addition –multiplication –exponentiation –subtraction A polynomial function: f(x,y) = 3x 7 + xy – 7y 2.
65
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
66 Exponentiation: exp(m,n) = n m exp(0,n)= exp(m+1,n)= 1 mult(exp(m,n),n) = one(n) Subtraction sub(m,n) = m-n sub(0,n)= sub(m+1,n)= 0=zero(n) succ(sub(m,n))
67
67 Primitive Recursion: Extended Example f(x,y) = (3x 7 + xy) - 7y 2 f = sub◦ (add ◦ (f 1,f 2 ), f 3 ) f 1 (x,y) = mult(3,exp(7,x))f 1 = mult ◦ (three, exp ◦ (seven)) f 2 (x,y) = mult(x,y)f 2 = mult f 3 (x,y) = mult(7,exp(2,y))f 3 = mult ◦ (seven, exp ◦ (two)) f(x,y) = sub(add(f 1 (x,y),f 2 (x,y)),f 3 (x,y))
68
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– when using natural numbers. “True” division– undefined when divisor is 0. Trigonometric functions– undefined for some values. …
69
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
70 Division: div(m,n) = m n div(m,n) = min i, sub(succ(m),add(mult(i,n),n)) = 0 div(m,n) = minimum i such that i mni mn i n m-(n-1) i n+n m+1 (m+1) – (i n+n) 0 (m+1) (i n+n) = 0 Example
71
71 Relations Among Function Classes Functions TMs –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. partial recursive = recognizable recursive = decidable primitive recursive
72
72 More Examples of Primitive Recursion A recursive function is a function that calls itself (by using its own name within its function body). Even
73
73 Factorials
74
74 Is a number a square? Forward recursion ( -recursion)
75
75
76
76
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.