O baid O baid Ullah
1
Chapter 3: Recursive Definitions • A New Method for Defining Languages • Arithmetic Expressions (AE )
Obaid Ullah
2
Introduction • Recursive definition is a useful method for defining sets. • A recursive definition is basically a 3-step process: 1. First, First, we specify some basic basic objects objects in the set. 2. Second, Second, we give rules rules for constructing constructing more more objects in the set from the basic ones we already know. 3. Third, Third, we declare declare that no objects objects except those construct constructed ed in this way are allowed in the set.
• The reason that it is called recursive is that one of the rules used to define the set mentions the set itself, as we shall see in the following examples.
Obaid Ullah
3
Example • Let us define the set of positive even integers called E V E N , using recursive definition:
• Rule 1: 2 is in E V E N . • Rule 2: If x is in E V E N , then so is x + 2. • Rule 3: The only elements in the set E V E N are those that can be produced from the two rules above.
• Note we just state Rule 3 here once only, and it will be tacitly presumed in all recursive definitions.
Obaid Ullah
• To prove that 8 ∈ E V E N , we can proceed as follows: • By Rule 1, we know that 2 ∈ E V E N . • By Rule 2, we know that 2 + 2 = 4 ∈ E V E N . • Again by Rule 2, we have 4 + 2 = 6 ∈ E V E N . • Reapply Rule 2, we derive that 6 + 2 = 8 ∈ E V E N .
4
Obaid Ullah
5
• The set E V E N can also be defined recursively by these two rules: • Rule 1: 2 is in E V E N . • Rule 2: If x and y are both in E V E N , then so is x + y. • It should be understood that we can apply Rule 2 also to the case where x and y are the same number.
Obaid Ullah
• This recursive definition of E V E N is better than the previous one because it produces shorter proofs that elements are in E V E N . • For instance, we can show that 8 ∈ E V E N in fewer steps: • By Rule 1, we have 2 ∈ E V E N . • By Rule 2 with x = 2 and y = 2, we have 2 + 2 = 4 ∈ E V E N . • By Rule 2 with x = 4 and y = 4, we have 4 + 4 = 8 ∈ E V E N .
6
Obaid Ullah
7
Example • Recall that a polynomial is a finite sum of terms, each of which is in the form of a real number times a power of x (that may be 0 x = 1) .
• Let us recursively define the set POLY NOMIAL by these three rules:
• Rule 1: Any (real) number is in POLY NOMIAL. • Rule 2: The variable x is in POLYNOMIAL . • Rule 3: If p and q are in POLY NOMIAL, then so are ( p), p + q , p − q , and pq.
Obaid Ullah
8
We can apply the above rules to show that 3x2 + 7x − 9 is in POLY NOMIAL:
• By Rule 1, 3 ∈ POLY NOMIAL. • By Rule 2, x ∈ P OLY NOMIAL. • By Rule 3, (3)(x) ∈ POLY NOMIAL; call it 3x. • By Rule 3, (3x)(x) ∈ POLY NOMIAL; call it 3x2 . • By Rule 1, 7 ∈ POLY NOMIAL. • By Rule 3, (7)(x) ∈ POLY NOMIAL; call it 7x. • By Rule 3, 3x2 + 7x ∈ P OLY NOMIAL. • By Rule 1, −9 ∈ POLY NOMIAL. • By Rule 3, 3x2 + 7x + (−9) = 3x2 + 7x − 9 ∈ POLY NOMIAL.
Obaid Ullah
9
Example We can use recursive definitions to define the languages that we defined in Chapter 2, e.g., L1 , L2 , etc. as follows:
• Rule 1: x is in L1 . Rule 2: If w is any word in L1 , then xw is also in L1 . Hence, L1 = Σ+ = {x, xx, xxx, ... }
• Rule 1: Λ is in L4 . Rule 2: If w is any word in L4 , then xw is also in L4 . Thus, L4 = Σ = {Λ,x,xx,xxx,...} ∗
• Rule 1: x is in L2 Rule 2: If w is any word in L2 , then xxw is also in L2 Thus, L2 = {xodd } = {x, xxx, xxxxx, ... }.
Obaid Ullah
10
Example • The Kleene closure can also be defined recursively: • Rule 1: If S is a language, then all the words of S are in S . ∗
• Rule 2: Λ is in S . ∗
• Rule 3: If x and y are in S , then so is their concatenation xy. ∗
Obaid Ullah
11
Arithmetic Expressions • Suppose we ask ourselves what constitutes a valid arithmetic expression, or AE for short. • The alphabet for this language is Σ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, −, ∗, /, (, )}
Obaid Ullah
12
• Obviously, the following expressions are not valid: (3 + 5) + 6)
2(/8 + 9)
(3 + (4−)8)
• The first contains unbalanced parentheses; the second contains the forbidden substring (/; the third contains the forbidden substring −). • Are there more rules? The substrings // and ∗/ are also forbidden. Are there still more?
• The most natural way of defining a valid AE is by using a recursive definition, rather than a long list of forbidden substrings.
Obaid Ullah
13
Recursive Definition of AE • Rule 1: Any number (positive, negative, or zero) is in AE . • Rule 2: If x is in AE , then so are (i) (x) (ii) −x (provided that x does not already start with a minus sign)
• Rule 3: If x and y are in AE , then so are (i) x + y (if the first symbol in y is not + or −) (ii) x − y (if the first symbol in y is not + or −) (iii) x ∗ y (iv) x/y (v) x ∗ ∗y (our notation for exponentiation)
Obaid Ullah
14
• The above definition is the most natural, because it is the method we use to recognize valid arithmetic expressions in real life.
• For instance, we wish to determine if the following expression is valid:
(2 + 4) ∗ (7 ∗ (9 − 3)/4)/4 ∗ (2 + 8) − 1
• We do not really scan over the string, looking for forbidden substrings or count the parentheses.
• We actually imagine the expression in our mind broken down into components:
– Is (2 + 4) OK? Yes – Is (9 − 3) OK? Yes – Is 7 ∗ (9 − 3)/4 OK? Yes, and so on.
Obaid Ullah
15
• Note that the recursive definition of the set AE gives us the possibility of writing 8/4/2, which is ambiguous, because it could mean 8/(4/2) = 4 or (8/4)/2 = 1. • However, the ambiguity of 8/4/2 is a problem of meaning. There is no doubt that this string is a word in AE , only doubt about what it means.
• By applying Rule 2, we could always put enough parentheses to avoid such a confusion.
• The recursive definition of the set AE is useful for proving many theorems about arithmetic expressions, as we shall see in the next few slides.
Obaid Ullah
16
Theorem 2 An arithmetic expression cannot contain the character $. Proof
• This character is not part of any number, so it cannot be introduced into an AE by Rule 1. • If the character string x does not contain the character $, then neither do the string (x) and −x. So, the character $ cannot be introduced into an AE by Rule 2. • If neither x nor y contains the character $, then neither do any of the expressions defined in Rule 3.
• Therefore, the character $ can never get into an AE .
Obaid Ullah
17
Theorem 3 No arithmetic expression can begin or end with the symbol /. Proof
• No number begins or ends with this symbol, so it can not occur by Rule 1.
• Any arithmetic expression formed by Rule 2 must begin and end with parentheses or begin with a minus sign, so an expression beginning or ending with the / symbol cannot be introduced into AE by Rule 2.
• If x does not begin with a / and y does not end with a /, then any AE formed by any clause in Rule 3 will not begin or end with a /.
• Therefore, no arithmetic expression can begin or end with the symbol /.
Obaid Ullah
18
Theorem 4 No arithmetic expression can contain the substring //. Proof
• A direct proof similar to the above theorems can be given. For variation, however, we shall prove this theorem by contradiction.
• Suppose that there were some arithmetic expressions that contain the substring //. Let w be the shortest of these expressions. • We know that w must be formed by some sequence of applications of Rule 1, 2, and 3. Our question is: Which was the last rule used in the production of w? We shall show that it must have been Rule 3(iv).
Obaid Ullah
19
Proof of Theorem 4 (cont.)
• If it were Rule 3(iii) for instance, then the // must either be found in the x part or the y part. Since x and y are in AE , this would mean that x or y is a shorter word than w that contains //, which contradicts the assumption that w is the shortest. • Similarly, we can eliminate all the other possibilities. Therefore, the last rule used to produce w must have been Rule 3(iv). • Now, since the // cannot have been contributed to w from the x part alone or from the y part alone (or else x or y would be the shortest word in AE with a double slash), it must have been included by either the x part that ended in a /, or by the y part that began with a /. But both x and y are in AE and Theorem 3 says that neither case can happen.
• Therefore, even Rule 3(iv) cannot introduce the substring //. Hence, w cannot be in the set AE or no expression in AE can have //.
Obaid Ullah
20
Propositional Calculus • Propositional calculus (or sentential calculus) is a branch of symbolic logic that we shall be interested in.
• The version we define here uses only negation ( ¬) and implication (→), together with the phrase variables. • The alphabet for this language is Σ = {¬, →, (, ),a,b,c,d,...}
• A valid expression in this language is called WFF ( well-form formula).
Obaid Ullah
21
Propositional Calculus (cont.) • The rules for forming WFFs are: Rule 1: Any single Latin letter is a WFF, for instance a, b, c, ... Rule 2: If p is a WFF, then so are ( p) and ¬ p. Rule 3: If p and q are WFFs, then so is p → q.
• Can you show that p → (( p → p) → q) is a WFF? • Can you show that the following are NOT WFFs? p →
→p p) → p(