Sie sind auf Seite 1von 11

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, we specify some basic objects in the set.
2. Second, we give rules for constructing more objects in the set
from the basic ones we already know.
3. Third, we declare that no objects except those constructed 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 EV EN , using


recursive definition:
• Rule 1: 2 is in EV EN .

• Rule 2: If x is in EV EN , then so is x + 2.
• Rule 3: The only elements in the set EV EN 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 4

• To prove that 8 ∈ EV EN , we can proceed as follows:


• By Rule 1, we know that 2 ∈ EV EN .
• By Rule 2, we know that 2 + 2 = 4 ∈ EV EN .

• Again by Rule 2, we have 4 + 2 = 6 ∈ EV EN .


• Reapply Rule 2, we derive that 6 + 2 = 8 ∈ EV EN .
Obaid Ullah 5

• The set EV EN can also be defined recursively by these two rules:


• Rule 1: 2 is in EV EN .

• Rule 2: If x and y are both in EV EN , 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 6

• This recursive definition of EV EN is better than the previous one


because it produces shorter proofs that elements are in EV EN .

• For instance, we can show that 8 ∈ EV EN in fewer steps:


• By Rule 1, we have 2 ∈ EV EN .
• By Rule 2 with x = 2 and y = 2, we have 2 + 2 = 4 ∈ EV EN .

• By Rule 2 with x = 4 and y = 4, we have 4 + 4 = 8 ∈ EV EN .


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
x0 = 1).
• Let us recursively define the set P OLY N OM IAL by these three
rules:

• Rule 1: Any (real) number is in P OLY N OM IAL.


• Rule 2: The variable x is in P OLY N OM IAL.
• Rule 3: If p and q are in P OLY N OM IAL, 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


P OLY N OM IAL:
• By Rule 1, 3 ∈ P OLY N OM IAL.
• By Rule 2, x ∈ P OLY N OM IAL.

• By Rule 3, (3)(x) ∈ P OLY N OM IAL; call it 3x.


• By Rule 3, (3x)(x) ∈ P OLY N OM IAL; call it 3x2 .
• By Rule 1, 7 ∈ P OLY N OM IAL.

• By Rule 3, (7)(x) ∈ P OLY N OM IAL; call it 7x.


• By Rule 3, 3x2 + 7x ∈ P OLY N OM IAL.

• By Rule 1, −9 ∈ P OLY N OM IAL.


• By Rule 3, 3x2 + 7x + (−9) = 3x2 + 7x − 9 ∈ P OLY N OM IAL.
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(

Das könnte Ihnen auch gefallen