General Recursive Definitions
description
Transcript of General Recursive Definitions
General Recursive Definitions
Lecture 37
Section 8.4
Fri, Mar 25, 2005
Recursively Defined Sets
The definition of a recursively defined set consists of three parts.Base – Specific objects are in the set.Recursion – Rules describing how to form
new members from other members.Restriction – No other elements are in the
set.
Boolean Expressions
Define Boolean expressions as follows.Base – Each lowercase letter of the
alphabet represents a Boolean expression and T and F are Boolean expressions.
Recursion – If p and q are Boolean expressions, then so are p q, p q, p and (p).
Restriction – There are no other Boolean expressions.
Strings Not Containing the Substring 11
Define a set of strings as follows.Base – , 0, and 1 are in the set.Recursion – If s is in the set, then so is 0s
and 10s.Restriction – No other strings are in the set.
The set consists of all strings that do not contain the substring 11.
The MIU System
Define the strings in the MIU system as follows.Base – MI is in the MIU system.
The MIU System
Recursion• If xI is in the MIU system, then so is xIU.• If Mx is in the MIU system, then so is Mxx.• If xIIIy is in the MIU system, then so is xUy.• If xUUy is in the MIU system, then so is xUy.
Restriction – There are no other strings in the MIU system.
The MIU System
Derive a few strings in the MIU system.MI MIUMI MII MIIII MUIMI MII MIIII MIIIIIIII MUIIIII
MUUII MUII
The MU Puzzle
Is MU in the MIU system?
The MU Puzzle
Let n be the number of I’s in a string in the MIU system.
If we apply Rule 1, then the resulting string still contains n I’s.
If we apply Rule 2, then the resulting string contains 2n I’s.
If we apply Rule 3, then the resulting string contains n – 3 I’s.
If we apply Rule 4, then the resulting string still contains n I’s.
The MU Puzzle
Initially, n is 1. Only Rules 2 and 3 change the number of
I’s. Thus, the following changes are possible.
n 2n.n n – 3.
The MU Puzzle
The first change will map A multiple of 3 to a multiple of 3, and A non-multiple of 3 to a non-multiple of 3.
The second change will also map A multiple of 3 to a multiple of 3, and A non-multiple of 3 to a non-multiple of 3.
Given that n = 1 in the initial string and 1 is not a multiple of 3, it is impossible to eliminate I from the strings.
Arithmetic Expressions
Define a set of arithmetic expressions as follows.Base – Any number is an arithmetic
expression.
Arithmetic Expressions
Recursion – If x and y are arithmetic expressions, then so are
• x + y• x – y• x * y• x / y• (x)
Restriction – There are no other arithmetic expressions.
Arithmetic Expressions
Derive the arithmetic expression
(2 + 3) * (10 / 2 – 4)
Pointer Arithmetic
Pointer- and integer-valued expressions may be defined recursively.
BaseAny letter may represent a pointer-valued
expression.Any letter may represent an integer-valued
expression.
Pointer Arithmetic
Recursion – Let p and q be pointer-valued expressions and i and j be integer-valued expressions.i + j is an integer-valued expression.i – j is an integer-valued expressionp – q is an integer-valued expression.(i) is an integer-valued expression.
Pointer Arithmetic
p + i is a pointer-valued expression.p – i is a pointer-valued expression.(p) is a pointer-valued expression.
Restriction – No other expressions are in the set.
Pointer Arithmetic
Which expressions are legal expressions? Which are integer-valued and which are pointer-
valued? p + (q + i) (p + q) + i (p – q) + i (p – q) + (r – s) (p + i) – (q – j) (p – (q – j)) + i (p – q) + (j + i)
Recursively Defined Functions
Mathematical functions may be defined recursively.
The classic example is the factorial function:0! = 1,n! = n (n – 1)! for all n 1.
Recursively Defined Functions
Given the “successor” function
succ(n) = n + 1,
addition of nonnegative integers may be defined recursively.
• sum(0, 0) = 0,• sum(m, 0) = succ(sum(m – 1, 0)) for all m 1,• sum(m, n) = succ(sum(m, n – 1)) for all n 1.
Recursively Defined Functions
Given the sum() function just defined, how could we define multiplication of nonnegative integers?prod(m, n) = ?
The Ackermann Function
Define the Ackermann function
A : Znonneg Znonneg Z+
byA(0, n) = n + 1, n 0.A(m, 0) = A(m – 1, 1), m > 0.A(m, n) = A(m – 1, A(m, n – 1)), m > 0, n >
0.
The Ackermann Function
A(0, 0) = 1. A(0, 1) = 2. A(0, 2) = 3. A(0, 3) = 4. In general, A(0, n) = n + 1.
The Ackermann Function
A(1, 0) = A(0, 1) = 2. A(1, 1) = A(0, A(1, 0)) = A(0, 2) = 3. A(1, 2) = A(0, A(1, 1)) = A(0, 3) = 4. A(1, 3) = A(0, A(1, 2)) = A(0, 4) = 5. In general, A(1, n) = n + 2.
The Ackermann Function
A(2, 0) = A(1, 1) = 3. A(2, 1) = A(1, A(2, 0)) = A(1, 3) = 5. A(2, 2) = A(1, A(2, 1)) = A(1, 5) = 7. A(2, 3) = A(1, A(2, 2)) = A(1, 7) = 9. In general, A(2, n) = 2n + 3.
The Ackermann Function
A(3, 0) = A(2, 1) = 5. A(3, 1) = A(2, A(3, 0)) = A(2, 5) = 13. A(3, 2) = A(2, A(3, 1)) = A(2, 13) = 29. A(3, 3) = A(2, A(3, 2)) = A(2, 29) = 61. In general, A(3, n) = 2n + 3 – 3.
The Ackermann Function
A(4, 0) = A(3, 1) = 13. A(4, 1) = A(3, A(4, 0)) = A(3, 13) = 216 – 3
= 65533. A(4, 2) = A(3, 65533) = 265533 – 3. A(4, 3) = ? A(4, 4) = ? A(5, 5) = ?