Lesson

Words: Inductive Definitions and Operations

Words defined inductively (length, letter count, concatenation, powers, reverse), and why every yes/no question about text is a question about a language.

Last updated:


A compiler (the tool that translates a program into machine instructions) receives the text total_2 = 7.
Before it can translate anything, it must answer a much simpler question:
is total_2 a legal name? Is 2total one? A login form asks the same kind of question about a password, and a search box about a date.
If the check is wrong, the compiler accepts a faulty program or rejects a correct one, and the form rejects a valid password or accepts one that breaks the rules.

Try it yourself: Python checks names before it runs a program.
Run the cell, then change the name total_2 (in both lines) to 2total, to total-2 and to class.
Before each run, predict whether Python will accept the name.

total_2 = 7
print(total_2)
7

For each name Python gives one of two answers: yes, the name is legal and the program runs, or no, it stops with a SyntaxError.

Decision problems

Each of these questions is a yes/no question about a piece of text, and such a question is called a decision problem.

The goal of this course is to build computational models, that is, machines defined precisely by mathematical definitions, so that we can characterize which problems a computer can solve.

A decision problem is given by a predicate: a statement P(w)P(w) about a word ww that is either true or false, for example P(w)P(w) = ”ww is a legal name”.
Let LPL_P be the language of all the words that satisfy the predicate:

LP={w∣P(w)}L_P=\{w \mid P(w)\}

Once the input is written as a word, deciding the problem is the same as answering a membership problem:
given a word ww⁠, is w∈LPw\in L_P⁠? Conversely, every language LL poses such a question, with the predicate P(w)P(w) = "w∈Lw\in L".
For the opening question, LPL_P is the set of legal names: total_2 belongs to it and 2total does not.
What do the names Python accepted have in common? Describing that is describing LPL_P⁠.

Defining an infinite set

From the discrete mathematics course we know the inductive definition of an infinite set (starting elements and rules that build new elements from old ones) and the definition of a function by recursion along such a definition, and this is how we define words and the operations on them.

Alphabet and words

An alphabet, written Σ\Sigma⁠, is a finite, nonempty set of letters, for example Σ={a,b}\Sigma=\{a,b\} or Σ={0,1}\Sigma=\{0,1\}⁠.
Real alphabets include the bits of a file, the four DNA letters {A,C,G,T}\{A,C,G,T\}⁠, and the characters of Unicode.
When we speak about an arbitrary letter, as with a variable, we write a lowercase Latin letter such as aa⁠.

The empty word is written ε\varepsilon (epsilon). It is like "" in programming:
a real word with no letters, not “nothing”.

Let Σ∗\Sigma^* denote the set of all words over Σ\Sigma⁠, that is, the set of all strings that can be formed from the letters of Σ\Sigma⁠.
Look at the words over Σ={a,b}\Sigma=\{a,b\} by their number of letters:

Number of lettersWords
0ε\varepsilon
1a, ba,\ b
2aa, ab, ba, bbaa,\ ab,\ ba,\ bb

Formulate it yourselves. How do we build all the words, starting from ε\varepsilon and adding one letter at a time?
Write an inductive definition in your own words: what do we start from, and which rule creates new words from words already built?

Only after you have written a definition of your own, open the candidate definitions and choose the one you think is right:

Candidate definitions

In every option u,v∈Σ∗u,v\in\Sigma^* are words and a∈Σa\in\Sigma is a letter.

A: base Σ⊆Σ∗\Sigma\subseteq\Sigma^*; rule: if u∈Σ∗u\in\Sigma^* then ua∈Σ∗ua\in\Sigma^*

The thinking: start from the letters themselves and append one letter at each step.

The mistake: the empty word is not in the base and cannot be reached from any word, so ε∉Σ∗\varepsilon\notin\Sigma^*⁠.
But ε\varepsilon is a word (like "" in programming). This definition gives the set of nonempty words.

B: base ε∈Σ∗\varepsilon\in\Sigma^*; rule: if u∈Σ∗u\in\Sigma^* then ua∈Σ∗ua\in\Sigma^*

The thinking: start from the empty word, and at each step append one letter on the right of a word already built.

This is the definition we wanted. Check on abab⁠: ε\varepsilon gives a=εaa=\varepsilon a⁠, and aa gives abab⁠.
Appending on the left gives the same set; by convention we append on the right.

C: base ε∈Σ∗\varepsilon\in\Sigma^*; rule: if u∈Σ∗u\in\Sigma^* then a∈Σ∗a\in\Sigma^*

The thinking: from ε\varepsilon we reach the letters.

The mistake: the rule does not use uu⁠, only the letter, so it creates only ε\varepsilon and the single letters.
The word abab is never created.

D: base ε∈Σ∗\varepsilon\in\Sigma^*; rule: if u,v∈Σ∗u,v\in\Sigma^* then uv∈Σ∗uv\in\Sigma^*

The thinking: glue two words that were already built.

The mistake: the rule glues existing words and the base holds only ε\varepsilon⁠.
Gluing ε\varepsilon to ε\varepsilon gives ε\varepsilon again, so the set is just {ε}\{\varepsilon\}⁠.
The rule never introduces letters.

Hint 1

Build the table by hand: from ε\varepsilon we reach aa and bb⁠.
From which words, and in which step, do we reach abab and baba⁠?

Hint 2

An inductive definition needs two parts: a base word that exists without being built, and a rule that takes a word uu already built and a letter aa and computes a new word from them.

The official definition
  • base: ε∈Σ∗\varepsilon\in\Sigma^*⁠;
  • rule: if u∈Σ∗u\in\Sigma^* and a∈Σa\in\Sigma then ua∈Σ∗ua\in\Sigma^* (append one letter on the right).

In the formula sheets the definition is written with the inductive-set notation I(B,F)I(B,F)⁠, the set generated from a base set BB by generating functions FF⁠, as in the discrete mathematics course.
The same notation also gives Σ+\Sigma^+⁠, the set of nonempty words:

Σ∗=I({ε}, {f:Σ∗×Σ→Σ∗∣f(w,a)=w⋅a})Σ+=I(Σ, {f:Σ∗×Σ→Σ∗∣f(w,a)=w⋅a})\begin{aligned} \Sigma^*&=I\bigl(\{\varepsilon\},\ \{f:\Sigma^*\times\Sigma\to\Sigma^*\mid f(w,a)=w\cdot a\}\bigr)\\ \Sigma^+&=I\bigl(\Sigma,\ \{f:\Sigma^*\times\Sigma\to\Sigma^*\mid f(w,a)=w\cdot a\}\bigr) \end{aligned}

The base of Σ∗\Sigma^* is the empty word, the base of Σ+\Sigma^+ is the single letters, and the one generating function appends a letter on the right.

Every nonempty word can be written uniquely as w=uaw=ua with uu a word and aa its last letter.
Functions on words are defined by recursion on this last letter.
The set of nonempty words is written Σ+\Sigma^+ and defined by Σ+=Σ∗−{ε}\Sigma^+=\Sigma^*-\{\varepsilon\}⁠.

Check yourself. Answer each part before you open its answer.

(a) Is ε∈Σ∗\varepsilon\in\Sigma^*⁠?

Answer

Yes: it is the base case of the definition.

(b) List all the words in {a,b}∗\{a,b\}^* that have at most two letters.

Answer

ε,a,b,aa,ab,ba,bb\varepsilon, a, b, aa, ab, ba, bb⁠: seven words. From ε\varepsilon we append a letter on the right, and then one more letter on the right.

(c) Is ba∈{a,b}∗ba\in\{a,b\}^*⁠? Build it from the definition.

Answer

Yes. When Σ={a,b}\Sigma=\{a,b\}⁠: ε∈Σ∗\varepsilon\in\Sigma^* by the base case, then b=ε⋅b∈Σ∗b=\varepsilon\cdot b\in\Sigma^* by the rule, and then ba=b⋅a∈Σ∗ba=b\cdot a\in\Sigma^* by the rule.

The length of a word

Length: let us build it together. The length of a word ww is the number of its letters, written ∣w∣|w| (what len(s) returns for a string s in Python).
Some examples first:

wordε\varepsilonaaabababbabb
length0123

Question 1. Every nonempty word has the form w=uaw=ua (a word u∈Σ∗u\in\Sigma^* followed by one last letter a∈Σa\in\Sigma⁠).
Suppose you already know the length of uu⁠. What is the length of ww⁠?
And what should the length of ε\varepsilon be? Write down a definition in your own words before you open anything below.

Only after you have written a definition of your own, open the candidate definitions and choose the one you think is right:

Candidate definitions

In every option u∈Σ∗u\in\Sigma^* is a word and a∈Σa\in\Sigma is a letter.

A: ∣w∣={0w=ε∣u∣w=ua|w|=\begin{cases}0 & w=\varepsilon\\ |u| & w=ua\end{cases}

The thinking: the length of the word before the last letter, with the base value 0.

The mistakes: appending a letter never changes the length in this rule, so every word would have length 0.
Check it on aa⁠: it should be 1.

B: ∣w∣={1w=ε∣u∣+1w=ua|w|=\begin{cases}1 & w=\varepsilon\\ |u|+1 & w=ua\end{cases}

The thinking: the step rule is right, only the base value is different.

The mistakes: the empty word has no letters, so its length is 0.
With the base value 1, every word would be one too long: this rule gives ∣a∣=2|a|=2⁠.

C: ∣w∣={0w=ε∣u∣+∣a∣w=ua|w|=\begin{cases}0 & w=\varepsilon\\ |u|+|a| & w=ua\end{cases}

The thinking: the length of uaua is the length of uu plus the length of the letter aa⁠.

The mistakes: this is true, but it quietly uses ∣a∣=1|a|=1⁠, which is exactly the one-letter case we are defining.
It does not tell us why a letter has length 1, so it is not yet a definition.

D: ∣w∣={0w=ε∣u∣+1w=ua|w|=\begin{cases}0 & w=\varepsilon\\ |u|+1 & w=ua\end{cases}

The thinking: no letters in ε\varepsilon⁠, and each appended letter adds one.

This is the definition we want. Check it on a=εaa=\varepsilon a⁠: ∣a∣=∣ε∣+1=1|a|=|\varepsilon|+1=1⁠.

Hint 1

Compare abab with aa⁠, and abbabb with abab⁠. What changed in the word, and what changed in the length?

Hint 2

A recursive definition needs two parts: a value for the base word ε\varepsilon⁠, and a rule that gets the value for uaua from the value for uu⁠.

The official definition, and how to run it on a word

Written as cases, as in a formula sheet (here and below, uu is a word and aa a letter):

∣w∣={0w=ε∣u∣+1w=u⋅a∧u∈Σ∗∧a∈Σ|w|= \begin{cases} 0 & w=\varepsilon\\ |u|+1 & w=u\cdot a\wedge u\in\Sigma^*\wedge a\in\Sigma \end{cases}

For example:

∣abb∣=∣ab∣+1=(∣a∣+1)+1=((∣ε∣+1)+1)+1=0+3=3|abb|=|ab|+1=(|a|+1)+1=\bigl((|\varepsilon|+1)+1\bigr)+1=0+3=3

In each step the rule for the last letter replaces ∣ab∣|ab| by ∣a∣+1|a|+1⁠, then ∣a∣|a| by ∣ε∣+1|\varepsilon|+1⁠, and finally ∣ε∣=0|\varepsilon|=0 comes from the base case.

What if? Suppose we defined the length by recursion on the first letter instead, w=auw=au⁠, with the same two rules.
Would any word get a different length?

Answer

No. Both definitions count one for every letter, so they agree on every word.
Recursion on the last letter is only our convention, because words grow on the right in the inductive definition.

Counting a letter

The number of occurrences of the letter aa in the word ww is written #a(w)\#_a(w)⁠.
For example #a(baa)=2\#_a(baa)=2 and #b(baa)=1\#_b(baa)=1⁠.

Formulate. Define #a(w)\#_a(w) by recursion on the last letter, as we defined the length:
what is #a(ε)\#_a(\varepsilon)⁠? And if w=uxw=ux (with x∈Σx\in\Sigma⁠) and we already know #a(u)\#_a(u)⁠, what is #a(w)\#_a(w)⁠?

Hint

What changes in the count when the last letter xx is aa⁠, and what when it is another letter?

The official definition

Here uu is a word and cc is any letter other than aa⁠:

#a(w)={0w=ε#a(u)+1w=u⋅a∧u∈Σ∗#a(u)w=u⋅c∧u∈Σ∗∧c∈Σ−{a}\#_a(w)= \begin{cases} 0 & w=\varepsilon\\ \#_a(u)+1 & w=u\cdot a\wedge u\in\Sigma^*\\ \#_a(u) & w=u\cdot c\wedge u\in\Sigma^*\wedge c\in\Sigma-\{a\} \end{cases}

Question 2. Use the definition of #b\#_b⁠, answering each part before you open its answer.

(a) Which line of the definition applies to #b(abb)\#_b(abb) at the first step?

Answer

The middle line, because the last letter is bb⁠: #b(abb)=#b(ab)+1\#_b(abb)=\#_b(ab)+1⁠.

(b) How much is #b(abb)\#_b(abb)⁠?

Answer

Continue: #b(ab)=#b(a)+1\#_b(ab)=\#_b(a)+1⁠, then #b(a)=#b(ε)=0\#_b(a)=\#_b(\varepsilon)=0 (the last letter is a≠ba\neq b⁠, so the last line applies).
Total: 2.

Concatenation of words

Question 3. Concatenation places one word after another, and we usually write uvuv⁠.
The notation u⋅vu\cdot v means the same and is used when the operation must be visible.
Define w1⋅w2w_1\cdot w_2 by recursion on the second word: what is w1⋅εw_1\cdot\varepsilon⁠, and how do you get w1⋅(ua)w_1\cdot(ua) from w1⋅uw_1\cdot u⁠?

Hint

Try ab⋅baab\cdot ba⁠. Append the letters of the second word to abab one at a time.

The official definition
w1⋅w2={w1w2=ε(w1⋅u)⋅aw2=u⋅a∧u∈Σ∗∧a∈Σw_1\cdot w_2= \begin{cases} w_1 & w_2=\varepsilon\\ (w_1\cdot u)\cdot a & w_2=u\cdot a\wedge u\in\Sigma^*\wedge a\in\Sigma \end{cases}

Order matters: ab⋅ba=abbaab\cdot ba=abba but ba⋅ab=baabba\cdot ab=baab⁠.

Why the length of a concatenation is the sum of the lengths (proof by induction)

For example ∣ab⋅ba∣=∣abba∣=4=2+2=∣ab∣+∣ba∣|ab\cdot ba|=|abba|=4=2+2=|ab|+|ba|⁠. We prove that this holds for all words.

Claim. For all words u,vu,v⁠: ∣uv∣=∣u∣+∣v∣|uv|=|u|+|v|⁠.

Proof, by induction on vv (with uu an arbitrary fixed word).
Let P(v)P(v) be the statement ∣uv∣=∣u∣+∣v∣|uv|=|u|+|v|⁠.

Base case, v=εv=\varepsilon⁠. Then uv=uuv=u⁠, so ∣uv∣=∣u∣=∣u∣+0=∣u∣+∣ε∣|uv|=|u|=|u|+0=|u|+|\varepsilon|⁠.

Induction hypothesis. Assume P(v)P(v) holds for some word vv⁠.

Induction step. We prove P(va)P(va) for every letter a∈Σa\in\Sigma⁠:

∣u⋅va∣=∣(uv)a∣=∣uv∣+1=∣u∣+∣v∣+1=∣u∣+∣va∣|u\cdot va|=|(uv)a|=|uv|+1=|u|+|v|+1=|u|+|va|

The first equality is the definition of concatenation, the second and the last are the definition of length, and the third is the induction hypothesis.

Conclusion. Every word is ε\varepsilon or is built from ε\varepsilon by appending letters, so P(v)P(v) holds for every word vv⁠.

After concatenation and length we can return to the question we opened with:
what the names that Python accepted have in common. Suppose Σ\Sigma is the alphabet of the Latin letters, the digits and the underscore _, and we ignore reserved words such as class.

Formulate. Write a rule on a word w∈Σ∗w\in\Sigma^* that says when it is a legal name.
What must hold for its first letter, and what for its length?

Hint

Compare total_2 with 2total: how do they differ? And what about the empty word?

Answer
LP={w∈Σ∗ ∣ w=a⋅u ∧ u∈Σ∗ ∧a∈Σ−{0,1,…,9}}\begin{aligned} L_P=\bigl\{w\in\Sigma^*\ \bigm|\ {}&w=a\cdot u\ \wedge\ u\in\Sigma^*\ \wedge\\ &a\in\Sigma-\{0,1,\ldots,9\}\bigr\} \end{aligned}

That is, the name is not empty and its first letter is not a digit.
total_2 belongs to the language (the first letter is tt⁠), 2total does not (its first letter is a digit), and the empty word does not (it has no first letter).

Powers of a word

Here n∈Nn\in\mathbb{N}⁠, where N={0,1,2,…}\mathbb{N}=\{0,1,2,\ldots\} includes zero and N+\mathbb{N}^+ means the positive numbers.

Question 4. We want (ab)3=ababab(ab)^3=ababab⁠. The power wnw^n is ww repeated nn times.
Define wnw^n by recursion on nn⁠: what is w0w^0⁠, and how do you get wnw^n from wn−1w^{n-1}⁠?

Hint

Compare (ab)3(ab)^3 with (ab)2(ab)^2⁠. What is w0w^0 if repeating zero times should change nothing under concatenation?

The official definition wn={εn=0wn−1⋅wn∈N+w^n= \begin{cases} \varepsilon & n=0\\ w^{n-1}\cdot w & n\in\mathbb{N}^+ \end{cases}

Question 5. Answer each part before you open its answer.

(a) What is a0a^0⁠?

Answer

a0=εa^0=\varepsilon⁠: zero repetitions give the empty word.

(b) What is (ab)2(ab)^2⁠?

Answer

(ab)2=abab(ab)^2=abab⁠.

(c) What is ∣wn∣|w^n| for a word ww⁠?

Answer

Each repetition adds ∣w∣|w| letters, so ∣wn∣=n⋅∣w∣|w^n|=n\cdot|w|⁠.

The reverse of a word

Question 6. The reverse wRw^R lists the letters backwards, so (abb)R=bba(abb)^R=bba and (ab)R=ba(ab)^R=ba⁠.
Suppose w=uaw=ua with u∈Σ∗u\in\Sigma^* and a∈Σa\in\Sigma⁠, and you already know uRu^R⁠.
How do you build wRw^R from it? What is εR\varepsilon^R⁠?

Hint

Where does the last letter aa of ww end up in wRw^R⁠:
at the beginning or at the end? Test on abab⁠, with u=au=a⁠.

The official definition
wR={εw=εa⋅uRw=u⋅a∧u∈Σ∗∧a∈Σw^R= \begin{cases} \varepsilon & w=\varepsilon\\ a\cdot u^R & w=u\cdot a\wedge u\in\Sigma^*\wedge a\in\Sigma \end{cases}

so:

(abb)R=b⋅(ab)R(abb=ab⋅b)=b⋅b⋅(a)R(ab=a⋅b)=b⋅b⋅a⋅εR(a=ε⋅a)=bba⋅ε(εR=ε)=bba\begin{aligned} (abb)^R&=b\cdot(ab)^R && (abb=ab\cdot b)\\ &=b\cdot b\cdot(a)^R && (ab=a\cdot b)\\ &=b\cdot b\cdot a\cdot\varepsilon^R && (a=\varepsilon\cdot a)\\ &=bba\cdot\varepsilon && (\varepsilon^R=\varepsilon)\\ &=bba \end{aligned}

Question 7. What is (uv)R(uv)^R in terms of uRu^R and vRv^R⁠?

Hint

Try u=au=a and v=bv=b⁠. What is (ab)R(ab)^R⁠, and what is aRbRa^Rb^R⁠?

Answer

(uv)R=vRuR(uv)^R=v^Ru^R⁠: the order is reversed. (ab)R=ba(ab)^R=ba and bRaR=bab^Ra^R=ba⁠, whereas aRbR=aba^Rb^R=ab is different.

Proof by induction of the rule for the reverse of a concatenation

Claim. For all words u,vu,v⁠: (uv)R=vRuR(uv)^R=v^Ru^R⁠.

Proof, by induction on vv (with uu an arbitrary fixed word).
Let P(v)P(v) be the statement (uv)R=vRuR(uv)^R=v^Ru^R⁠.

Base case, v=εv=\varepsilon⁠. Then uv=uuv=u⁠, so (uv)R=uR=ε⋅uR=εRuR=vRuR(uv)^R=u^R=\varepsilon\cdot u^R=\varepsilon^Ru^R=v^Ru^R⁠.

Induction hypothesis. Assume P(v)P(v) holds for some word vv⁠.

Induction step. We prove P(va)P(va) for every letter a∈Σa\in\Sigma⁠:

(u⋅va)R=((uv)a)R=a⋅(uv)R=a⋅vRuR=(va)RuR(u\cdot va)^R=((uv)a)^R=a\cdot(uv)^R=a\cdot v^Ru^R=(va)^Ru^R

The first equality is the definition of concatenation, the second and the last are the definition of reverse (wR=a⋅xRw^R=a\cdot x^R for w=xaw=xa⁠), and the third is the induction hypothesis.

Conclusion. Every word is ε\varepsilon or is built from ε\varepsilon by appending letters, so P(v)P(v) holds for every word vv⁠.

Counting words

For an alphabet with two letters, such as Σ={a,b}\Sigma=\{a,b\}⁠, there are 2n2^n words of length nn⁠:
two choices for each of the nn positions. So Σ∗\Sigma^* is infinite although every word in it is finite.

Question 8. Answer each part before you open its answer.

(a) How many words of length 33 are there over {0,1}\{0,1\}⁠?

Answer

There are 23=82^3=8 of them: two choices for each of the three positions.

(b) How many words of length at most 33 are there?

Answer

Adding the lengths 0,1,2,30,1,2,3 gives 1+2+4+8=151+2+4+8=15 words.

See Languages: operations and equalities, where we collect words into languages.